xref: /openbmc/linux/drivers/net/ethernet/intel/iavf/iavf_main.c (revision f8523d0e83613ab8d082cd504dc53a09fbba4889)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 #include "iavf.h"
5 #include "iavf_prototype.h"
6 #include "iavf_client.h"
7 /* All iavf tracepoints are defined by the include below, which must
8  * be included exactly once across the whole kernel with
9  * CREATE_TRACE_POINTS defined
10  */
11 #define CREATE_TRACE_POINTS
12 #include "iavf_trace.h"
13 
14 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter);
15 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter);
16 static int iavf_close(struct net_device *netdev);
17 static int iavf_init_get_resources(struct iavf_adapter *adapter);
18 static int iavf_check_reset_complete(struct iavf_hw *hw);
19 
20 char iavf_driver_name[] = "iavf";
21 static const char iavf_driver_string[] =
22 	"Intel(R) Ethernet Adaptive Virtual Function Network Driver";
23 
24 #define DRV_KERN "-k"
25 
26 #define DRV_VERSION_MAJOR 3
27 #define DRV_VERSION_MINOR 2
28 #define DRV_VERSION_BUILD 3
29 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
30 	     __stringify(DRV_VERSION_MINOR) "." \
31 	     __stringify(DRV_VERSION_BUILD) \
32 	     DRV_KERN
33 const char iavf_driver_version[] = DRV_VERSION;
34 static const char iavf_copyright[] =
35 	"Copyright (c) 2013 - 2018 Intel Corporation.";
36 
37 /* iavf_pci_tbl - PCI Device ID Table
38  *
39  * Wildcard entries (PCI_ANY_ID) should come last
40  * Last entry must be all 0s
41  *
42  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
43  *   Class, Class Mask, private data (not used) }
44  */
45 static const struct pci_device_id iavf_pci_tbl[] = {
46 	{PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF), 0},
47 	{PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF_HV), 0},
48 	{PCI_VDEVICE(INTEL, IAVF_DEV_ID_X722_VF), 0},
49 	{PCI_VDEVICE(INTEL, IAVF_DEV_ID_ADAPTIVE_VF), 0},
50 	/* required last entry */
51 	{0, }
52 };
53 
54 MODULE_DEVICE_TABLE(pci, iavf_pci_tbl);
55 
56 MODULE_ALIAS("i40evf");
57 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
58 MODULE_DESCRIPTION("Intel(R) Ethernet Adaptive Virtual Function Network Driver");
59 MODULE_LICENSE("GPL v2");
60 MODULE_VERSION(DRV_VERSION);
61 
62 static const struct net_device_ops iavf_netdev_ops;
63 struct workqueue_struct *iavf_wq;
64 
65 /**
66  * iavf_allocate_dma_mem_d - OS specific memory alloc for shared code
67  * @hw:   pointer to the HW structure
68  * @mem:  ptr to mem struct to fill out
69  * @size: size of memory requested
70  * @alignment: what to align the allocation to
71  **/
72 enum iavf_status iavf_allocate_dma_mem_d(struct iavf_hw *hw,
73 					 struct iavf_dma_mem *mem,
74 					 u64 size, u32 alignment)
75 {
76 	struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
77 
78 	if (!mem)
79 		return IAVF_ERR_PARAM;
80 
81 	mem->size = ALIGN(size, alignment);
82 	mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
83 				     (dma_addr_t *)&mem->pa, GFP_KERNEL);
84 	if (mem->va)
85 		return 0;
86 	else
87 		return IAVF_ERR_NO_MEMORY;
88 }
89 
90 /**
91  * iavf_free_dma_mem_d - OS specific memory free for shared code
92  * @hw:   pointer to the HW structure
93  * @mem:  ptr to mem struct to free
94  **/
95 enum iavf_status iavf_free_dma_mem_d(struct iavf_hw *hw,
96 				     struct iavf_dma_mem *mem)
97 {
98 	struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
99 
100 	if (!mem || !mem->va)
101 		return IAVF_ERR_PARAM;
102 	dma_free_coherent(&adapter->pdev->dev, mem->size,
103 			  mem->va, (dma_addr_t)mem->pa);
104 	return 0;
105 }
106 
107 /**
108  * iavf_allocate_virt_mem_d - OS specific memory alloc for shared code
109  * @hw:   pointer to the HW structure
110  * @mem:  ptr to mem struct to fill out
111  * @size: size of memory requested
112  **/
113 enum iavf_status iavf_allocate_virt_mem_d(struct iavf_hw *hw,
114 					  struct iavf_virt_mem *mem, u32 size)
115 {
116 	if (!mem)
117 		return IAVF_ERR_PARAM;
118 
119 	mem->size = size;
120 	mem->va = kzalloc(size, GFP_KERNEL);
121 
122 	if (mem->va)
123 		return 0;
124 	else
125 		return IAVF_ERR_NO_MEMORY;
126 }
127 
128 /**
129  * iavf_free_virt_mem_d - OS specific memory free for shared code
130  * @hw:   pointer to the HW structure
131  * @mem:  ptr to mem struct to free
132  **/
133 enum iavf_status iavf_free_virt_mem_d(struct iavf_hw *hw,
134 				      struct iavf_virt_mem *mem)
135 {
136 	if (!mem)
137 		return IAVF_ERR_PARAM;
138 
139 	/* it's ok to kfree a NULL pointer */
140 	kfree(mem->va);
141 
142 	return 0;
143 }
144 
145 /**
146  * iavf_schedule_reset - Set the flags and schedule a reset event
147  * @adapter: board private structure
148  **/
149 void iavf_schedule_reset(struct iavf_adapter *adapter)
150 {
151 	if (!(adapter->flags &
152 	      (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
153 		adapter->flags |= IAVF_FLAG_RESET_NEEDED;
154 		queue_work(iavf_wq, &adapter->reset_task);
155 	}
156 }
157 
158 /**
159  * iavf_tx_timeout - Respond to a Tx Hang
160  * @netdev: network interface device structure
161  **/
162 static void iavf_tx_timeout(struct net_device *netdev, unsigned int txqueue)
163 {
164 	struct iavf_adapter *adapter = netdev_priv(netdev);
165 
166 	adapter->tx_timeout_count++;
167 	iavf_schedule_reset(adapter);
168 }
169 
170 /**
171  * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
172  * @adapter: board private structure
173  **/
174 static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
175 {
176 	struct iavf_hw *hw = &adapter->hw;
177 
178 	if (!adapter->msix_entries)
179 		return;
180 
181 	wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
182 
183 	iavf_flush(hw);
184 
185 	synchronize_irq(adapter->msix_entries[0].vector);
186 }
187 
188 /**
189  * iavf_misc_irq_enable - Enable default interrupt generation settings
190  * @adapter: board private structure
191  **/
192 static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
193 {
194 	struct iavf_hw *hw = &adapter->hw;
195 
196 	wr32(hw, IAVF_VFINT_DYN_CTL01, IAVF_VFINT_DYN_CTL01_INTENA_MASK |
197 				       IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
198 	wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
199 
200 	iavf_flush(hw);
201 }
202 
203 /**
204  * iavf_irq_disable - Mask off interrupt generation on the NIC
205  * @adapter: board private structure
206  **/
207 static void iavf_irq_disable(struct iavf_adapter *adapter)
208 {
209 	int i;
210 	struct iavf_hw *hw = &adapter->hw;
211 
212 	if (!adapter->msix_entries)
213 		return;
214 
215 	for (i = 1; i < adapter->num_msix_vectors; i++) {
216 		wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1), 0);
217 		synchronize_irq(adapter->msix_entries[i].vector);
218 	}
219 	iavf_flush(hw);
220 }
221 
222 /**
223  * iavf_irq_enable_queues - Enable interrupt for specified queues
224  * @adapter: board private structure
225  * @mask: bitmap of queues to enable
226  **/
227 void iavf_irq_enable_queues(struct iavf_adapter *adapter, u32 mask)
228 {
229 	struct iavf_hw *hw = &adapter->hw;
230 	int i;
231 
232 	for (i = 1; i < adapter->num_msix_vectors; i++) {
233 		if (mask & BIT(i - 1)) {
234 			wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1),
235 			     IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
236 			     IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
237 		}
238 	}
239 }
240 
241 /**
242  * iavf_irq_enable - Enable default interrupt generation settings
243  * @adapter: board private structure
244  * @flush: boolean value whether to run rd32()
245  **/
246 void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
247 {
248 	struct iavf_hw *hw = &adapter->hw;
249 
250 	iavf_misc_irq_enable(adapter);
251 	iavf_irq_enable_queues(adapter, ~0);
252 
253 	if (flush)
254 		iavf_flush(hw);
255 }
256 
257 /**
258  * iavf_msix_aq - Interrupt handler for vector 0
259  * @irq: interrupt number
260  * @data: pointer to netdev
261  **/
262 static irqreturn_t iavf_msix_aq(int irq, void *data)
263 {
264 	struct net_device *netdev = data;
265 	struct iavf_adapter *adapter = netdev_priv(netdev);
266 	struct iavf_hw *hw = &adapter->hw;
267 
268 	/* handle non-queue interrupts, these reads clear the registers */
269 	rd32(hw, IAVF_VFINT_ICR01);
270 	rd32(hw, IAVF_VFINT_ICR0_ENA1);
271 
272 	/* schedule work on the private workqueue */
273 	queue_work(iavf_wq, &adapter->adminq_task);
274 
275 	return IRQ_HANDLED;
276 }
277 
278 /**
279  * iavf_msix_clean_rings - MSIX mode Interrupt Handler
280  * @irq: interrupt number
281  * @data: pointer to a q_vector
282  **/
283 static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
284 {
285 	struct iavf_q_vector *q_vector = data;
286 
287 	if (!q_vector->tx.ring && !q_vector->rx.ring)
288 		return IRQ_HANDLED;
289 
290 	napi_schedule_irqoff(&q_vector->napi);
291 
292 	return IRQ_HANDLED;
293 }
294 
295 /**
296  * iavf_map_vector_to_rxq - associate irqs with rx queues
297  * @adapter: board private structure
298  * @v_idx: interrupt number
299  * @r_idx: queue number
300  **/
301 static void
302 iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
303 {
304 	struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
305 	struct iavf_ring *rx_ring = &adapter->rx_rings[r_idx];
306 	struct iavf_hw *hw = &adapter->hw;
307 
308 	rx_ring->q_vector = q_vector;
309 	rx_ring->next = q_vector->rx.ring;
310 	rx_ring->vsi = &adapter->vsi;
311 	q_vector->rx.ring = rx_ring;
312 	q_vector->rx.count++;
313 	q_vector->rx.next_update = jiffies + 1;
314 	q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
315 	q_vector->ring_mask |= BIT(r_idx);
316 	wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, q_vector->reg_idx),
317 	     q_vector->rx.current_itr >> 1);
318 	q_vector->rx.current_itr = q_vector->rx.target_itr;
319 }
320 
321 /**
322  * iavf_map_vector_to_txq - associate irqs with tx queues
323  * @adapter: board private structure
324  * @v_idx: interrupt number
325  * @t_idx: queue number
326  **/
327 static void
328 iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
329 {
330 	struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
331 	struct iavf_ring *tx_ring = &adapter->tx_rings[t_idx];
332 	struct iavf_hw *hw = &adapter->hw;
333 
334 	tx_ring->q_vector = q_vector;
335 	tx_ring->next = q_vector->tx.ring;
336 	tx_ring->vsi = &adapter->vsi;
337 	q_vector->tx.ring = tx_ring;
338 	q_vector->tx.count++;
339 	q_vector->tx.next_update = jiffies + 1;
340 	q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
341 	q_vector->num_ringpairs++;
342 	wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, q_vector->reg_idx),
343 	     q_vector->tx.target_itr >> 1);
344 	q_vector->tx.current_itr = q_vector->tx.target_itr;
345 }
346 
347 /**
348  * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
349  * @adapter: board private structure to initialize
350  *
351  * This function maps descriptor rings to the queue-specific vectors
352  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
353  * one vector per ring/queue, but on a constrained vector budget, we
354  * group the rings as "efficiently" as possible.  You would add new
355  * mapping configurations in here.
356  **/
357 static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
358 {
359 	int rings_remaining = adapter->num_active_queues;
360 	int ridx = 0, vidx = 0;
361 	int q_vectors;
362 
363 	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
364 
365 	for (; ridx < rings_remaining; ridx++) {
366 		iavf_map_vector_to_rxq(adapter, vidx, ridx);
367 		iavf_map_vector_to_txq(adapter, vidx, ridx);
368 
369 		/* In the case where we have more queues than vectors, continue
370 		 * round-robin on vectors until all queues are mapped.
371 		 */
372 		if (++vidx >= q_vectors)
373 			vidx = 0;
374 	}
375 
376 	adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
377 }
378 
379 /**
380  * iavf_irq_affinity_notify - Callback for affinity changes
381  * @notify: context as to what irq was changed
382  * @mask: the new affinity mask
383  *
384  * This is a callback function used by the irq_set_affinity_notifier function
385  * so that we may register to receive changes to the irq affinity masks.
386  **/
387 static void iavf_irq_affinity_notify(struct irq_affinity_notify *notify,
388 				     const cpumask_t *mask)
389 {
390 	struct iavf_q_vector *q_vector =
391 		container_of(notify, struct iavf_q_vector, affinity_notify);
392 
393 	cpumask_copy(&q_vector->affinity_mask, mask);
394 }
395 
396 /**
397  * iavf_irq_affinity_release - Callback for affinity notifier release
398  * @ref: internal core kernel usage
399  *
400  * This is a callback function used by the irq_set_affinity_notifier function
401  * to inform the current notification subscriber that they will no longer
402  * receive notifications.
403  **/
404 static void iavf_irq_affinity_release(struct kref *ref) {}
405 
406 /**
407  * iavf_request_traffic_irqs - Initialize MSI-X interrupts
408  * @adapter: board private structure
409  * @basename: device basename
410  *
411  * Allocates MSI-X vectors for tx and rx handling, and requests
412  * interrupts from the kernel.
413  **/
414 static int
415 iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
416 {
417 	unsigned int vector, q_vectors;
418 	unsigned int rx_int_idx = 0, tx_int_idx = 0;
419 	int irq_num, err;
420 	int cpu;
421 
422 	iavf_irq_disable(adapter);
423 	/* Decrement for Other and TCP Timer vectors */
424 	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
425 
426 	for (vector = 0; vector < q_vectors; vector++) {
427 		struct iavf_q_vector *q_vector = &adapter->q_vectors[vector];
428 
429 		irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
430 
431 		if (q_vector->tx.ring && q_vector->rx.ring) {
432 			snprintf(q_vector->name, sizeof(q_vector->name),
433 				 "iavf-%s-TxRx-%d", basename, rx_int_idx++);
434 			tx_int_idx++;
435 		} else if (q_vector->rx.ring) {
436 			snprintf(q_vector->name, sizeof(q_vector->name),
437 				 "iavf-%s-rx-%d", basename, rx_int_idx++);
438 		} else if (q_vector->tx.ring) {
439 			snprintf(q_vector->name, sizeof(q_vector->name),
440 				 "iavf-%s-tx-%d", basename, tx_int_idx++);
441 		} else {
442 			/* skip this unused q_vector */
443 			continue;
444 		}
445 		err = request_irq(irq_num,
446 				  iavf_msix_clean_rings,
447 				  0,
448 				  q_vector->name,
449 				  q_vector);
450 		if (err) {
451 			dev_info(&adapter->pdev->dev,
452 				 "Request_irq failed, error: %d\n", err);
453 			goto free_queue_irqs;
454 		}
455 		/* register for affinity change notifications */
456 		q_vector->affinity_notify.notify = iavf_irq_affinity_notify;
457 		q_vector->affinity_notify.release =
458 						   iavf_irq_affinity_release;
459 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
460 		/* Spread the IRQ affinity hints across online CPUs. Note that
461 		 * get_cpu_mask returns a mask with a permanent lifetime so
462 		 * it's safe to use as a hint for irq_set_affinity_hint.
463 		 */
464 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
465 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
466 	}
467 
468 	return 0;
469 
470 free_queue_irqs:
471 	while (vector) {
472 		vector--;
473 		irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
474 		irq_set_affinity_notifier(irq_num, NULL);
475 		irq_set_affinity_hint(irq_num, NULL);
476 		free_irq(irq_num, &adapter->q_vectors[vector]);
477 	}
478 	return err;
479 }
480 
481 /**
482  * iavf_request_misc_irq - Initialize MSI-X interrupts
483  * @adapter: board private structure
484  *
485  * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
486  * vector is only for the admin queue, and stays active even when the netdev
487  * is closed.
488  **/
489 static int iavf_request_misc_irq(struct iavf_adapter *adapter)
490 {
491 	struct net_device *netdev = adapter->netdev;
492 	int err;
493 
494 	snprintf(adapter->misc_vector_name,
495 		 sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
496 		 dev_name(&adapter->pdev->dev));
497 	err = request_irq(adapter->msix_entries[0].vector,
498 			  &iavf_msix_aq, 0,
499 			  adapter->misc_vector_name, netdev);
500 	if (err) {
501 		dev_err(&adapter->pdev->dev,
502 			"request_irq for %s failed: %d\n",
503 			adapter->misc_vector_name, err);
504 		free_irq(adapter->msix_entries[0].vector, netdev);
505 	}
506 	return err;
507 }
508 
509 /**
510  * iavf_free_traffic_irqs - Free MSI-X interrupts
511  * @adapter: board private structure
512  *
513  * Frees all MSI-X vectors other than 0.
514  **/
515 static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
516 {
517 	int vector, irq_num, q_vectors;
518 
519 	if (!adapter->msix_entries)
520 		return;
521 
522 	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
523 
524 	for (vector = 0; vector < q_vectors; vector++) {
525 		irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
526 		irq_set_affinity_notifier(irq_num, NULL);
527 		irq_set_affinity_hint(irq_num, NULL);
528 		free_irq(irq_num, &adapter->q_vectors[vector]);
529 	}
530 }
531 
532 /**
533  * iavf_free_misc_irq - Free MSI-X miscellaneous vector
534  * @adapter: board private structure
535  *
536  * Frees MSI-X vector 0.
537  **/
538 static void iavf_free_misc_irq(struct iavf_adapter *adapter)
539 {
540 	struct net_device *netdev = adapter->netdev;
541 
542 	if (!adapter->msix_entries)
543 		return;
544 
545 	free_irq(adapter->msix_entries[0].vector, netdev);
546 }
547 
548 /**
549  * iavf_configure_tx - Configure Transmit Unit after Reset
550  * @adapter: board private structure
551  *
552  * Configure the Tx unit of the MAC after a reset.
553  **/
554 static void iavf_configure_tx(struct iavf_adapter *adapter)
555 {
556 	struct iavf_hw *hw = &adapter->hw;
557 	int i;
558 
559 	for (i = 0; i < adapter->num_active_queues; i++)
560 		adapter->tx_rings[i].tail = hw->hw_addr + IAVF_QTX_TAIL1(i);
561 }
562 
563 /**
564  * iavf_configure_rx - Configure Receive Unit after Reset
565  * @adapter: board private structure
566  *
567  * Configure the Rx unit of the MAC after a reset.
568  **/
569 static void iavf_configure_rx(struct iavf_adapter *adapter)
570 {
571 	unsigned int rx_buf_len = IAVF_RXBUFFER_2048;
572 	struct iavf_hw *hw = &adapter->hw;
573 	int i;
574 
575 	/* Legacy Rx will always default to a 2048 buffer size. */
576 #if (PAGE_SIZE < 8192)
577 	if (!(adapter->flags & IAVF_FLAG_LEGACY_RX)) {
578 		struct net_device *netdev = adapter->netdev;
579 
580 		/* For jumbo frames on systems with 4K pages we have to use
581 		 * an order 1 page, so we might as well increase the size
582 		 * of our Rx buffer to make better use of the available space
583 		 */
584 		rx_buf_len = IAVF_RXBUFFER_3072;
585 
586 		/* We use a 1536 buffer size for configurations with
587 		 * standard Ethernet mtu.  On x86 this gives us enough room
588 		 * for shared info and 192 bytes of padding.
589 		 */
590 		if (!IAVF_2K_TOO_SMALL_WITH_PADDING &&
591 		    (netdev->mtu <= ETH_DATA_LEN))
592 			rx_buf_len = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
593 	}
594 #endif
595 
596 	for (i = 0; i < adapter->num_active_queues; i++) {
597 		adapter->rx_rings[i].tail = hw->hw_addr + IAVF_QRX_TAIL1(i);
598 		adapter->rx_rings[i].rx_buf_len = rx_buf_len;
599 
600 		if (adapter->flags & IAVF_FLAG_LEGACY_RX)
601 			clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
602 		else
603 			set_ring_build_skb_enabled(&adapter->rx_rings[i]);
604 	}
605 }
606 
607 /**
608  * iavf_find_vlan - Search filter list for specific vlan filter
609  * @adapter: board private structure
610  * @vlan: vlan tag
611  *
612  * Returns ptr to the filter object or NULL. Must be called while holding the
613  * mac_vlan_list_lock.
614  **/
615 static struct
616 iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter, u16 vlan)
617 {
618 	struct iavf_vlan_filter *f;
619 
620 	list_for_each_entry(f, &adapter->vlan_filter_list, list) {
621 		if (vlan == f->vlan)
622 			return f;
623 	}
624 	return NULL;
625 }
626 
627 /**
628  * iavf_add_vlan - Add a vlan filter to the list
629  * @adapter: board private structure
630  * @vlan: VLAN tag
631  *
632  * Returns ptr to the filter object or NULL when no memory available.
633  **/
634 static struct
635 iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter, u16 vlan)
636 {
637 	struct iavf_vlan_filter *f = NULL;
638 
639 	spin_lock_bh(&adapter->mac_vlan_list_lock);
640 
641 	f = iavf_find_vlan(adapter, vlan);
642 	if (!f) {
643 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
644 		if (!f)
645 			goto clearout;
646 
647 		f->vlan = vlan;
648 
649 		list_add_tail(&f->list, &adapter->vlan_filter_list);
650 		f->add = true;
651 		adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
652 	}
653 
654 clearout:
655 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
656 	return f;
657 }
658 
659 /**
660  * iavf_del_vlan - Remove a vlan filter from the list
661  * @adapter: board private structure
662  * @vlan: VLAN tag
663  **/
664 static void iavf_del_vlan(struct iavf_adapter *adapter, u16 vlan)
665 {
666 	struct iavf_vlan_filter *f;
667 
668 	spin_lock_bh(&adapter->mac_vlan_list_lock);
669 
670 	f = iavf_find_vlan(adapter, vlan);
671 	if (f) {
672 		f->remove = true;
673 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
674 	}
675 
676 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
677 }
678 
679 /**
680  * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
681  * @netdev: network device struct
682  * @proto: unused protocol data
683  * @vid: VLAN tag
684  **/
685 static int iavf_vlan_rx_add_vid(struct net_device *netdev,
686 				__always_unused __be16 proto, u16 vid)
687 {
688 	struct iavf_adapter *adapter = netdev_priv(netdev);
689 
690 	if (!VLAN_ALLOWED(adapter))
691 		return -EIO;
692 	if (iavf_add_vlan(adapter, vid) == NULL)
693 		return -ENOMEM;
694 	return 0;
695 }
696 
697 /**
698  * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
699  * @netdev: network device struct
700  * @proto: unused protocol data
701  * @vid: VLAN tag
702  **/
703 static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
704 				 __always_unused __be16 proto, u16 vid)
705 {
706 	struct iavf_adapter *adapter = netdev_priv(netdev);
707 
708 	if (VLAN_ALLOWED(adapter)) {
709 		iavf_del_vlan(adapter, vid);
710 		return 0;
711 	}
712 	return -EIO;
713 }
714 
715 /**
716  * iavf_find_filter - Search filter list for specific mac filter
717  * @adapter: board private structure
718  * @macaddr: the MAC address
719  *
720  * Returns ptr to the filter object or NULL. Must be called while holding the
721  * mac_vlan_list_lock.
722  **/
723 static struct
724 iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
725 				  const u8 *macaddr)
726 {
727 	struct iavf_mac_filter *f;
728 
729 	if (!macaddr)
730 		return NULL;
731 
732 	list_for_each_entry(f, &adapter->mac_filter_list, list) {
733 		if (ether_addr_equal(macaddr, f->macaddr))
734 			return f;
735 	}
736 	return NULL;
737 }
738 
739 /**
740  * iavf_add_filter - Add a mac filter to the filter list
741  * @adapter: board private structure
742  * @macaddr: the MAC address
743  *
744  * Returns ptr to the filter object or NULL when no memory available.
745  **/
746 struct iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
747 					const u8 *macaddr)
748 {
749 	struct iavf_mac_filter *f;
750 
751 	if (!macaddr)
752 		return NULL;
753 
754 	f = iavf_find_filter(adapter, macaddr);
755 	if (!f) {
756 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
757 		if (!f)
758 			return f;
759 
760 		ether_addr_copy(f->macaddr, macaddr);
761 
762 		list_add_tail(&f->list, &adapter->mac_filter_list);
763 		f->add = true;
764 		adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
765 	} else {
766 		f->remove = false;
767 	}
768 
769 	return f;
770 }
771 
772 /**
773  * iavf_set_mac - NDO callback to set port mac address
774  * @netdev: network interface device structure
775  * @p: pointer to an address structure
776  *
777  * Returns 0 on success, negative on failure
778  **/
779 static int iavf_set_mac(struct net_device *netdev, void *p)
780 {
781 	struct iavf_adapter *adapter = netdev_priv(netdev);
782 	struct iavf_hw *hw = &adapter->hw;
783 	struct iavf_mac_filter *f;
784 	struct sockaddr *addr = p;
785 
786 	if (!is_valid_ether_addr(addr->sa_data))
787 		return -EADDRNOTAVAIL;
788 
789 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
790 		return 0;
791 
792 	spin_lock_bh(&adapter->mac_vlan_list_lock);
793 
794 	f = iavf_find_filter(adapter, hw->mac.addr);
795 	if (f) {
796 		f->remove = true;
797 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
798 	}
799 
800 	f = iavf_add_filter(adapter, addr->sa_data);
801 
802 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
803 
804 	if (f) {
805 		ether_addr_copy(hw->mac.addr, addr->sa_data);
806 	}
807 
808 	return (f == NULL) ? -ENOMEM : 0;
809 }
810 
811 /**
812  * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
813  * @netdev: the netdevice
814  * @addr: address to add
815  *
816  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
817  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
818  */
819 static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
820 {
821 	struct iavf_adapter *adapter = netdev_priv(netdev);
822 
823 	if (iavf_add_filter(adapter, addr))
824 		return 0;
825 	else
826 		return -ENOMEM;
827 }
828 
829 /**
830  * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
831  * @netdev: the netdevice
832  * @addr: address to add
833  *
834  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
835  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
836  */
837 static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
838 {
839 	struct iavf_adapter *adapter = netdev_priv(netdev);
840 	struct iavf_mac_filter *f;
841 
842 	/* Under some circumstances, we might receive a request to delete
843 	 * our own device address from our uc list. Because we store the
844 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
845 	 * such requests and not delete our device address from this list.
846 	 */
847 	if (ether_addr_equal(addr, netdev->dev_addr))
848 		return 0;
849 
850 	f = iavf_find_filter(adapter, addr);
851 	if (f) {
852 		f->remove = true;
853 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
854 	}
855 	return 0;
856 }
857 
858 /**
859  * iavf_set_rx_mode - NDO callback to set the netdev filters
860  * @netdev: network interface device structure
861  **/
862 static void iavf_set_rx_mode(struct net_device *netdev)
863 {
864 	struct iavf_adapter *adapter = netdev_priv(netdev);
865 
866 	spin_lock_bh(&adapter->mac_vlan_list_lock);
867 	__dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
868 	__dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
869 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
870 
871 	if (netdev->flags & IFF_PROMISC &&
872 	    !(adapter->flags & IAVF_FLAG_PROMISC_ON))
873 		adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_PROMISC;
874 	else if (!(netdev->flags & IFF_PROMISC) &&
875 		 adapter->flags & IAVF_FLAG_PROMISC_ON)
876 		adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_PROMISC;
877 
878 	if (netdev->flags & IFF_ALLMULTI &&
879 	    !(adapter->flags & IAVF_FLAG_ALLMULTI_ON))
880 		adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_ALLMULTI;
881 	else if (!(netdev->flags & IFF_ALLMULTI) &&
882 		 adapter->flags & IAVF_FLAG_ALLMULTI_ON)
883 		adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_ALLMULTI;
884 }
885 
886 /**
887  * iavf_napi_enable_all - enable NAPI on all queue vectors
888  * @adapter: board private structure
889  **/
890 static void iavf_napi_enable_all(struct iavf_adapter *adapter)
891 {
892 	int q_idx;
893 	struct iavf_q_vector *q_vector;
894 	int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
895 
896 	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
897 		struct napi_struct *napi;
898 
899 		q_vector = &adapter->q_vectors[q_idx];
900 		napi = &q_vector->napi;
901 		napi_enable(napi);
902 	}
903 }
904 
905 /**
906  * iavf_napi_disable_all - disable NAPI on all queue vectors
907  * @adapter: board private structure
908  **/
909 static void iavf_napi_disable_all(struct iavf_adapter *adapter)
910 {
911 	int q_idx;
912 	struct iavf_q_vector *q_vector;
913 	int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
914 
915 	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
916 		q_vector = &adapter->q_vectors[q_idx];
917 		napi_disable(&q_vector->napi);
918 	}
919 }
920 
921 /**
922  * iavf_configure - set up transmit and receive data structures
923  * @adapter: board private structure
924  **/
925 static void iavf_configure(struct iavf_adapter *adapter)
926 {
927 	struct net_device *netdev = adapter->netdev;
928 	int i;
929 
930 	iavf_set_rx_mode(netdev);
931 
932 	iavf_configure_tx(adapter);
933 	iavf_configure_rx(adapter);
934 	adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
935 
936 	for (i = 0; i < adapter->num_active_queues; i++) {
937 		struct iavf_ring *ring = &adapter->rx_rings[i];
938 
939 		iavf_alloc_rx_buffers(ring, IAVF_DESC_UNUSED(ring));
940 	}
941 }
942 
943 /**
944  * iavf_up_complete - Finish the last steps of bringing up a connection
945  * @adapter: board private structure
946  *
947  * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
948  **/
949 static void iavf_up_complete(struct iavf_adapter *adapter)
950 {
951 	adapter->state = __IAVF_RUNNING;
952 	clear_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
953 
954 	iavf_napi_enable_all(adapter);
955 
956 	adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_QUEUES;
957 	if (CLIENT_ENABLED(adapter))
958 		adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_OPEN;
959 	mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
960 }
961 
962 /**
963  * iavf_down - Shutdown the connection processing
964  * @adapter: board private structure
965  *
966  * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
967  **/
968 void iavf_down(struct iavf_adapter *adapter)
969 {
970 	struct net_device *netdev = adapter->netdev;
971 	struct iavf_vlan_filter *vlf;
972 	struct iavf_mac_filter *f;
973 	struct iavf_cloud_filter *cf;
974 
975 	if (adapter->state <= __IAVF_DOWN_PENDING)
976 		return;
977 
978 	netif_carrier_off(netdev);
979 	netif_tx_disable(netdev);
980 	adapter->link_up = false;
981 	iavf_napi_disable_all(adapter);
982 	iavf_irq_disable(adapter);
983 
984 	spin_lock_bh(&adapter->mac_vlan_list_lock);
985 
986 	/* clear the sync flag on all filters */
987 	__dev_uc_unsync(adapter->netdev, NULL);
988 	__dev_mc_unsync(adapter->netdev, NULL);
989 
990 	/* remove all MAC filters */
991 	list_for_each_entry(f, &adapter->mac_filter_list, list) {
992 		f->remove = true;
993 	}
994 
995 	/* remove all VLAN filters */
996 	list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
997 		vlf->remove = true;
998 	}
999 
1000 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
1001 
1002 	/* remove all cloud filters */
1003 	spin_lock_bh(&adapter->cloud_filter_list_lock);
1004 	list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1005 		cf->del = true;
1006 	}
1007 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
1008 
1009 	if (!(adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) &&
1010 	    adapter->state != __IAVF_RESETTING) {
1011 		/* cancel any current operation */
1012 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1013 		/* Schedule operations to close down the HW. Don't wait
1014 		 * here for this to complete. The watchdog is still running
1015 		 * and it will take care of this.
1016 		 */
1017 		adapter->aq_required = IAVF_FLAG_AQ_DEL_MAC_FILTER;
1018 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
1019 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1020 		adapter->aq_required |= IAVF_FLAG_AQ_DISABLE_QUEUES;
1021 	}
1022 
1023 	mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1024 }
1025 
1026 /**
1027  * iavf_acquire_msix_vectors - Setup the MSIX capability
1028  * @adapter: board private structure
1029  * @vectors: number of vectors to request
1030  *
1031  * Work with the OS to set up the MSIX vectors needed.
1032  *
1033  * Returns 0 on success, negative on failure
1034  **/
1035 static int
1036 iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1037 {
1038 	int err, vector_threshold;
1039 
1040 	/* We'll want at least 3 (vector_threshold):
1041 	 * 0) Other (Admin Queue and link, mostly)
1042 	 * 1) TxQ[0] Cleanup
1043 	 * 2) RxQ[0] Cleanup
1044 	 */
1045 	vector_threshold = MIN_MSIX_COUNT;
1046 
1047 	/* The more we get, the more we will assign to Tx/Rx Cleanup
1048 	 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1049 	 * Right now, we simply care about how many we'll get; we'll
1050 	 * set them up later while requesting irq's.
1051 	 */
1052 	err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1053 				    vector_threshold, vectors);
1054 	if (err < 0) {
1055 		dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1056 		kfree(adapter->msix_entries);
1057 		adapter->msix_entries = NULL;
1058 		return err;
1059 	}
1060 
1061 	/* Adjust for only the vectors we'll use, which is minimum
1062 	 * of max_msix_q_vectors + NONQ_VECS, or the number of
1063 	 * vectors we were allocated.
1064 	 */
1065 	adapter->num_msix_vectors = err;
1066 	return 0;
1067 }
1068 
1069 /**
1070  * iavf_free_queues - Free memory for all rings
1071  * @adapter: board private structure to initialize
1072  *
1073  * Free all of the memory associated with queue pairs.
1074  **/
1075 static void iavf_free_queues(struct iavf_adapter *adapter)
1076 {
1077 	if (!adapter->vsi_res)
1078 		return;
1079 	adapter->num_active_queues = 0;
1080 	kfree(adapter->tx_rings);
1081 	adapter->tx_rings = NULL;
1082 	kfree(adapter->rx_rings);
1083 	adapter->rx_rings = NULL;
1084 }
1085 
1086 /**
1087  * iavf_alloc_queues - Allocate memory for all rings
1088  * @adapter: board private structure to initialize
1089  *
1090  * We allocate one ring per queue at run-time since we don't know the
1091  * number of queues at compile-time.  The polling_netdev array is
1092  * intended for Multiqueue, but should work fine with a single queue.
1093  **/
1094 static int iavf_alloc_queues(struct iavf_adapter *adapter)
1095 {
1096 	int i, num_active_queues;
1097 
1098 	/* If we're in reset reallocating queues we don't actually know yet for
1099 	 * certain the PF gave us the number of queues we asked for but we'll
1100 	 * assume it did.  Once basic reset is finished we'll confirm once we
1101 	 * start negotiating config with PF.
1102 	 */
1103 	if (adapter->num_req_queues)
1104 		num_active_queues = adapter->num_req_queues;
1105 	else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1106 		 adapter->num_tc)
1107 		num_active_queues = adapter->ch_config.total_qps;
1108 	else
1109 		num_active_queues = min_t(int,
1110 					  adapter->vsi_res->num_queue_pairs,
1111 					  (int)(num_online_cpus()));
1112 
1113 
1114 	adapter->tx_rings = kcalloc(num_active_queues,
1115 				    sizeof(struct iavf_ring), GFP_KERNEL);
1116 	if (!adapter->tx_rings)
1117 		goto err_out;
1118 	adapter->rx_rings = kcalloc(num_active_queues,
1119 				    sizeof(struct iavf_ring), GFP_KERNEL);
1120 	if (!adapter->rx_rings)
1121 		goto err_out;
1122 
1123 	for (i = 0; i < num_active_queues; i++) {
1124 		struct iavf_ring *tx_ring;
1125 		struct iavf_ring *rx_ring;
1126 
1127 		tx_ring = &adapter->tx_rings[i];
1128 
1129 		tx_ring->queue_index = i;
1130 		tx_ring->netdev = adapter->netdev;
1131 		tx_ring->dev = &adapter->pdev->dev;
1132 		tx_ring->count = adapter->tx_desc_count;
1133 		tx_ring->itr_setting = IAVF_ITR_TX_DEF;
1134 		if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1135 			tx_ring->flags |= IAVF_TXR_FLAGS_WB_ON_ITR;
1136 
1137 		rx_ring = &adapter->rx_rings[i];
1138 		rx_ring->queue_index = i;
1139 		rx_ring->netdev = adapter->netdev;
1140 		rx_ring->dev = &adapter->pdev->dev;
1141 		rx_ring->count = adapter->rx_desc_count;
1142 		rx_ring->itr_setting = IAVF_ITR_RX_DEF;
1143 	}
1144 
1145 	adapter->num_active_queues = num_active_queues;
1146 
1147 	return 0;
1148 
1149 err_out:
1150 	iavf_free_queues(adapter);
1151 	return -ENOMEM;
1152 }
1153 
1154 /**
1155  * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1156  * @adapter: board private structure to initialize
1157  *
1158  * Attempt to configure the interrupts using the best available
1159  * capabilities of the hardware and the kernel.
1160  **/
1161 static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1162 {
1163 	int vector, v_budget;
1164 	int pairs = 0;
1165 	int err = 0;
1166 
1167 	if (!adapter->vsi_res) {
1168 		err = -EIO;
1169 		goto out;
1170 	}
1171 	pairs = adapter->num_active_queues;
1172 
1173 	/* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1174 	 * us much good if we have more vectors than CPUs. However, we already
1175 	 * limit the total number of queues by the number of CPUs so we do not
1176 	 * need any further limiting here.
1177 	 */
1178 	v_budget = min_t(int, pairs + NONQ_VECS,
1179 			 (int)adapter->vf_res->max_vectors);
1180 
1181 	adapter->msix_entries = kcalloc(v_budget,
1182 					sizeof(struct msix_entry), GFP_KERNEL);
1183 	if (!adapter->msix_entries) {
1184 		err = -ENOMEM;
1185 		goto out;
1186 	}
1187 
1188 	for (vector = 0; vector < v_budget; vector++)
1189 		adapter->msix_entries[vector].entry = vector;
1190 
1191 	err = iavf_acquire_msix_vectors(adapter, v_budget);
1192 
1193 out:
1194 	netif_set_real_num_rx_queues(adapter->netdev, pairs);
1195 	netif_set_real_num_tx_queues(adapter->netdev, pairs);
1196 	return err;
1197 }
1198 
1199 /**
1200  * iavf_config_rss_aq - Configure RSS keys and lut by using AQ commands
1201  * @adapter: board private structure
1202  *
1203  * Return 0 on success, negative on failure
1204  **/
1205 static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1206 {
1207 	struct iavf_aqc_get_set_rss_key_data *rss_key =
1208 		(struct iavf_aqc_get_set_rss_key_data *)adapter->rss_key;
1209 	struct iavf_hw *hw = &adapter->hw;
1210 	int ret = 0;
1211 
1212 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1213 		/* bail because we already have a command pending */
1214 		dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1215 			adapter->current_op);
1216 		return -EBUSY;
1217 	}
1218 
1219 	ret = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1220 	if (ret) {
1221 		dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1222 			iavf_stat_str(hw, ret),
1223 			iavf_aq_str(hw, hw->aq.asq_last_status));
1224 		return ret;
1225 
1226 	}
1227 
1228 	ret = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1229 				  adapter->rss_lut, adapter->rss_lut_size);
1230 	if (ret) {
1231 		dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1232 			iavf_stat_str(hw, ret),
1233 			iavf_aq_str(hw, hw->aq.asq_last_status));
1234 	}
1235 
1236 	return ret;
1237 
1238 }
1239 
1240 /**
1241  * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1242  * @adapter: board private structure
1243  *
1244  * Returns 0 on success, negative on failure
1245  **/
1246 static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1247 {
1248 	struct iavf_hw *hw = &adapter->hw;
1249 	u32 *dw;
1250 	u16 i;
1251 
1252 	dw = (u32 *)adapter->rss_key;
1253 	for (i = 0; i <= adapter->rss_key_size / 4; i++)
1254 		wr32(hw, IAVF_VFQF_HKEY(i), dw[i]);
1255 
1256 	dw = (u32 *)adapter->rss_lut;
1257 	for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1258 		wr32(hw, IAVF_VFQF_HLUT(i), dw[i]);
1259 
1260 	iavf_flush(hw);
1261 
1262 	return 0;
1263 }
1264 
1265 /**
1266  * iavf_config_rss - Configure RSS keys and lut
1267  * @adapter: board private structure
1268  *
1269  * Returns 0 on success, negative on failure
1270  **/
1271 int iavf_config_rss(struct iavf_adapter *adapter)
1272 {
1273 
1274 	if (RSS_PF(adapter)) {
1275 		adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1276 					IAVF_FLAG_AQ_SET_RSS_KEY;
1277 		return 0;
1278 	} else if (RSS_AQ(adapter)) {
1279 		return iavf_config_rss_aq(adapter);
1280 	} else {
1281 		return iavf_config_rss_reg(adapter);
1282 	}
1283 }
1284 
1285 /**
1286  * iavf_fill_rss_lut - Fill the lut with default values
1287  * @adapter: board private structure
1288  **/
1289 static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1290 {
1291 	u16 i;
1292 
1293 	for (i = 0; i < adapter->rss_lut_size; i++)
1294 		adapter->rss_lut[i] = i % adapter->num_active_queues;
1295 }
1296 
1297 /**
1298  * iavf_init_rss - Prepare for RSS
1299  * @adapter: board private structure
1300  *
1301  * Return 0 on success, negative on failure
1302  **/
1303 static int iavf_init_rss(struct iavf_adapter *adapter)
1304 {
1305 	struct iavf_hw *hw = &adapter->hw;
1306 	int ret;
1307 
1308 	if (!RSS_PF(adapter)) {
1309 		/* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1310 		if (adapter->vf_res->vf_cap_flags &
1311 		    VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1312 			adapter->hena = IAVF_DEFAULT_RSS_HENA_EXPANDED;
1313 		else
1314 			adapter->hena = IAVF_DEFAULT_RSS_HENA;
1315 
1316 		wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->hena);
1317 		wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1318 	}
1319 
1320 	iavf_fill_rss_lut(adapter);
1321 	netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1322 	ret = iavf_config_rss(adapter);
1323 
1324 	return ret;
1325 }
1326 
1327 /**
1328  * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1329  * @adapter: board private structure to initialize
1330  *
1331  * We allocate one q_vector per queue interrupt.  If allocation fails we
1332  * return -ENOMEM.
1333  **/
1334 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1335 {
1336 	int q_idx = 0, num_q_vectors;
1337 	struct iavf_q_vector *q_vector;
1338 
1339 	num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1340 	adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1341 				     GFP_KERNEL);
1342 	if (!adapter->q_vectors)
1343 		return -ENOMEM;
1344 
1345 	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1346 		q_vector = &adapter->q_vectors[q_idx];
1347 		q_vector->adapter = adapter;
1348 		q_vector->vsi = &adapter->vsi;
1349 		q_vector->v_idx = q_idx;
1350 		q_vector->reg_idx = q_idx;
1351 		cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1352 		netif_napi_add(adapter->netdev, &q_vector->napi,
1353 			       iavf_napi_poll, NAPI_POLL_WEIGHT);
1354 	}
1355 
1356 	return 0;
1357 }
1358 
1359 /**
1360  * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1361  * @adapter: board private structure to initialize
1362  *
1363  * This function frees the memory allocated to the q_vectors.  In addition if
1364  * NAPI is enabled it will delete any references to the NAPI struct prior
1365  * to freeing the q_vector.
1366  **/
1367 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1368 {
1369 	int q_idx, num_q_vectors;
1370 	int napi_vectors;
1371 
1372 	if (!adapter->q_vectors)
1373 		return;
1374 
1375 	num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1376 	napi_vectors = adapter->num_active_queues;
1377 
1378 	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1379 		struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1380 
1381 		if (q_idx < napi_vectors)
1382 			netif_napi_del(&q_vector->napi);
1383 	}
1384 	kfree(adapter->q_vectors);
1385 	adapter->q_vectors = NULL;
1386 }
1387 
1388 /**
1389  * iavf_reset_interrupt_capability - Reset MSIX setup
1390  * @adapter: board private structure
1391  *
1392  **/
1393 void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1394 {
1395 	if (!adapter->msix_entries)
1396 		return;
1397 
1398 	pci_disable_msix(adapter->pdev);
1399 	kfree(adapter->msix_entries);
1400 	adapter->msix_entries = NULL;
1401 }
1402 
1403 /**
1404  * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1405  * @adapter: board private structure to initialize
1406  *
1407  **/
1408 int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1409 {
1410 	int err;
1411 
1412 	err = iavf_alloc_queues(adapter);
1413 	if (err) {
1414 		dev_err(&adapter->pdev->dev,
1415 			"Unable to allocate memory for queues\n");
1416 		goto err_alloc_queues;
1417 	}
1418 
1419 	rtnl_lock();
1420 	err = iavf_set_interrupt_capability(adapter);
1421 	rtnl_unlock();
1422 	if (err) {
1423 		dev_err(&adapter->pdev->dev,
1424 			"Unable to setup interrupt capabilities\n");
1425 		goto err_set_interrupt;
1426 	}
1427 
1428 	err = iavf_alloc_q_vectors(adapter);
1429 	if (err) {
1430 		dev_err(&adapter->pdev->dev,
1431 			"Unable to allocate memory for queue vectors\n");
1432 		goto err_alloc_q_vectors;
1433 	}
1434 
1435 	/* If we've made it so far while ADq flag being ON, then we haven't
1436 	 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1437 	 * resources have been allocated in the reset path.
1438 	 * Now we can truly claim that ADq is enabled.
1439 	 */
1440 	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1441 	    adapter->num_tc)
1442 		dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1443 			 adapter->num_tc);
1444 
1445 	dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1446 		 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1447 		 adapter->num_active_queues);
1448 
1449 	return 0;
1450 err_alloc_q_vectors:
1451 	iavf_reset_interrupt_capability(adapter);
1452 err_set_interrupt:
1453 	iavf_free_queues(adapter);
1454 err_alloc_queues:
1455 	return err;
1456 }
1457 
1458 /**
1459  * iavf_free_rss - Free memory used by RSS structs
1460  * @adapter: board private structure
1461  **/
1462 static void iavf_free_rss(struct iavf_adapter *adapter)
1463 {
1464 	kfree(adapter->rss_key);
1465 	adapter->rss_key = NULL;
1466 
1467 	kfree(adapter->rss_lut);
1468 	adapter->rss_lut = NULL;
1469 }
1470 
1471 /**
1472  * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1473  * @adapter: board private structure
1474  *
1475  * Returns 0 on success, negative on failure
1476  **/
1477 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter)
1478 {
1479 	struct net_device *netdev = adapter->netdev;
1480 	int err;
1481 
1482 	if (netif_running(netdev))
1483 		iavf_free_traffic_irqs(adapter);
1484 	iavf_free_misc_irq(adapter);
1485 	iavf_reset_interrupt_capability(adapter);
1486 	iavf_free_q_vectors(adapter);
1487 	iavf_free_queues(adapter);
1488 
1489 	err =  iavf_init_interrupt_scheme(adapter);
1490 	if (err)
1491 		goto err;
1492 
1493 	netif_tx_stop_all_queues(netdev);
1494 
1495 	err = iavf_request_misc_irq(adapter);
1496 	if (err)
1497 		goto err;
1498 
1499 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1500 
1501 	iavf_map_rings_to_vectors(adapter);
1502 
1503 	if (RSS_AQ(adapter))
1504 		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
1505 	else
1506 		err = iavf_init_rss(adapter);
1507 err:
1508 	return err;
1509 }
1510 
1511 /**
1512  * iavf_process_aq_command - process aq_required flags
1513  * and sends aq command
1514  * @adapter: pointer to iavf adapter structure
1515  *
1516  * Returns 0 on success
1517  * Returns error code if no command was sent
1518  * or error code if the command failed.
1519  **/
1520 static int iavf_process_aq_command(struct iavf_adapter *adapter)
1521 {
1522 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG)
1523 		return iavf_send_vf_config_msg(adapter);
1524 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
1525 		iavf_disable_queues(adapter);
1526 		return 0;
1527 	}
1528 
1529 	if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
1530 		iavf_map_queues(adapter);
1531 		return 0;
1532 	}
1533 
1534 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
1535 		iavf_add_ether_addrs(adapter);
1536 		return 0;
1537 	}
1538 
1539 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
1540 		iavf_add_vlans(adapter);
1541 		return 0;
1542 	}
1543 
1544 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
1545 		iavf_del_ether_addrs(adapter);
1546 		return 0;
1547 	}
1548 
1549 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
1550 		iavf_del_vlans(adapter);
1551 		return 0;
1552 	}
1553 
1554 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1555 		iavf_enable_vlan_stripping(adapter);
1556 		return 0;
1557 	}
1558 
1559 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1560 		iavf_disable_vlan_stripping(adapter);
1561 		return 0;
1562 	}
1563 
1564 	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
1565 		iavf_configure_queues(adapter);
1566 		return 0;
1567 	}
1568 
1569 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
1570 		iavf_enable_queues(adapter);
1571 		return 0;
1572 	}
1573 
1574 	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
1575 		/* This message goes straight to the firmware, not the
1576 		 * PF, so we don't have to set current_op as we will
1577 		 * not get a response through the ARQ.
1578 		 */
1579 		adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
1580 		return 0;
1581 	}
1582 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
1583 		iavf_get_hena(adapter);
1584 		return 0;
1585 	}
1586 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
1587 		iavf_set_hena(adapter);
1588 		return 0;
1589 	}
1590 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
1591 		iavf_set_rss_key(adapter);
1592 		return 0;
1593 	}
1594 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
1595 		iavf_set_rss_lut(adapter);
1596 		return 0;
1597 	}
1598 
1599 	if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_PROMISC) {
1600 		iavf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1601 				       FLAG_VF_MULTICAST_PROMISC);
1602 		return 0;
1603 	}
1604 
1605 	if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_ALLMULTI) {
1606 		iavf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1607 		return 0;
1608 	}
1609 
1610 	if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) &&
1611 	    (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1612 		iavf_set_promiscuous(adapter, 0);
1613 		return 0;
1614 	}
1615 
1616 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
1617 		iavf_enable_channels(adapter);
1618 		return 0;
1619 	}
1620 
1621 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
1622 		iavf_disable_channels(adapter);
1623 		return 0;
1624 	}
1625 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1626 		iavf_add_cloud_filter(adapter);
1627 		return 0;
1628 	}
1629 
1630 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1631 		iavf_del_cloud_filter(adapter);
1632 		return 0;
1633 	}
1634 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1635 		iavf_del_cloud_filter(adapter);
1636 		return 0;
1637 	}
1638 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1639 		iavf_add_cloud_filter(adapter);
1640 		return 0;
1641 	}
1642 	return -EAGAIN;
1643 }
1644 
1645 /**
1646  * iavf_startup - first step of driver startup
1647  * @adapter: board private structure
1648  *
1649  * Function process __IAVF_STARTUP driver state.
1650  * When success the state is changed to __IAVF_INIT_VERSION_CHECK
1651  * when fails it returns -EAGAIN
1652  **/
1653 static int iavf_startup(struct iavf_adapter *adapter)
1654 {
1655 	struct pci_dev *pdev = adapter->pdev;
1656 	struct iavf_hw *hw = &adapter->hw;
1657 	int err;
1658 
1659 	WARN_ON(adapter->state != __IAVF_STARTUP);
1660 
1661 	/* driver loaded, probe complete */
1662 	adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1663 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1664 	err = iavf_set_mac_type(hw);
1665 	if (err) {
1666 		dev_err(&pdev->dev, "Failed to set MAC type (%d)\n", err);
1667 		goto err;
1668 	}
1669 
1670 	err = iavf_check_reset_complete(hw);
1671 	if (err) {
1672 		dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
1673 			 err);
1674 		goto err;
1675 	}
1676 	hw->aq.num_arq_entries = IAVF_AQ_LEN;
1677 	hw->aq.num_asq_entries = IAVF_AQ_LEN;
1678 	hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
1679 	hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
1680 
1681 	err = iavf_init_adminq(hw);
1682 	if (err) {
1683 		dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n", err);
1684 		goto err;
1685 	}
1686 	err = iavf_send_api_ver(adapter);
1687 	if (err) {
1688 		dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
1689 		iavf_shutdown_adminq(hw);
1690 		goto err;
1691 	}
1692 	adapter->state = __IAVF_INIT_VERSION_CHECK;
1693 err:
1694 	return err;
1695 }
1696 
1697 /**
1698  * iavf_init_version_check - second step of driver startup
1699  * @adapter: board private structure
1700  *
1701  * Function process __IAVF_INIT_VERSION_CHECK driver state.
1702  * When success the state is changed to __IAVF_INIT_GET_RESOURCES
1703  * when fails it returns -EAGAIN
1704  **/
1705 static int iavf_init_version_check(struct iavf_adapter *adapter)
1706 {
1707 	struct pci_dev *pdev = adapter->pdev;
1708 	struct iavf_hw *hw = &adapter->hw;
1709 	int err = -EAGAIN;
1710 
1711 	WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
1712 
1713 	if (!iavf_asq_done(hw)) {
1714 		dev_err(&pdev->dev, "Admin queue command never completed\n");
1715 		iavf_shutdown_adminq(hw);
1716 		adapter->state = __IAVF_STARTUP;
1717 		goto err;
1718 	}
1719 
1720 	/* aq msg sent, awaiting reply */
1721 	err = iavf_verify_api_ver(adapter);
1722 	if (err) {
1723 		if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK)
1724 			err = iavf_send_api_ver(adapter);
1725 		else
1726 			dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
1727 				adapter->pf_version.major,
1728 				adapter->pf_version.minor,
1729 				VIRTCHNL_VERSION_MAJOR,
1730 				VIRTCHNL_VERSION_MINOR);
1731 		goto err;
1732 	}
1733 	err = iavf_send_vf_config_msg(adapter);
1734 	if (err) {
1735 		dev_err(&pdev->dev, "Unable to send config request (%d)\n",
1736 			err);
1737 		goto err;
1738 	}
1739 	adapter->state = __IAVF_INIT_GET_RESOURCES;
1740 
1741 err:
1742 	return err;
1743 }
1744 
1745 /**
1746  * iavf_init_get_resources - third step of driver startup
1747  * @adapter: board private structure
1748  *
1749  * Function process __IAVF_INIT_GET_RESOURCES driver state and
1750  * finishes driver initialization procedure.
1751  * When success the state is changed to __IAVF_DOWN
1752  * when fails it returns -EAGAIN
1753  **/
1754 static int iavf_init_get_resources(struct iavf_adapter *adapter)
1755 {
1756 	struct net_device *netdev = adapter->netdev;
1757 	struct pci_dev *pdev = adapter->pdev;
1758 	struct iavf_hw *hw = &adapter->hw;
1759 	int err;
1760 
1761 	WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
1762 	/* aq msg sent, awaiting reply */
1763 	if (!adapter->vf_res) {
1764 		adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
1765 					  GFP_KERNEL);
1766 		if (!adapter->vf_res) {
1767 			err = -ENOMEM;
1768 			goto err;
1769 		}
1770 	}
1771 	err = iavf_get_vf_config(adapter);
1772 	if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK) {
1773 		err = iavf_send_vf_config_msg(adapter);
1774 		goto err;
1775 	} else if (err == IAVF_ERR_PARAM) {
1776 		/* We only get ERR_PARAM if the device is in a very bad
1777 		 * state or if we've been disabled for previous bad
1778 		 * behavior. Either way, we're done now.
1779 		 */
1780 		iavf_shutdown_adminq(hw);
1781 		dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
1782 		return 0;
1783 	}
1784 	if (err) {
1785 		dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
1786 		goto err_alloc;
1787 	}
1788 
1789 	if (iavf_process_config(adapter))
1790 		goto err_alloc;
1791 	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1792 
1793 	adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
1794 
1795 	netdev->netdev_ops = &iavf_netdev_ops;
1796 	iavf_set_ethtool_ops(netdev);
1797 	netdev->watchdog_timeo = 5 * HZ;
1798 
1799 	/* MTU range: 68 - 9710 */
1800 	netdev->min_mtu = ETH_MIN_MTU;
1801 	netdev->max_mtu = IAVF_MAX_RXBUFFER - IAVF_PACKET_HDR_PAD;
1802 
1803 	if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
1804 		dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1805 			 adapter->hw.mac.addr);
1806 		eth_hw_addr_random(netdev);
1807 		ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1808 	} else {
1809 		ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
1810 		ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
1811 	}
1812 
1813 	adapter->tx_desc_count = IAVF_DEFAULT_TXD;
1814 	adapter->rx_desc_count = IAVF_DEFAULT_RXD;
1815 	err = iavf_init_interrupt_scheme(adapter);
1816 	if (err)
1817 		goto err_sw_init;
1818 	iavf_map_rings_to_vectors(adapter);
1819 	if (adapter->vf_res->vf_cap_flags &
1820 		VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1821 		adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
1822 
1823 	err = iavf_request_misc_irq(adapter);
1824 	if (err)
1825 		goto err_sw_init;
1826 
1827 	netif_carrier_off(netdev);
1828 	adapter->link_up = false;
1829 
1830 	/* set the semaphore to prevent any callbacks after device registration
1831 	 * up to time when state of driver will be set to __IAVF_DOWN
1832 	 */
1833 	rtnl_lock();
1834 	if (!adapter->netdev_registered) {
1835 		err = register_netdevice(netdev);
1836 		if (err) {
1837 			rtnl_unlock();
1838 			goto err_register;
1839 		}
1840 	}
1841 
1842 	adapter->netdev_registered = true;
1843 
1844 	netif_tx_stop_all_queues(netdev);
1845 	if (CLIENT_ALLOWED(adapter)) {
1846 		err = iavf_lan_add_device(adapter);
1847 		if (err) {
1848 			rtnl_unlock();
1849 			dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
1850 				 err);
1851 		}
1852 	}
1853 	dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
1854 	if (netdev->features & NETIF_F_GRO)
1855 		dev_info(&pdev->dev, "GRO is enabled\n");
1856 
1857 	adapter->state = __IAVF_DOWN;
1858 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1859 	rtnl_unlock();
1860 
1861 	iavf_misc_irq_enable(adapter);
1862 	wake_up(&adapter->down_waitqueue);
1863 
1864 	adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
1865 	adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
1866 	if (!adapter->rss_key || !adapter->rss_lut)
1867 		goto err_mem;
1868 	if (RSS_AQ(adapter))
1869 		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
1870 	else
1871 		iavf_init_rss(adapter);
1872 
1873 	return err;
1874 err_mem:
1875 	iavf_free_rss(adapter);
1876 err_register:
1877 	iavf_free_misc_irq(adapter);
1878 err_sw_init:
1879 	iavf_reset_interrupt_capability(adapter);
1880 err_alloc:
1881 	kfree(adapter->vf_res);
1882 	adapter->vf_res = NULL;
1883 err:
1884 	return err;
1885 }
1886 
1887 /**
1888  * iavf_watchdog_task - Periodic call-back task
1889  * @work: pointer to work_struct
1890  **/
1891 static void iavf_watchdog_task(struct work_struct *work)
1892 {
1893 	struct iavf_adapter *adapter = container_of(work,
1894 						    struct iavf_adapter,
1895 						    watchdog_task.work);
1896 	struct iavf_hw *hw = &adapter->hw;
1897 	u32 reg_val;
1898 
1899 	if (test_and_set_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section))
1900 		goto restart_watchdog;
1901 
1902 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
1903 		adapter->state = __IAVF_COMM_FAILED;
1904 
1905 	switch (adapter->state) {
1906 	case __IAVF_COMM_FAILED:
1907 		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
1908 			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1909 		if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
1910 		    reg_val == VIRTCHNL_VFR_COMPLETED) {
1911 			/* A chance for redemption! */
1912 			dev_err(&adapter->pdev->dev,
1913 				"Hardware came out of reset. Attempting reinit.\n");
1914 			adapter->state = __IAVF_STARTUP;
1915 			adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1916 			queue_delayed_work(iavf_wq, &adapter->init_task, 10);
1917 			clear_bit(__IAVF_IN_CRITICAL_TASK,
1918 				  &adapter->crit_section);
1919 			/* Don't reschedule the watchdog, since we've restarted
1920 			 * the init task. When init_task contacts the PF and
1921 			 * gets everything set up again, it'll restart the
1922 			 * watchdog for us. Down, boy. Sit. Stay. Woof.
1923 			 */
1924 			return;
1925 		}
1926 		adapter->aq_required = 0;
1927 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1928 		clear_bit(__IAVF_IN_CRITICAL_TASK,
1929 			  &adapter->crit_section);
1930 		queue_delayed_work(iavf_wq,
1931 				   &adapter->watchdog_task,
1932 				   msecs_to_jiffies(10));
1933 		goto watchdog_done;
1934 	case __IAVF_RESETTING:
1935 		clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1936 		queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
1937 		return;
1938 	case __IAVF_DOWN:
1939 	case __IAVF_DOWN_PENDING:
1940 	case __IAVF_TESTING:
1941 	case __IAVF_RUNNING:
1942 		if (adapter->current_op) {
1943 			if (!iavf_asq_done(hw)) {
1944 				dev_dbg(&adapter->pdev->dev,
1945 					"Admin queue timeout\n");
1946 				iavf_send_api_ver(adapter);
1947 			}
1948 		} else {
1949 			if (!iavf_process_aq_command(adapter) &&
1950 			    adapter->state == __IAVF_RUNNING)
1951 				iavf_request_stats(adapter);
1952 		}
1953 		break;
1954 	case __IAVF_REMOVE:
1955 		clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1956 		return;
1957 	default:
1958 		goto restart_watchdog;
1959 	}
1960 
1961 		/* check for hw reset */
1962 	reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
1963 	if (!reg_val) {
1964 		adapter->state = __IAVF_RESETTING;
1965 		adapter->flags |= IAVF_FLAG_RESET_PENDING;
1966 		adapter->aq_required = 0;
1967 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1968 		dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1969 		queue_work(iavf_wq, &adapter->reset_task);
1970 		goto watchdog_done;
1971 	}
1972 
1973 	schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1974 watchdog_done:
1975 	if (adapter->state == __IAVF_RUNNING ||
1976 	    adapter->state == __IAVF_COMM_FAILED)
1977 		iavf_detect_recover_hung(&adapter->vsi);
1978 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1979 restart_watchdog:
1980 	if (adapter->aq_required)
1981 		queue_delayed_work(iavf_wq, &adapter->watchdog_task,
1982 				   msecs_to_jiffies(20));
1983 	else
1984 		queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
1985 	queue_work(iavf_wq, &adapter->adminq_task);
1986 }
1987 
1988 static void iavf_disable_vf(struct iavf_adapter *adapter)
1989 {
1990 	struct iavf_mac_filter *f, *ftmp;
1991 	struct iavf_vlan_filter *fv, *fvtmp;
1992 	struct iavf_cloud_filter *cf, *cftmp;
1993 
1994 	adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
1995 
1996 	/* We don't use netif_running() because it may be true prior to
1997 	 * ndo_open() returning, so we can't assume it means all our open
1998 	 * tasks have finished, since we're not holding the rtnl_lock here.
1999 	 */
2000 	if (adapter->state == __IAVF_RUNNING) {
2001 		set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2002 		netif_carrier_off(adapter->netdev);
2003 		netif_tx_disable(adapter->netdev);
2004 		adapter->link_up = false;
2005 		iavf_napi_disable_all(adapter);
2006 		iavf_irq_disable(adapter);
2007 		iavf_free_traffic_irqs(adapter);
2008 		iavf_free_all_tx_resources(adapter);
2009 		iavf_free_all_rx_resources(adapter);
2010 	}
2011 
2012 	spin_lock_bh(&adapter->mac_vlan_list_lock);
2013 
2014 	/* Delete all of the filters */
2015 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2016 		list_del(&f->list);
2017 		kfree(f);
2018 	}
2019 
2020 	list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
2021 		list_del(&fv->list);
2022 		kfree(fv);
2023 	}
2024 
2025 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
2026 
2027 	spin_lock_bh(&adapter->cloud_filter_list_lock);
2028 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
2029 		list_del(&cf->list);
2030 		kfree(cf);
2031 		adapter->num_cloud_filters--;
2032 	}
2033 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2034 
2035 	iavf_free_misc_irq(adapter);
2036 	iavf_reset_interrupt_capability(adapter);
2037 	iavf_free_queues(adapter);
2038 	iavf_free_q_vectors(adapter);
2039 	memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
2040 	iavf_shutdown_adminq(&adapter->hw);
2041 	adapter->netdev->flags &= ~IFF_UP;
2042 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2043 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2044 	adapter->state = __IAVF_DOWN;
2045 	wake_up(&adapter->down_waitqueue);
2046 	dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
2047 }
2048 
2049 /**
2050  * iavf_reset_task - Call-back task to handle hardware reset
2051  * @work: pointer to work_struct
2052  *
2053  * During reset we need to shut down and reinitialize the admin queue
2054  * before we can use it to communicate with the PF again. We also clear
2055  * and reinit the rings because that context is lost as well.
2056  **/
2057 static void iavf_reset_task(struct work_struct *work)
2058 {
2059 	struct iavf_adapter *adapter = container_of(work,
2060 						      struct iavf_adapter,
2061 						      reset_task);
2062 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
2063 	struct net_device *netdev = adapter->netdev;
2064 	struct iavf_hw *hw = &adapter->hw;
2065 	struct iavf_mac_filter *f, *ftmp;
2066 	struct iavf_vlan_filter *vlf;
2067 	struct iavf_cloud_filter *cf;
2068 	u32 reg_val;
2069 	int i = 0, err;
2070 	bool running;
2071 
2072 	/* When device is being removed it doesn't make sense to run the reset
2073 	 * task, just return in such a case.
2074 	 */
2075 	if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
2076 		return;
2077 
2078 	while (test_and_set_bit(__IAVF_IN_CLIENT_TASK,
2079 				&adapter->crit_section))
2080 		usleep_range(500, 1000);
2081 	if (CLIENT_ENABLED(adapter)) {
2082 		adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
2083 				    IAVF_FLAG_CLIENT_NEEDS_CLOSE |
2084 				    IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
2085 				    IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
2086 		cancel_delayed_work_sync(&adapter->client_task);
2087 		iavf_notify_client_close(&adapter->vsi, true);
2088 	}
2089 	iavf_misc_irq_disable(adapter);
2090 	if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
2091 		adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
2092 		/* Restart the AQ here. If we have been reset but didn't
2093 		 * detect it, or if the PF had to reinit, our AQ will be hosed.
2094 		 */
2095 		iavf_shutdown_adminq(hw);
2096 		iavf_init_adminq(hw);
2097 		iavf_request_reset(adapter);
2098 	}
2099 	adapter->flags |= IAVF_FLAG_RESET_PENDING;
2100 
2101 	/* poll until we see the reset actually happen */
2102 	for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
2103 		reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
2104 			  IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2105 		if (!reg_val)
2106 			break;
2107 		usleep_range(5000, 10000);
2108 	}
2109 	if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
2110 		dev_info(&adapter->pdev->dev, "Never saw reset\n");
2111 		goto continue_reset; /* act like the reset happened */
2112 	}
2113 
2114 	/* wait until the reset is complete and the PF is responding to us */
2115 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
2116 		/* sleep first to make sure a minimum wait time is met */
2117 		msleep(IAVF_RESET_WAIT_MS);
2118 
2119 		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2120 			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2121 		if (reg_val == VIRTCHNL_VFR_VFACTIVE)
2122 			break;
2123 	}
2124 
2125 	pci_set_master(adapter->pdev);
2126 
2127 	if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
2128 		dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
2129 			reg_val);
2130 		iavf_disable_vf(adapter);
2131 		clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2132 		return; /* Do not attempt to reinit. It's dead, Jim. */
2133 	}
2134 
2135 continue_reset:
2136 	/* We don't use netif_running() because it may be true prior to
2137 	 * ndo_open() returning, so we can't assume it means all our open
2138 	 * tasks have finished, since we're not holding the rtnl_lock here.
2139 	 */
2140 	running = ((adapter->state == __IAVF_RUNNING) ||
2141 		   (adapter->state == __IAVF_RESETTING));
2142 
2143 	if (running) {
2144 		netif_carrier_off(netdev);
2145 		netif_tx_stop_all_queues(netdev);
2146 		adapter->link_up = false;
2147 		iavf_napi_disable_all(adapter);
2148 	}
2149 	iavf_irq_disable(adapter);
2150 
2151 	adapter->state = __IAVF_RESETTING;
2152 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2153 
2154 	/* free the Tx/Rx rings and descriptors, might be better to just
2155 	 * re-use them sometime in the future
2156 	 */
2157 	iavf_free_all_rx_resources(adapter);
2158 	iavf_free_all_tx_resources(adapter);
2159 
2160 	adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
2161 	/* kill and reinit the admin queue */
2162 	iavf_shutdown_adminq(hw);
2163 	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2164 	err = iavf_init_adminq(hw);
2165 	if (err)
2166 		dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
2167 			 err);
2168 	adapter->aq_required = 0;
2169 
2170 	if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
2171 		err = iavf_reinit_interrupt_scheme(adapter);
2172 		if (err)
2173 			goto reset_err;
2174 	}
2175 
2176 	adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
2177 	adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
2178 
2179 	spin_lock_bh(&adapter->mac_vlan_list_lock);
2180 
2181 	/* Delete filter for the current MAC address, it could have
2182 	 * been changed by the PF via administratively set MAC.
2183 	 * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
2184 	 */
2185 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2186 		if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
2187 			list_del(&f->list);
2188 			kfree(f);
2189 		}
2190 	}
2191 	/* re-add all MAC filters */
2192 	list_for_each_entry(f, &adapter->mac_filter_list, list) {
2193 		f->add = true;
2194 	}
2195 	/* re-add all VLAN filters */
2196 	list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
2197 		vlf->add = true;
2198 	}
2199 
2200 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
2201 
2202 	/* check if TCs are running and re-add all cloud filters */
2203 	spin_lock_bh(&adapter->cloud_filter_list_lock);
2204 	if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
2205 	    adapter->num_tc) {
2206 		list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
2207 			cf->add = true;
2208 		}
2209 	}
2210 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2211 
2212 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
2213 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
2214 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
2215 	iavf_misc_irq_enable(adapter);
2216 
2217 	mod_delayed_work(iavf_wq, &adapter->watchdog_task, 2);
2218 
2219 	/* We were running when the reset started, so we need to restore some
2220 	 * state here.
2221 	 */
2222 	if (running) {
2223 		/* allocate transmit descriptors */
2224 		err = iavf_setup_all_tx_resources(adapter);
2225 		if (err)
2226 			goto reset_err;
2227 
2228 		/* allocate receive descriptors */
2229 		err = iavf_setup_all_rx_resources(adapter);
2230 		if (err)
2231 			goto reset_err;
2232 
2233 		if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
2234 			err = iavf_request_traffic_irqs(adapter, netdev->name);
2235 			if (err)
2236 				goto reset_err;
2237 
2238 			adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
2239 		}
2240 
2241 		iavf_configure(adapter);
2242 
2243 		iavf_up_complete(adapter);
2244 
2245 		iavf_irq_enable(adapter, true);
2246 	} else {
2247 		adapter->state = __IAVF_DOWN;
2248 		wake_up(&adapter->down_waitqueue);
2249 	}
2250 	clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2251 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2252 
2253 	return;
2254 reset_err:
2255 	clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2256 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2257 	dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
2258 	iavf_close(netdev);
2259 }
2260 
2261 /**
2262  * iavf_adminq_task - worker thread to clean the admin queue
2263  * @work: pointer to work_struct containing our data
2264  **/
2265 static void iavf_adminq_task(struct work_struct *work)
2266 {
2267 	struct iavf_adapter *adapter =
2268 		container_of(work, struct iavf_adapter, adminq_task);
2269 	struct iavf_hw *hw = &adapter->hw;
2270 	struct iavf_arq_event_info event;
2271 	enum virtchnl_ops v_op;
2272 	enum iavf_status ret, v_ret;
2273 	u32 val, oldval;
2274 	u16 pending;
2275 
2276 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2277 		goto out;
2278 
2279 	event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
2280 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2281 	if (!event.msg_buf)
2282 		goto out;
2283 
2284 	do {
2285 		ret = iavf_clean_arq_element(hw, &event, &pending);
2286 		v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2287 		v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
2288 
2289 		if (ret || !v_op)
2290 			break; /* No event to process or error cleaning ARQ */
2291 
2292 		iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2293 					 event.msg_len);
2294 		if (pending != 0)
2295 			memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
2296 	} while (pending);
2297 
2298 	if ((adapter->flags &
2299 	     (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED)) ||
2300 	    adapter->state == __IAVF_RESETTING)
2301 		goto freedom;
2302 
2303 	/* check for error indications */
2304 	val = rd32(hw, hw->aq.arq.len);
2305 	if (val == 0xdeadbeef) /* indicates device in reset */
2306 		goto freedom;
2307 	oldval = val;
2308 	if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
2309 		dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2310 		val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
2311 	}
2312 	if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
2313 		dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2314 		val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
2315 	}
2316 	if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
2317 		dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2318 		val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
2319 	}
2320 	if (oldval != val)
2321 		wr32(hw, hw->aq.arq.len, val);
2322 
2323 	val = rd32(hw, hw->aq.asq.len);
2324 	oldval = val;
2325 	if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
2326 		dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2327 		val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
2328 	}
2329 	if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
2330 		dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2331 		val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
2332 	}
2333 	if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
2334 		dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2335 		val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
2336 	}
2337 	if (oldval != val)
2338 		wr32(hw, hw->aq.asq.len, val);
2339 
2340 freedom:
2341 	kfree(event.msg_buf);
2342 out:
2343 	/* re-enable Admin queue interrupt cause */
2344 	iavf_misc_irq_enable(adapter);
2345 }
2346 
2347 /**
2348  * iavf_client_task - worker thread to perform client work
2349  * @work: pointer to work_struct containing our data
2350  *
2351  * This task handles client interactions. Because client calls can be
2352  * reentrant, we can't handle them in the watchdog.
2353  **/
2354 static void iavf_client_task(struct work_struct *work)
2355 {
2356 	struct iavf_adapter *adapter =
2357 		container_of(work, struct iavf_adapter, client_task.work);
2358 
2359 	/* If we can't get the client bit, just give up. We'll be rescheduled
2360 	 * later.
2361 	 */
2362 
2363 	if (test_and_set_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section))
2364 		return;
2365 
2366 	if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2367 		iavf_client_subtask(adapter);
2368 		adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
2369 		goto out;
2370 	}
2371 	if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2372 		iavf_notify_client_l2_params(&adapter->vsi);
2373 		adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2374 		goto out;
2375 	}
2376 	if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_CLOSE) {
2377 		iavf_notify_client_close(&adapter->vsi, false);
2378 		adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_CLOSE;
2379 		goto out;
2380 	}
2381 	if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
2382 		iavf_notify_client_open(&adapter->vsi);
2383 		adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
2384 	}
2385 out:
2386 	clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2387 }
2388 
2389 /**
2390  * iavf_free_all_tx_resources - Free Tx Resources for All Queues
2391  * @adapter: board private structure
2392  *
2393  * Free all transmit software resources
2394  **/
2395 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
2396 {
2397 	int i;
2398 
2399 	if (!adapter->tx_rings)
2400 		return;
2401 
2402 	for (i = 0; i < adapter->num_active_queues; i++)
2403 		if (adapter->tx_rings[i].desc)
2404 			iavf_free_tx_resources(&adapter->tx_rings[i]);
2405 }
2406 
2407 /**
2408  * iavf_setup_all_tx_resources - allocate all queues Tx resources
2409  * @adapter: board private structure
2410  *
2411  * If this function returns with an error, then it's possible one or
2412  * more of the rings is populated (while the rest are not).  It is the
2413  * callers duty to clean those orphaned rings.
2414  *
2415  * Return 0 on success, negative on failure
2416  **/
2417 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
2418 {
2419 	int i, err = 0;
2420 
2421 	for (i = 0; i < adapter->num_active_queues; i++) {
2422 		adapter->tx_rings[i].count = adapter->tx_desc_count;
2423 		err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
2424 		if (!err)
2425 			continue;
2426 		dev_err(&adapter->pdev->dev,
2427 			"Allocation for Tx Queue %u failed\n", i);
2428 		break;
2429 	}
2430 
2431 	return err;
2432 }
2433 
2434 /**
2435  * iavf_setup_all_rx_resources - allocate all queues Rx resources
2436  * @adapter: board private structure
2437  *
2438  * If this function returns with an error, then it's possible one or
2439  * more of the rings is populated (while the rest are not).  It is the
2440  * callers duty to clean those orphaned rings.
2441  *
2442  * Return 0 on success, negative on failure
2443  **/
2444 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
2445 {
2446 	int i, err = 0;
2447 
2448 	for (i = 0; i < adapter->num_active_queues; i++) {
2449 		adapter->rx_rings[i].count = adapter->rx_desc_count;
2450 		err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
2451 		if (!err)
2452 			continue;
2453 		dev_err(&adapter->pdev->dev,
2454 			"Allocation for Rx Queue %u failed\n", i);
2455 		break;
2456 	}
2457 	return err;
2458 }
2459 
2460 /**
2461  * iavf_free_all_rx_resources - Free Rx Resources for All Queues
2462  * @adapter: board private structure
2463  *
2464  * Free all receive software resources
2465  **/
2466 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
2467 {
2468 	int i;
2469 
2470 	if (!adapter->rx_rings)
2471 		return;
2472 
2473 	for (i = 0; i < adapter->num_active_queues; i++)
2474 		if (adapter->rx_rings[i].desc)
2475 			iavf_free_rx_resources(&adapter->rx_rings[i]);
2476 }
2477 
2478 /**
2479  * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
2480  * @adapter: board private structure
2481  * @max_tx_rate: max Tx bw for a tc
2482  **/
2483 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
2484 				      u64 max_tx_rate)
2485 {
2486 	int speed = 0, ret = 0;
2487 
2488 	if (ADV_LINK_SUPPORT(adapter)) {
2489 		if (adapter->link_speed_mbps < U32_MAX) {
2490 			speed = adapter->link_speed_mbps;
2491 			goto validate_bw;
2492 		} else {
2493 			dev_err(&adapter->pdev->dev, "Unknown link speed\n");
2494 			return -EINVAL;
2495 		}
2496 	}
2497 
2498 	switch (adapter->link_speed) {
2499 	case VIRTCHNL_LINK_SPEED_40GB:
2500 		speed = SPEED_40000;
2501 		break;
2502 	case VIRTCHNL_LINK_SPEED_25GB:
2503 		speed = SPEED_25000;
2504 		break;
2505 	case VIRTCHNL_LINK_SPEED_20GB:
2506 		speed = SPEED_20000;
2507 		break;
2508 	case VIRTCHNL_LINK_SPEED_10GB:
2509 		speed = SPEED_10000;
2510 		break;
2511 	case VIRTCHNL_LINK_SPEED_5GB:
2512 		speed = SPEED_5000;
2513 		break;
2514 	case VIRTCHNL_LINK_SPEED_2_5GB:
2515 		speed = SPEED_2500;
2516 		break;
2517 	case VIRTCHNL_LINK_SPEED_1GB:
2518 		speed = SPEED_1000;
2519 		break;
2520 	case VIRTCHNL_LINK_SPEED_100MB:
2521 		speed = SPEED_100;
2522 		break;
2523 	default:
2524 		break;
2525 	}
2526 
2527 validate_bw:
2528 	if (max_tx_rate > speed) {
2529 		dev_err(&adapter->pdev->dev,
2530 			"Invalid tx rate specified\n");
2531 		ret = -EINVAL;
2532 	}
2533 
2534 	return ret;
2535 }
2536 
2537 /**
2538  * iavf_validate_channel_config - validate queue mapping info
2539  * @adapter: board private structure
2540  * @mqprio_qopt: queue parameters
2541  *
2542  * This function validates if the config provided by the user to
2543  * configure queue channels is valid or not. Returns 0 on a valid
2544  * config.
2545  **/
2546 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
2547 				   struct tc_mqprio_qopt_offload *mqprio_qopt)
2548 {
2549 	u64 total_max_rate = 0;
2550 	int i, num_qps = 0;
2551 	u64 tx_rate = 0;
2552 	int ret = 0;
2553 
2554 	if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
2555 	    mqprio_qopt->qopt.num_tc < 1)
2556 		return -EINVAL;
2557 
2558 	for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2559 		if (!mqprio_qopt->qopt.count[i] ||
2560 		    mqprio_qopt->qopt.offset[i] != num_qps)
2561 			return -EINVAL;
2562 		if (mqprio_qopt->min_rate[i]) {
2563 			dev_err(&adapter->pdev->dev,
2564 				"Invalid min tx rate (greater than 0) specified\n");
2565 			return -EINVAL;
2566 		}
2567 		/*convert to Mbps */
2568 		tx_rate = div_u64(mqprio_qopt->max_rate[i],
2569 				  IAVF_MBPS_DIVISOR);
2570 		total_max_rate += tx_rate;
2571 		num_qps += mqprio_qopt->qopt.count[i];
2572 	}
2573 	if (num_qps > IAVF_MAX_REQ_QUEUES)
2574 		return -EINVAL;
2575 
2576 	ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
2577 	return ret;
2578 }
2579 
2580 /**
2581  * iavf_del_all_cloud_filters - delete all cloud filters
2582  * on the traffic classes
2583  **/
2584 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
2585 {
2586 	struct iavf_cloud_filter *cf, *cftmp;
2587 
2588 	spin_lock_bh(&adapter->cloud_filter_list_lock);
2589 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2590 				 list) {
2591 		list_del(&cf->list);
2592 		kfree(cf);
2593 		adapter->num_cloud_filters--;
2594 	}
2595 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2596 }
2597 
2598 /**
2599  * __iavf_setup_tc - configure multiple traffic classes
2600  * @netdev: network interface device structure
2601  * @type_date: tc offload data
2602  *
2603  * This function processes the config information provided by the
2604  * user to configure traffic classes/queue channels and packages the
2605  * information to request the PF to setup traffic classes.
2606  *
2607  * Returns 0 on success.
2608  **/
2609 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
2610 {
2611 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2612 	struct iavf_adapter *adapter = netdev_priv(netdev);
2613 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
2614 	u8 num_tc = 0, total_qps = 0;
2615 	int ret = 0, netdev_tc = 0;
2616 	u64 max_tx_rate;
2617 	u16 mode;
2618 	int i;
2619 
2620 	num_tc = mqprio_qopt->qopt.num_tc;
2621 	mode = mqprio_qopt->mode;
2622 
2623 	/* delete queue_channel */
2624 	if (!mqprio_qopt->qopt.hw) {
2625 		if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
2626 			/* reset the tc configuration */
2627 			netdev_reset_tc(netdev);
2628 			adapter->num_tc = 0;
2629 			netif_tx_stop_all_queues(netdev);
2630 			netif_tx_disable(netdev);
2631 			iavf_del_all_cloud_filters(adapter);
2632 			adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
2633 			goto exit;
2634 		} else {
2635 			return -EINVAL;
2636 		}
2637 	}
2638 
2639 	/* add queue channel */
2640 	if (mode == TC_MQPRIO_MODE_CHANNEL) {
2641 		if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2642 			dev_err(&adapter->pdev->dev, "ADq not supported\n");
2643 			return -EOPNOTSUPP;
2644 		}
2645 		if (adapter->ch_config.state != __IAVF_TC_INVALID) {
2646 			dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2647 			return -EINVAL;
2648 		}
2649 
2650 		ret = iavf_validate_ch_config(adapter, mqprio_qopt);
2651 		if (ret)
2652 			return ret;
2653 		/* Return if same TC config is requested */
2654 		if (adapter->num_tc == num_tc)
2655 			return 0;
2656 		adapter->num_tc = num_tc;
2657 
2658 		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2659 			if (i < num_tc) {
2660 				adapter->ch_config.ch_info[i].count =
2661 					mqprio_qopt->qopt.count[i];
2662 				adapter->ch_config.ch_info[i].offset =
2663 					mqprio_qopt->qopt.offset[i];
2664 				total_qps += mqprio_qopt->qopt.count[i];
2665 				max_tx_rate = mqprio_qopt->max_rate[i];
2666 				/* convert to Mbps */
2667 				max_tx_rate = div_u64(max_tx_rate,
2668 						      IAVF_MBPS_DIVISOR);
2669 				adapter->ch_config.ch_info[i].max_tx_rate =
2670 					max_tx_rate;
2671 			} else {
2672 				adapter->ch_config.ch_info[i].count = 1;
2673 				adapter->ch_config.ch_info[i].offset = 0;
2674 			}
2675 		}
2676 		adapter->ch_config.total_qps = total_qps;
2677 		netif_tx_stop_all_queues(netdev);
2678 		netif_tx_disable(netdev);
2679 		adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
2680 		netdev_reset_tc(netdev);
2681 		/* Report the tc mapping up the stack */
2682 		netdev_set_num_tc(adapter->netdev, num_tc);
2683 		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2684 			u16 qcount = mqprio_qopt->qopt.count[i];
2685 			u16 qoffset = mqprio_qopt->qopt.offset[i];
2686 
2687 			if (i < num_tc)
2688 				netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2689 						    qoffset);
2690 		}
2691 	}
2692 exit:
2693 	return ret;
2694 }
2695 
2696 /**
2697  * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
2698  * @adapter: board private structure
2699  * @cls_flower: pointer to struct flow_cls_offload
2700  * @filter: pointer to cloud filter structure
2701  */
2702 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
2703 				 struct flow_cls_offload *f,
2704 				 struct iavf_cloud_filter *filter)
2705 {
2706 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
2707 	struct flow_dissector *dissector = rule->match.dissector;
2708 	u16 n_proto_mask = 0;
2709 	u16 n_proto_key = 0;
2710 	u8 field_flags = 0;
2711 	u16 addr_type = 0;
2712 	u16 n_proto = 0;
2713 	int i = 0;
2714 	struct virtchnl_filter *vf = &filter->f;
2715 
2716 	if (dissector->used_keys &
2717 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2718 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
2719 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2720 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
2721 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2722 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2723 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
2724 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2725 		dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2726 			dissector->used_keys);
2727 		return -EOPNOTSUPP;
2728 	}
2729 
2730 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2731 		struct flow_match_enc_keyid match;
2732 
2733 		flow_rule_match_enc_keyid(rule, &match);
2734 		if (match.mask->keyid != 0)
2735 			field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
2736 	}
2737 
2738 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
2739 		struct flow_match_basic match;
2740 
2741 		flow_rule_match_basic(rule, &match);
2742 		n_proto_key = ntohs(match.key->n_proto);
2743 		n_proto_mask = ntohs(match.mask->n_proto);
2744 
2745 		if (n_proto_key == ETH_P_ALL) {
2746 			n_proto_key = 0;
2747 			n_proto_mask = 0;
2748 		}
2749 		n_proto = n_proto_key & n_proto_mask;
2750 		if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2751 			return -EINVAL;
2752 		if (n_proto == ETH_P_IPV6) {
2753 			/* specify flow type as TCP IPv6 */
2754 			vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2755 		}
2756 
2757 		if (match.key->ip_proto != IPPROTO_TCP) {
2758 			dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2759 			return -EINVAL;
2760 		}
2761 	}
2762 
2763 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2764 		struct flow_match_eth_addrs match;
2765 
2766 		flow_rule_match_eth_addrs(rule, &match);
2767 
2768 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
2769 		if (!is_zero_ether_addr(match.mask->dst)) {
2770 			if (is_broadcast_ether_addr(match.mask->dst)) {
2771 				field_flags |= IAVF_CLOUD_FIELD_OMAC;
2772 			} else {
2773 				dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2774 					match.mask->dst);
2775 				return IAVF_ERR_CONFIG;
2776 			}
2777 		}
2778 
2779 		if (!is_zero_ether_addr(match.mask->src)) {
2780 			if (is_broadcast_ether_addr(match.mask->src)) {
2781 				field_flags |= IAVF_CLOUD_FIELD_IMAC;
2782 			} else {
2783 				dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2784 					match.mask->src);
2785 				return IAVF_ERR_CONFIG;
2786 			}
2787 		}
2788 
2789 		if (!is_zero_ether_addr(match.key->dst))
2790 			if (is_valid_ether_addr(match.key->dst) ||
2791 			    is_multicast_ether_addr(match.key->dst)) {
2792 				/* set the mask if a valid dst_mac address */
2793 				for (i = 0; i < ETH_ALEN; i++)
2794 					vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2795 				ether_addr_copy(vf->data.tcp_spec.dst_mac,
2796 						match.key->dst);
2797 			}
2798 
2799 		if (!is_zero_ether_addr(match.key->src))
2800 			if (is_valid_ether_addr(match.key->src) ||
2801 			    is_multicast_ether_addr(match.key->src)) {
2802 				/* set the mask if a valid dst_mac address */
2803 				for (i = 0; i < ETH_ALEN; i++)
2804 					vf->mask.tcp_spec.src_mac[i] |= 0xff;
2805 				ether_addr_copy(vf->data.tcp_spec.src_mac,
2806 						match.key->src);
2807 		}
2808 	}
2809 
2810 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
2811 		struct flow_match_vlan match;
2812 
2813 		flow_rule_match_vlan(rule, &match);
2814 		if (match.mask->vlan_id) {
2815 			if (match.mask->vlan_id == VLAN_VID_MASK) {
2816 				field_flags |= IAVF_CLOUD_FIELD_IVLAN;
2817 			} else {
2818 				dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2819 					match.mask->vlan_id);
2820 				return IAVF_ERR_CONFIG;
2821 			}
2822 		}
2823 		vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2824 		vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
2825 	}
2826 
2827 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
2828 		struct flow_match_control match;
2829 
2830 		flow_rule_match_control(rule, &match);
2831 		addr_type = match.key->addr_type;
2832 	}
2833 
2834 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2835 		struct flow_match_ipv4_addrs match;
2836 
2837 		flow_rule_match_ipv4_addrs(rule, &match);
2838 		if (match.mask->dst) {
2839 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
2840 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2841 			} else {
2842 				dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2843 					be32_to_cpu(match.mask->dst));
2844 				return IAVF_ERR_CONFIG;
2845 			}
2846 		}
2847 
2848 		if (match.mask->src) {
2849 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
2850 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2851 			} else {
2852 				dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2853 					be32_to_cpu(match.mask->dst));
2854 				return IAVF_ERR_CONFIG;
2855 			}
2856 		}
2857 
2858 		if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
2859 			dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2860 			return IAVF_ERR_CONFIG;
2861 		}
2862 		if (match.key->dst) {
2863 			vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2864 			vf->data.tcp_spec.dst_ip[0] = match.key->dst;
2865 		}
2866 		if (match.key->src) {
2867 			vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2868 			vf->data.tcp_spec.src_ip[0] = match.key->src;
2869 		}
2870 	}
2871 
2872 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2873 		struct flow_match_ipv6_addrs match;
2874 
2875 		flow_rule_match_ipv6_addrs(rule, &match);
2876 
2877 		/* validate mask, make sure it is not IPV6_ADDR_ANY */
2878 		if (ipv6_addr_any(&match.mask->dst)) {
2879 			dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2880 				IPV6_ADDR_ANY);
2881 			return IAVF_ERR_CONFIG;
2882 		}
2883 
2884 		/* src and dest IPv6 address should not be LOOPBACK
2885 		 * (0:0:0:0:0:0:0:1) which can be represented as ::1
2886 		 */
2887 		if (ipv6_addr_loopback(&match.key->dst) ||
2888 		    ipv6_addr_loopback(&match.key->src)) {
2889 			dev_err(&adapter->pdev->dev,
2890 				"ipv6 addr should not be loopback\n");
2891 			return IAVF_ERR_CONFIG;
2892 		}
2893 		if (!ipv6_addr_any(&match.mask->dst) ||
2894 		    !ipv6_addr_any(&match.mask->src))
2895 			field_flags |= IAVF_CLOUD_FIELD_IIP;
2896 
2897 		for (i = 0; i < 4; i++)
2898 			vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2899 		memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
2900 		       sizeof(vf->data.tcp_spec.dst_ip));
2901 		for (i = 0; i < 4; i++)
2902 			vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2903 		memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
2904 		       sizeof(vf->data.tcp_spec.src_ip));
2905 	}
2906 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
2907 		struct flow_match_ports match;
2908 
2909 		flow_rule_match_ports(rule, &match);
2910 		if (match.mask->src) {
2911 			if (match.mask->src == cpu_to_be16(0xffff)) {
2912 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2913 			} else {
2914 				dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
2915 					be16_to_cpu(match.mask->src));
2916 				return IAVF_ERR_CONFIG;
2917 			}
2918 		}
2919 
2920 		if (match.mask->dst) {
2921 			if (match.mask->dst == cpu_to_be16(0xffff)) {
2922 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2923 			} else {
2924 				dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
2925 					be16_to_cpu(match.mask->dst));
2926 				return IAVF_ERR_CONFIG;
2927 			}
2928 		}
2929 		if (match.key->dst) {
2930 			vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
2931 			vf->data.tcp_spec.dst_port = match.key->dst;
2932 		}
2933 
2934 		if (match.key->src) {
2935 			vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
2936 			vf->data.tcp_spec.src_port = match.key->src;
2937 		}
2938 	}
2939 	vf->field_flags = field_flags;
2940 
2941 	return 0;
2942 }
2943 
2944 /**
2945  * iavf_handle_tclass - Forward to a traffic class on the device
2946  * @adapter: board private structure
2947  * @tc: traffic class index on the device
2948  * @filter: pointer to cloud filter structure
2949  */
2950 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
2951 			      struct iavf_cloud_filter *filter)
2952 {
2953 	if (tc == 0)
2954 		return 0;
2955 	if (tc < adapter->num_tc) {
2956 		if (!filter->f.data.tcp_spec.dst_port) {
2957 			dev_err(&adapter->pdev->dev,
2958 				"Specify destination port to redirect to traffic class other than TC0\n");
2959 			return -EINVAL;
2960 		}
2961 	}
2962 	/* redirect to a traffic class on the same device */
2963 	filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
2964 	filter->f.action_meta = tc;
2965 	return 0;
2966 }
2967 
2968 /**
2969  * iavf_configure_clsflower - Add tc flower filters
2970  * @adapter: board private structure
2971  * @cls_flower: Pointer to struct flow_cls_offload
2972  */
2973 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
2974 				    struct flow_cls_offload *cls_flower)
2975 {
2976 	int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
2977 	struct iavf_cloud_filter *filter = NULL;
2978 	int err = -EINVAL, count = 50;
2979 
2980 	if (tc < 0) {
2981 		dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
2982 		return -EINVAL;
2983 	}
2984 
2985 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2986 	if (!filter)
2987 		return -ENOMEM;
2988 
2989 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
2990 				&adapter->crit_section)) {
2991 		if (--count == 0)
2992 			goto err;
2993 		udelay(1);
2994 	}
2995 
2996 	filter->cookie = cls_flower->cookie;
2997 
2998 	/* set the mask to all zeroes to begin with */
2999 	memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
3000 	/* start out with flow type and eth type IPv4 to begin with */
3001 	filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
3002 	err = iavf_parse_cls_flower(adapter, cls_flower, filter);
3003 	if (err < 0)
3004 		goto err;
3005 
3006 	err = iavf_handle_tclass(adapter, tc, filter);
3007 	if (err < 0)
3008 		goto err;
3009 
3010 	/* add filter to the list */
3011 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3012 	list_add_tail(&filter->list, &adapter->cloud_filter_list);
3013 	adapter->num_cloud_filters++;
3014 	filter->add = true;
3015 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3016 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3017 err:
3018 	if (err)
3019 		kfree(filter);
3020 
3021 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3022 	return err;
3023 }
3024 
3025 /* iavf_find_cf - Find the cloud filter in the list
3026  * @adapter: Board private structure
3027  * @cookie: filter specific cookie
3028  *
3029  * Returns ptr to the filter object or NULL. Must be called while holding the
3030  * cloud_filter_list_lock.
3031  */
3032 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
3033 					      unsigned long *cookie)
3034 {
3035 	struct iavf_cloud_filter *filter = NULL;
3036 
3037 	if (!cookie)
3038 		return NULL;
3039 
3040 	list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
3041 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
3042 			return filter;
3043 	}
3044 	return NULL;
3045 }
3046 
3047 /**
3048  * iavf_delete_clsflower - Remove tc flower filters
3049  * @adapter: board private structure
3050  * @cls_flower: Pointer to struct flow_cls_offload
3051  */
3052 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
3053 				 struct flow_cls_offload *cls_flower)
3054 {
3055 	struct iavf_cloud_filter *filter = NULL;
3056 	int err = 0;
3057 
3058 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3059 	filter = iavf_find_cf(adapter, &cls_flower->cookie);
3060 	if (filter) {
3061 		filter->del = true;
3062 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
3063 	} else {
3064 		err = -EINVAL;
3065 	}
3066 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3067 
3068 	return err;
3069 }
3070 
3071 /**
3072  * iavf_setup_tc_cls_flower - flower classifier offloads
3073  * @netdev: net device to configure
3074  * @type_data: offload data
3075  */
3076 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
3077 				    struct flow_cls_offload *cls_flower)
3078 {
3079 	switch (cls_flower->command) {
3080 	case FLOW_CLS_REPLACE:
3081 		return iavf_configure_clsflower(adapter, cls_flower);
3082 	case FLOW_CLS_DESTROY:
3083 		return iavf_delete_clsflower(adapter, cls_flower);
3084 	case FLOW_CLS_STATS:
3085 		return -EOPNOTSUPP;
3086 	default:
3087 		return -EOPNOTSUPP;
3088 	}
3089 }
3090 
3091 /**
3092  * iavf_setup_tc_block_cb - block callback for tc
3093  * @type: type of offload
3094  * @type_data: offload data
3095  * @cb_priv:
3096  *
3097  * This function is the block callback for traffic classes
3098  **/
3099 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3100 				  void *cb_priv)
3101 {
3102 	struct iavf_adapter *adapter = cb_priv;
3103 
3104 	if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
3105 		return -EOPNOTSUPP;
3106 
3107 	switch (type) {
3108 	case TC_SETUP_CLSFLOWER:
3109 		return iavf_setup_tc_cls_flower(cb_priv, type_data);
3110 	default:
3111 		return -EOPNOTSUPP;
3112 	}
3113 }
3114 
3115 static LIST_HEAD(iavf_block_cb_list);
3116 
3117 /**
3118  * iavf_setup_tc - configure multiple traffic classes
3119  * @netdev: network interface device structure
3120  * @type: type of offload
3121  * @type_date: tc offload data
3122  *
3123  * This function is the callback to ndo_setup_tc in the
3124  * netdev_ops.
3125  *
3126  * Returns 0 on success
3127  **/
3128 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
3129 			 void *type_data)
3130 {
3131 	struct iavf_adapter *adapter = netdev_priv(netdev);
3132 
3133 	switch (type) {
3134 	case TC_SETUP_QDISC_MQPRIO:
3135 		return __iavf_setup_tc(netdev, type_data);
3136 	case TC_SETUP_BLOCK:
3137 		return flow_block_cb_setup_simple(type_data,
3138 						  &iavf_block_cb_list,
3139 						  iavf_setup_tc_block_cb,
3140 						  adapter, adapter, true);
3141 	default:
3142 		return -EOPNOTSUPP;
3143 	}
3144 }
3145 
3146 /**
3147  * iavf_open - Called when a network interface is made active
3148  * @netdev: network interface device structure
3149  *
3150  * Returns 0 on success, negative value on failure
3151  *
3152  * The open entry point is called when a network interface is made
3153  * active by the system (IFF_UP).  At this point all resources needed
3154  * for transmit and receive operations are allocated, the interrupt
3155  * handler is registered with the OS, the watchdog is started,
3156  * and the stack is notified that the interface is ready.
3157  **/
3158 static int iavf_open(struct net_device *netdev)
3159 {
3160 	struct iavf_adapter *adapter = netdev_priv(netdev);
3161 	int err;
3162 
3163 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
3164 		dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
3165 		return -EIO;
3166 	}
3167 
3168 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3169 				&adapter->crit_section))
3170 		usleep_range(500, 1000);
3171 
3172 	if (adapter->state != __IAVF_DOWN) {
3173 		err = -EBUSY;
3174 		goto err_unlock;
3175 	}
3176 
3177 	/* allocate transmit descriptors */
3178 	err = iavf_setup_all_tx_resources(adapter);
3179 	if (err)
3180 		goto err_setup_tx;
3181 
3182 	/* allocate receive descriptors */
3183 	err = iavf_setup_all_rx_resources(adapter);
3184 	if (err)
3185 		goto err_setup_rx;
3186 
3187 	/* clear any pending interrupts, may auto mask */
3188 	err = iavf_request_traffic_irqs(adapter, netdev->name);
3189 	if (err)
3190 		goto err_req_irq;
3191 
3192 	spin_lock_bh(&adapter->mac_vlan_list_lock);
3193 
3194 	iavf_add_filter(adapter, adapter->hw.mac.addr);
3195 
3196 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3197 
3198 	iavf_configure(adapter);
3199 
3200 	iavf_up_complete(adapter);
3201 
3202 	iavf_irq_enable(adapter, true);
3203 
3204 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3205 
3206 	return 0;
3207 
3208 err_req_irq:
3209 	iavf_down(adapter);
3210 	iavf_free_traffic_irqs(adapter);
3211 err_setup_rx:
3212 	iavf_free_all_rx_resources(adapter);
3213 err_setup_tx:
3214 	iavf_free_all_tx_resources(adapter);
3215 err_unlock:
3216 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3217 
3218 	return err;
3219 }
3220 
3221 /**
3222  * iavf_close - Disables a network interface
3223  * @netdev: network interface device structure
3224  *
3225  * Returns 0, this is not allowed to fail
3226  *
3227  * The close entry point is called when an interface is de-activated
3228  * by the OS.  The hardware is still under the drivers control, but
3229  * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3230  * are freed, along with all transmit and receive resources.
3231  **/
3232 static int iavf_close(struct net_device *netdev)
3233 {
3234 	struct iavf_adapter *adapter = netdev_priv(netdev);
3235 	int status;
3236 
3237 	if (adapter->state <= __IAVF_DOWN_PENDING)
3238 		return 0;
3239 
3240 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3241 				&adapter->crit_section))
3242 		usleep_range(500, 1000);
3243 
3244 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3245 	if (CLIENT_ENABLED(adapter))
3246 		adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3247 
3248 	iavf_down(adapter);
3249 	adapter->state = __IAVF_DOWN_PENDING;
3250 	iavf_free_traffic_irqs(adapter);
3251 
3252 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3253 
3254 	/* We explicitly don't free resources here because the hardware is
3255 	 * still active and can DMA into memory. Resources are cleared in
3256 	 * iavf_virtchnl_completion() after we get confirmation from the PF
3257 	 * driver that the rings have been stopped.
3258 	 *
3259 	 * Also, we wait for state to transition to __IAVF_DOWN before
3260 	 * returning. State change occurs in iavf_virtchnl_completion() after
3261 	 * VF resources are released (which occurs after PF driver processes and
3262 	 * responds to admin queue commands).
3263 	 */
3264 
3265 	status = wait_event_timeout(adapter->down_waitqueue,
3266 				    adapter->state == __IAVF_DOWN,
3267 				    msecs_to_jiffies(500));
3268 	if (!status)
3269 		netdev_warn(netdev, "Device resources not yet released\n");
3270 	return 0;
3271 }
3272 
3273 /**
3274  * iavf_change_mtu - Change the Maximum Transfer Unit
3275  * @netdev: network interface device structure
3276  * @new_mtu: new value for maximum frame size
3277  *
3278  * Returns 0 on success, negative on failure
3279  **/
3280 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
3281 {
3282 	struct iavf_adapter *adapter = netdev_priv(netdev);
3283 
3284 	netdev->mtu = new_mtu;
3285 	if (CLIENT_ENABLED(adapter)) {
3286 		iavf_notify_client_l2_params(&adapter->vsi);
3287 		adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3288 	}
3289 	adapter->flags |= IAVF_FLAG_RESET_NEEDED;
3290 	queue_work(iavf_wq, &adapter->reset_task);
3291 
3292 	return 0;
3293 }
3294 
3295 /**
3296  * iavf_set_features - set the netdev feature flags
3297  * @netdev: ptr to the netdev being adjusted
3298  * @features: the feature set that the stack is suggesting
3299  * Note: expects to be called while under rtnl_lock()
3300  **/
3301 static int iavf_set_features(struct net_device *netdev,
3302 			     netdev_features_t features)
3303 {
3304 	struct iavf_adapter *adapter = netdev_priv(netdev);
3305 
3306 	/* Don't allow changing VLAN_RX flag when adapter is not capable
3307 	 * of VLAN offload
3308 	 */
3309 	if (!VLAN_ALLOWED(adapter)) {
3310 		if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX)
3311 			return -EINVAL;
3312 	} else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3313 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
3314 			adapter->aq_required |=
3315 				IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3316 		else
3317 			adapter->aq_required |=
3318 				IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3319 	}
3320 
3321 	return 0;
3322 }
3323 
3324 /**
3325  * iavf_features_check - Validate encapsulated packet conforms to limits
3326  * @skb: skb buff
3327  * @dev: This physical port's netdev
3328  * @features: Offload features that the stack believes apply
3329  **/
3330 static netdev_features_t iavf_features_check(struct sk_buff *skb,
3331 					     struct net_device *dev,
3332 					     netdev_features_t features)
3333 {
3334 	size_t len;
3335 
3336 	/* No point in doing any of this if neither checksum nor GSO are
3337 	 * being requested for this frame.  We can rule out both by just
3338 	 * checking for CHECKSUM_PARTIAL
3339 	 */
3340 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3341 		return features;
3342 
3343 	/* We cannot support GSO if the MSS is going to be less than
3344 	 * 64 bytes.  If it is then we need to drop support for GSO.
3345 	 */
3346 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3347 		features &= ~NETIF_F_GSO_MASK;
3348 
3349 	/* MACLEN can support at most 63 words */
3350 	len = skb_network_header(skb) - skb->data;
3351 	if (len & ~(63 * 2))
3352 		goto out_err;
3353 
3354 	/* IPLEN and EIPLEN can support at most 127 dwords */
3355 	len = skb_transport_header(skb) - skb_network_header(skb);
3356 	if (len & ~(127 * 4))
3357 		goto out_err;
3358 
3359 	if (skb->encapsulation) {
3360 		/* L4TUNLEN can support 127 words */
3361 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
3362 		if (len & ~(127 * 2))
3363 			goto out_err;
3364 
3365 		/* IPLEN can support at most 127 dwords */
3366 		len = skb_inner_transport_header(skb) -
3367 		      skb_inner_network_header(skb);
3368 		if (len & ~(127 * 4))
3369 			goto out_err;
3370 	}
3371 
3372 	/* No need to validate L4LEN as TCP is the only protocol with a
3373 	 * a flexible value and we support all possible values supported
3374 	 * by TCP, which is at most 15 dwords
3375 	 */
3376 
3377 	return features;
3378 out_err:
3379 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3380 }
3381 
3382 /**
3383  * iavf_fix_features - fix up the netdev feature bits
3384  * @netdev: our net device
3385  * @features: desired feature bits
3386  *
3387  * Returns fixed-up features bits
3388  **/
3389 static netdev_features_t iavf_fix_features(struct net_device *netdev,
3390 					   netdev_features_t features)
3391 {
3392 	struct iavf_adapter *adapter = netdev_priv(netdev);
3393 
3394 	if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3395 		features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3396 			      NETIF_F_HW_VLAN_CTAG_RX |
3397 			      NETIF_F_HW_VLAN_CTAG_FILTER);
3398 
3399 	return features;
3400 }
3401 
3402 static const struct net_device_ops iavf_netdev_ops = {
3403 	.ndo_open		= iavf_open,
3404 	.ndo_stop		= iavf_close,
3405 	.ndo_start_xmit		= iavf_xmit_frame,
3406 	.ndo_set_rx_mode	= iavf_set_rx_mode,
3407 	.ndo_validate_addr	= eth_validate_addr,
3408 	.ndo_set_mac_address	= iavf_set_mac,
3409 	.ndo_change_mtu		= iavf_change_mtu,
3410 	.ndo_tx_timeout		= iavf_tx_timeout,
3411 	.ndo_vlan_rx_add_vid	= iavf_vlan_rx_add_vid,
3412 	.ndo_vlan_rx_kill_vid	= iavf_vlan_rx_kill_vid,
3413 	.ndo_features_check	= iavf_features_check,
3414 	.ndo_fix_features	= iavf_fix_features,
3415 	.ndo_set_features	= iavf_set_features,
3416 	.ndo_setup_tc		= iavf_setup_tc,
3417 };
3418 
3419 /**
3420  * iavf_check_reset_complete - check that VF reset is complete
3421  * @hw: pointer to hw struct
3422  *
3423  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3424  **/
3425 static int iavf_check_reset_complete(struct iavf_hw *hw)
3426 {
3427 	u32 rstat;
3428 	int i;
3429 
3430 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3431 		rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
3432 			     IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3433 		if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3434 		    (rstat == VIRTCHNL_VFR_COMPLETED))
3435 			return 0;
3436 		usleep_range(10, 20);
3437 	}
3438 	return -EBUSY;
3439 }
3440 
3441 /**
3442  * iavf_process_config - Process the config information we got from the PF
3443  * @adapter: board private structure
3444  *
3445  * Verify that we have a valid config struct, and set up our netdev features
3446  * and our VSI struct.
3447  **/
3448 int iavf_process_config(struct iavf_adapter *adapter)
3449 {
3450 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
3451 	int i, num_req_queues = adapter->num_req_queues;
3452 	struct net_device *netdev = adapter->netdev;
3453 	struct iavf_vsi *vsi = &adapter->vsi;
3454 	netdev_features_t hw_enc_features;
3455 	netdev_features_t hw_features;
3456 
3457 	/* got VF config message back from PF, now we can parse it */
3458 	for (i = 0; i < vfres->num_vsis; i++) {
3459 		if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3460 			adapter->vsi_res = &vfres->vsi_res[i];
3461 	}
3462 	if (!adapter->vsi_res) {
3463 		dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3464 		return -ENODEV;
3465 	}
3466 
3467 	if (num_req_queues &&
3468 	    num_req_queues > adapter->vsi_res->num_queue_pairs) {
3469 		/* Problem.  The PF gave us fewer queues than what we had
3470 		 * negotiated in our request.  Need a reset to see if we can't
3471 		 * get back to a working state.
3472 		 */
3473 		dev_err(&adapter->pdev->dev,
3474 			"Requested %d queues, but PF only gave us %d.\n",
3475 			num_req_queues,
3476 			adapter->vsi_res->num_queue_pairs);
3477 		adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
3478 		adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3479 		iavf_schedule_reset(adapter);
3480 		return -ENODEV;
3481 	}
3482 	adapter->num_req_queues = 0;
3483 
3484 	hw_enc_features = NETIF_F_SG			|
3485 			  NETIF_F_IP_CSUM		|
3486 			  NETIF_F_IPV6_CSUM		|
3487 			  NETIF_F_HIGHDMA		|
3488 			  NETIF_F_SOFT_FEATURES	|
3489 			  NETIF_F_TSO			|
3490 			  NETIF_F_TSO_ECN		|
3491 			  NETIF_F_TSO6			|
3492 			  NETIF_F_SCTP_CRC		|
3493 			  NETIF_F_RXHASH		|
3494 			  NETIF_F_RXCSUM		|
3495 			  0;
3496 
3497 	/* advertise to stack only if offloads for encapsulated packets is
3498 	 * supported
3499 	 */
3500 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3501 		hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL	|
3502 				   NETIF_F_GSO_GRE		|
3503 				   NETIF_F_GSO_GRE_CSUM		|
3504 				   NETIF_F_GSO_IPXIP4		|
3505 				   NETIF_F_GSO_IPXIP6		|
3506 				   NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3507 				   NETIF_F_GSO_PARTIAL		|
3508 				   0;
3509 
3510 		if (!(vfres->vf_cap_flags &
3511 		      VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3512 			netdev->gso_partial_features |=
3513 				NETIF_F_GSO_UDP_TUNNEL_CSUM;
3514 
3515 		netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3516 		netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3517 		netdev->hw_enc_features |= hw_enc_features;
3518 	}
3519 	/* record features VLANs can make use of */
3520 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3521 
3522 	/* Write features and hw_features separately to avoid polluting
3523 	 * with, or dropping, features that are set when we registered.
3524 	 */
3525 	hw_features = hw_enc_features;
3526 
3527 	/* Enable VLAN features if supported */
3528 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3529 		hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3530 				NETIF_F_HW_VLAN_CTAG_RX);
3531 	/* Enable cloud filter if ADQ is supported */
3532 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3533 		hw_features |= NETIF_F_HW_TC;
3534 
3535 	netdev->hw_features |= hw_features;
3536 
3537 	netdev->features |= hw_features;
3538 
3539 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3540 		netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3541 
3542 	netdev->priv_flags |= IFF_UNICAST_FLT;
3543 
3544 	/* Do not turn on offloads when they are requested to be turned off.
3545 	 * TSO needs minimum 576 bytes to work correctly.
3546 	 */
3547 	if (netdev->wanted_features) {
3548 		if (!(netdev->wanted_features & NETIF_F_TSO) ||
3549 		    netdev->mtu < 576)
3550 			netdev->features &= ~NETIF_F_TSO;
3551 		if (!(netdev->wanted_features & NETIF_F_TSO6) ||
3552 		    netdev->mtu < 576)
3553 			netdev->features &= ~NETIF_F_TSO6;
3554 		if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
3555 			netdev->features &= ~NETIF_F_TSO_ECN;
3556 		if (!(netdev->wanted_features & NETIF_F_GRO))
3557 			netdev->features &= ~NETIF_F_GRO;
3558 		if (!(netdev->wanted_features & NETIF_F_GSO))
3559 			netdev->features &= ~NETIF_F_GSO;
3560 	}
3561 
3562 	adapter->vsi.id = adapter->vsi_res->vsi_id;
3563 
3564 	adapter->vsi.back = adapter;
3565 	adapter->vsi.base_vector = 1;
3566 	adapter->vsi.work_limit = IAVF_DEFAULT_IRQ_WORK;
3567 	vsi->netdev = adapter->netdev;
3568 	vsi->qs_handle = adapter->vsi_res->qset_handle;
3569 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3570 		adapter->rss_key_size = vfres->rss_key_size;
3571 		adapter->rss_lut_size = vfres->rss_lut_size;
3572 	} else {
3573 		adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
3574 		adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
3575 	}
3576 
3577 	return 0;
3578 }
3579 
3580 /**
3581  * iavf_init_task - worker thread to perform delayed initialization
3582  * @work: pointer to work_struct containing our data
3583  *
3584  * This task completes the work that was begun in probe. Due to the nature
3585  * of VF-PF communications, we may need to wait tens of milliseconds to get
3586  * responses back from the PF. Rather than busy-wait in probe and bog down the
3587  * whole system, we'll do it in a task so we can sleep.
3588  * This task only runs during driver init. Once we've established
3589  * communications with the PF driver and set up our netdev, the watchdog
3590  * takes over.
3591  **/
3592 static void iavf_init_task(struct work_struct *work)
3593 {
3594 	struct iavf_adapter *adapter = container_of(work,
3595 						    struct iavf_adapter,
3596 						    init_task.work);
3597 	struct iavf_hw *hw = &adapter->hw;
3598 
3599 	switch (adapter->state) {
3600 	case __IAVF_STARTUP:
3601 		if (iavf_startup(adapter) < 0)
3602 			goto init_failed;
3603 		break;
3604 	case __IAVF_INIT_VERSION_CHECK:
3605 		if (iavf_init_version_check(adapter) < 0)
3606 			goto init_failed;
3607 		break;
3608 	case __IAVF_INIT_GET_RESOURCES:
3609 		if (iavf_init_get_resources(adapter) < 0)
3610 			goto init_failed;
3611 		return;
3612 	default:
3613 		goto init_failed;
3614 	}
3615 
3616 	queue_delayed_work(iavf_wq, &adapter->init_task,
3617 			   msecs_to_jiffies(30));
3618 	return;
3619 init_failed:
3620 	if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
3621 		dev_err(&adapter->pdev->dev,
3622 			"Failed to communicate with PF; waiting before retry\n");
3623 		adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
3624 		iavf_shutdown_adminq(hw);
3625 		adapter->state = __IAVF_STARTUP;
3626 		queue_delayed_work(iavf_wq, &adapter->init_task, HZ * 5);
3627 		return;
3628 	}
3629 	queue_delayed_work(iavf_wq, &adapter->init_task, HZ);
3630 }
3631 
3632 /**
3633  * iavf_shutdown - Shutdown the device in preparation for a reboot
3634  * @pdev: pci device structure
3635  **/
3636 static void iavf_shutdown(struct pci_dev *pdev)
3637 {
3638 	struct net_device *netdev = pci_get_drvdata(pdev);
3639 	struct iavf_adapter *adapter = netdev_priv(netdev);
3640 
3641 	netif_device_detach(netdev);
3642 
3643 	if (netif_running(netdev))
3644 		iavf_close(netdev);
3645 
3646 	/* Prevent the watchdog from running. */
3647 	adapter->state = __IAVF_REMOVE;
3648 	adapter->aq_required = 0;
3649 
3650 #ifdef CONFIG_PM
3651 	pci_save_state(pdev);
3652 
3653 #endif
3654 	pci_disable_device(pdev);
3655 }
3656 
3657 /**
3658  * iavf_probe - Device Initialization Routine
3659  * @pdev: PCI device information struct
3660  * @ent: entry in iavf_pci_tbl
3661  *
3662  * Returns 0 on success, negative on failure
3663  *
3664  * iavf_probe initializes an adapter identified by a pci_dev structure.
3665  * The OS initialization, configuring of the adapter private structure,
3666  * and a hardware reset occur.
3667  **/
3668 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3669 {
3670 	struct net_device *netdev;
3671 	struct iavf_adapter *adapter = NULL;
3672 	struct iavf_hw *hw = NULL;
3673 	int err;
3674 
3675 	err = pci_enable_device(pdev);
3676 	if (err)
3677 		return err;
3678 
3679 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3680 	if (err) {
3681 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3682 		if (err) {
3683 			dev_err(&pdev->dev,
3684 				"DMA configuration failed: 0x%x\n", err);
3685 			goto err_dma;
3686 		}
3687 	}
3688 
3689 	err = pci_request_regions(pdev, iavf_driver_name);
3690 	if (err) {
3691 		dev_err(&pdev->dev,
3692 			"pci_request_regions failed 0x%x\n", err);
3693 		goto err_pci_reg;
3694 	}
3695 
3696 	pci_enable_pcie_error_reporting(pdev);
3697 
3698 	pci_set_master(pdev);
3699 
3700 	netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
3701 				   IAVF_MAX_REQ_QUEUES);
3702 	if (!netdev) {
3703 		err = -ENOMEM;
3704 		goto err_alloc_etherdev;
3705 	}
3706 
3707 	SET_NETDEV_DEV(netdev, &pdev->dev);
3708 
3709 	pci_set_drvdata(pdev, netdev);
3710 	adapter = netdev_priv(netdev);
3711 
3712 	adapter->netdev = netdev;
3713 	adapter->pdev = pdev;
3714 
3715 	hw = &adapter->hw;
3716 	hw->back = adapter;
3717 
3718 	adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3719 	adapter->state = __IAVF_STARTUP;
3720 
3721 	/* Call save state here because it relies on the adapter struct. */
3722 	pci_save_state(pdev);
3723 
3724 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3725 			      pci_resource_len(pdev, 0));
3726 	if (!hw->hw_addr) {
3727 		err = -EIO;
3728 		goto err_ioremap;
3729 	}
3730 	hw->vendor_id = pdev->vendor;
3731 	hw->device_id = pdev->device;
3732 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3733 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
3734 	hw->subsystem_device_id = pdev->subsystem_device;
3735 	hw->bus.device = PCI_SLOT(pdev->devfn);
3736 	hw->bus.func = PCI_FUNC(pdev->devfn);
3737 	hw->bus.bus_id = pdev->bus->number;
3738 
3739 	/* set up the locks for the AQ, do this only once in probe
3740 	 * and destroy them only once in remove
3741 	 */
3742 	mutex_init(&hw->aq.asq_mutex);
3743 	mutex_init(&hw->aq.arq_mutex);
3744 
3745 	spin_lock_init(&adapter->mac_vlan_list_lock);
3746 	spin_lock_init(&adapter->cloud_filter_list_lock);
3747 
3748 	INIT_LIST_HEAD(&adapter->mac_filter_list);
3749 	INIT_LIST_HEAD(&adapter->vlan_filter_list);
3750 	INIT_LIST_HEAD(&adapter->cloud_filter_list);
3751 
3752 	INIT_WORK(&adapter->reset_task, iavf_reset_task);
3753 	INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
3754 	INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
3755 	INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
3756 	INIT_DELAYED_WORK(&adapter->init_task, iavf_init_task);
3757 	queue_delayed_work(iavf_wq, &adapter->init_task,
3758 			   msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3759 
3760 	/* Setup the wait queue for indicating transition to down status */
3761 	init_waitqueue_head(&adapter->down_waitqueue);
3762 
3763 	return 0;
3764 
3765 err_ioremap:
3766 	free_netdev(netdev);
3767 err_alloc_etherdev:
3768 	pci_release_regions(pdev);
3769 err_pci_reg:
3770 err_dma:
3771 	pci_disable_device(pdev);
3772 	return err;
3773 }
3774 
3775 #ifdef CONFIG_PM
3776 /**
3777  * iavf_suspend - Power management suspend routine
3778  * @pdev: PCI device information struct
3779  * @state: unused
3780  *
3781  * Called when the system (VM) is entering sleep/suspend.
3782  **/
3783 static int iavf_suspend(struct pci_dev *pdev, pm_message_t state)
3784 {
3785 	struct net_device *netdev = pci_get_drvdata(pdev);
3786 	struct iavf_adapter *adapter = netdev_priv(netdev);
3787 	int retval = 0;
3788 
3789 	netif_device_detach(netdev);
3790 
3791 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3792 				&adapter->crit_section))
3793 		usleep_range(500, 1000);
3794 
3795 	if (netif_running(netdev)) {
3796 		rtnl_lock();
3797 		iavf_down(adapter);
3798 		rtnl_unlock();
3799 	}
3800 	iavf_free_misc_irq(adapter);
3801 	iavf_reset_interrupt_capability(adapter);
3802 
3803 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3804 
3805 	retval = pci_save_state(pdev);
3806 	if (retval)
3807 		return retval;
3808 
3809 	pci_disable_device(pdev);
3810 
3811 	return 0;
3812 }
3813 
3814 /**
3815  * iavf_resume - Power management resume routine
3816  * @pdev: PCI device information struct
3817  *
3818  * Called when the system (VM) is resumed from sleep/suspend.
3819  **/
3820 static int iavf_resume(struct pci_dev *pdev)
3821 {
3822 	struct iavf_adapter *adapter = pci_get_drvdata(pdev);
3823 	struct net_device *netdev = adapter->netdev;
3824 	u32 err;
3825 
3826 	pci_set_power_state(pdev, PCI_D0);
3827 	pci_restore_state(pdev);
3828 	/* pci_restore_state clears dev->state_saved so call
3829 	 * pci_save_state to restore it.
3830 	 */
3831 	pci_save_state(pdev);
3832 
3833 	err = pci_enable_device_mem(pdev);
3834 	if (err) {
3835 		dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3836 		return err;
3837 	}
3838 	pci_set_master(pdev);
3839 
3840 	rtnl_lock();
3841 	err = iavf_set_interrupt_capability(adapter);
3842 	if (err) {
3843 		rtnl_unlock();
3844 		dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3845 		return err;
3846 	}
3847 	err = iavf_request_misc_irq(adapter);
3848 	rtnl_unlock();
3849 	if (err) {
3850 		dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3851 		return err;
3852 	}
3853 
3854 	queue_work(iavf_wq, &adapter->reset_task);
3855 
3856 	netif_device_attach(netdev);
3857 
3858 	return err;
3859 }
3860 
3861 #endif /* CONFIG_PM */
3862 /**
3863  * iavf_remove - Device Removal Routine
3864  * @pdev: PCI device information struct
3865  *
3866  * iavf_remove is called by the PCI subsystem to alert the driver
3867  * that it should release a PCI device.  The could be caused by a
3868  * Hot-Plug event, or because the driver is going to be removed from
3869  * memory.
3870  **/
3871 static void iavf_remove(struct pci_dev *pdev)
3872 {
3873 	struct net_device *netdev = pci_get_drvdata(pdev);
3874 	struct iavf_adapter *adapter = netdev_priv(netdev);
3875 	struct iavf_vlan_filter *vlf, *vlftmp;
3876 	struct iavf_mac_filter *f, *ftmp;
3877 	struct iavf_cloud_filter *cf, *cftmp;
3878 	struct iavf_hw *hw = &adapter->hw;
3879 	int err;
3880 	/* Indicate we are in remove and not to run reset_task */
3881 	set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section);
3882 	cancel_delayed_work_sync(&adapter->init_task);
3883 	cancel_work_sync(&adapter->reset_task);
3884 	cancel_delayed_work_sync(&adapter->client_task);
3885 	if (adapter->netdev_registered) {
3886 		unregister_netdev(netdev);
3887 		adapter->netdev_registered = false;
3888 	}
3889 	if (CLIENT_ALLOWED(adapter)) {
3890 		err = iavf_lan_del_device(adapter);
3891 		if (err)
3892 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3893 				 err);
3894 	}
3895 
3896 	/* Shut down all the garbage mashers on the detention level */
3897 	adapter->state = __IAVF_REMOVE;
3898 	adapter->aq_required = 0;
3899 	adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3900 	iavf_request_reset(adapter);
3901 	msleep(50);
3902 	/* If the FW isn't responding, kick it once, but only once. */
3903 	if (!iavf_asq_done(hw)) {
3904 		iavf_request_reset(adapter);
3905 		msleep(50);
3906 	}
3907 	iavf_free_all_tx_resources(adapter);
3908 	iavf_free_all_rx_resources(adapter);
3909 	iavf_misc_irq_disable(adapter);
3910 	iavf_free_misc_irq(adapter);
3911 	iavf_reset_interrupt_capability(adapter);
3912 	iavf_free_q_vectors(adapter);
3913 
3914 	cancel_delayed_work_sync(&adapter->watchdog_task);
3915 
3916 	cancel_work_sync(&adapter->adminq_task);
3917 
3918 	iavf_free_rss(adapter);
3919 
3920 	if (hw->aq.asq.count)
3921 		iavf_shutdown_adminq(hw);
3922 
3923 	/* destroy the locks only once, here */
3924 	mutex_destroy(&hw->aq.arq_mutex);
3925 	mutex_destroy(&hw->aq.asq_mutex);
3926 
3927 	iounmap(hw->hw_addr);
3928 	pci_release_regions(pdev);
3929 	iavf_free_all_tx_resources(adapter);
3930 	iavf_free_all_rx_resources(adapter);
3931 	iavf_free_queues(adapter);
3932 	kfree(adapter->vf_res);
3933 	spin_lock_bh(&adapter->mac_vlan_list_lock);
3934 	/* If we got removed before an up/down sequence, we've got a filter
3935 	 * hanging out there that we need to get rid of.
3936 	 */
3937 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3938 		list_del(&f->list);
3939 		kfree(f);
3940 	}
3941 	list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3942 				 list) {
3943 		list_del(&vlf->list);
3944 		kfree(vlf);
3945 	}
3946 
3947 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3948 
3949 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3950 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3951 		list_del(&cf->list);
3952 		kfree(cf);
3953 	}
3954 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3955 
3956 	free_netdev(netdev);
3957 
3958 	pci_disable_pcie_error_reporting(pdev);
3959 
3960 	pci_disable_device(pdev);
3961 }
3962 
3963 static struct pci_driver iavf_driver = {
3964 	.name     = iavf_driver_name,
3965 	.id_table = iavf_pci_tbl,
3966 	.probe    = iavf_probe,
3967 	.remove   = iavf_remove,
3968 #ifdef CONFIG_PM
3969 	.suspend  = iavf_suspend,
3970 	.resume   = iavf_resume,
3971 #endif
3972 	.shutdown = iavf_shutdown,
3973 };
3974 
3975 /**
3976  * iavf_init_module - Driver Registration Routine
3977  *
3978  * iavf_init_module is the first routine called when the driver is
3979  * loaded. All it does is register with the PCI subsystem.
3980  **/
3981 static int __init iavf_init_module(void)
3982 {
3983 	int ret;
3984 
3985 	pr_info("iavf: %s - version %s\n", iavf_driver_string,
3986 		iavf_driver_version);
3987 
3988 	pr_info("%s\n", iavf_copyright);
3989 
3990 	iavf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3991 				  iavf_driver_name);
3992 	if (!iavf_wq) {
3993 		pr_err("%s: Failed to create workqueue\n", iavf_driver_name);
3994 		return -ENOMEM;
3995 	}
3996 	ret = pci_register_driver(&iavf_driver);
3997 	return ret;
3998 }
3999 
4000 module_init(iavf_init_module);
4001 
4002 /**
4003  * iavf_exit_module - Driver Exit Cleanup Routine
4004  *
4005  * iavf_exit_module is called just before the driver is removed
4006  * from memory.
4007  **/
4008 static void __exit iavf_exit_module(void)
4009 {
4010 	pci_unregister_driver(&iavf_driver);
4011 	destroy_workqueue(iavf_wq);
4012 }
4013 
4014 module_exit(iavf_exit_module);
4015 
4016 /* iavf_main.c */
4017