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