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