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