powerpc/mm: Avoid calling arch_enter/leave_lazy_mmu() in set_ptes
[platform/kernel/linux-starfive.git] / drivers / net / ethernet / intel / iavf / iavf_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4 #include "iavf.h"
5 #include "iavf_prototype.h"
6 #include "iavf_client.h"
7 /* All iavf 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 "iavf_trace.h"
13
14 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter);
15 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter);
16 static int iavf_close(struct net_device *netdev);
17 static void iavf_init_get_resources(struct iavf_adapter *adapter);
18 static int iavf_check_reset_complete(struct iavf_hw *hw);
19
20 char iavf_driver_name[] = "iavf";
21 static const char iavf_driver_string[] =
22         "Intel(R) Ethernet Adaptive Virtual Function Network Driver";
23
24 static const char iavf_copyright[] =
25         "Copyright (c) 2013 - 2018 Intel Corporation.";
26
27 /* iavf_pci_tbl - PCI Device ID Table
28  *
29  * Wildcard entries (PCI_ANY_ID) should come last
30  * Last entry must be all 0s
31  *
32  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
33  *   Class, Class Mask, private data (not used) }
34  */
35 static const struct pci_device_id iavf_pci_tbl[] = {
36         {PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF), 0},
37         {PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF_HV), 0},
38         {PCI_VDEVICE(INTEL, IAVF_DEV_ID_X722_VF), 0},
39         {PCI_VDEVICE(INTEL, IAVF_DEV_ID_ADAPTIVE_VF), 0},
40         /* required last entry */
41         {0, }
42 };
43
44 MODULE_DEVICE_TABLE(pci, iavf_pci_tbl);
45
46 MODULE_ALIAS("i40evf");
47 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
48 MODULE_DESCRIPTION("Intel(R) Ethernet Adaptive Virtual Function Network Driver");
49 MODULE_LICENSE("GPL v2");
50
51 static const struct net_device_ops iavf_netdev_ops;
52
53 int iavf_status_to_errno(enum iavf_status status)
54 {
55         switch (status) {
56         case IAVF_SUCCESS:
57                 return 0;
58         case IAVF_ERR_PARAM:
59         case IAVF_ERR_MAC_TYPE:
60         case IAVF_ERR_INVALID_MAC_ADDR:
61         case IAVF_ERR_INVALID_LINK_SETTINGS:
62         case IAVF_ERR_INVALID_PD_ID:
63         case IAVF_ERR_INVALID_QP_ID:
64         case IAVF_ERR_INVALID_CQ_ID:
65         case IAVF_ERR_INVALID_CEQ_ID:
66         case IAVF_ERR_INVALID_AEQ_ID:
67         case IAVF_ERR_INVALID_SIZE:
68         case IAVF_ERR_INVALID_ARP_INDEX:
69         case IAVF_ERR_INVALID_FPM_FUNC_ID:
70         case IAVF_ERR_QP_INVALID_MSG_SIZE:
71         case IAVF_ERR_INVALID_FRAG_COUNT:
72         case IAVF_ERR_INVALID_ALIGNMENT:
73         case IAVF_ERR_INVALID_PUSH_PAGE_INDEX:
74         case IAVF_ERR_INVALID_IMM_DATA_SIZE:
75         case IAVF_ERR_INVALID_VF_ID:
76         case IAVF_ERR_INVALID_HMCFN_ID:
77         case IAVF_ERR_INVALID_PBLE_INDEX:
78         case IAVF_ERR_INVALID_SD_INDEX:
79         case IAVF_ERR_INVALID_PAGE_DESC_INDEX:
80         case IAVF_ERR_INVALID_SD_TYPE:
81         case IAVF_ERR_INVALID_HMC_OBJ_INDEX:
82         case IAVF_ERR_INVALID_HMC_OBJ_COUNT:
83         case IAVF_ERR_INVALID_SRQ_ARM_LIMIT:
84                 return -EINVAL;
85         case IAVF_ERR_NVM:
86         case IAVF_ERR_NVM_CHECKSUM:
87         case IAVF_ERR_PHY:
88         case IAVF_ERR_CONFIG:
89         case IAVF_ERR_UNKNOWN_PHY:
90         case IAVF_ERR_LINK_SETUP:
91         case IAVF_ERR_ADAPTER_STOPPED:
92         case IAVF_ERR_PRIMARY_REQUESTS_PENDING:
93         case IAVF_ERR_AUTONEG_NOT_COMPLETE:
94         case IAVF_ERR_RESET_FAILED:
95         case IAVF_ERR_BAD_PTR:
96         case IAVF_ERR_SWFW_SYNC:
97         case IAVF_ERR_QP_TOOMANY_WRS_POSTED:
98         case IAVF_ERR_QUEUE_EMPTY:
99         case IAVF_ERR_FLUSHED_QUEUE:
100         case IAVF_ERR_OPCODE_MISMATCH:
101         case IAVF_ERR_CQP_COMPL_ERROR:
102         case IAVF_ERR_BACKING_PAGE_ERROR:
103         case IAVF_ERR_NO_PBLCHUNKS_AVAILABLE:
104         case IAVF_ERR_MEMCPY_FAILED:
105         case IAVF_ERR_SRQ_ENABLED:
106         case IAVF_ERR_ADMIN_QUEUE_ERROR:
107         case IAVF_ERR_ADMIN_QUEUE_FULL:
108         case IAVF_ERR_BAD_RDMA_CQE:
109         case IAVF_ERR_NVM_BLANK_MODE:
110         case IAVF_ERR_PE_DOORBELL_NOT_ENABLED:
111         case IAVF_ERR_DIAG_TEST_FAILED:
112         case IAVF_ERR_FIRMWARE_API_VERSION:
113         case IAVF_ERR_ADMIN_QUEUE_CRITICAL_ERROR:
114                 return -EIO;
115         case IAVF_ERR_DEVICE_NOT_SUPPORTED:
116                 return -ENODEV;
117         case IAVF_ERR_NO_AVAILABLE_VSI:
118         case IAVF_ERR_RING_FULL:
119                 return -ENOSPC;
120         case IAVF_ERR_NO_MEMORY:
121                 return -ENOMEM;
122         case IAVF_ERR_TIMEOUT:
123         case IAVF_ERR_ADMIN_QUEUE_TIMEOUT:
124                 return -ETIMEDOUT;
125         case IAVF_ERR_NOT_IMPLEMENTED:
126         case IAVF_NOT_SUPPORTED:
127                 return -EOPNOTSUPP;
128         case IAVF_ERR_ADMIN_QUEUE_NO_WORK:
129                 return -EALREADY;
130         case IAVF_ERR_NOT_READY:
131                 return -EBUSY;
132         case IAVF_ERR_BUF_TOO_SHORT:
133                 return -EMSGSIZE;
134         }
135
136         return -EIO;
137 }
138
139 int virtchnl_status_to_errno(enum virtchnl_status_code v_status)
140 {
141         switch (v_status) {
142         case VIRTCHNL_STATUS_SUCCESS:
143                 return 0;
144         case VIRTCHNL_STATUS_ERR_PARAM:
145         case VIRTCHNL_STATUS_ERR_INVALID_VF_ID:
146                 return -EINVAL;
147         case VIRTCHNL_STATUS_ERR_NO_MEMORY:
148                 return -ENOMEM;
149         case VIRTCHNL_STATUS_ERR_OPCODE_MISMATCH:
150         case VIRTCHNL_STATUS_ERR_CQP_COMPL_ERROR:
151         case VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR:
152                 return -EIO;
153         case VIRTCHNL_STATUS_ERR_NOT_SUPPORTED:
154                 return -EOPNOTSUPP;
155         }
156
157         return -EIO;
158 }
159
160 /**
161  * iavf_pdev_to_adapter - go from pci_dev to adapter
162  * @pdev: pci_dev pointer
163  */
164 static struct iavf_adapter *iavf_pdev_to_adapter(struct pci_dev *pdev)
165 {
166         return netdev_priv(pci_get_drvdata(pdev));
167 }
168
169 /**
170  * iavf_is_reset_in_progress - Check if a reset is in progress
171  * @adapter: board private structure
172  */
173 static bool iavf_is_reset_in_progress(struct iavf_adapter *adapter)
174 {
175         if (adapter->state == __IAVF_RESETTING ||
176             adapter->flags & (IAVF_FLAG_RESET_PENDING |
177                               IAVF_FLAG_RESET_NEEDED))
178                 return true;
179
180         return false;
181 }
182
183 /**
184  * iavf_wait_for_reset - Wait for reset to finish.
185  * @adapter: board private structure
186  *
187  * Returns 0 if reset finished successfully, negative on timeout or interrupt.
188  */
189 int iavf_wait_for_reset(struct iavf_adapter *adapter)
190 {
191         int ret = wait_event_interruptible_timeout(adapter->reset_waitqueue,
192                                         !iavf_is_reset_in_progress(adapter),
193                                         msecs_to_jiffies(5000));
194
195         /* If ret < 0 then it means wait was interrupted.
196          * If ret == 0 then it means we got a timeout while waiting
197          * for reset to finish.
198          * If ret > 0 it means reset has finished.
199          */
200         if (ret > 0)
201                 return 0;
202         else if (ret < 0)
203                 return -EINTR;
204         else
205                 return -EBUSY;
206 }
207
208 /**
209  * iavf_allocate_dma_mem_d - OS specific memory alloc for shared code
210  * @hw:   pointer to the HW structure
211  * @mem:  ptr to mem struct to fill out
212  * @size: size of memory requested
213  * @alignment: what to align the allocation to
214  **/
215 enum iavf_status iavf_allocate_dma_mem_d(struct iavf_hw *hw,
216                                          struct iavf_dma_mem *mem,
217                                          u64 size, u32 alignment)
218 {
219         struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
220
221         if (!mem)
222                 return IAVF_ERR_PARAM;
223
224         mem->size = ALIGN(size, alignment);
225         mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
226                                      (dma_addr_t *)&mem->pa, GFP_KERNEL);
227         if (mem->va)
228                 return 0;
229         else
230                 return IAVF_ERR_NO_MEMORY;
231 }
232
233 /**
234  * iavf_free_dma_mem - wrapper for DMA memory freeing
235  * @hw:   pointer to the HW structure
236  * @mem:  ptr to mem struct to free
237  **/
238 enum iavf_status iavf_free_dma_mem(struct iavf_hw *hw, struct iavf_dma_mem *mem)
239 {
240         struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
241
242         if (!mem || !mem->va)
243                 return IAVF_ERR_PARAM;
244         dma_free_coherent(&adapter->pdev->dev, mem->size,
245                           mem->va, (dma_addr_t)mem->pa);
246         return 0;
247 }
248
249 /**
250  * iavf_allocate_virt_mem - virt memory alloc wrapper
251  * @hw:   pointer to the HW structure
252  * @mem:  ptr to mem struct to fill out
253  * @size: size of memory requested
254  **/
255 enum iavf_status iavf_allocate_virt_mem(struct iavf_hw *hw,
256                                         struct iavf_virt_mem *mem, u32 size)
257 {
258         if (!mem)
259                 return IAVF_ERR_PARAM;
260
261         mem->size = size;
262         mem->va = kzalloc(size, GFP_KERNEL);
263
264         if (mem->va)
265                 return 0;
266         else
267                 return IAVF_ERR_NO_MEMORY;
268 }
269
270 /**
271  * iavf_free_virt_mem - virt memory free wrapper
272  * @hw:   pointer to the HW structure
273  * @mem:  ptr to mem struct to free
274  **/
275 void iavf_free_virt_mem(struct iavf_hw *hw, struct iavf_virt_mem *mem)
276 {
277         kfree(mem->va);
278 }
279
280 /**
281  * iavf_lock_timeout - try to lock mutex but give up after timeout
282  * @lock: mutex that should be locked
283  * @msecs: timeout in msecs
284  *
285  * Returns 0 on success, negative on failure
286  **/
287 static int iavf_lock_timeout(struct mutex *lock, unsigned int msecs)
288 {
289         unsigned int wait, delay = 10;
290
291         for (wait = 0; wait < msecs; wait += delay) {
292                 if (mutex_trylock(lock))
293                         return 0;
294
295                 msleep(delay);
296         }
297
298         return -1;
299 }
300
301 /**
302  * iavf_schedule_reset - Set the flags and schedule a reset event
303  * @adapter: board private structure
304  * @flags: IAVF_FLAG_RESET_PENDING or IAVF_FLAG_RESET_NEEDED
305  **/
306 void iavf_schedule_reset(struct iavf_adapter *adapter, u64 flags)
307 {
308         if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section) &&
309             !(adapter->flags &
310             (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
311                 adapter->flags |= flags;
312                 queue_work(adapter->wq, &adapter->reset_task);
313         }
314 }
315
316 /**
317  * iavf_schedule_request_stats - Set the flags and schedule statistics request
318  * @adapter: board private structure
319  *
320  * Sets IAVF_FLAG_AQ_REQUEST_STATS flag so iavf_watchdog_task() will explicitly
321  * request and refresh ethtool stats
322  **/
323 void iavf_schedule_request_stats(struct iavf_adapter *adapter)
324 {
325         adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_STATS;
326         mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
327 }
328
329 /**
330  * iavf_tx_timeout - Respond to a Tx Hang
331  * @netdev: network interface device structure
332  * @txqueue: queue number that is timing out
333  **/
334 static void iavf_tx_timeout(struct net_device *netdev, unsigned int txqueue)
335 {
336         struct iavf_adapter *adapter = netdev_priv(netdev);
337
338         adapter->tx_timeout_count++;
339         iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
340 }
341
342 /**
343  * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
344  * @adapter: board private structure
345  **/
346 static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
347 {
348         struct iavf_hw *hw = &adapter->hw;
349
350         if (!adapter->msix_entries)
351                 return;
352
353         wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
354
355         iavf_flush(hw);
356
357         synchronize_irq(adapter->msix_entries[0].vector);
358 }
359
360 /**
361  * iavf_misc_irq_enable - Enable default interrupt generation settings
362  * @adapter: board private structure
363  **/
364 static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
365 {
366         struct iavf_hw *hw = &adapter->hw;
367
368         wr32(hw, IAVF_VFINT_DYN_CTL01, IAVF_VFINT_DYN_CTL01_INTENA_MASK |
369                                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
370         wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
371
372         iavf_flush(hw);
373 }
374
375 /**
376  * iavf_irq_disable - Mask off interrupt generation on the NIC
377  * @adapter: board private structure
378  **/
379 static void iavf_irq_disable(struct iavf_adapter *adapter)
380 {
381         int i;
382         struct iavf_hw *hw = &adapter->hw;
383
384         if (!adapter->msix_entries)
385                 return;
386
387         for (i = 1; i < adapter->num_msix_vectors; i++) {
388                 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1), 0);
389                 synchronize_irq(adapter->msix_entries[i].vector);
390         }
391         iavf_flush(hw);
392 }
393
394 /**
395  * iavf_irq_enable_queues - Enable interrupt for all queues
396  * @adapter: board private structure
397  **/
398 static void iavf_irq_enable_queues(struct iavf_adapter *adapter)
399 {
400         struct iavf_hw *hw = &adapter->hw;
401         int i;
402
403         for (i = 1; i < adapter->num_msix_vectors; i++) {
404                 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1),
405                      IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
406                      IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
407         }
408 }
409
410 /**
411  * iavf_irq_enable - Enable default interrupt generation settings
412  * @adapter: board private structure
413  * @flush: boolean value whether to run rd32()
414  **/
415 void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
416 {
417         struct iavf_hw *hw = &adapter->hw;
418
419         iavf_misc_irq_enable(adapter);
420         iavf_irq_enable_queues(adapter);
421
422         if (flush)
423                 iavf_flush(hw);
424 }
425
426 /**
427  * iavf_msix_aq - Interrupt handler for vector 0
428  * @irq: interrupt number
429  * @data: pointer to netdev
430  **/
431 static irqreturn_t iavf_msix_aq(int irq, void *data)
432 {
433         struct net_device *netdev = data;
434         struct iavf_adapter *adapter = netdev_priv(netdev);
435         struct iavf_hw *hw = &adapter->hw;
436
437         /* handle non-queue interrupts, these reads clear the registers */
438         rd32(hw, IAVF_VFINT_ICR01);
439         rd32(hw, IAVF_VFINT_ICR0_ENA1);
440
441         if (adapter->state != __IAVF_REMOVE)
442                 /* schedule work on the private workqueue */
443                 queue_work(adapter->wq, &adapter->adminq_task);
444
445         return IRQ_HANDLED;
446 }
447
448 /**
449  * iavf_msix_clean_rings - MSIX mode Interrupt Handler
450  * @irq: interrupt number
451  * @data: pointer to a q_vector
452  **/
453 static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
454 {
455         struct iavf_q_vector *q_vector = data;
456
457         if (!q_vector->tx.ring && !q_vector->rx.ring)
458                 return IRQ_HANDLED;
459
460         napi_schedule_irqoff(&q_vector->napi);
461
462         return IRQ_HANDLED;
463 }
464
465 /**
466  * iavf_map_vector_to_rxq - associate irqs with rx queues
467  * @adapter: board private structure
468  * @v_idx: interrupt number
469  * @r_idx: queue number
470  **/
471 static void
472 iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
473 {
474         struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
475         struct iavf_ring *rx_ring = &adapter->rx_rings[r_idx];
476         struct iavf_hw *hw = &adapter->hw;
477
478         rx_ring->q_vector = q_vector;
479         rx_ring->next = q_vector->rx.ring;
480         rx_ring->vsi = &adapter->vsi;
481         q_vector->rx.ring = rx_ring;
482         q_vector->rx.count++;
483         q_vector->rx.next_update = jiffies + 1;
484         q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
485         q_vector->ring_mask |= BIT(r_idx);
486         wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, q_vector->reg_idx),
487              q_vector->rx.current_itr >> 1);
488         q_vector->rx.current_itr = q_vector->rx.target_itr;
489 }
490
491 /**
492  * iavf_map_vector_to_txq - associate irqs with tx queues
493  * @adapter: board private structure
494  * @v_idx: interrupt number
495  * @t_idx: queue number
496  **/
497 static void
498 iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
499 {
500         struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
501         struct iavf_ring *tx_ring = &adapter->tx_rings[t_idx];
502         struct iavf_hw *hw = &adapter->hw;
503
504         tx_ring->q_vector = q_vector;
505         tx_ring->next = q_vector->tx.ring;
506         tx_ring->vsi = &adapter->vsi;
507         q_vector->tx.ring = tx_ring;
508         q_vector->tx.count++;
509         q_vector->tx.next_update = jiffies + 1;
510         q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
511         q_vector->num_ringpairs++;
512         wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, q_vector->reg_idx),
513              q_vector->tx.target_itr >> 1);
514         q_vector->tx.current_itr = q_vector->tx.target_itr;
515 }
516
517 /**
518  * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
519  * @adapter: board private structure to initialize
520  *
521  * This function maps descriptor rings to the queue-specific vectors
522  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
523  * one vector per ring/queue, but on a constrained vector budget, we
524  * group the rings as "efficiently" as possible.  You would add new
525  * mapping configurations in here.
526  **/
527 static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
528 {
529         int rings_remaining = adapter->num_active_queues;
530         int ridx = 0, vidx = 0;
531         int q_vectors;
532
533         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
534
535         for (; ridx < rings_remaining; ridx++) {
536                 iavf_map_vector_to_rxq(adapter, vidx, ridx);
537                 iavf_map_vector_to_txq(adapter, vidx, ridx);
538
539                 /* In the case where we have more queues than vectors, continue
540                  * round-robin on vectors until all queues are mapped.
541                  */
542                 if (++vidx >= q_vectors)
543                         vidx = 0;
544         }
545
546         adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
547 }
548
549 /**
550  * iavf_irq_affinity_notify - Callback for affinity changes
551  * @notify: context as to what irq was changed
552  * @mask: the new affinity mask
553  *
554  * This is a callback function used by the irq_set_affinity_notifier function
555  * so that we may register to receive changes to the irq affinity masks.
556  **/
557 static void iavf_irq_affinity_notify(struct irq_affinity_notify *notify,
558                                      const cpumask_t *mask)
559 {
560         struct iavf_q_vector *q_vector =
561                 container_of(notify, struct iavf_q_vector, affinity_notify);
562
563         cpumask_copy(&q_vector->affinity_mask, mask);
564 }
565
566 /**
567  * iavf_irq_affinity_release - Callback for affinity notifier release
568  * @ref: internal core kernel usage
569  *
570  * This is a callback function used by the irq_set_affinity_notifier function
571  * to inform the current notification subscriber that they will no longer
572  * receive notifications.
573  **/
574 static void iavf_irq_affinity_release(struct kref *ref) {}
575
576 /**
577  * iavf_request_traffic_irqs - Initialize MSI-X interrupts
578  * @adapter: board private structure
579  * @basename: device basename
580  *
581  * Allocates MSI-X vectors for tx and rx handling, and requests
582  * interrupts from the kernel.
583  **/
584 static int
585 iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
586 {
587         unsigned int vector, q_vectors;
588         unsigned int rx_int_idx = 0, tx_int_idx = 0;
589         int irq_num, err;
590         int cpu;
591
592         iavf_irq_disable(adapter);
593         /* Decrement for Other and TCP Timer vectors */
594         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
595
596         for (vector = 0; vector < q_vectors; vector++) {
597                 struct iavf_q_vector *q_vector = &adapter->q_vectors[vector];
598
599                 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
600
601                 if (q_vector->tx.ring && q_vector->rx.ring) {
602                         snprintf(q_vector->name, sizeof(q_vector->name),
603                                  "iavf-%s-TxRx-%u", basename, rx_int_idx++);
604                         tx_int_idx++;
605                 } else if (q_vector->rx.ring) {
606                         snprintf(q_vector->name, sizeof(q_vector->name),
607                                  "iavf-%s-rx-%u", basename, rx_int_idx++);
608                 } else if (q_vector->tx.ring) {
609                         snprintf(q_vector->name, sizeof(q_vector->name),
610                                  "iavf-%s-tx-%u", basename, tx_int_idx++);
611                 } else {
612                         /* skip this unused q_vector */
613                         continue;
614                 }
615                 err = request_irq(irq_num,
616                                   iavf_msix_clean_rings,
617                                   0,
618                                   q_vector->name,
619                                   q_vector);
620                 if (err) {
621                         dev_info(&adapter->pdev->dev,
622                                  "Request_irq failed, error: %d\n", err);
623                         goto free_queue_irqs;
624                 }
625                 /* register for affinity change notifications */
626                 q_vector->affinity_notify.notify = iavf_irq_affinity_notify;
627                 q_vector->affinity_notify.release =
628                                                    iavf_irq_affinity_release;
629                 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
630                 /* Spread the IRQ affinity hints across online CPUs. Note that
631                  * get_cpu_mask returns a mask with a permanent lifetime so
632                  * it's safe to use as a hint for irq_update_affinity_hint.
633                  */
634                 cpu = cpumask_local_spread(q_vector->v_idx, -1);
635                 irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
636         }
637
638         return 0;
639
640 free_queue_irqs:
641         while (vector) {
642                 vector--;
643                 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
644                 irq_set_affinity_notifier(irq_num, NULL);
645                 irq_update_affinity_hint(irq_num, NULL);
646                 free_irq(irq_num, &adapter->q_vectors[vector]);
647         }
648         return err;
649 }
650
651 /**
652  * iavf_request_misc_irq - Initialize MSI-X interrupts
653  * @adapter: board private structure
654  *
655  * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
656  * vector is only for the admin queue, and stays active even when the netdev
657  * is closed.
658  **/
659 static int iavf_request_misc_irq(struct iavf_adapter *adapter)
660 {
661         struct net_device *netdev = adapter->netdev;
662         int err;
663
664         snprintf(adapter->misc_vector_name,
665                  sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
666                  dev_name(&adapter->pdev->dev));
667         err = request_irq(adapter->msix_entries[0].vector,
668                           &iavf_msix_aq, 0,
669                           adapter->misc_vector_name, netdev);
670         if (err) {
671                 dev_err(&adapter->pdev->dev,
672                         "request_irq for %s failed: %d\n",
673                         adapter->misc_vector_name, err);
674                 free_irq(adapter->msix_entries[0].vector, netdev);
675         }
676         return err;
677 }
678
679 /**
680  * iavf_free_traffic_irqs - Free MSI-X interrupts
681  * @adapter: board private structure
682  *
683  * Frees all MSI-X vectors other than 0.
684  **/
685 static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
686 {
687         int vector, irq_num, q_vectors;
688
689         if (!adapter->msix_entries)
690                 return;
691
692         q_vectors = adapter->num_msix_vectors - NONQ_VECS;
693
694         for (vector = 0; vector < q_vectors; vector++) {
695                 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
696                 irq_set_affinity_notifier(irq_num, NULL);
697                 irq_update_affinity_hint(irq_num, NULL);
698                 free_irq(irq_num, &adapter->q_vectors[vector]);
699         }
700 }
701
702 /**
703  * iavf_free_misc_irq - Free MSI-X miscellaneous vector
704  * @adapter: board private structure
705  *
706  * Frees MSI-X vector 0.
707  **/
708 static void iavf_free_misc_irq(struct iavf_adapter *adapter)
709 {
710         struct net_device *netdev = adapter->netdev;
711
712         if (!adapter->msix_entries)
713                 return;
714
715         free_irq(adapter->msix_entries[0].vector, netdev);
716 }
717
718 /**
719  * iavf_configure_tx - Configure Transmit Unit after Reset
720  * @adapter: board private structure
721  *
722  * Configure the Tx unit of the MAC after a reset.
723  **/
724 static void iavf_configure_tx(struct iavf_adapter *adapter)
725 {
726         struct iavf_hw *hw = &adapter->hw;
727         int i;
728
729         for (i = 0; i < adapter->num_active_queues; i++)
730                 adapter->tx_rings[i].tail = hw->hw_addr + IAVF_QTX_TAIL1(i);
731 }
732
733 /**
734  * iavf_configure_rx - Configure Receive Unit after Reset
735  * @adapter: board private structure
736  *
737  * Configure the Rx unit of the MAC after a reset.
738  **/
739 static void iavf_configure_rx(struct iavf_adapter *adapter)
740 {
741         unsigned int rx_buf_len = IAVF_RXBUFFER_2048;
742         struct iavf_hw *hw = &adapter->hw;
743         int i;
744
745         /* Legacy Rx will always default to a 2048 buffer size. */
746 #if (PAGE_SIZE < 8192)
747         if (!(adapter->flags & IAVF_FLAG_LEGACY_RX)) {
748                 struct net_device *netdev = adapter->netdev;
749
750                 /* For jumbo frames on systems with 4K pages we have to use
751                  * an order 1 page, so we might as well increase the size
752                  * of our Rx buffer to make better use of the available space
753                  */
754                 rx_buf_len = IAVF_RXBUFFER_3072;
755
756                 /* We use a 1536 buffer size for configurations with
757                  * standard Ethernet mtu.  On x86 this gives us enough room
758                  * for shared info and 192 bytes of padding.
759                  */
760                 if (!IAVF_2K_TOO_SMALL_WITH_PADDING &&
761                     (netdev->mtu <= ETH_DATA_LEN))
762                         rx_buf_len = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
763         }
764 #endif
765
766         for (i = 0; i < adapter->num_active_queues; i++) {
767                 adapter->rx_rings[i].tail = hw->hw_addr + IAVF_QRX_TAIL1(i);
768                 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
769
770                 if (adapter->flags & IAVF_FLAG_LEGACY_RX)
771                         clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
772                 else
773                         set_ring_build_skb_enabled(&adapter->rx_rings[i]);
774         }
775 }
776
777 /**
778  * iavf_find_vlan - Search filter list for specific vlan filter
779  * @adapter: board private structure
780  * @vlan: vlan tag
781  *
782  * Returns ptr to the filter object or NULL. Must be called while holding the
783  * mac_vlan_list_lock.
784  **/
785 static struct
786 iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter,
787                                  struct iavf_vlan vlan)
788 {
789         struct iavf_vlan_filter *f;
790
791         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
792                 if (f->vlan.vid == vlan.vid &&
793                     f->vlan.tpid == vlan.tpid)
794                         return f;
795         }
796
797         return NULL;
798 }
799
800 /**
801  * iavf_add_vlan - Add a vlan filter to the list
802  * @adapter: board private structure
803  * @vlan: VLAN tag
804  *
805  * Returns ptr to the filter object or NULL when no memory available.
806  **/
807 static struct
808 iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter,
809                                 struct iavf_vlan vlan)
810 {
811         struct iavf_vlan_filter *f = NULL;
812
813         spin_lock_bh(&adapter->mac_vlan_list_lock);
814
815         f = iavf_find_vlan(adapter, vlan);
816         if (!f) {
817                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
818                 if (!f)
819                         goto clearout;
820
821                 f->vlan = vlan;
822
823                 list_add_tail(&f->list, &adapter->vlan_filter_list);
824                 f->state = IAVF_VLAN_ADD;
825                 adapter->num_vlan_filters++;
826                 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
827         }
828
829 clearout:
830         spin_unlock_bh(&adapter->mac_vlan_list_lock);
831         return f;
832 }
833
834 /**
835  * iavf_del_vlan - Remove a vlan filter from the list
836  * @adapter: board private structure
837  * @vlan: VLAN tag
838  **/
839 static void iavf_del_vlan(struct iavf_adapter *adapter, struct iavf_vlan vlan)
840 {
841         struct iavf_vlan_filter *f;
842
843         spin_lock_bh(&adapter->mac_vlan_list_lock);
844
845         f = iavf_find_vlan(adapter, vlan);
846         if (f) {
847                 f->state = IAVF_VLAN_REMOVE;
848                 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
849         }
850
851         spin_unlock_bh(&adapter->mac_vlan_list_lock);
852 }
853
854 /**
855  * iavf_restore_filters
856  * @adapter: board private structure
857  *
858  * Restore existing non MAC filters when VF netdev comes back up
859  **/
860 static void iavf_restore_filters(struct iavf_adapter *adapter)
861 {
862         struct iavf_vlan_filter *f;
863
864         /* re-add all VLAN filters */
865         spin_lock_bh(&adapter->mac_vlan_list_lock);
866
867         list_for_each_entry(f, &adapter->vlan_filter_list, list) {
868                 if (f->state == IAVF_VLAN_INACTIVE)
869                         f->state = IAVF_VLAN_ADD;
870         }
871
872         spin_unlock_bh(&adapter->mac_vlan_list_lock);
873         adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
874 }
875
876 /**
877  * iavf_get_num_vlans_added - get number of VLANs added
878  * @adapter: board private structure
879  */
880 u16 iavf_get_num_vlans_added(struct iavf_adapter *adapter)
881 {
882         return adapter->num_vlan_filters;
883 }
884
885 /**
886  * iavf_get_max_vlans_allowed - get maximum VLANs allowed for this VF
887  * @adapter: board private structure
888  *
889  * This depends on the negotiated VLAN capability. For VIRTCHNL_VF_OFFLOAD_VLAN,
890  * do not impose a limit as that maintains current behavior and for
891  * VIRTCHNL_VF_OFFLOAD_VLAN_V2, use the maximum allowed sent from the PF.
892  **/
893 static u16 iavf_get_max_vlans_allowed(struct iavf_adapter *adapter)
894 {
895         /* don't impose any limit for VIRTCHNL_VF_OFFLOAD_VLAN since there has
896          * never been a limit on the VF driver side
897          */
898         if (VLAN_ALLOWED(adapter))
899                 return VLAN_N_VID;
900         else if (VLAN_V2_ALLOWED(adapter))
901                 return adapter->vlan_v2_caps.filtering.max_filters;
902
903         return 0;
904 }
905
906 /**
907  * iavf_max_vlans_added - check if maximum VLANs allowed already exist
908  * @adapter: board private structure
909  **/
910 static bool iavf_max_vlans_added(struct iavf_adapter *adapter)
911 {
912         if (iavf_get_num_vlans_added(adapter) <
913             iavf_get_max_vlans_allowed(adapter))
914                 return false;
915
916         return true;
917 }
918
919 /**
920  * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
921  * @netdev: network device struct
922  * @proto: unused protocol data
923  * @vid: VLAN tag
924  **/
925 static int iavf_vlan_rx_add_vid(struct net_device *netdev,
926                                 __always_unused __be16 proto, u16 vid)
927 {
928         struct iavf_adapter *adapter = netdev_priv(netdev);
929
930         /* Do not track VLAN 0 filter, always added by the PF on VF init */
931         if (!vid)
932                 return 0;
933
934         if (!VLAN_FILTERING_ALLOWED(adapter))
935                 return -EIO;
936
937         if (iavf_max_vlans_added(adapter)) {
938                 netdev_err(netdev, "Max allowed VLAN filters %u. Remove existing VLANs or disable filtering via Ethtool if supported.\n",
939                            iavf_get_max_vlans_allowed(adapter));
940                 return -EIO;
941         }
942
943         if (!iavf_add_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto))))
944                 return -ENOMEM;
945
946         return 0;
947 }
948
949 /**
950  * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
951  * @netdev: network device struct
952  * @proto: unused protocol data
953  * @vid: VLAN tag
954  **/
955 static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
956                                  __always_unused __be16 proto, u16 vid)
957 {
958         struct iavf_adapter *adapter = netdev_priv(netdev);
959
960         /* We do not track VLAN 0 filter */
961         if (!vid)
962                 return 0;
963
964         iavf_del_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto)));
965         return 0;
966 }
967
968 /**
969  * iavf_find_filter - Search filter list for specific mac filter
970  * @adapter: board private structure
971  * @macaddr: the MAC address
972  *
973  * Returns ptr to the filter object or NULL. Must be called while holding the
974  * mac_vlan_list_lock.
975  **/
976 static struct
977 iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
978                                   const u8 *macaddr)
979 {
980         struct iavf_mac_filter *f;
981
982         if (!macaddr)
983                 return NULL;
984
985         list_for_each_entry(f, &adapter->mac_filter_list, list) {
986                 if (ether_addr_equal(macaddr, f->macaddr))
987                         return f;
988         }
989         return NULL;
990 }
991
992 /**
993  * iavf_add_filter - Add a mac filter to the filter list
994  * @adapter: board private structure
995  * @macaddr: the MAC address
996  *
997  * Returns ptr to the filter object or NULL when no memory available.
998  **/
999 struct iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
1000                                         const u8 *macaddr)
1001 {
1002         struct iavf_mac_filter *f;
1003
1004         if (!macaddr)
1005                 return NULL;
1006
1007         f = iavf_find_filter(adapter, macaddr);
1008         if (!f) {
1009                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1010                 if (!f)
1011                         return f;
1012
1013                 ether_addr_copy(f->macaddr, macaddr);
1014
1015                 list_add_tail(&f->list, &adapter->mac_filter_list);
1016                 f->add = true;
1017                 f->add_handled = false;
1018                 f->is_new_mac = true;
1019                 f->is_primary = ether_addr_equal(macaddr, adapter->hw.mac.addr);
1020                 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
1021         } else {
1022                 f->remove = false;
1023         }
1024
1025         return f;
1026 }
1027
1028 /**
1029  * iavf_replace_primary_mac - Replace current primary address
1030  * @adapter: board private structure
1031  * @new_mac: new MAC address to be applied
1032  *
1033  * Replace current dev_addr and send request to PF for removal of previous
1034  * primary MAC address filter and addition of new primary MAC filter.
1035  * Return 0 for success, -ENOMEM for failure.
1036  *
1037  * Do not call this with mac_vlan_list_lock!
1038  **/
1039 static int iavf_replace_primary_mac(struct iavf_adapter *adapter,
1040                                     const u8 *new_mac)
1041 {
1042         struct iavf_hw *hw = &adapter->hw;
1043         struct iavf_mac_filter *new_f;
1044         struct iavf_mac_filter *old_f;
1045
1046         spin_lock_bh(&adapter->mac_vlan_list_lock);
1047
1048         new_f = iavf_add_filter(adapter, new_mac);
1049         if (!new_f) {
1050                 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1051                 return -ENOMEM;
1052         }
1053
1054         old_f = iavf_find_filter(adapter, hw->mac.addr);
1055         if (old_f) {
1056                 old_f->is_primary = false;
1057                 old_f->remove = true;
1058                 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1059         }
1060         /* Always send the request to add if changing primary MAC,
1061          * even if filter is already present on the list
1062          */
1063         new_f->is_primary = true;
1064         new_f->add = true;
1065         adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
1066         ether_addr_copy(hw->mac.addr, new_mac);
1067
1068         spin_unlock_bh(&adapter->mac_vlan_list_lock);
1069
1070         /* schedule the watchdog task to immediately process the request */
1071         mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
1072         return 0;
1073 }
1074
1075 /**
1076  * iavf_is_mac_set_handled - wait for a response to set MAC from PF
1077  * @netdev: network interface device structure
1078  * @macaddr: MAC address to set
1079  *
1080  * Returns true on success, false on failure
1081  */
1082 static bool iavf_is_mac_set_handled(struct net_device *netdev,
1083                                     const u8 *macaddr)
1084 {
1085         struct iavf_adapter *adapter = netdev_priv(netdev);
1086         struct iavf_mac_filter *f;
1087         bool ret = false;
1088
1089         spin_lock_bh(&adapter->mac_vlan_list_lock);
1090
1091         f = iavf_find_filter(adapter, macaddr);
1092
1093         if (!f || (!f->add && f->add_handled))
1094                 ret = true;
1095
1096         spin_unlock_bh(&adapter->mac_vlan_list_lock);
1097
1098         return ret;
1099 }
1100
1101 /**
1102  * iavf_set_mac - NDO callback to set port MAC address
1103  * @netdev: network interface device structure
1104  * @p: pointer to an address structure
1105  *
1106  * Returns 0 on success, negative on failure
1107  */
1108 static int iavf_set_mac(struct net_device *netdev, void *p)
1109 {
1110         struct iavf_adapter *adapter = netdev_priv(netdev);
1111         struct sockaddr *addr = p;
1112         int ret;
1113
1114         if (!is_valid_ether_addr(addr->sa_data))
1115                 return -EADDRNOTAVAIL;
1116
1117         ret = iavf_replace_primary_mac(adapter, addr->sa_data);
1118
1119         if (ret)
1120                 return ret;
1121
1122         ret = wait_event_interruptible_timeout(adapter->vc_waitqueue,
1123                                                iavf_is_mac_set_handled(netdev, addr->sa_data),
1124                                                msecs_to_jiffies(2500));
1125
1126         /* If ret < 0 then it means wait was interrupted.
1127          * If ret == 0 then it means we got a timeout.
1128          * else it means we got response for set MAC from PF,
1129          * check if netdev MAC was updated to requested MAC,
1130          * if yes then set MAC succeeded otherwise it failed return -EACCES
1131          */
1132         if (ret < 0)
1133                 return ret;
1134
1135         if (!ret)
1136                 return -EAGAIN;
1137
1138         if (!ether_addr_equal(netdev->dev_addr, addr->sa_data))
1139                 return -EACCES;
1140
1141         return 0;
1142 }
1143
1144 /**
1145  * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
1146  * @netdev: the netdevice
1147  * @addr: address to add
1148  *
1149  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1150  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1151  */
1152 static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
1153 {
1154         struct iavf_adapter *adapter = netdev_priv(netdev);
1155
1156         if (iavf_add_filter(adapter, addr))
1157                 return 0;
1158         else
1159                 return -ENOMEM;
1160 }
1161
1162 /**
1163  * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1164  * @netdev: the netdevice
1165  * @addr: address to add
1166  *
1167  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1168  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1169  */
1170 static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
1171 {
1172         struct iavf_adapter *adapter = netdev_priv(netdev);
1173         struct iavf_mac_filter *f;
1174
1175         /* Under some circumstances, we might receive a request to delete
1176          * our own device address from our uc list. Because we store the
1177          * device address in the VSI's MAC/VLAN filter list, we need to ignore
1178          * such requests and not delete our device address from this list.
1179          */
1180         if (ether_addr_equal(addr, netdev->dev_addr))
1181                 return 0;
1182
1183         f = iavf_find_filter(adapter, addr);
1184         if (f) {
1185                 f->remove = true;
1186                 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1187         }
1188         return 0;
1189 }
1190
1191 /**
1192  * iavf_set_rx_mode - NDO callback to set the netdev filters
1193  * @netdev: network interface device structure
1194  **/
1195 static void iavf_set_rx_mode(struct net_device *netdev)
1196 {
1197         struct iavf_adapter *adapter = netdev_priv(netdev);
1198
1199         spin_lock_bh(&adapter->mac_vlan_list_lock);
1200         __dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1201         __dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1202         spin_unlock_bh(&adapter->mac_vlan_list_lock);
1203
1204         if (netdev->flags & IFF_PROMISC &&
1205             !(adapter->flags & IAVF_FLAG_PROMISC_ON))
1206                 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_PROMISC;
1207         else if (!(netdev->flags & IFF_PROMISC) &&
1208                  adapter->flags & IAVF_FLAG_PROMISC_ON)
1209                 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_PROMISC;
1210
1211         if (netdev->flags & IFF_ALLMULTI &&
1212             !(adapter->flags & IAVF_FLAG_ALLMULTI_ON))
1213                 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_ALLMULTI;
1214         else if (!(netdev->flags & IFF_ALLMULTI) &&
1215                  adapter->flags & IAVF_FLAG_ALLMULTI_ON)
1216                 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_ALLMULTI;
1217 }
1218
1219 /**
1220  * iavf_napi_enable_all - enable NAPI on all queue vectors
1221  * @adapter: board private structure
1222  **/
1223 static void iavf_napi_enable_all(struct iavf_adapter *adapter)
1224 {
1225         int q_idx;
1226         struct iavf_q_vector *q_vector;
1227         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1228
1229         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1230                 struct napi_struct *napi;
1231
1232                 q_vector = &adapter->q_vectors[q_idx];
1233                 napi = &q_vector->napi;
1234                 napi_enable(napi);
1235         }
1236 }
1237
1238 /**
1239  * iavf_napi_disable_all - disable NAPI on all queue vectors
1240  * @adapter: board private structure
1241  **/
1242 static void iavf_napi_disable_all(struct iavf_adapter *adapter)
1243 {
1244         int q_idx;
1245         struct iavf_q_vector *q_vector;
1246         int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1247
1248         for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1249                 q_vector = &adapter->q_vectors[q_idx];
1250                 napi_disable(&q_vector->napi);
1251         }
1252 }
1253
1254 /**
1255  * iavf_configure - set up transmit and receive data structures
1256  * @adapter: board private structure
1257  **/
1258 static void iavf_configure(struct iavf_adapter *adapter)
1259 {
1260         struct net_device *netdev = adapter->netdev;
1261         int i;
1262
1263         iavf_set_rx_mode(netdev);
1264
1265         iavf_configure_tx(adapter);
1266         iavf_configure_rx(adapter);
1267         adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
1268
1269         for (i = 0; i < adapter->num_active_queues; i++) {
1270                 struct iavf_ring *ring = &adapter->rx_rings[i];
1271
1272                 iavf_alloc_rx_buffers(ring, IAVF_DESC_UNUSED(ring));
1273         }
1274 }
1275
1276 /**
1277  * iavf_up_complete - Finish the last steps of bringing up a connection
1278  * @adapter: board private structure
1279  *
1280  * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
1281  **/
1282 static void iavf_up_complete(struct iavf_adapter *adapter)
1283 {
1284         iavf_change_state(adapter, __IAVF_RUNNING);
1285         clear_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1286
1287         iavf_napi_enable_all(adapter);
1288
1289         adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_QUEUES;
1290         if (CLIENT_ENABLED(adapter))
1291                 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_OPEN;
1292         mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
1293 }
1294
1295 /**
1296  * iavf_clear_mac_vlan_filters - Remove mac and vlan filters not sent to PF
1297  * yet and mark other to be removed.
1298  * @adapter: board private structure
1299  **/
1300 static void iavf_clear_mac_vlan_filters(struct iavf_adapter *adapter)
1301 {
1302         struct iavf_vlan_filter *vlf, *vlftmp;
1303         struct iavf_mac_filter *f, *ftmp;
1304
1305         spin_lock_bh(&adapter->mac_vlan_list_lock);
1306         /* clear the sync flag on all filters */
1307         __dev_uc_unsync(adapter->netdev, NULL);
1308         __dev_mc_unsync(adapter->netdev, NULL);
1309
1310         /* remove all MAC filters */
1311         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1312                                  list) {
1313                 if (f->add) {
1314                         list_del(&f->list);
1315                         kfree(f);
1316                 } else {
1317                         f->remove = true;
1318                 }
1319         }
1320
1321         /* disable all VLAN filters */
1322         list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
1323                                  list)
1324                 vlf->state = IAVF_VLAN_DISABLE;
1325
1326         spin_unlock_bh(&adapter->mac_vlan_list_lock);
1327 }
1328
1329 /**
1330  * iavf_clear_cloud_filters - Remove cloud filters not sent to PF yet and
1331  * mark other to be removed.
1332  * @adapter: board private structure
1333  **/
1334 static void iavf_clear_cloud_filters(struct iavf_adapter *adapter)
1335 {
1336         struct iavf_cloud_filter *cf, *cftmp;
1337
1338         /* remove all cloud filters */
1339         spin_lock_bh(&adapter->cloud_filter_list_lock);
1340         list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
1341                                  list) {
1342                 if (cf->add) {
1343                         list_del(&cf->list);
1344                         kfree(cf);
1345                         adapter->num_cloud_filters--;
1346                 } else {
1347                         cf->del = true;
1348                 }
1349         }
1350         spin_unlock_bh(&adapter->cloud_filter_list_lock);
1351 }
1352
1353 /**
1354  * iavf_clear_fdir_filters - Remove fdir filters not sent to PF yet and mark
1355  * other to be removed.
1356  * @adapter: board private structure
1357  **/
1358 static void iavf_clear_fdir_filters(struct iavf_adapter *adapter)
1359 {
1360         struct iavf_fdir_fltr *fdir, *fdirtmp;
1361
1362         /* remove all Flow Director filters */
1363         spin_lock_bh(&adapter->fdir_fltr_lock);
1364         list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head,
1365                                  list) {
1366                 if (fdir->state == IAVF_FDIR_FLTR_ADD_REQUEST) {
1367                         list_del(&fdir->list);
1368                         kfree(fdir);
1369                         adapter->fdir_active_fltr--;
1370                 } else {
1371                         fdir->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1372                 }
1373         }
1374         spin_unlock_bh(&adapter->fdir_fltr_lock);
1375 }
1376
1377 /**
1378  * iavf_clear_adv_rss_conf - Remove adv rss conf not sent to PF yet and mark
1379  * other to be removed.
1380  * @adapter: board private structure
1381  **/
1382 static void iavf_clear_adv_rss_conf(struct iavf_adapter *adapter)
1383 {
1384         struct iavf_adv_rss *rss, *rsstmp;
1385
1386         /* remove all advance RSS configuration */
1387         spin_lock_bh(&adapter->adv_rss_lock);
1388         list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
1389                                  list) {
1390                 if (rss->state == IAVF_ADV_RSS_ADD_REQUEST) {
1391                         list_del(&rss->list);
1392                         kfree(rss);
1393                 } else {
1394                         rss->state = IAVF_ADV_RSS_DEL_REQUEST;
1395                 }
1396         }
1397         spin_unlock_bh(&adapter->adv_rss_lock);
1398 }
1399
1400 /**
1401  * iavf_down - Shutdown the connection processing
1402  * @adapter: board private structure
1403  *
1404  * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
1405  **/
1406 void iavf_down(struct iavf_adapter *adapter)
1407 {
1408         struct net_device *netdev = adapter->netdev;
1409
1410         if (adapter->state <= __IAVF_DOWN_PENDING)
1411                 return;
1412
1413         netif_carrier_off(netdev);
1414         netif_tx_disable(netdev);
1415         adapter->link_up = false;
1416         iavf_napi_disable_all(adapter);
1417         iavf_irq_disable(adapter);
1418
1419         iavf_clear_mac_vlan_filters(adapter);
1420         iavf_clear_cloud_filters(adapter);
1421         iavf_clear_fdir_filters(adapter);
1422         iavf_clear_adv_rss_conf(adapter);
1423
1424         if (!(adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)) {
1425                 /* cancel any current operation */
1426                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1427                 /* Schedule operations to close down the HW. Don't wait
1428                  * here for this to complete. The watchdog is still running
1429                  * and it will take care of this.
1430                  */
1431                 if (!list_empty(&adapter->mac_filter_list))
1432                         adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1433                 if (!list_empty(&adapter->vlan_filter_list))
1434                         adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
1435                 if (!list_empty(&adapter->cloud_filter_list))
1436                         adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1437                 if (!list_empty(&adapter->fdir_list_head))
1438                         adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1439                 if (!list_empty(&adapter->adv_rss_list_head))
1440                         adapter->aq_required |= IAVF_FLAG_AQ_DEL_ADV_RSS_CFG;
1441                 adapter->aq_required |= IAVF_FLAG_AQ_DISABLE_QUEUES;
1442         }
1443
1444         mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
1445 }
1446
1447 /**
1448  * iavf_acquire_msix_vectors - Setup the MSIX capability
1449  * @adapter: board private structure
1450  * @vectors: number of vectors to request
1451  *
1452  * Work with the OS to set up the MSIX vectors needed.
1453  *
1454  * Returns 0 on success, negative on failure
1455  **/
1456 static int
1457 iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1458 {
1459         int err, vector_threshold;
1460
1461         /* We'll want at least 3 (vector_threshold):
1462          * 0) Other (Admin Queue and link, mostly)
1463          * 1) TxQ[0] Cleanup
1464          * 2) RxQ[0] Cleanup
1465          */
1466         vector_threshold = MIN_MSIX_COUNT;
1467
1468         /* The more we get, the more we will assign to Tx/Rx Cleanup
1469          * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1470          * Right now, we simply care about how many we'll get; we'll
1471          * set them up later while requesting irq's.
1472          */
1473         err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1474                                     vector_threshold, vectors);
1475         if (err < 0) {
1476                 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1477                 kfree(adapter->msix_entries);
1478                 adapter->msix_entries = NULL;
1479                 return err;
1480         }
1481
1482         /* Adjust for only the vectors we'll use, which is minimum
1483          * of max_msix_q_vectors + NONQ_VECS, or the number of
1484          * vectors we were allocated.
1485          */
1486         adapter->num_msix_vectors = err;
1487         return 0;
1488 }
1489
1490 /**
1491  * iavf_free_queues - Free memory for all rings
1492  * @adapter: board private structure to initialize
1493  *
1494  * Free all of the memory associated with queue pairs.
1495  **/
1496 static void iavf_free_queues(struct iavf_adapter *adapter)
1497 {
1498         if (!adapter->vsi_res)
1499                 return;
1500         adapter->num_active_queues = 0;
1501         kfree(adapter->tx_rings);
1502         adapter->tx_rings = NULL;
1503         kfree(adapter->rx_rings);
1504         adapter->rx_rings = NULL;
1505 }
1506
1507 /**
1508  * iavf_set_queue_vlan_tag_loc - set location for VLAN tag offload
1509  * @adapter: board private structure
1510  *
1511  * Based on negotiated capabilities, the VLAN tag needs to be inserted and/or
1512  * stripped in certain descriptor fields. Instead of checking the offload
1513  * capability bits in the hot path, cache the location the ring specific
1514  * flags.
1515  */
1516 void iavf_set_queue_vlan_tag_loc(struct iavf_adapter *adapter)
1517 {
1518         int i;
1519
1520         for (i = 0; i < adapter->num_active_queues; i++) {
1521                 struct iavf_ring *tx_ring = &adapter->tx_rings[i];
1522                 struct iavf_ring *rx_ring = &adapter->rx_rings[i];
1523
1524                 /* prevent multiple L2TAG bits being set after VFR */
1525                 tx_ring->flags &=
1526                         ~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1527                           IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2);
1528                 rx_ring->flags &=
1529                         ~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1530                           IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2);
1531
1532                 if (VLAN_ALLOWED(adapter)) {
1533                         tx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1534                         rx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1535                 } else if (VLAN_V2_ALLOWED(adapter)) {
1536                         struct virtchnl_vlan_supported_caps *stripping_support;
1537                         struct virtchnl_vlan_supported_caps *insertion_support;
1538
1539                         stripping_support =
1540                                 &adapter->vlan_v2_caps.offloads.stripping_support;
1541                         insertion_support =
1542                                 &adapter->vlan_v2_caps.offloads.insertion_support;
1543
1544                         if (stripping_support->outer) {
1545                                 if (stripping_support->outer &
1546                                     VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1547                                         rx_ring->flags |=
1548                                                 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1549                                 else if (stripping_support->outer &
1550                                          VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1551                                         rx_ring->flags |=
1552                                                 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1553                         } else if (stripping_support->inner) {
1554                                 if (stripping_support->inner &
1555                                     VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1556                                         rx_ring->flags |=
1557                                                 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1558                                 else if (stripping_support->inner &
1559                                          VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1560                                         rx_ring->flags |=
1561                                                 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1562                         }
1563
1564                         if (insertion_support->outer) {
1565                                 if (insertion_support->outer &
1566                                     VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1567                                         tx_ring->flags |=
1568                                                 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1569                                 else if (insertion_support->outer &
1570                                          VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1571                                         tx_ring->flags |=
1572                                                 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1573                         } else if (insertion_support->inner) {
1574                                 if (insertion_support->inner &
1575                                     VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1576                                         tx_ring->flags |=
1577                                                 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1578                                 else if (insertion_support->inner &
1579                                          VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1580                                         tx_ring->flags |=
1581                                                 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1582                         }
1583                 }
1584         }
1585 }
1586
1587 /**
1588  * iavf_alloc_queues - Allocate memory for all rings
1589  * @adapter: board private structure to initialize
1590  *
1591  * We allocate one ring per queue at run-time since we don't know the
1592  * number of queues at compile-time.  The polling_netdev array is
1593  * intended for Multiqueue, but should work fine with a single queue.
1594  **/
1595 static int iavf_alloc_queues(struct iavf_adapter *adapter)
1596 {
1597         int i, num_active_queues;
1598
1599         /* If we're in reset reallocating queues we don't actually know yet for
1600          * certain the PF gave us the number of queues we asked for but we'll
1601          * assume it did.  Once basic reset is finished we'll confirm once we
1602          * start negotiating config with PF.
1603          */
1604         if (adapter->num_req_queues)
1605                 num_active_queues = adapter->num_req_queues;
1606         else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1607                  adapter->num_tc)
1608                 num_active_queues = adapter->ch_config.total_qps;
1609         else
1610                 num_active_queues = min_t(int,
1611                                           adapter->vsi_res->num_queue_pairs,
1612                                           (int)(num_online_cpus()));
1613
1614
1615         adapter->tx_rings = kcalloc(num_active_queues,
1616                                     sizeof(struct iavf_ring), GFP_KERNEL);
1617         if (!adapter->tx_rings)
1618                 goto err_out;
1619         adapter->rx_rings = kcalloc(num_active_queues,
1620                                     sizeof(struct iavf_ring), GFP_KERNEL);
1621         if (!adapter->rx_rings)
1622                 goto err_out;
1623
1624         for (i = 0; i < num_active_queues; i++) {
1625                 struct iavf_ring *tx_ring;
1626                 struct iavf_ring *rx_ring;
1627
1628                 tx_ring = &adapter->tx_rings[i];
1629
1630                 tx_ring->queue_index = i;
1631                 tx_ring->netdev = adapter->netdev;
1632                 tx_ring->dev = &adapter->pdev->dev;
1633                 tx_ring->count = adapter->tx_desc_count;
1634                 tx_ring->itr_setting = IAVF_ITR_TX_DEF;
1635                 if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1636                         tx_ring->flags |= IAVF_TXR_FLAGS_WB_ON_ITR;
1637
1638                 rx_ring = &adapter->rx_rings[i];
1639                 rx_ring->queue_index = i;
1640                 rx_ring->netdev = adapter->netdev;
1641                 rx_ring->dev = &adapter->pdev->dev;
1642                 rx_ring->count = adapter->rx_desc_count;
1643                 rx_ring->itr_setting = IAVF_ITR_RX_DEF;
1644         }
1645
1646         adapter->num_active_queues = num_active_queues;
1647
1648         iavf_set_queue_vlan_tag_loc(adapter);
1649
1650         return 0;
1651
1652 err_out:
1653         iavf_free_queues(adapter);
1654         return -ENOMEM;
1655 }
1656
1657 /**
1658  * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1659  * @adapter: board private structure to initialize
1660  *
1661  * Attempt to configure the interrupts using the best available
1662  * capabilities of the hardware and the kernel.
1663  **/
1664 static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1665 {
1666         int vector, v_budget;
1667         int pairs = 0;
1668         int err = 0;
1669
1670         if (!adapter->vsi_res) {
1671                 err = -EIO;
1672                 goto out;
1673         }
1674         pairs = adapter->num_active_queues;
1675
1676         /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1677          * us much good if we have more vectors than CPUs. However, we already
1678          * limit the total number of queues by the number of CPUs so we do not
1679          * need any further limiting here.
1680          */
1681         v_budget = min_t(int, pairs + NONQ_VECS,
1682                          (int)adapter->vf_res->max_vectors);
1683
1684         adapter->msix_entries = kcalloc(v_budget,
1685                                         sizeof(struct msix_entry), GFP_KERNEL);
1686         if (!adapter->msix_entries) {
1687                 err = -ENOMEM;
1688                 goto out;
1689         }
1690
1691         for (vector = 0; vector < v_budget; vector++)
1692                 adapter->msix_entries[vector].entry = vector;
1693
1694         err = iavf_acquire_msix_vectors(adapter, v_budget);
1695         if (!err)
1696                 iavf_schedule_finish_config(adapter);
1697
1698 out:
1699         return err;
1700 }
1701
1702 /**
1703  * iavf_config_rss_aq - Configure RSS keys and lut by using AQ commands
1704  * @adapter: board private structure
1705  *
1706  * Return 0 on success, negative on failure
1707  **/
1708 static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1709 {
1710         struct iavf_aqc_get_set_rss_key_data *rss_key =
1711                 (struct iavf_aqc_get_set_rss_key_data *)adapter->rss_key;
1712         struct iavf_hw *hw = &adapter->hw;
1713         enum iavf_status status;
1714
1715         if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1716                 /* bail because we already have a command pending */
1717                 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1718                         adapter->current_op);
1719                 return -EBUSY;
1720         }
1721
1722         status = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1723         if (status) {
1724                 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1725                         iavf_stat_str(hw, status),
1726                         iavf_aq_str(hw, hw->aq.asq_last_status));
1727                 return iavf_status_to_errno(status);
1728
1729         }
1730
1731         status = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1732                                      adapter->rss_lut, adapter->rss_lut_size);
1733         if (status) {
1734                 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1735                         iavf_stat_str(hw, status),
1736                         iavf_aq_str(hw, hw->aq.asq_last_status));
1737                 return iavf_status_to_errno(status);
1738         }
1739
1740         return 0;
1741
1742 }
1743
1744 /**
1745  * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1746  * @adapter: board private structure
1747  *
1748  * Returns 0 on success, negative on failure
1749  **/
1750 static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1751 {
1752         struct iavf_hw *hw = &adapter->hw;
1753         u32 *dw;
1754         u16 i;
1755
1756         dw = (u32 *)adapter->rss_key;
1757         for (i = 0; i <= adapter->rss_key_size / 4; i++)
1758                 wr32(hw, IAVF_VFQF_HKEY(i), dw[i]);
1759
1760         dw = (u32 *)adapter->rss_lut;
1761         for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1762                 wr32(hw, IAVF_VFQF_HLUT(i), dw[i]);
1763
1764         iavf_flush(hw);
1765
1766         return 0;
1767 }
1768
1769 /**
1770  * iavf_config_rss - Configure RSS keys and lut
1771  * @adapter: board private structure
1772  *
1773  * Returns 0 on success, negative on failure
1774  **/
1775 int iavf_config_rss(struct iavf_adapter *adapter)
1776 {
1777
1778         if (RSS_PF(adapter)) {
1779                 adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1780                                         IAVF_FLAG_AQ_SET_RSS_KEY;
1781                 return 0;
1782         } else if (RSS_AQ(adapter)) {
1783                 return iavf_config_rss_aq(adapter);
1784         } else {
1785                 return iavf_config_rss_reg(adapter);
1786         }
1787 }
1788
1789 /**
1790  * iavf_fill_rss_lut - Fill the lut with default values
1791  * @adapter: board private structure
1792  **/
1793 static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1794 {
1795         u16 i;
1796
1797         for (i = 0; i < adapter->rss_lut_size; i++)
1798                 adapter->rss_lut[i] = i % adapter->num_active_queues;
1799 }
1800
1801 /**
1802  * iavf_init_rss - Prepare for RSS
1803  * @adapter: board private structure
1804  *
1805  * Return 0 on success, negative on failure
1806  **/
1807 static int iavf_init_rss(struct iavf_adapter *adapter)
1808 {
1809         struct iavf_hw *hw = &adapter->hw;
1810
1811         if (!RSS_PF(adapter)) {
1812                 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1813                 if (adapter->vf_res->vf_cap_flags &
1814                     VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1815                         adapter->hena = IAVF_DEFAULT_RSS_HENA_EXPANDED;
1816                 else
1817                         adapter->hena = IAVF_DEFAULT_RSS_HENA;
1818
1819                 wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->hena);
1820                 wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1821         }
1822
1823         iavf_fill_rss_lut(adapter);
1824         netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1825
1826         return iavf_config_rss(adapter);
1827 }
1828
1829 /**
1830  * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1831  * @adapter: board private structure to initialize
1832  *
1833  * We allocate one q_vector per queue interrupt.  If allocation fails we
1834  * return -ENOMEM.
1835  **/
1836 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1837 {
1838         int q_idx = 0, num_q_vectors;
1839         struct iavf_q_vector *q_vector;
1840
1841         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1842         adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1843                                      GFP_KERNEL);
1844         if (!adapter->q_vectors)
1845                 return -ENOMEM;
1846
1847         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1848                 q_vector = &adapter->q_vectors[q_idx];
1849                 q_vector->adapter = adapter;
1850                 q_vector->vsi = &adapter->vsi;
1851                 q_vector->v_idx = q_idx;
1852                 q_vector->reg_idx = q_idx;
1853                 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1854                 netif_napi_add(adapter->netdev, &q_vector->napi,
1855                                iavf_napi_poll);
1856         }
1857
1858         return 0;
1859 }
1860
1861 /**
1862  * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1863  * @adapter: board private structure to initialize
1864  *
1865  * This function frees the memory allocated to the q_vectors.  In addition if
1866  * NAPI is enabled it will delete any references to the NAPI struct prior
1867  * to freeing the q_vector.
1868  **/
1869 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1870 {
1871         int q_idx, num_q_vectors;
1872
1873         if (!adapter->q_vectors)
1874                 return;
1875
1876         num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1877
1878         for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1879                 struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1880
1881                 netif_napi_del(&q_vector->napi);
1882         }
1883         kfree(adapter->q_vectors);
1884         adapter->q_vectors = NULL;
1885 }
1886
1887 /**
1888  * iavf_reset_interrupt_capability - Reset MSIX setup
1889  * @adapter: board private structure
1890  *
1891  **/
1892 static void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1893 {
1894         if (!adapter->msix_entries)
1895                 return;
1896
1897         pci_disable_msix(adapter->pdev);
1898         kfree(adapter->msix_entries);
1899         adapter->msix_entries = NULL;
1900 }
1901
1902 /**
1903  * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1904  * @adapter: board private structure to initialize
1905  *
1906  **/
1907 static int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1908 {
1909         int err;
1910
1911         err = iavf_alloc_queues(adapter);
1912         if (err) {
1913                 dev_err(&adapter->pdev->dev,
1914                         "Unable to allocate memory for queues\n");
1915                 goto err_alloc_queues;
1916         }
1917
1918         err = iavf_set_interrupt_capability(adapter);
1919         if (err) {
1920                 dev_err(&adapter->pdev->dev,
1921                         "Unable to setup interrupt capabilities\n");
1922                 goto err_set_interrupt;
1923         }
1924
1925         err = iavf_alloc_q_vectors(adapter);
1926         if (err) {
1927                 dev_err(&adapter->pdev->dev,
1928                         "Unable to allocate memory for queue vectors\n");
1929                 goto err_alloc_q_vectors;
1930         }
1931
1932         /* If we've made it so far while ADq flag being ON, then we haven't
1933          * bailed out anywhere in middle. And ADq isn't just enabled but actual
1934          * resources have been allocated in the reset path.
1935          * Now we can truly claim that ADq is enabled.
1936          */
1937         if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1938             adapter->num_tc)
1939                 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1940                          adapter->num_tc);
1941
1942         dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1943                  (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1944                  adapter->num_active_queues);
1945
1946         return 0;
1947 err_alloc_q_vectors:
1948         iavf_reset_interrupt_capability(adapter);
1949 err_set_interrupt:
1950         iavf_free_queues(adapter);
1951 err_alloc_queues:
1952         return err;
1953 }
1954
1955 /**
1956  * iavf_free_rss - Free memory used by RSS structs
1957  * @adapter: board private structure
1958  **/
1959 static void iavf_free_rss(struct iavf_adapter *adapter)
1960 {
1961         kfree(adapter->rss_key);
1962         adapter->rss_key = NULL;
1963
1964         kfree(adapter->rss_lut);
1965         adapter->rss_lut = NULL;
1966 }
1967
1968 /**
1969  * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1970  * @adapter: board private structure
1971  * @running: true if adapter->state == __IAVF_RUNNING
1972  *
1973  * Returns 0 on success, negative on failure
1974  **/
1975 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter, bool running)
1976 {
1977         struct net_device *netdev = adapter->netdev;
1978         int err;
1979
1980         if (running)
1981                 iavf_free_traffic_irqs(adapter);
1982         iavf_free_misc_irq(adapter);
1983         iavf_reset_interrupt_capability(adapter);
1984         iavf_free_q_vectors(adapter);
1985         iavf_free_queues(adapter);
1986
1987         err =  iavf_init_interrupt_scheme(adapter);
1988         if (err)
1989                 goto err;
1990
1991         netif_tx_stop_all_queues(netdev);
1992
1993         err = iavf_request_misc_irq(adapter);
1994         if (err)
1995                 goto err;
1996
1997         set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1998
1999         iavf_map_rings_to_vectors(adapter);
2000 err:
2001         return err;
2002 }
2003
2004 /**
2005  * iavf_finish_config - do all netdev work that needs RTNL
2006  * @work: our work_struct
2007  *
2008  * Do work that needs both RTNL and crit_lock.
2009  **/
2010 static void iavf_finish_config(struct work_struct *work)
2011 {
2012         struct iavf_adapter *adapter;
2013         int pairs, err;
2014
2015         adapter = container_of(work, struct iavf_adapter, finish_config);
2016
2017         /* Always take RTNL first to prevent circular lock dependency */
2018         rtnl_lock();
2019         mutex_lock(&adapter->crit_lock);
2020
2021         if ((adapter->flags & IAVF_FLAG_SETUP_NETDEV_FEATURES) &&
2022             adapter->netdev_registered &&
2023             !test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section)) {
2024                 netdev_update_features(adapter->netdev);
2025                 adapter->flags &= ~IAVF_FLAG_SETUP_NETDEV_FEATURES;
2026         }
2027
2028         switch (adapter->state) {
2029         case __IAVF_DOWN:
2030                 if (!adapter->netdev_registered) {
2031                         err = register_netdevice(adapter->netdev);
2032                         if (err) {
2033                                 dev_err(&adapter->pdev->dev, "Unable to register netdev (%d)\n",
2034                                         err);
2035
2036                                 /* go back and try again.*/
2037                                 iavf_free_rss(adapter);
2038                                 iavf_free_misc_irq(adapter);
2039                                 iavf_reset_interrupt_capability(adapter);
2040                                 iavf_change_state(adapter,
2041                                                   __IAVF_INIT_CONFIG_ADAPTER);
2042                                 goto out;
2043                         }
2044                         adapter->netdev_registered = true;
2045                 }
2046
2047                 /* Set the real number of queues when reset occurs while
2048                  * state == __IAVF_DOWN
2049                  */
2050                 fallthrough;
2051         case __IAVF_RUNNING:
2052                 pairs = adapter->num_active_queues;
2053                 netif_set_real_num_rx_queues(adapter->netdev, pairs);
2054                 netif_set_real_num_tx_queues(adapter->netdev, pairs);
2055                 break;
2056
2057         default:
2058                 break;
2059         }
2060
2061 out:
2062         mutex_unlock(&adapter->crit_lock);
2063         rtnl_unlock();
2064 }
2065
2066 /**
2067  * iavf_schedule_finish_config - Set the flags and schedule a reset event
2068  * @adapter: board private structure
2069  **/
2070 void iavf_schedule_finish_config(struct iavf_adapter *adapter)
2071 {
2072         if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
2073                 queue_work(adapter->wq, &adapter->finish_config);
2074 }
2075
2076 /**
2077  * iavf_process_aq_command - process aq_required flags
2078  * and sends aq command
2079  * @adapter: pointer to iavf adapter structure
2080  *
2081  * Returns 0 on success
2082  * Returns error code if no command was sent
2083  * or error code if the command failed.
2084  **/
2085 static int iavf_process_aq_command(struct iavf_adapter *adapter)
2086 {
2087         if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG)
2088                 return iavf_send_vf_config_msg(adapter);
2089         if (adapter->aq_required & IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS)
2090                 return iavf_send_vf_offload_vlan_v2_msg(adapter);
2091         if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
2092                 iavf_disable_queues(adapter);
2093                 return 0;
2094         }
2095
2096         if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
2097                 iavf_map_queues(adapter);
2098                 return 0;
2099         }
2100
2101         if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
2102                 iavf_add_ether_addrs(adapter);
2103                 return 0;
2104         }
2105
2106         if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
2107                 iavf_add_vlans(adapter);
2108                 return 0;
2109         }
2110
2111         if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
2112                 iavf_del_ether_addrs(adapter);
2113                 return 0;
2114         }
2115
2116         if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
2117                 iavf_del_vlans(adapter);
2118                 return 0;
2119         }
2120
2121         if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
2122                 iavf_enable_vlan_stripping(adapter);
2123                 return 0;
2124         }
2125
2126         if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
2127                 iavf_disable_vlan_stripping(adapter);
2128                 return 0;
2129         }
2130
2131         if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
2132                 iavf_configure_queues(adapter);
2133                 return 0;
2134         }
2135
2136         if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
2137                 iavf_enable_queues(adapter);
2138                 return 0;
2139         }
2140
2141         if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
2142                 /* This message goes straight to the firmware, not the
2143                  * PF, so we don't have to set current_op as we will
2144                  * not get a response through the ARQ.
2145                  */
2146                 adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
2147                 return 0;
2148         }
2149         if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
2150                 iavf_get_hena(adapter);
2151                 return 0;
2152         }
2153         if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
2154                 iavf_set_hena(adapter);
2155                 return 0;
2156         }
2157         if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
2158                 iavf_set_rss_key(adapter);
2159                 return 0;
2160         }
2161         if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
2162                 iavf_set_rss_lut(adapter);
2163                 return 0;
2164         }
2165
2166         if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_PROMISC) {
2167                 iavf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
2168                                        FLAG_VF_MULTICAST_PROMISC);
2169                 return 0;
2170         }
2171
2172         if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_ALLMULTI) {
2173                 iavf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
2174                 return 0;
2175         }
2176         if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) ||
2177             (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
2178                 iavf_set_promiscuous(adapter, 0);
2179                 return 0;
2180         }
2181
2182         if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
2183                 iavf_enable_channels(adapter);
2184                 return 0;
2185         }
2186
2187         if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
2188                 iavf_disable_channels(adapter);
2189                 return 0;
2190         }
2191         if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
2192                 iavf_add_cloud_filter(adapter);
2193                 return 0;
2194         }
2195
2196         if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
2197                 iavf_del_cloud_filter(adapter);
2198                 return 0;
2199         }
2200         if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
2201                 iavf_del_cloud_filter(adapter);
2202                 return 0;
2203         }
2204         if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
2205                 iavf_add_cloud_filter(adapter);
2206                 return 0;
2207         }
2208         if (adapter->aq_required & IAVF_FLAG_AQ_ADD_FDIR_FILTER) {
2209                 iavf_add_fdir_filter(adapter);
2210                 return IAVF_SUCCESS;
2211         }
2212         if (adapter->aq_required & IAVF_FLAG_AQ_DEL_FDIR_FILTER) {
2213                 iavf_del_fdir_filter(adapter);
2214                 return IAVF_SUCCESS;
2215         }
2216         if (adapter->aq_required & IAVF_FLAG_AQ_ADD_ADV_RSS_CFG) {
2217                 iavf_add_adv_rss_cfg(adapter);
2218                 return 0;
2219         }
2220         if (adapter->aq_required & IAVF_FLAG_AQ_DEL_ADV_RSS_CFG) {
2221                 iavf_del_adv_rss_cfg(adapter);
2222                 return 0;
2223         }
2224         if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING) {
2225                 iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2226                 return 0;
2227         }
2228         if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING) {
2229                 iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2230                 return 0;
2231         }
2232         if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING) {
2233                 iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2234                 return 0;
2235         }
2236         if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING) {
2237                 iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2238                 return 0;
2239         }
2240         if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION) {
2241                 iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2242                 return 0;
2243         }
2244         if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION) {
2245                 iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2246                 return 0;
2247         }
2248         if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION) {
2249                 iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2250                 return 0;
2251         }
2252         if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION) {
2253                 iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2254                 return 0;
2255         }
2256
2257         if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_STATS) {
2258                 iavf_request_stats(adapter);
2259                 return 0;
2260         }
2261
2262         return -EAGAIN;
2263 }
2264
2265 /**
2266  * iavf_set_vlan_offload_features - set VLAN offload configuration
2267  * @adapter: board private structure
2268  * @prev_features: previous features used for comparison
2269  * @features: updated features used for configuration
2270  *
2271  * Set the aq_required bit(s) based on the requested features passed in to
2272  * configure VLAN stripping and/or VLAN insertion if supported. Also, schedule
2273  * the watchdog if any changes are requested to expedite the request via
2274  * virtchnl.
2275  **/
2276 static void
2277 iavf_set_vlan_offload_features(struct iavf_adapter *adapter,
2278                                netdev_features_t prev_features,
2279                                netdev_features_t features)
2280 {
2281         bool enable_stripping = true, enable_insertion = true;
2282         u16 vlan_ethertype = 0;
2283         u64 aq_required = 0;
2284
2285         /* keep cases separate because one ethertype for offloads can be
2286          * disabled at the same time as another is disabled, so check for an
2287          * enabled ethertype first, then check for disabled. Default to
2288          * ETH_P_8021Q so an ethertype is specified if disabling insertion and
2289          * stripping.
2290          */
2291         if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2292                 vlan_ethertype = ETH_P_8021AD;
2293         else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2294                 vlan_ethertype = ETH_P_8021Q;
2295         else if (prev_features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2296                 vlan_ethertype = ETH_P_8021AD;
2297         else if (prev_features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2298                 vlan_ethertype = ETH_P_8021Q;
2299         else
2300                 vlan_ethertype = ETH_P_8021Q;
2301
2302         if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
2303                 enable_stripping = false;
2304         if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
2305                 enable_insertion = false;
2306
2307         if (VLAN_ALLOWED(adapter)) {
2308                 /* VIRTCHNL_VF_OFFLOAD_VLAN only has support for toggling VLAN
2309                  * stripping via virtchnl. VLAN insertion can be toggled on the
2310                  * netdev, but it doesn't require a virtchnl message
2311                  */
2312                 if (enable_stripping)
2313                         aq_required |= IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2314                 else
2315                         aq_required |= IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2316
2317         } else if (VLAN_V2_ALLOWED(adapter)) {
2318                 switch (vlan_ethertype) {
2319                 case ETH_P_8021Q:
2320                         if (enable_stripping)
2321                                 aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING;
2322                         else
2323                                 aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING;
2324
2325                         if (enable_insertion)
2326                                 aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION;
2327                         else
2328                                 aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION;
2329                         break;
2330                 case ETH_P_8021AD:
2331                         if (enable_stripping)
2332                                 aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING;
2333                         else
2334                                 aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING;
2335
2336                         if (enable_insertion)
2337                                 aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION;
2338                         else
2339                                 aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION;
2340                         break;
2341                 }
2342         }
2343
2344         if (aq_required) {
2345                 adapter->aq_required |= aq_required;
2346                 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
2347         }
2348 }
2349
2350 /**
2351  * iavf_startup - first step of driver startup
2352  * @adapter: board private structure
2353  *
2354  * Function process __IAVF_STARTUP driver state.
2355  * When success the state is changed to __IAVF_INIT_VERSION_CHECK
2356  * when fails the state is changed to __IAVF_INIT_FAILED
2357  **/
2358 static void iavf_startup(struct iavf_adapter *adapter)
2359 {
2360         struct pci_dev *pdev = adapter->pdev;
2361         struct iavf_hw *hw = &adapter->hw;
2362         enum iavf_status status;
2363         int ret;
2364
2365         WARN_ON(adapter->state != __IAVF_STARTUP);
2366
2367         /* driver loaded, probe complete */
2368         adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2369         adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2370         status = iavf_set_mac_type(hw);
2371         if (status) {
2372                 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n", status);
2373                 goto err;
2374         }
2375
2376         ret = iavf_check_reset_complete(hw);
2377         if (ret) {
2378                 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2379                          ret);
2380                 goto err;
2381         }
2382         hw->aq.num_arq_entries = IAVF_AQ_LEN;
2383         hw->aq.num_asq_entries = IAVF_AQ_LEN;
2384         hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2385         hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2386
2387         status = iavf_init_adminq(hw);
2388         if (status) {
2389                 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2390                         status);
2391                 goto err;
2392         }
2393         ret = iavf_send_api_ver(adapter);
2394         if (ret) {
2395                 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", ret);
2396                 iavf_shutdown_adminq(hw);
2397                 goto err;
2398         }
2399         iavf_change_state(adapter, __IAVF_INIT_VERSION_CHECK);
2400         return;
2401 err:
2402         iavf_change_state(adapter, __IAVF_INIT_FAILED);
2403 }
2404
2405 /**
2406  * iavf_init_version_check - second step of driver startup
2407  * @adapter: board private structure
2408  *
2409  * Function process __IAVF_INIT_VERSION_CHECK driver state.
2410  * When success the state is changed to __IAVF_INIT_GET_RESOURCES
2411  * when fails the state is changed to __IAVF_INIT_FAILED
2412  **/
2413 static void iavf_init_version_check(struct iavf_adapter *adapter)
2414 {
2415         struct pci_dev *pdev = adapter->pdev;
2416         struct iavf_hw *hw = &adapter->hw;
2417         int err = -EAGAIN;
2418
2419         WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
2420
2421         if (!iavf_asq_done(hw)) {
2422                 dev_err(&pdev->dev, "Admin queue command never completed\n");
2423                 iavf_shutdown_adminq(hw);
2424                 iavf_change_state(adapter, __IAVF_STARTUP);
2425                 goto err;
2426         }
2427
2428         /* aq msg sent, awaiting reply */
2429         err = iavf_verify_api_ver(adapter);
2430         if (err) {
2431                 if (err == -EALREADY)
2432                         err = iavf_send_api_ver(adapter);
2433                 else
2434                         dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2435                                 adapter->pf_version.major,
2436                                 adapter->pf_version.minor,
2437                                 VIRTCHNL_VERSION_MAJOR,
2438                                 VIRTCHNL_VERSION_MINOR);
2439                 goto err;
2440         }
2441         err = iavf_send_vf_config_msg(adapter);
2442         if (err) {
2443                 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2444                         err);
2445                 goto err;
2446         }
2447         iavf_change_state(adapter, __IAVF_INIT_GET_RESOURCES);
2448         return;
2449 err:
2450         iavf_change_state(adapter, __IAVF_INIT_FAILED);
2451 }
2452
2453 /**
2454  * iavf_parse_vf_resource_msg - parse response from VIRTCHNL_OP_GET_VF_RESOURCES
2455  * @adapter: board private structure
2456  */
2457 int iavf_parse_vf_resource_msg(struct iavf_adapter *adapter)
2458 {
2459         int i, num_req_queues = adapter->num_req_queues;
2460         struct iavf_vsi *vsi = &adapter->vsi;
2461
2462         for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2463                 if (adapter->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
2464                         adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2465         }
2466         if (!adapter->vsi_res) {
2467                 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2468                 return -ENODEV;
2469         }
2470
2471         if (num_req_queues &&
2472             num_req_queues > adapter->vsi_res->num_queue_pairs) {
2473                 /* Problem.  The PF gave us fewer queues than what we had
2474                  * negotiated in our request.  Need a reset to see if we can't
2475                  * get back to a working state.
2476                  */
2477                 dev_err(&adapter->pdev->dev,
2478                         "Requested %d queues, but PF only gave us %d.\n",
2479                         num_req_queues,
2480                         adapter->vsi_res->num_queue_pairs);
2481                 adapter->flags |= IAVF_FLAG_REINIT_MSIX_NEEDED;
2482                 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2483                 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
2484
2485                 return -EAGAIN;
2486         }
2487         adapter->num_req_queues = 0;
2488         adapter->vsi.id = adapter->vsi_res->vsi_id;
2489
2490         adapter->vsi.back = adapter;
2491         adapter->vsi.base_vector = 1;
2492         vsi->netdev = adapter->netdev;
2493         vsi->qs_handle = adapter->vsi_res->qset_handle;
2494         if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2495                 adapter->rss_key_size = adapter->vf_res->rss_key_size;
2496                 adapter->rss_lut_size = adapter->vf_res->rss_lut_size;
2497         } else {
2498                 adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
2499                 adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
2500         }
2501
2502         return 0;
2503 }
2504
2505 /**
2506  * iavf_init_get_resources - third step of driver startup
2507  * @adapter: board private structure
2508  *
2509  * Function process __IAVF_INIT_GET_RESOURCES driver state and
2510  * finishes driver initialization procedure.
2511  * When success the state is changed to __IAVF_DOWN
2512  * when fails the state is changed to __IAVF_INIT_FAILED
2513  **/
2514 static void iavf_init_get_resources(struct iavf_adapter *adapter)
2515 {
2516         struct pci_dev *pdev = adapter->pdev;
2517         struct iavf_hw *hw = &adapter->hw;
2518         int err;
2519
2520         WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
2521         /* aq msg sent, awaiting reply */
2522         if (!adapter->vf_res) {
2523                 adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
2524                                           GFP_KERNEL);
2525                 if (!adapter->vf_res) {
2526                         err = -ENOMEM;
2527                         goto err;
2528                 }
2529         }
2530         err = iavf_get_vf_config(adapter);
2531         if (err == -EALREADY) {
2532                 err = iavf_send_vf_config_msg(adapter);
2533                 goto err;
2534         } else if (err == -EINVAL) {
2535                 /* We only get -EINVAL if the device is in a very bad
2536                  * state or if we've been disabled for previous bad
2537                  * behavior. Either way, we're done now.
2538                  */
2539                 iavf_shutdown_adminq(hw);
2540                 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2541                 return;
2542         }
2543         if (err) {
2544                 dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
2545                 goto err_alloc;
2546         }
2547
2548         err = iavf_parse_vf_resource_msg(adapter);
2549         if (err) {
2550                 dev_err(&pdev->dev, "Failed to parse VF resource message from PF (%d)\n",
2551                         err);
2552                 goto err_alloc;
2553         }
2554         /* Some features require additional messages to negotiate extended
2555          * capabilities. These are processed in sequence by the
2556          * __IAVF_INIT_EXTENDED_CAPS driver state.
2557          */
2558         adapter->extended_caps = IAVF_EXTENDED_CAPS;
2559
2560         iavf_change_state(adapter, __IAVF_INIT_EXTENDED_CAPS);
2561         return;
2562
2563 err_alloc:
2564         kfree(adapter->vf_res);
2565         adapter->vf_res = NULL;
2566 err:
2567         iavf_change_state(adapter, __IAVF_INIT_FAILED);
2568 }
2569
2570 /**
2571  * iavf_init_send_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2572  * @adapter: board private structure
2573  *
2574  * Function processes send of the extended VLAN V2 capability message to the
2575  * PF. Must clear IAVF_EXTENDED_CAP_RECV_VLAN_V2 if the message is not sent,
2576  * e.g. due to PF not negotiating VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2577  */
2578 static void iavf_init_send_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2579 {
2580         int ret;
2581
2582         WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2));
2583
2584         ret = iavf_send_vf_offload_vlan_v2_msg(adapter);
2585         if (ret && ret == -EOPNOTSUPP) {
2586                 /* PF does not support VIRTCHNL_VF_OFFLOAD_V2. In this case,
2587                  * we did not send the capability exchange message and do not
2588                  * expect a response.
2589                  */
2590                 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2591         }
2592
2593         /* We sent the message, so move on to the next step */
2594         adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2595 }
2596
2597 /**
2598  * iavf_init_recv_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2599  * @adapter: board private structure
2600  *
2601  * Function processes receipt of the extended VLAN V2 capability message from
2602  * the PF.
2603  **/
2604 static void iavf_init_recv_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2605 {
2606         int ret;
2607
2608         WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2));
2609
2610         memset(&adapter->vlan_v2_caps, 0, sizeof(adapter->vlan_v2_caps));
2611
2612         ret = iavf_get_vf_vlan_v2_caps(adapter);
2613         if (ret)
2614                 goto err;
2615
2616         /* We've processed receipt of the VLAN V2 caps message */
2617         adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2618         return;
2619 err:
2620         /* We didn't receive a reply. Make sure we try sending again when
2621          * __IAVF_INIT_FAILED attempts to recover.
2622          */
2623         adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2624         iavf_change_state(adapter, __IAVF_INIT_FAILED);
2625 }
2626
2627 /**
2628  * iavf_init_process_extended_caps - Part of driver startup
2629  * @adapter: board private structure
2630  *
2631  * Function processes __IAVF_INIT_EXTENDED_CAPS driver state. This state
2632  * handles negotiating capabilities for features which require an additional
2633  * message.
2634  *
2635  * Once all extended capabilities exchanges are finished, the driver will
2636  * transition into __IAVF_INIT_CONFIG_ADAPTER.
2637  */
2638 static void iavf_init_process_extended_caps(struct iavf_adapter *adapter)
2639 {
2640         WARN_ON(adapter->state != __IAVF_INIT_EXTENDED_CAPS);
2641
2642         /* Process capability exchange for VLAN V2 */
2643         if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2) {
2644                 iavf_init_send_offload_vlan_v2_caps(adapter);
2645                 return;
2646         } else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2) {
2647                 iavf_init_recv_offload_vlan_v2_caps(adapter);
2648                 return;
2649         }
2650
2651         /* When we reach here, no further extended capabilities exchanges are
2652          * necessary, so we finally transition into __IAVF_INIT_CONFIG_ADAPTER
2653          */
2654         iavf_change_state(adapter, __IAVF_INIT_CONFIG_ADAPTER);
2655 }
2656
2657 /**
2658  * iavf_init_config_adapter - last part of driver startup
2659  * @adapter: board private structure
2660  *
2661  * After all the supported capabilities are negotiated, then the
2662  * __IAVF_INIT_CONFIG_ADAPTER state will finish driver initialization.
2663  */
2664 static void iavf_init_config_adapter(struct iavf_adapter *adapter)
2665 {
2666         struct net_device *netdev = adapter->netdev;
2667         struct pci_dev *pdev = adapter->pdev;
2668         int err;
2669
2670         WARN_ON(adapter->state != __IAVF_INIT_CONFIG_ADAPTER);
2671
2672         if (iavf_process_config(adapter))
2673                 goto err;
2674
2675         adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2676
2677         adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
2678
2679         netdev->netdev_ops = &iavf_netdev_ops;
2680         iavf_set_ethtool_ops(netdev);
2681         netdev->watchdog_timeo = 5 * HZ;
2682
2683         /* MTU range: 68 - 9710 */
2684         netdev->min_mtu = ETH_MIN_MTU;
2685         netdev->max_mtu = IAVF_MAX_RXBUFFER - IAVF_PACKET_HDR_PAD;
2686
2687         if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2688                 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2689                          adapter->hw.mac.addr);
2690                 eth_hw_addr_random(netdev);
2691                 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2692         } else {
2693                 eth_hw_addr_set(netdev, adapter->hw.mac.addr);
2694                 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2695         }
2696
2697         adapter->tx_desc_count = IAVF_DEFAULT_TXD;
2698         adapter->rx_desc_count = IAVF_DEFAULT_RXD;
2699         err = iavf_init_interrupt_scheme(adapter);
2700         if (err)
2701                 goto err_sw_init;
2702         iavf_map_rings_to_vectors(adapter);
2703         if (adapter->vf_res->vf_cap_flags &
2704                 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2705                 adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
2706
2707         err = iavf_request_misc_irq(adapter);
2708         if (err)
2709                 goto err_sw_init;
2710
2711         netif_carrier_off(netdev);
2712         adapter->link_up = false;
2713         netif_tx_stop_all_queues(netdev);
2714
2715         if (CLIENT_ALLOWED(adapter)) {
2716                 err = iavf_lan_add_device(adapter);
2717                 if (err)
2718                         dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2719                                  err);
2720         }
2721         dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2722         if (netdev->features & NETIF_F_GRO)
2723                 dev_info(&pdev->dev, "GRO is enabled\n");
2724
2725         iavf_change_state(adapter, __IAVF_DOWN);
2726         set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2727
2728         iavf_misc_irq_enable(adapter);
2729         wake_up(&adapter->down_waitqueue);
2730
2731         adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2732         adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2733         if (!adapter->rss_key || !adapter->rss_lut) {
2734                 err = -ENOMEM;
2735                 goto err_mem;
2736         }
2737         if (RSS_AQ(adapter))
2738                 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
2739         else
2740                 iavf_init_rss(adapter);
2741
2742         if (VLAN_V2_ALLOWED(adapter))
2743                 /* request initial VLAN offload settings */
2744                 iavf_set_vlan_offload_features(adapter, 0, netdev->features);
2745
2746         iavf_schedule_finish_config(adapter);
2747         return;
2748
2749 err_mem:
2750         iavf_free_rss(adapter);
2751         iavf_free_misc_irq(adapter);
2752 err_sw_init:
2753         iavf_reset_interrupt_capability(adapter);
2754 err:
2755         iavf_change_state(adapter, __IAVF_INIT_FAILED);
2756 }
2757
2758 /**
2759  * iavf_watchdog_task - Periodic call-back task
2760  * @work: pointer to work_struct
2761  **/
2762 static void iavf_watchdog_task(struct work_struct *work)
2763 {
2764         struct iavf_adapter *adapter = container_of(work,
2765                                                     struct iavf_adapter,
2766                                                     watchdog_task.work);
2767         struct iavf_hw *hw = &adapter->hw;
2768         u32 reg_val;
2769
2770         if (!mutex_trylock(&adapter->crit_lock)) {
2771                 if (adapter->state == __IAVF_REMOVE)
2772                         return;
2773
2774                 goto restart_watchdog;
2775         }
2776
2777         if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2778                 iavf_change_state(adapter, __IAVF_COMM_FAILED);
2779
2780         switch (adapter->state) {
2781         case __IAVF_STARTUP:
2782                 iavf_startup(adapter);
2783                 mutex_unlock(&adapter->crit_lock);
2784                 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2785                                    msecs_to_jiffies(30));
2786                 return;
2787         case __IAVF_INIT_VERSION_CHECK:
2788                 iavf_init_version_check(adapter);
2789                 mutex_unlock(&adapter->crit_lock);
2790                 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2791                                    msecs_to_jiffies(30));
2792                 return;
2793         case __IAVF_INIT_GET_RESOURCES:
2794                 iavf_init_get_resources(adapter);
2795                 mutex_unlock(&adapter->crit_lock);
2796                 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2797                                    msecs_to_jiffies(1));
2798                 return;
2799         case __IAVF_INIT_EXTENDED_CAPS:
2800                 iavf_init_process_extended_caps(adapter);
2801                 mutex_unlock(&adapter->crit_lock);
2802                 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2803                                    msecs_to_jiffies(1));
2804                 return;
2805         case __IAVF_INIT_CONFIG_ADAPTER:
2806                 iavf_init_config_adapter(adapter);
2807                 mutex_unlock(&adapter->crit_lock);
2808                 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2809                                    msecs_to_jiffies(1));
2810                 return;
2811         case __IAVF_INIT_FAILED:
2812                 if (test_bit(__IAVF_IN_REMOVE_TASK,
2813                              &adapter->crit_section)) {
2814                         /* Do not update the state and do not reschedule
2815                          * watchdog task, iavf_remove should handle this state
2816                          * as it can loop forever
2817                          */
2818                         mutex_unlock(&adapter->crit_lock);
2819                         return;
2820                 }
2821                 if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
2822                         dev_err(&adapter->pdev->dev,
2823                                 "Failed to communicate with PF; waiting before retry\n");
2824                         adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2825                         iavf_shutdown_adminq(hw);
2826                         mutex_unlock(&adapter->crit_lock);
2827                         queue_delayed_work(adapter->wq,
2828                                            &adapter->watchdog_task, (5 * HZ));
2829                         return;
2830                 }
2831                 /* Try again from failed step*/
2832                 iavf_change_state(adapter, adapter->last_state);
2833                 mutex_unlock(&adapter->crit_lock);
2834                 queue_delayed_work(adapter->wq, &adapter->watchdog_task, HZ);
2835                 return;
2836         case __IAVF_COMM_FAILED:
2837                 if (test_bit(__IAVF_IN_REMOVE_TASK,
2838                              &adapter->crit_section)) {
2839                         /* Set state to __IAVF_INIT_FAILED and perform remove
2840                          * steps. Remove IAVF_FLAG_PF_COMMS_FAILED so the task
2841                          * doesn't bring the state back to __IAVF_COMM_FAILED.
2842                          */
2843                         iavf_change_state(adapter, __IAVF_INIT_FAILED);
2844                         adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2845                         mutex_unlock(&adapter->crit_lock);
2846                         return;
2847                 }
2848                 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2849                           IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2850                 if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
2851                     reg_val == VIRTCHNL_VFR_COMPLETED) {
2852                         /* A chance for redemption! */
2853                         dev_err(&adapter->pdev->dev,
2854                                 "Hardware came out of reset. Attempting reinit.\n");
2855                         /* When init task contacts the PF and
2856                          * gets everything set up again, it'll restart the
2857                          * watchdog for us. Down, boy. Sit. Stay. Woof.
2858                          */
2859                         iavf_change_state(adapter, __IAVF_STARTUP);
2860                         adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2861                 }
2862                 adapter->aq_required = 0;
2863                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2864                 mutex_unlock(&adapter->crit_lock);
2865                 queue_delayed_work(adapter->wq,
2866                                    &adapter->watchdog_task,
2867                                    msecs_to_jiffies(10));
2868                 return;
2869         case __IAVF_RESETTING:
2870                 mutex_unlock(&adapter->crit_lock);
2871                 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2872                                    HZ * 2);
2873                 return;
2874         case __IAVF_DOWN:
2875         case __IAVF_DOWN_PENDING:
2876         case __IAVF_TESTING:
2877         case __IAVF_RUNNING:
2878                 if (adapter->current_op) {
2879                         if (!iavf_asq_done(hw)) {
2880                                 dev_dbg(&adapter->pdev->dev,
2881                                         "Admin queue timeout\n");
2882                                 iavf_send_api_ver(adapter);
2883                         }
2884                 } else {
2885                         int ret = iavf_process_aq_command(adapter);
2886
2887                         /* An error will be returned if no commands were
2888                          * processed; use this opportunity to update stats
2889                          * if the error isn't -ENOTSUPP
2890                          */
2891                         if (ret && ret != -EOPNOTSUPP &&
2892                             adapter->state == __IAVF_RUNNING)
2893                                 iavf_request_stats(adapter);
2894                 }
2895                 if (adapter->state == __IAVF_RUNNING)
2896                         iavf_detect_recover_hung(&adapter->vsi);
2897                 break;
2898         case __IAVF_REMOVE:
2899         default:
2900                 mutex_unlock(&adapter->crit_lock);
2901                 return;
2902         }
2903
2904         /* check for hw reset */
2905         reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2906         if (!reg_val) {
2907                 adapter->aq_required = 0;
2908                 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2909                 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
2910                 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_PENDING);
2911                 mutex_unlock(&adapter->crit_lock);
2912                 queue_delayed_work(adapter->wq,
2913                                    &adapter->watchdog_task, HZ * 2);
2914                 return;
2915         }
2916
2917         schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
2918         mutex_unlock(&adapter->crit_lock);
2919 restart_watchdog:
2920         if (adapter->state >= __IAVF_DOWN)
2921                 queue_work(adapter->wq, &adapter->adminq_task);
2922         if (adapter->aq_required)
2923                 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2924                                    msecs_to_jiffies(20));
2925         else
2926                 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2927                                    HZ * 2);
2928 }
2929
2930 /**
2931  * iavf_disable_vf - disable VF
2932  * @adapter: board private structure
2933  *
2934  * Set communication failed flag and free all resources.
2935  * NOTE: This function is expected to be called with crit_lock being held.
2936  **/
2937 static void iavf_disable_vf(struct iavf_adapter *adapter)
2938 {
2939         struct iavf_mac_filter *f, *ftmp;
2940         struct iavf_vlan_filter *fv, *fvtmp;
2941         struct iavf_cloud_filter *cf, *cftmp;
2942
2943         adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2944
2945         /* We don't use netif_running() because it may be true prior to
2946          * ndo_open() returning, so we can't assume it means all our open
2947          * tasks have finished, since we're not holding the rtnl_lock here.
2948          */
2949         if (adapter->state == __IAVF_RUNNING) {
2950                 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2951                 netif_carrier_off(adapter->netdev);
2952                 netif_tx_disable(adapter->netdev);
2953                 adapter->link_up = false;
2954                 iavf_napi_disable_all(adapter);
2955                 iavf_irq_disable(adapter);
2956                 iavf_free_traffic_irqs(adapter);
2957                 iavf_free_all_tx_resources(adapter);
2958                 iavf_free_all_rx_resources(adapter);
2959         }
2960
2961         spin_lock_bh(&adapter->mac_vlan_list_lock);
2962
2963         /* Delete all of the filters */
2964         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2965                 list_del(&f->list);
2966                 kfree(f);
2967         }
2968
2969         list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
2970                 list_del(&fv->list);
2971                 kfree(fv);
2972         }
2973         adapter->num_vlan_filters = 0;
2974
2975         spin_unlock_bh(&adapter->mac_vlan_list_lock);
2976
2977         spin_lock_bh(&adapter->cloud_filter_list_lock);
2978         list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
2979                 list_del(&cf->list);
2980                 kfree(cf);
2981                 adapter->num_cloud_filters--;
2982         }
2983         spin_unlock_bh(&adapter->cloud_filter_list_lock);
2984
2985         iavf_free_misc_irq(adapter);
2986         iavf_reset_interrupt_capability(adapter);
2987         iavf_free_q_vectors(adapter);
2988         iavf_free_queues(adapter);
2989         memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
2990         iavf_shutdown_adminq(&adapter->hw);
2991         adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2992         iavf_change_state(adapter, __IAVF_DOWN);
2993         wake_up(&adapter->down_waitqueue);
2994         dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
2995 }
2996
2997 /**
2998  * iavf_reset_task - Call-back task to handle hardware reset
2999  * @work: pointer to work_struct
3000  *
3001  * During reset we need to shut down and reinitialize the admin queue
3002  * before we can use it to communicate with the PF again. We also clear
3003  * and reinit the rings because that context is lost as well.
3004  **/
3005 static void iavf_reset_task(struct work_struct *work)
3006 {
3007         struct iavf_adapter *adapter = container_of(work,
3008                                                       struct iavf_adapter,
3009                                                       reset_task);
3010         struct virtchnl_vf_resource *vfres = adapter->vf_res;
3011         struct net_device *netdev = adapter->netdev;
3012         struct iavf_hw *hw = &adapter->hw;
3013         struct iavf_mac_filter *f, *ftmp;
3014         struct iavf_cloud_filter *cf;
3015         enum iavf_status status;
3016         u32 reg_val;
3017         int i = 0, err;
3018         bool running;
3019
3020         /* When device is being removed it doesn't make sense to run the reset
3021          * task, just return in such a case.
3022          */
3023         if (!mutex_trylock(&adapter->crit_lock)) {
3024                 if (adapter->state != __IAVF_REMOVE)
3025                         queue_work(adapter->wq, &adapter->reset_task);
3026
3027                 return;
3028         }
3029
3030         while (!mutex_trylock(&adapter->client_lock))
3031                 usleep_range(500, 1000);
3032         if (CLIENT_ENABLED(adapter)) {
3033                 adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
3034                                     IAVF_FLAG_CLIENT_NEEDS_CLOSE |
3035                                     IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
3036                                     IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
3037                 cancel_delayed_work_sync(&adapter->client_task);
3038                 iavf_notify_client_close(&adapter->vsi, true);
3039         }
3040         iavf_misc_irq_disable(adapter);
3041         if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
3042                 adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
3043                 /* Restart the AQ here. If we have been reset but didn't
3044                  * detect it, or if the PF had to reinit, our AQ will be hosed.
3045                  */
3046                 iavf_shutdown_adminq(hw);
3047                 iavf_init_adminq(hw);
3048                 iavf_request_reset(adapter);
3049         }
3050         adapter->flags |= IAVF_FLAG_RESET_PENDING;
3051
3052         /* poll until we see the reset actually happen */
3053         for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
3054                 reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
3055                           IAVF_VF_ARQLEN1_ARQENABLE_MASK;
3056                 if (!reg_val)
3057                         break;
3058                 usleep_range(5000, 10000);
3059         }
3060         if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
3061                 dev_info(&adapter->pdev->dev, "Never saw reset\n");
3062                 goto continue_reset; /* act like the reset happened */
3063         }
3064
3065         /* wait until the reset is complete and the PF is responding to us */
3066         for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3067                 /* sleep first to make sure a minimum wait time is met */
3068                 msleep(IAVF_RESET_WAIT_MS);
3069
3070                 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
3071                           IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3072                 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
3073                         break;
3074         }
3075
3076         pci_set_master(adapter->pdev);
3077         pci_restore_msi_state(adapter->pdev);
3078
3079         if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
3080                 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
3081                         reg_val);
3082                 iavf_disable_vf(adapter);
3083                 mutex_unlock(&adapter->client_lock);
3084                 mutex_unlock(&adapter->crit_lock);
3085                 return; /* Do not attempt to reinit. It's dead, Jim. */
3086         }
3087
3088 continue_reset:
3089         /* We don't use netif_running() because it may be true prior to
3090          * ndo_open() returning, so we can't assume it means all our open
3091          * tasks have finished, since we're not holding the rtnl_lock here.
3092          */
3093         running = adapter->state == __IAVF_RUNNING;
3094
3095         if (running) {
3096                 netif_carrier_off(netdev);
3097                 netif_tx_stop_all_queues(netdev);
3098                 adapter->link_up = false;
3099                 iavf_napi_disable_all(adapter);
3100         }
3101         iavf_irq_disable(adapter);
3102
3103         iavf_change_state(adapter, __IAVF_RESETTING);
3104         adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3105
3106         /* free the Tx/Rx rings and descriptors, might be better to just
3107          * re-use them sometime in the future
3108          */
3109         iavf_free_all_rx_resources(adapter);
3110         iavf_free_all_tx_resources(adapter);
3111
3112         adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
3113         /* kill and reinit the admin queue */
3114         iavf_shutdown_adminq(hw);
3115         adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3116         status = iavf_init_adminq(hw);
3117         if (status) {
3118                 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
3119                          status);
3120                 goto reset_err;
3121         }
3122         adapter->aq_required = 0;
3123
3124         if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3125             (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3126                 err = iavf_reinit_interrupt_scheme(adapter, running);
3127                 if (err)
3128                         goto reset_err;
3129         }
3130
3131         if (RSS_AQ(adapter)) {
3132                 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
3133         } else {
3134                 err = iavf_init_rss(adapter);
3135                 if (err)
3136                         goto reset_err;
3137         }
3138
3139         adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
3140         /* always set since VIRTCHNL_OP_GET_VF_RESOURCES has not been
3141          * sent/received yet, so VLAN_V2_ALLOWED() cannot is not reliable here,
3142          * however the VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS won't be sent until
3143          * VIRTCHNL_OP_GET_VF_RESOURCES and VIRTCHNL_VF_OFFLOAD_VLAN_V2 have
3144          * been successfully sent and negotiated
3145          */
3146         adapter->aq_required |= IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS;
3147         adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
3148
3149         spin_lock_bh(&adapter->mac_vlan_list_lock);
3150
3151         /* Delete filter for the current MAC address, it could have
3152          * been changed by the PF via administratively set MAC.
3153          * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
3154          */
3155         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3156                 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
3157                         list_del(&f->list);
3158                         kfree(f);
3159                 }
3160         }
3161         /* re-add all MAC filters */
3162         list_for_each_entry(f, &adapter->mac_filter_list, list) {
3163                 f->add = true;
3164         }
3165         spin_unlock_bh(&adapter->mac_vlan_list_lock);
3166
3167         /* check if TCs are running and re-add all cloud filters */
3168         spin_lock_bh(&adapter->cloud_filter_list_lock);
3169         if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
3170             adapter->num_tc) {
3171                 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
3172                         cf->add = true;
3173                 }
3174         }
3175         spin_unlock_bh(&adapter->cloud_filter_list_lock);
3176
3177         adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
3178         adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3179         iavf_misc_irq_enable(adapter);
3180
3181         mod_delayed_work(adapter->wq, &adapter->watchdog_task, 2);
3182
3183         /* We were running when the reset started, so we need to restore some
3184          * state here.
3185          */
3186         if (running) {
3187                 /* allocate transmit descriptors */
3188                 err = iavf_setup_all_tx_resources(adapter);
3189                 if (err)
3190                         goto reset_err;
3191
3192                 /* allocate receive descriptors */
3193                 err = iavf_setup_all_rx_resources(adapter);
3194                 if (err)
3195                         goto reset_err;
3196
3197                 if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3198                     (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3199                         err = iavf_request_traffic_irqs(adapter, netdev->name);
3200                         if (err)
3201                                 goto reset_err;
3202
3203                         adapter->flags &= ~IAVF_FLAG_REINIT_MSIX_NEEDED;
3204                 }
3205
3206                 iavf_configure(adapter);
3207
3208                 /* iavf_up_complete() will switch device back
3209                  * to __IAVF_RUNNING
3210                  */
3211                 iavf_up_complete(adapter);
3212
3213                 iavf_irq_enable(adapter, true);
3214         } else {
3215                 iavf_change_state(adapter, __IAVF_DOWN);
3216                 wake_up(&adapter->down_waitqueue);
3217         }
3218
3219         adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3220
3221         wake_up(&adapter->reset_waitqueue);
3222         mutex_unlock(&adapter->client_lock);
3223         mutex_unlock(&adapter->crit_lock);
3224
3225         return;
3226 reset_err:
3227         if (running) {
3228                 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3229                 iavf_free_traffic_irqs(adapter);
3230         }
3231         iavf_disable_vf(adapter);
3232
3233         mutex_unlock(&adapter->client_lock);
3234         mutex_unlock(&adapter->crit_lock);
3235         dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
3236 }
3237
3238 /**
3239  * iavf_adminq_task - worker thread to clean the admin queue
3240  * @work: pointer to work_struct containing our data
3241  **/
3242 static void iavf_adminq_task(struct work_struct *work)
3243 {
3244         struct iavf_adapter *adapter =
3245                 container_of(work, struct iavf_adapter, adminq_task);
3246         struct iavf_hw *hw = &adapter->hw;
3247         struct iavf_arq_event_info event;
3248         enum virtchnl_ops v_op;
3249         enum iavf_status ret, v_ret;
3250         u32 val, oldval;
3251         u16 pending;
3252
3253         if (!mutex_trylock(&adapter->crit_lock)) {
3254                 if (adapter->state == __IAVF_REMOVE)
3255                         return;
3256
3257                 queue_work(adapter->wq, &adapter->adminq_task);
3258                 goto out;
3259         }
3260
3261         if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
3262                 goto unlock;
3263
3264         event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
3265         event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
3266         if (!event.msg_buf)
3267                 goto unlock;
3268
3269         do {
3270                 ret = iavf_clean_arq_element(hw, &event, &pending);
3271                 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
3272                 v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
3273
3274                 if (ret || !v_op)
3275                         break; /* No event to process or error cleaning ARQ */
3276
3277                 iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
3278                                          event.msg_len);
3279                 if (pending != 0)
3280                         memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
3281         } while (pending);
3282
3283         if (iavf_is_reset_in_progress(adapter))
3284                 goto freedom;
3285
3286         /* check for error indications */
3287         val = rd32(hw, hw->aq.arq.len);
3288         if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
3289                 goto freedom;
3290         oldval = val;
3291         if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
3292                 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
3293                 val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
3294         }
3295         if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
3296                 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
3297                 val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
3298         }
3299         if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
3300                 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
3301                 val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
3302         }
3303         if (oldval != val)
3304                 wr32(hw, hw->aq.arq.len, val);
3305
3306         val = rd32(hw, hw->aq.asq.len);
3307         oldval = val;
3308         if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
3309                 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
3310                 val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
3311         }
3312         if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
3313                 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
3314                 val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
3315         }
3316         if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
3317                 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
3318                 val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
3319         }
3320         if (oldval != val)
3321                 wr32(hw, hw->aq.asq.len, val);
3322
3323 freedom:
3324         kfree(event.msg_buf);
3325 unlock:
3326         mutex_unlock(&adapter->crit_lock);
3327 out:
3328         /* re-enable Admin queue interrupt cause */
3329         iavf_misc_irq_enable(adapter);
3330 }
3331
3332 /**
3333  * iavf_client_task - worker thread to perform client work
3334  * @work: pointer to work_struct containing our data
3335  *
3336  * This task handles client interactions. Because client calls can be
3337  * reentrant, we can't handle them in the watchdog.
3338  **/
3339 static void iavf_client_task(struct work_struct *work)
3340 {
3341         struct iavf_adapter *adapter =
3342                 container_of(work, struct iavf_adapter, client_task.work);
3343
3344         /* If we can't get the client bit, just give up. We'll be rescheduled
3345          * later.
3346          */
3347
3348         if (!mutex_trylock(&adapter->client_lock))
3349                 return;
3350
3351         if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
3352                 iavf_client_subtask(adapter);
3353                 adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3354                 goto out;
3355         }
3356         if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
3357                 iavf_notify_client_l2_params(&adapter->vsi);
3358                 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
3359                 goto out;
3360         }
3361         if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_CLOSE) {
3362                 iavf_notify_client_close(&adapter->vsi, false);
3363                 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3364                 goto out;
3365         }
3366         if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
3367                 iavf_notify_client_open(&adapter->vsi);
3368                 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
3369         }
3370 out:
3371         mutex_unlock(&adapter->client_lock);
3372 }
3373
3374 /**
3375  * iavf_free_all_tx_resources - Free Tx Resources for All Queues
3376  * @adapter: board private structure
3377  *
3378  * Free all transmit software resources
3379  **/
3380 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
3381 {
3382         int i;
3383
3384         if (!adapter->tx_rings)
3385                 return;
3386
3387         for (i = 0; i < adapter->num_active_queues; i++)
3388                 if (adapter->tx_rings[i].desc)
3389                         iavf_free_tx_resources(&adapter->tx_rings[i]);
3390 }
3391
3392 /**
3393  * iavf_setup_all_tx_resources - allocate all queues Tx resources
3394  * @adapter: board private structure
3395  *
3396  * If this function returns with an error, then it's possible one or
3397  * more of the rings is populated (while the rest are not).  It is the
3398  * callers duty to clean those orphaned rings.
3399  *
3400  * Return 0 on success, negative on failure
3401  **/
3402 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
3403 {
3404         int i, err = 0;
3405
3406         for (i = 0; i < adapter->num_active_queues; i++) {
3407                 adapter->tx_rings[i].count = adapter->tx_desc_count;
3408                 err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
3409                 if (!err)
3410                         continue;
3411                 dev_err(&adapter->pdev->dev,
3412                         "Allocation for Tx Queue %u failed\n", i);
3413                 break;
3414         }
3415
3416         return err;
3417 }
3418
3419 /**
3420  * iavf_setup_all_rx_resources - allocate all queues Rx resources
3421  * @adapter: board private structure
3422  *
3423  * If this function returns with an error, then it's possible one or
3424  * more of the rings is populated (while the rest are not).  It is the
3425  * callers duty to clean those orphaned rings.
3426  *
3427  * Return 0 on success, negative on failure
3428  **/
3429 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
3430 {
3431         int i, err = 0;
3432
3433         for (i = 0; i < adapter->num_active_queues; i++) {
3434                 adapter->rx_rings[i].count = adapter->rx_desc_count;
3435                 err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
3436                 if (!err)
3437                         continue;
3438                 dev_err(&adapter->pdev->dev,
3439                         "Allocation for Rx Queue %u failed\n", i);
3440                 break;
3441         }
3442         return err;
3443 }
3444
3445 /**
3446  * iavf_free_all_rx_resources - Free Rx Resources for All Queues
3447  * @adapter: board private structure
3448  *
3449  * Free all receive software resources
3450  **/
3451 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
3452 {
3453         int i;
3454
3455         if (!adapter->rx_rings)
3456                 return;
3457
3458         for (i = 0; i < adapter->num_active_queues; i++)
3459                 if (adapter->rx_rings[i].desc)
3460                         iavf_free_rx_resources(&adapter->rx_rings[i]);
3461 }
3462
3463 /**
3464  * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
3465  * @adapter: board private structure
3466  * @max_tx_rate: max Tx bw for a tc
3467  **/
3468 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
3469                                       u64 max_tx_rate)
3470 {
3471         int speed = 0, ret = 0;
3472
3473         if (ADV_LINK_SUPPORT(adapter)) {
3474                 if (adapter->link_speed_mbps < U32_MAX) {
3475                         speed = adapter->link_speed_mbps;
3476                         goto validate_bw;
3477                 } else {
3478                         dev_err(&adapter->pdev->dev, "Unknown link speed\n");
3479                         return -EINVAL;
3480                 }
3481         }
3482
3483         switch (adapter->link_speed) {
3484         case VIRTCHNL_LINK_SPEED_40GB:
3485                 speed = SPEED_40000;
3486                 break;
3487         case VIRTCHNL_LINK_SPEED_25GB:
3488                 speed = SPEED_25000;
3489                 break;
3490         case VIRTCHNL_LINK_SPEED_20GB:
3491                 speed = SPEED_20000;
3492                 break;
3493         case VIRTCHNL_LINK_SPEED_10GB:
3494                 speed = SPEED_10000;
3495                 break;
3496         case VIRTCHNL_LINK_SPEED_5GB:
3497                 speed = SPEED_5000;
3498                 break;
3499         case VIRTCHNL_LINK_SPEED_2_5GB:
3500                 speed = SPEED_2500;
3501                 break;
3502         case VIRTCHNL_LINK_SPEED_1GB:
3503                 speed = SPEED_1000;
3504                 break;
3505         case VIRTCHNL_LINK_SPEED_100MB:
3506                 speed = SPEED_100;
3507                 break;
3508         default:
3509                 break;
3510         }
3511
3512 validate_bw:
3513         if (max_tx_rate > speed) {
3514                 dev_err(&adapter->pdev->dev,
3515                         "Invalid tx rate specified\n");
3516                 ret = -EINVAL;
3517         }
3518
3519         return ret;
3520 }
3521
3522 /**
3523  * iavf_validate_ch_config - validate queue mapping info
3524  * @adapter: board private structure
3525  * @mqprio_qopt: queue parameters
3526  *
3527  * This function validates if the config provided by the user to
3528  * configure queue channels is valid or not. Returns 0 on a valid
3529  * config.
3530  **/
3531 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
3532                                    struct tc_mqprio_qopt_offload *mqprio_qopt)
3533 {
3534         u64 total_max_rate = 0;
3535         u32 tx_rate_rem = 0;
3536         int i, num_qps = 0;
3537         u64 tx_rate = 0;
3538         int ret = 0;
3539
3540         if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
3541             mqprio_qopt->qopt.num_tc < 1)
3542                 return -EINVAL;
3543
3544         for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
3545                 if (!mqprio_qopt->qopt.count[i] ||
3546                     mqprio_qopt->qopt.offset[i] != num_qps)
3547                         return -EINVAL;
3548                 if (mqprio_qopt->min_rate[i]) {
3549                         dev_err(&adapter->pdev->dev,
3550                                 "Invalid min tx rate (greater than 0) specified for TC%d\n",
3551                                 i);
3552                         return -EINVAL;
3553                 }
3554
3555                 /* convert to Mbps */
3556                 tx_rate = div_u64(mqprio_qopt->max_rate[i],
3557                                   IAVF_MBPS_DIVISOR);
3558
3559                 if (mqprio_qopt->max_rate[i] &&
3560                     tx_rate < IAVF_MBPS_QUANTA) {
3561                         dev_err(&adapter->pdev->dev,
3562                                 "Invalid max tx rate for TC%d, minimum %dMbps\n",
3563                                 i, IAVF_MBPS_QUANTA);
3564                         return -EINVAL;
3565                 }
3566
3567                 (void)div_u64_rem(tx_rate, IAVF_MBPS_QUANTA, &tx_rate_rem);
3568
3569                 if (tx_rate_rem != 0) {
3570                         dev_err(&adapter->pdev->dev,
3571                                 "Invalid max tx rate for TC%d, not divisible by %d\n",
3572                                 i, IAVF_MBPS_QUANTA);
3573                         return -EINVAL;
3574                 }
3575
3576                 total_max_rate += tx_rate;
3577                 num_qps += mqprio_qopt->qopt.count[i];
3578         }
3579         if (num_qps > adapter->num_active_queues) {
3580                 dev_err(&adapter->pdev->dev,
3581                         "Cannot support requested number of queues\n");
3582                 return -EINVAL;
3583         }
3584
3585         ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
3586         return ret;
3587 }
3588
3589 /**
3590  * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
3591  * @adapter: board private structure
3592  **/
3593 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
3594 {
3595         struct iavf_cloud_filter *cf, *cftmp;
3596
3597         spin_lock_bh(&adapter->cloud_filter_list_lock);
3598         list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
3599                                  list) {
3600                 list_del(&cf->list);
3601                 kfree(cf);
3602                 adapter->num_cloud_filters--;
3603         }
3604         spin_unlock_bh(&adapter->cloud_filter_list_lock);
3605 }
3606
3607 /**
3608  * __iavf_setup_tc - configure multiple traffic classes
3609  * @netdev: network interface device structure
3610  * @type_data: tc offload data
3611  *
3612  * This function processes the config information provided by the
3613  * user to configure traffic classes/queue channels and packages the
3614  * information to request the PF to setup traffic classes.
3615  *
3616  * Returns 0 on success.
3617  **/
3618 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
3619 {
3620         struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
3621         struct iavf_adapter *adapter = netdev_priv(netdev);
3622         struct virtchnl_vf_resource *vfres = adapter->vf_res;
3623         u8 num_tc = 0, total_qps = 0;
3624         int ret = 0, netdev_tc = 0;
3625         u64 max_tx_rate;
3626         u16 mode;
3627         int i;
3628
3629         num_tc = mqprio_qopt->qopt.num_tc;
3630         mode = mqprio_qopt->mode;
3631
3632         /* delete queue_channel */
3633         if (!mqprio_qopt->qopt.hw) {
3634                 if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
3635                         /* reset the tc configuration */
3636                         netdev_reset_tc(netdev);
3637                         adapter->num_tc = 0;
3638                         netif_tx_stop_all_queues(netdev);
3639                         netif_tx_disable(netdev);
3640                         iavf_del_all_cloud_filters(adapter);
3641                         adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
3642                         total_qps = adapter->orig_num_active_queues;
3643                         goto exit;
3644                 } else {
3645                         return -EINVAL;
3646                 }
3647         }
3648
3649         /* add queue channel */
3650         if (mode == TC_MQPRIO_MODE_CHANNEL) {
3651                 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3652                         dev_err(&adapter->pdev->dev, "ADq not supported\n");
3653                         return -EOPNOTSUPP;
3654                 }
3655                 if (adapter->ch_config.state != __IAVF_TC_INVALID) {
3656                         dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
3657                         return -EINVAL;
3658                 }
3659
3660                 ret = iavf_validate_ch_config(adapter, mqprio_qopt);
3661                 if (ret)
3662                         return ret;
3663                 /* Return if same TC config is requested */
3664                 if (adapter->num_tc == num_tc)
3665                         return 0;
3666                 adapter->num_tc = num_tc;
3667
3668                 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3669                         if (i < num_tc) {
3670                                 adapter->ch_config.ch_info[i].count =
3671                                         mqprio_qopt->qopt.count[i];
3672                                 adapter->ch_config.ch_info[i].offset =
3673                                         mqprio_qopt->qopt.offset[i];
3674                                 total_qps += mqprio_qopt->qopt.count[i];
3675                                 max_tx_rate = mqprio_qopt->max_rate[i];
3676                                 /* convert to Mbps */
3677                                 max_tx_rate = div_u64(max_tx_rate,
3678                                                       IAVF_MBPS_DIVISOR);
3679                                 adapter->ch_config.ch_info[i].max_tx_rate =
3680                                         max_tx_rate;
3681                         } else {
3682                                 adapter->ch_config.ch_info[i].count = 1;
3683                                 adapter->ch_config.ch_info[i].offset = 0;
3684                         }
3685                 }
3686
3687                 /* Take snapshot of original config such as "num_active_queues"
3688                  * It is used later when delete ADQ flow is exercised, so that
3689                  * once delete ADQ flow completes, VF shall go back to its
3690                  * original queue configuration
3691                  */
3692
3693                 adapter->orig_num_active_queues = adapter->num_active_queues;
3694
3695                 /* Store queue info based on TC so that VF gets configured
3696                  * with correct number of queues when VF completes ADQ config
3697                  * flow
3698                  */
3699                 adapter->ch_config.total_qps = total_qps;
3700
3701                 netif_tx_stop_all_queues(netdev);
3702                 netif_tx_disable(netdev);
3703                 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
3704                 netdev_reset_tc(netdev);
3705                 /* Report the tc mapping up the stack */
3706                 netdev_set_num_tc(adapter->netdev, num_tc);
3707                 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3708                         u16 qcount = mqprio_qopt->qopt.count[i];
3709                         u16 qoffset = mqprio_qopt->qopt.offset[i];
3710
3711                         if (i < num_tc)
3712                                 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
3713                                                     qoffset);
3714                 }
3715         }
3716 exit:
3717         if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
3718                 return 0;
3719
3720         netif_set_real_num_rx_queues(netdev, total_qps);
3721         netif_set_real_num_tx_queues(netdev, total_qps);
3722
3723         return ret;
3724 }
3725
3726 /**
3727  * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
3728  * @adapter: board private structure
3729  * @f: pointer to struct flow_cls_offload
3730  * @filter: pointer to cloud filter structure
3731  */
3732 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
3733                                  struct flow_cls_offload *f,
3734                                  struct iavf_cloud_filter *filter)
3735 {
3736         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
3737         struct flow_dissector *dissector = rule->match.dissector;
3738         u16 n_proto_mask = 0;
3739         u16 n_proto_key = 0;
3740         u8 field_flags = 0;
3741         u16 addr_type = 0;
3742         u16 n_proto = 0;
3743         int i = 0;
3744         struct virtchnl_filter *vf = &filter->f;
3745
3746         if (dissector->used_keys &
3747             ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
3748               BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
3749               BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
3750               BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
3751               BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
3752               BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
3753               BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
3754               BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
3755                 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%llx\n",
3756                         dissector->used_keys);
3757                 return -EOPNOTSUPP;
3758         }
3759
3760         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
3761                 struct flow_match_enc_keyid match;
3762
3763                 flow_rule_match_enc_keyid(rule, &match);
3764                 if (match.mask->keyid != 0)
3765                         field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
3766         }
3767
3768         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
3769                 struct flow_match_basic match;
3770
3771                 flow_rule_match_basic(rule, &match);
3772                 n_proto_key = ntohs(match.key->n_proto);
3773                 n_proto_mask = ntohs(match.mask->n_proto);
3774
3775                 if (n_proto_key == ETH_P_ALL) {
3776                         n_proto_key = 0;
3777                         n_proto_mask = 0;
3778                 }
3779                 n_proto = n_proto_key & n_proto_mask;
3780                 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
3781                         return -EINVAL;
3782                 if (n_proto == ETH_P_IPV6) {
3783                         /* specify flow type as TCP IPv6 */
3784                         vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
3785                 }
3786
3787                 if (match.key->ip_proto != IPPROTO_TCP) {
3788                         dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
3789                         return -EINVAL;
3790                 }
3791         }
3792
3793         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
3794                 struct flow_match_eth_addrs match;
3795
3796                 flow_rule_match_eth_addrs(rule, &match);
3797
3798                 /* use is_broadcast and is_zero to check for all 0xf or 0 */
3799                 if (!is_zero_ether_addr(match.mask->dst)) {
3800                         if (is_broadcast_ether_addr(match.mask->dst)) {
3801                                 field_flags |= IAVF_CLOUD_FIELD_OMAC;
3802                         } else {
3803                                 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
3804                                         match.mask->dst);
3805                                 return -EINVAL;
3806                         }
3807                 }
3808
3809                 if (!is_zero_ether_addr(match.mask->src)) {
3810                         if (is_broadcast_ether_addr(match.mask->src)) {
3811                                 field_flags |= IAVF_CLOUD_FIELD_IMAC;
3812                         } else {
3813                                 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
3814                                         match.mask->src);
3815                                 return -EINVAL;
3816                         }
3817                 }
3818
3819                 if (!is_zero_ether_addr(match.key->dst))
3820                         if (is_valid_ether_addr(match.key->dst) ||
3821                             is_multicast_ether_addr(match.key->dst)) {
3822                                 /* set the mask if a valid dst_mac address */
3823                                 for (i = 0; i < ETH_ALEN; i++)
3824                                         vf->mask.tcp_spec.dst_mac[i] |= 0xff;
3825                                 ether_addr_copy(vf->data.tcp_spec.dst_mac,
3826                                                 match.key->dst);
3827                         }
3828
3829                 if (!is_zero_ether_addr(match.key->src))
3830                         if (is_valid_ether_addr(match.key->src) ||
3831                             is_multicast_ether_addr(match.key->src)) {
3832                                 /* set the mask if a valid dst_mac address */
3833                                 for (i = 0; i < ETH_ALEN; i++)
3834                                         vf->mask.tcp_spec.src_mac[i] |= 0xff;
3835                                 ether_addr_copy(vf->data.tcp_spec.src_mac,
3836                                                 match.key->src);
3837                 }
3838         }
3839
3840         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
3841                 struct flow_match_vlan match;
3842
3843                 flow_rule_match_vlan(rule, &match);
3844                 if (match.mask->vlan_id) {
3845                         if (match.mask->vlan_id == VLAN_VID_MASK) {
3846                                 field_flags |= IAVF_CLOUD_FIELD_IVLAN;
3847                         } else {
3848                                 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
3849                                         match.mask->vlan_id);
3850                                 return -EINVAL;
3851                         }
3852                 }
3853                 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
3854                 vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
3855         }
3856
3857         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
3858                 struct flow_match_control match;
3859
3860                 flow_rule_match_control(rule, &match);
3861                 addr_type = match.key->addr_type;
3862         }
3863
3864         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
3865                 struct flow_match_ipv4_addrs match;
3866
3867                 flow_rule_match_ipv4_addrs(rule, &match);
3868                 if (match.mask->dst) {
3869                         if (match.mask->dst == cpu_to_be32(0xffffffff)) {
3870                                 field_flags |= IAVF_CLOUD_FIELD_IIP;
3871                         } else {
3872                                 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
3873                                         be32_to_cpu(match.mask->dst));
3874                                 return -EINVAL;
3875                         }
3876                 }
3877
3878                 if (match.mask->src) {
3879                         if (match.mask->src == cpu_to_be32(0xffffffff)) {
3880                                 field_flags |= IAVF_CLOUD_FIELD_IIP;
3881                         } else {
3882                                 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
3883                                         be32_to_cpu(match.mask->src));
3884                                 return -EINVAL;
3885                         }
3886                 }
3887
3888                 if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
3889                         dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
3890                         return -EINVAL;
3891                 }
3892                 if (match.key->dst) {
3893                         vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
3894                         vf->data.tcp_spec.dst_ip[0] = match.key->dst;
3895                 }
3896                 if (match.key->src) {
3897                         vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
3898                         vf->data.tcp_spec.src_ip[0] = match.key->src;
3899                 }
3900         }
3901
3902         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
3903                 struct flow_match_ipv6_addrs match;
3904
3905                 flow_rule_match_ipv6_addrs(rule, &match);
3906
3907                 /* validate mask, make sure it is not IPV6_ADDR_ANY */
3908                 if (ipv6_addr_any(&match.mask->dst)) {
3909                         dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
3910                                 IPV6_ADDR_ANY);
3911                         return -EINVAL;
3912                 }
3913
3914                 /* src and dest IPv6 address should not be LOOPBACK
3915                  * (0:0:0:0:0:0:0:1) which can be represented as ::1
3916                  */
3917                 if (ipv6_addr_loopback(&match.key->dst) ||
3918                     ipv6_addr_loopback(&match.key->src)) {
3919                         dev_err(&adapter->pdev->dev,
3920                                 "ipv6 addr should not be loopback\n");
3921                         return -EINVAL;
3922                 }
3923                 if (!ipv6_addr_any(&match.mask->dst) ||
3924                     !ipv6_addr_any(&match.mask->src))
3925                         field_flags |= IAVF_CLOUD_FIELD_IIP;
3926
3927                 for (i = 0; i < 4; i++)
3928                         vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
3929                 memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
3930                        sizeof(vf->data.tcp_spec.dst_ip));
3931                 for (i = 0; i < 4; i++)
3932                         vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
3933                 memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
3934                        sizeof(vf->data.tcp_spec.src_ip));
3935         }
3936         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
3937                 struct flow_match_ports match;
3938
3939                 flow_rule_match_ports(rule, &match);
3940                 if (match.mask->src) {
3941                         if (match.mask->src == cpu_to_be16(0xffff)) {
3942                                 field_flags |= IAVF_CLOUD_FIELD_IIP;
3943                         } else {
3944                                 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
3945                                         be16_to_cpu(match.mask->src));
3946                                 return -EINVAL;
3947                         }
3948                 }
3949
3950                 if (match.mask->dst) {
3951                         if (match.mask->dst == cpu_to_be16(0xffff)) {
3952                                 field_flags |= IAVF_CLOUD_FIELD_IIP;
3953                         } else {
3954                                 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
3955                                         be16_to_cpu(match.mask->dst));
3956                                 return -EINVAL;
3957                         }
3958                 }
3959                 if (match.key->dst) {
3960                         vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
3961                         vf->data.tcp_spec.dst_port = match.key->dst;
3962                 }
3963
3964                 if (match.key->src) {
3965                         vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
3966                         vf->data.tcp_spec.src_port = match.key->src;
3967                 }
3968         }
3969         vf->field_flags = field_flags;
3970
3971         return 0;
3972 }
3973
3974 /**
3975  * iavf_handle_tclass - Forward to a traffic class on the device
3976  * @adapter: board private structure
3977  * @tc: traffic class index on the device
3978  * @filter: pointer to cloud filter structure
3979  */
3980 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
3981                               struct iavf_cloud_filter *filter)
3982 {
3983         if (tc == 0)
3984                 return 0;
3985         if (tc < adapter->num_tc) {
3986                 if (!filter->f.data.tcp_spec.dst_port) {
3987                         dev_err(&adapter->pdev->dev,
3988                                 "Specify destination port to redirect to traffic class other than TC0\n");
3989                         return -EINVAL;
3990                 }
3991         }
3992         /* redirect to a traffic class on the same device */
3993         filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
3994         filter->f.action_meta = tc;
3995         return 0;
3996 }
3997
3998 /**
3999  * iavf_find_cf - Find the cloud filter in the list
4000  * @adapter: Board private structure
4001  * @cookie: filter specific cookie
4002  *
4003  * Returns ptr to the filter object or NULL. Must be called while holding the
4004  * cloud_filter_list_lock.
4005  */
4006 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
4007                                               unsigned long *cookie)
4008 {
4009         struct iavf_cloud_filter *filter = NULL;
4010
4011         if (!cookie)
4012                 return NULL;
4013
4014         list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
4015                 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
4016                         return filter;
4017         }
4018         return NULL;
4019 }
4020
4021 /**
4022  * iavf_configure_clsflower - Add tc flower filters
4023  * @adapter: board private structure
4024  * @cls_flower: Pointer to struct flow_cls_offload
4025  */
4026 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
4027                                     struct flow_cls_offload *cls_flower)
4028 {
4029         int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
4030         struct iavf_cloud_filter *filter = NULL;
4031         int err = -EINVAL, count = 50;
4032
4033         if (tc < 0) {
4034                 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
4035                 return -EINVAL;
4036         }
4037
4038         filter = kzalloc(sizeof(*filter), GFP_KERNEL);
4039         if (!filter)
4040                 return -ENOMEM;
4041
4042         while (!mutex_trylock(&adapter->crit_lock)) {
4043                 if (--count == 0) {
4044                         kfree(filter);
4045                         return err;
4046                 }
4047                 udelay(1);
4048         }
4049
4050         filter->cookie = cls_flower->cookie;
4051
4052         /* bail out here if filter already exists */
4053         spin_lock_bh(&adapter->cloud_filter_list_lock);
4054         if (iavf_find_cf(adapter, &cls_flower->cookie)) {
4055                 dev_err(&adapter->pdev->dev, "Failed to add TC Flower filter, it already exists\n");
4056                 err = -EEXIST;
4057                 goto spin_unlock;
4058         }
4059         spin_unlock_bh(&adapter->cloud_filter_list_lock);
4060
4061         /* set the mask to all zeroes to begin with */
4062         memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
4063         /* start out with flow type and eth type IPv4 to begin with */
4064         filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
4065         err = iavf_parse_cls_flower(adapter, cls_flower, filter);
4066         if (err)
4067                 goto err;
4068
4069         err = iavf_handle_tclass(adapter, tc, filter);
4070         if (err)
4071                 goto err;
4072
4073         /* add filter to the list */
4074         spin_lock_bh(&adapter->cloud_filter_list_lock);
4075         list_add_tail(&filter->list, &adapter->cloud_filter_list);
4076         adapter->num_cloud_filters++;
4077         filter->add = true;
4078         adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
4079 spin_unlock:
4080         spin_unlock_bh(&adapter->cloud_filter_list_lock);
4081 err:
4082         if (err)
4083                 kfree(filter);
4084
4085         mutex_unlock(&adapter->crit_lock);
4086         return err;
4087 }
4088
4089 /**
4090  * iavf_delete_clsflower - Remove tc flower filters
4091  * @adapter: board private structure
4092  * @cls_flower: Pointer to struct flow_cls_offload
4093  */
4094 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
4095                                  struct flow_cls_offload *cls_flower)
4096 {
4097         struct iavf_cloud_filter *filter = NULL;
4098         int err = 0;
4099
4100         spin_lock_bh(&adapter->cloud_filter_list_lock);
4101         filter = iavf_find_cf(adapter, &cls_flower->cookie);
4102         if (filter) {
4103                 filter->del = true;
4104                 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
4105         } else {
4106                 err = -EINVAL;
4107         }
4108         spin_unlock_bh(&adapter->cloud_filter_list_lock);
4109
4110         return err;
4111 }
4112
4113 /**
4114  * iavf_setup_tc_cls_flower - flower classifier offloads
4115  * @adapter: board private structure
4116  * @cls_flower: pointer to flow_cls_offload struct with flow info
4117  */
4118 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
4119                                     struct flow_cls_offload *cls_flower)
4120 {
4121         switch (cls_flower->command) {
4122         case FLOW_CLS_REPLACE:
4123                 return iavf_configure_clsflower(adapter, cls_flower);
4124         case FLOW_CLS_DESTROY:
4125                 return iavf_delete_clsflower(adapter, cls_flower);
4126         case FLOW_CLS_STATS:
4127                 return -EOPNOTSUPP;
4128         default:
4129                 return -EOPNOTSUPP;
4130         }
4131 }
4132
4133 /**
4134  * iavf_setup_tc_block_cb - block callback for tc
4135  * @type: type of offload
4136  * @type_data: offload data
4137  * @cb_priv:
4138  *
4139  * This function is the block callback for traffic classes
4140  **/
4141 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
4142                                   void *cb_priv)
4143 {
4144         struct iavf_adapter *adapter = cb_priv;
4145
4146         if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
4147                 return -EOPNOTSUPP;
4148
4149         switch (type) {
4150         case TC_SETUP_CLSFLOWER:
4151                 return iavf_setup_tc_cls_flower(cb_priv, type_data);
4152         default:
4153                 return -EOPNOTSUPP;
4154         }
4155 }
4156
4157 static LIST_HEAD(iavf_block_cb_list);
4158
4159 /**
4160  * iavf_setup_tc - configure multiple traffic classes
4161  * @netdev: network interface device structure
4162  * @type: type of offload
4163  * @type_data: tc offload data
4164  *
4165  * This function is the callback to ndo_setup_tc in the
4166  * netdev_ops.
4167  *
4168  * Returns 0 on success
4169  **/
4170 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
4171                          void *type_data)
4172 {
4173         struct iavf_adapter *adapter = netdev_priv(netdev);
4174
4175         switch (type) {
4176         case TC_SETUP_QDISC_MQPRIO:
4177                 return __iavf_setup_tc(netdev, type_data);
4178         case TC_SETUP_BLOCK:
4179                 return flow_block_cb_setup_simple(type_data,
4180                                                   &iavf_block_cb_list,
4181                                                   iavf_setup_tc_block_cb,
4182                                                   adapter, adapter, true);
4183         default:
4184                 return -EOPNOTSUPP;
4185         }
4186 }
4187
4188 /**
4189  * iavf_open - Called when a network interface is made active
4190  * @netdev: network interface device structure
4191  *
4192  * Returns 0 on success, negative value on failure
4193  *
4194  * The open entry point is called when a network interface is made
4195  * active by the system (IFF_UP).  At this point all resources needed
4196  * for transmit and receive operations are allocated, the interrupt
4197  * handler is registered with the OS, the watchdog is started,
4198  * and the stack is notified that the interface is ready.
4199  **/
4200 static int iavf_open(struct net_device *netdev)
4201 {
4202         struct iavf_adapter *adapter = netdev_priv(netdev);
4203         int err;
4204
4205         if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
4206                 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
4207                 return -EIO;
4208         }
4209
4210         while (!mutex_trylock(&adapter->crit_lock)) {
4211                 /* If we are in __IAVF_INIT_CONFIG_ADAPTER state the crit_lock
4212                  * is already taken and iavf_open is called from an upper
4213                  * device's notifier reacting on NETDEV_REGISTER event.
4214                  * We have to leave here to avoid dead lock.
4215                  */
4216                 if (adapter->state == __IAVF_INIT_CONFIG_ADAPTER)
4217                         return -EBUSY;
4218
4219                 usleep_range(500, 1000);
4220         }
4221
4222         if (adapter->state != __IAVF_DOWN) {
4223                 err = -EBUSY;
4224                 goto err_unlock;
4225         }
4226
4227         if (adapter->state == __IAVF_RUNNING &&
4228             !test_bit(__IAVF_VSI_DOWN, adapter->vsi.state)) {
4229                 dev_dbg(&adapter->pdev->dev, "VF is already open.\n");
4230                 err = 0;
4231                 goto err_unlock;
4232         }
4233
4234         /* allocate transmit descriptors */
4235         err = iavf_setup_all_tx_resources(adapter);
4236         if (err)
4237                 goto err_setup_tx;
4238
4239         /* allocate receive descriptors */
4240         err = iavf_setup_all_rx_resources(adapter);
4241         if (err)
4242                 goto err_setup_rx;
4243
4244         /* clear any pending interrupts, may auto mask */
4245         err = iavf_request_traffic_irqs(adapter, netdev->name);
4246         if (err)
4247                 goto err_req_irq;
4248
4249         spin_lock_bh(&adapter->mac_vlan_list_lock);
4250
4251         iavf_add_filter(adapter, adapter->hw.mac.addr);
4252
4253         spin_unlock_bh(&adapter->mac_vlan_list_lock);
4254
4255         /* Restore VLAN filters that were removed with IFF_DOWN */
4256         iavf_restore_filters(adapter);
4257
4258         iavf_configure(adapter);
4259
4260         iavf_up_complete(adapter);
4261
4262         iavf_irq_enable(adapter, true);
4263
4264         mutex_unlock(&adapter->crit_lock);
4265
4266         return 0;
4267
4268 err_req_irq:
4269         iavf_down(adapter);
4270         iavf_free_traffic_irqs(adapter);
4271 err_setup_rx:
4272         iavf_free_all_rx_resources(adapter);
4273 err_setup_tx:
4274         iavf_free_all_tx_resources(adapter);
4275 err_unlock:
4276         mutex_unlock(&adapter->crit_lock);
4277
4278         return err;
4279 }
4280
4281 /**
4282  * iavf_close - Disables a network interface
4283  * @netdev: network interface device structure
4284  *
4285  * Returns 0, this is not allowed to fail
4286  *
4287  * The close entry point is called when an interface is de-activated
4288  * by the OS.  The hardware is still under the drivers control, but
4289  * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
4290  * are freed, along with all transmit and receive resources.
4291  **/
4292 static int iavf_close(struct net_device *netdev)
4293 {
4294         struct iavf_adapter *adapter = netdev_priv(netdev);
4295         u64 aq_to_restore;
4296         int status;
4297
4298         mutex_lock(&adapter->crit_lock);
4299
4300         if (adapter->state <= __IAVF_DOWN_PENDING) {
4301                 mutex_unlock(&adapter->crit_lock);
4302                 return 0;
4303         }
4304
4305         set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
4306         if (CLIENT_ENABLED(adapter))
4307                 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
4308         /* We cannot send IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS before
4309          * IAVF_FLAG_AQ_DISABLE_QUEUES because in such case there is rtnl
4310          * deadlock with adminq_task() until iavf_close timeouts. We must send
4311          * IAVF_FLAG_AQ_GET_CONFIG before IAVF_FLAG_AQ_DISABLE_QUEUES to make
4312          * disable queues possible for vf. Give only necessary flags to
4313          * iavf_down and save other to set them right before iavf_close()
4314          * returns, when IAVF_FLAG_AQ_DISABLE_QUEUES will be already sent and
4315          * iavf will be in DOWN state.
4316          */
4317         aq_to_restore = adapter->aq_required;
4318         adapter->aq_required &= IAVF_FLAG_AQ_GET_CONFIG;
4319
4320         /* Remove flags which we do not want to send after close or we want to
4321          * send before disable queues.
4322          */
4323         aq_to_restore &= ~(IAVF_FLAG_AQ_GET_CONFIG              |
4324                            IAVF_FLAG_AQ_ENABLE_QUEUES           |
4325                            IAVF_FLAG_AQ_CONFIGURE_QUEUES        |
4326                            IAVF_FLAG_AQ_ADD_VLAN_FILTER         |
4327                            IAVF_FLAG_AQ_ADD_MAC_FILTER          |
4328                            IAVF_FLAG_AQ_ADD_CLOUD_FILTER        |
4329                            IAVF_FLAG_AQ_ADD_FDIR_FILTER         |
4330                            IAVF_FLAG_AQ_ADD_ADV_RSS_CFG);
4331
4332         iavf_down(adapter);
4333         iavf_change_state(adapter, __IAVF_DOWN_PENDING);
4334         iavf_free_traffic_irqs(adapter);
4335
4336         mutex_unlock(&adapter->crit_lock);
4337
4338         /* We explicitly don't free resources here because the hardware is
4339          * still active and can DMA into memory. Resources are cleared in
4340          * iavf_virtchnl_completion() after we get confirmation from the PF
4341          * driver that the rings have been stopped.
4342          *
4343          * Also, we wait for state to transition to __IAVF_DOWN before
4344          * returning. State change occurs in iavf_virtchnl_completion() after
4345          * VF resources are released (which occurs after PF driver processes and
4346          * responds to admin queue commands).
4347          */
4348
4349         status = wait_event_timeout(adapter->down_waitqueue,
4350                                     adapter->state == __IAVF_DOWN,
4351                                     msecs_to_jiffies(500));
4352         if (!status)
4353                 netdev_warn(netdev, "Device resources not yet released\n");
4354
4355         mutex_lock(&adapter->crit_lock);
4356         adapter->aq_required |= aq_to_restore;
4357         mutex_unlock(&adapter->crit_lock);
4358         return 0;
4359 }
4360
4361 /**
4362  * iavf_change_mtu - Change the Maximum Transfer Unit
4363  * @netdev: network interface device structure
4364  * @new_mtu: new value for maximum frame size
4365  *
4366  * Returns 0 on success, negative on failure
4367  **/
4368 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
4369 {
4370         struct iavf_adapter *adapter = netdev_priv(netdev);
4371         int ret = 0;
4372
4373         netdev_dbg(netdev, "changing MTU from %d to %d\n",
4374                    netdev->mtu, new_mtu);
4375         netdev->mtu = new_mtu;
4376         if (CLIENT_ENABLED(adapter)) {
4377                 iavf_notify_client_l2_params(&adapter->vsi);
4378                 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
4379         }
4380
4381         if (netif_running(netdev)) {
4382                 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
4383                 ret = iavf_wait_for_reset(adapter);
4384                 if (ret < 0)
4385                         netdev_warn(netdev, "MTU change interrupted waiting for reset");
4386                 else if (ret)
4387                         netdev_warn(netdev, "MTU change timed out waiting for reset");
4388         }
4389
4390         return ret;
4391 }
4392
4393 #define NETIF_VLAN_OFFLOAD_FEATURES     (NETIF_F_HW_VLAN_CTAG_RX | \
4394                                          NETIF_F_HW_VLAN_CTAG_TX | \
4395                                          NETIF_F_HW_VLAN_STAG_RX | \
4396                                          NETIF_F_HW_VLAN_STAG_TX)
4397
4398 /**
4399  * iavf_set_features - set the netdev feature flags
4400  * @netdev: ptr to the netdev being adjusted
4401  * @features: the feature set that the stack is suggesting
4402  * Note: expects to be called while under rtnl_lock()
4403  **/
4404 static int iavf_set_features(struct net_device *netdev,
4405                              netdev_features_t features)
4406 {
4407         struct iavf_adapter *adapter = netdev_priv(netdev);
4408
4409         /* trigger update on any VLAN feature change */
4410         if ((netdev->features & NETIF_VLAN_OFFLOAD_FEATURES) ^
4411             (features & NETIF_VLAN_OFFLOAD_FEATURES))
4412                 iavf_set_vlan_offload_features(adapter, netdev->features,
4413                                                features);
4414
4415         return 0;
4416 }
4417
4418 /**
4419  * iavf_features_check - Validate encapsulated packet conforms to limits
4420  * @skb: skb buff
4421  * @dev: This physical port's netdev
4422  * @features: Offload features that the stack believes apply
4423  **/
4424 static netdev_features_t iavf_features_check(struct sk_buff *skb,
4425                                              struct net_device *dev,
4426                                              netdev_features_t features)
4427 {
4428         size_t len;
4429
4430         /* No point in doing any of this if neither checksum nor GSO are
4431          * being requested for this frame.  We can rule out both by just
4432          * checking for CHECKSUM_PARTIAL
4433          */
4434         if (skb->ip_summed != CHECKSUM_PARTIAL)
4435                 return features;
4436
4437         /* We cannot support GSO if the MSS is going to be less than
4438          * 64 bytes.  If it is then we need to drop support for GSO.
4439          */
4440         if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4441                 features &= ~NETIF_F_GSO_MASK;
4442
4443         /* MACLEN can support at most 63 words */
4444         len = skb_network_header(skb) - skb->data;
4445         if (len & ~(63 * 2))
4446                 goto out_err;
4447
4448         /* IPLEN and EIPLEN can support at most 127 dwords */
4449         len = skb_transport_header(skb) - skb_network_header(skb);
4450         if (len & ~(127 * 4))
4451                 goto out_err;
4452
4453         if (skb->encapsulation) {
4454                 /* L4TUNLEN can support 127 words */
4455                 len = skb_inner_network_header(skb) - skb_transport_header(skb);
4456                 if (len & ~(127 * 2))
4457                         goto out_err;
4458
4459                 /* IPLEN can support at most 127 dwords */
4460                 len = skb_inner_transport_header(skb) -
4461                       skb_inner_network_header(skb);
4462                 if (len & ~(127 * 4))
4463                         goto out_err;
4464         }
4465
4466         /* No need to validate L4LEN as TCP is the only protocol with a
4467          * flexible value and we support all possible values supported
4468          * by TCP, which is at most 15 dwords
4469          */
4470
4471         return features;
4472 out_err:
4473         return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4474 }
4475
4476 /**
4477  * iavf_get_netdev_vlan_hw_features - get NETDEV VLAN features that can toggle on/off
4478  * @adapter: board private structure
4479  *
4480  * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4481  * were negotiated determine the VLAN features that can be toggled on and off.
4482  **/
4483 static netdev_features_t
4484 iavf_get_netdev_vlan_hw_features(struct iavf_adapter *adapter)
4485 {
4486         netdev_features_t hw_features = 0;
4487
4488         if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4489                 return hw_features;
4490
4491         /* Enable VLAN features if supported */
4492         if (VLAN_ALLOWED(adapter)) {
4493                 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
4494                                 NETIF_F_HW_VLAN_CTAG_RX);
4495         } else if (VLAN_V2_ALLOWED(adapter)) {
4496                 struct virtchnl_vlan_caps *vlan_v2_caps =
4497                         &adapter->vlan_v2_caps;
4498                 struct virtchnl_vlan_supported_caps *stripping_support =
4499                         &vlan_v2_caps->offloads.stripping_support;
4500                 struct virtchnl_vlan_supported_caps *insertion_support =
4501                         &vlan_v2_caps->offloads.insertion_support;
4502
4503                 if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4504                     stripping_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4505                         if (stripping_support->outer &
4506                             VIRTCHNL_VLAN_ETHERTYPE_8100)
4507                                 hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4508                         if (stripping_support->outer &
4509                             VIRTCHNL_VLAN_ETHERTYPE_88A8)
4510                                 hw_features |= NETIF_F_HW_VLAN_STAG_RX;
4511                 } else if (stripping_support->inner !=
4512                            VIRTCHNL_VLAN_UNSUPPORTED &&
4513                            stripping_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4514                         if (stripping_support->inner &
4515                             VIRTCHNL_VLAN_ETHERTYPE_8100)
4516                                 hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4517                 }
4518
4519                 if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4520                     insertion_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4521                         if (insertion_support->outer &
4522                             VIRTCHNL_VLAN_ETHERTYPE_8100)
4523                                 hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4524                         if (insertion_support->outer &
4525                             VIRTCHNL_VLAN_ETHERTYPE_88A8)
4526                                 hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4527                 } else if (insertion_support->inner &&
4528                            insertion_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4529                         if (insertion_support->inner &
4530                             VIRTCHNL_VLAN_ETHERTYPE_8100)
4531                                 hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4532                 }
4533         }
4534
4535         return hw_features;
4536 }
4537
4538 /**
4539  * iavf_get_netdev_vlan_features - get the enabled NETDEV VLAN fetures
4540  * @adapter: board private structure
4541  *
4542  * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4543  * were negotiated determine the VLAN features that are enabled by default.
4544  **/
4545 static netdev_features_t
4546 iavf_get_netdev_vlan_features(struct iavf_adapter *adapter)
4547 {
4548         netdev_features_t features = 0;
4549
4550         if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4551                 return features;
4552
4553         if (VLAN_ALLOWED(adapter)) {
4554                 features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4555                         NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
4556         } else if (VLAN_V2_ALLOWED(adapter)) {
4557                 struct virtchnl_vlan_caps *vlan_v2_caps =
4558                         &adapter->vlan_v2_caps;
4559                 struct virtchnl_vlan_supported_caps *filtering_support =
4560                         &vlan_v2_caps->filtering.filtering_support;
4561                 struct virtchnl_vlan_supported_caps *stripping_support =
4562                         &vlan_v2_caps->offloads.stripping_support;
4563                 struct virtchnl_vlan_supported_caps *insertion_support =
4564                         &vlan_v2_caps->offloads.insertion_support;
4565                 u32 ethertype_init;
4566
4567                 /* give priority to outer stripping and don't support both outer
4568                  * and inner stripping
4569                  */
4570                 ethertype_init = vlan_v2_caps->offloads.ethertype_init;
4571                 if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4572                         if (stripping_support->outer &
4573                             VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4574                             ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4575                                 features |= NETIF_F_HW_VLAN_CTAG_RX;
4576                         else if (stripping_support->outer &
4577                                  VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4578                                  ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4579                                 features |= NETIF_F_HW_VLAN_STAG_RX;
4580                 } else if (stripping_support->inner !=
4581                            VIRTCHNL_VLAN_UNSUPPORTED) {
4582                         if (stripping_support->inner &
4583                             VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4584                             ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4585                                 features |= NETIF_F_HW_VLAN_CTAG_RX;
4586                 }
4587
4588                 /* give priority to outer insertion and don't support both outer
4589                  * and inner insertion
4590                  */
4591                 if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4592                         if (insertion_support->outer &
4593                             VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4594                             ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4595                                 features |= NETIF_F_HW_VLAN_CTAG_TX;
4596                         else if (insertion_support->outer &
4597                                  VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4598                                  ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4599                                 features |= NETIF_F_HW_VLAN_STAG_TX;
4600                 } else if (insertion_support->inner !=
4601                            VIRTCHNL_VLAN_UNSUPPORTED) {
4602                         if (insertion_support->inner &
4603                             VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4604                             ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4605                                 features |= NETIF_F_HW_VLAN_CTAG_TX;
4606                 }
4607
4608                 /* give priority to outer filtering and don't bother if both
4609                  * outer and inner filtering are enabled
4610                  */
4611                 ethertype_init = vlan_v2_caps->filtering.ethertype_init;
4612                 if (filtering_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4613                         if (filtering_support->outer &
4614                             VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4615                             ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4616                                 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4617                         if (filtering_support->outer &
4618                             VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4619                             ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4620                                 features |= NETIF_F_HW_VLAN_STAG_FILTER;
4621                 } else if (filtering_support->inner !=
4622                            VIRTCHNL_VLAN_UNSUPPORTED) {
4623                         if (filtering_support->inner &
4624                             VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4625                             ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4626                                 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4627                         if (filtering_support->inner &
4628                             VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4629                             ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4630                                 features |= NETIF_F_HW_VLAN_STAG_FILTER;
4631                 }
4632         }
4633
4634         return features;
4635 }
4636
4637 #define IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested, allowed, feature_bit) \
4638         (!(((requested) & (feature_bit)) && \
4639            !((allowed) & (feature_bit))))
4640
4641 /**
4642  * iavf_fix_netdev_vlan_features - fix NETDEV VLAN features based on support
4643  * @adapter: board private structure
4644  * @requested_features: stack requested NETDEV features
4645  **/
4646 static netdev_features_t
4647 iavf_fix_netdev_vlan_features(struct iavf_adapter *adapter,
4648                               netdev_features_t requested_features)
4649 {
4650         netdev_features_t allowed_features;
4651
4652         allowed_features = iavf_get_netdev_vlan_hw_features(adapter) |
4653                 iavf_get_netdev_vlan_features(adapter);
4654
4655         if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4656                                               allowed_features,
4657                                               NETIF_F_HW_VLAN_CTAG_TX))
4658                 requested_features &= ~NETIF_F_HW_VLAN_CTAG_TX;
4659
4660         if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4661                                               allowed_features,
4662                                               NETIF_F_HW_VLAN_CTAG_RX))
4663                 requested_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
4664
4665         if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4666                                               allowed_features,
4667                                               NETIF_F_HW_VLAN_STAG_TX))
4668                 requested_features &= ~NETIF_F_HW_VLAN_STAG_TX;
4669         if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4670                                               allowed_features,
4671                                               NETIF_F_HW_VLAN_STAG_RX))
4672                 requested_features &= ~NETIF_F_HW_VLAN_STAG_RX;
4673
4674         if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4675                                               allowed_features,
4676                                               NETIF_F_HW_VLAN_CTAG_FILTER))
4677                 requested_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
4678
4679         if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4680                                               allowed_features,
4681                                               NETIF_F_HW_VLAN_STAG_FILTER))
4682                 requested_features &= ~NETIF_F_HW_VLAN_STAG_FILTER;
4683
4684         if ((requested_features &
4685              (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
4686             (requested_features &
4687              (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX)) &&
4688             adapter->vlan_v2_caps.offloads.ethertype_match ==
4689             VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION) {
4690                 netdev_warn(adapter->netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
4691                 requested_features &= ~(NETIF_F_HW_VLAN_STAG_RX |
4692                                         NETIF_F_HW_VLAN_STAG_TX);
4693         }
4694
4695         return requested_features;
4696 }
4697
4698 /**
4699  * iavf_fix_features - fix up the netdev feature bits
4700  * @netdev: our net device
4701  * @features: desired feature bits
4702  *
4703  * Returns fixed-up features bits
4704  **/
4705 static netdev_features_t iavf_fix_features(struct net_device *netdev,
4706                                            netdev_features_t features)
4707 {
4708         struct iavf_adapter *adapter = netdev_priv(netdev);
4709
4710         return iavf_fix_netdev_vlan_features(adapter, features);
4711 }
4712
4713 static const struct net_device_ops iavf_netdev_ops = {
4714         .ndo_open               = iavf_open,
4715         .ndo_stop               = iavf_close,
4716         .ndo_start_xmit         = iavf_xmit_frame,
4717         .ndo_set_rx_mode        = iavf_set_rx_mode,
4718         .ndo_validate_addr      = eth_validate_addr,
4719         .ndo_set_mac_address    = iavf_set_mac,
4720         .ndo_change_mtu         = iavf_change_mtu,
4721         .ndo_tx_timeout         = iavf_tx_timeout,
4722         .ndo_vlan_rx_add_vid    = iavf_vlan_rx_add_vid,
4723         .ndo_vlan_rx_kill_vid   = iavf_vlan_rx_kill_vid,
4724         .ndo_features_check     = iavf_features_check,
4725         .ndo_fix_features       = iavf_fix_features,
4726         .ndo_set_features       = iavf_set_features,
4727         .ndo_setup_tc           = iavf_setup_tc,
4728 };
4729
4730 /**
4731  * iavf_check_reset_complete - check that VF reset is complete
4732  * @hw: pointer to hw struct
4733  *
4734  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
4735  **/
4736 static int iavf_check_reset_complete(struct iavf_hw *hw)
4737 {
4738         u32 rstat;
4739         int i;
4740
4741         for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
4742                 rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
4743                              IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
4744                 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
4745                     (rstat == VIRTCHNL_VFR_COMPLETED))
4746                         return 0;
4747                 usleep_range(10, 20);
4748         }
4749         return -EBUSY;
4750 }
4751
4752 /**
4753  * iavf_process_config - Process the config information we got from the PF
4754  * @adapter: board private structure
4755  *
4756  * Verify that we have a valid config struct, and set up our netdev features
4757  * and our VSI struct.
4758  **/
4759 int iavf_process_config(struct iavf_adapter *adapter)
4760 {
4761         struct virtchnl_vf_resource *vfres = adapter->vf_res;
4762         netdev_features_t hw_vlan_features, vlan_features;
4763         struct net_device *netdev = adapter->netdev;
4764         netdev_features_t hw_enc_features;
4765         netdev_features_t hw_features;
4766
4767         hw_enc_features = NETIF_F_SG                    |
4768                           NETIF_F_IP_CSUM               |
4769                           NETIF_F_IPV6_CSUM             |
4770                           NETIF_F_HIGHDMA               |
4771                           NETIF_F_SOFT_FEATURES |
4772                           NETIF_F_TSO                   |
4773                           NETIF_F_TSO_ECN               |
4774                           NETIF_F_TSO6                  |
4775                           NETIF_F_SCTP_CRC              |
4776                           NETIF_F_RXHASH                |
4777                           NETIF_F_RXCSUM                |
4778                           0;
4779
4780         /* advertise to stack only if offloads for encapsulated packets is
4781          * supported
4782          */
4783         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
4784                 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL       |
4785                                    NETIF_F_GSO_GRE              |
4786                                    NETIF_F_GSO_GRE_CSUM         |
4787                                    NETIF_F_GSO_IPXIP4           |
4788                                    NETIF_F_GSO_IPXIP6           |
4789                                    NETIF_F_GSO_UDP_TUNNEL_CSUM  |
4790                                    NETIF_F_GSO_PARTIAL          |
4791                                    0;
4792
4793                 if (!(vfres->vf_cap_flags &
4794                       VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
4795                         netdev->gso_partial_features |=
4796                                 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4797
4798                 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
4799                 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
4800                 netdev->hw_enc_features |= hw_enc_features;
4801         }
4802         /* record features VLANs can make use of */
4803         netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
4804
4805         /* Write features and hw_features separately to avoid polluting
4806          * with, or dropping, features that are set when we registered.
4807          */
4808         hw_features = hw_enc_features;
4809
4810         /* get HW VLAN features that can be toggled */
4811         hw_vlan_features = iavf_get_netdev_vlan_hw_features(adapter);
4812
4813         /* Enable cloud filter if ADQ is supported */
4814         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
4815                 hw_features |= NETIF_F_HW_TC;
4816         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_USO)
4817                 hw_features |= NETIF_F_GSO_UDP_L4;
4818
4819         netdev->hw_features |= hw_features | hw_vlan_features;
4820         vlan_features = iavf_get_netdev_vlan_features(adapter);
4821
4822         netdev->features |= hw_features | vlan_features;
4823
4824         if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
4825                 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4826
4827         netdev->priv_flags |= IFF_UNICAST_FLT;
4828
4829         /* Do not turn on offloads when they are requested to be turned off.
4830          * TSO needs minimum 576 bytes to work correctly.
4831          */
4832         if (netdev->wanted_features) {
4833                 if (!(netdev->wanted_features & NETIF_F_TSO) ||
4834                     netdev->mtu < 576)
4835                         netdev->features &= ~NETIF_F_TSO;
4836                 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
4837                     netdev->mtu < 576)
4838                         netdev->features &= ~NETIF_F_TSO6;
4839                 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
4840                         netdev->features &= ~NETIF_F_TSO_ECN;
4841                 if (!(netdev->wanted_features & NETIF_F_GRO))
4842                         netdev->features &= ~NETIF_F_GRO;
4843                 if (!(netdev->wanted_features & NETIF_F_GSO))
4844                         netdev->features &= ~NETIF_F_GSO;
4845         }
4846
4847         return 0;
4848 }
4849
4850 /**
4851  * iavf_shutdown - Shutdown the device in preparation for a reboot
4852  * @pdev: pci device structure
4853  **/
4854 static void iavf_shutdown(struct pci_dev *pdev)
4855 {
4856         struct iavf_adapter *adapter = iavf_pdev_to_adapter(pdev);
4857         struct net_device *netdev = adapter->netdev;
4858
4859         netif_device_detach(netdev);
4860
4861         if (netif_running(netdev))
4862                 iavf_close(netdev);
4863
4864         if (iavf_lock_timeout(&adapter->crit_lock, 5000))
4865                 dev_warn(&adapter->pdev->dev, "%s: failed to acquire crit_lock\n", __func__);
4866         /* Prevent the watchdog from running. */
4867         iavf_change_state(adapter, __IAVF_REMOVE);
4868         adapter->aq_required = 0;
4869         mutex_unlock(&adapter->crit_lock);
4870
4871 #ifdef CONFIG_PM
4872         pci_save_state(pdev);
4873
4874 #endif
4875         pci_disable_device(pdev);
4876 }
4877
4878 /**
4879  * iavf_probe - Device Initialization Routine
4880  * @pdev: PCI device information struct
4881  * @ent: entry in iavf_pci_tbl
4882  *
4883  * Returns 0 on success, negative on failure
4884  *
4885  * iavf_probe initializes an adapter identified by a pci_dev structure.
4886  * The OS initialization, configuring of the adapter private structure,
4887  * and a hardware reset occur.
4888  **/
4889 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4890 {
4891         struct net_device *netdev;
4892         struct iavf_adapter *adapter = NULL;
4893         struct iavf_hw *hw = NULL;
4894         int err;
4895
4896         err = pci_enable_device(pdev);
4897         if (err)
4898                 return err;
4899
4900         err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4901         if (err) {
4902                 dev_err(&pdev->dev,
4903                         "DMA configuration failed: 0x%x\n", err);
4904                 goto err_dma;
4905         }
4906
4907         err = pci_request_regions(pdev, iavf_driver_name);
4908         if (err) {
4909                 dev_err(&pdev->dev,
4910                         "pci_request_regions failed 0x%x\n", err);
4911                 goto err_pci_reg;
4912         }
4913
4914         pci_set_master(pdev);
4915
4916         netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
4917                                    IAVF_MAX_REQ_QUEUES);
4918         if (!netdev) {
4919                 err = -ENOMEM;
4920                 goto err_alloc_etherdev;
4921         }
4922
4923         SET_NETDEV_DEV(netdev, &pdev->dev);
4924
4925         pci_set_drvdata(pdev, netdev);
4926         adapter = netdev_priv(netdev);
4927
4928         adapter->netdev = netdev;
4929         adapter->pdev = pdev;
4930
4931         hw = &adapter->hw;
4932         hw->back = adapter;
4933
4934         adapter->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
4935                                               iavf_driver_name);
4936         if (!adapter->wq) {
4937                 err = -ENOMEM;
4938                 goto err_alloc_wq;
4939         }
4940
4941         adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
4942         iavf_change_state(adapter, __IAVF_STARTUP);
4943
4944         /* Call save state here because it relies on the adapter struct. */
4945         pci_save_state(pdev);
4946
4947         hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4948                               pci_resource_len(pdev, 0));
4949         if (!hw->hw_addr) {
4950                 err = -EIO;
4951                 goto err_ioremap;
4952         }
4953         hw->vendor_id = pdev->vendor;
4954         hw->device_id = pdev->device;
4955         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4956         hw->subsystem_vendor_id = pdev->subsystem_vendor;
4957         hw->subsystem_device_id = pdev->subsystem_device;
4958         hw->bus.device = PCI_SLOT(pdev->devfn);
4959         hw->bus.func = PCI_FUNC(pdev->devfn);
4960         hw->bus.bus_id = pdev->bus->number;
4961
4962         /* set up the locks for the AQ, do this only once in probe
4963          * and destroy them only once in remove
4964          */
4965         mutex_init(&adapter->crit_lock);
4966         mutex_init(&adapter->client_lock);
4967         mutex_init(&hw->aq.asq_mutex);
4968         mutex_init(&hw->aq.arq_mutex);
4969
4970         spin_lock_init(&adapter->mac_vlan_list_lock);
4971         spin_lock_init(&adapter->cloud_filter_list_lock);
4972         spin_lock_init(&adapter->fdir_fltr_lock);
4973         spin_lock_init(&adapter->adv_rss_lock);
4974
4975         INIT_LIST_HEAD(&adapter->mac_filter_list);
4976         INIT_LIST_HEAD(&adapter->vlan_filter_list);
4977         INIT_LIST_HEAD(&adapter->cloud_filter_list);
4978         INIT_LIST_HEAD(&adapter->fdir_list_head);
4979         INIT_LIST_HEAD(&adapter->adv_rss_list_head);
4980
4981         INIT_WORK(&adapter->reset_task, iavf_reset_task);
4982         INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
4983         INIT_WORK(&adapter->finish_config, iavf_finish_config);
4984         INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
4985         INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
4986         queue_delayed_work(adapter->wq, &adapter->watchdog_task,
4987                            msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
4988
4989         /* Setup the wait queue for indicating transition to down status */
4990         init_waitqueue_head(&adapter->down_waitqueue);
4991
4992         /* Setup the wait queue for indicating transition to running state */
4993         init_waitqueue_head(&adapter->reset_waitqueue);
4994
4995         /* Setup the wait queue for indicating virtchannel events */
4996         init_waitqueue_head(&adapter->vc_waitqueue);
4997
4998         return 0;
4999
5000 err_ioremap:
5001         destroy_workqueue(adapter->wq);
5002 err_alloc_wq:
5003         free_netdev(netdev);
5004 err_alloc_etherdev:
5005         pci_release_regions(pdev);
5006 err_pci_reg:
5007 err_dma:
5008         pci_disable_device(pdev);
5009         return err;
5010 }
5011
5012 /**
5013  * iavf_suspend - Power management suspend routine
5014  * @dev_d: device info pointer
5015  *
5016  * Called when the system (VM) is entering sleep/suspend.
5017  **/
5018 static int __maybe_unused iavf_suspend(struct device *dev_d)
5019 {
5020         struct net_device *netdev = dev_get_drvdata(dev_d);
5021         struct iavf_adapter *adapter = netdev_priv(netdev);
5022
5023         netif_device_detach(netdev);
5024
5025         while (!mutex_trylock(&adapter->crit_lock))
5026                 usleep_range(500, 1000);
5027
5028         if (netif_running(netdev)) {
5029                 rtnl_lock();
5030                 iavf_down(adapter);
5031                 rtnl_unlock();
5032         }
5033         iavf_free_misc_irq(adapter);
5034         iavf_reset_interrupt_capability(adapter);
5035
5036         mutex_unlock(&adapter->crit_lock);
5037
5038         return 0;
5039 }
5040
5041 /**
5042  * iavf_resume - Power management resume routine
5043  * @dev_d: device info pointer
5044  *
5045  * Called when the system (VM) is resumed from sleep/suspend.
5046  **/
5047 static int __maybe_unused iavf_resume(struct device *dev_d)
5048 {
5049         struct pci_dev *pdev = to_pci_dev(dev_d);
5050         struct iavf_adapter *adapter;
5051         u32 err;
5052
5053         adapter = iavf_pdev_to_adapter(pdev);
5054
5055         pci_set_master(pdev);
5056
5057         rtnl_lock();
5058         err = iavf_set_interrupt_capability(adapter);
5059         if (err) {
5060                 rtnl_unlock();
5061                 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
5062                 return err;
5063         }
5064         err = iavf_request_misc_irq(adapter);
5065         rtnl_unlock();
5066         if (err) {
5067                 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
5068                 return err;
5069         }
5070
5071         queue_work(adapter->wq, &adapter->reset_task);
5072
5073         netif_device_attach(adapter->netdev);
5074
5075         return err;
5076 }
5077
5078 /**
5079  * iavf_remove - Device Removal Routine
5080  * @pdev: PCI device information struct
5081  *
5082  * iavf_remove is called by the PCI subsystem to alert the driver
5083  * that it should release a PCI device.  The could be caused by a
5084  * Hot-Plug event, or because the driver is going to be removed from
5085  * memory.
5086  **/
5087 static void iavf_remove(struct pci_dev *pdev)
5088 {
5089         struct iavf_adapter *adapter = iavf_pdev_to_adapter(pdev);
5090         struct iavf_fdir_fltr *fdir, *fdirtmp;
5091         struct iavf_vlan_filter *vlf, *vlftmp;
5092         struct iavf_cloud_filter *cf, *cftmp;
5093         struct iavf_adv_rss *rss, *rsstmp;
5094         struct iavf_mac_filter *f, *ftmp;
5095         struct net_device *netdev;
5096         struct iavf_hw *hw;
5097         int err;
5098
5099         netdev = adapter->netdev;
5100         hw = &adapter->hw;
5101
5102         if (test_and_set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
5103                 return;
5104
5105         /* Wait until port initialization is complete.
5106          * There are flows where register/unregister netdev may race.
5107          */
5108         while (1) {
5109                 mutex_lock(&adapter->crit_lock);
5110                 if (adapter->state == __IAVF_RUNNING ||
5111                     adapter->state == __IAVF_DOWN ||
5112                     adapter->state == __IAVF_INIT_FAILED) {
5113                         mutex_unlock(&adapter->crit_lock);
5114                         break;
5115                 }
5116                 /* Simply return if we already went through iavf_shutdown */
5117                 if (adapter->state == __IAVF_REMOVE) {
5118                         mutex_unlock(&adapter->crit_lock);
5119                         return;
5120                 }
5121
5122                 mutex_unlock(&adapter->crit_lock);
5123                 usleep_range(500, 1000);
5124         }
5125         cancel_delayed_work_sync(&adapter->watchdog_task);
5126         cancel_work_sync(&adapter->finish_config);
5127
5128         rtnl_lock();
5129         if (adapter->netdev_registered) {
5130                 unregister_netdevice(netdev);
5131                 adapter->netdev_registered = false;
5132         }
5133         rtnl_unlock();
5134
5135         if (CLIENT_ALLOWED(adapter)) {
5136                 err = iavf_lan_del_device(adapter);
5137                 if (err)
5138                         dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
5139                                  err);
5140         }
5141
5142         mutex_lock(&adapter->crit_lock);
5143         dev_info(&adapter->pdev->dev, "Removing device\n");
5144         iavf_change_state(adapter, __IAVF_REMOVE);
5145
5146         iavf_request_reset(adapter);
5147         msleep(50);
5148         /* If the FW isn't responding, kick it once, but only once. */
5149         if (!iavf_asq_done(hw)) {
5150                 iavf_request_reset(adapter);
5151                 msleep(50);
5152         }
5153
5154         iavf_misc_irq_disable(adapter);
5155         /* Shut down all the garbage mashers on the detention level */
5156         cancel_work_sync(&adapter->reset_task);
5157         cancel_delayed_work_sync(&adapter->watchdog_task);
5158         cancel_work_sync(&adapter->adminq_task);
5159         cancel_delayed_work_sync(&adapter->client_task);
5160
5161         adapter->aq_required = 0;
5162         adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
5163
5164         iavf_free_all_tx_resources(adapter);
5165         iavf_free_all_rx_resources(adapter);
5166         iavf_free_misc_irq(adapter);
5167
5168         iavf_reset_interrupt_capability(adapter);
5169         iavf_free_q_vectors(adapter);
5170
5171         iavf_free_rss(adapter);
5172
5173         if (hw->aq.asq.count)
5174                 iavf_shutdown_adminq(hw);
5175
5176         /* destroy the locks only once, here */
5177         mutex_destroy(&hw->aq.arq_mutex);
5178         mutex_destroy(&hw->aq.asq_mutex);
5179         mutex_destroy(&adapter->client_lock);
5180         mutex_unlock(&adapter->crit_lock);
5181         mutex_destroy(&adapter->crit_lock);
5182
5183         iounmap(hw->hw_addr);
5184         pci_release_regions(pdev);
5185         iavf_free_queues(adapter);
5186         kfree(adapter->vf_res);
5187         spin_lock_bh(&adapter->mac_vlan_list_lock);
5188         /* If we got removed before an up/down sequence, we've got a filter
5189          * hanging out there that we need to get rid of.
5190          */
5191         list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
5192                 list_del(&f->list);
5193                 kfree(f);
5194         }
5195         list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
5196                                  list) {
5197                 list_del(&vlf->list);
5198                 kfree(vlf);
5199         }
5200
5201         spin_unlock_bh(&adapter->mac_vlan_list_lock);
5202
5203         spin_lock_bh(&adapter->cloud_filter_list_lock);
5204         list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
5205                 list_del(&cf->list);
5206                 kfree(cf);
5207         }
5208         spin_unlock_bh(&adapter->cloud_filter_list_lock);
5209
5210         spin_lock_bh(&adapter->fdir_fltr_lock);
5211         list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head, list) {
5212                 list_del(&fdir->list);
5213                 kfree(fdir);
5214         }
5215         spin_unlock_bh(&adapter->fdir_fltr_lock);
5216
5217         spin_lock_bh(&adapter->adv_rss_lock);
5218         list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
5219                                  list) {
5220                 list_del(&rss->list);
5221                 kfree(rss);
5222         }
5223         spin_unlock_bh(&adapter->adv_rss_lock);
5224
5225         destroy_workqueue(adapter->wq);
5226
5227         free_netdev(netdev);
5228
5229         pci_disable_device(pdev);
5230 }
5231
5232 static SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
5233
5234 static struct pci_driver iavf_driver = {
5235         .name      = iavf_driver_name,
5236         .id_table  = iavf_pci_tbl,
5237         .probe     = iavf_probe,
5238         .remove    = iavf_remove,
5239         .driver.pm = &iavf_pm_ops,
5240         .shutdown  = iavf_shutdown,
5241 };
5242
5243 /**
5244  * iavf_init_module - Driver Registration Routine
5245  *
5246  * iavf_init_module is the first routine called when the driver is
5247  * loaded. All it does is register with the PCI subsystem.
5248  **/
5249 static int __init iavf_init_module(void)
5250 {
5251         pr_info("iavf: %s\n", iavf_driver_string);
5252
5253         pr_info("%s\n", iavf_copyright);
5254
5255         return pci_register_driver(&iavf_driver);
5256 }
5257
5258 module_init(iavf_init_module);
5259
5260 /**
5261  * iavf_exit_module - Driver Exit Cleanup Routine
5262  *
5263  * iavf_exit_module is called just before the driver is removed
5264  * from memory.
5265  **/
5266 static void __exit iavf_exit_module(void)
5267 {
5268         pci_unregister_driver(&iavf_driver);
5269 }
5270
5271 module_exit(iavf_exit_module);
5272
5273 /* iavf_main.c */