1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
8 * ice_err_to_virt err - translate errors for VF return code
9 * @ice_err: error return code
11 static enum virtchnl_status_code ice_err_to_virt_err(enum ice_status ice_err)
15 return VIRTCHNL_STATUS_SUCCESS;
17 case ICE_ERR_INVAL_SIZE:
18 case ICE_ERR_DEVICE_NOT_SUPPORTED:
21 return VIRTCHNL_STATUS_ERR_PARAM;
22 case ICE_ERR_NO_MEMORY:
23 return VIRTCHNL_STATUS_ERR_NO_MEMORY;
24 case ICE_ERR_NOT_READY:
25 case ICE_ERR_RESET_FAILED:
26 case ICE_ERR_FW_API_VER:
27 case ICE_ERR_AQ_ERROR:
28 case ICE_ERR_AQ_TIMEOUT:
30 case ICE_ERR_AQ_NO_WORK:
31 case ICE_ERR_AQ_EMPTY:
32 return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
34 return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
39 * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
40 * @pf: pointer to the PF structure
41 * @v_opcode: operation code
42 * @v_retval: return value
43 * @msg: pointer to the msg buffer
47 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
48 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
50 struct ice_hw *hw = &pf->hw;
51 struct ice_vf *vf = pf->vf;
54 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
55 /* Not all vfs are enabled so skip the ones that are not */
56 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
57 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
60 /* Ignore return value on purpose - a given VF may fail, but
61 * we need to keep going and send to all of them
63 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
69 * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
70 * @vf: pointer to the VF structure
71 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
72 * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
73 * @link_up: whether or not to set the link up/down
76 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
77 int ice_link_speed, bool link_up)
79 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
80 pfe->event_data.link_event_adv.link_status = link_up;
82 pfe->event_data.link_event_adv.link_speed =
83 ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
85 pfe->event_data.link_event.link_status = link_up;
86 /* Legacy method for virtchnl link speeds */
87 pfe->event_data.link_event.link_speed =
88 (enum virtchnl_link_speed)
89 ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
94 * ice_set_pfe_link_forced - Force the virtchnl_pf_event link speed/status
95 * @vf: pointer to the VF structure
96 * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
97 * @link_up: whether or not to set the link up/down
100 ice_set_pfe_link_forced(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
106 link_speed = ICE_AQ_LINK_SPEED_100GB;
108 link_speed = ICE_AQ_LINK_SPEED_UNKNOWN;
110 ice_set_pfe_link(vf, pfe, link_speed, link_up);
114 * ice_vc_notify_vf_link_state - Inform a VF of link status
115 * @vf: pointer to the VF structure
117 * send a link status message to a single VF
119 static void ice_vc_notify_vf_link_state(struct ice_vf *vf)
121 struct virtchnl_pf_event pfe = { 0 };
122 struct ice_link_status *ls;
123 struct ice_pf *pf = vf->pf;
127 ls = &hw->port_info->phy.link_info;
129 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
130 pfe.severity = PF_EVENT_SEVERITY_INFO;
133 ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
135 ice_set_pfe_link(vf, &pfe, ls->link_speed, ls->link_info &
138 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
139 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
144 * ice_free_vf_res - Free a VF's resources
145 * @vf: pointer to the VF info
147 static void ice_free_vf_res(struct ice_vf *vf)
149 struct ice_pf *pf = vf->pf;
150 int i, last_vector_idx;
152 /* First, disable VF's configuration API to prevent OS from
153 * accessing the VF's VSI after it's freed or invalidated.
155 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
157 /* free vsi & disconnect it from the parent uplink */
158 if (vf->lan_vsi_idx) {
159 ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
165 last_vector_idx = vf->first_vector_idx + pf->num_vf_msix - 1;
166 /* Disable interrupts so that VF starts in a known state */
167 for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
168 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
171 /* reset some of the state variables keeping track of the resources */
172 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
173 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
177 * ice_dis_vf_mappings
178 * @vf: pointer to the VF structure
180 static void ice_dis_vf_mappings(struct ice_vf *vf)
182 struct ice_pf *pf = vf->pf;
188 vsi = pf->vsi[vf->lan_vsi_idx];
190 wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
191 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
193 first = vf->first_vector_idx;
194 last = first + pf->num_vf_msix - 1;
195 for (v = first; v <= last; v++) {
198 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
199 GLINT_VECT2FUNC_IS_PF_M) |
200 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
201 GLINT_VECT2FUNC_PF_NUM_M));
202 wr32(hw, GLINT_VECT2FUNC(v), reg);
205 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
206 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
208 dev_err(&pf->pdev->dev,
209 "Scattered mode for VF Tx queues is not yet implemented\n");
211 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
212 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
214 dev_err(&pf->pdev->dev,
215 "Scattered mode for VF Rx queues is not yet implemented\n");
219 * ice_sriov_free_msix_res - Reset/free any used MSIX resources
220 * @pf: pointer to the PF structure
222 * If MSIX entries from the pf->irq_tracker were needed then we need to
223 * reset the irq_tracker->end and give back the entries we needed to
226 * If no MSIX entries were taken from the pf->irq_tracker then just clear
227 * the pf->sriov_base_vector.
229 * Returns 0 on success, and -EINVAL on error.
231 static int ice_sriov_free_msix_res(struct ice_pf *pf)
233 struct ice_res_tracker *res;
238 res = pf->irq_tracker;
242 /* give back irq_tracker resources used */
243 if (pf->sriov_base_vector < res->num_entries) {
244 res->end = res->num_entries;
245 pf->num_avail_sw_msix +=
246 res->num_entries - pf->sriov_base_vector;
249 pf->sriov_base_vector = 0;
255 * ice_free_vfs - Free all VFs
256 * @pf: pointer to the PF structure
258 void ice_free_vfs(struct ice_pf *pf)
260 struct ice_hw *hw = &pf->hw;
266 while (test_and_set_bit(__ICE_VF_DIS, pf->state))
267 usleep_range(1000, 2000);
269 /* Avoid wait time by stopping all VFs at the same time */
270 for (i = 0; i < pf->num_alloc_vfs; i++) {
273 if (!test_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states))
276 vsi = pf->vsi[pf->vf[i].lan_vsi_idx];
277 /* stop rings without wait time */
278 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, i);
279 ice_vsi_stop_rx_rings(vsi);
281 clear_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states);
284 /* Disable IOV before freeing resources. This lets any VF drivers
285 * running in the host get themselves cleaned up before we yank
286 * the carpet out from underneath their feet.
288 if (!pci_vfs_assigned(pf->pdev))
289 pci_disable_sriov(pf->pdev);
291 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
293 tmp = pf->num_alloc_vfs;
295 pf->num_alloc_vfs = 0;
296 for (i = 0; i < tmp; i++) {
297 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
298 /* disable VF qp mappings */
299 ice_dis_vf_mappings(&pf->vf[i]);
301 /* Set this state so that assigned VF vectors can be
302 * reclaimed by PF for reuse in ice_vsi_release(). No
303 * need to clear this bit since pf->vf array is being
304 * freed anyways after this for loop
306 set_bit(ICE_VF_STATE_CFG_INTR, pf->vf[i].vf_states);
307 ice_free_vf_res(&pf->vf[i]);
311 if (ice_sriov_free_msix_res(pf))
312 dev_err(&pf->pdev->dev,
313 "Failed to free MSIX resources used by SR-IOV\n");
315 devm_kfree(&pf->pdev->dev, pf->vf);
318 /* This check is for when the driver is unloaded while VFs are
319 * assigned. Setting the number of VFs to 0 through sysfs is caught
320 * before this function ever gets called.
322 if (!pci_vfs_assigned(pf->pdev)) {
325 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
326 * work correctly when SR-IOV gets re-enabled.
328 for (vf_id = 0; vf_id < tmp; vf_id++) {
329 u32 reg_idx, bit_idx;
331 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
332 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
333 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
336 clear_bit(__ICE_VF_DIS, pf->state);
337 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
341 * ice_trigger_vf_reset - Reset a VF on HW
342 * @vf: pointer to the VF structure
343 * @is_vflr: true if VFLR was issued, false if not
345 * Trigger hardware to start a reset for a particular VF. Expects the caller
346 * to wait the proper amount of time to allow hardware to reset the VF before
347 * it cleans up and restores VF functionality.
349 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr)
351 struct ice_pf *pf = vf->pf;
352 u32 reg, reg_idx, bit_idx;
357 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
359 /* Inform VF that it is no longer active, as a warning */
360 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
362 /* Disable VF's configuration API during reset. The flag is re-enabled
363 * in ice_alloc_vf_res(), when it's safe again to access VF's VSI.
364 * It's normally disabled in ice_free_vf_res(), but it's safer
365 * to do it earlier to give some time to finish to any VF config
366 * functions that may still be running at this point.
368 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
370 /* Clear the VF's ARQLEN register. This is how the VF detects reset,
371 * since the VFGEN_RSTAT register doesn't stick at 0 after reset.
373 wr32(hw, VF_MBX_ARQLEN(vf_abs_id), 0);
375 /* In the case of a VFLR, the HW has already reset the VF and we
376 * just need to clean up, so don't hit the VFRTRIG register.
379 /* reset VF using VPGEN_VFRTRIG reg */
380 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
381 reg |= VPGEN_VFRTRIG_VFSWR_M;
382 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
384 /* clear the VFLR bit in GLGEN_VFLRSTAT */
385 reg_idx = (vf_abs_id) / 32;
386 bit_idx = (vf_abs_id) % 32;
387 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
390 wr32(hw, PF_PCI_CIAA,
391 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
392 for (i = 0; i < 100; i++) {
393 reg = rd32(hw, PF_PCI_CIAD);
394 if ((reg & VF_TRANS_PENDING_M) != 0)
395 dev_err(&pf->pdev->dev,
396 "VF %d PCI transactions stuck\n", vf->vf_id);
402 * ice_vsi_set_pvid_fill_ctxt - Set VSI ctxt for add PVID
403 * @ctxt: the VSI ctxt to fill
404 * @vid: the VLAN ID to set as a PVID
406 static void ice_vsi_set_pvid_fill_ctxt(struct ice_vsi_ctx *ctxt, u16 vid)
408 ctxt->info.vlan_flags = (ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
409 ICE_AQ_VSI_PVLAN_INSERT_PVID |
410 ICE_AQ_VSI_VLAN_EMOD_STR);
411 ctxt->info.pvid = cpu_to_le16(vid);
412 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
413 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
414 ICE_AQ_VSI_PROP_SW_VALID);
418 * ice_vsi_kill_pvid_fill_ctxt - Set VSI ctx for remove PVID
419 * @ctxt: the VSI ctxt to fill
421 static void ice_vsi_kill_pvid_fill_ctxt(struct ice_vsi_ctx *ctxt)
423 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
424 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
425 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
426 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
427 ICE_AQ_VSI_PROP_SW_VALID);
431 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
432 * @vsi: the VSI to update
433 * @vid: the VLAN ID to set as a PVID
434 * @enable: true for enable PVID false for disable
436 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 vid, bool enable)
438 struct device *dev = &vsi->back->pdev->dev;
439 struct ice_hw *hw = &vsi->back->hw;
440 struct ice_vsi_ctx *ctxt;
441 enum ice_status status;
444 ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
448 ctxt->info = vsi->info;
450 ice_vsi_set_pvid_fill_ctxt(ctxt, vid);
452 ice_vsi_kill_pvid_fill_ctxt(ctxt);
454 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
456 dev_info(dev, "update VSI for port VLAN failed, err %d aq_err %d\n",
457 status, hw->adminq.sq_last_status);
462 vsi->info = ctxt->info;
464 devm_kfree(dev, ctxt);
469 * ice_vf_vsi_setup - Set up a VF VSI
470 * @pf: board private structure
471 * @pi: pointer to the port_info instance
472 * @vf_id: defines VF ID to which this VSI connects.
474 * Returns pointer to the successfully allocated VSI struct on success,
475 * otherwise returns NULL on failure.
477 static struct ice_vsi *
478 ice_vf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi, u16 vf_id)
480 return ice_vsi_setup(pf, pi, ICE_VSI_VF, vf_id);
484 * ice_calc_vf_first_vector_idx - Calculate absolute MSIX vector index in HW
485 * @pf: pointer to PF structure
486 * @vf: pointer to VF that the first MSIX vector index is being calculated for
488 * This returns the first MSIX vector index in HW that is used by this VF and
489 * this will always be the OICR index in the AVF driver so any functionality
490 * using vf->first_vector_idx for queue configuration will have to increment by
491 * 1 to avoid meddling with the OICR index.
493 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
495 return pf->hw.func_caps.common_cap.msix_vector_first_id +
496 pf->sriov_base_vector + vf->vf_id * pf->num_vf_msix;
500 * ice_alloc_vsi_res - Setup VF VSI and its resources
501 * @vf: pointer to the VF structure
503 * Returns 0 on success, negative value on failure
505 static int ice_alloc_vsi_res(struct ice_vf *vf)
507 struct ice_pf *pf = vf->pf;
508 LIST_HEAD(tmp_add_list);
509 u8 broadcast[ETH_ALEN];
513 /* first vector index is the VFs OICR index */
514 vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
516 vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
519 dev_err(&pf->pdev->dev, "Failed to create VF VSI\n");
523 vf->lan_vsi_idx = vsi->idx;
524 vf->lan_vsi_num = vsi->vsi_num;
526 /* Check if port VLAN exist before, and restore it accordingly */
527 if (vf->port_vlan_id) {
528 ice_vsi_manage_pvid(vsi, vf->port_vlan_id, true);
529 ice_vsi_add_vlan(vsi, vf->port_vlan_id & ICE_VLAN_M);
532 eth_broadcast_addr(broadcast);
534 status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
536 goto ice_alloc_vsi_res_exit;
538 if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
539 status = ice_add_mac_to_list(vsi, &tmp_add_list,
540 vf->dflt_lan_addr.addr);
542 goto ice_alloc_vsi_res_exit;
545 status = ice_add_mac(&pf->hw, &tmp_add_list);
547 dev_err(&pf->pdev->dev, "could not add mac filters\n");
549 /* Clear this bit after VF initialization since we shouldn't reclaim
550 * and reassign interrupts for synchronous or asynchronous VFR events.
551 * We don't want to reconfigure interrupts since AVF driver doesn't
552 * expect vector assignment to be changed unless there is a request for
555 clear_bit(ICE_VF_STATE_CFG_INTR, vf->vf_states);
556 ice_alloc_vsi_res_exit:
557 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
562 * ice_alloc_vf_res - Allocate VF resources
563 * @vf: pointer to the VF structure
565 static int ice_alloc_vf_res(struct ice_vf *vf)
567 struct ice_pf *pf = vf->pf;
568 int tx_rx_queue_left;
571 /* setup VF VSI and necessary resources */
572 status = ice_alloc_vsi_res(vf);
574 goto ice_alloc_vf_res_exit;
576 /* Update number of VF queues, in case VF had requested for queue
579 tx_rx_queue_left = min_t(int, pf->q_left_tx, pf->q_left_rx);
580 tx_rx_queue_left += ICE_DFLT_QS_PER_VF;
581 if (vf->num_req_qs && vf->num_req_qs <= tx_rx_queue_left &&
582 vf->num_req_qs != vf->num_vf_qs)
583 vf->num_vf_qs = vf->num_req_qs;
586 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
588 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
590 /* VF is now completely initialized */
591 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
595 ice_alloc_vf_res_exit:
601 * ice_ena_vf_mappings
602 * @vf: pointer to the VF structure
604 * Enable VF vectors and queues allocation by writing the details into
605 * respective registers.
607 static void ice_ena_vf_mappings(struct ice_vf *vf)
609 struct ice_pf *pf = vf->pf;
617 vsi = pf->vsi[vf->lan_vsi_idx];
618 first = vf->first_vector_idx;
619 last = (first + pf->num_vf_msix) - 1;
620 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
622 /* VF Vector allocation */
623 reg = (((first << VPINT_ALLOC_FIRST_S) & VPINT_ALLOC_FIRST_M) |
624 ((last << VPINT_ALLOC_LAST_S) & VPINT_ALLOC_LAST_M) |
625 VPINT_ALLOC_VALID_M);
626 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
628 reg = (((first << VPINT_ALLOC_PCI_FIRST_S) & VPINT_ALLOC_PCI_FIRST_M) |
629 ((last << VPINT_ALLOC_PCI_LAST_S) & VPINT_ALLOC_PCI_LAST_M) |
630 VPINT_ALLOC_PCI_VALID_M);
631 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
632 /* map the interrupts to its functions */
633 for (v = first; v <= last; v++) {
634 reg = (((abs_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
635 GLINT_VECT2FUNC_VF_NUM_M) |
636 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
637 GLINT_VECT2FUNC_PF_NUM_M));
638 wr32(hw, GLINT_VECT2FUNC(v), reg);
641 /* Map mailbox interrupt. We put an explicit 0 here to remind us that
642 * VF admin queue interrupts will go to VF MSI-X vector 0.
644 wr32(hw, VPINT_MBX_CTL(abs_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M | 0);
645 /* set regardless of mapping mode */
646 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
648 /* VF Tx queues allocation */
649 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
650 /* set the VF PF Tx queue range
651 * VFNUMQ value should be set to (number of queues - 1). A value
652 * of 0 means 1 queue and a value of 255 means 256 queues
654 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
655 VPLAN_TX_QBASE_VFFIRSTQ_M) |
656 (((vsi->alloc_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
657 VPLAN_TX_QBASE_VFNUMQ_M));
658 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
660 dev_err(&pf->pdev->dev,
661 "Scattered mode for VF Tx queues is not yet implemented\n");
664 /* set regardless of mapping mode */
665 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
667 /* VF Rx queues allocation */
668 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
669 /* set the VF PF Rx queue range
670 * VFNUMQ value should be set to (number of queues - 1). A value
671 * of 0 means 1 queue and a value of 255 means 256 queues
673 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
674 VPLAN_RX_QBASE_VFFIRSTQ_M) |
675 (((vsi->alloc_txq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
676 VPLAN_RX_QBASE_VFNUMQ_M));
677 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
679 dev_err(&pf->pdev->dev,
680 "Scattered mode for VF Rx queues is not yet implemented\n");
686 * @pf: pointer to the PF structure
687 * @avail_res: available resources in the PF structure
688 * @max_res: maximum resources that can be given per VF
689 * @min_res: minimum resources that can be given per VF
691 * Returns non-zero value if resources (queues/vectors) are available or
692 * returns zero if PF cannot accommodate for all num_alloc_vfs.
695 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
697 bool checked_min_res = false;
700 /* start by checking if PF can assign max number of resources for
702 * if yes, return number per VF
703 * If no, divide by 2 and roundup, check again
704 * repeat the loop till we reach a point where even minimum resources
705 * are not available, in that case return 0
708 while ((res >= min_res) && !checked_min_res) {
711 num_all_res = pf->num_alloc_vfs * res;
712 if (num_all_res <= avail_res)
716 checked_min_res = true;
718 res = DIV_ROUND_UP(res, 2);
724 * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
725 * @vf: VF to calculate the register index for
726 * @q_vector: a q_vector associated to the VF
728 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
732 if (!vf || !q_vector)
737 /* always add one to account for the OICR being the first MSIX */
738 return pf->sriov_base_vector + pf->num_vf_msix * vf->vf_id +
743 * ice_get_max_valid_res_idx - Get the max valid resource index
744 * @res: pointer to the resource to find the max valid index for
746 * Start from the end of the ice_res_tracker and return right when we find the
747 * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
748 * valid for SR-IOV because it is the only consumer that manipulates the
749 * res->end and this is always called when res->end is set to res->num_entries.
751 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
758 for (i = res->num_entries - 1; i >= 0; i--)
759 if (res->list[i] & ICE_RES_VALID_BIT)
766 * ice_sriov_set_msix_res - Set any used MSIX resources
767 * @pf: pointer to PF structure
768 * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
770 * This function allows SR-IOV resources to be taken from the end of the PF's
771 * allowed HW MSIX vectors so in many cases the irq_tracker will not
772 * be needed. In these cases we just set the pf->sriov_base_vector and return
775 * If SR-IOV needs to use any pf->irq_tracker entries it updates the
776 * irq_tracker->end based on the first entry needed for SR-IOV. This makes it
777 * so any calls to ice_get_res() using the irq_tracker will not try to use
778 * resources at or beyond the newly set value.
780 * Return 0 on success, and -EINVAL when there are not enough MSIX vectors in
781 * in the PF's space available for SR-IOV.
783 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
785 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
786 u16 pf_total_msix_vectors =
787 pf->hw.func_caps.common_cap.num_msix_vectors;
788 struct ice_res_tracker *res = pf->irq_tracker;
789 int sriov_base_vector;
791 if (max_valid_res_idx < 0)
792 return max_valid_res_idx;
794 sriov_base_vector = pf_total_msix_vectors - num_msix_needed;
796 /* make sure we only grab irq_tracker entries from the list end and
797 * that we have enough available MSIX vectors
799 if (sriov_base_vector <= max_valid_res_idx)
802 pf->sriov_base_vector = sriov_base_vector;
804 /* dip into irq_tracker entries and update used resources */
805 if (num_msix_needed > (pf_total_msix_vectors - res->num_entries)) {
806 pf->num_avail_sw_msix -=
807 res->num_entries - pf->sriov_base_vector;
808 res->end = pf->sriov_base_vector;
815 * ice_check_avail_res - check if vectors and queues are available
816 * @pf: pointer to the PF structure
818 * This function is where we calculate actual number of resources for VF VSIs,
819 * we don't reserve ahead of time during probe. Returns success if vectors and
820 * queues resources are available, otherwise returns error code
822 static int ice_check_avail_res(struct ice_pf *pf)
824 int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
825 u16 num_msix, num_txq, num_rxq, num_avail_msix;
827 if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
830 /* add 1 to max_valid_res_idx to account for it being 0-based */
831 num_avail_msix = pf->hw.func_caps.common_cap.num_msix_vectors -
832 (max_valid_res_idx + 1);
834 /* Grab from HW interrupts common pool
835 * Note: By the time the user decides it needs more vectors in a VF
836 * its already too late since one must decide this prior to creating the
837 * VF interface. So the best we can do is take a guess as to what the
840 * We have two policies for vector allocation:
841 * 1. if num_alloc_vfs is from 1 to 16, then we consider this as small
842 * number of NFV VFs used for NFV appliances, since this is a special
843 * case, we try to assign maximum vectors per VF (65) as much as
844 * possible, based on determine_resources algorithm.
845 * 2. if num_alloc_vfs is from 17 to 256, then its large number of
846 * regular VFs which are not used for any special purpose. Hence try to
847 * grab default interrupt vectors (5 as supported by AVF driver).
849 if (pf->num_alloc_vfs <= 16) {
850 num_msix = ice_determine_res(pf, num_avail_msix,
852 ICE_MIN_INTR_PER_VF);
853 } else if (pf->num_alloc_vfs <= ICE_MAX_VF_COUNT) {
854 num_msix = ice_determine_res(pf, num_avail_msix,
855 ICE_DFLT_INTR_PER_VF,
856 ICE_MIN_INTR_PER_VF);
858 dev_err(&pf->pdev->dev,
859 "Number of VFs %d exceeds max VF count %d\n",
860 pf->num_alloc_vfs, ICE_MAX_VF_COUNT);
867 /* Grab from the common pool
868 * start by requesting Default queues (4 as supported by AVF driver),
869 * Note that, the main difference between queues and vectors is, latter
870 * can only be reserved at init time but queues can be requested by VF
871 * at runtime through Virtchnl, that is the reason we start by reserving
874 num_txq = ice_determine_res(pf, pf->q_left_tx, ICE_DFLT_QS_PER_VF,
877 num_rxq = ice_determine_res(pf, pf->q_left_rx, ICE_DFLT_QS_PER_VF,
880 if (!num_txq || !num_rxq)
883 if (ice_sriov_set_msix_res(pf, num_msix * pf->num_alloc_vfs))
886 /* since AVF driver works with only queue pairs which means, it expects
887 * to have equal number of Rx and Tx queues, so take the minimum of
888 * available Tx or Rx queues
890 pf->num_vf_qps = min_t(int, num_txq, num_rxq);
891 pf->num_vf_msix = num_msix;
897 * ice_cleanup_and_realloc_vf - Clean up VF and reallocate resources after reset
898 * @vf: pointer to the VF structure
900 * Cleanup a VF after the hardware reset is finished. Expects the caller to
901 * have verified whether the reset is finished properly, and ensure the
902 * minimum amount of wait time has passed. Reallocate VF resources back to make
905 static void ice_cleanup_and_realloc_vf(struct ice_vf *vf)
907 struct ice_pf *pf = vf->pf;
913 /* PF software completes the flow by notifying VF that reset flow is
914 * completed. This is done by enabling hardware by clearing the reset
915 * bit in the VPGEN_VFRTRIG reg and setting VFR_STATE in the VFGEN_RSTAT
916 * register to VFR completed (done at the end of this function)
917 * By doing this we allow HW to access VF memory at any point. If we
918 * did it any sooner, HW could access memory while it was being freed
919 * in ice_free_vf_res(), causing an IOMMU fault.
921 * On the other hand, this needs to be done ASAP, because the VF driver
922 * is waiting for this to happen and may report a timeout. It's
923 * harmless, but it gets logged into Guest OS kernel log, so best avoid
926 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
927 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
928 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
930 /* reallocate VF resources to finish resetting the VSI state */
931 if (!ice_alloc_vf_res(vf)) {
932 ice_ena_vf_mappings(vf);
933 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
934 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
938 /* Tell the VF driver the reset is done. This needs to be done only
939 * after VF has been fully initialized, because the VF driver may
940 * request resources immediately after setting this flag.
942 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
946 * ice_vf_set_vsi_promisc - set given VF VSI to given promiscuous mode(s)
947 * @vf: pointer to the VF info
948 * @vsi: the VSI being configured
949 * @promisc_m: mask of promiscuous config bits
950 * @rm_promisc: promisc flag request from the VF to remove or add filter
952 * This function configures VF VSI promiscuous mode, based on the VF requests,
953 * for Unicast, Multicast and VLAN
955 static enum ice_status
956 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m,
959 struct ice_pf *pf = vf->pf;
960 enum ice_status status = 0;
965 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
967 } else if (vf->port_vlan_id) {
969 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
972 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
976 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
979 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
987 * ice_reset_all_vfs - reset all allocated VFs in one go
988 * @pf: pointer to the PF structure
989 * @is_vflr: true if VFLR was issued, false if not
991 * First, tell the hardware to reset each VF, then do all the waiting in one
992 * chunk, and finally finish restoring each VF after the wait. This is useful
993 * during PF routines which need to reset all VFs, as otherwise it must perform
994 * these resets in a serialized fashion.
996 * Returns true if any VFs were reset, and false otherwise.
998 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1000 struct ice_hw *hw = &pf->hw;
1004 /* If we don't have any VFs, then there is nothing to reset */
1005 if (!pf->num_alloc_vfs)
1008 /* If VFs have been disabled, there is no need to reset */
1009 if (test_and_set_bit(__ICE_VF_DIS, pf->state))
1012 /* Begin reset on all VFs at once */
1013 for (v = 0; v < pf->num_alloc_vfs; v++)
1014 ice_trigger_vf_reset(&pf->vf[v], is_vflr);
1016 for (v = 0; v < pf->num_alloc_vfs; v++) {
1017 struct ice_vsi *vsi;
1020 vsi = pf->vsi[vf->lan_vsi_idx];
1021 if (test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
1022 ice_vsi_stop_lan_tx_rings(vsi, ICE_VF_RESET, vf->vf_id);
1023 ice_vsi_stop_rx_rings(vsi);
1024 clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
1028 /* HW requires some time to make sure it can flush the FIFO for a VF
1029 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1030 * sequence to make sure that it has completed. We'll keep track of
1031 * the VFs using a simple iterator that increments once that VF has
1032 * finished resetting.
1034 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1035 usleep_range(10000, 20000);
1037 /* Check each VF in sequence */
1038 while (v < pf->num_alloc_vfs) {
1042 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1043 if (!(reg & VPGEN_VFRSTAT_VFRD_M))
1046 /* If the current VF has finished resetting, move on
1047 * to the next VF in sequence.
1053 /* Display a warning if at least one VF didn't manage to reset in
1054 * time, but continue on with the operation.
1056 if (v < pf->num_alloc_vfs)
1057 dev_warn(&pf->pdev->dev, "VF reset check timeout\n");
1058 usleep_range(10000, 20000);
1060 /* free VF resources to begin resetting the VSI state */
1061 for (v = 0; v < pf->num_alloc_vfs; v++) {
1064 ice_free_vf_res(vf);
1066 /* Free VF queues as well, and reallocate later.
1067 * If a given VF has different number of queues
1068 * configured, the request for update will come
1069 * via mailbox communication.
1074 if (ice_sriov_free_msix_res(pf))
1075 dev_err(&pf->pdev->dev,
1076 "Failed to free MSIX resources used by SR-IOV\n");
1078 if (ice_check_avail_res(pf)) {
1079 dev_err(&pf->pdev->dev,
1080 "Cannot allocate VF resources, try with fewer number of VFs\n");
1084 /* Finish the reset on each VF */
1085 for (v = 0; v < pf->num_alloc_vfs; v++) {
1088 vf->num_vf_qs = pf->num_vf_qps;
1089 dev_dbg(&pf->pdev->dev,
1090 "VF-id %d has %d queues configured\n",
1091 vf->vf_id, vf->num_vf_qs);
1092 ice_cleanup_and_realloc_vf(vf);
1096 clear_bit(__ICE_VF_DIS, pf->state);
1102 * ice_reset_vf - Reset a particular VF
1103 * @vf: pointer to the VF structure
1104 * @is_vflr: true if VFLR was issued, false if not
1106 * Returns true if the VF is reset, false otherwise.
1108 static bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1110 struct ice_pf *pf = vf->pf;
1111 struct ice_vsi *vsi;
1118 /* If the VFs have been disabled, this means something else is
1119 * resetting the VF, so we shouldn't continue.
1121 if (test_and_set_bit(__ICE_VF_DIS, pf->state))
1124 ice_trigger_vf_reset(vf, is_vflr);
1126 vsi = pf->vsi[vf->lan_vsi_idx];
1128 if (test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
1129 ice_vsi_stop_lan_tx_rings(vsi, ICE_VF_RESET, vf->vf_id);
1130 ice_vsi_stop_rx_rings(vsi);
1131 clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
1133 /* Call Disable LAN Tx queue AQ call even when queues are not
1134 * enabled. This is needed for successful completiom of VFR
1136 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1137 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1141 /* poll VPGEN_VFRSTAT reg to make sure
1142 * that reset is complete
1144 for (i = 0; i < 10; i++) {
1145 /* VF reset requires driver to first reset the VF and then
1146 * poll the status register to make sure that the reset
1147 * completed successfully.
1149 usleep_range(10000, 20000);
1150 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1151 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1157 /* Display a warning if VF didn't manage to reset in time, but need to
1158 * continue on with the operation.
1161 dev_warn(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1164 usleep_range(10000, 20000);
1166 /* disable promiscuous modes in case they were enabled
1167 * ignore any error if disabling process failed
1169 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1170 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1171 if (vf->port_vlan_id || vf->num_vlan)
1172 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1174 promisc_m = ICE_UCAST_PROMISC_BITS;
1176 vsi = pf->vsi[vf->lan_vsi_idx];
1177 if (ice_vf_set_vsi_promisc(vf, vsi, promisc_m, true))
1178 dev_err(&pf->pdev->dev, "disabling promiscuous mode failed\n");
1181 /* free VF resources to begin resetting the VSI state */
1182 ice_free_vf_res(vf);
1184 ice_cleanup_and_realloc_vf(vf);
1187 clear_bit(__ICE_VF_DIS, pf->state);
1193 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1194 * @pf: pointer to the PF structure
1196 void ice_vc_notify_link_state(struct ice_pf *pf)
1200 for (i = 0; i < pf->num_alloc_vfs; i++)
1201 ice_vc_notify_vf_link_state(&pf->vf[i]);
1205 * ice_vc_notify_reset - Send pending reset message to all VFs
1206 * @pf: pointer to the PF structure
1208 * indicate a pending reset to all VFs on a given PF
1210 void ice_vc_notify_reset(struct ice_pf *pf)
1212 struct virtchnl_pf_event pfe;
1214 if (!pf->num_alloc_vfs)
1217 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1218 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1219 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1220 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1224 * ice_vc_notify_vf_reset - Notify VF of a reset event
1225 * @vf: pointer to the VF structure
1227 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1229 struct virtchnl_pf_event pfe;
1231 /* validate the request */
1232 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1235 /* verify if the VF is in either init or active before proceeding */
1236 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1237 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1240 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1241 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1242 ice_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1243 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1248 * ice_alloc_vfs - Allocate and set up VFs resources
1249 * @pf: pointer to the PF structure
1250 * @num_alloc_vfs: number of VFs to allocate
1252 static int ice_alloc_vfs(struct ice_pf *pf, u16 num_alloc_vfs)
1254 struct ice_hw *hw = &pf->hw;
1258 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1259 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1260 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1264 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1266 pf->num_alloc_vfs = 0;
1267 goto err_unroll_intr;
1269 /* allocate memory */
1270 vfs = devm_kcalloc(&pf->pdev->dev, num_alloc_vfs, sizeof(*vfs),
1274 goto err_pci_disable_sriov;
1278 /* apply default profile */
1279 for (i = 0; i < num_alloc_vfs; i++) {
1281 vfs[i].vf_sw_id = pf->first_sw;
1284 /* assign default capabilities */
1285 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1286 vfs[i].spoofchk = true;
1288 /* Set this state so that PF driver does VF vector assignment */
1289 set_bit(ICE_VF_STATE_CFG_INTR, vfs[i].vf_states);
1291 pf->num_alloc_vfs = num_alloc_vfs;
1293 /* VF resources get allocated during reset */
1294 if (!ice_reset_all_vfs(pf, true)) {
1296 goto err_unroll_sriov;
1299 goto err_unroll_intr;
1303 devm_kfree(&pf->pdev->dev, vfs);
1305 pf->num_alloc_vfs = 0;
1306 err_pci_disable_sriov:
1307 pci_disable_sriov(pf->pdev);
1309 /* rearm interrupts here */
1310 ice_irq_dynamic_ena(hw, NULL, NULL);
1315 * ice_pf_state_is_nominal - checks the pf for nominal state
1316 * @pf: pointer to pf to check
1318 * Check the PF's state for a collection of bits that would indicate
1319 * the PF is in a state that would inhibit normal operation for
1320 * driver functionality.
1322 * Returns true if PF is in a nominal state.
1323 * Returns false otherwise
1325 static bool ice_pf_state_is_nominal(struct ice_pf *pf)
1327 DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
1332 bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
1333 if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
1340 * ice_pci_sriov_ena - Enable or change number of VFs
1341 * @pf: pointer to the PF structure
1342 * @num_vfs: number of VFs to allocate
1344 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1346 int pre_existing_vfs = pci_num_vf(pf->pdev);
1347 struct device *dev = &pf->pdev->dev;
1350 if (!ice_pf_state_is_nominal(pf)) {
1351 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1355 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1356 dev_err(dev, "This device is not capable of SR-IOV\n");
1360 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1362 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1365 if (num_vfs > pf->num_vfs_supported) {
1366 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1367 num_vfs, pf->num_vfs_supported);
1371 dev_info(dev, "Allocating %d VFs\n", num_vfs);
1372 err = ice_alloc_vfs(pf, num_vfs);
1374 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1378 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1383 * ice_sriov_configure - Enable or change number of VFs via sysfs
1384 * @pdev: pointer to a pci_dev structure
1385 * @num_vfs: number of VFs to allocate
1387 * This function is called when the user updates the number of VFs in sysfs.
1389 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1391 struct ice_pf *pf = pci_get_drvdata(pdev);
1394 return ice_pci_sriov_ena(pf, num_vfs);
1396 if (!pci_vfs_assigned(pdev)) {
1399 dev_err(&pf->pdev->dev,
1400 "can't free VFs because some are assigned to VMs.\n");
1408 * ice_process_vflr_event - Free VF resources via IRQ calls
1409 * @pf: pointer to the PF structure
1411 * called from the VFLR IRQ handler to
1412 * free up VF resources and state variables
1414 void ice_process_vflr_event(struct ice_pf *pf)
1416 struct ice_hw *hw = &pf->hw;
1420 if (!test_and_clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1424 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1425 struct ice_vf *vf = &pf->vf[vf_id];
1426 u32 reg_idx, bit_idx;
1428 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1429 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1430 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1431 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1432 if (reg & BIT(bit_idx))
1433 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1434 ice_reset_vf(vf, true);
1439 * ice_vc_dis_vf - Disable a given VF via SW reset
1440 * @vf: pointer to the VF info
1442 * Disable the VF through a SW reset
1444 static void ice_vc_dis_vf(struct ice_vf *vf)
1446 ice_vc_notify_vf_reset(vf);
1447 ice_reset_vf(vf, false);
1451 * ice_vc_send_msg_to_vf - Send message to VF
1452 * @vf: pointer to the VF info
1453 * @v_opcode: virtual channel opcode
1454 * @v_retval: virtual channel return value
1455 * @msg: pointer to the msg buffer
1456 * @msglen: msg length
1461 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
1462 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
1464 enum ice_status aq_ret;
1467 /* validate the request */
1468 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1473 /* single place to detect unsuccessful return values */
1475 vf->num_inval_msgs++;
1476 dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1477 vf->vf_id, v_opcode, v_retval);
1478 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
1479 dev_err(&pf->pdev->dev,
1480 "Number of invalid messages exceeded for VF %d\n",
1482 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1483 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1487 vf->num_valid_msgs++;
1488 /* reset the invalid counter, if a valid message is received. */
1489 vf->num_inval_msgs = 0;
1492 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
1495 dev_info(&pf->pdev->dev,
1496 "Unable to send the message to VF %d aq_err %d\n",
1497 vf->vf_id, pf->hw.mailboxq.sq_last_status);
1505 * ice_vc_get_ver_msg
1506 * @vf: pointer to the VF info
1507 * @msg: pointer to the msg buffer
1509 * called from the VF to request the API version used by the PF
1511 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
1513 struct virtchnl_version_info info = {
1514 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1517 vf->vf_ver = *(struct virtchnl_version_info *)msg;
1518 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1519 if (VF_IS_V10(&vf->vf_ver))
1520 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1522 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1523 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
1524 sizeof(struct virtchnl_version_info));
1528 * ice_vc_get_vf_res_msg
1529 * @vf: pointer to the VF info
1530 * @msg: pointer to the msg buffer
1532 * called from the VF to request its resources
1534 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
1536 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1537 struct virtchnl_vf_resource *vfres = NULL;
1538 struct ice_pf *pf = vf->pf;
1539 struct ice_vsi *vsi;
1543 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
1544 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1548 len = sizeof(struct virtchnl_vf_resource);
1550 vfres = devm_kzalloc(&pf->pdev->dev, len, GFP_KERNEL);
1552 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1556 if (VF_IS_V11(&vf->vf_ver))
1557 vf->driver_caps = *(u32 *)msg;
1559 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1560 VIRTCHNL_VF_OFFLOAD_RSS_REG |
1561 VIRTCHNL_VF_OFFLOAD_VLAN;
1563 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1564 vsi = pf->vsi[vf->lan_vsi_idx];
1566 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1570 if (!vsi->info.pvid)
1571 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1573 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1574 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1576 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1577 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1579 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1582 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1583 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1585 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1586 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1588 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
1589 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1591 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
1592 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1594 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1595 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1597 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1598 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1600 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1601 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
1603 vfres->num_vsis = 1;
1604 /* Tx and Rx queue are equal for VF */
1605 vfres->num_queue_pairs = vsi->num_txq;
1606 vfres->max_vectors = pf->num_vf_msix;
1607 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
1608 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
1610 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
1611 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1612 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
1613 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1614 vf->dflt_lan_addr.addr);
1616 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
1619 /* send the response back to the VF */
1620 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
1623 devm_kfree(&pf->pdev->dev, vfres);
1628 * ice_vc_reset_vf_msg
1629 * @vf: pointer to the VF info
1631 * called from the VF to reset itself,
1632 * unlike other virtchnl messages, PF driver
1633 * doesn't send the response back to the VF
1635 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
1637 if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1638 ice_reset_vf(vf, false);
1642 * ice_find_vsi_from_id
1643 * @pf: the pf structure to search for the VSI
1644 * @id: ID of the VSI it is searching for
1646 * searches for the VSI with the given ID
1648 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
1652 ice_for_each_vsi(pf, i)
1653 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
1660 * ice_vc_isvalid_vsi_id
1661 * @vf: pointer to the VF info
1662 * @vsi_id: VF relative VSI ID
1664 * check for the valid VSI ID
1666 static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
1668 struct ice_pf *pf = vf->pf;
1669 struct ice_vsi *vsi;
1671 vsi = ice_find_vsi_from_id(pf, vsi_id);
1673 return (vsi && (vsi->vf_id == vf->vf_id));
1677 * ice_vc_isvalid_q_id
1678 * @vf: pointer to the VF info
1680 * @qid: VSI relative queue ID
1682 * check for the valid queue ID
1684 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
1686 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
1687 /* allocated Tx and Rx queues should be always equal for VF VSI */
1688 return (vsi && (qid < vsi->alloc_txq));
1692 * ice_vc_config_rss_key
1693 * @vf: pointer to the VF info
1694 * @msg: pointer to the msg buffer
1696 * Configure the VF's RSS key
1698 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
1700 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1701 struct virtchnl_rss_key *vrk =
1702 (struct virtchnl_rss_key *)msg;
1703 struct ice_pf *pf = vf->pf;
1704 struct ice_vsi *vsi = NULL;
1706 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1707 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1711 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
1712 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1716 vsi = pf->vsi[vf->lan_vsi_idx];
1718 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1722 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
1723 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1727 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1728 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1732 if (ice_set_rss(vsi, vrk->key, NULL, 0))
1733 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1735 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
1740 * ice_vc_config_rss_lut
1741 * @vf: pointer to the VF info
1742 * @msg: pointer to the msg buffer
1744 * Configure the VF's RSS LUT
1746 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
1748 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
1749 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1750 struct ice_pf *pf = vf->pf;
1751 struct ice_vsi *vsi = NULL;
1753 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1754 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1758 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
1759 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1763 vsi = pf->vsi[vf->lan_vsi_idx];
1765 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1769 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
1770 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1774 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1775 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1779 if (ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
1780 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1782 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
1787 * ice_vc_get_stats_msg
1788 * @vf: pointer to the VF info
1789 * @msg: pointer to the msg buffer
1791 * called from the VF to get VSI stats
1793 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
1795 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1796 struct virtchnl_queue_select *vqs =
1797 (struct virtchnl_queue_select *)msg;
1798 struct ice_pf *pf = vf->pf;
1799 struct ice_eth_stats stats;
1800 struct ice_vsi *vsi;
1802 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1803 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1807 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1808 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1812 vsi = pf->vsi[vf->lan_vsi_idx];
1814 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1818 memset(&stats, 0, sizeof(struct ice_eth_stats));
1819 ice_update_eth_stats(vsi);
1821 stats = vsi->eth_stats;
1824 /* send the response to the VF */
1825 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
1826 (u8 *)&stats, sizeof(stats));
1831 * @vf: pointer to the VF info
1832 * @msg: pointer to the msg buffer
1834 * called from the VF to enable all or specific queue(s)
1836 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
1838 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1839 struct virtchnl_queue_select *vqs =
1840 (struct virtchnl_queue_select *)msg;
1841 struct ice_pf *pf = vf->pf;
1842 struct ice_vsi *vsi;
1844 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1845 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1849 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1850 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1854 if (!vqs->rx_queues && !vqs->tx_queues) {
1855 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1859 vsi = pf->vsi[vf->lan_vsi_idx];
1861 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1865 /* Enable only Rx rings, Tx rings were enabled by the FW when the
1866 * Tx queue group list was configured and the context bits were
1867 * programmed using ice_vsi_cfg_txqs
1869 if (ice_vsi_start_rx_rings(vsi))
1870 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1872 /* Set flag to indicate that queues are enabled */
1873 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1874 set_bit(ICE_VF_STATE_ENA, vf->vf_states);
1877 /* send the response to the VF */
1878 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
1884 * @vf: pointer to the VF info
1885 * @msg: pointer to the msg buffer
1887 * called from the VF to disable all or specific
1890 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
1892 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1893 struct virtchnl_queue_select *vqs =
1894 (struct virtchnl_queue_select *)msg;
1895 struct ice_pf *pf = vf->pf;
1896 struct ice_vsi *vsi;
1898 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
1899 !test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
1900 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1904 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1905 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1909 if (!vqs->rx_queues && !vqs->tx_queues) {
1910 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1914 vsi = pf->vsi[vf->lan_vsi_idx];
1916 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1920 if (ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id)) {
1921 dev_err(&vsi->back->pdev->dev,
1922 "Failed to stop tx rings on VSI %d\n",
1924 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1927 if (ice_vsi_stop_rx_rings(vsi)) {
1928 dev_err(&vsi->back->pdev->dev,
1929 "Failed to stop rx rings on VSI %d\n",
1931 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1934 /* Clear enabled queues flag */
1935 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1936 clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
1939 /* send the response to the VF */
1940 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
1945 * ice_vc_cfg_irq_map_msg
1946 * @vf: pointer to the VF info
1947 * @msg: pointer to the msg buffer
1949 * called from the VF to configure the IRQ to queue map
1951 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
1953 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1954 struct virtchnl_irq_map_info *irqmap_info;
1955 u16 vsi_id, vsi_q_id, vector_id;
1956 struct virtchnl_vector_map *map;
1957 struct ice_pf *pf = vf->pf;
1958 u16 num_q_vectors_mapped;
1959 struct ice_vsi *vsi;
1963 irqmap_info = (struct virtchnl_irq_map_info *)msg;
1964 num_q_vectors_mapped = irqmap_info->num_vectors;
1966 vsi = pf->vsi[vf->lan_vsi_idx];
1968 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1972 /* Check to make sure number of VF vectors mapped is not greater than
1973 * number of VF vectors originally allocated, and check that
1974 * there is actually at least a single VF queue vector mapped
1976 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1977 pf->num_vf_msix < num_q_vectors_mapped ||
1978 !irqmap_info->num_vectors) {
1979 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1983 for (i = 0; i < num_q_vectors_mapped; i++) {
1984 struct ice_q_vector *q_vector;
1986 map = &irqmap_info->vecmap[i];
1988 vector_id = map->vector_id;
1989 vsi_id = map->vsi_id;
1990 /* validate msg params */
1991 if (!(vector_id < pf->hw.func_caps.common_cap
1992 .num_msix_vectors) || !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
1993 (!vector_id && (map->rxq_map || map->txq_map))) {
1994 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1998 /* No need to map VF miscellaneous or rogue vector */
2002 /* Subtract non queue vector from vector_id passed by VF
2003 * to get actual number of VSI queue vector array index
2005 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
2007 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2011 /* lookout for the invalid queue index */
2012 qmap = map->rxq_map;
2013 q_vector->num_ring_rx = 0;
2014 for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
2015 if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
2016 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2019 q_vector->num_ring_rx++;
2020 q_vector->rx.itr_idx = map->rxitr_idx;
2021 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
2022 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
2023 q_vector->rx.itr_idx);
2026 qmap = map->txq_map;
2027 q_vector->num_ring_tx = 0;
2028 for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
2029 if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
2030 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2033 q_vector->num_ring_tx++;
2034 q_vector->tx.itr_idx = map->txitr_idx;
2035 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
2036 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
2037 q_vector->tx.itr_idx);
2042 /* send the response to the VF */
2043 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
2049 * @vf: pointer to the VF info
2050 * @msg: pointer to the msg buffer
2052 * called from the VF to configure the Rx/Tx queues
2054 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
2056 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2057 struct virtchnl_vsi_queue_config_info *qci =
2058 (struct virtchnl_vsi_queue_config_info *)msg;
2059 struct virtchnl_queue_pair_info *qpi;
2060 struct ice_pf *pf = vf->pf;
2061 struct ice_vsi *vsi;
2064 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2065 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2069 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
2070 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2074 vsi = pf->vsi[vf->lan_vsi_idx];
2078 if (qci->num_queue_pairs > ICE_MAX_BASE_QS_PER_VF) {
2079 dev_err(&pf->pdev->dev,
2080 "VF-%d requesting more than supported number of queues: %d\n",
2081 vf->vf_id, qci->num_queue_pairs);
2082 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2086 for (i = 0; i < qci->num_queue_pairs; i++) {
2087 qpi = &qci->qpair[i];
2088 if (qpi->txq.vsi_id != qci->vsi_id ||
2089 qpi->rxq.vsi_id != qci->vsi_id ||
2090 qpi->rxq.queue_id != qpi->txq.queue_id ||
2091 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
2092 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2095 /* copy Tx queue info from VF into VSI */
2096 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
2097 vsi->tx_rings[i]->count = qpi->txq.ring_len;
2098 /* copy Rx queue info from VF into VSI */
2099 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
2100 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
2101 if (qpi->rxq.databuffer_size > ((16 * 1024) - 128)) {
2102 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2105 vsi->rx_buf_len = qpi->rxq.databuffer_size;
2106 if (qpi->rxq.max_pkt_size >= (16 * 1024) ||
2107 qpi->rxq.max_pkt_size < 64) {
2108 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2111 vsi->max_frame = qpi->rxq.max_pkt_size;
2114 /* VF can request to configure less than allocated queues
2115 * or default allocated queues. So update the VSI with new number
2117 vsi->num_txq = qci->num_queue_pairs;
2118 vsi->num_rxq = qci->num_queue_pairs;
2119 /* All queues of VF VSI are in TC 0 */
2120 vsi->tc_cfg.tc_info[0].qcount_tx = qci->num_queue_pairs;
2121 vsi->tc_cfg.tc_info[0].qcount_rx = qci->num_queue_pairs;
2123 if (ice_vsi_cfg_lan_txqs(vsi) || ice_vsi_cfg_rxqs(vsi))
2124 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2127 /* send the response to the VF */
2128 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
2134 * @vf: pointer to the VF info
2136 static bool ice_is_vf_trusted(struct ice_vf *vf)
2138 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
2142 * ice_can_vf_change_mac
2143 * @vf: pointer to the VF info
2145 * Return true if the VF is allowed to change its MAC filters, false otherwise
2147 static bool ice_can_vf_change_mac(struct ice_vf *vf)
2149 /* If the VF MAC address has been set administratively (via the
2150 * ndo_set_vf_mac command), then deny permission to the VF to
2151 * add/delete unicast MAC addresses, unless the VF is trusted
2153 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
2160 * ice_vc_handle_mac_addr_msg
2161 * @vf: pointer to the VF info
2162 * @msg: pointer to the msg buffer
2163 * @set: true if MAC filters are being set, false otherwise
2165 * add guest MAC address filter
2168 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
2170 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2171 struct virtchnl_ether_addr_list *al =
2172 (struct virtchnl_ether_addr_list *)msg;
2173 struct ice_pf *pf = vf->pf;
2174 enum virtchnl_ops vc_op;
2175 LIST_HEAD(mac_list);
2176 struct ice_vsi *vsi;
2181 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2183 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2185 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2186 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2187 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2188 goto handle_mac_exit;
2191 if (set && !ice_is_vf_trusted(vf) &&
2192 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2193 dev_err(&pf->pdev->dev,
2194 "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",
2196 /* There is no need to let VF know about not being trusted
2197 * to add more MAC addr, so we can just return success message.
2199 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2200 goto handle_mac_exit;
2203 vsi = pf->vsi[vf->lan_vsi_idx];
2205 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2206 goto handle_mac_exit;
2209 for (i = 0; i < al->num_elements; i++) {
2210 u8 *maddr = al->list[i].addr;
2212 if (ether_addr_equal(maddr, vf->dflt_lan_addr.addr) ||
2213 is_broadcast_ether_addr(maddr)) {
2215 /* VF is trying to add filters that the PF
2216 * already added. Just continue.
2218 dev_info(&pf->pdev->dev,
2219 "MAC %pM already set for VF %d\n",
2223 /* VF can't remove dflt_lan_addr/bcast MAC */
2224 dev_err(&pf->pdev->dev,
2225 "VF can't remove default MAC address or MAC %pM programmed by PF for VF %d\n",
2231 /* check for the invalid cases and bail if necessary */
2232 if (is_zero_ether_addr(maddr)) {
2233 dev_err(&pf->pdev->dev,
2234 "invalid MAC %pM provided for VF %d\n",
2236 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2237 goto handle_mac_exit;
2240 if (is_unicast_ether_addr(maddr) &&
2241 !ice_can_vf_change_mac(vf)) {
2242 dev_err(&pf->pdev->dev,
2243 "can't change unicast MAC for untrusted VF %d\n",
2245 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2246 goto handle_mac_exit;
2249 /* get here if maddr is multicast or if VF can change MAC */
2250 if (ice_add_mac_to_list(vsi, &mac_list, al->list[i].addr)) {
2251 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2252 goto handle_mac_exit;
2257 /* program the updated filter list */
2259 v_ret = ice_err_to_virt_err(ice_add_mac(&pf->hw, &mac_list));
2261 v_ret = ice_err_to_virt_err(ice_remove_mac(&pf->hw, &mac_list));
2264 dev_err(&pf->pdev->dev,
2265 "can't update MAC filters for VF %d, error %d\n",
2269 vf->num_mac += mac_count;
2271 vf->num_mac -= mac_count;
2275 ice_free_fltr_list(&pf->pdev->dev, &mac_list);
2276 /* send the response to the VF */
2277 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2281 * ice_vc_add_mac_addr_msg
2282 * @vf: pointer to the VF info
2283 * @msg: pointer to the msg buffer
2285 * add guest MAC address filter
2287 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2289 return ice_vc_handle_mac_addr_msg(vf, msg, true);
2293 * ice_vc_del_mac_addr_msg
2294 * @vf: pointer to the VF info
2295 * @msg: pointer to the msg buffer
2297 * remove guest MAC address filter
2299 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2301 return ice_vc_handle_mac_addr_msg(vf, msg, false);
2305 * ice_vc_request_qs_msg
2306 * @vf: pointer to the VF info
2307 * @msg: pointer to the msg buffer
2309 * VFs get a default number of queues but can use this message to request a
2310 * different number. If the request is successful, PF will reset the VF and
2311 * return 0. If unsuccessful, PF will send message informing VF of number of
2312 * available queue pairs via virtchnl message response to VF.
2314 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2316 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2317 struct virtchnl_vf_res_request *vfres =
2318 (struct virtchnl_vf_res_request *)msg;
2319 int req_queues = vfres->num_queue_pairs;
2320 struct ice_pf *pf = vf->pf;
2321 int max_allowed_vf_queues;
2322 int tx_rx_queue_left;
2325 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2326 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2330 cur_queues = vf->num_vf_qs;
2331 tx_rx_queue_left = min_t(int, pf->q_left_tx, pf->q_left_rx);
2332 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2333 if (req_queues <= 0) {
2334 dev_err(&pf->pdev->dev,
2335 "VF %d tried to request %d queues. Ignoring.\n",
2336 vf->vf_id, req_queues);
2337 } else if (req_queues > ICE_MAX_BASE_QS_PER_VF) {
2338 dev_err(&pf->pdev->dev,
2339 "VF %d tried to request more than %d queues.\n",
2340 vf->vf_id, ICE_MAX_BASE_QS_PER_VF);
2341 vfres->num_queue_pairs = ICE_MAX_BASE_QS_PER_VF;
2342 } else if (req_queues - cur_queues > tx_rx_queue_left) {
2343 dev_warn(&pf->pdev->dev,
2344 "VF %d requested %d more queues, but only %d left.\n",
2345 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2346 vfres->num_queue_pairs = min_t(int, max_allowed_vf_queues,
2347 ICE_MAX_BASE_QS_PER_VF);
2349 /* request is successful, then reset VF */
2350 vf->num_req_qs = req_queues;
2352 dev_info(&pf->pdev->dev,
2353 "VF %d granted request of %d queues.\n",
2354 vf->vf_id, req_queues);
2359 /* send the response to the VF */
2360 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2361 v_ret, (u8 *)vfres, sizeof(*vfres));
2365 * ice_set_vf_port_vlan
2366 * @netdev: network interface device structure
2367 * @vf_id: VF identifier
2368 * @vlan_id: VLAN ID being set
2369 * @qos: priority setting
2370 * @vlan_proto: VLAN protocol
2372 * program VF Port VLAN ID and/or QoS
2375 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
2378 u16 vlanprio = vlan_id | (qos << ICE_VLAN_PRIORITY_S);
2379 struct ice_netdev_priv *np = netdev_priv(netdev);
2380 struct ice_pf *pf = np->vsi->back;
2381 struct ice_vsi *vsi;
2385 /* validate the request */
2386 if (vf_id >= pf->num_alloc_vfs) {
2387 dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
2391 if (vlan_id > ICE_MAX_VLANID || qos > 7) {
2392 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2396 if (vlan_proto != htons(ETH_P_8021Q)) {
2397 dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
2398 return -EPROTONOSUPPORT;
2401 vf = &pf->vf[vf_id];
2402 vsi = pf->vsi[vf->lan_vsi_idx];
2403 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2404 dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
2408 if (le16_to_cpu(vsi->info.pvid) == vlanprio) {
2409 /* duplicate request, so just return success */
2410 dev_info(&pf->pdev->dev,
2411 "Duplicate pvid %d request\n", vlanprio);
2415 /* If PVID, then remove all filters on the old VLAN */
2417 ice_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2420 if (vlan_id || qos) {
2421 ret = ice_vsi_manage_pvid(vsi, vlanprio, true);
2423 goto error_set_pvid;
2425 ice_vsi_manage_pvid(vsi, 0, false);
2430 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2431 vlan_id, qos, vf_id);
2433 /* add new VLAN filter for each MAC */
2434 ret = ice_vsi_add_vlan(vsi, vlan_id);
2436 goto error_set_pvid;
2439 /* The Port VLAN needs to be saved across resets the same as the
2440 * default LAN MAC address.
2442 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
2449 * ice_vc_process_vlan_msg
2450 * @vf: pointer to the VF info
2451 * @msg: pointer to the msg buffer
2452 * @add_v: Add VLAN if true, otherwise delete VLAN
2454 * Process virtchnl op to add or remove programmed guest VLAN ID
2456 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2458 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2459 struct virtchnl_vlan_filter_list *vfl =
2460 (struct virtchnl_vlan_filter_list *)msg;
2461 struct ice_pf *pf = vf->pf;
2462 bool vlan_promisc = false;
2463 struct ice_vsi *vsi;
2469 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2470 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2474 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2475 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2479 if (add_v && !ice_is_vf_trusted(vf) &&
2480 vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
2481 dev_info(&pf->pdev->dev,
2482 "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2484 /* There is no need to let VF know about being not trusted,
2485 * so we can just return success message here
2490 for (i = 0; i < vfl->num_elements; i++) {
2491 if (vfl->vlan_id[i] > ICE_MAX_VLANID) {
2492 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2493 dev_err(&pf->pdev->dev,
2494 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2500 vsi = pf->vsi[vf->lan_vsi_idx];
2502 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2506 if (vsi->info.pvid) {
2507 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2511 if (ice_vsi_manage_vlan_stripping(vsi, add_v)) {
2512 dev_err(&pf->pdev->dev,
2513 "%sable VLAN stripping failed for VSI %i\n",
2514 add_v ? "en" : "dis", vsi->vsi_num);
2515 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2519 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2520 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2521 vlan_promisc = true;
2524 for (i = 0; i < vfl->num_elements; i++) {
2525 u16 vid = vfl->vlan_id[i];
2527 if (!ice_is_vf_trusted(vf) &&
2528 vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
2529 dev_info(&pf->pdev->dev,
2530 "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2532 /* There is no need to let VF know about being
2533 * not trusted, so we can just return success
2534 * message here as well.
2539 if (ice_vsi_add_vlan(vsi, vid)) {
2540 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2545 /* Enable VLAN pruning when VLAN is added */
2546 if (!vlan_promisc) {
2547 status = ice_cfg_vlan_pruning(vsi, true, false);
2549 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2550 dev_err(&pf->pdev->dev,
2551 "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2556 /* Enable Ucast/Mcast VLAN promiscuous mode */
2557 promisc_m = ICE_PROMISC_VLAN_TX |
2558 ICE_PROMISC_VLAN_RX;
2560 status = ice_set_vsi_promisc(hw, vsi->idx,
2563 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2564 dev_err(&pf->pdev->dev,
2565 "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2571 /* In case of non_trusted VF, number of VLAN elements passed
2572 * to PF for removal might be greater than number of VLANs
2573 * filter programmed for that VF - So, use actual number of
2574 * VLANS added earlier with add VLAN opcode. In order to avoid
2575 * removing VLAN that doesn't exist, which result to sending
2576 * erroneous failed message back to the VF
2580 num_vf_vlan = vf->num_vlan;
2581 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
2582 u16 vid = vfl->vlan_id[i];
2584 /* Make sure ice_vsi_kill_vlan is successful before
2585 * updating VLAN information
2587 if (ice_vsi_kill_vlan(vsi, vid)) {
2588 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2593 /* Disable VLAN pruning when removing VLAN */
2594 ice_cfg_vlan_pruning(vsi, false, false);
2596 /* Disable Unicast/Multicast VLAN promiscuous mode */
2598 promisc_m = ICE_PROMISC_VLAN_TX |
2599 ICE_PROMISC_VLAN_RX;
2601 ice_clear_vsi_promisc(hw, vsi->idx,
2608 /* send the response to the VF */
2610 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
2613 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
2618 * ice_vc_add_vlan_msg
2619 * @vf: pointer to the VF info
2620 * @msg: pointer to the msg buffer
2622 * Add and program guest VLAN ID
2624 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2626 return ice_vc_process_vlan_msg(vf, msg, true);
2630 * ice_vc_remove_vlan_msg
2631 * @vf: pointer to the VF info
2632 * @msg: pointer to the msg buffer
2634 * remove programmed guest VLAN ID
2636 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2638 return ice_vc_process_vlan_msg(vf, msg, false);
2642 * ice_vc_ena_vlan_stripping
2643 * @vf: pointer to the VF info
2645 * Enable VLAN header stripping for a given VF
2647 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2649 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2650 struct ice_pf *pf = vf->pf;
2651 struct ice_vsi *vsi;
2653 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2654 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2658 vsi = pf->vsi[vf->lan_vsi_idx];
2659 if (ice_vsi_manage_vlan_stripping(vsi, true))
2660 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2663 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2668 * ice_vc_dis_vlan_stripping
2669 * @vf: pointer to the VF info
2671 * Disable VLAN header stripping for a given VF
2673 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2675 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2676 struct ice_pf *pf = vf->pf;
2677 struct ice_vsi *vsi;
2679 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2680 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2684 vsi = pf->vsi[vf->lan_vsi_idx];
2686 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2690 if (ice_vsi_manage_vlan_stripping(vsi, false))
2691 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2694 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2699 * ice_vc_process_vf_msg - Process request from VF
2700 * @pf: pointer to the PF structure
2701 * @event: pointer to the AQ event
2703 * called from the common asq/arq handler to
2704 * process request from VF
2706 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
2708 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
2709 s16 vf_id = le16_to_cpu(event->desc.retval);
2710 u16 msglen = event->msg_len;
2711 u8 *msg = event->msg_buf;
2712 struct ice_vf *vf = NULL;
2715 if (vf_id >= pf->num_alloc_vfs) {
2720 vf = &pf->vf[vf_id];
2722 /* Check if VF is disabled. */
2723 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
2728 /* Perform basic checks on the msg */
2729 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
2731 if (err == VIRTCHNL_STATUS_ERR_PARAM)
2738 /* Perform additional checks specific to RSS and Virtchnl */
2739 if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
2740 struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
2742 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE)
2744 } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
2745 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2747 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE)
2753 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
2755 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
2756 vf_id, v_opcode, msglen, err);
2761 case VIRTCHNL_OP_VERSION:
2762 err = ice_vc_get_ver_msg(vf, msg);
2764 case VIRTCHNL_OP_GET_VF_RESOURCES:
2765 err = ice_vc_get_vf_res_msg(vf, msg);
2767 case VIRTCHNL_OP_RESET_VF:
2768 ice_vc_reset_vf_msg(vf);
2770 case VIRTCHNL_OP_ADD_ETH_ADDR:
2771 err = ice_vc_add_mac_addr_msg(vf, msg);
2773 case VIRTCHNL_OP_DEL_ETH_ADDR:
2774 err = ice_vc_del_mac_addr_msg(vf, msg);
2776 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2777 err = ice_vc_cfg_qs_msg(vf, msg);
2779 case VIRTCHNL_OP_ENABLE_QUEUES:
2780 err = ice_vc_ena_qs_msg(vf, msg);
2781 ice_vc_notify_vf_link_state(vf);
2783 case VIRTCHNL_OP_DISABLE_QUEUES:
2784 err = ice_vc_dis_qs_msg(vf, msg);
2786 case VIRTCHNL_OP_REQUEST_QUEUES:
2787 err = ice_vc_request_qs_msg(vf, msg);
2789 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
2790 err = ice_vc_cfg_irq_map_msg(vf, msg);
2792 case VIRTCHNL_OP_CONFIG_RSS_KEY:
2793 err = ice_vc_config_rss_key(vf, msg);
2795 case VIRTCHNL_OP_CONFIG_RSS_LUT:
2796 err = ice_vc_config_rss_lut(vf, msg);
2798 case VIRTCHNL_OP_GET_STATS:
2799 err = ice_vc_get_stats_msg(vf, msg);
2801 case VIRTCHNL_OP_ADD_VLAN:
2802 err = ice_vc_add_vlan_msg(vf, msg);
2804 case VIRTCHNL_OP_DEL_VLAN:
2805 err = ice_vc_remove_vlan_msg(vf, msg);
2807 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
2808 err = ice_vc_ena_vlan_stripping(vf);
2810 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
2811 err = ice_vc_dis_vlan_stripping(vf);
2813 case VIRTCHNL_OP_UNKNOWN:
2815 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
2817 err = ice_vc_send_msg_to_vf(vf, v_opcode,
2818 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
2823 /* Helper function cares less about error return values here
2824 * as it is busy with pending work.
2826 dev_info(&pf->pdev->dev,
2827 "PF failed to honor VF %d, opcode %d, error %d\n",
2828 vf_id, v_opcode, err);
2834 * @netdev: network interface device structure
2835 * @vf_id: VF identifier
2836 * @ivi: VF configuration structure
2838 * return VF configuration
2841 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
2843 struct ice_netdev_priv *np = netdev_priv(netdev);
2844 struct ice_vsi *vsi = np->vsi;
2845 struct ice_pf *pf = vsi->back;
2848 /* validate the request */
2849 if (vf_id >= pf->num_alloc_vfs) {
2850 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
2854 vf = &pf->vf[vf_id];
2855 vsi = pf->vsi[vf->lan_vsi_idx];
2857 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2858 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
2863 ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
2865 /* VF configuration for VLAN and applicable QoS */
2866 ivi->vlan = le16_to_cpu(vsi->info.pvid) & ICE_VLAN_M;
2867 ivi->qos = (le16_to_cpu(vsi->info.pvid) & ICE_PRIORITY_M) >>
2868 ICE_VLAN_PRIORITY_S;
2870 ivi->trusted = vf->trusted;
2871 ivi->spoofchk = vf->spoofchk;
2872 if (!vf->link_forced)
2873 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2874 else if (vf->link_up)
2875 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2877 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
2878 ivi->max_tx_rate = vf->tx_rate;
2879 ivi->min_tx_rate = 0;
2884 * ice_set_vf_spoofchk
2885 * @netdev: network interface device structure
2886 * @vf_id: VF identifier
2887 * @ena: flag to enable or disable feature
2889 * Enable or disable VF spoof checking
2891 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2893 struct ice_netdev_priv *np = netdev_priv(netdev);
2894 struct ice_vsi *vsi = np->vsi;
2895 struct ice_pf *pf = vsi->back;
2896 struct ice_vsi_ctx *ctx;
2897 enum ice_status status;
2901 /* validate the request */
2902 if (vf_id >= pf->num_alloc_vfs) {
2903 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
2907 vf = &pf->vf[vf_id];
2908 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2909 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
2913 if (ena == vf->spoofchk) {
2914 dev_dbg(&pf->pdev->dev, "VF spoofchk already %s\n",
2915 ena ? "ON" : "OFF");
2919 ctx = devm_kzalloc(&pf->pdev->dev, sizeof(*ctx), GFP_KERNEL);
2923 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2926 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
2927 ctx->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_PRUNE_EN_M;
2930 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
2932 dev_dbg(&pf->pdev->dev,
2933 "Error %d, failed to update VSI* parameters\n", status);
2939 vsi->info.sec_flags = ctx->info.sec_flags;
2940 vsi->info.sw_flags2 = ctx->info.sw_flags2;
2942 devm_kfree(&pf->pdev->dev, ctx);
2948 * @netdev: network interface device structure
2949 * @vf_id: VF identifier
2952 * program VF MAC address
2954 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2956 struct ice_netdev_priv *np = netdev_priv(netdev);
2957 struct ice_vsi *vsi = np->vsi;
2958 struct ice_pf *pf = vsi->back;
2962 /* validate the request */
2963 if (vf_id >= pf->num_alloc_vfs) {
2964 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
2968 vf = &pf->vf[vf_id];
2969 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2970 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
2974 if (is_zero_ether_addr(mac) || is_multicast_ether_addr(mac)) {
2975 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
2979 /* copy MAC into dflt_lan_addr and trigger a VF reset. The reset
2980 * flow will use the updated dflt_lan_addr and add a MAC filter
2981 * using ice_add_mac. Also set pf_set_mac to indicate that the PF has
2982 * set the MAC address for this VF.
2984 ether_addr_copy(vf->dflt_lan_addr.addr, mac);
2985 vf->pf_set_mac = true;
2987 "MAC on VF %d set to %pM. VF driver will be reinitialized\n",
2996 * @netdev: network interface device structure
2997 * @vf_id: VF identifier
2998 * @trusted: Boolean value to enable/disable trusted VF
3000 * Enable or disable a given VF as trusted
3002 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
3004 struct ice_netdev_priv *np = netdev_priv(netdev);
3005 struct ice_vsi *vsi = np->vsi;
3006 struct ice_pf *pf = vsi->back;
3009 /* validate the request */
3010 if (vf_id >= pf->num_alloc_vfs) {
3011 dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
3015 vf = &pf->vf[vf_id];
3016 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
3017 dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
3021 /* Check if already trusted */
3022 if (trusted == vf->trusted)
3025 vf->trusted = trusted;
3027 dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
3028 vf_id, trusted ? "" : "un");
3034 * ice_set_vf_link_state
3035 * @netdev: network interface device structure
3036 * @vf_id: VF identifier
3037 * @link_state: required link state
3039 * Set VF's link state, irrespective of physical link state status
3041 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
3043 struct ice_netdev_priv *np = netdev_priv(netdev);
3044 struct ice_pf *pf = np->vsi->back;
3045 struct virtchnl_pf_event pfe = { 0 };
3046 struct ice_link_status *ls;
3050 if (vf_id >= pf->num_alloc_vfs) {
3051 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3055 vf = &pf->vf[vf_id];
3057 ls = &pf->hw.port_info->phy.link_info;
3059 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
3060 dev_err(&pf->pdev->dev, "vf %d in reset. Try again.\n", vf_id);
3064 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
3065 pfe.severity = PF_EVENT_SEVERITY_INFO;
3067 switch (link_state) {
3068 case IFLA_VF_LINK_STATE_AUTO:
3069 vf->link_forced = false;
3070 vf->link_up = ls->link_info & ICE_AQ_LINK_UP;
3072 case IFLA_VF_LINK_STATE_ENABLE:
3073 vf->link_forced = true;
3076 case IFLA_VF_LINK_STATE_DISABLE:
3077 vf->link_forced = true;
3078 vf->link_up = false;
3084 if (vf->link_forced)
3085 ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
3087 ice_set_pfe_link(vf, &pfe, ls->link_speed, vf->link_up);
3089 /* Notify the VF of its new link state */
3090 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
3091 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,