GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / net / ethernet / intel / i40e / i40e_virtchnl_pf.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4 #include "i40e.h"
5
6 /*********************notification routines***********************/
7
8 /**
9  * i40e_vc_vf_broadcast
10  * @pf: pointer to the PF structure
11  * @v_opcode: operation code
12  * @v_retval: return value
13  * @msg: pointer to the msg buffer
14  * @msglen: msg length
15  *
16  * send a message to all VFs on a given PF
17  **/
18 static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
19                                  enum virtchnl_ops v_opcode,
20                                  i40e_status v_retval, u8 *msg,
21                                  u16 msglen)
22 {
23         struct i40e_hw *hw = &pf->hw;
24         struct i40e_vf *vf = pf->vf;
25         int i;
26
27         for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
28                 int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
29                 /* Not all vfs are enabled so skip the ones that are not */
30                 if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
31                     !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
32                         continue;
33
34                 /* Ignore return value on purpose - a given VF may fail, but
35                  * we need to keep going and send to all of them
36                  */
37                 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
38                                        msg, msglen, NULL);
39         }
40 }
41
42 /**
43  * i40e_vc_notify_vf_link_state
44  * @vf: pointer to the VF structure
45  *
46  * send a link status message to a single VF
47  **/
48 static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
49 {
50         struct virtchnl_pf_event pfe;
51         struct i40e_pf *pf = vf->pf;
52         struct i40e_hw *hw = &pf->hw;
53         struct i40e_link_status *ls = &pf->hw.phy.link_info;
54         int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
55
56         pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
57         pfe.severity = PF_EVENT_SEVERITY_INFO;
58         if (vf->link_forced) {
59                 pfe.event_data.link_event.link_status = vf->link_up;
60                 pfe.event_data.link_event.link_speed =
61                         (vf->link_up ? VIRTCHNL_LINK_SPEED_40GB : 0);
62         } else {
63                 pfe.event_data.link_event.link_status =
64                         ls->link_info & I40E_AQ_LINK_UP;
65                 pfe.event_data.link_event.link_speed =
66                         i40e_virtchnl_link_speed(ls->link_speed);
67         }
68         i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
69                                0, (u8 *)&pfe, sizeof(pfe), NULL);
70 }
71
72 /**
73  * i40e_vc_notify_link_state
74  * @pf: pointer to the PF structure
75  *
76  * send a link status message to all VFs on a given PF
77  **/
78 void i40e_vc_notify_link_state(struct i40e_pf *pf)
79 {
80         int i;
81
82         for (i = 0; i < pf->num_alloc_vfs; i++)
83                 i40e_vc_notify_vf_link_state(&pf->vf[i]);
84 }
85
86 /**
87  * i40e_vc_notify_reset
88  * @pf: pointer to the PF structure
89  *
90  * indicate a pending reset to all VFs on a given PF
91  **/
92 void i40e_vc_notify_reset(struct i40e_pf *pf)
93 {
94         struct virtchnl_pf_event pfe;
95
96         pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
97         pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
98         i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
99                              (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
100 }
101
102 /**
103  * i40e_vc_notify_vf_reset
104  * @vf: pointer to the VF structure
105  *
106  * indicate a pending reset to the given VF
107  **/
108 void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
109 {
110         struct virtchnl_pf_event pfe;
111         int abs_vf_id;
112
113         /* validate the request */
114         if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
115                 return;
116
117         /* verify if the VF is in either init or active before proceeding */
118         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
119             !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
120                 return;
121
122         abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
123
124         pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
125         pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
126         i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, VIRTCHNL_OP_EVENT,
127                                0, (u8 *)&pfe,
128                                sizeof(struct virtchnl_pf_event), NULL);
129 }
130 /***********************misc routines*****************************/
131
132 /**
133  * i40e_vc_disable_vf
134  * @vf: pointer to the VF info
135  *
136  * Disable the VF through a SW reset.
137  **/
138 static inline void i40e_vc_disable_vf(struct i40e_vf *vf)
139 {
140         struct i40e_pf *pf = vf->pf;
141         int i;
142
143         i40e_vc_notify_vf_reset(vf);
144
145         /* We want to ensure that an actual reset occurs initiated after this
146          * function was called. However, we do not want to wait forever, so
147          * we'll give a reasonable time and print a message if we failed to
148          * ensure a reset.
149          */
150         for (i = 0; i < 20; i++) {
151                 /* If PF is in VFs releasing state reset VF is impossible,
152                  * so leave it.
153                  */
154                 if (test_bit(__I40E_VFS_RELEASING, pf->state))
155                         return;
156                 if (i40e_reset_vf(vf, false))
157                         return;
158                 usleep_range(10000, 20000);
159         }
160
161         dev_warn(&vf->pf->pdev->dev,
162                  "Failed to initiate reset for VF %d after 200 milliseconds\n",
163                  vf->vf_id);
164 }
165
166 /**
167  * i40e_vc_isvalid_vsi_id
168  * @vf: pointer to the VF info
169  * @vsi_id: VF relative VSI id
170  *
171  * check for the valid VSI id
172  **/
173 static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
174 {
175         struct i40e_pf *pf = vf->pf;
176         struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
177
178         return (vsi && (vsi->vf_id == vf->vf_id));
179 }
180
181 /**
182  * i40e_vc_isvalid_queue_id
183  * @vf: pointer to the VF info
184  * @vsi_id: vsi id
185  * @qid: vsi relative queue id
186  *
187  * check for the valid queue id
188  **/
189 static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
190                                             u16 qid)
191 {
192         struct i40e_pf *pf = vf->pf;
193         struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
194
195         return (vsi && (qid < vsi->alloc_queue_pairs));
196 }
197
198 /**
199  * i40e_vc_isvalid_vector_id
200  * @vf: pointer to the VF info
201  * @vector_id: VF relative vector id
202  *
203  * check for the valid vector id
204  **/
205 static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u32 vector_id)
206 {
207         struct i40e_pf *pf = vf->pf;
208
209         return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
210 }
211
212 /***********************vf resource mgmt routines*****************/
213
214 /**
215  * i40e_vc_get_pf_queue_id
216  * @vf: pointer to the VF info
217  * @vsi_id: id of VSI as provided by the FW
218  * @vsi_queue_id: vsi relative queue id
219  *
220  * return PF relative queue id
221  **/
222 static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
223                                    u8 vsi_queue_id)
224 {
225         struct i40e_pf *pf = vf->pf;
226         struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
227         u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
228
229         if (!vsi)
230                 return pf_queue_id;
231
232         if (le16_to_cpu(vsi->info.mapping_flags) &
233             I40E_AQ_VSI_QUE_MAP_NONCONTIG)
234                 pf_queue_id =
235                         le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
236         else
237                 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
238                               vsi_queue_id;
239
240         return pf_queue_id;
241 }
242
243 /**
244  * i40e_get_real_pf_qid
245  * @vf: pointer to the VF info
246  * @vsi_id: vsi id
247  * @queue_id: queue number
248  *
249  * wrapper function to get pf_queue_id handling ADq code as well
250  **/
251 static u16 i40e_get_real_pf_qid(struct i40e_vf *vf, u16 vsi_id, u16 queue_id)
252 {
253         int i;
254
255         if (vf->adq_enabled) {
256                 /* Although VF considers all the queues(can be 1 to 16) as its
257                  * own but they may actually belong to different VSIs(up to 4).
258                  * We need to find which queues belongs to which VSI.
259                  */
260                 for (i = 0; i < vf->num_tc; i++) {
261                         if (queue_id < vf->ch[i].num_qps) {
262                                 vsi_id = vf->ch[i].vsi_id;
263                                 break;
264                         }
265                         /* find right queue id which is relative to a
266                          * given VSI.
267                          */
268                         queue_id -= vf->ch[i].num_qps;
269                         }
270                 }
271
272         return i40e_vc_get_pf_queue_id(vf, vsi_id, queue_id);
273 }
274
275 /**
276  * i40e_config_irq_link_list
277  * @vf: pointer to the VF info
278  * @vsi_id: id of VSI as given by the FW
279  * @vecmap: irq map info
280  *
281  * configure irq link list from the map
282  **/
283 static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
284                                       struct virtchnl_vector_map *vecmap)
285 {
286         unsigned long linklistmap = 0, tempmap;
287         struct i40e_pf *pf = vf->pf;
288         struct i40e_hw *hw = &pf->hw;
289         u16 vsi_queue_id, pf_queue_id;
290         enum i40e_queue_type qtype;
291         u16 next_q, vector_id, size;
292         u32 reg, reg_idx;
293         u16 itr_idx = 0;
294
295         vector_id = vecmap->vector_id;
296         /* setup the head */
297         if (0 == vector_id)
298                 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
299         else
300                 reg_idx = I40E_VPINT_LNKLSTN(
301                      ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
302                      (vector_id - 1));
303
304         if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
305                 /* Special case - No queues mapped on this vector */
306                 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
307                 goto irq_list_done;
308         }
309         tempmap = vecmap->rxq_map;
310         for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
311                 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
312                                     vsi_queue_id));
313         }
314
315         tempmap = vecmap->txq_map;
316         for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
317                 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
318                                      vsi_queue_id + 1));
319         }
320
321         size = I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES;
322         next_q = find_first_bit(&linklistmap, size);
323         if (unlikely(next_q == size))
324                 goto irq_list_done;
325
326         vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
327         qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
328         pf_queue_id = i40e_get_real_pf_qid(vf, vsi_id, vsi_queue_id);
329         reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
330
331         wr32(hw, reg_idx, reg);
332
333         while (next_q < size) {
334                 switch (qtype) {
335                 case I40E_QUEUE_TYPE_RX:
336                         reg_idx = I40E_QINT_RQCTL(pf_queue_id);
337                         itr_idx = vecmap->rxitr_idx;
338                         break;
339                 case I40E_QUEUE_TYPE_TX:
340                         reg_idx = I40E_QINT_TQCTL(pf_queue_id);
341                         itr_idx = vecmap->txitr_idx;
342                         break;
343                 default:
344                         break;
345                 }
346
347                 next_q = find_next_bit(&linklistmap, size, next_q + 1);
348                 if (next_q < size) {
349                         vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
350                         qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
351                         pf_queue_id = i40e_get_real_pf_qid(vf,
352                                                            vsi_id,
353                                                            vsi_queue_id);
354                 } else {
355                         pf_queue_id = I40E_QUEUE_END_OF_LIST;
356                         qtype = 0;
357                 }
358
359                 /* format for the RQCTL & TQCTL regs is same */
360                 reg = (vector_id) |
361                     (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
362                     (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
363                     BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
364                     (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
365                 wr32(hw, reg_idx, reg);
366         }
367
368         /* if the vf is running in polling mode and using interrupt zero,
369          * need to disable auto-mask on enabling zero interrupt for VFs.
370          */
371         if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
372             (vector_id == 0)) {
373                 reg = rd32(hw, I40E_GLINT_CTL);
374                 if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
375                         reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
376                         wr32(hw, I40E_GLINT_CTL, reg);
377                 }
378         }
379
380 irq_list_done:
381         i40e_flush(hw);
382 }
383
384 /**
385  * i40e_release_iwarp_qvlist
386  * @vf: pointer to the VF.
387  *
388  **/
389 static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
390 {
391         struct i40e_pf *pf = vf->pf;
392         struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
393         u32 msix_vf;
394         u32 i;
395
396         if (!vf->qvlist_info)
397                 return;
398
399         msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
400         for (i = 0; i < qvlist_info->num_vectors; i++) {
401                 struct virtchnl_iwarp_qv_info *qv_info;
402                 u32 next_q_index, next_q_type;
403                 struct i40e_hw *hw = &pf->hw;
404                 u32 v_idx, reg_idx, reg;
405
406                 qv_info = &qvlist_info->qv_info[i];
407                 if (!qv_info)
408                         continue;
409                 v_idx = qv_info->v_idx;
410                 if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
411                         /* Figure out the queue after CEQ and make that the
412                          * first queue.
413                          */
414                         reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
415                         reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
416                         next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
417                                         >> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
418                         next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
419                                         >> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
420
421                         reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
422                         reg = (next_q_index &
423                                I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
424                                (next_q_type <<
425                                I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
426
427                         wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
428                 }
429         }
430         kfree(vf->qvlist_info);
431         vf->qvlist_info = NULL;
432 }
433
434 /**
435  * i40e_config_iwarp_qvlist
436  * @vf: pointer to the VF info
437  * @qvlist_info: queue and vector list
438  *
439  * Return 0 on success or < 0 on error
440  **/
441 static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
442                                     struct virtchnl_iwarp_qvlist_info *qvlist_info)
443 {
444         struct i40e_pf *pf = vf->pf;
445         struct i40e_hw *hw = &pf->hw;
446         struct virtchnl_iwarp_qv_info *qv_info;
447         u32 v_idx, i, reg_idx, reg;
448         u32 next_q_idx, next_q_type;
449         u32 msix_vf, size;
450         int ret = 0;
451
452         msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
453
454         if (qvlist_info->num_vectors > msix_vf) {
455                 dev_warn(&pf->pdev->dev,
456                          "Incorrect number of iwarp vectors %u. Maximum %u allowed.\n",
457                          qvlist_info->num_vectors,
458                          msix_vf);
459                 ret = -EINVAL;
460                 goto err_out;
461         }
462
463         size = sizeof(struct virtchnl_iwarp_qvlist_info) +
464                (sizeof(struct virtchnl_iwarp_qv_info) *
465                                                 (qvlist_info->num_vectors - 1));
466         kfree(vf->qvlist_info);
467         vf->qvlist_info = kzalloc(size, GFP_KERNEL);
468         if (!vf->qvlist_info) {
469                 ret = -ENOMEM;
470                 goto err_out;
471         }
472         vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
473
474         msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
475         for (i = 0; i < qvlist_info->num_vectors; i++) {
476                 qv_info = &qvlist_info->qv_info[i];
477                 if (!qv_info)
478                         continue;
479                 v_idx = qv_info->v_idx;
480
481                 /* Validate vector id belongs to this vf */
482                 if (!i40e_vc_isvalid_vector_id(vf, v_idx)) {
483                         ret = -EINVAL;
484                         goto err_free;
485                 }
486
487                 vf->qvlist_info->qv_info[i] = *qv_info;
488
489                 reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
490                 /* We might be sharing the interrupt, so get the first queue
491                  * index and type, push it down the list by adding the new
492                  * queue on top. Also link it with the new queue in CEQCTL.
493                  */
494                 reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
495                 next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
496                                 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
497                 next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
498                                 I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
499
500                 if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
501                         reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
502                         reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
503                         (v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
504                         (qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
505                         (next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
506                         (next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
507                         wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
508
509                         reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
510                         reg = (qv_info->ceq_idx &
511                                I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
512                                (I40E_QUEUE_TYPE_PE_CEQ <<
513                                I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
514                         wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
515                 }
516
517                 if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
518                         reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
519                         (v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
520                         (qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
521
522                         wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
523                 }
524         }
525
526         return 0;
527 err_free:
528         kfree(vf->qvlist_info);
529         vf->qvlist_info = NULL;
530 err_out:
531         return ret;
532 }
533
534 /**
535  * i40e_config_vsi_tx_queue
536  * @vf: pointer to the VF info
537  * @vsi_id: id of VSI as provided by the FW
538  * @vsi_queue_id: vsi relative queue index
539  * @info: config. info
540  *
541  * configure tx queue
542  **/
543 static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
544                                     u16 vsi_queue_id,
545                                     struct virtchnl_txq_info *info)
546 {
547         struct i40e_pf *pf = vf->pf;
548         struct i40e_hw *hw = &pf->hw;
549         struct i40e_hmc_obj_txq tx_ctx;
550         struct i40e_vsi *vsi;
551         u16 pf_queue_id;
552         u32 qtx_ctl;
553         int ret = 0;
554
555         if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
556                 ret = -ENOENT;
557                 goto error_context;
558         }
559         pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
560         vsi = i40e_find_vsi_from_id(pf, vsi_id);
561         if (!vsi) {
562                 ret = -ENOENT;
563                 goto error_context;
564         }
565
566         /* clear the context structure first */
567         memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
568
569         /* only set the required fields */
570         tx_ctx.base = info->dma_ring_addr / 128;
571         tx_ctx.qlen = info->ring_len;
572         tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
573         tx_ctx.rdylist_act = 0;
574         tx_ctx.head_wb_ena = info->headwb_enabled;
575         tx_ctx.head_wb_addr = info->dma_headwb_addr;
576
577         /* clear the context in the HMC */
578         ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
579         if (ret) {
580                 dev_err(&pf->pdev->dev,
581                         "Failed to clear VF LAN Tx queue context %d, error: %d\n",
582                         pf_queue_id, ret);
583                 ret = -ENOENT;
584                 goto error_context;
585         }
586
587         /* set the context in the HMC */
588         ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
589         if (ret) {
590                 dev_err(&pf->pdev->dev,
591                         "Failed to set VF LAN Tx queue context %d error: %d\n",
592                         pf_queue_id, ret);
593                 ret = -ENOENT;
594                 goto error_context;
595         }
596
597         /* associate this queue with the PCI VF function */
598         qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
599         qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
600                     & I40E_QTX_CTL_PF_INDX_MASK);
601         qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
602                      << I40E_QTX_CTL_VFVM_INDX_SHIFT)
603                     & I40E_QTX_CTL_VFVM_INDX_MASK);
604         wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
605         i40e_flush(hw);
606
607 error_context:
608         return ret;
609 }
610
611 /**
612  * i40e_config_vsi_rx_queue
613  * @vf: pointer to the VF info
614  * @vsi_id: id of VSI  as provided by the FW
615  * @vsi_queue_id: vsi relative queue index
616  * @info: config. info
617  *
618  * configure rx queue
619  **/
620 static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
621                                     u16 vsi_queue_id,
622                                     struct virtchnl_rxq_info *info)
623 {
624         struct i40e_pf *pf = vf->pf;
625         struct i40e_hw *hw = &pf->hw;
626         struct i40e_hmc_obj_rxq rx_ctx;
627         u16 pf_queue_id;
628         int ret = 0;
629
630         pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
631
632         /* clear the context structure first */
633         memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
634
635         /* only set the required fields */
636         rx_ctx.base = info->dma_ring_addr / 128;
637         rx_ctx.qlen = info->ring_len;
638
639         if (info->splithdr_enabled) {
640                 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2      |
641                                   I40E_RX_SPLIT_IP      |
642                                   I40E_RX_SPLIT_TCP_UDP |
643                                   I40E_RX_SPLIT_SCTP;
644                 /* header length validation */
645                 if (info->hdr_size > ((2 * 1024) - 64)) {
646                         ret = -EINVAL;
647                         goto error_param;
648                 }
649                 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
650
651                 /* set split mode 10b */
652                 rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
653         }
654
655         /* databuffer length validation */
656         if (info->databuffer_size > ((16 * 1024) - 128)) {
657                 ret = -EINVAL;
658                 goto error_param;
659         }
660         rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
661
662         /* max pkt. length validation */
663         if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
664                 ret = -EINVAL;
665                 goto error_param;
666         }
667         rx_ctx.rxmax = info->max_pkt_size;
668
669         /* enable 32bytes desc always */
670         rx_ctx.dsize = 1;
671
672         /* default values */
673         rx_ctx.lrxqthresh = 1;
674         rx_ctx.crcstrip = 1;
675         rx_ctx.prefena = 1;
676         rx_ctx.l2tsel = 1;
677
678         /* clear the context in the HMC */
679         ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
680         if (ret) {
681                 dev_err(&pf->pdev->dev,
682                         "Failed to clear VF LAN Rx queue context %d, error: %d\n",
683                         pf_queue_id, ret);
684                 ret = -ENOENT;
685                 goto error_param;
686         }
687
688         /* set the context in the HMC */
689         ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
690         if (ret) {
691                 dev_err(&pf->pdev->dev,
692                         "Failed to set VF LAN Rx queue context %d error: %d\n",
693                         pf_queue_id, ret);
694                 ret = -ENOENT;
695                 goto error_param;
696         }
697
698 error_param:
699         return ret;
700 }
701
702 /**
703  * i40e_alloc_vsi_res
704  * @vf: pointer to the VF info
705  * @idx: VSI index, applies only for ADq mode, zero otherwise
706  *
707  * alloc VF vsi context & resources
708  **/
709 static int i40e_alloc_vsi_res(struct i40e_vf *vf, u8 idx)
710 {
711         struct i40e_mac_filter *f = NULL;
712         struct i40e_pf *pf = vf->pf;
713         struct i40e_vsi *vsi;
714         u64 max_tx_rate = 0;
715         int ret = 0;
716
717         vsi = i40e_vsi_setup(pf, I40E_VSI_SRIOV, pf->vsi[pf->lan_vsi]->seid,
718                              vf->vf_id);
719
720         if (!vsi) {
721                 dev_err(&pf->pdev->dev,
722                         "add vsi failed for VF %d, aq_err %d\n",
723                         vf->vf_id, pf->hw.aq.asq_last_status);
724                 ret = -ENOENT;
725                 goto error_alloc_vsi_res;
726         }
727
728         if (!idx) {
729                 u64 hena = i40e_pf_get_default_rss_hena(pf);
730                 u8 broadcast[ETH_ALEN];
731
732                 vf->lan_vsi_idx = vsi->idx;
733                 vf->lan_vsi_id = vsi->id;
734                 /* If the port VLAN has been configured and then the
735                  * VF driver was removed then the VSI port VLAN
736                  * configuration was destroyed.  Check if there is
737                  * a port VLAN and restore the VSI configuration if
738                  * needed.
739                  */
740                 if (vf->port_vlan_id)
741                         i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
742
743                 spin_lock_bh(&vsi->mac_filter_hash_lock);
744                 if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
745                         f = i40e_add_mac_filter(vsi,
746                                                 vf->default_lan_addr.addr);
747                         if (!f)
748                                 dev_info(&pf->pdev->dev,
749                                          "Could not add MAC filter %pM for VF %d\n",
750                                         vf->default_lan_addr.addr, vf->vf_id);
751                 }
752                 eth_broadcast_addr(broadcast);
753                 f = i40e_add_mac_filter(vsi, broadcast);
754                 if (!f)
755                         dev_info(&pf->pdev->dev,
756                                  "Could not allocate VF broadcast filter\n");
757                 spin_unlock_bh(&vsi->mac_filter_hash_lock);
758                 wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
759                 wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
760                 /* program mac filter only for VF VSI */
761                 ret = i40e_sync_vsi_filters(vsi);
762                 if (ret)
763                         dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
764         }
765
766         /* storing VSI index and id for ADq and don't apply the mac filter */
767         if (vf->adq_enabled) {
768                 vf->ch[idx].vsi_idx = vsi->idx;
769                 vf->ch[idx].vsi_id = vsi->id;
770         }
771
772         /* Set VF bandwidth if specified */
773         if (vf->tx_rate) {
774                 max_tx_rate = vf->tx_rate;
775         } else if (vf->ch[idx].max_tx_rate) {
776                 max_tx_rate = vf->ch[idx].max_tx_rate;
777         }
778
779         if (max_tx_rate) {
780                 max_tx_rate = div_u64(max_tx_rate, I40E_BW_CREDIT_DIVISOR);
781                 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
782                                                   max_tx_rate, 0, NULL);
783                 if (ret)
784                         dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
785                                 vf->vf_id, ret);
786         }
787
788 error_alloc_vsi_res:
789         return ret;
790 }
791
792 /**
793  * i40e_map_pf_queues_to_vsi
794  * @vf: pointer to the VF info
795  *
796  * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
797  * function takes care of first part VSILAN_QTABLE, mapping pf queues to VSI.
798  **/
799 static void i40e_map_pf_queues_to_vsi(struct i40e_vf *vf)
800 {
801         struct i40e_pf *pf = vf->pf;
802         struct i40e_hw *hw = &pf->hw;
803         u32 reg, num_tc = 1; /* VF has at least one traffic class */
804         u16 vsi_id, qps;
805         int i, j;
806
807         if (vf->adq_enabled)
808                 num_tc = vf->num_tc;
809
810         for (i = 0; i < num_tc; i++) {
811                 if (vf->adq_enabled) {
812                         qps = vf->ch[i].num_qps;
813                         vsi_id =  vf->ch[i].vsi_id;
814                 } else {
815                         qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
816                         vsi_id = vf->lan_vsi_id;
817                 }
818
819                 for (j = 0; j < 7; j++) {
820                         if (j * 2 >= qps) {
821                                 /* end of list */
822                                 reg = 0x07FF07FF;
823                         } else {
824                                 u16 qid = i40e_vc_get_pf_queue_id(vf,
825                                                                   vsi_id,
826                                                                   j * 2);
827                                 reg = qid;
828                                 qid = i40e_vc_get_pf_queue_id(vf, vsi_id,
829                                                               (j * 2) + 1);
830                                 reg |= qid << 16;
831                         }
832                         i40e_write_rx_ctl(hw,
833                                           I40E_VSILAN_QTABLE(j, vsi_id),
834                                           reg);
835                 }
836         }
837 }
838
839 /**
840  * i40e_map_pf_to_vf_queues
841  * @vf: pointer to the VF info
842  *
843  * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
844  * function takes care of the second part VPLAN_QTABLE & completes VF mappings.
845  **/
846 static void i40e_map_pf_to_vf_queues(struct i40e_vf *vf)
847 {
848         struct i40e_pf *pf = vf->pf;
849         struct i40e_hw *hw = &pf->hw;
850         u32 reg, total_qps = 0;
851         u32 qps, num_tc = 1; /* VF has at least one traffic class */
852         u16 vsi_id, qid;
853         int i, j;
854
855         if (vf->adq_enabled)
856                 num_tc = vf->num_tc;
857
858         for (i = 0; i < num_tc; i++) {
859                 if (vf->adq_enabled) {
860                         qps = vf->ch[i].num_qps;
861                         vsi_id =  vf->ch[i].vsi_id;
862                 } else {
863                         qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
864                         vsi_id = vf->lan_vsi_id;
865                 }
866
867                 for (j = 0; j < qps; j++) {
868                         qid = i40e_vc_get_pf_queue_id(vf, vsi_id, j);
869
870                         reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
871                         wr32(hw, I40E_VPLAN_QTABLE(total_qps, vf->vf_id),
872                              reg);
873                         total_qps++;
874                 }
875         }
876 }
877
878 /**
879  * i40e_enable_vf_mappings
880  * @vf: pointer to the VF info
881  *
882  * enable VF mappings
883  **/
884 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
885 {
886         struct i40e_pf *pf = vf->pf;
887         struct i40e_hw *hw = &pf->hw;
888         u32 reg;
889
890         /* Tell the hardware we're using noncontiguous mapping. HW requires
891          * that VF queues be mapped using this method, even when they are
892          * contiguous in real life
893          */
894         i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
895                           I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
896
897         /* enable VF vplan_qtable mappings */
898         reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
899         wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
900
901         i40e_map_pf_to_vf_queues(vf);
902         i40e_map_pf_queues_to_vsi(vf);
903
904         i40e_flush(hw);
905 }
906
907 /**
908  * i40e_disable_vf_mappings
909  * @vf: pointer to the VF info
910  *
911  * disable VF mappings
912  **/
913 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
914 {
915         struct i40e_pf *pf = vf->pf;
916         struct i40e_hw *hw = &pf->hw;
917         int i;
918
919         /* disable qp mappings */
920         wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
921         for (i = 0; i < I40E_MAX_VSI_QP; i++)
922                 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
923                      I40E_QUEUE_END_OF_LIST);
924         i40e_flush(hw);
925 }
926
927 /**
928  * i40e_free_vf_res
929  * @vf: pointer to the VF info
930  *
931  * free VF resources
932  **/
933 static void i40e_free_vf_res(struct i40e_vf *vf)
934 {
935         struct i40e_pf *pf = vf->pf;
936         struct i40e_hw *hw = &pf->hw;
937         u32 reg_idx, reg;
938         int i, j, msix_vf;
939
940         /* Start by disabling VF's configuration API to prevent the OS from
941          * accessing the VF's VSI after it's freed / invalidated.
942          */
943         clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
944
945         /* It's possible the VF had requeuested more queues than the default so
946          * do the accounting here when we're about to free them.
947          */
948         if (vf->num_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF) {
949                 pf->queues_left += vf->num_queue_pairs -
950                                    I40E_DEFAULT_QUEUES_PER_VF;
951         }
952
953         /* free vsi & disconnect it from the parent uplink */
954         if (vf->lan_vsi_idx) {
955                 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
956                 vf->lan_vsi_idx = 0;
957                 vf->lan_vsi_id = 0;
958                 vf->num_mac = 0;
959         }
960
961         /* do the accounting and remove additional ADq VSI's */
962         if (vf->adq_enabled && vf->ch[0].vsi_idx) {
963                 for (j = 0; j < vf->num_tc; j++) {
964                         /* At this point VSI0 is already released so don't
965                          * release it again and only clear their values in
966                          * structure variables
967                          */
968                         if (j)
969                                 i40e_vsi_release(pf->vsi[vf->ch[j].vsi_idx]);
970                         vf->ch[j].vsi_idx = 0;
971                         vf->ch[j].vsi_id = 0;
972                 }
973         }
974         msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
975
976         /* disable interrupts so the VF starts in a known state */
977         for (i = 0; i < msix_vf; i++) {
978                 /* format is same for both registers */
979                 if (0 == i)
980                         reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
981                 else
982                         reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
983                                                       (vf->vf_id))
984                                                      + (i - 1));
985                 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
986                 i40e_flush(hw);
987         }
988
989         /* clear the irq settings */
990         for (i = 0; i < msix_vf; i++) {
991                 /* format is same for both registers */
992                 if (0 == i)
993                         reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
994                 else
995                         reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
996                                                       (vf->vf_id))
997                                                      + (i - 1));
998                 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
999                        I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
1000                 wr32(hw, reg_idx, reg);
1001                 i40e_flush(hw);
1002         }
1003         /* reset some of the state variables keeping track of the resources */
1004         vf->num_queue_pairs = 0;
1005         clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
1006         clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
1007 }
1008
1009 /**
1010  * i40e_alloc_vf_res
1011  * @vf: pointer to the VF info
1012  *
1013  * allocate VF resources
1014  **/
1015 static int i40e_alloc_vf_res(struct i40e_vf *vf)
1016 {
1017         struct i40e_pf *pf = vf->pf;
1018         int total_queue_pairs = 0;
1019         int ret, idx;
1020
1021         if (vf->num_req_queues &&
1022             vf->num_req_queues <= pf->queues_left + I40E_DEFAULT_QUEUES_PER_VF)
1023                 pf->num_vf_qps = vf->num_req_queues;
1024         else
1025                 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
1026
1027         /* allocate hw vsi context & associated resources */
1028         ret = i40e_alloc_vsi_res(vf, 0);
1029         if (ret)
1030                 goto error_alloc;
1031         total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
1032
1033         /* allocate additional VSIs based on tc information for ADq */
1034         if (vf->adq_enabled) {
1035                 if (pf->queues_left >=
1036                     (I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF)) {
1037                         /* TC 0 always belongs to VF VSI */
1038                         for (idx = 1; idx < vf->num_tc; idx++) {
1039                                 ret = i40e_alloc_vsi_res(vf, idx);
1040                                 if (ret)
1041                                         goto error_alloc;
1042                         }
1043                         /* send correct number of queues */
1044                         total_queue_pairs = I40E_MAX_VF_QUEUES;
1045                 } else {
1046                         dev_info(&pf->pdev->dev, "VF %d: Not enough queues to allocate, disabling ADq\n",
1047                                  vf->vf_id);
1048                         vf->adq_enabled = false;
1049                 }
1050         }
1051
1052         /* We account for each VF to get a default number of queue pairs.  If
1053          * the VF has now requested more, we need to account for that to make
1054          * certain we never request more queues than we actually have left in
1055          * HW.
1056          */
1057         if (total_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF)
1058                 pf->queues_left -=
1059                         total_queue_pairs - I40E_DEFAULT_QUEUES_PER_VF;
1060
1061         if (vf->trusted)
1062                 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1063         else
1064                 clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1065
1066         /* store the total qps number for the runtime
1067          * VF req validation
1068          */
1069         vf->num_queue_pairs = total_queue_pairs;
1070
1071         /* VF is now completely initialized */
1072         set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
1073
1074 error_alloc:
1075         if (ret)
1076                 i40e_free_vf_res(vf);
1077
1078         return ret;
1079 }
1080
1081 #define VF_DEVICE_STATUS 0xAA
1082 #define VF_TRANS_PENDING_MASK 0x20
1083 /**
1084  * i40e_quiesce_vf_pci
1085  * @vf: pointer to the VF structure
1086  *
1087  * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
1088  * if the transactions never clear.
1089  **/
1090 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
1091 {
1092         struct i40e_pf *pf = vf->pf;
1093         struct i40e_hw *hw = &pf->hw;
1094         int vf_abs_id, i;
1095         u32 reg;
1096
1097         vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
1098
1099         wr32(hw, I40E_PF_PCI_CIAA,
1100              VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
1101         for (i = 0; i < 100; i++) {
1102                 reg = rd32(hw, I40E_PF_PCI_CIAD);
1103                 if ((reg & VF_TRANS_PENDING_MASK) == 0)
1104                         return 0;
1105                 udelay(1);
1106         }
1107         return -EIO;
1108 }
1109
1110 /**
1111  * i40e_trigger_vf_reset
1112  * @vf: pointer to the VF structure
1113  * @flr: VFLR was issued or not
1114  *
1115  * Trigger hardware to start a reset for a particular VF. Expects the caller
1116  * to wait the proper amount of time to allow hardware to reset the VF before
1117  * it cleans up and restores VF functionality.
1118  **/
1119 static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
1120 {
1121         struct i40e_pf *pf = vf->pf;
1122         struct i40e_hw *hw = &pf->hw;
1123         u32 reg, reg_idx, bit_idx;
1124
1125         /* warn the VF */
1126         clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1127
1128         /* Disable VF's configuration API during reset. The flag is re-enabled
1129          * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
1130          * It's normally disabled in i40e_free_vf_res(), but it's safer
1131          * to do it earlier to give some time to finish to any VF config
1132          * functions that may still be running at this point.
1133          */
1134         clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
1135
1136         /* In the case of a VFLR, the HW has already reset the VF and we
1137          * just need to clean up, so don't hit the VFRTRIG register.
1138          */
1139         if (!flr) {
1140                 /* reset VF using VPGEN_VFRTRIG reg */
1141                 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1142                 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1143                 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1144                 i40e_flush(hw);
1145         }
1146         /* clear the VFLR bit in GLGEN_VFLRSTAT */
1147         reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
1148         bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
1149         wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1150         i40e_flush(hw);
1151
1152         if (i40e_quiesce_vf_pci(vf))
1153                 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
1154                         vf->vf_id);
1155 }
1156
1157 /**
1158  * i40e_cleanup_reset_vf
1159  * @vf: pointer to the VF structure
1160  *
1161  * Cleanup a VF after the hardware reset is finished. Expects the caller to
1162  * have verified whether the reset is finished properly, and ensure the
1163  * minimum amount of wait time has passed.
1164  **/
1165 static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
1166 {
1167         struct i40e_pf *pf = vf->pf;
1168         struct i40e_hw *hw = &pf->hw;
1169         u32 reg;
1170
1171         /* free VF resources to begin resetting the VSI state */
1172         i40e_free_vf_res(vf);
1173
1174         /* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
1175          * By doing this we allow HW to access VF memory at any point. If we
1176          * did it any sooner, HW could access memory while it was being freed
1177          * in i40e_free_vf_res(), causing an IOMMU fault.
1178          *
1179          * On the other hand, this needs to be done ASAP, because the VF driver
1180          * is waiting for this to happen and may report a timeout. It's
1181          * harmless, but it gets logged into Guest OS kernel log, so best avoid
1182          * it.
1183          */
1184         reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1185         reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1186         wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1187
1188         /* reallocate VF resources to finish resetting the VSI state */
1189         if (!i40e_alloc_vf_res(vf)) {
1190                 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1191                 i40e_enable_vf_mappings(vf);
1192                 set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1193                 clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1194                 /* Do not notify the client during VF init */
1195                 if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
1196                                         &vf->vf_states))
1197                         i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1198                 vf->num_vlan = 0;
1199         }
1200
1201         /* Tell the VF driver the reset is done. This needs to be done only
1202          * after VF has been fully initialized, because the VF driver may
1203          * request resources immediately after setting this flag.
1204          */
1205         wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1206 }
1207
1208 /**
1209  * i40e_reset_vf
1210  * @vf: pointer to the VF structure
1211  * @flr: VFLR was issued or not
1212  *
1213  * Returns true if the VF is in reset, resets successfully, or resets
1214  * are disabled and false otherwise.
1215  **/
1216 bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
1217 {
1218         struct i40e_pf *pf = vf->pf;
1219         struct i40e_hw *hw = &pf->hw;
1220         bool rsd = false;
1221         u32 reg;
1222         int i;
1223
1224         if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state))
1225                 return true;
1226
1227         /* If the VFs have been disabled, this means something else is
1228          * resetting the VF, so we shouldn't continue.
1229          */
1230         if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1231                 return true;
1232
1233         i40e_trigger_vf_reset(vf, flr);
1234
1235         /* poll VPGEN_VFRSTAT reg to make sure
1236          * that reset is complete
1237          */
1238         for (i = 0; i < 10; i++) {
1239                 /* VF reset requires driver to first reset the VF and then
1240                  * poll the status register to make sure that the reset
1241                  * completed successfully. Due to internal HW FIFO flushes,
1242                  * we must wait 10ms before the register will be valid.
1243                  */
1244                 usleep_range(10000, 20000);
1245                 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1246                 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
1247                         rsd = true;
1248                         break;
1249                 }
1250         }
1251
1252         if (flr)
1253                 usleep_range(10000, 20000);
1254
1255         if (!rsd)
1256                 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1257                         vf->vf_id);
1258         usleep_range(10000, 20000);
1259
1260         /* On initial reset, we don't have any queues to disable */
1261         if (vf->lan_vsi_idx != 0)
1262                 i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1263
1264         i40e_cleanup_reset_vf(vf);
1265
1266         i40e_flush(hw);
1267         clear_bit(__I40E_VF_DISABLE, pf->state);
1268
1269         return true;
1270 }
1271
1272 /**
1273  * i40e_reset_all_vfs
1274  * @pf: pointer to the PF structure
1275  * @flr: VFLR was issued or not
1276  *
1277  * Reset all allocated VFs in one go. First, tell the hardware to reset each
1278  * VF, then do all the waiting in one chunk, and finally finish restoring each
1279  * VF after the wait. This is useful during PF routines which need to reset
1280  * all VFs, as otherwise it must perform these resets in a serialized fashion.
1281  *
1282  * Returns true if any VFs were reset, and false otherwise.
1283  **/
1284 bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
1285 {
1286         struct i40e_hw *hw = &pf->hw;
1287         struct i40e_vf *vf;
1288         int i, v;
1289         u32 reg;
1290
1291         /* If we don't have any VFs, then there is nothing to reset */
1292         if (!pf->num_alloc_vfs)
1293                 return false;
1294
1295         /* If VFs have been disabled, there is no need to reset */
1296         if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1297                 return false;
1298
1299         /* Begin reset on all VFs at once */
1300         for (v = 0; v < pf->num_alloc_vfs; v++)
1301                 i40e_trigger_vf_reset(&pf->vf[v], flr);
1302
1303         /* HW requires some time to make sure it can flush the FIFO for a VF
1304          * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1305          * sequence to make sure that it has completed. We'll keep track of
1306          * the VFs using a simple iterator that increments once that VF has
1307          * finished resetting.
1308          */
1309         for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1310                 usleep_range(10000, 20000);
1311
1312                 /* Check each VF in sequence, beginning with the VF to fail
1313                  * the previous check.
1314                  */
1315                 while (v < pf->num_alloc_vfs) {
1316                         vf = &pf->vf[v];
1317                         reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1318                         if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
1319                                 break;
1320
1321                         /* If the current VF has finished resetting, move on
1322                          * to the next VF in sequence.
1323                          */
1324                         v++;
1325                 }
1326         }
1327
1328         if (flr)
1329                 usleep_range(10000, 20000);
1330
1331         /* Display a warning if at least one VF didn't manage to reset in
1332          * time, but continue on with the operation.
1333          */
1334         if (v < pf->num_alloc_vfs)
1335                 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1336                         pf->vf[v].vf_id);
1337         usleep_range(10000, 20000);
1338
1339         /* Begin disabling all the rings associated with VFs, but do not wait
1340          * between each VF.
1341          */
1342         for (v = 0; v < pf->num_alloc_vfs; v++) {
1343                 /* On initial reset, we don't have any queues to disable */
1344                 if (pf->vf[v].lan_vsi_idx == 0)
1345                         continue;
1346
1347                 i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
1348         }
1349
1350         /* Now that we've notified HW to disable all of the VF rings, wait
1351          * until they finish.
1352          */
1353         for (v = 0; v < pf->num_alloc_vfs; v++) {
1354                 /* On initial reset, we don't have any queues to disable */
1355                 if (pf->vf[v].lan_vsi_idx == 0)
1356                         continue;
1357
1358                 i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
1359         }
1360
1361         /* Hw may need up to 50ms to finish disabling the RX queues. We
1362          * minimize the wait by delaying only once for all VFs.
1363          */
1364         mdelay(50);
1365
1366         /* Finish the reset on each VF */
1367         for (v = 0; v < pf->num_alloc_vfs; v++)
1368                 i40e_cleanup_reset_vf(&pf->vf[v]);
1369
1370         i40e_flush(hw);
1371         clear_bit(__I40E_VF_DISABLE, pf->state);
1372
1373         return true;
1374 }
1375
1376 /**
1377  * i40e_free_vfs
1378  * @pf: pointer to the PF structure
1379  *
1380  * free VF resources
1381  **/
1382 void i40e_free_vfs(struct i40e_pf *pf)
1383 {
1384         struct i40e_hw *hw = &pf->hw;
1385         u32 reg_idx, bit_idx;
1386         int i, tmp, vf_id;
1387
1388         if (!pf->vf)
1389                 return;
1390
1391         set_bit(__I40E_VFS_RELEASING, pf->state);
1392         while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1393                 usleep_range(1000, 2000);
1394
1395         i40e_notify_client_of_vf_enable(pf, 0);
1396
1397         /* Disable IOV before freeing resources. This lets any VF drivers
1398          * running in the host get themselves cleaned up before we yank
1399          * the carpet out from underneath their feet.
1400          */
1401         if (!pci_vfs_assigned(pf->pdev))
1402                 pci_disable_sriov(pf->pdev);
1403         else
1404                 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1405
1406         /* Amortize wait time by stopping all VFs at the same time */
1407         for (i = 0; i < pf->num_alloc_vfs; i++) {
1408                 if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1409                         continue;
1410
1411                 i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
1412         }
1413
1414         for (i = 0; i < pf->num_alloc_vfs; i++) {
1415                 if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1416                         continue;
1417
1418                 i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
1419         }
1420
1421         /* free up VF resources */
1422         tmp = pf->num_alloc_vfs;
1423         pf->num_alloc_vfs = 0;
1424         for (i = 0; i < tmp; i++) {
1425                 if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1426                         i40e_free_vf_res(&pf->vf[i]);
1427                 /* disable qp mappings */
1428                 i40e_disable_vf_mappings(&pf->vf[i]);
1429         }
1430
1431         kfree(pf->vf);
1432         pf->vf = NULL;
1433
1434         /* This check is for when the driver is unloaded while VFs are
1435          * assigned. Setting the number of VFs to 0 through sysfs is caught
1436          * before this function ever gets called.
1437          */
1438         if (!pci_vfs_assigned(pf->pdev)) {
1439                 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
1440                  * work correctly when SR-IOV gets re-enabled.
1441                  */
1442                 for (vf_id = 0; vf_id < tmp; vf_id++) {
1443                         reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1444                         bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1445                         wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1446                 }
1447         }
1448         clear_bit(__I40E_VF_DISABLE, pf->state);
1449         clear_bit(__I40E_VFS_RELEASING, pf->state);
1450 }
1451
1452 #ifdef CONFIG_PCI_IOV
1453 /**
1454  * i40e_alloc_vfs
1455  * @pf: pointer to the PF structure
1456  * @num_alloc_vfs: number of VFs to allocate
1457  *
1458  * allocate VF resources
1459  **/
1460 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1461 {
1462         struct i40e_vf *vfs;
1463         int i, ret = 0;
1464
1465         /* Disable interrupt 0 so we don't try to handle the VFLR. */
1466         i40e_irq_dynamic_disable_icr0(pf);
1467
1468         /* Check to see if we're just allocating resources for extant VFs */
1469         if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
1470                 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1471                 if (ret) {
1472                         pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1473                         pf->num_alloc_vfs = 0;
1474                         goto err_iov;
1475                 }
1476         }
1477         /* allocate memory */
1478         vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1479         if (!vfs) {
1480                 ret = -ENOMEM;
1481                 goto err_alloc;
1482         }
1483         pf->vf = vfs;
1484
1485         /* apply default profile */
1486         for (i = 0; i < num_alloc_vfs; i++) {
1487                 vfs[i].pf = pf;
1488                 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
1489                 vfs[i].vf_id = i;
1490
1491                 /* assign default capabilities */
1492                 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1493                 vfs[i].spoofchk = true;
1494
1495                 set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
1496
1497         }
1498         pf->num_alloc_vfs = num_alloc_vfs;
1499
1500         /* VF resources get allocated during reset */
1501         i40e_reset_all_vfs(pf, false);
1502
1503         i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
1504
1505 err_alloc:
1506         if (ret)
1507                 i40e_free_vfs(pf);
1508 err_iov:
1509         /* Re-enable interrupt 0. */
1510         i40e_irq_dynamic_enable_icr0(pf);
1511         return ret;
1512 }
1513
1514 #endif
1515 /**
1516  * i40e_pci_sriov_enable
1517  * @pdev: pointer to a pci_dev structure
1518  * @num_vfs: number of VFs to allocate
1519  *
1520  * Enable or change the number of VFs
1521  **/
1522 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1523 {
1524 #ifdef CONFIG_PCI_IOV
1525         struct i40e_pf *pf = pci_get_drvdata(pdev);
1526         int pre_existing_vfs = pci_num_vf(pdev);
1527         int err = 0;
1528
1529         if (test_bit(__I40E_TESTING, pf->state)) {
1530                 dev_warn(&pdev->dev,
1531                          "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1532                 err = -EPERM;
1533                 goto err_out;
1534         }
1535
1536         if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1537                 i40e_free_vfs(pf);
1538         else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1539                 goto out;
1540
1541         if (num_vfs > pf->num_req_vfs) {
1542                 dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1543                          num_vfs, pf->num_req_vfs);
1544                 err = -EPERM;
1545                 goto err_out;
1546         }
1547
1548         dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1549         err = i40e_alloc_vfs(pf, num_vfs);
1550         if (err) {
1551                 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1552                 goto err_out;
1553         }
1554
1555 out:
1556         return num_vfs;
1557
1558 err_out:
1559         return err;
1560 #endif
1561         return 0;
1562 }
1563
1564 /**
1565  * i40e_pci_sriov_configure
1566  * @pdev: pointer to a pci_dev structure
1567  * @num_vfs: number of VFs to allocate
1568  *
1569  * Enable or change the number of VFs. Called when the user updates the number
1570  * of VFs in sysfs.
1571  **/
1572 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1573 {
1574         struct i40e_pf *pf = pci_get_drvdata(pdev);
1575
1576         if (num_vfs) {
1577                 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1578                         pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1579                         i40e_do_reset_safe(pf, I40E_PF_RESET_AND_REBUILD_FLAG);
1580                 }
1581                 return i40e_pci_sriov_enable(pdev, num_vfs);
1582         }
1583
1584         if (!pci_vfs_assigned(pf->pdev)) {
1585                 i40e_free_vfs(pf);
1586                 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1587                 i40e_do_reset_safe(pf, I40E_PF_RESET_AND_REBUILD_FLAG);
1588         } else {
1589                 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1590                 return -EINVAL;
1591         }
1592         return 0;
1593 }
1594
1595 /***********************virtual channel routines******************/
1596
1597 /**
1598  * i40e_vc_send_msg_to_vf
1599  * @vf: pointer to the VF info
1600  * @v_opcode: virtual channel opcode
1601  * @v_retval: virtual channel return value
1602  * @msg: pointer to the msg buffer
1603  * @msglen: msg length
1604  *
1605  * send msg to VF
1606  **/
1607 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1608                                   u32 v_retval, u8 *msg, u16 msglen)
1609 {
1610         struct i40e_pf *pf;
1611         struct i40e_hw *hw;
1612         int abs_vf_id;
1613         i40e_status aq_ret;
1614
1615         /* validate the request */
1616         if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1617                 return -EINVAL;
1618
1619         pf = vf->pf;
1620         hw = &pf->hw;
1621         abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1622
1623         /* single place to detect unsuccessful return values */
1624         if (v_retval) {
1625                 vf->num_invalid_msgs++;
1626                 dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1627                          vf->vf_id, v_opcode, v_retval);
1628                 if (vf->num_invalid_msgs >
1629                     I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1630                         dev_err(&pf->pdev->dev,
1631                                 "Number of invalid messages exceeded for VF %d\n",
1632                                 vf->vf_id);
1633                         dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1634                         set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1635                 }
1636         } else {
1637                 vf->num_valid_msgs++;
1638                 /* reset the invalid counter, if a valid message is received. */
1639                 vf->num_invalid_msgs = 0;
1640         }
1641
1642         aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,  v_opcode, v_retval,
1643                                         msg, msglen, NULL);
1644         if (aq_ret) {
1645                 dev_info(&pf->pdev->dev,
1646                          "Unable to send the message to VF %d aq_err %d\n",
1647                          vf->vf_id, pf->hw.aq.asq_last_status);
1648                 return -EIO;
1649         }
1650
1651         return 0;
1652 }
1653
1654 /**
1655  * i40e_vc_send_resp_to_vf
1656  * @vf: pointer to the VF info
1657  * @opcode: operation code
1658  * @retval: return value
1659  *
1660  * send resp msg to VF
1661  **/
1662 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1663                                    enum virtchnl_ops opcode,
1664                                    i40e_status retval)
1665 {
1666         return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1667 }
1668
1669 /**
1670  * i40e_vc_get_version_msg
1671  * @vf: pointer to the VF info
1672  * @msg: pointer to the msg buffer
1673  *
1674  * called from the VF to request the API version used by the PF
1675  **/
1676 static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
1677 {
1678         struct virtchnl_version_info info = {
1679                 VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1680         };
1681
1682         vf->vf_ver = *(struct virtchnl_version_info *)msg;
1683         /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1684         if (VF_IS_V10(&vf->vf_ver))
1685                 info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1686         return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1687                                       I40E_SUCCESS, (u8 *)&info,
1688                                       sizeof(struct virtchnl_version_info));
1689 }
1690
1691 /**
1692  * i40e_del_qch - delete all the additional VSIs created as a part of ADq
1693  * @vf: pointer to VF structure
1694  **/
1695 static void i40e_del_qch(struct i40e_vf *vf)
1696 {
1697         struct i40e_pf *pf = vf->pf;
1698         int i;
1699
1700         /* first element in the array belongs to primary VF VSI and we shouldn't
1701          * delete it. We should however delete the rest of the VSIs created
1702          */
1703         for (i = 1; i < vf->num_tc; i++) {
1704                 if (vf->ch[i].vsi_idx) {
1705                         i40e_vsi_release(pf->vsi[vf->ch[i].vsi_idx]);
1706                         vf->ch[i].vsi_idx = 0;
1707                         vf->ch[i].vsi_id = 0;
1708                 }
1709         }
1710 }
1711
1712 /**
1713  * i40e_vc_get_vf_resources_msg
1714  * @vf: pointer to the VF info
1715  * @msg: pointer to the msg buffer
1716  *
1717  * called from the VF to request its resources
1718  **/
1719 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
1720 {
1721         struct virtchnl_vf_resource *vfres = NULL;
1722         struct i40e_pf *pf = vf->pf;
1723         i40e_status aq_ret = 0;
1724         struct i40e_vsi *vsi;
1725         int num_vsis = 1;
1726         int len = 0;
1727         int ret;
1728
1729         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
1730                 aq_ret = I40E_ERR_PARAM;
1731                 goto err;
1732         }
1733
1734         len = (sizeof(struct virtchnl_vf_resource) +
1735                sizeof(struct virtchnl_vsi_resource) * num_vsis);
1736
1737         vfres = kzalloc(len, GFP_KERNEL);
1738         if (!vfres) {
1739                 aq_ret = I40E_ERR_NO_MEMORY;
1740                 len = 0;
1741                 goto err;
1742         }
1743         if (VF_IS_V11(&vf->vf_ver))
1744                 vf->driver_caps = *(u32 *)msg;
1745         else
1746                 vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1747                                   VIRTCHNL_VF_OFFLOAD_RSS_REG |
1748                                   VIRTCHNL_VF_OFFLOAD_VLAN;
1749
1750         vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1751         vsi = pf->vsi[vf->lan_vsi_idx];
1752         if (!vsi->info.pvid)
1753                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1754
1755         if (i40e_vf_client_capable(pf, vf->vf_id) &&
1756             (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
1757                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
1758                 set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1759         } else {
1760                 clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1761         }
1762
1763         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1764                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1765         } else {
1766                 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
1767                     (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
1768                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1769                 else
1770                         vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1771         }
1772
1773         if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
1774                 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1775                         vfres->vf_cap_flags |=
1776                                 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1777         }
1778
1779         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1780                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1781
1782         if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
1783             (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
1784                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1785
1786         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
1787                 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
1788                         dev_err(&pf->pdev->dev,
1789                                 "VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
1790                                  vf->vf_id);
1791                         aq_ret = I40E_ERR_PARAM;
1792                         goto err;
1793                 }
1794                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1795         }
1796
1797         if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
1798                 if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1799                         vfres->vf_cap_flags |=
1800                                         VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1801         }
1802
1803         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1804                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1805
1806         if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)
1807                 vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADQ;
1808
1809         vfres->num_vsis = num_vsis;
1810         vfres->num_queue_pairs = vf->num_queue_pairs;
1811         vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1812         vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
1813         vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
1814
1815         if (vf->lan_vsi_idx) {
1816                 vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
1817                 vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1818                 vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
1819                 /* VFs only use TC 0 */
1820                 vfres->vsi_res[0].qset_handle
1821                                           = le16_to_cpu(vsi->info.qs_handle[0]);
1822                 ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1823                                 vf->default_lan_addr.addr);
1824         }
1825         set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1826
1827 err:
1828         /* send the response back to the VF */
1829         ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
1830                                      aq_ret, (u8 *)vfres, len);
1831
1832         kfree(vfres);
1833         return ret;
1834 }
1835
1836 /**
1837  * i40e_vc_reset_vf_msg
1838  * @vf: pointer to the VF info
1839  *
1840  * called from the VF to reset itself,
1841  * unlike other virtchnl messages, PF driver
1842  * doesn't send the response back to the VF
1843  **/
1844 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1845 {
1846         if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
1847                 i40e_reset_vf(vf, false);
1848 }
1849
1850 /**
1851  * i40e_getnum_vf_vsi_vlan_filters
1852  * @vsi: pointer to the vsi
1853  *
1854  * called to get the number of VLANs offloaded on this VF
1855  **/
1856 static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
1857 {
1858         struct i40e_mac_filter *f;
1859         int num_vlans = 0, bkt;
1860
1861         hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1862                 if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
1863                         num_vlans++;
1864         }
1865
1866         return num_vlans;
1867 }
1868
1869 /**
1870  * i40e_vc_config_promiscuous_mode_msg
1871  * @vf: pointer to the VF info
1872  * @msg: pointer to the msg buffer
1873  * @msglen: msg length
1874  *
1875  * called from the VF to configure the promiscuous mode of
1876  * VF vsis
1877  **/
1878 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1879                                                u8 *msg, u16 msglen)
1880 {
1881         struct virtchnl_promisc_info *info =
1882             (struct virtchnl_promisc_info *)msg;
1883         struct i40e_pf *pf = vf->pf;
1884         struct i40e_hw *hw = &pf->hw;
1885         struct i40e_mac_filter *f;
1886         i40e_status aq_ret = 0;
1887         bool allmulti = false;
1888         struct i40e_vsi *vsi;
1889         bool alluni = false;
1890         int aq_err = 0;
1891         int bkt;
1892
1893         vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
1894         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
1895             !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1896             !vsi) {
1897                 aq_ret = I40E_ERR_PARAM;
1898                 goto error_param;
1899         }
1900         if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
1901                 dev_err(&pf->pdev->dev,
1902                         "Unprivileged VF %d is attempting to configure promiscuous mode\n",
1903                         vf->vf_id);
1904                 /* Lie to the VF on purpose. */
1905                 aq_ret = 0;
1906                 goto error_param;
1907         }
1908         /* Multicast promiscuous handling*/
1909         if (info->flags & FLAG_VF_MULTICAST_PROMISC)
1910                 allmulti = true;
1911
1912         if (vf->port_vlan_id) {
1913                 aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
1914                                                             allmulti,
1915                                                             vf->port_vlan_id,
1916                                                             NULL);
1917         } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1918                 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1919                         if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1920                                 continue;
1921                         aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
1922                                                                     vsi->seid,
1923                                                                     allmulti,
1924                                                                     f->vlan,
1925                                                                     NULL);
1926                         aq_err = pf->hw.aq.asq_last_status;
1927                         if (aq_ret) {
1928                                 dev_err(&pf->pdev->dev,
1929                                         "Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
1930                                         f->vlan,
1931                                         i40e_stat_str(&pf->hw, aq_ret),
1932                                         i40e_aq_str(&pf->hw, aq_err));
1933                                 break;
1934                         }
1935                 }
1936         } else {
1937                 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
1938                                                                allmulti, NULL);
1939                 aq_err = pf->hw.aq.asq_last_status;
1940                 if (aq_ret) {
1941                         dev_err(&pf->pdev->dev,
1942                                 "VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1943                                 vf->vf_id,
1944                                 i40e_stat_str(&pf->hw, aq_ret),
1945                                 i40e_aq_str(&pf->hw, aq_err));
1946                         goto error_param;
1947                 }
1948         }
1949
1950         if (!aq_ret) {
1951                 dev_info(&pf->pdev->dev,
1952                          "VF %d successfully set multicast promiscuous mode\n",
1953                          vf->vf_id);
1954                 if (allmulti)
1955                         set_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
1956                 else
1957                         clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
1958         }
1959
1960         if (info->flags & FLAG_VF_UNICAST_PROMISC)
1961                 alluni = true;
1962         if (vf->port_vlan_id) {
1963                 aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
1964                                                             alluni,
1965                                                             vf->port_vlan_id,
1966                                                             NULL);
1967         } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1968                 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1969                         if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1970                                 continue;
1971                         aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
1972                                                                     vsi->seid,
1973                                                                     alluni,
1974                                                                     f->vlan,
1975                                                                     NULL);
1976                         aq_err = pf->hw.aq.asq_last_status;
1977                         if (aq_ret)
1978                                 dev_err(&pf->pdev->dev,
1979                                         "Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
1980                                         f->vlan,
1981                                         i40e_stat_str(&pf->hw, aq_ret),
1982                                         i40e_aq_str(&pf->hw, aq_err));
1983                 }
1984         } else {
1985                 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
1986                                                              alluni, NULL,
1987                                                              true);
1988                 aq_err = pf->hw.aq.asq_last_status;
1989                 if (aq_ret) {
1990                         dev_err(&pf->pdev->dev,
1991                                 "VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
1992                                 vf->vf_id, info->flags,
1993                                 i40e_stat_str(&pf->hw, aq_ret),
1994                                 i40e_aq_str(&pf->hw, aq_err));
1995                         goto error_param;
1996                 }
1997         }
1998
1999         if (!aq_ret) {
2000                 dev_info(&pf->pdev->dev,
2001                          "VF %d successfully set unicast promiscuous mode\n",
2002                          vf->vf_id);
2003                 if (alluni)
2004                         set_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
2005                 else
2006                         clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
2007         }
2008
2009 error_param:
2010         /* send the response to the VF */
2011         return i40e_vc_send_resp_to_vf(vf,
2012                                        VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
2013                                        aq_ret);
2014 }
2015
2016 /**
2017  * i40e_vc_config_queues_msg
2018  * @vf: pointer to the VF info
2019  * @msg: pointer to the msg buffer
2020  * @msglen: msg length
2021  *
2022  * called from the VF to configure the rx/tx
2023  * queues
2024  **/
2025 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2026 {
2027         struct virtchnl_vsi_queue_config_info *qci =
2028             (struct virtchnl_vsi_queue_config_info *)msg;
2029         struct virtchnl_queue_pair_info *qpi;
2030         struct i40e_pf *pf = vf->pf;
2031         u16 vsi_id, vsi_queue_id = 0;
2032         i40e_status aq_ret = 0;
2033         int i, j = 0, idx = 0;
2034
2035         vsi_id = qci->vsi_id;
2036
2037         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2038                 aq_ret = I40E_ERR_PARAM;
2039                 goto error_param;
2040         }
2041
2042         if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2043                 aq_ret = I40E_ERR_PARAM;
2044                 goto error_param;
2045         }
2046
2047         for (i = 0; i < qci->num_queue_pairs; i++) {
2048                 qpi = &qci->qpair[i];
2049
2050                 if (!vf->adq_enabled) {
2051                         vsi_queue_id = qpi->txq.queue_id;
2052
2053                         if (qpi->txq.vsi_id != qci->vsi_id ||
2054                             qpi->rxq.vsi_id != qci->vsi_id ||
2055                             qpi->rxq.queue_id != vsi_queue_id) {
2056                                 aq_ret = I40E_ERR_PARAM;
2057                                 goto error_param;
2058                         }
2059                 }
2060
2061                 if (!i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
2062                         aq_ret = I40E_ERR_PARAM;
2063                         goto error_param;
2064                 }
2065
2066                 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
2067                                              &qpi->rxq) ||
2068                     i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
2069                                              &qpi->txq)) {
2070                         aq_ret = I40E_ERR_PARAM;
2071                         goto error_param;
2072                 }
2073
2074                 /* For ADq there can be up to 4 VSIs with max 4 queues each.
2075                  * VF does not know about these additional VSIs and all
2076                  * it cares is about its own queues. PF configures these queues
2077                  * to its appropriate VSIs based on TC mapping
2078                  **/
2079                 if (vf->adq_enabled) {
2080                         if (j == (vf->ch[idx].num_qps - 1)) {
2081                                 idx++;
2082                                 j = 0; /* resetting the queue count */
2083                                 vsi_queue_id = 0;
2084                         } else {
2085                                 j++;
2086                                 vsi_queue_id++;
2087                         }
2088                         vsi_id = vf->ch[idx].vsi_id;
2089                 }
2090         }
2091         /* set vsi num_queue_pairs in use to num configured by VF */
2092         if (!vf->adq_enabled) {
2093                 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs =
2094                         qci->num_queue_pairs;
2095         } else {
2096                 for (i = 0; i < vf->num_tc; i++)
2097                         pf->vsi[vf->ch[i].vsi_idx]->num_queue_pairs =
2098                                vf->ch[i].num_qps;
2099         }
2100
2101 error_param:
2102         /* send the response to the VF */
2103         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2104                                        aq_ret);
2105 }
2106
2107 /**
2108  * i40e_validate_queue_map
2109  * @vsi_id: vsi id
2110  * @queuemap: Tx or Rx queue map
2111  *
2112  * check if Tx or Rx queue map is valid
2113  **/
2114 static int i40e_validate_queue_map(struct i40e_vf *vf, u16 vsi_id,
2115                                    unsigned long queuemap)
2116 {
2117         u16 vsi_queue_id, queue_id;
2118
2119         for_each_set_bit(vsi_queue_id, &queuemap, I40E_MAX_VSI_QP) {
2120                 if (vf->adq_enabled) {
2121                         vsi_id = vf->ch[vsi_queue_id / I40E_MAX_VF_VSI].vsi_id;
2122                         queue_id = (vsi_queue_id % I40E_DEFAULT_QUEUES_PER_VF);
2123                 } else {
2124                         queue_id = vsi_queue_id;
2125                 }
2126
2127                 if (!i40e_vc_isvalid_queue_id(vf, vsi_id, queue_id))
2128                         return -EINVAL;
2129         }
2130
2131         return 0;
2132 }
2133
2134 /**
2135  * i40e_vc_config_irq_map_msg
2136  * @vf: pointer to the VF info
2137  * @msg: pointer to the msg buffer
2138  * @msglen: msg length
2139  *
2140  * called from the VF to configure the irq to
2141  * queue map
2142  **/
2143 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2144 {
2145         struct virtchnl_irq_map_info *irqmap_info =
2146             (struct virtchnl_irq_map_info *)msg;
2147         struct virtchnl_vector_map *map;
2148         u16 vsi_id, vector_id;
2149         i40e_status aq_ret = 0;
2150         int i;
2151
2152         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2153                 aq_ret = I40E_ERR_PARAM;
2154                 goto error_param;
2155         }
2156
2157         for (i = 0; i < irqmap_info->num_vectors; i++) {
2158                 map = &irqmap_info->vecmap[i];
2159                 vector_id = map->vector_id;
2160                 vsi_id = map->vsi_id;
2161                 /* validate msg params */
2162                 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
2163                     !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2164                         aq_ret = I40E_ERR_PARAM;
2165                         goto error_param;
2166                 }
2167
2168                 if (i40e_validate_queue_map(vf, vsi_id, map->rxq_map)) {
2169                         aq_ret = I40E_ERR_PARAM;
2170                         goto error_param;
2171                 }
2172
2173                 if (i40e_validate_queue_map(vf, vsi_id, map->txq_map)) {
2174                         aq_ret = I40E_ERR_PARAM;
2175                         goto error_param;
2176                 }
2177
2178                 i40e_config_irq_link_list(vf, vsi_id, map);
2179         }
2180 error_param:
2181         /* send the response to the VF */
2182         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
2183                                        aq_ret);
2184 }
2185
2186 /**
2187  * i40e_ctrl_vf_tx_rings
2188  * @vsi: the SRIOV VSI being configured
2189  * @q_map: bit map of the queues to be enabled
2190  * @enable: start or stop the queue
2191  **/
2192 static int i40e_ctrl_vf_tx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2193                                  bool enable)
2194 {
2195         struct i40e_pf *pf = vsi->back;
2196         int ret = 0;
2197         u16 q_id;
2198
2199         for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2200                 ret = i40e_control_wait_tx_q(vsi->seid, pf,
2201                                              vsi->base_queue + q_id,
2202                                              false /*is xdp*/, enable);
2203                 if (ret)
2204                         break;
2205         }
2206         return ret;
2207 }
2208
2209 /**
2210  * i40e_ctrl_vf_rx_rings
2211  * @vsi: the SRIOV VSI being configured
2212  * @q_map: bit map of the queues to be enabled
2213  * @enable: start or stop the queue
2214  **/
2215 static int i40e_ctrl_vf_rx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2216                                  bool enable)
2217 {
2218         struct i40e_pf *pf = vsi->back;
2219         int ret = 0;
2220         u16 q_id;
2221
2222         for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2223                 ret = i40e_control_wait_rx_q(pf, vsi->base_queue + q_id,
2224                                              enable);
2225                 if (ret)
2226                         break;
2227         }
2228         return ret;
2229 }
2230
2231 /**
2232  * i40e_vc_enable_queues_msg
2233  * @vf: pointer to the VF info
2234  * @msg: pointer to the msg buffer
2235  * @msglen: msg length
2236  *
2237  * called from the VF to enable all or specific queue(s)
2238  **/
2239 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2240 {
2241         struct virtchnl_queue_select *vqs =
2242             (struct virtchnl_queue_select *)msg;
2243         struct i40e_pf *pf = vf->pf;
2244         u16 vsi_id = vqs->vsi_id;
2245         i40e_status aq_ret = 0;
2246         int i;
2247
2248         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2249                 aq_ret = I40E_ERR_PARAM;
2250                 goto error_param;
2251         }
2252
2253         if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2254                 aq_ret = I40E_ERR_PARAM;
2255                 goto error_param;
2256         }
2257
2258         if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
2259                 aq_ret = I40E_ERR_PARAM;
2260                 goto error_param;
2261         }
2262
2263         /* Use the queue bit map sent by the VF */
2264         if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2265                                   true)) {
2266                 aq_ret = I40E_ERR_TIMEOUT;
2267                 goto error_param;
2268         }
2269         if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2270                                   true)) {
2271                 aq_ret = I40E_ERR_TIMEOUT;
2272                 goto error_param;
2273         }
2274
2275         /* need to start the rings for additional ADq VSI's as well */
2276         if (vf->adq_enabled) {
2277                 /* zero belongs to LAN VSI */
2278                 for (i = 1; i < vf->num_tc; i++) {
2279                         if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
2280                                 aq_ret = I40E_ERR_TIMEOUT;
2281                 }
2282         }
2283
2284 error_param:
2285         /* send the response to the VF */
2286         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
2287                                        aq_ret);
2288 }
2289
2290 /**
2291  * i40e_vc_disable_queues_msg
2292  * @vf: pointer to the VF info
2293  * @msg: pointer to the msg buffer
2294  * @msglen: msg length
2295  *
2296  * called from the VF to disable all or specific
2297  * queue(s)
2298  **/
2299 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2300 {
2301         struct virtchnl_queue_select *vqs =
2302             (struct virtchnl_queue_select *)msg;
2303         struct i40e_pf *pf = vf->pf;
2304         i40e_status aq_ret = 0;
2305
2306         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2307                 aq_ret = I40E_ERR_PARAM;
2308                 goto error_param;
2309         }
2310
2311         if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2312                 aq_ret = I40E_ERR_PARAM;
2313                 goto error_param;
2314         }
2315
2316         if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
2317                 aq_ret = I40E_ERR_PARAM;
2318                 goto error_param;
2319         }
2320
2321         /* Use the queue bit map sent by the VF */
2322         if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2323                                   false)) {
2324                 aq_ret = I40E_ERR_TIMEOUT;
2325                 goto error_param;
2326         }
2327         if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2328                                   false)) {
2329                 aq_ret = I40E_ERR_TIMEOUT;
2330                 goto error_param;
2331         }
2332 error_param:
2333         /* send the response to the VF */
2334         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
2335                                        aq_ret);
2336 }
2337
2338 /**
2339  * i40e_vc_request_queues_msg
2340  * @vf: pointer to the VF info
2341  * @msg: pointer to the msg buffer
2342  * @msglen: msg length
2343  *
2344  * VFs get a default number of queues but can use this message to request a
2345  * different number.  If the request is successful, PF will reset the VF and
2346  * return 0.  If unsuccessful, PF will send message informing VF of number of
2347  * available queues and return result of sending VF a message.
2348  **/
2349 static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg, int msglen)
2350 {
2351         struct virtchnl_vf_res_request *vfres =
2352                 (struct virtchnl_vf_res_request *)msg;
2353         int req_pairs = vfres->num_queue_pairs;
2354         int cur_pairs = vf->num_queue_pairs;
2355         struct i40e_pf *pf = vf->pf;
2356
2357         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
2358                 return -EINVAL;
2359
2360         if (req_pairs <= 0) {
2361                 dev_err(&pf->pdev->dev,
2362                         "VF %d tried to request %d queues.  Ignoring.\n",
2363                         vf->vf_id, req_pairs);
2364         } else if (req_pairs > I40E_MAX_VF_QUEUES) {
2365                 dev_err(&pf->pdev->dev,
2366                         "VF %d tried to request more than %d queues.\n",
2367                         vf->vf_id,
2368                         I40E_MAX_VF_QUEUES);
2369                 vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
2370         } else if (req_pairs - cur_pairs > pf->queues_left) {
2371                 dev_warn(&pf->pdev->dev,
2372                          "VF %d requested %d more queues, but only %d left.\n",
2373                          vf->vf_id,
2374                          req_pairs - cur_pairs,
2375                          pf->queues_left);
2376                 vfres->num_queue_pairs = pf->queues_left + cur_pairs;
2377         } else {
2378                 /* successful request */
2379                 vf->num_req_queues = req_pairs;
2380                 i40e_vc_notify_vf_reset(vf);
2381                 i40e_reset_vf(vf, false);
2382                 return 0;
2383         }
2384
2385         return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
2386                                       (u8 *)vfres, sizeof(*vfres));
2387 }
2388
2389 /**
2390  * i40e_vc_get_stats_msg
2391  * @vf: pointer to the VF info
2392  * @msg: pointer to the msg buffer
2393  * @msglen: msg length
2394  *
2395  * called from the VF to get vsi stats
2396  **/
2397 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2398 {
2399         struct virtchnl_queue_select *vqs =
2400             (struct virtchnl_queue_select *)msg;
2401         struct i40e_pf *pf = vf->pf;
2402         struct i40e_eth_stats stats;
2403         i40e_status aq_ret = 0;
2404         struct i40e_vsi *vsi;
2405
2406         memset(&stats, 0, sizeof(struct i40e_eth_stats));
2407
2408         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2409                 aq_ret = I40E_ERR_PARAM;
2410                 goto error_param;
2411         }
2412
2413         if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2414                 aq_ret = I40E_ERR_PARAM;
2415                 goto error_param;
2416         }
2417
2418         vsi = pf->vsi[vf->lan_vsi_idx];
2419         if (!vsi) {
2420                 aq_ret = I40E_ERR_PARAM;
2421                 goto error_param;
2422         }
2423         i40e_update_eth_stats(vsi);
2424         stats = vsi->eth_stats;
2425
2426 error_param:
2427         /* send the response back to the VF */
2428         return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
2429                                       (u8 *)&stats, sizeof(stats));
2430 }
2431
2432 /* If the VF is not trusted restrict the number of MAC/VLAN it can program
2433  * MAC filters: 16 for multicast, 1 for MAC, 1 for broadcast
2434  */
2435 #define I40E_VC_MAX_MAC_ADDR_PER_VF (16 + 1 + 1)
2436 #define I40E_VC_MAX_VLAN_PER_VF 8
2437
2438 /**
2439  * i40e_check_vf_permission
2440  * @vf: pointer to the VF info
2441  * @al: MAC address list from virtchnl
2442  *
2443  * Check that the given list of MAC addresses is allowed. Will return -EPERM
2444  * if any address in the list is not valid. Checks the following conditions:
2445  *
2446  * 1) broadcast and zero addresses are never valid
2447  * 2) unicast addresses are not allowed if the VMM has administratively set
2448  *    the VF MAC address, unless the VF is marked as privileged.
2449  * 3) There is enough space to add all the addresses.
2450  *
2451  * Note that to guarantee consistency, it is expected this function be called
2452  * while holding the mac_filter_hash_lock, as otherwise the current number of
2453  * addresses might not be accurate.
2454  **/
2455 static inline int i40e_check_vf_permission(struct i40e_vf *vf,
2456                                            struct virtchnl_ether_addr_list *al)
2457 {
2458         struct i40e_pf *pf = vf->pf;
2459         int i;
2460
2461         /* If this VF is not privileged, then we can't add more than a limited
2462          * number of addresses. Check to make sure that the additions do not
2463          * push us over the limit.
2464          */
2465         if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2466             (vf->num_mac + al->num_elements) > I40E_VC_MAX_MAC_ADDR_PER_VF) {
2467                 dev_err(&pf->pdev->dev,
2468                         "Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
2469                 return -EPERM;
2470         }
2471
2472         for (i = 0; i < al->num_elements; i++) {
2473                 u8 *addr = al->list[i].addr;
2474
2475                 if (is_broadcast_ether_addr(addr) ||
2476                     is_zero_ether_addr(addr)) {
2477                         dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
2478                                 addr);
2479                         return I40E_ERR_INVALID_MAC_ADDR;
2480                 }
2481
2482                 /* If the host VMM administrator has set the VF MAC address
2483                  * administratively via the ndo_set_vf_mac command then deny
2484                  * permission to the VF to add or delete unicast MAC addresses.
2485                  * Unless the VF is privileged and then it can do whatever.
2486                  * The VF may request to set the MAC address filter already
2487                  * assigned to it so do not return an error in that case.
2488                  */
2489                 if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2490                     !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
2491                     !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
2492                         dev_err(&pf->pdev->dev,
2493                                 "VF attempting to override administratively set MAC address, reload the VF driver to resume normal operation\n");
2494                         return -EPERM;
2495                 }
2496         }
2497
2498         return 0;
2499 }
2500
2501 /**
2502  * i40e_vc_add_mac_addr_msg
2503  * @vf: pointer to the VF info
2504  * @msg: pointer to the msg buffer
2505  * @msglen: msg length
2506  *
2507  * add guest mac address filter
2508  **/
2509 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2510 {
2511         struct virtchnl_ether_addr_list *al =
2512             (struct virtchnl_ether_addr_list *)msg;
2513         struct i40e_pf *pf = vf->pf;
2514         struct i40e_vsi *vsi = NULL;
2515         u16 vsi_id = al->vsi_id;
2516         i40e_status ret = 0;
2517         int i;
2518
2519         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2520             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2521                 ret = I40E_ERR_PARAM;
2522                 goto error_param;
2523         }
2524
2525         vsi = pf->vsi[vf->lan_vsi_idx];
2526
2527         /* Lock once, because all function inside for loop accesses VSI's
2528          * MAC filter list which needs to be protected using same lock.
2529          */
2530         spin_lock_bh(&vsi->mac_filter_hash_lock);
2531
2532         ret = i40e_check_vf_permission(vf, al);
2533         if (ret) {
2534                 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2535                 goto error_param;
2536         }
2537
2538         /* add new addresses to the list */
2539         for (i = 0; i < al->num_elements; i++) {
2540                 struct i40e_mac_filter *f;
2541
2542                 f = i40e_find_mac(vsi, al->list[i].addr);
2543                 if (!f) {
2544                         f = i40e_add_mac_filter(vsi, al->list[i].addr);
2545
2546                         if (!f) {
2547                                 dev_err(&pf->pdev->dev,
2548                                         "Unable to add MAC filter %pM for VF %d\n",
2549                                         al->list[i].addr, vf->vf_id);
2550                                 ret = I40E_ERR_PARAM;
2551                                 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2552                                 goto error_param;
2553                         } else {
2554                                 vf->num_mac++;
2555                         }
2556                 }
2557         }
2558         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2559
2560         /* program the updated filter list */
2561         ret = i40e_sync_vsi_filters(vsi);
2562         if (ret)
2563                 dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2564                         vf->vf_id, ret);
2565
2566 error_param:
2567         /* send the response to the VF */
2568         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
2569                                        ret);
2570 }
2571
2572 /**
2573  * i40e_vc_del_mac_addr_msg
2574  * @vf: pointer to the VF info
2575  * @msg: pointer to the msg buffer
2576  * @msglen: msg length
2577  *
2578  * remove guest mac address filter
2579  **/
2580 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2581 {
2582         struct virtchnl_ether_addr_list *al =
2583             (struct virtchnl_ether_addr_list *)msg;
2584         struct i40e_pf *pf = vf->pf;
2585         struct i40e_vsi *vsi = NULL;
2586         u16 vsi_id = al->vsi_id;
2587         i40e_status ret = 0;
2588         int i;
2589
2590         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2591             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2592                 ret = I40E_ERR_PARAM;
2593                 goto error_param;
2594         }
2595
2596         for (i = 0; i < al->num_elements; i++) {
2597                 if (is_broadcast_ether_addr(al->list[i].addr) ||
2598                     is_zero_ether_addr(al->list[i].addr)) {
2599                         dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
2600                                 al->list[i].addr, vf->vf_id);
2601                         ret = I40E_ERR_INVALID_MAC_ADDR;
2602                         goto error_param;
2603                 }
2604
2605                 if (vf->pf_set_mac &&
2606                     ether_addr_equal(al->list[i].addr,
2607                                      vf->default_lan_addr.addr)) {
2608                         dev_err(&pf->pdev->dev,
2609                                 "MAC addr %pM has been set by PF, cannot delete it for VF %d, reset VF to change MAC addr\n",
2610                                 vf->default_lan_addr.addr, vf->vf_id);
2611                         ret = I40E_ERR_PARAM;
2612                         goto error_param;
2613                 }
2614         }
2615         vsi = pf->vsi[vf->lan_vsi_idx];
2616
2617         spin_lock_bh(&vsi->mac_filter_hash_lock);
2618         /* delete addresses from the list */
2619         for (i = 0; i < al->num_elements; i++)
2620                 if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2621                         ret = I40E_ERR_INVALID_MAC_ADDR;
2622                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2623                         goto error_param;
2624                 } else {
2625                         vf->num_mac--;
2626                 }
2627
2628         spin_unlock_bh(&vsi->mac_filter_hash_lock);
2629
2630         /* program the updated filter list */
2631         ret = i40e_sync_vsi_filters(vsi);
2632         if (ret)
2633                 dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2634                         vf->vf_id, ret);
2635
2636 error_param:
2637         /* send the response to the VF */
2638         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
2639                                        ret);
2640 }
2641
2642 /**
2643  * i40e_vc_add_vlan_msg
2644  * @vf: pointer to the VF info
2645  * @msg: pointer to the msg buffer
2646  * @msglen: msg length
2647  *
2648  * program guest vlan id
2649  **/
2650 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2651 {
2652         struct virtchnl_vlan_filter_list *vfl =
2653             (struct virtchnl_vlan_filter_list *)msg;
2654         struct i40e_pf *pf = vf->pf;
2655         struct i40e_vsi *vsi = NULL;
2656         u16 vsi_id = vfl->vsi_id;
2657         i40e_status aq_ret = 0;
2658         int i;
2659
2660         if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
2661             !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2662                 dev_err(&pf->pdev->dev,
2663                         "VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
2664                 goto error_param;
2665         }
2666         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2667             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2668                 aq_ret = I40E_ERR_PARAM;
2669                 goto error_param;
2670         }
2671
2672         for (i = 0; i < vfl->num_elements; i++) {
2673                 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2674                         aq_ret = I40E_ERR_PARAM;
2675                         dev_err(&pf->pdev->dev,
2676                                 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2677                         goto error_param;
2678                 }
2679         }
2680         vsi = pf->vsi[vf->lan_vsi_idx];
2681         if (vsi->info.pvid) {
2682                 aq_ret = I40E_ERR_PARAM;
2683                 goto error_param;
2684         }
2685
2686         i40e_vlan_stripping_enable(vsi);
2687         for (i = 0; i < vfl->num_elements; i++) {
2688                 /* add new VLAN filter */
2689                 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
2690                 if (!ret)
2691                         vf->num_vlan++;
2692
2693                 if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2694                         i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2695                                                            true,
2696                                                            vfl->vlan_id[i],
2697                                                            NULL);
2698                 if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2699                         i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2700                                                            true,
2701                                                            vfl->vlan_id[i],
2702                                                            NULL);
2703
2704                 if (ret)
2705                         dev_err(&pf->pdev->dev,
2706                                 "Unable to add VLAN filter %d for VF %d, error %d\n",
2707                                 vfl->vlan_id[i], vf->vf_id, ret);
2708         }
2709
2710 error_param:
2711         /* send the response to the VF */
2712         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
2713 }
2714
2715 /**
2716  * i40e_vc_remove_vlan_msg
2717  * @vf: pointer to the VF info
2718  * @msg: pointer to the msg buffer
2719  * @msglen: msg length
2720  *
2721  * remove programmed guest vlan id
2722  **/
2723 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2724 {
2725         struct virtchnl_vlan_filter_list *vfl =
2726             (struct virtchnl_vlan_filter_list *)msg;
2727         struct i40e_pf *pf = vf->pf;
2728         struct i40e_vsi *vsi = NULL;
2729         u16 vsi_id = vfl->vsi_id;
2730         i40e_status aq_ret = 0;
2731         int i;
2732
2733         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2734             !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2735                 aq_ret = I40E_ERR_PARAM;
2736                 goto error_param;
2737         }
2738
2739         for (i = 0; i < vfl->num_elements; i++) {
2740                 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2741                         aq_ret = I40E_ERR_PARAM;
2742                         goto error_param;
2743                 }
2744         }
2745
2746         vsi = pf->vsi[vf->lan_vsi_idx];
2747         if (vsi->info.pvid) {
2748                 aq_ret = I40E_ERR_PARAM;
2749                 goto error_param;
2750         }
2751
2752         for (i = 0; i < vfl->num_elements; i++) {
2753                 i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
2754                 vf->num_vlan--;
2755
2756                 if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2757                         i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2758                                                            false,
2759                                                            vfl->vlan_id[i],
2760                                                            NULL);
2761                 if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2762                         i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2763                                                            false,
2764                                                            vfl->vlan_id[i],
2765                                                            NULL);
2766         }
2767
2768 error_param:
2769         /* send the response to the VF */
2770         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
2771 }
2772
2773 /**
2774  * i40e_vc_iwarp_msg
2775  * @vf: pointer to the VF info
2776  * @msg: pointer to the msg buffer
2777  * @msglen: msg length
2778  *
2779  * called from the VF for the iwarp msgs
2780  **/
2781 static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2782 {
2783         struct i40e_pf *pf = vf->pf;
2784         int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
2785         i40e_status aq_ret = 0;
2786
2787         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2788             !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2789                 aq_ret = I40E_ERR_PARAM;
2790                 goto error_param;
2791         }
2792
2793         i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
2794                                      msg, msglen);
2795
2796 error_param:
2797         /* send the response to the VF */
2798         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
2799                                        aq_ret);
2800 }
2801
2802 /**
2803  * i40e_vc_iwarp_qvmap_msg
2804  * @vf: pointer to the VF info
2805  * @msg: pointer to the msg buffer
2806  * @msglen: msg length
2807  * @config: config qvmap or release it
2808  *
2809  * called from the VF for the iwarp msgs
2810  **/
2811 static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
2812                                    bool config)
2813 {
2814         struct virtchnl_iwarp_qvlist_info *qvlist_info =
2815                                 (struct virtchnl_iwarp_qvlist_info *)msg;
2816         i40e_status aq_ret = 0;
2817
2818         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2819             !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2820                 aq_ret = I40E_ERR_PARAM;
2821                 goto error_param;
2822         }
2823
2824         if (config) {
2825                 if (i40e_config_iwarp_qvlist(vf, qvlist_info))
2826                         aq_ret = I40E_ERR_PARAM;
2827         } else {
2828                 i40e_release_iwarp_qvlist(vf);
2829         }
2830
2831 error_param:
2832         /* send the response to the VF */
2833         return i40e_vc_send_resp_to_vf(vf,
2834                                config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
2835                                VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
2836                                aq_ret);
2837 }
2838
2839 /**
2840  * i40e_vc_config_rss_key
2841  * @vf: pointer to the VF info
2842  * @msg: pointer to the msg buffer
2843  * @msglen: msg length
2844  *
2845  * Configure the VF's RSS key
2846  **/
2847 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
2848 {
2849         struct virtchnl_rss_key *vrk =
2850                 (struct virtchnl_rss_key *)msg;
2851         struct i40e_pf *pf = vf->pf;
2852         struct i40e_vsi *vsi = NULL;
2853         u16 vsi_id = vrk->vsi_id;
2854         i40e_status aq_ret = 0;
2855
2856         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2857             !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2858             (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
2859                 aq_ret = I40E_ERR_PARAM;
2860                 goto err;
2861         }
2862
2863         vsi = pf->vsi[vf->lan_vsi_idx];
2864         aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
2865 err:
2866         /* send the response to the VF */
2867         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
2868                                        aq_ret);
2869 }
2870
2871 /**
2872  * i40e_vc_config_rss_lut
2873  * @vf: pointer to the VF info
2874  * @msg: pointer to the msg buffer
2875  * @msglen: msg length
2876  *
2877  * Configure the VF's RSS LUT
2878  **/
2879 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
2880 {
2881         struct virtchnl_rss_lut *vrl =
2882                 (struct virtchnl_rss_lut *)msg;
2883         struct i40e_pf *pf = vf->pf;
2884         struct i40e_vsi *vsi = NULL;
2885         u16 vsi_id = vrl->vsi_id;
2886         i40e_status aq_ret = 0;
2887
2888         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2889             !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2890             (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
2891                 aq_ret = I40E_ERR_PARAM;
2892                 goto err;
2893         }
2894
2895         vsi = pf->vsi[vf->lan_vsi_idx];
2896         aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
2897         /* send the response to the VF */
2898 err:
2899         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
2900                                        aq_ret);
2901 }
2902
2903 /**
2904  * i40e_vc_get_rss_hena
2905  * @vf: pointer to the VF info
2906  * @msg: pointer to the msg buffer
2907  * @msglen: msg length
2908  *
2909  * Return the RSS HENA bits allowed by the hardware
2910  **/
2911 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2912 {
2913         struct virtchnl_rss_hena *vrh = NULL;
2914         struct i40e_pf *pf = vf->pf;
2915         i40e_status aq_ret = 0;
2916         int len = 0;
2917
2918         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2919                 aq_ret = I40E_ERR_PARAM;
2920                 goto err;
2921         }
2922         len = sizeof(struct virtchnl_rss_hena);
2923
2924         vrh = kzalloc(len, GFP_KERNEL);
2925         if (!vrh) {
2926                 aq_ret = I40E_ERR_NO_MEMORY;
2927                 len = 0;
2928                 goto err;
2929         }
2930         vrh->hena = i40e_pf_get_default_rss_hena(pf);
2931 err:
2932         /* send the response back to the VF */
2933         aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
2934                                         aq_ret, (u8 *)vrh, len);
2935         kfree(vrh);
2936         return aq_ret;
2937 }
2938
2939 /**
2940  * i40e_vc_set_rss_hena
2941  * @vf: pointer to the VF info
2942  * @msg: pointer to the msg buffer
2943  * @msglen: msg length
2944  *
2945  * Set the RSS HENA bits for the VF
2946  **/
2947 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2948 {
2949         struct virtchnl_rss_hena *vrh =
2950                 (struct virtchnl_rss_hena *)msg;
2951         struct i40e_pf *pf = vf->pf;
2952         struct i40e_hw *hw = &pf->hw;
2953         i40e_status aq_ret = 0;
2954
2955         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2956                 aq_ret = I40E_ERR_PARAM;
2957                 goto err;
2958         }
2959         i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
2960         i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
2961                           (u32)(vrh->hena >> 32));
2962
2963         /* send the response to the VF */
2964 err:
2965         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
2966 }
2967
2968 /**
2969  * i40e_vc_enable_vlan_stripping
2970  * @vf: pointer to the VF info
2971  * @msg: pointer to the msg buffer
2972  * @msglen: msg length
2973  *
2974  * Enable vlan header stripping for the VF
2975  **/
2976 static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
2977                                          u16 msglen)
2978 {
2979         struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
2980         i40e_status aq_ret = 0;
2981
2982         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2983                 aq_ret = I40E_ERR_PARAM;
2984                 goto err;
2985         }
2986
2987         i40e_vlan_stripping_enable(vsi);
2988
2989         /* send the response to the VF */
2990 err:
2991         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2992                                        aq_ret);
2993 }
2994
2995 /**
2996  * i40e_vc_disable_vlan_stripping
2997  * @vf: pointer to the VF info
2998  * @msg: pointer to the msg buffer
2999  * @msglen: msg length
3000  *
3001  * Disable vlan header stripping for the VF
3002  **/
3003 static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
3004                                           u16 msglen)
3005 {
3006         struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
3007         i40e_status aq_ret = 0;
3008
3009         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3010                 aq_ret = I40E_ERR_PARAM;
3011                 goto err;
3012         }
3013
3014         i40e_vlan_stripping_disable(vsi);
3015
3016         /* send the response to the VF */
3017 err:
3018         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
3019                                        aq_ret);
3020 }
3021
3022 /**
3023  * i40e_validate_cloud_filter
3024  * @mask: mask for TC filter
3025  * @data: data for TC filter
3026  *
3027  * This function validates cloud filter programmed as TC filter for ADq
3028  **/
3029 static int i40e_validate_cloud_filter(struct i40e_vf *vf,
3030                                       struct virtchnl_filter *tc_filter)
3031 {
3032         struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
3033         struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
3034         struct i40e_pf *pf = vf->pf;
3035         struct i40e_vsi *vsi = NULL;
3036         struct i40e_mac_filter *f;
3037         struct hlist_node *h;
3038         bool found = false;
3039         int bkt;
3040
3041         if (!tc_filter->action) {
3042                 dev_info(&pf->pdev->dev,
3043                          "VF %d: Currently ADq doesn't support Drop Action\n",
3044                          vf->vf_id);
3045                 goto err;
3046         }
3047
3048         /* action_meta is TC number here to which the filter is applied */
3049         if (!tc_filter->action_meta ||
3050             tc_filter->action_meta > I40E_MAX_VF_VSI) {
3051                 dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
3052                          vf->vf_id, tc_filter->action_meta);
3053                 goto err;
3054         }
3055
3056         /* Check filter if it's programmed for advanced mode or basic mode.
3057          * There are two ADq modes (for VF only),
3058          * 1. Basic mode: intended to allow as many filter options as possible
3059          *                to be added to a VF in Non-trusted mode. Main goal is
3060          *                to add filters to its own MAC and VLAN id.
3061          * 2. Advanced mode: is for allowing filters to be applied other than
3062          *                its own MAC or VLAN. This mode requires the VF to be
3063          *                Trusted.
3064          */
3065         if (mask.dst_mac[0] && !mask.dst_ip[0]) {
3066                 vsi = pf->vsi[vf->lan_vsi_idx];
3067                 f = i40e_find_mac(vsi, data.dst_mac);
3068
3069                 if (!f) {
3070                         dev_info(&pf->pdev->dev,
3071                                  "Destination MAC %pM doesn't belong to VF %d\n",
3072                                  data.dst_mac, vf->vf_id);
3073                         goto err;
3074                 }
3075
3076                 if (mask.vlan_id) {
3077                         hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
3078                                            hlist) {
3079                                 if (f->vlan == ntohs(data.vlan_id)) {
3080                                         found = true;
3081                                         break;
3082                                 }
3083                         }
3084                         if (!found) {
3085                                 dev_info(&pf->pdev->dev,
3086                                          "VF %d doesn't have any VLAN id %u\n",
3087                                          vf->vf_id, ntohs(data.vlan_id));
3088                                 goto err;
3089                         }
3090                 }
3091         } else {
3092                 /* Check if VF is trusted */
3093                 if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3094                         dev_err(&pf->pdev->dev,
3095                                 "VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
3096                                 vf->vf_id);
3097                         return I40E_ERR_CONFIG;
3098                 }
3099         }
3100
3101         if (mask.dst_mac[0] & data.dst_mac[0]) {
3102                 if (is_broadcast_ether_addr(data.dst_mac) ||
3103                     is_zero_ether_addr(data.dst_mac)) {
3104                         dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
3105                                  vf->vf_id, data.dst_mac);
3106                         goto err;
3107                 }
3108         }
3109
3110         if (mask.src_mac[0] & data.src_mac[0]) {
3111                 if (is_broadcast_ether_addr(data.src_mac) ||
3112                     is_zero_ether_addr(data.src_mac)) {
3113                         dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
3114                                  vf->vf_id, data.src_mac);
3115                         goto err;
3116                 }
3117         }
3118
3119         if (mask.dst_port & data.dst_port) {
3120                 if (!data.dst_port || be16_to_cpu(data.dst_port) > 0xFFFF) {
3121                         dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
3122                                  vf->vf_id);
3123                         goto err;
3124                 }
3125         }
3126
3127         if (mask.src_port & data.src_port) {
3128                 if (!data.src_port || be16_to_cpu(data.src_port) > 0xFFFF) {
3129                         dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
3130                                  vf->vf_id);
3131                         goto err;
3132                 }
3133         }
3134
3135         if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
3136             tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
3137                 dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
3138                          vf->vf_id);
3139                 goto err;
3140         }
3141
3142         if (mask.vlan_id & data.vlan_id) {
3143                 if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
3144                         dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
3145                                  vf->vf_id);
3146                         goto err;
3147                 }
3148         }
3149
3150         return I40E_SUCCESS;
3151 err:
3152         return I40E_ERR_CONFIG;
3153 }
3154
3155 /**
3156  * i40e_find_vsi_from_seid - searches for the vsi with the given seid
3157  * @vf: pointer to the VF info
3158  * @seid - seid of the vsi it is searching for
3159  **/
3160 static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
3161 {
3162         struct i40e_pf *pf = vf->pf;
3163         struct i40e_vsi *vsi = NULL;
3164         int i;
3165
3166         for (i = 0; i < vf->num_tc ; i++) {
3167                 vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
3168                 if (vsi && vsi->seid == seid)
3169                         return vsi;
3170         }
3171         return NULL;
3172 }
3173
3174 /**
3175  * i40e_del_all_cloud_filters
3176  * @vf: pointer to the VF info
3177  *
3178  * This function deletes all cloud filters
3179  **/
3180 static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
3181 {
3182         struct i40e_cloud_filter *cfilter = NULL;
3183         struct i40e_pf *pf = vf->pf;
3184         struct i40e_vsi *vsi = NULL;
3185         struct hlist_node *node;
3186         int ret;
3187
3188         hlist_for_each_entry_safe(cfilter, node,
3189                                   &vf->cloud_filter_list, cloud_node) {
3190                 vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);
3191
3192                 if (!vsi) {
3193                         dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
3194                                 vf->vf_id, cfilter->seid);
3195                         continue;
3196                 }
3197
3198                 if (cfilter->dst_port)
3199                         ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
3200                                                                 false);
3201                 else
3202                         ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
3203                 if (ret)
3204                         dev_err(&pf->pdev->dev,
3205                                 "VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3206                                 vf->vf_id, i40e_stat_str(&pf->hw, ret),
3207                                 i40e_aq_str(&pf->hw,
3208                                             pf->hw.aq.asq_last_status));
3209
3210                 hlist_del(&cfilter->cloud_node);
3211                 kfree(cfilter);
3212                 vf->num_cloud_filters--;
3213         }
3214 }
3215
3216 /**
3217  * i40e_vc_del_cloud_filter
3218  * @vf: pointer to the VF info
3219  * @msg: pointer to the msg buffer
3220  *
3221  * This function deletes a cloud filter programmed as TC filter for ADq
3222  **/
3223 static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
3224 {
3225         struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3226         struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3227         struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3228         struct i40e_cloud_filter cfilter, *cf = NULL;
3229         struct i40e_pf *pf = vf->pf;
3230         struct i40e_vsi *vsi = NULL;
3231         struct hlist_node *node;
3232         i40e_status aq_ret = 0;
3233         int i, ret;
3234
3235         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3236                 aq_ret = I40E_ERR_PARAM;
3237                 goto err;
3238         }
3239
3240         if (!vf->adq_enabled) {
3241                 dev_info(&pf->pdev->dev,
3242                          "VF %d: ADq not enabled, can't apply cloud filter\n",
3243                          vf->vf_id);
3244                 aq_ret = I40E_ERR_PARAM;
3245                 goto err;
3246         }
3247
3248         if (i40e_validate_cloud_filter(vf, vcf)) {
3249                 dev_info(&pf->pdev->dev,
3250                          "VF %d: Invalid input, can't apply cloud filter\n",
3251                          vf->vf_id);
3252                 aq_ret = I40E_ERR_PARAM;
3253                 goto err;
3254         }
3255
3256         memset(&cfilter, 0, sizeof(cfilter));
3257         /* parse destination mac address */
3258         for (i = 0; i < ETH_ALEN; i++)
3259                 cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3260
3261         /* parse source mac address */
3262         for (i = 0; i < ETH_ALEN; i++)
3263                 cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3264
3265         cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
3266         cfilter.dst_port = mask.dst_port & tcf.dst_port;
3267         cfilter.src_port = mask.src_port & tcf.src_port;
3268
3269         switch (vcf->flow_type) {
3270         case VIRTCHNL_TCP_V4_FLOW:
3271                 cfilter.n_proto = ETH_P_IP;
3272                 if (mask.dst_ip[0] & tcf.dst_ip[0])
3273                         memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
3274                                ARRAY_SIZE(tcf.dst_ip));
3275                 else if (mask.src_ip[0] & tcf.dst_ip[0])
3276                         memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
3277                                ARRAY_SIZE(tcf.dst_ip));
3278                 break;
3279         case VIRTCHNL_TCP_V6_FLOW:
3280                 cfilter.n_proto = ETH_P_IPV6;
3281                 if (mask.dst_ip[3] & tcf.dst_ip[3])
3282                         memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
3283                                sizeof(cfilter.ip.v6.dst_ip6));
3284                 if (mask.src_ip[3] & tcf.src_ip[3])
3285                         memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
3286                                sizeof(cfilter.ip.v6.src_ip6));
3287                 break;
3288         default:
3289                 /* TC filter can be configured based on different combinations
3290                  * and in this case IP is not a part of filter config
3291                  */
3292                 dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3293                          vf->vf_id);
3294         }
3295
3296         /* get the vsi to which the tc belongs to */
3297         vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3298         cfilter.seid = vsi->seid;
3299         cfilter.flags = vcf->field_flags;
3300
3301         /* Deleting TC filter */
3302         if (tcf.dst_port)
3303                 ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
3304         else
3305                 ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
3306         if (ret) {
3307                 dev_err(&pf->pdev->dev,
3308                         "VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3309                         vf->vf_id, i40e_stat_str(&pf->hw, ret),
3310                         i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3311                 goto err;
3312         }
3313
3314         hlist_for_each_entry_safe(cf, node,
3315                                   &vf->cloud_filter_list, cloud_node) {
3316                 if (cf->seid != cfilter.seid)
3317                         continue;
3318                 if (mask.dst_port)
3319                         if (cfilter.dst_port != cf->dst_port)
3320                                 continue;
3321                 if (mask.dst_mac[0])
3322                         if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
3323                                 continue;
3324                 /* for ipv4 data to be valid, only first byte of mask is set */
3325                 if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
3326                         if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
3327                                    ARRAY_SIZE(tcf.dst_ip)))
3328                                 continue;
3329                 /* for ipv6, mask is set for all sixteen bytes (4 words) */
3330                 if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
3331                         if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
3332                                    sizeof(cfilter.ip.v6.src_ip6)))
3333                                 continue;
3334                 if (mask.vlan_id)
3335                         if (cfilter.vlan_id != cf->vlan_id)
3336                                 continue;
3337
3338                 hlist_del(&cf->cloud_node);
3339                 kfree(cf);
3340                 vf->num_cloud_filters--;
3341         }
3342
3343 err:
3344         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
3345                                        aq_ret);
3346 }
3347
3348 /**
3349  * i40e_vc_add_cloud_filter
3350  * @vf: pointer to the VF info
3351  * @msg: pointer to the msg buffer
3352  *
3353  * This function adds a cloud filter programmed as TC filter for ADq
3354  **/
3355 static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
3356 {
3357         struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3358         struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3359         struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3360         struct i40e_cloud_filter *cfilter = NULL;
3361         struct i40e_pf *pf = vf->pf;
3362         struct i40e_vsi *vsi = NULL;
3363         i40e_status aq_ret = 0;
3364         int i, ret;
3365
3366         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3367                 aq_ret = I40E_ERR_PARAM;
3368                 goto err_out;
3369         }
3370
3371         if (!vf->adq_enabled) {
3372                 dev_info(&pf->pdev->dev,
3373                          "VF %d: ADq is not enabled, can't apply cloud filter\n",
3374                          vf->vf_id);
3375                 aq_ret = I40E_ERR_PARAM;
3376                 goto err_out;
3377         }
3378
3379         if (i40e_validate_cloud_filter(vf, vcf)) {
3380                 dev_info(&pf->pdev->dev,
3381                          "VF %d: Invalid input/s, can't apply cloud filter\n",
3382                          vf->vf_id);
3383                 aq_ret = I40E_ERR_PARAM;
3384                 goto err_out;
3385         }
3386
3387         cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
3388         if (!cfilter)
3389                 return -ENOMEM;
3390
3391         /* parse destination mac address */
3392         for (i = 0; i < ETH_ALEN; i++)
3393                 cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3394
3395         /* parse source mac address */
3396         for (i = 0; i < ETH_ALEN; i++)
3397                 cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3398
3399         cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
3400         cfilter->dst_port = mask.dst_port & tcf.dst_port;
3401         cfilter->src_port = mask.src_port & tcf.src_port;
3402
3403         switch (vcf->flow_type) {
3404         case VIRTCHNL_TCP_V4_FLOW:
3405                 cfilter->n_proto = ETH_P_IP;
3406                 if (mask.dst_ip[0] & tcf.dst_ip[0])
3407                         memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
3408                                ARRAY_SIZE(tcf.dst_ip));
3409                 else if (mask.src_ip[0] & tcf.dst_ip[0])
3410                         memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
3411                                ARRAY_SIZE(tcf.dst_ip));
3412                 break;
3413         case VIRTCHNL_TCP_V6_FLOW:
3414                 cfilter->n_proto = ETH_P_IPV6;
3415                 if (mask.dst_ip[3] & tcf.dst_ip[3])
3416                         memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
3417                                sizeof(cfilter->ip.v6.dst_ip6));
3418                 if (mask.src_ip[3] & tcf.src_ip[3])
3419                         memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
3420                                sizeof(cfilter->ip.v6.src_ip6));
3421                 break;
3422         default:
3423                 /* TC filter can be configured based on different combinations
3424                  * and in this case IP is not a part of filter config
3425                  */
3426                 dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3427                          vf->vf_id);
3428         }
3429
3430         /* get the VSI to which the TC belongs to */
3431         vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3432         cfilter->seid = vsi->seid;
3433         cfilter->flags = vcf->field_flags;
3434
3435         /* Adding cloud filter programmed as TC filter */
3436         if (tcf.dst_port)
3437                 ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
3438         else
3439                 ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
3440         if (ret) {
3441                 dev_err(&pf->pdev->dev,
3442                         "VF %d: Failed to add cloud filter, err %s aq_err %s\n",
3443                         vf->vf_id, i40e_stat_str(&pf->hw, ret),
3444                         i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3445                 goto err_free;
3446         }
3447
3448         INIT_HLIST_NODE(&cfilter->cloud_node);
3449         hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
3450         /* release the pointer passing it to the collection */
3451         cfilter = NULL;
3452         vf->num_cloud_filters++;
3453 err_free:
3454         kfree(cfilter);
3455 err_out:
3456         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
3457                                        aq_ret);
3458 }
3459
3460 /**
3461  * i40e_vc_add_qch_msg: Add queue channel and enable ADq
3462  * @vf: pointer to the VF info
3463  * @msg: pointer to the msg buffer
3464  **/
3465 static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
3466 {
3467         struct virtchnl_tc_info *tci =
3468                 (struct virtchnl_tc_info *)msg;
3469         struct i40e_pf *pf = vf->pf;
3470         struct i40e_link_status *ls = &pf->hw.phy.link_info;
3471         int i, adq_request_qps = 0, speed = 0;
3472         i40e_status aq_ret = 0;
3473
3474         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3475                 aq_ret = I40E_ERR_PARAM;
3476                 goto err;
3477         }
3478
3479         /* ADq cannot be applied if spoof check is ON */
3480         if (vf->spoofchk) {
3481                 dev_err(&pf->pdev->dev,
3482                         "Spoof check is ON, turn it OFF to enable ADq\n");
3483                 aq_ret = I40E_ERR_PARAM;
3484                 goto err;
3485         }
3486
3487         if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3488                 dev_err(&pf->pdev->dev,
3489                         "VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
3490                         vf->vf_id);
3491                 aq_ret = I40E_ERR_PARAM;
3492                 goto err;
3493         }
3494
3495         /* max number of traffic classes for VF currently capped at 4 */
3496         if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
3497                 dev_err(&pf->pdev->dev,
3498                         "VF %d trying to set %u TCs, valid range 1-4 TCs per VF\n",
3499                         vf->vf_id, tci->num_tc);
3500                 aq_ret = I40E_ERR_PARAM;
3501                 goto err;
3502         }
3503
3504         /* validate queues for each TC */
3505         for (i = 0; i < tci->num_tc; i++)
3506                 if (!tci->list[i].count ||
3507                     tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
3508                         dev_err(&pf->pdev->dev,
3509                                 "VF %d: TC %d trying to set %u queues, valid range 1-4 queues per TC\n",
3510                                 vf->vf_id, i, tci->list[i].count);
3511                         aq_ret = I40E_ERR_PARAM;
3512                         goto err;
3513                 }
3514
3515         /* need Max VF queues but already have default number of queues */
3516         adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;
3517
3518         if (pf->queues_left < adq_request_qps) {
3519                 dev_err(&pf->pdev->dev,
3520                         "No queues left to allocate to VF %d\n",
3521                         vf->vf_id);
3522                 aq_ret = I40E_ERR_PARAM;
3523                 goto err;
3524         } else {
3525                 /* we need to allocate max VF queues to enable ADq so as to
3526                  * make sure ADq enabled VF always gets back queues when it
3527                  * goes through a reset.
3528                  */
3529                 vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
3530         }
3531
3532         /* get link speed in MB to validate rate limit */
3533         switch (ls->link_speed) {
3534         case VIRTCHNL_LINK_SPEED_100MB:
3535                 speed = SPEED_100;
3536                 break;
3537         case VIRTCHNL_LINK_SPEED_1GB:
3538                 speed = SPEED_1000;
3539                 break;
3540         case VIRTCHNL_LINK_SPEED_10GB:
3541                 speed = SPEED_10000;
3542                 break;
3543         case VIRTCHNL_LINK_SPEED_20GB:
3544                 speed = SPEED_20000;
3545                 break;
3546         case VIRTCHNL_LINK_SPEED_25GB:
3547                 speed = SPEED_25000;
3548                 break;
3549         case VIRTCHNL_LINK_SPEED_40GB:
3550                 speed = SPEED_40000;
3551                 break;
3552         default:
3553                 dev_err(&pf->pdev->dev,
3554                         "Cannot detect link speed\n");
3555                 aq_ret = I40E_ERR_PARAM;
3556                 goto err;
3557         }
3558
3559         /* parse data from the queue channel info */
3560         vf->num_tc = tci->num_tc;
3561         for (i = 0; i < vf->num_tc; i++) {
3562                 if (tci->list[i].max_tx_rate) {
3563                         if (tci->list[i].max_tx_rate > speed) {
3564                                 dev_err(&pf->pdev->dev,
3565                                         "Invalid max tx rate %llu specified for VF %d.",
3566                                         tci->list[i].max_tx_rate,
3567                                         vf->vf_id);
3568                                 aq_ret = I40E_ERR_PARAM;
3569                                 goto err;
3570                         } else {
3571                                 vf->ch[i].max_tx_rate =
3572                                         tci->list[i].max_tx_rate;
3573                         }
3574                 }
3575                 vf->ch[i].num_qps = tci->list[i].count;
3576         }
3577
3578         /* set this flag only after making sure all inputs are sane */
3579         vf->adq_enabled = true;
3580         /* num_req_queues is set when user changes number of queues via ethtool
3581          * and this causes issue for default VSI(which depends on this variable)
3582          * when ADq is enabled, hence reset it.
3583          */
3584         vf->num_req_queues = 0;
3585
3586         /* reset the VF in order to allocate resources */
3587         i40e_vc_notify_vf_reset(vf);
3588         i40e_reset_vf(vf, false);
3589
3590         return I40E_SUCCESS;
3591
3592         /* send the response to the VF */
3593 err:
3594         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
3595                                        aq_ret);
3596 }
3597
3598 /**
3599  * i40e_vc_del_qch_msg
3600  * @vf: pointer to the VF info
3601  * @msg: pointer to the msg buffer
3602  **/
3603 static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
3604 {
3605         struct i40e_pf *pf = vf->pf;
3606         i40e_status aq_ret = 0;
3607
3608         if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3609                 aq_ret = I40E_ERR_PARAM;
3610                 goto err;
3611         }
3612
3613         if (vf->adq_enabled) {
3614                 i40e_del_all_cloud_filters(vf);
3615                 i40e_del_qch(vf);
3616                 vf->adq_enabled = false;
3617                 vf->num_tc = 0;
3618                 dev_info(&pf->pdev->dev,
3619                          "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
3620                          vf->vf_id);
3621         } else {
3622                 dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
3623                          vf->vf_id);
3624                 aq_ret = I40E_ERR_PARAM;
3625         }
3626
3627         /* reset the VF in order to allocate resources */
3628         i40e_vc_notify_vf_reset(vf);
3629         i40e_reset_vf(vf, false);
3630
3631         return I40E_SUCCESS;
3632
3633 err:
3634         return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
3635                                        aq_ret);
3636 }
3637
3638 /**
3639  * i40e_vc_process_vf_msg
3640  * @pf: pointer to the PF structure
3641  * @vf_id: source VF id
3642  * @v_opcode: operation code
3643  * @v_retval: unused return value code
3644  * @msg: pointer to the msg buffer
3645  * @msglen: msg length
3646  *
3647  * called from the common aeq/arq handler to
3648  * process request from VF
3649  **/
3650 int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
3651                            u32 __always_unused v_retval, u8 *msg, u16 msglen)
3652 {
3653         struct i40e_hw *hw = &pf->hw;
3654         int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
3655         struct i40e_vf *vf;
3656         int ret;
3657
3658         pf->vf_aq_requests++;
3659         if (local_vf_id >= pf->num_alloc_vfs)
3660                 return -EINVAL;
3661         vf = &(pf->vf[local_vf_id]);
3662
3663         /* Check if VF is disabled. */
3664         if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
3665                 return I40E_ERR_PARAM;
3666
3667         /* perform basic checks on the msg */
3668         ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
3669
3670         /* perform additional checks specific to this driver */
3671         if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
3672                 struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
3673
3674                 if (vrk->key_len != I40E_HKEY_ARRAY_SIZE)
3675                         ret = -EINVAL;
3676         } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
3677                 struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
3678
3679                 if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)
3680                         ret = -EINVAL;
3681         }
3682
3683         if (ret) {
3684                 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
3685                 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
3686                         local_vf_id, v_opcode, msglen);
3687                 switch (ret) {
3688                 case VIRTCHNL_ERR_PARAM:
3689                         return -EPERM;
3690                 default:
3691                         return -EINVAL;
3692                 }
3693         }
3694
3695         switch (v_opcode) {
3696         case VIRTCHNL_OP_VERSION:
3697                 ret = i40e_vc_get_version_msg(vf, msg);
3698                 break;
3699         case VIRTCHNL_OP_GET_VF_RESOURCES:
3700                 ret = i40e_vc_get_vf_resources_msg(vf, msg);
3701                 i40e_vc_notify_vf_link_state(vf);
3702                 break;
3703         case VIRTCHNL_OP_RESET_VF:
3704                 i40e_vc_reset_vf_msg(vf);
3705                 ret = 0;
3706                 break;
3707         case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
3708                 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
3709                 break;
3710         case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3711                 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
3712                 break;
3713         case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3714                 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
3715                 break;
3716         case VIRTCHNL_OP_ENABLE_QUEUES:
3717                 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
3718                 i40e_vc_notify_vf_link_state(vf);
3719                 break;
3720         case VIRTCHNL_OP_DISABLE_QUEUES:
3721                 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
3722                 break;
3723         case VIRTCHNL_OP_ADD_ETH_ADDR:
3724                 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
3725                 break;
3726         case VIRTCHNL_OP_DEL_ETH_ADDR:
3727                 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
3728                 break;
3729         case VIRTCHNL_OP_ADD_VLAN:
3730                 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
3731                 break;
3732         case VIRTCHNL_OP_DEL_VLAN:
3733                 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
3734                 break;
3735         case VIRTCHNL_OP_GET_STATS:
3736                 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
3737                 break;
3738         case VIRTCHNL_OP_IWARP:
3739                 ret = i40e_vc_iwarp_msg(vf, msg, msglen);
3740                 break;
3741         case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
3742                 ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
3743                 break;
3744         case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
3745                 ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
3746                 break;
3747         case VIRTCHNL_OP_CONFIG_RSS_KEY:
3748                 ret = i40e_vc_config_rss_key(vf, msg, msglen);
3749                 break;
3750         case VIRTCHNL_OP_CONFIG_RSS_LUT:
3751                 ret = i40e_vc_config_rss_lut(vf, msg, msglen);
3752                 break;
3753         case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
3754                 ret = i40e_vc_get_rss_hena(vf, msg, msglen);
3755                 break;
3756         case VIRTCHNL_OP_SET_RSS_HENA:
3757                 ret = i40e_vc_set_rss_hena(vf, msg, msglen);
3758                 break;
3759         case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
3760                 ret = i40e_vc_enable_vlan_stripping(vf, msg, msglen);
3761                 break;
3762         case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
3763                 ret = i40e_vc_disable_vlan_stripping(vf, msg, msglen);
3764                 break;
3765         case VIRTCHNL_OP_REQUEST_QUEUES:
3766                 ret = i40e_vc_request_queues_msg(vf, msg, msglen);
3767                 break;
3768         case VIRTCHNL_OP_ENABLE_CHANNELS:
3769                 ret = i40e_vc_add_qch_msg(vf, msg);
3770                 break;
3771         case VIRTCHNL_OP_DISABLE_CHANNELS:
3772                 ret = i40e_vc_del_qch_msg(vf, msg);
3773                 break;
3774         case VIRTCHNL_OP_ADD_CLOUD_FILTER:
3775                 ret = i40e_vc_add_cloud_filter(vf, msg);
3776                 break;
3777         case VIRTCHNL_OP_DEL_CLOUD_FILTER:
3778                 ret = i40e_vc_del_cloud_filter(vf, msg);
3779                 break;
3780         case VIRTCHNL_OP_UNKNOWN:
3781         default:
3782                 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
3783                         v_opcode, local_vf_id);
3784                 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
3785                                               I40E_ERR_NOT_IMPLEMENTED);
3786                 break;
3787         }
3788
3789         return ret;
3790 }
3791
3792 /**
3793  * i40e_vc_process_vflr_event
3794  * @pf: pointer to the PF structure
3795  *
3796  * called from the vlfr irq handler to
3797  * free up VF resources and state variables
3798  **/
3799 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
3800 {
3801         struct i40e_hw *hw = &pf->hw;
3802         u32 reg, reg_idx, bit_idx;
3803         struct i40e_vf *vf;
3804         int vf_id;
3805
3806         if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
3807                 return 0;
3808
3809         /* Re-enable the VFLR interrupt cause here, before looking for which
3810          * VF got reset. Otherwise, if another VF gets a reset while the
3811          * first one is being processed, that interrupt will be lost, and
3812          * that VF will be stuck in reset forever.
3813          */
3814         reg = rd32(hw, I40E_PFINT_ICR0_ENA);
3815         reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
3816         wr32(hw, I40E_PFINT_ICR0_ENA, reg);
3817         i40e_flush(hw);
3818
3819         clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3820         for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
3821                 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
3822                 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
3823                 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
3824                 vf = &pf->vf[vf_id];
3825                 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
3826                 if (reg & BIT(bit_idx))
3827                         /* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
3828                         i40e_reset_vf(vf, true);
3829         }
3830
3831         return 0;
3832 }
3833
3834 /**
3835  * i40e_ndo_set_vf_mac
3836  * @netdev: network interface device structure
3837  * @vf_id: VF identifier
3838  * @mac: mac address
3839  *
3840  * program VF mac address
3841  **/
3842 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
3843 {
3844         struct i40e_netdev_priv *np = netdev_priv(netdev);
3845         struct i40e_vsi *vsi = np->vsi;
3846         struct i40e_pf *pf = vsi->back;
3847         struct i40e_mac_filter *f;
3848         struct i40e_vf *vf;
3849         int ret = 0;
3850         struct hlist_node *h;
3851         int bkt;
3852         u8 i;
3853
3854         /* validate the request */
3855         if (vf_id >= pf->num_alloc_vfs) {
3856                 dev_err(&pf->pdev->dev,
3857                         "Invalid VF Identifier %d\n", vf_id);
3858                 ret = -EINVAL;
3859                 goto error_param;
3860         }
3861
3862         vf = &(pf->vf[vf_id]);
3863         vsi = pf->vsi[vf->lan_vsi_idx];
3864
3865         /* When the VF is resetting wait until it is done.
3866          * It can take up to 200 milliseconds,
3867          * but wait for up to 300 milliseconds to be safe.
3868          */
3869         for (i = 0; i < 15; i++) {
3870                 if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states))
3871                         break;
3872                 msleep(20);
3873         }
3874         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3875                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3876                         vf_id);
3877                 ret = -EAGAIN;
3878                 goto error_param;
3879         }
3880
3881         if (is_multicast_ether_addr(mac)) {
3882                 dev_err(&pf->pdev->dev,
3883                         "Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
3884                 ret = -EINVAL;
3885                 goto error_param;
3886         }
3887
3888         /* Lock once because below invoked function add/del_filter requires
3889          * mac_filter_hash_lock to be held
3890          */
3891         spin_lock_bh(&vsi->mac_filter_hash_lock);
3892
3893         /* delete the temporary mac address */
3894         if (!is_zero_ether_addr(vf->default_lan_addr.addr))
3895                 i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
3896
3897         /* Delete all the filters for this VSI - we're going to kill it
3898          * anyway.
3899          */
3900         hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
3901                 __i40e_del_filter(vsi, f);
3902
3903         spin_unlock_bh(&vsi->mac_filter_hash_lock);
3904
3905         /* program mac filter */
3906         if (i40e_sync_vsi_filters(vsi)) {
3907                 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
3908                 ret = -EIO;
3909                 goto error_param;
3910         }
3911         ether_addr_copy(vf->default_lan_addr.addr, mac);
3912
3913         if (is_zero_ether_addr(mac)) {
3914                 vf->pf_set_mac = false;
3915                 dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
3916         } else {
3917                 vf->pf_set_mac = true;
3918                 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
3919                          mac, vf_id);
3920         }
3921
3922         /* Force the VF driver stop so it has to reload with new MAC address */
3923         i40e_vc_disable_vf(vf);
3924         dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
3925
3926 error_param:
3927         return ret;
3928 }
3929
3930 /**
3931  * i40e_vsi_has_vlans - True if VSI has configured VLANs
3932  * @vsi: pointer to the vsi
3933  *
3934  * Check if a VSI has configured any VLANs. False if we have a port VLAN or if
3935  * we have no configured VLANs. Do not call while holding the
3936  * mac_filter_hash_lock.
3937  */
3938 static bool i40e_vsi_has_vlans(struct i40e_vsi *vsi)
3939 {
3940         bool have_vlans;
3941
3942         /* If we have a port VLAN, then the VSI cannot have any VLANs
3943          * configured, as all MAC/VLAN filters will be assigned to the PVID.
3944          */
3945         if (vsi->info.pvid)
3946                 return false;
3947
3948         /* Since we don't have a PVID, we know that if the device is in VLAN
3949          * mode it must be because of a VLAN filter configured on this VSI.
3950          */
3951         spin_lock_bh(&vsi->mac_filter_hash_lock);
3952         have_vlans = i40e_is_vsi_in_vlan(vsi);
3953         spin_unlock_bh(&vsi->mac_filter_hash_lock);
3954
3955         return have_vlans;
3956 }
3957
3958 /**
3959  * i40e_ndo_set_vf_port_vlan
3960  * @netdev: network interface device structure
3961  * @vf_id: VF identifier
3962  * @vlan_id: mac address
3963  * @qos: priority setting
3964  * @vlan_proto: vlan protocol
3965  *
3966  * program VF vlan id and/or qos
3967  **/
3968 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
3969                               u16 vlan_id, u8 qos, __be16 vlan_proto)
3970 {
3971         u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
3972         struct i40e_netdev_priv *np = netdev_priv(netdev);
3973         struct i40e_pf *pf = np->vsi->back;
3974         struct i40e_vsi *vsi;
3975         struct i40e_vf *vf;
3976         int ret = 0;
3977
3978         /* validate the request */
3979         if (vf_id >= pf->num_alloc_vfs) {
3980                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3981                 ret = -EINVAL;
3982                 goto error_pvid;
3983         }
3984
3985         if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
3986                 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
3987                 ret = -EINVAL;
3988                 goto error_pvid;
3989         }
3990
3991         if (vlan_proto != htons(ETH_P_8021Q)) {
3992                 dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
3993                 ret = -EPROTONOSUPPORT;
3994                 goto error_pvid;
3995         }
3996
3997         vf = &(pf->vf[vf_id]);
3998         vsi = pf->vsi[vf->lan_vsi_idx];
3999         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4000                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4001                         vf_id);
4002                 ret = -EAGAIN;
4003                 goto error_pvid;
4004         }
4005
4006         if (le16_to_cpu(vsi->info.pvid) == vlanprio)
4007                 /* duplicate request, so just return success */
4008                 goto error_pvid;
4009
4010         if (i40e_vsi_has_vlans(vsi)) {
4011                 dev_err(&pf->pdev->dev,
4012                         "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",
4013                         vf_id);
4014                 /* Administrator Error - knock the VF offline until he does
4015                  * the right thing by reconfiguring his network correctly
4016                  * and then reloading the VF driver.
4017                  */
4018                 i40e_vc_disable_vf(vf);
4019                 /* During reset the VF got a new VSI, so refresh the pointer. */
4020                 vsi = pf->vsi[vf->lan_vsi_idx];
4021         }
4022
4023         /* Locked once because multiple functions below iterate list */
4024         spin_lock_bh(&vsi->mac_filter_hash_lock);
4025
4026         /* Check for condition where there was already a port VLAN ID
4027          * filter set and now it is being deleted by setting it to zero.
4028          * Additionally check for the condition where there was a port
4029          * VLAN but now there is a new and different port VLAN being set.
4030          * Before deleting all the old VLAN filters we must add new ones
4031          * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
4032          * MAC addresses deleted.
4033          */
4034         if ((!(vlan_id || qos) ||
4035             vlanprio != le16_to_cpu(vsi->info.pvid)) &&
4036             vsi->info.pvid) {
4037                 ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
4038                 if (ret) {
4039                         dev_info(&vsi->back->pdev->dev,
4040                                  "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4041                                  vsi->back->hw.aq.asq_last_status);
4042                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
4043                         goto error_pvid;
4044                 }
4045         }
4046
4047         if (vsi->info.pvid) {
4048                 /* remove all filters on the old VLAN */
4049                 i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
4050                                            VLAN_VID_MASK));
4051         }
4052
4053         spin_unlock_bh(&vsi->mac_filter_hash_lock);
4054         if (vlan_id || qos)
4055                 ret = i40e_vsi_add_pvid(vsi, vlanprio);
4056         else
4057                 i40e_vsi_remove_pvid(vsi);
4058         spin_lock_bh(&vsi->mac_filter_hash_lock);
4059
4060         if (vlan_id) {
4061                 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
4062                          vlan_id, qos, vf_id);
4063
4064                 /* add new VLAN filter for each MAC */
4065                 ret = i40e_add_vlan_all_mac(vsi, vlan_id);
4066                 if (ret) {
4067                         dev_info(&vsi->back->pdev->dev,
4068                                  "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4069                                  vsi->back->hw.aq.asq_last_status);
4070                         spin_unlock_bh(&vsi->mac_filter_hash_lock);
4071                         goto error_pvid;
4072                 }
4073
4074                 /* remove the previously added non-VLAN MAC filters */
4075                 i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
4076         }
4077
4078         spin_unlock_bh(&vsi->mac_filter_hash_lock);
4079
4080         /* Schedule the worker thread to take care of applying changes */
4081         i40e_service_event_schedule(vsi->back);
4082
4083         if (ret) {
4084                 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
4085                 goto error_pvid;
4086         }
4087
4088         /* The Port VLAN needs to be saved across resets the same as the
4089          * default LAN MAC address.
4090          */
4091         vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
4092         ret = 0;
4093
4094 error_pvid:
4095         return ret;
4096 }
4097
4098 /**
4099  * i40e_ndo_set_vf_bw
4100  * @netdev: network interface device structure
4101  * @vf_id: VF identifier
4102  * @min_tx_rate: Minimum Tx rate
4103  * @max_tx_rate: Maximum Tx rate
4104  *
4105  * configure VF Tx rate
4106  **/
4107 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
4108                        int max_tx_rate)
4109 {
4110         struct i40e_netdev_priv *np = netdev_priv(netdev);
4111         struct i40e_pf *pf = np->vsi->back;
4112         struct i40e_vsi *vsi;
4113         struct i40e_vf *vf;
4114         int ret = 0;
4115
4116         /* validate the request */
4117         if (vf_id >= pf->num_alloc_vfs) {
4118                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
4119                 ret = -EINVAL;
4120                 goto error;
4121         }
4122
4123         if (min_tx_rate) {
4124                 dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
4125                         min_tx_rate, vf_id);
4126                 return -EINVAL;
4127         }
4128
4129         vf = &(pf->vf[vf_id]);
4130         vsi = pf->vsi[vf->lan_vsi_idx];
4131         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4132                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4133                         vf_id);
4134                 ret = -EAGAIN;
4135                 goto error;
4136         }
4137
4138         ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
4139         if (ret)
4140                 goto error;
4141
4142         vf->tx_rate = max_tx_rate;
4143 error:
4144         return ret;
4145 }
4146
4147 /**
4148  * i40e_ndo_get_vf_config
4149  * @netdev: network interface device structure
4150  * @vf_id: VF identifier
4151  * @ivi: VF configuration structure
4152  *
4153  * return VF configuration
4154  **/
4155 int i40e_ndo_get_vf_config(struct net_device *netdev,
4156                            int vf_id, struct ifla_vf_info *ivi)
4157 {
4158         struct i40e_netdev_priv *np = netdev_priv(netdev);
4159         struct i40e_vsi *vsi = np->vsi;
4160         struct i40e_pf *pf = vsi->back;
4161         struct i40e_vf *vf;
4162         int ret = 0;
4163
4164         /* validate the request */
4165         if (vf_id >= pf->num_alloc_vfs) {
4166                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4167                 ret = -EINVAL;
4168                 goto error_param;
4169         }
4170
4171         vf = &(pf->vf[vf_id]);
4172         /* first vsi is always the LAN vsi */
4173         vsi = pf->vsi[vf->lan_vsi_idx];
4174         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4175                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4176                         vf_id);
4177                 ret = -EAGAIN;
4178                 goto error_param;
4179         }
4180
4181         ivi->vf = vf_id;
4182
4183         ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
4184
4185         ivi->max_tx_rate = vf->tx_rate;
4186         ivi->min_tx_rate = 0;
4187         ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
4188         ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
4189                    I40E_VLAN_PRIORITY_SHIFT;
4190         if (vf->link_forced == false)
4191                 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4192         else if (vf->link_up == true)
4193                 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4194         else
4195                 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4196         ivi->spoofchk = vf->spoofchk;
4197         ivi->trusted = vf->trusted;
4198         ret = 0;
4199
4200 error_param:
4201         return ret;
4202 }
4203
4204 /**
4205  * i40e_ndo_set_vf_link_state
4206  * @netdev: network interface device structure
4207  * @vf_id: VF identifier
4208  * @link: required link state
4209  *
4210  * Set the link state of a specified VF, regardless of physical link state
4211  **/
4212 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
4213 {
4214         struct i40e_netdev_priv *np = netdev_priv(netdev);
4215         struct i40e_pf *pf = np->vsi->back;
4216         struct virtchnl_pf_event pfe;
4217         struct i40e_hw *hw = &pf->hw;
4218         struct i40e_vf *vf;
4219         int abs_vf_id;
4220         int ret = 0;
4221
4222         /* validate the request */
4223         if (vf_id >= pf->num_alloc_vfs) {
4224                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4225                 ret = -EINVAL;
4226                 goto error_out;
4227         }
4228
4229         vf = &pf->vf[vf_id];
4230         abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
4231
4232         pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
4233         pfe.severity = PF_EVENT_SEVERITY_INFO;
4234
4235         switch (link) {
4236         case IFLA_VF_LINK_STATE_AUTO:
4237                 vf->link_forced = false;
4238                 pfe.event_data.link_event.link_status =
4239                         pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
4240                 pfe.event_data.link_event.link_speed =
4241                         (enum virtchnl_link_speed)
4242                         pf->hw.phy.link_info.link_speed;
4243                 break;
4244         case IFLA_VF_LINK_STATE_ENABLE:
4245                 vf->link_forced = true;
4246                 vf->link_up = true;
4247                 pfe.event_data.link_event.link_status = true;
4248                 pfe.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
4249                 break;
4250         case IFLA_VF_LINK_STATE_DISABLE:
4251                 vf->link_forced = true;
4252                 vf->link_up = false;
4253                 pfe.event_data.link_event.link_status = false;
4254                 pfe.event_data.link_event.link_speed = 0;
4255                 break;
4256         default:
4257                 ret = -EINVAL;
4258                 goto error_out;
4259         }
4260         /* Notify the VF of its new link state */
4261         i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
4262                                0, (u8 *)&pfe, sizeof(pfe), NULL);
4263
4264 error_out:
4265         return ret;
4266 }
4267
4268 /**
4269  * i40e_ndo_set_vf_spoofchk
4270  * @netdev: network interface device structure
4271  * @vf_id: VF identifier
4272  * @enable: flag to enable or disable feature
4273  *
4274  * Enable or disable VF spoof checking
4275  **/
4276 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
4277 {
4278         struct i40e_netdev_priv *np = netdev_priv(netdev);
4279         struct i40e_vsi *vsi = np->vsi;
4280         struct i40e_pf *pf = vsi->back;
4281         struct i40e_vsi_context ctxt;
4282         struct i40e_hw *hw = &pf->hw;
4283         struct i40e_vf *vf;
4284         int ret = 0;
4285
4286         /* validate the request */
4287         if (vf_id >= pf->num_alloc_vfs) {
4288                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4289                 ret = -EINVAL;
4290                 goto out;
4291         }
4292
4293         vf = &(pf->vf[vf_id]);
4294         if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4295                 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4296                         vf_id);
4297                 ret = -EAGAIN;
4298                 goto out;
4299         }
4300
4301         if (enable == vf->spoofchk)
4302                 goto out;
4303
4304         vf->spoofchk = enable;
4305         memset(&ctxt, 0, sizeof(ctxt));
4306         ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
4307         ctxt.pf_num = pf->hw.pf_id;
4308         ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
4309         if (enable)
4310                 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
4311                                         I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
4312         ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
4313         if (ret) {
4314                 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
4315                         ret);
4316                 ret = -EIO;
4317         }
4318 out:
4319         return ret;
4320 }
4321
4322 /**
4323  * i40e_ndo_set_vf_trust
4324  * @netdev: network interface device structure of the pf
4325  * @vf_id: VF identifier
4326  * @setting: trust setting
4327  *
4328  * Enable or disable VF trust setting
4329  **/
4330 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
4331 {
4332         struct i40e_netdev_priv *np = netdev_priv(netdev);
4333         struct i40e_pf *pf = np->vsi->back;
4334         struct i40e_vf *vf;
4335         int ret = 0;
4336
4337         /* validate the request */
4338         if (vf_id >= pf->num_alloc_vfs) {
4339                 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4340                 return -EINVAL;
4341         }
4342
4343         if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4344                 dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
4345                 return -EINVAL;
4346         }
4347
4348         vf = &pf->vf[vf_id];
4349
4350         if (setting == vf->trusted)
4351                 goto out;
4352
4353         vf->trusted = setting;
4354         i40e_vc_disable_vf(vf);
4355         dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
4356                  vf_id, setting ? "" : "un");
4357
4358         if (vf->adq_enabled) {
4359                 if (!vf->trusted) {
4360                         dev_info(&pf->pdev->dev,
4361                                  "VF %u no longer Trusted, deleting all cloud filters\n",
4362                                  vf_id);
4363                         i40e_del_all_cloud_filters(vf);
4364                 }
4365         }
4366
4367 out:
4368         return ret;
4369 }