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