• 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 	iavf_change_state(adapter, __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 
1322 	if (!RSS_PF(adapter)) {
1323 		/* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1324 		if (adapter->vf_res->vf_cap_flags &
1325 		    VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1326 			adapter->hena = IAVF_DEFAULT_RSS_HENA_EXPANDED;
1327 		else
1328 			adapter->hena = IAVF_DEFAULT_RSS_HENA;
1329 
1330 		wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->hena);
1331 		wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1332 	}
1333 
1334 	iavf_fill_rss_lut(adapter);
1335 	netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1336 
1337 	return iavf_config_rss(adapter);
1338 }
1339 
1340 /**
1341  * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1342  * @adapter: board private structure to initialize
1343  *
1344  * We allocate one q_vector per queue interrupt.  If allocation fails we
1345  * return -ENOMEM.
1346  **/
iavf_alloc_q_vectors(struct iavf_adapter * adapter)1347 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1348 {
1349 	int q_idx = 0, num_q_vectors;
1350 	struct iavf_q_vector *q_vector;
1351 
1352 	num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1353 	adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1354 				     GFP_KERNEL);
1355 	if (!adapter->q_vectors)
1356 		return -ENOMEM;
1357 
1358 	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1359 		q_vector = &adapter->q_vectors[q_idx];
1360 		q_vector->adapter = adapter;
1361 		q_vector->vsi = &adapter->vsi;
1362 		q_vector->v_idx = q_idx;
1363 		q_vector->reg_idx = q_idx;
1364 		cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1365 		netif_napi_add(adapter->netdev, &q_vector->napi,
1366 			       iavf_napi_poll, NAPI_POLL_WEIGHT);
1367 	}
1368 
1369 	return 0;
1370 }
1371 
1372 /**
1373  * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1374  * @adapter: board private structure to initialize
1375  *
1376  * This function frees the memory allocated to the q_vectors.  In addition if
1377  * NAPI is enabled it will delete any references to the NAPI struct prior
1378  * to freeing the q_vector.
1379  **/
iavf_free_q_vectors(struct iavf_adapter * adapter)1380 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1381 {
1382 	int q_idx, num_q_vectors;
1383 	int napi_vectors;
1384 
1385 	if (!adapter->q_vectors)
1386 		return;
1387 
1388 	num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1389 	napi_vectors = adapter->num_active_queues;
1390 
1391 	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1392 		struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1393 
1394 		if (q_idx < napi_vectors)
1395 			netif_napi_del(&q_vector->napi);
1396 	}
1397 	kfree(adapter->q_vectors);
1398 	adapter->q_vectors = NULL;
1399 }
1400 
1401 /**
1402  * iavf_reset_interrupt_capability - Reset MSIX setup
1403  * @adapter: board private structure
1404  *
1405  **/
iavf_reset_interrupt_capability(struct iavf_adapter * adapter)1406 void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1407 {
1408 	if (!adapter->msix_entries)
1409 		return;
1410 
1411 	pci_disable_msix(adapter->pdev);
1412 	kfree(adapter->msix_entries);
1413 	adapter->msix_entries = NULL;
1414 }
1415 
1416 /**
1417  * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1418  * @adapter: board private structure to initialize
1419  *
1420  **/
iavf_init_interrupt_scheme(struct iavf_adapter * adapter)1421 int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1422 {
1423 	int err;
1424 
1425 	err = iavf_alloc_queues(adapter);
1426 	if (err) {
1427 		dev_err(&adapter->pdev->dev,
1428 			"Unable to allocate memory for queues\n");
1429 		goto err_alloc_queues;
1430 	}
1431 
1432 	rtnl_lock();
1433 	err = iavf_set_interrupt_capability(adapter);
1434 	rtnl_unlock();
1435 	if (err) {
1436 		dev_err(&adapter->pdev->dev,
1437 			"Unable to setup interrupt capabilities\n");
1438 		goto err_set_interrupt;
1439 	}
1440 
1441 	err = iavf_alloc_q_vectors(adapter);
1442 	if (err) {
1443 		dev_err(&adapter->pdev->dev,
1444 			"Unable to allocate memory for queue vectors\n");
1445 		goto err_alloc_q_vectors;
1446 	}
1447 
1448 	/* If we've made it so far while ADq flag being ON, then we haven't
1449 	 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1450 	 * resources have been allocated in the reset path.
1451 	 * Now we can truly claim that ADq is enabled.
1452 	 */
1453 	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1454 	    adapter->num_tc)
1455 		dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1456 			 adapter->num_tc);
1457 
1458 	dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1459 		 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1460 		 adapter->num_active_queues);
1461 
1462 	return 0;
1463 err_alloc_q_vectors:
1464 	iavf_reset_interrupt_capability(adapter);
1465 err_set_interrupt:
1466 	iavf_free_queues(adapter);
1467 err_alloc_queues:
1468 	return err;
1469 }
1470 
1471 /**
1472  * iavf_free_rss - Free memory used by RSS structs
1473  * @adapter: board private structure
1474  **/
iavf_free_rss(struct iavf_adapter * adapter)1475 static void iavf_free_rss(struct iavf_adapter *adapter)
1476 {
1477 	kfree(adapter->rss_key);
1478 	adapter->rss_key = NULL;
1479 
1480 	kfree(adapter->rss_lut);
1481 	adapter->rss_lut = NULL;
1482 }
1483 
1484 /**
1485  * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1486  * @adapter: board private structure
1487  *
1488  * Returns 0 on success, negative on failure
1489  **/
iavf_reinit_interrupt_scheme(struct iavf_adapter * adapter)1490 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter)
1491 {
1492 	struct net_device *netdev = adapter->netdev;
1493 	int err;
1494 
1495 	if (netif_running(netdev))
1496 		iavf_free_traffic_irqs(adapter);
1497 	iavf_free_misc_irq(adapter);
1498 	iavf_reset_interrupt_capability(adapter);
1499 	iavf_free_q_vectors(adapter);
1500 	iavf_free_queues(adapter);
1501 
1502 	err =  iavf_init_interrupt_scheme(adapter);
1503 	if (err)
1504 		goto err;
1505 
1506 	netif_tx_stop_all_queues(netdev);
1507 
1508 	err = iavf_request_misc_irq(adapter);
1509 	if (err)
1510 		goto err;
1511 
1512 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1513 
1514 	iavf_map_rings_to_vectors(adapter);
1515 err:
1516 	return err;
1517 }
1518 
1519 /**
1520  * iavf_process_aq_command - process aq_required flags
1521  * and sends aq command
1522  * @adapter: pointer to iavf adapter structure
1523  *
1524  * Returns 0 on success
1525  * Returns error code if no command was sent
1526  * or error code if the command failed.
1527  **/
iavf_process_aq_command(struct iavf_adapter * adapter)1528 static int iavf_process_aq_command(struct iavf_adapter *adapter)
1529 {
1530 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG)
1531 		return iavf_send_vf_config_msg(adapter);
1532 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
1533 		iavf_disable_queues(adapter);
1534 		return 0;
1535 	}
1536 
1537 	if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
1538 		iavf_map_queues(adapter);
1539 		return 0;
1540 	}
1541 
1542 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
1543 		iavf_add_ether_addrs(adapter);
1544 		return 0;
1545 	}
1546 
1547 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
1548 		iavf_add_vlans(adapter);
1549 		return 0;
1550 	}
1551 
1552 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
1553 		iavf_del_ether_addrs(adapter);
1554 		return 0;
1555 	}
1556 
1557 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
1558 		iavf_del_vlans(adapter);
1559 		return 0;
1560 	}
1561 
1562 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1563 		iavf_enable_vlan_stripping(adapter);
1564 		return 0;
1565 	}
1566 
1567 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1568 		iavf_disable_vlan_stripping(adapter);
1569 		return 0;
1570 	}
1571 
1572 	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
1573 		iavf_configure_queues(adapter);
1574 		return 0;
1575 	}
1576 
1577 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
1578 		iavf_enable_queues(adapter);
1579 		return 0;
1580 	}
1581 
1582 	if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
1583 		/* This message goes straight to the firmware, not the
1584 		 * PF, so we don't have to set current_op as we will
1585 		 * not get a response through the ARQ.
1586 		 */
1587 		adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
1588 		return 0;
1589 	}
1590 	if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
1591 		iavf_get_hena(adapter);
1592 		return 0;
1593 	}
1594 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
1595 		iavf_set_hena(adapter);
1596 		return 0;
1597 	}
1598 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
1599 		iavf_set_rss_key(adapter);
1600 		return 0;
1601 	}
1602 	if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
1603 		iavf_set_rss_lut(adapter);
1604 		return 0;
1605 	}
1606 
1607 	if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_PROMISC) {
1608 		iavf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1609 				       FLAG_VF_MULTICAST_PROMISC);
1610 		return 0;
1611 	}
1612 
1613 	if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_ALLMULTI) {
1614 		iavf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1615 		return 0;
1616 	}
1617 	if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) ||
1618 	    (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1619 		iavf_set_promiscuous(adapter, 0);
1620 		return 0;
1621 	}
1622 
1623 	if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
1624 		iavf_enable_channels(adapter);
1625 		return 0;
1626 	}
1627 
1628 	if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
1629 		iavf_disable_channels(adapter);
1630 		return 0;
1631 	}
1632 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1633 		iavf_add_cloud_filter(adapter);
1634 		return 0;
1635 	}
1636 
1637 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1638 		iavf_del_cloud_filter(adapter);
1639 		return 0;
1640 	}
1641 	if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1642 		iavf_del_cloud_filter(adapter);
1643 		return 0;
1644 	}
1645 	if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1646 		iavf_add_cloud_filter(adapter);
1647 		return 0;
1648 	}
1649 	return -EAGAIN;
1650 }
1651 
1652 /**
1653  * iavf_startup - first step of driver startup
1654  * @adapter: board private structure
1655  *
1656  * Function process __IAVF_STARTUP driver state.
1657  * When success the state is changed to __IAVF_INIT_VERSION_CHECK
1658  * when fails it returns -EAGAIN
1659  **/
iavf_startup(struct iavf_adapter * adapter)1660 static int iavf_startup(struct iavf_adapter *adapter)
1661 {
1662 	struct pci_dev *pdev = adapter->pdev;
1663 	struct iavf_hw *hw = &adapter->hw;
1664 	int err;
1665 
1666 	WARN_ON(adapter->state != __IAVF_STARTUP);
1667 
1668 	/* driver loaded, probe complete */
1669 	adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1670 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1671 	err = iavf_set_mac_type(hw);
1672 	if (err) {
1673 		dev_err(&pdev->dev, "Failed to set MAC type (%d)\n", err);
1674 		goto err;
1675 	}
1676 
1677 	err = iavf_check_reset_complete(hw);
1678 	if (err) {
1679 		dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
1680 			 err);
1681 		goto err;
1682 	}
1683 	hw->aq.num_arq_entries = IAVF_AQ_LEN;
1684 	hw->aq.num_asq_entries = IAVF_AQ_LEN;
1685 	hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
1686 	hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
1687 
1688 	err = iavf_init_adminq(hw);
1689 	if (err) {
1690 		dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n", err);
1691 		goto err;
1692 	}
1693 	err = iavf_send_api_ver(adapter);
1694 	if (err) {
1695 		dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
1696 		iavf_shutdown_adminq(hw);
1697 		goto err;
1698 	}
1699 	iavf_change_state(adapter, __IAVF_INIT_VERSION_CHECK);
1700 err:
1701 	return err;
1702 }
1703 
1704 /**
1705  * iavf_init_version_check - second step of driver startup
1706  * @adapter: board private structure
1707  *
1708  * Function process __IAVF_INIT_VERSION_CHECK driver state.
1709  * When success the state is changed to __IAVF_INIT_GET_RESOURCES
1710  * when fails it returns -EAGAIN
1711  **/
iavf_init_version_check(struct iavf_adapter * adapter)1712 static int iavf_init_version_check(struct iavf_adapter *adapter)
1713 {
1714 	struct pci_dev *pdev = adapter->pdev;
1715 	struct iavf_hw *hw = &adapter->hw;
1716 	int err = -EAGAIN;
1717 
1718 	WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
1719 
1720 	if (!iavf_asq_done(hw)) {
1721 		dev_err(&pdev->dev, "Admin queue command never completed\n");
1722 		iavf_shutdown_adminq(hw);
1723 		iavf_change_state(adapter, __IAVF_STARTUP);
1724 		goto err;
1725 	}
1726 
1727 	/* aq msg sent, awaiting reply */
1728 	err = iavf_verify_api_ver(adapter);
1729 	if (err) {
1730 		if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK)
1731 			err = iavf_send_api_ver(adapter);
1732 		else
1733 			dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
1734 				adapter->pf_version.major,
1735 				adapter->pf_version.minor,
1736 				VIRTCHNL_VERSION_MAJOR,
1737 				VIRTCHNL_VERSION_MINOR);
1738 		goto err;
1739 	}
1740 	err = iavf_send_vf_config_msg(adapter);
1741 	if (err) {
1742 		dev_err(&pdev->dev, "Unable to send config request (%d)\n",
1743 			err);
1744 		goto err;
1745 	}
1746 	iavf_change_state(adapter, __IAVF_INIT_GET_RESOURCES);
1747 err:
1748 	return err;
1749 }
1750 
1751 /**
1752  * iavf_init_get_resources - third step of driver startup
1753  * @adapter: board private structure
1754  *
1755  * Function process __IAVF_INIT_GET_RESOURCES driver state and
1756  * finishes driver initialization procedure.
1757  * When success the state is changed to __IAVF_DOWN
1758  * when fails it returns -EAGAIN
1759  **/
iavf_init_get_resources(struct iavf_adapter * adapter)1760 static int iavf_init_get_resources(struct iavf_adapter *adapter)
1761 {
1762 	struct net_device *netdev = adapter->netdev;
1763 	struct pci_dev *pdev = adapter->pdev;
1764 	struct iavf_hw *hw = &adapter->hw;
1765 	int err;
1766 
1767 	WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
1768 	/* aq msg sent, awaiting reply */
1769 	if (!adapter->vf_res) {
1770 		adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
1771 					  GFP_KERNEL);
1772 		if (!adapter->vf_res) {
1773 			err = -ENOMEM;
1774 			goto err;
1775 		}
1776 	}
1777 	err = iavf_get_vf_config(adapter);
1778 	if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK) {
1779 		err = iavf_send_vf_config_msg(adapter);
1780 		goto err;
1781 	} else if (err == IAVF_ERR_PARAM) {
1782 		/* We only get ERR_PARAM if the device is in a very bad
1783 		 * state or if we've been disabled for previous bad
1784 		 * behavior. Either way, we're done now.
1785 		 */
1786 		iavf_shutdown_adminq(hw);
1787 		dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
1788 		return 0;
1789 	}
1790 	if (err) {
1791 		dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
1792 		goto err_alloc;
1793 	}
1794 
1795 	err = iavf_process_config(adapter);
1796 	if (err)
1797 		goto err_alloc;
1798 	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1799 
1800 	adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
1801 
1802 	netdev->netdev_ops = &iavf_netdev_ops;
1803 	iavf_set_ethtool_ops(netdev);
1804 	netdev->watchdog_timeo = 5 * HZ;
1805 
1806 	/* MTU range: 68 - 9710 */
1807 	netdev->min_mtu = ETH_MIN_MTU;
1808 	netdev->max_mtu = IAVF_MAX_RXBUFFER - IAVF_PACKET_HDR_PAD;
1809 
1810 	if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
1811 		dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1812 			 adapter->hw.mac.addr);
1813 		eth_hw_addr_random(netdev);
1814 		ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1815 	} else {
1816 		ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
1817 		ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
1818 	}
1819 
1820 	adapter->tx_desc_count = IAVF_DEFAULT_TXD;
1821 	adapter->rx_desc_count = IAVF_DEFAULT_RXD;
1822 	err = iavf_init_interrupt_scheme(adapter);
1823 	if (err)
1824 		goto err_sw_init;
1825 	iavf_map_rings_to_vectors(adapter);
1826 	if (adapter->vf_res->vf_cap_flags &
1827 		VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1828 		adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
1829 
1830 	err = iavf_request_misc_irq(adapter);
1831 	if (err)
1832 		goto err_sw_init;
1833 
1834 	netif_carrier_off(netdev);
1835 	adapter->link_up = false;
1836 
1837 	/* set the semaphore to prevent any callbacks after device registration
1838 	 * up to time when state of driver will be set to __IAVF_DOWN
1839 	 */
1840 	rtnl_lock();
1841 	if (!adapter->netdev_registered) {
1842 		err = register_netdevice(netdev);
1843 		if (err) {
1844 			rtnl_unlock();
1845 			goto err_register;
1846 		}
1847 	}
1848 
1849 	adapter->netdev_registered = true;
1850 
1851 	netif_tx_stop_all_queues(netdev);
1852 	if (CLIENT_ALLOWED(adapter)) {
1853 		err = iavf_lan_add_device(adapter);
1854 		if (err)
1855 			dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
1856 				 err);
1857 	}
1858 	dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
1859 	if (netdev->features & NETIF_F_GRO)
1860 		dev_info(&pdev->dev, "GRO is enabled\n");
1861 
1862 	iavf_change_state(adapter, __IAVF_DOWN);
1863 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1864 	rtnl_unlock();
1865 
1866 	iavf_misc_irq_enable(adapter);
1867 	wake_up(&adapter->down_waitqueue);
1868 
1869 	adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
1870 	adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
1871 	if (!adapter->rss_key || !adapter->rss_lut) {
1872 		err = -ENOMEM;
1873 		goto err_mem;
1874 	}
1875 	if (RSS_AQ(adapter))
1876 		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
1877 	else
1878 		iavf_init_rss(adapter);
1879 
1880 	return err;
1881 err_mem:
1882 	iavf_free_rss(adapter);
1883 err_register:
1884 	iavf_free_misc_irq(adapter);
1885 err_sw_init:
1886 	iavf_reset_interrupt_capability(adapter);
1887 err_alloc:
1888 	kfree(adapter->vf_res);
1889 	adapter->vf_res = NULL;
1890 err:
1891 	return err;
1892 }
1893 
1894 /**
1895  * iavf_watchdog_task - Periodic call-back task
1896  * @work: pointer to work_struct
1897  **/
iavf_watchdog_task(struct work_struct * work)1898 static void iavf_watchdog_task(struct work_struct *work)
1899 {
1900 	struct iavf_adapter *adapter = container_of(work,
1901 						    struct iavf_adapter,
1902 						    watchdog_task.work);
1903 	struct iavf_hw *hw = &adapter->hw;
1904 	u32 reg_val;
1905 
1906 	if (test_and_set_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section))
1907 		goto restart_watchdog;
1908 
1909 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
1910 		iavf_change_state(adapter, __IAVF_COMM_FAILED);
1911 
1912 	switch (adapter->state) {
1913 	case __IAVF_COMM_FAILED:
1914 		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
1915 			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1916 		if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
1917 		    reg_val == VIRTCHNL_VFR_COMPLETED) {
1918 			/* A chance for redemption! */
1919 			dev_err(&adapter->pdev->dev,
1920 				"Hardware came out of reset. Attempting reinit.\n");
1921 			iavf_change_state(adapter, __IAVF_STARTUP);
1922 			adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1923 			queue_delayed_work(iavf_wq, &adapter->init_task, 10);
1924 			clear_bit(__IAVF_IN_CRITICAL_TASK,
1925 				  &adapter->crit_section);
1926 			/* Don't reschedule the watchdog, since we've restarted
1927 			 * the init task. When init_task contacts the PF and
1928 			 * gets everything set up again, it'll restart the
1929 			 * watchdog for us. Down, boy. Sit. Stay. Woof.
1930 			 */
1931 			return;
1932 		}
1933 		adapter->aq_required = 0;
1934 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1935 		clear_bit(__IAVF_IN_CRITICAL_TASK,
1936 			  &adapter->crit_section);
1937 		queue_delayed_work(iavf_wq,
1938 				   &adapter->watchdog_task,
1939 				   msecs_to_jiffies(10));
1940 		goto watchdog_done;
1941 	case __IAVF_RESETTING:
1942 		clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1943 		queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
1944 		return;
1945 	case __IAVF_DOWN:
1946 	case __IAVF_DOWN_PENDING:
1947 	case __IAVF_TESTING:
1948 	case __IAVF_RUNNING:
1949 		if (adapter->current_op) {
1950 			if (!iavf_asq_done(hw)) {
1951 				dev_dbg(&adapter->pdev->dev,
1952 					"Admin queue timeout\n");
1953 				iavf_send_api_ver(adapter);
1954 			}
1955 		} else {
1956 			/* An error will be returned if no commands were
1957 			 * processed; use this opportunity to update stats
1958 			 */
1959 			if (iavf_process_aq_command(adapter) &&
1960 			    adapter->state == __IAVF_RUNNING)
1961 				iavf_request_stats(adapter);
1962 		}
1963 		break;
1964 	case __IAVF_REMOVE:
1965 		clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1966 		return;
1967 	default:
1968 		goto restart_watchdog;
1969 	}
1970 
1971 	/* check for hw reset */
1972 	reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
1973 	if (!reg_val) {
1974 		iavf_change_state(adapter, __IAVF_RESETTING);
1975 		adapter->flags |= IAVF_FLAG_RESET_PENDING;
1976 		adapter->aq_required = 0;
1977 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1978 		dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1979 		queue_work(iavf_wq, &adapter->reset_task);
1980 		goto watchdog_done;
1981 	}
1982 
1983 	schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1984 watchdog_done:
1985 	if (adapter->state == __IAVF_RUNNING ||
1986 	    adapter->state == __IAVF_COMM_FAILED)
1987 		iavf_detect_recover_hung(&adapter->vsi);
1988 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1989 restart_watchdog:
1990 	if (adapter->aq_required)
1991 		queue_delayed_work(iavf_wq, &adapter->watchdog_task,
1992 				   msecs_to_jiffies(20));
1993 	else
1994 		queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
1995 	queue_work(iavf_wq, &adapter->adminq_task);
1996 }
1997 
iavf_disable_vf(struct iavf_adapter * adapter)1998 static void iavf_disable_vf(struct iavf_adapter *adapter)
1999 {
2000 	struct iavf_mac_filter *f, *ftmp;
2001 	struct iavf_vlan_filter *fv, *fvtmp;
2002 	struct iavf_cloud_filter *cf, *cftmp;
2003 
2004 	adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2005 
2006 	/* We don't use netif_running() because it may be true prior to
2007 	 * ndo_open() returning, so we can't assume it means all our open
2008 	 * tasks have finished, since we're not holding the rtnl_lock here.
2009 	 */
2010 	if (adapter->state == __IAVF_RUNNING) {
2011 		set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2012 		netif_carrier_off(adapter->netdev);
2013 		netif_tx_disable(adapter->netdev);
2014 		adapter->link_up = false;
2015 		iavf_napi_disable_all(adapter);
2016 		iavf_irq_disable(adapter);
2017 		iavf_free_traffic_irqs(adapter);
2018 		iavf_free_all_tx_resources(adapter);
2019 		iavf_free_all_rx_resources(adapter);
2020 	}
2021 
2022 	spin_lock_bh(&adapter->mac_vlan_list_lock);
2023 
2024 	/* Delete all of the filters */
2025 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2026 		list_del(&f->list);
2027 		kfree(f);
2028 	}
2029 
2030 	list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
2031 		list_del(&fv->list);
2032 		kfree(fv);
2033 	}
2034 
2035 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
2036 
2037 	spin_lock_bh(&adapter->cloud_filter_list_lock);
2038 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
2039 		list_del(&cf->list);
2040 		kfree(cf);
2041 		adapter->num_cloud_filters--;
2042 	}
2043 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2044 
2045 	iavf_free_misc_irq(adapter);
2046 	iavf_reset_interrupt_capability(adapter);
2047 	iavf_free_q_vectors(adapter);
2048 	iavf_free_queues(adapter);
2049 	memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
2050 	iavf_shutdown_adminq(&adapter->hw);
2051 	adapter->netdev->flags &= ~IFF_UP;
2052 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2053 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2054 	iavf_change_state(adapter, __IAVF_DOWN);
2055 	wake_up(&adapter->down_waitqueue);
2056 	dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
2057 }
2058 
2059 /**
2060  * iavf_reset_task - Call-back task to handle hardware reset
2061  * @work: pointer to work_struct
2062  *
2063  * During reset we need to shut down and reinitialize the admin queue
2064  * before we can use it to communicate with the PF again. We also clear
2065  * and reinit the rings because that context is lost as well.
2066  **/
iavf_reset_task(struct work_struct * work)2067 static void iavf_reset_task(struct work_struct *work)
2068 {
2069 	struct iavf_adapter *adapter = container_of(work,
2070 						      struct iavf_adapter,
2071 						      reset_task);
2072 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
2073 	struct net_device *netdev = adapter->netdev;
2074 	struct iavf_hw *hw = &adapter->hw;
2075 	struct iavf_mac_filter *f, *ftmp;
2076 	struct iavf_vlan_filter *vlf;
2077 	struct iavf_cloud_filter *cf;
2078 	u32 reg_val;
2079 	int i = 0, err;
2080 	bool running;
2081 
2082 	/* When device is being removed it doesn't make sense to run the reset
2083 	 * task, just return in such a case.
2084 	 */
2085 	if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
2086 		return;
2087 
2088 	if (iavf_lock_timeout(adapter, __IAVF_IN_CRITICAL_TASK, 200)) {
2089 		schedule_work(&adapter->reset_task);
2090 		return;
2091 	}
2092 	while (test_and_set_bit(__IAVF_IN_CLIENT_TASK,
2093 				&adapter->crit_section))
2094 		usleep_range(500, 1000);
2095 	if (CLIENT_ENABLED(adapter)) {
2096 		adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
2097 				    IAVF_FLAG_CLIENT_NEEDS_CLOSE |
2098 				    IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
2099 				    IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
2100 		cancel_delayed_work_sync(&adapter->client_task);
2101 		iavf_notify_client_close(&adapter->vsi, true);
2102 	}
2103 	iavf_misc_irq_disable(adapter);
2104 	if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
2105 		adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
2106 		/* Restart the AQ here. If we have been reset but didn't
2107 		 * detect it, or if the PF had to reinit, our AQ will be hosed.
2108 		 */
2109 		iavf_shutdown_adminq(hw);
2110 		iavf_init_adminq(hw);
2111 		iavf_request_reset(adapter);
2112 	}
2113 	adapter->flags |= IAVF_FLAG_RESET_PENDING;
2114 
2115 	/* poll until we see the reset actually happen */
2116 	for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
2117 		reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
2118 			  IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2119 		if (!reg_val)
2120 			break;
2121 		usleep_range(5000, 10000);
2122 	}
2123 	if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
2124 		dev_info(&adapter->pdev->dev, "Never saw reset\n");
2125 		goto continue_reset; /* act like the reset happened */
2126 	}
2127 
2128 	/* wait until the reset is complete and the PF is responding to us */
2129 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
2130 		/* sleep first to make sure a minimum wait time is met */
2131 		msleep(IAVF_RESET_WAIT_MS);
2132 
2133 		reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2134 			  IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2135 		if (reg_val == VIRTCHNL_VFR_VFACTIVE)
2136 			break;
2137 	}
2138 
2139 	pci_set_master(adapter->pdev);
2140 	pci_restore_msi_state(adapter->pdev);
2141 
2142 	if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
2143 		dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
2144 			reg_val);
2145 		iavf_disable_vf(adapter);
2146 		clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2147 		return; /* Do not attempt to reinit. It's dead, Jim. */
2148 	}
2149 
2150 continue_reset:
2151 	/* We don't use netif_running() because it may be true prior to
2152 	 * ndo_open() returning, so we can't assume it means all our open
2153 	 * tasks have finished, since we're not holding the rtnl_lock here.
2154 	 */
2155 	running = ((adapter->state == __IAVF_RUNNING) ||
2156 		   (adapter->state == __IAVF_RESETTING));
2157 
2158 	if (running) {
2159 		netif_carrier_off(netdev);
2160 		netif_tx_stop_all_queues(netdev);
2161 		adapter->link_up = false;
2162 		iavf_napi_disable_all(adapter);
2163 	}
2164 	iavf_irq_disable(adapter);
2165 
2166 	iavf_change_state(adapter, __IAVF_RESETTING);
2167 	adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2168 
2169 	/* free the Tx/Rx rings and descriptors, might be better to just
2170 	 * re-use them sometime in the future
2171 	 */
2172 	iavf_free_all_rx_resources(adapter);
2173 	iavf_free_all_tx_resources(adapter);
2174 
2175 	adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
2176 	/* kill and reinit the admin queue */
2177 	iavf_shutdown_adminq(hw);
2178 	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2179 	err = iavf_init_adminq(hw);
2180 	if (err)
2181 		dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
2182 			 err);
2183 	adapter->aq_required = 0;
2184 
2185 	if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
2186 		err = iavf_reinit_interrupt_scheme(adapter);
2187 		if (err)
2188 			goto reset_err;
2189 	}
2190 
2191 	if (RSS_AQ(adapter)) {
2192 		adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
2193 	} else {
2194 		err = iavf_init_rss(adapter);
2195 		if (err)
2196 			goto reset_err;
2197 	}
2198 
2199 	adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
2200 	adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
2201 
2202 	spin_lock_bh(&adapter->mac_vlan_list_lock);
2203 
2204 	/* Delete filter for the current MAC address, it could have
2205 	 * been changed by the PF via administratively set MAC.
2206 	 * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
2207 	 */
2208 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2209 		if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
2210 			list_del(&f->list);
2211 			kfree(f);
2212 		}
2213 	}
2214 	/* re-add all MAC filters */
2215 	list_for_each_entry(f, &adapter->mac_filter_list, list) {
2216 		f->add = true;
2217 	}
2218 	/* re-add all VLAN filters */
2219 	list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
2220 		vlf->add = true;
2221 	}
2222 
2223 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
2224 
2225 	/* check if TCs are running and re-add all cloud filters */
2226 	spin_lock_bh(&adapter->cloud_filter_list_lock);
2227 	if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
2228 	    adapter->num_tc) {
2229 		list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
2230 			cf->add = true;
2231 		}
2232 	}
2233 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2234 
2235 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
2236 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
2237 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
2238 	iavf_misc_irq_enable(adapter);
2239 
2240 	mod_delayed_work(iavf_wq, &adapter->watchdog_task, 2);
2241 
2242 	/* We were running when the reset started, so we need to restore some
2243 	 * state here.
2244 	 */
2245 	if (running) {
2246 		/* allocate transmit descriptors */
2247 		err = iavf_setup_all_tx_resources(adapter);
2248 		if (err)
2249 			goto reset_err;
2250 
2251 		/* allocate receive descriptors */
2252 		err = iavf_setup_all_rx_resources(adapter);
2253 		if (err)
2254 			goto reset_err;
2255 
2256 		if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
2257 			err = iavf_request_traffic_irqs(adapter, netdev->name);
2258 			if (err)
2259 				goto reset_err;
2260 
2261 			adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
2262 		}
2263 
2264 		iavf_configure(adapter);
2265 
2266 		/* iavf_up_complete() will switch device back
2267 		 * to __IAVF_RUNNING
2268 		 */
2269 		iavf_up_complete(adapter);
2270 
2271 		iavf_irq_enable(adapter, true);
2272 	} else {
2273 		iavf_change_state(adapter, __IAVF_DOWN);
2274 		wake_up(&adapter->down_waitqueue);
2275 	}
2276 	clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2277 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2278 
2279 	return;
2280 reset_err:
2281 	clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2282 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2283 	dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
2284 	iavf_close(netdev);
2285 }
2286 
2287 /**
2288  * iavf_adminq_task - worker thread to clean the admin queue
2289  * @work: pointer to work_struct containing our data
2290  **/
iavf_adminq_task(struct work_struct * work)2291 static void iavf_adminq_task(struct work_struct *work)
2292 {
2293 	struct iavf_adapter *adapter =
2294 		container_of(work, struct iavf_adapter, adminq_task);
2295 	struct iavf_hw *hw = &adapter->hw;
2296 	struct iavf_arq_event_info event;
2297 	enum virtchnl_ops v_op;
2298 	enum iavf_status ret, v_ret;
2299 	u32 val, oldval;
2300 	u16 pending;
2301 
2302 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2303 		goto out;
2304 
2305 	event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
2306 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2307 	if (!event.msg_buf)
2308 		goto out;
2309 
2310 	if (iavf_lock_timeout(adapter, __IAVF_IN_CRITICAL_TASK, 200))
2311 		goto freedom;
2312 	do {
2313 		ret = iavf_clean_arq_element(hw, &event, &pending);
2314 		v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2315 		v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
2316 
2317 		if (ret || !v_op)
2318 			break; /* No event to process or error cleaning ARQ */
2319 
2320 		iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2321 					 event.msg_len);
2322 		if (pending != 0)
2323 			memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
2324 	} while (pending);
2325 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2326 
2327 	if ((adapter->flags &
2328 	     (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED)) ||
2329 	    adapter->state == __IAVF_RESETTING)
2330 		goto freedom;
2331 
2332 	/* check for error indications */
2333 	val = rd32(hw, hw->aq.arq.len);
2334 	if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
2335 		goto freedom;
2336 	oldval = val;
2337 	if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
2338 		dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2339 		val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
2340 	}
2341 	if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
2342 		dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2343 		val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
2344 	}
2345 	if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
2346 		dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2347 		val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
2348 	}
2349 	if (oldval != val)
2350 		wr32(hw, hw->aq.arq.len, val);
2351 
2352 	val = rd32(hw, hw->aq.asq.len);
2353 	oldval = val;
2354 	if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
2355 		dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2356 		val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
2357 	}
2358 	if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
2359 		dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2360 		val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
2361 	}
2362 	if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
2363 		dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2364 		val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
2365 	}
2366 	if (oldval != val)
2367 		wr32(hw, hw->aq.asq.len, val);
2368 
2369 freedom:
2370 	kfree(event.msg_buf);
2371 out:
2372 	/* re-enable Admin queue interrupt cause */
2373 	iavf_misc_irq_enable(adapter);
2374 }
2375 
2376 /**
2377  * iavf_client_task - worker thread to perform client work
2378  * @work: pointer to work_struct containing our data
2379  *
2380  * This task handles client interactions. Because client calls can be
2381  * reentrant, we can't handle them in the watchdog.
2382  **/
iavf_client_task(struct work_struct * work)2383 static void iavf_client_task(struct work_struct *work)
2384 {
2385 	struct iavf_adapter *adapter =
2386 		container_of(work, struct iavf_adapter, client_task.work);
2387 
2388 	/* If we can't get the client bit, just give up. We'll be rescheduled
2389 	 * later.
2390 	 */
2391 
2392 	if (test_and_set_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section))
2393 		return;
2394 
2395 	if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2396 		iavf_client_subtask(adapter);
2397 		adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
2398 		goto out;
2399 	}
2400 	if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2401 		iavf_notify_client_l2_params(&adapter->vsi);
2402 		adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2403 		goto out;
2404 	}
2405 	if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_CLOSE) {
2406 		iavf_notify_client_close(&adapter->vsi, false);
2407 		adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_CLOSE;
2408 		goto out;
2409 	}
2410 	if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
2411 		iavf_notify_client_open(&adapter->vsi);
2412 		adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
2413 	}
2414 out:
2415 	clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2416 }
2417 
2418 /**
2419  * iavf_free_all_tx_resources - Free Tx Resources for All Queues
2420  * @adapter: board private structure
2421  *
2422  * Free all transmit software resources
2423  **/
iavf_free_all_tx_resources(struct iavf_adapter * adapter)2424 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
2425 {
2426 	int i;
2427 
2428 	if (!adapter->tx_rings)
2429 		return;
2430 
2431 	for (i = 0; i < adapter->num_active_queues; i++)
2432 		if (adapter->tx_rings[i].desc)
2433 			iavf_free_tx_resources(&adapter->tx_rings[i]);
2434 }
2435 
2436 /**
2437  * iavf_setup_all_tx_resources - allocate all queues Tx resources
2438  * @adapter: board private structure
2439  *
2440  * If this function returns with an error, then it's possible one or
2441  * more of the rings is populated (while the rest are not).  It is the
2442  * callers duty to clean those orphaned rings.
2443  *
2444  * Return 0 on success, negative on failure
2445  **/
iavf_setup_all_tx_resources(struct iavf_adapter * adapter)2446 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
2447 {
2448 	int i, err = 0;
2449 
2450 	for (i = 0; i < adapter->num_active_queues; i++) {
2451 		adapter->tx_rings[i].count = adapter->tx_desc_count;
2452 		err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
2453 		if (!err)
2454 			continue;
2455 		dev_err(&adapter->pdev->dev,
2456 			"Allocation for Tx Queue %u failed\n", i);
2457 		break;
2458 	}
2459 
2460 	return err;
2461 }
2462 
2463 /**
2464  * iavf_setup_all_rx_resources - allocate all queues Rx resources
2465  * @adapter: board private structure
2466  *
2467  * If this function returns with an error, then it's possible one or
2468  * more of the rings is populated (while the rest are not).  It is the
2469  * callers duty to clean those orphaned rings.
2470  *
2471  * Return 0 on success, negative on failure
2472  **/
iavf_setup_all_rx_resources(struct iavf_adapter * adapter)2473 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
2474 {
2475 	int i, err = 0;
2476 
2477 	for (i = 0; i < adapter->num_active_queues; i++) {
2478 		adapter->rx_rings[i].count = adapter->rx_desc_count;
2479 		err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
2480 		if (!err)
2481 			continue;
2482 		dev_err(&adapter->pdev->dev,
2483 			"Allocation for Rx Queue %u failed\n", i);
2484 		break;
2485 	}
2486 	return err;
2487 }
2488 
2489 /**
2490  * iavf_free_all_rx_resources - Free Rx Resources for All Queues
2491  * @adapter: board private structure
2492  *
2493  * Free all receive software resources
2494  **/
iavf_free_all_rx_resources(struct iavf_adapter * adapter)2495 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
2496 {
2497 	int i;
2498 
2499 	if (!adapter->rx_rings)
2500 		return;
2501 
2502 	for (i = 0; i < adapter->num_active_queues; i++)
2503 		if (adapter->rx_rings[i].desc)
2504 			iavf_free_rx_resources(&adapter->rx_rings[i]);
2505 }
2506 
2507 /**
2508  * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
2509  * @adapter: board private structure
2510  * @max_tx_rate: max Tx bw for a tc
2511  **/
iavf_validate_tx_bandwidth(struct iavf_adapter * adapter,u64 max_tx_rate)2512 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
2513 				      u64 max_tx_rate)
2514 {
2515 	int speed = 0, ret = 0;
2516 
2517 	if (ADV_LINK_SUPPORT(adapter)) {
2518 		if (adapter->link_speed_mbps < U32_MAX) {
2519 			speed = adapter->link_speed_mbps;
2520 			goto validate_bw;
2521 		} else {
2522 			dev_err(&adapter->pdev->dev, "Unknown link speed\n");
2523 			return -EINVAL;
2524 		}
2525 	}
2526 
2527 	switch (adapter->link_speed) {
2528 	case VIRTCHNL_LINK_SPEED_40GB:
2529 		speed = SPEED_40000;
2530 		break;
2531 	case VIRTCHNL_LINK_SPEED_25GB:
2532 		speed = SPEED_25000;
2533 		break;
2534 	case VIRTCHNL_LINK_SPEED_20GB:
2535 		speed = SPEED_20000;
2536 		break;
2537 	case VIRTCHNL_LINK_SPEED_10GB:
2538 		speed = SPEED_10000;
2539 		break;
2540 	case VIRTCHNL_LINK_SPEED_5GB:
2541 		speed = SPEED_5000;
2542 		break;
2543 	case VIRTCHNL_LINK_SPEED_2_5GB:
2544 		speed = SPEED_2500;
2545 		break;
2546 	case VIRTCHNL_LINK_SPEED_1GB:
2547 		speed = SPEED_1000;
2548 		break;
2549 	case VIRTCHNL_LINK_SPEED_100MB:
2550 		speed = SPEED_100;
2551 		break;
2552 	default:
2553 		break;
2554 	}
2555 
2556 validate_bw:
2557 	if (max_tx_rate > speed) {
2558 		dev_err(&adapter->pdev->dev,
2559 			"Invalid tx rate specified\n");
2560 		ret = -EINVAL;
2561 	}
2562 
2563 	return ret;
2564 }
2565 
2566 /**
2567  * iavf_validate_channel_config - validate queue mapping info
2568  * @adapter: board private structure
2569  * @mqprio_qopt: queue parameters
2570  *
2571  * This function validates if the config provided by the user to
2572  * configure queue channels is valid or not. Returns 0 on a valid
2573  * config.
2574  **/
iavf_validate_ch_config(struct iavf_adapter * adapter,struct tc_mqprio_qopt_offload * mqprio_qopt)2575 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
2576 				   struct tc_mqprio_qopt_offload *mqprio_qopt)
2577 {
2578 	u64 total_max_rate = 0;
2579 	u32 tx_rate_rem = 0;
2580 	int i, num_qps = 0;
2581 	u64 tx_rate = 0;
2582 	int ret = 0;
2583 
2584 	if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
2585 	    mqprio_qopt->qopt.num_tc < 1)
2586 		return -EINVAL;
2587 
2588 	for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2589 		if (!mqprio_qopt->qopt.count[i] ||
2590 		    mqprio_qopt->qopt.offset[i] != num_qps)
2591 			return -EINVAL;
2592 		if (mqprio_qopt->min_rate[i]) {
2593 			dev_err(&adapter->pdev->dev,
2594 				"Invalid min tx rate (greater than 0) specified for TC%d\n",
2595 				i);
2596 			return -EINVAL;
2597 		}
2598 
2599 		/* convert to Mbps */
2600 		tx_rate = div_u64(mqprio_qopt->max_rate[i],
2601 				  IAVF_MBPS_DIVISOR);
2602 
2603 		if (mqprio_qopt->max_rate[i] &&
2604 		    tx_rate < IAVF_MBPS_QUANTA) {
2605 			dev_err(&adapter->pdev->dev,
2606 				"Invalid max tx rate for TC%d, minimum %dMbps\n",
2607 				i, IAVF_MBPS_QUANTA);
2608 			return -EINVAL;
2609 		}
2610 
2611 		(void)div_u64_rem(tx_rate, IAVF_MBPS_QUANTA, &tx_rate_rem);
2612 
2613 		if (tx_rate_rem != 0) {
2614 			dev_err(&adapter->pdev->dev,
2615 				"Invalid max tx rate for TC%d, not divisible by %d\n",
2616 				i, IAVF_MBPS_QUANTA);
2617 			return -EINVAL;
2618 		}
2619 
2620 		total_max_rate += tx_rate;
2621 		num_qps += mqprio_qopt->qopt.count[i];
2622 	}
2623 	if (num_qps > adapter->num_active_queues) {
2624 		dev_err(&adapter->pdev->dev,
2625 			"Cannot support requested number of queues\n");
2626 		return -EINVAL;
2627 	}
2628 
2629 	ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
2630 	return ret;
2631 }
2632 
2633 /**
2634  * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
2635  * @adapter: board private structure
2636  **/
iavf_del_all_cloud_filters(struct iavf_adapter * adapter)2637 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
2638 {
2639 	struct iavf_cloud_filter *cf, *cftmp;
2640 
2641 	spin_lock_bh(&adapter->cloud_filter_list_lock);
2642 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2643 				 list) {
2644 		list_del(&cf->list);
2645 		kfree(cf);
2646 		adapter->num_cloud_filters--;
2647 	}
2648 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
2649 }
2650 
2651 /**
2652  * __iavf_setup_tc - configure multiple traffic classes
2653  * @netdev: network interface device structure
2654  * @type_data: tc offload data
2655  *
2656  * This function processes the config information provided by the
2657  * user to configure traffic classes/queue channels and packages the
2658  * information to request the PF to setup traffic classes.
2659  *
2660  * Returns 0 on success.
2661  **/
__iavf_setup_tc(struct net_device * netdev,void * type_data)2662 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
2663 {
2664 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2665 	struct iavf_adapter *adapter = netdev_priv(netdev);
2666 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
2667 	u8 num_tc = 0, total_qps = 0;
2668 	int ret = 0, netdev_tc = 0;
2669 	u64 max_tx_rate;
2670 	u16 mode;
2671 	int i;
2672 
2673 	num_tc = mqprio_qopt->qopt.num_tc;
2674 	mode = mqprio_qopt->mode;
2675 
2676 	/* delete queue_channel */
2677 	if (!mqprio_qopt->qopt.hw) {
2678 		if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
2679 			/* reset the tc configuration */
2680 			netdev_reset_tc(netdev);
2681 			adapter->num_tc = 0;
2682 			netif_tx_stop_all_queues(netdev);
2683 			netif_tx_disable(netdev);
2684 			iavf_del_all_cloud_filters(adapter);
2685 			adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
2686 			goto exit;
2687 		} else {
2688 			return -EINVAL;
2689 		}
2690 	}
2691 
2692 	/* add queue channel */
2693 	if (mode == TC_MQPRIO_MODE_CHANNEL) {
2694 		if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2695 			dev_err(&adapter->pdev->dev, "ADq not supported\n");
2696 			return -EOPNOTSUPP;
2697 		}
2698 		if (adapter->ch_config.state != __IAVF_TC_INVALID) {
2699 			dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2700 			return -EINVAL;
2701 		}
2702 
2703 		ret = iavf_validate_ch_config(adapter, mqprio_qopt);
2704 		if (ret)
2705 			return ret;
2706 		/* Return if same TC config is requested */
2707 		if (adapter->num_tc == num_tc)
2708 			return 0;
2709 		adapter->num_tc = num_tc;
2710 
2711 		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2712 			if (i < num_tc) {
2713 				adapter->ch_config.ch_info[i].count =
2714 					mqprio_qopt->qopt.count[i];
2715 				adapter->ch_config.ch_info[i].offset =
2716 					mqprio_qopt->qopt.offset[i];
2717 				total_qps += mqprio_qopt->qopt.count[i];
2718 				max_tx_rate = mqprio_qopt->max_rate[i];
2719 				/* convert to Mbps */
2720 				max_tx_rate = div_u64(max_tx_rate,
2721 						      IAVF_MBPS_DIVISOR);
2722 				adapter->ch_config.ch_info[i].max_tx_rate =
2723 					max_tx_rate;
2724 			} else {
2725 				adapter->ch_config.ch_info[i].count = 1;
2726 				adapter->ch_config.ch_info[i].offset = 0;
2727 			}
2728 		}
2729 		adapter->ch_config.total_qps = total_qps;
2730 		netif_tx_stop_all_queues(netdev);
2731 		netif_tx_disable(netdev);
2732 		adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
2733 		netdev_reset_tc(netdev);
2734 		/* Report the tc mapping up the stack */
2735 		netdev_set_num_tc(adapter->netdev, num_tc);
2736 		for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2737 			u16 qcount = mqprio_qopt->qopt.count[i];
2738 			u16 qoffset = mqprio_qopt->qopt.offset[i];
2739 
2740 			if (i < num_tc)
2741 				netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2742 						    qoffset);
2743 		}
2744 	}
2745 exit:
2746 	return ret;
2747 }
2748 
2749 /**
2750  * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
2751  * @adapter: board private structure
2752  * @f: pointer to struct flow_cls_offload
2753  * @filter: pointer to cloud filter structure
2754  */
iavf_parse_cls_flower(struct iavf_adapter * adapter,struct flow_cls_offload * f,struct iavf_cloud_filter * filter)2755 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
2756 				 struct flow_cls_offload *f,
2757 				 struct iavf_cloud_filter *filter)
2758 {
2759 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
2760 	struct flow_dissector *dissector = rule->match.dissector;
2761 	u16 n_proto_mask = 0;
2762 	u16 n_proto_key = 0;
2763 	u8 field_flags = 0;
2764 	u16 addr_type = 0;
2765 	u16 n_proto = 0;
2766 	int i = 0;
2767 	struct virtchnl_filter *vf = &filter->f;
2768 
2769 	if (dissector->used_keys &
2770 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2771 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
2772 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2773 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
2774 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2775 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2776 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
2777 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2778 		dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2779 			dissector->used_keys);
2780 		return -EOPNOTSUPP;
2781 	}
2782 
2783 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2784 		struct flow_match_enc_keyid match;
2785 
2786 		flow_rule_match_enc_keyid(rule, &match);
2787 		if (match.mask->keyid != 0)
2788 			field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
2789 	}
2790 
2791 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
2792 		struct flow_match_basic match;
2793 
2794 		flow_rule_match_basic(rule, &match);
2795 		n_proto_key = ntohs(match.key->n_proto);
2796 		n_proto_mask = ntohs(match.mask->n_proto);
2797 
2798 		if (n_proto_key == ETH_P_ALL) {
2799 			n_proto_key = 0;
2800 			n_proto_mask = 0;
2801 		}
2802 		n_proto = n_proto_key & n_proto_mask;
2803 		if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2804 			return -EINVAL;
2805 		if (n_proto == ETH_P_IPV6) {
2806 			/* specify flow type as TCP IPv6 */
2807 			vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2808 		}
2809 
2810 		if (match.key->ip_proto != IPPROTO_TCP) {
2811 			dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2812 			return -EINVAL;
2813 		}
2814 	}
2815 
2816 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2817 		struct flow_match_eth_addrs match;
2818 
2819 		flow_rule_match_eth_addrs(rule, &match);
2820 
2821 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
2822 		if (!is_zero_ether_addr(match.mask->dst)) {
2823 			if (is_broadcast_ether_addr(match.mask->dst)) {
2824 				field_flags |= IAVF_CLOUD_FIELD_OMAC;
2825 			} else {
2826 				dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2827 					match.mask->dst);
2828 				return IAVF_ERR_CONFIG;
2829 			}
2830 		}
2831 
2832 		if (!is_zero_ether_addr(match.mask->src)) {
2833 			if (is_broadcast_ether_addr(match.mask->src)) {
2834 				field_flags |= IAVF_CLOUD_FIELD_IMAC;
2835 			} else {
2836 				dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2837 					match.mask->src);
2838 				return IAVF_ERR_CONFIG;
2839 			}
2840 		}
2841 
2842 		if (!is_zero_ether_addr(match.key->dst))
2843 			if (is_valid_ether_addr(match.key->dst) ||
2844 			    is_multicast_ether_addr(match.key->dst)) {
2845 				/* set the mask if a valid dst_mac address */
2846 				for (i = 0; i < ETH_ALEN; i++)
2847 					vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2848 				ether_addr_copy(vf->data.tcp_spec.dst_mac,
2849 						match.key->dst);
2850 			}
2851 
2852 		if (!is_zero_ether_addr(match.key->src))
2853 			if (is_valid_ether_addr(match.key->src) ||
2854 			    is_multicast_ether_addr(match.key->src)) {
2855 				/* set the mask if a valid dst_mac address */
2856 				for (i = 0; i < ETH_ALEN; i++)
2857 					vf->mask.tcp_spec.src_mac[i] |= 0xff;
2858 				ether_addr_copy(vf->data.tcp_spec.src_mac,
2859 						match.key->src);
2860 		}
2861 	}
2862 
2863 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
2864 		struct flow_match_vlan match;
2865 
2866 		flow_rule_match_vlan(rule, &match);
2867 		if (match.mask->vlan_id) {
2868 			if (match.mask->vlan_id == VLAN_VID_MASK) {
2869 				field_flags |= IAVF_CLOUD_FIELD_IVLAN;
2870 			} else {
2871 				dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2872 					match.mask->vlan_id);
2873 				return IAVF_ERR_CONFIG;
2874 			}
2875 		}
2876 		vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2877 		vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
2878 	}
2879 
2880 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
2881 		struct flow_match_control match;
2882 
2883 		flow_rule_match_control(rule, &match);
2884 		addr_type = match.key->addr_type;
2885 	}
2886 
2887 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2888 		struct flow_match_ipv4_addrs match;
2889 
2890 		flow_rule_match_ipv4_addrs(rule, &match);
2891 		if (match.mask->dst) {
2892 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
2893 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2894 			} else {
2895 				dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2896 					be32_to_cpu(match.mask->dst));
2897 				return IAVF_ERR_CONFIG;
2898 			}
2899 		}
2900 
2901 		if (match.mask->src) {
2902 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
2903 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2904 			} else {
2905 				dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2906 					be32_to_cpu(match.mask->dst));
2907 				return IAVF_ERR_CONFIG;
2908 			}
2909 		}
2910 
2911 		if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
2912 			dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2913 			return IAVF_ERR_CONFIG;
2914 		}
2915 		if (match.key->dst) {
2916 			vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2917 			vf->data.tcp_spec.dst_ip[0] = match.key->dst;
2918 		}
2919 		if (match.key->src) {
2920 			vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2921 			vf->data.tcp_spec.src_ip[0] = match.key->src;
2922 		}
2923 	}
2924 
2925 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2926 		struct flow_match_ipv6_addrs match;
2927 
2928 		flow_rule_match_ipv6_addrs(rule, &match);
2929 
2930 		/* validate mask, make sure it is not IPV6_ADDR_ANY */
2931 		if (ipv6_addr_any(&match.mask->dst)) {
2932 			dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2933 				IPV6_ADDR_ANY);
2934 			return IAVF_ERR_CONFIG;
2935 		}
2936 
2937 		/* src and dest IPv6 address should not be LOOPBACK
2938 		 * (0:0:0:0:0:0:0:1) which can be represented as ::1
2939 		 */
2940 		if (ipv6_addr_loopback(&match.key->dst) ||
2941 		    ipv6_addr_loopback(&match.key->src)) {
2942 			dev_err(&adapter->pdev->dev,
2943 				"ipv6 addr should not be loopback\n");
2944 			return IAVF_ERR_CONFIG;
2945 		}
2946 		if (!ipv6_addr_any(&match.mask->dst) ||
2947 		    !ipv6_addr_any(&match.mask->src))
2948 			field_flags |= IAVF_CLOUD_FIELD_IIP;
2949 
2950 		for (i = 0; i < 4; i++)
2951 			vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2952 		memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
2953 		       sizeof(vf->data.tcp_spec.dst_ip));
2954 		for (i = 0; i < 4; i++)
2955 			vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2956 		memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
2957 		       sizeof(vf->data.tcp_spec.src_ip));
2958 	}
2959 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
2960 		struct flow_match_ports match;
2961 
2962 		flow_rule_match_ports(rule, &match);
2963 		if (match.mask->src) {
2964 			if (match.mask->src == cpu_to_be16(0xffff)) {
2965 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2966 			} else {
2967 				dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
2968 					be16_to_cpu(match.mask->src));
2969 				return IAVF_ERR_CONFIG;
2970 			}
2971 		}
2972 
2973 		if (match.mask->dst) {
2974 			if (match.mask->dst == cpu_to_be16(0xffff)) {
2975 				field_flags |= IAVF_CLOUD_FIELD_IIP;
2976 			} else {
2977 				dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
2978 					be16_to_cpu(match.mask->dst));
2979 				return IAVF_ERR_CONFIG;
2980 			}
2981 		}
2982 		if (match.key->dst) {
2983 			vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
2984 			vf->data.tcp_spec.dst_port = match.key->dst;
2985 		}
2986 
2987 		if (match.key->src) {
2988 			vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
2989 			vf->data.tcp_spec.src_port = match.key->src;
2990 		}
2991 	}
2992 	vf->field_flags = field_flags;
2993 
2994 	return 0;
2995 }
2996 
2997 /**
2998  * iavf_handle_tclass - Forward to a traffic class on the device
2999  * @adapter: board private structure
3000  * @tc: traffic class index on the device
3001  * @filter: pointer to cloud filter structure
3002  */
iavf_handle_tclass(struct iavf_adapter * adapter,u32 tc,struct iavf_cloud_filter * filter)3003 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
3004 			      struct iavf_cloud_filter *filter)
3005 {
3006 	if (tc == 0)
3007 		return 0;
3008 	if (tc < adapter->num_tc) {
3009 		if (!filter->f.data.tcp_spec.dst_port) {
3010 			dev_err(&adapter->pdev->dev,
3011 				"Specify destination port to redirect to traffic class other than TC0\n");
3012 			return -EINVAL;
3013 		}
3014 	}
3015 	/* redirect to a traffic class on the same device */
3016 	filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
3017 	filter->f.action_meta = tc;
3018 	return 0;
3019 }
3020 
3021 /**
3022  * iavf_configure_clsflower - Add tc flower filters
3023  * @adapter: board private structure
3024  * @cls_flower: Pointer to struct flow_cls_offload
3025  */
iavf_configure_clsflower(struct iavf_adapter * adapter,struct flow_cls_offload * cls_flower)3026 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
3027 				    struct flow_cls_offload *cls_flower)
3028 {
3029 	int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
3030 	struct iavf_cloud_filter *filter = NULL;
3031 	int err = -EINVAL, count = 50;
3032 
3033 	if (tc < 0) {
3034 		dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
3035 		return -EINVAL;
3036 	}
3037 
3038 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
3039 	if (!filter)
3040 		return -ENOMEM;
3041 
3042 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3043 				&adapter->crit_section)) {
3044 		if (--count == 0)
3045 			goto err;
3046 		udelay(1);
3047 	}
3048 
3049 	filter->cookie = cls_flower->cookie;
3050 
3051 	/* set the mask to all zeroes to begin with */
3052 	memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
3053 	/* start out with flow type and eth type IPv4 to begin with */
3054 	filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
3055 	err = iavf_parse_cls_flower(adapter, cls_flower, filter);
3056 	if (err)
3057 		goto err;
3058 
3059 	err = iavf_handle_tclass(adapter, tc, filter);
3060 	if (err)
3061 		goto err;
3062 
3063 	/* add filter to the list */
3064 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3065 	list_add_tail(&filter->list, &adapter->cloud_filter_list);
3066 	adapter->num_cloud_filters++;
3067 	filter->add = true;
3068 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3069 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3070 err:
3071 	if (err)
3072 		kfree(filter);
3073 
3074 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3075 	return err;
3076 }
3077 
3078 /* iavf_find_cf - Find the cloud filter in the list
3079  * @adapter: Board private structure
3080  * @cookie: filter specific cookie
3081  *
3082  * Returns ptr to the filter object or NULL. Must be called while holding the
3083  * cloud_filter_list_lock.
3084  */
iavf_find_cf(struct iavf_adapter * adapter,unsigned long * cookie)3085 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
3086 					      unsigned long *cookie)
3087 {
3088 	struct iavf_cloud_filter *filter = NULL;
3089 
3090 	if (!cookie)
3091 		return NULL;
3092 
3093 	list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
3094 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
3095 			return filter;
3096 	}
3097 	return NULL;
3098 }
3099 
3100 /**
3101  * iavf_delete_clsflower - Remove tc flower filters
3102  * @adapter: board private structure
3103  * @cls_flower: Pointer to struct flow_cls_offload
3104  */
iavf_delete_clsflower(struct iavf_adapter * adapter,struct flow_cls_offload * cls_flower)3105 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
3106 				 struct flow_cls_offload *cls_flower)
3107 {
3108 	struct iavf_cloud_filter *filter = NULL;
3109 	int err = 0;
3110 
3111 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3112 	filter = iavf_find_cf(adapter, &cls_flower->cookie);
3113 	if (filter) {
3114 		filter->del = true;
3115 		adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
3116 	} else {
3117 		err = -EINVAL;
3118 	}
3119 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
3120 
3121 	return err;
3122 }
3123 
3124 /**
3125  * iavf_setup_tc_cls_flower - flower classifier offloads
3126  * @adapter: board private structure
3127  * @cls_flower: pointer to flow_cls_offload struct with flow info
3128  */
iavf_setup_tc_cls_flower(struct iavf_adapter * adapter,struct flow_cls_offload * cls_flower)3129 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
3130 				    struct flow_cls_offload *cls_flower)
3131 {
3132 	switch (cls_flower->command) {
3133 	case FLOW_CLS_REPLACE:
3134 		return iavf_configure_clsflower(adapter, cls_flower);
3135 	case FLOW_CLS_DESTROY:
3136 		return iavf_delete_clsflower(adapter, cls_flower);
3137 	case FLOW_CLS_STATS:
3138 		return -EOPNOTSUPP;
3139 	default:
3140 		return -EOPNOTSUPP;
3141 	}
3142 }
3143 
3144 /**
3145  * iavf_setup_tc_block_cb - block callback for tc
3146  * @type: type of offload
3147  * @type_data: offload data
3148  * @cb_priv:
3149  *
3150  * This function is the block callback for traffic classes
3151  **/
iavf_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)3152 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3153 				  void *cb_priv)
3154 {
3155 	struct iavf_adapter *adapter = cb_priv;
3156 
3157 	if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
3158 		return -EOPNOTSUPP;
3159 
3160 	switch (type) {
3161 	case TC_SETUP_CLSFLOWER:
3162 		return iavf_setup_tc_cls_flower(cb_priv, type_data);
3163 	default:
3164 		return -EOPNOTSUPP;
3165 	}
3166 }
3167 
3168 static LIST_HEAD(iavf_block_cb_list);
3169 
3170 /**
3171  * iavf_setup_tc - configure multiple traffic classes
3172  * @netdev: network interface device structure
3173  * @type: type of offload
3174  * @type_data: tc offload data
3175  *
3176  * This function is the callback to ndo_setup_tc in the
3177  * netdev_ops.
3178  *
3179  * Returns 0 on success
3180  **/
iavf_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)3181 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
3182 			 void *type_data)
3183 {
3184 	struct iavf_adapter *adapter = netdev_priv(netdev);
3185 
3186 	switch (type) {
3187 	case TC_SETUP_QDISC_MQPRIO:
3188 		return __iavf_setup_tc(netdev, type_data);
3189 	case TC_SETUP_BLOCK:
3190 		return flow_block_cb_setup_simple(type_data,
3191 						  &iavf_block_cb_list,
3192 						  iavf_setup_tc_block_cb,
3193 						  adapter, adapter, true);
3194 	default:
3195 		return -EOPNOTSUPP;
3196 	}
3197 }
3198 
3199 /**
3200  * iavf_open - Called when a network interface is made active
3201  * @netdev: network interface device structure
3202  *
3203  * Returns 0 on success, negative value on failure
3204  *
3205  * The open entry point is called when a network interface is made
3206  * active by the system (IFF_UP).  At this point all resources needed
3207  * for transmit and receive operations are allocated, the interrupt
3208  * handler is registered with the OS, the watchdog is started,
3209  * and the stack is notified that the interface is ready.
3210  **/
iavf_open(struct net_device * netdev)3211 static int iavf_open(struct net_device *netdev)
3212 {
3213 	struct iavf_adapter *adapter = netdev_priv(netdev);
3214 	int err;
3215 
3216 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
3217 		dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
3218 		return -EIO;
3219 	}
3220 
3221 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3222 				&adapter->crit_section))
3223 		usleep_range(500, 1000);
3224 
3225 	if (adapter->state != __IAVF_DOWN) {
3226 		err = -EBUSY;
3227 		goto err_unlock;
3228 	}
3229 
3230 	/* allocate transmit descriptors */
3231 	err = iavf_setup_all_tx_resources(adapter);
3232 	if (err)
3233 		goto err_setup_tx;
3234 
3235 	/* allocate receive descriptors */
3236 	err = iavf_setup_all_rx_resources(adapter);
3237 	if (err)
3238 		goto err_setup_rx;
3239 
3240 	/* clear any pending interrupts, may auto mask */
3241 	err = iavf_request_traffic_irqs(adapter, netdev->name);
3242 	if (err)
3243 		goto err_req_irq;
3244 
3245 	spin_lock_bh(&adapter->mac_vlan_list_lock);
3246 
3247 	iavf_add_filter(adapter, adapter->hw.mac.addr);
3248 
3249 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3250 
3251 	iavf_configure(adapter);
3252 
3253 	iavf_up_complete(adapter);
3254 
3255 	iavf_irq_enable(adapter, true);
3256 
3257 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3258 
3259 	return 0;
3260 
3261 err_req_irq:
3262 	iavf_down(adapter);
3263 	iavf_free_traffic_irqs(adapter);
3264 err_setup_rx:
3265 	iavf_free_all_rx_resources(adapter);
3266 err_setup_tx:
3267 	iavf_free_all_tx_resources(adapter);
3268 err_unlock:
3269 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3270 
3271 	return err;
3272 }
3273 
3274 /**
3275  * iavf_close - Disables a network interface
3276  * @netdev: network interface device structure
3277  *
3278  * Returns 0, this is not allowed to fail
3279  *
3280  * The close entry point is called when an interface is de-activated
3281  * by the OS.  The hardware is still under the drivers control, but
3282  * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3283  * are freed, along with all transmit and receive resources.
3284  **/
iavf_close(struct net_device * netdev)3285 static int iavf_close(struct net_device *netdev)
3286 {
3287 	struct iavf_adapter *adapter = netdev_priv(netdev);
3288 	int status;
3289 
3290 	if (adapter->state <= __IAVF_DOWN_PENDING)
3291 		return 0;
3292 
3293 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3294 				&adapter->crit_section))
3295 		usleep_range(500, 1000);
3296 
3297 	set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3298 	if (CLIENT_ENABLED(adapter))
3299 		adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3300 
3301 	iavf_down(adapter);
3302 	iavf_change_state(adapter, __IAVF_DOWN_PENDING);
3303 	iavf_free_traffic_irqs(adapter);
3304 
3305 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3306 
3307 	/* We explicitly don't free resources here because the hardware is
3308 	 * still active and can DMA into memory. Resources are cleared in
3309 	 * iavf_virtchnl_completion() after we get confirmation from the PF
3310 	 * driver that the rings have been stopped.
3311 	 *
3312 	 * Also, we wait for state to transition to __IAVF_DOWN before
3313 	 * returning. State change occurs in iavf_virtchnl_completion() after
3314 	 * VF resources are released (which occurs after PF driver processes and
3315 	 * responds to admin queue commands).
3316 	 */
3317 
3318 	status = wait_event_timeout(adapter->down_waitqueue,
3319 				    adapter->state == __IAVF_DOWN,
3320 				    msecs_to_jiffies(500));
3321 	if (!status)
3322 		netdev_warn(netdev, "Device resources not yet released\n");
3323 	return 0;
3324 }
3325 
3326 /**
3327  * iavf_change_mtu - Change the Maximum Transfer Unit
3328  * @netdev: network interface device structure
3329  * @new_mtu: new value for maximum frame size
3330  *
3331  * Returns 0 on success, negative on failure
3332  **/
iavf_change_mtu(struct net_device * netdev,int new_mtu)3333 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
3334 {
3335 	struct iavf_adapter *adapter = netdev_priv(netdev);
3336 
3337 	netdev->mtu = new_mtu;
3338 	if (CLIENT_ENABLED(adapter)) {
3339 		iavf_notify_client_l2_params(&adapter->vsi);
3340 		adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3341 	}
3342 
3343 	if (netif_running(netdev)) {
3344 		adapter->flags |= IAVF_FLAG_RESET_NEEDED;
3345 		queue_work(iavf_wq, &adapter->reset_task);
3346 	}
3347 
3348 	return 0;
3349 }
3350 
3351 /**
3352  * iavf_set_features - set the netdev feature flags
3353  * @netdev: ptr to the netdev being adjusted
3354  * @features: the feature set that the stack is suggesting
3355  * Note: expects to be called while under rtnl_lock()
3356  **/
iavf_set_features(struct net_device * netdev,netdev_features_t features)3357 static int iavf_set_features(struct net_device *netdev,
3358 			     netdev_features_t features)
3359 {
3360 	struct iavf_adapter *adapter = netdev_priv(netdev);
3361 
3362 	/* Don't allow changing VLAN_RX flag when adapter is not capable
3363 	 * of VLAN offload
3364 	 */
3365 	if (!VLAN_ALLOWED(adapter)) {
3366 		if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX)
3367 			return -EINVAL;
3368 	} else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3369 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
3370 			adapter->aq_required |=
3371 				IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3372 		else
3373 			adapter->aq_required |=
3374 				IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3375 	}
3376 
3377 	return 0;
3378 }
3379 
3380 /**
3381  * iavf_features_check - Validate encapsulated packet conforms to limits
3382  * @skb: skb buff
3383  * @dev: This physical port's netdev
3384  * @features: Offload features that the stack believes apply
3385  **/
iavf_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)3386 static netdev_features_t iavf_features_check(struct sk_buff *skb,
3387 					     struct net_device *dev,
3388 					     netdev_features_t features)
3389 {
3390 	size_t len;
3391 
3392 	/* No point in doing any of this if neither checksum nor GSO are
3393 	 * being requested for this frame.  We can rule out both by just
3394 	 * checking for CHECKSUM_PARTIAL
3395 	 */
3396 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3397 		return features;
3398 
3399 	/* We cannot support GSO if the MSS is going to be less than
3400 	 * 64 bytes.  If it is then we need to drop support for GSO.
3401 	 */
3402 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3403 		features &= ~NETIF_F_GSO_MASK;
3404 
3405 	/* MACLEN can support at most 63 words */
3406 	len = skb_network_header(skb) - skb->data;
3407 	if (len & ~(63 * 2))
3408 		goto out_err;
3409 
3410 	/* IPLEN and EIPLEN can support at most 127 dwords */
3411 	len = skb_transport_header(skb) - skb_network_header(skb);
3412 	if (len & ~(127 * 4))
3413 		goto out_err;
3414 
3415 	if (skb->encapsulation) {
3416 		/* L4TUNLEN can support 127 words */
3417 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
3418 		if (len & ~(127 * 2))
3419 			goto out_err;
3420 
3421 		/* IPLEN can support at most 127 dwords */
3422 		len = skb_inner_transport_header(skb) -
3423 		      skb_inner_network_header(skb);
3424 		if (len & ~(127 * 4))
3425 			goto out_err;
3426 	}
3427 
3428 	/* No need to validate L4LEN as TCP is the only protocol with a
3429 	 * a flexible value and we support all possible values supported
3430 	 * by TCP, which is at most 15 dwords
3431 	 */
3432 
3433 	return features;
3434 out_err:
3435 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3436 }
3437 
3438 /**
3439  * iavf_fix_features - fix up the netdev feature bits
3440  * @netdev: our net device
3441  * @features: desired feature bits
3442  *
3443  * Returns fixed-up features bits
3444  **/
iavf_fix_features(struct net_device * netdev,netdev_features_t features)3445 static netdev_features_t iavf_fix_features(struct net_device *netdev,
3446 					   netdev_features_t features)
3447 {
3448 	struct iavf_adapter *adapter = netdev_priv(netdev);
3449 
3450 	if (adapter->vf_res &&
3451 	    !(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3452 		features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3453 			      NETIF_F_HW_VLAN_CTAG_RX |
3454 			      NETIF_F_HW_VLAN_CTAG_FILTER);
3455 
3456 	return features;
3457 }
3458 
3459 static const struct net_device_ops iavf_netdev_ops = {
3460 	.ndo_open		= iavf_open,
3461 	.ndo_stop		= iavf_close,
3462 	.ndo_start_xmit		= iavf_xmit_frame,
3463 	.ndo_set_rx_mode	= iavf_set_rx_mode,
3464 	.ndo_validate_addr	= eth_validate_addr,
3465 	.ndo_set_mac_address	= iavf_set_mac,
3466 	.ndo_change_mtu		= iavf_change_mtu,
3467 	.ndo_tx_timeout		= iavf_tx_timeout,
3468 	.ndo_vlan_rx_add_vid	= iavf_vlan_rx_add_vid,
3469 	.ndo_vlan_rx_kill_vid	= iavf_vlan_rx_kill_vid,
3470 	.ndo_features_check	= iavf_features_check,
3471 	.ndo_fix_features	= iavf_fix_features,
3472 	.ndo_set_features	= iavf_set_features,
3473 	.ndo_setup_tc		= iavf_setup_tc,
3474 };
3475 
3476 /**
3477  * iavf_check_reset_complete - check that VF reset is complete
3478  * @hw: pointer to hw struct
3479  *
3480  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3481  **/
iavf_check_reset_complete(struct iavf_hw * hw)3482 static int iavf_check_reset_complete(struct iavf_hw *hw)
3483 {
3484 	u32 rstat;
3485 	int i;
3486 
3487 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3488 		rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
3489 			     IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3490 		if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3491 		    (rstat == VIRTCHNL_VFR_COMPLETED))
3492 			return 0;
3493 		usleep_range(10, 20);
3494 	}
3495 	return -EBUSY;
3496 }
3497 
3498 /**
3499  * iavf_process_config - Process the config information we got from the PF
3500  * @adapter: board private structure
3501  *
3502  * Verify that we have a valid config struct, and set up our netdev features
3503  * and our VSI struct.
3504  **/
iavf_process_config(struct iavf_adapter * adapter)3505 int iavf_process_config(struct iavf_adapter *adapter)
3506 {
3507 	struct virtchnl_vf_resource *vfres = adapter->vf_res;
3508 	int i, num_req_queues = adapter->num_req_queues;
3509 	struct net_device *netdev = adapter->netdev;
3510 	struct iavf_vsi *vsi = &adapter->vsi;
3511 	netdev_features_t hw_enc_features;
3512 	netdev_features_t hw_features;
3513 
3514 	/* got VF config message back from PF, now we can parse it */
3515 	for (i = 0; i < vfres->num_vsis; i++) {
3516 		if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3517 			adapter->vsi_res = &vfres->vsi_res[i];
3518 	}
3519 	if (!adapter->vsi_res) {
3520 		dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3521 		return -ENODEV;
3522 	}
3523 
3524 	if (num_req_queues &&
3525 	    num_req_queues > adapter->vsi_res->num_queue_pairs) {
3526 		/* Problem.  The PF gave us fewer queues than what we had
3527 		 * negotiated in our request.  Need a reset to see if we can't
3528 		 * get back to a working state.
3529 		 */
3530 		dev_err(&adapter->pdev->dev,
3531 			"Requested %d queues, but PF only gave us %d.\n",
3532 			num_req_queues,
3533 			adapter->vsi_res->num_queue_pairs);
3534 		adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
3535 		adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3536 		iavf_schedule_reset(adapter);
3537 		return -ENODEV;
3538 	}
3539 	adapter->num_req_queues = 0;
3540 
3541 	hw_enc_features = NETIF_F_SG			|
3542 			  NETIF_F_IP_CSUM		|
3543 			  NETIF_F_IPV6_CSUM		|
3544 			  NETIF_F_HIGHDMA		|
3545 			  NETIF_F_SOFT_FEATURES	|
3546 			  NETIF_F_TSO			|
3547 			  NETIF_F_TSO_ECN		|
3548 			  NETIF_F_TSO6			|
3549 			  NETIF_F_SCTP_CRC		|
3550 			  NETIF_F_RXHASH		|
3551 			  NETIF_F_RXCSUM		|
3552 			  0;
3553 
3554 	/* advertise to stack only if offloads for encapsulated packets is
3555 	 * supported
3556 	 */
3557 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3558 		hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL	|
3559 				   NETIF_F_GSO_GRE		|
3560 				   NETIF_F_GSO_GRE_CSUM		|
3561 				   NETIF_F_GSO_IPXIP4		|
3562 				   NETIF_F_GSO_IPXIP6		|
3563 				   NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3564 				   NETIF_F_GSO_PARTIAL		|
3565 				   0;
3566 
3567 		if (!(vfres->vf_cap_flags &
3568 		      VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3569 			netdev->gso_partial_features |=
3570 				NETIF_F_GSO_UDP_TUNNEL_CSUM;
3571 
3572 		netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3573 		netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3574 		netdev->hw_enc_features |= hw_enc_features;
3575 	}
3576 	/* record features VLANs can make use of */
3577 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3578 
3579 	/* Write features and hw_features separately to avoid polluting
3580 	 * with, or dropping, features that are set when we registered.
3581 	 */
3582 	hw_features = hw_enc_features;
3583 
3584 	/* Enable VLAN features if supported */
3585 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3586 		hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3587 				NETIF_F_HW_VLAN_CTAG_RX);
3588 	/* Enable cloud filter if ADQ is supported */
3589 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3590 		hw_features |= NETIF_F_HW_TC;
3591 
3592 	netdev->hw_features |= hw_features;
3593 
3594 	netdev->features |= hw_features;
3595 
3596 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3597 		netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3598 
3599 	netdev->priv_flags |= IFF_UNICAST_FLT;
3600 
3601 	/* Do not turn on offloads when they are requested to be turned off.
3602 	 * TSO needs minimum 576 bytes to work correctly.
3603 	 */
3604 	if (netdev->wanted_features) {
3605 		if (!(netdev->wanted_features & NETIF_F_TSO) ||
3606 		    netdev->mtu < 576)
3607 			netdev->features &= ~NETIF_F_TSO;
3608 		if (!(netdev->wanted_features & NETIF_F_TSO6) ||
3609 		    netdev->mtu < 576)
3610 			netdev->features &= ~NETIF_F_TSO6;
3611 		if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
3612 			netdev->features &= ~NETIF_F_TSO_ECN;
3613 		if (!(netdev->wanted_features & NETIF_F_GRO))
3614 			netdev->features &= ~NETIF_F_GRO;
3615 		if (!(netdev->wanted_features & NETIF_F_GSO))
3616 			netdev->features &= ~NETIF_F_GSO;
3617 	}
3618 
3619 	adapter->vsi.id = adapter->vsi_res->vsi_id;
3620 
3621 	adapter->vsi.back = adapter;
3622 	adapter->vsi.base_vector = 1;
3623 	adapter->vsi.work_limit = IAVF_DEFAULT_IRQ_WORK;
3624 	vsi->netdev = adapter->netdev;
3625 	vsi->qs_handle = adapter->vsi_res->qset_handle;
3626 	if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3627 		adapter->rss_key_size = vfres->rss_key_size;
3628 		adapter->rss_lut_size = vfres->rss_lut_size;
3629 	} else {
3630 		adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
3631 		adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
3632 	}
3633 
3634 	return 0;
3635 }
3636 
3637 /**
3638  * iavf_init_task - worker thread to perform delayed initialization
3639  * @work: pointer to work_struct containing our data
3640  *
3641  * This task completes the work that was begun in probe. Due to the nature
3642  * of VF-PF communications, we may need to wait tens of milliseconds to get
3643  * responses back from the PF. Rather than busy-wait in probe and bog down the
3644  * whole system, we'll do it in a task so we can sleep.
3645  * This task only runs during driver init. Once we've established
3646  * communications with the PF driver and set up our netdev, the watchdog
3647  * takes over.
3648  **/
iavf_init_task(struct work_struct * work)3649 static void iavf_init_task(struct work_struct *work)
3650 {
3651 	struct iavf_adapter *adapter = container_of(work,
3652 						    struct iavf_adapter,
3653 						    init_task.work);
3654 	struct iavf_hw *hw = &adapter->hw;
3655 
3656 	if (iavf_lock_timeout(adapter, __IAVF_IN_CRITICAL_TASK, 5000)) {
3657 		dev_warn(&adapter->pdev->dev, "failed to set __IAVF_IN_CRITICAL_TASK in %s\n", __FUNCTION__);
3658 		return;
3659 	}
3660 	switch (adapter->state) {
3661 	case __IAVF_STARTUP:
3662 		if (iavf_startup(adapter) < 0)
3663 			goto init_failed;
3664 		break;
3665 	case __IAVF_INIT_VERSION_CHECK:
3666 		if (iavf_init_version_check(adapter) < 0)
3667 			goto init_failed;
3668 		break;
3669 	case __IAVF_INIT_GET_RESOURCES:
3670 		if (iavf_init_get_resources(adapter) < 0)
3671 			goto init_failed;
3672 		goto out;
3673 	default:
3674 		goto init_failed;
3675 	}
3676 
3677 	queue_delayed_work(iavf_wq, &adapter->init_task,
3678 			   msecs_to_jiffies(30));
3679 	goto out;
3680 init_failed:
3681 	if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
3682 		dev_err(&adapter->pdev->dev,
3683 			"Failed to communicate with PF; waiting before retry\n");
3684 		adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
3685 		iavf_shutdown_adminq(hw);
3686 		iavf_change_state(adapter, __IAVF_STARTUP);
3687 		queue_delayed_work(iavf_wq, &adapter->init_task, HZ * 5);
3688 		goto out;
3689 	}
3690 	queue_delayed_work(iavf_wq, &adapter->init_task, HZ);
3691 out:
3692 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3693 }
3694 
3695 /**
3696  * iavf_shutdown - Shutdown the device in preparation for a reboot
3697  * @pdev: pci device structure
3698  **/
iavf_shutdown(struct pci_dev * pdev)3699 static void iavf_shutdown(struct pci_dev *pdev)
3700 {
3701 	struct net_device *netdev = pci_get_drvdata(pdev);
3702 	struct iavf_adapter *adapter = netdev_priv(netdev);
3703 
3704 	netif_device_detach(netdev);
3705 
3706 	if (netif_running(netdev))
3707 		iavf_close(netdev);
3708 
3709 	if (iavf_lock_timeout(adapter, __IAVF_IN_CRITICAL_TASK, 5000))
3710 		dev_warn(&adapter->pdev->dev, "failed to set __IAVF_IN_CRITICAL_TASK in %s\n", __FUNCTION__);
3711 	/* Prevent the watchdog from running. */
3712 	iavf_change_state(adapter, __IAVF_REMOVE);
3713 	adapter->aq_required = 0;
3714 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3715 
3716 #ifdef CONFIG_PM
3717 	pci_save_state(pdev);
3718 
3719 #endif
3720 	pci_disable_device(pdev);
3721 }
3722 
3723 /**
3724  * iavf_probe - Device Initialization Routine
3725  * @pdev: PCI device information struct
3726  * @ent: entry in iavf_pci_tbl
3727  *
3728  * Returns 0 on success, negative on failure
3729  *
3730  * iavf_probe initializes an adapter identified by a pci_dev structure.
3731  * The OS initialization, configuring of the adapter private structure,
3732  * and a hardware reset occur.
3733  **/
iavf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)3734 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3735 {
3736 	struct net_device *netdev;
3737 	struct iavf_adapter *adapter = NULL;
3738 	struct iavf_hw *hw = NULL;
3739 	int err;
3740 
3741 	err = pci_enable_device(pdev);
3742 	if (err)
3743 		return err;
3744 
3745 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3746 	if (err) {
3747 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3748 		if (err) {
3749 			dev_err(&pdev->dev,
3750 				"DMA configuration failed: 0x%x\n", err);
3751 			goto err_dma;
3752 		}
3753 	}
3754 
3755 	err = pci_request_regions(pdev, iavf_driver_name);
3756 	if (err) {
3757 		dev_err(&pdev->dev,
3758 			"pci_request_regions failed 0x%x\n", err);
3759 		goto err_pci_reg;
3760 	}
3761 
3762 	pci_enable_pcie_error_reporting(pdev);
3763 
3764 	pci_set_master(pdev);
3765 
3766 	netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
3767 				   IAVF_MAX_REQ_QUEUES);
3768 	if (!netdev) {
3769 		err = -ENOMEM;
3770 		goto err_alloc_etherdev;
3771 	}
3772 
3773 	SET_NETDEV_DEV(netdev, &pdev->dev);
3774 
3775 	pci_set_drvdata(pdev, netdev);
3776 	adapter = netdev_priv(netdev);
3777 
3778 	adapter->netdev = netdev;
3779 	adapter->pdev = pdev;
3780 
3781 	hw = &adapter->hw;
3782 	hw->back = adapter;
3783 
3784 	adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3785 	iavf_change_state(adapter, __IAVF_STARTUP);
3786 
3787 	/* Call save state here because it relies on the adapter struct. */
3788 	pci_save_state(pdev);
3789 
3790 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3791 			      pci_resource_len(pdev, 0));
3792 	if (!hw->hw_addr) {
3793 		err = -EIO;
3794 		goto err_ioremap;
3795 	}
3796 	hw->vendor_id = pdev->vendor;
3797 	hw->device_id = pdev->device;
3798 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3799 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
3800 	hw->subsystem_device_id = pdev->subsystem_device;
3801 	hw->bus.device = PCI_SLOT(pdev->devfn);
3802 	hw->bus.func = PCI_FUNC(pdev->devfn);
3803 	hw->bus.bus_id = pdev->bus->number;
3804 
3805 	/* set up the locks for the AQ, do this only once in probe
3806 	 * and destroy them only once in remove
3807 	 */
3808 	mutex_init(&hw->aq.asq_mutex);
3809 	mutex_init(&hw->aq.arq_mutex);
3810 
3811 	spin_lock_init(&adapter->mac_vlan_list_lock);
3812 	spin_lock_init(&adapter->cloud_filter_list_lock);
3813 
3814 	INIT_LIST_HEAD(&adapter->mac_filter_list);
3815 	INIT_LIST_HEAD(&adapter->vlan_filter_list);
3816 	INIT_LIST_HEAD(&adapter->cloud_filter_list);
3817 
3818 	INIT_WORK(&adapter->reset_task, iavf_reset_task);
3819 	INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
3820 	INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
3821 	INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
3822 	INIT_DELAYED_WORK(&adapter->init_task, iavf_init_task);
3823 	queue_delayed_work(iavf_wq, &adapter->init_task,
3824 			   msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3825 
3826 	/* Setup the wait queue for indicating transition to down status */
3827 	init_waitqueue_head(&adapter->down_waitqueue);
3828 
3829 	return 0;
3830 
3831 err_ioremap:
3832 	free_netdev(netdev);
3833 err_alloc_etherdev:
3834 	pci_disable_pcie_error_reporting(pdev);
3835 	pci_release_regions(pdev);
3836 err_pci_reg:
3837 err_dma:
3838 	pci_disable_device(pdev);
3839 	return err;
3840 }
3841 
3842 /**
3843  * iavf_suspend - Power management suspend routine
3844  * @dev_d: device info pointer
3845  *
3846  * Called when the system (VM) is entering sleep/suspend.
3847  **/
iavf_suspend(struct device * dev_d)3848 static int __maybe_unused iavf_suspend(struct device *dev_d)
3849 {
3850 	struct net_device *netdev = dev_get_drvdata(dev_d);
3851 	struct iavf_adapter *adapter = netdev_priv(netdev);
3852 
3853 	netif_device_detach(netdev);
3854 
3855 	while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3856 				&adapter->crit_section))
3857 		usleep_range(500, 1000);
3858 
3859 	if (netif_running(netdev)) {
3860 		rtnl_lock();
3861 		iavf_down(adapter);
3862 		rtnl_unlock();
3863 	}
3864 	iavf_free_misc_irq(adapter);
3865 	iavf_reset_interrupt_capability(adapter);
3866 
3867 	clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3868 
3869 	return 0;
3870 }
3871 
3872 /**
3873  * iavf_resume - Power management resume routine
3874  * @dev_d: device info pointer
3875  *
3876  * Called when the system (VM) is resumed from sleep/suspend.
3877  **/
iavf_resume(struct device * dev_d)3878 static int __maybe_unused iavf_resume(struct device *dev_d)
3879 {
3880 	struct pci_dev *pdev = to_pci_dev(dev_d);
3881 	struct net_device *netdev = pci_get_drvdata(pdev);
3882 	struct iavf_adapter *adapter = netdev_priv(netdev);
3883 	u32 err;
3884 
3885 	pci_set_master(pdev);
3886 
3887 	rtnl_lock();
3888 	err = iavf_set_interrupt_capability(adapter);
3889 	if (err) {
3890 		rtnl_unlock();
3891 		dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3892 		return err;
3893 	}
3894 	err = iavf_request_misc_irq(adapter);
3895 	rtnl_unlock();
3896 	if (err) {
3897 		dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3898 		return err;
3899 	}
3900 
3901 	queue_work(iavf_wq, &adapter->reset_task);
3902 
3903 	netif_device_attach(netdev);
3904 
3905 	return err;
3906 }
3907 
3908 /**
3909  * iavf_remove - Device Removal Routine
3910  * @pdev: PCI device information struct
3911  *
3912  * iavf_remove is called by the PCI subsystem to alert the driver
3913  * that it should release a PCI device.  The could be caused by a
3914  * Hot-Plug event, or because the driver is going to be removed from
3915  * memory.
3916  **/
iavf_remove(struct pci_dev * pdev)3917 static void iavf_remove(struct pci_dev *pdev)
3918 {
3919 	struct net_device *netdev = pci_get_drvdata(pdev);
3920 	struct iavf_adapter *adapter = netdev_priv(netdev);
3921 	struct iavf_vlan_filter *vlf, *vlftmp;
3922 	struct iavf_mac_filter *f, *ftmp;
3923 	struct iavf_cloud_filter *cf, *cftmp;
3924 	struct iavf_hw *hw = &adapter->hw;
3925 	int err;
3926 	/* Indicate we are in remove and not to run reset_task */
3927 	set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section);
3928 	cancel_delayed_work_sync(&adapter->init_task);
3929 	cancel_work_sync(&adapter->reset_task);
3930 	cancel_delayed_work_sync(&adapter->client_task);
3931 	if (adapter->netdev_registered) {
3932 		unregister_netdev(netdev);
3933 		adapter->netdev_registered = false;
3934 	}
3935 	if (CLIENT_ALLOWED(adapter)) {
3936 		err = iavf_lan_del_device(adapter);
3937 		if (err)
3938 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3939 				 err);
3940 	}
3941 
3942 	iavf_request_reset(adapter);
3943 	msleep(50);
3944 	/* If the FW isn't responding, kick it once, but only once. */
3945 	if (!iavf_asq_done(hw)) {
3946 		iavf_request_reset(adapter);
3947 		msleep(50);
3948 	}
3949 	if (iavf_lock_timeout(adapter, __IAVF_IN_CRITICAL_TASK, 5000))
3950 		dev_warn(&adapter->pdev->dev, "failed to set __IAVF_IN_CRITICAL_TASK in %s\n", __FUNCTION__);
3951 
3952 	/* Shut down all the garbage mashers on the detention level */
3953 	iavf_change_state(adapter, __IAVF_REMOVE);
3954 	adapter->aq_required = 0;
3955 	adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3956 	iavf_free_all_tx_resources(adapter);
3957 	iavf_free_all_rx_resources(adapter);
3958 	iavf_misc_irq_disable(adapter);
3959 	iavf_free_misc_irq(adapter);
3960 	iavf_reset_interrupt_capability(adapter);
3961 	iavf_free_q_vectors(adapter);
3962 
3963 	cancel_delayed_work_sync(&adapter->watchdog_task);
3964 
3965 	cancel_work_sync(&adapter->adminq_task);
3966 
3967 	iavf_free_rss(adapter);
3968 
3969 	if (hw->aq.asq.count)
3970 		iavf_shutdown_adminq(hw);
3971 
3972 	/* destroy the locks only once, here */
3973 	mutex_destroy(&hw->aq.arq_mutex);
3974 	mutex_destroy(&hw->aq.asq_mutex);
3975 
3976 	iounmap(hw->hw_addr);
3977 	pci_release_regions(pdev);
3978 	iavf_free_queues(adapter);
3979 	kfree(adapter->vf_res);
3980 	spin_lock_bh(&adapter->mac_vlan_list_lock);
3981 	/* If we got removed before an up/down sequence, we've got a filter
3982 	 * hanging out there that we need to get rid of.
3983 	 */
3984 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3985 		list_del(&f->list);
3986 		kfree(f);
3987 	}
3988 	list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3989 				 list) {
3990 		list_del(&vlf->list);
3991 		kfree(vlf);
3992 	}
3993 
3994 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
3995 
3996 	spin_lock_bh(&adapter->cloud_filter_list_lock);
3997 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3998 		list_del(&cf->list);
3999 		kfree(cf);
4000 	}
4001 	spin_unlock_bh(&adapter->cloud_filter_list_lock);
4002 
4003 	free_netdev(netdev);
4004 
4005 	pci_disable_pcie_error_reporting(pdev);
4006 
4007 	pci_disable_device(pdev);
4008 }
4009 
4010 static SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
4011 
4012 static struct pci_driver iavf_driver = {
4013 	.name      = iavf_driver_name,
4014 	.id_table  = iavf_pci_tbl,
4015 	.probe     = iavf_probe,
4016 	.remove    = iavf_remove,
4017 	.driver.pm = &iavf_pm_ops,
4018 	.shutdown  = iavf_shutdown,
4019 };
4020 
4021 /**
4022  * iavf_init_module - Driver Registration Routine
4023  *
4024  * iavf_init_module is the first routine called when the driver is
4025  * loaded. All it does is register with the PCI subsystem.
4026  **/
iavf_init_module(void)4027 static int __init iavf_init_module(void)
4028 {
4029 	int ret;
4030 
4031 	pr_info("iavf: %s\n", iavf_driver_string);
4032 
4033 	pr_info("%s\n", iavf_copyright);
4034 
4035 	iavf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
4036 				  iavf_driver_name);
4037 	if (!iavf_wq) {
4038 		pr_err("%s: Failed to create workqueue\n", iavf_driver_name);
4039 		return -ENOMEM;
4040 	}
4041 
4042 	ret = pci_register_driver(&iavf_driver);
4043 	if (ret)
4044 		destroy_workqueue(iavf_wq);
4045 
4046 	return ret;
4047 }
4048 
4049 module_init(iavf_init_module);
4050 
4051 /**
4052  * iavf_exit_module - Driver Exit Cleanup Routine
4053  *
4054  * iavf_exit_module is called just before the driver is removed
4055  * from memory.
4056  **/
iavf_exit_module(void)4057 static void __exit iavf_exit_module(void)
4058 {
4059 	pci_unregister_driver(&iavf_driver);
4060 	destroy_workqueue(iavf_wq);
4061 }
4062 
4063 module_exit(iavf_exit_module);
4064 
4065 /* iavf_main.c */
4066