1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019, Intel Corporation. */ 3 4 #include "ice_common.h" 5 #include "ice_flex_pipe.h" 6 #include "ice_flow.h" 7 #include "ice.h" 8 9 static const u32 ice_sect_lkup[ICE_BLK_COUNT][ICE_SECT_COUNT] = { 10 /* SWITCH */ 11 { 12 ICE_SID_XLT0_SW, 13 ICE_SID_XLT_KEY_BUILDER_SW, 14 ICE_SID_XLT1_SW, 15 ICE_SID_XLT2_SW, 16 ICE_SID_PROFID_TCAM_SW, 17 ICE_SID_PROFID_REDIR_SW, 18 ICE_SID_FLD_VEC_SW, 19 ICE_SID_CDID_KEY_BUILDER_SW, 20 ICE_SID_CDID_REDIR_SW 21 }, 22 23 /* ACL */ 24 { 25 ICE_SID_XLT0_ACL, 26 ICE_SID_XLT_KEY_BUILDER_ACL, 27 ICE_SID_XLT1_ACL, 28 ICE_SID_XLT2_ACL, 29 ICE_SID_PROFID_TCAM_ACL, 30 ICE_SID_PROFID_REDIR_ACL, 31 ICE_SID_FLD_VEC_ACL, 32 ICE_SID_CDID_KEY_BUILDER_ACL, 33 ICE_SID_CDID_REDIR_ACL 34 }, 35 36 /* FD */ 37 { 38 ICE_SID_XLT0_FD, 39 ICE_SID_XLT_KEY_BUILDER_FD, 40 ICE_SID_XLT1_FD, 41 ICE_SID_XLT2_FD, 42 ICE_SID_PROFID_TCAM_FD, 43 ICE_SID_PROFID_REDIR_FD, 44 ICE_SID_FLD_VEC_FD, 45 ICE_SID_CDID_KEY_BUILDER_FD, 46 ICE_SID_CDID_REDIR_FD 47 }, 48 49 /* RSS */ 50 { 51 ICE_SID_XLT0_RSS, 52 ICE_SID_XLT_KEY_BUILDER_RSS, 53 ICE_SID_XLT1_RSS, 54 ICE_SID_XLT2_RSS, 55 ICE_SID_PROFID_TCAM_RSS, 56 ICE_SID_PROFID_REDIR_RSS, 57 ICE_SID_FLD_VEC_RSS, 58 ICE_SID_CDID_KEY_BUILDER_RSS, 59 ICE_SID_CDID_REDIR_RSS 60 }, 61 62 /* PE */ 63 { 64 ICE_SID_XLT0_PE, 65 ICE_SID_XLT_KEY_BUILDER_PE, 66 ICE_SID_XLT1_PE, 67 ICE_SID_XLT2_PE, 68 ICE_SID_PROFID_TCAM_PE, 69 ICE_SID_PROFID_REDIR_PE, 70 ICE_SID_FLD_VEC_PE, 71 ICE_SID_CDID_KEY_BUILDER_PE, 72 ICE_SID_CDID_REDIR_PE 73 } 74 }; 75 76 /** 77 * ice_sect_id - returns section ID 78 * @blk: block type 79 * @sect: section type 80 * 81 * This helper function returns the proper section ID given a block type and a 82 * section type. 83 */ 84 static u32 ice_sect_id(enum ice_block blk, enum ice_sect sect) 85 { 86 return ice_sect_lkup[blk][sect]; 87 } 88 89 /** 90 * ice_hw_ptype_ena - check if the PTYPE is enabled or not 91 * @hw: pointer to the HW structure 92 * @ptype: the hardware PTYPE 93 */ 94 bool ice_hw_ptype_ena(struct ice_hw *hw, u16 ptype) 95 { 96 return ptype < ICE_FLOW_PTYPE_MAX && 97 test_bit(ptype, hw->hw_ptype); 98 } 99 100 /* Key creation */ 101 102 #define ICE_DC_KEY 0x1 /* don't care */ 103 #define ICE_DC_KEYINV 0x1 104 #define ICE_NM_KEY 0x0 /* never match */ 105 #define ICE_NM_KEYINV 0x0 106 #define ICE_0_KEY 0x1 /* match 0 */ 107 #define ICE_0_KEYINV 0x0 108 #define ICE_1_KEY 0x0 /* match 1 */ 109 #define ICE_1_KEYINV 0x1 110 111 /** 112 * ice_gen_key_word - generate 16-bits of a key/mask word 113 * @val: the value 114 * @valid: valid bits mask (change only the valid bits) 115 * @dont_care: don't care mask 116 * @nvr_mtch: never match mask 117 * @key: pointer to an array of where the resulting key portion 118 * @key_inv: pointer to an array of where the resulting key invert portion 119 * 120 * This function generates 16-bits from a 8-bit value, an 8-bit don't care mask 121 * and an 8-bit never match mask. The 16-bits of output are divided into 8 bits 122 * of key and 8 bits of key invert. 123 * 124 * '0' = b01, always match a 0 bit 125 * '1' = b10, always match a 1 bit 126 * '?' = b11, don't care bit (always matches) 127 * '~' = b00, never match bit 128 * 129 * Input: 130 * val: b0 1 0 1 0 1 131 * dont_care: b0 0 1 1 0 0 132 * never_mtch: b0 0 0 0 1 1 133 * ------------------------------ 134 * Result: key: b01 10 11 11 00 00 135 */ 136 static int 137 ice_gen_key_word(u8 val, u8 valid, u8 dont_care, u8 nvr_mtch, u8 *key, 138 u8 *key_inv) 139 { 140 u8 in_key = *key, in_key_inv = *key_inv; 141 u8 i; 142 143 /* 'dont_care' and 'nvr_mtch' masks cannot overlap */ 144 if ((dont_care ^ nvr_mtch) != (dont_care | nvr_mtch)) 145 return -EIO; 146 147 *key = 0; 148 *key_inv = 0; 149 150 /* encode the 8 bits into 8-bit key and 8-bit key invert */ 151 for (i = 0; i < 8; i++) { 152 *key >>= 1; 153 *key_inv >>= 1; 154 155 if (!(valid & 0x1)) { /* change only valid bits */ 156 *key |= (in_key & 0x1) << 7; 157 *key_inv |= (in_key_inv & 0x1) << 7; 158 } else if (dont_care & 0x1) { /* don't care bit */ 159 *key |= ICE_DC_KEY << 7; 160 *key_inv |= ICE_DC_KEYINV << 7; 161 } else if (nvr_mtch & 0x1) { /* never match bit */ 162 *key |= ICE_NM_KEY << 7; 163 *key_inv |= ICE_NM_KEYINV << 7; 164 } else if (val & 0x01) { /* exact 1 match */ 165 *key |= ICE_1_KEY << 7; 166 *key_inv |= ICE_1_KEYINV << 7; 167 } else { /* exact 0 match */ 168 *key |= ICE_0_KEY << 7; 169 *key_inv |= ICE_0_KEYINV << 7; 170 } 171 172 dont_care >>= 1; 173 nvr_mtch >>= 1; 174 valid >>= 1; 175 val >>= 1; 176 in_key >>= 1; 177 in_key_inv >>= 1; 178 } 179 180 return 0; 181 } 182 183 /** 184 * ice_bits_max_set - determine if the number of bits set is within a maximum 185 * @mask: pointer to the byte array which is the mask 186 * @size: the number of bytes in the mask 187 * @max: the max number of set bits 188 * 189 * This function determines if there are at most 'max' number of bits set in an 190 * array. Returns true if the number for bits set is <= max or will return false 191 * otherwise. 192 */ 193 static bool ice_bits_max_set(const u8 *mask, u16 size, u16 max) 194 { 195 u16 count = 0; 196 u16 i; 197 198 /* check each byte */ 199 for (i = 0; i < size; i++) { 200 /* if 0, go to next byte */ 201 if (!mask[i]) 202 continue; 203 204 /* We know there is at least one set bit in this byte because of 205 * the above check; if we already have found 'max' number of 206 * bits set, then we can return failure now. 207 */ 208 if (count == max) 209 return false; 210 211 /* count the bits in this byte, checking threshold */ 212 count += hweight8(mask[i]); 213 if (count > max) 214 return false; 215 } 216 217 return true; 218 } 219 220 /** 221 * ice_set_key - generate a variable sized key with multiples of 16-bits 222 * @key: pointer to where the key will be stored 223 * @size: the size of the complete key in bytes (must be even) 224 * @val: array of 8-bit values that makes up the value portion of the key 225 * @upd: array of 8-bit masks that determine what key portion to update 226 * @dc: array of 8-bit masks that make up the don't care mask 227 * @nm: array of 8-bit masks that make up the never match mask 228 * @off: the offset of the first byte in the key to update 229 * @len: the number of bytes in the key update 230 * 231 * This function generates a key from a value, a don't care mask and a never 232 * match mask. 233 * upd, dc, and nm are optional parameters, and can be NULL: 234 * upd == NULL --> upd mask is all 1's (update all bits) 235 * dc == NULL --> dc mask is all 0's (no don't care bits) 236 * nm == NULL --> nm mask is all 0's (no never match bits) 237 */ 238 static int 239 ice_set_key(u8 *key, u16 size, u8 *val, u8 *upd, u8 *dc, u8 *nm, u16 off, 240 u16 len) 241 { 242 u16 half_size; 243 u16 i; 244 245 /* size must be a multiple of 2 bytes. */ 246 if (size % 2) 247 return -EIO; 248 249 half_size = size / 2; 250 if (off + len > half_size) 251 return -EIO; 252 253 /* Make sure at most one bit is set in the never match mask. Having more 254 * than one never match mask bit set will cause HW to consume excessive 255 * power otherwise; this is a power management efficiency check. 256 */ 257 #define ICE_NVR_MTCH_BITS_MAX 1 258 if (nm && !ice_bits_max_set(nm, len, ICE_NVR_MTCH_BITS_MAX)) 259 return -EIO; 260 261 for (i = 0; i < len; i++) 262 if (ice_gen_key_word(val[i], upd ? upd[i] : 0xff, 263 dc ? dc[i] : 0, nm ? nm[i] : 0, 264 key + off + i, key + half_size + off + i)) 265 return -EIO; 266 267 return 0; 268 } 269 270 /** 271 * ice_acquire_change_lock 272 * @hw: pointer to the HW structure 273 * @access: access type (read or write) 274 * 275 * This function will request ownership of the change lock. 276 */ 277 int 278 ice_acquire_change_lock(struct ice_hw *hw, enum ice_aq_res_access_type access) 279 { 280 return ice_acquire_res(hw, ICE_CHANGE_LOCK_RES_ID, access, 281 ICE_CHANGE_LOCK_TIMEOUT); 282 } 283 284 /** 285 * ice_release_change_lock 286 * @hw: pointer to the HW structure 287 * 288 * This function will release the change lock using the proper Admin Command. 289 */ 290 void ice_release_change_lock(struct ice_hw *hw) 291 { 292 ice_release_res(hw, ICE_CHANGE_LOCK_RES_ID); 293 } 294 295 /** 296 * ice_get_open_tunnel_port - retrieve an open tunnel port 297 * @hw: pointer to the HW structure 298 * @port: returns open port 299 * @type: type of tunnel, can be TNL_LAST if it doesn't matter 300 */ 301 bool 302 ice_get_open_tunnel_port(struct ice_hw *hw, u16 *port, 303 enum ice_tunnel_type type) 304 { 305 bool res = false; 306 u16 i; 307 308 mutex_lock(&hw->tnl_lock); 309 310 for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++) 311 if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].port && 312 (type == TNL_LAST || type == hw->tnl.tbl[i].type)) { 313 *port = hw->tnl.tbl[i].port; 314 res = true; 315 break; 316 } 317 318 mutex_unlock(&hw->tnl_lock); 319 320 return res; 321 } 322 323 /** 324 * ice_upd_dvm_boost_entry 325 * @hw: pointer to the HW structure 326 * @entry: pointer to double vlan boost entry info 327 */ 328 static int 329 ice_upd_dvm_boost_entry(struct ice_hw *hw, struct ice_dvm_entry *entry) 330 { 331 struct ice_boost_tcam_section *sect_rx, *sect_tx; 332 int status = -ENOSPC; 333 struct ice_buf_build *bld; 334 u8 val, dc, nm; 335 336 bld = ice_pkg_buf_alloc(hw); 337 if (!bld) 338 return -ENOMEM; 339 340 /* allocate 2 sections, one for Rx parser, one for Tx parser */ 341 if (ice_pkg_buf_reserve_section(bld, 2)) 342 goto ice_upd_dvm_boost_entry_err; 343 344 sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM, 345 struct_size(sect_rx, tcam, 1)); 346 if (!sect_rx) 347 goto ice_upd_dvm_boost_entry_err; 348 sect_rx->count = cpu_to_le16(1); 349 350 sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM, 351 struct_size(sect_tx, tcam, 1)); 352 if (!sect_tx) 353 goto ice_upd_dvm_boost_entry_err; 354 sect_tx->count = cpu_to_le16(1); 355 356 /* copy original boost entry to update package buffer */ 357 memcpy(sect_rx->tcam, entry->boost_entry, sizeof(*sect_rx->tcam)); 358 359 /* re-write the don't care and never match bits accordingly */ 360 if (entry->enable) { 361 /* all bits are don't care */ 362 val = 0x00; 363 dc = 0xFF; 364 nm = 0x00; 365 } else { 366 /* disable, one never match bit, the rest are don't care */ 367 val = 0x00; 368 dc = 0xF7; 369 nm = 0x08; 370 } 371 372 ice_set_key((u8 *)§_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key), 373 &val, NULL, &dc, &nm, 0, sizeof(u8)); 374 375 /* exact copy of entry to Tx section entry */ 376 memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam)); 377 378 status = ice_update_pkg_no_lock(hw, ice_pkg_buf(bld), 1); 379 380 ice_upd_dvm_boost_entry_err: 381 ice_pkg_buf_free(hw, bld); 382 383 return status; 384 } 385 386 /** 387 * ice_set_dvm_boost_entries 388 * @hw: pointer to the HW structure 389 * 390 * Enable double vlan by updating the appropriate boost tcam entries. 391 */ 392 int ice_set_dvm_boost_entries(struct ice_hw *hw) 393 { 394 u16 i; 395 396 for (i = 0; i < hw->dvm_upd.count; i++) { 397 int status; 398 399 status = ice_upd_dvm_boost_entry(hw, &hw->dvm_upd.tbl[i]); 400 if (status) 401 return status; 402 } 403 404 return 0; 405 } 406 407 /** 408 * ice_tunnel_idx_to_entry - convert linear index to the sparse one 409 * @hw: pointer to the HW structure 410 * @type: type of tunnel 411 * @idx: linear index 412 * 413 * Stack assumes we have 2 linear tables with indexes [0, count_valid), 414 * but really the port table may be sprase, and types are mixed, so convert 415 * the stack index into the device index. 416 */ 417 static u16 ice_tunnel_idx_to_entry(struct ice_hw *hw, enum ice_tunnel_type type, 418 u16 idx) 419 { 420 u16 i; 421 422 for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++) 423 if (hw->tnl.tbl[i].valid && 424 hw->tnl.tbl[i].type == type && 425 idx-- == 0) 426 return i; 427 428 WARN_ON_ONCE(1); 429 return 0; 430 } 431 432 /** 433 * ice_create_tunnel 434 * @hw: pointer to the HW structure 435 * @index: device table entry 436 * @type: type of tunnel 437 * @port: port of tunnel to create 438 * 439 * Create a tunnel by updating the parse graph in the parser. We do that by 440 * creating a package buffer with the tunnel info and issuing an update package 441 * command. 442 */ 443 static int 444 ice_create_tunnel(struct ice_hw *hw, u16 index, 445 enum ice_tunnel_type type, u16 port) 446 { 447 struct ice_boost_tcam_section *sect_rx, *sect_tx; 448 struct ice_buf_build *bld; 449 int status = -ENOSPC; 450 451 mutex_lock(&hw->tnl_lock); 452 453 bld = ice_pkg_buf_alloc(hw); 454 if (!bld) { 455 status = -ENOMEM; 456 goto ice_create_tunnel_end; 457 } 458 459 /* allocate 2 sections, one for Rx parser, one for Tx parser */ 460 if (ice_pkg_buf_reserve_section(bld, 2)) 461 goto ice_create_tunnel_err; 462 463 sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM, 464 struct_size(sect_rx, tcam, 1)); 465 if (!sect_rx) 466 goto ice_create_tunnel_err; 467 sect_rx->count = cpu_to_le16(1); 468 469 sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM, 470 struct_size(sect_tx, tcam, 1)); 471 if (!sect_tx) 472 goto ice_create_tunnel_err; 473 sect_tx->count = cpu_to_le16(1); 474 475 /* copy original boost entry to update package buffer */ 476 memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry, 477 sizeof(*sect_rx->tcam)); 478 479 /* over-write the never-match dest port key bits with the encoded port 480 * bits 481 */ 482 ice_set_key((u8 *)§_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key), 483 (u8 *)&port, NULL, NULL, NULL, 484 (u16)offsetof(struct ice_boost_key_value, hv_dst_port_key), 485 sizeof(sect_rx->tcam[0].key.key.hv_dst_port_key)); 486 487 /* exact copy of entry to Tx section entry */ 488 memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam)); 489 490 status = ice_update_pkg(hw, ice_pkg_buf(bld), 1); 491 if (!status) 492 hw->tnl.tbl[index].port = port; 493 494 ice_create_tunnel_err: 495 ice_pkg_buf_free(hw, bld); 496 497 ice_create_tunnel_end: 498 mutex_unlock(&hw->tnl_lock); 499 500 return status; 501 } 502 503 /** 504 * ice_destroy_tunnel 505 * @hw: pointer to the HW structure 506 * @index: device table entry 507 * @type: type of tunnel 508 * @port: port of tunnel to destroy (ignored if the all parameter is true) 509 * 510 * Destroys a tunnel or all tunnels by creating an update package buffer 511 * targeting the specific updates requested and then performing an update 512 * package. 513 */ 514 static int 515 ice_destroy_tunnel(struct ice_hw *hw, u16 index, enum ice_tunnel_type type, 516 u16 port) 517 { 518 struct ice_boost_tcam_section *sect_rx, *sect_tx; 519 struct ice_buf_build *bld; 520 int status = -ENOSPC; 521 522 mutex_lock(&hw->tnl_lock); 523 524 if (WARN_ON(!hw->tnl.tbl[index].valid || 525 hw->tnl.tbl[index].type != type || 526 hw->tnl.tbl[index].port != port)) { 527 status = -EIO; 528 goto ice_destroy_tunnel_end; 529 } 530 531 bld = ice_pkg_buf_alloc(hw); 532 if (!bld) { 533 status = -ENOMEM; 534 goto ice_destroy_tunnel_end; 535 } 536 537 /* allocate 2 sections, one for Rx parser, one for Tx parser */ 538 if (ice_pkg_buf_reserve_section(bld, 2)) 539 goto ice_destroy_tunnel_err; 540 541 sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM, 542 struct_size(sect_rx, tcam, 1)); 543 if (!sect_rx) 544 goto ice_destroy_tunnel_err; 545 sect_rx->count = cpu_to_le16(1); 546 547 sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM, 548 struct_size(sect_tx, tcam, 1)); 549 if (!sect_tx) 550 goto ice_destroy_tunnel_err; 551 sect_tx->count = cpu_to_le16(1); 552 553 /* copy original boost entry to update package buffer, one copy to Rx 554 * section, another copy to the Tx section 555 */ 556 memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry, 557 sizeof(*sect_rx->tcam)); 558 memcpy(sect_tx->tcam, hw->tnl.tbl[index].boost_entry, 559 sizeof(*sect_tx->tcam)); 560 561 status = ice_update_pkg(hw, ice_pkg_buf(bld), 1); 562 if (!status) 563 hw->tnl.tbl[index].port = 0; 564 565 ice_destroy_tunnel_err: 566 ice_pkg_buf_free(hw, bld); 567 568 ice_destroy_tunnel_end: 569 mutex_unlock(&hw->tnl_lock); 570 571 return status; 572 } 573 574 int ice_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 575 unsigned int idx, struct udp_tunnel_info *ti) 576 { 577 struct ice_pf *pf = ice_netdev_to_pf(netdev); 578 enum ice_tunnel_type tnl_type; 579 int status; 580 u16 index; 581 582 tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE; 583 index = ice_tunnel_idx_to_entry(&pf->hw, tnl_type, idx); 584 585 status = ice_create_tunnel(&pf->hw, index, tnl_type, ntohs(ti->port)); 586 if (status) { 587 netdev_err(netdev, "Error adding UDP tunnel - %d\n", 588 status); 589 return -EIO; 590 } 591 592 udp_tunnel_nic_set_port_priv(netdev, table, idx, index); 593 return 0; 594 } 595 596 int ice_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 597 unsigned int idx, struct udp_tunnel_info *ti) 598 { 599 struct ice_pf *pf = ice_netdev_to_pf(netdev); 600 enum ice_tunnel_type tnl_type; 601 int status; 602 603 tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE; 604 605 status = ice_destroy_tunnel(&pf->hw, ti->hw_priv, tnl_type, 606 ntohs(ti->port)); 607 if (status) { 608 netdev_err(netdev, "Error removing UDP tunnel - %d\n", 609 status); 610 return -EIO; 611 } 612 613 return 0; 614 } 615 616 /** 617 * ice_find_prot_off - find prot ID and offset pair, based on prof and FV index 618 * @hw: pointer to the hardware structure 619 * @blk: hardware block 620 * @prof: profile ID 621 * @fv_idx: field vector word index 622 * @prot: variable to receive the protocol ID 623 * @off: variable to receive the protocol offset 624 */ 625 int 626 ice_find_prot_off(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 fv_idx, 627 u8 *prot, u16 *off) 628 { 629 struct ice_fv_word *fv_ext; 630 631 if (prof >= hw->blk[blk].es.count) 632 return -EINVAL; 633 634 if (fv_idx >= hw->blk[blk].es.fvw) 635 return -EINVAL; 636 637 fv_ext = hw->blk[blk].es.t + (prof * hw->blk[blk].es.fvw); 638 639 *prot = fv_ext[fv_idx].prot_id; 640 *off = fv_ext[fv_idx].off; 641 642 return 0; 643 } 644 645 /* PTG Management */ 646 647 /** 648 * ice_ptg_find_ptype - Search for packet type group using packet type (ptype) 649 * @hw: pointer to the hardware structure 650 * @blk: HW block 651 * @ptype: the ptype to search for 652 * @ptg: pointer to variable that receives the PTG 653 * 654 * This function will search the PTGs for a particular ptype, returning the 655 * PTG ID that contains it through the PTG parameter, with the value of 656 * ICE_DEFAULT_PTG (0) meaning it is part the default PTG. 657 */ 658 static int 659 ice_ptg_find_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 *ptg) 660 { 661 if (ptype >= ICE_XLT1_CNT || !ptg) 662 return -EINVAL; 663 664 *ptg = hw->blk[blk].xlt1.ptypes[ptype].ptg; 665 return 0; 666 } 667 668 /** 669 * ice_ptg_alloc_val - Allocates a new packet type group ID by value 670 * @hw: pointer to the hardware structure 671 * @blk: HW block 672 * @ptg: the PTG to allocate 673 * 674 * This function allocates a given packet type group ID specified by the PTG 675 * parameter. 676 */ 677 static void ice_ptg_alloc_val(struct ice_hw *hw, enum ice_block blk, u8 ptg) 678 { 679 hw->blk[blk].xlt1.ptg_tbl[ptg].in_use = true; 680 } 681 682 /** 683 * ice_ptg_remove_ptype - Removes ptype from a particular packet type group 684 * @hw: pointer to the hardware structure 685 * @blk: HW block 686 * @ptype: the ptype to remove 687 * @ptg: the PTG to remove the ptype from 688 * 689 * This function will remove the ptype from the specific PTG, and move it to 690 * the default PTG (ICE_DEFAULT_PTG). 691 */ 692 static int 693 ice_ptg_remove_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg) 694 { 695 struct ice_ptg_ptype **ch; 696 struct ice_ptg_ptype *p; 697 698 if (ptype > ICE_XLT1_CNT - 1) 699 return -EINVAL; 700 701 if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use) 702 return -ENOENT; 703 704 /* Should not happen if .in_use is set, bad config */ 705 if (!hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype) 706 return -EIO; 707 708 /* find the ptype within this PTG, and bypass the link over it */ 709 p = hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype; 710 ch = &hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype; 711 while (p) { 712 if (ptype == (p - hw->blk[blk].xlt1.ptypes)) { 713 *ch = p->next_ptype; 714 break; 715 } 716 717 ch = &p->next_ptype; 718 p = p->next_ptype; 719 } 720 721 hw->blk[blk].xlt1.ptypes[ptype].ptg = ICE_DEFAULT_PTG; 722 hw->blk[blk].xlt1.ptypes[ptype].next_ptype = NULL; 723 724 return 0; 725 } 726 727 /** 728 * ice_ptg_add_mv_ptype - Adds/moves ptype to a particular packet type group 729 * @hw: pointer to the hardware structure 730 * @blk: HW block 731 * @ptype: the ptype to add or move 732 * @ptg: the PTG to add or move the ptype to 733 * 734 * This function will either add or move a ptype to a particular PTG depending 735 * on if the ptype is already part of another group. Note that using a 736 * destination PTG ID of ICE_DEFAULT_PTG (0) will move the ptype to the 737 * default PTG. 738 */ 739 static int 740 ice_ptg_add_mv_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg) 741 { 742 u8 original_ptg; 743 int status; 744 745 if (ptype > ICE_XLT1_CNT - 1) 746 return -EINVAL; 747 748 if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use && ptg != ICE_DEFAULT_PTG) 749 return -ENOENT; 750 751 status = ice_ptg_find_ptype(hw, blk, ptype, &original_ptg); 752 if (status) 753 return status; 754 755 /* Is ptype already in the correct PTG? */ 756 if (original_ptg == ptg) 757 return 0; 758 759 /* Remove from original PTG and move back to the default PTG */ 760 if (original_ptg != ICE_DEFAULT_PTG) 761 ice_ptg_remove_ptype(hw, blk, ptype, original_ptg); 762 763 /* Moving to default PTG? Then we're done with this request */ 764 if (ptg == ICE_DEFAULT_PTG) 765 return 0; 766 767 /* Add ptype to PTG at beginning of list */ 768 hw->blk[blk].xlt1.ptypes[ptype].next_ptype = 769 hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype; 770 hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype = 771 &hw->blk[blk].xlt1.ptypes[ptype]; 772 773 hw->blk[blk].xlt1.ptypes[ptype].ptg = ptg; 774 hw->blk[blk].xlt1.t[ptype] = ptg; 775 776 return 0; 777 } 778 779 /* Block / table size info */ 780 struct ice_blk_size_details { 781 u16 xlt1; /* # XLT1 entries */ 782 u16 xlt2; /* # XLT2 entries */ 783 u16 prof_tcam; /* # profile ID TCAM entries */ 784 u16 prof_id; /* # profile IDs */ 785 u8 prof_cdid_bits; /* # CDID one-hot bits used in key */ 786 u16 prof_redir; /* # profile redirection entries */ 787 u16 es; /* # extraction sequence entries */ 788 u16 fvw; /* # field vector words */ 789 u8 overwrite; /* overwrite existing entries allowed */ 790 u8 reverse; /* reverse FV order */ 791 }; 792 793 static const struct ice_blk_size_details blk_sizes[ICE_BLK_COUNT] = { 794 /** 795 * Table Definitions 796 * XLT1 - Number of entries in XLT1 table 797 * XLT2 - Number of entries in XLT2 table 798 * TCAM - Number of entries Profile ID TCAM table 799 * CDID - Control Domain ID of the hardware block 800 * PRED - Number of entries in the Profile Redirection Table 801 * FV - Number of entries in the Field Vector 802 * FVW - Width (in WORDs) of the Field Vector 803 * OVR - Overwrite existing table entries 804 * REV - Reverse FV 805 */ 806 /* XLT1 , XLT2 ,TCAM, PID,CDID,PRED, FV, FVW */ 807 /* Overwrite , Reverse FV */ 808 /* SW */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 256, 0, 256, 256, 48, 809 false, false }, 810 /* ACL */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128, 0, 128, 128, 32, 811 false, false }, 812 /* FD */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128, 0, 128, 128, 24, 813 false, true }, 814 /* RSS */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128, 0, 128, 128, 24, 815 true, true }, 816 /* PE */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 64, 32, 0, 32, 32, 24, 817 false, false }, 818 }; 819 820 enum ice_sid_all { 821 ICE_SID_XLT1_OFF = 0, 822 ICE_SID_XLT2_OFF, 823 ICE_SID_PR_OFF, 824 ICE_SID_PR_REDIR_OFF, 825 ICE_SID_ES_OFF, 826 ICE_SID_OFF_COUNT, 827 }; 828 829 /* Characteristic handling */ 830 831 /** 832 * ice_match_prop_lst - determine if properties of two lists match 833 * @list1: first properties list 834 * @list2: second properties list 835 * 836 * Count, cookies and the order must match in order to be considered equivalent. 837 */ 838 static bool 839 ice_match_prop_lst(struct list_head *list1, struct list_head *list2) 840 { 841 struct ice_vsig_prof *tmp1; 842 struct ice_vsig_prof *tmp2; 843 u16 chk_count = 0; 844 u16 count = 0; 845 846 /* compare counts */ 847 list_for_each_entry(tmp1, list1, list) 848 count++; 849 list_for_each_entry(tmp2, list2, list) 850 chk_count++; 851 if (!count || count != chk_count) 852 return false; 853 854 tmp1 = list_first_entry(list1, struct ice_vsig_prof, list); 855 tmp2 = list_first_entry(list2, struct ice_vsig_prof, list); 856 857 /* profile cookies must compare, and in the exact same order to take 858 * into account priority 859 */ 860 while (count--) { 861 if (tmp2->profile_cookie != tmp1->profile_cookie) 862 return false; 863 864 tmp1 = list_next_entry(tmp1, list); 865 tmp2 = list_next_entry(tmp2, list); 866 } 867 868 return true; 869 } 870 871 /* VSIG Management */ 872 873 /** 874 * ice_vsig_find_vsi - find a VSIG that contains a specified VSI 875 * @hw: pointer to the hardware structure 876 * @blk: HW block 877 * @vsi: VSI of interest 878 * @vsig: pointer to receive the VSI group 879 * 880 * This function will lookup the VSI entry in the XLT2 list and return 881 * the VSI group its associated with. 882 */ 883 static int 884 ice_vsig_find_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 *vsig) 885 { 886 if (!vsig || vsi >= ICE_MAX_VSI) 887 return -EINVAL; 888 889 /* As long as there's a default or valid VSIG associated with the input 890 * VSI, the functions returns a success. Any handling of VSIG will be 891 * done by the following add, update or remove functions. 892 */ 893 *vsig = hw->blk[blk].xlt2.vsis[vsi].vsig; 894 895 return 0; 896 } 897 898 /** 899 * ice_vsig_alloc_val - allocate a new VSIG by value 900 * @hw: pointer to the hardware structure 901 * @blk: HW block 902 * @vsig: the VSIG to allocate 903 * 904 * This function will allocate a given VSIG specified by the VSIG parameter. 905 */ 906 static u16 ice_vsig_alloc_val(struct ice_hw *hw, enum ice_block blk, u16 vsig) 907 { 908 u16 idx = vsig & ICE_VSIG_IDX_M; 909 910 if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) { 911 INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst); 912 hw->blk[blk].xlt2.vsig_tbl[idx].in_use = true; 913 } 914 915 return ICE_VSIG_VALUE(idx, hw->pf_id); 916 } 917 918 /** 919 * ice_vsig_alloc - Finds a free entry and allocates a new VSIG 920 * @hw: pointer to the hardware structure 921 * @blk: HW block 922 * 923 * This function will iterate through the VSIG list and mark the first 924 * unused entry for the new VSIG entry as used and return that value. 925 */ 926 static u16 ice_vsig_alloc(struct ice_hw *hw, enum ice_block blk) 927 { 928 u16 i; 929 930 for (i = 1; i < ICE_MAX_VSIGS; i++) 931 if (!hw->blk[blk].xlt2.vsig_tbl[i].in_use) 932 return ice_vsig_alloc_val(hw, blk, i); 933 934 return ICE_DEFAULT_VSIG; 935 } 936 937 /** 938 * ice_find_dup_props_vsig - find VSI group with a specified set of properties 939 * @hw: pointer to the hardware structure 940 * @blk: HW block 941 * @chs: characteristic list 942 * @vsig: returns the VSIG with the matching profiles, if found 943 * 944 * Each VSIG is associated with a characteristic set; i.e. all VSIs under 945 * a group have the same characteristic set. To check if there exists a VSIG 946 * which has the same characteristics as the input characteristics; this 947 * function will iterate through the XLT2 list and return the VSIG that has a 948 * matching configuration. In order to make sure that priorities are accounted 949 * for, the list must match exactly, including the order in which the 950 * characteristics are listed. 951 */ 952 static int 953 ice_find_dup_props_vsig(struct ice_hw *hw, enum ice_block blk, 954 struct list_head *chs, u16 *vsig) 955 { 956 struct ice_xlt2 *xlt2 = &hw->blk[blk].xlt2; 957 u16 i; 958 959 for (i = 0; i < xlt2->count; i++) 960 if (xlt2->vsig_tbl[i].in_use && 961 ice_match_prop_lst(chs, &xlt2->vsig_tbl[i].prop_lst)) { 962 *vsig = ICE_VSIG_VALUE(i, hw->pf_id); 963 return 0; 964 } 965 966 return -ENOENT; 967 } 968 969 /** 970 * ice_vsig_free - free VSI group 971 * @hw: pointer to the hardware structure 972 * @blk: HW block 973 * @vsig: VSIG to remove 974 * 975 * The function will remove all VSIs associated with the input VSIG and move 976 * them to the DEFAULT_VSIG and mark the VSIG available. 977 */ 978 static int ice_vsig_free(struct ice_hw *hw, enum ice_block blk, u16 vsig) 979 { 980 struct ice_vsig_prof *dtmp, *del; 981 struct ice_vsig_vsi *vsi_cur; 982 u16 idx; 983 984 idx = vsig & ICE_VSIG_IDX_M; 985 if (idx >= ICE_MAX_VSIGS) 986 return -EINVAL; 987 988 if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) 989 return -ENOENT; 990 991 hw->blk[blk].xlt2.vsig_tbl[idx].in_use = false; 992 993 vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi; 994 /* If the VSIG has at least 1 VSI then iterate through the 995 * list and remove the VSIs before deleting the group. 996 */ 997 if (vsi_cur) { 998 /* remove all vsis associated with this VSIG XLT2 entry */ 999 do { 1000 struct ice_vsig_vsi *tmp = vsi_cur->next_vsi; 1001 1002 vsi_cur->vsig = ICE_DEFAULT_VSIG; 1003 vsi_cur->changed = 1; 1004 vsi_cur->next_vsi = NULL; 1005 vsi_cur = tmp; 1006 } while (vsi_cur); 1007 1008 /* NULL terminate head of VSI list */ 1009 hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi = NULL; 1010 } 1011 1012 /* free characteristic list */ 1013 list_for_each_entry_safe(del, dtmp, 1014 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst, 1015 list) { 1016 list_del(&del->list); 1017 devm_kfree(ice_hw_to_dev(hw), del); 1018 } 1019 1020 /* if VSIG characteristic list was cleared for reset 1021 * re-initialize the list head 1022 */ 1023 INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst); 1024 1025 return 0; 1026 } 1027 1028 /** 1029 * ice_vsig_remove_vsi - remove VSI from VSIG 1030 * @hw: pointer to the hardware structure 1031 * @blk: HW block 1032 * @vsi: VSI to remove 1033 * @vsig: VSI group to remove from 1034 * 1035 * The function will remove the input VSI from its VSI group and move it 1036 * to the DEFAULT_VSIG. 1037 */ 1038 static int 1039 ice_vsig_remove_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig) 1040 { 1041 struct ice_vsig_vsi **vsi_head, *vsi_cur, *vsi_tgt; 1042 u16 idx; 1043 1044 idx = vsig & ICE_VSIG_IDX_M; 1045 1046 if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS) 1047 return -EINVAL; 1048 1049 if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) 1050 return -ENOENT; 1051 1052 /* entry already in default VSIG, don't have to remove */ 1053 if (idx == ICE_DEFAULT_VSIG) 1054 return 0; 1055 1056 vsi_head = &hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi; 1057 if (!(*vsi_head)) 1058 return -EIO; 1059 1060 vsi_tgt = &hw->blk[blk].xlt2.vsis[vsi]; 1061 vsi_cur = (*vsi_head); 1062 1063 /* iterate the VSI list, skip over the entry to be removed */ 1064 while (vsi_cur) { 1065 if (vsi_tgt == vsi_cur) { 1066 (*vsi_head) = vsi_cur->next_vsi; 1067 break; 1068 } 1069 vsi_head = &vsi_cur->next_vsi; 1070 vsi_cur = vsi_cur->next_vsi; 1071 } 1072 1073 /* verify if VSI was removed from group list */ 1074 if (!vsi_cur) 1075 return -ENOENT; 1076 1077 vsi_cur->vsig = ICE_DEFAULT_VSIG; 1078 vsi_cur->changed = 1; 1079 vsi_cur->next_vsi = NULL; 1080 1081 return 0; 1082 } 1083 1084 /** 1085 * ice_vsig_add_mv_vsi - add or move a VSI to a VSI group 1086 * @hw: pointer to the hardware structure 1087 * @blk: HW block 1088 * @vsi: VSI to move 1089 * @vsig: destination VSI group 1090 * 1091 * This function will move or add the input VSI to the target VSIG. 1092 * The function will find the original VSIG the VSI belongs to and 1093 * move the entry to the DEFAULT_VSIG, update the original VSIG and 1094 * then move entry to the new VSIG. 1095 */ 1096 static int 1097 ice_vsig_add_mv_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig) 1098 { 1099 struct ice_vsig_vsi *tmp; 1100 u16 orig_vsig, idx; 1101 int status; 1102 1103 idx = vsig & ICE_VSIG_IDX_M; 1104 1105 if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS) 1106 return -EINVAL; 1107 1108 /* if VSIG not in use and VSIG is not default type this VSIG 1109 * doesn't exist. 1110 */ 1111 if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use && 1112 vsig != ICE_DEFAULT_VSIG) 1113 return -ENOENT; 1114 1115 status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig); 1116 if (status) 1117 return status; 1118 1119 /* no update required if vsigs match */ 1120 if (orig_vsig == vsig) 1121 return 0; 1122 1123 if (orig_vsig != ICE_DEFAULT_VSIG) { 1124 /* remove entry from orig_vsig and add to default VSIG */ 1125 status = ice_vsig_remove_vsi(hw, blk, vsi, orig_vsig); 1126 if (status) 1127 return status; 1128 } 1129 1130 if (idx == ICE_DEFAULT_VSIG) 1131 return 0; 1132 1133 /* Create VSI entry and add VSIG and prop_mask values */ 1134 hw->blk[blk].xlt2.vsis[vsi].vsig = vsig; 1135 hw->blk[blk].xlt2.vsis[vsi].changed = 1; 1136 1137 /* Add new entry to the head of the VSIG list */ 1138 tmp = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi; 1139 hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi = 1140 &hw->blk[blk].xlt2.vsis[vsi]; 1141 hw->blk[blk].xlt2.vsis[vsi].next_vsi = tmp; 1142 hw->blk[blk].xlt2.t[vsi] = vsig; 1143 1144 return 0; 1145 } 1146 1147 /** 1148 * ice_prof_has_mask_idx - determine if profile index masking is identical 1149 * @hw: pointer to the hardware structure 1150 * @blk: HW block 1151 * @prof: profile to check 1152 * @idx: profile index to check 1153 * @mask: mask to match 1154 */ 1155 static bool 1156 ice_prof_has_mask_idx(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 idx, 1157 u16 mask) 1158 { 1159 bool expect_no_mask = false; 1160 bool found = false; 1161 bool match = false; 1162 u16 i; 1163 1164 /* If mask is 0x0000 or 0xffff, then there is no masking */ 1165 if (mask == 0 || mask == 0xffff) 1166 expect_no_mask = true; 1167 1168 /* Scan the enabled masks on this profile, for the specified idx */ 1169 for (i = hw->blk[blk].masks.first; i < hw->blk[blk].masks.first + 1170 hw->blk[blk].masks.count; i++) 1171 if (hw->blk[blk].es.mask_ena[prof] & BIT(i)) 1172 if (hw->blk[blk].masks.masks[i].in_use && 1173 hw->blk[blk].masks.masks[i].idx == idx) { 1174 found = true; 1175 if (hw->blk[blk].masks.masks[i].mask == mask) 1176 match = true; 1177 break; 1178 } 1179 1180 if (expect_no_mask) { 1181 if (found) 1182 return false; 1183 } else { 1184 if (!match) 1185 return false; 1186 } 1187 1188 return true; 1189 } 1190 1191 /** 1192 * ice_prof_has_mask - determine if profile masking is identical 1193 * @hw: pointer to the hardware structure 1194 * @blk: HW block 1195 * @prof: profile to check 1196 * @masks: masks to match 1197 */ 1198 static bool 1199 ice_prof_has_mask(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 *masks) 1200 { 1201 u16 i; 1202 1203 /* es->mask_ena[prof] will have the mask */ 1204 for (i = 0; i < hw->blk[blk].es.fvw; i++) 1205 if (!ice_prof_has_mask_idx(hw, blk, prof, i, masks[i])) 1206 return false; 1207 1208 return true; 1209 } 1210 1211 /** 1212 * ice_find_prof_id_with_mask - find profile ID for a given field vector 1213 * @hw: pointer to the hardware structure 1214 * @blk: HW block 1215 * @fv: field vector to search for 1216 * @masks: masks for FV 1217 * @symm: symmetric setting for RSS flows 1218 * @prof_id: receives the profile ID 1219 */ 1220 static int 1221 ice_find_prof_id_with_mask(struct ice_hw *hw, enum ice_block blk, 1222 struct ice_fv_word *fv, u16 *masks, bool symm, 1223 u8 *prof_id) 1224 { 1225 struct ice_es *es = &hw->blk[blk].es; 1226 u8 i; 1227 1228 /* For FD, we don't want to re-use a existed profile with the same 1229 * field vector and mask. This will cause rule interference. 1230 */ 1231 if (blk == ICE_BLK_FD) 1232 return -ENOENT; 1233 1234 for (i = 0; i < (u8)es->count; i++) { 1235 u16 off = i * es->fvw; 1236 1237 if (blk == ICE_BLK_RSS && es->symm[i] != symm) 1238 continue; 1239 1240 if (memcmp(&es->t[off], fv, es->fvw * sizeof(*fv))) 1241 continue; 1242 1243 /* check if masks settings are the same for this profile */ 1244 if (masks && !ice_prof_has_mask(hw, blk, i, masks)) 1245 continue; 1246 1247 *prof_id = i; 1248 return 0; 1249 } 1250 1251 return -ENOENT; 1252 } 1253 1254 /** 1255 * ice_prof_id_rsrc_type - get profile ID resource type for a block type 1256 * @blk: the block type 1257 * @rsrc_type: pointer to variable to receive the resource type 1258 */ 1259 static bool ice_prof_id_rsrc_type(enum ice_block blk, u16 *rsrc_type) 1260 { 1261 switch (blk) { 1262 case ICE_BLK_FD: 1263 *rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_PROFID; 1264 break; 1265 case ICE_BLK_RSS: 1266 *rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_PROFID; 1267 break; 1268 default: 1269 return false; 1270 } 1271 return true; 1272 } 1273 1274 /** 1275 * ice_tcam_ent_rsrc_type - get TCAM entry resource type for a block type 1276 * @blk: the block type 1277 * @rsrc_type: pointer to variable to receive the resource type 1278 */ 1279 static bool ice_tcam_ent_rsrc_type(enum ice_block blk, u16 *rsrc_type) 1280 { 1281 switch (blk) { 1282 case ICE_BLK_FD: 1283 *rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_TCAM; 1284 break; 1285 case ICE_BLK_RSS: 1286 *rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_TCAM; 1287 break; 1288 default: 1289 return false; 1290 } 1291 return true; 1292 } 1293 1294 /** 1295 * ice_alloc_tcam_ent - allocate hardware TCAM entry 1296 * @hw: pointer to the HW struct 1297 * @blk: the block to allocate the TCAM for 1298 * @btm: true to allocate from bottom of table, false to allocate from top 1299 * @tcam_idx: pointer to variable to receive the TCAM entry 1300 * 1301 * This function allocates a new entry in a Profile ID TCAM for a specific 1302 * block. 1303 */ 1304 static int 1305 ice_alloc_tcam_ent(struct ice_hw *hw, enum ice_block blk, bool btm, 1306 u16 *tcam_idx) 1307 { 1308 u16 res_type; 1309 1310 if (!ice_tcam_ent_rsrc_type(blk, &res_type)) 1311 return -EINVAL; 1312 1313 return ice_alloc_hw_res(hw, res_type, 1, btm, tcam_idx); 1314 } 1315 1316 /** 1317 * ice_free_tcam_ent - free hardware TCAM entry 1318 * @hw: pointer to the HW struct 1319 * @blk: the block from which to free the TCAM entry 1320 * @tcam_idx: the TCAM entry to free 1321 * 1322 * This function frees an entry in a Profile ID TCAM for a specific block. 1323 */ 1324 static int 1325 ice_free_tcam_ent(struct ice_hw *hw, enum ice_block blk, u16 tcam_idx) 1326 { 1327 u16 res_type; 1328 1329 if (!ice_tcam_ent_rsrc_type(blk, &res_type)) 1330 return -EINVAL; 1331 1332 return ice_free_hw_res(hw, res_type, 1, &tcam_idx); 1333 } 1334 1335 /** 1336 * ice_alloc_prof_id - allocate profile ID 1337 * @hw: pointer to the HW struct 1338 * @blk: the block to allocate the profile ID for 1339 * @prof_id: pointer to variable to receive the profile ID 1340 * 1341 * This function allocates a new profile ID, which also corresponds to a Field 1342 * Vector (Extraction Sequence) entry. 1343 */ 1344 static int ice_alloc_prof_id(struct ice_hw *hw, enum ice_block blk, u8 *prof_id) 1345 { 1346 u16 res_type; 1347 u16 get_prof; 1348 int status; 1349 1350 if (!ice_prof_id_rsrc_type(blk, &res_type)) 1351 return -EINVAL; 1352 1353 status = ice_alloc_hw_res(hw, res_type, 1, false, &get_prof); 1354 if (!status) 1355 *prof_id = (u8)get_prof; 1356 1357 return status; 1358 } 1359 1360 /** 1361 * ice_free_prof_id - free profile ID 1362 * @hw: pointer to the HW struct 1363 * @blk: the block from which to free the profile ID 1364 * @prof_id: the profile ID to free 1365 * 1366 * This function frees a profile ID, which also corresponds to a Field Vector. 1367 */ 1368 static int ice_free_prof_id(struct ice_hw *hw, enum ice_block blk, u8 prof_id) 1369 { 1370 u16 tmp_prof_id = (u16)prof_id; 1371 u16 res_type; 1372 1373 if (!ice_prof_id_rsrc_type(blk, &res_type)) 1374 return -EINVAL; 1375 1376 return ice_free_hw_res(hw, res_type, 1, &tmp_prof_id); 1377 } 1378 1379 /** 1380 * ice_prof_inc_ref - increment reference count for profile 1381 * @hw: pointer to the HW struct 1382 * @blk: the block from which to free the profile ID 1383 * @prof_id: the profile ID for which to increment the reference count 1384 */ 1385 static int ice_prof_inc_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id) 1386 { 1387 if (prof_id > hw->blk[blk].es.count) 1388 return -EINVAL; 1389 1390 hw->blk[blk].es.ref_count[prof_id]++; 1391 1392 return 0; 1393 } 1394 1395 /** 1396 * ice_write_prof_mask_reg - write profile mask register 1397 * @hw: pointer to the HW struct 1398 * @blk: hardware block 1399 * @mask_idx: mask index 1400 * @idx: index of the FV which will use the mask 1401 * @mask: the 16-bit mask 1402 */ 1403 static void 1404 ice_write_prof_mask_reg(struct ice_hw *hw, enum ice_block blk, u16 mask_idx, 1405 u16 idx, u16 mask) 1406 { 1407 u32 offset; 1408 u32 val; 1409 1410 switch (blk) { 1411 case ICE_BLK_RSS: 1412 offset = GLQF_HMASK(mask_idx); 1413 val = FIELD_PREP(GLQF_HMASK_MSK_INDEX_M, idx); 1414 val |= FIELD_PREP(GLQF_HMASK_MASK_M, mask); 1415 break; 1416 case ICE_BLK_FD: 1417 offset = GLQF_FDMASK(mask_idx); 1418 val = FIELD_PREP(GLQF_FDMASK_MSK_INDEX_M, idx); 1419 val |= FIELD_PREP(GLQF_FDMASK_MASK_M, mask); 1420 break; 1421 default: 1422 ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n", 1423 blk); 1424 return; 1425 } 1426 1427 wr32(hw, offset, val); 1428 ice_debug(hw, ICE_DBG_PKG, "write mask, blk %d (%d): %x = %x\n", 1429 blk, idx, offset, val); 1430 } 1431 1432 /** 1433 * ice_write_prof_mask_enable_res - write profile mask enable register 1434 * @hw: pointer to the HW struct 1435 * @blk: hardware block 1436 * @prof_id: profile ID 1437 * @enable_mask: enable mask 1438 */ 1439 static void 1440 ice_write_prof_mask_enable_res(struct ice_hw *hw, enum ice_block blk, 1441 u16 prof_id, u32 enable_mask) 1442 { 1443 u32 offset; 1444 1445 switch (blk) { 1446 case ICE_BLK_RSS: 1447 offset = GLQF_HMASK_SEL(prof_id); 1448 break; 1449 case ICE_BLK_FD: 1450 offset = GLQF_FDMASK_SEL(prof_id); 1451 break; 1452 default: 1453 ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n", 1454 blk); 1455 return; 1456 } 1457 1458 wr32(hw, offset, enable_mask); 1459 ice_debug(hw, ICE_DBG_PKG, "write mask enable, blk %d (%d): %x = %x\n", 1460 blk, prof_id, offset, enable_mask); 1461 } 1462 1463 /** 1464 * ice_init_prof_masks - initial prof masks 1465 * @hw: pointer to the HW struct 1466 * @blk: hardware block 1467 */ 1468 static void ice_init_prof_masks(struct ice_hw *hw, enum ice_block blk) 1469 { 1470 u16 per_pf; 1471 u16 i; 1472 1473 mutex_init(&hw->blk[blk].masks.lock); 1474 1475 per_pf = ICE_PROF_MASK_COUNT / hw->dev_caps.num_funcs; 1476 1477 hw->blk[blk].masks.count = per_pf; 1478 hw->blk[blk].masks.first = hw->logical_pf_id * per_pf; 1479 1480 memset(hw->blk[blk].masks.masks, 0, sizeof(hw->blk[blk].masks.masks)); 1481 1482 for (i = hw->blk[blk].masks.first; 1483 i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++) 1484 ice_write_prof_mask_reg(hw, blk, i, 0, 0); 1485 } 1486 1487 /** 1488 * ice_init_all_prof_masks - initialize all prof masks 1489 * @hw: pointer to the HW struct 1490 */ 1491 static void ice_init_all_prof_masks(struct ice_hw *hw) 1492 { 1493 ice_init_prof_masks(hw, ICE_BLK_RSS); 1494 ice_init_prof_masks(hw, ICE_BLK_FD); 1495 } 1496 1497 /** 1498 * ice_alloc_prof_mask - allocate profile mask 1499 * @hw: pointer to the HW struct 1500 * @blk: hardware block 1501 * @idx: index of FV which will use the mask 1502 * @mask: the 16-bit mask 1503 * @mask_idx: variable to receive the mask index 1504 */ 1505 static int 1506 ice_alloc_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 idx, u16 mask, 1507 u16 *mask_idx) 1508 { 1509 bool found_unused = false, found_copy = false; 1510 u16 unused_idx = 0, copy_idx = 0; 1511 int status = -ENOSPC; 1512 u16 i; 1513 1514 if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD) 1515 return -EINVAL; 1516 1517 mutex_lock(&hw->blk[blk].masks.lock); 1518 1519 for (i = hw->blk[blk].masks.first; 1520 i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++) 1521 if (hw->blk[blk].masks.masks[i].in_use) { 1522 /* if mask is in use and it exactly duplicates the 1523 * desired mask and index, then in can be reused 1524 */ 1525 if (hw->blk[blk].masks.masks[i].mask == mask && 1526 hw->blk[blk].masks.masks[i].idx == idx) { 1527 found_copy = true; 1528 copy_idx = i; 1529 break; 1530 } 1531 } else { 1532 /* save off unused index, but keep searching in case 1533 * there is an exact match later on 1534 */ 1535 if (!found_unused) { 1536 found_unused = true; 1537 unused_idx = i; 1538 } 1539 } 1540 1541 if (found_copy) 1542 i = copy_idx; 1543 else if (found_unused) 1544 i = unused_idx; 1545 else 1546 goto err_ice_alloc_prof_mask; 1547 1548 /* update mask for a new entry */ 1549 if (found_unused) { 1550 hw->blk[blk].masks.masks[i].in_use = true; 1551 hw->blk[blk].masks.masks[i].mask = mask; 1552 hw->blk[blk].masks.masks[i].idx = idx; 1553 hw->blk[blk].masks.masks[i].ref = 0; 1554 ice_write_prof_mask_reg(hw, blk, i, idx, mask); 1555 } 1556 1557 hw->blk[blk].masks.masks[i].ref++; 1558 *mask_idx = i; 1559 status = 0; 1560 1561 err_ice_alloc_prof_mask: 1562 mutex_unlock(&hw->blk[blk].masks.lock); 1563 1564 return status; 1565 } 1566 1567 /** 1568 * ice_free_prof_mask - free profile mask 1569 * @hw: pointer to the HW struct 1570 * @blk: hardware block 1571 * @mask_idx: index of mask 1572 */ 1573 static int 1574 ice_free_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 mask_idx) 1575 { 1576 if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD) 1577 return -EINVAL; 1578 1579 if (!(mask_idx >= hw->blk[blk].masks.first && 1580 mask_idx < hw->blk[blk].masks.first + hw->blk[blk].masks.count)) 1581 return -ENOENT; 1582 1583 mutex_lock(&hw->blk[blk].masks.lock); 1584 1585 if (!hw->blk[blk].masks.masks[mask_idx].in_use) 1586 goto exit_ice_free_prof_mask; 1587 1588 if (hw->blk[blk].masks.masks[mask_idx].ref > 1) { 1589 hw->blk[blk].masks.masks[mask_idx].ref--; 1590 goto exit_ice_free_prof_mask; 1591 } 1592 1593 /* remove mask */ 1594 hw->blk[blk].masks.masks[mask_idx].in_use = false; 1595 hw->blk[blk].masks.masks[mask_idx].mask = 0; 1596 hw->blk[blk].masks.masks[mask_idx].idx = 0; 1597 1598 /* update mask as unused entry */ 1599 ice_debug(hw, ICE_DBG_PKG, "Free mask, blk %d, mask %d\n", blk, 1600 mask_idx); 1601 ice_write_prof_mask_reg(hw, blk, mask_idx, 0, 0); 1602 1603 exit_ice_free_prof_mask: 1604 mutex_unlock(&hw->blk[blk].masks.lock); 1605 1606 return 0; 1607 } 1608 1609 /** 1610 * ice_free_prof_masks - free all profile masks for a profile 1611 * @hw: pointer to the HW struct 1612 * @blk: hardware block 1613 * @prof_id: profile ID 1614 */ 1615 static int 1616 ice_free_prof_masks(struct ice_hw *hw, enum ice_block blk, u16 prof_id) 1617 { 1618 u32 mask_bm; 1619 u16 i; 1620 1621 if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD) 1622 return -EINVAL; 1623 1624 mask_bm = hw->blk[blk].es.mask_ena[prof_id]; 1625 for (i = 0; i < BITS_PER_BYTE * sizeof(mask_bm); i++) 1626 if (mask_bm & BIT(i)) 1627 ice_free_prof_mask(hw, blk, i); 1628 1629 return 0; 1630 } 1631 1632 /** 1633 * ice_shutdown_prof_masks - releases lock for masking 1634 * @hw: pointer to the HW struct 1635 * @blk: hardware block 1636 * 1637 * This should be called before unloading the driver 1638 */ 1639 static void ice_shutdown_prof_masks(struct ice_hw *hw, enum ice_block blk) 1640 { 1641 u16 i; 1642 1643 mutex_lock(&hw->blk[blk].masks.lock); 1644 1645 for (i = hw->blk[blk].masks.first; 1646 i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++) { 1647 ice_write_prof_mask_reg(hw, blk, i, 0, 0); 1648 1649 hw->blk[blk].masks.masks[i].in_use = false; 1650 hw->blk[blk].masks.masks[i].idx = 0; 1651 hw->blk[blk].masks.masks[i].mask = 0; 1652 } 1653 1654 mutex_unlock(&hw->blk[blk].masks.lock); 1655 mutex_destroy(&hw->blk[blk].masks.lock); 1656 } 1657 1658 /** 1659 * ice_shutdown_all_prof_masks - releases all locks for masking 1660 * @hw: pointer to the HW struct 1661 * 1662 * This should be called before unloading the driver 1663 */ 1664 static void ice_shutdown_all_prof_masks(struct ice_hw *hw) 1665 { 1666 ice_shutdown_prof_masks(hw, ICE_BLK_RSS); 1667 ice_shutdown_prof_masks(hw, ICE_BLK_FD); 1668 } 1669 1670 /** 1671 * ice_update_prof_masking - set registers according to masking 1672 * @hw: pointer to the HW struct 1673 * @blk: hardware block 1674 * @prof_id: profile ID 1675 * @masks: masks 1676 */ 1677 static int 1678 ice_update_prof_masking(struct ice_hw *hw, enum ice_block blk, u16 prof_id, 1679 u16 *masks) 1680 { 1681 bool err = false; 1682 u32 ena_mask = 0; 1683 u16 idx; 1684 u16 i; 1685 1686 /* Only support FD and RSS masking, otherwise nothing to be done */ 1687 if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD) 1688 return 0; 1689 1690 for (i = 0; i < hw->blk[blk].es.fvw; i++) 1691 if (masks[i] && masks[i] != 0xFFFF) { 1692 if (!ice_alloc_prof_mask(hw, blk, i, masks[i], &idx)) { 1693 ena_mask |= BIT(idx); 1694 } else { 1695 /* not enough bitmaps */ 1696 err = true; 1697 break; 1698 } 1699 } 1700 1701 if (err) { 1702 /* free any bitmaps we have allocated */ 1703 for (i = 0; i < BITS_PER_BYTE * sizeof(ena_mask); i++) 1704 if (ena_mask & BIT(i)) 1705 ice_free_prof_mask(hw, blk, i); 1706 1707 return -EIO; 1708 } 1709 1710 /* enable the masks for this profile */ 1711 ice_write_prof_mask_enable_res(hw, blk, prof_id, ena_mask); 1712 1713 /* store enabled masks with profile so that they can be freed later */ 1714 hw->blk[blk].es.mask_ena[prof_id] = ena_mask; 1715 1716 return 0; 1717 } 1718 1719 /** 1720 * ice_write_es - write an extraction sequence and symmetric setting to hardware 1721 * @hw: pointer to the HW struct 1722 * @blk: the block in which to write the extraction sequence 1723 * @prof_id: the profile ID to write 1724 * @fv: pointer to the extraction sequence to write - NULL to clear extraction 1725 * @symm: symmetric setting for RSS profiles 1726 */ 1727 static void 1728 ice_write_es(struct ice_hw *hw, enum ice_block blk, u8 prof_id, 1729 struct ice_fv_word *fv, bool symm) 1730 { 1731 u16 off; 1732 1733 off = prof_id * hw->blk[blk].es.fvw; 1734 if (!fv) { 1735 memset(&hw->blk[blk].es.t[off], 0, 1736 hw->blk[blk].es.fvw * sizeof(*fv)); 1737 hw->blk[blk].es.written[prof_id] = false; 1738 } else { 1739 memcpy(&hw->blk[blk].es.t[off], fv, 1740 hw->blk[blk].es.fvw * sizeof(*fv)); 1741 } 1742 1743 if (blk == ICE_BLK_RSS) 1744 hw->blk[blk].es.symm[prof_id] = symm; 1745 } 1746 1747 /** 1748 * ice_prof_dec_ref - decrement reference count for profile 1749 * @hw: pointer to the HW struct 1750 * @blk: the block from which to free the profile ID 1751 * @prof_id: the profile ID for which to decrement the reference count 1752 */ 1753 static int 1754 ice_prof_dec_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id) 1755 { 1756 if (prof_id > hw->blk[blk].es.count) 1757 return -EINVAL; 1758 1759 if (hw->blk[blk].es.ref_count[prof_id] > 0) { 1760 if (!--hw->blk[blk].es.ref_count[prof_id]) { 1761 ice_write_es(hw, blk, prof_id, NULL, false); 1762 ice_free_prof_masks(hw, blk, prof_id); 1763 return ice_free_prof_id(hw, blk, prof_id); 1764 } 1765 } 1766 1767 return 0; 1768 } 1769 1770 /* Block / table section IDs */ 1771 static const u32 ice_blk_sids[ICE_BLK_COUNT][ICE_SID_OFF_COUNT] = { 1772 /* SWITCH */ 1773 { ICE_SID_XLT1_SW, 1774 ICE_SID_XLT2_SW, 1775 ICE_SID_PROFID_TCAM_SW, 1776 ICE_SID_PROFID_REDIR_SW, 1777 ICE_SID_FLD_VEC_SW 1778 }, 1779 1780 /* ACL */ 1781 { ICE_SID_XLT1_ACL, 1782 ICE_SID_XLT2_ACL, 1783 ICE_SID_PROFID_TCAM_ACL, 1784 ICE_SID_PROFID_REDIR_ACL, 1785 ICE_SID_FLD_VEC_ACL 1786 }, 1787 1788 /* FD */ 1789 { ICE_SID_XLT1_FD, 1790 ICE_SID_XLT2_FD, 1791 ICE_SID_PROFID_TCAM_FD, 1792 ICE_SID_PROFID_REDIR_FD, 1793 ICE_SID_FLD_VEC_FD 1794 }, 1795 1796 /* RSS */ 1797 { ICE_SID_XLT1_RSS, 1798 ICE_SID_XLT2_RSS, 1799 ICE_SID_PROFID_TCAM_RSS, 1800 ICE_SID_PROFID_REDIR_RSS, 1801 ICE_SID_FLD_VEC_RSS 1802 }, 1803 1804 /* PE */ 1805 { ICE_SID_XLT1_PE, 1806 ICE_SID_XLT2_PE, 1807 ICE_SID_PROFID_TCAM_PE, 1808 ICE_SID_PROFID_REDIR_PE, 1809 ICE_SID_FLD_VEC_PE 1810 } 1811 }; 1812 1813 /** 1814 * ice_init_sw_xlt1_db - init software XLT1 database from HW tables 1815 * @hw: pointer to the hardware structure 1816 * @blk: the HW block to initialize 1817 */ 1818 static void ice_init_sw_xlt1_db(struct ice_hw *hw, enum ice_block blk) 1819 { 1820 u16 pt; 1821 1822 for (pt = 0; pt < hw->blk[blk].xlt1.count; pt++) { 1823 u8 ptg; 1824 1825 ptg = hw->blk[blk].xlt1.t[pt]; 1826 if (ptg != ICE_DEFAULT_PTG) { 1827 ice_ptg_alloc_val(hw, blk, ptg); 1828 ice_ptg_add_mv_ptype(hw, blk, pt, ptg); 1829 } 1830 } 1831 } 1832 1833 /** 1834 * ice_init_sw_xlt2_db - init software XLT2 database from HW tables 1835 * @hw: pointer to the hardware structure 1836 * @blk: the HW block to initialize 1837 */ 1838 static void ice_init_sw_xlt2_db(struct ice_hw *hw, enum ice_block blk) 1839 { 1840 u16 vsi; 1841 1842 for (vsi = 0; vsi < hw->blk[blk].xlt2.count; vsi++) { 1843 u16 vsig; 1844 1845 vsig = hw->blk[blk].xlt2.t[vsi]; 1846 if (vsig) { 1847 ice_vsig_alloc_val(hw, blk, vsig); 1848 ice_vsig_add_mv_vsi(hw, blk, vsi, vsig); 1849 /* no changes at this time, since this has been 1850 * initialized from the original package 1851 */ 1852 hw->blk[blk].xlt2.vsis[vsi].changed = 0; 1853 } 1854 } 1855 } 1856 1857 /** 1858 * ice_init_sw_db - init software database from HW tables 1859 * @hw: pointer to the hardware structure 1860 */ 1861 static void ice_init_sw_db(struct ice_hw *hw) 1862 { 1863 u16 i; 1864 1865 for (i = 0; i < ICE_BLK_COUNT; i++) { 1866 ice_init_sw_xlt1_db(hw, (enum ice_block)i); 1867 ice_init_sw_xlt2_db(hw, (enum ice_block)i); 1868 } 1869 } 1870 1871 /** 1872 * ice_fill_tbl - Reads content of a single table type into database 1873 * @hw: pointer to the hardware structure 1874 * @block_id: Block ID of the table to copy 1875 * @sid: Section ID of the table to copy 1876 * 1877 * Will attempt to read the entire content of a given table of a single block 1878 * into the driver database. We assume that the buffer will always 1879 * be as large or larger than the data contained in the package. If 1880 * this condition is not met, there is most likely an error in the package 1881 * contents. 1882 */ 1883 static void ice_fill_tbl(struct ice_hw *hw, enum ice_block block_id, u32 sid) 1884 { 1885 u32 dst_len, sect_len, offset = 0; 1886 struct ice_prof_redir_section *pr; 1887 struct ice_prof_id_section *pid; 1888 struct ice_xlt1_section *xlt1; 1889 struct ice_xlt2_section *xlt2; 1890 struct ice_sw_fv_section *es; 1891 struct ice_pkg_enum state; 1892 u8 *src, *dst; 1893 void *sect; 1894 1895 /* if the HW segment pointer is null then the first iteration of 1896 * ice_pkg_enum_section() will fail. In this case the HW tables will 1897 * not be filled and return success. 1898 */ 1899 if (!hw->seg) { 1900 ice_debug(hw, ICE_DBG_PKG, "hw->seg is NULL, tables are not filled\n"); 1901 return; 1902 } 1903 1904 memset(&state, 0, sizeof(state)); 1905 1906 sect = ice_pkg_enum_section(hw->seg, &state, sid); 1907 1908 while (sect) { 1909 switch (sid) { 1910 case ICE_SID_XLT1_SW: 1911 case ICE_SID_XLT1_FD: 1912 case ICE_SID_XLT1_RSS: 1913 case ICE_SID_XLT1_ACL: 1914 case ICE_SID_XLT1_PE: 1915 xlt1 = sect; 1916 src = xlt1->value; 1917 sect_len = le16_to_cpu(xlt1->count) * 1918 sizeof(*hw->blk[block_id].xlt1.t); 1919 dst = hw->blk[block_id].xlt1.t; 1920 dst_len = hw->blk[block_id].xlt1.count * 1921 sizeof(*hw->blk[block_id].xlt1.t); 1922 break; 1923 case ICE_SID_XLT2_SW: 1924 case ICE_SID_XLT2_FD: 1925 case ICE_SID_XLT2_RSS: 1926 case ICE_SID_XLT2_ACL: 1927 case ICE_SID_XLT2_PE: 1928 xlt2 = sect; 1929 src = (__force u8 *)xlt2->value; 1930 sect_len = le16_to_cpu(xlt2->count) * 1931 sizeof(*hw->blk[block_id].xlt2.t); 1932 dst = (u8 *)hw->blk[block_id].xlt2.t; 1933 dst_len = hw->blk[block_id].xlt2.count * 1934 sizeof(*hw->blk[block_id].xlt2.t); 1935 break; 1936 case ICE_SID_PROFID_TCAM_SW: 1937 case ICE_SID_PROFID_TCAM_FD: 1938 case ICE_SID_PROFID_TCAM_RSS: 1939 case ICE_SID_PROFID_TCAM_ACL: 1940 case ICE_SID_PROFID_TCAM_PE: 1941 pid = sect; 1942 src = (u8 *)pid->entry; 1943 sect_len = le16_to_cpu(pid->count) * 1944 sizeof(*hw->blk[block_id].prof.t); 1945 dst = (u8 *)hw->blk[block_id].prof.t; 1946 dst_len = hw->blk[block_id].prof.count * 1947 sizeof(*hw->blk[block_id].prof.t); 1948 break; 1949 case ICE_SID_PROFID_REDIR_SW: 1950 case ICE_SID_PROFID_REDIR_FD: 1951 case ICE_SID_PROFID_REDIR_RSS: 1952 case ICE_SID_PROFID_REDIR_ACL: 1953 case ICE_SID_PROFID_REDIR_PE: 1954 pr = sect; 1955 src = pr->redir_value; 1956 sect_len = le16_to_cpu(pr->count) * 1957 sizeof(*hw->blk[block_id].prof_redir.t); 1958 dst = hw->blk[block_id].prof_redir.t; 1959 dst_len = hw->blk[block_id].prof_redir.count * 1960 sizeof(*hw->blk[block_id].prof_redir.t); 1961 break; 1962 case ICE_SID_FLD_VEC_SW: 1963 case ICE_SID_FLD_VEC_FD: 1964 case ICE_SID_FLD_VEC_RSS: 1965 case ICE_SID_FLD_VEC_ACL: 1966 case ICE_SID_FLD_VEC_PE: 1967 es = sect; 1968 src = (u8 *)es->fv; 1969 sect_len = (u32)(le16_to_cpu(es->count) * 1970 hw->blk[block_id].es.fvw) * 1971 sizeof(*hw->blk[block_id].es.t); 1972 dst = (u8 *)hw->blk[block_id].es.t; 1973 dst_len = (u32)(hw->blk[block_id].es.count * 1974 hw->blk[block_id].es.fvw) * 1975 sizeof(*hw->blk[block_id].es.t); 1976 break; 1977 default: 1978 return; 1979 } 1980 1981 /* if the section offset exceeds destination length, terminate 1982 * table fill. 1983 */ 1984 if (offset > dst_len) 1985 return; 1986 1987 /* if the sum of section size and offset exceed destination size 1988 * then we are out of bounds of the HW table size for that PF. 1989 * Changing section length to fill the remaining table space 1990 * of that PF. 1991 */ 1992 if ((offset + sect_len) > dst_len) 1993 sect_len = dst_len - offset; 1994 1995 memcpy(dst + offset, src, sect_len); 1996 offset += sect_len; 1997 sect = ice_pkg_enum_section(NULL, &state, sid); 1998 } 1999 } 2000 2001 /** 2002 * ice_fill_blk_tbls - Read package context for tables 2003 * @hw: pointer to the hardware structure 2004 * 2005 * Reads the current package contents and populates the driver 2006 * database with the data iteratively for all advanced feature 2007 * blocks. Assume that the HW tables have been allocated. 2008 */ 2009 void ice_fill_blk_tbls(struct ice_hw *hw) 2010 { 2011 u8 i; 2012 2013 for (i = 0; i < ICE_BLK_COUNT; i++) { 2014 enum ice_block blk_id = (enum ice_block)i; 2015 2016 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt1.sid); 2017 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt2.sid); 2018 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof.sid); 2019 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof_redir.sid); 2020 ice_fill_tbl(hw, blk_id, hw->blk[blk_id].es.sid); 2021 } 2022 2023 ice_init_sw_db(hw); 2024 } 2025 2026 /** 2027 * ice_free_prof_map - free profile map 2028 * @hw: pointer to the hardware structure 2029 * @blk_idx: HW block index 2030 */ 2031 static void ice_free_prof_map(struct ice_hw *hw, u8 blk_idx) 2032 { 2033 struct ice_es *es = &hw->blk[blk_idx].es; 2034 struct ice_prof_map *del, *tmp; 2035 2036 mutex_lock(&es->prof_map_lock); 2037 list_for_each_entry_safe(del, tmp, &es->prof_map, list) { 2038 list_del(&del->list); 2039 devm_kfree(ice_hw_to_dev(hw), del); 2040 } 2041 INIT_LIST_HEAD(&es->prof_map); 2042 mutex_unlock(&es->prof_map_lock); 2043 } 2044 2045 /** 2046 * ice_free_flow_profs - free flow profile entries 2047 * @hw: pointer to the hardware structure 2048 * @blk_idx: HW block index 2049 */ 2050 static void ice_free_flow_profs(struct ice_hw *hw, u8 blk_idx) 2051 { 2052 struct ice_flow_prof *p, *tmp; 2053 2054 mutex_lock(&hw->fl_profs_locks[blk_idx]); 2055 list_for_each_entry_safe(p, tmp, &hw->fl_profs[blk_idx], l_entry) { 2056 struct ice_flow_entry *e, *t; 2057 2058 list_for_each_entry_safe(e, t, &p->entries, l_entry) 2059 ice_flow_rem_entry(hw, (enum ice_block)blk_idx, 2060 ICE_FLOW_ENTRY_HNDL(e)); 2061 2062 list_del(&p->l_entry); 2063 2064 mutex_destroy(&p->entries_lock); 2065 devm_kfree(ice_hw_to_dev(hw), p); 2066 } 2067 mutex_unlock(&hw->fl_profs_locks[blk_idx]); 2068 2069 /* if driver is in reset and tables are being cleared 2070 * re-initialize the flow profile list heads 2071 */ 2072 INIT_LIST_HEAD(&hw->fl_profs[blk_idx]); 2073 } 2074 2075 /** 2076 * ice_free_vsig_tbl - free complete VSIG table entries 2077 * @hw: pointer to the hardware structure 2078 * @blk: the HW block on which to free the VSIG table entries 2079 */ 2080 static void ice_free_vsig_tbl(struct ice_hw *hw, enum ice_block blk) 2081 { 2082 u16 i; 2083 2084 if (!hw->blk[blk].xlt2.vsig_tbl) 2085 return; 2086 2087 for (i = 1; i < ICE_MAX_VSIGS; i++) 2088 if (hw->blk[blk].xlt2.vsig_tbl[i].in_use) 2089 ice_vsig_free(hw, blk, i); 2090 } 2091 2092 /** 2093 * ice_free_hw_tbls - free hardware table memory 2094 * @hw: pointer to the hardware structure 2095 */ 2096 void ice_free_hw_tbls(struct ice_hw *hw) 2097 { 2098 struct ice_rss_cfg *r, *rt; 2099 u8 i; 2100 2101 for (i = 0; i < ICE_BLK_COUNT; i++) { 2102 if (hw->blk[i].is_list_init) { 2103 struct ice_es *es = &hw->blk[i].es; 2104 2105 ice_free_prof_map(hw, i); 2106 mutex_destroy(&es->prof_map_lock); 2107 2108 ice_free_flow_profs(hw, i); 2109 mutex_destroy(&hw->fl_profs_locks[i]); 2110 2111 hw->blk[i].is_list_init = false; 2112 } 2113 ice_free_vsig_tbl(hw, (enum ice_block)i); 2114 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptypes); 2115 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptg_tbl); 2116 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.t); 2117 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.t); 2118 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsig_tbl); 2119 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsis); 2120 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof.t); 2121 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof_redir.t); 2122 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.t); 2123 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.ref_count); 2124 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.symm); 2125 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.written); 2126 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.mask_ena); 2127 devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof_id.id); 2128 } 2129 2130 list_for_each_entry_safe(r, rt, &hw->rss_list_head, l_entry) { 2131 list_del(&r->l_entry); 2132 devm_kfree(ice_hw_to_dev(hw), r); 2133 } 2134 mutex_destroy(&hw->rss_locks); 2135 ice_shutdown_all_prof_masks(hw); 2136 memset(hw->blk, 0, sizeof(hw->blk)); 2137 } 2138 2139 /** 2140 * ice_init_flow_profs - init flow profile locks and list heads 2141 * @hw: pointer to the hardware structure 2142 * @blk_idx: HW block index 2143 */ 2144 static void ice_init_flow_profs(struct ice_hw *hw, u8 blk_idx) 2145 { 2146 mutex_init(&hw->fl_profs_locks[blk_idx]); 2147 INIT_LIST_HEAD(&hw->fl_profs[blk_idx]); 2148 } 2149 2150 /** 2151 * ice_clear_hw_tbls - clear HW tables and flow profiles 2152 * @hw: pointer to the hardware structure 2153 */ 2154 void ice_clear_hw_tbls(struct ice_hw *hw) 2155 { 2156 u8 i; 2157 2158 for (i = 0; i < ICE_BLK_COUNT; i++) { 2159 struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir; 2160 struct ice_prof_id *prof_id = &hw->blk[i].prof_id; 2161 struct ice_prof_tcam *prof = &hw->blk[i].prof; 2162 struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1; 2163 struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2; 2164 struct ice_es *es = &hw->blk[i].es; 2165 2166 if (hw->blk[i].is_list_init) { 2167 ice_free_prof_map(hw, i); 2168 ice_free_flow_profs(hw, i); 2169 } 2170 2171 ice_free_vsig_tbl(hw, (enum ice_block)i); 2172 2173 memset(xlt1->ptypes, 0, xlt1->count * sizeof(*xlt1->ptypes)); 2174 memset(xlt1->ptg_tbl, 0, 2175 ICE_MAX_PTGS * sizeof(*xlt1->ptg_tbl)); 2176 memset(xlt1->t, 0, xlt1->count * sizeof(*xlt1->t)); 2177 2178 memset(xlt2->vsis, 0, xlt2->count * sizeof(*xlt2->vsis)); 2179 memset(xlt2->vsig_tbl, 0, 2180 xlt2->count * sizeof(*xlt2->vsig_tbl)); 2181 memset(xlt2->t, 0, xlt2->count * sizeof(*xlt2->t)); 2182 2183 memset(prof->t, 0, prof->count * sizeof(*prof->t)); 2184 memset(prof_redir->t, 0, 2185 prof_redir->count * sizeof(*prof_redir->t)); 2186 2187 memset(es->t, 0, es->count * sizeof(*es->t) * es->fvw); 2188 memset(es->ref_count, 0, es->count * sizeof(*es->ref_count)); 2189 memset(es->symm, 0, es->count * sizeof(*es->symm)); 2190 memset(es->written, 0, es->count * sizeof(*es->written)); 2191 memset(es->mask_ena, 0, es->count * sizeof(*es->mask_ena)); 2192 2193 memset(prof_id->id, 0, prof_id->count * sizeof(*prof_id->id)); 2194 } 2195 } 2196 2197 /** 2198 * ice_init_hw_tbls - init hardware table memory 2199 * @hw: pointer to the hardware structure 2200 */ 2201 int ice_init_hw_tbls(struct ice_hw *hw) 2202 { 2203 u8 i; 2204 2205 mutex_init(&hw->rss_locks); 2206 INIT_LIST_HEAD(&hw->rss_list_head); 2207 ice_init_all_prof_masks(hw); 2208 for (i = 0; i < ICE_BLK_COUNT; i++) { 2209 struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir; 2210 struct ice_prof_id *prof_id = &hw->blk[i].prof_id; 2211 struct ice_prof_tcam *prof = &hw->blk[i].prof; 2212 struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1; 2213 struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2; 2214 struct ice_es *es = &hw->blk[i].es; 2215 u16 j; 2216 2217 if (hw->blk[i].is_list_init) 2218 continue; 2219 2220 ice_init_flow_profs(hw, i); 2221 mutex_init(&es->prof_map_lock); 2222 INIT_LIST_HEAD(&es->prof_map); 2223 hw->blk[i].is_list_init = true; 2224 2225 hw->blk[i].overwrite = blk_sizes[i].overwrite; 2226 es->reverse = blk_sizes[i].reverse; 2227 2228 xlt1->sid = ice_blk_sids[i][ICE_SID_XLT1_OFF]; 2229 xlt1->count = blk_sizes[i].xlt1; 2230 2231 xlt1->ptypes = devm_kzalloc_objs(ice_hw_to_dev(hw), 2232 *xlt1->ptypes, xlt1->count); 2233 2234 if (!xlt1->ptypes) 2235 goto err; 2236 2237 xlt1->ptg_tbl = devm_kzalloc_objs(ice_hw_to_dev(hw), 2238 *xlt1->ptg_tbl, ICE_MAX_PTGS); 2239 2240 if (!xlt1->ptg_tbl) 2241 goto err; 2242 2243 xlt1->t = devm_kzalloc_objs(ice_hw_to_dev(hw), *xlt1->t, 2244 xlt1->count); 2245 if (!xlt1->t) 2246 goto err; 2247 2248 xlt2->sid = ice_blk_sids[i][ICE_SID_XLT2_OFF]; 2249 xlt2->count = blk_sizes[i].xlt2; 2250 2251 xlt2->vsis = devm_kzalloc_objs(ice_hw_to_dev(hw), *xlt2->vsis, 2252 xlt2->count); 2253 2254 if (!xlt2->vsis) 2255 goto err; 2256 2257 xlt2->vsig_tbl = devm_kzalloc_objs(ice_hw_to_dev(hw), 2258 *xlt2->vsig_tbl, xlt2->count); 2259 if (!xlt2->vsig_tbl) 2260 goto err; 2261 2262 for (j = 0; j < xlt2->count; j++) 2263 INIT_LIST_HEAD(&xlt2->vsig_tbl[j].prop_lst); 2264 2265 xlt2->t = devm_kzalloc_objs(ice_hw_to_dev(hw), *xlt2->t, 2266 xlt2->count); 2267 if (!xlt2->t) 2268 goto err; 2269 2270 prof->sid = ice_blk_sids[i][ICE_SID_PR_OFF]; 2271 prof->count = blk_sizes[i].prof_tcam; 2272 prof->max_prof_id = blk_sizes[i].prof_id; 2273 prof->cdid_bits = blk_sizes[i].prof_cdid_bits; 2274 prof->t = devm_kzalloc_objs(ice_hw_to_dev(hw), *prof->t, 2275 prof->count); 2276 2277 if (!prof->t) 2278 goto err; 2279 2280 prof_redir->sid = ice_blk_sids[i][ICE_SID_PR_REDIR_OFF]; 2281 prof_redir->count = blk_sizes[i].prof_redir; 2282 prof_redir->t = devm_kzalloc_objs(ice_hw_to_dev(hw), 2283 *prof_redir->t, 2284 prof_redir->count); 2285 2286 if (!prof_redir->t) 2287 goto err; 2288 2289 es->sid = ice_blk_sids[i][ICE_SID_ES_OFF]; 2290 es->count = blk_sizes[i].es; 2291 es->fvw = blk_sizes[i].fvw; 2292 es->t = devm_kzalloc_objs(ice_hw_to_dev(hw), *es->t, 2293 (u32)(es->count * es->fvw)); 2294 if (!es->t) 2295 goto err; 2296 2297 es->ref_count = devm_kzalloc_objs(ice_hw_to_dev(hw), 2298 *es->ref_count, es->count); 2299 if (!es->ref_count) 2300 goto err; 2301 2302 es->symm = devm_kzalloc_objs(ice_hw_to_dev(hw), *es->symm, 2303 es->count); 2304 if (!es->symm) 2305 goto err; 2306 2307 es->written = devm_kzalloc_objs(ice_hw_to_dev(hw), *es->written, 2308 es->count); 2309 if (!es->written) 2310 goto err; 2311 2312 es->mask_ena = devm_kzalloc_objs(ice_hw_to_dev(hw), 2313 *es->mask_ena, es->count); 2314 if (!es->mask_ena) 2315 goto err; 2316 2317 prof_id->count = blk_sizes[i].prof_id; 2318 prof_id->id = devm_kzalloc_objs(ice_hw_to_dev(hw), *prof_id->id, 2319 prof_id->count); 2320 if (!prof_id->id) 2321 goto err; 2322 } 2323 return 0; 2324 2325 err: 2326 ice_free_hw_tbls(hw); 2327 return -ENOMEM; 2328 } 2329 2330 /** 2331 * ice_prof_gen_key - generate profile ID key 2332 * @hw: pointer to the HW struct 2333 * @blk: the block in which to write profile ID to 2334 * @ptg: packet type group (PTG) portion of key 2335 * @vsig: VSIG portion of key 2336 * @cdid: CDID portion of key 2337 * @flags: flag portion of key 2338 * @vl_msk: valid mask 2339 * @dc_msk: don't care mask 2340 * @nm_msk: never match mask 2341 * @key: output of profile ID key 2342 */ 2343 static int 2344 ice_prof_gen_key(struct ice_hw *hw, enum ice_block blk, u8 ptg, u16 vsig, 2345 u8 cdid, u16 flags, u8 vl_msk[ICE_TCAM_KEY_VAL_SZ], 2346 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ], u8 nm_msk[ICE_TCAM_KEY_VAL_SZ], 2347 u8 key[ICE_TCAM_KEY_SZ]) 2348 { 2349 struct ice_prof_id_key inkey; 2350 2351 inkey.xlt1 = ptg; 2352 inkey.xlt2_cdid = cpu_to_le16(vsig); 2353 inkey.flags = cpu_to_le16(flags); 2354 2355 switch (hw->blk[blk].prof.cdid_bits) { 2356 case 0: 2357 break; 2358 case 2: 2359 #define ICE_CD_2_M 0xC000U 2360 #define ICE_CD_2_S 14 2361 inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_2_M); 2362 inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_2_S); 2363 break; 2364 case 4: 2365 #define ICE_CD_4_M 0xF000U 2366 #define ICE_CD_4_S 12 2367 inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_4_M); 2368 inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_4_S); 2369 break; 2370 case 8: 2371 #define ICE_CD_8_M 0xFF00U 2372 #define ICE_CD_8_S 16 2373 inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_8_M); 2374 inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_8_S); 2375 break; 2376 default: 2377 ice_debug(hw, ICE_DBG_PKG, "Error in profile config\n"); 2378 break; 2379 } 2380 2381 return ice_set_key(key, ICE_TCAM_KEY_SZ, (u8 *)&inkey, vl_msk, dc_msk, 2382 nm_msk, 0, ICE_TCAM_KEY_SZ / 2); 2383 } 2384 2385 /** 2386 * ice_tcam_write_entry - write TCAM entry 2387 * @hw: pointer to the HW struct 2388 * @blk: the block in which to write profile ID to 2389 * @idx: the entry index to write to 2390 * @prof_id: profile ID 2391 * @ptg: packet type group (PTG) portion of key 2392 * @vsig: VSIG portion of key 2393 * @cdid: CDID portion of key 2394 * @flags: flag portion of key 2395 * @vl_msk: valid mask 2396 * @dc_msk: don't care mask 2397 * @nm_msk: never match mask 2398 */ 2399 static int 2400 ice_tcam_write_entry(struct ice_hw *hw, enum ice_block blk, u16 idx, 2401 u8 prof_id, u8 ptg, u16 vsig, u8 cdid, u16 flags, 2402 u8 vl_msk[ICE_TCAM_KEY_VAL_SZ], 2403 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ], 2404 u8 nm_msk[ICE_TCAM_KEY_VAL_SZ]) 2405 { 2406 struct ice_prof_tcam_entry; 2407 int status; 2408 2409 status = ice_prof_gen_key(hw, blk, ptg, vsig, cdid, flags, vl_msk, 2410 dc_msk, nm_msk, hw->blk[blk].prof.t[idx].key); 2411 if (!status) { 2412 hw->blk[blk].prof.t[idx].addr = cpu_to_le16(idx); 2413 hw->blk[blk].prof.t[idx].prof_id = prof_id; 2414 } 2415 2416 return status; 2417 } 2418 2419 /** 2420 * ice_vsig_get_ref - returns number of VSIs belong to a VSIG 2421 * @hw: pointer to the hardware structure 2422 * @blk: HW block 2423 * @vsig: VSIG to query 2424 * @refs: pointer to variable to receive the reference count 2425 */ 2426 static int 2427 ice_vsig_get_ref(struct ice_hw *hw, enum ice_block blk, u16 vsig, u16 *refs) 2428 { 2429 u16 idx = vsig & ICE_VSIG_IDX_M; 2430 struct ice_vsig_vsi *ptr; 2431 2432 *refs = 0; 2433 2434 if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) 2435 return -ENOENT; 2436 2437 ptr = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi; 2438 while (ptr) { 2439 (*refs)++; 2440 ptr = ptr->next_vsi; 2441 } 2442 2443 return 0; 2444 } 2445 2446 /** 2447 * ice_has_prof_vsig - check to see if VSIG has a specific profile 2448 * @hw: pointer to the hardware structure 2449 * @blk: HW block 2450 * @vsig: VSIG to check against 2451 * @hdl: profile handle 2452 */ 2453 static bool 2454 ice_has_prof_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl) 2455 { 2456 u16 idx = vsig & ICE_VSIG_IDX_M; 2457 struct ice_vsig_prof *ent; 2458 2459 list_for_each_entry(ent, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst, 2460 list) 2461 if (ent->profile_cookie == hdl) 2462 return true; 2463 2464 ice_debug(hw, ICE_DBG_INIT, "Characteristic list for VSI group %d not found.\n", 2465 vsig); 2466 return false; 2467 } 2468 2469 /** 2470 * ice_prof_bld_es - build profile ID extraction sequence changes 2471 * @hw: pointer to the HW struct 2472 * @blk: hardware block 2473 * @bld: the update package buffer build to add to 2474 * @chgs: the list of changes to make in hardware 2475 */ 2476 static int 2477 ice_prof_bld_es(struct ice_hw *hw, enum ice_block blk, 2478 struct ice_buf_build *bld, struct list_head *chgs) 2479 { 2480 u16 vec_size = hw->blk[blk].es.fvw * sizeof(struct ice_fv_word); 2481 struct ice_chs_chg *tmp; 2482 2483 list_for_each_entry(tmp, chgs, list_entry) 2484 if (tmp->type == ICE_PTG_ES_ADD && tmp->add_prof) { 2485 u16 off = tmp->prof_id * hw->blk[blk].es.fvw; 2486 struct ice_pkg_es *p; 2487 u32 id; 2488 2489 id = ice_sect_id(blk, ICE_VEC_TBL); 2490 p = ice_pkg_buf_alloc_section(bld, id, 2491 struct_size(p, es, 1) + 2492 vec_size - 2493 sizeof(p->es[0])); 2494 2495 if (!p) 2496 return -ENOSPC; 2497 2498 p->count = cpu_to_le16(1); 2499 p->offset = cpu_to_le16(tmp->prof_id); 2500 2501 memcpy(p->es, &hw->blk[blk].es.t[off], vec_size); 2502 } 2503 2504 return 0; 2505 } 2506 2507 /** 2508 * ice_prof_bld_tcam - build profile ID TCAM changes 2509 * @hw: pointer to the HW struct 2510 * @blk: hardware block 2511 * @bld: the update package buffer build to add to 2512 * @chgs: the list of changes to make in hardware 2513 */ 2514 static int 2515 ice_prof_bld_tcam(struct ice_hw *hw, enum ice_block blk, 2516 struct ice_buf_build *bld, struct list_head *chgs) 2517 { 2518 struct ice_chs_chg *tmp; 2519 2520 list_for_each_entry(tmp, chgs, list_entry) 2521 if (tmp->type == ICE_TCAM_ADD && tmp->add_tcam_idx) { 2522 struct ice_prof_id_section *p; 2523 u32 id; 2524 2525 id = ice_sect_id(blk, ICE_PROF_TCAM); 2526 p = ice_pkg_buf_alloc_section(bld, id, 2527 struct_size(p, entry, 1)); 2528 2529 if (!p) 2530 return -ENOSPC; 2531 2532 p->count = cpu_to_le16(1); 2533 p->entry[0].addr = cpu_to_le16(tmp->tcam_idx); 2534 p->entry[0].prof_id = tmp->prof_id; 2535 2536 memcpy(p->entry[0].key, 2537 &hw->blk[blk].prof.t[tmp->tcam_idx].key, 2538 sizeof(hw->blk[blk].prof.t->key)); 2539 } 2540 2541 return 0; 2542 } 2543 2544 /** 2545 * ice_prof_bld_xlt1 - build XLT1 changes 2546 * @blk: hardware block 2547 * @bld: the update package buffer build to add to 2548 * @chgs: the list of changes to make in hardware 2549 */ 2550 static int 2551 ice_prof_bld_xlt1(enum ice_block blk, struct ice_buf_build *bld, 2552 struct list_head *chgs) 2553 { 2554 struct ice_chs_chg *tmp; 2555 2556 list_for_each_entry(tmp, chgs, list_entry) 2557 if (tmp->type == ICE_PTG_ES_ADD && tmp->add_ptg) { 2558 struct ice_xlt1_section *p; 2559 u32 id; 2560 2561 id = ice_sect_id(blk, ICE_XLT1); 2562 p = ice_pkg_buf_alloc_section(bld, id, 2563 struct_size(p, value, 1)); 2564 2565 if (!p) 2566 return -ENOSPC; 2567 2568 p->count = cpu_to_le16(1); 2569 p->offset = cpu_to_le16(tmp->ptype); 2570 p->value[0] = tmp->ptg; 2571 } 2572 2573 return 0; 2574 } 2575 2576 /** 2577 * ice_prof_bld_xlt2 - build XLT2 changes 2578 * @blk: hardware block 2579 * @bld: the update package buffer build to add to 2580 * @chgs: the list of changes to make in hardware 2581 */ 2582 static int 2583 ice_prof_bld_xlt2(enum ice_block blk, struct ice_buf_build *bld, 2584 struct list_head *chgs) 2585 { 2586 struct ice_chs_chg *tmp; 2587 2588 list_for_each_entry(tmp, chgs, list_entry) { 2589 struct ice_xlt2_section *p; 2590 u32 id; 2591 2592 switch (tmp->type) { 2593 case ICE_VSIG_ADD: 2594 case ICE_VSI_MOVE: 2595 case ICE_VSIG_REM: 2596 id = ice_sect_id(blk, ICE_XLT2); 2597 p = ice_pkg_buf_alloc_section(bld, id, 2598 struct_size(p, value, 1)); 2599 2600 if (!p) 2601 return -ENOSPC; 2602 2603 p->count = cpu_to_le16(1); 2604 p->offset = cpu_to_le16(tmp->vsi); 2605 p->value[0] = cpu_to_le16(tmp->vsig); 2606 break; 2607 default: 2608 break; 2609 } 2610 } 2611 2612 return 0; 2613 } 2614 2615 /** 2616 * ice_upd_prof_hw - update hardware using the change list 2617 * @hw: pointer to the HW struct 2618 * @blk: hardware block 2619 * @chgs: the list of changes to make in hardware 2620 */ 2621 static int 2622 ice_upd_prof_hw(struct ice_hw *hw, enum ice_block blk, 2623 struct list_head *chgs) 2624 { 2625 struct ice_buf_build *b; 2626 struct ice_chs_chg *tmp; 2627 u16 pkg_sects; 2628 u16 xlt1 = 0; 2629 u16 xlt2 = 0; 2630 u16 tcam = 0; 2631 u16 es = 0; 2632 int status; 2633 u16 sects; 2634 2635 /* count number of sections we need */ 2636 list_for_each_entry(tmp, chgs, list_entry) { 2637 switch (tmp->type) { 2638 case ICE_PTG_ES_ADD: 2639 if (tmp->add_ptg) 2640 xlt1++; 2641 if (tmp->add_prof) 2642 es++; 2643 break; 2644 case ICE_TCAM_ADD: 2645 tcam++; 2646 break; 2647 case ICE_VSIG_ADD: 2648 case ICE_VSI_MOVE: 2649 case ICE_VSIG_REM: 2650 xlt2++; 2651 break; 2652 default: 2653 break; 2654 } 2655 } 2656 sects = xlt1 + xlt2 + tcam + es; 2657 2658 if (!sects) 2659 return 0; 2660 2661 /* Build update package buffer */ 2662 b = ice_pkg_buf_alloc(hw); 2663 if (!b) 2664 return -ENOMEM; 2665 2666 status = ice_pkg_buf_reserve_section(b, sects); 2667 if (status) 2668 goto error_tmp; 2669 2670 /* Preserve order of table update: ES, TCAM, PTG, VSIG */ 2671 if (es) { 2672 status = ice_prof_bld_es(hw, blk, b, chgs); 2673 if (status) 2674 goto error_tmp; 2675 } 2676 2677 if (tcam) { 2678 status = ice_prof_bld_tcam(hw, blk, b, chgs); 2679 if (status) 2680 goto error_tmp; 2681 } 2682 2683 if (xlt1) { 2684 status = ice_prof_bld_xlt1(blk, b, chgs); 2685 if (status) 2686 goto error_tmp; 2687 } 2688 2689 if (xlt2) { 2690 status = ice_prof_bld_xlt2(blk, b, chgs); 2691 if (status) 2692 goto error_tmp; 2693 } 2694 2695 /* After package buffer build check if the section count in buffer is 2696 * non-zero and matches the number of sections detected for package 2697 * update. 2698 */ 2699 pkg_sects = ice_pkg_buf_get_active_sections(b); 2700 if (!pkg_sects || pkg_sects != sects) { 2701 status = -EINVAL; 2702 goto error_tmp; 2703 } 2704 2705 /* update package */ 2706 status = ice_update_pkg(hw, ice_pkg_buf(b), 1); 2707 if (status == -EIO) 2708 ice_debug(hw, ICE_DBG_INIT, "Unable to update HW profile\n"); 2709 2710 error_tmp: 2711 ice_pkg_buf_free(hw, b); 2712 return status; 2713 } 2714 2715 /** 2716 * ice_update_fd_mask - set Flow Director Field Vector mask for a profile 2717 * @hw: pointer to the HW struct 2718 * @prof_id: profile ID 2719 * @mask_sel: mask select 2720 * 2721 * This function enable any of the masks selected by the mask select parameter 2722 * for the profile specified. 2723 */ 2724 static void ice_update_fd_mask(struct ice_hw *hw, u16 prof_id, u32 mask_sel) 2725 { 2726 wr32(hw, GLQF_FDMASK_SEL(prof_id), mask_sel); 2727 2728 ice_debug(hw, ICE_DBG_INIT, "fd mask(%d): %x = %x\n", prof_id, 2729 GLQF_FDMASK_SEL(prof_id), mask_sel); 2730 } 2731 2732 struct ice_fd_src_dst_pair { 2733 u8 prot_id; 2734 u8 count; 2735 u16 off; 2736 }; 2737 2738 static const struct ice_fd_src_dst_pair ice_fd_pairs[] = { 2739 /* These are defined in pairs */ 2740 { ICE_PROT_IPV4_OF_OR_S, 2, 12 }, 2741 { ICE_PROT_IPV4_OF_OR_S, 2, 16 }, 2742 2743 { ICE_PROT_IPV4_IL, 2, 12 }, 2744 { ICE_PROT_IPV4_IL, 2, 16 }, 2745 2746 { ICE_PROT_IPV6_OF_OR_S, 8, 8 }, 2747 { ICE_PROT_IPV6_OF_OR_S, 8, 24 }, 2748 2749 { ICE_PROT_IPV6_IL, 8, 8 }, 2750 { ICE_PROT_IPV6_IL, 8, 24 }, 2751 2752 { ICE_PROT_TCP_IL, 1, 0 }, 2753 { ICE_PROT_TCP_IL, 1, 2 }, 2754 2755 { ICE_PROT_UDP_OF, 1, 0 }, 2756 { ICE_PROT_UDP_OF, 1, 2 }, 2757 2758 { ICE_PROT_UDP_IL_OR_S, 1, 0 }, 2759 { ICE_PROT_UDP_IL_OR_S, 1, 2 }, 2760 2761 { ICE_PROT_SCTP_IL, 1, 0 }, 2762 { ICE_PROT_SCTP_IL, 1, 2 } 2763 }; 2764 2765 #define ICE_FD_SRC_DST_PAIR_COUNT ARRAY_SIZE(ice_fd_pairs) 2766 2767 /** 2768 * ice_update_fd_swap - set register appropriately for a FD FV extraction 2769 * @hw: pointer to the HW struct 2770 * @prof_id: profile ID 2771 * @es: extraction sequence (length of array is determined by the block) 2772 */ 2773 static int 2774 ice_update_fd_swap(struct ice_hw *hw, u16 prof_id, struct ice_fv_word *es) 2775 { 2776 DECLARE_BITMAP(pair_list, ICE_FD_SRC_DST_PAIR_COUNT); 2777 u8 pair_start[ICE_FD_SRC_DST_PAIR_COUNT] = { 0 }; 2778 #define ICE_FD_FV_NOT_FOUND (-2) 2779 s8 first_free = ICE_FD_FV_NOT_FOUND; 2780 u8 used[ICE_MAX_FV_WORDS] = { 0 }; 2781 s8 orig_free, si; 2782 u32 mask_sel = 0; 2783 u8 i, j, k; 2784 2785 bitmap_zero(pair_list, ICE_FD_SRC_DST_PAIR_COUNT); 2786 2787 /* This code assumes that the Flow Director field vectors are assigned 2788 * from the end of the FV indexes working towards the zero index, that 2789 * only complete fields will be included and will be consecutive, and 2790 * that there are no gaps between valid indexes. 2791 */ 2792 2793 /* Determine swap fields present */ 2794 for (i = 0; i < hw->blk[ICE_BLK_FD].es.fvw; i++) { 2795 /* Find the first free entry, assuming right to left population. 2796 * This is where we can start adding additional pairs if needed. 2797 */ 2798 if (first_free == ICE_FD_FV_NOT_FOUND && es[i].prot_id != 2799 ICE_PROT_INVALID) 2800 first_free = i - 1; 2801 2802 for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++) 2803 if (es[i].prot_id == ice_fd_pairs[j].prot_id && 2804 es[i].off == ice_fd_pairs[j].off) { 2805 __set_bit(j, pair_list); 2806 pair_start[j] = i; 2807 } 2808 } 2809 2810 orig_free = first_free; 2811 2812 /* determine missing swap fields that need to be added */ 2813 for (i = 0; i < ICE_FD_SRC_DST_PAIR_COUNT; i += 2) { 2814 u8 bit1 = test_bit(i + 1, pair_list); 2815 u8 bit0 = test_bit(i, pair_list); 2816 2817 if (bit0 ^ bit1) { 2818 u8 index; 2819 2820 /* add the appropriate 'paired' entry */ 2821 if (!bit0) 2822 index = i; 2823 else 2824 index = i + 1; 2825 2826 /* check for room */ 2827 if (first_free + 1 < (s8)ice_fd_pairs[index].count) 2828 return -ENOSPC; 2829 2830 /* place in extraction sequence */ 2831 for (k = 0; k < ice_fd_pairs[index].count; k++) { 2832 es[first_free - k].prot_id = 2833 ice_fd_pairs[index].prot_id; 2834 es[first_free - k].off = 2835 ice_fd_pairs[index].off + (k * 2); 2836 2837 if (k > first_free) 2838 return -EIO; 2839 2840 /* keep track of non-relevant fields */ 2841 mask_sel |= BIT(first_free - k); 2842 } 2843 2844 pair_start[index] = first_free; 2845 first_free -= ice_fd_pairs[index].count; 2846 } 2847 } 2848 2849 /* fill in the swap array */ 2850 si = hw->blk[ICE_BLK_FD].es.fvw - 1; 2851 while (si >= 0) { 2852 u8 indexes_used = 1; 2853 2854 /* assume flat at this index */ 2855 #define ICE_SWAP_VALID 0x80 2856 used[si] = si | ICE_SWAP_VALID; 2857 2858 if (orig_free == ICE_FD_FV_NOT_FOUND || si <= orig_free) { 2859 si -= indexes_used; 2860 continue; 2861 } 2862 2863 /* check for a swap location */ 2864 for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++) 2865 if (es[si].prot_id == ice_fd_pairs[j].prot_id && 2866 es[si].off == ice_fd_pairs[j].off) { 2867 u8 idx; 2868 2869 /* determine the appropriate matching field */ 2870 idx = j + ((j % 2) ? -1 : 1); 2871 2872 indexes_used = ice_fd_pairs[idx].count; 2873 for (k = 0; k < indexes_used; k++) { 2874 used[si - k] = (pair_start[idx] - k) | 2875 ICE_SWAP_VALID; 2876 } 2877 2878 break; 2879 } 2880 2881 si -= indexes_used; 2882 } 2883 2884 /* for each set of 4 swap and 4 inset indexes, write the appropriate 2885 * register 2886 */ 2887 for (j = 0; j < hw->blk[ICE_BLK_FD].es.fvw / 4; j++) { 2888 u32 raw_swap = 0; 2889 u32 raw_in = 0; 2890 2891 for (k = 0; k < 4; k++) { 2892 u8 idx; 2893 2894 idx = (j * 4) + k; 2895 if (used[idx] && !(mask_sel & BIT(idx))) { 2896 raw_swap |= used[idx] << (k * BITS_PER_BYTE); 2897 #define ICE_INSET_DFLT 0x9f 2898 raw_in |= ICE_INSET_DFLT << (k * BITS_PER_BYTE); 2899 } 2900 } 2901 2902 /* write the appropriate swap register set */ 2903 wr32(hw, GLQF_FDSWAP(prof_id, j), raw_swap); 2904 2905 ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): %x = %08x\n", 2906 prof_id, j, GLQF_FDSWAP(prof_id, j), raw_swap); 2907 2908 /* write the appropriate inset register set */ 2909 wr32(hw, GLQF_FDINSET(prof_id, j), raw_in); 2910 2911 ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): %x = %08x\n", 2912 prof_id, j, GLQF_FDINSET(prof_id, j), raw_in); 2913 } 2914 2915 /* initially clear the mask select for this profile */ 2916 ice_update_fd_mask(hw, prof_id, 0); 2917 2918 return 0; 2919 } 2920 2921 /* The entries here needs to match the order of enum ice_ptype_attrib */ 2922 static const struct ice_ptype_attrib_info ice_ptype_attributes[] = { 2923 { ICE_GTP_PDU_EH, ICE_GTP_PDU_FLAG_MASK }, 2924 { ICE_GTP_SESSION, ICE_GTP_FLAGS_MASK }, 2925 { ICE_GTP_DOWNLINK, ICE_GTP_FLAGS_MASK }, 2926 { ICE_GTP_UPLINK, ICE_GTP_FLAGS_MASK }, 2927 }; 2928 2929 /** 2930 * ice_get_ptype_attrib_info - get PTYPE attribute information 2931 * @type: attribute type 2932 * @info: pointer to variable to the attribute information 2933 */ 2934 static void 2935 ice_get_ptype_attrib_info(enum ice_ptype_attrib_type type, 2936 struct ice_ptype_attrib_info *info) 2937 { 2938 *info = ice_ptype_attributes[type]; 2939 } 2940 2941 /** 2942 * ice_add_prof_attrib - add any PTG with attributes to profile 2943 * @prof: pointer to the profile to which PTG entries will be added 2944 * @ptg: PTG to be added 2945 * @ptype: PTYPE that needs to be looked up 2946 * @attr: array of attributes that will be considered 2947 * @attr_cnt: number of elements in the attribute array 2948 */ 2949 static int 2950 ice_add_prof_attrib(struct ice_prof_map *prof, u8 ptg, u16 ptype, 2951 const struct ice_ptype_attributes *attr, u16 attr_cnt) 2952 { 2953 bool found = false; 2954 u16 i; 2955 2956 for (i = 0; i < attr_cnt; i++) 2957 if (attr[i].ptype == ptype) { 2958 found = true; 2959 2960 prof->ptg[prof->ptg_cnt] = ptg; 2961 ice_get_ptype_attrib_info(attr[i].attrib, 2962 &prof->attr[prof->ptg_cnt]); 2963 2964 if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE) 2965 return -ENOSPC; 2966 } 2967 2968 if (!found) 2969 return -ENOENT; 2970 2971 return 0; 2972 } 2973 2974 /** 2975 * ice_disable_fd_swap - set register appropriately to disable FD SWAP 2976 * @hw: pointer to the HW struct 2977 * @prof_id: profile ID 2978 */ 2979 static void 2980 ice_disable_fd_swap(struct ice_hw *hw, u8 prof_id) 2981 { 2982 u16 swap_val, fvw_num; 2983 unsigned int i; 2984 2985 swap_val = ICE_SWAP_VALID; 2986 fvw_num = hw->blk[ICE_BLK_FD].es.fvw / ICE_FDIR_REG_SET_SIZE; 2987 2988 /* Since the SWAP Flag in the Programming Desc doesn't work, 2989 * here add method to disable the SWAP Option via setting 2990 * certain SWAP and INSET register sets. 2991 */ 2992 for (i = 0; i < fvw_num ; i++) { 2993 u32 raw_swap, raw_in; 2994 unsigned int j; 2995 2996 raw_swap = 0; 2997 raw_in = 0; 2998 2999 for (j = 0; j < ICE_FDIR_REG_SET_SIZE; j++) { 3000 raw_swap |= (swap_val++) << (j * BITS_PER_BYTE); 3001 raw_in |= ICE_INSET_DFLT << (j * BITS_PER_BYTE); 3002 } 3003 3004 /* write the FDIR swap register set */ 3005 wr32(hw, GLQF_FDSWAP(prof_id, i), raw_swap); 3006 3007 ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): 0x%x = 0x%08x\n", 3008 prof_id, i, GLQF_FDSWAP(prof_id, i), raw_swap); 3009 3010 /* write the FDIR inset register set */ 3011 wr32(hw, GLQF_FDINSET(prof_id, i), raw_in); 3012 3013 ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): 0x%x = 0x%08x\n", 3014 prof_id, i, GLQF_FDINSET(prof_id, i), raw_in); 3015 } 3016 } 3017 3018 /* 3019 * ice_add_prof - add profile 3020 * @hw: pointer to the HW struct 3021 * @blk: hardware block 3022 * @id: profile tracking ID 3023 * @ptypes: array of bitmaps indicating ptypes (ICE_FLOW_PTYPE_MAX bits) 3024 * @attr: array of attributes 3025 * @attr_cnt: number of elements in attr array 3026 * @es: extraction sequence (length of array is determined by the block) 3027 * @masks: mask for extraction sequence 3028 * @symm: symmetric setting for RSS profiles 3029 * @fd_swap: enable/disable FDIR paired src/dst fields swap option 3030 * 3031 * This function registers a profile, which matches a set of PTYPES with a 3032 * particular extraction sequence. While the hardware profile is allocated 3033 * it will not be written until the first call to ice_add_flow that specifies 3034 * the ID value used here. 3035 */ 3036 int 3037 ice_add_prof(struct ice_hw *hw, enum ice_block blk, u64 id, 3038 unsigned long *ptypes, const struct ice_ptype_attributes *attr, 3039 u16 attr_cnt, struct ice_fv_word *es, u16 *masks, bool symm, 3040 bool fd_swap) 3041 { 3042 DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT); 3043 struct ice_prof_map *prof; 3044 int status; 3045 u8 prof_id; 3046 u16 ptype; 3047 3048 bitmap_zero(ptgs_used, ICE_XLT1_CNT); 3049 3050 mutex_lock(&hw->blk[blk].es.prof_map_lock); 3051 3052 /* search for existing profile */ 3053 status = ice_find_prof_id_with_mask(hw, blk, es, masks, symm, &prof_id); 3054 if (status) { 3055 /* allocate profile ID */ 3056 status = ice_alloc_prof_id(hw, blk, &prof_id); 3057 if (status) 3058 goto err_ice_add_prof; 3059 if (blk == ICE_BLK_FD && fd_swap) { 3060 /* For Flow Director block, the extraction sequence may 3061 * need to be altered in the case where there are paired 3062 * fields that have no match. This is necessary because 3063 * for Flow Director, src and dest fields need to paired 3064 * for filter programming and these values are swapped 3065 * during Tx. 3066 */ 3067 status = ice_update_fd_swap(hw, prof_id, es); 3068 if (status) 3069 goto err_ice_add_prof; 3070 } else if (blk == ICE_BLK_FD) { 3071 ice_disable_fd_swap(hw, prof_id); 3072 } 3073 status = ice_update_prof_masking(hw, blk, prof_id, masks); 3074 if (status) 3075 goto err_ice_add_prof; 3076 3077 /* and write new es */ 3078 ice_write_es(hw, blk, prof_id, es, symm); 3079 } 3080 3081 ice_prof_inc_ref(hw, blk, prof_id); 3082 3083 /* add profile info */ 3084 prof = devm_kzalloc_obj(ice_hw_to_dev(hw), *prof); 3085 if (!prof) { 3086 status = -ENOMEM; 3087 goto err_ice_add_prof; 3088 } 3089 3090 prof->profile_cookie = id; 3091 prof->prof_id = prof_id; 3092 prof->ptg_cnt = 0; 3093 prof->context = 0; 3094 3095 /* build list of ptgs */ 3096 for_each_set_bit(ptype, ptypes, ICE_FLOW_PTYPE_MAX) { 3097 u8 ptg; 3098 3099 /* The package should place all ptypes in a non-zero 3100 * PTG, so the following call should never fail. 3101 */ 3102 if (ice_ptg_find_ptype(hw, blk, ptype, &ptg)) 3103 continue; 3104 3105 /* If PTG is already added, skip and continue */ 3106 if (test_bit(ptg, ptgs_used)) 3107 continue; 3108 3109 set_bit(ptg, ptgs_used); 3110 /* Check to see there are any attributes for this ptype, and 3111 * add them if found. 3112 */ 3113 status = ice_add_prof_attrib(prof, ptg, ptype, attr, attr_cnt); 3114 if (status == -ENOSPC) 3115 break; 3116 if (status) { 3117 /* This is simple a ptype/PTG with no attribute */ 3118 prof->ptg[prof->ptg_cnt] = ptg; 3119 prof->attr[prof->ptg_cnt].flags = 0; 3120 prof->attr[prof->ptg_cnt].mask = 0; 3121 3122 if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE) 3123 break; 3124 } 3125 } 3126 3127 list_add(&prof->list, &hw->blk[blk].es.prof_map); 3128 status = 0; 3129 3130 err_ice_add_prof: 3131 mutex_unlock(&hw->blk[blk].es.prof_map_lock); 3132 return status; 3133 } 3134 3135 /** 3136 * ice_search_prof_id - Search for a profile tracking ID 3137 * @hw: pointer to the HW struct 3138 * @blk: hardware block 3139 * @id: profile tracking ID 3140 * 3141 * This will search for a profile tracking ID which was previously added. 3142 * The profile map lock should be held before calling this function. 3143 */ 3144 struct ice_prof_map * 3145 ice_search_prof_id(struct ice_hw *hw, enum ice_block blk, u64 id) 3146 { 3147 struct ice_prof_map *entry = NULL; 3148 struct ice_prof_map *map; 3149 3150 list_for_each_entry(map, &hw->blk[blk].es.prof_map, list) 3151 if (map->profile_cookie == id) { 3152 entry = map; 3153 break; 3154 } 3155 3156 return entry; 3157 } 3158 3159 /** 3160 * ice_vsig_prof_id_count - count profiles in a VSIG 3161 * @hw: pointer to the HW struct 3162 * @blk: hardware block 3163 * @vsig: VSIG to remove the profile from 3164 */ 3165 static u16 3166 ice_vsig_prof_id_count(struct ice_hw *hw, enum ice_block blk, u16 vsig) 3167 { 3168 u16 idx = vsig & ICE_VSIG_IDX_M, count = 0; 3169 struct ice_vsig_prof *p; 3170 3171 list_for_each_entry(p, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst, 3172 list) 3173 count++; 3174 3175 return count; 3176 } 3177 3178 /** 3179 * ice_rel_tcam_idx - release a TCAM index 3180 * @hw: pointer to the HW struct 3181 * @blk: hardware block 3182 * @idx: the index to release 3183 */ 3184 static int ice_rel_tcam_idx(struct ice_hw *hw, enum ice_block blk, u16 idx) 3185 { 3186 /* Masks to invoke a never match entry */ 3187 u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 3188 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF }; 3189 u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x01, 0x00, 0x00, 0x00, 0x00 }; 3190 int status; 3191 3192 /* write the TCAM entry */ 3193 status = ice_tcam_write_entry(hw, blk, idx, 0, 0, 0, 0, 0, vl_msk, 3194 dc_msk, nm_msk); 3195 if (status) 3196 return status; 3197 3198 /* release the TCAM entry */ 3199 status = ice_free_tcam_ent(hw, blk, idx); 3200 3201 return status; 3202 } 3203 3204 /** 3205 * ice_rem_prof_id - remove one profile from a VSIG 3206 * @hw: pointer to the HW struct 3207 * @blk: hardware block 3208 * @prof: pointer to profile structure to remove 3209 */ 3210 static int 3211 ice_rem_prof_id(struct ice_hw *hw, enum ice_block blk, 3212 struct ice_vsig_prof *prof) 3213 { 3214 int status; 3215 u16 i; 3216 3217 for (i = 0; i < prof->tcam_count; i++) 3218 if (prof->tcam[i].in_use) { 3219 prof->tcam[i].in_use = false; 3220 status = ice_rel_tcam_idx(hw, blk, 3221 prof->tcam[i].tcam_idx); 3222 if (status) 3223 return -EIO; 3224 } 3225 3226 return 0; 3227 } 3228 3229 /** 3230 * ice_rem_vsig - remove VSIG 3231 * @hw: pointer to the HW struct 3232 * @blk: hardware block 3233 * @vsig: the VSIG to remove 3234 * @chg: the change list 3235 */ 3236 static int 3237 ice_rem_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, 3238 struct list_head *chg) 3239 { 3240 u16 idx = vsig & ICE_VSIG_IDX_M; 3241 struct ice_vsig_vsi *vsi_cur; 3242 struct ice_vsig_prof *d, *t; 3243 3244 /* remove TCAM entries */ 3245 list_for_each_entry_safe(d, t, 3246 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst, 3247 list) { 3248 int status; 3249 3250 status = ice_rem_prof_id(hw, blk, d); 3251 if (status) 3252 return status; 3253 3254 list_del(&d->list); 3255 devm_kfree(ice_hw_to_dev(hw), d); 3256 } 3257 3258 /* Move all VSIS associated with this VSIG to the default VSIG */ 3259 vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi; 3260 /* If the VSIG has at least 1 VSI then iterate through the list 3261 * and remove the VSIs before deleting the group. 3262 */ 3263 if (vsi_cur) 3264 do { 3265 struct ice_vsig_vsi *tmp = vsi_cur->next_vsi; 3266 struct ice_chs_chg *p; 3267 3268 p = devm_kzalloc_obj(ice_hw_to_dev(hw), *p); 3269 if (!p) 3270 return -ENOMEM; 3271 3272 p->type = ICE_VSIG_REM; 3273 p->orig_vsig = vsig; 3274 p->vsig = ICE_DEFAULT_VSIG; 3275 p->vsi = vsi_cur - hw->blk[blk].xlt2.vsis; 3276 3277 list_add(&p->list_entry, chg); 3278 3279 vsi_cur = tmp; 3280 } while (vsi_cur); 3281 3282 return ice_vsig_free(hw, blk, vsig); 3283 } 3284 3285 /** 3286 * ice_rem_prof_id_vsig - remove a specific profile from a VSIG 3287 * @hw: pointer to the HW struct 3288 * @blk: hardware block 3289 * @vsig: VSIG to remove the profile from 3290 * @hdl: profile handle indicating which profile to remove 3291 * @chg: list to receive a record of changes 3292 */ 3293 static int 3294 ice_rem_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl, 3295 struct list_head *chg) 3296 { 3297 u16 idx = vsig & ICE_VSIG_IDX_M; 3298 struct ice_vsig_prof *p, *t; 3299 3300 list_for_each_entry_safe(p, t, 3301 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst, 3302 list) 3303 if (p->profile_cookie == hdl) { 3304 int status; 3305 3306 if (ice_vsig_prof_id_count(hw, blk, vsig) == 1) 3307 /* this is the last profile, remove the VSIG */ 3308 return ice_rem_vsig(hw, blk, vsig, chg); 3309 3310 status = ice_rem_prof_id(hw, blk, p); 3311 if (!status) { 3312 list_del(&p->list); 3313 devm_kfree(ice_hw_to_dev(hw), p); 3314 } 3315 return status; 3316 } 3317 3318 return -ENOENT; 3319 } 3320 3321 /** 3322 * ice_rem_flow_all - remove all flows with a particular profile 3323 * @hw: pointer to the HW struct 3324 * @blk: hardware block 3325 * @id: profile tracking ID 3326 */ 3327 static int ice_rem_flow_all(struct ice_hw *hw, enum ice_block blk, u64 id) 3328 { 3329 struct ice_chs_chg *del, *tmp; 3330 struct list_head chg; 3331 int status; 3332 u16 i; 3333 3334 INIT_LIST_HEAD(&chg); 3335 3336 for (i = 1; i < ICE_MAX_VSIGS; i++) 3337 if (hw->blk[blk].xlt2.vsig_tbl[i].in_use) { 3338 if (ice_has_prof_vsig(hw, blk, i, id)) { 3339 status = ice_rem_prof_id_vsig(hw, blk, i, id, 3340 &chg); 3341 if (status) 3342 goto err_ice_rem_flow_all; 3343 } 3344 } 3345 3346 status = ice_upd_prof_hw(hw, blk, &chg); 3347 3348 err_ice_rem_flow_all: 3349 list_for_each_entry_safe(del, tmp, &chg, list_entry) { 3350 list_del(&del->list_entry); 3351 devm_kfree(ice_hw_to_dev(hw), del); 3352 } 3353 3354 return status; 3355 } 3356 3357 /** 3358 * ice_rem_prof - remove profile 3359 * @hw: pointer to the HW struct 3360 * @blk: hardware block 3361 * @id: profile tracking ID 3362 * 3363 * This will remove the profile specified by the ID parameter, which was 3364 * previously created through ice_add_prof. If any existing entries 3365 * are associated with this profile, they will be removed as well. 3366 */ 3367 int ice_rem_prof(struct ice_hw *hw, enum ice_block blk, u64 id) 3368 { 3369 struct ice_prof_map *pmap; 3370 int status; 3371 3372 mutex_lock(&hw->blk[blk].es.prof_map_lock); 3373 3374 pmap = ice_search_prof_id(hw, blk, id); 3375 if (!pmap) { 3376 status = -ENOENT; 3377 goto err_ice_rem_prof; 3378 } 3379 3380 /* remove all flows with this profile */ 3381 status = ice_rem_flow_all(hw, blk, pmap->profile_cookie); 3382 if (status) 3383 goto err_ice_rem_prof; 3384 3385 /* dereference profile, and possibly remove */ 3386 ice_prof_dec_ref(hw, blk, pmap->prof_id); 3387 3388 list_del(&pmap->list); 3389 devm_kfree(ice_hw_to_dev(hw), pmap); 3390 3391 err_ice_rem_prof: 3392 mutex_unlock(&hw->blk[blk].es.prof_map_lock); 3393 return status; 3394 } 3395 3396 /** 3397 * ice_get_prof - get profile 3398 * @hw: pointer to the HW struct 3399 * @blk: hardware block 3400 * @hdl: profile handle 3401 * @chg: change list 3402 */ 3403 static int 3404 ice_get_prof(struct ice_hw *hw, enum ice_block blk, u64 hdl, 3405 struct list_head *chg) 3406 { 3407 struct ice_prof_map *map; 3408 struct ice_chs_chg *p; 3409 int status = 0; 3410 u16 i; 3411 3412 mutex_lock(&hw->blk[blk].es.prof_map_lock); 3413 /* Get the details on the profile specified by the handle ID */ 3414 map = ice_search_prof_id(hw, blk, hdl); 3415 if (!map) { 3416 status = -ENOENT; 3417 goto err_ice_get_prof; 3418 } 3419 3420 for (i = 0; i < map->ptg_cnt; i++) 3421 if (!hw->blk[blk].es.written[map->prof_id]) { 3422 /* add ES to change list */ 3423 p = devm_kzalloc_obj(ice_hw_to_dev(hw), *p); 3424 if (!p) { 3425 status = -ENOMEM; 3426 goto err_ice_get_prof; 3427 } 3428 3429 p->type = ICE_PTG_ES_ADD; 3430 p->ptype = 0; 3431 p->ptg = map->ptg[i]; 3432 p->add_ptg = 0; 3433 3434 p->add_prof = 1; 3435 p->prof_id = map->prof_id; 3436 3437 hw->blk[blk].es.written[map->prof_id] = true; 3438 3439 list_add(&p->list_entry, chg); 3440 } 3441 3442 err_ice_get_prof: 3443 mutex_unlock(&hw->blk[blk].es.prof_map_lock); 3444 /* let caller clean up the change list */ 3445 return status; 3446 } 3447 3448 /** 3449 * ice_get_profs_vsig - get a copy of the list of profiles from a VSIG 3450 * @hw: pointer to the HW struct 3451 * @blk: hardware block 3452 * @vsig: VSIG from which to copy the list 3453 * @lst: output list 3454 * 3455 * This routine makes a copy of the list of profiles in the specified VSIG. 3456 */ 3457 static int 3458 ice_get_profs_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, 3459 struct list_head *lst) 3460 { 3461 struct ice_vsig_prof *ent1, *ent2; 3462 u16 idx = vsig & ICE_VSIG_IDX_M; 3463 3464 list_for_each_entry(ent1, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst, 3465 list) { 3466 struct ice_vsig_prof *p; 3467 3468 /* copy to the input list */ 3469 p = devm_kmemdup(ice_hw_to_dev(hw), ent1, sizeof(*p), 3470 GFP_KERNEL); 3471 if (!p) 3472 goto err_ice_get_profs_vsig; 3473 3474 list_add_tail(&p->list, lst); 3475 } 3476 3477 return 0; 3478 3479 err_ice_get_profs_vsig: 3480 list_for_each_entry_safe(ent1, ent2, lst, list) { 3481 list_del(&ent1->list); 3482 devm_kfree(ice_hw_to_dev(hw), ent1); 3483 } 3484 3485 return -ENOMEM; 3486 } 3487 3488 /** 3489 * ice_add_prof_to_lst - add profile entry to a list 3490 * @hw: pointer to the HW struct 3491 * @blk: hardware block 3492 * @lst: the list to be added to 3493 * @hdl: profile handle of entry to add 3494 */ 3495 static int 3496 ice_add_prof_to_lst(struct ice_hw *hw, enum ice_block blk, 3497 struct list_head *lst, u64 hdl) 3498 { 3499 struct ice_prof_map *map; 3500 struct ice_vsig_prof *p; 3501 int status = 0; 3502 u16 i; 3503 3504 mutex_lock(&hw->blk[blk].es.prof_map_lock); 3505 map = ice_search_prof_id(hw, blk, hdl); 3506 if (!map) { 3507 status = -ENOENT; 3508 goto err_ice_add_prof_to_lst; 3509 } 3510 3511 p = devm_kzalloc_obj(ice_hw_to_dev(hw), *p); 3512 if (!p) { 3513 status = -ENOMEM; 3514 goto err_ice_add_prof_to_lst; 3515 } 3516 3517 p->profile_cookie = map->profile_cookie; 3518 p->prof_id = map->prof_id; 3519 p->tcam_count = map->ptg_cnt; 3520 3521 for (i = 0; i < map->ptg_cnt; i++) { 3522 p->tcam[i].prof_id = map->prof_id; 3523 p->tcam[i].tcam_idx = ICE_INVALID_TCAM; 3524 p->tcam[i].ptg = map->ptg[i]; 3525 } 3526 3527 list_add(&p->list, lst); 3528 3529 err_ice_add_prof_to_lst: 3530 mutex_unlock(&hw->blk[blk].es.prof_map_lock); 3531 return status; 3532 } 3533 3534 /** 3535 * ice_move_vsi - move VSI to another VSIG 3536 * @hw: pointer to the HW struct 3537 * @blk: hardware block 3538 * @vsi: the VSI to move 3539 * @vsig: the VSIG to move the VSI to 3540 * @chg: the change list 3541 */ 3542 static int 3543 ice_move_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig, 3544 struct list_head *chg) 3545 { 3546 struct ice_chs_chg *p; 3547 u16 orig_vsig; 3548 int status; 3549 3550 p = devm_kzalloc_obj(ice_hw_to_dev(hw), *p); 3551 if (!p) 3552 return -ENOMEM; 3553 3554 status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig); 3555 if (!status) 3556 status = ice_vsig_add_mv_vsi(hw, blk, vsi, vsig); 3557 3558 if (status) { 3559 devm_kfree(ice_hw_to_dev(hw), p); 3560 return status; 3561 } 3562 3563 p->type = ICE_VSI_MOVE; 3564 p->vsi = vsi; 3565 p->orig_vsig = orig_vsig; 3566 p->vsig = vsig; 3567 3568 list_add(&p->list_entry, chg); 3569 3570 return 0; 3571 } 3572 3573 /** 3574 * ice_set_tcam_flags - set TCAM flag don't care mask 3575 * @mask: mask for flags 3576 * @dc_mask: pointer to the don't care mask 3577 */ 3578 static void ice_set_tcam_flags(u16 mask, u8 dc_mask[ICE_TCAM_KEY_VAL_SZ]) 3579 { 3580 u16 inverted_mask = ~mask; 3581 3582 /* flags are lowest u16 */ 3583 put_unaligned_le16(inverted_mask, dc_mask); 3584 } 3585 3586 /** 3587 * ice_rem_chg_tcam_ent - remove a specific TCAM entry from change list 3588 * @hw: pointer to the HW struct 3589 * @idx: the index of the TCAM entry to remove 3590 * @chg: the list of change structures to search 3591 */ 3592 static void 3593 ice_rem_chg_tcam_ent(struct ice_hw *hw, u16 idx, struct list_head *chg) 3594 { 3595 struct ice_chs_chg *pos, *tmp; 3596 3597 list_for_each_entry_safe(tmp, pos, chg, list_entry) 3598 if (tmp->type == ICE_TCAM_ADD && tmp->tcam_idx == idx) { 3599 list_del(&tmp->list_entry); 3600 devm_kfree(ice_hw_to_dev(hw), tmp); 3601 } 3602 } 3603 3604 /** 3605 * ice_prof_tcam_ena_dis - add enable or disable TCAM change 3606 * @hw: pointer to the HW struct 3607 * @blk: hardware block 3608 * @enable: true to enable, false to disable 3609 * @vsig: the VSIG of the TCAM entry 3610 * @tcam: pointer the TCAM info structure of the TCAM to disable 3611 * @chg: the change list 3612 * 3613 * This function appends an enable or disable TCAM entry in the change log 3614 */ 3615 static int 3616 ice_prof_tcam_ena_dis(struct ice_hw *hw, enum ice_block blk, bool enable, 3617 u16 vsig, struct ice_tcam_inf *tcam, 3618 struct list_head *chg) 3619 { 3620 struct ice_chs_chg *p; 3621 int status; 3622 3623 u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 3624 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 }; 3625 u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 }; 3626 3627 /* if disabling, free the TCAM */ 3628 if (!enable) { 3629 status = ice_rel_tcam_idx(hw, blk, tcam->tcam_idx); 3630 3631 /* if we have already created a change for this TCAM entry, then 3632 * we need to remove that entry, in order to prevent writing to 3633 * a TCAM entry we no longer will have ownership of. 3634 */ 3635 ice_rem_chg_tcam_ent(hw, tcam->tcam_idx, chg); 3636 tcam->tcam_idx = 0; 3637 tcam->in_use = 0; 3638 return status; 3639 } 3640 3641 /* for re-enabling, reallocate a TCAM */ 3642 /* for entries with empty attribute masks, allocate entry from 3643 * the bottom of the TCAM table; otherwise, allocate from the 3644 * top of the table in order to give it higher priority 3645 */ 3646 status = ice_alloc_tcam_ent(hw, blk, tcam->attr.mask == 0, 3647 &tcam->tcam_idx); 3648 if (status) 3649 return status; 3650 3651 /* add TCAM to change list */ 3652 p = devm_kzalloc_obj(ice_hw_to_dev(hw), *p); 3653 if (!p) 3654 return -ENOMEM; 3655 3656 /* set don't care masks for TCAM flags */ 3657 ice_set_tcam_flags(tcam->attr.mask, dc_msk); 3658 3659 status = ice_tcam_write_entry(hw, blk, tcam->tcam_idx, tcam->prof_id, 3660 tcam->ptg, vsig, 0, tcam->attr.flags, 3661 vl_msk, dc_msk, nm_msk); 3662 if (status) 3663 goto err_ice_prof_tcam_ena_dis; 3664 3665 tcam->in_use = 1; 3666 3667 p->type = ICE_TCAM_ADD; 3668 p->add_tcam_idx = true; 3669 p->prof_id = tcam->prof_id; 3670 p->ptg = tcam->ptg; 3671 p->vsig = 0; 3672 p->tcam_idx = tcam->tcam_idx; 3673 3674 /* log change */ 3675 list_add(&p->list_entry, chg); 3676 3677 return 0; 3678 3679 err_ice_prof_tcam_ena_dis: 3680 devm_kfree(ice_hw_to_dev(hw), p); 3681 return status; 3682 } 3683 3684 /** 3685 * ice_ptg_attr_in_use - determine if PTG and attribute pair is in use 3686 * @ptg_attr: pointer to the PTG and attribute pair to check 3687 * @ptgs_used: bitmap that denotes which PTGs are in use 3688 * @attr_used: array of PTG and attributes pairs already used 3689 * @attr_cnt: count of entries in the attr_used array 3690 * 3691 * Return: true if the PTG and attribute pair is in use, false otherwise. 3692 */ 3693 static bool 3694 ice_ptg_attr_in_use(struct ice_tcam_inf *ptg_attr, unsigned long *ptgs_used, 3695 struct ice_tcam_inf *attr_used[], u16 attr_cnt) 3696 { 3697 u16 i; 3698 3699 if (!test_bit(ptg_attr->ptg, ptgs_used)) 3700 return false; 3701 3702 /* the PTG is used, so now look for correct attributes */ 3703 for (i = 0; i < attr_cnt; i++) 3704 if (attr_used[i]->ptg == ptg_attr->ptg && 3705 attr_used[i]->attr.flags == ptg_attr->attr.flags && 3706 attr_used[i]->attr.mask == ptg_attr->attr.mask) 3707 return true; 3708 3709 return false; 3710 } 3711 3712 /** 3713 * ice_adj_prof_priorities - adjust profile based on priorities 3714 * @hw: pointer to the HW struct 3715 * @blk: hardware block 3716 * @vsig: the VSIG for which to adjust profile priorities 3717 * @chg: the change list 3718 */ 3719 static int 3720 ice_adj_prof_priorities(struct ice_hw *hw, enum ice_block blk, u16 vsig, 3721 struct list_head *chg) 3722 { 3723 DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT); 3724 struct ice_tcam_inf **attr_used; 3725 struct ice_vsig_prof *t; 3726 u16 attr_used_cnt = 0; 3727 int status = 0; 3728 u16 idx; 3729 3730 attr_used = kzalloc_objs(*attr_used, ICE_MAX_PTG_ATTRS); 3731 if (!attr_used) 3732 return -ENOMEM; 3733 3734 bitmap_zero(ptgs_used, ICE_XLT1_CNT); 3735 idx = vsig & ICE_VSIG_IDX_M; 3736 3737 /* Priority is based on the order in which the profiles are added. The 3738 * newest added profile has highest priority and the oldest added 3739 * profile has the lowest priority. Since the profile property list for 3740 * a VSIG is sorted from newest to oldest, this code traverses the list 3741 * in order and enables the first of each PTG that it finds (that is not 3742 * already enabled); it also disables any duplicate PTGs that it finds 3743 * in the older profiles (that are currently enabled). 3744 */ 3745 3746 list_for_each_entry(t, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst, 3747 list) { 3748 u16 i; 3749 3750 for (i = 0; i < t->tcam_count; i++) { 3751 bool used; 3752 3753 /* Scan the priorities from newest to oldest. 3754 * Make sure that the newest profiles take priority. 3755 */ 3756 used = ice_ptg_attr_in_use(&t->tcam[i], ptgs_used, 3757 attr_used, attr_used_cnt); 3758 3759 if (used && t->tcam[i].in_use) { 3760 /* need to mark this PTG as never match, as it 3761 * was already in use and therefore duplicate 3762 * (and lower priority) 3763 */ 3764 status = ice_prof_tcam_ena_dis(hw, blk, false, 3765 vsig, 3766 &t->tcam[i], 3767 chg); 3768 if (status) 3769 goto free_attr_used; 3770 } else if (!used && !t->tcam[i].in_use) { 3771 /* need to enable this PTG, as it in not in use 3772 * and not enabled (highest priority) 3773 */ 3774 status = ice_prof_tcam_ena_dis(hw, blk, true, 3775 vsig, 3776 &t->tcam[i], 3777 chg); 3778 if (status) 3779 goto free_attr_used; 3780 } 3781 3782 /* keep track of used ptgs */ 3783 set_bit(t->tcam[i].ptg, ptgs_used); 3784 if (attr_used_cnt < ICE_MAX_PTG_ATTRS) 3785 attr_used[attr_used_cnt++] = &t->tcam[i]; 3786 else 3787 ice_debug(hw, ICE_DBG_INIT, "Warn: ICE_MAX_PTG_ATTRS exceeded\n"); 3788 } 3789 } 3790 3791 free_attr_used: 3792 kfree(attr_used); 3793 return status; 3794 } 3795 3796 /** 3797 * ice_add_prof_id_vsig - add profile to VSIG 3798 * @hw: pointer to the HW struct 3799 * @blk: hardware block 3800 * @vsig: the VSIG to which this profile is to be added 3801 * @hdl: the profile handle indicating the profile to add 3802 * @rev: true to add entries to the end of the list 3803 * @chg: the change list 3804 */ 3805 static int 3806 ice_add_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl, 3807 bool rev, struct list_head *chg) 3808 { 3809 /* Masks that ignore flags */ 3810 u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 3811 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 }; 3812 u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 }; 3813 struct ice_prof_map *map; 3814 struct ice_vsig_prof *t; 3815 struct ice_chs_chg *p; 3816 u16 vsig_idx, i; 3817 int status = 0; 3818 3819 /* Error, if this VSIG already has this profile */ 3820 if (ice_has_prof_vsig(hw, blk, vsig, hdl)) 3821 return -EEXIST; 3822 3823 /* new VSIG profile structure */ 3824 t = devm_kzalloc_obj(ice_hw_to_dev(hw), *t); 3825 if (!t) 3826 return -ENOMEM; 3827 3828 mutex_lock(&hw->blk[blk].es.prof_map_lock); 3829 /* Get the details on the profile specified by the handle ID */ 3830 map = ice_search_prof_id(hw, blk, hdl); 3831 if (!map) { 3832 status = -ENOENT; 3833 goto err_ice_add_prof_id_vsig; 3834 } 3835 3836 t->profile_cookie = map->profile_cookie; 3837 t->prof_id = map->prof_id; 3838 t->tcam_count = map->ptg_cnt; 3839 3840 /* create TCAM entries */ 3841 for (i = 0; i < map->ptg_cnt; i++) { 3842 u16 tcam_idx; 3843 3844 /* add TCAM to change list */ 3845 p = devm_kzalloc_obj(ice_hw_to_dev(hw), *p); 3846 if (!p) { 3847 status = -ENOMEM; 3848 goto err_ice_add_prof_id_vsig; 3849 } 3850 3851 /* allocate the TCAM entry index */ 3852 /* for entries with empty attribute masks, allocate entry from 3853 * the bottom of the TCAM table; otherwise, allocate from the 3854 * top of the table in order to give it higher priority 3855 */ 3856 status = ice_alloc_tcam_ent(hw, blk, map->attr[i].mask == 0, 3857 &tcam_idx); 3858 if (status) { 3859 devm_kfree(ice_hw_to_dev(hw), p); 3860 goto err_ice_add_prof_id_vsig; 3861 } 3862 3863 t->tcam[i].ptg = map->ptg[i]; 3864 t->tcam[i].prof_id = map->prof_id; 3865 t->tcam[i].tcam_idx = tcam_idx; 3866 t->tcam[i].attr = map->attr[i]; 3867 t->tcam[i].in_use = true; 3868 3869 p->type = ICE_TCAM_ADD; 3870 p->add_tcam_idx = true; 3871 p->prof_id = t->tcam[i].prof_id; 3872 p->ptg = t->tcam[i].ptg; 3873 p->vsig = vsig; 3874 p->tcam_idx = t->tcam[i].tcam_idx; 3875 3876 /* set don't care masks for TCAM flags */ 3877 ice_set_tcam_flags(t->tcam[i].attr.mask, dc_msk); 3878 3879 /* write the TCAM entry */ 3880 status = ice_tcam_write_entry(hw, blk, t->tcam[i].tcam_idx, 3881 t->tcam[i].prof_id, 3882 t->tcam[i].ptg, vsig, 0, 3883 t->tcam[i].attr.flags, vl_msk, 3884 dc_msk, nm_msk); 3885 if (status) { 3886 devm_kfree(ice_hw_to_dev(hw), p); 3887 goto err_ice_add_prof_id_vsig; 3888 } 3889 3890 /* log change */ 3891 list_add(&p->list_entry, chg); 3892 } 3893 3894 /* add profile to VSIG */ 3895 vsig_idx = vsig & ICE_VSIG_IDX_M; 3896 if (rev) 3897 list_add_tail(&t->list, 3898 &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst); 3899 else 3900 list_add(&t->list, 3901 &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst); 3902 3903 mutex_unlock(&hw->blk[blk].es.prof_map_lock); 3904 return status; 3905 3906 err_ice_add_prof_id_vsig: 3907 mutex_unlock(&hw->blk[blk].es.prof_map_lock); 3908 /* let caller clean up the change list */ 3909 devm_kfree(ice_hw_to_dev(hw), t); 3910 return status; 3911 } 3912 3913 /** 3914 * ice_create_prof_id_vsig - add a new VSIG with a single profile 3915 * @hw: pointer to the HW struct 3916 * @blk: hardware block 3917 * @vsi: the initial VSI that will be in VSIG 3918 * @hdl: the profile handle of the profile that will be added to the VSIG 3919 * @chg: the change list 3920 */ 3921 static int 3922 ice_create_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl, 3923 struct list_head *chg) 3924 { 3925 struct ice_chs_chg *p; 3926 u16 new_vsig; 3927 int status; 3928 3929 p = devm_kzalloc_obj(ice_hw_to_dev(hw), *p); 3930 if (!p) 3931 return -ENOMEM; 3932 3933 new_vsig = ice_vsig_alloc(hw, blk); 3934 if (!new_vsig) { 3935 status = -EIO; 3936 goto err_ice_create_prof_id_vsig; 3937 } 3938 3939 status = ice_move_vsi(hw, blk, vsi, new_vsig, chg); 3940 if (status) 3941 goto err_ice_create_prof_id_vsig; 3942 3943 status = ice_add_prof_id_vsig(hw, blk, new_vsig, hdl, false, chg); 3944 if (status) 3945 goto err_ice_create_prof_id_vsig; 3946 3947 p->type = ICE_VSIG_ADD; 3948 p->vsi = vsi; 3949 p->orig_vsig = ICE_DEFAULT_VSIG; 3950 p->vsig = new_vsig; 3951 3952 list_add(&p->list_entry, chg); 3953 3954 return 0; 3955 3956 err_ice_create_prof_id_vsig: 3957 /* let caller clean up the change list */ 3958 devm_kfree(ice_hw_to_dev(hw), p); 3959 return status; 3960 } 3961 3962 /** 3963 * ice_create_vsig_from_lst - create a new VSIG with a list of profiles 3964 * @hw: pointer to the HW struct 3965 * @blk: hardware block 3966 * @vsi: the initial VSI that will be in VSIG 3967 * @lst: the list of profile that will be added to the VSIG 3968 * @new_vsig: return of new VSIG 3969 * @chg: the change list 3970 */ 3971 static int 3972 ice_create_vsig_from_lst(struct ice_hw *hw, enum ice_block blk, u16 vsi, 3973 struct list_head *lst, u16 *new_vsig, 3974 struct list_head *chg) 3975 { 3976 struct ice_vsig_prof *t; 3977 int status; 3978 u16 vsig; 3979 3980 vsig = ice_vsig_alloc(hw, blk); 3981 if (!vsig) 3982 return -EIO; 3983 3984 status = ice_move_vsi(hw, blk, vsi, vsig, chg); 3985 if (status) 3986 return status; 3987 3988 list_for_each_entry(t, lst, list) { 3989 /* Reverse the order here since we are copying the list */ 3990 status = ice_add_prof_id_vsig(hw, blk, vsig, t->profile_cookie, 3991 true, chg); 3992 if (status) 3993 return status; 3994 } 3995 3996 *new_vsig = vsig; 3997 3998 return 0; 3999 } 4000 4001 /** 4002 * ice_find_prof_vsig - find a VSIG with a specific profile handle 4003 * @hw: pointer to the HW struct 4004 * @blk: hardware block 4005 * @hdl: the profile handle of the profile to search for 4006 * @vsig: returns the VSIG with the matching profile 4007 */ 4008 static bool 4009 ice_find_prof_vsig(struct ice_hw *hw, enum ice_block blk, u64 hdl, u16 *vsig) 4010 { 4011 struct ice_vsig_prof *t; 4012 struct list_head lst; 4013 int status; 4014 4015 INIT_LIST_HEAD(&lst); 4016 4017 t = kzalloc_obj(*t); 4018 if (!t) 4019 return false; 4020 4021 t->profile_cookie = hdl; 4022 list_add(&t->list, &lst); 4023 4024 status = ice_find_dup_props_vsig(hw, blk, &lst, vsig); 4025 4026 list_del(&t->list); 4027 kfree(t); 4028 4029 return !status; 4030 } 4031 4032 /** 4033 * ice_add_prof_id_flow - add profile flow 4034 * @hw: pointer to the HW struct 4035 * @blk: hardware block 4036 * @vsi: the VSI to enable with the profile specified by ID 4037 * @hdl: profile handle 4038 * 4039 * Calling this function will update the hardware tables to enable the 4040 * profile indicated by the ID parameter for the VSIs specified in the VSI 4041 * array. Once successfully called, the flow will be enabled. 4042 */ 4043 int 4044 ice_add_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl) 4045 { 4046 struct ice_vsig_prof *tmp1, *del1; 4047 struct ice_chs_chg *tmp, *del; 4048 struct list_head union_lst; 4049 struct list_head chg; 4050 int status; 4051 u16 vsig; 4052 4053 INIT_LIST_HEAD(&union_lst); 4054 INIT_LIST_HEAD(&chg); 4055 4056 /* Get profile */ 4057 status = ice_get_prof(hw, blk, hdl, &chg); 4058 if (status) 4059 return status; 4060 4061 /* determine if VSI is already part of a VSIG */ 4062 status = ice_vsig_find_vsi(hw, blk, vsi, &vsig); 4063 if (!status && vsig) { 4064 bool only_vsi; 4065 u16 or_vsig; 4066 u16 ref; 4067 4068 /* found in VSIG */ 4069 or_vsig = vsig; 4070 4071 /* make sure that there is no overlap/conflict between the new 4072 * characteristics and the existing ones; we don't support that 4073 * scenario 4074 */ 4075 if (ice_has_prof_vsig(hw, blk, vsig, hdl)) { 4076 status = -EEXIST; 4077 goto err_ice_add_prof_id_flow; 4078 } 4079 4080 /* last VSI in the VSIG? */ 4081 status = ice_vsig_get_ref(hw, blk, vsig, &ref); 4082 if (status) 4083 goto err_ice_add_prof_id_flow; 4084 only_vsi = (ref == 1); 4085 4086 /* create a union of the current profiles and the one being 4087 * added 4088 */ 4089 status = ice_get_profs_vsig(hw, blk, vsig, &union_lst); 4090 if (status) 4091 goto err_ice_add_prof_id_flow; 4092 4093 status = ice_add_prof_to_lst(hw, blk, &union_lst, hdl); 4094 if (status) 4095 goto err_ice_add_prof_id_flow; 4096 4097 /* search for an existing VSIG with an exact charc match */ 4098 status = ice_find_dup_props_vsig(hw, blk, &union_lst, &vsig); 4099 if (!status) { 4100 /* move VSI to the VSIG that matches */ 4101 status = ice_move_vsi(hw, blk, vsi, vsig, &chg); 4102 if (status) 4103 goto err_ice_add_prof_id_flow; 4104 4105 /* VSI has been moved out of or_vsig. If the or_vsig had 4106 * only that VSI it is now empty and can be removed. 4107 */ 4108 if (only_vsi) { 4109 status = ice_rem_vsig(hw, blk, or_vsig, &chg); 4110 if (status) 4111 goto err_ice_add_prof_id_flow; 4112 } 4113 } else if (only_vsi) { 4114 /* If the original VSIG only contains one VSI, then it 4115 * will be the requesting VSI. In this case the VSI is 4116 * not sharing entries and we can simply add the new 4117 * profile to the VSIG. 4118 */ 4119 status = ice_add_prof_id_vsig(hw, blk, vsig, hdl, false, 4120 &chg); 4121 if (status) 4122 goto err_ice_add_prof_id_flow; 4123 4124 /* Adjust priorities */ 4125 status = ice_adj_prof_priorities(hw, blk, vsig, &chg); 4126 if (status) 4127 goto err_ice_add_prof_id_flow; 4128 } else { 4129 /* No match, so we need a new VSIG */ 4130 status = ice_create_vsig_from_lst(hw, blk, vsi, 4131 &union_lst, &vsig, 4132 &chg); 4133 if (status) 4134 goto err_ice_add_prof_id_flow; 4135 4136 /* Adjust priorities */ 4137 status = ice_adj_prof_priorities(hw, blk, vsig, &chg); 4138 if (status) 4139 goto err_ice_add_prof_id_flow; 4140 } 4141 } else { 4142 /* need to find or add a VSIG */ 4143 /* search for an existing VSIG with an exact charc match */ 4144 if (ice_find_prof_vsig(hw, blk, hdl, &vsig)) { 4145 /* found an exact match */ 4146 /* add or move VSI to the VSIG that matches */ 4147 status = ice_move_vsi(hw, blk, vsi, vsig, &chg); 4148 if (status) 4149 goto err_ice_add_prof_id_flow; 4150 } else { 4151 /* we did not find an exact match */ 4152 /* we need to add a VSIG */ 4153 status = ice_create_prof_id_vsig(hw, blk, vsi, hdl, 4154 &chg); 4155 if (status) 4156 goto err_ice_add_prof_id_flow; 4157 } 4158 } 4159 4160 /* update hardware */ 4161 if (!status) 4162 status = ice_upd_prof_hw(hw, blk, &chg); 4163 4164 err_ice_add_prof_id_flow: 4165 list_for_each_entry_safe(del, tmp, &chg, list_entry) { 4166 list_del(&del->list_entry); 4167 devm_kfree(ice_hw_to_dev(hw), del); 4168 } 4169 4170 list_for_each_entry_safe(del1, tmp1, &union_lst, list) { 4171 list_del(&del1->list); 4172 devm_kfree(ice_hw_to_dev(hw), del1); 4173 } 4174 4175 return status; 4176 } 4177 4178 /** 4179 * ice_flow_assoc_fdir_prof - add an FDIR profile for main/ctrl VSI 4180 * @hw: pointer to the HW struct 4181 * @blk: HW block 4182 * @dest_vsi: dest VSI 4183 * @fdir_vsi: fdir programming VSI 4184 * @hdl: profile handle 4185 * 4186 * Update the hardware tables to enable the FDIR profile indicated by @hdl for 4187 * the VSI specified by @dest_vsi. On success, the flow will be enabled. 4188 * 4189 * Return: 0 on success or negative errno on failure. 4190 */ 4191 int 4192 ice_flow_assoc_fdir_prof(struct ice_hw *hw, enum ice_block blk, 4193 u16 dest_vsi, u16 fdir_vsi, u64 hdl) 4194 { 4195 u16 vsi_num; 4196 int status; 4197 4198 vsi_num = ice_get_hw_vsi_num(hw, dest_vsi); 4199 status = ice_add_prof_id_flow(hw, blk, vsi_num, hdl); 4200 if (status) { 4201 ice_debug(hw, ICE_DBG_FLOW, "Adding HW profile failed for main VSI flow entry: %d\n", 4202 status); 4203 return status; 4204 } 4205 4206 if (blk != ICE_BLK_FD) 4207 return 0; 4208 4209 vsi_num = ice_get_hw_vsi_num(hw, fdir_vsi); 4210 status = ice_add_prof_id_flow(hw, blk, vsi_num, hdl); 4211 if (status) { 4212 ice_debug(hw, ICE_DBG_FLOW, "Adding HW profile failed for ctrl VSI flow entry: %d\n", 4213 status); 4214 goto err; 4215 } 4216 4217 return 0; 4218 4219 err: 4220 vsi_num = ice_get_hw_vsi_num(hw, dest_vsi); 4221 ice_rem_prof_id_flow(hw, blk, vsi_num, hdl); 4222 4223 return status; 4224 } 4225 4226 /** 4227 * ice_rem_prof_from_list - remove a profile from list 4228 * @hw: pointer to the HW struct 4229 * @lst: list to remove the profile from 4230 * @hdl: the profile handle indicating the profile to remove 4231 */ 4232 static int 4233 ice_rem_prof_from_list(struct ice_hw *hw, struct list_head *lst, u64 hdl) 4234 { 4235 struct ice_vsig_prof *ent, *tmp; 4236 4237 list_for_each_entry_safe(ent, tmp, lst, list) 4238 if (ent->profile_cookie == hdl) { 4239 list_del(&ent->list); 4240 devm_kfree(ice_hw_to_dev(hw), ent); 4241 return 0; 4242 } 4243 4244 return -ENOENT; 4245 } 4246 4247 /** 4248 * ice_rem_prof_id_flow - remove flow 4249 * @hw: pointer to the HW struct 4250 * @blk: hardware block 4251 * @vsi: the VSI from which to remove the profile specified by ID 4252 * @hdl: profile tracking handle 4253 * 4254 * Calling this function will update the hardware tables to remove the 4255 * profile indicated by the ID parameter for the VSIs specified in the VSI 4256 * array. Once successfully called, the flow will be disabled. 4257 */ 4258 int 4259 ice_rem_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl) 4260 { 4261 struct ice_vsig_prof *tmp1, *del1; 4262 struct ice_chs_chg *tmp, *del; 4263 struct list_head chg, copy; 4264 int status; 4265 u16 vsig; 4266 4267 INIT_LIST_HEAD(©); 4268 INIT_LIST_HEAD(&chg); 4269 4270 /* determine if VSI is already part of a VSIG */ 4271 status = ice_vsig_find_vsi(hw, blk, vsi, &vsig); 4272 if (!status && vsig) { 4273 bool last_profile; 4274 bool only_vsi; 4275 u16 ref; 4276 4277 /* found in VSIG */ 4278 last_profile = ice_vsig_prof_id_count(hw, blk, vsig) == 1; 4279 status = ice_vsig_get_ref(hw, blk, vsig, &ref); 4280 if (status) 4281 goto err_ice_rem_prof_id_flow; 4282 only_vsi = (ref == 1); 4283 4284 if (only_vsi) { 4285 /* If the original VSIG only contains one reference, 4286 * which will be the requesting VSI, then the VSI is not 4287 * sharing entries and we can simply remove the specific 4288 * characteristics from the VSIG. 4289 */ 4290 4291 if (last_profile) { 4292 /* If there are no profiles left for this VSIG, 4293 * then simply remove the VSIG. 4294 */ 4295 status = ice_rem_vsig(hw, blk, vsig, &chg); 4296 if (status) 4297 goto err_ice_rem_prof_id_flow; 4298 } else { 4299 status = ice_rem_prof_id_vsig(hw, blk, vsig, 4300 hdl, &chg); 4301 if (status) 4302 goto err_ice_rem_prof_id_flow; 4303 4304 /* Adjust priorities */ 4305 status = ice_adj_prof_priorities(hw, blk, vsig, 4306 &chg); 4307 if (status) 4308 goto err_ice_rem_prof_id_flow; 4309 } 4310 4311 } else { 4312 /* Make a copy of the VSIG's list of Profiles */ 4313 status = ice_get_profs_vsig(hw, blk, vsig, ©); 4314 if (status) 4315 goto err_ice_rem_prof_id_flow; 4316 4317 /* Remove specified profile entry from the list */ 4318 status = ice_rem_prof_from_list(hw, ©, hdl); 4319 if (status) 4320 goto err_ice_rem_prof_id_flow; 4321 4322 if (list_empty(©)) { 4323 status = ice_move_vsi(hw, blk, vsi, 4324 ICE_DEFAULT_VSIG, &chg); 4325 if (status) 4326 goto err_ice_rem_prof_id_flow; 4327 4328 } else if (!ice_find_dup_props_vsig(hw, blk, ©, 4329 &vsig)) { 4330 /* found an exact match */ 4331 /* add or move VSI to the VSIG that matches */ 4332 /* Search for a VSIG with a matching profile 4333 * list 4334 */ 4335 4336 /* Found match, move VSI to the matching VSIG */ 4337 status = ice_move_vsi(hw, blk, vsi, vsig, &chg); 4338 if (status) 4339 goto err_ice_rem_prof_id_flow; 4340 } else { 4341 /* since no existing VSIG supports this 4342 * characteristic pattern, we need to create a 4343 * new VSIG and TCAM entries 4344 */ 4345 status = ice_create_vsig_from_lst(hw, blk, vsi, 4346 ©, &vsig, 4347 &chg); 4348 if (status) 4349 goto err_ice_rem_prof_id_flow; 4350 4351 /* Adjust priorities */ 4352 status = ice_adj_prof_priorities(hw, blk, vsig, 4353 &chg); 4354 if (status) 4355 goto err_ice_rem_prof_id_flow; 4356 } 4357 } 4358 } else { 4359 status = -ENOENT; 4360 } 4361 4362 /* update hardware tables */ 4363 if (!status) 4364 status = ice_upd_prof_hw(hw, blk, &chg); 4365 4366 err_ice_rem_prof_id_flow: 4367 list_for_each_entry_safe(del, tmp, &chg, list_entry) { 4368 list_del(&del->list_entry); 4369 devm_kfree(ice_hw_to_dev(hw), del); 4370 } 4371 4372 list_for_each_entry_safe(del1, tmp1, ©, list) { 4373 list_del(&del1->list); 4374 devm_kfree(ice_hw_to_dev(hw), del1); 4375 } 4376 4377 return status; 4378 } 4379