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