1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
5 #include "iavf_prototype.h"
6 #include "iavf_client.h"
7 /* All iavf tracepoints are defined by the include below, which must
8 * be included exactly once across the whole kernel with
9 * CREATE_TRACE_POINTS defined
11 #define CREATE_TRACE_POINTS
12 #include "iavf_trace.h"
14 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter);
15 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter);
16 static int iavf_close(struct net_device *netdev);
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, IAVF_DEV_ID_VF), 0},
45 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF_HV), 0},
46 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_X722_VF), 0},
47 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_ADAPTIVE_VF), 0},
48 /* required last entry */
52 MODULE_DEVICE_TABLE(pci, iavf_pci_tbl);
54 MODULE_ALIAS("i40evf");
56 MODULE_DESCRIPTION("Intel(R) Ethernet Adaptive Virtual Function Network Driver");
57 MODULE_LICENSE("GPL v2");
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 iavf_hw *hw,
70 struct iavf_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 iavf_hw *hw, struct iavf_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 iavf_hw *hw,
110 struct iavf_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 iavf_hw *hw, struct iavf_virt_mem *mem)
132 return I40E_ERR_PARAM;
134 /* it's ok to kfree a NULL pointer */
141 * iavf_debug_d - OS dependent version of debug printing
142 * @hw: pointer to the HW structure
143 * @mask: debug level mask
144 * @fmt_str: printf-type format description
146 void iavf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
151 if (!(mask & ((struct iavf_hw *)hw)->debug_mask))
154 va_start(argptr, fmt_str);
155 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
158 /* the debug string is already formatted with a newline */
163 * iavf_schedule_reset - Set the flags and schedule a reset event
164 * @adapter: board private structure
166 void iavf_schedule_reset(struct iavf_adapter *adapter)
168 if (!(adapter->flags &
169 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
170 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
171 schedule_work(&adapter->reset_task);
176 * iavf_tx_timeout - Respond to a Tx Hang
177 * @netdev: network interface device structure
179 static void iavf_tx_timeout(struct net_device *netdev)
181 struct iavf_adapter *adapter = netdev_priv(netdev);
183 adapter->tx_timeout_count++;
184 iavf_schedule_reset(adapter);
188 * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
189 * @adapter: board private structure
191 static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
193 struct iavf_hw *hw = &adapter->hw;
195 if (!adapter->msix_entries)
198 wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
202 synchronize_irq(adapter->msix_entries[0].vector);
206 * iavf_misc_irq_enable - Enable default interrupt generation settings
207 * @adapter: board private structure
209 static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
211 struct iavf_hw *hw = &adapter->hw;
213 wr32(hw, IAVF_VFINT_DYN_CTL01, IAVF_VFINT_DYN_CTL01_INTENA_MASK |
214 IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
215 wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
221 * iavf_irq_disable - Mask off interrupt generation on the NIC
222 * @adapter: board private structure
224 static void iavf_irq_disable(struct iavf_adapter *adapter)
227 struct iavf_hw *hw = &adapter->hw;
229 if (!adapter->msix_entries)
232 for (i = 1; i < adapter->num_msix_vectors; i++) {
233 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1), 0);
234 synchronize_irq(adapter->msix_entries[i].vector);
240 * iavf_irq_enable_queues - Enable interrupt for specified queues
241 * @adapter: board private structure
242 * @mask: bitmap of queues to enable
244 void iavf_irq_enable_queues(struct iavf_adapter *adapter, u32 mask)
246 struct iavf_hw *hw = &adapter->hw;
249 for (i = 1; i < adapter->num_msix_vectors; i++) {
250 if (mask & BIT(i - 1)) {
251 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1),
252 IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
253 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
259 * iavf_irq_enable - Enable default interrupt generation settings
260 * @adapter: board private structure
261 * @flush: boolean value whether to run rd32()
263 void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
265 struct iavf_hw *hw = &adapter->hw;
267 iavf_misc_irq_enable(adapter);
268 iavf_irq_enable_queues(adapter, ~0);
275 * iavf_msix_aq - Interrupt handler for vector 0
276 * @irq: interrupt number
277 * @data: pointer to netdev
279 static irqreturn_t iavf_msix_aq(int irq, void *data)
281 struct net_device *netdev = data;
282 struct iavf_adapter *adapter = netdev_priv(netdev);
283 struct iavf_hw *hw = &adapter->hw;
285 /* handle non-queue interrupts, these reads clear the registers */
286 rd32(hw, IAVF_VFINT_ICR01);
287 rd32(hw, IAVF_VFINT_ICR0_ENA1);
289 /* schedule work on the private workqueue */
290 schedule_work(&adapter->adminq_task);
296 * iavf_msix_clean_rings - MSIX mode Interrupt Handler
297 * @irq: interrupt number
298 * @data: pointer to a q_vector
300 static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
302 struct iavf_q_vector *q_vector = data;
304 if (!q_vector->tx.ring && !q_vector->rx.ring)
307 napi_schedule_irqoff(&q_vector->napi);
313 * iavf_map_vector_to_rxq - associate irqs with rx queues
314 * @adapter: board private structure
315 * @v_idx: interrupt number
316 * @r_idx: queue number
319 iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
321 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
322 struct iavf_ring *rx_ring = &adapter->rx_rings[r_idx];
323 struct iavf_hw *hw = &adapter->hw;
325 rx_ring->q_vector = q_vector;
326 rx_ring->next = q_vector->rx.ring;
327 rx_ring->vsi = &adapter->vsi;
328 q_vector->rx.ring = rx_ring;
329 q_vector->rx.count++;
330 q_vector->rx.next_update = jiffies + 1;
331 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
332 q_vector->ring_mask |= BIT(r_idx);
333 wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, q_vector->reg_idx),
334 q_vector->rx.current_itr);
335 q_vector->rx.current_itr = q_vector->rx.target_itr;
339 * iavf_map_vector_to_txq - associate irqs with tx queues
340 * @adapter: board private structure
341 * @v_idx: interrupt number
342 * @t_idx: queue number
345 iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
347 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
348 struct iavf_ring *tx_ring = &adapter->tx_rings[t_idx];
349 struct iavf_hw *hw = &adapter->hw;
351 tx_ring->q_vector = q_vector;
352 tx_ring->next = q_vector->tx.ring;
353 tx_ring->vsi = &adapter->vsi;
354 q_vector->tx.ring = tx_ring;
355 q_vector->tx.count++;
356 q_vector->tx.next_update = jiffies + 1;
357 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
358 q_vector->num_ringpairs++;
359 wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, q_vector->reg_idx),
360 q_vector->tx.target_itr);
361 q_vector->tx.current_itr = q_vector->tx.target_itr;
365 * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
366 * @adapter: board private structure to initialize
368 * This function maps descriptor rings to the queue-specific vectors
369 * we were allotted through the MSI-X enabling code. Ideally, we'd have
370 * one vector per ring/queue, but on a constrained vector budget, we
371 * group the rings as "efficiently" as possible. You would add new
372 * mapping configurations in here.
374 static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
376 int rings_remaining = adapter->num_active_queues;
377 int ridx = 0, vidx = 0;
380 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
382 for (; ridx < rings_remaining; ridx++) {
383 iavf_map_vector_to_rxq(adapter, vidx, ridx);
384 iavf_map_vector_to_txq(adapter, vidx, ridx);
386 /* In the case where we have more queues than vectors, continue
387 * round-robin on vectors until all queues are mapped.
389 if (++vidx >= q_vectors)
393 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
397 * iavf_irq_affinity_notify - Callback for affinity changes
398 * @notify: context as to what irq was changed
399 * @mask: the new affinity mask
401 * This is a callback function used by the irq_set_affinity_notifier function
402 * so that we may register to receive changes to the irq affinity masks.
404 static void iavf_irq_affinity_notify(struct irq_affinity_notify *notify,
405 const cpumask_t *mask)
407 struct iavf_q_vector *q_vector =
408 container_of(notify, struct iavf_q_vector, affinity_notify);
410 cpumask_copy(&q_vector->affinity_mask, mask);
414 * iavf_irq_affinity_release - Callback for affinity notifier release
415 * @ref: internal core kernel usage
417 * This is a callback function used by the irq_set_affinity_notifier function
418 * to inform the current notification subscriber that they will no longer
419 * receive notifications.
421 static void iavf_irq_affinity_release(struct kref *ref) {}
424 * iavf_request_traffic_irqs - Initialize MSI-X interrupts
425 * @adapter: board private structure
426 * @basename: device basename
428 * Allocates MSI-X vectors for tx and rx handling, and requests
429 * interrupts from the kernel.
432 iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
434 unsigned int vector, q_vectors;
435 unsigned int rx_int_idx = 0, tx_int_idx = 0;
439 iavf_irq_disable(adapter);
440 /* Decrement for Other and TCP Timer vectors */
441 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
443 for (vector = 0; vector < q_vectors; vector++) {
444 struct iavf_q_vector *q_vector = &adapter->q_vectors[vector];
446 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
448 if (q_vector->tx.ring && q_vector->rx.ring) {
449 snprintf(q_vector->name, sizeof(q_vector->name),
450 "iavf-%s-TxRx-%d", basename, rx_int_idx++);
452 } else if (q_vector->rx.ring) {
453 snprintf(q_vector->name, sizeof(q_vector->name),
454 "iavf-%s-rx-%d", basename, rx_int_idx++);
455 } else if (q_vector->tx.ring) {
456 snprintf(q_vector->name, sizeof(q_vector->name),
457 "iavf-%s-tx-%d", basename, tx_int_idx++);
459 /* skip this unused q_vector */
462 err = request_irq(irq_num,
463 iavf_msix_clean_rings,
468 dev_info(&adapter->pdev->dev,
469 "Request_irq failed, error: %d\n", err);
470 goto free_queue_irqs;
472 /* register for affinity change notifications */
473 q_vector->affinity_notify.notify = iavf_irq_affinity_notify;
474 q_vector->affinity_notify.release =
475 iavf_irq_affinity_release;
476 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
477 /* Spread the IRQ affinity hints across online CPUs. Note that
478 * get_cpu_mask returns a mask with a permanent lifetime so
479 * it's safe to use as a hint for irq_set_affinity_hint.
481 cpu = cpumask_local_spread(q_vector->v_idx, -1);
482 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
490 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
491 irq_set_affinity_notifier(irq_num, NULL);
492 irq_set_affinity_hint(irq_num, NULL);
493 free_irq(irq_num, &adapter->q_vectors[vector]);
499 * iavf_request_misc_irq - Initialize MSI-X interrupts
500 * @adapter: board private structure
502 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
503 * vector is only for the admin queue, and stays active even when the netdev
506 static int iavf_request_misc_irq(struct iavf_adapter *adapter)
508 struct net_device *netdev = adapter->netdev;
511 snprintf(adapter->misc_vector_name,
512 sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
513 dev_name(&adapter->pdev->dev));
514 err = request_irq(adapter->msix_entries[0].vector,
516 adapter->misc_vector_name, netdev);
518 dev_err(&adapter->pdev->dev,
519 "request_irq for %s failed: %d\n",
520 adapter->misc_vector_name, err);
521 free_irq(adapter->msix_entries[0].vector, netdev);
527 * iavf_free_traffic_irqs - Free MSI-X interrupts
528 * @adapter: board private structure
530 * Frees all MSI-X vectors other than 0.
532 static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
534 int vector, irq_num, q_vectors;
536 if (!adapter->msix_entries)
539 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
541 for (vector = 0; vector < q_vectors; vector++) {
542 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
543 irq_set_affinity_notifier(irq_num, NULL);
544 irq_set_affinity_hint(irq_num, NULL);
545 free_irq(irq_num, &adapter->q_vectors[vector]);
550 * iavf_free_misc_irq - Free MSI-X miscellaneous vector
551 * @adapter: board private structure
553 * Frees MSI-X vector 0.
555 static void iavf_free_misc_irq(struct iavf_adapter *adapter)
557 struct net_device *netdev = adapter->netdev;
559 if (!adapter->msix_entries)
562 free_irq(adapter->msix_entries[0].vector, netdev);
566 * iavf_configure_tx - Configure Transmit Unit after Reset
567 * @adapter: board private structure
569 * Configure the Tx unit of the MAC after a reset.
571 static void iavf_configure_tx(struct iavf_adapter *adapter)
573 struct iavf_hw *hw = &adapter->hw;
576 for (i = 0; i < adapter->num_active_queues; i++)
577 adapter->tx_rings[i].tail = hw->hw_addr + IAVF_QTX_TAIL1(i);
581 * iavf_configure_rx - Configure Receive Unit after Reset
582 * @adapter: board private structure
584 * Configure the Rx unit of the MAC after a reset.
586 static void iavf_configure_rx(struct iavf_adapter *adapter)
588 unsigned int rx_buf_len = IAVF_RXBUFFER_2048;
589 struct iavf_hw *hw = &adapter->hw;
592 /* Legacy Rx will always default to a 2048 buffer size. */
593 #if (PAGE_SIZE < 8192)
594 if (!(adapter->flags & IAVF_FLAG_LEGACY_RX)) {
595 struct net_device *netdev = adapter->netdev;
597 /* For jumbo frames on systems with 4K pages we have to use
598 * an order 1 page, so we might as well increase the size
599 * of our Rx buffer to make better use of the available space
601 rx_buf_len = IAVF_RXBUFFER_3072;
603 /* We use a 1536 buffer size for configurations with
604 * standard Ethernet mtu. On x86 this gives us enough room
605 * for shared info and 192 bytes of padding.
607 if (!IAVF_2K_TOO_SMALL_WITH_PADDING &&
608 (netdev->mtu <= ETH_DATA_LEN))
609 rx_buf_len = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
613 for (i = 0; i < adapter->num_active_queues; i++) {
614 adapter->rx_rings[i].tail = hw->hw_addr + IAVF_QRX_TAIL1(i);
615 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
617 if (adapter->flags & IAVF_FLAG_LEGACY_RX)
618 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
620 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
625 * iavf_find_vlan - Search filter list for specific vlan filter
626 * @adapter: board private structure
629 * Returns ptr to the filter object or NULL. Must be called while holding the
630 * mac_vlan_list_lock.
633 iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter, u16 vlan)
635 struct iavf_vlan_filter *f;
637 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
645 * iavf_add_vlan - Add a vlan filter to the list
646 * @adapter: board private structure
649 * Returns ptr to the filter object or NULL when no memory available.
652 iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter, u16 vlan)
654 struct iavf_vlan_filter *f = NULL;
656 spin_lock_bh(&adapter->mac_vlan_list_lock);
658 f = iavf_find_vlan(adapter, vlan);
660 f = kzalloc(sizeof(*f), GFP_KERNEL);
666 INIT_LIST_HEAD(&f->list);
667 list_add(&f->list, &adapter->vlan_filter_list);
669 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
673 spin_unlock_bh(&adapter->mac_vlan_list_lock);
678 * iavf_del_vlan - Remove a vlan filter from the list
679 * @adapter: board private structure
682 static void iavf_del_vlan(struct iavf_adapter *adapter, u16 vlan)
684 struct iavf_vlan_filter *f;
686 spin_lock_bh(&adapter->mac_vlan_list_lock);
688 f = iavf_find_vlan(adapter, vlan);
691 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
694 spin_unlock_bh(&adapter->mac_vlan_list_lock);
698 * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
699 * @netdev: network device struct
700 * @proto: unused protocol data
703 static int iavf_vlan_rx_add_vid(struct net_device *netdev,
704 __always_unused __be16 proto, u16 vid)
706 struct iavf_adapter *adapter = netdev_priv(netdev);
708 if (!VLAN_ALLOWED(adapter))
710 if (iavf_add_vlan(adapter, vid) == NULL)
716 * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
717 * @netdev: network device struct
718 * @proto: unused protocol data
721 static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
722 __always_unused __be16 proto, u16 vid)
724 struct iavf_adapter *adapter = netdev_priv(netdev);
726 if (VLAN_ALLOWED(adapter)) {
727 iavf_del_vlan(adapter, vid);
734 * iavf_find_filter - Search filter list for specific mac filter
735 * @adapter: board private structure
736 * @macaddr: the MAC address
738 * Returns ptr to the filter object or NULL. Must be called while holding the
739 * mac_vlan_list_lock.
742 iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
745 struct iavf_mac_filter *f;
750 list_for_each_entry(f, &adapter->mac_filter_list, list) {
751 if (ether_addr_equal(macaddr, f->macaddr))
758 * iavf_add_filter - Add a mac filter to the filter list
759 * @adapter: board private structure
760 * @macaddr: the MAC address
762 * Returns ptr to the filter object or NULL when no memory available.
765 iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
768 struct iavf_mac_filter *f;
773 f = iavf_find_filter(adapter, macaddr);
775 f = kzalloc(sizeof(*f), GFP_ATOMIC);
779 ether_addr_copy(f->macaddr, macaddr);
781 list_add_tail(&f->list, &adapter->mac_filter_list);
783 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
792 * iavf_set_mac - NDO callback to set port mac address
793 * @netdev: network interface device structure
794 * @p: pointer to an address structure
796 * Returns 0 on success, negative on failure
798 static int iavf_set_mac(struct net_device *netdev, void *p)
800 struct iavf_adapter *adapter = netdev_priv(netdev);
801 struct iavf_hw *hw = &adapter->hw;
802 struct iavf_mac_filter *f;
803 struct sockaddr *addr = p;
805 if (!is_valid_ether_addr(addr->sa_data))
806 return -EADDRNOTAVAIL;
808 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
811 if (adapter->flags & IAVF_FLAG_ADDR_SET_BY_PF)
814 spin_lock_bh(&adapter->mac_vlan_list_lock);
816 f = iavf_find_filter(adapter, hw->mac.addr);
819 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
822 f = iavf_add_filter(adapter, addr->sa_data);
824 spin_unlock_bh(&adapter->mac_vlan_list_lock);
827 ether_addr_copy(hw->mac.addr, addr->sa_data);
828 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
831 return (f == NULL) ? -ENOMEM : 0;
835 * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
836 * @netdev: the netdevice
837 * @addr: address to add
839 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
840 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
842 static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
844 struct iavf_adapter *adapter = netdev_priv(netdev);
846 if (iavf_add_filter(adapter, addr))
853 * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
854 * @netdev: the netdevice
855 * @addr: address to add
857 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
858 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
860 static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
862 struct iavf_adapter *adapter = netdev_priv(netdev);
863 struct iavf_mac_filter *f;
865 /* Under some circumstances, we might receive a request to delete
866 * our own device address from our uc list. Because we store the
867 * device address in the VSI's MAC/VLAN filter list, we need to ignore
868 * such requests and not delete our device address from this list.
870 if (ether_addr_equal(addr, netdev->dev_addr))
873 f = iavf_find_filter(adapter, addr);
876 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
882 * iavf_set_rx_mode - NDO callback to set the netdev filters
883 * @netdev: network interface device structure
885 static void iavf_set_rx_mode(struct net_device *netdev)
887 struct iavf_adapter *adapter = netdev_priv(netdev);
889 spin_lock_bh(&adapter->mac_vlan_list_lock);
890 __dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
891 __dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
892 spin_unlock_bh(&adapter->mac_vlan_list_lock);
894 if (netdev->flags & IFF_PROMISC &&
895 !(adapter->flags & IAVF_FLAG_PROMISC_ON))
896 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_PROMISC;
897 else if (!(netdev->flags & IFF_PROMISC) &&
898 adapter->flags & IAVF_FLAG_PROMISC_ON)
899 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_PROMISC;
901 if (netdev->flags & IFF_ALLMULTI &&
902 !(adapter->flags & IAVF_FLAG_ALLMULTI_ON))
903 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_ALLMULTI;
904 else if (!(netdev->flags & IFF_ALLMULTI) &&
905 adapter->flags & IAVF_FLAG_ALLMULTI_ON)
906 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_ALLMULTI;
910 * iavf_napi_enable_all - enable NAPI on all queue vectors
911 * @adapter: board private structure
913 static void iavf_napi_enable_all(struct iavf_adapter *adapter)
916 struct iavf_q_vector *q_vector;
917 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
919 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
920 struct napi_struct *napi;
922 q_vector = &adapter->q_vectors[q_idx];
923 napi = &q_vector->napi;
929 * iavf_napi_disable_all - disable NAPI on all queue vectors
930 * @adapter: board private structure
932 static void iavf_napi_disable_all(struct iavf_adapter *adapter)
935 struct iavf_q_vector *q_vector;
936 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
938 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
939 q_vector = &adapter->q_vectors[q_idx];
940 napi_disable(&q_vector->napi);
945 * iavf_configure - set up transmit and receive data structures
946 * @adapter: board private structure
948 static void iavf_configure(struct iavf_adapter *adapter)
950 struct net_device *netdev = adapter->netdev;
953 iavf_set_rx_mode(netdev);
955 iavf_configure_tx(adapter);
956 iavf_configure_rx(adapter);
957 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
959 for (i = 0; i < adapter->num_active_queues; i++) {
960 struct iavf_ring *ring = &adapter->rx_rings[i];
962 iavf_alloc_rx_buffers(ring, IAVF_DESC_UNUSED(ring));
967 * iavf_up_complete - Finish the last steps of bringing up a connection
968 * @adapter: board private structure
970 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
972 static void iavf_up_complete(struct iavf_adapter *adapter)
974 adapter->state = __IAVF_RUNNING;
975 clear_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
977 iavf_napi_enable_all(adapter);
979 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_QUEUES;
980 if (CLIENT_ENABLED(adapter))
981 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_OPEN;
982 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
986 * iavf_down - Shutdown the connection processing
987 * @adapter: board private structure
989 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
991 void iavf_down(struct iavf_adapter *adapter)
993 struct net_device *netdev = adapter->netdev;
994 struct iavf_vlan_filter *vlf;
995 struct iavf_mac_filter *f;
996 struct iavf_cloud_filter *cf;
998 if (adapter->state <= __IAVF_DOWN_PENDING)
1001 netif_carrier_off(netdev);
1002 netif_tx_disable(netdev);
1003 adapter->link_up = false;
1004 iavf_napi_disable_all(adapter);
1005 iavf_irq_disable(adapter);
1007 spin_lock_bh(&adapter->mac_vlan_list_lock);
1009 /* clear the sync flag on all filters */
1010 __dev_uc_unsync(adapter->netdev, NULL);
1011 __dev_mc_unsync(adapter->netdev, NULL);
1013 /* remove all MAC filters */
1014 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1018 /* remove all VLAN filters */
1019 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1023 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1025 /* remove all cloud filters */
1026 spin_lock_bh(&adapter->cloud_filter_list_lock);
1027 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1030 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1032 if (!(adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) &&
1033 adapter->state != __IAVF_RESETTING) {
1034 /* cancel any current operation */
1035 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1036 /* Schedule operations to close down the HW. Don't wait
1037 * here for this to complete. The watchdog is still running
1038 * and it will take care of this.
1040 adapter->aq_required = IAVF_FLAG_AQ_DEL_MAC_FILTER;
1041 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
1042 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1043 adapter->aq_required |= IAVF_FLAG_AQ_DISABLE_QUEUES;
1046 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1050 * iavf_acquire_msix_vectors - Setup the MSIX capability
1051 * @adapter: board private structure
1052 * @vectors: number of vectors to request
1054 * Work with the OS to set up the MSIX vectors needed.
1056 * Returns 0 on success, negative on failure
1059 iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1061 int err, vector_threshold;
1063 /* We'll want at least 3 (vector_threshold):
1064 * 0) Other (Admin Queue and link, mostly)
1068 vector_threshold = MIN_MSIX_COUNT;
1070 /* The more we get, the more we will assign to Tx/Rx Cleanup
1071 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1072 * Right now, we simply care about how many we'll get; we'll
1073 * set them up later while requesting irq's.
1075 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1076 vector_threshold, vectors);
1078 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1079 kfree(adapter->msix_entries);
1080 adapter->msix_entries = NULL;
1084 /* Adjust for only the vectors we'll use, which is minimum
1085 * of max_msix_q_vectors + NONQ_VECS, or the number of
1086 * vectors we were allocated.
1088 adapter->num_msix_vectors = err;
1093 * iavf_free_queues - Free memory for all rings
1094 * @adapter: board private structure to initialize
1096 * Free all of the memory associated with queue pairs.
1098 static void iavf_free_queues(struct iavf_adapter *adapter)
1100 if (!adapter->vsi_res)
1102 adapter->num_active_queues = 0;
1103 kfree(adapter->tx_rings);
1104 adapter->tx_rings = NULL;
1105 kfree(adapter->rx_rings);
1106 adapter->rx_rings = NULL;
1110 * iavf_alloc_queues - Allocate memory for all rings
1111 * @adapter: board private structure to initialize
1113 * We allocate one ring per queue at run-time since we don't know the
1114 * number of queues at compile-time. The polling_netdev array is
1115 * intended for Multiqueue, but should work fine with a single queue.
1117 static int iavf_alloc_queues(struct iavf_adapter *adapter)
1119 int i, num_active_queues;
1121 /* If we're in reset reallocating queues we don't actually know yet for
1122 * certain the PF gave us the number of queues we asked for but we'll
1123 * assume it did. Once basic reset is finished we'll confirm once we
1124 * start negotiating config with PF.
1126 if (adapter->num_req_queues)
1127 num_active_queues = adapter->num_req_queues;
1128 else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1130 num_active_queues = adapter->ch_config.total_qps;
1132 num_active_queues = min_t(int,
1133 adapter->vsi_res->num_queue_pairs,
1134 (int)(num_online_cpus()));
1137 adapter->tx_rings = kcalloc(num_active_queues,
1138 sizeof(struct iavf_ring), GFP_KERNEL);
1139 if (!adapter->tx_rings)
1141 adapter->rx_rings = kcalloc(num_active_queues,
1142 sizeof(struct iavf_ring), GFP_KERNEL);
1143 if (!adapter->rx_rings)
1146 for (i = 0; i < num_active_queues; i++) {
1147 struct iavf_ring *tx_ring;
1148 struct iavf_ring *rx_ring;
1150 tx_ring = &adapter->tx_rings[i];
1152 tx_ring->queue_index = i;
1153 tx_ring->netdev = adapter->netdev;
1154 tx_ring->dev = &adapter->pdev->dev;
1155 tx_ring->count = adapter->tx_desc_count;
1156 tx_ring->itr_setting = IAVF_ITR_TX_DEF;
1157 if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1158 tx_ring->flags |= IAVF_TXR_FLAGS_WB_ON_ITR;
1160 rx_ring = &adapter->rx_rings[i];
1161 rx_ring->queue_index = i;
1162 rx_ring->netdev = adapter->netdev;
1163 rx_ring->dev = &adapter->pdev->dev;
1164 rx_ring->count = adapter->rx_desc_count;
1165 rx_ring->itr_setting = IAVF_ITR_RX_DEF;
1168 adapter->num_active_queues = num_active_queues;
1173 iavf_free_queues(adapter);
1178 * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1179 * @adapter: board private structure to initialize
1181 * Attempt to configure the interrupts using the best available
1182 * capabilities of the hardware and the kernel.
1184 static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1186 int vector, v_budget;
1190 if (!adapter->vsi_res) {
1194 pairs = adapter->num_active_queues;
1196 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1197 * us much good if we have more vectors than CPUs. However, we already
1198 * limit the total number of queues by the number of CPUs so we do not
1199 * need any further limiting here.
1201 v_budget = min_t(int, pairs + NONQ_VECS,
1202 (int)adapter->vf_res->max_vectors);
1204 adapter->msix_entries = kcalloc(v_budget,
1205 sizeof(struct msix_entry), GFP_KERNEL);
1206 if (!adapter->msix_entries) {
1211 for (vector = 0; vector < v_budget; vector++)
1212 adapter->msix_entries[vector].entry = vector;
1214 err = iavf_acquire_msix_vectors(adapter, v_budget);
1217 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1218 netif_set_real_num_tx_queues(adapter->netdev, pairs);
1223 * iavf_config_rss_aq - Configure RSS keys and lut by using AQ commands
1224 * @adapter: board private structure
1226 * Return 0 on success, negative on failure
1228 static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1230 struct i40e_aqc_get_set_rss_key_data *rss_key =
1231 (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
1232 struct iavf_hw *hw = &adapter->hw;
1235 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1236 /* bail because we already have a command pending */
1237 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1238 adapter->current_op);
1242 ret = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1244 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1245 iavf_stat_str(hw, ret),
1246 iavf_aq_str(hw, hw->aq.asq_last_status));
1251 ret = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1252 adapter->rss_lut, adapter->rss_lut_size);
1254 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1255 iavf_stat_str(hw, ret),
1256 iavf_aq_str(hw, hw->aq.asq_last_status));
1264 * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1265 * @adapter: board private structure
1267 * Returns 0 on success, negative on failure
1269 static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1271 struct iavf_hw *hw = &adapter->hw;
1275 dw = (u32 *)adapter->rss_key;
1276 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1277 wr32(hw, IAVF_VFQF_HKEY(i), dw[i]);
1279 dw = (u32 *)adapter->rss_lut;
1280 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1281 wr32(hw, IAVF_VFQF_HLUT(i), dw[i]);
1289 * iavf_config_rss - Configure RSS keys and lut
1290 * @adapter: board private structure
1292 * Returns 0 on success, negative on failure
1294 int iavf_config_rss(struct iavf_adapter *adapter)
1297 if (RSS_PF(adapter)) {
1298 adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1299 IAVF_FLAG_AQ_SET_RSS_KEY;
1301 } else if (RSS_AQ(adapter)) {
1302 return iavf_config_rss_aq(adapter);
1304 return iavf_config_rss_reg(adapter);
1309 * iavf_fill_rss_lut - Fill the lut with default values
1310 * @adapter: board private structure
1312 static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1316 for (i = 0; i < adapter->rss_lut_size; i++)
1317 adapter->rss_lut[i] = i % adapter->num_active_queues;
1321 * iavf_init_rss - Prepare for RSS
1322 * @adapter: board private structure
1324 * Return 0 on success, negative on failure
1326 static int iavf_init_rss(struct iavf_adapter *adapter)
1328 struct iavf_hw *hw = &adapter->hw;
1331 if (!RSS_PF(adapter)) {
1332 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1333 if (adapter->vf_res->vf_cap_flags &
1334 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1335 adapter->hena = IAVF_DEFAULT_RSS_HENA_EXPANDED;
1337 adapter->hena = IAVF_DEFAULT_RSS_HENA;
1339 wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->hena);
1340 wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1343 iavf_fill_rss_lut(adapter);
1344 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1345 ret = iavf_config_rss(adapter);
1351 * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1352 * @adapter: board private structure to initialize
1354 * We allocate one q_vector per queue interrupt. If allocation fails we
1357 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1359 int q_idx = 0, num_q_vectors;
1360 struct iavf_q_vector *q_vector;
1362 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1363 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1365 if (!adapter->q_vectors)
1368 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1369 q_vector = &adapter->q_vectors[q_idx];
1370 q_vector->adapter = adapter;
1371 q_vector->vsi = &adapter->vsi;
1372 q_vector->v_idx = q_idx;
1373 q_vector->reg_idx = q_idx;
1374 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1375 netif_napi_add(adapter->netdev, &q_vector->napi,
1376 iavf_napi_poll, NAPI_POLL_WEIGHT);
1383 * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1384 * @adapter: board private structure to initialize
1386 * This function frees the memory allocated to the q_vectors. In addition if
1387 * NAPI is enabled it will delete any references to the NAPI struct prior
1388 * to freeing the q_vector.
1390 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1392 int q_idx, num_q_vectors;
1395 if (!adapter->q_vectors)
1398 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1399 napi_vectors = adapter->num_active_queues;
1401 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1402 struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1404 if (q_idx < napi_vectors)
1405 netif_napi_del(&q_vector->napi);
1407 kfree(adapter->q_vectors);
1408 adapter->q_vectors = NULL;
1412 * iavf_reset_interrupt_capability - Reset MSIX setup
1413 * @adapter: board private structure
1416 void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1418 if (!adapter->msix_entries)
1421 pci_disable_msix(adapter->pdev);
1422 kfree(adapter->msix_entries);
1423 adapter->msix_entries = NULL;
1427 * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1428 * @adapter: board private structure to initialize
1431 int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1435 err = iavf_alloc_queues(adapter);
1437 dev_err(&adapter->pdev->dev,
1438 "Unable to allocate memory for queues\n");
1439 goto err_alloc_queues;
1443 err = iavf_set_interrupt_capability(adapter);
1446 dev_err(&adapter->pdev->dev,
1447 "Unable to setup interrupt capabilities\n");
1448 goto err_set_interrupt;
1451 err = iavf_alloc_q_vectors(adapter);
1453 dev_err(&adapter->pdev->dev,
1454 "Unable to allocate memory for queue vectors\n");
1455 goto err_alloc_q_vectors;
1458 /* If we've made it so far while ADq flag being ON, then we haven't
1459 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1460 * resources have been allocated in the reset path.
1461 * Now we can truly claim that ADq is enabled.
1463 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1465 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1468 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1469 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1470 adapter->num_active_queues);
1473 err_alloc_q_vectors:
1474 iavf_reset_interrupt_capability(adapter);
1476 iavf_free_queues(adapter);
1482 * iavf_free_rss - Free memory used by RSS structs
1483 * @adapter: board private structure
1485 static void iavf_free_rss(struct iavf_adapter *adapter)
1487 kfree(adapter->rss_key);
1488 adapter->rss_key = NULL;
1490 kfree(adapter->rss_lut);
1491 adapter->rss_lut = NULL;
1495 * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1496 * @adapter: board private structure
1498 * Returns 0 on success, negative on failure
1500 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter)
1502 struct net_device *netdev = adapter->netdev;
1505 if (netif_running(netdev))
1506 iavf_free_traffic_irqs(adapter);
1507 iavf_free_misc_irq(adapter);
1508 iavf_reset_interrupt_capability(adapter);
1509 iavf_free_q_vectors(adapter);
1510 iavf_free_queues(adapter);
1512 err = iavf_init_interrupt_scheme(adapter);
1516 netif_tx_stop_all_queues(netdev);
1518 err = iavf_request_misc_irq(adapter);
1522 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1524 iavf_map_rings_to_vectors(adapter);
1526 if (RSS_AQ(adapter))
1527 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
1529 err = iavf_init_rss(adapter);
1535 * iavf_watchdog_timer - Periodic call-back timer
1536 * @data: pointer to adapter disguised as unsigned long
1538 static void iavf_watchdog_timer(struct timer_list *t)
1540 struct iavf_adapter *adapter = from_timer(adapter, t,
1543 schedule_work(&adapter->watchdog_task);
1544 /* timer will be rescheduled in watchdog task */
1548 * iavf_watchdog_task - Periodic call-back task
1549 * @work: pointer to work_struct
1551 static void iavf_watchdog_task(struct work_struct *work)
1553 struct iavf_adapter *adapter = container_of(work,
1554 struct iavf_adapter,
1556 struct iavf_hw *hw = &adapter->hw;
1559 if (test_and_set_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section))
1560 goto restart_watchdog;
1562 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
1563 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
1564 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1565 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1566 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
1567 /* A chance for redemption! */
1568 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1569 adapter->state = __IAVF_STARTUP;
1570 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1571 schedule_delayed_work(&adapter->init_task, 10);
1572 clear_bit(__IAVF_IN_CRITICAL_TASK,
1573 &adapter->crit_section);
1574 /* Don't reschedule the watchdog, since we've restarted
1575 * the init task. When init_task contacts the PF and
1576 * gets everything set up again, it'll restart the
1577 * watchdog for us. Down, boy. Sit. Stay. Woof.
1581 adapter->aq_required = 0;
1582 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1586 if ((adapter->state < __IAVF_DOWN) ||
1587 (adapter->flags & IAVF_FLAG_RESET_PENDING))
1590 /* check for reset */
1591 reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
1592 if (!(adapter->flags & IAVF_FLAG_RESET_PENDING) && !reg_val) {
1593 adapter->state = __IAVF_RESETTING;
1594 adapter->flags |= IAVF_FLAG_RESET_PENDING;
1595 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1596 schedule_work(&adapter->reset_task);
1597 adapter->aq_required = 0;
1598 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1602 /* Process admin queue tasks. After init, everything gets done
1603 * here so we don't race on the admin queue.
1605 if (adapter->current_op) {
1606 if (!iavf_asq_done(hw)) {
1607 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1608 iavf_send_api_ver(adapter);
1612 if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG) {
1613 iavf_send_vf_config_msg(adapter);
1617 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
1618 iavf_disable_queues(adapter);
1622 if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
1623 iavf_map_queues(adapter);
1627 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
1628 iavf_add_ether_addrs(adapter);
1632 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
1633 iavf_add_vlans(adapter);
1637 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
1638 iavf_del_ether_addrs(adapter);
1642 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
1643 iavf_del_vlans(adapter);
1647 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1648 iavf_enable_vlan_stripping(adapter);
1652 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1653 iavf_disable_vlan_stripping(adapter);
1657 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
1658 iavf_configure_queues(adapter);
1662 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
1663 iavf_enable_queues(adapter);
1667 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
1668 /* This message goes straight to the firmware, not the
1669 * PF, so we don't have to set current_op as we will
1670 * not get a response through the ARQ.
1672 iavf_init_rss(adapter);
1673 adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
1676 if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
1677 iavf_get_hena(adapter);
1680 if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
1681 iavf_set_hena(adapter);
1684 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
1685 iavf_set_rss_key(adapter);
1688 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
1689 iavf_set_rss_lut(adapter);
1693 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_PROMISC) {
1694 iavf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1695 FLAG_VF_MULTICAST_PROMISC);
1699 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_ALLMULTI) {
1700 iavf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1704 if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) &&
1705 (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1706 iavf_set_promiscuous(adapter, 0);
1710 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
1711 iavf_enable_channels(adapter);
1715 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
1716 iavf_disable_channels(adapter);
1720 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1721 iavf_add_cloud_filter(adapter);
1725 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1726 iavf_del_cloud_filter(adapter);
1730 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1732 if (adapter->state == __IAVF_RUNNING)
1733 iavf_request_stats(adapter);
1735 if (adapter->state == __IAVF_RUNNING)
1736 iavf_detect_recover_hung(&adapter->vsi);
1737 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1739 if (adapter->state == __IAVF_REMOVE)
1741 if (adapter->aq_required)
1742 mod_timer(&adapter->watchdog_timer,
1743 jiffies + msecs_to_jiffies(20));
1745 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1746 schedule_work(&adapter->adminq_task);
1749 static void iavf_disable_vf(struct iavf_adapter *adapter)
1751 struct iavf_mac_filter *f, *ftmp;
1752 struct iavf_vlan_filter *fv, *fvtmp;
1753 struct iavf_cloud_filter *cf, *cftmp;
1755 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
1757 /* We don't use netif_running() because it may be true prior to
1758 * ndo_open() returning, so we can't assume it means all our open
1759 * tasks have finished, since we're not holding the rtnl_lock here.
1761 if (adapter->state == __IAVF_RUNNING) {
1762 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1763 netif_carrier_off(adapter->netdev);
1764 netif_tx_disable(adapter->netdev);
1765 adapter->link_up = false;
1766 iavf_napi_disable_all(adapter);
1767 iavf_irq_disable(adapter);
1768 iavf_free_traffic_irqs(adapter);
1769 iavf_free_all_tx_resources(adapter);
1770 iavf_free_all_rx_resources(adapter);
1773 spin_lock_bh(&adapter->mac_vlan_list_lock);
1775 /* Delete all of the filters */
1776 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1781 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1782 list_del(&fv->list);
1786 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1788 spin_lock_bh(&adapter->cloud_filter_list_lock);
1789 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1790 list_del(&cf->list);
1792 adapter->num_cloud_filters--;
1794 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1796 iavf_free_misc_irq(adapter);
1797 iavf_reset_interrupt_capability(adapter);
1798 iavf_free_queues(adapter);
1799 iavf_free_q_vectors(adapter);
1800 kfree(adapter->vf_res);
1801 iavf_shutdown_adminq(&adapter->hw);
1802 adapter->netdev->flags &= ~IFF_UP;
1803 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1804 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1805 adapter->state = __IAVF_DOWN;
1806 wake_up(&adapter->down_waitqueue);
1807 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1810 #define IAVF_RESET_WAIT_MS 10
1811 #define IAVF_RESET_WAIT_COUNT 500
1813 * iavf_reset_task - Call-back task to handle hardware reset
1814 * @work: pointer to work_struct
1816 * During reset we need to shut down and reinitialize the admin queue
1817 * before we can use it to communicate with the PF again. We also clear
1818 * and reinit the rings because that context is lost as well.
1820 static void iavf_reset_task(struct work_struct *work)
1822 struct iavf_adapter *adapter = container_of(work,
1823 struct iavf_adapter,
1825 struct virtchnl_vf_resource *vfres = adapter->vf_res;
1826 struct net_device *netdev = adapter->netdev;
1827 struct iavf_hw *hw = &adapter->hw;
1828 struct iavf_vlan_filter *vlf;
1829 struct iavf_cloud_filter *cf;
1830 struct iavf_mac_filter *f;
1835 /* When device is being removed it doesn't make sense to run the reset
1836 * task, just return in such a case.
1838 if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
1841 while (test_and_set_bit(__IAVF_IN_CLIENT_TASK,
1842 &adapter->crit_section))
1843 usleep_range(500, 1000);
1844 if (CLIENT_ENABLED(adapter)) {
1845 adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
1846 IAVF_FLAG_CLIENT_NEEDS_CLOSE |
1847 IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1848 IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
1849 cancel_delayed_work_sync(&adapter->client_task);
1850 iavf_notify_client_close(&adapter->vsi, true);
1852 iavf_misc_irq_disable(adapter);
1853 if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
1854 adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
1855 /* Restart the AQ here. If we have been reset but didn't
1856 * detect it, or if the PF had to reinit, our AQ will be hosed.
1858 iavf_shutdown_adminq(hw);
1859 iavf_init_adminq(hw);
1860 iavf_request_reset(adapter);
1862 adapter->flags |= IAVF_FLAG_RESET_PENDING;
1864 /* poll until we see the reset actually happen */
1865 for (i = 0; i < IAVF_RESET_WAIT_COUNT; i++) {
1866 reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
1867 IAVF_VF_ARQLEN1_ARQENABLE_MASK;
1870 usleep_range(5000, 10000);
1872 if (i == IAVF_RESET_WAIT_COUNT) {
1873 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1874 goto continue_reset; /* act like the reset happened */
1877 /* wait until the reset is complete and the PF is responding to us */
1878 for (i = 0; i < IAVF_RESET_WAIT_COUNT; i++) {
1879 /* sleep first to make sure a minimum wait time is met */
1880 msleep(IAVF_RESET_WAIT_MS);
1882 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
1883 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1884 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
1888 pci_set_master(adapter->pdev);
1890 if (i == IAVF_RESET_WAIT_COUNT) {
1891 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1893 iavf_disable_vf(adapter);
1894 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
1895 return; /* Do not attempt to reinit. It's dead, Jim. */
1899 /* We don't use netif_running() because it may be true prior to
1900 * ndo_open() returning, so we can't assume it means all our open
1901 * tasks have finished, since we're not holding the rtnl_lock here.
1903 running = ((adapter->state == __IAVF_RUNNING) ||
1904 (adapter->state == __IAVF_RESETTING));
1907 netif_carrier_off(netdev);
1908 netif_tx_stop_all_queues(netdev);
1909 adapter->link_up = false;
1910 iavf_napi_disable_all(adapter);
1912 iavf_irq_disable(adapter);
1914 adapter->state = __IAVF_RESETTING;
1915 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1917 /* free the Tx/Rx rings and descriptors, might be better to just
1918 * re-use them sometime in the future
1920 iavf_free_all_rx_resources(adapter);
1921 iavf_free_all_tx_resources(adapter);
1923 adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
1924 /* kill and reinit the admin queue */
1925 iavf_shutdown_adminq(hw);
1926 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1927 err = iavf_init_adminq(hw);
1929 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1931 adapter->aq_required = 0;
1933 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
1934 err = iavf_reinit_interrupt_scheme(adapter);
1939 adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
1940 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
1942 spin_lock_bh(&adapter->mac_vlan_list_lock);
1944 /* re-add all MAC filters */
1945 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1948 /* re-add all VLAN filters */
1949 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1953 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1955 /* check if TCs are running and re-add all cloud filters */
1956 spin_lock_bh(&adapter->cloud_filter_list_lock);
1957 if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1959 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1963 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1965 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
1966 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
1967 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
1968 iavf_misc_irq_enable(adapter);
1970 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1972 /* We were running when the reset started, so we need to restore some
1976 /* allocate transmit descriptors */
1977 err = iavf_setup_all_tx_resources(adapter);
1981 /* allocate receive descriptors */
1982 err = iavf_setup_all_rx_resources(adapter);
1986 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
1987 err = iavf_request_traffic_irqs(adapter, netdev->name);
1991 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1994 iavf_configure(adapter);
1996 iavf_up_complete(adapter);
1998 iavf_irq_enable(adapter, true);
2000 adapter->state = __IAVF_DOWN;
2001 wake_up(&adapter->down_waitqueue);
2003 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2004 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2008 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2009 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2010 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
2015 * iavf_adminq_task - worker thread to clean the admin queue
2016 * @work: pointer to work_struct containing our data
2018 static void iavf_adminq_task(struct work_struct *work)
2020 struct iavf_adapter *adapter =
2021 container_of(work, struct iavf_adapter, adminq_task);
2022 struct iavf_hw *hw = &adapter->hw;
2023 struct i40e_arq_event_info event;
2024 enum virtchnl_ops v_op;
2025 iavf_status ret, v_ret;
2029 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2032 event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
2033 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2038 ret = iavf_clean_arq_element(hw, &event, &pending);
2039 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2040 v_ret = (iavf_status)le32_to_cpu(event.desc.cookie_low);
2043 break; /* No event to process or error cleaning ARQ */
2045 iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2048 memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
2051 if ((adapter->flags &
2052 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED)) ||
2053 adapter->state == __IAVF_RESETTING)
2056 /* check for error indications */
2057 val = rd32(hw, hw->aq.arq.len);
2058 if (val == 0xdeadbeef) /* indicates device in reset */
2061 if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
2062 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2063 val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
2065 if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
2066 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2067 val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
2069 if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
2070 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2071 val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
2074 wr32(hw, hw->aq.arq.len, val);
2076 val = rd32(hw, hw->aq.asq.len);
2078 if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
2079 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2080 val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
2082 if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
2083 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2084 val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
2086 if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
2087 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2088 val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
2091 wr32(hw, hw->aq.asq.len, val);
2094 kfree(event.msg_buf);
2096 /* re-enable Admin queue interrupt cause */
2097 iavf_misc_irq_enable(adapter);
2101 * iavf_client_task - worker thread to perform client work
2102 * @work: pointer to work_struct containing our data
2104 * This task handles client interactions. Because client calls can be
2105 * reentrant, we can't handle them in the watchdog.
2107 static void iavf_client_task(struct work_struct *work)
2109 struct iavf_adapter *adapter =
2110 container_of(work, struct iavf_adapter, client_task.work);
2112 /* If we can't get the client bit, just give up. We'll be rescheduled
2116 if (test_and_set_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section))
2119 if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2120 iavf_client_subtask(adapter);
2121 adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
2124 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2125 iavf_notify_client_l2_params(&adapter->vsi);
2126 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2129 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_CLOSE) {
2130 iavf_notify_client_close(&adapter->vsi, false);
2131 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_CLOSE;
2134 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
2135 iavf_notify_client_open(&adapter->vsi);
2136 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
2139 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2143 * iavf_free_all_tx_resources - Free Tx Resources for All Queues
2144 * @adapter: board private structure
2146 * Free all transmit software resources
2148 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
2152 if (!adapter->tx_rings)
2155 for (i = 0; i < adapter->num_active_queues; i++)
2156 if (adapter->tx_rings[i].desc)
2157 iavf_free_tx_resources(&adapter->tx_rings[i]);
2161 * iavf_setup_all_tx_resources - allocate all queues Tx resources
2162 * @adapter: board private structure
2164 * If this function returns with an error, then it's possible one or
2165 * more of the rings is populated (while the rest are not). It is the
2166 * callers duty to clean those orphaned rings.
2168 * Return 0 on success, negative on failure
2170 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
2174 for (i = 0; i < adapter->num_active_queues; i++) {
2175 adapter->tx_rings[i].count = adapter->tx_desc_count;
2176 err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
2179 dev_err(&adapter->pdev->dev,
2180 "Allocation for Tx Queue %u failed\n", i);
2188 * iavf_setup_all_rx_resources - allocate all queues Rx resources
2189 * @adapter: board private structure
2191 * If this function returns with an error, then it's possible one or
2192 * more of the rings is populated (while the rest are not). It is the
2193 * callers duty to clean those orphaned rings.
2195 * Return 0 on success, negative on failure
2197 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
2201 for (i = 0; i < adapter->num_active_queues; i++) {
2202 adapter->rx_rings[i].count = adapter->rx_desc_count;
2203 err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
2206 dev_err(&adapter->pdev->dev,
2207 "Allocation for Rx Queue %u failed\n", i);
2214 * iavf_free_all_rx_resources - Free Rx Resources for All Queues
2215 * @adapter: board private structure
2217 * Free all receive software resources
2219 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
2223 if (!adapter->rx_rings)
2226 for (i = 0; i < adapter->num_active_queues; i++)
2227 if (adapter->rx_rings[i].desc)
2228 iavf_free_rx_resources(&adapter->rx_rings[i]);
2232 * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
2233 * @adapter: board private structure
2234 * @max_tx_rate: max Tx bw for a tc
2236 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
2239 int speed = 0, ret = 0;
2241 switch (adapter->link_speed) {
2242 case I40E_LINK_SPEED_40GB:
2245 case I40E_LINK_SPEED_25GB:
2248 case I40E_LINK_SPEED_20GB:
2251 case I40E_LINK_SPEED_10GB:
2254 case I40E_LINK_SPEED_1GB:
2257 case I40E_LINK_SPEED_100MB:
2264 if (max_tx_rate > speed) {
2265 dev_err(&adapter->pdev->dev,
2266 "Invalid tx rate specified\n");
2274 * iavf_validate_channel_config - validate queue mapping info
2275 * @adapter: board private structure
2276 * @mqprio_qopt: queue parameters
2278 * This function validates if the config provided by the user to
2279 * configure queue channels is valid or not. Returns 0 on a valid
2282 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
2283 struct tc_mqprio_qopt_offload *mqprio_qopt)
2285 u64 total_max_rate = 0;
2290 if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
2291 mqprio_qopt->qopt.num_tc < 1)
2294 for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2295 if (!mqprio_qopt->qopt.count[i] ||
2296 mqprio_qopt->qopt.offset[i] != num_qps)
2298 if (mqprio_qopt->min_rate[i]) {
2299 dev_err(&adapter->pdev->dev,
2300 "Invalid min tx rate (greater than 0) specified\n");
2303 /*convert to Mbps */
2304 tx_rate = div_u64(mqprio_qopt->max_rate[i],
2306 total_max_rate += tx_rate;
2307 num_qps += mqprio_qopt->qopt.count[i];
2309 if (num_qps > IAVF_MAX_REQ_QUEUES)
2312 ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
2317 * iavf_del_all_cloud_filters - delete all cloud filters
2318 * on the traffic classes
2320 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
2322 struct iavf_cloud_filter *cf, *cftmp;
2324 spin_lock_bh(&adapter->cloud_filter_list_lock);
2325 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2327 list_del(&cf->list);
2329 adapter->num_cloud_filters--;
2331 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2335 * __iavf_setup_tc - configure multiple traffic classes
2336 * @netdev: network interface device structure
2337 * @type_date: tc offload data
2339 * This function processes the config information provided by the
2340 * user to configure traffic classes/queue channels and packages the
2341 * information to request the PF to setup traffic classes.
2343 * Returns 0 on success.
2345 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
2347 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2348 struct iavf_adapter *adapter = netdev_priv(netdev);
2349 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2350 u8 num_tc = 0, total_qps = 0;
2351 int ret = 0, netdev_tc = 0;
2356 num_tc = mqprio_qopt->qopt.num_tc;
2357 mode = mqprio_qopt->mode;
2359 /* delete queue_channel */
2360 if (!mqprio_qopt->qopt.hw) {
2361 if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
2362 /* reset the tc configuration */
2363 netdev_reset_tc(netdev);
2364 adapter->num_tc = 0;
2365 netif_tx_stop_all_queues(netdev);
2366 netif_tx_disable(netdev);
2367 iavf_del_all_cloud_filters(adapter);
2368 adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
2375 /* add queue channel */
2376 if (mode == TC_MQPRIO_MODE_CHANNEL) {
2377 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2378 dev_err(&adapter->pdev->dev, "ADq not supported\n");
2381 if (adapter->ch_config.state != __IAVF_TC_INVALID) {
2382 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2386 ret = iavf_validate_ch_config(adapter, mqprio_qopt);
2389 /* Return if same TC config is requested */
2390 if (adapter->num_tc == num_tc)
2392 adapter->num_tc = num_tc;
2394 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2396 adapter->ch_config.ch_info[i].count =
2397 mqprio_qopt->qopt.count[i];
2398 adapter->ch_config.ch_info[i].offset =
2399 mqprio_qopt->qopt.offset[i];
2400 total_qps += mqprio_qopt->qopt.count[i];
2401 max_tx_rate = mqprio_qopt->max_rate[i];
2402 /* convert to Mbps */
2403 max_tx_rate = div_u64(max_tx_rate,
2405 adapter->ch_config.ch_info[i].max_tx_rate =
2408 adapter->ch_config.ch_info[i].count = 1;
2409 adapter->ch_config.ch_info[i].offset = 0;
2412 adapter->ch_config.total_qps = total_qps;
2413 netif_tx_stop_all_queues(netdev);
2414 netif_tx_disable(netdev);
2415 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
2416 netdev_reset_tc(netdev);
2417 /* Report the tc mapping up the stack */
2418 netdev_set_num_tc(adapter->netdev, num_tc);
2419 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2420 u16 qcount = mqprio_qopt->qopt.count[i];
2421 u16 qoffset = mqprio_qopt->qopt.offset[i];
2424 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2433 * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
2434 * @adapter: board private structure
2435 * @cls_flower: pointer to struct tc_cls_flower_offload
2436 * @filter: pointer to cloud filter structure
2438 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
2439 struct tc_cls_flower_offload *f,
2440 struct iavf_cloud_filter *filter)
2442 u16 n_proto_mask = 0;
2443 u16 n_proto_key = 0;
2448 struct virtchnl_filter *vf = &filter->f;
2450 if (f->dissector->used_keys &
2451 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2452 BIT(FLOW_DISSECTOR_KEY_BASIC) |
2453 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2454 BIT(FLOW_DISSECTOR_KEY_VLAN) |
2455 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2456 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2457 BIT(FLOW_DISSECTOR_KEY_PORTS) |
2458 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2459 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2460 f->dissector->used_keys);
2464 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2465 struct flow_dissector_key_keyid *mask =
2466 skb_flow_dissector_target(f->dissector,
2467 FLOW_DISSECTOR_KEY_ENC_KEYID,
2470 if (mask->keyid != 0)
2471 field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
2474 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
2475 struct flow_dissector_key_basic *key =
2476 skb_flow_dissector_target(f->dissector,
2477 FLOW_DISSECTOR_KEY_BASIC,
2480 struct flow_dissector_key_basic *mask =
2481 skb_flow_dissector_target(f->dissector,
2482 FLOW_DISSECTOR_KEY_BASIC,
2484 n_proto_key = ntohs(key->n_proto);
2485 n_proto_mask = ntohs(mask->n_proto);
2487 if (n_proto_key == ETH_P_ALL) {
2491 n_proto = n_proto_key & n_proto_mask;
2492 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2494 if (n_proto == ETH_P_IPV6) {
2495 /* specify flow type as TCP IPv6 */
2496 vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2499 if (key->ip_proto != IPPROTO_TCP) {
2500 dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2505 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2506 struct flow_dissector_key_eth_addrs *key =
2507 skb_flow_dissector_target(f->dissector,
2508 FLOW_DISSECTOR_KEY_ETH_ADDRS,
2511 struct flow_dissector_key_eth_addrs *mask =
2512 skb_flow_dissector_target(f->dissector,
2513 FLOW_DISSECTOR_KEY_ETH_ADDRS,
2515 /* use is_broadcast and is_zero to check for all 0xf or 0 */
2516 if (!is_zero_ether_addr(mask->dst)) {
2517 if (is_broadcast_ether_addr(mask->dst)) {
2518 field_flags |= IAVF_CLOUD_FIELD_OMAC;
2520 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2522 return I40E_ERR_CONFIG;
2526 if (!is_zero_ether_addr(mask->src)) {
2527 if (is_broadcast_ether_addr(mask->src)) {
2528 field_flags |= IAVF_CLOUD_FIELD_IMAC;
2530 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2532 return I40E_ERR_CONFIG;
2536 if (!is_zero_ether_addr(key->dst))
2537 if (is_valid_ether_addr(key->dst) ||
2538 is_multicast_ether_addr(key->dst)) {
2539 /* set the mask if a valid dst_mac address */
2540 for (i = 0; i < ETH_ALEN; i++)
2541 vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2542 ether_addr_copy(vf->data.tcp_spec.dst_mac,
2546 if (!is_zero_ether_addr(key->src))
2547 if (is_valid_ether_addr(key->src) ||
2548 is_multicast_ether_addr(key->src)) {
2549 /* set the mask if a valid dst_mac address */
2550 for (i = 0; i < ETH_ALEN; i++)
2551 vf->mask.tcp_spec.src_mac[i] |= 0xff;
2552 ether_addr_copy(vf->data.tcp_spec.src_mac,
2557 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
2558 struct flow_dissector_key_vlan *key =
2559 skb_flow_dissector_target(f->dissector,
2560 FLOW_DISSECTOR_KEY_VLAN,
2562 struct flow_dissector_key_vlan *mask =
2563 skb_flow_dissector_target(f->dissector,
2564 FLOW_DISSECTOR_KEY_VLAN,
2567 if (mask->vlan_id) {
2568 if (mask->vlan_id == VLAN_VID_MASK) {
2569 field_flags |= IAVF_CLOUD_FIELD_IVLAN;
2571 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2573 return I40E_ERR_CONFIG;
2576 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2577 vf->data.tcp_spec.vlan_id = cpu_to_be16(key->vlan_id);
2580 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
2581 struct flow_dissector_key_control *key =
2582 skb_flow_dissector_target(f->dissector,
2583 FLOW_DISSECTOR_KEY_CONTROL,
2586 addr_type = key->addr_type;
2589 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2590 struct flow_dissector_key_ipv4_addrs *key =
2591 skb_flow_dissector_target(f->dissector,
2592 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2594 struct flow_dissector_key_ipv4_addrs *mask =
2595 skb_flow_dissector_target(f->dissector,
2596 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
2600 if (mask->dst == cpu_to_be32(0xffffffff)) {
2601 field_flags |= IAVF_CLOUD_FIELD_IIP;
2603 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2604 be32_to_cpu(mask->dst));
2605 return I40E_ERR_CONFIG;
2610 if (mask->src == cpu_to_be32(0xffffffff)) {
2611 field_flags |= IAVF_CLOUD_FIELD_IIP;
2613 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2614 be32_to_cpu(mask->dst));
2615 return I40E_ERR_CONFIG;
2619 if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
2620 dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2621 return I40E_ERR_CONFIG;
2624 vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2625 vf->data.tcp_spec.dst_ip[0] = key->dst;
2628 vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2629 vf->data.tcp_spec.src_ip[0] = key->src;
2633 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2634 struct flow_dissector_key_ipv6_addrs *key =
2635 skb_flow_dissector_target(f->dissector,
2636 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2638 struct flow_dissector_key_ipv6_addrs *mask =
2639 skb_flow_dissector_target(f->dissector,
2640 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
2643 /* validate mask, make sure it is not IPV6_ADDR_ANY */
2644 if (ipv6_addr_any(&mask->dst)) {
2645 dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2647 return I40E_ERR_CONFIG;
2650 /* src and dest IPv6 address should not be LOOPBACK
2651 * (0:0:0:0:0:0:0:1) which can be represented as ::1
2653 if (ipv6_addr_loopback(&key->dst) ||
2654 ipv6_addr_loopback(&key->src)) {
2655 dev_err(&adapter->pdev->dev,
2656 "ipv6 addr should not be loopback\n");
2657 return I40E_ERR_CONFIG;
2659 if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
2660 field_flags |= IAVF_CLOUD_FIELD_IIP;
2662 for (i = 0; i < 4; i++)
2663 vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2664 memcpy(&vf->data.tcp_spec.dst_ip, &key->dst.s6_addr32,
2665 sizeof(vf->data.tcp_spec.dst_ip));
2666 for (i = 0; i < 4; i++)
2667 vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2668 memcpy(&vf->data.tcp_spec.src_ip, &key->src.s6_addr32,
2669 sizeof(vf->data.tcp_spec.src_ip));
2671 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
2672 struct flow_dissector_key_ports *key =
2673 skb_flow_dissector_target(f->dissector,
2674 FLOW_DISSECTOR_KEY_PORTS,
2676 struct flow_dissector_key_ports *mask =
2677 skb_flow_dissector_target(f->dissector,
2678 FLOW_DISSECTOR_KEY_PORTS,
2682 if (mask->src == cpu_to_be16(0xffff)) {
2683 field_flags |= IAVF_CLOUD_FIELD_IIP;
2685 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
2686 be16_to_cpu(mask->src));
2687 return I40E_ERR_CONFIG;
2692 if (mask->dst == cpu_to_be16(0xffff)) {
2693 field_flags |= IAVF_CLOUD_FIELD_IIP;
2695 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
2696 be16_to_cpu(mask->dst));
2697 return I40E_ERR_CONFIG;
2701 vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
2702 vf->data.tcp_spec.dst_port = key->dst;
2706 vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
2707 vf->data.tcp_spec.src_port = key->src;
2710 vf->field_flags = field_flags;
2716 * iavf_handle_tclass - Forward to a traffic class on the device
2717 * @adapter: board private structure
2718 * @tc: traffic class index on the device
2719 * @filter: pointer to cloud filter structure
2721 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
2722 struct iavf_cloud_filter *filter)
2726 if (tc < adapter->num_tc) {
2727 if (!filter->f.data.tcp_spec.dst_port) {
2728 dev_err(&adapter->pdev->dev,
2729 "Specify destination port to redirect to traffic class other than TC0\n");
2733 /* redirect to a traffic class on the same device */
2734 filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
2735 filter->f.action_meta = tc;
2740 * iavf_configure_clsflower - Add tc flower filters
2741 * @adapter: board private structure
2742 * @cls_flower: Pointer to struct tc_cls_flower_offload
2744 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
2745 struct tc_cls_flower_offload *cls_flower)
2747 int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
2748 struct iavf_cloud_filter *filter = NULL;
2749 int err = -EINVAL, count = 50;
2752 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
2756 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2760 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
2761 &adapter->crit_section)) {
2767 filter->cookie = cls_flower->cookie;
2769 /* set the mask to all zeroes to begin with */
2770 memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
2771 /* start out with flow type and eth type IPv4 to begin with */
2772 filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
2773 err = iavf_parse_cls_flower(adapter, cls_flower, filter);
2777 err = iavf_handle_tclass(adapter, tc, filter);
2781 /* add filter to the list */
2782 spin_lock_bh(&adapter->cloud_filter_list_lock);
2783 list_add_tail(&filter->list, &adapter->cloud_filter_list);
2784 adapter->num_cloud_filters++;
2786 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
2787 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2792 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2796 /* iavf_find_cf - Find the cloud filter in the list
2797 * @adapter: Board private structure
2798 * @cookie: filter specific cookie
2800 * Returns ptr to the filter object or NULL. Must be called while holding the
2801 * cloud_filter_list_lock.
2803 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
2804 unsigned long *cookie)
2806 struct iavf_cloud_filter *filter = NULL;
2811 list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
2812 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
2819 * iavf_delete_clsflower - Remove tc flower filters
2820 * @adapter: board private structure
2821 * @cls_flower: Pointer to struct tc_cls_flower_offload
2823 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
2824 struct tc_cls_flower_offload *cls_flower)
2826 struct iavf_cloud_filter *filter = NULL;
2829 spin_lock_bh(&adapter->cloud_filter_list_lock);
2830 filter = iavf_find_cf(adapter, &cls_flower->cookie);
2833 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
2837 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2843 * iavf_setup_tc_cls_flower - flower classifier offloads
2844 * @netdev: net device to configure
2845 * @type_data: offload data
2847 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
2848 struct tc_cls_flower_offload *cls_flower)
2850 if (cls_flower->common.chain_index)
2853 switch (cls_flower->command) {
2854 case TC_CLSFLOWER_REPLACE:
2855 return iavf_configure_clsflower(adapter, cls_flower);
2856 case TC_CLSFLOWER_DESTROY:
2857 return iavf_delete_clsflower(adapter, cls_flower);
2858 case TC_CLSFLOWER_STATS:
2866 * iavf_setup_tc_block_cb - block callback for tc
2867 * @type: type of offload
2868 * @type_data: offload data
2871 * This function is the block callback for traffic classes
2873 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2877 case TC_SETUP_CLSFLOWER:
2878 return iavf_setup_tc_cls_flower(cb_priv, type_data);
2885 * iavf_setup_tc_block - register callbacks for tc
2886 * @netdev: network interface device structure
2887 * @f: tc offload data
2889 * This function registers block callbacks for tc
2892 static int iavf_setup_tc_block(struct net_device *dev,
2893 struct tc_block_offload *f)
2895 struct iavf_adapter *adapter = netdev_priv(dev);
2897 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2900 switch (f->command) {
2902 return tcf_block_cb_register(f->block, iavf_setup_tc_block_cb,
2903 adapter, adapter, f->extack);
2904 case TC_BLOCK_UNBIND:
2905 tcf_block_cb_unregister(f->block, iavf_setup_tc_block_cb,
2914 * iavf_setup_tc - configure multiple traffic classes
2915 * @netdev: network interface device structure
2916 * @type: type of offload
2917 * @type_date: tc offload data
2919 * This function is the callback to ndo_setup_tc in the
2922 * Returns 0 on success
2924 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
2928 case TC_SETUP_QDISC_MQPRIO:
2929 return __iavf_setup_tc(netdev, type_data);
2930 case TC_SETUP_BLOCK:
2931 return iavf_setup_tc_block(netdev, type_data);
2938 * iavf_open - Called when a network interface is made active
2939 * @netdev: network interface device structure
2941 * Returns 0 on success, negative value on failure
2943 * The open entry point is called when a network interface is made
2944 * active by the system (IFF_UP). At this point all resources needed
2945 * for transmit and receive operations are allocated, the interrupt
2946 * handler is registered with the OS, the watchdog timer is started,
2947 * and the stack is notified that the interface is ready.
2949 static int iavf_open(struct net_device *netdev)
2951 struct iavf_adapter *adapter = netdev_priv(netdev);
2954 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
2955 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2959 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
2960 &adapter->crit_section))
2961 usleep_range(500, 1000);
2963 if (adapter->state != __IAVF_DOWN) {
2968 /* allocate transmit descriptors */
2969 err = iavf_setup_all_tx_resources(adapter);
2973 /* allocate receive descriptors */
2974 err = iavf_setup_all_rx_resources(adapter);
2978 /* clear any pending interrupts, may auto mask */
2979 err = iavf_request_traffic_irqs(adapter, netdev->name);
2983 spin_lock_bh(&adapter->mac_vlan_list_lock);
2985 iavf_add_filter(adapter, adapter->hw.mac.addr);
2987 spin_unlock_bh(&adapter->mac_vlan_list_lock);
2989 iavf_configure(adapter);
2991 iavf_up_complete(adapter);
2993 iavf_irq_enable(adapter, true);
2995 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3001 iavf_free_traffic_irqs(adapter);
3003 iavf_free_all_rx_resources(adapter);
3005 iavf_free_all_tx_resources(adapter);
3007 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3013 * iavf_close - Disables a network interface
3014 * @netdev: network interface device structure
3016 * Returns 0, this is not allowed to fail
3018 * The close entry point is called when an interface is de-activated
3019 * by the OS. The hardware is still under the drivers control, but
3020 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3021 * are freed, along with all transmit and receive resources.
3023 static int iavf_close(struct net_device *netdev)
3025 struct iavf_adapter *adapter = netdev_priv(netdev);
3028 if (adapter->state <= __IAVF_DOWN_PENDING)
3031 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3032 &adapter->crit_section))
3033 usleep_range(500, 1000);
3035 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3036 if (CLIENT_ENABLED(adapter))
3037 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3040 adapter->state = __IAVF_DOWN_PENDING;
3041 iavf_free_traffic_irqs(adapter);
3043 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3045 /* We explicitly don't free resources here because the hardware is
3046 * still active and can DMA into memory. Resources are cleared in
3047 * iavf_virtchnl_completion() after we get confirmation from the PF
3048 * driver that the rings have been stopped.
3050 * Also, we wait for state to transition to __IAVF_DOWN before
3051 * returning. State change occurs in iavf_virtchnl_completion() after
3052 * VF resources are released (which occurs after PF driver processes and
3053 * responds to admin queue commands).
3056 status = wait_event_timeout(adapter->down_waitqueue,
3057 adapter->state == __IAVF_DOWN,
3058 msecs_to_jiffies(200));
3060 netdev_warn(netdev, "Device resources not yet released\n");
3065 * iavf_change_mtu - Change the Maximum Transfer Unit
3066 * @netdev: network interface device structure
3067 * @new_mtu: new value for maximum frame size
3069 * Returns 0 on success, negative on failure
3071 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
3073 struct iavf_adapter *adapter = netdev_priv(netdev);
3075 netdev->mtu = new_mtu;
3076 if (CLIENT_ENABLED(adapter)) {
3077 iavf_notify_client_l2_params(&adapter->vsi);
3078 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3080 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
3081 schedule_work(&adapter->reset_task);
3087 * iavf_set_features - set the netdev feature flags
3088 * @netdev: ptr to the netdev being adjusted
3089 * @features: the feature set that the stack is suggesting
3090 * Note: expects to be called while under rtnl_lock()
3092 static int iavf_set_features(struct net_device *netdev,
3093 netdev_features_t features)
3095 struct iavf_adapter *adapter = netdev_priv(netdev);
3097 /* Don't allow changing VLAN_RX flag when adapter is not capable
3100 if (!VLAN_ALLOWED(adapter)) {
3101 if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX)
3103 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3104 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3105 adapter->aq_required |=
3106 IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3108 adapter->aq_required |=
3109 IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3116 * iavf_features_check - Validate encapsulated packet conforms to limits
3118 * @dev: This physical port's netdev
3119 * @features: Offload features that the stack believes apply
3121 static netdev_features_t iavf_features_check(struct sk_buff *skb,
3122 struct net_device *dev,
3123 netdev_features_t features)
3127 /* No point in doing any of this if neither checksum nor GSO are
3128 * being requested for this frame. We can rule out both by just
3129 * checking for CHECKSUM_PARTIAL
3131 if (skb->ip_summed != CHECKSUM_PARTIAL)
3134 /* We cannot support GSO if the MSS is going to be less than
3135 * 64 bytes. If it is then we need to drop support for GSO.
3137 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3138 features &= ~NETIF_F_GSO_MASK;
3140 /* MACLEN can support at most 63 words */
3141 len = skb_network_header(skb) - skb->data;
3142 if (len & ~(63 * 2))
3145 /* IPLEN and EIPLEN can support at most 127 dwords */
3146 len = skb_transport_header(skb) - skb_network_header(skb);
3147 if (len & ~(127 * 4))
3150 if (skb->encapsulation) {
3151 /* L4TUNLEN can support 127 words */
3152 len = skb_inner_network_header(skb) - skb_transport_header(skb);
3153 if (len & ~(127 * 2))
3156 /* IPLEN can support at most 127 dwords */
3157 len = skb_inner_transport_header(skb) -
3158 skb_inner_network_header(skb);
3159 if (len & ~(127 * 4))
3163 /* No need to validate L4LEN as TCP is the only protocol with a
3164 * a flexible value and we support all possible values supported
3165 * by TCP, which is at most 15 dwords
3170 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3174 * iavf_fix_features - fix up the netdev feature bits
3175 * @netdev: our net device
3176 * @features: desired feature bits
3178 * Returns fixed-up features bits
3180 static netdev_features_t iavf_fix_features(struct net_device *netdev,
3181 netdev_features_t features)
3183 struct iavf_adapter *adapter = netdev_priv(netdev);
3185 if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3186 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3187 NETIF_F_HW_VLAN_CTAG_RX |
3188 NETIF_F_HW_VLAN_CTAG_FILTER);
3193 static const struct net_device_ops iavf_netdev_ops = {
3194 .ndo_open = iavf_open,
3195 .ndo_stop = iavf_close,
3196 .ndo_start_xmit = iavf_xmit_frame,
3197 .ndo_set_rx_mode = iavf_set_rx_mode,
3198 .ndo_validate_addr = eth_validate_addr,
3199 .ndo_set_mac_address = iavf_set_mac,
3200 .ndo_change_mtu = iavf_change_mtu,
3201 .ndo_tx_timeout = iavf_tx_timeout,
3202 .ndo_vlan_rx_add_vid = iavf_vlan_rx_add_vid,
3203 .ndo_vlan_rx_kill_vid = iavf_vlan_rx_kill_vid,
3204 .ndo_features_check = iavf_features_check,
3205 .ndo_fix_features = iavf_fix_features,
3206 .ndo_set_features = iavf_set_features,
3207 .ndo_setup_tc = iavf_setup_tc,
3211 * iavf_check_reset_complete - check that VF reset is complete
3212 * @hw: pointer to hw struct
3214 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3216 static int iavf_check_reset_complete(struct iavf_hw *hw)
3221 for (i = 0; i < 100; i++) {
3222 rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
3223 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3224 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3225 (rstat == VIRTCHNL_VFR_COMPLETED))
3227 usleep_range(10, 20);
3233 * iavf_process_config - Process the config information we got from the PF
3234 * @adapter: board private structure
3236 * Verify that we have a valid config struct, and set up our netdev features
3237 * and our VSI struct.
3239 int iavf_process_config(struct iavf_adapter *adapter)
3241 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3242 int i, num_req_queues = adapter->num_req_queues;
3243 struct net_device *netdev = adapter->netdev;
3244 struct iavf_vsi *vsi = &adapter->vsi;
3245 netdev_features_t hw_enc_features;
3246 netdev_features_t hw_features;
3248 /* got VF config message back from PF, now we can parse it */
3249 for (i = 0; i < vfres->num_vsis; i++) {
3250 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3251 adapter->vsi_res = &vfres->vsi_res[i];
3253 if (!adapter->vsi_res) {
3254 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3258 if (num_req_queues &&
3259 num_req_queues != adapter->vsi_res->num_queue_pairs) {
3260 /* Problem. The PF gave us fewer queues than what we had
3261 * negotiated in our request. Need a reset to see if we can't
3262 * get back to a working state.
3264 dev_err(&adapter->pdev->dev,
3265 "Requested %d queues, but PF only gave us %d.\n",
3267 adapter->vsi_res->num_queue_pairs);
3268 adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
3269 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3270 iavf_schedule_reset(adapter);
3273 adapter->num_req_queues = 0;
3275 hw_enc_features = NETIF_F_SG |
3279 NETIF_F_SOFT_FEATURES |
3288 /* advertise to stack only if offloads for encapsulated packets is
3291 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3292 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
3294 NETIF_F_GSO_GRE_CSUM |
3295 NETIF_F_GSO_IPXIP4 |
3296 NETIF_F_GSO_IPXIP6 |
3297 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3298 NETIF_F_GSO_PARTIAL |
3301 if (!(vfres->vf_cap_flags &
3302 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3303 netdev->gso_partial_features |=
3304 NETIF_F_GSO_UDP_TUNNEL_CSUM;
3306 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3307 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3308 netdev->hw_enc_features |= hw_enc_features;
3310 /* record features VLANs can make use of */
3311 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3313 /* Write features and hw_features separately to avoid polluting
3314 * with, or dropping, features that are set when we registered.
3316 hw_features = hw_enc_features;
3318 /* Enable VLAN features if supported */
3319 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3320 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3321 NETIF_F_HW_VLAN_CTAG_RX);
3322 /* Enable cloud filter if ADQ is supported */
3323 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3324 hw_features |= NETIF_F_HW_TC;
3326 netdev->hw_features |= hw_features;
3328 netdev->features |= hw_features;
3330 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3331 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3333 netdev->priv_flags |= IFF_UNICAST_FLT;
3335 /* Do not turn on offloads when they are requested to be turned off.
3336 * TSO needs minimum 576 bytes to work correctly.
3338 if (netdev->wanted_features) {
3339 if (!(netdev->wanted_features & NETIF_F_TSO) ||
3341 netdev->features &= ~NETIF_F_TSO;
3342 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
3344 netdev->features &= ~NETIF_F_TSO6;
3345 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
3346 netdev->features &= ~NETIF_F_TSO_ECN;
3347 if (!(netdev->wanted_features & NETIF_F_GRO))
3348 netdev->features &= ~NETIF_F_GRO;
3349 if (!(netdev->wanted_features & NETIF_F_GSO))
3350 netdev->features &= ~NETIF_F_GSO;
3353 adapter->vsi.id = adapter->vsi_res->vsi_id;
3355 adapter->vsi.back = adapter;
3356 adapter->vsi.base_vector = 1;
3357 adapter->vsi.work_limit = IAVF_DEFAULT_IRQ_WORK;
3358 vsi->netdev = adapter->netdev;
3359 vsi->qs_handle = adapter->vsi_res->qset_handle;
3360 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3361 adapter->rss_key_size = vfres->rss_key_size;
3362 adapter->rss_lut_size = vfres->rss_lut_size;
3364 adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
3365 adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
3372 * iavf_init_task - worker thread to perform delayed initialization
3373 * @work: pointer to work_struct containing our data
3375 * This task completes the work that was begun in probe. Due to the nature
3376 * of VF-PF communications, we may need to wait tens of milliseconds to get
3377 * responses back from the PF. Rather than busy-wait in probe and bog down the
3378 * whole system, we'll do it in a task so we can sleep.
3379 * This task only runs during driver init. Once we've established
3380 * communications with the PF driver and set up our netdev, the watchdog
3383 static void iavf_init_task(struct work_struct *work)
3385 struct iavf_adapter *adapter = container_of(work,
3386 struct iavf_adapter,
3388 struct net_device *netdev = adapter->netdev;
3389 struct iavf_hw *hw = &adapter->hw;
3390 struct pci_dev *pdev = adapter->pdev;
3393 switch (adapter->state) {
3394 case __IAVF_STARTUP:
3395 /* driver loaded, probe complete */
3396 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
3397 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3398 err = iavf_set_mac_type(hw);
3400 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
3404 err = iavf_check_reset_complete(hw);
3406 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
3410 hw->aq.num_arq_entries = IAVF_AQ_LEN;
3411 hw->aq.num_asq_entries = IAVF_AQ_LEN;
3412 hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
3413 hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
3415 err = iavf_init_adminq(hw);
3417 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
3421 err = iavf_send_api_ver(adapter);
3423 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
3424 iavf_shutdown_adminq(hw);
3427 adapter->state = __IAVF_INIT_VERSION_CHECK;
3429 case __IAVF_INIT_VERSION_CHECK:
3430 if (!iavf_asq_done(hw)) {
3431 dev_err(&pdev->dev, "Admin queue command never completed\n");
3432 iavf_shutdown_adminq(hw);
3433 adapter->state = __IAVF_STARTUP;
3437 /* aq msg sent, awaiting reply */
3438 err = iavf_verify_api_ver(adapter);
3440 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
3441 err = iavf_send_api_ver(adapter);
3443 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
3444 adapter->pf_version.major,
3445 adapter->pf_version.minor,
3446 VIRTCHNL_VERSION_MAJOR,
3447 VIRTCHNL_VERSION_MINOR);
3450 err = iavf_send_vf_config_msg(adapter);
3452 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
3456 adapter->state = __IAVF_INIT_GET_RESOURCES;
3458 case __IAVF_INIT_GET_RESOURCES:
3459 /* aq msg sent, awaiting reply */
3460 if (!adapter->vf_res) {
3461 bufsz = sizeof(struct virtchnl_vf_resource) +
3463 sizeof(struct virtchnl_vsi_resource));
3464 adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
3465 if (!adapter->vf_res)
3468 err = iavf_get_vf_config(adapter);
3469 if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
3470 err = iavf_send_vf_config_msg(adapter);
3472 } else if (err == I40E_ERR_PARAM) {
3473 /* We only get ERR_PARAM if the device is in a very bad
3474 * state or if we've been disabled for previous bad
3475 * behavior. Either way, we're done now.
3477 iavf_shutdown_adminq(hw);
3478 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
3482 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
3486 adapter->state = __IAVF_INIT_SW;
3492 if (iavf_process_config(adapter))
3494 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3496 adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
3498 netdev->netdev_ops = &iavf_netdev_ops;
3499 iavf_set_ethtool_ops(netdev);
3500 netdev->watchdog_timeo = 5 * HZ;
3502 /* MTU range: 68 - 9710 */
3503 netdev->min_mtu = ETH_MIN_MTU;
3504 netdev->max_mtu = IAVF_MAX_RXBUFFER - IAVF_PACKET_HDR_PAD;
3506 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
3507 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
3508 adapter->hw.mac.addr);
3509 eth_hw_addr_random(netdev);
3510 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
3512 adapter->flags |= IAVF_FLAG_ADDR_SET_BY_PF;
3513 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
3514 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
3517 timer_setup(&adapter->watchdog_timer, iavf_watchdog_timer, 0);
3518 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3520 adapter->tx_desc_count = IAVF_DEFAULT_TXD;
3521 adapter->rx_desc_count = IAVF_DEFAULT_RXD;
3522 err = iavf_init_interrupt_scheme(adapter);
3525 iavf_map_rings_to_vectors(adapter);
3526 if (adapter->vf_res->vf_cap_flags &
3527 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
3528 adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
3530 err = iavf_request_misc_irq(adapter);
3534 netif_carrier_off(netdev);
3535 adapter->link_up = false;
3537 if (!adapter->netdev_registered) {
3538 err = register_netdev(netdev);
3543 adapter->netdev_registered = true;
3545 netif_tx_stop_all_queues(netdev);
3546 if (CLIENT_ALLOWED(adapter)) {
3547 err = iavf_lan_add_device(adapter);
3549 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
3553 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
3554 if (netdev->features & NETIF_F_GRO)
3555 dev_info(&pdev->dev, "GRO is enabled\n");
3557 adapter->state = __IAVF_DOWN;
3558 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3559 iavf_misc_irq_enable(adapter);
3560 wake_up(&adapter->down_waitqueue);
3562 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
3563 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
3564 if (!adapter->rss_key || !adapter->rss_lut)
3567 if (RSS_AQ(adapter)) {
3568 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
3569 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
3571 iavf_init_rss(adapter);
3575 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
3578 iavf_free_rss(adapter);
3580 iavf_free_misc_irq(adapter);
3582 iavf_reset_interrupt_capability(adapter);
3584 kfree(adapter->vf_res);
3585 adapter->vf_res = NULL;
3587 /* Things went into the weeds, so try again later */
3588 if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
3589 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
3590 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
3591 iavf_shutdown_adminq(hw);
3592 adapter->state = __IAVF_STARTUP;
3593 schedule_delayed_work(&adapter->init_task, HZ * 5);
3596 schedule_delayed_work(&adapter->init_task, HZ);
3600 * iavf_shutdown - Shutdown the device in preparation for a reboot
3601 * @pdev: pci device structure
3603 static void iavf_shutdown(struct pci_dev *pdev)
3605 struct net_device *netdev = pci_get_drvdata(pdev);
3606 struct iavf_adapter *adapter = netdev_priv(netdev);
3608 netif_device_detach(netdev);
3610 if (netif_running(netdev))
3613 /* Prevent the watchdog from running. */
3614 adapter->state = __IAVF_REMOVE;
3615 adapter->aq_required = 0;
3618 pci_save_state(pdev);
3621 pci_disable_device(pdev);
3625 * iavf_probe - Device Initialization Routine
3626 * @pdev: PCI device information struct
3627 * @ent: entry in iavf_pci_tbl
3629 * Returns 0 on success, negative on failure
3631 * iavf_probe initializes an adapter identified by a pci_dev structure.
3632 * The OS initialization, configuring of the adapter private structure,
3633 * and a hardware reset occur.
3635 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3637 struct net_device *netdev;
3638 struct iavf_adapter *adapter = NULL;
3639 struct iavf_hw *hw = NULL;
3642 err = pci_enable_device(pdev);
3646 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3648 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3651 "DMA configuration failed: 0x%x\n", err);
3656 err = pci_request_regions(pdev, iavf_driver_name);
3659 "pci_request_regions failed 0x%x\n", err);
3663 pci_enable_pcie_error_reporting(pdev);
3665 pci_set_master(pdev);
3667 netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
3668 IAVF_MAX_REQ_QUEUES);
3671 goto err_alloc_etherdev;
3674 SET_NETDEV_DEV(netdev, &pdev->dev);
3676 pci_set_drvdata(pdev, netdev);
3677 adapter = netdev_priv(netdev);
3679 adapter->netdev = netdev;
3680 adapter->pdev = pdev;
3685 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3686 adapter->state = __IAVF_STARTUP;
3688 /* Call save state here because it relies on the adapter struct. */
3689 pci_save_state(pdev);
3691 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3692 pci_resource_len(pdev, 0));
3697 hw->vendor_id = pdev->vendor;
3698 hw->device_id = pdev->device;
3699 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3700 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3701 hw->subsystem_device_id = pdev->subsystem_device;
3702 hw->bus.device = PCI_SLOT(pdev->devfn);
3703 hw->bus.func = PCI_FUNC(pdev->devfn);
3704 hw->bus.bus_id = pdev->bus->number;
3706 /* set up the locks for the AQ, do this only once in probe
3707 * and destroy them only once in remove
3709 mutex_init(&hw->aq.asq_mutex);
3710 mutex_init(&hw->aq.arq_mutex);
3712 spin_lock_init(&adapter->mac_vlan_list_lock);
3713 spin_lock_init(&adapter->cloud_filter_list_lock);
3715 INIT_LIST_HEAD(&adapter->mac_filter_list);
3716 INIT_LIST_HEAD(&adapter->vlan_filter_list);
3717 INIT_LIST_HEAD(&adapter->cloud_filter_list);
3719 INIT_WORK(&adapter->reset_task, iavf_reset_task);
3720 INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
3721 INIT_WORK(&adapter->watchdog_task, iavf_watchdog_task);
3722 INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
3723 INIT_DELAYED_WORK(&adapter->init_task, iavf_init_task);
3724 schedule_delayed_work(&adapter->init_task,
3725 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3727 /* Setup the wait queue for indicating transition to down status */
3728 init_waitqueue_head(&adapter->down_waitqueue);
3733 free_netdev(netdev);
3735 pci_release_regions(pdev);
3738 pci_disable_device(pdev);
3744 * iavf_suspend - Power management suspend routine
3745 * @pdev: PCI device information struct
3748 * Called when the system (VM) is entering sleep/suspend.
3750 static int iavf_suspend(struct pci_dev *pdev, pm_message_t state)
3752 struct net_device *netdev = pci_get_drvdata(pdev);
3753 struct iavf_adapter *adapter = netdev_priv(netdev);
3756 netif_device_detach(netdev);
3758 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3759 &adapter->crit_section))
3760 usleep_range(500, 1000);
3762 if (netif_running(netdev)) {
3767 iavf_free_misc_irq(adapter);
3768 iavf_reset_interrupt_capability(adapter);
3770 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3772 retval = pci_save_state(pdev);
3776 pci_disable_device(pdev);
3782 * iavf_resume - Power management resume routine
3783 * @pdev: PCI device information struct
3785 * Called when the system (VM) is resumed from sleep/suspend.
3787 static int iavf_resume(struct pci_dev *pdev)
3789 struct iavf_adapter *adapter = pci_get_drvdata(pdev);
3790 struct net_device *netdev = adapter->netdev;
3793 pci_set_power_state(pdev, PCI_D0);
3794 pci_restore_state(pdev);
3795 /* pci_restore_state clears dev->state_saved so call
3796 * pci_save_state to restore it.
3798 pci_save_state(pdev);
3800 err = pci_enable_device_mem(pdev);
3802 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3805 pci_set_master(pdev);
3808 err = iavf_set_interrupt_capability(adapter);
3811 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3814 err = iavf_request_misc_irq(adapter);
3817 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3821 schedule_work(&adapter->reset_task);
3823 netif_device_attach(netdev);
3828 #endif /* CONFIG_PM */
3830 * iavf_remove - Device Removal Routine
3831 * @pdev: PCI device information struct
3833 * iavf_remove is called by the PCI subsystem to alert the driver
3834 * that it should release a PCI device. The could be caused by a
3835 * Hot-Plug event, or because the driver is going to be removed from
3838 static void iavf_remove(struct pci_dev *pdev)
3840 struct net_device *netdev = pci_get_drvdata(pdev);
3841 struct iavf_adapter *adapter = netdev_priv(netdev);
3842 struct iavf_vlan_filter *vlf, *vlftmp;
3843 struct iavf_mac_filter *f, *ftmp;
3844 struct iavf_cloud_filter *cf, *cftmp;
3845 struct iavf_hw *hw = &adapter->hw;
3847 /* Indicate we are in remove and not to run reset_task */
3848 set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section);
3849 cancel_delayed_work_sync(&adapter->init_task);
3850 cancel_work_sync(&adapter->reset_task);
3851 cancel_delayed_work_sync(&adapter->client_task);
3852 if (adapter->netdev_registered) {
3853 unregister_netdev(netdev);
3854 adapter->netdev_registered = false;
3856 if (CLIENT_ALLOWED(adapter)) {
3857 err = iavf_lan_del_device(adapter);
3859 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3863 /* Shut down all the garbage mashers on the detention level */
3864 adapter->state = __IAVF_REMOVE;
3865 adapter->aq_required = 0;
3866 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3867 iavf_request_reset(adapter);
3869 /* If the FW isn't responding, kick it once, but only once. */
3870 if (!iavf_asq_done(hw)) {
3871 iavf_request_reset(adapter);
3874 iavf_free_all_tx_resources(adapter);
3875 iavf_free_all_rx_resources(adapter);
3876 iavf_misc_irq_disable(adapter);
3877 iavf_free_misc_irq(adapter);
3878 iavf_reset_interrupt_capability(adapter);
3879 iavf_free_q_vectors(adapter);
3881 if (adapter->watchdog_timer.function)
3882 del_timer_sync(&adapter->watchdog_timer);
3884 cancel_work_sync(&adapter->adminq_task);
3886 iavf_free_rss(adapter);
3888 if (hw->aq.asq.count)
3889 iavf_shutdown_adminq(hw);
3891 /* destroy the locks only once, here */
3892 mutex_destroy(&hw->aq.arq_mutex);
3893 mutex_destroy(&hw->aq.asq_mutex);
3895 iounmap(hw->hw_addr);
3896 pci_release_regions(pdev);
3897 iavf_free_all_tx_resources(adapter);
3898 iavf_free_all_rx_resources(adapter);
3899 iavf_free_queues(adapter);
3900 kfree(adapter->vf_res);
3901 spin_lock_bh(&adapter->mac_vlan_list_lock);
3902 /* If we got removed before an up/down sequence, we've got a filter
3903 * hanging out there that we need to get rid of.
3905 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3909 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3911 list_del(&vlf->list);
3915 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3917 spin_lock_bh(&adapter->cloud_filter_list_lock);
3918 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3919 list_del(&cf->list);
3922 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3924 free_netdev(netdev);
3926 pci_disable_pcie_error_reporting(pdev);
3928 pci_disable_device(pdev);
3931 static struct pci_driver iavf_driver = {
3932 .name = iavf_driver_name,
3933 .id_table = iavf_pci_tbl,
3934 .probe = iavf_probe,
3935 .remove = iavf_remove,
3937 .suspend = iavf_suspend,
3938 .resume = iavf_resume,
3940 .shutdown = iavf_shutdown,
3944 * iavf_init_module - Driver Registration Routine
3946 * iavf_init_module is the first routine called when the driver is
3947 * loaded. All it does is register with the PCI subsystem.
3949 static int __init iavf_init_module(void)
3953 pr_info("iavf: %s - version %s\n", iavf_driver_string,
3954 iavf_driver_version);
3956 pr_info("%s\n", iavf_copyright);
3958 iavf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3961 pr_err("%s: Failed to create workqueue\n", iavf_driver_name);
3964 ret = pci_register_driver(&iavf_driver);
3968 module_init(iavf_init_module);
3971 * iavf_exit_module - Driver Exit Cleanup Routine
3973 * iavf_exit_module is called just before the driver is removed
3976 static void __exit iavf_exit_module(void)
3978 pci_unregister_driver(&iavf_driver);
3979 destroy_workqueue(iavf_wq);
3982 module_exit(iavf_exit_module);