1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2018, Intel Corporation. */ 3 4 #include "ice.h" 5 #include "ice_vf_lib_private.h" 6 #include "ice_base.h" 7 #include "ice_lib.h" 8 #include "ice_fltr.h" 9 #include "ice_dcb_lib.h" 10 #include "ice_flow.h" 11 #include "ice_eswitch.h" 12 #include "ice_virtchnl_allowlist.h" 13 #include "ice_flex_pipe.h" 14 #include "ice_vf_vsi_vlan_ops.h" 15 #include "ice_vlan.h" 16 17 /** 18 * ice_free_vf_entries - Free all VF entries from the hash table 19 * @pf: pointer to the PF structure 20 * 21 * Iterate over the VF hash table, removing and releasing all VF entries. 22 * Called during VF teardown or as cleanup during failed VF initialization. 23 */ 24 static void ice_free_vf_entries(struct ice_pf *pf) 25 { 26 struct ice_vfs *vfs = &pf->vfs; 27 struct hlist_node *tmp; 28 struct ice_vf *vf; 29 unsigned int bkt; 30 31 /* Remove all VFs from the hash table and release their main 32 * reference. Once all references to the VF are dropped, ice_put_vf() 33 * will call ice_release_vf which will remove the VF memory. 34 */ 35 lockdep_assert_held(&vfs->table_lock); 36 37 hash_for_each_safe(vfs->table, bkt, tmp, vf, entry) { 38 hash_del_rcu(&vf->entry); 39 ice_put_vf(vf); 40 } 41 } 42 43 /** 44 * ice_free_vf_res - Free a VF's resources 45 * @vf: pointer to the VF info 46 */ 47 static void ice_free_vf_res(struct ice_vf *vf) 48 { 49 struct ice_pf *pf = vf->pf; 50 int i, last_vector_idx; 51 52 /* First, disable VF's configuration API to prevent OS from 53 * accessing the VF's VSI after it's freed or invalidated. 54 */ 55 clear_bit(ICE_VF_STATE_INIT, vf->vf_states); 56 ice_vf_fdir_exit(vf); 57 /* free VF control VSI */ 58 if (vf->ctrl_vsi_idx != ICE_NO_VSI) 59 ice_vf_ctrl_vsi_release(vf); 60 61 /* free VSI and disconnect it from the parent uplink */ 62 if (vf->lan_vsi_idx != ICE_NO_VSI) { 63 ice_vf_vsi_release(vf); 64 vf->num_mac = 0; 65 } 66 67 last_vector_idx = vf->first_vector_idx + vf->num_msix - 1; 68 69 /* clear VF MDD event information */ 70 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events)); 71 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events)); 72 73 /* Disable interrupts so that VF starts in a known state */ 74 for (i = vf->first_vector_idx; i <= last_vector_idx; i++) { 75 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M); 76 ice_flush(&pf->hw); 77 } 78 /* reset some of the state variables keeping track of the resources */ 79 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states); 80 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states); 81 } 82 83 /** 84 * ice_dis_vf_mappings 85 * @vf: pointer to the VF structure 86 */ 87 static void ice_dis_vf_mappings(struct ice_vf *vf) 88 { 89 struct ice_pf *pf = vf->pf; 90 struct ice_vsi *vsi; 91 struct device *dev; 92 int first, last, v; 93 struct ice_hw *hw; 94 95 hw = &pf->hw; 96 vsi = ice_get_vf_vsi(vf); 97 if (WARN_ON(!vsi)) 98 return; 99 100 dev = ice_pf_to_dev(pf); 101 wr32(hw, VPINT_ALLOC(vf->vf_id), 0); 102 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0); 103 104 first = vf->first_vector_idx; 105 last = first + vf->num_msix - 1; 106 for (v = first; v <= last; v++) { 107 u32 reg; 108 109 reg = FIELD_PREP(GLINT_VECT2FUNC_IS_PF_M, 1) | 110 FIELD_PREP(GLINT_VECT2FUNC_PF_NUM_M, hw->pf_id); 111 wr32(hw, GLINT_VECT2FUNC(v), reg); 112 } 113 114 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) 115 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0); 116 else 117 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n"); 118 119 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) 120 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0); 121 else 122 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n"); 123 } 124 125 /** 126 * ice_free_vfs - Free all VFs 127 * @pf: pointer to the PF structure 128 */ 129 void ice_free_vfs(struct ice_pf *pf) 130 { 131 struct device *dev = ice_pf_to_dev(pf); 132 struct ice_vfs *vfs = &pf->vfs; 133 struct ice_hw *hw = &pf->hw; 134 struct ice_vf *vf; 135 unsigned int bkt; 136 137 if (!ice_has_vfs(pf)) 138 return; 139 140 while (test_and_set_bit(ICE_VF_DIS, pf->state)) 141 usleep_range(1000, 2000); 142 143 /* Disable IOV before freeing resources. This lets any VF drivers 144 * running in the host get themselves cleaned up before we yank 145 * the carpet out from underneath their feet. 146 */ 147 if (!pci_vfs_assigned(pf->pdev)) 148 pci_disable_sriov(pf->pdev); 149 else 150 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n"); 151 152 mutex_lock(&vfs->table_lock); 153 154 ice_for_each_vf(pf, bkt, vf) { 155 mutex_lock(&vf->cfg_lock); 156 157 ice_eswitch_detach_vf(pf, vf); 158 ice_dis_vf_qs(vf); 159 ice_virt_free_irqs(pf, vf->first_vector_idx, vf->num_msix); 160 161 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states)) { 162 /* disable VF qp mappings and set VF disable state */ 163 ice_dis_vf_mappings(vf); 164 set_bit(ICE_VF_STATE_DIS, vf->vf_states); 165 ice_free_vf_res(vf); 166 } 167 168 if (!pci_vfs_assigned(pf->pdev)) { 169 u32 reg_idx, bit_idx; 170 171 reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32; 172 bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32; 173 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx)); 174 } 175 176 /* clear malicious info since the VF is getting released */ 177 if (!ice_is_feature_supported(pf, ICE_F_MBX_LIMIT)) 178 list_del(&vf->mbx_info.list_entry); 179 180 mutex_unlock(&vf->cfg_lock); 181 } 182 183 vfs->num_qps_per = 0; 184 ice_free_vf_entries(pf); 185 186 mutex_unlock(&vfs->table_lock); 187 188 clear_bit(ICE_VF_DIS, pf->state); 189 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags); 190 } 191 192 /** 193 * ice_vf_vsi_setup - Set up a VF VSI 194 * @vf: VF to setup VSI for 195 * 196 * Returns pointer to the successfully allocated VSI struct on success, 197 * otherwise returns NULL on failure. 198 */ 199 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf) 200 { 201 struct ice_vsi_cfg_params params = {}; 202 struct ice_pf *pf = vf->pf; 203 struct ice_vsi *vsi; 204 205 params.type = ICE_VSI_VF; 206 params.port_info = ice_vf_get_port_info(vf); 207 params.vf = vf; 208 params.flags = ICE_VSI_FLAG_INIT; 209 210 vsi = ice_vsi_setup(pf, ¶ms); 211 212 if (!vsi) { 213 dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n"); 214 ice_vf_invalidate_vsi(vf); 215 return NULL; 216 } 217 218 vf->lan_vsi_idx = vsi->idx; 219 220 return vsi; 221 } 222 223 224 /** 225 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware 226 * @vf: VF to enable MSIX mappings for 227 * 228 * Some of the registers need to be indexed/configured using hardware global 229 * device values and other registers need 0-based values, which represent PF 230 * based values. 231 */ 232 static void ice_ena_vf_msix_mappings(struct ice_vf *vf) 233 { 234 int device_based_first_msix, device_based_last_msix; 235 int pf_based_first_msix, pf_based_last_msix, v; 236 struct ice_pf *pf = vf->pf; 237 int device_based_vf_id; 238 struct ice_hw *hw; 239 u32 reg; 240 241 hw = &pf->hw; 242 pf_based_first_msix = vf->first_vector_idx; 243 pf_based_last_msix = (pf_based_first_msix + vf->num_msix) - 1; 244 245 device_based_first_msix = pf_based_first_msix + 246 pf->hw.func_caps.common_cap.msix_vector_first_id; 247 device_based_last_msix = 248 (device_based_first_msix + vf->num_msix) - 1; 249 device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id; 250 251 reg = FIELD_PREP(VPINT_ALLOC_FIRST_M, device_based_first_msix) | 252 FIELD_PREP(VPINT_ALLOC_LAST_M, device_based_last_msix) | 253 VPINT_ALLOC_VALID_M; 254 wr32(hw, VPINT_ALLOC(vf->vf_id), reg); 255 256 reg = FIELD_PREP(VPINT_ALLOC_PCI_FIRST_M, device_based_first_msix) | 257 FIELD_PREP(VPINT_ALLOC_PCI_LAST_M, device_based_last_msix) | 258 VPINT_ALLOC_PCI_VALID_M; 259 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg); 260 261 /* map the interrupts to its functions */ 262 for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) { 263 reg = FIELD_PREP(GLINT_VECT2FUNC_VF_NUM_M, device_based_vf_id) | 264 FIELD_PREP(GLINT_VECT2FUNC_PF_NUM_M, hw->pf_id); 265 wr32(hw, GLINT_VECT2FUNC(v), reg); 266 } 267 268 /* Map mailbox interrupt to VF MSI-X vector 0 */ 269 wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M); 270 } 271 272 /** 273 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF 274 * @vf: VF to enable the mappings for 275 * @max_txq: max Tx queues allowed on the VF's VSI 276 * @max_rxq: max Rx queues allowed on the VF's VSI 277 */ 278 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq) 279 { 280 struct device *dev = ice_pf_to_dev(vf->pf); 281 struct ice_vsi *vsi = ice_get_vf_vsi(vf); 282 struct ice_hw *hw = &vf->pf->hw; 283 u32 reg; 284 285 if (WARN_ON(!vsi)) 286 return; 287 288 /* set regardless of mapping mode */ 289 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M); 290 291 /* VF Tx queues allocation */ 292 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) { 293 /* set the VF PF Tx queue range 294 * VFNUMQ value should be set to (number of queues - 1). A value 295 * of 0 means 1 queue and a value of 255 means 256 queues 296 */ 297 reg = FIELD_PREP(VPLAN_TX_QBASE_VFFIRSTQ_M, vsi->txq_map[0]) | 298 FIELD_PREP(VPLAN_TX_QBASE_VFNUMQ_M, max_txq - 1); 299 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg); 300 } else { 301 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n"); 302 } 303 304 /* set regardless of mapping mode */ 305 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M); 306 307 /* VF Rx queues allocation */ 308 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) { 309 /* set the VF PF Rx queue range 310 * VFNUMQ value should be set to (number of queues - 1). A value 311 * of 0 means 1 queue and a value of 255 means 256 queues 312 */ 313 reg = FIELD_PREP(VPLAN_RX_QBASE_VFFIRSTQ_M, vsi->rxq_map[0]) | 314 FIELD_PREP(VPLAN_RX_QBASE_VFNUMQ_M, max_rxq - 1); 315 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg); 316 } else { 317 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n"); 318 } 319 } 320 321 /** 322 * ice_ena_vf_mappings - enable VF MSIX and queue mapping 323 * @vf: pointer to the VF structure 324 */ 325 static void ice_ena_vf_mappings(struct ice_vf *vf) 326 { 327 struct ice_vsi *vsi = ice_get_vf_vsi(vf); 328 329 if (WARN_ON(!vsi)) 330 return; 331 332 ice_ena_vf_msix_mappings(vf); 333 ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq); 334 } 335 336 /** 337 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space 338 * @vf: VF to calculate the register index for 339 * @q_vector: a q_vector associated to the VF 340 */ 341 void ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector) 342 { 343 if (!vf || !q_vector) 344 return; 345 346 /* always add one to account for the OICR being the first MSIX */ 347 q_vector->vf_reg_idx = q_vector->v_idx + ICE_NONQ_VECS_VF; 348 q_vector->reg_idx = vf->first_vector_idx + q_vector->vf_reg_idx; 349 } 350 351 /** 352 * ice_set_per_vf_res - check if vectors and queues are available 353 * @pf: pointer to the PF structure 354 * @num_vfs: the number of SR-IOV VFs being configured 355 * 356 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we 357 * get more vectors and can enable more queues per VF. Note that this does not 358 * grab any vectors from the SW pool already allocated. Also note, that all 359 * vector counts include one for each VF's miscellaneous interrupt vector 360 * (i.e. OICR). 361 * 362 * Minimum VFs - 2 vectors, 1 queue pair 363 * Small VFs - 5 vectors, 4 queue pairs 364 * Medium VFs - 17 vectors, 16 queue pairs 365 * 366 * Second, determine number of queue pairs per VF by starting with a pre-defined 367 * maximum each VF supports. If this is not possible, then we adjust based on 368 * queue pairs available on the device. 369 * 370 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used 371 * by each VF during VF initialization and reset. 372 */ 373 static int ice_set_per_vf_res(struct ice_pf *pf, u16 num_vfs) 374 { 375 u16 num_msix_per_vf, num_txq, num_rxq, avail_qs; 376 int msix_avail_per_vf, msix_avail_for_sriov; 377 struct device *dev = ice_pf_to_dev(pf); 378 379 lockdep_assert_held(&pf->vfs.table_lock); 380 381 if (!num_vfs) 382 return -EINVAL; 383 384 /* determine MSI-X resources per VF */ 385 msix_avail_for_sriov = pf->virt_irq_tracker.num_entries; 386 msix_avail_per_vf = msix_avail_for_sriov / num_vfs; 387 if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) { 388 num_msix_per_vf = ICE_NUM_VF_MSIX_MED; 389 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) { 390 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL; 391 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) { 392 num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN; 393 } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) { 394 num_msix_per_vf = ICE_MIN_INTR_PER_VF; 395 } else { 396 dev_err(dev, "Only %d MSI-X interrupts available for SR-IOV. Not enough to support minimum of %d MSI-X interrupts per VF for %d VFs\n", 397 msix_avail_for_sriov, ICE_MIN_INTR_PER_VF, 398 num_vfs); 399 return -ENOSPC; 400 } 401 402 num_txq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF, 403 ICE_MAX_RSS_QS_PER_VF); 404 avail_qs = ice_get_avail_txq_count(pf) / num_vfs; 405 if (!avail_qs) 406 num_txq = 0; 407 else if (num_txq > avail_qs) 408 num_txq = rounddown_pow_of_two(avail_qs); 409 410 num_rxq = min_t(u16, num_msix_per_vf - ICE_NONQ_VECS_VF, 411 ICE_MAX_RSS_QS_PER_VF); 412 avail_qs = ice_get_avail_rxq_count(pf) / num_vfs; 413 if (!avail_qs) 414 num_rxq = 0; 415 else if (num_rxq > avail_qs) 416 num_rxq = rounddown_pow_of_two(avail_qs); 417 418 if (num_txq < ICE_MIN_QS_PER_VF || num_rxq < ICE_MIN_QS_PER_VF) { 419 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n", 420 ICE_MIN_QS_PER_VF, num_vfs); 421 return -ENOSPC; 422 } 423 424 /* only allow equal Tx/Rx queue count (i.e. queue pairs) */ 425 pf->vfs.num_qps_per = min_t(int, num_txq, num_rxq); 426 pf->vfs.num_msix_per = num_msix_per_vf; 427 dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n", 428 num_vfs, pf->vfs.num_msix_per, pf->vfs.num_qps_per); 429 430 return 0; 431 } 432 433 /** 434 * ice_init_vf_vsi_res - initialize/setup VF VSI resources 435 * @vf: VF to initialize/setup the VSI for 436 * 437 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the 438 * VF VSI's broadcast filter and is only used during initial VF creation. 439 */ 440 static int ice_init_vf_vsi_res(struct ice_vf *vf) 441 { 442 struct ice_pf *pf = vf->pf; 443 struct ice_vsi *vsi; 444 int err; 445 446 vf->first_vector_idx = ice_virt_get_irqs(pf, vf->num_msix); 447 if (vf->first_vector_idx < 0) 448 return -ENOMEM; 449 450 vsi = ice_vf_vsi_setup(vf); 451 if (!vsi) 452 return -ENOMEM; 453 454 err = ice_vf_init_host_cfg(vf, vsi); 455 if (err) 456 goto release_vsi; 457 458 return 0; 459 460 release_vsi: 461 ice_vf_vsi_release(vf); 462 return err; 463 } 464 465 /** 466 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV 467 * @pf: PF the VFs are associated with 468 */ 469 static int ice_start_vfs(struct ice_pf *pf) 470 { 471 struct ice_hw *hw = &pf->hw; 472 unsigned int bkt, it_cnt; 473 struct ice_vf *vf; 474 int retval; 475 476 lockdep_assert_held(&pf->vfs.table_lock); 477 478 it_cnt = 0; 479 ice_for_each_vf(pf, bkt, vf) { 480 vf->vf_ops->clear_reset_trigger(vf); 481 482 retval = ice_init_vf_vsi_res(vf); 483 if (retval) { 484 dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n", 485 vf->vf_id, retval); 486 goto teardown; 487 } 488 489 retval = ice_eswitch_attach_vf(pf, vf); 490 if (retval) { 491 dev_err(ice_pf_to_dev(pf), "Failed to attach VF %d to eswitch, error %d", 492 vf->vf_id, retval); 493 ice_vf_vsi_release(vf); 494 goto teardown; 495 } 496 497 set_bit(ICE_VF_STATE_INIT, vf->vf_states); 498 ice_ena_vf_mappings(vf); 499 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE); 500 it_cnt++; 501 } 502 503 ice_flush(hw); 504 return 0; 505 506 teardown: 507 ice_for_each_vf(pf, bkt, vf) { 508 if (it_cnt == 0) 509 break; 510 511 ice_dis_vf_mappings(vf); 512 ice_vf_vsi_release(vf); 513 it_cnt--; 514 } 515 516 return retval; 517 } 518 519 /** 520 * ice_sriov_free_vf - Free VF memory after all references are dropped 521 * @vf: pointer to VF to free 522 * 523 * Called by ice_put_vf through ice_release_vf once the last reference to a VF 524 * structure has been dropped. 525 */ 526 static void ice_sriov_free_vf(struct ice_vf *vf) 527 { 528 mutex_destroy(&vf->cfg_lock); 529 530 kfree_rcu(vf, rcu); 531 } 532 533 /** 534 * ice_sriov_clear_reset_state - clears VF Reset status register 535 * @vf: the vf to configure 536 */ 537 static void ice_sriov_clear_reset_state(struct ice_vf *vf) 538 { 539 struct ice_hw *hw = &vf->pf->hw; 540 541 /* Clear the reset status register so that VF immediately sees that 542 * the device is resetting, even if hardware hasn't yet gotten around 543 * to clearing VFGEN_RSTAT for us. 544 */ 545 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_INPROGRESS); 546 } 547 548 /** 549 * ice_sriov_clear_mbx_register - clears SRIOV VF's mailbox registers 550 * @vf: the vf to configure 551 */ 552 static void ice_sriov_clear_mbx_register(struct ice_vf *vf) 553 { 554 struct ice_pf *pf = vf->pf; 555 556 wr32(&pf->hw, VF_MBX_ARQLEN(vf->vf_id), 0); 557 wr32(&pf->hw, VF_MBX_ATQLEN(vf->vf_id), 0); 558 } 559 560 /** 561 * ice_sriov_trigger_reset_register - trigger VF reset for SRIOV VF 562 * @vf: pointer to VF structure 563 * @is_vflr: true if reset occurred due to VFLR 564 * 565 * Trigger and cleanup after a VF reset for a SR-IOV VF. 566 */ 567 static void ice_sriov_trigger_reset_register(struct ice_vf *vf, bool is_vflr) 568 { 569 struct ice_pf *pf = vf->pf; 570 u32 reg, reg_idx, bit_idx; 571 unsigned int vf_abs_id, i; 572 struct device *dev; 573 struct ice_hw *hw; 574 575 dev = ice_pf_to_dev(pf); 576 hw = &pf->hw; 577 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id; 578 579 /* In the case of a VFLR, HW has already reset the VF and we just need 580 * to clean up. Otherwise we must first trigger the reset using the 581 * VFRTRIG register. 582 */ 583 if (!is_vflr) { 584 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id)); 585 reg |= VPGEN_VFRTRIG_VFSWR_M; 586 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg); 587 } 588 589 /* clear the VFLR bit in GLGEN_VFLRSTAT */ 590 reg_idx = (vf_abs_id) / 32; 591 bit_idx = (vf_abs_id) % 32; 592 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx)); 593 ice_flush(hw); 594 595 wr32(hw, PF_PCI_CIAA, 596 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S)); 597 for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) { 598 reg = rd32(hw, PF_PCI_CIAD); 599 /* no transactions pending so stop polling */ 600 if ((reg & VF_TRANS_PENDING_M) == 0) 601 break; 602 603 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id); 604 udelay(ICE_PCI_CIAD_WAIT_DELAY_US); 605 } 606 } 607 608 /** 609 * ice_sriov_poll_reset_status - poll SRIOV VF reset status 610 * @vf: pointer to VF structure 611 * 612 * Returns true when reset is successful, else returns false 613 */ 614 static bool ice_sriov_poll_reset_status(struct ice_vf *vf) 615 { 616 struct ice_pf *pf = vf->pf; 617 unsigned int i; 618 u32 reg; 619 620 for (i = 0; i < 10; i++) { 621 /* VF reset requires driver to first reset the VF and then 622 * poll the status register to make sure that the reset 623 * completed successfully. 624 */ 625 reg = rd32(&pf->hw, VPGEN_VFRSTAT(vf->vf_id)); 626 if (reg & VPGEN_VFRSTAT_VFRD_M) 627 return true; 628 629 /* only sleep if the reset is not done */ 630 usleep_range(10, 20); 631 } 632 return false; 633 } 634 635 /** 636 * ice_sriov_clear_reset_trigger - enable VF to access hardware 637 * @vf: VF to enabled hardware access for 638 */ 639 static void ice_sriov_clear_reset_trigger(struct ice_vf *vf) 640 { 641 struct ice_hw *hw = &vf->pf->hw; 642 u32 reg; 643 644 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id)); 645 reg &= ~VPGEN_VFRTRIG_VFSWR_M; 646 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg); 647 ice_flush(hw); 648 } 649 650 /** 651 * ice_sriov_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt 652 * @vf: VF to perform tasks on 653 */ 654 static void ice_sriov_post_vsi_rebuild(struct ice_vf *vf) 655 { 656 ice_ena_vf_mappings(vf); 657 wr32(&vf->pf->hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE); 658 } 659 660 static const struct ice_vf_ops ice_sriov_vf_ops = { 661 .reset_type = ICE_VF_RESET, 662 .free = ice_sriov_free_vf, 663 .clear_reset_state = ice_sriov_clear_reset_state, 664 .clear_mbx_register = ice_sriov_clear_mbx_register, 665 .trigger_reset_register = ice_sriov_trigger_reset_register, 666 .poll_reset_status = ice_sriov_poll_reset_status, 667 .clear_reset_trigger = ice_sriov_clear_reset_trigger, 668 .irq_close = NULL, 669 .post_vsi_rebuild = ice_sriov_post_vsi_rebuild, 670 }; 671 672 /** 673 * ice_create_vf_entries - Allocate and insert VF entries 674 * @pf: pointer to the PF structure 675 * @num_vfs: the number of VFs to allocate 676 * 677 * Allocate new VF entries and insert them into the hash table. Set some 678 * basic default fields for initializing the new VFs. 679 * 680 * After this function exits, the hash table will have num_vfs entries 681 * inserted. 682 * 683 * Returns 0 on success or an integer error code on failure. 684 */ 685 static int ice_create_vf_entries(struct ice_pf *pf, u16 num_vfs) 686 { 687 struct pci_dev *pdev = pf->pdev; 688 struct ice_vfs *vfs = &pf->vfs; 689 struct pci_dev *vfdev = NULL; 690 struct ice_vf *vf; 691 u16 vf_pdev_id; 692 int err, pos; 693 694 lockdep_assert_held(&vfs->table_lock); 695 696 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); 697 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID, &vf_pdev_id); 698 699 for (u16 vf_id = 0; vf_id < num_vfs; vf_id++) { 700 vf = kzalloc(sizeof(*vf), GFP_KERNEL); 701 if (!vf) { 702 err = -ENOMEM; 703 goto err_free_entries; 704 } 705 kref_init(&vf->refcnt); 706 707 vf->pf = pf; 708 vf->vf_id = vf_id; 709 710 /* set sriov vf ops for VFs created during SRIOV flow */ 711 vf->vf_ops = &ice_sriov_vf_ops; 712 713 ice_initialize_vf_entry(vf); 714 715 do { 716 vfdev = pci_get_device(pdev->vendor, vf_pdev_id, vfdev); 717 } while (vfdev && vfdev->physfn != pdev); 718 vf->vfdev = vfdev; 719 vf->vf_sw_id = pf->first_sw; 720 721 pci_dev_get(vfdev); 722 723 hash_add_rcu(vfs->table, &vf->entry, vf_id); 724 } 725 726 /* Decrement of refcount done by pci_get_device() inside the loop does 727 * not touch the last iteration's vfdev, so it has to be done manually 728 * to balance pci_dev_get() added within the loop. 729 */ 730 pci_dev_put(vfdev); 731 732 return 0; 733 734 err_free_entries: 735 ice_free_vf_entries(pf); 736 return err; 737 } 738 739 /** 740 * ice_ena_vfs - enable VFs so they are ready to be used 741 * @pf: pointer to the PF structure 742 * @num_vfs: number of VFs to enable 743 */ 744 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs) 745 { 746 struct device *dev = ice_pf_to_dev(pf); 747 struct ice_hw *hw = &pf->hw; 748 int ret; 749 750 /* Disable global interrupt 0 so we don't try to handle the VFLR. */ 751 wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index), 752 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S); 753 set_bit(ICE_OICR_INTR_DIS, pf->state); 754 ice_flush(hw); 755 756 ret = pci_enable_sriov(pf->pdev, num_vfs); 757 if (ret) 758 goto err_unroll_intr; 759 760 mutex_lock(&pf->vfs.table_lock); 761 762 ret = ice_set_per_vf_res(pf, num_vfs); 763 if (ret) { 764 dev_err(dev, "Not enough resources for %d VFs, err %d. Try with fewer number of VFs\n", 765 num_vfs, ret); 766 goto err_unroll_sriov; 767 } 768 769 ret = ice_create_vf_entries(pf, num_vfs); 770 if (ret) { 771 dev_err(dev, "Failed to allocate VF entries for %d VFs\n", 772 num_vfs); 773 goto err_unroll_sriov; 774 } 775 776 ret = ice_start_vfs(pf); 777 if (ret) { 778 dev_err(dev, "Failed to start %d VFs, err %d\n", num_vfs, ret); 779 ret = -EAGAIN; 780 goto err_unroll_vf_entries; 781 } 782 783 clear_bit(ICE_VF_DIS, pf->state); 784 785 /* rearm global interrupts */ 786 if (test_and_clear_bit(ICE_OICR_INTR_DIS, pf->state)) 787 ice_irq_dynamic_ena(hw, NULL, NULL); 788 789 mutex_unlock(&pf->vfs.table_lock); 790 791 return 0; 792 793 err_unroll_vf_entries: 794 ice_free_vf_entries(pf); 795 err_unroll_sriov: 796 mutex_unlock(&pf->vfs.table_lock); 797 pci_disable_sriov(pf->pdev); 798 err_unroll_intr: 799 /* rearm interrupts here */ 800 ice_irq_dynamic_ena(hw, NULL, NULL); 801 clear_bit(ICE_OICR_INTR_DIS, pf->state); 802 return ret; 803 } 804 805 /** 806 * ice_pci_sriov_ena - Enable or change number of VFs 807 * @pf: pointer to the PF structure 808 * @num_vfs: number of VFs to allocate 809 * 810 * Returns 0 on success and negative on failure 811 */ 812 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs) 813 { 814 struct device *dev = ice_pf_to_dev(pf); 815 int err; 816 817 if (!num_vfs) { 818 ice_free_vfs(pf); 819 return 0; 820 } 821 822 if (num_vfs > pf->vfs.num_supported) { 823 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n", 824 num_vfs, pf->vfs.num_supported); 825 return -EOPNOTSUPP; 826 } 827 828 dev_info(dev, "Enabling %d VFs\n", num_vfs); 829 err = ice_ena_vfs(pf, num_vfs); 830 if (err) { 831 dev_err(dev, "Failed to enable SR-IOV: %d\n", err); 832 return err; 833 } 834 835 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags); 836 return 0; 837 } 838 839 /** 840 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks 841 * @pf: PF to enabled SR-IOV on 842 */ 843 static int ice_check_sriov_allowed(struct ice_pf *pf) 844 { 845 struct device *dev = ice_pf_to_dev(pf); 846 847 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) { 848 dev_err(dev, "This device is not capable of SR-IOV\n"); 849 return -EOPNOTSUPP; 850 } 851 852 if (ice_is_safe_mode(pf)) { 853 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n"); 854 return -EOPNOTSUPP; 855 } 856 857 if (!ice_pf_state_is_nominal(pf)) { 858 dev_err(dev, "Cannot enable SR-IOV, device not ready\n"); 859 return -EBUSY; 860 } 861 862 return 0; 863 } 864 865 /** 866 * ice_sriov_get_vf_total_msix - return number of MSI-X used by VFs 867 * @pdev: pointer to pci_dev struct 868 * 869 * The function is called via sysfs ops 870 */ 871 u32 ice_sriov_get_vf_total_msix(struct pci_dev *pdev) 872 { 873 struct ice_pf *pf = pci_get_drvdata(pdev); 874 875 return pf->virt_irq_tracker.num_entries; 876 } 877 878 static void ice_sriov_remap_vectors(struct ice_pf *pf, u16 restricted_id) 879 { 880 u16 vf_ids[ICE_MAX_SRIOV_VFS]; 881 struct ice_vf *tmp_vf; 882 int to_remap = 0, bkt; 883 884 /* For better irqs usage try to remap irqs of VFs 885 * that aren't running yet 886 */ 887 ice_for_each_vf(pf, bkt, tmp_vf) { 888 /* skip VF which is changing the number of MSI-X */ 889 if (restricted_id == tmp_vf->vf_id || 890 test_bit(ICE_VF_STATE_ACTIVE, tmp_vf->vf_states)) 891 continue; 892 893 ice_dis_vf_mappings(tmp_vf); 894 ice_virt_free_irqs(pf, tmp_vf->first_vector_idx, 895 tmp_vf->num_msix); 896 897 vf_ids[to_remap] = tmp_vf->vf_id; 898 to_remap += 1; 899 } 900 901 for (int i = 0; i < to_remap; i++) { 902 tmp_vf = ice_get_vf_by_id(pf, vf_ids[i]); 903 if (!tmp_vf) 904 continue; 905 906 tmp_vf->first_vector_idx = 907 ice_virt_get_irqs(pf, tmp_vf->num_msix); 908 /* there is no need to rebuild VSI as we are only changing the 909 * vector indexes not amount of MSI-X or queues 910 */ 911 ice_ena_vf_mappings(tmp_vf); 912 ice_put_vf(tmp_vf); 913 } 914 } 915 916 /** 917 * ice_sriov_set_msix_vec_count 918 * @vf_dev: pointer to pci_dev struct of VF device 919 * @msix_vec_count: new value for MSI-X amount on this VF 920 * 921 * Set requested MSI-X, queues and registers for @vf_dev. 922 * 923 * First do some sanity checks like if there are any VFs, if the new value 924 * is correct etc. Then disable old mapping (MSI-X and queues registers), change 925 * MSI-X and queues, rebuild VSI and enable new mapping. 926 * 927 * If it is possible (driver not binded to VF) try to remap also other VFs to 928 * linearize irqs register usage. 929 */ 930 int ice_sriov_set_msix_vec_count(struct pci_dev *vf_dev, int msix_vec_count) 931 { 932 struct pci_dev *pdev = pci_physfn(vf_dev); 933 struct ice_pf *pf = pci_get_drvdata(pdev); 934 u16 prev_msix, prev_queues, queues; 935 bool needs_rebuild = false; 936 struct ice_vsi *vsi; 937 struct ice_vf *vf; 938 int id; 939 940 if (!ice_get_num_vfs(pf)) 941 return -ENOENT; 942 943 if (!msix_vec_count) 944 return 0; 945 946 queues = msix_vec_count; 947 /* add 1 MSI-X for OICR */ 948 msix_vec_count += 1; 949 950 if (queues > min(ice_get_avail_txq_count(pf), 951 ice_get_avail_rxq_count(pf))) 952 return -EINVAL; 953 954 if (msix_vec_count < ICE_MIN_INTR_PER_VF) 955 return -EINVAL; 956 957 /* Transition of PCI VF function number to function_id */ 958 for (id = 0; id < pci_num_vf(pdev); id++) { 959 if (vf_dev->devfn == pci_iov_virtfn_devfn(pdev, id)) 960 break; 961 } 962 963 if (id == pci_num_vf(pdev)) 964 return -ENOENT; 965 966 vf = ice_get_vf_by_id(pf, id); 967 968 if (!vf) 969 return -ENOENT; 970 971 vsi = ice_get_vf_vsi(vf); 972 if (!vsi) { 973 ice_put_vf(vf); 974 return -ENOENT; 975 } 976 977 prev_msix = vf->num_msix; 978 prev_queues = vf->num_vf_qs; 979 980 ice_dis_vf_mappings(vf); 981 ice_virt_free_irqs(pf, vf->first_vector_idx, vf->num_msix); 982 983 /* Remap all VFs beside the one is now configured */ 984 ice_sriov_remap_vectors(pf, vf->vf_id); 985 986 vf->num_msix = msix_vec_count; 987 vf->num_vf_qs = queues; 988 vf->first_vector_idx = ice_virt_get_irqs(pf, vf->num_msix); 989 if (vf->first_vector_idx < 0) 990 goto unroll; 991 992 vsi->req_txq = queues; 993 vsi->req_rxq = queues; 994 995 if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) { 996 /* Try to rebuild with previous values */ 997 needs_rebuild = true; 998 goto unroll; 999 } 1000 1001 dev_info(ice_pf_to_dev(pf), 1002 "Changing VF %d resources to %d vectors and %d queues\n", 1003 vf->vf_id, vf->num_msix, vf->num_vf_qs); 1004 1005 ice_ena_vf_mappings(vf); 1006 ice_put_vf(vf); 1007 1008 return 0; 1009 1010 unroll: 1011 dev_info(ice_pf_to_dev(pf), 1012 "Can't set %d vectors on VF %d, falling back to %d\n", 1013 vf->num_msix, vf->vf_id, prev_msix); 1014 1015 vf->num_msix = prev_msix; 1016 vf->num_vf_qs = prev_queues; 1017 1018 vf->first_vector_idx = ice_virt_get_irqs(pf, vf->num_msix); 1019 if (vf->first_vector_idx < 0) { 1020 ice_put_vf(vf); 1021 return -EINVAL; 1022 } 1023 1024 if (needs_rebuild) { 1025 vsi->req_txq = prev_queues; 1026 vsi->req_rxq = prev_queues; 1027 1028 ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT); 1029 } 1030 1031 ice_ena_vf_mappings(vf); 1032 ice_put_vf(vf); 1033 1034 return -EINVAL; 1035 } 1036 1037 /** 1038 * ice_sriov_configure - Enable or change number of VFs via sysfs 1039 * @pdev: pointer to a pci_dev structure 1040 * @num_vfs: number of VFs to allocate or 0 to free VFs 1041 * 1042 * This function is called when the user updates the number of VFs in sysfs. On 1043 * success return whatever num_vfs was set to by the caller. Return negative on 1044 * failure. 1045 */ 1046 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs) 1047 { 1048 struct ice_pf *pf = pci_get_drvdata(pdev); 1049 struct device *dev = ice_pf_to_dev(pf); 1050 int err; 1051 1052 err = ice_check_sriov_allowed(pf); 1053 if (err) 1054 return err; 1055 1056 if (!num_vfs) { 1057 if (!pci_vfs_assigned(pdev)) { 1058 ice_free_vfs(pf); 1059 return 0; 1060 } 1061 1062 dev_err(dev, "can't free VFs because some are assigned to VMs.\n"); 1063 return -EBUSY; 1064 } 1065 1066 err = ice_pci_sriov_ena(pf, num_vfs); 1067 if (err) 1068 return err; 1069 1070 return num_vfs; 1071 } 1072 1073 /** 1074 * ice_process_vflr_event - Free VF resources via IRQ calls 1075 * @pf: pointer to the PF structure 1076 * 1077 * called from the VFLR IRQ handler to 1078 * free up VF resources and state variables 1079 */ 1080 void ice_process_vflr_event(struct ice_pf *pf) 1081 { 1082 struct ice_hw *hw = &pf->hw; 1083 struct ice_vf *vf; 1084 unsigned int bkt; 1085 u32 reg; 1086 1087 if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) || 1088 !ice_has_vfs(pf)) 1089 return; 1090 1091 mutex_lock(&pf->vfs.table_lock); 1092 ice_for_each_vf(pf, bkt, vf) { 1093 u32 reg_idx, bit_idx; 1094 1095 reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32; 1096 bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32; 1097 /* read GLGEN_VFLRSTAT register to find out the flr VFs */ 1098 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx)); 1099 if (reg & BIT(bit_idx)) 1100 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */ 1101 ice_reset_vf(vf, ICE_VF_RESET_VFLR | ICE_VF_RESET_LOCK); 1102 } 1103 mutex_unlock(&pf->vfs.table_lock); 1104 } 1105 1106 /** 1107 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in 1108 * @pf: PF used to index all VFs 1109 * @pfq: queue index relative to the PF's function space 1110 * 1111 * If no VF is found who owns the pfq then return NULL, otherwise return a 1112 * pointer to the VF who owns the pfq 1113 * 1114 * If this function returns non-NULL, it acquires a reference count of the VF 1115 * structure. The caller is responsible for calling ice_put_vf() to drop this 1116 * reference. 1117 */ 1118 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq) 1119 { 1120 struct ice_vf *vf; 1121 unsigned int bkt; 1122 1123 rcu_read_lock(); 1124 ice_for_each_vf_rcu(pf, bkt, vf) { 1125 struct ice_vsi *vsi; 1126 u16 rxq_idx; 1127 1128 vsi = ice_get_vf_vsi(vf); 1129 if (!vsi) 1130 continue; 1131 1132 ice_for_each_rxq(vsi, rxq_idx) 1133 if (vsi->rxq_map[rxq_idx] == pfq) { 1134 struct ice_vf *found; 1135 1136 if (kref_get_unless_zero(&vf->refcnt)) 1137 found = vf; 1138 else 1139 found = NULL; 1140 rcu_read_unlock(); 1141 return found; 1142 } 1143 } 1144 rcu_read_unlock(); 1145 1146 return NULL; 1147 } 1148 1149 /** 1150 * ice_globalq_to_pfq - convert from global queue index to PF space queue index 1151 * @pf: PF used for conversion 1152 * @globalq: global queue index used to convert to PF space queue index 1153 */ 1154 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq) 1155 { 1156 return globalq - pf->hw.func_caps.common_cap.rxq_first_id; 1157 } 1158 1159 /** 1160 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF 1161 * @pf: PF that the LAN overflow event happened on 1162 * @event: structure holding the event information for the LAN overflow event 1163 * 1164 * Determine if the LAN overflow event was caused by a VF queue. If it was not 1165 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a 1166 * reset on the offending VF. 1167 */ 1168 void 1169 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event) 1170 { 1171 u32 gldcb_rtctq, queue; 1172 struct ice_vf *vf; 1173 1174 gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq); 1175 dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq); 1176 1177 /* event returns device global Rx queue number */ 1178 queue = FIELD_GET(GLDCB_RTCTQ_RXQNUM_M, gldcb_rtctq); 1179 1180 vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue)); 1181 if (!vf) 1182 return; 1183 1184 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY | ICE_VF_RESET_LOCK); 1185 ice_put_vf(vf); 1186 } 1187 1188 /** 1189 * ice_set_vf_spoofchk 1190 * @netdev: network interface device structure 1191 * @vf_id: VF identifier 1192 * @ena: flag to enable or disable feature 1193 * 1194 * Enable or disable VF spoof checking 1195 */ 1196 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena) 1197 { 1198 struct ice_netdev_priv *np = netdev_priv(netdev); 1199 struct ice_pf *pf = np->vsi->back; 1200 struct ice_vsi *vf_vsi; 1201 struct device *dev; 1202 struct ice_vf *vf; 1203 int ret; 1204 1205 dev = ice_pf_to_dev(pf); 1206 1207 vf = ice_get_vf_by_id(pf, vf_id); 1208 if (!vf) 1209 return -EINVAL; 1210 1211 ret = ice_check_vf_ready_for_cfg(vf); 1212 if (ret) 1213 goto out_put_vf; 1214 1215 vf_vsi = ice_get_vf_vsi(vf); 1216 if (!vf_vsi) { 1217 netdev_err(netdev, "VSI %d for VF %d is null\n", 1218 vf->lan_vsi_idx, vf->vf_id); 1219 ret = -EINVAL; 1220 goto out_put_vf; 1221 } 1222 1223 if (vf_vsi->type != ICE_VSI_VF) { 1224 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n", 1225 vf_vsi->type, vf_vsi->vsi_num, vf->vf_id); 1226 ret = -ENODEV; 1227 goto out_put_vf; 1228 } 1229 1230 if (ena == vf->spoofchk) { 1231 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF"); 1232 ret = 0; 1233 goto out_put_vf; 1234 } 1235 1236 ret = ice_vsi_apply_spoofchk(vf_vsi, ena); 1237 if (ret) 1238 dev_err(dev, "Failed to set spoofchk %s for VF %d VSI %d\n error %d\n", 1239 ena ? "ON" : "OFF", vf->vf_id, vf_vsi->vsi_num, ret); 1240 else 1241 vf->spoofchk = ena; 1242 1243 out_put_vf: 1244 ice_put_vf(vf); 1245 return ret; 1246 } 1247 1248 /** 1249 * ice_get_vf_cfg 1250 * @netdev: network interface device structure 1251 * @vf_id: VF identifier 1252 * @ivi: VF configuration structure 1253 * 1254 * return VF configuration 1255 */ 1256 int 1257 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi) 1258 { 1259 struct ice_pf *pf = ice_netdev_to_pf(netdev); 1260 struct ice_vf *vf; 1261 int ret; 1262 1263 vf = ice_get_vf_by_id(pf, vf_id); 1264 if (!vf) 1265 return -EINVAL; 1266 1267 ret = ice_check_vf_ready_for_cfg(vf); 1268 if (ret) 1269 goto out_put_vf; 1270 1271 ivi->vf = vf_id; 1272 ether_addr_copy(ivi->mac, vf->hw_lan_addr); 1273 1274 /* VF configuration for VLAN and applicable QoS */ 1275 ivi->vlan = ice_vf_get_port_vlan_id(vf); 1276 ivi->qos = ice_vf_get_port_vlan_prio(vf); 1277 if (ice_vf_is_port_vlan_ena(vf)) 1278 ivi->vlan_proto = cpu_to_be16(ice_vf_get_port_vlan_tpid(vf)); 1279 1280 ivi->trusted = vf->trusted; 1281 ivi->spoofchk = vf->spoofchk; 1282 if (!vf->link_forced) 1283 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO; 1284 else if (vf->link_up) 1285 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE; 1286 else 1287 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE; 1288 ivi->max_tx_rate = vf->max_tx_rate; 1289 ivi->min_tx_rate = vf->min_tx_rate; 1290 1291 out_put_vf: 1292 ice_put_vf(vf); 1293 return ret; 1294 } 1295 1296 /** 1297 * __ice_set_vf_mac - program VF MAC address 1298 * @pf: PF to be configure 1299 * @vf_id: VF identifier 1300 * @mac: MAC address 1301 * 1302 * program VF MAC address 1303 * Return: zero on success or an error code on failure 1304 */ 1305 int __ice_set_vf_mac(struct ice_pf *pf, u16 vf_id, const u8 *mac) 1306 { 1307 struct device *dev; 1308 struct ice_vf *vf; 1309 int ret; 1310 1311 dev = ice_pf_to_dev(pf); 1312 if (is_multicast_ether_addr(mac)) { 1313 dev_err(dev, "%pM not a valid unicast address\n", mac); 1314 return -EINVAL; 1315 } 1316 1317 vf = ice_get_vf_by_id(pf, vf_id); 1318 if (!vf) 1319 return -EINVAL; 1320 1321 /* nothing left to do, unicast MAC already set */ 1322 if (ether_addr_equal(vf->dev_lan_addr, mac) && 1323 ether_addr_equal(vf->hw_lan_addr, mac)) { 1324 ret = 0; 1325 goto out_put_vf; 1326 } 1327 1328 ret = ice_check_vf_ready_for_cfg(vf); 1329 if (ret) 1330 goto out_put_vf; 1331 1332 mutex_lock(&vf->cfg_lock); 1333 1334 /* VF is notified of its new MAC via the PF's response to the 1335 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset 1336 */ 1337 ether_addr_copy(vf->dev_lan_addr, mac); 1338 ether_addr_copy(vf->hw_lan_addr, mac); 1339 if (is_zero_ether_addr(mac)) { 1340 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */ 1341 vf->pf_set_mac = false; 1342 dev_info(dev, "Removing MAC on VF %d. VF driver will be reinitialized\n", 1343 vf->vf_id); 1344 } else { 1345 /* PF will add MAC rule for the VF */ 1346 vf->pf_set_mac = true; 1347 dev_info(dev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n", 1348 mac, vf_id); 1349 } 1350 1351 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY); 1352 mutex_unlock(&vf->cfg_lock); 1353 1354 out_put_vf: 1355 ice_put_vf(vf); 1356 return ret; 1357 } 1358 1359 /** 1360 * ice_set_vf_mac - .ndo_set_vf_mac handler 1361 * @netdev: network interface device structure 1362 * @vf_id: VF identifier 1363 * @mac: MAC address 1364 * 1365 * program VF MAC address 1366 * Return: zero on success or an error code on failure 1367 */ 1368 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac) 1369 { 1370 return __ice_set_vf_mac(ice_netdev_to_pf(netdev), vf_id, mac); 1371 } 1372 1373 /** 1374 * ice_set_vf_trust 1375 * @netdev: network interface device structure 1376 * @vf_id: VF identifier 1377 * @trusted: Boolean value to enable/disable trusted VF 1378 * 1379 * Enable or disable a given VF as trusted 1380 */ 1381 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted) 1382 { 1383 struct ice_pf *pf = ice_netdev_to_pf(netdev); 1384 struct ice_vf *vf; 1385 int ret; 1386 1387 vf = ice_get_vf_by_id(pf, vf_id); 1388 if (!vf) 1389 return -EINVAL; 1390 1391 if (ice_is_eswitch_mode_switchdev(pf)) { 1392 dev_info(ice_pf_to_dev(pf), "Trusted VF is forbidden in switchdev mode\n"); 1393 return -EOPNOTSUPP; 1394 } 1395 1396 ret = ice_check_vf_ready_for_cfg(vf); 1397 if (ret) 1398 goto out_put_vf; 1399 1400 /* Check if already trusted */ 1401 if (trusted == vf->trusted) { 1402 ret = 0; 1403 goto out_put_vf; 1404 } 1405 1406 mutex_lock(&vf->cfg_lock); 1407 1408 vf->trusted = trusted; 1409 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY); 1410 dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n", 1411 vf_id, trusted ? "" : "un"); 1412 1413 mutex_unlock(&vf->cfg_lock); 1414 1415 out_put_vf: 1416 ice_put_vf(vf); 1417 return ret; 1418 } 1419 1420 /** 1421 * ice_set_vf_link_state 1422 * @netdev: network interface device structure 1423 * @vf_id: VF identifier 1424 * @link_state: required link state 1425 * 1426 * Set VF's link state, irrespective of physical link state status 1427 */ 1428 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state) 1429 { 1430 struct ice_pf *pf = ice_netdev_to_pf(netdev); 1431 struct ice_vf *vf; 1432 int ret; 1433 1434 vf = ice_get_vf_by_id(pf, vf_id); 1435 if (!vf) 1436 return -EINVAL; 1437 1438 ret = ice_check_vf_ready_for_cfg(vf); 1439 if (ret) 1440 goto out_put_vf; 1441 1442 switch (link_state) { 1443 case IFLA_VF_LINK_STATE_AUTO: 1444 vf->link_forced = false; 1445 break; 1446 case IFLA_VF_LINK_STATE_ENABLE: 1447 vf->link_forced = true; 1448 vf->link_up = true; 1449 break; 1450 case IFLA_VF_LINK_STATE_DISABLE: 1451 vf->link_forced = true; 1452 vf->link_up = false; 1453 break; 1454 default: 1455 ret = -EINVAL; 1456 goto out_put_vf; 1457 } 1458 1459 ice_vc_notify_vf_link_state(vf); 1460 1461 out_put_vf: 1462 ice_put_vf(vf); 1463 return ret; 1464 } 1465 1466 /** 1467 * ice_calc_all_vfs_min_tx_rate - calculate cumulative min Tx rate on all VFs 1468 * @pf: PF associated with VFs 1469 */ 1470 static int ice_calc_all_vfs_min_tx_rate(struct ice_pf *pf) 1471 { 1472 struct ice_vf *vf; 1473 unsigned int bkt; 1474 int rate = 0; 1475 1476 rcu_read_lock(); 1477 ice_for_each_vf_rcu(pf, bkt, vf) 1478 rate += vf->min_tx_rate; 1479 rcu_read_unlock(); 1480 1481 return rate; 1482 } 1483 1484 /** 1485 * ice_min_tx_rate_oversubscribed - check if min Tx rate causes oversubscription 1486 * @vf: VF trying to configure min_tx_rate 1487 * @min_tx_rate: min Tx rate in Mbps 1488 * 1489 * Check if the min_tx_rate being passed in will cause oversubscription of total 1490 * min_tx_rate based on the current link speed and all other VFs configured 1491 * min_tx_rate 1492 * 1493 * Return true if the passed min_tx_rate would cause oversubscription, else 1494 * return false 1495 */ 1496 static bool 1497 ice_min_tx_rate_oversubscribed(struct ice_vf *vf, int min_tx_rate) 1498 { 1499 struct ice_vsi *vsi = ice_get_vf_vsi(vf); 1500 int all_vfs_min_tx_rate; 1501 int link_speed_mbps; 1502 1503 if (WARN_ON(!vsi)) 1504 return false; 1505 1506 link_speed_mbps = ice_get_link_speed_mbps(vsi); 1507 all_vfs_min_tx_rate = ice_calc_all_vfs_min_tx_rate(vf->pf); 1508 1509 /* this VF's previous rate is being overwritten */ 1510 all_vfs_min_tx_rate -= vf->min_tx_rate; 1511 1512 if (all_vfs_min_tx_rate + min_tx_rate > link_speed_mbps) { 1513 dev_err(ice_pf_to_dev(vf->pf), "min_tx_rate of %d Mbps on VF %u would cause oversubscription of %d Mbps based on the current link speed %d Mbps\n", 1514 min_tx_rate, vf->vf_id, 1515 all_vfs_min_tx_rate + min_tx_rate - link_speed_mbps, 1516 link_speed_mbps); 1517 return true; 1518 } 1519 1520 return false; 1521 } 1522 1523 /** 1524 * ice_set_vf_bw - set min/max VF bandwidth 1525 * @netdev: network interface device structure 1526 * @vf_id: VF identifier 1527 * @min_tx_rate: Minimum Tx rate in Mbps 1528 * @max_tx_rate: Maximum Tx rate in Mbps 1529 */ 1530 int 1531 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate, 1532 int max_tx_rate) 1533 { 1534 struct ice_pf *pf = ice_netdev_to_pf(netdev); 1535 struct ice_vsi *vsi; 1536 struct device *dev; 1537 struct ice_vf *vf; 1538 int ret; 1539 1540 dev = ice_pf_to_dev(pf); 1541 1542 vf = ice_get_vf_by_id(pf, vf_id); 1543 if (!vf) 1544 return -EINVAL; 1545 1546 ret = ice_check_vf_ready_for_cfg(vf); 1547 if (ret) 1548 goto out_put_vf; 1549 1550 vsi = ice_get_vf_vsi(vf); 1551 if (!vsi) { 1552 ret = -EINVAL; 1553 goto out_put_vf; 1554 } 1555 1556 if (min_tx_rate && ice_is_dcb_active(pf)) { 1557 dev_err(dev, "DCB on PF is currently enabled. VF min Tx rate limiting not allowed on this PF.\n"); 1558 ret = -EOPNOTSUPP; 1559 goto out_put_vf; 1560 } 1561 1562 if (ice_min_tx_rate_oversubscribed(vf, min_tx_rate)) { 1563 ret = -EINVAL; 1564 goto out_put_vf; 1565 } 1566 1567 if (vf->min_tx_rate != (unsigned int)min_tx_rate) { 1568 ret = ice_set_min_bw_limit(vsi, (u64)min_tx_rate * 1000); 1569 if (ret) { 1570 dev_err(dev, "Unable to set min-tx-rate for VF %d\n", 1571 vf->vf_id); 1572 goto out_put_vf; 1573 } 1574 1575 vf->min_tx_rate = min_tx_rate; 1576 } 1577 1578 if (vf->max_tx_rate != (unsigned int)max_tx_rate) { 1579 ret = ice_set_max_bw_limit(vsi, (u64)max_tx_rate * 1000); 1580 if (ret) { 1581 dev_err(dev, "Unable to set max-tx-rate for VF %d\n", 1582 vf->vf_id); 1583 goto out_put_vf; 1584 } 1585 1586 vf->max_tx_rate = max_tx_rate; 1587 } 1588 1589 out_put_vf: 1590 ice_put_vf(vf); 1591 return ret; 1592 } 1593 1594 /** 1595 * ice_get_vf_stats - populate some stats for the VF 1596 * @netdev: the netdev of the PF 1597 * @vf_id: the host OS identifier (0-255) 1598 * @vf_stats: pointer to the OS memory to be initialized 1599 */ 1600 int ice_get_vf_stats(struct net_device *netdev, int vf_id, 1601 struct ifla_vf_stats *vf_stats) 1602 { 1603 struct ice_pf *pf = ice_netdev_to_pf(netdev); 1604 struct ice_eth_stats *stats; 1605 struct ice_vsi *vsi; 1606 struct ice_vf *vf; 1607 int ret; 1608 1609 vf = ice_get_vf_by_id(pf, vf_id); 1610 if (!vf) 1611 return -EINVAL; 1612 1613 ret = ice_check_vf_ready_for_cfg(vf); 1614 if (ret) 1615 goto out_put_vf; 1616 1617 vsi = ice_get_vf_vsi(vf); 1618 if (!vsi) { 1619 ret = -EINVAL; 1620 goto out_put_vf; 1621 } 1622 1623 ice_update_eth_stats(vsi); 1624 stats = &vsi->eth_stats; 1625 1626 memset(vf_stats, 0, sizeof(*vf_stats)); 1627 1628 vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast + 1629 stats->rx_multicast; 1630 vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast + 1631 stats->tx_multicast; 1632 vf_stats->rx_bytes = stats->rx_bytes; 1633 vf_stats->tx_bytes = stats->tx_bytes; 1634 vf_stats->broadcast = stats->rx_broadcast; 1635 vf_stats->multicast = stats->rx_multicast; 1636 vf_stats->rx_dropped = stats->rx_discards; 1637 vf_stats->tx_dropped = stats->tx_discards; 1638 1639 out_put_vf: 1640 ice_put_vf(vf); 1641 return ret; 1642 } 1643 1644 /** 1645 * ice_is_supported_port_vlan_proto - make sure the vlan_proto is supported 1646 * @hw: hardware structure used to check the VLAN mode 1647 * @vlan_proto: VLAN TPID being checked 1648 * 1649 * If the device is configured in Double VLAN Mode (DVM), then both ETH_P_8021Q 1650 * and ETH_P_8021AD are supported. If the device is configured in Single VLAN 1651 * Mode (SVM), then only ETH_P_8021Q is supported. 1652 */ 1653 static bool 1654 ice_is_supported_port_vlan_proto(struct ice_hw *hw, u16 vlan_proto) 1655 { 1656 bool is_supported = false; 1657 1658 switch (vlan_proto) { 1659 case ETH_P_8021Q: 1660 is_supported = true; 1661 break; 1662 case ETH_P_8021AD: 1663 if (ice_is_dvm_ena(hw)) 1664 is_supported = true; 1665 break; 1666 } 1667 1668 return is_supported; 1669 } 1670 1671 /** 1672 * ice_set_vf_port_vlan 1673 * @netdev: network interface device structure 1674 * @vf_id: VF identifier 1675 * @vlan_id: VLAN ID being set 1676 * @qos: priority setting 1677 * @vlan_proto: VLAN protocol 1678 * 1679 * program VF Port VLAN ID and/or QoS 1680 */ 1681 int 1682 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos, 1683 __be16 vlan_proto) 1684 { 1685 struct ice_pf *pf = ice_netdev_to_pf(netdev); 1686 u16 local_vlan_proto = ntohs(vlan_proto); 1687 struct device *dev; 1688 struct ice_vf *vf; 1689 int ret; 1690 1691 dev = ice_pf_to_dev(pf); 1692 1693 if (vlan_id >= VLAN_N_VID || qos > 7) { 1694 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n", 1695 vf_id, vlan_id, qos); 1696 return -EINVAL; 1697 } 1698 1699 if (!ice_is_supported_port_vlan_proto(&pf->hw, local_vlan_proto)) { 1700 dev_err(dev, "VF VLAN protocol 0x%04x is not supported\n", 1701 local_vlan_proto); 1702 return -EPROTONOSUPPORT; 1703 } 1704 1705 vf = ice_get_vf_by_id(pf, vf_id); 1706 if (!vf) 1707 return -EINVAL; 1708 1709 ret = ice_check_vf_ready_for_cfg(vf); 1710 if (ret) 1711 goto out_put_vf; 1712 1713 if (ice_vf_get_port_vlan_prio(vf) == qos && 1714 ice_vf_get_port_vlan_tpid(vf) == local_vlan_proto && 1715 ice_vf_get_port_vlan_id(vf) == vlan_id) { 1716 /* duplicate request, so just return success */ 1717 dev_dbg(dev, "Duplicate port VLAN %u, QoS %u, TPID 0x%04x request\n", 1718 vlan_id, qos, local_vlan_proto); 1719 ret = 0; 1720 goto out_put_vf; 1721 } 1722 1723 mutex_lock(&vf->cfg_lock); 1724 1725 vf->port_vlan_info = ICE_VLAN(local_vlan_proto, vlan_id, qos); 1726 if (ice_vf_is_port_vlan_ena(vf)) 1727 dev_info(dev, "Setting VLAN %u, QoS %u, TPID 0x%04x on VF %d\n", 1728 vlan_id, qos, local_vlan_proto, vf_id); 1729 else 1730 dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id); 1731 1732 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY); 1733 mutex_unlock(&vf->cfg_lock); 1734 1735 out_put_vf: 1736 ice_put_vf(vf); 1737 return ret; 1738 } 1739 1740 /** 1741 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event 1742 * @vf: pointer to the VF structure 1743 */ 1744 void ice_print_vf_rx_mdd_event(struct ice_vf *vf) 1745 { 1746 struct ice_pf *pf = vf->pf; 1747 struct device *dev; 1748 1749 dev = ice_pf_to_dev(pf); 1750 1751 dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n", 1752 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id, 1753 vf->dev_lan_addr, 1754 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags) 1755 ? "on" : "off"); 1756 } 1757 1758 /** 1759 * ice_print_vf_tx_mdd_event - print VF Tx malicious driver detect event 1760 * @vf: pointer to the VF structure 1761 */ 1762 void ice_print_vf_tx_mdd_event(struct ice_vf *vf) 1763 { 1764 struct ice_pf *pf = vf->pf; 1765 struct device *dev; 1766 1767 dev = ice_pf_to_dev(pf); 1768 1769 dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n", 1770 vf->mdd_tx_events.count, pf->hw.pf_id, vf->vf_id, 1771 vf->dev_lan_addr, 1772 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags) 1773 ? "on" : "off"); 1774 } 1775 1776 /** 1777 * ice_print_vfs_mdd_events - print VFs malicious driver detect event 1778 * @pf: pointer to the PF structure 1779 * 1780 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events. 1781 */ 1782 void ice_print_vfs_mdd_events(struct ice_pf *pf) 1783 { 1784 struct ice_vf *vf; 1785 unsigned int bkt; 1786 1787 /* check that there are pending MDD events to print */ 1788 if (!test_and_clear_bit(ICE_MDD_VF_PRINT_PENDING, pf->state)) 1789 return; 1790 1791 /* VF MDD event logs are rate limited to one second intervals */ 1792 if (time_is_after_jiffies(pf->vfs.last_printed_mdd_jiffies + HZ * 1)) 1793 return; 1794 1795 pf->vfs.last_printed_mdd_jiffies = jiffies; 1796 1797 mutex_lock(&pf->vfs.table_lock); 1798 ice_for_each_vf(pf, bkt, vf) { 1799 /* only print Rx MDD event message if there are new events */ 1800 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) { 1801 vf->mdd_rx_events.last_printed = 1802 vf->mdd_rx_events.count; 1803 ice_print_vf_rx_mdd_event(vf); 1804 } 1805 1806 /* only print Tx MDD event message if there are new events */ 1807 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) { 1808 vf->mdd_tx_events.last_printed = 1809 vf->mdd_tx_events.count; 1810 ice_print_vf_tx_mdd_event(vf); 1811 } 1812 } 1813 mutex_unlock(&pf->vfs.table_lock); 1814 } 1815 1816 /** 1817 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR 1818 * @pf: pointer to the PF structure 1819 * 1820 * Called when recovering from a PF FLR to restore interrupt capability to 1821 * the VFs. 1822 */ 1823 void ice_restore_all_vfs_msi_state(struct ice_pf *pf) 1824 { 1825 struct ice_vf *vf; 1826 u32 bkt; 1827 1828 ice_for_each_vf(pf, bkt, vf) 1829 pci_restore_msi_state(vf->vfdev); 1830 } 1831