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