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