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