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_40GB;
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_get_vf_vector - get VF interrupt vector register offset
145 * @vf_msix: number of MSIx vector per VF on a PF
146 * @vf_id: VF identifier
147 * @i: index of MSIx vector
149 static u32 ice_get_vf_vector(int vf_msix, int vf_id, int i)
151 return ((i == 0) ? VFINT_DYN_CTLN(vf_id) :
152 VFINT_DYN_CTLN(((vf_msix - 1) * (vf_id)) + (i - 1)));
156 * ice_free_vf_res - Free a VF's resources
157 * @vf: pointer to the VF info
159 static void ice_free_vf_res(struct ice_vf *vf)
161 struct ice_pf *pf = vf->pf;
164 /* First, disable VF's configuration API to prevent OS from
165 * accessing the VF's VSI after it's freed or invalidated.
167 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
169 /* free vsi & disconnect it from the parent uplink */
170 if (vf->lan_vsi_idx) {
171 ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
177 pf_vf_msix = pf->num_vf_msix;
178 /* Disable interrupts so that VF starts in a known state */
179 for (i = 0; i < pf_vf_msix; i++) {
182 reg_idx = ice_get_vf_vector(pf_vf_msix, vf->vf_id, i);
183 wr32(&pf->hw, reg_idx, VFINT_DYN_CTLN_CLEARPBA_M);
186 /* reset some of the state variables keeping track of the resources */
187 clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
188 clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
192 * ice_dis_vf_mappings
193 * @vf: pointer to the VF structure
195 static void ice_dis_vf_mappings(struct ice_vf *vf)
197 struct ice_pf *pf = vf->pf;
203 vsi = pf->vsi[vf->lan_vsi_idx];
205 wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
206 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
208 first = vf->first_vector_idx +
209 hw->func_caps.common_cap.msix_vector_first_id;
210 last = first + pf->num_vf_msix - 1;
211 for (v = first; v <= last; v++) {
214 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
215 GLINT_VECT2FUNC_IS_PF_M) |
216 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
217 GLINT_VECT2FUNC_PF_NUM_M));
218 wr32(hw, GLINT_VECT2FUNC(v), reg);
221 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
222 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
224 dev_err(&pf->pdev->dev,
225 "Scattered mode for VF Tx queues is not yet implemented\n");
227 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
228 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
230 dev_err(&pf->pdev->dev,
231 "Scattered mode for VF Rx queues is not yet implemented\n");
235 * ice_free_vfs - Free all VFs
236 * @pf: pointer to the PF structure
238 void ice_free_vfs(struct ice_pf *pf)
240 struct ice_hw *hw = &pf->hw;
246 while (test_and_set_bit(__ICE_VF_DIS, pf->state))
247 usleep_range(1000, 2000);
249 /* Disable IOV before freeing resources. This lets any VF drivers
250 * running in the host get themselves cleaned up before we yank
251 * the carpet out from underneath their feet.
253 if (!pci_vfs_assigned(pf->pdev))
254 pci_disable_sriov(pf->pdev);
256 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
258 /* Avoid wait time by stopping all VFs at the same time */
259 for (i = 0; i < pf->num_alloc_vfs; i++) {
262 if (!test_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states))
265 vsi = pf->vsi[pf->vf[i].lan_vsi_idx];
266 /* stop rings without wait time */
267 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, i);
268 ice_vsi_stop_rx_rings(vsi);
270 clear_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states);
273 tmp = pf->num_alloc_vfs;
275 pf->num_alloc_vfs = 0;
276 for (i = 0; i < tmp; i++) {
277 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
278 /* disable VF qp mappings */
279 ice_dis_vf_mappings(&pf->vf[i]);
281 /* Set this state so that assigned VF vectors can be
282 * reclaimed by PF for reuse in ice_vsi_release(). No
283 * need to clear this bit since pf->vf array is being
284 * freed anyways after this for loop
286 set_bit(ICE_VF_STATE_CFG_INTR, pf->vf[i].vf_states);
287 ice_free_vf_res(&pf->vf[i]);
291 devm_kfree(&pf->pdev->dev, pf->vf);
294 /* This check is for when the driver is unloaded while VFs are
295 * assigned. Setting the number of VFs to 0 through sysfs is caught
296 * before this function ever gets called.
298 if (!pci_vfs_assigned(pf->pdev)) {
301 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
302 * work correctly when SR-IOV gets re-enabled.
304 for (vf_id = 0; vf_id < tmp; vf_id++) {
305 u32 reg_idx, bit_idx;
307 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
308 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
309 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
312 clear_bit(__ICE_VF_DIS, pf->state);
313 clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
317 * ice_trigger_vf_reset - Reset a VF on HW
318 * @vf: pointer to the VF structure
319 * @is_vflr: true if VFLR was issued, false if not
321 * Trigger hardware to start a reset for a particular VF. Expects the caller
322 * to wait the proper amount of time to allow hardware to reset the VF before
323 * it cleans up and restores VF functionality.
325 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr)
327 struct ice_pf *pf = vf->pf;
328 u32 reg, reg_idx, bit_idx;
333 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
335 /* Inform VF that it is no longer active, as a warning */
336 clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
338 /* Disable VF's configuration API during reset. The flag is re-enabled
339 * in ice_alloc_vf_res(), when it's safe again to access VF's VSI.
340 * It's normally disabled in ice_free_vf_res(), but it's safer
341 * to do it earlier to give some time to finish to any VF config
342 * functions that may still be running at this point.
344 clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
346 /* Clear the VF's ARQLEN register. This is how the VF detects reset,
347 * since the VFGEN_RSTAT register doesn't stick at 0 after reset.
349 wr32(hw, VF_MBX_ARQLEN(vf_abs_id), 0);
351 /* In the case of a VFLR, the HW has already reset the VF and we
352 * just need to clean up, so don't hit the VFRTRIG register.
355 /* reset VF using VPGEN_VFRTRIG reg */
356 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
357 reg |= VPGEN_VFRTRIG_VFSWR_M;
358 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
360 /* clear the VFLR bit in GLGEN_VFLRSTAT */
361 reg_idx = (vf_abs_id) / 32;
362 bit_idx = (vf_abs_id) % 32;
363 wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
366 wr32(hw, PF_PCI_CIAA,
367 VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
368 for (i = 0; i < 100; i++) {
369 reg = rd32(hw, PF_PCI_CIAD);
370 if ((reg & VF_TRANS_PENDING_M) != 0)
371 dev_err(&pf->pdev->dev,
372 "VF %d PCI transactions stuck\n", vf->vf_id);
378 * ice_vsi_set_pvid_fill_ctxt - Set VSI ctxt for add PVID
379 * @ctxt: the VSI ctxt to fill
380 * @vid: the VLAN ID to set as a PVID
382 static void ice_vsi_set_pvid_fill_ctxt(struct ice_vsi_ctx *ctxt, u16 vid)
384 ctxt->info.vlan_flags = (ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
385 ICE_AQ_VSI_PVLAN_INSERT_PVID |
386 ICE_AQ_VSI_VLAN_EMOD_STR);
387 ctxt->info.pvid = cpu_to_le16(vid);
388 ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
389 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
390 ICE_AQ_VSI_PROP_SW_VALID);
394 * ice_vsi_kill_pvid_fill_ctxt - Set VSI ctx for remove PVID
395 * @ctxt: the VSI ctxt to fill
397 static void ice_vsi_kill_pvid_fill_ctxt(struct ice_vsi_ctx *ctxt)
399 ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
400 ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
401 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
402 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
403 ICE_AQ_VSI_PROP_SW_VALID);
407 * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
408 * @vsi: the VSI to update
409 * @vid: the VLAN ID to set as a PVID
410 * @enable: true for enable PVID false for disable
412 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 vid, bool enable)
414 struct device *dev = &vsi->back->pdev->dev;
415 struct ice_hw *hw = &vsi->back->hw;
416 struct ice_vsi_ctx *ctxt;
417 enum ice_status status;
420 ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
424 ctxt->info = vsi->info;
426 ice_vsi_set_pvid_fill_ctxt(ctxt, vid);
428 ice_vsi_kill_pvid_fill_ctxt(ctxt);
430 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
432 dev_info(dev, "update VSI for port VLAN failed, err %d aq_err %d\n",
433 status, hw->adminq.sq_last_status);
438 vsi->info = ctxt->info;
440 devm_kfree(dev, ctxt);
445 * ice_vf_vsi_setup - Set up a VF VSI
446 * @pf: board private structure
447 * @pi: pointer to the port_info instance
448 * @vf_id: defines VF ID to which this VSI connects.
450 * Returns pointer to the successfully allocated VSI struct on success,
451 * otherwise returns NULL on failure.
453 static struct ice_vsi *
454 ice_vf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi, u16 vf_id)
456 return ice_vsi_setup(pf, pi, ICE_VSI_VF, vf_id);
460 * ice_alloc_vsi_res - Setup VF VSI and its resources
461 * @vf: pointer to the VF structure
463 * Returns 0 on success, negative value on failure
465 static int ice_alloc_vsi_res(struct ice_vf *vf)
467 struct ice_pf *pf = vf->pf;
468 LIST_HEAD(tmp_add_list);
469 u8 broadcast[ETH_ALEN];
473 vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
476 dev_err(&pf->pdev->dev, "Failed to create VF VSI\n");
480 vf->lan_vsi_idx = vsi->idx;
481 vf->lan_vsi_num = vsi->vsi_num;
483 /* first vector index is the VFs OICR index */
484 vf->first_vector_idx = vsi->hw_base_vector;
485 /* Since hw_base_vector holds the vector where data queue interrupts
486 * starts, increment by 1 since VFs allocated vectors include OICR intr
489 vsi->hw_base_vector += 1;
491 /* Check if port VLAN exist before, and restore it accordingly */
492 if (vf->port_vlan_id) {
493 ice_vsi_manage_pvid(vsi, vf->port_vlan_id, true);
494 ice_vsi_add_vlan(vsi, vf->port_vlan_id & ICE_VLAN_M);
497 eth_broadcast_addr(broadcast);
499 status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
501 goto ice_alloc_vsi_res_exit;
503 if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
504 status = ice_add_mac_to_list(vsi, &tmp_add_list,
505 vf->dflt_lan_addr.addr);
507 goto ice_alloc_vsi_res_exit;
510 status = ice_add_mac(&pf->hw, &tmp_add_list);
512 dev_err(&pf->pdev->dev, "could not add mac filters\n");
514 /* Clear this bit after VF initialization since we shouldn't reclaim
515 * and reassign interrupts for synchronous or asynchronous VFR events.
516 * We don't want to reconfigure interrupts since AVF driver doesn't
517 * expect vector assignment to be changed unless there is a request for
520 clear_bit(ICE_VF_STATE_CFG_INTR, vf->vf_states);
521 ice_alloc_vsi_res_exit:
522 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
527 * ice_alloc_vf_res - Allocate VF resources
528 * @vf: pointer to the VF structure
530 static int ice_alloc_vf_res(struct ice_vf *vf)
532 struct ice_pf *pf = vf->pf;
533 int tx_rx_queue_left;
536 /* setup VF VSI and necessary resources */
537 status = ice_alloc_vsi_res(vf);
539 goto ice_alloc_vf_res_exit;
541 /* Update number of VF queues, in case VF had requested for queue
544 tx_rx_queue_left = min_t(int, pf->q_left_tx, pf->q_left_rx);
545 tx_rx_queue_left += ICE_DFLT_QS_PER_VF;
546 if (vf->num_req_qs && vf->num_req_qs <= tx_rx_queue_left &&
547 vf->num_req_qs != vf->num_vf_qs)
548 vf->num_vf_qs = vf->num_req_qs;
551 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
553 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
555 /* VF is now completely initialized */
556 set_bit(ICE_VF_STATE_INIT, vf->vf_states);
560 ice_alloc_vf_res_exit:
566 * ice_ena_vf_mappings
567 * @vf: pointer to the VF structure
569 * Enable VF vectors and queues allocation by writing the details into
570 * respective registers.
572 static void ice_ena_vf_mappings(struct ice_vf *vf)
574 struct ice_pf *pf = vf->pf;
582 vsi = pf->vsi[vf->lan_vsi_idx];
583 first = vf->first_vector_idx +
584 hw->func_caps.common_cap.msix_vector_first_id;
585 last = (first + pf->num_vf_msix) - 1;
586 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
588 /* VF Vector allocation */
589 reg = (((first << VPINT_ALLOC_FIRST_S) & VPINT_ALLOC_FIRST_M) |
590 ((last << VPINT_ALLOC_LAST_S) & VPINT_ALLOC_LAST_M) |
591 VPINT_ALLOC_VALID_M);
592 wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
594 reg = (((first << VPINT_ALLOC_PCI_FIRST_S) & VPINT_ALLOC_PCI_FIRST_M) |
595 ((last << VPINT_ALLOC_PCI_LAST_S) & VPINT_ALLOC_PCI_LAST_M) |
596 VPINT_ALLOC_PCI_VALID_M);
597 wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
598 /* map the interrupts to its functions */
599 for (v = first; v <= last; v++) {
600 reg = (((abs_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
601 GLINT_VECT2FUNC_VF_NUM_M) |
602 ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
603 GLINT_VECT2FUNC_PF_NUM_M));
604 wr32(hw, GLINT_VECT2FUNC(v), reg);
607 /* Map mailbox interrupt. We put an explicit 0 here to remind us that
608 * VF admin queue interrupts will go to VF MSI-X vector 0.
610 wr32(hw, VPINT_MBX_CTL(abs_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M | 0);
611 /* set regardless of mapping mode */
612 wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
614 /* VF Tx queues allocation */
615 if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
616 /* set the VF PF Tx queue range
617 * VFNUMQ value should be set to (number of queues - 1). A value
618 * of 0 means 1 queue and a value of 255 means 256 queues
620 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
621 VPLAN_TX_QBASE_VFFIRSTQ_M) |
622 (((vsi->alloc_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
623 VPLAN_TX_QBASE_VFNUMQ_M));
624 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
626 dev_err(&pf->pdev->dev,
627 "Scattered mode for VF Tx queues is not yet implemented\n");
630 /* set regardless of mapping mode */
631 wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
633 /* VF Rx queues allocation */
634 if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
635 /* set the VF PF Rx queue range
636 * VFNUMQ value should be set to (number of queues - 1). A value
637 * of 0 means 1 queue and a value of 255 means 256 queues
639 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
640 VPLAN_RX_QBASE_VFFIRSTQ_M) |
641 (((vsi->alloc_txq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
642 VPLAN_RX_QBASE_VFNUMQ_M));
643 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
645 dev_err(&pf->pdev->dev,
646 "Scattered mode for VF Rx queues is not yet implemented\n");
652 * @pf: pointer to the PF structure
653 * @avail_res: available resources in the PF structure
654 * @max_res: maximum resources that can be given per VF
655 * @min_res: minimum resources that can be given per VF
657 * Returns non-zero value if resources (queues/vectors) are available or
658 * returns zero if PF cannot accommodate for all num_alloc_vfs.
661 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
663 bool checked_min_res = false;
666 /* start by checking if PF can assign max number of resources for
668 * if yes, return number per VF
669 * If no, divide by 2 and roundup, check again
670 * repeat the loop till we reach a point where even minimum resources
671 * are not available, in that case return 0
674 while ((res >= min_res) && !checked_min_res) {
677 num_all_res = pf->num_alloc_vfs * res;
678 if (num_all_res <= avail_res)
682 checked_min_res = true;
684 res = DIV_ROUND_UP(res, 2);
690 * ice_check_avail_res - check if vectors and queues are available
691 * @pf: pointer to the PF structure
693 * This function is where we calculate actual number of resources for VF VSIs,
694 * we don't reserve ahead of time during probe. Returns success if vectors and
695 * queues resources are available, otherwise returns error code
697 static int ice_check_avail_res(struct ice_pf *pf)
699 u16 num_msix, num_txq, num_rxq;
701 if (!pf->num_alloc_vfs)
704 /* Grab from HW interrupts common pool
705 * Note: By the time the user decides it needs more vectors in a VF
706 * its already too late since one must decide this prior to creating the
707 * VF interface. So the best we can do is take a guess as to what the
710 * We have two policies for vector allocation:
711 * 1. if num_alloc_vfs is from 1 to 16, then we consider this as small
712 * number of NFV VFs used for NFV appliances, since this is a special
713 * case, we try to assign maximum vectors per VF (65) as much as
714 * possible, based on determine_resources algorithm.
715 * 2. if num_alloc_vfs is from 17 to 256, then its large number of
716 * regular VFs which are not used for any special purpose. Hence try to
717 * grab default interrupt vectors (5 as supported by AVF driver).
719 if (pf->num_alloc_vfs <= 16) {
720 num_msix = ice_determine_res(pf, pf->num_avail_hw_msix,
722 ICE_MIN_INTR_PER_VF);
723 } else if (pf->num_alloc_vfs <= ICE_MAX_VF_COUNT) {
724 num_msix = ice_determine_res(pf, pf->num_avail_hw_msix,
725 ICE_DFLT_INTR_PER_VF,
726 ICE_MIN_INTR_PER_VF);
728 dev_err(&pf->pdev->dev,
729 "Number of VFs %d exceeds max VF count %d\n",
730 pf->num_alloc_vfs, ICE_MAX_VF_COUNT);
737 /* Grab from the common pool
738 * start by requesting Default queues (4 as supported by AVF driver),
739 * Note that, the main difference between queues and vectors is, latter
740 * can only be reserved at init time but queues can be requested by VF
741 * at runtime through Virtchnl, that is the reason we start by reserving
744 num_txq = ice_determine_res(pf, pf->q_left_tx, ICE_DFLT_QS_PER_VF,
747 num_rxq = ice_determine_res(pf, pf->q_left_rx, ICE_DFLT_QS_PER_VF,
750 if (!num_txq || !num_rxq)
753 /* since AVF driver works with only queue pairs which means, it expects
754 * to have equal number of Rx and Tx queues, so take the minimum of
755 * available Tx or Rx queues
757 pf->num_vf_qps = min_t(int, num_txq, num_rxq);
758 pf->num_vf_msix = num_msix;
764 * ice_cleanup_and_realloc_vf - Clean up VF and reallocate resources after reset
765 * @vf: pointer to the VF structure
767 * Cleanup a VF after the hardware reset is finished. Expects the caller to
768 * have verified whether the reset is finished properly, and ensure the
769 * minimum amount of wait time has passed. Reallocate VF resources back to make
772 static void ice_cleanup_and_realloc_vf(struct ice_vf *vf)
774 struct ice_pf *pf = vf->pf;
780 /* PF software completes the flow by notifying VF that reset flow is
781 * completed. This is done by enabling hardware by clearing the reset
782 * bit in the VPGEN_VFRTRIG reg and setting VFR_STATE in the VFGEN_RSTAT
783 * register to VFR completed (done at the end of this function)
784 * By doing this we allow HW to access VF memory at any point. If we
785 * did it any sooner, HW could access memory while it was being freed
786 * in ice_free_vf_res(), causing an IOMMU fault.
788 * On the other hand, this needs to be done ASAP, because the VF driver
789 * is waiting for this to happen and may report a timeout. It's
790 * harmless, but it gets logged into Guest OS kernel log, so best avoid
793 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
794 reg &= ~VPGEN_VFRTRIG_VFSWR_M;
795 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
797 /* reallocate VF resources to finish resetting the VSI state */
798 if (!ice_alloc_vf_res(vf)) {
799 ice_ena_vf_mappings(vf);
800 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
801 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
805 /* Tell the VF driver the reset is done. This needs to be done only
806 * after VF has been fully initialized, because the VF driver may
807 * request resources immediately after setting this flag.
809 wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
813 * ice_vf_set_vsi_promisc - set given VF VSI to given promiscuous mode(s)
814 * @vf: pointer to the VF info
815 * @vsi: the VSI being configured
816 * @promisc_m: mask of promiscuous config bits
817 * @rm_promisc: promisc flag request from the VF to remove or add filter
819 * This function configures VF VSI promiscuous mode, based on the VF requests,
820 * for Unicast, Multicast and VLAN
822 static enum ice_status
823 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m,
826 struct ice_pf *pf = vf->pf;
827 enum ice_status status = 0;
832 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
834 } else if (vf->port_vlan_id) {
836 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
839 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
843 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
846 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
854 * ice_reset_all_vfs - reset all allocated VFs in one go
855 * @pf: pointer to the PF structure
856 * @is_vflr: true if VFLR was issued, false if not
858 * First, tell the hardware to reset each VF, then do all the waiting in one
859 * chunk, and finally finish restoring each VF after the wait. This is useful
860 * during PF routines which need to reset all VFs, as otherwise it must perform
861 * these resets in a serialized fashion.
863 * Returns true if any VFs were reset, and false otherwise.
865 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
867 struct ice_hw *hw = &pf->hw;
871 /* If we don't have any VFs, then there is nothing to reset */
872 if (!pf->num_alloc_vfs)
875 /* If VFs have been disabled, there is no need to reset */
876 if (test_and_set_bit(__ICE_VF_DIS, pf->state))
879 /* Begin reset on all VFs at once */
880 for (v = 0; v < pf->num_alloc_vfs; v++)
881 ice_trigger_vf_reset(&pf->vf[v], is_vflr);
883 for (v = 0; v < pf->num_alloc_vfs; v++) {
887 vsi = pf->vsi[vf->lan_vsi_idx];
888 if (test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
889 ice_vsi_stop_lan_tx_rings(vsi, ICE_VF_RESET, vf->vf_id);
890 ice_vsi_stop_rx_rings(vsi);
891 clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
895 /* HW requires some time to make sure it can flush the FIFO for a VF
896 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
897 * sequence to make sure that it has completed. We'll keep track of
898 * the VFs using a simple iterator that increments once that VF has
899 * finished resetting.
901 for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
902 usleep_range(10000, 20000);
904 /* Check each VF in sequence */
905 while (v < pf->num_alloc_vfs) {
909 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
910 if (!(reg & VPGEN_VFRSTAT_VFRD_M))
913 /* If the current VF has finished resetting, move on
914 * to the next VF in sequence.
920 /* Display a warning if at least one VF didn't manage to reset in
921 * time, but continue on with the operation.
923 if (v < pf->num_alloc_vfs)
924 dev_warn(&pf->pdev->dev, "VF reset check timeout\n");
925 usleep_range(10000, 20000);
927 /* free VF resources to begin resetting the VSI state */
928 for (v = 0; v < pf->num_alloc_vfs; v++) {
933 /* Free VF queues as well, and reallocate later.
934 * If a given VF has different number of queues
935 * configured, the request for update will come
936 * via mailbox communication.
941 if (ice_check_avail_res(pf)) {
942 dev_err(&pf->pdev->dev,
943 "Cannot allocate VF resources, try with fewer number of VFs\n");
947 /* Finish the reset on each VF */
948 for (v = 0; v < pf->num_alloc_vfs; v++) {
951 vf->num_vf_qs = pf->num_vf_qps;
952 dev_dbg(&pf->pdev->dev,
953 "VF-id %d has %d queues configured\n",
954 vf->vf_id, vf->num_vf_qs);
955 ice_cleanup_and_realloc_vf(vf);
959 clear_bit(__ICE_VF_DIS, pf->state);
965 * ice_reset_vf - Reset a particular VF
966 * @vf: pointer to the VF structure
967 * @is_vflr: true if VFLR was issued, false if not
969 * Returns true if the VF is reset, false otherwise.
971 static bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
973 struct ice_pf *pf = vf->pf;
981 /* If the VFs have been disabled, this means something else is
982 * resetting the VF, so we shouldn't continue.
984 if (test_and_set_bit(__ICE_VF_DIS, pf->state))
987 ice_trigger_vf_reset(vf, is_vflr);
989 vsi = pf->vsi[vf->lan_vsi_idx];
991 if (test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
992 ice_vsi_stop_lan_tx_rings(vsi, ICE_VF_RESET, vf->vf_id);
993 ice_vsi_stop_rx_rings(vsi);
994 clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
996 /* Call Disable LAN Tx queue AQ call even when queues are not
997 * enabled. This is needed for successful completiom of VFR
999 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1000 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1004 /* poll VPGEN_VFRSTAT reg to make sure
1005 * that reset is complete
1007 for (i = 0; i < 10; i++) {
1008 /* VF reset requires driver to first reset the VF and then
1009 * poll the status register to make sure that the reset
1010 * completed successfully.
1012 usleep_range(10000, 20000);
1013 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1014 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1020 /* Display a warning if VF didn't manage to reset in time, but need to
1021 * continue on with the operation.
1024 dev_warn(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1027 usleep_range(10000, 20000);
1029 /* disable promiscuous modes in case they were enabled
1030 * ignore any error if disabling process failed
1032 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1033 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1034 if (vf->port_vlan_id || vf->num_vlan)
1035 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1037 promisc_m = ICE_UCAST_PROMISC_BITS;
1039 vsi = pf->vsi[vf->lan_vsi_idx];
1040 if (ice_vf_set_vsi_promisc(vf, vsi, promisc_m, true))
1041 dev_err(&pf->pdev->dev, "disabling promiscuous mode failed\n");
1044 /* free VF resources to begin resetting the VSI state */
1045 ice_free_vf_res(vf);
1047 ice_cleanup_and_realloc_vf(vf);
1050 clear_bit(__ICE_VF_DIS, pf->state);
1056 * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1057 * @pf: pointer to the PF structure
1059 void ice_vc_notify_link_state(struct ice_pf *pf)
1063 for (i = 0; i < pf->num_alloc_vfs; i++)
1064 ice_vc_notify_vf_link_state(&pf->vf[i]);
1068 * ice_vc_notify_reset - Send pending reset message to all VFs
1069 * @pf: pointer to the PF structure
1071 * indicate a pending reset to all VFs on a given PF
1073 void ice_vc_notify_reset(struct ice_pf *pf)
1075 struct virtchnl_pf_event pfe;
1077 if (!pf->num_alloc_vfs)
1080 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1081 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1082 ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1083 (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1087 * ice_vc_notify_vf_reset - Notify VF of a reset event
1088 * @vf: pointer to the VF structure
1090 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1092 struct virtchnl_pf_event pfe;
1094 /* validate the request */
1095 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1098 /* verify if the VF is in either init or active before proceeding */
1099 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1100 !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1103 pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1104 pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1105 ice_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1106 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1111 * ice_alloc_vfs - Allocate and set up VFs resources
1112 * @pf: pointer to the PF structure
1113 * @num_alloc_vfs: number of VFs to allocate
1115 static int ice_alloc_vfs(struct ice_pf *pf, u16 num_alloc_vfs)
1117 struct ice_hw *hw = &pf->hw;
1121 /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1122 wr32(hw, GLINT_DYN_CTL(pf->hw_oicr_idx),
1123 ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1127 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1129 pf->num_alloc_vfs = 0;
1130 goto err_unroll_intr;
1132 /* allocate memory */
1133 vfs = devm_kcalloc(&pf->pdev->dev, num_alloc_vfs, sizeof(*vfs),
1137 goto err_pci_disable_sriov;
1141 /* apply default profile */
1142 for (i = 0; i < num_alloc_vfs; i++) {
1144 vfs[i].vf_sw_id = pf->first_sw;
1147 /* assign default capabilities */
1148 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1149 vfs[i].spoofchk = true;
1151 /* Set this state so that PF driver does VF vector assignment */
1152 set_bit(ICE_VF_STATE_CFG_INTR, vfs[i].vf_states);
1154 pf->num_alloc_vfs = num_alloc_vfs;
1156 /* VF resources get allocated during reset */
1157 if (!ice_reset_all_vfs(pf, true)) {
1159 goto err_unroll_sriov;
1162 goto err_unroll_intr;
1166 devm_kfree(&pf->pdev->dev, vfs);
1168 pf->num_alloc_vfs = 0;
1169 err_pci_disable_sriov:
1170 pci_disable_sriov(pf->pdev);
1172 /* rearm interrupts here */
1173 ice_irq_dynamic_ena(hw, NULL, NULL);
1178 * ice_pf_state_is_nominal - checks the pf for nominal state
1179 * @pf: pointer to pf to check
1181 * Check the PF's state for a collection of bits that would indicate
1182 * the PF is in a state that would inhibit normal operation for
1183 * driver functionality.
1185 * Returns true if PF is in a nominal state.
1186 * Returns false otherwise
1188 static bool ice_pf_state_is_nominal(struct ice_pf *pf)
1190 DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
1195 bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
1196 if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
1203 * ice_pci_sriov_ena - Enable or change number of VFs
1204 * @pf: pointer to the PF structure
1205 * @num_vfs: number of VFs to allocate
1207 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1209 int pre_existing_vfs = pci_num_vf(pf->pdev);
1210 struct device *dev = &pf->pdev->dev;
1213 if (!ice_pf_state_is_nominal(pf)) {
1214 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1218 if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1219 dev_err(dev, "This device is not capable of SR-IOV\n");
1223 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1225 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1228 if (num_vfs > pf->num_vfs_supported) {
1229 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1230 num_vfs, pf->num_vfs_supported);
1234 dev_info(dev, "Allocating %d VFs\n", num_vfs);
1235 err = ice_alloc_vfs(pf, num_vfs);
1237 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1241 set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1246 * ice_sriov_configure - Enable or change number of VFs via sysfs
1247 * @pdev: pointer to a pci_dev structure
1248 * @num_vfs: number of VFs to allocate
1250 * This function is called when the user updates the number of VFs in sysfs.
1252 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1254 struct ice_pf *pf = pci_get_drvdata(pdev);
1257 return ice_pci_sriov_ena(pf, num_vfs);
1259 if (!pci_vfs_assigned(pdev)) {
1262 dev_err(&pf->pdev->dev,
1263 "can't free VFs because some are assigned to VMs.\n");
1271 * ice_process_vflr_event - Free VF resources via IRQ calls
1272 * @pf: pointer to the PF structure
1274 * called from the VFLR IRQ handler to
1275 * free up VF resources and state variables
1277 void ice_process_vflr_event(struct ice_pf *pf)
1279 struct ice_hw *hw = &pf->hw;
1283 if (!test_and_clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1287 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1288 struct ice_vf *vf = &pf->vf[vf_id];
1289 u32 reg_idx, bit_idx;
1291 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1292 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1293 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1294 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1295 if (reg & BIT(bit_idx))
1296 /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1297 ice_reset_vf(vf, true);
1302 * ice_vc_dis_vf - Disable a given VF via SW reset
1303 * @vf: pointer to the VF info
1305 * Disable the VF through a SW reset
1307 static void ice_vc_dis_vf(struct ice_vf *vf)
1309 ice_vc_notify_vf_reset(vf);
1310 ice_reset_vf(vf, false);
1314 * ice_vc_send_msg_to_vf - Send message to VF
1315 * @vf: pointer to the VF info
1316 * @v_opcode: virtual channel opcode
1317 * @v_retval: virtual channel return value
1318 * @msg: pointer to the msg buffer
1319 * @msglen: msg length
1324 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
1325 enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
1327 enum ice_status aq_ret;
1330 /* validate the request */
1331 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1336 /* single place to detect unsuccessful return values */
1338 vf->num_inval_msgs++;
1339 dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1340 vf->vf_id, v_opcode, v_retval);
1341 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
1342 dev_err(&pf->pdev->dev,
1343 "Number of invalid messages exceeded for VF %d\n",
1345 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1346 set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1350 vf->num_valid_msgs++;
1351 /* reset the invalid counter, if a valid message is received. */
1352 vf->num_inval_msgs = 0;
1355 aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
1358 dev_info(&pf->pdev->dev,
1359 "Unable to send the message to VF %d aq_err %d\n",
1360 vf->vf_id, pf->hw.mailboxq.sq_last_status);
1368 * ice_vc_get_ver_msg
1369 * @vf: pointer to the VF info
1370 * @msg: pointer to the msg buffer
1372 * called from the VF to request the API version used by the PF
1374 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
1376 struct virtchnl_version_info info = {
1377 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1380 vf->vf_ver = *(struct virtchnl_version_info *)msg;
1381 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1382 if (VF_IS_V10(&vf->vf_ver))
1383 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1385 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1386 VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
1387 sizeof(struct virtchnl_version_info));
1391 * ice_vc_get_vf_res_msg
1392 * @vf: pointer to the VF info
1393 * @msg: pointer to the msg buffer
1395 * called from the VF to request its resources
1397 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
1399 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1400 struct virtchnl_vf_resource *vfres = NULL;
1401 struct ice_pf *pf = vf->pf;
1402 struct ice_vsi *vsi;
1406 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
1407 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1411 len = sizeof(struct virtchnl_vf_resource);
1413 vfres = devm_kzalloc(&pf->pdev->dev, len, GFP_KERNEL);
1415 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1419 if (VF_IS_V11(&vf->vf_ver))
1420 vf->driver_caps = *(u32 *)msg;
1422 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1423 VIRTCHNL_VF_OFFLOAD_RSS_REG |
1424 VIRTCHNL_VF_OFFLOAD_VLAN;
1426 vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1427 vsi = pf->vsi[vf->lan_vsi_idx];
1429 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1433 if (!vsi->info.pvid)
1434 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1436 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1437 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1439 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1440 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1442 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1445 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1446 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1448 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1449 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1451 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
1452 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1454 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
1455 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1457 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1458 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1460 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1461 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1463 if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1464 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
1466 vfres->num_vsis = 1;
1467 /* Tx and Rx queue are equal for VF */
1468 vfres->num_queue_pairs = vsi->num_txq;
1469 vfres->max_vectors = pf->num_vf_msix;
1470 vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
1471 vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
1473 vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
1474 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1475 vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
1476 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1477 vf->dflt_lan_addr.addr);
1479 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
1482 /* send the response back to the VF */
1483 ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
1486 devm_kfree(&pf->pdev->dev, vfres);
1491 * ice_vc_reset_vf_msg
1492 * @vf: pointer to the VF info
1494 * called from the VF to reset itself,
1495 * unlike other virtchnl messages, PF driver
1496 * doesn't send the response back to the VF
1498 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
1500 if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1501 ice_reset_vf(vf, false);
1505 * ice_find_vsi_from_id
1506 * @pf: the pf structure to search for the VSI
1507 * @id: ID of the VSI it is searching for
1509 * searches for the VSI with the given ID
1511 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
1515 ice_for_each_vsi(pf, i)
1516 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
1523 * ice_vc_isvalid_vsi_id
1524 * @vf: pointer to the VF info
1525 * @vsi_id: VF relative VSI ID
1527 * check for the valid VSI ID
1529 static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
1531 struct ice_pf *pf = vf->pf;
1532 struct ice_vsi *vsi;
1534 vsi = ice_find_vsi_from_id(pf, vsi_id);
1536 return (vsi && (vsi->vf_id == vf->vf_id));
1540 * ice_vc_isvalid_q_id
1541 * @vf: pointer to the VF info
1543 * @qid: VSI relative queue ID
1545 * check for the valid queue ID
1547 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
1549 struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
1550 /* allocated Tx and Rx queues should be always equal for VF VSI */
1551 return (vsi && (qid < vsi->alloc_txq));
1555 * ice_vc_config_rss_key
1556 * @vf: pointer to the VF info
1557 * @msg: pointer to the msg buffer
1559 * Configure the VF's RSS key
1561 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
1563 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1564 struct virtchnl_rss_key *vrk =
1565 (struct virtchnl_rss_key *)msg;
1566 struct ice_pf *pf = vf->pf;
1567 struct ice_vsi *vsi = NULL;
1569 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1570 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1574 if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
1575 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1579 vsi = pf->vsi[vf->lan_vsi_idx];
1581 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1585 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
1586 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1590 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1591 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1595 if (ice_set_rss(vsi, vrk->key, NULL, 0))
1596 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1598 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
1603 * ice_vc_config_rss_lut
1604 * @vf: pointer to the VF info
1605 * @msg: pointer to the msg buffer
1607 * Configure the VF's RSS LUT
1609 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
1611 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
1612 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1613 struct ice_pf *pf = vf->pf;
1614 struct ice_vsi *vsi = NULL;
1616 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1617 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1621 if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
1622 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1626 vsi = pf->vsi[vf->lan_vsi_idx];
1628 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1632 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
1633 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1637 if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1638 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1642 if (ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
1643 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1645 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
1650 * ice_vc_get_stats_msg
1651 * @vf: pointer to the VF info
1652 * @msg: pointer to the msg buffer
1654 * called from the VF to get VSI stats
1656 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
1658 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1659 struct virtchnl_queue_select *vqs =
1660 (struct virtchnl_queue_select *)msg;
1661 struct ice_pf *pf = vf->pf;
1662 struct ice_eth_stats stats;
1663 struct ice_vsi *vsi;
1665 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1666 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1670 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1671 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1675 vsi = pf->vsi[vf->lan_vsi_idx];
1677 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1681 memset(&stats, 0, sizeof(struct ice_eth_stats));
1682 ice_update_eth_stats(vsi);
1684 stats = vsi->eth_stats;
1687 /* send the response to the VF */
1688 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
1689 (u8 *)&stats, sizeof(stats));
1694 * @vf: pointer to the VF info
1695 * @msg: pointer to the msg buffer
1697 * called from the VF to enable all or specific queue(s)
1699 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
1701 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1702 struct virtchnl_queue_select *vqs =
1703 (struct virtchnl_queue_select *)msg;
1704 struct ice_pf *pf = vf->pf;
1705 struct ice_vsi *vsi;
1707 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1708 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1712 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1713 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1717 if (!vqs->rx_queues && !vqs->tx_queues) {
1718 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1722 vsi = pf->vsi[vf->lan_vsi_idx];
1724 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1728 /* Enable only Rx rings, Tx rings were enabled by the FW when the
1729 * Tx queue group list was configured and the context bits were
1730 * programmed using ice_vsi_cfg_txqs
1732 if (ice_vsi_start_rx_rings(vsi))
1733 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1735 /* Set flag to indicate that queues are enabled */
1736 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1737 set_bit(ICE_VF_STATE_ENA, vf->vf_states);
1740 /* send the response to the VF */
1741 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
1747 * @vf: pointer to the VF info
1748 * @msg: pointer to the msg buffer
1750 * called from the VF to disable all or specific
1753 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
1755 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1756 struct virtchnl_queue_select *vqs =
1757 (struct virtchnl_queue_select *)msg;
1758 struct ice_pf *pf = vf->pf;
1759 struct ice_vsi *vsi;
1761 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
1762 !test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
1763 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1767 if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1768 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1772 if (!vqs->rx_queues && !vqs->tx_queues) {
1773 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1777 vsi = pf->vsi[vf->lan_vsi_idx];
1779 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1783 if (ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id)) {
1784 dev_err(&vsi->back->pdev->dev,
1785 "Failed to stop tx rings on VSI %d\n",
1787 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1790 if (ice_vsi_stop_rx_rings(vsi)) {
1791 dev_err(&vsi->back->pdev->dev,
1792 "Failed to stop rx rings on VSI %d\n",
1794 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1797 /* Clear enabled queues flag */
1798 if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1799 clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
1802 /* send the response to the VF */
1803 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
1808 * ice_vc_cfg_irq_map_msg
1809 * @vf: pointer to the VF info
1810 * @msg: pointer to the msg buffer
1812 * called from the VF to configure the IRQ to queue map
1814 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
1816 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1817 struct virtchnl_irq_map_info *irqmap_info;
1818 u16 vsi_id, vsi_q_id, vector_id;
1819 struct virtchnl_vector_map *map;
1820 struct ice_pf *pf = vf->pf;
1821 struct ice_vsi *vsi;
1826 irqmap_info = (struct virtchnl_irq_map_info *)msg;
1827 num_q_vectors = irqmap_info->num_vectors - ICE_NONQ_VECS_VF;
1828 vsi = pf->vsi[vf->lan_vsi_idx];
1830 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1831 !vsi || vsi->num_q_vectors < num_q_vectors ||
1832 irqmap_info->num_vectors == 0) {
1833 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1837 for (i = 0; i < num_q_vectors; i++) {
1838 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1840 map = &irqmap_info->vecmap[i];
1842 vector_id = map->vector_id;
1843 vsi_id = map->vsi_id;
1844 /* validate msg params */
1845 if (!(vector_id < pf->hw.func_caps.common_cap
1846 .num_msix_vectors) || !ice_vc_isvalid_vsi_id(vf, vsi_id)) {
1847 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1851 /* lookout for the invalid queue index */
1852 qmap = map->rxq_map;
1853 q_vector->num_ring_rx = 0;
1854 for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
1855 if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
1856 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1859 q_vector->num_ring_rx++;
1860 q_vector->rx.itr_idx = map->rxitr_idx;
1861 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
1864 qmap = map->txq_map;
1865 q_vector->num_ring_tx = 0;
1866 for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
1867 if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
1868 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1871 q_vector->num_ring_tx++;
1872 q_vector->tx.itr_idx = map->txitr_idx;
1873 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
1878 ice_vsi_cfg_msix(vsi);
1880 /* send the response to the VF */
1881 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
1887 * @vf: pointer to the VF info
1888 * @msg: pointer to the msg buffer
1890 * called from the VF to configure the Rx/Tx queues
1892 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
1894 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1895 struct virtchnl_vsi_queue_config_info *qci =
1896 (struct virtchnl_vsi_queue_config_info *)msg;
1897 struct virtchnl_queue_pair_info *qpi;
1898 struct ice_pf *pf = vf->pf;
1899 struct ice_vsi *vsi;
1902 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1903 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1907 if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
1908 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1912 vsi = pf->vsi[vf->lan_vsi_idx];
1916 if (qci->num_queue_pairs > ICE_MAX_BASE_QS_PER_VF) {
1917 dev_err(&pf->pdev->dev,
1918 "VF-%d requesting more than supported number of queues: %d\n",
1919 vf->vf_id, qci->num_queue_pairs);
1920 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1924 for (i = 0; i < qci->num_queue_pairs; i++) {
1925 qpi = &qci->qpair[i];
1926 if (qpi->txq.vsi_id != qci->vsi_id ||
1927 qpi->rxq.vsi_id != qci->vsi_id ||
1928 qpi->rxq.queue_id != qpi->txq.queue_id ||
1929 !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
1930 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1933 /* copy Tx queue info from VF into VSI */
1934 vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
1935 vsi->tx_rings[i]->count = qpi->txq.ring_len;
1936 /* copy Rx queue info from VF into VSI */
1937 vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
1938 vsi->rx_rings[i]->count = qpi->rxq.ring_len;
1939 if (qpi->rxq.databuffer_size > ((16 * 1024) - 128)) {
1940 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1943 vsi->rx_buf_len = qpi->rxq.databuffer_size;
1944 if (qpi->rxq.max_pkt_size >= (16 * 1024) ||
1945 qpi->rxq.max_pkt_size < 64) {
1946 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1949 vsi->max_frame = qpi->rxq.max_pkt_size;
1952 /* VF can request to configure less than allocated queues
1953 * or default allocated queues. So update the VSI with new number
1955 vsi->num_txq = qci->num_queue_pairs;
1956 vsi->num_rxq = qci->num_queue_pairs;
1957 /* All queues of VF VSI are in TC 0 */
1958 vsi->tc_cfg.tc_info[0].qcount_tx = qci->num_queue_pairs;
1959 vsi->tc_cfg.tc_info[0].qcount_rx = qci->num_queue_pairs;
1961 if (ice_vsi_cfg_lan_txqs(vsi) || ice_vsi_cfg_rxqs(vsi))
1962 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1965 /* send the response to the VF */
1966 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
1972 * @vf: pointer to the VF info
1974 static bool ice_is_vf_trusted(struct ice_vf *vf)
1976 return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1980 * ice_can_vf_change_mac
1981 * @vf: pointer to the VF info
1983 * Return true if the VF is allowed to change its MAC filters, false otherwise
1985 static bool ice_can_vf_change_mac(struct ice_vf *vf)
1987 /* If the VF MAC address has been set administratively (via the
1988 * ndo_set_vf_mac command), then deny permission to the VF to
1989 * add/delete unicast MAC addresses, unless the VF is trusted
1991 if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
1998 * ice_vc_handle_mac_addr_msg
1999 * @vf: pointer to the VF info
2000 * @msg: pointer to the msg buffer
2001 * @set: true if MAC filters are being set, false otherwise
2003 * add guest MAC address filter
2006 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
2008 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2009 struct virtchnl_ether_addr_list *al =
2010 (struct virtchnl_ether_addr_list *)msg;
2011 struct ice_pf *pf = vf->pf;
2012 enum virtchnl_ops vc_op;
2013 LIST_HEAD(mac_list);
2014 struct ice_vsi *vsi;
2019 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2021 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2023 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2024 !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2025 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2026 goto handle_mac_exit;
2029 if (set && !ice_is_vf_trusted(vf) &&
2030 (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2031 dev_err(&pf->pdev->dev,
2032 "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",
2034 /* There is no need to let VF know about not being trusted
2035 * to add more MAC addr, so we can just return success message.
2037 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2038 goto handle_mac_exit;
2041 vsi = pf->vsi[vf->lan_vsi_idx];
2043 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2044 goto handle_mac_exit;
2047 for (i = 0; i < al->num_elements; i++) {
2048 u8 *maddr = al->list[i].addr;
2050 if (ether_addr_equal(maddr, vf->dflt_lan_addr.addr) ||
2051 is_broadcast_ether_addr(maddr)) {
2053 /* VF is trying to add filters that the PF
2054 * already added. Just continue.
2056 dev_info(&pf->pdev->dev,
2057 "MAC %pM already set for VF %d\n",
2061 /* VF can't remove dflt_lan_addr/bcast MAC */
2062 dev_err(&pf->pdev->dev,
2063 "VF can't remove default MAC address or MAC %pM programmed by PF for VF %d\n",
2069 /* check for the invalid cases and bail if necessary */
2070 if (is_zero_ether_addr(maddr)) {
2071 dev_err(&pf->pdev->dev,
2072 "invalid MAC %pM provided for VF %d\n",
2074 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2075 goto handle_mac_exit;
2078 if (is_unicast_ether_addr(maddr) &&
2079 !ice_can_vf_change_mac(vf)) {
2080 dev_err(&pf->pdev->dev,
2081 "can't change unicast MAC for untrusted VF %d\n",
2083 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2084 goto handle_mac_exit;
2087 /* get here if maddr is multicast or if VF can change MAC */
2088 if (ice_add_mac_to_list(vsi, &mac_list, al->list[i].addr)) {
2089 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2090 goto handle_mac_exit;
2095 /* program the updated filter list */
2097 v_ret = ice_err_to_virt_err(ice_add_mac(&pf->hw, &mac_list));
2099 v_ret = ice_err_to_virt_err(ice_remove_mac(&pf->hw, &mac_list));
2102 dev_err(&pf->pdev->dev,
2103 "can't update MAC filters for VF %d, error %d\n",
2107 vf->num_mac += mac_count;
2109 vf->num_mac -= mac_count;
2113 ice_free_fltr_list(&pf->pdev->dev, &mac_list);
2114 /* send the response to the VF */
2115 return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2119 * ice_vc_add_mac_addr_msg
2120 * @vf: pointer to the VF info
2121 * @msg: pointer to the msg buffer
2123 * add guest MAC address filter
2125 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2127 return ice_vc_handle_mac_addr_msg(vf, msg, true);
2131 * ice_vc_del_mac_addr_msg
2132 * @vf: pointer to the VF info
2133 * @msg: pointer to the msg buffer
2135 * remove guest MAC address filter
2137 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2139 return ice_vc_handle_mac_addr_msg(vf, msg, false);
2143 * ice_vc_request_qs_msg
2144 * @vf: pointer to the VF info
2145 * @msg: pointer to the msg buffer
2147 * VFs get a default number of queues but can use this message to request a
2148 * different number. If the request is successful, PF will reset the VF and
2149 * return 0. If unsuccessful, PF will send message informing VF of number of
2150 * available queue pairs via virtchnl message response to VF.
2152 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2154 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2155 struct virtchnl_vf_res_request *vfres =
2156 (struct virtchnl_vf_res_request *)msg;
2157 int req_queues = vfres->num_queue_pairs;
2158 struct ice_pf *pf = vf->pf;
2159 int max_allowed_vf_queues;
2160 int tx_rx_queue_left;
2163 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2164 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2168 cur_queues = vf->num_vf_qs;
2169 tx_rx_queue_left = min_t(int, pf->q_left_tx, pf->q_left_rx);
2170 max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2171 if (req_queues <= 0) {
2172 dev_err(&pf->pdev->dev,
2173 "VF %d tried to request %d queues. Ignoring.\n",
2174 vf->vf_id, req_queues);
2175 } else if (req_queues > ICE_MAX_BASE_QS_PER_VF) {
2176 dev_err(&pf->pdev->dev,
2177 "VF %d tried to request more than %d queues.\n",
2178 vf->vf_id, ICE_MAX_BASE_QS_PER_VF);
2179 vfres->num_queue_pairs = ICE_MAX_BASE_QS_PER_VF;
2180 } else if (req_queues - cur_queues > tx_rx_queue_left) {
2181 dev_warn(&pf->pdev->dev,
2182 "VF %d requested %d more queues, but only %d left.\n",
2183 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2184 vfres->num_queue_pairs = min_t(int, max_allowed_vf_queues,
2185 ICE_MAX_BASE_QS_PER_VF);
2187 /* request is successful, then reset VF */
2188 vf->num_req_qs = req_queues;
2190 dev_info(&pf->pdev->dev,
2191 "VF %d granted request of %d queues.\n",
2192 vf->vf_id, req_queues);
2197 /* send the response to the VF */
2198 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2199 v_ret, (u8 *)vfres, sizeof(*vfres));
2203 * ice_set_vf_port_vlan
2204 * @netdev: network interface device structure
2205 * @vf_id: VF identifier
2206 * @vlan_id: VLAN ID being set
2207 * @qos: priority setting
2208 * @vlan_proto: VLAN protocol
2210 * program VF Port VLAN ID and/or QoS
2213 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
2216 u16 vlanprio = vlan_id | (qos << ICE_VLAN_PRIORITY_S);
2217 struct ice_netdev_priv *np = netdev_priv(netdev);
2218 struct ice_pf *pf = np->vsi->back;
2219 struct ice_vsi *vsi;
2223 /* validate the request */
2224 if (vf_id >= pf->num_alloc_vfs) {
2225 dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
2229 if (vlan_id > ICE_MAX_VLANID || qos > 7) {
2230 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2234 if (vlan_proto != htons(ETH_P_8021Q)) {
2235 dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
2236 return -EPROTONOSUPPORT;
2239 vf = &pf->vf[vf_id];
2240 vsi = pf->vsi[vf->lan_vsi_idx];
2241 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2242 dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
2246 if (le16_to_cpu(vsi->info.pvid) == vlanprio) {
2247 /* duplicate request, so just return success */
2248 dev_info(&pf->pdev->dev,
2249 "Duplicate pvid %d request\n", vlanprio);
2253 /* If PVID, then remove all filters on the old VLAN */
2255 ice_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2258 if (vlan_id || qos) {
2259 ret = ice_vsi_manage_pvid(vsi, vlanprio, true);
2261 goto error_set_pvid;
2263 ice_vsi_manage_pvid(vsi, 0, false);
2268 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2269 vlan_id, qos, vf_id);
2271 /* add new VLAN filter for each MAC */
2272 ret = ice_vsi_add_vlan(vsi, vlan_id);
2274 goto error_set_pvid;
2277 /* The Port VLAN needs to be saved across resets the same as the
2278 * default LAN MAC address.
2280 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
2287 * ice_vc_process_vlan_msg
2288 * @vf: pointer to the VF info
2289 * @msg: pointer to the msg buffer
2290 * @add_v: Add VLAN if true, otherwise delete VLAN
2292 * Process virtchnl op to add or remove programmed guest VLAN ID
2294 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2296 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2297 struct virtchnl_vlan_filter_list *vfl =
2298 (struct virtchnl_vlan_filter_list *)msg;
2299 struct ice_pf *pf = vf->pf;
2300 bool vlan_promisc = false;
2301 struct ice_vsi *vsi;
2307 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2308 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2312 if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2313 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2317 if (add_v && !ice_is_vf_trusted(vf) &&
2318 vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
2319 dev_info(&pf->pdev->dev,
2320 "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2322 /* There is no need to let VF know about being not trusted,
2323 * so we can just return success message here
2328 for (i = 0; i < vfl->num_elements; i++) {
2329 if (vfl->vlan_id[i] > ICE_MAX_VLANID) {
2330 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2331 dev_err(&pf->pdev->dev,
2332 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2338 vsi = pf->vsi[vf->lan_vsi_idx];
2340 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2344 if (vsi->info.pvid) {
2345 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2349 if (ice_vsi_manage_vlan_stripping(vsi, add_v)) {
2350 dev_err(&pf->pdev->dev,
2351 "%sable VLAN stripping failed for VSI %i\n",
2352 add_v ? "en" : "dis", vsi->vsi_num);
2353 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2357 if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2358 test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2359 vlan_promisc = true;
2362 for (i = 0; i < vfl->num_elements; i++) {
2363 u16 vid = vfl->vlan_id[i];
2365 if (!ice_is_vf_trusted(vf) &&
2366 vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
2367 dev_info(&pf->pdev->dev,
2368 "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2370 /* There is no need to let VF know about being
2371 * not trusted, so we can just return success
2372 * message here as well.
2377 if (ice_vsi_add_vlan(vsi, vid)) {
2378 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2383 /* Enable VLAN pruning when VLAN is added */
2384 if (!vlan_promisc) {
2385 status = ice_cfg_vlan_pruning(vsi, true, false);
2387 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2388 dev_err(&pf->pdev->dev,
2389 "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2394 /* Enable Ucast/Mcast VLAN promiscuous mode */
2395 promisc_m = ICE_PROMISC_VLAN_TX |
2396 ICE_PROMISC_VLAN_RX;
2398 status = ice_set_vsi_promisc(hw, vsi->idx,
2401 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2402 dev_err(&pf->pdev->dev,
2403 "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2409 /* In case of non_trusted VF, number of VLAN elements passed
2410 * to PF for removal might be greater than number of VLANs
2411 * filter programmed for that VF - So, use actual number of
2412 * VLANS added earlier with add VLAN opcode. In order to avoid
2413 * removing VLAN that doesn't exist, which result to sending
2414 * erroneous failed message back to the VF
2418 num_vf_vlan = vf->num_vlan;
2419 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
2420 u16 vid = vfl->vlan_id[i];
2422 /* Make sure ice_vsi_kill_vlan is successful before
2423 * updating VLAN information
2425 if (ice_vsi_kill_vlan(vsi, vid)) {
2426 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2431 /* Disable VLAN pruning when removing VLAN */
2432 ice_cfg_vlan_pruning(vsi, false, false);
2434 /* Disable Unicast/Multicast VLAN promiscuous mode */
2436 promisc_m = ICE_PROMISC_VLAN_TX |
2437 ICE_PROMISC_VLAN_RX;
2439 ice_clear_vsi_promisc(hw, vsi->idx,
2446 /* send the response to the VF */
2448 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
2451 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
2456 * ice_vc_add_vlan_msg
2457 * @vf: pointer to the VF info
2458 * @msg: pointer to the msg buffer
2460 * Add and program guest VLAN ID
2462 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2464 return ice_vc_process_vlan_msg(vf, msg, true);
2468 * ice_vc_remove_vlan_msg
2469 * @vf: pointer to the VF info
2470 * @msg: pointer to the msg buffer
2472 * remove programmed guest VLAN ID
2474 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2476 return ice_vc_process_vlan_msg(vf, msg, false);
2480 * ice_vc_ena_vlan_stripping
2481 * @vf: pointer to the VF info
2483 * Enable VLAN header stripping for a given VF
2485 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2487 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2488 struct ice_pf *pf = vf->pf;
2489 struct ice_vsi *vsi;
2491 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2492 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2496 vsi = pf->vsi[vf->lan_vsi_idx];
2497 if (ice_vsi_manage_vlan_stripping(vsi, true))
2498 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2501 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2506 * ice_vc_dis_vlan_stripping
2507 * @vf: pointer to the VF info
2509 * Disable VLAN header stripping for a given VF
2511 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2513 enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2514 struct ice_pf *pf = vf->pf;
2515 struct ice_vsi *vsi;
2517 if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2518 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2522 vsi = pf->vsi[vf->lan_vsi_idx];
2524 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2528 if (ice_vsi_manage_vlan_stripping(vsi, false))
2529 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2532 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2537 * ice_vc_process_vf_msg - Process request from VF
2538 * @pf: pointer to the PF structure
2539 * @event: pointer to the AQ event
2541 * called from the common asq/arq handler to
2542 * process request from VF
2544 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
2546 u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
2547 s16 vf_id = le16_to_cpu(event->desc.retval);
2548 u16 msglen = event->msg_len;
2549 u8 *msg = event->msg_buf;
2550 struct ice_vf *vf = NULL;
2553 if (vf_id >= pf->num_alloc_vfs) {
2558 vf = &pf->vf[vf_id];
2560 /* Check if VF is disabled. */
2561 if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
2566 /* Perform basic checks on the msg */
2567 err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
2569 if (err == VIRTCHNL_STATUS_ERR_PARAM)
2576 /* Perform additional checks specific to RSS and Virtchnl */
2577 if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
2578 struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
2580 if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE)
2582 } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
2583 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2585 if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE)
2591 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
2593 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
2594 vf_id, v_opcode, msglen, err);
2599 case VIRTCHNL_OP_VERSION:
2600 err = ice_vc_get_ver_msg(vf, msg);
2602 case VIRTCHNL_OP_GET_VF_RESOURCES:
2603 err = ice_vc_get_vf_res_msg(vf, msg);
2605 case VIRTCHNL_OP_RESET_VF:
2606 ice_vc_reset_vf_msg(vf);
2608 case VIRTCHNL_OP_ADD_ETH_ADDR:
2609 err = ice_vc_add_mac_addr_msg(vf, msg);
2611 case VIRTCHNL_OP_DEL_ETH_ADDR:
2612 err = ice_vc_del_mac_addr_msg(vf, msg);
2614 case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2615 err = ice_vc_cfg_qs_msg(vf, msg);
2617 case VIRTCHNL_OP_ENABLE_QUEUES:
2618 err = ice_vc_ena_qs_msg(vf, msg);
2619 ice_vc_notify_vf_link_state(vf);
2621 case VIRTCHNL_OP_DISABLE_QUEUES:
2622 err = ice_vc_dis_qs_msg(vf, msg);
2624 case VIRTCHNL_OP_REQUEST_QUEUES:
2625 err = ice_vc_request_qs_msg(vf, msg);
2627 case VIRTCHNL_OP_CONFIG_IRQ_MAP:
2628 err = ice_vc_cfg_irq_map_msg(vf, msg);
2630 case VIRTCHNL_OP_CONFIG_RSS_KEY:
2631 err = ice_vc_config_rss_key(vf, msg);
2633 case VIRTCHNL_OP_CONFIG_RSS_LUT:
2634 err = ice_vc_config_rss_lut(vf, msg);
2636 case VIRTCHNL_OP_GET_STATS:
2637 err = ice_vc_get_stats_msg(vf, msg);
2639 case VIRTCHNL_OP_ADD_VLAN:
2640 err = ice_vc_add_vlan_msg(vf, msg);
2642 case VIRTCHNL_OP_DEL_VLAN:
2643 err = ice_vc_remove_vlan_msg(vf, msg);
2645 case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
2646 err = ice_vc_ena_vlan_stripping(vf);
2648 case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
2649 err = ice_vc_dis_vlan_stripping(vf);
2651 case VIRTCHNL_OP_UNKNOWN:
2653 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
2655 err = ice_vc_send_msg_to_vf(vf, v_opcode,
2656 VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
2661 /* Helper function cares less about error return values here
2662 * as it is busy with pending work.
2664 dev_info(&pf->pdev->dev,
2665 "PF failed to honor VF %d, opcode %d, error %d\n",
2666 vf_id, v_opcode, err);
2672 * @netdev: network interface device structure
2673 * @vf_id: VF identifier
2674 * @ivi: VF configuration structure
2676 * return VF configuration
2679 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
2681 struct ice_netdev_priv *np = netdev_priv(netdev);
2682 struct ice_vsi *vsi = np->vsi;
2683 struct ice_pf *pf = vsi->back;
2686 /* validate the request */
2687 if (vf_id >= pf->num_alloc_vfs) {
2688 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
2692 vf = &pf->vf[vf_id];
2693 vsi = pf->vsi[vf->lan_vsi_idx];
2695 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2696 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
2701 ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
2703 /* VF configuration for VLAN and applicable QoS */
2704 ivi->vlan = le16_to_cpu(vsi->info.pvid) & ICE_VLAN_M;
2705 ivi->qos = (le16_to_cpu(vsi->info.pvid) & ICE_PRIORITY_M) >>
2706 ICE_VLAN_PRIORITY_S;
2708 ivi->trusted = vf->trusted;
2709 ivi->spoofchk = vf->spoofchk;
2710 if (!vf->link_forced)
2711 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2712 else if (vf->link_up)
2713 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2715 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
2716 ivi->max_tx_rate = vf->tx_rate;
2717 ivi->min_tx_rate = 0;
2722 * ice_set_vf_spoofchk
2723 * @netdev: network interface device structure
2724 * @vf_id: VF identifier
2725 * @ena: flag to enable or disable feature
2727 * Enable or disable VF spoof checking
2729 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
2731 struct ice_netdev_priv *np = netdev_priv(netdev);
2732 struct ice_vsi *vsi = np->vsi;
2733 struct ice_pf *pf = vsi->back;
2734 struct ice_vsi_ctx *ctx;
2735 enum ice_status status;
2739 /* validate the request */
2740 if (vf_id >= pf->num_alloc_vfs) {
2741 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
2745 vf = &pf->vf[vf_id];
2746 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2747 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
2751 if (ena == vf->spoofchk) {
2752 dev_dbg(&pf->pdev->dev, "VF spoofchk already %s\n",
2753 ena ? "ON" : "OFF");
2757 ctx = devm_kzalloc(&pf->pdev->dev, sizeof(*ctx), GFP_KERNEL);
2761 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2764 ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
2765 ctx->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_PRUNE_EN_M;
2768 status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
2770 dev_dbg(&pf->pdev->dev,
2771 "Error %d, failed to update VSI* parameters\n", status);
2777 vsi->info.sec_flags = ctx->info.sec_flags;
2778 vsi->info.sw_flags2 = ctx->info.sw_flags2;
2780 devm_kfree(&pf->pdev->dev, ctx);
2786 * @netdev: network interface device structure
2787 * @vf_id: VF identifier
2790 * program VF MAC address
2792 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2794 struct ice_netdev_priv *np = netdev_priv(netdev);
2795 struct ice_vsi *vsi = np->vsi;
2796 struct ice_pf *pf = vsi->back;
2800 /* validate the request */
2801 if (vf_id >= pf->num_alloc_vfs) {
2802 netdev_err(netdev, "invalid VF id: %d\n", vf_id);
2806 vf = &pf->vf[vf_id];
2807 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2808 netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
2812 if (is_zero_ether_addr(mac) || is_multicast_ether_addr(mac)) {
2813 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
2817 /* copy MAC into dflt_lan_addr and trigger a VF reset. The reset
2818 * flow will use the updated dflt_lan_addr and add a MAC filter
2819 * using ice_add_mac. Also set pf_set_mac to indicate that the PF has
2820 * set the MAC address for this VF.
2822 ether_addr_copy(vf->dflt_lan_addr.addr, mac);
2823 vf->pf_set_mac = true;
2825 "MAC on VF %d set to %pM. VF driver will be reinitialized\n",
2834 * @netdev: network interface device structure
2835 * @vf_id: VF identifier
2836 * @trusted: Boolean value to enable/disable trusted VF
2838 * Enable or disable a given VF as trusted
2840 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
2842 struct ice_netdev_priv *np = netdev_priv(netdev);
2843 struct ice_vsi *vsi = np->vsi;
2844 struct ice_pf *pf = vsi->back;
2847 /* validate the request */
2848 if (vf_id >= pf->num_alloc_vfs) {
2849 dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
2853 vf = &pf->vf[vf_id];
2854 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2855 dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
2859 /* Check if already trusted */
2860 if (trusted == vf->trusted)
2863 vf->trusted = trusted;
2865 dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
2866 vf_id, trusted ? "" : "un");
2872 * ice_set_vf_link_state
2873 * @netdev: network interface device structure
2874 * @vf_id: VF identifier
2875 * @link_state: required link state
2877 * Set VF's link state, irrespective of physical link state status
2879 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
2881 struct ice_netdev_priv *np = netdev_priv(netdev);
2882 struct ice_pf *pf = np->vsi->back;
2883 struct virtchnl_pf_event pfe = { 0 };
2884 struct ice_link_status *ls;
2888 if (vf_id >= pf->num_alloc_vfs) {
2889 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2893 vf = &pf->vf[vf_id];
2895 ls = &pf->hw.port_info->phy.link_info;
2897 if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
2898 dev_err(&pf->pdev->dev, "vf %d in reset. Try again.\n", vf_id);
2902 pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
2903 pfe.severity = PF_EVENT_SEVERITY_INFO;
2905 switch (link_state) {
2906 case IFLA_VF_LINK_STATE_AUTO:
2907 vf->link_forced = false;
2908 vf->link_up = ls->link_info & ICE_AQ_LINK_UP;
2910 case IFLA_VF_LINK_STATE_ENABLE:
2911 vf->link_forced = true;
2914 case IFLA_VF_LINK_STATE_DISABLE:
2915 vf->link_forced = true;
2916 vf->link_up = false;
2922 if (vf->link_forced)
2923 ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
2925 ice_set_pfe_link(vf, &pfe, ls->link_speed, vf->link_up);
2927 /* Notify the VF of its new link state */
2928 ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
2929 VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,