• 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 
1620 	if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) &&
1621 	    (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1622 		iavf_set_promiscuous(adapter, 0);
1623 		return 0;
1624 	}
1625 
1626 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
1627 		iavf_enable_channels(adapter);
1628 		return 0;
1629 	}
1630 
1631 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
1632 		iavf_disable_channels(adapter);
1633 		return 0;
1634 	}
1635 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1636 		iavf_add_cloud_filter(adapter);
1637 		return 0;
1638 	}
1639 
1640 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1641 		iavf_del_cloud_filter(adapter);
1642 		return 0;
1643 	}
1644 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1645 		iavf_del_cloud_filter(adapter);
1646 		return 0;
1647 	}
1648 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1649 		iavf_add_cloud_filter(adapter);
1650 		return 0;
1651 	}
1652 	return -EAGAIN;
1653 }
1654 
1655 /**
1656  * iavf_startup - first step of driver startup
1657  * @adapter: board private structure
1658  *
1659  * Function process __IAVF_STARTUP driver state.
1660  * When success the state is changed to __IAVF_INIT_VERSION_CHECK
1661  * when fails it returns -EAGAIN
1662  **/
iavf_startup(struct iavf_adapter * adapter)1663 static int iavf_startup(struct iavf_adapter *adapter)
1664 {
1665 	struct pci_dev *pdev = adapter->pdev;
1666 	struct iavf_hw *hw = &adapter->hw;
1667 	int err;
1668 
1669 	WARN_ON(adapter->state != __IAVF_STARTUP);
1670 
1671 	/* driver loaded, probe complete */
1672 	adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1673 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1674 	err = iavf_set_mac_type(hw);
1675 	if (err) {
1676 		dev_err(&pdev->dev, "Failed to set MAC type (%d)\n", err);
1677 		goto err;
1678 	}
1679 
1680 	err = iavf_check_reset_complete(hw);
1681 	if (err) {
1682 		dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
1683 			 err);
1684 		goto err;
1685 	}
1686 	hw->aq.num_arq_entries = IAVF_AQ_LEN;
1687 	hw->aq.num_asq_entries = IAVF_AQ_LEN;
1688 	hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
1689 	hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
1690 
1691 	err = iavf_init_adminq(hw);
1692 	if (err) {
1693 		dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n", err);
1694 		goto err;
1695 	}
1696 	err = iavf_send_api_ver(adapter);
1697 	if (err) {
1698 		dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
1699 		iavf_shutdown_adminq(hw);
1700 		goto err;
1701 	}
1702 	adapter->state = __IAVF_INIT_VERSION_CHECK;
1703 err:
1704 	return err;
1705 }
1706 
1707 /**
1708  * iavf_init_version_check - second step of driver startup
1709  * @adapter: board private structure
1710  *
1711  * Function process __IAVF_INIT_VERSION_CHECK driver state.
1712  * When success the state is changed to __IAVF_INIT_GET_RESOURCES
1713  * when fails it returns -EAGAIN
1714  **/
iavf_init_version_check(struct iavf_adapter * adapter)1715 static int iavf_init_version_check(struct iavf_adapter *adapter)
1716 {
1717 	struct pci_dev *pdev = adapter->pdev;
1718 	struct iavf_hw *hw = &adapter->hw;
1719 	int err = -EAGAIN;
1720 
1721 	WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
1722 
1723 	if (!iavf_asq_done(hw)) {
1724 		dev_err(&pdev->dev, "Admin queue command never completed\n");
1725 		iavf_shutdown_adminq(hw);
1726 		adapter->state = __IAVF_STARTUP;
1727 		goto err;
1728 	}
1729 
1730 	/* aq msg sent, awaiting reply */
1731 	err = iavf_verify_api_ver(adapter);
1732 	if (err) {
1733 		if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK)
1734 			err = iavf_send_api_ver(adapter);
1735 		else
1736 			dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
1737 				adapter->pf_version.major,
1738 				adapter->pf_version.minor,
1739 				VIRTCHNL_VERSION_MAJOR,
1740 				VIRTCHNL_VERSION_MINOR);
1741 		goto err;
1742 	}
1743 	err = iavf_send_vf_config_msg(adapter);
1744 	if (err) {
1745 		dev_err(&pdev->dev, "Unable to send config request (%d)\n",
1746 			err);
1747 		goto err;
1748 	}
1749 	adapter->state = __IAVF_INIT_GET_RESOURCES;
1750 
1751 err:
1752 	return err;
1753 }
1754 
1755 /**
1756  * iavf_init_get_resources - third step of driver startup
1757  * @adapter: board private structure
1758  *
1759  * Function process __IAVF_INIT_GET_RESOURCES driver state and
1760  * finishes driver initialization procedure.
1761  * When success the state is changed to __IAVF_DOWN
1762  * when fails it returns -EAGAIN
1763  **/
iavf_init_get_resources(struct iavf_adapter * adapter)1764 static int iavf_init_get_resources(struct iavf_adapter *adapter)
1765 {
1766 	struct net_device *netdev = adapter->netdev;
1767 	struct pci_dev *pdev = adapter->pdev;
1768 	struct iavf_hw *hw = &adapter->hw;
1769 	int err;
1770 
1771 	WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
1772 	/* aq msg sent, awaiting reply */
1773 	if (!adapter->vf_res) {
1774 		adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
1775 					  GFP_KERNEL);
1776 		if (!adapter->vf_res) {
1777 			err = -ENOMEM;
1778 			goto err;
1779 		}
1780 	}
1781 	err = iavf_get_vf_config(adapter);
1782 	if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK) {
1783 		err = iavf_send_vf_config_msg(adapter);
1784 		goto err;
1785 	} else if (err == IAVF_ERR_PARAM) {
1786 		/* We only get ERR_PARAM if the device is in a very bad
1787 		 * state or if we've been disabled for previous bad
1788 		 * behavior. Either way, we're done now.
1789 		 */
1790 		iavf_shutdown_adminq(hw);
1791 		dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
1792 		return 0;
1793 	}
1794 	if (err) {
1795 		dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
1796 		goto err_alloc;
1797 	}
1798 
1799 	err = iavf_process_config(adapter);
1800 	if (err)
1801 		goto err_alloc;
1802 	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1803 
1804 	adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
1805 
1806 	netdev->netdev_ops = &iavf_netdev_ops;
1807 	iavf_set_ethtool_ops(netdev);
1808 	netdev->watchdog_timeo = 5 * HZ;
1809 
1810 	/* MTU range: 68 - 9710 */
1811 	netdev->min_mtu = ETH_MIN_MTU;
1812 	netdev->max_mtu = IAVF_MAX_RXBUFFER - IAVF_PACKET_HDR_PAD;
1813 
1814 	if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
1815 		dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1816 			 adapter->hw.mac.addr);
1817 		eth_hw_addr_random(netdev);
1818 		ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1819 	} else {
1820 		ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
1821 		ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
1822 	}
1823 
1824 	adapter->tx_desc_count = IAVF_DEFAULT_TXD;
1825 	adapter->rx_desc_count = IAVF_DEFAULT_RXD;
1826 	err = iavf_init_interrupt_scheme(adapter);
1827 	if (err)
1828 		goto err_sw_init;
1829 	iavf_map_rings_to_vectors(adapter);
1830 	if (adapter->vf_res->vf_cap_flags &
1831 		VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1832 		adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
1833 
1834 	err = iavf_request_misc_irq(adapter);
1835 	if (err)
1836 		goto err_sw_init;
1837 
1838 	netif_carrier_off(netdev);
1839 	adapter->link_up = false;
1840 
1841 	/* set the semaphore to prevent any callbacks after device registration
1842 	 * up to time when state of driver will be set to __IAVF_DOWN
1843 	 */
1844 	rtnl_lock();
1845 	if (!adapter->netdev_registered) {
1846 		err = register_netdevice(netdev);
1847 		if (err) {
1848 			rtnl_unlock();
1849 			goto err_register;
1850 		}
1851 	}
1852 
1853 	adapter->netdev_registered = true;
1854 
1855 	netif_tx_stop_all_queues(netdev);
1856 	if (CLIENT_ALLOWED(adapter)) {
1857 		err = iavf_lan_add_device(adapter);
1858 		if (err)
1859 			dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
1860 				 err);
1861 	}
1862 	dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
1863 	if (netdev->features & NETIF_F_GRO)
1864 		dev_info(&pdev->dev, "GRO is enabled\n");
1865 
1866 	adapter->state = __IAVF_DOWN;
1867 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1868 	rtnl_unlock();
1869 
1870 	iavf_misc_irq_enable(adapter);
1871 	wake_up(&adapter->down_waitqueue);
1872 
1873 	adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
1874 	adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
1875 	if (!adapter->rss_key || !adapter->rss_lut) {
1876 		err = -ENOMEM;
1877 		goto err_mem;
1878 	}
1879 	if (RSS_AQ(adapter))
1880 		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
1881 	else
1882 		iavf_init_rss(adapter);
1883 
1884 	return err;
1885 err_mem:
1886 	iavf_free_rss(adapter);
1887 err_register:
1888 	iavf_free_misc_irq(adapter);
1889 err_sw_init:
1890 	iavf_reset_interrupt_capability(adapter);
1891 err_alloc:
1892 	kfree(adapter->vf_res);
1893 	adapter->vf_res = NULL;
1894 err:
1895 	return err;
1896 }
1897 
1898 /**
1899  * iavf_watchdog_task - Periodic call-back task
1900  * @work: pointer to work_struct
1901  **/
iavf_watchdog_task(struct work_struct * work)1902 static void iavf_watchdog_task(struct work_struct *work)
1903 {
1904 	struct iavf_adapter *adapter = container_of(work,
1905 						    struct iavf_adapter,
1906 						    watchdog_task.work);
1907 	struct iavf_hw *hw = &adapter->hw;
1908 	u32 reg_val;
1909 
1910 	if (test_and_set_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section))
1911 		goto restart_watchdog;
1912 
1913 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
1914 		adapter->state = __IAVF_COMM_FAILED;
1915 
1916 	switch (adapter->state) {
1917 	case __IAVF_COMM_FAILED:
1918 		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
1919 			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1920 		if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
1921 		    reg_val == VIRTCHNL_VFR_COMPLETED) {
1922 			/* A chance for redemption! */
1923 			dev_err(&adapter->pdev->dev,
1924 				"Hardware came out of reset. Attempting reinit.\n");
1925 			adapter->state = __IAVF_STARTUP;
1926 			adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1927 			queue_delayed_work(iavf_wq, &adapter->init_task, 10);
1928 			clear_bit(__IAVF_IN_CRITICAL_TASK,
1929 				  &adapter->crit_section);
1930 			/* Don't reschedule the watchdog, since we've restarted
1931 			 * the init task. When init_task contacts the PF and
1932 			 * gets everything set up again, it'll restart the
1933 			 * watchdog for us. Down, boy. Sit. Stay. Woof.
1934 			 */
1935 			return;
1936 		}
1937 		adapter->aq_required = 0;
1938 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1939 		clear_bit(__IAVF_IN_CRITICAL_TASK,
1940 			  &adapter->crit_section);
1941 		queue_delayed_work(iavf_wq,
1942 				   &adapter->watchdog_task,
1943 				   msecs_to_jiffies(10));
1944 		goto watchdog_done;
1945 	case __IAVF_RESETTING:
1946 		clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1947 		queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
1948 		return;
1949 	case __IAVF_DOWN:
1950 	case __IAVF_DOWN_PENDING:
1951 	case __IAVF_TESTING:
1952 	case __IAVF_RUNNING:
1953 		if (adapter->current_op) {
1954 			if (!iavf_asq_done(hw)) {
1955 				dev_dbg(&adapter->pdev->dev,
1956 					"Admin queue timeout\n");
1957 				iavf_send_api_ver(adapter);
1958 			}
1959 		} else {
1960 			/* An error will be returned if no commands were
1961 			 * processed; use this opportunity to update stats
1962 			 */
1963 			if (iavf_process_aq_command(adapter) &&
1964 			    adapter->state == __IAVF_RUNNING)
1965 				iavf_request_stats(adapter);
1966 		}
1967 		break;
1968 	case __IAVF_REMOVE:
1969 		clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1970 		return;
1971 	default:
1972 		goto restart_watchdog;
1973 	}
1974 
1975 		/* check for hw reset */
1976 	reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
1977 	if (!reg_val) {
1978 		adapter->flags |= IAVF_FLAG_RESET_PENDING;
1979 		adapter->aq_required = 0;
1980 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1981 		dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1982 		queue_work(iavf_wq, &adapter->reset_task);
1983 		goto watchdog_done;
1984 	}
1985 
1986 	schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1987 watchdog_done:
1988 	if (adapter->state == __IAVF_RUNNING ||
1989 	    adapter->state == __IAVF_COMM_FAILED)
1990 		iavf_detect_recover_hung(&adapter->vsi);
1991 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1992 restart_watchdog:
1993 	if (adapter->aq_required)
1994 		queue_delayed_work(iavf_wq, &adapter->watchdog_task,
1995 				   msecs_to_jiffies(20));
1996 	else
1997 		queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
1998 	queue_work(iavf_wq, &adapter->adminq_task);
1999 }
2000 
iavf_disable_vf(struct iavf_adapter * adapter)2001 static void iavf_disable_vf(struct iavf_adapter *adapter)
2002 {
2003 	struct iavf_mac_filter *f, *ftmp;
2004 	struct iavf_vlan_filter *fv, *fvtmp;
2005 	struct iavf_cloud_filter *cf, *cftmp;
2006 
2007 	adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2008 
2009 	/* We don't use netif_running() because it may be true prior to
2010 	 * ndo_open() returning, so we can't assume it means all our open
2011 	 * tasks have finished, since we're not holding the rtnl_lock here.
2012 	 */
2013 	if (adapter->state == __IAVF_RUNNING) {
2014 		set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2015 		netif_carrier_off(adapter->netdev);
2016 		netif_tx_disable(adapter->netdev);
2017 		adapter->link_up = false;
2018 		iavf_napi_disable_all(adapter);
2019 		iavf_irq_disable(adapter);
2020 		iavf_free_traffic_irqs(adapter);
2021 		iavf_free_all_tx_resources(adapter);
2022 		iavf_free_all_rx_resources(adapter);
2023 	}
2024 
2025 	spin_lock_bh(&adapter->mac_vlan_list_lock);
2026 
2027 	/* Delete all of the filters */
2028 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2029 		list_del(&f->list);
2030 		kfree(f);
2031 	}
2032 
2033 	list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
2034 		list_del(&fv->list);
2035 		kfree(fv);
2036 	}
2037 
2038 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
2039 
2040 	spin_lock_bh(&adapter->cloud_filter_list_lock);
2041 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
2042 		list_del(&cf->list);
2043 		kfree(cf);
2044 		adapter->num_cloud_filters--;
2045 	}
2046 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2047 
2048 	iavf_free_misc_irq(adapter);
2049 	iavf_reset_interrupt_capability(adapter);
2050 	iavf_free_queues(adapter);
2051 	iavf_free_q_vectors(adapter);
2052 	memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
2053 	iavf_shutdown_adminq(&adapter->hw);
2054 	adapter->netdev->flags &= ~IFF_UP;
2055 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2056 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2057 	adapter->state = __IAVF_DOWN;
2058 	wake_up(&adapter->down_waitqueue);
2059 	dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
2060 }
2061 
2062 /**
2063  * iavf_reset_task - Call-back task to handle hardware reset
2064  * @work: pointer to work_struct
2065  *
2066  * During reset we need to shut down and reinitialize the admin queue
2067  * before we can use it to communicate with the PF again. We also clear
2068  * and reinit the rings because that context is lost as well.
2069  **/
iavf_reset_task(struct work_struct * work)2070 static void iavf_reset_task(struct work_struct *work)
2071 {
2072 	struct iavf_adapter *adapter = container_of(work,
2073 						      struct iavf_adapter,
2074 						      reset_task);
2075 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
2076 	struct net_device *netdev = adapter->netdev;
2077 	struct iavf_hw *hw = &adapter->hw;
2078 	struct iavf_mac_filter *f, *ftmp;
2079 	struct iavf_vlan_filter *vlf;
2080 	struct iavf_cloud_filter *cf;
2081 	u32 reg_val;
2082 	int i = 0, err;
2083 	bool running;
2084 
2085 	/* When device is being removed it doesn't make sense to run the reset
2086 	 * task, just return in such a case.
2087 	 */
2088 	if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
2089 		return;
2090 
2091 	if (iavf_lock_timeout(adapter, __IAVF_IN_CRITICAL_TASK, 200)) {
2092 		schedule_work(&adapter->reset_task);
2093 		return;
2094 	}
2095 	while (test_and_set_bit(__IAVF_IN_CLIENT_TASK,
2096 				&adapter->crit_section))
2097 		usleep_range(500, 1000);
2098 	if (CLIENT_ENABLED(adapter)) {
2099 		adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
2100 				    IAVF_FLAG_CLIENT_NEEDS_CLOSE |
2101 				    IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
2102 				    IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
2103 		cancel_delayed_work_sync(&adapter->client_task);
2104 		iavf_notify_client_close(&adapter->vsi, true);
2105 	}
2106 	iavf_misc_irq_disable(adapter);
2107 	if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
2108 		adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
2109 		/* Restart the AQ here. If we have been reset but didn't
2110 		 * detect it, or if the PF had to reinit, our AQ will be hosed.
2111 		 */
2112 		iavf_shutdown_adminq(hw);
2113 		iavf_init_adminq(hw);
2114 		iavf_request_reset(adapter);
2115 	}
2116 	adapter->flags |= IAVF_FLAG_RESET_PENDING;
2117 
2118 	/* poll until we see the reset actually happen */
2119 	for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
2120 		reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
2121 			  IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2122 		if (!reg_val)
2123 			break;
2124 		usleep_range(5000, 10000);
2125 	}
2126 	if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
2127 		dev_info(&adapter->pdev->dev, "Never saw reset\n");
2128 		goto continue_reset; /* act like the reset happened */
2129 	}
2130 
2131 	/* wait until the reset is complete and the PF is responding to us */
2132 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
2133 		/* sleep first to make sure a minimum wait time is met */
2134 		msleep(IAVF_RESET_WAIT_MS);
2135 
2136 		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2137 			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2138 		if (reg_val == VIRTCHNL_VFR_VFACTIVE)
2139 			break;
2140 	}
2141 
2142 	pci_set_master(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) /* indicates 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 > IAVF_MAX_REQ_QUEUES)
2602 		return -EINVAL;
2603 
2604 	ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
2605 	return ret;
2606 }
2607 
2608 /**
2609  * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
2610  * @adapter: board private structure
2611  **/
iavf_del_all_cloud_filters(struct iavf_adapter * adapter)2612 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
2613 {
2614 	struct iavf_cloud_filter *cf, *cftmp;
2615 
2616 	spin_lock_bh(&adapter->cloud_filter_list_lock);
2617 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2618 				 list) {
2619 		list_del(&cf->list);
2620 		kfree(cf);
2621 		adapter->num_cloud_filters--;
2622 	}
2623 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2624 }
2625 
2626 /**
2627  * __iavf_setup_tc - configure multiple traffic classes
2628  * @netdev: network interface device structure
2629  * @type_data: tc offload data
2630  *
2631  * This function processes the config information provided by the
2632  * user to configure traffic classes/queue channels and packages the
2633  * information to request the PF to setup traffic classes.
2634  *
2635  * Returns 0 on success.
2636  **/
__iavf_setup_tc(struct net_device * netdev,void * type_data)2637 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
2638 {
2639 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2640 	struct iavf_adapter *adapter = netdev_priv(netdev);
2641 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
2642 	u8 num_tc = 0, total_qps = 0;
2643 	int ret = 0, netdev_tc = 0;
2644 	u64 max_tx_rate;
2645 	u16 mode;
2646 	int i;
2647 
2648 	num_tc = mqprio_qopt->qopt.num_tc;
2649 	mode = mqprio_qopt->mode;
2650 
2651 	/* delete queue_channel */
2652 	if (!mqprio_qopt->qopt.hw) {
2653 		if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
2654 			/* reset the tc configuration */
2655 			netdev_reset_tc(netdev);
2656 			adapter->num_tc = 0;
2657 			netif_tx_stop_all_queues(netdev);
2658 			netif_tx_disable(netdev);
2659 			iavf_del_all_cloud_filters(adapter);
2660 			adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
2661 			goto exit;
2662 		} else {
2663 			return -EINVAL;
2664 		}
2665 	}
2666 
2667 	/* add queue channel */
2668 	if (mode == TC_MQPRIO_MODE_CHANNEL) {
2669 		if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2670 			dev_err(&adapter->pdev->dev, "ADq not supported\n");
2671 			return -EOPNOTSUPP;
2672 		}
2673 		if (adapter->ch_config.state != __IAVF_TC_INVALID) {
2674 			dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2675 			return -EINVAL;
2676 		}
2677 
2678 		ret = iavf_validate_ch_config(adapter, mqprio_qopt);
2679 		if (ret)
2680 			return ret;
2681 		/* Return if same TC config is requested */
2682 		if (adapter->num_tc == num_tc)
2683 			return 0;
2684 		adapter->num_tc = num_tc;
2685 
2686 		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2687 			if (i < num_tc) {
2688 				adapter->ch_config.ch_info[i].count =
2689 					mqprio_qopt->qopt.count[i];
2690 				adapter->ch_config.ch_info[i].offset =
2691 					mqprio_qopt->qopt.offset[i];
2692 				total_qps += mqprio_qopt->qopt.count[i];
2693 				max_tx_rate = mqprio_qopt->max_rate[i];
2694 				/* convert to Mbps */
2695 				max_tx_rate = div_u64(max_tx_rate,
2696 						      IAVF_MBPS_DIVISOR);
2697 				adapter->ch_config.ch_info[i].max_tx_rate =
2698 					max_tx_rate;
2699 			} else {
2700 				adapter->ch_config.ch_info[i].count = 1;
2701 				adapter->ch_config.ch_info[i].offset = 0;
2702 			}
2703 		}
2704 		adapter->ch_config.total_qps = total_qps;
2705 		netif_tx_stop_all_queues(netdev);
2706 		netif_tx_disable(netdev);
2707 		adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
2708 		netdev_reset_tc(netdev);
2709 		/* Report the tc mapping up the stack */
2710 		netdev_set_num_tc(adapter->netdev, num_tc);
2711 		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2712 			u16 qcount = mqprio_qopt->qopt.count[i];
2713 			u16 qoffset = mqprio_qopt->qopt.offset[i];
2714 
2715 			if (i < num_tc)
2716 				netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2717 						    qoffset);
2718 		}
2719 	}
2720 exit:
2721 	return ret;
2722 }
2723 
2724 /**
2725  * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
2726  * @adapter: board private structure
2727  * @f: pointer to struct flow_cls_offload
2728  * @filter: pointer to cloud filter structure
2729  */
iavf_parse_cls_flower(struct iavf_adapter * adapter,struct flow_cls_offload * f,struct iavf_cloud_filter * filter)2730 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
2731 				 struct flow_cls_offload *f,
2732 				 struct iavf_cloud_filter *filter)
2733 {
2734 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
2735 	struct flow_dissector *dissector = rule->match.dissector;
2736 	u16 n_proto_mask = 0;
2737 	u16 n_proto_key = 0;
2738 	u8 field_flags = 0;
2739 	u16 addr_type = 0;
2740 	u16 n_proto = 0;
2741 	int i = 0;
2742 	struct virtchnl_filter *vf = &filter->f;
2743 
2744 	if (dissector->used_keys &
2745 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2746 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
2747 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2748 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
2749 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2750 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2751 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
2752 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2753 		dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2754 			dissector->used_keys);
2755 		return -EOPNOTSUPP;
2756 	}
2757 
2758 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2759 		struct flow_match_enc_keyid match;
2760 
2761 		flow_rule_match_enc_keyid(rule, &match);
2762 		if (match.mask->keyid != 0)
2763 			field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
2764 	}
2765 
2766 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
2767 		struct flow_match_basic match;
2768 
2769 		flow_rule_match_basic(rule, &match);
2770 		n_proto_key = ntohs(match.key->n_proto);
2771 		n_proto_mask = ntohs(match.mask->n_proto);
2772 
2773 		if (n_proto_key == ETH_P_ALL) {
2774 			n_proto_key = 0;
2775 			n_proto_mask = 0;
2776 		}
2777 		n_proto = n_proto_key & n_proto_mask;
2778 		if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2779 			return -EINVAL;
2780 		if (n_proto == ETH_P_IPV6) {
2781 			/* specify flow type as TCP IPv6 */
2782 			vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2783 		}
2784 
2785 		if (match.key->ip_proto != IPPROTO_TCP) {
2786 			dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2787 			return -EINVAL;
2788 		}
2789 	}
2790 
2791 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2792 		struct flow_match_eth_addrs match;
2793 
2794 		flow_rule_match_eth_addrs(rule, &match);
2795 
2796 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
2797 		if (!is_zero_ether_addr(match.mask->dst)) {
2798 			if (is_broadcast_ether_addr(match.mask->dst)) {
2799 				field_flags |= IAVF_CLOUD_FIELD_OMAC;
2800 			} else {
2801 				dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2802 					match.mask->dst);
2803 				return IAVF_ERR_CONFIG;
2804 			}
2805 		}
2806 
2807 		if (!is_zero_ether_addr(match.mask->src)) {
2808 			if (is_broadcast_ether_addr(match.mask->src)) {
2809 				field_flags |= IAVF_CLOUD_FIELD_IMAC;
2810 			} else {
2811 				dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2812 					match.mask->src);
2813 				return IAVF_ERR_CONFIG;
2814 			}
2815 		}
2816 
2817 		if (!is_zero_ether_addr(match.key->dst))
2818 			if (is_valid_ether_addr(match.key->dst) ||
2819 			    is_multicast_ether_addr(match.key->dst)) {
2820 				/* set the mask if a valid dst_mac address */
2821 				for (i = 0; i < ETH_ALEN; i++)
2822 					vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2823 				ether_addr_copy(vf->data.tcp_spec.dst_mac,
2824 						match.key->dst);
2825 			}
2826 
2827 		if (!is_zero_ether_addr(match.key->src))
2828 			if (is_valid_ether_addr(match.key->src) ||
2829 			    is_multicast_ether_addr(match.key->src)) {
2830 				/* set the mask if a valid dst_mac address */
2831 				for (i = 0; i < ETH_ALEN; i++)
2832 					vf->mask.tcp_spec.src_mac[i] |= 0xff;
2833 				ether_addr_copy(vf->data.tcp_spec.src_mac,
2834 						match.key->src);
2835 		}
2836 	}
2837 
2838 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
2839 		struct flow_match_vlan match;
2840 
2841 		flow_rule_match_vlan(rule, &match);
2842 		if (match.mask->vlan_id) {
2843 			if (match.mask->vlan_id == VLAN_VID_MASK) {
2844 				field_flags |= IAVF_CLOUD_FIELD_IVLAN;
2845 			} else {
2846 				dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2847 					match.mask->vlan_id);
2848 				return IAVF_ERR_CONFIG;
2849 			}
2850 		}
2851 		vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2852 		vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
2853 	}
2854 
2855 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
2856 		struct flow_match_control match;
2857 
2858 		flow_rule_match_control(rule, &match);
2859 		addr_type = match.key->addr_type;
2860 	}
2861 
2862 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2863 		struct flow_match_ipv4_addrs match;
2864 
2865 		flow_rule_match_ipv4_addrs(rule, &match);
2866 		if (match.mask->dst) {
2867 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
2868 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2869 			} else {
2870 				dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2871 					be32_to_cpu(match.mask->dst));
2872 				return IAVF_ERR_CONFIG;
2873 			}
2874 		}
2875 
2876 		if (match.mask->src) {
2877 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
2878 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2879 			} else {
2880 				dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2881 					be32_to_cpu(match.mask->dst));
2882 				return IAVF_ERR_CONFIG;
2883 			}
2884 		}
2885 
2886 		if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
2887 			dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2888 			return IAVF_ERR_CONFIG;
2889 		}
2890 		if (match.key->dst) {
2891 			vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2892 			vf->data.tcp_spec.dst_ip[0] = match.key->dst;
2893 		}
2894 		if (match.key->src) {
2895 			vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2896 			vf->data.tcp_spec.src_ip[0] = match.key->src;
2897 		}
2898 	}
2899 
2900 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2901 		struct flow_match_ipv6_addrs match;
2902 
2903 		flow_rule_match_ipv6_addrs(rule, &match);
2904 
2905 		/* validate mask, make sure it is not IPV6_ADDR_ANY */
2906 		if (ipv6_addr_any(&match.mask->dst)) {
2907 			dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2908 				IPV6_ADDR_ANY);
2909 			return IAVF_ERR_CONFIG;
2910 		}
2911 
2912 		/* src and dest IPv6 address should not be LOOPBACK
2913 		 * (0:0:0:0:0:0:0:1) which can be represented as ::1
2914 		 */
2915 		if (ipv6_addr_loopback(&match.key->dst) ||
2916 		    ipv6_addr_loopback(&match.key->src)) {
2917 			dev_err(&adapter->pdev->dev,
2918 				"ipv6 addr should not be loopback\n");
2919 			return IAVF_ERR_CONFIG;
2920 		}
2921 		if (!ipv6_addr_any(&match.mask->dst) ||
2922 		    !ipv6_addr_any(&match.mask->src))
2923 			field_flags |= IAVF_CLOUD_FIELD_IIP;
2924 
2925 		for (i = 0; i < 4; i++)
2926 			vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2927 		memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
2928 		       sizeof(vf->data.tcp_spec.dst_ip));
2929 		for (i = 0; i < 4; i++)
2930 			vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2931 		memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
2932 		       sizeof(vf->data.tcp_spec.src_ip));
2933 	}
2934 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
2935 		struct flow_match_ports match;
2936 
2937 		flow_rule_match_ports(rule, &match);
2938 		if (match.mask->src) {
2939 			if (match.mask->src == cpu_to_be16(0xffff)) {
2940 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2941 			} else {
2942 				dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
2943 					be16_to_cpu(match.mask->src));
2944 				return IAVF_ERR_CONFIG;
2945 			}
2946 		}
2947 
2948 		if (match.mask->dst) {
2949 			if (match.mask->dst == cpu_to_be16(0xffff)) {
2950 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2951 			} else {
2952 				dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
2953 					be16_to_cpu(match.mask->dst));
2954 				return IAVF_ERR_CONFIG;
2955 			}
2956 		}
2957 		if (match.key->dst) {
2958 			vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
2959 			vf->data.tcp_spec.dst_port = match.key->dst;
2960 		}
2961 
2962 		if (match.key->src) {
2963 			vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
2964 			vf->data.tcp_spec.src_port = match.key->src;
2965 		}
2966 	}
2967 	vf->field_flags = field_flags;
2968 
2969 	return 0;
2970 }
2971 
2972 /**
2973  * iavf_handle_tclass - Forward to a traffic class on the device
2974  * @adapter: board private structure
2975  * @tc: traffic class index on the device
2976  * @filter: pointer to cloud filter structure
2977  */
iavf_handle_tclass(struct iavf_adapter * adapter,u32 tc,struct iavf_cloud_filter * filter)2978 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
2979 			      struct iavf_cloud_filter *filter)
2980 {
2981 	if (tc == 0)
2982 		return 0;
2983 	if (tc < adapter->num_tc) {
2984 		if (!filter->f.data.tcp_spec.dst_port) {
2985 			dev_err(&adapter->pdev->dev,
2986 				"Specify destination port to redirect to traffic class other than TC0\n");
2987 			return -EINVAL;
2988 		}
2989 	}
2990 	/* redirect to a traffic class on the same device */
2991 	filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
2992 	filter->f.action_meta = tc;
2993 	return 0;
2994 }
2995 
2996 /**
2997  * iavf_configure_clsflower - Add tc flower filters
2998  * @adapter: board private structure
2999  * @cls_flower: Pointer to struct flow_cls_offload
3000  */
iavf_configure_clsflower(struct iavf_adapter * adapter,struct flow_cls_offload * cls_flower)3001 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
3002 				    struct flow_cls_offload *cls_flower)
3003 {
3004 	int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
3005 	struct iavf_cloud_filter *filter = NULL;
3006 	int err = -EINVAL, count = 50;
3007 
3008 	if (tc < 0) {
3009 		dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
3010 		return -EINVAL;
3011 	}
3012 
3013 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
3014 	if (!filter)
3015 		return -ENOMEM;
3016 
3017 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3018 				&adapter->crit_section)) {
3019 		if (--count == 0)
3020 			goto err;
3021 		udelay(1);
3022 	}
3023 
3024 	filter->cookie = cls_flower->cookie;
3025 
3026 	/* set the mask to all zeroes to begin with */
3027 	memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
3028 	/* start out with flow type and eth type IPv4 to begin with */
3029 	filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
3030 	err = iavf_parse_cls_flower(adapter, cls_flower, filter);
3031 	if (err < 0)
3032 		goto err;
3033 
3034 	err = iavf_handle_tclass(adapter, tc, filter);
3035 	if (err < 0)
3036 		goto err;
3037 
3038 	/* add filter to the list */
3039 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3040 	list_add_tail(&filter->list, &adapter->cloud_filter_list);
3041 	adapter->num_cloud_filters++;
3042 	filter->add = true;
3043 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3044 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3045 err:
3046 	if (err)
3047 		kfree(filter);
3048 
3049 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3050 	return err;
3051 }
3052 
3053 /* iavf_find_cf - Find the cloud filter in the list
3054  * @adapter: Board private structure
3055  * @cookie: filter specific cookie
3056  *
3057  * Returns ptr to the filter object or NULL. Must be called while holding the
3058  * cloud_filter_list_lock.
3059  */
iavf_find_cf(struct iavf_adapter * adapter,unsigned long * cookie)3060 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
3061 					      unsigned long *cookie)
3062 {
3063 	struct iavf_cloud_filter *filter = NULL;
3064 
3065 	if (!cookie)
3066 		return NULL;
3067 
3068 	list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
3069 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
3070 			return filter;
3071 	}
3072 	return NULL;
3073 }
3074 
3075 /**
3076  * iavf_delete_clsflower - Remove tc flower filters
3077  * @adapter: board private structure
3078  * @cls_flower: Pointer to struct flow_cls_offload
3079  */
iavf_delete_clsflower(struct iavf_adapter * adapter,struct flow_cls_offload * cls_flower)3080 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
3081 				 struct flow_cls_offload *cls_flower)
3082 {
3083 	struct iavf_cloud_filter *filter = NULL;
3084 	int err = 0;
3085 
3086 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3087 	filter = iavf_find_cf(adapter, &cls_flower->cookie);
3088 	if (filter) {
3089 		filter->del = true;
3090 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
3091 	} else {
3092 		err = -EINVAL;
3093 	}
3094 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3095 
3096 	return err;
3097 }
3098 
3099 /**
3100  * iavf_setup_tc_cls_flower - flower classifier offloads
3101  * @adapter: board private structure
3102  * @cls_flower: pointer to flow_cls_offload struct with flow info
3103  */
iavf_setup_tc_cls_flower(struct iavf_adapter * adapter,struct flow_cls_offload * cls_flower)3104 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
3105 				    struct flow_cls_offload *cls_flower)
3106 {
3107 	switch (cls_flower->command) {
3108 	case FLOW_CLS_REPLACE:
3109 		return iavf_configure_clsflower(adapter, cls_flower);
3110 	case FLOW_CLS_DESTROY:
3111 		return iavf_delete_clsflower(adapter, cls_flower);
3112 	case FLOW_CLS_STATS:
3113 		return -EOPNOTSUPP;
3114 	default:
3115 		return -EOPNOTSUPP;
3116 	}
3117 }
3118 
3119 /**
3120  * iavf_setup_tc_block_cb - block callback for tc
3121  * @type: type of offload
3122  * @type_data: offload data
3123  * @cb_priv:
3124  *
3125  * This function is the block callback for traffic classes
3126  **/
iavf_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)3127 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3128 				  void *cb_priv)
3129 {
3130 	struct iavf_adapter *adapter = cb_priv;
3131 
3132 	if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
3133 		return -EOPNOTSUPP;
3134 
3135 	switch (type) {
3136 	case TC_SETUP_CLSFLOWER:
3137 		return iavf_setup_tc_cls_flower(cb_priv, type_data);
3138 	default:
3139 		return -EOPNOTSUPP;
3140 	}
3141 }
3142 
3143 static LIST_HEAD(iavf_block_cb_list);
3144 
3145 /**
3146  * iavf_setup_tc - configure multiple traffic classes
3147  * @netdev: network interface device structure
3148  * @type: type of offload
3149  * @type_data: tc offload data
3150  *
3151  * This function is the callback to ndo_setup_tc in the
3152  * netdev_ops.
3153  *
3154  * Returns 0 on success
3155  **/
iavf_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)3156 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
3157 			 void *type_data)
3158 {
3159 	struct iavf_adapter *adapter = netdev_priv(netdev);
3160 
3161 	switch (type) {
3162 	case TC_SETUP_QDISC_MQPRIO:
3163 		return __iavf_setup_tc(netdev, type_data);
3164 	case TC_SETUP_BLOCK:
3165 		return flow_block_cb_setup_simple(type_data,
3166 						  &iavf_block_cb_list,
3167 						  iavf_setup_tc_block_cb,
3168 						  adapter, adapter, true);
3169 	default:
3170 		return -EOPNOTSUPP;
3171 	}
3172 }
3173 
3174 /**
3175  * iavf_open - Called when a network interface is made active
3176  * @netdev: network interface device structure
3177  *
3178  * Returns 0 on success, negative value on failure
3179  *
3180  * The open entry point is called when a network interface is made
3181  * active by the system (IFF_UP).  At this point all resources needed
3182  * for transmit and receive operations are allocated, the interrupt
3183  * handler is registered with the OS, the watchdog is started,
3184  * and the stack is notified that the interface is ready.
3185  **/
iavf_open(struct net_device * netdev)3186 static int iavf_open(struct net_device *netdev)
3187 {
3188 	struct iavf_adapter *adapter = netdev_priv(netdev);
3189 	int err;
3190 
3191 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
3192 		dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
3193 		return -EIO;
3194 	}
3195 
3196 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3197 				&adapter->crit_section))
3198 		usleep_range(500, 1000);
3199 
3200 	if (adapter->state != __IAVF_DOWN) {
3201 		err = -EBUSY;
3202 		goto err_unlock;
3203 	}
3204 
3205 	/* allocate transmit descriptors */
3206 	err = iavf_setup_all_tx_resources(adapter);
3207 	if (err)
3208 		goto err_setup_tx;
3209 
3210 	/* allocate receive descriptors */
3211 	err = iavf_setup_all_rx_resources(adapter);
3212 	if (err)
3213 		goto err_setup_rx;
3214 
3215 	/* clear any pending interrupts, may auto mask */
3216 	err = iavf_request_traffic_irqs(adapter, netdev->name);
3217 	if (err)
3218 		goto err_req_irq;
3219 
3220 	spin_lock_bh(&adapter->mac_vlan_list_lock);
3221 
3222 	iavf_add_filter(adapter, adapter->hw.mac.addr);
3223 
3224 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3225 
3226 	iavf_configure(adapter);
3227 
3228 	iavf_up_complete(adapter);
3229 
3230 	iavf_irq_enable(adapter, true);
3231 
3232 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3233 
3234 	return 0;
3235 
3236 err_req_irq:
3237 	iavf_down(adapter);
3238 	iavf_free_traffic_irqs(adapter);
3239 err_setup_rx:
3240 	iavf_free_all_rx_resources(adapter);
3241 err_setup_tx:
3242 	iavf_free_all_tx_resources(adapter);
3243 err_unlock:
3244 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3245 
3246 	return err;
3247 }
3248 
3249 /**
3250  * iavf_close - Disables a network interface
3251  * @netdev: network interface device structure
3252  *
3253  * Returns 0, this is not allowed to fail
3254  *
3255  * The close entry point is called when an interface is de-activated
3256  * by the OS.  The hardware is still under the drivers control, but
3257  * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3258  * are freed, along with all transmit and receive resources.
3259  **/
iavf_close(struct net_device * netdev)3260 static int iavf_close(struct net_device *netdev)
3261 {
3262 	struct iavf_adapter *adapter = netdev_priv(netdev);
3263 	int status;
3264 
3265 	if (adapter->state <= __IAVF_DOWN_PENDING)
3266 		return 0;
3267 
3268 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3269 				&adapter->crit_section))
3270 		usleep_range(500, 1000);
3271 
3272 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3273 	if (CLIENT_ENABLED(adapter))
3274 		adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3275 
3276 	iavf_down(adapter);
3277 	adapter->state = __IAVF_DOWN_PENDING;
3278 	iavf_free_traffic_irqs(adapter);
3279 
3280 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3281 
3282 	/* We explicitly don't free resources here because the hardware is
3283 	 * still active and can DMA into memory. Resources are cleared in
3284 	 * iavf_virtchnl_completion() after we get confirmation from the PF
3285 	 * driver that the rings have been stopped.
3286 	 *
3287 	 * Also, we wait for state to transition to __IAVF_DOWN before
3288 	 * returning. State change occurs in iavf_virtchnl_completion() after
3289 	 * VF resources are released (which occurs after PF driver processes and
3290 	 * responds to admin queue commands).
3291 	 */
3292 
3293 	status = wait_event_timeout(adapter->down_waitqueue,
3294 				    adapter->state == __IAVF_DOWN,
3295 				    msecs_to_jiffies(500));
3296 	if (!status)
3297 		netdev_warn(netdev, "Device resources not yet released\n");
3298 	return 0;
3299 }
3300 
3301 /**
3302  * iavf_change_mtu - Change the Maximum Transfer Unit
3303  * @netdev: network interface device structure
3304  * @new_mtu: new value for maximum frame size
3305  *
3306  * Returns 0 on success, negative on failure
3307  **/
iavf_change_mtu(struct net_device * netdev,int new_mtu)3308 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
3309 {
3310 	struct iavf_adapter *adapter = netdev_priv(netdev);
3311 
3312 	netdev->mtu = new_mtu;
3313 	if (CLIENT_ENABLED(adapter)) {
3314 		iavf_notify_client_l2_params(&adapter->vsi);
3315 		adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3316 	}
3317 	adapter->flags |= IAVF_FLAG_RESET_NEEDED;
3318 	queue_work(iavf_wq, &adapter->reset_task);
3319 
3320 	return 0;
3321 }
3322 
3323 /**
3324  * iavf_set_features - set the netdev feature flags
3325  * @netdev: ptr to the netdev being adjusted
3326  * @features: the feature set that the stack is suggesting
3327  * Note: expects to be called while under rtnl_lock()
3328  **/
iavf_set_features(struct net_device * netdev,netdev_features_t features)3329 static int iavf_set_features(struct net_device *netdev,
3330 			     netdev_features_t features)
3331 {
3332 	struct iavf_adapter *adapter = netdev_priv(netdev);
3333 
3334 	/* Don't allow changing VLAN_RX flag when adapter is not capable
3335 	 * of VLAN offload
3336 	 */
3337 	if (!VLAN_ALLOWED(adapter)) {
3338 		if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX)
3339 			return -EINVAL;
3340 	} else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3341 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
3342 			adapter->aq_required |=
3343 				IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3344 		else
3345 			adapter->aq_required |=
3346 				IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3347 	}
3348 
3349 	return 0;
3350 }
3351 
3352 /**
3353  * iavf_features_check - Validate encapsulated packet conforms to limits
3354  * @skb: skb buff
3355  * @dev: This physical port's netdev
3356  * @features: Offload features that the stack believes apply
3357  **/
iavf_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)3358 static netdev_features_t iavf_features_check(struct sk_buff *skb,
3359 					     struct net_device *dev,
3360 					     netdev_features_t features)
3361 {
3362 	size_t len;
3363 
3364 	/* No point in doing any of this if neither checksum nor GSO are
3365 	 * being requested for this frame.  We can rule out both by just
3366 	 * checking for CHECKSUM_PARTIAL
3367 	 */
3368 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3369 		return features;
3370 
3371 	/* We cannot support GSO if the MSS is going to be less than
3372 	 * 64 bytes.  If it is then we need to drop support for GSO.
3373 	 */
3374 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3375 		features &= ~NETIF_F_GSO_MASK;
3376 
3377 	/* MACLEN can support at most 63 words */
3378 	len = skb_network_header(skb) - skb->data;
3379 	if (len & ~(63 * 2))
3380 		goto out_err;
3381 
3382 	/* IPLEN and EIPLEN can support at most 127 dwords */
3383 	len = skb_transport_header(skb) - skb_network_header(skb);
3384 	if (len & ~(127 * 4))
3385 		goto out_err;
3386 
3387 	if (skb->encapsulation) {
3388 		/* L4TUNLEN can support 127 words */
3389 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
3390 		if (len & ~(127 * 2))
3391 			goto out_err;
3392 
3393 		/* IPLEN can support at most 127 dwords */
3394 		len = skb_inner_transport_header(skb) -
3395 		      skb_inner_network_header(skb);
3396 		if (len & ~(127 * 4))
3397 			goto out_err;
3398 	}
3399 
3400 	/* No need to validate L4LEN as TCP is the only protocol with a
3401 	 * a flexible value and we support all possible values supported
3402 	 * by TCP, which is at most 15 dwords
3403 	 */
3404 
3405 	return features;
3406 out_err:
3407 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3408 }
3409 
3410 /**
3411  * iavf_fix_features - fix up the netdev feature bits
3412  * @netdev: our net device
3413  * @features: desired feature bits
3414  *
3415  * Returns fixed-up features bits
3416  **/
iavf_fix_features(struct net_device * netdev,netdev_features_t features)3417 static netdev_features_t iavf_fix_features(struct net_device *netdev,
3418 					   netdev_features_t features)
3419 {
3420 	struct iavf_adapter *adapter = netdev_priv(netdev);
3421 
3422 	if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3423 		features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3424 			      NETIF_F_HW_VLAN_CTAG_RX |
3425 			      NETIF_F_HW_VLAN_CTAG_FILTER);
3426 
3427 	return features;
3428 }
3429 
3430 static const struct net_device_ops iavf_netdev_ops = {
3431 	.ndo_open		= iavf_open,
3432 	.ndo_stop		= iavf_close,
3433 	.ndo_start_xmit		= iavf_xmit_frame,
3434 	.ndo_set_rx_mode	= iavf_set_rx_mode,
3435 	.ndo_validate_addr	= eth_validate_addr,
3436 	.ndo_set_mac_address	= iavf_set_mac,
3437 	.ndo_change_mtu		= iavf_change_mtu,
3438 	.ndo_tx_timeout		= iavf_tx_timeout,
3439 	.ndo_vlan_rx_add_vid	= iavf_vlan_rx_add_vid,
3440 	.ndo_vlan_rx_kill_vid	= iavf_vlan_rx_kill_vid,
3441 	.ndo_features_check	= iavf_features_check,
3442 	.ndo_fix_features	= iavf_fix_features,
3443 	.ndo_set_features	= iavf_set_features,
3444 	.ndo_setup_tc		= iavf_setup_tc,
3445 };
3446 
3447 /**
3448  * iavf_check_reset_complete - check that VF reset is complete
3449  * @hw: pointer to hw struct
3450  *
3451  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3452  **/
iavf_check_reset_complete(struct iavf_hw * hw)3453 static int iavf_check_reset_complete(struct iavf_hw *hw)
3454 {
3455 	u32 rstat;
3456 	int i;
3457 
3458 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3459 		rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
3460 			     IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3461 		if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3462 		    (rstat == VIRTCHNL_VFR_COMPLETED))
3463 			return 0;
3464 		usleep_range(10, 20);
3465 	}
3466 	return -EBUSY;
3467 }
3468 
3469 /**
3470  * iavf_process_config - Process the config information we got from the PF
3471  * @adapter: board private structure
3472  *
3473  * Verify that we have a valid config struct, and set up our netdev features
3474  * and our VSI struct.
3475  **/
iavf_process_config(struct iavf_adapter * adapter)3476 int iavf_process_config(struct iavf_adapter *adapter)
3477 {
3478 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
3479 	int i, num_req_queues = adapter->num_req_queues;
3480 	struct net_device *netdev = adapter->netdev;
3481 	struct iavf_vsi *vsi = &adapter->vsi;
3482 	netdev_features_t hw_enc_features;
3483 	netdev_features_t hw_features;
3484 
3485 	/* got VF config message back from PF, now we can parse it */
3486 	for (i = 0; i < vfres->num_vsis; i++) {
3487 		if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3488 			adapter->vsi_res = &vfres->vsi_res[i];
3489 	}
3490 	if (!adapter->vsi_res) {
3491 		dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3492 		return -ENODEV;
3493 	}
3494 
3495 	if (num_req_queues &&
3496 	    num_req_queues > adapter->vsi_res->num_queue_pairs) {
3497 		/* Problem.  The PF gave us fewer queues than what we had
3498 		 * negotiated in our request.  Need a reset to see if we can't
3499 		 * get back to a working state.
3500 		 */
3501 		dev_err(&adapter->pdev->dev,
3502 			"Requested %d queues, but PF only gave us %d.\n",
3503 			num_req_queues,
3504 			adapter->vsi_res->num_queue_pairs);
3505 		adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
3506 		adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3507 		iavf_schedule_reset(adapter);
3508 		return -ENODEV;
3509 	}
3510 	adapter->num_req_queues = 0;
3511 
3512 	hw_enc_features = NETIF_F_SG			|
3513 			  NETIF_F_IP_CSUM		|
3514 			  NETIF_F_IPV6_CSUM		|
3515 			  NETIF_F_HIGHDMA		|
3516 			  NETIF_F_SOFT_FEATURES	|
3517 			  NETIF_F_TSO			|
3518 			  NETIF_F_TSO_ECN		|
3519 			  NETIF_F_TSO6			|
3520 			  NETIF_F_SCTP_CRC		|
3521 			  NETIF_F_RXHASH		|
3522 			  NETIF_F_RXCSUM		|
3523 			  0;
3524 
3525 	/* advertise to stack only if offloads for encapsulated packets is
3526 	 * supported
3527 	 */
3528 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3529 		hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL	|
3530 				   NETIF_F_GSO_GRE		|
3531 				   NETIF_F_GSO_GRE_CSUM		|
3532 				   NETIF_F_GSO_IPXIP4		|
3533 				   NETIF_F_GSO_IPXIP6		|
3534 				   NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3535 				   NETIF_F_GSO_PARTIAL		|
3536 				   0;
3537 
3538 		if (!(vfres->vf_cap_flags &
3539 		      VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3540 			netdev->gso_partial_features |=
3541 				NETIF_F_GSO_UDP_TUNNEL_CSUM;
3542 
3543 		netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3544 		netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3545 		netdev->hw_enc_features |= hw_enc_features;
3546 	}
3547 	/* record features VLANs can make use of */
3548 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3549 
3550 	/* Write features and hw_features separately to avoid polluting
3551 	 * with, or dropping, features that are set when we registered.
3552 	 */
3553 	hw_features = hw_enc_features;
3554 
3555 	/* Enable VLAN features if supported */
3556 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3557 		hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3558 				NETIF_F_HW_VLAN_CTAG_RX);
3559 	/* Enable cloud filter if ADQ is supported */
3560 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3561 		hw_features |= NETIF_F_HW_TC;
3562 
3563 	netdev->hw_features |= hw_features;
3564 
3565 	netdev->features |= hw_features;
3566 
3567 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3568 		netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3569 
3570 	netdev->priv_flags |= IFF_UNICAST_FLT;
3571 
3572 	/* Do not turn on offloads when they are requested to be turned off.
3573 	 * TSO needs minimum 576 bytes to work correctly.
3574 	 */
3575 	if (netdev->wanted_features) {
3576 		if (!(netdev->wanted_features & NETIF_F_TSO) ||
3577 		    netdev->mtu < 576)
3578 			netdev->features &= ~NETIF_F_TSO;
3579 		if (!(netdev->wanted_features & NETIF_F_TSO6) ||
3580 		    netdev->mtu < 576)
3581 			netdev->features &= ~NETIF_F_TSO6;
3582 		if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
3583 			netdev->features &= ~NETIF_F_TSO_ECN;
3584 		if (!(netdev->wanted_features & NETIF_F_GRO))
3585 			netdev->features &= ~NETIF_F_GRO;
3586 		if (!(netdev->wanted_features & NETIF_F_GSO))
3587 			netdev->features &= ~NETIF_F_GSO;
3588 	}
3589 
3590 	adapter->vsi.id = adapter->vsi_res->vsi_id;
3591 
3592 	adapter->vsi.back = adapter;
3593 	adapter->vsi.base_vector = 1;
3594 	adapter->vsi.work_limit = IAVF_DEFAULT_IRQ_WORK;
3595 	vsi->netdev = adapter->netdev;
3596 	vsi->qs_handle = adapter->vsi_res->qset_handle;
3597 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3598 		adapter->rss_key_size = vfres->rss_key_size;
3599 		adapter->rss_lut_size = vfres->rss_lut_size;
3600 	} else {
3601 		adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
3602 		adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
3603 	}
3604 
3605 	return 0;
3606 }
3607 
3608 /**
3609  * iavf_init_task - worker thread to perform delayed initialization
3610  * @work: pointer to work_struct containing our data
3611  *
3612  * This task completes the work that was begun in probe. Due to the nature
3613  * of VF-PF communications, we may need to wait tens of milliseconds to get
3614  * responses back from the PF. Rather than busy-wait in probe and bog down the
3615  * whole system, we'll do it in a task so we can sleep.
3616  * This task only runs during driver init. Once we've established
3617  * communications with the PF driver and set up our netdev, the watchdog
3618  * takes over.
3619  **/
iavf_init_task(struct work_struct * work)3620 static void iavf_init_task(struct work_struct *work)
3621 {
3622 	struct iavf_adapter *adapter = container_of(work,
3623 						    struct iavf_adapter,
3624 						    init_task.work);
3625 	struct iavf_hw *hw = &adapter->hw;
3626 
3627 	if (iavf_lock_timeout(adapter, __IAVF_IN_CRITICAL_TASK, 5000)) {
3628 		dev_warn(&adapter->pdev->dev, "failed to set __IAVF_IN_CRITICAL_TASK in %s\n", __FUNCTION__);
3629 		return;
3630 	}
3631 	switch (adapter->state) {
3632 	case __IAVF_STARTUP:
3633 		if (iavf_startup(adapter) < 0)
3634 			goto init_failed;
3635 		break;
3636 	case __IAVF_INIT_VERSION_CHECK:
3637 		if (iavf_init_version_check(adapter) < 0)
3638 			goto init_failed;
3639 		break;
3640 	case __IAVF_INIT_GET_RESOURCES:
3641 		if (iavf_init_get_resources(adapter) < 0)
3642 			goto init_failed;
3643 		goto out;
3644 	default:
3645 		goto init_failed;
3646 	}
3647 
3648 	queue_delayed_work(iavf_wq, &adapter->init_task,
3649 			   msecs_to_jiffies(30));
3650 	goto out;
3651 init_failed:
3652 	if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
3653 		dev_err(&adapter->pdev->dev,
3654 			"Failed to communicate with PF; waiting before retry\n");
3655 		adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
3656 		iavf_shutdown_adminq(hw);
3657 		adapter->state = __IAVF_STARTUP;
3658 		queue_delayed_work(iavf_wq, &adapter->init_task, HZ * 5);
3659 		goto out;
3660 	}
3661 	queue_delayed_work(iavf_wq, &adapter->init_task, HZ);
3662 out:
3663 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3664 }
3665 
3666 /**
3667  * iavf_shutdown - Shutdown the device in preparation for a reboot
3668  * @pdev: pci device structure
3669  **/
iavf_shutdown(struct pci_dev * pdev)3670 static void iavf_shutdown(struct pci_dev *pdev)
3671 {
3672 	struct net_device *netdev = pci_get_drvdata(pdev);
3673 	struct iavf_adapter *adapter = netdev_priv(netdev);
3674 
3675 	netif_device_detach(netdev);
3676 
3677 	if (netif_running(netdev))
3678 		iavf_close(netdev);
3679 
3680 	if (iavf_lock_timeout(adapter, __IAVF_IN_CRITICAL_TASK, 5000))
3681 		dev_warn(&adapter->pdev->dev, "failed to set __IAVF_IN_CRITICAL_TASK in %s\n", __FUNCTION__);
3682 	/* Prevent the watchdog from running. */
3683 	adapter->state = __IAVF_REMOVE;
3684 	adapter->aq_required = 0;
3685 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3686 
3687 #ifdef CONFIG_PM
3688 	pci_save_state(pdev);
3689 
3690 #endif
3691 	pci_disable_device(pdev);
3692 }
3693 
3694 /**
3695  * iavf_probe - Device Initialization Routine
3696  * @pdev: PCI device information struct
3697  * @ent: entry in iavf_pci_tbl
3698  *
3699  * Returns 0 on success, negative on failure
3700  *
3701  * iavf_probe initializes an adapter identified by a pci_dev structure.
3702  * The OS initialization, configuring of the adapter private structure,
3703  * and a hardware reset occur.
3704  **/
iavf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)3705 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3706 {
3707 	struct net_device *netdev;
3708 	struct iavf_adapter *adapter = NULL;
3709 	struct iavf_hw *hw = NULL;
3710 	int err;
3711 
3712 	err = pci_enable_device(pdev);
3713 	if (err)
3714 		return err;
3715 
3716 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3717 	if (err) {
3718 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3719 		if (err) {
3720 			dev_err(&pdev->dev,
3721 				"DMA configuration failed: 0x%x\n", err);
3722 			goto err_dma;
3723 		}
3724 	}
3725 
3726 	err = pci_request_regions(pdev, iavf_driver_name);
3727 	if (err) {
3728 		dev_err(&pdev->dev,
3729 			"pci_request_regions failed 0x%x\n", err);
3730 		goto err_pci_reg;
3731 	}
3732 
3733 	pci_enable_pcie_error_reporting(pdev);
3734 
3735 	pci_set_master(pdev);
3736 
3737 	netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
3738 				   IAVF_MAX_REQ_QUEUES);
3739 	if (!netdev) {
3740 		err = -ENOMEM;
3741 		goto err_alloc_etherdev;
3742 	}
3743 
3744 	SET_NETDEV_DEV(netdev, &pdev->dev);
3745 
3746 	pci_set_drvdata(pdev, netdev);
3747 	adapter = netdev_priv(netdev);
3748 
3749 	adapter->netdev = netdev;
3750 	adapter->pdev = pdev;
3751 
3752 	hw = &adapter->hw;
3753 	hw->back = adapter;
3754 
3755 	adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3756 	adapter->state = __IAVF_STARTUP;
3757 
3758 	/* Call save state here because it relies on the adapter struct. */
3759 	pci_save_state(pdev);
3760 
3761 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3762 			      pci_resource_len(pdev, 0));
3763 	if (!hw->hw_addr) {
3764 		err = -EIO;
3765 		goto err_ioremap;
3766 	}
3767 	hw->vendor_id = pdev->vendor;
3768 	hw->device_id = pdev->device;
3769 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3770 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
3771 	hw->subsystem_device_id = pdev->subsystem_device;
3772 	hw->bus.device = PCI_SLOT(pdev->devfn);
3773 	hw->bus.func = PCI_FUNC(pdev->devfn);
3774 	hw->bus.bus_id = pdev->bus->number;
3775 
3776 	/* set up the locks for the AQ, do this only once in probe
3777 	 * and destroy them only once in remove
3778 	 */
3779 	mutex_init(&hw->aq.asq_mutex);
3780 	mutex_init(&hw->aq.arq_mutex);
3781 
3782 	spin_lock_init(&adapter->mac_vlan_list_lock);
3783 	spin_lock_init(&adapter->cloud_filter_list_lock);
3784 
3785 	INIT_LIST_HEAD(&adapter->mac_filter_list);
3786 	INIT_LIST_HEAD(&adapter->vlan_filter_list);
3787 	INIT_LIST_HEAD(&adapter->cloud_filter_list);
3788 
3789 	INIT_WORK(&adapter->reset_task, iavf_reset_task);
3790 	INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
3791 	INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
3792 	INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
3793 	INIT_DELAYED_WORK(&adapter->init_task, iavf_init_task);
3794 	queue_delayed_work(iavf_wq, &adapter->init_task,
3795 			   msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3796 
3797 	/* Setup the wait queue for indicating transition to down status */
3798 	init_waitqueue_head(&adapter->down_waitqueue);
3799 
3800 	return 0;
3801 
3802 err_ioremap:
3803 	free_netdev(netdev);
3804 err_alloc_etherdev:
3805 	pci_disable_pcie_error_reporting(pdev);
3806 	pci_release_regions(pdev);
3807 err_pci_reg:
3808 err_dma:
3809 	pci_disable_device(pdev);
3810 	return err;
3811 }
3812 
3813 /**
3814  * iavf_suspend - Power management suspend routine
3815  * @dev_d: device info pointer
3816  *
3817  * Called when the system (VM) is entering sleep/suspend.
3818  **/
iavf_suspend(struct device * dev_d)3819 static int __maybe_unused iavf_suspend(struct device *dev_d)
3820 {
3821 	struct net_device *netdev = dev_get_drvdata(dev_d);
3822 	struct iavf_adapter *adapter = netdev_priv(netdev);
3823 
3824 	netif_device_detach(netdev);
3825 
3826 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3827 				&adapter->crit_section))
3828 		usleep_range(500, 1000);
3829 
3830 	if (netif_running(netdev)) {
3831 		rtnl_lock();
3832 		iavf_down(adapter);
3833 		rtnl_unlock();
3834 	}
3835 	iavf_free_misc_irq(adapter);
3836 	iavf_reset_interrupt_capability(adapter);
3837 
3838 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3839 
3840 	return 0;
3841 }
3842 
3843 /**
3844  * iavf_resume - Power management resume routine
3845  * @dev_d: device info pointer
3846  *
3847  * Called when the system (VM) is resumed from sleep/suspend.
3848  **/
iavf_resume(struct device * dev_d)3849 static int __maybe_unused iavf_resume(struct device *dev_d)
3850 {
3851 	struct pci_dev *pdev = to_pci_dev(dev_d);
3852 	struct net_device *netdev = pci_get_drvdata(pdev);
3853 	struct iavf_adapter *adapter = netdev_priv(netdev);
3854 	u32 err;
3855 
3856 	pci_set_master(pdev);
3857 
3858 	rtnl_lock();
3859 	err = iavf_set_interrupt_capability(adapter);
3860 	if (err) {
3861 		rtnl_unlock();
3862 		dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3863 		return err;
3864 	}
3865 	err = iavf_request_misc_irq(adapter);
3866 	rtnl_unlock();
3867 	if (err) {
3868 		dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3869 		return err;
3870 	}
3871 
3872 	queue_work(iavf_wq, &adapter->reset_task);
3873 
3874 	netif_device_attach(netdev);
3875 
3876 	return err;
3877 }
3878 
3879 /**
3880  * iavf_remove - Device Removal Routine
3881  * @pdev: PCI device information struct
3882  *
3883  * iavf_remove is called by the PCI subsystem to alert the driver
3884  * that it should release a PCI device.  The could be caused by a
3885  * Hot-Plug event, or because the driver is going to be removed from
3886  * memory.
3887  **/
iavf_remove(struct pci_dev * pdev)3888 static void iavf_remove(struct pci_dev *pdev)
3889 {
3890 	struct net_device *netdev = pci_get_drvdata(pdev);
3891 	struct iavf_adapter *adapter = netdev_priv(netdev);
3892 	struct iavf_vlan_filter *vlf, *vlftmp;
3893 	struct iavf_mac_filter *f, *ftmp;
3894 	struct iavf_cloud_filter *cf, *cftmp;
3895 	struct iavf_hw *hw = &adapter->hw;
3896 	int err;
3897 	/* Indicate we are in remove and not to run reset_task */
3898 	set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section);
3899 	cancel_delayed_work_sync(&adapter->init_task);
3900 	cancel_work_sync(&adapter->reset_task);
3901 	cancel_delayed_work_sync(&adapter->client_task);
3902 	if (adapter->netdev_registered) {
3903 		unregister_netdev(netdev);
3904 		adapter->netdev_registered = false;
3905 	}
3906 	if (CLIENT_ALLOWED(adapter)) {
3907 		err = iavf_lan_del_device(adapter);
3908 		if (err)
3909 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3910 				 err);
3911 	}
3912 
3913 	iavf_request_reset(adapter);
3914 	msleep(50);
3915 	/* If the FW isn't responding, kick it once, but only once. */
3916 	if (!iavf_asq_done(hw)) {
3917 		iavf_request_reset(adapter);
3918 		msleep(50);
3919 	}
3920 	if (iavf_lock_timeout(adapter, __IAVF_IN_CRITICAL_TASK, 5000))
3921 		dev_warn(&adapter->pdev->dev, "failed to set __IAVF_IN_CRITICAL_TASK in %s\n", __FUNCTION__);
3922 
3923 	/* Shut down all the garbage mashers on the detention level */
3924 	adapter->state = __IAVF_REMOVE;
3925 	adapter->aq_required = 0;
3926 	adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3927 	iavf_free_all_tx_resources(adapter);
3928 	iavf_free_all_rx_resources(adapter);
3929 	iavf_misc_irq_disable(adapter);
3930 	iavf_free_misc_irq(adapter);
3931 	iavf_reset_interrupt_capability(adapter);
3932 	iavf_free_q_vectors(adapter);
3933 
3934 	cancel_delayed_work_sync(&adapter->watchdog_task);
3935 
3936 	cancel_work_sync(&adapter->adminq_task);
3937 
3938 	iavf_free_rss(adapter);
3939 
3940 	if (hw->aq.asq.count)
3941 		iavf_shutdown_adminq(hw);
3942 
3943 	/* destroy the locks only once, here */
3944 	mutex_destroy(&hw->aq.arq_mutex);
3945 	mutex_destroy(&hw->aq.asq_mutex);
3946 
3947 	iounmap(hw->hw_addr);
3948 	pci_release_regions(pdev);
3949 	iavf_free_queues(adapter);
3950 	kfree(adapter->vf_res);
3951 	spin_lock_bh(&adapter->mac_vlan_list_lock);
3952 	/* If we got removed before an up/down sequence, we've got a filter
3953 	 * hanging out there that we need to get rid of.
3954 	 */
3955 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3956 		list_del(&f->list);
3957 		kfree(f);
3958 	}
3959 	list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3960 				 list) {
3961 		list_del(&vlf->list);
3962 		kfree(vlf);
3963 	}
3964 
3965 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3966 
3967 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3968 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3969 		list_del(&cf->list);
3970 		kfree(cf);
3971 	}
3972 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3973 
3974 	free_netdev(netdev);
3975 
3976 	pci_disable_pcie_error_reporting(pdev);
3977 
3978 	pci_disable_device(pdev);
3979 }
3980 
3981 static SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
3982 
3983 static struct pci_driver iavf_driver = {
3984 	.name      = iavf_driver_name,
3985 	.id_table  = iavf_pci_tbl,
3986 	.probe     = iavf_probe,
3987 	.remove    = iavf_remove,
3988 	.driver.pm = &iavf_pm_ops,
3989 	.shutdown  = iavf_shutdown,
3990 };
3991 
3992 /**
3993  * iavf_init_module - Driver Registration Routine
3994  *
3995  * iavf_init_module is the first routine called when the driver is
3996  * loaded. All it does is register with the PCI subsystem.
3997  **/
iavf_init_module(void)3998 static int __init iavf_init_module(void)
3999 {
4000 	int ret;
4001 
4002 	pr_info("iavf: %s\n", iavf_driver_string);
4003 
4004 	pr_info("%s\n", iavf_copyright);
4005 
4006 	iavf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
4007 				  iavf_driver_name);
4008 	if (!iavf_wq) {
4009 		pr_err("%s: Failed to create workqueue\n", iavf_driver_name);
4010 		return -ENOMEM;
4011 	}
4012 	ret = pci_register_driver(&iavf_driver);
4013 	return ret;
4014 }
4015 
4016 module_init(iavf_init_module);
4017 
4018 /**
4019  * iavf_exit_module - Driver Exit Cleanup Routine
4020  *
4021  * iavf_exit_module is called just before the driver is removed
4022  * from memory.
4023  **/
iavf_exit_module(void)4024 static void __exit iavf_exit_module(void)
4025 {
4026 	pci_unregister_driver(&iavf_driver);
4027 	destroy_workqueue(iavf_wq);
4028 }
4029 
4030 module_exit(iavf_exit_module);
4031 
4032 /* iavf_main.c */
4033