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_free_vf_res - Free a VF's resources
186 * @vf: pointer to the VF info
188 static void ice_free_vf_res(struct ice_vf *vf)
190 struct ice_pf *pf = vf->pf;
191 int i, last_vector_idx;
193 /* First, disable VF's configuration API to prevent OS from
194 * accessing the VF's VSI after it's freed or invalidated.
196 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
198 /* free VSI and disconnect it from the parent uplink */
199 if (vf->lan_vsi_idx) {
200 ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
206 last_vector_idx = vf->first_vector_idx + pf->num_msix_per_vf - 1;
208 /* clear VF MDD event information */
209 memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
210 memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
212 /* Disable interrupts so that VF starts in a known state */
213 for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
214 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
217 /* reset some of the state variables keeping track of the resources */
218 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
219 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
223 * ice_dis_vf_mappings
224 * @vf: pointer to the VF structure
226 static void ice_dis_vf_mappings(struct ice_vf *vf)
228 struct ice_pf *pf = vf->pf;
235 vsi = pf->vsi[vf->lan_vsi_idx];
237 dev = ice_pf_to_dev(pf);
238 wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
239 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
241 first = vf->first_vector_idx;
242 last = first + pf->num_msix_per_vf - 1;
243 for (v = first; v <= last; v++) {
246 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
247 GLINT_VECT2FUNC_IS_PF_M) |
248 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
249 GLINT_VECT2FUNC_PF_NUM_M));
250 wr32(hw, GLINT_VECT2FUNC(v), reg);
253 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
254 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
256 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
258 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
259 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
261 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
265 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
266 * @pf: pointer to the PF structure
268 * Since no MSIX entries are taken from the pf->irq_tracker then just clear
269 * the pf->sriov_base_vector.
271 * Returns 0 on success, and -EINVAL on error.
273 static int ice_sriov_free_msix_res(struct ice_pf *pf)
275 struct ice_res_tracker *res;
280 res = pf->irq_tracker;
284 /* give back irq_tracker resources used */
285 WARN_ON(pf->sriov_base_vector < res->num_entries);
287 pf->sriov_base_vector = 0;
293 * ice_set_vf_state_qs_dis - Set VF queues state to disabled
294 * @vf: pointer to the VF structure
296 void ice_set_vf_state_qs_dis(struct ice_vf *vf)
298 /* Clear Rx/Tx enabled queues flag */
299 bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
300 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
301 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
305 * ice_dis_vf_qs - Disable the VF queues
306 * @vf: pointer to the VF structure
308 static void ice_dis_vf_qs(struct ice_vf *vf)
310 struct ice_pf *pf = vf->pf;
313 vsi = pf->vsi[vf->lan_vsi_idx];
315 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
316 ice_vsi_stop_all_rx_rings(vsi);
317 ice_set_vf_state_qs_dis(vf);
321 * ice_free_vfs - Free all VFs
322 * @pf: pointer to the PF structure
324 void ice_free_vfs(struct ice_pf *pf)
326 struct device *dev = ice_pf_to_dev(pf);
327 struct ice_hw *hw = &pf->hw;
333 while (test_and_set_bit(__ICE_VF_DIS, pf->state))
334 usleep_range(1000, 2000);
336 /* Disable IOV before freeing resources. This lets any VF drivers
337 * running in the host get themselves cleaned up before we yank
338 * the carpet out from underneath their feet.
340 if (!pci_vfs_assigned(pf->pdev))
341 pci_disable_sriov(pf->pdev);
343 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
345 /* Avoid wait time by stopping all VFs at the same time */
346 ice_for_each_vf(pf, i)
347 if (test_bit(ICE_VF_STATE_QS_ENA, pf->vf[i].vf_states))
348 ice_dis_vf_qs(&pf->vf[i]);
350 tmp = pf->num_alloc_vfs;
351 pf->num_qps_per_vf = 0;
352 pf->num_alloc_vfs = 0;
353 for (i = 0; i < tmp; i++) {
354 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
355 /* disable VF qp mappings and set VF disable state */
356 ice_dis_vf_mappings(&pf->vf[i]);
357 set_bit(ICE_VF_STATE_DIS, pf->vf[i].vf_states);
358 ice_free_vf_res(&pf->vf[i]);
362 if (ice_sriov_free_msix_res(pf))
363 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
365 devm_kfree(dev, pf->vf);
368 /* This check is for when the driver is unloaded while VFs are
369 * assigned. Setting the number of VFs to 0 through sysfs is caught
370 * before this function ever gets called.
372 if (!pci_vfs_assigned(pf->pdev)) {
375 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
376 * work correctly when SR-IOV gets re-enabled.
378 for (vf_id = 0; vf_id < tmp; vf_id++) {
379 u32 reg_idx, bit_idx;
381 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
382 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
383 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
386 clear_bit(__ICE_VF_DIS, pf->state);
387 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
391 * ice_trigger_vf_reset - Reset a VF on HW
392 * @vf: pointer to the VF structure
393 * @is_vflr: true if VFLR was issued, false if not
394 * @is_pfr: true if the reset was triggered due to a previous PFR
396 * Trigger hardware to start a reset for a particular VF. Expects the caller
397 * to wait the proper amount of time to allow hardware to reset the VF before
398 * it cleans up and restores VF functionality.
400 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
402 struct ice_pf *pf = vf->pf;
403 u32 reg, reg_idx, bit_idx;
404 unsigned int vf_abs_id, i;
408 dev = ice_pf_to_dev(pf);
410 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
412 /* Inform VF that it is no longer active, as a warning */
413 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
415 /* Disable VF's configuration API during reset. The flag is re-enabled
416 * in ice_alloc_vf_res(), when it's safe again to access VF's VSI.
417 * It's normally disabled in ice_free_vf_res(), but it's safer
418 * to do it earlier to give some time to finish to any VF config
419 * functions that may still be running at this point.
421 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
423 /* VF_MBX_ARQLEN is cleared by PFR, so the driver needs to clear it
424 * in the case of VFR. If this is done for PFR, it can mess up VF
425 * resets because the VF driver may already have started cleanup
426 * by the time we get here.
429 wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
431 /* In the case of a VFLR, the HW has already reset the VF and we
432 * just need to clean up, so don't hit the VFRTRIG register.
435 /* reset VF using VPGEN_VFRTRIG reg */
436 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
437 reg |= VPGEN_VFRTRIG_VFSWR_M;
438 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
440 /* clear the VFLR bit in GLGEN_VFLRSTAT */
441 reg_idx = (vf_abs_id) / 32;
442 bit_idx = (vf_abs_id) % 32;
443 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
446 wr32(hw, PF_PCI_CIAA,
447 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
448 for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
449 reg = rd32(hw, PF_PCI_CIAD);
450 /* no transactions pending so stop polling */
451 if ((reg & VF_TRANS_PENDING_M) == 0)
454 dev_err(dev, "VF %u PCI transactions stuck\n", vf->vf_id);
455 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
460 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
461 * @vsi: the VSI to update
462 * @pvid_info: VLAN ID and QoS used to set the PVID VSI context field
463 * @enable: true for enable PVID false for disable
465 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 pvid_info, bool enable)
467 struct ice_hw *hw = &vsi->back->hw;
468 struct ice_aqc_vsi_props *info;
469 struct ice_vsi_ctx *ctxt;
470 enum ice_status status;
473 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
477 ctxt->info = vsi->info;
480 info->vlan_flags = ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
481 ICE_AQ_VSI_PVLAN_INSERT_PVID |
482 ICE_AQ_VSI_VLAN_EMOD_STR;
483 info->sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
485 info->vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING |
486 ICE_AQ_VSI_VLAN_MODE_ALL;
487 info->sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
490 info->pvid = cpu_to_le16(pvid_info);
491 info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
492 ICE_AQ_VSI_PROP_SW_VALID);
494 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
496 dev_info(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %s aq_err %s\n",
497 ice_stat_str(status),
498 ice_aq_str(hw->adminq.sq_last_status));
503 vsi->info.vlan_flags = info->vlan_flags;
504 vsi->info.sw_flags2 = info->sw_flags2;
505 vsi->info.pvid = info->pvid;
512 * ice_vf_vsi_setup - Set up a VF VSI
513 * @pf: board private structure
514 * @pi: pointer to the port_info instance
515 * @vf_id: defines VF ID to which this VSI connects.
517 * Returns pointer to the successfully allocated VSI struct on success,
518 * otherwise returns NULL on failure.
520 static struct ice_vsi *
521 ice_vf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi, u16 vf_id)
523 return ice_vsi_setup(pf, pi, ICE_VSI_VF, vf_id);
527 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
528 * @pf: pointer to PF structure
529 * @vf: pointer to VF that the first MSIX vector index is being calculated for
531 * This returns the first MSIX vector index in PF space that is used by this VF.
532 * This index is used when accessing PF relative registers such as
533 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
534 * This will always be the OICR index in the AVF driver so any functionality
535 * using vf->first_vector_idx for queue configuration will have to increment by
536 * 1 to avoid meddling with the OICR index.
538 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
540 return pf->sriov_base_vector + vf->vf_id * pf->num_msix_per_vf;
544 * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
545 * @vf: VF to add MAC filters for
547 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
548 * always re-adds either a VLAN 0 or port VLAN based filter after reset.
550 static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf)
552 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
553 struct device *dev = ice_pf_to_dev(vf->pf);
557 if (vf->port_vlan_info) {
558 err = ice_vsi_manage_pvid(vsi, vf->port_vlan_info, true);
560 dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
565 vlan_id = vf->port_vlan_info & VLAN_VID_MASK;
568 /* vlan_id will either be 0 or the port VLAN number */
569 err = ice_vsi_add_vlan(vsi, vlan_id, ICE_FWD_TO_VSI);
571 dev_err(dev, "failed to add %s VLAN %u filter for VF %u, error %d\n",
572 vf->port_vlan_info ? "port" : "", vlan_id, vf->vf_id,
581 * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
582 * @vf: VF to add MAC filters for
584 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
585 * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
587 static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
589 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
590 struct device *dev = ice_pf_to_dev(vf->pf);
591 enum ice_status status;
592 u8 broadcast[ETH_ALEN];
594 eth_broadcast_addr(broadcast);
595 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
597 dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %s\n",
598 vf->vf_id, ice_stat_str(status));
599 return ice_status_to_errno(status);
604 if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
605 status = ice_fltr_add_mac(vsi, vf->dflt_lan_addr.addr,
608 dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %s\n",
609 &vf->dflt_lan_addr.addr[0], vf->vf_id,
610 ice_stat_str(status));
611 return ice_status_to_errno(status);
620 * ice_alloc_vsi_res - Setup VF VSI and its resources
621 * @vf: pointer to the VF structure
623 * Returns 0 on success, negative value on failure
625 static int ice_alloc_vsi_res(struct ice_vf *vf)
627 struct ice_pf *pf = vf->pf;
632 dev = ice_pf_to_dev(pf);
633 /* first vector index is the VFs OICR index */
634 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
636 vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
638 dev_err(dev, "Failed to create VF VSI\n");
642 vf->lan_vsi_idx = vsi->idx;
643 vf->lan_vsi_num = vsi->vsi_num;
645 ret = ice_vf_rebuild_host_vlan_cfg(vf);
647 dev_err(dev, "failed to rebuild default MAC configuration for VF %d, error %d\n",
649 goto ice_alloc_vsi_res_exit;
653 ret = ice_vf_rebuild_host_mac_cfg(vf);
655 dev_err(dev, "failed to rebuild default MAC configuration for VF %d, error %d\n",
657 goto ice_alloc_vsi_res_exit;
660 /* Clear this bit after VF initialization since we shouldn't reclaim
661 * and reassign interrupts for synchronous or asynchronous VFR events.
662 * We don't want to reconfigure interrupts since AVF driver doesn't
663 * expect vector assignment to be changed unless there is a request for
666 ice_alloc_vsi_res_exit:
671 * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
672 * @vf: VF to configure trust setting for
674 static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
677 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
679 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
683 * ice_alloc_vf_res - Allocate VF resources
684 * @vf: pointer to the VF structure
686 static int ice_alloc_vf_res(struct ice_vf *vf)
688 struct ice_pf *pf = vf->pf;
689 int tx_rx_queue_left;
692 /* Update number of VF queues, in case VF had requested for queue
695 tx_rx_queue_left = min_t(int, ice_get_avail_txq_count(pf),
696 ice_get_avail_rxq_count(pf));
697 tx_rx_queue_left += pf->num_qps_per_vf;
698 if (vf->num_req_qs && vf->num_req_qs <= tx_rx_queue_left &&
699 vf->num_req_qs != vf->num_vf_qs)
700 vf->num_vf_qs = vf->num_req_qs;
702 /* setup VF VSI and necessary resources */
703 status = ice_alloc_vsi_res(vf);
705 goto ice_alloc_vf_res_exit;
707 ice_vf_set_host_trust_cfg(vf);
709 /* VF is now completely initialized */
710 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
714 ice_alloc_vf_res_exit:
720 * ice_ena_vf_msix_mappings - enable VF MSIX mappings in hardware
721 * @vf: VF to enable MSIX mappings for
723 * Some of the registers need to be indexed/configured using hardware global
724 * device values and other registers need 0-based values, which represent PF
727 static void ice_ena_vf_msix_mappings(struct ice_vf *vf)
729 int device_based_first_msix, device_based_last_msix;
730 int pf_based_first_msix, pf_based_last_msix, v;
731 struct ice_pf *pf = vf->pf;
732 int device_based_vf_id;
737 pf_based_first_msix = vf->first_vector_idx;
738 pf_based_last_msix = (pf_based_first_msix + pf->num_msix_per_vf) - 1;
740 device_based_first_msix = pf_based_first_msix +
741 pf->hw.func_caps.common_cap.msix_vector_first_id;
742 device_based_last_msix =
743 (device_based_first_msix + pf->num_msix_per_vf) - 1;
744 device_based_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
746 reg = (((device_based_first_msix << VPINT_ALLOC_FIRST_S) &
747 VPINT_ALLOC_FIRST_M) |
748 ((device_based_last_msix << VPINT_ALLOC_LAST_S) &
749 VPINT_ALLOC_LAST_M) | VPINT_ALLOC_VALID_M);
750 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
752 reg = (((device_based_first_msix << VPINT_ALLOC_PCI_FIRST_S)
753 & VPINT_ALLOC_PCI_FIRST_M) |
754 ((device_based_last_msix << VPINT_ALLOC_PCI_LAST_S) &
755 VPINT_ALLOC_PCI_LAST_M) | VPINT_ALLOC_PCI_VALID_M);
756 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
758 /* map the interrupts to its functions */
759 for (v = pf_based_first_msix; v <= pf_based_last_msix; v++) {
760 reg = (((device_based_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
761 GLINT_VECT2FUNC_VF_NUM_M) |
762 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
763 GLINT_VECT2FUNC_PF_NUM_M));
764 wr32(hw, GLINT_VECT2FUNC(v), reg);
767 /* Map mailbox interrupt to VF MSI-X vector 0 */
768 wr32(hw, VPINT_MBX_CTL(device_based_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M);
772 * ice_ena_vf_q_mappings - enable Rx/Tx queue mappings for a VF
773 * @vf: VF to enable the mappings for
774 * @max_txq: max Tx queues allowed on the VF's VSI
775 * @max_rxq: max Rx queues allowed on the VF's VSI
777 static void ice_ena_vf_q_mappings(struct ice_vf *vf, u16 max_txq, u16 max_rxq)
779 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
780 struct device *dev = ice_pf_to_dev(vf->pf);
781 struct ice_hw *hw = &vf->pf->hw;
784 /* set regardless of mapping mode */
785 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
787 /* VF Tx queues allocation */
788 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
789 /* set the VF PF Tx queue range
790 * VFNUMQ value should be set to (number of queues - 1). A value
791 * of 0 means 1 queue and a value of 255 means 256 queues
793 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
794 VPLAN_TX_QBASE_VFFIRSTQ_M) |
795 (((max_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
796 VPLAN_TX_QBASE_VFNUMQ_M));
797 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
799 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
802 /* set regardless of mapping mode */
803 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
805 /* VF Rx queues allocation */
806 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
807 /* set the VF PF Rx queue range
808 * VFNUMQ value should be set to (number of queues - 1). A value
809 * of 0 means 1 queue and a value of 255 means 256 queues
811 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
812 VPLAN_RX_QBASE_VFFIRSTQ_M) |
813 (((max_rxq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
814 VPLAN_RX_QBASE_VFNUMQ_M));
815 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
817 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
822 * ice_ena_vf_mappings - enable VF MSIX and queue mapping
823 * @vf: pointer to the VF structure
825 static void ice_ena_vf_mappings(struct ice_vf *vf)
827 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
829 ice_ena_vf_msix_mappings(vf);
830 ice_ena_vf_q_mappings(vf, vsi->alloc_txq, vsi->alloc_rxq);
835 * @pf: pointer to the PF structure
836 * @avail_res: available resources in the PF structure
837 * @max_res: maximum resources that can be given per VF
838 * @min_res: minimum resources that can be given per VF
840 * Returns non-zero value if resources (queues/vectors) are available or
841 * returns zero if PF cannot accommodate for all num_alloc_vfs.
844 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
846 bool checked_min_res = false;
849 /* start by checking if PF can assign max number of resources for
851 * if yes, return number per VF
852 * If no, divide by 2 and roundup, check again
853 * repeat the loop till we reach a point where even minimum resources
854 * are not available, in that case return 0
857 while ((res >= min_res) && !checked_min_res) {
860 num_all_res = pf->num_alloc_vfs * res;
861 if (num_all_res <= avail_res)
865 checked_min_res = true;
867 res = DIV_ROUND_UP(res, 2);
873 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
874 * @vf: VF to calculate the register index for
875 * @q_vector: a q_vector associated to the VF
877 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
881 if (!vf || !q_vector)
886 /* always add one to account for the OICR being the first MSIX */
887 return pf->sriov_base_vector + pf->num_msix_per_vf * vf->vf_id +
892 * ice_get_max_valid_res_idx - Get the max valid resource index
893 * @res: pointer to the resource to find the max valid index for
895 * Start from the end of the ice_res_tracker and return right when we find the
896 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
897 * valid for SR-IOV because it is the only consumer that manipulates the
898 * res->end and this is always called when res->end is set to res->num_entries.
900 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
907 for (i = res->num_entries - 1; i >= 0; i--)
908 if (res->list[i] & ICE_RES_VALID_BIT)
915 * ice_sriov_set_msix_res - Set any used MSIX resources
916 * @pf: pointer to PF structure
917 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
919 * This function allows SR-IOV resources to be taken from the end of the PF's
920 * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
921 * just set the pf->sriov_base_vector and return success.
923 * If there are not enough resources available, return an error. This should
924 * always be caught by ice_set_per_vf_res().
926 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors in
927 * in the PF's space available for SR-IOV.
929 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
931 u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
932 int vectors_used = pf->irq_tracker->num_entries;
933 int sriov_base_vector;
935 sriov_base_vector = total_vectors - num_msix_needed;
937 /* make sure we only grab irq_tracker entries from the list end and
938 * that we have enough available MSIX vectors
940 if (sriov_base_vector < vectors_used)
943 pf->sriov_base_vector = sriov_base_vector;
949 * ice_set_per_vf_res - check if vectors and queues are available
950 * @pf: pointer to the PF structure
952 * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
953 * get more vectors and can enable more queues per VF. Note that this does not
954 * grab any vectors from the SW pool already allocated. Also note, that all
955 * vector counts include one for each VF's miscellaneous interrupt vector
958 * Minimum VFs - 2 vectors, 1 queue pair
959 * Small VFs - 5 vectors, 4 queue pairs
960 * Medium VFs - 17 vectors, 16 queue pairs
962 * Second, determine number of queue pairs per VF by starting with a pre-defined
963 * maximum each VF supports. If this is not possible, then we adjust based on
964 * queue pairs available on the device.
966 * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
967 * by each VF during VF initialization and reset.
969 static int ice_set_per_vf_res(struct ice_pf *pf)
971 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
972 int msix_avail_per_vf, msix_avail_for_sriov;
973 struct device *dev = ice_pf_to_dev(pf);
974 u16 num_msix_per_vf, num_txq, num_rxq;
976 if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
979 /* determine MSI-X resources per VF */
980 msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
981 pf->irq_tracker->num_entries;
982 msix_avail_per_vf = msix_avail_for_sriov / pf->num_alloc_vfs;
983 if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
984 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
985 } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
986 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
987 } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
988 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
990 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",
991 msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
996 /* determine queue resources per VF */
997 num_txq = ice_determine_res(pf, ice_get_avail_txq_count(pf),
999 num_msix_per_vf - ICE_NONQ_VECS_VF,
1000 ICE_MAX_RSS_QS_PER_VF),
1003 num_rxq = ice_determine_res(pf, ice_get_avail_rxq_count(pf),
1005 num_msix_per_vf - ICE_NONQ_VECS_VF,
1006 ICE_MAX_RSS_QS_PER_VF),
1009 if (!num_txq || !num_rxq) {
1010 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
1011 ICE_MIN_QS_PER_VF, pf->num_alloc_vfs);
1015 if (ice_sriov_set_msix_res(pf, num_msix_per_vf * pf->num_alloc_vfs)) {
1016 dev_err(dev, "Unable to set MSI-X resources for %d VFs\n",
1021 /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
1022 pf->num_qps_per_vf = min_t(int, num_txq, num_rxq);
1023 pf->num_msix_per_vf = num_msix_per_vf;
1024 dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
1025 pf->num_alloc_vfs, pf->num_msix_per_vf, pf->num_qps_per_vf);
1031 * ice_clear_vf_reset_trigger - enable VF to access hardware
1032 * @vf: VF to enabled hardware access for
1034 static void ice_clear_vf_reset_trigger(struct ice_vf *vf)
1036 struct ice_hw *hw = &vf->pf->hw;
1039 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
1040 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
1041 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
1046 * ice_cleanup_and_realloc_vf - Clean up VF and reallocate resources after reset
1047 * @vf: pointer to the VF structure
1049 * Cleanup a VF after the hardware reset is finished. Expects the caller to
1050 * have verified whether the reset is finished properly, and ensure the
1051 * minimum amount of wait time has passed. Reallocate VF resources back to make
1054 static void ice_cleanup_and_realloc_vf(struct ice_vf *vf)
1056 struct ice_pf *pf = vf->pf;
1061 /* Allow HW to access VF memory after calling
1062 * ice_clear_vf_reset_trigger(). If we did it any sooner, HW could
1063 * access memory while it was being freed in ice_free_vf_res(), causing
1066 * On the other hand, this needs to be done ASAP, because the VF driver
1067 * is waiting for this to happen and may report a timeout. It's
1068 * harmless, but it gets logged into Guest OS kernel log, so best avoid
1071 ice_clear_vf_reset_trigger(vf);
1073 /* reallocate VF resources to finish resetting the VSI state */
1074 if (!ice_alloc_vf_res(vf)) {
1075 ice_ena_vf_mappings(vf);
1076 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
1077 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
1080 /* Tell the VF driver the reset is done. This needs to be done only
1081 * after VF has been fully initialized, because the VF driver may
1082 * request resources immediately after setting this flag.
1084 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1088 * ice_vf_set_vsi_promisc - set given VF VSI to given promiscuous mode(s)
1089 * @vf: pointer to the VF info
1090 * @vsi: the VSI being configured
1091 * @promisc_m: mask of promiscuous config bits
1092 * @rm_promisc: promisc flag request from the VF to remove or add filter
1094 * This function configures VF VSI promiscuous mode, based on the VF requests,
1095 * for Unicast, Multicast and VLAN
1097 static enum ice_status
1098 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m,
1101 struct ice_pf *pf = vf->pf;
1102 enum ice_status status = 0;
1106 if (vsi->num_vlan) {
1107 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
1109 } else if (vf->port_vlan_info) {
1111 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1112 vf->port_vlan_info);
1114 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1115 vf->port_vlan_info);
1118 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1121 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1129 * ice_config_res_vfs - Finalize allocation of VFs resources in one go
1130 * @pf: pointer to the PF structure
1132 * This function is being called as last part of resetting all VFs, or when
1133 * configuring VFs for the first time, where there is no resource to be freed
1134 * Returns true if resources were properly allocated for all VFs, and false
1137 static bool ice_config_res_vfs(struct ice_pf *pf)
1139 struct device *dev = ice_pf_to_dev(pf);
1140 struct ice_hw *hw = &pf->hw;
1143 if (ice_set_per_vf_res(pf)) {
1144 dev_err(dev, "Cannot allocate VF resources, try with fewer number of VFs\n");
1148 /* rearm global interrupts */
1149 if (test_and_clear_bit(__ICE_OICR_INTR_DIS, pf->state))
1150 ice_irq_dynamic_ena(hw, NULL, NULL);
1152 /* Finish resetting each VF and allocate resources */
1153 ice_for_each_vf(pf, v) {
1154 struct ice_vf *vf = &pf->vf[v];
1156 vf->num_vf_qs = pf->num_qps_per_vf;
1157 dev_dbg(dev, "VF-id %d has %d queues configured\n", vf->vf_id,
1159 ice_cleanup_and_realloc_vf(vf);
1163 clear_bit(__ICE_VF_DIS, pf->state);
1169 * ice_reset_all_vfs - reset all allocated VFs in one go
1170 * @pf: pointer to the PF structure
1171 * @is_vflr: true if VFLR was issued, false if not
1173 * First, tell the hardware to reset each VF, then do all the waiting in one
1174 * chunk, and finally finish restoring each VF after the wait. This is useful
1175 * during PF routines which need to reset all VFs, as otherwise it must perform
1176 * these resets in a serialized fashion.
1178 * Returns true if any VFs were reset, and false otherwise.
1180 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1182 struct device *dev = ice_pf_to_dev(pf);
1183 struct ice_hw *hw = &pf->hw;
1187 /* If we don't have any VFs, then there is nothing to reset */
1188 if (!pf->num_alloc_vfs)
1191 /* If VFs have been disabled, there is no need to reset */
1192 if (test_and_set_bit(__ICE_VF_DIS, pf->state))
1195 /* Begin reset on all VFs at once */
1196 ice_for_each_vf(pf, v)
1197 ice_trigger_vf_reset(&pf->vf[v], is_vflr, true);
1199 ice_for_each_vf(pf, v) {
1200 struct ice_vsi *vsi;
1203 vsi = pf->vsi[vf->lan_vsi_idx];
1204 if (test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states))
1206 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1207 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1210 /* HW requires some time to make sure it can flush the FIFO for a VF
1211 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1212 * sequence to make sure that it has completed. We'll keep track of
1213 * the VFs using a simple iterator that increments once that VF has
1214 * finished resetting.
1216 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1217 /* Check each VF in sequence */
1218 while (v < pf->num_alloc_vfs) {
1222 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1223 if (!(reg & VPGEN_VFRSTAT_VFRD_M)) {
1224 /* only delay if the check failed */
1225 usleep_range(10, 20);
1229 /* If the current VF has finished resetting, move on
1230 * to the next VF in sequence.
1236 /* Display a warning if at least one VF didn't manage to reset in
1237 * time, but continue on with the operation.
1239 if (v < pf->num_alloc_vfs)
1240 dev_warn(dev, "VF reset check timeout\n");
1242 /* free VF resources to begin resetting the VSI state */
1243 ice_for_each_vf(pf, v) {
1246 ice_free_vf_res(vf);
1248 /* Free VF queues as well, and reallocate later.
1249 * If a given VF has different number of queues
1250 * configured, the request for update will come
1251 * via mailbox communication.
1256 if (ice_sriov_free_msix_res(pf))
1257 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
1259 if (!ice_config_res_vfs(pf))
1266 * ice_is_vf_disabled
1267 * @vf: pointer to the VF info
1269 * Returns true if the PF or VF is disabled, false otherwise.
1271 static bool ice_is_vf_disabled(struct ice_vf *vf)
1273 struct ice_pf *pf = vf->pf;
1275 /* If the PF has been disabled, there is no need resetting VF until
1276 * PF is active again. Similarly, if the VF has been disabled, this
1277 * means something else is resetting the VF, so we shouldn't continue.
1278 * Otherwise, set disable VF state bit for actual reset, and continue.
1280 return (test_bit(__ICE_VF_DIS, pf->state) ||
1281 test_bit(ICE_VF_STATE_DIS, vf->vf_states));
1285 * ice_reset_vf - Reset a particular VF
1286 * @vf: pointer to the VF structure
1287 * @is_vflr: true if VFLR was issued, false if not
1289 * Returns true if the VF is currently in reset, resets successfully, or resets
1290 * are disabled and false otherwise.
1292 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1294 struct ice_pf *pf = vf->pf;
1295 struct ice_vsi *vsi;
1303 dev = ice_pf_to_dev(pf);
1305 if (test_bit(__ICE_VF_RESETS_DISABLED, pf->state)) {
1306 dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
1311 if (ice_is_vf_disabled(vf)) {
1312 dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
1317 /* Set VF disable bit state here, before triggering reset */
1318 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1319 ice_trigger_vf_reset(vf, is_vflr, false);
1321 vsi = pf->vsi[vf->lan_vsi_idx];
1323 if (test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states))
1326 /* Call Disable LAN Tx queue AQ whether or not queues are
1327 * enabled. This is needed for successful completion of VFR.
1329 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1330 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1333 /* poll VPGEN_VFRSTAT reg to make sure
1334 * that reset is complete
1336 for (i = 0; i < 10; i++) {
1337 /* VF reset requires driver to first reset the VF and then
1338 * poll the status register to make sure that the reset
1339 * completed successfully.
1341 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1342 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1347 /* only sleep if the reset is not done */
1348 usleep_range(10, 20);
1351 /* Display a warning if VF didn't manage to reset in time, but need to
1352 * continue on with the operation.
1355 dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
1357 /* disable promiscuous modes in case they were enabled
1358 * ignore any error if disabling process failed
1360 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1361 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1362 if (vf->port_vlan_info || vsi->num_vlan)
1363 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1365 promisc_m = ICE_UCAST_PROMISC_BITS;
1367 vsi = pf->vsi[vf->lan_vsi_idx];
1368 if (ice_vf_set_vsi_promisc(vf, vsi, promisc_m, true))
1369 dev_err(dev, "disabling promiscuous mode failed\n");
1372 /* free VF resources to begin resetting the VSI state */
1373 ice_free_vf_res(vf);
1375 ice_cleanup_and_realloc_vf(vf);
1383 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1384 * @pf: pointer to the PF structure
1386 void ice_vc_notify_link_state(struct ice_pf *pf)
1390 ice_for_each_vf(pf, i)
1391 ice_vc_notify_vf_link_state(&pf->vf[i]);
1395 * ice_vc_notify_reset - Send pending reset message to all VFs
1396 * @pf: pointer to the PF structure
1398 * indicate a pending reset to all VFs on a given PF
1400 void ice_vc_notify_reset(struct ice_pf *pf)
1402 struct virtchnl_pf_event pfe;
1404 if (!pf->num_alloc_vfs)
1407 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1408 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1409 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1410 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1414 * ice_vc_notify_vf_reset - Notify VF of a reset event
1415 * @vf: pointer to the VF structure
1417 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1419 struct virtchnl_pf_event pfe;
1426 if (ice_validate_vf_id(pf, vf->vf_id))
1429 /* Bail out if VF is in disabled state, neither initialized, nor active
1430 * state - otherwise proceed with notifications
1432 if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1433 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
1434 test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1437 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1438 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1439 ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1440 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1445 * ice_init_vf_vsi_res - initialize/setup VF VSI resources
1446 * @vf: VF to initialize/setup the VSI for
1448 * This function creates a VSI for the VF, adds a VLAN 0 filter, and sets up the
1449 * VF VSI's broadcast filter and is only used during initial VF creation.
1451 static int ice_init_vf_vsi_res(struct ice_vf *vf)
1453 struct ice_pf *pf = vf->pf;
1454 u8 broadcast[ETH_ALEN];
1455 enum ice_status status;
1456 struct ice_vsi *vsi;
1460 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
1462 dev = ice_pf_to_dev(pf);
1463 vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
1465 dev_err(dev, "Failed to create VF VSI\n");
1469 vf->lan_vsi_idx = vsi->idx;
1470 vf->lan_vsi_num = vsi->vsi_num;
1472 err = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
1474 dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
1479 eth_broadcast_addr(broadcast);
1480 status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
1482 dev_err(dev, "Failed to add broadcast MAC filter for VF %d, status %s\n",
1483 vf->vf_id, ice_stat_str(status));
1484 err = ice_status_to_errno(status);
1493 ice_vsi_release(vsi);
1498 * ice_start_vfs - start VFs so they are ready to be used by SR-IOV
1499 * @pf: PF the VFs are associated with
1501 static int ice_start_vfs(struct ice_pf *pf)
1503 struct ice_hw *hw = &pf->hw;
1506 ice_for_each_vf(pf, i) {
1507 struct ice_vf *vf = &pf->vf[i];
1509 ice_clear_vf_reset_trigger(vf);
1511 retval = ice_init_vf_vsi_res(vf);
1513 dev_err(ice_pf_to_dev(pf), "Failed to initialize VSI resources for VF %d, error %d\n",
1518 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1519 ice_ena_vf_mappings(vf);
1520 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1527 for (i = i - 1; i >= 0; i--) {
1528 struct ice_vf *vf = &pf->vf[i];
1530 ice_dis_vf_mappings(vf);
1531 ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
1538 * ice_set_dflt_settings - set VF defaults during initialization/creation
1539 * @pf: PF holding reference to all VFs for default configuration
1541 static void ice_set_dflt_settings_vfs(struct ice_pf *pf)
1545 ice_for_each_vf(pf, i) {
1546 struct ice_vf *vf = &pf->vf[i];
1550 vf->vf_sw_id = pf->first_sw;
1551 /* assign default capabilities */
1552 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vf->vf_caps);
1553 vf->spoofchk = true;
1554 vf->num_vf_qs = pf->num_qps_per_vf;
1559 * ice_alloc_vfs - allocate num_vfs in the PF structure
1560 * @pf: PF to store the allocated VFs in
1561 * @num_vfs: number of VFs to allocate
1563 static int ice_alloc_vfs(struct ice_pf *pf, int num_vfs)
1567 vfs = devm_kcalloc(ice_pf_to_dev(pf), num_vfs, sizeof(*vfs),
1573 pf->num_alloc_vfs = num_vfs;
1579 * ice_ena_vfs - enable VFs so they are ready to be used
1580 * @pf: pointer to the PF structure
1581 * @num_vfs: number of VFs to enable
1583 static int ice_ena_vfs(struct ice_pf *pf, u16 num_vfs)
1585 struct device *dev = ice_pf_to_dev(pf);
1586 struct ice_hw *hw = &pf->hw;
1589 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1590 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1591 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1592 set_bit(__ICE_OICR_INTR_DIS, pf->state);
1595 ret = pci_enable_sriov(pf->pdev, num_vfs);
1597 pf->num_alloc_vfs = 0;
1598 goto err_unroll_intr;
1601 ret = ice_alloc_vfs(pf, num_vfs);
1603 goto err_pci_disable_sriov;
1605 if (ice_set_per_vf_res(pf)) {
1606 dev_err(dev, "Not enough resources for %d VFs, try with fewer number of VFs\n",
1609 goto err_unroll_sriov;
1612 ice_set_dflt_settings_vfs(pf);
1614 if (ice_start_vfs(pf)) {
1615 dev_err(dev, "Failed to start VF(s)\n");
1617 goto err_unroll_sriov;
1620 clear_bit(__ICE_VF_DIS, pf->state);
1624 devm_kfree(dev, pf->vf);
1626 pf->num_alloc_vfs = 0;
1627 err_pci_disable_sriov:
1628 pci_disable_sriov(pf->pdev);
1630 /* rearm interrupts here */
1631 ice_irq_dynamic_ena(hw, NULL, NULL);
1632 clear_bit(__ICE_OICR_INTR_DIS, pf->state);
1637 * ice_pf_state_is_nominal - checks the PF for nominal state
1638 * @pf: pointer to PF to check
1640 * Check the PF's state for a collection of bits that would indicate
1641 * the PF is in a state that would inhibit normal operation for
1642 * driver functionality.
1644 * Returns true if PF is in a nominal state.
1645 * Returns false otherwise
1647 static bool ice_pf_state_is_nominal(struct ice_pf *pf)
1649 DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
1654 bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
1655 if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
1662 * ice_pci_sriov_ena - Enable or change number of VFs
1663 * @pf: pointer to the PF structure
1664 * @num_vfs: number of VFs to allocate
1666 * Returns 0 on success and negative on failure
1668 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1670 int pre_existing_vfs = pci_num_vf(pf->pdev);
1671 struct device *dev = ice_pf_to_dev(pf);
1674 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1676 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1679 if (num_vfs > pf->num_vfs_supported) {
1680 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1681 num_vfs, pf->num_vfs_supported);
1685 dev_info(dev, "Enabling %d VFs\n", num_vfs);
1686 err = ice_ena_vfs(pf, num_vfs);
1688 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1692 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1697 * ice_check_sriov_allowed - check if SR-IOV is allowed based on various checks
1698 * @pf: PF to enabled SR-IOV on
1700 static int ice_check_sriov_allowed(struct ice_pf *pf)
1702 struct device *dev = ice_pf_to_dev(pf);
1704 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1705 dev_err(dev, "This device is not capable of SR-IOV\n");
1709 if (ice_is_safe_mode(pf)) {
1710 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
1714 if (!ice_pf_state_is_nominal(pf)) {
1715 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1723 * ice_sriov_configure - Enable or change number of VFs via sysfs
1724 * @pdev: pointer to a pci_dev structure
1725 * @num_vfs: number of VFs to allocate or 0 to free VFs
1727 * This function is called when the user updates the number of VFs in sysfs. On
1728 * success return whatever num_vfs was set to by the caller. Return negative on
1731 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1733 struct ice_pf *pf = pci_get_drvdata(pdev);
1734 struct device *dev = ice_pf_to_dev(pf);
1737 err = ice_check_sriov_allowed(pf);
1742 if (!pci_vfs_assigned(pdev)) {
1747 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
1751 err = ice_pci_sriov_ena(pf, num_vfs);
1759 * ice_process_vflr_event - Free VF resources via IRQ calls
1760 * @pf: pointer to the PF structure
1762 * called from the VFLR IRQ handler to
1763 * free up VF resources and state variables
1765 void ice_process_vflr_event(struct ice_pf *pf)
1767 struct ice_hw *hw = &pf->hw;
1771 if (!test_and_clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1775 ice_for_each_vf(pf, vf_id) {
1776 struct ice_vf *vf = &pf->vf[vf_id];
1777 u32 reg_idx, bit_idx;
1779 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1780 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1781 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1782 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1783 if (reg & BIT(bit_idx))
1784 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1785 ice_reset_vf(vf, true);
1790 * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
1791 * @vf: pointer to the VF info
1793 static void ice_vc_reset_vf(struct ice_vf *vf)
1795 ice_vc_notify_vf_reset(vf);
1796 ice_reset_vf(vf, false);
1800 * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
1801 * @pf: PF used to index all VFs
1802 * @pfq: queue index relative to the PF's function space
1804 * If no VF is found who owns the pfq then return NULL, otherwise return a
1805 * pointer to the VF who owns the pfq
1807 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
1811 ice_for_each_vf(pf, vf_id) {
1812 struct ice_vf *vf = &pf->vf[vf_id];
1813 struct ice_vsi *vsi;
1816 vsi = pf->vsi[vf->lan_vsi_idx];
1818 ice_for_each_rxq(vsi, rxq_idx)
1819 if (vsi->rxq_map[rxq_idx] == pfq)
1827 * ice_globalq_to_pfq - convert from global queue index to PF space queue index
1828 * @pf: PF used for conversion
1829 * @globalq: global queue index used to convert to PF space queue index
1831 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
1833 return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
1837 * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
1838 * @pf: PF that the LAN overflow event happened on
1839 * @event: structure holding the event information for the LAN overflow event
1841 * Determine if the LAN overflow event was caused by a VF queue. If it was not
1842 * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
1843 * reset on the offending VF.
1846 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1848 u32 gldcb_rtctq, queue;
1851 gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
1852 dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
1854 /* event returns device global Rx queue number */
1855 queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
1856 GLDCB_RTCTQ_RXQNUM_S;
1858 vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
1862 ice_vc_reset_vf(vf);
1866 * ice_vc_send_msg_to_vf - Send message to VF
1867 * @vf: pointer to the VF info
1868 * @v_opcode: virtual channel opcode
1869 * @v_retval: virtual channel return value
1870 * @msg: pointer to the msg buffer
1871 * @msglen: msg length
1876 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
1877 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
1879 enum ice_status aq_ret;
1887 if (ice_validate_vf_id(pf, vf->vf_id))
1890 dev = ice_pf_to_dev(pf);
1892 /* single place to detect unsuccessful return values */
1894 vf->num_inval_msgs++;
1895 dev_info(dev, "VF %d failed opcode %d, retval: %d\n", vf->vf_id,
1896 v_opcode, v_retval);
1897 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
1898 dev_err(dev, "Number of invalid messages exceeded for VF %d\n",
1900 dev_err(dev, "Use PF Control I/F to enable the VF\n");
1901 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1905 vf->num_valid_msgs++;
1906 /* reset the invalid counter, if a valid message is received. */
1907 vf->num_inval_msgs = 0;
1910 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
1912 if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
1913 dev_info(dev, "Unable to send the message to VF %d ret %s aq_err %s\n",
1914 vf->vf_id, ice_stat_str(aq_ret),
1915 ice_aq_str(pf->hw.mailboxq.sq_last_status));
1923 * ice_vc_get_ver_msg
1924 * @vf: pointer to the VF info
1925 * @msg: pointer to the msg buffer
1927 * called from the VF to request the API version used by the PF
1929 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
1931 struct virtchnl_version_info info = {
1932 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1935 vf->vf_ver = *(struct virtchnl_version_info *)msg;
1936 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1937 if (VF_IS_V10(&vf->vf_ver))
1938 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1940 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1941 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
1942 sizeof(struct virtchnl_version_info));
1946 * ice_vc_get_vf_res_msg
1947 * @vf: pointer to the VF info
1948 * @msg: pointer to the msg buffer
1950 * called from the VF to request its resources
1952 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
1954 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1955 struct virtchnl_vf_resource *vfres = NULL;
1956 struct ice_pf *pf = vf->pf;
1957 struct ice_vsi *vsi;
1961 if (ice_check_vf_init(pf, vf)) {
1962 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1966 len = sizeof(struct virtchnl_vf_resource);
1968 vfres = kzalloc(len, GFP_KERNEL);
1970 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1974 if (VF_IS_V11(&vf->vf_ver))
1975 vf->driver_caps = *(u32 *)msg;
1977 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1978 VIRTCHNL_VF_OFFLOAD_RSS_REG |
1979 VIRTCHNL_VF_OFFLOAD_VLAN;
1981 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1982 vsi = pf->vsi[vf->lan_vsi_idx];
1984 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1988 if (!vsi->info.pvid)
1989 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1991 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1992 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1994 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1995 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1997 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
2000 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
2001 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
2003 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
2004 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
2006 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
2007 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
2009 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
2010 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
2012 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2013 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
2015 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
2016 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
2018 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
2019 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
2021 vfres->num_vsis = 1;
2022 /* Tx and Rx queue are equal for VF */
2023 vfres->num_queue_pairs = vsi->num_txq;
2024 vfres->max_vectors = pf->num_msix_per_vf;
2025 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
2026 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
2028 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
2029 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
2030 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
2031 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2032 vf->dflt_lan_addr.addr);
2034 /* match guest capabilities */
2035 vf->driver_caps = vfres->vf_cap_flags;
2037 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
2040 /* send the response back to the VF */
2041 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
2049 * ice_vc_reset_vf_msg
2050 * @vf: pointer to the VF info
2052 * called from the VF to reset itself,
2053 * unlike other virtchnl messages, PF driver
2054 * doesn't send the response back to the VF
2056 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
2058 if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
2059 ice_reset_vf(vf, false);
2063 * ice_find_vsi_from_id
2064 * @pf: the PF structure to search for the VSI
2065 * @id: ID of the VSI it is searching for
2067 * searches for the VSI with the given ID
2069 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
2073 ice_for_each_vsi(pf, i)
2074 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
2081 * ice_vc_isvalid_vsi_id
2082 * @vf: pointer to the VF info
2083 * @vsi_id: VF relative VSI ID
2085 * check for the valid VSI ID
2087 static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
2089 struct ice_pf *pf = vf->pf;
2090 struct ice_vsi *vsi;
2092 vsi = ice_find_vsi_from_id(pf, vsi_id);
2094 return (vsi && (vsi->vf_id == vf->vf_id));
2098 * ice_vc_isvalid_q_id
2099 * @vf: pointer to the VF info
2101 * @qid: VSI relative queue ID
2103 * check for the valid queue ID
2105 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
2107 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
2108 /* allocated Tx and Rx queues should be always equal for VF VSI */
2109 return (vsi && (qid < vsi->alloc_txq));
2113 * ice_vc_isvalid_ring_len
2114 * @ring_len: length of ring
2116 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
2119 static bool ice_vc_isvalid_ring_len(u16 ring_len)
2121 return ring_len == 0 ||
2122 (ring_len >= ICE_MIN_NUM_DESC &&
2123 ring_len <= ICE_MAX_NUM_DESC &&
2124 !(ring_len % ICE_REQ_DESC_MULTIPLE));
2128 * ice_vc_config_rss_key
2129 * @vf: pointer to the VF info
2130 * @msg: pointer to the msg buffer
2132 * Configure the VF's RSS key
2134 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
2136 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2137 struct virtchnl_rss_key *vrk =
2138 (struct virtchnl_rss_key *)msg;
2139 struct ice_pf *pf = vf->pf;
2140 struct ice_vsi *vsi;
2142 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2143 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2147 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
2148 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2152 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
2153 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2157 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2158 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2162 vsi = pf->vsi[vf->lan_vsi_idx];
2164 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2168 if (ice_set_rss(vsi, vrk->key, NULL, 0))
2169 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2171 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
2176 * ice_vc_config_rss_lut
2177 * @vf: pointer to the VF info
2178 * @msg: pointer to the msg buffer
2180 * Configure the VF's RSS LUT
2182 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
2184 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2185 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2186 struct ice_pf *pf = vf->pf;
2187 struct ice_vsi *vsi;
2189 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2190 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2194 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
2195 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2199 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
2200 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2204 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2205 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2209 vsi = pf->vsi[vf->lan_vsi_idx];
2211 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2215 if (ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
2216 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2218 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
2223 * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
2224 * @vf: The VF being resseting
2226 * The max poll time is about ~800ms, which is about the maximum time it takes
2227 * for a VF to be reset and/or a VF driver to be removed.
2229 static void ice_wait_on_vf_reset(struct ice_vf *vf)
2233 for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
2234 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
2236 msleep(ICE_MAX_VF_RESET_SLEEP_MS);
2241 * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
2242 * @vf: VF to check if it's ready to be configured/queried
2244 * The purpose of this function is to make sure the VF is not in reset, not
2245 * disabled, and initialized so it can be configured and/or queried by a host
2248 static int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
2252 ice_wait_on_vf_reset(vf);
2254 if (ice_is_vf_disabled(vf))
2258 if (ice_check_vf_init(pf, vf))
2265 * ice_set_vf_spoofchk
2266 * @netdev: network interface device structure
2267 * @vf_id: VF identifier
2268 * @ena: flag to enable or disable feature
2270 * Enable or disable VF spoof checking
2272 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2274 struct ice_netdev_priv *np = netdev_priv(netdev);
2275 struct ice_pf *pf = np->vsi->back;
2276 struct ice_vsi_ctx *ctx;
2277 struct ice_vsi *vf_vsi;
2278 enum ice_status status;
2283 dev = ice_pf_to_dev(pf);
2284 if (ice_validate_vf_id(pf, vf_id))
2287 vf = &pf->vf[vf_id];
2288 ret = ice_check_vf_ready_for_cfg(vf);
2292 vf_vsi = pf->vsi[vf->lan_vsi_idx];
2294 netdev_err(netdev, "VSI %d for VF %d is null\n",
2295 vf->lan_vsi_idx, vf->vf_id);
2299 if (vf_vsi->type != ICE_VSI_VF) {
2300 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
2301 vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
2305 if (ena == vf->spoofchk) {
2306 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
2310 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2314 ctx->info.sec_flags = vf_vsi->info.sec_flags;
2315 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2317 ctx->info.sec_flags |=
2318 ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2319 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2320 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
2322 ctx->info.sec_flags &=
2323 ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2324 (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2325 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
2328 status = ice_update_vsi(&pf->hw, vf_vsi->idx, ctx, NULL);
2330 dev_err(dev, "Failed to %sable spoofchk on VF %d VSI %d\n error %s\n",
2331 ena ? "en" : "dis", vf->vf_id, vf_vsi->vsi_num,
2332 ice_stat_str(status));
2337 /* only update spoofchk state and VSI context on success */
2338 vf_vsi->info.sec_flags = ctx->info.sec_flags;
2347 * ice_is_any_vf_in_promisc - check if any VF(s) are in promiscuous mode
2348 * @pf: PF structure for accessing VF(s)
2350 * Return false if no VF(s) are in unicast and/or multicast promiscuous mode,
2353 bool ice_is_any_vf_in_promisc(struct ice_pf *pf)
2357 ice_for_each_vf(pf, vf_idx) {
2358 struct ice_vf *vf = &pf->vf[vf_idx];
2360 /* found a VF that has promiscuous mode configured */
2361 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2362 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2370 * ice_vc_cfg_promiscuous_mode_msg
2371 * @vf: pointer to the VF info
2372 * @msg: pointer to the msg buffer
2374 * called from the VF to configure VF VSIs promiscuous mode
2376 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
2378 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2379 struct virtchnl_promisc_info *info =
2380 (struct virtchnl_promisc_info *)msg;
2381 struct ice_pf *pf = vf->pf;
2382 struct ice_vsi *vsi;
2387 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2388 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2392 if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
2393 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2397 vsi = pf->vsi[vf->lan_vsi_idx];
2399 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2403 dev = ice_pf_to_dev(pf);
2404 if (!test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2405 dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
2407 /* Leave v_ret alone, lie to the VF on purpose. */
2411 rm_promisc = !(info->flags & FLAG_VF_UNICAST_PROMISC) &&
2412 !(info->flags & FLAG_VF_MULTICAST_PROMISC);
2414 if (vsi->num_vlan || vf->port_vlan_info) {
2415 struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
2416 struct net_device *pf_netdev;
2419 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2423 pf_netdev = pf_vsi->netdev;
2425 ret = ice_set_vf_spoofchk(pf_netdev, vf->vf_id, rm_promisc);
2427 dev_err(dev, "Failed to update spoofchk to %s for VF %d VSI %d when setting promiscuous mode\n",
2428 rm_promisc ? "ON" : "OFF", vf->vf_id,
2430 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2433 ret = ice_cfg_vlan_pruning(vsi, true, !rm_promisc);
2435 dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
2436 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2441 if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
2442 bool set_dflt_vsi = !!(info->flags & FLAG_VF_UNICAST_PROMISC);
2444 if (set_dflt_vsi && !ice_is_dflt_vsi_in_use(pf->first_sw))
2445 /* only attempt to set the default forwarding VSI if
2446 * it's not currently set
2448 ret = ice_set_dflt_vsi(pf->first_sw, vsi);
2449 else if (!set_dflt_vsi &&
2450 ice_is_vsi_dflt_vsi(pf->first_sw, vsi))
2451 /* only attempt to free the default forwarding VSI if we
2454 ret = ice_clear_dflt_vsi(pf->first_sw);
2457 dev_err(dev, "%sable VF %d as the default VSI failed, error %d\n",
2458 set_dflt_vsi ? "en" : "dis", vf->vf_id, ret);
2459 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2463 enum ice_status status;
2466 if (info->flags & FLAG_VF_UNICAST_PROMISC) {
2467 if (vf->port_vlan_info || vsi->num_vlan)
2468 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
2470 promisc_m = ICE_UCAST_PROMISC_BITS;
2471 } else if (info->flags & FLAG_VF_MULTICAST_PROMISC) {
2472 if (vf->port_vlan_info || vsi->num_vlan)
2473 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
2475 promisc_m = ICE_MCAST_PROMISC_BITS;
2477 if (vf->port_vlan_info || vsi->num_vlan)
2478 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
2480 promisc_m = ICE_UCAST_PROMISC_BITS;
2483 /* Configure multicast/unicast with or without VLAN promiscuous
2486 status = ice_vf_set_vsi_promisc(vf, vsi, promisc_m, rm_promisc);
2488 dev_err(dev, "%sable Tx/Rx filter promiscuous mode on VF-%d failed, error: %s\n",
2489 rm_promisc ? "dis" : "en", vf->vf_id,
2490 ice_stat_str(status));
2491 v_ret = ice_err_to_virt_err(status);
2494 dev_dbg(dev, "%sable Tx/Rx filter promiscuous mode on VF-%d succeeded\n",
2495 rm_promisc ? "dis" : "en", vf->vf_id);
2499 if (info->flags & FLAG_VF_MULTICAST_PROMISC)
2500 set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
2502 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
2504 if (info->flags & FLAG_VF_UNICAST_PROMISC)
2505 set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
2507 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
2510 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
2515 * ice_vc_get_stats_msg
2516 * @vf: pointer to the VF info
2517 * @msg: pointer to the msg buffer
2519 * called from the VF to get VSI stats
2521 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
2523 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2524 struct virtchnl_queue_select *vqs =
2525 (struct virtchnl_queue_select *)msg;
2526 struct ice_eth_stats stats = { 0 };
2527 struct ice_pf *pf = vf->pf;
2528 struct ice_vsi *vsi;
2530 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2531 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2535 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2536 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2540 vsi = pf->vsi[vf->lan_vsi_idx];
2542 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2546 ice_update_eth_stats(vsi);
2548 stats = vsi->eth_stats;
2551 /* send the response to the VF */
2552 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
2553 (u8 *)&stats, sizeof(stats));
2557 * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
2558 * @vqs: virtchnl_queue_select structure containing bitmaps to validate
2560 * Return true on successful validation, else false
2562 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
2564 if ((!vqs->rx_queues && !vqs->tx_queues) ||
2565 vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
2566 vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
2573 * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
2574 * @vsi: VSI of the VF to configure
2575 * @q_idx: VF queue index used to determine the queue in the PF's space
2577 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
2579 struct ice_hw *hw = &vsi->back->hw;
2580 u32 pfq = vsi->txq_map[q_idx];
2583 reg = rd32(hw, QINT_TQCTL(pfq));
2585 /* MSI-X index 0 in the VF's space is always for the OICR, which means
2586 * this is most likely a poll mode VF driver, so don't enable an
2587 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
2589 if (!(reg & QINT_TQCTL_MSIX_INDX_M))
2592 wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
2596 * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
2597 * @vsi: VSI of the VF to configure
2598 * @q_idx: VF queue index used to determine the queue in the PF's space
2600 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
2602 struct ice_hw *hw = &vsi->back->hw;
2603 u32 pfq = vsi->rxq_map[q_idx];
2606 reg = rd32(hw, QINT_RQCTL(pfq));
2608 /* MSI-X index 0 in the VF's space is always for the OICR, which means
2609 * this is most likely a poll mode VF driver, so don't enable an
2610 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
2612 if (!(reg & QINT_RQCTL_MSIX_INDX_M))
2615 wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
2620 * @vf: pointer to the VF info
2621 * @msg: pointer to the msg buffer
2623 * called from the VF to enable all or specific queue(s)
2625 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
2627 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2628 struct virtchnl_queue_select *vqs =
2629 (struct virtchnl_queue_select *)msg;
2630 struct ice_pf *pf = vf->pf;
2631 struct ice_vsi *vsi;
2632 unsigned long q_map;
2635 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2636 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2640 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2641 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2645 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
2646 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2650 vsi = pf->vsi[vf->lan_vsi_idx];
2652 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2656 /* Enable only Rx rings, Tx rings were enabled by the FW when the
2657 * Tx queue group list was configured and the context bits were
2658 * programmed using ice_vsi_cfg_txqs
2660 q_map = vqs->rx_queues;
2661 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2662 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2663 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2667 /* Skip queue if enabled */
2668 if (test_bit(vf_q_id, vf->rxq_ena))
2671 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
2672 dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
2673 vf_q_id, vsi->vsi_num);
2674 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2678 ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
2679 set_bit(vf_q_id, vf->rxq_ena);
2682 vsi = pf->vsi[vf->lan_vsi_idx];
2683 q_map = vqs->tx_queues;
2684 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2685 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2686 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2690 /* Skip queue if enabled */
2691 if (test_bit(vf_q_id, vf->txq_ena))
2694 ice_vf_ena_txq_interrupt(vsi, vf_q_id);
2695 set_bit(vf_q_id, vf->txq_ena);
2698 /* Set flag to indicate that queues are enabled */
2699 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
2700 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
2703 /* send the response to the VF */
2704 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
2710 * @vf: pointer to the VF info
2711 * @msg: pointer to the msg buffer
2713 * called from the VF to disable all or specific
2716 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
2718 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2719 struct virtchnl_queue_select *vqs =
2720 (struct virtchnl_queue_select *)msg;
2721 struct ice_pf *pf = vf->pf;
2722 struct ice_vsi *vsi;
2723 unsigned long q_map;
2726 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
2727 !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
2728 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2732 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2733 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2737 if (!ice_vc_validate_vqs_bitmaps(vqs)) {
2738 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2742 vsi = pf->vsi[vf->lan_vsi_idx];
2744 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2748 if (vqs->tx_queues) {
2749 q_map = vqs->tx_queues;
2751 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2752 struct ice_ring *ring = vsi->tx_rings[vf_q_id];
2753 struct ice_txq_meta txq_meta = { 0 };
2755 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2756 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2760 /* Skip queue if not enabled */
2761 if (!test_bit(vf_q_id, vf->txq_ena))
2764 ice_fill_txq_meta(vsi, ring, &txq_meta);
2766 if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
2768 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
2769 vf_q_id, vsi->vsi_num);
2770 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2774 /* Clear enabled queues flag */
2775 clear_bit(vf_q_id, vf->txq_ena);
2779 q_map = vqs->rx_queues;
2780 /* speed up Rx queue disable by batching them if possible */
2782 bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
2783 if (ice_vsi_stop_all_rx_rings(vsi)) {
2784 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
2786 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2790 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
2792 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2793 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2794 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2798 /* Skip queue if not enabled */
2799 if (!test_bit(vf_q_id, vf->rxq_ena))
2802 if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
2804 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
2805 vf_q_id, vsi->vsi_num);
2806 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2810 /* Clear enabled queues flag */
2811 clear_bit(vf_q_id, vf->rxq_ena);
2815 /* Clear enabled queues flag */
2816 if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
2817 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
2820 /* send the response to the VF */
2821 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
2827 * @vf: pointer to the VF info
2828 * @vsi: the VSI being configured
2829 * @vector_id: vector ID
2830 * @map: vector map for mapping vectors to queues
2831 * @q_vector: structure for interrupt vector
2832 * configure the IRQ to queue map
2835 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
2836 struct virtchnl_vector_map *map,
2837 struct ice_q_vector *q_vector)
2839 u16 vsi_q_id, vsi_q_id_idx;
2842 q_vector->num_ring_rx = 0;
2843 q_vector->num_ring_tx = 0;
2845 qmap = map->rxq_map;
2846 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
2847 vsi_q_id = vsi_q_id_idx;
2849 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
2850 return VIRTCHNL_STATUS_ERR_PARAM;
2852 q_vector->num_ring_rx++;
2853 q_vector->rx.itr_idx = map->rxitr_idx;
2854 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
2855 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
2856 q_vector->rx.itr_idx);
2859 qmap = map->txq_map;
2860 for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
2861 vsi_q_id = vsi_q_id_idx;
2863 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
2864 return VIRTCHNL_STATUS_ERR_PARAM;
2866 q_vector->num_ring_tx++;
2867 q_vector->tx.itr_idx = map->txitr_idx;
2868 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
2869 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
2870 q_vector->tx.itr_idx);
2873 return VIRTCHNL_STATUS_SUCCESS;
2877 * ice_vc_cfg_irq_map_msg
2878 * @vf: pointer to the VF info
2879 * @msg: pointer to the msg buffer
2881 * called from the VF to configure the IRQ to queue map
2883 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
2885 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2886 u16 num_q_vectors_mapped, vsi_id, vector_id;
2887 struct virtchnl_irq_map_info *irqmap_info;
2888 struct virtchnl_vector_map *map;
2889 struct ice_pf *pf = vf->pf;
2890 struct ice_vsi *vsi;
2893 irqmap_info = (struct virtchnl_irq_map_info *)msg;
2894 num_q_vectors_mapped = irqmap_info->num_vectors;
2896 /* Check to make sure number of VF vectors mapped is not greater than
2897 * number of VF vectors originally allocated, and check that
2898 * there is actually at least a single VF queue vector mapped
2900 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2901 pf->num_msix_per_vf < num_q_vectors_mapped ||
2902 !num_q_vectors_mapped) {
2903 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2907 vsi = pf->vsi[vf->lan_vsi_idx];
2909 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2913 for (i = 0; i < num_q_vectors_mapped; i++) {
2914 struct ice_q_vector *q_vector;
2916 map = &irqmap_info->vecmap[i];
2918 vector_id = map->vector_id;
2919 vsi_id = map->vsi_id;
2920 /* vector_id is always 0-based for each VF, and can never be
2921 * larger than or equal to the max allowed interrupts per VF
2923 if (!(vector_id < pf->num_msix_per_vf) ||
2924 !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
2925 (!vector_id && (map->rxq_map || map->txq_map))) {
2926 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2930 /* No need to map VF miscellaneous or rogue vector */
2934 /* Subtract non queue vector from vector_id passed by VF
2935 * to get actual number of VSI queue vector array index
2937 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
2939 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2943 /* lookout for the invalid queue index */
2944 v_ret = (enum virtchnl_status_code)
2945 ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
2951 /* send the response to the VF */
2952 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
2958 * @vf: pointer to the VF info
2959 * @msg: pointer to the msg buffer
2961 * called from the VF to configure the Rx/Tx queues
2963 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
2965 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2966 struct virtchnl_vsi_queue_config_info *qci =
2967 (struct virtchnl_vsi_queue_config_info *)msg;
2968 struct virtchnl_queue_pair_info *qpi;
2969 u16 num_rxq = 0, num_txq = 0;
2970 struct ice_pf *pf = vf->pf;
2971 struct ice_vsi *vsi;
2974 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2975 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2979 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
2980 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2984 vsi = pf->vsi[vf->lan_vsi_idx];
2986 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2990 if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
2991 qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
2992 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
2993 vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
2994 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2998 for (i = 0; i < qci->num_queue_pairs; i++) {
2999 qpi = &qci->qpair[i];
3000 if (qpi->txq.vsi_id != qci->vsi_id ||
3001 qpi->rxq.vsi_id != qci->vsi_id ||
3002 qpi->rxq.queue_id != qpi->txq.queue_id ||
3003 qpi->txq.headwb_enabled ||
3004 !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
3005 !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
3006 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
3007 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3010 /* copy Tx queue info from VF into VSI */
3011 if (qpi->txq.ring_len > 0) {
3013 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
3014 vsi->tx_rings[i]->count = qpi->txq.ring_len;
3017 /* copy Rx queue info from VF into VSI */
3018 if (qpi->rxq.ring_len > 0) {
3020 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
3021 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
3023 if (qpi->rxq.databuffer_size != 0 &&
3024 (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
3025 qpi->rxq.databuffer_size < 1024)) {
3026 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3029 vsi->rx_buf_len = qpi->rxq.databuffer_size;
3030 vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
3031 if (qpi->rxq.max_pkt_size >= (16 * 1024) ||
3032 qpi->rxq.max_pkt_size < 64) {
3033 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3038 vsi->max_frame = qpi->rxq.max_pkt_size;
3041 /* VF can request to configure less than allocated queues
3042 * or default allocated queues. So update the VSI with new number
3044 vsi->num_txq = num_txq;
3045 vsi->num_rxq = num_rxq;
3046 /* All queues of VF VSI are in TC 0 */
3047 vsi->tc_cfg.tc_info[0].qcount_tx = num_txq;
3048 vsi->tc_cfg.tc_info[0].qcount_rx = num_rxq;
3050 if (ice_vsi_cfg_lan_txqs(vsi) || ice_vsi_cfg_rxqs(vsi))
3051 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3054 /* send the response to the VF */
3055 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
3061 * @vf: pointer to the VF info
3063 static bool ice_is_vf_trusted(struct ice_vf *vf)
3065 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
3069 * ice_can_vf_change_mac
3070 * @vf: pointer to the VF info
3072 * Return true if the VF is allowed to change its MAC filters, false otherwise
3074 static bool ice_can_vf_change_mac(struct ice_vf *vf)
3076 /* If the VF MAC address has been set administratively (via the
3077 * ndo_set_vf_mac command), then deny permission to the VF to
3078 * add/delete unicast MAC addresses, unless the VF is trusted
3080 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
3087 * ice_vc_add_mac_addr - attempt to add the MAC address passed in
3088 * @vf: pointer to the VF info
3089 * @vsi: pointer to the VF's VSI
3090 * @mac_addr: MAC address to add
3093 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr)
3095 struct device *dev = ice_pf_to_dev(vf->pf);
3096 enum ice_status status;
3098 /* default unicast MAC already added */
3099 if (ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
3102 if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
3103 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
3107 status = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3108 if (status == ICE_ERR_ALREADY_EXISTS) {
3109 dev_err(dev, "MAC %pM already exists for VF %d\n", mac_addr,
3112 } else if (status) {
3113 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %s\n",
3114 mac_addr, vf->vf_id, ice_stat_str(status));
3118 /* Set the default LAN address to the latest unicast MAC address added
3119 * by the VF. The default LAN address is reported by the PF via
3120 * ndo_get_vf_config.
3122 if (is_unicast_ether_addr(mac_addr))
3123 ether_addr_copy(vf->dflt_lan_addr.addr, mac_addr);
3131 * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
3132 * @vf: pointer to the VF info
3133 * @vsi: pointer to the VF's VSI
3134 * @mac_addr: MAC address to delete
3137 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr)
3139 struct device *dev = ice_pf_to_dev(vf->pf);
3140 enum ice_status status;
3142 if (!ice_can_vf_change_mac(vf) &&
3143 ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
3146 status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
3147 if (status == ICE_ERR_DOES_NOT_EXIST) {
3148 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
3151 } else if (status) {
3152 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %s\n",
3153 mac_addr, vf->vf_id, ice_stat_str(status));
3157 if (ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
3158 eth_zero_addr(vf->dflt_lan_addr.addr);
3166 * ice_vc_handle_mac_addr_msg
3167 * @vf: pointer to the VF info
3168 * @msg: pointer to the msg buffer
3169 * @set: true if MAC filters are being set, false otherwise
3171 * add guest MAC address filter
3174 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
3176 int (*ice_vc_cfg_mac)
3177 (struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr);
3178 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3179 struct virtchnl_ether_addr_list *al =
3180 (struct virtchnl_ether_addr_list *)msg;
3181 struct ice_pf *pf = vf->pf;
3182 enum virtchnl_ops vc_op;
3183 struct ice_vsi *vsi;
3187 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
3188 ice_vc_cfg_mac = ice_vc_add_mac_addr;
3190 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
3191 ice_vc_cfg_mac = ice_vc_del_mac_addr;
3194 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3195 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3196 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3197 goto handle_mac_exit;
3200 /* If this VF is not privileged, then we can't add more than a
3201 * limited number of addresses. Check to make sure that the
3202 * additions do not push us over the limit.
3204 if (set && !ice_is_vf_trusted(vf) &&
3205 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
3206 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",
3208 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3209 goto handle_mac_exit;
3212 vsi = pf->vsi[vf->lan_vsi_idx];
3214 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3215 goto handle_mac_exit;
3218 for (i = 0; i < al->num_elements; i++) {
3219 u8 *mac_addr = al->list[i].addr;
3222 if (is_broadcast_ether_addr(mac_addr) ||
3223 is_zero_ether_addr(mac_addr))
3226 result = ice_vc_cfg_mac(vf, vsi, mac_addr);
3227 if (result == -EEXIST || result == -ENOENT) {
3229 } else if (result) {
3230 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3231 goto handle_mac_exit;
3236 /* send the response to the VF */
3237 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
3241 * ice_vc_add_mac_addr_msg
3242 * @vf: pointer to the VF info
3243 * @msg: pointer to the msg buffer
3245 * add guest MAC address filter
3247 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
3249 return ice_vc_handle_mac_addr_msg(vf, msg, true);
3253 * ice_vc_del_mac_addr_msg
3254 * @vf: pointer to the VF info
3255 * @msg: pointer to the msg buffer
3257 * remove guest MAC address filter
3259 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
3261 return ice_vc_handle_mac_addr_msg(vf, msg, false);
3265 * ice_vc_request_qs_msg
3266 * @vf: pointer to the VF info
3267 * @msg: pointer to the msg buffer
3269 * VFs get a default number of queues but can use this message to request a
3270 * different number. If the request is successful, PF will reset the VF and
3271 * return 0. If unsuccessful, PF will send message informing VF of number of
3272 * available queue pairs via virtchnl message response to VF.
3274 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
3276 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3277 struct virtchnl_vf_res_request *vfres =
3278 (struct virtchnl_vf_res_request *)msg;
3279 u16 req_queues = vfres->num_queue_pairs;
3280 struct ice_pf *pf = vf->pf;
3281 u16 max_allowed_vf_queues;
3282 u16 tx_rx_queue_left;
3286 dev = ice_pf_to_dev(pf);
3287 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3288 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3292 cur_queues = vf->num_vf_qs;
3293 tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
3294 ice_get_avail_rxq_count(pf));
3295 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
3297 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
3299 } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
3300 dev_err(dev, "VF %d tried to request more than %d queues.\n",
3301 vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
3302 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
3303 } else if (req_queues > cur_queues &&
3304 req_queues - cur_queues > tx_rx_queue_left) {
3305 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
3306 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
3307 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
3308 ICE_MAX_RSS_QS_PER_VF);
3310 /* request is successful, then reset VF */
3311 vf->num_req_qs = req_queues;
3312 ice_vc_reset_vf(vf);
3313 dev_info(dev, "VF %d granted request of %u queues.\n",
3314 vf->vf_id, req_queues);
3319 /* send the response to the VF */
3320 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
3321 v_ret, (u8 *)vfres, sizeof(*vfres));
3325 * ice_set_vf_port_vlan
3326 * @netdev: network interface device structure
3327 * @vf_id: VF identifier
3328 * @vlan_id: VLAN ID being set
3329 * @qos: priority setting
3330 * @vlan_proto: VLAN protocol
3332 * program VF Port VLAN ID and/or QoS
3335 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
3338 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3339 struct ice_vsi *vsi;
3345 dev = ice_pf_to_dev(pf);
3346 if (ice_validate_vf_id(pf, vf_id))
3349 if (vlan_id >= VLAN_N_VID || qos > 7) {
3350 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
3351 vf_id, vlan_id, qos);
3355 if (vlan_proto != htons(ETH_P_8021Q)) {
3356 dev_err(dev, "VF VLAN protocol is not supported\n");
3357 return -EPROTONOSUPPORT;
3360 vf = &pf->vf[vf_id];
3361 vsi = pf->vsi[vf->lan_vsi_idx];
3363 ret = ice_check_vf_ready_for_cfg(vf);
3367 vlanprio = vlan_id | (qos << VLAN_PRIO_SHIFT);
3369 if (vf->port_vlan_info == vlanprio) {
3370 /* duplicate request, so just return success */
3371 dev_dbg(dev, "Duplicate pvid %d request\n", vlanprio);
3375 if (vlan_id || qos) {
3376 /* remove VLAN 0 filter set by default when transitioning from
3377 * no port VLAN to a port VLAN. No change to old port VLAN on
3380 ret = ice_vsi_kill_vlan(vsi, 0);
3383 ret = ice_vsi_manage_pvid(vsi, vlanprio, true);
3387 /* add VLAN 0 filter back when transitioning from port VLAN to
3388 * no port VLAN. No change to old port VLAN on failure.
3390 ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
3393 ret = ice_vsi_manage_pvid(vsi, 0, false);
3399 dev_info(dev, "Setting VLAN %d, QoS 0x%x on VF %d\n",
3400 vlan_id, qos, vf_id);
3402 /* add VLAN filter for the port VLAN */
3403 ret = ice_vsi_add_vlan(vsi, vlan_id, ICE_FWD_TO_VSI);
3407 /* remove old port VLAN filter with valid VLAN ID or QoS fields */
3408 if (vf->port_vlan_info)
3409 ice_vsi_kill_vlan(vsi, vf->port_vlan_info & VLAN_VID_MASK);
3411 /* keep port VLAN information persistent on resets */
3412 vf->port_vlan_info = le16_to_cpu(vsi->info.pvid);
3418 * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
3419 * @caps: VF driver negotiated capabilities
3421 * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
3423 static bool ice_vf_vlan_offload_ena(u32 caps)
3425 return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
3429 * ice_vc_process_vlan_msg
3430 * @vf: pointer to the VF info
3431 * @msg: pointer to the msg buffer
3432 * @add_v: Add VLAN if true, otherwise delete VLAN
3434 * Process virtchnl op to add or remove programmed guest VLAN ID
3436 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
3438 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3439 struct virtchnl_vlan_filter_list *vfl =
3440 (struct virtchnl_vlan_filter_list *)msg;
3441 struct ice_pf *pf = vf->pf;
3442 bool vlan_promisc = false;
3443 struct ice_vsi *vsi;
3450 dev = ice_pf_to_dev(pf);
3451 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3452 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3456 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
3457 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3461 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
3462 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3466 for (i = 0; i < vfl->num_elements; i++) {
3467 if (vfl->vlan_id[i] >= VLAN_N_VID) {
3468 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3469 dev_err(dev, "invalid VF VLAN id %d\n",
3476 vsi = pf->vsi[vf->lan_vsi_idx];
3478 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3482 if (add_v && !ice_is_vf_trusted(vf) &&
3483 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
3484 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
3486 /* There is no need to let VF know about being not trusted,
3487 * so we can just return success message here
3492 if (vsi->info.pvid) {
3493 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3497 if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
3498 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
3499 test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags))
3500 vlan_promisc = true;
3503 for (i = 0; i < vfl->num_elements; i++) {
3504 u16 vid = vfl->vlan_id[i];
3506 if (!ice_is_vf_trusted(vf) &&
3507 vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
3508 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
3510 /* There is no need to let VF know about being
3511 * not trusted, so we can just return success
3512 * message here as well.
3517 /* we add VLAN 0 by default for each VF so we can enable
3518 * Tx VLAN anti-spoof without triggering MDD events so
3519 * we don't need to add it again here
3524 status = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
3526 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3530 /* Enable VLAN pruning when non-zero VLAN is added */
3531 if (!vlan_promisc && vid &&
3532 !ice_vsi_is_vlan_pruning_ena(vsi)) {
3533 status = ice_cfg_vlan_pruning(vsi, true, false);
3535 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3536 dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
3540 } else if (vlan_promisc) {
3541 /* Enable Ucast/Mcast VLAN promiscuous mode */
3542 promisc_m = ICE_PROMISC_VLAN_TX |
3543 ICE_PROMISC_VLAN_RX;
3545 status = ice_set_vsi_promisc(hw, vsi->idx,
3548 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3549 dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
3555 /* In case of non_trusted VF, number of VLAN elements passed
3556 * to PF for removal might be greater than number of VLANs
3557 * filter programmed for that VF - So, use actual number of
3558 * VLANS added earlier with add VLAN opcode. In order to avoid
3559 * removing VLAN that doesn't exist, which result to sending
3560 * erroneous failed message back to the VF
3564 num_vf_vlan = vsi->num_vlan;
3565 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
3566 u16 vid = vfl->vlan_id[i];
3568 /* we add VLAN 0 by default for each VF so we can enable
3569 * Tx VLAN anti-spoof without triggering MDD events so
3570 * we don't want a VIRTCHNL request to remove it
3575 /* Make sure ice_vsi_kill_vlan is successful before
3576 * updating VLAN information
3578 status = ice_vsi_kill_vlan(vsi, vid);
3580 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3584 /* Disable VLAN pruning when only VLAN 0 is left */
3585 if (vsi->num_vlan == 1 &&
3586 ice_vsi_is_vlan_pruning_ena(vsi))
3587 ice_cfg_vlan_pruning(vsi, false, false);
3589 /* Disable Unicast/Multicast VLAN promiscuous mode */
3591 promisc_m = ICE_PROMISC_VLAN_TX |
3592 ICE_PROMISC_VLAN_RX;
3594 ice_clear_vsi_promisc(hw, vsi->idx,
3601 /* send the response to the VF */
3603 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
3606 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
3611 * ice_vc_add_vlan_msg
3612 * @vf: pointer to the VF info
3613 * @msg: pointer to the msg buffer
3615 * Add and program guest VLAN ID
3617 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
3619 return ice_vc_process_vlan_msg(vf, msg, true);
3623 * ice_vc_remove_vlan_msg
3624 * @vf: pointer to the VF info
3625 * @msg: pointer to the msg buffer
3627 * remove programmed guest VLAN ID
3629 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
3631 return ice_vc_process_vlan_msg(vf, msg, false);
3635 * ice_vc_ena_vlan_stripping
3636 * @vf: pointer to the VF info
3638 * Enable VLAN header stripping for a given VF
3640 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
3642 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3643 struct ice_pf *pf = vf->pf;
3644 struct ice_vsi *vsi;
3646 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3647 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3651 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
3652 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3656 vsi = pf->vsi[vf->lan_vsi_idx];
3657 if (ice_vsi_manage_vlan_stripping(vsi, true))
3658 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3661 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
3666 * ice_vc_dis_vlan_stripping
3667 * @vf: pointer to the VF info
3669 * Disable VLAN header stripping for a given VF
3671 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
3673 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3674 struct ice_pf *pf = vf->pf;
3675 struct ice_vsi *vsi;
3677 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3678 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3682 if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
3683 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3687 vsi = pf->vsi[vf->lan_vsi_idx];
3689 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3693 if (ice_vsi_manage_vlan_stripping(vsi, false))
3694 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3697 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
3702 * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
3703 * @vf: VF to enable/disable VLAN stripping for on initialization
3705 * If the VIRTCHNL_VF_OFFLOAD_VLAN flag is set enable VLAN stripping, else if
3706 * the flag is cleared then we want to disable stripping. For example, the flag
3707 * will be cleared when port VLANs are configured by the administrator before
3708 * passing the VF to the guest or if the AVF driver doesn't support VLAN
3711 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
3713 struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
3718 /* don't modify stripping if port VLAN is configured */
3722 if (ice_vf_vlan_offload_ena(vf->driver_caps))
3723 return ice_vsi_manage_vlan_stripping(vsi, true);
3725 return ice_vsi_manage_vlan_stripping(vsi, false);
3729 * ice_vc_process_vf_msg - Process request from VF
3730 * @pf: pointer to the PF structure
3731 * @event: pointer to the AQ event
3733 * called from the common asq/arq handler to
3734 * process request from VF
3736 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
3738 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
3739 s16 vf_id = le16_to_cpu(event->desc.retval);
3740 u16 msglen = event->msg_len;
3741 u8 *msg = event->msg_buf;
3742 struct ice_vf *vf = NULL;
3746 dev = ice_pf_to_dev(pf);
3747 if (ice_validate_vf_id(pf, vf_id)) {
3752 vf = &pf->vf[vf_id];
3754 /* Check if VF is disabled. */
3755 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
3760 /* Perform basic checks on the msg */
3761 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
3763 if (err == VIRTCHNL_STATUS_ERR_PARAM)
3771 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
3773 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
3774 vf_id, v_opcode, msglen, err);
3779 case VIRTCHNL_OP_VERSION:
3780 err = ice_vc_get_ver_msg(vf, msg);
3782 case VIRTCHNL_OP_GET_VF_RESOURCES:
3783 err = ice_vc_get_vf_res_msg(vf, msg);
3784 if (ice_vf_init_vlan_stripping(vf))
3785 dev_err(dev, "Failed to initialize VLAN stripping for VF %d\n",
3787 ice_vc_notify_vf_link_state(vf);
3789 case VIRTCHNL_OP_RESET_VF:
3790 ice_vc_reset_vf_msg(vf);
3792 case VIRTCHNL_OP_ADD_ETH_ADDR:
3793 err = ice_vc_add_mac_addr_msg(vf, msg);
3795 case VIRTCHNL_OP_DEL_ETH_ADDR:
3796 err = ice_vc_del_mac_addr_msg(vf, msg);
3798 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3799 err = ice_vc_cfg_qs_msg(vf, msg);
3801 case VIRTCHNL_OP_ENABLE_QUEUES:
3802 err = ice_vc_ena_qs_msg(vf, msg);
3803 ice_vc_notify_vf_link_state(vf);
3805 case VIRTCHNL_OP_DISABLE_QUEUES:
3806 err = ice_vc_dis_qs_msg(vf, msg);
3808 case VIRTCHNL_OP_REQUEST_QUEUES:
3809 err = ice_vc_request_qs_msg(vf, msg);
3811 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3812 err = ice_vc_cfg_irq_map_msg(vf, msg);
3814 case VIRTCHNL_OP_CONFIG_RSS_KEY:
3815 err = ice_vc_config_rss_key(vf, msg);
3817 case VIRTCHNL_OP_CONFIG_RSS_LUT:
3818 err = ice_vc_config_rss_lut(vf, msg);
3820 case VIRTCHNL_OP_GET_STATS:
3821 err = ice_vc_get_stats_msg(vf, msg);
3823 case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
3824 err = ice_vc_cfg_promiscuous_mode_msg(vf, msg);
3826 case VIRTCHNL_OP_ADD_VLAN:
3827 err = ice_vc_add_vlan_msg(vf, msg);
3829 case VIRTCHNL_OP_DEL_VLAN:
3830 err = ice_vc_remove_vlan_msg(vf, msg);
3832 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
3833 err = ice_vc_ena_vlan_stripping(vf);
3835 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
3836 err = ice_vc_dis_vlan_stripping(vf);
3838 case VIRTCHNL_OP_UNKNOWN:
3840 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
3842 err = ice_vc_send_msg_to_vf(vf, v_opcode,
3843 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
3848 /* Helper function cares less about error return values here
3849 * as it is busy with pending work.
3851 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
3852 vf_id, v_opcode, err);
3858 * @netdev: network interface device structure
3859 * @vf_id: VF identifier
3860 * @ivi: VF configuration structure
3862 * return VF configuration
3865 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
3867 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3870 if (ice_validate_vf_id(pf, vf_id))
3873 vf = &pf->vf[vf_id];
3875 if (ice_check_vf_init(pf, vf))
3879 ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
3881 /* VF configuration for VLAN and applicable QoS */
3882 ivi->vlan = vf->port_vlan_info & VLAN_VID_MASK;
3883 ivi->qos = (vf->port_vlan_info & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
3885 ivi->trusted = vf->trusted;
3886 ivi->spoofchk = vf->spoofchk;
3887 if (!vf->link_forced)
3888 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
3889 else if (vf->link_up)
3890 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
3892 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
3893 ivi->max_tx_rate = vf->tx_rate;
3894 ivi->min_tx_rate = 0;
3899 * ice_unicast_mac_exists - check if the unicast MAC exists on the PF's switch
3900 * @pf: PF used to reference the switch's rules
3901 * @umac: unicast MAC to compare against existing switch rules
3903 * Return true on the first/any match, else return false
3905 static bool ice_unicast_mac_exists(struct ice_pf *pf, u8 *umac)
3907 struct ice_sw_recipe *mac_recipe_list =
3908 &pf->hw.switch_info->recp_list[ICE_SW_LKUP_MAC];
3909 struct ice_fltr_mgmt_list_entry *list_itr;
3910 struct list_head *rule_head;
3911 struct mutex *rule_lock; /* protect MAC filter list access */
3913 rule_head = &mac_recipe_list->filt_rules;
3914 rule_lock = &mac_recipe_list->filt_rule_lock;
3916 mutex_lock(rule_lock);
3917 list_for_each_entry(list_itr, rule_head, list_entry) {
3918 u8 *existing_mac = &list_itr->fltr_info.l_data.mac.mac_addr[0];
3920 if (ether_addr_equal(existing_mac, umac)) {
3921 mutex_unlock(rule_lock);
3926 mutex_unlock(rule_lock);
3933 * @netdev: network interface device structure
3934 * @vf_id: VF identifier
3937 * program VF MAC address
3939 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
3941 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3945 if (ice_validate_vf_id(pf, vf_id))
3948 if (is_zero_ether_addr(mac) || is_multicast_ether_addr(mac)) {
3949 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
3953 vf = &pf->vf[vf_id];
3954 /* nothing left to do, unicast MAC already set */
3955 if (ether_addr_equal(vf->dflt_lan_addr.addr, mac))
3958 ret = ice_check_vf_ready_for_cfg(vf);
3962 if (ice_unicast_mac_exists(pf, mac)) {
3963 netdev_err(netdev, "Unicast MAC %pM already exists on this PF. Preventing setting VF %u unicast MAC address to %pM\n",
3968 /* copy MAC into dflt_lan_addr and trigger a VF reset. The reset
3969 * flow will use the updated dflt_lan_addr and add a MAC filter
3970 * using ice_add_mac. Also set pf_set_mac to indicate that the PF has
3971 * set the MAC address for this VF.
3973 ether_addr_copy(vf->dflt_lan_addr.addr, mac);
3974 vf->pf_set_mac = true;
3975 netdev_info(netdev, "MAC on VF %d set to %pM. VF driver will be reinitialized\n",
3978 ice_vc_reset_vf(vf);
3984 * @netdev: network interface device structure
3985 * @vf_id: VF identifier
3986 * @trusted: Boolean value to enable/disable trusted VF
3988 * Enable or disable a given VF as trusted
3990 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
3992 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3996 if (ice_validate_vf_id(pf, vf_id))
3999 vf = &pf->vf[vf_id];
4000 ret = ice_check_vf_ready_for_cfg(vf);
4004 /* Check if already trusted */
4005 if (trusted == vf->trusted)
4008 vf->trusted = trusted;
4009 ice_vc_reset_vf(vf);
4010 dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
4011 vf_id, trusted ? "" : "un");
4017 * ice_set_vf_link_state
4018 * @netdev: network interface device structure
4019 * @vf_id: VF identifier
4020 * @link_state: required link state
4022 * Set VF's link state, irrespective of physical link state status
4024 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
4026 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4030 if (ice_validate_vf_id(pf, vf_id))
4033 vf = &pf->vf[vf_id];
4034 ret = ice_check_vf_ready_for_cfg(vf);
4038 switch (link_state) {
4039 case IFLA_VF_LINK_STATE_AUTO:
4040 vf->link_forced = false;
4042 case IFLA_VF_LINK_STATE_ENABLE:
4043 vf->link_forced = true;
4046 case IFLA_VF_LINK_STATE_DISABLE:
4047 vf->link_forced = true;
4048 vf->link_up = false;
4054 ice_vc_notify_vf_link_state(vf);
4060 * ice_get_vf_stats - populate some stats for the VF
4061 * @netdev: the netdev of the PF
4062 * @vf_id: the host OS identifier (0-255)
4063 * @vf_stats: pointer to the OS memory to be initialized
4065 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
4066 struct ifla_vf_stats *vf_stats)
4068 struct ice_pf *pf = ice_netdev_to_pf(netdev);
4069 struct ice_eth_stats *stats;
4070 struct ice_vsi *vsi;
4074 if (ice_validate_vf_id(pf, vf_id))
4077 vf = &pf->vf[vf_id];
4078 ret = ice_check_vf_ready_for_cfg(vf);
4082 vsi = pf->vsi[vf->lan_vsi_idx];
4086 ice_update_eth_stats(vsi);
4087 stats = &vsi->eth_stats;
4089 memset(vf_stats, 0, sizeof(*vf_stats));
4091 vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
4092 stats->rx_multicast;
4093 vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
4094 stats->tx_multicast;
4095 vf_stats->rx_bytes = stats->rx_bytes;
4096 vf_stats->tx_bytes = stats->tx_bytes;
4097 vf_stats->broadcast = stats->rx_broadcast;
4098 vf_stats->multicast = stats->rx_multicast;
4099 vf_stats->rx_dropped = stats->rx_discards;
4100 vf_stats->tx_dropped = stats->tx_discards;
4106 * ice_print_vf_rx_mdd_event - print VF Rx malicious driver detect event
4107 * @vf: pointer to the VF structure
4109 void ice_print_vf_rx_mdd_event(struct ice_vf *vf)
4111 struct ice_pf *pf = vf->pf;
4114 dev = ice_pf_to_dev(pf);
4116 dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
4117 vf->mdd_rx_events.count, pf->hw.pf_id, vf->vf_id,
4118 vf->dflt_lan_addr.addr,
4119 test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
4124 * ice_print_vfs_mdd_event - print VFs malicious driver detect event
4125 * @pf: pointer to the PF structure
4127 * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
4129 void ice_print_vfs_mdd_events(struct ice_pf *pf)
4131 struct device *dev = ice_pf_to_dev(pf);
4132 struct ice_hw *hw = &pf->hw;
4135 /* check that there are pending MDD events to print */
4136 if (!test_and_clear_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state))
4139 /* VF MDD event logs are rate limited to one second intervals */
4140 if (time_is_after_jiffies(pf->last_printed_mdd_jiffies + HZ * 1))
4143 pf->last_printed_mdd_jiffies = jiffies;
4145 ice_for_each_vf(pf, i) {
4146 struct ice_vf *vf = &pf->vf[i];
4148 /* only print Rx MDD event message if there are new events */
4149 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
4150 vf->mdd_rx_events.last_printed =
4151 vf->mdd_rx_events.count;
4152 ice_print_vf_rx_mdd_event(vf);
4155 /* only print Tx MDD event message if there are new events */
4156 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
4157 vf->mdd_tx_events.last_printed =
4158 vf->mdd_tx_events.count;
4160 dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
4161 vf->mdd_tx_events.count, hw->pf_id, i,
4162 vf->dflt_lan_addr.addr);