Merge tag 'sound-5.7-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / ice / ice_virtchnl_pf.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 #include "ice.h"
5 #include "ice_base.h"
6 #include "ice_lib.h"
7
8 /**
9  * ice_validate_vf_id - helper to check if VF ID is valid
10  * @pf: pointer to the PF structure
11  * @vf_id: the ID of the VF to check
12  */
13 static int ice_validate_vf_id(struct ice_pf *pf, int vf_id)
14 {
15         if (vf_id >= pf->num_alloc_vfs) {
16                 dev_err(ice_pf_to_dev(pf), "Invalid VF ID: %d\n", vf_id);
17                 return -EINVAL;
18         }
19         return 0;
20 }
21
22 /**
23  * ice_check_vf_init - helper to check if VF init complete
24  * @pf: pointer to the PF structure
25  * @vf: the pointer to the VF to check
26  */
27 static int ice_check_vf_init(struct ice_pf *pf, struct ice_vf *vf)
28 {
29         if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
30                 dev_err(ice_pf_to_dev(pf), "VF ID: %d in reset. Try again.\n",
31                         vf->vf_id);
32                 return -EBUSY;
33         }
34         return 0;
35 }
36
37 /**
38  * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
39  * @pf: pointer to the PF structure
40  * @v_opcode: operation code
41  * @v_retval: return value
42  * @msg: pointer to the msg buffer
43  * @msglen: msg length
44  */
45 static void
46 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
47                     enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
48 {
49         struct ice_hw *hw = &pf->hw;
50         int i;
51
52         ice_for_each_vf(pf, i) {
53                 struct ice_vf *vf = &pf->vf[i];
54
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))
58                         continue;
59
60                 /* Ignore return value on purpose - a given VF may fail, but
61                  * we need to keep going and send to all of them
62                  */
63                 ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
64                                       msglen, NULL);
65         }
66 }
67
68 /**
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
74  */
75 static void
76 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
77                  int ice_link_speed, bool link_up)
78 {
79         if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
80                 pfe->event_data.link_event_adv.link_status = link_up;
81                 /* Speed in Mbps */
82                 pfe->event_data.link_event_adv.link_speed =
83                         ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
84         } else {
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);
90         }
91 }
92
93 /**
94  * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
95  * @vf: the VF to check
96  *
97  * Returns true if the VF has no Rx and no Tx queues enabled and returns false
98  * otherwise
99  */
100 static bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
101 {
102         return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
103                 !bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
104 }
105
106 /**
107  * ice_is_vf_link_up - check if the VF's link is up
108  * @vf: VF to check if link is up
109  */
110 static bool ice_is_vf_link_up(struct ice_vf *vf)
111 {
112         struct ice_pf *pf = vf->pf;
113
114         if (ice_check_vf_init(pf, vf))
115                 return false;
116
117         if (ice_vf_has_no_qs_ena(vf))
118                 return false;
119         else if (vf->link_forced)
120                 return vf->link_up;
121         else
122                 return pf->hw.port_info->phy.link_info.link_info &
123                         ICE_AQ_LINK_UP;
124 }
125
126 /**
127  * ice_vc_notify_vf_link_state - Inform a VF of link status
128  * @vf: pointer to the VF structure
129  *
130  * send a link status message to a single VF
131  */
132 static void ice_vc_notify_vf_link_state(struct ice_vf *vf)
133 {
134         struct virtchnl_pf_event pfe = { 0 };
135         struct ice_hw *hw = &vf->pf->hw;
136
137         pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
138         pfe.severity = PF_EVENT_SEVERITY_INFO;
139
140         if (ice_is_vf_link_up(vf))
141                 ice_set_pfe_link(vf, &pfe,
142                                  hw->port_info->phy.link_info.link_speed, true);
143         else
144                 ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
145
146         ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
147                               VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
148                               sizeof(pfe), NULL);
149 }
150
151 /**
152  * ice_free_vf_res - Free a VF's resources
153  * @vf: pointer to the VF info
154  */
155 static void ice_free_vf_res(struct ice_vf *vf)
156 {
157         struct ice_pf *pf = vf->pf;
158         int i, last_vector_idx;
159
160         /* First, disable VF's configuration API to prevent OS from
161          * accessing the VF's VSI after it's freed or invalidated.
162          */
163         clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
164
165         /* free VSI and disconnect it from the parent uplink */
166         if (vf->lan_vsi_idx) {
167                 ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
168                 vf->lan_vsi_idx = 0;
169                 vf->lan_vsi_num = 0;
170                 vf->num_mac = 0;
171         }
172
173         last_vector_idx = vf->first_vector_idx + pf->num_msix_per_vf - 1;
174
175         /* clear VF MDD event information */
176         memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
177         memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
178
179         /* Disable interrupts so that VF starts in a known state */
180         for (i = vf->first_vector_idx; i <= last_vector_idx; i++) {
181                 wr32(&pf->hw, GLINT_DYN_CTL(i), GLINT_DYN_CTL_CLEARPBA_M);
182                 ice_flush(&pf->hw);
183         }
184         /* reset some of the state variables keeping track of the resources */
185         clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
186         clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
187 }
188
189 /**
190  * ice_dis_vf_mappings
191  * @vf: pointer to the VF structure
192  */
193 static void ice_dis_vf_mappings(struct ice_vf *vf)
194 {
195         struct ice_pf *pf = vf->pf;
196         struct ice_vsi *vsi;
197         struct device *dev;
198         int first, last, v;
199         struct ice_hw *hw;
200
201         hw = &pf->hw;
202         vsi = pf->vsi[vf->lan_vsi_idx];
203
204         dev = ice_pf_to_dev(pf);
205         wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
206         wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), 0);
207
208         first = vf->first_vector_idx;
209         last = first + pf->num_msix_per_vf - 1;
210         for (v = first; v <= last; v++) {
211                 u32 reg;
212
213                 reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
214                         GLINT_VECT2FUNC_IS_PF_M) |
215                        ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
216                         GLINT_VECT2FUNC_PF_NUM_M));
217                 wr32(hw, GLINT_VECT2FUNC(v), reg);
218         }
219
220         if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
221                 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
222         else
223                 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
224
225         if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
226                 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
227         else
228                 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
229 }
230
231 /**
232  * ice_sriov_free_msix_res - Reset/free any used MSIX resources
233  * @pf: pointer to the PF structure
234  *
235  * Since no MSIX entries are taken from the pf->irq_tracker then just clear
236  * the pf->sriov_base_vector.
237  *
238  * Returns 0 on success, and -EINVAL on error.
239  */
240 static int ice_sriov_free_msix_res(struct ice_pf *pf)
241 {
242         struct ice_res_tracker *res;
243
244         if (!pf)
245                 return -EINVAL;
246
247         res = pf->irq_tracker;
248         if (!res)
249                 return -EINVAL;
250
251         /* give back irq_tracker resources used */
252         WARN_ON(pf->sriov_base_vector < res->num_entries);
253
254         pf->sriov_base_vector = 0;
255
256         return 0;
257 }
258
259 /**
260  * ice_set_vf_state_qs_dis - Set VF queues state to disabled
261  * @vf: pointer to the VF structure
262  */
263 void ice_set_vf_state_qs_dis(struct ice_vf *vf)
264 {
265         /* Clear Rx/Tx enabled queues flag */
266         bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
267         bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
268         clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
269 }
270
271 /**
272  * ice_dis_vf_qs - Disable the VF queues
273  * @vf: pointer to the VF structure
274  */
275 static void ice_dis_vf_qs(struct ice_vf *vf)
276 {
277         struct ice_pf *pf = vf->pf;
278         struct ice_vsi *vsi;
279
280         vsi = pf->vsi[vf->lan_vsi_idx];
281
282         ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
283         ice_vsi_stop_all_rx_rings(vsi);
284         ice_set_vf_state_qs_dis(vf);
285 }
286
287 /**
288  * ice_free_vfs - Free all VFs
289  * @pf: pointer to the PF structure
290  */
291 void ice_free_vfs(struct ice_pf *pf)
292 {
293         struct device *dev = ice_pf_to_dev(pf);
294         struct ice_hw *hw = &pf->hw;
295         int tmp, i;
296
297         if (!pf->vf)
298                 return;
299
300         while (test_and_set_bit(__ICE_VF_DIS, pf->state))
301                 usleep_range(1000, 2000);
302
303         /* Disable IOV before freeing resources. This lets any VF drivers
304          * running in the host get themselves cleaned up before we yank
305          * the carpet out from underneath their feet.
306          */
307         if (!pci_vfs_assigned(pf->pdev))
308                 pci_disable_sriov(pf->pdev);
309         else
310                 dev_warn(dev, "VFs are assigned - not disabling SR-IOV\n");
311
312         /* Avoid wait time by stopping all VFs at the same time */
313         ice_for_each_vf(pf, i)
314                 if (test_bit(ICE_VF_STATE_QS_ENA, pf->vf[i].vf_states))
315                         ice_dis_vf_qs(&pf->vf[i]);
316
317         tmp = pf->num_alloc_vfs;
318         pf->num_qps_per_vf = 0;
319         pf->num_alloc_vfs = 0;
320         for (i = 0; i < tmp; i++) {
321                 if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
322                         /* disable VF qp mappings and set VF disable state */
323                         ice_dis_vf_mappings(&pf->vf[i]);
324                         set_bit(ICE_VF_STATE_DIS, pf->vf[i].vf_states);
325                         ice_free_vf_res(&pf->vf[i]);
326                 }
327         }
328
329         if (ice_sriov_free_msix_res(pf))
330                 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
331
332         devm_kfree(dev, pf->vf);
333         pf->vf = NULL;
334
335         /* This check is for when the driver is unloaded while VFs are
336          * assigned. Setting the number of VFs to 0 through sysfs is caught
337          * before this function ever gets called.
338          */
339         if (!pci_vfs_assigned(pf->pdev)) {
340                 int vf_id;
341
342                 /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
343                  * work correctly when SR-IOV gets re-enabled.
344                  */
345                 for (vf_id = 0; vf_id < tmp; vf_id++) {
346                         u32 reg_idx, bit_idx;
347
348                         reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
349                         bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
350                         wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
351                 }
352         }
353         clear_bit(__ICE_VF_DIS, pf->state);
354         clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
355 }
356
357 /**
358  * ice_trigger_vf_reset - Reset a VF on HW
359  * @vf: pointer to the VF structure
360  * @is_vflr: true if VFLR was issued, false if not
361  * @is_pfr: true if the reset was triggered due to a previous PFR
362  *
363  * Trigger hardware to start a reset for a particular VF. Expects the caller
364  * to wait the proper amount of time to allow hardware to reset the VF before
365  * it cleans up and restores VF functionality.
366  */
367 static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
368 {
369         struct ice_pf *pf = vf->pf;
370         u32 reg, reg_idx, bit_idx;
371         struct device *dev;
372         struct ice_hw *hw;
373         int vf_abs_id, i;
374
375         dev = ice_pf_to_dev(pf);
376         hw = &pf->hw;
377         vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
378
379         /* Inform VF that it is no longer active, as a warning */
380         clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
381
382         /* Disable VF's configuration API during reset. The flag is re-enabled
383          * in ice_alloc_vf_res(), when it's safe again to access VF's VSI.
384          * It's normally disabled in ice_free_vf_res(), but it's safer
385          * to do it earlier to give some time to finish to any VF config
386          * functions that may still be running at this point.
387          */
388         clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
389
390         /* VF_MBX_ARQLEN is cleared by PFR, so the driver needs to clear it
391          * in the case of VFR. If this is done for PFR, it can mess up VF
392          * resets because the VF driver may already have started cleanup
393          * by the time we get here.
394          */
395         if (!is_pfr)
396                 wr32(hw, VF_MBX_ARQLEN(vf->vf_id), 0);
397
398         /* In the case of a VFLR, the HW has already reset the VF and we
399          * just need to clean up, so don't hit the VFRTRIG register.
400          */
401         if (!is_vflr) {
402                 /* reset VF using VPGEN_VFRTRIG reg */
403                 reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
404                 reg |= VPGEN_VFRTRIG_VFSWR_M;
405                 wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
406         }
407         /* clear the VFLR bit in GLGEN_VFLRSTAT */
408         reg_idx = (vf_abs_id) / 32;
409         bit_idx = (vf_abs_id) % 32;
410         wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
411         ice_flush(hw);
412
413         wr32(hw, PF_PCI_CIAA,
414              VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
415         for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
416                 reg = rd32(hw, PF_PCI_CIAD);
417                 /* no transactions pending so stop polling */
418                 if ((reg & VF_TRANS_PENDING_M) == 0)
419                         break;
420
421                 dev_err(dev, "VF %d PCI transactions stuck\n", vf->vf_id);
422                 udelay(ICE_PCI_CIAD_WAIT_DELAY_US);
423         }
424 }
425
426 /**
427  * ice_vsi_manage_pvid - Enable or disable port VLAN for VSI
428  * @vsi: the VSI to update
429  * @pvid_info: VLAN ID and QoS used to set the PVID VSI context field
430  * @enable: true for enable PVID false for disable
431  */
432 static int ice_vsi_manage_pvid(struct ice_vsi *vsi, u16 pvid_info, bool enable)
433 {
434         struct ice_hw *hw = &vsi->back->hw;
435         struct ice_aqc_vsi_props *info;
436         struct ice_vsi_ctx *ctxt;
437         enum ice_status status;
438         int ret = 0;
439
440         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
441         if (!ctxt)
442                 return -ENOMEM;
443
444         ctxt->info = vsi->info;
445         info = &ctxt->info;
446         if (enable) {
447                 info->vlan_flags = ICE_AQ_VSI_VLAN_MODE_UNTAGGED |
448                         ICE_AQ_VSI_PVLAN_INSERT_PVID |
449                         ICE_AQ_VSI_VLAN_EMOD_STR;
450                 info->sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
451         } else {
452                 info->vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING |
453                         ICE_AQ_VSI_VLAN_MODE_ALL;
454                 info->sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
455         }
456
457         info->pvid = cpu_to_le16(pvid_info);
458         info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
459                                            ICE_AQ_VSI_PROP_SW_VALID);
460
461         status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
462         if (status) {
463                 dev_info(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %d aq_err %d\n",
464                          status, hw->adminq.sq_last_status);
465                 ret = -EIO;
466                 goto out;
467         }
468
469         vsi->info.vlan_flags = info->vlan_flags;
470         vsi->info.sw_flags2 = info->sw_flags2;
471         vsi->info.pvid = info->pvid;
472 out:
473         kfree(ctxt);
474         return ret;
475 }
476
477 /**
478  * ice_vf_vsi_setup - Set up a VF VSI
479  * @pf: board private structure
480  * @pi: pointer to the port_info instance
481  * @vf_id: defines VF ID to which this VSI connects.
482  *
483  * Returns pointer to the successfully allocated VSI struct on success,
484  * otherwise returns NULL on failure.
485  */
486 static struct ice_vsi *
487 ice_vf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi, u16 vf_id)
488 {
489         return ice_vsi_setup(pf, pi, ICE_VSI_VF, vf_id);
490 }
491
492 /**
493  * ice_calc_vf_first_vector_idx - Calculate MSIX vector index in the PF space
494  * @pf: pointer to PF structure
495  * @vf: pointer to VF that the first MSIX vector index is being calculated for
496  *
497  * This returns the first MSIX vector index in PF space that is used by this VF.
498  * This index is used when accessing PF relative registers such as
499  * GLINT_VECT2FUNC and GLINT_DYN_CTL.
500  * This will always be the OICR index in the AVF driver so any functionality
501  * using vf->first_vector_idx for queue configuration will have to increment by
502  * 1 to avoid meddling with the OICR index.
503  */
504 static int ice_calc_vf_first_vector_idx(struct ice_pf *pf, struct ice_vf *vf)
505 {
506         return pf->sriov_base_vector + vf->vf_id * pf->num_msix_per_vf;
507 }
508
509 /**
510  * ice_alloc_vsi_res - Setup VF VSI and its resources
511  * @vf: pointer to the VF structure
512  *
513  * Returns 0 on success, negative value on failure
514  */
515 static int ice_alloc_vsi_res(struct ice_vf *vf)
516 {
517         struct ice_pf *pf = vf->pf;
518         LIST_HEAD(tmp_add_list);
519         u8 broadcast[ETH_ALEN];
520         struct ice_vsi *vsi;
521         struct device *dev;
522         int status = 0;
523
524         dev = ice_pf_to_dev(pf);
525         /* first vector index is the VFs OICR index */
526         vf->first_vector_idx = ice_calc_vf_first_vector_idx(pf, vf);
527
528         vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
529         if (!vsi) {
530                 dev_err(dev, "Failed to create VF VSI\n");
531                 return -ENOMEM;
532         }
533
534         vf->lan_vsi_idx = vsi->idx;
535         vf->lan_vsi_num = vsi->vsi_num;
536
537         /* Check if port VLAN exist before, and restore it accordingly */
538         if (vf->port_vlan_info) {
539                 ice_vsi_manage_pvid(vsi, vf->port_vlan_info, true);
540                 if (ice_vsi_add_vlan(vsi, vf->port_vlan_info & VLAN_VID_MASK))
541                         dev_warn(ice_pf_to_dev(pf), "Failed to add Port VLAN %d filter for VF %d\n",
542                                  vf->port_vlan_info & VLAN_VID_MASK, vf->vf_id);
543         } else {
544                 /* set VLAN 0 filter by default when no port VLAN is
545                  * enabled. If a port VLAN is enabled we don't want
546                  * untagged broadcast/multicast traffic seen on the VF
547                  * interface.
548                  */
549                 if (ice_vsi_add_vlan(vsi, 0))
550                         dev_warn(ice_pf_to_dev(pf), "Failed to add VLAN 0 filter for VF %d, MDD events will trigger. Reset the VF, disable spoofchk, or enable 8021q module on the guest\n",
551                                  vf->vf_id);
552         }
553
554         eth_broadcast_addr(broadcast);
555
556         status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
557         if (status)
558                 goto ice_alloc_vsi_res_exit;
559
560         if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
561                 status = ice_add_mac_to_list(vsi, &tmp_add_list,
562                                              vf->dflt_lan_addr.addr);
563                 if (status)
564                         goto ice_alloc_vsi_res_exit;
565         }
566
567         status = ice_add_mac(&pf->hw, &tmp_add_list);
568         if (status)
569                 dev_err(dev, "could not add mac filters error %d\n", status);
570         else
571                 vf->num_mac = 1;
572
573         /* Clear this bit after VF initialization since we shouldn't reclaim
574          * and reassign interrupts for synchronous or asynchronous VFR events.
575          * We don't want to reconfigure interrupts since AVF driver doesn't
576          * expect vector assignment to be changed unless there is a request for
577          * more vectors.
578          */
579 ice_alloc_vsi_res_exit:
580         ice_free_fltr_list(dev, &tmp_add_list);
581         return status;
582 }
583
584 /**
585  * ice_alloc_vf_res - Allocate VF resources
586  * @vf: pointer to the VF structure
587  */
588 static int ice_alloc_vf_res(struct ice_vf *vf)
589 {
590         struct ice_pf *pf = vf->pf;
591         int tx_rx_queue_left;
592         int status;
593
594         /* Update number of VF queues, in case VF had requested for queue
595          * changes
596          */
597         tx_rx_queue_left = min_t(int, ice_get_avail_txq_count(pf),
598                                  ice_get_avail_rxq_count(pf));
599         tx_rx_queue_left += pf->num_qps_per_vf;
600         if (vf->num_req_qs && vf->num_req_qs <= tx_rx_queue_left &&
601             vf->num_req_qs != vf->num_vf_qs)
602                 vf->num_vf_qs = vf->num_req_qs;
603
604         /* setup VF VSI and necessary resources */
605         status = ice_alloc_vsi_res(vf);
606         if (status)
607                 goto ice_alloc_vf_res_exit;
608
609         if (vf->trusted)
610                 set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
611         else
612                 clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
613
614         /* VF is now completely initialized */
615         set_bit(ICE_VF_STATE_INIT, vf->vf_states);
616
617         return status;
618
619 ice_alloc_vf_res_exit:
620         ice_free_vf_res(vf);
621         return status;
622 }
623
624 /**
625  * ice_ena_vf_mappings
626  * @vf: pointer to the VF structure
627  *
628  * Enable VF vectors and queues allocation by writing the details into
629  * respective registers.
630  */
631 static void ice_ena_vf_mappings(struct ice_vf *vf)
632 {
633         int abs_vf_id, abs_first, abs_last;
634         struct ice_pf *pf = vf->pf;
635         struct ice_vsi *vsi;
636         struct device *dev;
637         int first, last, v;
638         struct ice_hw *hw;
639         u32 reg;
640
641         dev = ice_pf_to_dev(pf);
642         hw = &pf->hw;
643         vsi = pf->vsi[vf->lan_vsi_idx];
644         first = vf->first_vector_idx;
645         last = (first + pf->num_msix_per_vf) - 1;
646         abs_first = first + pf->hw.func_caps.common_cap.msix_vector_first_id;
647         abs_last = (abs_first + pf->num_msix_per_vf) - 1;
648         abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
649
650         /* VF Vector allocation */
651         reg = (((abs_first << VPINT_ALLOC_FIRST_S) & VPINT_ALLOC_FIRST_M) |
652                ((abs_last << VPINT_ALLOC_LAST_S) & VPINT_ALLOC_LAST_M) |
653                VPINT_ALLOC_VALID_M);
654         wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
655
656         reg = (((abs_first << VPINT_ALLOC_PCI_FIRST_S)
657                  & VPINT_ALLOC_PCI_FIRST_M) |
658                ((abs_last << VPINT_ALLOC_PCI_LAST_S) & VPINT_ALLOC_PCI_LAST_M) |
659                VPINT_ALLOC_PCI_VALID_M);
660         wr32(hw, VPINT_ALLOC_PCI(vf->vf_id), reg);
661         /* map the interrupts to its functions */
662         for (v = first; v <= last; v++) {
663                 reg = (((abs_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
664                         GLINT_VECT2FUNC_VF_NUM_M) |
665                        ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
666                         GLINT_VECT2FUNC_PF_NUM_M));
667                 wr32(hw, GLINT_VECT2FUNC(v), reg);
668         }
669
670         /* Map mailbox interrupt. We put an explicit 0 here to remind us that
671          * VF admin queue interrupts will go to VF MSI-X vector 0.
672          */
673         wr32(hw, VPINT_MBX_CTL(abs_vf_id), VPINT_MBX_CTL_CAUSE_ENA_M | 0);
674         /* set regardless of mapping mode */
675         wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id), VPLAN_TXQ_MAPENA_TX_ENA_M);
676
677         /* VF Tx queues allocation */
678         if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
679                 /* set the VF PF Tx queue range
680                  * VFNUMQ value should be set to (number of queues - 1). A value
681                  * of 0 means 1 queue and a value of 255 means 256 queues
682                  */
683                 reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
684                         VPLAN_TX_QBASE_VFFIRSTQ_M) |
685                        (((vsi->alloc_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
686                         VPLAN_TX_QBASE_VFNUMQ_M));
687                 wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
688         } else {
689                 dev_err(dev, "Scattered mode for VF Tx queues is not yet implemented\n");
690         }
691
692         /* set regardless of mapping mode */
693         wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id), VPLAN_RXQ_MAPENA_RX_ENA_M);
694
695         /* VF Rx queues allocation */
696         if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
697                 /* set the VF PF Rx queue range
698                  * VFNUMQ value should be set to (number of queues - 1). A value
699                  * of 0 means 1 queue and a value of 255 means 256 queues
700                  */
701                 reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
702                         VPLAN_RX_QBASE_VFFIRSTQ_M) |
703                        (((vsi->alloc_txq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
704                         VPLAN_RX_QBASE_VFNUMQ_M));
705                 wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
706         } else {
707                 dev_err(dev, "Scattered mode for VF Rx queues is not yet implemented\n");
708         }
709 }
710
711 /**
712  * ice_determine_res
713  * @pf: pointer to the PF structure
714  * @avail_res: available resources in the PF structure
715  * @max_res: maximum resources that can be given per VF
716  * @min_res: minimum resources that can be given per VF
717  *
718  * Returns non-zero value if resources (queues/vectors) are available or
719  * returns zero if PF cannot accommodate for all num_alloc_vfs.
720  */
721 static int
722 ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
723 {
724         bool checked_min_res = false;
725         int res;
726
727         /* start by checking if PF can assign max number of resources for
728          * all num_alloc_vfs.
729          * if yes, return number per VF
730          * If no, divide by 2 and roundup, check again
731          * repeat the loop till we reach a point where even minimum resources
732          * are not available, in that case return 0
733          */
734         res = max_res;
735         while ((res >= min_res) && !checked_min_res) {
736                 int num_all_res;
737
738                 num_all_res = pf->num_alloc_vfs * res;
739                 if (num_all_res <= avail_res)
740                         return res;
741
742                 if (res == min_res)
743                         checked_min_res = true;
744
745                 res = DIV_ROUND_UP(res, 2);
746         }
747         return 0;
748 }
749
750 /**
751  * ice_calc_vf_reg_idx - Calculate the VF's register index in the PF space
752  * @vf: VF to calculate the register index for
753  * @q_vector: a q_vector associated to the VF
754  */
755 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector)
756 {
757         struct ice_pf *pf;
758
759         if (!vf || !q_vector)
760                 return -EINVAL;
761
762         pf = vf->pf;
763
764         /* always add one to account for the OICR being the first MSIX */
765         return pf->sriov_base_vector + pf->num_msix_per_vf * vf->vf_id +
766                 q_vector->v_idx + 1;
767 }
768
769 /**
770  * ice_get_max_valid_res_idx - Get the max valid resource index
771  * @res: pointer to the resource to find the max valid index for
772  *
773  * Start from the end of the ice_res_tracker and return right when we find the
774  * first res->list entry with the ICE_RES_VALID_BIT set. This function is only
775  * valid for SR-IOV because it is the only consumer that manipulates the
776  * res->end and this is always called when res->end is set to res->num_entries.
777  */
778 static int ice_get_max_valid_res_idx(struct ice_res_tracker *res)
779 {
780         int i;
781
782         if (!res)
783                 return -EINVAL;
784
785         for (i = res->num_entries - 1; i >= 0; i--)
786                 if (res->list[i] & ICE_RES_VALID_BIT)
787                         return i;
788
789         return 0;
790 }
791
792 /**
793  * ice_sriov_set_msix_res - Set any used MSIX resources
794  * @pf: pointer to PF structure
795  * @num_msix_needed: number of MSIX vectors needed for all SR-IOV VFs
796  *
797  * This function allows SR-IOV resources to be taken from the end of the PF's
798  * allowed HW MSIX vectors so that the irq_tracker will not be affected. We
799  * just set the pf->sriov_base_vector and return success.
800  *
801  * If there are not enough resources available, return an error. This should
802  * always be caught by ice_set_per_vf_res().
803  *
804  * Return 0 on success, and -EINVAL when there are not enough MSIX vectors in
805  * in the PF's space available for SR-IOV.
806  */
807 static int ice_sriov_set_msix_res(struct ice_pf *pf, u16 num_msix_needed)
808 {
809         u16 total_vectors = pf->hw.func_caps.common_cap.num_msix_vectors;
810         int vectors_used = pf->irq_tracker->num_entries;
811         int sriov_base_vector;
812
813         sriov_base_vector = total_vectors - num_msix_needed;
814
815         /* make sure we only grab irq_tracker entries from the list end and
816          * that we have enough available MSIX vectors
817          */
818         if (sriov_base_vector < vectors_used)
819                 return -EINVAL;
820
821         pf->sriov_base_vector = sriov_base_vector;
822
823         return 0;
824 }
825
826 /**
827  * ice_set_per_vf_res - check if vectors and queues are available
828  * @pf: pointer to the PF structure
829  *
830  * First, determine HW interrupts from common pool. If we allocate fewer VFs, we
831  * get more vectors and can enable more queues per VF. Note that this does not
832  * grab any vectors from the SW pool already allocated. Also note, that all
833  * vector counts include one for each VF's miscellaneous interrupt vector
834  * (i.e. OICR).
835  *
836  * Minimum VFs - 2 vectors, 1 queue pair
837  * Small VFs - 5 vectors, 4 queue pairs
838  * Medium VFs - 17 vectors, 16 queue pairs
839  *
840  * Second, determine number of queue pairs per VF by starting with a pre-defined
841  * maximum each VF supports. If this is not possible, then we adjust based on
842  * queue pairs available on the device.
843  *
844  * Lastly, set queue and MSI-X VF variables tracked by the PF so it can be used
845  * by each VF during VF initialization and reset.
846  */
847 static int ice_set_per_vf_res(struct ice_pf *pf)
848 {
849         int max_valid_res_idx = ice_get_max_valid_res_idx(pf->irq_tracker);
850         int msix_avail_per_vf, msix_avail_for_sriov;
851         struct device *dev = ice_pf_to_dev(pf);
852         u16 num_msix_per_vf, num_txq, num_rxq;
853
854         if (!pf->num_alloc_vfs || max_valid_res_idx < 0)
855                 return -EINVAL;
856
857         /* determine MSI-X resources per VF */
858         msix_avail_for_sriov = pf->hw.func_caps.common_cap.num_msix_vectors -
859                 pf->irq_tracker->num_entries;
860         msix_avail_per_vf = msix_avail_for_sriov / pf->num_alloc_vfs;
861         if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_MED) {
862                 num_msix_per_vf = ICE_NUM_VF_MSIX_MED;
863         } else if (msix_avail_per_vf >= ICE_NUM_VF_MSIX_SMALL) {
864                 num_msix_per_vf = ICE_NUM_VF_MSIX_SMALL;
865         } else if (msix_avail_per_vf >= ICE_MIN_INTR_PER_VF) {
866                 num_msix_per_vf = ICE_MIN_INTR_PER_VF;
867         } else {
868                 dev_err(dev, "Only %d MSI-X interrupts available for SR-IOV. Not enough to support minimum of %d MSI-X interrupts per VF for %d VFs\n",
869                         msix_avail_for_sriov, ICE_MIN_INTR_PER_VF,
870                         pf->num_alloc_vfs);
871                 return -EIO;
872         }
873
874         /* determine queue resources per VF */
875         num_txq = ice_determine_res(pf, ice_get_avail_txq_count(pf),
876                                     min_t(u16,
877                                           num_msix_per_vf - ICE_NONQ_VECS_VF,
878                                           ICE_MAX_RSS_QS_PER_VF),
879                                     ICE_MIN_QS_PER_VF);
880
881         num_rxq = ice_determine_res(pf, ice_get_avail_rxq_count(pf),
882                                     min_t(u16,
883                                           num_msix_per_vf - ICE_NONQ_VECS_VF,
884                                           ICE_MAX_RSS_QS_PER_VF),
885                                     ICE_MIN_QS_PER_VF);
886
887         if (!num_txq || !num_rxq) {
888                 dev_err(dev, "Not enough queues to support minimum of %d queue pairs per VF for %d VFs\n",
889                         ICE_MIN_QS_PER_VF, pf->num_alloc_vfs);
890                 return -EIO;
891         }
892
893         if (ice_sriov_set_msix_res(pf, num_msix_per_vf * pf->num_alloc_vfs)) {
894                 dev_err(dev, "Unable to set MSI-X resources for %d VFs\n",
895                         pf->num_alloc_vfs);
896                 return -EINVAL;
897         }
898
899         /* only allow equal Tx/Rx queue count (i.e. queue pairs) */
900         pf->num_qps_per_vf = min_t(int, num_txq, num_rxq);
901         pf->num_msix_per_vf = num_msix_per_vf;
902         dev_info(dev, "Enabling %d VFs with %d vectors and %d queues per VF\n",
903                  pf->num_alloc_vfs, pf->num_msix_per_vf, pf->num_qps_per_vf);
904
905         return 0;
906 }
907
908 /**
909  * ice_cleanup_and_realloc_vf - Clean up VF and reallocate resources after reset
910  * @vf: pointer to the VF structure
911  *
912  * Cleanup a VF after the hardware reset is finished. Expects the caller to
913  * have verified whether the reset is finished properly, and ensure the
914  * minimum amount of wait time has passed. Reallocate VF resources back to make
915  * VF state active
916  */
917 static void ice_cleanup_and_realloc_vf(struct ice_vf *vf)
918 {
919         struct ice_pf *pf = vf->pf;
920         struct ice_hw *hw;
921         u32 reg;
922
923         hw = &pf->hw;
924
925         /* PF software completes the flow by notifying VF that reset flow is
926          * completed. This is done by enabling hardware by clearing the reset
927          * bit in the VPGEN_VFRTRIG reg and setting VFR_STATE in the VFGEN_RSTAT
928          * register to VFR completed (done at the end of this function)
929          * By doing this we allow HW to access VF memory at any point. If we
930          * did it any sooner, HW could access memory while it was being freed
931          * in ice_free_vf_res(), causing an IOMMU fault.
932          *
933          * On the other hand, this needs to be done ASAP, because the VF driver
934          * is waiting for this to happen and may report a timeout. It's
935          * harmless, but it gets logged into Guest OS kernel log, so best avoid
936          * it.
937          */
938         reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
939         reg &= ~VPGEN_VFRTRIG_VFSWR_M;
940         wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
941
942         /* reallocate VF resources to finish resetting the VSI state */
943         if (!ice_alloc_vf_res(vf)) {
944                 ice_ena_vf_mappings(vf);
945                 set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
946                 clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
947         }
948
949         /* Tell the VF driver the reset is done. This needs to be done only
950          * after VF has been fully initialized, because the VF driver may
951          * request resources immediately after setting this flag.
952          */
953         wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
954 }
955
956 /**
957  * ice_vf_set_vsi_promisc - set given VF VSI to given promiscuous mode(s)
958  * @vf: pointer to the VF info
959  * @vsi: the VSI being configured
960  * @promisc_m: mask of promiscuous config bits
961  * @rm_promisc: promisc flag request from the VF to remove or add filter
962  *
963  * This function configures VF VSI promiscuous mode, based on the VF requests,
964  * for Unicast, Multicast and VLAN
965  */
966 static enum ice_status
967 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m,
968                        bool rm_promisc)
969 {
970         struct ice_pf *pf = vf->pf;
971         enum ice_status status = 0;
972         struct ice_hw *hw;
973
974         hw = &pf->hw;
975         if (vsi->num_vlan) {
976                 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
977                                                   rm_promisc);
978         } else if (vf->port_vlan_info) {
979                 if (rm_promisc)
980                         status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
981                                                        vf->port_vlan_info);
982                 else
983                         status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
984                                                      vf->port_vlan_info);
985         } else {
986                 if (rm_promisc)
987                         status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
988                                                        0);
989                 else
990                         status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
991                                                      0);
992         }
993
994         return status;
995 }
996
997 /**
998  * ice_config_res_vfs - Finalize allocation of VFs resources in one go
999  * @pf: pointer to the PF structure
1000  *
1001  * This function is being called as last part of resetting all VFs, or when
1002  * configuring VFs for the first time, where there is no resource to be freed
1003  * Returns true if resources were properly allocated for all VFs, and false
1004  * otherwise.
1005  */
1006 static bool ice_config_res_vfs(struct ice_pf *pf)
1007 {
1008         struct device *dev = ice_pf_to_dev(pf);
1009         struct ice_hw *hw = &pf->hw;
1010         int v;
1011
1012         if (ice_set_per_vf_res(pf)) {
1013                 dev_err(dev, "Cannot allocate VF resources, try with fewer number of VFs\n");
1014                 return false;
1015         }
1016
1017         /* rearm global interrupts */
1018         if (test_and_clear_bit(__ICE_OICR_INTR_DIS, pf->state))
1019                 ice_irq_dynamic_ena(hw, NULL, NULL);
1020
1021         /* Finish resetting each VF and allocate resources */
1022         ice_for_each_vf(pf, v) {
1023                 struct ice_vf *vf = &pf->vf[v];
1024
1025                 vf->num_vf_qs = pf->num_qps_per_vf;
1026                 dev_dbg(dev, "VF-id %d has %d queues configured\n", vf->vf_id,
1027                         vf->num_vf_qs);
1028                 ice_cleanup_and_realloc_vf(vf);
1029         }
1030
1031         ice_flush(hw);
1032         clear_bit(__ICE_VF_DIS, pf->state);
1033
1034         return true;
1035 }
1036
1037 /**
1038  * ice_reset_all_vfs - reset all allocated VFs in one go
1039  * @pf: pointer to the PF structure
1040  * @is_vflr: true if VFLR was issued, false if not
1041  *
1042  * First, tell the hardware to reset each VF, then do all the waiting in one
1043  * chunk, and finally finish restoring each VF after the wait. This is useful
1044  * during PF routines which need to reset all VFs, as otherwise it must perform
1045  * these resets in a serialized fashion.
1046  *
1047  * Returns true if any VFs were reset, and false otherwise.
1048  */
1049 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
1050 {
1051         struct device *dev = ice_pf_to_dev(pf);
1052         struct ice_hw *hw = &pf->hw;
1053         struct ice_vf *vf;
1054         int v, i;
1055
1056         /* If we don't have any VFs, then there is nothing to reset */
1057         if (!pf->num_alloc_vfs)
1058                 return false;
1059
1060         /* If VFs have been disabled, there is no need to reset */
1061         if (test_and_set_bit(__ICE_VF_DIS, pf->state))
1062                 return false;
1063
1064         /* Begin reset on all VFs at once */
1065         ice_for_each_vf(pf, v)
1066                 ice_trigger_vf_reset(&pf->vf[v], is_vflr, true);
1067
1068         ice_for_each_vf(pf, v) {
1069                 struct ice_vsi *vsi;
1070
1071                 vf = &pf->vf[v];
1072                 vsi = pf->vsi[vf->lan_vsi_idx];
1073                 if (test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states))
1074                         ice_dis_vf_qs(vf);
1075                 ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1076                                 NULL, ICE_VF_RESET, vf->vf_id, NULL);
1077         }
1078
1079         /* HW requires some time to make sure it can flush the FIFO for a VF
1080          * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1081          * sequence to make sure that it has completed. We'll keep track of
1082          * the VFs using a simple iterator that increments once that VF has
1083          * finished resetting.
1084          */
1085         for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1086                 /* Check each VF in sequence */
1087                 while (v < pf->num_alloc_vfs) {
1088                         u32 reg;
1089
1090                         vf = &pf->vf[v];
1091                         reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1092                         if (!(reg & VPGEN_VFRSTAT_VFRD_M)) {
1093                                 /* only delay if the check failed */
1094                                 usleep_range(10, 20);
1095                                 break;
1096                         }
1097
1098                         /* If the current VF has finished resetting, move on
1099                          * to the next VF in sequence.
1100                          */
1101                         v++;
1102                 }
1103         }
1104
1105         /* Display a warning if at least one VF didn't manage to reset in
1106          * time, but continue on with the operation.
1107          */
1108         if (v < pf->num_alloc_vfs)
1109                 dev_warn(dev, "VF reset check timeout\n");
1110
1111         /* free VF resources to begin resetting the VSI state */
1112         ice_for_each_vf(pf, v) {
1113                 vf = &pf->vf[v];
1114
1115                 ice_free_vf_res(vf);
1116
1117                 /* Free VF queues as well, and reallocate later.
1118                  * If a given VF has different number of queues
1119                  * configured, the request for update will come
1120                  * via mailbox communication.
1121                  */
1122                 vf->num_vf_qs = 0;
1123         }
1124
1125         if (ice_sriov_free_msix_res(pf))
1126                 dev_err(dev, "Failed to free MSIX resources used by SR-IOV\n");
1127
1128         if (!ice_config_res_vfs(pf))
1129                 return false;
1130
1131         return true;
1132 }
1133
1134 /**
1135  * ice_is_vf_disabled
1136  * @vf: pointer to the VF info
1137  *
1138  * Returns true if the PF or VF is disabled, false otherwise.
1139  */
1140 static bool ice_is_vf_disabled(struct ice_vf *vf)
1141 {
1142         struct ice_pf *pf = vf->pf;
1143
1144         /* If the PF has been disabled, there is no need resetting VF until
1145          * PF is active again. Similarly, if the VF has been disabled, this
1146          * means something else is resetting the VF, so we shouldn't continue.
1147          * Otherwise, set disable VF state bit for actual reset, and continue.
1148          */
1149         return (test_bit(__ICE_VF_DIS, pf->state) ||
1150                 test_bit(ICE_VF_STATE_DIS, vf->vf_states));
1151 }
1152
1153 /**
1154  * ice_reset_vf - Reset a particular VF
1155  * @vf: pointer to the VF structure
1156  * @is_vflr: true if VFLR was issued, false if not
1157  *
1158  * Returns true if the VF is currently in reset, resets successfully, or resets
1159  * are disabled and false otherwise.
1160  */
1161 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
1162 {
1163         struct ice_pf *pf = vf->pf;
1164         struct ice_vsi *vsi;
1165         struct device *dev;
1166         struct ice_hw *hw;
1167         bool rsd = false;
1168         u8 promisc_m;
1169         u32 reg;
1170         int i;
1171
1172         dev = ice_pf_to_dev(pf);
1173
1174         if (test_bit(__ICE_VF_RESETS_DISABLED, pf->state)) {
1175                 dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
1176                         vf->vf_id);
1177                 return true;
1178         }
1179
1180         if (ice_is_vf_disabled(vf)) {
1181                 dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
1182                         vf->vf_id);
1183                 return true;
1184         }
1185
1186         /* Set VF disable bit state here, before triggering reset */
1187         set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1188         ice_trigger_vf_reset(vf, is_vflr, false);
1189
1190         vsi = pf->vsi[vf->lan_vsi_idx];
1191
1192         if (test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states))
1193                 ice_dis_vf_qs(vf);
1194
1195         /* Call Disable LAN Tx queue AQ whether or not queues are
1196          * enabled. This is needed for successful completion of VFR.
1197          */
1198         ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
1199                         NULL, ICE_VF_RESET, vf->vf_id, NULL);
1200
1201         hw = &pf->hw;
1202         /* poll VPGEN_VFRSTAT reg to make sure
1203          * that reset is complete
1204          */
1205         for (i = 0; i < 10; i++) {
1206                 /* VF reset requires driver to first reset the VF and then
1207                  * poll the status register to make sure that the reset
1208                  * completed successfully.
1209                  */
1210                 reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
1211                 if (reg & VPGEN_VFRSTAT_VFRD_M) {
1212                         rsd = true;
1213                         break;
1214                 }
1215
1216                 /* only sleep if the reset is not done */
1217                 usleep_range(10, 20);
1218         }
1219
1220         /* Display a warning if VF didn't manage to reset in time, but need to
1221          * continue on with the operation.
1222          */
1223         if (!rsd)
1224                 dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
1225
1226         /* disable promiscuous modes in case they were enabled
1227          * ignore any error if disabling process failed
1228          */
1229         if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
1230             test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
1231                 if (vf->port_vlan_info || vsi->num_vlan)
1232                         promisc_m = ICE_UCAST_VLAN_PROMISC_BITS;
1233                 else
1234                         promisc_m = ICE_UCAST_PROMISC_BITS;
1235
1236                 vsi = pf->vsi[vf->lan_vsi_idx];
1237                 if (ice_vf_set_vsi_promisc(vf, vsi, promisc_m, true))
1238                         dev_err(dev, "disabling promiscuous mode failed\n");
1239         }
1240
1241         /* free VF resources to begin resetting the VSI state */
1242         ice_free_vf_res(vf);
1243
1244         ice_cleanup_and_realloc_vf(vf);
1245
1246         ice_flush(hw);
1247
1248         return true;
1249 }
1250
1251 /**
1252  * ice_vc_notify_link_state - Inform all VFs on a PF of link status
1253  * @pf: pointer to the PF structure
1254  */
1255 void ice_vc_notify_link_state(struct ice_pf *pf)
1256 {
1257         int i;
1258
1259         ice_for_each_vf(pf, i)
1260                 ice_vc_notify_vf_link_state(&pf->vf[i]);
1261 }
1262
1263 /**
1264  * ice_vc_notify_reset - Send pending reset message to all VFs
1265  * @pf: pointer to the PF structure
1266  *
1267  * indicate a pending reset to all VFs on a given PF
1268  */
1269 void ice_vc_notify_reset(struct ice_pf *pf)
1270 {
1271         struct virtchnl_pf_event pfe;
1272
1273         if (!pf->num_alloc_vfs)
1274                 return;
1275
1276         pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1277         pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1278         ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1279                             (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
1280 }
1281
1282 /**
1283  * ice_vc_notify_vf_reset - Notify VF of a reset event
1284  * @vf: pointer to the VF structure
1285  */
1286 static void ice_vc_notify_vf_reset(struct ice_vf *vf)
1287 {
1288         struct virtchnl_pf_event pfe;
1289         struct ice_pf *pf;
1290
1291         if (!vf)
1292                 return;
1293
1294         pf = vf->pf;
1295         if (ice_validate_vf_id(pf, vf->vf_id))
1296                 return;
1297
1298         /* Bail out if VF is in disabled state, neither initialized, nor active
1299          * state - otherwise proceed with notifications
1300          */
1301         if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
1302              !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
1303             test_bit(ICE_VF_STATE_DIS, vf->vf_states))
1304                 return;
1305
1306         pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1307         pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1308         ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT,
1309                               VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
1310                               NULL);
1311 }
1312
1313 /**
1314  * ice_alloc_vfs - Allocate and set up VFs resources
1315  * @pf: pointer to the PF structure
1316  * @num_alloc_vfs: number of VFs to allocate
1317  */
1318 static int ice_alloc_vfs(struct ice_pf *pf, u16 num_alloc_vfs)
1319 {
1320         struct device *dev = ice_pf_to_dev(pf);
1321         struct ice_hw *hw = &pf->hw;
1322         struct ice_vf *vfs;
1323         int i, ret;
1324
1325         /* Disable global interrupt 0 so we don't try to handle the VFLR. */
1326         wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1327              ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
1328         set_bit(__ICE_OICR_INTR_DIS, pf->state);
1329         ice_flush(hw);
1330
1331         ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1332         if (ret) {
1333                 pf->num_alloc_vfs = 0;
1334                 goto err_unroll_intr;
1335         }
1336         /* allocate memory */
1337         vfs = devm_kcalloc(dev, num_alloc_vfs, sizeof(*vfs), GFP_KERNEL);
1338         if (!vfs) {
1339                 ret = -ENOMEM;
1340                 goto err_pci_disable_sriov;
1341         }
1342         pf->vf = vfs;
1343         pf->num_alloc_vfs = num_alloc_vfs;
1344
1345         /* apply default profile */
1346         ice_for_each_vf(pf, i) {
1347                 vfs[i].pf = pf;
1348                 vfs[i].vf_sw_id = pf->first_sw;
1349                 vfs[i].vf_id = i;
1350
1351                 /* assign default capabilities */
1352                 set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1353                 vfs[i].spoofchk = true;
1354         }
1355
1356         /* VF resources get allocated with initialization */
1357         if (!ice_config_res_vfs(pf)) {
1358                 ret = -EIO;
1359                 goto err_unroll_sriov;
1360         }
1361
1362         return ret;
1363
1364 err_unroll_sriov:
1365         pf->vf = NULL;
1366         devm_kfree(dev, vfs);
1367         vfs = NULL;
1368         pf->num_alloc_vfs = 0;
1369 err_pci_disable_sriov:
1370         pci_disable_sriov(pf->pdev);
1371 err_unroll_intr:
1372         /* rearm interrupts here */
1373         ice_irq_dynamic_ena(hw, NULL, NULL);
1374         clear_bit(__ICE_OICR_INTR_DIS, pf->state);
1375         return ret;
1376 }
1377
1378 /**
1379  * ice_pf_state_is_nominal - checks the PF for nominal state
1380  * @pf: pointer to PF to check
1381  *
1382  * Check the PF's state for a collection of bits that would indicate
1383  * the PF is in a state that would inhibit normal operation for
1384  * driver functionality.
1385  *
1386  * Returns true if PF is in a nominal state.
1387  * Returns false otherwise
1388  */
1389 static bool ice_pf_state_is_nominal(struct ice_pf *pf)
1390 {
1391         DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
1392
1393         if (!pf)
1394                 return false;
1395
1396         bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
1397         if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
1398                 return false;
1399
1400         return true;
1401 }
1402
1403 /**
1404  * ice_pci_sriov_ena - Enable or change number of VFs
1405  * @pf: pointer to the PF structure
1406  * @num_vfs: number of VFs to allocate
1407  */
1408 static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
1409 {
1410         int pre_existing_vfs = pci_num_vf(pf->pdev);
1411         struct device *dev = ice_pf_to_dev(pf);
1412         int err;
1413
1414         if (!ice_pf_state_is_nominal(pf)) {
1415                 dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
1416                 return -EBUSY;
1417         }
1418
1419         if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
1420                 dev_err(dev, "This device is not capable of SR-IOV\n");
1421                 return -EOPNOTSUPP;
1422         }
1423
1424         if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1425                 ice_free_vfs(pf);
1426         else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1427                 return num_vfs;
1428
1429         if (num_vfs > pf->num_vfs_supported) {
1430                 dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
1431                         num_vfs, pf->num_vfs_supported);
1432                 return -EOPNOTSUPP;
1433         }
1434
1435         dev_info(dev, "Allocating %d VFs\n", num_vfs);
1436         err = ice_alloc_vfs(pf, num_vfs);
1437         if (err) {
1438                 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1439                 return err;
1440         }
1441
1442         set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
1443         return num_vfs;
1444 }
1445
1446 /**
1447  * ice_sriov_configure - Enable or change number of VFs via sysfs
1448  * @pdev: pointer to a pci_dev structure
1449  * @num_vfs: number of VFs to allocate
1450  *
1451  * This function is called when the user updates the number of VFs in sysfs.
1452  */
1453 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
1454 {
1455         struct ice_pf *pf = pci_get_drvdata(pdev);
1456         struct device *dev = ice_pf_to_dev(pf);
1457
1458         if (ice_is_safe_mode(pf)) {
1459                 dev_err(dev, "SR-IOV cannot be configured - Device is in Safe Mode\n");
1460                 return -EOPNOTSUPP;
1461         }
1462
1463         if (num_vfs)
1464                 return ice_pci_sriov_ena(pf, num_vfs);
1465
1466         if (!pci_vfs_assigned(pdev)) {
1467                 ice_free_vfs(pf);
1468         } else {
1469                 dev_err(dev, "can't free VFs because some are assigned to VMs.\n");
1470                 return -EBUSY;
1471         }
1472
1473         return 0;
1474 }
1475
1476 /**
1477  * ice_process_vflr_event - Free VF resources via IRQ calls
1478  * @pf: pointer to the PF structure
1479  *
1480  * called from the VFLR IRQ handler to
1481  * free up VF resources and state variables
1482  */
1483 void ice_process_vflr_event(struct ice_pf *pf)
1484 {
1485         struct ice_hw *hw = &pf->hw;
1486         int vf_id;
1487         u32 reg;
1488
1489         if (!test_and_clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
1490             !pf->num_alloc_vfs)
1491                 return;
1492
1493         ice_for_each_vf(pf, vf_id) {
1494                 struct ice_vf *vf = &pf->vf[vf_id];
1495                 u32 reg_idx, bit_idx;
1496
1497                 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1498                 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1499                 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1500                 reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
1501                 if (reg & BIT(bit_idx))
1502                         /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
1503                         ice_reset_vf(vf, true);
1504         }
1505 }
1506
1507 /**
1508  * ice_vc_reset_vf - Perform software reset on the VF after informing the AVF
1509  * @vf: pointer to the VF info
1510  */
1511 static void ice_vc_reset_vf(struct ice_vf *vf)
1512 {
1513         ice_vc_notify_vf_reset(vf);
1514         ice_reset_vf(vf, false);
1515 }
1516
1517 /**
1518  * ice_get_vf_from_pfq - get the VF who owns the PF space queue passed in
1519  * @pf: PF used to index all VFs
1520  * @pfq: queue index relative to the PF's function space
1521  *
1522  * If no VF is found who owns the pfq then return NULL, otherwise return a
1523  * pointer to the VF who owns the pfq
1524  */
1525 static struct ice_vf *ice_get_vf_from_pfq(struct ice_pf *pf, u16 pfq)
1526 {
1527         int vf_id;
1528
1529         ice_for_each_vf(pf, vf_id) {
1530                 struct ice_vf *vf = &pf->vf[vf_id];
1531                 struct ice_vsi *vsi;
1532                 u16 rxq_idx;
1533
1534                 vsi = pf->vsi[vf->lan_vsi_idx];
1535
1536                 ice_for_each_rxq(vsi, rxq_idx)
1537                         if (vsi->rxq_map[rxq_idx] == pfq)
1538                                 return vf;
1539         }
1540
1541         return NULL;
1542 }
1543
1544 /**
1545  * ice_globalq_to_pfq - convert from global queue index to PF space queue index
1546  * @pf: PF used for conversion
1547  * @globalq: global queue index used to convert to PF space queue index
1548  */
1549 static u32 ice_globalq_to_pfq(struct ice_pf *pf, u32 globalq)
1550 {
1551         return globalq - pf->hw.func_caps.common_cap.rxq_first_id;
1552 }
1553
1554 /**
1555  * ice_vf_lan_overflow_event - handle LAN overflow event for a VF
1556  * @pf: PF that the LAN overflow event happened on
1557  * @event: structure holding the event information for the LAN overflow event
1558  *
1559  * Determine if the LAN overflow event was caused by a VF queue. If it was not
1560  * caused by a VF, do nothing. If a VF caused this LAN overflow event trigger a
1561  * reset on the offending VF.
1562  */
1563 void
1564 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1565 {
1566         u32 gldcb_rtctq, queue;
1567         struct ice_vf *vf;
1568
1569         gldcb_rtctq = le32_to_cpu(event->desc.params.lan_overflow.prtdcb_ruptq);
1570         dev_dbg(ice_pf_to_dev(pf), "GLDCB_RTCTQ: 0x%08x\n", gldcb_rtctq);
1571
1572         /* event returns device global Rx queue number */
1573         queue = (gldcb_rtctq & GLDCB_RTCTQ_RXQNUM_M) >>
1574                 GLDCB_RTCTQ_RXQNUM_S;
1575
1576         vf = ice_get_vf_from_pfq(pf, ice_globalq_to_pfq(pf, queue));
1577         if (!vf)
1578                 return;
1579
1580         ice_vc_reset_vf(vf);
1581 }
1582
1583 /**
1584  * ice_vc_send_msg_to_vf - Send message to VF
1585  * @vf: pointer to the VF info
1586  * @v_opcode: virtual channel opcode
1587  * @v_retval: virtual channel return value
1588  * @msg: pointer to the msg buffer
1589  * @msglen: msg length
1590  *
1591  * send msg to VF
1592  */
1593 static int
1594 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
1595                       enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
1596 {
1597         enum ice_status aq_ret;
1598         struct device *dev;
1599         struct ice_pf *pf;
1600
1601         if (!vf)
1602                 return -EINVAL;
1603
1604         pf = vf->pf;
1605         if (ice_validate_vf_id(pf, vf->vf_id))
1606                 return -EINVAL;
1607
1608         dev = ice_pf_to_dev(pf);
1609
1610         /* single place to detect unsuccessful return values */
1611         if (v_retval) {
1612                 vf->num_inval_msgs++;
1613                 dev_info(dev, "VF %d failed opcode %d, retval: %d\n", vf->vf_id,
1614                          v_opcode, v_retval);
1615                 if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
1616                         dev_err(dev, "Number of invalid messages exceeded for VF %d\n",
1617                                 vf->vf_id);
1618                         dev_err(dev, "Use PF Control I/F to enable the VF\n");
1619                         set_bit(ICE_VF_STATE_DIS, vf->vf_states);
1620                         return -EIO;
1621                 }
1622         } else {
1623                 vf->num_valid_msgs++;
1624                 /* reset the invalid counter, if a valid message is received. */
1625                 vf->num_inval_msgs = 0;
1626         }
1627
1628         aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
1629                                        msg, msglen, NULL);
1630         if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
1631                 dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %d\n",
1632                          vf->vf_id, aq_ret, pf->hw.mailboxq.sq_last_status);
1633                 return -EIO;
1634         }
1635
1636         return 0;
1637 }
1638
1639 /**
1640  * ice_vc_get_ver_msg
1641  * @vf: pointer to the VF info
1642  * @msg: pointer to the msg buffer
1643  *
1644  * called from the VF to request the API version used by the PF
1645  */
1646 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
1647 {
1648         struct virtchnl_version_info info = {
1649                 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1650         };
1651
1652         vf->vf_ver = *(struct virtchnl_version_info *)msg;
1653         /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1654         if (VF_IS_V10(&vf->vf_ver))
1655                 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1656
1657         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1658                                      VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
1659                                      sizeof(struct virtchnl_version_info));
1660 }
1661
1662 /**
1663  * ice_vc_get_vf_res_msg
1664  * @vf: pointer to the VF info
1665  * @msg: pointer to the msg buffer
1666  *
1667  * called from the VF to request its resources
1668  */
1669 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
1670 {
1671         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1672         struct virtchnl_vf_resource *vfres = NULL;
1673         struct ice_pf *pf = vf->pf;
1674         struct ice_vsi *vsi;
1675         int len = 0;
1676         int ret;
1677
1678         if (ice_check_vf_init(pf, vf)) {
1679                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1680                 goto err;
1681         }
1682
1683         len = sizeof(struct virtchnl_vf_resource);
1684
1685         vfres = kzalloc(len, GFP_KERNEL);
1686         if (!vfres) {
1687                 v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1688                 len = 0;
1689                 goto err;
1690         }
1691         if (VF_IS_V11(&vf->vf_ver))
1692                 vf->driver_caps = *(u32 *)msg;
1693         else
1694                 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1695                                   VIRTCHNL_VF_OFFLOAD_RSS_REG |
1696                                   VIRTCHNL_VF_OFFLOAD_VLAN;
1697
1698         vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1699         vsi = pf->vsi[vf->lan_vsi_idx];
1700         if (!vsi) {
1701                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1702                 goto err;
1703         }
1704
1705         if (!vsi->info.pvid)
1706                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1707
1708         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1709                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1710         } else {
1711                 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1712                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1713                 else
1714                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1715         }
1716
1717         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1718                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1719
1720         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1721                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1722
1723         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
1724                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1725
1726         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
1727                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1728
1729         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1730                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1731
1732         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1733                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1734
1735         if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1736                 vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
1737
1738         vfres->num_vsis = 1;
1739         /* Tx and Rx queue are equal for VF */
1740         vfres->num_queue_pairs = vsi->num_txq;
1741         vfres->max_vectors = pf->num_msix_per_vf;
1742         vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
1743         vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
1744
1745         vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
1746         vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1747         vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
1748         ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1749                         vf->dflt_lan_addr.addr);
1750
1751         /* match guest capabilities */
1752         vf->driver_caps = vfres->vf_cap_flags;
1753
1754         set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
1755
1756 err:
1757         /* send the response back to the VF */
1758         ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
1759                                     (u8 *)vfres, len);
1760
1761         kfree(vfres);
1762         return ret;
1763 }
1764
1765 /**
1766  * ice_vc_reset_vf_msg
1767  * @vf: pointer to the VF info
1768  *
1769  * called from the VF to reset itself,
1770  * unlike other virtchnl messages, PF driver
1771  * doesn't send the response back to the VF
1772  */
1773 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
1774 {
1775         if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1776                 ice_reset_vf(vf, false);
1777 }
1778
1779 /**
1780  * ice_find_vsi_from_id
1781  * @pf: the PF structure to search for the VSI
1782  * @id: ID of the VSI it is searching for
1783  *
1784  * searches for the VSI with the given ID
1785  */
1786 static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
1787 {
1788         int i;
1789
1790         ice_for_each_vsi(pf, i)
1791                 if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
1792                         return pf->vsi[i];
1793
1794         return NULL;
1795 }
1796
1797 /**
1798  * ice_vc_isvalid_vsi_id
1799  * @vf: pointer to the VF info
1800  * @vsi_id: VF relative VSI ID
1801  *
1802  * check for the valid VSI ID
1803  */
1804 static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
1805 {
1806         struct ice_pf *pf = vf->pf;
1807         struct ice_vsi *vsi;
1808
1809         vsi = ice_find_vsi_from_id(pf, vsi_id);
1810
1811         return (vsi && (vsi->vf_id == vf->vf_id));
1812 }
1813
1814 /**
1815  * ice_vc_isvalid_q_id
1816  * @vf: pointer to the VF info
1817  * @vsi_id: VSI ID
1818  * @qid: VSI relative queue ID
1819  *
1820  * check for the valid queue ID
1821  */
1822 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
1823 {
1824         struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
1825         /* allocated Tx and Rx queues should be always equal for VF VSI */
1826         return (vsi && (qid < vsi->alloc_txq));
1827 }
1828
1829 /**
1830  * ice_vc_isvalid_ring_len
1831  * @ring_len: length of ring
1832  *
1833  * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
1834  * or zero
1835  */
1836 static bool ice_vc_isvalid_ring_len(u16 ring_len)
1837 {
1838         return ring_len == 0 ||
1839                (ring_len >= ICE_MIN_NUM_DESC &&
1840                 ring_len <= ICE_MAX_NUM_DESC &&
1841                 !(ring_len % ICE_REQ_DESC_MULTIPLE));
1842 }
1843
1844 /**
1845  * ice_vc_config_rss_key
1846  * @vf: pointer to the VF info
1847  * @msg: pointer to the msg buffer
1848  *
1849  * Configure the VF's RSS key
1850  */
1851 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
1852 {
1853         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1854         struct virtchnl_rss_key *vrk =
1855                 (struct virtchnl_rss_key *)msg;
1856         struct ice_pf *pf = vf->pf;
1857         struct ice_vsi *vsi;
1858
1859         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1860                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1861                 goto error_param;
1862         }
1863
1864         if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
1865                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1866                 goto error_param;
1867         }
1868
1869         if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
1870                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1871                 goto error_param;
1872         }
1873
1874         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1875                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1876                 goto error_param;
1877         }
1878
1879         vsi = pf->vsi[vf->lan_vsi_idx];
1880         if (!vsi) {
1881                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1882                 goto error_param;
1883         }
1884
1885         if (ice_set_rss(vsi, vrk->key, NULL, 0))
1886                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1887 error_param:
1888         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
1889                                      NULL, 0);
1890 }
1891
1892 /**
1893  * ice_vc_config_rss_lut
1894  * @vf: pointer to the VF info
1895  * @msg: pointer to the msg buffer
1896  *
1897  * Configure the VF's RSS LUT
1898  */
1899 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
1900 {
1901         struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
1902         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1903         struct ice_pf *pf = vf->pf;
1904         struct ice_vsi *vsi;
1905
1906         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1907                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1908                 goto error_param;
1909         }
1910
1911         if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
1912                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1913                 goto error_param;
1914         }
1915
1916         if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
1917                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1918                 goto error_param;
1919         }
1920
1921         if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
1922                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1923                 goto error_param;
1924         }
1925
1926         vsi = pf->vsi[vf->lan_vsi_idx];
1927         if (!vsi) {
1928                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1929                 goto error_param;
1930         }
1931
1932         if (ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE))
1933                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1934 error_param:
1935         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
1936                                      NULL, 0);
1937 }
1938
1939 /**
1940  * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
1941  * @vf: The VF being resseting
1942  *
1943  * The max poll time is about ~800ms, which is about the maximum time it takes
1944  * for a VF to be reset and/or a VF driver to be removed.
1945  */
1946 static void ice_wait_on_vf_reset(struct ice_vf *vf)
1947 {
1948         int i;
1949
1950         for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
1951                 if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
1952                         break;
1953                 msleep(ICE_MAX_VF_RESET_SLEEP_MS);
1954         }
1955 }
1956
1957 /**
1958  * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
1959  * @vf: VF to check if it's ready to be configured/queried
1960  *
1961  * The purpose of this function is to make sure the VF is not in reset, not
1962  * disabled, and initialized so it can be configured and/or queried by a host
1963  * administrator.
1964  */
1965 static int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
1966 {
1967         struct ice_pf *pf;
1968
1969         ice_wait_on_vf_reset(vf);
1970
1971         if (ice_is_vf_disabled(vf))
1972                 return -EINVAL;
1973
1974         pf = vf->pf;
1975         if (ice_check_vf_init(pf, vf))
1976                 return -EBUSY;
1977
1978         return 0;
1979 }
1980
1981 /**
1982  * ice_set_vf_spoofchk
1983  * @netdev: network interface device structure
1984  * @vf_id: VF identifier
1985  * @ena: flag to enable or disable feature
1986  *
1987  * Enable or disable VF spoof checking
1988  */
1989 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
1990 {
1991         struct ice_netdev_priv *np = netdev_priv(netdev);
1992         struct ice_pf *pf = np->vsi->back;
1993         struct ice_vsi_ctx *ctx;
1994         struct ice_vsi *vf_vsi;
1995         enum ice_status status;
1996         struct device *dev;
1997         struct ice_vf *vf;
1998         int ret;
1999
2000         dev = ice_pf_to_dev(pf);
2001         if (ice_validate_vf_id(pf, vf_id))
2002                 return -EINVAL;
2003
2004         vf = &pf->vf[vf_id];
2005         ret = ice_check_vf_ready_for_cfg(vf);
2006         if (ret)
2007                 return ret;
2008
2009         vf_vsi = pf->vsi[vf->lan_vsi_idx];
2010         if (!vf_vsi) {
2011                 netdev_err(netdev, "VSI %d for VF %d is null\n",
2012                            vf->lan_vsi_idx, vf->vf_id);
2013                 return -EINVAL;
2014         }
2015
2016         if (vf_vsi->type != ICE_VSI_VF) {
2017                 netdev_err(netdev, "Type %d of VSI %d for VF %d is no ICE_VSI_VF\n",
2018                            vf_vsi->type, vf_vsi->vsi_num, vf->vf_id);
2019                 return -ENODEV;
2020         }
2021
2022         if (ena == vf->spoofchk) {
2023                 dev_dbg(dev, "VF spoofchk already %s\n", ena ? "ON" : "OFF");
2024                 return 0;
2025         }
2026
2027         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2028         if (!ctx)
2029                 return -ENOMEM;
2030
2031         ctx->info.sec_flags = vf_vsi->info.sec_flags;
2032         ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
2033         if (ena) {
2034                 ctx->info.sec_flags |=
2035                         ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2036                         (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2037                          ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
2038         } else {
2039                 ctx->info.sec_flags &=
2040                         ~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
2041                           (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
2042                            ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
2043         }
2044
2045         status = ice_update_vsi(&pf->hw, vf_vsi->idx, ctx, NULL);
2046         if (status) {
2047                 dev_err(dev, "Failed to %sable spoofchk on VF %d VSI %d\n error %d\n",
2048                         ena ? "en" : "dis", vf->vf_id, vf_vsi->vsi_num, status);
2049                 ret = -EIO;
2050                 goto out;
2051         }
2052
2053         /* only update spoofchk state and VSI context on success */
2054         vf_vsi->info.sec_flags = ctx->info.sec_flags;
2055         vf->spoofchk = ena;
2056
2057 out:
2058         kfree(ctx);
2059         return ret;
2060 }
2061
2062 /**
2063  * ice_vc_get_stats_msg
2064  * @vf: pointer to the VF info
2065  * @msg: pointer to the msg buffer
2066  *
2067  * called from the VF to get VSI stats
2068  */
2069 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
2070 {
2071         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2072         struct virtchnl_queue_select *vqs =
2073                 (struct virtchnl_queue_select *)msg;
2074         struct ice_eth_stats stats = { 0 };
2075         struct ice_pf *pf = vf->pf;
2076         struct ice_vsi *vsi;
2077
2078         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2079                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2080                 goto error_param;
2081         }
2082
2083         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2084                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2085                 goto error_param;
2086         }
2087
2088         vsi = pf->vsi[vf->lan_vsi_idx];
2089         if (!vsi) {
2090                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2091                 goto error_param;
2092         }
2093
2094         ice_update_eth_stats(vsi);
2095
2096         stats = vsi->eth_stats;
2097
2098 error_param:
2099         /* send the response to the VF */
2100         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
2101                                      (u8 *)&stats, sizeof(stats));
2102 }
2103
2104 /**
2105  * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
2106  * @vqs: virtchnl_queue_select structure containing bitmaps to validate
2107  *
2108  * Return true on successful validation, else false
2109  */
2110 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
2111 {
2112         if ((!vqs->rx_queues && !vqs->tx_queues) ||
2113             vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
2114             vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
2115                 return false;
2116
2117         return true;
2118 }
2119
2120 /**
2121  * ice_vc_ena_qs_msg
2122  * @vf: pointer to the VF info
2123  * @msg: pointer to the msg buffer
2124  *
2125  * called from the VF to enable all or specific queue(s)
2126  */
2127 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
2128 {
2129         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2130         struct virtchnl_queue_select *vqs =
2131             (struct virtchnl_queue_select *)msg;
2132         struct ice_pf *pf = vf->pf;
2133         struct ice_vsi *vsi;
2134         unsigned long q_map;
2135         u16 vf_q_id;
2136
2137         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2138                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2139                 goto error_param;
2140         }
2141
2142         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2143                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2144                 goto error_param;
2145         }
2146
2147         if (!ice_vc_validate_vqs_bitmaps(vqs)) {
2148                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2149                 goto error_param;
2150         }
2151
2152         vsi = pf->vsi[vf->lan_vsi_idx];
2153         if (!vsi) {
2154                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2155                 goto error_param;
2156         }
2157
2158         /* Enable only Rx rings, Tx rings were enabled by the FW when the
2159          * Tx queue group list was configured and the context bits were
2160          * programmed using ice_vsi_cfg_txqs
2161          */
2162         q_map = vqs->rx_queues;
2163         for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2164                 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2165                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2166                         goto error_param;
2167                 }
2168
2169                 /* Skip queue if enabled */
2170                 if (test_bit(vf_q_id, vf->rxq_ena))
2171                         continue;
2172
2173                 if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
2174                         dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
2175                                 vf_q_id, vsi->vsi_num);
2176                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2177                         goto error_param;
2178                 }
2179
2180                 set_bit(vf_q_id, vf->rxq_ena);
2181         }
2182
2183         vsi = pf->vsi[vf->lan_vsi_idx];
2184         q_map = vqs->tx_queues;
2185         for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2186                 if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2187                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2188                         goto error_param;
2189                 }
2190
2191                 /* Skip queue if enabled */
2192                 if (test_bit(vf_q_id, vf->txq_ena))
2193                         continue;
2194
2195                 set_bit(vf_q_id, vf->txq_ena);
2196         }
2197
2198         /* Set flag to indicate that queues are enabled */
2199         if (v_ret == VIRTCHNL_STATUS_SUCCESS)
2200                 set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
2201
2202 error_param:
2203         /* send the response to the VF */
2204         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
2205                                      NULL, 0);
2206 }
2207
2208 /**
2209  * ice_vc_dis_qs_msg
2210  * @vf: pointer to the VF info
2211  * @msg: pointer to the msg buffer
2212  *
2213  * called from the VF to disable all or specific
2214  * queue(s)
2215  */
2216 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
2217 {
2218         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2219         struct virtchnl_queue_select *vqs =
2220             (struct virtchnl_queue_select *)msg;
2221         struct ice_pf *pf = vf->pf;
2222         struct ice_vsi *vsi;
2223         unsigned long q_map;
2224         u16 vf_q_id;
2225
2226         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
2227             !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
2228                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2229                 goto error_param;
2230         }
2231
2232         if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2233                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2234                 goto error_param;
2235         }
2236
2237         if (!ice_vc_validate_vqs_bitmaps(vqs)) {
2238                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2239                 goto error_param;
2240         }
2241
2242         vsi = pf->vsi[vf->lan_vsi_idx];
2243         if (!vsi) {
2244                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2245                 goto error_param;
2246         }
2247
2248         if (vqs->tx_queues) {
2249                 q_map = vqs->tx_queues;
2250
2251                 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2252                         struct ice_ring *ring = vsi->tx_rings[vf_q_id];
2253                         struct ice_txq_meta txq_meta = { 0 };
2254
2255                         if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2256                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2257                                 goto error_param;
2258                         }
2259
2260                         /* Skip queue if not enabled */
2261                         if (!test_bit(vf_q_id, vf->txq_ena))
2262                                 continue;
2263
2264                         ice_fill_txq_meta(vsi, ring, &txq_meta);
2265
2266                         if (ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id,
2267                                                  ring, &txq_meta)) {
2268                                 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
2269                                         vf_q_id, vsi->vsi_num);
2270                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2271                                 goto error_param;
2272                         }
2273
2274                         /* Clear enabled queues flag */
2275                         clear_bit(vf_q_id, vf->txq_ena);
2276                 }
2277         }
2278
2279         q_map = vqs->rx_queues;
2280         /* speed up Rx queue disable by batching them if possible */
2281         if (q_map &&
2282             bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
2283                 if (ice_vsi_stop_all_rx_rings(vsi)) {
2284                         dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
2285                                 vsi->vsi_num);
2286                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2287                         goto error_param;
2288                 }
2289
2290                 bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
2291         } else if (q_map) {
2292                 for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
2293                         if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
2294                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2295                                 goto error_param;
2296                         }
2297
2298                         /* Skip queue if not enabled */
2299                         if (!test_bit(vf_q_id, vf->rxq_ena))
2300                                 continue;
2301
2302                         if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
2303                                                      true)) {
2304                                 dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
2305                                         vf_q_id, vsi->vsi_num);
2306                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2307                                 goto error_param;
2308                         }
2309
2310                         /* Clear enabled queues flag */
2311                         clear_bit(vf_q_id, vf->rxq_ena);
2312                 }
2313         }
2314
2315         /* Clear enabled queues flag */
2316         if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
2317                 clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
2318
2319 error_param:
2320         /* send the response to the VF */
2321         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
2322                                      NULL, 0);
2323 }
2324
2325 /**
2326  * ice_cfg_interrupt
2327  * @vf: pointer to the VF info
2328  * @vsi: the VSI being configured
2329  * @vector_id: vector ID
2330  * @map: vector map for mapping vectors to queues
2331  * @q_vector: structure for interrupt vector
2332  * configure the IRQ to queue map
2333  */
2334 static int
2335 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
2336                   struct virtchnl_vector_map *map,
2337                   struct ice_q_vector *q_vector)
2338 {
2339         u16 vsi_q_id, vsi_q_id_idx;
2340         unsigned long qmap;
2341
2342         q_vector->num_ring_rx = 0;
2343         q_vector->num_ring_tx = 0;
2344
2345         qmap = map->rxq_map;
2346         for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
2347                 vsi_q_id = vsi_q_id_idx;
2348
2349                 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
2350                         return VIRTCHNL_STATUS_ERR_PARAM;
2351
2352                 q_vector->num_ring_rx++;
2353                 q_vector->rx.itr_idx = map->rxitr_idx;
2354                 vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
2355                 ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
2356                                       q_vector->rx.itr_idx);
2357         }
2358
2359         qmap = map->txq_map;
2360         for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
2361                 vsi_q_id = vsi_q_id_idx;
2362
2363                 if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
2364                         return VIRTCHNL_STATUS_ERR_PARAM;
2365
2366                 q_vector->num_ring_tx++;
2367                 q_vector->tx.itr_idx = map->txitr_idx;
2368                 vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
2369                 ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
2370                                       q_vector->tx.itr_idx);
2371         }
2372
2373         return VIRTCHNL_STATUS_SUCCESS;
2374 }
2375
2376 /**
2377  * ice_vc_cfg_irq_map_msg
2378  * @vf: pointer to the VF info
2379  * @msg: pointer to the msg buffer
2380  *
2381  * called from the VF to configure the IRQ to queue map
2382  */
2383 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
2384 {
2385         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2386         u16 num_q_vectors_mapped, vsi_id, vector_id;
2387         struct virtchnl_irq_map_info *irqmap_info;
2388         struct virtchnl_vector_map *map;
2389         struct ice_pf *pf = vf->pf;
2390         struct ice_vsi *vsi;
2391         int i;
2392
2393         irqmap_info = (struct virtchnl_irq_map_info *)msg;
2394         num_q_vectors_mapped = irqmap_info->num_vectors;
2395
2396         /* Check to make sure number of VF vectors mapped is not greater than
2397          * number of VF vectors originally allocated, and check that
2398          * there is actually at least a single VF queue vector mapped
2399          */
2400         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2401             pf->num_msix_per_vf < num_q_vectors_mapped ||
2402             !num_q_vectors_mapped) {
2403                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2404                 goto error_param;
2405         }
2406
2407         vsi = pf->vsi[vf->lan_vsi_idx];
2408         if (!vsi) {
2409                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2410                 goto error_param;
2411         }
2412
2413         for (i = 0; i < num_q_vectors_mapped; i++) {
2414                 struct ice_q_vector *q_vector;
2415
2416                 map = &irqmap_info->vecmap[i];
2417
2418                 vector_id = map->vector_id;
2419                 vsi_id = map->vsi_id;
2420                 /* vector_id is always 0-based for each VF, and can never be
2421                  * larger than or equal to the max allowed interrupts per VF
2422                  */
2423                 if (!(vector_id < pf->num_msix_per_vf) ||
2424                     !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
2425                     (!vector_id && (map->rxq_map || map->txq_map))) {
2426                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2427                         goto error_param;
2428                 }
2429
2430                 /* No need to map VF miscellaneous or rogue vector */
2431                 if (!vector_id)
2432                         continue;
2433
2434                 /* Subtract non queue vector from vector_id passed by VF
2435                  * to get actual number of VSI queue vector array index
2436                  */
2437                 q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
2438                 if (!q_vector) {
2439                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2440                         goto error_param;
2441                 }
2442
2443                 /* lookout for the invalid queue index */
2444                 v_ret = (enum virtchnl_status_code)
2445                         ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
2446                 if (v_ret)
2447                         goto error_param;
2448         }
2449
2450 error_param:
2451         /* send the response to the VF */
2452         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
2453                                      NULL, 0);
2454 }
2455
2456 /**
2457  * ice_vc_cfg_qs_msg
2458  * @vf: pointer to the VF info
2459  * @msg: pointer to the msg buffer
2460  *
2461  * called from the VF to configure the Rx/Tx queues
2462  */
2463 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
2464 {
2465         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2466         struct virtchnl_vsi_queue_config_info *qci =
2467             (struct virtchnl_vsi_queue_config_info *)msg;
2468         struct virtchnl_queue_pair_info *qpi;
2469         u16 num_rxq = 0, num_txq = 0;
2470         struct ice_pf *pf = vf->pf;
2471         struct ice_vsi *vsi;
2472         int i;
2473
2474         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2475                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2476                 goto error_param;
2477         }
2478
2479         if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
2480                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2481                 goto error_param;
2482         }
2483
2484         vsi = pf->vsi[vf->lan_vsi_idx];
2485         if (!vsi) {
2486                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2487                 goto error_param;
2488         }
2489
2490         if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
2491             qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
2492                 dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
2493                         vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
2494                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2495                 goto error_param;
2496         }
2497
2498         for (i = 0; i < qci->num_queue_pairs; i++) {
2499                 qpi = &qci->qpair[i];
2500                 if (qpi->txq.vsi_id != qci->vsi_id ||
2501                     qpi->rxq.vsi_id != qci->vsi_id ||
2502                     qpi->rxq.queue_id != qpi->txq.queue_id ||
2503                     qpi->txq.headwb_enabled ||
2504                     !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
2505                     !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
2506                     !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
2507                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2508                         goto error_param;
2509                 }
2510                 /* copy Tx queue info from VF into VSI */
2511                 if (qpi->txq.ring_len > 0) {
2512                         num_txq++;
2513                         vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
2514                         vsi->tx_rings[i]->count = qpi->txq.ring_len;
2515                 }
2516
2517                 /* copy Rx queue info from VF into VSI */
2518                 if (qpi->rxq.ring_len > 0) {
2519                         num_rxq++;
2520                         vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
2521                         vsi->rx_rings[i]->count = qpi->rxq.ring_len;
2522
2523                         if (qpi->rxq.databuffer_size != 0 &&
2524                             (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
2525                              qpi->rxq.databuffer_size < 1024)) {
2526                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2527                                 goto error_param;
2528                         }
2529                         vsi->rx_buf_len = qpi->rxq.databuffer_size;
2530                         vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
2531                         if (qpi->rxq.max_pkt_size >= (16 * 1024) ||
2532                             qpi->rxq.max_pkt_size < 64) {
2533                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2534                                 goto error_param;
2535                         }
2536                 }
2537
2538                 vsi->max_frame = qpi->rxq.max_pkt_size;
2539         }
2540
2541         /* VF can request to configure less than allocated queues
2542          * or default allocated queues. So update the VSI with new number
2543          */
2544         vsi->num_txq = num_txq;
2545         vsi->num_rxq = num_rxq;
2546         /* All queues of VF VSI are in TC 0 */
2547         vsi->tc_cfg.tc_info[0].qcount_tx = num_txq;
2548         vsi->tc_cfg.tc_info[0].qcount_rx = num_rxq;
2549
2550         if (ice_vsi_cfg_lan_txqs(vsi) || ice_vsi_cfg_rxqs(vsi))
2551                 v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2552
2553 error_param:
2554         /* send the response to the VF */
2555         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, v_ret,
2556                                      NULL, 0);
2557 }
2558
2559 /**
2560  * ice_is_vf_trusted
2561  * @vf: pointer to the VF info
2562  */
2563 static bool ice_is_vf_trusted(struct ice_vf *vf)
2564 {
2565         return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
2566 }
2567
2568 /**
2569  * ice_can_vf_change_mac
2570  * @vf: pointer to the VF info
2571  *
2572  * Return true if the VF is allowed to change its MAC filters, false otherwise
2573  */
2574 static bool ice_can_vf_change_mac(struct ice_vf *vf)
2575 {
2576         /* If the VF MAC address has been set administratively (via the
2577          * ndo_set_vf_mac command), then deny permission to the VF to
2578          * add/delete unicast MAC addresses, unless the VF is trusted
2579          */
2580         if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
2581                 return false;
2582
2583         return true;
2584 }
2585
2586 /**
2587  * ice_vc_add_mac_addr - attempt to add the MAC address passed in
2588  * @vf: pointer to the VF info
2589  * @vsi: pointer to the VF's VSI
2590  * @mac_addr: MAC address to add
2591  */
2592 static int
2593 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr)
2594 {
2595         struct device *dev = ice_pf_to_dev(vf->pf);
2596         enum ice_status status;
2597
2598         /* default unicast MAC already added */
2599         if (ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
2600                 return 0;
2601
2602         if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
2603                 dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
2604                 return -EPERM;
2605         }
2606
2607         status = ice_vsi_cfg_mac_fltr(vsi, mac_addr, true);
2608         if (status == ICE_ERR_ALREADY_EXISTS) {
2609                 dev_err(dev, "MAC %pM already exists for VF %d\n", mac_addr,
2610                         vf->vf_id);
2611                 return -EEXIST;
2612         } else if (status) {
2613                 dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
2614                         mac_addr, vf->vf_id, status);
2615                 return -EIO;
2616         }
2617
2618         /* only set dflt_lan_addr once */
2619         if (is_zero_ether_addr(vf->dflt_lan_addr.addr) &&
2620             is_unicast_ether_addr(mac_addr))
2621                 ether_addr_copy(vf->dflt_lan_addr.addr, mac_addr);
2622
2623         vf->num_mac++;
2624
2625         return 0;
2626 }
2627
2628 /**
2629  * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
2630  * @vf: pointer to the VF info
2631  * @vsi: pointer to the VF's VSI
2632  * @mac_addr: MAC address to delete
2633  */
2634 static int
2635 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr)
2636 {
2637         struct device *dev = ice_pf_to_dev(vf->pf);
2638         enum ice_status status;
2639
2640         if (!ice_can_vf_change_mac(vf) &&
2641             ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
2642                 return 0;
2643
2644         status = ice_vsi_cfg_mac_fltr(vsi, mac_addr, false);
2645         if (status == ICE_ERR_DOES_NOT_EXIST) {
2646                 dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
2647                         vf->vf_id);
2648                 return -ENOENT;
2649         } else if (status) {
2650                 dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
2651                         mac_addr, vf->vf_id, status);
2652                 return -EIO;
2653         }
2654
2655         if (ether_addr_equal(mac_addr, vf->dflt_lan_addr.addr))
2656                 eth_zero_addr(vf->dflt_lan_addr.addr);
2657
2658         vf->num_mac--;
2659
2660         return 0;
2661 }
2662
2663 /**
2664  * ice_vc_handle_mac_addr_msg
2665  * @vf: pointer to the VF info
2666  * @msg: pointer to the msg buffer
2667  * @set: true if MAC filters are being set, false otherwise
2668  *
2669  * add guest MAC address filter
2670  */
2671 static int
2672 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
2673 {
2674         int (*ice_vc_cfg_mac)
2675                 (struct ice_vf *vf, struct ice_vsi *vsi, u8 *mac_addr);
2676         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2677         struct virtchnl_ether_addr_list *al =
2678             (struct virtchnl_ether_addr_list *)msg;
2679         struct ice_pf *pf = vf->pf;
2680         enum virtchnl_ops vc_op;
2681         struct ice_vsi *vsi;
2682         int i;
2683
2684         if (set) {
2685                 vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2686                 ice_vc_cfg_mac = ice_vc_add_mac_addr;
2687         } else {
2688                 vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2689                 ice_vc_cfg_mac = ice_vc_del_mac_addr;
2690         }
2691
2692         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2693             !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2694                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2695                 goto handle_mac_exit;
2696         }
2697
2698         /* If this VF is not privileged, then we can't add more than a
2699          * limited number of addresses. Check to make sure that the
2700          * additions do not push us over the limit.
2701          */
2702         if (set && !ice_is_vf_trusted(vf) &&
2703             (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2704                 dev_err(ice_pf_to_dev(pf), "Can't add more MAC addresses, because VF-%d is not trusted, switch the VF to trusted mode in order to add more functionalities\n",
2705                         vf->vf_id);
2706                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2707                 goto handle_mac_exit;
2708         }
2709
2710         vsi = pf->vsi[vf->lan_vsi_idx];
2711         if (!vsi) {
2712                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2713                 goto handle_mac_exit;
2714         }
2715
2716         for (i = 0; i < al->num_elements; i++) {
2717                 u8 *mac_addr = al->list[i].addr;
2718                 int result;
2719
2720                 if (is_broadcast_ether_addr(mac_addr) ||
2721                     is_zero_ether_addr(mac_addr))
2722                         continue;
2723
2724                 result = ice_vc_cfg_mac(vf, vsi, mac_addr);
2725                 if (result == -EEXIST || result == -ENOENT) {
2726                         continue;
2727                 } else if (result) {
2728                         v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2729                         goto handle_mac_exit;
2730                 }
2731         }
2732
2733 handle_mac_exit:
2734         /* send the response to the VF */
2735         return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2736 }
2737
2738 /**
2739  * ice_vc_add_mac_addr_msg
2740  * @vf: pointer to the VF info
2741  * @msg: pointer to the msg buffer
2742  *
2743  * add guest MAC address filter
2744  */
2745 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2746 {
2747         return ice_vc_handle_mac_addr_msg(vf, msg, true);
2748 }
2749
2750 /**
2751  * ice_vc_del_mac_addr_msg
2752  * @vf: pointer to the VF info
2753  * @msg: pointer to the msg buffer
2754  *
2755  * remove guest MAC address filter
2756  */
2757 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2758 {
2759         return ice_vc_handle_mac_addr_msg(vf, msg, false);
2760 }
2761
2762 /**
2763  * ice_vc_request_qs_msg
2764  * @vf: pointer to the VF info
2765  * @msg: pointer to the msg buffer
2766  *
2767  * VFs get a default number of queues but can use this message to request a
2768  * different number. If the request is successful, PF will reset the VF and
2769  * return 0. If unsuccessful, PF will send message informing VF of number of
2770  * available queue pairs via virtchnl message response to VF.
2771  */
2772 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2773 {
2774         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2775         struct virtchnl_vf_res_request *vfres =
2776                 (struct virtchnl_vf_res_request *)msg;
2777         u16 req_queues = vfres->num_queue_pairs;
2778         struct ice_pf *pf = vf->pf;
2779         u16 max_allowed_vf_queues;
2780         u16 tx_rx_queue_left;
2781         struct device *dev;
2782         u16 cur_queues;
2783
2784         dev = ice_pf_to_dev(pf);
2785         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2786                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2787                 goto error_param;
2788         }
2789
2790         cur_queues = vf->num_vf_qs;
2791         tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
2792                                  ice_get_avail_rxq_count(pf));
2793         max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2794         if (!req_queues) {
2795                 dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
2796                         vf->vf_id);
2797         } else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
2798                 dev_err(dev, "VF %d tried to request more than %d queues.\n",
2799                         vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
2800                 vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
2801         } else if (req_queues > cur_queues &&
2802                    req_queues - cur_queues > tx_rx_queue_left) {
2803                 dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
2804                          vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2805                 vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
2806                                                ICE_MAX_RSS_QS_PER_VF);
2807         } else {
2808                 /* request is successful, then reset VF */
2809                 vf->num_req_qs = req_queues;
2810                 ice_vc_reset_vf(vf);
2811                 dev_info(dev, "VF %d granted request of %u queues.\n",
2812                          vf->vf_id, req_queues);
2813                 return 0;
2814         }
2815
2816 error_param:
2817         /* send the response to the VF */
2818         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2819                                      v_ret, (u8 *)vfres, sizeof(*vfres));
2820 }
2821
2822 /**
2823  * ice_set_vf_port_vlan
2824  * @netdev: network interface device structure
2825  * @vf_id: VF identifier
2826  * @vlan_id: VLAN ID being set
2827  * @qos: priority setting
2828  * @vlan_proto: VLAN protocol
2829  *
2830  * program VF Port VLAN ID and/or QoS
2831  */
2832 int
2833 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
2834                      __be16 vlan_proto)
2835 {
2836         struct ice_pf *pf = ice_netdev_to_pf(netdev);
2837         struct ice_vsi *vsi;
2838         struct device *dev;
2839         struct ice_vf *vf;
2840         u16 vlanprio;
2841         int ret;
2842
2843         dev = ice_pf_to_dev(pf);
2844         if (ice_validate_vf_id(pf, vf_id))
2845                 return -EINVAL;
2846
2847         if (vlan_id >= VLAN_N_VID || qos > 7) {
2848                 dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
2849                         vf_id, vlan_id, qos);
2850                 return -EINVAL;
2851         }
2852
2853         if (vlan_proto != htons(ETH_P_8021Q)) {
2854                 dev_err(dev, "VF VLAN protocol is not supported\n");
2855                 return -EPROTONOSUPPORT;
2856         }
2857
2858         vf = &pf->vf[vf_id];
2859         vsi = pf->vsi[vf->lan_vsi_idx];
2860
2861         ret = ice_check_vf_ready_for_cfg(vf);
2862         if (ret)
2863                 return ret;
2864
2865         vlanprio = vlan_id | (qos << VLAN_PRIO_SHIFT);
2866
2867         if (vf->port_vlan_info == vlanprio) {
2868                 /* duplicate request, so just return success */
2869                 dev_dbg(dev, "Duplicate pvid %d request\n", vlanprio);
2870                 return 0;
2871         }
2872
2873         if (vlan_id || qos) {
2874                 /* remove VLAN 0 filter set by default when transitioning from
2875                  * no port VLAN to a port VLAN. No change to old port VLAN on
2876                  * failure.
2877                  */
2878                 ret = ice_vsi_kill_vlan(vsi, 0);
2879                 if (ret)
2880                         return ret;
2881                 ret = ice_vsi_manage_pvid(vsi, vlanprio, true);
2882                 if (ret)
2883                         return ret;
2884         } else {
2885                 /* add VLAN 0 filter back when transitioning from port VLAN to
2886                  * no port VLAN. No change to old port VLAN on failure.
2887                  */
2888                 ret = ice_vsi_add_vlan(vsi, 0);
2889                 if (ret)
2890                         return ret;
2891                 ret = ice_vsi_manage_pvid(vsi, 0, false);
2892                 if (ret)
2893                         return ret;
2894         }
2895
2896         if (vlan_id) {
2897                 dev_info(dev, "Setting VLAN %d, QoS 0x%x on VF %d\n",
2898                          vlan_id, qos, vf_id);
2899
2900                 /* add VLAN filter for the port VLAN */
2901                 ret = ice_vsi_add_vlan(vsi, vlan_id);
2902                 if (ret)
2903                         return ret;
2904         }
2905         /* remove old port VLAN filter with valid VLAN ID or QoS fields */
2906         if (vf->port_vlan_info)
2907                 ice_vsi_kill_vlan(vsi, vf->port_vlan_info & VLAN_VID_MASK);
2908
2909         /* keep port VLAN information persistent on resets */
2910         vf->port_vlan_info = le16_to_cpu(vsi->info.pvid);
2911
2912         return 0;
2913 }
2914
2915 /**
2916  * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
2917  * @caps: VF driver negotiated capabilities
2918  *
2919  * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
2920  */
2921 static bool ice_vf_vlan_offload_ena(u32 caps)
2922 {
2923         return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
2924 }
2925
2926 /**
2927  * ice_vc_process_vlan_msg
2928  * @vf: pointer to the VF info
2929  * @msg: pointer to the msg buffer
2930  * @add_v: Add VLAN if true, otherwise delete VLAN
2931  *
2932  * Process virtchnl op to add or remove programmed guest VLAN ID
2933  */
2934 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2935 {
2936         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2937         struct virtchnl_vlan_filter_list *vfl =
2938             (struct virtchnl_vlan_filter_list *)msg;
2939         struct ice_pf *pf = vf->pf;
2940         bool vlan_promisc = false;
2941         struct ice_vsi *vsi;
2942         struct device *dev;
2943         struct ice_hw *hw;
2944         int status = 0;
2945         u8 promisc_m;
2946         int i;
2947
2948         dev = ice_pf_to_dev(pf);
2949         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2950                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2951                 goto error_param;
2952         }
2953
2954         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2955                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2956                 goto error_param;
2957         }
2958
2959         if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2960                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2961                 goto error_param;
2962         }
2963
2964         for (i = 0; i < vfl->num_elements; i++) {
2965                 if (vfl->vlan_id[i] >= VLAN_N_VID) {
2966                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2967                         dev_err(dev, "invalid VF VLAN id %d\n",
2968                                 vfl->vlan_id[i]);
2969                         goto error_param;
2970                 }
2971         }
2972
2973         hw = &pf->hw;
2974         vsi = pf->vsi[vf->lan_vsi_idx];
2975         if (!vsi) {
2976                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2977                 goto error_param;
2978         }
2979
2980         if (add_v && !ice_is_vf_trusted(vf) &&
2981             vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
2982                 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2983                          vf->vf_id);
2984                 /* There is no need to let VF know about being not trusted,
2985                  * so we can just return success message here
2986                  */
2987                 goto error_param;
2988         }
2989
2990         if (vsi->info.pvid) {
2991                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2992                 goto error_param;
2993         }
2994
2995         if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2996             test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2997                 vlan_promisc = true;
2998
2999         if (add_v) {
3000                 for (i = 0; i < vfl->num_elements; i++) {
3001                         u16 vid = vfl->vlan_id[i];
3002
3003                         if (!ice_is_vf_trusted(vf) &&
3004                             vsi->num_vlan >= ICE_MAX_VLAN_PER_VF) {
3005                                 dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
3006                                          vf->vf_id);
3007                                 /* There is no need to let VF know about being
3008                                  * not trusted, so we can just return success
3009                                  * message here as well.
3010                                  */
3011                                 goto error_param;
3012                         }
3013
3014                         /* we add VLAN 0 by default for each VF so we can enable
3015                          * Tx VLAN anti-spoof without triggering MDD events so
3016                          * we don't need to add it again here
3017                          */
3018                         if (!vid)
3019                                 continue;
3020
3021                         status = ice_vsi_add_vlan(vsi, vid);
3022                         if (status) {
3023                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3024                                 goto error_param;
3025                         }
3026
3027                         /* Enable VLAN pruning when non-zero VLAN is added */
3028                         if (!vlan_promisc && vid &&
3029                             !ice_vsi_is_vlan_pruning_ena(vsi)) {
3030                                 status = ice_cfg_vlan_pruning(vsi, true, false);
3031                                 if (status) {
3032                                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3033                                         dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
3034                                                 vid, status);
3035                                         goto error_param;
3036                                 }
3037                         } else if (vlan_promisc) {
3038                                 /* Enable Ucast/Mcast VLAN promiscuous mode */
3039                                 promisc_m = ICE_PROMISC_VLAN_TX |
3040                                             ICE_PROMISC_VLAN_RX;
3041
3042                                 status = ice_set_vsi_promisc(hw, vsi->idx,
3043                                                              promisc_m, vid);
3044                                 if (status) {
3045                                         v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3046                                         dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
3047                                                 vid, status);
3048                                 }
3049                         }
3050                 }
3051         } else {
3052                 /* In case of non_trusted VF, number of VLAN elements passed
3053                  * to PF for removal might be greater than number of VLANs
3054                  * filter programmed for that VF - So, use actual number of
3055                  * VLANS added earlier with add VLAN opcode. In order to avoid
3056                  * removing VLAN that doesn't exist, which result to sending
3057                  * erroneous failed message back to the VF
3058                  */
3059                 int num_vf_vlan;
3060
3061                 num_vf_vlan = vsi->num_vlan;
3062                 for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
3063                         u16 vid = vfl->vlan_id[i];
3064
3065                         /* we add VLAN 0 by default for each VF so we can enable
3066                          * Tx VLAN anti-spoof without triggering MDD events so
3067                          * we don't want a VIRTCHNL request to remove it
3068                          */
3069                         if (!vid)
3070                                 continue;
3071
3072                         /* Make sure ice_vsi_kill_vlan is successful before
3073                          * updating VLAN information
3074                          */
3075                         status = ice_vsi_kill_vlan(vsi, vid);
3076                         if (status) {
3077                                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3078                                 goto error_param;
3079                         }
3080
3081                         /* Disable VLAN pruning when only VLAN 0 is left */
3082                         if (vsi->num_vlan == 1 &&
3083                             ice_vsi_is_vlan_pruning_ena(vsi))
3084                                 ice_cfg_vlan_pruning(vsi, false, false);
3085
3086                         /* Disable Unicast/Multicast VLAN promiscuous mode */
3087                         if (vlan_promisc) {
3088                                 promisc_m = ICE_PROMISC_VLAN_TX |
3089                                             ICE_PROMISC_VLAN_RX;
3090
3091                                 ice_clear_vsi_promisc(hw, vsi->idx,
3092                                                       promisc_m, vid);
3093                         }
3094                 }
3095         }
3096
3097 error_param:
3098         /* send the response to the VF */
3099         if (add_v)
3100                 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
3101                                              NULL, 0);
3102         else
3103                 return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
3104                                              NULL, 0);
3105 }
3106
3107 /**
3108  * ice_vc_add_vlan_msg
3109  * @vf: pointer to the VF info
3110  * @msg: pointer to the msg buffer
3111  *
3112  * Add and program guest VLAN ID
3113  */
3114 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
3115 {
3116         return ice_vc_process_vlan_msg(vf, msg, true);
3117 }
3118
3119 /**
3120  * ice_vc_remove_vlan_msg
3121  * @vf: pointer to the VF info
3122  * @msg: pointer to the msg buffer
3123  *
3124  * remove programmed guest VLAN ID
3125  */
3126 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
3127 {
3128         return ice_vc_process_vlan_msg(vf, msg, false);
3129 }
3130
3131 /**
3132  * ice_vc_ena_vlan_stripping
3133  * @vf: pointer to the VF info
3134  *
3135  * Enable VLAN header stripping for a given VF
3136  */
3137 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
3138 {
3139         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3140         struct ice_pf *pf = vf->pf;
3141         struct ice_vsi *vsi;
3142
3143         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3144                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3145                 goto error_param;
3146         }
3147
3148         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
3149                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3150                 goto error_param;
3151         }
3152
3153         vsi = pf->vsi[vf->lan_vsi_idx];
3154         if (ice_vsi_manage_vlan_stripping(vsi, true))
3155                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3156
3157 error_param:
3158         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
3159                                      v_ret, NULL, 0);
3160 }
3161
3162 /**
3163  * ice_vc_dis_vlan_stripping
3164  * @vf: pointer to the VF info
3165  *
3166  * Disable VLAN header stripping for a given VF
3167  */
3168 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
3169 {
3170         enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3171         struct ice_pf *pf = vf->pf;
3172         struct ice_vsi *vsi;
3173
3174         if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3175                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3176                 goto error_param;
3177         }
3178
3179         if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
3180                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3181                 goto error_param;
3182         }
3183
3184         vsi = pf->vsi[vf->lan_vsi_idx];
3185         if (!vsi) {
3186                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3187                 goto error_param;
3188         }
3189
3190         if (ice_vsi_manage_vlan_stripping(vsi, false))
3191                 v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3192
3193 error_param:
3194         return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
3195                                      v_ret, NULL, 0);
3196 }
3197
3198 /**
3199  * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
3200  * @vf: VF to enable/disable VLAN stripping for on initialization
3201  *
3202  * If the VIRTCHNL_VF_OFFLOAD_VLAN flag is set enable VLAN stripping, else if
3203  * the flag is cleared then we want to disable stripping. For example, the flag
3204  * will be cleared when port VLANs are configured by the administrator before
3205  * passing the VF to the guest or if the AVF driver doesn't support VLAN
3206  * offloads.
3207  */
3208 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
3209 {
3210         struct ice_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
3211
3212         if (!vsi)
3213                 return -EINVAL;
3214
3215         /* don't modify stripping if port VLAN is configured */
3216         if (vsi->info.pvid)
3217                 return 0;
3218
3219         if (ice_vf_vlan_offload_ena(vf->driver_caps))
3220                 return ice_vsi_manage_vlan_stripping(vsi, true);
3221         else
3222                 return ice_vsi_manage_vlan_stripping(vsi, false);
3223 }
3224
3225 /**
3226  * ice_vc_process_vf_msg - Process request from VF
3227  * @pf: pointer to the PF structure
3228  * @event: pointer to the AQ event
3229  *
3230  * called from the common asq/arq handler to
3231  * process request from VF
3232  */
3233 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
3234 {
3235         u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
3236         s16 vf_id = le16_to_cpu(event->desc.retval);
3237         u16 msglen = event->msg_len;
3238         u8 *msg = event->msg_buf;
3239         struct ice_vf *vf = NULL;
3240         struct device *dev;
3241         int err = 0;
3242
3243         dev = ice_pf_to_dev(pf);
3244         if (ice_validate_vf_id(pf, vf_id)) {
3245                 err = -EINVAL;
3246                 goto error_handler;
3247         }
3248
3249         vf = &pf->vf[vf_id];
3250
3251         /* Check if VF is disabled. */
3252         if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
3253                 err = -EPERM;
3254                 goto error_handler;
3255         }
3256
3257         /* Perform basic checks on the msg */
3258         err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
3259         if (err) {
3260                 if (err == VIRTCHNL_STATUS_ERR_PARAM)
3261                         err = -EPERM;
3262                 else
3263                         err = -EINVAL;
3264         }
3265
3266 error_handler:
3267         if (err) {
3268                 ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
3269                                       NULL, 0);
3270                 dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
3271                         vf_id, v_opcode, msglen, err);
3272                 return;
3273         }
3274
3275         switch (v_opcode) {
3276         case VIRTCHNL_OP_VERSION:
3277                 err = ice_vc_get_ver_msg(vf, msg);
3278                 break;
3279         case VIRTCHNL_OP_GET_VF_RESOURCES:
3280                 err = ice_vc_get_vf_res_msg(vf, msg);
3281                 if (ice_vf_init_vlan_stripping(vf))
3282                         dev_err(dev, "Failed to initialize VLAN stripping for VF %d\n",
3283                                 vf->vf_id);
3284                 ice_vc_notify_vf_link_state(vf);
3285                 break;
3286         case VIRTCHNL_OP_RESET_VF:
3287                 ice_vc_reset_vf_msg(vf);
3288                 break;
3289         case VIRTCHNL_OP_ADD_ETH_ADDR:
3290                 err = ice_vc_add_mac_addr_msg(vf, msg);
3291                 break;
3292         case VIRTCHNL_OP_DEL_ETH_ADDR:
3293                 err = ice_vc_del_mac_addr_msg(vf, msg);
3294                 break;
3295         case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3296                 err = ice_vc_cfg_qs_msg(vf, msg);
3297                 break;
3298         case VIRTCHNL_OP_ENABLE_QUEUES:
3299                 err = ice_vc_ena_qs_msg(vf, msg);
3300                 ice_vc_notify_vf_link_state(vf);
3301                 break;
3302         case VIRTCHNL_OP_DISABLE_QUEUES:
3303                 err = ice_vc_dis_qs_msg(vf, msg);
3304                 break;
3305         case VIRTCHNL_OP_REQUEST_QUEUES:
3306                 err = ice_vc_request_qs_msg(vf, msg);
3307                 break;
3308         case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3309                 err = ice_vc_cfg_irq_map_msg(vf, msg);
3310                 break;
3311         case VIRTCHNL_OP_CONFIG_RSS_KEY:
3312                 err = ice_vc_config_rss_key(vf, msg);
3313                 break;
3314         case VIRTCHNL_OP_CONFIG_RSS_LUT:
3315                 err = ice_vc_config_rss_lut(vf, msg);
3316                 break;
3317         case VIRTCHNL_OP_GET_STATS:
3318                 err = ice_vc_get_stats_msg(vf, msg);
3319                 break;
3320         case VIRTCHNL_OP_ADD_VLAN:
3321                 err = ice_vc_add_vlan_msg(vf, msg);
3322                 break;
3323         case VIRTCHNL_OP_DEL_VLAN:
3324                 err = ice_vc_remove_vlan_msg(vf, msg);
3325                 break;
3326         case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
3327                 err = ice_vc_ena_vlan_stripping(vf);
3328                 break;
3329         case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
3330                 err = ice_vc_dis_vlan_stripping(vf);
3331                 break;
3332         case VIRTCHNL_OP_UNKNOWN:
3333         default:
3334                 dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
3335                         vf_id);
3336                 err = ice_vc_send_msg_to_vf(vf, v_opcode,
3337                                             VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
3338                                             NULL, 0);
3339                 break;
3340         }
3341         if (err) {
3342                 /* Helper function cares less about error return values here
3343                  * as it is busy with pending work.
3344                  */
3345                 dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
3346                          vf_id, v_opcode, err);
3347         }
3348 }
3349
3350 /**
3351  * ice_get_vf_cfg
3352  * @netdev: network interface device structure
3353  * @vf_id: VF identifier
3354  * @ivi: VF configuration structure
3355  *
3356  * return VF configuration
3357  */
3358 int
3359 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
3360 {
3361         struct ice_pf *pf = ice_netdev_to_pf(netdev);
3362         struct ice_vf *vf;
3363
3364         if (ice_validate_vf_id(pf, vf_id))
3365                 return -EINVAL;
3366
3367         vf = &pf->vf[vf_id];
3368
3369         if (ice_check_vf_init(pf, vf))
3370                 return -EBUSY;
3371
3372         ivi->vf = vf_id;
3373         ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
3374
3375         /* VF configuration for VLAN and applicable QoS */
3376         ivi->vlan = vf->port_vlan_info & VLAN_VID_MASK;
3377         ivi->qos = (vf->port_vlan_info & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
3378
3379         ivi->trusted = vf->trusted;
3380         ivi->spoofchk = vf->spoofchk;
3381         if (!vf->link_forced)
3382                 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
3383         else if (vf->link_up)
3384                 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
3385         else
3386                 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
3387         ivi->max_tx_rate = vf->tx_rate;
3388         ivi->min_tx_rate = 0;
3389         return 0;
3390 }
3391
3392 /**
3393  * ice_set_vf_mac
3394  * @netdev: network interface device structure
3395  * @vf_id: VF identifier
3396  * @mac: MAC address
3397  *
3398  * program VF MAC address
3399  */
3400 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
3401 {
3402         struct ice_pf *pf = ice_netdev_to_pf(netdev);
3403         struct ice_vf *vf;
3404         int ret;
3405
3406         if (ice_validate_vf_id(pf, vf_id))
3407                 return -EINVAL;
3408
3409         if (is_zero_ether_addr(mac) || is_multicast_ether_addr(mac)) {
3410                 netdev_err(netdev, "%pM not a valid unicast address\n", mac);
3411                 return -EINVAL;
3412         }
3413
3414         vf = &pf->vf[vf_id];
3415         ret = ice_check_vf_ready_for_cfg(vf);
3416         if (ret)
3417                 return ret;
3418
3419         /* copy MAC into dflt_lan_addr and trigger a VF reset. The reset
3420          * flow will use the updated dflt_lan_addr and add a MAC filter
3421          * using ice_add_mac. Also set pf_set_mac to indicate that the PF has
3422          * set the MAC address for this VF.
3423          */
3424         ether_addr_copy(vf->dflt_lan_addr.addr, mac);
3425         vf->pf_set_mac = true;
3426         netdev_info(netdev, "MAC on VF %d set to %pM. VF driver will be reinitialized\n",
3427                     vf_id, mac);
3428
3429         ice_vc_reset_vf(vf);
3430         return 0;
3431 }
3432
3433 /**
3434  * ice_set_vf_trust
3435  * @netdev: network interface device structure
3436  * @vf_id: VF identifier
3437  * @trusted: Boolean value to enable/disable trusted VF
3438  *
3439  * Enable or disable a given VF as trusted
3440  */
3441 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
3442 {
3443         struct ice_pf *pf = ice_netdev_to_pf(netdev);
3444         struct ice_vf *vf;
3445         int ret;
3446
3447         if (ice_validate_vf_id(pf, vf_id))
3448                 return -EINVAL;
3449
3450         vf = &pf->vf[vf_id];
3451         ret = ice_check_vf_ready_for_cfg(vf);
3452         if (ret)
3453                 return ret;
3454
3455         /* Check if already trusted */
3456         if (trusted == vf->trusted)
3457                 return 0;
3458
3459         vf->trusted = trusted;
3460         ice_vc_reset_vf(vf);
3461         dev_info(ice_pf_to_dev(pf), "VF %u is now %strusted\n",
3462                  vf_id, trusted ? "" : "un");
3463
3464         return 0;
3465 }
3466
3467 /**
3468  * ice_set_vf_link_state
3469  * @netdev: network interface device structure
3470  * @vf_id: VF identifier
3471  * @link_state: required link state
3472  *
3473  * Set VF's link state, irrespective of physical link state status
3474  */
3475 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
3476 {
3477         struct ice_pf *pf = ice_netdev_to_pf(netdev);
3478         struct ice_vf *vf;
3479         int ret;
3480
3481         if (ice_validate_vf_id(pf, vf_id))
3482                 return -EINVAL;
3483
3484         vf = &pf->vf[vf_id];
3485         ret = ice_check_vf_ready_for_cfg(vf);
3486         if (ret)
3487                 return ret;
3488
3489         switch (link_state) {
3490         case IFLA_VF_LINK_STATE_AUTO:
3491                 vf->link_forced = false;
3492                 break;
3493         case IFLA_VF_LINK_STATE_ENABLE:
3494                 vf->link_forced = true;
3495                 vf->link_up = true;
3496                 break;
3497         case IFLA_VF_LINK_STATE_DISABLE:
3498                 vf->link_forced = true;
3499                 vf->link_up = false;
3500                 break;
3501         default:
3502                 return -EINVAL;
3503         }
3504
3505         ice_vc_notify_vf_link_state(vf);
3506
3507         return 0;
3508 }
3509
3510 /**
3511  * ice_get_vf_stats - populate some stats for the VF
3512  * @netdev: the netdev of the PF
3513  * @vf_id: the host OS identifier (0-255)
3514  * @vf_stats: pointer to the OS memory to be initialized
3515  */
3516 int ice_get_vf_stats(struct net_device *netdev, int vf_id,
3517                      struct ifla_vf_stats *vf_stats)
3518 {
3519         struct ice_pf *pf = ice_netdev_to_pf(netdev);
3520         struct ice_eth_stats *stats;
3521         struct ice_vsi *vsi;
3522         struct ice_vf *vf;
3523         int ret;
3524
3525         if (ice_validate_vf_id(pf, vf_id))
3526                 return -EINVAL;
3527
3528         vf = &pf->vf[vf_id];
3529         ret = ice_check_vf_ready_for_cfg(vf);
3530         if (ret)
3531                 return ret;
3532
3533         vsi = pf->vsi[vf->lan_vsi_idx];
3534         if (!vsi)
3535                 return -EINVAL;
3536
3537         ice_update_eth_stats(vsi);
3538         stats = &vsi->eth_stats;
3539
3540         memset(vf_stats, 0, sizeof(*vf_stats));
3541
3542         vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
3543                 stats->rx_multicast;
3544         vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
3545                 stats->tx_multicast;
3546         vf_stats->rx_bytes   = stats->rx_bytes;
3547         vf_stats->tx_bytes   = stats->tx_bytes;
3548         vf_stats->broadcast  = stats->rx_broadcast;
3549         vf_stats->multicast  = stats->rx_multicast;
3550         vf_stats->rx_dropped = stats->rx_discards;
3551         vf_stats->tx_dropped = stats->tx_discards;
3552
3553         return 0;
3554 }
3555
3556 /**
3557  * ice_print_vfs_mdd_event - print VFs malicious driver detect event
3558  * @pf: pointer to the PF structure
3559  *
3560  * Called from ice_handle_mdd_event to rate limit and print VFs MDD events.
3561  */
3562 void ice_print_vfs_mdd_events(struct ice_pf *pf)
3563 {
3564         struct device *dev = ice_pf_to_dev(pf);
3565         struct ice_hw *hw = &pf->hw;
3566         int i;
3567
3568         /* check that there are pending MDD events to print */
3569         if (!test_and_clear_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state))
3570                 return;
3571
3572         /* VF MDD event logs are rate limited to one second intervals */
3573         if (time_is_after_jiffies(pf->last_printed_mdd_jiffies + HZ * 1))
3574                 return;
3575
3576         pf->last_printed_mdd_jiffies = jiffies;
3577
3578         ice_for_each_vf(pf, i) {
3579                 struct ice_vf *vf = &pf->vf[i];
3580
3581                 /* only print Rx MDD event message if there are new events */
3582                 if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
3583                         vf->mdd_rx_events.last_printed =
3584                                                         vf->mdd_rx_events.count;
3585
3586                         dev_info(dev, "%d Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pM. mdd-auto-reset-vfs=%s\n",
3587                                  vf->mdd_rx_events.count, hw->pf_id, i,
3588                                  vf->dflt_lan_addr.addr,
3589                                  test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)
3590                                           ? "on" : "off");
3591                 }
3592
3593                 /* only print Tx MDD event message if there are new events */
3594                 if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
3595                         vf->mdd_tx_events.last_printed =
3596                                                         vf->mdd_tx_events.count;
3597
3598                         dev_info(dev, "%d Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pM.\n",
3599                                  vf->mdd_tx_events.count, hw->pf_id, i,
3600                                  vf->dflt_lan_addr.addr);
3601                 }
3602         }
3603 }