1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
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
11 #define CREATE_TRACE_POINTS
12 #include "iavf_trace.h"
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);
20 char iavf_driver_name[] = "iavf";
21 static const char iavf_driver_string[] =
22 "Intel(R) Ethernet Adaptive Virtual Function Network Driver";
24 static const char iavf_copyright[] =
25 "Copyright (c) 2013 - 2018 Intel Corporation.";
27 /* iavf_pci_tbl - PCI Device ID Table
29 * Wildcard entries (PCI_ANY_ID) should come last
30 * Last entry must be all 0s
32 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
33 * Class, Class Mask, private data (not used) }
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 */
44 MODULE_DEVICE_TABLE(pci, iavf_pci_tbl);
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");
51 static const struct net_device_ops iavf_netdev_ops;
52 struct workqueue_struct *iavf_wq;
54 int iavf_status_to_errno(enum iavf_status status)
60 case IAVF_ERR_MAC_TYPE:
61 case IAVF_ERR_INVALID_MAC_ADDR:
62 case IAVF_ERR_INVALID_LINK_SETTINGS:
63 case IAVF_ERR_INVALID_PD_ID:
64 case IAVF_ERR_INVALID_QP_ID:
65 case IAVF_ERR_INVALID_CQ_ID:
66 case IAVF_ERR_INVALID_CEQ_ID:
67 case IAVF_ERR_INVALID_AEQ_ID:
68 case IAVF_ERR_INVALID_SIZE:
69 case IAVF_ERR_INVALID_ARP_INDEX:
70 case IAVF_ERR_INVALID_FPM_FUNC_ID:
71 case IAVF_ERR_QP_INVALID_MSG_SIZE:
72 case IAVF_ERR_INVALID_FRAG_COUNT:
73 case IAVF_ERR_INVALID_ALIGNMENT:
74 case IAVF_ERR_INVALID_PUSH_PAGE_INDEX:
75 case IAVF_ERR_INVALID_IMM_DATA_SIZE:
76 case IAVF_ERR_INVALID_VF_ID:
77 case IAVF_ERR_INVALID_HMCFN_ID:
78 case IAVF_ERR_INVALID_PBLE_INDEX:
79 case IAVF_ERR_INVALID_SD_INDEX:
80 case IAVF_ERR_INVALID_PAGE_DESC_INDEX:
81 case IAVF_ERR_INVALID_SD_TYPE:
82 case IAVF_ERR_INVALID_HMC_OBJ_INDEX:
83 case IAVF_ERR_INVALID_HMC_OBJ_COUNT:
84 case IAVF_ERR_INVALID_SRQ_ARM_LIMIT:
87 case IAVF_ERR_NVM_CHECKSUM:
90 case IAVF_ERR_UNKNOWN_PHY:
91 case IAVF_ERR_LINK_SETUP:
92 case IAVF_ERR_ADAPTER_STOPPED:
93 case IAVF_ERR_PRIMARY_REQUESTS_PENDING:
94 case IAVF_ERR_AUTONEG_NOT_COMPLETE:
95 case IAVF_ERR_RESET_FAILED:
96 case IAVF_ERR_BAD_PTR:
97 case IAVF_ERR_SWFW_SYNC:
98 case IAVF_ERR_QP_TOOMANY_WRS_POSTED:
99 case IAVF_ERR_QUEUE_EMPTY:
100 case IAVF_ERR_FLUSHED_QUEUE:
101 case IAVF_ERR_OPCODE_MISMATCH:
102 case IAVF_ERR_CQP_COMPL_ERROR:
103 case IAVF_ERR_BACKING_PAGE_ERROR:
104 case IAVF_ERR_NO_PBLCHUNKS_AVAILABLE:
105 case IAVF_ERR_MEMCPY_FAILED:
106 case IAVF_ERR_SRQ_ENABLED:
107 case IAVF_ERR_ADMIN_QUEUE_ERROR:
108 case IAVF_ERR_ADMIN_QUEUE_FULL:
109 case IAVF_ERR_BAD_IWARP_CQE:
110 case IAVF_ERR_NVM_BLANK_MODE:
111 case IAVF_ERR_PE_DOORBELL_NOT_ENABLED:
112 case IAVF_ERR_DIAG_TEST_FAILED:
113 case IAVF_ERR_FIRMWARE_API_VERSION:
114 case IAVF_ERR_ADMIN_QUEUE_CRITICAL_ERROR:
116 case IAVF_ERR_DEVICE_NOT_SUPPORTED:
118 case IAVF_ERR_NO_AVAILABLE_VSI:
119 case IAVF_ERR_RING_FULL:
121 case IAVF_ERR_NO_MEMORY:
123 case IAVF_ERR_TIMEOUT:
124 case IAVF_ERR_ADMIN_QUEUE_TIMEOUT:
126 case IAVF_ERR_NOT_IMPLEMENTED:
127 case IAVF_NOT_SUPPORTED:
129 case IAVF_ERR_ADMIN_QUEUE_NO_WORK:
131 case IAVF_ERR_NOT_READY:
133 case IAVF_ERR_BUF_TOO_SHORT:
140 int virtchnl_status_to_errno(enum virtchnl_status_code v_status)
143 case VIRTCHNL_STATUS_SUCCESS:
145 case VIRTCHNL_STATUS_ERR_PARAM:
146 case VIRTCHNL_STATUS_ERR_INVALID_VF_ID:
148 case VIRTCHNL_STATUS_ERR_NO_MEMORY:
150 case VIRTCHNL_STATUS_ERR_OPCODE_MISMATCH:
151 case VIRTCHNL_STATUS_ERR_CQP_COMPL_ERROR:
152 case VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR:
154 case VIRTCHNL_STATUS_ERR_NOT_SUPPORTED:
162 * iavf_pdev_to_adapter - go from pci_dev to adapter
163 * @pdev: pci_dev pointer
165 static struct iavf_adapter *iavf_pdev_to_adapter(struct pci_dev *pdev)
167 return netdev_priv(pci_get_drvdata(pdev));
171 * iavf_allocate_dma_mem_d - OS specific memory alloc for shared code
172 * @hw: pointer to the HW structure
173 * @mem: ptr to mem struct to fill out
174 * @size: size of memory requested
175 * @alignment: what to align the allocation to
177 enum iavf_status iavf_allocate_dma_mem_d(struct iavf_hw *hw,
178 struct iavf_dma_mem *mem,
179 u64 size, u32 alignment)
181 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
184 return IAVF_ERR_PARAM;
186 mem->size = ALIGN(size, alignment);
187 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
188 (dma_addr_t *)&mem->pa, GFP_KERNEL);
192 return IAVF_ERR_NO_MEMORY;
196 * iavf_free_dma_mem_d - OS specific memory free for shared code
197 * @hw: pointer to the HW structure
198 * @mem: ptr to mem struct to free
200 enum iavf_status iavf_free_dma_mem_d(struct iavf_hw *hw,
201 struct iavf_dma_mem *mem)
203 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
205 if (!mem || !mem->va)
206 return IAVF_ERR_PARAM;
207 dma_free_coherent(&adapter->pdev->dev, mem->size,
208 mem->va, (dma_addr_t)mem->pa);
213 * iavf_allocate_virt_mem_d - OS specific memory alloc for shared code
214 * @hw: pointer to the HW structure
215 * @mem: ptr to mem struct to fill out
216 * @size: size of memory requested
218 enum iavf_status iavf_allocate_virt_mem_d(struct iavf_hw *hw,
219 struct iavf_virt_mem *mem, u32 size)
222 return IAVF_ERR_PARAM;
225 mem->va = kzalloc(size, GFP_KERNEL);
230 return IAVF_ERR_NO_MEMORY;
234 * iavf_free_virt_mem_d - OS specific memory free for shared code
235 * @hw: pointer to the HW structure
236 * @mem: ptr to mem struct to free
238 enum iavf_status iavf_free_virt_mem_d(struct iavf_hw *hw,
239 struct iavf_virt_mem *mem)
242 return IAVF_ERR_PARAM;
244 /* it's ok to kfree a NULL pointer */
251 * iavf_lock_timeout - try to lock mutex but give up after timeout
252 * @lock: mutex that should be locked
253 * @msecs: timeout in msecs
255 * Returns 0 on success, negative on failure
257 int iavf_lock_timeout(struct mutex *lock, unsigned int msecs)
259 unsigned int wait, delay = 10;
261 for (wait = 0; wait < msecs; wait += delay) {
262 if (mutex_trylock(lock))
272 * iavf_schedule_reset - Set the flags and schedule a reset event
273 * @adapter: board private structure
275 void iavf_schedule_reset(struct iavf_adapter *adapter)
277 if (!(adapter->flags &
278 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
279 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
280 queue_work(iavf_wq, &adapter->reset_task);
285 * iavf_schedule_request_stats - Set the flags and schedule statistics request
286 * @adapter: board private structure
288 * Sets IAVF_FLAG_AQ_REQUEST_STATS flag so iavf_watchdog_task() will explicitly
289 * request and refresh ethtool stats
291 void iavf_schedule_request_stats(struct iavf_adapter *adapter)
293 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_STATS;
294 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
298 * iavf_tx_timeout - Respond to a Tx Hang
299 * @netdev: network interface device structure
300 * @txqueue: queue number that is timing out
302 static void iavf_tx_timeout(struct net_device *netdev, unsigned int txqueue)
304 struct iavf_adapter *adapter = netdev_priv(netdev);
306 adapter->tx_timeout_count++;
307 iavf_schedule_reset(adapter);
311 * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
312 * @adapter: board private structure
314 static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
316 struct iavf_hw *hw = &adapter->hw;
318 if (!adapter->msix_entries)
321 wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
325 synchronize_irq(adapter->msix_entries[0].vector);
329 * iavf_misc_irq_enable - Enable default interrupt generation settings
330 * @adapter: board private structure
332 static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
334 struct iavf_hw *hw = &adapter->hw;
336 wr32(hw, IAVF_VFINT_DYN_CTL01, IAVF_VFINT_DYN_CTL01_INTENA_MASK |
337 IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
338 wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
344 * iavf_irq_disable - Mask off interrupt generation on the NIC
345 * @adapter: board private structure
347 static void iavf_irq_disable(struct iavf_adapter *adapter)
350 struct iavf_hw *hw = &adapter->hw;
352 if (!adapter->msix_entries)
355 for (i = 1; i < adapter->num_msix_vectors; i++) {
356 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1), 0);
357 synchronize_irq(adapter->msix_entries[i].vector);
363 * iavf_irq_enable_queues - Enable interrupt for specified queues
364 * @adapter: board private structure
365 * @mask: bitmap of queues to enable
367 void iavf_irq_enable_queues(struct iavf_adapter *adapter, u32 mask)
369 struct iavf_hw *hw = &adapter->hw;
372 for (i = 1; i < adapter->num_msix_vectors; i++) {
373 if (mask & BIT(i - 1)) {
374 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1),
375 IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
376 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
382 * iavf_irq_enable - Enable default interrupt generation settings
383 * @adapter: board private structure
384 * @flush: boolean value whether to run rd32()
386 void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
388 struct iavf_hw *hw = &adapter->hw;
390 iavf_misc_irq_enable(adapter);
391 iavf_irq_enable_queues(adapter, ~0);
398 * iavf_msix_aq - Interrupt handler for vector 0
399 * @irq: interrupt number
400 * @data: pointer to netdev
402 static irqreturn_t iavf_msix_aq(int irq, void *data)
404 struct net_device *netdev = data;
405 struct iavf_adapter *adapter = netdev_priv(netdev);
406 struct iavf_hw *hw = &adapter->hw;
408 /* handle non-queue interrupts, these reads clear the registers */
409 rd32(hw, IAVF_VFINT_ICR01);
410 rd32(hw, IAVF_VFINT_ICR0_ENA1);
412 if (adapter->state != __IAVF_REMOVE)
413 /* schedule work on the private workqueue */
414 queue_work(iavf_wq, &adapter->adminq_task);
420 * iavf_msix_clean_rings - MSIX mode Interrupt Handler
421 * @irq: interrupt number
422 * @data: pointer to a q_vector
424 static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
426 struct iavf_q_vector *q_vector = data;
428 if (!q_vector->tx.ring && !q_vector->rx.ring)
431 napi_schedule_irqoff(&q_vector->napi);
437 * iavf_map_vector_to_rxq - associate irqs with rx queues
438 * @adapter: board private structure
439 * @v_idx: interrupt number
440 * @r_idx: queue number
443 iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
445 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
446 struct iavf_ring *rx_ring = &adapter->rx_rings[r_idx];
447 struct iavf_hw *hw = &adapter->hw;
449 rx_ring->q_vector = q_vector;
450 rx_ring->next = q_vector->rx.ring;
451 rx_ring->vsi = &adapter->vsi;
452 q_vector->rx.ring = rx_ring;
453 q_vector->rx.count++;
454 q_vector->rx.next_update = jiffies + 1;
455 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
456 q_vector->ring_mask |= BIT(r_idx);
457 wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, q_vector->reg_idx),
458 q_vector->rx.current_itr >> 1);
459 q_vector->rx.current_itr = q_vector->rx.target_itr;
463 * iavf_map_vector_to_txq - associate irqs with tx queues
464 * @adapter: board private structure
465 * @v_idx: interrupt number
466 * @t_idx: queue number
469 iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
471 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
472 struct iavf_ring *tx_ring = &adapter->tx_rings[t_idx];
473 struct iavf_hw *hw = &adapter->hw;
475 tx_ring->q_vector = q_vector;
476 tx_ring->next = q_vector->tx.ring;
477 tx_ring->vsi = &adapter->vsi;
478 q_vector->tx.ring = tx_ring;
479 q_vector->tx.count++;
480 q_vector->tx.next_update = jiffies + 1;
481 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
482 q_vector->num_ringpairs++;
483 wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, q_vector->reg_idx),
484 q_vector->tx.target_itr >> 1);
485 q_vector->tx.current_itr = q_vector->tx.target_itr;
489 * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
490 * @adapter: board private structure to initialize
492 * This function maps descriptor rings to the queue-specific vectors
493 * we were allotted through the MSI-X enabling code. Ideally, we'd have
494 * one vector per ring/queue, but on a constrained vector budget, we
495 * group the rings as "efficiently" as possible. You would add new
496 * mapping configurations in here.
498 static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
500 int rings_remaining = adapter->num_active_queues;
501 int ridx = 0, vidx = 0;
504 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
506 for (; ridx < rings_remaining; ridx++) {
507 iavf_map_vector_to_rxq(adapter, vidx, ridx);
508 iavf_map_vector_to_txq(adapter, vidx, ridx);
510 /* In the case where we have more queues than vectors, continue
511 * round-robin on vectors until all queues are mapped.
513 if (++vidx >= q_vectors)
517 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
521 * iavf_irq_affinity_notify - Callback for affinity changes
522 * @notify: context as to what irq was changed
523 * @mask: the new affinity mask
525 * This is a callback function used by the irq_set_affinity_notifier function
526 * so that we may register to receive changes to the irq affinity masks.
528 static void iavf_irq_affinity_notify(struct irq_affinity_notify *notify,
529 const cpumask_t *mask)
531 struct iavf_q_vector *q_vector =
532 container_of(notify, struct iavf_q_vector, affinity_notify);
534 cpumask_copy(&q_vector->affinity_mask, mask);
538 * iavf_irq_affinity_release - Callback for affinity notifier release
539 * @ref: internal core kernel usage
541 * This is a callback function used by the irq_set_affinity_notifier function
542 * to inform the current notification subscriber that they will no longer
543 * receive notifications.
545 static void iavf_irq_affinity_release(struct kref *ref) {}
548 * iavf_request_traffic_irqs - Initialize MSI-X interrupts
549 * @adapter: board private structure
550 * @basename: device basename
552 * Allocates MSI-X vectors for tx and rx handling, and requests
553 * interrupts from the kernel.
556 iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
558 unsigned int vector, q_vectors;
559 unsigned int rx_int_idx = 0, tx_int_idx = 0;
563 iavf_irq_disable(adapter);
564 /* Decrement for Other and TCP Timer vectors */
565 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
567 for (vector = 0; vector < q_vectors; vector++) {
568 struct iavf_q_vector *q_vector = &adapter->q_vectors[vector];
570 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
572 if (q_vector->tx.ring && q_vector->rx.ring) {
573 snprintf(q_vector->name, sizeof(q_vector->name),
574 "iavf-%s-TxRx-%u", basename, rx_int_idx++);
576 } else if (q_vector->rx.ring) {
577 snprintf(q_vector->name, sizeof(q_vector->name),
578 "iavf-%s-rx-%u", basename, rx_int_idx++);
579 } else if (q_vector->tx.ring) {
580 snprintf(q_vector->name, sizeof(q_vector->name),
581 "iavf-%s-tx-%u", basename, tx_int_idx++);
583 /* skip this unused q_vector */
586 err = request_irq(irq_num,
587 iavf_msix_clean_rings,
592 dev_info(&adapter->pdev->dev,
593 "Request_irq failed, error: %d\n", err);
594 goto free_queue_irqs;
596 /* register for affinity change notifications */
597 q_vector->affinity_notify.notify = iavf_irq_affinity_notify;
598 q_vector->affinity_notify.release =
599 iavf_irq_affinity_release;
600 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
601 /* Spread the IRQ affinity hints across online CPUs. Note that
602 * get_cpu_mask returns a mask with a permanent lifetime so
603 * it's safe to use as a hint for irq_update_affinity_hint.
605 cpu = cpumask_local_spread(q_vector->v_idx, -1);
606 irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
614 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
615 irq_set_affinity_notifier(irq_num, NULL);
616 irq_update_affinity_hint(irq_num, NULL);
617 free_irq(irq_num, &adapter->q_vectors[vector]);
623 * iavf_request_misc_irq - Initialize MSI-X interrupts
624 * @adapter: board private structure
626 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
627 * vector is only for the admin queue, and stays active even when the netdev
630 static int iavf_request_misc_irq(struct iavf_adapter *adapter)
632 struct net_device *netdev = adapter->netdev;
635 snprintf(adapter->misc_vector_name,
636 sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
637 dev_name(&adapter->pdev->dev));
638 err = request_irq(adapter->msix_entries[0].vector,
640 adapter->misc_vector_name, netdev);
642 dev_err(&adapter->pdev->dev,
643 "request_irq for %s failed: %d\n",
644 adapter->misc_vector_name, err);
645 free_irq(adapter->msix_entries[0].vector, netdev);
651 * iavf_free_traffic_irqs - Free MSI-X interrupts
652 * @adapter: board private structure
654 * Frees all MSI-X vectors other than 0.
656 static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
658 int vector, irq_num, q_vectors;
660 if (!adapter->msix_entries)
663 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
665 for (vector = 0; vector < q_vectors; vector++) {
666 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
667 irq_set_affinity_notifier(irq_num, NULL);
668 irq_update_affinity_hint(irq_num, NULL);
669 free_irq(irq_num, &adapter->q_vectors[vector]);
674 * iavf_free_misc_irq - Free MSI-X miscellaneous vector
675 * @adapter: board private structure
677 * Frees MSI-X vector 0.
679 static void iavf_free_misc_irq(struct iavf_adapter *adapter)
681 struct net_device *netdev = adapter->netdev;
683 if (!adapter->msix_entries)
686 free_irq(adapter->msix_entries[0].vector, netdev);
690 * iavf_configure_tx - Configure Transmit Unit after Reset
691 * @adapter: board private structure
693 * Configure the Tx unit of the MAC after a reset.
695 static void iavf_configure_tx(struct iavf_adapter *adapter)
697 struct iavf_hw *hw = &adapter->hw;
700 for (i = 0; i < adapter->num_active_queues; i++)
701 adapter->tx_rings[i].tail = hw->hw_addr + IAVF_QTX_TAIL1(i);
705 * iavf_configure_rx - Configure Receive Unit after Reset
706 * @adapter: board private structure
708 * Configure the Rx unit of the MAC after a reset.
710 static void iavf_configure_rx(struct iavf_adapter *adapter)
712 unsigned int rx_buf_len = IAVF_RXBUFFER_2048;
713 struct iavf_hw *hw = &adapter->hw;
716 /* Legacy Rx will always default to a 2048 buffer size. */
717 #if (PAGE_SIZE < 8192)
718 if (!(adapter->flags & IAVF_FLAG_LEGACY_RX)) {
719 struct net_device *netdev = adapter->netdev;
721 /* For jumbo frames on systems with 4K pages we have to use
722 * an order 1 page, so we might as well increase the size
723 * of our Rx buffer to make better use of the available space
725 rx_buf_len = IAVF_RXBUFFER_3072;
727 /* We use a 1536 buffer size for configurations with
728 * standard Ethernet mtu. On x86 this gives us enough room
729 * for shared info and 192 bytes of padding.
731 if (!IAVF_2K_TOO_SMALL_WITH_PADDING &&
732 (netdev->mtu <= ETH_DATA_LEN))
733 rx_buf_len = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
737 for (i = 0; i < adapter->num_active_queues; i++) {
738 adapter->rx_rings[i].tail = hw->hw_addr + IAVF_QRX_TAIL1(i);
739 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
741 if (adapter->flags & IAVF_FLAG_LEGACY_RX)
742 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
744 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
749 * iavf_find_vlan - Search filter list for specific vlan filter
750 * @adapter: board private structure
753 * Returns ptr to the filter object or NULL. Must be called while holding the
754 * mac_vlan_list_lock.
757 iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter,
758 struct iavf_vlan vlan)
760 struct iavf_vlan_filter *f;
762 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
763 if (f->vlan.vid == vlan.vid &&
764 f->vlan.tpid == vlan.tpid)
772 * iavf_add_vlan - Add a vlan filter to the list
773 * @adapter: board private structure
776 * Returns ptr to the filter object or NULL when no memory available.
779 iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter,
780 struct iavf_vlan vlan)
782 struct iavf_vlan_filter *f = NULL;
784 spin_lock_bh(&adapter->mac_vlan_list_lock);
786 f = iavf_find_vlan(adapter, vlan);
788 f = kzalloc(sizeof(*f), GFP_ATOMIC);
794 list_add_tail(&f->list, &adapter->vlan_filter_list);
796 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
800 spin_unlock_bh(&adapter->mac_vlan_list_lock);
805 * iavf_del_vlan - Remove a vlan filter from the list
806 * @adapter: board private structure
809 static void iavf_del_vlan(struct iavf_adapter *adapter, struct iavf_vlan vlan)
811 struct iavf_vlan_filter *f;
813 spin_lock_bh(&adapter->mac_vlan_list_lock);
815 f = iavf_find_vlan(adapter, vlan);
818 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
821 spin_unlock_bh(&adapter->mac_vlan_list_lock);
825 * iavf_restore_filters
826 * @adapter: board private structure
828 * Restore existing non MAC filters when VF netdev comes back up
830 static void iavf_restore_filters(struct iavf_adapter *adapter)
834 /* re-add all VLAN filters */
835 for_each_set_bit(vid, adapter->vsi.active_cvlans, VLAN_N_VID)
836 iavf_add_vlan(adapter, IAVF_VLAN(vid, ETH_P_8021Q));
838 for_each_set_bit(vid, adapter->vsi.active_svlans, VLAN_N_VID)
839 iavf_add_vlan(adapter, IAVF_VLAN(vid, ETH_P_8021AD));
843 * iavf_get_num_vlans_added - get number of VLANs added
844 * @adapter: board private structure
846 u16 iavf_get_num_vlans_added(struct iavf_adapter *adapter)
848 return bitmap_weight(adapter->vsi.active_cvlans, VLAN_N_VID) +
849 bitmap_weight(adapter->vsi.active_svlans, VLAN_N_VID);
853 * iavf_get_max_vlans_allowed - get maximum VLANs allowed for this VF
854 * @adapter: board private structure
856 * This depends on the negotiated VLAN capability. For VIRTCHNL_VF_OFFLOAD_VLAN,
857 * do not impose a limit as that maintains current behavior and for
858 * VIRTCHNL_VF_OFFLOAD_VLAN_V2, use the maximum allowed sent from the PF.
860 static u16 iavf_get_max_vlans_allowed(struct iavf_adapter *adapter)
862 /* don't impose any limit for VIRTCHNL_VF_OFFLOAD_VLAN since there has
863 * never been a limit on the VF driver side
865 if (VLAN_ALLOWED(adapter))
867 else if (VLAN_V2_ALLOWED(adapter))
868 return adapter->vlan_v2_caps.filtering.max_filters;
874 * iavf_max_vlans_added - check if maximum VLANs allowed already exist
875 * @adapter: board private structure
877 static bool iavf_max_vlans_added(struct iavf_adapter *adapter)
879 if (iavf_get_num_vlans_added(adapter) <
880 iavf_get_max_vlans_allowed(adapter))
887 * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
888 * @netdev: network device struct
889 * @proto: unused protocol data
892 static int iavf_vlan_rx_add_vid(struct net_device *netdev,
893 __always_unused __be16 proto, u16 vid)
895 struct iavf_adapter *adapter = netdev_priv(netdev);
897 if (!VLAN_FILTERING_ALLOWED(adapter))
900 if (iavf_max_vlans_added(adapter)) {
901 netdev_err(netdev, "Max allowed VLAN filters %u. Remove existing VLANs or disable filtering via Ethtool if supported.\n",
902 iavf_get_max_vlans_allowed(adapter));
906 if (!iavf_add_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto))))
913 * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
914 * @netdev: network device struct
915 * @proto: unused protocol data
918 static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
919 __always_unused __be16 proto, u16 vid)
921 struct iavf_adapter *adapter = netdev_priv(netdev);
923 iavf_del_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto)));
924 if (proto == cpu_to_be16(ETH_P_8021Q))
925 clear_bit(vid, adapter->vsi.active_cvlans);
927 clear_bit(vid, adapter->vsi.active_svlans);
933 * iavf_find_filter - Search filter list for specific mac filter
934 * @adapter: board private structure
935 * @macaddr: the MAC address
937 * Returns ptr to the filter object or NULL. Must be called while holding the
938 * mac_vlan_list_lock.
941 iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
944 struct iavf_mac_filter *f;
949 list_for_each_entry(f, &adapter->mac_filter_list, list) {
950 if (ether_addr_equal(macaddr, f->macaddr))
957 * iavf_add_filter - Add a mac filter to the filter list
958 * @adapter: board private structure
959 * @macaddr: the MAC address
961 * Returns ptr to the filter object or NULL when no memory available.
963 struct iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
966 struct iavf_mac_filter *f;
971 f = iavf_find_filter(adapter, macaddr);
973 f = kzalloc(sizeof(*f), GFP_ATOMIC);
977 ether_addr_copy(f->macaddr, macaddr);
979 list_add_tail(&f->list, &adapter->mac_filter_list);
981 f->add_handled = false;
982 f->is_new_mac = true;
983 f->is_primary = ether_addr_equal(macaddr, adapter->hw.mac.addr);
984 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
993 * iavf_replace_primary_mac - Replace current primary address
994 * @adapter: board private structure
995 * @new_mac: new MAC address to be applied
997 * Replace current dev_addr and send request to PF for removal of previous
998 * primary MAC address filter and addition of new primary MAC filter.
999 * Return 0 for success, -ENOMEM for failure.
1001 * Do not call this with mac_vlan_list_lock!
1003 int iavf_replace_primary_mac(struct iavf_adapter *adapter,
1006 struct iavf_hw *hw = &adapter->hw;
1007 struct iavf_mac_filter *f;
1009 spin_lock_bh(&adapter->mac_vlan_list_lock);
1011 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1012 f->is_primary = false;
1015 f = iavf_find_filter(adapter, hw->mac.addr);
1018 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1021 f = iavf_add_filter(adapter, new_mac);
1024 /* Always send the request to add if changing primary MAC
1025 * even if filter is already present on the list
1027 f->is_primary = true;
1029 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
1030 ether_addr_copy(hw->mac.addr, new_mac);
1033 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1035 /* schedule the watchdog task to immediately process the request */
1037 queue_work(iavf_wq, &adapter->watchdog_task.work);
1044 * iavf_is_mac_set_handled - wait for a response to set MAC from PF
1045 * @netdev: network interface device structure
1046 * @macaddr: MAC address to set
1048 * Returns true on success, false on failure
1050 static bool iavf_is_mac_set_handled(struct net_device *netdev,
1053 struct iavf_adapter *adapter = netdev_priv(netdev);
1054 struct iavf_mac_filter *f;
1057 spin_lock_bh(&adapter->mac_vlan_list_lock);
1059 f = iavf_find_filter(adapter, macaddr);
1061 if (!f || (!f->add && f->add_handled))
1064 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1070 * iavf_set_mac - NDO callback to set port MAC address
1071 * @netdev: network interface device structure
1072 * @p: pointer to an address structure
1074 * Returns 0 on success, negative on failure
1076 static int iavf_set_mac(struct net_device *netdev, void *p)
1078 struct iavf_adapter *adapter = netdev_priv(netdev);
1079 struct sockaddr *addr = p;
1082 if (!is_valid_ether_addr(addr->sa_data))
1083 return -EADDRNOTAVAIL;
1085 ret = iavf_replace_primary_mac(adapter, addr->sa_data);
1090 ret = wait_event_interruptible_timeout(adapter->vc_waitqueue,
1091 iavf_is_mac_set_handled(netdev, addr->sa_data),
1092 msecs_to_jiffies(2500));
1094 /* If ret < 0 then it means wait was interrupted.
1095 * If ret == 0 then it means we got a timeout.
1096 * else it means we got response for set MAC from PF,
1097 * check if netdev MAC was updated to requested MAC,
1098 * if yes then set MAC succeeded otherwise it failed return -EACCES
1106 if (!ether_addr_equal(netdev->dev_addr, addr->sa_data))
1113 * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
1114 * @netdev: the netdevice
1115 * @addr: address to add
1117 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1118 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1120 static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
1122 struct iavf_adapter *adapter = netdev_priv(netdev);
1124 if (iavf_add_filter(adapter, addr))
1131 * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1132 * @netdev: the netdevice
1133 * @addr: address to add
1135 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1136 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1138 static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
1140 struct iavf_adapter *adapter = netdev_priv(netdev);
1141 struct iavf_mac_filter *f;
1143 /* Under some circumstances, we might receive a request to delete
1144 * our own device address from our uc list. Because we store the
1145 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1146 * such requests and not delete our device address from this list.
1148 if (ether_addr_equal(addr, netdev->dev_addr))
1151 f = iavf_find_filter(adapter, addr);
1154 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1160 * iavf_set_rx_mode - NDO callback to set the netdev filters
1161 * @netdev: network interface device structure
1163 static void iavf_set_rx_mode(struct net_device *netdev)
1165 struct iavf_adapter *adapter = netdev_priv(netdev);
1167 spin_lock_bh(&adapter->mac_vlan_list_lock);
1168 __dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1169 __dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1170 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1172 if (netdev->flags & IFF_PROMISC &&
1173 !(adapter->flags & IAVF_FLAG_PROMISC_ON))
1174 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_PROMISC;
1175 else if (!(netdev->flags & IFF_PROMISC) &&
1176 adapter->flags & IAVF_FLAG_PROMISC_ON)
1177 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_PROMISC;
1179 if (netdev->flags & IFF_ALLMULTI &&
1180 !(adapter->flags & IAVF_FLAG_ALLMULTI_ON))
1181 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_ALLMULTI;
1182 else if (!(netdev->flags & IFF_ALLMULTI) &&
1183 adapter->flags & IAVF_FLAG_ALLMULTI_ON)
1184 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_ALLMULTI;
1188 * iavf_napi_enable_all - enable NAPI on all queue vectors
1189 * @adapter: board private structure
1191 static void iavf_napi_enable_all(struct iavf_adapter *adapter)
1194 struct iavf_q_vector *q_vector;
1195 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1197 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1198 struct napi_struct *napi;
1200 q_vector = &adapter->q_vectors[q_idx];
1201 napi = &q_vector->napi;
1207 * iavf_napi_disable_all - disable NAPI on all queue vectors
1208 * @adapter: board private structure
1210 static void iavf_napi_disable_all(struct iavf_adapter *adapter)
1213 struct iavf_q_vector *q_vector;
1214 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1216 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1217 q_vector = &adapter->q_vectors[q_idx];
1218 napi_disable(&q_vector->napi);
1223 * iavf_configure - set up transmit and receive data structures
1224 * @adapter: board private structure
1226 static void iavf_configure(struct iavf_adapter *adapter)
1228 struct net_device *netdev = adapter->netdev;
1231 iavf_set_rx_mode(netdev);
1233 iavf_configure_tx(adapter);
1234 iavf_configure_rx(adapter);
1235 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
1237 for (i = 0; i < adapter->num_active_queues; i++) {
1238 struct iavf_ring *ring = &adapter->rx_rings[i];
1240 iavf_alloc_rx_buffers(ring, IAVF_DESC_UNUSED(ring));
1245 * iavf_up_complete - Finish the last steps of bringing up a connection
1246 * @adapter: board private structure
1248 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
1250 static void iavf_up_complete(struct iavf_adapter *adapter)
1252 iavf_change_state(adapter, __IAVF_RUNNING);
1253 clear_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1255 iavf_napi_enable_all(adapter);
1257 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_QUEUES;
1258 if (CLIENT_ENABLED(adapter))
1259 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_OPEN;
1260 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1264 * iavf_clear_mac_vlan_filters - Remove mac and vlan filters not sent to PF
1265 * yet and mark other to be removed.
1266 * @adapter: board private structure
1268 static void iavf_clear_mac_vlan_filters(struct iavf_adapter *adapter)
1270 struct iavf_vlan_filter *vlf, *vlftmp;
1271 struct iavf_mac_filter *f, *ftmp;
1273 spin_lock_bh(&adapter->mac_vlan_list_lock);
1274 /* clear the sync flag on all filters */
1275 __dev_uc_unsync(adapter->netdev, NULL);
1276 __dev_mc_unsync(adapter->netdev, NULL);
1278 /* remove all MAC filters */
1279 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1289 /* remove all VLAN filters */
1290 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
1293 list_del(&vlf->list);
1299 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1303 * iavf_clear_cloud_filters - Remove cloud filters not sent to PF yet and
1304 * mark other to be removed.
1305 * @adapter: board private structure
1307 static void iavf_clear_cloud_filters(struct iavf_adapter *adapter)
1309 struct iavf_cloud_filter *cf, *cftmp;
1311 /* remove all cloud filters */
1312 spin_lock_bh(&adapter->cloud_filter_list_lock);
1313 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
1316 list_del(&cf->list);
1318 adapter->num_cloud_filters--;
1323 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1327 * iavf_clear_fdir_filters - Remove fdir filters not sent to PF yet and mark
1328 * other to be removed.
1329 * @adapter: board private structure
1331 static void iavf_clear_fdir_filters(struct iavf_adapter *adapter)
1333 struct iavf_fdir_fltr *fdir, *fdirtmp;
1335 /* remove all Flow Director filters */
1336 spin_lock_bh(&adapter->fdir_fltr_lock);
1337 list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head,
1339 if (fdir->state == IAVF_FDIR_FLTR_ADD_REQUEST) {
1340 list_del(&fdir->list);
1342 adapter->fdir_active_fltr--;
1344 fdir->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1347 spin_unlock_bh(&adapter->fdir_fltr_lock);
1351 * iavf_clear_adv_rss_conf - Remove adv rss conf not sent to PF yet and mark
1352 * other to be removed.
1353 * @adapter: board private structure
1355 static void iavf_clear_adv_rss_conf(struct iavf_adapter *adapter)
1357 struct iavf_adv_rss *rss, *rsstmp;
1359 /* remove all advance RSS configuration */
1360 spin_lock_bh(&adapter->adv_rss_lock);
1361 list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
1363 if (rss->state == IAVF_ADV_RSS_ADD_REQUEST) {
1364 list_del(&rss->list);
1367 rss->state = IAVF_ADV_RSS_DEL_REQUEST;
1370 spin_unlock_bh(&adapter->adv_rss_lock);
1374 * iavf_down - Shutdown the connection processing
1375 * @adapter: board private structure
1377 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
1379 void iavf_down(struct iavf_adapter *adapter)
1381 struct net_device *netdev = adapter->netdev;
1383 if (adapter->state <= __IAVF_DOWN_PENDING)
1386 netif_carrier_off(netdev);
1387 netif_tx_disable(netdev);
1388 adapter->link_up = false;
1389 iavf_napi_disable_all(adapter);
1390 iavf_irq_disable(adapter);
1392 iavf_clear_mac_vlan_filters(adapter);
1393 iavf_clear_cloud_filters(adapter);
1394 iavf_clear_fdir_filters(adapter);
1395 iavf_clear_adv_rss_conf(adapter);
1397 if (!(adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)) {
1398 /* cancel any current operation */
1399 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1400 /* Schedule operations to close down the HW. Don't wait
1401 * here for this to complete. The watchdog is still running
1402 * and it will take care of this.
1404 if (!list_empty(&adapter->mac_filter_list))
1405 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1406 if (!list_empty(&adapter->vlan_filter_list))
1407 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
1408 if (!list_empty(&adapter->cloud_filter_list))
1409 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1410 if (!list_empty(&adapter->fdir_list_head))
1411 adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1412 if (!list_empty(&adapter->adv_rss_list_head))
1413 adapter->aq_required |= IAVF_FLAG_AQ_DEL_ADV_RSS_CFG;
1414 adapter->aq_required |= IAVF_FLAG_AQ_DISABLE_QUEUES;
1417 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1421 * iavf_acquire_msix_vectors - Setup the MSIX capability
1422 * @adapter: board private structure
1423 * @vectors: number of vectors to request
1425 * Work with the OS to set up the MSIX vectors needed.
1427 * Returns 0 on success, negative on failure
1430 iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1432 int err, vector_threshold;
1434 /* We'll want at least 3 (vector_threshold):
1435 * 0) Other (Admin Queue and link, mostly)
1439 vector_threshold = MIN_MSIX_COUNT;
1441 /* The more we get, the more we will assign to Tx/Rx Cleanup
1442 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1443 * Right now, we simply care about how many we'll get; we'll
1444 * set them up later while requesting irq's.
1446 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1447 vector_threshold, vectors);
1449 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1450 kfree(adapter->msix_entries);
1451 adapter->msix_entries = NULL;
1455 /* Adjust for only the vectors we'll use, which is minimum
1456 * of max_msix_q_vectors + NONQ_VECS, or the number of
1457 * vectors we were allocated.
1459 adapter->num_msix_vectors = err;
1464 * iavf_free_queues - Free memory for all rings
1465 * @adapter: board private structure to initialize
1467 * Free all of the memory associated with queue pairs.
1469 static void iavf_free_queues(struct iavf_adapter *adapter)
1471 if (!adapter->vsi_res)
1473 adapter->num_active_queues = 0;
1474 kfree(adapter->tx_rings);
1475 adapter->tx_rings = NULL;
1476 kfree(adapter->rx_rings);
1477 adapter->rx_rings = NULL;
1481 * iavf_set_queue_vlan_tag_loc - set location for VLAN tag offload
1482 * @adapter: board private structure
1484 * Based on negotiated capabilities, the VLAN tag needs to be inserted and/or
1485 * stripped in certain descriptor fields. Instead of checking the offload
1486 * capability bits in the hot path, cache the location the ring specific
1489 void iavf_set_queue_vlan_tag_loc(struct iavf_adapter *adapter)
1493 for (i = 0; i < adapter->num_active_queues; i++) {
1494 struct iavf_ring *tx_ring = &adapter->tx_rings[i];
1495 struct iavf_ring *rx_ring = &adapter->rx_rings[i];
1497 /* prevent multiple L2TAG bits being set after VFR */
1499 ~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1500 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2);
1502 ~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1503 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2);
1505 if (VLAN_ALLOWED(adapter)) {
1506 tx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1507 rx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1508 } else if (VLAN_V2_ALLOWED(adapter)) {
1509 struct virtchnl_vlan_supported_caps *stripping_support;
1510 struct virtchnl_vlan_supported_caps *insertion_support;
1513 &adapter->vlan_v2_caps.offloads.stripping_support;
1515 &adapter->vlan_v2_caps.offloads.insertion_support;
1517 if (stripping_support->outer) {
1518 if (stripping_support->outer &
1519 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1521 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1522 else if (stripping_support->outer &
1523 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1525 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1526 } else if (stripping_support->inner) {
1527 if (stripping_support->inner &
1528 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1530 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1531 else if (stripping_support->inner &
1532 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1534 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1537 if (insertion_support->outer) {
1538 if (insertion_support->outer &
1539 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1541 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1542 else if (insertion_support->outer &
1543 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1545 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1546 } else if (insertion_support->inner) {
1547 if (insertion_support->inner &
1548 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1550 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1551 else if (insertion_support->inner &
1552 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1554 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1561 * iavf_alloc_queues - Allocate memory for all rings
1562 * @adapter: board private structure to initialize
1564 * We allocate one ring per queue at run-time since we don't know the
1565 * number of queues at compile-time. The polling_netdev array is
1566 * intended for Multiqueue, but should work fine with a single queue.
1568 static int iavf_alloc_queues(struct iavf_adapter *adapter)
1570 int i, num_active_queues;
1572 /* If we're in reset reallocating queues we don't actually know yet for
1573 * certain the PF gave us the number of queues we asked for but we'll
1574 * assume it did. Once basic reset is finished we'll confirm once we
1575 * start negotiating config with PF.
1577 if (adapter->num_req_queues)
1578 num_active_queues = adapter->num_req_queues;
1579 else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1581 num_active_queues = adapter->ch_config.total_qps;
1583 num_active_queues = min_t(int,
1584 adapter->vsi_res->num_queue_pairs,
1585 (int)(num_online_cpus()));
1588 adapter->tx_rings = kcalloc(num_active_queues,
1589 sizeof(struct iavf_ring), GFP_KERNEL);
1590 if (!adapter->tx_rings)
1592 adapter->rx_rings = kcalloc(num_active_queues,
1593 sizeof(struct iavf_ring), GFP_KERNEL);
1594 if (!adapter->rx_rings)
1597 for (i = 0; i < num_active_queues; i++) {
1598 struct iavf_ring *tx_ring;
1599 struct iavf_ring *rx_ring;
1601 tx_ring = &adapter->tx_rings[i];
1603 tx_ring->queue_index = i;
1604 tx_ring->netdev = adapter->netdev;
1605 tx_ring->dev = &adapter->pdev->dev;
1606 tx_ring->count = adapter->tx_desc_count;
1607 tx_ring->itr_setting = IAVF_ITR_TX_DEF;
1608 if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1609 tx_ring->flags |= IAVF_TXR_FLAGS_WB_ON_ITR;
1611 rx_ring = &adapter->rx_rings[i];
1612 rx_ring->queue_index = i;
1613 rx_ring->netdev = adapter->netdev;
1614 rx_ring->dev = &adapter->pdev->dev;
1615 rx_ring->count = adapter->rx_desc_count;
1616 rx_ring->itr_setting = IAVF_ITR_RX_DEF;
1619 adapter->num_active_queues = num_active_queues;
1621 iavf_set_queue_vlan_tag_loc(adapter);
1626 iavf_free_queues(adapter);
1631 * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1632 * @adapter: board private structure to initialize
1634 * Attempt to configure the interrupts using the best available
1635 * capabilities of the hardware and the kernel.
1637 static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1639 int vector, v_budget;
1643 if (!adapter->vsi_res) {
1647 pairs = adapter->num_active_queues;
1649 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1650 * us much good if we have more vectors than CPUs. However, we already
1651 * limit the total number of queues by the number of CPUs so we do not
1652 * need any further limiting here.
1654 v_budget = min_t(int, pairs + NONQ_VECS,
1655 (int)adapter->vf_res->max_vectors);
1657 adapter->msix_entries = kcalloc(v_budget,
1658 sizeof(struct msix_entry), GFP_KERNEL);
1659 if (!adapter->msix_entries) {
1664 for (vector = 0; vector < v_budget; vector++)
1665 adapter->msix_entries[vector].entry = vector;
1667 err = iavf_acquire_msix_vectors(adapter, v_budget);
1670 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1671 netif_set_real_num_tx_queues(adapter->netdev, pairs);
1676 * iavf_config_rss_aq - Configure RSS keys and lut by using AQ commands
1677 * @adapter: board private structure
1679 * Return 0 on success, negative on failure
1681 static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1683 struct iavf_aqc_get_set_rss_key_data *rss_key =
1684 (struct iavf_aqc_get_set_rss_key_data *)adapter->rss_key;
1685 struct iavf_hw *hw = &adapter->hw;
1686 enum iavf_status status;
1688 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1689 /* bail because we already have a command pending */
1690 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1691 adapter->current_op);
1695 status = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1697 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1698 iavf_stat_str(hw, status),
1699 iavf_aq_str(hw, hw->aq.asq_last_status));
1700 return iavf_status_to_errno(status);
1704 status = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1705 adapter->rss_lut, adapter->rss_lut_size);
1707 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1708 iavf_stat_str(hw, status),
1709 iavf_aq_str(hw, hw->aq.asq_last_status));
1710 return iavf_status_to_errno(status);
1718 * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1719 * @adapter: board private structure
1721 * Returns 0 on success, negative on failure
1723 static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1725 struct iavf_hw *hw = &adapter->hw;
1729 dw = (u32 *)adapter->rss_key;
1730 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1731 wr32(hw, IAVF_VFQF_HKEY(i), dw[i]);
1733 dw = (u32 *)adapter->rss_lut;
1734 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1735 wr32(hw, IAVF_VFQF_HLUT(i), dw[i]);
1743 * iavf_config_rss - Configure RSS keys and lut
1744 * @adapter: board private structure
1746 * Returns 0 on success, negative on failure
1748 int iavf_config_rss(struct iavf_adapter *adapter)
1751 if (RSS_PF(adapter)) {
1752 adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1753 IAVF_FLAG_AQ_SET_RSS_KEY;
1755 } else if (RSS_AQ(adapter)) {
1756 return iavf_config_rss_aq(adapter);
1758 return iavf_config_rss_reg(adapter);
1763 * iavf_fill_rss_lut - Fill the lut with default values
1764 * @adapter: board private structure
1766 static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1770 for (i = 0; i < adapter->rss_lut_size; i++)
1771 adapter->rss_lut[i] = i % adapter->num_active_queues;
1775 * iavf_init_rss - Prepare for RSS
1776 * @adapter: board private structure
1778 * Return 0 on success, negative on failure
1780 static int iavf_init_rss(struct iavf_adapter *adapter)
1782 struct iavf_hw *hw = &adapter->hw;
1784 if (!RSS_PF(adapter)) {
1785 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1786 if (adapter->vf_res->vf_cap_flags &
1787 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1788 adapter->hena = IAVF_DEFAULT_RSS_HENA_EXPANDED;
1790 adapter->hena = IAVF_DEFAULT_RSS_HENA;
1792 wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->hena);
1793 wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1796 iavf_fill_rss_lut(adapter);
1797 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1799 return iavf_config_rss(adapter);
1803 * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1804 * @adapter: board private structure to initialize
1806 * We allocate one q_vector per queue interrupt. If allocation fails we
1809 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1811 int q_idx = 0, num_q_vectors;
1812 struct iavf_q_vector *q_vector;
1814 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1815 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1817 if (!adapter->q_vectors)
1820 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1821 q_vector = &adapter->q_vectors[q_idx];
1822 q_vector->adapter = adapter;
1823 q_vector->vsi = &adapter->vsi;
1824 q_vector->v_idx = q_idx;
1825 q_vector->reg_idx = q_idx;
1826 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1827 netif_napi_add(adapter->netdev, &q_vector->napi,
1835 * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1836 * @adapter: board private structure to initialize
1838 * This function frees the memory allocated to the q_vectors. In addition if
1839 * NAPI is enabled it will delete any references to the NAPI struct prior
1840 * to freeing the q_vector.
1842 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1844 int q_idx, num_q_vectors;
1847 if (!adapter->q_vectors)
1850 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1851 napi_vectors = adapter->num_active_queues;
1853 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1854 struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1856 if (q_idx < napi_vectors)
1857 netif_napi_del(&q_vector->napi);
1859 kfree(adapter->q_vectors);
1860 adapter->q_vectors = NULL;
1864 * iavf_reset_interrupt_capability - Reset MSIX setup
1865 * @adapter: board private structure
1868 void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1870 if (!adapter->msix_entries)
1873 pci_disable_msix(adapter->pdev);
1874 kfree(adapter->msix_entries);
1875 adapter->msix_entries = NULL;
1879 * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1880 * @adapter: board private structure to initialize
1883 int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1887 err = iavf_alloc_queues(adapter);
1889 dev_err(&adapter->pdev->dev,
1890 "Unable to allocate memory for queues\n");
1891 goto err_alloc_queues;
1895 err = iavf_set_interrupt_capability(adapter);
1898 dev_err(&adapter->pdev->dev,
1899 "Unable to setup interrupt capabilities\n");
1900 goto err_set_interrupt;
1903 err = iavf_alloc_q_vectors(adapter);
1905 dev_err(&adapter->pdev->dev,
1906 "Unable to allocate memory for queue vectors\n");
1907 goto err_alloc_q_vectors;
1910 /* If we've made it so far while ADq flag being ON, then we haven't
1911 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1912 * resources have been allocated in the reset path.
1913 * Now we can truly claim that ADq is enabled.
1915 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1917 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1920 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1921 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1922 adapter->num_active_queues);
1925 err_alloc_q_vectors:
1926 iavf_reset_interrupt_capability(adapter);
1928 iavf_free_queues(adapter);
1934 * iavf_free_rss - Free memory used by RSS structs
1935 * @adapter: board private structure
1937 static void iavf_free_rss(struct iavf_adapter *adapter)
1939 kfree(adapter->rss_key);
1940 adapter->rss_key = NULL;
1942 kfree(adapter->rss_lut);
1943 adapter->rss_lut = NULL;
1947 * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1948 * @adapter: board private structure
1950 * Returns 0 on success, negative on failure
1952 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter)
1954 struct net_device *netdev = adapter->netdev;
1957 if (netif_running(netdev))
1958 iavf_free_traffic_irqs(adapter);
1959 iavf_free_misc_irq(adapter);
1960 iavf_reset_interrupt_capability(adapter);
1961 iavf_free_q_vectors(adapter);
1962 iavf_free_queues(adapter);
1964 err = iavf_init_interrupt_scheme(adapter);
1968 netif_tx_stop_all_queues(netdev);
1970 err = iavf_request_misc_irq(adapter);
1974 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1976 iavf_map_rings_to_vectors(adapter);
1982 * iavf_process_aq_command - process aq_required flags
1983 * and sends aq command
1984 * @adapter: pointer to iavf adapter structure
1986 * Returns 0 on success
1987 * Returns error code if no command was sent
1988 * or error code if the command failed.
1990 static int iavf_process_aq_command(struct iavf_adapter *adapter)
1992 if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG)
1993 return iavf_send_vf_config_msg(adapter);
1994 if (adapter->aq_required & IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS)
1995 return iavf_send_vf_offload_vlan_v2_msg(adapter);
1996 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
1997 iavf_disable_queues(adapter);
2001 if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
2002 iavf_map_queues(adapter);
2006 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
2007 iavf_add_ether_addrs(adapter);
2011 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
2012 iavf_add_vlans(adapter);
2016 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
2017 iavf_del_ether_addrs(adapter);
2021 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
2022 iavf_del_vlans(adapter);
2026 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
2027 iavf_enable_vlan_stripping(adapter);
2031 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
2032 iavf_disable_vlan_stripping(adapter);
2036 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
2037 iavf_configure_queues(adapter);
2041 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
2042 iavf_enable_queues(adapter);
2046 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
2047 /* This message goes straight to the firmware, not the
2048 * PF, so we don't have to set current_op as we will
2049 * not get a response through the ARQ.
2051 adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
2054 if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
2055 iavf_get_hena(adapter);
2058 if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
2059 iavf_set_hena(adapter);
2062 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
2063 iavf_set_rss_key(adapter);
2066 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
2067 iavf_set_rss_lut(adapter);
2071 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_PROMISC) {
2072 iavf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
2073 FLAG_VF_MULTICAST_PROMISC);
2077 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_ALLMULTI) {
2078 iavf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
2081 if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) ||
2082 (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
2083 iavf_set_promiscuous(adapter, 0);
2087 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
2088 iavf_enable_channels(adapter);
2092 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
2093 iavf_disable_channels(adapter);
2096 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
2097 iavf_add_cloud_filter(adapter);
2101 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
2102 iavf_del_cloud_filter(adapter);
2105 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
2106 iavf_del_cloud_filter(adapter);
2109 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
2110 iavf_add_cloud_filter(adapter);
2113 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_FDIR_FILTER) {
2114 iavf_add_fdir_filter(adapter);
2115 return IAVF_SUCCESS;
2117 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_FDIR_FILTER) {
2118 iavf_del_fdir_filter(adapter);
2119 return IAVF_SUCCESS;
2121 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_ADV_RSS_CFG) {
2122 iavf_add_adv_rss_cfg(adapter);
2125 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_ADV_RSS_CFG) {
2126 iavf_del_adv_rss_cfg(adapter);
2129 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING) {
2130 iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2133 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING) {
2134 iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2137 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING) {
2138 iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2141 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING) {
2142 iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2145 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION) {
2146 iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2149 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION) {
2150 iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2153 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION) {
2154 iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2157 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION) {
2158 iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2162 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_STATS) {
2163 iavf_request_stats(adapter);
2171 * iavf_set_vlan_offload_features - set VLAN offload configuration
2172 * @adapter: board private structure
2173 * @prev_features: previous features used for comparison
2174 * @features: updated features used for configuration
2176 * Set the aq_required bit(s) based on the requested features passed in to
2177 * configure VLAN stripping and/or VLAN insertion if supported. Also, schedule
2178 * the watchdog if any changes are requested to expedite the request via
2182 iavf_set_vlan_offload_features(struct iavf_adapter *adapter,
2183 netdev_features_t prev_features,
2184 netdev_features_t features)
2186 bool enable_stripping = true, enable_insertion = true;
2187 u16 vlan_ethertype = 0;
2188 u64 aq_required = 0;
2190 /* keep cases separate because one ethertype for offloads can be
2191 * disabled at the same time as another is disabled, so check for an
2192 * enabled ethertype first, then check for disabled. Default to
2193 * ETH_P_8021Q so an ethertype is specified if disabling insertion and
2196 if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2197 vlan_ethertype = ETH_P_8021AD;
2198 else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2199 vlan_ethertype = ETH_P_8021Q;
2200 else if (prev_features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2201 vlan_ethertype = ETH_P_8021AD;
2202 else if (prev_features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2203 vlan_ethertype = ETH_P_8021Q;
2205 vlan_ethertype = ETH_P_8021Q;
2207 if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
2208 enable_stripping = false;
2209 if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
2210 enable_insertion = false;
2212 if (VLAN_ALLOWED(adapter)) {
2213 /* VIRTCHNL_VF_OFFLOAD_VLAN only has support for toggling VLAN
2214 * stripping via virtchnl. VLAN insertion can be toggled on the
2215 * netdev, but it doesn't require a virtchnl message
2217 if (enable_stripping)
2218 aq_required |= IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2220 aq_required |= IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2222 } else if (VLAN_V2_ALLOWED(adapter)) {
2223 switch (vlan_ethertype) {
2225 if (enable_stripping)
2226 aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING;
2228 aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING;
2230 if (enable_insertion)
2231 aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION;
2233 aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION;
2236 if (enable_stripping)
2237 aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING;
2239 aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING;
2241 if (enable_insertion)
2242 aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION;
2244 aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION;
2250 adapter->aq_required |= aq_required;
2251 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
2256 * iavf_startup - first step of driver startup
2257 * @adapter: board private structure
2259 * Function process __IAVF_STARTUP driver state.
2260 * When success the state is changed to __IAVF_INIT_VERSION_CHECK
2261 * when fails the state is changed to __IAVF_INIT_FAILED
2263 static void iavf_startup(struct iavf_adapter *adapter)
2265 struct pci_dev *pdev = adapter->pdev;
2266 struct iavf_hw *hw = &adapter->hw;
2267 enum iavf_status status;
2270 WARN_ON(adapter->state != __IAVF_STARTUP);
2272 /* driver loaded, probe complete */
2273 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2274 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2275 status = iavf_set_mac_type(hw);
2277 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n", status);
2281 ret = iavf_check_reset_complete(hw);
2283 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2287 hw->aq.num_arq_entries = IAVF_AQ_LEN;
2288 hw->aq.num_asq_entries = IAVF_AQ_LEN;
2289 hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2290 hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2292 status = iavf_init_adminq(hw);
2294 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2298 ret = iavf_send_api_ver(adapter);
2300 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", ret);
2301 iavf_shutdown_adminq(hw);
2304 iavf_change_state(adapter, __IAVF_INIT_VERSION_CHECK);
2307 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2311 * iavf_init_version_check - second step of driver startup
2312 * @adapter: board private structure
2314 * Function process __IAVF_INIT_VERSION_CHECK driver state.
2315 * When success the state is changed to __IAVF_INIT_GET_RESOURCES
2316 * when fails the state is changed to __IAVF_INIT_FAILED
2318 static void iavf_init_version_check(struct iavf_adapter *adapter)
2320 struct pci_dev *pdev = adapter->pdev;
2321 struct iavf_hw *hw = &adapter->hw;
2324 WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
2326 if (!iavf_asq_done(hw)) {
2327 dev_err(&pdev->dev, "Admin queue command never completed\n");
2328 iavf_shutdown_adminq(hw);
2329 iavf_change_state(adapter, __IAVF_STARTUP);
2333 /* aq msg sent, awaiting reply */
2334 err = iavf_verify_api_ver(adapter);
2336 if (err == -EALREADY)
2337 err = iavf_send_api_ver(adapter);
2339 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2340 adapter->pf_version.major,
2341 adapter->pf_version.minor,
2342 VIRTCHNL_VERSION_MAJOR,
2343 VIRTCHNL_VERSION_MINOR);
2346 err = iavf_send_vf_config_msg(adapter);
2348 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2352 iavf_change_state(adapter, __IAVF_INIT_GET_RESOURCES);
2355 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2359 * iavf_parse_vf_resource_msg - parse response from VIRTCHNL_OP_GET_VF_RESOURCES
2360 * @adapter: board private structure
2362 int iavf_parse_vf_resource_msg(struct iavf_adapter *adapter)
2364 int i, num_req_queues = adapter->num_req_queues;
2365 struct iavf_vsi *vsi = &adapter->vsi;
2367 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2368 if (adapter->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
2369 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2371 if (!adapter->vsi_res) {
2372 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2376 if (num_req_queues &&
2377 num_req_queues > adapter->vsi_res->num_queue_pairs) {
2378 /* Problem. The PF gave us fewer queues than what we had
2379 * negotiated in our request. Need a reset to see if we can't
2380 * get back to a working state.
2382 dev_err(&adapter->pdev->dev,
2383 "Requested %d queues, but PF only gave us %d.\n",
2385 adapter->vsi_res->num_queue_pairs);
2386 adapter->flags |= IAVF_FLAG_REINIT_MSIX_NEEDED;
2387 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2388 iavf_schedule_reset(adapter);
2392 adapter->num_req_queues = 0;
2393 adapter->vsi.id = adapter->vsi_res->vsi_id;
2395 adapter->vsi.back = adapter;
2396 adapter->vsi.base_vector = 1;
2397 vsi->netdev = adapter->netdev;
2398 vsi->qs_handle = adapter->vsi_res->qset_handle;
2399 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2400 adapter->rss_key_size = adapter->vf_res->rss_key_size;
2401 adapter->rss_lut_size = adapter->vf_res->rss_lut_size;
2403 adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
2404 adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
2411 * iavf_init_get_resources - third step of driver startup
2412 * @adapter: board private structure
2414 * Function process __IAVF_INIT_GET_RESOURCES driver state and
2415 * finishes driver initialization procedure.
2416 * When success the state is changed to __IAVF_DOWN
2417 * when fails the state is changed to __IAVF_INIT_FAILED
2419 static void iavf_init_get_resources(struct iavf_adapter *adapter)
2421 struct pci_dev *pdev = adapter->pdev;
2422 struct iavf_hw *hw = &adapter->hw;
2425 WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
2426 /* aq msg sent, awaiting reply */
2427 if (!adapter->vf_res) {
2428 adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
2430 if (!adapter->vf_res) {
2435 err = iavf_get_vf_config(adapter);
2436 if (err == -EALREADY) {
2437 err = iavf_send_vf_config_msg(adapter);
2439 } else if (err == -EINVAL) {
2440 /* We only get -EINVAL if the device is in a very bad
2441 * state or if we've been disabled for previous bad
2442 * behavior. Either way, we're done now.
2444 iavf_shutdown_adminq(hw);
2445 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2449 dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
2453 err = iavf_parse_vf_resource_msg(adapter);
2455 dev_err(&pdev->dev, "Failed to parse VF resource message from PF (%d)\n",
2459 /* Some features require additional messages to negotiate extended
2460 * capabilities. These are processed in sequence by the
2461 * __IAVF_INIT_EXTENDED_CAPS driver state.
2463 adapter->extended_caps = IAVF_EXTENDED_CAPS;
2465 iavf_change_state(adapter, __IAVF_INIT_EXTENDED_CAPS);
2469 kfree(adapter->vf_res);
2470 adapter->vf_res = NULL;
2472 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2476 * iavf_init_send_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2477 * @adapter: board private structure
2479 * Function processes send of the extended VLAN V2 capability message to the
2480 * PF. Must clear IAVF_EXTENDED_CAP_RECV_VLAN_V2 if the message is not sent,
2481 * e.g. due to PF not negotiating VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2483 static void iavf_init_send_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2487 WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2));
2489 ret = iavf_send_vf_offload_vlan_v2_msg(adapter);
2490 if (ret && ret == -EOPNOTSUPP) {
2491 /* PF does not support VIRTCHNL_VF_OFFLOAD_V2. In this case,
2492 * we did not send the capability exchange message and do not
2493 * expect a response.
2495 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2498 /* We sent the message, so move on to the next step */
2499 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2503 * iavf_init_recv_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2504 * @adapter: board private structure
2506 * Function processes receipt of the extended VLAN V2 capability message from
2509 static void iavf_init_recv_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2513 WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2));
2515 memset(&adapter->vlan_v2_caps, 0, sizeof(adapter->vlan_v2_caps));
2517 ret = iavf_get_vf_vlan_v2_caps(adapter);
2521 /* We've processed receipt of the VLAN V2 caps message */
2522 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2525 /* We didn't receive a reply. Make sure we try sending again when
2526 * __IAVF_INIT_FAILED attempts to recover.
2528 adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2529 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2533 * iavf_init_process_extended_caps - Part of driver startup
2534 * @adapter: board private structure
2536 * Function processes __IAVF_INIT_EXTENDED_CAPS driver state. This state
2537 * handles negotiating capabilities for features which require an additional
2540 * Once all extended capabilities exchanges are finished, the driver will
2541 * transition into __IAVF_INIT_CONFIG_ADAPTER.
2543 static void iavf_init_process_extended_caps(struct iavf_adapter *adapter)
2545 WARN_ON(adapter->state != __IAVF_INIT_EXTENDED_CAPS);
2547 /* Process capability exchange for VLAN V2 */
2548 if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2) {
2549 iavf_init_send_offload_vlan_v2_caps(adapter);
2551 } else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2) {
2552 iavf_init_recv_offload_vlan_v2_caps(adapter);
2556 /* When we reach here, no further extended capabilities exchanges are
2557 * necessary, so we finally transition into __IAVF_INIT_CONFIG_ADAPTER
2559 iavf_change_state(adapter, __IAVF_INIT_CONFIG_ADAPTER);
2563 * iavf_init_config_adapter - last part of driver startup
2564 * @adapter: board private structure
2566 * After all the supported capabilities are negotiated, then the
2567 * __IAVF_INIT_CONFIG_ADAPTER state will finish driver initialization.
2569 static void iavf_init_config_adapter(struct iavf_adapter *adapter)
2571 struct net_device *netdev = adapter->netdev;
2572 struct pci_dev *pdev = adapter->pdev;
2575 WARN_ON(adapter->state != __IAVF_INIT_CONFIG_ADAPTER);
2577 if (iavf_process_config(adapter))
2580 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2582 adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
2584 netdev->netdev_ops = &iavf_netdev_ops;
2585 iavf_set_ethtool_ops(netdev);
2586 netdev->watchdog_timeo = 5 * HZ;
2588 /* MTU range: 68 - 9710 */
2589 netdev->min_mtu = ETH_MIN_MTU;
2590 netdev->max_mtu = IAVF_MAX_RXBUFFER - IAVF_PACKET_HDR_PAD;
2592 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2593 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2594 adapter->hw.mac.addr);
2595 eth_hw_addr_random(netdev);
2596 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2598 eth_hw_addr_set(netdev, adapter->hw.mac.addr);
2599 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2602 adapter->tx_desc_count = IAVF_DEFAULT_TXD;
2603 adapter->rx_desc_count = IAVF_DEFAULT_RXD;
2604 err = iavf_init_interrupt_scheme(adapter);
2607 iavf_map_rings_to_vectors(adapter);
2608 if (adapter->vf_res->vf_cap_flags &
2609 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2610 adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
2612 err = iavf_request_misc_irq(adapter);
2616 netif_carrier_off(netdev);
2617 adapter->link_up = false;
2619 /* set the semaphore to prevent any callbacks after device registration
2620 * up to time when state of driver will be set to __IAVF_DOWN
2623 if (!adapter->netdev_registered) {
2624 err = register_netdevice(netdev);
2631 adapter->netdev_registered = true;
2633 netif_tx_stop_all_queues(netdev);
2634 if (CLIENT_ALLOWED(adapter)) {
2635 err = iavf_lan_add_device(adapter);
2637 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2640 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2641 if (netdev->features & NETIF_F_GRO)
2642 dev_info(&pdev->dev, "GRO is enabled\n");
2644 iavf_change_state(adapter, __IAVF_DOWN);
2645 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2648 iavf_misc_irq_enable(adapter);
2649 wake_up(&adapter->down_waitqueue);
2651 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2652 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2653 if (!adapter->rss_key || !adapter->rss_lut) {
2657 if (RSS_AQ(adapter))
2658 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
2660 iavf_init_rss(adapter);
2662 if (VLAN_V2_ALLOWED(adapter))
2663 /* request initial VLAN offload settings */
2664 iavf_set_vlan_offload_features(adapter, 0, netdev->features);
2668 iavf_free_rss(adapter);
2670 iavf_free_misc_irq(adapter);
2672 iavf_reset_interrupt_capability(adapter);
2674 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2678 * iavf_watchdog_task - Periodic call-back task
2679 * @work: pointer to work_struct
2681 static void iavf_watchdog_task(struct work_struct *work)
2683 struct iavf_adapter *adapter = container_of(work,
2684 struct iavf_adapter,
2685 watchdog_task.work);
2686 struct iavf_hw *hw = &adapter->hw;
2689 if (!mutex_trylock(&adapter->crit_lock)) {
2690 if (adapter->state == __IAVF_REMOVE)
2693 goto restart_watchdog;
2696 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2697 iavf_change_state(adapter, __IAVF_COMM_FAILED);
2699 if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
2700 adapter->aq_required = 0;
2701 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2702 mutex_unlock(&adapter->crit_lock);
2703 queue_work(iavf_wq, &adapter->reset_task);
2707 switch (adapter->state) {
2708 case __IAVF_STARTUP:
2709 iavf_startup(adapter);
2710 mutex_unlock(&adapter->crit_lock);
2711 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
2712 msecs_to_jiffies(30));
2714 case __IAVF_INIT_VERSION_CHECK:
2715 iavf_init_version_check(adapter);
2716 mutex_unlock(&adapter->crit_lock);
2717 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
2718 msecs_to_jiffies(30));
2720 case __IAVF_INIT_GET_RESOURCES:
2721 iavf_init_get_resources(adapter);
2722 mutex_unlock(&adapter->crit_lock);
2723 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
2724 msecs_to_jiffies(1));
2726 case __IAVF_INIT_EXTENDED_CAPS:
2727 iavf_init_process_extended_caps(adapter);
2728 mutex_unlock(&adapter->crit_lock);
2729 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
2730 msecs_to_jiffies(1));
2732 case __IAVF_INIT_CONFIG_ADAPTER:
2733 iavf_init_config_adapter(adapter);
2734 mutex_unlock(&adapter->crit_lock);
2735 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
2736 msecs_to_jiffies(1));
2738 case __IAVF_INIT_FAILED:
2739 if (test_bit(__IAVF_IN_REMOVE_TASK,
2740 &adapter->crit_section)) {
2741 /* Do not update the state and do not reschedule
2742 * watchdog task, iavf_remove should handle this state
2743 * as it can loop forever
2745 mutex_unlock(&adapter->crit_lock);
2748 if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
2749 dev_err(&adapter->pdev->dev,
2750 "Failed to communicate with PF; waiting before retry\n");
2751 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2752 iavf_shutdown_adminq(hw);
2753 mutex_unlock(&adapter->crit_lock);
2754 queue_delayed_work(iavf_wq,
2755 &adapter->watchdog_task, (5 * HZ));
2758 /* Try again from failed step*/
2759 iavf_change_state(adapter, adapter->last_state);
2760 mutex_unlock(&adapter->crit_lock);
2761 queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ);
2763 case __IAVF_COMM_FAILED:
2764 if (test_bit(__IAVF_IN_REMOVE_TASK,
2765 &adapter->crit_section)) {
2766 /* Set state to __IAVF_INIT_FAILED and perform remove
2767 * steps. Remove IAVF_FLAG_PF_COMMS_FAILED so the task
2768 * doesn't bring the state back to __IAVF_COMM_FAILED.
2770 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2771 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2772 mutex_unlock(&adapter->crit_lock);
2775 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2776 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2777 if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
2778 reg_val == VIRTCHNL_VFR_COMPLETED) {
2779 /* A chance for redemption! */
2780 dev_err(&adapter->pdev->dev,
2781 "Hardware came out of reset. Attempting reinit.\n");
2782 /* When init task contacts the PF and
2783 * gets everything set up again, it'll restart the
2784 * watchdog for us. Down, boy. Sit. Stay. Woof.
2786 iavf_change_state(adapter, __IAVF_STARTUP);
2787 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2789 adapter->aq_required = 0;
2790 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2791 mutex_unlock(&adapter->crit_lock);
2792 queue_delayed_work(iavf_wq,
2793 &adapter->watchdog_task,
2794 msecs_to_jiffies(10));
2796 case __IAVF_RESETTING:
2797 mutex_unlock(&adapter->crit_lock);
2798 queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
2801 case __IAVF_DOWN_PENDING:
2802 case __IAVF_TESTING:
2803 case __IAVF_RUNNING:
2804 if (adapter->current_op) {
2805 if (!iavf_asq_done(hw)) {
2806 dev_dbg(&adapter->pdev->dev,
2807 "Admin queue timeout\n");
2808 iavf_send_api_ver(adapter);
2811 int ret = iavf_process_aq_command(adapter);
2813 /* An error will be returned if no commands were
2814 * processed; use this opportunity to update stats
2815 * if the error isn't -ENOTSUPP
2817 if (ret && ret != -EOPNOTSUPP &&
2818 adapter->state == __IAVF_RUNNING)
2819 iavf_request_stats(adapter);
2821 if (adapter->state == __IAVF_RUNNING)
2822 iavf_detect_recover_hung(&adapter->vsi);
2826 mutex_unlock(&adapter->crit_lock);
2830 /* check for hw reset */
2831 reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2833 adapter->flags |= IAVF_FLAG_RESET_PENDING;
2834 adapter->aq_required = 0;
2835 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2836 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
2837 queue_work(iavf_wq, &adapter->reset_task);
2838 mutex_unlock(&adapter->crit_lock);
2839 queue_delayed_work(iavf_wq,
2840 &adapter->watchdog_task, HZ * 2);
2844 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
2845 mutex_unlock(&adapter->crit_lock);
2847 if (adapter->state >= __IAVF_DOWN)
2848 queue_work(iavf_wq, &adapter->adminq_task);
2849 if (adapter->aq_required)
2850 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
2851 msecs_to_jiffies(20));
2853 queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
2857 * iavf_disable_vf - disable VF
2858 * @adapter: board private structure
2860 * Set communication failed flag and free all resources.
2861 * NOTE: This function is expected to be called with crit_lock being held.
2863 static void iavf_disable_vf(struct iavf_adapter *adapter)
2865 struct iavf_mac_filter *f, *ftmp;
2866 struct iavf_vlan_filter *fv, *fvtmp;
2867 struct iavf_cloud_filter *cf, *cftmp;
2869 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2871 /* We don't use netif_running() because it may be true prior to
2872 * ndo_open() returning, so we can't assume it means all our open
2873 * tasks have finished, since we're not holding the rtnl_lock here.
2875 if (adapter->state == __IAVF_RUNNING) {
2876 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2877 netif_carrier_off(adapter->netdev);
2878 netif_tx_disable(adapter->netdev);
2879 adapter->link_up = false;
2880 iavf_napi_disable_all(adapter);
2881 iavf_irq_disable(adapter);
2882 iavf_free_traffic_irqs(adapter);
2883 iavf_free_all_tx_resources(adapter);
2884 iavf_free_all_rx_resources(adapter);
2887 spin_lock_bh(&adapter->mac_vlan_list_lock);
2889 /* Delete all of the filters */
2890 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2895 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
2896 list_del(&fv->list);
2900 spin_unlock_bh(&adapter->mac_vlan_list_lock);
2902 spin_lock_bh(&adapter->cloud_filter_list_lock);
2903 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
2904 list_del(&cf->list);
2906 adapter->num_cloud_filters--;
2908 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2910 iavf_free_misc_irq(adapter);
2911 iavf_reset_interrupt_capability(adapter);
2912 iavf_free_q_vectors(adapter);
2913 iavf_free_queues(adapter);
2914 memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
2915 iavf_shutdown_adminq(&adapter->hw);
2916 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2917 iavf_change_state(adapter, __IAVF_DOWN);
2918 wake_up(&adapter->down_waitqueue);
2919 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
2923 * iavf_reset_task - Call-back task to handle hardware reset
2924 * @work: pointer to work_struct
2926 * During reset we need to shut down and reinitialize the admin queue
2927 * before we can use it to communicate with the PF again. We also clear
2928 * and reinit the rings because that context is lost as well.
2930 static void iavf_reset_task(struct work_struct *work)
2932 struct iavf_adapter *adapter = container_of(work,
2933 struct iavf_adapter,
2935 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2936 struct net_device *netdev = adapter->netdev;
2937 struct iavf_hw *hw = &adapter->hw;
2938 struct iavf_mac_filter *f, *ftmp;
2939 struct iavf_cloud_filter *cf;
2940 enum iavf_status status;
2945 /* Detach interface to avoid subsequent NDO callbacks */
2947 netif_device_detach(netdev);
2950 /* When device is being removed it doesn't make sense to run the reset
2951 * task, just return in such a case.
2953 if (!mutex_trylock(&adapter->crit_lock)) {
2954 if (adapter->state != __IAVF_REMOVE)
2955 queue_work(iavf_wq, &adapter->reset_task);
2960 while (!mutex_trylock(&adapter->client_lock))
2961 usleep_range(500, 1000);
2962 if (CLIENT_ENABLED(adapter)) {
2963 adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
2964 IAVF_FLAG_CLIENT_NEEDS_CLOSE |
2965 IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
2966 IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
2967 cancel_delayed_work_sync(&adapter->client_task);
2968 iavf_notify_client_close(&adapter->vsi, true);
2970 iavf_misc_irq_disable(adapter);
2971 if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
2972 adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
2973 /* Restart the AQ here. If we have been reset but didn't
2974 * detect it, or if the PF had to reinit, our AQ will be hosed.
2976 iavf_shutdown_adminq(hw);
2977 iavf_init_adminq(hw);
2978 iavf_request_reset(adapter);
2980 adapter->flags |= IAVF_FLAG_RESET_PENDING;
2982 /* poll until we see the reset actually happen */
2983 for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
2984 reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
2985 IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2988 usleep_range(5000, 10000);
2990 if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
2991 dev_info(&adapter->pdev->dev, "Never saw reset\n");
2992 goto continue_reset; /* act like the reset happened */
2995 /* wait until the reset is complete and the PF is responding to us */
2996 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
2997 /* sleep first to make sure a minimum wait time is met */
2998 msleep(IAVF_RESET_WAIT_MS);
3000 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
3001 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3002 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
3006 pci_set_master(adapter->pdev);
3007 pci_restore_msi_state(adapter->pdev);
3009 if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
3010 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
3012 iavf_disable_vf(adapter);
3013 mutex_unlock(&adapter->client_lock);
3014 mutex_unlock(&adapter->crit_lock);
3015 if (netif_running(netdev)) {
3020 return; /* Do not attempt to reinit. It's dead, Jim. */
3024 /* We don't use netif_running() because it may be true prior to
3025 * ndo_open() returning, so we can't assume it means all our open
3026 * tasks have finished, since we're not holding the rtnl_lock here.
3028 running = adapter->state == __IAVF_RUNNING;
3031 netif_carrier_off(netdev);
3032 netif_tx_stop_all_queues(netdev);
3033 adapter->link_up = false;
3034 iavf_napi_disable_all(adapter);
3036 iavf_irq_disable(adapter);
3038 iavf_change_state(adapter, __IAVF_RESETTING);
3039 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3041 /* free the Tx/Rx rings and descriptors, might be better to just
3042 * re-use them sometime in the future
3044 iavf_free_all_rx_resources(adapter);
3045 iavf_free_all_tx_resources(adapter);
3047 adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
3048 /* kill and reinit the admin queue */
3049 iavf_shutdown_adminq(hw);
3050 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3051 status = iavf_init_adminq(hw);
3053 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
3057 adapter->aq_required = 0;
3059 if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3060 (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3061 err = iavf_reinit_interrupt_scheme(adapter);
3066 if (RSS_AQ(adapter)) {
3067 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
3069 err = iavf_init_rss(adapter);
3074 adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
3075 /* always set since VIRTCHNL_OP_GET_VF_RESOURCES has not been
3076 * sent/received yet, so VLAN_V2_ALLOWED() cannot is not reliable here,
3077 * however the VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS won't be sent until
3078 * VIRTCHNL_OP_GET_VF_RESOURCES and VIRTCHNL_VF_OFFLOAD_VLAN_V2 have
3079 * been successfully sent and negotiated
3081 adapter->aq_required |= IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS;
3082 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
3084 spin_lock_bh(&adapter->mac_vlan_list_lock);
3086 /* Delete filter for the current MAC address, it could have
3087 * been changed by the PF via administratively set MAC.
3088 * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
3090 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3091 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
3096 /* re-add all MAC filters */
3097 list_for_each_entry(f, &adapter->mac_filter_list, list) {
3100 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3102 /* check if TCs are running and re-add all cloud filters */
3103 spin_lock_bh(&adapter->cloud_filter_list_lock);
3104 if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
3106 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
3110 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3112 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
3113 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3114 iavf_misc_irq_enable(adapter);
3116 bitmap_clear(adapter->vsi.active_cvlans, 0, VLAN_N_VID);
3117 bitmap_clear(adapter->vsi.active_svlans, 0, VLAN_N_VID);
3119 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 2);
3121 /* We were running when the reset started, so we need to restore some
3125 /* allocate transmit descriptors */
3126 err = iavf_setup_all_tx_resources(adapter);
3130 /* allocate receive descriptors */
3131 err = iavf_setup_all_rx_resources(adapter);
3135 if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3136 (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3137 err = iavf_request_traffic_irqs(adapter, netdev->name);
3141 adapter->flags &= ~IAVF_FLAG_REINIT_MSIX_NEEDED;
3144 iavf_configure(adapter);
3146 /* iavf_up_complete() will switch device back
3149 iavf_up_complete(adapter);
3151 iavf_irq_enable(adapter, true);
3153 iavf_change_state(adapter, __IAVF_DOWN);
3154 wake_up(&adapter->down_waitqueue);
3157 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3159 mutex_unlock(&adapter->client_lock);
3160 mutex_unlock(&adapter->crit_lock);
3165 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3166 iavf_free_traffic_irqs(adapter);
3168 iavf_disable_vf(adapter);
3170 mutex_unlock(&adapter->client_lock);
3171 mutex_unlock(&adapter->crit_lock);
3173 if (netif_running(netdev)) {
3174 /* Close device to ensure that Tx queues will not be started
3175 * during netif_device_attach() at the end of the reset task.
3182 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
3185 netif_device_attach(netdev);
3190 * iavf_adminq_task - worker thread to clean the admin queue
3191 * @work: pointer to work_struct containing our data
3193 static void iavf_adminq_task(struct work_struct *work)
3195 struct iavf_adapter *adapter =
3196 container_of(work, struct iavf_adapter, adminq_task);
3197 struct iavf_hw *hw = &adapter->hw;
3198 struct iavf_arq_event_info event;
3199 enum virtchnl_ops v_op;
3200 enum iavf_status ret, v_ret;
3204 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
3207 if (!mutex_trylock(&adapter->crit_lock)) {
3208 if (adapter->state == __IAVF_REMOVE)
3211 queue_work(iavf_wq, &adapter->adminq_task);
3215 event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
3216 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
3221 ret = iavf_clean_arq_element(hw, &event, &pending);
3222 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
3223 v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
3226 break; /* No event to process or error cleaning ARQ */
3228 iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
3231 memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
3233 mutex_unlock(&adapter->crit_lock);
3235 if ((adapter->flags & IAVF_FLAG_SETUP_NETDEV_FEATURES)) {
3236 if (adapter->netdev_registered ||
3237 !test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section)) {
3238 struct net_device *netdev = adapter->netdev;
3241 netdev_update_features(netdev);
3243 /* Request VLAN offload settings */
3244 if (VLAN_V2_ALLOWED(adapter))
3245 iavf_set_vlan_offload_features
3246 (adapter, 0, netdev->features);
3248 iavf_set_queue_vlan_tag_loc(adapter);
3251 adapter->flags &= ~IAVF_FLAG_SETUP_NETDEV_FEATURES;
3253 if ((adapter->flags &
3254 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED)) ||
3255 adapter->state == __IAVF_RESETTING)
3258 /* check for error indications */
3259 val = rd32(hw, hw->aq.arq.len);
3260 if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
3263 if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
3264 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
3265 val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
3267 if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
3268 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
3269 val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
3271 if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
3272 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
3273 val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
3276 wr32(hw, hw->aq.arq.len, val);
3278 val = rd32(hw, hw->aq.asq.len);
3280 if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
3281 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
3282 val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
3284 if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
3285 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
3286 val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
3288 if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
3289 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
3290 val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
3293 wr32(hw, hw->aq.asq.len, val);
3296 kfree(event.msg_buf);
3298 /* re-enable Admin queue interrupt cause */
3299 iavf_misc_irq_enable(adapter);
3303 * iavf_client_task - worker thread to perform client work
3304 * @work: pointer to work_struct containing our data
3306 * This task handles client interactions. Because client calls can be
3307 * reentrant, we can't handle them in the watchdog.
3309 static void iavf_client_task(struct work_struct *work)
3311 struct iavf_adapter *adapter =
3312 container_of(work, struct iavf_adapter, client_task.work);
3314 /* If we can't get the client bit, just give up. We'll be rescheduled
3318 if (!mutex_trylock(&adapter->client_lock))
3321 if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
3322 iavf_client_subtask(adapter);
3323 adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
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;
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;
3336 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
3337 iavf_notify_client_open(&adapter->vsi);
3338 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
3341 mutex_unlock(&adapter->client_lock);
3345 * iavf_free_all_tx_resources - Free Tx Resources for All Queues
3346 * @adapter: board private structure
3348 * Free all transmit software resources
3350 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
3354 if (!adapter->tx_rings)
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]);
3363 * iavf_setup_all_tx_resources - allocate all queues Tx resources
3364 * @adapter: board private structure
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.
3370 * Return 0 on success, negative on failure
3372 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
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]);
3381 dev_err(&adapter->pdev->dev,
3382 "Allocation for Tx Queue %u failed\n", i);
3390 * iavf_setup_all_rx_resources - allocate all queues Rx resources
3391 * @adapter: board private structure
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.
3397 * Return 0 on success, negative on failure
3399 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
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]);
3408 dev_err(&adapter->pdev->dev,
3409 "Allocation for Rx Queue %u failed\n", i);
3416 * iavf_free_all_rx_resources - Free Rx Resources for All Queues
3417 * @adapter: board private structure
3419 * Free all receive software resources
3421 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
3425 if (!adapter->rx_rings)
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]);
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
3438 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
3441 int speed = 0, ret = 0;
3443 if (ADV_LINK_SUPPORT(adapter)) {
3444 if (adapter->link_speed_mbps < U32_MAX) {
3445 speed = adapter->link_speed_mbps;
3448 dev_err(&adapter->pdev->dev, "Unknown link speed\n");
3453 switch (adapter->link_speed) {
3454 case VIRTCHNL_LINK_SPEED_40GB:
3455 speed = SPEED_40000;
3457 case VIRTCHNL_LINK_SPEED_25GB:
3458 speed = SPEED_25000;
3460 case VIRTCHNL_LINK_SPEED_20GB:
3461 speed = SPEED_20000;
3463 case VIRTCHNL_LINK_SPEED_10GB:
3464 speed = SPEED_10000;
3466 case VIRTCHNL_LINK_SPEED_5GB:
3469 case VIRTCHNL_LINK_SPEED_2_5GB:
3472 case VIRTCHNL_LINK_SPEED_1GB:
3475 case VIRTCHNL_LINK_SPEED_100MB:
3483 if (max_tx_rate > speed) {
3484 dev_err(&adapter->pdev->dev,
3485 "Invalid tx rate specified\n");
3493 * iavf_validate_ch_config - validate queue mapping info
3494 * @adapter: board private structure
3495 * @mqprio_qopt: queue parameters
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
3501 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
3502 struct tc_mqprio_qopt_offload *mqprio_qopt)
3504 u64 total_max_rate = 0;
3505 u32 tx_rate_rem = 0;
3510 if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
3511 mqprio_qopt->qopt.num_tc < 1)
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)
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",
3525 /* convert to Mbps */
3526 tx_rate = div_u64(mqprio_qopt->max_rate[i],
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);
3537 (void)div_u64_rem(tx_rate, IAVF_MBPS_QUANTA, &tx_rate_rem);
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);
3546 total_max_rate += tx_rate;
3547 num_qps += mqprio_qopt->qopt.count[i];
3549 if (num_qps > adapter->num_active_queues) {
3550 dev_err(&adapter->pdev->dev,
3551 "Cannot support requested number of queues\n");
3555 ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
3560 * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
3561 * @adapter: board private structure
3563 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
3565 struct iavf_cloud_filter *cf, *cftmp;
3567 spin_lock_bh(&adapter->cloud_filter_list_lock);
3568 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
3570 list_del(&cf->list);
3572 adapter->num_cloud_filters--;
3574 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3578 * __iavf_setup_tc - configure multiple traffic classes
3579 * @netdev: network interface device structure
3580 * @type_data: tc offload data
3582 * This function processes the config information provided by the
3583 * user to configure traffic classes/queue channels and packages the
3584 * information to request the PF to setup traffic classes.
3586 * Returns 0 on success.
3588 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
3590 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
3591 struct iavf_adapter *adapter = netdev_priv(netdev);
3592 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3593 u8 num_tc = 0, total_qps = 0;
3594 int ret = 0, netdev_tc = 0;
3599 num_tc = mqprio_qopt->qopt.num_tc;
3600 mode = mqprio_qopt->mode;
3602 /* delete queue_channel */
3603 if (!mqprio_qopt->qopt.hw) {
3604 if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
3605 /* reset the tc configuration */
3606 netdev_reset_tc(netdev);
3607 adapter->num_tc = 0;
3608 netif_tx_stop_all_queues(netdev);
3609 netif_tx_disable(netdev);
3610 iavf_del_all_cloud_filters(adapter);
3611 adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
3612 total_qps = adapter->orig_num_active_queues;
3619 /* add queue channel */
3620 if (mode == TC_MQPRIO_MODE_CHANNEL) {
3621 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3622 dev_err(&adapter->pdev->dev, "ADq not supported\n");
3625 if (adapter->ch_config.state != __IAVF_TC_INVALID) {
3626 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
3630 ret = iavf_validate_ch_config(adapter, mqprio_qopt);
3633 /* Return if same TC config is requested */
3634 if (adapter->num_tc == num_tc)
3636 adapter->num_tc = num_tc;
3638 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3640 adapter->ch_config.ch_info[i].count =
3641 mqprio_qopt->qopt.count[i];
3642 adapter->ch_config.ch_info[i].offset =
3643 mqprio_qopt->qopt.offset[i];
3644 total_qps += mqprio_qopt->qopt.count[i];
3645 max_tx_rate = mqprio_qopt->max_rate[i];
3646 /* convert to Mbps */
3647 max_tx_rate = div_u64(max_tx_rate,
3649 adapter->ch_config.ch_info[i].max_tx_rate =
3652 adapter->ch_config.ch_info[i].count = 1;
3653 adapter->ch_config.ch_info[i].offset = 0;
3657 /* Take snapshot of original config such as "num_active_queues"
3658 * It is used later when delete ADQ flow is exercised, so that
3659 * once delete ADQ flow completes, VF shall go back to its
3660 * original queue configuration
3663 adapter->orig_num_active_queues = adapter->num_active_queues;
3665 /* Store queue info based on TC so that VF gets configured
3666 * with correct number of queues when VF completes ADQ config
3669 adapter->ch_config.total_qps = total_qps;
3671 netif_tx_stop_all_queues(netdev);
3672 netif_tx_disable(netdev);
3673 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
3674 netdev_reset_tc(netdev);
3675 /* Report the tc mapping up the stack */
3676 netdev_set_num_tc(adapter->netdev, num_tc);
3677 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3678 u16 qcount = mqprio_qopt->qopt.count[i];
3679 u16 qoffset = mqprio_qopt->qopt.offset[i];
3682 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
3687 if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
3690 netif_set_real_num_rx_queues(netdev, total_qps);
3691 netif_set_real_num_tx_queues(netdev, total_qps);
3697 * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
3698 * @adapter: board private structure
3699 * @f: pointer to struct flow_cls_offload
3700 * @filter: pointer to cloud filter structure
3702 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
3703 struct flow_cls_offload *f,
3704 struct iavf_cloud_filter *filter)
3706 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
3707 struct flow_dissector *dissector = rule->match.dissector;
3708 u16 n_proto_mask = 0;
3709 u16 n_proto_key = 0;
3714 struct virtchnl_filter *vf = &filter->f;
3716 if (dissector->used_keys &
3717 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
3718 BIT(FLOW_DISSECTOR_KEY_BASIC) |
3719 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
3720 BIT(FLOW_DISSECTOR_KEY_VLAN) |
3721 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
3722 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
3723 BIT(FLOW_DISSECTOR_KEY_PORTS) |
3724 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
3725 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
3726 dissector->used_keys);
3730 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
3731 struct flow_match_enc_keyid match;
3733 flow_rule_match_enc_keyid(rule, &match);
3734 if (match.mask->keyid != 0)
3735 field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
3738 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
3739 struct flow_match_basic match;
3741 flow_rule_match_basic(rule, &match);
3742 n_proto_key = ntohs(match.key->n_proto);
3743 n_proto_mask = ntohs(match.mask->n_proto);
3745 if (n_proto_key == ETH_P_ALL) {
3749 n_proto = n_proto_key & n_proto_mask;
3750 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
3752 if (n_proto == ETH_P_IPV6) {
3753 /* specify flow type as TCP IPv6 */
3754 vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
3757 if (match.key->ip_proto != IPPROTO_TCP) {
3758 dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
3763 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
3764 struct flow_match_eth_addrs match;
3766 flow_rule_match_eth_addrs(rule, &match);
3768 /* use is_broadcast and is_zero to check for all 0xf or 0 */
3769 if (!is_zero_ether_addr(match.mask->dst)) {
3770 if (is_broadcast_ether_addr(match.mask->dst)) {
3771 field_flags |= IAVF_CLOUD_FIELD_OMAC;
3773 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
3779 if (!is_zero_ether_addr(match.mask->src)) {
3780 if (is_broadcast_ether_addr(match.mask->src)) {
3781 field_flags |= IAVF_CLOUD_FIELD_IMAC;
3783 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
3789 if (!is_zero_ether_addr(match.key->dst))
3790 if (is_valid_ether_addr(match.key->dst) ||
3791 is_multicast_ether_addr(match.key->dst)) {
3792 /* set the mask if a valid dst_mac address */
3793 for (i = 0; i < ETH_ALEN; i++)
3794 vf->mask.tcp_spec.dst_mac[i] |= 0xff;
3795 ether_addr_copy(vf->data.tcp_spec.dst_mac,
3799 if (!is_zero_ether_addr(match.key->src))
3800 if (is_valid_ether_addr(match.key->src) ||
3801 is_multicast_ether_addr(match.key->src)) {
3802 /* set the mask if a valid dst_mac address */
3803 for (i = 0; i < ETH_ALEN; i++)
3804 vf->mask.tcp_spec.src_mac[i] |= 0xff;
3805 ether_addr_copy(vf->data.tcp_spec.src_mac,
3810 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
3811 struct flow_match_vlan match;
3813 flow_rule_match_vlan(rule, &match);
3814 if (match.mask->vlan_id) {
3815 if (match.mask->vlan_id == VLAN_VID_MASK) {
3816 field_flags |= IAVF_CLOUD_FIELD_IVLAN;
3818 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
3819 match.mask->vlan_id);
3823 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
3824 vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
3827 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
3828 struct flow_match_control match;
3830 flow_rule_match_control(rule, &match);
3831 addr_type = match.key->addr_type;
3834 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
3835 struct flow_match_ipv4_addrs match;
3837 flow_rule_match_ipv4_addrs(rule, &match);
3838 if (match.mask->dst) {
3839 if (match.mask->dst == cpu_to_be32(0xffffffff)) {
3840 field_flags |= IAVF_CLOUD_FIELD_IIP;
3842 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
3843 be32_to_cpu(match.mask->dst));
3848 if (match.mask->src) {
3849 if (match.mask->src == cpu_to_be32(0xffffffff)) {
3850 field_flags |= IAVF_CLOUD_FIELD_IIP;
3852 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
3853 be32_to_cpu(match.mask->src));
3858 if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
3859 dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
3862 if (match.key->dst) {
3863 vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
3864 vf->data.tcp_spec.dst_ip[0] = match.key->dst;
3866 if (match.key->src) {
3867 vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
3868 vf->data.tcp_spec.src_ip[0] = match.key->src;
3872 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
3873 struct flow_match_ipv6_addrs match;
3875 flow_rule_match_ipv6_addrs(rule, &match);
3877 /* validate mask, make sure it is not IPV6_ADDR_ANY */
3878 if (ipv6_addr_any(&match.mask->dst)) {
3879 dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
3884 /* src and dest IPv6 address should not be LOOPBACK
3885 * (0:0:0:0:0:0:0:1) which can be represented as ::1
3887 if (ipv6_addr_loopback(&match.key->dst) ||
3888 ipv6_addr_loopback(&match.key->src)) {
3889 dev_err(&adapter->pdev->dev,
3890 "ipv6 addr should not be loopback\n");
3893 if (!ipv6_addr_any(&match.mask->dst) ||
3894 !ipv6_addr_any(&match.mask->src))
3895 field_flags |= IAVF_CLOUD_FIELD_IIP;
3897 for (i = 0; i < 4; i++)
3898 vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
3899 memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
3900 sizeof(vf->data.tcp_spec.dst_ip));
3901 for (i = 0; i < 4; i++)
3902 vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
3903 memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
3904 sizeof(vf->data.tcp_spec.src_ip));
3906 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
3907 struct flow_match_ports match;
3909 flow_rule_match_ports(rule, &match);
3910 if (match.mask->src) {
3911 if (match.mask->src == cpu_to_be16(0xffff)) {
3912 field_flags |= IAVF_CLOUD_FIELD_IIP;
3914 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
3915 be16_to_cpu(match.mask->src));
3920 if (match.mask->dst) {
3921 if (match.mask->dst == cpu_to_be16(0xffff)) {
3922 field_flags |= IAVF_CLOUD_FIELD_IIP;
3924 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
3925 be16_to_cpu(match.mask->dst));
3929 if (match.key->dst) {
3930 vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
3931 vf->data.tcp_spec.dst_port = match.key->dst;
3934 if (match.key->src) {
3935 vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
3936 vf->data.tcp_spec.src_port = match.key->src;
3939 vf->field_flags = field_flags;
3945 * iavf_handle_tclass - Forward to a traffic class on the device
3946 * @adapter: board private structure
3947 * @tc: traffic class index on the device
3948 * @filter: pointer to cloud filter structure
3950 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
3951 struct iavf_cloud_filter *filter)
3955 if (tc < adapter->num_tc) {
3956 if (!filter->f.data.tcp_spec.dst_port) {
3957 dev_err(&adapter->pdev->dev,
3958 "Specify destination port to redirect to traffic class other than TC0\n");
3962 /* redirect to a traffic class on the same device */
3963 filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
3964 filter->f.action_meta = tc;
3969 * iavf_find_cf - Find the cloud filter in the list
3970 * @adapter: Board private structure
3971 * @cookie: filter specific cookie
3973 * Returns ptr to the filter object or NULL. Must be called while holding the
3974 * cloud_filter_list_lock.
3976 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
3977 unsigned long *cookie)
3979 struct iavf_cloud_filter *filter = NULL;
3984 list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
3985 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
3992 * iavf_configure_clsflower - Add tc flower filters
3993 * @adapter: board private structure
3994 * @cls_flower: Pointer to struct flow_cls_offload
3996 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
3997 struct flow_cls_offload *cls_flower)
3999 int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
4000 struct iavf_cloud_filter *filter = NULL;
4001 int err = -EINVAL, count = 50;
4004 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
4008 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
4012 while (!mutex_trylock(&adapter->crit_lock)) {
4020 filter->cookie = cls_flower->cookie;
4022 /* bail out here if filter already exists */
4023 spin_lock_bh(&adapter->cloud_filter_list_lock);
4024 if (iavf_find_cf(adapter, &cls_flower->cookie)) {
4025 dev_err(&adapter->pdev->dev, "Failed to add TC Flower filter, it already exists\n");
4029 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4031 /* set the mask to all zeroes to begin with */
4032 memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
4033 /* start out with flow type and eth type IPv4 to begin with */
4034 filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
4035 err = iavf_parse_cls_flower(adapter, cls_flower, filter);
4039 err = iavf_handle_tclass(adapter, tc, filter);
4043 /* add filter to the list */
4044 spin_lock_bh(&adapter->cloud_filter_list_lock);
4045 list_add_tail(&filter->list, &adapter->cloud_filter_list);
4046 adapter->num_cloud_filters++;
4048 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
4050 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4055 mutex_unlock(&adapter->crit_lock);
4060 * iavf_delete_clsflower - Remove tc flower filters
4061 * @adapter: board private structure
4062 * @cls_flower: Pointer to struct flow_cls_offload
4064 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
4065 struct flow_cls_offload *cls_flower)
4067 struct iavf_cloud_filter *filter = NULL;
4070 spin_lock_bh(&adapter->cloud_filter_list_lock);
4071 filter = iavf_find_cf(adapter, &cls_flower->cookie);
4074 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
4078 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4084 * iavf_setup_tc_cls_flower - flower classifier offloads
4085 * @adapter: board private structure
4086 * @cls_flower: pointer to flow_cls_offload struct with flow info
4088 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
4089 struct flow_cls_offload *cls_flower)
4091 switch (cls_flower->command) {
4092 case FLOW_CLS_REPLACE:
4093 return iavf_configure_clsflower(adapter, cls_flower);
4094 case FLOW_CLS_DESTROY:
4095 return iavf_delete_clsflower(adapter, cls_flower);
4096 case FLOW_CLS_STATS:
4104 * iavf_setup_tc_block_cb - block callback for tc
4105 * @type: type of offload
4106 * @type_data: offload data
4109 * This function is the block callback for traffic classes
4111 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
4114 struct iavf_adapter *adapter = cb_priv;
4116 if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
4120 case TC_SETUP_CLSFLOWER:
4121 return iavf_setup_tc_cls_flower(cb_priv, type_data);
4127 static LIST_HEAD(iavf_block_cb_list);
4130 * iavf_setup_tc - configure multiple traffic classes
4131 * @netdev: network interface device structure
4132 * @type: type of offload
4133 * @type_data: tc offload data
4135 * This function is the callback to ndo_setup_tc in the
4138 * Returns 0 on success
4140 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
4143 struct iavf_adapter *adapter = netdev_priv(netdev);
4146 case TC_SETUP_QDISC_MQPRIO:
4147 return __iavf_setup_tc(netdev, type_data);
4148 case TC_SETUP_BLOCK:
4149 return flow_block_cb_setup_simple(type_data,
4150 &iavf_block_cb_list,
4151 iavf_setup_tc_block_cb,
4152 adapter, adapter, true);
4159 * iavf_open - Called when a network interface is made active
4160 * @netdev: network interface device structure
4162 * Returns 0 on success, negative value on failure
4164 * The open entry point is called when a network interface is made
4165 * active by the system (IFF_UP). At this point all resources needed
4166 * for transmit and receive operations are allocated, the interrupt
4167 * handler is registered with the OS, the watchdog is started,
4168 * and the stack is notified that the interface is ready.
4170 static int iavf_open(struct net_device *netdev)
4172 struct iavf_adapter *adapter = netdev_priv(netdev);
4175 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
4176 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
4180 while (!mutex_trylock(&adapter->crit_lock)) {
4181 /* If we are in __IAVF_INIT_CONFIG_ADAPTER state the crit_lock
4182 * is already taken and iavf_open is called from an upper
4183 * device's notifier reacting on NETDEV_REGISTER event.
4184 * We have to leave here to avoid dead lock.
4186 if (adapter->state == __IAVF_INIT_CONFIG_ADAPTER)
4189 usleep_range(500, 1000);
4192 if (adapter->state != __IAVF_DOWN) {
4197 if (adapter->state == __IAVF_RUNNING &&
4198 !test_bit(__IAVF_VSI_DOWN, adapter->vsi.state)) {
4199 dev_dbg(&adapter->pdev->dev, "VF is already open.\n");
4204 /* allocate transmit descriptors */
4205 err = iavf_setup_all_tx_resources(adapter);
4209 /* allocate receive descriptors */
4210 err = iavf_setup_all_rx_resources(adapter);
4214 /* clear any pending interrupts, may auto mask */
4215 err = iavf_request_traffic_irqs(adapter, netdev->name);
4219 spin_lock_bh(&adapter->mac_vlan_list_lock);
4221 iavf_add_filter(adapter, adapter->hw.mac.addr);
4223 spin_unlock_bh(&adapter->mac_vlan_list_lock);
4225 /* Restore VLAN filters that were removed with IFF_DOWN */
4226 iavf_restore_filters(adapter);
4228 iavf_configure(adapter);
4230 iavf_up_complete(adapter);
4232 iavf_irq_enable(adapter, true);
4234 mutex_unlock(&adapter->crit_lock);
4240 iavf_free_traffic_irqs(adapter);
4242 iavf_free_all_rx_resources(adapter);
4244 iavf_free_all_tx_resources(adapter);
4246 mutex_unlock(&adapter->crit_lock);
4252 * iavf_close - Disables a network interface
4253 * @netdev: network interface device structure
4255 * Returns 0, this is not allowed to fail
4257 * The close entry point is called when an interface is de-activated
4258 * by the OS. The hardware is still under the drivers control, but
4259 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
4260 * are freed, along with all transmit and receive resources.
4262 static int iavf_close(struct net_device *netdev)
4264 struct iavf_adapter *adapter = netdev_priv(netdev);
4268 mutex_lock(&adapter->crit_lock);
4270 if (adapter->state <= __IAVF_DOWN_PENDING) {
4271 mutex_unlock(&adapter->crit_lock);
4275 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
4276 if (CLIENT_ENABLED(adapter))
4277 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
4278 /* We cannot send IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS before
4279 * IAVF_FLAG_AQ_DISABLE_QUEUES because in such case there is rtnl
4280 * deadlock with adminq_task() until iavf_close timeouts. We must send
4281 * IAVF_FLAG_AQ_GET_CONFIG before IAVF_FLAG_AQ_DISABLE_QUEUES to make
4282 * disable queues possible for vf. Give only necessary flags to
4283 * iavf_down and save other to set them right before iavf_close()
4284 * returns, when IAVF_FLAG_AQ_DISABLE_QUEUES will be already sent and
4285 * iavf will be in DOWN state.
4287 aq_to_restore = adapter->aq_required;
4288 adapter->aq_required &= IAVF_FLAG_AQ_GET_CONFIG;
4290 /* Remove flags which we do not want to send after close or we want to
4291 * send before disable queues.
4293 aq_to_restore &= ~(IAVF_FLAG_AQ_GET_CONFIG |
4294 IAVF_FLAG_AQ_ENABLE_QUEUES |
4295 IAVF_FLAG_AQ_CONFIGURE_QUEUES |
4296 IAVF_FLAG_AQ_ADD_VLAN_FILTER |
4297 IAVF_FLAG_AQ_ADD_MAC_FILTER |
4298 IAVF_FLAG_AQ_ADD_CLOUD_FILTER |
4299 IAVF_FLAG_AQ_ADD_FDIR_FILTER |
4300 IAVF_FLAG_AQ_ADD_ADV_RSS_CFG);
4303 iavf_change_state(adapter, __IAVF_DOWN_PENDING);
4304 iavf_free_traffic_irqs(adapter);
4306 mutex_unlock(&adapter->crit_lock);
4308 /* We explicitly don't free resources here because the hardware is
4309 * still active and can DMA into memory. Resources are cleared in
4310 * iavf_virtchnl_completion() after we get confirmation from the PF
4311 * driver that the rings have been stopped.
4313 * Also, we wait for state to transition to __IAVF_DOWN before
4314 * returning. State change occurs in iavf_virtchnl_completion() after
4315 * VF resources are released (which occurs after PF driver processes and
4316 * responds to admin queue commands).
4319 status = wait_event_timeout(adapter->down_waitqueue,
4320 adapter->state == __IAVF_DOWN,
4321 msecs_to_jiffies(500));
4323 netdev_warn(netdev, "Device resources not yet released\n");
4325 mutex_lock(&adapter->crit_lock);
4326 adapter->aq_required |= aq_to_restore;
4327 mutex_unlock(&adapter->crit_lock);
4332 * iavf_change_mtu - Change the Maximum Transfer Unit
4333 * @netdev: network interface device structure
4334 * @new_mtu: new value for maximum frame size
4336 * Returns 0 on success, negative on failure
4338 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
4340 struct iavf_adapter *adapter = netdev_priv(netdev);
4342 netdev_dbg(netdev, "changing MTU from %d to %d\n",
4343 netdev->mtu, new_mtu);
4344 netdev->mtu = new_mtu;
4345 if (CLIENT_ENABLED(adapter)) {
4346 iavf_notify_client_l2_params(&adapter->vsi);
4347 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
4350 if (netif_running(netdev)) {
4351 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
4352 queue_work(iavf_wq, &adapter->reset_task);
4358 #define NETIF_VLAN_OFFLOAD_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
4359 NETIF_F_HW_VLAN_CTAG_TX | \
4360 NETIF_F_HW_VLAN_STAG_RX | \
4361 NETIF_F_HW_VLAN_STAG_TX)
4364 * iavf_set_features - set the netdev feature flags
4365 * @netdev: ptr to the netdev being adjusted
4366 * @features: the feature set that the stack is suggesting
4367 * Note: expects to be called while under rtnl_lock()
4369 static int iavf_set_features(struct net_device *netdev,
4370 netdev_features_t features)
4372 struct iavf_adapter *adapter = netdev_priv(netdev);
4374 /* trigger update on any VLAN feature change */
4375 if ((netdev->features & NETIF_VLAN_OFFLOAD_FEATURES) ^
4376 (features & NETIF_VLAN_OFFLOAD_FEATURES))
4377 iavf_set_vlan_offload_features(adapter, netdev->features,
4384 * iavf_features_check - Validate encapsulated packet conforms to limits
4386 * @dev: This physical port's netdev
4387 * @features: Offload features that the stack believes apply
4389 static netdev_features_t iavf_features_check(struct sk_buff *skb,
4390 struct net_device *dev,
4391 netdev_features_t features)
4395 /* No point in doing any of this if neither checksum nor GSO are
4396 * being requested for this frame. We can rule out both by just
4397 * checking for CHECKSUM_PARTIAL
4399 if (skb->ip_summed != CHECKSUM_PARTIAL)
4402 /* We cannot support GSO if the MSS is going to be less than
4403 * 64 bytes. If it is then we need to drop support for GSO.
4405 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4406 features &= ~NETIF_F_GSO_MASK;
4408 /* MACLEN can support at most 63 words */
4409 len = skb_network_header(skb) - skb->data;
4410 if (len & ~(63 * 2))
4413 /* IPLEN and EIPLEN can support at most 127 dwords */
4414 len = skb_transport_header(skb) - skb_network_header(skb);
4415 if (len & ~(127 * 4))
4418 if (skb->encapsulation) {
4419 /* L4TUNLEN can support 127 words */
4420 len = skb_inner_network_header(skb) - skb_transport_header(skb);
4421 if (len & ~(127 * 2))
4424 /* IPLEN can support at most 127 dwords */
4425 len = skb_inner_transport_header(skb) -
4426 skb_inner_network_header(skb);
4427 if (len & ~(127 * 4))
4431 /* No need to validate L4LEN as TCP is the only protocol with a
4432 * flexible value and we support all possible values supported
4433 * by TCP, which is at most 15 dwords
4438 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4442 * iavf_get_netdev_vlan_hw_features - get NETDEV VLAN features that can toggle on/off
4443 * @adapter: board private structure
4445 * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4446 * were negotiated determine the VLAN features that can be toggled on and off.
4448 static netdev_features_t
4449 iavf_get_netdev_vlan_hw_features(struct iavf_adapter *adapter)
4451 netdev_features_t hw_features = 0;
4453 if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4456 /* Enable VLAN features if supported */
4457 if (VLAN_ALLOWED(adapter)) {
4458 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
4459 NETIF_F_HW_VLAN_CTAG_RX);
4460 } else if (VLAN_V2_ALLOWED(adapter)) {
4461 struct virtchnl_vlan_caps *vlan_v2_caps =
4462 &adapter->vlan_v2_caps;
4463 struct virtchnl_vlan_supported_caps *stripping_support =
4464 &vlan_v2_caps->offloads.stripping_support;
4465 struct virtchnl_vlan_supported_caps *insertion_support =
4466 &vlan_v2_caps->offloads.insertion_support;
4468 if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4469 stripping_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4470 if (stripping_support->outer &
4471 VIRTCHNL_VLAN_ETHERTYPE_8100)
4472 hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4473 if (stripping_support->outer &
4474 VIRTCHNL_VLAN_ETHERTYPE_88A8)
4475 hw_features |= NETIF_F_HW_VLAN_STAG_RX;
4476 } else if (stripping_support->inner !=
4477 VIRTCHNL_VLAN_UNSUPPORTED &&
4478 stripping_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4479 if (stripping_support->inner &
4480 VIRTCHNL_VLAN_ETHERTYPE_8100)
4481 hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4484 if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4485 insertion_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4486 if (insertion_support->outer &
4487 VIRTCHNL_VLAN_ETHERTYPE_8100)
4488 hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4489 if (insertion_support->outer &
4490 VIRTCHNL_VLAN_ETHERTYPE_88A8)
4491 hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4492 } else if (insertion_support->inner &&
4493 insertion_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4494 if (insertion_support->inner &
4495 VIRTCHNL_VLAN_ETHERTYPE_8100)
4496 hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4504 * iavf_get_netdev_vlan_features - get the enabled NETDEV VLAN fetures
4505 * @adapter: board private structure
4507 * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4508 * were negotiated determine the VLAN features that are enabled by default.
4510 static netdev_features_t
4511 iavf_get_netdev_vlan_features(struct iavf_adapter *adapter)
4513 netdev_features_t features = 0;
4515 if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4518 if (VLAN_ALLOWED(adapter)) {
4519 features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4520 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
4521 } else if (VLAN_V2_ALLOWED(adapter)) {
4522 struct virtchnl_vlan_caps *vlan_v2_caps =
4523 &adapter->vlan_v2_caps;
4524 struct virtchnl_vlan_supported_caps *filtering_support =
4525 &vlan_v2_caps->filtering.filtering_support;
4526 struct virtchnl_vlan_supported_caps *stripping_support =
4527 &vlan_v2_caps->offloads.stripping_support;
4528 struct virtchnl_vlan_supported_caps *insertion_support =
4529 &vlan_v2_caps->offloads.insertion_support;
4532 /* give priority to outer stripping and don't support both outer
4533 * and inner stripping
4535 ethertype_init = vlan_v2_caps->offloads.ethertype_init;
4536 if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4537 if (stripping_support->outer &
4538 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4539 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4540 features |= NETIF_F_HW_VLAN_CTAG_RX;
4541 else if (stripping_support->outer &
4542 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4543 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4544 features |= NETIF_F_HW_VLAN_STAG_RX;
4545 } else if (stripping_support->inner !=
4546 VIRTCHNL_VLAN_UNSUPPORTED) {
4547 if (stripping_support->inner &
4548 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4549 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4550 features |= NETIF_F_HW_VLAN_CTAG_RX;
4553 /* give priority to outer insertion and don't support both outer
4554 * and inner insertion
4556 if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4557 if (insertion_support->outer &
4558 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4559 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4560 features |= NETIF_F_HW_VLAN_CTAG_TX;
4561 else if (insertion_support->outer &
4562 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4563 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4564 features |= NETIF_F_HW_VLAN_STAG_TX;
4565 } else if (insertion_support->inner !=
4566 VIRTCHNL_VLAN_UNSUPPORTED) {
4567 if (insertion_support->inner &
4568 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4569 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4570 features |= NETIF_F_HW_VLAN_CTAG_TX;
4573 /* give priority to outer filtering and don't bother if both
4574 * outer and inner filtering are enabled
4576 ethertype_init = vlan_v2_caps->filtering.ethertype_init;
4577 if (filtering_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4578 if (filtering_support->outer &
4579 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4580 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4581 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4582 if (filtering_support->outer &
4583 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4584 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4585 features |= NETIF_F_HW_VLAN_STAG_FILTER;
4586 } else if (filtering_support->inner !=
4587 VIRTCHNL_VLAN_UNSUPPORTED) {
4588 if (filtering_support->inner &
4589 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4590 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4591 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4592 if (filtering_support->inner &
4593 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4594 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4595 features |= NETIF_F_HW_VLAN_STAG_FILTER;
4602 #define IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested, allowed, feature_bit) \
4603 (!(((requested) & (feature_bit)) && \
4604 !((allowed) & (feature_bit))))
4607 * iavf_fix_netdev_vlan_features - fix NETDEV VLAN features based on support
4608 * @adapter: board private structure
4609 * @requested_features: stack requested NETDEV features
4611 static netdev_features_t
4612 iavf_fix_netdev_vlan_features(struct iavf_adapter *adapter,
4613 netdev_features_t requested_features)
4615 netdev_features_t allowed_features;
4617 allowed_features = iavf_get_netdev_vlan_hw_features(adapter) |
4618 iavf_get_netdev_vlan_features(adapter);
4620 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4622 NETIF_F_HW_VLAN_CTAG_TX))
4623 requested_features &= ~NETIF_F_HW_VLAN_CTAG_TX;
4625 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4627 NETIF_F_HW_VLAN_CTAG_RX))
4628 requested_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
4630 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4632 NETIF_F_HW_VLAN_STAG_TX))
4633 requested_features &= ~NETIF_F_HW_VLAN_STAG_TX;
4634 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4636 NETIF_F_HW_VLAN_STAG_RX))
4637 requested_features &= ~NETIF_F_HW_VLAN_STAG_RX;
4639 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4641 NETIF_F_HW_VLAN_CTAG_FILTER))
4642 requested_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
4644 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4646 NETIF_F_HW_VLAN_STAG_FILTER))
4647 requested_features &= ~NETIF_F_HW_VLAN_STAG_FILTER;
4649 if ((requested_features &
4650 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
4651 (requested_features &
4652 (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX)) &&
4653 adapter->vlan_v2_caps.offloads.ethertype_match ==
4654 VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION) {
4655 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");
4656 requested_features &= ~(NETIF_F_HW_VLAN_STAG_RX |
4657 NETIF_F_HW_VLAN_STAG_TX);
4660 return requested_features;
4664 * iavf_fix_features - fix up the netdev feature bits
4665 * @netdev: our net device
4666 * @features: desired feature bits
4668 * Returns fixed-up features bits
4670 static netdev_features_t iavf_fix_features(struct net_device *netdev,
4671 netdev_features_t features)
4673 struct iavf_adapter *adapter = netdev_priv(netdev);
4675 return iavf_fix_netdev_vlan_features(adapter, features);
4678 static const struct net_device_ops iavf_netdev_ops = {
4679 .ndo_open = iavf_open,
4680 .ndo_stop = iavf_close,
4681 .ndo_start_xmit = iavf_xmit_frame,
4682 .ndo_set_rx_mode = iavf_set_rx_mode,
4683 .ndo_validate_addr = eth_validate_addr,
4684 .ndo_set_mac_address = iavf_set_mac,
4685 .ndo_change_mtu = iavf_change_mtu,
4686 .ndo_tx_timeout = iavf_tx_timeout,
4687 .ndo_vlan_rx_add_vid = iavf_vlan_rx_add_vid,
4688 .ndo_vlan_rx_kill_vid = iavf_vlan_rx_kill_vid,
4689 .ndo_features_check = iavf_features_check,
4690 .ndo_fix_features = iavf_fix_features,
4691 .ndo_set_features = iavf_set_features,
4692 .ndo_setup_tc = iavf_setup_tc,
4696 * iavf_check_reset_complete - check that VF reset is complete
4697 * @hw: pointer to hw struct
4699 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
4701 static int iavf_check_reset_complete(struct iavf_hw *hw)
4706 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
4707 rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
4708 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
4709 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
4710 (rstat == VIRTCHNL_VFR_COMPLETED))
4712 usleep_range(10, 20);
4718 * iavf_process_config - Process the config information we got from the PF
4719 * @adapter: board private structure
4721 * Verify that we have a valid config struct, and set up our netdev features
4722 * and our VSI struct.
4724 int iavf_process_config(struct iavf_adapter *adapter)
4726 struct virtchnl_vf_resource *vfres = adapter->vf_res;
4727 netdev_features_t hw_vlan_features, vlan_features;
4728 struct net_device *netdev = adapter->netdev;
4729 netdev_features_t hw_enc_features;
4730 netdev_features_t hw_features;
4732 hw_enc_features = NETIF_F_SG |
4736 NETIF_F_SOFT_FEATURES |
4745 /* advertise to stack only if offloads for encapsulated packets is
4748 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
4749 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4751 NETIF_F_GSO_GRE_CSUM |
4752 NETIF_F_GSO_IPXIP4 |
4753 NETIF_F_GSO_IPXIP6 |
4754 NETIF_F_GSO_UDP_TUNNEL_CSUM |
4755 NETIF_F_GSO_PARTIAL |
4758 if (!(vfres->vf_cap_flags &
4759 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
4760 netdev->gso_partial_features |=
4761 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4763 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
4764 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
4765 netdev->hw_enc_features |= hw_enc_features;
4767 /* record features VLANs can make use of */
4768 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
4770 /* Write features and hw_features separately to avoid polluting
4771 * with, or dropping, features that are set when we registered.
4773 hw_features = hw_enc_features;
4775 /* get HW VLAN features that can be toggled */
4776 hw_vlan_features = iavf_get_netdev_vlan_hw_features(adapter);
4778 /* Enable cloud filter if ADQ is supported */
4779 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
4780 hw_features |= NETIF_F_HW_TC;
4781 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_USO)
4782 hw_features |= NETIF_F_GSO_UDP_L4;
4784 netdev->hw_features |= hw_features | hw_vlan_features;
4785 vlan_features = iavf_get_netdev_vlan_features(adapter);
4787 netdev->features |= hw_features | vlan_features;
4789 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
4790 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4792 netdev->priv_flags |= IFF_UNICAST_FLT;
4794 /* Do not turn on offloads when they are requested to be turned off.
4795 * TSO needs minimum 576 bytes to work correctly.
4797 if (netdev->wanted_features) {
4798 if (!(netdev->wanted_features & NETIF_F_TSO) ||
4800 netdev->features &= ~NETIF_F_TSO;
4801 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
4803 netdev->features &= ~NETIF_F_TSO6;
4804 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
4805 netdev->features &= ~NETIF_F_TSO_ECN;
4806 if (!(netdev->wanted_features & NETIF_F_GRO))
4807 netdev->features &= ~NETIF_F_GRO;
4808 if (!(netdev->wanted_features & NETIF_F_GSO))
4809 netdev->features &= ~NETIF_F_GSO;
4816 * iavf_shutdown - Shutdown the device in preparation for a reboot
4817 * @pdev: pci device structure
4819 static void iavf_shutdown(struct pci_dev *pdev)
4821 struct iavf_adapter *adapter = iavf_pdev_to_adapter(pdev);
4822 struct net_device *netdev = adapter->netdev;
4824 netif_device_detach(netdev);
4826 if (netif_running(netdev))
4829 if (iavf_lock_timeout(&adapter->crit_lock, 5000))
4830 dev_warn(&adapter->pdev->dev, "failed to acquire crit_lock in %s\n", __FUNCTION__);
4831 /* Prevent the watchdog from running. */
4832 iavf_change_state(adapter, __IAVF_REMOVE);
4833 adapter->aq_required = 0;
4834 mutex_unlock(&adapter->crit_lock);
4837 pci_save_state(pdev);
4840 pci_disable_device(pdev);
4844 * iavf_probe - Device Initialization Routine
4845 * @pdev: PCI device information struct
4846 * @ent: entry in iavf_pci_tbl
4848 * Returns 0 on success, negative on failure
4850 * iavf_probe initializes an adapter identified by a pci_dev structure.
4851 * The OS initialization, configuring of the adapter private structure,
4852 * and a hardware reset occur.
4854 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4856 struct net_device *netdev;
4857 struct iavf_adapter *adapter = NULL;
4858 struct iavf_hw *hw = NULL;
4861 err = pci_enable_device(pdev);
4865 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4868 "DMA configuration failed: 0x%x\n", err);
4872 err = pci_request_regions(pdev, iavf_driver_name);
4875 "pci_request_regions failed 0x%x\n", err);
4879 pci_enable_pcie_error_reporting(pdev);
4881 pci_set_master(pdev);
4883 netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
4884 IAVF_MAX_REQ_QUEUES);
4887 goto err_alloc_etherdev;
4890 SET_NETDEV_DEV(netdev, &pdev->dev);
4892 pci_set_drvdata(pdev, netdev);
4893 adapter = netdev_priv(netdev);
4895 adapter->netdev = netdev;
4896 adapter->pdev = pdev;
4901 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
4902 iavf_change_state(adapter, __IAVF_STARTUP);
4904 /* Call save state here because it relies on the adapter struct. */
4905 pci_save_state(pdev);
4907 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4908 pci_resource_len(pdev, 0));
4913 hw->vendor_id = pdev->vendor;
4914 hw->device_id = pdev->device;
4915 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4916 hw->subsystem_vendor_id = pdev->subsystem_vendor;
4917 hw->subsystem_device_id = pdev->subsystem_device;
4918 hw->bus.device = PCI_SLOT(pdev->devfn);
4919 hw->bus.func = PCI_FUNC(pdev->devfn);
4920 hw->bus.bus_id = pdev->bus->number;
4922 /* set up the locks for the AQ, do this only once in probe
4923 * and destroy them only once in remove
4925 mutex_init(&adapter->crit_lock);
4926 mutex_init(&adapter->client_lock);
4927 mutex_init(&hw->aq.asq_mutex);
4928 mutex_init(&hw->aq.arq_mutex);
4930 spin_lock_init(&adapter->mac_vlan_list_lock);
4931 spin_lock_init(&adapter->cloud_filter_list_lock);
4932 spin_lock_init(&adapter->fdir_fltr_lock);
4933 spin_lock_init(&adapter->adv_rss_lock);
4935 INIT_LIST_HEAD(&adapter->mac_filter_list);
4936 INIT_LIST_HEAD(&adapter->vlan_filter_list);
4937 INIT_LIST_HEAD(&adapter->cloud_filter_list);
4938 INIT_LIST_HEAD(&adapter->fdir_list_head);
4939 INIT_LIST_HEAD(&adapter->adv_rss_list_head);
4941 INIT_WORK(&adapter->reset_task, iavf_reset_task);
4942 INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
4943 INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
4944 INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
4945 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
4946 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
4948 /* Setup the wait queue for indicating transition to down status */
4949 init_waitqueue_head(&adapter->down_waitqueue);
4951 /* Setup the wait queue for indicating virtchannel events */
4952 init_waitqueue_head(&adapter->vc_waitqueue);
4957 free_netdev(netdev);
4959 pci_disable_pcie_error_reporting(pdev);
4960 pci_release_regions(pdev);
4963 pci_disable_device(pdev);
4968 * iavf_suspend - Power management suspend routine
4969 * @dev_d: device info pointer
4971 * Called when the system (VM) is entering sleep/suspend.
4973 static int __maybe_unused iavf_suspend(struct device *dev_d)
4975 struct net_device *netdev = dev_get_drvdata(dev_d);
4976 struct iavf_adapter *adapter = netdev_priv(netdev);
4978 netif_device_detach(netdev);
4980 while (!mutex_trylock(&adapter->crit_lock))
4981 usleep_range(500, 1000);
4983 if (netif_running(netdev)) {
4988 iavf_free_misc_irq(adapter);
4989 iavf_reset_interrupt_capability(adapter);
4991 mutex_unlock(&adapter->crit_lock);
4997 * iavf_resume - Power management resume routine
4998 * @dev_d: device info pointer
5000 * Called when the system (VM) is resumed from sleep/suspend.
5002 static int __maybe_unused iavf_resume(struct device *dev_d)
5004 struct pci_dev *pdev = to_pci_dev(dev_d);
5005 struct iavf_adapter *adapter;
5008 adapter = iavf_pdev_to_adapter(pdev);
5010 pci_set_master(pdev);
5013 err = iavf_set_interrupt_capability(adapter);
5016 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
5019 err = iavf_request_misc_irq(adapter);
5022 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
5026 queue_work(iavf_wq, &adapter->reset_task);
5028 netif_device_attach(adapter->netdev);
5034 * iavf_remove - Device Removal Routine
5035 * @pdev: PCI device information struct
5037 * iavf_remove is called by the PCI subsystem to alert the driver
5038 * that it should release a PCI device. The could be caused by a
5039 * Hot-Plug event, or because the driver is going to be removed from
5042 static void iavf_remove(struct pci_dev *pdev)
5044 struct iavf_adapter *adapter = iavf_pdev_to_adapter(pdev);
5045 struct iavf_fdir_fltr *fdir, *fdirtmp;
5046 struct iavf_vlan_filter *vlf, *vlftmp;
5047 struct iavf_cloud_filter *cf, *cftmp;
5048 struct iavf_adv_rss *rss, *rsstmp;
5049 struct iavf_mac_filter *f, *ftmp;
5050 struct net_device *netdev;
5054 netdev = adapter->netdev;
5057 if (test_and_set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
5060 /* Wait until port initialization is complete.
5061 * There are flows where register/unregister netdev may race.
5064 mutex_lock(&adapter->crit_lock);
5065 if (adapter->state == __IAVF_RUNNING ||
5066 adapter->state == __IAVF_DOWN ||
5067 adapter->state == __IAVF_INIT_FAILED) {
5068 mutex_unlock(&adapter->crit_lock);
5072 mutex_unlock(&adapter->crit_lock);
5073 usleep_range(500, 1000);
5075 cancel_delayed_work_sync(&adapter->watchdog_task);
5077 if (adapter->netdev_registered) {
5079 unregister_netdevice(netdev);
5080 adapter->netdev_registered = false;
5083 if (CLIENT_ALLOWED(adapter)) {
5084 err = iavf_lan_del_device(adapter);
5086 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
5090 mutex_lock(&adapter->crit_lock);
5091 dev_info(&adapter->pdev->dev, "Remove device\n");
5092 iavf_change_state(adapter, __IAVF_REMOVE);
5094 iavf_request_reset(adapter);
5096 /* If the FW isn't responding, kick it once, but only once. */
5097 if (!iavf_asq_done(hw)) {
5098 iavf_request_reset(adapter);
5102 iavf_misc_irq_disable(adapter);
5103 /* Shut down all the garbage mashers on the detention level */
5104 cancel_work_sync(&adapter->reset_task);
5105 cancel_delayed_work_sync(&adapter->watchdog_task);
5106 cancel_work_sync(&adapter->adminq_task);
5107 cancel_delayed_work_sync(&adapter->client_task);
5109 adapter->aq_required = 0;
5110 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
5112 iavf_free_all_tx_resources(adapter);
5113 iavf_free_all_rx_resources(adapter);
5114 iavf_free_misc_irq(adapter);
5116 iavf_reset_interrupt_capability(adapter);
5117 iavf_free_q_vectors(adapter);
5119 iavf_free_rss(adapter);
5121 if (hw->aq.asq.count)
5122 iavf_shutdown_adminq(hw);
5124 /* destroy the locks only once, here */
5125 mutex_destroy(&hw->aq.arq_mutex);
5126 mutex_destroy(&hw->aq.asq_mutex);
5127 mutex_destroy(&adapter->client_lock);
5128 mutex_unlock(&adapter->crit_lock);
5129 mutex_destroy(&adapter->crit_lock);
5131 iounmap(hw->hw_addr);
5132 pci_release_regions(pdev);
5133 iavf_free_queues(adapter);
5134 kfree(adapter->vf_res);
5135 spin_lock_bh(&adapter->mac_vlan_list_lock);
5136 /* If we got removed before an up/down sequence, we've got a filter
5137 * hanging out there that we need to get rid of.
5139 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
5143 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
5145 list_del(&vlf->list);
5149 spin_unlock_bh(&adapter->mac_vlan_list_lock);
5151 spin_lock_bh(&adapter->cloud_filter_list_lock);
5152 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
5153 list_del(&cf->list);
5156 spin_unlock_bh(&adapter->cloud_filter_list_lock);
5158 spin_lock_bh(&adapter->fdir_fltr_lock);
5159 list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head, list) {
5160 list_del(&fdir->list);
5163 spin_unlock_bh(&adapter->fdir_fltr_lock);
5165 spin_lock_bh(&adapter->adv_rss_lock);
5166 list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
5168 list_del(&rss->list);
5171 spin_unlock_bh(&adapter->adv_rss_lock);
5173 free_netdev(netdev);
5175 pci_disable_pcie_error_reporting(pdev);
5177 pci_disable_device(pdev);
5180 static SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
5182 static struct pci_driver iavf_driver = {
5183 .name = iavf_driver_name,
5184 .id_table = iavf_pci_tbl,
5185 .probe = iavf_probe,
5186 .remove = iavf_remove,
5187 .driver.pm = &iavf_pm_ops,
5188 .shutdown = iavf_shutdown,
5192 * iavf_init_module - Driver Registration Routine
5194 * iavf_init_module is the first routine called when the driver is
5195 * loaded. All it does is register with the PCI subsystem.
5197 static int __init iavf_init_module(void)
5201 pr_info("iavf: %s\n", iavf_driver_string);
5203 pr_info("%s\n", iavf_copyright);
5205 iavf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
5208 pr_err("%s: Failed to create workqueue\n", iavf_driver_name);
5212 ret = pci_register_driver(&iavf_driver);
5214 destroy_workqueue(iavf_wq);
5219 module_init(iavf_init_module);
5222 * iavf_exit_module - Driver Exit Cleanup Routine
5224 * iavf_exit_module is called just before the driver is removed
5227 static void __exit iavf_exit_module(void)
5229 pci_unregister_driver(&iavf_driver);
5230 destroy_workqueue(iavf_wq);
5233 module_exit(iavf_exit_module);