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