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