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