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