1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
10 * ice_validate_vf_id - helper to check if VF ID is valid
11 * @pf: pointer to the PF structure
12 * @vf_id: the ID of the VF to check
14 static int ice_validate_vf_id(struct ice_pf *pf, u16 vf_id)
16 /* vf_id range is only valid for 0-255, and should always be unsigned */
17 if (vf_id >= pf->num_alloc_vfs) {
18 dev_err(ice_pf_to_dev(pf), "Invalid VF ID: %u\n", vf_id);
25 * ice_check_vf_init - helper to check if VF init complete
26 * @pf: pointer to the PF structure
27 * @vf: the pointer to the VF to check
29 static int ice_check_vf_init(struct ice_pf *pf, struct ice_vf *vf)
31 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
32 dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
40 * ice_err_to_virt_err - translate errors for VF return code
41 * @ice_err: error return code
43 static enum virtchnl_status_code ice_err_to_virt_err(enum ice_status ice_err)
47 return VIRTCHNL_STATUS_SUCCESS;
49 case ICE_ERR_INVAL_SIZE:
50 case ICE_ERR_DEVICE_NOT_SUPPORTED:
53 return VIRTCHNL_STATUS_ERR_PARAM;
54 case ICE_ERR_NO_MEMORY:
55 return VIRTCHNL_STATUS_ERR_NO_MEMORY;
56 case ICE_ERR_NOT_READY:
57 case ICE_ERR_RESET_FAILED:
58 case ICE_ERR_FW_API_VER:
59 case ICE_ERR_AQ_ERROR:
60 case ICE_ERR_AQ_TIMEOUT:
62 case ICE_ERR_AQ_NO_WORK:
63 case ICE_ERR_AQ_EMPTY:
64 return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
66 return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
71 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
72 * @pf: pointer to the PF structure
73 * @v_opcode: operation code
74 * @v_retval: return value
75 * @msg: pointer to the msg buffer
79 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
80 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
82 struct ice_hw *hw = &pf->hw;
85 ice_for_each_vf(pf, i) {
86 struct ice_vf *vf = &pf->vf[i];
88 /* Not all vfs are enabled so skip the ones that are not */
89 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
90 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
93 /* Ignore return value on purpose - a given VF may fail, but
94 * we need to keep going and send to all of them
96 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
102 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
103 * @vf: pointer to the VF structure
104 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
105 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
106 * @link_up: whether or not to set the link up/down
109 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
110 int ice_link_speed, bool link_up)
112 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
113 pfe->event_data.link_event_adv.link_status = link_up;
115 pfe->event_data.link_event_adv.link_speed =
116 ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
118 pfe->event_data.link_event.link_status = link_up;
119 /* Legacy method for virtchnl link speeds */
120 pfe->event_data.link_event.link_speed =
121 (enum virtchnl_link_speed)
122 ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
127 * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
128 * @vf: the VF to check
130 * Returns true if the VF has no Rx and no Tx queues enabled and returns false
133 static bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
135 return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
136 !bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
140 * ice_is_vf_link_up - check if the VF's link is up
141 * @vf: VF to check if link is up
143 static bool ice_is_vf_link_up(struct ice_vf *vf)
145 struct ice_pf *pf = vf->pf;
147 if (ice_check_vf_init(pf, vf))
150 if (ice_vf_has_no_qs_ena(vf))
152 else if (vf->link_forced)
155 return pf->hw.port_info->phy.link_info.link_info &
160 * ice_vc_notify_vf_link_state - Inform a VF of link status
161 * @vf: pointer to the VF structure
163 * send a link status message to a single VF
165 static void ice_vc_notify_vf_link_state(struct ice_vf *vf)
167 struct virtchnl_pf_event pfe = { 0 };
168 struct ice_hw *hw = &vf->pf->hw;
170 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
171 pfe.severity = PF_EVENT_SEVERITY_INFO;
173 if (ice_is_vf_link_up(vf))
174 ice_set_pfe_link(vf, &pfe,
175 hw->port_info->phy.link_info.link_speed, true);
177 ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
179 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
180 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
185 * ice_vf_invalidate_vsi - invalidate vsi_idx/vsi_num to remove VSI access
186 * @vf: VF to remove access to VSI for
188 static void ice_vf_invalidate_vsi(struct ice_vf *vf)
190 vf->lan_vsi_idx = ICE_NO_VSI;
191 vf->lan_vsi_num = ICE_NO_VSI;
195 * ice_vf_vsi_release - invalidate the VF's VSI after freeing it
196 * @vf: invalidate this VF's VSI after freeing it
198 static void ice_vf_vsi_release(struct ice_vf *vf)
200 ice_vsi_release(vf->pf->vsi[vf->lan_vsi_idx]);
201 ice_vf_invalidate_vsi(vf);
205 * ice_free_vf_res - Free a VF's resources
206 * @vf: pointer to the VF info
208 static void ice_free_vf_res(struct ice_vf *vf)
210 struct ice_pf *pf = vf->pf;
211 int i, last_vector_idx;
213 /* First, disable VF's configuration API to prevent OS from
214 * accessing the VF's VSI after it's freed or invalidated.
216 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
218 /* free VSI and disconnect it from the parent uplink */
219 if (vf->lan_vsi_idx != ICE_NO_VSI) {
220 ice_vf_vsi_release(vf);
224 last_vector_idx = vf->first_vector_idx + pf->num_msix_per_vf - 1;
226 /* clear VF MDD event information */
227 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
228 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
230 /* Disable interrupts so that VF starts in a known state */
231 for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
232 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
235 /* reset some of the state variables keeping track of the resources */
236 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
237 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
241 * ice_dis_vf_mappings
242 * @vf: pointer to the VF structure
244 static void ice_dis_vf_mappings(struct ice_vf *vf)
246 struct ice_pf *pf = vf->pf;
253 vsi = pf->vsi[vf->lan_vsi_idx];
255 dev = ice_pf_to_dev(pf);
256 wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
257 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
259 first = vf->first_vector_idx;
260 last = first + pf->num_msix_per_vf - 1;
261 for (v = first; v <= last; v++) {
264 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
265 GLINT_VECT2FUNC_IS_PF_M) |
266 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
267 GLINT_VECT2FUNC_PF_NUM_M));
268 wr32(hw, GLINT_VECT2FUNC(v), reg);
271 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
272 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
274 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
276 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
277 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
279 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
283 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
284 * @pf: pointer to the PF structure
286 * Since no MSIX entries are taken from the pf->irq_tracker then just clear
287 * the pf->sriov_base_vector.
289 * Returns 0 on success, and -EINVAL on error.
291 static int ice_sriov_free_msix_res(struct ice_pf *pf)
293 struct ice_res_tracker *res;
298 res = pf->irq_tracker;
302 /* give back irq_tracker resources used */
303 WARN_ON(pf->sriov_base_vector < res->num_entries);
305 pf->sriov_base_vector = 0;
311 * ice_set_vf_state_qs_dis - Set VF queues state to disabled
312 * @vf: pointer to the VF structure
314 void ice_set_vf_state_qs_dis(struct ice_vf *vf)
316 /* Clear Rx/Tx enabled queues flag */
317 bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
318 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
319 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
323 * ice_dis_vf_qs - Disable the VF queues
324 * @vf: pointer to the VF structure
326 static void ice_dis_vf_qs(struct ice_vf *vf)
328 struct ice_pf *pf = vf->pf;
331 vsi = pf->vsi[vf->lan_vsi_idx];
333 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
334 ice_vsi_stop_all_rx_rings(vsi);
335 ice_set_vf_state_qs_dis(vf);
339 * ice_free_vfs - Free all VFs
340 * @pf: pointer to the PF structure
342 void ice_free_vfs(struct ice_pf *pf)
344 struct device *dev = ice_pf_to_dev(pf);
345 struct ice_hw *hw = &pf->hw;
351 while (test_and_set_bit(__ICE_VF_DIS, pf->state))
352 usleep_range(1000, 2000);
354 /* Disable IOV before freeing resources. This lets any VF drivers
355 * running in the host get themselves cleaned up before we yank
356 * the carpet out from underneath their feet.
358 if (!pci_vfs_assigned(pf->pdev))
359 pci_disable_sriov(pf->pdev);
361 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
363 tmp = pf->num_alloc_vfs;
364 pf->num_qps_per_vf = 0;
365 pf->num_alloc_vfs = 0;
366 for (i = 0; i < tmp; i++) {
367 struct ice_vf *vf = &pf->vf[i];
369 mutex_lock(&vf->cfg_lock);
373 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
374 /* disable VF qp mappings and set VF disable state */
375 ice_dis_vf_mappings(vf);
376 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
380 mutex_unlock(&vf->cfg_lock);
382 mutex_destroy(&vf->cfg_lock);
385 if (ice_sriov_free_msix_res(pf))
386 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
388 devm_kfree(dev, pf->vf);
391 /* This check is for when the driver is unloaded while VFs are
392 * assigned. Setting the number of VFs to 0 through sysfs is caught
393 * before this function ever gets called.
395 if (!pci_vfs_assigned(pf->pdev)) {
398 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
399 * work correctly when SR-IOV gets re-enabled.
401 for (vf_id = 0; vf_id < tmp; vf_id++) {
402 u32 reg_idx, bit_idx;
404 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
405 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
406 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
409 clear_bit(__ICE_VF_DIS, pf->state);
410 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
414 * ice_trigger_vf_reset - Reset a VF on HW
415 * @vf: pointer to the VF structure
416 * @is_vflr: true if VFLR was issued, false if not
417 * @is_pfr: true if the reset was triggered due to a previous PFR
419 * Trigger hardware to start a reset for a particular VF. Expects the caller
420 * to wait the proper amount of time to allow hardware to reset the VF before
421 * it cleans up and restores VF functionality.
423 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
425 struct ice_pf *pf = vf->pf;
426 u32 reg, reg_idx, bit_idx;
427 unsigned int vf_abs_id, i;
431 dev = ice_pf_to_dev(pf);
433 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
435 /* Inform VF that it is no longer active, as a warning */
436 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
438 /* Disable VF's configuration API during reset. The flag is re-enabled
439 * when it's safe again to access VF's VSI.
441 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
443 /* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
444 * needs to clear them in the case of VFR/VFLR. If this is done for
445 * PFR, it can mess up VF resets because the VF driver may already
446 * have started cleanup by the time we get here.
449 wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
450 wr32(hw, VF_MBX_ATQLEN(vf->vf_id), 0);
453 /* In the case of a VFLR, the HW has already reset the VF and we
454 * just need to clean up, so don't hit the VFRTRIG register.
457 /* reset VF using VPGEN_VFRTRIG reg */
458 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
459 reg |= VPGEN_VFRTRIG_VFSWR_M;
460 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
462 /* clear the VFLR bit in GLGEN_VFLRSTAT */
463 reg_idx = (vf_abs_id) / 32;
464 bit_idx = (vf_abs_id) % 32;
465 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
468 wr32(hw, PF_PCI_CIAA,
469 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
470 for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
471 reg = rd32(hw, PF_PCI_CIAD);
472 /* no transactions pending so stop polling */
473 if ((reg & VF_TRANS_PENDING_M) == 0)
476 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
477 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
482 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
483 * @vsi: the VSI to update
484 * @pvid_info: VLAN ID and QoS used to set the PVID VSI context field
485 * @enable: true for enable PVID false for disable
487 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 pvid_info, bool enable)
489 struct ice_hw *hw = &vsi->back->hw;
490 struct ice_aqc_vsi_props *info;
491 struct ice_vsi_ctx *ctxt;
492 enum ice_status status;
495 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
499 ctxt->info = vsi->info;
502 info->vlan_flags = ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
503 ICE_AQ_VSI_PVLAN_INSERT_PVID |
504 ICE_AQ_VSI_VLAN_EMOD_STR;
505 info->sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
507 info->vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING |
508 ICE_AQ_VSI_VLAN_MODE_ALL;
509 info->sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
512 info->pvid = cpu_to_le16(pvid_info);
513 info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
514 ICE_AQ_VSI_PROP_SW_VALID);
516 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
518 dev_info(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %s aq_err %s\n",
519 ice_stat_str(status),
520 ice_aq_str(hw->adminq.sq_last_status));
525 vsi->info.vlan_flags = info->vlan_flags;
526 vsi->info.sw_flags2 = info->sw_flags2;
527 vsi->info.pvid = info->pvid;
534 * ice_vf_get_port_info - Get the VF's port info structure
535 * @vf: VF used to get the port info structure for
537 static struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
539 return vf->pf->hw.port_info;
543 * ice_vf_vsi_setup - Set up a VF VSI
544 * @vf: VF to setup VSI for
546 * Returns pointer to the successfully allocated VSI struct on success,
547 * otherwise returns NULL on failure.
549 static struct ice_vsi *ice_vf_vsi_setup(struct ice_vf *vf)
551 struct ice_port_info *pi = ice_vf_get_port_info(vf);
552 struct ice_pf *pf = vf->pf;
555 vsi = ice_vsi_setup(pf, pi, ICE_VSI_VF, vf->vf_id);
558 dev_err(ice_pf_to_dev(pf), "Failed to create VF VSI\n");
559 ice_vf_invalidate_vsi(vf);
563 vf->lan_vsi_idx = vsi->idx;
564 vf->lan_vsi_num = vsi->vsi_num;
570 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
571 * @pf: pointer to PF structure
572 * @vf: pointer to VF that the first MSIX vector index is being calculated for
574 * This returns the first MSIX vector index in PF space that is used by this VF.
575 * This index is used when accessing PF relative registers such as
576 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
577 * This will always be the OICR index in the AVF driver so any functionality
578 * using vf->first_vector_idx for queue configuration will have to increment by
579 * 1 to avoid meddling with the OICR index.
581 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
583 return pf->sriov_base_vector + vf->vf_id * pf->num_msix_per_vf;
587 * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
588 * @vf: VF to add MAC filters for
590 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
591 * always re-adds either a VLAN 0 or port VLAN based filter after reset.
593 static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf)
595 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
596 struct device *dev = ice_pf_to_dev(vf->pf);
600 if (vf->port_vlan_info) {
601 err = ice_vsi_manage_pvid(vsi, vf->port_vlan_info, true);
603 dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
608 vlan_id = vf->port_vlan_info & VLAN_VID_MASK;
611 /* vlan_id will either be 0 or the port VLAN number */
612 err = ice_vsi_add_vlan(vsi, vlan_id, ICE_FWD_TO_VSI);
614 dev_err(dev, "failed to add %s VLAN %u filter for VF %u, error %d\n",
615 vf->port_vlan_info ? "port" : "", vlan_id, vf->vf_id,
624 * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
625 * @vf: VF to add MAC filters for
627 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
628 * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
630 static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
632 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
633 struct device *dev = ice_pf_to_dev(vf->pf);
634 enum ice_status status;
635 u8 broadcast[ETH_ALEN];
637 eth_broadcast_addr(broadcast);
638 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
640 dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %s\n",
641 vf->vf_id, ice_stat_str(status));
642 return ice_status_to_errno(status);
647 if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
648 status = ice_fltr_add_mac(vsi, vf->dflt_lan_addr.addr,
651 dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %s\n",
652 &vf->dflt_lan_addr.addr[0], vf->vf_id,
653 ice_stat_str(status));
654 return ice_status_to_errno(status);
663 * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
664 * @vf: VF to configure trust setting for
666 static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
669 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
671 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
675 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
676 * @vf: VF to enable MSIX mappings for
678 * Some of the registers need to be indexed/configured using hardware global
679 * device values and other registers need 0-based values, which represent PF
682 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
684 int device_based_first_msix, device_based_last_msix;
685 int pf_based_first_msix, pf_based_last_msix, v;
686 struct ice_pf *pf = vf->pf;
687 int device_based_vf_id;
692 pf_based_first_msix = vf->first_vector_idx;
693 pf_based_last_msix = (pf_based_first_msix + pf->num_msix_per_vf) - 1;
695 device_based_first_msix = pf_based_first_msix +
696 pf->hw.func_caps.common_cap.msix_vector_first_id;
697 device_based_last_msix =
698 (device_based_first_msix + pf->num_msix_per_vf) - 1;
699 device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
701 reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
702 VPINT_ALLOC_FIRST_M) |
703 ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
704 VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
705 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
707 reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
708 & VPINT_ALLOC_PCI_FIRST_M) |
709 ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
710 VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
711 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
713 /* map the interrupts to its functions */
714 for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
715 reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
716 GLINT_VECT2FUNC_VF_NUM_M) |
717 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
718 GLINT_VECT2FUNC_PF_NUM_M));
719 wr32(hw, GLINT_VECT2FUNC(v), reg);
722 /* Map mailbox interrupt to VF MSI-X vector 0 */
723 wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
727 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
728 * @vf: VF to enable the mappings for
729 * @max_txq: max Tx queues allowed on the VF's VSI
730 * @max_rxq: max Rx queues allowed on the VF's VSI
732 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
734 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
735 struct device *dev = ice_pf_to_dev(vf->pf);
736 struct ice_hw *hw = &vf->pf->hw;
739 /* set regardless of mapping mode */
740 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
742 /* VF Tx queues allocation */
743 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
744 /* set the VF PF Tx queue range
745 * VFNUMQ value should be set to (number of queues - 1). A value
746 * of 0 means 1 queue and a value of 255 means 256 queues
748 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
749 VPLAN_TX_QBASE_VFFIRSTQ_M) |
750 (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
751 VPLAN_TX_QBASE_VFNUMQ_M));
752 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
754 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
757 /* set regardless of mapping mode */
758 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
760 /* VF Rx queues allocation */
761 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
762 /* set the VF PF Rx queue range
763 * VFNUMQ value should be set to (number of queues - 1). A value
764 * of 0 means 1 queue and a value of 255 means 256 queues
766 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
767 VPLAN_RX_QBASE_VFFIRSTQ_M) |
768 (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
769 VPLAN_RX_QBASE_VFNUMQ_M));
770 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
772 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
777 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
778 * @vf: pointer to the VF structure
780 static void ice_ena_vf_mappings(struct ice_vf *vf)
782 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
784 ice_ena_vf_msix_mappings(vf);
785 ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
790 * @pf: pointer to the PF structure
791 * @avail_res: available resources in the PF structure
792 * @max_res: maximum resources that can be given per VF
793 * @min_res: minimum resources that can be given per VF
795 * Returns non-zero value if resources (queues/vectors) are available or
796 * returns zero if PF cannot accommodate for all num_alloc_vfs.
799 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
801 bool checked_min_res = false;
804 /* start by checking if PF can assign max number of resources for
806 * if yes, return number per VF
807 * If no, divide by 2 and roundup, check again
808 * repeat the loop till we reach a point where even minimum resources
809 * are not available, in that case return 0
812 while ((res >= min_res) && !checked_min_res) {
815 num_all_res = pf->num_alloc_vfs * res;
816 if (num_all_res <= avail_res)
820 checked_min_res = true;
822 res = DIV_ROUND_UP(res, 2);
828 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
829 * @vf: VF to calculate the register index for
830 * @q_vector: a q_vector associated to the VF
832 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
836 if (!vf || !q_vector)
841 /* always add one to account for the OICR being the first MSIX */
842 return pf->sriov_base_vector + pf->num_msix_per_vf * vf->vf_id +
847 * ice_get_max_valid_res_idx - Get the max valid resource index
848 * @res: pointer to the resource to find the max valid index for
850 * Start from the end of the ice_res_tracker and return right when we find the
851 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
852 * valid for SR-IOV because it is the only consumer that manipulates the
853 * res->end and this is always called when res->end is set to res->num_entries.
855 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
862 for (i = res->num_entries - 1; i >= 0; i--)
863 if (res->list[i] & ICE_RES_VALID_BIT)
870 * ice_sriov_set_msix_res - Set any used MSIX resources
871 * @pf: pointer to PF structure
872 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
874 * This function allows SR-IOV resources to be taken from the end of the PF's
875 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
876 * just set the pf->sriov_base_vector and return success.
878 * If there are not enough resources available, return an error. This should
879 * always be caught by ice_set_per_vf_res().
881 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors
882 * in the PF's space available for SR-IOV.
884 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
886 u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
887 int vectors_used = pf->irq_tracker->num_entries;
888 int sriov_base_vector;
890 sriov_base_vector = total_vectors - num_msix_needed;
892 /* make sure we only grab irq_tracker entries from the list end and
893 * that we have enough available MSIX vectors
895 if (sriov_base_vector < vectors_used)
898 pf->sriov_base_vector = sriov_base_vector;
904 * ice_set_per_vf_res - check if vectors and queues are available
905 * @pf: pointer to the PF structure
907 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
908 * get more vectors and can enable more queues per VF. Note that this does not
909 * grab any vectors from the SW pool already allocated. Also note, that all
910 * vector counts include one for each VF's miscellaneous interrupt vector
913 * Minimum VFs - 2 vectors, 1 queue pair
914 * Small VFs - 5 vectors, 4 queue pairs
915 * Medium VFs - 17 vectors, 16 queue pairs
917 * Second, determine number of queue pairs per VF by starting with a pre-defined
918 * maximum each VF supports. If this is not possible, then we adjust based on
919 * queue pairs available on the device.
921 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
922 * by each VF during VF initialization and reset.
924 static int ice_set_per_vf_res(struct ice_pf *pf)
926 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
927 int msix_avail_per_vf, msix_avail_for_sriov;
928 struct device *dev = ice_pf_to_dev(pf);
929 u16 num_msix_per_vf, num_txq, num_rxq;
931 if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
934 /* determine MSI-X resources per VF */
935 msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
936 pf->irq_tracker->num_entries;
937 msix_avail_per_vf = msix_avail_for_sriov / pf->num_alloc_vfs;
938 if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
939 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
940 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
941 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
942 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MULTIQ_MIN) {
943 num_msix_per_vf = ICE_NUM_VF_MSIX_MULTIQ_MIN;
944 } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
945 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
947 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",
948 msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
953 /* determine queue resources per VF */
954 num_txq = ice_determine_res(pf, ice_get_avail_txq_count(pf),
956 num_msix_per_vf - ICE_NONQ_VECS_VF,
957 ICE_MAX_RSS_QS_PER_VF),
960 num_rxq = ice_determine_res(pf, ice_get_avail_rxq_count(pf),
962 num_msix_per_vf - ICE_NONQ_VECS_VF,
963 ICE_MAX_RSS_QS_PER_VF),
966 if (!num_txq || !num_rxq) {
967 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
968 ICE_MIN_QS_PER_VF, pf->num_alloc_vfs);
972 if (ice_sriov_set_msix_res(pf, num_msix_per_vf * pf->num_alloc_vfs)) {
973 dev_err(dev, "Unable to set MSI-X resources for %d VFs\n",
978 /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
979 pf->num_qps_per_vf = min_t(int, num_txq, num_rxq);
980 pf->num_msix_per_vf = num_msix_per_vf;
981 dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
982 pf->num_alloc_vfs, pf->num_msix_per_vf, pf->num_qps_per_vf);
988 * ice_clear_vf_reset_trigger - enable VF to access hardware
989 * @vf: VF to enabled hardware access for
991 static void ice_clear_vf_reset_trigger(struct ice_vf *vf)
993 struct ice_hw *hw = &vf->pf->hw;
996 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
997 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
998 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
1003 * ice_vf_set_vsi_promisc - set given VF VSI to given promiscuous mode(s)
1004 * @vf: pointer to the VF info
1005 * @vsi: the VSI being configured
1006 * @promisc_m: mask of promiscuous config bits
1007 * @rm_promisc: promisc flag request from the VF to remove or add filter
1009 * This function configures VF VSI promiscuous mode, based on the VF requests,
1010 * for Unicast, Multicast and VLAN
1012 static enum ice_status
1013 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m,
1016 struct ice_pf *pf = vf->pf;
1017 enum ice_status status = 0;
1021 if (vsi->num_vlan) {
1022 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
1024 } else if (vf->port_vlan_info) {
1026 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1027 vf->port_vlan_info);
1029 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1030 vf->port_vlan_info);
1033 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1036 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1043 static void ice_vf_clear_counters(struct ice_vf *vf)
1045 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
1049 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
1050 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
1054 * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
1055 * @vf: VF to perform pre VSI rebuild tasks
1057 * These tasks are items that don't need to be amortized since they are most
1058 * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
1060 static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
1062 ice_vf_clear_counters(vf);
1063 ice_clear_vf_reset_trigger(vf);
1067 * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
1068 * @vf: VF to rebuild host configuration on
1070 static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
1072 struct device *dev = ice_pf_to_dev(vf->pf);
1074 ice_vf_set_host_trust_cfg(vf);
1076 if (ice_vf_rebuild_host_mac_cfg(vf))
1077 dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
1080 if (ice_vf_rebuild_host_vlan_cfg(vf))
1081 dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
1086 * ice_vf_rebuild_vsi_with_release - release and setup the VF's VSI
1087 * @vf: VF to release and setup the VSI for
1089 * This is only called when a single VF is being reset (i.e. VFR, VFLR, host VF
1090 * configuration change, etc.).
1092 static int ice_vf_rebuild_vsi_with_release(struct ice_vf *vf)
1094 ice_vf_vsi_release(vf);
1095 if (!ice_vf_vsi_setup(vf))
1102 * ice_vf_rebuild_vsi - rebuild the VF's VSI
1103 * @vf: VF to rebuild the VSI for
1105 * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
1106 * host, PFR, CORER, etc.).
1108 static int ice_vf_rebuild_vsi(struct ice_vf *vf)
1110 struct ice_pf *pf = vf->pf;
1111 struct ice_vsi *vsi;
1113 vsi = pf->vsi[vf->lan_vsi_idx];
1115 if (ice_vsi_rebuild(vsi, true)) {
1116 dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
1120 /* vsi->idx will remain the same in this case so don't update
1123 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
1124 vf->lan_vsi_num = vsi->vsi_num;
1130 * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
1131 * @vf: VF to set in initialized state
1133 * After this function the VF will be ready to receive/handle the
1134 * VIRTCHNL_OP_GET_VF_RESOURCES message
1136 static void ice_vf_set_initialized(struct ice_vf *vf)
1138 ice_set_vf_state_qs_dis(vf);
1139 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
1140 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
1141 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
1142 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1146 * ice_vf_post_vsi_rebuild - tasks to do after the VF's VSI have been rebuilt
1147 * @vf: VF to perform tasks on
1149 static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
1151 struct ice_pf *pf = vf->pf;
1156 ice_vf_rebuild_host_cfg(vf);
1158 ice_vf_set_initialized(vf);
1159 ice_ena_vf_mappings(vf);
1160 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1164 * ice_reset_all_vfs - reset all allocated VFs in one go
1165 * @pf: pointer to the PF structure
1166 * @is_vflr: true if VFLR was issued, false if not
1168 * First, tell the hardware to reset each VF, then do all the waiting in one
1169 * chunk, and finally finish restoring each VF after the wait. This is useful
1170 * during PF routines which need to reset all VFs, as otherwise it must perform
1171 * these resets in a serialized fashion.
1173 * Returns true if any VFs were reset, and false otherwise.
1175 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1177 struct device *dev = ice_pf_to_dev(pf);
1178 struct ice_hw *hw = &pf->hw;
1182 /* If we don't have any VFs, then there is nothing to reset */
1183 if (!pf->num_alloc_vfs)
1186 /* If VFs have been disabled, there is no need to reset */
1187 if (test_and_set_bit(__ICE_VF_DIS, pf->state))
1190 /* Begin reset on all VFs at once */
1191 ice_for_each_vf(pf, v)
1192 ice_trigger_vf_reset(&pf->vf[v], is_vflr, true);
1194 /* HW requires some time to make sure it can flush the FIFO for a VF
1195 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1196 * sequence to make sure that it has completed. We'll keep track of
1197 * the VFs using a simple iterator that increments once that VF has
1198 * finished resetting.
1200 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1201 /* Check each VF in sequence */
1202 while (v < pf->num_alloc_vfs) {
1206 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1207 if (!(reg & VPGEN_VFRSTAT_VFRD_M)) {
1208 /* only delay if the check failed */
1209 usleep_range(10, 20);
1213 /* If the current VF has finished resetting, move on
1214 * to the next VF in sequence.
1220 /* Display a warning if at least one VF didn't manage to reset in
1221 * time, but continue on with the operation.
1223 if (v < pf->num_alloc_vfs)
1224 dev_warn(dev, "VF reset check timeout\n");
1226 /* free VF resources to begin resetting the VSI state */
1227 ice_for_each_vf(pf, v) {
1230 mutex_lock(&vf->cfg_lock);
1232 ice_vf_pre_vsi_rebuild(vf);
1233 ice_vf_rebuild_vsi(vf);
1234 ice_vf_post_vsi_rebuild(vf);
1236 mutex_unlock(&vf->cfg_lock);
1240 clear_bit(__ICE_VF_DIS, pf->state);
1246 * ice_is_vf_disabled
1247 * @vf: pointer to the VF info
1249 * Returns true if the PF or VF is disabled, false otherwise.
1251 static bool ice_is_vf_disabled(struct ice_vf *vf)
1253 struct ice_pf *pf = vf->pf;
1255 /* If the PF has been disabled, there is no need resetting VF until
1256 * PF is active again. Similarly, if the VF has been disabled, this
1257 * means something else is resetting the VF, so we shouldn't continue.
1258 * Otherwise, set disable VF state bit for actual reset, and continue.
1260 return (test_bit(__ICE_VF_DIS, pf->state) ||
1261 test_bit(ICE_VF_STATE_DIS, vf->vf_states));
1265 * ice_reset_vf - Reset a particular VF
1266 * @vf: pointer to the VF structure
1267 * @is_vflr: true if VFLR was issued, false if not
1269 * Returns true if the VF is currently in reset, resets successfully, or resets
1270 * are disabled and false otherwise.
1272 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1274 struct ice_pf *pf = vf->pf;
1275 struct ice_vsi *vsi;
1283 lockdep_assert_held(&vf->cfg_lock);
1285 dev = ice_pf_to_dev(pf);
1287 if (test_bit(__ICE_VF_RESETS_DISABLED, pf->state)) {
1288 dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
1293 if (ice_is_vf_disabled(vf)) {
1294 dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
1299 /* Set VF disable bit state here, before triggering reset */
1300 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1301 ice_trigger_vf_reset(vf, is_vflr, false);
1303 vsi = pf->vsi[vf->lan_vsi_idx];
1307 /* Call Disable LAN Tx queue AQ whether or not queues are
1308 * enabled. This is needed for successful completion of VFR.
1310 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1311 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1314 /* poll VPGEN_VFRSTAT reg to make sure
1315 * that reset is complete
1317 for (i = 0; i < 10; i++) {
1318 /* VF reset requires driver to first reset the VF and then
1319 * poll the status register to make sure that the reset
1320 * completed successfully.
1322 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1323 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1328 /* only sleep if the reset is not done */
1329 usleep_range(10, 20);
1332 /* Display a warning if VF didn't manage to reset in time, but need to
1333 * continue on with the operation.
1336 dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
1338 /* disable promiscuous modes in case they were enabled
1339 * ignore any error if disabling process failed
1341 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1342 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1343 if (vf->port_vlan_info || vsi->num_vlan)
1344 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1346 promisc_m = ICE_UCAST_PROMISC_BITS;
1348 vsi = pf->vsi[vf->lan_vsi_idx];
1349 if (ice_vf_set_vsi_promisc(vf, vsi, promisc_m, true))
1350 dev_err(dev, "disabling promiscuous mode failed\n");
1353 ice_vf_pre_vsi_rebuild(vf);
1355 if (ice_vf_rebuild_vsi_with_release(vf)) {
1356 dev_err(dev, "Failed to release and setup the VF%u's VSI\n", vf->vf_id);
1360 ice_vf_post_vsi_rebuild(vf);
1366 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1367 * @pf: pointer to the PF structure
1369 void ice_vc_notify_link_state(struct ice_pf *pf)
1373 ice_for_each_vf(pf, i)
1374 ice_vc_notify_vf_link_state(&pf->vf[i]);
1378 * ice_vc_notify_reset - Send pending reset message to all VFs
1379 * @pf: pointer to the PF structure
1381 * indicate a pending reset to all VFs on a given PF
1383 void ice_vc_notify_reset(struct ice_pf *pf)
1385 struct virtchnl_pf_event pfe;
1387 if (!pf->num_alloc_vfs)
1390 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1391 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1392 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1393 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1397 * ice_vc_notify_vf_reset - Notify VF of a reset event
1398 * @vf: pointer to the VF structure
1400 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1402 struct virtchnl_pf_event pfe;
1409 if (ice_validate_vf_id(pf, vf->vf_id))
1412 /* Bail out if VF is in disabled state, neither initialized, nor active
1413 * state - otherwise proceed with notifications
1415 if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1416 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
1417 test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1420 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1421 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1422 ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1423 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1428 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
1429 * @vf: VF to initialize/setup the VSI for
1431 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
1432 * VF VSI's broadcast filter and is only used during initial VF creation.
1434 static int ice_init_vf_vsi_res(struct ice_vf *vf)
1436 struct ice_pf *pf = vf->pf;
1437 u8 broadcast[ETH_ALEN];
1438 enum ice_status status;
1439 struct ice_vsi *vsi;
1443 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
1445 dev = ice_pf_to_dev(pf);
1446 vsi = ice_vf_vsi_setup(vf);
1450 err = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
1452 dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
1457 eth_broadcast_addr(broadcast);
1458 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
1460 dev_err(dev, "Failed to add broadcast MAC filter for VF %d, status %s\n",
1461 vf->vf_id, ice_stat_str(status));
1462 err = ice_status_to_errno(status);
1471 ice_vf_vsi_release(vf);
1476 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
1477 * @pf: PF the VFs are associated with
1479 static int ice_start_vfs(struct ice_pf *pf)
1481 struct ice_hw *hw = &pf->hw;
1484 ice_for_each_vf(pf, i) {
1485 struct ice_vf *vf = &pf->vf[i];
1487 ice_clear_vf_reset_trigger(vf);
1489 retval = ice_init_vf_vsi_res(vf);
1491 dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
1496 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1497 ice_ena_vf_mappings(vf);
1498 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1505 for (i = i - 1; i >= 0; i--) {
1506 struct ice_vf *vf = &pf->vf[i];
1508 ice_dis_vf_mappings(vf);
1509 ice_vf_vsi_release(vf);
1516 * ice_set_dflt_settings - set VF defaults during initialization/creation
1517 * @pf: PF holding reference to all VFs for default configuration
1519 static void ice_set_dflt_settings_vfs(struct ice_pf *pf)
1523 ice_for_each_vf(pf, i) {
1524 struct ice_vf *vf = &pf->vf[i];
1528 vf->vf_sw_id = pf->first_sw;
1529 /* assign default capabilities */
1530 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vf->vf_caps);
1531 vf->spoofchk = true;
1532 vf->num_vf_qs = pf->num_qps_per_vf;
1534 mutex_init(&vf->cfg_lock);
1539 * ice_alloc_vfs - allocate num_vfs in the PF structure
1540 * @pf: PF to store the allocated VFs in
1541 * @num_vfs: number of VFs to allocate
1543 static int ice_alloc_vfs(struct ice_pf *pf, int num_vfs)
1547 vfs = devm_kcalloc(ice_pf_to_dev(pf), num_vfs, sizeof(*vfs),
1553 pf->num_alloc_vfs = num_vfs;
1559 * ice_ena_vfs - enable VFs so they are ready to be used
1560 * @pf: pointer to the PF structure
1561 * @num_vfs: number of VFs to enable
1563 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
1565 struct device *dev = ice_pf_to_dev(pf);
1566 struct ice_hw *hw = &pf->hw;
1569 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1570 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1571 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1572 set_bit(__ICE_OICR_INTR_DIS, pf->state);
1575 ret = pci_enable_sriov(pf->pdev, num_vfs);
1577 pf->num_alloc_vfs = 0;
1578 goto err_unroll_intr;
1581 ret = ice_alloc_vfs(pf, num_vfs);
1583 goto err_pci_disable_sriov;
1585 if (ice_set_per_vf_res(pf)) {
1586 dev_err(dev, "Not enough resources for %d VFs, try with fewer number of VFs\n",
1589 goto err_unroll_sriov;
1592 ice_set_dflt_settings_vfs(pf);
1594 if (ice_start_vfs(pf)) {
1595 dev_err(dev, "Failed to start VF(s)\n");
1597 goto err_unroll_sriov;
1600 clear_bit(__ICE_VF_DIS, pf->state);
1604 devm_kfree(dev, pf->vf);
1606 pf->num_alloc_vfs = 0;
1607 err_pci_disable_sriov:
1608 pci_disable_sriov(pf->pdev);
1610 /* rearm interrupts here */
1611 ice_irq_dynamic_ena(hw, NULL, NULL);
1612 clear_bit(__ICE_OICR_INTR_DIS, pf->state);
1617 * ice_pci_sriov_ena - Enable or change number of VFs
1618 * @pf: pointer to the PF structure
1619 * @num_vfs: number of VFs to allocate
1621 * Returns 0 on success and negative on failure
1623 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1625 int pre_existing_vfs = pci_num_vf(pf->pdev);
1626 struct device *dev = ice_pf_to_dev(pf);
1629 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1631 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1634 if (num_vfs > pf->num_vfs_supported) {
1635 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1636 num_vfs, pf->num_vfs_supported);
1640 dev_info(dev, "Enabling %d VFs\n", num_vfs);
1641 err = ice_ena_vfs(pf, num_vfs);
1643 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1647 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1652 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
1653 * @pf: PF to enabled SR-IOV on
1655 static int ice_check_sriov_allowed(struct ice_pf *pf)
1657 struct device *dev = ice_pf_to_dev(pf);
1659 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1660 dev_err(dev, "This device is not capable of SR-IOV\n");
1664 if (ice_is_safe_mode(pf)) {
1665 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
1669 if (!ice_pf_state_is_nominal(pf)) {
1670 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1678 * ice_sriov_configure - Enable or change number of VFs via sysfs
1679 * @pdev: pointer to a pci_dev structure
1680 * @num_vfs: number of VFs to allocate or 0 to free VFs
1682 * This function is called when the user updates the number of VFs in sysfs. On
1683 * success return whatever num_vfs was set to by the caller. Return negative on
1686 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1688 struct ice_pf *pf = pci_get_drvdata(pdev);
1689 struct device *dev = ice_pf_to_dev(pf);
1692 err = ice_check_sriov_allowed(pf);
1697 if (!pci_vfs_assigned(pdev)) {
1702 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
1706 err = ice_pci_sriov_ena(pf, num_vfs);
1714 * ice_process_vflr_event - Free VF resources via IRQ calls
1715 * @pf: pointer to the PF structure
1717 * called from the VFLR IRQ handler to
1718 * free up VF resources and state variables
1720 void ice_process_vflr_event(struct ice_pf *pf)
1722 struct ice_hw *hw = &pf->hw;
1726 if (!test_and_clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1730 ice_for_each_vf(pf, vf_id) {
1731 struct ice_vf *vf = &pf->vf[vf_id];
1732 u32 reg_idx, bit_idx;
1734 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1735 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1736 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1737 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1738 if (reg & BIT(bit_idx)) {
1739 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1740 mutex_lock(&vf->cfg_lock);
1741 ice_reset_vf(vf, true);
1742 mutex_unlock(&vf->cfg_lock);
1748 * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
1749 * @vf: pointer to the VF info
1751 static void ice_vc_reset_vf(struct ice_vf *vf)
1753 ice_vc_notify_vf_reset(vf);
1754 ice_reset_vf(vf, false);
1758 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
1759 * @pf: PF used to index all VFs
1760 * @pfq: queue index relative to the PF's function space
1762 * If no VF is found who owns the pfq then return NULL, otherwise return a
1763 * pointer to the VF who owns the pfq
1765 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
1769 ice_for_each_vf(pf, vf_id) {
1770 struct ice_vf *vf = &pf->vf[vf_id];
1771 struct ice_vsi *vsi;
1774 vsi = pf->vsi[vf->lan_vsi_idx];
1776 ice_for_each_rxq(vsi, rxq_idx)
1777 if (vsi->rxq_map[rxq_idx] == pfq)
1785 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
1786 * @pf: PF used for conversion
1787 * @globalq: global queue index used to convert to PF space queue index
1789 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
1791 return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
1795 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
1796 * @pf: PF that the LAN overflow event happened on
1797 * @event: structure holding the event information for the LAN overflow event
1799 * Determine if the LAN overflow event was caused by a VF queue. If it was not
1800 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
1801 * reset on the offending VF.
1804 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1806 u32 gldcb_rtctq, queue;
1809 gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
1810 dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
1812 /* event returns device global Rx queue number */
1813 queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
1814 GLDCB_RTCTQ_RXQNUM_S;
1816 vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
1820 mutex_lock(&vf->cfg_lock);
1821 ice_vc_reset_vf(vf);
1822 mutex_unlock(&vf->cfg_lock);
1826 * ice_vc_send_msg_to_vf - Send message to VF
1827 * @vf: pointer to the VF info
1828 * @v_opcode: virtual channel opcode
1829 * @v_retval: virtual channel return value
1830 * @msg: pointer to the msg buffer
1831 * @msglen: msg length
1836 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
1837 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
1839 enum ice_status aq_ret;
1847 if (ice_validate_vf_id(pf, vf->vf_id))
1850 dev = ice_pf_to_dev(pf);
1852 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
1854 if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
1855 dev_info(dev, "Unable to send the message to VF %d ret %s aq_err %s\n",
1856 vf->vf_id, ice_stat_str(aq_ret),
1857 ice_aq_str(pf->hw.mailboxq.sq_last_status));
1865 * ice_vc_get_ver_msg
1866 * @vf: pointer to the VF info
1867 * @msg: pointer to the msg buffer
1869 * called from the VF to request the API version used by the PF
1871 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
1873 struct virtchnl_version_info info = {
1874 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1877 vf->vf_ver = *(struct virtchnl_version_info *)msg;
1878 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1879 if (VF_IS_V10(&vf->vf_ver))
1880 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1882 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1883 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
1884 sizeof(struct virtchnl_version_info));
1888 * ice_vc_get_max_frame_size - get max frame size allowed for VF
1889 * @vf: VF used to determine max frame size
1891 * Max frame size is determined based on the current port's max frame size and
1892 * whether a port VLAN is configured on this VF. The VF is not aware whether
1893 * it's in a port VLAN so the PF needs to account for this in max frame size
1894 * checks and sending the max frame size to the VF.
1896 static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
1898 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
1899 struct ice_port_info *pi = vsi->port_info;
1902 max_frame_size = pi->phy.link_info.max_frame_size;
1904 if (vf->port_vlan_info)
1905 max_frame_size -= VLAN_HLEN;
1907 return max_frame_size;
1911 * ice_vc_get_vf_res_msg
1912 * @vf: pointer to the VF info
1913 * @msg: pointer to the msg buffer
1915 * called from the VF to request its resources
1917 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
1919 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1920 struct virtchnl_vf_resource *vfres = NULL;
1921 struct ice_pf *pf = vf->pf;
1922 struct ice_vsi *vsi;
1926 if (ice_check_vf_init(pf, vf)) {
1927 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1931 len = sizeof(struct virtchnl_vf_resource);
1933 vfres = kzalloc(len, GFP_KERNEL);
1935 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1939 if (VF_IS_V11(&vf->vf_ver))
1940 vf->driver_caps = *(u32 *)msg;
1942 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1943 VIRTCHNL_VF_OFFLOAD_RSS_REG |
1944 VIRTCHNL_VF_OFFLOAD_VLAN;
1946 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1947 vsi = pf->vsi[vf->lan_vsi_idx];
1949 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1953 if (!vsi->info.pvid)
1954 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1956 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1957 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1959 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1960 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1962 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1965 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1966 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1968 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1969 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1971 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
1972 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1974 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
1975 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1977 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1978 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1980 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1981 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1983 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1984 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
1986 vfres->num_vsis = 1;
1987 /* Tx and Rx queue are equal for VF */
1988 vfres->num_queue_pairs = vsi->num_txq;
1989 vfres->max_vectors = pf->num_msix_per_vf;
1990 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
1991 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
1992 vfres->max_mtu = ice_vc_get_max_frame_size(vf);
1994 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
1995 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1996 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
1997 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1998 vf->dflt_lan_addr.addr);
2000 /* match guest capabilities */
2001 vf->driver_caps = vfres->vf_cap_flags;
2003 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
2006 /* send the response back to the VF */
2007 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
2015 * ice_vc_reset_vf_msg
2016 * @vf: pointer to the VF info
2018 * called from the VF to reset itself,
2019 * unlike other virtchnl messages, PF driver
2020 * doesn't send the response back to the VF
2022 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
2024 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2025 ice_reset_vf(vf, false);
2029 * ice_find_vsi_from_id
2030 * @pf: the PF structure to search for the VSI
2031 * @id: ID of the VSI it is searching for
2033 * searches for the VSI with the given ID
2035 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
2039 ice_for_each_vsi(pf, i)
2040 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
2047 * ice_vc_isvalid_vsi_id
2048 * @vf: pointer to the VF info
2049 * @vsi_id: VF relative VSI ID
2051 * check for the valid VSI ID
2053 static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
2055 struct ice_pf *pf = vf->pf;
2056 struct ice_vsi *vsi;
2058 vsi = ice_find_vsi_from_id(pf, vsi_id);
2060 return (vsi && (vsi->vf_id == vf->vf_id));
2064 * ice_vc_isvalid_q_id
2065 * @vf: pointer to the VF info
2067 * @qid: VSI relative queue ID
2069 * check for the valid queue ID
2071 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
2073 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
2074 /* allocated Tx and Rx queues should be always equal for VF VSI */
2075 return (vsi && (qid < vsi->alloc_txq));
2079 * ice_vc_isvalid_ring_len
2080 * @ring_len: length of ring
2082 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
2085 static bool ice_vc_isvalid_ring_len(u16 ring_len)
2087 return ring_len == 0 ||
2088 (ring_len >= ICE_MIN_NUM_DESC &&
2089 ring_len <= ICE_MAX_NUM_DESC &&
2090 !(ring_len % ICE_REQ_DESC_MULTIPLE));
2094 * ice_vc_config_rss_key
2095 * @vf: pointer to the VF info
2096 * @msg: pointer to the msg buffer
2098 * Configure the VF's RSS key
2100 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
2102 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2103 struct virtchnl_rss_key *vrk =
2104 (struct virtchnl_rss_key *)msg;
2105 struct ice_pf *pf = vf->pf;
2106 struct ice_vsi *vsi;
2108 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2109 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2113 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
2114 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2118 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
2119 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2123 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2124 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2128 vsi = pf->vsi[vf->lan_vsi_idx];
2130 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2134 if (ice_set_rss(vsi, vrk->key, NULL, 0))
2135 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2137 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
2142 * ice_vc_config_rss_lut
2143 * @vf: pointer to the VF info
2144 * @msg: pointer to the msg buffer
2146 * Configure the VF's RSS LUT
2148 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
2150 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2151 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2152 struct ice_pf *pf = vf->pf;
2153 struct ice_vsi *vsi;
2155 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2156 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2160 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
2161 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2165 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
2166 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2170 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2171 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2175 vsi = pf->vsi[vf->lan_vsi_idx];
2177 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2181 if (ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
2182 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2184 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
2189 * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
2190 * @vf: The VF being resseting
2192 * The max poll time is about ~800ms, which is about the maximum time it takes
2193 * for a VF to be reset and/or a VF driver to be removed.
2195 static void ice_wait_on_vf_reset(struct ice_vf *vf)
2199 for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
2200 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2202 msleep(ICE_MAX_VF_RESET_SLEEP_MS);
2207 * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
2208 * @vf: VF to check if it's ready to be configured/queried
2210 * The purpose of this function is to make sure the VF is not in reset, not
2211 * disabled, and initialized so it can be configured and/or queried by a host
2214 static int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
2218 ice_wait_on_vf_reset(vf);
2220 if (ice_is_vf_disabled(vf))
2224 if (ice_check_vf_init(pf, vf))
2231 * ice_set_vf_spoofchk
2232 * @netdev: network interface device structure
2233 * @vf_id: VF identifier
2234 * @ena: flag to enable or disable feature
2236 * Enable or disable VF spoof checking
2238 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2240 struct ice_netdev_priv *np = netdev_priv(netdev);
2241 struct ice_pf *pf = np->vsi->back;
2242 struct ice_vsi_ctx *ctx;
2243 struct ice_vsi *vf_vsi;
2244 enum ice_status status;
2249 dev = ice_pf_to_dev(pf);
2250 if (ice_validate_vf_id(pf, vf_id))
2253 vf = &pf->vf[vf_id];
2254 ret = ice_check_vf_ready_for_cfg(vf);
2258 vf_vsi = pf->vsi[vf->lan_vsi_idx];
2260 netdev_err(netdev, "VSI %d for VF %d is null\n",
2261 vf->lan_vsi_idx, vf->vf_id);
2265 if (vf_vsi->type != ICE_VSI_VF) {
2266 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
2267 vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
2271 if (ena == vf->spoofchk) {
2272 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
2276 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2280 ctx->info.sec_flags = vf_vsi->info.sec_flags;
2281 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2283 ctx->info.sec_flags |=
2284 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2285 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2286 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
2288 ctx->info.sec_flags &=
2289 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2290 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2291 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
2294 status = ice_update_vsi(&pf->hw, vf_vsi->idx, ctx, NULL);
2296 dev_err(dev, "Failed to %sable spoofchk on VF %d VSI %d\n error %s\n",
2297 ena ? "en" : "dis", vf->vf_id, vf_vsi->vsi_num,
2298 ice_stat_str(status));
2303 /* only update spoofchk state and VSI context on success */
2304 vf_vsi->info.sec_flags = ctx->info.sec_flags;
2313 * ice_is_any_vf_in_promisc - check if any VF(s) are in promiscuous mode
2314 * @pf: PF structure for accessing VF(s)
2316 * Return false if no VF(s) are in unicast and/or multicast promiscuous mode,
2319 bool ice_is_any_vf_in_promisc(struct ice_pf *pf)
2323 ice_for_each_vf(pf, vf_idx) {
2324 struct ice_vf *vf = &pf->vf[vf_idx];
2326 /* found a VF that has promiscuous mode configured */
2327 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2328 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2336 * ice_vc_cfg_promiscuous_mode_msg
2337 * @vf: pointer to the VF info
2338 * @msg: pointer to the msg buffer
2340 * called from the VF to configure VF VSIs promiscuous mode
2342 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
2344 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2345 struct virtchnl_promisc_info *info =
2346 (struct virtchnl_promisc_info *)msg;
2347 struct ice_pf *pf = vf->pf;
2348 struct ice_vsi *vsi;
2353 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2354 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2358 if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
2359 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2363 vsi = pf->vsi[vf->lan_vsi_idx];
2365 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2369 dev = ice_pf_to_dev(pf);
2370 if (!test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2371 dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
2373 /* Leave v_ret alone, lie to the VF on purpose. */
2377 rm_promisc = !(info->flags & FLAG_VF_UNICAST_PROMISC) &&
2378 !(info->flags & FLAG_VF_MULTICAST_PROMISC);
2380 if (vsi->num_vlan || vf->port_vlan_info) {
2381 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
2382 struct net_device *pf_netdev;
2385 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2389 pf_netdev = pf_vsi->netdev;
2391 ret = ice_set_vf_spoofchk(pf_netdev, vf->vf_id, rm_promisc);
2393 dev_err(dev, "Failed to update spoofchk to %s for VF %d VSI %d when setting promiscuous mode\n",
2394 rm_promisc ? "ON" : "OFF", vf->vf_id,
2396 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2399 ret = ice_cfg_vlan_pruning(vsi, true, !rm_promisc);
2401 dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
2402 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2407 if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
2408 bool set_dflt_vsi = !!(info->flags & FLAG_VF_UNICAST_PROMISC);
2410 if (set_dflt_vsi && !ice_is_dflt_vsi_in_use(pf->first_sw))
2411 /* only attempt to set the default forwarding VSI if
2412 * it's not currently set
2414 ret = ice_set_dflt_vsi(pf->first_sw, vsi);
2415 else if (!set_dflt_vsi &&
2416 ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
2417 /* only attempt to free the default forwarding VSI if we
2420 ret = ice_clear_dflt_vsi(pf->first_sw);
2423 dev_err(dev, "%sable VF %d as the default VSI failed, error %d\n",
2424 set_dflt_vsi ? "en" : "dis", vf->vf_id, ret);
2425 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2429 enum ice_status status;
2432 if (info->flags & FLAG_VF_UNICAST_PROMISC) {
2433 if (vf->port_vlan_info || vsi->num_vlan)
2434 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
2436 promisc_m = ICE_UCAST_PROMISC_BITS;
2437 } else if (info->flags & FLAG_VF_MULTICAST_PROMISC) {
2438 if (vf->port_vlan_info || vsi->num_vlan)
2439 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
2441 promisc_m = ICE_MCAST_PROMISC_BITS;
2443 if (vf->port_vlan_info || vsi->num_vlan)
2444 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
2446 promisc_m = ICE_UCAST_PROMISC_BITS;
2449 /* Configure multicast/unicast with or without VLAN promiscuous
2452 status = ice_vf_set_vsi_promisc(vf, vsi, promisc_m, rm_promisc);
2454 dev_err(dev, "%sable Tx/Rx filter promiscuous mode on VF-%d failed, error: %s\n",
2455 rm_promisc ? "dis" : "en", vf->vf_id,
2456 ice_stat_str(status));
2457 v_ret = ice_err_to_virt_err(status);
2460 dev_dbg(dev, "%sable Tx/Rx filter promiscuous mode on VF-%d succeeded\n",
2461 rm_promisc ? "dis" : "en", vf->vf_id);
2465 if (info->flags & FLAG_VF_MULTICAST_PROMISC)
2466 set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
2468 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
2470 if (info->flags & FLAG_VF_UNICAST_PROMISC)
2471 set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
2473 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
2476 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
2481 * ice_vc_get_stats_msg
2482 * @vf: pointer to the VF info
2483 * @msg: pointer to the msg buffer
2485 * called from the VF to get VSI stats
2487 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
2489 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2490 struct virtchnl_queue_select *vqs =
2491 (struct virtchnl_queue_select *)msg;
2492 struct ice_eth_stats stats = { 0 };
2493 struct ice_pf *pf = vf->pf;
2494 struct ice_vsi *vsi;
2496 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2497 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2501 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2502 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2506 vsi = pf->vsi[vf->lan_vsi_idx];
2508 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2512 ice_update_eth_stats(vsi);
2514 stats = vsi->eth_stats;
2517 /* send the response to the VF */
2518 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
2519 (u8 *)&stats, sizeof(stats));
2523 * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
2524 * @vqs: virtchnl_queue_select structure containing bitmaps to validate
2526 * Return true on successful validation, else false
2528 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
2530 if ((!vqs->rx_queues && !vqs->tx_queues) ||
2531 vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
2532 vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
2539 * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
2540 * @vsi: VSI of the VF to configure
2541 * @q_idx: VF queue index used to determine the queue in the PF's space
2543 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
2545 struct ice_hw *hw = &vsi->back->hw;
2546 u32 pfq = vsi->txq_map[q_idx];
2549 reg = rd32(hw, QINT_TQCTL(pfq));
2551 /* MSI-X index 0 in the VF's space is always for the OICR, which means
2552 * this is most likely a poll mode VF driver, so don't enable an
2553 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
2555 if (!(reg & QINT_TQCTL_MSIX_INDX_M))
2558 wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
2562 * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
2563 * @vsi: VSI of the VF to configure
2564 * @q_idx: VF queue index used to determine the queue in the PF's space
2566 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
2568 struct ice_hw *hw = &vsi->back->hw;
2569 u32 pfq = vsi->rxq_map[q_idx];
2572 reg = rd32(hw, QINT_RQCTL(pfq));
2574 /* MSI-X index 0 in the VF's space is always for the OICR, which means
2575 * this is most likely a poll mode VF driver, so don't enable an
2576 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
2578 if (!(reg & QINT_RQCTL_MSIX_INDX_M))
2581 wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
2586 * @vf: pointer to the VF info
2587 * @msg: pointer to the msg buffer
2589 * called from the VF to enable all or specific queue(s)
2591 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
2593 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2594 struct virtchnl_queue_select *vqs =
2595 (struct virtchnl_queue_select *)msg;
2596 struct ice_pf *pf = vf->pf;
2597 struct ice_vsi *vsi;
2598 unsigned long q_map;
2601 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2602 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2606 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2607 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2611 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
2612 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2616 vsi = pf->vsi[vf->lan_vsi_idx];
2618 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2622 /* Enable only Rx rings, Tx rings were enabled by the FW when the
2623 * Tx queue group list was configured and the context bits were
2624 * programmed using ice_vsi_cfg_txqs
2626 q_map = vqs->rx_queues;
2627 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2628 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2629 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2633 /* Skip queue if enabled */
2634 if (test_bit(vf_q_id, vf->rxq_ena))
2637 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
2638 dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
2639 vf_q_id, vsi->vsi_num);
2640 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2644 ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
2645 set_bit(vf_q_id, vf->rxq_ena);
2648 vsi = pf->vsi[vf->lan_vsi_idx];
2649 q_map = vqs->tx_queues;
2650 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2651 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2652 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2656 /* Skip queue if enabled */
2657 if (test_bit(vf_q_id, vf->txq_ena))
2660 ice_vf_ena_txq_interrupt(vsi, vf_q_id);
2661 set_bit(vf_q_id, vf->txq_ena);
2664 /* Set flag to indicate that queues are enabled */
2665 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
2666 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
2669 /* send the response to the VF */
2670 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
2676 * @vf: pointer to the VF info
2677 * @msg: pointer to the msg buffer
2679 * called from the VF to disable all or specific
2682 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
2684 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2685 struct virtchnl_queue_select *vqs =
2686 (struct virtchnl_queue_select *)msg;
2687 struct ice_pf *pf = vf->pf;
2688 struct ice_vsi *vsi;
2689 unsigned long q_map;
2692 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
2693 !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
2694 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2698 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2699 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2703 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
2704 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2708 vsi = pf->vsi[vf->lan_vsi_idx];
2710 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2714 if (vqs->tx_queues) {
2715 q_map = vqs->tx_queues;
2717 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2718 struct ice_ring *ring = vsi->tx_rings[vf_q_id];
2719 struct ice_txq_meta txq_meta = { 0 };
2721 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2722 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2726 if (!test_bit(vf_q_id, vf->txq_ena))
2727 dev_dbg(ice_pf_to_dev(vsi->back), "Queue %u on VSI %u is not enabled, but stopping it anyway\n",
2728 vf_q_id, vsi->vsi_num);
2730 ice_fill_txq_meta(vsi, ring, &txq_meta);
2732 if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
2734 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
2735 vf_q_id, vsi->vsi_num);
2736 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2740 /* Clear enabled queues flag */
2741 clear_bit(vf_q_id, vf->txq_ena);
2745 q_map = vqs->rx_queues;
2746 /* speed up Rx queue disable by batching them if possible */
2748 bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
2749 if (ice_vsi_stop_all_rx_rings(vsi)) {
2750 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
2752 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2756 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
2758 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2759 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2760 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2764 /* Skip queue if not enabled */
2765 if (!test_bit(vf_q_id, vf->rxq_ena))
2768 if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
2770 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
2771 vf_q_id, vsi->vsi_num);
2772 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2776 /* Clear enabled queues flag */
2777 clear_bit(vf_q_id, vf->rxq_ena);
2781 /* Clear enabled queues flag */
2782 if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
2783 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
2786 /* send the response to the VF */
2787 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
2793 * @vf: pointer to the VF info
2794 * @vsi: the VSI being configured
2795 * @vector_id: vector ID
2796 * @map: vector map for mapping vectors to queues
2797 * @q_vector: structure for interrupt vector
2798 * configure the IRQ to queue map
2801 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
2802 struct virtchnl_vector_map *map,
2803 struct ice_q_vector *q_vector)
2805 u16 vsi_q_id, vsi_q_id_idx;
2808 q_vector->num_ring_rx = 0;
2809 q_vector->num_ring_tx = 0;
2811 qmap = map->rxq_map;
2812 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
2813 vsi_q_id = vsi_q_id_idx;
2815 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
2816 return VIRTCHNL_STATUS_ERR_PARAM;
2818 q_vector->num_ring_rx++;
2819 q_vector->rx.itr_idx = map->rxitr_idx;
2820 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
2821 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
2822 q_vector->rx.itr_idx);
2825 qmap = map->txq_map;
2826 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
2827 vsi_q_id = vsi_q_id_idx;
2829 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
2830 return VIRTCHNL_STATUS_ERR_PARAM;
2832 q_vector->num_ring_tx++;
2833 q_vector->tx.itr_idx = map->txitr_idx;
2834 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
2835 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
2836 q_vector->tx.itr_idx);
2839 return VIRTCHNL_STATUS_SUCCESS;
2843 * ice_vc_cfg_irq_map_msg
2844 * @vf: pointer to the VF info
2845 * @msg: pointer to the msg buffer
2847 * called from the VF to configure the IRQ to queue map
2849 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
2851 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2852 u16 num_q_vectors_mapped, vsi_id, vector_id;
2853 struct virtchnl_irq_map_info *irqmap_info;
2854 struct virtchnl_vector_map *map;
2855 struct ice_pf *pf = vf->pf;
2856 struct ice_vsi *vsi;
2859 irqmap_info = (struct virtchnl_irq_map_info *)msg;
2860 num_q_vectors_mapped = irqmap_info->num_vectors;
2862 /* Check to make sure number of VF vectors mapped is not greater than
2863 * number of VF vectors originally allocated, and check that
2864 * there is actually at least a single VF queue vector mapped
2866 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2867 pf->num_msix_per_vf < num_q_vectors_mapped ||
2868 !num_q_vectors_mapped) {
2869 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2873 vsi = pf->vsi[vf->lan_vsi_idx];
2875 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2879 for (i = 0; i < num_q_vectors_mapped; i++) {
2880 struct ice_q_vector *q_vector;
2882 map = &irqmap_info->vecmap[i];
2884 vector_id = map->vector_id;
2885 vsi_id = map->vsi_id;
2886 /* vector_id is always 0-based for each VF, and can never be
2887 * larger than or equal to the max allowed interrupts per VF
2889 if (!(vector_id < pf->num_msix_per_vf) ||
2890 !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
2891 (!vector_id && (map->rxq_map || map->txq_map))) {
2892 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2896 /* No need to map VF miscellaneous or rogue vector */
2900 /* Subtract non queue vector from vector_id passed by VF
2901 * to get actual number of VSI queue vector array index
2903 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
2905 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2909 /* lookout for the invalid queue index */
2910 v_ret = (enum virtchnl_status_code)
2911 ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
2917 /* send the response to the VF */
2918 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
2924 * @vf: pointer to the VF info
2925 * @msg: pointer to the msg buffer
2927 * called from the VF to configure the Rx/Tx queues
2929 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
2931 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2932 struct virtchnl_vsi_queue_config_info *qci =
2933 (struct virtchnl_vsi_queue_config_info *)msg;
2934 struct virtchnl_queue_pair_info *qpi;
2935 u16 num_rxq = 0, num_txq = 0;
2936 struct ice_pf *pf = vf->pf;
2937 struct ice_vsi *vsi;
2940 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2941 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2945 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
2946 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2950 vsi = pf->vsi[vf->lan_vsi_idx];
2952 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2956 if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
2957 qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
2958 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
2959 vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
2960 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2964 for (i = 0; i < qci->num_queue_pairs; i++) {
2965 qpi = &qci->qpair[i];
2966 if (qpi->txq.vsi_id != qci->vsi_id ||
2967 qpi->rxq.vsi_id != qci->vsi_id ||
2968 qpi->rxq.queue_id != qpi->txq.queue_id ||
2969 qpi->txq.headwb_enabled ||
2970 !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
2971 !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
2972 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
2973 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2976 /* copy Tx queue info from VF into VSI */
2977 if (qpi->txq.ring_len > 0) {
2979 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
2980 vsi->tx_rings[i]->count = qpi->txq.ring_len;
2983 /* copy Rx queue info from VF into VSI */
2984 if (qpi->rxq.ring_len > 0) {
2985 u16 max_frame_size = ice_vc_get_max_frame_size(vf);
2988 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
2989 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
2991 if (qpi->rxq.databuffer_size != 0 &&
2992 (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
2993 qpi->rxq.databuffer_size < 1024)) {
2994 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2997 vsi->rx_buf_len = qpi->rxq.databuffer_size;
2998 vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
2999 if (qpi->rxq.max_pkt_size > max_frame_size ||
3000 qpi->rxq.max_pkt_size < 64) {
3001 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3006 vsi->max_frame = qpi->rxq.max_pkt_size;
3007 /* add space for the port VLAN since the VF driver is not
3008 * expected to account for it in the MTU calculation
3010 if (vf->port_vlan_info)
3011 vsi->max_frame += VLAN_HLEN;
3014 /* VF can request to configure less than allocated queues or default
3015 * allocated queues. So update the VSI with new number
3017 vsi->num_txq = num_txq;
3018 vsi->num_rxq = num_rxq;
3019 /* All queues of VF VSI are in TC 0 */
3020 vsi->tc_cfg.tc_info[0].qcount_tx = num_txq;
3021 vsi->tc_cfg.tc_info[0].qcount_rx = num_rxq;
3023 if (ice_vsi_cfg_lan_txqs(vsi) || ice_vsi_cfg_rxqs(vsi))
3024 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3027 /* send the response to the VF */
3028 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
3034 * @vf: pointer to the VF info
3036 static bool ice_is_vf_trusted(struct ice_vf *vf)
3038 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
3042 * ice_can_vf_change_mac
3043 * @vf: pointer to the VF info
3045 * Return true if the VF is allowed to change its MAC filters, false otherwise
3047 static bool ice_can_vf_change_mac(struct ice_vf *vf)
3049 /* If the VF MAC address has been set administratively (via the
3050 * ndo_set_vf_mac command), then deny permission to the VF to
3051 * add/delete unicast MAC addresses, unless the VF is trusted
3053 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
3060 * ice_vc_add_mac_addr - attempt to add the MAC address passed in
3061 * @vf: pointer to the VF info
3062 * @vsi: pointer to the VF's VSI
3063 * @mac_addr: MAC address to add
3066 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr)
3068 struct device *dev = ice_pf_to_dev(vf->pf);
3069 enum ice_status status;
3072 /* default unicast MAC already added */
3073 if (ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
3076 if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
3077 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
3081 status = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3082 if (status == ICE_ERR_ALREADY_EXISTS) {
3083 dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
3085 /* don't return since we might need to update
3086 * the primary MAC in ice_vfhw_mac_add() below
3089 } else if (status) {
3090 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %s\n",
3091 mac_addr, vf->vf_id, ice_stat_str(status));
3097 /* Set the default LAN address to the latest unicast MAC address added
3098 * by the VF. The default LAN address is reported by the PF via
3099 * ndo_get_vf_config.
3101 if (is_unicast_ether_addr(mac_addr))
3102 ether_addr_copy(vf->dflt_lan_addr.addr, mac_addr);
3108 * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
3109 * @vf: pointer to the VF info
3110 * @vsi: pointer to the VF's VSI
3111 * @mac_addr: MAC address to delete
3114 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr)
3116 struct device *dev = ice_pf_to_dev(vf->pf);
3117 enum ice_status status;
3119 if (!ice_can_vf_change_mac(vf) &&
3120 ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
3123 status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3124 if (status == ICE_ERR_DOES_NOT_EXIST) {
3125 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
3128 } else if (status) {
3129 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %s\n",
3130 mac_addr, vf->vf_id, ice_stat_str(status));
3134 if (ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
3135 eth_zero_addr(vf->dflt_lan_addr.addr);
3143 * ice_vc_handle_mac_addr_msg
3144 * @vf: pointer to the VF info
3145 * @msg: pointer to the msg buffer
3146 * @set: true if MAC filters are being set, false otherwise
3148 * add guest MAC address filter
3151 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
3153 int (*ice_vc_cfg_mac)
3154 (struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr);
3155 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3156 struct virtchnl_ether_addr_list *al =
3157 (struct virtchnl_ether_addr_list *)msg;
3158 struct ice_pf *pf = vf->pf;
3159 enum virtchnl_ops vc_op;
3160 struct ice_vsi *vsi;
3164 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
3165 ice_vc_cfg_mac = ice_vc_add_mac_addr;
3167 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
3168 ice_vc_cfg_mac = ice_vc_del_mac_addr;
3171 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3172 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3173 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3174 goto handle_mac_exit;
3177 /* If this VF is not privileged, then we can't add more than a
3178 * limited number of addresses. Check to make sure that the
3179 * additions do not push us over the limit.
3181 if (set && !ice_is_vf_trusted(vf) &&
3182 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
3183 dev_err(ice_pf_to_dev(pf), "Can't add more MAC addresses, because VF-%d is not trusted, switch the VF to trusted mode in order to add more functionalities\n",
3185 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3186 goto handle_mac_exit;
3189 vsi = pf->vsi[vf->lan_vsi_idx];
3191 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3192 goto handle_mac_exit;
3195 for (i = 0; i < al->num_elements; i++) {
3196 u8 *mac_addr = al->list[i].addr;
3199 if (is_broadcast_ether_addr(mac_addr) ||
3200 is_zero_ether_addr(mac_addr))
3203 result = ice_vc_cfg_mac(vf, vsi, mac_addr);
3204 if (result == -EEXIST || result == -ENOENT) {
3206 } else if (result) {
3207 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3208 goto handle_mac_exit;
3213 /* send the response to the VF */
3214 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
3218 * ice_vc_add_mac_addr_msg
3219 * @vf: pointer to the VF info
3220 * @msg: pointer to the msg buffer
3222 * add guest MAC address filter
3224 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
3226 return ice_vc_handle_mac_addr_msg(vf, msg, true);
3230 * ice_vc_del_mac_addr_msg
3231 * @vf: pointer to the VF info
3232 * @msg: pointer to the msg buffer
3234 * remove guest MAC address filter
3236 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
3238 return ice_vc_handle_mac_addr_msg(vf, msg, false);
3242 * ice_vc_request_qs_msg
3243 * @vf: pointer to the VF info
3244 * @msg: pointer to the msg buffer
3246 * VFs get a default number of queues but can use this message to request a
3247 * different number. If the request is successful, PF will reset the VF and
3248 * return 0. If unsuccessful, PF will send message informing VF of number of
3249 * available queue pairs via virtchnl message response to VF.
3251 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
3253 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3254 struct virtchnl_vf_res_request *vfres =
3255 (struct virtchnl_vf_res_request *)msg;
3256 u16 req_queues = vfres->num_queue_pairs;
3257 struct ice_pf *pf = vf->pf;
3258 u16 max_allowed_vf_queues;
3259 u16 tx_rx_queue_left;
3263 dev = ice_pf_to_dev(pf);
3264 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3265 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3269 cur_queues = vf->num_vf_qs;
3270 tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
3271 ice_get_avail_rxq_count(pf));
3272 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
3274 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
3276 } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
3277 dev_err(dev, "VF %d tried to request more than %d queues.\n",
3278 vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
3279 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
3280 } else if (req_queues > cur_queues &&
3281 req_queues - cur_queues > tx_rx_queue_left) {
3282 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
3283 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
3284 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
3285 ICE_MAX_RSS_QS_PER_VF);
3287 /* request is successful, then reset VF */
3288 vf->num_req_qs = req_queues;
3289 ice_vc_reset_vf(vf);
3290 dev_info(dev, "VF %d granted request of %u queues.\n",
3291 vf->vf_id, req_queues);
3296 /* send the response to the VF */
3297 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
3298 v_ret, (u8 *)vfres, sizeof(*vfres));
3302 * ice_set_vf_port_vlan
3303 * @netdev: network interface device structure
3304 * @vf_id: VF identifier
3305 * @vlan_id: VLAN ID being set
3306 * @qos: priority setting
3307 * @vlan_proto: VLAN protocol
3309 * program VF Port VLAN ID and/or QoS
3312 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
3315 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3321 dev = ice_pf_to_dev(pf);
3322 if (ice_validate_vf_id(pf, vf_id))
3325 if (vlan_id >= VLAN_N_VID || qos > 7) {
3326 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
3327 vf_id, vlan_id, qos);
3331 if (vlan_proto != htons(ETH_P_8021Q)) {
3332 dev_err(dev, "VF VLAN protocol is not supported\n");
3333 return -EPROTONOSUPPORT;
3336 vf = &pf->vf[vf_id];
3337 ret = ice_check_vf_ready_for_cfg(vf);
3341 vlanprio = vlan_id | (qos << VLAN_PRIO_SHIFT);
3343 if (vf->port_vlan_info == vlanprio) {
3344 /* duplicate request, so just return success */
3345 dev_dbg(dev, "Duplicate pvid %d request\n", vlanprio);
3349 mutex_lock(&vf->cfg_lock);
3351 vf->port_vlan_info = vlanprio;
3353 if (vf->port_vlan_info)
3354 dev_info(dev, "Setting VLAN %d, QoS 0x%x on VF %d\n",
3355 vlan_id, qos, vf_id);
3357 dev_info(dev, "Clearing port VLAN on VF %d\n", vf_id);
3359 ice_vc_reset_vf(vf);
3360 mutex_unlock(&vf->cfg_lock);
3366 * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
3367 * @caps: VF driver negotiated capabilities
3369 * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
3371 static bool ice_vf_vlan_offload_ena(u32 caps)
3373 return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
3377 * ice_vc_process_vlan_msg
3378 * @vf: pointer to the VF info
3379 * @msg: pointer to the msg buffer
3380 * @add_v: Add VLAN if true, otherwise delete VLAN
3382 * Process virtchnl op to add or remove programmed guest VLAN ID
3384 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
3386 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3387 struct virtchnl_vlan_filter_list *vfl =
3388 (struct virtchnl_vlan_filter_list *)msg;
3389 struct ice_pf *pf = vf->pf;
3390 bool vlan_promisc = false;
3391 struct ice_vsi *vsi;
3398 dev = ice_pf_to_dev(pf);
3399 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3400 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3404 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
3405 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3409 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
3410 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3414 for (i = 0; i < vfl->num_elements; i++) {
3415 if (vfl->vlan_id[i] >= VLAN_N_VID) {
3416 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3417 dev_err(dev, "invalid VF VLAN id %d\n",
3424 vsi = pf->vsi[vf->lan_vsi_idx];
3426 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3430 if (add_v && !ice_is_vf_trusted(vf) &&
3431 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
3432 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
3434 /* There is no need to let VF know about being not trusted,
3435 * so we can just return success message here
3440 if (vsi->info.pvid) {
3441 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3445 if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
3446 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
3447 test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags))
3448 vlan_promisc = true;
3451 for (i = 0; i < vfl->num_elements; i++) {
3452 u16 vid = vfl->vlan_id[i];
3454 if (!ice_is_vf_trusted(vf) &&
3455 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
3456 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
3458 /* There is no need to let VF know about being
3459 * not trusted, so we can just return success
3460 * message here as well.
3465 /* we add VLAN 0 by default for each VF so we can enable
3466 * Tx VLAN anti-spoof without triggering MDD events so
3467 * we don't need to add it again here
3472 status = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
3474 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3478 /* Enable VLAN pruning when non-zero VLAN is added */
3479 if (!vlan_promisc && vid &&
3480 !ice_vsi_is_vlan_pruning_ena(vsi)) {
3481 status = ice_cfg_vlan_pruning(vsi, true, false);
3483 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3484 dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
3488 } else if (vlan_promisc) {
3489 /* Enable Ucast/Mcast VLAN promiscuous mode */
3490 promisc_m = ICE_PROMISC_VLAN_TX |
3491 ICE_PROMISC_VLAN_RX;
3493 status = ice_set_vsi_promisc(hw, vsi->idx,
3496 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3497 dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
3503 /* In case of non_trusted VF, number of VLAN elements passed
3504 * to PF for removal might be greater than number of VLANs
3505 * filter programmed for that VF - So, use actual number of
3506 * VLANS added earlier with add VLAN opcode. In order to avoid
3507 * removing VLAN that doesn't exist, which result to sending
3508 * erroneous failed message back to the VF
3512 num_vf_vlan = vsi->num_vlan;
3513 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
3514 u16 vid = vfl->vlan_id[i];
3516 /* we add VLAN 0 by default for each VF so we can enable
3517 * Tx VLAN anti-spoof without triggering MDD events so
3518 * we don't want a VIRTCHNL request to remove it
3523 /* Make sure ice_vsi_kill_vlan is successful before
3524 * updating VLAN information
3526 status = ice_vsi_kill_vlan(vsi, vid);
3528 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3532 /* Disable VLAN pruning when only VLAN 0 is left */
3533 if (vsi->num_vlan == 1 &&
3534 ice_vsi_is_vlan_pruning_ena(vsi))
3535 ice_cfg_vlan_pruning(vsi, false, false);
3537 /* Disable Unicast/Multicast VLAN promiscuous mode */
3539 promisc_m = ICE_PROMISC_VLAN_TX |
3540 ICE_PROMISC_VLAN_RX;
3542 ice_clear_vsi_promisc(hw, vsi->idx,
3549 /* send the response to the VF */
3551 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
3554 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
3559 * ice_vc_add_vlan_msg
3560 * @vf: pointer to the VF info
3561 * @msg: pointer to the msg buffer
3563 * Add and program guest VLAN ID
3565 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
3567 return ice_vc_process_vlan_msg(vf, msg, true);
3571 * ice_vc_remove_vlan_msg
3572 * @vf: pointer to the VF info
3573 * @msg: pointer to the msg buffer
3575 * remove programmed guest VLAN ID
3577 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
3579 return ice_vc_process_vlan_msg(vf, msg, false);
3583 * ice_vc_ena_vlan_stripping
3584 * @vf: pointer to the VF info
3586 * Enable VLAN header stripping for a given VF
3588 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
3590 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3591 struct ice_pf *pf = vf->pf;
3592 struct ice_vsi *vsi;
3594 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3595 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3599 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
3600 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3604 vsi = pf->vsi[vf->lan_vsi_idx];
3605 if (ice_vsi_manage_vlan_stripping(vsi, true))
3606 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3609 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
3614 * ice_vc_dis_vlan_stripping
3615 * @vf: pointer to the VF info
3617 * Disable VLAN header stripping for a given VF
3619 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
3621 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3622 struct ice_pf *pf = vf->pf;
3623 struct ice_vsi *vsi;
3625 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3626 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3630 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
3631 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3635 vsi = pf->vsi[vf->lan_vsi_idx];
3637 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3641 if (ice_vsi_manage_vlan_stripping(vsi, false))
3642 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3645 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
3650 * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
3651 * @vf: VF to enable/disable VLAN stripping for on initialization
3653 * If the VIRTCHNL_VF_OFFLOAD_VLAN flag is set enable VLAN stripping, else if
3654 * the flag is cleared then we want to disable stripping. For example, the flag
3655 * will be cleared when port VLANs are configured by the administrator before
3656 * passing the VF to the guest or if the AVF driver doesn't support VLAN
3659 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
3661 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
3666 /* don't modify stripping if port VLAN is configured */
3670 if (ice_vf_vlan_offload_ena(vf->driver_caps))
3671 return ice_vsi_manage_vlan_stripping(vsi, true);
3673 return ice_vsi_manage_vlan_stripping(vsi, false);
3677 * ice_vc_process_vf_msg - Process request from VF
3678 * @pf: pointer to the PF structure
3679 * @event: pointer to the AQ event
3681 * called from the common asq/arq handler to
3682 * process request from VF
3684 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
3686 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
3687 s16 vf_id = le16_to_cpu(event->desc.retval);
3688 u16 msglen = event->msg_len;
3689 u8 *msg = event->msg_buf;
3690 struct ice_vf *vf = NULL;
3694 dev = ice_pf_to_dev(pf);
3695 if (ice_validate_vf_id(pf, vf_id)) {
3700 vf = &pf->vf[vf_id];
3702 /* Check if VF is disabled. */
3703 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
3708 /* Perform basic checks on the msg */
3709 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
3711 if (err == VIRTCHNL_STATUS_ERR_PARAM)
3719 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
3721 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
3722 vf_id, v_opcode, msglen, err);
3726 /* VF is being configured in another context that triggers a VFR, so no
3727 * need to process this message
3729 if (!mutex_trylock(&vf->cfg_lock)) {
3730 dev_info(dev, "VF %u is being configured in another context that will trigger a VFR, so there is no need to handle this message\n",
3736 case VIRTCHNL_OP_VERSION:
3737 err = ice_vc_get_ver_msg(vf, msg);
3739 case VIRTCHNL_OP_GET_VF_RESOURCES:
3740 err = ice_vc_get_vf_res_msg(vf, msg);
3741 if (ice_vf_init_vlan_stripping(vf))
3742 dev_err(dev, "Failed to initialize VLAN stripping for VF %d\n",
3744 ice_vc_notify_vf_link_state(vf);
3746 case VIRTCHNL_OP_RESET_VF:
3747 ice_vc_reset_vf_msg(vf);
3749 case VIRTCHNL_OP_ADD_ETH_ADDR:
3750 err = ice_vc_add_mac_addr_msg(vf, msg);
3752 case VIRTCHNL_OP_DEL_ETH_ADDR:
3753 err = ice_vc_del_mac_addr_msg(vf, msg);
3755 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3756 err = ice_vc_cfg_qs_msg(vf, msg);
3758 case VIRTCHNL_OP_ENABLE_QUEUES:
3759 err = ice_vc_ena_qs_msg(vf, msg);
3760 ice_vc_notify_vf_link_state(vf);
3762 case VIRTCHNL_OP_DISABLE_QUEUES:
3763 err = ice_vc_dis_qs_msg(vf, msg);
3765 case VIRTCHNL_OP_REQUEST_QUEUES:
3766 err = ice_vc_request_qs_msg(vf, msg);
3768 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3769 err = ice_vc_cfg_irq_map_msg(vf, msg);
3771 case VIRTCHNL_OP_CONFIG_RSS_KEY:
3772 err = ice_vc_config_rss_key(vf, msg);
3774 case VIRTCHNL_OP_CONFIG_RSS_LUT:
3775 err = ice_vc_config_rss_lut(vf, msg);
3777 case VIRTCHNL_OP_GET_STATS:
3778 err = ice_vc_get_stats_msg(vf, msg);
3780 case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
3781 err = ice_vc_cfg_promiscuous_mode_msg(vf, msg);
3783 case VIRTCHNL_OP_ADD_VLAN:
3784 err = ice_vc_add_vlan_msg(vf, msg);
3786 case VIRTCHNL_OP_DEL_VLAN:
3787 err = ice_vc_remove_vlan_msg(vf, msg);
3789 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
3790 err = ice_vc_ena_vlan_stripping(vf);
3792 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
3793 err = ice_vc_dis_vlan_stripping(vf);
3795 case VIRTCHNL_OP_UNKNOWN:
3797 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
3799 err = ice_vc_send_msg_to_vf(vf, v_opcode,
3800 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
3805 /* Helper function cares less about error return values here
3806 * as it is busy with pending work.
3808 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
3809 vf_id, v_opcode, err);
3812 mutex_unlock(&vf->cfg_lock);
3817 * @netdev: network interface device structure
3818 * @vf_id: VF identifier
3819 * @ivi: VF configuration structure
3821 * return VF configuration
3824 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
3826 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3829 if (ice_validate_vf_id(pf, vf_id))
3832 vf = &pf->vf[vf_id];
3834 if (ice_check_vf_init(pf, vf))
3838 ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
3840 /* VF configuration for VLAN and applicable QoS */
3841 ivi->vlan = vf->port_vlan_info & VLAN_VID_MASK;
3842 ivi->qos = (vf->port_vlan_info & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
3844 ivi->trusted = vf->trusted;
3845 ivi->spoofchk = vf->spoofchk;
3846 if (!vf->link_forced)
3847 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
3848 else if (vf->link_up)
3849 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
3851 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
3852 ivi->max_tx_rate = vf->tx_rate;
3853 ivi->min_tx_rate = 0;
3858 * ice_unicast_mac_exists - check if the unicast MAC exists on the PF's switch
3859 * @pf: PF used to reference the switch's rules
3860 * @umac: unicast MAC to compare against existing switch rules
3862 * Return true on the first/any match, else return false
3864 static bool ice_unicast_mac_exists(struct ice_pf *pf, u8 *umac)
3866 struct ice_sw_recipe *mac_recipe_list =
3867 &pf->hw.switch_info->recp_list[ICE_SW_LKUP_MAC];
3868 struct ice_fltr_mgmt_list_entry *list_itr;
3869 struct list_head *rule_head;
3870 struct mutex *rule_lock; /* protect MAC filter list access */
3872 rule_head = &mac_recipe_list->filt_rules;
3873 rule_lock = &mac_recipe_list->filt_rule_lock;
3875 mutex_lock(rule_lock);
3876 list_for_each_entry(list_itr, rule_head, list_entry) {
3877 u8 *existing_mac = &list_itr->fltr_info.l_data.mac.mac_addr[0];
3879 if (ether_addr_equal(existing_mac, umac)) {
3880 mutex_unlock(rule_lock);
3885 mutex_unlock(rule_lock);
3892 * @netdev: network interface device structure
3893 * @vf_id: VF identifier
3896 * program VF MAC address
3898 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
3900 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3904 if (ice_validate_vf_id(pf, vf_id))
3907 if (is_multicast_ether_addr(mac)) {
3908 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
3912 vf = &pf->vf[vf_id];
3913 /* nothing left to do, unicast MAC already set */
3914 if (ether_addr_equal(vf->dflt_lan_addr.addr, mac))
3917 ret = ice_check_vf_ready_for_cfg(vf);
3921 if (ice_unicast_mac_exists(pf, mac)) {
3922 netdev_err(netdev, "Unicast MAC %pM already exists on this PF. Preventing setting VF %u unicast MAC address to %pM\n",
3927 mutex_lock(&vf->cfg_lock);
3929 /* VF is notified of its new MAC via the PF's response to the
3930 * VIRTCHNL_OP_GET_VF_RESOURCES message after the VF has been reset
3932 ether_addr_copy(vf->dflt_lan_addr.addr, mac);
3933 if (is_zero_ether_addr(mac)) {
3934 /* VF will send VIRTCHNL_OP_ADD_ETH_ADDR message with its MAC */
3935 vf->pf_set_mac = false;
3936 netdev_info(netdev, "Removing MAC on VF %d. VF driver will be reinitialized\n",
3939 /* PF will add MAC rule for the VF */
3940 vf->pf_set_mac = true;
3941 netdev_info(netdev, "Setting MAC %pM on VF %d. VF driver will be reinitialized\n",
3945 ice_vc_reset_vf(vf);
3946 mutex_unlock(&vf->cfg_lock);
3952 * @netdev: network interface device structure
3953 * @vf_id: VF identifier
3954 * @trusted: Boolean value to enable/disable trusted VF
3956 * Enable or disable a given VF as trusted
3958 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
3960 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3964 if (ice_validate_vf_id(pf, vf_id))
3967 vf = &pf->vf[vf_id];
3968 ret = ice_check_vf_ready_for_cfg(vf);
3972 /* Check if already trusted */
3973 if (trusted == vf->trusted)
3976 mutex_lock(&vf->cfg_lock);
3978 vf->trusted = trusted;
3979 ice_vc_reset_vf(vf);
3980 dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
3981 vf_id, trusted ? "" : "un");
3983 mutex_unlock(&vf->cfg_lock);
3989 * ice_set_vf_link_state
3990 * @netdev: network interface device structure
3991 * @vf_id: VF identifier
3992 * @link_state: required link state
3994 * Set VF's link state, irrespective of physical link state status
3996 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
3998 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4002 if (ice_validate_vf_id(pf, vf_id))
4005 vf = &pf->vf[vf_id];
4006 ret = ice_check_vf_ready_for_cfg(vf);
4010 switch (link_state) {
4011 case IFLA_VF_LINK_STATE_AUTO:
4012 vf->link_forced = false;
4014 case IFLA_VF_LINK_STATE_ENABLE:
4015 vf->link_forced = true;
4018 case IFLA_VF_LINK_STATE_DISABLE:
4019 vf->link_forced = true;
4020 vf->link_up = false;
4026 ice_vc_notify_vf_link_state(vf);
4032 * ice_get_vf_stats - populate some stats for the VF
4033 * @netdev: the netdev of the PF
4034 * @vf_id: the host OS identifier (0-255)
4035 * @vf_stats: pointer to the OS memory to be initialized
4037 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
4038 struct ifla_vf_stats *vf_stats)
4040 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4041 struct ice_eth_stats *stats;
4042 struct ice_vsi *vsi;
4046 if (ice_validate_vf_id(pf, vf_id))
4049 vf = &pf->vf[vf_id];
4050 ret = ice_check_vf_ready_for_cfg(vf);
4054 vsi = pf->vsi[vf->lan_vsi_idx];
4058 ice_update_eth_stats(vsi);
4059 stats = &vsi->eth_stats;
4061 memset(vf_stats, 0, sizeof(*vf_stats));
4063 vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
4064 stats->rx_multicast;
4065 vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
4066 stats->tx_multicast;
4067 vf_stats->rx_bytes = stats->rx_bytes;
4068 vf_stats->tx_bytes = stats->tx_bytes;
4069 vf_stats->broadcast = stats->rx_broadcast;
4070 vf_stats->multicast = stats->rx_multicast;
4071 vf_stats->rx_dropped = stats->rx_discards;
4072 vf_stats->tx_dropped = stats->tx_discards;
4078 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
4079 * @vf: pointer to the VF structure
4081 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
4083 struct ice_pf *pf = vf->pf;
4086 dev = ice_pf_to_dev(pf);
4088 dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
4089 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
4090 vf->dflt_lan_addr.addr,
4091 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
4096 * ice_print_vfs_mdd_event - print VFs malicious driver detect event
4097 * @pf: pointer to the PF structure
4099 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
4101 void ice_print_vfs_mdd_events(struct ice_pf *pf)
4103 struct device *dev = ice_pf_to_dev(pf);
4104 struct ice_hw *hw = &pf->hw;
4107 /* check that there are pending MDD events to print */
4108 if (!test_and_clear_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state))
4111 /* VF MDD event logs are rate limited to one second intervals */
4112 if (time_is_after_jiffies(pf->last_printed_mdd_jiffies + HZ * 1))
4115 pf->last_printed_mdd_jiffies = jiffies;
4117 ice_for_each_vf(pf, i) {
4118 struct ice_vf *vf = &pf->vf[i];
4120 /* only print Rx MDD event message if there are new events */
4121 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
4122 vf->mdd_rx_events.last_printed =
4123 vf->mdd_rx_events.count;
4124 ice_print_vf_rx_mdd_event(vf);
4127 /* only print Tx MDD event message if there are new events */
4128 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
4129 vf->mdd_tx_events.last_printed =
4130 vf->mdd_tx_events.count;
4132 dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
4133 vf->mdd_tx_events.count, hw->pf_id, i,
4134 vf->dflt_lan_addr.addr);
4140 * ice_restore_all_vfs_msi_state - restore VF MSI state after PF FLR
4141 * @pdev: pointer to a pci_dev structure
4143 * Called when recovering from a PF FLR to restore interrupt capability to
4146 void ice_restore_all_vfs_msi_state(struct pci_dev *pdev)
4148 struct pci_dev *vfdev;
4152 if (!pci_num_vf(pdev))
4155 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4157 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID,
4159 vfdev = pci_get_device(pdev->vendor, vf_id, NULL);
4161 if (vfdev->is_virtfn && vfdev->physfn == pdev)
4162 pci_restore_msi_state(vfdev);
4163 vfdev = pci_get_device(pdev->vendor, vf_id,