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