1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2018, Intel Corporation. */ 3 4 #include "ice.h" 5 #include "ice_base.h" 6 #include "ice_flow.h" 7 #include "ice_lib.h" 8 #include "ice_fltr.h" 9 #include "ice_dcb_lib.h" 10 #include "ice_devlink.h" 11 #include "ice_vsi_vlan_ops.h" 12 13 /** 14 * ice_vsi_type_str - maps VSI type enum to string equivalents 15 * @vsi_type: VSI type enum 16 */ 17 const char *ice_vsi_type_str(enum ice_vsi_type vsi_type) 18 { 19 switch (vsi_type) { 20 case ICE_VSI_PF: 21 return "ICE_VSI_PF"; 22 case ICE_VSI_VF: 23 return "ICE_VSI_VF"; 24 case ICE_VSI_CTRL: 25 return "ICE_VSI_CTRL"; 26 case ICE_VSI_CHNL: 27 return "ICE_VSI_CHNL"; 28 case ICE_VSI_LB: 29 return "ICE_VSI_LB"; 30 case ICE_VSI_SWITCHDEV_CTRL: 31 return "ICE_VSI_SWITCHDEV_CTRL"; 32 default: 33 return "unknown"; 34 } 35 } 36 37 /** 38 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings 39 * @vsi: the VSI being configured 40 * @ena: start or stop the Rx rings 41 * 42 * First enable/disable all of the Rx rings, flush any remaining writes, and 43 * then verify that they have all been enabled/disabled successfully. This will 44 * let all of the register writes complete when enabling/disabling the Rx rings 45 * before waiting for the change in hardware to complete. 46 */ 47 static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena) 48 { 49 int ret = 0; 50 u16 i; 51 52 ice_for_each_rxq(vsi, i) 53 ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false); 54 55 ice_flush(&vsi->back->hw); 56 57 ice_for_each_rxq(vsi, i) { 58 ret = ice_vsi_wait_one_rx_ring(vsi, ena, i); 59 if (ret) 60 break; 61 } 62 63 return ret; 64 } 65 66 /** 67 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI 68 * @vsi: VSI pointer 69 * 70 * On error: returns error code (negative) 71 * On success: returns 0 72 */ 73 static int ice_vsi_alloc_arrays(struct ice_vsi *vsi) 74 { 75 struct ice_pf *pf = vsi->back; 76 struct device *dev; 77 78 dev = ice_pf_to_dev(pf); 79 if (vsi->type == ICE_VSI_CHNL) 80 return 0; 81 82 /* allocate memory for both Tx and Rx ring pointers */ 83 vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq, 84 sizeof(*vsi->tx_rings), GFP_KERNEL); 85 if (!vsi->tx_rings) 86 return -ENOMEM; 87 88 vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq, 89 sizeof(*vsi->rx_rings), GFP_KERNEL); 90 if (!vsi->rx_rings) 91 goto err_rings; 92 93 /* txq_map needs to have enough space to track both Tx (stack) rings 94 * and XDP rings; at this point vsi->num_xdp_txq might not be set, 95 * so use num_possible_cpus() as we want to always provide XDP ring 96 * per CPU, regardless of queue count settings from user that might 97 * have come from ethtool's set_channels() callback; 98 */ 99 vsi->txq_map = devm_kcalloc(dev, (vsi->alloc_txq + num_possible_cpus()), 100 sizeof(*vsi->txq_map), GFP_KERNEL); 101 102 if (!vsi->txq_map) 103 goto err_txq_map; 104 105 vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq, 106 sizeof(*vsi->rxq_map), GFP_KERNEL); 107 if (!vsi->rxq_map) 108 goto err_rxq_map; 109 110 /* There is no need to allocate q_vectors for a loopback VSI. */ 111 if (vsi->type == ICE_VSI_LB) 112 return 0; 113 114 /* allocate memory for q_vector pointers */ 115 vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors, 116 sizeof(*vsi->q_vectors), GFP_KERNEL); 117 if (!vsi->q_vectors) 118 goto err_vectors; 119 120 return 0; 121 122 err_vectors: 123 devm_kfree(dev, vsi->rxq_map); 124 err_rxq_map: 125 devm_kfree(dev, vsi->txq_map); 126 err_txq_map: 127 devm_kfree(dev, vsi->rx_rings); 128 err_rings: 129 devm_kfree(dev, vsi->tx_rings); 130 return -ENOMEM; 131 } 132 133 /** 134 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI 135 * @vsi: the VSI being configured 136 */ 137 static void ice_vsi_set_num_desc(struct ice_vsi *vsi) 138 { 139 switch (vsi->type) { 140 case ICE_VSI_PF: 141 case ICE_VSI_SWITCHDEV_CTRL: 142 case ICE_VSI_CTRL: 143 case ICE_VSI_LB: 144 /* a user could change the values of num_[tr]x_desc using 145 * ethtool -G so we should keep those values instead of 146 * overwriting them with the defaults. 147 */ 148 if (!vsi->num_rx_desc) 149 vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC; 150 if (!vsi->num_tx_desc) 151 vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC; 152 break; 153 default: 154 dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n", 155 vsi->type); 156 break; 157 } 158 } 159 160 /** 161 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI 162 * @vsi: the VSI being configured 163 * 164 * Return 0 on success and a negative value on error 165 */ 166 static void ice_vsi_set_num_qs(struct ice_vsi *vsi) 167 { 168 enum ice_vsi_type vsi_type = vsi->type; 169 struct ice_pf *pf = vsi->back; 170 struct ice_vf *vf = vsi->vf; 171 172 if (WARN_ON(vsi_type == ICE_VSI_VF && !vf)) 173 return; 174 175 switch (vsi_type) { 176 case ICE_VSI_PF: 177 if (vsi->req_txq) { 178 vsi->alloc_txq = vsi->req_txq; 179 vsi->num_txq = vsi->req_txq; 180 } else { 181 vsi->alloc_txq = min3(pf->num_lan_msix, 182 ice_get_avail_txq_count(pf), 183 (u16)num_online_cpus()); 184 } 185 186 pf->num_lan_tx = vsi->alloc_txq; 187 188 /* only 1 Rx queue unless RSS is enabled */ 189 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) { 190 vsi->alloc_rxq = 1; 191 } else { 192 if (vsi->req_rxq) { 193 vsi->alloc_rxq = vsi->req_rxq; 194 vsi->num_rxq = vsi->req_rxq; 195 } else { 196 vsi->alloc_rxq = min3(pf->num_lan_msix, 197 ice_get_avail_rxq_count(pf), 198 (u16)num_online_cpus()); 199 } 200 } 201 202 pf->num_lan_rx = vsi->alloc_rxq; 203 204 vsi->num_q_vectors = min_t(int, pf->num_lan_msix, 205 max_t(int, vsi->alloc_rxq, 206 vsi->alloc_txq)); 207 break; 208 case ICE_VSI_SWITCHDEV_CTRL: 209 /* The number of queues for ctrl VSI is equal to number of VFs. 210 * Each ring is associated to the corresponding VF_PR netdev. 211 */ 212 vsi->alloc_txq = ice_get_num_vfs(pf); 213 vsi->alloc_rxq = vsi->alloc_txq; 214 vsi->num_q_vectors = 1; 215 break; 216 case ICE_VSI_VF: 217 if (vf->num_req_qs) 218 vf->num_vf_qs = vf->num_req_qs; 219 vsi->alloc_txq = vf->num_vf_qs; 220 vsi->alloc_rxq = vf->num_vf_qs; 221 /* pf->vfs.num_msix_per includes (VF miscellaneous vector + 222 * data queue interrupts). Since vsi->num_q_vectors is number 223 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the 224 * original vector count 225 */ 226 vsi->num_q_vectors = pf->vfs.num_msix_per - ICE_NONQ_VECS_VF; 227 break; 228 case ICE_VSI_CTRL: 229 vsi->alloc_txq = 1; 230 vsi->alloc_rxq = 1; 231 vsi->num_q_vectors = 1; 232 break; 233 case ICE_VSI_CHNL: 234 vsi->alloc_txq = 0; 235 vsi->alloc_rxq = 0; 236 break; 237 case ICE_VSI_LB: 238 vsi->alloc_txq = 1; 239 vsi->alloc_rxq = 1; 240 break; 241 default: 242 dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi_type); 243 break; 244 } 245 246 ice_vsi_set_num_desc(vsi); 247 } 248 249 /** 250 * ice_get_free_slot - get the next non-NULL location index in array 251 * @array: array to search 252 * @size: size of the array 253 * @curr: last known occupied index to be used as a search hint 254 * 255 * void * is being used to keep the functionality generic. This lets us use this 256 * function on any array of pointers. 257 */ 258 static int ice_get_free_slot(void *array, int size, int curr) 259 { 260 int **tmp_array = (int **)array; 261 int next; 262 263 if (curr < (size - 1) && !tmp_array[curr + 1]) { 264 next = curr + 1; 265 } else { 266 int i = 0; 267 268 while ((i < size) && (tmp_array[i])) 269 i++; 270 if (i == size) 271 next = ICE_NO_VSI; 272 else 273 next = i; 274 } 275 return next; 276 } 277 278 /** 279 * ice_vsi_delete_from_hw - delete a VSI from the switch 280 * @vsi: pointer to VSI being removed 281 */ 282 static void ice_vsi_delete_from_hw(struct ice_vsi *vsi) 283 { 284 struct ice_pf *pf = vsi->back; 285 struct ice_vsi_ctx *ctxt; 286 int status; 287 288 ice_fltr_remove_all(vsi); 289 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL); 290 if (!ctxt) 291 return; 292 293 if (vsi->type == ICE_VSI_VF) 294 ctxt->vf_num = vsi->vf->vf_id; 295 ctxt->vsi_num = vsi->vsi_num; 296 297 memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info)); 298 299 status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL); 300 if (status) 301 dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n", 302 vsi->vsi_num, status); 303 304 kfree(ctxt); 305 } 306 307 /** 308 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI 309 * @vsi: pointer to VSI being cleared 310 */ 311 static void ice_vsi_free_arrays(struct ice_vsi *vsi) 312 { 313 struct ice_pf *pf = vsi->back; 314 struct device *dev; 315 316 dev = ice_pf_to_dev(pf); 317 318 /* free the ring and vector containers */ 319 devm_kfree(dev, vsi->q_vectors); 320 vsi->q_vectors = NULL; 321 devm_kfree(dev, vsi->tx_rings); 322 vsi->tx_rings = NULL; 323 devm_kfree(dev, vsi->rx_rings); 324 vsi->rx_rings = NULL; 325 devm_kfree(dev, vsi->txq_map); 326 vsi->txq_map = NULL; 327 devm_kfree(dev, vsi->rxq_map); 328 vsi->rxq_map = NULL; 329 } 330 331 /** 332 * ice_vsi_free_stats - Free the ring statistics structures 333 * @vsi: VSI pointer 334 */ 335 static void ice_vsi_free_stats(struct ice_vsi *vsi) 336 { 337 struct ice_vsi_stats *vsi_stat; 338 struct ice_pf *pf = vsi->back; 339 int i; 340 341 if (vsi->type == ICE_VSI_CHNL) 342 return; 343 if (!pf->vsi_stats) 344 return; 345 346 vsi_stat = pf->vsi_stats[vsi->idx]; 347 if (!vsi_stat) 348 return; 349 350 ice_for_each_alloc_txq(vsi, i) { 351 if (vsi_stat->tx_ring_stats[i]) { 352 kfree_rcu(vsi_stat->tx_ring_stats[i], rcu); 353 WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL); 354 } 355 } 356 357 ice_for_each_alloc_rxq(vsi, i) { 358 if (vsi_stat->rx_ring_stats[i]) { 359 kfree_rcu(vsi_stat->rx_ring_stats[i], rcu); 360 WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL); 361 } 362 } 363 364 kfree(vsi_stat->tx_ring_stats); 365 kfree(vsi_stat->rx_ring_stats); 366 kfree(vsi_stat); 367 pf->vsi_stats[vsi->idx] = NULL; 368 } 369 370 /** 371 * ice_vsi_alloc_ring_stats - Allocates Tx and Rx ring stats for the VSI 372 * @vsi: VSI which is having stats allocated 373 */ 374 static int ice_vsi_alloc_ring_stats(struct ice_vsi *vsi) 375 { 376 struct ice_ring_stats **tx_ring_stats; 377 struct ice_ring_stats **rx_ring_stats; 378 struct ice_vsi_stats *vsi_stats; 379 struct ice_pf *pf = vsi->back; 380 u16 i; 381 382 vsi_stats = pf->vsi_stats[vsi->idx]; 383 tx_ring_stats = vsi_stats->tx_ring_stats; 384 rx_ring_stats = vsi_stats->rx_ring_stats; 385 386 /* Allocate Tx ring stats */ 387 ice_for_each_alloc_txq(vsi, i) { 388 struct ice_ring_stats *ring_stats; 389 struct ice_tx_ring *ring; 390 391 ring = vsi->tx_rings[i]; 392 ring_stats = tx_ring_stats[i]; 393 394 if (!ring_stats) { 395 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL); 396 if (!ring_stats) 397 goto err_out; 398 399 WRITE_ONCE(tx_ring_stats[i], ring_stats); 400 } 401 402 ring->ring_stats = ring_stats; 403 } 404 405 /* Allocate Rx ring stats */ 406 ice_for_each_alloc_rxq(vsi, i) { 407 struct ice_ring_stats *ring_stats; 408 struct ice_rx_ring *ring; 409 410 ring = vsi->rx_rings[i]; 411 ring_stats = rx_ring_stats[i]; 412 413 if (!ring_stats) { 414 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL); 415 if (!ring_stats) 416 goto err_out; 417 418 WRITE_ONCE(rx_ring_stats[i], ring_stats); 419 } 420 421 ring->ring_stats = ring_stats; 422 } 423 424 return 0; 425 426 err_out: 427 ice_vsi_free_stats(vsi); 428 return -ENOMEM; 429 } 430 431 /** 432 * ice_vsi_free - clean up and deallocate the provided VSI 433 * @vsi: pointer to VSI being cleared 434 * 435 * This deallocates the VSI's queue resources, removes it from the PF's 436 * VSI array if necessary, and deallocates the VSI 437 */ 438 static void ice_vsi_free(struct ice_vsi *vsi) 439 { 440 struct ice_pf *pf = NULL; 441 struct device *dev; 442 443 if (!vsi || !vsi->back) 444 return; 445 446 pf = vsi->back; 447 dev = ice_pf_to_dev(pf); 448 449 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) { 450 dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx); 451 return; 452 } 453 454 mutex_lock(&pf->sw_mutex); 455 /* updates the PF for this cleared VSI */ 456 457 pf->vsi[vsi->idx] = NULL; 458 pf->next_vsi = vsi->idx; 459 460 ice_vsi_free_stats(vsi); 461 ice_vsi_free_arrays(vsi); 462 mutex_destroy(&vsi->xdp_state_lock); 463 mutex_unlock(&pf->sw_mutex); 464 devm_kfree(dev, vsi); 465 } 466 467 void ice_vsi_delete(struct ice_vsi *vsi) 468 { 469 ice_vsi_delete_from_hw(vsi); 470 ice_vsi_free(vsi); 471 } 472 473 /** 474 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI 475 * @irq: interrupt number 476 * @data: pointer to a q_vector 477 */ 478 static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data) 479 { 480 struct ice_q_vector *q_vector = (struct ice_q_vector *)data; 481 482 if (!q_vector->tx.tx_ring) 483 return IRQ_HANDLED; 484 485 #define FDIR_RX_DESC_CLEAN_BUDGET 64 486 ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET); 487 ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring); 488 489 return IRQ_HANDLED; 490 } 491 492 /** 493 * ice_msix_clean_rings - MSIX mode Interrupt Handler 494 * @irq: interrupt number 495 * @data: pointer to a q_vector 496 */ 497 static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data) 498 { 499 struct ice_q_vector *q_vector = (struct ice_q_vector *)data; 500 501 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring) 502 return IRQ_HANDLED; 503 504 q_vector->total_events++; 505 506 napi_schedule(&q_vector->napi); 507 508 return IRQ_HANDLED; 509 } 510 511 static irqreturn_t ice_eswitch_msix_clean_rings(int __always_unused irq, void *data) 512 { 513 struct ice_q_vector *q_vector = (struct ice_q_vector *)data; 514 struct ice_pf *pf = q_vector->vsi->back; 515 struct ice_vf *vf; 516 unsigned int bkt; 517 518 if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring) 519 return IRQ_HANDLED; 520 521 rcu_read_lock(); 522 ice_for_each_vf_rcu(pf, bkt, vf) 523 napi_schedule(&vf->repr->q_vector->napi); 524 rcu_read_unlock(); 525 526 return IRQ_HANDLED; 527 } 528 529 /** 530 * ice_vsi_alloc_stat_arrays - Allocate statistics arrays 531 * @vsi: VSI pointer 532 */ 533 static int ice_vsi_alloc_stat_arrays(struct ice_vsi *vsi) 534 { 535 struct ice_vsi_stats *vsi_stat; 536 struct ice_pf *pf = vsi->back; 537 538 if (vsi->type == ICE_VSI_CHNL) 539 return 0; 540 if (!pf->vsi_stats) 541 return -ENOENT; 542 543 if (pf->vsi_stats[vsi->idx]) 544 /* realloc will happen in rebuild path */ 545 return 0; 546 547 vsi_stat = kzalloc(sizeof(*vsi_stat), GFP_KERNEL); 548 if (!vsi_stat) 549 return -ENOMEM; 550 551 vsi_stat->tx_ring_stats = 552 kcalloc(vsi->alloc_txq, sizeof(*vsi_stat->tx_ring_stats), 553 GFP_KERNEL); 554 if (!vsi_stat->tx_ring_stats) 555 goto err_alloc_tx; 556 557 vsi_stat->rx_ring_stats = 558 kcalloc(vsi->alloc_rxq, sizeof(*vsi_stat->rx_ring_stats), 559 GFP_KERNEL); 560 if (!vsi_stat->rx_ring_stats) 561 goto err_alloc_rx; 562 563 pf->vsi_stats[vsi->idx] = vsi_stat; 564 565 return 0; 566 567 err_alloc_rx: 568 kfree(vsi_stat->rx_ring_stats); 569 err_alloc_tx: 570 kfree(vsi_stat->tx_ring_stats); 571 kfree(vsi_stat); 572 pf->vsi_stats[vsi->idx] = NULL; 573 return -ENOMEM; 574 } 575 576 /** 577 * ice_vsi_alloc_def - set default values for already allocated VSI 578 * @vsi: ptr to VSI 579 * @ch: ptr to channel 580 */ 581 static int 582 ice_vsi_alloc_def(struct ice_vsi *vsi, struct ice_channel *ch) 583 { 584 if (vsi->type != ICE_VSI_CHNL) { 585 ice_vsi_set_num_qs(vsi); 586 if (ice_vsi_alloc_arrays(vsi)) 587 return -ENOMEM; 588 } 589 590 switch (vsi->type) { 591 case ICE_VSI_SWITCHDEV_CTRL: 592 /* Setup eswitch MSIX irq handler for VSI */ 593 vsi->irq_handler = ice_eswitch_msix_clean_rings; 594 break; 595 case ICE_VSI_PF: 596 /* Setup default MSIX irq handler for VSI */ 597 vsi->irq_handler = ice_msix_clean_rings; 598 break; 599 case ICE_VSI_CTRL: 600 /* Setup ctrl VSI MSIX irq handler */ 601 vsi->irq_handler = ice_msix_clean_ctrl_vsi; 602 break; 603 case ICE_VSI_CHNL: 604 if (!ch) 605 return -EINVAL; 606 607 vsi->num_rxq = ch->num_rxq; 608 vsi->num_txq = ch->num_txq; 609 vsi->next_base_q = ch->base_q; 610 break; 611 case ICE_VSI_VF: 612 case ICE_VSI_LB: 613 break; 614 default: 615 ice_vsi_free_arrays(vsi); 616 return -EINVAL; 617 } 618 619 return 0; 620 } 621 622 /** 623 * ice_vsi_alloc - Allocates the next available struct VSI in the PF 624 * @pf: board private structure 625 * 626 * Reserves a VSI index from the PF and allocates an empty VSI structure 627 * without a type. The VSI structure must later be initialized by calling 628 * ice_vsi_cfg(). 629 * 630 * returns a pointer to a VSI on success, NULL on failure. 631 */ 632 static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf) 633 { 634 struct device *dev = ice_pf_to_dev(pf); 635 struct ice_vsi *vsi = NULL; 636 637 /* Need to protect the allocation of the VSIs at the PF level */ 638 mutex_lock(&pf->sw_mutex); 639 640 /* If we have already allocated our maximum number of VSIs, 641 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index 642 * is available to be populated 643 */ 644 if (pf->next_vsi == ICE_NO_VSI) { 645 dev_dbg(dev, "out of VSI slots!\n"); 646 goto unlock_pf; 647 } 648 649 vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL); 650 if (!vsi) 651 goto unlock_pf; 652 653 vsi->back = pf; 654 set_bit(ICE_VSI_DOWN, vsi->state); 655 656 /* fill slot and make note of the index */ 657 vsi->idx = pf->next_vsi; 658 pf->vsi[pf->next_vsi] = vsi; 659 660 /* prepare pf->next_vsi for next use */ 661 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi, 662 pf->next_vsi); 663 664 mutex_init(&vsi->xdp_state_lock); 665 666 unlock_pf: 667 mutex_unlock(&pf->sw_mutex); 668 return vsi; 669 } 670 671 /** 672 * ice_alloc_fd_res - Allocate FD resource for a VSI 673 * @vsi: pointer to the ice_vsi 674 * 675 * This allocates the FD resources 676 * 677 * Returns 0 on success, -EPERM on no-op or -EIO on failure 678 */ 679 static int ice_alloc_fd_res(struct ice_vsi *vsi) 680 { 681 struct ice_pf *pf = vsi->back; 682 u32 g_val, b_val; 683 684 /* Flow Director filters are only allocated/assigned to the PF VSI or 685 * CHNL VSI which passes the traffic. The CTRL VSI is only used to 686 * add/delete filters so resources are not allocated to it 687 */ 688 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags)) 689 return -EPERM; 690 691 if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF || 692 vsi->type == ICE_VSI_CHNL)) 693 return -EPERM; 694 695 /* FD filters from guaranteed pool per VSI */ 696 g_val = pf->hw.func_caps.fd_fltr_guar; 697 if (!g_val) 698 return -EPERM; 699 700 /* FD filters from best effort pool */ 701 b_val = pf->hw.func_caps.fd_fltr_best_effort; 702 if (!b_val) 703 return -EPERM; 704 705 /* PF main VSI gets only 64 FD resources from guaranteed pool 706 * when ADQ is configured. 707 */ 708 #define ICE_PF_VSI_GFLTR 64 709 710 /* determine FD filter resources per VSI from shared(best effort) and 711 * dedicated pool 712 */ 713 if (vsi->type == ICE_VSI_PF) { 714 vsi->num_gfltr = g_val; 715 /* if MQPRIO is configured, main VSI doesn't get all FD 716 * resources from guaranteed pool. PF VSI gets 64 FD resources 717 */ 718 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) { 719 if (g_val < ICE_PF_VSI_GFLTR) 720 return -EPERM; 721 /* allow bare minimum entries for PF VSI */ 722 vsi->num_gfltr = ICE_PF_VSI_GFLTR; 723 } 724 725 /* each VSI gets same "best_effort" quota */ 726 vsi->num_bfltr = b_val; 727 } else if (vsi->type == ICE_VSI_VF) { 728 vsi->num_gfltr = 0; 729 730 /* each VSI gets same "best_effort" quota */ 731 vsi->num_bfltr = b_val; 732 } else { 733 struct ice_vsi *main_vsi; 734 int numtc; 735 736 main_vsi = ice_get_main_vsi(pf); 737 if (!main_vsi) 738 return -EPERM; 739 740 if (!main_vsi->all_numtc) 741 return -EINVAL; 742 743 /* figure out ADQ numtc */ 744 numtc = main_vsi->all_numtc - ICE_CHNL_START_TC; 745 746 /* only one TC but still asking resources for channels, 747 * invalid config 748 */ 749 if (numtc < ICE_CHNL_START_TC) 750 return -EPERM; 751 752 g_val -= ICE_PF_VSI_GFLTR; 753 /* channel VSIs gets equal share from guaranteed pool */ 754 vsi->num_gfltr = g_val / numtc; 755 756 /* each VSI gets same "best_effort" quota */ 757 vsi->num_bfltr = b_val; 758 } 759 760 return 0; 761 } 762 763 /** 764 * ice_vsi_get_qs - Assign queues from PF to VSI 765 * @vsi: the VSI to assign queues to 766 * 767 * Returns 0 on success and a negative value on error 768 */ 769 static int ice_vsi_get_qs(struct ice_vsi *vsi) 770 { 771 struct ice_pf *pf = vsi->back; 772 struct ice_qs_cfg tx_qs_cfg = { 773 .qs_mutex = &pf->avail_q_mutex, 774 .pf_map = pf->avail_txqs, 775 .pf_map_size = pf->max_pf_txqs, 776 .q_count = vsi->alloc_txq, 777 .scatter_count = ICE_MAX_SCATTER_TXQS, 778 .vsi_map = vsi->txq_map, 779 .vsi_map_offset = 0, 780 .mapping_mode = ICE_VSI_MAP_CONTIG 781 }; 782 struct ice_qs_cfg rx_qs_cfg = { 783 .qs_mutex = &pf->avail_q_mutex, 784 .pf_map = pf->avail_rxqs, 785 .pf_map_size = pf->max_pf_rxqs, 786 .q_count = vsi->alloc_rxq, 787 .scatter_count = ICE_MAX_SCATTER_RXQS, 788 .vsi_map = vsi->rxq_map, 789 .vsi_map_offset = 0, 790 .mapping_mode = ICE_VSI_MAP_CONTIG 791 }; 792 int ret; 793 794 if (vsi->type == ICE_VSI_CHNL) 795 return 0; 796 797 ret = __ice_vsi_get_qs(&tx_qs_cfg); 798 if (ret) 799 return ret; 800 vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode; 801 802 ret = __ice_vsi_get_qs(&rx_qs_cfg); 803 if (ret) 804 return ret; 805 vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode; 806 807 return 0; 808 } 809 810 /** 811 * ice_vsi_put_qs - Release queues from VSI to PF 812 * @vsi: the VSI that is going to release queues 813 */ 814 static void ice_vsi_put_qs(struct ice_vsi *vsi) 815 { 816 struct ice_pf *pf = vsi->back; 817 int i; 818 819 mutex_lock(&pf->avail_q_mutex); 820 821 ice_for_each_alloc_txq(vsi, i) { 822 clear_bit(vsi->txq_map[i], pf->avail_txqs); 823 vsi->txq_map[i] = ICE_INVAL_Q_INDEX; 824 } 825 826 ice_for_each_alloc_rxq(vsi, i) { 827 clear_bit(vsi->rxq_map[i], pf->avail_rxqs); 828 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX; 829 } 830 831 mutex_unlock(&pf->avail_q_mutex); 832 } 833 834 /** 835 * ice_is_safe_mode 836 * @pf: pointer to the PF struct 837 * 838 * returns true if driver is in safe mode, false otherwise 839 */ 840 bool ice_is_safe_mode(struct ice_pf *pf) 841 { 842 return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags); 843 } 844 845 /** 846 * ice_is_rdma_ena 847 * @pf: pointer to the PF struct 848 * 849 * returns true if RDMA is currently supported, false otherwise 850 */ 851 bool ice_is_rdma_ena(struct ice_pf *pf) 852 { 853 return test_bit(ICE_FLAG_RDMA_ENA, pf->flags); 854 } 855 856 /** 857 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration 858 * @vsi: the VSI being cleaned up 859 * 860 * This function deletes RSS input set for all flows that were configured 861 * for this VSI 862 */ 863 static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi) 864 { 865 struct ice_pf *pf = vsi->back; 866 int status; 867 868 if (ice_is_safe_mode(pf)) 869 return; 870 871 status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx); 872 if (status) 873 dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n", 874 vsi->vsi_num, status); 875 } 876 877 /** 878 * ice_rss_clean - Delete RSS related VSI structures and configuration 879 * @vsi: the VSI being removed 880 */ 881 static void ice_rss_clean(struct ice_vsi *vsi) 882 { 883 struct ice_pf *pf = vsi->back; 884 struct device *dev; 885 886 dev = ice_pf_to_dev(pf); 887 888 devm_kfree(dev, vsi->rss_hkey_user); 889 devm_kfree(dev, vsi->rss_lut_user); 890 891 ice_vsi_clean_rss_flow_fld(vsi); 892 /* remove RSS replay list */ 893 if (!ice_is_safe_mode(pf)) 894 ice_rem_vsi_rss_list(&pf->hw, vsi->idx); 895 } 896 897 /** 898 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type 899 * @vsi: the VSI being configured 900 */ 901 static void ice_vsi_set_rss_params(struct ice_vsi *vsi) 902 { 903 struct ice_hw_common_caps *cap; 904 struct ice_pf *pf = vsi->back; 905 u16 max_rss_size; 906 907 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) { 908 vsi->rss_size = 1; 909 return; 910 } 911 912 cap = &pf->hw.func_caps.common_cap; 913 max_rss_size = BIT(cap->rss_table_entry_width); 914 switch (vsi->type) { 915 case ICE_VSI_CHNL: 916 case ICE_VSI_PF: 917 /* PF VSI will inherit RSS instance of PF */ 918 vsi->rss_table_size = (u16)cap->rss_table_size; 919 if (vsi->type == ICE_VSI_CHNL) 920 vsi->rss_size = min_t(u16, vsi->num_rxq, max_rss_size); 921 else 922 vsi->rss_size = min_t(u16, num_online_cpus(), 923 max_rss_size); 924 vsi->rss_lut_type = ICE_LUT_PF; 925 break; 926 case ICE_VSI_SWITCHDEV_CTRL: 927 vsi->rss_table_size = ICE_LUT_VSI_SIZE; 928 vsi->rss_size = min_t(u16, num_online_cpus(), max_rss_size); 929 vsi->rss_lut_type = ICE_LUT_VSI; 930 break; 931 case ICE_VSI_VF: 932 /* VF VSI will get a small RSS table. 933 * For VSI_LUT, LUT size should be set to 64 bytes. 934 */ 935 vsi->rss_table_size = ICE_LUT_VSI_SIZE; 936 vsi->rss_size = ICE_MAX_RSS_QS_PER_VF; 937 vsi->rss_lut_type = ICE_LUT_VSI; 938 break; 939 case ICE_VSI_LB: 940 break; 941 default: 942 dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n", 943 ice_vsi_type_str(vsi->type)); 944 break; 945 } 946 } 947 948 /** 949 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI 950 * @hw: HW structure used to determine the VLAN mode of the device 951 * @ctxt: the VSI context being set 952 * 953 * This initializes a default VSI context for all sections except the Queues. 954 */ 955 static void ice_set_dflt_vsi_ctx(struct ice_hw *hw, struct ice_vsi_ctx *ctxt) 956 { 957 u32 table = 0; 958 959 memset(&ctxt->info, 0, sizeof(ctxt->info)); 960 /* VSI's should be allocated from shared pool */ 961 ctxt->alloc_from_pool = true; 962 /* Src pruning enabled by default */ 963 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE; 964 /* Traffic from VSI can be sent to LAN */ 965 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA; 966 /* allow all untagged/tagged packets by default on Tx */ 967 ctxt->info.inner_vlan_flags = ((ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL & 968 ICE_AQ_VSI_INNER_VLAN_TX_MODE_M) >> 969 ICE_AQ_VSI_INNER_VLAN_TX_MODE_S); 970 /* SVM - by default bits 3 and 4 in inner_vlan_flags are 0's which 971 * results in legacy behavior (show VLAN, DEI, and UP) in descriptor. 972 * 973 * DVM - leave inner VLAN in packet by default 974 */ 975 if (ice_is_dvm_ena(hw)) { 976 ctxt->info.inner_vlan_flags |= 977 FIELD_PREP(ICE_AQ_VSI_INNER_VLAN_EMODE_M, 978 ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING); 979 ctxt->info.outer_vlan_flags = 980 (ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL << 981 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_S) & 982 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M; 983 ctxt->info.outer_vlan_flags |= 984 (ICE_AQ_VSI_OUTER_TAG_VLAN_8100 << 985 ICE_AQ_VSI_OUTER_TAG_TYPE_S) & 986 ICE_AQ_VSI_OUTER_TAG_TYPE_M; 987 ctxt->info.outer_vlan_flags |= 988 FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_EMODE_M, 989 ICE_AQ_VSI_OUTER_VLAN_EMODE_NOTHING); 990 } 991 /* Have 1:1 UP mapping for both ingress/egress tables */ 992 table |= ICE_UP_TABLE_TRANSLATE(0, 0); 993 table |= ICE_UP_TABLE_TRANSLATE(1, 1); 994 table |= ICE_UP_TABLE_TRANSLATE(2, 2); 995 table |= ICE_UP_TABLE_TRANSLATE(3, 3); 996 table |= ICE_UP_TABLE_TRANSLATE(4, 4); 997 table |= ICE_UP_TABLE_TRANSLATE(5, 5); 998 table |= ICE_UP_TABLE_TRANSLATE(6, 6); 999 table |= ICE_UP_TABLE_TRANSLATE(7, 7); 1000 ctxt->info.ingress_table = cpu_to_le32(table); 1001 ctxt->info.egress_table = cpu_to_le32(table); 1002 /* Have 1:1 UP mapping for outer to inner UP table */ 1003 ctxt->info.outer_up_table = cpu_to_le32(table); 1004 /* No Outer tag support outer_tag_flags remains to zero */ 1005 } 1006 1007 /** 1008 * ice_vsi_setup_q_map - Setup a VSI queue map 1009 * @vsi: the VSI being configured 1010 * @ctxt: VSI context structure 1011 */ 1012 static int ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt) 1013 { 1014 u16 offset = 0, qmap = 0, tx_count = 0, rx_count = 0, pow = 0; 1015 u16 num_txq_per_tc, num_rxq_per_tc; 1016 u16 qcount_tx = vsi->alloc_txq; 1017 u16 qcount_rx = vsi->alloc_rxq; 1018 u8 netdev_tc = 0; 1019 int i; 1020 1021 if (!vsi->tc_cfg.numtc) { 1022 /* at least TC0 should be enabled by default */ 1023 vsi->tc_cfg.numtc = 1; 1024 vsi->tc_cfg.ena_tc = 1; 1025 } 1026 1027 num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC); 1028 if (!num_rxq_per_tc) 1029 num_rxq_per_tc = 1; 1030 num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc; 1031 if (!num_txq_per_tc) 1032 num_txq_per_tc = 1; 1033 1034 /* find the (rounded up) power-of-2 of qcount */ 1035 pow = (u16)order_base_2(num_rxq_per_tc); 1036 1037 /* TC mapping is a function of the number of Rx queues assigned to the 1038 * VSI for each traffic class and the offset of these queues. 1039 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of 1040 * queues allocated to TC0. No:of queues is a power-of-2. 1041 * 1042 * If TC is not enabled, the queue offset is set to 0, and allocate one 1043 * queue, this way, traffic for the given TC will be sent to the default 1044 * queue. 1045 * 1046 * Setup number and offset of Rx queues for all TCs for the VSI 1047 */ 1048 ice_for_each_traffic_class(i) { 1049 if (!(vsi->tc_cfg.ena_tc & BIT(i))) { 1050 /* TC is not enabled */ 1051 vsi->tc_cfg.tc_info[i].qoffset = 0; 1052 vsi->tc_cfg.tc_info[i].qcount_rx = 1; 1053 vsi->tc_cfg.tc_info[i].qcount_tx = 1; 1054 vsi->tc_cfg.tc_info[i].netdev_tc = 0; 1055 ctxt->info.tc_mapping[i] = 0; 1056 continue; 1057 } 1058 1059 /* TC is enabled */ 1060 vsi->tc_cfg.tc_info[i].qoffset = offset; 1061 vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc; 1062 vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc; 1063 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++; 1064 1065 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) & 1066 ICE_AQ_VSI_TC_Q_OFFSET_M) | 1067 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) & 1068 ICE_AQ_VSI_TC_Q_NUM_M); 1069 offset += num_rxq_per_tc; 1070 tx_count += num_txq_per_tc; 1071 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap); 1072 } 1073 1074 /* if offset is non-zero, means it is calculated correctly based on 1075 * enabled TCs for a given VSI otherwise qcount_rx will always 1076 * be correct and non-zero because it is based off - VSI's 1077 * allocated Rx queues which is at least 1 (hence qcount_tx will be 1078 * at least 1) 1079 */ 1080 if (offset) 1081 rx_count = offset; 1082 else 1083 rx_count = num_rxq_per_tc; 1084 1085 if (rx_count > vsi->alloc_rxq) { 1086 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n", 1087 rx_count, vsi->alloc_rxq); 1088 return -EINVAL; 1089 } 1090 1091 if (tx_count > vsi->alloc_txq) { 1092 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n", 1093 tx_count, vsi->alloc_txq); 1094 return -EINVAL; 1095 } 1096 1097 vsi->num_txq = tx_count; 1098 vsi->num_rxq = rx_count; 1099 1100 if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) { 1101 dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n"); 1102 /* since there is a chance that num_rxq could have been changed 1103 * in the above for loop, make num_txq equal to num_rxq. 1104 */ 1105 vsi->num_txq = vsi->num_rxq; 1106 } 1107 1108 /* Rx queue mapping */ 1109 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG); 1110 /* q_mapping buffer holds the info for the first queue allocated for 1111 * this VSI in the PF space and also the number of queues associated 1112 * with this VSI. 1113 */ 1114 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]); 1115 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq); 1116 1117 return 0; 1118 } 1119 1120 /** 1121 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI 1122 * @ctxt: the VSI context being set 1123 * @vsi: the VSI being configured 1124 */ 1125 static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi) 1126 { 1127 u8 dflt_q_group, dflt_q_prio; 1128 u16 dflt_q, report_q, val; 1129 1130 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL && 1131 vsi->type != ICE_VSI_VF && vsi->type != ICE_VSI_CHNL) 1132 return; 1133 1134 val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID; 1135 ctxt->info.valid_sections |= cpu_to_le16(val); 1136 dflt_q = 0; 1137 dflt_q_group = 0; 1138 report_q = 0; 1139 dflt_q_prio = 0; 1140 1141 /* enable flow director filtering/programming */ 1142 val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE; 1143 ctxt->info.fd_options = cpu_to_le16(val); 1144 /* max of allocated flow director filters */ 1145 ctxt->info.max_fd_fltr_dedicated = 1146 cpu_to_le16(vsi->num_gfltr); 1147 /* max of shared flow director filters any VSI may program */ 1148 ctxt->info.max_fd_fltr_shared = 1149 cpu_to_le16(vsi->num_bfltr); 1150 /* default queue index within the VSI of the default FD */ 1151 val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) & 1152 ICE_AQ_VSI_FD_DEF_Q_M); 1153 /* target queue or queue group to the FD filter */ 1154 val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) & 1155 ICE_AQ_VSI_FD_DEF_GRP_M); 1156 ctxt->info.fd_def_q = cpu_to_le16(val); 1157 /* queue index on which FD filter completion is reported */ 1158 val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) & 1159 ICE_AQ_VSI_FD_REPORT_Q_M); 1160 /* priority of the default qindex action */ 1161 val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) & 1162 ICE_AQ_VSI_FD_DEF_PRIORITY_M); 1163 ctxt->info.fd_report_opt = cpu_to_le16(val); 1164 } 1165 1166 /** 1167 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI 1168 * @ctxt: the VSI context being set 1169 * @vsi: the VSI being configured 1170 */ 1171 static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi) 1172 { 1173 u8 lut_type, hash_type; 1174 struct device *dev; 1175 struct ice_pf *pf; 1176 1177 pf = vsi->back; 1178 dev = ice_pf_to_dev(pf); 1179 1180 switch (vsi->type) { 1181 case ICE_VSI_CHNL: 1182 case ICE_VSI_PF: 1183 /* PF VSI will inherit RSS instance of PF */ 1184 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF; 1185 hash_type = ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ; 1186 break; 1187 case ICE_VSI_VF: 1188 /* VF VSI will gets a small RSS table which is a VSI LUT type */ 1189 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI; 1190 hash_type = ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ; 1191 break; 1192 default: 1193 dev_dbg(dev, "Unsupported VSI type %s\n", 1194 ice_vsi_type_str(vsi->type)); 1195 return; 1196 } 1197 1198 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) & 1199 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) | 1200 (hash_type & ICE_AQ_VSI_Q_OPT_RSS_HASH_M); 1201 } 1202 1203 static void 1204 ice_chnl_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt) 1205 { 1206 struct ice_pf *pf = vsi->back; 1207 u16 qcount, qmap; 1208 u8 offset = 0; 1209 int pow; 1210 1211 qcount = min_t(int, vsi->num_rxq, pf->num_lan_msix); 1212 1213 pow = order_base_2(qcount); 1214 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) & 1215 ICE_AQ_VSI_TC_Q_OFFSET_M) | 1216 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) & 1217 ICE_AQ_VSI_TC_Q_NUM_M); 1218 1219 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap); 1220 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG); 1221 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->next_base_q); 1222 ctxt->info.q_mapping[1] = cpu_to_le16(qcount); 1223 } 1224 1225 /** 1226 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not 1227 * @vsi: VSI to check whether or not VLAN pruning is enabled. 1228 * 1229 * returns true if Rx VLAN pruning is enabled and false otherwise. 1230 */ 1231 static bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi) 1232 { 1233 return vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA; 1234 } 1235 1236 /** 1237 * ice_vsi_init - Create and initialize a VSI 1238 * @vsi: the VSI being configured 1239 * @vsi_flags: VSI configuration flags 1240 * 1241 * Set ICE_FLAG_VSI_INIT to initialize a new VSI context, clear it to 1242 * reconfigure an existing context. 1243 * 1244 * This initializes a VSI context depending on the VSI type to be added and 1245 * passes it down to the add_vsi aq command to create a new VSI. 1246 */ 1247 static int ice_vsi_init(struct ice_vsi *vsi, u32 vsi_flags) 1248 { 1249 struct ice_pf *pf = vsi->back; 1250 struct ice_hw *hw = &pf->hw; 1251 struct ice_vsi_ctx *ctxt; 1252 struct device *dev; 1253 int ret = 0; 1254 1255 dev = ice_pf_to_dev(pf); 1256 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL); 1257 if (!ctxt) 1258 return -ENOMEM; 1259 1260 switch (vsi->type) { 1261 case ICE_VSI_CTRL: 1262 case ICE_VSI_LB: 1263 case ICE_VSI_PF: 1264 ctxt->flags = ICE_AQ_VSI_TYPE_PF; 1265 break; 1266 case ICE_VSI_SWITCHDEV_CTRL: 1267 case ICE_VSI_CHNL: 1268 ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2; 1269 break; 1270 case ICE_VSI_VF: 1271 ctxt->flags = ICE_AQ_VSI_TYPE_VF; 1272 /* VF number here is the absolute VF number (0-255) */ 1273 ctxt->vf_num = vsi->vf->vf_id + hw->func_caps.vf_base_id; 1274 break; 1275 default: 1276 ret = -ENODEV; 1277 goto out; 1278 } 1279 1280 /* Handle VLAN pruning for channel VSI if main VSI has VLAN 1281 * prune enabled 1282 */ 1283 if (vsi->type == ICE_VSI_CHNL) { 1284 struct ice_vsi *main_vsi; 1285 1286 main_vsi = ice_get_main_vsi(pf); 1287 if (main_vsi && ice_vsi_is_vlan_pruning_ena(main_vsi)) 1288 ctxt->info.sw_flags2 |= 1289 ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA; 1290 else 1291 ctxt->info.sw_flags2 &= 1292 ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA; 1293 } 1294 1295 ice_set_dflt_vsi_ctx(hw, ctxt); 1296 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) 1297 ice_set_fd_vsi_ctx(ctxt, vsi); 1298 /* if the switch is in VEB mode, allow VSI loopback */ 1299 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB) 1300 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB; 1301 1302 /* Set LUT type and HASH type if RSS is enabled */ 1303 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) && 1304 vsi->type != ICE_VSI_CTRL) { 1305 ice_set_rss_vsi_ctx(ctxt, vsi); 1306 /* if updating VSI context, make sure to set valid_section: 1307 * to indicate which section of VSI context being updated 1308 */ 1309 if (!(vsi_flags & ICE_VSI_FLAG_INIT)) 1310 ctxt->info.valid_sections |= 1311 cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID); 1312 } 1313 1314 ctxt->info.sw_id = vsi->port_info->sw_id; 1315 if (vsi->type == ICE_VSI_CHNL) { 1316 ice_chnl_vsi_setup_q_map(vsi, ctxt); 1317 } else { 1318 ret = ice_vsi_setup_q_map(vsi, ctxt); 1319 if (ret) 1320 goto out; 1321 1322 if (!(vsi_flags & ICE_VSI_FLAG_INIT)) 1323 /* means VSI being updated */ 1324 /* must to indicate which section of VSI context are 1325 * being modified 1326 */ 1327 ctxt->info.valid_sections |= 1328 cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID); 1329 } 1330 1331 /* Allow control frames out of main VSI */ 1332 if (vsi->type == ICE_VSI_PF) { 1333 ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD; 1334 ctxt->info.valid_sections |= 1335 cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID); 1336 } 1337 1338 if (vsi_flags & ICE_VSI_FLAG_INIT) { 1339 ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL); 1340 if (ret) { 1341 dev_err(dev, "Add VSI failed, err %d\n", ret); 1342 ret = -EIO; 1343 goto out; 1344 } 1345 } else { 1346 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL); 1347 if (ret) { 1348 dev_err(dev, "Update VSI failed, err %d\n", ret); 1349 ret = -EIO; 1350 goto out; 1351 } 1352 } 1353 1354 /* keep context for update VSI operations */ 1355 vsi->info = ctxt->info; 1356 1357 /* record VSI number returned */ 1358 vsi->vsi_num = ctxt->vsi_num; 1359 1360 out: 1361 kfree(ctxt); 1362 return ret; 1363 } 1364 1365 /** 1366 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI 1367 * @vsi: the VSI having rings deallocated 1368 */ 1369 static void ice_vsi_clear_rings(struct ice_vsi *vsi) 1370 { 1371 int i; 1372 1373 /* Avoid stale references by clearing map from vector to ring */ 1374 if (vsi->q_vectors) { 1375 ice_for_each_q_vector(vsi, i) { 1376 struct ice_q_vector *q_vector = vsi->q_vectors[i]; 1377 1378 if (q_vector) { 1379 q_vector->tx.tx_ring = NULL; 1380 q_vector->rx.rx_ring = NULL; 1381 } 1382 } 1383 } 1384 1385 if (vsi->tx_rings) { 1386 ice_for_each_alloc_txq(vsi, i) { 1387 if (vsi->tx_rings[i]) { 1388 kfree_rcu(vsi->tx_rings[i], rcu); 1389 WRITE_ONCE(vsi->tx_rings[i], NULL); 1390 } 1391 } 1392 } 1393 if (vsi->rx_rings) { 1394 ice_for_each_alloc_rxq(vsi, i) { 1395 if (vsi->rx_rings[i]) { 1396 kfree_rcu(vsi->rx_rings[i], rcu); 1397 WRITE_ONCE(vsi->rx_rings[i], NULL); 1398 } 1399 } 1400 } 1401 } 1402 1403 /** 1404 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI 1405 * @vsi: VSI which is having rings allocated 1406 */ 1407 static int ice_vsi_alloc_rings(struct ice_vsi *vsi) 1408 { 1409 bool dvm_ena = ice_is_dvm_ena(&vsi->back->hw); 1410 struct ice_pf *pf = vsi->back; 1411 struct device *dev; 1412 u16 i; 1413 1414 dev = ice_pf_to_dev(pf); 1415 /* Allocate Tx rings */ 1416 ice_for_each_alloc_txq(vsi, i) { 1417 struct ice_tx_ring *ring; 1418 1419 /* allocate with kzalloc(), free with kfree_rcu() */ 1420 ring = kzalloc(sizeof(*ring), GFP_KERNEL); 1421 1422 if (!ring) 1423 goto err_out; 1424 1425 ring->q_index = i; 1426 ring->reg_idx = vsi->txq_map[i]; 1427 ring->vsi = vsi; 1428 ring->tx_tstamps = &pf->ptp.port.tx; 1429 ring->dev = dev; 1430 ring->count = vsi->num_tx_desc; 1431 ring->txq_teid = ICE_INVAL_TEID; 1432 if (dvm_ena) 1433 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG2; 1434 else 1435 ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG1; 1436 WRITE_ONCE(vsi->tx_rings[i], ring); 1437 } 1438 1439 /* Allocate Rx rings */ 1440 ice_for_each_alloc_rxq(vsi, i) { 1441 struct ice_rx_ring *ring; 1442 1443 /* allocate with kzalloc(), free with kfree_rcu() */ 1444 ring = kzalloc(sizeof(*ring), GFP_KERNEL); 1445 if (!ring) 1446 goto err_out; 1447 1448 ring->q_index = i; 1449 ring->reg_idx = vsi->rxq_map[i]; 1450 ring->vsi = vsi; 1451 ring->netdev = vsi->netdev; 1452 ring->dev = dev; 1453 ring->count = vsi->num_rx_desc; 1454 ring->cached_phctime = pf->ptp.cached_phc_time; 1455 WRITE_ONCE(vsi->rx_rings[i], ring); 1456 } 1457 1458 return 0; 1459 1460 err_out: 1461 ice_vsi_clear_rings(vsi); 1462 return -ENOMEM; 1463 } 1464 1465 /** 1466 * ice_vsi_manage_rss_lut - disable/enable RSS 1467 * @vsi: the VSI being changed 1468 * @ena: boolean value indicating if this is an enable or disable request 1469 * 1470 * In the event of disable request for RSS, this function will zero out RSS 1471 * LUT, while in the event of enable request for RSS, it will reconfigure RSS 1472 * LUT. 1473 */ 1474 void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena) 1475 { 1476 u8 *lut; 1477 1478 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL); 1479 if (!lut) 1480 return; 1481 1482 if (ena) { 1483 if (vsi->rss_lut_user) 1484 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size); 1485 else 1486 ice_fill_rss_lut(lut, vsi->rss_table_size, 1487 vsi->rss_size); 1488 } 1489 1490 ice_set_rss_lut(vsi, lut, vsi->rss_table_size); 1491 kfree(lut); 1492 } 1493 1494 /** 1495 * ice_vsi_cfg_crc_strip - Configure CRC stripping for a VSI 1496 * @vsi: VSI to be configured 1497 * @disable: set to true to have FCS / CRC in the frame data 1498 */ 1499 void ice_vsi_cfg_crc_strip(struct ice_vsi *vsi, bool disable) 1500 { 1501 int i; 1502 1503 ice_for_each_rxq(vsi, i) 1504 if (disable) 1505 vsi->rx_rings[i]->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS; 1506 else 1507 vsi->rx_rings[i]->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS; 1508 } 1509 1510 /** 1511 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI 1512 * @vsi: VSI to be configured 1513 */ 1514 int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi) 1515 { 1516 struct ice_pf *pf = vsi->back; 1517 struct device *dev; 1518 u8 *lut, *key; 1519 int err; 1520 1521 dev = ice_pf_to_dev(pf); 1522 if (vsi->type == ICE_VSI_PF && vsi->ch_rss_size && 1523 (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))) { 1524 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->ch_rss_size); 1525 } else { 1526 vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq); 1527 1528 /* If orig_rss_size is valid and it is less than determined 1529 * main VSI's rss_size, update main VSI's rss_size to be 1530 * orig_rss_size so that when tc-qdisc is deleted, main VSI 1531 * RSS table gets programmed to be correct (whatever it was 1532 * to begin with (prior to setup-tc for ADQ config) 1533 */ 1534 if (vsi->orig_rss_size && vsi->rss_size < vsi->orig_rss_size && 1535 vsi->orig_rss_size <= vsi->num_rxq) { 1536 vsi->rss_size = vsi->orig_rss_size; 1537 /* now orig_rss_size is used, reset it to zero */ 1538 vsi->orig_rss_size = 0; 1539 } 1540 } 1541 1542 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL); 1543 if (!lut) 1544 return -ENOMEM; 1545 1546 if (vsi->rss_lut_user) 1547 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size); 1548 else 1549 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size); 1550 1551 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size); 1552 if (err) { 1553 dev_err(dev, "set_rss_lut failed, error %d\n", err); 1554 goto ice_vsi_cfg_rss_exit; 1555 } 1556 1557 key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL); 1558 if (!key) { 1559 err = -ENOMEM; 1560 goto ice_vsi_cfg_rss_exit; 1561 } 1562 1563 if (vsi->rss_hkey_user) 1564 memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE); 1565 else 1566 netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE); 1567 1568 err = ice_set_rss_key(vsi, key); 1569 if (err) 1570 dev_err(dev, "set_rss_key failed, error %d\n", err); 1571 1572 kfree(key); 1573 ice_vsi_cfg_rss_exit: 1574 kfree(lut); 1575 return err; 1576 } 1577 1578 /** 1579 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows 1580 * @vsi: VSI to be configured 1581 * 1582 * This function will only be called during the VF VSI setup. Upon successful 1583 * completion of package download, this function will configure default RSS 1584 * input sets for VF VSI. 1585 */ 1586 static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi) 1587 { 1588 struct ice_pf *pf = vsi->back; 1589 struct device *dev; 1590 int status; 1591 1592 dev = ice_pf_to_dev(pf); 1593 if (ice_is_safe_mode(pf)) { 1594 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n", 1595 vsi->vsi_num); 1596 return; 1597 } 1598 1599 status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA); 1600 if (status) 1601 dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n", 1602 vsi->vsi_num, status); 1603 } 1604 1605 /** 1606 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows 1607 * @vsi: VSI to be configured 1608 * 1609 * This function will only be called after successful download package call 1610 * during initialization of PF. Since the downloaded package will erase the 1611 * RSS section, this function will configure RSS input sets for different 1612 * flow types. The last profile added has the highest priority, therefore 2 1613 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles 1614 * (i.e. IPv4 src/dst TCP src/dst port). 1615 */ 1616 static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi) 1617 { 1618 u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num; 1619 struct ice_pf *pf = vsi->back; 1620 struct ice_hw *hw = &pf->hw; 1621 struct device *dev; 1622 int status; 1623 1624 dev = ice_pf_to_dev(pf); 1625 if (ice_is_safe_mode(pf)) { 1626 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n", 1627 vsi_num); 1628 return; 1629 } 1630 /* configure RSS for IPv4 with input set IP src/dst */ 1631 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4, 1632 ICE_FLOW_SEG_HDR_IPV4); 1633 if (status) 1634 dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %d\n", 1635 vsi_num, status); 1636 1637 /* configure RSS for IPv6 with input set IPv6 src/dst */ 1638 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6, 1639 ICE_FLOW_SEG_HDR_IPV6); 1640 if (status) 1641 dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %d\n", 1642 vsi_num, status); 1643 1644 /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */ 1645 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4, 1646 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4); 1647 if (status) 1648 dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %d\n", 1649 vsi_num, status); 1650 1651 /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */ 1652 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4, 1653 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4); 1654 if (status) 1655 dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %d\n", 1656 vsi_num, status); 1657 1658 /* configure RSS for sctp4 with input set IP src/dst */ 1659 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4, 1660 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4); 1661 if (status) 1662 dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %d\n", 1663 vsi_num, status); 1664 1665 /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */ 1666 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6, 1667 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6); 1668 if (status) 1669 dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %d\n", 1670 vsi_num, status); 1671 1672 /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */ 1673 status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6, 1674 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6); 1675 if (status) 1676 dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %d\n", 1677 vsi_num, status); 1678 1679 /* configure RSS for sctp6 with input set IPv6 src/dst */ 1680 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6, 1681 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6); 1682 if (status) 1683 dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %d\n", 1684 vsi_num, status); 1685 1686 status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_ESP_SPI, 1687 ICE_FLOW_SEG_HDR_ESP); 1688 if (status) 1689 dev_dbg(dev, "ice_add_rss_cfg failed for esp/spi flow, vsi = %d, error = %d\n", 1690 vsi_num, status); 1691 } 1692 1693 /** 1694 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length 1695 * @vsi: VSI 1696 */ 1697 static void ice_vsi_cfg_frame_size(struct ice_vsi *vsi) 1698 { 1699 if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) { 1700 vsi->max_frame = ICE_MAX_FRAME_LEGACY_RX; 1701 vsi->rx_buf_len = ICE_RXBUF_1664; 1702 #if (PAGE_SIZE < 8192) 1703 } else if (!ICE_2K_TOO_SMALL_WITH_PADDING && 1704 (vsi->netdev->mtu <= ETH_DATA_LEN)) { 1705 vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN; 1706 vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN; 1707 #endif 1708 } else { 1709 vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX; 1710 vsi->rx_buf_len = ICE_RXBUF_3072; 1711 } 1712 } 1713 1714 /** 1715 * ice_pf_state_is_nominal - checks the PF for nominal state 1716 * @pf: pointer to PF to check 1717 * 1718 * Check the PF's state for a collection of bits that would indicate 1719 * the PF is in a state that would inhibit normal operation for 1720 * driver functionality. 1721 * 1722 * Returns true if PF is in a nominal state, false otherwise 1723 */ 1724 bool ice_pf_state_is_nominal(struct ice_pf *pf) 1725 { 1726 DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 }; 1727 1728 if (!pf) 1729 return false; 1730 1731 bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS); 1732 if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS)) 1733 return false; 1734 1735 return true; 1736 } 1737 1738 /** 1739 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters 1740 * @vsi: the VSI to be updated 1741 */ 1742 void ice_update_eth_stats(struct ice_vsi *vsi) 1743 { 1744 struct ice_eth_stats *prev_es, *cur_es; 1745 struct ice_hw *hw = &vsi->back->hw; 1746 struct ice_pf *pf = vsi->back; 1747 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */ 1748 1749 prev_es = &vsi->eth_stats_prev; 1750 cur_es = &vsi->eth_stats; 1751 1752 if (ice_is_reset_in_progress(pf->state)) 1753 vsi->stat_offsets_loaded = false; 1754 1755 ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded, 1756 &prev_es->rx_bytes, &cur_es->rx_bytes); 1757 1758 ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded, 1759 &prev_es->rx_unicast, &cur_es->rx_unicast); 1760 1761 ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded, 1762 &prev_es->rx_multicast, &cur_es->rx_multicast); 1763 1764 ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded, 1765 &prev_es->rx_broadcast, &cur_es->rx_broadcast); 1766 1767 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded, 1768 &prev_es->rx_discards, &cur_es->rx_discards); 1769 1770 ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded, 1771 &prev_es->tx_bytes, &cur_es->tx_bytes); 1772 1773 ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded, 1774 &prev_es->tx_unicast, &cur_es->tx_unicast); 1775 1776 ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded, 1777 &prev_es->tx_multicast, &cur_es->tx_multicast); 1778 1779 ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded, 1780 &prev_es->tx_broadcast, &cur_es->tx_broadcast); 1781 1782 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded, 1783 &prev_es->tx_errors, &cur_es->tx_errors); 1784 1785 vsi->stat_offsets_loaded = true; 1786 } 1787 1788 /** 1789 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register 1790 * @hw: HW pointer 1791 * @pf_q: index of the Rx queue in the PF's queue space 1792 * @rxdid: flexible descriptor RXDID 1793 * @prio: priority for the RXDID for this queue 1794 * @ena_ts: true to enable timestamp and false to disable timestamp 1795 */ 1796 void 1797 ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio, 1798 bool ena_ts) 1799 { 1800 int regval = rd32(hw, QRXFLXP_CNTXT(pf_q)); 1801 1802 /* clear any previous values */ 1803 regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M | 1804 QRXFLXP_CNTXT_RXDID_PRIO_M | 1805 QRXFLXP_CNTXT_TS_M); 1806 1807 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) & 1808 QRXFLXP_CNTXT_RXDID_IDX_M; 1809 1810 regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) & 1811 QRXFLXP_CNTXT_RXDID_PRIO_M; 1812 1813 if (ena_ts) 1814 /* Enable TimeSync on this queue */ 1815 regval |= QRXFLXP_CNTXT_TS_M; 1816 1817 wr32(hw, QRXFLXP_CNTXT(pf_q), regval); 1818 } 1819 1820 int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx) 1821 { 1822 if (q_idx >= vsi->num_rxq) 1823 return -EINVAL; 1824 1825 return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]); 1826 } 1827 1828 int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_tx_ring **tx_rings, u16 q_idx) 1829 { 1830 struct ice_aqc_add_tx_qgrp *qg_buf; 1831 int err; 1832 1833 if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx]) 1834 return -EINVAL; 1835 1836 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL); 1837 if (!qg_buf) 1838 return -ENOMEM; 1839 1840 qg_buf->num_txqs = 1; 1841 1842 err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf); 1843 kfree(qg_buf); 1844 return err; 1845 } 1846 1847 /** 1848 * ice_vsi_cfg_rxqs - Configure the VSI for Rx 1849 * @vsi: the VSI being configured 1850 * 1851 * Return 0 on success and a negative value on error 1852 * Configure the Rx VSI for operation. 1853 */ 1854 int ice_vsi_cfg_rxqs(struct ice_vsi *vsi) 1855 { 1856 u16 i; 1857 1858 if (vsi->type == ICE_VSI_VF) 1859 goto setup_rings; 1860 1861 ice_vsi_cfg_frame_size(vsi); 1862 setup_rings: 1863 /* set up individual rings */ 1864 ice_for_each_rxq(vsi, i) { 1865 int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]); 1866 1867 if (err) 1868 return err; 1869 } 1870 1871 return 0; 1872 } 1873 1874 /** 1875 * ice_vsi_cfg_txqs - Configure the VSI for Tx 1876 * @vsi: the VSI being configured 1877 * @rings: Tx ring array to be configured 1878 * @count: number of Tx ring array elements 1879 * 1880 * Return 0 on success and a negative value on error 1881 * Configure the Tx VSI for operation. 1882 */ 1883 static int 1884 ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_tx_ring **rings, u16 count) 1885 { 1886 struct ice_aqc_add_tx_qgrp *qg_buf; 1887 u16 q_idx = 0; 1888 int err = 0; 1889 1890 qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL); 1891 if (!qg_buf) 1892 return -ENOMEM; 1893 1894 qg_buf->num_txqs = 1; 1895 1896 for (q_idx = 0; q_idx < count; q_idx++) { 1897 err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf); 1898 if (err) 1899 goto err_cfg_txqs; 1900 } 1901 1902 err_cfg_txqs: 1903 kfree(qg_buf); 1904 return err; 1905 } 1906 1907 /** 1908 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx 1909 * @vsi: the VSI being configured 1910 * 1911 * Return 0 on success and a negative value on error 1912 * Configure the Tx VSI for operation. 1913 */ 1914 int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi) 1915 { 1916 return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq); 1917 } 1918 1919 /** 1920 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI 1921 * @vsi: the VSI being configured 1922 * 1923 * Return 0 on success and a negative value on error 1924 * Configure the Tx queues dedicated for XDP in given VSI for operation. 1925 */ 1926 int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi) 1927 { 1928 int ret; 1929 int i; 1930 1931 ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq); 1932 if (ret) 1933 return ret; 1934 1935 ice_for_each_rxq(vsi, i) 1936 ice_tx_xsk_pool(vsi, i); 1937 1938 return 0; 1939 } 1940 1941 /** 1942 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value 1943 * @intrl: interrupt rate limit in usecs 1944 * @gran: interrupt rate limit granularity in usecs 1945 * 1946 * This function converts a decimal interrupt rate limit in usecs to the format 1947 * expected by firmware. 1948 */ 1949 static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran) 1950 { 1951 u32 val = intrl / gran; 1952 1953 if (val) 1954 return val | GLINT_RATE_INTRL_ENA_M; 1955 return 0; 1956 } 1957 1958 /** 1959 * ice_write_intrl - write throttle rate limit to interrupt specific register 1960 * @q_vector: pointer to interrupt specific structure 1961 * @intrl: throttle rate limit in microseconds to write 1962 */ 1963 void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl) 1964 { 1965 struct ice_hw *hw = &q_vector->vsi->back->hw; 1966 1967 wr32(hw, GLINT_RATE(q_vector->reg_idx), 1968 ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25)); 1969 } 1970 1971 static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc) 1972 { 1973 switch (rc->type) { 1974 case ICE_RX_CONTAINER: 1975 if (rc->rx_ring) 1976 return rc->rx_ring->q_vector; 1977 break; 1978 case ICE_TX_CONTAINER: 1979 if (rc->tx_ring) 1980 return rc->tx_ring->q_vector; 1981 break; 1982 default: 1983 break; 1984 } 1985 1986 return NULL; 1987 } 1988 1989 /** 1990 * __ice_write_itr - write throttle rate to register 1991 * @q_vector: pointer to interrupt data structure 1992 * @rc: pointer to ring container 1993 * @itr: throttle rate in microseconds to write 1994 */ 1995 static void __ice_write_itr(struct ice_q_vector *q_vector, 1996 struct ice_ring_container *rc, u16 itr) 1997 { 1998 struct ice_hw *hw = &q_vector->vsi->back->hw; 1999 2000 wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx), 2001 ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S); 2002 } 2003 2004 /** 2005 * ice_write_itr - write throttle rate to queue specific register 2006 * @rc: pointer to ring container 2007 * @itr: throttle rate in microseconds to write 2008 */ 2009 void ice_write_itr(struct ice_ring_container *rc, u16 itr) 2010 { 2011 struct ice_q_vector *q_vector; 2012 2013 q_vector = ice_pull_qvec_from_rc(rc); 2014 if (!q_vector) 2015 return; 2016 2017 __ice_write_itr(q_vector, rc, itr); 2018 } 2019 2020 /** 2021 * ice_set_q_vector_intrl - set up interrupt rate limiting 2022 * @q_vector: the vector to be configured 2023 * 2024 * Interrupt rate limiting is local to the vector, not per-queue so we must 2025 * detect if either ring container has dynamic moderation enabled to decide 2026 * what to set the interrupt rate limit to via INTRL settings. In the case that 2027 * dynamic moderation is disabled on both, write the value with the cached 2028 * setting to make sure INTRL register matches the user visible value. 2029 */ 2030 void ice_set_q_vector_intrl(struct ice_q_vector *q_vector) 2031 { 2032 if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) { 2033 /* in the case of dynamic enabled, cap each vector to no more 2034 * than (4 us) 250,000 ints/sec, which allows low latency 2035 * but still less than 500,000 interrupts per second, which 2036 * reduces CPU a bit in the case of the lowest latency 2037 * setting. The 4 here is a value in microseconds. 2038 */ 2039 ice_write_intrl(q_vector, 4); 2040 } else { 2041 ice_write_intrl(q_vector, q_vector->intrl); 2042 } 2043 } 2044 2045 /** 2046 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW 2047 * @vsi: the VSI being configured 2048 * 2049 * This configures MSIX mode interrupts for the PF VSI, and should not be used 2050 * for the VF VSI. 2051 */ 2052 void ice_vsi_cfg_msix(struct ice_vsi *vsi) 2053 { 2054 struct ice_pf *pf = vsi->back; 2055 struct ice_hw *hw = &pf->hw; 2056 u16 txq = 0, rxq = 0; 2057 int i, q; 2058 2059 ice_for_each_q_vector(vsi, i) { 2060 struct ice_q_vector *q_vector = vsi->q_vectors[i]; 2061 u16 reg_idx = q_vector->reg_idx; 2062 2063 ice_cfg_itr(hw, q_vector); 2064 2065 /* Both Transmit Queue Interrupt Cause Control register 2066 * and Receive Queue Interrupt Cause control register 2067 * expects MSIX_INDX field to be the vector index 2068 * within the function space and not the absolute 2069 * vector index across PF or across device. 2070 * For SR-IOV VF VSIs queue vector index always starts 2071 * with 1 since first vector index(0) is used for OICR 2072 * in VF space. Since VMDq and other PF VSIs are within 2073 * the PF function space, use the vector index that is 2074 * tracked for this PF. 2075 */ 2076 for (q = 0; q < q_vector->num_ring_tx; q++) { 2077 ice_cfg_txq_interrupt(vsi, txq, reg_idx, 2078 q_vector->tx.itr_idx); 2079 txq++; 2080 } 2081 2082 for (q = 0; q < q_vector->num_ring_rx; q++) { 2083 ice_cfg_rxq_interrupt(vsi, rxq, reg_idx, 2084 q_vector->rx.itr_idx); 2085 rxq++; 2086 } 2087 } 2088 } 2089 2090 /** 2091 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings 2092 * @vsi: the VSI whose rings are to be enabled 2093 * 2094 * Returns 0 on success and a negative value on error 2095 */ 2096 int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi) 2097 { 2098 return ice_vsi_ctrl_all_rx_rings(vsi, true); 2099 } 2100 2101 /** 2102 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings 2103 * @vsi: the VSI whose rings are to be disabled 2104 * 2105 * Returns 0 on success and a negative value on error 2106 */ 2107 int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi) 2108 { 2109 return ice_vsi_ctrl_all_rx_rings(vsi, false); 2110 } 2111 2112 /** 2113 * ice_vsi_stop_tx_rings - Disable Tx rings 2114 * @vsi: the VSI being configured 2115 * @rst_src: reset source 2116 * @rel_vmvf_num: Relative ID of VF/VM 2117 * @rings: Tx ring array to be stopped 2118 * @count: number of Tx ring array elements 2119 */ 2120 static int 2121 ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src, 2122 u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count) 2123 { 2124 u16 q_idx; 2125 2126 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS) 2127 return -EINVAL; 2128 2129 for (q_idx = 0; q_idx < count; q_idx++) { 2130 struct ice_txq_meta txq_meta = { }; 2131 int status; 2132 2133 if (!rings || !rings[q_idx]) 2134 return -EINVAL; 2135 2136 ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta); 2137 status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num, 2138 rings[q_idx], &txq_meta); 2139 2140 if (status) 2141 return status; 2142 } 2143 2144 return 0; 2145 } 2146 2147 /** 2148 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings 2149 * @vsi: the VSI being configured 2150 * @rst_src: reset source 2151 * @rel_vmvf_num: Relative ID of VF/VM 2152 */ 2153 int 2154 ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src, 2155 u16 rel_vmvf_num) 2156 { 2157 return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq); 2158 } 2159 2160 /** 2161 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings 2162 * @vsi: the VSI being configured 2163 */ 2164 int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi) 2165 { 2166 return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq); 2167 } 2168 2169 /** 2170 * ice_vsi_is_rx_queue_active 2171 * @vsi: the VSI being configured 2172 * 2173 * Return true if at least one queue is active. 2174 */ 2175 bool ice_vsi_is_rx_queue_active(struct ice_vsi *vsi) 2176 { 2177 struct ice_pf *pf = vsi->back; 2178 struct ice_hw *hw = &pf->hw; 2179 int i; 2180 2181 ice_for_each_rxq(vsi, i) { 2182 u32 rx_reg; 2183 int pf_q; 2184 2185 pf_q = vsi->rxq_map[i]; 2186 rx_reg = rd32(hw, QRX_CTRL(pf_q)); 2187 if (rx_reg & QRX_CTRL_QENA_STAT_M) 2188 return true; 2189 } 2190 2191 return false; 2192 } 2193 2194 static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi) 2195 { 2196 if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) { 2197 vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS; 2198 vsi->tc_cfg.numtc = 1; 2199 return; 2200 } 2201 2202 /* set VSI TC information based on DCB config */ 2203 ice_vsi_set_dcb_tc_cfg(vsi); 2204 } 2205 2206 /** 2207 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling 2208 * @vsi: the VSI being configured 2209 * @tx: bool to determine Tx or Rx rule 2210 * @create: bool to determine create or remove Rule 2211 */ 2212 void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create) 2213 { 2214 int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag, 2215 enum ice_sw_fwd_act_type act); 2216 struct ice_pf *pf = vsi->back; 2217 struct device *dev; 2218 int status; 2219 2220 dev = ice_pf_to_dev(pf); 2221 eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth; 2222 2223 if (tx) { 2224 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX, 2225 ICE_DROP_PACKET); 2226 } else { 2227 if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) { 2228 status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num, 2229 create); 2230 } else { 2231 status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX, 2232 ICE_FWD_TO_VSI); 2233 } 2234 } 2235 2236 if (status) 2237 dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n", 2238 create ? "adding" : "removing", tx ? "TX" : "RX", 2239 vsi->vsi_num, status); 2240 } 2241 2242 /** 2243 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it 2244 * @vsi: pointer to the VSI 2245 * 2246 * This function will allocate new scheduler aggregator now if needed and will 2247 * move specified VSI into it. 2248 */ 2249 static void ice_set_agg_vsi(struct ice_vsi *vsi) 2250 { 2251 struct device *dev = ice_pf_to_dev(vsi->back); 2252 struct ice_agg_node *agg_node_iter = NULL; 2253 u32 agg_id = ICE_INVALID_AGG_NODE_ID; 2254 struct ice_agg_node *agg_node = NULL; 2255 int node_offset, max_agg_nodes = 0; 2256 struct ice_port_info *port_info; 2257 struct ice_pf *pf = vsi->back; 2258 u32 agg_node_id_start = 0; 2259 int status; 2260 2261 /* create (as needed) scheduler aggregator node and move VSI into 2262 * corresponding aggregator node 2263 * - PF aggregator node to contains VSIs of type _PF and _CTRL 2264 * - VF aggregator nodes will contain VF VSI 2265 */ 2266 port_info = pf->hw.port_info; 2267 if (!port_info) 2268 return; 2269 2270 switch (vsi->type) { 2271 case ICE_VSI_CTRL: 2272 case ICE_VSI_CHNL: 2273 case ICE_VSI_LB: 2274 case ICE_VSI_PF: 2275 case ICE_VSI_SWITCHDEV_CTRL: 2276 max_agg_nodes = ICE_MAX_PF_AGG_NODES; 2277 agg_node_id_start = ICE_PF_AGG_NODE_ID_START; 2278 agg_node_iter = &pf->pf_agg_node[0]; 2279 break; 2280 case ICE_VSI_VF: 2281 /* user can create 'n' VFs on a given PF, but since max children 2282 * per aggregator node can be only 64. Following code handles 2283 * aggregator(s) for VF VSIs, either selects a agg_node which 2284 * was already created provided num_vsis < 64, otherwise 2285 * select next available node, which will be created 2286 */ 2287 max_agg_nodes = ICE_MAX_VF_AGG_NODES; 2288 agg_node_id_start = ICE_VF_AGG_NODE_ID_START; 2289 agg_node_iter = &pf->vf_agg_node[0]; 2290 break; 2291 default: 2292 /* other VSI type, handle later if needed */ 2293 dev_dbg(dev, "unexpected VSI type %s\n", 2294 ice_vsi_type_str(vsi->type)); 2295 return; 2296 } 2297 2298 /* find the appropriate aggregator node */ 2299 for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) { 2300 /* see if we can find space in previously created 2301 * node if num_vsis < 64, otherwise skip 2302 */ 2303 if (agg_node_iter->num_vsis && 2304 agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) { 2305 agg_node_iter++; 2306 continue; 2307 } 2308 2309 if (agg_node_iter->valid && 2310 agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) { 2311 agg_id = agg_node_iter->agg_id; 2312 agg_node = agg_node_iter; 2313 break; 2314 } 2315 2316 /* find unclaimed agg_id */ 2317 if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) { 2318 agg_id = node_offset + agg_node_id_start; 2319 agg_node = agg_node_iter; 2320 break; 2321 } 2322 /* move to next agg_node */ 2323 agg_node_iter++; 2324 } 2325 2326 if (!agg_node) 2327 return; 2328 2329 /* if selected aggregator node was not created, create it */ 2330 if (!agg_node->valid) { 2331 status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG, 2332 (u8)vsi->tc_cfg.ena_tc); 2333 if (status) { 2334 dev_err(dev, "unable to create aggregator node with agg_id %u\n", 2335 agg_id); 2336 return; 2337 } 2338 /* aggregator node is created, store the needed info */ 2339 agg_node->valid = true; 2340 agg_node->agg_id = agg_id; 2341 } 2342 2343 /* move VSI to corresponding aggregator node */ 2344 status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx, 2345 (u8)vsi->tc_cfg.ena_tc); 2346 if (status) { 2347 dev_err(dev, "unable to move VSI idx %u into aggregator %u node", 2348 vsi->idx, agg_id); 2349 return; 2350 } 2351 2352 /* keep active children count for aggregator node */ 2353 agg_node->num_vsis++; 2354 2355 /* cache the 'agg_id' in VSI, so that after reset - VSI will be moved 2356 * to aggregator node 2357 */ 2358 vsi->agg_node = agg_node; 2359 dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n", 2360 vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id, 2361 vsi->agg_node->num_vsis); 2362 } 2363 2364 static int ice_vsi_cfg_tc_lan(struct ice_pf *pf, struct ice_vsi *vsi) 2365 { 2366 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 }; 2367 struct device *dev = ice_pf_to_dev(pf); 2368 int ret, i; 2369 2370 /* configure VSI nodes based on number of queues and TC's */ 2371 ice_for_each_traffic_class(i) { 2372 if (!(vsi->tc_cfg.ena_tc & BIT(i))) 2373 continue; 2374 2375 if (vsi->type == ICE_VSI_CHNL) { 2376 if (!vsi->alloc_txq && vsi->num_txq) 2377 max_txqs[i] = vsi->num_txq; 2378 else 2379 max_txqs[i] = pf->num_lan_tx; 2380 } else { 2381 max_txqs[i] = vsi->alloc_txq; 2382 } 2383 2384 if (vsi->type == ICE_VSI_PF) 2385 max_txqs[i] += vsi->num_xdp_txq; 2386 } 2387 2388 dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc); 2389 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc, 2390 max_txqs); 2391 if (ret) { 2392 dev_err(dev, "VSI %d failed lan queue config, error %d\n", 2393 vsi->vsi_num, ret); 2394 return ret; 2395 } 2396 2397 return 0; 2398 } 2399 2400 /** 2401 * ice_vsi_cfg_def - configure default VSI based on the type 2402 * @vsi: pointer to VSI 2403 * @params: the parameters to configure this VSI with 2404 */ 2405 static int 2406 ice_vsi_cfg_def(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params) 2407 { 2408 struct device *dev = ice_pf_to_dev(vsi->back); 2409 struct ice_pf *pf = vsi->back; 2410 int ret; 2411 2412 vsi->vsw = pf->first_sw; 2413 2414 ret = ice_vsi_alloc_def(vsi, params->ch); 2415 if (ret) 2416 return ret; 2417 2418 /* allocate memory for Tx/Rx ring stat pointers */ 2419 ret = ice_vsi_alloc_stat_arrays(vsi); 2420 if (ret) 2421 goto unroll_vsi_alloc; 2422 2423 ice_alloc_fd_res(vsi); 2424 2425 ret = ice_vsi_get_qs(vsi); 2426 if (ret) { 2427 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n", 2428 vsi->idx); 2429 goto unroll_vsi_alloc_stat; 2430 } 2431 2432 /* set RSS capabilities */ 2433 ice_vsi_set_rss_params(vsi); 2434 2435 /* set TC configuration */ 2436 ice_vsi_set_tc_cfg(vsi); 2437 2438 /* create the VSI */ 2439 ret = ice_vsi_init(vsi, params->flags); 2440 if (ret) 2441 goto unroll_get_qs; 2442 2443 ice_vsi_init_vlan_ops(vsi); 2444 2445 switch (vsi->type) { 2446 case ICE_VSI_CTRL: 2447 case ICE_VSI_SWITCHDEV_CTRL: 2448 case ICE_VSI_PF: 2449 ret = ice_vsi_alloc_q_vectors(vsi); 2450 if (ret) 2451 goto unroll_vsi_init; 2452 2453 ret = ice_vsi_alloc_rings(vsi); 2454 if (ret) 2455 goto unroll_vector_base; 2456 2457 ret = ice_vsi_alloc_ring_stats(vsi); 2458 if (ret) 2459 goto unroll_vector_base; 2460 2461 ice_vsi_map_rings_to_vectors(vsi); 2462 vsi->stat_offsets_loaded = false; 2463 2464 if (ice_is_xdp_ena_vsi(vsi)) { 2465 ret = ice_vsi_determine_xdp_res(vsi); 2466 if (ret) 2467 goto unroll_vector_base; 2468 ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog, 2469 ICE_XDP_CFG_PART); 2470 if (ret) 2471 goto unroll_vector_base; 2472 } 2473 2474 /* ICE_VSI_CTRL does not need RSS so skip RSS processing */ 2475 if (vsi->type != ICE_VSI_CTRL) 2476 /* Do not exit if configuring RSS had an issue, at 2477 * least receive traffic on first queue. Hence no 2478 * need to capture return value 2479 */ 2480 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) { 2481 ice_vsi_cfg_rss_lut_key(vsi); 2482 ice_vsi_set_rss_flow_fld(vsi); 2483 } 2484 ice_init_arfs(vsi); 2485 break; 2486 case ICE_VSI_CHNL: 2487 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) { 2488 ice_vsi_cfg_rss_lut_key(vsi); 2489 ice_vsi_set_rss_flow_fld(vsi); 2490 } 2491 break; 2492 case ICE_VSI_VF: 2493 /* VF driver will take care of creating netdev for this type and 2494 * map queues to vectors through Virtchnl, PF driver only 2495 * creates a VSI and corresponding structures for bookkeeping 2496 * purpose 2497 */ 2498 ret = ice_vsi_alloc_q_vectors(vsi); 2499 if (ret) 2500 goto unroll_vsi_init; 2501 2502 ret = ice_vsi_alloc_rings(vsi); 2503 if (ret) 2504 goto unroll_alloc_q_vector; 2505 2506 ret = ice_vsi_alloc_ring_stats(vsi); 2507 if (ret) 2508 goto unroll_vector_base; 2509 2510 vsi->stat_offsets_loaded = false; 2511 2512 /* Do not exit if configuring RSS had an issue, at least 2513 * receive traffic on first queue. Hence no need to capture 2514 * return value 2515 */ 2516 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) { 2517 ice_vsi_cfg_rss_lut_key(vsi); 2518 ice_vsi_set_vf_rss_flow_fld(vsi); 2519 } 2520 break; 2521 case ICE_VSI_LB: 2522 ret = ice_vsi_alloc_rings(vsi); 2523 if (ret) 2524 goto unroll_vsi_init; 2525 2526 ret = ice_vsi_alloc_ring_stats(vsi); 2527 if (ret) 2528 goto unroll_vector_base; 2529 2530 break; 2531 default: 2532 /* clean up the resources and exit */ 2533 ret = -EINVAL; 2534 goto unroll_vsi_init; 2535 } 2536 2537 return 0; 2538 2539 unroll_vector_base: 2540 /* reclaim SW interrupts back to the common pool */ 2541 unroll_alloc_q_vector: 2542 ice_vsi_free_q_vectors(vsi); 2543 unroll_vsi_init: 2544 ice_vsi_delete_from_hw(vsi); 2545 unroll_get_qs: 2546 ice_vsi_put_qs(vsi); 2547 unroll_vsi_alloc_stat: 2548 ice_vsi_free_stats(vsi); 2549 unroll_vsi_alloc: 2550 ice_vsi_free_arrays(vsi); 2551 return ret; 2552 } 2553 2554 /** 2555 * ice_vsi_cfg - configure a previously allocated VSI 2556 * @vsi: pointer to VSI 2557 * @params: parameters used to configure this VSI 2558 */ 2559 int ice_vsi_cfg(struct ice_vsi *vsi, struct ice_vsi_cfg_params *params) 2560 { 2561 struct ice_pf *pf = vsi->back; 2562 int ret; 2563 2564 if (WARN_ON(params->type == ICE_VSI_VF && !params->vf)) 2565 return -EINVAL; 2566 2567 vsi->type = params->type; 2568 vsi->port_info = params->pi; 2569 2570 /* For VSIs which don't have a connected VF, this will be NULL */ 2571 vsi->vf = params->vf; 2572 2573 ret = ice_vsi_cfg_def(vsi, params); 2574 if (ret) 2575 return ret; 2576 2577 ret = ice_vsi_cfg_tc_lan(vsi->back, vsi); 2578 if (ret) 2579 ice_vsi_decfg(vsi); 2580 2581 if (vsi->type == ICE_VSI_CTRL) { 2582 if (vsi->vf) { 2583 WARN_ON(vsi->vf->ctrl_vsi_idx != ICE_NO_VSI); 2584 vsi->vf->ctrl_vsi_idx = vsi->idx; 2585 } else { 2586 WARN_ON(pf->ctrl_vsi_idx != ICE_NO_VSI); 2587 pf->ctrl_vsi_idx = vsi->idx; 2588 } 2589 } 2590 2591 return ret; 2592 } 2593 2594 /** 2595 * ice_vsi_decfg - remove all VSI configuration 2596 * @vsi: pointer to VSI 2597 */ 2598 void ice_vsi_decfg(struct ice_vsi *vsi) 2599 { 2600 struct ice_pf *pf = vsi->back; 2601 int err; 2602 2603 /* The Rx rule will only exist to remove if the LLDP FW 2604 * engine is currently stopped 2605 */ 2606 if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF && 2607 !test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags)) 2608 ice_cfg_sw_lldp(vsi, false, false); 2609 2610 ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx); 2611 err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx); 2612 if (err) 2613 dev_err(ice_pf_to_dev(pf), "Failed to remove RDMA scheduler config for VSI %u, err %d\n", 2614 vsi->vsi_num, err); 2615 2616 if (ice_is_xdp_ena_vsi(vsi)) 2617 /* return value check can be skipped here, it always returns 2618 * 0 if reset is in progress 2619 */ 2620 ice_destroy_xdp_rings(vsi, ICE_XDP_CFG_PART); 2621 2622 ice_vsi_clear_rings(vsi); 2623 ice_vsi_free_q_vectors(vsi); 2624 ice_vsi_put_qs(vsi); 2625 ice_vsi_free_arrays(vsi); 2626 2627 /* SR-IOV determines needed MSIX resources all at once instead of per 2628 * VSI since when VFs are spawned we know how many VFs there are and how 2629 * many interrupts each VF needs. SR-IOV MSIX resources are also 2630 * cleared in the same manner. 2631 */ 2632 2633 if (vsi->type == ICE_VSI_VF && 2634 vsi->agg_node && vsi->agg_node->valid) 2635 vsi->agg_node->num_vsis--; 2636 } 2637 2638 /** 2639 * ice_vsi_setup - Set up a VSI by a given type 2640 * @pf: board private structure 2641 * @params: parameters to use when creating the VSI 2642 * 2643 * This allocates the sw VSI structure and its queue resources. 2644 * 2645 * Returns pointer to the successfully allocated and configured VSI sw struct on 2646 * success, NULL on failure. 2647 */ 2648 struct ice_vsi * 2649 ice_vsi_setup(struct ice_pf *pf, struct ice_vsi_cfg_params *params) 2650 { 2651 struct device *dev = ice_pf_to_dev(pf); 2652 struct ice_vsi *vsi; 2653 int ret; 2654 2655 /* ice_vsi_setup can only initialize a new VSI, and we must have 2656 * a port_info structure for it. 2657 */ 2658 if (WARN_ON(!(params->flags & ICE_VSI_FLAG_INIT)) || 2659 WARN_ON(!params->pi)) 2660 return NULL; 2661 2662 vsi = ice_vsi_alloc(pf); 2663 if (!vsi) { 2664 dev_err(dev, "could not allocate VSI\n"); 2665 return NULL; 2666 } 2667 2668 ret = ice_vsi_cfg(vsi, params); 2669 if (ret) 2670 goto err_vsi_cfg; 2671 2672 /* Add switch rule to drop all Tx Flow Control Frames, of look up 2673 * type ETHERTYPE from VSIs, and restrict malicious VF from sending 2674 * out PAUSE or PFC frames. If enabled, FW can still send FC frames. 2675 * The rule is added once for PF VSI in order to create appropriate 2676 * recipe, since VSI/VSI list is ignored with drop action... 2677 * Also add rules to handle LLDP Tx packets. Tx LLDP packets need to 2678 * be dropped so that VFs cannot send LLDP packets to reconfig DCB 2679 * settings in the HW. 2680 */ 2681 if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF) { 2682 ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX, 2683 ICE_DROP_PACKET); 2684 ice_cfg_sw_lldp(vsi, true, true); 2685 } 2686 2687 if (!vsi->agg_node) 2688 ice_set_agg_vsi(vsi); 2689 2690 return vsi; 2691 2692 err_vsi_cfg: 2693 ice_vsi_free(vsi); 2694 2695 return NULL; 2696 } 2697 2698 /** 2699 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW 2700 * @vsi: the VSI being cleaned up 2701 */ 2702 static void ice_vsi_release_msix(struct ice_vsi *vsi) 2703 { 2704 struct ice_pf *pf = vsi->back; 2705 struct ice_hw *hw = &pf->hw; 2706 u32 txq = 0; 2707 u32 rxq = 0; 2708 int i, q; 2709 2710 ice_for_each_q_vector(vsi, i) { 2711 struct ice_q_vector *q_vector = vsi->q_vectors[i]; 2712 2713 ice_write_intrl(q_vector, 0); 2714 for (q = 0; q < q_vector->num_ring_tx; q++) { 2715 ice_write_itr(&q_vector->tx, 0); 2716 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0); 2717 if (ice_is_xdp_ena_vsi(vsi)) { 2718 u32 xdp_txq = txq + vsi->num_xdp_txq; 2719 2720 wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0); 2721 } 2722 txq++; 2723 } 2724 2725 for (q = 0; q < q_vector->num_ring_rx; q++) { 2726 ice_write_itr(&q_vector->rx, 0); 2727 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0); 2728 rxq++; 2729 } 2730 } 2731 2732 ice_flush(hw); 2733 } 2734 2735 /** 2736 * ice_vsi_free_irq - Free the IRQ association with the OS 2737 * @vsi: the VSI being configured 2738 */ 2739 void ice_vsi_free_irq(struct ice_vsi *vsi) 2740 { 2741 struct ice_pf *pf = vsi->back; 2742 int i; 2743 2744 if (!vsi->q_vectors || !vsi->irqs_ready) 2745 return; 2746 2747 ice_vsi_release_msix(vsi); 2748 if (vsi->type == ICE_VSI_VF) 2749 return; 2750 2751 vsi->irqs_ready = false; 2752 ice_free_cpu_rx_rmap(vsi); 2753 2754 ice_for_each_q_vector(vsi, i) { 2755 int irq_num; 2756 2757 irq_num = vsi->q_vectors[i]->irq.virq; 2758 2759 /* free only the irqs that were actually requested */ 2760 if (!vsi->q_vectors[i] || 2761 !(vsi->q_vectors[i]->num_ring_tx || 2762 vsi->q_vectors[i]->num_ring_rx)) 2763 continue; 2764 2765 /* clear the affinity notifier in the IRQ descriptor */ 2766 if (!IS_ENABLED(CONFIG_RFS_ACCEL)) 2767 irq_set_affinity_notifier(irq_num, NULL); 2768 2769 /* clear the affinity_mask in the IRQ descriptor */ 2770 irq_set_affinity_hint(irq_num, NULL); 2771 synchronize_irq(irq_num); 2772 devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]); 2773 } 2774 } 2775 2776 /** 2777 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues 2778 * @vsi: the VSI having resources freed 2779 */ 2780 void ice_vsi_free_tx_rings(struct ice_vsi *vsi) 2781 { 2782 int i; 2783 2784 if (!vsi->tx_rings) 2785 return; 2786 2787 ice_for_each_txq(vsi, i) 2788 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) 2789 ice_free_tx_ring(vsi->tx_rings[i]); 2790 } 2791 2792 /** 2793 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues 2794 * @vsi: the VSI having resources freed 2795 */ 2796 void ice_vsi_free_rx_rings(struct ice_vsi *vsi) 2797 { 2798 int i; 2799 2800 if (!vsi->rx_rings) 2801 return; 2802 2803 ice_for_each_rxq(vsi, i) 2804 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc) 2805 ice_free_rx_ring(vsi->rx_rings[i]); 2806 } 2807 2808 /** 2809 * ice_vsi_close - Shut down a VSI 2810 * @vsi: the VSI being shut down 2811 */ 2812 void ice_vsi_close(struct ice_vsi *vsi) 2813 { 2814 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) 2815 ice_down(vsi); 2816 2817 ice_vsi_free_irq(vsi); 2818 ice_vsi_free_tx_rings(vsi); 2819 ice_vsi_free_rx_rings(vsi); 2820 } 2821 2822 /** 2823 * ice_ena_vsi - resume a VSI 2824 * @vsi: the VSI being resume 2825 * @locked: is the rtnl_lock already held 2826 */ 2827 int ice_ena_vsi(struct ice_vsi *vsi, bool locked) 2828 { 2829 int err = 0; 2830 2831 if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state)) 2832 return 0; 2833 2834 clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state); 2835 2836 if (vsi->netdev && vsi->type == ICE_VSI_PF) { 2837 if (netif_running(vsi->netdev)) { 2838 if (!locked) 2839 rtnl_lock(); 2840 2841 err = ice_open_internal(vsi->netdev); 2842 2843 if (!locked) 2844 rtnl_unlock(); 2845 } 2846 } else if (vsi->type == ICE_VSI_CTRL) { 2847 err = ice_vsi_open_ctrl(vsi); 2848 } 2849 2850 return err; 2851 } 2852 2853 /** 2854 * ice_dis_vsi - pause a VSI 2855 * @vsi: the VSI being paused 2856 * @locked: is the rtnl_lock already held 2857 */ 2858 void ice_dis_vsi(struct ice_vsi *vsi, bool locked) 2859 { 2860 if (test_bit(ICE_VSI_DOWN, vsi->state)) 2861 return; 2862 2863 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state); 2864 2865 if (vsi->type == ICE_VSI_PF && vsi->netdev) { 2866 if (netif_running(vsi->netdev)) { 2867 if (!locked) 2868 rtnl_lock(); 2869 2870 ice_vsi_close(vsi); 2871 2872 if (!locked) 2873 rtnl_unlock(); 2874 } else { 2875 ice_vsi_close(vsi); 2876 } 2877 } else if (vsi->type == ICE_VSI_CTRL || 2878 vsi->type == ICE_VSI_SWITCHDEV_CTRL) { 2879 ice_vsi_close(vsi); 2880 } 2881 } 2882 2883 /** 2884 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI 2885 * @vsi: the VSI being un-configured 2886 */ 2887 void ice_vsi_dis_irq(struct ice_vsi *vsi) 2888 { 2889 struct ice_pf *pf = vsi->back; 2890 struct ice_hw *hw = &pf->hw; 2891 u32 val; 2892 int i; 2893 2894 /* disable interrupt causation from each queue */ 2895 if (vsi->tx_rings) { 2896 ice_for_each_txq(vsi, i) { 2897 if (vsi->tx_rings[i]) { 2898 u16 reg; 2899 2900 reg = vsi->tx_rings[i]->reg_idx; 2901 val = rd32(hw, QINT_TQCTL(reg)); 2902 val &= ~QINT_TQCTL_CAUSE_ENA_M; 2903 wr32(hw, QINT_TQCTL(reg), val); 2904 } 2905 } 2906 } 2907 2908 if (vsi->rx_rings) { 2909 ice_for_each_rxq(vsi, i) { 2910 if (vsi->rx_rings[i]) { 2911 u16 reg; 2912 2913 reg = vsi->rx_rings[i]->reg_idx; 2914 val = rd32(hw, QINT_RQCTL(reg)); 2915 val &= ~QINT_RQCTL_CAUSE_ENA_M; 2916 wr32(hw, QINT_RQCTL(reg), val); 2917 } 2918 } 2919 } 2920 2921 /* disable each interrupt */ 2922 ice_for_each_q_vector(vsi, i) { 2923 if (!vsi->q_vectors[i]) 2924 continue; 2925 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0); 2926 } 2927 2928 ice_flush(hw); 2929 2930 /* don't call synchronize_irq() for VF's from the host */ 2931 if (vsi->type == ICE_VSI_VF) 2932 return; 2933 2934 ice_for_each_q_vector(vsi, i) 2935 synchronize_irq(vsi->q_vectors[i]->irq.virq); 2936 } 2937 2938 /** 2939 * ice_vsi_release - Delete a VSI and free its resources 2940 * @vsi: the VSI being removed 2941 * 2942 * Returns 0 on success or < 0 on error 2943 */ 2944 int ice_vsi_release(struct ice_vsi *vsi) 2945 { 2946 struct ice_pf *pf; 2947 2948 if (!vsi->back) 2949 return -ENODEV; 2950 pf = vsi->back; 2951 2952 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) 2953 ice_rss_clean(vsi); 2954 2955 ice_vsi_close(vsi); 2956 ice_vsi_decfg(vsi); 2957 2958 /* retain SW VSI data structure since it is needed to unregister and 2959 * free VSI netdev when PF is not in reset recovery pending state,\ 2960 * for ex: during rmmod. 2961 */ 2962 if (!ice_is_reset_in_progress(pf->state)) 2963 ice_vsi_delete(vsi); 2964 2965 return 0; 2966 } 2967 2968 /** 2969 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors 2970 * @vsi: VSI connected with q_vectors 2971 * @coalesce: array of struct with stored coalesce 2972 * 2973 * Returns array size. 2974 */ 2975 static int 2976 ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi, 2977 struct ice_coalesce_stored *coalesce) 2978 { 2979 int i; 2980 2981 ice_for_each_q_vector(vsi, i) { 2982 struct ice_q_vector *q_vector = vsi->q_vectors[i]; 2983 2984 coalesce[i].itr_tx = q_vector->tx.itr_settings; 2985 coalesce[i].itr_rx = q_vector->rx.itr_settings; 2986 coalesce[i].intrl = q_vector->intrl; 2987 2988 if (i < vsi->num_txq) 2989 coalesce[i].tx_valid = true; 2990 if (i < vsi->num_rxq) 2991 coalesce[i].rx_valid = true; 2992 } 2993 2994 return vsi->num_q_vectors; 2995 } 2996 2997 /** 2998 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays 2999 * @vsi: VSI connected with q_vectors 3000 * @coalesce: pointer to array of struct with stored coalesce 3001 * @size: size of coalesce array 3002 * 3003 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save 3004 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce 3005 * to default value. 3006 */ 3007 static void 3008 ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi, 3009 struct ice_coalesce_stored *coalesce, int size) 3010 { 3011 struct ice_ring_container *rc; 3012 int i; 3013 3014 if ((size && !coalesce) || !vsi) 3015 return; 3016 3017 /* There are a couple of cases that have to be handled here: 3018 * 1. The case where the number of queue vectors stays the same, but 3019 * the number of Tx or Rx rings changes (the first for loop) 3020 * 2. The case where the number of queue vectors increased (the 3021 * second for loop) 3022 */ 3023 for (i = 0; i < size && i < vsi->num_q_vectors; i++) { 3024 /* There are 2 cases to handle here and they are the same for 3025 * both Tx and Rx: 3026 * if the entry was valid previously (coalesce[i].[tr]x_valid 3027 * and the loop variable is less than the number of rings 3028 * allocated, then write the previous values 3029 * 3030 * if the entry was not valid previously, but the number of 3031 * rings is less than are allocated (this means the number of 3032 * rings increased from previously), then write out the 3033 * values in the first element 3034 * 3035 * Also, always write the ITR, even if in ITR_IS_DYNAMIC 3036 * as there is no harm because the dynamic algorithm 3037 * will just overwrite. 3038 */ 3039 if (i < vsi->alloc_rxq && coalesce[i].rx_valid) { 3040 rc = &vsi->q_vectors[i]->rx; 3041 rc->itr_settings = coalesce[i].itr_rx; 3042 ice_write_itr(rc, rc->itr_setting); 3043 } else if (i < vsi->alloc_rxq) { 3044 rc = &vsi->q_vectors[i]->rx; 3045 rc->itr_settings = coalesce[0].itr_rx; 3046 ice_write_itr(rc, rc->itr_setting); 3047 } 3048 3049 if (i < vsi->alloc_txq && coalesce[i].tx_valid) { 3050 rc = &vsi->q_vectors[i]->tx; 3051 rc->itr_settings = coalesce[i].itr_tx; 3052 ice_write_itr(rc, rc->itr_setting); 3053 } else if (i < vsi->alloc_txq) { 3054 rc = &vsi->q_vectors[i]->tx; 3055 rc->itr_settings = coalesce[0].itr_tx; 3056 ice_write_itr(rc, rc->itr_setting); 3057 } 3058 3059 vsi->q_vectors[i]->intrl = coalesce[i].intrl; 3060 ice_set_q_vector_intrl(vsi->q_vectors[i]); 3061 } 3062 3063 /* the number of queue vectors increased so write whatever is in 3064 * the first element 3065 */ 3066 for (; i < vsi->num_q_vectors; i++) { 3067 /* transmit */ 3068 rc = &vsi->q_vectors[i]->tx; 3069 rc->itr_settings = coalesce[0].itr_tx; 3070 ice_write_itr(rc, rc->itr_setting); 3071 3072 /* receive */ 3073 rc = &vsi->q_vectors[i]->rx; 3074 rc->itr_settings = coalesce[0].itr_rx; 3075 ice_write_itr(rc, rc->itr_setting); 3076 3077 vsi->q_vectors[i]->intrl = coalesce[0].intrl; 3078 ice_set_q_vector_intrl(vsi->q_vectors[i]); 3079 } 3080 } 3081 3082 /** 3083 * ice_vsi_realloc_stat_arrays - Frees unused stat structures or alloc new ones 3084 * @vsi: VSI pointer 3085 */ 3086 static int 3087 ice_vsi_realloc_stat_arrays(struct ice_vsi *vsi) 3088 { 3089 u16 req_txq = vsi->req_txq ? vsi->req_txq : vsi->alloc_txq; 3090 u16 req_rxq = vsi->req_rxq ? vsi->req_rxq : vsi->alloc_rxq; 3091 struct ice_ring_stats **tx_ring_stats; 3092 struct ice_ring_stats **rx_ring_stats; 3093 struct ice_vsi_stats *vsi_stat; 3094 struct ice_pf *pf = vsi->back; 3095 u16 prev_txq = vsi->alloc_txq; 3096 u16 prev_rxq = vsi->alloc_rxq; 3097 int i; 3098 3099 vsi_stat = pf->vsi_stats[vsi->idx]; 3100 3101 if (req_txq < prev_txq) { 3102 for (i = req_txq; i < prev_txq; i++) { 3103 if (vsi_stat->tx_ring_stats[i]) { 3104 kfree_rcu(vsi_stat->tx_ring_stats[i], rcu); 3105 WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL); 3106 } 3107 } 3108 } 3109 3110 tx_ring_stats = vsi_stat->tx_ring_stats; 3111 vsi_stat->tx_ring_stats = 3112 krealloc_array(vsi_stat->tx_ring_stats, req_txq, 3113 sizeof(*vsi_stat->tx_ring_stats), 3114 GFP_KERNEL | __GFP_ZERO); 3115 if (!vsi_stat->tx_ring_stats) { 3116 vsi_stat->tx_ring_stats = tx_ring_stats; 3117 return -ENOMEM; 3118 } 3119 3120 if (req_rxq < prev_rxq) { 3121 for (i = req_rxq; i < prev_rxq; i++) { 3122 if (vsi_stat->rx_ring_stats[i]) { 3123 kfree_rcu(vsi_stat->rx_ring_stats[i], rcu); 3124 WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL); 3125 } 3126 } 3127 } 3128 3129 rx_ring_stats = vsi_stat->rx_ring_stats; 3130 vsi_stat->rx_ring_stats = 3131 krealloc_array(vsi_stat->rx_ring_stats, req_rxq, 3132 sizeof(*vsi_stat->rx_ring_stats), 3133 GFP_KERNEL | __GFP_ZERO); 3134 if (!vsi_stat->rx_ring_stats) { 3135 vsi_stat->rx_ring_stats = rx_ring_stats; 3136 return -ENOMEM; 3137 } 3138 3139 return 0; 3140 } 3141 3142 /** 3143 * ice_vsi_rebuild - Rebuild VSI after reset 3144 * @vsi: VSI to be rebuild 3145 * @vsi_flags: flags used for VSI rebuild flow 3146 * 3147 * Set vsi_flags to ICE_VSI_FLAG_INIT to initialize a new VSI, or 3148 * ICE_VSI_FLAG_NO_INIT to rebuild an existing VSI in hardware. 3149 * 3150 * Returns 0 on success and negative value on failure 3151 */ 3152 int ice_vsi_rebuild(struct ice_vsi *vsi, u32 vsi_flags) 3153 { 3154 struct ice_vsi_cfg_params params = {}; 3155 struct ice_coalesce_stored *coalesce; 3156 int prev_num_q_vectors; 3157 struct ice_pf *pf; 3158 int ret; 3159 3160 if (!vsi) 3161 return -EINVAL; 3162 3163 params = ice_vsi_to_params(vsi); 3164 params.flags = vsi_flags; 3165 3166 pf = vsi->back; 3167 if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf)) 3168 return -EINVAL; 3169 3170 mutex_lock(&vsi->xdp_state_lock); 3171 3172 ret = ice_vsi_realloc_stat_arrays(vsi); 3173 if (ret) 3174 goto unlock; 3175 3176 ice_vsi_decfg(vsi); 3177 ret = ice_vsi_cfg_def(vsi, ¶ms); 3178 if (ret) 3179 goto unlock; 3180 3181 coalesce = kcalloc(vsi->num_q_vectors, 3182 sizeof(struct ice_coalesce_stored), GFP_KERNEL); 3183 if (!coalesce) { 3184 ret = -ENOMEM; 3185 goto decfg; 3186 } 3187 3188 prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce); 3189 3190 ret = ice_vsi_cfg_tc_lan(pf, vsi); 3191 if (ret) { 3192 if (vsi_flags & ICE_VSI_FLAG_INIT) { 3193 ret = -EIO; 3194 goto free_coalesce; 3195 } 3196 3197 ret = ice_schedule_reset(pf, ICE_RESET_PFR); 3198 goto free_coalesce; 3199 } 3200 3201 ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors); 3202 clear_bit(ICE_VSI_REBUILD_PENDING, vsi->state); 3203 3204 free_coalesce: 3205 kfree(coalesce); 3206 decfg: 3207 if (ret) 3208 ice_vsi_decfg(vsi); 3209 unlock: 3210 mutex_unlock(&vsi->xdp_state_lock); 3211 return ret; 3212 } 3213 3214 /** 3215 * ice_is_reset_in_progress - check for a reset in progress 3216 * @state: PF state field 3217 */ 3218 bool ice_is_reset_in_progress(unsigned long *state) 3219 { 3220 return test_bit(ICE_RESET_OICR_RECV, state) || 3221 test_bit(ICE_PFR_REQ, state) || 3222 test_bit(ICE_CORER_REQ, state) || 3223 test_bit(ICE_GLOBR_REQ, state); 3224 } 3225 3226 /** 3227 * ice_wait_for_reset - Wait for driver to finish reset and rebuild 3228 * @pf: pointer to the PF structure 3229 * @timeout: length of time to wait, in jiffies 3230 * 3231 * Wait (sleep) for a short time until the driver finishes cleaning up from 3232 * a device reset. The caller must be able to sleep. Use this to delay 3233 * operations that could fail while the driver is cleaning up after a device 3234 * reset. 3235 * 3236 * Returns 0 on success, -EBUSY if the reset is not finished within the 3237 * timeout, and -ERESTARTSYS if the thread was interrupted. 3238 */ 3239 int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout) 3240 { 3241 long ret; 3242 3243 ret = wait_event_interruptible_timeout(pf->reset_wait_queue, 3244 !ice_is_reset_in_progress(pf->state), 3245 timeout); 3246 if (ret < 0) 3247 return ret; 3248 else if (!ret) 3249 return -EBUSY; 3250 else 3251 return 0; 3252 } 3253 3254 /** 3255 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map 3256 * @vsi: VSI being configured 3257 * @ctx: the context buffer returned from AQ VSI update command 3258 */ 3259 static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx) 3260 { 3261 vsi->info.mapping_flags = ctx->info.mapping_flags; 3262 memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping, 3263 sizeof(vsi->info.q_mapping)); 3264 memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping, 3265 sizeof(vsi->info.tc_mapping)); 3266 } 3267 3268 /** 3269 * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration 3270 * @vsi: the VSI being configured 3271 * @ena_tc: TC map to be enabled 3272 */ 3273 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc) 3274 { 3275 struct net_device *netdev = vsi->netdev; 3276 struct ice_pf *pf = vsi->back; 3277 int numtc = vsi->tc_cfg.numtc; 3278 struct ice_dcbx_cfg *dcbcfg; 3279 u8 netdev_tc; 3280 int i; 3281 3282 if (!netdev) 3283 return; 3284 3285 /* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */ 3286 if (vsi->type == ICE_VSI_CHNL) 3287 return; 3288 3289 if (!ena_tc) { 3290 netdev_reset_tc(netdev); 3291 return; 3292 } 3293 3294 if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf)) 3295 numtc = vsi->all_numtc; 3296 3297 if (netdev_set_num_tc(netdev, numtc)) 3298 return; 3299 3300 dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg; 3301 3302 ice_for_each_traffic_class(i) 3303 if (vsi->tc_cfg.ena_tc & BIT(i)) 3304 netdev_set_tc_queue(netdev, 3305 vsi->tc_cfg.tc_info[i].netdev_tc, 3306 vsi->tc_cfg.tc_info[i].qcount_tx, 3307 vsi->tc_cfg.tc_info[i].qoffset); 3308 /* setup TC queue map for CHNL TCs */ 3309 ice_for_each_chnl_tc(i) { 3310 if (!(vsi->all_enatc & BIT(i))) 3311 break; 3312 if (!vsi->mqprio_qopt.qopt.count[i]) 3313 break; 3314 netdev_set_tc_queue(netdev, i, 3315 vsi->mqprio_qopt.qopt.count[i], 3316 vsi->mqprio_qopt.qopt.offset[i]); 3317 } 3318 3319 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) 3320 return; 3321 3322 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) { 3323 u8 ets_tc = dcbcfg->etscfg.prio_table[i]; 3324 3325 /* Get the mapped netdev TC# for the UP */ 3326 netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc; 3327 netdev_set_prio_tc_map(netdev, i, netdev_tc); 3328 } 3329 } 3330 3331 /** 3332 * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config 3333 * @vsi: the VSI being configured, 3334 * @ctxt: VSI context structure 3335 * @ena_tc: number of traffic classes to enable 3336 * 3337 * Prepares VSI tc_config to have queue configurations based on MQPRIO options. 3338 */ 3339 static int 3340 ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt, 3341 u8 ena_tc) 3342 { 3343 u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap; 3344 u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0]; 3345 int tc0_qcount = vsi->mqprio_qopt.qopt.count[0]; 3346 u16 new_txq, new_rxq; 3347 u8 netdev_tc = 0; 3348 int i; 3349 3350 vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1; 3351 3352 pow = order_base_2(tc0_qcount); 3353 qmap = ((tc0_offset << ICE_AQ_VSI_TC_Q_OFFSET_S) & 3354 ICE_AQ_VSI_TC_Q_OFFSET_M) | 3355 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) & ICE_AQ_VSI_TC_Q_NUM_M); 3356 3357 ice_for_each_traffic_class(i) { 3358 if (!(vsi->tc_cfg.ena_tc & BIT(i))) { 3359 /* TC is not enabled */ 3360 vsi->tc_cfg.tc_info[i].qoffset = 0; 3361 vsi->tc_cfg.tc_info[i].qcount_rx = 1; 3362 vsi->tc_cfg.tc_info[i].qcount_tx = 1; 3363 vsi->tc_cfg.tc_info[i].netdev_tc = 0; 3364 ctxt->info.tc_mapping[i] = 0; 3365 continue; 3366 } 3367 3368 offset = vsi->mqprio_qopt.qopt.offset[i]; 3369 qcount_rx = vsi->mqprio_qopt.qopt.count[i]; 3370 qcount_tx = vsi->mqprio_qopt.qopt.count[i]; 3371 vsi->tc_cfg.tc_info[i].qoffset = offset; 3372 vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx; 3373 vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx; 3374 vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++; 3375 } 3376 3377 if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) { 3378 ice_for_each_chnl_tc(i) { 3379 if (!(vsi->all_enatc & BIT(i))) 3380 continue; 3381 offset = vsi->mqprio_qopt.qopt.offset[i]; 3382 qcount_rx = vsi->mqprio_qopt.qopt.count[i]; 3383 qcount_tx = vsi->mqprio_qopt.qopt.count[i]; 3384 } 3385 } 3386 3387 new_txq = offset + qcount_tx; 3388 if (new_txq > vsi->alloc_txq) { 3389 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n", 3390 new_txq, vsi->alloc_txq); 3391 return -EINVAL; 3392 } 3393 3394 new_rxq = offset + qcount_rx; 3395 if (new_rxq > vsi->alloc_rxq) { 3396 dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n", 3397 new_rxq, vsi->alloc_rxq); 3398 return -EINVAL; 3399 } 3400 3401 /* Set actual Tx/Rx queue pairs */ 3402 vsi->num_txq = new_txq; 3403 vsi->num_rxq = new_rxq; 3404 3405 /* Setup queue TC[0].qmap for given VSI context */ 3406 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap); 3407 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]); 3408 ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount); 3409 3410 /* Find queue count available for channel VSIs and starting offset 3411 * for channel VSIs 3412 */ 3413 if (tc0_qcount && tc0_qcount < vsi->num_rxq) { 3414 vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount; 3415 vsi->next_base_q = tc0_qcount; 3416 } 3417 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n", vsi->num_txq); 3418 dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n", vsi->num_rxq); 3419 dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n", 3420 vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc); 3421 3422 return 0; 3423 } 3424 3425 /** 3426 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map 3427 * @vsi: VSI to be configured 3428 * @ena_tc: TC bitmap 3429 * 3430 * VSI queues expected to be quiesced before calling this function 3431 */ 3432 int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc) 3433 { 3434 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 }; 3435 struct ice_pf *pf = vsi->back; 3436 struct ice_tc_cfg old_tc_cfg; 3437 struct ice_vsi_ctx *ctx; 3438 struct device *dev; 3439 int i, ret = 0; 3440 u8 num_tc = 0; 3441 3442 dev = ice_pf_to_dev(pf); 3443 if (vsi->tc_cfg.ena_tc == ena_tc && 3444 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL) 3445 return 0; 3446 3447 ice_for_each_traffic_class(i) { 3448 /* build bitmap of enabled TCs */ 3449 if (ena_tc & BIT(i)) 3450 num_tc++; 3451 /* populate max_txqs per TC */ 3452 max_txqs[i] = vsi->alloc_txq; 3453 /* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are 3454 * zero for CHNL VSI, hence use num_txq instead as max_txqs 3455 */ 3456 if (vsi->type == ICE_VSI_CHNL && 3457 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) 3458 max_txqs[i] = vsi->num_txq; 3459 } 3460 3461 memcpy(&old_tc_cfg, &vsi->tc_cfg, sizeof(old_tc_cfg)); 3462 vsi->tc_cfg.ena_tc = ena_tc; 3463 vsi->tc_cfg.numtc = num_tc; 3464 3465 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 3466 if (!ctx) 3467 return -ENOMEM; 3468 3469 ctx->vf_num = 0; 3470 ctx->info = vsi->info; 3471 3472 if (vsi->type == ICE_VSI_PF && 3473 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) 3474 ret = ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc); 3475 else 3476 ret = ice_vsi_setup_q_map(vsi, ctx); 3477 3478 if (ret) { 3479 memcpy(&vsi->tc_cfg, &old_tc_cfg, sizeof(vsi->tc_cfg)); 3480 goto out; 3481 } 3482 3483 /* must to indicate which section of VSI context are being modified */ 3484 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID); 3485 ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL); 3486 if (ret) { 3487 dev_info(dev, "Failed VSI Update\n"); 3488 goto out; 3489 } 3490 3491 if (vsi->type == ICE_VSI_PF && 3492 test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) 3493 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs); 3494 else 3495 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 3496 vsi->tc_cfg.ena_tc, max_txqs); 3497 3498 if (ret) { 3499 dev_err(dev, "VSI %d failed TC config, error %d\n", 3500 vsi->vsi_num, ret); 3501 goto out; 3502 } 3503 ice_vsi_update_q_map(vsi, ctx); 3504 vsi->info.valid_sections = 0; 3505 3506 ice_vsi_cfg_netdev_tc(vsi, ena_tc); 3507 out: 3508 kfree(ctx); 3509 return ret; 3510 } 3511 3512 /** 3513 * ice_update_ring_stats - Update ring statistics 3514 * @stats: stats to be updated 3515 * @pkts: number of processed packets 3516 * @bytes: number of processed bytes 3517 * 3518 * This function assumes that caller has acquired a u64_stats_sync lock. 3519 */ 3520 static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes) 3521 { 3522 stats->bytes += bytes; 3523 stats->pkts += pkts; 3524 } 3525 3526 /** 3527 * ice_update_tx_ring_stats - Update Tx ring specific counters 3528 * @tx_ring: ring to update 3529 * @pkts: number of processed packets 3530 * @bytes: number of processed bytes 3531 */ 3532 void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes) 3533 { 3534 u64_stats_update_begin(&tx_ring->ring_stats->syncp); 3535 ice_update_ring_stats(&tx_ring->ring_stats->stats, pkts, bytes); 3536 u64_stats_update_end(&tx_ring->ring_stats->syncp); 3537 } 3538 3539 /** 3540 * ice_update_rx_ring_stats - Update Rx ring specific counters 3541 * @rx_ring: ring to update 3542 * @pkts: number of processed packets 3543 * @bytes: number of processed bytes 3544 */ 3545 void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes) 3546 { 3547 u64_stats_update_begin(&rx_ring->ring_stats->syncp); 3548 ice_update_ring_stats(&rx_ring->ring_stats->stats, pkts, bytes); 3549 u64_stats_update_end(&rx_ring->ring_stats->syncp); 3550 } 3551 3552 /** 3553 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used 3554 * @pi: port info of the switch with default VSI 3555 * 3556 * Return true if the there is a single VSI in default forwarding VSI list 3557 */ 3558 bool ice_is_dflt_vsi_in_use(struct ice_port_info *pi) 3559 { 3560 bool exists = false; 3561 3562 ice_check_if_dflt_vsi(pi, 0, &exists); 3563 return exists; 3564 } 3565 3566 /** 3567 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI 3568 * @vsi: VSI to compare against default forwarding VSI 3569 * 3570 * If this VSI passed in is the default forwarding VSI then return true, else 3571 * return false 3572 */ 3573 bool ice_is_vsi_dflt_vsi(struct ice_vsi *vsi) 3574 { 3575 return ice_check_if_dflt_vsi(vsi->port_info, vsi->idx, NULL); 3576 } 3577 3578 /** 3579 * ice_set_dflt_vsi - set the default forwarding VSI 3580 * @vsi: VSI getting set as the default forwarding VSI on the switch 3581 * 3582 * If the VSI passed in is already the default VSI and it's enabled just return 3583 * success. 3584 * 3585 * Otherwise try to set the VSI passed in as the switch's default VSI and 3586 * return the result. 3587 */ 3588 int ice_set_dflt_vsi(struct ice_vsi *vsi) 3589 { 3590 struct device *dev; 3591 int status; 3592 3593 if (!vsi) 3594 return -EINVAL; 3595 3596 dev = ice_pf_to_dev(vsi->back); 3597 3598 if (ice_lag_is_switchdev_running(vsi->back)) { 3599 dev_dbg(dev, "VSI %d passed is a part of LAG containing interfaces in switchdev mode, nothing to do\n", 3600 vsi->vsi_num); 3601 return 0; 3602 } 3603 3604 /* the VSI passed in is already the default VSI */ 3605 if (ice_is_vsi_dflt_vsi(vsi)) { 3606 dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n", 3607 vsi->vsi_num); 3608 return 0; 3609 } 3610 3611 status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, true, ICE_FLTR_RX); 3612 if (status) { 3613 dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n", 3614 vsi->vsi_num, status); 3615 return status; 3616 } 3617 3618 return 0; 3619 } 3620 3621 /** 3622 * ice_clear_dflt_vsi - clear the default forwarding VSI 3623 * @vsi: VSI to remove from filter list 3624 * 3625 * If the switch has no default VSI or it's not enabled then return error. 3626 * 3627 * Otherwise try to clear the default VSI and return the result. 3628 */ 3629 int ice_clear_dflt_vsi(struct ice_vsi *vsi) 3630 { 3631 struct device *dev; 3632 int status; 3633 3634 if (!vsi) 3635 return -EINVAL; 3636 3637 dev = ice_pf_to_dev(vsi->back); 3638 3639 /* there is no default VSI configured */ 3640 if (!ice_is_dflt_vsi_in_use(vsi->port_info)) 3641 return -ENODEV; 3642 3643 status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, false, 3644 ICE_FLTR_RX); 3645 if (status) { 3646 dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n", 3647 vsi->vsi_num, status); 3648 return -EIO; 3649 } 3650 3651 return 0; 3652 } 3653 3654 /** 3655 * ice_get_link_speed_mbps - get link speed in Mbps 3656 * @vsi: the VSI whose link speed is being queried 3657 * 3658 * Return current VSI link speed and 0 if the speed is unknown. 3659 */ 3660 int ice_get_link_speed_mbps(struct ice_vsi *vsi) 3661 { 3662 unsigned int link_speed; 3663 3664 link_speed = vsi->port_info->phy.link_info.link_speed; 3665 3666 return (int)ice_get_link_speed(fls(link_speed) - 1); 3667 } 3668 3669 /** 3670 * ice_get_link_speed_kbps - get link speed in Kbps 3671 * @vsi: the VSI whose link speed is being queried 3672 * 3673 * Return current VSI link speed and 0 if the speed is unknown. 3674 */ 3675 int ice_get_link_speed_kbps(struct ice_vsi *vsi) 3676 { 3677 int speed_mbps; 3678 3679 speed_mbps = ice_get_link_speed_mbps(vsi); 3680 3681 return speed_mbps * 1000; 3682 } 3683 3684 /** 3685 * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate 3686 * @vsi: VSI to be configured 3687 * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit 3688 * 3689 * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit 3690 * profile, otherwise a non-zero value will force a minimum BW limit for the VSI 3691 * on TC 0. 3692 */ 3693 int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate) 3694 { 3695 struct ice_pf *pf = vsi->back; 3696 struct device *dev; 3697 int status; 3698 int speed; 3699 3700 dev = ice_pf_to_dev(pf); 3701 if (!vsi->port_info) { 3702 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n", 3703 vsi->idx, vsi->type); 3704 return -EINVAL; 3705 } 3706 3707 speed = ice_get_link_speed_kbps(vsi); 3708 if (min_tx_rate > (u64)speed) { 3709 dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n", 3710 min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx, 3711 speed); 3712 return -EINVAL; 3713 } 3714 3715 /* Configure min BW for VSI limit */ 3716 if (min_tx_rate) { 3717 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0, 3718 ICE_MIN_BW, min_tx_rate); 3719 if (status) { 3720 dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n", 3721 min_tx_rate, ice_vsi_type_str(vsi->type), 3722 vsi->idx); 3723 return status; 3724 } 3725 3726 dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n", 3727 min_tx_rate, ice_vsi_type_str(vsi->type)); 3728 } else { 3729 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info, 3730 vsi->idx, 0, 3731 ICE_MIN_BW); 3732 if (status) { 3733 dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n", 3734 ice_vsi_type_str(vsi->type), vsi->idx); 3735 return status; 3736 } 3737 3738 dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n", 3739 ice_vsi_type_str(vsi->type), vsi->idx); 3740 } 3741 3742 return 0; 3743 } 3744 3745 /** 3746 * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate 3747 * @vsi: VSI to be configured 3748 * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit 3749 * 3750 * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit 3751 * profile, otherwise a non-zero value will force a maximum BW limit for the VSI 3752 * on TC 0. 3753 */ 3754 int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate) 3755 { 3756 struct ice_pf *pf = vsi->back; 3757 struct device *dev; 3758 int status; 3759 int speed; 3760 3761 dev = ice_pf_to_dev(pf); 3762 if (!vsi->port_info) { 3763 dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n", 3764 vsi->idx, vsi->type); 3765 return -EINVAL; 3766 } 3767 3768 speed = ice_get_link_speed_kbps(vsi); 3769 if (max_tx_rate > (u64)speed) { 3770 dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n", 3771 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx, 3772 speed); 3773 return -EINVAL; 3774 } 3775 3776 /* Configure max BW for VSI limit */ 3777 if (max_tx_rate) { 3778 status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0, 3779 ICE_MAX_BW, max_tx_rate); 3780 if (status) { 3781 dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n", 3782 max_tx_rate, ice_vsi_type_str(vsi->type), 3783 vsi->idx); 3784 return status; 3785 } 3786 3787 dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n", 3788 max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx); 3789 } else { 3790 status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info, 3791 vsi->idx, 0, 3792 ICE_MAX_BW); 3793 if (status) { 3794 dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n", 3795 ice_vsi_type_str(vsi->type), vsi->idx); 3796 return status; 3797 } 3798 3799 dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n", 3800 ice_vsi_type_str(vsi->type), vsi->idx); 3801 } 3802 3803 return 0; 3804 } 3805 3806 /** 3807 * ice_set_link - turn on/off physical link 3808 * @vsi: VSI to modify physical link on 3809 * @ena: turn on/off physical link 3810 */ 3811 int ice_set_link(struct ice_vsi *vsi, bool ena) 3812 { 3813 struct device *dev = ice_pf_to_dev(vsi->back); 3814 struct ice_port_info *pi = vsi->port_info; 3815 struct ice_hw *hw = pi->hw; 3816 int status; 3817 3818 if (vsi->type != ICE_VSI_PF) 3819 return -EINVAL; 3820 3821 status = ice_aq_set_link_restart_an(pi, ena, NULL); 3822 3823 /* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE. 3824 * this is not a fatal error, so print a warning message and return 3825 * a success code. Return an error if FW returns an error code other 3826 * than ICE_AQ_RC_EMODE 3827 */ 3828 if (status == -EIO) { 3829 if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE) 3830 dev_dbg(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n", 3831 (ena ? "ON" : "OFF"), status, 3832 ice_aq_str(hw->adminq.sq_last_status)); 3833 } else if (status) { 3834 dev_err(dev, "can't set link to %s, err %d aq_err %s\n", 3835 (ena ? "ON" : "OFF"), status, 3836 ice_aq_str(hw->adminq.sq_last_status)); 3837 return status; 3838 } 3839 3840 return 0; 3841 } 3842 3843 /** 3844 * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI 3845 * @vsi: VSI used to add VLAN filters 3846 * 3847 * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based 3848 * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't 3849 * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via 3850 * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID. 3851 * 3852 * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic 3853 * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged 3854 * traffic in SVM, since the VLAN TPID isn't part of filtering. 3855 * 3856 * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be 3857 * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is 3858 * part of filtering. 3859 */ 3860 int ice_vsi_add_vlan_zero(struct ice_vsi *vsi) 3861 { 3862 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi); 3863 struct ice_vlan vlan; 3864 int err; 3865 3866 vlan = ICE_VLAN(0, 0, 0); 3867 err = vlan_ops->add_vlan(vsi, &vlan); 3868 if (err && err != -EEXIST) 3869 return err; 3870 3871 /* in SVM both VLAN 0 filters are identical */ 3872 if (!ice_is_dvm_ena(&vsi->back->hw)) 3873 return 0; 3874 3875 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0); 3876 err = vlan_ops->add_vlan(vsi, &vlan); 3877 if (err && err != -EEXIST) 3878 return err; 3879 3880 return 0; 3881 } 3882 3883 /** 3884 * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI 3885 * @vsi: VSI used to add VLAN filters 3886 * 3887 * Delete the VLAN 0 filters in the same manner that they were added in 3888 * ice_vsi_add_vlan_zero. 3889 */ 3890 int ice_vsi_del_vlan_zero(struct ice_vsi *vsi) 3891 { 3892 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi); 3893 struct ice_vlan vlan; 3894 int err; 3895 3896 vlan = ICE_VLAN(0, 0, 0); 3897 err = vlan_ops->del_vlan(vsi, &vlan); 3898 if (err && err != -EEXIST) 3899 return err; 3900 3901 /* in SVM both VLAN 0 filters are identical */ 3902 if (!ice_is_dvm_ena(&vsi->back->hw)) 3903 return 0; 3904 3905 vlan = ICE_VLAN(ETH_P_8021Q, 0, 0); 3906 err = vlan_ops->del_vlan(vsi, &vlan); 3907 if (err && err != -EEXIST) 3908 return err; 3909 3910 /* when deleting the last VLAN filter, make sure to disable the VLAN 3911 * promisc mode so the filter isn't left by accident 3912 */ 3913 return ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx, 3914 ICE_MCAST_VLAN_PROMISC_BITS, 0); 3915 } 3916 3917 /** 3918 * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode 3919 * @vsi: VSI used to get the VLAN mode 3920 * 3921 * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled 3922 * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details. 3923 */ 3924 static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi) 3925 { 3926 #define ICE_DVM_NUM_ZERO_VLAN_FLTRS 2 3927 #define ICE_SVM_NUM_ZERO_VLAN_FLTRS 1 3928 /* no VLAN 0 filter is created when a port VLAN is active */ 3929 if (vsi->type == ICE_VSI_VF) { 3930 if (WARN_ON(!vsi->vf)) 3931 return 0; 3932 3933 if (ice_vf_is_port_vlan_ena(vsi->vf)) 3934 return 0; 3935 } 3936 3937 if (ice_is_dvm_ena(&vsi->back->hw)) 3938 return ICE_DVM_NUM_ZERO_VLAN_FLTRS; 3939 else 3940 return ICE_SVM_NUM_ZERO_VLAN_FLTRS; 3941 } 3942 3943 /** 3944 * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs 3945 * @vsi: VSI used to determine if any non-zero VLANs have been added 3946 */ 3947 bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi) 3948 { 3949 return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi)); 3950 } 3951 3952 /** 3953 * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI 3954 * @vsi: VSI used to get the number of non-zero VLANs added 3955 */ 3956 u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi) 3957 { 3958 return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi)); 3959 } 3960 3961 /** 3962 * ice_is_feature_supported 3963 * @pf: pointer to the struct ice_pf instance 3964 * @f: feature enum to be checked 3965 * 3966 * returns true if feature is supported, false otherwise 3967 */ 3968 bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f) 3969 { 3970 if (f < 0 || f >= ICE_F_MAX) 3971 return false; 3972 3973 return test_bit(f, pf->features); 3974 } 3975 3976 /** 3977 * ice_set_feature_support 3978 * @pf: pointer to the struct ice_pf instance 3979 * @f: feature enum to set 3980 */ 3981 void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f) 3982 { 3983 if (f < 0 || f >= ICE_F_MAX) 3984 return; 3985 3986 set_bit(f, pf->features); 3987 } 3988 3989 /** 3990 * ice_clear_feature_support 3991 * @pf: pointer to the struct ice_pf instance 3992 * @f: feature enum to clear 3993 */ 3994 void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f) 3995 { 3996 if (f < 0 || f >= ICE_F_MAX) 3997 return; 3998 3999 clear_bit(f, pf->features); 4000 } 4001 4002 /** 4003 * ice_init_feature_support 4004 * @pf: pointer to the struct ice_pf instance 4005 * 4006 * called during init to setup supported feature 4007 */ 4008 void ice_init_feature_support(struct ice_pf *pf) 4009 { 4010 switch (pf->hw.device_id) { 4011 case ICE_DEV_ID_E810C_BACKPLANE: 4012 case ICE_DEV_ID_E810C_QSFP: 4013 case ICE_DEV_ID_E810C_SFP: 4014 ice_set_feature_support(pf, ICE_F_DSCP); 4015 ice_set_feature_support(pf, ICE_F_PTP_EXTTS); 4016 if (ice_is_e810t(&pf->hw)) { 4017 ice_set_feature_support(pf, ICE_F_SMA_CTRL); 4018 if (ice_gnss_is_gps_present(&pf->hw)) 4019 ice_set_feature_support(pf, ICE_F_GNSS); 4020 } 4021 break; 4022 default: 4023 break; 4024 } 4025 } 4026 4027 /** 4028 * ice_vsi_update_security - update security block in VSI 4029 * @vsi: pointer to VSI structure 4030 * @fill: function pointer to fill ctx 4031 */ 4032 int 4033 ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *)) 4034 { 4035 struct ice_vsi_ctx ctx = { 0 }; 4036 4037 ctx.info = vsi->info; 4038 ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID); 4039 fill(&ctx); 4040 4041 if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL)) 4042 return -ENODEV; 4043 4044 vsi->info = ctx.info; 4045 return 0; 4046 } 4047 4048 /** 4049 * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx 4050 * @ctx: pointer to VSI ctx structure 4051 */ 4052 void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx) 4053 { 4054 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF | 4055 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA << 4056 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S); 4057 } 4058 4059 /** 4060 * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx 4061 * @ctx: pointer to VSI ctx structure 4062 */ 4063 void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx) 4064 { 4065 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF & 4066 ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA << 4067 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S); 4068 } 4069 4070 /** 4071 * ice_vsi_ctx_set_allow_override - allow destination override on VSI 4072 * @ctx: pointer to VSI ctx structure 4073 */ 4074 void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx) 4075 { 4076 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD; 4077 } 4078 4079 /** 4080 * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI 4081 * @ctx: pointer to VSI ctx structure 4082 */ 4083 void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx) 4084 { 4085 ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD; 4086 } 4087 4088 /** 4089 * ice_vsi_update_local_lb - update sw block in VSI with local loopback bit 4090 * @vsi: pointer to VSI structure 4091 * @set: set or unset the bit 4092 */ 4093 int 4094 ice_vsi_update_local_lb(struct ice_vsi *vsi, bool set) 4095 { 4096 struct ice_vsi_ctx ctx = { 4097 .info = vsi->info, 4098 }; 4099 4100 ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID); 4101 if (set) 4102 ctx.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_LOCAL_LB; 4103 else 4104 ctx.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_LOCAL_LB; 4105 4106 if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL)) 4107 return -ENODEV; 4108 4109 vsi->info = ctx.info; 4110 return 0; 4111 } 4112