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