1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
9 * ice_err_to_virt err - translate errors for VF return code
10 * @ice_err: error return code
12 static enum virtchnl_status_code ice_err_to_virt_err(enum ice_status ice_err)
16 return VIRTCHNL_STATUS_SUCCESS;
18 case ICE_ERR_INVAL_SIZE:
19 case ICE_ERR_DEVICE_NOT_SUPPORTED:
22 return VIRTCHNL_STATUS_ERR_PARAM;
23 case ICE_ERR_NO_MEMORY:
24 return VIRTCHNL_STATUS_ERR_NO_MEMORY;
25 case ICE_ERR_NOT_READY:
26 case ICE_ERR_RESET_FAILED:
27 case ICE_ERR_FW_API_VER:
28 case ICE_ERR_AQ_ERROR:
29 case ICE_ERR_AQ_TIMEOUT:
31 case ICE_ERR_AQ_NO_WORK:
32 case ICE_ERR_AQ_EMPTY:
33 return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
35 return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
40 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
41 * @pf: pointer to the PF structure
42 * @v_opcode: operation code
43 * @v_retval: return value
44 * @msg: pointer to the msg buffer
48 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
49 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
51 struct ice_hw *hw = &pf->hw;
52 struct ice_vf *vf = pf->vf;
55 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
56 /* Not all vfs are enabled so skip the ones that are not */
57 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
58 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
61 /* Ignore return value on purpose - a given VF may fail, but
62 * we need to keep going and send to all of them
64 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
70 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
71 * @vf: pointer to the VF structure
72 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
73 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
74 * @link_up: whether or not to set the link up/down
77 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
78 int ice_link_speed, bool link_up)
80 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
81 pfe->event_data.link_event_adv.link_status = link_up;
83 pfe->event_data.link_event_adv.link_speed =
84 ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
86 pfe->event_data.link_event.link_status = link_up;
87 /* Legacy method for virtchnl link speeds */
88 pfe->event_data.link_event.link_speed =
89 (enum virtchnl_link_speed)
90 ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
95 * ice_set_pfe_link_forced - Force the virtchnl_pf_event link speed/status
96 * @vf: pointer to the VF structure
97 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
98 * @link_up: whether or not to set the link up/down
101 ice_set_pfe_link_forced(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
107 link_speed = ICE_AQ_LINK_SPEED_100GB;
109 link_speed = ICE_AQ_LINK_SPEED_UNKNOWN;
111 ice_set_pfe_link(vf, pfe, link_speed, link_up);
115 * ice_vc_notify_vf_link_state - Inform a VF of link status
116 * @vf: pointer to the VF structure
118 * send a link status message to a single VF
120 static void ice_vc_notify_vf_link_state(struct ice_vf *vf)
122 struct virtchnl_pf_event pfe = { 0 };
123 struct ice_link_status *ls;
124 struct ice_pf *pf = vf->pf;
128 ls = &hw->port_info->phy.link_info;
130 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
131 pfe.severity = PF_EVENT_SEVERITY_INFO;
133 /* Always report link is down if the VF queues aren't enabled */
135 ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
136 else if (vf->link_forced)
137 ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
139 ice_set_pfe_link(vf, &pfe, ls->link_speed, ls->link_info &
142 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
143 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
148 * ice_free_vf_res - Free a VF's resources
149 * @vf: pointer to the VF info
151 static void ice_free_vf_res(struct ice_vf *vf)
153 struct ice_pf *pf = vf->pf;
154 int i, last_vector_idx;
156 /* First, disable VF's configuration API to prevent OS from
157 * accessing the VF's VSI after it's freed or invalidated.
159 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
161 /* free VSI and disconnect it from the parent uplink */
162 if (vf->lan_vsi_idx) {
163 ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
169 last_vector_idx = vf->first_vector_idx + pf->num_vf_msix - 1;
170 /* Disable interrupts so that VF starts in a known state */
171 for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
172 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
175 /* reset some of the state variables keeping track of the resources */
176 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
177 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
181 * ice_dis_vf_mappings
182 * @vf: pointer to the VF structure
184 static void ice_dis_vf_mappings(struct ice_vf *vf)
186 struct ice_pf *pf = vf->pf;
192 vsi = pf->vsi[vf->lan_vsi_idx];
194 wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
195 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
197 first = vf->first_vector_idx;
198 last = first + pf->num_vf_msix - 1;
199 for (v = first; v <= last; v++) {
202 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
203 GLINT_VECT2FUNC_IS_PF_M) |
204 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
205 GLINT_VECT2FUNC_PF_NUM_M));
206 wr32(hw, GLINT_VECT2FUNC(v), reg);
209 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
210 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
212 dev_err(&pf->pdev->dev,
213 "Scattered mode for VF Tx queues is not yet implemented\n");
215 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
216 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
218 dev_err(&pf->pdev->dev,
219 "Scattered mode for VF Rx queues is not yet implemented\n");
223 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
224 * @pf: pointer to the PF structure
226 * If MSIX entries from the pf->irq_tracker were needed then we need to
227 * reset the irq_tracker->end and give back the entries we needed to
230 * If no MSIX entries were taken from the pf->irq_tracker then just clear
231 * the pf->sriov_base_vector.
233 * Returns 0 on success, and -EINVAL on error.
235 static int ice_sriov_free_msix_res(struct ice_pf *pf)
237 struct ice_res_tracker *res;
242 res = pf->irq_tracker;
246 /* give back irq_tracker resources used */
247 if (pf->sriov_base_vector < res->num_entries) {
248 res->end = res->num_entries;
249 pf->num_avail_sw_msix +=
250 res->num_entries - pf->sriov_base_vector;
253 pf->sriov_base_vector = 0;
259 * ice_set_vf_state_qs_dis - Set VF queues state to disabled
260 * @vf: pointer to the VF structure
262 void ice_set_vf_state_qs_dis(struct ice_vf *vf)
264 /* Clear Rx/Tx enabled queues flag */
265 bitmap_zero(vf->txq_ena, ICE_MAX_BASE_QS_PER_VF);
266 bitmap_zero(vf->rxq_ena, ICE_MAX_BASE_QS_PER_VF);
268 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
272 * ice_dis_vf_qs - Disable the VF queues
273 * @vf: pointer to the VF structure
275 static void ice_dis_vf_qs(struct ice_vf *vf)
277 struct ice_pf *pf = vf->pf;
280 vsi = pf->vsi[vf->lan_vsi_idx];
282 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
283 ice_vsi_stop_rx_rings(vsi);
284 ice_set_vf_state_qs_dis(vf);
288 * ice_free_vfs - Free all VFs
289 * @pf: pointer to the PF structure
291 void ice_free_vfs(struct ice_pf *pf)
293 struct ice_hw *hw = &pf->hw;
299 while (test_and_set_bit(__ICE_VF_DIS, pf->state))
300 usleep_range(1000, 2000);
302 /* Avoid wait time by stopping all VFs at the same time */
303 for (i = 0; i < pf->num_alloc_vfs; i++)
304 if (test_bit(ICE_VF_STATE_QS_ENA, pf->vf[i].vf_states))
305 ice_dis_vf_qs(&pf->vf[i]);
307 /* Disable IOV before freeing resources. This lets any VF drivers
308 * running in the host get themselves cleaned up before we yank
309 * the carpet out from underneath their feet.
311 if (!pci_vfs_assigned(pf->pdev))
312 pci_disable_sriov(pf->pdev);
314 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
316 tmp = pf->num_alloc_vfs;
318 pf->num_alloc_vfs = 0;
319 for (i = 0; i < tmp; i++) {
320 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
321 /* disable VF qp mappings */
322 ice_dis_vf_mappings(&pf->vf[i]);
323 ice_free_vf_res(&pf->vf[i]);
327 if (ice_sriov_free_msix_res(pf))
328 dev_err(&pf->pdev->dev,
329 "Failed to free MSIX resources used by SR-IOV\n");
331 devm_kfree(&pf->pdev->dev, pf->vf);
334 /* This check is for when the driver is unloaded while VFs are
335 * assigned. Setting the number of VFs to 0 through sysfs is caught
336 * before this function ever gets called.
338 if (!pci_vfs_assigned(pf->pdev)) {
341 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
342 * work correctly when SR-IOV gets re-enabled.
344 for (vf_id = 0; vf_id < tmp; vf_id++) {
345 u32 reg_idx, bit_idx;
347 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
348 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
349 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
352 clear_bit(__ICE_VF_DIS, pf->state);
353 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
357 * ice_trigger_vf_reset - Reset a VF on HW
358 * @vf: pointer to the VF structure
359 * @is_vflr: true if VFLR was issued, false if not
360 * @is_pfr: true if the reset was triggered due to a previous PFR
362 * Trigger hardware to start a reset for a particular VF. Expects the caller
363 * to wait the proper amount of time to allow hardware to reset the VF before
364 * it cleans up and restores VF functionality.
366 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
368 struct ice_pf *pf = vf->pf;
369 u32 reg, reg_idx, bit_idx;
374 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
376 /* Inform VF that it is no longer active, as a warning */
377 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
379 /* Disable VF's configuration API during reset. The flag is re-enabled
380 * in ice_alloc_vf_res(), when it's safe again to access VF's VSI.
381 * It's normally disabled in ice_free_vf_res(), but it's safer
382 * to do it earlier to give some time to finish to any VF config
383 * functions that may still be running at this point.
385 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
387 /* VF_MBX_ARQLEN is cleared by PFR, so the driver needs to clear it
388 * in the case of VFR. If this is done for PFR, it can mess up VF
389 * resets because the VF driver may already have started cleanup
390 * by the time we get here.
393 wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
395 /* In the case of a VFLR, the HW has already reset the VF and we
396 * just need to clean up, so don't hit the VFRTRIG register.
399 /* reset VF using VPGEN_VFRTRIG reg */
400 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
401 reg |= VPGEN_VFRTRIG_VFSWR_M;
402 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
404 /* clear the VFLR bit in GLGEN_VFLRSTAT */
405 reg_idx = (vf_abs_id) / 32;
406 bit_idx = (vf_abs_id) % 32;
407 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
410 wr32(hw, PF_PCI_CIAA,
411 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
412 for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
413 reg = rd32(hw, PF_PCI_CIAD);
414 /* no transactions pending so stop polling */
415 if ((reg & VF_TRANS_PENDING_M) == 0)
418 dev_err(&pf->pdev->dev,
419 "VF %d PCI transactions stuck\n", vf->vf_id);
420 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
425 * ice_vsi_set_pvid_fill_ctxt - Set VSI ctxt for add PVID
426 * @ctxt: the VSI ctxt to fill
427 * @vid: the VLAN ID to set as a PVID
429 static void ice_vsi_set_pvid_fill_ctxt(struct ice_vsi_ctx *ctxt, u16 vid)
431 ctxt->info.vlan_flags = (ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
432 ICE_AQ_VSI_PVLAN_INSERT_PVID |
433 ICE_AQ_VSI_VLAN_EMOD_STR);
434 ctxt->info.pvid = cpu_to_le16(vid);
435 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
436 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
437 ICE_AQ_VSI_PROP_SW_VALID);
441 * ice_vsi_kill_pvid_fill_ctxt - Set VSI ctx for remove PVID
442 * @ctxt: the VSI ctxt to fill
444 static void ice_vsi_kill_pvid_fill_ctxt(struct ice_vsi_ctx *ctxt)
446 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
447 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
448 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
449 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
450 ICE_AQ_VSI_PROP_SW_VALID);
454 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
455 * @vsi: the VSI to update
456 * @vid: the VLAN ID to set as a PVID
457 * @enable: true for enable PVID false for disable
459 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 vid, bool enable)
461 struct device *dev = &vsi->back->pdev->dev;
462 struct ice_hw *hw = &vsi->back->hw;
463 struct ice_vsi_ctx *ctxt;
464 enum ice_status status;
467 ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
471 ctxt->info = vsi->info;
473 ice_vsi_set_pvid_fill_ctxt(ctxt, vid);
475 ice_vsi_kill_pvid_fill_ctxt(ctxt);
477 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
479 dev_info(dev, "update VSI for port VLAN failed, err %d aq_err %d\n",
480 status, hw->adminq.sq_last_status);
485 vsi->info = ctxt->info;
487 devm_kfree(dev, ctxt);
492 * ice_vf_vsi_setup - Set up a VF VSI
493 * @pf: board private structure
494 * @pi: pointer to the port_info instance
495 * @vf_id: defines VF ID to which this VSI connects.
497 * Returns pointer to the successfully allocated VSI struct on success,
498 * otherwise returns NULL on failure.
500 static struct ice_vsi *
501 ice_vf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi, u16 vf_id)
503 return ice_vsi_setup(pf, pi, ICE_VSI_VF, vf_id);
507 * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
508 * @pf: pointer to PF structure
509 * @vf: pointer to VF that the first MSIX vector index is being calculated for
511 * This returns the first MSIX vector index in PF space that is used by this VF.
512 * This index is used when accessing PF relative registers such as
513 * GLINT_VECT2FUNC and GLINT_DYN_CTL.
514 * This will always be the OICR index in the AVF driver so any functionality
515 * using vf->first_vector_idx for queue configuration will have to increment by
516 * 1 to avoid meddling with the OICR index.
518 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
520 return pf->sriov_base_vector + vf->vf_id * pf->num_vf_msix;
524 * ice_alloc_vsi_res - Setup VF VSI and its resources
525 * @vf: pointer to the VF structure
527 * Returns 0 on success, negative value on failure
529 static int ice_alloc_vsi_res(struct ice_vf *vf)
531 struct ice_pf *pf = vf->pf;
532 LIST_HEAD(tmp_add_list);
533 u8 broadcast[ETH_ALEN];
537 /* first vector index is the VFs OICR index */
538 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
540 vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
542 dev_err(&pf->pdev->dev, "Failed to create VF VSI\n");
546 vf->lan_vsi_idx = vsi->idx;
547 vf->lan_vsi_num = vsi->vsi_num;
549 /* Check if port VLAN exist before, and restore it accordingly */
550 if (vf->port_vlan_id) {
551 ice_vsi_manage_pvid(vsi, vf->port_vlan_id, true);
552 ice_vsi_add_vlan(vsi, vf->port_vlan_id & ICE_VLAN_M);
555 eth_broadcast_addr(broadcast);
557 status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
559 goto ice_alloc_vsi_res_exit;
561 if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
562 status = ice_add_mac_to_list(vsi, &tmp_add_list,
563 vf->dflt_lan_addr.addr);
565 goto ice_alloc_vsi_res_exit;
568 status = ice_add_mac(&pf->hw, &tmp_add_list);
570 dev_err(&pf->pdev->dev,
571 "could not add mac filters error %d\n", status);
575 /* Clear this bit after VF initialization since we shouldn't reclaim
576 * and reassign interrupts for synchronous or asynchronous VFR events.
577 * We don't want to reconfigure interrupts since AVF driver doesn't
578 * expect vector assignment to be changed unless there is a request for
581 ice_alloc_vsi_res_exit:
582 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
587 * ice_alloc_vf_res - Allocate VF resources
588 * @vf: pointer to the VF structure
590 static int ice_alloc_vf_res(struct ice_vf *vf)
592 struct ice_pf *pf = vf->pf;
593 int tx_rx_queue_left;
596 /* Update number of VF queues, in case VF had requested for queue
599 tx_rx_queue_left = min_t(int, ice_get_avail_txq_count(pf),
600 ice_get_avail_rxq_count(pf));
601 tx_rx_queue_left += ICE_DFLT_QS_PER_VF;
602 if (vf->num_req_qs && vf->num_req_qs <= tx_rx_queue_left &&
603 vf->num_req_qs != vf->num_vf_qs)
604 vf->num_vf_qs = vf->num_req_qs;
606 /* setup VF VSI and necessary resources */
607 status = ice_alloc_vsi_res(vf);
609 goto ice_alloc_vf_res_exit;
612 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
614 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
616 /* VF is now completely initialized */
617 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
621 ice_alloc_vf_res_exit:
627 * ice_ena_vf_mappings
628 * @vf: pointer to the VF structure
630 * Enable VF vectors and queues allocation by writing the details into
631 * respective registers.
633 static void ice_ena_vf_mappings(struct ice_vf *vf)
635 int abs_vf_id, abs_first, abs_last;
636 struct ice_pf *pf = vf->pf;
643 vsi = pf->vsi[vf->lan_vsi_idx];
644 first = vf->first_vector_idx;
645 last = (first + pf->num_vf_msix) - 1;
646 abs_first = first + pf->hw.func_caps.common_cap.msix_vector_first_id;
647 abs_last = (abs_first + pf->num_vf_msix) - 1;
648 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
650 /* VF Vector allocation */
651 reg = (((abs_first << VPINT_ALLOC_FIRST_S) & VPINT_ALLOC_FIRST_M) |
652 ((abs_last << VPINT_ALLOC_LAST_S) & VPINT_ALLOC_LAST_M) |
653 VPINT_ALLOC_VALID_M);
654 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
656 reg = (((abs_first << VPINT_ALLOC_PCI_FIRST_S)
657 & VPINT_ALLOC_PCI_FIRST_M) |
658 ((abs_last << VPINT_ALLOC_PCI_LAST_S) & VPINT_ALLOC_PCI_LAST_M) |
659 VPINT_ALLOC_PCI_VALID_M);
660 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
661 /* map the interrupts to its functions */
662 for (v = first; v <= last; v++) {
663 reg = (((abs_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
664 GLINT_VECT2FUNC_VF_NUM_M) |
665 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
666 GLINT_VECT2FUNC_PF_NUM_M));
667 wr32(hw, GLINT_VECT2FUNC(v), reg);
670 /* Map mailbox interrupt. We put an explicit 0 here to remind us that
671 * VF admin queue interrupts will go to VF MSI-X vector 0.
673 wr32(hw, VPINT_MBX_CTL(abs_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M | 0);
674 /* set regardless of mapping mode */
675 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
677 /* VF Tx queues allocation */
678 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
679 /* set the VF PF Tx queue range
680 * VFNUMQ value should be set to (number of queues - 1). A value
681 * of 0 means 1 queue and a value of 255 means 256 queues
683 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
684 VPLAN_TX_QBASE_VFFIRSTQ_M) |
685 (((vsi->alloc_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
686 VPLAN_TX_QBASE_VFNUMQ_M));
687 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
689 dev_err(&pf->pdev->dev,
690 "Scattered mode for VF Tx queues is not yet implemented\n");
693 /* set regardless of mapping mode */
694 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
696 /* VF Rx queues allocation */
697 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
698 /* set the VF PF Rx queue range
699 * VFNUMQ value should be set to (number of queues - 1). A value
700 * of 0 means 1 queue and a value of 255 means 256 queues
702 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
703 VPLAN_RX_QBASE_VFFIRSTQ_M) |
704 (((vsi->alloc_txq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
705 VPLAN_RX_QBASE_VFNUMQ_M));
706 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
708 dev_err(&pf->pdev->dev,
709 "Scattered mode for VF Rx queues is not yet implemented\n");
715 * @pf: pointer to the PF structure
716 * @avail_res: available resources in the PF structure
717 * @max_res: maximum resources that can be given per VF
718 * @min_res: minimum resources that can be given per VF
720 * Returns non-zero value if resources (queues/vectors) are available or
721 * returns zero if PF cannot accommodate for all num_alloc_vfs.
724 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
726 bool checked_min_res = false;
729 /* start by checking if PF can assign max number of resources for
731 * if yes, return number per VF
732 * If no, divide by 2 and roundup, check again
733 * repeat the loop till we reach a point where even minimum resources
734 * are not available, in that case return 0
737 while ((res >= min_res) && !checked_min_res) {
740 num_all_res = pf->num_alloc_vfs * res;
741 if (num_all_res <= avail_res)
745 checked_min_res = true;
747 res = DIV_ROUND_UP(res, 2);
753 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
754 * @vf: VF to calculate the register index for
755 * @q_vector: a q_vector associated to the VF
757 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
761 if (!vf || !q_vector)
766 /* always add one to account for the OICR being the first MSIX */
767 return pf->sriov_base_vector + pf->num_vf_msix * vf->vf_id +
772 * ice_get_max_valid_res_idx - Get the max valid resource index
773 * @res: pointer to the resource to find the max valid index for
775 * Start from the end of the ice_res_tracker and return right when we find the
776 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
777 * valid for SR-IOV because it is the only consumer that manipulates the
778 * res->end and this is always called when res->end is set to res->num_entries.
780 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
787 for (i = res->num_entries - 1; i >= 0; i--)
788 if (res->list[i] & ICE_RES_VALID_BIT)
795 * ice_sriov_set_msix_res - Set any used MSIX resources
796 * @pf: pointer to PF structure
797 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
799 * This function allows SR-IOV resources to be taken from the end of the PF's
800 * allowed HW MSIX vectors so in many cases the irq_tracker will not
801 * be needed. In these cases we just set the pf->sriov_base_vector and return
804 * If SR-IOV needs to use any pf->irq_tracker entries it updates the
805 * irq_tracker->end based on the first entry needed for SR-IOV. This makes it
806 * so any calls to ice_get_res() using the irq_tracker will not try to use
807 * resources at or beyond the newly set value.
809 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors in
810 * in the PF's space available for SR-IOV.
812 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
814 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
815 u16 pf_total_msix_vectors =
816 pf->hw.func_caps.common_cap.num_msix_vectors;
817 struct ice_res_tracker *res = pf->irq_tracker;
818 int sriov_base_vector;
820 if (max_valid_res_idx < 0)
821 return max_valid_res_idx;
823 sriov_base_vector = pf_total_msix_vectors - num_msix_needed;
825 /* make sure we only grab irq_tracker entries from the list end and
826 * that we have enough available MSIX vectors
828 if (sriov_base_vector <= max_valid_res_idx)
831 pf->sriov_base_vector = sriov_base_vector;
833 /* dip into irq_tracker entries and update used resources */
834 if (num_msix_needed > (pf_total_msix_vectors - res->num_entries)) {
835 pf->num_avail_sw_msix -=
836 res->num_entries - pf->sriov_base_vector;
837 res->end = pf->sriov_base_vector;
844 * ice_check_avail_res - check if vectors and queues are available
845 * @pf: pointer to the PF structure
847 * This function is where we calculate actual number of resources for VF VSIs,
848 * we don't reserve ahead of time during probe. Returns success if vectors and
849 * queues resources are available, otherwise returns error code
851 static int ice_check_avail_res(struct ice_pf *pf)
853 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
854 u16 num_msix, num_txq, num_rxq, num_avail_msix;
856 if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
859 /* add 1 to max_valid_res_idx to account for it being 0-based */
860 num_avail_msix = pf->hw.func_caps.common_cap.num_msix_vectors -
861 (max_valid_res_idx + 1);
863 /* Grab from HW interrupts common pool
864 * Note: By the time the user decides it needs more vectors in a VF
865 * its already too late since one must decide this prior to creating the
866 * VF interface. So the best we can do is take a guess as to what the
869 * We have two policies for vector allocation:
870 * 1. if num_alloc_vfs is from 1 to 16, then we consider this as small
871 * number of NFV VFs used for NFV appliances, since this is a special
872 * case, we try to assign maximum vectors per VF (65) as much as
873 * possible, based on determine_resources algorithm.
874 * 2. if num_alloc_vfs is from 17 to 256, then its large number of
875 * regular VFs which are not used for any special purpose. Hence try to
876 * grab default interrupt vectors (5 as supported by AVF driver).
878 if (pf->num_alloc_vfs <= 16) {
879 num_msix = ice_determine_res(pf, num_avail_msix,
881 ICE_MIN_INTR_PER_VF);
882 } else if (pf->num_alloc_vfs <= ICE_MAX_VF_COUNT) {
883 num_msix = ice_determine_res(pf, num_avail_msix,
884 ICE_DFLT_INTR_PER_VF,
885 ICE_MIN_INTR_PER_VF);
887 dev_err(&pf->pdev->dev,
888 "Number of VFs %d exceeds max VF count %d\n",
889 pf->num_alloc_vfs, ICE_MAX_VF_COUNT);
896 /* Grab from the common pool
897 * start by requesting Default queues (4 as supported by AVF driver),
898 * Note that, the main difference between queues and vectors is, latter
899 * can only be reserved at init time but queues can be requested by VF
900 * at runtime through Virtchnl, that is the reason we start by reserving
903 num_txq = ice_determine_res(pf, ice_get_avail_txq_count(pf),
904 ICE_DFLT_QS_PER_VF, ICE_MIN_QS_PER_VF);
906 num_rxq = ice_determine_res(pf, ice_get_avail_rxq_count(pf),
907 ICE_DFLT_QS_PER_VF, ICE_MIN_QS_PER_VF);
909 if (!num_txq || !num_rxq)
912 if (ice_sriov_set_msix_res(pf, num_msix * pf->num_alloc_vfs))
915 /* since AVF driver works with only queue pairs which means, it expects
916 * to have equal number of Rx and Tx queues, so take the minimum of
917 * available Tx or Rx queues
919 pf->num_vf_qps = min_t(int, num_txq, num_rxq);
920 pf->num_vf_msix = num_msix;
926 * ice_cleanup_and_realloc_vf - Clean up VF and reallocate resources after reset
927 * @vf: pointer to the VF structure
929 * Cleanup a VF after the hardware reset is finished. Expects the caller to
930 * have verified whether the reset is finished properly, and ensure the
931 * minimum amount of wait time has passed. Reallocate VF resources back to make
934 static void ice_cleanup_and_realloc_vf(struct ice_vf *vf)
936 struct ice_pf *pf = vf->pf;
942 /* PF software completes the flow by notifying VF that reset flow is
943 * completed. This is done by enabling hardware by clearing the reset
944 * bit in the VPGEN_VFRTRIG reg and setting VFR_STATE in the VFGEN_RSTAT
945 * register to VFR completed (done at the end of this function)
946 * By doing this we allow HW to access VF memory at any point. If we
947 * did it any sooner, HW could access memory while it was being freed
948 * in ice_free_vf_res(), causing an IOMMU fault.
950 * On the other hand, this needs to be done ASAP, because the VF driver
951 * is waiting for this to happen and may report a timeout. It's
952 * harmless, but it gets logged into Guest OS kernel log, so best avoid
955 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
956 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
957 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
959 /* reallocate VF resources to finish resetting the VSI state */
960 if (!ice_alloc_vf_res(vf)) {
961 ice_ena_vf_mappings(vf);
962 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
963 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
967 /* Tell the VF driver the reset is done. This needs to be done only
968 * after VF has been fully initialized, because the VF driver may
969 * request resources immediately after setting this flag.
971 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
975 * ice_vf_set_vsi_promisc - set given VF VSI to given promiscuous mode(s)
976 * @vf: pointer to the VF info
977 * @vsi: the VSI being configured
978 * @promisc_m: mask of promiscuous config bits
979 * @rm_promisc: promisc flag request from the VF to remove or add filter
981 * This function configures VF VSI promiscuous mode, based on the VF requests,
982 * for Unicast, Multicast and VLAN
984 static enum ice_status
985 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m,
988 struct ice_pf *pf = vf->pf;
989 enum ice_status status = 0;
994 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
996 } else if (vf->port_vlan_id) {
998 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1001 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1005 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
1008 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
1016 * ice_config_res_vfs - Finalize allocation of VFs resources in one go
1017 * @pf: pointer to the PF structure
1019 * This function is being called as last part of resetting all VFs, or when
1020 * configuring VFs for the first time, where there is no resource to be freed
1021 * Returns true if resources were properly allocated for all VFs, and false
1024 static bool ice_config_res_vfs(struct ice_pf *pf)
1026 struct ice_hw *hw = &pf->hw;
1029 if (ice_check_avail_res(pf)) {
1030 dev_err(&pf->pdev->dev,
1031 "Cannot allocate VF resources, try with fewer number of VFs\n");
1035 /* rearm global interrupts */
1036 if (test_and_clear_bit(__ICE_OICR_INTR_DIS, pf->state))
1037 ice_irq_dynamic_ena(hw, NULL, NULL);
1039 /* Finish resetting each VF and allocate resources */
1040 for (v = 0; v < pf->num_alloc_vfs; v++) {
1041 struct ice_vf *vf = &pf->vf[v];
1043 vf->num_vf_qs = pf->num_vf_qps;
1044 dev_dbg(&pf->pdev->dev,
1045 "VF-id %d has %d queues configured\n",
1046 vf->vf_id, vf->num_vf_qs);
1047 ice_cleanup_and_realloc_vf(vf);
1051 clear_bit(__ICE_VF_DIS, pf->state);
1057 * ice_reset_all_vfs - reset all allocated VFs in one go
1058 * @pf: pointer to the PF structure
1059 * @is_vflr: true if VFLR was issued, false if not
1061 * First, tell the hardware to reset each VF, then do all the waiting in one
1062 * chunk, and finally finish restoring each VF after the wait. This is useful
1063 * during PF routines which need to reset all VFs, as otherwise it must perform
1064 * these resets in a serialized fashion.
1066 * Returns true if any VFs were reset, and false otherwise.
1068 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1070 struct ice_hw *hw = &pf->hw;
1074 /* If we don't have any VFs, then there is nothing to reset */
1075 if (!pf->num_alloc_vfs)
1078 /* If VFs have been disabled, there is no need to reset */
1079 if (test_and_set_bit(__ICE_VF_DIS, pf->state))
1082 /* Begin reset on all VFs at once */
1083 for (v = 0; v < pf->num_alloc_vfs; v++)
1084 ice_trigger_vf_reset(&pf->vf[v], is_vflr, true);
1086 for (v = 0; v < pf->num_alloc_vfs; v++) {
1087 struct ice_vsi *vsi;
1090 vsi = pf->vsi[vf->lan_vsi_idx];
1091 if (test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states))
1093 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1094 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1097 /* HW requires some time to make sure it can flush the FIFO for a VF
1098 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1099 * sequence to make sure that it has completed. We'll keep track of
1100 * the VFs using a simple iterator that increments once that VF has
1101 * finished resetting.
1103 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1105 /* Check each VF in sequence */
1106 while (v < pf->num_alloc_vfs) {
1110 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1111 if (!(reg & VPGEN_VFRSTAT_VFRD_M)) {
1112 /* only delay if the check failed */
1113 usleep_range(10, 20);
1117 /* If the current VF has finished resetting, move on
1118 * to the next VF in sequence.
1124 /* Display a warning if at least one VF didn't manage to reset in
1125 * time, but continue on with the operation.
1127 if (v < pf->num_alloc_vfs)
1128 dev_warn(&pf->pdev->dev, "VF reset check timeout\n");
1130 /* free VF resources to begin resetting the VSI state */
1131 for (v = 0; v < pf->num_alloc_vfs; v++) {
1134 ice_free_vf_res(vf);
1136 /* Free VF queues as well, and reallocate later.
1137 * If a given VF has different number of queues
1138 * configured, the request for update will come
1139 * via mailbox communication.
1144 if (ice_sriov_free_msix_res(pf))
1145 dev_err(&pf->pdev->dev,
1146 "Failed to free MSIX resources used by SR-IOV\n");
1148 if (!ice_config_res_vfs(pf))
1155 * ice_reset_vf - Reset a particular VF
1156 * @vf: pointer to the VF structure
1157 * @is_vflr: true if VFLR was issued, false if not
1159 * Returns true if the VF is reset, false otherwise.
1161 static bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1163 struct ice_pf *pf = vf->pf;
1164 struct ice_vsi *vsi;
1171 /* If the PF has been disabled, there is no need resetting VF until
1172 * PF is active again.
1174 if (test_bit(__ICE_VF_DIS, pf->state))
1177 /* If the VF has been disabled, this means something else is
1178 * resetting the VF, so we shouldn't continue. Otherwise, set
1179 * disable VF state bit for actual reset, and continue.
1181 if (test_and_set_bit(ICE_VF_STATE_DIS, vf->vf_states))
1184 ice_trigger_vf_reset(vf, is_vflr, false);
1186 vsi = pf->vsi[vf->lan_vsi_idx];
1188 if (test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states))
1191 /* Call Disable LAN Tx queue AQ whether or not queues are
1192 * enabled. This is needed for successful completion of VFR.
1194 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1195 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1198 /* poll VPGEN_VFRSTAT reg to make sure
1199 * that reset is complete
1201 for (i = 0; i < 10; i++) {
1202 /* VF reset requires driver to first reset the VF and then
1203 * poll the status register to make sure that the reset
1204 * completed successfully.
1206 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1207 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1212 /* only sleep if the reset is not done */
1213 usleep_range(10, 20);
1216 /* Display a warning if VF didn't manage to reset in time, but need to
1217 * continue on with the operation.
1220 dev_warn(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1223 /* disable promiscuous modes in case they were enabled
1224 * ignore any error if disabling process failed
1226 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1227 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1228 if (vf->port_vlan_id || vf->num_vlan)
1229 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1231 promisc_m = ICE_UCAST_PROMISC_BITS;
1233 vsi = pf->vsi[vf->lan_vsi_idx];
1234 if (ice_vf_set_vsi_promisc(vf, vsi, promisc_m, true))
1235 dev_err(&pf->pdev->dev, "disabling promiscuous mode failed\n");
1238 /* free VF resources to begin resetting the VSI state */
1239 ice_free_vf_res(vf);
1241 ice_cleanup_and_realloc_vf(vf);
1249 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1250 * @pf: pointer to the PF structure
1252 void ice_vc_notify_link_state(struct ice_pf *pf)
1256 for (i = 0; i < pf->num_alloc_vfs; i++)
1257 ice_vc_notify_vf_link_state(&pf->vf[i]);
1261 * ice_vc_notify_reset - Send pending reset message to all VFs
1262 * @pf: pointer to the PF structure
1264 * indicate a pending reset to all VFs on a given PF
1266 void ice_vc_notify_reset(struct ice_pf *pf)
1268 struct virtchnl_pf_event pfe;
1270 if (!pf->num_alloc_vfs)
1273 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1274 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1275 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1276 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1280 * ice_vc_notify_vf_reset - Notify VF of a reset event
1281 * @vf: pointer to the VF structure
1283 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1285 struct virtchnl_pf_event pfe;
1287 /* validate the request */
1288 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1291 /* verify if the VF is in either init or active before proceeding */
1292 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1293 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1296 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1297 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1298 ice_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1299 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1304 * ice_alloc_vfs - Allocate and set up VFs resources
1305 * @pf: pointer to the PF structure
1306 * @num_alloc_vfs: number of VFs to allocate
1308 static int ice_alloc_vfs(struct ice_pf *pf, u16 num_alloc_vfs)
1310 struct ice_hw *hw = &pf->hw;
1314 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1315 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1316 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1317 set_bit(__ICE_OICR_INTR_DIS, pf->state);
1320 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1322 pf->num_alloc_vfs = 0;
1323 goto err_unroll_intr;
1325 /* allocate memory */
1326 vfs = devm_kcalloc(&pf->pdev->dev, num_alloc_vfs, sizeof(*vfs),
1330 goto err_pci_disable_sriov;
1334 /* apply default profile */
1335 for (i = 0; i < num_alloc_vfs; i++) {
1337 vfs[i].vf_sw_id = pf->first_sw;
1340 /* assign default capabilities */
1341 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1342 vfs[i].spoofchk = true;
1344 pf->num_alloc_vfs = num_alloc_vfs;
1346 /* VF resources get allocated with initialization */
1347 if (!ice_config_res_vfs(pf)) {
1349 goto err_unroll_sriov;
1356 devm_kfree(&pf->pdev->dev, vfs);
1358 pf->num_alloc_vfs = 0;
1359 err_pci_disable_sriov:
1360 pci_disable_sriov(pf->pdev);
1362 /* rearm interrupts here */
1363 ice_irq_dynamic_ena(hw, NULL, NULL);
1364 clear_bit(__ICE_OICR_INTR_DIS, pf->state);
1369 * ice_pf_state_is_nominal - checks the PF for nominal state
1370 * @pf: pointer to PF to check
1372 * Check the PF's state for a collection of bits that would indicate
1373 * the PF is in a state that would inhibit normal operation for
1374 * driver functionality.
1376 * Returns true if PF is in a nominal state.
1377 * Returns false otherwise
1379 static bool ice_pf_state_is_nominal(struct ice_pf *pf)
1381 DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
1386 bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
1387 if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
1394 * ice_pci_sriov_ena - Enable or change number of VFs
1395 * @pf: pointer to the PF structure
1396 * @num_vfs: number of VFs to allocate
1398 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1400 int pre_existing_vfs = pci_num_vf(pf->pdev);
1401 struct device *dev = &pf->pdev->dev;
1404 if (!ice_pf_state_is_nominal(pf)) {
1405 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1409 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1410 dev_err(dev, "This device is not capable of SR-IOV\n");
1414 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1416 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1419 if (num_vfs > pf->num_vfs_supported) {
1420 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1421 num_vfs, pf->num_vfs_supported);
1425 dev_info(dev, "Allocating %d VFs\n", num_vfs);
1426 err = ice_alloc_vfs(pf, num_vfs);
1428 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1432 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1437 * ice_sriov_configure - Enable or change number of VFs via sysfs
1438 * @pdev: pointer to a pci_dev structure
1439 * @num_vfs: number of VFs to allocate
1441 * This function is called when the user updates the number of VFs in sysfs.
1443 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1445 struct ice_pf *pf = pci_get_drvdata(pdev);
1447 if (ice_is_safe_mode(pf)) {
1448 dev_err(&pf->pdev->dev,
1449 "SR-IOV cannot be configured - Device is in Safe Mode\n");
1454 return ice_pci_sriov_ena(pf, num_vfs);
1456 if (!pci_vfs_assigned(pdev)) {
1459 dev_err(&pf->pdev->dev,
1460 "can't free VFs because some are assigned to VMs.\n");
1468 * ice_process_vflr_event - Free VF resources via IRQ calls
1469 * @pf: pointer to the PF structure
1471 * called from the VFLR IRQ handler to
1472 * free up VF resources and state variables
1474 void ice_process_vflr_event(struct ice_pf *pf)
1476 struct ice_hw *hw = &pf->hw;
1480 if (!test_and_clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1484 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1485 struct ice_vf *vf = &pf->vf[vf_id];
1486 u32 reg_idx, bit_idx;
1488 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1489 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1490 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1491 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1492 if (reg & BIT(bit_idx))
1493 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1494 ice_reset_vf(vf, true);
1499 * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
1500 * @vf: pointer to the VF info
1502 static void ice_vc_reset_vf(struct ice_vf *vf)
1504 ice_vc_notify_vf_reset(vf);
1505 ice_reset_vf(vf, false);
1509 * ice_vc_send_msg_to_vf - Send message to VF
1510 * @vf: pointer to the VF info
1511 * @v_opcode: virtual channel opcode
1512 * @v_retval: virtual channel return value
1513 * @msg: pointer to the msg buffer
1514 * @msglen: msg length
1519 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
1520 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
1522 enum ice_status aq_ret;
1525 /* validate the request */
1526 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1531 /* single place to detect unsuccessful return values */
1533 vf->num_inval_msgs++;
1534 dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1535 vf->vf_id, v_opcode, v_retval);
1536 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
1537 dev_err(&pf->pdev->dev,
1538 "Number of invalid messages exceeded for VF %d\n",
1540 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1541 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1545 vf->num_valid_msgs++;
1546 /* reset the invalid counter, if a valid message is received. */
1547 vf->num_inval_msgs = 0;
1550 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
1552 if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
1553 dev_info(&pf->pdev->dev,
1554 "Unable to send the message to VF %d ret %d aq_err %d\n",
1555 vf->vf_id, aq_ret, pf->hw.mailboxq.sq_last_status);
1563 * ice_vc_get_ver_msg
1564 * @vf: pointer to the VF info
1565 * @msg: pointer to the msg buffer
1567 * called from the VF to request the API version used by the PF
1569 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
1571 struct virtchnl_version_info info = {
1572 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1575 vf->vf_ver = *(struct virtchnl_version_info *)msg;
1576 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1577 if (VF_IS_V10(&vf->vf_ver))
1578 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1580 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1581 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
1582 sizeof(struct virtchnl_version_info));
1586 * ice_vc_get_vf_res_msg
1587 * @vf: pointer to the VF info
1588 * @msg: pointer to the msg buffer
1590 * called from the VF to request its resources
1592 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
1594 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1595 struct virtchnl_vf_resource *vfres = NULL;
1596 struct ice_pf *pf = vf->pf;
1597 struct ice_vsi *vsi;
1601 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
1602 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1606 len = sizeof(struct virtchnl_vf_resource);
1608 vfres = devm_kzalloc(&pf->pdev->dev, len, GFP_KERNEL);
1610 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1614 if (VF_IS_V11(&vf->vf_ver))
1615 vf->driver_caps = *(u32 *)msg;
1617 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1618 VIRTCHNL_VF_OFFLOAD_RSS_REG |
1619 VIRTCHNL_VF_OFFLOAD_VLAN;
1621 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1622 vsi = pf->vsi[vf->lan_vsi_idx];
1624 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1628 if (!vsi->info.pvid)
1629 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1631 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1632 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1634 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1635 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1637 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1640 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1641 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1643 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1644 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1646 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
1647 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1649 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
1650 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1652 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1653 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1655 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1656 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1658 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1659 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
1661 vfres->num_vsis = 1;
1662 /* Tx and Rx queue are equal for VF */
1663 vfres->num_queue_pairs = vsi->num_txq;
1664 vfres->max_vectors = pf->num_vf_msix;
1665 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
1666 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
1668 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
1669 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1670 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
1671 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1672 vf->dflt_lan_addr.addr);
1674 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
1677 /* send the response back to the VF */
1678 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
1681 devm_kfree(&pf->pdev->dev, vfres);
1686 * ice_vc_reset_vf_msg
1687 * @vf: pointer to the VF info
1689 * called from the VF to reset itself,
1690 * unlike other virtchnl messages, PF driver
1691 * doesn't send the response back to the VF
1693 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
1695 if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1696 ice_reset_vf(vf, false);
1700 * ice_find_vsi_from_id
1701 * @pf: the PF structure to search for the VSI
1702 * @id: ID of the VSI it is searching for
1704 * searches for the VSI with the given ID
1706 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
1710 ice_for_each_vsi(pf, i)
1711 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
1718 * ice_vc_isvalid_vsi_id
1719 * @vf: pointer to the VF info
1720 * @vsi_id: VF relative VSI ID
1722 * check for the valid VSI ID
1724 static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
1726 struct ice_pf *pf = vf->pf;
1727 struct ice_vsi *vsi;
1729 vsi = ice_find_vsi_from_id(pf, vsi_id);
1731 return (vsi && (vsi->vf_id == vf->vf_id));
1735 * ice_vc_isvalid_q_id
1736 * @vf: pointer to the VF info
1738 * @qid: VSI relative queue ID
1740 * check for the valid queue ID
1742 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
1744 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
1745 /* allocated Tx and Rx queues should be always equal for VF VSI */
1746 return (vsi && (qid < vsi->alloc_txq));
1750 * ice_vc_isvalid_ring_len
1751 * @ring_len: length of ring
1753 * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
1756 static bool ice_vc_isvalid_ring_len(u16 ring_len)
1758 return ring_len == 0 ||
1759 (ring_len >= ICE_MIN_NUM_DESC &&
1760 ring_len <= ICE_MAX_NUM_DESC &&
1761 !(ring_len % ICE_REQ_DESC_MULTIPLE));
1765 * ice_vc_config_rss_key
1766 * @vf: pointer to the VF info
1767 * @msg: pointer to the msg buffer
1769 * Configure the VF's RSS key
1771 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
1773 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1774 struct virtchnl_rss_key *vrk =
1775 (struct virtchnl_rss_key *)msg;
1776 struct ice_pf *pf = vf->pf;
1777 struct ice_vsi *vsi = NULL;
1779 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1780 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1784 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
1785 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1789 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
1790 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1794 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1795 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1799 vsi = pf->vsi[vf->lan_vsi_idx];
1801 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1805 if (ice_set_rss(vsi, vrk->key, NULL, 0))
1806 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1808 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
1813 * ice_vc_config_rss_lut
1814 * @vf: pointer to the VF info
1815 * @msg: pointer to the msg buffer
1817 * Configure the VF's RSS LUT
1819 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
1821 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
1822 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1823 struct ice_pf *pf = vf->pf;
1824 struct ice_vsi *vsi = NULL;
1826 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1827 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1831 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
1832 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1836 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
1837 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1841 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1842 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1846 vsi = pf->vsi[vf->lan_vsi_idx];
1848 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1852 if (ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
1853 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1855 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
1860 * ice_vc_get_stats_msg
1861 * @vf: pointer to the VF info
1862 * @msg: pointer to the msg buffer
1864 * called from the VF to get VSI stats
1866 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
1868 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1869 struct virtchnl_queue_select *vqs =
1870 (struct virtchnl_queue_select *)msg;
1871 struct ice_pf *pf = vf->pf;
1872 struct ice_eth_stats stats;
1873 struct ice_vsi *vsi;
1875 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1876 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1880 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1881 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1885 vsi = pf->vsi[vf->lan_vsi_idx];
1887 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1891 memset(&stats, 0, sizeof(struct ice_eth_stats));
1892 ice_update_eth_stats(vsi);
1894 stats = vsi->eth_stats;
1897 /* send the response to the VF */
1898 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
1899 (u8 *)&stats, sizeof(stats));
1904 * @vf: pointer to the VF info
1905 * @msg: pointer to the msg buffer
1907 * called from the VF to enable all or specific queue(s)
1909 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
1911 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1912 struct virtchnl_queue_select *vqs =
1913 (struct virtchnl_queue_select *)msg;
1914 struct ice_pf *pf = vf->pf;
1915 struct ice_vsi *vsi;
1916 unsigned long q_map;
1919 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1920 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1924 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1925 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1929 if (!vqs->rx_queues && !vqs->tx_queues) {
1930 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1934 if (vqs->rx_queues > ICE_MAX_BASE_QS_PER_VF ||
1935 vqs->tx_queues > ICE_MAX_BASE_QS_PER_VF) {
1936 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1940 vsi = pf->vsi[vf->lan_vsi_idx];
1942 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1946 /* Enable only Rx rings, Tx rings were enabled by the FW when the
1947 * Tx queue group list was configured and the context bits were
1948 * programmed using ice_vsi_cfg_txqs
1950 q_map = vqs->rx_queues;
1951 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_BASE_QS_PER_VF) {
1952 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1953 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1957 /* Skip queue if enabled */
1958 if (test_bit(vf_q_id, vf->rxq_ena))
1961 if (ice_vsi_ctrl_rx_ring(vsi, true, vf_q_id)) {
1962 dev_err(&vsi->back->pdev->dev,
1963 "Failed to enable Rx ring %d on VSI %d\n",
1964 vf_q_id, vsi->vsi_num);
1965 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1969 set_bit(vf_q_id, vf->rxq_ena);
1973 vsi = pf->vsi[vf->lan_vsi_idx];
1974 q_map = vqs->tx_queues;
1975 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_BASE_QS_PER_VF) {
1976 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1977 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1981 /* Skip queue if enabled */
1982 if (test_bit(vf_q_id, vf->txq_ena))
1985 set_bit(vf_q_id, vf->txq_ena);
1989 /* Set flag to indicate that queues are enabled */
1990 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1991 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1994 /* send the response to the VF */
1995 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
2001 * @vf: pointer to the VF info
2002 * @msg: pointer to the msg buffer
2004 * called from the VF to disable all or specific
2007 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
2009 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2010 struct virtchnl_queue_select *vqs =
2011 (struct virtchnl_queue_select *)msg;
2012 struct ice_pf *pf = vf->pf;
2013 struct ice_vsi *vsi;
2014 unsigned long q_map;
2017 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
2018 !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
2019 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2023 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2024 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2028 if (!vqs->rx_queues && !vqs->tx_queues) {
2029 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2033 if (vqs->rx_queues > ICE_MAX_BASE_QS_PER_VF ||
2034 vqs->tx_queues > ICE_MAX_BASE_QS_PER_VF) {
2035 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2039 vsi = pf->vsi[vf->lan_vsi_idx];
2041 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2045 if (vqs->tx_queues) {
2046 q_map = vqs->tx_queues;
2048 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_BASE_QS_PER_VF) {
2049 struct ice_ring *ring = vsi->tx_rings[vf_q_id];
2050 struct ice_txq_meta txq_meta = { 0 };
2052 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2053 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2057 /* Skip queue if not enabled */
2058 if (!test_bit(vf_q_id, vf->txq_ena))
2061 ice_fill_txq_meta(vsi, ring, &txq_meta);
2063 if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
2065 dev_err(&vsi->back->pdev->dev,
2066 "Failed to stop Tx ring %d on VSI %d\n",
2067 vf_q_id, vsi->vsi_num);
2068 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2072 /* Clear enabled queues flag */
2073 clear_bit(vf_q_id, vf->txq_ena);
2078 if (vqs->rx_queues) {
2079 q_map = vqs->rx_queues;
2081 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_BASE_QS_PER_VF) {
2082 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2083 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2087 /* Skip queue if not enabled */
2088 if (!test_bit(vf_q_id, vf->rxq_ena))
2091 if (ice_vsi_ctrl_rx_ring(vsi, false, vf_q_id)) {
2092 dev_err(&vsi->back->pdev->dev,
2093 "Failed to stop Rx ring %d on VSI %d\n",
2094 vf_q_id, vsi->vsi_num);
2095 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2099 /* Clear enabled queues flag */
2100 clear_bit(vf_q_id, vf->rxq_ena);
2105 /* Clear enabled queues flag */
2106 if (v_ret == VIRTCHNL_STATUS_SUCCESS && !vf->num_qs_ena)
2107 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
2110 /* send the response to the VF */
2111 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
2116 * ice_vc_cfg_irq_map_msg
2117 * @vf: pointer to the VF info
2118 * @msg: pointer to the msg buffer
2120 * called from the VF to configure the IRQ to queue map
2122 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
2124 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2125 struct virtchnl_irq_map_info *irqmap_info;
2126 u16 vsi_id, vsi_q_id, vector_id;
2127 struct virtchnl_vector_map *map;
2128 struct ice_pf *pf = vf->pf;
2129 u16 num_q_vectors_mapped;
2130 struct ice_vsi *vsi;
2134 irqmap_info = (struct virtchnl_irq_map_info *)msg;
2135 num_q_vectors_mapped = irqmap_info->num_vectors;
2137 /* Check to make sure number of VF vectors mapped is not greater than
2138 * number of VF vectors originally allocated, and check that
2139 * there is actually at least a single VF queue vector mapped
2141 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2142 pf->num_vf_msix < num_q_vectors_mapped ||
2143 !irqmap_info->num_vectors) {
2144 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2148 vsi = pf->vsi[vf->lan_vsi_idx];
2150 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2154 for (i = 0; i < num_q_vectors_mapped; i++) {
2155 struct ice_q_vector *q_vector;
2157 map = &irqmap_info->vecmap[i];
2159 vector_id = map->vector_id;
2160 vsi_id = map->vsi_id;
2161 /* validate msg params */
2162 if (!(vector_id < pf->hw.func_caps.common_cap
2163 .num_msix_vectors) || !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
2164 (!vector_id && (map->rxq_map || map->txq_map))) {
2165 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2169 /* No need to map VF miscellaneous or rogue vector */
2173 /* Subtract non queue vector from vector_id passed by VF
2174 * to get actual number of VSI queue vector array index
2176 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
2178 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2182 /* lookout for the invalid queue index */
2183 qmap = map->rxq_map;
2184 q_vector->num_ring_rx = 0;
2185 for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
2186 if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
2187 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2190 q_vector->num_ring_rx++;
2191 q_vector->rx.itr_idx = map->rxitr_idx;
2192 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
2193 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
2194 q_vector->rx.itr_idx);
2197 qmap = map->txq_map;
2198 q_vector->num_ring_tx = 0;
2199 for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
2200 if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
2201 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2204 q_vector->num_ring_tx++;
2205 q_vector->tx.itr_idx = map->txitr_idx;
2206 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
2207 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
2208 q_vector->tx.itr_idx);
2213 /* send the response to the VF */
2214 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
2220 * @vf: pointer to the VF info
2221 * @msg: pointer to the msg buffer
2223 * called from the VF to configure the Rx/Tx queues
2225 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
2227 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2228 struct virtchnl_vsi_queue_config_info *qci =
2229 (struct virtchnl_vsi_queue_config_info *)msg;
2230 struct virtchnl_queue_pair_info *qpi;
2231 u16 num_rxq = 0, num_txq = 0;
2232 struct ice_pf *pf = vf->pf;
2233 struct ice_vsi *vsi;
2236 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2237 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2241 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
2242 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2246 vsi = pf->vsi[vf->lan_vsi_idx];
2248 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2252 if (qci->num_queue_pairs > ICE_MAX_BASE_QS_PER_VF ||
2253 qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
2254 dev_err(&pf->pdev->dev,
2255 "VF-%d requesting more than supported number of queues: %d\n",
2256 vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
2257 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2261 for (i = 0; i < qci->num_queue_pairs; i++) {
2262 qpi = &qci->qpair[i];
2263 if (qpi->txq.vsi_id != qci->vsi_id ||
2264 qpi->rxq.vsi_id != qci->vsi_id ||
2265 qpi->rxq.queue_id != qpi->txq.queue_id ||
2266 qpi->txq.headwb_enabled ||
2267 !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
2268 !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
2269 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
2270 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2273 /* copy Tx queue info from VF into VSI */
2274 if (qpi->txq.ring_len > 0) {
2276 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
2277 vsi->tx_rings[i]->count = qpi->txq.ring_len;
2280 /* copy Rx queue info from VF into VSI */
2281 if (qpi->rxq.ring_len > 0) {
2283 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
2284 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
2286 if (qpi->rxq.databuffer_size != 0 &&
2287 (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
2288 qpi->rxq.databuffer_size < 1024)) {
2289 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2292 vsi->rx_buf_len = qpi->rxq.databuffer_size;
2293 vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
2294 if (qpi->rxq.max_pkt_size >= (16 * 1024) ||
2295 qpi->rxq.max_pkt_size < 64) {
2296 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2301 vsi->max_frame = qpi->rxq.max_pkt_size;
2304 /* VF can request to configure less than allocated queues
2305 * or default allocated queues. So update the VSI with new number
2307 vsi->num_txq = num_txq;
2308 vsi->num_rxq = num_rxq;
2309 /* All queues of VF VSI are in TC 0 */
2310 vsi->tc_cfg.tc_info[0].qcount_tx = num_txq;
2311 vsi->tc_cfg.tc_info[0].qcount_rx = num_rxq;
2313 if (ice_vsi_cfg_lan_txqs(vsi) || ice_vsi_cfg_rxqs(vsi))
2314 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2317 /* send the response to the VF */
2318 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
2324 * @vf: pointer to the VF info
2326 static bool ice_is_vf_trusted(struct ice_vf *vf)
2328 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
2332 * ice_can_vf_change_mac
2333 * @vf: pointer to the VF info
2335 * Return true if the VF is allowed to change its MAC filters, false otherwise
2337 static bool ice_can_vf_change_mac(struct ice_vf *vf)
2339 /* If the VF MAC address has been set administratively (via the
2340 * ndo_set_vf_mac command), then deny permission to the VF to
2341 * add/delete unicast MAC addresses, unless the VF is trusted
2343 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
2350 * ice_vc_handle_mac_addr_msg
2351 * @vf: pointer to the VF info
2352 * @msg: pointer to the msg buffer
2353 * @set: true if MAC filters are being set, false otherwise
2355 * add guest MAC address filter
2358 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
2360 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2361 struct virtchnl_ether_addr_list *al =
2362 (struct virtchnl_ether_addr_list *)msg;
2363 struct ice_pf *pf = vf->pf;
2364 enum virtchnl_ops vc_op;
2365 enum ice_status status;
2366 struct ice_vsi *vsi;
2371 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2373 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2375 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2376 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2377 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2378 goto handle_mac_exit;
2381 if (set && !ice_is_vf_trusted(vf) &&
2382 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2383 dev_err(&pf->pdev->dev,
2384 "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",
2386 /* There is no need to let VF know about not being trusted
2387 * to add more MAC addr, so we can just return success message.
2389 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2390 goto handle_mac_exit;
2393 vsi = pf->vsi[vf->lan_vsi_idx];
2395 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2396 goto handle_mac_exit;
2399 for (i = 0; i < al->num_elements; i++) {
2400 u8 *maddr = al->list[i].addr;
2402 if (ether_addr_equal(maddr, vf->dflt_lan_addr.addr) ||
2403 is_broadcast_ether_addr(maddr)) {
2405 /* VF is trying to add filters that the PF
2406 * already added. Just continue.
2408 dev_info(&pf->pdev->dev,
2409 "MAC %pM already set for VF %d\n",
2413 /* VF can't remove dflt_lan_addr/bcast MAC */
2414 dev_err(&pf->pdev->dev,
2415 "VF can't remove default MAC address or MAC %pM programmed by PF for VF %d\n",
2421 /* check for the invalid cases and bail if necessary */
2422 if (is_zero_ether_addr(maddr)) {
2423 dev_err(&pf->pdev->dev,
2424 "invalid MAC %pM provided for VF %d\n",
2426 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2427 goto handle_mac_exit;
2430 if (is_unicast_ether_addr(maddr) &&
2431 !ice_can_vf_change_mac(vf)) {
2432 dev_err(&pf->pdev->dev,
2433 "can't change unicast MAC for untrusted VF %d\n",
2435 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2436 goto handle_mac_exit;
2439 /* program the updated filter list */
2440 status = ice_vsi_cfg_mac_fltr(vsi, maddr, set);
2441 if (status == ICE_ERR_DOES_NOT_EXIST ||
2442 status == ICE_ERR_ALREADY_EXISTS) {
2443 dev_info(&pf->pdev->dev,
2444 "can't %s MAC filters %pM for VF %d, error %d\n",
2445 set ? "add" : "remove", maddr, vf->vf_id,
2447 } else if (status) {
2448 dev_err(&pf->pdev->dev,
2449 "can't %s MAC filters for VF %d, error %d\n",
2450 set ? "add" : "remove", vf->vf_id, status);
2451 v_ret = ice_err_to_virt_err(status);
2452 goto handle_mac_exit;
2458 /* Track number of MAC filters programmed for the VF VSI */
2460 vf->num_mac += mac_count;
2462 vf->num_mac -= mac_count;
2465 /* send the response to the VF */
2466 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2470 * ice_vc_add_mac_addr_msg
2471 * @vf: pointer to the VF info
2472 * @msg: pointer to the msg buffer
2474 * add guest MAC address filter
2476 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2478 return ice_vc_handle_mac_addr_msg(vf, msg, true);
2482 * ice_vc_del_mac_addr_msg
2483 * @vf: pointer to the VF info
2484 * @msg: pointer to the msg buffer
2486 * remove guest MAC address filter
2488 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2490 return ice_vc_handle_mac_addr_msg(vf, msg, false);
2494 * ice_vc_request_qs_msg
2495 * @vf: pointer to the VF info
2496 * @msg: pointer to the msg buffer
2498 * VFs get a default number of queues but can use this message to request a
2499 * different number. If the request is successful, PF will reset the VF and
2500 * return 0. If unsuccessful, PF will send message informing VF of number of
2501 * available queue pairs via virtchnl message response to VF.
2503 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2505 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2506 struct virtchnl_vf_res_request *vfres =
2507 (struct virtchnl_vf_res_request *)msg;
2508 u16 req_queues = vfres->num_queue_pairs;
2509 struct ice_pf *pf = vf->pf;
2510 u16 max_allowed_vf_queues;
2511 u16 tx_rx_queue_left;
2514 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2515 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2519 cur_queues = vf->num_vf_qs;
2520 tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
2521 ice_get_avail_rxq_count(pf));
2522 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2524 dev_err(&pf->pdev->dev,
2525 "VF %d tried to request 0 queues. Ignoring.\n",
2527 } else if (req_queues > ICE_MAX_BASE_QS_PER_VF) {
2528 dev_err(&pf->pdev->dev,
2529 "VF %d tried to request more than %d queues.\n",
2530 vf->vf_id, ICE_MAX_BASE_QS_PER_VF);
2531 vfres->num_queue_pairs = ICE_MAX_BASE_QS_PER_VF;
2532 } else if (req_queues > cur_queues &&
2533 req_queues - cur_queues > tx_rx_queue_left) {
2534 dev_warn(&pf->pdev->dev,
2535 "VF %d requested %u more queues, but only %u left.\n",
2536 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2537 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
2538 ICE_MAX_BASE_QS_PER_VF);
2540 /* request is successful, then reset VF */
2541 vf->num_req_qs = req_queues;
2542 ice_vc_reset_vf(vf);
2543 dev_info(&pf->pdev->dev,
2544 "VF %d granted request of %u queues.\n",
2545 vf->vf_id, req_queues);
2550 /* send the response to the VF */
2551 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2552 v_ret, (u8 *)vfres, sizeof(*vfres));
2556 * ice_set_vf_port_vlan
2557 * @netdev: network interface device structure
2558 * @vf_id: VF identifier
2559 * @vlan_id: VLAN ID being set
2560 * @qos: priority setting
2561 * @vlan_proto: VLAN protocol
2563 * program VF Port VLAN ID and/or QoS
2566 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
2569 u16 vlanprio = vlan_id | (qos << ICE_VLAN_PRIORITY_S);
2570 struct ice_netdev_priv *np = netdev_priv(netdev);
2571 struct ice_pf *pf = np->vsi->back;
2572 struct ice_vsi *vsi;
2576 /* validate the request */
2577 if (vf_id >= pf->num_alloc_vfs) {
2578 dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
2582 if (vlan_id > ICE_MAX_VLANID || qos > 7) {
2583 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2587 if (vlan_proto != htons(ETH_P_8021Q)) {
2588 dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
2589 return -EPROTONOSUPPORT;
2592 vf = &pf->vf[vf_id];
2593 vsi = pf->vsi[vf->lan_vsi_idx];
2594 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2595 dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
2599 if (le16_to_cpu(vsi->info.pvid) == vlanprio) {
2600 /* duplicate request, so just return success */
2601 dev_info(&pf->pdev->dev,
2602 "Duplicate pvid %d request\n", vlanprio);
2606 /* If PVID, then remove all filters on the old VLAN */
2608 ice_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2611 if (vlan_id || qos) {
2612 ret = ice_vsi_manage_pvid(vsi, vlanprio, true);
2614 goto error_set_pvid;
2616 ice_vsi_manage_pvid(vsi, 0, false);
2621 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2622 vlan_id, qos, vf_id);
2624 /* add new VLAN filter for each MAC */
2625 ret = ice_vsi_add_vlan(vsi, vlan_id);
2627 goto error_set_pvid;
2630 /* The Port VLAN needs to be saved across resets the same as the
2631 * default LAN MAC address.
2633 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
2640 * ice_vc_process_vlan_msg
2641 * @vf: pointer to the VF info
2642 * @msg: pointer to the msg buffer
2643 * @add_v: Add VLAN if true, otherwise delete VLAN
2645 * Process virtchnl op to add or remove programmed guest VLAN ID
2647 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2649 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2650 struct virtchnl_vlan_filter_list *vfl =
2651 (struct virtchnl_vlan_filter_list *)msg;
2652 struct ice_pf *pf = vf->pf;
2653 bool vlan_promisc = false;
2654 struct ice_vsi *vsi;
2660 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2661 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2665 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2666 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2670 if (add_v && !ice_is_vf_trusted(vf) &&
2671 vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
2672 dev_info(&pf->pdev->dev,
2673 "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2675 /* There is no need to let VF know about being not trusted,
2676 * so we can just return success message here
2681 for (i = 0; i < vfl->num_elements; i++) {
2682 if (vfl->vlan_id[i] > ICE_MAX_VLANID) {
2683 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2684 dev_err(&pf->pdev->dev,
2685 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2691 vsi = pf->vsi[vf->lan_vsi_idx];
2693 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2697 if (vsi->info.pvid) {
2698 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2702 if (ice_vsi_manage_vlan_stripping(vsi, add_v)) {
2703 dev_err(&pf->pdev->dev,
2704 "%sable VLAN stripping failed for VSI %i\n",
2705 add_v ? "en" : "dis", vsi->vsi_num);
2706 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2710 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2711 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2712 vlan_promisc = true;
2715 for (i = 0; i < vfl->num_elements; i++) {
2716 u16 vid = vfl->vlan_id[i];
2718 if (!ice_is_vf_trusted(vf) &&
2719 vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
2720 dev_info(&pf->pdev->dev,
2721 "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2723 /* There is no need to let VF know about being
2724 * not trusted, so we can just return success
2725 * message here as well.
2730 if (ice_vsi_add_vlan(vsi, vid)) {
2731 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2736 /* Enable VLAN pruning when VLAN is added */
2737 if (!vlan_promisc) {
2738 status = ice_cfg_vlan_pruning(vsi, true, false);
2740 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2741 dev_err(&pf->pdev->dev,
2742 "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2747 /* Enable Ucast/Mcast VLAN promiscuous mode */
2748 promisc_m = ICE_PROMISC_VLAN_TX |
2749 ICE_PROMISC_VLAN_RX;
2751 status = ice_set_vsi_promisc(hw, vsi->idx,
2754 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2755 dev_err(&pf->pdev->dev,
2756 "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2762 /* In case of non_trusted VF, number of VLAN elements passed
2763 * to PF for removal might be greater than number of VLANs
2764 * filter programmed for that VF - So, use actual number of
2765 * VLANS added earlier with add VLAN opcode. In order to avoid
2766 * removing VLAN that doesn't exist, which result to sending
2767 * erroneous failed message back to the VF
2771 num_vf_vlan = vf->num_vlan;
2772 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
2773 u16 vid = vfl->vlan_id[i];
2775 /* Make sure ice_vsi_kill_vlan is successful before
2776 * updating VLAN information
2778 if (ice_vsi_kill_vlan(vsi, vid)) {
2779 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2784 /* Disable VLAN pruning when the last VLAN is removed */
2786 ice_cfg_vlan_pruning(vsi, false, false);
2788 /* Disable Unicast/Multicast VLAN promiscuous mode */
2790 promisc_m = ICE_PROMISC_VLAN_TX |
2791 ICE_PROMISC_VLAN_RX;
2793 ice_clear_vsi_promisc(hw, vsi->idx,
2800 /* send the response to the VF */
2802 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
2805 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
2810 * ice_vc_add_vlan_msg
2811 * @vf: pointer to the VF info
2812 * @msg: pointer to the msg buffer
2814 * Add and program guest VLAN ID
2816 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2818 return ice_vc_process_vlan_msg(vf, msg, true);
2822 * ice_vc_remove_vlan_msg
2823 * @vf: pointer to the VF info
2824 * @msg: pointer to the msg buffer
2826 * remove programmed guest VLAN ID
2828 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2830 return ice_vc_process_vlan_msg(vf, msg, false);
2834 * ice_vc_ena_vlan_stripping
2835 * @vf: pointer to the VF info
2837 * Enable VLAN header stripping for a given VF
2839 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2841 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2842 struct ice_pf *pf = vf->pf;
2843 struct ice_vsi *vsi;
2845 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2846 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2850 vsi = pf->vsi[vf->lan_vsi_idx];
2851 if (ice_vsi_manage_vlan_stripping(vsi, true))
2852 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2855 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2860 * ice_vc_dis_vlan_stripping
2861 * @vf: pointer to the VF info
2863 * Disable VLAN header stripping for a given VF
2865 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2867 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2868 struct ice_pf *pf = vf->pf;
2869 struct ice_vsi *vsi;
2871 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2872 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2876 vsi = pf->vsi[vf->lan_vsi_idx];
2878 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2882 if (ice_vsi_manage_vlan_stripping(vsi, false))
2883 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2886 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2891 * ice_vc_process_vf_msg - Process request from VF
2892 * @pf: pointer to the PF structure
2893 * @event: pointer to the AQ event
2895 * called from the common asq/arq handler to
2896 * process request from VF
2898 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
2900 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
2901 s16 vf_id = le16_to_cpu(event->desc.retval);
2902 u16 msglen = event->msg_len;
2903 u8 *msg = event->msg_buf;
2904 struct ice_vf *vf = NULL;
2907 if (vf_id >= pf->num_alloc_vfs) {
2912 vf = &pf->vf[vf_id];
2914 /* Check if VF is disabled. */
2915 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
2920 /* Perform basic checks on the msg */
2921 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
2923 if (err == VIRTCHNL_STATUS_ERR_PARAM)
2931 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
2933 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
2934 vf_id, v_opcode, msglen, err);
2939 case VIRTCHNL_OP_VERSION:
2940 err = ice_vc_get_ver_msg(vf, msg);
2942 case VIRTCHNL_OP_GET_VF_RESOURCES:
2943 err = ice_vc_get_vf_res_msg(vf, msg);
2944 ice_vc_notify_vf_link_state(vf);
2946 case VIRTCHNL_OP_RESET_VF:
2947 ice_vc_reset_vf_msg(vf);
2949 case VIRTCHNL_OP_ADD_ETH_ADDR:
2950 err = ice_vc_add_mac_addr_msg(vf, msg);
2952 case VIRTCHNL_OP_DEL_ETH_ADDR:
2953 err = ice_vc_del_mac_addr_msg(vf, msg);
2955 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2956 err = ice_vc_cfg_qs_msg(vf, msg);
2958 case VIRTCHNL_OP_ENABLE_QUEUES:
2959 err = ice_vc_ena_qs_msg(vf, msg);
2960 ice_vc_notify_vf_link_state(vf);
2962 case VIRTCHNL_OP_DISABLE_QUEUES:
2963 err = ice_vc_dis_qs_msg(vf, msg);
2965 case VIRTCHNL_OP_REQUEST_QUEUES:
2966 err = ice_vc_request_qs_msg(vf, msg);
2968 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
2969 err = ice_vc_cfg_irq_map_msg(vf, msg);
2971 case VIRTCHNL_OP_CONFIG_RSS_KEY:
2972 err = ice_vc_config_rss_key(vf, msg);
2974 case VIRTCHNL_OP_CONFIG_RSS_LUT:
2975 err = ice_vc_config_rss_lut(vf, msg);
2977 case VIRTCHNL_OP_GET_STATS:
2978 err = ice_vc_get_stats_msg(vf, msg);
2980 case VIRTCHNL_OP_ADD_VLAN:
2981 err = ice_vc_add_vlan_msg(vf, msg);
2983 case VIRTCHNL_OP_DEL_VLAN:
2984 err = ice_vc_remove_vlan_msg(vf, msg);
2986 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
2987 err = ice_vc_ena_vlan_stripping(vf);
2989 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
2990 err = ice_vc_dis_vlan_stripping(vf);
2992 case VIRTCHNL_OP_UNKNOWN:
2994 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
2996 err = ice_vc_send_msg_to_vf(vf, v_opcode,
2997 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
3002 /* Helper function cares less about error return values here
3003 * as it is busy with pending work.
3005 dev_info(&pf->pdev->dev,
3006 "PF failed to honor VF %d, opcode %d, error %d\n",
3007 vf_id, v_opcode, err);
3013 * @netdev: network interface device structure
3014 * @vf_id: VF identifier
3015 * @ivi: VF configuration structure
3017 * return VF configuration
3020 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
3022 struct ice_netdev_priv *np = netdev_priv(netdev);
3023 struct ice_vsi *vsi = np->vsi;
3024 struct ice_pf *pf = vsi->back;
3027 /* validate the request */
3028 if (vf_id >= pf->num_alloc_vfs) {
3029 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
3033 vf = &pf->vf[vf_id];
3034 vsi = pf->vsi[vf->lan_vsi_idx];
3036 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
3037 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
3042 ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
3044 /* VF configuration for VLAN and applicable QoS */
3045 ivi->vlan = le16_to_cpu(vsi->info.pvid) & ICE_VLAN_M;
3046 ivi->qos = (le16_to_cpu(vsi->info.pvid) & ICE_PRIORITY_M) >>
3047 ICE_VLAN_PRIORITY_S;
3049 ivi->trusted = vf->trusted;
3050 ivi->spoofchk = vf->spoofchk;
3051 if (!vf->link_forced)
3052 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
3053 else if (vf->link_up)
3054 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
3056 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
3057 ivi->max_tx_rate = vf->tx_rate;
3058 ivi->min_tx_rate = 0;
3063 * ice_set_vf_spoofchk
3064 * @netdev: network interface device structure
3065 * @vf_id: VF identifier
3066 * @ena: flag to enable or disable feature
3068 * Enable or disable VF spoof checking
3070 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
3072 struct ice_netdev_priv *np = netdev_priv(netdev);
3073 struct ice_vsi *vsi = np->vsi;
3074 struct ice_pf *pf = vsi->back;
3075 struct ice_vsi_ctx *ctx;
3076 enum ice_status status;
3080 /* validate the request */
3081 if (vf_id >= pf->num_alloc_vfs) {
3082 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
3086 vf = &pf->vf[vf_id];
3087 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
3088 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
3092 if (ena == vf->spoofchk) {
3093 dev_dbg(&pf->pdev->dev, "VF spoofchk already %s\n",
3094 ena ? "ON" : "OFF");
3098 ctx = devm_kzalloc(&pf->pdev->dev, sizeof(*ctx), GFP_KERNEL);
3102 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
3105 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
3106 ctx->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_PRUNE_EN_M;
3109 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3111 dev_dbg(&pf->pdev->dev,
3112 "Error %d, failed to update VSI* parameters\n", status);
3118 vsi->info.sec_flags = ctx->info.sec_flags;
3119 vsi->info.sw_flags2 = ctx->info.sw_flags2;
3121 devm_kfree(&pf->pdev->dev, ctx);
3127 * @netdev: network interface device structure
3128 * @vf_id: VF identifier
3131 * program VF MAC address
3133 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
3135 struct ice_netdev_priv *np = netdev_priv(netdev);
3136 struct ice_vsi *vsi = np->vsi;
3137 struct ice_pf *pf = vsi->back;
3141 /* validate the request */
3142 if (vf_id >= pf->num_alloc_vfs) {
3143 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
3147 vf = &pf->vf[vf_id];
3148 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
3149 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
3153 if (is_zero_ether_addr(mac) || is_multicast_ether_addr(mac)) {
3154 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
3158 /* copy MAC into dflt_lan_addr and trigger a VF reset. The reset
3159 * flow will use the updated dflt_lan_addr and add a MAC filter
3160 * using ice_add_mac. Also set pf_set_mac to indicate that the PF has
3161 * set the MAC address for this VF.
3163 ether_addr_copy(vf->dflt_lan_addr.addr, mac);
3164 vf->pf_set_mac = true;
3166 "MAC on VF %d set to %pM. VF driver will be reinitialized\n",
3169 ice_vc_reset_vf(vf);
3175 * @netdev: network interface device structure
3176 * @vf_id: VF identifier
3177 * @trusted: Boolean value to enable/disable trusted VF
3179 * Enable or disable a given VF as trusted
3181 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
3183 struct ice_netdev_priv *np = netdev_priv(netdev);
3184 struct ice_vsi *vsi = np->vsi;
3185 struct ice_pf *pf = vsi->back;
3188 /* validate the request */
3189 if (vf_id >= pf->num_alloc_vfs) {
3190 dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
3194 vf = &pf->vf[vf_id];
3195 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
3196 dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
3200 /* Check if already trusted */
3201 if (trusted == vf->trusted)
3204 vf->trusted = trusted;
3205 ice_vc_reset_vf(vf);
3206 dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
3207 vf_id, trusted ? "" : "un");
3213 * ice_set_vf_link_state
3214 * @netdev: network interface device structure
3215 * @vf_id: VF identifier
3216 * @link_state: required link state
3218 * Set VF's link state, irrespective of physical link state status
3220 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
3222 struct ice_netdev_priv *np = netdev_priv(netdev);
3223 struct ice_pf *pf = np->vsi->back;
3224 struct virtchnl_pf_event pfe = { 0 };
3225 struct ice_link_status *ls;
3229 if (vf_id >= pf->num_alloc_vfs) {
3230 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3234 vf = &pf->vf[vf_id];
3236 ls = &pf->hw.port_info->phy.link_info;
3238 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
3239 dev_err(&pf->pdev->dev, "vf %d in reset. Try again.\n", vf_id);
3243 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
3244 pfe.severity = PF_EVENT_SEVERITY_INFO;
3246 switch (link_state) {
3247 case IFLA_VF_LINK_STATE_AUTO:
3248 vf->link_forced = false;
3249 vf->link_up = ls->link_info & ICE_AQ_LINK_UP;
3251 case IFLA_VF_LINK_STATE_ENABLE:
3252 vf->link_forced = true;
3255 case IFLA_VF_LINK_STATE_DISABLE:
3256 vf->link_forced = true;
3257 vf->link_up = false;
3263 if (vf->link_forced)
3264 ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
3266 ice_set_pfe_link(vf, &pfe, ls->link_speed, vf->link_up);
3268 /* Notify the VF of its new link state */
3269 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
3270 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,