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