GNU Linux-libre 4.14.290-gnu1
[releases.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 - 2016 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 #include "i40evf_client.h"
30 /* All i40evf tracepoints are defined by the include below, which must
31  * be included exactly once across the whole kernel with
32  * CREATE_TRACE_POINTS defined
33  */
34 #define CREATE_TRACE_POINTS
35 #include "i40e_trace.h"
36
37 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
38 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
39 static int i40evf_close(struct net_device *netdev);
40
41 char i40evf_driver_name[] = "i40evf";
42 static const char i40evf_driver_string[] =
43         "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
44
45 #define DRV_KERN "-k"
46
47 #define DRV_VERSION_MAJOR 3
48 #define DRV_VERSION_MINOR 0
49 #define DRV_VERSION_BUILD 0
50 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51              __stringify(DRV_VERSION_MINOR) "." \
52              __stringify(DRV_VERSION_BUILD) \
53              DRV_KERN
54 const char i40evf_driver_version[] = DRV_VERSION;
55 static const char i40evf_copyright[] =
56         "Copyright (c) 2013 - 2015 Intel Corporation.";
57
58 /* i40evf_pci_tbl - PCI Device ID Table
59  *
60  * Wildcard entries (PCI_ANY_ID) should come last
61  * Last entry must be all 0s
62  *
63  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64  *   Class, Class Mask, private data (not used) }
65  */
66 static const struct pci_device_id i40evf_pci_tbl[] = {
67         {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
68         {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
69         {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
70         {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
71         /* required last entry */
72         {0, }
73 };
74
75 MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
76
77 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
78 MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
79 MODULE_LICENSE("GPL");
80 MODULE_VERSION(DRV_VERSION);
81
82 static struct workqueue_struct *i40evf_wq;
83
84 /**
85  * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
86  * @hw:   pointer to the HW structure
87  * @mem:  ptr to mem struct to fill out
88  * @size: size of memory requested
89  * @alignment: what to align the allocation to
90  **/
91 i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
92                                       struct i40e_dma_mem *mem,
93                                       u64 size, u32 alignment)
94 {
95         struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
96
97         if (!mem)
98                 return I40E_ERR_PARAM;
99
100         mem->size = ALIGN(size, alignment);
101         mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
102                                      (dma_addr_t *)&mem->pa, GFP_KERNEL);
103         if (mem->va)
104                 return 0;
105         else
106                 return I40E_ERR_NO_MEMORY;
107 }
108
109 /**
110  * i40evf_free_dma_mem_d - OS specific memory free for shared code
111  * @hw:   pointer to the HW structure
112  * @mem:  ptr to mem struct to free
113  **/
114 i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
115 {
116         struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
117
118         if (!mem || !mem->va)
119                 return I40E_ERR_PARAM;
120         dma_free_coherent(&adapter->pdev->dev, mem->size,
121                           mem->va, (dma_addr_t)mem->pa);
122         return 0;
123 }
124
125 /**
126  * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
127  * @hw:   pointer to the HW structure
128  * @mem:  ptr to mem struct to fill out
129  * @size: size of memory requested
130  **/
131 i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
132                                        struct i40e_virt_mem *mem, u32 size)
133 {
134         if (!mem)
135                 return I40E_ERR_PARAM;
136
137         mem->size = size;
138         mem->va = kzalloc(size, GFP_KERNEL);
139
140         if (mem->va)
141                 return 0;
142         else
143                 return I40E_ERR_NO_MEMORY;
144 }
145
146 /**
147  * i40evf_free_virt_mem_d - OS specific memory free for shared code
148  * @hw:   pointer to the HW structure
149  * @mem:  ptr to mem struct to free
150  **/
151 i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
152                                    struct i40e_virt_mem *mem)
153 {
154         if (!mem)
155                 return I40E_ERR_PARAM;
156
157         /* it's ok to kfree a NULL pointer */
158         kfree(mem->va);
159
160         return 0;
161 }
162
163 /**
164  * i40evf_debug_d - OS dependent version of debug printing
165  * @hw:  pointer to the HW structure
166  * @mask: debug level mask
167  * @fmt_str: printf-type format description
168  **/
169 void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
170 {
171         char buf[512];
172         va_list argptr;
173
174         if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
175                 return;
176
177         va_start(argptr, fmt_str);
178         vsnprintf(buf, sizeof(buf), fmt_str, argptr);
179         va_end(argptr);
180
181         /* the debug string is already formatted with a newline */
182         pr_info("%s", buf);
183 }
184
185 /**
186  * i40evf_schedule_reset - Set the flags and schedule a reset event
187  * @adapter: board private structure
188  **/
189 void i40evf_schedule_reset(struct i40evf_adapter *adapter)
190 {
191         if (!(adapter->flags &
192               (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
193                 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
194                 schedule_work(&adapter->reset_task);
195         }
196 }
197
198 /**
199  * i40evf_tx_timeout - Respond to a Tx Hang
200  * @netdev: network interface device structure
201  **/
202 static void i40evf_tx_timeout(struct net_device *netdev)
203 {
204         struct i40evf_adapter *adapter = netdev_priv(netdev);
205
206         adapter->tx_timeout_count++;
207         i40evf_schedule_reset(adapter);
208 }
209
210 /**
211  * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
212  * @adapter: board private structure
213  **/
214 static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
215 {
216         struct i40e_hw *hw = &adapter->hw;
217
218         if (!adapter->msix_entries)
219                 return;
220
221         wr32(hw, I40E_VFINT_DYN_CTL01, 0);
222
223         /* read flush */
224         rd32(hw, I40E_VFGEN_RSTAT);
225
226         synchronize_irq(adapter->msix_entries[0].vector);
227 }
228
229 /**
230  * i40evf_misc_irq_enable - Enable default interrupt generation settings
231  * @adapter: board private structure
232  **/
233 static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
234 {
235         struct i40e_hw *hw = &adapter->hw;
236
237         wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
238                                        I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
239         wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
240
241         /* read flush */
242         rd32(hw, I40E_VFGEN_RSTAT);
243 }
244
245 /**
246  * i40evf_irq_disable - Mask off interrupt generation on the NIC
247  * @adapter: board private structure
248  **/
249 static void i40evf_irq_disable(struct i40evf_adapter *adapter)
250 {
251         int i;
252         struct i40e_hw *hw = &adapter->hw;
253
254         if (!adapter->msix_entries)
255                 return;
256
257         for (i = 1; i < adapter->num_msix_vectors; i++) {
258                 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
259                 synchronize_irq(adapter->msix_entries[i].vector);
260         }
261         /* read flush */
262         rd32(hw, I40E_VFGEN_RSTAT);
263 }
264
265 /**
266  * i40evf_irq_enable_queues - Enable interrupt for specified queues
267  * @adapter: board private structure
268  * @mask: bitmap of queues to enable
269  **/
270 void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
271 {
272         struct i40e_hw *hw = &adapter->hw;
273         int i;
274
275         for (i = 1; i < adapter->num_msix_vectors; i++) {
276                 if (mask & BIT(i - 1)) {
277                         wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
278                              I40E_VFINT_DYN_CTLN1_INTENA_MASK |
279                              I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
280                              I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
281                 }
282         }
283 }
284
285 /**
286  * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
287  * @adapter: board private structure
288  * @mask: bitmap of vectors to trigger
289  **/
290 static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
291 {
292         struct i40e_hw *hw = &adapter->hw;
293         int i;
294         u32 dyn_ctl;
295
296         if (mask & 1) {
297                 dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
298                 dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
299                            I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
300                            I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
301                 wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
302         }
303         for (i = 1; i < adapter->num_msix_vectors; i++) {
304                 if (mask & BIT(i)) {
305                         dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
306                         dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
307                                    I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
308                                    I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
309                         wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
310                 }
311         }
312 }
313
314 /**
315  * i40evf_irq_enable - Enable default interrupt generation settings
316  * @adapter: board private structure
317  * @flush: boolean value whether to run rd32()
318  **/
319 void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
320 {
321         struct i40e_hw *hw = &adapter->hw;
322
323         i40evf_misc_irq_enable(adapter);
324         i40evf_irq_enable_queues(adapter, ~0);
325
326         if (flush)
327                 rd32(hw, I40E_VFGEN_RSTAT);
328 }
329
330 /**
331  * i40evf_msix_aq - Interrupt handler for vector 0
332  * @irq: interrupt number
333  * @data: pointer to netdev
334  **/
335 static irqreturn_t i40evf_msix_aq(int irq, void *data)
336 {
337         struct net_device *netdev = data;
338         struct i40evf_adapter *adapter = netdev_priv(netdev);
339         struct i40e_hw *hw = &adapter->hw;
340         u32 val;
341
342         /* handle non-queue interrupts, these reads clear the registers */
343         val = rd32(hw, I40E_VFINT_ICR01);
344         val = rd32(hw, I40E_VFINT_ICR0_ENA1);
345
346         val = rd32(hw, I40E_VFINT_DYN_CTL01) |
347               I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
348         wr32(hw, I40E_VFINT_DYN_CTL01, val);
349
350         /* schedule work on the private workqueue */
351         schedule_work(&adapter->adminq_task);
352
353         return IRQ_HANDLED;
354 }
355
356 /**
357  * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
358  * @irq: interrupt number
359  * @data: pointer to a q_vector
360  **/
361 static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
362 {
363         struct i40e_q_vector *q_vector = data;
364
365         if (!q_vector->tx.ring && !q_vector->rx.ring)
366                 return IRQ_HANDLED;
367
368         napi_schedule_irqoff(&q_vector->napi);
369
370         return IRQ_HANDLED;
371 }
372
373 /**
374  * i40evf_map_vector_to_rxq - associate irqs with rx queues
375  * @adapter: board private structure
376  * @v_idx: interrupt number
377  * @r_idx: queue number
378  **/
379 static void
380 i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
381 {
382         struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
383         struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
384         struct i40e_hw *hw = &adapter->hw;
385
386         rx_ring->q_vector = q_vector;
387         rx_ring->next = q_vector->rx.ring;
388         rx_ring->vsi = &adapter->vsi;
389         q_vector->rx.ring = rx_ring;
390         q_vector->rx.count++;
391         q_vector->rx.latency_range = I40E_LOW_LATENCY;
392         q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
393         q_vector->ring_mask |= BIT(r_idx);
394         q_vector->itr_countdown = ITR_COUNTDOWN_START;
395         wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
396 }
397
398 /**
399  * i40evf_map_vector_to_txq - associate irqs with tx queues
400  * @adapter: board private structure
401  * @v_idx: interrupt number
402  * @t_idx: queue number
403  **/
404 static void
405 i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
406 {
407         struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
408         struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
409         struct i40e_hw *hw = &adapter->hw;
410
411         tx_ring->q_vector = q_vector;
412         tx_ring->next = q_vector->tx.ring;
413         tx_ring->vsi = &adapter->vsi;
414         q_vector->tx.ring = tx_ring;
415         q_vector->tx.count++;
416         q_vector->tx.latency_range = I40E_LOW_LATENCY;
417         q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
418         q_vector->itr_countdown = ITR_COUNTDOWN_START;
419         q_vector->num_ringpairs++;
420         wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
421 }
422
423 /**
424  * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
425  * @adapter: board private structure to initialize
426  *
427  * This function maps descriptor rings to the queue-specific vectors
428  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
429  * one vector per ring/queue, but on a constrained vector budget, we
430  * group the rings as "efficiently" as possible.  You would add new
431  * mapping configurations in here.
432  **/
433 static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
434 {
435         int q_vectors;
436         int v_start = 0;
437         int rxr_idx = 0, txr_idx = 0;
438         int rxr_remaining = adapter->num_active_queues;
439         int txr_remaining = adapter->num_active_queues;
440         int i, j;
441         int rqpv, tqpv;
442         int err = 0;
443
444         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
445
446         /* The ideal configuration...
447          * We have enough vectors to map one per queue.
448          */
449         if (q_vectors >= (rxr_remaining * 2)) {
450                 for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
451                         i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
452
453                 for (; txr_idx < txr_remaining; v_start++, txr_idx++)
454                         i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
455                 goto out;
456         }
457
458         /* If we don't have enough vectors for a 1-to-1
459          * mapping, we'll have to group them so there are
460          * multiple queues per vector.
461          * Re-adjusting *qpv takes care of the remainder.
462          */
463         for (i = v_start; i < q_vectors; i++) {
464                 rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
465                 for (j = 0; j < rqpv; j++) {
466                         i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
467                         rxr_idx++;
468                         rxr_remaining--;
469                 }
470         }
471         for (i = v_start; i < q_vectors; i++) {
472                 tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
473                 for (j = 0; j < tqpv; j++) {
474                         i40evf_map_vector_to_txq(adapter, i, txr_idx);
475                         txr_idx++;
476                         txr_remaining--;
477                 }
478         }
479
480 out:
481         adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
482
483         return err;
484 }
485
486 #ifdef CONFIG_NET_POLL_CONTROLLER
487 /**
488  * i40evf_netpoll - A Polling 'interrupt' handler
489  * @netdev: network interface device structure
490  *
491  * This is used by netconsole to send skbs without having to re-enable
492  * interrupts.  It's not called while the normal interrupt routine is executing.
493  **/
494 static void i40evf_netpoll(struct net_device *netdev)
495 {
496         struct i40evf_adapter *adapter = netdev_priv(netdev);
497         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
498         int i;
499
500         /* if interface is down do nothing */
501         if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
502                 return;
503
504         for (i = 0; i < q_vectors; i++)
505                 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
506 }
507
508 #endif
509 /**
510  * i40evf_irq_affinity_notify - Callback for affinity changes
511  * @notify: context as to what irq was changed
512  * @mask: the new affinity mask
513  *
514  * This is a callback function used by the irq_set_affinity_notifier function
515  * so that we may register to receive changes to the irq affinity masks.
516  **/
517 static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
518                                        const cpumask_t *mask)
519 {
520         struct i40e_q_vector *q_vector =
521                 container_of(notify, struct i40e_q_vector, affinity_notify);
522
523         cpumask_copy(&q_vector->affinity_mask, mask);
524 }
525
526 /**
527  * i40evf_irq_affinity_release - Callback for affinity notifier release
528  * @ref: internal core kernel usage
529  *
530  * This is a callback function used by the irq_set_affinity_notifier function
531  * to inform the current notification subscriber that they will no longer
532  * receive notifications.
533  **/
534 static void i40evf_irq_affinity_release(struct kref *ref) {}
535
536 /**
537  * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
538  * @adapter: board private structure
539  *
540  * Allocates MSI-X vectors for tx and rx handling, and requests
541  * interrupts from the kernel.
542  **/
543 static int
544 i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
545 {
546         unsigned int vector, q_vectors;
547         unsigned int rx_int_idx = 0, tx_int_idx = 0;
548         int irq_num, err;
549         int cpu;
550
551         i40evf_irq_disable(adapter);
552         /* Decrement for Other and TCP Timer vectors */
553         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
554
555         for (vector = 0; vector < q_vectors; vector++) {
556                 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
557                 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
558
559                 if (q_vector->tx.ring && q_vector->rx.ring) {
560                         snprintf(q_vector->name, sizeof(q_vector->name),
561                                  "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
562                         tx_int_idx++;
563                 } else if (q_vector->rx.ring) {
564                         snprintf(q_vector->name, sizeof(q_vector->name),
565                                  "i40evf-%s-rx-%d", basename, rx_int_idx++);
566                 } else if (q_vector->tx.ring) {
567                         snprintf(q_vector->name, sizeof(q_vector->name),
568                                  "i40evf-%s-tx-%d", basename, tx_int_idx++);
569                 } else {
570                         /* skip this unused q_vector */
571                         continue;
572                 }
573                 err = request_irq(irq_num,
574                                   i40evf_msix_clean_rings,
575                                   0,
576                                   q_vector->name,
577                                   q_vector);
578                 if (err) {
579                         dev_info(&adapter->pdev->dev,
580                                  "Request_irq failed, error: %d\n", err);
581                         goto free_queue_irqs;
582                 }
583                 /* register for affinity change notifications */
584                 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
585                 q_vector->affinity_notify.release =
586                                                    i40evf_irq_affinity_release;
587                 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
588                 /* Spread the IRQ affinity hints across online CPUs. Note that
589                  * get_cpu_mask returns a mask with a permanent lifetime so
590                  * it's safe to use as a hint for irq_set_affinity_hint.
591                  */
592                 cpu = cpumask_local_spread(q_vector->v_idx, -1);
593                 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
594         }
595
596         return 0;
597
598 free_queue_irqs:
599         while (vector) {
600                 vector--;
601                 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
602                 irq_set_affinity_notifier(irq_num, NULL);
603                 irq_set_affinity_hint(irq_num, NULL);
604                 free_irq(irq_num, &adapter->q_vectors[vector]);
605         }
606         return err;
607 }
608
609 /**
610  * i40evf_request_misc_irq - Initialize MSI-X interrupts
611  * @adapter: board private structure
612  *
613  * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
614  * vector is only for the admin queue, and stays active even when the netdev
615  * is closed.
616  **/
617 static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
618 {
619         struct net_device *netdev = adapter->netdev;
620         int err;
621
622         snprintf(adapter->misc_vector_name,
623                  sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
624                  dev_name(&adapter->pdev->dev));
625         err = request_irq(adapter->msix_entries[0].vector,
626                           &i40evf_msix_aq, 0,
627                           adapter->misc_vector_name, netdev);
628         if (err) {
629                 dev_err(&adapter->pdev->dev,
630                         "request_irq for %s failed: %d\n",
631                         adapter->misc_vector_name, err);
632                 free_irq(adapter->msix_entries[0].vector, netdev);
633         }
634         return err;
635 }
636
637 /**
638  * i40evf_free_traffic_irqs - Free MSI-X interrupts
639  * @adapter: board private structure
640  *
641  * Frees all MSI-X vectors other than 0.
642  **/
643 static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
644 {
645         int vector, irq_num, q_vectors;
646
647         if (!adapter->msix_entries)
648                 return;
649
650         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
651
652         for (vector = 0; vector < q_vectors; vector++) {
653                 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
654                 irq_set_affinity_notifier(irq_num, NULL);
655                 irq_set_affinity_hint(irq_num, NULL);
656                 free_irq(irq_num, &adapter->q_vectors[vector]);
657         }
658 }
659
660 /**
661  * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
662  * @adapter: board private structure
663  *
664  * Frees MSI-X vector 0.
665  **/
666 static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
667 {
668         struct net_device *netdev = adapter->netdev;
669
670         if (!adapter->msix_entries)
671                 return;
672
673         free_irq(adapter->msix_entries[0].vector, netdev);
674 }
675
676 /**
677  * i40evf_configure_tx - Configure Transmit Unit after Reset
678  * @adapter: board private structure
679  *
680  * Configure the Tx unit of the MAC after a reset.
681  **/
682 static void i40evf_configure_tx(struct i40evf_adapter *adapter)
683 {
684         struct i40e_hw *hw = &adapter->hw;
685         int i;
686
687         for (i = 0; i < adapter->num_active_queues; i++)
688                 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
689 }
690
691 /**
692  * i40evf_configure_rx - Configure Receive Unit after Reset
693  * @adapter: board private structure
694  *
695  * Configure the Rx unit of the MAC after a reset.
696  **/
697 static void i40evf_configure_rx(struct i40evf_adapter *adapter)
698 {
699         unsigned int rx_buf_len = I40E_RXBUFFER_2048;
700         struct i40e_hw *hw = &adapter->hw;
701         int i;
702
703         /* Legacy Rx will always default to a 2048 buffer size. */
704 #if (PAGE_SIZE < 8192)
705         if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
706                 struct net_device *netdev = adapter->netdev;
707
708                 /* For jumbo frames on systems with 4K pages we have to use
709                  * an order 1 page, so we might as well increase the size
710                  * of our Rx buffer to make better use of the available space
711                  */
712                 rx_buf_len = I40E_RXBUFFER_3072;
713
714                 /* We use a 1536 buffer size for configurations with
715                  * standard Ethernet mtu.  On x86 this gives us enough room
716                  * for shared info and 192 bytes of padding.
717                  */
718                 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
719                     (netdev->mtu <= ETH_DATA_LEN))
720                         rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
721         }
722 #endif
723
724         for (i = 0; i < adapter->num_active_queues; i++) {
725                 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
726                 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
727
728                 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
729                         clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
730                 else
731                         set_ring_build_skb_enabled(&adapter->rx_rings[i]);
732         }
733 }
734
735 /**
736  * i40evf_find_vlan - Search filter list for specific vlan filter
737  * @adapter: board private structure
738  * @vlan: vlan tag
739  *
740  * Returns ptr to the filter object or NULL
741  **/
742 static struct
743 i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
744 {
745         struct i40evf_vlan_filter *f;
746
747         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
748                 if (vlan == f->vlan)
749                         return f;
750         }
751         return NULL;
752 }
753
754 /**
755  * i40evf_add_vlan - Add a vlan filter to the list
756  * @adapter: board private structure
757  * @vlan: VLAN tag
758  *
759  * Returns ptr to the filter object or NULL when no memory available.
760  **/
761 static struct
762 i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
763 {
764         struct i40evf_vlan_filter *f = NULL;
765         int count = 50;
766
767         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
768                                 &adapter->crit_section)) {
769                 udelay(1);
770                 if (--count == 0)
771                         goto out;
772         }
773
774         f = i40evf_find_vlan(adapter, vlan);
775         if (!f) {
776                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
777                 if (!f)
778                         goto clearout;
779
780                 f->vlan = vlan;
781
782                 INIT_LIST_HEAD(&f->list);
783                 list_add(&f->list, &adapter->vlan_filter_list);
784                 f->add = true;
785                 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
786         }
787
788 clearout:
789         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
790 out:
791         return f;
792 }
793
794 /**
795  * i40evf_del_vlan - Remove a vlan filter from the list
796  * @adapter: board private structure
797  * @vlan: VLAN tag
798  **/
799 static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
800 {
801         struct i40evf_vlan_filter *f;
802         int count = 50;
803
804         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
805                                 &adapter->crit_section)) {
806                 udelay(1);
807                 if (--count == 0)
808                         return;
809         }
810
811         f = i40evf_find_vlan(adapter, vlan);
812         if (f) {
813                 f->remove = true;
814                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
815         }
816         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
817 }
818
819 /**
820  * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
821  * @netdev: network device struct
822  * @vid: VLAN tag
823  **/
824 static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
825                                   __always_unused __be16 proto, u16 vid)
826 {
827         struct i40evf_adapter *adapter = netdev_priv(netdev);
828
829         if (!VLAN_ALLOWED(adapter))
830                 return -EIO;
831         if (i40evf_add_vlan(adapter, vid) == NULL)
832                 return -ENOMEM;
833         return 0;
834 }
835
836 /**
837  * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
838  * @netdev: network device struct
839  * @vid: VLAN tag
840  **/
841 static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
842                                    __always_unused __be16 proto, u16 vid)
843 {
844         struct i40evf_adapter *adapter = netdev_priv(netdev);
845
846         if (VLAN_ALLOWED(adapter)) {
847                 i40evf_del_vlan(adapter, vid);
848                 return 0;
849         }
850         return -EIO;
851 }
852
853 /**
854  * i40evf_find_filter - Search filter list for specific mac filter
855  * @adapter: board private structure
856  * @macaddr: the MAC address
857  *
858  * Returns ptr to the filter object or NULL
859  **/
860 static struct
861 i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
862                                       u8 *macaddr)
863 {
864         struct i40evf_mac_filter *f;
865
866         if (!macaddr)
867                 return NULL;
868
869         list_for_each_entry(f, &adapter->mac_filter_list, list) {
870                 if (ether_addr_equal(macaddr, f->macaddr))
871                         return f;
872         }
873         return NULL;
874 }
875
876 /**
877  * i40e_add_filter - Add a mac filter to the filter list
878  * @adapter: board private structure
879  * @macaddr: the MAC address
880  *
881  * Returns ptr to the filter object or NULL when no memory available.
882  **/
883 static struct
884 i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
885                                      u8 *macaddr)
886 {
887         struct i40evf_mac_filter *f;
888         int count = 50;
889
890         if (!macaddr)
891                 return NULL;
892
893         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
894                                 &adapter->crit_section)) {
895                 udelay(1);
896                 if (--count == 0)
897                         return NULL;
898         }
899
900         f = i40evf_find_filter(adapter, macaddr);
901         if (!f) {
902                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
903                 if (!f) {
904                         clear_bit(__I40EVF_IN_CRITICAL_TASK,
905                                   &adapter->crit_section);
906                         return NULL;
907                 }
908
909                 ether_addr_copy(f->macaddr, macaddr);
910
911                 list_add_tail(&f->list, &adapter->mac_filter_list);
912                 f->add = true;
913                 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
914         }
915
916         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
917         return f;
918 }
919
920 /**
921  * i40evf_set_mac - NDO callback to set port mac address
922  * @netdev: network interface device structure
923  * @p: pointer to an address structure
924  *
925  * Returns 0 on success, negative on failure
926  **/
927 static int i40evf_set_mac(struct net_device *netdev, void *p)
928 {
929         struct i40evf_adapter *adapter = netdev_priv(netdev);
930         struct i40e_hw *hw = &adapter->hw;
931         struct i40evf_mac_filter *f;
932         struct sockaddr *addr = p;
933
934         if (!is_valid_ether_addr(addr->sa_data))
935                 return -EADDRNOTAVAIL;
936
937         if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
938                 return 0;
939
940         if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
941                 return -EPERM;
942
943         f = i40evf_find_filter(adapter, hw->mac.addr);
944         if (f) {
945                 f->remove = true;
946                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
947         }
948
949         f = i40evf_add_filter(adapter, addr->sa_data);
950         if (f) {
951                 ether_addr_copy(hw->mac.addr, addr->sa_data);
952                 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
953         }
954
955         return (f == NULL) ? -ENOMEM : 0;
956 }
957
958 /**
959  * i40evf_set_rx_mode - NDO callback to set the netdev filters
960  * @netdev: network interface device structure
961  **/
962 static void i40evf_set_rx_mode(struct net_device *netdev)
963 {
964         struct i40evf_adapter *adapter = netdev_priv(netdev);
965         struct i40evf_mac_filter *f, *ftmp;
966         struct netdev_hw_addr *uca;
967         struct netdev_hw_addr *mca;
968         struct netdev_hw_addr *ha;
969         int count = 50;
970
971         /* add addr if not already in the filter list */
972         netdev_for_each_uc_addr(uca, netdev) {
973                 i40evf_add_filter(adapter, uca->addr);
974         }
975         netdev_for_each_mc_addr(mca, netdev) {
976                 i40evf_add_filter(adapter, mca->addr);
977         }
978
979         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
980                                 &adapter->crit_section)) {
981                 udelay(1);
982                 if (--count == 0) {
983                         dev_err(&adapter->pdev->dev,
984                                 "Failed to get lock in %s\n", __func__);
985                         return;
986                 }
987         }
988         /* remove filter if not in netdev list */
989         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
990                 netdev_for_each_mc_addr(mca, netdev)
991                         if (ether_addr_equal(mca->addr, f->macaddr))
992                                 goto bottom_of_search_loop;
993
994                 netdev_for_each_uc_addr(uca, netdev)
995                         if (ether_addr_equal(uca->addr, f->macaddr))
996                                 goto bottom_of_search_loop;
997
998                 for_each_dev_addr(netdev, ha)
999                         if (ether_addr_equal(ha->addr, f->macaddr))
1000                                 goto bottom_of_search_loop;
1001
1002                 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
1003                         goto bottom_of_search_loop;
1004
1005                 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1006                 f->remove = true;
1007                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1008
1009 bottom_of_search_loop:
1010                 continue;
1011         }
1012
1013         if (netdev->flags & IFF_PROMISC &&
1014             !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
1015                 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
1016         else if (!(netdev->flags & IFF_PROMISC) &&
1017                  adapter->flags & I40EVF_FLAG_PROMISC_ON)
1018                 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
1019
1020         if (netdev->flags & IFF_ALLMULTI &&
1021             !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
1022                 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
1023         else if (!(netdev->flags & IFF_ALLMULTI) &&
1024                  adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
1025                 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
1026
1027         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1028 }
1029
1030 /**
1031  * i40evf_napi_enable_all - enable NAPI on all queue vectors
1032  * @adapter: board private structure
1033  **/
1034 static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
1035 {
1036         int q_idx;
1037         struct i40e_q_vector *q_vector;
1038         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1039
1040         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1041                 struct napi_struct *napi;
1042
1043                 q_vector = &adapter->q_vectors[q_idx];
1044                 napi = &q_vector->napi;
1045                 napi_enable(napi);
1046         }
1047 }
1048
1049 /**
1050  * i40evf_napi_disable_all - disable NAPI on all queue vectors
1051  * @adapter: board private structure
1052  **/
1053 static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
1054 {
1055         int q_idx;
1056         struct i40e_q_vector *q_vector;
1057         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1058
1059         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1060                 q_vector = &adapter->q_vectors[q_idx];
1061                 napi_disable(&q_vector->napi);
1062         }
1063 }
1064
1065 /**
1066  * i40evf_configure - set up transmit and receive data structures
1067  * @adapter: board private structure
1068  **/
1069 static void i40evf_configure(struct i40evf_adapter *adapter)
1070 {
1071         struct net_device *netdev = adapter->netdev;
1072         int i;
1073
1074         i40evf_set_rx_mode(netdev);
1075
1076         i40evf_configure_tx(adapter);
1077         i40evf_configure_rx(adapter);
1078         adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
1079
1080         for (i = 0; i < adapter->num_active_queues; i++) {
1081                 struct i40e_ring *ring = &adapter->rx_rings[i];
1082
1083                 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
1084         }
1085 }
1086
1087 /**
1088  * i40evf_up_complete - Finish the last steps of bringing up a connection
1089  * @adapter: board private structure
1090  **/
1091 static void i40evf_up_complete(struct i40evf_adapter *adapter)
1092 {
1093         adapter->state = __I40EVF_RUNNING;
1094         clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1095
1096         i40evf_napi_enable_all(adapter);
1097
1098         adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1099         if (CLIENT_ENABLED(adapter))
1100                 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
1101         mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1102 }
1103
1104 /**
1105  * i40e_down - Shutdown the connection processing
1106  * @adapter: board private structure
1107  **/
1108 void i40evf_down(struct i40evf_adapter *adapter)
1109 {
1110         struct net_device *netdev = adapter->netdev;
1111         struct i40evf_mac_filter *f;
1112
1113         if (adapter->state <= __I40EVF_DOWN_PENDING)
1114                 return;
1115
1116         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
1117                                 &adapter->crit_section))
1118                 usleep_range(500, 1000);
1119
1120         netif_carrier_off(netdev);
1121         netif_tx_disable(netdev);
1122         adapter->link_up = false;
1123         i40evf_napi_disable_all(adapter);
1124         i40evf_irq_disable(adapter);
1125
1126         /* remove all MAC filters */
1127         list_for_each_entry(f, &adapter->mac_filter_list, list) {
1128                 f->remove = true;
1129         }
1130         /* remove all VLAN filters */
1131         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
1132                 f->remove = true;
1133         }
1134         if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1135             adapter->state != __I40EVF_RESETTING) {
1136                 /* cancel any current operation */
1137                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1138                 /* Schedule operations to close down the HW. Don't wait
1139                  * here for this to complete. The watchdog is still running
1140                  * and it will take care of this.
1141                  */
1142                 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1143                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1144                 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1145         }
1146
1147         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1148         mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1149 }
1150
1151 /**
1152  * i40evf_acquire_msix_vectors - Setup the MSIX capability
1153  * @adapter: board private structure
1154  * @vectors: number of vectors to request
1155  *
1156  * Work with the OS to set up the MSIX vectors needed.
1157  *
1158  * Returns 0 on success, negative on failure
1159  **/
1160 static int
1161 i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1162 {
1163         int err, vector_threshold;
1164
1165         /* We'll want at least 3 (vector_threshold):
1166          * 0) Other (Admin Queue and link, mostly)
1167          * 1) TxQ[0] Cleanup
1168          * 2) RxQ[0] Cleanup
1169          */
1170         vector_threshold = MIN_MSIX_COUNT;
1171
1172         /* The more we get, the more we will assign to Tx/Rx Cleanup
1173          * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1174          * Right now, we simply care about how many we'll get; we'll
1175          * set them up later while requesting irq's.
1176          */
1177         err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1178                                     vector_threshold, vectors);
1179         if (err < 0) {
1180                 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1181                 kfree(adapter->msix_entries);
1182                 adapter->msix_entries = NULL;
1183                 return err;
1184         }
1185
1186         /* Adjust for only the vectors we'll use, which is minimum
1187          * of max_msix_q_vectors + NONQ_VECS, or the number of
1188          * vectors we were allocated.
1189          */
1190         adapter->num_msix_vectors = err;
1191         return 0;
1192 }
1193
1194 /**
1195  * i40evf_free_queues - Free memory for all rings
1196  * @adapter: board private structure to initialize
1197  *
1198  * Free all of the memory associated with queue pairs.
1199  **/
1200 static void i40evf_free_queues(struct i40evf_adapter *adapter)
1201 {
1202         if (!adapter->vsi_res)
1203                 return;
1204         adapter->num_active_queues = 0;
1205         kfree(adapter->tx_rings);
1206         adapter->tx_rings = NULL;
1207         kfree(adapter->rx_rings);
1208         adapter->rx_rings = NULL;
1209 }
1210
1211 /**
1212  * i40evf_alloc_queues - Allocate memory for all rings
1213  * @adapter: board private structure to initialize
1214  *
1215  * We allocate one ring per queue at run-time since we don't know the
1216  * number of queues at compile-time.  The polling_netdev array is
1217  * intended for Multiqueue, but should work fine with a single queue.
1218  **/
1219 static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1220 {
1221         int i, num_active_queues;
1222
1223         num_active_queues = min_t(int,
1224                                   adapter->vsi_res->num_queue_pairs,
1225                                   (int)(num_online_cpus()));
1226
1227         adapter->tx_rings = kcalloc(num_active_queues,
1228                                     sizeof(struct i40e_ring), GFP_KERNEL);
1229         if (!adapter->tx_rings)
1230                 goto err_out;
1231         adapter->rx_rings = kcalloc(num_active_queues,
1232                                     sizeof(struct i40e_ring), GFP_KERNEL);
1233         if (!adapter->rx_rings)
1234                 goto err_out;
1235
1236         for (i = 0; i < num_active_queues; i++) {
1237                 struct i40e_ring *tx_ring;
1238                 struct i40e_ring *rx_ring;
1239
1240                 tx_ring = &adapter->tx_rings[i];
1241
1242                 tx_ring->queue_index = i;
1243                 tx_ring->netdev = adapter->netdev;
1244                 tx_ring->dev = &adapter->pdev->dev;
1245                 tx_ring->count = adapter->tx_desc_count;
1246                 tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
1247                 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
1248                         tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
1249
1250                 rx_ring = &adapter->rx_rings[i];
1251                 rx_ring->queue_index = i;
1252                 rx_ring->netdev = adapter->netdev;
1253                 rx_ring->dev = &adapter->pdev->dev;
1254                 rx_ring->count = adapter->rx_desc_count;
1255                 rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
1256         }
1257
1258         adapter->num_active_queues = num_active_queues;
1259
1260         return 0;
1261
1262 err_out:
1263         i40evf_free_queues(adapter);
1264         return -ENOMEM;
1265 }
1266
1267 /**
1268  * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1269  * @adapter: board private structure to initialize
1270  *
1271  * Attempt to configure the interrupts using the best available
1272  * capabilities of the hardware and the kernel.
1273  **/
1274 static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1275 {
1276         int vector, v_budget;
1277         int pairs = 0;
1278         int err = 0;
1279
1280         if (!adapter->vsi_res) {
1281                 err = -EIO;
1282                 goto out;
1283         }
1284         pairs = adapter->num_active_queues;
1285
1286         /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1287          * us much good if we have more vectors than CPUs. However, we already
1288          * limit the total number of queues by the number of CPUs so we do not
1289          * need any further limiting here.
1290          */
1291         v_budget = min_t(int, pairs + NONQ_VECS,
1292                          (int)adapter->vf_res->max_vectors);
1293
1294         adapter->msix_entries = kcalloc(v_budget,
1295                                         sizeof(struct msix_entry), GFP_KERNEL);
1296         if (!adapter->msix_entries) {
1297                 err = -ENOMEM;
1298                 goto out;
1299         }
1300
1301         for (vector = 0; vector < v_budget; vector++)
1302                 adapter->msix_entries[vector].entry = vector;
1303
1304         err = i40evf_acquire_msix_vectors(adapter, v_budget);
1305
1306 out:
1307         netif_set_real_num_rx_queues(adapter->netdev, pairs);
1308         netif_set_real_num_tx_queues(adapter->netdev, pairs);
1309         return err;
1310 }
1311
1312 /**
1313  * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1314  * @adapter: board private structure
1315  *
1316  * Return 0 on success, negative on failure
1317  **/
1318 static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
1319 {
1320         struct i40e_aqc_get_set_rss_key_data *rss_key =
1321                 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
1322         struct i40e_hw *hw = &adapter->hw;
1323         int ret = 0;
1324
1325         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1326                 /* bail because we already have a command pending */
1327                 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1328                         adapter->current_op);
1329                 return -EBUSY;
1330         }
1331
1332         ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1333         if (ret) {
1334                 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1335                         i40evf_stat_str(hw, ret),
1336                         i40evf_aq_str(hw, hw->aq.asq_last_status));
1337                 return ret;
1338
1339         }
1340
1341         ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1342                                     adapter->rss_lut, adapter->rss_lut_size);
1343         if (ret) {
1344                 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1345                         i40evf_stat_str(hw, ret),
1346                         i40evf_aq_str(hw, hw->aq.asq_last_status));
1347         }
1348
1349         return ret;
1350
1351 }
1352
1353 /**
1354  * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
1355  * @adapter: board private structure
1356  *
1357  * Returns 0 on success, negative on failure
1358  **/
1359 static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
1360 {
1361         struct i40e_hw *hw = &adapter->hw;
1362         u32 *dw;
1363         u16 i;
1364
1365         dw = (u32 *)adapter->rss_key;
1366         for (i = 0; i <= adapter->rss_key_size / 4; i++)
1367                 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
1368
1369         dw = (u32 *)adapter->rss_lut;
1370         for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1371                 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
1372
1373         i40e_flush(hw);
1374
1375         return 0;
1376 }
1377
1378 /**
1379  * i40evf_config_rss - Configure RSS keys and lut
1380  * @adapter: board private structure
1381  *
1382  * Returns 0 on success, negative on failure
1383  **/
1384 int i40evf_config_rss(struct i40evf_adapter *adapter)
1385 {
1386
1387         if (RSS_PF(adapter)) {
1388                 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1389                                         I40EVF_FLAG_AQ_SET_RSS_KEY;
1390                 return 0;
1391         } else if (RSS_AQ(adapter)) {
1392                 return i40evf_config_rss_aq(adapter);
1393         } else {
1394                 return i40evf_config_rss_reg(adapter);
1395         }
1396 }
1397
1398 /**
1399  * i40evf_fill_rss_lut - Fill the lut with default values
1400  * @adapter: board private structure
1401  **/
1402 static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
1403 {
1404         u16 i;
1405
1406         for (i = 0; i < adapter->rss_lut_size; i++)
1407                 adapter->rss_lut[i] = i % adapter->num_active_queues;
1408 }
1409
1410 /**
1411  * i40evf_init_rss - Prepare for RSS
1412  * @adapter: board private structure
1413  *
1414  * Return 0 on success, negative on failure
1415  **/
1416 static int i40evf_init_rss(struct i40evf_adapter *adapter)
1417 {
1418         struct i40e_hw *hw = &adapter->hw;
1419         int ret;
1420
1421         if (!RSS_PF(adapter)) {
1422                 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1423                 if (adapter->vf_res->vf_cap_flags &
1424                     VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1425                         adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1426                 else
1427                         adapter->hena = I40E_DEFAULT_RSS_HENA;
1428
1429                 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1430                 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1431         }
1432
1433         i40evf_fill_rss_lut(adapter);
1434
1435         netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1436         ret = i40evf_config_rss(adapter);
1437
1438         return ret;
1439 }
1440
1441 /**
1442  * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1443  * @adapter: board private structure to initialize
1444  *
1445  * We allocate one q_vector per queue interrupt.  If allocation fails we
1446  * return -ENOMEM.
1447  **/
1448 static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1449 {
1450         int q_idx = 0, num_q_vectors;
1451         struct i40e_q_vector *q_vector;
1452
1453         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1454         adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1455                                      GFP_KERNEL);
1456         if (!adapter->q_vectors)
1457                 return -ENOMEM;
1458
1459         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1460                 q_vector = &adapter->q_vectors[q_idx];
1461                 q_vector->adapter = adapter;
1462                 q_vector->vsi = &adapter->vsi;
1463                 q_vector->v_idx = q_idx;
1464                 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1465                 netif_napi_add(adapter->netdev, &q_vector->napi,
1466                                i40evf_napi_poll, NAPI_POLL_WEIGHT);
1467         }
1468
1469         return 0;
1470 }
1471
1472 /**
1473  * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1474  * @adapter: board private structure to initialize
1475  *
1476  * This function frees the memory allocated to the q_vectors.  In addition if
1477  * NAPI is enabled it will delete any references to the NAPI struct prior
1478  * to freeing the q_vector.
1479  **/
1480 static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1481 {
1482         int q_idx, num_q_vectors;
1483         int napi_vectors;
1484
1485         if (!adapter->q_vectors)
1486                 return;
1487
1488         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1489         napi_vectors = adapter->num_active_queues;
1490
1491         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1492                 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
1493                 if (q_idx < napi_vectors)
1494                         netif_napi_del(&q_vector->napi);
1495         }
1496         kfree(adapter->q_vectors);
1497         adapter->q_vectors = NULL;
1498 }
1499
1500 /**
1501  * i40evf_reset_interrupt_capability - Reset MSIX setup
1502  * @adapter: board private structure
1503  *
1504  **/
1505 void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1506 {
1507         if (!adapter->msix_entries)
1508                 return;
1509
1510         pci_disable_msix(adapter->pdev);
1511         kfree(adapter->msix_entries);
1512         adapter->msix_entries = NULL;
1513 }
1514
1515 /**
1516  * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1517  * @adapter: board private structure to initialize
1518  *
1519  **/
1520 int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1521 {
1522         int err;
1523
1524         err = i40evf_alloc_queues(adapter);
1525         if (err) {
1526                 dev_err(&adapter->pdev->dev,
1527                         "Unable to allocate memory for queues\n");
1528                 goto err_alloc_queues;
1529         }
1530
1531         rtnl_lock();
1532         err = i40evf_set_interrupt_capability(adapter);
1533         rtnl_unlock();
1534         if (err) {
1535                 dev_err(&adapter->pdev->dev,
1536                         "Unable to setup interrupt capabilities\n");
1537                 goto err_set_interrupt;
1538         }
1539
1540         err = i40evf_alloc_q_vectors(adapter);
1541         if (err) {
1542                 dev_err(&adapter->pdev->dev,
1543                         "Unable to allocate memory for queue vectors\n");
1544                 goto err_alloc_q_vectors;
1545         }
1546
1547         dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1548                  (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1549                  adapter->num_active_queues);
1550
1551         return 0;
1552 err_alloc_q_vectors:
1553         i40evf_reset_interrupt_capability(adapter);
1554 err_set_interrupt:
1555         i40evf_free_queues(adapter);
1556 err_alloc_queues:
1557         return err;
1558 }
1559
1560 /**
1561  * i40evf_free_rss - Free memory used by RSS structs
1562  * @adapter: board private structure
1563  **/
1564 static void i40evf_free_rss(struct i40evf_adapter *adapter)
1565 {
1566         kfree(adapter->rss_key);
1567         adapter->rss_key = NULL;
1568
1569         kfree(adapter->rss_lut);
1570         adapter->rss_lut = NULL;
1571 }
1572
1573 /**
1574  * i40evf_watchdog_timer - Periodic call-back timer
1575  * @data: pointer to adapter disguised as unsigned long
1576  **/
1577 static void i40evf_watchdog_timer(unsigned long data)
1578 {
1579         struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
1580
1581         schedule_work(&adapter->watchdog_task);
1582         /* timer will be rescheduled in watchdog task */
1583 }
1584
1585 /**
1586  * i40evf_watchdog_task - Periodic call-back task
1587  * @work: pointer to work_struct
1588  **/
1589 static void i40evf_watchdog_task(struct work_struct *work)
1590 {
1591         struct i40evf_adapter *adapter = container_of(work,
1592                                                       struct i40evf_adapter,
1593                                                       watchdog_task);
1594         struct i40e_hw *hw = &adapter->hw;
1595         u32 reg_val;
1596
1597         if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1598                 goto restart_watchdog;
1599
1600         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1601                 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1602                           I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1603                 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1604                     (reg_val == VIRTCHNL_VFR_COMPLETED)) {
1605                         /* A chance for redemption! */
1606                         dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1607                         adapter->state = __I40EVF_STARTUP;
1608                         adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1609                         schedule_delayed_work(&adapter->init_task, 10);
1610                         clear_bit(__I40EVF_IN_CRITICAL_TASK,
1611                                   &adapter->crit_section);
1612                         /* Don't reschedule the watchdog, since we've restarted
1613                          * the init task. When init_task contacts the PF and
1614                          * gets everything set up again, it'll restart the
1615                          * watchdog for us. Down, boy. Sit. Stay. Woof.
1616                          */
1617                         return;
1618                 }
1619                 adapter->aq_required = 0;
1620                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1621                 goto watchdog_done;
1622         }
1623
1624         if ((adapter->state < __I40EVF_DOWN) ||
1625             (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1626                 goto watchdog_done;
1627
1628         /* check for reset */
1629         reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1630         if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
1631                 adapter->state = __I40EVF_RESETTING;
1632                 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1633                 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1634                 schedule_work(&adapter->reset_task);
1635                 adapter->aq_required = 0;
1636                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1637                 goto watchdog_done;
1638         }
1639
1640         /* Process admin queue tasks. After init, everything gets done
1641          * here so we don't race on the admin queue.
1642          */
1643         if (adapter->current_op) {
1644                 if (!i40evf_asq_done(hw)) {
1645                         dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1646                         i40evf_send_api_ver(adapter);
1647                 }
1648                 goto watchdog_done;
1649         }
1650         if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1651                 i40evf_send_vf_config_msg(adapter);
1652                 goto watchdog_done;
1653         }
1654
1655         if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1656                 i40evf_disable_queues(adapter);
1657                 goto watchdog_done;
1658         }
1659
1660         if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1661                 i40evf_map_queues(adapter);
1662                 goto watchdog_done;
1663         }
1664
1665         if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1666                 i40evf_add_ether_addrs(adapter);
1667                 goto watchdog_done;
1668         }
1669
1670         if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1671                 i40evf_add_vlans(adapter);
1672                 goto watchdog_done;
1673         }
1674
1675         if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1676                 i40evf_del_ether_addrs(adapter);
1677                 goto watchdog_done;
1678         }
1679
1680         if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1681                 i40evf_del_vlans(adapter);
1682                 goto watchdog_done;
1683         }
1684
1685         if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1686                 i40evf_enable_vlan_stripping(adapter);
1687                 goto watchdog_done;
1688         }
1689
1690         if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1691                 i40evf_disable_vlan_stripping(adapter);
1692                 goto watchdog_done;
1693         }
1694
1695         if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1696                 i40evf_configure_queues(adapter);
1697                 goto watchdog_done;
1698         }
1699
1700         if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1701                 i40evf_enable_queues(adapter);
1702                 goto watchdog_done;
1703         }
1704
1705         if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1706                 /* This message goes straight to the firmware, not the
1707                  * PF, so we don't have to set current_op as we will
1708                  * not get a response through the ARQ.
1709                  */
1710                 i40evf_init_rss(adapter);
1711                 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1712                 goto watchdog_done;
1713         }
1714         if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1715                 i40evf_get_hena(adapter);
1716                 goto watchdog_done;
1717         }
1718         if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1719                 i40evf_set_hena(adapter);
1720                 goto watchdog_done;
1721         }
1722         if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1723                 i40evf_set_rss_key(adapter);
1724                 goto watchdog_done;
1725         }
1726         if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1727                 i40evf_set_rss_lut(adapter);
1728                 goto watchdog_done;
1729         }
1730
1731         if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1732                 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1733                                        FLAG_VF_MULTICAST_PROMISC);
1734                 goto watchdog_done;
1735         }
1736
1737         if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
1738                 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1739                 goto watchdog_done;
1740         }
1741
1742         if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1743             (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1744                 i40evf_set_promiscuous(adapter, 0);
1745                 goto watchdog_done;
1746         }
1747         schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1748
1749         if (adapter->state == __I40EVF_RUNNING)
1750                 i40evf_request_stats(adapter);
1751 watchdog_done:
1752         if (adapter->state == __I40EVF_RUNNING) {
1753                 i40evf_irq_enable_queues(adapter, ~0);
1754                 i40evf_fire_sw_int(adapter, 0xFF);
1755         } else {
1756                 i40evf_fire_sw_int(adapter, 0x1);
1757         }
1758
1759         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1760 restart_watchdog:
1761         if (adapter->state == __I40EVF_REMOVE)
1762                 return;
1763         if (adapter->aq_required)
1764                 mod_timer(&adapter->watchdog_timer,
1765                           jiffies + msecs_to_jiffies(20));
1766         else
1767                 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1768         schedule_work(&adapter->adminq_task);
1769 }
1770
1771 static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1772 {
1773         struct i40evf_mac_filter *f, *ftmp;
1774         struct i40evf_vlan_filter *fv, *fvtmp;
1775
1776         adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1777
1778         /* We don't use netif_running() because it may be true prior to
1779          * ndo_open() returning, so we can't assume it means all our open
1780          * tasks have finished, since we're not holding the rtnl_lock here.
1781          */
1782         if (adapter->state == __I40EVF_RUNNING) {
1783                 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1784                 netif_carrier_off(adapter->netdev);
1785                 netif_tx_disable(adapter->netdev);
1786                 adapter->link_up = false;
1787                 i40evf_napi_disable_all(adapter);
1788                 i40evf_irq_disable(adapter);
1789                 i40evf_free_traffic_irqs(adapter);
1790                 i40evf_free_all_tx_resources(adapter);
1791                 i40evf_free_all_rx_resources(adapter);
1792         }
1793
1794         /* Delete all of the filters, both MAC and VLAN. */
1795         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1796                 list_del(&f->list);
1797                 kfree(f);
1798         }
1799
1800         list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1801                 list_del(&fv->list);
1802                 kfree(fv);
1803         }
1804
1805         i40evf_free_misc_irq(adapter);
1806         i40evf_reset_interrupt_capability(adapter);
1807         i40evf_free_queues(adapter);
1808         i40evf_free_q_vectors(adapter);
1809         kfree(adapter->vf_res);
1810         i40evf_shutdown_adminq(&adapter->hw);
1811         adapter->netdev->flags &= ~IFF_UP;
1812         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1813         adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1814         adapter->state = __I40EVF_DOWN;
1815         wake_up(&adapter->down_waitqueue);
1816         dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1817 }
1818
1819 #define I40EVF_RESET_WAIT_MS 10
1820 #define I40EVF_RESET_WAIT_COUNT 500
1821 /**
1822  * i40evf_reset_task - Call-back task to handle hardware reset
1823  * @work: pointer to work_struct
1824  *
1825  * During reset we need to shut down and reinitialize the admin queue
1826  * before we can use it to communicate with the PF again. We also clear
1827  * and reinit the rings because that context is lost as well.
1828  **/
1829 static void i40evf_reset_task(struct work_struct *work)
1830 {
1831         struct i40evf_adapter *adapter = container_of(work,
1832                                                       struct i40evf_adapter,
1833                                                       reset_task);
1834         struct net_device *netdev = adapter->netdev;
1835         struct i40e_hw *hw = &adapter->hw;
1836         struct i40evf_vlan_filter *vlf;
1837         struct i40evf_mac_filter *f;
1838         u32 reg_val;
1839         int i = 0, err;
1840         bool running;
1841
1842         /* When device is being removed it doesn't make sense to run the reset
1843          * task, just return in such a case.
1844          */
1845         if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
1846                 return;
1847
1848         while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
1849                                 &adapter->crit_section))
1850                 usleep_range(500, 1000);
1851         if (CLIENT_ENABLED(adapter)) {
1852                 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1853                                     I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1854                                     I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1855                                     I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1856                 cancel_delayed_work_sync(&adapter->client_task);
1857                 i40evf_notify_client_close(&adapter->vsi, true);
1858         }
1859         i40evf_misc_irq_disable(adapter);
1860         if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1861                 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1862                 /* Restart the AQ here. If we have been reset but didn't
1863                  * detect it, or if the PF had to reinit, our AQ will be hosed.
1864                  */
1865                 i40evf_shutdown_adminq(hw);
1866                 i40evf_init_adminq(hw);
1867                 i40evf_request_reset(adapter);
1868         }
1869         adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1870
1871         /* poll until we see the reset actually happen */
1872         for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1873                 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1874                           I40E_VF_ARQLEN1_ARQENABLE_MASK;
1875                 if (!reg_val)
1876                         break;
1877                 usleep_range(5000, 10000);
1878         }
1879         if (i == I40EVF_RESET_WAIT_COUNT) {
1880                 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1881                 goto continue_reset; /* act like the reset happened */
1882         }
1883
1884         /* wait until the reset is complete and the PF is responding to us */
1885         for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1886                 /* sleep first to make sure a minimum wait time is met */
1887                 msleep(I40EVF_RESET_WAIT_MS);
1888
1889                 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1890                           I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1891                 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
1892                         break;
1893         }
1894
1895         pci_set_master(adapter->pdev);
1896
1897         if (i == I40EVF_RESET_WAIT_COUNT) {
1898                 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1899                         reg_val);
1900                 i40evf_disable_vf(adapter);
1901                 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1902                 return; /* Do not attempt to reinit. It's dead, Jim. */
1903         }
1904
1905 continue_reset:
1906         /* We don't use netif_running() because it may be true prior to
1907          * ndo_open() returning, so we can't assume it means all our open
1908          * tasks have finished, since we're not holding the rtnl_lock here.
1909          */
1910         running = (adapter->state == __I40EVF_RUNNING);
1911
1912         if (running) {
1913                 netif_carrier_off(netdev);
1914                 netif_tx_stop_all_queues(netdev);
1915                 adapter->link_up = false;
1916                 i40evf_napi_disable_all(adapter);
1917         }
1918         i40evf_irq_disable(adapter);
1919
1920         adapter->state = __I40EVF_RESETTING;
1921         adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1922
1923         /* free the Tx/Rx rings and descriptors, might be better to just
1924          * re-use them sometime in the future
1925          */
1926         i40evf_free_all_rx_resources(adapter);
1927         i40evf_free_all_tx_resources(adapter);
1928
1929         /* kill and reinit the admin queue */
1930         i40evf_shutdown_adminq(hw);
1931         adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1932         err = i40evf_init_adminq(hw);
1933         if (err)
1934                 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1935                          err);
1936
1937         adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
1938         adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
1939
1940         /* re-add all MAC filters */
1941         list_for_each_entry(f, &adapter->mac_filter_list, list) {
1942                 f->add = true;
1943         }
1944         /* re-add all VLAN filters */
1945         list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1946                 vlf->add = true;
1947         }
1948         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1949         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
1950         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1951         clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1952         i40evf_misc_irq_enable(adapter);
1953
1954         mod_timer(&adapter->watchdog_timer, jiffies + 2);
1955
1956         /* We were running when the reset started, so we need to restore some
1957          * state here.
1958          */
1959         if (running) {
1960                 /* allocate transmit descriptors */
1961                 err = i40evf_setup_all_tx_resources(adapter);
1962                 if (err)
1963                         goto reset_err;
1964
1965                 /* allocate receive descriptors */
1966                 err = i40evf_setup_all_rx_resources(adapter);
1967                 if (err)
1968                         goto reset_err;
1969
1970                 i40evf_configure(adapter);
1971
1972                 i40evf_up_complete(adapter);
1973
1974                 i40evf_irq_enable(adapter, true);
1975         } else {
1976                 adapter->state = __I40EVF_DOWN;
1977                 wake_up(&adapter->down_waitqueue);
1978         }
1979
1980         return;
1981 reset_err:
1982         dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
1983         i40evf_close(netdev);
1984 }
1985
1986 /**
1987  * i40evf_adminq_task - worker thread to clean the admin queue
1988  * @work: pointer to work_struct containing our data
1989  **/
1990 static void i40evf_adminq_task(struct work_struct *work)
1991 {
1992         struct i40evf_adapter *adapter =
1993                 container_of(work, struct i40evf_adapter, adminq_task);
1994         struct i40e_hw *hw = &adapter->hw;
1995         struct i40e_arq_event_info event;
1996         enum virtchnl_ops v_op;
1997         i40e_status ret, v_ret;
1998         u32 val, oldval;
1999         u16 pending;
2000
2001         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
2002                 goto out;
2003
2004         event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
2005         event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2006         if (!event.msg_buf)
2007                 goto out;
2008
2009         do {
2010                 ret = i40evf_clean_arq_element(hw, &event, &pending);
2011                 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2012                 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2013
2014                 if (ret || !v_op)
2015                         break; /* No event to process or error cleaning ARQ */
2016
2017                 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2018                                            event.msg_len);
2019                 if (pending != 0)
2020                         memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
2021         } while (pending);
2022
2023         if ((adapter->flags &
2024              (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2025             adapter->state == __I40EVF_RESETTING)
2026                 goto freedom;
2027
2028         /* check for error indications */
2029         val = rd32(hw, hw->aq.arq.len);
2030         if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
2031                 goto freedom;
2032         oldval = val;
2033         if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
2034                 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2035                 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
2036         }
2037         if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
2038                 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2039                 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
2040         }
2041         if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
2042                 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2043                 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
2044         }
2045         if (oldval != val)
2046                 wr32(hw, hw->aq.arq.len, val);
2047
2048         val = rd32(hw, hw->aq.asq.len);
2049         oldval = val;
2050         if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
2051                 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2052                 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
2053         }
2054         if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
2055                 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2056                 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
2057         }
2058         if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
2059                 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2060                 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
2061         }
2062         if (oldval != val)
2063                 wr32(hw, hw->aq.asq.len, val);
2064
2065 freedom:
2066         kfree(event.msg_buf);
2067 out:
2068         /* re-enable Admin queue interrupt cause */
2069         i40evf_misc_irq_enable(adapter);
2070 }
2071
2072 /**
2073  * i40evf_client_task - worker thread to perform client work
2074  * @work: pointer to work_struct containing our data
2075  *
2076  * This task handles client interactions. Because client calls can be
2077  * reentrant, we can't handle them in the watchdog.
2078  **/
2079 static void i40evf_client_task(struct work_struct *work)
2080 {
2081         struct i40evf_adapter *adapter =
2082                 container_of(work, struct i40evf_adapter, client_task.work);
2083
2084         /* If we can't get the client bit, just give up. We'll be rescheduled
2085          * later.
2086          */
2087
2088         if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2089                 return;
2090
2091         if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2092                 i40evf_client_subtask(adapter);
2093                 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2094                 goto out;
2095         }
2096         if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2097                 i40evf_notify_client_close(&adapter->vsi, false);
2098                 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2099                 goto out;
2100         }
2101         if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2102                 i40evf_notify_client_open(&adapter->vsi);
2103                 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2104                 goto out;
2105         }
2106         if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2107                 i40evf_notify_client_l2_params(&adapter->vsi);
2108                 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2109         }
2110 out:
2111         clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2112 }
2113
2114 /**
2115  * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2116  * @adapter: board private structure
2117  *
2118  * Free all transmit software resources
2119  **/
2120 void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
2121 {
2122         int i;
2123
2124         if (!adapter->tx_rings)
2125                 return;
2126
2127         for (i = 0; i < adapter->num_active_queues; i++)
2128                 if (adapter->tx_rings[i].desc)
2129                         i40evf_free_tx_resources(&adapter->tx_rings[i]);
2130 }
2131
2132 /**
2133  * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2134  * @adapter: board private structure
2135  *
2136  * If this function returns with an error, then it's possible one or
2137  * more of the rings is populated (while the rest are not).  It is the
2138  * callers duty to clean those orphaned rings.
2139  *
2140  * Return 0 on success, negative on failure
2141  **/
2142 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2143 {
2144         int i, err = 0;
2145
2146         for (i = 0; i < adapter->num_active_queues; i++) {
2147                 adapter->tx_rings[i].count = adapter->tx_desc_count;
2148                 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
2149                 if (!err)
2150                         continue;
2151                 dev_err(&adapter->pdev->dev,
2152                         "Allocation for Tx Queue %u failed\n", i);
2153                 break;
2154         }
2155
2156         return err;
2157 }
2158
2159 /**
2160  * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2161  * @adapter: board private structure
2162  *
2163  * If this function returns with an error, then it's possible one or
2164  * more of the rings is populated (while the rest are not).  It is the
2165  * callers duty to clean those orphaned rings.
2166  *
2167  * Return 0 on success, negative on failure
2168  **/
2169 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2170 {
2171         int i, err = 0;
2172
2173         for (i = 0; i < adapter->num_active_queues; i++) {
2174                 adapter->rx_rings[i].count = adapter->rx_desc_count;
2175                 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
2176                 if (!err)
2177                         continue;
2178                 dev_err(&adapter->pdev->dev,
2179                         "Allocation for Rx Queue %u failed\n", i);
2180                 break;
2181         }
2182         return err;
2183 }
2184
2185 /**
2186  * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2187  * @adapter: board private structure
2188  *
2189  * Free all receive software resources
2190  **/
2191 void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
2192 {
2193         int i;
2194
2195         if (!adapter->rx_rings)
2196                 return;
2197
2198         for (i = 0; i < adapter->num_active_queues; i++)
2199                 if (adapter->rx_rings[i].desc)
2200                         i40evf_free_rx_resources(&adapter->rx_rings[i]);
2201 }
2202
2203 /**
2204  * i40evf_open - Called when a network interface is made active
2205  * @netdev: network interface device structure
2206  *
2207  * Returns 0 on success, negative value on failure
2208  *
2209  * The open entry point is called when a network interface is made
2210  * active by the system (IFF_UP).  At this point all resources needed
2211  * for transmit and receive operations are allocated, the interrupt
2212  * handler is registered with the OS, the watchdog timer is started,
2213  * and the stack is notified that the interface is ready.
2214  **/
2215 static int i40evf_open(struct net_device *netdev)
2216 {
2217         struct i40evf_adapter *adapter = netdev_priv(netdev);
2218         int err;
2219
2220         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2221                 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2222                 return -EIO;
2223         }
2224
2225         if (adapter->state != __I40EVF_DOWN)
2226                 return -EBUSY;
2227
2228         /* allocate transmit descriptors */
2229         err = i40evf_setup_all_tx_resources(adapter);
2230         if (err)
2231                 goto err_setup_tx;
2232
2233         /* allocate receive descriptors */
2234         err = i40evf_setup_all_rx_resources(adapter);
2235         if (err)
2236                 goto err_setup_rx;
2237
2238         /* clear any pending interrupts, may auto mask */
2239         err = i40evf_request_traffic_irqs(adapter, netdev->name);
2240         if (err)
2241                 goto err_req_irq;
2242
2243         i40evf_add_filter(adapter, adapter->hw.mac.addr);
2244         i40evf_configure(adapter);
2245
2246         i40evf_up_complete(adapter);
2247
2248         i40evf_irq_enable(adapter, true);
2249
2250         return 0;
2251
2252 err_req_irq:
2253         i40evf_down(adapter);
2254         i40evf_free_traffic_irqs(adapter);
2255 err_setup_rx:
2256         i40evf_free_all_rx_resources(adapter);
2257 err_setup_tx:
2258         i40evf_free_all_tx_resources(adapter);
2259
2260         return err;
2261 }
2262
2263 /**
2264  * i40evf_close - Disables a network interface
2265  * @netdev: network interface device structure
2266  *
2267  * Returns 0, this is not allowed to fail
2268  *
2269  * The close entry point is called when an interface is de-activated
2270  * by the OS.  The hardware is still under the drivers control, but
2271  * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2272  * are freed, along with all transmit and receive resources.
2273  **/
2274 static int i40evf_close(struct net_device *netdev)
2275 {
2276         struct i40evf_adapter *adapter = netdev_priv(netdev);
2277         int status;
2278
2279         if (adapter->state <= __I40EVF_DOWN_PENDING)
2280                 return 0;
2281
2282
2283         set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
2284         if (CLIENT_ENABLED(adapter))
2285                 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2286
2287         i40evf_down(adapter);
2288         adapter->state = __I40EVF_DOWN_PENDING;
2289         i40evf_free_traffic_irqs(adapter);
2290
2291         /* We explicitly don't free resources here because the hardware is
2292          * still active and can DMA into memory. Resources are cleared in
2293          * i40evf_virtchnl_completion() after we get confirmation from the PF
2294          * driver that the rings have been stopped.
2295          *
2296          * Also, we wait for state to transition to __I40EVF_DOWN before
2297          * returning. State change occurs in i40evf_virtchnl_completion() after
2298          * VF resources are released (which occurs after PF driver processes and
2299          * responds to admin queue commands).
2300          */
2301
2302         status = wait_event_timeout(adapter->down_waitqueue,
2303                                     adapter->state == __I40EVF_DOWN,
2304                                     msecs_to_jiffies(200));
2305         if (!status)
2306                 netdev_warn(netdev, "Device resources not yet released\n");
2307         return 0;
2308 }
2309
2310 /**
2311  * i40evf_change_mtu - Change the Maximum Transfer Unit
2312  * @netdev: network interface device structure
2313  * @new_mtu: new value for maximum frame size
2314  *
2315  * Returns 0 on success, negative on failure
2316  **/
2317 static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
2318 {
2319         struct i40evf_adapter *adapter = netdev_priv(netdev);
2320
2321         netdev->mtu = new_mtu;
2322         if (CLIENT_ENABLED(adapter)) {
2323                 i40evf_notify_client_l2_params(&adapter->vsi);
2324                 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2325         }
2326         adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
2327         schedule_work(&adapter->reset_task);
2328
2329         return 0;
2330 }
2331
2332 /**
2333  * i40e_set_features - set the netdev feature flags
2334  * @netdev: ptr to the netdev being adjusted
2335  * @features: the feature set that the stack is suggesting
2336  * Note: expects to be called while under rtnl_lock()
2337  **/
2338 static int i40evf_set_features(struct net_device *netdev,
2339                                netdev_features_t features)
2340 {
2341         struct i40evf_adapter *adapter = netdev_priv(netdev);
2342
2343         if (!VLAN_ALLOWED(adapter))
2344                 return -EINVAL;
2345
2346         if (features & NETIF_F_HW_VLAN_CTAG_RX)
2347                 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2348         else
2349                 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2350
2351         return 0;
2352 }
2353
2354 /**
2355  * i40evf_features_check - Validate encapsulated packet conforms to limits
2356  * @skb: skb buff
2357  * @netdev: This physical port's netdev
2358  * @features: Offload features that the stack believes apply
2359  **/
2360 static netdev_features_t i40evf_features_check(struct sk_buff *skb,
2361                                                struct net_device *dev,
2362                                                netdev_features_t features)
2363 {
2364         size_t len;
2365
2366         /* No point in doing any of this if neither checksum nor GSO are
2367          * being requested for this frame.  We can rule out both by just
2368          * checking for CHECKSUM_PARTIAL
2369          */
2370         if (skb->ip_summed != CHECKSUM_PARTIAL)
2371                 return features;
2372
2373         /* We cannot support GSO if the MSS is going to be less than
2374          * 64 bytes.  If it is then we need to drop support for GSO.
2375          */
2376         if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
2377                 features &= ~NETIF_F_GSO_MASK;
2378
2379         /* MACLEN can support at most 63 words */
2380         len = skb_network_header(skb) - skb->data;
2381         if (len & ~(63 * 2))
2382                 goto out_err;
2383
2384         /* IPLEN and EIPLEN can support at most 127 dwords */
2385         len = skb_transport_header(skb) - skb_network_header(skb);
2386         if (len & ~(127 * 4))
2387                 goto out_err;
2388
2389         if (skb->encapsulation) {
2390                 /* L4TUNLEN can support 127 words */
2391                 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2392                 if (len & ~(127 * 2))
2393                         goto out_err;
2394
2395                 /* IPLEN can support at most 127 dwords */
2396                 len = skb_inner_transport_header(skb) -
2397                       skb_inner_network_header(skb);
2398                 if (len & ~(127 * 4))
2399                         goto out_err;
2400         }
2401
2402         /* No need to validate L4LEN as TCP is the only protocol with a
2403          * a flexible value and we support all possible values supported
2404          * by TCP, which is at most 15 dwords
2405          */
2406
2407         return features;
2408 out_err:
2409         return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2410 }
2411
2412 #define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
2413                               NETIF_F_HW_VLAN_CTAG_RX |\
2414                               NETIF_F_HW_VLAN_CTAG_FILTER)
2415
2416 /**
2417  * i40evf_fix_features - fix up the netdev feature bits
2418  * @netdev: our net device
2419  * @features: desired feature bits
2420  *
2421  * Returns fixed-up features bits
2422  **/
2423 static netdev_features_t i40evf_fix_features(struct net_device *netdev,
2424                                              netdev_features_t features)
2425 {
2426         struct i40evf_adapter *adapter = netdev_priv(netdev);
2427
2428         features &= ~I40EVF_VLAN_FEATURES;
2429         if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
2430                 features |= I40EVF_VLAN_FEATURES;
2431         return features;
2432 }
2433
2434 static const struct net_device_ops i40evf_netdev_ops = {
2435         .ndo_open               = i40evf_open,
2436         .ndo_stop               = i40evf_close,
2437         .ndo_start_xmit         = i40evf_xmit_frame,
2438         .ndo_set_rx_mode        = i40evf_set_rx_mode,
2439         .ndo_validate_addr      = eth_validate_addr,
2440         .ndo_set_mac_address    = i40evf_set_mac,
2441         .ndo_change_mtu         = i40evf_change_mtu,
2442         .ndo_tx_timeout         = i40evf_tx_timeout,
2443         .ndo_vlan_rx_add_vid    = i40evf_vlan_rx_add_vid,
2444         .ndo_vlan_rx_kill_vid   = i40evf_vlan_rx_kill_vid,
2445         .ndo_features_check     = i40evf_features_check,
2446         .ndo_fix_features       = i40evf_fix_features,
2447         .ndo_set_features       = i40evf_set_features,
2448 #ifdef CONFIG_NET_POLL_CONTROLLER
2449         .ndo_poll_controller    = i40evf_netpoll,
2450 #endif
2451 };
2452
2453 /**
2454  * i40evf_check_reset_complete - check that VF reset is complete
2455  * @hw: pointer to hw struct
2456  *
2457  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
2458  **/
2459 static int i40evf_check_reset_complete(struct i40e_hw *hw)
2460 {
2461         u32 rstat;
2462         int i;
2463
2464         for (i = 0; i < 100; i++) {
2465                 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
2466                             I40E_VFGEN_RSTAT_VFR_STATE_MASK;
2467                 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
2468                     (rstat == VIRTCHNL_VFR_COMPLETED))
2469                         return 0;
2470                 usleep_range(10, 20);
2471         }
2472         return -EBUSY;
2473 }
2474
2475 /**
2476  * i40evf_process_config - Process the config information we got from the PF
2477  * @adapter: board private structure
2478  *
2479  * Verify that we have a valid config struct, and set up our netdev features
2480  * and our VSI struct.
2481  **/
2482 int i40evf_process_config(struct i40evf_adapter *adapter)
2483 {
2484         struct virtchnl_vf_resource *vfres = adapter->vf_res;
2485         struct net_device *netdev = adapter->netdev;
2486         struct i40e_vsi *vsi = &adapter->vsi;
2487         int i;
2488         netdev_features_t hw_enc_features;
2489         netdev_features_t hw_features;
2490
2491         /* got VF config message back from PF, now we can parse it */
2492         for (i = 0; i < vfres->num_vsis; i++) {
2493                 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
2494                         adapter->vsi_res = &vfres->vsi_res[i];
2495         }
2496         if (!adapter->vsi_res) {
2497                 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2498                 return -ENODEV;
2499         }
2500
2501         hw_enc_features = NETIF_F_SG                    |
2502                           NETIF_F_IP_CSUM               |
2503                           NETIF_F_IPV6_CSUM             |
2504                           NETIF_F_HIGHDMA               |
2505                           NETIF_F_SOFT_FEATURES |
2506                           NETIF_F_TSO                   |
2507                           NETIF_F_TSO_ECN               |
2508                           NETIF_F_TSO6                  |
2509                           NETIF_F_SCTP_CRC              |
2510                           NETIF_F_RXHASH                |
2511                           NETIF_F_RXCSUM                |
2512                           0;
2513
2514         /* advertise to stack only if offloads for encapsulated packets is
2515          * supported
2516          */
2517         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
2518                 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL       |
2519                                    NETIF_F_GSO_GRE              |
2520                                    NETIF_F_GSO_GRE_CSUM         |
2521                                    NETIF_F_GSO_IPXIP4           |
2522                                    NETIF_F_GSO_IPXIP6           |
2523                                    NETIF_F_GSO_UDP_TUNNEL_CSUM  |
2524                                    NETIF_F_GSO_PARTIAL          |
2525                                    0;
2526
2527                 if (!(vfres->vf_cap_flags &
2528                       VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
2529                         netdev->gso_partial_features |=
2530                                 NETIF_F_GSO_UDP_TUNNEL_CSUM;
2531
2532                 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
2533                 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
2534                 netdev->hw_enc_features |= hw_enc_features;
2535         }
2536         /* record features VLANs can make use of */
2537         netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
2538
2539         /* Write features and hw_features separately to avoid polluting
2540          * with, or dropping, features that are set when we registered.
2541          */
2542         hw_features = hw_enc_features;
2543
2544         netdev->hw_features |= hw_features;
2545
2546         netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
2547
2548         adapter->vsi.id = adapter->vsi_res->vsi_id;
2549
2550         adapter->vsi.back = adapter;
2551         adapter->vsi.base_vector = 1;
2552         adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
2553         vsi->netdev = adapter->netdev;
2554         vsi->qs_handle = adapter->vsi_res->qset_handle;
2555         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2556                 adapter->rss_key_size = vfres->rss_key_size;
2557                 adapter->rss_lut_size = vfres->rss_lut_size;
2558         } else {
2559                 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
2560                 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
2561         }
2562
2563         return 0;
2564 }
2565
2566 /**
2567  * i40evf_init_task - worker thread to perform delayed initialization
2568  * @work: pointer to work_struct containing our data
2569  *
2570  * This task completes the work that was begun in probe. Due to the nature
2571  * of VF-PF communications, we may need to wait tens of milliseconds to get
2572  * responses back from the PF. Rather than busy-wait in probe and bog down the
2573  * whole system, we'll do it in a task so we can sleep.
2574  * This task only runs during driver init. Once we've established
2575  * communications with the PF driver and set up our netdev, the watchdog
2576  * takes over.
2577  **/
2578 static void i40evf_init_task(struct work_struct *work)
2579 {
2580         struct i40evf_adapter *adapter = container_of(work,
2581                                                       struct i40evf_adapter,
2582                                                       init_task.work);
2583         struct net_device *netdev = adapter->netdev;
2584         struct i40e_hw *hw = &adapter->hw;
2585         struct pci_dev *pdev = adapter->pdev;
2586         int err, bufsz;
2587
2588         switch (adapter->state) {
2589         case __I40EVF_STARTUP:
2590                 /* driver loaded, probe complete */
2591                 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
2592                 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
2593                 err = i40e_set_mac_type(hw);
2594                 if (err) {
2595                         dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
2596                                 err);
2597                         goto err;
2598                 }
2599                 err = i40evf_check_reset_complete(hw);
2600                 if (err) {
2601                         dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2602                                  err);
2603                         goto err;
2604                 }
2605                 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
2606                 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
2607                 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2608                 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
2609
2610                 err = i40evf_init_adminq(hw);
2611                 if (err) {
2612                         dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2613                                 err);
2614                         goto err;
2615                 }
2616                 err = i40evf_send_api_ver(adapter);
2617                 if (err) {
2618                         dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
2619                         i40evf_shutdown_adminq(hw);
2620                         goto err;
2621                 }
2622                 adapter->state = __I40EVF_INIT_VERSION_CHECK;
2623                 goto restart;
2624         case __I40EVF_INIT_VERSION_CHECK:
2625                 if (!i40evf_asq_done(hw)) {
2626                         dev_err(&pdev->dev, "Admin queue command never completed\n");
2627                         i40evf_shutdown_adminq(hw);
2628                         adapter->state = __I40EVF_STARTUP;
2629                         goto err;
2630                 }
2631
2632                 /* aq msg sent, awaiting reply */
2633                 err = i40evf_verify_api_ver(adapter);
2634                 if (err) {
2635                         if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
2636                                 err = i40evf_send_api_ver(adapter);
2637                         else
2638                                 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2639                                         adapter->pf_version.major,
2640                                         adapter->pf_version.minor,
2641                                         VIRTCHNL_VERSION_MAJOR,
2642                                         VIRTCHNL_VERSION_MINOR);
2643                         goto err;
2644                 }
2645                 err = i40evf_send_vf_config_msg(adapter);
2646                 if (err) {
2647                         dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2648                                 err);
2649                         goto err;
2650                 }
2651                 adapter->state = __I40EVF_INIT_GET_RESOURCES;
2652                 goto restart;
2653         case __I40EVF_INIT_GET_RESOURCES:
2654                 /* aq msg sent, awaiting reply */
2655                 if (!adapter->vf_res) {
2656                         bufsz = sizeof(struct virtchnl_vf_resource) +
2657                                 (I40E_MAX_VF_VSI *
2658                                  sizeof(struct virtchnl_vsi_resource));
2659                         adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
2660                         if (!adapter->vf_res)
2661                                 goto err;
2662                 }
2663                 err = i40evf_get_vf_config(adapter);
2664                 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
2665                         err = i40evf_send_vf_config_msg(adapter);
2666                         goto err;
2667                 } else if (err == I40E_ERR_PARAM) {
2668                         /* We only get ERR_PARAM if the device is in a very bad
2669                          * state or if we've been disabled for previous bad
2670                          * behavior. Either way, we're done now.
2671                          */
2672                         i40evf_shutdown_adminq(hw);
2673                         dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2674                         return;
2675                 }
2676                 if (err) {
2677                         dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
2678                                 err);
2679                         goto err_alloc;
2680                 }
2681                 adapter->state = __I40EVF_INIT_SW;
2682                 break;
2683         default:
2684                 goto err_alloc;
2685         }
2686
2687         if (i40evf_process_config(adapter))
2688                 goto err_alloc;
2689         adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2690
2691         adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
2692
2693         netdev->netdev_ops = &i40evf_netdev_ops;
2694         i40evf_set_ethtool_ops(netdev);
2695         netdev->watchdog_timeo = 5 * HZ;
2696
2697         /* MTU range: 68 - 9710 */
2698         netdev->min_mtu = ETH_MIN_MTU;
2699         netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
2700
2701         if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2702                 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2703                          adapter->hw.mac.addr);
2704                 eth_hw_addr_random(netdev);
2705                 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2706         } else {
2707                 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
2708                 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2709                 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2710         }
2711
2712         init_timer(&adapter->watchdog_timer);
2713         adapter->watchdog_timer.function = &i40evf_watchdog_timer;
2714         adapter->watchdog_timer.data = (unsigned long)adapter;
2715         mod_timer(&adapter->watchdog_timer, jiffies + 1);
2716
2717         adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
2718         adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
2719         err = i40evf_init_interrupt_scheme(adapter);
2720         if (err)
2721                 goto err_sw_init;
2722         i40evf_map_rings_to_vectors(adapter);
2723         if (adapter->vf_res->vf_cap_flags &
2724             VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2725                 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
2726
2727         err = i40evf_request_misc_irq(adapter);
2728         if (err)
2729                 goto err_sw_init;
2730
2731         netif_carrier_off(netdev);
2732         adapter->link_up = false;
2733
2734         if (!adapter->netdev_registered) {
2735                 err = register_netdev(netdev);
2736                 if (err)
2737                         goto err_register;
2738         }
2739
2740         adapter->netdev_registered = true;
2741
2742         netif_tx_stop_all_queues(netdev);
2743         if (CLIENT_ALLOWED(adapter)) {
2744                 err = i40evf_lan_add_device(adapter);
2745                 if (err)
2746                         dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2747                                  err);
2748         }
2749
2750         dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2751         if (netdev->features & NETIF_F_GRO)
2752                 dev_info(&pdev->dev, "GRO is enabled\n");
2753
2754         adapter->state = __I40EVF_DOWN;
2755         set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
2756         i40evf_misc_irq_enable(adapter);
2757         wake_up(&adapter->down_waitqueue);
2758
2759         adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2760         adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2761         if (!adapter->rss_key || !adapter->rss_lut)
2762                 goto err_mem;
2763
2764         if (RSS_AQ(adapter)) {
2765                 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
2766                 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
2767         } else {
2768                 i40evf_init_rss(adapter);
2769         }
2770         return;
2771 restart:
2772         schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
2773         return;
2774 err_mem:
2775         i40evf_free_rss(adapter);
2776 err_register:
2777         i40evf_free_misc_irq(adapter);
2778 err_sw_init:
2779         i40evf_reset_interrupt_capability(adapter);
2780 err_alloc:
2781         kfree(adapter->vf_res);
2782         adapter->vf_res = NULL;
2783 err:
2784         /* Things went into the weeds, so try again later */
2785         if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
2786                 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
2787                 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
2788                 i40evf_shutdown_adminq(hw);
2789                 adapter->state = __I40EVF_STARTUP;
2790                 schedule_delayed_work(&adapter->init_task, HZ * 5);
2791                 return;
2792         }
2793         schedule_delayed_work(&adapter->init_task, HZ);
2794 }
2795
2796 /**
2797  * i40evf_shutdown - Shutdown the device in preparation for a reboot
2798  * @pdev: pci device structure
2799  **/
2800 static void i40evf_shutdown(struct pci_dev *pdev)
2801 {
2802         struct net_device *netdev = pci_get_drvdata(pdev);
2803         struct i40evf_adapter *adapter = netdev_priv(netdev);
2804
2805         netif_device_detach(netdev);
2806
2807         if (netif_running(netdev))
2808                 i40evf_close(netdev);
2809
2810         /* Prevent the watchdog from running. */
2811         adapter->state = __I40EVF_REMOVE;
2812         adapter->aq_required = 0;
2813
2814 #ifdef CONFIG_PM
2815         pci_save_state(pdev);
2816
2817 #endif
2818         pci_disable_device(pdev);
2819 }
2820
2821 /**
2822  * i40evf_probe - Device Initialization Routine
2823  * @pdev: PCI device information struct
2824  * @ent: entry in i40evf_pci_tbl
2825  *
2826  * Returns 0 on success, negative on failure
2827  *
2828  * i40evf_probe initializes an adapter identified by a pci_dev structure.
2829  * The OS initialization, configuring of the adapter private structure,
2830  * and a hardware reset occur.
2831  **/
2832 static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2833 {
2834         struct net_device *netdev;
2835         struct i40evf_adapter *adapter = NULL;
2836         struct i40e_hw *hw = NULL;
2837         int err;
2838
2839         err = pci_enable_device(pdev);
2840         if (err)
2841                 return err;
2842
2843         err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2844         if (err) {
2845                 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2846                 if (err) {
2847                         dev_err(&pdev->dev,
2848                                 "DMA configuration failed: 0x%x\n", err);
2849                         goto err_dma;
2850                 }
2851         }
2852
2853         err = pci_request_regions(pdev, i40evf_driver_name);
2854         if (err) {
2855                 dev_err(&pdev->dev,
2856                         "pci_request_regions failed 0x%x\n", err);
2857                 goto err_pci_reg;
2858         }
2859
2860         pci_enable_pcie_error_reporting(pdev);
2861
2862         pci_set_master(pdev);
2863
2864         netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
2865         if (!netdev) {
2866                 err = -ENOMEM;
2867                 goto err_alloc_etherdev;
2868         }
2869
2870         SET_NETDEV_DEV(netdev, &pdev->dev);
2871
2872         pci_set_drvdata(pdev, netdev);
2873         adapter = netdev_priv(netdev);
2874
2875         adapter->netdev = netdev;
2876         adapter->pdev = pdev;
2877
2878         hw = &adapter->hw;
2879         hw->back = adapter;
2880
2881         adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
2882         adapter->state = __I40EVF_STARTUP;
2883
2884         /* Call save state here because it relies on the adapter struct. */
2885         pci_save_state(pdev);
2886
2887         hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
2888                               pci_resource_len(pdev, 0));
2889         if (!hw->hw_addr) {
2890                 err = -EIO;
2891                 goto err_ioremap;
2892         }
2893         hw->vendor_id = pdev->vendor;
2894         hw->device_id = pdev->device;
2895         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
2896         hw->subsystem_vendor_id = pdev->subsystem_vendor;
2897         hw->subsystem_device_id = pdev->subsystem_device;
2898         hw->bus.device = PCI_SLOT(pdev->devfn);
2899         hw->bus.func = PCI_FUNC(pdev->devfn);
2900         hw->bus.bus_id = pdev->bus->number;
2901
2902         /* set up the locks for the AQ, do this only once in probe
2903          * and destroy them only once in remove
2904          */
2905         mutex_init(&hw->aq.asq_mutex);
2906         mutex_init(&hw->aq.arq_mutex);
2907
2908         INIT_LIST_HEAD(&adapter->mac_filter_list);
2909         INIT_LIST_HEAD(&adapter->vlan_filter_list);
2910
2911         INIT_WORK(&adapter->reset_task, i40evf_reset_task);
2912         INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
2913         INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
2914         INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
2915         INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
2916         schedule_delayed_work(&adapter->init_task,
2917                               msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
2918
2919         /* Setup the wait queue for indicating transition to down status */
2920         init_waitqueue_head(&adapter->down_waitqueue);
2921
2922         return 0;
2923
2924 err_ioremap:
2925         free_netdev(netdev);
2926 err_alloc_etherdev:
2927         pci_disable_pcie_error_reporting(pdev);
2928         pci_release_regions(pdev);
2929 err_pci_reg:
2930 err_dma:
2931         pci_disable_device(pdev);
2932         return err;
2933 }
2934
2935 #ifdef CONFIG_PM
2936 /**
2937  * i40evf_suspend - Power management suspend routine
2938  * @pdev: PCI device information struct
2939  * @state: unused
2940  *
2941  * Called when the system (VM) is entering sleep/suspend.
2942  **/
2943 static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
2944 {
2945         struct net_device *netdev = pci_get_drvdata(pdev);
2946         struct i40evf_adapter *adapter = netdev_priv(netdev);
2947         int retval = 0;
2948
2949         netif_device_detach(netdev);
2950
2951         if (netif_running(netdev)) {
2952                 rtnl_lock();
2953                 i40evf_down(adapter);
2954                 rtnl_unlock();
2955         }
2956         i40evf_free_misc_irq(adapter);
2957         i40evf_reset_interrupt_capability(adapter);
2958
2959         retval = pci_save_state(pdev);
2960         if (retval)
2961                 return retval;
2962
2963         pci_disable_device(pdev);
2964
2965         return 0;
2966 }
2967
2968 /**
2969  * i40evf_resume - Power management resume routine
2970  * @pdev: PCI device information struct
2971  *
2972  * Called when the system (VM) is resumed from sleep/suspend.
2973  **/
2974 static int i40evf_resume(struct pci_dev *pdev)
2975 {
2976         struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
2977         struct net_device *netdev = adapter->netdev;
2978         u32 err;
2979
2980         pci_set_power_state(pdev, PCI_D0);
2981         pci_restore_state(pdev);
2982         /* pci_restore_state clears dev->state_saved so call
2983          * pci_save_state to restore it.
2984          */
2985         pci_save_state(pdev);
2986
2987         err = pci_enable_device_mem(pdev);
2988         if (err) {
2989                 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
2990                 return err;
2991         }
2992         pci_set_master(pdev);
2993
2994         rtnl_lock();
2995         err = i40evf_set_interrupt_capability(adapter);
2996         if (err) {
2997                 rtnl_unlock();
2998                 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
2999                 return err;
3000         }
3001         err = i40evf_request_misc_irq(adapter);
3002         rtnl_unlock();
3003         if (err) {
3004                 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3005                 return err;
3006         }
3007
3008         schedule_work(&adapter->reset_task);
3009
3010         netif_device_attach(netdev);
3011
3012         return err;
3013 }
3014
3015 #endif /* CONFIG_PM */
3016 /**
3017  * i40evf_remove - Device Removal Routine
3018  * @pdev: PCI device information struct
3019  *
3020  * i40evf_remove is called by the PCI subsystem to alert the driver
3021  * that it should release a PCI device.  The could be caused by a
3022  * Hot-Plug event, or because the driver is going to be removed from
3023  * memory.
3024  **/
3025 static void i40evf_remove(struct pci_dev *pdev)
3026 {
3027         struct net_device *netdev = pci_get_drvdata(pdev);
3028         struct i40evf_adapter *adapter = netdev_priv(netdev);
3029         struct i40evf_mac_filter *f, *ftmp;
3030         struct i40e_hw *hw = &adapter->hw;
3031         int err;
3032         /* Indicate we are in remove and not to run reset_task */
3033         set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
3034         cancel_delayed_work_sync(&adapter->init_task);
3035         cancel_work_sync(&adapter->reset_task);
3036         cancel_delayed_work_sync(&adapter->client_task);
3037         if (adapter->netdev_registered) {
3038                 unregister_netdev(netdev);
3039                 adapter->netdev_registered = false;
3040         }
3041         if (CLIENT_ALLOWED(adapter)) {
3042                 err = i40evf_lan_del_device(adapter);
3043                 if (err)
3044                         dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3045                                  err);
3046         }
3047
3048         /* Shut down all the garbage mashers on the detention level */
3049         adapter->state = __I40EVF_REMOVE;
3050         adapter->aq_required = 0;
3051         i40evf_request_reset(adapter);
3052         msleep(50);
3053         /* If the FW isn't responding, kick it once, but only once. */
3054         if (!i40evf_asq_done(hw)) {
3055                 i40evf_request_reset(adapter);
3056                 msleep(50);
3057         }
3058         i40evf_free_all_tx_resources(adapter);
3059         i40evf_free_all_rx_resources(adapter);
3060         i40evf_misc_irq_disable(adapter);
3061         i40evf_free_misc_irq(adapter);
3062         i40evf_reset_interrupt_capability(adapter);
3063         i40evf_free_q_vectors(adapter);
3064
3065         if (adapter->watchdog_timer.function)
3066                 del_timer_sync(&adapter->watchdog_timer);
3067
3068         flush_scheduled_work();
3069
3070         i40evf_free_rss(adapter);
3071
3072         if (hw->aq.asq.count)
3073                 i40evf_shutdown_adminq(hw);
3074
3075         /* destroy the locks only once, here */
3076         mutex_destroy(&hw->aq.arq_mutex);
3077         mutex_destroy(&hw->aq.asq_mutex);
3078
3079         iounmap(hw->hw_addr);
3080         pci_release_regions(pdev);
3081         i40evf_free_all_tx_resources(adapter);
3082         i40evf_free_all_rx_resources(adapter);
3083         i40evf_free_queues(adapter);
3084         kfree(adapter->vf_res);
3085         /* If we got removed before an up/down sequence, we've got a filter
3086          * hanging out there that we need to get rid of.
3087          */
3088         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3089                 list_del(&f->list);
3090                 kfree(f);
3091         }
3092         list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
3093                 list_del(&f->list);
3094                 kfree(f);
3095         }
3096
3097         free_netdev(netdev);
3098
3099         pci_disable_pcie_error_reporting(pdev);
3100
3101         pci_disable_device(pdev);
3102 }
3103
3104 static struct pci_driver i40evf_driver = {
3105         .name     = i40evf_driver_name,
3106         .id_table = i40evf_pci_tbl,
3107         .probe    = i40evf_probe,
3108         .remove   = i40evf_remove,
3109 #ifdef CONFIG_PM
3110         .suspend  = i40evf_suspend,
3111         .resume   = i40evf_resume,
3112 #endif
3113         .shutdown = i40evf_shutdown,
3114 };
3115
3116 /**
3117  * i40e_init_module - Driver Registration Routine
3118  *
3119  * i40e_init_module is the first routine called when the driver is
3120  * loaded. All it does is register with the PCI subsystem.
3121  **/
3122 static int __init i40evf_init_module(void)
3123 {
3124         int ret;
3125
3126         pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
3127                 i40evf_driver_version);
3128
3129         pr_info("%s\n", i40evf_copyright);
3130
3131         i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3132                                     i40evf_driver_name);
3133         if (!i40evf_wq) {
3134                 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3135                 return -ENOMEM;
3136         }
3137         ret = pci_register_driver(&i40evf_driver);
3138         return ret;
3139 }
3140
3141 module_init(i40evf_init_module);
3142
3143 /**
3144  * i40e_exit_module - Driver Exit Cleanup Routine
3145  *
3146  * i40e_exit_module is called just before the driver is removed
3147  * from memory.
3148  **/
3149 static void __exit i40evf_exit_module(void)
3150 {
3151         pci_unregister_driver(&i40evf_driver);
3152         destroy_workqueue(i40evf_wq);
3153 }
3154
3155 module_exit(i40evf_exit_module);
3156
3157 /* i40evf_main.c */