i40evf: cancel workqueue sync for adminq when a VF is removed
[sfrench/cifs-2.6.git] / drivers / net / ethernet / intel / i40evf / i40evf_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4 #include "i40evf.h"
5 #include "i40e_prototype.h"
6 #include "i40evf_client.h"
7 /* All i40evf tracepoints are defined by the include below, which must
8  * be included exactly once across the whole kernel with
9  * CREATE_TRACE_POINTS defined
10  */
11 #define CREATE_TRACE_POINTS
12 #include "i40e_trace.h"
13
14 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
15 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
16 static int i40evf_close(struct net_device *netdev);
17
18 char i40evf_driver_name[] = "i40evf";
19 static const char i40evf_driver_string[] =
20         "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
21
22 #define DRV_KERN "-k"
23
24 #define DRV_VERSION_MAJOR 3
25 #define DRV_VERSION_MINOR 2
26 #define DRV_VERSION_BUILD 2
27 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
28              __stringify(DRV_VERSION_MINOR) "." \
29              __stringify(DRV_VERSION_BUILD) \
30              DRV_KERN
31 const char i40evf_driver_version[] = DRV_VERSION;
32 static const char i40evf_copyright[] =
33         "Copyright (c) 2013 - 2015 Intel Corporation.";
34
35 /* i40evf_pci_tbl - PCI Device ID Table
36  *
37  * Wildcard entries (PCI_ANY_ID) should come last
38  * Last entry must be all 0s
39  *
40  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
41  *   Class, Class Mask, private data (not used) }
42  */
43 static const struct pci_device_id i40evf_pci_tbl[] = {
44         {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
45         {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
46         {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
47         {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
48         /* required last entry */
49         {0, }
50 };
51
52 MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
53
54 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
55 MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
56 MODULE_LICENSE("GPL");
57 MODULE_VERSION(DRV_VERSION);
58
59 static struct workqueue_struct *i40evf_wq;
60
61 /**
62  * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
63  * @hw:   pointer to the HW structure
64  * @mem:  ptr to mem struct to fill out
65  * @size: size of memory requested
66  * @alignment: what to align the allocation to
67  **/
68 i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
69                                       struct i40e_dma_mem *mem,
70                                       u64 size, u32 alignment)
71 {
72         struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
73
74         if (!mem)
75                 return I40E_ERR_PARAM;
76
77         mem->size = ALIGN(size, alignment);
78         mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
79                                      (dma_addr_t *)&mem->pa, GFP_KERNEL);
80         if (mem->va)
81                 return 0;
82         else
83                 return I40E_ERR_NO_MEMORY;
84 }
85
86 /**
87  * i40evf_free_dma_mem_d - OS specific memory free for shared code
88  * @hw:   pointer to the HW structure
89  * @mem:  ptr to mem struct to free
90  **/
91 i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
92 {
93         struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
94
95         if (!mem || !mem->va)
96                 return I40E_ERR_PARAM;
97         dma_free_coherent(&adapter->pdev->dev, mem->size,
98                           mem->va, (dma_addr_t)mem->pa);
99         return 0;
100 }
101
102 /**
103  * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
104  * @hw:   pointer to the HW structure
105  * @mem:  ptr to mem struct to fill out
106  * @size: size of memory requested
107  **/
108 i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
109                                        struct i40e_virt_mem *mem, u32 size)
110 {
111         if (!mem)
112                 return I40E_ERR_PARAM;
113
114         mem->size = size;
115         mem->va = kzalloc(size, GFP_KERNEL);
116
117         if (mem->va)
118                 return 0;
119         else
120                 return I40E_ERR_NO_MEMORY;
121 }
122
123 /**
124  * i40evf_free_virt_mem_d - OS specific memory free for shared code
125  * @hw:   pointer to the HW structure
126  * @mem:  ptr to mem struct to free
127  **/
128 i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
129                                    struct i40e_virt_mem *mem)
130 {
131         if (!mem)
132                 return I40E_ERR_PARAM;
133
134         /* it's ok to kfree a NULL pointer */
135         kfree(mem->va);
136
137         return 0;
138 }
139
140 /**
141  * i40evf_debug_d - OS dependent version of debug printing
142  * @hw:  pointer to the HW structure
143  * @mask: debug level mask
144  * @fmt_str: printf-type format description
145  **/
146 void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
147 {
148         char buf[512];
149         va_list argptr;
150
151         if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
152                 return;
153
154         va_start(argptr, fmt_str);
155         vsnprintf(buf, sizeof(buf), fmt_str, argptr);
156         va_end(argptr);
157
158         /* the debug string is already formatted with a newline */
159         pr_info("%s", buf);
160 }
161
162 /**
163  * i40evf_schedule_reset - Set the flags and schedule a reset event
164  * @adapter: board private structure
165  **/
166 void i40evf_schedule_reset(struct i40evf_adapter *adapter)
167 {
168         if (!(adapter->flags &
169               (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
170                 adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
171                 schedule_work(&adapter->reset_task);
172         }
173 }
174
175 /**
176  * i40evf_tx_timeout - Respond to a Tx Hang
177  * @netdev: network interface device structure
178  **/
179 static void i40evf_tx_timeout(struct net_device *netdev)
180 {
181         struct i40evf_adapter *adapter = netdev_priv(netdev);
182
183         adapter->tx_timeout_count++;
184         i40evf_schedule_reset(adapter);
185 }
186
187 /**
188  * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
189  * @adapter: board private structure
190  **/
191 static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
192 {
193         struct i40e_hw *hw = &adapter->hw;
194
195         if (!adapter->msix_entries)
196                 return;
197
198         wr32(hw, I40E_VFINT_DYN_CTL01, 0);
199
200         /* read flush */
201         rd32(hw, I40E_VFGEN_RSTAT);
202
203         synchronize_irq(adapter->msix_entries[0].vector);
204 }
205
206 /**
207  * i40evf_misc_irq_enable - Enable default interrupt generation settings
208  * @adapter: board private structure
209  **/
210 static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
211 {
212         struct i40e_hw *hw = &adapter->hw;
213
214         wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
215                                        I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
216         wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
217
218         /* read flush */
219         rd32(hw, I40E_VFGEN_RSTAT);
220 }
221
222 /**
223  * i40evf_irq_disable - Mask off interrupt generation on the NIC
224  * @adapter: board private structure
225  **/
226 static void i40evf_irq_disable(struct i40evf_adapter *adapter)
227 {
228         int i;
229         struct i40e_hw *hw = &adapter->hw;
230
231         if (!adapter->msix_entries)
232                 return;
233
234         for (i = 1; i < adapter->num_msix_vectors; i++) {
235                 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
236                 synchronize_irq(adapter->msix_entries[i].vector);
237         }
238         /* read flush */
239         rd32(hw, I40E_VFGEN_RSTAT);
240 }
241
242 /**
243  * i40evf_irq_enable_queues - Enable interrupt for specified queues
244  * @adapter: board private structure
245  * @mask: bitmap of queues to enable
246  **/
247 void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
248 {
249         struct i40e_hw *hw = &adapter->hw;
250         int i;
251
252         for (i = 1; i < adapter->num_msix_vectors; i++) {
253                 if (mask & BIT(i - 1)) {
254                         wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
255                              I40E_VFINT_DYN_CTLN1_INTENA_MASK |
256                              I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK);
257                 }
258         }
259 }
260
261 /**
262  * i40evf_irq_enable - Enable default interrupt generation settings
263  * @adapter: board private structure
264  * @flush: boolean value whether to run rd32()
265  **/
266 void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
267 {
268         struct i40e_hw *hw = &adapter->hw;
269
270         i40evf_misc_irq_enable(adapter);
271         i40evf_irq_enable_queues(adapter, ~0);
272
273         if (flush)
274                 rd32(hw, I40E_VFGEN_RSTAT);
275 }
276
277 /**
278  * i40evf_msix_aq - Interrupt handler for vector 0
279  * @irq: interrupt number
280  * @data: pointer to netdev
281  **/
282 static irqreturn_t i40evf_msix_aq(int irq, void *data)
283 {
284         struct net_device *netdev = data;
285         struct i40evf_adapter *adapter = netdev_priv(netdev);
286         struct i40e_hw *hw = &adapter->hw;
287
288         /* handle non-queue interrupts, these reads clear the registers */
289         rd32(hw, I40E_VFINT_ICR01);
290         rd32(hw, I40E_VFINT_ICR0_ENA1);
291
292         /* schedule work on the private workqueue */
293         schedule_work(&adapter->adminq_task);
294
295         return IRQ_HANDLED;
296 }
297
298 /**
299  * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
300  * @irq: interrupt number
301  * @data: pointer to a q_vector
302  **/
303 static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
304 {
305         struct i40e_q_vector *q_vector = data;
306
307         if (!q_vector->tx.ring && !q_vector->rx.ring)
308                 return IRQ_HANDLED;
309
310         napi_schedule_irqoff(&q_vector->napi);
311
312         return IRQ_HANDLED;
313 }
314
315 /**
316  * i40evf_map_vector_to_rxq - associate irqs with rx queues
317  * @adapter: board private structure
318  * @v_idx: interrupt number
319  * @r_idx: queue number
320  **/
321 static void
322 i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
323 {
324         struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
325         struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
326         struct i40e_hw *hw = &adapter->hw;
327
328         rx_ring->q_vector = q_vector;
329         rx_ring->next = q_vector->rx.ring;
330         rx_ring->vsi = &adapter->vsi;
331         q_vector->rx.ring = rx_ring;
332         q_vector->rx.count++;
333         q_vector->rx.next_update = jiffies + 1;
334         q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
335         q_vector->ring_mask |= BIT(r_idx);
336         wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, q_vector->reg_idx),
337              q_vector->rx.current_itr);
338         q_vector->rx.current_itr = q_vector->rx.target_itr;
339 }
340
341 /**
342  * i40evf_map_vector_to_txq - associate irqs with tx queues
343  * @adapter: board private structure
344  * @v_idx: interrupt number
345  * @t_idx: queue number
346  **/
347 static void
348 i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
349 {
350         struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
351         struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
352         struct i40e_hw *hw = &adapter->hw;
353
354         tx_ring->q_vector = q_vector;
355         tx_ring->next = q_vector->tx.ring;
356         tx_ring->vsi = &adapter->vsi;
357         q_vector->tx.ring = tx_ring;
358         q_vector->tx.count++;
359         q_vector->tx.next_update = jiffies + 1;
360         q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
361         q_vector->num_ringpairs++;
362         wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, q_vector->reg_idx),
363              q_vector->tx.target_itr);
364         q_vector->tx.current_itr = q_vector->tx.target_itr;
365 }
366
367 /**
368  * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
369  * @adapter: board private structure to initialize
370  *
371  * This function maps descriptor rings to the queue-specific vectors
372  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
373  * one vector per ring/queue, but on a constrained vector budget, we
374  * group the rings as "efficiently" as possible.  You would add new
375  * mapping configurations in here.
376  **/
377 static void i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
378 {
379         int rings_remaining = adapter->num_active_queues;
380         int ridx = 0, vidx = 0;
381         int q_vectors;
382
383         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
384
385         for (; ridx < rings_remaining; ridx++) {
386                 i40evf_map_vector_to_rxq(adapter, vidx, ridx);
387                 i40evf_map_vector_to_txq(adapter, vidx, ridx);
388
389                 /* In the case where we have more queues than vectors, continue
390                  * round-robin on vectors until all queues are mapped.
391                  */
392                 if (++vidx >= q_vectors)
393                         vidx = 0;
394         }
395
396         adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
397 }
398
399 #ifdef CONFIG_NET_POLL_CONTROLLER
400 /**
401  * i40evf_netpoll - A Polling 'interrupt' handler
402  * @netdev: network interface device structure
403  *
404  * This is used by netconsole to send skbs without having to re-enable
405  * interrupts.  It's not called while the normal interrupt routine is executing.
406  **/
407 static void i40evf_netpoll(struct net_device *netdev)
408 {
409         struct i40evf_adapter *adapter = netdev_priv(netdev);
410         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
411         int i;
412
413         /* if interface is down do nothing */
414         if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
415                 return;
416
417         for (i = 0; i < q_vectors; i++)
418                 i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
419 }
420
421 #endif
422 /**
423  * i40evf_irq_affinity_notify - Callback for affinity changes
424  * @notify: context as to what irq was changed
425  * @mask: the new affinity mask
426  *
427  * This is a callback function used by the irq_set_affinity_notifier function
428  * so that we may register to receive changes to the irq affinity masks.
429  **/
430 static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
431                                        const cpumask_t *mask)
432 {
433         struct i40e_q_vector *q_vector =
434                 container_of(notify, struct i40e_q_vector, affinity_notify);
435
436         cpumask_copy(&q_vector->affinity_mask, mask);
437 }
438
439 /**
440  * i40evf_irq_affinity_release - Callback for affinity notifier release
441  * @ref: internal core kernel usage
442  *
443  * This is a callback function used by the irq_set_affinity_notifier function
444  * to inform the current notification subscriber that they will no longer
445  * receive notifications.
446  **/
447 static void i40evf_irq_affinity_release(struct kref *ref) {}
448
449 /**
450  * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
451  * @adapter: board private structure
452  * @basename: device basename
453  *
454  * Allocates MSI-X vectors for tx and rx handling, and requests
455  * interrupts from the kernel.
456  **/
457 static int
458 i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
459 {
460         unsigned int vector, q_vectors;
461         unsigned int rx_int_idx = 0, tx_int_idx = 0;
462         int irq_num, err;
463         int cpu;
464
465         i40evf_irq_disable(adapter);
466         /* Decrement for Other and TCP Timer vectors */
467         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
468
469         for (vector = 0; vector < q_vectors; vector++) {
470                 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
471                 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
472
473                 if (q_vector->tx.ring && q_vector->rx.ring) {
474                         snprintf(q_vector->name, sizeof(q_vector->name),
475                                  "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
476                         tx_int_idx++;
477                 } else if (q_vector->rx.ring) {
478                         snprintf(q_vector->name, sizeof(q_vector->name),
479                                  "i40evf-%s-rx-%d", basename, rx_int_idx++);
480                 } else if (q_vector->tx.ring) {
481                         snprintf(q_vector->name, sizeof(q_vector->name),
482                                  "i40evf-%s-tx-%d", basename, tx_int_idx++);
483                 } else {
484                         /* skip this unused q_vector */
485                         continue;
486                 }
487                 err = request_irq(irq_num,
488                                   i40evf_msix_clean_rings,
489                                   0,
490                                   q_vector->name,
491                                   q_vector);
492                 if (err) {
493                         dev_info(&adapter->pdev->dev,
494                                  "Request_irq failed, error: %d\n", err);
495                         goto free_queue_irqs;
496                 }
497                 /* register for affinity change notifications */
498                 q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
499                 q_vector->affinity_notify.release =
500                                                    i40evf_irq_affinity_release;
501                 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
502                 /* Spread the IRQ affinity hints across online CPUs. Note that
503                  * get_cpu_mask returns a mask with a permanent lifetime so
504                  * it's safe to use as a hint for irq_set_affinity_hint.
505                  */
506                 cpu = cpumask_local_spread(q_vector->v_idx, -1);
507                 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
508         }
509
510         return 0;
511
512 free_queue_irqs:
513         while (vector) {
514                 vector--;
515                 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
516                 irq_set_affinity_notifier(irq_num, NULL);
517                 irq_set_affinity_hint(irq_num, NULL);
518                 free_irq(irq_num, &adapter->q_vectors[vector]);
519         }
520         return err;
521 }
522
523 /**
524  * i40evf_request_misc_irq - Initialize MSI-X interrupts
525  * @adapter: board private structure
526  *
527  * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
528  * vector is only for the admin queue, and stays active even when the netdev
529  * is closed.
530  **/
531 static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
532 {
533         struct net_device *netdev = adapter->netdev;
534         int err;
535
536         snprintf(adapter->misc_vector_name,
537                  sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
538                  dev_name(&adapter->pdev->dev));
539         err = request_irq(adapter->msix_entries[0].vector,
540                           &i40evf_msix_aq, 0,
541                           adapter->misc_vector_name, netdev);
542         if (err) {
543                 dev_err(&adapter->pdev->dev,
544                         "request_irq for %s failed: %d\n",
545                         adapter->misc_vector_name, err);
546                 free_irq(adapter->msix_entries[0].vector, netdev);
547         }
548         return err;
549 }
550
551 /**
552  * i40evf_free_traffic_irqs - Free MSI-X interrupts
553  * @adapter: board private structure
554  *
555  * Frees all MSI-X vectors other than 0.
556  **/
557 static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
558 {
559         int vector, irq_num, q_vectors;
560
561         if (!adapter->msix_entries)
562                 return;
563
564         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
565
566         for (vector = 0; vector < q_vectors; vector++) {
567                 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
568                 irq_set_affinity_notifier(irq_num, NULL);
569                 irq_set_affinity_hint(irq_num, NULL);
570                 free_irq(irq_num, &adapter->q_vectors[vector]);
571         }
572 }
573
574 /**
575  * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
576  * @adapter: board private structure
577  *
578  * Frees MSI-X vector 0.
579  **/
580 static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
581 {
582         struct net_device *netdev = adapter->netdev;
583
584         if (!adapter->msix_entries)
585                 return;
586
587         free_irq(adapter->msix_entries[0].vector, netdev);
588 }
589
590 /**
591  * i40evf_configure_tx - Configure Transmit Unit after Reset
592  * @adapter: board private structure
593  *
594  * Configure the Tx unit of the MAC after a reset.
595  **/
596 static void i40evf_configure_tx(struct i40evf_adapter *adapter)
597 {
598         struct i40e_hw *hw = &adapter->hw;
599         int i;
600
601         for (i = 0; i < adapter->num_active_queues; i++)
602                 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
603 }
604
605 /**
606  * i40evf_configure_rx - Configure Receive Unit after Reset
607  * @adapter: board private structure
608  *
609  * Configure the Rx unit of the MAC after a reset.
610  **/
611 static void i40evf_configure_rx(struct i40evf_adapter *adapter)
612 {
613         unsigned int rx_buf_len = I40E_RXBUFFER_2048;
614         struct i40e_hw *hw = &adapter->hw;
615         int i;
616
617         /* Legacy Rx will always default to a 2048 buffer size. */
618 #if (PAGE_SIZE < 8192)
619         if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
620                 struct net_device *netdev = adapter->netdev;
621
622                 /* For jumbo frames on systems with 4K pages we have to use
623                  * an order 1 page, so we might as well increase the size
624                  * of our Rx buffer to make better use of the available space
625                  */
626                 rx_buf_len = I40E_RXBUFFER_3072;
627
628                 /* We use a 1536 buffer size for configurations with
629                  * standard Ethernet mtu.  On x86 this gives us enough room
630                  * for shared info and 192 bytes of padding.
631                  */
632                 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
633                     (netdev->mtu <= ETH_DATA_LEN))
634                         rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
635         }
636 #endif
637
638         for (i = 0; i < adapter->num_active_queues; i++) {
639                 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
640                 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
641
642                 if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
643                         clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
644                 else
645                         set_ring_build_skb_enabled(&adapter->rx_rings[i]);
646         }
647 }
648
649 /**
650  * i40evf_find_vlan - Search filter list for specific vlan filter
651  * @adapter: board private structure
652  * @vlan: vlan tag
653  *
654  * Returns ptr to the filter object or NULL. Must be called while holding the
655  * mac_vlan_list_lock.
656  **/
657 static struct
658 i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
659 {
660         struct i40evf_vlan_filter *f;
661
662         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
663                 if (vlan == f->vlan)
664                         return f;
665         }
666         return NULL;
667 }
668
669 /**
670  * i40evf_add_vlan - Add a vlan filter to the list
671  * @adapter: board private structure
672  * @vlan: VLAN tag
673  *
674  * Returns ptr to the filter object or NULL when no memory available.
675  **/
676 static struct
677 i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
678 {
679         struct i40evf_vlan_filter *f = NULL;
680
681         spin_lock_bh(&adapter->mac_vlan_list_lock);
682
683         f = i40evf_find_vlan(adapter, vlan);
684         if (!f) {
685                 f = kzalloc(sizeof(*f), GFP_KERNEL);
686                 if (!f)
687                         goto clearout;
688
689                 f->vlan = vlan;
690
691                 INIT_LIST_HEAD(&f->list);
692                 list_add(&f->list, &adapter->vlan_filter_list);
693                 f->add = true;
694                 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
695         }
696
697 clearout:
698         spin_unlock_bh(&adapter->mac_vlan_list_lock);
699         return f;
700 }
701
702 /**
703  * i40evf_del_vlan - Remove a vlan filter from the list
704  * @adapter: board private structure
705  * @vlan: VLAN tag
706  **/
707 static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
708 {
709         struct i40evf_vlan_filter *f;
710
711         spin_lock_bh(&adapter->mac_vlan_list_lock);
712
713         f = i40evf_find_vlan(adapter, vlan);
714         if (f) {
715                 f->remove = true;
716                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
717         }
718
719         spin_unlock_bh(&adapter->mac_vlan_list_lock);
720 }
721
722 /**
723  * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
724  * @netdev: network device struct
725  * @proto: unused protocol data
726  * @vid: VLAN tag
727  **/
728 static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
729                                   __always_unused __be16 proto, u16 vid)
730 {
731         struct i40evf_adapter *adapter = netdev_priv(netdev);
732
733         if (!VLAN_ALLOWED(adapter))
734                 return -EIO;
735         if (i40evf_add_vlan(adapter, vid) == NULL)
736                 return -ENOMEM;
737         return 0;
738 }
739
740 /**
741  * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
742  * @netdev: network device struct
743  * @proto: unused protocol data
744  * @vid: VLAN tag
745  **/
746 static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
747                                    __always_unused __be16 proto, u16 vid)
748 {
749         struct i40evf_adapter *adapter = netdev_priv(netdev);
750
751         if (VLAN_ALLOWED(adapter)) {
752                 i40evf_del_vlan(adapter, vid);
753                 return 0;
754         }
755         return -EIO;
756 }
757
758 /**
759  * i40evf_find_filter - Search filter list for specific mac filter
760  * @adapter: board private structure
761  * @macaddr: the MAC address
762  *
763  * Returns ptr to the filter object or NULL. Must be called while holding the
764  * mac_vlan_list_lock.
765  **/
766 static struct
767 i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
768                                       const u8 *macaddr)
769 {
770         struct i40evf_mac_filter *f;
771
772         if (!macaddr)
773                 return NULL;
774
775         list_for_each_entry(f, &adapter->mac_filter_list, list) {
776                 if (ether_addr_equal(macaddr, f->macaddr))
777                         return f;
778         }
779         return NULL;
780 }
781
782 /**
783  * i40e_add_filter - Add a mac filter to the filter list
784  * @adapter: board private structure
785  * @macaddr: the MAC address
786  *
787  * Returns ptr to the filter object or NULL when no memory available.
788  **/
789 static struct
790 i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
791                                      const u8 *macaddr)
792 {
793         struct i40evf_mac_filter *f;
794
795         if (!macaddr)
796                 return NULL;
797
798         f = i40evf_find_filter(adapter, macaddr);
799         if (!f) {
800                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
801                 if (!f)
802                         return f;
803
804                 ether_addr_copy(f->macaddr, macaddr);
805
806                 list_add_tail(&f->list, &adapter->mac_filter_list);
807                 f->add = true;
808                 adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
809         } else {
810                 f->remove = false;
811         }
812
813         return f;
814 }
815
816 /**
817  * i40evf_set_mac - NDO callback to set port mac address
818  * @netdev: network interface device structure
819  * @p: pointer to an address structure
820  *
821  * Returns 0 on success, negative on failure
822  **/
823 static int i40evf_set_mac(struct net_device *netdev, void *p)
824 {
825         struct i40evf_adapter *adapter = netdev_priv(netdev);
826         struct i40e_hw *hw = &adapter->hw;
827         struct i40evf_mac_filter *f;
828         struct sockaddr *addr = p;
829
830         if (!is_valid_ether_addr(addr->sa_data))
831                 return -EADDRNOTAVAIL;
832
833         if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
834                 return 0;
835
836         if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
837                 return -EPERM;
838
839         spin_lock_bh(&adapter->mac_vlan_list_lock);
840
841         f = i40evf_find_filter(adapter, hw->mac.addr);
842         if (f) {
843                 f->remove = true;
844                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
845         }
846
847         f = i40evf_add_filter(adapter, addr->sa_data);
848
849         spin_unlock_bh(&adapter->mac_vlan_list_lock);
850
851         if (f) {
852                 ether_addr_copy(hw->mac.addr, addr->sa_data);
853                 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
854         }
855
856         return (f == NULL) ? -ENOMEM : 0;
857 }
858
859 /**
860  * i40evf_addr_sync - Callback for dev_(mc|uc)_sync to add address
861  * @netdev: the netdevice
862  * @addr: address to add
863  *
864  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
865  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
866  */
867 static int i40evf_addr_sync(struct net_device *netdev, const u8 *addr)
868 {
869         struct i40evf_adapter *adapter = netdev_priv(netdev);
870
871         if (i40evf_add_filter(adapter, addr))
872                 return 0;
873         else
874                 return -ENOMEM;
875 }
876
877 /**
878  * i40evf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
879  * @netdev: the netdevice
880  * @addr: address to add
881  *
882  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
883  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
884  */
885 static int i40evf_addr_unsync(struct net_device *netdev, const u8 *addr)
886 {
887         struct i40evf_adapter *adapter = netdev_priv(netdev);
888         struct i40evf_mac_filter *f;
889
890         /* Under some circumstances, we might receive a request to delete
891          * our own device address from our uc list. Because we store the
892          * device address in the VSI's MAC/VLAN filter list, we need to ignore
893          * such requests and not delete our device address from this list.
894          */
895         if (ether_addr_equal(addr, netdev->dev_addr))
896                 return 0;
897
898         f = i40evf_find_filter(adapter, addr);
899         if (f) {
900                 f->remove = true;
901                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
902         }
903         return 0;
904 }
905
906 /**
907  * i40evf_set_rx_mode - NDO callback to set the netdev filters
908  * @netdev: network interface device structure
909  **/
910 static void i40evf_set_rx_mode(struct net_device *netdev)
911 {
912         struct i40evf_adapter *adapter = netdev_priv(netdev);
913
914         spin_lock_bh(&adapter->mac_vlan_list_lock);
915         __dev_uc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
916         __dev_mc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
917         spin_unlock_bh(&adapter->mac_vlan_list_lock);
918
919         if (netdev->flags & IFF_PROMISC &&
920             !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
921                 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
922         else if (!(netdev->flags & IFF_PROMISC) &&
923                  adapter->flags & I40EVF_FLAG_PROMISC_ON)
924                 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
925
926         if (netdev->flags & IFF_ALLMULTI &&
927             !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
928                 adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
929         else if (!(netdev->flags & IFF_ALLMULTI) &&
930                  adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
931                 adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
932 }
933
934 /**
935  * i40evf_napi_enable_all - enable NAPI on all queue vectors
936  * @adapter: board private structure
937  **/
938 static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
939 {
940         int q_idx;
941         struct i40e_q_vector *q_vector;
942         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
943
944         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
945                 struct napi_struct *napi;
946
947                 q_vector = &adapter->q_vectors[q_idx];
948                 napi = &q_vector->napi;
949                 napi_enable(napi);
950         }
951 }
952
953 /**
954  * i40evf_napi_disable_all - disable NAPI on all queue vectors
955  * @adapter: board private structure
956  **/
957 static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
958 {
959         int q_idx;
960         struct i40e_q_vector *q_vector;
961         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
962
963         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
964                 q_vector = &adapter->q_vectors[q_idx];
965                 napi_disable(&q_vector->napi);
966         }
967 }
968
969 /**
970  * i40evf_configure - set up transmit and receive data structures
971  * @adapter: board private structure
972  **/
973 static void i40evf_configure(struct i40evf_adapter *adapter)
974 {
975         struct net_device *netdev = adapter->netdev;
976         int i;
977
978         i40evf_set_rx_mode(netdev);
979
980         i40evf_configure_tx(adapter);
981         i40evf_configure_rx(adapter);
982         adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
983
984         for (i = 0; i < adapter->num_active_queues; i++) {
985                 struct i40e_ring *ring = &adapter->rx_rings[i];
986
987                 i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
988         }
989 }
990
991 /**
992  * i40evf_up_complete - Finish the last steps of bringing up a connection
993  * @adapter: board private structure
994  *
995  * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
996  **/
997 static void i40evf_up_complete(struct i40evf_adapter *adapter)
998 {
999         adapter->state = __I40EVF_RUNNING;
1000         clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1001
1002         i40evf_napi_enable_all(adapter);
1003
1004         adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
1005         if (CLIENT_ENABLED(adapter))
1006                 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
1007         mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1008 }
1009
1010 /**
1011  * i40e_down - Shutdown the connection processing
1012  * @adapter: board private structure
1013  *
1014  * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
1015  **/
1016 void i40evf_down(struct i40evf_adapter *adapter)
1017 {
1018         struct net_device *netdev = adapter->netdev;
1019         struct i40evf_vlan_filter *vlf;
1020         struct i40evf_mac_filter *f;
1021         struct i40evf_cloud_filter *cf;
1022
1023         if (adapter->state <= __I40EVF_DOWN_PENDING)
1024                 return;
1025
1026         netif_carrier_off(netdev);
1027         netif_tx_disable(netdev);
1028         adapter->link_up = false;
1029         i40evf_napi_disable_all(adapter);
1030         i40evf_irq_disable(adapter);
1031
1032         spin_lock_bh(&adapter->mac_vlan_list_lock);
1033
1034         /* clear the sync flag on all filters */
1035         __dev_uc_unsync(adapter->netdev, NULL);
1036         __dev_mc_unsync(adapter->netdev, NULL);
1037
1038         /* remove all MAC filters */
1039         list_for_each_entry(f, &adapter->mac_filter_list, list) {
1040                 f->remove = true;
1041         }
1042
1043         /* remove all VLAN filters */
1044         list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1045                 vlf->remove = true;
1046         }
1047
1048         spin_unlock_bh(&adapter->mac_vlan_list_lock);
1049
1050         /* remove all cloud filters */
1051         spin_lock_bh(&adapter->cloud_filter_list_lock);
1052         list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1053                 cf->del = true;
1054         }
1055         spin_unlock_bh(&adapter->cloud_filter_list_lock);
1056
1057         if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
1058             adapter->state != __I40EVF_RESETTING) {
1059                 /* cancel any current operation */
1060                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1061                 /* Schedule operations to close down the HW. Don't wait
1062                  * here for this to complete. The watchdog is still running
1063                  * and it will take care of this.
1064                  */
1065                 adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
1066                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
1067                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_CLOUD_FILTER;
1068                 adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
1069         }
1070
1071         mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1072 }
1073
1074 /**
1075  * i40evf_acquire_msix_vectors - Setup the MSIX capability
1076  * @adapter: board private structure
1077  * @vectors: number of vectors to request
1078  *
1079  * Work with the OS to set up the MSIX vectors needed.
1080  *
1081  * Returns 0 on success, negative on failure
1082  **/
1083 static int
1084 i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
1085 {
1086         int err, vector_threshold;
1087
1088         /* We'll want at least 3 (vector_threshold):
1089          * 0) Other (Admin Queue and link, mostly)
1090          * 1) TxQ[0] Cleanup
1091          * 2) RxQ[0] Cleanup
1092          */
1093         vector_threshold = MIN_MSIX_COUNT;
1094
1095         /* The more we get, the more we will assign to Tx/Rx Cleanup
1096          * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1097          * Right now, we simply care about how many we'll get; we'll
1098          * set them up later while requesting irq's.
1099          */
1100         err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1101                                     vector_threshold, vectors);
1102         if (err < 0) {
1103                 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1104                 kfree(adapter->msix_entries);
1105                 adapter->msix_entries = NULL;
1106                 return err;
1107         }
1108
1109         /* Adjust for only the vectors we'll use, which is minimum
1110          * of max_msix_q_vectors + NONQ_VECS, or the number of
1111          * vectors we were allocated.
1112          */
1113         adapter->num_msix_vectors = err;
1114         return 0;
1115 }
1116
1117 /**
1118  * i40evf_free_queues - Free memory for all rings
1119  * @adapter: board private structure to initialize
1120  *
1121  * Free all of the memory associated with queue pairs.
1122  **/
1123 static void i40evf_free_queues(struct i40evf_adapter *adapter)
1124 {
1125         if (!adapter->vsi_res)
1126                 return;
1127         adapter->num_active_queues = 0;
1128         kfree(adapter->tx_rings);
1129         adapter->tx_rings = NULL;
1130         kfree(adapter->rx_rings);
1131         adapter->rx_rings = NULL;
1132 }
1133
1134 /**
1135  * i40evf_alloc_queues - Allocate memory for all rings
1136  * @adapter: board private structure to initialize
1137  *
1138  * We allocate one ring per queue at run-time since we don't know the
1139  * number of queues at compile-time.  The polling_netdev array is
1140  * intended for Multiqueue, but should work fine with a single queue.
1141  **/
1142 static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
1143 {
1144         int i, num_active_queues;
1145
1146         /* If we're in reset reallocating queues we don't actually know yet for
1147          * certain the PF gave us the number of queues we asked for but we'll
1148          * assume it did.  Once basic reset is finished we'll confirm once we
1149          * start negotiating config with PF.
1150          */
1151         if (adapter->num_req_queues)
1152                 num_active_queues = adapter->num_req_queues;
1153         else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1154                  adapter->num_tc)
1155                 num_active_queues = adapter->ch_config.total_qps;
1156         else
1157                 num_active_queues = min_t(int,
1158                                           adapter->vsi_res->num_queue_pairs,
1159                                           (int)(num_online_cpus()));
1160
1161
1162         adapter->tx_rings = kcalloc(num_active_queues,
1163                                     sizeof(struct i40e_ring), GFP_KERNEL);
1164         if (!adapter->tx_rings)
1165                 goto err_out;
1166         adapter->rx_rings = kcalloc(num_active_queues,
1167                                     sizeof(struct i40e_ring), GFP_KERNEL);
1168         if (!adapter->rx_rings)
1169                 goto err_out;
1170
1171         for (i = 0; i < num_active_queues; i++) {
1172                 struct i40e_ring *tx_ring;
1173                 struct i40e_ring *rx_ring;
1174
1175                 tx_ring = &adapter->tx_rings[i];
1176
1177                 tx_ring->queue_index = i;
1178                 tx_ring->netdev = adapter->netdev;
1179                 tx_ring->dev = &adapter->pdev->dev;
1180                 tx_ring->count = adapter->tx_desc_count;
1181                 tx_ring->itr_setting = I40E_ITR_TX_DEF;
1182                 if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
1183                         tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
1184
1185                 rx_ring = &adapter->rx_rings[i];
1186                 rx_ring->queue_index = i;
1187                 rx_ring->netdev = adapter->netdev;
1188                 rx_ring->dev = &adapter->pdev->dev;
1189                 rx_ring->count = adapter->rx_desc_count;
1190                 rx_ring->itr_setting = I40E_ITR_RX_DEF;
1191         }
1192
1193         adapter->num_active_queues = num_active_queues;
1194
1195         return 0;
1196
1197 err_out:
1198         i40evf_free_queues(adapter);
1199         return -ENOMEM;
1200 }
1201
1202 /**
1203  * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
1204  * @adapter: board private structure to initialize
1205  *
1206  * Attempt to configure the interrupts using the best available
1207  * capabilities of the hardware and the kernel.
1208  **/
1209 static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
1210 {
1211         int vector, v_budget;
1212         int pairs = 0;
1213         int err = 0;
1214
1215         if (!adapter->vsi_res) {
1216                 err = -EIO;
1217                 goto out;
1218         }
1219         pairs = adapter->num_active_queues;
1220
1221         /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1222          * us much good if we have more vectors than CPUs. However, we already
1223          * limit the total number of queues by the number of CPUs so we do not
1224          * need any further limiting here.
1225          */
1226         v_budget = min_t(int, pairs + NONQ_VECS,
1227                          (int)adapter->vf_res->max_vectors);
1228
1229         adapter->msix_entries = kcalloc(v_budget,
1230                                         sizeof(struct msix_entry), GFP_KERNEL);
1231         if (!adapter->msix_entries) {
1232                 err = -ENOMEM;
1233                 goto out;
1234         }
1235
1236         for (vector = 0; vector < v_budget; vector++)
1237                 adapter->msix_entries[vector].entry = vector;
1238
1239         err = i40evf_acquire_msix_vectors(adapter, v_budget);
1240
1241 out:
1242         netif_set_real_num_rx_queues(adapter->netdev, pairs);
1243         netif_set_real_num_tx_queues(adapter->netdev, pairs);
1244         return err;
1245 }
1246
1247 /**
1248  * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1249  * @adapter: board private structure
1250  *
1251  * Return 0 on success, negative on failure
1252  **/
1253 static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
1254 {
1255         struct i40e_aqc_get_set_rss_key_data *rss_key =
1256                 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
1257         struct i40e_hw *hw = &adapter->hw;
1258         int ret = 0;
1259
1260         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1261                 /* bail because we already have a command pending */
1262                 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1263                         adapter->current_op);
1264                 return -EBUSY;
1265         }
1266
1267         ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1268         if (ret) {
1269                 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1270                         i40evf_stat_str(hw, ret),
1271                         i40evf_aq_str(hw, hw->aq.asq_last_status));
1272                 return ret;
1273
1274         }
1275
1276         ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1277                                     adapter->rss_lut, adapter->rss_lut_size);
1278         if (ret) {
1279                 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1280                         i40evf_stat_str(hw, ret),
1281                         i40evf_aq_str(hw, hw->aq.asq_last_status));
1282         }
1283
1284         return ret;
1285
1286 }
1287
1288 /**
1289  * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
1290  * @adapter: board private structure
1291  *
1292  * Returns 0 on success, negative on failure
1293  **/
1294 static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
1295 {
1296         struct i40e_hw *hw = &adapter->hw;
1297         u32 *dw;
1298         u16 i;
1299
1300         dw = (u32 *)adapter->rss_key;
1301         for (i = 0; i <= adapter->rss_key_size / 4; i++)
1302                 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
1303
1304         dw = (u32 *)adapter->rss_lut;
1305         for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1306                 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
1307
1308         i40e_flush(hw);
1309
1310         return 0;
1311 }
1312
1313 /**
1314  * i40evf_config_rss - Configure RSS keys and lut
1315  * @adapter: board private structure
1316  *
1317  * Returns 0 on success, negative on failure
1318  **/
1319 int i40evf_config_rss(struct i40evf_adapter *adapter)
1320 {
1321
1322         if (RSS_PF(adapter)) {
1323                 adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
1324                                         I40EVF_FLAG_AQ_SET_RSS_KEY;
1325                 return 0;
1326         } else if (RSS_AQ(adapter)) {
1327                 return i40evf_config_rss_aq(adapter);
1328         } else {
1329                 return i40evf_config_rss_reg(adapter);
1330         }
1331 }
1332
1333 /**
1334  * i40evf_fill_rss_lut - Fill the lut with default values
1335  * @adapter: board private structure
1336  **/
1337 static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
1338 {
1339         u16 i;
1340
1341         for (i = 0; i < adapter->rss_lut_size; i++)
1342                 adapter->rss_lut[i] = i % adapter->num_active_queues;
1343 }
1344
1345 /**
1346  * i40evf_init_rss - Prepare for RSS
1347  * @adapter: board private structure
1348  *
1349  * Return 0 on success, negative on failure
1350  **/
1351 static int i40evf_init_rss(struct i40evf_adapter *adapter)
1352 {
1353         struct i40e_hw *hw = &adapter->hw;
1354         int ret;
1355
1356         if (!RSS_PF(adapter)) {
1357                 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1358                 if (adapter->vf_res->vf_cap_flags &
1359                     VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1360                         adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1361                 else
1362                         adapter->hena = I40E_DEFAULT_RSS_HENA;
1363
1364                 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1365                 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1366         }
1367
1368         i40evf_fill_rss_lut(adapter);
1369
1370         netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1371         ret = i40evf_config_rss(adapter);
1372
1373         return ret;
1374 }
1375
1376 /**
1377  * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
1378  * @adapter: board private structure to initialize
1379  *
1380  * We allocate one q_vector per queue interrupt.  If allocation fails we
1381  * return -ENOMEM.
1382  **/
1383 static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
1384 {
1385         int q_idx = 0, num_q_vectors;
1386         struct i40e_q_vector *q_vector;
1387
1388         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1389         adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1390                                      GFP_KERNEL);
1391         if (!adapter->q_vectors)
1392                 return -ENOMEM;
1393
1394         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1395                 q_vector = &adapter->q_vectors[q_idx];
1396                 q_vector->adapter = adapter;
1397                 q_vector->vsi = &adapter->vsi;
1398                 q_vector->v_idx = q_idx;
1399                 q_vector->reg_idx = q_idx;
1400                 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1401                 netif_napi_add(adapter->netdev, &q_vector->napi,
1402                                i40evf_napi_poll, NAPI_POLL_WEIGHT);
1403         }
1404
1405         return 0;
1406 }
1407
1408 /**
1409  * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
1410  * @adapter: board private structure to initialize
1411  *
1412  * This function frees the memory allocated to the q_vectors.  In addition if
1413  * NAPI is enabled it will delete any references to the NAPI struct prior
1414  * to freeing the q_vector.
1415  **/
1416 static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
1417 {
1418         int q_idx, num_q_vectors;
1419         int napi_vectors;
1420
1421         if (!adapter->q_vectors)
1422                 return;
1423
1424         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1425         napi_vectors = adapter->num_active_queues;
1426
1427         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1428                 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
1429                 if (q_idx < napi_vectors)
1430                         netif_napi_del(&q_vector->napi);
1431         }
1432         kfree(adapter->q_vectors);
1433         adapter->q_vectors = NULL;
1434 }
1435
1436 /**
1437  * i40evf_reset_interrupt_capability - Reset MSIX setup
1438  * @adapter: board private structure
1439  *
1440  **/
1441 void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
1442 {
1443         if (!adapter->msix_entries)
1444                 return;
1445
1446         pci_disable_msix(adapter->pdev);
1447         kfree(adapter->msix_entries);
1448         adapter->msix_entries = NULL;
1449 }
1450
1451 /**
1452  * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
1453  * @adapter: board private structure to initialize
1454  *
1455  **/
1456 int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
1457 {
1458         int err;
1459
1460         err = i40evf_alloc_queues(adapter);
1461         if (err) {
1462                 dev_err(&adapter->pdev->dev,
1463                         "Unable to allocate memory for queues\n");
1464                 goto err_alloc_queues;
1465         }
1466
1467         rtnl_lock();
1468         err = i40evf_set_interrupt_capability(adapter);
1469         rtnl_unlock();
1470         if (err) {
1471                 dev_err(&adapter->pdev->dev,
1472                         "Unable to setup interrupt capabilities\n");
1473                 goto err_set_interrupt;
1474         }
1475
1476         err = i40evf_alloc_q_vectors(adapter);
1477         if (err) {
1478                 dev_err(&adapter->pdev->dev,
1479                         "Unable to allocate memory for queue vectors\n");
1480                 goto err_alloc_q_vectors;
1481         }
1482
1483         /* If we've made it so far while ADq flag being ON, then we haven't
1484          * bailed out anywhere in middle. And ADq isn't just enabled but actual
1485          * resources have been allocated in the reset path.
1486          * Now we can truly claim that ADq is enabled.
1487          */
1488         if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1489             adapter->num_tc)
1490                 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1491                          adapter->num_tc);
1492
1493         dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1494                  (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1495                  adapter->num_active_queues);
1496
1497         return 0;
1498 err_alloc_q_vectors:
1499         i40evf_reset_interrupt_capability(adapter);
1500 err_set_interrupt:
1501         i40evf_free_queues(adapter);
1502 err_alloc_queues:
1503         return err;
1504 }
1505
1506 /**
1507  * i40evf_free_rss - Free memory used by RSS structs
1508  * @adapter: board private structure
1509  **/
1510 static void i40evf_free_rss(struct i40evf_adapter *adapter)
1511 {
1512         kfree(adapter->rss_key);
1513         adapter->rss_key = NULL;
1514
1515         kfree(adapter->rss_lut);
1516         adapter->rss_lut = NULL;
1517 }
1518
1519 /**
1520  * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
1521  * @adapter: board private structure
1522  *
1523  * Returns 0 on success, negative on failure
1524  **/
1525 static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
1526 {
1527         struct net_device *netdev = adapter->netdev;
1528         int err;
1529
1530         if (netif_running(netdev))
1531                 i40evf_free_traffic_irqs(adapter);
1532         i40evf_free_misc_irq(adapter);
1533         i40evf_reset_interrupt_capability(adapter);
1534         i40evf_free_q_vectors(adapter);
1535         i40evf_free_queues(adapter);
1536
1537         err =  i40evf_init_interrupt_scheme(adapter);
1538         if (err)
1539                 goto err;
1540
1541         netif_tx_stop_all_queues(netdev);
1542
1543         err = i40evf_request_misc_irq(adapter);
1544         if (err)
1545                 goto err;
1546
1547         set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1548
1549         i40evf_map_rings_to_vectors(adapter);
1550
1551         if (RSS_AQ(adapter))
1552                 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
1553         else
1554                 err = i40evf_init_rss(adapter);
1555 err:
1556         return err;
1557 }
1558
1559 /**
1560  * i40evf_watchdog_timer - Periodic call-back timer
1561  * @data: pointer to adapter disguised as unsigned long
1562  **/
1563 static void i40evf_watchdog_timer(struct timer_list *t)
1564 {
1565         struct i40evf_adapter *adapter = from_timer(adapter, t,
1566                                                     watchdog_timer);
1567
1568         schedule_work(&adapter->watchdog_task);
1569         /* timer will be rescheduled in watchdog task */
1570 }
1571
1572 /**
1573  * i40evf_watchdog_task - Periodic call-back task
1574  * @work: pointer to work_struct
1575  **/
1576 static void i40evf_watchdog_task(struct work_struct *work)
1577 {
1578         struct i40evf_adapter *adapter = container_of(work,
1579                                                       struct i40evf_adapter,
1580                                                       watchdog_task);
1581         struct i40e_hw *hw = &adapter->hw;
1582         u32 reg_val;
1583
1584         if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
1585                 goto restart_watchdog;
1586
1587         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
1588                 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1589                           I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1590                 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1591                     (reg_val == VIRTCHNL_VFR_COMPLETED)) {
1592                         /* A chance for redemption! */
1593                         dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1594                         adapter->state = __I40EVF_STARTUP;
1595                         adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
1596                         schedule_delayed_work(&adapter->init_task, 10);
1597                         clear_bit(__I40EVF_IN_CRITICAL_TASK,
1598                                   &adapter->crit_section);
1599                         /* Don't reschedule the watchdog, since we've restarted
1600                          * the init task. When init_task contacts the PF and
1601                          * gets everything set up again, it'll restart the
1602                          * watchdog for us. Down, boy. Sit. Stay. Woof.
1603                          */
1604                         return;
1605                 }
1606                 adapter->aq_required = 0;
1607                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1608                 goto watchdog_done;
1609         }
1610
1611         if ((adapter->state < __I40EVF_DOWN) ||
1612             (adapter->flags & I40EVF_FLAG_RESET_PENDING))
1613                 goto watchdog_done;
1614
1615         /* check for reset */
1616         reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1617         if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
1618                 adapter->state = __I40EVF_RESETTING;
1619                 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1620                 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1621                 schedule_work(&adapter->reset_task);
1622                 adapter->aq_required = 0;
1623                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1624                 goto watchdog_done;
1625         }
1626
1627         /* Process admin queue tasks. After init, everything gets done
1628          * here so we don't race on the admin queue.
1629          */
1630         if (adapter->current_op) {
1631                 if (!i40evf_asq_done(hw)) {
1632                         dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1633                         i40evf_send_api_ver(adapter);
1634                 }
1635                 goto watchdog_done;
1636         }
1637         if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
1638                 i40evf_send_vf_config_msg(adapter);
1639                 goto watchdog_done;
1640         }
1641
1642         if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
1643                 i40evf_disable_queues(adapter);
1644                 goto watchdog_done;
1645         }
1646
1647         if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
1648                 i40evf_map_queues(adapter);
1649                 goto watchdog_done;
1650         }
1651
1652         if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
1653                 i40evf_add_ether_addrs(adapter);
1654                 goto watchdog_done;
1655         }
1656
1657         if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
1658                 i40evf_add_vlans(adapter);
1659                 goto watchdog_done;
1660         }
1661
1662         if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
1663                 i40evf_del_ether_addrs(adapter);
1664                 goto watchdog_done;
1665         }
1666
1667         if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
1668                 i40evf_del_vlans(adapter);
1669                 goto watchdog_done;
1670         }
1671
1672         if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1673                 i40evf_enable_vlan_stripping(adapter);
1674                 goto watchdog_done;
1675         }
1676
1677         if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1678                 i40evf_disable_vlan_stripping(adapter);
1679                 goto watchdog_done;
1680         }
1681
1682         if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
1683                 i40evf_configure_queues(adapter);
1684                 goto watchdog_done;
1685         }
1686
1687         if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
1688                 i40evf_enable_queues(adapter);
1689                 goto watchdog_done;
1690         }
1691
1692         if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
1693                 /* This message goes straight to the firmware, not the
1694                  * PF, so we don't have to set current_op as we will
1695                  * not get a response through the ARQ.
1696                  */
1697                 i40evf_init_rss(adapter);
1698                 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
1699                 goto watchdog_done;
1700         }
1701         if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
1702                 i40evf_get_hena(adapter);
1703                 goto watchdog_done;
1704         }
1705         if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
1706                 i40evf_set_hena(adapter);
1707                 goto watchdog_done;
1708         }
1709         if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
1710                 i40evf_set_rss_key(adapter);
1711                 goto watchdog_done;
1712         }
1713         if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
1714                 i40evf_set_rss_lut(adapter);
1715                 goto watchdog_done;
1716         }
1717
1718         if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
1719                 i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1720                                        FLAG_VF_MULTICAST_PROMISC);
1721                 goto watchdog_done;
1722         }
1723
1724         if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
1725                 i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1726                 goto watchdog_done;
1727         }
1728
1729         if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
1730             (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1731                 i40evf_set_promiscuous(adapter, 0);
1732                 goto watchdog_done;
1733         }
1734
1735         if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_CHANNELS) {
1736                 i40evf_enable_channels(adapter);
1737                 goto watchdog_done;
1738         }
1739
1740         if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_CHANNELS) {
1741                 i40evf_disable_channels(adapter);
1742                 goto watchdog_done;
1743         }
1744
1745         if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1746                 i40evf_add_cloud_filter(adapter);
1747                 goto watchdog_done;
1748         }
1749
1750         if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1751                 i40evf_del_cloud_filter(adapter);
1752                 goto watchdog_done;
1753         }
1754
1755         schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1756
1757         if (adapter->state == __I40EVF_RUNNING)
1758                 i40evf_request_stats(adapter);
1759 watchdog_done:
1760         if (adapter->state == __I40EVF_RUNNING)
1761                 i40evf_detect_recover_hung(&adapter->vsi);
1762         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1763 restart_watchdog:
1764         if (adapter->state == __I40EVF_REMOVE)
1765                 return;
1766         if (adapter->aq_required)
1767                 mod_timer(&adapter->watchdog_timer,
1768                           jiffies + msecs_to_jiffies(20));
1769         else
1770                 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1771         schedule_work(&adapter->adminq_task);
1772 }
1773
1774 static void i40evf_disable_vf(struct i40evf_adapter *adapter)
1775 {
1776         struct i40evf_mac_filter *f, *ftmp;
1777         struct i40evf_vlan_filter *fv, *fvtmp;
1778         struct i40evf_cloud_filter *cf, *cftmp;
1779
1780         adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
1781
1782         /* We don't use netif_running() because it may be true prior to
1783          * ndo_open() returning, so we can't assume it means all our open
1784          * tasks have finished, since we're not holding the rtnl_lock here.
1785          */
1786         if (adapter->state == __I40EVF_RUNNING) {
1787                 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1788                 netif_carrier_off(adapter->netdev);
1789                 netif_tx_disable(adapter->netdev);
1790                 adapter->link_up = false;
1791                 i40evf_napi_disable_all(adapter);
1792                 i40evf_irq_disable(adapter);
1793                 i40evf_free_traffic_irqs(adapter);
1794                 i40evf_free_all_tx_resources(adapter);
1795                 i40evf_free_all_rx_resources(adapter);
1796         }
1797
1798         spin_lock_bh(&adapter->mac_vlan_list_lock);
1799
1800         /* Delete all of the filters */
1801         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1802                 list_del(&f->list);
1803                 kfree(f);
1804         }
1805
1806         list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1807                 list_del(&fv->list);
1808                 kfree(fv);
1809         }
1810
1811         spin_unlock_bh(&adapter->mac_vlan_list_lock);
1812
1813         spin_lock_bh(&adapter->cloud_filter_list_lock);
1814         list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1815                 list_del(&cf->list);
1816                 kfree(cf);
1817                 adapter->num_cloud_filters--;
1818         }
1819         spin_unlock_bh(&adapter->cloud_filter_list_lock);
1820
1821         i40evf_free_misc_irq(adapter);
1822         i40evf_reset_interrupt_capability(adapter);
1823         i40evf_free_queues(adapter);
1824         i40evf_free_q_vectors(adapter);
1825         kfree(adapter->vf_res);
1826         i40evf_shutdown_adminq(&adapter->hw);
1827         adapter->netdev->flags &= ~IFF_UP;
1828         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
1829         adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1830         adapter->state = __I40EVF_DOWN;
1831         wake_up(&adapter->down_waitqueue);
1832         dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1833 }
1834
1835 #define I40EVF_RESET_WAIT_MS 10
1836 #define I40EVF_RESET_WAIT_COUNT 500
1837 /**
1838  * i40evf_reset_task - Call-back task to handle hardware reset
1839  * @work: pointer to work_struct
1840  *
1841  * During reset we need to shut down and reinitialize the admin queue
1842  * before we can use it to communicate with the PF again. We also clear
1843  * and reinit the rings because that context is lost as well.
1844  **/
1845 static void i40evf_reset_task(struct work_struct *work)
1846 {
1847         struct i40evf_adapter *adapter = container_of(work,
1848                                                       struct i40evf_adapter,
1849                                                       reset_task);
1850         struct virtchnl_vf_resource *vfres = adapter->vf_res;
1851         struct net_device *netdev = adapter->netdev;
1852         struct i40e_hw *hw = &adapter->hw;
1853         struct i40evf_vlan_filter *vlf;
1854         struct i40evf_cloud_filter *cf;
1855         struct i40evf_mac_filter *f;
1856         u32 reg_val;
1857         int i = 0, err;
1858         bool running;
1859
1860         /* When device is being removed it doesn't make sense to run the reset
1861          * task, just return in such a case.
1862          */
1863         if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
1864                 return;
1865
1866         while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
1867                                 &adapter->crit_section))
1868                 usleep_range(500, 1000);
1869         if (CLIENT_ENABLED(adapter)) {
1870                 adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
1871                                     I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
1872                                     I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1873                                     I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
1874                 cancel_delayed_work_sync(&adapter->client_task);
1875                 i40evf_notify_client_close(&adapter->vsi, true);
1876         }
1877         i40evf_misc_irq_disable(adapter);
1878         if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
1879                 adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
1880                 /* Restart the AQ here. If we have been reset but didn't
1881                  * detect it, or if the PF had to reinit, our AQ will be hosed.
1882                  */
1883                 i40evf_shutdown_adminq(hw);
1884                 i40evf_init_adminq(hw);
1885                 i40evf_request_reset(adapter);
1886         }
1887         adapter->flags |= I40EVF_FLAG_RESET_PENDING;
1888
1889         /* poll until we see the reset actually happen */
1890         for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1891                 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1892                           I40E_VF_ARQLEN1_ARQENABLE_MASK;
1893                 if (!reg_val)
1894                         break;
1895                 usleep_range(5000, 10000);
1896         }
1897         if (i == I40EVF_RESET_WAIT_COUNT) {
1898                 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1899                 goto continue_reset; /* act like the reset happened */
1900         }
1901
1902         /* wait until the reset is complete and the PF is responding to us */
1903         for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
1904                 /* sleep first to make sure a minimum wait time is met */
1905                 msleep(I40EVF_RESET_WAIT_MS);
1906
1907                 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1908                           I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1909                 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
1910                         break;
1911         }
1912
1913         pci_set_master(adapter->pdev);
1914
1915         if (i == I40EVF_RESET_WAIT_COUNT) {
1916                 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1917                         reg_val);
1918                 i40evf_disable_vf(adapter);
1919                 clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
1920                 return; /* Do not attempt to reinit. It's dead, Jim. */
1921         }
1922
1923 continue_reset:
1924         /* We don't use netif_running() because it may be true prior to
1925          * ndo_open() returning, so we can't assume it means all our open
1926          * tasks have finished, since we're not holding the rtnl_lock here.
1927          */
1928         running = ((adapter->state == __I40EVF_RUNNING) ||
1929                    (adapter->state == __I40EVF_RESETTING));
1930
1931         if (running) {
1932                 netif_carrier_off(netdev);
1933                 netif_tx_stop_all_queues(netdev);
1934                 adapter->link_up = false;
1935                 i40evf_napi_disable_all(adapter);
1936         }
1937         i40evf_irq_disable(adapter);
1938
1939         adapter->state = __I40EVF_RESETTING;
1940         adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
1941
1942         /* free the Tx/Rx rings and descriptors, might be better to just
1943          * re-use them sometime in the future
1944          */
1945         i40evf_free_all_rx_resources(adapter);
1946         i40evf_free_all_tx_resources(adapter);
1947
1948         adapter->flags |= I40EVF_FLAG_QUEUES_DISABLED;
1949         /* kill and reinit the admin queue */
1950         i40evf_shutdown_adminq(hw);
1951         adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1952         err = i40evf_init_adminq(hw);
1953         if (err)
1954                 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1955                          err);
1956         adapter->aq_required = 0;
1957
1958         if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
1959                 err = i40evf_reinit_interrupt_scheme(adapter);
1960                 if (err)
1961                         goto reset_err;
1962         }
1963
1964         adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
1965         adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
1966
1967         spin_lock_bh(&adapter->mac_vlan_list_lock);
1968
1969         /* re-add all MAC filters */
1970         list_for_each_entry(f, &adapter->mac_filter_list, list) {
1971                 f->add = true;
1972         }
1973         /* re-add all VLAN filters */
1974         list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1975                 vlf->add = true;
1976         }
1977
1978         spin_unlock_bh(&adapter->mac_vlan_list_lock);
1979
1980         /* check if TCs are running and re-add all cloud filters */
1981         spin_lock_bh(&adapter->cloud_filter_list_lock);
1982         if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1983             adapter->num_tc) {
1984                 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1985                         cf->add = true;
1986                 }
1987         }
1988         spin_unlock_bh(&adapter->cloud_filter_list_lock);
1989
1990         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
1991         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
1992         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_CLOUD_FILTER;
1993         i40evf_misc_irq_enable(adapter);
1994
1995         mod_timer(&adapter->watchdog_timer, jiffies + 2);
1996
1997         /* We were running when the reset started, so we need to restore some
1998          * state here.
1999          */
2000         if (running) {
2001                 /* allocate transmit descriptors */
2002                 err = i40evf_setup_all_tx_resources(adapter);
2003                 if (err)
2004                         goto reset_err;
2005
2006                 /* allocate receive descriptors */
2007                 err = i40evf_setup_all_rx_resources(adapter);
2008                 if (err)
2009                         goto reset_err;
2010
2011                 if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
2012                         err = i40evf_request_traffic_irqs(adapter,
2013                                                           netdev->name);
2014                         if (err)
2015                                 goto reset_err;
2016
2017                         adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
2018                 }
2019
2020                 i40evf_configure(adapter);
2021
2022                 i40evf_up_complete(adapter);
2023
2024                 i40evf_irq_enable(adapter, true);
2025         } else {
2026                 adapter->state = __I40EVF_DOWN;
2027                 wake_up(&adapter->down_waitqueue);
2028         }
2029         clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2030         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2031
2032         return;
2033 reset_err:
2034         clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2035         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2036         dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
2037         i40evf_close(netdev);
2038 }
2039
2040 /**
2041  * i40evf_adminq_task - worker thread to clean the admin queue
2042  * @work: pointer to work_struct containing our data
2043  **/
2044 static void i40evf_adminq_task(struct work_struct *work)
2045 {
2046         struct i40evf_adapter *adapter =
2047                 container_of(work, struct i40evf_adapter, adminq_task);
2048         struct i40e_hw *hw = &adapter->hw;
2049         struct i40e_arq_event_info event;
2050         enum virtchnl_ops v_op;
2051         i40e_status ret, v_ret;
2052         u32 val, oldval;
2053         u16 pending;
2054
2055         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
2056                 goto out;
2057
2058         event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
2059         event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2060         if (!event.msg_buf)
2061                 goto out;
2062
2063         do {
2064                 ret = i40evf_clean_arq_element(hw, &event, &pending);
2065                 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2066                 v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
2067
2068                 if (ret || !v_op)
2069                         break; /* No event to process or error cleaning ARQ */
2070
2071                 i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2072                                            event.msg_len);
2073                 if (pending != 0)
2074                         memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
2075         } while (pending);
2076
2077         if ((adapter->flags &
2078              (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
2079             adapter->state == __I40EVF_RESETTING)
2080                 goto freedom;
2081
2082         /* check for error indications */
2083         val = rd32(hw, hw->aq.arq.len);
2084         if (val == 0xdeadbeef) /* indicates device in reset */
2085                 goto freedom;
2086         oldval = val;
2087         if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
2088                 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2089                 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
2090         }
2091         if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
2092                 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2093                 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
2094         }
2095         if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
2096                 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2097                 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
2098         }
2099         if (oldval != val)
2100                 wr32(hw, hw->aq.arq.len, val);
2101
2102         val = rd32(hw, hw->aq.asq.len);
2103         oldval = val;
2104         if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
2105                 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2106                 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
2107         }
2108         if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
2109                 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2110                 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
2111         }
2112         if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
2113                 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2114                 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
2115         }
2116         if (oldval != val)
2117                 wr32(hw, hw->aq.asq.len, val);
2118
2119 freedom:
2120         kfree(event.msg_buf);
2121 out:
2122         /* re-enable Admin queue interrupt cause */
2123         i40evf_misc_irq_enable(adapter);
2124 }
2125
2126 /**
2127  * i40evf_client_task - worker thread to perform client work
2128  * @work: pointer to work_struct containing our data
2129  *
2130  * This task handles client interactions. Because client calls can be
2131  * reentrant, we can't handle them in the watchdog.
2132  **/
2133 static void i40evf_client_task(struct work_struct *work)
2134 {
2135         struct i40evf_adapter *adapter =
2136                 container_of(work, struct i40evf_adapter, client_task.work);
2137
2138         /* If we can't get the client bit, just give up. We'll be rescheduled
2139          * later.
2140          */
2141
2142         if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
2143                 return;
2144
2145         if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2146                 i40evf_client_subtask(adapter);
2147                 adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
2148                 goto out;
2149         }
2150         if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2151                 i40evf_notify_client_l2_params(&adapter->vsi);
2152                 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2153                 goto out;
2154         }
2155         if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
2156                 i40evf_notify_client_close(&adapter->vsi, false);
2157                 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
2158                 goto out;
2159         }
2160         if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
2161                 i40evf_notify_client_open(&adapter->vsi);
2162                 adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
2163         }
2164 out:
2165         clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
2166 }
2167
2168 /**
2169  * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
2170  * @adapter: board private structure
2171  *
2172  * Free all transmit software resources
2173  **/
2174 void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
2175 {
2176         int i;
2177
2178         if (!adapter->tx_rings)
2179                 return;
2180
2181         for (i = 0; i < adapter->num_active_queues; i++)
2182                 if (adapter->tx_rings[i].desc)
2183                         i40evf_free_tx_resources(&adapter->tx_rings[i]);
2184 }
2185
2186 /**
2187  * i40evf_setup_all_tx_resources - allocate all queues Tx resources
2188  * @adapter: board private structure
2189  *
2190  * If this function returns with an error, then it's possible one or
2191  * more of the rings is populated (while the rest are not).  It is the
2192  * callers duty to clean those orphaned rings.
2193  *
2194  * Return 0 on success, negative on failure
2195  **/
2196 static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
2197 {
2198         int i, err = 0;
2199
2200         for (i = 0; i < adapter->num_active_queues; i++) {
2201                 adapter->tx_rings[i].count = adapter->tx_desc_count;
2202                 err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
2203                 if (!err)
2204                         continue;
2205                 dev_err(&adapter->pdev->dev,
2206                         "Allocation for Tx Queue %u failed\n", i);
2207                 break;
2208         }
2209
2210         return err;
2211 }
2212
2213 /**
2214  * i40evf_setup_all_rx_resources - allocate all queues Rx resources
2215  * @adapter: board private structure
2216  *
2217  * If this function returns with an error, then it's possible one or
2218  * more of the rings is populated (while the rest are not).  It is the
2219  * callers duty to clean those orphaned rings.
2220  *
2221  * Return 0 on success, negative on failure
2222  **/
2223 static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
2224 {
2225         int i, err = 0;
2226
2227         for (i = 0; i < adapter->num_active_queues; i++) {
2228                 adapter->rx_rings[i].count = adapter->rx_desc_count;
2229                 err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
2230                 if (!err)
2231                         continue;
2232                 dev_err(&adapter->pdev->dev,
2233                         "Allocation for Rx Queue %u failed\n", i);
2234                 break;
2235         }
2236         return err;
2237 }
2238
2239 /**
2240  * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
2241  * @adapter: board private structure
2242  *
2243  * Free all receive software resources
2244  **/
2245 void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
2246 {
2247         int i;
2248
2249         if (!adapter->rx_rings)
2250                 return;
2251
2252         for (i = 0; i < adapter->num_active_queues; i++)
2253                 if (adapter->rx_rings[i].desc)
2254                         i40evf_free_rx_resources(&adapter->rx_rings[i]);
2255 }
2256
2257 /**
2258  * i40evf_validate_tx_bandwidth - validate the max Tx bandwidth
2259  * @adapter: board private structure
2260  * @max_tx_rate: max Tx bw for a tc
2261  **/
2262 static int i40evf_validate_tx_bandwidth(struct i40evf_adapter *adapter,
2263                                         u64 max_tx_rate)
2264 {
2265         int speed = 0, ret = 0;
2266
2267         switch (adapter->link_speed) {
2268         case I40E_LINK_SPEED_40GB:
2269                 speed = 40000;
2270                 break;
2271         case I40E_LINK_SPEED_25GB:
2272                 speed = 25000;
2273                 break;
2274         case I40E_LINK_SPEED_20GB:
2275                 speed = 20000;
2276                 break;
2277         case I40E_LINK_SPEED_10GB:
2278                 speed = 10000;
2279                 break;
2280         case I40E_LINK_SPEED_1GB:
2281                 speed = 1000;
2282                 break;
2283         case I40E_LINK_SPEED_100MB:
2284                 speed = 100;
2285                 break;
2286         default:
2287                 break;
2288         }
2289
2290         if (max_tx_rate > speed) {
2291                 dev_err(&adapter->pdev->dev,
2292                         "Invalid tx rate specified\n");
2293                 ret = -EINVAL;
2294         }
2295
2296         return ret;
2297 }
2298
2299 /**
2300  * i40evf_validate_channel_config - validate queue mapping info
2301  * @adapter: board private structure
2302  * @mqprio_qopt: queue parameters
2303  *
2304  * This function validates if the config provided by the user to
2305  * configure queue channels is valid or not. Returns 0 on a valid
2306  * config.
2307  **/
2308 static int i40evf_validate_ch_config(struct i40evf_adapter *adapter,
2309                                      struct tc_mqprio_qopt_offload *mqprio_qopt)
2310 {
2311         u64 total_max_rate = 0;
2312         int i, num_qps = 0;
2313         u64 tx_rate = 0;
2314         int ret = 0;
2315
2316         if (mqprio_qopt->qopt.num_tc > I40EVF_MAX_TRAFFIC_CLASS ||
2317             mqprio_qopt->qopt.num_tc < 1)
2318                 return -EINVAL;
2319
2320         for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2321                 if (!mqprio_qopt->qopt.count[i] ||
2322                     mqprio_qopt->qopt.offset[i] != num_qps)
2323                         return -EINVAL;
2324                 if (mqprio_qopt->min_rate[i]) {
2325                         dev_err(&adapter->pdev->dev,
2326                                 "Invalid min tx rate (greater than 0) specified\n");
2327                         return -EINVAL;
2328                 }
2329                 /*convert to Mbps */
2330                 tx_rate = div_u64(mqprio_qopt->max_rate[i],
2331                                   I40EVF_MBPS_DIVISOR);
2332                 total_max_rate += tx_rate;
2333                 num_qps += mqprio_qopt->qopt.count[i];
2334         }
2335         if (num_qps > I40EVF_MAX_REQ_QUEUES)
2336                 return -EINVAL;
2337
2338         ret = i40evf_validate_tx_bandwidth(adapter, total_max_rate);
2339         return ret;
2340 }
2341
2342 /**
2343  * i40evf_del_all_cloud_filters - delete all cloud filters
2344  * on the traffic classes
2345  **/
2346 static void i40evf_del_all_cloud_filters(struct i40evf_adapter *adapter)
2347 {
2348         struct i40evf_cloud_filter *cf, *cftmp;
2349
2350         spin_lock_bh(&adapter->cloud_filter_list_lock);
2351         list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2352                                  list) {
2353                 list_del(&cf->list);
2354                 kfree(cf);
2355                 adapter->num_cloud_filters--;
2356         }
2357         spin_unlock_bh(&adapter->cloud_filter_list_lock);
2358 }
2359
2360 /**
2361  * __i40evf_setup_tc - configure multiple traffic classes
2362  * @netdev: network interface device structure
2363  * @type_date: tc offload data
2364  *
2365  * This function processes the config information provided by the
2366  * user to configure traffic classes/queue channels and packages the
2367  * information to request the PF to setup traffic classes.
2368  *
2369  * Returns 0 on success.
2370  **/
2371 static int __i40evf_setup_tc(struct net_device *netdev, void *type_data)
2372 {
2373         struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2374         struct i40evf_adapter *adapter = netdev_priv(netdev);
2375         struct virtchnl_vf_resource *vfres = adapter->vf_res;
2376         u8 num_tc = 0, total_qps = 0;
2377         int ret = 0, netdev_tc = 0;
2378         u64 max_tx_rate;
2379         u16 mode;
2380         int i;
2381
2382         num_tc = mqprio_qopt->qopt.num_tc;
2383         mode = mqprio_qopt->mode;
2384
2385         /* delete queue_channel */
2386         if (!mqprio_qopt->qopt.hw) {
2387                 if (adapter->ch_config.state == __I40EVF_TC_RUNNING) {
2388                         /* reset the tc configuration */
2389                         netdev_reset_tc(netdev);
2390                         adapter->num_tc = 0;
2391                         netif_tx_stop_all_queues(netdev);
2392                         netif_tx_disable(netdev);
2393                         i40evf_del_all_cloud_filters(adapter);
2394                         adapter->aq_required = I40EVF_FLAG_AQ_DISABLE_CHANNELS;
2395                         goto exit;
2396                 } else {
2397                         return -EINVAL;
2398                 }
2399         }
2400
2401         /* add queue channel */
2402         if (mode == TC_MQPRIO_MODE_CHANNEL) {
2403                 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2404                         dev_err(&adapter->pdev->dev, "ADq not supported\n");
2405                         return -EOPNOTSUPP;
2406                 }
2407                 if (adapter->ch_config.state != __I40EVF_TC_INVALID) {
2408                         dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2409                         return -EINVAL;
2410                 }
2411
2412                 ret = i40evf_validate_ch_config(adapter, mqprio_qopt);
2413                 if (ret)
2414                         return ret;
2415                 /* Return if same TC config is requested */
2416                 if (adapter->num_tc == num_tc)
2417                         return 0;
2418                 adapter->num_tc = num_tc;
2419
2420                 for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
2421                         if (i < num_tc) {
2422                                 adapter->ch_config.ch_info[i].count =
2423                                         mqprio_qopt->qopt.count[i];
2424                                 adapter->ch_config.ch_info[i].offset =
2425                                         mqprio_qopt->qopt.offset[i];
2426                                 total_qps += mqprio_qopt->qopt.count[i];
2427                                 max_tx_rate = mqprio_qopt->max_rate[i];
2428                                 /* convert to Mbps */
2429                                 max_tx_rate = div_u64(max_tx_rate,
2430                                                       I40EVF_MBPS_DIVISOR);
2431                                 adapter->ch_config.ch_info[i].max_tx_rate =
2432                                         max_tx_rate;
2433                         } else {
2434                                 adapter->ch_config.ch_info[i].count = 1;
2435                                 adapter->ch_config.ch_info[i].offset = 0;
2436                         }
2437                 }
2438                 adapter->ch_config.total_qps = total_qps;
2439                 netif_tx_stop_all_queues(netdev);
2440                 netif_tx_disable(netdev);
2441                 adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_CHANNELS;
2442                 netdev_reset_tc(netdev);
2443                 /* Report the tc mapping up the stack */
2444                 netdev_set_num_tc(adapter->netdev, num_tc);
2445                 for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
2446                         u16 qcount = mqprio_qopt->qopt.count[i];
2447                         u16 qoffset = mqprio_qopt->qopt.offset[i];
2448
2449                         if (i < num_tc)
2450                                 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2451                                                     qoffset);
2452                 }
2453         }
2454 exit:
2455         return ret;
2456 }
2457
2458 /**
2459  * i40evf_parse_cls_flower - Parse tc flower filters provided by kernel
2460  * @adapter: board private structure
2461  * @cls_flower: pointer to struct tc_cls_flower_offload
2462  * @filter: pointer to cloud filter structure
2463  */
2464 static int i40evf_parse_cls_flower(struct i40evf_adapter *adapter,
2465                                    struct tc_cls_flower_offload *f,
2466                                    struct i40evf_cloud_filter *filter)
2467 {
2468         u16 n_proto_mask = 0;
2469         u16 n_proto_key = 0;
2470         u8 field_flags = 0;
2471         u16 addr_type = 0;
2472         u16 n_proto = 0;
2473         int i = 0;
2474         struct virtchnl_filter *vf = &filter->f;
2475
2476         if (f->dissector->used_keys &
2477             ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2478               BIT(FLOW_DISSECTOR_KEY_BASIC) |
2479               BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2480               BIT(FLOW_DISSECTOR_KEY_VLAN) |
2481               BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2482               BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2483               BIT(FLOW_DISSECTOR_KEY_PORTS) |
2484               BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2485                 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2486                         f->dissector->used_keys);
2487                 return -EOPNOTSUPP;
2488         }
2489
2490         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2491                 struct flow_dissector_key_keyid *mask =
2492                         skb_flow_dissector_target(f->dissector,
2493                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
2494                                                   f->mask);
2495
2496                 if (mask->keyid != 0)
2497                         field_flags |= I40EVF_CLOUD_FIELD_TEN_ID;
2498         }
2499
2500         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
2501                 struct flow_dissector_key_basic *key =
2502                         skb_flow_dissector_target(f->dissector,
2503                                                   FLOW_DISSECTOR_KEY_BASIC,
2504                                                   f->key);
2505
2506                 struct flow_dissector_key_basic *mask =
2507                         skb_flow_dissector_target(f->dissector,
2508                                                   FLOW_DISSECTOR_KEY_BASIC,
2509                                                   f->mask);
2510                 n_proto_key = ntohs(key->n_proto);
2511                 n_proto_mask = ntohs(mask->n_proto);
2512
2513                 if (n_proto_key == ETH_P_ALL) {
2514                         n_proto_key = 0;
2515                         n_proto_mask = 0;
2516                 }
2517                 n_proto = n_proto_key & n_proto_mask;
2518                 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2519                         return -EINVAL;
2520                 if (n_proto == ETH_P_IPV6) {
2521                         /* specify flow type as TCP IPv6 */
2522                         vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2523                 }
2524
2525                 if (key->ip_proto != IPPROTO_TCP) {
2526                         dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2527                         return -EINVAL;
2528                 }
2529         }
2530
2531         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2532                 struct flow_dissector_key_eth_addrs *key =
2533                         skb_flow_dissector_target(f->dissector,
2534                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
2535                                                   f->key);
2536
2537                 struct flow_dissector_key_eth_addrs *mask =
2538                         skb_flow_dissector_target(f->dissector,
2539                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
2540                                                   f->mask);
2541                 /* use is_broadcast and is_zero to check for all 0xf or 0 */
2542                 if (!is_zero_ether_addr(mask->dst)) {
2543                         if (is_broadcast_ether_addr(mask->dst)) {
2544                                 field_flags |= I40EVF_CLOUD_FIELD_OMAC;
2545                         } else {
2546                                 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2547                                         mask->dst);
2548                                 return I40E_ERR_CONFIG;
2549                         }
2550                 }
2551
2552                 if (!is_zero_ether_addr(mask->src)) {
2553                         if (is_broadcast_ether_addr(mask->src)) {
2554                                 field_flags |= I40EVF_CLOUD_FIELD_IMAC;
2555                         } else {
2556                                 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2557                                         mask->src);
2558                                 return I40E_ERR_CONFIG;
2559                         }
2560                 }
2561
2562                 if (!is_zero_ether_addr(key->dst))
2563                         if (is_valid_ether_addr(key->dst) ||
2564                             is_multicast_ether_addr(key->dst)) {
2565                                 /* set the mask if a valid dst_mac address */
2566                                 for (i = 0; i < ETH_ALEN; i++)
2567                                         vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2568                                 ether_addr_copy(vf->data.tcp_spec.dst_mac,
2569                                                 key->dst);
2570                         }
2571
2572                 if (!is_zero_ether_addr(key->src))
2573                         if (is_valid_ether_addr(key->src) ||
2574                             is_multicast_ether_addr(key->src)) {
2575                                 /* set the mask if a valid dst_mac address */
2576                                 for (i = 0; i < ETH_ALEN; i++)
2577                                         vf->mask.tcp_spec.src_mac[i] |= 0xff;
2578                                 ether_addr_copy(vf->data.tcp_spec.src_mac,
2579                                                 key->src);
2580                 }
2581         }
2582
2583         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
2584                 struct flow_dissector_key_vlan *key =
2585                         skb_flow_dissector_target(f->dissector,
2586                                                   FLOW_DISSECTOR_KEY_VLAN,
2587                                                   f->key);
2588                 struct flow_dissector_key_vlan *mask =
2589                         skb_flow_dissector_target(f->dissector,
2590                                                   FLOW_DISSECTOR_KEY_VLAN,
2591                                                   f->mask);
2592
2593                 if (mask->vlan_id) {
2594                         if (mask->vlan_id == VLAN_VID_MASK) {
2595                                 field_flags |= I40EVF_CLOUD_FIELD_IVLAN;
2596                         } else {
2597                                 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2598                                         mask->vlan_id);
2599                                 return I40E_ERR_CONFIG;
2600                         }
2601                 }
2602                 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2603                 vf->data.tcp_spec.vlan_id = cpu_to_be16(key->vlan_id);
2604         }
2605
2606         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
2607                 struct flow_dissector_key_control *key =
2608                         skb_flow_dissector_target(f->dissector,
2609                                                   FLOW_DISSECTOR_KEY_CONTROL,
2610                                                   f->key);
2611
2612                 addr_type = key->addr_type;
2613         }
2614
2615         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2616                 struct flow_dissector_key_ipv4_addrs *key =
2617                         skb_flow_dissector_target(f->dissector,
2618                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2619                                                   f->key);
2620                 struct flow_dissector_key_ipv4_addrs *mask =
2621                         skb_flow_dissector_target(f->dissector,
2622                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2623                                                   f->mask);
2624
2625                 if (mask->dst) {
2626                         if (mask->dst == cpu_to_be32(0xffffffff)) {
2627                                 field_flags |= I40EVF_CLOUD_FIELD_IIP;
2628                         } else {
2629                                 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2630                                         be32_to_cpu(mask->dst));
2631                                 return I40E_ERR_CONFIG;
2632                         }
2633                 }
2634
2635                 if (mask->src) {
2636                         if (mask->src == cpu_to_be32(0xffffffff)) {
2637                                 field_flags |= I40EVF_CLOUD_FIELD_IIP;
2638                         } else {
2639                                 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2640                                         be32_to_cpu(mask->dst));
2641                                 return I40E_ERR_CONFIG;
2642                         }
2643                 }
2644
2645                 if (field_flags & I40EVF_CLOUD_FIELD_TEN_ID) {
2646                         dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2647                         return I40E_ERR_CONFIG;
2648                 }
2649                 if (key->dst) {
2650                         vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2651                         vf->data.tcp_spec.dst_ip[0] = key->dst;
2652                 }
2653                 if (key->src) {
2654                         vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2655                         vf->data.tcp_spec.src_ip[0] = key->src;
2656                 }
2657         }
2658
2659         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2660                 struct flow_dissector_key_ipv6_addrs *key =
2661                         skb_flow_dissector_target(f->dissector,
2662                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2663                                                   f->key);
2664                 struct flow_dissector_key_ipv6_addrs *mask =
2665                         skb_flow_dissector_target(f->dissector,
2666                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2667                                                   f->mask);
2668
2669                 /* validate mask, make sure it is not IPV6_ADDR_ANY */
2670                 if (ipv6_addr_any(&mask->dst)) {
2671                         dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2672                                 IPV6_ADDR_ANY);
2673                         return I40E_ERR_CONFIG;
2674                 }
2675
2676                 /* src and dest IPv6 address should not be LOOPBACK
2677                  * (0:0:0:0:0:0:0:1) which can be represented as ::1
2678                  */
2679                 if (ipv6_addr_loopback(&key->dst) ||
2680                     ipv6_addr_loopback(&key->src)) {
2681                         dev_err(&adapter->pdev->dev,
2682                                 "ipv6 addr should not be loopback\n");
2683                         return I40E_ERR_CONFIG;
2684                 }
2685                 if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
2686                         field_flags |= I40EVF_CLOUD_FIELD_IIP;
2687
2688                 for (i = 0; i < 4; i++)
2689                         vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2690                 memcpy(&vf->data.tcp_spec.dst_ip, &key->dst.s6_addr32,
2691                        sizeof(vf->data.tcp_spec.dst_ip));
2692                 for (i = 0; i < 4; i++)
2693                         vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2694                 memcpy(&vf->data.tcp_spec.src_ip, &key->src.s6_addr32,
2695                        sizeof(vf->data.tcp_spec.src_ip));
2696         }
2697         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
2698                 struct flow_dissector_key_ports *key =
2699                         skb_flow_dissector_target(f->dissector,
2700                                                   FLOW_DISSECTOR_KEY_PORTS,
2701                                                   f->key);
2702                 struct flow_dissector_key_ports *mask =
2703                         skb_flow_dissector_target(f->dissector,
2704                                                   FLOW_DISSECTOR_KEY_PORTS,
2705                                                   f->mask);
2706
2707                 if (mask->src) {
2708                         if (mask->src == cpu_to_be16(0xffff)) {
2709                                 field_flags |= I40EVF_CLOUD_FIELD_IIP;
2710                         } else {
2711                                 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
2712                                         be16_to_cpu(mask->src));
2713                                 return I40E_ERR_CONFIG;
2714                         }
2715                 }
2716
2717                 if (mask->dst) {
2718                         if (mask->dst == cpu_to_be16(0xffff)) {
2719                                 field_flags |= I40EVF_CLOUD_FIELD_IIP;
2720                         } else {
2721                                 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
2722                                         be16_to_cpu(mask->dst));
2723                                 return I40E_ERR_CONFIG;
2724                         }
2725                 }
2726                 if (key->dst) {
2727                         vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
2728                         vf->data.tcp_spec.dst_port = key->dst;
2729                 }
2730
2731                 if (key->src) {
2732                         vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
2733                         vf->data.tcp_spec.src_port = key->src;
2734                 }
2735         }
2736         vf->field_flags = field_flags;
2737
2738         return 0;
2739 }
2740
2741 /**
2742  * i40evf_handle_tclass - Forward to a traffic class on the device
2743  * @adapter: board private structure
2744  * @tc: traffic class index on the device
2745  * @filter: pointer to cloud filter structure
2746  */
2747 static int i40evf_handle_tclass(struct i40evf_adapter *adapter, u32 tc,
2748                                 struct i40evf_cloud_filter *filter)
2749 {
2750         if (tc == 0)
2751                 return 0;
2752         if (tc < adapter->num_tc) {
2753                 if (!filter->f.data.tcp_spec.dst_port) {
2754                         dev_err(&adapter->pdev->dev,
2755                                 "Specify destination port to redirect to traffic class other than TC0\n");
2756                         return -EINVAL;
2757                 }
2758         }
2759         /* redirect to a traffic class on the same device */
2760         filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
2761         filter->f.action_meta = tc;
2762         return 0;
2763 }
2764
2765 /**
2766  * i40evf_configure_clsflower - Add tc flower filters
2767  * @adapter: board private structure
2768  * @cls_flower: Pointer to struct tc_cls_flower_offload
2769  */
2770 static int i40evf_configure_clsflower(struct i40evf_adapter *adapter,
2771                                       struct tc_cls_flower_offload *cls_flower)
2772 {
2773         int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
2774         struct i40evf_cloud_filter *filter = NULL;
2775         int err = -EINVAL, count = 50;
2776
2777         if (tc < 0) {
2778                 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
2779                 return -EINVAL;
2780         }
2781
2782         filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2783         if (!filter)
2784                 return -ENOMEM;
2785
2786         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2787                                 &adapter->crit_section)) {
2788                 if (--count == 0)
2789                         goto err;
2790                 udelay(1);
2791         }
2792
2793         filter->cookie = cls_flower->cookie;
2794
2795         /* set the mask to all zeroes to begin with */
2796         memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
2797         /* start out with flow type and eth type IPv4 to begin with */
2798         filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
2799         err = i40evf_parse_cls_flower(adapter, cls_flower, filter);
2800         if (err < 0)
2801                 goto err;
2802
2803         err = i40evf_handle_tclass(adapter, tc, filter);
2804         if (err < 0)
2805                 goto err;
2806
2807         /* add filter to the list */
2808         spin_lock_bh(&adapter->cloud_filter_list_lock);
2809         list_add_tail(&filter->list, &adapter->cloud_filter_list);
2810         adapter->num_cloud_filters++;
2811         filter->add = true;
2812         adapter->aq_required |= I40EVF_FLAG_AQ_ADD_CLOUD_FILTER;
2813         spin_unlock_bh(&adapter->cloud_filter_list_lock);
2814 err:
2815         if (err)
2816                 kfree(filter);
2817
2818         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
2819         return err;
2820 }
2821
2822 /* i40evf_find_cf - Find the cloud filter in the list
2823  * @adapter: Board private structure
2824  * @cookie: filter specific cookie
2825  *
2826  * Returns ptr to the filter object or NULL. Must be called while holding the
2827  * cloud_filter_list_lock.
2828  */
2829 static struct i40evf_cloud_filter *i40evf_find_cf(struct i40evf_adapter *adapter,
2830                                                   unsigned long *cookie)
2831 {
2832         struct i40evf_cloud_filter *filter = NULL;
2833
2834         if (!cookie)
2835                 return NULL;
2836
2837         list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
2838                 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
2839                         return filter;
2840         }
2841         return NULL;
2842 }
2843
2844 /**
2845  * i40evf_delete_clsflower - Remove tc flower filters
2846  * @adapter: board private structure
2847  * @cls_flower: Pointer to struct tc_cls_flower_offload
2848  */
2849 static int i40evf_delete_clsflower(struct i40evf_adapter *adapter,
2850                                    struct tc_cls_flower_offload *cls_flower)
2851 {
2852         struct i40evf_cloud_filter *filter = NULL;
2853         int err = 0;
2854
2855         spin_lock_bh(&adapter->cloud_filter_list_lock);
2856         filter = i40evf_find_cf(adapter, &cls_flower->cookie);
2857         if (filter) {
2858                 filter->del = true;
2859                 adapter->aq_required |= I40EVF_FLAG_AQ_DEL_CLOUD_FILTER;
2860         } else {
2861                 err = -EINVAL;
2862         }
2863         spin_unlock_bh(&adapter->cloud_filter_list_lock);
2864
2865         return err;
2866 }
2867
2868 /**
2869  * i40evf_setup_tc_cls_flower - flower classifier offloads
2870  * @netdev: net device to configure
2871  * @type_data: offload data
2872  */
2873 static int i40evf_setup_tc_cls_flower(struct i40evf_adapter *adapter,
2874                                       struct tc_cls_flower_offload *cls_flower)
2875 {
2876         if (cls_flower->common.chain_index)
2877                 return -EOPNOTSUPP;
2878
2879         switch (cls_flower->command) {
2880         case TC_CLSFLOWER_REPLACE:
2881                 return i40evf_configure_clsflower(adapter, cls_flower);
2882         case TC_CLSFLOWER_DESTROY:
2883                 return i40evf_delete_clsflower(adapter, cls_flower);
2884         case TC_CLSFLOWER_STATS:
2885                 return -EOPNOTSUPP;
2886         default:
2887                 return -EOPNOTSUPP;
2888         }
2889 }
2890
2891 /**
2892  * i40evf_setup_tc_block_cb - block callback for tc
2893  * @type: type of offload
2894  * @type_data: offload data
2895  * @cb_priv:
2896  *
2897  * This function is the block callback for traffic classes
2898  **/
2899 static int i40evf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2900                                     void *cb_priv)
2901 {
2902         switch (type) {
2903         case TC_SETUP_CLSFLOWER:
2904                 return i40evf_setup_tc_cls_flower(cb_priv, type_data);
2905         default:
2906                 return -EOPNOTSUPP;
2907         }
2908 }
2909
2910 /**
2911  * i40evf_setup_tc_block - register callbacks for tc
2912  * @netdev: network interface device structure
2913  * @f: tc offload data
2914  *
2915  * This function registers block callbacks for tc
2916  * offloads
2917  **/
2918 static int i40evf_setup_tc_block(struct net_device *dev,
2919                                  struct tc_block_offload *f)
2920 {
2921         struct i40evf_adapter *adapter = netdev_priv(dev);
2922
2923         if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2924                 return -EOPNOTSUPP;
2925
2926         switch (f->command) {
2927         case TC_BLOCK_BIND:
2928                 return tcf_block_cb_register(f->block, i40evf_setup_tc_block_cb,
2929                                              adapter, adapter, f->extack);
2930         case TC_BLOCK_UNBIND:
2931                 tcf_block_cb_unregister(f->block, i40evf_setup_tc_block_cb,
2932                                         adapter);
2933                 return 0;
2934         default:
2935                 return -EOPNOTSUPP;
2936         }
2937 }
2938
2939 /**
2940  * i40evf_setup_tc - configure multiple traffic classes
2941  * @netdev: network interface device structure
2942  * @type: type of offload
2943  * @type_date: tc offload data
2944  *
2945  * This function is the callback to ndo_setup_tc in the
2946  * netdev_ops.
2947  *
2948  * Returns 0 on success
2949  **/
2950 static int i40evf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
2951                            void *type_data)
2952 {
2953         switch (type) {
2954         case TC_SETUP_QDISC_MQPRIO:
2955                 return __i40evf_setup_tc(netdev, type_data);
2956         case TC_SETUP_BLOCK:
2957                 return i40evf_setup_tc_block(netdev, type_data);
2958         default:
2959                 return -EOPNOTSUPP;
2960         }
2961 }
2962
2963 /**
2964  * i40evf_open - Called when a network interface is made active
2965  * @netdev: network interface device structure
2966  *
2967  * Returns 0 on success, negative value on failure
2968  *
2969  * The open entry point is called when a network interface is made
2970  * active by the system (IFF_UP).  At this point all resources needed
2971  * for transmit and receive operations are allocated, the interrupt
2972  * handler is registered with the OS, the watchdog timer is started,
2973  * and the stack is notified that the interface is ready.
2974  **/
2975 static int i40evf_open(struct net_device *netdev)
2976 {
2977         struct i40evf_adapter *adapter = netdev_priv(netdev);
2978         int err;
2979
2980         if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
2981                 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2982                 return -EIO;
2983         }
2984
2985         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
2986                                 &adapter->crit_section))
2987                 usleep_range(500, 1000);
2988
2989         if (adapter->state != __I40EVF_DOWN) {
2990                 err = -EBUSY;
2991                 goto err_unlock;
2992         }
2993
2994         /* allocate transmit descriptors */
2995         err = i40evf_setup_all_tx_resources(adapter);
2996         if (err)
2997                 goto err_setup_tx;
2998
2999         /* allocate receive descriptors */
3000         err = i40evf_setup_all_rx_resources(adapter);
3001         if (err)
3002                 goto err_setup_rx;
3003
3004         /* clear any pending interrupts, may auto mask */
3005         err = i40evf_request_traffic_irqs(adapter, netdev->name);
3006         if (err)
3007                 goto err_req_irq;
3008
3009         spin_lock_bh(&adapter->mac_vlan_list_lock);
3010
3011         i40evf_add_filter(adapter, adapter->hw.mac.addr);
3012
3013         spin_unlock_bh(&adapter->mac_vlan_list_lock);
3014
3015         i40evf_configure(adapter);
3016
3017         i40evf_up_complete(adapter);
3018
3019         i40evf_irq_enable(adapter, true);
3020
3021         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3022
3023         return 0;
3024
3025 err_req_irq:
3026         i40evf_down(adapter);
3027         i40evf_free_traffic_irqs(adapter);
3028 err_setup_rx:
3029         i40evf_free_all_rx_resources(adapter);
3030 err_setup_tx:
3031         i40evf_free_all_tx_resources(adapter);
3032 err_unlock:
3033         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3034
3035         return err;
3036 }
3037
3038 /**
3039  * i40evf_close - Disables a network interface
3040  * @netdev: network interface device structure
3041  *
3042  * Returns 0, this is not allowed to fail
3043  *
3044  * The close entry point is called when an interface is de-activated
3045  * by the OS.  The hardware is still under the drivers control, but
3046  * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3047  * are freed, along with all transmit and receive resources.
3048  **/
3049 static int i40evf_close(struct net_device *netdev)
3050 {
3051         struct i40evf_adapter *adapter = netdev_priv(netdev);
3052         int status;
3053
3054         if (adapter->state <= __I40EVF_DOWN_PENDING)
3055                 return 0;
3056
3057         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
3058                                 &adapter->crit_section))
3059                 usleep_range(500, 1000);
3060
3061         set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
3062         if (CLIENT_ENABLED(adapter))
3063                 adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
3064
3065         i40evf_down(adapter);
3066         adapter->state = __I40EVF_DOWN_PENDING;
3067         i40evf_free_traffic_irqs(adapter);
3068
3069         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3070
3071         /* We explicitly don't free resources here because the hardware is
3072          * still active and can DMA into memory. Resources are cleared in
3073          * i40evf_virtchnl_completion() after we get confirmation from the PF
3074          * driver that the rings have been stopped.
3075          *
3076          * Also, we wait for state to transition to __I40EVF_DOWN before
3077          * returning. State change occurs in i40evf_virtchnl_completion() after
3078          * VF resources are released (which occurs after PF driver processes and
3079          * responds to admin queue commands).
3080          */
3081
3082         status = wait_event_timeout(adapter->down_waitqueue,
3083                                     adapter->state == __I40EVF_DOWN,
3084                                     msecs_to_jiffies(200));
3085         if (!status)
3086                 netdev_warn(netdev, "Device resources not yet released\n");
3087         return 0;
3088 }
3089
3090 /**
3091  * i40evf_change_mtu - Change the Maximum Transfer Unit
3092  * @netdev: network interface device structure
3093  * @new_mtu: new value for maximum frame size
3094  *
3095  * Returns 0 on success, negative on failure
3096  **/
3097 static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
3098 {
3099         struct i40evf_adapter *adapter = netdev_priv(netdev);
3100
3101         netdev->mtu = new_mtu;
3102         if (CLIENT_ENABLED(adapter)) {
3103                 i40evf_notify_client_l2_params(&adapter->vsi);
3104                 adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
3105         }
3106         adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
3107         schedule_work(&adapter->reset_task);
3108
3109         return 0;
3110 }
3111
3112 /**
3113  * i40e_set_features - set the netdev feature flags
3114  * @netdev: ptr to the netdev being adjusted
3115  * @features: the feature set that the stack is suggesting
3116  * Note: expects to be called while under rtnl_lock()
3117  **/
3118 static int i40evf_set_features(struct net_device *netdev,
3119                                netdev_features_t features)
3120 {
3121         struct i40evf_adapter *adapter = netdev_priv(netdev);
3122
3123         /* Don't allow changing VLAN_RX flag when adapter is not capable
3124          * of VLAN offload
3125          */
3126         if (!VLAN_ALLOWED(adapter)) {
3127                 if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX)
3128                         return -EINVAL;
3129         } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3130                 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3131                         adapter->aq_required |=
3132                                 I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3133                 else
3134                         adapter->aq_required |=
3135                                 I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3136         }
3137
3138         return 0;
3139 }
3140
3141 /**
3142  * i40evf_features_check - Validate encapsulated packet conforms to limits
3143  * @skb: skb buff
3144  * @dev: This physical port's netdev
3145  * @features: Offload features that the stack believes apply
3146  **/
3147 static netdev_features_t i40evf_features_check(struct sk_buff *skb,
3148                                                struct net_device *dev,
3149                                                netdev_features_t features)
3150 {
3151         size_t len;
3152
3153         /* No point in doing any of this if neither checksum nor GSO are
3154          * being requested for this frame.  We can rule out both by just
3155          * checking for CHECKSUM_PARTIAL
3156          */
3157         if (skb->ip_summed != CHECKSUM_PARTIAL)
3158                 return features;
3159
3160         /* We cannot support GSO if the MSS is going to be less than
3161          * 64 bytes.  If it is then we need to drop support for GSO.
3162          */
3163         if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3164                 features &= ~NETIF_F_GSO_MASK;
3165
3166         /* MACLEN can support at most 63 words */
3167         len = skb_network_header(skb) - skb->data;
3168         if (len & ~(63 * 2))
3169                 goto out_err;
3170
3171         /* IPLEN and EIPLEN can support at most 127 dwords */
3172         len = skb_transport_header(skb) - skb_network_header(skb);
3173         if (len & ~(127 * 4))
3174                 goto out_err;
3175
3176         if (skb->encapsulation) {
3177                 /* L4TUNLEN can support 127 words */
3178                 len = skb_inner_network_header(skb) - skb_transport_header(skb);
3179                 if (len & ~(127 * 2))
3180                         goto out_err;
3181
3182                 /* IPLEN can support at most 127 dwords */
3183                 len = skb_inner_transport_header(skb) -
3184                       skb_inner_network_header(skb);
3185                 if (len & ~(127 * 4))
3186                         goto out_err;
3187         }
3188
3189         /* No need to validate L4LEN as TCP is the only protocol with a
3190          * a flexible value and we support all possible values supported
3191          * by TCP, which is at most 15 dwords
3192          */
3193
3194         return features;
3195 out_err:
3196         return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3197 }
3198
3199 /**
3200  * i40evf_fix_features - fix up the netdev feature bits
3201  * @netdev: our net device
3202  * @features: desired feature bits
3203  *
3204  * Returns fixed-up features bits
3205  **/
3206 static netdev_features_t i40evf_fix_features(struct net_device *netdev,
3207                                              netdev_features_t features)
3208 {
3209         struct i40evf_adapter *adapter = netdev_priv(netdev);
3210
3211         if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3212                 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3213                               NETIF_F_HW_VLAN_CTAG_RX |
3214                               NETIF_F_HW_VLAN_CTAG_FILTER);
3215
3216         return features;
3217 }
3218
3219 static const struct net_device_ops i40evf_netdev_ops = {
3220         .ndo_open               = i40evf_open,
3221         .ndo_stop               = i40evf_close,
3222         .ndo_start_xmit         = i40evf_xmit_frame,
3223         .ndo_set_rx_mode        = i40evf_set_rx_mode,
3224         .ndo_validate_addr      = eth_validate_addr,
3225         .ndo_set_mac_address    = i40evf_set_mac,
3226         .ndo_change_mtu         = i40evf_change_mtu,
3227         .ndo_tx_timeout         = i40evf_tx_timeout,
3228         .ndo_vlan_rx_add_vid    = i40evf_vlan_rx_add_vid,
3229         .ndo_vlan_rx_kill_vid   = i40evf_vlan_rx_kill_vid,
3230         .ndo_features_check     = i40evf_features_check,
3231         .ndo_fix_features       = i40evf_fix_features,
3232         .ndo_set_features       = i40evf_set_features,
3233 #ifdef CONFIG_NET_POLL_CONTROLLER
3234         .ndo_poll_controller    = i40evf_netpoll,
3235 #endif
3236         .ndo_setup_tc           = i40evf_setup_tc,
3237 };
3238
3239 /**
3240  * i40evf_check_reset_complete - check that VF reset is complete
3241  * @hw: pointer to hw struct
3242  *
3243  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3244  **/
3245 static int i40evf_check_reset_complete(struct i40e_hw *hw)
3246 {
3247         u32 rstat;
3248         int i;
3249
3250         for (i = 0; i < 100; i++) {
3251                 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
3252                             I40E_VFGEN_RSTAT_VFR_STATE_MASK;
3253                 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3254                     (rstat == VIRTCHNL_VFR_COMPLETED))
3255                         return 0;
3256                 usleep_range(10, 20);
3257         }
3258         return -EBUSY;
3259 }
3260
3261 /**
3262  * i40evf_process_config - Process the config information we got from the PF
3263  * @adapter: board private structure
3264  *
3265  * Verify that we have a valid config struct, and set up our netdev features
3266  * and our VSI struct.
3267  **/
3268 int i40evf_process_config(struct i40evf_adapter *adapter)
3269 {
3270         struct virtchnl_vf_resource *vfres = adapter->vf_res;
3271         int i, num_req_queues = adapter->num_req_queues;
3272         struct net_device *netdev = adapter->netdev;
3273         struct i40e_vsi *vsi = &adapter->vsi;
3274         netdev_features_t hw_enc_features;
3275         netdev_features_t hw_features;
3276
3277         /* got VF config message back from PF, now we can parse it */
3278         for (i = 0; i < vfres->num_vsis; i++) {
3279                 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3280                         adapter->vsi_res = &vfres->vsi_res[i];
3281         }
3282         if (!adapter->vsi_res) {
3283                 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3284                 return -ENODEV;
3285         }
3286
3287         if (num_req_queues &&
3288             num_req_queues != adapter->vsi_res->num_queue_pairs) {
3289                 /* Problem.  The PF gave us fewer queues than what we had
3290                  * negotiated in our request.  Need a reset to see if we can't
3291                  * get back to a working state.
3292                  */
3293                 dev_err(&adapter->pdev->dev,
3294                         "Requested %d queues, but PF only gave us %d.\n",
3295                         num_req_queues,
3296                         adapter->vsi_res->num_queue_pairs);
3297                 adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
3298                 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3299                 i40evf_schedule_reset(adapter);
3300                 return -ENODEV;
3301         }
3302         adapter->num_req_queues = 0;
3303
3304         hw_enc_features = NETIF_F_SG                    |
3305                           NETIF_F_IP_CSUM               |
3306                           NETIF_F_IPV6_CSUM             |
3307                           NETIF_F_HIGHDMA               |
3308                           NETIF_F_SOFT_FEATURES |
3309                           NETIF_F_TSO                   |
3310                           NETIF_F_TSO_ECN               |
3311                           NETIF_F_TSO6                  |
3312                           NETIF_F_SCTP_CRC              |
3313                           NETIF_F_RXHASH                |
3314                           NETIF_F_RXCSUM                |
3315                           0;
3316
3317         /* advertise to stack only if offloads for encapsulated packets is
3318          * supported
3319          */
3320         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3321                 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL       |
3322                                    NETIF_F_GSO_GRE              |
3323                                    NETIF_F_GSO_GRE_CSUM         |
3324                                    NETIF_F_GSO_IPXIP4           |
3325                                    NETIF_F_GSO_IPXIP6           |
3326                                    NETIF_F_GSO_UDP_TUNNEL_CSUM  |
3327                                    NETIF_F_GSO_PARTIAL          |
3328                                    0;
3329
3330                 if (!(vfres->vf_cap_flags &
3331                       VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3332                         netdev->gso_partial_features |=
3333                                 NETIF_F_GSO_UDP_TUNNEL_CSUM;
3334
3335                 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3336                 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3337                 netdev->hw_enc_features |= hw_enc_features;
3338         }
3339         /* record features VLANs can make use of */
3340         netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3341
3342         /* Write features and hw_features separately to avoid polluting
3343          * with, or dropping, features that are set when we registered.
3344          */
3345         hw_features = hw_enc_features;
3346
3347         /* Enable VLAN features if supported */
3348         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3349                 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3350                                 NETIF_F_HW_VLAN_CTAG_RX);
3351         /* Enable cloud filter if ADQ is supported */
3352         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3353                 hw_features |= NETIF_F_HW_TC;
3354
3355         netdev->hw_features |= hw_features;
3356
3357         netdev->features |= hw_features;
3358
3359         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3360                 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3361
3362         netdev->priv_flags |= IFF_UNICAST_FLT;
3363
3364         /* Do not turn on offloads when they are requested to be turned off.
3365          * TSO needs minimum 576 bytes to work correctly.
3366          */
3367         if (netdev->wanted_features) {
3368                 if (!(netdev->wanted_features & NETIF_F_TSO) ||
3369                     netdev->mtu < 576)
3370                         netdev->features &= ~NETIF_F_TSO;
3371                 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
3372                     netdev->mtu < 576)
3373                         netdev->features &= ~NETIF_F_TSO6;
3374                 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
3375                         netdev->features &= ~NETIF_F_TSO_ECN;
3376                 if (!(netdev->wanted_features & NETIF_F_GRO))
3377                         netdev->features &= ~NETIF_F_GRO;
3378                 if (!(netdev->wanted_features & NETIF_F_GSO))
3379                         netdev->features &= ~NETIF_F_GSO;
3380         }
3381
3382         adapter->vsi.id = adapter->vsi_res->vsi_id;
3383
3384         adapter->vsi.back = adapter;
3385         adapter->vsi.base_vector = 1;
3386         adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
3387         vsi->netdev = adapter->netdev;
3388         vsi->qs_handle = adapter->vsi_res->qset_handle;
3389         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3390                 adapter->rss_key_size = vfres->rss_key_size;
3391                 adapter->rss_lut_size = vfres->rss_lut_size;
3392         } else {
3393                 adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
3394                 adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
3395         }
3396
3397         return 0;
3398 }
3399
3400 /**
3401  * i40evf_init_task - worker thread to perform delayed initialization
3402  * @work: pointer to work_struct containing our data
3403  *
3404  * This task completes the work that was begun in probe. Due to the nature
3405  * of VF-PF communications, we may need to wait tens of milliseconds to get
3406  * responses back from the PF. Rather than busy-wait in probe and bog down the
3407  * whole system, we'll do it in a task so we can sleep.
3408  * This task only runs during driver init. Once we've established
3409  * communications with the PF driver and set up our netdev, the watchdog
3410  * takes over.
3411  **/
3412 static void i40evf_init_task(struct work_struct *work)
3413 {
3414         struct i40evf_adapter *adapter = container_of(work,
3415                                                       struct i40evf_adapter,
3416                                                       init_task.work);
3417         struct net_device *netdev = adapter->netdev;
3418         struct i40e_hw *hw = &adapter->hw;
3419         struct pci_dev *pdev = adapter->pdev;
3420         int err, bufsz;
3421
3422         switch (adapter->state) {
3423         case __I40EVF_STARTUP:
3424                 /* driver loaded, probe complete */
3425                 adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
3426                 adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
3427                 err = i40e_set_mac_type(hw);
3428                 if (err) {
3429                         dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
3430                                 err);
3431                         goto err;
3432                 }
3433                 err = i40evf_check_reset_complete(hw);
3434                 if (err) {
3435                         dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
3436                                  err);
3437                         goto err;
3438                 }
3439                 hw->aq.num_arq_entries = I40EVF_AQ_LEN;
3440                 hw->aq.num_asq_entries = I40EVF_AQ_LEN;
3441                 hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
3442                 hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
3443
3444                 err = i40evf_init_adminq(hw);
3445                 if (err) {
3446                         dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
3447                                 err);
3448                         goto err;
3449                 }
3450                 err = i40evf_send_api_ver(adapter);
3451                 if (err) {
3452                         dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
3453                         i40evf_shutdown_adminq(hw);
3454                         goto err;
3455                 }
3456                 adapter->state = __I40EVF_INIT_VERSION_CHECK;
3457                 goto restart;
3458         case __I40EVF_INIT_VERSION_CHECK:
3459                 if (!i40evf_asq_done(hw)) {
3460                         dev_err(&pdev->dev, "Admin queue command never completed\n");
3461                         i40evf_shutdown_adminq(hw);
3462                         adapter->state = __I40EVF_STARTUP;
3463                         goto err;
3464                 }
3465
3466                 /* aq msg sent, awaiting reply */
3467                 err = i40evf_verify_api_ver(adapter);
3468                 if (err) {
3469                         if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
3470                                 err = i40evf_send_api_ver(adapter);
3471                         else
3472                                 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
3473                                         adapter->pf_version.major,
3474                                         adapter->pf_version.minor,
3475                                         VIRTCHNL_VERSION_MAJOR,
3476                                         VIRTCHNL_VERSION_MINOR);
3477                         goto err;
3478                 }
3479                 err = i40evf_send_vf_config_msg(adapter);
3480                 if (err) {
3481                         dev_err(&pdev->dev, "Unable to send config request (%d)\n",
3482                                 err);
3483                         goto err;
3484                 }
3485                 adapter->state = __I40EVF_INIT_GET_RESOURCES;
3486                 goto restart;
3487         case __I40EVF_INIT_GET_RESOURCES:
3488                 /* aq msg sent, awaiting reply */
3489                 if (!adapter->vf_res) {
3490                         bufsz = sizeof(struct virtchnl_vf_resource) +
3491                                 (I40E_MAX_VF_VSI *
3492                                  sizeof(struct virtchnl_vsi_resource));
3493                         adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
3494                         if (!adapter->vf_res)
3495                                 goto err;
3496                 }
3497                 err = i40evf_get_vf_config(adapter);
3498                 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
3499                         err = i40evf_send_vf_config_msg(adapter);
3500                         goto err;
3501                 } else if (err == I40E_ERR_PARAM) {
3502                         /* We only get ERR_PARAM if the device is in a very bad
3503                          * state or if we've been disabled for previous bad
3504                          * behavior. Either way, we're done now.
3505                          */
3506                         i40evf_shutdown_adminq(hw);
3507                         dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
3508                         return;
3509                 }
3510                 if (err) {
3511                         dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
3512                                 err);
3513                         goto err_alloc;
3514                 }
3515                 adapter->state = __I40EVF_INIT_SW;
3516                 break;
3517         default:
3518                 goto err_alloc;
3519         }
3520
3521         if (i40evf_process_config(adapter))
3522                 goto err_alloc;
3523         adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3524
3525         adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
3526
3527         netdev->netdev_ops = &i40evf_netdev_ops;
3528         i40evf_set_ethtool_ops(netdev);
3529         netdev->watchdog_timeo = 5 * HZ;
3530
3531         /* MTU range: 68 - 9710 */
3532         netdev->min_mtu = ETH_MIN_MTU;
3533         netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
3534
3535         if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
3536                 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
3537                          adapter->hw.mac.addr);
3538                 eth_hw_addr_random(netdev);
3539                 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
3540         } else {
3541                 adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
3542                 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
3543                 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
3544         }
3545
3546         timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
3547         mod_timer(&adapter->watchdog_timer, jiffies + 1);
3548
3549         adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
3550         adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
3551         err = i40evf_init_interrupt_scheme(adapter);
3552         if (err)
3553                 goto err_sw_init;
3554         i40evf_map_rings_to_vectors(adapter);
3555         if (adapter->vf_res->vf_cap_flags &
3556             VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
3557                 adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
3558
3559         err = i40evf_request_misc_irq(adapter);
3560         if (err)
3561                 goto err_sw_init;
3562
3563         netif_carrier_off(netdev);
3564         adapter->link_up = false;
3565
3566         if (!adapter->netdev_registered) {
3567                 err = register_netdev(netdev);
3568                 if (err)
3569                         goto err_register;
3570         }
3571
3572         adapter->netdev_registered = true;
3573
3574         netif_tx_stop_all_queues(netdev);
3575         if (CLIENT_ALLOWED(adapter)) {
3576                 err = i40evf_lan_add_device(adapter);
3577                 if (err)
3578                         dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
3579                                  err);
3580         }
3581
3582         dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
3583         if (netdev->features & NETIF_F_GRO)
3584                 dev_info(&pdev->dev, "GRO is enabled\n");
3585
3586         adapter->state = __I40EVF_DOWN;
3587         set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
3588         i40evf_misc_irq_enable(adapter);
3589         wake_up(&adapter->down_waitqueue);
3590
3591         adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
3592         adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
3593         if (!adapter->rss_key || !adapter->rss_lut)
3594                 goto err_mem;
3595
3596         if (RSS_AQ(adapter)) {
3597                 adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
3598                 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
3599         } else {
3600                 i40evf_init_rss(adapter);
3601         }
3602         return;
3603 restart:
3604         schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
3605         return;
3606 err_mem:
3607         i40evf_free_rss(adapter);
3608 err_register:
3609         i40evf_free_misc_irq(adapter);
3610 err_sw_init:
3611         i40evf_reset_interrupt_capability(adapter);
3612 err_alloc:
3613         kfree(adapter->vf_res);
3614         adapter->vf_res = NULL;
3615 err:
3616         /* Things went into the weeds, so try again later */
3617         if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
3618                 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
3619                 adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
3620                 i40evf_shutdown_adminq(hw);
3621                 adapter->state = __I40EVF_STARTUP;
3622                 schedule_delayed_work(&adapter->init_task, HZ * 5);
3623                 return;
3624         }
3625         schedule_delayed_work(&adapter->init_task, HZ);
3626 }
3627
3628 /**
3629  * i40evf_shutdown - Shutdown the device in preparation for a reboot
3630  * @pdev: pci device structure
3631  **/
3632 static void i40evf_shutdown(struct pci_dev *pdev)
3633 {
3634         struct net_device *netdev = pci_get_drvdata(pdev);
3635         struct i40evf_adapter *adapter = netdev_priv(netdev);
3636
3637         netif_device_detach(netdev);
3638
3639         if (netif_running(netdev))
3640                 i40evf_close(netdev);
3641
3642         /* Prevent the watchdog from running. */
3643         adapter->state = __I40EVF_REMOVE;
3644         adapter->aq_required = 0;
3645
3646 #ifdef CONFIG_PM
3647         pci_save_state(pdev);
3648
3649 #endif
3650         pci_disable_device(pdev);
3651 }
3652
3653 /**
3654  * i40evf_probe - Device Initialization Routine
3655  * @pdev: PCI device information struct
3656  * @ent: entry in i40evf_pci_tbl
3657  *
3658  * Returns 0 on success, negative on failure
3659  *
3660  * i40evf_probe initializes an adapter identified by a pci_dev structure.
3661  * The OS initialization, configuring of the adapter private structure,
3662  * and a hardware reset occur.
3663  **/
3664 static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3665 {
3666         struct net_device *netdev;
3667         struct i40evf_adapter *adapter = NULL;
3668         struct i40e_hw *hw = NULL;
3669         int err;
3670
3671         err = pci_enable_device(pdev);
3672         if (err)
3673                 return err;
3674
3675         err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3676         if (err) {
3677                 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3678                 if (err) {
3679                         dev_err(&pdev->dev,
3680                                 "DMA configuration failed: 0x%x\n", err);
3681                         goto err_dma;
3682                 }
3683         }
3684
3685         err = pci_request_regions(pdev, i40evf_driver_name);
3686         if (err) {
3687                 dev_err(&pdev->dev,
3688                         "pci_request_regions failed 0x%x\n", err);
3689                 goto err_pci_reg;
3690         }
3691
3692         pci_enable_pcie_error_reporting(pdev);
3693
3694         pci_set_master(pdev);
3695
3696         netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
3697                                    I40EVF_MAX_REQ_QUEUES);
3698         if (!netdev) {
3699                 err = -ENOMEM;
3700                 goto err_alloc_etherdev;
3701         }
3702
3703         SET_NETDEV_DEV(netdev, &pdev->dev);
3704
3705         pci_set_drvdata(pdev, netdev);
3706         adapter = netdev_priv(netdev);
3707
3708         adapter->netdev = netdev;
3709         adapter->pdev = pdev;
3710
3711         hw = &adapter->hw;
3712         hw->back = adapter;
3713
3714         adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3715         adapter->state = __I40EVF_STARTUP;
3716
3717         /* Call save state here because it relies on the adapter struct. */
3718         pci_save_state(pdev);
3719
3720         hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3721                               pci_resource_len(pdev, 0));
3722         if (!hw->hw_addr) {
3723                 err = -EIO;
3724                 goto err_ioremap;
3725         }
3726         hw->vendor_id = pdev->vendor;
3727         hw->device_id = pdev->device;
3728         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3729         hw->subsystem_vendor_id = pdev->subsystem_vendor;
3730         hw->subsystem_device_id = pdev->subsystem_device;
3731         hw->bus.device = PCI_SLOT(pdev->devfn);
3732         hw->bus.func = PCI_FUNC(pdev->devfn);
3733         hw->bus.bus_id = pdev->bus->number;
3734
3735         /* set up the locks for the AQ, do this only once in probe
3736          * and destroy them only once in remove
3737          */
3738         mutex_init(&hw->aq.asq_mutex);
3739         mutex_init(&hw->aq.arq_mutex);
3740
3741         spin_lock_init(&adapter->mac_vlan_list_lock);
3742         spin_lock_init(&adapter->cloud_filter_list_lock);
3743
3744         INIT_LIST_HEAD(&adapter->mac_filter_list);
3745         INIT_LIST_HEAD(&adapter->vlan_filter_list);
3746         INIT_LIST_HEAD(&adapter->cloud_filter_list);
3747
3748         INIT_WORK(&adapter->reset_task, i40evf_reset_task);
3749         INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
3750         INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
3751         INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
3752         INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
3753         schedule_delayed_work(&adapter->init_task,
3754                               msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3755
3756         /* Setup the wait queue for indicating transition to down status */
3757         init_waitqueue_head(&adapter->down_waitqueue);
3758
3759         return 0;
3760
3761 err_ioremap:
3762         free_netdev(netdev);
3763 err_alloc_etherdev:
3764         pci_release_regions(pdev);
3765 err_pci_reg:
3766 err_dma:
3767         pci_disable_device(pdev);
3768         return err;
3769 }
3770
3771 #ifdef CONFIG_PM
3772 /**
3773  * i40evf_suspend - Power management suspend routine
3774  * @pdev: PCI device information struct
3775  * @state: unused
3776  *
3777  * Called when the system (VM) is entering sleep/suspend.
3778  **/
3779 static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
3780 {
3781         struct net_device *netdev = pci_get_drvdata(pdev);
3782         struct i40evf_adapter *adapter = netdev_priv(netdev);
3783         int retval = 0;
3784
3785         netif_device_detach(netdev);
3786
3787         while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
3788                                 &adapter->crit_section))
3789                 usleep_range(500, 1000);
3790
3791         if (netif_running(netdev)) {
3792                 rtnl_lock();
3793                 i40evf_down(adapter);
3794                 rtnl_unlock();
3795         }
3796         i40evf_free_misc_irq(adapter);
3797         i40evf_reset_interrupt_capability(adapter);
3798
3799         clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
3800
3801         retval = pci_save_state(pdev);
3802         if (retval)
3803                 return retval;
3804
3805         pci_disable_device(pdev);
3806
3807         return 0;
3808 }
3809
3810 /**
3811  * i40evf_resume - Power management resume routine
3812  * @pdev: PCI device information struct
3813  *
3814  * Called when the system (VM) is resumed from sleep/suspend.
3815  **/
3816 static int i40evf_resume(struct pci_dev *pdev)
3817 {
3818         struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
3819         struct net_device *netdev = adapter->netdev;
3820         u32 err;
3821
3822         pci_set_power_state(pdev, PCI_D0);
3823         pci_restore_state(pdev);
3824         /* pci_restore_state clears dev->state_saved so call
3825          * pci_save_state to restore it.
3826          */
3827         pci_save_state(pdev);
3828
3829         err = pci_enable_device_mem(pdev);
3830         if (err) {
3831                 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3832                 return err;
3833         }
3834         pci_set_master(pdev);
3835
3836         rtnl_lock();
3837         err = i40evf_set_interrupt_capability(adapter);
3838         if (err) {
3839                 rtnl_unlock();
3840                 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3841                 return err;
3842         }
3843         err = i40evf_request_misc_irq(adapter);
3844         rtnl_unlock();
3845         if (err) {
3846                 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3847                 return err;
3848         }
3849
3850         schedule_work(&adapter->reset_task);
3851
3852         netif_device_attach(netdev);
3853
3854         return err;
3855 }
3856
3857 #endif /* CONFIG_PM */
3858 /**
3859  * i40evf_remove - Device Removal Routine
3860  * @pdev: PCI device information struct
3861  *
3862  * i40evf_remove is called by the PCI subsystem to alert the driver
3863  * that it should release a PCI device.  The could be caused by a
3864  * Hot-Plug event, or because the driver is going to be removed from
3865  * memory.
3866  **/
3867 static void i40evf_remove(struct pci_dev *pdev)
3868 {
3869         struct net_device *netdev = pci_get_drvdata(pdev);
3870         struct i40evf_adapter *adapter = netdev_priv(netdev);
3871         struct i40evf_vlan_filter *vlf, *vlftmp;
3872         struct i40evf_mac_filter *f, *ftmp;
3873         struct i40evf_cloud_filter *cf, *cftmp;
3874         struct i40e_hw *hw = &adapter->hw;
3875         int err;
3876         /* Indicate we are in remove and not to run reset_task */
3877         set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
3878         cancel_delayed_work_sync(&adapter->init_task);
3879         cancel_work_sync(&adapter->reset_task);
3880         cancel_delayed_work_sync(&adapter->client_task);
3881         if (adapter->netdev_registered) {
3882                 unregister_netdev(netdev);
3883                 adapter->netdev_registered = false;
3884         }
3885         if (CLIENT_ALLOWED(adapter)) {
3886                 err = i40evf_lan_del_device(adapter);
3887                 if (err)
3888                         dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3889                                  err);
3890         }
3891
3892         /* Shut down all the garbage mashers on the detention level */
3893         adapter->state = __I40EVF_REMOVE;
3894         adapter->aq_required = 0;
3895         adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
3896         i40evf_request_reset(adapter);
3897         msleep(50);
3898         /* If the FW isn't responding, kick it once, but only once. */
3899         if (!i40evf_asq_done(hw)) {
3900                 i40evf_request_reset(adapter);
3901                 msleep(50);
3902         }
3903         i40evf_free_all_tx_resources(adapter);
3904         i40evf_free_all_rx_resources(adapter);
3905         i40evf_misc_irq_disable(adapter);
3906         i40evf_free_misc_irq(adapter);
3907         i40evf_reset_interrupt_capability(adapter);
3908         i40evf_free_q_vectors(adapter);
3909
3910         if (adapter->watchdog_timer.function)
3911                 del_timer_sync(&adapter->watchdog_timer);
3912
3913         cancel_work_sync(&adapter->adminq_task);
3914
3915         i40evf_free_rss(adapter);
3916
3917         if (hw->aq.asq.count)
3918                 i40evf_shutdown_adminq(hw);
3919
3920         /* destroy the locks only once, here */
3921         mutex_destroy(&hw->aq.arq_mutex);
3922         mutex_destroy(&hw->aq.asq_mutex);
3923
3924         iounmap(hw->hw_addr);
3925         pci_release_regions(pdev);
3926         i40evf_free_all_tx_resources(adapter);
3927         i40evf_free_all_rx_resources(adapter);
3928         i40evf_free_queues(adapter);
3929         kfree(adapter->vf_res);
3930         spin_lock_bh(&adapter->mac_vlan_list_lock);
3931         /* If we got removed before an up/down sequence, we've got a filter
3932          * hanging out there that we need to get rid of.
3933          */
3934         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3935                 list_del(&f->list);
3936                 kfree(f);
3937         }
3938         list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3939                                  list) {
3940                 list_del(&vlf->list);
3941                 kfree(vlf);
3942         }
3943
3944         spin_unlock_bh(&adapter->mac_vlan_list_lock);
3945
3946         spin_lock_bh(&adapter->cloud_filter_list_lock);
3947         list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3948                 list_del(&cf->list);
3949                 kfree(cf);
3950         }
3951         spin_unlock_bh(&adapter->cloud_filter_list_lock);
3952
3953         free_netdev(netdev);
3954
3955         pci_disable_pcie_error_reporting(pdev);
3956
3957         pci_disable_device(pdev);
3958 }
3959
3960 static struct pci_driver i40evf_driver = {
3961         .name     = i40evf_driver_name,
3962         .id_table = i40evf_pci_tbl,
3963         .probe    = i40evf_probe,
3964         .remove   = i40evf_remove,
3965 #ifdef CONFIG_PM
3966         .suspend  = i40evf_suspend,
3967         .resume   = i40evf_resume,
3968 #endif
3969         .shutdown = i40evf_shutdown,
3970 };
3971
3972 /**
3973  * i40e_init_module - Driver Registration Routine
3974  *
3975  * i40e_init_module is the first routine called when the driver is
3976  * loaded. All it does is register with the PCI subsystem.
3977  **/
3978 static int __init i40evf_init_module(void)
3979 {
3980         int ret;
3981
3982         pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
3983                 i40evf_driver_version);
3984
3985         pr_info("%s\n", i40evf_copyright);
3986
3987         i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3988                                     i40evf_driver_name);
3989         if (!i40evf_wq) {
3990                 pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
3991                 return -ENOMEM;
3992         }
3993         ret = pci_register_driver(&i40evf_driver);
3994         return ret;
3995 }
3996
3997 module_init(i40evf_init_module);
3998
3999 /**
4000  * i40e_exit_module - Driver Exit Cleanup Routine
4001  *
4002  * i40e_exit_module is called just before the driver is removed
4003  * from memory.
4004  **/
4005 static void __exit i40evf_exit_module(void)
4006 {
4007         pci_unregister_driver(&i40evf_driver);
4008         destroy_workqueue(i40evf_wq);
4009 }
4010
4011 module_exit(i40evf_exit_module);
4012
4013 /* i40evf_main.c */