Merge tag 'v3.17-rc4' into sched/core, to prevent conflicts with upcoming patches...
[cascardo/linux.git] / drivers / net / ethernet / intel / i40evf / i40evf_virtchnl.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
4  * Copyright(c) 2013 - 2014 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26
27 #include "i40evf.h"
28 #include "i40e_prototype.h"
29
30 /* busy wait delay in msec */
31 #define I40EVF_BUSY_WAIT_DELAY 10
32 #define I40EVF_BUSY_WAIT_COUNT 50
33
34 /**
35  * i40evf_send_pf_msg
36  * @adapter: adapter structure
37  * @op: virtual channel opcode
38  * @msg: pointer to message buffer
39  * @len: message length
40  *
41  * Send message to PF and print status if failure.
42  **/
43 static int i40evf_send_pf_msg(struct i40evf_adapter *adapter,
44                               enum i40e_virtchnl_ops op, u8 *msg, u16 len)
45 {
46         struct i40e_hw *hw = &adapter->hw;
47         i40e_status err;
48
49         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
50                 return 0; /* nothing to see here, move along */
51
52         err = i40e_aq_send_msg_to_pf(hw, op, 0, msg, len, NULL);
53         if (err)
54                 dev_err(&adapter->pdev->dev, "Unable to send opcode %d to PF, error %d, aq status %d\n",
55                         op, err, hw->aq.asq_last_status);
56         return err;
57 }
58
59 /**
60  * i40evf_send_api_ver
61  * @adapter: adapter structure
62  *
63  * Send API version admin queue message to the PF. The reply is not checked
64  * in this function. Returns 0 if the message was successfully
65  * sent, or one of the I40E_ADMIN_QUEUE_ERROR_ statuses if not.
66  **/
67 int i40evf_send_api_ver(struct i40evf_adapter *adapter)
68 {
69         struct i40e_virtchnl_version_info vvi;
70
71         vvi.major = I40E_VIRTCHNL_VERSION_MAJOR;
72         vvi.minor = I40E_VIRTCHNL_VERSION_MINOR;
73
74         return i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_VERSION, (u8 *)&vvi,
75                                   sizeof(vvi));
76 }
77
78 /**
79  * i40evf_verify_api_ver
80  * @adapter: adapter structure
81  *
82  * Compare API versions with the PF. Must be called after admin queue is
83  * initialized. Returns 0 if API versions match, -EIO if they do not,
84  * I40E_ERR_ADMIN_QUEUE_NO_WORK if the admin queue is empty, and any errors
85  * from the firmware are propagated.
86  **/
87 int i40evf_verify_api_ver(struct i40evf_adapter *adapter)
88 {
89         struct i40e_virtchnl_version_info *pf_vvi;
90         struct i40e_hw *hw = &adapter->hw;
91         struct i40e_arq_event_info event;
92         i40e_status err;
93
94         event.msg_size = I40EVF_MAX_AQ_BUF_SIZE;
95         event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
96         if (!event.msg_buf) {
97                 err = -ENOMEM;
98                 goto out;
99         }
100
101         err = i40evf_clean_arq_element(hw, &event, NULL);
102         if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
103                 goto out_alloc;
104
105         err = (i40e_status)le32_to_cpu(event.desc.cookie_low);
106         if (err)
107                 goto out_alloc;
108
109         if ((enum i40e_virtchnl_ops)le32_to_cpu(event.desc.cookie_high) !=
110             I40E_VIRTCHNL_OP_VERSION) {
111                 dev_info(&adapter->pdev->dev, "Invalid reply type %d from PF\n",
112                          le32_to_cpu(event.desc.cookie_high));
113                 err = -EIO;
114                 goto out_alloc;
115         }
116
117         pf_vvi = (struct i40e_virtchnl_version_info *)event.msg_buf;
118         if ((pf_vvi->major != I40E_VIRTCHNL_VERSION_MAJOR) ||
119             (pf_vvi->minor != I40E_VIRTCHNL_VERSION_MINOR))
120                 err = -EIO;
121
122 out_alloc:
123         kfree(event.msg_buf);
124 out:
125         return err;
126 }
127
128 /**
129  * i40evf_send_vf_config_msg
130  * @adapter: adapter structure
131  *
132  * Send VF configuration request admin queue message to the PF. The reply
133  * is not checked in this function. Returns 0 if the message was
134  * successfully sent, or one of the I40E_ADMIN_QUEUE_ERROR_ statuses if not.
135  **/
136 int i40evf_send_vf_config_msg(struct i40evf_adapter *adapter)
137 {
138         return i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
139                                   NULL, 0);
140 }
141
142 /**
143  * i40evf_get_vf_config
144  * @hw: pointer to the hardware structure
145  * @len: length of buffer
146  *
147  * Get VF configuration from PF and populate hw structure. Must be called after
148  * admin queue is initialized. Busy waits until response is received from PF,
149  * with maximum timeout. Response from PF is returned in the buffer for further
150  * processing by the caller.
151  **/
152 int i40evf_get_vf_config(struct i40evf_adapter *adapter)
153 {
154         struct i40e_hw *hw = &adapter->hw;
155         struct i40e_arq_event_info event;
156         u16 len;
157         i40e_status err;
158
159         len =  sizeof(struct i40e_virtchnl_vf_resource) +
160                 I40E_MAX_VF_VSI * sizeof(struct i40e_virtchnl_vsi_resource);
161         event.msg_size = len;
162         event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
163         if (!event.msg_buf) {
164                 err = -ENOMEM;
165                 goto out;
166         }
167
168         err = i40evf_clean_arq_element(hw, &event, NULL);
169         if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
170                 goto out_alloc;
171
172         err = (i40e_status)le32_to_cpu(event.desc.cookie_low);
173         if (err) {
174                 dev_err(&adapter->pdev->dev,
175                         "%s: Error returned from PF, %d, %d\n", __func__,
176                         le32_to_cpu(event.desc.cookie_high),
177                         le32_to_cpu(event.desc.cookie_low));
178                 err = -EIO;
179                 goto out_alloc;
180         }
181
182         if ((enum i40e_virtchnl_ops)le32_to_cpu(event.desc.cookie_high) !=
183             I40E_VIRTCHNL_OP_GET_VF_RESOURCES) {
184                 dev_err(&adapter->pdev->dev,
185                         "%s: Invalid response from PF, %d, %d\n", __func__,
186                         le32_to_cpu(event.desc.cookie_high),
187                         le32_to_cpu(event.desc.cookie_low));
188                 err = -EIO;
189                 goto out_alloc;
190         }
191         memcpy(adapter->vf_res, event.msg_buf, min(event.msg_size, len));
192
193         i40e_vf_parse_hw_config(hw, adapter->vf_res);
194 out_alloc:
195         kfree(event.msg_buf);
196 out:
197         return err;
198 }
199
200 /**
201  * i40evf_configure_queues
202  * @adapter: adapter structure
203  *
204  * Request that the PF set up our (previously allocated) queues.
205  **/
206 void i40evf_configure_queues(struct i40evf_adapter *adapter)
207 {
208         struct i40e_virtchnl_vsi_queue_config_info *vqci;
209         struct i40e_virtchnl_queue_pair_info *vqpi;
210         int pairs = adapter->vsi_res->num_queue_pairs;
211         int i, len;
212
213         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
214                 /* bail because we already have a command pending */
215                 dev_err(&adapter->pdev->dev, "%s: command %d pending\n",
216                         __func__, adapter->current_op);
217                 return;
218         }
219         adapter->current_op = I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES;
220         len = sizeof(struct i40e_virtchnl_vsi_queue_config_info) +
221                        (sizeof(struct i40e_virtchnl_queue_pair_info) * pairs);
222         vqci = kzalloc(len, GFP_ATOMIC);
223         if (!vqci)
224                 return;
225
226         vqci->vsi_id = adapter->vsi_res->vsi_id;
227         vqci->num_queue_pairs = pairs;
228         vqpi = vqci->qpair;
229         /* Size check is not needed here - HW max is 16 queue pairs, and we
230          * can fit info for 31 of them into the AQ buffer before it overflows.
231          */
232         for (i = 0; i < pairs; i++) {
233                 vqpi->txq.vsi_id = vqci->vsi_id;
234                 vqpi->txq.queue_id = i;
235                 vqpi->txq.ring_len = adapter->tx_rings[i]->count;
236                 vqpi->txq.dma_ring_addr = adapter->tx_rings[i]->dma;
237                 vqpi->txq.headwb_enabled = 1;
238                 vqpi->txq.dma_headwb_addr = vqpi->txq.dma_ring_addr +
239                     (vqpi->txq.ring_len * sizeof(struct i40e_tx_desc));
240
241                 vqpi->rxq.vsi_id = vqci->vsi_id;
242                 vqpi->rxq.queue_id = i;
243                 vqpi->rxq.ring_len = adapter->rx_rings[i]->count;
244                 vqpi->rxq.dma_ring_addr = adapter->rx_rings[i]->dma;
245                 vqpi->rxq.max_pkt_size = adapter->netdev->mtu
246                                         + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
247                 vqpi->rxq.databuffer_size = adapter->rx_rings[i]->rx_buf_len;
248                 vqpi++;
249         }
250
251         adapter->aq_pending |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
252         adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
253         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
254                            (u8 *)vqci, len);
255         kfree(vqci);
256 }
257
258 /**
259  * i40evf_enable_queues
260  * @adapter: adapter structure
261  *
262  * Request that the PF enable all of our queues.
263  **/
264 void i40evf_enable_queues(struct i40evf_adapter *adapter)
265 {
266         struct i40e_virtchnl_queue_select vqs;
267
268         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
269                 /* bail because we already have a command pending */
270                 dev_err(&adapter->pdev->dev, "%s: command %d pending\n",
271                         __func__, adapter->current_op);
272                 return;
273         }
274         adapter->current_op = I40E_VIRTCHNL_OP_ENABLE_QUEUES;
275         vqs.vsi_id = adapter->vsi_res->vsi_id;
276         vqs.tx_queues = (1 << adapter->vsi_res->num_queue_pairs) - 1;
277         vqs.rx_queues = vqs.tx_queues;
278         adapter->aq_pending |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
279         adapter->aq_required &= ~I40EVF_FLAG_AQ_ENABLE_QUEUES;
280         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
281                            (u8 *)&vqs, sizeof(vqs));
282 }
283
284 /**
285  * i40evf_disable_queues
286  * @adapter: adapter structure
287  *
288  * Request that the PF disable all of our queues.
289  **/
290 void i40evf_disable_queues(struct i40evf_adapter *adapter)
291 {
292         struct i40e_virtchnl_queue_select vqs;
293
294         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
295                 /* bail because we already have a command pending */
296                 dev_err(&adapter->pdev->dev, "%s: command %d pending\n",
297                         __func__, adapter->current_op);
298                 return;
299         }
300         adapter->current_op = I40E_VIRTCHNL_OP_DISABLE_QUEUES;
301         vqs.vsi_id = adapter->vsi_res->vsi_id;
302         vqs.tx_queues = (1 << adapter->vsi_res->num_queue_pairs) - 1;
303         vqs.rx_queues = vqs.tx_queues;
304         adapter->aq_pending |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
305         adapter->aq_required &= ~I40EVF_FLAG_AQ_DISABLE_QUEUES;
306         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
307                            (u8 *)&vqs, sizeof(vqs));
308 }
309
310 /**
311  * i40evf_map_queues
312  * @adapter: adapter structure
313  *
314  * Request that the PF map queues to interrupt vectors. Misc causes, including
315  * admin queue, are always mapped to vector 0.
316  **/
317 void i40evf_map_queues(struct i40evf_adapter *adapter)
318 {
319         struct i40e_virtchnl_irq_map_info *vimi;
320         int v_idx, q_vectors, len;
321         struct i40e_q_vector *q_vector;
322
323         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
324                 /* bail because we already have a command pending */
325                 dev_err(&adapter->pdev->dev, "%s: command %d pending\n",
326                         __func__, adapter->current_op);
327                 return;
328         }
329         adapter->current_op = I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP;
330
331         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
332
333         len = sizeof(struct i40e_virtchnl_irq_map_info) +
334               (adapter->num_msix_vectors *
335                 sizeof(struct i40e_virtchnl_vector_map));
336         vimi = kzalloc(len, GFP_ATOMIC);
337         if (!vimi)
338                 return;
339
340         vimi->num_vectors = adapter->num_msix_vectors;
341         /* Queue vectors first */
342         for (v_idx = 0; v_idx < q_vectors; v_idx++) {
343                 q_vector = adapter->q_vector[v_idx];
344                 vimi->vecmap[v_idx].vsi_id = adapter->vsi_res->vsi_id;
345                 vimi->vecmap[v_idx].vector_id = v_idx + NONQ_VECS;
346                 vimi->vecmap[v_idx].txq_map = q_vector->ring_mask;
347                 vimi->vecmap[v_idx].rxq_map = q_vector->ring_mask;
348         }
349         /* Misc vector last - this is only for AdminQ messages */
350         vimi->vecmap[v_idx].vsi_id = adapter->vsi_res->vsi_id;
351         vimi->vecmap[v_idx].vector_id = 0;
352         vimi->vecmap[v_idx].txq_map = 0;
353         vimi->vecmap[v_idx].rxq_map = 0;
354
355         adapter->aq_pending |= I40EVF_FLAG_AQ_MAP_VECTORS;
356         adapter->aq_required &= ~I40EVF_FLAG_AQ_MAP_VECTORS;
357         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
358                            (u8 *)vimi, len);
359         kfree(vimi);
360 }
361
362 /**
363  * i40evf_add_ether_addrs
364  * @adapter: adapter structure
365  * @addrs: the MAC address filters to add (contiguous)
366  * @count: number of filters
367  *
368  * Request that the PF add one or more addresses to our filters.
369  **/
370 void i40evf_add_ether_addrs(struct i40evf_adapter *adapter)
371 {
372         struct i40e_virtchnl_ether_addr_list *veal;
373         int len, i = 0, count = 0;
374         struct i40evf_mac_filter *f;
375
376         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
377                 /* bail because we already have a command pending */
378                 dev_err(&adapter->pdev->dev, "%s: command %d pending\n",
379                         __func__, adapter->current_op);
380                 return;
381         }
382         list_for_each_entry(f, &adapter->mac_filter_list, list) {
383                 if (f->add)
384                         count++;
385         }
386         if (!count) {
387                 adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_MAC_FILTER;
388                 return;
389         }
390         adapter->current_op = I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS;
391
392         len = sizeof(struct i40e_virtchnl_ether_addr_list) +
393               (count * sizeof(struct i40e_virtchnl_ether_addr));
394         if (len > I40EVF_MAX_AQ_BUF_SIZE) {
395                 dev_warn(&adapter->pdev->dev, "%s: Too many MAC address changes in one request\n",
396                         __func__);
397                 count = (I40EVF_MAX_AQ_BUF_SIZE -
398                          sizeof(struct i40e_virtchnl_ether_addr_list)) /
399                         sizeof(struct i40e_virtchnl_ether_addr);
400                 len = I40EVF_MAX_AQ_BUF_SIZE;
401         }
402
403         veal = kzalloc(len, GFP_ATOMIC);
404         if (!veal)
405                 return;
406
407         veal->vsi_id = adapter->vsi_res->vsi_id;
408         veal->num_elements = count;
409         list_for_each_entry(f, &adapter->mac_filter_list, list) {
410                 if (f->add) {
411                         ether_addr_copy(veal->list[i].addr, f->macaddr);
412                         i++;
413                         f->add = false;
414                 }
415         }
416         adapter->aq_pending |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
417         adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_MAC_FILTER;
418         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
419                            (u8 *)veal, len);
420         kfree(veal);
421 }
422
423 /**
424  * i40evf_del_ether_addrs
425  * @adapter: adapter structure
426  * @addrs: the MAC address filters to remove (contiguous)
427  * @count: number of filtes
428  *
429  * Request that the PF remove one or more addresses from our filters.
430  **/
431 void i40evf_del_ether_addrs(struct i40evf_adapter *adapter)
432 {
433         struct i40e_virtchnl_ether_addr_list *veal;
434         struct i40evf_mac_filter *f, *ftmp;
435         int len, i = 0, count = 0;
436
437         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
438                 /* bail because we already have a command pending */
439                 dev_err(&adapter->pdev->dev, "%s: command %d pending\n",
440                         __func__, adapter->current_op);
441                 return;
442         }
443         list_for_each_entry(f, &adapter->mac_filter_list, list) {
444                 if (f->remove)
445                         count++;
446         }
447         if (!count) {
448                 adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_MAC_FILTER;
449                 return;
450         }
451         adapter->current_op = I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS;
452
453         len = sizeof(struct i40e_virtchnl_ether_addr_list) +
454               (count * sizeof(struct i40e_virtchnl_ether_addr));
455         if (len > I40EVF_MAX_AQ_BUF_SIZE) {
456                 dev_warn(&adapter->pdev->dev, "%s: Too many MAC address changes in one request\n",
457                         __func__);
458                 count = (I40EVF_MAX_AQ_BUF_SIZE -
459                          sizeof(struct i40e_virtchnl_ether_addr_list)) /
460                         sizeof(struct i40e_virtchnl_ether_addr);
461                 len = I40EVF_MAX_AQ_BUF_SIZE;
462         }
463         veal = kzalloc(len, GFP_ATOMIC);
464         if (!veal)
465                 return;
466
467         veal->vsi_id = adapter->vsi_res->vsi_id;
468         veal->num_elements = count;
469         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
470                 if (f->remove) {
471                         ether_addr_copy(veal->list[i].addr, f->macaddr);
472                         i++;
473                         list_del(&f->list);
474                         kfree(f);
475                 }
476         }
477         adapter->aq_pending |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
478         adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_MAC_FILTER;
479         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
480                            (u8 *)veal, len);
481         kfree(veal);
482 }
483
484 /**
485  * i40evf_add_vlans
486  * @adapter: adapter structure
487  * @vlans: the VLANs to add
488  * @count: number of VLANs
489  *
490  * Request that the PF add one or more VLAN filters to our VSI.
491  **/
492 void i40evf_add_vlans(struct i40evf_adapter *adapter)
493 {
494         struct i40e_virtchnl_vlan_filter_list *vvfl;
495         int len, i = 0, count = 0;
496         struct i40evf_vlan_filter *f;
497
498         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
499                 /* bail because we already have a command pending */
500                 dev_err(&adapter->pdev->dev, "%s: command %d pending\n",
501                         __func__, adapter->current_op);
502                 return;
503         }
504
505         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
506                 if (f->add)
507                         count++;
508         }
509         if (!count) {
510                 adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
511                 return;
512         }
513         adapter->current_op = I40E_VIRTCHNL_OP_ADD_VLAN;
514
515         len = sizeof(struct i40e_virtchnl_vlan_filter_list) +
516               (count * sizeof(u16));
517         if (len > I40EVF_MAX_AQ_BUF_SIZE) {
518                 dev_warn(&adapter->pdev->dev, "%s: Too many VLAN changes in one request\n",
519                         __func__);
520                 count = (I40EVF_MAX_AQ_BUF_SIZE -
521                          sizeof(struct i40e_virtchnl_vlan_filter_list)) /
522                         sizeof(u16);
523                 len = I40EVF_MAX_AQ_BUF_SIZE;
524         }
525         vvfl = kzalloc(len, GFP_ATOMIC);
526         if (!vvfl)
527                 return;
528
529         vvfl->vsi_id = adapter->vsi_res->vsi_id;
530         vvfl->num_elements = count;
531         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
532                 if (f->add) {
533                         vvfl->vlan_id[i] = f->vlan;
534                         i++;
535                         f->add = false;
536                 }
537         }
538         adapter->aq_pending |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
539         adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
540         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_ADD_VLAN, (u8 *)vvfl, len);
541         kfree(vvfl);
542 }
543
544 /**
545  * i40evf_del_vlans
546  * @adapter: adapter structure
547  * @vlans: the VLANs to remove
548  * @count: number of VLANs
549  *
550  * Request that the PF remove one or more VLAN filters from our VSI.
551  **/
552 void i40evf_del_vlans(struct i40evf_adapter *adapter)
553 {
554         struct i40e_virtchnl_vlan_filter_list *vvfl;
555         struct i40evf_vlan_filter *f, *ftmp;
556         int len, i = 0, count = 0;
557
558         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
559                 /* bail because we already have a command pending */
560                 dev_err(&adapter->pdev->dev, "%s: command %d pending\n",
561                         __func__, adapter->current_op);
562                 return;
563         }
564
565         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
566                 if (f->remove)
567                         count++;
568         }
569         if (!count) {
570                 adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
571                 return;
572         }
573         adapter->current_op = I40E_VIRTCHNL_OP_DEL_VLAN;
574
575         len = sizeof(struct i40e_virtchnl_vlan_filter_list) +
576               (count * sizeof(u16));
577         if (len > I40EVF_MAX_AQ_BUF_SIZE) {
578                 dev_warn(&adapter->pdev->dev, "%s: Too many VLAN changes in one request\n",
579                         __func__);
580                 count = (I40EVF_MAX_AQ_BUF_SIZE -
581                          sizeof(struct i40e_virtchnl_vlan_filter_list)) /
582                         sizeof(u16);
583                 len = I40EVF_MAX_AQ_BUF_SIZE;
584         }
585         vvfl = kzalloc(len, GFP_ATOMIC);
586         if (!vvfl)
587                 return;
588
589         vvfl->vsi_id = adapter->vsi_res->vsi_id;
590         vvfl->num_elements = count;
591         list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
592                 if (f->remove) {
593                         vvfl->vlan_id[i] = f->vlan;
594                         i++;
595                         list_del(&f->list);
596                         kfree(f);
597                 }
598         }
599         adapter->aq_pending |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
600         adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
601         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_DEL_VLAN, (u8 *)vvfl, len);
602         kfree(vvfl);
603 }
604
605 /**
606  * i40evf_set_promiscuous
607  * @adapter: adapter structure
608  * @flags: bitmask to control unicast/multicast promiscuous.
609  *
610  * Request that the PF enable promiscuous mode for our VSI.
611  **/
612 void i40evf_set_promiscuous(struct i40evf_adapter *adapter, int flags)
613 {
614         struct i40e_virtchnl_promisc_info vpi;
615
616         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
617                 /* bail because we already have a command pending */
618                 dev_err(&adapter->pdev->dev, "%s: command %d pending\n",
619                         __func__, adapter->current_op);
620                 return;
621         }
622         adapter->current_op = I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
623         vpi.vsi_id = adapter->vsi_res->vsi_id;
624         vpi.flags = flags;
625         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
626                            (u8 *)&vpi, sizeof(vpi));
627 }
628
629 /**
630  * i40evf_request_stats
631  * @adapter: adapter structure
632  *
633  * Request VSI statistics from PF.
634  **/
635 void i40evf_request_stats(struct i40evf_adapter *adapter)
636 {
637         struct i40e_virtchnl_queue_select vqs;
638         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
639                 /* no error message, this isn't crucial */
640                 return;
641         }
642         adapter->current_op = I40E_VIRTCHNL_OP_GET_STATS;
643         vqs.vsi_id = adapter->vsi_res->vsi_id;
644         /* queue maps are ignored for this message - only the vsi is used */
645         if (i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_GET_STATS,
646                                (u8 *)&vqs, sizeof(vqs)))
647                 /* if the request failed, don't lock out others */
648                 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
649 }
650 /**
651  * i40evf_request_reset
652  * @adapter: adapter structure
653  *
654  * Request that the PF reset this VF. No response is expected.
655  **/
656 void i40evf_request_reset(struct i40evf_adapter *adapter)
657 {
658         /* Don't check CURRENT_OP - this is always higher priority */
659         i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_RESET_VF, NULL, 0);
660         adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
661 }
662
663 /**
664  * i40evf_virtchnl_completion
665  * @adapter: adapter structure
666  * @v_opcode: opcode sent by PF
667  * @v_retval: retval sent by PF
668  * @msg: message sent by PF
669  * @msglen: message length
670  *
671  * Asynchronous completion function for admin queue messages. Rather than busy
672  * wait, we fire off our requests and assume that no errors will be returned.
673  * This function handles the reply messages.
674  **/
675 void i40evf_virtchnl_completion(struct i40evf_adapter *adapter,
676                                 enum i40e_virtchnl_ops v_opcode,
677                                 i40e_status v_retval,
678                                 u8 *msg, u16 msglen)
679 {
680         struct net_device *netdev = adapter->netdev;
681
682         if (v_opcode == I40E_VIRTCHNL_OP_EVENT) {
683                 struct i40e_virtchnl_pf_event *vpe =
684                         (struct i40e_virtchnl_pf_event *)msg;
685                 switch (vpe->event) {
686                 case I40E_VIRTCHNL_EVENT_LINK_CHANGE:
687                         adapter->link_up =
688                                 vpe->event_data.link_event.link_status;
689                         if (adapter->link_up && !netif_carrier_ok(netdev)) {
690                                 dev_info(&adapter->pdev->dev, "NIC Link is Up\n");
691                                 netif_carrier_on(netdev);
692                                 netif_tx_wake_all_queues(netdev);
693                         } else if (!adapter->link_up) {
694                                 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
695                                 netif_carrier_off(netdev);
696                                 netif_tx_stop_all_queues(netdev);
697                         }
698                         break;
699                 case I40E_VIRTCHNL_EVENT_RESET_IMPENDING:
700                         dev_info(&adapter->pdev->dev, "PF reset warning received\n");
701                         if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING)) {
702                                 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
703                                 dev_info(&adapter->pdev->dev, "Scheduling reset task\n");
704                                 schedule_work(&adapter->reset_task);
705                         }
706                         break;
707                 default:
708                         dev_err(&adapter->pdev->dev,
709                                 "%s: Unknown event %d from pf\n",
710                                 __func__, vpe->event);
711                         break;
712
713                 }
714                 return;
715         }
716         if (v_opcode != adapter->current_op) {
717                 dev_err(&adapter->pdev->dev, "%s: Pending op is %d, received %d\n",
718                         __func__, adapter->current_op, v_opcode);
719                 /* We're probably completely screwed at this point, but clear
720                  * the current op and try to carry on....
721                  */
722                 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
723                 return;
724         }
725         if (v_retval) {
726                 dev_err(&adapter->pdev->dev, "%s: PF returned error %d to our request %d\n",
727                         __func__, v_retval, v_opcode);
728         }
729         switch (v_opcode) {
730         case I40E_VIRTCHNL_OP_GET_STATS: {
731                 struct i40e_eth_stats *stats =
732                         (struct i40e_eth_stats *)msg;
733                 adapter->net_stats.rx_packets = stats->rx_unicast +
734                                                  stats->rx_multicast +
735                                                  stats->rx_broadcast;
736                 adapter->net_stats.tx_packets = stats->tx_unicast +
737                                                  stats->tx_multicast +
738                                                  stats->tx_broadcast;
739                 adapter->net_stats.rx_bytes = stats->rx_bytes;
740                 adapter->net_stats.tx_bytes = stats->tx_bytes;
741                 adapter->net_stats.tx_errors = stats->tx_errors;
742                 adapter->net_stats.rx_dropped = stats->rx_discards;
743                 adapter->net_stats.tx_dropped = stats->tx_discards;
744                 adapter->current_stats = *stats;
745                 }
746                 break;
747         case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
748                 adapter->aq_pending &= ~(I40EVF_FLAG_AQ_ADD_MAC_FILTER);
749                 break;
750         case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
751                 adapter->aq_pending &= ~(I40EVF_FLAG_AQ_DEL_MAC_FILTER);
752                 break;
753         case I40E_VIRTCHNL_OP_ADD_VLAN:
754                 adapter->aq_pending &= ~(I40EVF_FLAG_AQ_ADD_VLAN_FILTER);
755                 break;
756         case I40E_VIRTCHNL_OP_DEL_VLAN:
757                 adapter->aq_pending &= ~(I40EVF_FLAG_AQ_DEL_VLAN_FILTER);
758                 break;
759         case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
760                 adapter->aq_pending &= ~(I40EVF_FLAG_AQ_ENABLE_QUEUES);
761                 /* enable transmits */
762                 i40evf_irq_enable(adapter, true);
763                 netif_tx_start_all_queues(adapter->netdev);
764                 netif_carrier_on(adapter->netdev);
765                 break;
766         case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
767                 adapter->aq_pending &= ~(I40EVF_FLAG_AQ_DISABLE_QUEUES);
768                 break;
769         case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
770                 adapter->aq_pending &= ~(I40EVF_FLAG_AQ_CONFIGURE_QUEUES);
771                 break;
772         case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
773                 adapter->aq_pending &= ~(I40EVF_FLAG_AQ_MAP_VECTORS);
774                 break;
775         default:
776                 dev_warn(&adapter->pdev->dev, "%s: Received unexpected message %d from PF\n",
777                         __func__, v_opcode);
778                 break;
779         } /* switch v_opcode */
780         adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
781 }