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