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