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