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