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