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