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