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