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