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