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;
53 int iavf_status_to_errno(enum iavf_status status)
59 case IAVF_ERR_MAC_TYPE:
60 case IAVF_ERR_INVALID_MAC_ADDR:
61 case IAVF_ERR_INVALID_LINK_SETTINGS:
62 case IAVF_ERR_INVALID_PD_ID:
63 case IAVF_ERR_INVALID_QP_ID:
64 case IAVF_ERR_INVALID_CQ_ID:
65 case IAVF_ERR_INVALID_CEQ_ID:
66 case IAVF_ERR_INVALID_AEQ_ID:
67 case IAVF_ERR_INVALID_SIZE:
68 case IAVF_ERR_INVALID_ARP_INDEX:
69 case IAVF_ERR_INVALID_FPM_FUNC_ID:
70 case IAVF_ERR_QP_INVALID_MSG_SIZE:
71 case IAVF_ERR_INVALID_FRAG_COUNT:
72 case IAVF_ERR_INVALID_ALIGNMENT:
73 case IAVF_ERR_INVALID_PUSH_PAGE_INDEX:
74 case IAVF_ERR_INVALID_IMM_DATA_SIZE:
75 case IAVF_ERR_INVALID_VF_ID:
76 case IAVF_ERR_INVALID_HMCFN_ID:
77 case IAVF_ERR_INVALID_PBLE_INDEX:
78 case IAVF_ERR_INVALID_SD_INDEX:
79 case IAVF_ERR_INVALID_PAGE_DESC_INDEX:
80 case IAVF_ERR_INVALID_SD_TYPE:
81 case IAVF_ERR_INVALID_HMC_OBJ_INDEX:
82 case IAVF_ERR_INVALID_HMC_OBJ_COUNT:
83 case IAVF_ERR_INVALID_SRQ_ARM_LIMIT:
86 case IAVF_ERR_NVM_CHECKSUM:
89 case IAVF_ERR_UNKNOWN_PHY:
90 case IAVF_ERR_LINK_SETUP:
91 case IAVF_ERR_ADAPTER_STOPPED:
92 case IAVF_ERR_PRIMARY_REQUESTS_PENDING:
93 case IAVF_ERR_AUTONEG_NOT_COMPLETE:
94 case IAVF_ERR_RESET_FAILED:
95 case IAVF_ERR_BAD_PTR:
96 case IAVF_ERR_SWFW_SYNC:
97 case IAVF_ERR_QP_TOOMANY_WRS_POSTED:
98 case IAVF_ERR_QUEUE_EMPTY:
99 case IAVF_ERR_FLUSHED_QUEUE:
100 case IAVF_ERR_OPCODE_MISMATCH:
101 case IAVF_ERR_CQP_COMPL_ERROR:
102 case IAVF_ERR_BACKING_PAGE_ERROR:
103 case IAVF_ERR_NO_PBLCHUNKS_AVAILABLE:
104 case IAVF_ERR_MEMCPY_FAILED:
105 case IAVF_ERR_SRQ_ENABLED:
106 case IAVF_ERR_ADMIN_QUEUE_ERROR:
107 case IAVF_ERR_ADMIN_QUEUE_FULL:
108 case IAVF_ERR_BAD_RDMA_CQE:
109 case IAVF_ERR_NVM_BLANK_MODE:
110 case IAVF_ERR_PE_DOORBELL_NOT_ENABLED:
111 case IAVF_ERR_DIAG_TEST_FAILED:
112 case IAVF_ERR_FIRMWARE_API_VERSION:
113 case IAVF_ERR_ADMIN_QUEUE_CRITICAL_ERROR:
115 case IAVF_ERR_DEVICE_NOT_SUPPORTED:
117 case IAVF_ERR_NO_AVAILABLE_VSI:
118 case IAVF_ERR_RING_FULL:
120 case IAVF_ERR_NO_MEMORY:
122 case IAVF_ERR_TIMEOUT:
123 case IAVF_ERR_ADMIN_QUEUE_TIMEOUT:
125 case IAVF_ERR_NOT_IMPLEMENTED:
126 case IAVF_NOT_SUPPORTED:
128 case IAVF_ERR_ADMIN_QUEUE_NO_WORK:
130 case IAVF_ERR_NOT_READY:
132 case IAVF_ERR_BUF_TOO_SHORT:
139 int virtchnl_status_to_errno(enum virtchnl_status_code v_status)
142 case VIRTCHNL_STATUS_SUCCESS:
144 case VIRTCHNL_STATUS_ERR_PARAM:
145 case VIRTCHNL_STATUS_ERR_INVALID_VF_ID:
147 case VIRTCHNL_STATUS_ERR_NO_MEMORY:
149 case VIRTCHNL_STATUS_ERR_OPCODE_MISMATCH:
150 case VIRTCHNL_STATUS_ERR_CQP_COMPL_ERROR:
151 case VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR:
153 case VIRTCHNL_STATUS_ERR_NOT_SUPPORTED:
161 * iavf_pdev_to_adapter - go from pci_dev to adapter
162 * @pdev: pci_dev pointer
164 static struct iavf_adapter *iavf_pdev_to_adapter(struct pci_dev *pdev)
166 return netdev_priv(pci_get_drvdata(pdev));
170 * iavf_is_reset_in_progress - Check if a reset is in progress
171 * @adapter: board private structure
173 static bool iavf_is_reset_in_progress(struct iavf_adapter *adapter)
175 if (adapter->state == __IAVF_RESETTING ||
176 adapter->flags & (IAVF_FLAG_RESET_PENDING |
177 IAVF_FLAG_RESET_NEEDED))
184 * iavf_wait_for_reset - Wait for reset to finish.
185 * @adapter: board private structure
187 * Returns 0 if reset finished successfully, negative on timeout or interrupt.
189 int iavf_wait_for_reset(struct iavf_adapter *adapter)
191 int ret = wait_event_interruptible_timeout(adapter->reset_waitqueue,
192 !iavf_is_reset_in_progress(adapter),
193 msecs_to_jiffies(5000));
195 /* If ret < 0 then it means wait was interrupted.
196 * If ret == 0 then it means we got a timeout while waiting
197 * for reset to finish.
198 * If ret > 0 it means reset has finished.
209 * iavf_allocate_dma_mem_d - OS specific memory alloc for shared code
210 * @hw: pointer to the HW structure
211 * @mem: ptr to mem struct to fill out
212 * @size: size of memory requested
213 * @alignment: what to align the allocation to
215 enum iavf_status iavf_allocate_dma_mem_d(struct iavf_hw *hw,
216 struct iavf_dma_mem *mem,
217 u64 size, u32 alignment)
219 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
222 return IAVF_ERR_PARAM;
224 mem->size = ALIGN(size, alignment);
225 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
226 (dma_addr_t *)&mem->pa, GFP_KERNEL);
230 return IAVF_ERR_NO_MEMORY;
234 * iavf_free_dma_mem - wrapper for DMA memory freeing
235 * @hw: pointer to the HW structure
236 * @mem: ptr to mem struct to free
238 enum iavf_status iavf_free_dma_mem(struct iavf_hw *hw, struct iavf_dma_mem *mem)
240 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
242 if (!mem || !mem->va)
243 return IAVF_ERR_PARAM;
244 dma_free_coherent(&adapter->pdev->dev, mem->size,
245 mem->va, (dma_addr_t)mem->pa);
250 * iavf_allocate_virt_mem - virt memory alloc wrapper
251 * @hw: pointer to the HW structure
252 * @mem: ptr to mem struct to fill out
253 * @size: size of memory requested
255 enum iavf_status iavf_allocate_virt_mem(struct iavf_hw *hw,
256 struct iavf_virt_mem *mem, u32 size)
259 return IAVF_ERR_PARAM;
262 mem->va = kzalloc(size, GFP_KERNEL);
267 return IAVF_ERR_NO_MEMORY;
271 * iavf_free_virt_mem - virt memory free wrapper
272 * @hw: pointer to the HW structure
273 * @mem: ptr to mem struct to free
275 void iavf_free_virt_mem(struct iavf_hw *hw, struct iavf_virt_mem *mem)
281 * iavf_lock_timeout - try to lock mutex but give up after timeout
282 * @lock: mutex that should be locked
283 * @msecs: timeout in msecs
285 * Returns 0 on success, negative on failure
287 static int iavf_lock_timeout(struct mutex *lock, unsigned int msecs)
289 unsigned int wait, delay = 10;
291 for (wait = 0; wait < msecs; wait += delay) {
292 if (mutex_trylock(lock))
302 * iavf_schedule_reset - Set the flags and schedule a reset event
303 * @adapter: board private structure
304 * @flags: IAVF_FLAG_RESET_PENDING or IAVF_FLAG_RESET_NEEDED
306 void iavf_schedule_reset(struct iavf_adapter *adapter, u64 flags)
308 if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section) &&
310 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
311 adapter->flags |= flags;
312 queue_work(adapter->wq, &adapter->reset_task);
317 * iavf_schedule_request_stats - Set the flags and schedule statistics request
318 * @adapter: board private structure
320 * Sets IAVF_FLAG_AQ_REQUEST_STATS flag so iavf_watchdog_task() will explicitly
321 * request and refresh ethtool stats
323 void iavf_schedule_request_stats(struct iavf_adapter *adapter)
325 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_STATS;
326 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
330 * iavf_tx_timeout - Respond to a Tx Hang
331 * @netdev: network interface device structure
332 * @txqueue: queue number that is timing out
334 static void iavf_tx_timeout(struct net_device *netdev, unsigned int txqueue)
336 struct iavf_adapter *adapter = netdev_priv(netdev);
338 adapter->tx_timeout_count++;
339 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
343 * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
344 * @adapter: board private structure
346 static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
348 struct iavf_hw *hw = &adapter->hw;
350 if (!adapter->msix_entries)
353 wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
357 synchronize_irq(adapter->msix_entries[0].vector);
361 * iavf_misc_irq_enable - Enable default interrupt generation settings
362 * @adapter: board private structure
364 static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
366 struct iavf_hw *hw = &adapter->hw;
368 wr32(hw, IAVF_VFINT_DYN_CTL01, IAVF_VFINT_DYN_CTL01_INTENA_MASK |
369 IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
370 wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
376 * iavf_irq_disable - Mask off interrupt generation on the NIC
377 * @adapter: board private structure
379 static void iavf_irq_disable(struct iavf_adapter *adapter)
382 struct iavf_hw *hw = &adapter->hw;
384 if (!adapter->msix_entries)
387 for (i = 1; i < adapter->num_msix_vectors; i++) {
388 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1), 0);
389 synchronize_irq(adapter->msix_entries[i].vector);
395 * iavf_irq_enable_queues - Enable interrupt for all queues
396 * @adapter: board private structure
398 static void iavf_irq_enable_queues(struct iavf_adapter *adapter)
400 struct iavf_hw *hw = &adapter->hw;
403 for (i = 1; i < adapter->num_msix_vectors; i++) {
404 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1),
405 IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
406 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
411 * iavf_irq_enable - Enable default interrupt generation settings
412 * @adapter: board private structure
413 * @flush: boolean value whether to run rd32()
415 void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
417 struct iavf_hw *hw = &adapter->hw;
419 iavf_misc_irq_enable(adapter);
420 iavf_irq_enable_queues(adapter);
427 * iavf_msix_aq - Interrupt handler for vector 0
428 * @irq: interrupt number
429 * @data: pointer to netdev
431 static irqreturn_t iavf_msix_aq(int irq, void *data)
433 struct net_device *netdev = data;
434 struct iavf_adapter *adapter = netdev_priv(netdev);
435 struct iavf_hw *hw = &adapter->hw;
437 /* handle non-queue interrupts, these reads clear the registers */
438 rd32(hw, IAVF_VFINT_ICR01);
439 rd32(hw, IAVF_VFINT_ICR0_ENA1);
441 if (adapter->state != __IAVF_REMOVE)
442 /* schedule work on the private workqueue */
443 queue_work(adapter->wq, &adapter->adminq_task);
449 * iavf_msix_clean_rings - MSIX mode Interrupt Handler
450 * @irq: interrupt number
451 * @data: pointer to a q_vector
453 static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
455 struct iavf_q_vector *q_vector = data;
457 if (!q_vector->tx.ring && !q_vector->rx.ring)
460 napi_schedule_irqoff(&q_vector->napi);
466 * iavf_map_vector_to_rxq - associate irqs with rx queues
467 * @adapter: board private structure
468 * @v_idx: interrupt number
469 * @r_idx: queue number
472 iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
474 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
475 struct iavf_ring *rx_ring = &adapter->rx_rings[r_idx];
476 struct iavf_hw *hw = &adapter->hw;
478 rx_ring->q_vector = q_vector;
479 rx_ring->next = q_vector->rx.ring;
480 rx_ring->vsi = &adapter->vsi;
481 q_vector->rx.ring = rx_ring;
482 q_vector->rx.count++;
483 q_vector->rx.next_update = jiffies + 1;
484 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
485 q_vector->ring_mask |= BIT(r_idx);
486 wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, q_vector->reg_idx),
487 q_vector->rx.current_itr >> 1);
488 q_vector->rx.current_itr = q_vector->rx.target_itr;
492 * iavf_map_vector_to_txq - associate irqs with tx queues
493 * @adapter: board private structure
494 * @v_idx: interrupt number
495 * @t_idx: queue number
498 iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
500 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
501 struct iavf_ring *tx_ring = &adapter->tx_rings[t_idx];
502 struct iavf_hw *hw = &adapter->hw;
504 tx_ring->q_vector = q_vector;
505 tx_ring->next = q_vector->tx.ring;
506 tx_ring->vsi = &adapter->vsi;
507 q_vector->tx.ring = tx_ring;
508 q_vector->tx.count++;
509 q_vector->tx.next_update = jiffies + 1;
510 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
511 q_vector->num_ringpairs++;
512 wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, q_vector->reg_idx),
513 q_vector->tx.target_itr >> 1);
514 q_vector->tx.current_itr = q_vector->tx.target_itr;
518 * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
519 * @adapter: board private structure to initialize
521 * This function maps descriptor rings to the queue-specific vectors
522 * we were allotted through the MSI-X enabling code. Ideally, we'd have
523 * one vector per ring/queue, but on a constrained vector budget, we
524 * group the rings as "efficiently" as possible. You would add new
525 * mapping configurations in here.
527 static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
529 int rings_remaining = adapter->num_active_queues;
530 int ridx = 0, vidx = 0;
533 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
535 for (; ridx < rings_remaining; ridx++) {
536 iavf_map_vector_to_rxq(adapter, vidx, ridx);
537 iavf_map_vector_to_txq(adapter, vidx, ridx);
539 /* In the case where we have more queues than vectors, continue
540 * round-robin on vectors until all queues are mapped.
542 if (++vidx >= q_vectors)
546 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
550 * iavf_irq_affinity_notify - Callback for affinity changes
551 * @notify: context as to what irq was changed
552 * @mask: the new affinity mask
554 * This is a callback function used by the irq_set_affinity_notifier function
555 * so that we may register to receive changes to the irq affinity masks.
557 static void iavf_irq_affinity_notify(struct irq_affinity_notify *notify,
558 const cpumask_t *mask)
560 struct iavf_q_vector *q_vector =
561 container_of(notify, struct iavf_q_vector, affinity_notify);
563 cpumask_copy(&q_vector->affinity_mask, mask);
567 * iavf_irq_affinity_release - Callback for affinity notifier release
568 * @ref: internal core kernel usage
570 * This is a callback function used by the irq_set_affinity_notifier function
571 * to inform the current notification subscriber that they will no longer
572 * receive notifications.
574 static void iavf_irq_affinity_release(struct kref *ref) {}
577 * iavf_request_traffic_irqs - Initialize MSI-X interrupts
578 * @adapter: board private structure
579 * @basename: device basename
581 * Allocates MSI-X vectors for tx and rx handling, and requests
582 * interrupts from the kernel.
585 iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
587 unsigned int vector, q_vectors;
588 unsigned int rx_int_idx = 0, tx_int_idx = 0;
592 iavf_irq_disable(adapter);
593 /* Decrement for Other and TCP Timer vectors */
594 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
596 for (vector = 0; vector < q_vectors; vector++) {
597 struct iavf_q_vector *q_vector = &adapter->q_vectors[vector];
599 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
601 if (q_vector->tx.ring && q_vector->rx.ring) {
602 snprintf(q_vector->name, sizeof(q_vector->name),
603 "iavf-%s-TxRx-%u", basename, rx_int_idx++);
605 } else if (q_vector->rx.ring) {
606 snprintf(q_vector->name, sizeof(q_vector->name),
607 "iavf-%s-rx-%u", basename, rx_int_idx++);
608 } else if (q_vector->tx.ring) {
609 snprintf(q_vector->name, sizeof(q_vector->name),
610 "iavf-%s-tx-%u", basename, tx_int_idx++);
612 /* skip this unused q_vector */
615 err = request_irq(irq_num,
616 iavf_msix_clean_rings,
621 dev_info(&adapter->pdev->dev,
622 "Request_irq failed, error: %d\n", err);
623 goto free_queue_irqs;
625 /* register for affinity change notifications */
626 q_vector->affinity_notify.notify = iavf_irq_affinity_notify;
627 q_vector->affinity_notify.release =
628 iavf_irq_affinity_release;
629 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
630 /* Spread the IRQ affinity hints across online CPUs. Note that
631 * get_cpu_mask returns a mask with a permanent lifetime so
632 * it's safe to use as a hint for irq_update_affinity_hint.
634 cpu = cpumask_local_spread(q_vector->v_idx, -1);
635 irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
643 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
644 irq_set_affinity_notifier(irq_num, NULL);
645 irq_update_affinity_hint(irq_num, NULL);
646 free_irq(irq_num, &adapter->q_vectors[vector]);
652 * iavf_request_misc_irq - Initialize MSI-X interrupts
653 * @adapter: board private structure
655 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
656 * vector is only for the admin queue, and stays active even when the netdev
659 static int iavf_request_misc_irq(struct iavf_adapter *adapter)
661 struct net_device *netdev = adapter->netdev;
664 snprintf(adapter->misc_vector_name,
665 sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
666 dev_name(&adapter->pdev->dev));
667 err = request_irq(adapter->msix_entries[0].vector,
669 adapter->misc_vector_name, netdev);
671 dev_err(&adapter->pdev->dev,
672 "request_irq for %s failed: %d\n",
673 adapter->misc_vector_name, err);
674 free_irq(adapter->msix_entries[0].vector, netdev);
680 * iavf_free_traffic_irqs - Free MSI-X interrupts
681 * @adapter: board private structure
683 * Frees all MSI-X vectors other than 0.
685 static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
687 int vector, irq_num, q_vectors;
689 if (!adapter->msix_entries)
692 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
694 for (vector = 0; vector < q_vectors; vector++) {
695 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
696 irq_set_affinity_notifier(irq_num, NULL);
697 irq_update_affinity_hint(irq_num, NULL);
698 free_irq(irq_num, &adapter->q_vectors[vector]);
703 * iavf_free_misc_irq - Free MSI-X miscellaneous vector
704 * @adapter: board private structure
706 * Frees MSI-X vector 0.
708 static void iavf_free_misc_irq(struct iavf_adapter *adapter)
710 struct net_device *netdev = adapter->netdev;
712 if (!adapter->msix_entries)
715 free_irq(adapter->msix_entries[0].vector, netdev);
719 * iavf_configure_tx - Configure Transmit Unit after Reset
720 * @adapter: board private structure
722 * Configure the Tx unit of the MAC after a reset.
724 static void iavf_configure_tx(struct iavf_adapter *adapter)
726 struct iavf_hw *hw = &adapter->hw;
729 for (i = 0; i < adapter->num_active_queues; i++)
730 adapter->tx_rings[i].tail = hw->hw_addr + IAVF_QTX_TAIL1(i);
734 * iavf_configure_rx - Configure Receive Unit after Reset
735 * @adapter: board private structure
737 * Configure the Rx unit of the MAC after a reset.
739 static void iavf_configure_rx(struct iavf_adapter *adapter)
741 unsigned int rx_buf_len = IAVF_RXBUFFER_2048;
742 struct iavf_hw *hw = &adapter->hw;
745 /* Legacy Rx will always default to a 2048 buffer size. */
746 #if (PAGE_SIZE < 8192)
747 if (!(adapter->flags & IAVF_FLAG_LEGACY_RX)) {
748 struct net_device *netdev = adapter->netdev;
750 /* For jumbo frames on systems with 4K pages we have to use
751 * an order 1 page, so we might as well increase the size
752 * of our Rx buffer to make better use of the available space
754 rx_buf_len = IAVF_RXBUFFER_3072;
756 /* We use a 1536 buffer size for configurations with
757 * standard Ethernet mtu. On x86 this gives us enough room
758 * for shared info and 192 bytes of padding.
760 if (!IAVF_2K_TOO_SMALL_WITH_PADDING &&
761 (netdev->mtu <= ETH_DATA_LEN))
762 rx_buf_len = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
766 for (i = 0; i < adapter->num_active_queues; i++) {
767 adapter->rx_rings[i].tail = hw->hw_addr + IAVF_QRX_TAIL1(i);
768 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
770 if (adapter->flags & IAVF_FLAG_LEGACY_RX)
771 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
773 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
778 * iavf_find_vlan - Search filter list for specific vlan filter
779 * @adapter: board private structure
782 * Returns ptr to the filter object or NULL. Must be called while holding the
783 * mac_vlan_list_lock.
786 iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter,
787 struct iavf_vlan vlan)
789 struct iavf_vlan_filter *f;
791 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
792 if (f->vlan.vid == vlan.vid &&
793 f->vlan.tpid == vlan.tpid)
801 * iavf_add_vlan - Add a vlan filter to the list
802 * @adapter: board private structure
805 * Returns ptr to the filter object or NULL when no memory available.
808 iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter,
809 struct iavf_vlan vlan)
811 struct iavf_vlan_filter *f = NULL;
813 spin_lock_bh(&adapter->mac_vlan_list_lock);
815 f = iavf_find_vlan(adapter, vlan);
817 f = kzalloc(sizeof(*f), GFP_ATOMIC);
823 list_add_tail(&f->list, &adapter->vlan_filter_list);
824 f->state = IAVF_VLAN_ADD;
825 adapter->num_vlan_filters++;
826 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
830 spin_unlock_bh(&adapter->mac_vlan_list_lock);
835 * iavf_del_vlan - Remove a vlan filter from the list
836 * @adapter: board private structure
839 static void iavf_del_vlan(struct iavf_adapter *adapter, struct iavf_vlan vlan)
841 struct iavf_vlan_filter *f;
843 spin_lock_bh(&adapter->mac_vlan_list_lock);
845 f = iavf_find_vlan(adapter, vlan);
847 f->state = IAVF_VLAN_REMOVE;
848 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
851 spin_unlock_bh(&adapter->mac_vlan_list_lock);
855 * iavf_restore_filters
856 * @adapter: board private structure
858 * Restore existing non MAC filters when VF netdev comes back up
860 static void iavf_restore_filters(struct iavf_adapter *adapter)
862 struct iavf_vlan_filter *f;
864 /* re-add all VLAN filters */
865 spin_lock_bh(&adapter->mac_vlan_list_lock);
867 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
868 if (f->state == IAVF_VLAN_INACTIVE)
869 f->state = IAVF_VLAN_ADD;
872 spin_unlock_bh(&adapter->mac_vlan_list_lock);
873 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
877 * iavf_get_num_vlans_added - get number of VLANs added
878 * @adapter: board private structure
880 u16 iavf_get_num_vlans_added(struct iavf_adapter *adapter)
882 return adapter->num_vlan_filters;
886 * iavf_get_max_vlans_allowed - get maximum VLANs allowed for this VF
887 * @adapter: board private structure
889 * This depends on the negotiated VLAN capability. For VIRTCHNL_VF_OFFLOAD_VLAN,
890 * do not impose a limit as that maintains current behavior and for
891 * VIRTCHNL_VF_OFFLOAD_VLAN_V2, use the maximum allowed sent from the PF.
893 static u16 iavf_get_max_vlans_allowed(struct iavf_adapter *adapter)
895 /* don't impose any limit for VIRTCHNL_VF_OFFLOAD_VLAN since there has
896 * never been a limit on the VF driver side
898 if (VLAN_ALLOWED(adapter))
900 else if (VLAN_V2_ALLOWED(adapter))
901 return adapter->vlan_v2_caps.filtering.max_filters;
907 * iavf_max_vlans_added - check if maximum VLANs allowed already exist
908 * @adapter: board private structure
910 static bool iavf_max_vlans_added(struct iavf_adapter *adapter)
912 if (iavf_get_num_vlans_added(adapter) <
913 iavf_get_max_vlans_allowed(adapter))
920 * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
921 * @netdev: network device struct
922 * @proto: unused protocol data
925 static int iavf_vlan_rx_add_vid(struct net_device *netdev,
926 __always_unused __be16 proto, u16 vid)
928 struct iavf_adapter *adapter = netdev_priv(netdev);
930 /* Do not track VLAN 0 filter, always added by the PF on VF init */
934 if (!VLAN_FILTERING_ALLOWED(adapter))
937 if (iavf_max_vlans_added(adapter)) {
938 netdev_err(netdev, "Max allowed VLAN filters %u. Remove existing VLANs or disable filtering via Ethtool if supported.\n",
939 iavf_get_max_vlans_allowed(adapter));
943 if (!iavf_add_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto))))
950 * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
951 * @netdev: network device struct
952 * @proto: unused protocol data
955 static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
956 __always_unused __be16 proto, u16 vid)
958 struct iavf_adapter *adapter = netdev_priv(netdev);
960 /* We do not track VLAN 0 filter */
964 iavf_del_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto)));
969 * iavf_find_filter - Search filter list for specific mac filter
970 * @adapter: board private structure
971 * @macaddr: the MAC address
973 * Returns ptr to the filter object or NULL. Must be called while holding the
974 * mac_vlan_list_lock.
977 iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
980 struct iavf_mac_filter *f;
985 list_for_each_entry(f, &adapter->mac_filter_list, list) {
986 if (ether_addr_equal(macaddr, f->macaddr))
993 * iavf_add_filter - Add a mac filter to the filter list
994 * @adapter: board private structure
995 * @macaddr: the MAC address
997 * Returns ptr to the filter object or NULL when no memory available.
999 struct iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
1002 struct iavf_mac_filter *f;
1007 f = iavf_find_filter(adapter, macaddr);
1009 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1013 ether_addr_copy(f->macaddr, macaddr);
1015 list_add_tail(&f->list, &adapter->mac_filter_list);
1017 f->add_handled = false;
1018 f->is_new_mac = true;
1019 f->is_primary = ether_addr_equal(macaddr, adapter->hw.mac.addr);
1020 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
1029 * iavf_replace_primary_mac - Replace current primary address
1030 * @adapter: board private structure
1031 * @new_mac: new MAC address to be applied
1033 * Replace current dev_addr and send request to PF for removal of previous
1034 * primary MAC address filter and addition of new primary MAC filter.
1035 * Return 0 for success, -ENOMEM for failure.
1037 * Do not call this with mac_vlan_list_lock!
1039 static int iavf_replace_primary_mac(struct iavf_adapter *adapter,
1042 struct iavf_hw *hw = &adapter->hw;
1043 struct iavf_mac_filter *new_f;
1044 struct iavf_mac_filter *old_f;
1046 spin_lock_bh(&adapter->mac_vlan_list_lock);
1048 new_f = iavf_add_filter(adapter, new_mac);
1050 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1054 old_f = iavf_find_filter(adapter, hw->mac.addr);
1056 old_f->is_primary = false;
1057 old_f->remove = true;
1058 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1060 /* Always send the request to add if changing primary MAC,
1061 * even if filter is already present on the list
1063 new_f->is_primary = true;
1065 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
1066 ether_addr_copy(hw->mac.addr, new_mac);
1068 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1070 /* schedule the watchdog task to immediately process the request */
1071 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
1076 * iavf_is_mac_set_handled - wait for a response to set MAC from PF
1077 * @netdev: network interface device structure
1078 * @macaddr: MAC address to set
1080 * Returns true on success, false on failure
1082 static bool iavf_is_mac_set_handled(struct net_device *netdev,
1085 struct iavf_adapter *adapter = netdev_priv(netdev);
1086 struct iavf_mac_filter *f;
1089 spin_lock_bh(&adapter->mac_vlan_list_lock);
1091 f = iavf_find_filter(adapter, macaddr);
1093 if (!f || (!f->add && f->add_handled))
1096 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1102 * iavf_set_mac - NDO callback to set port MAC address
1103 * @netdev: network interface device structure
1104 * @p: pointer to an address structure
1106 * Returns 0 on success, negative on failure
1108 static int iavf_set_mac(struct net_device *netdev, void *p)
1110 struct iavf_adapter *adapter = netdev_priv(netdev);
1111 struct sockaddr *addr = p;
1114 if (!is_valid_ether_addr(addr->sa_data))
1115 return -EADDRNOTAVAIL;
1117 ret = iavf_replace_primary_mac(adapter, addr->sa_data);
1122 ret = wait_event_interruptible_timeout(adapter->vc_waitqueue,
1123 iavf_is_mac_set_handled(netdev, addr->sa_data),
1124 msecs_to_jiffies(2500));
1126 /* If ret < 0 then it means wait was interrupted.
1127 * If ret == 0 then it means we got a timeout.
1128 * else it means we got response for set MAC from PF,
1129 * check if netdev MAC was updated to requested MAC,
1130 * if yes then set MAC succeeded otherwise it failed return -EACCES
1138 if (!ether_addr_equal(netdev->dev_addr, addr->sa_data))
1145 * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
1146 * @netdev: the netdevice
1147 * @addr: address to add
1149 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1150 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1152 static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
1154 struct iavf_adapter *adapter = netdev_priv(netdev);
1156 if (iavf_add_filter(adapter, addr))
1163 * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1164 * @netdev: the netdevice
1165 * @addr: address to add
1167 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1168 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1170 static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
1172 struct iavf_adapter *adapter = netdev_priv(netdev);
1173 struct iavf_mac_filter *f;
1175 /* Under some circumstances, we might receive a request to delete
1176 * our own device address from our uc list. Because we store the
1177 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1178 * such requests and not delete our device address from this list.
1180 if (ether_addr_equal(addr, netdev->dev_addr))
1183 f = iavf_find_filter(adapter, addr);
1186 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1192 * iavf_set_rx_mode - NDO callback to set the netdev filters
1193 * @netdev: network interface device structure
1195 static void iavf_set_rx_mode(struct net_device *netdev)
1197 struct iavf_adapter *adapter = netdev_priv(netdev);
1199 spin_lock_bh(&adapter->mac_vlan_list_lock);
1200 __dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1201 __dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1202 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1204 if (netdev->flags & IFF_PROMISC &&
1205 !(adapter->flags & IAVF_FLAG_PROMISC_ON))
1206 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_PROMISC;
1207 else if (!(netdev->flags & IFF_PROMISC) &&
1208 adapter->flags & IAVF_FLAG_PROMISC_ON)
1209 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_PROMISC;
1211 if (netdev->flags & IFF_ALLMULTI &&
1212 !(adapter->flags & IAVF_FLAG_ALLMULTI_ON))
1213 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_ALLMULTI;
1214 else if (!(netdev->flags & IFF_ALLMULTI) &&
1215 adapter->flags & IAVF_FLAG_ALLMULTI_ON)
1216 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_ALLMULTI;
1220 * iavf_napi_enable_all - enable NAPI on all queue vectors
1221 * @adapter: board private structure
1223 static void iavf_napi_enable_all(struct iavf_adapter *adapter)
1226 struct iavf_q_vector *q_vector;
1227 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1229 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1230 struct napi_struct *napi;
1232 q_vector = &adapter->q_vectors[q_idx];
1233 napi = &q_vector->napi;
1239 * iavf_napi_disable_all - disable NAPI on all queue vectors
1240 * @adapter: board private structure
1242 static void iavf_napi_disable_all(struct iavf_adapter *adapter)
1245 struct iavf_q_vector *q_vector;
1246 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1248 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1249 q_vector = &adapter->q_vectors[q_idx];
1250 napi_disable(&q_vector->napi);
1255 * iavf_configure - set up transmit and receive data structures
1256 * @adapter: board private structure
1258 static void iavf_configure(struct iavf_adapter *adapter)
1260 struct net_device *netdev = adapter->netdev;
1263 iavf_set_rx_mode(netdev);
1265 iavf_configure_tx(adapter);
1266 iavf_configure_rx(adapter);
1267 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
1269 for (i = 0; i < adapter->num_active_queues; i++) {
1270 struct iavf_ring *ring = &adapter->rx_rings[i];
1272 iavf_alloc_rx_buffers(ring, IAVF_DESC_UNUSED(ring));
1277 * iavf_up_complete - Finish the last steps of bringing up a connection
1278 * @adapter: board private structure
1280 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
1282 static void iavf_up_complete(struct iavf_adapter *adapter)
1284 iavf_change_state(adapter, __IAVF_RUNNING);
1285 clear_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1287 iavf_napi_enable_all(adapter);
1289 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_QUEUES;
1290 if (CLIENT_ENABLED(adapter))
1291 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_OPEN;
1292 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
1296 * iavf_clear_mac_vlan_filters - Remove mac and vlan filters not sent to PF
1297 * yet and mark other to be removed.
1298 * @adapter: board private structure
1300 static void iavf_clear_mac_vlan_filters(struct iavf_adapter *adapter)
1302 struct iavf_vlan_filter *vlf, *vlftmp;
1303 struct iavf_mac_filter *f, *ftmp;
1305 spin_lock_bh(&adapter->mac_vlan_list_lock);
1306 /* clear the sync flag on all filters */
1307 __dev_uc_unsync(adapter->netdev, NULL);
1308 __dev_mc_unsync(adapter->netdev, NULL);
1310 /* remove all MAC filters */
1311 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1321 /* disable all VLAN filters */
1322 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
1324 vlf->state = IAVF_VLAN_DISABLE;
1326 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1330 * iavf_clear_cloud_filters - Remove cloud filters not sent to PF yet and
1331 * mark other to be removed.
1332 * @adapter: board private structure
1334 static void iavf_clear_cloud_filters(struct iavf_adapter *adapter)
1336 struct iavf_cloud_filter *cf, *cftmp;
1338 /* remove all cloud filters */
1339 spin_lock_bh(&adapter->cloud_filter_list_lock);
1340 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
1343 list_del(&cf->list);
1345 adapter->num_cloud_filters--;
1350 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1354 * iavf_clear_fdir_filters - Remove fdir filters not sent to PF yet and mark
1355 * other to be removed.
1356 * @adapter: board private structure
1358 static void iavf_clear_fdir_filters(struct iavf_adapter *adapter)
1360 struct iavf_fdir_fltr *fdir, *fdirtmp;
1362 /* remove all Flow Director filters */
1363 spin_lock_bh(&adapter->fdir_fltr_lock);
1364 list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head,
1366 if (fdir->state == IAVF_FDIR_FLTR_ADD_REQUEST) {
1367 list_del(&fdir->list);
1369 adapter->fdir_active_fltr--;
1371 fdir->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1374 spin_unlock_bh(&adapter->fdir_fltr_lock);
1378 * iavf_clear_adv_rss_conf - Remove adv rss conf not sent to PF yet and mark
1379 * other to be removed.
1380 * @adapter: board private structure
1382 static void iavf_clear_adv_rss_conf(struct iavf_adapter *adapter)
1384 struct iavf_adv_rss *rss, *rsstmp;
1386 /* remove all advance RSS configuration */
1387 spin_lock_bh(&adapter->adv_rss_lock);
1388 list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
1390 if (rss->state == IAVF_ADV_RSS_ADD_REQUEST) {
1391 list_del(&rss->list);
1394 rss->state = IAVF_ADV_RSS_DEL_REQUEST;
1397 spin_unlock_bh(&adapter->adv_rss_lock);
1401 * iavf_down - Shutdown the connection processing
1402 * @adapter: board private structure
1404 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
1406 void iavf_down(struct iavf_adapter *adapter)
1408 struct net_device *netdev = adapter->netdev;
1410 if (adapter->state <= __IAVF_DOWN_PENDING)
1413 netif_carrier_off(netdev);
1414 netif_tx_disable(netdev);
1415 adapter->link_up = false;
1416 iavf_napi_disable_all(adapter);
1417 iavf_irq_disable(adapter);
1419 iavf_clear_mac_vlan_filters(adapter);
1420 iavf_clear_cloud_filters(adapter);
1421 iavf_clear_fdir_filters(adapter);
1422 iavf_clear_adv_rss_conf(adapter);
1424 if (!(adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)) {
1425 /* cancel any current operation */
1426 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1427 /* Schedule operations to close down the HW. Don't wait
1428 * here for this to complete. The watchdog is still running
1429 * and it will take care of this.
1431 if (!list_empty(&adapter->mac_filter_list))
1432 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1433 if (!list_empty(&adapter->vlan_filter_list))
1434 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
1435 if (!list_empty(&adapter->cloud_filter_list))
1436 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1437 if (!list_empty(&adapter->fdir_list_head))
1438 adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1439 if (!list_empty(&adapter->adv_rss_list_head))
1440 adapter->aq_required |= IAVF_FLAG_AQ_DEL_ADV_RSS_CFG;
1441 adapter->aq_required |= IAVF_FLAG_AQ_DISABLE_QUEUES;
1444 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
1448 * iavf_acquire_msix_vectors - Setup the MSIX capability
1449 * @adapter: board private structure
1450 * @vectors: number of vectors to request
1452 * Work with the OS to set up the MSIX vectors needed.
1454 * Returns 0 on success, negative on failure
1457 iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1459 int err, vector_threshold;
1461 /* We'll want at least 3 (vector_threshold):
1462 * 0) Other (Admin Queue and link, mostly)
1466 vector_threshold = MIN_MSIX_COUNT;
1468 /* The more we get, the more we will assign to Tx/Rx Cleanup
1469 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1470 * Right now, we simply care about how many we'll get; we'll
1471 * set them up later while requesting irq's.
1473 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1474 vector_threshold, vectors);
1476 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1477 kfree(adapter->msix_entries);
1478 adapter->msix_entries = NULL;
1482 /* Adjust for only the vectors we'll use, which is minimum
1483 * of max_msix_q_vectors + NONQ_VECS, or the number of
1484 * vectors we were allocated.
1486 adapter->num_msix_vectors = err;
1491 * iavf_free_queues - Free memory for all rings
1492 * @adapter: board private structure to initialize
1494 * Free all of the memory associated with queue pairs.
1496 static void iavf_free_queues(struct iavf_adapter *adapter)
1498 if (!adapter->vsi_res)
1500 adapter->num_active_queues = 0;
1501 kfree(adapter->tx_rings);
1502 adapter->tx_rings = NULL;
1503 kfree(adapter->rx_rings);
1504 adapter->rx_rings = NULL;
1508 * iavf_set_queue_vlan_tag_loc - set location for VLAN tag offload
1509 * @adapter: board private structure
1511 * Based on negotiated capabilities, the VLAN tag needs to be inserted and/or
1512 * stripped in certain descriptor fields. Instead of checking the offload
1513 * capability bits in the hot path, cache the location the ring specific
1516 void iavf_set_queue_vlan_tag_loc(struct iavf_adapter *adapter)
1520 for (i = 0; i < adapter->num_active_queues; i++) {
1521 struct iavf_ring *tx_ring = &adapter->tx_rings[i];
1522 struct iavf_ring *rx_ring = &adapter->rx_rings[i];
1524 /* prevent multiple L2TAG bits being set after VFR */
1526 ~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1527 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2);
1529 ~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1530 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2);
1532 if (VLAN_ALLOWED(adapter)) {
1533 tx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1534 rx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1535 } else if (VLAN_V2_ALLOWED(adapter)) {
1536 struct virtchnl_vlan_supported_caps *stripping_support;
1537 struct virtchnl_vlan_supported_caps *insertion_support;
1540 &adapter->vlan_v2_caps.offloads.stripping_support;
1542 &adapter->vlan_v2_caps.offloads.insertion_support;
1544 if (stripping_support->outer) {
1545 if (stripping_support->outer &
1546 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1548 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1549 else if (stripping_support->outer &
1550 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1552 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1553 } else if (stripping_support->inner) {
1554 if (stripping_support->inner &
1555 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1557 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1558 else if (stripping_support->inner &
1559 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1561 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1564 if (insertion_support->outer) {
1565 if (insertion_support->outer &
1566 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1568 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1569 else if (insertion_support->outer &
1570 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1572 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1573 } else if (insertion_support->inner) {
1574 if (insertion_support->inner &
1575 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1577 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1578 else if (insertion_support->inner &
1579 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1581 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1588 * iavf_alloc_queues - Allocate memory for all rings
1589 * @adapter: board private structure to initialize
1591 * We allocate one ring per queue at run-time since we don't know the
1592 * number of queues at compile-time. The polling_netdev array is
1593 * intended for Multiqueue, but should work fine with a single queue.
1595 static int iavf_alloc_queues(struct iavf_adapter *adapter)
1597 int i, num_active_queues;
1599 /* If we're in reset reallocating queues we don't actually know yet for
1600 * certain the PF gave us the number of queues we asked for but we'll
1601 * assume it did. Once basic reset is finished we'll confirm once we
1602 * start negotiating config with PF.
1604 if (adapter->num_req_queues)
1605 num_active_queues = adapter->num_req_queues;
1606 else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1608 num_active_queues = adapter->ch_config.total_qps;
1610 num_active_queues = min_t(int,
1611 adapter->vsi_res->num_queue_pairs,
1612 (int)(num_online_cpus()));
1615 adapter->tx_rings = kcalloc(num_active_queues,
1616 sizeof(struct iavf_ring), GFP_KERNEL);
1617 if (!adapter->tx_rings)
1619 adapter->rx_rings = kcalloc(num_active_queues,
1620 sizeof(struct iavf_ring), GFP_KERNEL);
1621 if (!adapter->rx_rings)
1624 for (i = 0; i < num_active_queues; i++) {
1625 struct iavf_ring *tx_ring;
1626 struct iavf_ring *rx_ring;
1628 tx_ring = &adapter->tx_rings[i];
1630 tx_ring->queue_index = i;
1631 tx_ring->netdev = adapter->netdev;
1632 tx_ring->dev = &adapter->pdev->dev;
1633 tx_ring->count = adapter->tx_desc_count;
1634 tx_ring->itr_setting = IAVF_ITR_TX_DEF;
1635 if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1636 tx_ring->flags |= IAVF_TXR_FLAGS_WB_ON_ITR;
1638 rx_ring = &adapter->rx_rings[i];
1639 rx_ring->queue_index = i;
1640 rx_ring->netdev = adapter->netdev;
1641 rx_ring->dev = &adapter->pdev->dev;
1642 rx_ring->count = adapter->rx_desc_count;
1643 rx_ring->itr_setting = IAVF_ITR_RX_DEF;
1646 adapter->num_active_queues = num_active_queues;
1648 iavf_set_queue_vlan_tag_loc(adapter);
1653 iavf_free_queues(adapter);
1658 * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1659 * @adapter: board private structure to initialize
1661 * Attempt to configure the interrupts using the best available
1662 * capabilities of the hardware and the kernel.
1664 static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1666 int vector, v_budget;
1670 if (!adapter->vsi_res) {
1674 pairs = adapter->num_active_queues;
1676 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1677 * us much good if we have more vectors than CPUs. However, we already
1678 * limit the total number of queues by the number of CPUs so we do not
1679 * need any further limiting here.
1681 v_budget = min_t(int, pairs + NONQ_VECS,
1682 (int)adapter->vf_res->max_vectors);
1684 adapter->msix_entries = kcalloc(v_budget,
1685 sizeof(struct msix_entry), GFP_KERNEL);
1686 if (!adapter->msix_entries) {
1691 for (vector = 0; vector < v_budget; vector++)
1692 adapter->msix_entries[vector].entry = vector;
1694 err = iavf_acquire_msix_vectors(adapter, v_budget);
1696 iavf_schedule_finish_config(adapter);
1703 * iavf_config_rss_aq - Configure RSS keys and lut by using AQ commands
1704 * @adapter: board private structure
1706 * Return 0 on success, negative on failure
1708 static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1710 struct iavf_aqc_get_set_rss_key_data *rss_key =
1711 (struct iavf_aqc_get_set_rss_key_data *)adapter->rss_key;
1712 struct iavf_hw *hw = &adapter->hw;
1713 enum iavf_status status;
1715 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1716 /* bail because we already have a command pending */
1717 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1718 adapter->current_op);
1722 status = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1724 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1725 iavf_stat_str(hw, status),
1726 iavf_aq_str(hw, hw->aq.asq_last_status));
1727 return iavf_status_to_errno(status);
1731 status = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1732 adapter->rss_lut, adapter->rss_lut_size);
1734 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1735 iavf_stat_str(hw, status),
1736 iavf_aq_str(hw, hw->aq.asq_last_status));
1737 return iavf_status_to_errno(status);
1745 * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1746 * @adapter: board private structure
1748 * Returns 0 on success, negative on failure
1750 static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1752 struct iavf_hw *hw = &adapter->hw;
1756 dw = (u32 *)adapter->rss_key;
1757 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1758 wr32(hw, IAVF_VFQF_HKEY(i), dw[i]);
1760 dw = (u32 *)adapter->rss_lut;
1761 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1762 wr32(hw, IAVF_VFQF_HLUT(i), dw[i]);
1770 * iavf_config_rss - Configure RSS keys and lut
1771 * @adapter: board private structure
1773 * Returns 0 on success, negative on failure
1775 int iavf_config_rss(struct iavf_adapter *adapter)
1778 if (RSS_PF(adapter)) {
1779 adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1780 IAVF_FLAG_AQ_SET_RSS_KEY;
1782 } else if (RSS_AQ(adapter)) {
1783 return iavf_config_rss_aq(adapter);
1785 return iavf_config_rss_reg(adapter);
1790 * iavf_fill_rss_lut - Fill the lut with default values
1791 * @adapter: board private structure
1793 static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1797 for (i = 0; i < adapter->rss_lut_size; i++)
1798 adapter->rss_lut[i] = i % adapter->num_active_queues;
1802 * iavf_init_rss - Prepare for RSS
1803 * @adapter: board private structure
1805 * Return 0 on success, negative on failure
1807 static int iavf_init_rss(struct iavf_adapter *adapter)
1809 struct iavf_hw *hw = &adapter->hw;
1811 if (!RSS_PF(adapter)) {
1812 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1813 if (adapter->vf_res->vf_cap_flags &
1814 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1815 adapter->hena = IAVF_DEFAULT_RSS_HENA_EXPANDED;
1817 adapter->hena = IAVF_DEFAULT_RSS_HENA;
1819 wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->hena);
1820 wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1823 iavf_fill_rss_lut(adapter);
1824 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1826 return iavf_config_rss(adapter);
1830 * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1831 * @adapter: board private structure to initialize
1833 * We allocate one q_vector per queue interrupt. If allocation fails we
1836 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1838 int q_idx = 0, num_q_vectors;
1839 struct iavf_q_vector *q_vector;
1841 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1842 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1844 if (!adapter->q_vectors)
1847 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1848 q_vector = &adapter->q_vectors[q_idx];
1849 q_vector->adapter = adapter;
1850 q_vector->vsi = &adapter->vsi;
1851 q_vector->v_idx = q_idx;
1852 q_vector->reg_idx = q_idx;
1853 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1854 netif_napi_add(adapter->netdev, &q_vector->napi,
1862 * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1863 * @adapter: board private structure to initialize
1865 * This function frees the memory allocated to the q_vectors. In addition if
1866 * NAPI is enabled it will delete any references to the NAPI struct prior
1867 * to freeing the q_vector.
1869 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1871 int q_idx, num_q_vectors;
1873 if (!adapter->q_vectors)
1876 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1878 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1879 struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1881 netif_napi_del(&q_vector->napi);
1883 kfree(adapter->q_vectors);
1884 adapter->q_vectors = NULL;
1888 * iavf_reset_interrupt_capability - Reset MSIX setup
1889 * @adapter: board private structure
1892 static void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1894 if (!adapter->msix_entries)
1897 pci_disable_msix(adapter->pdev);
1898 kfree(adapter->msix_entries);
1899 adapter->msix_entries = NULL;
1903 * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1904 * @adapter: board private structure to initialize
1907 static int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1911 err = iavf_alloc_queues(adapter);
1913 dev_err(&adapter->pdev->dev,
1914 "Unable to allocate memory for queues\n");
1915 goto err_alloc_queues;
1918 err = iavf_set_interrupt_capability(adapter);
1920 dev_err(&adapter->pdev->dev,
1921 "Unable to setup interrupt capabilities\n");
1922 goto err_set_interrupt;
1925 err = iavf_alloc_q_vectors(adapter);
1927 dev_err(&adapter->pdev->dev,
1928 "Unable to allocate memory for queue vectors\n");
1929 goto err_alloc_q_vectors;
1932 /* If we've made it so far while ADq flag being ON, then we haven't
1933 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1934 * resources have been allocated in the reset path.
1935 * Now we can truly claim that ADq is enabled.
1937 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1939 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1942 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1943 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1944 adapter->num_active_queues);
1947 err_alloc_q_vectors:
1948 iavf_reset_interrupt_capability(adapter);
1950 iavf_free_queues(adapter);
1956 * iavf_free_rss - Free memory used by RSS structs
1957 * @adapter: board private structure
1959 static void iavf_free_rss(struct iavf_adapter *adapter)
1961 kfree(adapter->rss_key);
1962 adapter->rss_key = NULL;
1964 kfree(adapter->rss_lut);
1965 adapter->rss_lut = NULL;
1969 * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1970 * @adapter: board private structure
1971 * @running: true if adapter->state == __IAVF_RUNNING
1973 * Returns 0 on success, negative on failure
1975 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter, bool running)
1977 struct net_device *netdev = adapter->netdev;
1981 iavf_free_traffic_irqs(adapter);
1982 iavf_free_misc_irq(adapter);
1983 iavf_reset_interrupt_capability(adapter);
1984 iavf_free_q_vectors(adapter);
1985 iavf_free_queues(adapter);
1987 err = iavf_init_interrupt_scheme(adapter);
1991 netif_tx_stop_all_queues(netdev);
1993 err = iavf_request_misc_irq(adapter);
1997 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1999 iavf_map_rings_to_vectors(adapter);
2005 * iavf_finish_config - do all netdev work that needs RTNL
2006 * @work: our work_struct
2008 * Do work that needs both RTNL and crit_lock.
2010 static void iavf_finish_config(struct work_struct *work)
2012 struct iavf_adapter *adapter;
2015 adapter = container_of(work, struct iavf_adapter, finish_config);
2017 /* Always take RTNL first to prevent circular lock dependency */
2019 mutex_lock(&adapter->crit_lock);
2021 if ((adapter->flags & IAVF_FLAG_SETUP_NETDEV_FEATURES) &&
2022 adapter->netdev_registered &&
2023 !test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section)) {
2024 netdev_update_features(adapter->netdev);
2025 adapter->flags &= ~IAVF_FLAG_SETUP_NETDEV_FEATURES;
2028 switch (adapter->state) {
2030 if (!adapter->netdev_registered) {
2031 err = register_netdevice(adapter->netdev);
2033 dev_err(&adapter->pdev->dev, "Unable to register netdev (%d)\n",
2036 /* go back and try again.*/
2037 iavf_free_rss(adapter);
2038 iavf_free_misc_irq(adapter);
2039 iavf_reset_interrupt_capability(adapter);
2040 iavf_change_state(adapter,
2041 __IAVF_INIT_CONFIG_ADAPTER);
2044 adapter->netdev_registered = true;
2047 /* Set the real number of queues when reset occurs while
2048 * state == __IAVF_DOWN
2051 case __IAVF_RUNNING:
2052 pairs = adapter->num_active_queues;
2053 netif_set_real_num_rx_queues(adapter->netdev, pairs);
2054 netif_set_real_num_tx_queues(adapter->netdev, pairs);
2062 mutex_unlock(&adapter->crit_lock);
2067 * iavf_schedule_finish_config - Set the flags and schedule a reset event
2068 * @adapter: board private structure
2070 void iavf_schedule_finish_config(struct iavf_adapter *adapter)
2072 if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
2073 queue_work(adapter->wq, &adapter->finish_config);
2077 * iavf_process_aq_command - process aq_required flags
2078 * and sends aq command
2079 * @adapter: pointer to iavf adapter structure
2081 * Returns 0 on success
2082 * Returns error code if no command was sent
2083 * or error code if the command failed.
2085 static int iavf_process_aq_command(struct iavf_adapter *adapter)
2087 if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG)
2088 return iavf_send_vf_config_msg(adapter);
2089 if (adapter->aq_required & IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS)
2090 return iavf_send_vf_offload_vlan_v2_msg(adapter);
2091 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
2092 iavf_disable_queues(adapter);
2096 if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
2097 iavf_map_queues(adapter);
2101 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
2102 iavf_add_ether_addrs(adapter);
2106 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
2107 iavf_add_vlans(adapter);
2111 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
2112 iavf_del_ether_addrs(adapter);
2116 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
2117 iavf_del_vlans(adapter);
2121 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
2122 iavf_enable_vlan_stripping(adapter);
2126 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
2127 iavf_disable_vlan_stripping(adapter);
2131 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
2132 iavf_configure_queues(adapter);
2136 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
2137 iavf_enable_queues(adapter);
2141 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
2142 /* This message goes straight to the firmware, not the
2143 * PF, so we don't have to set current_op as we will
2144 * not get a response through the ARQ.
2146 adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
2149 if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
2150 iavf_get_hena(adapter);
2153 if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
2154 iavf_set_hena(adapter);
2157 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
2158 iavf_set_rss_key(adapter);
2161 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
2162 iavf_set_rss_lut(adapter);
2166 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_PROMISC) {
2167 iavf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
2168 FLAG_VF_MULTICAST_PROMISC);
2172 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_ALLMULTI) {
2173 iavf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
2176 if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) ||
2177 (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
2178 iavf_set_promiscuous(adapter, 0);
2182 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
2183 iavf_enable_channels(adapter);
2187 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
2188 iavf_disable_channels(adapter);
2191 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
2192 iavf_add_cloud_filter(adapter);
2196 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
2197 iavf_del_cloud_filter(adapter);
2200 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
2201 iavf_del_cloud_filter(adapter);
2204 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
2205 iavf_add_cloud_filter(adapter);
2208 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_FDIR_FILTER) {
2209 iavf_add_fdir_filter(adapter);
2210 return IAVF_SUCCESS;
2212 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_FDIR_FILTER) {
2213 iavf_del_fdir_filter(adapter);
2214 return IAVF_SUCCESS;
2216 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_ADV_RSS_CFG) {
2217 iavf_add_adv_rss_cfg(adapter);
2220 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_ADV_RSS_CFG) {
2221 iavf_del_adv_rss_cfg(adapter);
2224 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING) {
2225 iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2228 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING) {
2229 iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2232 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING) {
2233 iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2236 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING) {
2237 iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2240 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION) {
2241 iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2244 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION) {
2245 iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2248 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION) {
2249 iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2252 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION) {
2253 iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2257 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_STATS) {
2258 iavf_request_stats(adapter);
2266 * iavf_set_vlan_offload_features - set VLAN offload configuration
2267 * @adapter: board private structure
2268 * @prev_features: previous features used for comparison
2269 * @features: updated features used for configuration
2271 * Set the aq_required bit(s) based on the requested features passed in to
2272 * configure VLAN stripping and/or VLAN insertion if supported. Also, schedule
2273 * the watchdog if any changes are requested to expedite the request via
2277 iavf_set_vlan_offload_features(struct iavf_adapter *adapter,
2278 netdev_features_t prev_features,
2279 netdev_features_t features)
2281 bool enable_stripping = true, enable_insertion = true;
2282 u16 vlan_ethertype = 0;
2283 u64 aq_required = 0;
2285 /* keep cases separate because one ethertype for offloads can be
2286 * disabled at the same time as another is disabled, so check for an
2287 * enabled ethertype first, then check for disabled. Default to
2288 * ETH_P_8021Q so an ethertype is specified if disabling insertion and
2291 if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2292 vlan_ethertype = ETH_P_8021AD;
2293 else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2294 vlan_ethertype = ETH_P_8021Q;
2295 else if (prev_features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2296 vlan_ethertype = ETH_P_8021AD;
2297 else if (prev_features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2298 vlan_ethertype = ETH_P_8021Q;
2300 vlan_ethertype = ETH_P_8021Q;
2302 if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
2303 enable_stripping = false;
2304 if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
2305 enable_insertion = false;
2307 if (VLAN_ALLOWED(adapter)) {
2308 /* VIRTCHNL_VF_OFFLOAD_VLAN only has support for toggling VLAN
2309 * stripping via virtchnl. VLAN insertion can be toggled on the
2310 * netdev, but it doesn't require a virtchnl message
2312 if (enable_stripping)
2313 aq_required |= IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2315 aq_required |= IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2317 } else if (VLAN_V2_ALLOWED(adapter)) {
2318 switch (vlan_ethertype) {
2320 if (enable_stripping)
2321 aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING;
2323 aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING;
2325 if (enable_insertion)
2326 aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION;
2328 aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION;
2331 if (enable_stripping)
2332 aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING;
2334 aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING;
2336 if (enable_insertion)
2337 aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION;
2339 aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION;
2345 adapter->aq_required |= aq_required;
2346 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
2351 * iavf_startup - first step of driver startup
2352 * @adapter: board private structure
2354 * Function process __IAVF_STARTUP driver state.
2355 * When success the state is changed to __IAVF_INIT_VERSION_CHECK
2356 * when fails the state is changed to __IAVF_INIT_FAILED
2358 static void iavf_startup(struct iavf_adapter *adapter)
2360 struct pci_dev *pdev = adapter->pdev;
2361 struct iavf_hw *hw = &adapter->hw;
2362 enum iavf_status status;
2365 WARN_ON(adapter->state != __IAVF_STARTUP);
2367 /* driver loaded, probe complete */
2368 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2369 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2370 status = iavf_set_mac_type(hw);
2372 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n", status);
2376 ret = iavf_check_reset_complete(hw);
2378 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2382 hw->aq.num_arq_entries = IAVF_AQ_LEN;
2383 hw->aq.num_asq_entries = IAVF_AQ_LEN;
2384 hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2385 hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2387 status = iavf_init_adminq(hw);
2389 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2393 ret = iavf_send_api_ver(adapter);
2395 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", ret);
2396 iavf_shutdown_adminq(hw);
2399 iavf_change_state(adapter, __IAVF_INIT_VERSION_CHECK);
2402 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2406 * iavf_init_version_check - second step of driver startup
2407 * @adapter: board private structure
2409 * Function process __IAVF_INIT_VERSION_CHECK driver state.
2410 * When success the state is changed to __IAVF_INIT_GET_RESOURCES
2411 * when fails the state is changed to __IAVF_INIT_FAILED
2413 static void iavf_init_version_check(struct iavf_adapter *adapter)
2415 struct pci_dev *pdev = adapter->pdev;
2416 struct iavf_hw *hw = &adapter->hw;
2419 WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
2421 if (!iavf_asq_done(hw)) {
2422 dev_err(&pdev->dev, "Admin queue command never completed\n");
2423 iavf_shutdown_adminq(hw);
2424 iavf_change_state(adapter, __IAVF_STARTUP);
2428 /* aq msg sent, awaiting reply */
2429 err = iavf_verify_api_ver(adapter);
2431 if (err == -EALREADY)
2432 err = iavf_send_api_ver(adapter);
2434 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2435 adapter->pf_version.major,
2436 adapter->pf_version.minor,
2437 VIRTCHNL_VERSION_MAJOR,
2438 VIRTCHNL_VERSION_MINOR);
2441 err = iavf_send_vf_config_msg(adapter);
2443 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2447 iavf_change_state(adapter, __IAVF_INIT_GET_RESOURCES);
2450 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2454 * iavf_parse_vf_resource_msg - parse response from VIRTCHNL_OP_GET_VF_RESOURCES
2455 * @adapter: board private structure
2457 int iavf_parse_vf_resource_msg(struct iavf_adapter *adapter)
2459 int i, num_req_queues = adapter->num_req_queues;
2460 struct iavf_vsi *vsi = &adapter->vsi;
2462 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2463 if (adapter->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
2464 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2466 if (!adapter->vsi_res) {
2467 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2471 if (num_req_queues &&
2472 num_req_queues > adapter->vsi_res->num_queue_pairs) {
2473 /* Problem. The PF gave us fewer queues than what we had
2474 * negotiated in our request. Need a reset to see if we can't
2475 * get back to a working state.
2477 dev_err(&adapter->pdev->dev,
2478 "Requested %d queues, but PF only gave us %d.\n",
2480 adapter->vsi_res->num_queue_pairs);
2481 adapter->flags |= IAVF_FLAG_REINIT_MSIX_NEEDED;
2482 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2483 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
2487 adapter->num_req_queues = 0;
2488 adapter->vsi.id = adapter->vsi_res->vsi_id;
2490 adapter->vsi.back = adapter;
2491 adapter->vsi.base_vector = 1;
2492 vsi->netdev = adapter->netdev;
2493 vsi->qs_handle = adapter->vsi_res->qset_handle;
2494 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2495 adapter->rss_key_size = adapter->vf_res->rss_key_size;
2496 adapter->rss_lut_size = adapter->vf_res->rss_lut_size;
2498 adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
2499 adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
2506 * iavf_init_get_resources - third step of driver startup
2507 * @adapter: board private structure
2509 * Function process __IAVF_INIT_GET_RESOURCES driver state and
2510 * finishes driver initialization procedure.
2511 * When success the state is changed to __IAVF_DOWN
2512 * when fails the state is changed to __IAVF_INIT_FAILED
2514 static void iavf_init_get_resources(struct iavf_adapter *adapter)
2516 struct pci_dev *pdev = adapter->pdev;
2517 struct iavf_hw *hw = &adapter->hw;
2520 WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
2521 /* aq msg sent, awaiting reply */
2522 if (!adapter->vf_res) {
2523 adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
2525 if (!adapter->vf_res) {
2530 err = iavf_get_vf_config(adapter);
2531 if (err == -EALREADY) {
2532 err = iavf_send_vf_config_msg(adapter);
2534 } else if (err == -EINVAL) {
2535 /* We only get -EINVAL if the device is in a very bad
2536 * state or if we've been disabled for previous bad
2537 * behavior. Either way, we're done now.
2539 iavf_shutdown_adminq(hw);
2540 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2544 dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
2548 err = iavf_parse_vf_resource_msg(adapter);
2550 dev_err(&pdev->dev, "Failed to parse VF resource message from PF (%d)\n",
2554 /* Some features require additional messages to negotiate extended
2555 * capabilities. These are processed in sequence by the
2556 * __IAVF_INIT_EXTENDED_CAPS driver state.
2558 adapter->extended_caps = IAVF_EXTENDED_CAPS;
2560 iavf_change_state(adapter, __IAVF_INIT_EXTENDED_CAPS);
2564 kfree(adapter->vf_res);
2565 adapter->vf_res = NULL;
2567 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2571 * iavf_init_send_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2572 * @adapter: board private structure
2574 * Function processes send of the extended VLAN V2 capability message to the
2575 * PF. Must clear IAVF_EXTENDED_CAP_RECV_VLAN_V2 if the message is not sent,
2576 * e.g. due to PF not negotiating VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2578 static void iavf_init_send_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2582 WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2));
2584 ret = iavf_send_vf_offload_vlan_v2_msg(adapter);
2585 if (ret && ret == -EOPNOTSUPP) {
2586 /* PF does not support VIRTCHNL_VF_OFFLOAD_V2. In this case,
2587 * we did not send the capability exchange message and do not
2588 * expect a response.
2590 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2593 /* We sent the message, so move on to the next step */
2594 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2598 * iavf_init_recv_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2599 * @adapter: board private structure
2601 * Function processes receipt of the extended VLAN V2 capability message from
2604 static void iavf_init_recv_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2608 WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2));
2610 memset(&adapter->vlan_v2_caps, 0, sizeof(adapter->vlan_v2_caps));
2612 ret = iavf_get_vf_vlan_v2_caps(adapter);
2616 /* We've processed receipt of the VLAN V2 caps message */
2617 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2620 /* We didn't receive a reply. Make sure we try sending again when
2621 * __IAVF_INIT_FAILED attempts to recover.
2623 adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2624 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2628 * iavf_init_process_extended_caps - Part of driver startup
2629 * @adapter: board private structure
2631 * Function processes __IAVF_INIT_EXTENDED_CAPS driver state. This state
2632 * handles negotiating capabilities for features which require an additional
2635 * Once all extended capabilities exchanges are finished, the driver will
2636 * transition into __IAVF_INIT_CONFIG_ADAPTER.
2638 static void iavf_init_process_extended_caps(struct iavf_adapter *adapter)
2640 WARN_ON(adapter->state != __IAVF_INIT_EXTENDED_CAPS);
2642 /* Process capability exchange for VLAN V2 */
2643 if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2) {
2644 iavf_init_send_offload_vlan_v2_caps(adapter);
2646 } else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2) {
2647 iavf_init_recv_offload_vlan_v2_caps(adapter);
2651 /* When we reach here, no further extended capabilities exchanges are
2652 * necessary, so we finally transition into __IAVF_INIT_CONFIG_ADAPTER
2654 iavf_change_state(adapter, __IAVF_INIT_CONFIG_ADAPTER);
2658 * iavf_init_config_adapter - last part of driver startup
2659 * @adapter: board private structure
2661 * After all the supported capabilities are negotiated, then the
2662 * __IAVF_INIT_CONFIG_ADAPTER state will finish driver initialization.
2664 static void iavf_init_config_adapter(struct iavf_adapter *adapter)
2666 struct net_device *netdev = adapter->netdev;
2667 struct pci_dev *pdev = adapter->pdev;
2670 WARN_ON(adapter->state != __IAVF_INIT_CONFIG_ADAPTER);
2672 if (iavf_process_config(adapter))
2675 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2677 adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
2679 netdev->netdev_ops = &iavf_netdev_ops;
2680 iavf_set_ethtool_ops(netdev);
2681 netdev->watchdog_timeo = 5 * HZ;
2683 /* MTU range: 68 - 9710 */
2684 netdev->min_mtu = ETH_MIN_MTU;
2685 netdev->max_mtu = IAVF_MAX_RXBUFFER - IAVF_PACKET_HDR_PAD;
2687 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2688 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2689 adapter->hw.mac.addr);
2690 eth_hw_addr_random(netdev);
2691 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2693 eth_hw_addr_set(netdev, adapter->hw.mac.addr);
2694 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2697 adapter->tx_desc_count = IAVF_DEFAULT_TXD;
2698 adapter->rx_desc_count = IAVF_DEFAULT_RXD;
2699 err = iavf_init_interrupt_scheme(adapter);
2702 iavf_map_rings_to_vectors(adapter);
2703 if (adapter->vf_res->vf_cap_flags &
2704 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2705 adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
2707 err = iavf_request_misc_irq(adapter);
2711 netif_carrier_off(netdev);
2712 adapter->link_up = false;
2713 netif_tx_stop_all_queues(netdev);
2715 if (CLIENT_ALLOWED(adapter)) {
2716 err = iavf_lan_add_device(adapter);
2718 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
2721 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2722 if (netdev->features & NETIF_F_GRO)
2723 dev_info(&pdev->dev, "GRO is enabled\n");
2725 iavf_change_state(adapter, __IAVF_DOWN);
2726 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2728 iavf_misc_irq_enable(adapter);
2729 wake_up(&adapter->down_waitqueue);
2731 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2732 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2733 if (!adapter->rss_key || !adapter->rss_lut) {
2737 if (RSS_AQ(adapter))
2738 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
2740 iavf_init_rss(adapter);
2742 if (VLAN_V2_ALLOWED(adapter))
2743 /* request initial VLAN offload settings */
2744 iavf_set_vlan_offload_features(adapter, 0, netdev->features);
2746 iavf_schedule_finish_config(adapter);
2750 iavf_free_rss(adapter);
2751 iavf_free_misc_irq(adapter);
2753 iavf_reset_interrupt_capability(adapter);
2755 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2759 * iavf_watchdog_task - Periodic call-back task
2760 * @work: pointer to work_struct
2762 static void iavf_watchdog_task(struct work_struct *work)
2764 struct iavf_adapter *adapter = container_of(work,
2765 struct iavf_adapter,
2766 watchdog_task.work);
2767 struct iavf_hw *hw = &adapter->hw;
2770 if (!mutex_trylock(&adapter->crit_lock)) {
2771 if (adapter->state == __IAVF_REMOVE)
2774 goto restart_watchdog;
2777 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2778 iavf_change_state(adapter, __IAVF_COMM_FAILED);
2780 switch (adapter->state) {
2781 case __IAVF_STARTUP:
2782 iavf_startup(adapter);
2783 mutex_unlock(&adapter->crit_lock);
2784 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2785 msecs_to_jiffies(30));
2787 case __IAVF_INIT_VERSION_CHECK:
2788 iavf_init_version_check(adapter);
2789 mutex_unlock(&adapter->crit_lock);
2790 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2791 msecs_to_jiffies(30));
2793 case __IAVF_INIT_GET_RESOURCES:
2794 iavf_init_get_resources(adapter);
2795 mutex_unlock(&adapter->crit_lock);
2796 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2797 msecs_to_jiffies(1));
2799 case __IAVF_INIT_EXTENDED_CAPS:
2800 iavf_init_process_extended_caps(adapter);
2801 mutex_unlock(&adapter->crit_lock);
2802 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2803 msecs_to_jiffies(1));
2805 case __IAVF_INIT_CONFIG_ADAPTER:
2806 iavf_init_config_adapter(adapter);
2807 mutex_unlock(&adapter->crit_lock);
2808 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2809 msecs_to_jiffies(1));
2811 case __IAVF_INIT_FAILED:
2812 if (test_bit(__IAVF_IN_REMOVE_TASK,
2813 &adapter->crit_section)) {
2814 /* Do not update the state and do not reschedule
2815 * watchdog task, iavf_remove should handle this state
2816 * as it can loop forever
2818 mutex_unlock(&adapter->crit_lock);
2821 if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
2822 dev_err(&adapter->pdev->dev,
2823 "Failed to communicate with PF; waiting before retry\n");
2824 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2825 iavf_shutdown_adminq(hw);
2826 mutex_unlock(&adapter->crit_lock);
2827 queue_delayed_work(adapter->wq,
2828 &adapter->watchdog_task, (5 * HZ));
2831 /* Try again from failed step*/
2832 iavf_change_state(adapter, adapter->last_state);
2833 mutex_unlock(&adapter->crit_lock);
2834 queue_delayed_work(adapter->wq, &adapter->watchdog_task, HZ);
2836 case __IAVF_COMM_FAILED:
2837 if (test_bit(__IAVF_IN_REMOVE_TASK,
2838 &adapter->crit_section)) {
2839 /* Set state to __IAVF_INIT_FAILED and perform remove
2840 * steps. Remove IAVF_FLAG_PF_COMMS_FAILED so the task
2841 * doesn't bring the state back to __IAVF_COMM_FAILED.
2843 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2844 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2845 mutex_unlock(&adapter->crit_lock);
2848 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2849 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2850 if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
2851 reg_val == VIRTCHNL_VFR_COMPLETED) {
2852 /* A chance for redemption! */
2853 dev_err(&adapter->pdev->dev,
2854 "Hardware came out of reset. Attempting reinit.\n");
2855 /* When init task contacts the PF and
2856 * gets everything set up again, it'll restart the
2857 * watchdog for us. Down, boy. Sit. Stay. Woof.
2859 iavf_change_state(adapter, __IAVF_STARTUP);
2860 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2862 adapter->aq_required = 0;
2863 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2864 mutex_unlock(&adapter->crit_lock);
2865 queue_delayed_work(adapter->wq,
2866 &adapter->watchdog_task,
2867 msecs_to_jiffies(10));
2869 case __IAVF_RESETTING:
2870 mutex_unlock(&adapter->crit_lock);
2871 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2875 case __IAVF_DOWN_PENDING:
2876 case __IAVF_TESTING:
2877 case __IAVF_RUNNING:
2878 if (adapter->current_op) {
2879 if (!iavf_asq_done(hw)) {
2880 dev_dbg(&adapter->pdev->dev,
2881 "Admin queue timeout\n");
2882 iavf_send_api_ver(adapter);
2885 int ret = iavf_process_aq_command(adapter);
2887 /* An error will be returned if no commands were
2888 * processed; use this opportunity to update stats
2889 * if the error isn't -ENOTSUPP
2891 if (ret && ret != -EOPNOTSUPP &&
2892 adapter->state == __IAVF_RUNNING)
2893 iavf_request_stats(adapter);
2895 if (adapter->state == __IAVF_RUNNING)
2896 iavf_detect_recover_hung(&adapter->vsi);
2900 mutex_unlock(&adapter->crit_lock);
2904 /* check for hw reset */
2905 reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2907 adapter->aq_required = 0;
2908 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2909 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
2910 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_PENDING);
2911 mutex_unlock(&adapter->crit_lock);
2912 queue_delayed_work(adapter->wq,
2913 &adapter->watchdog_task, HZ * 2);
2917 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
2918 mutex_unlock(&adapter->crit_lock);
2920 if (adapter->state >= __IAVF_DOWN)
2921 queue_work(adapter->wq, &adapter->adminq_task);
2922 if (adapter->aq_required)
2923 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2924 msecs_to_jiffies(20));
2926 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2931 * iavf_disable_vf - disable VF
2932 * @adapter: board private structure
2934 * Set communication failed flag and free all resources.
2935 * NOTE: This function is expected to be called with crit_lock being held.
2937 static void iavf_disable_vf(struct iavf_adapter *adapter)
2939 struct iavf_mac_filter *f, *ftmp;
2940 struct iavf_vlan_filter *fv, *fvtmp;
2941 struct iavf_cloud_filter *cf, *cftmp;
2943 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2945 /* We don't use netif_running() because it may be true prior to
2946 * ndo_open() returning, so we can't assume it means all our open
2947 * tasks have finished, since we're not holding the rtnl_lock here.
2949 if (adapter->state == __IAVF_RUNNING) {
2950 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2951 netif_carrier_off(adapter->netdev);
2952 netif_tx_disable(adapter->netdev);
2953 adapter->link_up = false;
2954 iavf_napi_disable_all(adapter);
2955 iavf_irq_disable(adapter);
2956 iavf_free_traffic_irqs(adapter);
2957 iavf_free_all_tx_resources(adapter);
2958 iavf_free_all_rx_resources(adapter);
2961 spin_lock_bh(&adapter->mac_vlan_list_lock);
2963 /* Delete all of the filters */
2964 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2969 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
2970 list_del(&fv->list);
2973 adapter->num_vlan_filters = 0;
2975 spin_unlock_bh(&adapter->mac_vlan_list_lock);
2977 spin_lock_bh(&adapter->cloud_filter_list_lock);
2978 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
2979 list_del(&cf->list);
2981 adapter->num_cloud_filters--;
2983 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2985 iavf_free_misc_irq(adapter);
2986 iavf_reset_interrupt_capability(adapter);
2987 iavf_free_q_vectors(adapter);
2988 iavf_free_queues(adapter);
2989 memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
2990 iavf_shutdown_adminq(&adapter->hw);
2991 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2992 iavf_change_state(adapter, __IAVF_DOWN);
2993 wake_up(&adapter->down_waitqueue);
2994 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
2998 * iavf_reset_task - Call-back task to handle hardware reset
2999 * @work: pointer to work_struct
3001 * During reset we need to shut down and reinitialize the admin queue
3002 * before we can use it to communicate with the PF again. We also clear
3003 * and reinit the rings because that context is lost as well.
3005 static void iavf_reset_task(struct work_struct *work)
3007 struct iavf_adapter *adapter = container_of(work,
3008 struct iavf_adapter,
3010 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3011 struct net_device *netdev = adapter->netdev;
3012 struct iavf_hw *hw = &adapter->hw;
3013 struct iavf_mac_filter *f, *ftmp;
3014 struct iavf_cloud_filter *cf;
3015 enum iavf_status status;
3020 /* When device is being removed it doesn't make sense to run the reset
3021 * task, just return in such a case.
3023 if (!mutex_trylock(&adapter->crit_lock)) {
3024 if (adapter->state != __IAVF_REMOVE)
3025 queue_work(adapter->wq, &adapter->reset_task);
3030 while (!mutex_trylock(&adapter->client_lock))
3031 usleep_range(500, 1000);
3032 if (CLIENT_ENABLED(adapter)) {
3033 adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
3034 IAVF_FLAG_CLIENT_NEEDS_CLOSE |
3035 IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
3036 IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
3037 cancel_delayed_work_sync(&adapter->client_task);
3038 iavf_notify_client_close(&adapter->vsi, true);
3040 iavf_misc_irq_disable(adapter);
3041 if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
3042 adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
3043 /* Restart the AQ here. If we have been reset but didn't
3044 * detect it, or if the PF had to reinit, our AQ will be hosed.
3046 iavf_shutdown_adminq(hw);
3047 iavf_init_adminq(hw);
3048 iavf_request_reset(adapter);
3050 adapter->flags |= IAVF_FLAG_RESET_PENDING;
3052 /* poll until we see the reset actually happen */
3053 for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
3054 reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
3055 IAVF_VF_ARQLEN1_ARQENABLE_MASK;
3058 usleep_range(5000, 10000);
3060 if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
3061 dev_info(&adapter->pdev->dev, "Never saw reset\n");
3062 goto continue_reset; /* act like the reset happened */
3065 /* wait until the reset is complete and the PF is responding to us */
3066 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3067 /* sleep first to make sure a minimum wait time is met */
3068 msleep(IAVF_RESET_WAIT_MS);
3070 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
3071 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3072 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
3076 pci_set_master(adapter->pdev);
3077 pci_restore_msi_state(adapter->pdev);
3079 if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
3080 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
3082 iavf_disable_vf(adapter);
3083 mutex_unlock(&adapter->client_lock);
3084 mutex_unlock(&adapter->crit_lock);
3085 return; /* Do not attempt to reinit. It's dead, Jim. */
3089 /* We don't use netif_running() because it may be true prior to
3090 * ndo_open() returning, so we can't assume it means all our open
3091 * tasks have finished, since we're not holding the rtnl_lock here.
3093 running = adapter->state == __IAVF_RUNNING;
3096 netif_carrier_off(netdev);
3097 netif_tx_stop_all_queues(netdev);
3098 adapter->link_up = false;
3099 iavf_napi_disable_all(adapter);
3101 iavf_irq_disable(adapter);
3103 iavf_change_state(adapter, __IAVF_RESETTING);
3104 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3106 /* free the Tx/Rx rings and descriptors, might be better to just
3107 * re-use them sometime in the future
3109 iavf_free_all_rx_resources(adapter);
3110 iavf_free_all_tx_resources(adapter);
3112 adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
3113 /* kill and reinit the admin queue */
3114 iavf_shutdown_adminq(hw);
3115 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3116 status = iavf_init_adminq(hw);
3118 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
3122 adapter->aq_required = 0;
3124 if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3125 (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3126 err = iavf_reinit_interrupt_scheme(adapter, running);
3131 if (RSS_AQ(adapter)) {
3132 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
3134 err = iavf_init_rss(adapter);
3139 adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
3140 /* always set since VIRTCHNL_OP_GET_VF_RESOURCES has not been
3141 * sent/received yet, so VLAN_V2_ALLOWED() cannot is not reliable here,
3142 * however the VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS won't be sent until
3143 * VIRTCHNL_OP_GET_VF_RESOURCES and VIRTCHNL_VF_OFFLOAD_VLAN_V2 have
3144 * been successfully sent and negotiated
3146 adapter->aq_required |= IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS;
3147 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
3149 spin_lock_bh(&adapter->mac_vlan_list_lock);
3151 /* Delete filter for the current MAC address, it could have
3152 * been changed by the PF via administratively set MAC.
3153 * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
3155 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3156 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
3161 /* re-add all MAC filters */
3162 list_for_each_entry(f, &adapter->mac_filter_list, list) {
3165 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3167 /* check if TCs are running and re-add all cloud filters */
3168 spin_lock_bh(&adapter->cloud_filter_list_lock);
3169 if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
3171 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
3175 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3177 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
3178 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3179 iavf_misc_irq_enable(adapter);
3181 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 2);
3183 /* We were running when the reset started, so we need to restore some
3187 /* allocate transmit descriptors */
3188 err = iavf_setup_all_tx_resources(adapter);
3192 /* allocate receive descriptors */
3193 err = iavf_setup_all_rx_resources(adapter);
3197 if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3198 (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3199 err = iavf_request_traffic_irqs(adapter, netdev->name);
3203 adapter->flags &= ~IAVF_FLAG_REINIT_MSIX_NEEDED;
3206 iavf_configure(adapter);
3208 /* iavf_up_complete() will switch device back
3211 iavf_up_complete(adapter);
3213 iavf_irq_enable(adapter, true);
3215 iavf_change_state(adapter, __IAVF_DOWN);
3216 wake_up(&adapter->down_waitqueue);
3219 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3221 wake_up(&adapter->reset_waitqueue);
3222 mutex_unlock(&adapter->client_lock);
3223 mutex_unlock(&adapter->crit_lock);
3228 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3229 iavf_free_traffic_irqs(adapter);
3231 iavf_disable_vf(adapter);
3233 mutex_unlock(&adapter->client_lock);
3234 mutex_unlock(&adapter->crit_lock);
3235 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
3239 * iavf_adminq_task - worker thread to clean the admin queue
3240 * @work: pointer to work_struct containing our data
3242 static void iavf_adminq_task(struct work_struct *work)
3244 struct iavf_adapter *adapter =
3245 container_of(work, struct iavf_adapter, adminq_task);
3246 struct iavf_hw *hw = &adapter->hw;
3247 struct iavf_arq_event_info event;
3248 enum virtchnl_ops v_op;
3249 enum iavf_status ret, v_ret;
3253 if (!mutex_trylock(&adapter->crit_lock)) {
3254 if (adapter->state == __IAVF_REMOVE)
3257 queue_work(adapter->wq, &adapter->adminq_task);
3261 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
3264 event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
3265 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
3270 ret = iavf_clean_arq_element(hw, &event, &pending);
3271 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
3272 v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
3275 break; /* No event to process or error cleaning ARQ */
3277 iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
3280 memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
3283 if (iavf_is_reset_in_progress(adapter))
3286 /* check for error indications */
3287 val = rd32(hw, hw->aq.arq.len);
3288 if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
3291 if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
3292 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
3293 val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
3295 if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
3296 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
3297 val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
3299 if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
3300 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
3301 val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
3304 wr32(hw, hw->aq.arq.len, val);
3306 val = rd32(hw, hw->aq.asq.len);
3308 if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
3309 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
3310 val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
3312 if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
3313 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
3314 val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
3316 if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
3317 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
3318 val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
3321 wr32(hw, hw->aq.asq.len, val);
3324 kfree(event.msg_buf);
3326 mutex_unlock(&adapter->crit_lock);
3328 /* re-enable Admin queue interrupt cause */
3329 iavf_misc_irq_enable(adapter);
3333 * iavf_client_task - worker thread to perform client work
3334 * @work: pointer to work_struct containing our data
3336 * This task handles client interactions. Because client calls can be
3337 * reentrant, we can't handle them in the watchdog.
3339 static void iavf_client_task(struct work_struct *work)
3341 struct iavf_adapter *adapter =
3342 container_of(work, struct iavf_adapter, client_task.work);
3344 /* If we can't get the client bit, just give up. We'll be rescheduled
3348 if (!mutex_trylock(&adapter->client_lock))
3351 if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
3352 iavf_client_subtask(adapter);
3353 adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3356 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
3357 iavf_notify_client_l2_params(&adapter->vsi);
3358 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
3361 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_CLOSE) {
3362 iavf_notify_client_close(&adapter->vsi, false);
3363 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3366 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
3367 iavf_notify_client_open(&adapter->vsi);
3368 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
3371 mutex_unlock(&adapter->client_lock);
3375 * iavf_free_all_tx_resources - Free Tx Resources for All Queues
3376 * @adapter: board private structure
3378 * Free all transmit software resources
3380 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
3384 if (!adapter->tx_rings)
3387 for (i = 0; i < adapter->num_active_queues; i++)
3388 if (adapter->tx_rings[i].desc)
3389 iavf_free_tx_resources(&adapter->tx_rings[i]);
3393 * iavf_setup_all_tx_resources - allocate all queues Tx resources
3394 * @adapter: board private structure
3396 * If this function returns with an error, then it's possible one or
3397 * more of the rings is populated (while the rest are not). It is the
3398 * callers duty to clean those orphaned rings.
3400 * Return 0 on success, negative on failure
3402 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
3406 for (i = 0; i < adapter->num_active_queues; i++) {
3407 adapter->tx_rings[i].count = adapter->tx_desc_count;
3408 err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
3411 dev_err(&adapter->pdev->dev,
3412 "Allocation for Tx Queue %u failed\n", i);
3420 * iavf_setup_all_rx_resources - allocate all queues Rx resources
3421 * @adapter: board private structure
3423 * If this function returns with an error, then it's possible one or
3424 * more of the rings is populated (while the rest are not). It is the
3425 * callers duty to clean those orphaned rings.
3427 * Return 0 on success, negative on failure
3429 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
3433 for (i = 0; i < adapter->num_active_queues; i++) {
3434 adapter->rx_rings[i].count = adapter->rx_desc_count;
3435 err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
3438 dev_err(&adapter->pdev->dev,
3439 "Allocation for Rx Queue %u failed\n", i);
3446 * iavf_free_all_rx_resources - Free Rx Resources for All Queues
3447 * @adapter: board private structure
3449 * Free all receive software resources
3451 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
3455 if (!adapter->rx_rings)
3458 for (i = 0; i < adapter->num_active_queues; i++)
3459 if (adapter->rx_rings[i].desc)
3460 iavf_free_rx_resources(&adapter->rx_rings[i]);
3464 * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
3465 * @adapter: board private structure
3466 * @max_tx_rate: max Tx bw for a tc
3468 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
3471 int speed = 0, ret = 0;
3473 if (ADV_LINK_SUPPORT(adapter)) {
3474 if (adapter->link_speed_mbps < U32_MAX) {
3475 speed = adapter->link_speed_mbps;
3478 dev_err(&adapter->pdev->dev, "Unknown link speed\n");
3483 switch (adapter->link_speed) {
3484 case VIRTCHNL_LINK_SPEED_40GB:
3485 speed = SPEED_40000;
3487 case VIRTCHNL_LINK_SPEED_25GB:
3488 speed = SPEED_25000;
3490 case VIRTCHNL_LINK_SPEED_20GB:
3491 speed = SPEED_20000;
3493 case VIRTCHNL_LINK_SPEED_10GB:
3494 speed = SPEED_10000;
3496 case VIRTCHNL_LINK_SPEED_5GB:
3499 case VIRTCHNL_LINK_SPEED_2_5GB:
3502 case VIRTCHNL_LINK_SPEED_1GB:
3505 case VIRTCHNL_LINK_SPEED_100MB:
3513 if (max_tx_rate > speed) {
3514 dev_err(&adapter->pdev->dev,
3515 "Invalid tx rate specified\n");
3523 * iavf_validate_ch_config - validate queue mapping info
3524 * @adapter: board private structure
3525 * @mqprio_qopt: queue parameters
3527 * This function validates if the config provided by the user to
3528 * configure queue channels is valid or not. Returns 0 on a valid
3531 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
3532 struct tc_mqprio_qopt_offload *mqprio_qopt)
3534 u64 total_max_rate = 0;
3535 u32 tx_rate_rem = 0;
3540 if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
3541 mqprio_qopt->qopt.num_tc < 1)
3544 for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
3545 if (!mqprio_qopt->qopt.count[i] ||
3546 mqprio_qopt->qopt.offset[i] != num_qps)
3548 if (mqprio_qopt->min_rate[i]) {
3549 dev_err(&adapter->pdev->dev,
3550 "Invalid min tx rate (greater than 0) specified for TC%d\n",
3555 /* convert to Mbps */
3556 tx_rate = div_u64(mqprio_qopt->max_rate[i],
3559 if (mqprio_qopt->max_rate[i] &&
3560 tx_rate < IAVF_MBPS_QUANTA) {
3561 dev_err(&adapter->pdev->dev,
3562 "Invalid max tx rate for TC%d, minimum %dMbps\n",
3563 i, IAVF_MBPS_QUANTA);
3567 (void)div_u64_rem(tx_rate, IAVF_MBPS_QUANTA, &tx_rate_rem);
3569 if (tx_rate_rem != 0) {
3570 dev_err(&adapter->pdev->dev,
3571 "Invalid max tx rate for TC%d, not divisible by %d\n",
3572 i, IAVF_MBPS_QUANTA);
3576 total_max_rate += tx_rate;
3577 num_qps += mqprio_qopt->qopt.count[i];
3579 if (num_qps > adapter->num_active_queues) {
3580 dev_err(&adapter->pdev->dev,
3581 "Cannot support requested number of queues\n");
3585 ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
3590 * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
3591 * @adapter: board private structure
3593 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
3595 struct iavf_cloud_filter *cf, *cftmp;
3597 spin_lock_bh(&adapter->cloud_filter_list_lock);
3598 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
3600 list_del(&cf->list);
3602 adapter->num_cloud_filters--;
3604 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3608 * __iavf_setup_tc - configure multiple traffic classes
3609 * @netdev: network interface device structure
3610 * @type_data: tc offload data
3612 * This function processes the config information provided by the
3613 * user to configure traffic classes/queue channels and packages the
3614 * information to request the PF to setup traffic classes.
3616 * Returns 0 on success.
3618 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
3620 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
3621 struct iavf_adapter *adapter = netdev_priv(netdev);
3622 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3623 u8 num_tc = 0, total_qps = 0;
3624 int ret = 0, netdev_tc = 0;
3629 num_tc = mqprio_qopt->qopt.num_tc;
3630 mode = mqprio_qopt->mode;
3632 /* delete queue_channel */
3633 if (!mqprio_qopt->qopt.hw) {
3634 if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
3635 /* reset the tc configuration */
3636 netdev_reset_tc(netdev);
3637 adapter->num_tc = 0;
3638 netif_tx_stop_all_queues(netdev);
3639 netif_tx_disable(netdev);
3640 iavf_del_all_cloud_filters(adapter);
3641 adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
3642 total_qps = adapter->orig_num_active_queues;
3649 /* add queue channel */
3650 if (mode == TC_MQPRIO_MODE_CHANNEL) {
3651 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3652 dev_err(&adapter->pdev->dev, "ADq not supported\n");
3655 if (adapter->ch_config.state != __IAVF_TC_INVALID) {
3656 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
3660 ret = iavf_validate_ch_config(adapter, mqprio_qopt);
3663 /* Return if same TC config is requested */
3664 if (adapter->num_tc == num_tc)
3666 adapter->num_tc = num_tc;
3668 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3670 adapter->ch_config.ch_info[i].count =
3671 mqprio_qopt->qopt.count[i];
3672 adapter->ch_config.ch_info[i].offset =
3673 mqprio_qopt->qopt.offset[i];
3674 total_qps += mqprio_qopt->qopt.count[i];
3675 max_tx_rate = mqprio_qopt->max_rate[i];
3676 /* convert to Mbps */
3677 max_tx_rate = div_u64(max_tx_rate,
3679 adapter->ch_config.ch_info[i].max_tx_rate =
3682 adapter->ch_config.ch_info[i].count = 1;
3683 adapter->ch_config.ch_info[i].offset = 0;
3687 /* Take snapshot of original config such as "num_active_queues"
3688 * It is used later when delete ADQ flow is exercised, so that
3689 * once delete ADQ flow completes, VF shall go back to its
3690 * original queue configuration
3693 adapter->orig_num_active_queues = adapter->num_active_queues;
3695 /* Store queue info based on TC so that VF gets configured
3696 * with correct number of queues when VF completes ADQ config
3699 adapter->ch_config.total_qps = total_qps;
3701 netif_tx_stop_all_queues(netdev);
3702 netif_tx_disable(netdev);
3703 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
3704 netdev_reset_tc(netdev);
3705 /* Report the tc mapping up the stack */
3706 netdev_set_num_tc(adapter->netdev, num_tc);
3707 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3708 u16 qcount = mqprio_qopt->qopt.count[i];
3709 u16 qoffset = mqprio_qopt->qopt.offset[i];
3712 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
3717 if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
3720 netif_set_real_num_rx_queues(netdev, total_qps);
3721 netif_set_real_num_tx_queues(netdev, total_qps);
3727 * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
3728 * @adapter: board private structure
3729 * @f: pointer to struct flow_cls_offload
3730 * @filter: pointer to cloud filter structure
3732 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
3733 struct flow_cls_offload *f,
3734 struct iavf_cloud_filter *filter)
3736 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
3737 struct flow_dissector *dissector = rule->match.dissector;
3738 u16 n_proto_mask = 0;
3739 u16 n_proto_key = 0;
3744 struct virtchnl_filter *vf = &filter->f;
3746 if (dissector->used_keys &
3747 ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
3748 BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
3749 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
3750 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
3751 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
3752 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
3753 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
3754 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
3755 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%llx\n",
3756 dissector->used_keys);
3760 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
3761 struct flow_match_enc_keyid match;
3763 flow_rule_match_enc_keyid(rule, &match);
3764 if (match.mask->keyid != 0)
3765 field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
3768 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
3769 struct flow_match_basic match;
3771 flow_rule_match_basic(rule, &match);
3772 n_proto_key = ntohs(match.key->n_proto);
3773 n_proto_mask = ntohs(match.mask->n_proto);
3775 if (n_proto_key == ETH_P_ALL) {
3779 n_proto = n_proto_key & n_proto_mask;
3780 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
3782 if (n_proto == ETH_P_IPV6) {
3783 /* specify flow type as TCP IPv6 */
3784 vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
3787 if (match.key->ip_proto != IPPROTO_TCP) {
3788 dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
3793 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
3794 struct flow_match_eth_addrs match;
3796 flow_rule_match_eth_addrs(rule, &match);
3798 /* use is_broadcast and is_zero to check for all 0xf or 0 */
3799 if (!is_zero_ether_addr(match.mask->dst)) {
3800 if (is_broadcast_ether_addr(match.mask->dst)) {
3801 field_flags |= IAVF_CLOUD_FIELD_OMAC;
3803 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
3809 if (!is_zero_ether_addr(match.mask->src)) {
3810 if (is_broadcast_ether_addr(match.mask->src)) {
3811 field_flags |= IAVF_CLOUD_FIELD_IMAC;
3813 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
3819 if (!is_zero_ether_addr(match.key->dst))
3820 if (is_valid_ether_addr(match.key->dst) ||
3821 is_multicast_ether_addr(match.key->dst)) {
3822 /* set the mask if a valid dst_mac address */
3823 for (i = 0; i < ETH_ALEN; i++)
3824 vf->mask.tcp_spec.dst_mac[i] |= 0xff;
3825 ether_addr_copy(vf->data.tcp_spec.dst_mac,
3829 if (!is_zero_ether_addr(match.key->src))
3830 if (is_valid_ether_addr(match.key->src) ||
3831 is_multicast_ether_addr(match.key->src)) {
3832 /* set the mask if a valid dst_mac address */
3833 for (i = 0; i < ETH_ALEN; i++)
3834 vf->mask.tcp_spec.src_mac[i] |= 0xff;
3835 ether_addr_copy(vf->data.tcp_spec.src_mac,
3840 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
3841 struct flow_match_vlan match;
3843 flow_rule_match_vlan(rule, &match);
3844 if (match.mask->vlan_id) {
3845 if (match.mask->vlan_id == VLAN_VID_MASK) {
3846 field_flags |= IAVF_CLOUD_FIELD_IVLAN;
3848 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
3849 match.mask->vlan_id);
3853 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
3854 vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
3857 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
3858 struct flow_match_control match;
3860 flow_rule_match_control(rule, &match);
3861 addr_type = match.key->addr_type;
3864 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
3865 struct flow_match_ipv4_addrs match;
3867 flow_rule_match_ipv4_addrs(rule, &match);
3868 if (match.mask->dst) {
3869 if (match.mask->dst == cpu_to_be32(0xffffffff)) {
3870 field_flags |= IAVF_CLOUD_FIELD_IIP;
3872 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
3873 be32_to_cpu(match.mask->dst));
3878 if (match.mask->src) {
3879 if (match.mask->src == cpu_to_be32(0xffffffff)) {
3880 field_flags |= IAVF_CLOUD_FIELD_IIP;
3882 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
3883 be32_to_cpu(match.mask->src));
3888 if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
3889 dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
3892 if (match.key->dst) {
3893 vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
3894 vf->data.tcp_spec.dst_ip[0] = match.key->dst;
3896 if (match.key->src) {
3897 vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
3898 vf->data.tcp_spec.src_ip[0] = match.key->src;
3902 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
3903 struct flow_match_ipv6_addrs match;
3905 flow_rule_match_ipv6_addrs(rule, &match);
3907 /* validate mask, make sure it is not IPV6_ADDR_ANY */
3908 if (ipv6_addr_any(&match.mask->dst)) {
3909 dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
3914 /* src and dest IPv6 address should not be LOOPBACK
3915 * (0:0:0:0:0:0:0:1) which can be represented as ::1
3917 if (ipv6_addr_loopback(&match.key->dst) ||
3918 ipv6_addr_loopback(&match.key->src)) {
3919 dev_err(&adapter->pdev->dev,
3920 "ipv6 addr should not be loopback\n");
3923 if (!ipv6_addr_any(&match.mask->dst) ||
3924 !ipv6_addr_any(&match.mask->src))
3925 field_flags |= IAVF_CLOUD_FIELD_IIP;
3927 for (i = 0; i < 4; i++)
3928 vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
3929 memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
3930 sizeof(vf->data.tcp_spec.dst_ip));
3931 for (i = 0; i < 4; i++)
3932 vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
3933 memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
3934 sizeof(vf->data.tcp_spec.src_ip));
3936 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
3937 struct flow_match_ports match;
3939 flow_rule_match_ports(rule, &match);
3940 if (match.mask->src) {
3941 if (match.mask->src == cpu_to_be16(0xffff)) {
3942 field_flags |= IAVF_CLOUD_FIELD_IIP;
3944 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
3945 be16_to_cpu(match.mask->src));
3950 if (match.mask->dst) {
3951 if (match.mask->dst == cpu_to_be16(0xffff)) {
3952 field_flags |= IAVF_CLOUD_FIELD_IIP;
3954 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
3955 be16_to_cpu(match.mask->dst));
3959 if (match.key->dst) {
3960 vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
3961 vf->data.tcp_spec.dst_port = match.key->dst;
3964 if (match.key->src) {
3965 vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
3966 vf->data.tcp_spec.src_port = match.key->src;
3969 vf->field_flags = field_flags;
3975 * iavf_handle_tclass - Forward to a traffic class on the device
3976 * @adapter: board private structure
3977 * @tc: traffic class index on the device
3978 * @filter: pointer to cloud filter structure
3980 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
3981 struct iavf_cloud_filter *filter)
3985 if (tc < adapter->num_tc) {
3986 if (!filter->f.data.tcp_spec.dst_port) {
3987 dev_err(&adapter->pdev->dev,
3988 "Specify destination port to redirect to traffic class other than TC0\n");
3992 /* redirect to a traffic class on the same device */
3993 filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
3994 filter->f.action_meta = tc;
3999 * iavf_find_cf - Find the cloud filter in the list
4000 * @adapter: Board private structure
4001 * @cookie: filter specific cookie
4003 * Returns ptr to the filter object or NULL. Must be called while holding the
4004 * cloud_filter_list_lock.
4006 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
4007 unsigned long *cookie)
4009 struct iavf_cloud_filter *filter = NULL;
4014 list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
4015 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
4022 * iavf_configure_clsflower - Add tc flower filters
4023 * @adapter: board private structure
4024 * @cls_flower: Pointer to struct flow_cls_offload
4026 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
4027 struct flow_cls_offload *cls_flower)
4029 int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
4030 struct iavf_cloud_filter *filter = NULL;
4031 int err = -EINVAL, count = 50;
4034 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
4038 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
4042 while (!mutex_trylock(&adapter->crit_lock)) {
4050 filter->cookie = cls_flower->cookie;
4052 /* bail out here if filter already exists */
4053 spin_lock_bh(&adapter->cloud_filter_list_lock);
4054 if (iavf_find_cf(adapter, &cls_flower->cookie)) {
4055 dev_err(&adapter->pdev->dev, "Failed to add TC Flower filter, it already exists\n");
4059 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4061 /* set the mask to all zeroes to begin with */
4062 memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
4063 /* start out with flow type and eth type IPv4 to begin with */
4064 filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
4065 err = iavf_parse_cls_flower(adapter, cls_flower, filter);
4069 err = iavf_handle_tclass(adapter, tc, filter);
4073 /* add filter to the list */
4074 spin_lock_bh(&adapter->cloud_filter_list_lock);
4075 list_add_tail(&filter->list, &adapter->cloud_filter_list);
4076 adapter->num_cloud_filters++;
4078 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
4080 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4085 mutex_unlock(&adapter->crit_lock);
4090 * iavf_delete_clsflower - Remove tc flower filters
4091 * @adapter: board private structure
4092 * @cls_flower: Pointer to struct flow_cls_offload
4094 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
4095 struct flow_cls_offload *cls_flower)
4097 struct iavf_cloud_filter *filter = NULL;
4100 spin_lock_bh(&adapter->cloud_filter_list_lock);
4101 filter = iavf_find_cf(adapter, &cls_flower->cookie);
4104 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
4108 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4114 * iavf_setup_tc_cls_flower - flower classifier offloads
4115 * @adapter: board private structure
4116 * @cls_flower: pointer to flow_cls_offload struct with flow info
4118 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
4119 struct flow_cls_offload *cls_flower)
4121 switch (cls_flower->command) {
4122 case FLOW_CLS_REPLACE:
4123 return iavf_configure_clsflower(adapter, cls_flower);
4124 case FLOW_CLS_DESTROY:
4125 return iavf_delete_clsflower(adapter, cls_flower);
4126 case FLOW_CLS_STATS:
4134 * iavf_setup_tc_block_cb - block callback for tc
4135 * @type: type of offload
4136 * @type_data: offload data
4139 * This function is the block callback for traffic classes
4141 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
4144 struct iavf_adapter *adapter = cb_priv;
4146 if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
4150 case TC_SETUP_CLSFLOWER:
4151 return iavf_setup_tc_cls_flower(cb_priv, type_data);
4157 static LIST_HEAD(iavf_block_cb_list);
4160 * iavf_setup_tc - configure multiple traffic classes
4161 * @netdev: network interface device structure
4162 * @type: type of offload
4163 * @type_data: tc offload data
4165 * This function is the callback to ndo_setup_tc in the
4168 * Returns 0 on success
4170 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
4173 struct iavf_adapter *adapter = netdev_priv(netdev);
4176 case TC_SETUP_QDISC_MQPRIO:
4177 return __iavf_setup_tc(netdev, type_data);
4178 case TC_SETUP_BLOCK:
4179 return flow_block_cb_setup_simple(type_data,
4180 &iavf_block_cb_list,
4181 iavf_setup_tc_block_cb,
4182 adapter, adapter, true);
4189 * iavf_open - Called when a network interface is made active
4190 * @netdev: network interface device structure
4192 * Returns 0 on success, negative value on failure
4194 * The open entry point is called when a network interface is made
4195 * active by the system (IFF_UP). At this point all resources needed
4196 * for transmit and receive operations are allocated, the interrupt
4197 * handler is registered with the OS, the watchdog is started,
4198 * and the stack is notified that the interface is ready.
4200 static int iavf_open(struct net_device *netdev)
4202 struct iavf_adapter *adapter = netdev_priv(netdev);
4205 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
4206 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
4210 while (!mutex_trylock(&adapter->crit_lock)) {
4211 /* If we are in __IAVF_INIT_CONFIG_ADAPTER state the crit_lock
4212 * is already taken and iavf_open is called from an upper
4213 * device's notifier reacting on NETDEV_REGISTER event.
4214 * We have to leave here to avoid dead lock.
4216 if (adapter->state == __IAVF_INIT_CONFIG_ADAPTER)
4219 usleep_range(500, 1000);
4222 if (adapter->state != __IAVF_DOWN) {
4227 if (adapter->state == __IAVF_RUNNING &&
4228 !test_bit(__IAVF_VSI_DOWN, adapter->vsi.state)) {
4229 dev_dbg(&adapter->pdev->dev, "VF is already open.\n");
4234 /* allocate transmit descriptors */
4235 err = iavf_setup_all_tx_resources(adapter);
4239 /* allocate receive descriptors */
4240 err = iavf_setup_all_rx_resources(adapter);
4244 /* clear any pending interrupts, may auto mask */
4245 err = iavf_request_traffic_irqs(adapter, netdev->name);
4249 spin_lock_bh(&adapter->mac_vlan_list_lock);
4251 iavf_add_filter(adapter, adapter->hw.mac.addr);
4253 spin_unlock_bh(&adapter->mac_vlan_list_lock);
4255 /* Restore VLAN filters that were removed with IFF_DOWN */
4256 iavf_restore_filters(adapter);
4258 iavf_configure(adapter);
4260 iavf_up_complete(adapter);
4262 iavf_irq_enable(adapter, true);
4264 mutex_unlock(&adapter->crit_lock);
4270 iavf_free_traffic_irqs(adapter);
4272 iavf_free_all_rx_resources(adapter);
4274 iavf_free_all_tx_resources(adapter);
4276 mutex_unlock(&adapter->crit_lock);
4282 * iavf_close - Disables a network interface
4283 * @netdev: network interface device structure
4285 * Returns 0, this is not allowed to fail
4287 * The close entry point is called when an interface is de-activated
4288 * by the OS. The hardware is still under the drivers control, but
4289 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
4290 * are freed, along with all transmit and receive resources.
4292 static int iavf_close(struct net_device *netdev)
4294 struct iavf_adapter *adapter = netdev_priv(netdev);
4298 mutex_lock(&adapter->crit_lock);
4300 if (adapter->state <= __IAVF_DOWN_PENDING) {
4301 mutex_unlock(&adapter->crit_lock);
4305 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
4306 if (CLIENT_ENABLED(adapter))
4307 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
4308 /* We cannot send IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS before
4309 * IAVF_FLAG_AQ_DISABLE_QUEUES because in such case there is rtnl
4310 * deadlock with adminq_task() until iavf_close timeouts. We must send
4311 * IAVF_FLAG_AQ_GET_CONFIG before IAVF_FLAG_AQ_DISABLE_QUEUES to make
4312 * disable queues possible for vf. Give only necessary flags to
4313 * iavf_down and save other to set them right before iavf_close()
4314 * returns, when IAVF_FLAG_AQ_DISABLE_QUEUES will be already sent and
4315 * iavf will be in DOWN state.
4317 aq_to_restore = adapter->aq_required;
4318 adapter->aq_required &= IAVF_FLAG_AQ_GET_CONFIG;
4320 /* Remove flags which we do not want to send after close or we want to
4321 * send before disable queues.
4323 aq_to_restore &= ~(IAVF_FLAG_AQ_GET_CONFIG |
4324 IAVF_FLAG_AQ_ENABLE_QUEUES |
4325 IAVF_FLAG_AQ_CONFIGURE_QUEUES |
4326 IAVF_FLAG_AQ_ADD_VLAN_FILTER |
4327 IAVF_FLAG_AQ_ADD_MAC_FILTER |
4328 IAVF_FLAG_AQ_ADD_CLOUD_FILTER |
4329 IAVF_FLAG_AQ_ADD_FDIR_FILTER |
4330 IAVF_FLAG_AQ_ADD_ADV_RSS_CFG);
4333 iavf_change_state(adapter, __IAVF_DOWN_PENDING);
4334 iavf_free_traffic_irqs(adapter);
4336 mutex_unlock(&adapter->crit_lock);
4338 /* We explicitly don't free resources here because the hardware is
4339 * still active and can DMA into memory. Resources are cleared in
4340 * iavf_virtchnl_completion() after we get confirmation from the PF
4341 * driver that the rings have been stopped.
4343 * Also, we wait for state to transition to __IAVF_DOWN before
4344 * returning. State change occurs in iavf_virtchnl_completion() after
4345 * VF resources are released (which occurs after PF driver processes and
4346 * responds to admin queue commands).
4349 status = wait_event_timeout(adapter->down_waitqueue,
4350 adapter->state == __IAVF_DOWN,
4351 msecs_to_jiffies(500));
4353 netdev_warn(netdev, "Device resources not yet released\n");
4355 mutex_lock(&adapter->crit_lock);
4356 adapter->aq_required |= aq_to_restore;
4357 mutex_unlock(&adapter->crit_lock);
4362 * iavf_change_mtu - Change the Maximum Transfer Unit
4363 * @netdev: network interface device structure
4364 * @new_mtu: new value for maximum frame size
4366 * Returns 0 on success, negative on failure
4368 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
4370 struct iavf_adapter *adapter = netdev_priv(netdev);
4373 netdev_dbg(netdev, "changing MTU from %d to %d\n",
4374 netdev->mtu, new_mtu);
4375 netdev->mtu = new_mtu;
4376 if (CLIENT_ENABLED(adapter)) {
4377 iavf_notify_client_l2_params(&adapter->vsi);
4378 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
4381 if (netif_running(netdev)) {
4382 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
4383 ret = iavf_wait_for_reset(adapter);
4385 netdev_warn(netdev, "MTU change interrupted waiting for reset");
4387 netdev_warn(netdev, "MTU change timed out waiting for reset");
4393 #define NETIF_VLAN_OFFLOAD_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
4394 NETIF_F_HW_VLAN_CTAG_TX | \
4395 NETIF_F_HW_VLAN_STAG_RX | \
4396 NETIF_F_HW_VLAN_STAG_TX)
4399 * iavf_set_features - set the netdev feature flags
4400 * @netdev: ptr to the netdev being adjusted
4401 * @features: the feature set that the stack is suggesting
4402 * Note: expects to be called while under rtnl_lock()
4404 static int iavf_set_features(struct net_device *netdev,
4405 netdev_features_t features)
4407 struct iavf_adapter *adapter = netdev_priv(netdev);
4409 /* trigger update on any VLAN feature change */
4410 if ((netdev->features & NETIF_VLAN_OFFLOAD_FEATURES) ^
4411 (features & NETIF_VLAN_OFFLOAD_FEATURES))
4412 iavf_set_vlan_offload_features(adapter, netdev->features,
4419 * iavf_features_check - Validate encapsulated packet conforms to limits
4421 * @dev: This physical port's netdev
4422 * @features: Offload features that the stack believes apply
4424 static netdev_features_t iavf_features_check(struct sk_buff *skb,
4425 struct net_device *dev,
4426 netdev_features_t features)
4430 /* No point in doing any of this if neither checksum nor GSO are
4431 * being requested for this frame. We can rule out both by just
4432 * checking for CHECKSUM_PARTIAL
4434 if (skb->ip_summed != CHECKSUM_PARTIAL)
4437 /* We cannot support GSO if the MSS is going to be less than
4438 * 64 bytes. If it is then we need to drop support for GSO.
4440 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4441 features &= ~NETIF_F_GSO_MASK;
4443 /* MACLEN can support at most 63 words */
4444 len = skb_network_header(skb) - skb->data;
4445 if (len & ~(63 * 2))
4448 /* IPLEN and EIPLEN can support at most 127 dwords */
4449 len = skb_transport_header(skb) - skb_network_header(skb);
4450 if (len & ~(127 * 4))
4453 if (skb->encapsulation) {
4454 /* L4TUNLEN can support 127 words */
4455 len = skb_inner_network_header(skb) - skb_transport_header(skb);
4456 if (len & ~(127 * 2))
4459 /* IPLEN can support at most 127 dwords */
4460 len = skb_inner_transport_header(skb) -
4461 skb_inner_network_header(skb);
4462 if (len & ~(127 * 4))
4466 /* No need to validate L4LEN as TCP is the only protocol with a
4467 * flexible value and we support all possible values supported
4468 * by TCP, which is at most 15 dwords
4473 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4477 * iavf_get_netdev_vlan_hw_features - get NETDEV VLAN features that can toggle on/off
4478 * @adapter: board private structure
4480 * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4481 * were negotiated determine the VLAN features that can be toggled on and off.
4483 static netdev_features_t
4484 iavf_get_netdev_vlan_hw_features(struct iavf_adapter *adapter)
4486 netdev_features_t hw_features = 0;
4488 if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4491 /* Enable VLAN features if supported */
4492 if (VLAN_ALLOWED(adapter)) {
4493 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
4494 NETIF_F_HW_VLAN_CTAG_RX);
4495 } else if (VLAN_V2_ALLOWED(adapter)) {
4496 struct virtchnl_vlan_caps *vlan_v2_caps =
4497 &adapter->vlan_v2_caps;
4498 struct virtchnl_vlan_supported_caps *stripping_support =
4499 &vlan_v2_caps->offloads.stripping_support;
4500 struct virtchnl_vlan_supported_caps *insertion_support =
4501 &vlan_v2_caps->offloads.insertion_support;
4503 if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4504 stripping_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4505 if (stripping_support->outer &
4506 VIRTCHNL_VLAN_ETHERTYPE_8100)
4507 hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4508 if (stripping_support->outer &
4509 VIRTCHNL_VLAN_ETHERTYPE_88A8)
4510 hw_features |= NETIF_F_HW_VLAN_STAG_RX;
4511 } else if (stripping_support->inner !=
4512 VIRTCHNL_VLAN_UNSUPPORTED &&
4513 stripping_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4514 if (stripping_support->inner &
4515 VIRTCHNL_VLAN_ETHERTYPE_8100)
4516 hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4519 if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4520 insertion_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4521 if (insertion_support->outer &
4522 VIRTCHNL_VLAN_ETHERTYPE_8100)
4523 hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4524 if (insertion_support->outer &
4525 VIRTCHNL_VLAN_ETHERTYPE_88A8)
4526 hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4527 } else if (insertion_support->inner &&
4528 insertion_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4529 if (insertion_support->inner &
4530 VIRTCHNL_VLAN_ETHERTYPE_8100)
4531 hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4539 * iavf_get_netdev_vlan_features - get the enabled NETDEV VLAN fetures
4540 * @adapter: board private structure
4542 * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4543 * were negotiated determine the VLAN features that are enabled by default.
4545 static netdev_features_t
4546 iavf_get_netdev_vlan_features(struct iavf_adapter *adapter)
4548 netdev_features_t features = 0;
4550 if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4553 if (VLAN_ALLOWED(adapter)) {
4554 features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4555 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
4556 } else if (VLAN_V2_ALLOWED(adapter)) {
4557 struct virtchnl_vlan_caps *vlan_v2_caps =
4558 &adapter->vlan_v2_caps;
4559 struct virtchnl_vlan_supported_caps *filtering_support =
4560 &vlan_v2_caps->filtering.filtering_support;
4561 struct virtchnl_vlan_supported_caps *stripping_support =
4562 &vlan_v2_caps->offloads.stripping_support;
4563 struct virtchnl_vlan_supported_caps *insertion_support =
4564 &vlan_v2_caps->offloads.insertion_support;
4567 /* give priority to outer stripping and don't support both outer
4568 * and inner stripping
4570 ethertype_init = vlan_v2_caps->offloads.ethertype_init;
4571 if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4572 if (stripping_support->outer &
4573 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4574 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4575 features |= NETIF_F_HW_VLAN_CTAG_RX;
4576 else if (stripping_support->outer &
4577 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4578 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4579 features |= NETIF_F_HW_VLAN_STAG_RX;
4580 } else if (stripping_support->inner !=
4581 VIRTCHNL_VLAN_UNSUPPORTED) {
4582 if (stripping_support->inner &
4583 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4584 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4585 features |= NETIF_F_HW_VLAN_CTAG_RX;
4588 /* give priority to outer insertion and don't support both outer
4589 * and inner insertion
4591 if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4592 if (insertion_support->outer &
4593 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4594 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4595 features |= NETIF_F_HW_VLAN_CTAG_TX;
4596 else if (insertion_support->outer &
4597 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4598 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4599 features |= NETIF_F_HW_VLAN_STAG_TX;
4600 } else if (insertion_support->inner !=
4601 VIRTCHNL_VLAN_UNSUPPORTED) {
4602 if (insertion_support->inner &
4603 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4604 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4605 features |= NETIF_F_HW_VLAN_CTAG_TX;
4608 /* give priority to outer filtering and don't bother if both
4609 * outer and inner filtering are enabled
4611 ethertype_init = vlan_v2_caps->filtering.ethertype_init;
4612 if (filtering_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4613 if (filtering_support->outer &
4614 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4615 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4616 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4617 if (filtering_support->outer &
4618 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4619 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4620 features |= NETIF_F_HW_VLAN_STAG_FILTER;
4621 } else if (filtering_support->inner !=
4622 VIRTCHNL_VLAN_UNSUPPORTED) {
4623 if (filtering_support->inner &
4624 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4625 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4626 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4627 if (filtering_support->inner &
4628 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4629 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4630 features |= NETIF_F_HW_VLAN_STAG_FILTER;
4637 #define IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested, allowed, feature_bit) \
4638 (!(((requested) & (feature_bit)) && \
4639 !((allowed) & (feature_bit))))
4642 * iavf_fix_netdev_vlan_features - fix NETDEV VLAN features based on support
4643 * @adapter: board private structure
4644 * @requested_features: stack requested NETDEV features
4646 static netdev_features_t
4647 iavf_fix_netdev_vlan_features(struct iavf_adapter *adapter,
4648 netdev_features_t requested_features)
4650 netdev_features_t allowed_features;
4652 allowed_features = iavf_get_netdev_vlan_hw_features(adapter) |
4653 iavf_get_netdev_vlan_features(adapter);
4655 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4657 NETIF_F_HW_VLAN_CTAG_TX))
4658 requested_features &= ~NETIF_F_HW_VLAN_CTAG_TX;
4660 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4662 NETIF_F_HW_VLAN_CTAG_RX))
4663 requested_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
4665 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4667 NETIF_F_HW_VLAN_STAG_TX))
4668 requested_features &= ~NETIF_F_HW_VLAN_STAG_TX;
4669 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4671 NETIF_F_HW_VLAN_STAG_RX))
4672 requested_features &= ~NETIF_F_HW_VLAN_STAG_RX;
4674 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4676 NETIF_F_HW_VLAN_CTAG_FILTER))
4677 requested_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
4679 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4681 NETIF_F_HW_VLAN_STAG_FILTER))
4682 requested_features &= ~NETIF_F_HW_VLAN_STAG_FILTER;
4684 if ((requested_features &
4685 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
4686 (requested_features &
4687 (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX)) &&
4688 adapter->vlan_v2_caps.offloads.ethertype_match ==
4689 VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION) {
4690 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");
4691 requested_features &= ~(NETIF_F_HW_VLAN_STAG_RX |
4692 NETIF_F_HW_VLAN_STAG_TX);
4695 return requested_features;
4699 * iavf_fix_features - fix up the netdev feature bits
4700 * @netdev: our net device
4701 * @features: desired feature bits
4703 * Returns fixed-up features bits
4705 static netdev_features_t iavf_fix_features(struct net_device *netdev,
4706 netdev_features_t features)
4708 struct iavf_adapter *adapter = netdev_priv(netdev);
4710 return iavf_fix_netdev_vlan_features(adapter, features);
4713 static const struct net_device_ops iavf_netdev_ops = {
4714 .ndo_open = iavf_open,
4715 .ndo_stop = iavf_close,
4716 .ndo_start_xmit = iavf_xmit_frame,
4717 .ndo_set_rx_mode = iavf_set_rx_mode,
4718 .ndo_validate_addr = eth_validate_addr,
4719 .ndo_set_mac_address = iavf_set_mac,
4720 .ndo_change_mtu = iavf_change_mtu,
4721 .ndo_tx_timeout = iavf_tx_timeout,
4722 .ndo_vlan_rx_add_vid = iavf_vlan_rx_add_vid,
4723 .ndo_vlan_rx_kill_vid = iavf_vlan_rx_kill_vid,
4724 .ndo_features_check = iavf_features_check,
4725 .ndo_fix_features = iavf_fix_features,
4726 .ndo_set_features = iavf_set_features,
4727 .ndo_setup_tc = iavf_setup_tc,
4731 * iavf_check_reset_complete - check that VF reset is complete
4732 * @hw: pointer to hw struct
4734 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
4736 static int iavf_check_reset_complete(struct iavf_hw *hw)
4741 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
4742 rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
4743 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
4744 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
4745 (rstat == VIRTCHNL_VFR_COMPLETED))
4747 usleep_range(10, 20);
4753 * iavf_process_config - Process the config information we got from the PF
4754 * @adapter: board private structure
4756 * Verify that we have a valid config struct, and set up our netdev features
4757 * and our VSI struct.
4759 int iavf_process_config(struct iavf_adapter *adapter)
4761 struct virtchnl_vf_resource *vfres = adapter->vf_res;
4762 netdev_features_t hw_vlan_features, vlan_features;
4763 struct net_device *netdev = adapter->netdev;
4764 netdev_features_t hw_enc_features;
4765 netdev_features_t hw_features;
4767 hw_enc_features = NETIF_F_SG |
4771 NETIF_F_SOFT_FEATURES |
4780 /* advertise to stack only if offloads for encapsulated packets is
4783 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
4784 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4786 NETIF_F_GSO_GRE_CSUM |
4787 NETIF_F_GSO_IPXIP4 |
4788 NETIF_F_GSO_IPXIP6 |
4789 NETIF_F_GSO_UDP_TUNNEL_CSUM |
4790 NETIF_F_GSO_PARTIAL |
4793 if (!(vfres->vf_cap_flags &
4794 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
4795 netdev->gso_partial_features |=
4796 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4798 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
4799 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
4800 netdev->hw_enc_features |= hw_enc_features;
4802 /* record features VLANs can make use of */
4803 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
4805 /* Write features and hw_features separately to avoid polluting
4806 * with, or dropping, features that are set when we registered.
4808 hw_features = hw_enc_features;
4810 /* get HW VLAN features that can be toggled */
4811 hw_vlan_features = iavf_get_netdev_vlan_hw_features(adapter);
4813 /* Enable cloud filter if ADQ is supported */
4814 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
4815 hw_features |= NETIF_F_HW_TC;
4816 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_USO)
4817 hw_features |= NETIF_F_GSO_UDP_L4;
4819 netdev->hw_features |= hw_features | hw_vlan_features;
4820 vlan_features = iavf_get_netdev_vlan_features(adapter);
4822 netdev->features |= hw_features | vlan_features;
4824 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
4825 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4827 netdev->priv_flags |= IFF_UNICAST_FLT;
4829 /* Do not turn on offloads when they are requested to be turned off.
4830 * TSO needs minimum 576 bytes to work correctly.
4832 if (netdev->wanted_features) {
4833 if (!(netdev->wanted_features & NETIF_F_TSO) ||
4835 netdev->features &= ~NETIF_F_TSO;
4836 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
4838 netdev->features &= ~NETIF_F_TSO6;
4839 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
4840 netdev->features &= ~NETIF_F_TSO_ECN;
4841 if (!(netdev->wanted_features & NETIF_F_GRO))
4842 netdev->features &= ~NETIF_F_GRO;
4843 if (!(netdev->wanted_features & NETIF_F_GSO))
4844 netdev->features &= ~NETIF_F_GSO;
4851 * iavf_shutdown - Shutdown the device in preparation for a reboot
4852 * @pdev: pci device structure
4854 static void iavf_shutdown(struct pci_dev *pdev)
4856 struct iavf_adapter *adapter = iavf_pdev_to_adapter(pdev);
4857 struct net_device *netdev = adapter->netdev;
4859 netif_device_detach(netdev);
4861 if (netif_running(netdev))
4864 if (iavf_lock_timeout(&adapter->crit_lock, 5000))
4865 dev_warn(&adapter->pdev->dev, "%s: failed to acquire crit_lock\n", __func__);
4866 /* Prevent the watchdog from running. */
4867 iavf_change_state(adapter, __IAVF_REMOVE);
4868 adapter->aq_required = 0;
4869 mutex_unlock(&adapter->crit_lock);
4872 pci_save_state(pdev);
4875 pci_disable_device(pdev);
4879 * iavf_probe - Device Initialization Routine
4880 * @pdev: PCI device information struct
4881 * @ent: entry in iavf_pci_tbl
4883 * Returns 0 on success, negative on failure
4885 * iavf_probe initializes an adapter identified by a pci_dev structure.
4886 * The OS initialization, configuring of the adapter private structure,
4887 * and a hardware reset occur.
4889 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4891 struct net_device *netdev;
4892 struct iavf_adapter *adapter = NULL;
4893 struct iavf_hw *hw = NULL;
4896 err = pci_enable_device(pdev);
4900 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4903 "DMA configuration failed: 0x%x\n", err);
4907 err = pci_request_regions(pdev, iavf_driver_name);
4910 "pci_request_regions failed 0x%x\n", err);
4914 pci_set_master(pdev);
4916 netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
4917 IAVF_MAX_REQ_QUEUES);
4920 goto err_alloc_etherdev;
4923 SET_NETDEV_DEV(netdev, &pdev->dev);
4925 pci_set_drvdata(pdev, netdev);
4926 adapter = netdev_priv(netdev);
4928 adapter->netdev = netdev;
4929 adapter->pdev = pdev;
4934 adapter->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
4941 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
4942 iavf_change_state(adapter, __IAVF_STARTUP);
4944 /* Call save state here because it relies on the adapter struct. */
4945 pci_save_state(pdev);
4947 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4948 pci_resource_len(pdev, 0));
4953 hw->vendor_id = pdev->vendor;
4954 hw->device_id = pdev->device;
4955 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4956 hw->subsystem_vendor_id = pdev->subsystem_vendor;
4957 hw->subsystem_device_id = pdev->subsystem_device;
4958 hw->bus.device = PCI_SLOT(pdev->devfn);
4959 hw->bus.func = PCI_FUNC(pdev->devfn);
4960 hw->bus.bus_id = pdev->bus->number;
4962 /* set up the locks for the AQ, do this only once in probe
4963 * and destroy them only once in remove
4965 mutex_init(&adapter->crit_lock);
4966 mutex_init(&adapter->client_lock);
4967 mutex_init(&hw->aq.asq_mutex);
4968 mutex_init(&hw->aq.arq_mutex);
4970 spin_lock_init(&adapter->mac_vlan_list_lock);
4971 spin_lock_init(&adapter->cloud_filter_list_lock);
4972 spin_lock_init(&adapter->fdir_fltr_lock);
4973 spin_lock_init(&adapter->adv_rss_lock);
4975 INIT_LIST_HEAD(&adapter->mac_filter_list);
4976 INIT_LIST_HEAD(&adapter->vlan_filter_list);
4977 INIT_LIST_HEAD(&adapter->cloud_filter_list);
4978 INIT_LIST_HEAD(&adapter->fdir_list_head);
4979 INIT_LIST_HEAD(&adapter->adv_rss_list_head);
4981 INIT_WORK(&adapter->reset_task, iavf_reset_task);
4982 INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
4983 INIT_WORK(&adapter->finish_config, iavf_finish_config);
4984 INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
4985 INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
4986 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
4987 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
4989 /* Setup the wait queue for indicating transition to down status */
4990 init_waitqueue_head(&adapter->down_waitqueue);
4992 /* Setup the wait queue for indicating transition to running state */
4993 init_waitqueue_head(&adapter->reset_waitqueue);
4995 /* Setup the wait queue for indicating virtchannel events */
4996 init_waitqueue_head(&adapter->vc_waitqueue);
5001 destroy_workqueue(adapter->wq);
5003 free_netdev(netdev);
5005 pci_release_regions(pdev);
5008 pci_disable_device(pdev);
5013 * iavf_suspend - Power management suspend routine
5014 * @dev_d: device info pointer
5016 * Called when the system (VM) is entering sleep/suspend.
5018 static int __maybe_unused iavf_suspend(struct device *dev_d)
5020 struct net_device *netdev = dev_get_drvdata(dev_d);
5021 struct iavf_adapter *adapter = netdev_priv(netdev);
5023 netif_device_detach(netdev);
5025 while (!mutex_trylock(&adapter->crit_lock))
5026 usleep_range(500, 1000);
5028 if (netif_running(netdev)) {
5033 iavf_free_misc_irq(adapter);
5034 iavf_reset_interrupt_capability(adapter);
5036 mutex_unlock(&adapter->crit_lock);
5042 * iavf_resume - Power management resume routine
5043 * @dev_d: device info pointer
5045 * Called when the system (VM) is resumed from sleep/suspend.
5047 static int __maybe_unused iavf_resume(struct device *dev_d)
5049 struct pci_dev *pdev = to_pci_dev(dev_d);
5050 struct iavf_adapter *adapter;
5053 adapter = iavf_pdev_to_adapter(pdev);
5055 pci_set_master(pdev);
5058 err = iavf_set_interrupt_capability(adapter);
5061 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
5064 err = iavf_request_misc_irq(adapter);
5067 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
5071 queue_work(adapter->wq, &adapter->reset_task);
5073 netif_device_attach(adapter->netdev);
5079 * iavf_remove - Device Removal Routine
5080 * @pdev: PCI device information struct
5082 * iavf_remove is called by the PCI subsystem to alert the driver
5083 * that it should release a PCI device. The could be caused by a
5084 * Hot-Plug event, or because the driver is going to be removed from
5087 static void iavf_remove(struct pci_dev *pdev)
5089 struct iavf_adapter *adapter = iavf_pdev_to_adapter(pdev);
5090 struct iavf_fdir_fltr *fdir, *fdirtmp;
5091 struct iavf_vlan_filter *vlf, *vlftmp;
5092 struct iavf_cloud_filter *cf, *cftmp;
5093 struct iavf_adv_rss *rss, *rsstmp;
5094 struct iavf_mac_filter *f, *ftmp;
5095 struct net_device *netdev;
5099 netdev = adapter->netdev;
5102 if (test_and_set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
5105 /* Wait until port initialization is complete.
5106 * There are flows where register/unregister netdev may race.
5109 mutex_lock(&adapter->crit_lock);
5110 if (adapter->state == __IAVF_RUNNING ||
5111 adapter->state == __IAVF_DOWN ||
5112 adapter->state == __IAVF_INIT_FAILED) {
5113 mutex_unlock(&adapter->crit_lock);
5116 /* Simply return if we already went through iavf_shutdown */
5117 if (adapter->state == __IAVF_REMOVE) {
5118 mutex_unlock(&adapter->crit_lock);
5122 mutex_unlock(&adapter->crit_lock);
5123 usleep_range(500, 1000);
5125 cancel_delayed_work_sync(&adapter->watchdog_task);
5126 cancel_work_sync(&adapter->finish_config);
5129 if (adapter->netdev_registered) {
5130 unregister_netdevice(netdev);
5131 adapter->netdev_registered = false;
5135 if (CLIENT_ALLOWED(adapter)) {
5136 err = iavf_lan_del_device(adapter);
5138 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
5142 mutex_lock(&adapter->crit_lock);
5143 dev_info(&adapter->pdev->dev, "Removing device\n");
5144 iavf_change_state(adapter, __IAVF_REMOVE);
5146 iavf_request_reset(adapter);
5148 /* If the FW isn't responding, kick it once, but only once. */
5149 if (!iavf_asq_done(hw)) {
5150 iavf_request_reset(adapter);
5154 iavf_misc_irq_disable(adapter);
5155 /* Shut down all the garbage mashers on the detention level */
5156 cancel_work_sync(&adapter->reset_task);
5157 cancel_delayed_work_sync(&adapter->watchdog_task);
5158 cancel_work_sync(&adapter->adminq_task);
5159 cancel_delayed_work_sync(&adapter->client_task);
5161 adapter->aq_required = 0;
5162 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
5164 iavf_free_all_tx_resources(adapter);
5165 iavf_free_all_rx_resources(adapter);
5166 iavf_free_misc_irq(adapter);
5168 iavf_reset_interrupt_capability(adapter);
5169 iavf_free_q_vectors(adapter);
5171 iavf_free_rss(adapter);
5173 if (hw->aq.asq.count)
5174 iavf_shutdown_adminq(hw);
5176 /* destroy the locks only once, here */
5177 mutex_destroy(&hw->aq.arq_mutex);
5178 mutex_destroy(&hw->aq.asq_mutex);
5179 mutex_destroy(&adapter->client_lock);
5180 mutex_unlock(&adapter->crit_lock);
5181 mutex_destroy(&adapter->crit_lock);
5183 iounmap(hw->hw_addr);
5184 pci_release_regions(pdev);
5185 iavf_free_queues(adapter);
5186 kfree(adapter->vf_res);
5187 spin_lock_bh(&adapter->mac_vlan_list_lock);
5188 /* If we got removed before an up/down sequence, we've got a filter
5189 * hanging out there that we need to get rid of.
5191 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
5195 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
5197 list_del(&vlf->list);
5201 spin_unlock_bh(&adapter->mac_vlan_list_lock);
5203 spin_lock_bh(&adapter->cloud_filter_list_lock);
5204 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
5205 list_del(&cf->list);
5208 spin_unlock_bh(&adapter->cloud_filter_list_lock);
5210 spin_lock_bh(&adapter->fdir_fltr_lock);
5211 list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head, list) {
5212 list_del(&fdir->list);
5215 spin_unlock_bh(&adapter->fdir_fltr_lock);
5217 spin_lock_bh(&adapter->adv_rss_lock);
5218 list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
5220 list_del(&rss->list);
5223 spin_unlock_bh(&adapter->adv_rss_lock);
5225 destroy_workqueue(adapter->wq);
5227 free_netdev(netdev);
5229 pci_disable_device(pdev);
5232 static SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
5234 static struct pci_driver iavf_driver = {
5235 .name = iavf_driver_name,
5236 .id_table = iavf_pci_tbl,
5237 .probe = iavf_probe,
5238 .remove = iavf_remove,
5239 .driver.pm = &iavf_pm_ops,
5240 .shutdown = iavf_shutdown,
5244 * iavf_init_module - Driver Registration Routine
5246 * iavf_init_module is the first routine called when the driver is
5247 * loaded. All it does is register with the PCI subsystem.
5249 static int __init iavf_init_module(void)
5251 pr_info("iavf: %s\n", iavf_driver_string);
5253 pr_info("%s\n", iavf_copyright);
5255 return pci_register_driver(&iavf_driver);
5258 module_init(iavf_init_module);
5261 * iavf_exit_module - Driver Exit Cleanup Routine
5263 * iavf_exit_module is called just before the driver is removed
5266 static void __exit iavf_exit_module(void)
5268 pci_unregister_driver(&iavf_driver);
5271 module_exit(iavf_exit_module);