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