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