]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[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         /* Start by disabling VF's configuration API to prevent the OS from
813          * accessing the VF's VSI after it's freed / invalidated.
814          */
815         clear_bit(I40E_VF_STAT_INIT, &vf->vf_states);
816
817         /* free vsi & disconnect it from the parent uplink */
818         if (vf->lan_vsi_idx) {
819                 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
820                 vf->lan_vsi_idx = 0;
821                 vf->lan_vsi_id = 0;
822                 vf->num_mac = 0;
823         }
824         msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
825
826         /* disable interrupts so the VF starts in a known state */
827         for (i = 0; i < msix_vf; i++) {
828                 /* format is same for both registers */
829                 if (0 == i)
830                         reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
831                 else
832                         reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
833                                                       (vf->vf_id))
834                                                      + (i - 1));
835                 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
836                 i40e_flush(hw);
837         }
838
839         /* clear the irq settings */
840         for (i = 0; i < msix_vf; i++) {
841                 /* format is same for both registers */
842                 if (0 == i)
843                         reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
844                 else
845                         reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
846                                                       (vf->vf_id))
847                                                      + (i - 1));
848                 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
849                        I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
850                 wr32(hw, reg_idx, reg);
851                 i40e_flush(hw);
852         }
853         /* reset some of the state variables keeping track of the resources */
854         vf->num_queue_pairs = 0;
855         vf->vf_states = 0;
856 }
857
858 /**
859  * i40e_alloc_vf_res
860  * @vf: pointer to the VF info
861  *
862  * allocate VF resources
863  **/
864 static int i40e_alloc_vf_res(struct i40e_vf *vf)
865 {
866         struct i40e_pf *pf = vf->pf;
867         int total_queue_pairs = 0;
868         int ret;
869
870         /* allocate hw vsi context & associated resources */
871         ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
872         if (ret)
873                 goto error_alloc;
874         total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
875
876         if (vf->trusted)
877                 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
878         else
879                 clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
880
881         /* store the total qps number for the runtime
882          * VF req validation
883          */
884         vf->num_queue_pairs = total_queue_pairs;
885
886         /* VF is now completely initialized */
887         set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
888
889 error_alloc:
890         if (ret)
891                 i40e_free_vf_res(vf);
892
893         return ret;
894 }
895
896 #define VF_DEVICE_STATUS 0xAA
897 #define VF_TRANS_PENDING_MASK 0x20
898 /**
899  * i40e_quiesce_vf_pci
900  * @vf: pointer to the VF structure
901  *
902  * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
903  * if the transactions never clear.
904  **/
905 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
906 {
907         struct i40e_pf *pf = vf->pf;
908         struct i40e_hw *hw = &pf->hw;
909         int vf_abs_id, i;
910         u32 reg;
911
912         vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
913
914         wr32(hw, I40E_PF_PCI_CIAA,
915              VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
916         for (i = 0; i < 100; i++) {
917                 reg = rd32(hw, I40E_PF_PCI_CIAD);
918                 if ((reg & VF_TRANS_PENDING_MASK) == 0)
919                         return 0;
920                 udelay(1);
921         }
922         return -EIO;
923 }
924
925 /**
926  * i40e_reset_vf
927  * @vf: pointer to the VF structure
928  * @flr: VFLR was issued or not
929  *
930  * reset the VF
931  **/
932 void i40e_reset_vf(struct i40e_vf *vf, bool flr)
933 {
934         struct i40e_pf *pf = vf->pf;
935         struct i40e_hw *hw = &pf->hw;
936         u32 reg, reg_idx, bit_idx;
937         bool rsd = false;
938         int i;
939
940         if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
941                 return;
942
943         /* warn the VF */
944         clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
945
946         /* Disable VF's configuration API during reset. The flag is re-enabled
947          * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
948          * It's normally disabled in i40e_free_vf_res(), but it's safer
949          * to do it earlier to give some time to finish to any VF config
950          * functions that may still be running at this point.
951          */
952         clear_bit(I40E_VF_STAT_INIT, &vf->vf_states);
953
954         /* In the case of a VFLR, the HW has already reset the VF and we
955          * just need to clean up, so don't hit the VFRTRIG register.
956          */
957         if (!flr) {
958                 /* reset VF using VPGEN_VFRTRIG reg */
959                 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
960                 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
961                 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
962                 i40e_flush(hw);
963         }
964         /* clear the VFLR bit in GLGEN_VFLRSTAT */
965         reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
966         bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
967         wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
968         i40e_flush(hw);
969
970         if (i40e_quiesce_vf_pci(vf))
971                 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
972                         vf->vf_id);
973
974         /* poll VPGEN_VFRSTAT reg to make sure
975          * that reset is complete
976          */
977         for (i = 0; i < 10; i++) {
978                 /* VF reset requires driver to first reset the VF and then
979                  * poll the status register to make sure that the reset
980                  * completed successfully. Due to internal HW FIFO flushes,
981                  * we must wait 10ms before the register will be valid.
982                  */
983                 usleep_range(10000, 20000);
984                 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
985                 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
986                         rsd = true;
987                         break;
988                 }
989         }
990
991         if (flr)
992                 usleep_range(10000, 20000);
993
994         if (!rsd)
995                 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
996                         vf->vf_id);
997
998         /* On initial reset, we won't have any queues */
999         if (vf->lan_vsi_idx == 0)
1000                 goto complete_reset;
1001
1002         i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1003 complete_reset:
1004         /* free VF resources to begin resetting the VSI state */
1005         i40e_free_vf_res(vf);
1006
1007         /* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
1008          * By doing this we allow HW to access VF memory at any point. If we
1009          * did it any sooner, HW could access memory while it was being freed
1010          * in i40e_free_vf_res(), causing an IOMMU fault.
1011          *
1012          * On the other hand, this needs to be done ASAP, because the VF driver
1013          * is waiting for this to happen and may report a timeout. It's
1014          * harmless, but it gets logged into Guest OS kernel log, so best avoid
1015          * it.
1016          */
1017         reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1018         reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1019         wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1020
1021         /* reallocate VF resources to finish resetting the VSI state */
1022         if (!i40e_alloc_vf_res(vf)) {
1023                 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1024                 i40e_enable_vf_mappings(vf);
1025                 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1026                 clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1027                 /* Do not notify the client during VF init */
1028                 if (vf->pf->num_alloc_vfs)
1029                         i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1030                 vf->num_vlan = 0;
1031         }
1032
1033         /* Tell the VF driver the reset is done. This needs to be done only
1034          * after VF has been fully initialized, because the VF driver may
1035          * request resources immediately after setting this flag.
1036          */
1037         wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
1038
1039         i40e_flush(hw);
1040         clear_bit(__I40E_VF_DISABLE, &pf->state);
1041 }
1042
1043 /**
1044  * i40e_free_vfs
1045  * @pf: pointer to the PF structure
1046  *
1047  * free VF resources
1048  **/
1049 void i40e_free_vfs(struct i40e_pf *pf)
1050 {
1051         struct i40e_hw *hw = &pf->hw;
1052         u32 reg_idx, bit_idx;
1053         int i, tmp, vf_id;
1054
1055         if (!pf->vf)
1056                 return;
1057         while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
1058                 usleep_range(1000, 2000);
1059
1060         i40e_notify_client_of_vf_enable(pf, 0);
1061         for (i = 0; i < pf->num_alloc_vfs; i++)
1062                 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
1063                         i40e_vsi_stop_rings(pf->vsi[pf->vf[i].lan_vsi_idx]);
1064
1065         /* Disable IOV before freeing resources. This lets any VF drivers
1066          * running in the host get themselves cleaned up before we yank
1067          * the carpet out from underneath their feet.
1068          */
1069         if (!pci_vfs_assigned(pf->pdev))
1070                 pci_disable_sriov(pf->pdev);
1071         else
1072                 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1073
1074         msleep(20); /* let any messages in transit get finished up */
1075
1076         /* free up VF resources */
1077         tmp = pf->num_alloc_vfs;
1078         pf->num_alloc_vfs = 0;
1079         for (i = 0; i < tmp; i++) {
1080                 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
1081                         i40e_free_vf_res(&pf->vf[i]);
1082                 /* disable qp mappings */
1083                 i40e_disable_vf_mappings(&pf->vf[i]);
1084         }
1085
1086         kfree(pf->vf);
1087         pf->vf = NULL;
1088
1089         /* This check is for when the driver is unloaded while VFs are
1090          * assigned. Setting the number of VFs to 0 through sysfs is caught
1091          * before this function ever gets called.
1092          */
1093         if (!pci_vfs_assigned(pf->pdev)) {
1094                 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
1095                  * work correctly when SR-IOV gets re-enabled.
1096                  */
1097                 for (vf_id = 0; vf_id < tmp; vf_id++) {
1098                         reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1099                         bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1100                         wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1101                 }
1102         }
1103         clear_bit(__I40E_VF_DISABLE, &pf->state);
1104 }
1105
1106 #ifdef CONFIG_PCI_IOV
1107 /**
1108  * i40e_alloc_vfs
1109  * @pf: pointer to the PF structure
1110  * @num_alloc_vfs: number of VFs to allocate
1111  *
1112  * allocate VF resources
1113  **/
1114 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1115 {
1116         struct i40e_vf *vfs;
1117         int i, ret = 0;
1118
1119         /* Disable interrupt 0 so we don't try to handle the VFLR. */
1120         i40e_irq_dynamic_disable_icr0(pf);
1121
1122         /* Check to see if we're just allocating resources for extant VFs */
1123         if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
1124                 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1125                 if (ret) {
1126                         pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1127                         pf->num_alloc_vfs = 0;
1128                         goto err_iov;
1129                 }
1130         }
1131         /* allocate memory */
1132         vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1133         if (!vfs) {
1134                 ret = -ENOMEM;
1135                 goto err_alloc;
1136         }
1137         pf->vf = vfs;
1138
1139         /* apply default profile */
1140         for (i = 0; i < num_alloc_vfs; i++) {
1141                 vfs[i].pf = pf;
1142                 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
1143                 vfs[i].vf_id = i;
1144
1145                 /* assign default capabilities */
1146                 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1147                 vfs[i].spoofchk = true;
1148                 /* VF resources get allocated during reset */
1149                 i40e_reset_vf(&vfs[i], false);
1150
1151         }
1152         pf->num_alloc_vfs = num_alloc_vfs;
1153
1154         i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
1155
1156 err_alloc:
1157         if (ret)
1158                 i40e_free_vfs(pf);
1159 err_iov:
1160         /* Re-enable interrupt 0. */
1161         i40e_irq_dynamic_enable_icr0(pf, false);
1162         return ret;
1163 }
1164
1165 #endif
1166 /**
1167  * i40e_pci_sriov_enable
1168  * @pdev: pointer to a pci_dev structure
1169  * @num_vfs: number of VFs to allocate
1170  *
1171  * Enable or change the number of VFs
1172  **/
1173 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1174 {
1175 #ifdef CONFIG_PCI_IOV
1176         struct i40e_pf *pf = pci_get_drvdata(pdev);
1177         int pre_existing_vfs = pci_num_vf(pdev);
1178         int err = 0;
1179
1180         if (test_bit(__I40E_TESTING, &pf->state)) {
1181                 dev_warn(&pdev->dev,
1182                          "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1183                 err = -EPERM;
1184                 goto err_out;
1185         }
1186
1187         if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1188                 i40e_free_vfs(pf);
1189         else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1190                 goto out;
1191
1192         if (num_vfs > pf->num_req_vfs) {
1193                 dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1194                          num_vfs, pf->num_req_vfs);
1195                 err = -EPERM;
1196                 goto err_out;
1197         }
1198
1199         dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1200         err = i40e_alloc_vfs(pf, num_vfs);
1201         if (err) {
1202                 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1203                 goto err_out;
1204         }
1205
1206 out:
1207         return num_vfs;
1208
1209 err_out:
1210         return err;
1211 #endif
1212         return 0;
1213 }
1214
1215 /**
1216  * i40e_pci_sriov_configure
1217  * @pdev: pointer to a pci_dev structure
1218  * @num_vfs: number of VFs to allocate
1219  *
1220  * Enable or change the number of VFs. Called when the user updates the number
1221  * of VFs in sysfs.
1222  **/
1223 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1224 {
1225         struct i40e_pf *pf = pci_get_drvdata(pdev);
1226
1227         if (num_vfs) {
1228                 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1229                         pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1230                         i40e_do_reset_safe(pf,
1231                                            BIT_ULL(__I40E_PF_RESET_REQUESTED));
1232                 }
1233                 return i40e_pci_sriov_enable(pdev, num_vfs);
1234         }
1235
1236         if (!pci_vfs_assigned(pf->pdev)) {
1237                 i40e_free_vfs(pf);
1238                 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1239                 i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
1240         } else {
1241                 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1242                 return -EINVAL;
1243         }
1244         return 0;
1245 }
1246
1247 /***********************virtual channel routines******************/
1248
1249 /**
1250  * i40e_vc_send_msg_to_vf
1251  * @vf: pointer to the VF info
1252  * @v_opcode: virtual channel opcode
1253  * @v_retval: virtual channel return value
1254  * @msg: pointer to the msg buffer
1255  * @msglen: msg length
1256  *
1257  * send msg to VF
1258  **/
1259 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1260                                   u32 v_retval, u8 *msg, u16 msglen)
1261 {
1262         struct i40e_pf *pf;
1263         struct i40e_hw *hw;
1264         int abs_vf_id;
1265         i40e_status aq_ret;
1266
1267         /* validate the request */
1268         if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1269                 return -EINVAL;
1270
1271         pf = vf->pf;
1272         hw = &pf->hw;
1273         abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1274
1275         /* single place to detect unsuccessful return values */
1276         if (v_retval) {
1277                 vf->num_invalid_msgs++;
1278                 dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1279                          vf->vf_id, v_opcode, v_retval);
1280                 if (vf->num_invalid_msgs >
1281                     I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1282                         dev_err(&pf->pdev->dev,
1283                                 "Number of invalid messages exceeded for VF %d\n",
1284                                 vf->vf_id);
1285                         dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1286                         set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1287                 }
1288         } else {
1289                 vf->num_valid_msgs++;
1290                 /* reset the invalid counter, if a valid message is received. */
1291                 vf->num_invalid_msgs = 0;
1292         }
1293
1294         aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,  v_opcode, v_retval,
1295                                         msg, msglen, NULL);
1296         if (aq_ret) {
1297                 dev_info(&pf->pdev->dev,
1298                          "Unable to send the message to VF %d aq_err %d\n",
1299                          vf->vf_id, pf->hw.aq.asq_last_status);
1300                 return -EIO;
1301         }
1302
1303         return 0;
1304 }
1305
1306 /**
1307  * i40e_vc_send_resp_to_vf
1308  * @vf: pointer to the VF info
1309  * @opcode: operation code
1310  * @retval: return value
1311  *
1312  * send resp msg to VF
1313  **/
1314 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1315                                    enum i40e_virtchnl_ops opcode,
1316                                    i40e_status retval)
1317 {
1318         return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1319 }
1320
1321 /**
1322  * i40e_vc_get_version_msg
1323  * @vf: pointer to the VF info
1324  *
1325  * called from the VF to request the API version used by the PF
1326  **/
1327 static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
1328 {
1329         struct i40e_virtchnl_version_info info = {
1330                 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1331         };
1332
1333         vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
1334         /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1335         if (VF_IS_V10(vf))
1336                 info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1337         return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1338                                       I40E_SUCCESS, (u8 *)&info,
1339                                       sizeof(struct
1340                                              i40e_virtchnl_version_info));
1341 }
1342
1343 /**
1344  * i40e_vc_get_vf_resources_msg
1345  * @vf: pointer to the VF info
1346  * @msg: pointer to the msg buffer
1347  * @msglen: msg length
1348  *
1349  * called from the VF to request its resources
1350  **/
1351 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
1352 {
1353         struct i40e_virtchnl_vf_resource *vfres = NULL;
1354         struct i40e_pf *pf = vf->pf;
1355         i40e_status aq_ret = 0;
1356         struct i40e_vsi *vsi;
1357         int num_vsis = 1;
1358         int len = 0;
1359         int ret;
1360
1361         if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1362                 aq_ret = I40E_ERR_PARAM;
1363                 goto err;
1364         }
1365
1366         len = (sizeof(struct i40e_virtchnl_vf_resource) +
1367                sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1368
1369         vfres = kzalloc(len, GFP_KERNEL);
1370         if (!vfres) {
1371                 aq_ret = I40E_ERR_NO_MEMORY;
1372                 len = 0;
1373                 goto err;
1374         }
1375         if (VF_IS_V11(vf))
1376                 vf->driver_caps = *(u32 *)msg;
1377         else
1378                 vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
1379                                   I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
1380                                   I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1381
1382         vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
1383         vsi = pf->vsi[vf->lan_vsi_idx];
1384         if (!vsi->info.pvid)
1385                 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1386
1387         if (i40e_vf_client_capable(pf, vf->vf_id) &&
1388             (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_IWARP)) {
1389                 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_IWARP;
1390                 set_bit(I40E_VF_STAT_IWARPENA, &vf->vf_states);
1391         }
1392
1393         if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1394                 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF;
1395         } else {
1396                 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
1397                     (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ))
1398                         vfres->vf_offload_flags |=
1399                                         I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1400                 else
1401                         vfres->vf_offload_flags |=
1402                                         I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
1403         }
1404
1405         if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
1406                 if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1407                         vfres->vf_offload_flags |=
1408                                 I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1409         }
1410
1411         if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_ENCAP)
1412                 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_ENCAP;
1413
1414         if ((pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE) &&
1415             (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
1416                 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1417
1418         if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
1419                 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
1420                         dev_err(&pf->pdev->dev,
1421                                 "VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
1422                                  vf->vf_id);
1423                         ret = I40E_ERR_PARAM;
1424                         goto err;
1425                 }
1426                 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1427         }
1428
1429         if (pf->flags & I40E_FLAG_WB_ON_ITR_CAPABLE) {
1430                 if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1431                         vfres->vf_offload_flags |=
1432                                         I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1433         }
1434
1435         vfres->num_vsis = num_vsis;
1436         vfres->num_queue_pairs = vf->num_queue_pairs;
1437         vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1438         vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
1439         vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
1440
1441         if (vf->lan_vsi_idx) {
1442                 vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
1443                 vfres->vsi_res[0].vsi_type = I40E_VSI_SRIOV;
1444                 vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
1445                 /* VFs only use TC 0 */
1446                 vfres->vsi_res[0].qset_handle
1447                                           = le16_to_cpu(vsi->info.qs_handle[0]);
1448                 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1449                                 vf->default_lan_addr.addr);
1450         }
1451         set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1452
1453 err:
1454         /* send the response back to the VF */
1455         ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1456                                      aq_ret, (u8 *)vfres, len);
1457
1458         kfree(vfres);
1459         return ret;
1460 }
1461
1462 /**
1463  * i40e_vc_reset_vf_msg
1464  * @vf: pointer to the VF info
1465  * @msg: pointer to the msg buffer
1466  * @msglen: msg length
1467  *
1468  * called from the VF to reset itself,
1469  * unlike other virtchnl messages, PF driver
1470  * doesn't send the response back to the VF
1471  **/
1472 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1473 {
1474         if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1475                 i40e_reset_vf(vf, false);
1476 }
1477
1478 /**
1479  * i40e_getnum_vf_vsi_vlan_filters
1480  * @vsi: pointer to the vsi
1481  *
1482  * called to get the number of VLANs offloaded on this VF
1483  **/
1484 static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
1485 {
1486         struct i40e_mac_filter *f;
1487         int num_vlans = 0, bkt;
1488
1489         hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1490                 if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
1491                         num_vlans++;
1492         }
1493
1494         return num_vlans;
1495 }
1496
1497 /**
1498  * i40e_vc_config_promiscuous_mode_msg
1499  * @vf: pointer to the VF info
1500  * @msg: pointer to the msg buffer
1501  * @msglen: msg length
1502  *
1503  * called from the VF to configure the promiscuous mode of
1504  * VF vsis
1505  **/
1506 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1507                                                u8 *msg, u16 msglen)
1508 {
1509         struct i40e_virtchnl_promisc_info *info =
1510             (struct i40e_virtchnl_promisc_info *)msg;
1511         struct i40e_pf *pf = vf->pf;
1512         struct i40e_hw *hw = &pf->hw;
1513         struct i40e_mac_filter *f;
1514         i40e_status aq_ret = 0;
1515         bool allmulti = false;
1516         struct i40e_vsi *vsi;
1517         bool alluni = false;
1518         int aq_err = 0;
1519         int bkt;
1520
1521         vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
1522         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1523             !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1524             !vsi) {
1525                 aq_ret = I40E_ERR_PARAM;
1526                 goto error_param;
1527         }
1528         if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
1529                 dev_err(&pf->pdev->dev,
1530                         "Unprivileged VF %d is attempting to configure promiscuous mode\n",
1531                         vf->vf_id);
1532                 /* Lie to the VF on purpose. */
1533                 aq_ret = 0;
1534                 goto error_param;
1535         }
1536         /* Multicast promiscuous handling*/
1537         if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1538                 allmulti = true;
1539
1540         if (vf->port_vlan_id) {
1541                 aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
1542                                                             allmulti,
1543                                                             vf->port_vlan_id,
1544                                                             NULL);
1545         } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1546                 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1547                         if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1548                                 continue;
1549                         aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
1550                                                                     vsi->seid,
1551                                                                     allmulti,
1552                                                                     f->vlan,
1553                                                                     NULL);
1554                         aq_err = pf->hw.aq.asq_last_status;
1555                         if (aq_ret) {
1556                                 dev_err(&pf->pdev->dev,
1557                                         "Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
1558                                         f->vlan,
1559                                         i40e_stat_str(&pf->hw, aq_ret),
1560                                         i40e_aq_str(&pf->hw, aq_err));
1561                                 break;
1562                         }
1563                 }
1564         } else {
1565                 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
1566                                                                allmulti, NULL);
1567                 aq_err = pf->hw.aq.asq_last_status;
1568                 if (aq_ret) {
1569                         dev_err(&pf->pdev->dev,
1570                                 "VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1571                                 vf->vf_id,
1572                                 i40e_stat_str(&pf->hw, aq_ret),
1573                                 i40e_aq_str(&pf->hw, aq_err));
1574                         goto error_param;
1575                 }
1576         }
1577
1578         if (!aq_ret) {
1579                 dev_info(&pf->pdev->dev,
1580                          "VF %d successfully set multicast promiscuous mode\n",
1581                          vf->vf_id);
1582                 if (allmulti)
1583                         set_bit(I40E_VF_STAT_MC_PROMISC, &vf->vf_states);
1584                 else
1585                         clear_bit(I40E_VF_STAT_MC_PROMISC, &vf->vf_states);
1586         }
1587
1588         if (info->flags & I40E_FLAG_VF_UNICAST_PROMISC)
1589                 alluni = true;
1590         if (vf->port_vlan_id) {
1591                 aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
1592                                                             alluni,
1593                                                             vf->port_vlan_id,
1594                                                             NULL);
1595         } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1596                 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1597                         aq_ret = 0;
1598                         if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID) {
1599                                 aq_ret =
1600                                 i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
1601                                                                    vsi->seid,
1602                                                                    alluni,
1603                                                                    f->vlan,
1604                                                                    NULL);
1605                                 aq_err = pf->hw.aq.asq_last_status;
1606                         }
1607                         if (aq_ret)
1608                                 dev_err(&pf->pdev->dev,
1609                                         "Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
1610                                         f->vlan,
1611                                         i40e_stat_str(&pf->hw, aq_ret),
1612                                         i40e_aq_str(&pf->hw, aq_err));
1613                 }
1614         } else {
1615                 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
1616                                                              allmulti, NULL,
1617                                                              true);
1618                 aq_err = pf->hw.aq.asq_last_status;
1619                 if (aq_ret) {
1620                         dev_err(&pf->pdev->dev,
1621                                 "VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
1622                                 vf->vf_id, info->flags,
1623                                 i40e_stat_str(&pf->hw, aq_ret),
1624                                 i40e_aq_str(&pf->hw, aq_err));
1625                         goto error_param;
1626                 }
1627         }
1628
1629         if (!aq_ret) {
1630                 dev_info(&pf->pdev->dev,
1631                          "VF %d successfully set unicast promiscuous mode\n",
1632                          vf->vf_id);
1633                 if (alluni)
1634                         set_bit(I40E_VF_STAT_UC_PROMISC, &vf->vf_states);
1635                 else
1636                         clear_bit(I40E_VF_STAT_UC_PROMISC, &vf->vf_states);
1637         }
1638
1639 error_param:
1640         /* send the response to the VF */
1641         return i40e_vc_send_resp_to_vf(vf,
1642                                        I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1643                                        aq_ret);
1644 }
1645
1646 /**
1647  * i40e_vc_config_queues_msg
1648  * @vf: pointer to the VF info
1649  * @msg: pointer to the msg buffer
1650  * @msglen: msg length
1651  *
1652  * called from the VF to configure the rx/tx
1653  * queues
1654  **/
1655 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1656 {
1657         struct i40e_virtchnl_vsi_queue_config_info *qci =
1658             (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1659         struct i40e_virtchnl_queue_pair_info *qpi;
1660         struct i40e_pf *pf = vf->pf;
1661         u16 vsi_id, vsi_queue_id;
1662         i40e_status aq_ret = 0;
1663         int i;
1664
1665         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1666                 aq_ret = I40E_ERR_PARAM;
1667                 goto error_param;
1668         }
1669
1670         vsi_id = qci->vsi_id;
1671         if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1672                 aq_ret = I40E_ERR_PARAM;
1673                 goto error_param;
1674         }
1675         for (i = 0; i < qci->num_queue_pairs; i++) {
1676                 qpi = &qci->qpair[i];
1677                 vsi_queue_id = qpi->txq.queue_id;
1678                 if ((qpi->txq.vsi_id != vsi_id) ||
1679                     (qpi->rxq.vsi_id != vsi_id) ||
1680                     (qpi->rxq.queue_id != vsi_queue_id) ||
1681                     !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1682                         aq_ret = I40E_ERR_PARAM;
1683                         goto error_param;
1684                 }
1685
1686                 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1687                                              &qpi->rxq) ||
1688                     i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1689                                              &qpi->txq)) {
1690                         aq_ret = I40E_ERR_PARAM;
1691                         goto error_param;
1692                 }
1693         }
1694         /* set vsi num_queue_pairs in use to num configured by VF */
1695         pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
1696
1697 error_param:
1698         /* send the response to the VF */
1699         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1700                                        aq_ret);
1701 }
1702
1703 /**
1704  * i40e_vc_config_irq_map_msg
1705  * @vf: pointer to the VF info
1706  * @msg: pointer to the msg buffer
1707  * @msglen: msg length
1708  *
1709  * called from the VF to configure the irq to
1710  * queue map
1711  **/
1712 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1713 {
1714         struct i40e_virtchnl_irq_map_info *irqmap_info =
1715             (struct i40e_virtchnl_irq_map_info *)msg;
1716         struct i40e_virtchnl_vector_map *map;
1717         u16 vsi_id, vsi_queue_id, vector_id;
1718         i40e_status aq_ret = 0;
1719         unsigned long tempmap;
1720         int i;
1721
1722         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1723                 aq_ret = I40E_ERR_PARAM;
1724                 goto error_param;
1725         }
1726
1727         for (i = 0; i < irqmap_info->num_vectors; i++) {
1728                 map = &irqmap_info->vecmap[i];
1729
1730                 vector_id = map->vector_id;
1731                 vsi_id = map->vsi_id;
1732                 /* validate msg params */
1733                 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1734                     !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1735                         aq_ret = I40E_ERR_PARAM;
1736                         goto error_param;
1737                 }
1738
1739                 /* lookout for the invalid queue index */
1740                 tempmap = map->rxq_map;
1741                 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1742                         if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1743                                                       vsi_queue_id)) {
1744                                 aq_ret = I40E_ERR_PARAM;
1745                                 goto error_param;
1746                         }
1747                 }
1748
1749                 tempmap = map->txq_map;
1750                 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1751                         if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1752                                                       vsi_queue_id)) {
1753                                 aq_ret = I40E_ERR_PARAM;
1754                                 goto error_param;
1755                         }
1756                 }
1757
1758                 i40e_config_irq_link_list(vf, vsi_id, map);
1759         }
1760 error_param:
1761         /* send the response to the VF */
1762         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1763                                        aq_ret);
1764 }
1765
1766 /**
1767  * i40e_vc_enable_queues_msg
1768  * @vf: pointer to the VF info
1769  * @msg: pointer to the msg buffer
1770  * @msglen: msg length
1771  *
1772  * called from the VF to enable all or specific queue(s)
1773  **/
1774 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1775 {
1776         struct i40e_virtchnl_queue_select *vqs =
1777             (struct i40e_virtchnl_queue_select *)msg;
1778         struct i40e_pf *pf = vf->pf;
1779         u16 vsi_id = vqs->vsi_id;
1780         i40e_status aq_ret = 0;
1781
1782         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1783                 aq_ret = I40E_ERR_PARAM;
1784                 goto error_param;
1785         }
1786
1787         if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1788                 aq_ret = I40E_ERR_PARAM;
1789                 goto error_param;
1790         }
1791
1792         if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1793                 aq_ret = I40E_ERR_PARAM;
1794                 goto error_param;
1795         }
1796
1797         if (i40e_vsi_start_rings(pf->vsi[vf->lan_vsi_idx]))
1798                 aq_ret = I40E_ERR_TIMEOUT;
1799 error_param:
1800         /* send the response to the VF */
1801         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1802                                        aq_ret);
1803 }
1804
1805 /**
1806  * i40e_vc_disable_queues_msg
1807  * @vf: pointer to the VF info
1808  * @msg: pointer to the msg buffer
1809  * @msglen: msg length
1810  *
1811  * called from the VF to disable all or specific
1812  * queue(s)
1813  **/
1814 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1815 {
1816         struct i40e_virtchnl_queue_select *vqs =
1817             (struct i40e_virtchnl_queue_select *)msg;
1818         struct i40e_pf *pf = vf->pf;
1819         i40e_status aq_ret = 0;
1820
1821         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1822                 aq_ret = I40E_ERR_PARAM;
1823                 goto error_param;
1824         }
1825
1826         if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1827                 aq_ret = I40E_ERR_PARAM;
1828                 goto error_param;
1829         }
1830
1831         if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1832                 aq_ret = I40E_ERR_PARAM;
1833                 goto error_param;
1834         }
1835
1836         i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1837
1838 error_param:
1839         /* send the response to the VF */
1840         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1841                                        aq_ret);
1842 }
1843
1844 /**
1845  * i40e_vc_get_stats_msg
1846  * @vf: pointer to the VF info
1847  * @msg: pointer to the msg buffer
1848  * @msglen: msg length
1849  *
1850  * called from the VF to get vsi stats
1851  **/
1852 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1853 {
1854         struct i40e_virtchnl_queue_select *vqs =
1855             (struct i40e_virtchnl_queue_select *)msg;
1856         struct i40e_pf *pf = vf->pf;
1857         struct i40e_eth_stats stats;
1858         i40e_status aq_ret = 0;
1859         struct i40e_vsi *vsi;
1860
1861         memset(&stats, 0, sizeof(struct i40e_eth_stats));
1862
1863         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1864                 aq_ret = I40E_ERR_PARAM;
1865                 goto error_param;
1866         }
1867
1868         if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1869                 aq_ret = I40E_ERR_PARAM;
1870                 goto error_param;
1871         }
1872
1873         vsi = pf->vsi[vf->lan_vsi_idx];
1874         if (!vsi) {
1875                 aq_ret = I40E_ERR_PARAM;
1876                 goto error_param;
1877         }
1878         i40e_update_eth_stats(vsi);
1879         stats = vsi->eth_stats;
1880
1881 error_param:
1882         /* send the response back to the VF */
1883         return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1884                                       (u8 *)&stats, sizeof(stats));
1885 }
1886
1887 /* If the VF is not trusted restrict the number of MAC/VLAN it can program */
1888 #define I40E_VC_MAX_MAC_ADDR_PER_VF 12
1889 #define I40E_VC_MAX_VLAN_PER_VF 8
1890
1891 /**
1892  * i40e_check_vf_permission
1893  * @vf: pointer to the VF info
1894  * @macaddr: pointer to the MAC Address being checked
1895  *
1896  * Check if the VF has permission to add or delete unicast MAC address
1897  * filters and return error code -EPERM if not.  Then check if the
1898  * address filter requested is broadcast or zero and if so return
1899  * an invalid MAC address error code.
1900  **/
1901 static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1902 {
1903         struct i40e_pf *pf = vf->pf;
1904         int ret = 0;
1905
1906         if (is_broadcast_ether_addr(macaddr) ||
1907                    is_zero_ether_addr(macaddr)) {
1908                 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1909                 ret = I40E_ERR_INVALID_MAC_ADDR;
1910         } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1911                    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
1912                    !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
1913                 /* If the host VMM administrator has set the VF MAC address
1914                  * administratively via the ndo_set_vf_mac command then deny
1915                  * permission to the VF to add or delete unicast MAC addresses.
1916                  * Unless the VF is privileged and then it can do whatever.
1917                  * The VF may request to set the MAC address filter already
1918                  * assigned to it so do not return an error in that case.
1919                  */
1920                 dev_err(&pf->pdev->dev,
1921                         "VF attempting to override administratively set MAC address, reload the VF driver to resume normal operation\n");
1922                 ret = -EPERM;
1923         } else if ((vf->num_mac >= I40E_VC_MAX_MAC_ADDR_PER_VF) &&
1924                    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
1925                 dev_err(&pf->pdev->dev,
1926                         "VF is not trusted, switch the VF to trusted to add more functionality\n");
1927                 ret = -EPERM;
1928         }
1929         return ret;
1930 }
1931
1932 /**
1933  * i40e_vc_add_mac_addr_msg
1934  * @vf: pointer to the VF info
1935  * @msg: pointer to the msg buffer
1936  * @msglen: msg length
1937  *
1938  * add guest mac address filter
1939  **/
1940 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1941 {
1942         struct i40e_virtchnl_ether_addr_list *al =
1943             (struct i40e_virtchnl_ether_addr_list *)msg;
1944         struct i40e_pf *pf = vf->pf;
1945         struct i40e_vsi *vsi = NULL;
1946         u16 vsi_id = al->vsi_id;
1947         i40e_status ret = 0;
1948         int i;
1949
1950         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1951             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1952                 ret = I40E_ERR_PARAM;
1953                 goto error_param;
1954         }
1955
1956         for (i = 0; i < al->num_elements; i++) {
1957                 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1958                 if (ret)
1959                         goto error_param;
1960         }
1961         vsi = pf->vsi[vf->lan_vsi_idx];
1962
1963         /* Lock once, because all function inside for loop accesses VSI's
1964          * MAC filter list which needs to be protected using same lock.
1965          */
1966         spin_lock_bh(&vsi->mac_filter_hash_lock);
1967
1968         /* add new addresses to the list */
1969         for (i = 0; i < al->num_elements; i++) {
1970                 struct i40e_mac_filter *f;
1971
1972                 f = i40e_find_mac(vsi, al->list[i].addr);
1973                 if (!f)
1974                         f = i40e_add_mac_filter(vsi, al->list[i].addr);
1975
1976                 if (!f) {
1977                         dev_err(&pf->pdev->dev,
1978                                 "Unable to add MAC filter %pM for VF %d\n",
1979                                  al->list[i].addr, vf->vf_id);
1980                         ret = I40E_ERR_PARAM;
1981                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
1982                         goto error_param;
1983                 } else {
1984                         vf->num_mac++;
1985                 }
1986         }
1987         spin_unlock_bh(&vsi->mac_filter_hash_lock);
1988
1989         /* program the updated filter list */
1990         ret = i40e_sync_vsi_filters(vsi);
1991         if (ret)
1992                 dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
1993                         vf->vf_id, ret);
1994
1995 error_param:
1996         /* send the response to the VF */
1997         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
1998                                        ret);
1999 }
2000
2001 /**
2002  * i40e_vc_del_mac_addr_msg
2003  * @vf: pointer to the VF info
2004  * @msg: pointer to the msg buffer
2005  * @msglen: msg length
2006  *
2007  * remove guest mac address filter
2008  **/
2009 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2010 {
2011         struct i40e_virtchnl_ether_addr_list *al =
2012             (struct i40e_virtchnl_ether_addr_list *)msg;
2013         struct i40e_pf *pf = vf->pf;
2014         struct i40e_vsi *vsi = NULL;
2015         u16 vsi_id = al->vsi_id;
2016         i40e_status ret = 0;
2017         int i;
2018
2019         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2020             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2021                 ret = I40E_ERR_PARAM;
2022                 goto error_param;
2023         }
2024
2025         for (i = 0; i < al->num_elements; i++) {
2026                 if (is_broadcast_ether_addr(al->list[i].addr) ||
2027                     is_zero_ether_addr(al->list[i].addr)) {
2028                         dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
2029                                 al->list[i].addr, vf->vf_id);
2030                         ret = I40E_ERR_INVALID_MAC_ADDR;
2031                         goto error_param;
2032                 }
2033         }
2034         vsi = pf->vsi[vf->lan_vsi_idx];
2035
2036         spin_lock_bh(&vsi->mac_filter_hash_lock);
2037         /* delete addresses from the list */
2038         for (i = 0; i < al->num_elements; i++)
2039                 if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2040                         ret = I40E_ERR_INVALID_MAC_ADDR;
2041                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2042                         goto error_param;
2043                 } else {
2044                         vf->num_mac--;
2045                 }
2046
2047         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2048
2049         /* program the updated filter list */
2050         ret = i40e_sync_vsi_filters(vsi);
2051         if (ret)
2052                 dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2053                         vf->vf_id, ret);
2054
2055 error_param:
2056         /* send the response to the VF */
2057         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
2058                                        ret);
2059 }
2060
2061 /**
2062  * i40e_vc_add_vlan_msg
2063  * @vf: pointer to the VF info
2064  * @msg: pointer to the msg buffer
2065  * @msglen: msg length
2066  *
2067  * program guest vlan id
2068  **/
2069 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2070 {
2071         struct i40e_virtchnl_vlan_filter_list *vfl =
2072             (struct i40e_virtchnl_vlan_filter_list *)msg;
2073         struct i40e_pf *pf = vf->pf;
2074         struct i40e_vsi *vsi = NULL;
2075         u16 vsi_id = vfl->vsi_id;
2076         i40e_status aq_ret = 0;
2077         int i;
2078
2079         if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
2080             !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2081                 dev_err(&pf->pdev->dev,
2082                         "VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
2083                 goto error_param;
2084         }
2085         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2086             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2087                 aq_ret = I40E_ERR_PARAM;
2088                 goto error_param;
2089         }
2090
2091         for (i = 0; i < vfl->num_elements; i++) {
2092                 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2093                         aq_ret = I40E_ERR_PARAM;
2094                         dev_err(&pf->pdev->dev,
2095                                 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2096                         goto error_param;
2097                 }
2098         }
2099         vsi = pf->vsi[vf->lan_vsi_idx];
2100         if (vsi->info.pvid) {
2101                 aq_ret = I40E_ERR_PARAM;
2102                 goto error_param;
2103         }
2104
2105         i40e_vlan_stripping_enable(vsi);
2106         for (i = 0; i < vfl->num_elements; i++) {
2107                 /* add new VLAN filter */
2108                 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
2109                 if (!ret)
2110                         vf->num_vlan++;
2111
2112                 if (test_bit(I40E_VF_STAT_UC_PROMISC, &vf->vf_states))
2113                         i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2114                                                            true,
2115                                                            vfl->vlan_id[i],
2116                                                            NULL);
2117                 if (test_bit(I40E_VF_STAT_MC_PROMISC, &vf->vf_states))
2118                         i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2119                                                            true,
2120                                                            vfl->vlan_id[i],
2121                                                            NULL);
2122
2123                 if (ret)
2124                         dev_err(&pf->pdev->dev,
2125                                 "Unable to add VLAN filter %d for VF %d, error %d\n",
2126                                 vfl->vlan_id[i], vf->vf_id, ret);
2127         }
2128
2129 error_param:
2130         /* send the response to the VF */
2131         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
2132 }
2133
2134 /**
2135  * i40e_vc_remove_vlan_msg
2136  * @vf: pointer to the VF info
2137  * @msg: pointer to the msg buffer
2138  * @msglen: msg length
2139  *
2140  * remove programmed guest vlan id
2141  **/
2142 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2143 {
2144         struct i40e_virtchnl_vlan_filter_list *vfl =
2145             (struct i40e_virtchnl_vlan_filter_list *)msg;
2146         struct i40e_pf *pf = vf->pf;
2147         struct i40e_vsi *vsi = NULL;
2148         u16 vsi_id = vfl->vsi_id;
2149         i40e_status aq_ret = 0;
2150         int i;
2151
2152         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2153             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2154                 aq_ret = I40E_ERR_PARAM;
2155                 goto error_param;
2156         }
2157
2158         for (i = 0; i < vfl->num_elements; i++) {
2159                 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2160                         aq_ret = I40E_ERR_PARAM;
2161                         goto error_param;
2162                 }
2163         }
2164
2165         vsi = pf->vsi[vf->lan_vsi_idx];
2166         if (vsi->info.pvid) {
2167                 aq_ret = I40E_ERR_PARAM;
2168                 goto error_param;
2169         }
2170
2171         for (i = 0; i < vfl->num_elements; i++) {
2172                 i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
2173                 vf->num_vlan--;
2174
2175                 if (test_bit(I40E_VF_STAT_UC_PROMISC, &vf->vf_states))
2176                         i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2177                                                            false,
2178                                                            vfl->vlan_id[i],
2179                                                            NULL);
2180                 if (test_bit(I40E_VF_STAT_MC_PROMISC, &vf->vf_states))
2181                         i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2182                                                            false,
2183                                                            vfl->vlan_id[i],
2184                                                            NULL);
2185         }
2186
2187 error_param:
2188         /* send the response to the VF */
2189         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
2190 }
2191
2192 /**
2193  * i40e_vc_iwarp_msg
2194  * @vf: pointer to the VF info
2195  * @msg: pointer to the msg buffer
2196  * @msglen: msg length
2197  *
2198  * called from the VF for the iwarp msgs
2199  **/
2200 static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2201 {
2202         struct i40e_pf *pf = vf->pf;
2203         int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
2204         i40e_status aq_ret = 0;
2205
2206         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2207             !test_bit(I40E_VF_STAT_IWARPENA, &vf->vf_states)) {
2208                 aq_ret = I40E_ERR_PARAM;
2209                 goto error_param;
2210         }
2211
2212         i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
2213                                      msg, msglen);
2214
2215 error_param:
2216         /* send the response to the VF */
2217         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_IWARP,
2218                                        aq_ret);
2219 }
2220
2221 /**
2222  * i40e_vc_iwarp_qvmap_msg
2223  * @vf: pointer to the VF info
2224  * @msg: pointer to the msg buffer
2225  * @msglen: msg length
2226  * @config: config qvmap or release it
2227  *
2228  * called from the VF for the iwarp msgs
2229  **/
2230 static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
2231                                    bool config)
2232 {
2233         struct i40e_virtchnl_iwarp_qvlist_info *qvlist_info =
2234                                 (struct i40e_virtchnl_iwarp_qvlist_info *)msg;
2235         i40e_status aq_ret = 0;
2236
2237         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2238             !test_bit(I40E_VF_STAT_IWARPENA, &vf->vf_states)) {
2239                 aq_ret = I40E_ERR_PARAM;
2240                 goto error_param;
2241         }
2242
2243         if (config) {
2244                 if (i40e_config_iwarp_qvlist(vf, qvlist_info))
2245                         aq_ret = I40E_ERR_PARAM;
2246         } else {
2247                 i40e_release_iwarp_qvlist(vf);
2248         }
2249
2250 error_param:
2251         /* send the response to the VF */
2252         return i40e_vc_send_resp_to_vf(vf,
2253                                config ? I40E_VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
2254                                I40E_VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
2255                                aq_ret);
2256 }
2257
2258 /**
2259  * i40e_vc_config_rss_key
2260  * @vf: pointer to the VF info
2261  * @msg: pointer to the msg buffer
2262  * @msglen: msg length
2263  *
2264  * Configure the VF's RSS key
2265  **/
2266 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
2267 {
2268         struct i40e_virtchnl_rss_key *vrk =
2269                 (struct i40e_virtchnl_rss_key *)msg;
2270         struct i40e_pf *pf = vf->pf;
2271         struct i40e_vsi *vsi = NULL;
2272         u16 vsi_id = vrk->vsi_id;
2273         i40e_status aq_ret = 0;
2274
2275         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2276             !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2277             (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
2278                 aq_ret = I40E_ERR_PARAM;
2279                 goto err;
2280         }
2281
2282         vsi = pf->vsi[vf->lan_vsi_idx];
2283         aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
2284 err:
2285         /* send the response to the VF */
2286         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_RSS_KEY,
2287                                        aq_ret);
2288 }
2289
2290 /**
2291  * i40e_vc_config_rss_lut
2292  * @vf: pointer to the VF info
2293  * @msg: pointer to the msg buffer
2294  * @msglen: msg length
2295  *
2296  * Configure the VF's RSS LUT
2297  **/
2298 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
2299 {
2300         struct i40e_virtchnl_rss_lut *vrl =
2301                 (struct i40e_virtchnl_rss_lut *)msg;
2302         struct i40e_pf *pf = vf->pf;
2303         struct i40e_vsi *vsi = NULL;
2304         u16 vsi_id = vrl->vsi_id;
2305         i40e_status aq_ret = 0;
2306
2307         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2308             !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2309             (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
2310                 aq_ret = I40E_ERR_PARAM;
2311                 goto err;
2312         }
2313
2314         vsi = pf->vsi[vf->lan_vsi_idx];
2315         aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
2316         /* send the response to the VF */
2317 err:
2318         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_RSS_LUT,
2319                                        aq_ret);
2320 }
2321
2322 /**
2323  * i40e_vc_get_rss_hena
2324  * @vf: pointer to the VF info
2325  * @msg: pointer to the msg buffer
2326  * @msglen: msg length
2327  *
2328  * Return the RSS HENA bits allowed by the hardware
2329  **/
2330 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2331 {
2332         struct i40e_virtchnl_rss_hena *vrh = NULL;
2333         struct i40e_pf *pf = vf->pf;
2334         i40e_status aq_ret = 0;
2335         int len = 0;
2336
2337         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
2338                 aq_ret = I40E_ERR_PARAM;
2339                 goto err;
2340         }
2341         len = sizeof(struct i40e_virtchnl_rss_hena);
2342
2343         vrh = kzalloc(len, GFP_KERNEL);
2344         if (!vrh) {
2345                 aq_ret = I40E_ERR_NO_MEMORY;
2346                 len = 0;
2347                 goto err;
2348         }
2349         vrh->hena = i40e_pf_get_default_rss_hena(pf);
2350 err:
2351         /* send the response back to the VF */
2352         aq_ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_RSS_HENA_CAPS,
2353                                         aq_ret, (u8 *)vrh, len);
2354         kfree(vrh);
2355         return aq_ret;
2356 }
2357
2358 /**
2359  * i40e_vc_set_rss_hena
2360  * @vf: pointer to the VF info
2361  * @msg: pointer to the msg buffer
2362  * @msglen: msg length
2363  *
2364  * Set the RSS HENA bits for the VF
2365  **/
2366 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2367 {
2368         struct i40e_virtchnl_rss_hena *vrh =
2369                 (struct i40e_virtchnl_rss_hena *)msg;
2370         struct i40e_pf *pf = vf->pf;
2371         struct i40e_hw *hw = &pf->hw;
2372         i40e_status aq_ret = 0;
2373
2374         if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
2375                 aq_ret = I40E_ERR_PARAM;
2376                 goto err;
2377         }
2378         i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
2379         i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
2380                           (u32)(vrh->hena >> 32));
2381
2382         /* send the response to the VF */
2383 err:
2384         return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_SET_RSS_HENA,
2385                                        aq_ret);
2386 }
2387
2388 /**
2389  * i40e_vc_validate_vf_msg
2390  * @vf: pointer to the VF info
2391  * @msg: pointer to the msg buffer
2392  * @msglen: msg length
2393  * @msghndl: msg handle
2394  *
2395  * validate msg
2396  **/
2397 static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
2398                                    u32 v_retval, u8 *msg, u16 msglen)
2399 {
2400         bool err_msg_format = false;
2401         int valid_len = 0;
2402
2403         /* Check if VF is disabled. */
2404         if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
2405                 return I40E_ERR_PARAM;
2406
2407         /* Validate message length. */
2408         switch (v_opcode) {
2409         case I40E_VIRTCHNL_OP_VERSION:
2410                 valid_len = sizeof(struct i40e_virtchnl_version_info);
2411                 break;
2412         case I40E_VIRTCHNL_OP_RESET_VF:
2413                 break;
2414         case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
2415                 if (VF_IS_V11(vf))
2416                         valid_len = sizeof(u32);
2417                 break;
2418         case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
2419                 valid_len = sizeof(struct i40e_virtchnl_txq_info);
2420                 break;
2421         case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
2422                 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
2423                 break;
2424         case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2425                 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
2426                 if (msglen >= valid_len) {
2427                         struct i40e_virtchnl_vsi_queue_config_info *vqc =
2428                             (struct i40e_virtchnl_vsi_queue_config_info *)msg;
2429                         valid_len += (vqc->num_queue_pairs *
2430                                       sizeof(struct
2431                                              i40e_virtchnl_queue_pair_info));
2432                         if (vqc->num_queue_pairs == 0)
2433                                 err_msg_format = true;
2434                 }
2435                 break;
2436         case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
2437                 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
2438                 if (msglen >= valid_len) {
2439                         struct i40e_virtchnl_irq_map_info *vimi =
2440                             (struct i40e_virtchnl_irq_map_info *)msg;
2441                         valid_len += (vimi->num_vectors *
2442                                       sizeof(struct i40e_virtchnl_vector_map));
2443                         if (vimi->num_vectors == 0)
2444                                 err_msg_format = true;
2445                 }
2446                 break;
2447         case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
2448         case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
2449                 valid_len = sizeof(struct i40e_virtchnl_queue_select);
2450                 break;
2451         case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
2452         case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
2453                 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
2454                 if (msglen >= valid_len) {
2455                         struct i40e_virtchnl_ether_addr_list *veal =
2456                             (struct i40e_virtchnl_ether_addr_list *)msg;
2457                         valid_len += veal->num_elements *
2458                             sizeof(struct i40e_virtchnl_ether_addr);
2459                         if (veal->num_elements == 0)
2460                                 err_msg_format = true;
2461                 }
2462                 break;
2463         case I40E_VIRTCHNL_OP_ADD_VLAN:
2464         case I40E_VIRTCHNL_OP_DEL_VLAN:
2465                 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
2466                 if (msglen >= valid_len) {
2467                         struct i40e_virtchnl_vlan_filter_list *vfl =
2468                             (struct i40e_virtchnl_vlan_filter_list *)msg;
2469                         valid_len += vfl->num_elements * sizeof(u16);
2470                         if (vfl->num_elements == 0)
2471                                 err_msg_format = true;
2472                 }
2473                 break;
2474         case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
2475                 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
2476                 break;
2477         case I40E_VIRTCHNL_OP_GET_STATS:
2478                 valid_len = sizeof(struct i40e_virtchnl_queue_select);
2479                 break;
2480         case I40E_VIRTCHNL_OP_IWARP:
2481                 /* These messages are opaque to us and will be validated in
2482                  * the RDMA client code. We just need to check for nonzero
2483                  * length. The firmware will enforce max length restrictions.
2484                  */
2485                 if (msglen)
2486                         valid_len = msglen;
2487                 else
2488                         err_msg_format = true;
2489                 break;
2490         case I40E_VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
2491                 valid_len = 0;
2492                 break;
2493         case I40E_VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
2494                 valid_len = sizeof(struct i40e_virtchnl_iwarp_qvlist_info);
2495                 if (msglen >= valid_len) {
2496                         struct i40e_virtchnl_iwarp_qvlist_info *qv =
2497                                 (struct i40e_virtchnl_iwarp_qvlist_info *)msg;
2498                         if (qv->num_vectors == 0) {
2499                                 err_msg_format = true;
2500                                 break;
2501                         }
2502                         valid_len += ((qv->num_vectors - 1) *
2503                                 sizeof(struct i40e_virtchnl_iwarp_qv_info));
2504                 }
2505                 break;
2506         case I40E_VIRTCHNL_OP_CONFIG_RSS_KEY:
2507                 valid_len = sizeof(struct i40e_virtchnl_rss_key);
2508                 if (msglen >= valid_len) {
2509                         struct i40e_virtchnl_rss_key *vrk =
2510                                 (struct i40e_virtchnl_rss_key *)msg;
2511                         if (vrk->key_len != I40E_HKEY_ARRAY_SIZE) {
2512                                 err_msg_format = true;
2513                                 break;
2514                         }
2515                         valid_len += vrk->key_len - 1;
2516                 }
2517                 break;
2518         case I40E_VIRTCHNL_OP_CONFIG_RSS_LUT:
2519                 valid_len = sizeof(struct i40e_virtchnl_rss_lut);
2520                 if (msglen >= valid_len) {
2521                         struct i40e_virtchnl_rss_lut *vrl =
2522                                 (struct i40e_virtchnl_rss_lut *)msg;
2523                         if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE) {
2524                                 err_msg_format = true;
2525                                 break;
2526                         }
2527                         valid_len += vrl->lut_entries - 1;
2528                 }
2529                 break;
2530         case I40E_VIRTCHNL_OP_GET_RSS_HENA_CAPS:
2531                 break;
2532         case I40E_VIRTCHNL_OP_SET_RSS_HENA:
2533                 valid_len = sizeof(struct i40e_virtchnl_rss_hena);
2534                 break;
2535         /* These are always errors coming from the VF. */
2536         case I40E_VIRTCHNL_OP_EVENT:
2537         case I40E_VIRTCHNL_OP_UNKNOWN:
2538         default:
2539                 return -EPERM;
2540         }
2541         /* few more checks */
2542         if ((valid_len != msglen) || (err_msg_format)) {
2543                 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
2544                 return -EINVAL;
2545         } else {
2546                 return 0;
2547         }
2548 }
2549
2550 /**
2551  * i40e_vc_process_vf_msg
2552  * @pf: pointer to the PF structure
2553  * @vf_id: source VF id
2554  * @msg: pointer to the msg buffer
2555  * @msglen: msg length
2556  * @msghndl: msg handle
2557  *
2558  * called from the common aeq/arq handler to
2559  * process request from VF
2560  **/
2561 int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
2562                            u32 v_retval, u8 *msg, u16 msglen)
2563 {
2564         struct i40e_hw *hw = &pf->hw;
2565         int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
2566         struct i40e_vf *vf;
2567         int ret;
2568
2569         pf->vf_aq_requests++;
2570         if (local_vf_id >= pf->num_alloc_vfs)
2571                 return -EINVAL;
2572         vf = &(pf->vf[local_vf_id]);
2573         /* perform basic checks on the msg */
2574         ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
2575
2576         if (ret) {
2577                 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
2578                         local_vf_id, v_opcode, msglen);
2579                 return ret;
2580         }
2581
2582         switch (v_opcode) {
2583         case I40E_VIRTCHNL_OP_VERSION:
2584                 ret = i40e_vc_get_version_msg(vf, msg);
2585                 break;
2586         case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
2587                 ret = i40e_vc_get_vf_resources_msg(vf, msg);
2588                 break;
2589         case I40E_VIRTCHNL_OP_RESET_VF:
2590                 i40e_vc_reset_vf_msg(vf);
2591                 ret = 0;
2592                 break;
2593         case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
2594                 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
2595                 break;
2596         case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2597                 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
2598                 break;
2599         case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
2600                 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
2601                 break;
2602         case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
2603                 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
2604                 i40e_vc_notify_vf_link_state(vf);
2605                 break;
2606         case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
2607                 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
2608                 break;
2609         case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
2610                 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
2611                 break;
2612         case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
2613                 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
2614                 break;
2615         case I40E_VIRTCHNL_OP_ADD_VLAN:
2616                 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
2617                 break;
2618         case I40E_VIRTCHNL_OP_DEL_VLAN:
2619                 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
2620                 break;
2621         case I40E_VIRTCHNL_OP_GET_STATS:
2622                 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
2623                 break;
2624         case I40E_VIRTCHNL_OP_IWARP:
2625                 ret = i40e_vc_iwarp_msg(vf, msg, msglen);
2626                 break;
2627         case I40E_VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
2628                 ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
2629                 break;
2630         case I40E_VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
2631                 ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
2632                 break;
2633         case I40E_VIRTCHNL_OP_CONFIG_RSS_KEY:
2634                 ret = i40e_vc_config_rss_key(vf, msg, msglen);
2635                 break;
2636         case I40E_VIRTCHNL_OP_CONFIG_RSS_LUT:
2637                 ret = i40e_vc_config_rss_lut(vf, msg, msglen);
2638                 break;
2639         case I40E_VIRTCHNL_OP_GET_RSS_HENA_CAPS:
2640                 ret = i40e_vc_get_rss_hena(vf, msg, msglen);
2641                 break;
2642         case I40E_VIRTCHNL_OP_SET_RSS_HENA:
2643                 ret = i40e_vc_set_rss_hena(vf, msg, msglen);
2644                 break;
2645
2646         case I40E_VIRTCHNL_OP_UNKNOWN:
2647         default:
2648                 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
2649                         v_opcode, local_vf_id);
2650                 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
2651                                               I40E_ERR_NOT_IMPLEMENTED);
2652                 break;
2653         }
2654
2655         return ret;
2656 }
2657
2658 /**
2659  * i40e_vc_process_vflr_event
2660  * @pf: pointer to the PF structure
2661  *
2662  * called from the vlfr irq handler to
2663  * free up VF resources and state variables
2664  **/
2665 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
2666 {
2667         struct i40e_hw *hw = &pf->hw;
2668         u32 reg, reg_idx, bit_idx;
2669         struct i40e_vf *vf;
2670         int vf_id;
2671
2672         if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
2673                 return 0;
2674
2675         /* Re-enable the VFLR interrupt cause here, before looking for which
2676          * VF got reset. Otherwise, if another VF gets a reset while the
2677          * first one is being processed, that interrupt will be lost, and
2678          * that VF will be stuck in reset forever.
2679          */
2680         reg = rd32(hw, I40E_PFINT_ICR0_ENA);
2681         reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
2682         wr32(hw, I40E_PFINT_ICR0_ENA, reg);
2683         i40e_flush(hw);
2684
2685         clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2686         for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
2687                 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2688                 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
2689                 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
2690                 vf = &pf->vf[vf_id];
2691                 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
2692                 if (reg & BIT(bit_idx))
2693                         /* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
2694                         i40e_reset_vf(vf, true);
2695         }
2696
2697         return 0;
2698 }
2699
2700 /**
2701  * i40e_ndo_set_vf_mac
2702  * @netdev: network interface device structure
2703  * @vf_id: VF identifier
2704  * @mac: mac address
2705  *
2706  * program VF mac address
2707  **/
2708 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2709 {
2710         struct i40e_netdev_priv *np = netdev_priv(netdev);
2711         struct i40e_vsi *vsi = np->vsi;
2712         struct i40e_pf *pf = vsi->back;
2713         struct i40e_mac_filter *f;
2714         struct i40e_vf *vf;
2715         int ret = 0;
2716         int bkt;
2717
2718         /* validate the request */
2719         if (vf_id >= pf->num_alloc_vfs) {
2720                 dev_err(&pf->pdev->dev,
2721                         "Invalid VF Identifier %d\n", vf_id);
2722                 ret = -EINVAL;
2723                 goto error_param;
2724         }
2725
2726         vf = &(pf->vf[vf_id]);
2727         vsi = pf->vsi[vf->lan_vsi_idx];
2728         if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2729                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2730                         vf_id);
2731                 ret = -EAGAIN;
2732                 goto error_param;
2733         }
2734
2735         if (is_multicast_ether_addr(mac)) {
2736                 dev_err(&pf->pdev->dev,
2737                         "Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
2738                 ret = -EINVAL;
2739                 goto error_param;
2740         }
2741
2742         /* Lock once because below invoked function add/del_filter requires
2743          * mac_filter_hash_lock to be held
2744          */
2745         spin_lock_bh(&vsi->mac_filter_hash_lock);
2746
2747         /* delete the temporary mac address */
2748         if (!is_zero_ether_addr(vf->default_lan_addr.addr))
2749                 i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
2750
2751         /* Delete all the filters for this VSI - we're going to kill it
2752          * anyway.
2753          */
2754         hash_for_each(vsi->mac_filter_hash, bkt, f, hlist)
2755                 __i40e_del_filter(vsi, f);
2756
2757         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2758
2759         dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2760         /* program mac filter */
2761         if (i40e_sync_vsi_filters(vsi)) {
2762                 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2763                 ret = -EIO;
2764                 goto error_param;
2765         }
2766         ether_addr_copy(vf->default_lan_addr.addr, mac);
2767         vf->pf_set_mac = true;
2768         /* Force the VF driver stop so it has to reload with new MAC address */
2769         i40e_vc_disable_vf(pf, vf);
2770         dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
2771
2772 error_param:
2773         return ret;
2774 }
2775
2776 /**
2777  * i40e_ndo_set_vf_port_vlan
2778  * @netdev: network interface device structure
2779  * @vf_id: VF identifier
2780  * @vlan_id: mac address
2781  * @qos: priority setting
2782  * @vlan_proto: vlan protocol
2783  *
2784  * program VF vlan id and/or qos
2785  **/
2786 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
2787                               u16 vlan_id, u8 qos, __be16 vlan_proto)
2788 {
2789         u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
2790         struct i40e_netdev_priv *np = netdev_priv(netdev);
2791         struct i40e_pf *pf = np->vsi->back;
2792         struct i40e_vsi *vsi;
2793         struct i40e_vf *vf;
2794         int ret = 0;
2795
2796         /* validate the request */
2797         if (vf_id >= pf->num_alloc_vfs) {
2798                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2799                 ret = -EINVAL;
2800                 goto error_pvid;
2801         }
2802
2803         if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2804                 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2805                 ret = -EINVAL;
2806                 goto error_pvid;
2807         }
2808
2809         if (vlan_proto != htons(ETH_P_8021Q)) {
2810                 dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
2811                 ret = -EPROTONOSUPPORT;
2812                 goto error_pvid;
2813         }
2814
2815         vf = &(pf->vf[vf_id]);
2816         vsi = pf->vsi[vf->lan_vsi_idx];
2817         if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2818                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2819                         vf_id);
2820                 ret = -EAGAIN;
2821                 goto error_pvid;
2822         }
2823
2824         if (le16_to_cpu(vsi->info.pvid) == vlanprio)
2825                 /* duplicate request, so just return success */
2826                 goto error_pvid;
2827
2828         /* Locked once because multiple functions below iterate list */
2829         spin_lock_bh(&vsi->mac_filter_hash_lock);
2830
2831         if (le16_to_cpu(vsi->info.pvid) == 0 && i40e_is_vsi_in_vlan(vsi)) {
2832                 dev_err(&pf->pdev->dev,
2833                         "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",
2834                         vf_id);
2835                 /* Administrator Error - knock the VF offline until he does
2836                  * the right thing by reconfiguring his network correctly
2837                  * and then reloading the VF driver.
2838                  */
2839                 i40e_vc_disable_vf(pf, vf);
2840                 /* During reset the VF got a new VSI, so refresh the pointer. */
2841                 vsi = pf->vsi[vf->lan_vsi_idx];
2842         }
2843
2844         /* Check for condition where there was already a port VLAN ID
2845          * filter set and now it is being deleted by setting it to zero.
2846          * Additionally check for the condition where there was a port
2847          * VLAN but now there is a new and different port VLAN being set.
2848          * Before deleting all the old VLAN filters we must add new ones
2849          * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2850          * MAC addresses deleted.
2851          */
2852         if ((!(vlan_id || qos) ||
2853             vlanprio != le16_to_cpu(vsi->info.pvid)) &&
2854             vsi->info.pvid) {
2855                 ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
2856                 if (ret) {
2857                         dev_info(&vsi->back->pdev->dev,
2858                                  "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2859                                  vsi->back->hw.aq.asq_last_status);
2860                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2861                         goto error_pvid;
2862                 }
2863         }
2864
2865         if (vsi->info.pvid) {
2866                 /* remove all filters on the old VLAN */
2867                 i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
2868                                            VLAN_VID_MASK));
2869         }
2870
2871         if (vlan_id || qos)
2872                 ret = i40e_vsi_add_pvid(vsi, vlanprio);
2873         else
2874                 i40e_vsi_remove_pvid(vsi);
2875
2876         if (vlan_id) {
2877                 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2878                          vlan_id, qos, vf_id);
2879
2880                 /* add new VLAN filter for each MAC */
2881                 ret = i40e_add_vlan_all_mac(vsi, vlan_id);
2882                 if (ret) {
2883                         dev_info(&vsi->back->pdev->dev,
2884                                  "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2885                                  vsi->back->hw.aq.asq_last_status);
2886                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2887                         goto error_pvid;
2888                 }
2889
2890                 /* remove the previously added non-VLAN MAC filters */
2891                 i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
2892         }
2893
2894         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2895
2896         /* Schedule the worker thread to take care of applying changes */
2897         i40e_service_event_schedule(vsi->back);
2898
2899         if (ret) {
2900                 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2901                 goto error_pvid;
2902         }
2903
2904         /* The Port VLAN needs to be saved across resets the same as the
2905          * default LAN MAC address.
2906          */
2907         vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
2908         ret = 0;
2909
2910 error_pvid:
2911         return ret;
2912 }
2913
2914 #define I40E_BW_CREDIT_DIVISOR 50     /* 50Mbps per BW credit */
2915 #define I40E_MAX_BW_INACTIVE_ACCUM 4  /* device can accumulate 4 credits max */
2916 /**
2917  * i40e_ndo_set_vf_bw
2918  * @netdev: network interface device structure
2919  * @vf_id: VF identifier
2920  * @tx_rate: Tx rate
2921  *
2922  * configure VF Tx rate
2923  **/
2924 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2925                        int max_tx_rate)
2926 {
2927         struct i40e_netdev_priv *np = netdev_priv(netdev);
2928         struct i40e_pf *pf = np->vsi->back;
2929         struct i40e_vsi *vsi;
2930         struct i40e_vf *vf;
2931         int speed = 0;
2932         int ret = 0;
2933
2934         /* validate the request */
2935         if (vf_id >= pf->num_alloc_vfs) {
2936                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2937                 ret = -EINVAL;
2938                 goto error;
2939         }
2940
2941         if (min_tx_rate) {
2942                 dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
2943                         min_tx_rate, vf_id);
2944                 return -EINVAL;
2945         }
2946
2947         vf = &(pf->vf[vf_id]);
2948         vsi = pf->vsi[vf->lan_vsi_idx];
2949         if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2950                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2951                         vf_id);
2952                 ret = -EAGAIN;
2953                 goto error;
2954         }
2955
2956         switch (pf->hw.phy.link_info.link_speed) {
2957         case I40E_LINK_SPEED_40GB:
2958                 speed = 40000;
2959                 break;
2960         case I40E_LINK_SPEED_25GB:
2961                 speed = 25000;
2962                 break;
2963         case I40E_LINK_SPEED_20GB:
2964                 speed = 20000;
2965                 break;
2966         case I40E_LINK_SPEED_10GB:
2967                 speed = 10000;
2968                 break;
2969         case I40E_LINK_SPEED_1GB:
2970                 speed = 1000;
2971                 break;
2972         default:
2973                 break;
2974         }
2975
2976         if (max_tx_rate > speed) {
2977                 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.\n",
2978                         max_tx_rate, vf->vf_id);
2979                 ret = -EINVAL;
2980                 goto error;
2981         }
2982
2983         if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2984                 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2985                 max_tx_rate = 50;
2986         }
2987
2988         /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
2989         ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2990                                           max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2991                                           I40E_MAX_BW_INACTIVE_ACCUM, NULL);
2992         if (ret) {
2993                 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
2994                         ret);
2995                 ret = -EIO;
2996                 goto error;
2997         }
2998         vf->tx_rate = max_tx_rate;
2999 error:
3000         return ret;
3001 }
3002
3003 /**
3004  * i40e_ndo_get_vf_config
3005  * @netdev: network interface device structure
3006  * @vf_id: VF identifier
3007  * @ivi: VF configuration structure
3008  *
3009  * return VF configuration
3010  **/
3011 int i40e_ndo_get_vf_config(struct net_device *netdev,
3012                            int vf_id, struct ifla_vf_info *ivi)
3013 {
3014         struct i40e_netdev_priv *np = netdev_priv(netdev);
3015         struct i40e_vsi *vsi = np->vsi;
3016         struct i40e_pf *pf = vsi->back;
3017         struct i40e_vf *vf;
3018         int ret = 0;
3019
3020         /* validate the request */
3021         if (vf_id >= pf->num_alloc_vfs) {
3022                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3023                 ret = -EINVAL;
3024                 goto error_param;
3025         }
3026
3027         vf = &(pf->vf[vf_id]);
3028         /* first vsi is always the LAN vsi */
3029         vsi = pf->vsi[vf->lan_vsi_idx];
3030         if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
3031                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3032                         vf_id);
3033                 ret = -EAGAIN;
3034                 goto error_param;
3035         }
3036
3037         ivi->vf = vf_id;
3038
3039         ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
3040
3041         ivi->max_tx_rate = vf->tx_rate;
3042         ivi->min_tx_rate = 0;
3043         ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
3044         ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
3045                    I40E_VLAN_PRIORITY_SHIFT;
3046         if (vf->link_forced == false)
3047                 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
3048         else if (vf->link_up == true)
3049                 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
3050         else
3051                 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
3052         ivi->spoofchk = vf->spoofchk;
3053         ivi->trusted = vf->trusted;
3054         ret = 0;
3055
3056 error_param:
3057         return ret;
3058 }
3059
3060 /**
3061  * i40e_ndo_set_vf_link_state
3062  * @netdev: network interface device structure
3063  * @vf_id: VF identifier
3064  * @link: required link state
3065  *
3066  * Set the link state of a specified VF, regardless of physical link state
3067  **/
3068 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
3069 {
3070         struct i40e_netdev_priv *np = netdev_priv(netdev);
3071         struct i40e_pf *pf = np->vsi->back;
3072         struct i40e_virtchnl_pf_event pfe;
3073         struct i40e_hw *hw = &pf->hw;
3074         struct i40e_vf *vf;
3075         int abs_vf_id;
3076         int ret = 0;
3077
3078         /* validate the request */
3079         if (vf_id >= pf->num_alloc_vfs) {
3080                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3081                 ret = -EINVAL;
3082                 goto error_out;
3083         }
3084
3085         vf = &pf->vf[vf_id];
3086         abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
3087
3088         pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
3089         pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
3090
3091         switch (link) {
3092         case IFLA_VF_LINK_STATE_AUTO:
3093                 vf->link_forced = false;
3094                 pfe.event_data.link_event.link_status =
3095                         pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
3096                 pfe.event_data.link_event.link_speed =
3097                         pf->hw.phy.link_info.link_speed;
3098                 break;
3099         case IFLA_VF_LINK_STATE_ENABLE:
3100                 vf->link_forced = true;
3101                 vf->link_up = true;
3102                 pfe.event_data.link_event.link_status = true;
3103                 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
3104                 break;
3105         case IFLA_VF_LINK_STATE_DISABLE:
3106                 vf->link_forced = true;
3107                 vf->link_up = false;
3108                 pfe.event_data.link_event.link_status = false;
3109                 pfe.event_data.link_event.link_speed = 0;
3110                 break;
3111         default:
3112                 ret = -EINVAL;
3113                 goto error_out;
3114         }
3115         /* Notify the VF of its new link state */
3116         i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
3117                                0, (u8 *)&pfe, sizeof(pfe), NULL);
3118
3119 error_out:
3120         return ret;
3121 }
3122
3123 /**
3124  * i40e_ndo_set_vf_spoofchk
3125  * @netdev: network interface device structure
3126  * @vf_id: VF identifier
3127  * @enable: flag to enable or disable feature
3128  *
3129  * Enable or disable VF spoof checking
3130  **/
3131 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
3132 {
3133         struct i40e_netdev_priv *np = netdev_priv(netdev);
3134         struct i40e_vsi *vsi = np->vsi;
3135         struct i40e_pf *pf = vsi->back;
3136         struct i40e_vsi_context ctxt;
3137         struct i40e_hw *hw = &pf->hw;
3138         struct i40e_vf *vf;
3139         int ret = 0;
3140
3141         /* validate the request */
3142         if (vf_id >= pf->num_alloc_vfs) {
3143                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3144                 ret = -EINVAL;
3145                 goto out;
3146         }
3147
3148         vf = &(pf->vf[vf_id]);
3149         if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
3150                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3151                         vf_id);
3152                 ret = -EAGAIN;
3153                 goto out;
3154         }
3155
3156         if (enable == vf->spoofchk)
3157                 goto out;
3158
3159         vf->spoofchk = enable;
3160         memset(&ctxt, 0, sizeof(ctxt));
3161         ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
3162         ctxt.pf_num = pf->hw.pf_id;
3163         ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
3164         if (enable)
3165                 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
3166                                         I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
3167         ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
3168         if (ret) {
3169                 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
3170                         ret);
3171                 ret = -EIO;
3172         }
3173 out:
3174         return ret;
3175 }
3176
3177 /**
3178  * i40e_ndo_set_vf_trust
3179  * @netdev: network interface device structure of the pf
3180  * @vf_id: VF identifier
3181  * @setting: trust setting
3182  *
3183  * Enable or disable VF trust setting
3184  **/
3185 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
3186 {
3187         struct i40e_netdev_priv *np = netdev_priv(netdev);
3188         struct i40e_pf *pf = np->vsi->back;
3189         struct i40e_vf *vf;
3190         int ret = 0;
3191
3192         /* validate the request */
3193         if (vf_id >= pf->num_alloc_vfs) {
3194                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3195                 return -EINVAL;
3196         }
3197
3198         if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3199                 dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
3200                 return -EINVAL;
3201         }
3202
3203         vf = &pf->vf[vf_id];
3204
3205         if (!vf)
3206                 return -EINVAL;
3207         if (setting == vf->trusted)
3208                 goto out;
3209
3210         vf->trusted = setting;
3211         i40e_vc_notify_vf_reset(vf);
3212         i40e_reset_vf(vf, false);
3213         dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
3214                  vf_id, setting ? "" : "un");
3215 out:
3216         return ret;
3217 }