iavf: fix speed reporting over virtchnl
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / iavf / iavf_virtchnl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4 #include "iavf.h"
5 #include "iavf_prototype.h"
6 #include "iavf_client.h"
7
8 /* busy wait delay in msec */
9 #define IAVF_BUSY_WAIT_DELAY 10
10 #define IAVF_BUSY_WAIT_COUNT 50
11
12 /**
13  * iavf_send_pf_msg
14  * @adapter: adapter structure
15  * @op: virtual channel opcode
16  * @msg: pointer to message buffer
17  * @len: message length
18  *
19  * Send message to PF and print status if failure.
20  **/
21 static int iavf_send_pf_msg(struct iavf_adapter *adapter,
22                             enum virtchnl_ops op, u8 *msg, u16 len)
23 {
24         struct iavf_hw *hw = &adapter->hw;
25         enum iavf_status err;
26
27         if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
28                 return 0; /* nothing to see here, move along */
29
30         err = iavf_aq_send_msg_to_pf(hw, op, 0, msg, len, NULL);
31         if (err)
32                 dev_dbg(&adapter->pdev->dev, "Unable to send opcode %d to PF, err %s, aq_err %s\n",
33                         op, iavf_stat_str(hw, err),
34                         iavf_aq_str(hw, hw->aq.asq_last_status));
35         return err;
36 }
37
38 /**
39  * iavf_send_api_ver
40  * @adapter: adapter structure
41  *
42  * Send API version admin queue message to the PF. The reply is not checked
43  * in this function. Returns 0 if the message was successfully
44  * sent, or one of the IAVF_ADMIN_QUEUE_ERROR_ statuses if not.
45  **/
46 int iavf_send_api_ver(struct iavf_adapter *adapter)
47 {
48         struct virtchnl_version_info vvi;
49
50         vvi.major = VIRTCHNL_VERSION_MAJOR;
51         vvi.minor = VIRTCHNL_VERSION_MINOR;
52
53         return iavf_send_pf_msg(adapter, VIRTCHNL_OP_VERSION, (u8 *)&vvi,
54                                 sizeof(vvi));
55 }
56
57 /**
58  * iavf_verify_api_ver
59  * @adapter: adapter structure
60  *
61  * Compare API versions with the PF. Must be called after admin queue is
62  * initialized. Returns 0 if API versions match, -EIO if they do not,
63  * IAVF_ERR_ADMIN_QUEUE_NO_WORK if the admin queue is empty, and any errors
64  * from the firmware are propagated.
65  **/
66 int iavf_verify_api_ver(struct iavf_adapter *adapter)
67 {
68         struct virtchnl_version_info *pf_vvi;
69         struct iavf_hw *hw = &adapter->hw;
70         struct iavf_arq_event_info event;
71         enum virtchnl_ops op;
72         enum iavf_status err;
73
74         event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
75         event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
76         if (!event.msg_buf) {
77                 err = -ENOMEM;
78                 goto out;
79         }
80
81         while (1) {
82                 err = iavf_clean_arq_element(hw, &event, NULL);
83                 /* When the AQ is empty, iavf_clean_arq_element will return
84                  * nonzero and this loop will terminate.
85                  */
86                 if (err)
87                         goto out_alloc;
88                 op =
89                     (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
90                 if (op == VIRTCHNL_OP_VERSION)
91                         break;
92         }
93
94
95         err = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
96         if (err)
97                 goto out_alloc;
98
99         if (op != VIRTCHNL_OP_VERSION) {
100                 dev_info(&adapter->pdev->dev, "Invalid reply type %d from PF\n",
101                         op);
102                 err = -EIO;
103                 goto out_alloc;
104         }
105
106         pf_vvi = (struct virtchnl_version_info *)event.msg_buf;
107         adapter->pf_version = *pf_vvi;
108
109         if ((pf_vvi->major > VIRTCHNL_VERSION_MAJOR) ||
110             ((pf_vvi->major == VIRTCHNL_VERSION_MAJOR) &&
111              (pf_vvi->minor > VIRTCHNL_VERSION_MINOR)))
112                 err = -EIO;
113
114 out_alloc:
115         kfree(event.msg_buf);
116 out:
117         return err;
118 }
119
120 /**
121  * iavf_send_vf_config_msg
122  * @adapter: adapter structure
123  *
124  * Send VF configuration request admin queue message to the PF. The reply
125  * is not checked in this function. Returns 0 if the message was
126  * successfully sent, or one of the IAVF_ADMIN_QUEUE_ERROR_ statuses if not.
127  **/
128 int iavf_send_vf_config_msg(struct iavf_adapter *adapter)
129 {
130         u32 caps;
131
132         caps = VIRTCHNL_VF_OFFLOAD_L2 |
133                VIRTCHNL_VF_OFFLOAD_RSS_PF |
134                VIRTCHNL_VF_OFFLOAD_RSS_AQ |
135                VIRTCHNL_VF_OFFLOAD_RSS_REG |
136                VIRTCHNL_VF_OFFLOAD_VLAN |
137                VIRTCHNL_VF_OFFLOAD_WB_ON_ITR |
138                VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2 |
139                VIRTCHNL_VF_OFFLOAD_ENCAP |
140                VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM |
141                VIRTCHNL_VF_OFFLOAD_REQ_QUEUES |
142                VIRTCHNL_VF_OFFLOAD_ADQ |
143                VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
144
145         adapter->current_op = VIRTCHNL_OP_GET_VF_RESOURCES;
146         adapter->aq_required &= ~IAVF_FLAG_AQ_GET_CONFIG;
147         if (PF_IS_V11(adapter))
148                 return iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_VF_RESOURCES,
149                                         (u8 *)&caps, sizeof(caps));
150         else
151                 return iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_VF_RESOURCES,
152                                         NULL, 0);
153 }
154
155 /**
156  * iavf_validate_num_queues
157  * @adapter: adapter structure
158  *
159  * Validate that the number of queues the PF has sent in
160  * VIRTCHNL_OP_GET_VF_RESOURCES is not larger than the VF can handle.
161  **/
162 static void iavf_validate_num_queues(struct iavf_adapter *adapter)
163 {
164         if (adapter->vf_res->num_queue_pairs > IAVF_MAX_REQ_QUEUES) {
165                 struct virtchnl_vsi_resource *vsi_res;
166                 int i;
167
168                 dev_info(&adapter->pdev->dev, "Received %d queues, but can only have a max of %d\n",
169                          adapter->vf_res->num_queue_pairs,
170                          IAVF_MAX_REQ_QUEUES);
171                 dev_info(&adapter->pdev->dev, "Fixing by reducing queues to %d\n",
172                          IAVF_MAX_REQ_QUEUES);
173                 adapter->vf_res->num_queue_pairs = IAVF_MAX_REQ_QUEUES;
174                 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
175                         vsi_res = &adapter->vf_res->vsi_res[i];
176                         vsi_res->num_queue_pairs = IAVF_MAX_REQ_QUEUES;
177                 }
178         }
179 }
180
181 /**
182  * iavf_get_vf_config
183  * @adapter: private adapter structure
184  *
185  * Get VF configuration from PF and populate hw structure. Must be called after
186  * admin queue is initialized. Busy waits until response is received from PF,
187  * with maximum timeout. Response from PF is returned in the buffer for further
188  * processing by the caller.
189  **/
190 int iavf_get_vf_config(struct iavf_adapter *adapter)
191 {
192         struct iavf_hw *hw = &adapter->hw;
193         struct iavf_arq_event_info event;
194         enum virtchnl_ops op;
195         enum iavf_status err;
196         u16 len;
197
198         len =  sizeof(struct virtchnl_vf_resource) +
199                 IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
200         event.buf_len = len;
201         event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
202         if (!event.msg_buf) {
203                 err = -ENOMEM;
204                 goto out;
205         }
206
207         while (1) {
208                 /* When the AQ is empty, iavf_clean_arq_element will return
209                  * nonzero and this loop will terminate.
210                  */
211                 err = iavf_clean_arq_element(hw, &event, NULL);
212                 if (err)
213                         goto out_alloc;
214                 op =
215                     (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
216                 if (op == VIRTCHNL_OP_GET_VF_RESOURCES)
217                         break;
218         }
219
220         err = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
221         memcpy(adapter->vf_res, event.msg_buf, min(event.msg_len, len));
222
223         /* some PFs send more queues than we should have so validate that
224          * we aren't getting too many queues
225          */
226         if (!err)
227                 iavf_validate_num_queues(adapter);
228         iavf_vf_parse_hw_config(hw, adapter->vf_res);
229 out_alloc:
230         kfree(event.msg_buf);
231 out:
232         return err;
233 }
234
235 /**
236  * iavf_configure_queues
237  * @adapter: adapter structure
238  *
239  * Request that the PF set up our (previously allocated) queues.
240  **/
241 void iavf_configure_queues(struct iavf_adapter *adapter)
242 {
243         struct virtchnl_vsi_queue_config_info *vqci;
244         struct virtchnl_queue_pair_info *vqpi;
245         int pairs = adapter->num_active_queues;
246         int i, max_frame = IAVF_MAX_RXBUFFER;
247         size_t len;
248
249         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
250                 /* bail because we already have a command pending */
251                 dev_err(&adapter->pdev->dev, "Cannot configure queues, command %d pending\n",
252                         adapter->current_op);
253                 return;
254         }
255         adapter->current_op = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
256         len = struct_size(vqci, qpair, pairs);
257         vqci = kzalloc(len, GFP_KERNEL);
258         if (!vqci)
259                 return;
260
261         /* Limit maximum frame size when jumbo frames is not enabled */
262         if (!(adapter->flags & IAVF_FLAG_LEGACY_RX) &&
263             (adapter->netdev->mtu <= ETH_DATA_LEN))
264                 max_frame = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
265
266         vqci->vsi_id = adapter->vsi_res->vsi_id;
267         vqci->num_queue_pairs = pairs;
268         vqpi = vqci->qpair;
269         /* Size check is not needed here - HW max is 16 queue pairs, and we
270          * can fit info for 31 of them into the AQ buffer before it overflows.
271          */
272         for (i = 0; i < pairs; i++) {
273                 vqpi->txq.vsi_id = vqci->vsi_id;
274                 vqpi->txq.queue_id = i;
275                 vqpi->txq.ring_len = adapter->tx_rings[i].count;
276                 vqpi->txq.dma_ring_addr = adapter->tx_rings[i].dma;
277                 vqpi->rxq.vsi_id = vqci->vsi_id;
278                 vqpi->rxq.queue_id = i;
279                 vqpi->rxq.ring_len = adapter->rx_rings[i].count;
280                 vqpi->rxq.dma_ring_addr = adapter->rx_rings[i].dma;
281                 vqpi->rxq.max_pkt_size = max_frame;
282                 vqpi->rxq.databuffer_size =
283                         ALIGN(adapter->rx_rings[i].rx_buf_len,
284                               BIT_ULL(IAVF_RXQ_CTX_DBUFF_SHIFT));
285                 vqpi++;
286         }
287
288         adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_QUEUES;
289         iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
290                          (u8 *)vqci, len);
291         kfree(vqci);
292 }
293
294 /**
295  * iavf_enable_queues
296  * @adapter: adapter structure
297  *
298  * Request that the PF enable all of our queues.
299  **/
300 void iavf_enable_queues(struct iavf_adapter *adapter)
301 {
302         struct virtchnl_queue_select vqs;
303
304         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
305                 /* bail because we already have a command pending */
306                 dev_err(&adapter->pdev->dev, "Cannot enable queues, command %d pending\n",
307                         adapter->current_op);
308                 return;
309         }
310         adapter->current_op = VIRTCHNL_OP_ENABLE_QUEUES;
311         vqs.vsi_id = adapter->vsi_res->vsi_id;
312         vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
313         vqs.rx_queues = vqs.tx_queues;
314         adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_QUEUES;
315         iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_QUEUES,
316                          (u8 *)&vqs, sizeof(vqs));
317 }
318
319 /**
320  * iavf_disable_queues
321  * @adapter: adapter structure
322  *
323  * Request that the PF disable all of our queues.
324  **/
325 void iavf_disable_queues(struct iavf_adapter *adapter)
326 {
327         struct virtchnl_queue_select vqs;
328
329         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
330                 /* bail because we already have a command pending */
331                 dev_err(&adapter->pdev->dev, "Cannot disable queues, command %d pending\n",
332                         adapter->current_op);
333                 return;
334         }
335         adapter->current_op = VIRTCHNL_OP_DISABLE_QUEUES;
336         vqs.vsi_id = adapter->vsi_res->vsi_id;
337         vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
338         vqs.rx_queues = vqs.tx_queues;
339         adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_QUEUES;
340         iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_QUEUES,
341                          (u8 *)&vqs, sizeof(vqs));
342 }
343
344 /**
345  * iavf_map_queues
346  * @adapter: adapter structure
347  *
348  * Request that the PF map queues to interrupt vectors. Misc causes, including
349  * admin queue, are always mapped to vector 0.
350  **/
351 void iavf_map_queues(struct iavf_adapter *adapter)
352 {
353         struct virtchnl_irq_map_info *vimi;
354         struct virtchnl_vector_map *vecmap;
355         struct iavf_q_vector *q_vector;
356         int v_idx, q_vectors;
357         size_t len;
358
359         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
360                 /* bail because we already have a command pending */
361                 dev_err(&adapter->pdev->dev, "Cannot map queues to vectors, command %d pending\n",
362                         adapter->current_op);
363                 return;
364         }
365         adapter->current_op = VIRTCHNL_OP_CONFIG_IRQ_MAP;
366
367         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
368
369         len = struct_size(vimi, vecmap, adapter->num_msix_vectors);
370         vimi = kzalloc(len, GFP_KERNEL);
371         if (!vimi)
372                 return;
373
374         vimi->num_vectors = adapter->num_msix_vectors;
375         /* Queue vectors first */
376         for (v_idx = 0; v_idx < q_vectors; v_idx++) {
377                 q_vector = &adapter->q_vectors[v_idx];
378                 vecmap = &vimi->vecmap[v_idx];
379
380                 vecmap->vsi_id = adapter->vsi_res->vsi_id;
381                 vecmap->vector_id = v_idx + NONQ_VECS;
382                 vecmap->txq_map = q_vector->ring_mask;
383                 vecmap->rxq_map = q_vector->ring_mask;
384                 vecmap->rxitr_idx = IAVF_RX_ITR;
385                 vecmap->txitr_idx = IAVF_TX_ITR;
386         }
387         /* Misc vector last - this is only for AdminQ messages */
388         vecmap = &vimi->vecmap[v_idx];
389         vecmap->vsi_id = adapter->vsi_res->vsi_id;
390         vecmap->vector_id = 0;
391         vecmap->txq_map = 0;
392         vecmap->rxq_map = 0;
393
394         adapter->aq_required &= ~IAVF_FLAG_AQ_MAP_VECTORS;
395         iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_IRQ_MAP,
396                          (u8 *)vimi, len);
397         kfree(vimi);
398 }
399
400 /**
401  * iavf_add_ether_addrs
402  * @adapter: adapter structure
403  *
404  * Request that the PF add one or more addresses to our filters.
405  **/
406 void iavf_add_ether_addrs(struct iavf_adapter *adapter)
407 {
408         struct virtchnl_ether_addr_list *veal;
409         struct iavf_mac_filter *f;
410         int i = 0, count = 0;
411         bool more = false;
412         size_t len;
413
414         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
415                 /* bail because we already have a command pending */
416                 dev_err(&adapter->pdev->dev, "Cannot add filters, command %d pending\n",
417                         adapter->current_op);
418                 return;
419         }
420
421         spin_lock_bh(&adapter->mac_vlan_list_lock);
422
423         list_for_each_entry(f, &adapter->mac_filter_list, list) {
424                 if (f->add)
425                         count++;
426         }
427         if (!count) {
428                 adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_MAC_FILTER;
429                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
430                 return;
431         }
432         adapter->current_op = VIRTCHNL_OP_ADD_ETH_ADDR;
433
434         len = struct_size(veal, list, count);
435         if (len > IAVF_MAX_AQ_BUF_SIZE) {
436                 dev_warn(&adapter->pdev->dev, "Too many add MAC changes in one request\n");
437                 count = (IAVF_MAX_AQ_BUF_SIZE -
438                          sizeof(struct virtchnl_ether_addr_list)) /
439                         sizeof(struct virtchnl_ether_addr);
440                 len = struct_size(veal, list, count);
441                 more = true;
442         }
443
444         veal = kzalloc(len, GFP_ATOMIC);
445         if (!veal) {
446                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
447                 return;
448         }
449
450         veal->vsi_id = adapter->vsi_res->vsi_id;
451         veal->num_elements = count;
452         list_for_each_entry(f, &adapter->mac_filter_list, list) {
453                 if (f->add) {
454                         ether_addr_copy(veal->list[i].addr, f->macaddr);
455                         i++;
456                         f->add = false;
457                         if (i == count)
458                                 break;
459                 }
460         }
461         if (!more)
462                 adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_MAC_FILTER;
463
464         spin_unlock_bh(&adapter->mac_vlan_list_lock);
465
466         iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_ETH_ADDR, (u8 *)veal, len);
467         kfree(veal);
468 }
469
470 /**
471  * iavf_del_ether_addrs
472  * @adapter: adapter structure
473  *
474  * Request that the PF remove one or more addresses from our filters.
475  **/
476 void iavf_del_ether_addrs(struct iavf_adapter *adapter)
477 {
478         struct virtchnl_ether_addr_list *veal;
479         struct iavf_mac_filter *f, *ftmp;
480         int i = 0, count = 0;
481         bool more = false;
482         size_t len;
483
484         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
485                 /* bail because we already have a command pending */
486                 dev_err(&adapter->pdev->dev, "Cannot remove filters, command %d pending\n",
487                         adapter->current_op);
488                 return;
489         }
490
491         spin_lock_bh(&adapter->mac_vlan_list_lock);
492
493         list_for_each_entry(f, &adapter->mac_filter_list, list) {
494                 if (f->remove)
495                         count++;
496         }
497         if (!count) {
498                 adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_MAC_FILTER;
499                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
500                 return;
501         }
502         adapter->current_op = VIRTCHNL_OP_DEL_ETH_ADDR;
503
504         len = struct_size(veal, list, count);
505         if (len > IAVF_MAX_AQ_BUF_SIZE) {
506                 dev_warn(&adapter->pdev->dev, "Too many delete MAC changes in one request\n");
507                 count = (IAVF_MAX_AQ_BUF_SIZE -
508                          sizeof(struct virtchnl_ether_addr_list)) /
509                         sizeof(struct virtchnl_ether_addr);
510                 len = struct_size(veal, list, count);
511                 more = true;
512         }
513         veal = kzalloc(len, GFP_ATOMIC);
514         if (!veal) {
515                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
516                 return;
517         }
518
519         veal->vsi_id = adapter->vsi_res->vsi_id;
520         veal->num_elements = count;
521         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
522                 if (f->remove) {
523                         ether_addr_copy(veal->list[i].addr, f->macaddr);
524                         i++;
525                         list_del(&f->list);
526                         kfree(f);
527                         if (i == count)
528                                 break;
529                 }
530         }
531         if (!more)
532                 adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_MAC_FILTER;
533
534         spin_unlock_bh(&adapter->mac_vlan_list_lock);
535
536         iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_ETH_ADDR, (u8 *)veal, len);
537         kfree(veal);
538 }
539
540 /**
541  * iavf_add_vlans
542  * @adapter: adapter structure
543  *
544  * Request that the PF add one or more VLAN filters to our VSI.
545  **/
546 void iavf_add_vlans(struct iavf_adapter *adapter)
547 {
548         struct virtchnl_vlan_filter_list *vvfl;
549         int len, i = 0, count = 0;
550         struct iavf_vlan_filter *f;
551         bool more = false;
552
553         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
554                 /* bail because we already have a command pending */
555                 dev_err(&adapter->pdev->dev, "Cannot add VLANs, command %d pending\n",
556                         adapter->current_op);
557                 return;
558         }
559
560         spin_lock_bh(&adapter->mac_vlan_list_lock);
561
562         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
563                 if (f->add)
564                         count++;
565         }
566         if (!count) {
567                 adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_VLAN_FILTER;
568                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
569                 return;
570         }
571         adapter->current_op = VIRTCHNL_OP_ADD_VLAN;
572
573         len = sizeof(struct virtchnl_vlan_filter_list) +
574               (count * sizeof(u16));
575         if (len > IAVF_MAX_AQ_BUF_SIZE) {
576                 dev_warn(&adapter->pdev->dev, "Too many add VLAN changes in one request\n");
577                 count = (IAVF_MAX_AQ_BUF_SIZE -
578                          sizeof(struct virtchnl_vlan_filter_list)) /
579                         sizeof(u16);
580                 len = sizeof(struct virtchnl_vlan_filter_list) +
581                       (count * sizeof(u16));
582                 more = true;
583         }
584         vvfl = kzalloc(len, GFP_ATOMIC);
585         if (!vvfl) {
586                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
587                 return;
588         }
589
590         vvfl->vsi_id = adapter->vsi_res->vsi_id;
591         vvfl->num_elements = count;
592         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
593                 if (f->add) {
594                         vvfl->vlan_id[i] = f->vlan;
595                         i++;
596                         f->add = false;
597                         if (i == count)
598                                 break;
599                 }
600         }
601         if (!more)
602                 adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_VLAN_FILTER;
603
604         spin_unlock_bh(&adapter->mac_vlan_list_lock);
605
606         iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_VLAN, (u8 *)vvfl, len);
607         kfree(vvfl);
608 }
609
610 /**
611  * iavf_del_vlans
612  * @adapter: adapter structure
613  *
614  * Request that the PF remove one or more VLAN filters from our VSI.
615  **/
616 void iavf_del_vlans(struct iavf_adapter *adapter)
617 {
618         struct virtchnl_vlan_filter_list *vvfl;
619         struct iavf_vlan_filter *f, *ftmp;
620         int len, i = 0, count = 0;
621         bool more = false;
622
623         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
624                 /* bail because we already have a command pending */
625                 dev_err(&adapter->pdev->dev, "Cannot remove VLANs, command %d pending\n",
626                         adapter->current_op);
627                 return;
628         }
629
630         spin_lock_bh(&adapter->mac_vlan_list_lock);
631
632         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
633                 if (f->remove)
634                         count++;
635         }
636         if (!count) {
637                 adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_VLAN_FILTER;
638                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
639                 return;
640         }
641         adapter->current_op = VIRTCHNL_OP_DEL_VLAN;
642
643         len = sizeof(struct virtchnl_vlan_filter_list) +
644               (count * sizeof(u16));
645         if (len > IAVF_MAX_AQ_BUF_SIZE) {
646                 dev_warn(&adapter->pdev->dev, "Too many delete VLAN changes in one request\n");
647                 count = (IAVF_MAX_AQ_BUF_SIZE -
648                          sizeof(struct virtchnl_vlan_filter_list)) /
649                         sizeof(u16);
650                 len = sizeof(struct virtchnl_vlan_filter_list) +
651                       (count * sizeof(u16));
652                 more = true;
653         }
654         vvfl = kzalloc(len, GFP_ATOMIC);
655         if (!vvfl) {
656                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
657                 return;
658         }
659
660         vvfl->vsi_id = adapter->vsi_res->vsi_id;
661         vvfl->num_elements = count;
662         list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
663                 if (f->remove) {
664                         vvfl->vlan_id[i] = f->vlan;
665                         i++;
666                         list_del(&f->list);
667                         kfree(f);
668                         if (i == count)
669                                 break;
670                 }
671         }
672         if (!more)
673                 adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_VLAN_FILTER;
674
675         spin_unlock_bh(&adapter->mac_vlan_list_lock);
676
677         iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_VLAN, (u8 *)vvfl, len);
678         kfree(vvfl);
679 }
680
681 /**
682  * iavf_set_promiscuous
683  * @adapter: adapter structure
684  * @flags: bitmask to control unicast/multicast promiscuous.
685  *
686  * Request that the PF enable promiscuous mode for our VSI.
687  **/
688 void iavf_set_promiscuous(struct iavf_adapter *adapter, int flags)
689 {
690         struct virtchnl_promisc_info vpi;
691         int promisc_all;
692
693         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
694                 /* bail because we already have a command pending */
695                 dev_err(&adapter->pdev->dev, "Cannot set promiscuous mode, command %d pending\n",
696                         adapter->current_op);
697                 return;
698         }
699
700         promisc_all = FLAG_VF_UNICAST_PROMISC |
701                       FLAG_VF_MULTICAST_PROMISC;
702         if ((flags & promisc_all) == promisc_all) {
703                 adapter->flags |= IAVF_FLAG_PROMISC_ON;
704                 adapter->aq_required &= ~IAVF_FLAG_AQ_REQUEST_PROMISC;
705                 dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n");
706         }
707
708         if (flags & FLAG_VF_MULTICAST_PROMISC) {
709                 adapter->flags |= IAVF_FLAG_ALLMULTI_ON;
710                 adapter->aq_required &= ~IAVF_FLAG_AQ_REQUEST_ALLMULTI;
711                 dev_info(&adapter->pdev->dev, "Entering multicast promiscuous mode\n");
712         }
713
714         if (!flags) {
715                 adapter->flags &= ~(IAVF_FLAG_PROMISC_ON |
716                                     IAVF_FLAG_ALLMULTI_ON);
717                 adapter->aq_required &= ~(IAVF_FLAG_AQ_RELEASE_PROMISC |
718                                           IAVF_FLAG_AQ_RELEASE_ALLMULTI);
719                 dev_info(&adapter->pdev->dev, "Leaving promiscuous mode\n");
720         }
721
722         adapter->current_op = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
723         vpi.vsi_id = adapter->vsi_res->vsi_id;
724         vpi.flags = flags;
725         iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
726                          (u8 *)&vpi, sizeof(vpi));
727 }
728
729 /**
730  * iavf_request_stats
731  * @adapter: adapter structure
732  *
733  * Request VSI statistics from PF.
734  **/
735 void iavf_request_stats(struct iavf_adapter *adapter)
736 {
737         struct virtchnl_queue_select vqs;
738
739         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
740                 /* no error message, this isn't crucial */
741                 return;
742         }
743         adapter->current_op = VIRTCHNL_OP_GET_STATS;
744         vqs.vsi_id = adapter->vsi_res->vsi_id;
745         /* queue maps are ignored for this message - only the vsi is used */
746         if (iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_STATS, (u8 *)&vqs,
747                              sizeof(vqs)))
748                 /* if the request failed, don't lock out others */
749                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
750 }
751
752 /**
753  * iavf_get_hena
754  * @adapter: adapter structure
755  *
756  * Request hash enable capabilities from PF
757  **/
758 void iavf_get_hena(struct iavf_adapter *adapter)
759 {
760         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
761                 /* bail because we already have a command pending */
762                 dev_err(&adapter->pdev->dev, "Cannot get RSS hash capabilities, command %d pending\n",
763                         adapter->current_op);
764                 return;
765         }
766         adapter->current_op = VIRTCHNL_OP_GET_RSS_HENA_CAPS;
767         adapter->aq_required &= ~IAVF_FLAG_AQ_GET_HENA;
768         iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_RSS_HENA_CAPS, NULL, 0);
769 }
770
771 /**
772  * iavf_set_hena
773  * @adapter: adapter structure
774  *
775  * Request the PF to set our RSS hash capabilities
776  **/
777 void iavf_set_hena(struct iavf_adapter *adapter)
778 {
779         struct virtchnl_rss_hena vrh;
780
781         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
782                 /* bail because we already have a command pending */
783                 dev_err(&adapter->pdev->dev, "Cannot set RSS hash enable, command %d pending\n",
784                         adapter->current_op);
785                 return;
786         }
787         vrh.hena = adapter->hena;
788         adapter->current_op = VIRTCHNL_OP_SET_RSS_HENA;
789         adapter->aq_required &= ~IAVF_FLAG_AQ_SET_HENA;
790         iavf_send_pf_msg(adapter, VIRTCHNL_OP_SET_RSS_HENA, (u8 *)&vrh,
791                          sizeof(vrh));
792 }
793
794 /**
795  * iavf_set_rss_key
796  * @adapter: adapter structure
797  *
798  * Request the PF to set our RSS hash key
799  **/
800 void iavf_set_rss_key(struct iavf_adapter *adapter)
801 {
802         struct virtchnl_rss_key *vrk;
803         int len;
804
805         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
806                 /* bail because we already have a command pending */
807                 dev_err(&adapter->pdev->dev, "Cannot set RSS key, command %d pending\n",
808                         adapter->current_op);
809                 return;
810         }
811         len = sizeof(struct virtchnl_rss_key) +
812               (adapter->rss_key_size * sizeof(u8)) - 1;
813         vrk = kzalloc(len, GFP_KERNEL);
814         if (!vrk)
815                 return;
816         vrk->vsi_id = adapter->vsi.id;
817         vrk->key_len = adapter->rss_key_size;
818         memcpy(vrk->key, adapter->rss_key, adapter->rss_key_size);
819
820         adapter->current_op = VIRTCHNL_OP_CONFIG_RSS_KEY;
821         adapter->aq_required &= ~IAVF_FLAG_AQ_SET_RSS_KEY;
822         iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_RSS_KEY, (u8 *)vrk, len);
823         kfree(vrk);
824 }
825
826 /**
827  * iavf_set_rss_lut
828  * @adapter: adapter structure
829  *
830  * Request the PF to set our RSS lookup table
831  **/
832 void iavf_set_rss_lut(struct iavf_adapter *adapter)
833 {
834         struct virtchnl_rss_lut *vrl;
835         int len;
836
837         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
838                 /* bail because we already have a command pending */
839                 dev_err(&adapter->pdev->dev, "Cannot set RSS LUT, command %d pending\n",
840                         adapter->current_op);
841                 return;
842         }
843         len = sizeof(struct virtchnl_rss_lut) +
844               (adapter->rss_lut_size * sizeof(u8)) - 1;
845         vrl = kzalloc(len, GFP_KERNEL);
846         if (!vrl)
847                 return;
848         vrl->vsi_id = adapter->vsi.id;
849         vrl->lut_entries = adapter->rss_lut_size;
850         memcpy(vrl->lut, adapter->rss_lut, adapter->rss_lut_size);
851         adapter->current_op = VIRTCHNL_OP_CONFIG_RSS_LUT;
852         adapter->aq_required &= ~IAVF_FLAG_AQ_SET_RSS_LUT;
853         iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_RSS_LUT, (u8 *)vrl, len);
854         kfree(vrl);
855 }
856
857 /**
858  * iavf_enable_vlan_stripping
859  * @adapter: adapter structure
860  *
861  * Request VLAN header stripping to be enabled
862  **/
863 void iavf_enable_vlan_stripping(struct iavf_adapter *adapter)
864 {
865         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
866                 /* bail because we already have a command pending */
867                 dev_err(&adapter->pdev->dev, "Cannot enable stripping, command %d pending\n",
868                         adapter->current_op);
869                 return;
870         }
871         adapter->current_op = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
872         adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
873         iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING, NULL, 0);
874 }
875
876 /**
877  * iavf_disable_vlan_stripping
878  * @adapter: adapter structure
879  *
880  * Request VLAN header stripping to be disabled
881  **/
882 void iavf_disable_vlan_stripping(struct iavf_adapter *adapter)
883 {
884         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
885                 /* bail because we already have a command pending */
886                 dev_err(&adapter->pdev->dev, "Cannot disable stripping, command %d pending\n",
887                         adapter->current_op);
888                 return;
889         }
890         adapter->current_op = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
891         adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
892         iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING, NULL, 0);
893 }
894
895 #define IAVF_MAX_SPEED_STRLEN   13
896
897 /**
898  * iavf_print_link_message - print link up or down
899  * @adapter: adapter structure
900  *
901  * Log a message telling the world of our wonderous link status
902  */
903 static void iavf_print_link_message(struct iavf_adapter *adapter)
904 {
905         struct net_device *netdev = adapter->netdev;
906         int link_speed_mbps;
907         char *speed;
908
909         if (!adapter->link_up) {
910                 netdev_info(netdev, "NIC Link is Down\n");
911                 return;
912         }
913
914         speed = kcalloc(1, IAVF_MAX_SPEED_STRLEN, GFP_KERNEL);
915         if (!speed)
916                 return;
917
918         if (ADV_LINK_SUPPORT(adapter)) {
919                 link_speed_mbps = adapter->link_speed_mbps;
920                 goto print_link_msg;
921         }
922
923         switch (adapter->link_speed) {
924         case IAVF_LINK_SPEED_40GB:
925                 link_speed_mbps = SPEED_40000;
926                 break;
927         case IAVF_LINK_SPEED_25GB:
928                 link_speed_mbps = SPEED_25000;
929                 break;
930         case IAVF_LINK_SPEED_20GB:
931                 link_speed_mbps = SPEED_20000;
932                 break;
933         case IAVF_LINK_SPEED_10GB:
934                 link_speed_mbps = SPEED_10000;
935                 break;
936         case IAVF_LINK_SPEED_1GB:
937                 link_speed_mbps = SPEED_1000;
938                 break;
939         case IAVF_LINK_SPEED_100MB:
940                 link_speed_mbps = SPEED_100;
941                 break;
942         default:
943                 link_speed_mbps = SPEED_UNKNOWN;
944                 break;
945         }
946
947 print_link_msg:
948         if (link_speed_mbps > SPEED_1000) {
949                 if (link_speed_mbps == SPEED_2500)
950                         snprintf(speed, IAVF_MAX_SPEED_STRLEN, "2.5 Gbps");
951                 else
952                         /* convert to Gbps inline */
953                         snprintf(speed, IAVF_MAX_SPEED_STRLEN, "%d %s",
954                                  link_speed_mbps / 1000, "Gbps");
955         } else if (link_speed_mbps == SPEED_UNKNOWN) {
956                 snprintf(speed, IAVF_MAX_SPEED_STRLEN, "%s", "Unknown Mbps");
957         } else {
958                 snprintf(speed, IAVF_MAX_SPEED_STRLEN, "%u %s",
959                          link_speed_mbps, "Mbps");
960         }
961
962         netdev_info(netdev, "NIC Link is Up Speed is %s Full Duplex\n", speed);
963         kfree(speed);
964 }
965
966 /**
967  * iavf_get_vpe_link_status
968  * @adapter: adapter structure
969  * @vpe: virtchnl_pf_event structure
970  *
971  * Helper function for determining the link status
972  **/
973 static bool
974 iavf_get_vpe_link_status(struct iavf_adapter *adapter,
975                          struct virtchnl_pf_event *vpe)
976 {
977         if (ADV_LINK_SUPPORT(adapter))
978                 return vpe->event_data.link_event_adv.link_status;
979         else
980                 return vpe->event_data.link_event.link_status;
981 }
982
983 /**
984  * iavf_set_adapter_link_speed_from_vpe
985  * @adapter: adapter structure for which we are setting the link speed
986  * @vpe: virtchnl_pf_event structure that contains the link speed we are setting
987  *
988  * Helper function for setting iavf_adapter link speed
989  **/
990 static void
991 iavf_set_adapter_link_speed_from_vpe(struct iavf_adapter *adapter,
992                                      struct virtchnl_pf_event *vpe)
993 {
994         if (ADV_LINK_SUPPORT(adapter))
995                 adapter->link_speed_mbps =
996                         vpe->event_data.link_event_adv.link_speed;
997         else
998                 adapter->link_speed = vpe->event_data.link_event.link_speed;
999 }
1000
1001 /**
1002  * iavf_enable_channel
1003  * @adapter: adapter structure
1004  *
1005  * Request that the PF enable channels as specified by
1006  * the user via tc tool.
1007  **/
1008 void iavf_enable_channels(struct iavf_adapter *adapter)
1009 {
1010         struct virtchnl_tc_info *vti = NULL;
1011         size_t len;
1012         int i;
1013
1014         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1015                 /* bail because we already have a command pending */
1016                 dev_err(&adapter->pdev->dev, "Cannot configure mqprio, command %d pending\n",
1017                         adapter->current_op);
1018                 return;
1019         }
1020
1021         len = struct_size(vti, list, adapter->num_tc - 1);
1022         vti = kzalloc(len, GFP_KERNEL);
1023         if (!vti)
1024                 return;
1025         vti->num_tc = adapter->num_tc;
1026         for (i = 0; i < vti->num_tc; i++) {
1027                 vti->list[i].count = adapter->ch_config.ch_info[i].count;
1028                 vti->list[i].offset = adapter->ch_config.ch_info[i].offset;
1029                 vti->list[i].pad = 0;
1030                 vti->list[i].max_tx_rate =
1031                                 adapter->ch_config.ch_info[i].max_tx_rate;
1032         }
1033
1034         adapter->ch_config.state = __IAVF_TC_RUNNING;
1035         adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1036         adapter->current_op = VIRTCHNL_OP_ENABLE_CHANNELS;
1037         adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_CHANNELS;
1038         iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_CHANNELS, (u8 *)vti, len);
1039         kfree(vti);
1040 }
1041
1042 /**
1043  * iavf_disable_channel
1044  * @adapter: adapter structure
1045  *
1046  * Request that the PF disable channels that are configured
1047  **/
1048 void iavf_disable_channels(struct iavf_adapter *adapter)
1049 {
1050         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1051                 /* bail because we already have a command pending */
1052                 dev_err(&adapter->pdev->dev, "Cannot configure mqprio, command %d pending\n",
1053                         adapter->current_op);
1054                 return;
1055         }
1056
1057         adapter->ch_config.state = __IAVF_TC_INVALID;
1058         adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1059         adapter->current_op = VIRTCHNL_OP_DISABLE_CHANNELS;
1060         adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_CHANNELS;
1061         iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_CHANNELS, NULL, 0);
1062 }
1063
1064 /**
1065  * iavf_print_cloud_filter
1066  * @adapter: adapter structure
1067  * @f: cloud filter to print
1068  *
1069  * Print the cloud filter
1070  **/
1071 static void iavf_print_cloud_filter(struct iavf_adapter *adapter,
1072                                     struct virtchnl_filter *f)
1073 {
1074         switch (f->flow_type) {
1075         case VIRTCHNL_TCP_V4_FLOW:
1076                 dev_info(&adapter->pdev->dev, "dst_mac: %pM src_mac: %pM vlan_id: %hu dst_ip: %pI4 src_ip %pI4 dst_port %hu src_port %hu\n",
1077                          &f->data.tcp_spec.dst_mac,
1078                          &f->data.tcp_spec.src_mac,
1079                          ntohs(f->data.tcp_spec.vlan_id),
1080                          &f->data.tcp_spec.dst_ip[0],
1081                          &f->data.tcp_spec.src_ip[0],
1082                          ntohs(f->data.tcp_spec.dst_port),
1083                          ntohs(f->data.tcp_spec.src_port));
1084                 break;
1085         case VIRTCHNL_TCP_V6_FLOW:
1086                 dev_info(&adapter->pdev->dev, "dst_mac: %pM src_mac: %pM vlan_id: %hu dst_ip: %pI6 src_ip %pI6 dst_port %hu src_port %hu\n",
1087                          &f->data.tcp_spec.dst_mac,
1088                          &f->data.tcp_spec.src_mac,
1089                          ntohs(f->data.tcp_spec.vlan_id),
1090                          &f->data.tcp_spec.dst_ip,
1091                          &f->data.tcp_spec.src_ip,
1092                          ntohs(f->data.tcp_spec.dst_port),
1093                          ntohs(f->data.tcp_spec.src_port));
1094                 break;
1095         }
1096 }
1097
1098 /**
1099  * iavf_add_cloud_filter
1100  * @adapter: adapter structure
1101  *
1102  * Request that the PF add cloud filters as specified
1103  * by the user via tc tool.
1104  **/
1105 void iavf_add_cloud_filter(struct iavf_adapter *adapter)
1106 {
1107         struct iavf_cloud_filter *cf;
1108         struct virtchnl_filter *f;
1109         int len = 0, count = 0;
1110
1111         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1112                 /* bail because we already have a command pending */
1113                 dev_err(&adapter->pdev->dev, "Cannot add cloud filter, command %d pending\n",
1114                         adapter->current_op);
1115                 return;
1116         }
1117         list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1118                 if (cf->add) {
1119                         count++;
1120                         break;
1121                 }
1122         }
1123         if (!count) {
1124                 adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
1125                 return;
1126         }
1127         adapter->current_op = VIRTCHNL_OP_ADD_CLOUD_FILTER;
1128
1129         len = sizeof(struct virtchnl_filter);
1130         f = kzalloc(len, GFP_KERNEL);
1131         if (!f)
1132                 return;
1133
1134         list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1135                 if (cf->add) {
1136                         memcpy(f, &cf->f, sizeof(struct virtchnl_filter));
1137                         cf->add = false;
1138                         cf->state = __IAVF_CF_ADD_PENDING;
1139                         iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_CLOUD_FILTER,
1140                                          (u8 *)f, len);
1141                 }
1142         }
1143         kfree(f);
1144 }
1145
1146 /**
1147  * iavf_del_cloud_filter
1148  * @adapter: adapter structure
1149  *
1150  * Request that the PF delete cloud filters as specified
1151  * by the user via tc tool.
1152  **/
1153 void iavf_del_cloud_filter(struct iavf_adapter *adapter)
1154 {
1155         struct iavf_cloud_filter *cf, *cftmp;
1156         struct virtchnl_filter *f;
1157         int len = 0, count = 0;
1158
1159         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1160                 /* bail because we already have a command pending */
1161                 dev_err(&adapter->pdev->dev, "Cannot remove cloud filter, command %d pending\n",
1162                         adapter->current_op);
1163                 return;
1164         }
1165         list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1166                 if (cf->del) {
1167                         count++;
1168                         break;
1169                 }
1170         }
1171         if (!count) {
1172                 adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1173                 return;
1174         }
1175         adapter->current_op = VIRTCHNL_OP_DEL_CLOUD_FILTER;
1176
1177         len = sizeof(struct virtchnl_filter);
1178         f = kzalloc(len, GFP_KERNEL);
1179         if (!f)
1180                 return;
1181
1182         list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1183                 if (cf->del) {
1184                         memcpy(f, &cf->f, sizeof(struct virtchnl_filter));
1185                         cf->del = false;
1186                         cf->state = __IAVF_CF_DEL_PENDING;
1187                         iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_CLOUD_FILTER,
1188                                          (u8 *)f, len);
1189                 }
1190         }
1191         kfree(f);
1192 }
1193
1194 /**
1195  * iavf_request_reset
1196  * @adapter: adapter structure
1197  *
1198  * Request that the PF reset this VF. No response is expected.
1199  **/
1200 void iavf_request_reset(struct iavf_adapter *adapter)
1201 {
1202         /* Don't check CURRENT_OP - this is always higher priority */
1203         iavf_send_pf_msg(adapter, VIRTCHNL_OP_RESET_VF, NULL, 0);
1204         adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1205 }
1206
1207 /**
1208  * iavf_virtchnl_completion
1209  * @adapter: adapter structure
1210  * @v_opcode: opcode sent by PF
1211  * @v_retval: retval sent by PF
1212  * @msg: message sent by PF
1213  * @msglen: message length
1214  *
1215  * Asynchronous completion function for admin queue messages. Rather than busy
1216  * wait, we fire off our requests and assume that no errors will be returned.
1217  * This function handles the reply messages.
1218  **/
1219 void iavf_virtchnl_completion(struct iavf_adapter *adapter,
1220                               enum virtchnl_ops v_opcode,
1221                               enum iavf_status v_retval, u8 *msg, u16 msglen)
1222 {
1223         struct net_device *netdev = adapter->netdev;
1224
1225         if (v_opcode == VIRTCHNL_OP_EVENT) {
1226                 struct virtchnl_pf_event *vpe =
1227                         (struct virtchnl_pf_event *)msg;
1228                 bool link_up = iavf_get_vpe_link_status(adapter, vpe);
1229
1230                 switch (vpe->event) {
1231                 case VIRTCHNL_EVENT_LINK_CHANGE:
1232                         iavf_set_adapter_link_speed_from_vpe(adapter, vpe);
1233
1234                         /* we've already got the right link status, bail */
1235                         if (adapter->link_up == link_up)
1236                                 break;
1237
1238                         if (link_up) {
1239                                 /* If we get link up message and start queues
1240                                  * before our queues are configured it will
1241                                  * trigger a TX hang. In that case, just ignore
1242                                  * the link status message,we'll get another one
1243                                  * after we enable queues and actually prepared
1244                                  * to send traffic.
1245                                  */
1246                                 if (adapter->state != __IAVF_RUNNING)
1247                                         break;
1248
1249                                 /* For ADq enabled VF, we reconfigure VSIs and
1250                                  * re-allocate queues. Hence wait till all
1251                                  * queues are enabled.
1252                                  */
1253                                 if (adapter->flags &
1254                                     IAVF_FLAG_QUEUES_DISABLED)
1255                                         break;
1256                         }
1257
1258                         adapter->link_up = link_up;
1259                         if (link_up) {
1260                                 netif_tx_start_all_queues(netdev);
1261                                 netif_carrier_on(netdev);
1262                         } else {
1263                                 netif_tx_stop_all_queues(netdev);
1264                                 netif_carrier_off(netdev);
1265                         }
1266                         iavf_print_link_message(adapter);
1267                         break;
1268                 case VIRTCHNL_EVENT_RESET_IMPENDING:
1269                         dev_info(&adapter->pdev->dev, "Reset warning received from the PF\n");
1270                         if (!(adapter->flags & IAVF_FLAG_RESET_PENDING)) {
1271                                 adapter->flags |= IAVF_FLAG_RESET_PENDING;
1272                                 dev_info(&adapter->pdev->dev, "Scheduling reset task\n");
1273                                 queue_work(iavf_wq, &adapter->reset_task);
1274                         }
1275                         break;
1276                 default:
1277                         dev_err(&adapter->pdev->dev, "Unknown event %d from PF\n",
1278                                 vpe->event);
1279                         break;
1280                 }
1281                 return;
1282         }
1283         if (v_retval) {
1284                 switch (v_opcode) {
1285                 case VIRTCHNL_OP_ADD_VLAN:
1286                         dev_err(&adapter->pdev->dev, "Failed to add VLAN filter, error %s\n",
1287                                 iavf_stat_str(&adapter->hw, v_retval));
1288                         break;
1289                 case VIRTCHNL_OP_ADD_ETH_ADDR:
1290                         dev_err(&adapter->pdev->dev, "Failed to add MAC filter, error %s\n",
1291                                 iavf_stat_str(&adapter->hw, v_retval));
1292                         /* restore administratively set MAC address */
1293                         ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1294                         break;
1295                 case VIRTCHNL_OP_DEL_VLAN:
1296                         dev_err(&adapter->pdev->dev, "Failed to delete VLAN filter, error %s\n",
1297                                 iavf_stat_str(&adapter->hw, v_retval));
1298                         break;
1299                 case VIRTCHNL_OP_DEL_ETH_ADDR:
1300                         dev_err(&adapter->pdev->dev, "Failed to delete MAC filter, error %s\n",
1301                                 iavf_stat_str(&adapter->hw, v_retval));
1302                         break;
1303                 case VIRTCHNL_OP_ENABLE_CHANNELS:
1304                         dev_err(&adapter->pdev->dev, "Failed to configure queue channels, error %s\n",
1305                                 iavf_stat_str(&adapter->hw, v_retval));
1306                         adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1307                         adapter->ch_config.state = __IAVF_TC_INVALID;
1308                         netdev_reset_tc(netdev);
1309                         netif_tx_start_all_queues(netdev);
1310                         break;
1311                 case VIRTCHNL_OP_DISABLE_CHANNELS:
1312                         dev_err(&adapter->pdev->dev, "Failed to disable queue channels, error %s\n",
1313                                 iavf_stat_str(&adapter->hw, v_retval));
1314                         adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1315                         adapter->ch_config.state = __IAVF_TC_RUNNING;
1316                         netif_tx_start_all_queues(netdev);
1317                         break;
1318                 case VIRTCHNL_OP_ADD_CLOUD_FILTER: {
1319                         struct iavf_cloud_filter *cf, *cftmp;
1320
1321                         list_for_each_entry_safe(cf, cftmp,
1322                                                  &adapter->cloud_filter_list,
1323                                                  list) {
1324                                 if (cf->state == __IAVF_CF_ADD_PENDING) {
1325                                         cf->state = __IAVF_CF_INVALID;
1326                                         dev_info(&adapter->pdev->dev, "Failed to add cloud filter, error %s\n",
1327                                                  iavf_stat_str(&adapter->hw,
1328                                                                v_retval));
1329                                         iavf_print_cloud_filter(adapter,
1330                                                                 &cf->f);
1331                                         list_del(&cf->list);
1332                                         kfree(cf);
1333                                         adapter->num_cloud_filters--;
1334                                 }
1335                         }
1336                         }
1337                         break;
1338                 case VIRTCHNL_OP_DEL_CLOUD_FILTER: {
1339                         struct iavf_cloud_filter *cf;
1340
1341                         list_for_each_entry(cf, &adapter->cloud_filter_list,
1342                                             list) {
1343                                 if (cf->state == __IAVF_CF_DEL_PENDING) {
1344                                         cf->state = __IAVF_CF_ACTIVE;
1345                                         dev_info(&adapter->pdev->dev, "Failed to del cloud filter, error %s\n",
1346                                                  iavf_stat_str(&adapter->hw,
1347                                                                v_retval));
1348                                         iavf_print_cloud_filter(adapter,
1349                                                                 &cf->f);
1350                                 }
1351                         }
1352                         }
1353                         break;
1354                 default:
1355                         dev_err(&adapter->pdev->dev, "PF returned error %d (%s) to our request %d\n",
1356                                 v_retval, iavf_stat_str(&adapter->hw, v_retval),
1357                                 v_opcode);
1358                 }
1359         }
1360         switch (v_opcode) {
1361         case VIRTCHNL_OP_ADD_ETH_ADDR: {
1362                 if (!ether_addr_equal(netdev->dev_addr, adapter->hw.mac.addr))
1363                         ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
1364                 }
1365                 break;
1366         case VIRTCHNL_OP_GET_STATS: {
1367                 struct iavf_eth_stats *stats =
1368                         (struct iavf_eth_stats *)msg;
1369                 netdev->stats.rx_packets = stats->rx_unicast +
1370                                            stats->rx_multicast +
1371                                            stats->rx_broadcast;
1372                 netdev->stats.tx_packets = stats->tx_unicast +
1373                                            stats->tx_multicast +
1374                                            stats->tx_broadcast;
1375                 netdev->stats.rx_bytes = stats->rx_bytes;
1376                 netdev->stats.tx_bytes = stats->tx_bytes;
1377                 netdev->stats.tx_errors = stats->tx_errors;
1378                 netdev->stats.rx_dropped = stats->rx_discards;
1379                 netdev->stats.tx_dropped = stats->tx_discards;
1380                 adapter->current_stats = *stats;
1381                 }
1382                 break;
1383         case VIRTCHNL_OP_GET_VF_RESOURCES: {
1384                 u16 len = sizeof(struct virtchnl_vf_resource) +
1385                           IAVF_MAX_VF_VSI *
1386                           sizeof(struct virtchnl_vsi_resource);
1387                 memcpy(adapter->vf_res, msg, min(msglen, len));
1388                 iavf_validate_num_queues(adapter);
1389                 iavf_vf_parse_hw_config(&adapter->hw, adapter->vf_res);
1390                 if (is_zero_ether_addr(adapter->hw.mac.addr)) {
1391                         /* restore current mac address */
1392                         ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1393                 } else {
1394                         /* refresh current mac address if changed */
1395                         ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
1396                         ether_addr_copy(netdev->perm_addr,
1397                                         adapter->hw.mac.addr);
1398                 }
1399                 spin_lock_bh(&adapter->mac_vlan_list_lock);
1400                 iavf_add_filter(adapter, adapter->hw.mac.addr);
1401                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1402                 iavf_process_config(adapter);
1403                 }
1404                 break;
1405         case VIRTCHNL_OP_ENABLE_QUEUES:
1406                 /* enable transmits */
1407                 iavf_irq_enable(adapter, true);
1408                 adapter->flags &= ~IAVF_FLAG_QUEUES_DISABLED;
1409                 break;
1410         case VIRTCHNL_OP_DISABLE_QUEUES:
1411                 iavf_free_all_tx_resources(adapter);
1412                 iavf_free_all_rx_resources(adapter);
1413                 if (adapter->state == __IAVF_DOWN_PENDING) {
1414                         adapter->state = __IAVF_DOWN;
1415                         wake_up(&adapter->down_waitqueue);
1416                 }
1417                 break;
1418         case VIRTCHNL_OP_VERSION:
1419         case VIRTCHNL_OP_CONFIG_IRQ_MAP:
1420                 /* Don't display an error if we get these out of sequence.
1421                  * If the firmware needed to get kicked, we'll get these and
1422                  * it's no problem.
1423                  */
1424                 if (v_opcode != adapter->current_op)
1425                         return;
1426                 break;
1427         case VIRTCHNL_OP_IWARP:
1428                 /* Gobble zero-length replies from the PF. They indicate that
1429                  * a previous message was received OK, and the client doesn't
1430                  * care about that.
1431                  */
1432                 if (msglen && CLIENT_ENABLED(adapter))
1433                         iavf_notify_client_message(&adapter->vsi, msg, msglen);
1434                 break;
1435
1436         case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
1437                 adapter->client_pending &=
1438                                 ~(BIT(VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP));
1439                 break;
1440         case VIRTCHNL_OP_GET_RSS_HENA_CAPS: {
1441                 struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
1442
1443                 if (msglen == sizeof(*vrh))
1444                         adapter->hena = vrh->hena;
1445                 else
1446                         dev_warn(&adapter->pdev->dev,
1447                                  "Invalid message %d from PF\n", v_opcode);
1448                 }
1449                 break;
1450         case VIRTCHNL_OP_REQUEST_QUEUES: {
1451                 struct virtchnl_vf_res_request *vfres =
1452                         (struct virtchnl_vf_res_request *)msg;
1453
1454                 if (vfres->num_queue_pairs != adapter->num_req_queues) {
1455                         dev_info(&adapter->pdev->dev,
1456                                  "Requested %d queues, PF can support %d\n",
1457                                  adapter->num_req_queues,
1458                                  vfres->num_queue_pairs);
1459                         adapter->num_req_queues = 0;
1460                         adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1461                 }
1462                 }
1463                 break;
1464         case VIRTCHNL_OP_ADD_CLOUD_FILTER: {
1465                 struct iavf_cloud_filter *cf;
1466
1467                 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1468                         if (cf->state == __IAVF_CF_ADD_PENDING)
1469                                 cf->state = __IAVF_CF_ACTIVE;
1470                 }
1471                 }
1472                 break;
1473         case VIRTCHNL_OP_DEL_CLOUD_FILTER: {
1474                 struct iavf_cloud_filter *cf, *cftmp;
1475
1476                 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
1477                                          list) {
1478                         if (cf->state == __IAVF_CF_DEL_PENDING) {
1479                                 cf->state = __IAVF_CF_INVALID;
1480                                 list_del(&cf->list);
1481                                 kfree(cf);
1482                                 adapter->num_cloud_filters--;
1483                         }
1484                 }
1485                 }
1486                 break;
1487         default:
1488                 if (adapter->current_op && (v_opcode != adapter->current_op))
1489                         dev_warn(&adapter->pdev->dev, "Expected response %d from PF, received %d\n",
1490                                  adapter->current_op, v_opcode);
1491                 break;
1492         } /* switch v_opcode */
1493         adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1494 }