0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/bitfield.h>
0009 #include <linux/module.h>
0010 #include <linux/pci.h>
0011
0012 #include "rvu_struct.h"
0013 #include "rvu_reg.h"
0014 #include "rvu.h"
0015 #include "npc.h"
0016 #include "cgx.h"
0017 #include "npc_profile.h"
0018 #include "rvu_npc_hash.h"
0019
0020 #define RSVD_MCAM_ENTRIES_PER_PF 3
0021 #define RSVD_MCAM_ENTRIES_PER_NIXLF 1
0022
0023 #define NPC_PARSE_RESULT_DMAC_OFFSET 8
0024 #define NPC_HW_TSTAMP_OFFSET 8ULL
0025 #define NPC_KEX_CHAN_MASK 0xFFFULL
0026 #define NPC_KEX_PF_FUNC_MASK 0xFFFFULL
0027
0028 #define ALIGN_8B_CEIL(__a) (((__a) + 7) & (-8))
0029
0030 static const char def_pfl_name[] = "default";
0031
0032 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
0033 int blkaddr, u16 pcifunc);
0034 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
0035 u16 pcifunc);
0036
0037 bool is_npc_intf_tx(u8 intf)
0038 {
0039 return !!(intf & 0x1);
0040 }
0041
0042 bool is_npc_intf_rx(u8 intf)
0043 {
0044 return !(intf & 0x1);
0045 }
0046
0047 bool is_npc_interface_valid(struct rvu *rvu, u8 intf)
0048 {
0049 struct rvu_hwinfo *hw = rvu->hw;
0050
0051 return intf < hw->npc_intfs;
0052 }
0053
0054 int rvu_npc_get_tx_nibble_cfg(struct rvu *rvu, u64 nibble_ena)
0055 {
0056
0057
0058
0059 if (is_rvu_96xx_B0(rvu))
0060 return nibble_ena;
0061 return 0;
0062 }
0063
0064 static int npc_mcam_verify_pf_func(struct rvu *rvu,
0065 struct mcam_entry *entry_data, u8 intf,
0066 u16 pcifunc)
0067 {
0068 u16 pf_func, pf_func_mask;
0069
0070 if (is_npc_intf_rx(intf))
0071 return 0;
0072
0073 pf_func_mask = (entry_data->kw_mask[0] >> 32) &
0074 NPC_KEX_PF_FUNC_MASK;
0075 pf_func = (entry_data->kw[0] >> 32) & NPC_KEX_PF_FUNC_MASK;
0076
0077 pf_func = be16_to_cpu((__force __be16)pf_func);
0078 if (pf_func_mask != NPC_KEX_PF_FUNC_MASK ||
0079 ((pf_func & ~RVU_PFVF_FUNC_MASK) !=
0080 (pcifunc & ~RVU_PFVF_FUNC_MASK)))
0081 return -EINVAL;
0082
0083 return 0;
0084 }
0085
0086 void rvu_npc_set_pkind(struct rvu *rvu, int pkind, struct rvu_pfvf *pfvf)
0087 {
0088 int blkaddr;
0089 u64 val = 0;
0090
0091 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
0092 if (blkaddr < 0)
0093 return;
0094
0095
0096 val = pkind | 1ULL << 62;
0097 rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_CPI_DEFX(pkind, 0), val);
0098 }
0099
0100 int rvu_npc_get_pkind(struct rvu *rvu, u16 pf)
0101 {
0102 struct npc_pkind *pkind = &rvu->hw->pkind;
0103 u32 map;
0104 int i;
0105
0106 for (i = 0; i < pkind->rsrc.max; i++) {
0107 map = pkind->pfchan_map[i];
0108 if (((map >> 16) & 0x3F) == pf)
0109 return i;
0110 }
0111 return -1;
0112 }
0113
0114 #define NPC_AF_ACTION0_PTR_ADVANCE GENMASK_ULL(27, 20)
0115
0116 int npc_config_ts_kpuaction(struct rvu *rvu, int pf, u16 pcifunc, bool enable)
0117 {
0118 int pkind, blkaddr;
0119 u64 val;
0120
0121 pkind = rvu_npc_get_pkind(rvu, pf);
0122 if (pkind < 0) {
0123 dev_err(rvu->dev, "%s: pkind not mapped\n", __func__);
0124 return -EINVAL;
0125 }
0126
0127 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, pcifunc);
0128 if (blkaddr < 0) {
0129 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
0130 return -EINVAL;
0131 }
0132
0133 val = rvu_read64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind));
0134 val &= ~NPC_AF_ACTION0_PTR_ADVANCE;
0135
0136 if (enable)
0137 val |= FIELD_PREP(NPC_AF_ACTION0_PTR_ADVANCE,
0138 NPC_HW_TSTAMP_OFFSET);
0139 rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind), val);
0140
0141 return 0;
0142 }
0143
0144 static int npc_get_ucast_mcam_index(struct npc_mcam *mcam, u16 pcifunc,
0145 int nixlf)
0146 {
0147 struct rvu_hwinfo *hw = container_of(mcam, struct rvu_hwinfo, mcam);
0148 struct rvu *rvu = hw->rvu;
0149 int blkaddr = 0, max = 0;
0150 struct rvu_block *block;
0151 struct rvu_pfvf *pfvf;
0152
0153 pfvf = rvu_get_pfvf(rvu, pcifunc);
0154
0155
0156
0157 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
0158 while (blkaddr) {
0159 if (pfvf->nix_blkaddr == blkaddr)
0160 break;
0161 block = &rvu->hw->block[blkaddr];
0162 max += block->lf.max;
0163 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
0164 }
0165
0166 return mcam->nixlf_offset + (max + nixlf) * RSVD_MCAM_ENTRIES_PER_NIXLF;
0167 }
0168
0169 int npc_get_nixlf_mcam_index(struct npc_mcam *mcam,
0170 u16 pcifunc, int nixlf, int type)
0171 {
0172 int pf = rvu_get_pf(pcifunc);
0173 int index;
0174
0175
0176 if (pf && !(pcifunc & RVU_PFVF_FUNC_MASK)) {
0177
0178 pf--;
0179 index = mcam->pf_offset + (pf * RSVD_MCAM_ENTRIES_PER_PF);
0180
0181
0182
0183 if (type == NIXLF_BCAST_ENTRY)
0184 return index;
0185 else if (type == NIXLF_ALLMULTI_ENTRY)
0186 return index + 1;
0187 else if (type == NIXLF_PROMISC_ENTRY)
0188 return index + 2;
0189 }
0190
0191 return npc_get_ucast_mcam_index(mcam, pcifunc, nixlf);
0192 }
0193
0194 int npc_get_bank(struct npc_mcam *mcam, int index)
0195 {
0196 int bank = index / mcam->banksize;
0197
0198
0199 if (mcam->keysize == NPC_MCAM_KEY_X2)
0200 return bank ? 2 : 0;
0201
0202 return bank;
0203 }
0204
0205 bool is_mcam_entry_enabled(struct rvu *rvu, struct npc_mcam *mcam,
0206 int blkaddr, int index)
0207 {
0208 int bank = npc_get_bank(mcam, index);
0209 u64 cfg;
0210
0211 index &= (mcam->banksize - 1);
0212 cfg = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_CFG(index, bank));
0213 return (cfg & 1);
0214 }
0215
0216 void npc_enable_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
0217 int blkaddr, int index, bool enable)
0218 {
0219 int bank = npc_get_bank(mcam, index);
0220 int actbank = bank;
0221
0222 index &= (mcam->banksize - 1);
0223 for (; bank < (actbank + mcam->banks_per_entry); bank++) {
0224 rvu_write64(rvu, blkaddr,
0225 NPC_AF_MCAMEX_BANKX_CFG(index, bank),
0226 enable ? 1 : 0);
0227 }
0228 }
0229
0230 static void npc_clear_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
0231 int blkaddr, int index)
0232 {
0233 int bank = npc_get_bank(mcam, index);
0234 int actbank = bank;
0235
0236 index &= (mcam->banksize - 1);
0237 for (; bank < (actbank + mcam->banks_per_entry); bank++) {
0238 rvu_write64(rvu, blkaddr,
0239 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1), 0);
0240 rvu_write64(rvu, blkaddr,
0241 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0), 0);
0242
0243 rvu_write64(rvu, blkaddr,
0244 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), 0);
0245 rvu_write64(rvu, blkaddr,
0246 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), 0);
0247
0248 rvu_write64(rvu, blkaddr,
0249 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), 0);
0250 rvu_write64(rvu, blkaddr,
0251 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), 0);
0252 }
0253 }
0254
0255 static void npc_get_keyword(struct mcam_entry *entry, int idx,
0256 u64 *cam0, u64 *cam1)
0257 {
0258 u64 kw_mask = 0x00;
0259
0260 #define CAM_MASK(n) (BIT_ULL(n) - 1)
0261
0262
0263
0264
0265
0266
0267 switch (idx) {
0268 case 0:
0269
0270 *cam1 = entry->kw[0];
0271 kw_mask = entry->kw_mask[0];
0272 break;
0273 case 1:
0274
0275 *cam1 = entry->kw[1] & CAM_MASK(48);
0276 kw_mask = entry->kw_mask[1] & CAM_MASK(48);
0277 break;
0278 case 2:
0279
0280
0281
0282 *cam1 = (entry->kw[1] >> 48) & CAM_MASK(16);
0283 *cam1 |= ((entry->kw[2] & CAM_MASK(48)) << 16);
0284 kw_mask = (entry->kw_mask[1] >> 48) & CAM_MASK(16);
0285 kw_mask |= ((entry->kw_mask[2] & CAM_MASK(48)) << 16);
0286 break;
0287 case 3:
0288
0289
0290
0291 *cam1 = (entry->kw[2] >> 48) & CAM_MASK(16);
0292 *cam1 |= ((entry->kw[3] & CAM_MASK(32)) << 16);
0293 kw_mask = (entry->kw_mask[2] >> 48) & CAM_MASK(16);
0294 kw_mask |= ((entry->kw_mask[3] & CAM_MASK(32)) << 16);
0295 break;
0296 case 4:
0297
0298
0299
0300 *cam1 = (entry->kw[3] >> 32) & CAM_MASK(32);
0301 *cam1 |= ((entry->kw[4] & CAM_MASK(32)) << 32);
0302 kw_mask = (entry->kw_mask[3] >> 32) & CAM_MASK(32);
0303 kw_mask |= ((entry->kw_mask[4] & CAM_MASK(32)) << 32);
0304 break;
0305 case 5:
0306
0307
0308
0309 *cam1 = (entry->kw[4] >> 32) & CAM_MASK(32);
0310 *cam1 |= ((entry->kw[5] & CAM_MASK(16)) << 32);
0311 kw_mask = (entry->kw_mask[4] >> 32) & CAM_MASK(32);
0312 kw_mask |= ((entry->kw_mask[5] & CAM_MASK(16)) << 32);
0313 break;
0314 case 6:
0315
0316
0317
0318 *cam1 = (entry->kw[5] >> 16) & CAM_MASK(48);
0319 *cam1 |= ((entry->kw[6] & CAM_MASK(16)) << 48);
0320 kw_mask = (entry->kw_mask[5] >> 16) & CAM_MASK(48);
0321 kw_mask |= ((entry->kw_mask[6] & CAM_MASK(16)) << 48);
0322 break;
0323 case 7:
0324
0325 *cam1 = (entry->kw[6] >> 16) & CAM_MASK(48);
0326 kw_mask = (entry->kw_mask[6] >> 16) & CAM_MASK(48);
0327 break;
0328 }
0329
0330 *cam1 &= kw_mask;
0331 *cam0 = ~*cam1 & kw_mask;
0332 }
0333
0334 static void npc_fill_entryword(struct mcam_entry *entry, int idx,
0335 u64 cam0, u64 cam1)
0336 {
0337
0338
0339
0340 switch (idx) {
0341 case 0:
0342 entry->kw[0] = cam1;
0343 entry->kw_mask[0] = cam1 ^ cam0;
0344 break;
0345 case 1:
0346 entry->kw[1] = cam1;
0347 entry->kw_mask[1] = cam1 ^ cam0;
0348 break;
0349 case 2:
0350 entry->kw[1] |= (cam1 & CAM_MASK(16)) << 48;
0351 entry->kw[2] = (cam1 >> 16) & CAM_MASK(48);
0352 entry->kw_mask[1] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
0353 entry->kw_mask[2] = ((cam1 ^ cam0) >> 16) & CAM_MASK(48);
0354 break;
0355 case 3:
0356 entry->kw[2] |= (cam1 & CAM_MASK(16)) << 48;
0357 entry->kw[3] = (cam1 >> 16) & CAM_MASK(32);
0358 entry->kw_mask[2] |= ((cam1 ^ cam0) & CAM_MASK(16)) << 48;
0359 entry->kw_mask[3] = ((cam1 ^ cam0) >> 16) & CAM_MASK(32);
0360 break;
0361 case 4:
0362 entry->kw[3] |= (cam1 & CAM_MASK(32)) << 32;
0363 entry->kw[4] = (cam1 >> 32) & CAM_MASK(32);
0364 entry->kw_mask[3] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
0365 entry->kw_mask[4] = ((cam1 ^ cam0) >> 32) & CAM_MASK(32);
0366 break;
0367 case 5:
0368 entry->kw[4] |= (cam1 & CAM_MASK(32)) << 32;
0369 entry->kw[5] = (cam1 >> 32) & CAM_MASK(16);
0370 entry->kw_mask[4] |= ((cam1 ^ cam0) & CAM_MASK(32)) << 32;
0371 entry->kw_mask[5] = ((cam1 ^ cam0) >> 32) & CAM_MASK(16);
0372 break;
0373 case 6:
0374 entry->kw[5] |= (cam1 & CAM_MASK(48)) << 16;
0375 entry->kw[6] = (cam1 >> 48) & CAM_MASK(16);
0376 entry->kw_mask[5] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
0377 entry->kw_mask[6] = ((cam1 ^ cam0) >> 48) & CAM_MASK(16);
0378 break;
0379 case 7:
0380 entry->kw[6] |= (cam1 & CAM_MASK(48)) << 16;
0381 entry->kw_mask[6] |= ((cam1 ^ cam0) & CAM_MASK(48)) << 16;
0382 break;
0383 }
0384 }
0385
0386 static u64 npc_get_default_entry_action(struct rvu *rvu, struct npc_mcam *mcam,
0387 int blkaddr, u16 pf_func)
0388 {
0389 int bank, nixlf, index;
0390
0391
0392 nix_get_nixlf(rvu, pf_func, &nixlf, NULL);
0393 index = npc_get_nixlf_mcam_index(mcam, pf_func, nixlf,
0394 NIXLF_UCAST_ENTRY);
0395 bank = npc_get_bank(mcam, index);
0396 index &= (mcam->banksize - 1);
0397
0398 return rvu_read64(rvu, blkaddr,
0399 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
0400 }
0401
0402 static void npc_fixup_vf_rule(struct rvu *rvu, struct npc_mcam *mcam,
0403 int blkaddr, int index, struct mcam_entry *entry,
0404 bool *enable)
0405 {
0406 struct rvu_npc_mcam_rule *rule;
0407 u16 owner, target_func;
0408 struct rvu_pfvf *pfvf;
0409 u64 rx_action;
0410
0411 owner = mcam->entry2pfvf_map[index];
0412 target_func = (entry->action >> 4) & 0xffff;
0413
0414 if (is_pffunc_af(owner) || is_afvf(target_func) ||
0415 (owner & RVU_PFVF_FUNC_MASK) ||
0416 !(target_func & RVU_PFVF_FUNC_MASK))
0417 return;
0418
0419
0420 pfvf = rvu_get_pfvf(rvu, target_func);
0421 mcam->entry2target_pffunc[index] = target_func;
0422
0423
0424 if (!(is_nixlf_attached(rvu, target_func) &&
0425 test_bit(NIXLF_INITIALIZED, &pfvf->flags)))
0426 *enable = false;
0427
0428
0429 list_for_each_entry(rule, &mcam->mcam_rules, list) {
0430 if (rule->entry == index)
0431 return;
0432 }
0433
0434
0435 rx_action = npc_get_default_entry_action(rvu, mcam, blkaddr,
0436 target_func);
0437 if (rx_action)
0438 entry->action = rx_action;
0439 }
0440
0441 static void npc_config_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
0442 int blkaddr, int index, u8 intf,
0443 struct mcam_entry *entry, bool enable)
0444 {
0445 int bank = npc_get_bank(mcam, index);
0446 int kw = 0, actbank, actindex;
0447 u8 tx_intf_mask = ~intf & 0x3;
0448 u8 tx_intf = intf;
0449 u64 cam0, cam1;
0450
0451 actbank = bank;
0452 actindex = index;
0453 index &= (mcam->banksize - 1);
0454
0455
0456 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, false);
0457
0458
0459 npc_clear_mcam_entry(rvu, mcam, blkaddr, actindex);
0460
0461
0462
0463
0464
0465
0466
0467 for (; bank < (actbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
0468
0469 if (is_npc_intf_tx(intf)) {
0470
0471
0472
0473 tx_intf_mask = 0x1;
0474 tx_intf = intf & tx_intf_mask;
0475 tx_intf_mask = ~tx_intf & tx_intf_mask;
0476 }
0477
0478 rvu_write64(rvu, blkaddr,
0479 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 1),
0480 tx_intf);
0481 rvu_write64(rvu, blkaddr,
0482 NPC_AF_MCAMEX_BANKX_CAMX_INTF(index, bank, 0),
0483 tx_intf_mask);
0484
0485
0486 npc_get_keyword(entry, kw, &cam0, &cam1);
0487 rvu_write64(rvu, blkaddr,
0488 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 1), cam1);
0489 rvu_write64(rvu, blkaddr,
0490 NPC_AF_MCAMEX_BANKX_CAMX_W0(index, bank, 0), cam0);
0491
0492 npc_get_keyword(entry, kw + 1, &cam0, &cam1);
0493 rvu_write64(rvu, blkaddr,
0494 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 1), cam1);
0495 rvu_write64(rvu, blkaddr,
0496 NPC_AF_MCAMEX_BANKX_CAMX_W1(index, bank, 0), cam0);
0497 }
0498
0499
0500 if (is_npc_intf_rx(intf) && actindex < mcam->bmap_entries)
0501 npc_fixup_vf_rule(rvu, mcam, blkaddr, actindex, entry, &enable);
0502
0503
0504 rvu_write64(rvu, blkaddr,
0505 NPC_AF_MCAMEX_BANKX_ACTION(index, actbank), entry->action);
0506
0507
0508 rvu_write64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_TAG_ACT(index, actbank),
0509 entry->vtag_action);
0510
0511
0512 if (enable)
0513 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex, true);
0514 }
0515
0516 void npc_read_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
0517 int blkaddr, u16 src,
0518 struct mcam_entry *entry, u8 *intf, u8 *ena)
0519 {
0520 int sbank = npc_get_bank(mcam, src);
0521 int bank, kw = 0;
0522 u64 cam0, cam1;
0523
0524 src &= (mcam->banksize - 1);
0525 bank = sbank;
0526
0527 for (; bank < (sbank + mcam->banks_per_entry); bank++, kw = kw + 2) {
0528 cam1 = rvu_read64(rvu, blkaddr,
0529 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 1));
0530 cam0 = rvu_read64(rvu, blkaddr,
0531 NPC_AF_MCAMEX_BANKX_CAMX_W0(src, bank, 0));
0532 npc_fill_entryword(entry, kw, cam0, cam1);
0533
0534 cam1 = rvu_read64(rvu, blkaddr,
0535 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 1));
0536 cam0 = rvu_read64(rvu, blkaddr,
0537 NPC_AF_MCAMEX_BANKX_CAMX_W1(src, bank, 0));
0538 npc_fill_entryword(entry, kw + 1, cam0, cam1);
0539 }
0540
0541 entry->action = rvu_read64(rvu, blkaddr,
0542 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
0543 entry->vtag_action =
0544 rvu_read64(rvu, blkaddr,
0545 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
0546 *intf = rvu_read64(rvu, blkaddr,
0547 NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank, 1)) & 3;
0548 *ena = rvu_read64(rvu, blkaddr,
0549 NPC_AF_MCAMEX_BANKX_CFG(src, sbank)) & 1;
0550 }
0551
0552 static void npc_copy_mcam_entry(struct rvu *rvu, struct npc_mcam *mcam,
0553 int blkaddr, u16 src, u16 dest)
0554 {
0555 int dbank = npc_get_bank(mcam, dest);
0556 int sbank = npc_get_bank(mcam, src);
0557 u64 cfg, sreg, dreg;
0558 int bank, i;
0559
0560 src &= (mcam->banksize - 1);
0561 dest &= (mcam->banksize - 1);
0562
0563
0564 for (bank = 0; bank < mcam->banks_per_entry; bank++) {
0565 sreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(src, sbank + bank, 0);
0566 dreg = NPC_AF_MCAMEX_BANKX_CAMX_INTF(dest, dbank + bank, 0);
0567 for (i = 0; i < 6; i++) {
0568 cfg = rvu_read64(rvu, blkaddr, sreg + (i * 8));
0569 rvu_write64(rvu, blkaddr, dreg + (i * 8), cfg);
0570 }
0571 }
0572
0573
0574 cfg = rvu_read64(rvu, blkaddr,
0575 NPC_AF_MCAMEX_BANKX_ACTION(src, sbank));
0576 rvu_write64(rvu, blkaddr,
0577 NPC_AF_MCAMEX_BANKX_ACTION(dest, dbank), cfg);
0578
0579
0580 cfg = rvu_read64(rvu, blkaddr,
0581 NPC_AF_MCAMEX_BANKX_TAG_ACT(src, sbank));
0582 rvu_write64(rvu, blkaddr,
0583 NPC_AF_MCAMEX_BANKX_TAG_ACT(dest, dbank), cfg);
0584
0585
0586 cfg = rvu_read64(rvu, blkaddr,
0587 NPC_AF_MCAMEX_BANKX_CFG(src, sbank));
0588 rvu_write64(rvu, blkaddr,
0589 NPC_AF_MCAMEX_BANKX_CFG(dest, dbank), cfg);
0590 }
0591
0592 static u64 npc_get_mcam_action(struct rvu *rvu, struct npc_mcam *mcam,
0593 int blkaddr, int index)
0594 {
0595 int bank = npc_get_bank(mcam, index);
0596
0597 index &= (mcam->banksize - 1);
0598 return rvu_read64(rvu, blkaddr,
0599 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
0600 }
0601
0602 void rvu_npc_install_ucast_entry(struct rvu *rvu, u16 pcifunc,
0603 int nixlf, u64 chan, u8 *mac_addr)
0604 {
0605 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
0606 struct npc_install_flow_req req = { 0 };
0607 struct npc_install_flow_rsp rsp = { 0 };
0608 struct npc_mcam *mcam = &rvu->hw->mcam;
0609 struct nix_rx_action action = { 0 };
0610 int blkaddr, index;
0611
0612
0613 if (is_afvf(pcifunc) || is_sdp_vf(pcifunc))
0614 return;
0615
0616 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
0617 if (blkaddr < 0)
0618 return;
0619
0620 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
0621 nixlf, NIXLF_UCAST_ENTRY);
0622
0623
0624
0625
0626 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
0627 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
0628 blkaddr, index);
0629 } else {
0630 action.op = NIX_RX_ACTIONOP_UCAST;
0631 action.pf_func = pcifunc;
0632 }
0633
0634 req.default_rule = 1;
0635 ether_addr_copy(req.packet.dmac, mac_addr);
0636 eth_broadcast_addr((u8 *)&req.mask.dmac);
0637 req.features = BIT_ULL(NPC_DMAC);
0638 req.channel = chan;
0639 req.chan_mask = 0xFFFU;
0640 req.intf = pfvf->nix_rx_intf;
0641 req.op = action.op;
0642 req.hdr.pcifunc = 0;
0643 req.vf = action.pf_func;
0644 req.index = action.index;
0645 req.match_id = action.match_id;
0646 req.flow_key_alg = action.flow_key_alg;
0647
0648 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
0649 }
0650
0651 void rvu_npc_install_promisc_entry(struct rvu *rvu, u16 pcifunc,
0652 int nixlf, u64 chan, u8 chan_cnt)
0653 {
0654 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
0655 struct npc_install_flow_req req = { 0 };
0656 struct npc_install_flow_rsp rsp = { 0 };
0657 struct npc_mcam *mcam = &rvu->hw->mcam;
0658 struct rvu_hwinfo *hw = rvu->hw;
0659 int blkaddr, ucast_idx, index;
0660 struct nix_rx_action action = { 0 };
0661 u64 relaxed_mask;
0662
0663 if (!hw->cap.nix_rx_multicast && is_cgx_vf(rvu, pcifunc))
0664 return;
0665
0666 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
0667 if (blkaddr < 0)
0668 return;
0669
0670 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
0671 nixlf, NIXLF_PROMISC_ENTRY);
0672
0673 if (is_cgx_vf(rvu, pcifunc))
0674 index = npc_get_nixlf_mcam_index(mcam,
0675 pcifunc & ~RVU_PFVF_FUNC_MASK,
0676 nixlf, NIXLF_PROMISC_ENTRY);
0677
0678
0679
0680
0681 ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
0682 nixlf, NIXLF_UCAST_ENTRY);
0683 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
0684 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
0685 blkaddr, ucast_idx);
0686
0687 if (action.op != NIX_RX_ACTIONOP_RSS) {
0688 *(u64 *)&action = 0;
0689 action.op = NIX_RX_ACTIONOP_UCAST;
0690 }
0691
0692
0693 if (hw->cap.nix_rx_multicast && pfvf->use_mce_list &&
0694 is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc))) {
0695 *(u64 *)&action = 0;
0696 action.op = NIX_RX_ACTIONOP_MCAST;
0697 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
0698 action.index = pfvf->promisc_mce_idx;
0699 }
0700
0701
0702
0703
0704
0705
0706 if (!is_rvu_otx2(rvu)) {
0707 req.chan_mask = NIX_CHAN_CPT_X2P_MASK;
0708 } else {
0709 req.chan_mask = 0xFFFU;
0710 }
0711
0712 if (chan_cnt > 1) {
0713 if (!is_power_of_2(chan_cnt)) {
0714 dev_err(rvu->dev,
0715 "%s: channel count more than 1, must be power of 2\n", __func__);
0716 return;
0717 }
0718 relaxed_mask = GENMASK_ULL(BITS_PER_LONG_LONG - 1,
0719 ilog2(chan_cnt));
0720 req.chan_mask &= relaxed_mask;
0721 }
0722
0723 req.channel = chan;
0724 req.intf = pfvf->nix_rx_intf;
0725 req.entry = index;
0726 req.op = action.op;
0727 req.hdr.pcifunc = 0;
0728 req.vf = pcifunc;
0729 req.index = action.index;
0730 req.match_id = action.match_id;
0731 req.flow_key_alg = action.flow_key_alg;
0732
0733 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
0734 }
0735
0736 void rvu_npc_enable_promisc_entry(struct rvu *rvu, u16 pcifunc,
0737 int nixlf, bool enable)
0738 {
0739 struct npc_mcam *mcam = &rvu->hw->mcam;
0740 int blkaddr, index;
0741
0742 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
0743 if (blkaddr < 0)
0744 return;
0745
0746
0747 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
0748
0749 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
0750 nixlf, NIXLF_PROMISC_ENTRY);
0751 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
0752 }
0753
0754 void rvu_npc_install_bcast_match_entry(struct rvu *rvu, u16 pcifunc,
0755 int nixlf, u64 chan)
0756 {
0757 struct rvu_pfvf *pfvf;
0758 struct npc_install_flow_req req = { 0 };
0759 struct npc_install_flow_rsp rsp = { 0 };
0760 struct npc_mcam *mcam = &rvu->hw->mcam;
0761 struct rvu_hwinfo *hw = rvu->hw;
0762 int blkaddr, index;
0763
0764 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
0765 if (blkaddr < 0)
0766 return;
0767
0768
0769 if (is_afvf(pcifunc))
0770 return;
0771
0772
0773
0774
0775 if (!hw->cap.nix_rx_multicast && is_vf(pcifunc))
0776 return;
0777
0778
0779 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
0780 pfvf = rvu_get_pfvf(rvu, pcifunc);
0781 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
0782 nixlf, NIXLF_BCAST_ENTRY);
0783
0784 if (!hw->cap.nix_rx_multicast) {
0785
0786
0787
0788
0789 req.op = NIX_RX_ACTIONOP_UCAST;
0790 } else {
0791 req.op = NIX_RX_ACTIONOP_MCAST;
0792 req.index = pfvf->bcast_mce_idx;
0793 }
0794
0795 eth_broadcast_addr((u8 *)&req.packet.dmac);
0796 eth_broadcast_addr((u8 *)&req.mask.dmac);
0797 req.features = BIT_ULL(NPC_DMAC);
0798 req.channel = chan;
0799 req.chan_mask = 0xFFFU;
0800 req.intf = pfvf->nix_rx_intf;
0801 req.entry = index;
0802 req.hdr.pcifunc = 0;
0803 req.vf = pcifunc;
0804
0805 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
0806 }
0807
0808 void rvu_npc_enable_bcast_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
0809 bool enable)
0810 {
0811 struct npc_mcam *mcam = &rvu->hw->mcam;
0812 int blkaddr, index;
0813
0814 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
0815 if (blkaddr < 0)
0816 return;
0817
0818
0819 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
0820
0821 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
0822 NIXLF_BCAST_ENTRY);
0823 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
0824 }
0825
0826 void rvu_npc_install_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
0827 u64 chan)
0828 {
0829 struct npc_install_flow_req req = { 0 };
0830 struct npc_install_flow_rsp rsp = { 0 };
0831 struct npc_mcam *mcam = &rvu->hw->mcam;
0832 struct rvu_hwinfo *hw = rvu->hw;
0833 int blkaddr, ucast_idx, index;
0834 u8 mac_addr[ETH_ALEN] = { 0 };
0835 struct nix_rx_action action = { 0 };
0836 struct rvu_pfvf *pfvf;
0837 u16 vf_func;
0838
0839
0840 if (is_afvf(pcifunc) && is_sdp_vf(pcifunc))
0841 return;
0842
0843 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
0844 if (blkaddr < 0)
0845 return;
0846
0847
0848 vf_func = pcifunc & RVU_PFVF_FUNC_MASK;
0849 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
0850 pfvf = rvu_get_pfvf(rvu, pcifunc);
0851 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
0852 nixlf, NIXLF_ALLMULTI_ENTRY);
0853
0854
0855
0856
0857 ucast_idx = npc_get_nixlf_mcam_index(mcam, pcifunc,
0858 nixlf, NIXLF_UCAST_ENTRY);
0859 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, ucast_idx))
0860 *(u64 *)&action = npc_get_mcam_action(rvu, mcam,
0861 blkaddr, ucast_idx);
0862
0863 if (action.op != NIX_RX_ACTIONOP_RSS) {
0864 *(u64 *)&action = 0;
0865 action.op = NIX_RX_ACTIONOP_UCAST;
0866 action.pf_func = pcifunc;
0867 }
0868
0869
0870 if (hw->cap.nix_rx_multicast && pfvf->use_mce_list) {
0871 *(u64 *)&action = 0;
0872 action.op = NIX_RX_ACTIONOP_MCAST;
0873 action.index = pfvf->mcast_mce_idx;
0874 }
0875
0876 mac_addr[0] = 0x01;
0877 ether_addr_copy(req.packet.dmac, mac_addr);
0878 ether_addr_copy(req.mask.dmac, mac_addr);
0879 req.features = BIT_ULL(NPC_DMAC);
0880
0881
0882
0883
0884
0885
0886 if (!is_rvu_otx2(rvu))
0887 req.chan_mask = NIX_CHAN_CPT_X2P_MASK;
0888 else
0889 req.chan_mask = 0xFFFU;
0890
0891 req.channel = chan;
0892 req.intf = pfvf->nix_rx_intf;
0893 req.entry = index;
0894 req.op = action.op;
0895 req.hdr.pcifunc = 0;
0896 req.vf = pcifunc | vf_func;
0897 req.index = action.index;
0898 req.match_id = action.match_id;
0899 req.flow_key_alg = action.flow_key_alg;
0900
0901 rvu_mbox_handler_npc_install_flow(rvu, &req, &rsp);
0902 }
0903
0904 void rvu_npc_enable_allmulti_entry(struct rvu *rvu, u16 pcifunc, int nixlf,
0905 bool enable)
0906 {
0907 struct npc_mcam *mcam = &rvu->hw->mcam;
0908 int blkaddr, index;
0909
0910 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
0911 if (blkaddr < 0)
0912 return;
0913
0914
0915 pcifunc = pcifunc & ~RVU_PFVF_FUNC_MASK;
0916
0917 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
0918 NIXLF_ALLMULTI_ENTRY);
0919 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
0920 }
0921
0922 static void npc_update_vf_flow_entry(struct rvu *rvu, struct npc_mcam *mcam,
0923 int blkaddr, u16 pcifunc, u64 rx_action)
0924 {
0925 int actindex, index, bank, entry;
0926 struct rvu_npc_mcam_rule *rule;
0927 bool enable, update;
0928
0929 if (!(pcifunc & RVU_PFVF_FUNC_MASK))
0930 return;
0931
0932 mutex_lock(&mcam->lock);
0933 for (index = 0; index < mcam->bmap_entries; index++) {
0934 if (mcam->entry2target_pffunc[index] == pcifunc) {
0935 update = true;
0936
0937 list_for_each_entry(rule, &mcam->mcam_rules, list) {
0938 if (rule->entry == index)
0939 update = false;
0940 }
0941 if (!update)
0942 continue;
0943 bank = npc_get_bank(mcam, index);
0944 actindex = index;
0945 entry = index & (mcam->banksize - 1);
0946
0947
0948 enable = is_mcam_entry_enabled(rvu, mcam, blkaddr,
0949 actindex);
0950
0951 npc_enable_mcam_entry(rvu, mcam, blkaddr, actindex,
0952 false);
0953
0954 rvu_write64(rvu, blkaddr,
0955 NPC_AF_MCAMEX_BANKX_ACTION(entry, bank),
0956 rx_action);
0957 if (enable)
0958 npc_enable_mcam_entry(rvu, mcam, blkaddr,
0959 actindex, true);
0960 }
0961 }
0962 mutex_unlock(&mcam->lock);
0963 }
0964
0965 void rvu_npc_update_flowkey_alg_idx(struct rvu *rvu, u16 pcifunc, int nixlf,
0966 int group, int alg_idx, int mcam_index)
0967 {
0968 struct npc_mcam *mcam = &rvu->hw->mcam;
0969 struct rvu_hwinfo *hw = rvu->hw;
0970 struct nix_rx_action action;
0971 int blkaddr, index, bank;
0972 struct rvu_pfvf *pfvf;
0973
0974 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
0975 if (blkaddr < 0)
0976 return;
0977
0978
0979 if (mcam_index < 0) {
0980 if (group != DEFAULT_RSS_CONTEXT_GROUP)
0981 return;
0982 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
0983 nixlf, NIXLF_UCAST_ENTRY);
0984 } else {
0985
0986 index = mcam_index;
0987 }
0988
0989 if (index >= mcam->total_entries)
0990 return;
0991
0992 bank = npc_get_bank(mcam, index);
0993 index &= (mcam->banksize - 1);
0994
0995 *(u64 *)&action = rvu_read64(rvu, blkaddr,
0996 NPC_AF_MCAMEX_BANKX_ACTION(index, bank));
0997
0998 if (!*(u64 *)&action)
0999 return;
1000
1001 action.op = NIX_RX_ACTIONOP_RSS;
1002 action.pf_func = pcifunc;
1003 action.index = group;
1004 action.flow_key_alg = alg_idx;
1005
1006 rvu_write64(rvu, blkaddr,
1007 NPC_AF_MCAMEX_BANKX_ACTION(index, bank), *(u64 *)&action);
1008
1009
1010 if (mcam_index < 0)
1011 npc_update_vf_flow_entry(rvu, mcam, blkaddr, pcifunc,
1012 *(u64 *)&action);
1013
1014
1015 pfvf = rvu_get_pfvf(rvu, pcifunc);
1016 if (pfvf->def_ucast_rule)
1017 pfvf->def_ucast_rule->rx_action = action;
1018
1019 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1020 nixlf, NIXLF_PROMISC_ENTRY);
1021
1022
1023
1024
1025 if ((!hw->cap.nix_rx_multicast || !pfvf->use_mce_list) &&
1026 is_mcam_entry_enabled(rvu, mcam, blkaddr, index)) {
1027 bank = npc_get_bank(mcam, index);
1028 index &= (mcam->banksize - 1);
1029
1030 rvu_write64(rvu, blkaddr,
1031 NPC_AF_MCAMEX_BANKX_ACTION(index, bank),
1032 *(u64 *)&action);
1033 }
1034 }
1035
1036 void npc_enadis_default_mce_entry(struct rvu *rvu, u16 pcifunc,
1037 int nixlf, int type, bool enable)
1038 {
1039 struct npc_mcam *mcam = &rvu->hw->mcam;
1040 struct rvu_hwinfo *hw = rvu->hw;
1041 struct nix_mce_list *mce_list;
1042 int index, blkaddr, mce_idx;
1043 struct rvu_pfvf *pfvf;
1044
1045 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1046 if (blkaddr < 0)
1047 return;
1048
1049 index = npc_get_nixlf_mcam_index(mcam, pcifunc & ~RVU_PFVF_FUNC_MASK,
1050 nixlf, type);
1051
1052
1053 if (!hw->cap.nix_rx_multicast && !is_vf(pcifunc)) {
1054 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1055 return;
1056 }
1057
1058
1059 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1060 if (hw->cap.nix_rx_multicast && is_vf(pcifunc) &&
1061 type != NIXLF_BCAST_ENTRY && !pfvf->use_mce_list)
1062 return;
1063
1064 nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
1065
1066 nix_update_mce_list(rvu, pcifunc, mce_list,
1067 mce_idx, index, enable);
1068 if (enable)
1069 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1070 }
1071
1072 static void npc_enadis_default_entries(struct rvu *rvu, u16 pcifunc,
1073 int nixlf, bool enable)
1074 {
1075 struct npc_mcam *mcam = &rvu->hw->mcam;
1076 int index, blkaddr;
1077
1078 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1079 if (blkaddr < 0)
1080 return;
1081
1082
1083 index = npc_get_nixlf_mcam_index(mcam, pcifunc,
1084 nixlf, NIXLF_UCAST_ENTRY);
1085 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, enable);
1086
1087
1088
1089
1090 if ((pcifunc & RVU_PFVF_FUNC_MASK) && !rvu->hw->cap.nix_rx_multicast)
1091 return;
1092
1093
1094 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1095 NIXLF_BCAST_ENTRY, enable);
1096 }
1097
1098 void rvu_npc_disable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1099 {
1100 if (nixlf < 0)
1101 return;
1102
1103 npc_enadis_default_entries(rvu, pcifunc, nixlf, false);
1104
1105
1106 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1107 NIXLF_ALLMULTI_ENTRY, false);
1108 npc_enadis_default_mce_entry(rvu, pcifunc, nixlf,
1109 NIXLF_PROMISC_ENTRY, false);
1110 }
1111
1112 bool rvu_npc_enable_mcam_by_entry_index(struct rvu *rvu, int entry, int intf, bool enable)
1113 {
1114 int blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1115 struct npc_mcam *mcam = &rvu->hw->mcam;
1116 struct rvu_npc_mcam_rule *rule, *tmp;
1117
1118 mutex_lock(&mcam->lock);
1119
1120 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1121 if (rule->intf != intf)
1122 continue;
1123
1124 if (rule->entry != entry)
1125 continue;
1126
1127 rule->enable = enable;
1128 mutex_unlock(&mcam->lock);
1129
1130 npc_enable_mcam_entry(rvu, mcam, blkaddr,
1131 entry, enable);
1132
1133 return true;
1134 }
1135
1136 mutex_unlock(&mcam->lock);
1137 return false;
1138 }
1139
1140 void rvu_npc_enable_default_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1141 {
1142 if (nixlf < 0)
1143 return;
1144
1145
1146
1147
1148 npc_enadis_default_entries(rvu, pcifunc, nixlf, true);
1149 }
1150
1151 void rvu_npc_disable_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1152 {
1153 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1154 struct npc_mcam *mcam = &rvu->hw->mcam;
1155 struct rvu_npc_mcam_rule *rule, *tmp;
1156 int blkaddr;
1157
1158 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1159 if (blkaddr < 0)
1160 return;
1161
1162 mutex_lock(&mcam->lock);
1163
1164
1165 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1166 if (is_npc_intf_rx(rule->intf) &&
1167 rule->rx_action.pf_func == pcifunc &&
1168 rule->rx_action.op != NIX_RX_ACTIONOP_MCAST) {
1169 npc_enable_mcam_entry(rvu, mcam, blkaddr,
1170 rule->entry, false);
1171 rule->enable = false;
1172
1173 if (rule->default_rule) {
1174 pfvf->def_ucast_rule = NULL;
1175 list_del(&rule->list);
1176 kfree(rule);
1177 }
1178 }
1179 }
1180
1181 mutex_unlock(&mcam->lock);
1182
1183 npc_mcam_disable_flows(rvu, pcifunc);
1184
1185 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1186 }
1187
1188 void rvu_npc_free_mcam_entries(struct rvu *rvu, u16 pcifunc, int nixlf)
1189 {
1190 struct npc_mcam *mcam = &rvu->hw->mcam;
1191 struct rvu_npc_mcam_rule *rule, *tmp;
1192 int blkaddr;
1193
1194 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1195 if (blkaddr < 0)
1196 return;
1197
1198 mutex_lock(&mcam->lock);
1199
1200
1201 npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
1202
1203
1204 npc_mcam_free_all_counters(rvu, mcam, pcifunc);
1205
1206
1207 list_for_each_entry_safe(rule, tmp, &mcam->mcam_rules, list) {
1208 if (rule->owner == pcifunc && !rule->default_rule) {
1209 list_del(&rule->list);
1210 kfree(rule);
1211 }
1212 }
1213
1214 mutex_unlock(&mcam->lock);
1215
1216 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1217 }
1218
1219 static void npc_program_mkex_rx(struct rvu *rvu, int blkaddr,
1220 struct npc_mcam_kex *mkex, u8 intf)
1221 {
1222 int lid, lt, ld, fl;
1223
1224 if (is_npc_intf_tx(intf))
1225 return;
1226
1227 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1228 mkex->keyx_cfg[NIX_INTF_RX]);
1229
1230
1231 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1232 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1233 for (ld = 0; ld < NPC_MAX_LD; ld++)
1234 SET_KEX_LD(intf, lid, lt, ld,
1235 mkex->intf_lid_lt_ld[NIX_INTF_RX]
1236 [lid][lt][ld]);
1237 }
1238 }
1239
1240 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1241 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1242 SET_KEX_LDFLAGS(intf, ld, fl,
1243 mkex->intf_ld_flags[NIX_INTF_RX]
1244 [ld][fl]);
1245 }
1246 }
1247
1248 static void npc_program_mkex_tx(struct rvu *rvu, int blkaddr,
1249 struct npc_mcam_kex *mkex, u8 intf)
1250 {
1251 int lid, lt, ld, fl;
1252
1253 if (is_npc_intf_rx(intf))
1254 return;
1255
1256 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1257 mkex->keyx_cfg[NIX_INTF_TX]);
1258
1259
1260 for (lid = 0; lid < NPC_MAX_LID; lid++) {
1261 for (lt = 0; lt < NPC_MAX_LT; lt++) {
1262 for (ld = 0; ld < NPC_MAX_LD; ld++)
1263 SET_KEX_LD(intf, lid, lt, ld,
1264 mkex->intf_lid_lt_ld[NIX_INTF_TX]
1265 [lid][lt][ld]);
1266 }
1267 }
1268
1269 for (ld = 0; ld < NPC_MAX_LD; ld++) {
1270 for (fl = 0; fl < NPC_MAX_LFL; fl++)
1271 SET_KEX_LDFLAGS(intf, ld, fl,
1272 mkex->intf_ld_flags[NIX_INTF_TX]
1273 [ld][fl]);
1274 }
1275 }
1276
1277 static void npc_program_mkex_profile(struct rvu *rvu, int blkaddr,
1278 struct npc_mcam_kex *mkex)
1279 {
1280 struct rvu_hwinfo *hw = rvu->hw;
1281 u8 intf;
1282 int ld;
1283
1284 for (ld = 0; ld < NPC_MAX_LD; ld++)
1285 rvu_write64(rvu, blkaddr, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld),
1286 mkex->kex_ld_flags[ld]);
1287
1288 for (intf = 0; intf < hw->npc_intfs; intf++) {
1289 npc_program_mkex_rx(rvu, blkaddr, mkex, intf);
1290 npc_program_mkex_tx(rvu, blkaddr, mkex, intf);
1291 }
1292
1293
1294 npc_program_mkex_hash(rvu, blkaddr);
1295 }
1296
1297 static int npc_fwdb_prfl_img_map(struct rvu *rvu, void __iomem **prfl_img_addr,
1298 u64 *size)
1299 {
1300 u64 prfl_addr, prfl_sz;
1301
1302 if (!rvu->fwdata)
1303 return -EINVAL;
1304
1305 prfl_addr = rvu->fwdata->mcam_addr;
1306 prfl_sz = rvu->fwdata->mcam_sz;
1307
1308 if (!prfl_addr || !prfl_sz)
1309 return -EINVAL;
1310
1311 *prfl_img_addr = ioremap_wc(prfl_addr, prfl_sz);
1312 if (!(*prfl_img_addr))
1313 return -ENOMEM;
1314
1315 *size = prfl_sz;
1316
1317 return 0;
1318 }
1319
1320
1321 #define MKEX_END_SIGN 0xdeadbeef
1322
1323 static void npc_load_mkex_profile(struct rvu *rvu, int blkaddr,
1324 const char *mkex_profile)
1325 {
1326 struct device *dev = &rvu->pdev->dev;
1327 struct npc_mcam_kex *mcam_kex;
1328 void __iomem *mkex_prfl_addr = NULL;
1329 u64 prfl_sz;
1330 int ret;
1331
1332
1333 if (rvu->kpu_fwdata_sz ||
1334 !strncmp(mkex_profile, def_pfl_name, MKEX_NAME_LEN))
1335 goto program_mkex;
1336
1337
1338 ret = npc_fwdb_prfl_img_map(rvu, &mkex_prfl_addr, &prfl_sz);
1339 if (ret < 0)
1340 goto program_mkex;
1341
1342 mcam_kex = (struct npc_mcam_kex __force *)mkex_prfl_addr;
1343
1344 while (((s64)prfl_sz > 0) && (mcam_kex->mkex_sign != MKEX_END_SIGN)) {
1345
1346 if (mcam_kex->mkex_sign == MKEX_SIGN &&
1347 !strncmp(mcam_kex->name, mkex_profile, MKEX_NAME_LEN)) {
1348
1349
1350
1351
1352 if (!is_rvu_96xx_B0(rvu) ||
1353 mcam_kex->keyx_cfg[NIX_INTF_RX] == mcam_kex->keyx_cfg[NIX_INTF_TX])
1354 rvu->kpu.mkex = mcam_kex;
1355 goto program_mkex;
1356 }
1357
1358 mcam_kex++;
1359 prfl_sz -= sizeof(struct npc_mcam_kex);
1360 }
1361 dev_warn(dev, "Failed to load requested profile: %s\n", mkex_profile);
1362
1363 program_mkex:
1364 dev_info(rvu->dev, "Using %s mkex profile\n", rvu->kpu.mkex->name);
1365
1366 npc_program_mkex_profile(rvu, blkaddr, rvu->kpu.mkex);
1367 if (mkex_prfl_addr)
1368 iounmap(mkex_prfl_addr);
1369 }
1370
1371 static void npc_config_kpuaction(struct rvu *rvu, int blkaddr,
1372 const struct npc_kpu_profile_action *kpuaction,
1373 int kpu, int entry, bool pkind)
1374 {
1375 struct npc_kpu_action0 action0 = {0};
1376 struct npc_kpu_action1 action1 = {0};
1377 u64 reg;
1378
1379 action1.errlev = kpuaction->errlev;
1380 action1.errcode = kpuaction->errcode;
1381 action1.dp0_offset = kpuaction->dp0_offset;
1382 action1.dp1_offset = kpuaction->dp1_offset;
1383 action1.dp2_offset = kpuaction->dp2_offset;
1384
1385 if (pkind)
1386 reg = NPC_AF_PKINDX_ACTION1(entry);
1387 else
1388 reg = NPC_AF_KPUX_ENTRYX_ACTION1(kpu, entry);
1389
1390 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action1);
1391
1392 action0.byp_count = kpuaction->bypass_count;
1393 action0.capture_ena = kpuaction->cap_ena;
1394 action0.parse_done = kpuaction->parse_done;
1395 action0.next_state = kpuaction->next_state;
1396 action0.capture_lid = kpuaction->lid;
1397 action0.capture_ltype = kpuaction->ltype;
1398 action0.capture_flags = kpuaction->flags;
1399 action0.ptr_advance = kpuaction->ptr_advance;
1400 action0.var_len_offset = kpuaction->offset;
1401 action0.var_len_mask = kpuaction->mask;
1402 action0.var_len_right = kpuaction->right;
1403 action0.var_len_shift = kpuaction->shift;
1404
1405 if (pkind)
1406 reg = NPC_AF_PKINDX_ACTION0(entry);
1407 else
1408 reg = NPC_AF_KPUX_ENTRYX_ACTION0(kpu, entry);
1409
1410 rvu_write64(rvu, blkaddr, reg, *(u64 *)&action0);
1411 }
1412
1413 static void npc_config_kpucam(struct rvu *rvu, int blkaddr,
1414 const struct npc_kpu_profile_cam *kpucam,
1415 int kpu, int entry)
1416 {
1417 struct npc_kpu_cam cam0 = {0};
1418 struct npc_kpu_cam cam1 = {0};
1419
1420 cam1.state = kpucam->state & kpucam->state_mask;
1421 cam1.dp0_data = kpucam->dp0 & kpucam->dp0_mask;
1422 cam1.dp1_data = kpucam->dp1 & kpucam->dp1_mask;
1423 cam1.dp2_data = kpucam->dp2 & kpucam->dp2_mask;
1424
1425 cam0.state = ~kpucam->state & kpucam->state_mask;
1426 cam0.dp0_data = ~kpucam->dp0 & kpucam->dp0_mask;
1427 cam0.dp1_data = ~kpucam->dp1 & kpucam->dp1_mask;
1428 cam0.dp2_data = ~kpucam->dp2 & kpucam->dp2_mask;
1429
1430 rvu_write64(rvu, blkaddr,
1431 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 0), *(u64 *)&cam0);
1432 rvu_write64(rvu, blkaddr,
1433 NPC_AF_KPUX_ENTRYX_CAMX(kpu, entry, 1), *(u64 *)&cam1);
1434 }
1435
1436 static inline u64 enable_mask(int count)
1437 {
1438 return (((count) < 64) ? ~(BIT_ULL(count) - 1) : (0x00ULL));
1439 }
1440
1441 static void npc_program_kpu_profile(struct rvu *rvu, int blkaddr, int kpu,
1442 const struct npc_kpu_profile *profile)
1443 {
1444 int entry, num_entries, max_entries;
1445 u64 entry_mask;
1446
1447 if (profile->cam_entries != profile->action_entries) {
1448 dev_err(rvu->dev,
1449 "KPU%d: CAM and action entries [%d != %d] not equal\n",
1450 kpu, profile->cam_entries, profile->action_entries);
1451 }
1452
1453 max_entries = rvu->hw->npc_kpu_entries;
1454
1455
1456 num_entries = min_t(int, profile->cam_entries, max_entries);
1457 for (entry = 0; entry < num_entries; entry++)
1458 npc_config_kpucam(rvu, blkaddr,
1459 &profile->cam[entry], kpu, entry);
1460
1461
1462 num_entries = min_t(int, profile->action_entries, max_entries);
1463 for (entry = 0; entry < num_entries; entry++)
1464 npc_config_kpuaction(rvu, blkaddr, &profile->action[entry],
1465 kpu, entry, false);
1466
1467
1468 num_entries = min_t(int, profile->action_entries, profile->cam_entries);
1469 entry_mask = enable_mask(num_entries);
1470
1471 if (!rvu->kpu.custom)
1472 entry_mask |= GENMASK_ULL(KPU_MAX_CST_ENT - 1, 0);
1473 rvu_write64(rvu, blkaddr,
1474 NPC_AF_KPUX_ENTRY_DISX(kpu, 0), entry_mask);
1475 if (num_entries > 64) {
1476 rvu_write64(rvu, blkaddr,
1477 NPC_AF_KPUX_ENTRY_DISX(kpu, 1),
1478 enable_mask(num_entries - 64));
1479 }
1480
1481
1482 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(kpu), 0x01);
1483 }
1484
1485 static int npc_prepare_default_kpu(struct npc_kpu_profile_adapter *profile)
1486 {
1487 profile->custom = 0;
1488 profile->name = def_pfl_name;
1489 profile->version = NPC_KPU_PROFILE_VER;
1490 profile->ikpu = ikpu_action_entries;
1491 profile->pkinds = ARRAY_SIZE(ikpu_action_entries);
1492 profile->kpu = npc_kpu_profiles;
1493 profile->kpus = ARRAY_SIZE(npc_kpu_profiles);
1494 profile->lt_def = &npc_lt_defaults;
1495 profile->mkex = &npc_mkex_default;
1496 profile->mkex_hash = &npc_mkex_hash_default;
1497
1498 return 0;
1499 }
1500
1501 static int npc_apply_custom_kpu(struct rvu *rvu,
1502 struct npc_kpu_profile_adapter *profile)
1503 {
1504 size_t hdr_sz = sizeof(struct npc_kpu_profile_fwdata), offset = 0;
1505 struct npc_kpu_profile_fwdata *fw = rvu->kpu_fwdata;
1506 struct npc_kpu_profile_action *action;
1507 struct npc_kpu_profile_cam *cam;
1508 struct npc_kpu_fwdata *fw_kpu;
1509 int entries;
1510 u16 kpu, entry;
1511
1512 if (rvu->kpu_fwdata_sz < hdr_sz) {
1513 dev_warn(rvu->dev, "Invalid KPU profile size\n");
1514 return -EINVAL;
1515 }
1516 if (le64_to_cpu(fw->signature) != KPU_SIGN) {
1517 dev_warn(rvu->dev, "Invalid KPU profile signature %llx\n",
1518 fw->signature);
1519 return -EINVAL;
1520 }
1521
1522 if (NPC_KPU_VER_MAJ(profile->version) >
1523 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER)) {
1524 dev_warn(rvu->dev, "Not supported Major version: %d > %d\n",
1525 NPC_KPU_VER_MAJ(profile->version),
1526 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER));
1527 return -EINVAL;
1528 }
1529
1530 if (NPC_KPU_VER_MIN(profile->version) <
1531 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER)) {
1532 dev_warn(rvu->dev,
1533 "Invalid KPU profile version: %d.%d.%d expected version <= %d.%d.%d\n",
1534 NPC_KPU_VER_MAJ(profile->version),
1535 NPC_KPU_VER_MIN(profile->version),
1536 NPC_KPU_VER_PATCH(profile->version),
1537 NPC_KPU_VER_MAJ(NPC_KPU_PROFILE_VER),
1538 NPC_KPU_VER_MIN(NPC_KPU_PROFILE_VER),
1539 NPC_KPU_VER_PATCH(NPC_KPU_PROFILE_VER));
1540 return -EINVAL;
1541 }
1542
1543 if (fw->kpus > profile->kpus) {
1544 dev_warn(rvu->dev, "Not enough KPUs: %d > %ld\n", fw->kpus,
1545 profile->kpus);
1546 return -EINVAL;
1547 }
1548
1549 profile->custom = 1;
1550 profile->name = fw->name;
1551 profile->version = le64_to_cpu(fw->version);
1552 profile->mkex = &fw->mkex;
1553 profile->lt_def = &fw->lt_def;
1554
1555 for (kpu = 0; kpu < fw->kpus; kpu++) {
1556 fw_kpu = (struct npc_kpu_fwdata *)(fw->data + offset);
1557 if (fw_kpu->entries > KPU_MAX_CST_ENT)
1558 dev_warn(rvu->dev,
1559 "Too many custom entries on KPU%d: %d > %d\n",
1560 kpu, fw_kpu->entries, KPU_MAX_CST_ENT);
1561 entries = min(fw_kpu->entries, KPU_MAX_CST_ENT);
1562 cam = (struct npc_kpu_profile_cam *)fw_kpu->data;
1563 offset += sizeof(*fw_kpu) + fw_kpu->entries * sizeof(*cam);
1564 action = (struct npc_kpu_profile_action *)(fw->data + offset);
1565 offset += fw_kpu->entries * sizeof(*action);
1566 if (rvu->kpu_fwdata_sz < hdr_sz + offset) {
1567 dev_warn(rvu->dev,
1568 "Profile size mismatch on KPU%i parsing.\n",
1569 kpu + 1);
1570 return -EINVAL;
1571 }
1572 for (entry = 0; entry < entries; entry++) {
1573 profile->kpu[kpu].cam[entry] = cam[entry];
1574 profile->kpu[kpu].action[entry] = action[entry];
1575 }
1576 }
1577
1578 return 0;
1579 }
1580
1581 static int npc_load_kpu_prfl_img(struct rvu *rvu, void __iomem *prfl_addr,
1582 u64 prfl_sz, const char *kpu_profile)
1583 {
1584 struct npc_kpu_profile_fwdata *kpu_data = NULL;
1585 int rc = -EINVAL;
1586
1587 kpu_data = (struct npc_kpu_profile_fwdata __force *)prfl_addr;
1588 if (le64_to_cpu(kpu_data->signature) == KPU_SIGN &&
1589 !strncmp(kpu_data->name, kpu_profile, KPU_NAME_LEN)) {
1590 dev_info(rvu->dev, "Loading KPU profile from firmware db: %s\n",
1591 kpu_profile);
1592 rvu->kpu_fwdata = kpu_data;
1593 rvu->kpu_fwdata_sz = prfl_sz;
1594 rvu->kpu_prfl_addr = prfl_addr;
1595 rc = 0;
1596 }
1597
1598 return rc;
1599 }
1600
1601 static int npc_fwdb_detect_load_prfl_img(struct rvu *rvu, uint64_t prfl_sz,
1602 const char *kpu_profile)
1603 {
1604 struct npc_coalesced_kpu_prfl *img_data = NULL;
1605 int i = 0, rc = -EINVAL;
1606 void __iomem *kpu_prfl_addr;
1607 u16 offset;
1608
1609 img_data = (struct npc_coalesced_kpu_prfl __force *)rvu->kpu_prfl_addr;
1610 if (le64_to_cpu(img_data->signature) == KPU_SIGN &&
1611 !strncmp(img_data->name, kpu_profile, KPU_NAME_LEN)) {
1612
1613 rc = npc_load_kpu_prfl_img(rvu, rvu->kpu_prfl_addr,
1614 prfl_sz, kpu_profile);
1615 goto done;
1616 }
1617
1618
1619 offset = offsetof(struct npc_coalesced_kpu_prfl, prfl_sz) +
1620 (img_data->num_prfl * sizeof(uint16_t));
1621
1622 while (i < img_data->num_prfl) {
1623
1624 offset = ALIGN_8B_CEIL(offset);
1625 kpu_prfl_addr = (void __iomem *)((uintptr_t)rvu->kpu_prfl_addr +
1626 offset);
1627 rc = npc_load_kpu_prfl_img(rvu, kpu_prfl_addr,
1628 img_data->prfl_sz[i], kpu_profile);
1629 if (!rc)
1630 break;
1631
1632 offset += img_data->prfl_sz[i];
1633 i++;
1634 }
1635 done:
1636 return rc;
1637 }
1638
1639 static int npc_load_kpu_profile_fwdb(struct rvu *rvu, const char *kpu_profile)
1640 {
1641 int ret = -EINVAL;
1642 u64 prfl_sz;
1643
1644
1645 ret = npc_fwdb_prfl_img_map(rvu, &rvu->kpu_prfl_addr, &prfl_sz);
1646 if (ret < 0)
1647 goto done;
1648
1649
1650 ret = npc_fwdb_detect_load_prfl_img(rvu, prfl_sz, kpu_profile);
1651 if (ret == 0)
1652 goto done;
1653
1654
1655 if (rvu->kpu_prfl_addr) {
1656 iounmap(rvu->kpu_prfl_addr);
1657 rvu->kpu_prfl_addr = NULL;
1658 rvu->kpu_fwdata_sz = 0;
1659 rvu->kpu_fwdata = NULL;
1660 }
1661
1662 done:
1663 return ret;
1664 }
1665
1666 static void npc_load_kpu_profile(struct rvu *rvu)
1667 {
1668 struct npc_kpu_profile_adapter *profile = &rvu->kpu;
1669 const char *kpu_profile = rvu->kpu_pfl_name;
1670 const struct firmware *fw = NULL;
1671 bool retry_fwdb = false;
1672
1673
1674 if (!strncmp(kpu_profile, def_pfl_name, KPU_NAME_LEN))
1675 goto revert_to_default;
1676
1677 npc_prepare_default_kpu(profile);
1678
1679
1680
1681
1682
1683
1684 if (!request_firmware_direct(&fw, kpu_profile, rvu->dev)) {
1685 dev_info(rvu->dev, "Loading KPU profile from firmware: %s\n",
1686 kpu_profile);
1687 rvu->kpu_fwdata = kzalloc(fw->size, GFP_KERNEL);
1688 if (rvu->kpu_fwdata) {
1689 memcpy(rvu->kpu_fwdata, fw->data, fw->size);
1690 rvu->kpu_fwdata_sz = fw->size;
1691 }
1692 release_firmware(fw);
1693 retry_fwdb = true;
1694 goto program_kpu;
1695 }
1696
1697 load_image_fwdb:
1698
1699 if (npc_load_kpu_profile_fwdb(rvu, kpu_profile))
1700 goto revert_to_default;
1701
1702 program_kpu:
1703
1704 if (!rvu->kpu_fwdata_sz || npc_apply_custom_kpu(rvu, profile)) {
1705
1706
1707
1708 if (rvu->kpu_fwdata || rvu->kpu_fwdata_sz) {
1709
1710 if (rvu->kpu_prfl_addr) {
1711 iounmap(rvu->kpu_prfl_addr);
1712 rvu->kpu_prfl_addr = NULL;
1713 } else {
1714 kfree(rvu->kpu_fwdata);
1715 }
1716 rvu->kpu_fwdata = NULL;
1717 rvu->kpu_fwdata_sz = 0;
1718 if (retry_fwdb) {
1719 retry_fwdb = false;
1720 goto load_image_fwdb;
1721 }
1722 }
1723
1724 dev_warn(rvu->dev,
1725 "Can't load KPU profile %s. Using default.\n",
1726 kpu_profile);
1727 kfree(rvu->kpu_fwdata);
1728 rvu->kpu_fwdata = NULL;
1729 goto revert_to_default;
1730 }
1731
1732 dev_info(rvu->dev, "Using custom profile '%s', version %d.%d.%d\n",
1733 profile->name, NPC_KPU_VER_MAJ(profile->version),
1734 NPC_KPU_VER_MIN(profile->version),
1735 NPC_KPU_VER_PATCH(profile->version));
1736
1737 return;
1738
1739 revert_to_default:
1740 npc_prepare_default_kpu(profile);
1741 }
1742
1743 static void npc_parser_profile_init(struct rvu *rvu, int blkaddr)
1744 {
1745 struct rvu_hwinfo *hw = rvu->hw;
1746 int num_pkinds, num_kpus, idx;
1747
1748
1749 for (idx = 0; idx < hw->npc_kpus; idx++) {
1750 rvu_write64(rvu, blkaddr,
1751 NPC_AF_KPUX_ENTRY_DISX(idx, 0), ~0ULL);
1752 rvu_write64(rvu, blkaddr,
1753 NPC_AF_KPUX_ENTRY_DISX(idx, 1), ~0ULL);
1754 rvu_write64(rvu, blkaddr, NPC_AF_KPUX_CFG(idx), 0x00);
1755 }
1756
1757
1758 npc_load_kpu_profile(rvu);
1759
1760
1761
1762
1763
1764 num_pkinds = rvu->kpu.pkinds;
1765 num_pkinds = min_t(int, hw->npc_pkinds, num_pkinds);
1766
1767 for (idx = 0; idx < num_pkinds; idx++)
1768 npc_config_kpuaction(rvu, blkaddr, &rvu->kpu.ikpu[idx], 0, idx, true);
1769
1770
1771 num_kpus = rvu->kpu.kpus;
1772 num_kpus = min_t(int, hw->npc_kpus, num_kpus);
1773
1774 for (idx = 0; idx < num_kpus; idx++)
1775 npc_program_kpu_profile(rvu, blkaddr, idx, &rvu->kpu.kpu[idx]);
1776 }
1777
1778 static int npc_mcam_rsrcs_init(struct rvu *rvu, int blkaddr)
1779 {
1780 int nixlf_count = rvu_get_nixlf_count(rvu);
1781 struct npc_mcam *mcam = &rvu->hw->mcam;
1782 int rsvd, err;
1783 u16 index;
1784 int cntr;
1785 u64 cfg;
1786
1787
1788 cfg = (rvu_read64(rvu, blkaddr,
1789 NPC_AF_INTFX_KEX_CFG(0)) >> 32) & 0x07;
1790 mcam->total_entries = (mcam->banks / BIT_ULL(cfg)) * mcam->banksize;
1791 mcam->keysize = cfg;
1792
1793
1794 if (cfg == NPC_MCAM_KEY_X4)
1795 mcam->banks_per_entry = 4;
1796 else if (cfg == NPC_MCAM_KEY_X2)
1797 mcam->banks_per_entry = 2;
1798 else
1799 mcam->banks_per_entry = 1;
1800
1801
1802
1803
1804
1805
1806
1807
1808 rsvd = (nixlf_count * RSVD_MCAM_ENTRIES_PER_NIXLF) +
1809 ((rvu->hw->total_pfs - 1) * RSVD_MCAM_ENTRIES_PER_PF);
1810 if (mcam->total_entries <= rsvd) {
1811 dev_warn(rvu->dev,
1812 "Insufficient NPC MCAM size %d for pkt I/O, exiting\n",
1813 mcam->total_entries);
1814 return -ENOMEM;
1815 }
1816
1817 mcam->bmap_entries = mcam->total_entries - rsvd;
1818 mcam->nixlf_offset = mcam->bmap_entries;
1819 mcam->pf_offset = mcam->nixlf_offset + nixlf_count;
1820
1821
1822 mcam->bmap = devm_kcalloc(rvu->dev, BITS_TO_LONGS(mcam->bmap_entries),
1823 sizeof(long), GFP_KERNEL);
1824 if (!mcam->bmap)
1825 return -ENOMEM;
1826
1827 mcam->bmap_reverse = devm_kcalloc(rvu->dev,
1828 BITS_TO_LONGS(mcam->bmap_entries),
1829 sizeof(long), GFP_KERNEL);
1830 if (!mcam->bmap_reverse)
1831 return -ENOMEM;
1832
1833 mcam->bmap_fcnt = mcam->bmap_entries;
1834
1835
1836 mcam->entry2pfvf_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1837 sizeof(u16), GFP_KERNEL);
1838 if (!mcam->entry2pfvf_map)
1839 return -ENOMEM;
1840
1841
1842
1843
1844
1845 mcam->lprio_count = mcam->bmap_entries / 8;
1846 if (mcam->lprio_count > BITS_PER_LONG)
1847 mcam->lprio_count = round_down(mcam->lprio_count,
1848 BITS_PER_LONG);
1849 mcam->lprio_start = mcam->bmap_entries - mcam->lprio_count;
1850 mcam->hprio_count = mcam->lprio_count;
1851 mcam->hprio_end = mcam->hprio_count;
1852
1853
1854
1855
1856 err = rvu_alloc_bitmap(&mcam->counters);
1857 if (err)
1858 return err;
1859
1860 mcam->cntr2pfvf_map = devm_kcalloc(rvu->dev, mcam->counters.max,
1861 sizeof(u16), GFP_KERNEL);
1862 if (!mcam->cntr2pfvf_map)
1863 goto free_mem;
1864
1865
1866
1867
1868 mcam->entry2cntr_map = devm_kcalloc(rvu->dev, mcam->bmap_entries,
1869 sizeof(u16), GFP_KERNEL);
1870 if (!mcam->entry2cntr_map)
1871 goto free_mem;
1872
1873 mcam->cntr_refcnt = devm_kcalloc(rvu->dev, mcam->counters.max,
1874 sizeof(u16), GFP_KERNEL);
1875 if (!mcam->cntr_refcnt)
1876 goto free_mem;
1877
1878
1879 mcam->entry2target_pffunc = devm_kcalloc(rvu->dev, mcam->total_entries,
1880 sizeof(u16), GFP_KERNEL);
1881 if (!mcam->entry2target_pffunc)
1882 goto free_mem;
1883
1884 for (index = 0; index < mcam->bmap_entries; index++) {
1885 mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
1886 mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
1887 }
1888
1889 for (cntr = 0; cntr < mcam->counters.max; cntr++)
1890 mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
1891
1892 mutex_init(&mcam->lock);
1893
1894 return 0;
1895
1896 free_mem:
1897 kfree(mcam->counters.bmap);
1898 return -ENOMEM;
1899 }
1900
1901 static void rvu_npc_hw_init(struct rvu *rvu, int blkaddr)
1902 {
1903 struct npc_pkind *pkind = &rvu->hw->pkind;
1904 struct npc_mcam *mcam = &rvu->hw->mcam;
1905 struct rvu_hwinfo *hw = rvu->hw;
1906 u64 npc_const, npc_const1;
1907 u64 npc_const2 = 0;
1908
1909 npc_const = rvu_read64(rvu, blkaddr, NPC_AF_CONST);
1910 npc_const1 = rvu_read64(rvu, blkaddr, NPC_AF_CONST1);
1911 if (npc_const1 & BIT_ULL(63))
1912 npc_const2 = rvu_read64(rvu, blkaddr, NPC_AF_CONST2);
1913
1914 pkind->rsrc.max = NPC_UNRESERVED_PKIND_COUNT;
1915 hw->npc_pkinds = (npc_const1 >> 12) & 0xFFULL;
1916 hw->npc_kpu_entries = npc_const1 & 0xFFFULL;
1917 hw->npc_kpus = (npc_const >> 8) & 0x1FULL;
1918 hw->npc_intfs = npc_const & 0xFULL;
1919 hw->npc_counters = (npc_const >> 48) & 0xFFFFULL;
1920
1921 mcam->banks = (npc_const >> 44) & 0xFULL;
1922 mcam->banksize = (npc_const >> 28) & 0xFFFFULL;
1923 hw->npc_stat_ena = BIT_ULL(9);
1924
1925 if (npc_const2) {
1926 hw->npc_ext_set = true;
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937 if (!hw->npc_counters)
1938 hw->npc_stat_ena = BIT_ULL(63);
1939 hw->npc_counters = (npc_const2 >> 16) & 0xFFFFULL;
1940 mcam->banksize = npc_const2 & 0xFFFFULL;
1941 }
1942
1943 mcam->counters.max = hw->npc_counters;
1944 }
1945
1946 static void rvu_npc_setup_interfaces(struct rvu *rvu, int blkaddr)
1947 {
1948 struct npc_mcam_kex *mkex = rvu->kpu.mkex;
1949 struct npc_mcam *mcam = &rvu->hw->mcam;
1950 struct rvu_hwinfo *hw = rvu->hw;
1951 u64 nibble_ena, rx_kex, tx_kex;
1952 u8 intf;
1953
1954
1955
1956
1957
1958 mcam->counters.max--;
1959 mcam->rx_miss_act_cntr = mcam->counters.max;
1960
1961 rx_kex = mkex->keyx_cfg[NIX_INTF_RX];
1962 tx_kex = mkex->keyx_cfg[NIX_INTF_TX];
1963 nibble_ena = FIELD_GET(NPC_PARSE_NIBBLE, rx_kex);
1964
1965 nibble_ena = rvu_npc_get_tx_nibble_cfg(rvu, nibble_ena);
1966 if (nibble_ena) {
1967 tx_kex &= ~NPC_PARSE_NIBBLE;
1968 tx_kex |= FIELD_PREP(NPC_PARSE_NIBBLE, nibble_ena);
1969 mkex->keyx_cfg[NIX_INTF_TX] = tx_kex;
1970 }
1971
1972
1973 for (intf = 0; intf < hw->npc_intfs; intf++) {
1974 if (is_npc_intf_tx(intf))
1975 continue;
1976
1977
1978 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
1979 rx_kex);
1980
1981
1982
1983
1984
1985 rvu_write64(rvu, blkaddr,
1986 NPC_AF_INTFX_MISS_ACT(intf), NIX_RX_ACTIONOP_DROP);
1987
1988
1989
1990
1991 rvu_write64(rvu, blkaddr,
1992 NPC_AF_INTFX_MISS_STAT_ACT(intf),
1993 ((mcam->rx_miss_act_cntr >> 9) << 12) |
1994 hw->npc_stat_ena | mcam->rx_miss_act_cntr);
1995 }
1996
1997
1998 for (intf = 0; intf < hw->npc_intfs; intf++) {
1999 if (is_npc_intf_rx(intf))
2000 continue;
2001
2002
2003 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf),
2004 tx_kex);
2005
2006
2007
2008
2009 rvu_write64(rvu, blkaddr,
2010 NPC_AF_INTFX_MISS_ACT(intf),
2011 NIX_TX_ACTIONOP_UCAST_DEFAULT);
2012 }
2013 }
2014
2015 int rvu_npc_init(struct rvu *rvu)
2016 {
2017 struct npc_kpu_profile_adapter *kpu = &rvu->kpu;
2018 struct npc_pkind *pkind = &rvu->hw->pkind;
2019 struct npc_mcam *mcam = &rvu->hw->mcam;
2020 int blkaddr, entry, bank, err;
2021
2022 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2023 if (blkaddr < 0) {
2024 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
2025 return -ENODEV;
2026 }
2027
2028 rvu_npc_hw_init(rvu, blkaddr);
2029
2030
2031 for (bank = 0; bank < mcam->banks; bank++) {
2032 for (entry = 0; entry < mcam->banksize; entry++)
2033 rvu_write64(rvu, blkaddr,
2034 NPC_AF_MCAMEX_BANKX_CFG(entry, bank), 0);
2035 }
2036
2037 err = rvu_alloc_bitmap(&pkind->rsrc);
2038 if (err)
2039 return err;
2040
2041
2042
2043 rvu_alloc_rsrc(&pkind->rsrc);
2044
2045
2046 pkind->pfchan_map = devm_kcalloc(rvu->dev, pkind->rsrc.max,
2047 sizeof(u32), GFP_KERNEL);
2048 if (!pkind->pfchan_map)
2049 return -ENOMEM;
2050
2051
2052 npc_parser_profile_init(rvu, blkaddr);
2053
2054
2055 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OL2,
2056 (kpu->lt_def->pck_ol2.lid << 8) | (kpu->lt_def->pck_ol2.ltype_match << 4) |
2057 kpu->lt_def->pck_ol2.ltype_mask);
2058 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_OIP4,
2059 (kpu->lt_def->pck_oip4.lid << 8) | (kpu->lt_def->pck_oip4.ltype_match << 4) |
2060 kpu->lt_def->pck_oip4.ltype_mask);
2061
2062
2063 rvu_write64(rvu, blkaddr, NPC_AF_PCK_DEF_IIP4,
2064 (kpu->lt_def->pck_iip4.lid << 8) | (kpu->lt_def->pck_iip4.ltype_match << 4) |
2065 kpu->lt_def->pck_iip4.ltype_mask);
2066
2067
2068
2069
2070
2071
2072
2073
2074 rvu_write64(rvu, blkaddr, NPC_AF_PCK_CFG,
2075 rvu_read64(rvu, blkaddr, NPC_AF_PCK_CFG) |
2076 ((u64)NPC_EC_OIP4_CSUM << 32) | (NPC_EC_IIP4_CSUM << 24) |
2077 BIT_ULL(7) | BIT_ULL(6) | BIT_ULL(2) | BIT_ULL(1));
2078
2079 rvu_npc_setup_interfaces(rvu, blkaddr);
2080
2081 npc_config_secret_key(rvu, blkaddr);
2082
2083 npc_load_mkex_profile(rvu, blkaddr, rvu->mkex_pfl_name);
2084
2085 err = npc_mcam_rsrcs_init(rvu, blkaddr);
2086 if (err)
2087 return err;
2088
2089 err = npc_flow_steering_init(rvu, blkaddr);
2090 if (err) {
2091 dev_err(rvu->dev,
2092 "Incorrect mkex profile loaded using default mkex\n");
2093 npc_load_mkex_profile(rvu, blkaddr, def_pfl_name);
2094 }
2095
2096 return 0;
2097 }
2098
2099 void rvu_npc_freemem(struct rvu *rvu)
2100 {
2101 struct npc_pkind *pkind = &rvu->hw->pkind;
2102 struct npc_mcam *mcam = &rvu->hw->mcam;
2103
2104 kfree(pkind->rsrc.bmap);
2105 kfree(mcam->counters.bmap);
2106 if (rvu->kpu_prfl_addr)
2107 iounmap(rvu->kpu_prfl_addr);
2108 else
2109 kfree(rvu->kpu_fwdata);
2110 mutex_destroy(&mcam->lock);
2111 }
2112
2113 void rvu_npc_get_mcam_entry_alloc_info(struct rvu *rvu, u16 pcifunc,
2114 int blkaddr, int *alloc_cnt,
2115 int *enable_cnt)
2116 {
2117 struct npc_mcam *mcam = &rvu->hw->mcam;
2118 int entry;
2119
2120 *alloc_cnt = 0;
2121 *enable_cnt = 0;
2122
2123 for (entry = 0; entry < mcam->bmap_entries; entry++) {
2124 if (mcam->entry2pfvf_map[entry] == pcifunc) {
2125 (*alloc_cnt)++;
2126 if (is_mcam_entry_enabled(rvu, mcam, blkaddr, entry))
2127 (*enable_cnt)++;
2128 }
2129 }
2130 }
2131
2132 void rvu_npc_get_mcam_counter_alloc_info(struct rvu *rvu, u16 pcifunc,
2133 int blkaddr, int *alloc_cnt,
2134 int *enable_cnt)
2135 {
2136 struct npc_mcam *mcam = &rvu->hw->mcam;
2137 int cntr;
2138
2139 *alloc_cnt = 0;
2140 *enable_cnt = 0;
2141
2142 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2143 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2144 (*alloc_cnt)++;
2145 if (mcam->cntr_refcnt[cntr])
2146 (*enable_cnt)++;
2147 }
2148 }
2149 }
2150
2151 static int npc_mcam_verify_entry(struct npc_mcam *mcam,
2152 u16 pcifunc, int entry)
2153 {
2154
2155 if (is_pffunc_af(pcifunc))
2156 return 0;
2157
2158
2159
2160 if (entry >= mcam->bmap_entries)
2161 return NPC_MCAM_INVALID_REQ;
2162
2163 if (pcifunc != mcam->entry2pfvf_map[entry])
2164 return NPC_MCAM_PERM_DENIED;
2165
2166 return 0;
2167 }
2168
2169 static int npc_mcam_verify_counter(struct npc_mcam *mcam,
2170 u16 pcifunc, int cntr)
2171 {
2172
2173
2174
2175 if (cntr >= mcam->counters.max)
2176 return NPC_MCAM_INVALID_REQ;
2177
2178 if (pcifunc != mcam->cntr2pfvf_map[cntr])
2179 return NPC_MCAM_PERM_DENIED;
2180
2181 return 0;
2182 }
2183
2184 static void npc_map_mcam_entry_and_cntr(struct rvu *rvu, struct npc_mcam *mcam,
2185 int blkaddr, u16 entry, u16 cntr)
2186 {
2187 u16 index = entry & (mcam->banksize - 1);
2188 u32 bank = npc_get_bank(mcam, entry);
2189 struct rvu_hwinfo *hw = rvu->hw;
2190
2191
2192 mcam->entry2cntr_map[entry] = cntr;
2193 mcam->cntr_refcnt[cntr]++;
2194
2195 rvu_write64(rvu, blkaddr,
2196 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank),
2197 ((cntr >> 9) << 12) | hw->npc_stat_ena | cntr);
2198 }
2199
2200 static void npc_unmap_mcam_entry_and_cntr(struct rvu *rvu,
2201 struct npc_mcam *mcam,
2202 int blkaddr, u16 entry, u16 cntr)
2203 {
2204 u16 index = entry & (mcam->banksize - 1);
2205 u32 bank = npc_get_bank(mcam, entry);
2206
2207
2208 mcam->entry2cntr_map[entry] = NPC_MCAM_INVALID_MAP;
2209 mcam->cntr_refcnt[cntr]--;
2210
2211 rvu_write64(rvu, blkaddr,
2212 NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank), 0x00);
2213 }
2214
2215
2216
2217
2218
2219 static void npc_mcam_set_bit(struct npc_mcam *mcam, u16 index)
2220 {
2221 u16 entry, rentry;
2222
2223 entry = index;
2224 rentry = mcam->bmap_entries - index - 1;
2225
2226 __set_bit(entry, mcam->bmap);
2227 __set_bit(rentry, mcam->bmap_reverse);
2228 mcam->bmap_fcnt--;
2229 }
2230
2231
2232
2233
2234
2235 static void npc_mcam_clear_bit(struct npc_mcam *mcam, u16 index)
2236 {
2237 u16 entry, rentry;
2238
2239 entry = index;
2240 rentry = mcam->bmap_entries - index - 1;
2241
2242 __clear_bit(entry, mcam->bmap);
2243 __clear_bit(rentry, mcam->bmap_reverse);
2244 mcam->bmap_fcnt++;
2245 }
2246
2247 static void npc_mcam_free_all_entries(struct rvu *rvu, struct npc_mcam *mcam,
2248 int blkaddr, u16 pcifunc)
2249 {
2250 u16 index, cntr;
2251
2252
2253 for (index = 0; index < mcam->bmap_entries; index++) {
2254 if (mcam->entry2pfvf_map[index] == pcifunc) {
2255 mcam->entry2pfvf_map[index] = NPC_MCAM_INVALID_MAP;
2256
2257 npc_mcam_clear_bit(mcam, index);
2258
2259 npc_enable_mcam_entry(rvu, mcam, blkaddr, index, false);
2260
2261
2262 cntr = mcam->entry2cntr_map[index];
2263 if (cntr != NPC_MCAM_INVALID_MAP)
2264 npc_unmap_mcam_entry_and_cntr(rvu, mcam,
2265 blkaddr, index,
2266 cntr);
2267 mcam->entry2target_pffunc[index] = 0x0;
2268 }
2269 }
2270 }
2271
2272 static void npc_mcam_free_all_counters(struct rvu *rvu, struct npc_mcam *mcam,
2273 u16 pcifunc)
2274 {
2275 u16 cntr;
2276
2277
2278 for (cntr = 0; cntr < mcam->counters.max; cntr++) {
2279 if (mcam->cntr2pfvf_map[cntr] == pcifunc) {
2280 mcam->cntr2pfvf_map[cntr] = NPC_MCAM_INVALID_MAP;
2281 mcam->cntr_refcnt[cntr] = 0;
2282 rvu_free_rsrc(&mcam->counters, cntr);
2283
2284
2285
2286
2287
2288 }
2289 }
2290 }
2291
2292
2293
2294
2295 static u16 npc_mcam_find_zero_area(unsigned long *map, u16 size, u16 start,
2296 u16 nr, u16 *max_area)
2297 {
2298 u16 max_area_start = 0;
2299 u16 index, next, end;
2300
2301 *max_area = 0;
2302
2303 again:
2304 index = find_next_zero_bit(map, size, start);
2305 if (index >= size)
2306 return max_area_start;
2307
2308 end = ((index + nr) >= size) ? size : index + nr;
2309 next = find_next_bit(map, end, index);
2310 if (*max_area < (next - index)) {
2311 *max_area = next - index;
2312 max_area_start = index;
2313 }
2314
2315 if (next < end) {
2316 start = next + 1;
2317 goto again;
2318 }
2319
2320 return max_area_start;
2321 }
2322
2323
2324
2325
2326 static u16 npc_mcam_get_free_count(unsigned long *map, u16 start, u16 end)
2327 {
2328 u16 index, next;
2329 u16 fcnt = 0;
2330
2331 again:
2332 if (start >= end)
2333 return fcnt;
2334
2335 index = find_next_zero_bit(map, end, start);
2336 if (index >= end)
2337 return fcnt;
2338
2339 next = find_next_bit(map, end, index);
2340 if (next <= end) {
2341 fcnt += next - index;
2342 start = next + 1;
2343 goto again;
2344 }
2345
2346 fcnt += end - index;
2347 return fcnt;
2348 }
2349
2350 static void
2351 npc_get_mcam_search_range_priority(struct npc_mcam *mcam,
2352 struct npc_mcam_alloc_entry_req *req,
2353 u16 *start, u16 *end, bool *reverse)
2354 {
2355 u16 fcnt;
2356
2357 if (req->priority == NPC_MCAM_HIGHER_PRIO)
2358 goto hprio;
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370 *reverse = false;
2371 *start = req->ref_entry + 1;
2372 *end = mcam->bmap_entries;
2373
2374 if (req->ref_entry >= mcam->hprio_end)
2375 return;
2376
2377 fcnt = npc_mcam_get_free_count(mcam->bmap,
2378 mcam->hprio_end, mcam->bmap_entries);
2379 if (fcnt > req->count)
2380 *start = mcam->hprio_end;
2381 else
2382 *reverse = true;
2383 return;
2384
2385 hprio:
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395 *reverse = true;
2396 *start = 0;
2397 *end = req->ref_entry;
2398
2399 if (req->ref_entry <= mcam->lprio_start)
2400 return;
2401
2402 fcnt = npc_mcam_get_free_count(mcam->bmap,
2403 mcam->hprio_end, mcam->lprio_start);
2404 if (fcnt < req->count)
2405 return;
2406 *start = mcam->hprio_end;
2407 *end = mcam->lprio_start;
2408 }
2409
2410 static int npc_mcam_alloc_entries(struct npc_mcam *mcam, u16 pcifunc,
2411 struct npc_mcam_alloc_entry_req *req,
2412 struct npc_mcam_alloc_entry_rsp *rsp)
2413 {
2414 u16 entry_list[NPC_MAX_NONCONTIG_ENTRIES];
2415 u16 fcnt, hp_fcnt, lp_fcnt;
2416 u16 start, end, index;
2417 int entry, next_start;
2418 bool reverse = false;
2419 unsigned long *bmap;
2420 u16 max_contig;
2421
2422 mutex_lock(&mcam->lock);
2423
2424
2425 if (!mcam->bmap_fcnt) {
2426 mutex_unlock(&mcam->lock);
2427 return NPC_MCAM_ALLOC_FAILED;
2428 }
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447 if (req->priority) {
2448 npc_get_mcam_search_range_priority(mcam, req,
2449 &start, &end, &reverse);
2450 goto alloc;
2451 }
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461 if (!(pcifunc & RVU_PFVF_FUNC_MASK))
2462 goto lprio_alloc;
2463
2464
2465
2466
2467
2468 lp_fcnt = npc_mcam_get_free_count(mcam->bmap,
2469 mcam->lprio_start,
2470 mcam->bmap_entries);
2471 hp_fcnt = npc_mcam_get_free_count(mcam->bmap, 0, mcam->hprio_end);
2472 fcnt = mcam->bmap_fcnt - lp_fcnt - hp_fcnt;
2473
2474
2475 if (fcnt > req->count) {
2476 start = mcam->hprio_end;
2477 end = mcam->lprio_start;
2478 } else if ((fcnt + (hp_fcnt / 2) + (lp_fcnt / 2)) > req->count) {
2479
2480
2481
2482 start = mcam->hprio_end / 2;
2483 end = mcam->bmap_entries - (mcam->lprio_count / 2);
2484 reverse = true;
2485 } else {
2486
2487
2488
2489 lprio_alloc:
2490 reverse = true;
2491 start = 0;
2492 end = mcam->bmap_entries;
2493 }
2494
2495 alloc:
2496 if (reverse) {
2497 bmap = mcam->bmap_reverse;
2498 start = mcam->bmap_entries - start;
2499 end = mcam->bmap_entries - end;
2500 swap(start, end);
2501 } else {
2502 bmap = mcam->bmap;
2503 }
2504
2505 if (req->contig) {
2506
2507
2508
2509 index = npc_mcam_find_zero_area(bmap, end, start,
2510 req->count, &max_contig);
2511 rsp->count = max_contig;
2512 if (reverse)
2513 rsp->entry = mcam->bmap_entries - index - max_contig;
2514 else
2515 rsp->entry = index;
2516 } else {
2517
2518
2519
2520 rsp->count = 0;
2521 next_start = start;
2522 for (entry = 0; entry < req->count; entry++) {
2523 index = find_next_zero_bit(bmap, end, next_start);
2524 if (index >= end)
2525 break;
2526
2527 next_start = start + (index - start) + 1;
2528
2529
2530 if (reverse)
2531 index = mcam->bmap_entries - index - 1;
2532 entry_list[entry] = index;
2533 rsp->count++;
2534 }
2535 }
2536
2537
2538
2539
2540 if (!req->priority && (rsp->count < req->count) &&
2541 ((end - start) != mcam->bmap_entries)) {
2542 reverse = true;
2543 start = 0;
2544 end = mcam->bmap_entries;
2545 goto alloc;
2546 }
2547
2548
2549
2550
2551 if (req->priority && rsp->count < req->count) {
2552 if (req->priority == NPC_MCAM_LOWER_PRIO &&
2553 (start != (req->ref_entry + 1))) {
2554 start = req->ref_entry + 1;
2555 end = mcam->bmap_entries;
2556 reverse = false;
2557 goto alloc;
2558 } else if ((req->priority == NPC_MCAM_HIGHER_PRIO) &&
2559 ((end - start) != req->ref_entry)) {
2560 start = 0;
2561 end = req->ref_entry;
2562 reverse = true;
2563 goto alloc;
2564 }
2565 }
2566
2567
2568
2569
2570
2571 if (!req->contig && rsp->count) {
2572 index = 0;
2573 for (entry = rsp->count - 1; entry >= 0; entry--) {
2574 if (reverse)
2575 rsp->entry_list[index++] = entry_list[entry];
2576 else
2577 rsp->entry_list[entry] = entry_list[entry];
2578 }
2579 }
2580
2581
2582 for (entry = 0; entry < rsp->count; entry++) {
2583 index = req->contig ?
2584 (rsp->entry + entry) : rsp->entry_list[entry];
2585 npc_mcam_set_bit(mcam, index);
2586 mcam->entry2pfvf_map[index] = pcifunc;
2587 mcam->entry2cntr_map[index] = NPC_MCAM_INVALID_MAP;
2588 }
2589
2590
2591 rsp->free_count = mcam->bmap_fcnt;
2592
2593 mutex_unlock(&mcam->lock);
2594 return 0;
2595 }
2596
2597
2598 void npc_mcam_rsrcs_reserve(struct rvu *rvu, int blkaddr, int entry_idx)
2599 {
2600 struct npc_mcam *mcam = &rvu->hw->mcam;
2601
2602 npc_mcam_set_bit(mcam, entry_idx);
2603 }
2604
2605 int rvu_mbox_handler_npc_mcam_alloc_entry(struct rvu *rvu,
2606 struct npc_mcam_alloc_entry_req *req,
2607 struct npc_mcam_alloc_entry_rsp *rsp)
2608 {
2609 struct npc_mcam *mcam = &rvu->hw->mcam;
2610 u16 pcifunc = req->hdr.pcifunc;
2611 int blkaddr;
2612
2613 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2614 if (blkaddr < 0)
2615 return NPC_MCAM_INVALID_REQ;
2616
2617 rsp->entry = NPC_MCAM_ENTRY_INVALID;
2618 rsp->free_count = 0;
2619
2620
2621 if (req->priority && req->ref_entry >= mcam->bmap_entries) {
2622 dev_err(rvu->dev, "%s: reference entry %d is out of range\n",
2623 __func__, req->ref_entry);
2624 return NPC_MCAM_INVALID_REQ;
2625 }
2626
2627
2628
2629
2630 if ((!req->ref_entry && req->priority == NPC_MCAM_HIGHER_PRIO) ||
2631 ((req->ref_entry == (mcam->bmap_entries - 1)) &&
2632 req->priority == NPC_MCAM_LOWER_PRIO))
2633 return NPC_MCAM_INVALID_REQ;
2634
2635
2636
2637
2638 if (!req->contig && req->count > NPC_MAX_NONCONTIG_ENTRIES) {
2639 dev_err(rvu->dev,
2640 "%s: %d Non-contiguous MCAM entries requested is more than max (%d) allowed\n",
2641 __func__, req->count, NPC_MAX_NONCONTIG_ENTRIES);
2642 return NPC_MCAM_INVALID_REQ;
2643 }
2644
2645
2646 if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2647 return NPC_MCAM_ALLOC_DENIED;
2648
2649 return npc_mcam_alloc_entries(mcam, pcifunc, req, rsp);
2650 }
2651
2652 int rvu_mbox_handler_npc_mcam_free_entry(struct rvu *rvu,
2653 struct npc_mcam_free_entry_req *req,
2654 struct msg_rsp *rsp)
2655 {
2656 struct npc_mcam *mcam = &rvu->hw->mcam;
2657 u16 pcifunc = req->hdr.pcifunc;
2658 int blkaddr, rc = 0;
2659 u16 cntr;
2660
2661 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2662 if (blkaddr < 0)
2663 return NPC_MCAM_INVALID_REQ;
2664
2665
2666 if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2667 return NPC_MCAM_INVALID_REQ;
2668
2669 mutex_lock(&mcam->lock);
2670
2671 if (req->all)
2672 goto free_all;
2673
2674 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2675 if (rc)
2676 goto exit;
2677
2678 mcam->entry2pfvf_map[req->entry] = NPC_MCAM_INVALID_MAP;
2679 mcam->entry2target_pffunc[req->entry] = 0x0;
2680 npc_mcam_clear_bit(mcam, req->entry);
2681 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2682
2683
2684 cntr = mcam->entry2cntr_map[req->entry];
2685 if (cntr != NPC_MCAM_INVALID_MAP)
2686 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2687 req->entry, cntr);
2688
2689 goto exit;
2690
2691 free_all:
2692
2693 npc_mcam_free_all_entries(rvu, mcam, blkaddr, pcifunc);
2694 exit:
2695 mutex_unlock(&mcam->lock);
2696 return rc;
2697 }
2698
2699 int rvu_mbox_handler_npc_mcam_read_entry(struct rvu *rvu,
2700 struct npc_mcam_read_entry_req *req,
2701 struct npc_mcam_read_entry_rsp *rsp)
2702 {
2703 struct npc_mcam *mcam = &rvu->hw->mcam;
2704 u16 pcifunc = req->hdr.pcifunc;
2705 int blkaddr, rc;
2706
2707 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2708 if (blkaddr < 0)
2709 return NPC_MCAM_INVALID_REQ;
2710
2711 mutex_lock(&mcam->lock);
2712 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2713 if (!rc) {
2714 npc_read_mcam_entry(rvu, mcam, blkaddr, req->entry,
2715 &rsp->entry_data,
2716 &rsp->intf, &rsp->enable);
2717 }
2718
2719 mutex_unlock(&mcam->lock);
2720 return rc;
2721 }
2722
2723 int rvu_mbox_handler_npc_mcam_write_entry(struct rvu *rvu,
2724 struct npc_mcam_write_entry_req *req,
2725 struct msg_rsp *rsp)
2726 {
2727 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
2728 struct npc_mcam *mcam = &rvu->hw->mcam;
2729 u16 pcifunc = req->hdr.pcifunc;
2730 int blkaddr, rc;
2731 u8 nix_intf;
2732
2733 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2734 if (blkaddr < 0)
2735 return NPC_MCAM_INVALID_REQ;
2736
2737 mutex_lock(&mcam->lock);
2738 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2739 if (rc)
2740 goto exit;
2741
2742 if (req->set_cntr &&
2743 npc_mcam_verify_counter(mcam, pcifunc, req->cntr)) {
2744 rc = NPC_MCAM_INVALID_REQ;
2745 goto exit;
2746 }
2747
2748 if (!is_npc_interface_valid(rvu, req->intf)) {
2749 rc = NPC_MCAM_INVALID_REQ;
2750 goto exit;
2751 }
2752
2753 if (is_npc_intf_tx(req->intf))
2754 nix_intf = pfvf->nix_tx_intf;
2755 else
2756 nix_intf = pfvf->nix_rx_intf;
2757
2758 if (!is_pffunc_af(pcifunc) &&
2759 npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf, pcifunc)) {
2760 rc = NPC_MCAM_INVALID_REQ;
2761 goto exit;
2762 }
2763
2764
2765 if (is_pffunc_af(req->hdr.pcifunc))
2766 nix_intf = req->intf;
2767
2768 npc_config_mcam_entry(rvu, mcam, blkaddr, req->entry, nix_intf,
2769 &req->entry_data, req->enable_entry);
2770
2771 if (req->set_cntr)
2772 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2773 req->entry, req->cntr);
2774
2775 rc = 0;
2776 exit:
2777 mutex_unlock(&mcam->lock);
2778 return rc;
2779 }
2780
2781 int rvu_mbox_handler_npc_mcam_ena_entry(struct rvu *rvu,
2782 struct npc_mcam_ena_dis_entry_req *req,
2783 struct msg_rsp *rsp)
2784 {
2785 struct npc_mcam *mcam = &rvu->hw->mcam;
2786 u16 pcifunc = req->hdr.pcifunc;
2787 int blkaddr, rc;
2788
2789 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2790 if (blkaddr < 0)
2791 return NPC_MCAM_INVALID_REQ;
2792
2793 mutex_lock(&mcam->lock);
2794 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2795 mutex_unlock(&mcam->lock);
2796 if (rc)
2797 return rc;
2798
2799 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, true);
2800
2801 return 0;
2802 }
2803
2804 int rvu_mbox_handler_npc_mcam_dis_entry(struct rvu *rvu,
2805 struct npc_mcam_ena_dis_entry_req *req,
2806 struct msg_rsp *rsp)
2807 {
2808 struct npc_mcam *mcam = &rvu->hw->mcam;
2809 u16 pcifunc = req->hdr.pcifunc;
2810 int blkaddr, rc;
2811
2812 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2813 if (blkaddr < 0)
2814 return NPC_MCAM_INVALID_REQ;
2815
2816 mutex_lock(&mcam->lock);
2817 rc = npc_mcam_verify_entry(mcam, pcifunc, req->entry);
2818 mutex_unlock(&mcam->lock);
2819 if (rc)
2820 return rc;
2821
2822 npc_enable_mcam_entry(rvu, mcam, blkaddr, req->entry, false);
2823
2824 return 0;
2825 }
2826
2827 int rvu_mbox_handler_npc_mcam_shift_entry(struct rvu *rvu,
2828 struct npc_mcam_shift_entry_req *req,
2829 struct npc_mcam_shift_entry_rsp *rsp)
2830 {
2831 struct npc_mcam *mcam = &rvu->hw->mcam;
2832 u16 pcifunc = req->hdr.pcifunc;
2833 u16 old_entry, new_entry;
2834 int blkaddr, rc = 0;
2835 u16 index, cntr;
2836
2837 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2838 if (blkaddr < 0)
2839 return NPC_MCAM_INVALID_REQ;
2840
2841 if (req->shift_count > NPC_MCAM_MAX_SHIFTS)
2842 return NPC_MCAM_INVALID_REQ;
2843
2844 mutex_lock(&mcam->lock);
2845 for (index = 0; index < req->shift_count; index++) {
2846 old_entry = req->curr_entry[index];
2847 new_entry = req->new_entry[index];
2848
2849
2850
2851
2852 rc = npc_mcam_verify_entry(mcam, pcifunc, old_entry);
2853 if (rc)
2854 break;
2855
2856 rc = npc_mcam_verify_entry(mcam, pcifunc, new_entry);
2857 if (rc)
2858 break;
2859
2860
2861 if (mcam->entry2cntr_map[new_entry] != NPC_MCAM_INVALID_MAP) {
2862 rc = NPC_MCAM_PERM_DENIED;
2863 break;
2864 }
2865
2866
2867 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, false);
2868
2869
2870 npc_copy_mcam_entry(rvu, mcam, blkaddr, old_entry, new_entry);
2871
2872
2873 cntr = mcam->entry2cntr_map[old_entry];
2874 if (cntr != NPC_MCAM_INVALID_MAP) {
2875 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2876 old_entry, cntr);
2877 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2878 new_entry, cntr);
2879 }
2880
2881
2882 npc_enable_mcam_entry(rvu, mcam, blkaddr, new_entry, true);
2883 npc_enable_mcam_entry(rvu, mcam, blkaddr, old_entry, false);
2884 }
2885
2886
2887 if (index != req->shift_count) {
2888 rc = NPC_MCAM_PERM_DENIED;
2889 rsp->failed_entry_idx = index;
2890 }
2891
2892 mutex_unlock(&mcam->lock);
2893 return rc;
2894 }
2895
2896 int rvu_mbox_handler_npc_mcam_alloc_counter(struct rvu *rvu,
2897 struct npc_mcam_alloc_counter_req *req,
2898 struct npc_mcam_alloc_counter_rsp *rsp)
2899 {
2900 struct npc_mcam *mcam = &rvu->hw->mcam;
2901 u16 pcifunc = req->hdr.pcifunc;
2902 u16 max_contig, cntr;
2903 int blkaddr, index;
2904
2905 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2906 if (blkaddr < 0)
2907 return NPC_MCAM_INVALID_REQ;
2908
2909
2910 if (!is_pffunc_af(pcifunc) && !is_nixlf_attached(rvu, pcifunc))
2911 return NPC_MCAM_INVALID_REQ;
2912
2913
2914
2915
2916 if (!req->contig && req->count > NPC_MAX_NONCONTIG_COUNTERS)
2917 return NPC_MCAM_INVALID_REQ;
2918
2919 mutex_lock(&mcam->lock);
2920
2921
2922 if (!rvu_rsrc_free_count(&mcam->counters)) {
2923 mutex_unlock(&mcam->lock);
2924 return NPC_MCAM_ALLOC_FAILED;
2925 }
2926
2927 rsp->count = 0;
2928
2929 if (req->contig) {
2930
2931
2932
2933 index = npc_mcam_find_zero_area(mcam->counters.bmap,
2934 mcam->counters.max, 0,
2935 req->count, &max_contig);
2936 rsp->count = max_contig;
2937 rsp->cntr = index;
2938 for (cntr = index; cntr < (index + max_contig); cntr++) {
2939 __set_bit(cntr, mcam->counters.bmap);
2940 mcam->cntr2pfvf_map[cntr] = pcifunc;
2941 }
2942 } else {
2943
2944
2945
2946 for (cntr = 0; cntr < req->count; cntr++) {
2947 index = rvu_alloc_rsrc(&mcam->counters);
2948 if (index < 0)
2949 break;
2950 rsp->cntr_list[cntr] = index;
2951 rsp->count++;
2952 mcam->cntr2pfvf_map[index] = pcifunc;
2953 }
2954 }
2955
2956 mutex_unlock(&mcam->lock);
2957 return 0;
2958 }
2959
2960 int rvu_mbox_handler_npc_mcam_free_counter(struct rvu *rvu,
2961 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
2962 {
2963 struct npc_mcam *mcam = &rvu->hw->mcam;
2964 u16 index, entry = 0;
2965 int blkaddr, err;
2966
2967 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2968 if (blkaddr < 0)
2969 return NPC_MCAM_INVALID_REQ;
2970
2971 mutex_lock(&mcam->lock);
2972 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
2973 if (err) {
2974 mutex_unlock(&mcam->lock);
2975 return err;
2976 }
2977
2978
2979 mcam->cntr2pfvf_map[req->cntr] = NPC_MCAM_INVALID_MAP;
2980 rvu_free_rsrc(&mcam->counters, req->cntr);
2981
2982
2983 while (entry < mcam->bmap_entries) {
2984 if (!mcam->cntr_refcnt[req->cntr])
2985 break;
2986
2987 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
2988 if (index >= mcam->bmap_entries)
2989 break;
2990 entry = index + 1;
2991 if (mcam->entry2cntr_map[index] != req->cntr)
2992 continue;
2993
2994 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
2995 index, req->cntr);
2996 }
2997
2998 mutex_unlock(&mcam->lock);
2999 return 0;
3000 }
3001
3002 int rvu_mbox_handler_npc_mcam_unmap_counter(struct rvu *rvu,
3003 struct npc_mcam_unmap_counter_req *req, struct msg_rsp *rsp)
3004 {
3005 struct npc_mcam *mcam = &rvu->hw->mcam;
3006 u16 index, entry = 0;
3007 int blkaddr, rc;
3008
3009 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3010 if (blkaddr < 0)
3011 return NPC_MCAM_INVALID_REQ;
3012
3013 mutex_lock(&mcam->lock);
3014 rc = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3015 if (rc)
3016 goto exit;
3017
3018
3019 if (!req->all) {
3020 rc = npc_mcam_verify_entry(mcam, req->hdr.pcifunc, req->entry);
3021 if (rc)
3022 goto exit;
3023 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
3024 req->entry, req->cntr);
3025 goto exit;
3026 }
3027
3028
3029 while (entry < mcam->bmap_entries) {
3030 if (!mcam->cntr_refcnt[req->cntr])
3031 break;
3032
3033 index = find_next_bit(mcam->bmap, mcam->bmap_entries, entry);
3034 if (index >= mcam->bmap_entries)
3035 break;
3036 entry = index + 1;
3037
3038 if (mcam->entry2cntr_map[index] != req->cntr)
3039 continue;
3040
3041 npc_unmap_mcam_entry_and_cntr(rvu, mcam, blkaddr,
3042 index, req->cntr);
3043 }
3044 exit:
3045 mutex_unlock(&mcam->lock);
3046 return rc;
3047 }
3048
3049 int rvu_mbox_handler_npc_mcam_clear_counter(struct rvu *rvu,
3050 struct npc_mcam_oper_counter_req *req, struct msg_rsp *rsp)
3051 {
3052 struct npc_mcam *mcam = &rvu->hw->mcam;
3053 int blkaddr, err;
3054
3055 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3056 if (blkaddr < 0)
3057 return NPC_MCAM_INVALID_REQ;
3058
3059 mutex_lock(&mcam->lock);
3060 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3061 mutex_unlock(&mcam->lock);
3062 if (err)
3063 return err;
3064
3065 rvu_write64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr), 0x00);
3066
3067 return 0;
3068 }
3069
3070 int rvu_mbox_handler_npc_mcam_counter_stats(struct rvu *rvu,
3071 struct npc_mcam_oper_counter_req *req,
3072 struct npc_mcam_oper_counter_rsp *rsp)
3073 {
3074 struct npc_mcam *mcam = &rvu->hw->mcam;
3075 int blkaddr, err;
3076
3077 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3078 if (blkaddr < 0)
3079 return NPC_MCAM_INVALID_REQ;
3080
3081 mutex_lock(&mcam->lock);
3082 err = npc_mcam_verify_counter(mcam, req->hdr.pcifunc, req->cntr);
3083 mutex_unlock(&mcam->lock);
3084 if (err)
3085 return err;
3086
3087 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(req->cntr));
3088 rsp->stat &= BIT_ULL(48) - 1;
3089
3090 return 0;
3091 }
3092
3093 int rvu_mbox_handler_npc_mcam_alloc_and_write_entry(struct rvu *rvu,
3094 struct npc_mcam_alloc_and_write_entry_req *req,
3095 struct npc_mcam_alloc_and_write_entry_rsp *rsp)
3096 {
3097 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
3098 struct npc_mcam_alloc_counter_req cntr_req;
3099 struct npc_mcam_alloc_counter_rsp cntr_rsp;
3100 struct npc_mcam_alloc_entry_req entry_req;
3101 struct npc_mcam_alloc_entry_rsp entry_rsp;
3102 struct npc_mcam *mcam = &rvu->hw->mcam;
3103 u16 entry = NPC_MCAM_ENTRY_INVALID;
3104 u16 cntr = NPC_MCAM_ENTRY_INVALID;
3105 int blkaddr, rc;
3106 u8 nix_intf;
3107
3108 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3109 if (blkaddr < 0)
3110 return NPC_MCAM_INVALID_REQ;
3111
3112 if (!is_npc_interface_valid(rvu, req->intf))
3113 return NPC_MCAM_INVALID_REQ;
3114
3115 if (npc_mcam_verify_pf_func(rvu, &req->entry_data, req->intf,
3116 req->hdr.pcifunc))
3117 return NPC_MCAM_INVALID_REQ;
3118
3119
3120 entry_req.hdr.pcifunc = req->hdr.pcifunc;
3121 entry_req.contig = true;
3122 entry_req.priority = req->priority;
3123 entry_req.ref_entry = req->ref_entry;
3124 entry_req.count = 1;
3125
3126 rc = rvu_mbox_handler_npc_mcam_alloc_entry(rvu,
3127 &entry_req, &entry_rsp);
3128 if (rc)
3129 return rc;
3130
3131 if (!entry_rsp.count)
3132 return NPC_MCAM_ALLOC_FAILED;
3133
3134 entry = entry_rsp.entry;
3135
3136 if (!req->alloc_cntr)
3137 goto write_entry;
3138
3139
3140 cntr_req.hdr.pcifunc = req->hdr.pcifunc;
3141 cntr_req.contig = true;
3142 cntr_req.count = 1;
3143
3144 rc = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req, &cntr_rsp);
3145 if (rc) {
3146
3147 mutex_lock(&mcam->lock);
3148 mcam->entry2pfvf_map[entry] = NPC_MCAM_INVALID_MAP;
3149 npc_mcam_clear_bit(mcam, entry);
3150 mutex_unlock(&mcam->lock);
3151 return rc;
3152 }
3153
3154 cntr = cntr_rsp.cntr;
3155
3156 write_entry:
3157 mutex_lock(&mcam->lock);
3158
3159 if (is_npc_intf_tx(req->intf))
3160 nix_intf = pfvf->nix_tx_intf;
3161 else
3162 nix_intf = pfvf->nix_rx_intf;
3163
3164 npc_config_mcam_entry(rvu, mcam, blkaddr, entry, nix_intf,
3165 &req->entry_data, req->enable_entry);
3166
3167 if (req->alloc_cntr)
3168 npc_map_mcam_entry_and_cntr(rvu, mcam, blkaddr, entry, cntr);
3169 mutex_unlock(&mcam->lock);
3170
3171 rsp->entry = entry;
3172 rsp->cntr = cntr;
3173
3174 return 0;
3175 }
3176
3177 #define GET_KEX_CFG(intf) \
3178 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_INTFX_KEX_CFG(intf))
3179
3180 #define GET_KEX_FLAGS(ld) \
3181 rvu_read64(rvu, BLKADDR_NPC, NPC_AF_KEX_LDATAX_FLAGS_CFG(ld))
3182
3183 #define GET_KEX_LD(intf, lid, lt, ld) \
3184 rvu_read64(rvu, BLKADDR_NPC, \
3185 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, lt, ld))
3186
3187 #define GET_KEX_LDFLAGS(intf, ld, fl) \
3188 rvu_read64(rvu, BLKADDR_NPC, \
3189 NPC_AF_INTFX_LDATAX_FLAGSX_CFG(intf, ld, fl))
3190
3191 int rvu_mbox_handler_npc_get_kex_cfg(struct rvu *rvu, struct msg_req *req,
3192 struct npc_get_kex_cfg_rsp *rsp)
3193 {
3194 int lid, lt, ld, fl;
3195
3196 rsp->rx_keyx_cfg = GET_KEX_CFG(NIX_INTF_RX);
3197 rsp->tx_keyx_cfg = GET_KEX_CFG(NIX_INTF_TX);
3198 for (lid = 0; lid < NPC_MAX_LID; lid++) {
3199 for (lt = 0; lt < NPC_MAX_LT; lt++) {
3200 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3201 rsp->intf_lid_lt_ld[NIX_INTF_RX][lid][lt][ld] =
3202 GET_KEX_LD(NIX_INTF_RX, lid, lt, ld);
3203 rsp->intf_lid_lt_ld[NIX_INTF_TX][lid][lt][ld] =
3204 GET_KEX_LD(NIX_INTF_TX, lid, lt, ld);
3205 }
3206 }
3207 }
3208 for (ld = 0; ld < NPC_MAX_LD; ld++)
3209 rsp->kex_ld_flags[ld] = GET_KEX_FLAGS(ld);
3210
3211 for (ld = 0; ld < NPC_MAX_LD; ld++) {
3212 for (fl = 0; fl < NPC_MAX_LFL; fl++) {
3213 rsp->intf_ld_flags[NIX_INTF_RX][ld][fl] =
3214 GET_KEX_LDFLAGS(NIX_INTF_RX, ld, fl);
3215 rsp->intf_ld_flags[NIX_INTF_TX][ld][fl] =
3216 GET_KEX_LDFLAGS(NIX_INTF_TX, ld, fl);
3217 }
3218 }
3219 memcpy(rsp->mkex_pfl_name, rvu->mkex_pfl_name, MKEX_NAME_LEN);
3220 return 0;
3221 }
3222
3223 static int
3224 npc_set_var_len_offset_pkind(struct rvu *rvu, u16 pcifunc, u64 pkind,
3225 u8 var_len_off, u8 var_len_off_mask, u8 shift_dir)
3226 {
3227 struct npc_kpu_action0 *act0;
3228 u8 shift_count = 0;
3229 int blkaddr;
3230 u64 val;
3231
3232 if (!var_len_off_mask)
3233 return -EINVAL;
3234
3235 if (var_len_off_mask != 0xff) {
3236 if (shift_dir)
3237 shift_count = __ffs(var_len_off_mask);
3238 else
3239 shift_count = (8 - __fls(var_len_off_mask));
3240 }
3241 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, pcifunc);
3242 if (blkaddr < 0) {
3243 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
3244 return -EINVAL;
3245 }
3246 val = rvu_read64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind));
3247 act0 = (struct npc_kpu_action0 *)&val;
3248 act0->var_len_shift = shift_count;
3249 act0->var_len_right = shift_dir;
3250 act0->var_len_mask = var_len_off_mask;
3251 act0->var_len_offset = var_len_off;
3252 rvu_write64(rvu, blkaddr, NPC_AF_PKINDX_ACTION0(pkind), val);
3253 return 0;
3254 }
3255
3256 int rvu_npc_set_parse_mode(struct rvu *rvu, u16 pcifunc, u64 mode, u8 dir,
3257 u64 pkind, u8 var_len_off, u8 var_len_off_mask,
3258 u8 shift_dir)
3259
3260 {
3261 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
3262 int blkaddr, nixlf, rc, intf_mode;
3263 int pf = rvu_get_pf(pcifunc);
3264 u64 rxpkind, txpkind;
3265 u8 cgx_id, lmac_id;
3266
3267
3268 rxpkind = rvu_npc_get_pkind(rvu, pf);
3269 txpkind = NPC_TX_DEF_PKIND;
3270 intf_mode = NPC_INTF_MODE_DEF;
3271
3272 if (mode & OTX2_PRIV_FLAGS_CUSTOM) {
3273 if (pkind == NPC_RX_CUSTOM_PRE_L2_PKIND) {
3274 rc = npc_set_var_len_offset_pkind(rvu, pcifunc, pkind,
3275 var_len_off,
3276 var_len_off_mask,
3277 shift_dir);
3278 if (rc)
3279 return rc;
3280 }
3281 rxpkind = pkind;
3282 txpkind = pkind;
3283 }
3284
3285 if (dir & PKIND_RX) {
3286
3287 if (!is_cgx_config_permitted(rvu, pcifunc))
3288 return 0;
3289 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
3290
3291 rc = cgx_set_pkind(rvu_cgx_pdata(cgx_id, rvu), lmac_id,
3292 rxpkind);
3293 if (rc)
3294 return rc;
3295 }
3296
3297 if (dir & PKIND_TX) {
3298
3299 rc = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3300 if (rc)
3301 return rc;
3302
3303 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_PARSE_CFG(nixlf),
3304 txpkind);
3305 }
3306
3307 pfvf->intf_mode = intf_mode;
3308 return 0;
3309 }
3310
3311 int rvu_mbox_handler_npc_set_pkind(struct rvu *rvu, struct npc_set_pkind *req,
3312 struct msg_rsp *rsp)
3313 {
3314 return rvu_npc_set_parse_mode(rvu, req->hdr.pcifunc, req->mode,
3315 req->dir, req->pkind, req->var_len_off,
3316 req->var_len_off_mask, req->shift_dir);
3317 }
3318
3319 int rvu_mbox_handler_npc_read_base_steer_rule(struct rvu *rvu,
3320 struct msg_req *req,
3321 struct npc_mcam_read_base_rule_rsp *rsp)
3322 {
3323 struct npc_mcam *mcam = &rvu->hw->mcam;
3324 int index, blkaddr, nixlf, rc = 0;
3325 u16 pcifunc = req->hdr.pcifunc;
3326 struct rvu_pfvf *pfvf;
3327 u8 intf, enable;
3328
3329 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3330 if (blkaddr < 0)
3331 return NPC_MCAM_INVALID_REQ;
3332
3333
3334 if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
3335 pfvf = rvu_get_pfvf(rvu, pcifunc);
3336 rsp->entry.kw[0] = pfvf->rx_chan_base;
3337 rsp->entry.kw_mask[0] = 0xFFFULL;
3338 goto out;
3339 }
3340
3341
3342 mutex_lock(&mcam->lock);
3343 for (index = 0; index < mcam->bmap_entries; index++) {
3344 if (mcam->entry2target_pffunc[index] == pcifunc)
3345 goto read_entry;
3346 }
3347
3348 rc = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
3349 if (rc < 0) {
3350 mutex_unlock(&mcam->lock);
3351 goto out;
3352 }
3353
3354 index = npc_get_nixlf_mcam_index(mcam, pcifunc, nixlf,
3355 NIXLF_UCAST_ENTRY);
3356 read_entry:
3357
3358 npc_read_mcam_entry(rvu, mcam, blkaddr, index, &rsp->entry, &intf,
3359 &enable);
3360 mutex_unlock(&mcam->lock);
3361 out:
3362 return rc;
3363 }
3364
3365 int rvu_mbox_handler_npc_mcam_entry_stats(struct rvu *rvu,
3366 struct npc_mcam_get_stats_req *req,
3367 struct npc_mcam_get_stats_rsp *rsp)
3368 {
3369 struct npc_mcam *mcam = &rvu->hw->mcam;
3370 u16 index, cntr;
3371 int blkaddr;
3372 u64 regval;
3373 u32 bank;
3374
3375 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3376 if (blkaddr < 0)
3377 return NPC_MCAM_INVALID_REQ;
3378
3379 mutex_lock(&mcam->lock);
3380
3381 index = req->entry & (mcam->banksize - 1);
3382 bank = npc_get_bank(mcam, req->entry);
3383
3384
3385 regval = rvu_read64(rvu, blkaddr, NPC_AF_MCAMEX_BANKX_STAT_ACT(index, bank));
3386
3387 if (!(regval & rvu->hw->npc_stat_ena)) {
3388 rsp->stat_ena = 0;
3389 mutex_unlock(&mcam->lock);
3390 return 0;
3391 }
3392
3393 cntr = regval & 0x1FF;
3394
3395 rsp->stat_ena = 1;
3396 rsp->stat = rvu_read64(rvu, blkaddr, NPC_AF_MATCH_STATX(cntr));
3397 rsp->stat &= BIT_ULL(48) - 1;
3398
3399 mutex_unlock(&mcam->lock);
3400
3401 return 0;
3402 }