1 // SPDX-License-Identifier: GPL-2.0 2 /* Marvell RVU Admin Function driver 3 * 4 * Copyright (C) 2020 Marvell. 5 */ 6 7 #include <linux/bitfield.h> 8 9 #include "rvu_struct.h" 10 #include "rvu_reg.h" 11 #include "rvu.h" 12 #include "npc.h" 13 #include "rvu_npc_fs.h" 14 #include "rvu_npc_hash.h" 15 #include "cn20k/reg.h" 16 #include "cn20k/npc.h" 17 18 static const char * const npc_flow_names[] = { 19 [NPC_DMAC] = "dmac", 20 [NPC_SMAC] = "smac", 21 [NPC_ETYPE] = "ether type", 22 [NPC_VLAN_ETYPE_CTAG] = "vlan ether type ctag", 23 [NPC_VLAN_ETYPE_STAG] = "vlan ether type stag", 24 [NPC_OUTER_VID] = "outer vlan id", 25 [NPC_INNER_VID] = "inner vlan id", 26 [NPC_TOS] = "tos", 27 [NPC_IPFRAG_IPV4] = "fragmented IPv4 header ", 28 [NPC_SIP_IPV4] = "ipv4 source ip", 29 [NPC_DIP_IPV4] = "ipv4 destination ip", 30 [NPC_IPFRAG_IPV6] = "fragmented IPv6 header ", 31 [NPC_SIP_IPV6] = "ipv6 source ip", 32 [NPC_DIP_IPV6] = "ipv6 destination ip", 33 [NPC_IPPROTO_TCP] = "ip proto tcp", 34 [NPC_IPPROTO_UDP] = "ip proto udp", 35 [NPC_IPPROTO_SCTP] = "ip proto sctp", 36 [NPC_IPPROTO_ICMP] = "ip proto icmp", 37 [NPC_IPPROTO_ICMP6] = "ip proto icmp6", 38 [NPC_IPPROTO_AH] = "ip proto AH", 39 [NPC_IPPROTO_ESP] = "ip proto ESP", 40 [NPC_SPORT_TCP] = "tcp source port", 41 [NPC_DPORT_TCP] = "tcp destination port", 42 [NPC_SPORT_UDP] = "udp source port", 43 [NPC_DPORT_UDP] = "udp destination port", 44 [NPC_SPORT_SCTP] = "sctp source port", 45 [NPC_DPORT_SCTP] = "sctp destination port", 46 [NPC_LXMB] = "Mcast/Bcast header ", 47 [NPC_IPSEC_SPI] = "SPI ", 48 [NPC_MPLS1_LBTCBOS] = "lse depth 1 label tc bos", 49 [NPC_MPLS1_TTL] = "lse depth 1 ttl", 50 [NPC_MPLS2_LBTCBOS] = "lse depth 2 label tc bos", 51 [NPC_MPLS2_TTL] = "lse depth 2 ttl", 52 [NPC_MPLS3_LBTCBOS] = "lse depth 3 label tc bos", 53 [NPC_MPLS3_TTL] = "lse depth 3 ttl", 54 [NPC_MPLS4_LBTCBOS] = "lse depth 4 label tc bos", 55 [NPC_MPLS4_TTL] = "lse depth 4", 56 [NPC_TYPE_ICMP] = "icmp type", 57 [NPC_CODE_ICMP] = "icmp code", 58 [NPC_TCP_FLAGS] = "tcp flags", 59 [NPC_UNKNOWN] = "unknown", 60 }; 61 62 bool npc_is_feature_supported(struct rvu *rvu, u64 features, u8 intf) 63 { 64 struct npc_mcam *mcam = &rvu->hw->mcam; 65 u64 mcam_features; 66 u64 unsupported; 67 68 mcam_features = is_npc_intf_tx(intf) ? mcam->tx_features : mcam->rx_features; 69 unsupported = (mcam_features ^ features) & ~mcam_features; 70 71 /* Return false if at least one of the input flows is not extracted */ 72 return !unsupported; 73 } 74 75 const char *npc_get_field_name(u8 hdr) 76 { 77 if (hdr >= ARRAY_SIZE(npc_flow_names)) 78 return npc_flow_names[NPC_UNKNOWN]; 79 80 return npc_flow_names[hdr]; 81 } 82 83 /* Compute keyword masks and figure out the number of keywords a field 84 * spans in the key. 85 */ 86 static void npc_set_kw_masks(struct rvu *rvu, struct npc_mcam *mcam, u8 type, 87 u8 nr_bits, int start_kwi, int offset, u8 intf) 88 { 89 struct npc_key_field *field = &mcam->rx_key_fields[type]; 90 u8 bits_in_kw; 91 int max_kwi; 92 93 if (is_cn20k(rvu->pdev)) { 94 if (mcam->banks_per_entry == 1) 95 max_kwi = 3; /* NPC_MCAM_KEY_X2 */ 96 else 97 max_kwi = 7; /* NPC_MCAM_KEY_X4 */ 98 } else { 99 if (mcam->banks_per_entry == 1) 100 max_kwi = 1; /* NPC_MCAM_KEY_X1 */ 101 else if (mcam->banks_per_entry == 2) 102 max_kwi = 3; /* NPC_MCAM_KEY_X2 */ 103 else 104 max_kwi = 6; /* NPC_MCAM_KEY_X4 */ 105 } 106 107 if (is_npc_intf_tx(intf)) 108 field = &mcam->tx_key_fields[type]; 109 110 if (offset + nr_bits <= 64) { 111 /* one KW only */ 112 if (start_kwi > max_kwi) 113 return; 114 field->kw_mask[start_kwi] |= GENMASK_ULL(nr_bits - 1, 0) 115 << offset; 116 field->nr_kws = 1; 117 } else if (offset + nr_bits > 64 && 118 offset + nr_bits <= 128) { 119 /* two KWs */ 120 if (start_kwi + 1 > max_kwi) 121 return; 122 /* first KW mask */ 123 bits_in_kw = 64 - offset; 124 field->kw_mask[start_kwi] |= GENMASK_ULL(bits_in_kw - 1, 0) 125 << offset; 126 /* second KW mask i.e. mask for rest of bits */ 127 bits_in_kw = nr_bits + offset - 64; 128 field->kw_mask[start_kwi + 1] |= GENMASK_ULL(bits_in_kw - 1, 0); 129 field->nr_kws = 2; 130 } else { 131 /* three KWs */ 132 if (start_kwi + 2 > max_kwi) 133 return; 134 /* first KW mask */ 135 bits_in_kw = 64 - offset; 136 field->kw_mask[start_kwi] |= GENMASK_ULL(bits_in_kw - 1, 0) 137 << offset; 138 /* second KW mask */ 139 field->kw_mask[start_kwi + 1] = ~0ULL; 140 /* third KW mask i.e. mask for rest of bits */ 141 bits_in_kw = nr_bits + offset - 128; 142 field->kw_mask[start_kwi + 2] |= GENMASK_ULL(bits_in_kw - 1, 0); 143 field->nr_kws = 3; 144 } 145 } 146 147 /* Helper function to figure out whether field exists in the key */ 148 static bool npc_is_field_present(struct rvu *rvu, enum key_fields type, u8 intf) 149 { 150 struct npc_mcam *mcam = &rvu->hw->mcam; 151 struct npc_key_field *input; 152 153 input = &mcam->rx_key_fields[type]; 154 if (is_npc_intf_tx(intf)) 155 input = &mcam->tx_key_fields[type]; 156 157 return input->nr_kws > 0; 158 } 159 160 static bool npc_is_same(struct npc_key_field *input, 161 struct npc_key_field *field) 162 { 163 return memcmp(&input->layer_mdata, &field->layer_mdata, 164 sizeof(struct npc_layer_mdata)) == 0; 165 } 166 167 static void npc_set_layer_mdata(struct rvu *rvu, 168 struct npc_mcam *mcam, enum key_fields type, 169 u64 cfg, u8 lid, u8 lt, u8 intf) 170 { 171 struct npc_key_field *input = &mcam->rx_key_fields[type]; 172 173 if (is_npc_intf_tx(intf)) 174 input = &mcam->tx_key_fields[type]; 175 176 input->layer_mdata.hdr = FIELD_GET(NPC_HDR_OFFSET, cfg); 177 input->layer_mdata.key = FIELD_GET(NPC_KEY_OFFSET, cfg); 178 if (is_cn20k(rvu->pdev)) 179 input->layer_mdata.len = FIELD_GET(NPC_CN20K_BYTESM, cfg) + 1; 180 else 181 input->layer_mdata.len = FIELD_GET(NPC_BYTESM, cfg) + 1; 182 input->layer_mdata.ltype = lt; 183 input->layer_mdata.lid = lid; 184 } 185 186 static bool npc_check_overlap_fields(struct npc_key_field *input1, 187 struct npc_key_field *input2, 188 int max_kw) 189 { 190 int kwi; 191 192 /* Fields with same layer id and different ltypes are mutually 193 * exclusive hence they can be overlapped 194 */ 195 if (input1->layer_mdata.lid == input2->layer_mdata.lid && 196 input1->layer_mdata.ltype != input2->layer_mdata.ltype) 197 return false; 198 199 for (kwi = 0; kwi < max_kw; kwi++) { 200 if (input1->kw_mask[kwi] & input2->kw_mask[kwi]) 201 return true; 202 } 203 204 return false; 205 } 206 207 /* Helper function to check whether given field overlaps with any other fields 208 * in the key. Due to limitations on key size and the key extraction profile in 209 * use higher layers can overwrite lower layer's header fields. Hence overlap 210 * needs to be checked. 211 */ 212 static bool npc_check_overlap(struct rvu *rvu, int blkaddr, 213 enum key_fields type, u8 start_lid, u8 intf) 214 { 215 struct npc_mcam *mcam = &rvu->hw->mcam; 216 struct npc_key_field *dummy, *input; 217 int start_kwi, offset; 218 u8 nr_bits, lid, lt, ld; 219 int extr, kws; 220 u64 cfg; 221 222 dummy = &mcam->rx_key_fields[NPC_UNKNOWN]; 223 input = &mcam->rx_key_fields[type]; 224 225 if (is_npc_intf_tx(intf)) { 226 dummy = &mcam->tx_key_fields[NPC_UNKNOWN]; 227 input = &mcam->tx_key_fields[type]; 228 } 229 230 kws = NPC_KWS_IN_KEY_SZ_7; 231 232 if (is_cn20k(rvu->pdev)) 233 goto skip_cn10k_config; 234 235 for (lid = start_lid; lid < NPC_MAX_LID; lid++) { 236 for (lt = 0; lt < NPC_MAX_LT; lt++) { 237 for (ld = 0; ld < NPC_MAX_LD; ld++) { 238 cfg = rvu_read64(rvu, blkaddr, 239 NPC_AF_INTFX_LIDX_LTX_LDX_CFG 240 (intf, lid, lt, ld)); 241 if (!FIELD_GET(NPC_LDATA_EN, cfg)) 242 continue; 243 memset(dummy, 0, sizeof(struct npc_key_field)); 244 npc_set_layer_mdata(rvu, mcam, NPC_UNKNOWN, 245 cfg, lid, lt, intf); 246 /* exclude input */ 247 if (npc_is_same(input, dummy)) 248 continue; 249 start_kwi = dummy->layer_mdata.key / 8; 250 offset = (dummy->layer_mdata.key * 8) % 64; 251 nr_bits = dummy->layer_mdata.len * 8; 252 /* form KW masks */ 253 npc_set_kw_masks(rvu, mcam, NPC_UNKNOWN, 254 nr_bits, start_kwi, 255 offset, intf); 256 /* check any input field bits falls in any 257 * other field bits. 258 */ 259 if (npc_check_overlap_fields(dummy, input, kws)) 260 return true; 261 } 262 } 263 } 264 return false; 265 266 skip_cn10k_config: 267 for (extr = 0 ; extr < rvu->hw->npc_kex_extr; extr++) { 268 lid = CN20K_GET_EXTR_LID(intf, extr); 269 if (lid < start_lid) 270 continue; 271 for (lt = 0; lt < NPC_MAX_LT; lt++) { 272 cfg = CN20K_GET_EXTR_LT(intf, extr, lt); 273 if (!FIELD_GET(NPC_LDATA_EN, cfg)) 274 continue; 275 276 memset(dummy, 0, sizeof(struct npc_key_field)); 277 npc_set_layer_mdata(rvu, mcam, NPC_UNKNOWN, cfg, 278 lid, lt, intf); 279 /* exclude input */ 280 if (npc_is_same(input, dummy)) 281 continue; 282 start_kwi = dummy->layer_mdata.key / 8; 283 offset = (dummy->layer_mdata.key * 8) % 64; 284 nr_bits = dummy->layer_mdata.len * 8; 285 /* form KW masks */ 286 npc_set_kw_masks(rvu, mcam, NPC_UNKNOWN, nr_bits, 287 start_kwi, offset, intf); 288 /* check any input field bits falls in any other 289 * field bits 290 */ 291 if (npc_check_overlap_fields(dummy, input, 292 NPC_KWS_IN_KEY_SZ_8)) 293 return true; 294 } 295 } 296 297 return false; 298 } 299 300 static bool npc_check_field(struct rvu *rvu, int blkaddr, enum key_fields type, 301 u8 intf) 302 { 303 if (!npc_is_field_present(rvu, type, intf) || 304 npc_check_overlap(rvu, blkaddr, type, 0, intf)) 305 return false; 306 return true; 307 } 308 309 static void npc_scan_exact_result(struct rvu *rvu, 310 struct npc_mcam *mcam, u8 bit_number, 311 u8 key_nibble, u8 intf) 312 { 313 u8 offset = (key_nibble * 4) % 64; /* offset within key word */ 314 u8 kwi = (key_nibble * 4) / 64; /* which word in key */ 315 u8 nr_bits = 4; /* bits in a nibble */ 316 u8 type; 317 318 switch (bit_number) { 319 case 40 ... 43: 320 type = NPC_EXACT_RESULT; 321 break; 322 323 default: 324 return; 325 } 326 npc_set_kw_masks(rvu, mcam, type, nr_bits, kwi, offset, intf); 327 } 328 329 static void npc_cn20k_scan_parse_result(struct rvu *rvu, struct npc_mcam *mcam, 330 u8 bit_number, u8 key_nibble, u8 intf) 331 { 332 u8 offset = (key_nibble * 4) % 64; /* offset within key word */ 333 u8 kwi = (key_nibble * 4) / 64; /* which word in key */ 334 u8 nr_bits = 4; /* bits in a nibble */ 335 u8 type; 336 337 switch (bit_number) { 338 case 0 ... 2: 339 type = NPC_CHAN; 340 break; 341 case 3: 342 type = NPC_ERRLEV; 343 break; 344 case 4 ... 5: 345 type = NPC_ERRCODE; 346 break; 347 case 6: 348 type = NPC_LXMB; 349 break; 350 case 8: 351 type = NPC_LA; 352 break; 353 case 10: 354 type = NPC_LB; 355 break; 356 case 12: 357 type = NPC_LC; 358 break; 359 case 14: 360 type = NPC_LD; 361 break; 362 case 16: 363 type = NPC_LE; 364 break; 365 case 18: 366 type = NPC_LF; 367 break; 368 case 20: 369 type = NPC_LG; 370 break; 371 case 22: 372 type = NPC_LH; 373 break; 374 default: 375 return; 376 } 377 378 npc_set_kw_masks(rvu, mcam, type, nr_bits, kwi, offset, intf); 379 } 380 381 static void npc_scan_parse_result(struct rvu *rvu, 382 struct npc_mcam *mcam, u8 bit_number, 383 u8 key_nibble, u8 intf) 384 { 385 u8 offset = (key_nibble * 4) % 64; /* offset within key word */ 386 u8 kwi = (key_nibble * 4) / 64; /* which word in key */ 387 u8 nr_bits = 4; /* bits in a nibble */ 388 u8 type; 389 390 if (is_cn20k(rvu->pdev)) { 391 npc_cn20k_scan_parse_result(rvu, mcam, bit_number, 392 key_nibble, intf); 393 return; 394 } 395 396 switch (bit_number) { 397 case 0 ... 2: 398 type = NPC_CHAN; 399 break; 400 case 3: 401 type = NPC_ERRLEV; 402 break; 403 case 4 ... 5: 404 type = NPC_ERRCODE; 405 break; 406 case 6: 407 type = NPC_LXMB; 408 break; 409 /* check for LTYPE only as of now */ 410 case 9: 411 type = NPC_LA; 412 break; 413 case 12: 414 type = NPC_LB; 415 break; 416 case 15: 417 type = NPC_LC; 418 break; 419 case 18: 420 type = NPC_LD; 421 break; 422 case 21: 423 type = NPC_LE; 424 break; 425 case 24: 426 type = NPC_LF; 427 break; 428 case 27: 429 type = NPC_LG; 430 break; 431 case 30: 432 type = NPC_LH; 433 break; 434 default: 435 return; 436 } 437 438 npc_set_kw_masks(rvu, mcam, type, nr_bits, kwi, offset, intf); 439 } 440 441 static void npc_handle_multi_layer_fields(struct rvu *rvu, int blkaddr, u8 intf) 442 { 443 struct npc_mcam *mcam = &rvu->hw->mcam; 444 struct npc_key_field *key_fields; 445 /* Ether type can come from three layers 446 * (ethernet, single tagged, double tagged) 447 */ 448 struct npc_key_field *etype_ether; 449 struct npc_key_field *etype_tag1; 450 struct npc_key_field *etype_tag2; 451 /* Outer VLAN TCI can come from two layers 452 * (single tagged, double tagged) 453 */ 454 struct npc_key_field *vlan_tag1; 455 struct npc_key_field *vlan_tag2; 456 /* Inner VLAN TCI for double tagged frames */ 457 struct npc_key_field *vlan_tag3; 458 u64 *features; 459 int i, max_kw; 460 u8 start_lid; 461 462 if (is_cn20k(rvu->pdev)) 463 max_kw = NPC_KWS_IN_KEY_SZ_8; 464 else 465 max_kw = NPC_KWS_IN_KEY_SZ_7; 466 467 key_fields = mcam->rx_key_fields; 468 features = &mcam->rx_features; 469 470 if (is_npc_intf_tx(intf)) { 471 key_fields = mcam->tx_key_fields; 472 features = &mcam->tx_features; 473 } 474 475 /* Handle header fields which can come from multiple layers like 476 * etype, outer vlan tci. These fields should have same position in 477 * the key otherwise to install a mcam rule more than one entry is 478 * needed which complicates mcam space management. 479 */ 480 etype_ether = &key_fields[NPC_ETYPE_ETHER]; 481 etype_tag1 = &key_fields[NPC_ETYPE_TAG1]; 482 etype_tag2 = &key_fields[NPC_ETYPE_TAG2]; 483 vlan_tag1 = &key_fields[NPC_VLAN_TAG1]; 484 vlan_tag2 = &key_fields[NPC_VLAN_TAG2]; 485 vlan_tag3 = &key_fields[NPC_VLAN_TAG3]; 486 487 /* if key profile programmed does not extract Ethertype at all */ 488 if (!etype_ether->nr_kws && !etype_tag1->nr_kws && !etype_tag2->nr_kws) { 489 dev_err(rvu->dev, "mkex: Ethertype is not extracted.\n"); 490 goto vlan_tci; 491 } 492 493 /* if key profile programmed extracts Ethertype from one layer */ 494 if (etype_ether->nr_kws && !etype_tag1->nr_kws && !etype_tag2->nr_kws) 495 key_fields[NPC_ETYPE] = *etype_ether; 496 if (!etype_ether->nr_kws && etype_tag1->nr_kws && !etype_tag2->nr_kws) 497 key_fields[NPC_ETYPE] = *etype_tag1; 498 if (!etype_ether->nr_kws && !etype_tag1->nr_kws && etype_tag2->nr_kws) 499 key_fields[NPC_ETYPE] = *etype_tag2; 500 501 /* if key profile programmed extracts Ethertype from multiple layers */ 502 if (etype_ether->nr_kws && etype_tag1->nr_kws) { 503 for (i = 0; i < max_kw; i++) { 504 if (etype_ether->kw_mask[i] != etype_tag1->kw_mask[i]) { 505 dev_err(rvu->dev, "mkex: Etype pos is different for untagged and tagged pkts.\n"); 506 goto vlan_tci; 507 } 508 } 509 key_fields[NPC_ETYPE] = *etype_tag1; 510 } 511 if (etype_ether->nr_kws && etype_tag2->nr_kws) { 512 for (i = 0; i < max_kw; i++) { 513 if (etype_ether->kw_mask[i] != etype_tag2->kw_mask[i]) { 514 dev_err(rvu->dev, "mkex: Etype pos is different for untagged and double tagged pkts.\n"); 515 goto vlan_tci; 516 } 517 } 518 key_fields[NPC_ETYPE] = *etype_tag2; 519 } 520 if (etype_tag1->nr_kws && etype_tag2->nr_kws) { 521 for (i = 0; i < max_kw; i++) { 522 if (etype_tag1->kw_mask[i] != etype_tag2->kw_mask[i]) { 523 dev_err(rvu->dev, "mkex: Etype pos is different for tagged and double tagged pkts.\n"); 524 goto vlan_tci; 525 } 526 } 527 key_fields[NPC_ETYPE] = *etype_tag2; 528 } 529 530 /* check none of higher layers overwrite Ethertype */ 531 start_lid = key_fields[NPC_ETYPE].layer_mdata.lid + 1; 532 if (npc_check_overlap(rvu, blkaddr, NPC_ETYPE, start_lid, intf)) { 533 dev_err(rvu->dev, "mkex: Ethertype is overwritten by higher layers.\n"); 534 goto vlan_tci; 535 } 536 *features |= BIT_ULL(NPC_ETYPE); 537 vlan_tci: 538 /* if key profile does not extract outer vlan tci at all */ 539 if (!vlan_tag1->nr_kws && !vlan_tag2->nr_kws) { 540 dev_err(rvu->dev, "mkex: Outer vlan tci is not extracted.\n"); 541 goto done; 542 } 543 544 /* if key profile extracts outer vlan tci from one layer */ 545 if (vlan_tag1->nr_kws && !vlan_tag2->nr_kws) 546 key_fields[NPC_OUTER_VID] = *vlan_tag1; 547 if (!vlan_tag1->nr_kws && vlan_tag2->nr_kws) 548 key_fields[NPC_OUTER_VID] = *vlan_tag2; 549 550 /* if key profile extracts outer vlan tci from multiple layers */ 551 if (vlan_tag1->nr_kws && vlan_tag2->nr_kws) { 552 for (i = 0; i < max_kw; i++) { 553 if (vlan_tag1->kw_mask[i] != vlan_tag2->kw_mask[i]) { 554 dev_err(rvu->dev, "mkex: Out vlan tci pos is different for tagged and double tagged pkts.\n"); 555 goto done; 556 } 557 } 558 key_fields[NPC_OUTER_VID] = *vlan_tag2; 559 } 560 /* check none of higher layers overwrite outer vlan tci */ 561 start_lid = key_fields[NPC_OUTER_VID].layer_mdata.lid + 1; 562 if (npc_check_overlap(rvu, blkaddr, NPC_OUTER_VID, start_lid, intf)) { 563 dev_err(rvu->dev, "mkex: Outer vlan tci is overwritten by higher layers.\n"); 564 goto done; 565 } 566 *features |= BIT_ULL(NPC_OUTER_VID); 567 568 /* If key profile extracts inner vlan tci */ 569 if (vlan_tag3->nr_kws) { 570 key_fields[NPC_INNER_VID] = *vlan_tag3; 571 *features |= BIT_ULL(NPC_INNER_VID); 572 } 573 done: 574 return; 575 } 576 577 static void npc_scan_ldata(struct rvu *rvu, int blkaddr, u8 lid, 578 u8 lt, u64 cfg, u8 intf) 579 { 580 struct npc_mcam_kex_hash *mkex_hash = rvu->kpu.mkex_hash; 581 struct npc_mcam *mcam = &rvu->hw->mcam; 582 u8 hdr, key, nr_bytes, bit_offset; 583 u8 la_ltype, la_start; 584 /* starting KW index and starting bit position */ 585 int start_kwi, offset; 586 587 if (is_cn20k(rvu->pdev)) 588 nr_bytes = FIELD_GET(NPC_CN20K_BYTESM, cfg) + 1; 589 else 590 nr_bytes = FIELD_GET(NPC_BYTESM, cfg) + 1; 591 592 hdr = FIELD_GET(NPC_HDR_OFFSET, cfg); 593 key = FIELD_GET(NPC_KEY_OFFSET, cfg); 594 595 /* For Tx, Layer A has NIX_INST_HDR_S(64 bytes) preceding 596 * ethernet header. 597 */ 598 if (is_npc_intf_tx(intf)) { 599 la_ltype = NPC_LT_LA_IH_NIX_ETHER; 600 la_start = 8; 601 } else { 602 la_ltype = NPC_LT_LA_ETHER; 603 la_start = 0; 604 } 605 606 #define NPC_SCAN_HDR(name, hlid, hlt, hstart, hlen) \ 607 do { \ 608 start_kwi = key / 8; \ 609 offset = (key * 8) % 64; \ 610 if (lid == (hlid) && lt == (hlt)) { \ 611 if ((hstart) >= hdr && \ 612 ((hstart) + (hlen)) <= (hdr + nr_bytes)) { \ 613 bit_offset = (hdr + nr_bytes - (hstart) - (hlen)) * 8; \ 614 npc_set_layer_mdata(rvu, mcam, (name), cfg, lid, lt, \ 615 intf); \ 616 offset += bit_offset; \ 617 start_kwi += offset / 64; \ 618 offset %= 64; \ 619 npc_set_kw_masks(rvu, mcam, (name), (hlen) * 8, \ 620 start_kwi, offset, intf); \ 621 } \ 622 } \ 623 } while (0) 624 625 /* List LID, LTYPE, start offset from layer and length(in bytes) of 626 * packet header fields below. 627 * Example: Source IP is 4 bytes and starts at 12th byte of IP header 628 */ 629 NPC_SCAN_HDR(NPC_TOS, NPC_LID_LC, NPC_LT_LC_IP, 1, 1); 630 NPC_SCAN_HDR(NPC_IPFRAG_IPV4, NPC_LID_LC, NPC_LT_LC_IP, 6, 1); 631 NPC_SCAN_HDR(NPC_SIP_IPV4, NPC_LID_LC, NPC_LT_LC_IP, 12, 4); 632 NPC_SCAN_HDR(NPC_DIP_IPV4, NPC_LID_LC, NPC_LT_LC_IP, 16, 4); 633 NPC_SCAN_HDR(NPC_IPFRAG_IPV6, NPC_LID_LC, NPC_LT_LC_IP6_EXT, 6, 1); 634 if (rvu->hw->cap.npc_hash_extract) { 635 if (mkex_hash->lid_lt_ld_hash_en[intf][lid][lt][0]) 636 NPC_SCAN_HDR(NPC_SIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 8, 4); 637 else 638 NPC_SCAN_HDR(NPC_SIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 8, 16); 639 640 if (mkex_hash->lid_lt_ld_hash_en[intf][lid][lt][1]) 641 NPC_SCAN_HDR(NPC_DIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 24, 4); 642 else 643 NPC_SCAN_HDR(NPC_DIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 24, 16); 644 } else { 645 NPC_SCAN_HDR(NPC_SIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 8, 16); 646 NPC_SCAN_HDR(NPC_DIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 24, 16); 647 } 648 649 NPC_SCAN_HDR(NPC_SPORT_UDP, NPC_LID_LD, NPC_LT_LD_UDP, 0, 2); 650 NPC_SCAN_HDR(NPC_DPORT_UDP, NPC_LID_LD, NPC_LT_LD_UDP, 2, 2); 651 NPC_SCAN_HDR(NPC_SPORT_TCP, NPC_LID_LD, NPC_LT_LD_TCP, 0, 2); 652 NPC_SCAN_HDR(NPC_DPORT_TCP, NPC_LID_LD, NPC_LT_LD_TCP, 2, 2); 653 NPC_SCAN_HDR(NPC_SPORT_SCTP, NPC_LID_LD, NPC_LT_LD_SCTP, 0, 2); 654 NPC_SCAN_HDR(NPC_DPORT_SCTP, NPC_LID_LD, NPC_LT_LD_SCTP, 2, 2); 655 NPC_SCAN_HDR(NPC_TYPE_ICMP, NPC_LID_LD, NPC_LT_LD_ICMP, 0, 1); 656 NPC_SCAN_HDR(NPC_CODE_ICMP, NPC_LID_LD, NPC_LT_LD_ICMP, 1, 1); 657 NPC_SCAN_HDR(NPC_TCP_FLAGS, NPC_LID_LD, NPC_LT_LD_TCP, 12, 2); 658 NPC_SCAN_HDR(NPC_ETYPE_ETHER, NPC_LID_LA, NPC_LT_LA_ETHER, 12, 2); 659 NPC_SCAN_HDR(NPC_ETYPE_TAG1, NPC_LID_LB, NPC_LT_LB_CTAG, 4, 2); 660 NPC_SCAN_HDR(NPC_ETYPE_TAG2, NPC_LID_LB, NPC_LT_LB_STAG_QINQ, 8, 2); 661 NPC_SCAN_HDR(NPC_VLAN_TAG1, NPC_LID_LB, NPC_LT_LB_CTAG, 2, 2); 662 NPC_SCAN_HDR(NPC_VLAN_TAG2, NPC_LID_LB, NPC_LT_LB_STAG_QINQ, 2, 2); 663 NPC_SCAN_HDR(NPC_VLAN_TAG3, NPC_LID_LB, NPC_LT_LB_STAG_QINQ, 6, 2); 664 NPC_SCAN_HDR(NPC_DMAC, NPC_LID_LA, la_ltype, la_start, 6); 665 666 NPC_SCAN_HDR(NPC_IPSEC_SPI, NPC_LID_LD, NPC_LT_LD_AH, 4, 4); 667 NPC_SCAN_HDR(NPC_IPSEC_SPI, NPC_LID_LE, NPC_LT_LE_ESP, 0, 4); 668 NPC_SCAN_HDR(NPC_MPLS1_LBTCBOS, NPC_LID_LC, NPC_LT_LC_MPLS, 0, 3); 669 NPC_SCAN_HDR(NPC_MPLS1_TTL, NPC_LID_LC, NPC_LT_LC_MPLS, 3, 1); 670 NPC_SCAN_HDR(NPC_MPLS2_LBTCBOS, NPC_LID_LC, NPC_LT_LC_MPLS, 4, 3); 671 NPC_SCAN_HDR(NPC_MPLS2_TTL, NPC_LID_LC, NPC_LT_LC_MPLS, 7, 1); 672 NPC_SCAN_HDR(NPC_MPLS3_LBTCBOS, NPC_LID_LC, NPC_LT_LC_MPLS, 8, 3); 673 NPC_SCAN_HDR(NPC_MPLS3_TTL, NPC_LID_LC, NPC_LT_LC_MPLS, 11, 1); 674 NPC_SCAN_HDR(NPC_MPLS4_LBTCBOS, NPC_LID_LC, NPC_LT_LC_MPLS, 12, 3); 675 NPC_SCAN_HDR(NPC_MPLS4_TTL, NPC_LID_LC, NPC_LT_LC_MPLS, 15, 1); 676 677 /* SMAC follows the DMAC(which is 6 bytes) */ 678 NPC_SCAN_HDR(NPC_SMAC, NPC_LID_LA, la_ltype, la_start + 6, 6); 679 /* PF_FUNC is 2 bytes at 0th byte of NPC_LT_LA_IH_NIX_ETHER */ 680 NPC_SCAN_HDR(NPC_PF_FUNC, NPC_LID_LA, NPC_LT_LA_IH_NIX_ETHER, 0, 2); 681 } 682 683 static void npc_set_features(struct rvu *rvu, int blkaddr, u8 intf) 684 { 685 struct npc_mcam *mcam = &rvu->hw->mcam; 686 u64 *features = &mcam->rx_features; 687 u64 proto_flags; 688 int hdr; 689 690 if (is_npc_intf_tx(intf)) 691 features = &mcam->tx_features; 692 693 for (hdr = NPC_DMAC; hdr < NPC_HEADER_FIELDS_MAX; hdr++) { 694 if (npc_check_field(rvu, blkaddr, hdr, intf)) 695 *features |= BIT_ULL(hdr); 696 } 697 698 proto_flags = BIT_ULL(NPC_SPORT_TCP) | BIT_ULL(NPC_SPORT_UDP) | 699 BIT_ULL(NPC_DPORT_TCP) | BIT_ULL(NPC_DPORT_UDP) | 700 BIT_ULL(NPC_SPORT_SCTP) | BIT_ULL(NPC_DPORT_SCTP) | 701 BIT_ULL(NPC_TYPE_ICMP) | BIT_ULL(NPC_CODE_ICMP) | 702 BIT_ULL(NPC_TCP_FLAGS); 703 704 /* for tcp/udp/sctp corresponding layer type should be in the key */ 705 if (*features & proto_flags) { 706 if (!npc_check_field(rvu, blkaddr, NPC_LD, intf)) 707 *features &= ~proto_flags; 708 else 709 *features |= BIT_ULL(NPC_IPPROTO_TCP) | 710 BIT_ULL(NPC_IPPROTO_UDP) | 711 BIT_ULL(NPC_IPPROTO_SCTP) | 712 BIT_ULL(NPC_IPPROTO_ICMP); 713 } 714 715 /* for AH/ICMP/ICMPv6/, check if corresponding layer type is present in the key */ 716 if (npc_check_field(rvu, blkaddr, NPC_LD, intf)) { 717 *features |= BIT_ULL(NPC_IPPROTO_AH); 718 *features |= BIT_ULL(NPC_IPPROTO_ICMP); 719 *features |= BIT_ULL(NPC_IPPROTO_ICMP6); 720 } 721 722 /* for ESP, check if corresponding layer type is present in the key */ 723 if (npc_check_field(rvu, blkaddr, NPC_LE, intf)) 724 *features |= BIT_ULL(NPC_IPPROTO_ESP); 725 726 /* for vlan corresponding layer type should be in the key */ 727 if (*features & BIT_ULL(NPC_OUTER_VID)) 728 if (!npc_check_field(rvu, blkaddr, NPC_LB, intf)) 729 *features &= ~BIT_ULL(NPC_OUTER_VID); 730 731 /* Warn on unrelated MKEX fields colliding with SPI key bits. AH/ESP 732 * sharing the same SPI key offset is valid; use npc_is_field_present(), 733 * not npc_check_field(), to advertise the feature. 734 */ 735 if (npc_check_overlap(rvu, blkaddr, NPC_IPSEC_SPI, 0, intf)) 736 dev_warn(rvu->dev, "Overlap detected the field NPC_IPSEC_SPI\n"); 737 if (npc_is_field_present(rvu, NPC_IPSEC_SPI, intf) && 738 (*features & (BIT_ULL(NPC_IPPROTO_ESP) | BIT_ULL(NPC_IPPROTO_AH)))) 739 *features |= BIT_ULL(NPC_IPSEC_SPI); 740 741 /* for vlan ethertypes corresponding layer type should be in the key */ 742 if (npc_check_field(rvu, blkaddr, NPC_LB, intf)) 743 *features |= BIT_ULL(NPC_VLAN_ETYPE_CTAG) | 744 BIT_ULL(NPC_VLAN_ETYPE_STAG); 745 746 /* for L2M/L2B/L3M/L3B, check if the type is present in the key */ 747 if (npc_check_field(rvu, blkaddr, NPC_LXMB, intf)) 748 *features |= BIT_ULL(NPC_LXMB); 749 750 for (hdr = NPC_MPLS1_LBTCBOS; hdr <= NPC_MPLS4_TTL; hdr++) { 751 if (npc_check_field(rvu, blkaddr, hdr, intf)) 752 *features |= BIT_ULL(hdr); 753 } 754 } 755 756 /* Scan key extraction profile and record how fields of our interest 757 * fill the key structure. Also verify Channel and DMAC exists in 758 * key and not overwritten by other header fields. 759 */ 760 static int npc_scan_kex(struct rvu *rvu, int blkaddr, u8 intf) 761 { 762 struct npc_mcam *mcam = &rvu->hw->mcam; 763 u8 lid, lt, ld, bitnr; 764 u64 cfg, masked_cfg; 765 u8 key_nibble = 0; 766 int extr; 767 768 /* Scan and note how parse result is going to be in key. 769 * A bit set in PARSE_NIBBLE_ENA corresponds to a nibble from 770 * parse result in the key. The enabled nibbles from parse result 771 * will be concatenated in key. 772 */ 773 cfg = rvu_read64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf)); 774 if (is_cn20k(rvu->pdev)) { 775 masked_cfg = cfg & NPC_CN20K_PARSE_NIBBLE; 776 for_each_set_bit(bitnr, (unsigned long *)&masked_cfg, 777 NPC_CN20K_TOTAL_NIBBLE) { 778 npc_scan_parse_result(rvu, mcam, bitnr, 779 key_nibble, intf); 780 key_nibble++; 781 } 782 } else { 783 masked_cfg = cfg & NPC_PARSE_NIBBLE; 784 for_each_set_bit(bitnr, (unsigned long *)&masked_cfg, 785 NPC_TOTAL_NIBBLE) { 786 npc_scan_parse_result(rvu, mcam, bitnr, 787 key_nibble, intf); 788 key_nibble++; 789 } 790 } 791 792 /* Ignore exact match bits for mcam entries except the first rule 793 * which is drop on hit. This first rule is configured explitcitly by 794 * exact match code. 795 */ 796 masked_cfg = cfg & NPC_EXACT_NIBBLE; 797 bitnr = NPC_EXACT_NIBBLE_START; 798 for_each_set_bit_from(bitnr, (unsigned long *)&masked_cfg, NPC_EXACT_NIBBLE_END + 1) { 799 npc_scan_exact_result(rvu, mcam, bitnr, key_nibble, intf); 800 key_nibble++; 801 } 802 803 if (is_cn20k(rvu->pdev)) 804 goto skip_cn10k_config; 805 806 /* Scan and note how layer data is going to be in key */ 807 for (lid = 0; lid < NPC_MAX_LID; lid++) { 808 for (lt = 0; lt < NPC_MAX_LT; lt++) { 809 for (ld = 0; ld < NPC_MAX_LD; ld++) { 810 cfg = rvu_read64(rvu, blkaddr, 811 NPC_AF_INTFX_LIDX_LTX_LDX_CFG 812 (intf, lid, lt, ld)); 813 if (!FIELD_GET(NPC_LDATA_EN, cfg)) 814 continue; 815 npc_scan_ldata(rvu, blkaddr, lid, lt, cfg, 816 intf); 817 } 818 } 819 } 820 821 return 0; 822 823 skip_cn10k_config: 824 for (extr = 0 ; extr < rvu->hw->npc_kex_extr; extr++) { 825 lid = CN20K_GET_EXTR_LID(intf, extr); 826 for (lt = 0; lt < NPC_MAX_LT; lt++) { 827 cfg = CN20K_GET_EXTR_LT(intf, extr, lt); 828 if (!FIELD_GET(NPC_LDATA_EN, cfg)) 829 continue; 830 npc_scan_ldata(rvu, blkaddr, lid, lt, cfg, 831 intf); 832 } 833 } 834 return 0; 835 } 836 837 static int npc_scan_verify_kex(struct rvu *rvu, int blkaddr) 838 { 839 int err; 840 841 err = npc_scan_kex(rvu, blkaddr, NIX_INTF_RX); 842 if (err) 843 return err; 844 845 err = npc_scan_kex(rvu, blkaddr, NIX_INTF_TX); 846 if (err) 847 return err; 848 849 /* Channel is mandatory */ 850 if (!npc_is_field_present(rvu, NPC_CHAN, NIX_INTF_RX)) { 851 dev_err(rvu->dev, "Channel not present in Key\n"); 852 return -EINVAL; 853 } 854 /* check that none of the fields overwrite channel */ 855 if (npc_check_overlap(rvu, blkaddr, NPC_CHAN, 0, NIX_INTF_RX)) { 856 dev_err(rvu->dev, "Channel cannot be overwritten\n"); 857 return -EINVAL; 858 } 859 860 npc_set_features(rvu, blkaddr, NIX_INTF_TX); 861 npc_set_features(rvu, blkaddr, NIX_INTF_RX); 862 npc_handle_multi_layer_fields(rvu, blkaddr, NIX_INTF_TX); 863 npc_handle_multi_layer_fields(rvu, blkaddr, NIX_INTF_RX); 864 865 return 0; 866 } 867 868 int npc_flow_steering_init(struct rvu *rvu, int blkaddr) 869 { 870 struct npc_mcam *mcam = &rvu->hw->mcam; 871 872 INIT_LIST_HEAD(&mcam->mcam_rules); 873 874 return npc_scan_verify_kex(rvu, blkaddr); 875 } 876 877 static int npc_check_unsupported_flows(struct rvu *rvu, u64 features, u8 intf) 878 { 879 struct npc_mcam *mcam = &rvu->hw->mcam; 880 u64 *mcam_features = &mcam->rx_features; 881 u64 unsupported; 882 u8 bit; 883 884 if (is_npc_intf_tx(intf)) 885 mcam_features = &mcam->tx_features; 886 887 unsupported = (*mcam_features ^ features) & ~(*mcam_features); 888 if (unsupported) { 889 dev_warn(rvu->dev, "Unsupported flow(s):\n"); 890 for_each_set_bit(bit, (unsigned long *)&unsupported, 64) 891 dev_warn(rvu->dev, "%s ", npc_get_field_name(bit)); 892 return -EOPNOTSUPP; 893 } 894 895 return 0; 896 } 897 898 /* npc_update_entry - Based on the masks generated during 899 * the key scanning, updates the given entry with value and 900 * masks for the field of interest. Maximum 16 bytes of a packet 901 * header can be extracted by HW hence lo and hi are sufficient. 902 * When field bytes are less than or equal to 8 then hi should be 903 * 0 for value and mask. 904 * 905 * If exact match of value is required then mask should be all 1's. 906 * If any bits in mask are 0 then corresponding bits in value are 907 * dont care. 908 */ 909 void npc_update_entry(struct rvu *rvu, enum key_fields type, 910 struct mcam_entry_mdata *mdata, u64 val_lo, 911 u64 val_hi, u64 mask_lo, u64 mask_hi, u8 intf) 912 { 913 u64 kw_mask[NPC_KWS_IN_KEY_SZ_MAX] = { 0 }; 914 u64 kw[NPC_KWS_IN_KEY_SZ_MAX] = { 0 }; 915 struct npc_mcam *mcam = &rvu->hw->mcam; 916 struct npc_key_field *field; 917 u64 kw1, kw2, kw3; 918 u64 *val, *mask; 919 int i, max_kw; 920 u8 shift; 921 922 field = &mcam->rx_key_fields[type]; 923 if (is_npc_intf_tx(intf)) 924 field = &mcam->tx_key_fields[type]; 925 926 if (!field->nr_kws) 927 return; 928 929 if (is_cn20k(rvu->pdev)) 930 max_kw = NPC_KWS_IN_KEY_SZ_8; 931 else 932 max_kw = NPC_KWS_IN_KEY_SZ_7; 933 934 for (i = 0; i < max_kw; i++) { 935 if (!field->kw_mask[i]) 936 continue; 937 /* place key value in kw[x] */ 938 shift = __ffs64(field->kw_mask[i]); 939 /* update entry value */ 940 kw1 = (val_lo << shift) & field->kw_mask[i]; 941 kw[i] = kw1; 942 /* update entry mask */ 943 kw1 = (mask_lo << shift) & field->kw_mask[i]; 944 kw_mask[i] = kw1; 945 946 if (field->nr_kws == 1) 947 break; 948 /* place remaining bits of key value in kw[x + 1] */ 949 if (field->nr_kws == 2) { 950 /* update entry value */ 951 kw2 = shift ? val_lo >> (64 - shift) : 0; 952 kw2 |= (val_hi << shift); 953 kw2 &= field->kw_mask[i + 1]; 954 kw[i + 1] = kw2; 955 /* update entry mask */ 956 kw2 = shift ? mask_lo >> (64 - shift) : 0; 957 kw2 |= (mask_hi << shift); 958 kw2 &= field->kw_mask[i + 1]; 959 kw_mask[i + 1] = kw2; 960 break; 961 } 962 /* place remaining bits of key value in kw[x + 1], kw[x + 2] */ 963 if (field->nr_kws == 3) { 964 /* update entry value */ 965 kw2 = shift ? val_lo >> (64 - shift) : 0; 966 kw2 |= (val_hi << shift); 967 kw2 &= field->kw_mask[i + 1]; 968 kw3 = shift ? val_hi >> (64 - shift) : 0; 969 kw3 &= field->kw_mask[i + 2]; 970 kw[i + 1] = kw2; 971 kw[i + 2] = kw3; 972 /* update entry mask */ 973 kw2 = shift ? mask_lo >> (64 - shift) : 0; 974 kw2 |= (mask_hi << shift); 975 kw2 &= field->kw_mask[i + 1]; 976 kw3 = shift ? mask_hi >> (64 - shift) : 0; 977 kw3 &= field->kw_mask[i + 2]; 978 kw_mask[i + 1] = kw2; 979 kw_mask[i + 2] = kw3; 980 break; 981 } 982 } 983 /* dummy is ready with values and masks for given key 984 * field now clear and update input entry with those 985 */ 986 987 val = mdata->kw; 988 mask = mdata->kw_mask; 989 990 for (i = 0; i < max_kw; i++, val++, mask++) { 991 if (!field->kw_mask[i]) 992 continue; 993 994 *val &= ~field->kw_mask[i]; 995 *mask &= ~field->kw_mask[i]; 996 997 *val |= kw[i]; 998 *mask |= kw_mask[i]; 999 } 1000 } 1001 1002 static void npc_update_ipv6_flow(struct rvu *rvu, 1003 struct mcam_entry_mdata *mdata, 1004 u64 features, struct flow_msg *pkt, 1005 struct flow_msg *mask, 1006 struct rvu_npc_mcam_rule *output, u8 intf) 1007 { 1008 u32 src_ip[IPV6_WORDS], src_ip_mask[IPV6_WORDS]; 1009 u32 dst_ip[IPV6_WORDS], dst_ip_mask[IPV6_WORDS]; 1010 struct flow_msg *opkt = &output->packet; 1011 struct flow_msg *omask = &output->mask; 1012 u64 mask_lo, mask_hi; 1013 u64 val_lo, val_hi; 1014 1015 /* For an ipv6 address fe80::2c68:63ff:fe5e:2d0a the packet 1016 * values to be programmed in MCAM should as below: 1017 * val_high: 0xfe80000000000000 1018 * val_low: 0x2c6863fffe5e2d0a 1019 */ 1020 if (features & BIT_ULL(NPC_SIP_IPV6)) { 1021 be32_to_cpu_array(src_ip_mask, mask->ip6src, IPV6_WORDS); 1022 be32_to_cpu_array(src_ip, pkt->ip6src, IPV6_WORDS); 1023 1024 mask_hi = (u64)src_ip_mask[0] << 32 | src_ip_mask[1]; 1025 mask_lo = (u64)src_ip_mask[2] << 32 | src_ip_mask[3]; 1026 val_hi = (u64)src_ip[0] << 32 | src_ip[1]; 1027 val_lo = (u64)src_ip[2] << 32 | src_ip[3]; 1028 1029 npc_update_entry(rvu, NPC_SIP_IPV6, mdata, val_lo, val_hi, 1030 mask_lo, mask_hi, intf); 1031 memcpy(opkt->ip6src, pkt->ip6src, sizeof(opkt->ip6src)); 1032 memcpy(omask->ip6src, mask->ip6src, sizeof(omask->ip6src)); 1033 } 1034 if (features & BIT_ULL(NPC_DIP_IPV6)) { 1035 be32_to_cpu_array(dst_ip_mask, mask->ip6dst, IPV6_WORDS); 1036 be32_to_cpu_array(dst_ip, pkt->ip6dst, IPV6_WORDS); 1037 1038 mask_hi = (u64)dst_ip_mask[0] << 32 | dst_ip_mask[1]; 1039 mask_lo = (u64)dst_ip_mask[2] << 32 | dst_ip_mask[3]; 1040 val_hi = (u64)dst_ip[0] << 32 | dst_ip[1]; 1041 val_lo = (u64)dst_ip[2] << 32 | dst_ip[3]; 1042 1043 npc_update_entry(rvu, NPC_DIP_IPV6, mdata, val_lo, val_hi, 1044 mask_lo, mask_hi, intf); 1045 memcpy(opkt->ip6dst, pkt->ip6dst, sizeof(opkt->ip6dst)); 1046 memcpy(omask->ip6dst, mask->ip6dst, sizeof(omask->ip6dst)); 1047 } 1048 } 1049 1050 static void npc_update_vlan_features(struct rvu *rvu, 1051 struct mcam_entry_mdata *mdata, 1052 u64 features, u8 intf) 1053 { 1054 bool ctag = !!(features & BIT_ULL(NPC_VLAN_ETYPE_CTAG)); 1055 bool stag = !!(features & BIT_ULL(NPC_VLAN_ETYPE_STAG)); 1056 bool vid = !!(features & BIT_ULL(NPC_OUTER_VID)); 1057 1058 /* If only VLAN id is given then always match outer VLAN id */ 1059 if (vid && !ctag && !stag) { 1060 npc_update_entry(rvu, NPC_LB, mdata, 1061 NPC_LT_LB_STAG_QINQ | NPC_LT_LB_CTAG, 0, 1062 NPC_LT_LB_STAG_QINQ & NPC_LT_LB_CTAG, 0, intf); 1063 return; 1064 } 1065 if (ctag) 1066 npc_update_entry(rvu, NPC_LB, mdata, NPC_LT_LB_CTAG, 0, 1067 ~0ULL, 0, intf); 1068 if (stag) 1069 npc_update_entry(rvu, NPC_LB, mdata, NPC_LT_LB_STAG_QINQ, 0, 1070 ~0ULL, 0, intf); 1071 } 1072 1073 void npc_update_flow(struct rvu *rvu, struct mcam_entry_mdata *mdata, 1074 u64 features, struct flow_msg *pkt, 1075 struct flow_msg *mask, 1076 struct rvu_npc_mcam_rule *output, u8 intf, 1077 int blkaddr) 1078 { 1079 u64 dmac_mask = ether_addr_to_u64(mask->dmac); 1080 u64 smac_mask = ether_addr_to_u64(mask->smac); 1081 u64 dmac_val = ether_addr_to_u64(pkt->dmac); 1082 u64 smac_val = ether_addr_to_u64(pkt->smac); 1083 struct flow_msg *opkt = &output->packet; 1084 struct flow_msg *omask = &output->mask; 1085 1086 if (!features) 1087 return; 1088 1089 /* For tcp/udp/sctp LTYPE should be present in entry */ 1090 if (features & BIT_ULL(NPC_IPPROTO_TCP)) 1091 npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_TCP, 1092 0, ~0ULL, 0, intf); 1093 if (features & BIT_ULL(NPC_IPPROTO_UDP)) 1094 npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_UDP, 1095 0, ~0ULL, 0, intf); 1096 if (features & BIT_ULL(NPC_IPPROTO_SCTP)) 1097 npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_SCTP, 1098 0, ~0ULL, 0, intf); 1099 if (features & BIT_ULL(NPC_IPPROTO_ICMP)) 1100 npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_ICMP, 1101 0, ~0ULL, 0, intf); 1102 if (features & BIT_ULL(NPC_IPPROTO_ICMP6)) 1103 npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_ICMP6, 1104 0, ~0ULL, 0, intf); 1105 1106 /* For AH, LTYPE should be present in entry */ 1107 if (features & BIT_ULL(NPC_IPPROTO_AH)) 1108 npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_AH, 1109 0, ~0ULL, 0, intf); 1110 /* For ESP, LTYPE should be present in entry */ 1111 if (features & BIT_ULL(NPC_IPPROTO_ESP)) 1112 npc_update_entry(rvu, NPC_LE, mdata, NPC_LT_LE_ESP, 1113 0, ~0ULL, 0, intf); 1114 1115 if (features & BIT_ULL(NPC_LXMB)) { 1116 output->lxmb = is_broadcast_ether_addr(pkt->dmac) ? 2 : 1; 1117 npc_update_entry(rvu, NPC_LXMB, mdata, output->lxmb, 0, 1118 output->lxmb, 0, intf); 1119 } 1120 #define NPC_WRITE_FLOW(field, member, val_lo, val_hi, mask_lo, mask_hi) \ 1121 do { \ 1122 if (features & BIT_ULL((field))) { \ 1123 npc_update_entry(rvu, (field), mdata, (val_lo), (val_hi), \ 1124 (mask_lo), (mask_hi), intf); \ 1125 memcpy(&opkt->member, &pkt->member, sizeof(pkt->member)); \ 1126 memcpy(&omask->member, &mask->member, sizeof(mask->member)); \ 1127 } \ 1128 } while (0) 1129 1130 NPC_WRITE_FLOW(NPC_DMAC, dmac, dmac_val, 0, dmac_mask, 0); 1131 1132 NPC_WRITE_FLOW(NPC_SMAC, smac, smac_val, 0, smac_mask, 0); 1133 NPC_WRITE_FLOW(NPC_ETYPE, etype, ntohs(pkt->etype), 0, 1134 ntohs(mask->etype), 0); 1135 NPC_WRITE_FLOW(NPC_TOS, tos, pkt->tos, 0, mask->tos, 0); 1136 NPC_WRITE_FLOW(NPC_IPFRAG_IPV4, ip_flag, pkt->ip_flag, 0, 1137 mask->ip_flag, 0); 1138 NPC_WRITE_FLOW(NPC_SIP_IPV4, ip4src, ntohl(pkt->ip4src), 0, 1139 ntohl(mask->ip4src), 0); 1140 NPC_WRITE_FLOW(NPC_DIP_IPV4, ip4dst, ntohl(pkt->ip4dst), 0, 1141 ntohl(mask->ip4dst), 0); 1142 NPC_WRITE_FLOW(NPC_SPORT_TCP, sport, ntohs(pkt->sport), 0, 1143 ntohs(mask->sport), 0); 1144 NPC_WRITE_FLOW(NPC_SPORT_UDP, sport, ntohs(pkt->sport), 0, 1145 ntohs(mask->sport), 0); 1146 NPC_WRITE_FLOW(NPC_DPORT_TCP, dport, ntohs(pkt->dport), 0, 1147 ntohs(mask->dport), 0); 1148 NPC_WRITE_FLOW(NPC_DPORT_UDP, dport, ntohs(pkt->dport), 0, 1149 ntohs(mask->dport), 0); 1150 NPC_WRITE_FLOW(NPC_SPORT_SCTP, sport, ntohs(pkt->sport), 0, 1151 ntohs(mask->sport), 0); 1152 NPC_WRITE_FLOW(NPC_DPORT_SCTP, dport, ntohs(pkt->dport), 0, 1153 ntohs(mask->dport), 0); 1154 NPC_WRITE_FLOW(NPC_TYPE_ICMP, icmp_type, pkt->icmp_type, 0, 1155 mask->icmp_type, 0); 1156 NPC_WRITE_FLOW(NPC_CODE_ICMP, icmp_code, pkt->icmp_code, 0, 1157 mask->icmp_code, 0); 1158 NPC_WRITE_FLOW(NPC_TCP_FLAGS, tcp_flags, ntohs(pkt->tcp_flags), 0, 1159 ntohs(mask->tcp_flags), 0); 1160 NPC_WRITE_FLOW(NPC_IPSEC_SPI, spi, ntohl(pkt->spi), 0, 1161 ntohl(mask->spi), 0); 1162 1163 NPC_WRITE_FLOW(NPC_OUTER_VID, vlan_tci, ntohs(pkt->vlan_tci), 0, 1164 ntohs(mask->vlan_tci), 0); 1165 NPC_WRITE_FLOW(NPC_INNER_VID, vlan_itci, ntohs(pkt->vlan_itci), 0, 1166 ntohs(mask->vlan_itci), 0); 1167 1168 NPC_WRITE_FLOW(NPC_MPLS1_LBTCBOS, mpls_lse, 1169 FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL, 1170 pkt->mpls_lse[0]), 0, 1171 FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL, 1172 mask->mpls_lse[0]), 0); 1173 NPC_WRITE_FLOW(NPC_MPLS1_TTL, mpls_lse, 1174 FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL, 1175 pkt->mpls_lse[0]), 0, 1176 FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL, 1177 mask->mpls_lse[0]), 0); 1178 NPC_WRITE_FLOW(NPC_MPLS2_LBTCBOS, mpls_lse, 1179 FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL, 1180 pkt->mpls_lse[1]), 0, 1181 FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL, 1182 mask->mpls_lse[1]), 0); 1183 NPC_WRITE_FLOW(NPC_MPLS2_TTL, mpls_lse, 1184 FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL, 1185 pkt->mpls_lse[1]), 0, 1186 FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL, 1187 mask->mpls_lse[1]), 0); 1188 NPC_WRITE_FLOW(NPC_MPLS3_LBTCBOS, mpls_lse, 1189 FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL, 1190 pkt->mpls_lse[2]), 0, 1191 FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL, 1192 mask->mpls_lse[2]), 0); 1193 NPC_WRITE_FLOW(NPC_MPLS3_TTL, mpls_lse, 1194 FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL, 1195 pkt->mpls_lse[2]), 0, 1196 FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL, 1197 mask->mpls_lse[2]), 0); 1198 NPC_WRITE_FLOW(NPC_MPLS4_LBTCBOS, mpls_lse, 1199 FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL, 1200 pkt->mpls_lse[3]), 0, 1201 FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL, 1202 mask->mpls_lse[3]), 0); 1203 NPC_WRITE_FLOW(NPC_MPLS4_TTL, mpls_lse, 1204 FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL, 1205 pkt->mpls_lse[3]), 0, 1206 FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL, 1207 mask->mpls_lse[3]), 0); 1208 1209 NPC_WRITE_FLOW(NPC_IPFRAG_IPV6, next_header, pkt->next_header, 0, 1210 mask->next_header, 0); 1211 npc_update_ipv6_flow(rvu, mdata, features, pkt, mask, output, intf); 1212 npc_update_vlan_features(rvu, mdata, features, intf); 1213 1214 npc_update_field_hash(rvu, intf, mdata, blkaddr, features, 1215 pkt, mask, opkt, omask); 1216 } 1217 1218 static struct rvu_npc_mcam_rule *rvu_mcam_find_rule(struct npc_mcam *mcam, u16 entry) 1219 { 1220 struct rvu_npc_mcam_rule *iter; 1221 1222 mutex_lock(&mcam->lock); 1223 list_for_each_entry(iter, &mcam->mcam_rules, list) { 1224 if (iter->entry == entry) { 1225 mutex_unlock(&mcam->lock); 1226 return iter; 1227 } 1228 } 1229 mutex_unlock(&mcam->lock); 1230 1231 return NULL; 1232 } 1233 1234 static void rvu_mcam_add_rule(struct npc_mcam *mcam, 1235 struct rvu_npc_mcam_rule *rule) 1236 { 1237 struct list_head *head = &mcam->mcam_rules; 1238 struct rvu_npc_mcam_rule *iter; 1239 1240 mutex_lock(&mcam->lock); 1241 list_for_each_entry(iter, &mcam->mcam_rules, list) { 1242 if (iter->entry > rule->entry) 1243 break; 1244 head = &iter->list; 1245 } 1246 1247 list_add(&rule->list, head); 1248 mutex_unlock(&mcam->lock); 1249 } 1250 1251 static void rvu_mcam_remove_counter_from_rule(struct rvu *rvu, u16 pcifunc, 1252 struct rvu_npc_mcam_rule *rule) 1253 { 1254 struct npc_mcam *mcam = &rvu->hw->mcam; 1255 1256 /* There is no counter allotted for cn20k */ 1257 if (is_cn20k(rvu->pdev)) 1258 return; 1259 1260 mutex_lock(&mcam->lock); 1261 1262 __rvu_mcam_remove_counter_from_rule(rvu, pcifunc, rule); 1263 1264 mutex_unlock(&mcam->lock); 1265 } 1266 1267 static void rvu_mcam_add_counter_to_rule(struct rvu *rvu, u16 pcifunc, 1268 struct rvu_npc_mcam_rule *rule, 1269 struct npc_install_flow_rsp *rsp) 1270 { 1271 struct npc_mcam *mcam = &rvu->hw->mcam; 1272 1273 mutex_lock(&mcam->lock); 1274 1275 __rvu_mcam_add_counter_to_rule(rvu, pcifunc, rule, rsp); 1276 1277 mutex_unlock(&mcam->lock); 1278 } 1279 1280 static int npc_mcast_update_action_index(struct rvu *rvu, struct npc_install_flow_req *req, 1281 u64 op, void *action) 1282 { 1283 int mce_index; 1284 1285 /* If a PF/VF is installing a multicast rule then it is expected 1286 * that the PF/VF should have created a group for the multicast/mirror 1287 * list. Otherwise reject the configuration. 1288 * During this scenario, req->index is set as multicast/mirror 1289 * group index. 1290 */ 1291 if (req->hdr.pcifunc && 1292 (op == NIX_RX_ACTIONOP_MCAST || op == NIX_TX_ACTIONOP_MCAST)) { 1293 mce_index = rvu_nix_mcast_get_mce_index(rvu, req->hdr.pcifunc, req->index); 1294 if (mce_index < 0) 1295 return mce_index; 1296 1297 if (op == NIX_RX_ACTIONOP_MCAST) 1298 ((struct nix_rx_action *)action)->index = mce_index; 1299 else 1300 ((struct nix_tx_action *)action)->index = mce_index; 1301 } 1302 1303 return 0; 1304 } 1305 1306 void 1307 npc_populate_mcam_mdata(struct rvu *rvu, 1308 struct mcam_entry_mdata *mdata, 1309 struct cn20k_mcam_entry *cn20k_entry, 1310 struct mcam_entry *entry) 1311 { 1312 if (is_cn20k(rvu->pdev)) { 1313 mdata->kw = cn20k_entry->kw; 1314 mdata->kw_mask = cn20k_entry->kw_mask; 1315 mdata->action = &cn20k_entry->action; 1316 mdata->vtag_action = &cn20k_entry->vtag_action; 1317 mdata->max_kw = NPC_KWS_IN_KEY_SZ_8; 1318 return; 1319 } 1320 mdata->kw = entry->kw; 1321 mdata->kw_mask = entry->kw_mask; 1322 mdata->action = &entry->action; 1323 mdata->vtag_action = &entry->vtag_action; 1324 mdata->max_kw = NPC_KWS_IN_KEY_SZ_7; 1325 } 1326 1327 static int npc_update_rx_entry(struct rvu *rvu, struct rvu_pfvf *pfvf, 1328 struct mcam_entry_mdata *mdata, 1329 struct npc_install_flow_req *req, 1330 u16 target, bool pf_set_vfs_mac) 1331 { 1332 struct rvu_switch *rswitch = &rvu->rswitch; 1333 struct nix_rx_action action; 1334 int ret; 1335 1336 if (rswitch->mode == DEVLINK_ESWITCH_MODE_SWITCHDEV && pf_set_vfs_mac) 1337 req->chan_mask = 0x0; /* Do not care channel */ 1338 1339 npc_update_entry(rvu, NPC_CHAN, mdata, req->channel, 0, req->chan_mask, 1340 0, NIX_INTF_RX); 1341 1342 *(u64 *)&action = 0x00; 1343 action.pf_func = target; 1344 action.op = req->op; 1345 action.index = req->index; 1346 1347 ret = npc_mcast_update_action_index(rvu, req, action.op, (void *)&action); 1348 if (ret) 1349 return ret; 1350 1351 action.match_id = req->match_id; 1352 action.flow_key_alg = req->flow_key_alg; 1353 1354 if (req->op == NIX_RX_ACTION_DEFAULT) { 1355 if (pfvf->def_ucast_rule) { 1356 action = pfvf->def_ucast_rule->rx_action; 1357 } else { 1358 /* For profiles which do not extract DMAC, the default 1359 * unicast entry is unused. Hence modify action for the 1360 * requests which use same action as default unicast 1361 * entry 1362 */ 1363 *(u64 *)&action = 0; 1364 action.pf_func = target; 1365 action.op = NIX_RX_ACTIONOP_UCAST; 1366 } 1367 if (req->match_id) 1368 action.match_id = req->match_id; 1369 } 1370 1371 *mdata->action = *(u64 *)&action; 1372 1373 /* VTAG0 starts at 0th byte of LID_B. 1374 * VTAG1 starts at 4th byte of LID_B. 1375 */ 1376 *mdata->vtag_action = FIELD_PREP(RX_VTAG0_VALID_BIT, req->vtag0_valid) | 1377 FIELD_PREP(RX_VTAG0_TYPE_MASK, req->vtag0_type) | 1378 FIELD_PREP(RX_VTAG0_LID_MASK, NPC_LID_LB) | 1379 FIELD_PREP(RX_VTAG0_RELPTR_MASK, 0) | 1380 FIELD_PREP(RX_VTAG1_VALID_BIT, req->vtag1_valid) | 1381 FIELD_PREP(RX_VTAG1_TYPE_MASK, req->vtag1_type) | 1382 FIELD_PREP(RX_VTAG1_LID_MASK, NPC_LID_LB) | 1383 FIELD_PREP(RX_VTAG1_RELPTR_MASK, 4); 1384 1385 return 0; 1386 } 1387 1388 static int npc_update_tx_entry(struct rvu *rvu, struct rvu_pfvf *pfvf, 1389 struct mcam_entry_mdata *mdata, 1390 struct npc_install_flow_req *req, u16 target) 1391 { 1392 struct nix_tx_action action; 1393 u64 mask = ~0ULL; 1394 int ret; 1395 1396 /* If AF is installing then do not care about 1397 * PF_FUNC in Send Descriptor 1398 */ 1399 if (is_pffunc_af(req->hdr.pcifunc)) 1400 mask = 0; 1401 1402 npc_update_entry(rvu, NPC_PF_FUNC, mdata, (__force u16)htons(target), 1403 0, mask, 0, NIX_INTF_TX); 1404 1405 *(u64 *)&action = 0x00; 1406 action.op = req->op; 1407 action.index = req->index; 1408 1409 ret = npc_mcast_update_action_index(rvu, req, action.op, (void *)&action); 1410 if (ret) 1411 return ret; 1412 1413 action.match_id = req->match_id; 1414 1415 *mdata->action = *(u64 *)&action; 1416 1417 /* VTAG0 starts at 0th byte of LID_B. 1418 * VTAG1 starts at 4th byte of LID_B. 1419 */ 1420 *mdata->vtag_action = FIELD_PREP(TX_VTAG0_DEF_MASK, req->vtag0_def) | 1421 FIELD_PREP(TX_VTAG0_OP_MASK, req->vtag0_op) | 1422 FIELD_PREP(TX_VTAG0_LID_MASK, NPC_LID_LA) | 1423 FIELD_PREP(TX_VTAG0_RELPTR_MASK, 20) | 1424 FIELD_PREP(TX_VTAG1_DEF_MASK, req->vtag1_def) | 1425 FIELD_PREP(TX_VTAG1_OP_MASK, req->vtag1_op) | 1426 FIELD_PREP(TX_VTAG1_LID_MASK, NPC_LID_LA) | 1427 FIELD_PREP(TX_VTAG1_RELPTR_MASK, 24); 1428 1429 return 0; 1430 } 1431 1432 static int npc_install_flow(struct rvu *rvu, int blkaddr, u16 target, 1433 int nixlf, struct rvu_pfvf *pfvf, 1434 struct npc_install_flow_req *req, 1435 struct npc_install_flow_rsp *rsp, bool enable, 1436 bool pf_set_vfs_mac) 1437 { 1438 struct rvu_npc_mcam_rule *def_ucast_rule = pfvf->def_ucast_rule; 1439 struct npc_cn20k_mcam_write_entry_req cn20k_wreq = { 0 }; 1440 u64 features, installed_features, missing_features = 0; 1441 struct npc_mcam_write_entry_req write_req = { 0 }; 1442 struct npc_mcam *mcam = &rvu->hw->mcam; 1443 struct cn20k_mcam_entry *cn20k_entry; 1444 struct mcam_entry_mdata mdata = { }; 1445 struct rvu_npc_mcam_rule dummy = { 0 }; 1446 struct rvu_npc_mcam_rule *rule; 1447 u16 owner = req->hdr.pcifunc; 1448 struct msg_rsp write_rsp; 1449 struct mcam_entry *entry; 1450 bool new = false; 1451 int entry_index; 1452 int err; 1453 1454 installed_features = req->features; 1455 features = req->features; 1456 entry_index = req->entry; 1457 1458 cn20k_entry = &cn20k_wreq.entry_data; 1459 entry = &write_req.entry_data; 1460 1461 npc_populate_mcam_mdata(rvu, &mdata, cn20k_entry, entry); 1462 1463 npc_update_flow(rvu, &mdata, features, &req->packet, &req->mask, &dummy, 1464 req->intf, blkaddr); 1465 1466 if (is_npc_intf_rx(req->intf)) { 1467 err = npc_update_rx_entry(rvu, pfvf, &mdata, req, target, 1468 pf_set_vfs_mac); 1469 if (err) 1470 return err; 1471 } else { 1472 err = npc_update_tx_entry(rvu, pfvf, &mdata, req, target); 1473 if (err) 1474 return err; 1475 } 1476 1477 /* Default unicast rules do not exist for TX */ 1478 if (is_npc_intf_tx(req->intf)) 1479 goto find_rule; 1480 1481 if (req->default_rule) { 1482 entry_index = npc_get_nixlf_mcam_index(mcam, target, nixlf, 1483 NIXLF_UCAST_ENTRY); 1484 1485 if (entry_index < 0) { 1486 dev_err(rvu->dev, 1487 "%s: Error to get ucast entry for target=%#x\n", 1488 __func__, target); 1489 return -EINVAL; 1490 } 1491 1492 enable = is_mcam_entry_enabled(rvu, mcam, blkaddr, entry_index); 1493 } 1494 1495 /* update mcam entry with default unicast rule attributes */ 1496 if (def_ucast_rule && (req->default_rule && req->append)) { 1497 missing_features = (def_ucast_rule->features ^ features) & 1498 def_ucast_rule->features; 1499 if (missing_features) 1500 npc_update_flow(rvu, &mdata, missing_features, 1501 &def_ucast_rule->packet, 1502 &def_ucast_rule->mask, 1503 &dummy, req->intf, 1504 blkaddr); 1505 installed_features = req->features | missing_features; 1506 } 1507 1508 find_rule: 1509 rule = rvu_mcam_find_rule(mcam, entry_index); 1510 if (!rule) { 1511 rule = kzalloc_obj(*rule); 1512 if (!rule) 1513 return -ENOMEM; 1514 new = true; 1515 } 1516 1517 if (!is_cn20k(rvu->pdev)) { 1518 write_req.hdr.pcifunc = owner; 1519 1520 /* allocate new counter if rule has no counter */ 1521 if (!req->default_rule && req->set_cntr && !rule->has_cntr) 1522 rvu_mcam_add_counter_to_rule(rvu, owner, rule, rsp); 1523 1524 /* if user wants to delete an existing counter for a rule then 1525 * free the counter 1526 */ 1527 if (!req->set_cntr && rule->has_cntr) 1528 rvu_mcam_remove_counter_from_rule(rvu, owner, rule); 1529 1530 /* AF owns the default rules so change the owner just to relax 1531 * the checks in rvu_mbox_handler_npc_mcam_write_entry 1532 */ 1533 if (req->default_rule) 1534 write_req.hdr.pcifunc = 0; 1535 1536 write_req.entry = entry_index; 1537 write_req.intf = req->intf; 1538 write_req.enable_entry = (u8)enable; 1539 /* if counter is available then clear and use it */ 1540 if (req->set_cntr && rule->has_cntr) { 1541 rvu_write64(rvu, blkaddr, 1542 NPC_AF_MATCH_STATX(rule->cntr), 1543 req->cntr_val); 1544 write_req.set_cntr = 1; 1545 write_req.cntr = rule->cntr; 1546 } 1547 goto update_rule; 1548 } 1549 1550 cn20k_wreq.hdr.pcifunc = owner; 1551 1552 if (req->default_rule) 1553 cn20k_wreq.hdr.pcifunc = 0; 1554 1555 cn20k_wreq.entry = entry_index; 1556 cn20k_wreq.intf = req->intf; 1557 cn20k_wreq.enable_entry = (u8)enable; 1558 cn20k_wreq.hw_prio = req->hw_prio; 1559 cn20k_wreq.req_kw_type = req->req_kw_type; 1560 1561 update_rule: 1562 1563 /* update rule */ 1564 memcpy(&rule->packet, &dummy.packet, sizeof(rule->packet)); 1565 memcpy(&rule->mask, &dummy.mask, sizeof(rule->mask)); 1566 rule->entry = entry_index; 1567 if (is_cn20k(rvu->pdev)) { 1568 memcpy(&rule->rx_action, &cn20k_entry->action, 1569 sizeof(struct nix_rx_action)); 1570 if (is_npc_intf_tx(req->intf)) 1571 memcpy(&rule->tx_action, &cn20k_entry->action, 1572 sizeof(struct nix_tx_action)); 1573 rule->vtag_action = cn20k_entry->vtag_action; 1574 } else { 1575 memcpy(&rule->rx_action, &entry->action, 1576 sizeof(struct nix_rx_action)); 1577 if (is_npc_intf_tx(req->intf)) 1578 memcpy(&rule->tx_action, &entry->action, 1579 sizeof(struct nix_tx_action)); 1580 rule->vtag_action = entry->vtag_action; 1581 } 1582 1583 rule->features = installed_features; 1584 rule->default_rule = req->default_rule; 1585 rule->owner = owner; 1586 rule->enable = enable; 1587 1588 if (is_cn20k(rvu->pdev)) { 1589 rule->chan_mask = cn20k_wreq.entry_data.kw_mask[0] & 1590 NPC_KEX_CHAN_MASK; 1591 rule->chan = cn20k_wreq.entry_data.kw[0] & 1592 NPC_KEX_CHAN_MASK; 1593 } else { 1594 rule->chan_mask = write_req.entry_data.kw_mask[0] & 1595 NPC_KEX_CHAN_MASK; 1596 rule->chan = write_req.entry_data.kw[0] & NPC_KEX_CHAN_MASK; 1597 } 1598 1599 rule->chan &= rule->chan_mask; 1600 rule->lxmb = dummy.lxmb; 1601 rule->hw_prio = req->hw_prio; 1602 if (is_npc_intf_tx(req->intf)) 1603 rule->intf = pfvf->nix_tx_intf; 1604 else 1605 rule->intf = pfvf->nix_rx_intf; 1606 1607 if (new) 1608 rvu_mcam_add_rule(mcam, rule); 1609 if (req->default_rule) 1610 pfvf->def_ucast_rule = rule; 1611 1612 /* write to mcam entry registers */ 1613 if (is_cn20k(rvu->pdev)) 1614 err = rvu_mbox_handler_npc_cn20k_mcam_write_entry(rvu, 1615 &cn20k_wreq, 1616 &write_rsp); 1617 else 1618 err = rvu_mbox_handler_npc_mcam_write_entry(rvu, &write_req, 1619 &write_rsp); 1620 1621 if (err) { 1622 rvu_mcam_remove_counter_from_rule(rvu, owner, rule); 1623 if (new) { 1624 list_del(&rule->list); 1625 kfree(rule); 1626 } 1627 return err; 1628 } 1629 1630 /* VF's MAC address is being changed via PF */ 1631 if (pf_set_vfs_mac) { 1632 ether_addr_copy(pfvf->default_mac, req->packet.dmac); 1633 ether_addr_copy(pfvf->mac_addr, req->packet.dmac); 1634 set_bit(PF_SET_VF_MAC, &pfvf->flags); 1635 } 1636 1637 if (test_bit(PF_SET_VF_CFG, &pfvf->flags) && 1638 req->vtag0_type == NIX_AF_LFX_RX_VTAG_TYPE7) 1639 rule->vfvlan_cfg = true; 1640 1641 if (is_npc_intf_rx(req->intf) && req->match_id && 1642 (req->op == NIX_RX_ACTIONOP_UCAST || req->op == NIX_RX_ACTIONOP_RSS)) 1643 return rvu_nix_setup_ratelimit_aggr(rvu, req->hdr.pcifunc, 1644 req->index, req->match_id); 1645 1646 if (owner && req->op == NIX_RX_ACTIONOP_MCAST) 1647 return rvu_nix_mcast_update_mcam_entry(rvu, req->hdr.pcifunc, 1648 req->index, entry_index); 1649 1650 return 0; 1651 } 1652 1653 static int 1654 rvu_npc_free_entry_for_flow_install(struct rvu *rvu, u16 pcifunc, 1655 bool free_entry, int mcam_idx) 1656 { 1657 struct npc_mcam_free_entry_req free_req = { 0 }; 1658 struct msg_rsp rsp; 1659 int rc; 1660 1661 if (!free_entry) 1662 return 0; 1663 1664 free_req.hdr.pcifunc = pcifunc; 1665 free_req.entry = mcam_idx; 1666 rc = rvu_mbox_handler_npc_mcam_free_entry(rvu, &free_req, &rsp); 1667 return rc; 1668 } 1669 1670 static int 1671 rvu_npc_alloc_entry_for_flow_install(struct rvu *rvu, 1672 struct npc_install_flow_req *fl_req, 1673 u16 *mcam_idx, u8 *kw_type, 1674 bool *allocated) 1675 { 1676 struct npc_mcam_alloc_entry_req entry_req; 1677 struct npc_mcam_alloc_entry_rsp entry_rsp; 1678 struct npc_get_pfl_info_rsp rsp = { 0 }; 1679 struct npc_get_num_kws_req kws_req; 1680 struct npc_get_num_kws_rsp kws_rsp; 1681 int off, kw_bits, rc; 1682 struct msg_req req; 1683 u8 *src, *dst; 1684 1685 if (!is_cn20k(rvu->pdev)) { 1686 *kw_type = -1; 1687 return 0; 1688 } 1689 1690 if (!fl_req->alloc_entry) { 1691 *kw_type = -1; 1692 return 0; 1693 } 1694 1695 off = offsetof(struct npc_install_flow_req, packet); 1696 dst = (u8 *)&kws_req.fl + off; 1697 src = (u8 *)fl_req + off; 1698 memcpy(dst, src, sizeof(struct npc_install_flow_req) - off); 1699 rc = rvu_mbox_handler_npc_get_num_kws(rvu, &kws_req, &kws_rsp); 1700 if (rc) 1701 return rc; 1702 1703 kw_bits = kws_rsp.kws * 64; 1704 1705 *kw_type = NPC_MCAM_KEY_X2; 1706 if (kw_bits > 256) { 1707 rvu_mbox_handler_npc_get_pfl_info(rvu, &req, &rsp); 1708 if (rsp.kw_type == NPC_MCAM_KEY_X2) { 1709 dev_err(rvu->dev, 1710 "Only X2 entries are supported in X2 profile\n"); 1711 return -EOPNOTSUPP; 1712 } 1713 1714 *kw_type = NPC_MCAM_KEY_X4; 1715 } 1716 1717 memset(&entry_req, 0, sizeof(entry_req)); 1718 memset(&entry_rsp, 0, sizeof(entry_rsp)); 1719 1720 entry_req.hdr.pcifunc = fl_req->hdr.pcifunc; 1721 entry_req.ref_prio = fl_req->ref_prio; 1722 entry_req.ref_entry = fl_req->ref_entry; 1723 entry_req.kw_type = *kw_type; 1724 entry_req.count = 1; 1725 rc = rvu_mbox_handler_npc_mcam_alloc_entry(rvu, 1726 &entry_req, 1727 &entry_rsp); 1728 if (rc) 1729 return rc; 1730 1731 *mcam_idx = entry_rsp.entry_list[0]; 1732 *allocated = true; 1733 return 0; 1734 } 1735 1736 int rvu_mbox_handler_npc_install_flow(struct rvu *rvu, 1737 struct npc_install_flow_req *req, 1738 struct npc_install_flow_rsp *rsp) 1739 { 1740 bool from_vf = !!(req->hdr.pcifunc & RVU_PFVF_FUNC_MASK); 1741 bool from_rep_dev = !!is_rep_dev(rvu, req->hdr.pcifunc); 1742 struct rvu_switch *rswitch = &rvu->rswitch; 1743 int blkaddr, nixlf, err; 1744 struct rvu_pfvf *pfvf; 1745 bool pf_set_vfs_mac = false; 1746 bool allocated = false; 1747 bool enable = true; 1748 u8 kw_type; 1749 u16 target; 1750 1751 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0); 1752 if (blkaddr < 0) { 1753 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__); 1754 return NPC_MCAM_INVALID_REQ; 1755 } 1756 1757 if (!is_npc_interface_valid(rvu, req->intf)) 1758 return NPC_FLOW_INTF_INVALID; 1759 1760 err = rvu_npc_alloc_entry_for_flow_install(rvu, req, &req->entry, 1761 &kw_type, &allocated); 1762 if (err) { 1763 dev_err(rvu->dev, 1764 "%s: Error to alloc mcam entry for pcifunc=%#x\n", 1765 __func__, req->hdr.pcifunc); 1766 return err; 1767 } 1768 1769 req->entry = npc_cn20k_vidx2idx(req->entry); 1770 1771 /* If DMAC is not extracted in MKEX, rules installed by AF 1772 * can rely on L2MB bit set by hardware protocol checker for 1773 * broadcast and multicast addresses. 1774 */ 1775 if (npc_check_field(rvu, blkaddr, NPC_DMAC, req->intf)) 1776 goto process_flow; 1777 1778 if (is_pffunc_af(req->hdr.pcifunc) && 1779 req->features & BIT_ULL(NPC_DMAC)) { 1780 if (is_unicast_ether_addr(req->packet.dmac)) { 1781 dev_warn(rvu->dev, 1782 "%s: mkex profile does not support ucast flow\n", 1783 __func__); 1784 rvu_npc_free_entry_for_flow_install(rvu, 1785 req->hdr.pcifunc, 1786 allocated, 1787 req->entry); 1788 return NPC_FLOW_NOT_SUPPORTED; 1789 } 1790 1791 if (!npc_is_field_present(rvu, NPC_LXMB, req->intf)) { 1792 dev_warn(rvu->dev, 1793 "%s: mkex profile does not support bcast/mcast flow", 1794 __func__); 1795 rvu_npc_free_entry_for_flow_install(rvu, 1796 req->hdr.pcifunc, 1797 allocated, 1798 req->entry); 1799 return NPC_FLOW_NOT_SUPPORTED; 1800 } 1801 1802 /* Modify feature to use LXMB instead of DMAC */ 1803 req->features &= ~BIT_ULL(NPC_DMAC); 1804 req->features |= BIT_ULL(NPC_LXMB); 1805 } 1806 1807 process_flow: 1808 if (from_vf && req->default_rule) { 1809 rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc, 1810 allocated, req->entry); 1811 return NPC_FLOW_VF_PERM_DENIED; 1812 } 1813 1814 /* Each PF/VF info is maintained in struct rvu_pfvf. 1815 * rvu_pfvf for the target PF/VF needs to be retrieved 1816 * hence modify pcifunc accordingly. 1817 */ 1818 1819 if (!req->hdr.pcifunc) { 1820 /* AF installing for a PF/VF */ 1821 target = req->vf; 1822 } else if (!from_vf && req->vf && !from_rep_dev) { 1823 /* PF installing for its VF */ 1824 target = (req->hdr.pcifunc & ~RVU_PFVF_FUNC_MASK) | req->vf; 1825 pf_set_vfs_mac = req->default_rule && 1826 (req->features & BIT_ULL(NPC_DMAC)); 1827 } else if (from_rep_dev && req->vf) { 1828 /* Representor device installing for a representee */ 1829 target = req->vf; 1830 } else { 1831 /* msg received from PF/VF */ 1832 target = req->hdr.pcifunc; 1833 } 1834 1835 /* ignore chan_mask in case pf func is not AF, revisit later */ 1836 if (!is_pffunc_af(req->hdr.pcifunc)) 1837 req->chan_mask = rvu_get_cpt_chan_mask(rvu); 1838 1839 err = npc_check_unsupported_flows(rvu, req->features, req->intf); 1840 if (err) { 1841 rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc, 1842 allocated, req->entry); 1843 return NPC_FLOW_NOT_SUPPORTED; 1844 } 1845 1846 pfvf = rvu_get_pfvf(rvu, target); 1847 1848 if (from_rep_dev) 1849 req->channel = pfvf->rx_chan_base; 1850 /* PF installing for its VF */ 1851 if (req->hdr.pcifunc && !from_vf && req->vf && !from_rep_dev) 1852 set_bit(PF_SET_VF_CFG, &pfvf->flags); 1853 1854 /* update req destination mac addr */ 1855 if ((req->features & BIT_ULL(NPC_DMAC)) && is_npc_intf_rx(req->intf) && 1856 is_zero_ether_addr(req->packet.dmac)) { 1857 ether_addr_copy(req->packet.dmac, pfvf->mac_addr); 1858 eth_broadcast_addr((u8 *)&req->mask.dmac); 1859 } 1860 1861 /* Proceed if NIXLF is attached or not for TX rules */ 1862 err = nix_get_nixlf(rvu, target, &nixlf, NULL); 1863 if (err && is_npc_intf_rx(req->intf) && !pf_set_vfs_mac) { 1864 rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc, 1865 allocated, req->entry); 1866 return NPC_FLOW_NO_NIXLF; 1867 } 1868 1869 /* don't enable rule when nixlf not attached or initialized */ 1870 if (!(is_nixlf_attached(rvu, target) && 1871 test_bit(NIXLF_INITIALIZED, &pfvf->flags))) 1872 enable = false; 1873 1874 /* Packets reaching NPC in Tx path implies that a 1875 * NIXLF is properly setup and transmitting. 1876 * Hence rules can be enabled for Tx. 1877 */ 1878 if (is_npc_intf_tx(req->intf)) 1879 enable = true; 1880 1881 /* Do not allow requests from uninitialized VFs */ 1882 if (from_vf && !enable) { 1883 rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc, 1884 allocated, req->entry); 1885 return NPC_FLOW_VF_NOT_INIT; 1886 } 1887 1888 /* PF sets VF mac & VF NIXLF is not attached, update the mac addr */ 1889 if (pf_set_vfs_mac && !enable) { 1890 ether_addr_copy(pfvf->default_mac, req->packet.dmac); 1891 ether_addr_copy(pfvf->mac_addr, req->packet.dmac); 1892 set_bit(PF_SET_VF_MAC, &pfvf->flags); 1893 rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc, 1894 allocated, req->entry); 1895 return 0; 1896 } 1897 1898 mutex_lock(&rswitch->switch_lock); 1899 err = npc_install_flow(rvu, blkaddr, target, nixlf, pfvf, 1900 req, rsp, enable, pf_set_vfs_mac); 1901 if (err) 1902 rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc, 1903 allocated, req->entry); 1904 1905 rsp->kw_type = kw_type; 1906 rsp->entry = req->entry; 1907 mutex_unlock(&rswitch->switch_lock); 1908 1909 return err; 1910 } 1911 1912 static int npc_delete_flow(struct rvu *rvu, struct rvu_npc_mcam_rule *rule, 1913 u16 pcifunc) 1914 { 1915 struct npc_mcam_ena_dis_entry_req dis_req = { 0 }; 1916 struct msg_rsp dis_rsp; 1917 1918 if (rule->default_rule) 1919 return 0; 1920 1921 if (rule->has_cntr) 1922 rvu_mcam_remove_counter_from_rule(rvu, pcifunc, rule); 1923 1924 dis_req.hdr.pcifunc = pcifunc; 1925 dis_req.entry = rule->entry; 1926 1927 list_del(&rule->list); 1928 kfree(rule); 1929 1930 return rvu_mbox_handler_npc_mcam_dis_entry(rvu, &dis_req, &dis_rsp); 1931 } 1932 1933 int rvu_mbox_handler_npc_delete_flow(struct rvu *rvu, 1934 struct npc_delete_flow_req *req, 1935 struct npc_delete_flow_rsp *rsp) 1936 { 1937 struct npc_mcam *mcam = &rvu->hw->mcam; 1938 struct rvu_npc_mcam_rule *iter, *tmp; 1939 u16 pcifunc = req->hdr.pcifunc; 1940 struct list_head del_list; 1941 int blkaddr; 1942 1943 req->entry = npc_cn20k_vidx2idx(req->entry); 1944 req->start = npc_cn20k_vidx2idx(req->start); 1945 req->end = npc_cn20k_vidx2idx(req->end); 1946 1947 INIT_LIST_HEAD(&del_list); 1948 1949 mutex_lock(&mcam->lock); 1950 list_for_each_entry_safe(iter, tmp, &mcam->mcam_rules, list) { 1951 if (iter->owner == pcifunc) { 1952 /* All rules */ 1953 if (req->all) { 1954 list_move_tail(&iter->list, &del_list); 1955 /* Range of rules */ 1956 } else if (req->end && iter->entry >= req->start && 1957 iter->entry <= req->end) { 1958 list_move_tail(&iter->list, &del_list); 1959 /* single rule */ 1960 } else if (req->entry == iter->entry) { 1961 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0); 1962 if (blkaddr) 1963 rsp->cntr_val = rvu_read64(rvu, blkaddr, 1964 NPC_AF_MATCH_STATX(iter->cntr)); 1965 list_move_tail(&iter->list, &del_list); 1966 break; 1967 } 1968 } 1969 } 1970 mutex_unlock(&mcam->lock); 1971 1972 list_for_each_entry_safe(iter, tmp, &del_list, list) { 1973 u16 entry = iter->entry; 1974 1975 /* clear the mcam entry target pcifunc */ 1976 mcam->entry2target_pffunc[entry] = 0x0; 1977 if (npc_delete_flow(rvu, iter, pcifunc)) 1978 dev_err(rvu->dev, "rule deletion failed for entry:%u", 1979 entry); 1980 } 1981 1982 return 0; 1983 } 1984 1985 static int npc_update_dmac_value(struct rvu *rvu, int npcblkaddr, 1986 struct rvu_npc_mcam_rule *rule, 1987 struct rvu_pfvf *pfvf) 1988 { 1989 struct npc_cn20k_mcam_write_entry_req cn20k_wreq = { 0 }; 1990 struct npc_mcam_write_entry_req write_req = { 0 }; 1991 struct mcam_entry_mdata mdata = { }; 1992 struct npc_mcam *mcam = &rvu->hw->mcam; 1993 struct cn20k_mcam_entry *cn20k_entry; 1994 struct mcam_entry *entry; 1995 u8 intf, enable, hw_prio; 1996 struct msg_rsp rsp; 1997 int err; 1998 1999 cn20k_entry = &cn20k_wreq.entry_data; 2000 entry = &write_req.entry_data; 2001 npc_populate_mcam_mdata(rvu, &mdata, cn20k_entry, entry); 2002 2003 ether_addr_copy(rule->packet.dmac, pfvf->mac_addr); 2004 2005 if (is_cn20k(rvu->pdev)) { 2006 if (npc_cn20k_read_mcam_entry(rvu, npcblkaddr, rule->entry, 2007 cn20k_entry, &intf, 2008 &enable, &hw_prio)) 2009 return -EINVAL; 2010 } else { 2011 npc_read_mcam_entry(rvu, mcam, npcblkaddr, rule->entry, 2012 entry, &intf, &enable); 2013 } 2014 2015 npc_update_entry(rvu, NPC_DMAC, &mdata, 2016 ether_addr_to_u64(pfvf->mac_addr), 0, 2017 0xffffffffffffull, 0, intf); 2018 2019 mutex_unlock(&mcam->lock); 2020 if (is_cn20k(rvu->pdev)) { 2021 cn20k_wreq.hdr.pcifunc = rule->owner; 2022 cn20k_wreq.entry = rule->entry; 2023 cn20k_wreq.intf = pfvf->nix_rx_intf; 2024 err = rvu_mbox_handler_npc_cn20k_mcam_write_entry(rvu, 2025 &cn20k_wreq, 2026 &rsp); 2027 } else { 2028 write_req.hdr.pcifunc = rule->owner; 2029 write_req.entry = rule->entry; 2030 write_req.intf = pfvf->nix_rx_intf; 2031 err = rvu_mbox_handler_npc_mcam_write_entry(rvu, &write_req, 2032 &rsp); 2033 } 2034 mutex_lock(&mcam->lock); 2035 2036 return err; 2037 } 2038 2039 void npc_mcam_enable_flows(struct rvu *rvu, u16 target) 2040 { 2041 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, target); 2042 struct rvu_npc_mcam_rule *def_ucast_rule; 2043 struct npc_mcam *mcam = &rvu->hw->mcam; 2044 struct rvu_npc_mcam_rule *rule; 2045 int blkaddr, bank, index; 2046 u64 def_action; 2047 2048 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0); 2049 if (blkaddr < 0) 2050 return; 2051 2052 def_ucast_rule = pfvf->def_ucast_rule; 2053 2054 mutex_lock(&mcam->lock); 2055 list_for_each_entry(rule, &mcam->mcam_rules, list) { 2056 if (is_npc_intf_rx(rule->intf) && 2057 rule->rx_action.pf_func == target && !rule->enable) { 2058 if (rule->default_rule) { 2059 npc_enable_mcam_entry(rvu, mcam, blkaddr, 2060 rule->entry, true); 2061 rule->enable = true; 2062 continue; 2063 } 2064 2065 if (rule->vfvlan_cfg) { 2066 if (npc_update_dmac_value(rvu, blkaddr, rule, pfvf)) 2067 dev_err(rvu->dev, 2068 "Update dmac failed for %u, target=%#x\n", 2069 rule->entry, target); 2070 } 2071 2072 if (rule->rx_action.op == NIX_RX_ACTION_DEFAULT) { 2073 if (!def_ucast_rule) 2074 continue; 2075 /* Use default unicast entry action */ 2076 rule->rx_action = def_ucast_rule->rx_action; 2077 def_action = *(u64 *)&def_ucast_rule->rx_action; 2078 bank = npc_get_bank(mcam, rule->entry); 2079 rvu_write64(rvu, blkaddr, 2080 NPC_AF_MCAMEX_BANKX_ACTION 2081 (rule->entry, bank), def_action); 2082 } 2083 2084 npc_enable_mcam_entry(rvu, mcam, blkaddr, 2085 rule->entry, true); 2086 rule->enable = true; 2087 } 2088 } 2089 2090 /* Enable MCAM entries installed by PF with target as VF pcifunc */ 2091 for (index = 0; index < mcam->bmap_entries; index++) { 2092 if (mcam->entry2target_pffunc[index] == target) 2093 npc_enable_mcam_entry(rvu, mcam, blkaddr, 2094 index, true); 2095 } 2096 mutex_unlock(&mcam->lock); 2097 } 2098 2099 void npc_mcam_disable_flows(struct rvu *rvu, u16 target) 2100 { 2101 struct npc_mcam *mcam = &rvu->hw->mcam; 2102 int blkaddr, index; 2103 2104 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0); 2105 if (blkaddr < 0) 2106 return; 2107 2108 mutex_lock(&mcam->lock); 2109 /* Disable MCAM entries installed by PF with target as VF pcifunc */ 2110 for (index = 0; index < mcam->bmap_entries; index++) { 2111 if (mcam->entry2target_pffunc[index] == target) 2112 npc_enable_mcam_entry(rvu, mcam, blkaddr, 2113 index, false); 2114 } 2115 mutex_unlock(&mcam->lock); 2116 } 2117 2118 /* single drop on non hit rule starting from 0th index. This an extension 2119 * to RPM mac filter to support more rules. 2120 */ 2121 int npc_install_mcam_drop_rule(struct rvu *rvu, int mcam_idx, u16 *counter_idx, 2122 u64 chan_val, u64 chan_mask, u64 exact_val, u64 exact_mask, 2123 u64 bcast_mcast_val, u64 bcast_mcast_mask) 2124 { 2125 struct npc_cn20k_mcam_write_entry_req cn20k_req = { 0 }; 2126 struct npc_mcam_alloc_counter_req cntr_req = { 0 }; 2127 struct npc_mcam_alloc_counter_rsp cntr_rsp = { 0 }; 2128 struct npc_mcam_write_entry_req req = { 0 }; 2129 struct npc_mcam *mcam = &rvu->hw->mcam; 2130 struct mcam_entry_mdata mdata = { }; 2131 struct rvu_npc_mcam_rule *rule; 2132 struct msg_rsp rsp; 2133 bool enabled; 2134 int blkaddr; 2135 int err; 2136 2137 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0); 2138 if (blkaddr < 0) { 2139 dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__); 2140 return -ENODEV; 2141 } 2142 2143 /* Bail out if no exact match support */ 2144 if (!rvu_npc_exact_has_match_table(rvu)) { 2145 dev_info(rvu->dev, "%s: No support for exact match feature\n", __func__); 2146 return -EINVAL; 2147 } 2148 2149 /* If 0th entry is already used, return err */ 2150 enabled = is_mcam_entry_enabled(rvu, mcam, blkaddr, mcam_idx); 2151 if (enabled) { 2152 dev_err(rvu->dev, "%s: failed to add single drop on non hit rule at %d th index\n", 2153 __func__, mcam_idx); 2154 return -EINVAL; 2155 } 2156 2157 /* Add this entry to mcam rules list */ 2158 rule = kzalloc_obj(*rule); 2159 if (!rule) 2160 return -ENOMEM; 2161 2162 /* Disable rule by default. Enable rule when first dmac filter is 2163 * installed 2164 */ 2165 rule->enable = false; 2166 rule->chan = chan_val; 2167 rule->chan_mask = chan_mask; 2168 rule->entry = mcam_idx; 2169 rvu_mcam_add_rule(mcam, rule); 2170 2171 /* Reserve slot 0 */ 2172 npc_mcam_rsrcs_reserve(rvu, blkaddr, mcam_idx); 2173 2174 if (!is_cn20k(rvu->pdev)) { 2175 /* Allocate counter for this single drop on non hit rule */ 2176 cntr_req.hdr.pcifunc = 0; /* AF request */ 2177 cntr_req.contig = true; 2178 cntr_req.count = 1; 2179 err = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req, 2180 &cntr_rsp); 2181 if (err) { 2182 dev_err(rvu->dev, 2183 "%s: Err to allocate cntr for drop rule (err=%d)\n", 2184 __func__, err); 2185 return -EFAULT; 2186 } 2187 *counter_idx = cntr_rsp.cntr; 2188 } 2189 2190 npc_populate_mcam_mdata(rvu, &mdata, 2191 &cn20k_req.entry_data, 2192 &req.entry_data); 2193 2194 /* Fill in fields for this mcam entry */ 2195 npc_update_entry(rvu, NPC_EXACT_RESULT, &mdata, exact_val, 0, 2196 exact_mask, 0, NIX_INTF_RX); 2197 npc_update_entry(rvu, NPC_CHAN, &mdata, chan_val, 0, 2198 chan_mask, 0, NIX_INTF_RX); 2199 npc_update_entry(rvu, NPC_LXMB, &mdata, bcast_mcast_val, 0, 2200 bcast_mcast_mask, 0, NIX_INTF_RX); 2201 2202 if (is_cn20k(rvu->pdev)) { 2203 cn20k_req.intf = NIX_INTF_RX; 2204 cn20k_req.entry = mcam_idx; 2205 2206 err = rvu_mbox_handler_npc_cn20k_mcam_write_entry(rvu, 2207 &cn20k_req, 2208 &rsp); 2209 if (err) { 2210 dev_err(rvu->dev, 2211 "%s: Installation of single drop on non hit rule at %d failed\n", 2212 __func__, mcam_idx); 2213 return err; 2214 } 2215 2216 goto enable_entry; 2217 } 2218 2219 req.intf = NIX_INTF_RX; 2220 req.set_cntr = true; 2221 req.cntr = cntr_rsp.cntr; 2222 req.entry = mcam_idx; 2223 2224 err = rvu_mbox_handler_npc_mcam_write_entry(rvu, &req, &rsp); 2225 if (err) { 2226 dev_err(rvu->dev, 2227 "%s: Installation of single drop on non hit rule at %d failed\n", 2228 __func__, mcam_idx); 2229 return err; 2230 } 2231 2232 dev_dbg(rvu->dev, 2233 "%s: Installed single drop on non hit rule at %d, cntr=%d\n", 2234 __func__, mcam_idx, req.cntr); 2235 2236 enable_entry: 2237 /* disable entry at Bank 0, index 0 */ 2238 npc_enable_mcam_entry(rvu, mcam, blkaddr, mcam_idx, false); 2239 2240 return 0; 2241 } 2242 2243 int rvu_mbox_handler_npc_get_field_status(struct rvu *rvu, 2244 struct npc_get_field_status_req *req, 2245 struct npc_get_field_status_rsp *rsp) 2246 { 2247 int blkaddr; 2248 2249 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0); 2250 if (blkaddr < 0) 2251 return NPC_MCAM_INVALID_REQ; 2252 2253 if (!is_npc_interface_valid(rvu, req->intf)) 2254 return NPC_FLOW_INTF_INVALID; 2255 2256 if (npc_check_field(rvu, blkaddr, req->field, req->intf)) 2257 rsp->enable = 1; 2258 2259 return 0; 2260 } 2261