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