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