GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / net / ethernet / intel / i40e / i40e_virtchnl_pf.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2016 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26
27 #include "i40e.h"
28
29 /*********************notification routines***********************/
30
31 /**
32  * i40e_vc_vf_broadcast
33  * @pf: pointer to the PF structure
34  * @opcode: operation code
35  * @retval: return value
36  * @msg: pointer to the msg buffer
37  * @msglen: msg length
38  *
39  * send a message to all VFs on a given PF
40  **/
41 static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
42                                  enum virtchnl_ops v_opcode,
43                                  i40e_status v_retval, u8 *msg,
44                                  u16 msglen)
45 {
46         struct i40e_hw *hw = &pf->hw;
47         struct i40e_vf *vf = pf->vf;
48         int i;
49
50         for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
51                 int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
52                 /* Not all vfs are enabled so skip the ones that are not */
53                 if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
54                     !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
55                         continue;
56
57                 /* Ignore return value on purpose - a given VF may fail, but
58                  * we need to keep going and send to all of them
59                  */
60                 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
61                                        msg, msglen, NULL);
62         }
63 }
64
65 /**
66  * i40e_vc_notify_vf_link_state
67  * @vf: pointer to the VF structure
68  *
69  * send a link status message to a single VF
70  **/
71 static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
72 {
73         struct virtchnl_pf_event pfe;
74         struct i40e_pf *pf = vf->pf;
75         struct i40e_hw *hw = &pf->hw;
76         struct i40e_link_status *ls = &pf->hw.phy.link_info;
77         int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
78
79         pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
80         pfe.severity = PF_EVENT_SEVERITY_INFO;
81         if (vf->link_forced) {
82                 pfe.event_data.link_event.link_status = vf->link_up;
83                 pfe.event_data.link_event.link_speed =
84                         (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
85         } else {
86                 pfe.event_data.link_event.link_status =
87                         ls->link_info & I40E_AQ_LINK_UP;
88                 pfe.event_data.link_event.link_speed =
89                         (enum virtchnl_link_speed)ls->link_speed;
90         }
91         i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
92                                0, (u8 *)&pfe, sizeof(pfe), NULL);
93 }
94
95 /**
96  * i40e_vc_notify_link_state
97  * @pf: pointer to the PF structure
98  *
99  * send a link status message to all VFs on a given PF
100  **/
101 void i40e_vc_notify_link_state(struct i40e_pf *pf)
102 {
103         int i;
104
105         for (i = 0; i < pf->num_alloc_vfs; i++)
106                 i40e_vc_notify_vf_link_state(&pf->vf[i]);
107 }
108
109 /**
110  * i40e_vc_notify_reset
111  * @pf: pointer to the PF structure
112  *
113  * indicate a pending reset to all VFs on a given PF
114  **/
115 void i40e_vc_notify_reset(struct i40e_pf *pf)
116 {
117         struct virtchnl_pf_event pfe;
118
119         pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
120         pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
121         i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
122                              (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
123 }
124
125 /**
126  * i40e_vc_notify_vf_reset
127  * @vf: pointer to the VF structure
128  *
129  * indicate a pending reset to the given VF
130  **/
131 void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
132 {
133         struct virtchnl_pf_event pfe;
134         int abs_vf_id;
135
136         /* validate the request */
137         if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
138                 return;
139
140         /* verify if the VF is in either init or active before proceeding */
141         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
142             !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
143                 return;
144
145         abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
146
147         pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
148         pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
149         i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, VIRTCHNL_OP_EVENT,
150                                0, (u8 *)&pfe,
151                                sizeof(struct virtchnl_pf_event), NULL);
152 }
153 /***********************misc routines*****************************/
154
155 /**
156  * i40e_vc_disable_vf
157  * @pf: pointer to the PF info
158  * @vf: pointer to the VF info
159  *
160  * Disable the VF through a SW reset
161  **/
162 static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
163 {
164         i40e_vc_notify_vf_reset(vf);
165         i40e_reset_vf(vf, false);
166 }
167
168 /**
169  * i40e_vc_isvalid_vsi_id
170  * @vf: pointer to the VF info
171  * @vsi_id: VF relative VSI id
172  *
173  * check for the valid VSI id
174  **/
175 static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
176 {
177         struct i40e_pf *pf = vf->pf;
178         struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
179
180         return (vsi && (vsi->vf_id == vf->vf_id));
181 }
182
183 /**
184  * i40e_vc_isvalid_queue_id
185  * @vf: pointer to the VF info
186  * @vsi_id: vsi id
187  * @qid: vsi relative queue id
188  *
189  * check for the valid queue id
190  **/
191 static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
192                                             u16 qid)
193 {
194         struct i40e_pf *pf = vf->pf;
195         struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
196
197         return (vsi && (qid < vsi->alloc_queue_pairs));
198 }
199
200 /**
201  * i40e_vc_isvalid_vector_id
202  * @vf: pointer to the VF info
203  * @vector_id: VF relative vector id
204  *
205  * check for the valid vector id
206  **/
207 static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u32 vector_id)
208 {
209         struct i40e_pf *pf = vf->pf;
210
211         return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
212 }
213
214 /***********************vf resource mgmt routines*****************/
215
216 /**
217  * i40e_vc_get_pf_queue_id
218  * @vf: pointer to the VF info
219  * @vsi_id: id of VSI as provided by the FW
220  * @vsi_queue_id: vsi relative queue id
221  *
222  * return PF relative queue id
223  **/
224 static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
225                                    u8 vsi_queue_id)
226 {
227         struct i40e_pf *pf = vf->pf;
228         struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
229         u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
230
231         if (!vsi)
232                 return pf_queue_id;
233
234         if (le16_to_cpu(vsi->info.mapping_flags) &
235             I40E_AQ_VSI_QUE_MAP_NONCONTIG)
236                 pf_queue_id =
237                         le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
238         else
239                 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
240                               vsi_queue_id;
241
242         return pf_queue_id;
243 }
244
245 /**
246  * i40e_config_irq_link_list
247  * @vf: pointer to the VF info
248  * @vsi_id: id of VSI as given by the FW
249  * @vecmap: irq map info
250  *
251  * configure irq link list from the map
252  **/
253 static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
254                                       struct virtchnl_vector_map *vecmap)
255 {
256         unsigned long linklistmap = 0, tempmap;
257         struct i40e_pf *pf = vf->pf;
258         struct i40e_hw *hw = &pf->hw;
259         u16 vsi_queue_id, pf_queue_id;
260         enum i40e_queue_type qtype;
261         u16 next_q, vector_id;
262         u32 reg, reg_idx;
263         u16 itr_idx = 0;
264
265         vector_id = vecmap->vector_id;
266         /* setup the head */
267         if (0 == vector_id)
268                 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
269         else
270                 reg_idx = I40E_VPINT_LNKLSTN(
271                      ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
272                      (vector_id - 1));
273
274         if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
275                 /* Special case - No queues mapped on this vector */
276                 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
277                 goto irq_list_done;
278         }
279         tempmap = vecmap->rxq_map;
280         for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
281                 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
282                                     vsi_queue_id));
283         }
284
285         tempmap = vecmap->txq_map;
286         for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
287                 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
288                                      vsi_queue_id + 1));
289         }
290
291         next_q = find_first_bit(&linklistmap,
292                                 (I40E_MAX_VSI_QP *
293                                  I40E_VIRTCHNL_SUPPORTED_QTYPES));
294         vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
295         qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
296         pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
297         reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
298
299         wr32(hw, reg_idx, reg);
300
301         while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
302                 switch (qtype) {
303                 case I40E_QUEUE_TYPE_RX:
304                         reg_idx = I40E_QINT_RQCTL(pf_queue_id);
305                         itr_idx = vecmap->rxitr_idx;
306                         break;
307                 case I40E_QUEUE_TYPE_TX:
308                         reg_idx = I40E_QINT_TQCTL(pf_queue_id);
309                         itr_idx = vecmap->txitr_idx;
310                         break;
311                 default:
312                         break;
313                 }
314
315                 next_q = find_next_bit(&linklistmap,
316                                        (I40E_MAX_VSI_QP *
317                                         I40E_VIRTCHNL_SUPPORTED_QTYPES),
318                                        next_q + 1);
319                 if (next_q <
320                     (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
321                         vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
322                         qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
323                         pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
324                                                               vsi_queue_id);
325                 } else {
326                         pf_queue_id = I40E_QUEUE_END_OF_LIST;
327                         qtype = 0;
328                 }
329
330                 /* format for the RQCTL & TQCTL regs is same */
331                 reg = (vector_id) |
332                     (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
333                     (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
334                     BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
335                     (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
336                 wr32(hw, reg_idx, reg);
337         }
338
339         /* if the vf is running in polling mode and using interrupt zero,
340          * need to disable auto-mask on enabling zero interrupt for VFs.
341          */
342         if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
343             (vector_id == 0)) {
344                 reg = rd32(hw, I40E_GLINT_CTL);
345                 if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
346                         reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
347                         wr32(hw, I40E_GLINT_CTL, reg);
348                 }
349         }
350
351 irq_list_done:
352         i40e_flush(hw);
353 }
354
355 /**
356  * i40e_release_iwarp_qvlist
357  * @vf: pointer to the VF.
358  *
359  **/
360 static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
361 {
362         struct i40e_pf *pf = vf->pf;
363         struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
364         u32 msix_vf;
365         u32 i;
366
367         if (!vf->qvlist_info)
368                 return;
369
370         msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
371         for (i = 0; i < qvlist_info->num_vectors; i++) {
372                 struct virtchnl_iwarp_qv_info *qv_info;
373                 u32 next_q_index, next_q_type;
374                 struct i40e_hw *hw = &pf->hw;
375                 u32 v_idx, reg_idx, reg;
376
377                 qv_info = &qvlist_info->qv_info[i];
378                 if (!qv_info)
379                         continue;
380                 v_idx = qv_info->v_idx;
381                 if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
382                         /* Figure out the queue after CEQ and make that the
383                          * first queue.
384                          */
385                         reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
386                         reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
387                         next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
388                                         >> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
389                         next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
390                                         >> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
391
392                         reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
393                         reg = (next_q_index &
394                                I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
395                                (next_q_type <<
396                                I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
397
398                         wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
399                 }
400         }
401         kfree(vf->qvlist_info);
402         vf->qvlist_info = NULL;
403 }
404
405 /**
406  * i40e_config_iwarp_qvlist
407  * @vf: pointer to the VF info
408  * @qvlist_info: queue and vector list
409  *
410  * Return 0 on success or < 0 on error
411  **/
412 static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
413                                     struct virtchnl_iwarp_qvlist_info *qvlist_info)
414 {
415         struct i40e_pf *pf = vf->pf;
416         struct i40e_hw *hw = &pf->hw;
417         struct virtchnl_iwarp_qv_info *qv_info;
418         u32 v_idx, i, reg_idx, reg;
419         u32 next_q_idx, next_q_type;
420         u32 msix_vf, size;
421         int ret = 0;
422
423         msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
424
425         if (qvlist_info->num_vectors > msix_vf) {
426                 dev_warn(&pf->pdev->dev,
427                          "Incorrect number of iwarp vectors %u. Maximum %u allowed.\n",
428                          qvlist_info->num_vectors,
429                          msix_vf);
430                 ret = -EINVAL;
431                 goto err_out;
432         }
433
434         size = sizeof(struct virtchnl_iwarp_qvlist_info) +
435                (sizeof(struct virtchnl_iwarp_qv_info) *
436                                                 (qvlist_info->num_vectors - 1));
437         kfree(vf->qvlist_info);
438         vf->qvlist_info = kzalloc(size, GFP_KERNEL);
439         if (!vf->qvlist_info) {
440                 ret = -ENOMEM;
441                 goto err_out;
442         }
443         vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
444
445         msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
446         for (i = 0; i < qvlist_info->num_vectors; i++) {
447                 qv_info = &qvlist_info->qv_info[i];
448                 if (!qv_info)
449                         continue;
450                 v_idx = qv_info->v_idx;
451
452                 /* Validate vector id belongs to this vf */
453                 if (!i40e_vc_isvalid_vector_id(vf, v_idx)) {
454                         ret = -EINVAL;
455                         goto err_free;
456                 }
457
458                 vf->qvlist_info->qv_info[i] = *qv_info;
459
460                 reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
461                 /* We might be sharing the interrupt, so get the first queue
462                  * index and type, push it down the list by adding the new
463                  * queue on top. Also link it with the new queue in CEQCTL.
464                  */
465                 reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
466                 next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
467                                 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
468                 next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
469                                 I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
470
471                 if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
472                         reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
473                         reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
474                         (v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
475                         (qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
476                         (next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
477                         (next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
478                         wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
479
480                         reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
481                         reg = (qv_info->ceq_idx &
482                                I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
483                                (I40E_QUEUE_TYPE_PE_CEQ <<
484                                I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
485                         wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
486                 }
487
488                 if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
489                         reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
490                         (v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
491                         (qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
492
493                         wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
494                 }
495         }
496
497         return 0;
498 err_free:
499         kfree(vf->qvlist_info);
500         vf->qvlist_info = NULL;
501 err_out:
502         return ret;
503 }
504
505 /**
506  * i40e_config_vsi_tx_queue
507  * @vf: pointer to the VF info
508  * @vsi_id: id of VSI as provided by the FW
509  * @vsi_queue_id: vsi relative queue index
510  * @info: config. info
511  *
512  * configure tx queue
513  **/
514 static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
515                                     u16 vsi_queue_id,
516                                     struct virtchnl_txq_info *info)
517 {
518         struct i40e_pf *pf = vf->pf;
519         struct i40e_hw *hw = &pf->hw;
520         struct i40e_hmc_obj_txq tx_ctx;
521         struct i40e_vsi *vsi;
522         u16 pf_queue_id;
523         u32 qtx_ctl;
524         int ret = 0;
525
526         if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
527                 ret = -ENOENT;
528                 goto error_context;
529         }
530         pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
531         vsi = i40e_find_vsi_from_id(pf, vsi_id);
532         if (!vsi) {
533                 ret = -ENOENT;
534                 goto error_context;
535         }
536
537         /* clear the context structure first */
538         memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
539
540         /* only set the required fields */
541         tx_ctx.base = info->dma_ring_addr / 128;
542         tx_ctx.qlen = info->ring_len;
543         tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
544         tx_ctx.rdylist_act = 0;
545         tx_ctx.head_wb_ena = info->headwb_enabled;
546         tx_ctx.head_wb_addr = info->dma_headwb_addr;
547
548         /* clear the context in the HMC */
549         ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
550         if (ret) {
551                 dev_err(&pf->pdev->dev,
552                         "Failed to clear VF LAN Tx queue context %d, error: %d\n",
553                         pf_queue_id, ret);
554                 ret = -ENOENT;
555                 goto error_context;
556         }
557
558         /* set the context in the HMC */
559         ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
560         if (ret) {
561                 dev_err(&pf->pdev->dev,
562                         "Failed to set VF LAN Tx queue context %d error: %d\n",
563                         pf_queue_id, ret);
564                 ret = -ENOENT;
565                 goto error_context;
566         }
567
568         /* associate this queue with the PCI VF function */
569         qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
570         qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
571                     & I40E_QTX_CTL_PF_INDX_MASK);
572         qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
573                      << I40E_QTX_CTL_VFVM_INDX_SHIFT)
574                     & I40E_QTX_CTL_VFVM_INDX_MASK);
575         wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
576         i40e_flush(hw);
577
578 error_context:
579         return ret;
580 }
581
582 /**
583  * i40e_config_vsi_rx_queue
584  * @vf: pointer to the VF info
585  * @vsi_id: id of VSI  as provided by the FW
586  * @vsi_queue_id: vsi relative queue index
587  * @info: config. info
588  *
589  * configure rx queue
590  **/
591 static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
592                                     u16 vsi_queue_id,
593                                     struct virtchnl_rxq_info *info)
594 {
595         struct i40e_pf *pf = vf->pf;
596         struct i40e_hw *hw = &pf->hw;
597         struct i40e_hmc_obj_rxq rx_ctx;
598         u16 pf_queue_id;
599         int ret = 0;
600
601         pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
602
603         /* clear the context structure first */
604         memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
605
606         /* only set the required fields */
607         rx_ctx.base = info->dma_ring_addr / 128;
608         rx_ctx.qlen = info->ring_len;
609
610         if (info->splithdr_enabled) {
611                 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2      |
612                                   I40E_RX_SPLIT_IP      |
613                                   I40E_RX_SPLIT_TCP_UDP |
614                                   I40E_RX_SPLIT_SCTP;
615                 /* header length validation */
616                 if (info->hdr_size > ((2 * 1024) - 64)) {
617                         ret = -EINVAL;
618                         goto error_param;
619                 }
620                 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
621
622                 /* set split mode 10b */
623                 rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
624         }
625
626         /* databuffer length validation */
627         if (info->databuffer_size > ((16 * 1024) - 128)) {
628                 ret = -EINVAL;
629                 goto error_param;
630         }
631         rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
632
633         /* max pkt. length validation */
634         if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
635                 ret = -EINVAL;
636                 goto error_param;
637         }
638         rx_ctx.rxmax = info->max_pkt_size;
639
640         /* enable 32bytes desc always */
641         rx_ctx.dsize = 1;
642
643         /* default values */
644         rx_ctx.lrxqthresh = 2;
645         rx_ctx.crcstrip = 1;
646         rx_ctx.prefena = 1;
647         rx_ctx.l2tsel = 1;
648
649         /* clear the context in the HMC */
650         ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
651         if (ret) {
652                 dev_err(&pf->pdev->dev,
653                         "Failed to clear VF LAN Rx queue context %d, error: %d\n",
654                         pf_queue_id, ret);
655                 ret = -ENOENT;
656                 goto error_param;
657         }
658
659         /* set the context in the HMC */
660         ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
661         if (ret) {
662                 dev_err(&pf->pdev->dev,
663                         "Failed to set VF LAN Rx queue context %d error: %d\n",
664                         pf_queue_id, ret);
665                 ret = -ENOENT;
666                 goto error_param;
667         }
668
669 error_param:
670         return ret;
671 }
672
673 /**
674  * i40e_alloc_vsi_res
675  * @vf: pointer to the VF info
676  * @type: type of VSI to allocate
677  *
678  * alloc VF vsi context & resources
679  **/
680 static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
681 {
682         struct i40e_mac_filter *f = NULL;
683         struct i40e_pf *pf = vf->pf;
684         struct i40e_vsi *vsi;
685         int ret = 0;
686
687         vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
688
689         if (!vsi) {
690                 dev_err(&pf->pdev->dev,
691                         "add vsi failed for VF %d, aq_err %d\n",
692                         vf->vf_id, pf->hw.aq.asq_last_status);
693                 ret = -ENOENT;
694                 goto error_alloc_vsi_res;
695         }
696         if (type == I40E_VSI_SRIOV) {
697                 u64 hena = i40e_pf_get_default_rss_hena(pf);
698                 u8 broadcast[ETH_ALEN];
699
700                 vf->lan_vsi_idx = vsi->idx;
701                 vf->lan_vsi_id = vsi->id;
702                 /* If the port VLAN has been configured and then the
703                  * VF driver was removed then the VSI port VLAN
704                  * configuration was destroyed.  Check if there is
705                  * a port VLAN and restore the VSI configuration if
706                  * needed.
707                  */
708                 if (vf->port_vlan_id)
709                         i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
710
711                 spin_lock_bh(&vsi->mac_filter_hash_lock);
712                 if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
713                         f = i40e_add_mac_filter(vsi,
714                                                 vf->default_lan_addr.addr);
715                         if (!f)
716                                 dev_info(&pf->pdev->dev,
717                                          "Could not add MAC filter %pM for VF %d\n",
718                                         vf->default_lan_addr.addr, vf->vf_id);
719                 }
720                 eth_broadcast_addr(broadcast);
721                 f = i40e_add_mac_filter(vsi, broadcast);
722                 if (!f)
723                         dev_info(&pf->pdev->dev,
724                                  "Could not allocate VF broadcast filter\n");
725                 spin_unlock_bh(&vsi->mac_filter_hash_lock);
726                 wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
727                 wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
728         }
729
730         /* program mac filter */
731         ret = i40e_sync_vsi_filters(vsi);
732         if (ret)
733                 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
734
735         /* Set VF bandwidth if specified */
736         if (vf->tx_rate) {
737                 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
738                                                   vf->tx_rate / 50, 0, NULL);
739                 if (ret)
740                         dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
741                                 vf->vf_id, ret);
742         }
743
744 error_alloc_vsi_res:
745         return ret;
746 }
747
748 /**
749  * i40e_enable_vf_mappings
750  * @vf: pointer to the VF info
751  *
752  * enable VF mappings
753  **/
754 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
755 {
756         struct i40e_pf *pf = vf->pf;
757         struct i40e_hw *hw = &pf->hw;
758         u32 reg, total_queue_pairs = 0;
759         int j;
760
761         /* Tell the hardware we're using noncontiguous mapping. HW requires
762          * that VF queues be mapped using this method, even when they are
763          * contiguous in real life
764          */
765         i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
766                           I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
767
768         /* enable VF vplan_qtable mappings */
769         reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
770         wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
771
772         /* map PF queues to VF queues */
773         for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
774                 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
775
776                 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
777                 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
778                 total_queue_pairs++;
779         }
780
781         /* map PF queues to VSI */
782         for (j = 0; j < 7; j++) {
783                 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
784                         reg = 0x07FF07FF;       /* unused */
785                 } else {
786                         u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
787                                                           j * 2);
788                         reg = qid;
789                         qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
790                                                       (j * 2) + 1);
791                         reg |= qid << 16;
792                 }
793                 i40e_write_rx_ctl(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id),
794                                   reg);
795         }
796
797         i40e_flush(hw);
798 }
799
800 /**
801  * i40e_disable_vf_mappings
802  * @vf: pointer to the VF info
803  *
804  * disable VF mappings
805  **/
806 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
807 {
808         struct i40e_pf *pf = vf->pf;
809         struct i40e_hw *hw = &pf->hw;
810         int i;
811
812         /* disable qp mappings */
813         wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
814         for (i = 0; i < I40E_MAX_VSI_QP; i++)
815                 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
816                      I40E_QUEUE_END_OF_LIST);
817         i40e_flush(hw);
818 }
819
820 /**
821  * i40e_free_vf_res
822  * @vf: pointer to the VF info
823  *
824  * free VF resources
825  **/
826 static void i40e_free_vf_res(struct i40e_vf *vf)
827 {
828         struct i40e_pf *pf = vf->pf;
829         struct i40e_hw *hw = &pf->hw;
830         u32 reg_idx, reg;
831         int i, msix_vf;
832
833         /* Start by disabling VF's configuration API to prevent the OS from
834          * accessing the VF's VSI after it's freed / invalidated.
835          */
836         clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
837
838         /* free vsi & disconnect it from the parent uplink */
839         if (vf->lan_vsi_idx) {
840                 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
841                 vf->lan_vsi_idx = 0;
842                 vf->lan_vsi_id = 0;
843                 vf->num_mac = 0;
844         }
845         msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
846
847         /* disable interrupts so the VF starts in a known state */
848         for (i = 0; i < msix_vf; i++) {
849                 /* format is same for both registers */
850                 if (0 == i)
851                         reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
852                 else
853                         reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
854                                                       (vf->vf_id))
855                                                      + (i - 1));
856                 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
857                 i40e_flush(hw);
858         }
859
860         /* clear the irq settings */
861         for (i = 0; i < msix_vf; i++) {
862                 /* format is same for both registers */
863                 if (0 == i)
864                         reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
865                 else
866                         reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
867                                                       (vf->vf_id))
868                                                      + (i - 1));
869                 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
870                        I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
871                 wr32(hw, reg_idx, reg);
872                 i40e_flush(hw);
873         }
874         /* reset some of the state variables keeping track of the resources */
875         vf->num_queue_pairs = 0;
876         vf->vf_states = 0;
877 }
878
879 /**
880  * i40e_alloc_vf_res
881  * @vf: pointer to the VF info
882  *
883  * allocate VF resources
884  **/
885 static int i40e_alloc_vf_res(struct i40e_vf *vf)
886 {
887         struct i40e_pf *pf = vf->pf;
888         int total_queue_pairs = 0;
889         int ret;
890
891         /* allocate hw vsi context & associated resources */
892         ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
893         if (ret)
894                 goto error_alloc;
895         total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
896
897         if (vf->trusted)
898                 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
899         else
900                 clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
901
902         /* store the total qps number for the runtime
903          * VF req validation
904          */
905         vf->num_queue_pairs = total_queue_pairs;
906
907         /* VF is now completely initialized */
908         set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
909
910 error_alloc:
911         if (ret)
912                 i40e_free_vf_res(vf);
913
914         return ret;
915 }
916
917 #define VF_DEVICE_STATUS 0xAA
918 #define VF_TRANS_PENDING_MASK 0x20
919 /**
920  * i40e_quiesce_vf_pci
921  * @vf: pointer to the VF structure
922  *
923  * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
924  * if the transactions never clear.
925  **/
926 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
927 {
928         struct i40e_pf *pf = vf->pf;
929         struct i40e_hw *hw = &pf->hw;
930         int vf_abs_id, i;
931         u32 reg;
932
933         vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
934
935         wr32(hw, I40E_PF_PCI_CIAA,
936              VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
937         for (i = 0; i < 100; i++) {
938                 reg = rd32(hw, I40E_PF_PCI_CIAD);
939                 if ((reg & VF_TRANS_PENDING_MASK) == 0)
940                         return 0;
941                 udelay(1);
942         }
943         return -EIO;
944 }
945
946 /**
947  * i40e_trigger_vf_reset
948  * @vf: pointer to the VF structure
949  * @flr: VFLR was issued or not
950  *
951  * Trigger hardware to start a reset for a particular VF. Expects the caller
952  * to wait the proper amount of time to allow hardware to reset the VF before
953  * it cleans up and restores VF functionality.
954  **/
955 static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
956 {
957         struct i40e_pf *pf = vf->pf;
958         struct i40e_hw *hw = &pf->hw;
959         u32 reg, reg_idx, bit_idx;
960
961         /* warn the VF */
962         clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
963
964         /* Disable VF's configuration API during reset. The flag is re-enabled
965          * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
966          * It's normally disabled in i40e_free_vf_res(), but it's safer
967          * to do it earlier to give some time to finish to any VF config
968          * functions that may still be running at this point.
969          */
970         clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
971
972         /* In the case of a VFLR, the HW has already reset the VF and we
973          * just need to clean up, so don't hit the VFRTRIG register.
974          */
975         if (!flr) {
976                 /* reset VF using VPGEN_VFRTRIG reg */
977                 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
978                 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
979                 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
980                 i40e_flush(hw);
981         }
982         /* clear the VFLR bit in GLGEN_VFLRSTAT */
983         reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
984         bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
985         wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
986         i40e_flush(hw);
987
988         if (i40e_quiesce_vf_pci(vf))
989                 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
990                         vf->vf_id);
991 }
992
993 /**
994  * i40e_cleanup_reset_vf
995  * @vf: pointer to the VF structure
996  *
997  * Cleanup a VF after the hardware reset is finished. Expects the caller to
998  * have verified whether the reset is finished properly, and ensure the
999  * minimum amount of wait time has passed.
1000  **/
1001 static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
1002 {
1003         struct i40e_pf *pf = vf->pf;
1004         struct i40e_hw *hw = &pf->hw;
1005         u32 reg;
1006
1007         /* free VF resources to begin resetting the VSI state */
1008         i40e_free_vf_res(vf);
1009
1010         /* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
1011          * By doing this we allow HW to access VF memory at any point. If we
1012          * did it any sooner, HW could access memory while it was being freed
1013          * in i40e_free_vf_res(), causing an IOMMU fault.
1014          *
1015          * On the other hand, this needs to be done ASAP, because the VF driver
1016          * is waiting for this to happen and may report a timeout. It's
1017          * harmless, but it gets logged into Guest OS kernel log, so best avoid
1018          * it.
1019          */
1020         reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1021         reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1022         wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1023
1024         /* reallocate VF resources to finish resetting the VSI state */
1025         if (!i40e_alloc_vf_res(vf)) {
1026                 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1027                 i40e_enable_vf_mappings(vf);
1028                 set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1029                 clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1030                 /* Do not notify the client during VF init */
1031                 if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
1032                                         &vf->vf_states))
1033                         i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1034                 vf->num_vlan = 0;
1035         }
1036
1037         /* Tell the VF driver the reset is done. This needs to be done only
1038          * after VF has been fully initialized, because the VF driver may
1039          * request resources immediately after setting this flag.
1040          */
1041         wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1042 }
1043
1044 /**
1045  * i40e_reset_vf
1046  * @vf: pointer to the VF structure
1047  * @flr: VFLR was issued or not
1048  *
1049  * reset the VF
1050  **/
1051 void i40e_reset_vf(struct i40e_vf *vf, bool flr)
1052 {
1053         struct i40e_pf *pf = vf->pf;
1054         struct i40e_hw *hw = &pf->hw;
1055         bool rsd = false;
1056         u32 reg;
1057         int i;
1058
1059         /* If VFs have been disabled, there is no need to reset */
1060         if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1061                 return;
1062
1063         i40e_trigger_vf_reset(vf, flr);
1064
1065         /* poll VPGEN_VFRSTAT reg to make sure
1066          * that reset is complete
1067          */
1068         for (i = 0; i < 10; i++) {
1069                 /* VF reset requires driver to first reset the VF and then
1070                  * poll the status register to make sure that the reset
1071                  * completed successfully. Due to internal HW FIFO flushes,
1072                  * we must wait 10ms before the register will be valid.
1073                  */
1074                 usleep_range(10000, 20000);
1075                 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1076                 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
1077                         rsd = true;
1078                         break;
1079                 }
1080         }
1081
1082         if (flr)
1083                 usleep_range(10000, 20000);
1084
1085         if (!rsd)
1086                 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1087                         vf->vf_id);
1088         usleep_range(10000, 20000);
1089
1090         /* On initial reset, we don't have any queues to disable */
1091         if (vf->lan_vsi_idx != 0)
1092                 i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1093
1094         i40e_cleanup_reset_vf(vf);
1095
1096         i40e_flush(hw);
1097         clear_bit(__I40E_VF_DISABLE, pf->state);
1098 }
1099
1100 /**
1101  * i40e_reset_all_vfs
1102  * @pf: pointer to the PF structure
1103  * @flr: VFLR was issued or not
1104  *
1105  * Reset all allocated VFs in one go. First, tell the hardware to reset each
1106  * VF, then do all the waiting in one chunk, and finally finish restoring each
1107  * VF after the wait. This is useful during PF routines which need to reset
1108  * all VFs, as otherwise it must perform these resets in a serialized fashion.
1109  **/
1110 void i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
1111 {
1112         struct i40e_hw *hw = &pf->hw;
1113         struct i40e_vf *vf;
1114         int i, v;
1115         u32 reg;
1116
1117         /* If we don't have any VFs, then there is nothing to reset */
1118         if (!pf->num_alloc_vfs)
1119                 return;
1120
1121         /* If VFs have been disabled, there is no need to reset */
1122         if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1123                 return;
1124
1125         /* Begin reset on all VFs at once */
1126         for (v = 0; v < pf->num_alloc_vfs; v++)
1127                 i40e_trigger_vf_reset(&pf->vf[v], flr);
1128
1129         /* HW requires some time to make sure it can flush the FIFO for a VF
1130          * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1131          * sequence to make sure that it has completed. We'll keep track of
1132          * the VFs using a simple iterator that increments once that VF has
1133          * finished resetting.
1134          */
1135         for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1136                 usleep_range(10000, 20000);
1137
1138                 /* Check each VF in sequence, beginning with the VF to fail
1139                  * the previous check.
1140                  */
1141                 while (v < pf->num_alloc_vfs) {
1142                         vf = &pf->vf[v];
1143                         reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1144                         if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
1145                                 break;
1146
1147                         /* If the current VF has finished resetting, move on
1148                          * to the next VF in sequence.
1149                          */
1150                         v++;
1151                 }
1152         }
1153
1154         if (flr)
1155                 usleep_range(10000, 20000);
1156
1157         /* Display a warning if at least one VF didn't manage to reset in
1158          * time, but continue on with the operation.
1159          */
1160         if (v < pf->num_alloc_vfs)
1161                 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1162                         pf->vf[v].vf_id);
1163         usleep_range(10000, 20000);
1164
1165         /* Begin disabling all the rings associated with VFs, but do not wait
1166          * between each VF.
1167          */
1168         for (v = 0; v < pf->num_alloc_vfs; v++) {
1169                 /* On initial reset, we don't have any queues to disable */
1170                 if (pf->vf[v].lan_vsi_idx == 0)
1171                         continue;
1172
1173                 i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
1174         }
1175
1176         /* Now that we've notified HW to disable all of the VF rings, wait
1177          * until they finish.
1178          */
1179         for (v = 0; v < pf->num_alloc_vfs; v++) {
1180                 /* On initial reset, we don't have any queues to disable */
1181                 if (pf->vf[v].lan_vsi_idx == 0)
1182                         continue;
1183
1184                 i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
1185         }
1186
1187         /* Hw may need up to 50ms to finish disabling the RX queues. We
1188          * minimize the wait by delaying only once for all VFs.
1189          */
1190         mdelay(50);
1191
1192         /* Finish the reset on each VF */
1193         for (v = 0; v < pf->num_alloc_vfs; v++)
1194                 i40e_cleanup_reset_vf(&pf->vf[v]);
1195
1196         i40e_flush(hw);
1197         clear_bit(__I40E_VF_DISABLE, pf->state);
1198 }
1199
1200 /**
1201  * i40e_free_vfs
1202  * @pf: pointer to the PF structure
1203  *
1204  * free VF resources
1205  **/
1206 void i40e_free_vfs(struct i40e_pf *pf)
1207 {
1208         struct i40e_hw *hw = &pf->hw;
1209         u32 reg_idx, bit_idx;
1210         int i, tmp, vf_id;
1211
1212         if (!pf->vf)
1213                 return;
1214         while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1215                 usleep_range(1000, 2000);
1216
1217         i40e_notify_client_of_vf_enable(pf, 0);
1218
1219         /* Amortize wait time by stopping all VFs at the same time */
1220         for (i = 0; i < pf->num_alloc_vfs; i++) {
1221                 if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1222                         continue;
1223
1224                 i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
1225         }
1226
1227         for (i = 0; i < pf->num_alloc_vfs; i++) {
1228                 if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1229                         continue;
1230
1231                 i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
1232         }
1233
1234         /* Disable IOV before freeing resources. This lets any VF drivers
1235          * running in the host get themselves cleaned up before we yank
1236          * the carpet out from underneath their feet.
1237          */
1238         if (!pci_vfs_assigned(pf->pdev))
1239                 pci_disable_sriov(pf->pdev);
1240         else
1241                 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1242
1243         /* free up VF resources */
1244         tmp = pf->num_alloc_vfs;
1245         pf->num_alloc_vfs = 0;
1246         for (i = 0; i < tmp; i++) {
1247                 if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1248                         i40e_free_vf_res(&pf->vf[i]);
1249                 /* disable qp mappings */
1250                 i40e_disable_vf_mappings(&pf->vf[i]);
1251         }
1252
1253         kfree(pf->vf);
1254         pf->vf = NULL;
1255
1256         /* This check is for when the driver is unloaded while VFs are
1257          * assigned. Setting the number of VFs to 0 through sysfs is caught
1258          * before this function ever gets called.
1259          */
1260         if (!pci_vfs_assigned(pf->pdev)) {
1261                 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
1262                  * work correctly when SR-IOV gets re-enabled.
1263                  */
1264                 for (vf_id = 0; vf_id < tmp; vf_id++) {
1265                         reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1266                         bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1267                         wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1268                 }
1269         }
1270         clear_bit(__I40E_VF_DISABLE, pf->state);
1271 }
1272
1273 #ifdef CONFIG_PCI_IOV
1274 /**
1275  * i40e_alloc_vfs
1276  * @pf: pointer to the PF structure
1277  * @num_alloc_vfs: number of VFs to allocate
1278  *
1279  * allocate VF resources
1280  **/
1281 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1282 {
1283         struct i40e_vf *vfs;
1284         int i, ret = 0;
1285
1286         /* Disable interrupt 0 so we don't try to handle the VFLR. */
1287         i40e_irq_dynamic_disable_icr0(pf);
1288
1289         /* Check to see if we're just allocating resources for extant VFs */
1290         if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
1291                 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1292                 if (ret) {
1293                         pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1294                         pf->num_alloc_vfs = 0;
1295                         goto err_iov;
1296                 }
1297         }
1298         /* allocate memory */
1299         vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1300         if (!vfs) {
1301                 ret = -ENOMEM;
1302                 goto err_alloc;
1303         }
1304         pf->vf = vfs;
1305
1306         /* apply default profile */
1307         for (i = 0; i < num_alloc_vfs; i++) {
1308                 vfs[i].pf = pf;
1309                 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
1310                 vfs[i].vf_id = i;
1311
1312                 /* assign default capabilities */
1313                 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1314                 vfs[i].spoofchk = true;
1315
1316                 set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
1317
1318         }
1319         pf->num_alloc_vfs = num_alloc_vfs;
1320
1321         /* VF resources get allocated during reset */
1322         i40e_reset_all_vfs(pf, false);
1323
1324         i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
1325
1326 err_alloc:
1327         if (ret)
1328                 i40e_free_vfs(pf);
1329 err_iov:
1330         /* Re-enable interrupt 0. */
1331         i40e_irq_dynamic_enable_icr0(pf, false);
1332         return ret;
1333 }
1334
1335 #endif
1336 /**
1337  * i40e_pci_sriov_enable
1338  * @pdev: pointer to a pci_dev structure
1339  * @num_vfs: number of VFs to allocate
1340  *
1341  * Enable or change the number of VFs
1342  **/
1343 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1344 {
1345 #ifdef CONFIG_PCI_IOV
1346         struct i40e_pf *pf = pci_get_drvdata(pdev);
1347         int pre_existing_vfs = pci_num_vf(pdev);
1348         int err = 0;
1349
1350         if (test_bit(__I40E_TESTING, pf->state)) {
1351                 dev_warn(&pdev->dev,
1352                          "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1353                 err = -EPERM;
1354                 goto err_out;
1355         }
1356
1357         if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1358                 i40e_free_vfs(pf);
1359         else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1360                 goto out;
1361
1362         if (num_vfs > pf->num_req_vfs) {
1363                 dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1364                          num_vfs, pf->num_req_vfs);
1365                 err = -EPERM;
1366                 goto err_out;
1367         }
1368
1369         dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1370         err = i40e_alloc_vfs(pf, num_vfs);
1371         if (err) {
1372                 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1373                 goto err_out;
1374         }
1375
1376 out:
1377         return num_vfs;
1378
1379 err_out:
1380         return err;
1381 #endif
1382         return 0;
1383 }
1384
1385 /**
1386  * i40e_pci_sriov_configure
1387  * @pdev: pointer to a pci_dev structure
1388  * @num_vfs: number of VFs to allocate
1389  *
1390  * Enable or change the number of VFs. Called when the user updates the number
1391  * of VFs in sysfs.
1392  **/
1393 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1394 {
1395         struct i40e_pf *pf = pci_get_drvdata(pdev);
1396
1397         if (num_vfs) {
1398                 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1399                         pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1400                         i40e_do_reset_safe(pf,
1401                                            BIT_ULL(__I40E_PF_RESET_REQUESTED));
1402                 }
1403                 return i40e_pci_sriov_enable(pdev, num_vfs);
1404         }
1405
1406         if (!pci_vfs_assigned(pf->pdev)) {
1407                 i40e_free_vfs(pf);
1408                 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1409                 i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
1410         } else {
1411                 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1412                 return -EINVAL;
1413         }
1414         return 0;
1415 }
1416
1417 /***********************virtual channel routines******************/
1418
1419 /**
1420  * i40e_vc_send_msg_to_vf
1421  * @vf: pointer to the VF info
1422  * @v_opcode: virtual channel opcode
1423  * @v_retval: virtual channel return value
1424  * @msg: pointer to the msg buffer
1425  * @msglen: msg length
1426  *
1427  * send msg to VF
1428  **/
1429 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1430                                   u32 v_retval, u8 *msg, u16 msglen)
1431 {
1432         struct i40e_pf *pf;
1433         struct i40e_hw *hw;
1434         int abs_vf_id;
1435         i40e_status aq_ret;
1436
1437         /* validate the request */
1438         if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1439                 return -EINVAL;
1440
1441         pf = vf->pf;
1442         hw = &pf->hw;
1443         abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1444
1445         /* single place to detect unsuccessful return values */
1446         if (v_retval) {
1447                 vf->num_invalid_msgs++;
1448                 dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1449                          vf->vf_id, v_opcode, v_retval);
1450                 if (vf->num_invalid_msgs >
1451                     I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1452                         dev_err(&pf->pdev->dev,
1453                                 "Number of invalid messages exceeded for VF %d\n",
1454                                 vf->vf_id);
1455                         dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1456                         set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1457                 }
1458         } else {
1459                 vf->num_valid_msgs++;
1460                 /* reset the invalid counter, if a valid message is received. */
1461                 vf->num_invalid_msgs = 0;
1462         }
1463
1464         aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,  v_opcode, v_retval,
1465                                         msg, msglen, NULL);
1466         if (aq_ret) {
1467                 dev_info(&pf->pdev->dev,
1468                          "Unable to send the message to VF %d aq_err %d\n",
1469                          vf->vf_id, pf->hw.aq.asq_last_status);
1470                 return -EIO;
1471         }
1472
1473         return 0;
1474 }
1475
1476 /**
1477  * i40e_vc_send_resp_to_vf
1478  * @vf: pointer to the VF info
1479  * @opcode: operation code
1480  * @retval: return value
1481  *
1482  * send resp msg to VF
1483  **/
1484 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1485                                    enum virtchnl_ops opcode,
1486                                    i40e_status retval)
1487 {
1488         return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1489 }
1490
1491 /**
1492  * i40e_vc_get_version_msg
1493  * @vf: pointer to the VF info
1494  *
1495  * called from the VF to request the API version used by the PF
1496  **/
1497 static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
1498 {
1499         struct virtchnl_version_info info = {
1500                 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1501         };
1502
1503         vf->vf_ver = *(struct virtchnl_version_info *)msg;
1504         /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1505         if (VF_IS_V10(&vf->vf_ver))
1506                 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1507         return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1508                                       I40E_SUCCESS, (u8 *)&info,
1509                                       sizeof(struct virtchnl_version_info));
1510 }
1511
1512 /**
1513  * i40e_vc_get_vf_resources_msg
1514  * @vf: pointer to the VF info
1515  * @msg: pointer to the msg buffer
1516  * @msglen: msg length
1517  *
1518  * called from the VF to request its resources
1519  **/
1520 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
1521 {
1522         struct virtchnl_vf_resource *vfres = NULL;
1523         struct i40e_pf *pf = vf->pf;
1524         i40e_status aq_ret = 0;
1525         struct i40e_vsi *vsi;
1526         int num_vsis = 1;
1527         int len = 0;
1528         int ret;
1529
1530         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
1531                 aq_ret = I40E_ERR_PARAM;
1532                 goto err;
1533         }
1534
1535         len = (sizeof(struct virtchnl_vf_resource) +
1536                sizeof(struct virtchnl_vsi_resource) * num_vsis);
1537
1538         vfres = kzalloc(len, GFP_KERNEL);
1539         if (!vfres) {
1540                 aq_ret = I40E_ERR_NO_MEMORY;
1541                 len = 0;
1542                 goto err;
1543         }
1544         if (VF_IS_V11(&vf->vf_ver))
1545                 vf->driver_caps = *(u32 *)msg;
1546         else
1547                 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1548                                   VIRTCHNL_VF_OFFLOAD_RSS_REG |
1549                                   VIRTCHNL_VF_OFFLOAD_VLAN;
1550
1551         vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1552         vsi = pf->vsi[vf->lan_vsi_idx];
1553         if (!vsi->info.pvid)
1554                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1555
1556         if (i40e_vf_client_capable(pf, vf->vf_id) &&
1557             (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
1558                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
1559                 set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1560         }
1561
1562         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1563                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1564         } else {
1565                 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
1566                     (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
1567                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1568                 else
1569                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1570         }
1571
1572         if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
1573                 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1574                         vfres->vf_cap_flags |=
1575                                 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1576         }
1577
1578         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1579                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1580
1581         if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
1582             (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
1583                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1584
1585         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
1586                 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
1587                         dev_err(&pf->pdev->dev,
1588                                 "VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
1589                                  vf->vf_id);
1590                         aq_ret = I40E_ERR_PARAM;
1591                         goto err;
1592                 }
1593                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1594         }
1595
1596         if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
1597                 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1598                         vfres->vf_cap_flags |=
1599                                         VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1600         }
1601
1602         vfres->num_vsis = num_vsis;
1603         vfres->num_queue_pairs = vf->num_queue_pairs;
1604         vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1605         vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
1606         vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
1607
1608         if (vf->lan_vsi_idx) {
1609                 vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
1610                 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1611                 vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
1612                 /* VFs only use TC 0 */
1613                 vfres->vsi_res[0].qset_handle
1614                                           = le16_to_cpu(vsi->info.qs_handle[0]);
1615                 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1616                                 vf->default_lan_addr.addr);
1617         }
1618         set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1619
1620 err:
1621         /* send the response back to the VF */
1622         ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
1623                                      aq_ret, (u8 *)vfres, len);
1624
1625         kfree(vfres);
1626         return ret;
1627 }
1628
1629 /**
1630  * i40e_vc_reset_vf_msg
1631  * @vf: pointer to the VF info
1632  * @msg: pointer to the msg buffer
1633  * @msglen: msg length
1634  *
1635  * called from the VF to reset itself,
1636  * unlike other virtchnl messages, PF driver
1637  * doesn't send the response back to the VF
1638  **/
1639 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1640 {
1641         if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
1642                 i40e_reset_vf(vf, false);
1643 }
1644
1645 /**
1646  * i40e_getnum_vf_vsi_vlan_filters
1647  * @vsi: pointer to the vsi
1648  *
1649  * called to get the number of VLANs offloaded on this VF
1650  **/
1651 static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
1652 {
1653         struct i40e_mac_filter *f;
1654         int num_vlans = 0, bkt;
1655
1656         hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1657                 if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
1658                         num_vlans++;
1659         }
1660
1661         return num_vlans;
1662 }
1663
1664 /**
1665  * i40e_vc_config_promiscuous_mode_msg
1666  * @vf: pointer to the VF info
1667  * @msg: pointer to the msg buffer
1668  * @msglen: msg length
1669  *
1670  * called from the VF to configure the promiscuous mode of
1671  * VF vsis
1672  **/
1673 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1674                                                u8 *msg, u16 msglen)
1675 {
1676         struct virtchnl_promisc_info *info =
1677             (struct virtchnl_promisc_info *)msg;
1678         struct i40e_pf *pf = vf->pf;
1679         struct i40e_hw *hw = &pf->hw;
1680         struct i40e_mac_filter *f;
1681         i40e_status aq_ret = 0;
1682         bool allmulti = false;
1683         struct i40e_vsi *vsi;
1684         bool alluni = false;
1685         int aq_err = 0;
1686         int bkt;
1687
1688         vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
1689         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
1690             !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1691             !vsi) {
1692                 aq_ret = I40E_ERR_PARAM;
1693                 goto error_param;
1694         }
1695         if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
1696                 dev_err(&pf->pdev->dev,
1697                         "Unprivileged VF %d is attempting to configure promiscuous mode\n",
1698                         vf->vf_id);
1699                 /* Lie to the VF on purpose. */
1700                 aq_ret = 0;
1701                 goto error_param;
1702         }
1703         /* Multicast promiscuous handling*/
1704         if (info->flags & FLAG_VF_MULTICAST_PROMISC)
1705                 allmulti = true;
1706
1707         if (vf->port_vlan_id) {
1708                 aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
1709                                                             allmulti,
1710                                                             vf->port_vlan_id,
1711                                                             NULL);
1712         } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1713                 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1714                         if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1715                                 continue;
1716                         aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
1717                                                                     vsi->seid,
1718                                                                     allmulti,
1719                                                                     f->vlan,
1720                                                                     NULL);
1721                         aq_err = pf->hw.aq.asq_last_status;
1722                         if (aq_ret) {
1723                                 dev_err(&pf->pdev->dev,
1724                                         "Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
1725                                         f->vlan,
1726                                         i40e_stat_str(&pf->hw, aq_ret),
1727                                         i40e_aq_str(&pf->hw, aq_err));
1728                                 break;
1729                         }
1730                 }
1731         } else {
1732                 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
1733                                                                allmulti, NULL);
1734                 aq_err = pf->hw.aq.asq_last_status;
1735                 if (aq_ret) {
1736                         dev_err(&pf->pdev->dev,
1737                                 "VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1738                                 vf->vf_id,
1739                                 i40e_stat_str(&pf->hw, aq_ret),
1740                                 i40e_aq_str(&pf->hw, aq_err));
1741                         goto error_param;
1742                 }
1743         }
1744
1745         if (!aq_ret) {
1746                 dev_info(&pf->pdev->dev,
1747                          "VF %d successfully set multicast promiscuous mode\n",
1748                          vf->vf_id);
1749                 if (allmulti)
1750                         set_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
1751                 else
1752                         clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
1753         }
1754
1755         if (info->flags & FLAG_VF_UNICAST_PROMISC)
1756                 alluni = true;
1757         if (vf->port_vlan_id) {
1758                 aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
1759                                                             alluni,
1760                                                             vf->port_vlan_id,
1761                                                             NULL);
1762         } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1763                 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1764                         if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1765                                 continue;
1766                         aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
1767                                                                     vsi->seid,
1768                                                                     alluni,
1769                                                                     f->vlan,
1770                                                                     NULL);
1771                         aq_err = pf->hw.aq.asq_last_status;
1772                         if (aq_ret)
1773                                 dev_err(&pf->pdev->dev,
1774                                         "Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
1775                                         f->vlan,
1776                                         i40e_stat_str(&pf->hw, aq_ret),
1777                                         i40e_aq_str(&pf->hw, aq_err));
1778                 }
1779         } else {
1780                 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
1781                                                              alluni, NULL,
1782                                                              true);
1783                 aq_err = pf->hw.aq.asq_last_status;
1784                 if (aq_ret) {
1785                         dev_err(&pf->pdev->dev,
1786                                 "VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
1787                                 vf->vf_id, info->flags,
1788                                 i40e_stat_str(&pf->hw, aq_ret),
1789                                 i40e_aq_str(&pf->hw, aq_err));
1790                         goto error_param;
1791                 }
1792         }
1793
1794         if (!aq_ret) {
1795                 dev_info(&pf->pdev->dev,
1796                          "VF %d successfully set unicast promiscuous mode\n",
1797                          vf->vf_id);
1798                 if (alluni)
1799                         set_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
1800                 else
1801                         clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
1802         }
1803
1804 error_param:
1805         /* send the response to the VF */
1806         return i40e_vc_send_resp_to_vf(vf,
1807                                        VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1808                                        aq_ret);
1809 }
1810
1811 /**
1812  * i40e_vc_config_queues_msg
1813  * @vf: pointer to the VF info
1814  * @msg: pointer to the msg buffer
1815  * @msglen: msg length
1816  *
1817  * called from the VF to configure the rx/tx
1818  * queues
1819  **/
1820 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1821 {
1822         struct virtchnl_vsi_queue_config_info *qci =
1823             (struct virtchnl_vsi_queue_config_info *)msg;
1824         struct virtchnl_queue_pair_info *qpi;
1825         struct i40e_pf *pf = vf->pf;
1826         u16 vsi_id, vsi_queue_id;
1827         i40e_status aq_ret = 0;
1828         int i;
1829
1830         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1831                 aq_ret = I40E_ERR_PARAM;
1832                 goto error_param;
1833         }
1834
1835         vsi_id = qci->vsi_id;
1836         if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1837                 aq_ret = I40E_ERR_PARAM;
1838                 goto error_param;
1839         }
1840         for (i = 0; i < qci->num_queue_pairs; i++) {
1841                 qpi = &qci->qpair[i];
1842                 vsi_queue_id = qpi->txq.queue_id;
1843                 if ((qpi->txq.vsi_id != vsi_id) ||
1844                     (qpi->rxq.vsi_id != vsi_id) ||
1845                     (qpi->rxq.queue_id != vsi_queue_id) ||
1846                     !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1847                         aq_ret = I40E_ERR_PARAM;
1848                         goto error_param;
1849                 }
1850
1851                 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1852                                              &qpi->rxq) ||
1853                     i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1854                                              &qpi->txq)) {
1855                         aq_ret = I40E_ERR_PARAM;
1856                         goto error_param;
1857                 }
1858         }
1859         /* set vsi num_queue_pairs in use to num configured by VF */
1860         pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
1861
1862 error_param:
1863         /* send the response to the VF */
1864         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1865                                        aq_ret);
1866 }
1867
1868 /**
1869  * i40e_vc_config_irq_map_msg
1870  * @vf: pointer to the VF info
1871  * @msg: pointer to the msg buffer
1872  * @msglen: msg length
1873  *
1874  * called from the VF to configure the irq to
1875  * queue map
1876  **/
1877 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1878 {
1879         struct virtchnl_irq_map_info *irqmap_info =
1880             (struct virtchnl_irq_map_info *)msg;
1881         struct virtchnl_vector_map *map;
1882         u16 vsi_id, vsi_queue_id, vector_id;
1883         i40e_status aq_ret = 0;
1884         unsigned long tempmap;
1885         int i;
1886
1887         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1888                 aq_ret = I40E_ERR_PARAM;
1889                 goto error_param;
1890         }
1891
1892         for (i = 0; i < irqmap_info->num_vectors; i++) {
1893                 map = &irqmap_info->vecmap[i];
1894
1895                 vector_id = map->vector_id;
1896                 vsi_id = map->vsi_id;
1897                 /* validate msg params */
1898                 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1899                     !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1900                         aq_ret = I40E_ERR_PARAM;
1901                         goto error_param;
1902                 }
1903
1904                 /* lookout for the invalid queue index */
1905                 tempmap = map->rxq_map;
1906                 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1907                         if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1908                                                       vsi_queue_id)) {
1909                                 aq_ret = I40E_ERR_PARAM;
1910                                 goto error_param;
1911                         }
1912                 }
1913
1914                 tempmap = map->txq_map;
1915                 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1916                         if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1917                                                       vsi_queue_id)) {
1918                                 aq_ret = I40E_ERR_PARAM;
1919                                 goto error_param;
1920                         }
1921                 }
1922
1923                 i40e_config_irq_link_list(vf, vsi_id, map);
1924         }
1925 error_param:
1926         /* send the response to the VF */
1927         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
1928                                        aq_ret);
1929 }
1930
1931 /**
1932  * i40e_vc_enable_queues_msg
1933  * @vf: pointer to the VF info
1934  * @msg: pointer to the msg buffer
1935  * @msglen: msg length
1936  *
1937  * called from the VF to enable all or specific queue(s)
1938  **/
1939 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1940 {
1941         struct virtchnl_queue_select *vqs =
1942             (struct virtchnl_queue_select *)msg;
1943         struct i40e_pf *pf = vf->pf;
1944         u16 vsi_id = vqs->vsi_id;
1945         i40e_status aq_ret = 0;
1946
1947         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1948                 aq_ret = I40E_ERR_PARAM;
1949                 goto error_param;
1950         }
1951
1952         if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1953                 aq_ret = I40E_ERR_PARAM;
1954                 goto error_param;
1955         }
1956
1957         if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1958                 aq_ret = I40E_ERR_PARAM;
1959                 goto error_param;
1960         }
1961
1962         if (i40e_vsi_start_rings(pf->vsi[vf->lan_vsi_idx]))
1963                 aq_ret = I40E_ERR_TIMEOUT;
1964 error_param:
1965         /* send the response to the VF */
1966         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
1967                                        aq_ret);
1968 }
1969
1970 /**
1971  * i40e_vc_disable_queues_msg
1972  * @vf: pointer to the VF info
1973  * @msg: pointer to the msg buffer
1974  * @msglen: msg length
1975  *
1976  * called from the VF to disable all or specific
1977  * queue(s)
1978  **/
1979 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1980 {
1981         struct virtchnl_queue_select *vqs =
1982             (struct virtchnl_queue_select *)msg;
1983         struct i40e_pf *pf = vf->pf;
1984         i40e_status aq_ret = 0;
1985
1986         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1987                 aq_ret = I40E_ERR_PARAM;
1988                 goto error_param;
1989         }
1990
1991         if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1992                 aq_ret = I40E_ERR_PARAM;
1993                 goto error_param;
1994         }
1995
1996         if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1997                 aq_ret = I40E_ERR_PARAM;
1998                 goto error_param;
1999         }
2000
2001         i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
2002
2003 error_param:
2004         /* send the response to the VF */
2005         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
2006                                        aq_ret);
2007 }
2008
2009 /**
2010  * i40e_vc_get_stats_msg
2011  * @vf: pointer to the VF info
2012  * @msg: pointer to the msg buffer
2013  * @msglen: msg length
2014  *
2015  * called from the VF to get vsi stats
2016  **/
2017 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2018 {
2019         struct virtchnl_queue_select *vqs =
2020             (struct virtchnl_queue_select *)msg;
2021         struct i40e_pf *pf = vf->pf;
2022         struct i40e_eth_stats stats;
2023         i40e_status aq_ret = 0;
2024         struct i40e_vsi *vsi;
2025
2026         memset(&stats, 0, sizeof(struct i40e_eth_stats));
2027
2028         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2029                 aq_ret = I40E_ERR_PARAM;
2030                 goto error_param;
2031         }
2032
2033         if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2034                 aq_ret = I40E_ERR_PARAM;
2035                 goto error_param;
2036         }
2037
2038         vsi = pf->vsi[vf->lan_vsi_idx];
2039         if (!vsi) {
2040                 aq_ret = I40E_ERR_PARAM;
2041                 goto error_param;
2042         }
2043         i40e_update_eth_stats(vsi);
2044         stats = vsi->eth_stats;
2045
2046 error_param:
2047         /* send the response back to the VF */
2048         return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
2049                                       (u8 *)&stats, sizeof(stats));
2050 }
2051
2052 /* If the VF is not trusted restrict the number of MAC/VLAN it can program
2053  * MAC filters: 16 for multicast, 1 for MAC, 1 for broadcast
2054  */
2055 #define I40E_VC_MAX_MAC_ADDR_PER_VF (16 + 1 + 1)
2056 #define I40E_VC_MAX_VLAN_PER_VF 8
2057
2058 /**
2059  * i40e_check_vf_permission
2060  * @vf: pointer to the VF info
2061  * @macaddr: pointer to the MAC Address being checked
2062  *
2063  * Check if the VF has permission to add or delete unicast MAC address
2064  * filters and return error code -EPERM if not.  Then check if the
2065  * address filter requested is broadcast or zero and if so return
2066  * an invalid MAC address error code.
2067  **/
2068 static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
2069 {
2070         struct i40e_pf *pf = vf->pf;
2071         int ret = 0;
2072
2073         if (is_broadcast_ether_addr(macaddr) ||
2074                    is_zero_ether_addr(macaddr)) {
2075                 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
2076                 ret = I40E_ERR_INVALID_MAC_ADDR;
2077         } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
2078                    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2079                    !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
2080                 /* If the host VMM administrator has set the VF MAC address
2081                  * administratively via the ndo_set_vf_mac command then deny
2082                  * permission to the VF to add or delete unicast MAC addresses.
2083                  * Unless the VF is privileged and then it can do whatever.
2084                  * The VF may request to set the MAC address filter already
2085                  * assigned to it so do not return an error in that case.
2086                  */
2087                 dev_err(&pf->pdev->dev,
2088                         "VF attempting to override administratively set MAC address, reload the VF driver to resume normal operation\n");
2089                 ret = -EPERM;
2090         } else if ((vf->num_mac >= I40E_VC_MAX_MAC_ADDR_PER_VF) &&
2091                    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2092                 dev_err(&pf->pdev->dev,
2093                         "VF is not trusted, switch the VF to trusted to add more functionality\n");
2094                 ret = -EPERM;
2095         }
2096         return ret;
2097 }
2098
2099 /**
2100  * i40e_vc_add_mac_addr_msg
2101  * @vf: pointer to the VF info
2102  * @msg: pointer to the msg buffer
2103  * @msglen: msg length
2104  *
2105  * add guest mac address filter
2106  **/
2107 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2108 {
2109         struct virtchnl_ether_addr_list *al =
2110             (struct virtchnl_ether_addr_list *)msg;
2111         struct i40e_pf *pf = vf->pf;
2112         struct i40e_vsi *vsi = NULL;
2113         u16 vsi_id = al->vsi_id;
2114         i40e_status ret = 0;
2115         int i;
2116
2117         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2118             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2119                 ret = I40E_ERR_PARAM;
2120                 goto error_param;
2121         }
2122
2123         for (i = 0; i < al->num_elements; i++) {
2124                 ret = i40e_check_vf_permission(vf, al->list[i].addr);
2125                 if (ret)
2126                         goto error_param;
2127         }
2128         vsi = pf->vsi[vf->lan_vsi_idx];
2129
2130         /* Lock once, because all function inside for loop accesses VSI's
2131          * MAC filter list which needs to be protected using same lock.
2132          */
2133         spin_lock_bh(&vsi->mac_filter_hash_lock);
2134
2135         /* add new addresses to the list */
2136         for (i = 0; i < al->num_elements; i++) {
2137                 struct i40e_mac_filter *f;
2138
2139                 f = i40e_find_mac(vsi, al->list[i].addr);
2140                 if (!f)
2141                         f = i40e_add_mac_filter(vsi, al->list[i].addr);
2142
2143                 if (!f) {
2144                         dev_err(&pf->pdev->dev,
2145                                 "Unable to add MAC filter %pM for VF %d\n",
2146                                  al->list[i].addr, vf->vf_id);
2147                         ret = I40E_ERR_PARAM;
2148                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2149                         goto error_param;
2150                 } else {
2151                         vf->num_mac++;
2152                 }
2153         }
2154         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2155
2156         /* program the updated filter list */
2157         ret = i40e_sync_vsi_filters(vsi);
2158         if (ret)
2159                 dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2160                         vf->vf_id, ret);
2161
2162 error_param:
2163         /* send the response to the VF */
2164         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
2165                                        ret);
2166 }
2167
2168 /**
2169  * i40e_vc_del_mac_addr_msg
2170  * @vf: pointer to the VF info
2171  * @msg: pointer to the msg buffer
2172  * @msglen: msg length
2173  *
2174  * remove guest mac address filter
2175  **/
2176 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2177 {
2178         struct virtchnl_ether_addr_list *al =
2179             (struct virtchnl_ether_addr_list *)msg;
2180         struct i40e_pf *pf = vf->pf;
2181         struct i40e_vsi *vsi = NULL;
2182         u16 vsi_id = al->vsi_id;
2183         i40e_status ret = 0;
2184         int i;
2185
2186         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2187             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2188                 ret = I40E_ERR_PARAM;
2189                 goto error_param;
2190         }
2191
2192         for (i = 0; i < al->num_elements; i++) {
2193                 if (is_broadcast_ether_addr(al->list[i].addr) ||
2194                     is_zero_ether_addr(al->list[i].addr)) {
2195                         dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
2196                                 al->list[i].addr, vf->vf_id);
2197                         ret = I40E_ERR_INVALID_MAC_ADDR;
2198                         goto error_param;
2199                 }
2200
2201                 if (vf->pf_set_mac &&
2202                     ether_addr_equal(al->list[i].addr,
2203                                      vf->default_lan_addr.addr)) {
2204                         dev_err(&pf->pdev->dev,
2205                                 "MAC addr %pM has been set by PF, cannot delete it for VF %d, reset VF to change MAC addr\n",
2206                                 vf->default_lan_addr.addr, vf->vf_id);
2207                         ret = I40E_ERR_PARAM;
2208                         goto error_param;
2209                 }
2210         }
2211         vsi = pf->vsi[vf->lan_vsi_idx];
2212
2213         spin_lock_bh(&vsi->mac_filter_hash_lock);
2214         /* delete addresses from the list */
2215         for (i = 0; i < al->num_elements; i++)
2216                 if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2217                         ret = I40E_ERR_INVALID_MAC_ADDR;
2218                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2219                         goto error_param;
2220                 } else {
2221                         vf->num_mac--;
2222                 }
2223
2224         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2225
2226         /* program the updated filter list */
2227         ret = i40e_sync_vsi_filters(vsi);
2228         if (ret)
2229                 dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2230                         vf->vf_id, ret);
2231
2232 error_param:
2233         /* send the response to the VF */
2234         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
2235                                        ret);
2236 }
2237
2238 /**
2239  * i40e_vc_add_vlan_msg
2240  * @vf: pointer to the VF info
2241  * @msg: pointer to the msg buffer
2242  * @msglen: msg length
2243  *
2244  * program guest vlan id
2245  **/
2246 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2247 {
2248         struct virtchnl_vlan_filter_list *vfl =
2249             (struct virtchnl_vlan_filter_list *)msg;
2250         struct i40e_pf *pf = vf->pf;
2251         struct i40e_vsi *vsi = NULL;
2252         u16 vsi_id = vfl->vsi_id;
2253         i40e_status aq_ret = 0;
2254         int i;
2255
2256         if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
2257             !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2258                 dev_err(&pf->pdev->dev,
2259                         "VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
2260                 goto error_param;
2261         }
2262         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2263             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2264                 aq_ret = I40E_ERR_PARAM;
2265                 goto error_param;
2266         }
2267
2268         for (i = 0; i < vfl->num_elements; i++) {
2269                 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2270                         aq_ret = I40E_ERR_PARAM;
2271                         dev_err(&pf->pdev->dev,
2272                                 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2273                         goto error_param;
2274                 }
2275         }
2276         vsi = pf->vsi[vf->lan_vsi_idx];
2277         if (vsi->info.pvid) {
2278                 aq_ret = I40E_ERR_PARAM;
2279                 goto error_param;
2280         }
2281
2282         i40e_vlan_stripping_enable(vsi);
2283         for (i = 0; i < vfl->num_elements; i++) {
2284                 /* add new VLAN filter */
2285                 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
2286                 if (!ret)
2287                         vf->num_vlan++;
2288
2289                 if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2290                         i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2291                                                            true,
2292                                                            vfl->vlan_id[i],
2293                                                            NULL);
2294                 if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2295                         i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2296                                                            true,
2297                                                            vfl->vlan_id[i],
2298                                                            NULL);
2299
2300                 if (ret)
2301                         dev_err(&pf->pdev->dev,
2302                                 "Unable to add VLAN filter %d for VF %d, error %d\n",
2303                                 vfl->vlan_id[i], vf->vf_id, ret);
2304         }
2305
2306 error_param:
2307         /* send the response to the VF */
2308         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
2309 }
2310
2311 /**
2312  * i40e_vc_remove_vlan_msg
2313  * @vf: pointer to the VF info
2314  * @msg: pointer to the msg buffer
2315  * @msglen: msg length
2316  *
2317  * remove programmed guest vlan id
2318  **/
2319 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2320 {
2321         struct virtchnl_vlan_filter_list *vfl =
2322             (struct virtchnl_vlan_filter_list *)msg;
2323         struct i40e_pf *pf = vf->pf;
2324         struct i40e_vsi *vsi = NULL;
2325         u16 vsi_id = vfl->vsi_id;
2326         i40e_status aq_ret = 0;
2327         int i;
2328
2329         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2330             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2331                 aq_ret = I40E_ERR_PARAM;
2332                 goto error_param;
2333         }
2334
2335         for (i = 0; i < vfl->num_elements; i++) {
2336                 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2337                         aq_ret = I40E_ERR_PARAM;
2338                         goto error_param;
2339                 }
2340         }
2341
2342         vsi = pf->vsi[vf->lan_vsi_idx];
2343         if (vsi->info.pvid) {
2344                 aq_ret = I40E_ERR_PARAM;
2345                 goto error_param;
2346         }
2347
2348         for (i = 0; i < vfl->num_elements; i++) {
2349                 i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
2350                 vf->num_vlan--;
2351
2352                 if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2353                         i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2354                                                            false,
2355                                                            vfl->vlan_id[i],
2356                                                            NULL);
2357                 if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2358                         i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2359                                                            false,
2360                                                            vfl->vlan_id[i],
2361                                                            NULL);
2362         }
2363
2364 error_param:
2365         /* send the response to the VF */
2366         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
2367 }
2368
2369 /**
2370  * i40e_vc_iwarp_msg
2371  * @vf: pointer to the VF info
2372  * @msg: pointer to the msg buffer
2373  * @msglen: msg length
2374  *
2375  * called from the VF for the iwarp msgs
2376  **/
2377 static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2378 {
2379         struct i40e_pf *pf = vf->pf;
2380         int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
2381         i40e_status aq_ret = 0;
2382
2383         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2384             !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2385                 aq_ret = I40E_ERR_PARAM;
2386                 goto error_param;
2387         }
2388
2389         i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
2390                                      msg, msglen);
2391
2392 error_param:
2393         /* send the response to the VF */
2394         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
2395                                        aq_ret);
2396 }
2397
2398 /**
2399  * i40e_vc_iwarp_qvmap_msg
2400  * @vf: pointer to the VF info
2401  * @msg: pointer to the msg buffer
2402  * @msglen: msg length
2403  * @config: config qvmap or release it
2404  *
2405  * called from the VF for the iwarp msgs
2406  **/
2407 static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
2408                                    bool config)
2409 {
2410         struct virtchnl_iwarp_qvlist_info *qvlist_info =
2411                                 (struct virtchnl_iwarp_qvlist_info *)msg;
2412         i40e_status aq_ret = 0;
2413
2414         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2415             !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2416                 aq_ret = I40E_ERR_PARAM;
2417                 goto error_param;
2418         }
2419
2420         if (config) {
2421                 if (i40e_config_iwarp_qvlist(vf, qvlist_info))
2422                         aq_ret = I40E_ERR_PARAM;
2423         } else {
2424                 i40e_release_iwarp_qvlist(vf);
2425         }
2426
2427 error_param:
2428         /* send the response to the VF */
2429         return i40e_vc_send_resp_to_vf(vf,
2430                                config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
2431                                VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
2432                                aq_ret);
2433 }
2434
2435 /**
2436  * i40e_vc_config_rss_key
2437  * @vf: pointer to the VF info
2438  * @msg: pointer to the msg buffer
2439  * @msglen: msg length
2440  *
2441  * Configure the VF's RSS key
2442  **/
2443 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
2444 {
2445         struct virtchnl_rss_key *vrk =
2446                 (struct virtchnl_rss_key *)msg;
2447         struct i40e_pf *pf = vf->pf;
2448         struct i40e_vsi *vsi = NULL;
2449         u16 vsi_id = vrk->vsi_id;
2450         i40e_status aq_ret = 0;
2451
2452         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2453             !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2454             (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
2455                 aq_ret = I40E_ERR_PARAM;
2456                 goto err;
2457         }
2458
2459         vsi = pf->vsi[vf->lan_vsi_idx];
2460         aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
2461 err:
2462         /* send the response to the VF */
2463         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
2464                                        aq_ret);
2465 }
2466
2467 /**
2468  * i40e_vc_config_rss_lut
2469  * @vf: pointer to the VF info
2470  * @msg: pointer to the msg buffer
2471  * @msglen: msg length
2472  *
2473  * Configure the VF's RSS LUT
2474  **/
2475 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
2476 {
2477         struct virtchnl_rss_lut *vrl =
2478                 (struct virtchnl_rss_lut *)msg;
2479         struct i40e_pf *pf = vf->pf;
2480         struct i40e_vsi *vsi = NULL;
2481         u16 vsi_id = vrl->vsi_id;
2482         i40e_status aq_ret = 0;
2483
2484         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2485             !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2486             (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
2487                 aq_ret = I40E_ERR_PARAM;
2488                 goto err;
2489         }
2490
2491         vsi = pf->vsi[vf->lan_vsi_idx];
2492         aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
2493         /* send the response to the VF */
2494 err:
2495         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
2496                                        aq_ret);
2497 }
2498
2499 /**
2500  * i40e_vc_get_rss_hena
2501  * @vf: pointer to the VF info
2502  * @msg: pointer to the msg buffer
2503  * @msglen: msg length
2504  *
2505  * Return the RSS HENA bits allowed by the hardware
2506  **/
2507 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2508 {
2509         struct virtchnl_rss_hena *vrh = NULL;
2510         struct i40e_pf *pf = vf->pf;
2511         i40e_status aq_ret = 0;
2512         int len = 0;
2513
2514         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2515                 aq_ret = I40E_ERR_PARAM;
2516                 goto err;
2517         }
2518         len = sizeof(struct virtchnl_rss_hena);
2519
2520         vrh = kzalloc(len, GFP_KERNEL);
2521         if (!vrh) {
2522                 aq_ret = I40E_ERR_NO_MEMORY;
2523                 len = 0;
2524                 goto err;
2525         }
2526         vrh->hena = i40e_pf_get_default_rss_hena(pf);
2527 err:
2528         /* send the response back to the VF */
2529         aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
2530                                         aq_ret, (u8 *)vrh, len);
2531         kfree(vrh);
2532         return aq_ret;
2533 }
2534
2535 /**
2536  * i40e_vc_set_rss_hena
2537  * @vf: pointer to the VF info
2538  * @msg: pointer to the msg buffer
2539  * @msglen: msg length
2540  *
2541  * Set the RSS HENA bits for the VF
2542  **/
2543 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2544 {
2545         struct virtchnl_rss_hena *vrh =
2546                 (struct virtchnl_rss_hena *)msg;
2547         struct i40e_pf *pf = vf->pf;
2548         struct i40e_hw *hw = &pf->hw;
2549         i40e_status aq_ret = 0;
2550
2551         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2552                 aq_ret = I40E_ERR_PARAM;
2553                 goto err;
2554         }
2555         i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
2556         i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
2557                           (u32)(vrh->hena >> 32));
2558
2559         /* send the response to the VF */
2560 err:
2561         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
2562 }
2563
2564 /**
2565  * i40e_vc_enable_vlan_stripping
2566  * @vf: pointer to the VF info
2567  * @msg: pointer to the msg buffer
2568  * @msglen: msg length
2569  *
2570  * Enable vlan header stripping for the VF
2571  **/
2572 static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
2573                                          u16 msglen)
2574 {
2575         struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
2576         i40e_status aq_ret = 0;
2577
2578         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2579                 aq_ret = I40E_ERR_PARAM;
2580                 goto err;
2581         }
2582
2583         i40e_vlan_stripping_enable(vsi);
2584
2585         /* send the response to the VF */
2586 err:
2587         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2588                                        aq_ret);
2589 }
2590
2591 /**
2592  * i40e_vc_disable_vlan_stripping
2593  * @vf: pointer to the VF info
2594  * @msg: pointer to the msg buffer
2595  * @msglen: msg length
2596  *
2597  * Disable vlan header stripping for the VF
2598  **/
2599 static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
2600                                           u16 msglen)
2601 {
2602         struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
2603         i40e_status aq_ret = 0;
2604
2605         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2606                 aq_ret = I40E_ERR_PARAM;
2607                 goto err;
2608         }
2609
2610         i40e_vlan_stripping_disable(vsi);
2611
2612         /* send the response to the VF */
2613 err:
2614         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2615                                        aq_ret);
2616 }
2617
2618 /**
2619  * i40e_vc_process_vf_msg
2620  * @pf: pointer to the PF structure
2621  * @vf_id: source VF id
2622  * @msg: pointer to the msg buffer
2623  * @msglen: msg length
2624  * @msghndl: msg handle
2625  *
2626  * called from the common aeq/arq handler to
2627  * process request from VF
2628  **/
2629 int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
2630                            u32 v_retval, u8 *msg, u16 msglen)
2631 {
2632         struct i40e_hw *hw = &pf->hw;
2633         int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
2634         struct i40e_vf *vf;
2635         int ret;
2636
2637         pf->vf_aq_requests++;
2638         if (local_vf_id >= pf->num_alloc_vfs)
2639                 return -EINVAL;
2640         vf = &(pf->vf[local_vf_id]);
2641
2642         /* Check if VF is disabled. */
2643         if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
2644                 return I40E_ERR_PARAM;
2645
2646         /* perform basic checks on the msg */
2647         ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
2648
2649         /* perform additional checks specific to this driver */
2650         if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
2651                 struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
2652
2653                 if (vrk->key_len != I40E_HKEY_ARRAY_SIZE)
2654                         ret = -EINVAL;
2655         } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
2656                 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2657
2658                 if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)
2659                         ret = -EINVAL;
2660         }
2661
2662         if (ret) {
2663                 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
2664                 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
2665                         local_vf_id, v_opcode, msglen);
2666                 switch (ret) {
2667                 case VIRTCHNL_ERR_PARAM:
2668                         return -EPERM;
2669                 default:
2670                         return -EINVAL;
2671                 }
2672         }
2673
2674         switch (v_opcode) {
2675         case VIRTCHNL_OP_VERSION:
2676                 ret = i40e_vc_get_version_msg(vf, msg);
2677                 break;
2678         case VIRTCHNL_OP_GET_VF_RESOURCES:
2679                 ret = i40e_vc_get_vf_resources_msg(vf, msg);
2680                 break;
2681         case VIRTCHNL_OP_RESET_VF:
2682                 i40e_vc_reset_vf_msg(vf);
2683                 ret = 0;
2684                 break;
2685         case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
2686                 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
2687                 break;
2688         case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2689                 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
2690                 break;
2691         case VIRTCHNL_OP_CONFIG_IRQ_MAP:
2692                 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
2693                 break;
2694         case VIRTCHNL_OP_ENABLE_QUEUES:
2695                 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
2696                 i40e_vc_notify_vf_link_state(vf);
2697                 break;
2698         case VIRTCHNL_OP_DISABLE_QUEUES:
2699                 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
2700                 break;
2701         case VIRTCHNL_OP_ADD_ETH_ADDR:
2702                 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
2703                 break;
2704         case VIRTCHNL_OP_DEL_ETH_ADDR:
2705                 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
2706                 break;
2707         case VIRTCHNL_OP_ADD_VLAN:
2708                 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
2709                 break;
2710         case VIRTCHNL_OP_DEL_VLAN:
2711                 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
2712                 break;
2713         case VIRTCHNL_OP_GET_STATS:
2714                 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
2715                 break;
2716         case VIRTCHNL_OP_IWARP:
2717                 ret = i40e_vc_iwarp_msg(vf, msg, msglen);
2718                 break;
2719         case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
2720                 ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
2721                 break;
2722         case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
2723                 ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
2724                 break;
2725         case VIRTCHNL_OP_CONFIG_RSS_KEY:
2726                 ret = i40e_vc_config_rss_key(vf, msg, msglen);
2727                 break;
2728         case VIRTCHNL_OP_CONFIG_RSS_LUT:
2729                 ret = i40e_vc_config_rss_lut(vf, msg, msglen);
2730                 break;
2731         case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
2732                 ret = i40e_vc_get_rss_hena(vf, msg, msglen);
2733                 break;
2734         case VIRTCHNL_OP_SET_RSS_HENA:
2735                 ret = i40e_vc_set_rss_hena(vf, msg, msglen);
2736                 break;
2737         case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
2738                 ret = i40e_vc_enable_vlan_stripping(vf, msg, msglen);
2739                 break;
2740         case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
2741                 ret = i40e_vc_disable_vlan_stripping(vf, msg, msglen);
2742                 break;
2743
2744         case VIRTCHNL_OP_UNKNOWN:
2745         default:
2746                 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
2747                         v_opcode, local_vf_id);
2748                 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
2749                                               I40E_ERR_NOT_IMPLEMENTED);
2750                 break;
2751         }
2752
2753         return ret;
2754 }
2755
2756 /**
2757  * i40e_vc_process_vflr_event
2758  * @pf: pointer to the PF structure
2759  *
2760  * called from the vlfr irq handler to
2761  * free up VF resources and state variables
2762  **/
2763 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
2764 {
2765         struct i40e_hw *hw = &pf->hw;
2766         u32 reg, reg_idx, bit_idx;
2767         struct i40e_vf *vf;
2768         int vf_id;
2769
2770         if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
2771                 return 0;
2772
2773         /* Re-enable the VFLR interrupt cause here, before looking for which
2774          * VF got reset. Otherwise, if another VF gets a reset while the
2775          * first one is being processed, that interrupt will be lost, and
2776          * that VF will be stuck in reset forever.
2777          */
2778         reg = rd32(hw, I40E_PFINT_ICR0_ENA);
2779         reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
2780         wr32(hw, I40E_PFINT_ICR0_ENA, reg);
2781         i40e_flush(hw);
2782
2783         clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
2784         for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
2785                 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2786                 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
2787                 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
2788                 vf = &pf->vf[vf_id];
2789                 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
2790                 if (reg & BIT(bit_idx))
2791                         /* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
2792                         i40e_reset_vf(vf, true);
2793         }
2794
2795         return 0;
2796 }
2797
2798 /**
2799  * i40e_ndo_set_vf_mac
2800  * @netdev: network interface device structure
2801  * @vf_id: VF identifier
2802  * @mac: mac address
2803  *
2804  * program VF mac address
2805  **/
2806 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2807 {
2808         struct i40e_netdev_priv *np = netdev_priv(netdev);
2809         struct i40e_vsi *vsi = np->vsi;
2810         struct i40e_pf *pf = vsi->back;
2811         struct i40e_mac_filter *f;
2812         struct i40e_vf *vf;
2813         int ret = 0;
2814         struct hlist_node *h;
2815         int bkt;
2816         u8 i;
2817
2818         /* validate the request */
2819         if (vf_id >= pf->num_alloc_vfs) {
2820                 dev_err(&pf->pdev->dev,
2821                         "Invalid VF Identifier %d\n", vf_id);
2822                 ret = -EINVAL;
2823                 goto error_param;
2824         }
2825
2826         vf = &(pf->vf[vf_id]);
2827         vsi = pf->vsi[vf->lan_vsi_idx];
2828
2829         /* When the VF is resetting wait until it is done.
2830          * It can take up to 200 milliseconds,
2831          * but wait for up to 300 milliseconds to be safe.
2832          */
2833         for (i = 0; i < 15; i++) {
2834                 if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states))
2835                         break;
2836                 msleep(20);
2837         }
2838         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
2839                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2840                         vf_id);
2841                 ret = -EAGAIN;
2842                 goto error_param;
2843         }
2844
2845         if (is_multicast_ether_addr(mac)) {
2846                 dev_err(&pf->pdev->dev,
2847                         "Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
2848                 ret = -EINVAL;
2849                 goto error_param;
2850         }
2851
2852         /* Lock once because below invoked function add/del_filter requires
2853          * mac_filter_hash_lock to be held
2854          */
2855         spin_lock_bh(&vsi->mac_filter_hash_lock);
2856
2857         /* delete the temporary mac address */
2858         if (!is_zero_ether_addr(vf->default_lan_addr.addr))
2859                 i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
2860
2861         /* Delete all the filters for this VSI - we're going to kill it
2862          * anyway.
2863          */
2864         hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
2865                 __i40e_del_filter(vsi, f);
2866
2867         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2868
2869         /* program mac filter */
2870         if (i40e_sync_vsi_filters(vsi)) {
2871                 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2872                 ret = -EIO;
2873                 goto error_param;
2874         }
2875         ether_addr_copy(vf->default_lan_addr.addr, mac);
2876
2877         if (is_zero_ether_addr(mac)) {
2878                 vf->pf_set_mac = false;
2879                 dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
2880         } else {
2881                 vf->pf_set_mac = true;
2882                 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
2883                          mac, vf_id);
2884         }
2885
2886         /* Force the VF driver stop so it has to reload with new MAC address */
2887         i40e_vc_disable_vf(pf, vf);
2888         dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
2889
2890 error_param:
2891         return ret;
2892 }
2893
2894 /**
2895  * i40e_ndo_set_vf_port_vlan
2896  * @netdev: network interface device structure
2897  * @vf_id: VF identifier
2898  * @vlan_id: mac address
2899  * @qos: priority setting
2900  * @vlan_proto: vlan protocol
2901  *
2902  * program VF vlan id and/or qos
2903  **/
2904 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
2905                               u16 vlan_id, u8 qos, __be16 vlan_proto)
2906 {
2907         u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
2908         struct i40e_netdev_priv *np = netdev_priv(netdev);
2909         struct i40e_pf *pf = np->vsi->back;
2910         struct i40e_vsi *vsi;
2911         struct i40e_vf *vf;
2912         int ret = 0;
2913
2914         /* validate the request */
2915         if (vf_id >= pf->num_alloc_vfs) {
2916                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2917                 ret = -EINVAL;
2918                 goto error_pvid;
2919         }
2920
2921         if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2922                 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2923                 ret = -EINVAL;
2924                 goto error_pvid;
2925         }
2926
2927         if (vlan_proto != htons(ETH_P_8021Q)) {
2928                 dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
2929                 ret = -EPROTONOSUPPORT;
2930                 goto error_pvid;
2931         }
2932
2933         vf = &(pf->vf[vf_id]);
2934         vsi = pf->vsi[vf->lan_vsi_idx];
2935         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
2936                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2937                         vf_id);
2938                 ret = -EAGAIN;
2939                 goto error_pvid;
2940         }
2941
2942         if (le16_to_cpu(vsi->info.pvid) == vlanprio)
2943                 /* duplicate request, so just return success */
2944                 goto error_pvid;
2945
2946         /* Locked once because multiple functions below iterate list */
2947         spin_lock_bh(&vsi->mac_filter_hash_lock);
2948
2949         if (le16_to_cpu(vsi->info.pvid) == 0 && i40e_is_vsi_in_vlan(vsi)) {
2950                 dev_err(&pf->pdev->dev,
2951                         "VF %d has already configured VLAN filters and the administrator is requesting a port VLAN override.\nPlease unload and reload the VF driver for this change to take effect.\n",
2952                         vf_id);
2953                 /* Administrator Error - knock the VF offline until he does
2954                  * the right thing by reconfiguring his network correctly
2955                  * and then reloading the VF driver.
2956                  */
2957                 i40e_vc_disable_vf(pf, vf);
2958                 /* During reset the VF got a new VSI, so refresh the pointer. */
2959                 vsi = pf->vsi[vf->lan_vsi_idx];
2960         }
2961
2962         /* Check for condition where there was already a port VLAN ID
2963          * filter set and now it is being deleted by setting it to zero.
2964          * Additionally check for the condition where there was a port
2965          * VLAN but now there is a new and different port VLAN being set.
2966          * Before deleting all the old VLAN filters we must add new ones
2967          * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2968          * MAC addresses deleted.
2969          */
2970         if ((!(vlan_id || qos) ||
2971             vlanprio != le16_to_cpu(vsi->info.pvid)) &&
2972             vsi->info.pvid) {
2973                 ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
2974                 if (ret) {
2975                         dev_info(&vsi->back->pdev->dev,
2976                                  "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2977                                  vsi->back->hw.aq.asq_last_status);
2978                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2979                         goto error_pvid;
2980                 }
2981         }
2982
2983         if (vsi->info.pvid) {
2984                 /* remove all filters on the old VLAN */
2985                 i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
2986                                            VLAN_VID_MASK));
2987         }
2988
2989         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2990         if (vlan_id || qos)
2991                 ret = i40e_vsi_add_pvid(vsi, vlanprio);
2992         else
2993                 i40e_vsi_remove_pvid(vsi);
2994         spin_lock_bh(&vsi->mac_filter_hash_lock);
2995
2996         if (vlan_id) {
2997                 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2998                          vlan_id, qos, vf_id);
2999
3000                 /* add new VLAN filter for each MAC */
3001                 ret = i40e_add_vlan_all_mac(vsi, vlan_id);
3002                 if (ret) {
3003                         dev_info(&vsi->back->pdev->dev,
3004                                  "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
3005                                  vsi->back->hw.aq.asq_last_status);
3006                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
3007                         goto error_pvid;
3008                 }
3009
3010                 /* remove the previously added non-VLAN MAC filters */
3011                 i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
3012         }
3013
3014         spin_unlock_bh(&vsi->mac_filter_hash_lock);
3015
3016         /* Schedule the worker thread to take care of applying changes */
3017         i40e_service_event_schedule(vsi->back);
3018
3019         if (ret) {
3020                 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
3021                 goto error_pvid;
3022         }
3023
3024         /* The Port VLAN needs to be saved across resets the same as the
3025          * default LAN MAC address.
3026          */
3027         vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
3028         ret = 0;
3029
3030 error_pvid:
3031         return ret;
3032 }
3033
3034 #define I40E_BW_CREDIT_DIVISOR 50     /* 50Mbps per BW credit */
3035 #define I40E_MAX_BW_INACTIVE_ACCUM 4  /* device can accumulate 4 credits max */
3036 /**
3037  * i40e_ndo_set_vf_bw
3038  * @netdev: network interface device structure
3039  * @vf_id: VF identifier
3040  * @tx_rate: Tx rate
3041  *
3042  * configure VF Tx rate
3043  **/
3044 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
3045                        int max_tx_rate)
3046 {
3047         struct i40e_netdev_priv *np = netdev_priv(netdev);
3048         struct i40e_pf *pf = np->vsi->back;
3049         struct i40e_vsi *vsi;
3050         struct i40e_vf *vf;
3051         int speed = 0;
3052         int ret = 0;
3053
3054         /* validate the request */
3055         if (vf_id >= pf->num_alloc_vfs) {
3056                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
3057                 ret = -EINVAL;
3058                 goto error;
3059         }
3060
3061         if (min_tx_rate) {
3062                 dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
3063                         min_tx_rate, vf_id);
3064                 return -EINVAL;
3065         }
3066
3067         vf = &(pf->vf[vf_id]);
3068         vsi = pf->vsi[vf->lan_vsi_idx];
3069         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3070                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3071                         vf_id);
3072                 ret = -EAGAIN;
3073                 goto error;
3074         }
3075
3076         switch (pf->hw.phy.link_info.link_speed) {
3077         case I40E_LINK_SPEED_40GB:
3078                 speed = 40000;
3079                 break;
3080         case I40E_LINK_SPEED_25GB:
3081                 speed = 25000;
3082                 break;
3083         case I40E_LINK_SPEED_20GB:
3084                 speed = 20000;
3085                 break;
3086         case I40E_LINK_SPEED_10GB:
3087                 speed = 10000;
3088                 break;
3089         case I40E_LINK_SPEED_1GB:
3090                 speed = 1000;
3091                 break;
3092         default:
3093                 break;
3094         }
3095
3096         if (max_tx_rate > speed) {
3097                 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.\n",
3098                         max_tx_rate, vf->vf_id);
3099                 ret = -EINVAL;
3100                 goto error;
3101         }
3102
3103         if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
3104                 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
3105                 max_tx_rate = 50;
3106         }
3107
3108         /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
3109         ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
3110                                           max_tx_rate / I40E_BW_CREDIT_DIVISOR,
3111                                           I40E_MAX_BW_INACTIVE_ACCUM, NULL);
3112         if (ret) {
3113                 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
3114                         ret);
3115                 ret = -EIO;
3116                 goto error;
3117         }
3118         vf->tx_rate = max_tx_rate;
3119 error:
3120         return ret;
3121 }
3122
3123 /**
3124  * i40e_ndo_get_vf_config
3125  * @netdev: network interface device structure
3126  * @vf_id: VF identifier
3127  * @ivi: VF configuration structure
3128  *
3129  * return VF configuration
3130  **/
3131 int i40e_ndo_get_vf_config(struct net_device *netdev,
3132                            int vf_id, struct ifla_vf_info *ivi)
3133 {
3134         struct i40e_netdev_priv *np = netdev_priv(netdev);
3135         struct i40e_vsi *vsi = np->vsi;
3136         struct i40e_pf *pf = vsi->back;
3137         struct i40e_vf *vf;
3138         int ret = 0;
3139
3140         /* validate the request */
3141         if (vf_id >= pf->num_alloc_vfs) {
3142                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3143                 ret = -EINVAL;
3144                 goto error_param;
3145         }
3146
3147         vf = &(pf->vf[vf_id]);
3148         /* first vsi is always the LAN vsi */
3149         vsi = pf->vsi[vf->lan_vsi_idx];
3150         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3151                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3152                         vf_id);
3153                 ret = -EAGAIN;
3154                 goto error_param;
3155         }
3156
3157         ivi->vf = vf_id;
3158
3159         ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
3160
3161         ivi->max_tx_rate = vf->tx_rate;
3162         ivi->min_tx_rate = 0;
3163         ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
3164         ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
3165                    I40E_VLAN_PRIORITY_SHIFT;
3166         if (vf->link_forced == false)
3167                 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
3168         else if (vf->link_up == true)
3169                 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
3170         else
3171                 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
3172         ivi->spoofchk = vf->spoofchk;
3173         ivi->trusted = vf->trusted;
3174         ret = 0;
3175
3176 error_param:
3177         return ret;
3178 }
3179
3180 /**
3181  * i40e_ndo_set_vf_link_state
3182  * @netdev: network interface device structure
3183  * @vf_id: VF identifier
3184  * @link: required link state
3185  *
3186  * Set the link state of a specified VF, regardless of physical link state
3187  **/
3188 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
3189 {
3190         struct i40e_netdev_priv *np = netdev_priv(netdev);
3191         struct i40e_pf *pf = np->vsi->back;
3192         struct virtchnl_pf_event pfe;
3193         struct i40e_hw *hw = &pf->hw;
3194         struct i40e_vf *vf;
3195         int abs_vf_id;
3196         int ret = 0;
3197
3198         /* validate the request */
3199         if (vf_id >= pf->num_alloc_vfs) {
3200                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3201                 ret = -EINVAL;
3202                 goto error_out;
3203         }
3204
3205         vf = &pf->vf[vf_id];
3206         abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
3207
3208         pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
3209         pfe.severity = PF_EVENT_SEVERITY_INFO;
3210
3211         switch (link) {
3212         case IFLA_VF_LINK_STATE_AUTO:
3213                 vf->link_forced = false;
3214                 pfe.event_data.link_event.link_status =
3215                         pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
3216                 pfe.event_data.link_event.link_speed =
3217                         (enum virtchnl_link_speed)
3218                         pf->hw.phy.link_info.link_speed;
3219                 break;
3220         case IFLA_VF_LINK_STATE_ENABLE:
3221                 vf->link_forced = true;
3222                 vf->link_up = true;
3223                 pfe.event_data.link_event.link_status = true;
3224                 pfe.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
3225                 break;
3226         case IFLA_VF_LINK_STATE_DISABLE:
3227                 vf->link_forced = true;
3228                 vf->link_up = false;
3229                 pfe.event_data.link_event.link_status = false;
3230                 pfe.event_data.link_event.link_speed = 0;
3231                 break;
3232         default:
3233                 ret = -EINVAL;
3234                 goto error_out;
3235         }
3236         /* Notify the VF of its new link state */
3237         i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
3238                                0, (u8 *)&pfe, sizeof(pfe), NULL);
3239
3240 error_out:
3241         return ret;
3242 }
3243
3244 /**
3245  * i40e_ndo_set_vf_spoofchk
3246  * @netdev: network interface device structure
3247  * @vf_id: VF identifier
3248  * @enable: flag to enable or disable feature
3249  *
3250  * Enable or disable VF spoof checking
3251  **/
3252 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
3253 {
3254         struct i40e_netdev_priv *np = netdev_priv(netdev);
3255         struct i40e_vsi *vsi = np->vsi;
3256         struct i40e_pf *pf = vsi->back;
3257         struct i40e_vsi_context ctxt;
3258         struct i40e_hw *hw = &pf->hw;
3259         struct i40e_vf *vf;
3260         int ret = 0;
3261
3262         /* validate the request */
3263         if (vf_id >= pf->num_alloc_vfs) {
3264                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3265                 ret = -EINVAL;
3266                 goto out;
3267         }
3268
3269         vf = &(pf->vf[vf_id]);
3270         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3271                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3272                         vf_id);
3273                 ret = -EAGAIN;
3274                 goto out;
3275         }
3276
3277         if (enable == vf->spoofchk)
3278                 goto out;
3279
3280         vf->spoofchk = enable;
3281         memset(&ctxt, 0, sizeof(ctxt));
3282         ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
3283         ctxt.pf_num = pf->hw.pf_id;
3284         ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
3285         if (enable)
3286                 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
3287                                         I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
3288         ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
3289         if (ret) {
3290                 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
3291                         ret);
3292                 ret = -EIO;
3293         }
3294 out:
3295         return ret;
3296 }
3297
3298 /**
3299  * i40e_ndo_set_vf_trust
3300  * @netdev: network interface device structure of the pf
3301  * @vf_id: VF identifier
3302  * @setting: trust setting
3303  *
3304  * Enable or disable VF trust setting
3305  **/
3306 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
3307 {
3308         struct i40e_netdev_priv *np = netdev_priv(netdev);
3309         struct i40e_pf *pf = np->vsi->back;
3310         struct i40e_vf *vf;
3311         int ret = 0;
3312
3313         /* validate the request */
3314         if (vf_id >= pf->num_alloc_vfs) {
3315                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3316                 return -EINVAL;
3317         }
3318
3319         if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3320                 dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
3321                 return -EINVAL;
3322         }
3323
3324         vf = &pf->vf[vf_id];
3325
3326         if (!vf)
3327                 return -EINVAL;
3328         if (setting == vf->trusted)
3329                 goto out;
3330
3331         vf->trusted = setting;
3332         i40e_vc_notify_vf_reset(vf);
3333         i40e_reset_vf(vf, false);
3334         dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
3335                  vf_id, setting ? "" : "un");
3336 out:
3337         return ret;
3338 }