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