Debian Codegraph @

drivers/net/ethernet/marvell/octeontx2/af/rvu_npc_hash.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Admin Function driver
3 *
4 * Copyright (C) 2022 Marvell.
5 *
6 */
7
8 #include <linux/bitfield.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/firmware.h>
12 #include <linux/stddef.h>
13 #include <linux/debugfs.h>
14
15 #include "rvu_struct.h"
16 #include "rvu_reg.h"
17 #include "rvu.h"
18 #include "npc.h"
19 #include "cgx.h"
20 #include "rvu_npc_fs.h"
21 #include "rvu_npc_hash.h"
22
23 static u64 rvu_npc_wide_extract(const u64 input[], size_t start_bit,
24 size_t width_bits)
25 {
26 const u64 mask = ~(u64)((~(__uint128_t)0) << width_bits);
27 const size_t msb = start_bit + width_bits - 1;
28 const size_t lword = start_bit >> 6;
29 const size_t uword = msb >> 6;
30 size_t lbits;
31 u64 hi, lo;
32
33 if (lword == uword)
34 return (input[lword] >> (start_bit & 63)) & mask;
35
36 lbits = 64 - (start_bit & 63);
37 hi = input[uword];
38 lo = (input[lword] >> (start_bit & 63));
39 return ((hi << lbits) | lo) & mask;
40 }
41
42 static void rvu_npc_lshift_key(u64 *key, size_t key_bit_len)
43 {
44 u64 prev_orig_word = 0;
45 u64 cur_orig_word = 0;
46 size_t extra = key_bit_len % 64;
47 size_t max_idx = key_bit_len / 64;
48 size_t i;
49
50 if (extra)
51 max_idx++;
52
53 for (i = 0; i < max_idx; i++) {
54 cur_orig_word = key[i];
55 key[i] = key[i] << 1;
56 key[i] |= ((prev_orig_word >> 63) & 0x1);
57 prev_orig_word = cur_orig_word;
58 }
59 }
60
61 static u32 rvu_npc_toeplitz_hash(const u64 *data, u64 *key, size_t data_bit_len,
62 size_t key_bit_len)
63 {
64 u32 hash_out = 0;
65 u64 temp_data = 0;
66 int i;
67
68 for (i = data_bit_len - 1; i >= 0; i--) {
69 temp_data = (data[i / 64]);
70 temp_data = temp_data >> (i % 64);
71 temp_data &= 0x1;
72 if (temp_data)
73 hash_out ^= (u32)(rvu_npc_wide_extract(key, key_bit_len - 32, 32));
74
75 rvu_npc_lshift_key(key, key_bit_len);
76 }
77
78 return hash_out;
79 }
80
81 u32 npc_field_hash_calc(u64 *ldata, struct npc_get_field_hash_info_rsp rsp,
82 u8 intf, u8 hash_idx)
83 {
84 u64 hash_key[3];
85 u64 data_padded[2];
86 u32 field_hash;
87
88 hash_key[0] = rsp.secret_key[1] << 31;
89 hash_key[0] |= rsp.secret_key[2];
90 hash_key[1] = rsp.secret_key[1] >> 33;
91 hash_key[1] |= rsp.secret_key[0] << 31;
92 hash_key[2] = rsp.secret_key[0] >> 33;
93
94 data_padded[0] = rsp.hash_mask[intf][hash_idx][0] & ldata[0];
95 data_padded[1] = rsp.hash_mask[intf][hash_idx][1] & ldata[1];
96 field_hash = rvu_npc_toeplitz_hash(data_padded, hash_key, 128, 159);
97
98 field_hash &= FIELD_GET(GENMASK(63, 32), rsp.hash_ctrl[intf][hash_idx]);
99 field_hash += FIELD_GET(GENMASK(31, 0), rsp.hash_ctrl[intf][hash_idx]);
100 return field_hash;
101 }
102
103 static u64 npc_update_use_hash(struct rvu *rvu, int blkaddr,
104 u8 intf, int lid, int lt, int ld)
105 {
106 u8 hdr, key;
107 u64 cfg;
108
109 cfg = rvu_read64(rvu, blkaddr, NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf, lid, lt, ld));
110 hdr = FIELD_GET(NPC_HDR_OFFSET, cfg);
111 key = FIELD_GET(NPC_KEY_OFFSET, cfg);
112
113 /* Update use_hash(bit-20) to 'true' and
114 * bytesm1(bit-16:19) to '0x3' in KEX_LD_CFG
115 */
116 cfg = KEX_LD_CFG_USE_HASH(0x1, 0x03,
117 hdr, 0x1, 0x0, key);
118
119 return cfg;
120 }
121
122 static void npc_program_mkex_hash_rx(struct rvu *rvu, int blkaddr,
123 u8 intf)
124 {
125 struct npc_mcam_kex_hash *mkex_hash = rvu->kpu.mkex_hash;
126 int lid, lt, ld, hash_cnt = 0;
127
128 if (is_cn20k(rvu->pdev))
129 return;
130
131 if (is_npc_intf_tx(intf))
132 return;
133
134 /* Program HASH_CFG */
135 for (lid = 0; lid < NPC_MAX_LID; lid++) {
136 for (lt = 0; lt < NPC_MAX_LT; lt++) {
137 for (ld = 0; ld < NPC_MAX_LD; ld++) {
138 if (mkex_hash->lid_lt_ld_hash_en[intf][lid][lt][ld]) {
139 u64 cfg;
140
141 if (hash_cnt == NPC_MAX_HASH)
142 return;
143
144 cfg = npc_update_use_hash(rvu, blkaddr,
145 intf, lid, lt, ld);
146 /* Set updated KEX configuration */
147 SET_KEX_LD(intf, lid, lt, ld, cfg);
148 /* Set HASH configuration */
149 SET_KEX_LD_HASH(intf, ld,
150 mkex_hash->hash[intf][ld]);
151 SET_KEX_LD_HASH_MASK(intf, ld, 0,
152 mkex_hash->hash_mask[intf][ld][0]);
153 SET_KEX_LD_HASH_MASK(intf, ld, 1,
154 mkex_hash->hash_mask[intf][ld][1]);
155 SET_KEX_LD_HASH_CTRL(intf, ld,
156 mkex_hash->hash_ctrl[intf][ld]);
157
158 hash_cnt++;
159 }
160 }
161 }
162 }
163 }
164
165 static void npc_program_mkex_hash_tx(struct rvu *rvu, int blkaddr,
166 u8 intf)
167 {
168 struct npc_mcam_kex_hash *mkex_hash = rvu->kpu.mkex_hash;
169 int lid, lt, ld, hash_cnt = 0;
170
171 if (is_cn20k(rvu->pdev))
172 return;
173
174 if (is_npc_intf_rx(intf))
175 return;
176
177 /* Program HASH_CFG */
178 for (lid = 0; lid < NPC_MAX_LID; lid++) {
179 for (lt = 0; lt < NPC_MAX_LT; lt++) {
180 for (ld = 0; ld < NPC_MAX_LD; ld++)
181 if (mkex_hash->lid_lt_ld_hash_en[intf][lid][lt][ld]) {
182 u64 cfg;
183
184 if (hash_cnt == NPC_MAX_HASH)
185 return;
186
187 cfg = npc_update_use_hash(rvu, blkaddr,
188 intf, lid, lt, ld);
189 /* Set updated KEX configuration */
190 SET_KEX_LD(intf, lid, lt, ld, cfg);
191 /* Set HASH configuration */
192 SET_KEX_LD_HASH(intf, ld,
193 mkex_hash->hash[intf][ld]);
194 SET_KEX_LD_HASH_MASK(intf, ld, 0,
195 mkex_hash->hash_mask[intf][ld][0]);
196 SET_KEX_LD_HASH_MASK(intf, ld, 1,
197 mkex_hash->hash_mask[intf][ld][1]);
198 SET_KEX_LD_HASH_CTRL(intf, ld,
199 mkex_hash->hash_ctrl[intf][ld]);
200 hash_cnt++;
201 }
202 }
203 }
204 }
205
206 void npc_config_secret_key(struct rvu *rvu, int blkaddr)
207 {
208 struct hw_cap *hwcap = &rvu->hw->cap;
209 struct rvu_hwinfo *hw = rvu->hw;
210 u8 intf;
211
212 if (!hwcap->npc_hash_extract)
213 return;
214
215 for (intf = 0; intf < hw->npc_intfs; intf++) {
216 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_SECRET_KEY0(intf),
217 RVU_NPC_HASH_SECRET_KEY0);
218 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_SECRET_KEY1(intf),
219 RVU_NPC_HASH_SECRET_KEY1);
220 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_SECRET_KEY2(intf),
221 RVU_NPC_HASH_SECRET_KEY2);
222 }
223 }
224
225 void npc_program_mkex_hash(struct rvu *rvu, int blkaddr)
226 {
227 struct npc_mcam_kex_hash *mh = rvu->kpu.mkex_hash;
228 struct hw_cap *hwcap = &rvu->hw->cap;
229 u8 intf, ld, hdr_offset, byte_len;
230 struct rvu_hwinfo *hw = rvu->hw;
231 u64 cfg;
232
233 if (is_cn20k(rvu->pdev))
234 return;
235
236 /* Check if hardware supports hash extraction */
237 if (!hwcap->npc_hash_extract)
238 return;
239
240 /* Check if IPv6 source/destination address
241 * should be hash enabled.
242 * Hashing reduces 128bit SIP/DIP fields to 32bit
243 * so that 224 bit X2 key can be used for IPv6 based filters as well,
244 * which in turn results in more number of MCAM entries available for
245 * use.
246 *
247 * Hashing of IPV6 SIP/DIP is enabled in below scenarios
248 * 1. If the silicon variant supports hashing feature
249 * 2. If the number of bytes of IP addr being extracted is 4 bytes ie
250 * 32bit. The assumption here is that if user wants 8bytes of LSB of
251 * IP addr or full 16 bytes then his intention is not to use 32bit
252 * hash.
253 */
254 for (intf = 0; intf < hw->npc_intfs; intf++) {
255 for (ld = 0; ld < NPC_MAX_LD; ld++) {
256 cfg = rvu_read64(rvu, blkaddr,
257 NPC_AF_INTFX_LIDX_LTX_LDX_CFG(intf,
258 NPC_LID_LC,
259 NPC_LT_LC_IP6,
260 ld));
261 hdr_offset = FIELD_GET(NPC_HDR_OFFSET, cfg);
262 byte_len = FIELD_GET(NPC_BYTESM, cfg);
263 /* Hashing of IPv6 source/destination address should be
264 * enabled if,
265 * hdr_offset == 8 (offset of source IPv6 address) or
266 * hdr_offset == 24 (offset of destination IPv6)
267 * address) and the number of byte to be
268 * extracted is 4. As per hardware configuration
269 * byte_len should be == actual byte_len - 1.
270 * Hence byte_len is checked against 3 but nor 4.
271 */
272 if ((hdr_offset == 8 || hdr_offset == 24) && byte_len == 3)
273 mh->lid_lt_ld_hash_en[intf][NPC_LID_LC][NPC_LT_LC_IP6][ld] = true;
274 }
275 }
276
277 /* Update hash configuration if the field is hash enabled */
278 for (intf = 0; intf < hw->npc_intfs; intf++) {
279 npc_program_mkex_hash_rx(rvu, blkaddr, intf);
280 npc_program_mkex_hash_tx(rvu, blkaddr, intf);
281 }
282 }
283
284 void npc_update_field_hash(struct rvu *rvu, u8 intf,
285 struct mcam_entry_mdata *mdata,
286 int blkaddr,
287 u64 features,
288 struct flow_msg *pkt,
289 struct flow_msg *mask,
290 struct flow_msg *opkt,
291 struct flow_msg *omask)
292 {
293 struct npc_mcam_kex_hash *mkex_hash = rvu->kpu.mkex_hash;
294 struct npc_get_field_hash_info_req req;
295 struct npc_get_field_hash_info_rsp rsp;
296 u8 hash_idx, lid, ltype, ltype_mask;
297 u64 ldata[2], cfg;
298 u32 field_hash;
299 bool en;
300
301 if (is_cn20k(rvu->pdev))
302 return;
303
304 if (!rvu->hw->cap.npc_hash_extract) {
305 dev_dbg(rvu->dev, "%s: Field hash extract feature is not supported\n", __func__);
306 return;
307 }
308
309 req.intf = intf;
310 rvu_mbox_handler_npc_get_field_hash_info(rvu, &req, &rsp);
311
312 for (hash_idx = 0; hash_idx < NPC_MAX_HASH; hash_idx++) {
313 cfg = rvu_read64(rvu, blkaddr, NPC_AF_INTFX_HASHX_CFG(intf, hash_idx));
314 en = !!(cfg & BIT_ULL(11)) && (cfg & BIT_ULL(12));
315 if (!en)
316 continue;
317
318 lid = (cfg & GENMASK_ULL(10, 8)) >> 8;
319 ltype = (cfg & GENMASK_ULL(7, 4)) >> 4;
320 ltype_mask = cfg & GENMASK_ULL(3, 0);
321
322 if (!mkex_hash->lid_lt_ld_hash_en[intf][lid][ltype][hash_idx])
323 continue;
324
325 /* If hash extract enabled is supported for IPv6 then
326 * 128 bit IPv6 source and destination addressed
327 * is hashed to 32 bit value.
328 */
329 if ((ltype & ltype_mask) != NPC_LT_LC_IP6)
330 continue;
331
332 /* ld[0] == hash_idx[0] == Source IPv6
333 * ld[1] == hash_idx[1] == Destination IPv6
334 */
335 if ((features & BIT_ULL(NPC_SIP_IPV6)) && !hash_idx) {
336 u32 src_ip[IPV6_WORDS];
337
338 be32_to_cpu_array(src_ip, pkt->ip6src, IPV6_WORDS);
339 ldata[1] = (u64)src_ip[0] << 32 | src_ip[1];
340 ldata[0] = (u64)src_ip[2] << 32 | src_ip[3];
341 field_hash = npc_field_hash_calc(ldata, rsp, intf,
342 hash_idx);
343 npc_update_entry(rvu, NPC_SIP_IPV6, mdata, field_hash,
344 0, GENMASK(31, 0), 0, intf);
345 memcpy(&opkt->ip6src, &pkt->ip6src,
346 sizeof(pkt->ip6src));
347 memcpy(&omask->ip6src, &mask->ip6src,
348 sizeof(mask->ip6src));
349 continue;
350 }
351
352 if ((features & BIT_ULL(NPC_DIP_IPV6)) && hash_idx) {
353 u32 dst_ip[IPV6_WORDS];
354
355 be32_to_cpu_array(dst_ip, pkt->ip6dst, IPV6_WORDS);
356 ldata[1] = (u64)dst_ip[0] << 32 | dst_ip[1];
357 ldata[0] = (u64)dst_ip[2] << 32 | dst_ip[3];
358 field_hash = npc_field_hash_calc(ldata, rsp, intf,
359 hash_idx);
360 npc_update_entry(rvu, NPC_DIP_IPV6, mdata,
361 field_hash, 0, GENMASK(31, 0),
362 0, intf);
363 memcpy(&opkt->ip6dst, &pkt->ip6dst,
364 sizeof(pkt->ip6dst));
365 memcpy(&omask->ip6dst, &mask->ip6dst,
366 sizeof(mask->ip6dst));
367 continue;
368 }
369 }
370 }
371
372 int rvu_mbox_handler_npc_get_field_hash_info(struct rvu *rvu,
373 struct npc_get_field_hash_info_req *req,
374 struct npc_get_field_hash_info_rsp *rsp)
375 {
376 u64 *secret_key = rsp->secret_key;
377 u8 intf = req->intf;
378 int i, j, blkaddr;
379
380 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
381 if (blkaddr < 0) {
382 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
383 return -EINVAL;
384 }
385
386 secret_key[0] = rvu_read64(rvu, blkaddr, NPC_AF_INTFX_SECRET_KEY0(intf));
387 secret_key[1] = rvu_read64(rvu, blkaddr, NPC_AF_INTFX_SECRET_KEY1(intf));
388 secret_key[2] = rvu_read64(rvu, blkaddr, NPC_AF_INTFX_SECRET_KEY2(intf));
389
390 for (i = 0; i < NPC_MAX_HASH; i++) {
391 for (j = 0; j < NPC_MAX_HASH_MASK; j++) {
392 rsp->hash_mask[NIX_INTF_RX][i][j] =
393 GET_KEX_LD_HASH_MASK(NIX_INTF_RX, i, j);
394 rsp->hash_mask[NIX_INTF_TX][i][j] =
395 GET_KEX_LD_HASH_MASK(NIX_INTF_TX, i, j);
396 }
397 }
398
399 for (i = 0; i < NPC_MAX_INTF; i++)
400 for (j = 0; j < NPC_MAX_HASH; j++)
401 rsp->hash_ctrl[i][j] = GET_KEX_LD_HASH_CTRL(i, j);
402
403 return 0;
404 }
405
406 /**
407 * rvu_exact_prepare_mdata - Make mdata for mcam entry
408 * @mac: MAC address
409 * @chan: Channel number.
410 * @ctype: Channel Type.
411 * @mask: LDATA mask.
412 * Return: Meta data
413 */
414 static u64 rvu_exact_prepare_mdata(u8 *mac, u16 chan, u16 ctype, u64 mask)
415 {
416 u64 ldata = ether_addr_to_u64(mac);
417
418 /* Please note that mask is 48bit which excludes chan and ctype.
419 * Increase mask bits if we need to include them as well.
420 */
421 ldata |= ((u64)chan << 48);
422 ldata |= ((u64)ctype << 60);
423 ldata &= mask;
424 ldata = ldata << 2;
425
426 return ldata;
427 }
428
429 /**
430 * rvu_exact_calculate_hash - calculate hash index to mem table.
431 * @rvu: resource virtualization unit.
432 * @chan: Channel number
433 * @ctype: Channel type.
434 * @mac: MAC address
435 * @mask: HASH mask.
436 * @table_depth: Depth of table.
437 * Return: Hash value
438 */
439 static u32 rvu_exact_calculate_hash(struct rvu *rvu, u16 chan, u16 ctype, u8 *mac,
440 u64 mask, u32 table_depth)
441 {
442 struct npc_exact_table *table = rvu->hw->table;
443 u64 hash_key[2];
444 u64 key_in[2];
445 u64 ldata;
446 u32 hash;
447
448 key_in[0] = RVU_NPC_HASH_SECRET_KEY0;
449 key_in[1] = RVU_NPC_HASH_SECRET_KEY2;
450
451 hash_key[0] = key_in[0] << 31;
452 hash_key[0] |= key_in[1];
453 hash_key[1] = key_in[0] >> 33;
454
455 ldata = rvu_exact_prepare_mdata(mac, chan, ctype, mask);
456
457 dev_dbg(rvu->dev, "%s: ldata=0x%llx hash_key0=0x%llx hash_key2=0x%llx\n", __func__,
458 ldata, hash_key[1], hash_key[0]);
459 hash = rvu_npc_toeplitz_hash(&ldata, (u64 *)hash_key, 64, 95);
460
461 hash &= table->mem_table.hash_mask;
462 hash += table->mem_table.hash_offset;
463 dev_dbg(rvu->dev, "%s: hash=%x\n", __func__, hash);
464
465 return hash;
466 }
467
468 /**
469 * rvu_npc_exact_alloc_mem_table_entry - find free entry in 4 way table.
470 * @rvu: resource virtualization unit.
471 * @way: Indicate way to table.
472 * @index: Hash index to 4 way table.
473 * @hash: Hash value.
474 *
475 * Searches 4 way table using hash index. Returns 0 on success.
476 * Return: 0 upon success.
477 */
478 static int rvu_npc_exact_alloc_mem_table_entry(struct rvu *rvu, u8 *way,
479 u32 *index, unsigned int hash)
480 {
481 struct npc_exact_table *table;
482 int depth, i;
483
484 table = rvu->hw->table;
485 depth = table->mem_table.depth;
486
487 /* Check all the 4 ways for a free slot. */
488 mutex_lock(&table->lock);
489 for (i = 0; i < table->mem_table.ways; i++) {
490 if (test_bit(hash + i * depth, table->mem_table.bmap))
491 continue;
492
493 set_bit(hash + i * depth, table->mem_table.bmap);
494 mutex_unlock(&table->lock);
495
496 dev_dbg(rvu->dev, "%s: mem table entry alloc success (way=%d index=%d)\n",
497 __func__, i, hash);
498
499 *way = i;
500 *index = hash;
501 return 0;
502 }
503 mutex_unlock(&table->lock);
504
505 dev_dbg(rvu->dev, "%s: No space in 4 way exact way, weight=%u\n", __func__,
506 bitmap_weight(table->mem_table.bmap, table->mem_table.depth));
507 return -ENOSPC;
508 }
509
510 /**
511 * rvu_npc_exact_free_id - Free seq id from bitmat.
512 * @rvu: Resource virtualization unit.
513 * @seq_id: Sequence identifier to be freed.
514 */
515 static void rvu_npc_exact_free_id(struct rvu *rvu, u32 seq_id)
516 {
517 struct npc_exact_table *table;
518
519 table = rvu->hw->table;
520 mutex_lock(&table->lock);
521 clear_bit(seq_id, table->id_bmap);
522 mutex_unlock(&table->lock);
523 dev_dbg(rvu->dev, "%s: freed id %d\n", __func__, seq_id);
524 }
525
526 /**
527 * rvu_npc_exact_alloc_id - Alloc seq id from bitmap.
528 * @rvu: Resource virtualization unit.
529 * @seq_id: Sequence identifier.
530 * Return: True or false.
531 */
532 static bool rvu_npc_exact_alloc_id(struct rvu *rvu, u32 *seq_id)
533 {
534 struct npc_exact_table *table;
535 u32 idx;
536
537 table = rvu->hw->table;
538
539 mutex_lock(&table->lock);
540 idx = find_first_zero_bit(table->id_bmap, table->tot_ids);
541 if (idx == table->tot_ids) {
542 mutex_unlock(&table->lock);
543 dev_err(rvu->dev, "%s: No space in id bitmap (%d)\n",
544 __func__, table->tot_ids);
545
546 return false;
547 }
548
549 /* Mark bit map to indicate that slot is used.*/
550 set_bit(idx, table->id_bmap);
551 mutex_unlock(&table->lock);
552
553 *seq_id = idx;
554 dev_dbg(rvu->dev, "%s: Allocated id (%d)\n", __func__, *seq_id);
555
556 return true;
557 }
558
559 /**
560 * rvu_npc_exact_alloc_cam_table_entry - find free slot in fully associative table.
561 * @rvu: resource virtualization unit.
562 * @index: Index to exact CAM table.
563 * Return: 0 upon success; else error number.
564 */
565 static int rvu_npc_exact_alloc_cam_table_entry(struct rvu *rvu, int *index)
566 {
567 struct npc_exact_table *table;
568 u32 idx;
569
570 table = rvu->hw->table;
571
572 mutex_lock(&table->lock);
573 idx = find_first_zero_bit(table->cam_table.bmap, table->cam_table.depth);
574 if (idx == table->cam_table.depth) {
575 mutex_unlock(&table->lock);
576 dev_info(rvu->dev, "%s: No space in exact cam table, weight=%u\n", __func__,
577 bitmap_weight(table->cam_table.bmap, table->cam_table.depth));
578 return -ENOSPC;
579 }
580
581 /* Mark bit map to indicate that slot is used.*/
582 set_bit(idx, table->cam_table.bmap);
583 mutex_unlock(&table->lock);
584
585 *index = idx;
586 dev_dbg(rvu->dev, "%s: cam table entry alloc success (index=%d)\n",
587 __func__, idx);
588 return 0;
589 }
590
591 /**
592 * rvu_exact_prepare_table_entry - Data for exact match table entry.
593 * @rvu: Resource virtualization unit.
594 * @enable: Enable/Disable entry
595 * @ctype: Software defined channel type. Currently set as 0.
596 * @chan: Channel number.
597 * @mac_addr: Destination mac address.
598 * Return: mdata for exact match table.
599 */
600 static u64 rvu_exact_prepare_table_entry(struct rvu *rvu, bool enable,
601 u8 ctype, u16 chan, u8 *mac_addr)
602
603 {
604 u64 ldata = ether_addr_to_u64(mac_addr);
605
606 /* Enable or disable */
607 u64 mdata = FIELD_PREP(GENMASK_ULL(63, 63), enable ? 1 : 0);
608
609 /* Set Ctype */
610 mdata |= FIELD_PREP(GENMASK_ULL(61, 60), ctype);
611
612 /* Set chan */
613 mdata |= FIELD_PREP(GENMASK_ULL(59, 48), chan);
614
615 /* MAC address */
616 mdata |= FIELD_PREP(GENMASK_ULL(47, 0), ldata);
617
618 return mdata;
619 }
620
621 /**
622 * rvu_exact_config_secret_key - Configure secret key.
623 * @rvu: Resource virtualization unit.
624 */
625 static void rvu_exact_config_secret_key(struct rvu *rvu)
626 {
627 int blkaddr;
628
629 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
630 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_EXACT_SECRET0(NIX_INTF_RX),
631 RVU_NPC_HASH_SECRET_KEY0);
632
633 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_EXACT_SECRET1(NIX_INTF_RX),
634 RVU_NPC_HASH_SECRET_KEY1);
635
636 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_EXACT_SECRET2(NIX_INTF_RX),
637 RVU_NPC_HASH_SECRET_KEY2);
638 }
639
640 /**
641 * rvu_exact_config_search_key - Configure search key
642 * @rvu: Resource virtualization unit.
643 */
644 static void rvu_exact_config_search_key(struct rvu *rvu)
645 {
646 int blkaddr;
647 u64 reg_val;
648
649 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
650
651 /* HDR offset */
652 reg_val = FIELD_PREP(GENMASK_ULL(39, 32), 0);
653
654 /* BYTESM1, number of bytes - 1 */
655 reg_val |= FIELD_PREP(GENMASK_ULL(18, 16), ETH_ALEN - 1);
656
657 /* Enable LID and set LID to NPC_LID_LA */
658 reg_val |= FIELD_PREP(GENMASK_ULL(11, 11), 1);
659 reg_val |= FIELD_PREP(GENMASK_ULL(10, 8), NPC_LID_LA);
660
661 /* Clear layer type based extraction */
662
663 /* Disable LT_EN */
664 reg_val |= FIELD_PREP(GENMASK_ULL(12, 12), 0);
665
666 /* Set LTYPE_MATCH to 0 */
667 reg_val |= FIELD_PREP(GENMASK_ULL(7, 4), 0);
668
669 /* Set LTYPE_MASK to 0 */
670 reg_val |= FIELD_PREP(GENMASK_ULL(3, 0), 0);
671
672 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_EXACT_CFG(NIX_INTF_RX), reg_val);
673 }
674
675 /**
676 * rvu_exact_config_result_ctrl - Set exact table hash control
677 * @rvu: Resource virtualization unit.
678 * @depth: Depth of Exact match table.
679 *
680 * Sets mask and offset for hash for mem table.
681 */
682 static void rvu_exact_config_result_ctrl(struct rvu *rvu, uint32_t depth)
683 {
684 int blkaddr;
685 u64 reg = 0;
686
687 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
688
689 /* Set mask. Note that depth is a power of 2 */
690 rvu->hw->table->mem_table.hash_mask = (depth - 1);
691 reg |= FIELD_PREP(GENMASK_ULL(42, 32), (depth - 1));
692
693 /* Set offset as 0 */
694 rvu->hw->table->mem_table.hash_offset = 0;
695 reg |= FIELD_PREP(GENMASK_ULL(10, 0), 0);
696
697 /* Set reg for RX */
698 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_EXACT_RESULT_CTL(NIX_INTF_RX), reg);
699 /* Store hash mask and offset for s/w algorithm */
700 }
701
702 /**
703 * rvu_exact_config_table_mask - Set exact table mask.
704 * @rvu: Resource virtualization unit.
705 */
706 static void rvu_exact_config_table_mask(struct rvu *rvu)
707 {
708 int blkaddr;
709 u64 mask = 0;
710
711 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
712
713 /* Don't use Ctype */
714 mask |= FIELD_PREP(GENMASK_ULL(61, 60), 0);
715
716 /* Set chan */
717 mask |= GENMASK_ULL(59, 48);
718
719 /* Full ldata */
720 mask |= GENMASK_ULL(47, 0);
721
722 /* Store mask for s/w hash calcualtion */
723 rvu->hw->table->mem_table.mask = mask;
724
725 /* Set mask for RX.*/
726 rvu_write64(rvu, blkaddr, NPC_AF_INTFX_EXACT_MASK(NIX_INTF_RX), mask);
727 }
728
729 /**
730 * rvu_npc_exact_get_max_entries - Get total number of entries in table.
731 * @rvu: resource virtualization unit.
732 * Return: Maximum table entries possible.
733 */
734 u32 rvu_npc_exact_get_max_entries(struct rvu *rvu)
735 {
736 struct npc_exact_table *table;
737
738 table = rvu->hw->table;
739 return table->tot_ids;
740 }
741
742 /**
743 * rvu_npc_exact_has_match_table - Checks support for exact match.
744 * @rvu: resource virtualization unit.
745 * Return: True if exact match table is supported/enabled.
746 */
747 bool rvu_npc_exact_has_match_table(struct rvu *rvu)
748 {
749 return rvu->hw->cap.npc_exact_match_enabled;
750 }
751
752 /**
753 * __rvu_npc_exact_find_entry_by_seq_id - find entry by id
754 * @rvu: resource virtualization unit.
755 * @seq_id: Sequence identifier.
756 *
757 * Caller should acquire the lock.
758 * Return: Pointer to table entry.
759 */
760 static struct npc_exact_table_entry *
761 __rvu_npc_exact_find_entry_by_seq_id(struct rvu *rvu, u32 seq_id)
762 {
763 struct npc_exact_table *table = rvu->hw->table;
764 struct npc_exact_table_entry *entry = NULL;
765 struct list_head *lhead;
766
767 lhead = &table->lhead_gbl;
768
769 /* traverse to find the matching entry */
770 list_for_each_entry(entry, lhead, glist) {
771 if (entry->seq_id != seq_id)
772 continue;
773
774 return entry;
775 }
776
777 return NULL;
778 }
779
780 /**
781 * rvu_npc_exact_add_to_list - Add entry to list
782 * @rvu: resource virtualization unit.
783 * @opc_type: OPCODE to select MEM/CAM table.
784 * @ways: MEM table ways.
785 * @index: Index in MEM/CAM table.
786 * @cgx_id: CGX identifier.
787 * @lmac_id: LMAC identifier.
788 * @mac_addr: MAC address.
789 * @chan: Channel number.
790 * @ctype: Channel Type.
791 * @seq_id: Sequence identifier
792 * @cmd: True if function is called by ethtool cmd
793 * @mcam_idx: NPC mcam index of DMAC entry in NPC mcam.
794 * @pcifunc: pci function
795 * Return: 0 upon success.
796 */
797 static int rvu_npc_exact_add_to_list(struct rvu *rvu, enum npc_exact_opc_type opc_type, u8 ways,
798 u32 index, u8 cgx_id, u8 lmac_id, u8 *mac_addr, u16 chan,
799 u8 ctype, u32 *seq_id, bool cmd, u32 mcam_idx, u16 pcifunc)
800 {
801 struct npc_exact_table_entry *entry, *tmp, *iter;
802 struct npc_exact_table *table = rvu->hw->table;
803 struct list_head *lhead, *pprev;
804
805 WARN_ON(ways >= NPC_EXACT_TBL_MAX_WAYS);
806
807 if (!rvu_npc_exact_alloc_id(rvu, seq_id)) {
808 dev_err(rvu->dev, "%s: Generate seq id failed\n", __func__);
809 return -EFAULT;
810 }
811
812 entry = kmalloc_obj(*entry);
813 if (!entry) {
814 rvu_npc_exact_free_id(rvu, *seq_id);
815 dev_err(rvu->dev, "%s: Memory allocation failed\n", __func__);
816 return -ENOMEM;
817 }
818
819 mutex_lock(&table->lock);
820 switch (opc_type) {
821 case NPC_EXACT_OPC_CAM:
822 lhead = &table->lhead_cam_tbl_entry;
823 table->cam_tbl_entry_cnt++;
824 break;
825
826 case NPC_EXACT_OPC_MEM:
827 lhead = &table->lhead_mem_tbl_entry[ways];
828 table->mem_tbl_entry_cnt++;
829 break;
830
831 default:
832 mutex_unlock(&table->lock);
833 kfree(entry);
834 rvu_npc_exact_free_id(rvu, *seq_id);
835
836 dev_err(rvu->dev, "%s: Unknown opc type%d\n", __func__, opc_type);
837 return -EINVAL;
838 }
839
840 /* Add to global list */
841 INIT_LIST_HEAD(&entry->glist);
842 list_add_tail(&entry->glist, &table->lhead_gbl);
843 INIT_LIST_HEAD(&entry->list);
844 entry->index = index;
845 entry->ways = ways;
846 entry->opc_type = opc_type;
847
848 entry->pcifunc = pcifunc;
849
850 ether_addr_copy(entry->mac, mac_addr);
851 entry->chan = chan;
852 entry->ctype = ctype;
853 entry->cgx_id = cgx_id;
854 entry->lmac_id = lmac_id;
855
856 entry->seq_id = *seq_id;
857
858 entry->mcam_idx = mcam_idx;
859 entry->cmd = cmd;
860
861 pprev = lhead;
862
863 /* Insert entry in ascending order of index */
864 list_for_each_entry_safe(iter, tmp, lhead, list) {
865 if (index < iter->index)
866 break;
867
868 pprev = &iter->list;
869 }
870
871 /* Add to each table list */
872 list_add(&entry->list, pprev);
873 mutex_unlock(&table->lock);
874 return 0;
875 }
876
877 /**
878 * rvu_npc_exact_mem_table_write - Wrapper for register write
879 * @rvu: resource virtualization unit.
880 * @blkaddr: Block address
881 * @ways: ways for MEM table.
882 * @index: Index in MEM
883 * @mdata: Meta data to be written to register.
884 */
885 static void rvu_npc_exact_mem_table_write(struct rvu *rvu, int blkaddr, u8 ways,
886 u32 index, u64 mdata)
887 {
888 rvu_write64(rvu, blkaddr, NPC_AF_EXACT_MEM_ENTRY(ways, index), mdata);
889 }
890
891 /**
892 * rvu_npc_exact_cam_table_write - Wrapper for register write
893 * @rvu: resource virtualization unit.
894 * @blkaddr: Block address
895 * @index: Index in MEM
896 * @mdata: Meta data to be written to register.
897 */
898 static void rvu_npc_exact_cam_table_write(struct rvu *rvu, int blkaddr,
899 u32 index, u64 mdata)
900 {
901 rvu_write64(rvu, blkaddr, NPC_AF_EXACT_CAM_ENTRY(index), mdata);
902 }
903
904 /**
905 * rvu_npc_exact_dealloc_table_entry - dealloc table entry
906 * @rvu: resource virtualization unit.
907 * @opc_type: OPCODE for selection of table(MEM or CAM)
908 * @ways: ways if opc_type is MEM table.
909 * @index: Index of MEM or CAM table.
910 * Return: 0 upon success.
911 */
912 static int rvu_npc_exact_dealloc_table_entry(struct rvu *rvu, enum npc_exact_opc_type opc_type,
913 u8 ways, u32 index)
914 {
915 int blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
916 struct npc_exact_table *table;
917 u8 null_dmac[6] = { 0 };
918 int depth;
919
920 /* Prepare entry with all fields set to zero */
921 u64 null_mdata = rvu_exact_prepare_table_entry(rvu, false, 0, 0, null_dmac);
922
923 table = rvu->hw->table;
924 depth = table->mem_table.depth;
925
926 mutex_lock(&table->lock);
927
928 switch (opc_type) {
929 case NPC_EXACT_OPC_CAM:
930
931 /* Check whether entry is used already */
932 if (!test_bit(index, table->cam_table.bmap)) {
933 mutex_unlock(&table->lock);
934 dev_err(rvu->dev, "%s: Trying to free an unused entry ways=%d index=%d\n",
935 __func__, ways, index);
936 return -EINVAL;
937 }
938
939 rvu_npc_exact_cam_table_write(rvu, blkaddr, index, null_mdata);
940 clear_bit(index, table->cam_table.bmap);
941 break;
942
943 case NPC_EXACT_OPC_MEM:
944
945 /* Check whether entry is used already */
946 if (!test_bit(index + ways * depth, table->mem_table.bmap)) {
947 mutex_unlock(&table->lock);
948 dev_err(rvu->dev, "%s: Trying to free an unused entry index=%d\n",
949 __func__, index);
950 return -EINVAL;
951 }
952
953 rvu_npc_exact_mem_table_write(rvu, blkaddr, ways, index, null_mdata);
954 clear_bit(index + ways * depth, table->mem_table.bmap);
955 break;
956
957 default:
958 mutex_unlock(&table->lock);
959 dev_err(rvu->dev, "%s: invalid opc type %d", __func__, opc_type);
960 return -ENOSPC;
961 }
962
963 mutex_unlock(&table->lock);
964
965 dev_dbg(rvu->dev, "%s: Successfully deleted entry (index=%d, ways=%d opc_type=%d\n",
966 __func__, index, ways, opc_type);
967
968 return 0;
969 }
970
971 /**
972 * rvu_npc_exact_alloc_table_entry - Allociate an entry
973 * @rvu: resource virtualization unit.
974 * @mac: MAC address.
975 * @chan: Channel number.
976 * @ctype: Channel Type.
977 * @index: Index of MEM table or CAM table.
978 * @ways: Ways. Only valid for MEM table.
979 * @opc_type: OPCODE to select table (MEM or CAM)
980 *
981 * Try allocating a slot from MEM table. If all 4 ways
982 * slot are full for a hash index, check availability in
983 * 32-entry CAM table for allocation.
984 * Return: 0 upon success.
985 */
986 static int rvu_npc_exact_alloc_table_entry(struct rvu *rvu, char *mac, u16 chan, u8 ctype,
987 u32 *index, u8 *ways, enum npc_exact_opc_type *opc_type)
988 {
989 struct npc_exact_table *table;
990 unsigned int hash;
991 int err;
992
993 table = rvu->hw->table;
994
995 /* Check in 4-ways mem entry for free slote */
996 hash = rvu_exact_calculate_hash(rvu, chan, ctype, mac, table->mem_table.mask,
997 table->mem_table.depth);
998 err = rvu_npc_exact_alloc_mem_table_entry(rvu, ways, index, hash);
999 if (!err) {
1000 *opc_type = NPC_EXACT_OPC_MEM;
1001 dev_dbg(rvu->dev, "%s: inserted in 4 ways hash table ways=%d, index=%d\n",
1002 __func__, *ways, *index);
1003 return 0;
1004 }
1005
1006 dev_dbg(rvu->dev, "%s: failed to insert in 4 ways hash table\n", __func__);
1007
1008 /* wayss is 0 for cam table */
1009 *ways = 0;
1010 err = rvu_npc_exact_alloc_cam_table_entry(rvu, index);
1011 if (!err) {
1012 *opc_type = NPC_EXACT_OPC_CAM;
1013 dev_dbg(rvu->dev, "%s: inserted in fully associative hash table index=%u\n",
1014 __func__, *index);
1015 return 0;
1016 }
1017
1018 dev_err(rvu->dev, "%s: failed to insert in fully associative hash table\n", __func__);
1019 return -ENOSPC;
1020 }
1021
1022 /**
1023 * rvu_npc_exact_save_drop_rule_chan_and_mask - Save drop rules info in data base.
1024 * @rvu: resource virtualization unit.
1025 * @drop_mcam_idx: Drop rule index in NPC mcam.
1026 * @chan_val: Channel value.
1027 * @chan_mask: Channel Mask.
1028 * @pcifunc: pcifunc of interface.
1029 * Return: True upon success.
1030 */
1031 static bool rvu_npc_exact_save_drop_rule_chan_and_mask(struct rvu *rvu, int drop_mcam_idx,
1032 u64 chan_val, u64 chan_mask, u16 pcifunc)
1033 {
1034 struct npc_exact_table *table;
1035 int i;
1036
1037 table = rvu->hw->table;
1038
1039 for (i = 0; i < NPC_MCAM_DROP_RULE_MAX; i++) {
1040 if (!table->drop_rule_map[i].valid)
1041 break;
1042
1043 if (table->drop_rule_map[i].chan_val != (u16)chan_val)
1044 continue;
1045
1046 if (table->drop_rule_map[i].chan_mask != (u16)chan_mask)
1047 continue;
1048
1049 return false;
1050 }
1051
1052 if (i == NPC_MCAM_DROP_RULE_MAX)
1053 return false;
1054
1055 table->drop_rule_map[i].drop_rule_idx = drop_mcam_idx;
1056 table->drop_rule_map[i].chan_val = (u16)chan_val;
1057 table->drop_rule_map[i].chan_mask = (u16)chan_mask;
1058 table->drop_rule_map[i].pcifunc = pcifunc;
1059 table->drop_rule_map[i].valid = true;
1060 return true;
1061 }
1062
1063 /**
1064 * rvu_npc_exact_calc_drop_rule_chan_and_mask - Calculate Channel number and mask.
1065 * @rvu: resource virtualization unit.
1066 * @intf_type: Interface type (SDK, LBK or CGX)
1067 * @cgx_id: CGX identifier.
1068 * @lmac_id: LAMC identifier.
1069 * @val: Channel number.
1070 * @mask: Channel mask.
1071 * Return: True upon success.
1072 */
1073 static bool rvu_npc_exact_calc_drop_rule_chan_and_mask(struct rvu *rvu, u8 intf_type,
1074 u8 cgx_id, u8 lmac_id,
1075 u64 *val, u64 *mask)
1076 {
1077 u16 chan_val, chan_mask;
1078
1079 /* No support for SDP and LBK */
1080 if (intf_type != NIX_INTF_TYPE_CGX)
1081 return false;
1082
1083 chan_val = rvu_nix_chan_cgx(rvu, cgx_id, lmac_id, 0);
1084 chan_mask = 0xfff;
1085
1086 if (val)
1087 *val = chan_val;
1088
1089 if (mask)
1090 *mask = chan_mask;
1091
1092 return true;
1093 }
1094
1095 /**
1096 * rvu_npc_exact_drop_rule_to_pcifunc - Retrieve pcifunc
1097 * @rvu: resource virtualization unit.
1098 * @drop_rule_idx: Drop rule index in NPC mcam.
1099 *
1100 * Debugfs (exact_drop_cnt) entry displays pcifunc for interface
1101 * by retrieving the pcifunc value from data base.
1102 * Return: Drop rule index.
1103 */
1104 u16 rvu_npc_exact_drop_rule_to_pcifunc(struct rvu *rvu, u32 drop_rule_idx)
1105 {
1106 struct npc_exact_table *table;
1107 int i;
1108
1109 table = rvu->hw->table;
1110
1111 for (i = 0; i < NPC_MCAM_DROP_RULE_MAX; i++) {
1112 if (!table->drop_rule_map[i].valid)
1113 break;
1114
1115 if (table->drop_rule_map[i].drop_rule_idx != drop_rule_idx)
1116 continue;
1117
1118 return table->drop_rule_map[i].pcifunc;
1119 }
1120
1121 dev_err(rvu->dev, "%s: drop mcam rule index (%d) >= NPC_MCAM_DROP_RULE_MAX\n",
1122 __func__, drop_rule_idx);
1123 return -1;
1124 }
1125
1126 /**
1127 * rvu_npc_exact_get_drop_rule_info - Get drop rule information.
1128 * @rvu: resource virtualization unit.
1129 * @intf_type: Interface type (CGX, SDP or LBK)
1130 * @cgx_id: CGX identifier.
1131 * @lmac_id: LMAC identifier.
1132 * @drop_mcam_idx: NPC mcam drop rule index.
1133 * @val: Channel value.
1134 * @mask: Channel mask.
1135 * @pcifunc: pcifunc of interface corresponding to the drop rule.
1136 * Return: True upon success.
1137 */
1138 static bool rvu_npc_exact_get_drop_rule_info(struct rvu *rvu, u8 intf_type, u8 cgx_id,
1139 u8 lmac_id, u32 *drop_mcam_idx, u64 *val,
1140 u64 *mask, u16 *pcifunc)
1141 {
1142 struct npc_exact_table *table;
1143 u64 chan_val, chan_mask;
1144 bool rc;
1145 int i;
1146
1147 table = rvu->hw->table;
1148
1149 if (intf_type != NIX_INTF_TYPE_CGX) {
1150 dev_err(rvu->dev, "%s: No drop rule for LBK/SDP mode\n", __func__);
1151 return false;
1152 }
1153
1154 rc = rvu_npc_exact_calc_drop_rule_chan_and_mask(rvu, intf_type, cgx_id,
1155 lmac_id, &chan_val, &chan_mask);
1156 if (!rc)
1157 return false;
1158
1159 for (i = 0; i < NPC_MCAM_DROP_RULE_MAX; i++) {
1160 if (!table->drop_rule_map[i].valid)
1161 break;
1162
1163 if (table->drop_rule_map[i].chan_val != (u16)chan_val)
1164 continue;
1165
1166 if (val)
1167 *val = table->drop_rule_map[i].chan_val;
1168 if (mask)
1169 *mask = table->drop_rule_map[i].chan_mask;
1170 if (pcifunc)
1171 *pcifunc = table->drop_rule_map[i].pcifunc;
1172
1173 *drop_mcam_idx = i;
1174 return true;
1175 }
1176
1177 if (i == NPC_MCAM_DROP_RULE_MAX) {
1178 dev_err(rvu->dev, "%s: drop mcam rule index (%d) >= NPC_MCAM_DROP_RULE_MAX\n",
1179 __func__, *drop_mcam_idx);
1180 return false;
1181 }
1182
1183 dev_err(rvu->dev, "%s: Could not retrieve for cgx=%d, lmac=%d\n",
1184 __func__, cgx_id, lmac_id);
1185 return false;
1186 }
1187
1188 /**
1189 * __rvu_npc_exact_cmd_rules_cnt_update - Update number dmac rules against a drop rule.
1190 * @rvu: resource virtualization unit.
1191 * @drop_mcam_idx: NPC mcam drop rule index.
1192 * @val: +1 or -1.
1193 * @enable_or_disable_cam: If no exact match rules against a drop rule, disable it.
1194 *
1195 * when first exact match entry against a drop rule is added, enable_or_disable_cam
1196 * is set to true. When last exact match entry against a drop rule is deleted,
1197 * enable_or_disable_cam is set to true.
1198 * Return: Number of rules
1199 */
1200 static u16 __rvu_npc_exact_cmd_rules_cnt_update(struct rvu *rvu, int drop_mcam_idx,
1201 int val, bool *enable_or_disable_cam)
1202 {
1203 struct npc_exact_table *table;
1204 u16 *cnt, old_cnt;
1205 bool promisc;
1206
1207 table = rvu->hw->table;
1208 promisc = table->promisc_mode[drop_mcam_idx];
1209
1210 cnt = &table->cnt_cmd_rules[drop_mcam_idx];
1211 old_cnt = *cnt;
1212
1213 *cnt += val;
1214
1215 if (!enable_or_disable_cam)
1216 goto done;
1217
1218 *enable_or_disable_cam = false;
1219
1220 if (promisc)
1221 goto done;
1222
1223 /* If all rules are deleted and not already in promisc mode;
1224 * disable cam
1225 */
1226 if (!*cnt && val < 0) {
1227 *enable_or_disable_cam = true;
1228 goto done;
1229 }
1230
1231 /* If rule got added and not already in promisc mode; enable cam */
1232 if (!old_cnt && val > 0) {
1233 *enable_or_disable_cam = true;
1234 goto done;
1235 }
1236
1237 done:
1238 return *cnt;
1239 }
1240
1241 /**
1242 * rvu_npc_exact_del_table_entry_by_id - Delete and free table entry.
1243 * @rvu: resource virtualization unit.
1244 * @seq_id: Sequence identifier of the entry.
1245 *
1246 * Deletes entry from linked lists and free up slot in HW MEM or CAM
1247 * table.
1248 * Return: 0 upon success.
1249 */
1250 static int rvu_npc_exact_del_table_entry_by_id(struct rvu *rvu, u32 seq_id)
1251 {
1252 struct npc_exact_table_entry *entry = NULL;
1253 struct npc_exact_table *table;
1254 bool disable_cam = false;
1255 u32 drop_mcam_idx = -1;
1256 int *cnt;
1257 bool rc;
1258
1259 table = rvu->hw->table;
1260
1261 mutex_lock(&table->lock);
1262
1263 /* Lookup for entry which needs to be updated */
1264 entry = __rvu_npc_exact_find_entry_by_seq_id(rvu, seq_id);
1265 if (!entry) {
1266 dev_dbg(rvu->dev, "%s: failed to find entry for id=%d\n", __func__, seq_id);
1267 mutex_unlock(&table->lock);
1268 return -ENODATA;
1269 }
1270
1271 cnt = (entry->opc_type == NPC_EXACT_OPC_CAM) ? &table->cam_tbl_entry_cnt :
1272 &table->mem_tbl_entry_cnt;
1273
1274 /* delete from lists */
1275 list_del_init(&entry->list);
1276 list_del_init(&entry->glist);
1277
1278 (*cnt)--;
1279
1280 rc = rvu_npc_exact_get_drop_rule_info(rvu, NIX_INTF_TYPE_CGX, entry->cgx_id,
1281 entry->lmac_id, &drop_mcam_idx, NULL, NULL, NULL);
1282 if (!rc) {
1283 dev_dbg(rvu->dev, "%s: failed to retrieve drop info for id=0x%x\n",
1284 __func__, seq_id);
1285 mutex_unlock(&table->lock);
1286 return -ENODATA;
1287 }
1288
1289 if (entry->cmd)
1290 __rvu_npc_exact_cmd_rules_cnt_update(rvu, drop_mcam_idx, -1, &disable_cam);
1291
1292 /* No dmac filter rules; disable drop on hit rule */
1293 if (disable_cam) {
1294 rvu_npc_enable_mcam_by_entry_index(rvu, drop_mcam_idx, NIX_INTF_RX, false);
1295 dev_dbg(rvu->dev, "%s: Disabling mcam idx %d\n",
1296 __func__, drop_mcam_idx);
1297 }
1298
1299 mutex_unlock(&table->lock);
1300
1301 rvu_npc_exact_dealloc_table_entry(rvu, entry->opc_type, entry->ways, entry->index);
1302
1303 rvu_npc_exact_free_id(rvu, seq_id);
1304
1305 dev_dbg(rvu->dev, "%s: delete entry success for id=0x%x, mca=%pM\n",
1306 __func__, seq_id, entry->mac);
1307 kfree(entry);
1308
1309 return 0;
1310 }
1311
1312 /**
1313 * rvu_npc_exact_add_table_entry - Adds a table entry
1314 * @rvu: resource virtualization unit.
1315 * @cgx_id: cgx identifier.
1316 * @lmac_id: lmac identifier.
1317 * @mac: MAC address.
1318 * @chan: Channel number.
1319 * @ctype: Channel Type.
1320 * @seq_id: Sequence number.
1321 * @cmd: Whether it is invoked by ethtool cmd.
1322 * @mcam_idx: NPC mcam index corresponding to MAC
1323 * @pcifunc: PCI func.
1324 *
1325 * Creates a new exact match table entry in either CAM or
1326 * MEM table.
1327 * Return: 0 upon success.
1328 */
1329 static int rvu_npc_exact_add_table_entry(struct rvu *rvu, u8 cgx_id, u8 lmac_id, u8 *mac,
1330 u16 chan, u8 ctype, u32 *seq_id, bool cmd,
1331 u32 mcam_idx, u16 pcifunc)
1332 {
1333 int blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1334 enum npc_exact_opc_type opc_type;
1335 bool enable_cam = false;
1336 u32 drop_mcam_idx;
1337 u32 index;
1338 u64 mdata;
1339 bool rc;
1340 int err;
1341 u8 ways;
1342
1343 ctype = 0;
1344
1345 err = rvu_npc_exact_alloc_table_entry(rvu, mac, chan, ctype, &index, &ways, &opc_type);
1346 if (err) {
1347 dev_err(rvu->dev, "%s: Could not alloc in exact match table\n", __func__);
1348 return err;
1349 }
1350
1351 /* Write mdata to table */
1352 mdata = rvu_exact_prepare_table_entry(rvu, true, ctype, chan, mac);
1353
1354 if (opc_type == NPC_EXACT_OPC_CAM)
1355 rvu_npc_exact_cam_table_write(rvu, blkaddr, index, mdata);
1356 else
1357 rvu_npc_exact_mem_table_write(rvu, blkaddr, ways, index, mdata);
1358
1359 /* Insert entry to linked list */
1360 err = rvu_npc_exact_add_to_list(rvu, opc_type, ways, index, cgx_id, lmac_id,
1361 mac, chan, ctype, seq_id, cmd, mcam_idx, pcifunc);
1362 if (err) {
1363 rvu_npc_exact_dealloc_table_entry(rvu, opc_type, ways, index);
1364 dev_err(rvu->dev, "%s: could not add to exact match table\n", __func__);
1365 return err;
1366 }
1367
1368 rc = rvu_npc_exact_get_drop_rule_info(rvu, NIX_INTF_TYPE_CGX, cgx_id, lmac_id,
1369 &drop_mcam_idx, NULL, NULL, NULL);
1370 if (!rc) {
1371 rvu_npc_exact_dealloc_table_entry(rvu, opc_type, ways, index);
1372 dev_dbg(rvu->dev, "%s: failed to get drop rule info cgx=%d lmac=%d\n",
1373 __func__, cgx_id, lmac_id);
1374 return -EINVAL;
1375 }
1376
1377 if (cmd)
1378 __rvu_npc_exact_cmd_rules_cnt_update(rvu, drop_mcam_idx, 1, &enable_cam);
1379
1380 /* First command rule; enable drop on hit rule */
1381 if (enable_cam) {
1382 rvu_npc_enable_mcam_by_entry_index(rvu, drop_mcam_idx, NIX_INTF_RX, true);
1383 dev_dbg(rvu->dev, "%s: Enabling mcam idx %d\n",
1384 __func__, drop_mcam_idx);
1385 }
1386
1387 dev_dbg(rvu->dev,
1388 "%s: Successfully added entry (index=%d, dmac=%pM, ways=%d opc_type=%d\n",
1389 __func__, index, mac, ways, opc_type);
1390
1391 return 0;
1392 }
1393
1394 /**
1395 * rvu_npc_exact_update_table_entry - Update exact match table.
1396 * @rvu: resource virtualization unit.
1397 * @cgx_id: CGX identifier.
1398 * @lmac_id: LMAC identifier.
1399 * @old_mac: Existing MAC address entry.
1400 * @new_mac: New MAC address entry.
1401 * @seq_id: Sequence identifier of the entry.
1402 *
1403 * Updates MAC address of an entry. If entry is in MEM table, new
1404 * hash value may not match with old one.
1405 * Return: 0 upon success.
1406 */
1407 static int rvu_npc_exact_update_table_entry(struct rvu *rvu, u8 cgx_id, u8 lmac_id,
1408 u8 *old_mac, u8 *new_mac, u32 *seq_id)
1409 {
1410 int blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1411 struct npc_exact_table_entry *entry;
1412 struct npc_exact_table *table;
1413 u32 hash_index;
1414 u64 mdata;
1415
1416 table = rvu->hw->table;
1417
1418 mutex_lock(&table->lock);
1419
1420 /* Lookup for entry which needs to be updated */
1421 entry = __rvu_npc_exact_find_entry_by_seq_id(rvu, *seq_id);
1422 if (!entry) {
1423 mutex_unlock(&table->lock);
1424 dev_dbg(rvu->dev,
1425 "%s: failed to find entry for cgx_id=%d lmac_id=%d old_mac=%pM\n",
1426 __func__, cgx_id, lmac_id, old_mac);
1427 return -ENODATA;
1428 }
1429
1430 /* If entry is in mem table and new hash index is different than old
1431 * hash index, we cannot update the entry. Fail in these scenarios.
1432 */
1433 if (entry->opc_type == NPC_EXACT_OPC_MEM) {
1434 hash_index = rvu_exact_calculate_hash(rvu, entry->chan, entry->ctype,
1435 new_mac, table->mem_table.mask,
1436 table->mem_table.depth);
1437 if (hash_index != entry->index) {
1438 dev_dbg(rvu->dev,
1439 "%s: Update failed due to index mismatch(new=0x%x, old=%x)\n",
1440 __func__, hash_index, entry->index);
1441 mutex_unlock(&table->lock);
1442 return -EINVAL;
1443 }
1444 }
1445
1446 mdata = rvu_exact_prepare_table_entry(rvu, true, entry->ctype, entry->chan, new_mac);
1447
1448 if (entry->opc_type == NPC_EXACT_OPC_MEM)
1449 rvu_npc_exact_mem_table_write(rvu, blkaddr, entry->ways, entry->index, mdata);
1450 else
1451 rvu_npc_exact_cam_table_write(rvu, blkaddr, entry->index, mdata);
1452
1453 /* Update entry fields */
1454 ether_addr_copy(entry->mac, new_mac);
1455 *seq_id = entry->seq_id;
1456
1457 dev_dbg(rvu->dev,
1458 "%s: Successfully updated entry (index=%d, dmac=%pM, ways=%d opc_type=%d\n",
1459 __func__, entry->index, entry->mac, entry->ways, entry->opc_type);
1460
1461 dev_dbg(rvu->dev, "%s: Successfully updated entry (old mac=%pM new_mac=%pM\n",
1462 __func__, old_mac, new_mac);
1463
1464 mutex_unlock(&table->lock);
1465 return 0;
1466 }
1467
1468 /**
1469 * rvu_npc_exact_promisc_disable - Disable promiscuous mode.
1470 * @rvu: resource virtualization unit.
1471 * @pcifunc: pcifunc
1472 *
1473 * Drop rule is against each PF. We dont support DMAC filter for
1474 * VF.
1475 * Return: 0 upon success
1476 */
1477
1478 int rvu_npc_exact_promisc_disable(struct rvu *rvu, u16 pcifunc)
1479 {
1480 struct npc_exact_table *table;
1481 int pf = rvu_get_pf(rvu->pdev, pcifunc);
1482 u8 cgx_id, lmac_id;
1483 u32 drop_mcam_idx;
1484 bool *promisc;
1485 bool rc;
1486
1487 table = rvu->hw->table;
1488
1489 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1490 rc = rvu_npc_exact_get_drop_rule_info(rvu, NIX_INTF_TYPE_CGX, cgx_id, lmac_id,
1491 &drop_mcam_idx, NULL, NULL, NULL);
1492 if (!rc) {
1493 dev_dbg(rvu->dev, "%s: failed to get drop rule info cgx=%d lmac=%d\n",
1494 __func__, cgx_id, lmac_id);
1495 return -EINVAL;
1496 }
1497
1498 mutex_lock(&table->lock);
1499 promisc = &table->promisc_mode[drop_mcam_idx];
1500
1501 if (!*promisc) {
1502 mutex_unlock(&table->lock);
1503 dev_dbg(rvu->dev, "%s: Err Already promisc mode disabled (cgx=%d lmac=%d)\n",
1504 __func__, cgx_id, lmac_id);
1505 return LMAC_AF_ERR_INVALID_PARAM;
1506 }
1507 *promisc = false;
1508 mutex_unlock(&table->lock);
1509
1510 /* Enable drop rule */
1511 rvu_npc_enable_mcam_by_entry_index(rvu, drop_mcam_idx, NIX_INTF_RX,
1512 true);
1513
1514 dev_dbg(rvu->dev, "%s: disabled promisc mode (cgx=%d lmac=%d)\n",
1515 __func__, cgx_id, lmac_id);
1516 return 0;
1517 }
1518
1519 /**
1520 * rvu_npc_exact_promisc_enable - Enable promiscuous mode.
1521 * @rvu: resource virtualization unit.
1522 * @pcifunc: pcifunc.
1523 * Return: 0 upon success
1524 */
1525 int rvu_npc_exact_promisc_enable(struct rvu *rvu, u16 pcifunc)
1526 {
1527 struct npc_exact_table *table;
1528 int pf = rvu_get_pf(rvu->pdev, pcifunc);
1529 u8 cgx_id, lmac_id;
1530 u32 drop_mcam_idx;
1531 bool *promisc;
1532 bool rc;
1533
1534 table = rvu->hw->table;
1535
1536 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1537 rc = rvu_npc_exact_get_drop_rule_info(rvu, NIX_INTF_TYPE_CGX, cgx_id, lmac_id,
1538 &drop_mcam_idx, NULL, NULL, NULL);
1539 if (!rc) {
1540 dev_dbg(rvu->dev, "%s: failed to get drop rule info cgx=%d lmac=%d\n",
1541 __func__, cgx_id, lmac_id);
1542 return -EINVAL;
1543 }
1544
1545 mutex_lock(&table->lock);
1546 promisc = &table->promisc_mode[drop_mcam_idx];
1547
1548 if (*promisc) {
1549 mutex_unlock(&table->lock);
1550 dev_dbg(rvu->dev, "%s: Already in promisc mode (cgx=%d lmac=%d)\n",
1551 __func__, cgx_id, lmac_id);
1552 return LMAC_AF_ERR_INVALID_PARAM;
1553 }
1554 *promisc = true;
1555 mutex_unlock(&table->lock);
1556
1557 /* disable drop rule */
1558 rvu_npc_enable_mcam_by_entry_index(rvu, drop_mcam_idx, NIX_INTF_RX,
1559 false);
1560
1561 dev_dbg(rvu->dev, "%s: Enabled promisc mode (cgx=%d lmac=%d)\n",
1562 __func__, cgx_id, lmac_id);
1563 return 0;
1564 }
1565
1566 /**
1567 * rvu_npc_exact_mac_addr_reset - Delete PF mac address.
1568 * @rvu: resource virtualization unit.
1569 * @req: Reset request
1570 * @rsp: Reset response.
1571 * Return: 0 upon success
1572 */
1573 int rvu_npc_exact_mac_addr_reset(struct rvu *rvu, struct cgx_mac_addr_reset_req *req,
1574 struct msg_rsp *rsp)
1575 {
1576 int pf = rvu_get_pf(rvu->pdev, req->hdr.pcifunc);
1577 u32 seq_id = req->index;
1578 struct rvu_pfvf *pfvf;
1579 u8 cgx_id, lmac_id;
1580 int rc;
1581
1582 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1583
1584 pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
1585
1586 rc = rvu_npc_exact_del_table_entry_by_id(rvu, seq_id);
1587 if (rc) {
1588 /* TODO: how to handle this error case ? */
1589 dev_err(rvu->dev, "%s MAC (%pM) del PF=%d failed\n", __func__, pfvf->mac_addr, pf);
1590 return 0;
1591 }
1592
1593 dev_dbg(rvu->dev, "%s MAC (%pM) del PF=%d success (seq_id=%u)\n",
1594 __func__, pfvf->mac_addr, pf, seq_id);
1595 return 0;
1596 }
1597
1598 /**
1599 * rvu_npc_exact_mac_addr_update - Update mac address field with new value.
1600 * @rvu: resource virtualization unit.
1601 * @req: Update request.
1602 * @rsp: Update response.
1603 * Return: 0 upon success
1604 */
1605 int rvu_npc_exact_mac_addr_update(struct rvu *rvu,
1606 struct cgx_mac_addr_update_req *req,
1607 struct cgx_mac_addr_update_rsp *rsp)
1608 {
1609 int pf = rvu_get_pf(rvu->pdev, req->hdr.pcifunc);
1610 struct npc_exact_table_entry *entry;
1611 struct npc_exact_table *table;
1612 struct rvu_pfvf *pfvf;
1613 u32 seq_id, mcam_idx;
1614 u8 old_mac[ETH_ALEN];
1615 u8 cgx_id, lmac_id;
1616 int rc;
1617
1618 if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
1619 return LMAC_AF_ERR_PERM_DENIED;
1620
1621 dev_dbg(rvu->dev, "%s: Update request for seq_id=%d, mac=%pM\n",
1622 __func__, req->index, req->mac_addr);
1623
1624 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1625
1626 pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
1627
1628 table = rvu->hw->table;
1629
1630 mutex_lock(&table->lock);
1631
1632 /* Lookup for entry which needs to be updated */
1633 entry = __rvu_npc_exact_find_entry_by_seq_id(rvu, req->index);
1634 if (!entry) {
1635 dev_err(rvu->dev, "%s: failed to find entry for id=0x%x\n", __func__, req->index);
1636 mutex_unlock(&table->lock);
1637 return LMAC_AF_ERR_EXACT_MATCH_TBL_LOOK_UP_FAILED;
1638 }
1639 ether_addr_copy(old_mac, entry->mac);
1640 seq_id = entry->seq_id;
1641 mcam_idx = entry->mcam_idx;
1642 mutex_unlock(&table->lock);
1643
1644 rc = rvu_npc_exact_update_table_entry(rvu, cgx_id, lmac_id, old_mac,
1645 req->mac_addr, &seq_id);
1646 if (!rc) {
1647 rsp->index = seq_id;
1648 dev_dbg(rvu->dev, "%s mac:%pM (pfvf:%pM default:%pM) update to PF=%d success\n",
1649 __func__, req->mac_addr, pfvf->mac_addr, pfvf->default_mac, pf);
1650 ether_addr_copy(pfvf->mac_addr, req->mac_addr);
1651 return 0;
1652 }
1653
1654 /* Try deleting and adding it again */
1655 rc = rvu_npc_exact_del_table_entry_by_id(rvu, req->index);
1656 if (rc) {
1657 /* This could be a new entry */
1658 dev_dbg(rvu->dev, "%s MAC (%pM) del PF=%d failed\n", __func__,
1659 pfvf->mac_addr, pf);
1660 }
1661
1662 rc = rvu_npc_exact_add_table_entry(rvu, cgx_id, lmac_id, req->mac_addr,
1663 pfvf->rx_chan_base, 0, &seq_id, true,
1664 mcam_idx, req->hdr.pcifunc);
1665 if (rc) {
1666 dev_err(rvu->dev, "%s MAC (%pM) add PF=%d failed\n", __func__,
1667 req->mac_addr, pf);
1668 return LMAC_AF_ERR_EXACT_MATCH_TBL_ADD_FAILED;
1669 }
1670
1671 rsp->index = seq_id;
1672 dev_dbg(rvu->dev,
1673 "%s MAC (new:%pM, old=%pM default:%pM) del and add to PF=%d success (seq_id=%u)\n",
1674 __func__, req->mac_addr, pfvf->mac_addr, pfvf->default_mac, pf, seq_id);
1675
1676 ether_addr_copy(pfvf->mac_addr, req->mac_addr);
1677 return 0;
1678 }
1679
1680 /**
1681 * rvu_npc_exact_mac_addr_add - Adds MAC address to exact match table.
1682 * @rvu: resource virtualization unit.
1683 * @req: Add request.
1684 * @rsp: Add response.
1685 * Return: 0 upon success
1686 */
1687 int rvu_npc_exact_mac_addr_add(struct rvu *rvu,
1688 struct cgx_mac_addr_add_req *req,
1689 struct cgx_mac_addr_add_rsp *rsp)
1690 {
1691 int pf = rvu_get_pf(rvu->pdev, req->hdr.pcifunc);
1692 struct rvu_pfvf *pfvf;
1693 u8 cgx_id, lmac_id;
1694 int rc = 0;
1695 u32 seq_id;
1696
1697 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1698 pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
1699
1700 rc = rvu_npc_exact_add_table_entry(rvu, cgx_id, lmac_id, req->mac_addr,
1701 pfvf->rx_chan_base, 0, &seq_id,
1702 true, -1, req->hdr.pcifunc);
1703
1704 if (!rc) {
1705 rsp->index = seq_id;
1706 dev_dbg(rvu->dev, "%s MAC (%pM) add to PF=%d success (seq_id=%u)\n",
1707 __func__, req->mac_addr, pf, seq_id);
1708 return 0;
1709 }
1710
1711 dev_err(rvu->dev, "%s MAC (%pM) add to PF=%d failed\n", __func__,
1712 req->mac_addr, pf);
1713 return LMAC_AF_ERR_EXACT_MATCH_TBL_ADD_FAILED;
1714 }
1715
1716 /**
1717 * rvu_npc_exact_mac_addr_del - Delete DMAC filter
1718 * @rvu: resource virtualization unit.
1719 * @req: Delete request.
1720 * @rsp: Delete response.
1721 * Return: 0 upon success
1722 */
1723 int rvu_npc_exact_mac_addr_del(struct rvu *rvu,
1724 struct cgx_mac_addr_del_req *req,
1725 struct msg_rsp *rsp)
1726 {
1727 int pf = rvu_get_pf(rvu->pdev, req->hdr.pcifunc);
1728 int rc;
1729
1730 rc = rvu_npc_exact_del_table_entry_by_id(rvu, req->index);
1731 if (!rc) {
1732 dev_dbg(rvu->dev, "%s del to PF=%d success (seq_id=%u)\n",
1733 __func__, pf, req->index);
1734 return 0;
1735 }
1736
1737 dev_err(rvu->dev, "%s del to PF=%d failed (seq_id=%u)\n",
1738 __func__, pf, req->index);
1739 return LMAC_AF_ERR_EXACT_MATCH_TBL_DEL_FAILED;
1740 }
1741
1742 /**
1743 * rvu_npc_exact_mac_addr_set - Add PF mac address to dmac filter.
1744 * @rvu: resource virtualization unit.
1745 * @req: Set request.
1746 * @rsp: Set response.
1747 * Return: 0 upon success
1748 */
1749 int rvu_npc_exact_mac_addr_set(struct rvu *rvu, struct cgx_mac_addr_set_or_get *req,
1750 struct cgx_mac_addr_set_or_get *rsp)
1751 {
1752 int pf = rvu_get_pf(rvu->pdev, req->hdr.pcifunc);
1753 u32 seq_id = req->index;
1754 struct rvu_pfvf *pfvf;
1755 u8 cgx_id, lmac_id;
1756 u32 mcam_idx = -1;
1757 int rc, nixlf;
1758
1759 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1760
1761 pfvf = &rvu->pf[pf];
1762
1763 /* If table does not have an entry; both update entry and del table entry API
1764 * below fails. Those are not failure conditions.
1765 */
1766 rc = rvu_npc_exact_update_table_entry(rvu, cgx_id, lmac_id, pfvf->mac_addr,
1767 req->mac_addr, &seq_id);
1768 if (!rc) {
1769 rsp->index = seq_id;
1770 ether_addr_copy(pfvf->mac_addr, req->mac_addr);
1771 ether_addr_copy(rsp->mac_addr, req->mac_addr);
1772 dev_dbg(rvu->dev, "%s MAC (%pM) update to PF=%d success\n",
1773 __func__, req->mac_addr, pf);
1774 return 0;
1775 }
1776
1777 /* Try deleting and adding it again */
1778 rc = rvu_npc_exact_del_table_entry_by_id(rvu, req->index);
1779 if (rc) {
1780 dev_dbg(rvu->dev, "%s MAC (%pM) del PF=%d failed\n",
1781 __func__, pfvf->mac_addr, pf);
1782 }
1783
1784 /* find mcam entry if exist */
1785 rc = nix_get_nixlf(rvu, req->hdr.pcifunc, &nixlf, NULL);
1786 if (!rc) {
1787 mcam_idx = npc_get_nixlf_mcam_index(&rvu->hw->mcam, req->hdr.pcifunc,
1788 nixlf, NIXLF_UCAST_ENTRY);
1789 }
1790
1791 rc = rvu_npc_exact_add_table_entry(rvu, cgx_id, lmac_id, req->mac_addr,
1792 pfvf->rx_chan_base, 0, &seq_id,
1793 true, mcam_idx, req->hdr.pcifunc);
1794 if (rc) {
1795 dev_err(rvu->dev, "%s MAC (%pM) add PF=%d failed\n",
1796 __func__, req->mac_addr, pf);
1797 return LMAC_AF_ERR_EXACT_MATCH_TBL_ADD_FAILED;
1798 }
1799
1800 rsp->index = seq_id;
1801 ether_addr_copy(rsp->mac_addr, req->mac_addr);
1802 ether_addr_copy(pfvf->mac_addr, req->mac_addr);
1803 dev_dbg(rvu->dev,
1804 "%s MAC (%pM) del and add to PF=%d success (seq_id=%u)\n",
1805 __func__, req->mac_addr, pf, seq_id);
1806 return 0;
1807 }
1808
1809 /**
1810 * rvu_npc_exact_can_disable_feature - Check if feature can be disabled.
1811 * @rvu: resource virtualization unit.
1812 * Return: True if exact match feature is supported.
1813 */
1814 bool rvu_npc_exact_can_disable_feature(struct rvu *rvu)
1815 {
1816 struct npc_exact_table *table = rvu->hw->table;
1817 bool empty;
1818
1819 if (!rvu->hw->cap.npc_exact_match_enabled)
1820 return false;
1821
1822 mutex_lock(&table->lock);
1823 empty = list_empty(&table->lhead_gbl);
1824 mutex_unlock(&table->lock);
1825
1826 return empty;
1827 }
1828
1829 /**
1830 * rvu_npc_exact_disable_feature - Disable feature.
1831 * @rvu: resource virtualization unit.
1832 */
1833 void rvu_npc_exact_disable_feature(struct rvu *rvu)
1834 {
1835 rvu->hw->cap.npc_exact_match_enabled = false;
1836 }
1837
1838 /**
1839 * rvu_npc_exact_reset - Delete and free all entry which match pcifunc.
1840 * @rvu: resource virtualization unit.
1841 * @pcifunc: PCI func to match.
1842 */
1843 void rvu_npc_exact_reset(struct rvu *rvu, u16 pcifunc)
1844 {
1845 struct npc_exact_table *table = rvu->hw->table;
1846 struct npc_exact_table_entry *tmp, *iter;
1847 u32 seq_id;
1848
1849 mutex_lock(&table->lock);
1850 list_for_each_entry_safe(iter, tmp, &table->lhead_gbl, glist) {
1851 if (pcifunc != iter->pcifunc)
1852 continue;
1853
1854 seq_id = iter->seq_id;
1855 dev_dbg(rvu->dev, "%s: resetting pcifun=%d seq_id=%u\n", __func__,
1856 pcifunc, seq_id);
1857
1858 mutex_unlock(&table->lock);
1859 rvu_npc_exact_del_table_entry_by_id(rvu, seq_id);
1860 mutex_lock(&table->lock);
1861 }
1862 mutex_unlock(&table->lock);
1863 }
1864
1865 /**
1866 * rvu_npc_exact_init - initialize exact match table
1867 * @rvu: resource virtualization unit.
1868 *
1869 * Initialize HW and SW resources to manage 4way-2K table and fully
1870 * associative 32-entry mcam table.
1871 * Return: 0 upon success.
1872 */
1873 int rvu_npc_exact_init(struct rvu *rvu)
1874 {
1875 u64 bcast_mcast_val, bcast_mcast_mask;
1876 struct npc_exact_table *table;
1877 u64 exact_val, exact_mask;
1878 u64 chan_val, chan_mask;
1879 u8 cgx_id, lmac_id;
1880 u32 *drop_mcam_idx;
1881 u16 max_lmac_cnt;
1882 u64 npc_const3;
1883 int table_size;
1884 int blkaddr;
1885 u16 pcifunc;
1886 int err, i;
1887 u64 cfg;
1888 bool rc;
1889
1890 if (is_cn20k(rvu->pdev))
1891 return 0;
1892
1893 /* Read NPC_AF_CONST3 and check for have exact
1894 * match functionality is present
1895 */
1896 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1897 if (blkaddr < 0) {
1898 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
1899 return -EINVAL;
1900 }
1901
1902 /* Check exact match feature is supported */
1903 npc_const3 = rvu_read64(rvu, blkaddr, NPC_AF_CONST3);
1904 if (!(npc_const3 & BIT_ULL(62)))
1905 return 0;
1906
1907 /* Check if kex profile has enabled EXACT match nibble */
1908 cfg = rvu_read64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(NIX_INTF_RX));
1909 if (!(cfg & NPC_EXACT_NIBBLE_HIT))
1910 return 0;
1911
1912 /* Set capability to true */
1913 rvu->hw->cap.npc_exact_match_enabled = true;
1914
1915 table = kzalloc_obj(*table);
1916 if (!table)
1917 return -ENOMEM;
1918
1919 dev_dbg(rvu->dev, "%s: Memory allocation for table success\n", __func__);
1920 rvu->hw->table = table;
1921
1922 /* Read table size, ways and depth */
1923 table->mem_table.ways = FIELD_GET(GENMASK_ULL(19, 16), npc_const3);
1924 table->mem_table.depth = FIELD_GET(GENMASK_ULL(15, 0), npc_const3);
1925 table->cam_table.depth = FIELD_GET(GENMASK_ULL(31, 24), npc_const3);
1926
1927 dev_dbg(rvu->dev, "%s: NPC exact match 4way_2k table(ways=%d, depth=%d)\n",
1928 __func__, table->mem_table.ways, table->cam_table.depth);
1929
1930 /* Check if depth of table is not a sequre of 2
1931 * TODO: why _builtin_popcount() is not working ?
1932 */
1933 if ((table->mem_table.depth & (table->mem_table.depth - 1)) != 0) {
1934 dev_err(rvu->dev,
1935 "%s: NPC exact match 4way_2k table depth(%d) is not square of 2\n",
1936 __func__, table->mem_table.depth);
1937 return -EINVAL;
1938 }
1939
1940 table_size = table->mem_table.depth * table->mem_table.ways;
1941
1942 /* Allocate bitmap for 4way 2K table */
1943 table->mem_table.bmap = devm_bitmap_zalloc(rvu->dev, table_size,
1944 GFP_KERNEL);
1945 if (!table->mem_table.bmap)
1946 return -ENOMEM;
1947
1948 dev_dbg(rvu->dev, "%s: Allocated bitmap for 4way 2K entry table\n", __func__);
1949
1950 /* Allocate bitmap for 32 entry mcam */
1951 table->cam_table.bmap = devm_bitmap_zalloc(rvu->dev, 32, GFP_KERNEL);
1952
1953 if (!table->cam_table.bmap)
1954 return -ENOMEM;
1955
1956 dev_dbg(rvu->dev, "%s: Allocated bitmap for 32 entry cam\n", __func__);
1957
1958 table->tot_ids = table_size + table->cam_table.depth;
1959 table->id_bmap = devm_bitmap_zalloc(rvu->dev, table->tot_ids,
1960 GFP_KERNEL);
1961
1962 if (!table->id_bmap)
1963 return -ENOMEM;
1964
1965 dev_dbg(rvu->dev, "%s: Allocated bitmap for id map (total=%d)\n",
1966 __func__, table->tot_ids);
1967
1968 /* Initialize list heads for npc_exact_table entries.
1969 * This entry is used by debugfs to show entries in
1970 * exact match table.
1971 */
1972 for (i = 0; i < NPC_EXACT_TBL_MAX_WAYS; i++)
1973 INIT_LIST_HEAD(&table->lhead_mem_tbl_entry[i]);
1974
1975 INIT_LIST_HEAD(&table->lhead_cam_tbl_entry);
1976 INIT_LIST_HEAD(&table->lhead_gbl);
1977
1978 mutex_init(&table->lock);
1979
1980 rvu_exact_config_secret_key(rvu);
1981 rvu_exact_config_search_key(rvu);
1982
1983 rvu_exact_config_table_mask(rvu);
1984 rvu_exact_config_result_ctrl(rvu, table->mem_table.depth);
1985
1986 /* - No drop rule for LBK
1987 * - Drop rules for SDP and each LMAC.
1988 */
1989 exact_val = !NPC_EXACT_RESULT_HIT;
1990 exact_mask = NPC_EXACT_RESULT_HIT;
1991
1992 /* nibble - 3 2 1 0
1993 * L3B L3M L2B L2M
1994 */
1995 bcast_mcast_val = 0b0000;
1996 bcast_mcast_mask = 0b0011;
1997
1998 /* Install SDP drop rule */
1999 drop_mcam_idx = &table->num_drop_rules;
2000
2001 max_lmac_cnt = rvu->cgx_cnt_max * rvu->hw->lmac_per_cgx +
2002 PF_CGXMAP_BASE;
2003
2004 for (i = PF_CGXMAP_BASE; i < max_lmac_cnt; i++) {
2005 if (rvu->pf2cgxlmac_map[i] == 0xFF)
2006 continue;
2007
2008 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[i], &cgx_id, &lmac_id);
2009
2010 rc = rvu_npc_exact_calc_drop_rule_chan_and_mask(rvu, NIX_INTF_TYPE_CGX, cgx_id,
2011 lmac_id, &chan_val, &chan_mask);
2012 if (!rc) {
2013 dev_err(rvu->dev,
2014 "%s: failed, info chan_val=0x%llx chan_mask=0x%llx rule_id=%d\n",
2015 __func__, chan_val, chan_mask, *drop_mcam_idx);
2016 return -EINVAL;
2017 }
2018
2019 /* Filter rules are only for PF */
2020 pcifunc = RVU_PFFUNC(rvu->pdev, i, 0);
2021
2022 dev_dbg(rvu->dev,
2023 "%s:Drop rule cgx=%d lmac=%d chan(val=0x%llx, mask=0x%llx\n",
2024 __func__, cgx_id, lmac_id, chan_val, chan_mask);
2025
2026 rc = rvu_npc_exact_save_drop_rule_chan_and_mask(rvu, table->num_drop_rules,
2027 chan_val, chan_mask, pcifunc);
2028 if (!rc) {
2029 dev_err(rvu->dev,
2030 "%s: failed to set drop info for cgx=%d, lmac=%d, chan=%llx\n",
2031 __func__, cgx_id, lmac_id, chan_val);
2032 return -EINVAL;
2033 }
2034
2035 err = npc_install_mcam_drop_rule(rvu, *drop_mcam_idx,
2036 &table->counter_idx[*drop_mcam_idx],
2037 chan_val, chan_mask,
2038 exact_val, exact_mask,
2039 bcast_mcast_val, bcast_mcast_mask);
2040 if (err) {
2041 dev_err(rvu->dev,
2042 "failed to configure drop rule (cgx=%d lmac=%d)\n",
2043 cgx_id, lmac_id);
2044 return err;
2045 }
2046
2047 (*drop_mcam_idx)++;
2048 }
2049
2050 dev_info(rvu->dev, "initialized exact match table successfully\n");
2051 return 0;
2052 }