]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/intel/i40evf/i40evf_main.c
Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetoot...
[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.13"
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                                  "Request_irq failed, error: %d\n", err);
493                         goto free_queue_irqs;
494                 }
495                 /* assign the mask for this irq */
496                 irq_set_affinity_hint(
497                         adapter->msix_entries[vector + NONQ_VECS].vector,
498                         q_vector->affinity_mask);
499         }
500
501         return 0;
502
503 free_queue_irqs:
504         while (vector) {
505                 vector--;
506                 irq_set_affinity_hint(
507                         adapter->msix_entries[vector + NONQ_VECS].vector,
508                         NULL);
509                 free_irq(adapter->msix_entries[vector + NONQ_VECS].vector,
510                          adapter->q_vector[vector]);
511         }
512         return err;
513 }
514
515 /**
516  * i40evf_request_misc_irq - Initialize MSI-X interrupts
517  * @adapter: board private structure
518  *
519  * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
520  * vector is only for the admin queue, and stays active even when the netdev
521  * is closed.
522  **/
523 static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
524 {
525         struct net_device *netdev = adapter->netdev;
526         int err;
527
528         snprintf(adapter->misc_vector_name,
529                  sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
530                  dev_name(&adapter->pdev->dev));
531         err = request_irq(adapter->msix_entries[0].vector,
532                           &i40evf_msix_aq, 0,
533                           adapter->misc_vector_name, netdev);
534         if (err) {
535                 dev_err(&adapter->pdev->dev,
536                         "request_irq for %s failed: %d\n",
537                         adapter->misc_vector_name, err);
538                 free_irq(adapter->msix_entries[0].vector, netdev);
539         }
540         return err;
541 }
542
543 /**
544  * i40evf_free_traffic_irqs - Free MSI-X interrupts
545  * @adapter: board private structure
546  *
547  * Frees all MSI-X vectors other than 0.
548  **/
549 static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
550 {
551         int i;
552         int q_vectors;
553
554         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
555
556         for (i = 0; i < q_vectors; i++) {
557                 irq_set_affinity_hint(adapter->msix_entries[i+1].vector,
558                                       NULL);
559                 free_irq(adapter->msix_entries[i+1].vector,
560                          adapter->q_vector[i]);
561         }
562 }
563
564 /**
565  * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
566  * @adapter: board private structure
567  *
568  * Frees MSI-X vector 0.
569  **/
570 static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
571 {
572         struct net_device *netdev = adapter->netdev;
573
574         free_irq(adapter->msix_entries[0].vector, netdev);
575 }
576
577 /**
578  * i40evf_configure_tx - Configure Transmit Unit after Reset
579  * @adapter: board private structure
580  *
581  * Configure the Tx unit of the MAC after a reset.
582  **/
583 static void i40evf_configure_tx(struct i40evf_adapter *adapter)
584 {
585         struct i40e_hw *hw = &adapter->hw;
586         int i;
587
588         for (i = 0; i < adapter->num_active_queues; i++)
589                 adapter->tx_rings[i]->tail = hw->hw_addr + I40E_QTX_TAIL1(i);
590 }
591
592 /**
593  * i40evf_configure_rx - Configure Receive Unit after Reset
594  * @adapter: board private structure
595  *
596  * Configure the Rx unit of the MAC after a reset.
597  **/
598 static void i40evf_configure_rx(struct i40evf_adapter *adapter)
599 {
600         struct i40e_hw *hw = &adapter->hw;
601         struct net_device *netdev = adapter->netdev;
602         int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
603         int i;
604         int rx_buf_len;
605
606
607         adapter->flags &= ~I40EVF_FLAG_RX_PS_CAPABLE;
608         adapter->flags |= I40EVF_FLAG_RX_1BUF_CAPABLE;
609
610         /* Decide whether to use packet split mode or not */
611         if (netdev->mtu > ETH_DATA_LEN) {
612                 if (adapter->flags & I40EVF_FLAG_RX_PS_CAPABLE)
613                         adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
614                 else
615                         adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
616         } else {
617                 if (adapter->flags & I40EVF_FLAG_RX_1BUF_CAPABLE)
618                         adapter->flags &= ~I40EVF_FLAG_RX_PS_ENABLED;
619                 else
620                         adapter->flags |= I40EVF_FLAG_RX_PS_ENABLED;
621         }
622
623         /* Set the RX buffer length according to the mode */
624         if (adapter->flags & I40EVF_FLAG_RX_PS_ENABLED) {
625                 rx_buf_len = I40E_RX_HDR_SIZE;
626         } else {
627                 if (netdev->mtu <= ETH_DATA_LEN)
628                         rx_buf_len = I40EVF_RXBUFFER_2048;
629                 else
630                         rx_buf_len = ALIGN(max_frame, 1024);
631         }
632
633         for (i = 0; i < adapter->num_active_queues; i++) {
634                 adapter->rx_rings[i]->tail = hw->hw_addr + I40E_QRX_TAIL1(i);
635                 adapter->rx_rings[i]->rx_buf_len = rx_buf_len;
636         }
637 }
638
639 /**
640  * i40evf_find_vlan - Search filter list for specific vlan filter
641  * @adapter: board private structure
642  * @vlan: vlan tag
643  *
644  * Returns ptr to the filter object or NULL
645  **/
646 static struct
647 i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
648 {
649         struct i40evf_vlan_filter *f;
650
651         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
652                 if (vlan == f->vlan)
653                         return f;
654         }
655         return NULL;
656 }
657
658 /**
659  * i40evf_add_vlan - Add a vlan filter to the list
660  * @adapter: board private structure
661  * @vlan: VLAN tag
662  *
663  * Returns ptr to the filter object or NULL when no memory available.
664  **/
665 static struct
666 i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
667 {
668         struct i40evf_vlan_filter *f = NULL;
669         int count = 50;
670
671         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
672                                 &adapter->crit_section)) {
673                 udelay(1);
674                 if (--count == 0)
675                         goto out;
676         }
677
678         f = i40evf_find_vlan(adapter, vlan);
679         if (!f) {
680                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
681                 if (!f)
682                         goto clearout;
683
684                 f->vlan = vlan;
685
686                 INIT_LIST_HEAD(&f->list);
687                 list_add(&f->list, &adapter->vlan_filter_list);
688                 f->add = true;
689                 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
690         }
691
692 clearout:
693         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
694 out:
695         return f;
696 }
697
698 /**
699  * i40evf_del_vlan - Remove a vlan filter from the list
700  * @adapter: board private structure
701  * @vlan: VLAN tag
702  **/
703 static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
704 {
705         struct i40evf_vlan_filter *f;
706         int count = 50;
707
708         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
709                                 &adapter->crit_section)) {
710                 udelay(1);
711                 if (--count == 0)
712                         return;
713         }
714
715         f = i40evf_find_vlan(adapter, vlan);
716         if (f) {
717                 f->remove = true;
718                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
719         }
720         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
721 }
722
723 /**
724  * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
725  * @netdev: network device struct
726  * @vid: VLAN tag
727  **/
728 static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
729                                   __always_unused __be16 proto, u16 vid)
730 {
731         struct i40evf_adapter *adapter = netdev_priv(netdev);
732
733         if (!VLAN_ALLOWED(adapter))
734                 return -EIO;
735         if (i40evf_add_vlan(adapter, vid) == NULL)
736                 return -ENOMEM;
737         return 0;
738 }
739
740 /**
741  * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
742  * @netdev: network device struct
743  * @vid: VLAN tag
744  **/
745 static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
746                                    __always_unused __be16 proto, u16 vid)
747 {
748         struct i40evf_adapter *adapter = netdev_priv(netdev);
749
750         if (VLAN_ALLOWED(adapter)) {
751                 i40evf_del_vlan(adapter, vid);
752                 return 0;
753         }
754         return -EIO;
755 }
756
757 /**
758  * i40evf_find_filter - Search filter list for specific mac filter
759  * @adapter: board private structure
760  * @macaddr: the MAC address
761  *
762  * Returns ptr to the filter object or NULL
763  **/
764 static struct
765 i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
766                                       u8 *macaddr)
767 {
768         struct i40evf_mac_filter *f;
769
770         if (!macaddr)
771                 return NULL;
772
773         list_for_each_entry(f, &adapter->mac_filter_list, list) {
774                 if (ether_addr_equal(macaddr, f->macaddr))
775                         return f;
776         }
777         return NULL;
778 }
779
780 /**
781  * i40e_add_filter - Add a mac filter to the filter list
782  * @adapter: board private structure
783  * @macaddr: the MAC address
784  *
785  * Returns ptr to the filter object or NULL when no memory available.
786  **/
787 static struct
788 i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
789                                      u8 *macaddr)
790 {
791         struct i40evf_mac_filter *f;
792         int count = 50;
793
794         if (!macaddr)
795                 return NULL;
796
797         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
798                                 &adapter->crit_section)) {
799                 udelay(1);
800                 if (--count == 0)
801                         return NULL;
802         }
803
804         f = i40evf_find_filter(adapter, macaddr);
805         if (!f) {
806                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
807                 if (!f) {
808                         clear_bit(__I40EVF_IN_CRITICAL_TASK,
809                                   &adapter->crit_section);
810                         return NULL;
811                 }
812
813                 ether_addr_copy(f->macaddr, macaddr);
814
815                 list_add(&f->list, &adapter->mac_filter_list);
816                 f->add = true;
817                 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
818         }
819
820         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
821         return f;
822 }
823
824 /**
825  * i40evf_set_mac - NDO callback to set port mac address
826  * @netdev: network interface device structure
827  * @p: pointer to an address structure
828  *
829  * Returns 0 on success, negative on failure
830  **/
831 static int i40evf_set_mac(struct net_device *netdev, void *p)
832 {
833         struct i40evf_adapter *adapter = netdev_priv(netdev);
834         struct i40e_hw *hw = &adapter->hw;
835         struct i40evf_mac_filter *f;
836         struct sockaddr *addr = p;
837
838         if (!is_valid_ether_addr(addr->sa_data))
839                 return -EADDRNOTAVAIL;
840
841         if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
842                 return 0;
843
844         f = i40evf_add_filter(adapter, addr->sa_data);
845         if (f) {
846                 ether_addr_copy(hw->mac.addr, addr->sa_data);
847                 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
848         }
849
850         return (f == NULL) ? -ENOMEM : 0;
851 }
852
853 /**
854  * i40evf_set_rx_mode - NDO callback to set the netdev filters
855  * @netdev: network interface device structure
856  **/
857 static void i40evf_set_rx_mode(struct net_device *netdev)
858 {
859         struct i40evf_adapter *adapter = netdev_priv(netdev);
860         struct i40evf_mac_filter *f, *ftmp;
861         struct netdev_hw_addr *uca;
862         struct netdev_hw_addr *mca;
863         struct netdev_hw_addr *ha;
864         int count = 50;
865
866         /* add addr if not already in the filter list */
867         netdev_for_each_uc_addr(uca, netdev) {
868                 i40evf_add_filter(adapter, uca->addr);
869         }
870         netdev_for_each_mc_addr(mca, netdev) {
871                 i40evf_add_filter(adapter, mca->addr);
872         }
873
874         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
875                                 &adapter->crit_section)) {
876                 udelay(1);
877                 if (--count == 0) {
878                         dev_err(&adapter->pdev->dev,
879                                 "Failed to get lock in %s\n", __func__);
880                         return;
881                 }
882         }
883         /* remove filter if not in netdev list */
884         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
885                 netdev_for_each_mc_addr(mca, netdev)
886                         if (ether_addr_equal(mca->addr, f->macaddr))
887                                 goto bottom_of_search_loop;
888
889                 netdev_for_each_uc_addr(uca, netdev)
890                         if (ether_addr_equal(uca->addr, f->macaddr))
891                                 goto bottom_of_search_loop;
892
893                 for_each_dev_addr(netdev, ha)
894                         if (ether_addr_equal(ha->addr, f->macaddr))
895                                 goto bottom_of_search_loop;
896
897                 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
898                         goto bottom_of_search_loop;
899
900                 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
901                 f->remove = true;
902                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
903
904 bottom_of_search_loop:
905                 continue;
906         }
907         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
908 }
909
910 /**
911  * i40evf_napi_enable_all - enable NAPI on all queue vectors
912  * @adapter: board private structure
913  **/
914 static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
915 {
916         int q_idx;
917         struct i40e_q_vector *q_vector;
918         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
919
920         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
921                 struct napi_struct *napi;
922
923                 q_vector = adapter->q_vector[q_idx];
924                 napi = &q_vector->napi;
925                 napi_enable(napi);
926         }
927 }
928
929 /**
930  * i40evf_napi_disable_all - disable NAPI on all queue vectors
931  * @adapter: board private structure
932  **/
933 static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
934 {
935         int q_idx;
936         struct i40e_q_vector *q_vector;
937         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
938
939         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
940                 q_vector = adapter->q_vector[q_idx];
941                 napi_disable(&q_vector->napi);
942         }
943 }
944
945 /**
946  * i40evf_configure - set up transmit and receive data structures
947  * @adapter: board private structure
948  **/
949 static void i40evf_configure(struct i40evf_adapter *adapter)
950 {
951         struct net_device *netdev = adapter->netdev;
952         int i;
953
954         i40evf_set_rx_mode(netdev);
955
956         i40evf_configure_tx(adapter);
957         i40evf_configure_rx(adapter);
958         adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
959
960         for (i = 0; i < adapter->num_active_queues; i++) {
961                 struct i40e_ring *ring = adapter->rx_rings[i];
962
963                 i40evf_alloc_rx_buffers_1buf(ring, ring->count);
964                 ring->next_to_use = ring->count - 1;
965                 writel(ring->next_to_use, ring->tail);
966         }
967 }
968
969 /**
970  * i40evf_up_complete - Finish the last steps of bringing up a connection
971  * @adapter: board private structure
972  **/
973 static int i40evf_up_complete(struct i40evf_adapter *adapter)
974 {
975         adapter->state = __I40EVF_RUNNING;
976         clear_bit(__I40E_DOWN, &adapter->vsi.state);
977
978         i40evf_napi_enable_all(adapter);
979
980         adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
981         mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
982         return 0;
983 }
984
985 /**
986  * i40e_down - Shutdown the connection processing
987  * @adapter: board private structure
988  **/
989 void i40evf_down(struct i40evf_adapter *adapter)
990 {
991         struct net_device *netdev = adapter->netdev;
992         struct i40evf_mac_filter *f;
993
994         if (adapter->state == __I40EVF_DOWN)
995                 return;
996
997         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
998                                 &adapter->crit_section))
999                 usleep_range(500, 1000);
1000
1001         netif_carrier_off(netdev);
1002         netif_tx_disable(netdev);
1003         i40evf_napi_disable_all(adapter);
1004         i40evf_irq_disable(adapter);
1005
1006         /* remove all MAC filters */
1007         list_for_each_entry(f, &adapter->mac_filter_list, list) {
1008                 f->remove = true;
1009         }
1010         /* remove all VLAN filters */
1011         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1012                 f->remove = true;
1013         }
1014         if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1015             adapter->state != __I40EVF_RESETTING) {
1016                 /* cancel any current operation */
1017                 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1018                 /* Schedule operations to close down the HW. Don't wait
1019                  * here for this to complete. The watchdog is still running
1020                  * and it will take care of this.
1021                  */
1022                 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1023                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1024                 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1025         }
1026
1027         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1028 }
1029
1030 /**
1031  * i40evf_acquire_msix_vectors - Setup the MSIX capability
1032  * @adapter: board private structure
1033  * @vectors: number of vectors to request
1034  *
1035  * Work with the OS to set up the MSIX vectors needed.
1036  *
1037  * Returns 0 on success, negative on failure
1038  **/
1039 static int
1040 i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1041 {
1042         int err, vector_threshold;
1043
1044         /* We'll want at least 3 (vector_threshold):
1045          * 0) Other (Admin Queue and link, mostly)
1046          * 1) TxQ[0] Cleanup
1047          * 2) RxQ[0] Cleanup
1048          */
1049         vector_threshold = MIN_MSIX_COUNT;
1050
1051         /* The more we get, the more we will assign to Tx/Rx Cleanup
1052          * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1053          * Right now, we simply care about how many we'll get; we'll
1054          * set them up later while requesting irq's.
1055          */
1056         err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1057                                     vector_threshold, vectors);
1058         if (err < 0) {
1059                 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1060                 kfree(adapter->msix_entries);
1061                 adapter->msix_entries = NULL;
1062                 return err;
1063         }
1064
1065         /* Adjust for only the vectors we'll use, which is minimum
1066          * of max_msix_q_vectors + NONQ_VECS, or the number of
1067          * vectors we were allocated.
1068          */
1069         adapter->num_msix_vectors = err;
1070         return 0;
1071 }
1072
1073 /**
1074  * i40evf_free_queues - Free memory for all rings
1075  * @adapter: board private structure to initialize
1076  *
1077  * Free all of the memory associated with queue pairs.
1078  **/
1079 static void i40evf_free_queues(struct i40evf_adapter *adapter)
1080 {
1081         int i;
1082
1083         if (!adapter->vsi_res)
1084                 return;
1085         for (i = 0; i < adapter->num_active_queues; i++) {
1086                 if (adapter->tx_rings[i])
1087                         kfree_rcu(adapter->tx_rings[i], rcu);
1088                 adapter->tx_rings[i] = NULL;
1089                 adapter->rx_rings[i] = NULL;
1090         }
1091 }
1092
1093 /**
1094  * i40evf_alloc_queues - Allocate memory for all rings
1095  * @adapter: board private structure to initialize
1096  *
1097  * We allocate one ring per queue at run-time since we don't know the
1098  * number of queues at compile-time.  The polling_netdev array is
1099  * intended for Multiqueue, but should work fine with a single queue.
1100  **/
1101 static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1102 {
1103         int i;
1104
1105         for (i = 0; i < adapter->num_active_queues; i++) {
1106                 struct i40e_ring *tx_ring;
1107                 struct i40e_ring *rx_ring;
1108
1109                 tx_ring = kzalloc(sizeof(*tx_ring) * 2, GFP_KERNEL);
1110                 if (!tx_ring)
1111                         goto err_out;
1112
1113                 tx_ring->queue_index = i;
1114                 tx_ring->netdev = adapter->netdev;
1115                 tx_ring->dev = &adapter->pdev->dev;
1116                 tx_ring->count = adapter->tx_desc_count;
1117                 adapter->tx_rings[i] = tx_ring;
1118
1119                 rx_ring = &tx_ring[1];
1120                 rx_ring->queue_index = i;
1121                 rx_ring->netdev = adapter->netdev;
1122                 rx_ring->dev = &adapter->pdev->dev;
1123                 rx_ring->count = adapter->rx_desc_count;
1124                 adapter->rx_rings[i] = rx_ring;
1125         }
1126
1127         return 0;
1128
1129 err_out:
1130         i40evf_free_queues(adapter);
1131         return -ENOMEM;
1132 }
1133
1134 /**
1135  * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1136  * @adapter: board private structure to initialize
1137  *
1138  * Attempt to configure the interrupts using the best available
1139  * capabilities of the hardware and the kernel.
1140  **/
1141 static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1142 {
1143         int vector, v_budget;
1144         int pairs = 0;
1145         int err = 0;
1146
1147         if (!adapter->vsi_res) {
1148                 err = -EIO;
1149                 goto out;
1150         }
1151         pairs = adapter->num_active_queues;
1152
1153         /* It's easy to be greedy for MSI-X vectors, but it really
1154          * doesn't do us much good if we have a lot more vectors
1155          * than CPU's.  So let's be conservative and only ask for
1156          * (roughly) twice the number of vectors as there are CPU's.
1157          */
1158         v_budget = min_t(int, pairs, (int)(num_online_cpus() * 2)) + NONQ_VECS;
1159         v_budget = min_t(int, v_budget, (int)adapter->vf_res->max_vectors);
1160
1161         adapter->msix_entries = kcalloc(v_budget,
1162                                         sizeof(struct msix_entry), GFP_KERNEL);
1163         if (!adapter->msix_entries) {
1164                 err = -ENOMEM;
1165                 goto out;
1166         }
1167
1168         for (vector = 0; vector < v_budget; vector++)
1169                 adapter->msix_entries[vector].entry = vector;
1170
1171         err = i40evf_acquire_msix_vectors(adapter, v_budget);
1172
1173 out:
1174         adapter->netdev->real_num_tx_queues = pairs;
1175         return err;
1176 }
1177
1178 /**
1179  * i40e_configure_rss_aq - Prepare for RSS using AQ commands
1180  * @vsi: vsi structure
1181  * @seed: RSS hash seed
1182  **/
1183 static void i40evf_configure_rss_aq(struct i40e_vsi *vsi, const u8 *seed)
1184 {
1185         struct i40e_aqc_get_set_rss_key_data rss_key;
1186         struct i40evf_adapter *adapter = vsi->back;
1187         struct i40e_hw *hw = &adapter->hw;
1188         int ret = 0, i;
1189         u8 *rss_lut;
1190
1191         if (!vsi->id)
1192                 return;
1193
1194         if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
1195                 /* bail because we already have a command pending */
1196                 dev_err(&adapter->pdev->dev, "Cannot confiure RSS, command %d pending\n",
1197                         adapter->current_op);
1198                 return;
1199         }
1200
1201         memset(&rss_key, 0, sizeof(rss_key));
1202         memcpy(&rss_key, seed, sizeof(rss_key));
1203
1204         rss_lut = kzalloc(((I40E_VFQF_HLUT_MAX_INDEX + 1) * 4), GFP_KERNEL);
1205         if (!rss_lut)
1206                 return;
1207
1208         /* Populate the LUT with max no. PF queues in round robin fashion */
1209         for (i = 0; i <= (I40E_VFQF_HLUT_MAX_INDEX * 4); i++)
1210                 rss_lut[i] = i % adapter->num_active_queues;
1211
1212         ret = i40evf_aq_set_rss_key(hw, vsi->id, &rss_key);
1213         if (ret) {
1214                 dev_err(&adapter->pdev->dev,
1215                         "Cannot set RSS key, err %s aq_err %s\n",
1216                         i40evf_stat_str(hw, ret),
1217                         i40evf_aq_str(hw, hw->aq.asq_last_status));
1218                 return;
1219         }
1220
1221         ret = i40evf_aq_set_rss_lut(hw, vsi->id, false, rss_lut,
1222                                     (I40E_VFQF_HLUT_MAX_INDEX + 1) * 4);
1223         if (ret)
1224                 dev_err(&adapter->pdev->dev,
1225                         "Cannot set RSS lut, err %s aq_err %s\n",
1226                         i40evf_stat_str(hw, ret),
1227                         i40evf_aq_str(hw, hw->aq.asq_last_status));
1228 }
1229
1230 /**
1231  * i40e_configure_rss_reg - Prepare for RSS if used
1232  * @adapter: board private structure
1233  * @seed: RSS hash seed
1234  **/
1235 static void i40evf_configure_rss_reg(struct i40evf_adapter *adapter,
1236                                      const u8 *seed)
1237 {
1238         struct i40e_hw *hw = &adapter->hw;
1239         u32 *seed_dw = (u32 *)seed;
1240         u32 cqueue = 0;
1241         u32 lut = 0;
1242         int i, j;
1243
1244         /* Fill out hash function seed */
1245         for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
1246                 wr32(hw, I40E_VFQF_HKEY(i), seed_dw[i]);
1247
1248         /* Populate the LUT with max no. PF queues in round robin fashion */
1249         for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) {
1250                 lut = 0;
1251                 for (j = 0; j < 4; j++) {
1252                         if (cqueue == adapter->num_active_queues)
1253                                 cqueue = 0;
1254                         lut |= ((cqueue) << (8 * j));
1255                         cqueue++;
1256                 }
1257                 wr32(hw, I40E_VFQF_HLUT(i), lut);
1258         }
1259         i40e_flush(hw);
1260 }
1261
1262 /**
1263  * i40evf_configure_rss - Prepare for RSS
1264  * @adapter: board private structure
1265  **/
1266 static void i40evf_configure_rss(struct i40evf_adapter *adapter)
1267 {
1268         struct i40e_hw *hw = &adapter->hw;
1269         u8 seed[I40EVF_HKEY_ARRAY_SIZE];
1270         u64 hena;
1271
1272         netdev_rss_key_fill((void *)seed, I40EVF_HKEY_ARRAY_SIZE);
1273
1274         /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1275         hena = I40E_DEFAULT_RSS_HENA;
1276         wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1277         wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1278
1279         if (RSS_AQ(adapter))
1280                 i40evf_configure_rss_aq(&adapter->vsi, seed);
1281         else
1282                 i40evf_configure_rss_reg(adapter, seed);
1283 }
1284
1285 /**
1286  * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1287  * @adapter: board private structure to initialize
1288  *
1289  * We allocate one q_vector per queue interrupt.  If allocation fails we
1290  * return -ENOMEM.
1291  **/
1292 static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1293 {
1294         int q_idx, num_q_vectors;
1295         struct i40e_q_vector *q_vector;
1296
1297         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1298
1299         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1300                 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
1301                 if (!q_vector)
1302                         goto err_out;
1303                 q_vector->adapter = adapter;
1304                 q_vector->vsi = &adapter->vsi;
1305                 q_vector->v_idx = q_idx;
1306                 netif_napi_add(adapter->netdev, &q_vector->napi,
1307                                i40evf_napi_poll, NAPI_POLL_WEIGHT);
1308                 adapter->q_vector[q_idx] = q_vector;
1309         }
1310
1311         return 0;
1312
1313 err_out:
1314         while (q_idx) {
1315                 q_idx--;
1316                 q_vector = adapter->q_vector[q_idx];
1317                 netif_napi_del(&q_vector->napi);
1318                 kfree(q_vector);
1319                 adapter->q_vector[q_idx] = NULL;
1320         }
1321         return -ENOMEM;
1322 }
1323
1324 /**
1325  * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1326  * @adapter: board private structure to initialize
1327  *
1328  * This function frees the memory allocated to the q_vectors.  In addition if
1329  * NAPI is enabled it will delete any references to the NAPI struct prior
1330  * to freeing the q_vector.
1331  **/
1332 static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1333 {
1334         int q_idx, num_q_vectors;
1335         int napi_vectors;
1336
1337         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1338         napi_vectors = adapter->num_active_queues;
1339
1340         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1341                 struct i40e_q_vector *q_vector = adapter->q_vector[q_idx];
1342
1343                 adapter->q_vector[q_idx] = NULL;
1344                 if (q_idx < napi_vectors)
1345                         netif_napi_del(&q_vector->napi);
1346                 kfree(q_vector);
1347         }
1348 }
1349
1350 /**
1351  * i40evf_reset_interrupt_capability - Reset MSIX setup
1352  * @adapter: board private structure
1353  *
1354  **/
1355 void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1356 {
1357         pci_disable_msix(adapter->pdev);
1358         kfree(adapter->msix_entries);
1359         adapter->msix_entries = NULL;
1360 }
1361
1362 /**
1363  * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1364  * @adapter: board private structure to initialize
1365  *
1366  **/
1367 int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1368 {
1369         int err;
1370
1371         err = i40evf_set_interrupt_capability(adapter);
1372         if (err) {
1373                 dev_err(&adapter->pdev->dev,
1374                         "Unable to setup interrupt capabilities\n");
1375                 goto err_set_interrupt;
1376         }
1377
1378         err = i40evf_alloc_q_vectors(adapter);
1379         if (err) {
1380                 dev_err(&adapter->pdev->dev,
1381                         "Unable to allocate memory for queue vectors\n");
1382                 goto err_alloc_q_vectors;
1383         }
1384
1385         err = i40evf_alloc_queues(adapter);
1386         if (err) {
1387                 dev_err(&adapter->pdev->dev,
1388                         "Unable to allocate memory for queues\n");
1389                 goto err_alloc_queues;
1390         }
1391
1392         dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1393                  (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1394                  adapter->num_active_queues);
1395
1396         return 0;
1397 err_alloc_queues:
1398         i40evf_free_q_vectors(adapter);
1399 err_alloc_q_vectors:
1400         i40evf_reset_interrupt_capability(adapter);
1401 err_set_interrupt:
1402         return err;
1403 }
1404
1405 /**
1406  * i40evf_watchdog_timer - Periodic call-back timer
1407  * @data: pointer to adapter disguised as unsigned long
1408  **/
1409 static void i40evf_watchdog_timer(unsigned long data)
1410 {
1411         struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1412
1413         schedule_work(&adapter->watchdog_task);
1414         /* timer will be rescheduled in watchdog task */
1415 }
1416
1417 /**
1418  * i40evf_watchdog_task - Periodic call-back task
1419  * @work: pointer to work_struct
1420  **/
1421 static void i40evf_watchdog_task(struct work_struct *work)
1422 {
1423         struct i40evf_adapter *adapter = container_of(work,
1424                                                       struct i40evf_adapter,
1425                                                       watchdog_task);
1426         struct i40e_hw *hw = &adapter->hw;
1427         u32 reg_val;
1428
1429         if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1430                 goto restart_watchdog;
1431
1432         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1433                 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1434                           I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1435                 if ((reg_val == I40E_VFR_VFACTIVE) ||
1436                     (reg_val == I40E_VFR_COMPLETED)) {
1437                         /* A chance for redemption! */
1438                         dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1439                         adapter->state = __I40EVF_STARTUP;
1440                         adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1441                         schedule_delayed_work(&adapter->init_task, 10);
1442                         clear_bit(__I40EVF_IN_CRITICAL_TASK,
1443                                   &adapter->crit_section);
1444                         /* Don't reschedule the watchdog, since we've restarted
1445                          * the init task. When init_task contacts the PF and
1446                          * gets everything set up again, it'll restart the
1447                          * watchdog for us. Down, boy. Sit. Stay. Woof.
1448                          */
1449                         return;
1450                 }
1451                 adapter->aq_required = 0;
1452                 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1453                 goto watchdog_done;
1454         }
1455
1456         if ((adapter->state < __I40EVF_DOWN) ||
1457             (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1458                 goto watchdog_done;
1459
1460         /* check for reset */
1461         reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1462         if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
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         u32 reg_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                 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1599                           I40E_VF_ARQLEN1_ARQENABLE_MASK;
1600                 if (!reg_val)
1601                         break;
1602                 usleep_range(5000, 10000);
1603         }
1604         if (i == I40EVF_RESET_WAIT_COUNT) {
1605                 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1606                 goto continue_reset; /* act like the reset happened */
1607         }
1608
1609         /* wait until the reset is complete and the PF is responding to us */
1610         for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1611                 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1612                           I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1613                 if (reg_val == I40E_VFR_VFACTIVE)
1614                         break;
1615                 msleep(I40EVF_RESET_WAIT_MS);
1616         }
1617         /* extra wait to make sure minimum wait is met */
1618         msleep(I40EVF_RESET_WAIT_MS);
1619         if (i == I40EVF_RESET_WAIT_COUNT) {
1620                 struct i40evf_mac_filter *f, *ftmp;
1621                 struct i40evf_vlan_filter *fv, *fvtmp;
1622
1623                 /* reset never finished */
1624                 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1625                         reg_val);
1626                 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1627
1628                 if (netif_running(adapter->netdev)) {
1629                         set_bit(__I40E_DOWN, &adapter->vsi.state);
1630                         netif_carrier_off(netdev);
1631                         netif_tx_disable(netdev);
1632                         i40evf_napi_disable_all(adapter);
1633                         i40evf_irq_disable(adapter);
1634                         i40evf_free_traffic_irqs(adapter);
1635                         i40evf_free_all_tx_resources(adapter);
1636                         i40evf_free_all_rx_resources(adapter);
1637                 }
1638
1639                 /* Delete all of the filters, both MAC and VLAN. */
1640                 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1641                                          list) {
1642                         list_del(&f->list);
1643                         kfree(f);
1644                 }
1645
1646                 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list,
1647                                          list) {
1648                         list_del(&fv->list);
1649                         kfree(fv);
1650                 }
1651
1652                 i40evf_free_misc_irq(adapter);
1653                 i40evf_reset_interrupt_capability(adapter);
1654                 i40evf_free_queues(adapter);
1655                 i40evf_free_q_vectors(adapter);
1656                 kfree(adapter->vf_res);
1657                 i40evf_shutdown_adminq(hw);
1658                 adapter->netdev->flags &= ~IFF_UP;
1659                 clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1660                 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1661                 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1662                 return; /* Do not attempt to reinit. It's dead, Jim. */
1663         }
1664
1665 continue_reset:
1666         if (netif_running(adapter->netdev)) {
1667                 netif_carrier_off(netdev);
1668                 netif_tx_stop_all_queues(netdev);
1669                 i40evf_napi_disable_all(adapter);
1670         }
1671         i40evf_irq_disable(adapter);
1672
1673         adapter->state = __I40EVF_RESETTING;
1674         adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1675
1676         /* free the Tx/Rx rings and descriptors, might be better to just
1677          * re-use them sometime in the future
1678          */
1679         i40evf_free_all_rx_resources(adapter);
1680         i40evf_free_all_tx_resources(adapter);
1681
1682         /* kill and reinit the admin queue */
1683         if (i40evf_shutdown_adminq(hw))
1684                 dev_warn(&adapter->pdev->dev, "Failed to shut down adminq\n");
1685         adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
1686         err = i40evf_init_adminq(hw);
1687         if (err)
1688                 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1689                          err);
1690
1691         adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1692         adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
1693
1694         /* re-add all MAC filters */
1695         list_for_each_entry(f, &adapter->mac_filter_list, list) {
1696                 f->add = true;
1697         }
1698         /* re-add all VLAN filters */
1699         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1700                 f->add = true;
1701         }
1702         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1703         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
1704         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1705         i40evf_misc_irq_enable(adapter);
1706
1707         mod_timer(&adapter->watchdog_timer, jiffies + 2);
1708
1709         if (netif_running(adapter->netdev)) {
1710                 /* allocate transmit descriptors */
1711                 err = i40evf_setup_all_tx_resources(adapter);
1712                 if (err)
1713                         goto reset_err;
1714
1715                 /* allocate receive descriptors */
1716                 err = i40evf_setup_all_rx_resources(adapter);
1717                 if (err)
1718                         goto reset_err;
1719
1720                 i40evf_configure(adapter);
1721
1722                 err = i40evf_up_complete(adapter);
1723                 if (err)
1724                         goto reset_err;
1725
1726                 i40evf_irq_enable(adapter, true);
1727         } else {
1728                 adapter->state = __I40EVF_DOWN;
1729         }
1730
1731         return;
1732 reset_err:
1733         dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
1734         i40evf_close(adapter->netdev);
1735 }
1736
1737 /**
1738  * i40evf_adminq_task - worker thread to clean the admin queue
1739  * @work: pointer to work_struct containing our data
1740  **/
1741 static void i40evf_adminq_task(struct work_struct *work)
1742 {
1743         struct i40evf_adapter *adapter =
1744                 container_of(work, struct i40evf_adapter, adminq_task);
1745         struct i40e_hw *hw = &adapter->hw;
1746         struct i40e_arq_event_info event;
1747         struct i40e_virtchnl_msg *v_msg;
1748         i40e_status ret;
1749         u32 val, oldval;
1750         u16 pending;
1751
1752         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
1753                 goto out;
1754
1755         event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
1756         event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1757         if (!event.msg_buf)
1758                 goto out;
1759
1760         v_msg = (struct i40e_virtchnl_msg *)&event.desc;
1761         do {
1762                 ret = i40evf_clean_arq_element(hw, &event, &pending);
1763                 if (ret || !v_msg->v_opcode)
1764                         break; /* No event to process or error cleaning ARQ */
1765
1766                 i40evf_virtchnl_completion(adapter, v_msg->v_opcode,
1767                                            v_msg->v_retval, event.msg_buf,
1768                                            event.msg_len);
1769                 if (pending != 0)
1770                         memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
1771         } while (pending);
1772
1773         if ((adapter->flags &
1774              (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
1775             adapter->state == __I40EVF_RESETTING)
1776                 goto freedom;
1777
1778         /* check for error indications */
1779         val = rd32(hw, hw->aq.arq.len);
1780         oldval = val;
1781         if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
1782                 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
1783                 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
1784         }
1785         if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
1786                 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
1787                 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
1788         }
1789         if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
1790                 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
1791                 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
1792         }
1793         if (oldval != val)
1794                 wr32(hw, hw->aq.arq.len, val);
1795
1796         val = rd32(hw, hw->aq.asq.len);
1797         oldval = val;
1798         if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
1799                 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
1800                 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
1801         }
1802         if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
1803                 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
1804                 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
1805         }
1806         if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
1807                 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
1808                 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
1809         }
1810         if (oldval != val)
1811                 wr32(hw, hw->aq.asq.len, val);
1812
1813 freedom:
1814         kfree(event.msg_buf);
1815 out:
1816         /* re-enable Admin queue interrupt cause */
1817         i40evf_misc_irq_enable(adapter);
1818 }
1819
1820 /**
1821  * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
1822  * @adapter: board private structure
1823  *
1824  * Free all transmit software resources
1825  **/
1826 void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
1827 {
1828         int i;
1829
1830         for (i = 0; i < adapter->num_active_queues; i++)
1831                 if (adapter->tx_rings[i]->desc)
1832                         i40evf_free_tx_resources(adapter->tx_rings[i]);
1833 }
1834
1835 /**
1836  * i40evf_setup_all_tx_resources - allocate all queues Tx resources
1837  * @adapter: board private structure
1838  *
1839  * If this function returns with an error, then it's possible one or
1840  * more of the rings is populated (while the rest are not).  It is the
1841  * callers duty to clean those orphaned rings.
1842  *
1843  * Return 0 on success, negative on failure
1844  **/
1845 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
1846 {
1847         int i, err = 0;
1848
1849         for (i = 0; i < adapter->num_active_queues; i++) {
1850                 adapter->tx_rings[i]->count = adapter->tx_desc_count;
1851                 err = i40evf_setup_tx_descriptors(adapter->tx_rings[i]);
1852                 if (!err)
1853                         continue;
1854                 dev_err(&adapter->pdev->dev,
1855                         "Allocation for Tx Queue %u failed\n", i);
1856                 break;
1857         }
1858
1859         return err;
1860 }
1861
1862 /**
1863  * i40evf_setup_all_rx_resources - allocate all queues Rx resources
1864  * @adapter: board private structure
1865  *
1866  * If this function returns with an error, then it's possible one or
1867  * more of the rings is populated (while the rest are not).  It is the
1868  * callers duty to clean those orphaned rings.
1869  *
1870  * Return 0 on success, negative on failure
1871  **/
1872 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
1873 {
1874         int i, err = 0;
1875
1876         for (i = 0; i < adapter->num_active_queues; i++) {
1877                 adapter->rx_rings[i]->count = adapter->rx_desc_count;
1878                 err = i40evf_setup_rx_descriptors(adapter->rx_rings[i]);
1879                 if (!err)
1880                         continue;
1881                 dev_err(&adapter->pdev->dev,
1882                         "Allocation for Rx Queue %u failed\n", i);
1883                 break;
1884         }
1885         return err;
1886 }
1887
1888 /**
1889  * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
1890  * @adapter: board private structure
1891  *
1892  * Free all receive software resources
1893  **/
1894 void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
1895 {
1896         int i;
1897
1898         for (i = 0; i < adapter->num_active_queues; i++)
1899                 if (adapter->rx_rings[i]->desc)
1900                         i40evf_free_rx_resources(adapter->rx_rings[i]);
1901 }
1902
1903 /**
1904  * i40evf_open - Called when a network interface is made active
1905  * @netdev: network interface device structure
1906  *
1907  * Returns 0 on success, negative value on failure
1908  *
1909  * The open entry point is called when a network interface is made
1910  * active by the system (IFF_UP).  At this point all resources needed
1911  * for transmit and receive operations are allocated, the interrupt
1912  * handler is registered with the OS, the watchdog timer is started,
1913  * and the stack is notified that the interface is ready.
1914  **/
1915 static int i40evf_open(struct net_device *netdev)
1916 {
1917         struct i40evf_adapter *adapter = netdev_priv(netdev);
1918         int err;
1919
1920         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1921                 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
1922                 return -EIO;
1923         }
1924         if (adapter->state != __I40EVF_DOWN || adapter->aq_required)
1925                 return -EBUSY;
1926
1927         /* allocate transmit descriptors */
1928         err = i40evf_setup_all_tx_resources(adapter);
1929         if (err)
1930                 goto err_setup_tx;
1931
1932         /* allocate receive descriptors */
1933         err = i40evf_setup_all_rx_resources(adapter);
1934         if (err)
1935                 goto err_setup_rx;
1936
1937         /* clear any pending interrupts, may auto mask */
1938         err = i40evf_request_traffic_irqs(adapter, netdev->name);
1939         if (err)
1940                 goto err_req_irq;
1941
1942         i40evf_add_filter(adapter, adapter->hw.mac.addr);
1943         i40evf_configure(adapter);
1944
1945         err = i40evf_up_complete(adapter);
1946         if (err)
1947                 goto err_req_irq;
1948
1949         i40evf_irq_enable(adapter, true);
1950
1951         return 0;
1952
1953 err_req_irq:
1954         i40evf_down(adapter);
1955         i40evf_free_traffic_irqs(adapter);
1956 err_setup_rx:
1957         i40evf_free_all_rx_resources(adapter);
1958 err_setup_tx:
1959         i40evf_free_all_tx_resources(adapter);
1960
1961         return err;
1962 }
1963
1964 /**
1965  * i40evf_close - Disables a network interface
1966  * @netdev: network interface device structure
1967  *
1968  * Returns 0, this is not allowed to fail
1969  *
1970  * The close entry point is called when an interface is de-activated
1971  * by the OS.  The hardware is still under the drivers control, but
1972  * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
1973  * are freed, along with all transmit and receive resources.
1974  **/
1975 static int i40evf_close(struct net_device *netdev)
1976 {
1977         struct i40evf_adapter *adapter = netdev_priv(netdev);
1978
1979         if (adapter->state <= __I40EVF_DOWN)
1980                 return 0;
1981
1982
1983         set_bit(__I40E_DOWN, &adapter->vsi.state);
1984
1985         i40evf_down(adapter);
1986         adapter->state = __I40EVF_DOWN;
1987         i40evf_free_traffic_irqs(adapter);
1988
1989         return 0;
1990 }
1991
1992 /**
1993  * i40evf_get_stats - Get System Network Statistics
1994  * @netdev: network interface device structure
1995  *
1996  * Returns the address of the device statistics structure.
1997  * The statistics are actually updated from the timer callback.
1998  **/
1999 static struct net_device_stats *i40evf_get_stats(struct net_device *netdev)
2000 {
2001         struct i40evf_adapter *adapter = netdev_priv(netdev);
2002
2003         /* only return the current stats */
2004         return &adapter->net_stats;
2005 }
2006
2007 /**
2008  * i40evf_change_mtu - Change the Maximum Transfer Unit
2009  * @netdev: network interface device structure
2010  * @new_mtu: new value for maximum frame size
2011  *
2012  * Returns 0 on success, negative on failure
2013  **/
2014 static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2015 {
2016         struct i40evf_adapter *adapter = netdev_priv(netdev);
2017         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
2018
2019         if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2020                 return -EINVAL;
2021
2022         netdev->mtu = new_mtu;
2023         adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2024         schedule_work(&adapter->reset_task);
2025
2026         return 0;
2027 }
2028
2029 static const struct net_device_ops i40evf_netdev_ops = {
2030         .ndo_open               = i40evf_open,
2031         .ndo_stop               = i40evf_close,
2032         .ndo_start_xmit         = i40evf_xmit_frame,
2033         .ndo_get_stats          = i40evf_get_stats,
2034         .ndo_set_rx_mode        = i40evf_set_rx_mode,
2035         .ndo_validate_addr      = eth_validate_addr,
2036         .ndo_set_mac_address    = i40evf_set_mac,
2037         .ndo_change_mtu         = i40evf_change_mtu,
2038         .ndo_tx_timeout         = i40evf_tx_timeout,
2039         .ndo_vlan_rx_add_vid    = i40evf_vlan_rx_add_vid,
2040         .ndo_vlan_rx_kill_vid   = i40evf_vlan_rx_kill_vid,
2041 };
2042
2043 /**
2044  * i40evf_check_reset_complete - check that VF reset is complete
2045  * @hw: pointer to hw struct
2046  *
2047  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2048  **/
2049 static int i40evf_check_reset_complete(struct i40e_hw *hw)
2050 {
2051         u32 rstat;
2052         int i;
2053
2054         for (i = 0; i < 100; i++) {
2055                 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2056                             I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2057                 if ((rstat == I40E_VFR_VFACTIVE) ||
2058                     (rstat == I40E_VFR_COMPLETED))
2059                         return 0;
2060                 usleep_range(10, 20);
2061         }
2062         return -EBUSY;
2063 }
2064
2065 /**
2066  * i40evf_process_config - Process the config information we got from the PF
2067  * @adapter: board private structure
2068  *
2069  * Verify that we have a valid config struct, and set up our netdev features
2070  * and our VSI struct.
2071  **/
2072 int i40evf_process_config(struct i40evf_adapter *adapter)
2073 {
2074         struct net_device *netdev = adapter->netdev;
2075         int i;
2076
2077         /* got VF config message back from PF, now we can parse it */
2078         for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2079                 if (adapter->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
2080                         adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2081         }
2082         if (!adapter->vsi_res) {
2083                 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2084                 return -ENODEV;
2085         }
2086
2087         if (adapter->vf_res->vf_offload_flags
2088             & I40E_VIRTCHNL_VF_OFFLOAD_VLAN) {
2089                 netdev->vlan_features = netdev->features;
2090                 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2091                                     NETIF_F_HW_VLAN_CTAG_RX |
2092                                     NETIF_F_HW_VLAN_CTAG_FILTER;
2093         }
2094         netdev->features |= NETIF_F_HIGHDMA |
2095                             NETIF_F_SG |
2096                             NETIF_F_IP_CSUM |
2097                             NETIF_F_SCTP_CSUM |
2098                             NETIF_F_IPV6_CSUM |
2099                             NETIF_F_TSO |
2100                             NETIF_F_TSO6 |
2101                             NETIF_F_RXCSUM |
2102                             NETIF_F_GRO;
2103
2104         /* copy netdev features into list of user selectable features */
2105         netdev->hw_features |= netdev->features;
2106         netdev->hw_features &= ~NETIF_F_RXCSUM;
2107
2108         adapter->vsi.id = adapter->vsi_res->vsi_id;
2109
2110         adapter->vsi.back = adapter;
2111         adapter->vsi.base_vector = 1;
2112         adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2113         adapter->vsi.rx_itr_setting = (I40E_ITR_DYNAMIC |
2114                                        ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
2115         adapter->vsi.tx_itr_setting = (I40E_ITR_DYNAMIC |
2116                                        ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
2117         adapter->vsi.netdev = adapter->netdev;
2118         adapter->vsi.qs_handle = adapter->vsi_res->qset_handle;
2119         return 0;
2120 }
2121
2122 /**
2123  * i40evf_init_task - worker thread to perform delayed initialization
2124  * @work: pointer to work_struct containing our data
2125  *
2126  * This task completes the work that was begun in probe. Due to the nature
2127  * of VF-PF communications, we may need to wait tens of milliseconds to get
2128  * responses back from the PF. Rather than busy-wait in probe and bog down the
2129  * whole system, we'll do it in a task so we can sleep.
2130  * This task only runs during driver init. Once we've established
2131  * communications with the PF driver and set up our netdev, the watchdog
2132  * takes over.
2133  **/
2134 static void i40evf_init_task(struct work_struct *work)
2135 {
2136         struct i40evf_adapter *adapter = container_of(work,
2137                                                       struct i40evf_adapter,
2138                                                       init_task.work);
2139         struct net_device *netdev = adapter->netdev;
2140         struct i40e_hw *hw = &adapter->hw;
2141         struct pci_dev *pdev = adapter->pdev;
2142         int err, bufsz;
2143
2144         switch (adapter->state) {
2145         case __I40EVF_STARTUP:
2146                 /* driver loaded, probe complete */
2147                 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2148                 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
2149                 err = i40e_set_mac_type(hw);
2150                 if (err) {
2151                         dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2152                                 err);
2153                         goto err;
2154                 }
2155                 err = i40evf_check_reset_complete(hw);
2156                 if (err) {
2157                         dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2158                                  err);
2159                         goto err;
2160                 }
2161                 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2162                 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2163                 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2164                 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2165
2166                 err = i40evf_init_adminq(hw);
2167                 if (err) {
2168                         dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2169                                 err);
2170                         goto err;
2171                 }
2172                 err = i40evf_send_api_ver(adapter);
2173                 if (err) {
2174                         dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
2175                         i40evf_shutdown_adminq(hw);
2176                         goto err;
2177                 }
2178                 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2179                 goto restart;
2180         case __I40EVF_INIT_VERSION_CHECK:
2181                 if (!i40evf_asq_done(hw)) {
2182                         dev_err(&pdev->dev, "Admin queue command never completed\n");
2183                         i40evf_shutdown_adminq(hw);
2184                         adapter->state = __I40EVF_STARTUP;
2185                         goto err;
2186                 }
2187
2188                 /* aq msg sent, awaiting reply */
2189                 err = i40evf_verify_api_ver(adapter);
2190                 if (err) {
2191                         if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2192                                 err = i40evf_send_api_ver(adapter);
2193                         else
2194                                 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2195                                         adapter->pf_version.major,
2196                                         adapter->pf_version.minor,
2197                                         I40E_VIRTCHNL_VERSION_MAJOR,
2198                                         I40E_VIRTCHNL_VERSION_MINOR);
2199                         goto err;
2200                 }
2201                 err = i40evf_send_vf_config_msg(adapter);
2202                 if (err) {
2203                         dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2204                                 err);
2205                         goto err;
2206                 }
2207                 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2208                 goto restart;
2209         case __I40EVF_INIT_GET_RESOURCES:
2210                 /* aq msg sent, awaiting reply */
2211                 if (!adapter->vf_res) {
2212                         bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
2213                                 (I40E_MAX_VF_VSI *
2214                                  sizeof(struct i40e_virtchnl_vsi_resource));
2215                         adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
2216                         if (!adapter->vf_res)
2217                                 goto err;
2218                 }
2219                 err = i40evf_get_vf_config(adapter);
2220                 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2221                         err = i40evf_send_vf_config_msg(adapter);
2222                         goto err;
2223                 }
2224                 if (err) {
2225                         dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2226                                 err);
2227                         goto err_alloc;
2228                 }
2229                 adapter->state = __I40EVF_INIT_SW;
2230                 break;
2231         default:
2232                 goto err_alloc;
2233         }
2234         if (i40evf_process_config(adapter))
2235                 goto err_alloc;
2236         adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
2237
2238         adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2239
2240         netdev->netdev_ops = &i40evf_netdev_ops;
2241         i40evf_set_ethtool_ops(netdev);
2242         netdev->watchdog_timeo = 5 * HZ;
2243
2244         if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2245                 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2246                          adapter->hw.mac.addr);
2247                 random_ether_addr(adapter->hw.mac.addr);
2248         }
2249         ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2250         ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2251
2252         init_timer(&adapter->watchdog_timer);
2253         adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2254         adapter->watchdog_timer.data = (unsigned long)adapter;
2255         mod_timer(&adapter->watchdog_timer, jiffies + 1);
2256
2257         adapter->num_active_queues = min_t(int,
2258                                            adapter->vsi_res->num_queue_pairs,
2259                                            (int)(num_online_cpus()));
2260         adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2261         adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
2262         err = i40evf_init_interrupt_scheme(adapter);
2263         if (err)
2264                 goto err_sw_init;
2265         i40evf_map_rings_to_vectors(adapter);
2266         if (!RSS_AQ(adapter))
2267                 i40evf_configure_rss(adapter);
2268         err = i40evf_request_misc_irq(adapter);
2269         if (err)
2270                 goto err_sw_init;
2271
2272         netif_carrier_off(netdev);
2273
2274         if (!adapter->netdev_registered) {
2275                 err = register_netdev(netdev);
2276                 if (err)
2277                         goto err_register;
2278         }
2279
2280         adapter->netdev_registered = true;
2281
2282         netif_tx_stop_all_queues(netdev);
2283
2284         dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2285         if (netdev->features & NETIF_F_GRO)
2286                 dev_info(&pdev->dev, "GRO is enabled\n");
2287
2288         dev_info(&pdev->dev, "%s\n", i40evf_driver_string);
2289         adapter->state = __I40EVF_DOWN;
2290         set_bit(__I40E_DOWN, &adapter->vsi.state);
2291         i40evf_misc_irq_enable(adapter);
2292
2293         if (RSS_AQ(adapter)) {
2294                 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2295                 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2296         } else {
2297                 i40evf_configure_rss(adapter);
2298         }
2299         return;
2300 restart:
2301         schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
2302         return;
2303
2304 err_register:
2305         i40evf_free_misc_irq(adapter);
2306 err_sw_init:
2307         i40evf_reset_interrupt_capability(adapter);
2308 err_alloc:
2309         kfree(adapter->vf_res);
2310         adapter->vf_res = NULL;
2311 err:
2312         /* Things went into the weeds, so try again later */
2313         if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2314                 dev_err(&pdev->dev, "Failed to communicate with PF; giving up\n");
2315                 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
2316                 return; /* do not reschedule */
2317         }
2318         schedule_delayed_work(&adapter->init_task, HZ * 3);
2319 }
2320
2321 /**
2322  * i40evf_shutdown - Shutdown the device in preparation for a reboot
2323  * @pdev: pci device structure
2324  **/
2325 static void i40evf_shutdown(struct pci_dev *pdev)
2326 {
2327         struct net_device *netdev = pci_get_drvdata(pdev);
2328         struct i40evf_adapter *adapter = netdev_priv(netdev);
2329
2330         netif_device_detach(netdev);
2331
2332         if (netif_running(netdev))
2333                 i40evf_close(netdev);
2334
2335         /* Prevent the watchdog from running. */
2336         adapter->state = __I40EVF_REMOVE;
2337         adapter->aq_required = 0;
2338
2339 #ifdef CONFIG_PM
2340         pci_save_state(pdev);
2341
2342 #endif
2343         pci_disable_device(pdev);
2344 }
2345
2346 /**
2347  * i40evf_probe - Device Initialization Routine
2348  * @pdev: PCI device information struct
2349  * @ent: entry in i40evf_pci_tbl
2350  *
2351  * Returns 0 on success, negative on failure
2352  *
2353  * i40evf_probe initializes an adapter identified by a pci_dev structure.
2354  * The OS initialization, configuring of the adapter private structure,
2355  * and a hardware reset occur.
2356  **/
2357 static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2358 {
2359         struct net_device *netdev;
2360         struct i40evf_adapter *adapter = NULL;
2361         struct i40e_hw *hw = NULL;
2362         int err;
2363
2364         err = pci_enable_device(pdev);
2365         if (err)
2366                 return err;
2367
2368         err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2369         if (err) {
2370                 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2371                 if (err) {
2372                         dev_err(&pdev->dev,
2373                                 "DMA configuration failed: 0x%x\n", err);
2374                         goto err_dma;
2375                 }
2376         }
2377
2378         err = pci_request_regions(pdev, i40evf_driver_name);
2379         if (err) {
2380                 dev_err(&pdev->dev,
2381                         "pci_request_regions failed 0x%x\n", err);
2382                 goto err_pci_reg;
2383         }
2384
2385         pci_enable_pcie_error_reporting(pdev);
2386
2387         pci_set_master(pdev);
2388
2389         netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
2390                                    MAX_TX_QUEUES);
2391         if (!netdev) {
2392                 err = -ENOMEM;
2393                 goto err_alloc_etherdev;
2394         }
2395
2396         SET_NETDEV_DEV(netdev, &pdev->dev);
2397
2398         pci_set_drvdata(pdev, netdev);
2399         adapter = netdev_priv(netdev);
2400
2401         adapter->netdev = netdev;
2402         adapter->pdev = pdev;
2403
2404         hw = &adapter->hw;
2405         hw->back = adapter;
2406
2407         adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2408         adapter->state = __I40EVF_STARTUP;
2409
2410         /* Call save state here because it relies on the adapter struct. */
2411         pci_save_state(pdev);
2412
2413         hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2414                               pci_resource_len(pdev, 0));
2415         if (!hw->hw_addr) {
2416                 err = -EIO;
2417                 goto err_ioremap;
2418         }
2419         hw->vendor_id = pdev->vendor;
2420         hw->device_id = pdev->device;
2421         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2422         hw->subsystem_vendor_id = pdev->subsystem_vendor;
2423         hw->subsystem_device_id = pdev->subsystem_device;
2424         hw->bus.device = PCI_SLOT(pdev->devfn);
2425         hw->bus.func = PCI_FUNC(pdev->devfn);
2426
2427         INIT_LIST_HEAD(&adapter->mac_filter_list);
2428         INIT_LIST_HEAD(&adapter->vlan_filter_list);
2429
2430         INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2431         INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2432         INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2433         INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2434         schedule_delayed_work(&adapter->init_task,
2435                               msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
2436
2437         return 0;
2438
2439 err_ioremap:
2440         free_netdev(netdev);
2441 err_alloc_etherdev:
2442         pci_release_regions(pdev);
2443 err_pci_reg:
2444 err_dma:
2445         pci_disable_device(pdev);
2446         return err;
2447 }
2448
2449 #ifdef CONFIG_PM
2450 /**
2451  * i40evf_suspend - Power management suspend routine
2452  * @pdev: PCI device information struct
2453  * @state: unused
2454  *
2455  * Called when the system (VM) is entering sleep/suspend.
2456  **/
2457 static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2458 {
2459         struct net_device *netdev = pci_get_drvdata(pdev);
2460         struct i40evf_adapter *adapter = netdev_priv(netdev);
2461         int retval = 0;
2462
2463         netif_device_detach(netdev);
2464
2465         if (netif_running(netdev)) {
2466                 rtnl_lock();
2467                 i40evf_down(adapter);
2468                 rtnl_unlock();
2469         }
2470         i40evf_free_misc_irq(adapter);
2471         i40evf_reset_interrupt_capability(adapter);
2472
2473         retval = pci_save_state(pdev);
2474         if (retval)
2475                 return retval;
2476
2477         pci_disable_device(pdev);
2478
2479         return 0;
2480 }
2481
2482 /**
2483  * i40evf_resume - Power management resume routine
2484  * @pdev: PCI device information struct
2485  *
2486  * Called when the system (VM) is resumed from sleep/suspend.
2487  **/
2488 static int i40evf_resume(struct pci_dev *pdev)
2489 {
2490         struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2491         struct net_device *netdev = adapter->netdev;
2492         u32 err;
2493
2494         pci_set_power_state(pdev, PCI_D0);
2495         pci_restore_state(pdev);
2496         /* pci_restore_state clears dev->state_saved so call
2497          * pci_save_state to restore it.
2498          */
2499         pci_save_state(pdev);
2500
2501         err = pci_enable_device_mem(pdev);
2502         if (err) {
2503                 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2504                 return err;
2505         }
2506         pci_set_master(pdev);
2507
2508         rtnl_lock();
2509         err = i40evf_set_interrupt_capability(adapter);
2510         if (err) {
2511                 rtnl_unlock();
2512                 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2513                 return err;
2514         }
2515         err = i40evf_request_misc_irq(adapter);
2516         rtnl_unlock();
2517         if (err) {
2518                 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
2519                 return err;
2520         }
2521
2522         schedule_work(&adapter->reset_task);
2523
2524         netif_device_attach(netdev);
2525
2526         return err;
2527 }
2528
2529 #endif /* CONFIG_PM */
2530 /**
2531  * i40evf_remove - Device Removal Routine
2532  * @pdev: PCI device information struct
2533  *
2534  * i40evf_remove is called by the PCI subsystem to alert the driver
2535  * that it should release a PCI device.  The could be caused by a
2536  * Hot-Plug event, or because the driver is going to be removed from
2537  * memory.
2538  **/
2539 static void i40evf_remove(struct pci_dev *pdev)
2540 {
2541         struct net_device *netdev = pci_get_drvdata(pdev);
2542         struct i40evf_adapter *adapter = netdev_priv(netdev);
2543         struct i40evf_mac_filter *f, *ftmp;
2544         struct i40e_hw *hw = &adapter->hw;
2545
2546         cancel_delayed_work_sync(&adapter->init_task);
2547         cancel_work_sync(&adapter->reset_task);
2548
2549         if (adapter->netdev_registered) {
2550                 unregister_netdev(netdev);
2551                 adapter->netdev_registered = false;
2552         }
2553
2554         /* Shut down all the garbage mashers on the detention level */
2555         adapter->state = __I40EVF_REMOVE;
2556         adapter->aq_required = 0;
2557         i40evf_request_reset(adapter);
2558         msleep(20);
2559         /* If the FW isn't responding, kick it once, but only once. */
2560         if (!i40evf_asq_done(hw)) {
2561                 i40evf_request_reset(adapter);
2562                 msleep(20);
2563         }
2564
2565         if (adapter->msix_entries) {
2566                 i40evf_misc_irq_disable(adapter);
2567                 i40evf_free_misc_irq(adapter);
2568                 i40evf_reset_interrupt_capability(adapter);
2569                 i40evf_free_q_vectors(adapter);
2570         }
2571
2572         if (adapter->watchdog_timer.function)
2573                 del_timer_sync(&adapter->watchdog_timer);
2574
2575         flush_scheduled_work();
2576
2577         if (hw->aq.asq.count)
2578                 i40evf_shutdown_adminq(hw);
2579
2580         iounmap(hw->hw_addr);
2581         pci_release_regions(pdev);
2582
2583         i40evf_free_all_tx_resources(adapter);
2584         i40evf_free_all_rx_resources(adapter);
2585         i40evf_free_queues(adapter);
2586         kfree(adapter->vf_res);
2587         /* If we got removed before an up/down sequence, we've got a filter
2588          * hanging out there that we need to get rid of.
2589          */
2590         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2591                 list_del(&f->list);
2592                 kfree(f);
2593         }
2594         list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
2595                 list_del(&f->list);
2596                 kfree(f);
2597         }
2598
2599         free_netdev(netdev);
2600
2601         pci_disable_pcie_error_reporting(pdev);
2602
2603         pci_disable_device(pdev);
2604 }
2605
2606 static struct pci_driver i40evf_driver = {
2607         .name     = i40evf_driver_name,
2608         .id_table = i40evf_pci_tbl,
2609         .probe    = i40evf_probe,
2610         .remove   = i40evf_remove,
2611 #ifdef CONFIG_PM
2612         .suspend  = i40evf_suspend,
2613         .resume   = i40evf_resume,
2614 #endif
2615         .shutdown = i40evf_shutdown,
2616 };
2617
2618 /**
2619  * i40e_init_module - Driver Registration Routine
2620  *
2621  * i40e_init_module is the first routine called when the driver is
2622  * loaded. All it does is register with the PCI subsystem.
2623  **/
2624 static int __init i40evf_init_module(void)
2625 {
2626         int ret;
2627
2628         pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
2629                 i40evf_driver_version);
2630
2631         pr_info("%s\n", i40evf_copyright);
2632
2633         ret = pci_register_driver(&i40evf_driver);
2634         return ret;
2635 }
2636
2637 module_init(i40evf_init_module);
2638
2639 /**
2640  * i40e_exit_module - Driver Exit Cleanup Routine
2641  *
2642  * i40e_exit_module is called just before the driver is removed
2643  * from memory.
2644  **/
2645 static void __exit i40evf_exit_module(void)
2646 {
2647         pci_unregister_driver(&i40evf_driver);
2648 }
2649
2650 module_exit(i40evf_exit_module);
2651
2652 /* i40evf_main.c */