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