]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/intel/i40evf/i40evf_main.c
Merge git://git.infradead.org/intel-iommu
[karo-tx-linux.git] / drivers / net / ethernet / intel / i40evf / i40evf_main.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
4  * Copyright(c) 2013 - 2015 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 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
30 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
31 static int i40evf_close(struct net_device *netdev);
32
33 char i40evf_driver_name[] = "i40evf";
34 static const char i40evf_driver_string[] =
35         "Intel(R) XL710/X710 Virtual Function Network Driver";
36
37 #define DRV_VERSION "1.3.5"
38 const char i40evf_driver_version[] = DRV_VERSION;
39 static const char i40evf_copyright[] =
40         "Copyright (c) 2013 - 2015 Intel Corporation.";
41
42 /* i40evf_pci_tbl - PCI Device ID Table
43  *
44  * Wildcard entries (PCI_ANY_ID) should come last
45  * Last entry must be all 0s
46  *
47  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
48  *   Class, Class Mask, private data (not used) }
49  */
50 static const struct pci_device_id i40evf_pci_tbl[] = {
51         {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
52         {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
53         /* required last entry */
54         {0, }
55 };
56
57 MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
58
59 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
60 MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
61 MODULE_LICENSE("GPL");
62 MODULE_VERSION(DRV_VERSION);
63
64 /**
65  * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
66  * @hw:   pointer to the HW structure
67  * @mem:  ptr to mem struct to fill out
68  * @size: size of memory requested
69  * @alignment: what to align the allocation to
70  **/
71 i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
72                                       struct i40e_dma_mem *mem,
73                                       u64 size, u32 alignment)
74 {
75         struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
76
77         if (!mem)
78                 return I40E_ERR_PARAM;
79
80         mem->size = ALIGN(size, alignment);
81         mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
82                                      (dma_addr_t *)&mem->pa, GFP_KERNEL);
83         if (mem->va)
84                 return 0;
85         else
86                 return I40E_ERR_NO_MEMORY;
87 }
88
89 /**
90  * i40evf_free_dma_mem_d - OS specific memory free for shared code
91  * @hw:   pointer to the HW structure
92  * @mem:  ptr to mem struct to free
93  **/
94 i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
95 {
96         struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
97
98         if (!mem || !mem->va)
99                 return I40E_ERR_PARAM;
100         dma_free_coherent(&adapter->pdev->dev, mem->size,
101                           mem->va, (dma_addr_t)mem->pa);
102         return 0;
103 }
104
105 /**
106  * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
107  * @hw:   pointer to the HW structure
108  * @mem:  ptr to mem struct to fill out
109  * @size: size of memory requested
110  **/
111 i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
112                                        struct i40e_virt_mem *mem, u32 size)
113 {
114         if (!mem)
115                 return I40E_ERR_PARAM;
116
117         mem->size = size;
118         mem->va = kzalloc(size, GFP_KERNEL);
119
120         if (mem->va)
121                 return 0;
122         else
123                 return I40E_ERR_NO_MEMORY;
124 }
125
126 /**
127  * i40evf_free_virt_mem_d - OS specific memory free for shared code
128  * @hw:   pointer to the HW structure
129  * @mem:  ptr to mem struct to free
130  **/
131 i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
132                                    struct i40e_virt_mem *mem)
133 {
134         if (!mem)
135                 return I40E_ERR_PARAM;
136
137         /* it's ok to kfree a NULL pointer */
138         kfree(mem->va);
139
140         return 0;
141 }
142
143 /**
144  * i40evf_debug_d - OS dependent version of debug printing
145  * @hw:  pointer to the HW structure
146  * @mask: debug level mask
147  * @fmt_str: printf-type format description
148  **/
149 void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
150 {
151         char buf[512];
152         va_list argptr;
153
154         if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
155                 return;
156
157         va_start(argptr, fmt_str);
158         vsnprintf(buf, sizeof(buf), fmt_str, argptr);
159         va_end(argptr);
160
161         /* the debug string is already formatted with a newline */
162         pr_info("%s", buf);
163 }
164
165 /**
166  * i40evf_tx_timeout - Respond to a Tx Hang
167  * @netdev: network interface device structure
168  **/
169 static void i40evf_tx_timeout(struct net_device *netdev)
170 {
171         struct i40evf_adapter *adapter = netdev_priv(netdev);
172
173         adapter->tx_timeout_count++;
174         if (!(adapter->flags & (I40EVF_FLAG_RESET_PENDING |
175                                 I40EVF_FLAG_RESET_NEEDED))) {
176                 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
177                 schedule_work(&adapter->reset_task);
178         }
179 }
180
181 /**
182  * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
183  * @adapter: board private structure
184  **/
185 static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
186 {
187         struct i40e_hw *hw = &adapter->hw;
188
189         wr32(hw, I40E_VFINT_DYN_CTL01, 0);
190
191         /* read flush */
192         rd32(hw, I40E_VFGEN_RSTAT);
193
194         synchronize_irq(adapter->msix_entries[0].vector);
195 }
196
197 /**
198  * i40evf_misc_irq_enable - Enable default interrupt generation settings
199  * @adapter: board private structure
200  **/
201 static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
202 {
203         struct i40e_hw *hw = &adapter->hw;
204
205         wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
206                                        I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
207         wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
208
209         /* read flush */
210         rd32(hw, I40E_VFGEN_RSTAT);
211 }
212
213 /**
214  * i40evf_irq_disable - Mask off interrupt generation on the NIC
215  * @adapter: board private structure
216  **/
217 static void i40evf_irq_disable(struct i40evf_adapter *adapter)
218 {
219         int i;
220         struct i40e_hw *hw = &adapter->hw;
221
222         if (!adapter->msix_entries)
223                 return;
224
225         for (i = 1; i < adapter->num_msix_vectors; i++) {
226                 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
227                 synchronize_irq(adapter->msix_entries[i].vector);
228         }
229         /* read flush */
230         rd32(hw, I40E_VFGEN_RSTAT);
231 }
232
233 /**
234  * i40evf_irq_enable_queues - Enable interrupt for specified queues
235  * @adapter: board private structure
236  * @mask: bitmap of queues to enable
237  **/
238 void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
239 {
240         struct i40e_hw *hw = &adapter->hw;
241         int i;
242
243         for (i = 1; i < adapter->num_msix_vectors; i++) {
244                 if (mask & BIT(i - 1)) {
245                         wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
246                              I40E_VFINT_DYN_CTLN1_INTENA_MASK |
247                              I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
248                              I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
249                 }
250         }
251 }
252
253 /**
254  * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
255  * @adapter: board private structure
256  * @mask: bitmap of vectors to trigger
257  **/
258 static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
259 {
260         struct i40e_hw *hw = &adapter->hw;
261         int i;
262         uint32_t dyn_ctl;
263
264         if (mask & 1) {
265                 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
266                 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
267                            I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
268                            I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
269                 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
270         }
271         for (i = 1; i < adapter->num_msix_vectors; i++) {
272                 if (mask & BIT(i)) {
273                         dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
274                         dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
275                                    I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
276                                    I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
277                         wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
278                 }
279         }
280 }
281
282 /**
283  * i40evf_irq_enable - Enable default interrupt generation settings
284  * @adapter: board private structure
285  **/
286 void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
287 {
288         struct i40e_hw *hw = &adapter->hw;
289
290         i40evf_misc_irq_enable(adapter);
291         i40evf_irq_enable_queues(adapter, ~0);
292
293         if (flush)
294                 rd32(hw, I40E_VFGEN_RSTAT);
295 }
296
297 /**
298  * i40evf_msix_aq - Interrupt handler for vector 0
299  * @irq: interrupt number
300  * @data: pointer to netdev
301  **/
302 static irqreturn_t i40evf_msix_aq(int irq, void *data)
303 {
304         struct net_device *netdev = data;
305         struct i40evf_adapter *adapter = netdev_priv(netdev);
306         struct i40e_hw *hw = &adapter->hw;
307         u32 val;
308         u32 ena_mask;
309
310         /* handle non-queue interrupts */
311         val = rd32(hw, I40E_VFINT_ICR01);
312         ena_mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
313
314
315         val = rd32(hw, I40E_VFINT_DYN_CTL01);
316         val = val | I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
317         wr32(hw, I40E_VFINT_DYN_CTL01, val);
318
319         /* schedule work on the private workqueue */
320         schedule_work(&adapter->adminq_task);
321
322         return IRQ_HANDLED;
323 }
324
325 /**
326  * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
327  * @irq: interrupt number
328  * @data: pointer to a q_vector
329  **/
330 static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
331 {
332         struct i40e_q_vector *q_vector = data;
333
334         if (!q_vector->tx.ring && !q_vector->rx.ring)
335                 return IRQ_HANDLED;
336
337         napi_schedule(&q_vector->napi);
338
339         return IRQ_HANDLED;
340 }
341
342 /**
343  * i40evf_map_vector_to_rxq - associate irqs with rx queues
344  * @adapter: board private structure
345  * @v_idx: interrupt number
346  * @r_idx: queue number
347  **/
348 static void
349 i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
350 {
351         struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
352         struct i40e_ring *rx_ring = adapter->rx_rings[r_idx];
353
354         rx_ring->q_vector = q_vector;
355         rx_ring->next = q_vector->rx.ring;
356         rx_ring->vsi = &adapter->vsi;
357         q_vector->rx.ring = rx_ring;
358         q_vector->rx.count++;
359         q_vector->rx.latency_range = I40E_LOW_LATENCY;
360 }
361
362 /**
363  * i40evf_map_vector_to_txq - associate irqs with tx queues
364  * @adapter: board private structure
365  * @v_idx: interrupt number
366  * @t_idx: queue number
367  **/
368 static void
369 i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
370 {
371         struct i40e_q_vector *q_vector = adapter->q_vector[v_idx];
372         struct i40e_ring *tx_ring = adapter->tx_rings[t_idx];
373
374         tx_ring->q_vector = q_vector;
375         tx_ring->next = q_vector->tx.ring;
376         tx_ring->vsi = &adapter->vsi;
377         q_vector->tx.ring = tx_ring;
378         q_vector->tx.count++;
379         q_vector->tx.latency_range = I40E_LOW_LATENCY;
380         q_vector->num_ringpairs++;
381         q_vector->ring_mask |= BIT(t_idx);
382 }
383
384 /**
385  * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
386  * @adapter: board private structure to initialize
387  *
388  * This function maps descriptor rings to the queue-specific vectors
389  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
390  * one vector per ring/queue, but on a constrained vector budget, we
391  * group the rings as "efficiently" as possible.  You would add new
392  * mapping configurations in here.
393  **/
394 static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
395 {
396         int q_vectors;
397         int v_start = 0;
398         int rxr_idx = 0, txr_idx = 0;
399         int rxr_remaining = adapter->num_active_queues;
400         int txr_remaining = adapter->num_active_queues;
401         int i, j;
402         int rqpv, tqpv;
403         int err = 0;
404
405         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
406
407         /* The ideal configuration...
408          * We have enough vectors to map one per queue.
409          */
410         if (q_vectors >= (rxr_remaining * 2)) {
411                 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
412                         i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
413
414                 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
415                         i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
416                 goto out;
417         }
418
419         /* If we don't have enough vectors for a 1-to-1
420          * mapping, we'll have to group them so there are
421          * multiple queues per vector.
422          * Re-adjusting *qpv takes care of the remainder.
423          */
424         for (i = v_start; i < q_vectors; i++) {
425                 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
426                 for (j = 0; j < rqpv; j++) {
427                         i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
428                         rxr_idx++;
429                         rxr_remaining--;
430                 }
431         }
432         for (i = v_start; i < q_vectors; i++) {
433                 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
434                 for (j = 0; j < tqpv; j++) {
435                         i40evf_map_vector_to_txq(adapter, i, txr_idx);
436                         txr_idx++;
437                         txr_remaining--;
438                 }
439         }
440
441 out:
442         adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
443
444         return err;
445 }
446
447 /**
448  * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
449  * @adapter: board private structure
450  *
451  * Allocates MSI-X vectors for tx and rx handling, and requests
452  * interrupts from the kernel.
453  **/
454 static int
455 i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
456 {
457         int vector, err, q_vectors;
458         int rx_int_idx = 0, tx_int_idx = 0;
459
460         i40evf_irq_disable(adapter);
461         /* Decrement for Other and TCP Timer vectors */
462         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
463
464         for (vector = 0; vector < q_vectors; vector++) {
465                 struct i40e_q_vector *q_vector = adapter->q_vector[vector];
466
467                 if (q_vector->tx.ring && q_vector->rx.ring) {
468                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
469                                  "i40evf-%s-%s-%d", basename,
470                                  "TxRx", rx_int_idx++);
471                         tx_int_idx++;
472                 } else if (q_vector->rx.ring) {
473                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
474                                  "i40evf-%s-%s-%d", basename,
475                                  "rx", rx_int_idx++);
476                 } else if (q_vector->tx.ring) {
477                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
478                                  "i40evf-%s-%s-%d", basename,
479                                  "tx", tx_int_idx++);
480                 } else {
481                         /* skip this unused q_vector */
482                         continue;
483                 }
484                 err = request_irq(
485                         adapter->msix_entries[vector + NONQ_VECS].vector,
486                         i40evf_msix_clean_rings,
487                         0,
488                         q_vector->name,
489                         q_vector);
490                 if (err) {
491                         dev_info(&adapter->pdev->dev,
492                                  "%s: request_irq failed, error: %d\n",
493                                 __func__, err);
494                         goto free_queue_irqs;
495                 }
496                 /* assign the mask for this irq */
497                 irq_set_affinity_hint(
498                         adapter->msix_entries[vector + NONQ_VECS].vector,
499                         q_vector->affinity_mask);
500         }
501
502         return 0;
503
504 free_queue_irqs:
505         while (vector) {
506                 vector--;
507                 irq_set_affinity_hint(
508                         adapter->msix_entries[vector + NONQ_VECS].vector,
509                         NULL);
510                 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
511                          adapter->q_vector[vector]);
512         }
513         return err;
514 }
515
516 /**
517  * i40evf_request_misc_irq - Initialize MSI-X interrupts
518  * @adapter: board private structure
519  *
520  * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
521  * vector is only for the admin queue, and stays active even when the netdev
522  * is closed.
523  **/
524 static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
525 {
526         struct net_device *netdev = adapter->netdev;
527         int err;
528
529         snprintf(adapter->misc_vector_name,
530                  sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
531                  dev_name(&adapter->pdev->dev));
532         err = request_irq(adapter->msix_entries[0].vector,
533                           &i40evf_msix_aq, 0,
534                           adapter->misc_vector_name, netdev);
535         if (err) {
536                 dev_err(&adapter->pdev->dev,
537                         "request_irq for %s failed: %d\n",
538                         adapter->misc_vector_name, err);
539                 free_irq(adapter->msix_entries[0].vector, netdev);
540         }
541         return err;
542 }
543
544 /**
545  * i40evf_free_traffic_irqs - Free MSI-X interrupts
546  * @adapter: board private structure
547  *
548  * Frees all MSI-X vectors other than 0.
549  **/
550 static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
551 {
552         int i;
553         int q_vectors;
554
555         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
556
557         for (i = 0; i < q_vectors; i++) {
558                 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
559                                       NULL);
560                 free_irq(adapter->msix_entries[i+1].vector,
561                          adapter->q_vector[i]);
562         }
563 }
564
565 /**
566  * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
567  * @adapter: board private structure
568  *
569  * Frees MSI-X vector 0.
570  **/
571 static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
572 {
573         struct net_device *netdev = adapter->netdev;
574
575         free_irq(adapter->msix_entries[0].vector, netdev);
576 }
577
578 /**
579  * i40evf_configure_tx - Configure Transmit Unit after Reset
580  * @adapter: board private structure
581  *
582  * Configure the Tx unit of the MAC after a reset.
583  **/
584 static void i40evf_configure_tx(struct i40evf_adapter *adapter)
585 {
586         struct i40e_hw *hw = &adapter->hw;
587         int i;
588
589         for (i = 0; i < adapter->num_active_queues; i++)
590                 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
591 }
592
593 /**
594  * i40evf_configure_rx - Configure Receive Unit after Reset
595  * @adapter: board private structure
596  *
597  * Configure the Rx unit of the MAC after a reset.
598  **/
599 static void i40evf_configure_rx(struct i40evf_adapter *adapter)
600 {
601         struct i40e_hw *hw = &adapter->hw;
602         struct net_device *netdev = adapter->netdev;
603         int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
604         int i;
605         int rx_buf_len;
606
607
608         adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
609         adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
610
611         /* Decide whether to use packet split mode or not */
612         if (netdev->mtu > ETH_DATA_LEN) {
613                 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
614                         adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
615                 else
616                         adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
617         } else {
618                 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
619                         adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
620                 else
621                         adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
622         }
623
624         /* Set the RX buffer length according to the mode */
625         if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
626                 rx_buf_len = I40E_RX_HDR_SIZE;
627         } else {
628                 if (netdev->mtu <= ETH_DATA_LEN)
629                         rx_buf_len = I40EVF_RXBUFFER_2048;
630                 else
631                         rx_buf_len = ALIGN(max_frame, 1024);
632         }
633
634         for (i = 0; i < adapter->num_active_queues; i++) {
635                 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
636                 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
637         }
638 }
639
640 /**
641  * i40evf_find_vlan - Search filter list for specific vlan filter
642  * @adapter: board private structure
643  * @vlan: vlan tag
644  *
645  * Returns ptr to the filter object or NULL
646  **/
647 static struct
648 i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
649 {
650         struct i40evf_vlan_filter *f;
651
652         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
653                 if (vlan == f->vlan)
654                         return f;
655         }
656         return NULL;
657 }
658
659 /**
660  * i40evf_add_vlan - Add a vlan filter to the list
661  * @adapter: board private structure
662  * @vlan: VLAN tag
663  *
664  * Returns ptr to the filter object or NULL when no memory available.
665  **/
666 static struct
667 i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
668 {
669         struct i40evf_vlan_filter *f = NULL;
670         int count = 50;
671
672         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
673                                 &adapter->crit_section)) {
674                 udelay(1);
675                 if (--count == 0)
676                         goto out;
677         }
678
679         f = i40evf_find_vlan(adapter, vlan);
680         if (!f) {
681                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
682                 if (!f)
683                         goto clearout;
684
685                 f->vlan = vlan;
686
687                 INIT_LIST_HEAD(&f->list);
688                 list_add(&f->list, &adapter->vlan_filter_list);
689                 f->add = true;
690                 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
691         }
692
693 clearout:
694         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
695 out:
696         return f;
697 }
698
699 /**
700  * i40evf_del_vlan - Remove a vlan filter from the list
701  * @adapter: board private structure
702  * @vlan: VLAN tag
703  **/
704 static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
705 {
706         struct i40evf_vlan_filter *f;
707         int count = 50;
708
709         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
710                                 &adapter->crit_section)) {
711                 udelay(1);
712                 if (--count == 0)
713                         return;
714         }
715
716         f = i40evf_find_vlan(adapter, vlan);
717         if (f) {
718                 f->remove = true;
719                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
720         }
721         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
722 }
723
724 /**
725  * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
726  * @netdev: network device struct
727  * @vid: VLAN tag
728  **/
729 static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
730                                   __always_unused __be16 proto, u16 vid)
731 {
732         struct i40evf_adapter *adapter = netdev_priv(netdev);
733
734         if (i40evf_add_vlan(adapter, vid) == NULL)
735                 return -ENOMEM;
736         return 0;
737 }
738
739 /**
740  * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
741  * @netdev: network device struct
742  * @vid: VLAN tag
743  **/
744 static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
745                                    __always_unused __be16 proto, u16 vid)
746 {
747         struct i40evf_adapter *adapter = netdev_priv(netdev);
748
749         i40evf_del_vlan(adapter, vid);
750         return 0;
751 }
752
753 /**
754  * i40evf_find_filter - Search filter list for specific mac filter
755  * @adapter: board private structure
756  * @macaddr: the MAC address
757  *
758  * Returns ptr to the filter object or NULL
759  **/
760 static struct
761 i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
762                                       u8 *macaddr)
763 {
764         struct i40evf_mac_filter *f;
765
766         if (!macaddr)
767                 return NULL;
768
769         list_for_each_entry(f, &adapter->mac_filter_list, list) {
770                 if (ether_addr_equal(macaddr, f->macaddr))
771                         return f;
772         }
773         return NULL;
774 }
775
776 /**
777  * i40e_add_filter - Add a mac filter to the filter list
778  * @adapter: board private structure
779  * @macaddr: the MAC address
780  *
781  * Returns ptr to the filter object or NULL when no memory available.
782  **/
783 static struct
784 i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
785                                      u8 *macaddr)
786 {
787         struct i40evf_mac_filter *f;
788         int count = 50;
789
790         if (!macaddr)
791                 return NULL;
792
793         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
794                                 &adapter->crit_section)) {
795                 udelay(1);
796                 if (--count == 0)
797                         return NULL;
798         }
799
800         f = i40evf_find_filter(adapter, macaddr);
801         if (!f) {
802                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
803                 if (!f) {
804                         clear_bit(__I40EVF_IN_CRITICAL_TASK,
805                                   &adapter->crit_section);
806                         return NULL;
807                 }
808
809                 ether_addr_copy(f->macaddr, macaddr);
810
811                 list_add(&f->list, &adapter->mac_filter_list);
812                 f->add = true;
813                 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
814         }
815
816         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
817         return f;
818 }
819
820 /**
821  * i40evf_set_mac - NDO callback to set port mac address
822  * @netdev: network interface device structure
823  * @p: pointer to an address structure
824  *
825  * Returns 0 on success, negative on failure
826  **/
827 static int i40evf_set_mac(struct net_device *netdev, void *p)
828 {
829         struct i40evf_adapter *adapter = netdev_priv(netdev);
830         struct i40e_hw *hw = &adapter->hw;
831         struct i40evf_mac_filter *f;
832         struct sockaddr *addr = p;
833
834         if (!is_valid_ether_addr(addr->sa_data))
835                 return -EADDRNOTAVAIL;
836
837         if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
838                 return 0;
839
840         f = i40evf_add_filter(adapter, addr->sa_data);
841         if (f) {
842                 ether_addr_copy(hw->mac.addr, addr->sa_data);
843                 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
844         }
845
846         return (f == NULL) ? -ENOMEM : 0;
847 }
848
849 /**
850  * i40evf_set_rx_mode - NDO callback to set the netdev filters
851  * @netdev: network interface device structure
852  **/
853 static void i40evf_set_rx_mode(struct net_device *netdev)
854 {
855         struct i40evf_adapter *adapter = netdev_priv(netdev);
856         struct i40evf_mac_filter *f, *ftmp;
857         struct netdev_hw_addr *uca;
858         struct netdev_hw_addr *mca;
859         int count = 50;
860
861         /* add addr if not already in the filter list */
862         netdev_for_each_uc_addr(uca, netdev) {
863                 i40evf_add_filter(adapter, uca->addr);
864         }
865         netdev_for_each_mc_addr(mca, netdev) {
866                 i40evf_add_filter(adapter, mca->addr);
867         }
868
869         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
870                                 &adapter->crit_section)) {
871                 udelay(1);
872                 if (--count == 0) {
873                         dev_err(&adapter->pdev->dev,
874                                 "Failed to get lock in %s\n", __func__);
875                         return;
876                 }
877         }
878         /* remove filter if not in netdev list */
879         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
880                 bool found = false;
881
882                 if (is_multicast_ether_addr(f->macaddr)) {
883                         netdev_for_each_mc_addr(mca, netdev) {
884                                 if (ether_addr_equal(mca->addr, f->macaddr)) {
885                                         found = true;
886                                         break;
887                                 }
888                         }
889                 } else {
890                         netdev_for_each_uc_addr(uca, netdev) {
891                                 if (ether_addr_equal(uca->addr, f->macaddr)) {
892                                         found = true;
893                                         break;
894                                 }
895                         }
896                         if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
897                                 found = true;
898                 }
899                 if (!found) {
900                         f->remove = true;
901                         adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
902                 }
903         }
904         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
905 }
906
907 /**
908  * i40evf_napi_enable_all - enable NAPI on all queue vectors
909  * @adapter: board private structure
910  **/
911 static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
912 {
913         int q_idx;
914         struct i40e_q_vector *q_vector;
915         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
916
917         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
918                 struct napi_struct *napi;
919
920                 q_vector = adapter->q_vector[q_idx];
921                 napi = &q_vector->napi;
922                 napi_enable(napi);
923         }
924 }
925
926 /**
927  * i40evf_napi_disable_all - disable NAPI on all queue vectors
928  * @adapter: board private structure
929  **/
930 static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
931 {
932         int q_idx;
933         struct i40e_q_vector *q_vector;
934         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
935
936         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
937                 q_vector = adapter->q_vector[q_idx];
938                 napi_disable(&q_vector->napi);
939         }
940 }
941
942 /**
943  * i40evf_configure - set up transmit and receive data structures
944  * @adapter: board private structure
945  **/
946 static void i40evf_configure(struct i40evf_adapter *adapter)
947 {
948         struct net_device *netdev = adapter->netdev;
949         int i;
950
951         i40evf_set_rx_mode(netdev);
952
953         i40evf_configure_tx(adapter);
954         i40evf_configure_rx(adapter);
955         adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
956
957         for (i = 0; i < adapter->num_active_queues; i++) {
958                 struct i40e_ring *ring = adapter->rx_rings[i];
959
960                 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
961                 ring->next_to_use = ring->count - 1;
962                 writel(ring->next_to_use, ring->tail);
963         }
964 }
965
966 /**
967  * i40evf_up_complete - Finish the last steps of bringing up a connection
968  * @adapter: board private structure
969  **/
970 static int i40evf_up_complete(struct i40evf_adapter *adapter)
971 {
972         adapter->state = __I40EVF_RUNNING;
973         clear_bit(__I40E_DOWN, &adapter->vsi.state);
974
975         i40evf_napi_enable_all(adapter);
976
977         adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
978         mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
979         return 0;
980 }
981
982 /**
983  * i40e_down - Shutdown the connection processing
984  * @adapter: board private structure
985  **/
986 void i40evf_down(struct i40evf_adapter *adapter)
987 {
988         struct net_device *netdev = adapter->netdev;
989         struct i40evf_mac_filter *f;
990
991         if (adapter->state == __I40EVF_DOWN)
992                 return;
993
994         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
995                                 &adapter->crit_section))
996                 usleep_range(500, 1000);
997
998         netif_carrier_off(netdev);
999         netif_tx_disable(netdev);
1000         i40evf_napi_disable_all(adapter);
1001         i40evf_irq_disable(adapter);
1002
1003         /* remove all MAC filters */
1004         list_for_each_entry(f, &adapter->mac_filter_list, list) {
1005                 f->remove = true;
1006         }
1007         /* remove all VLAN filters */
1008         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1009                 f->remove = true;
1010         }
1011         if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1012             adapter->state != __I40EVF_RESETTING) {
1013                 /* cancel any current operation */
1014                 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1015                 /* Schedule operations to close down the HW. Don't wait
1016                  * here for this to complete. The watchdog is still running
1017                  * and it will take care of this.
1018                  */
1019                 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1020                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1021                 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1022         }
1023
1024         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1025 }
1026
1027 /**
1028  * i40evf_acquire_msix_vectors - Setup the MSIX capability
1029  * @adapter: board private structure
1030  * @vectors: number of vectors to request
1031  *
1032  * Work with the OS to set up the MSIX vectors needed.
1033  *
1034  * Returns 0 on success, negative on failure
1035  **/
1036 static int
1037 i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1038 {
1039         int err, vector_threshold;
1040
1041         /* We'll want at least 3 (vector_threshold):
1042          * 0) Other (Admin Queue and link, mostly)
1043          * 1) TxQ[0] Cleanup
1044          * 2) RxQ[0] Cleanup
1045          */
1046         vector_threshold = MIN_MSIX_COUNT;
1047
1048         /* The more we get, the more we will assign to Tx/Rx Cleanup
1049          * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1050          * Right now, we simply care about how many we'll get; we'll
1051          * set them up later while requesting irq's.
1052          */
1053         err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1054                                     vector_threshold, vectors);
1055         if (err < 0) {
1056                 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1057                 kfree(adapter->msix_entries);
1058                 adapter->msix_entries = NULL;
1059                 return err;
1060         }
1061
1062         /* Adjust for only the vectors we'll use, which is minimum
1063          * of max_msix_q_vectors + NONQ_VECS, or the number of
1064          * vectors we were allocated.
1065          */
1066         adapter->num_msix_vectors = err;
1067         return 0;
1068 }
1069
1070 /**
1071  * i40evf_free_queues - Free memory for all rings
1072  * @adapter: board private structure to initialize
1073  *
1074  * Free all of the memory associated with queue pairs.
1075  **/
1076 static void i40evf_free_queues(struct i40evf_adapter *adapter)
1077 {
1078         int i;
1079
1080         if (!adapter->vsi_res)
1081                 return;
1082         for (i = 0; i < adapter->num_active_queues; i++) {
1083                 if (adapter->tx_rings[i])
1084                         kfree_rcu(adapter->tx_rings[i], rcu);
1085                 adapter->tx_rings[i] = NULL;
1086                 adapter->rx_rings[i] = NULL;
1087         }
1088 }
1089
1090 /**
1091  * i40evf_alloc_queues - Allocate memory for all rings
1092  * @adapter: board private structure to initialize
1093  *
1094  * We allocate one ring per queue at run-time since we don't know the
1095  * number of queues at compile-time.  The polling_netdev array is
1096  * intended for Multiqueue, but should work fine with a single queue.
1097  **/
1098 static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1099 {
1100         int i;
1101
1102         for (i = 0; i < adapter->num_active_queues; i++) {
1103                 struct i40e_ring *tx_ring;
1104                 struct i40e_ring *rx_ring;
1105
1106                 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
1107                 if (!tx_ring)
1108                         goto err_out;
1109
1110                 tx_ring->queue_index = i;
1111                 tx_ring->netdev = adapter->netdev;
1112                 tx_ring->dev = &adapter->pdev->dev;
1113                 tx_ring->count = adapter->tx_desc_count;
1114                 adapter->tx_rings[i] = tx_ring;
1115
1116                 rx_ring = &tx_ring[1];
1117                 rx_ring->queue_index = i;
1118                 rx_ring->netdev = adapter->netdev;
1119                 rx_ring->dev = &adapter->pdev->dev;
1120                 rx_ring->count = adapter->rx_desc_count;
1121                 adapter->rx_rings[i] = rx_ring;
1122         }
1123
1124         return 0;
1125
1126 err_out:
1127         i40evf_free_queues(adapter);
1128         return -ENOMEM;
1129 }
1130
1131 /**
1132  * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1133  * @adapter: board private structure to initialize
1134  *
1135  * Attempt to configure the interrupts using the best available
1136  * capabilities of the hardware and the kernel.
1137  **/
1138 static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1139 {
1140         int vector, v_budget;
1141         int pairs = 0;
1142         int err = 0;
1143
1144         if (!adapter->vsi_res) {
1145                 err = -EIO;
1146                 goto out;
1147         }
1148         pairs = adapter->num_active_queues;
1149
1150         /* It's easy to be greedy for MSI-X vectors, but it really
1151          * doesn't do us much good if we have a lot more vectors
1152          * than CPU's.  So let's be conservative and only ask for
1153          * (roughly) twice the number of vectors as there are CPU's.
1154          */
1155         v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1156         v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
1157
1158         adapter->msix_entries = kcalloc(v_budget,
1159                                         sizeof(struct msix_entry), GFP_KERNEL);
1160         if (!adapter->msix_entries) {
1161                 err = -ENOMEM;
1162                 goto out;
1163         }
1164
1165         for (vector = 0; vector < v_budget; vector++)
1166                 adapter->msix_entries[vector].entry = vector;
1167
1168         i40evf_acquire_msix_vectors(adapter, v_budget);
1169
1170 out:
1171         adapter->netdev->real_num_tx_queues = pairs;
1172         return err;
1173 }
1174
1175 /**
1176  * i40e_configure_rss_aq - Prepare for RSS using AQ commands
1177  * @vsi: vsi structure
1178  * @seed: RSS hash seed
1179  **/
1180 static void i40evf_configure_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
1181 {
1182         struct i40e_aqc_get_set_rss_key_data rss_key;
1183         struct i40evf_adapter *adapter = vsi->back;
1184         struct i40e_hw *hw = &adapter->hw;
1185         int ret = 0, i;
1186         u8 *rss_lut;
1187
1188         if (!vsi->id)
1189                 return;
1190
1191         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1192                 /* bail because we already have a command pending */
1193                 dev_err(&adapter->pdev->dev, "Cannot confiure RSS, command %d pending\n",
1194                         adapter->current_op);
1195                 return;
1196         }
1197
1198         memset(&rss_key, 0, sizeof(rss_key));
1199         memcpy(&rss_key, seed, sizeof(rss_key));
1200
1201         rss_lut = kzalloc(((I40E_VFQF_HLUT_MAX_INDEX + 1) * 4), GFP_KERNEL);
1202         if (!rss_lut)
1203                 return;
1204
1205         /* Populate the LUT with max no. PF queues in round robin fashion */
1206         for (i = 0; i <= (I40E_VFQF_HLUT_MAX_INDEX * 4); i++)
1207                 rss_lut[i] = i % adapter->num_active_queues;
1208
1209         ret = i40evf_aq_set_rss_key(hw, vsi->id, &rss_key);
1210         if (ret) {
1211                 dev_err(&adapter->pdev->dev,
1212                         "Cannot set RSS key, err %s aq_err %s\n",
1213                         i40evf_stat_str(hw, ret),
1214                         i40evf_aq_str(hw, hw->aq.asq_last_status));
1215                 return;
1216         }
1217
1218         ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, rss_lut,
1219                                     (I40E_VFQF_HLUT_MAX_INDEX + 1) * 4);
1220         if (ret)
1221                 dev_err(&adapter->pdev->dev,
1222                         "Cannot set RSS lut, err %s aq_err %s\n",
1223                         i40evf_stat_str(hw, ret),
1224                         i40evf_aq_str(hw, hw->aq.asq_last_status));
1225 }
1226
1227 /**
1228  * i40e_configure_rss_reg - Prepare for RSS if used
1229  * @adapter: board private structure
1230  * @seed: RSS hash seed
1231  **/
1232 static void i40evf_configure_rss_reg(struct i40evf_adapter *adapter,
1233                                      const u8 *seed)
1234 {
1235         struct i40e_hw *hw = &adapter->hw;
1236         u32 *seed_dw = (u32 *)seed;
1237         u32 cqueue = 0;
1238         u32 lut = 0;
1239         int i, j;
1240
1241         /* Fill out hash function seed */
1242         for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1243                 wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
1244
1245         /* Populate the LUT with max no. PF queues in round robin fashion */
1246         for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
1247                 lut = 0;
1248                 for (j = 0; j < 4; j++) {
1249                         if (cqueue == adapter->num_active_queues)
1250                                 cqueue = 0;
1251                         lut |= ((cqueue) << (8 * j));
1252                         cqueue++;
1253                 }
1254                 wr32(hw, I40E_VFQF_HLUT(i), lut);
1255         }
1256         i40e_flush(hw);
1257 }
1258
1259 /**
1260  * i40evf_configure_rss - Prepare for RSS
1261  * @adapter: board private structure
1262  **/
1263 static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1264 {
1265         struct i40e_hw *hw = &adapter->hw;
1266         u8 seed[I40EVF_HKEY_ARRAY_SIZE];
1267         u64 hena;
1268
1269         netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
1270
1271         /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1272         hena = I40E_DEFAULT_RSS_HENA;
1273         wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1274         wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1275
1276         if (RSS_AQ(adapter))
1277                 i40evf_configure_rss_aq(&adapter->vsi, seed);
1278         else
1279                 i40evf_configure_rss_reg(adapter, seed);
1280 }
1281
1282 /**
1283  * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1284  * @adapter: board private structure to initialize
1285  *
1286  * We allocate one q_vector per queue interrupt.  If allocation fails we
1287  * return -ENOMEM.
1288  **/
1289 static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1290 {
1291         int q_idx, num_q_vectors;
1292         struct i40e_q_vector *q_vector;
1293
1294         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1295
1296         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1297                 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
1298                 if (!q_vector)
1299                         goto err_out;
1300                 q_vector->adapter = adapter;
1301                 q_vector->vsi = &adapter->vsi;
1302                 q_vector->v_idx = q_idx;
1303                 netif_napi_add(adapter->netdev, &q_vector->napi,
1304                                i40evf_napi_poll, NAPI_POLL_WEIGHT);
1305                 adapter->q_vector[q_idx] = q_vector;
1306         }
1307
1308         return 0;
1309
1310 err_out:
1311         while (q_idx) {
1312                 q_idx--;
1313                 q_vector = adapter->q_vector[q_idx];
1314                 netif_napi_del(&q_vector->napi);
1315                 kfree(q_vector);
1316                 adapter->q_vector[q_idx] = NULL;
1317         }
1318         return -ENOMEM;
1319 }
1320
1321 /**
1322  * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1323  * @adapter: board private structure to initialize
1324  *
1325  * This function frees the memory allocated to the q_vectors.  In addition if
1326  * NAPI is enabled it will delete any references to the NAPI struct prior
1327  * to freeing the q_vector.
1328  **/
1329 static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1330 {
1331         int q_idx, num_q_vectors;
1332         int napi_vectors;
1333
1334         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1335         napi_vectors = adapter->num_active_queues;
1336
1337         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1338                 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1339
1340                 adapter->q_vector[q_idx] = NULL;
1341                 if (q_idx < napi_vectors)
1342                         netif_napi_del(&q_vector->napi);
1343                 kfree(q_vector);
1344         }
1345 }
1346
1347 /**
1348  * i40evf_reset_interrupt_capability - Reset MSIX setup
1349  * @adapter: board private structure
1350  *
1351  **/
1352 void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1353 {
1354         pci_disable_msix(adapter->pdev);
1355         kfree(adapter->msix_entries);
1356         adapter->msix_entries = NULL;
1357 }
1358
1359 /**
1360  * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1361  * @adapter: board private structure to initialize
1362  *
1363  **/
1364 int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1365 {
1366         int err;
1367
1368         err = i40evf_set_interrupt_capability(adapter);
1369         if (err) {
1370                 dev_err(&adapter->pdev->dev,
1371                         "Unable to setup interrupt capabilities\n");
1372                 goto err_set_interrupt;
1373         }
1374
1375         err = i40evf_alloc_q_vectors(adapter);
1376         if (err) {
1377                 dev_err(&adapter->pdev->dev,
1378                         "Unable to allocate memory for queue vectors\n");
1379                 goto err_alloc_q_vectors;
1380         }
1381
1382         err = i40evf_alloc_queues(adapter);
1383         if (err) {
1384                 dev_err(&adapter->pdev->dev,
1385                         "Unable to allocate memory for queues\n");
1386                 goto err_alloc_queues;
1387         }
1388
1389         dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1390                  (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1391                  adapter->num_active_queues);
1392
1393         return 0;
1394 err_alloc_queues:
1395         i40evf_free_q_vectors(adapter);
1396 err_alloc_q_vectors:
1397         i40evf_reset_interrupt_capability(adapter);
1398 err_set_interrupt:
1399         return err;
1400 }
1401
1402 /**
1403  * i40evf_watchdog_timer - Periodic call-back timer
1404  * @data: pointer to adapter disguised as unsigned long
1405  **/
1406 static void i40evf_watchdog_timer(unsigned long data)
1407 {
1408         struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1409
1410         schedule_work(&adapter->watchdog_task);
1411         /* timer will be rescheduled in watchdog task */
1412 }
1413
1414 /**
1415  * i40evf_watchdog_task - Periodic call-back task
1416  * @work: pointer to work_struct
1417  **/
1418 static void i40evf_watchdog_task(struct work_struct *work)
1419 {
1420         struct i40evf_adapter *adapter = container_of(work,
1421                                                       struct i40evf_adapter,
1422                                                       watchdog_task);
1423         struct i40e_hw *hw = &adapter->hw;
1424         uint32_t rstat_val;
1425
1426         if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1427                 goto restart_watchdog;
1428
1429         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1430                 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1431                             I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1432                 if ((rstat_val == I40E_VFR_VFACTIVE) ||
1433                     (rstat_val == I40E_VFR_COMPLETED)) {
1434                         /* A chance for redemption! */
1435                         dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1436                         adapter->state = __I40EVF_STARTUP;
1437                         adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1438                         schedule_delayed_work(&adapter->init_task, 10);
1439                         clear_bit(__I40EVF_IN_CRITICAL_TASK,
1440                                   &adapter->crit_section);
1441                         /* Don't reschedule the watchdog, since we've restarted
1442                          * the init task. When init_task contacts the PF and
1443                          * gets everything set up again, it'll restart the
1444                          * watchdog for us. Down, boy. Sit. Stay. Woof.
1445                          */
1446                         return;
1447                 }
1448                 adapter->aq_required = 0;
1449                 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1450                 goto watchdog_done;
1451         }
1452
1453         if ((adapter->state < __I40EVF_DOWN) ||
1454             (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1455                 goto watchdog_done;
1456
1457         /* check for reset */
1458         rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1459                     I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1460         if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) &&
1461             (rstat_val != I40E_VFR_VFACTIVE) &&
1462             (rstat_val != I40E_VFR_COMPLETED)) {
1463                 adapter->state = __I40EVF_RESETTING;
1464                 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1465                 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1466                 schedule_work(&adapter->reset_task);
1467                 adapter->aq_required = 0;
1468                 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1469                 goto watchdog_done;
1470         }
1471
1472         /* Process admin queue tasks. After init, everything gets done
1473          * here so we don't race on the admin queue.
1474          */
1475         if (adapter->current_op) {
1476                 if (!i40evf_asq_done(hw)) {
1477                         dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1478                         i40evf_send_api_ver(adapter);
1479                 }
1480                 goto watchdog_done;
1481         }
1482         if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1483                 i40evf_send_vf_config_msg(adapter);
1484                 goto watchdog_done;
1485         }
1486
1487         if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1488                 i40evf_disable_queues(adapter);
1489                 goto watchdog_done;
1490         }
1491
1492         if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1493                 i40evf_map_queues(adapter);
1494                 goto watchdog_done;
1495         }
1496
1497         if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1498                 i40evf_add_ether_addrs(adapter);
1499                 goto watchdog_done;
1500         }
1501
1502         if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1503                 i40evf_add_vlans(adapter);
1504                 goto watchdog_done;
1505         }
1506
1507         if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1508                 i40evf_del_ether_addrs(adapter);
1509                 goto watchdog_done;
1510         }
1511
1512         if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1513                 i40evf_del_vlans(adapter);
1514                 goto watchdog_done;
1515         }
1516
1517         if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1518                 i40evf_configure_queues(adapter);
1519                 goto watchdog_done;
1520         }
1521
1522         if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1523                 i40evf_enable_queues(adapter);
1524                 goto watchdog_done;
1525         }
1526
1527         if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1528                 /* This message goes straight to the firmware, not the
1529                  * PF, so we don't have to set current_op as we will
1530                  * not get a response through the ARQ.
1531                  */
1532                 i40evf_configure_rss(adapter);
1533                 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1534                 goto watchdog_done;
1535         }
1536
1537         if (adapter->state == __I40EVF_RUNNING)
1538                 i40evf_request_stats(adapter);
1539 watchdog_done:
1540         if (adapter->state == __I40EVF_RUNNING) {
1541                 i40evf_irq_enable_queues(adapter, ~0);
1542                 i40evf_fire_sw_int(adapter, 0xFF);
1543         } else {
1544                 i40evf_fire_sw_int(adapter, 0x1);
1545         }
1546
1547         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1548 restart_watchdog:
1549         if (adapter->state == __I40EVF_REMOVE)
1550                 return;
1551         if (adapter->aq_required)
1552                 mod_timer(&adapter->watchdog_timer,
1553                           jiffies + msecs_to_jiffies(20));
1554         else
1555                 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1556         schedule_work(&adapter->adminq_task);
1557 }
1558
1559 #define I40EVF_RESET_WAIT_MS 10
1560 #define I40EVF_RESET_WAIT_COUNT 500
1561 /**
1562  * i40evf_reset_task - Call-back task to handle hardware reset
1563  * @work: pointer to work_struct
1564  *
1565  * During reset we need to shut down and reinitialize the admin queue
1566  * before we can use it to communicate with the PF again. We also clear
1567  * and reinit the rings because that context is lost as well.
1568  **/
1569 static void i40evf_reset_task(struct work_struct *work)
1570 {
1571         struct i40evf_adapter *adapter = container_of(work,
1572                                                       struct i40evf_adapter,
1573                                                       reset_task);
1574         struct net_device *netdev = adapter->netdev;
1575         struct i40e_hw *hw = &adapter->hw;
1576         struct i40evf_mac_filter *f;
1577         uint32_t rstat_val;
1578         int i = 0, err;
1579
1580         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1581                                 &adapter->crit_section))
1582                 usleep_range(500, 1000);
1583
1584         i40evf_misc_irq_disable(adapter);
1585         if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1586                 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1587                 /* Restart the AQ here. If we have been reset but didn't
1588                  * detect it, or if the PF had to reinit, our AQ will be hosed.
1589                  */
1590                 i40evf_shutdown_adminq(hw);
1591                 i40evf_init_adminq(hw);
1592                 i40evf_request_reset(adapter);
1593         }
1594         adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1595
1596         /* poll until we see the reset actually happen */
1597         for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1598                 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1599                             I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1600                 if ((rstat_val != I40E_VFR_VFACTIVE) &&
1601                     (rstat_val != I40E_VFR_COMPLETED))
1602                         break;
1603                 usleep_range(500, 1000);
1604         }
1605         if (i == I40EVF_RESET_WAIT_COUNT) {
1606                 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1607                 goto continue_reset; /* act like the reset happened */
1608         }
1609
1610         /* wait until the reset is complete and the PF is responding to us */
1611         for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1612                 rstat_val = rd32(hw, I40E_VFGEN_RSTAT) &
1613                             I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1614                 if (rstat_val == I40E_VFR_VFACTIVE)
1615                         break;
1616                 msleep(I40EVF_RESET_WAIT_MS);
1617         }
1618         /* extra wait to make sure minimum wait is met */
1619         msleep(I40EVF_RESET_WAIT_MS);
1620         if (i == I40EVF_RESET_WAIT_COUNT) {
1621                 struct i40evf_mac_filter *f, *ftmp;
1622                 struct i40evf_vlan_filter *fv, *fvtmp;
1623
1624                 /* reset never finished */
1625                 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1626                         rstat_val);
1627                 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1628
1629                 if (netif_running(adapter->netdev)) {
1630                         set_bit(__I40E_DOWN, &adapter->vsi.state);
1631                         netif_carrier_off(netdev);
1632                         netif_tx_disable(netdev);
1633                         i40evf_napi_disable_all(adapter);
1634                         i40evf_irq_disable(adapter);
1635                         i40evf_free_traffic_irqs(adapter);
1636                         i40evf_free_all_tx_resources(adapter);
1637                         i40evf_free_all_rx_resources(adapter);
1638                 }
1639
1640                 /* Delete all of the filters, both MAC and VLAN. */
1641                 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1642                                          list) {
1643                         list_del(&f->list);
1644                         kfree(f);
1645                 }
1646
1647                 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1648                                          list) {
1649                         list_del(&fv->list);
1650                         kfree(fv);
1651                 }
1652
1653                 i40evf_free_misc_irq(adapter);
1654                 i40evf_reset_interrupt_capability(adapter);
1655                 i40evf_free_queues(adapter);
1656                 i40evf_free_q_vectors(adapter);
1657                 kfree(adapter->vf_res);
1658                 i40evf_shutdown_adminq(hw);
1659                 adapter->netdev->flags &= ~IFF_UP;
1660                 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1661                 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1662                 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1663                 return; /* Do not attempt to reinit. It's dead, Jim. */
1664         }
1665
1666 continue_reset:
1667         if (netif_running(adapter->netdev)) {
1668                 netif_carrier_off(netdev);
1669                 netif_tx_stop_all_queues(netdev);
1670                 i40evf_napi_disable_all(adapter);
1671         }
1672         i40evf_irq_disable(adapter);
1673
1674         adapter->state = __I40EVF_RESETTING;
1675         adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1676
1677         /* free the Tx/Rx rings and descriptors, might be better to just
1678          * re-use them sometime in the future
1679          */
1680         i40evf_free_all_rx_resources(adapter);
1681         i40evf_free_all_tx_resources(adapter);
1682
1683         /* kill and reinit the admin queue */
1684         if (i40evf_shutdown_adminq(hw))
1685                 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1686         adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1687         err = i40evf_init_adminq(hw);
1688         if (err)
1689                 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1690                          err);
1691
1692         adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1693         adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
1694
1695         /* re-add all MAC filters */
1696         list_for_each_entry(f, &adapter->mac_filter_list, list) {
1697                 f->add = true;
1698         }
1699         /* re-add all VLAN filters */
1700         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1701                 f->add = true;
1702         }
1703         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1704         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
1705         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1706         i40evf_misc_irq_enable(adapter);
1707
1708         mod_timer(&adapter->watchdog_timer, jiffies + 2);
1709
1710         if (netif_running(adapter->netdev)) {
1711                 /* allocate transmit descriptors */
1712                 err = i40evf_setup_all_tx_resources(adapter);
1713                 if (err)
1714                         goto reset_err;
1715
1716                 /* allocate receive descriptors */
1717                 err = i40evf_setup_all_rx_resources(adapter);
1718                 if (err)
1719                         goto reset_err;
1720
1721                 i40evf_configure(adapter);
1722
1723                 err = i40evf_up_complete(adapter);
1724                 if (err)
1725                         goto reset_err;
1726
1727                 i40evf_irq_enable(adapter, true);
1728         } else {
1729                 adapter->state = __I40EVF_DOWN;
1730         }
1731
1732         return;
1733 reset_err:
1734         dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
1735         i40evf_close(adapter->netdev);
1736 }
1737
1738 /**
1739  * i40evf_adminq_task - worker thread to clean the admin queue
1740  * @work: pointer to work_struct containing our data
1741  **/
1742 static void i40evf_adminq_task(struct work_struct *work)
1743 {
1744         struct i40evf_adapter *adapter =
1745                 container_of(work, struct i40evf_adapter, adminq_task);
1746         struct i40e_hw *hw = &adapter->hw;
1747         struct i40e_arq_event_info event;
1748         struct i40e_virtchnl_msg *v_msg;
1749         i40e_status ret;
1750         u32 val, oldval;
1751         u16 pending;
1752
1753         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1754                 goto out;
1755
1756         event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1757         event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1758         if (!event.msg_buf)
1759                 goto out;
1760
1761         v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1762         do {
1763                 ret = i40evf_clean_arq_element(hw, &event, &pending);
1764                 if (ret || !v_msg->v_opcode)
1765                         break; /* No event to process or error cleaning ARQ */
1766
1767                 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1768                                            v_msg->v_retval, event.msg_buf,
1769                                            event.msg_len);
1770                 if (pending != 0)
1771                         memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1772         } while (pending);
1773
1774         if ((adapter->flags &
1775              (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1776             adapter->state == __I40EVF_RESETTING)
1777                 goto freedom;
1778
1779         /* check for error indications */
1780         val = rd32(hw, hw->aq.arq.len);
1781         oldval = val;
1782         if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
1783                 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1784                 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
1785         }
1786         if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
1787                 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1788                 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
1789         }
1790         if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
1791                 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1792                 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
1793         }
1794         if (oldval != val)
1795                 wr32(hw, hw->aq.arq.len, val);
1796
1797         val = rd32(hw, hw->aq.asq.len);
1798         oldval = val;
1799         if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
1800                 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1801                 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
1802         }
1803         if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
1804                 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1805                 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
1806         }
1807         if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
1808                 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1809                 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
1810         }
1811         if (oldval != val)
1812                 wr32(hw, hw->aq.asq.len, val);
1813
1814 freedom:
1815         kfree(event.msg_buf);
1816 out:
1817         /* re-enable Admin queue interrupt cause */
1818         i40evf_misc_irq_enable(adapter);
1819 }
1820
1821 /**
1822  * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1823  * @adapter: board private structure
1824  *
1825  * Free all transmit software resources
1826  **/
1827 void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1828 {
1829         int i;
1830
1831         for (i = 0; i < adapter->num_active_queues; i++)
1832                 if (adapter->tx_rings[i]->desc)
1833                         i40evf_free_tx_resources(adapter->tx_rings[i]);
1834 }
1835
1836 /**
1837  * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1838  * @adapter: board private structure
1839  *
1840  * If this function returns with an error, then it's possible one or
1841  * more of the rings is populated (while the rest are not).  It is the
1842  * callers duty to clean those orphaned rings.
1843  *
1844  * Return 0 on success, negative on failure
1845  **/
1846 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1847 {
1848         int i, err = 0;
1849
1850         for (i = 0; i < adapter->num_active_queues; i++) {
1851                 adapter->tx_rings[i]->count = adapter->tx_desc_count;
1852                 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1853                 if (!err)
1854                         continue;
1855                 dev_err(&adapter->pdev->dev,
1856                         "%s: Allocation for Tx Queue %u failed\n",
1857                         __func__, i);
1858                 break;
1859         }
1860
1861         return err;
1862 }
1863
1864 /**
1865  * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1866  * @adapter: board private structure
1867  *
1868  * If this function returns with an error, then it's possible one or
1869  * more of the rings is populated (while the rest are not).  It is the
1870  * callers duty to clean those orphaned rings.
1871  *
1872  * Return 0 on success, negative on failure
1873  **/
1874 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1875 {
1876         int i, err = 0;
1877
1878         for (i = 0; i < adapter->num_active_queues; i++) {
1879                 adapter->rx_rings[i]->count = adapter->rx_desc_count;
1880                 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1881                 if (!err)
1882                         continue;
1883                 dev_err(&adapter->pdev->dev,
1884                         "%s: Allocation for Rx Queue %u failed\n",
1885                         __func__, i);
1886                 break;
1887         }
1888         return err;
1889 }
1890
1891 /**
1892  * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1893  * @adapter: board private structure
1894  *
1895  * Free all receive software resources
1896  **/
1897 void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1898 {
1899         int i;
1900
1901         for (i = 0; i < adapter->num_active_queues; i++)
1902                 if (adapter->rx_rings[i]->desc)
1903                         i40evf_free_rx_resources(adapter->rx_rings[i]);
1904 }
1905
1906 /**
1907  * i40evf_open - Called when a network interface is made active
1908  * @netdev: network interface device structure
1909  *
1910  * Returns 0 on success, negative value on failure
1911  *
1912  * The open entry point is called when a network interface is made
1913  * active by the system (IFF_UP).  At this point all resources needed
1914  * for transmit and receive operations are allocated, the interrupt
1915  * handler is registered with the OS, the watchdog timer is started,
1916  * and the stack is notified that the interface is ready.
1917  **/
1918 static int i40evf_open(struct net_device *netdev)
1919 {
1920         struct i40evf_adapter *adapter = netdev_priv(netdev);
1921         int err;
1922
1923         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1924                 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1925                 return -EIO;
1926         }
1927         if (adapter->state != __I40EVF_DOWN || adapter->aq_required)
1928                 return -EBUSY;
1929
1930         /* allocate transmit descriptors */
1931         err = i40evf_setup_all_tx_resources(adapter);
1932         if (err)
1933                 goto err_setup_tx;
1934
1935         /* allocate receive descriptors */
1936         err = i40evf_setup_all_rx_resources(adapter);
1937         if (err)
1938                 goto err_setup_rx;
1939
1940         /* clear any pending interrupts, may auto mask */
1941         err = i40evf_request_traffic_irqs(adapter, netdev->name);
1942         if (err)
1943                 goto err_req_irq;
1944
1945         i40evf_add_filter(adapter, adapter->hw.mac.addr);
1946         i40evf_configure(adapter);
1947
1948         err = i40evf_up_complete(adapter);
1949         if (err)
1950                 goto err_req_irq;
1951
1952         i40evf_irq_enable(adapter, true);
1953
1954         return 0;
1955
1956 err_req_irq:
1957         i40evf_down(adapter);
1958         i40evf_free_traffic_irqs(adapter);
1959 err_setup_rx:
1960         i40evf_free_all_rx_resources(adapter);
1961 err_setup_tx:
1962         i40evf_free_all_tx_resources(adapter);
1963
1964         return err;
1965 }
1966
1967 /**
1968  * i40evf_close - Disables a network interface
1969  * @netdev: network interface device structure
1970  *
1971  * Returns 0, this is not allowed to fail
1972  *
1973  * The close entry point is called when an interface is de-activated
1974  * by the OS.  The hardware is still under the drivers control, but
1975  * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1976  * are freed, along with all transmit and receive resources.
1977  **/
1978 static int i40evf_close(struct net_device *netdev)
1979 {
1980         struct i40evf_adapter *adapter = netdev_priv(netdev);
1981
1982         if (adapter->state <= __I40EVF_DOWN)
1983                 return 0;
1984
1985
1986         set_bit(__I40E_DOWN, &adapter->vsi.state);
1987
1988         i40evf_down(adapter);
1989         adapter->state = __I40EVF_DOWN;
1990         i40evf_free_traffic_irqs(adapter);
1991
1992         return 0;
1993 }
1994
1995 /**
1996  * i40evf_get_stats - Get System Network Statistics
1997  * @netdev: network interface device structure
1998  *
1999  * Returns the address of the device statistics structure.
2000  * The statistics are actually updated from the timer callback.
2001  **/
2002 static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2003 {
2004         struct i40evf_adapter *adapter = netdev_priv(netdev);
2005
2006         /* only return the current stats */
2007         return &adapter->net_stats;
2008 }
2009
2010 /**
2011  * i40evf_change_mtu - Change the Maximum Transfer Unit
2012  * @netdev: network interface device structure
2013  * @new_mtu: new value for maximum frame size
2014  *
2015  * Returns 0 on success, negative on failure
2016  **/
2017 static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2018 {
2019         struct i40evf_adapter *adapter = netdev_priv(netdev);
2020         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2021
2022         if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2023                 return -EINVAL;
2024
2025         netdev->mtu = new_mtu;
2026         adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2027         schedule_work(&adapter->reset_task);
2028
2029         return 0;
2030 }
2031
2032 static const struct net_device_ops i40evf_netdev_ops = {
2033         .ndo_open               = i40evf_open,
2034         .ndo_stop               = i40evf_close,
2035         .ndo_start_xmit         = i40evf_xmit_frame,
2036         .ndo_get_stats          = i40evf_get_stats,
2037         .ndo_set_rx_mode        = i40evf_set_rx_mode,
2038         .ndo_validate_addr      = eth_validate_addr,
2039         .ndo_set_mac_address    = i40evf_set_mac,
2040         .ndo_change_mtu         = i40evf_change_mtu,
2041         .ndo_tx_timeout         = i40evf_tx_timeout,
2042         .ndo_vlan_rx_add_vid    = i40evf_vlan_rx_add_vid,
2043         .ndo_vlan_rx_kill_vid   = i40evf_vlan_rx_kill_vid,
2044 };
2045
2046 /**
2047  * i40evf_check_reset_complete - check that VF reset is complete
2048  * @hw: pointer to hw struct
2049  *
2050  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2051  **/
2052 static int i40evf_check_reset_complete(struct i40e_hw *hw)
2053 {
2054         u32 rstat;
2055         int i;
2056
2057         for (i = 0; i < 100; i++) {
2058                 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2059                             I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2060                 if ((rstat == I40E_VFR_VFACTIVE) ||
2061                     (rstat == I40E_VFR_COMPLETED))
2062                         return 0;
2063                 usleep_range(10, 20);
2064         }
2065         return -EBUSY;
2066 }
2067
2068 /**
2069  * i40evf_process_config - Process the config information we got from the PF
2070  * @adapter: board private structure
2071  *
2072  * Verify that we have a valid config struct, and set up our netdev features
2073  * and our VSI struct.
2074  **/
2075 int i40evf_process_config(struct i40evf_adapter *adapter)
2076 {
2077         struct net_device *netdev = adapter->netdev;
2078         int i;
2079
2080         /* got VF config message back from PF, now we can parse it */
2081         for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2082                 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2083                         adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2084         }
2085         if (!adapter->vsi_res) {
2086                 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2087                 return -ENODEV;
2088         }
2089
2090         if (adapter->vf_res->vf_offload_flags
2091             & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2092                 netdev->vlan_features = netdev->features;
2093                 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2094                                     NETIF_F_HW_VLAN_CTAG_RX |
2095                                     NETIF_F_HW_VLAN_CTAG_FILTER;
2096         }
2097         netdev->features |= NETIF_F_HIGHDMA |
2098                             NETIF_F_SG |
2099                             NETIF_F_IP_CSUM |
2100                             NETIF_F_SCTP_CSUM |
2101                             NETIF_F_IPV6_CSUM |
2102                             NETIF_F_TSO |
2103                             NETIF_F_TSO6 |
2104                             NETIF_F_RXCSUM |
2105                             NETIF_F_GRO;
2106
2107         /* copy netdev features into list of user selectable features */
2108         netdev->hw_features |= netdev->features;
2109         netdev->hw_features &= ~NETIF_F_RXCSUM;
2110
2111         adapter->vsi.id = adapter->vsi_res->vsi_id;
2112
2113         adapter->vsi.back = adapter;
2114         adapter->vsi.base_vector = 1;
2115         adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2116         adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2117                                        ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2118         adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2119                                        ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2120         adapter->vsi.netdev = adapter->netdev;
2121         return 0;
2122 }
2123
2124 /**
2125  * i40evf_init_task - worker thread to perform delayed initialization
2126  * @work: pointer to work_struct containing our data
2127  *
2128  * This task completes the work that was begun in probe. Due to the nature
2129  * of VF-PF communications, we may need to wait tens of milliseconds to get
2130  * responses back from the PF. Rather than busy-wait in probe and bog down the
2131  * whole system, we'll do it in a task so we can sleep.
2132  * This task only runs during driver init. Once we've established
2133  * communications with the PF driver and set up our netdev, the watchdog
2134  * takes over.
2135  **/
2136 static void i40evf_init_task(struct work_struct *work)
2137 {
2138         struct i40evf_adapter *adapter = container_of(work,
2139                                                       struct i40evf_adapter,
2140                                                       init_task.work);
2141         struct net_device *netdev = adapter->netdev;
2142         struct i40e_hw *hw = &adapter->hw;
2143         struct pci_dev *pdev = adapter->pdev;
2144         int err, bufsz;
2145
2146         switch (adapter->state) {
2147         case __I40EVF_STARTUP:
2148                 /* driver loaded, probe complete */
2149                 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2150                 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
2151                 err = i40e_set_mac_type(hw);
2152                 if (err) {
2153                         dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2154                                 err);
2155                         goto err;
2156                 }
2157                 err = i40evf_check_reset_complete(hw);
2158                 if (err) {
2159                         dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2160                                  err);
2161                         goto err;
2162                 }
2163                 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2164                 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2165                 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2166                 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2167
2168                 err = i40evf_init_adminq(hw);
2169                 if (err) {
2170                         dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2171                                 err);
2172                         goto err;
2173                 }
2174                 err = i40evf_send_api_ver(adapter);
2175                 if (err) {
2176                         dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
2177                         i40evf_shutdown_adminq(hw);
2178                         goto err;
2179                 }
2180                 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2181                 goto restart;
2182         case __I40EVF_INIT_VERSION_CHECK:
2183                 if (!i40evf_asq_done(hw)) {
2184                         dev_err(&pdev->dev, "Admin queue command never completed\n");
2185                         i40evf_shutdown_adminq(hw);
2186                         adapter->state = __I40EVF_STARTUP;
2187                         goto err;
2188                 }
2189
2190                 /* aq msg sent, awaiting reply */
2191                 err = i40evf_verify_api_ver(adapter);
2192                 if (err) {
2193                         if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2194                                 err = i40evf_send_api_ver(adapter);
2195                         else
2196                                 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2197                                         adapter->pf_version.major,
2198                                         adapter->pf_version.minor,
2199                                         I40E_VIRTCHNL_VERSION_MAJOR,
2200                                         I40E_VIRTCHNL_VERSION_MINOR);
2201                         goto err;
2202                 }
2203                 err = i40evf_send_vf_config_msg(adapter);
2204                 if (err) {
2205                         dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2206                                 err);
2207                         goto err;
2208                 }
2209                 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2210                 goto restart;
2211         case __I40EVF_INIT_GET_RESOURCES:
2212                 /* aq msg sent, awaiting reply */
2213                 if (!adapter->vf_res) {
2214                         bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2215                                 (I40E_MAX_VF_VSI *
2216                                  sizeof(struct i40e_virtchnl_vsi_resource));
2217                         adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
2218                         if (!adapter->vf_res)
2219                                 goto err;
2220                 }
2221                 err = i40evf_get_vf_config(adapter);
2222                 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2223                         err = i40evf_send_vf_config_msg(adapter);
2224                         goto err;
2225                 }
2226                 if (err) {
2227                         dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2228                                 err);
2229                         goto err_alloc;
2230                 }
2231                 adapter->state = __I40EVF_INIT_SW;
2232                 break;
2233         default:
2234                 goto err_alloc;
2235         }
2236         if (i40evf_process_config(adapter))
2237                 goto err_alloc;
2238         adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
2239
2240         adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2241
2242         netdev->netdev_ops = &i40evf_netdev_ops;
2243         i40evf_set_ethtool_ops(netdev);
2244         netdev->watchdog_timeo = 5 * HZ;
2245
2246         if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2247                 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2248                          adapter->hw.mac.addr);
2249                 random_ether_addr(adapter->hw.mac.addr);
2250         }
2251         ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2252         ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2253
2254         init_timer(&adapter->watchdog_timer);
2255         adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2256         adapter->watchdog_timer.data = (unsigned long)adapter;
2257         mod_timer(&adapter->watchdog_timer, jiffies + 1);
2258
2259         adapter->num_active_queues = min_t(int,
2260                                            adapter->vsi_res->num_queue_pairs,
2261                                            (int)(num_online_cpus()));
2262         adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2263         adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
2264         err = i40evf_init_interrupt_scheme(adapter);
2265         if (err)
2266                 goto err_sw_init;
2267         i40evf_map_rings_to_vectors(adapter);
2268         if (!RSS_AQ(adapter))
2269                 i40evf_configure_rss(adapter);
2270         err = i40evf_request_misc_irq(adapter);
2271         if (err)
2272                 goto err_sw_init;
2273
2274         netif_carrier_off(netdev);
2275
2276         if (!adapter->netdev_registered) {
2277                 err = register_netdev(netdev);
2278                 if (err)
2279                         goto err_register;
2280         }
2281
2282         adapter->netdev_registered = true;
2283
2284         netif_tx_stop_all_queues(netdev);
2285
2286         dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2287         if (netdev->features & NETIF_F_GRO)
2288                 dev_info(&pdev->dev, "GRO is enabled\n");
2289
2290         dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2291         adapter->state = __I40EVF_DOWN;
2292         set_bit(__I40E_DOWN, &adapter->vsi.state);
2293         i40evf_misc_irq_enable(adapter);
2294
2295         if (RSS_AQ(adapter)) {
2296                 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2297                 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2298         } else {
2299                 i40evf_configure_rss(adapter);
2300         }
2301         return;
2302 restart:
2303         schedule_delayed_work(&adapter->init_task,
2304                               msecs_to_jiffies(50));
2305         return;
2306
2307 err_register:
2308         i40evf_free_misc_irq(adapter);
2309 err_sw_init:
2310         i40evf_reset_interrupt_capability(adapter);
2311 err_alloc:
2312         kfree(adapter->vf_res);
2313         adapter->vf_res = NULL;
2314 err:
2315         /* Things went into the weeds, so try again later */
2316         if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2317                 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
2318                 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
2319                 return; /* do not reschedule */
2320         }
2321         schedule_delayed_work(&adapter->init_task, HZ * 3);
2322 }
2323
2324 /**
2325  * i40evf_shutdown - Shutdown the device in preparation for a reboot
2326  * @pdev: pci device structure
2327  **/
2328 static void i40evf_shutdown(struct pci_dev *pdev)
2329 {
2330         struct net_device *netdev = pci_get_drvdata(pdev);
2331         struct i40evf_adapter *adapter = netdev_priv(netdev);
2332
2333         netif_device_detach(netdev);
2334
2335         if (netif_running(netdev))
2336                 i40evf_close(netdev);
2337
2338         /* Prevent the watchdog from running. */
2339         adapter->state = __I40EVF_REMOVE;
2340         adapter->aq_required = 0;
2341
2342 #ifdef CONFIG_PM
2343         pci_save_state(pdev);
2344
2345 #endif
2346         pci_disable_device(pdev);
2347 }
2348
2349 /**
2350  * i40evf_probe - Device Initialization Routine
2351  * @pdev: PCI device information struct
2352  * @ent: entry in i40evf_pci_tbl
2353  *
2354  * Returns 0 on success, negative on failure
2355  *
2356  * i40evf_probe initializes an adapter identified by a pci_dev structure.
2357  * The OS initialization, configuring of the adapter private structure,
2358  * and a hardware reset occur.
2359  **/
2360 static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2361 {
2362         struct net_device *netdev;
2363         struct i40evf_adapter *adapter = NULL;
2364         struct i40e_hw *hw = NULL;
2365         int err;
2366
2367         err = pci_enable_device(pdev);
2368         if (err)
2369                 return err;
2370
2371         err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2372         if (err) {
2373                 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2374                 if (err) {
2375                         dev_err(&pdev->dev,
2376                                 "DMA configuration failed: 0x%x\n", err);
2377                         goto err_dma;
2378                 }
2379         }
2380
2381         err = pci_request_regions(pdev, i40evf_driver_name);
2382         if (err) {
2383                 dev_err(&pdev->dev,
2384                         "pci_request_regions failed 0x%x\n", err);
2385                 goto err_pci_reg;
2386         }
2387
2388         pci_enable_pcie_error_reporting(pdev);
2389
2390         pci_set_master(pdev);
2391
2392         netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2393                                    MAX_TX_QUEUES);
2394         if (!netdev) {
2395                 err = -ENOMEM;
2396                 goto err_alloc_etherdev;
2397         }
2398
2399         SET_NETDEV_DEV(netdev, &pdev->dev);
2400
2401         pci_set_drvdata(pdev, netdev);
2402         adapter = netdev_priv(netdev);
2403
2404         adapter->netdev = netdev;
2405         adapter->pdev = pdev;
2406
2407         hw = &adapter->hw;
2408         hw->back = adapter;
2409
2410         adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2411         adapter->state = __I40EVF_STARTUP;
2412
2413         /* Call save state here because it relies on the adapter struct. */
2414         pci_save_state(pdev);
2415
2416         hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2417                               pci_resource_len(pdev, 0));
2418         if (!hw->hw_addr) {
2419                 err = -EIO;
2420                 goto err_ioremap;
2421         }
2422         hw->vendor_id = pdev->vendor;
2423         hw->device_id = pdev->device;
2424         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2425         hw->subsystem_vendor_id = pdev->subsystem_vendor;
2426         hw->subsystem_device_id = pdev->subsystem_device;
2427         hw->bus.device = PCI_SLOT(pdev->devfn);
2428         hw->bus.func = PCI_FUNC(pdev->devfn);
2429
2430         INIT_LIST_HEAD(&adapter->mac_filter_list);
2431         INIT_LIST_HEAD(&adapter->vlan_filter_list);
2432
2433         INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2434         INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2435         INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2436         INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2437         schedule_delayed_work(&adapter->init_task, 10);
2438
2439         return 0;
2440
2441 err_ioremap:
2442         free_netdev(netdev);
2443 err_alloc_etherdev:
2444         pci_release_regions(pdev);
2445 err_pci_reg:
2446 err_dma:
2447         pci_disable_device(pdev);
2448         return err;
2449 }
2450
2451 #ifdef CONFIG_PM
2452 /**
2453  * i40evf_suspend - Power management suspend routine
2454  * @pdev: PCI device information struct
2455  * @state: unused
2456  *
2457  * Called when the system (VM) is entering sleep/suspend.
2458  **/
2459 static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2460 {
2461         struct net_device *netdev = pci_get_drvdata(pdev);
2462         struct i40evf_adapter *adapter = netdev_priv(netdev);
2463         int retval = 0;
2464
2465         netif_device_detach(netdev);
2466
2467         if (netif_running(netdev)) {
2468                 rtnl_lock();
2469                 i40evf_down(adapter);
2470                 rtnl_unlock();
2471         }
2472         i40evf_free_misc_irq(adapter);
2473         i40evf_reset_interrupt_capability(adapter);
2474
2475         retval = pci_save_state(pdev);
2476         if (retval)
2477                 return retval;
2478
2479         pci_disable_device(pdev);
2480
2481         return 0;
2482 }
2483
2484 /**
2485  * i40evf_resume - Power management resume routine
2486  * @pdev: PCI device information struct
2487  *
2488  * Called when the system (VM) is resumed from sleep/suspend.
2489  **/
2490 static int i40evf_resume(struct pci_dev *pdev)
2491 {
2492         struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2493         struct net_device *netdev = adapter->netdev;
2494         u32 err;
2495
2496         pci_set_power_state(pdev, PCI_D0);
2497         pci_restore_state(pdev);
2498         /* pci_restore_state clears dev->state_saved so call
2499          * pci_save_state to restore it.
2500          */
2501         pci_save_state(pdev);
2502
2503         err = pci_enable_device_mem(pdev);
2504         if (err) {
2505                 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2506                 return err;
2507         }
2508         pci_set_master(pdev);
2509
2510         rtnl_lock();
2511         err = i40evf_set_interrupt_capability(adapter);
2512         if (err) {
2513                 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2514                 return err;
2515         }
2516         err = i40evf_request_misc_irq(adapter);
2517         rtnl_unlock();
2518         if (err) {
2519                 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2520                 return err;
2521         }
2522
2523         schedule_work(&adapter->reset_task);
2524
2525         netif_device_attach(netdev);
2526
2527         return err;
2528 }
2529
2530 #endif /* CONFIG_PM */
2531 /**
2532  * i40evf_remove - Device Removal Routine
2533  * @pdev: PCI device information struct
2534  *
2535  * i40evf_remove is called by the PCI subsystem to alert the driver
2536  * that it should release a PCI device.  The could be caused by a
2537  * Hot-Plug event, or because the driver is going to be removed from
2538  * memory.
2539  **/
2540 static void i40evf_remove(struct pci_dev *pdev)
2541 {
2542         struct net_device *netdev = pci_get_drvdata(pdev);
2543         struct i40evf_adapter *adapter = netdev_priv(netdev);
2544         struct i40evf_mac_filter *f, *ftmp;
2545         struct i40e_hw *hw = &adapter->hw;
2546
2547         cancel_delayed_work_sync(&adapter->init_task);
2548         cancel_work_sync(&adapter->reset_task);
2549
2550         if (adapter->netdev_registered) {
2551                 unregister_netdev(netdev);
2552                 adapter->netdev_registered = false;
2553         }
2554
2555         /* Shut down all the garbage mashers on the detention level */
2556         adapter->state = __I40EVF_REMOVE;
2557         adapter->aq_required = 0;
2558         i40evf_request_reset(adapter);
2559         msleep(20);
2560         /* If the FW isn't responding, kick it once, but only once. */
2561         if (!i40evf_asq_done(hw)) {
2562                 i40evf_request_reset(adapter);
2563                 msleep(20);
2564         }
2565
2566         if (adapter->msix_entries) {
2567                 i40evf_misc_irq_disable(adapter);
2568                 i40evf_free_misc_irq(adapter);
2569                 i40evf_reset_interrupt_capability(adapter);
2570                 i40evf_free_q_vectors(adapter);
2571         }
2572
2573         if (adapter->watchdog_timer.function)
2574                 del_timer_sync(&adapter->watchdog_timer);
2575
2576         flush_scheduled_work();
2577
2578         if (hw->aq.asq.count)
2579                 i40evf_shutdown_adminq(hw);
2580
2581         iounmap(hw->hw_addr);
2582         pci_release_regions(pdev);
2583
2584         i40evf_free_all_tx_resources(adapter);
2585         i40evf_free_all_rx_resources(adapter);
2586         i40evf_free_queues(adapter);
2587         kfree(adapter->vf_res);
2588         /* If we got removed before an up/down sequence, we've got a filter
2589          * hanging out there that we need to get rid of.
2590          */
2591         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2592                 list_del(&f->list);
2593                 kfree(f);
2594         }
2595         list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2596                 list_del(&f->list);
2597                 kfree(f);
2598         }
2599
2600         free_netdev(netdev);
2601
2602         pci_disable_pcie_error_reporting(pdev);
2603
2604         pci_disable_device(pdev);
2605 }
2606
2607 static struct pci_driver i40evf_driver = {
2608         .name     = i40evf_driver_name,
2609         .id_table = i40evf_pci_tbl,
2610         .probe    = i40evf_probe,
2611         .remove   = i40evf_remove,
2612 #ifdef CONFIG_PM
2613         .suspend  = i40evf_suspend,
2614         .resume   = i40evf_resume,
2615 #endif
2616         .shutdown = i40evf_shutdown,
2617 };
2618
2619 /**
2620  * i40e_init_module - Driver Registration Routine
2621  *
2622  * i40e_init_module is the first routine called when the driver is
2623  * loaded. All it does is register with the PCI subsystem.
2624  **/
2625 static int __init i40evf_init_module(void)
2626 {
2627         int ret;
2628
2629         pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2630                 i40evf_driver_version);
2631
2632         pr_info("%s\n", i40evf_copyright);
2633
2634         ret = pci_register_driver(&i40evf_driver);
2635         return ret;
2636 }
2637
2638 module_init(i40evf_init_module);
2639
2640 /**
2641  * i40e_exit_module - Driver Exit Cleanup Routine
2642  *
2643  * i40e_exit_module is called just before the driver is removed
2644  * from memory.
2645  **/
2646 static void __exit i40evf_exit_module(void)
2647 {
2648         pci_unregister_driver(&i40evf_driver);
2649 }
2650
2651 module_exit(i40evf_exit_module);
2652
2653 /* i40evf_main.c */