1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
5 #include "i40e_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 "i40e_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);
18 char iavf_driver_name[] = "iavf";
19 static const char iavf_driver_string[] =
20 "Intel(R) Ethernet Adaptive Virtual Function Network Driver";
24 #define DRV_VERSION_MAJOR 3
25 #define DRV_VERSION_MINOR 2
26 #define DRV_VERSION_BUILD 3
27 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
28 __stringify(DRV_VERSION_MINOR) "." \
29 __stringify(DRV_VERSION_BUILD) \
31 const char iavf_driver_version[] = DRV_VERSION;
32 static const char iavf_copyright[] =
33 "Copyright (c) 2013 - 2018 Intel Corporation.";
35 /* iavf_pci_tbl - PCI Device ID Table
37 * Wildcard entries (PCI_ANY_ID) should come last
38 * Last entry must be all 0s
40 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
41 * Class, Class Mask, private data (not used) }
43 static const struct pci_device_id iavf_pci_tbl[] = {
44 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
45 {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
46 {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
47 {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
48 /* required last entry */
52 MODULE_DEVICE_TABLE(pci, iavf_pci_tbl);
54 MODULE_ALIAS("i40evf");
55 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
56 MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(DRV_VERSION);
60 static struct workqueue_struct *iavf_wq;
63 * iavf_allocate_dma_mem_d - OS specific memory alloc for shared code
64 * @hw: pointer to the HW structure
65 * @mem: ptr to mem struct to fill out
66 * @size: size of memory requested
67 * @alignment: what to align the allocation to
69 iavf_status iavf_allocate_dma_mem_d(struct i40e_hw *hw,
70 struct i40e_dma_mem *mem,
71 u64 size, u32 alignment)
73 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
76 return I40E_ERR_PARAM;
78 mem->size = ALIGN(size, alignment);
79 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
80 (dma_addr_t *)&mem->pa, GFP_KERNEL);
84 return I40E_ERR_NO_MEMORY;
88 * iavf_free_dma_mem_d - OS specific memory free for shared code
89 * @hw: pointer to the HW structure
90 * @mem: ptr to mem struct to free
92 iavf_status iavf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
94 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
97 return I40E_ERR_PARAM;
98 dma_free_coherent(&adapter->pdev->dev, mem->size,
99 mem->va, (dma_addr_t)mem->pa);
104 * iavf_allocate_virt_mem_d - OS specific memory alloc for shared code
105 * @hw: pointer to the HW structure
106 * @mem: ptr to mem struct to fill out
107 * @size: size of memory requested
109 iavf_status iavf_allocate_virt_mem_d(struct i40e_hw *hw,
110 struct i40e_virt_mem *mem, u32 size)
113 return I40E_ERR_PARAM;
116 mem->va = kzalloc(size, GFP_KERNEL);
121 return I40E_ERR_NO_MEMORY;
125 * iavf_free_virt_mem_d - OS specific memory free for shared code
126 * @hw: pointer to the HW structure
127 * @mem: ptr to mem struct to free
129 iavf_status iavf_free_virt_mem_d(struct i40e_hw *hw,
130 struct i40e_virt_mem *mem)
133 return I40E_ERR_PARAM;
135 /* it's ok to kfree a NULL pointer */
142 * iavf_debug_d - OS dependent version of debug printing
143 * @hw: pointer to the HW structure
144 * @mask: debug level mask
145 * @fmt_str: printf-type format description
147 void iavf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
152 if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
155 va_start(argptr, fmt_str);
156 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
159 /* the debug string is already formatted with a newline */
164 * iavf_schedule_reset - Set the flags and schedule a reset event
165 * @adapter: board private structure
167 void iavf_schedule_reset(struct iavf_adapter *adapter)
169 if (!(adapter->flags &
170 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
171 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
172 schedule_work(&adapter->reset_task);
177 * iavf_tx_timeout - Respond to a Tx Hang
178 * @netdev: network interface device structure
180 static void iavf_tx_timeout(struct net_device *netdev)
182 struct iavf_adapter *adapter = netdev_priv(netdev);
184 adapter->tx_timeout_count++;
185 iavf_schedule_reset(adapter);
189 * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
190 * @adapter: board private structure
192 static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
194 struct i40e_hw *hw = &adapter->hw;
196 if (!adapter->msix_entries)
199 wr32(hw, I40E_VFINT_DYN_CTL01, 0);
202 rd32(hw, I40E_VFGEN_RSTAT);
204 synchronize_irq(adapter->msix_entries[0].vector);
208 * iavf_misc_irq_enable - Enable default interrupt generation settings
209 * @adapter: board private structure
211 static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
213 struct i40e_hw *hw = &adapter->hw;
215 wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
216 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
217 wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
220 rd32(hw, I40E_VFGEN_RSTAT);
224 * iavf_irq_disable - Mask off interrupt generation on the NIC
225 * @adapter: board private structure
227 static void iavf_irq_disable(struct iavf_adapter *adapter)
230 struct i40e_hw *hw = &adapter->hw;
232 if (!adapter->msix_entries)
235 for (i = 1; i < adapter->num_msix_vectors; i++) {
236 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
237 synchronize_irq(adapter->msix_entries[i].vector);
240 rd32(hw, I40E_VFGEN_RSTAT);
244 * iavf_irq_enable_queues - Enable interrupt for specified queues
245 * @adapter: board private structure
246 * @mask: bitmap of queues to enable
248 void iavf_irq_enable_queues(struct iavf_adapter *adapter, u32 mask)
250 struct i40e_hw *hw = &adapter->hw;
253 for (i = 1; i < adapter->num_msix_vectors; i++) {
254 if (mask & BIT(i - 1)) {
255 wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
256 I40E_VFINT_DYN_CTLN1_INTENA_MASK |
257 I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK);
263 * iavf_irq_enable - Enable default interrupt generation settings
264 * @adapter: board private structure
265 * @flush: boolean value whether to run rd32()
267 void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
269 struct i40e_hw *hw = &adapter->hw;
271 iavf_misc_irq_enable(adapter);
272 iavf_irq_enable_queues(adapter, ~0);
275 rd32(hw, I40E_VFGEN_RSTAT);
279 * iavf_msix_aq - Interrupt handler for vector 0
280 * @irq: interrupt number
281 * @data: pointer to netdev
283 static irqreturn_t iavf_msix_aq(int irq, void *data)
285 struct net_device *netdev = data;
286 struct iavf_adapter *adapter = netdev_priv(netdev);
287 struct i40e_hw *hw = &adapter->hw;
289 /* handle non-queue interrupts, these reads clear the registers */
290 rd32(hw, I40E_VFINT_ICR01);
291 rd32(hw, I40E_VFINT_ICR0_ENA1);
293 /* schedule work on the private workqueue */
294 schedule_work(&adapter->adminq_task);
300 * iavf_msix_clean_rings - MSIX mode Interrupt Handler
301 * @irq: interrupt number
302 * @data: pointer to a q_vector
304 static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
306 struct i40e_q_vector *q_vector = data;
308 if (!q_vector->tx.ring && !q_vector->rx.ring)
311 napi_schedule_irqoff(&q_vector->napi);
317 * iavf_map_vector_to_rxq - associate irqs with rx queues
318 * @adapter: board private structure
319 * @v_idx: interrupt number
320 * @r_idx: queue number
323 iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
325 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
326 struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
327 struct i40e_hw *hw = &adapter->hw;
329 rx_ring->q_vector = q_vector;
330 rx_ring->next = q_vector->rx.ring;
331 rx_ring->vsi = &adapter->vsi;
332 q_vector->rx.ring = rx_ring;
333 q_vector->rx.count++;
334 q_vector->rx.next_update = jiffies + 1;
335 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
336 q_vector->ring_mask |= BIT(r_idx);
337 wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, q_vector->reg_idx),
338 q_vector->rx.current_itr);
339 q_vector->rx.current_itr = q_vector->rx.target_itr;
343 * iavf_map_vector_to_txq - associate irqs with tx queues
344 * @adapter: board private structure
345 * @v_idx: interrupt number
346 * @t_idx: queue number
349 iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
351 struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
352 struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
353 struct i40e_hw *hw = &adapter->hw;
355 tx_ring->q_vector = q_vector;
356 tx_ring->next = q_vector->tx.ring;
357 tx_ring->vsi = &adapter->vsi;
358 q_vector->tx.ring = tx_ring;
359 q_vector->tx.count++;
360 q_vector->tx.next_update = jiffies + 1;
361 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
362 q_vector->num_ringpairs++;
363 wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, q_vector->reg_idx),
364 q_vector->tx.target_itr);
365 q_vector->tx.current_itr = q_vector->tx.target_itr;
369 * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
370 * @adapter: board private structure to initialize
372 * This function maps descriptor rings to the queue-specific vectors
373 * we were allotted through the MSI-X enabling code. Ideally, we'd have
374 * one vector per ring/queue, but on a constrained vector budget, we
375 * group the rings as "efficiently" as possible. You would add new
376 * mapping configurations in here.
378 static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
380 int rings_remaining = adapter->num_active_queues;
381 int ridx = 0, vidx = 0;
384 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
386 for (; ridx < rings_remaining; ridx++) {
387 iavf_map_vector_to_rxq(adapter, vidx, ridx);
388 iavf_map_vector_to_txq(adapter, vidx, ridx);
390 /* In the case where we have more queues than vectors, continue
391 * round-robin on vectors until all queues are mapped.
393 if (++vidx >= q_vectors)
397 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
400 #ifdef CONFIG_NET_POLL_CONTROLLER
402 * iavf_netpoll - A Polling 'interrupt' handler
403 * @netdev: network interface device structure
405 * This is used by netconsole to send skbs without having to re-enable
406 * interrupts. It's not called while the normal interrupt routine is executing.
408 static void iavf_netpoll(struct net_device *netdev)
410 struct iavf_adapter *adapter = netdev_priv(netdev);
411 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
414 /* if interface is down do nothing */
415 if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
418 for (i = 0; i < q_vectors; i++)
419 iavf_msix_clean_rings(0, &adapter->q_vectors[i]);
424 * iavf_irq_affinity_notify - Callback for affinity changes
425 * @notify: context as to what irq was changed
426 * @mask: the new affinity mask
428 * This is a callback function used by the irq_set_affinity_notifier function
429 * so that we may register to receive changes to the irq affinity masks.
431 static void iavf_irq_affinity_notify(struct irq_affinity_notify *notify,
432 const cpumask_t *mask)
434 struct i40e_q_vector *q_vector =
435 container_of(notify, struct i40e_q_vector, affinity_notify);
437 cpumask_copy(&q_vector->affinity_mask, mask);
441 * iavf_irq_affinity_release - Callback for affinity notifier release
442 * @ref: internal core kernel usage
444 * This is a callback function used by the irq_set_affinity_notifier function
445 * to inform the current notification subscriber that they will no longer
446 * receive notifications.
448 static void iavf_irq_affinity_release(struct kref *ref) {}
451 * iavf_request_traffic_irqs - Initialize MSI-X interrupts
452 * @adapter: board private structure
453 * @basename: device basename
455 * Allocates MSI-X vectors for tx and rx handling, and requests
456 * interrupts from the kernel.
459 iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
461 unsigned int vector, q_vectors;
462 unsigned int rx_int_idx = 0, tx_int_idx = 0;
466 iavf_irq_disable(adapter);
467 /* Decrement for Other and TCP Timer vectors */
468 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
470 for (vector = 0; vector < q_vectors; vector++) {
471 struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
473 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
475 if (q_vector->tx.ring && q_vector->rx.ring) {
476 snprintf(q_vector->name, sizeof(q_vector->name),
477 "iavf-%s-TxRx-%d", basename, rx_int_idx++);
479 } else if (q_vector->rx.ring) {
480 snprintf(q_vector->name, sizeof(q_vector->name),
481 "iavf-%s-rx-%d", basename, rx_int_idx++);
482 } else if (q_vector->tx.ring) {
483 snprintf(q_vector->name, sizeof(q_vector->name),
484 "iavf-%s-tx-%d", basename, tx_int_idx++);
486 /* skip this unused q_vector */
489 err = request_irq(irq_num,
490 iavf_msix_clean_rings,
495 dev_info(&adapter->pdev->dev,
496 "Request_irq failed, error: %d\n", err);
497 goto free_queue_irqs;
499 /* register for affinity change notifications */
500 q_vector->affinity_notify.notify = iavf_irq_affinity_notify;
501 q_vector->affinity_notify.release =
502 iavf_irq_affinity_release;
503 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
504 /* Spread the IRQ affinity hints across online CPUs. Note that
505 * get_cpu_mask returns a mask with a permanent lifetime so
506 * it's safe to use as a hint for irq_set_affinity_hint.
508 cpu = cpumask_local_spread(q_vector->v_idx, -1);
509 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
517 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
518 irq_set_affinity_notifier(irq_num, NULL);
519 irq_set_affinity_hint(irq_num, NULL);
520 free_irq(irq_num, &adapter->q_vectors[vector]);
526 * iavf_request_misc_irq - Initialize MSI-X interrupts
527 * @adapter: board private structure
529 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
530 * vector is only for the admin queue, and stays active even when the netdev
533 static int iavf_request_misc_irq(struct iavf_adapter *adapter)
535 struct net_device *netdev = adapter->netdev;
538 snprintf(adapter->misc_vector_name,
539 sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
540 dev_name(&adapter->pdev->dev));
541 err = request_irq(adapter->msix_entries[0].vector,
543 adapter->misc_vector_name, netdev);
545 dev_err(&adapter->pdev->dev,
546 "request_irq for %s failed: %d\n",
547 adapter->misc_vector_name, err);
548 free_irq(adapter->msix_entries[0].vector, netdev);
554 * iavf_free_traffic_irqs - Free MSI-X interrupts
555 * @adapter: board private structure
557 * Frees all MSI-X vectors other than 0.
559 static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
561 int vector, irq_num, q_vectors;
563 if (!adapter->msix_entries)
566 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
568 for (vector = 0; vector < q_vectors; vector++) {
569 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
570 irq_set_affinity_notifier(irq_num, NULL);
571 irq_set_affinity_hint(irq_num, NULL);
572 free_irq(irq_num, &adapter->q_vectors[vector]);
577 * iavf_free_misc_irq - Free MSI-X miscellaneous vector
578 * @adapter: board private structure
580 * Frees MSI-X vector 0.
582 static void iavf_free_misc_irq(struct iavf_adapter *adapter)
584 struct net_device *netdev = adapter->netdev;
586 if (!adapter->msix_entries)
589 free_irq(adapter->msix_entries[0].vector, netdev);
593 * iavf_configure_tx - Configure Transmit Unit after Reset
594 * @adapter: board private structure
596 * Configure the Tx unit of the MAC after a reset.
598 static void iavf_configure_tx(struct iavf_adapter *adapter)
600 struct i40e_hw *hw = &adapter->hw;
603 for (i = 0; i < adapter->num_active_queues; i++)
604 adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
608 * iavf_configure_rx - Configure Receive Unit after Reset
609 * @adapter: board private structure
611 * Configure the Rx unit of the MAC after a reset.
613 static void iavf_configure_rx(struct iavf_adapter *adapter)
615 unsigned int rx_buf_len = I40E_RXBUFFER_2048;
616 struct i40e_hw *hw = &adapter->hw;
619 /* Legacy Rx will always default to a 2048 buffer size. */
620 #if (PAGE_SIZE < 8192)
621 if (!(adapter->flags & IAVF_FLAG_LEGACY_RX)) {
622 struct net_device *netdev = adapter->netdev;
624 /* For jumbo frames on systems with 4K pages we have to use
625 * an order 1 page, so we might as well increase the size
626 * of our Rx buffer to make better use of the available space
628 rx_buf_len = I40E_RXBUFFER_3072;
630 /* We use a 1536 buffer size for configurations with
631 * standard Ethernet mtu. On x86 this gives us enough room
632 * for shared info and 192 bytes of padding.
634 if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
635 (netdev->mtu <= ETH_DATA_LEN))
636 rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
640 for (i = 0; i < adapter->num_active_queues; i++) {
641 adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
642 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
644 if (adapter->flags & IAVF_FLAG_LEGACY_RX)
645 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
647 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
652 * iavf_find_vlan - Search filter list for specific vlan filter
653 * @adapter: board private structure
656 * Returns ptr to the filter object or NULL. Must be called while holding the
657 * mac_vlan_list_lock.
660 iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter, u16 vlan)
662 struct iavf_vlan_filter *f;
664 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
672 * iavf_add_vlan - Add a vlan filter to the list
673 * @adapter: board private structure
676 * Returns ptr to the filter object or NULL when no memory available.
679 iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter, u16 vlan)
681 struct iavf_vlan_filter *f = NULL;
683 spin_lock_bh(&adapter->mac_vlan_list_lock);
685 f = iavf_find_vlan(adapter, vlan);
687 f = kzalloc(sizeof(*f), GFP_KERNEL);
693 INIT_LIST_HEAD(&f->list);
694 list_add(&f->list, &adapter->vlan_filter_list);
696 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
700 spin_unlock_bh(&adapter->mac_vlan_list_lock);
705 * iavf_del_vlan - Remove a vlan filter from the list
706 * @adapter: board private structure
709 static void iavf_del_vlan(struct iavf_adapter *adapter, u16 vlan)
711 struct iavf_vlan_filter *f;
713 spin_lock_bh(&adapter->mac_vlan_list_lock);
715 f = iavf_find_vlan(adapter, vlan);
718 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
721 spin_unlock_bh(&adapter->mac_vlan_list_lock);
725 * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
726 * @netdev: network device struct
727 * @proto: unused protocol data
730 static int iavf_vlan_rx_add_vid(struct net_device *netdev,
731 __always_unused __be16 proto, u16 vid)
733 struct iavf_adapter *adapter = netdev_priv(netdev);
735 if (!VLAN_ALLOWED(adapter))
737 if (iavf_add_vlan(adapter, vid) == NULL)
743 * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
744 * @netdev: network device struct
745 * @proto: unused protocol data
748 static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
749 __always_unused __be16 proto, u16 vid)
751 struct iavf_adapter *adapter = netdev_priv(netdev);
753 if (VLAN_ALLOWED(adapter)) {
754 iavf_del_vlan(adapter, vid);
761 * iavf_find_filter - Search filter list for specific mac filter
762 * @adapter: board private structure
763 * @macaddr: the MAC address
765 * Returns ptr to the filter object or NULL. Must be called while holding the
766 * mac_vlan_list_lock.
769 iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
772 struct iavf_mac_filter *f;
777 list_for_each_entry(f, &adapter->mac_filter_list, list) {
778 if (ether_addr_equal(macaddr, f->macaddr))
785 * i40e_add_filter - Add a mac filter to the filter list
786 * @adapter: board private structure
787 * @macaddr: the MAC address
789 * Returns ptr to the filter object or NULL when no memory available.
792 iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
795 struct iavf_mac_filter *f;
800 f = iavf_find_filter(adapter, macaddr);
802 f = kzalloc(sizeof(*f), GFP_ATOMIC);
806 ether_addr_copy(f->macaddr, macaddr);
808 list_add_tail(&f->list, &adapter->mac_filter_list);
810 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
819 * iavf_set_mac - NDO callback to set port mac address
820 * @netdev: network interface device structure
821 * @p: pointer to an address structure
823 * Returns 0 on success, negative on failure
825 static int iavf_set_mac(struct net_device *netdev, void *p)
827 struct iavf_adapter *adapter = netdev_priv(netdev);
828 struct i40e_hw *hw = &adapter->hw;
829 struct iavf_mac_filter *f;
830 struct sockaddr *addr = p;
832 if (!is_valid_ether_addr(addr->sa_data))
833 return -EADDRNOTAVAIL;
835 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
838 if (adapter->flags & IAVF_FLAG_ADDR_SET_BY_PF)
841 spin_lock_bh(&adapter->mac_vlan_list_lock);
843 f = iavf_find_filter(adapter, hw->mac.addr);
846 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
849 f = iavf_add_filter(adapter, addr->sa_data);
851 spin_unlock_bh(&adapter->mac_vlan_list_lock);
854 ether_addr_copy(hw->mac.addr, addr->sa_data);
855 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
858 return (f == NULL) ? -ENOMEM : 0;
862 * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
863 * @netdev: the netdevice
864 * @addr: address to add
866 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
867 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
869 static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
871 struct iavf_adapter *adapter = netdev_priv(netdev);
873 if (iavf_add_filter(adapter, addr))
880 * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
881 * @netdev: the netdevice
882 * @addr: address to add
884 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
885 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
887 static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
889 struct iavf_adapter *adapter = netdev_priv(netdev);
890 struct iavf_mac_filter *f;
892 /* Under some circumstances, we might receive a request to delete
893 * our own device address from our uc list. Because we store the
894 * device address in the VSI's MAC/VLAN filter list, we need to ignore
895 * such requests and not delete our device address from this list.
897 if (ether_addr_equal(addr, netdev->dev_addr))
900 f = iavf_find_filter(adapter, addr);
903 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
909 * iavf_set_rx_mode - NDO callback to set the netdev filters
910 * @netdev: network interface device structure
912 static void iavf_set_rx_mode(struct net_device *netdev)
914 struct iavf_adapter *adapter = netdev_priv(netdev);
916 spin_lock_bh(&adapter->mac_vlan_list_lock);
917 __dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
918 __dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
919 spin_unlock_bh(&adapter->mac_vlan_list_lock);
921 if (netdev->flags & IFF_PROMISC &&
922 !(adapter->flags & IAVF_FLAG_PROMISC_ON))
923 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_PROMISC;
924 else if (!(netdev->flags & IFF_PROMISC) &&
925 adapter->flags & IAVF_FLAG_PROMISC_ON)
926 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_PROMISC;
928 if (netdev->flags & IFF_ALLMULTI &&
929 !(adapter->flags & IAVF_FLAG_ALLMULTI_ON))
930 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_ALLMULTI;
931 else if (!(netdev->flags & IFF_ALLMULTI) &&
932 adapter->flags & IAVF_FLAG_ALLMULTI_ON)
933 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_ALLMULTI;
937 * iavf_napi_enable_all - enable NAPI on all queue vectors
938 * @adapter: board private structure
940 static void iavf_napi_enable_all(struct iavf_adapter *adapter)
943 struct i40e_q_vector *q_vector;
944 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
946 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
947 struct napi_struct *napi;
949 q_vector = &adapter->q_vectors[q_idx];
950 napi = &q_vector->napi;
956 * iavf_napi_disable_all - disable NAPI on all queue vectors
957 * @adapter: board private structure
959 static void iavf_napi_disable_all(struct iavf_adapter *adapter)
962 struct i40e_q_vector *q_vector;
963 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
965 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
966 q_vector = &adapter->q_vectors[q_idx];
967 napi_disable(&q_vector->napi);
972 * iavf_configure - set up transmit and receive data structures
973 * @adapter: board private structure
975 static void iavf_configure(struct iavf_adapter *adapter)
977 struct net_device *netdev = adapter->netdev;
980 iavf_set_rx_mode(netdev);
982 iavf_configure_tx(adapter);
983 iavf_configure_rx(adapter);
984 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
986 for (i = 0; i < adapter->num_active_queues; i++) {
987 struct i40e_ring *ring = &adapter->rx_rings[i];
989 iavf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
994 * iavf_up_complete - Finish the last steps of bringing up a connection
995 * @adapter: board private structure
997 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
999 static void iavf_up_complete(struct iavf_adapter *adapter)
1001 adapter->state = __IAVF_RUNNING;
1002 clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1004 iavf_napi_enable_all(adapter);
1006 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_QUEUES;
1007 if (CLIENT_ENABLED(adapter))
1008 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_OPEN;
1009 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1013 * i40e_down - Shutdown the connection processing
1014 * @adapter: board private structure
1016 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
1018 void iavf_down(struct iavf_adapter *adapter)
1020 struct net_device *netdev = adapter->netdev;
1021 struct iavf_vlan_filter *vlf;
1022 struct iavf_mac_filter *f;
1023 struct iavf_cloud_filter *cf;
1025 if (adapter->state <= __IAVF_DOWN_PENDING)
1028 netif_carrier_off(netdev);
1029 netif_tx_disable(netdev);
1030 adapter->link_up = false;
1031 iavf_napi_disable_all(adapter);
1032 iavf_irq_disable(adapter);
1034 spin_lock_bh(&adapter->mac_vlan_list_lock);
1036 /* clear the sync flag on all filters */
1037 __dev_uc_unsync(adapter->netdev, NULL);
1038 __dev_mc_unsync(adapter->netdev, NULL);
1040 /* remove all MAC filters */
1041 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1045 /* remove all VLAN filters */
1046 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1050 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1052 /* remove all cloud filters */
1053 spin_lock_bh(&adapter->cloud_filter_list_lock);
1054 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1057 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1059 if (!(adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) &&
1060 adapter->state != __IAVF_RESETTING) {
1061 /* cancel any current operation */
1062 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1063 /* Schedule operations to close down the HW. Don't wait
1064 * here for this to complete. The watchdog is still running
1065 * and it will take care of this.
1067 adapter->aq_required = IAVF_FLAG_AQ_DEL_MAC_FILTER;
1068 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
1069 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1070 adapter->aq_required |= IAVF_FLAG_AQ_DISABLE_QUEUES;
1073 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1077 * iavf_acquire_msix_vectors - Setup the MSIX capability
1078 * @adapter: board private structure
1079 * @vectors: number of vectors to request
1081 * Work with the OS to set up the MSIX vectors needed.
1083 * Returns 0 on success, negative on failure
1086 iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1088 int err, vector_threshold;
1090 /* We'll want at least 3 (vector_threshold):
1091 * 0) Other (Admin Queue and link, mostly)
1095 vector_threshold = MIN_MSIX_COUNT;
1097 /* The more we get, the more we will assign to Tx/Rx Cleanup
1098 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1099 * Right now, we simply care about how many we'll get; we'll
1100 * set them up later while requesting irq's.
1102 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1103 vector_threshold, vectors);
1105 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1106 kfree(adapter->msix_entries);
1107 adapter->msix_entries = NULL;
1111 /* Adjust for only the vectors we'll use, which is minimum
1112 * of max_msix_q_vectors + NONQ_VECS, or the number of
1113 * vectors we were allocated.
1115 adapter->num_msix_vectors = err;
1120 * iavf_free_queues - Free memory for all rings
1121 * @adapter: board private structure to initialize
1123 * Free all of the memory associated with queue pairs.
1125 static void iavf_free_queues(struct iavf_adapter *adapter)
1127 if (!adapter->vsi_res)
1129 adapter->num_active_queues = 0;
1130 kfree(adapter->tx_rings);
1131 adapter->tx_rings = NULL;
1132 kfree(adapter->rx_rings);
1133 adapter->rx_rings = NULL;
1137 * iavf_alloc_queues - Allocate memory for all rings
1138 * @adapter: board private structure to initialize
1140 * We allocate one ring per queue at run-time since we don't know the
1141 * number of queues at compile-time. The polling_netdev array is
1142 * intended for Multiqueue, but should work fine with a single queue.
1144 static int iavf_alloc_queues(struct iavf_adapter *adapter)
1146 int i, num_active_queues;
1148 /* If we're in reset reallocating queues we don't actually know yet for
1149 * certain the PF gave us the number of queues we asked for but we'll
1150 * assume it did. Once basic reset is finished we'll confirm once we
1151 * start negotiating config with PF.
1153 if (adapter->num_req_queues)
1154 num_active_queues = adapter->num_req_queues;
1155 else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1157 num_active_queues = adapter->ch_config.total_qps;
1159 num_active_queues = min_t(int,
1160 adapter->vsi_res->num_queue_pairs,
1161 (int)(num_online_cpus()));
1164 adapter->tx_rings = kcalloc(num_active_queues,
1165 sizeof(struct i40e_ring), GFP_KERNEL);
1166 if (!adapter->tx_rings)
1168 adapter->rx_rings = kcalloc(num_active_queues,
1169 sizeof(struct i40e_ring), GFP_KERNEL);
1170 if (!adapter->rx_rings)
1173 for (i = 0; i < num_active_queues; i++) {
1174 struct i40e_ring *tx_ring;
1175 struct i40e_ring *rx_ring;
1177 tx_ring = &adapter->tx_rings[i];
1179 tx_ring->queue_index = i;
1180 tx_ring->netdev = adapter->netdev;
1181 tx_ring->dev = &adapter->pdev->dev;
1182 tx_ring->count = adapter->tx_desc_count;
1183 tx_ring->itr_setting = I40E_ITR_TX_DEF;
1184 if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1185 tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
1187 rx_ring = &adapter->rx_rings[i];
1188 rx_ring->queue_index = i;
1189 rx_ring->netdev = adapter->netdev;
1190 rx_ring->dev = &adapter->pdev->dev;
1191 rx_ring->count = adapter->rx_desc_count;
1192 rx_ring->itr_setting = I40E_ITR_RX_DEF;
1195 adapter->num_active_queues = num_active_queues;
1200 iavf_free_queues(adapter);
1205 * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1206 * @adapter: board private structure to initialize
1208 * Attempt to configure the interrupts using the best available
1209 * capabilities of the hardware and the kernel.
1211 static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1213 int vector, v_budget;
1217 if (!adapter->vsi_res) {
1221 pairs = adapter->num_active_queues;
1223 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1224 * us much good if we have more vectors than CPUs. However, we already
1225 * limit the total number of queues by the number of CPUs so we do not
1226 * need any further limiting here.
1228 v_budget = min_t(int, pairs + NONQ_VECS,
1229 (int)adapter->vf_res->max_vectors);
1231 adapter->msix_entries = kcalloc(v_budget,
1232 sizeof(struct msix_entry), GFP_KERNEL);
1233 if (!adapter->msix_entries) {
1238 for (vector = 0; vector < v_budget; vector++)
1239 adapter->msix_entries[vector].entry = vector;
1241 err = iavf_acquire_msix_vectors(adapter, v_budget);
1244 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1245 netif_set_real_num_tx_queues(adapter->netdev, pairs);
1250 * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
1251 * @adapter: board private structure
1253 * Return 0 on success, negative on failure
1255 static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1257 struct i40e_aqc_get_set_rss_key_data *rss_key =
1258 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
1259 struct i40e_hw *hw = &adapter->hw;
1262 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1263 /* bail because we already have a command pending */
1264 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1265 adapter->current_op);
1269 ret = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1271 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1272 iavf_stat_str(hw, ret),
1273 iavf_aq_str(hw, hw->aq.asq_last_status));
1278 ret = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1279 adapter->rss_lut, adapter->rss_lut_size);
1281 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1282 iavf_stat_str(hw, ret),
1283 iavf_aq_str(hw, hw->aq.asq_last_status));
1291 * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1292 * @adapter: board private structure
1294 * Returns 0 on success, negative on failure
1296 static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1298 struct i40e_hw *hw = &adapter->hw;
1302 dw = (u32 *)adapter->rss_key;
1303 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1304 wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
1306 dw = (u32 *)adapter->rss_lut;
1307 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1308 wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
1316 * iavf_config_rss - Configure RSS keys and lut
1317 * @adapter: board private structure
1319 * Returns 0 on success, negative on failure
1321 int iavf_config_rss(struct iavf_adapter *adapter)
1324 if (RSS_PF(adapter)) {
1325 adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1326 IAVF_FLAG_AQ_SET_RSS_KEY;
1328 } else if (RSS_AQ(adapter)) {
1329 return iavf_config_rss_aq(adapter);
1331 return iavf_config_rss_reg(adapter);
1336 * iavf_fill_rss_lut - Fill the lut with default values
1337 * @adapter: board private structure
1339 static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1343 for (i = 0; i < adapter->rss_lut_size; i++)
1344 adapter->rss_lut[i] = i % adapter->num_active_queues;
1348 * iavf_init_rss - Prepare for RSS
1349 * @adapter: board private structure
1351 * Return 0 on success, negative on failure
1353 static int iavf_init_rss(struct iavf_adapter *adapter)
1355 struct i40e_hw *hw = &adapter->hw;
1358 if (!RSS_PF(adapter)) {
1359 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1360 if (adapter->vf_res->vf_cap_flags &
1361 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1362 adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
1364 adapter->hena = I40E_DEFAULT_RSS_HENA;
1366 wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
1367 wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1370 iavf_fill_rss_lut(adapter);
1372 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1373 ret = iavf_config_rss(adapter);
1379 * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1380 * @adapter: board private structure to initialize
1382 * We allocate one q_vector per queue interrupt. If allocation fails we
1385 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1387 int q_idx = 0, num_q_vectors;
1388 struct i40e_q_vector *q_vector;
1390 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1391 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1393 if (!adapter->q_vectors)
1396 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1397 q_vector = &adapter->q_vectors[q_idx];
1398 q_vector->adapter = adapter;
1399 q_vector->vsi = &adapter->vsi;
1400 q_vector->v_idx = q_idx;
1401 q_vector->reg_idx = q_idx;
1402 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1403 netif_napi_add(adapter->netdev, &q_vector->napi,
1404 iavf_napi_poll, NAPI_POLL_WEIGHT);
1411 * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1412 * @adapter: board private structure to initialize
1414 * This function frees the memory allocated to the q_vectors. In addition if
1415 * NAPI is enabled it will delete any references to the NAPI struct prior
1416 * to freeing the q_vector.
1418 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1420 int q_idx, num_q_vectors;
1423 if (!adapter->q_vectors)
1426 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1427 napi_vectors = adapter->num_active_queues;
1429 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1430 struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
1432 if (q_idx < napi_vectors)
1433 netif_napi_del(&q_vector->napi);
1435 kfree(adapter->q_vectors);
1436 adapter->q_vectors = NULL;
1440 * iavf_reset_interrupt_capability - Reset MSIX setup
1441 * @adapter: board private structure
1444 void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1446 if (!adapter->msix_entries)
1449 pci_disable_msix(adapter->pdev);
1450 kfree(adapter->msix_entries);
1451 adapter->msix_entries = NULL;
1455 * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1456 * @adapter: board private structure to initialize
1459 int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1463 err = iavf_alloc_queues(adapter);
1465 dev_err(&adapter->pdev->dev,
1466 "Unable to allocate memory for queues\n");
1467 goto err_alloc_queues;
1471 err = iavf_set_interrupt_capability(adapter);
1474 dev_err(&adapter->pdev->dev,
1475 "Unable to setup interrupt capabilities\n");
1476 goto err_set_interrupt;
1479 err = iavf_alloc_q_vectors(adapter);
1481 dev_err(&adapter->pdev->dev,
1482 "Unable to allocate memory for queue vectors\n");
1483 goto err_alloc_q_vectors;
1486 /* If we've made it so far while ADq flag being ON, then we haven't
1487 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1488 * resources have been allocated in the reset path.
1489 * Now we can truly claim that ADq is enabled.
1491 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1493 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1496 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1497 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1498 adapter->num_active_queues);
1501 err_alloc_q_vectors:
1502 iavf_reset_interrupt_capability(adapter);
1504 iavf_free_queues(adapter);
1510 * iavf_free_rss - Free memory used by RSS structs
1511 * @adapter: board private structure
1513 static void iavf_free_rss(struct iavf_adapter *adapter)
1515 kfree(adapter->rss_key);
1516 adapter->rss_key = NULL;
1518 kfree(adapter->rss_lut);
1519 adapter->rss_lut = NULL;
1523 * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1524 * @adapter: board private structure
1526 * Returns 0 on success, negative on failure
1528 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter)
1530 struct net_device *netdev = adapter->netdev;
1533 if (netif_running(netdev))
1534 iavf_free_traffic_irqs(adapter);
1535 iavf_free_misc_irq(adapter);
1536 iavf_reset_interrupt_capability(adapter);
1537 iavf_free_q_vectors(adapter);
1538 iavf_free_queues(adapter);
1540 err = iavf_init_interrupt_scheme(adapter);
1544 netif_tx_stop_all_queues(netdev);
1546 err = iavf_request_misc_irq(adapter);
1550 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1552 iavf_map_rings_to_vectors(adapter);
1554 if (RSS_AQ(adapter))
1555 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
1557 err = iavf_init_rss(adapter);
1563 * iavf_watchdog_timer - Periodic call-back timer
1564 * @data: pointer to adapter disguised as unsigned long
1566 static void iavf_watchdog_timer(struct timer_list *t)
1568 struct iavf_adapter *adapter = from_timer(adapter, t,
1571 schedule_work(&adapter->watchdog_task);
1572 /* timer will be rescheduled in watchdog task */
1576 * iavf_watchdog_task - Periodic call-back task
1577 * @work: pointer to work_struct
1579 static void iavf_watchdog_task(struct work_struct *work)
1581 struct iavf_adapter *adapter = container_of(work,
1582 struct iavf_adapter,
1584 struct i40e_hw *hw = &adapter->hw;
1587 if (test_and_set_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section))
1588 goto restart_watchdog;
1590 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
1591 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1592 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1593 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1594 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
1595 /* A chance for redemption! */
1596 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1597 adapter->state = __IAVF_STARTUP;
1598 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1599 schedule_delayed_work(&adapter->init_task, 10);
1600 clear_bit(__IAVF_IN_CRITICAL_TASK,
1601 &adapter->crit_section);
1602 /* Don't reschedule the watchdog, since we've restarted
1603 * the init task. When init_task contacts the PF and
1604 * gets everything set up again, it'll restart the
1605 * watchdog for us. Down, boy. Sit. Stay. Woof.
1609 adapter->aq_required = 0;
1610 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1614 if ((adapter->state < __IAVF_DOWN) ||
1615 (adapter->flags & IAVF_FLAG_RESET_PENDING))
1618 /* check for reset */
1619 reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
1620 if (!(adapter->flags & IAVF_FLAG_RESET_PENDING) && !reg_val) {
1621 adapter->state = __IAVF_RESETTING;
1622 adapter->flags |= IAVF_FLAG_RESET_PENDING;
1623 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1624 schedule_work(&adapter->reset_task);
1625 adapter->aq_required = 0;
1626 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1630 /* Process admin queue tasks. After init, everything gets done
1631 * here so we don't race on the admin queue.
1633 if (adapter->current_op) {
1634 if (!iavf_asq_done(hw)) {
1635 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1636 iavf_send_api_ver(adapter);
1640 if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG) {
1641 iavf_send_vf_config_msg(adapter);
1645 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
1646 iavf_disable_queues(adapter);
1650 if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
1651 iavf_map_queues(adapter);
1655 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
1656 iavf_add_ether_addrs(adapter);
1660 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
1661 iavf_add_vlans(adapter);
1665 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
1666 iavf_del_ether_addrs(adapter);
1670 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
1671 iavf_del_vlans(adapter);
1675 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1676 iavf_enable_vlan_stripping(adapter);
1680 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1681 iavf_disable_vlan_stripping(adapter);
1685 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
1686 iavf_configure_queues(adapter);
1690 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
1691 iavf_enable_queues(adapter);
1695 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
1696 /* This message goes straight to the firmware, not the
1697 * PF, so we don't have to set current_op as we will
1698 * not get a response through the ARQ.
1700 iavf_init_rss(adapter);
1701 adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
1704 if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
1705 iavf_get_hena(adapter);
1708 if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
1709 iavf_set_hena(adapter);
1712 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
1713 iavf_set_rss_key(adapter);
1716 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
1717 iavf_set_rss_lut(adapter);
1721 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_PROMISC) {
1722 iavf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1723 FLAG_VF_MULTICAST_PROMISC);
1727 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_ALLMULTI) {
1728 iavf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1732 if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) &&
1733 (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1734 iavf_set_promiscuous(adapter, 0);
1738 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
1739 iavf_enable_channels(adapter);
1743 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
1744 iavf_disable_channels(adapter);
1748 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1749 iavf_add_cloud_filter(adapter);
1753 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1754 iavf_del_cloud_filter(adapter);
1758 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1760 if (adapter->state == __IAVF_RUNNING)
1761 iavf_request_stats(adapter);
1763 if (adapter->state == __IAVF_RUNNING)
1764 iavf_detect_recover_hung(&adapter->vsi);
1765 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1767 if (adapter->state == __IAVF_REMOVE)
1769 if (adapter->aq_required)
1770 mod_timer(&adapter->watchdog_timer,
1771 jiffies + msecs_to_jiffies(20));
1773 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1774 schedule_work(&adapter->adminq_task);
1777 static void iavf_disable_vf(struct iavf_adapter *adapter)
1779 struct iavf_mac_filter *f, *ftmp;
1780 struct iavf_vlan_filter *fv, *fvtmp;
1781 struct iavf_cloud_filter *cf, *cftmp;
1783 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
1785 /* We don't use netif_running() because it may be true prior to
1786 * ndo_open() returning, so we can't assume it means all our open
1787 * tasks have finished, since we're not holding the rtnl_lock here.
1789 if (adapter->state == __IAVF_RUNNING) {
1790 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
1791 netif_carrier_off(adapter->netdev);
1792 netif_tx_disable(adapter->netdev);
1793 adapter->link_up = false;
1794 iavf_napi_disable_all(adapter);
1795 iavf_irq_disable(adapter);
1796 iavf_free_traffic_irqs(adapter);
1797 iavf_free_all_tx_resources(adapter);
1798 iavf_free_all_rx_resources(adapter);
1801 spin_lock_bh(&adapter->mac_vlan_list_lock);
1803 /* Delete all of the filters */
1804 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1809 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1810 list_del(&fv->list);
1814 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1816 spin_lock_bh(&adapter->cloud_filter_list_lock);
1817 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1818 list_del(&cf->list);
1820 adapter->num_cloud_filters--;
1822 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1824 iavf_free_misc_irq(adapter);
1825 iavf_reset_interrupt_capability(adapter);
1826 iavf_free_queues(adapter);
1827 iavf_free_q_vectors(adapter);
1828 kfree(adapter->vf_res);
1829 iavf_shutdown_adminq(&adapter->hw);
1830 adapter->netdev->flags &= ~IFF_UP;
1831 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1832 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1833 adapter->state = __IAVF_DOWN;
1834 wake_up(&adapter->down_waitqueue);
1835 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1838 #define IAVF_RESET_WAIT_MS 10
1839 #define IAVF_RESET_WAIT_COUNT 500
1841 * iavf_reset_task - Call-back task to handle hardware reset
1842 * @work: pointer to work_struct
1844 * During reset we need to shut down and reinitialize the admin queue
1845 * before we can use it to communicate with the PF again. We also clear
1846 * and reinit the rings because that context is lost as well.
1848 static void iavf_reset_task(struct work_struct *work)
1850 struct iavf_adapter *adapter = container_of(work,
1851 struct iavf_adapter,
1853 struct virtchnl_vf_resource *vfres = adapter->vf_res;
1854 struct net_device *netdev = adapter->netdev;
1855 struct i40e_hw *hw = &adapter->hw;
1856 struct iavf_vlan_filter *vlf;
1857 struct iavf_cloud_filter *cf;
1858 struct iavf_mac_filter *f;
1863 /* When device is being removed it doesn't make sense to run the reset
1864 * task, just return in such a case.
1866 if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
1869 while (test_and_set_bit(__IAVF_IN_CLIENT_TASK,
1870 &adapter->crit_section))
1871 usleep_range(500, 1000);
1872 if (CLIENT_ENABLED(adapter)) {
1873 adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
1874 IAVF_FLAG_CLIENT_NEEDS_CLOSE |
1875 IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1876 IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
1877 cancel_delayed_work_sync(&adapter->client_task);
1878 iavf_notify_client_close(&adapter->vsi, true);
1880 iavf_misc_irq_disable(adapter);
1881 if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
1882 adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
1883 /* Restart the AQ here. If we have been reset but didn't
1884 * detect it, or if the PF had to reinit, our AQ will be hosed.
1886 iavf_shutdown_adminq(hw);
1887 iavf_init_adminq(hw);
1888 iavf_request_reset(adapter);
1890 adapter->flags |= IAVF_FLAG_RESET_PENDING;
1892 /* poll until we see the reset actually happen */
1893 for (i = 0; i < IAVF_RESET_WAIT_COUNT; i++) {
1894 reg_val = rd32(hw, I40E_VF_ARQLEN1) &
1895 I40E_VF_ARQLEN1_ARQENABLE_MASK;
1898 usleep_range(5000, 10000);
1900 if (i == IAVF_RESET_WAIT_COUNT) {
1901 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1902 goto continue_reset; /* act like the reset happened */
1905 /* wait until the reset is complete and the PF is responding to us */
1906 for (i = 0; i < IAVF_RESET_WAIT_COUNT; i++) {
1907 /* sleep first to make sure a minimum wait time is met */
1908 msleep(IAVF_RESET_WAIT_MS);
1910 reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
1911 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1912 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
1916 pci_set_master(adapter->pdev);
1918 if (i == IAVF_RESET_WAIT_COUNT) {
1919 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1921 iavf_disable_vf(adapter);
1922 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
1923 return; /* Do not attempt to reinit. It's dead, Jim. */
1927 /* We don't use netif_running() because it may be true prior to
1928 * ndo_open() returning, so we can't assume it means all our open
1929 * tasks have finished, since we're not holding the rtnl_lock here.
1931 running = ((adapter->state == __IAVF_RUNNING) ||
1932 (adapter->state == __IAVF_RESETTING));
1935 netif_carrier_off(netdev);
1936 netif_tx_stop_all_queues(netdev);
1937 adapter->link_up = false;
1938 iavf_napi_disable_all(adapter);
1940 iavf_irq_disable(adapter);
1942 adapter->state = __IAVF_RESETTING;
1943 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1945 /* free the Tx/Rx rings and descriptors, might be better to just
1946 * re-use them sometime in the future
1948 iavf_free_all_rx_resources(adapter);
1949 iavf_free_all_tx_resources(adapter);
1951 adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
1952 /* kill and reinit the admin queue */
1953 iavf_shutdown_adminq(hw);
1954 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1955 err = iavf_init_adminq(hw);
1957 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1959 adapter->aq_required = 0;
1961 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
1962 err = iavf_reinit_interrupt_scheme(adapter);
1967 adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
1968 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
1970 spin_lock_bh(&adapter->mac_vlan_list_lock);
1972 /* re-add all MAC filters */
1973 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1976 /* re-add all VLAN filters */
1977 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1981 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1983 /* check if TCs are running and re-add all cloud filters */
1984 spin_lock_bh(&adapter->cloud_filter_list_lock);
1985 if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1987 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1991 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1993 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
1994 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
1995 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
1996 iavf_misc_irq_enable(adapter);
1998 mod_timer(&adapter->watchdog_timer, jiffies + 2);
2000 /* We were running when the reset started, so we need to restore some
2004 /* allocate transmit descriptors */
2005 err = iavf_setup_all_tx_resources(adapter);
2009 /* allocate receive descriptors */
2010 err = iavf_setup_all_rx_resources(adapter);
2014 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
2015 err = iavf_request_traffic_irqs(adapter, netdev->name);
2019 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
2022 iavf_configure(adapter);
2024 iavf_up_complete(adapter);
2026 iavf_irq_enable(adapter, true);
2028 adapter->state = __IAVF_DOWN;
2029 wake_up(&adapter->down_waitqueue);
2031 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2032 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2036 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2037 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2038 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
2043 * iavf_adminq_task - worker thread to clean the admin queue
2044 * @work: pointer to work_struct containing our data
2046 static void iavf_adminq_task(struct work_struct *work)
2048 struct iavf_adapter *adapter =
2049 container_of(work, struct iavf_adapter, adminq_task);
2050 struct i40e_hw *hw = &adapter->hw;
2051 struct i40e_arq_event_info event;
2052 enum virtchnl_ops v_op;
2053 iavf_status ret, v_ret;
2057 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2060 event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
2061 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2066 ret = iavf_clean_arq_element(hw, &event, &pending);
2067 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2068 v_ret = (iavf_status)le32_to_cpu(event.desc.cookie_low);
2071 break; /* No event to process or error cleaning ARQ */
2073 iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2076 memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
2079 if ((adapter->flags &
2080 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED)) ||
2081 adapter->state == __IAVF_RESETTING)
2084 /* check for error indications */
2085 val = rd32(hw, hw->aq.arq.len);
2086 if (val == 0xdeadbeef) /* indicates device in reset */
2089 if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
2090 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2091 val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
2093 if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
2094 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2095 val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
2097 if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
2098 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2099 val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
2102 wr32(hw, hw->aq.arq.len, val);
2104 val = rd32(hw, hw->aq.asq.len);
2106 if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
2107 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2108 val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
2110 if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
2111 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2112 val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
2114 if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
2115 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2116 val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
2119 wr32(hw, hw->aq.asq.len, val);
2122 kfree(event.msg_buf);
2124 /* re-enable Admin queue interrupt cause */
2125 iavf_misc_irq_enable(adapter);
2129 * iavf_client_task - worker thread to perform client work
2130 * @work: pointer to work_struct containing our data
2132 * This task handles client interactions. Because client calls can be
2133 * reentrant, we can't handle them in the watchdog.
2135 static void iavf_client_task(struct work_struct *work)
2137 struct iavf_adapter *adapter =
2138 container_of(work, struct iavf_adapter, client_task.work);
2140 /* If we can't get the client bit, just give up. We'll be rescheduled
2144 if (test_and_set_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section))
2147 if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2148 iavf_client_subtask(adapter);
2149 adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
2152 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2153 iavf_notify_client_l2_params(&adapter->vsi);
2154 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2157 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_CLOSE) {
2158 iavf_notify_client_close(&adapter->vsi, false);
2159 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_CLOSE;
2162 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
2163 iavf_notify_client_open(&adapter->vsi);
2164 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
2167 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2171 * iavf_free_all_tx_resources - Free Tx Resources for All Queues
2172 * @adapter: board private structure
2174 * Free all transmit software resources
2176 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
2180 if (!adapter->tx_rings)
2183 for (i = 0; i < adapter->num_active_queues; i++)
2184 if (adapter->tx_rings[i].desc)
2185 iavf_free_tx_resources(&adapter->tx_rings[i]);
2189 * iavf_setup_all_tx_resources - allocate all queues Tx resources
2190 * @adapter: board private structure
2192 * If this function returns with an error, then it's possible one or
2193 * more of the rings is populated (while the rest are not). It is the
2194 * callers duty to clean those orphaned rings.
2196 * Return 0 on success, negative on failure
2198 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
2202 for (i = 0; i < adapter->num_active_queues; i++) {
2203 adapter->tx_rings[i].count = adapter->tx_desc_count;
2204 err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
2207 dev_err(&adapter->pdev->dev,
2208 "Allocation for Tx Queue %u failed\n", i);
2216 * iavf_setup_all_rx_resources - allocate all queues Rx resources
2217 * @adapter: board private structure
2219 * If this function returns with an error, then it's possible one or
2220 * more of the rings is populated (while the rest are not). It is the
2221 * callers duty to clean those orphaned rings.
2223 * Return 0 on success, negative on failure
2225 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
2229 for (i = 0; i < adapter->num_active_queues; i++) {
2230 adapter->rx_rings[i].count = adapter->rx_desc_count;
2231 err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
2234 dev_err(&adapter->pdev->dev,
2235 "Allocation for Rx Queue %u failed\n", i);
2242 * iavf_free_all_rx_resources - Free Rx Resources for All Queues
2243 * @adapter: board private structure
2245 * Free all receive software resources
2247 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
2251 if (!adapter->rx_rings)
2254 for (i = 0; i < adapter->num_active_queues; i++)
2255 if (adapter->rx_rings[i].desc)
2256 iavf_free_rx_resources(&adapter->rx_rings[i]);
2260 * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
2261 * @adapter: board private structure
2262 * @max_tx_rate: max Tx bw for a tc
2264 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
2267 int speed = 0, ret = 0;
2269 switch (adapter->link_speed) {
2270 case I40E_LINK_SPEED_40GB:
2273 case I40E_LINK_SPEED_25GB:
2276 case I40E_LINK_SPEED_20GB:
2279 case I40E_LINK_SPEED_10GB:
2282 case I40E_LINK_SPEED_1GB:
2285 case I40E_LINK_SPEED_100MB:
2292 if (max_tx_rate > speed) {
2293 dev_err(&adapter->pdev->dev,
2294 "Invalid tx rate specified\n");
2302 * iavf_validate_channel_config - validate queue mapping info
2303 * @adapter: board private structure
2304 * @mqprio_qopt: queue parameters
2306 * This function validates if the config provided by the user to
2307 * configure queue channels is valid or not. Returns 0 on a valid
2310 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
2311 struct tc_mqprio_qopt_offload *mqprio_qopt)
2313 u64 total_max_rate = 0;
2318 if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
2319 mqprio_qopt->qopt.num_tc < 1)
2322 for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2323 if (!mqprio_qopt->qopt.count[i] ||
2324 mqprio_qopt->qopt.offset[i] != num_qps)
2326 if (mqprio_qopt->min_rate[i]) {
2327 dev_err(&adapter->pdev->dev,
2328 "Invalid min tx rate (greater than 0) specified\n");
2331 /*convert to Mbps */
2332 tx_rate = div_u64(mqprio_qopt->max_rate[i],
2334 total_max_rate += tx_rate;
2335 num_qps += mqprio_qopt->qopt.count[i];
2337 if (num_qps > IAVF_MAX_REQ_QUEUES)
2340 ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
2345 * iavf_del_all_cloud_filters - delete all cloud filters
2346 * on the traffic classes
2348 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
2350 struct iavf_cloud_filter *cf, *cftmp;
2352 spin_lock_bh(&adapter->cloud_filter_list_lock);
2353 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2355 list_del(&cf->list);
2357 adapter->num_cloud_filters--;
2359 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2363 * __iavf_setup_tc - configure multiple traffic classes
2364 * @netdev: network interface device structure
2365 * @type_date: tc offload data
2367 * This function processes the config information provided by the
2368 * user to configure traffic classes/queue channels and packages the
2369 * information to request the PF to setup traffic classes.
2371 * Returns 0 on success.
2373 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
2375 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2376 struct iavf_adapter *adapter = netdev_priv(netdev);
2377 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2378 u8 num_tc = 0, total_qps = 0;
2379 int ret = 0, netdev_tc = 0;
2384 num_tc = mqprio_qopt->qopt.num_tc;
2385 mode = mqprio_qopt->mode;
2387 /* delete queue_channel */
2388 if (!mqprio_qopt->qopt.hw) {
2389 if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
2390 /* reset the tc configuration */
2391 netdev_reset_tc(netdev);
2392 adapter->num_tc = 0;
2393 netif_tx_stop_all_queues(netdev);
2394 netif_tx_disable(netdev);
2395 iavf_del_all_cloud_filters(adapter);
2396 adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
2403 /* add queue channel */
2404 if (mode == TC_MQPRIO_MODE_CHANNEL) {
2405 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2406 dev_err(&adapter->pdev->dev, "ADq not supported\n");
2409 if (adapter->ch_config.state != __IAVF_TC_INVALID) {
2410 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2414 ret = iavf_validate_ch_config(adapter, mqprio_qopt);
2417 /* Return if same TC config is requested */
2418 if (adapter->num_tc == num_tc)
2420 adapter->num_tc = num_tc;
2422 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2424 adapter->ch_config.ch_info[i].count =
2425 mqprio_qopt->qopt.count[i];
2426 adapter->ch_config.ch_info[i].offset =
2427 mqprio_qopt->qopt.offset[i];
2428 total_qps += mqprio_qopt->qopt.count[i];
2429 max_tx_rate = mqprio_qopt->max_rate[i];
2430 /* convert to Mbps */
2431 max_tx_rate = div_u64(max_tx_rate,
2433 adapter->ch_config.ch_info[i].max_tx_rate =
2436 adapter->ch_config.ch_info[i].count = 1;
2437 adapter->ch_config.ch_info[i].offset = 0;
2440 adapter->ch_config.total_qps = total_qps;
2441 netif_tx_stop_all_queues(netdev);
2442 netif_tx_disable(netdev);
2443 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
2444 netdev_reset_tc(netdev);
2445 /* Report the tc mapping up the stack */
2446 netdev_set_num_tc(adapter->netdev, num_tc);
2447 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2448 u16 qcount = mqprio_qopt->qopt.count[i];
2449 u16 qoffset = mqprio_qopt->qopt.offset[i];
2452 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2461 * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
2462 * @adapter: board private structure
2463 * @cls_flower: pointer to struct tc_cls_flower_offload
2464 * @filter: pointer to cloud filter structure
2466 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
2467 struct tc_cls_flower_offload *f,
2468 struct iavf_cloud_filter *filter)
2470 u16 n_proto_mask = 0;
2471 u16 n_proto_key = 0;
2476 struct virtchnl_filter *vf = &filter->f;
2478 if (f->dissector->used_keys &
2479 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2480 BIT(FLOW_DISSECTOR_KEY_BASIC) |
2481 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2482 BIT(FLOW_DISSECTOR_KEY_VLAN) |
2483 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2484 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2485 BIT(FLOW_DISSECTOR_KEY_PORTS) |
2486 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2487 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2488 f->dissector->used_keys);
2492 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2493 struct flow_dissector_key_keyid *mask =
2494 skb_flow_dissector_target(f->dissector,
2495 FLOW_DISSECTOR_KEY_ENC_KEYID,
2498 if (mask->keyid != 0)
2499 field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
2502 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
2503 struct flow_dissector_key_basic *key =
2504 skb_flow_dissector_target(f->dissector,
2505 FLOW_DISSECTOR_KEY_BASIC,
2508 struct flow_dissector_key_basic *mask =
2509 skb_flow_dissector_target(f->dissector,
2510 FLOW_DISSECTOR_KEY_BASIC,
2512 n_proto_key = ntohs(key->n_proto);
2513 n_proto_mask = ntohs(mask->n_proto);
2515 if (n_proto_key == ETH_P_ALL) {
2519 n_proto = n_proto_key & n_proto_mask;
2520 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2522 if (n_proto == ETH_P_IPV6) {
2523 /* specify flow type as TCP IPv6 */
2524 vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2527 if (key->ip_proto != IPPROTO_TCP) {
2528 dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2533 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2534 struct flow_dissector_key_eth_addrs *key =
2535 skb_flow_dissector_target(f->dissector,
2536 FLOW_DISSECTOR_KEY_ETH_ADDRS,
2539 struct flow_dissector_key_eth_addrs *mask =
2540 skb_flow_dissector_target(f->dissector,
2541 FLOW_DISSECTOR_KEY_ETH_ADDRS,
2543 /* use is_broadcast and is_zero to check for all 0xf or 0 */
2544 if (!is_zero_ether_addr(mask->dst)) {
2545 if (is_broadcast_ether_addr(mask->dst)) {
2546 field_flags |= IAVF_CLOUD_FIELD_OMAC;
2548 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2550 return I40E_ERR_CONFIG;
2554 if (!is_zero_ether_addr(mask->src)) {
2555 if (is_broadcast_ether_addr(mask->src)) {
2556 field_flags |= IAVF_CLOUD_FIELD_IMAC;
2558 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2560 return I40E_ERR_CONFIG;
2564 if (!is_zero_ether_addr(key->dst))
2565 if (is_valid_ether_addr(key->dst) ||
2566 is_multicast_ether_addr(key->dst)) {
2567 /* set the mask if a valid dst_mac address */
2568 for (i = 0; i < ETH_ALEN; i++)
2569 vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2570 ether_addr_copy(vf->data.tcp_spec.dst_mac,
2574 if (!is_zero_ether_addr(key->src))
2575 if (is_valid_ether_addr(key->src) ||
2576 is_multicast_ether_addr(key->src)) {
2577 /* set the mask if a valid dst_mac address */
2578 for (i = 0; i < ETH_ALEN; i++)
2579 vf->mask.tcp_spec.src_mac[i] |= 0xff;
2580 ether_addr_copy(vf->data.tcp_spec.src_mac,
2585 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
2586 struct flow_dissector_key_vlan *key =
2587 skb_flow_dissector_target(f->dissector,
2588 FLOW_DISSECTOR_KEY_VLAN,
2590 struct flow_dissector_key_vlan *mask =
2591 skb_flow_dissector_target(f->dissector,
2592 FLOW_DISSECTOR_KEY_VLAN,
2595 if (mask->vlan_id) {
2596 if (mask->vlan_id == VLAN_VID_MASK) {
2597 field_flags |= IAVF_CLOUD_FIELD_IVLAN;
2599 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2601 return I40E_ERR_CONFIG;
2604 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2605 vf->data.tcp_spec.vlan_id = cpu_to_be16(key->vlan_id);
2608 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
2609 struct flow_dissector_key_control *key =
2610 skb_flow_dissector_target(f->dissector,
2611 FLOW_DISSECTOR_KEY_CONTROL,
2614 addr_type = key->addr_type;
2617 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2618 struct flow_dissector_key_ipv4_addrs *key =
2619 skb_flow_dissector_target(f->dissector,
2620 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2622 struct flow_dissector_key_ipv4_addrs *mask =
2623 skb_flow_dissector_target(f->dissector,
2624 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2628 if (mask->dst == cpu_to_be32(0xffffffff)) {
2629 field_flags |= IAVF_CLOUD_FIELD_IIP;
2631 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2632 be32_to_cpu(mask->dst));
2633 return I40E_ERR_CONFIG;
2638 if (mask->src == cpu_to_be32(0xffffffff)) {
2639 field_flags |= IAVF_CLOUD_FIELD_IIP;
2641 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2642 be32_to_cpu(mask->dst));
2643 return I40E_ERR_CONFIG;
2647 if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
2648 dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2649 return I40E_ERR_CONFIG;
2652 vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2653 vf->data.tcp_spec.dst_ip[0] = key->dst;
2656 vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2657 vf->data.tcp_spec.src_ip[0] = key->src;
2661 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2662 struct flow_dissector_key_ipv6_addrs *key =
2663 skb_flow_dissector_target(f->dissector,
2664 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2666 struct flow_dissector_key_ipv6_addrs *mask =
2667 skb_flow_dissector_target(f->dissector,
2668 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2671 /* validate mask, make sure it is not IPV6_ADDR_ANY */
2672 if (ipv6_addr_any(&mask->dst)) {
2673 dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2675 return I40E_ERR_CONFIG;
2678 /* src and dest IPv6 address should not be LOOPBACK
2679 * (0:0:0:0:0:0:0:1) which can be represented as ::1
2681 if (ipv6_addr_loopback(&key->dst) ||
2682 ipv6_addr_loopback(&key->src)) {
2683 dev_err(&adapter->pdev->dev,
2684 "ipv6 addr should not be loopback\n");
2685 return I40E_ERR_CONFIG;
2687 if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
2688 field_flags |= IAVF_CLOUD_FIELD_IIP;
2690 for (i = 0; i < 4; i++)
2691 vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2692 memcpy(&vf->data.tcp_spec.dst_ip, &key->dst.s6_addr32,
2693 sizeof(vf->data.tcp_spec.dst_ip));
2694 for (i = 0; i < 4; i++)
2695 vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2696 memcpy(&vf->data.tcp_spec.src_ip, &key->src.s6_addr32,
2697 sizeof(vf->data.tcp_spec.src_ip));
2699 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
2700 struct flow_dissector_key_ports *key =
2701 skb_flow_dissector_target(f->dissector,
2702 FLOW_DISSECTOR_KEY_PORTS,
2704 struct flow_dissector_key_ports *mask =
2705 skb_flow_dissector_target(f->dissector,
2706 FLOW_DISSECTOR_KEY_PORTS,
2710 if (mask->src == cpu_to_be16(0xffff)) {
2711 field_flags |= IAVF_CLOUD_FIELD_IIP;
2713 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
2714 be16_to_cpu(mask->src));
2715 return I40E_ERR_CONFIG;
2720 if (mask->dst == cpu_to_be16(0xffff)) {
2721 field_flags |= IAVF_CLOUD_FIELD_IIP;
2723 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
2724 be16_to_cpu(mask->dst));
2725 return I40E_ERR_CONFIG;
2729 vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
2730 vf->data.tcp_spec.dst_port = key->dst;
2734 vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
2735 vf->data.tcp_spec.src_port = key->src;
2738 vf->field_flags = field_flags;
2744 * iavf_handle_tclass - Forward to a traffic class on the device
2745 * @adapter: board private structure
2746 * @tc: traffic class index on the device
2747 * @filter: pointer to cloud filter structure
2749 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
2750 struct iavf_cloud_filter *filter)
2754 if (tc < adapter->num_tc) {
2755 if (!filter->f.data.tcp_spec.dst_port) {
2756 dev_err(&adapter->pdev->dev,
2757 "Specify destination port to redirect to traffic class other than TC0\n");
2761 /* redirect to a traffic class on the same device */
2762 filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
2763 filter->f.action_meta = tc;
2768 * iavf_configure_clsflower - Add tc flower filters
2769 * @adapter: board private structure
2770 * @cls_flower: Pointer to struct tc_cls_flower_offload
2772 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
2773 struct tc_cls_flower_offload *cls_flower)
2775 int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
2776 struct iavf_cloud_filter *filter = NULL;
2777 int err = -EINVAL, count = 50;
2780 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
2784 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2788 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
2789 &adapter->crit_section)) {
2795 filter->cookie = cls_flower->cookie;
2797 /* set the mask to all zeroes to begin with */
2798 memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
2799 /* start out with flow type and eth type IPv4 to begin with */
2800 filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
2801 err = iavf_parse_cls_flower(adapter, cls_flower, filter);
2805 err = iavf_handle_tclass(adapter, tc, filter);
2809 /* add filter to the list */
2810 spin_lock_bh(&adapter->cloud_filter_list_lock);
2811 list_add_tail(&filter->list, &adapter->cloud_filter_list);
2812 adapter->num_cloud_filters++;
2814 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
2815 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2820 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2824 /* iavf_find_cf - Find the cloud filter in the list
2825 * @adapter: Board private structure
2826 * @cookie: filter specific cookie
2828 * Returns ptr to the filter object or NULL. Must be called while holding the
2829 * cloud_filter_list_lock.
2831 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
2832 unsigned long *cookie)
2834 struct iavf_cloud_filter *filter = NULL;
2839 list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
2840 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
2847 * iavf_delete_clsflower - Remove tc flower filters
2848 * @adapter: board private structure
2849 * @cls_flower: Pointer to struct tc_cls_flower_offload
2851 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
2852 struct tc_cls_flower_offload *cls_flower)
2854 struct iavf_cloud_filter *filter = NULL;
2857 spin_lock_bh(&adapter->cloud_filter_list_lock);
2858 filter = iavf_find_cf(adapter, &cls_flower->cookie);
2861 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
2865 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2871 * iavf_setup_tc_cls_flower - flower classifier offloads
2872 * @netdev: net device to configure
2873 * @type_data: offload data
2875 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
2876 struct tc_cls_flower_offload *cls_flower)
2878 if (cls_flower->common.chain_index)
2881 switch (cls_flower->command) {
2882 case TC_CLSFLOWER_REPLACE:
2883 return iavf_configure_clsflower(adapter, cls_flower);
2884 case TC_CLSFLOWER_DESTROY:
2885 return iavf_delete_clsflower(adapter, cls_flower);
2886 case TC_CLSFLOWER_STATS:
2894 * iavf_setup_tc_block_cb - block callback for tc
2895 * @type: type of offload
2896 * @type_data: offload data
2899 * This function is the block callback for traffic classes
2901 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2905 case TC_SETUP_CLSFLOWER:
2906 return iavf_setup_tc_cls_flower(cb_priv, type_data);
2913 * iavf_setup_tc_block - register callbacks for tc
2914 * @netdev: network interface device structure
2915 * @f: tc offload data
2917 * This function registers block callbacks for tc
2920 static int iavf_setup_tc_block(struct net_device *dev,
2921 struct tc_block_offload *f)
2923 struct iavf_adapter *adapter = netdev_priv(dev);
2925 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2928 switch (f->command) {
2930 return tcf_block_cb_register(f->block, iavf_setup_tc_block_cb,
2931 adapter, adapter, f->extack);
2932 case TC_BLOCK_UNBIND:
2933 tcf_block_cb_unregister(f->block, iavf_setup_tc_block_cb,
2942 * iavf_setup_tc - configure multiple traffic classes
2943 * @netdev: network interface device structure
2944 * @type: type of offload
2945 * @type_date: tc offload data
2947 * This function is the callback to ndo_setup_tc in the
2950 * Returns 0 on success
2952 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
2956 case TC_SETUP_QDISC_MQPRIO:
2957 return __iavf_setup_tc(netdev, type_data);
2958 case TC_SETUP_BLOCK:
2959 return iavf_setup_tc_block(netdev, type_data);
2966 * iavf_open - Called when a network interface is made active
2967 * @netdev: network interface device structure
2969 * Returns 0 on success, negative value on failure
2971 * The open entry point is called when a network interface is made
2972 * active by the system (IFF_UP). At this point all resources needed
2973 * for transmit and receive operations are allocated, the interrupt
2974 * handler is registered with the OS, the watchdog timer is started,
2975 * and the stack is notified that the interface is ready.
2977 static int iavf_open(struct net_device *netdev)
2979 struct iavf_adapter *adapter = netdev_priv(netdev);
2982 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
2983 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2987 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
2988 &adapter->crit_section))
2989 usleep_range(500, 1000);
2991 if (adapter->state != __IAVF_DOWN) {
2996 /* allocate transmit descriptors */
2997 err = iavf_setup_all_tx_resources(adapter);
3001 /* allocate receive descriptors */
3002 err = iavf_setup_all_rx_resources(adapter);
3006 /* clear any pending interrupts, may auto mask */
3007 err = iavf_request_traffic_irqs(adapter, netdev->name);
3011 spin_lock_bh(&adapter->mac_vlan_list_lock);
3013 iavf_add_filter(adapter, adapter->hw.mac.addr);
3015 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3017 iavf_configure(adapter);
3019 iavf_up_complete(adapter);
3021 iavf_irq_enable(adapter, true);
3023 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3029 iavf_free_traffic_irqs(adapter);
3031 iavf_free_all_rx_resources(adapter);
3033 iavf_free_all_tx_resources(adapter);
3035 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3041 * iavf_close - Disables a network interface
3042 * @netdev: network interface device structure
3044 * Returns 0, this is not allowed to fail
3046 * The close entry point is called when an interface is de-activated
3047 * by the OS. The hardware is still under the drivers control, but
3048 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3049 * are freed, along with all transmit and receive resources.
3051 static int iavf_close(struct net_device *netdev)
3053 struct iavf_adapter *adapter = netdev_priv(netdev);
3056 if (adapter->state <= __IAVF_DOWN_PENDING)
3059 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3060 &adapter->crit_section))
3061 usleep_range(500, 1000);
3063 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
3064 if (CLIENT_ENABLED(adapter))
3065 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3068 adapter->state = __IAVF_DOWN_PENDING;
3069 iavf_free_traffic_irqs(adapter);
3071 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3073 /* We explicitly don't free resources here because the hardware is
3074 * still active and can DMA into memory. Resources are cleared in
3075 * iavf_virtchnl_completion() after we get confirmation from the PF
3076 * driver that the rings have been stopped.
3078 * Also, we wait for state to transition to __IAVF_DOWN before
3079 * returning. State change occurs in iavf_virtchnl_completion() after
3080 * VF resources are released (which occurs after PF driver processes and
3081 * responds to admin queue commands).
3084 status = wait_event_timeout(adapter->down_waitqueue,
3085 adapter->state == __IAVF_DOWN,
3086 msecs_to_jiffies(200));
3088 netdev_warn(netdev, "Device resources not yet released\n");
3093 * iavf_change_mtu - Change the Maximum Transfer Unit
3094 * @netdev: network interface device structure
3095 * @new_mtu: new value for maximum frame size
3097 * Returns 0 on success, negative on failure
3099 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
3101 struct iavf_adapter *adapter = netdev_priv(netdev);
3103 netdev->mtu = new_mtu;
3104 if (CLIENT_ENABLED(adapter)) {
3105 iavf_notify_client_l2_params(&adapter->vsi);
3106 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3108 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
3109 schedule_work(&adapter->reset_task);
3115 * i40e_set_features - set the netdev feature flags
3116 * @netdev: ptr to the netdev being adjusted
3117 * @features: the feature set that the stack is suggesting
3118 * Note: expects to be called while under rtnl_lock()
3120 static int iavf_set_features(struct net_device *netdev,
3121 netdev_features_t features)
3123 struct iavf_adapter *adapter = netdev_priv(netdev);
3125 /* Don't allow changing VLAN_RX flag when adapter is not capable
3128 if (!VLAN_ALLOWED(adapter)) {
3129 if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX)
3131 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3132 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3133 adapter->aq_required |=
3134 IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3136 adapter->aq_required |=
3137 IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3144 * iavf_features_check - Validate encapsulated packet conforms to limits
3146 * @dev: This physical port's netdev
3147 * @features: Offload features that the stack believes apply
3149 static netdev_features_t iavf_features_check(struct sk_buff *skb,
3150 struct net_device *dev,
3151 netdev_features_t features)
3155 /* No point in doing any of this if neither checksum nor GSO are
3156 * being requested for this frame. We can rule out both by just
3157 * checking for CHECKSUM_PARTIAL
3159 if (skb->ip_summed != CHECKSUM_PARTIAL)
3162 /* We cannot support GSO if the MSS is going to be less than
3163 * 64 bytes. If it is then we need to drop support for GSO.
3165 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3166 features &= ~NETIF_F_GSO_MASK;
3168 /* MACLEN can support at most 63 words */
3169 len = skb_network_header(skb) - skb->data;
3170 if (len & ~(63 * 2))
3173 /* IPLEN and EIPLEN can support at most 127 dwords */
3174 len = skb_transport_header(skb) - skb_network_header(skb);
3175 if (len & ~(127 * 4))
3178 if (skb->encapsulation) {
3179 /* L4TUNLEN can support 127 words */
3180 len = skb_inner_network_header(skb) - skb_transport_header(skb);
3181 if (len & ~(127 * 2))
3184 /* IPLEN can support at most 127 dwords */
3185 len = skb_inner_transport_header(skb) -
3186 skb_inner_network_header(skb);
3187 if (len & ~(127 * 4))
3191 /* No need to validate L4LEN as TCP is the only protocol with a
3192 * a flexible value and we support all possible values supported
3193 * by TCP, which is at most 15 dwords
3198 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3202 * iavf_fix_features - fix up the netdev feature bits
3203 * @netdev: our net device
3204 * @features: desired feature bits
3206 * Returns fixed-up features bits
3208 static netdev_features_t iavf_fix_features(struct net_device *netdev,
3209 netdev_features_t features)
3211 struct iavf_adapter *adapter = netdev_priv(netdev);
3213 if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3214 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3215 NETIF_F_HW_VLAN_CTAG_RX |
3216 NETIF_F_HW_VLAN_CTAG_FILTER);
3221 static const struct net_device_ops iavf_netdev_ops = {
3222 .ndo_open = iavf_open,
3223 .ndo_stop = iavf_close,
3224 .ndo_start_xmit = iavf_xmit_frame,
3225 .ndo_set_rx_mode = iavf_set_rx_mode,
3226 .ndo_validate_addr = eth_validate_addr,
3227 .ndo_set_mac_address = iavf_set_mac,
3228 .ndo_change_mtu = iavf_change_mtu,
3229 .ndo_tx_timeout = iavf_tx_timeout,
3230 .ndo_vlan_rx_add_vid = iavf_vlan_rx_add_vid,
3231 .ndo_vlan_rx_kill_vid = iavf_vlan_rx_kill_vid,
3232 .ndo_features_check = iavf_features_check,
3233 .ndo_fix_features = iavf_fix_features,
3234 .ndo_set_features = iavf_set_features,
3235 #ifdef CONFIG_NET_POLL_CONTROLLER
3236 .ndo_poll_controller = iavf_netpoll,
3238 .ndo_setup_tc = iavf_setup_tc,
3242 * iavf_check_reset_complete - check that VF reset is complete
3243 * @hw: pointer to hw struct
3245 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3247 static int iavf_check_reset_complete(struct i40e_hw *hw)
3252 for (i = 0; i < 100; i++) {
3253 rstat = rd32(hw, I40E_VFGEN_RSTAT) &
3254 I40E_VFGEN_RSTAT_VFR_STATE_MASK;
3255 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3256 (rstat == VIRTCHNL_VFR_COMPLETED))
3258 usleep_range(10, 20);
3264 * iavf_process_config - Process the config information we got from the PF
3265 * @adapter: board private structure
3267 * Verify that we have a valid config struct, and set up our netdev features
3268 * and our VSI struct.
3270 int iavf_process_config(struct iavf_adapter *adapter)
3272 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3273 int i, num_req_queues = adapter->num_req_queues;
3274 struct net_device *netdev = adapter->netdev;
3275 struct i40e_vsi *vsi = &adapter->vsi;
3276 netdev_features_t hw_enc_features;
3277 netdev_features_t hw_features;
3279 /* got VF config message back from PF, now we can parse it */
3280 for (i = 0; i < vfres->num_vsis; i++) {
3281 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3282 adapter->vsi_res = &vfres->vsi_res[i];
3284 if (!adapter->vsi_res) {
3285 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3289 if (num_req_queues &&
3290 num_req_queues != adapter->vsi_res->num_queue_pairs) {
3291 /* Problem. The PF gave us fewer queues than what we had
3292 * negotiated in our request. Need a reset to see if we can't
3293 * get back to a working state.
3295 dev_err(&adapter->pdev->dev,
3296 "Requested %d queues, but PF only gave us %d.\n",
3298 adapter->vsi_res->num_queue_pairs);
3299 adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
3300 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3301 iavf_schedule_reset(adapter);
3304 adapter->num_req_queues = 0;
3306 hw_enc_features = NETIF_F_SG |
3310 NETIF_F_SOFT_FEATURES |
3319 /* advertise to stack only if offloads for encapsulated packets is
3322 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3323 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
3325 NETIF_F_GSO_GRE_CSUM |
3326 NETIF_F_GSO_IPXIP4 |
3327 NETIF_F_GSO_IPXIP6 |
3328 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3329 NETIF_F_GSO_PARTIAL |
3332 if (!(vfres->vf_cap_flags &
3333 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3334 netdev->gso_partial_features |=
3335 NETIF_F_GSO_UDP_TUNNEL_CSUM;
3337 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3338 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3339 netdev->hw_enc_features |= hw_enc_features;
3341 /* record features VLANs can make use of */
3342 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3344 /* Write features and hw_features separately to avoid polluting
3345 * with, or dropping, features that are set when we registered.
3347 hw_features = hw_enc_features;
3349 /* Enable VLAN features if supported */
3350 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3351 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3352 NETIF_F_HW_VLAN_CTAG_RX);
3353 /* Enable cloud filter if ADQ is supported */
3354 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3355 hw_features |= NETIF_F_HW_TC;
3357 netdev->hw_features |= hw_features;
3359 netdev->features |= hw_features;
3361 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3362 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3364 netdev->priv_flags |= IFF_UNICAST_FLT;
3366 /* Do not turn on offloads when they are requested to be turned off.
3367 * TSO needs minimum 576 bytes to work correctly.
3369 if (netdev->wanted_features) {
3370 if (!(netdev->wanted_features & NETIF_F_TSO) ||
3372 netdev->features &= ~NETIF_F_TSO;
3373 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
3375 netdev->features &= ~NETIF_F_TSO6;
3376 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
3377 netdev->features &= ~NETIF_F_TSO_ECN;
3378 if (!(netdev->wanted_features & NETIF_F_GRO))
3379 netdev->features &= ~NETIF_F_GRO;
3380 if (!(netdev->wanted_features & NETIF_F_GSO))
3381 netdev->features &= ~NETIF_F_GSO;
3384 adapter->vsi.id = adapter->vsi_res->vsi_id;
3386 adapter->vsi.back = adapter;
3387 adapter->vsi.base_vector = 1;
3388 adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
3389 vsi->netdev = adapter->netdev;
3390 vsi->qs_handle = adapter->vsi_res->qset_handle;
3391 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3392 adapter->rss_key_size = vfres->rss_key_size;
3393 adapter->rss_lut_size = vfres->rss_lut_size;
3395 adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
3396 adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
3403 * iavf_init_task - worker thread to perform delayed initialization
3404 * @work: pointer to work_struct containing our data
3406 * This task completes the work that was begun in probe. Due to the nature
3407 * of VF-PF communications, we may need to wait tens of milliseconds to get
3408 * responses back from the PF. Rather than busy-wait in probe and bog down the
3409 * whole system, we'll do it in a task so we can sleep.
3410 * This task only runs during driver init. Once we've established
3411 * communications with the PF driver and set up our netdev, the watchdog
3414 static void iavf_init_task(struct work_struct *work)
3416 struct iavf_adapter *adapter = container_of(work,
3417 struct iavf_adapter,
3419 struct net_device *netdev = adapter->netdev;
3420 struct i40e_hw *hw = &adapter->hw;
3421 struct pci_dev *pdev = adapter->pdev;
3424 switch (adapter->state) {
3425 case __IAVF_STARTUP:
3426 /* driver loaded, probe complete */
3427 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
3428 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3429 err = i40e_set_mac_type(hw);
3431 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
3435 err = iavf_check_reset_complete(hw);
3437 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
3441 hw->aq.num_arq_entries = IAVF_AQ_LEN;
3442 hw->aq.num_asq_entries = IAVF_AQ_LEN;
3443 hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
3444 hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
3446 err = iavf_init_adminq(hw);
3448 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
3452 err = iavf_send_api_ver(adapter);
3454 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
3455 iavf_shutdown_adminq(hw);
3458 adapter->state = __IAVF_INIT_VERSION_CHECK;
3460 case __IAVF_INIT_VERSION_CHECK:
3461 if (!iavf_asq_done(hw)) {
3462 dev_err(&pdev->dev, "Admin queue command never completed\n");
3463 iavf_shutdown_adminq(hw);
3464 adapter->state = __IAVF_STARTUP;
3468 /* aq msg sent, awaiting reply */
3469 err = iavf_verify_api_ver(adapter);
3471 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
3472 err = iavf_send_api_ver(adapter);
3474 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
3475 adapter->pf_version.major,
3476 adapter->pf_version.minor,
3477 VIRTCHNL_VERSION_MAJOR,
3478 VIRTCHNL_VERSION_MINOR);
3481 err = iavf_send_vf_config_msg(adapter);
3483 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
3487 adapter->state = __IAVF_INIT_GET_RESOURCES;
3489 case __IAVF_INIT_GET_RESOURCES:
3490 /* aq msg sent, awaiting reply */
3491 if (!adapter->vf_res) {
3492 bufsz = sizeof(struct virtchnl_vf_resource) +
3494 sizeof(struct virtchnl_vsi_resource));
3495 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
3496 if (!adapter->vf_res)
3499 err = iavf_get_vf_config(adapter);
3500 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
3501 err = iavf_send_vf_config_msg(adapter);
3503 } else if (err == I40E_ERR_PARAM) {
3504 /* We only get ERR_PARAM if the device is in a very bad
3505 * state or if we've been disabled for previous bad
3506 * behavior. Either way, we're done now.
3508 iavf_shutdown_adminq(hw);
3509 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
3513 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
3517 adapter->state = __IAVF_INIT_SW;
3523 if (iavf_process_config(adapter))
3525 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3527 adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
3529 netdev->netdev_ops = &iavf_netdev_ops;
3530 iavf_set_ethtool_ops(netdev);
3531 netdev->watchdog_timeo = 5 * HZ;
3533 /* MTU range: 68 - 9710 */
3534 netdev->min_mtu = ETH_MIN_MTU;
3535 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
3537 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
3538 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
3539 adapter->hw.mac.addr);
3540 eth_hw_addr_random(netdev);
3541 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
3543 adapter->flags |= IAVF_FLAG_ADDR_SET_BY_PF;
3544 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
3545 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
3548 timer_setup(&adapter->watchdog_timer, iavf_watchdog_timer, 0);
3549 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3551 adapter->tx_desc_count = IAVF_DEFAULT_TXD;
3552 adapter->rx_desc_count = IAVF_DEFAULT_RXD;
3553 err = iavf_init_interrupt_scheme(adapter);
3556 iavf_map_rings_to_vectors(adapter);
3557 if (adapter->vf_res->vf_cap_flags &
3558 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
3559 adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
3561 err = iavf_request_misc_irq(adapter);
3565 netif_carrier_off(netdev);
3566 adapter->link_up = false;
3568 if (!adapter->netdev_registered) {
3569 err = register_netdev(netdev);
3574 adapter->netdev_registered = true;
3576 netif_tx_stop_all_queues(netdev);
3577 if (CLIENT_ALLOWED(adapter)) {
3578 err = iavf_lan_add_device(adapter);
3580 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
3584 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
3585 if (netdev->features & NETIF_F_GRO)
3586 dev_info(&pdev->dev, "GRO is enabled\n");
3588 adapter->state = __IAVF_DOWN;
3589 set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
3590 iavf_misc_irq_enable(adapter);
3591 wake_up(&adapter->down_waitqueue);
3593 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
3594 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
3595 if (!adapter->rss_key || !adapter->rss_lut)
3598 if (RSS_AQ(adapter)) {
3599 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
3600 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
3602 iavf_init_rss(adapter);
3606 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
3609 iavf_free_rss(adapter);
3611 iavf_free_misc_irq(adapter);
3613 iavf_reset_interrupt_capability(adapter);
3615 kfree(adapter->vf_res);
3616 adapter->vf_res = NULL;
3618 /* Things went into the weeds, so try again later */
3619 if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
3620 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
3621 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
3622 iavf_shutdown_adminq(hw);
3623 adapter->state = __IAVF_STARTUP;
3624 schedule_delayed_work(&adapter->init_task, HZ * 5);
3627 schedule_delayed_work(&adapter->init_task, HZ);
3631 * iavf_shutdown - Shutdown the device in preparation for a reboot
3632 * @pdev: pci device structure
3634 static void iavf_shutdown(struct pci_dev *pdev)
3636 struct net_device *netdev = pci_get_drvdata(pdev);
3637 struct iavf_adapter *adapter = netdev_priv(netdev);
3639 netif_device_detach(netdev);
3641 if (netif_running(netdev))
3644 /* Prevent the watchdog from running. */
3645 adapter->state = __IAVF_REMOVE;
3646 adapter->aq_required = 0;
3649 pci_save_state(pdev);
3652 pci_disable_device(pdev);
3656 * iavf_probe - Device Initialization Routine
3657 * @pdev: PCI device information struct
3658 * @ent: entry in iavf_pci_tbl
3660 * Returns 0 on success, negative on failure
3662 * iavf_probe initializes an adapter identified by a pci_dev structure.
3663 * The OS initialization, configuring of the adapter private structure,
3664 * and a hardware reset occur.
3666 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3668 struct net_device *netdev;
3669 struct iavf_adapter *adapter = NULL;
3670 struct i40e_hw *hw = NULL;
3673 err = pci_enable_device(pdev);
3677 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3679 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3682 "DMA configuration failed: 0x%x\n", err);
3687 err = pci_request_regions(pdev, iavf_driver_name);
3690 "pci_request_regions failed 0x%x\n", err);
3694 pci_enable_pcie_error_reporting(pdev);
3696 pci_set_master(pdev);
3698 netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
3699 IAVF_MAX_REQ_QUEUES);
3702 goto err_alloc_etherdev;
3705 SET_NETDEV_DEV(netdev, &pdev->dev);
3707 pci_set_drvdata(pdev, netdev);
3708 adapter = netdev_priv(netdev);
3710 adapter->netdev = netdev;
3711 adapter->pdev = pdev;
3716 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3717 adapter->state = __IAVF_STARTUP;
3719 /* Call save state here because it relies on the adapter struct. */
3720 pci_save_state(pdev);
3722 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3723 pci_resource_len(pdev, 0));
3728 hw->vendor_id = pdev->vendor;
3729 hw->device_id = pdev->device;
3730 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3731 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3732 hw->subsystem_device_id = pdev->subsystem_device;
3733 hw->bus.device = PCI_SLOT(pdev->devfn);
3734 hw->bus.func = PCI_FUNC(pdev->devfn);
3735 hw->bus.bus_id = pdev->bus->number;
3737 /* set up the locks for the AQ, do this only once in probe
3738 * and destroy them only once in remove
3740 mutex_init(&hw->aq.asq_mutex);
3741 mutex_init(&hw->aq.arq_mutex);
3743 spin_lock_init(&adapter->mac_vlan_list_lock);
3744 spin_lock_init(&adapter->cloud_filter_list_lock);
3746 INIT_LIST_HEAD(&adapter->mac_filter_list);
3747 INIT_LIST_HEAD(&adapter->vlan_filter_list);
3748 INIT_LIST_HEAD(&adapter->cloud_filter_list);
3750 INIT_WORK(&adapter->reset_task, iavf_reset_task);
3751 INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
3752 INIT_WORK(&adapter->watchdog_task, iavf_watchdog_task);
3753 INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
3754 INIT_DELAYED_WORK(&adapter->init_task, iavf_init_task);
3755 schedule_delayed_work(&adapter->init_task,
3756 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3758 /* Setup the wait queue for indicating transition to down status */
3759 init_waitqueue_head(&adapter->down_waitqueue);
3764 free_netdev(netdev);
3766 pci_release_regions(pdev);
3769 pci_disable_device(pdev);
3775 * iavf_suspend - Power management suspend routine
3776 * @pdev: PCI device information struct
3779 * Called when the system (VM) is entering sleep/suspend.
3781 static int iavf_suspend(struct pci_dev *pdev, pm_message_t state)
3783 struct net_device *netdev = pci_get_drvdata(pdev);
3784 struct iavf_adapter *adapter = netdev_priv(netdev);
3787 netif_device_detach(netdev);
3789 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3790 &adapter->crit_section))
3791 usleep_range(500, 1000);
3793 if (netif_running(netdev)) {
3798 iavf_free_misc_irq(adapter);
3799 iavf_reset_interrupt_capability(adapter);
3801 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3803 retval = pci_save_state(pdev);
3807 pci_disable_device(pdev);
3813 * iavf_resume - Power management resume routine
3814 * @pdev: PCI device information struct
3816 * Called when the system (VM) is resumed from sleep/suspend.
3818 static int iavf_resume(struct pci_dev *pdev)
3820 struct iavf_adapter *adapter = pci_get_drvdata(pdev);
3821 struct net_device *netdev = adapter->netdev;
3824 pci_set_power_state(pdev, PCI_D0);
3825 pci_restore_state(pdev);
3826 /* pci_restore_state clears dev->state_saved so call
3827 * pci_save_state to restore it.
3829 pci_save_state(pdev);
3831 err = pci_enable_device_mem(pdev);
3833 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3836 pci_set_master(pdev);
3839 err = iavf_set_interrupt_capability(adapter);
3842 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3845 err = iavf_request_misc_irq(adapter);
3848 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3852 schedule_work(&adapter->reset_task);
3854 netif_device_attach(netdev);
3859 #endif /* CONFIG_PM */
3861 * iavf_remove - Device Removal Routine
3862 * @pdev: PCI device information struct
3864 * iavf_remove is called by the PCI subsystem to alert the driver
3865 * that it should release a PCI device. The could be caused by a
3866 * Hot-Plug event, or because the driver is going to be removed from
3869 static void iavf_remove(struct pci_dev *pdev)
3871 struct net_device *netdev = pci_get_drvdata(pdev);
3872 struct iavf_adapter *adapter = netdev_priv(netdev);
3873 struct iavf_vlan_filter *vlf, *vlftmp;
3874 struct iavf_mac_filter *f, *ftmp;
3875 struct iavf_cloud_filter *cf, *cftmp;
3876 struct i40e_hw *hw = &adapter->hw;
3878 /* Indicate we are in remove and not to run reset_task */
3879 set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section);
3880 cancel_delayed_work_sync(&adapter->init_task);
3881 cancel_work_sync(&adapter->reset_task);
3882 cancel_delayed_work_sync(&adapter->client_task);
3883 if (adapter->netdev_registered) {
3884 unregister_netdev(netdev);
3885 adapter->netdev_registered = false;
3887 if (CLIENT_ALLOWED(adapter)) {
3888 err = iavf_lan_del_device(adapter);
3890 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3894 /* Shut down all the garbage mashers on the detention level */
3895 adapter->state = __IAVF_REMOVE;
3896 adapter->aq_required = 0;
3897 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3898 iavf_request_reset(adapter);
3900 /* If the FW isn't responding, kick it once, but only once. */
3901 if (!iavf_asq_done(hw)) {
3902 iavf_request_reset(adapter);
3905 iavf_free_all_tx_resources(adapter);
3906 iavf_free_all_rx_resources(adapter);
3907 iavf_misc_irq_disable(adapter);
3908 iavf_free_misc_irq(adapter);
3909 iavf_reset_interrupt_capability(adapter);
3910 iavf_free_q_vectors(adapter);
3912 if (adapter->watchdog_timer.function)
3913 del_timer_sync(&adapter->watchdog_timer);
3915 cancel_work_sync(&adapter->adminq_task);
3917 iavf_free_rss(adapter);
3919 if (hw->aq.asq.count)
3920 iavf_shutdown_adminq(hw);
3922 /* destroy the locks only once, here */
3923 mutex_destroy(&hw->aq.arq_mutex);
3924 mutex_destroy(&hw->aq.asq_mutex);
3926 iounmap(hw->hw_addr);
3927 pci_release_regions(pdev);
3928 iavf_free_all_tx_resources(adapter);
3929 iavf_free_all_rx_resources(adapter);
3930 iavf_free_queues(adapter);
3931 kfree(adapter->vf_res);
3932 spin_lock_bh(&adapter->mac_vlan_list_lock);
3933 /* If we got removed before an up/down sequence, we've got a filter
3934 * hanging out there that we need to get rid of.
3936 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3940 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3942 list_del(&vlf->list);
3946 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3948 spin_lock_bh(&adapter->cloud_filter_list_lock);
3949 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3950 list_del(&cf->list);
3953 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3955 free_netdev(netdev);
3957 pci_disable_pcie_error_reporting(pdev);
3959 pci_disable_device(pdev);
3962 static struct pci_driver iavf_driver = {
3963 .name = iavf_driver_name,
3964 .id_table = iavf_pci_tbl,
3965 .probe = iavf_probe,
3966 .remove = iavf_remove,
3968 .suspend = iavf_suspend,
3969 .resume = iavf_resume,
3971 .shutdown = iavf_shutdown,
3975 * i40e_init_module - Driver Registration Routine
3977 * i40e_init_module is the first routine called when the driver is
3978 * loaded. All it does is register with the PCI subsystem.
3980 static int __init iavf_init_module(void)
3984 pr_info("iavf: %s - version %s\n", iavf_driver_string,
3985 iavf_driver_version);
3987 pr_info("%s\n", iavf_copyright);
3989 iavf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3992 pr_err("%s: Failed to create workqueue\n", iavf_driver_name);
3995 ret = pci_register_driver(&iavf_driver);
3999 module_init(iavf_init_module);
4002 * i40e_exit_module - Driver Exit Cleanup Routine
4004 * i40e_exit_module is called just before the driver is removed
4007 static void __exit iavf_exit_module(void)
4009 pci_unregister_driver(&iavf_driver);
4010 destroy_workqueue(iavf_wq);
4013 module_exit(iavf_exit_module);