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");
55 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
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 enum 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 IAVF_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 IAVF_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 enum iavf_status iavf_free_dma_mem_d(struct iavf_hw *hw,
93 struct iavf_dma_mem *mem)
95 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
98 return IAVF_ERR_PARAM;
99 dma_free_coherent(&adapter->pdev->dev, mem->size,
100 mem->va, (dma_addr_t)mem->pa);
105 * iavf_allocate_virt_mem_d - OS specific memory alloc for shared code
106 * @hw: pointer to the HW structure
107 * @mem: ptr to mem struct to fill out
108 * @size: size of memory requested
110 enum iavf_status iavf_allocate_virt_mem_d(struct iavf_hw *hw,
111 struct iavf_virt_mem *mem, u32 size)
114 return IAVF_ERR_PARAM;
117 mem->va = kzalloc(size, GFP_KERNEL);
122 return IAVF_ERR_NO_MEMORY;
126 * iavf_free_virt_mem_d - OS specific memory free for shared code
127 * @hw: pointer to the HW structure
128 * @mem: ptr to mem struct to free
130 enum iavf_status iavf_free_virt_mem_d(struct iavf_hw *hw,
131 struct iavf_virt_mem *mem)
134 return IAVF_ERR_PARAM;
136 /* it's ok to kfree a NULL pointer */
143 * iavf_debug_d - OS dependent version of debug printing
144 * @hw: pointer to the HW structure
145 * @mask: debug level mask
146 * @fmt_str: printf-type format description
148 void iavf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
153 if (!(mask & ((struct iavf_hw *)hw)->debug_mask))
156 va_start(argptr, fmt_str);
157 vsnprintf(buf, sizeof(buf), fmt_str, argptr);
160 /* the debug string is already formatted with a newline */
165 * iavf_schedule_reset - Set the flags and schedule a reset event
166 * @adapter: board private structure
168 void iavf_schedule_reset(struct iavf_adapter *adapter)
170 if (!(adapter->flags &
171 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
172 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
173 schedule_work(&adapter->reset_task);
178 * iavf_tx_timeout - Respond to a Tx Hang
179 * @netdev: network interface device structure
181 static void iavf_tx_timeout(struct net_device *netdev)
183 struct iavf_adapter *adapter = netdev_priv(netdev);
185 adapter->tx_timeout_count++;
186 iavf_schedule_reset(adapter);
190 * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
191 * @adapter: board private structure
193 static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
195 struct iavf_hw *hw = &adapter->hw;
197 if (!adapter->msix_entries)
200 wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
204 synchronize_irq(adapter->msix_entries[0].vector);
208 * iavf_misc_irq_enable - Enable default interrupt generation settings
209 * @adapter: board private structure
211 static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
213 struct iavf_hw *hw = &adapter->hw;
215 wr32(hw, IAVF_VFINT_DYN_CTL01, IAVF_VFINT_DYN_CTL01_INTENA_MASK |
216 IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
217 wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
223 * iavf_irq_disable - Mask off interrupt generation on the NIC
224 * @adapter: board private structure
226 static void iavf_irq_disable(struct iavf_adapter *adapter)
229 struct iavf_hw *hw = &adapter->hw;
231 if (!adapter->msix_entries)
234 for (i = 1; i < adapter->num_msix_vectors; i++) {
235 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1), 0);
236 synchronize_irq(adapter->msix_entries[i].vector);
242 * iavf_irq_enable_queues - Enable interrupt for specified queues
243 * @adapter: board private structure
244 * @mask: bitmap of queues to enable
246 void iavf_irq_enable_queues(struct iavf_adapter *adapter, u32 mask)
248 struct iavf_hw *hw = &adapter->hw;
251 for (i = 1; i < adapter->num_msix_vectors; i++) {
252 if (mask & BIT(i - 1)) {
253 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1),
254 IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
255 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
261 * iavf_irq_enable - Enable default interrupt generation settings
262 * @adapter: board private structure
263 * @flush: boolean value whether to run rd32()
265 void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
267 struct iavf_hw *hw = &adapter->hw;
269 iavf_misc_irq_enable(adapter);
270 iavf_irq_enable_queues(adapter, ~0);
277 * iavf_msix_aq - Interrupt handler for vector 0
278 * @irq: interrupt number
279 * @data: pointer to netdev
281 static irqreturn_t iavf_msix_aq(int irq, void *data)
283 struct net_device *netdev = data;
284 struct iavf_adapter *adapter = netdev_priv(netdev);
285 struct iavf_hw *hw = &adapter->hw;
287 /* handle non-queue interrupts, these reads clear the registers */
288 rd32(hw, IAVF_VFINT_ICR01);
289 rd32(hw, IAVF_VFINT_ICR0_ENA1);
291 /* schedule work on the private workqueue */
292 schedule_work(&adapter->adminq_task);
298 * iavf_msix_clean_rings - MSIX mode Interrupt Handler
299 * @irq: interrupt number
300 * @data: pointer to a q_vector
302 static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
304 struct iavf_q_vector *q_vector = data;
306 if (!q_vector->tx.ring && !q_vector->rx.ring)
309 napi_schedule_irqoff(&q_vector->napi);
315 * iavf_map_vector_to_rxq - associate irqs with rx queues
316 * @adapter: board private structure
317 * @v_idx: interrupt number
318 * @r_idx: queue number
321 iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
323 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
324 struct iavf_ring *rx_ring = &adapter->rx_rings[r_idx];
325 struct iavf_hw *hw = &adapter->hw;
327 rx_ring->q_vector = q_vector;
328 rx_ring->next = q_vector->rx.ring;
329 rx_ring->vsi = &adapter->vsi;
330 q_vector->rx.ring = rx_ring;
331 q_vector->rx.count++;
332 q_vector->rx.next_update = jiffies + 1;
333 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
334 q_vector->ring_mask |= BIT(r_idx);
335 wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, q_vector->reg_idx),
336 q_vector->rx.current_itr);
337 q_vector->rx.current_itr = q_vector->rx.target_itr;
341 * iavf_map_vector_to_txq - associate irqs with tx queues
342 * @adapter: board private structure
343 * @v_idx: interrupt number
344 * @t_idx: queue number
347 iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
349 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
350 struct iavf_ring *tx_ring = &adapter->tx_rings[t_idx];
351 struct iavf_hw *hw = &adapter->hw;
353 tx_ring->q_vector = q_vector;
354 tx_ring->next = q_vector->tx.ring;
355 tx_ring->vsi = &adapter->vsi;
356 q_vector->tx.ring = tx_ring;
357 q_vector->tx.count++;
358 q_vector->tx.next_update = jiffies + 1;
359 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
360 q_vector->num_ringpairs++;
361 wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, q_vector->reg_idx),
362 q_vector->tx.target_itr);
363 q_vector->tx.current_itr = q_vector->tx.target_itr;
367 * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
368 * @adapter: board private structure to initialize
370 * This function maps descriptor rings to the queue-specific vectors
371 * we were allotted through the MSI-X enabling code. Ideally, we'd have
372 * one vector per ring/queue, but on a constrained vector budget, we
373 * group the rings as "efficiently" as possible. You would add new
374 * mapping configurations in here.
376 static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
378 int rings_remaining = adapter->num_active_queues;
379 int ridx = 0, vidx = 0;
382 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
384 for (; ridx < rings_remaining; ridx++) {
385 iavf_map_vector_to_rxq(adapter, vidx, ridx);
386 iavf_map_vector_to_txq(adapter, vidx, ridx);
388 /* In the case where we have more queues than vectors, continue
389 * round-robin on vectors until all queues are mapped.
391 if (++vidx >= q_vectors)
395 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
399 * iavf_irq_affinity_notify - Callback for affinity changes
400 * @notify: context as to what irq was changed
401 * @mask: the new affinity mask
403 * This is a callback function used by the irq_set_affinity_notifier function
404 * so that we may register to receive changes to the irq affinity masks.
406 static void iavf_irq_affinity_notify(struct irq_affinity_notify *notify,
407 const cpumask_t *mask)
409 struct iavf_q_vector *q_vector =
410 container_of(notify, struct iavf_q_vector, affinity_notify);
412 cpumask_copy(&q_vector->affinity_mask, mask);
416 * iavf_irq_affinity_release - Callback for affinity notifier release
417 * @ref: internal core kernel usage
419 * This is a callback function used by the irq_set_affinity_notifier function
420 * to inform the current notification subscriber that they will no longer
421 * receive notifications.
423 static void iavf_irq_affinity_release(struct kref *ref) {}
426 * iavf_request_traffic_irqs - Initialize MSI-X interrupts
427 * @adapter: board private structure
428 * @basename: device basename
430 * Allocates MSI-X vectors for tx and rx handling, and requests
431 * interrupts from the kernel.
434 iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
436 unsigned int vector, q_vectors;
437 unsigned int rx_int_idx = 0, tx_int_idx = 0;
441 iavf_irq_disable(adapter);
442 /* Decrement for Other and TCP Timer vectors */
443 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
445 for (vector = 0; vector < q_vectors; vector++) {
446 struct iavf_q_vector *q_vector = &adapter->q_vectors[vector];
448 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
450 if (q_vector->tx.ring && q_vector->rx.ring) {
451 snprintf(q_vector->name, sizeof(q_vector->name),
452 "iavf-%s-TxRx-%d", basename, rx_int_idx++);
454 } else if (q_vector->rx.ring) {
455 snprintf(q_vector->name, sizeof(q_vector->name),
456 "iavf-%s-rx-%d", basename, rx_int_idx++);
457 } else if (q_vector->tx.ring) {
458 snprintf(q_vector->name, sizeof(q_vector->name),
459 "iavf-%s-tx-%d", basename, tx_int_idx++);
461 /* skip this unused q_vector */
464 err = request_irq(irq_num,
465 iavf_msix_clean_rings,
470 dev_info(&adapter->pdev->dev,
471 "Request_irq failed, error: %d\n", err);
472 goto free_queue_irqs;
474 /* register for affinity change notifications */
475 q_vector->affinity_notify.notify = iavf_irq_affinity_notify;
476 q_vector->affinity_notify.release =
477 iavf_irq_affinity_release;
478 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
479 /* Spread the IRQ affinity hints across online CPUs. Note that
480 * get_cpu_mask returns a mask with a permanent lifetime so
481 * it's safe to use as a hint for irq_set_affinity_hint.
483 cpu = cpumask_local_spread(q_vector->v_idx, -1);
484 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
492 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
493 irq_set_affinity_notifier(irq_num, NULL);
494 irq_set_affinity_hint(irq_num, NULL);
495 free_irq(irq_num, &adapter->q_vectors[vector]);
501 * iavf_request_misc_irq - Initialize MSI-X interrupts
502 * @adapter: board private structure
504 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
505 * vector is only for the admin queue, and stays active even when the netdev
508 static int iavf_request_misc_irq(struct iavf_adapter *adapter)
510 struct net_device *netdev = adapter->netdev;
513 snprintf(adapter->misc_vector_name,
514 sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
515 dev_name(&adapter->pdev->dev));
516 err = request_irq(adapter->msix_entries[0].vector,
518 adapter->misc_vector_name, netdev);
520 dev_err(&adapter->pdev->dev,
521 "request_irq for %s failed: %d\n",
522 adapter->misc_vector_name, err);
523 free_irq(adapter->msix_entries[0].vector, netdev);
529 * iavf_free_traffic_irqs - Free MSI-X interrupts
530 * @adapter: board private structure
532 * Frees all MSI-X vectors other than 0.
534 static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
536 int vector, irq_num, q_vectors;
538 if (!adapter->msix_entries)
541 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
543 for (vector = 0; vector < q_vectors; vector++) {
544 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
545 irq_set_affinity_notifier(irq_num, NULL);
546 irq_set_affinity_hint(irq_num, NULL);
547 free_irq(irq_num, &adapter->q_vectors[vector]);
552 * iavf_free_misc_irq - Free MSI-X miscellaneous vector
553 * @adapter: board private structure
555 * Frees MSI-X vector 0.
557 static void iavf_free_misc_irq(struct iavf_adapter *adapter)
559 struct net_device *netdev = adapter->netdev;
561 if (!adapter->msix_entries)
564 free_irq(adapter->msix_entries[0].vector, netdev);
568 * iavf_configure_tx - Configure Transmit Unit after Reset
569 * @adapter: board private structure
571 * Configure the Tx unit of the MAC after a reset.
573 static void iavf_configure_tx(struct iavf_adapter *adapter)
575 struct iavf_hw *hw = &adapter->hw;
578 for (i = 0; i < adapter->num_active_queues; i++)
579 adapter->tx_rings[i].tail = hw->hw_addr + IAVF_QTX_TAIL1(i);
583 * iavf_configure_rx - Configure Receive Unit after Reset
584 * @adapter: board private structure
586 * Configure the Rx unit of the MAC after a reset.
588 static void iavf_configure_rx(struct iavf_adapter *adapter)
590 unsigned int rx_buf_len = IAVF_RXBUFFER_2048;
591 struct iavf_hw *hw = &adapter->hw;
594 /* Legacy Rx will always default to a 2048 buffer size. */
595 #if (PAGE_SIZE < 8192)
596 if (!(adapter->flags & IAVF_FLAG_LEGACY_RX)) {
597 struct net_device *netdev = adapter->netdev;
599 /* For jumbo frames on systems with 4K pages we have to use
600 * an order 1 page, so we might as well increase the size
601 * of our Rx buffer to make better use of the available space
603 rx_buf_len = IAVF_RXBUFFER_3072;
605 /* We use a 1536 buffer size for configurations with
606 * standard Ethernet mtu. On x86 this gives us enough room
607 * for shared info and 192 bytes of padding.
609 if (!IAVF_2K_TOO_SMALL_WITH_PADDING &&
610 (netdev->mtu <= ETH_DATA_LEN))
611 rx_buf_len = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
615 for (i = 0; i < adapter->num_active_queues; i++) {
616 adapter->rx_rings[i].tail = hw->hw_addr + IAVF_QRX_TAIL1(i);
617 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
619 if (adapter->flags & IAVF_FLAG_LEGACY_RX)
620 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
622 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
627 * iavf_find_vlan - Search filter list for specific vlan filter
628 * @adapter: board private structure
631 * Returns ptr to the filter object or NULL. Must be called while holding the
632 * mac_vlan_list_lock.
635 iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter, u16 vlan)
637 struct iavf_vlan_filter *f;
639 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
647 * iavf_add_vlan - Add a vlan filter to the list
648 * @adapter: board private structure
651 * Returns ptr to the filter object or NULL when no memory available.
654 iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter, u16 vlan)
656 struct iavf_vlan_filter *f = NULL;
658 spin_lock_bh(&adapter->mac_vlan_list_lock);
660 f = iavf_find_vlan(adapter, vlan);
662 f = kzalloc(sizeof(*f), GFP_KERNEL);
668 INIT_LIST_HEAD(&f->list);
669 list_add(&f->list, &adapter->vlan_filter_list);
671 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
675 spin_unlock_bh(&adapter->mac_vlan_list_lock);
680 * iavf_del_vlan - Remove a vlan filter from the list
681 * @adapter: board private structure
684 static void iavf_del_vlan(struct iavf_adapter *adapter, u16 vlan)
686 struct iavf_vlan_filter *f;
688 spin_lock_bh(&adapter->mac_vlan_list_lock);
690 f = iavf_find_vlan(adapter, vlan);
693 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
696 spin_unlock_bh(&adapter->mac_vlan_list_lock);
700 * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
701 * @netdev: network device struct
702 * @proto: unused protocol data
705 static int iavf_vlan_rx_add_vid(struct net_device *netdev,
706 __always_unused __be16 proto, u16 vid)
708 struct iavf_adapter *adapter = netdev_priv(netdev);
710 if (!VLAN_ALLOWED(adapter))
712 if (iavf_add_vlan(adapter, vid) == NULL)
718 * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
719 * @netdev: network device struct
720 * @proto: unused protocol data
723 static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
724 __always_unused __be16 proto, u16 vid)
726 struct iavf_adapter *adapter = netdev_priv(netdev);
728 if (VLAN_ALLOWED(adapter)) {
729 iavf_del_vlan(adapter, vid);
736 * iavf_find_filter - Search filter list for specific mac filter
737 * @adapter: board private structure
738 * @macaddr: the MAC address
740 * Returns ptr to the filter object or NULL. Must be called while holding the
741 * mac_vlan_list_lock.
744 iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
747 struct iavf_mac_filter *f;
752 list_for_each_entry(f, &adapter->mac_filter_list, list) {
753 if (ether_addr_equal(macaddr, f->macaddr))
760 * iavf_add_filter - Add a mac filter to the filter list
761 * @adapter: board private structure
762 * @macaddr: the MAC address
764 * Returns ptr to the filter object or NULL when no memory available.
767 iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
770 struct iavf_mac_filter *f;
775 f = iavf_find_filter(adapter, macaddr);
777 f = kzalloc(sizeof(*f), GFP_ATOMIC);
781 ether_addr_copy(f->macaddr, macaddr);
783 list_add_tail(&f->list, &adapter->mac_filter_list);
785 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
794 * iavf_set_mac - NDO callback to set port mac address
795 * @netdev: network interface device structure
796 * @p: pointer to an address structure
798 * Returns 0 on success, negative on failure
800 static int iavf_set_mac(struct net_device *netdev, void *p)
802 struct iavf_adapter *adapter = netdev_priv(netdev);
803 struct iavf_hw *hw = &adapter->hw;
804 struct iavf_mac_filter *f;
805 struct sockaddr *addr = p;
807 if (!is_valid_ether_addr(addr->sa_data))
808 return -EADDRNOTAVAIL;
810 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
813 if (adapter->flags & IAVF_FLAG_ADDR_SET_BY_PF)
816 spin_lock_bh(&adapter->mac_vlan_list_lock);
818 f = iavf_find_filter(adapter, hw->mac.addr);
821 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
824 f = iavf_add_filter(adapter, addr->sa_data);
826 spin_unlock_bh(&adapter->mac_vlan_list_lock);
829 ether_addr_copy(hw->mac.addr, addr->sa_data);
830 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
833 return (f == NULL) ? -ENOMEM : 0;
837 * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
838 * @netdev: the netdevice
839 * @addr: address to add
841 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
842 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
844 static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
846 struct iavf_adapter *adapter = netdev_priv(netdev);
848 if (iavf_add_filter(adapter, addr))
855 * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
856 * @netdev: the netdevice
857 * @addr: address to add
859 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
860 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
862 static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
864 struct iavf_adapter *adapter = netdev_priv(netdev);
865 struct iavf_mac_filter *f;
867 /* Under some circumstances, we might receive a request to delete
868 * our own device address from our uc list. Because we store the
869 * device address in the VSI's MAC/VLAN filter list, we need to ignore
870 * such requests and not delete our device address from this list.
872 if (ether_addr_equal(addr, netdev->dev_addr))
875 f = iavf_find_filter(adapter, addr);
878 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
884 * iavf_set_rx_mode - NDO callback to set the netdev filters
885 * @netdev: network interface device structure
887 static void iavf_set_rx_mode(struct net_device *netdev)
889 struct iavf_adapter *adapter = netdev_priv(netdev);
891 spin_lock_bh(&adapter->mac_vlan_list_lock);
892 __dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
893 __dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
894 spin_unlock_bh(&adapter->mac_vlan_list_lock);
896 if (netdev->flags & IFF_PROMISC &&
897 !(adapter->flags & IAVF_FLAG_PROMISC_ON))
898 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_PROMISC;
899 else if (!(netdev->flags & IFF_PROMISC) &&
900 adapter->flags & IAVF_FLAG_PROMISC_ON)
901 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_PROMISC;
903 if (netdev->flags & IFF_ALLMULTI &&
904 !(adapter->flags & IAVF_FLAG_ALLMULTI_ON))
905 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_ALLMULTI;
906 else if (!(netdev->flags & IFF_ALLMULTI) &&
907 adapter->flags & IAVF_FLAG_ALLMULTI_ON)
908 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_ALLMULTI;
912 * iavf_napi_enable_all - enable NAPI on all queue vectors
913 * @adapter: board private structure
915 static void iavf_napi_enable_all(struct iavf_adapter *adapter)
918 struct iavf_q_vector *q_vector;
919 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
921 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
922 struct napi_struct *napi;
924 q_vector = &adapter->q_vectors[q_idx];
925 napi = &q_vector->napi;
931 * iavf_napi_disable_all - disable NAPI on all queue vectors
932 * @adapter: board private structure
934 static void iavf_napi_disable_all(struct iavf_adapter *adapter)
937 struct iavf_q_vector *q_vector;
938 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
940 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
941 q_vector = &adapter->q_vectors[q_idx];
942 napi_disable(&q_vector->napi);
947 * iavf_configure - set up transmit and receive data structures
948 * @adapter: board private structure
950 static void iavf_configure(struct iavf_adapter *adapter)
952 struct net_device *netdev = adapter->netdev;
955 iavf_set_rx_mode(netdev);
957 iavf_configure_tx(adapter);
958 iavf_configure_rx(adapter);
959 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
961 for (i = 0; i < adapter->num_active_queues; i++) {
962 struct iavf_ring *ring = &adapter->rx_rings[i];
964 iavf_alloc_rx_buffers(ring, IAVF_DESC_UNUSED(ring));
969 * iavf_up_complete - Finish the last steps of bringing up a connection
970 * @adapter: board private structure
972 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
974 static void iavf_up_complete(struct iavf_adapter *adapter)
976 adapter->state = __IAVF_RUNNING;
977 clear_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
979 iavf_napi_enable_all(adapter);
981 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_QUEUES;
982 if (CLIENT_ENABLED(adapter))
983 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_OPEN;
984 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
988 * iavf_down - Shutdown the connection processing
989 * @adapter: board private structure
991 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
993 void iavf_down(struct iavf_adapter *adapter)
995 struct net_device *netdev = adapter->netdev;
996 struct iavf_vlan_filter *vlf;
997 struct iavf_mac_filter *f;
998 struct iavf_cloud_filter *cf;
1000 if (adapter->state <= __IAVF_DOWN_PENDING)
1003 netif_carrier_off(netdev);
1004 netif_tx_disable(netdev);
1005 adapter->link_up = false;
1006 iavf_napi_disable_all(adapter);
1007 iavf_irq_disable(adapter);
1009 spin_lock_bh(&adapter->mac_vlan_list_lock);
1011 /* clear the sync flag on all filters */
1012 __dev_uc_unsync(adapter->netdev, NULL);
1013 __dev_mc_unsync(adapter->netdev, NULL);
1015 /* remove all MAC filters */
1016 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1020 /* remove all VLAN filters */
1021 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1025 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1027 /* remove all cloud filters */
1028 spin_lock_bh(&adapter->cloud_filter_list_lock);
1029 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1032 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1034 if (!(adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) &&
1035 adapter->state != __IAVF_RESETTING) {
1036 /* cancel any current operation */
1037 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1038 /* Schedule operations to close down the HW. Don't wait
1039 * here for this to complete. The watchdog is still running
1040 * and it will take care of this.
1042 adapter->aq_required = IAVF_FLAG_AQ_DEL_MAC_FILTER;
1043 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
1044 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1045 adapter->aq_required |= IAVF_FLAG_AQ_DISABLE_QUEUES;
1048 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
1052 * iavf_acquire_msix_vectors - Setup the MSIX capability
1053 * @adapter: board private structure
1054 * @vectors: number of vectors to request
1056 * Work with the OS to set up the MSIX vectors needed.
1058 * Returns 0 on success, negative on failure
1061 iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1063 int err, vector_threshold;
1065 /* We'll want at least 3 (vector_threshold):
1066 * 0) Other (Admin Queue and link, mostly)
1070 vector_threshold = MIN_MSIX_COUNT;
1072 /* The more we get, the more we will assign to Tx/Rx Cleanup
1073 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1074 * Right now, we simply care about how many we'll get; we'll
1075 * set them up later while requesting irq's.
1077 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1078 vector_threshold, vectors);
1080 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1081 kfree(adapter->msix_entries);
1082 adapter->msix_entries = NULL;
1086 /* Adjust for only the vectors we'll use, which is minimum
1087 * of max_msix_q_vectors + NONQ_VECS, or the number of
1088 * vectors we were allocated.
1090 adapter->num_msix_vectors = err;
1095 * iavf_free_queues - Free memory for all rings
1096 * @adapter: board private structure to initialize
1098 * Free all of the memory associated with queue pairs.
1100 static void iavf_free_queues(struct iavf_adapter *adapter)
1102 if (!adapter->vsi_res)
1104 adapter->num_active_queues = 0;
1105 kfree(adapter->tx_rings);
1106 adapter->tx_rings = NULL;
1107 kfree(adapter->rx_rings);
1108 adapter->rx_rings = NULL;
1112 * iavf_alloc_queues - Allocate memory for all rings
1113 * @adapter: board private structure to initialize
1115 * We allocate one ring per queue at run-time since we don't know the
1116 * number of queues at compile-time. The polling_netdev array is
1117 * intended for Multiqueue, but should work fine with a single queue.
1119 static int iavf_alloc_queues(struct iavf_adapter *adapter)
1121 int i, num_active_queues;
1123 /* If we're in reset reallocating queues we don't actually know yet for
1124 * certain the PF gave us the number of queues we asked for but we'll
1125 * assume it did. Once basic reset is finished we'll confirm once we
1126 * start negotiating config with PF.
1128 if (adapter->num_req_queues)
1129 num_active_queues = adapter->num_req_queues;
1130 else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1132 num_active_queues = adapter->ch_config.total_qps;
1134 num_active_queues = min_t(int,
1135 adapter->vsi_res->num_queue_pairs,
1136 (int)(num_online_cpus()));
1139 adapter->tx_rings = kcalloc(num_active_queues,
1140 sizeof(struct iavf_ring), GFP_KERNEL);
1141 if (!adapter->tx_rings)
1143 adapter->rx_rings = kcalloc(num_active_queues,
1144 sizeof(struct iavf_ring), GFP_KERNEL);
1145 if (!adapter->rx_rings)
1148 for (i = 0; i < num_active_queues; i++) {
1149 struct iavf_ring *tx_ring;
1150 struct iavf_ring *rx_ring;
1152 tx_ring = &adapter->tx_rings[i];
1154 tx_ring->queue_index = i;
1155 tx_ring->netdev = adapter->netdev;
1156 tx_ring->dev = &adapter->pdev->dev;
1157 tx_ring->count = adapter->tx_desc_count;
1158 tx_ring->itr_setting = IAVF_ITR_TX_DEF;
1159 if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1160 tx_ring->flags |= IAVF_TXR_FLAGS_WB_ON_ITR;
1162 rx_ring = &adapter->rx_rings[i];
1163 rx_ring->queue_index = i;
1164 rx_ring->netdev = adapter->netdev;
1165 rx_ring->dev = &adapter->pdev->dev;
1166 rx_ring->count = adapter->rx_desc_count;
1167 rx_ring->itr_setting = IAVF_ITR_RX_DEF;
1170 adapter->num_active_queues = num_active_queues;
1175 iavf_free_queues(adapter);
1180 * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1181 * @adapter: board private structure to initialize
1183 * Attempt to configure the interrupts using the best available
1184 * capabilities of the hardware and the kernel.
1186 static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1188 int vector, v_budget;
1192 if (!adapter->vsi_res) {
1196 pairs = adapter->num_active_queues;
1198 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1199 * us much good if we have more vectors than CPUs. However, we already
1200 * limit the total number of queues by the number of CPUs so we do not
1201 * need any further limiting here.
1203 v_budget = min_t(int, pairs + NONQ_VECS,
1204 (int)adapter->vf_res->max_vectors);
1206 adapter->msix_entries = kcalloc(v_budget,
1207 sizeof(struct msix_entry), GFP_KERNEL);
1208 if (!adapter->msix_entries) {
1213 for (vector = 0; vector < v_budget; vector++)
1214 adapter->msix_entries[vector].entry = vector;
1216 err = iavf_acquire_msix_vectors(adapter, v_budget);
1219 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1220 netif_set_real_num_tx_queues(adapter->netdev, pairs);
1225 * iavf_config_rss_aq - Configure RSS keys and lut by using AQ commands
1226 * @adapter: board private structure
1228 * Return 0 on success, negative on failure
1230 static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1232 struct iavf_aqc_get_set_rss_key_data *rss_key =
1233 (struct iavf_aqc_get_set_rss_key_data *)adapter->rss_key;
1234 struct iavf_hw *hw = &adapter->hw;
1237 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1238 /* bail because we already have a command pending */
1239 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1240 adapter->current_op);
1244 ret = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1246 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1247 iavf_stat_str(hw, ret),
1248 iavf_aq_str(hw, hw->aq.asq_last_status));
1253 ret = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1254 adapter->rss_lut, adapter->rss_lut_size);
1256 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1257 iavf_stat_str(hw, ret),
1258 iavf_aq_str(hw, hw->aq.asq_last_status));
1266 * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1267 * @adapter: board private structure
1269 * Returns 0 on success, negative on failure
1271 static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1273 struct iavf_hw *hw = &adapter->hw;
1277 dw = (u32 *)adapter->rss_key;
1278 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1279 wr32(hw, IAVF_VFQF_HKEY(i), dw[i]);
1281 dw = (u32 *)adapter->rss_lut;
1282 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1283 wr32(hw, IAVF_VFQF_HLUT(i), dw[i]);
1291 * iavf_config_rss - Configure RSS keys and lut
1292 * @adapter: board private structure
1294 * Returns 0 on success, negative on failure
1296 int iavf_config_rss(struct iavf_adapter *adapter)
1299 if (RSS_PF(adapter)) {
1300 adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1301 IAVF_FLAG_AQ_SET_RSS_KEY;
1303 } else if (RSS_AQ(adapter)) {
1304 return iavf_config_rss_aq(adapter);
1306 return iavf_config_rss_reg(adapter);
1311 * iavf_fill_rss_lut - Fill the lut with default values
1312 * @adapter: board private structure
1314 static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1318 for (i = 0; i < adapter->rss_lut_size; i++)
1319 adapter->rss_lut[i] = i % adapter->num_active_queues;
1323 * iavf_init_rss - Prepare for RSS
1324 * @adapter: board private structure
1326 * Return 0 on success, negative on failure
1328 static int iavf_init_rss(struct iavf_adapter *adapter)
1330 struct iavf_hw *hw = &adapter->hw;
1333 if (!RSS_PF(adapter)) {
1334 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1335 if (adapter->vf_res->vf_cap_flags &
1336 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1337 adapter->hena = IAVF_DEFAULT_RSS_HENA_EXPANDED;
1339 adapter->hena = IAVF_DEFAULT_RSS_HENA;
1341 wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->hena);
1342 wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1345 iavf_fill_rss_lut(adapter);
1346 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1347 ret = iavf_config_rss(adapter);
1353 * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1354 * @adapter: board private structure to initialize
1356 * We allocate one q_vector per queue interrupt. If allocation fails we
1359 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1361 int q_idx = 0, num_q_vectors;
1362 struct iavf_q_vector *q_vector;
1364 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1365 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1367 if (!adapter->q_vectors)
1370 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1371 q_vector = &adapter->q_vectors[q_idx];
1372 q_vector->adapter = adapter;
1373 q_vector->vsi = &adapter->vsi;
1374 q_vector->v_idx = q_idx;
1375 q_vector->reg_idx = q_idx;
1376 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1377 netif_napi_add(adapter->netdev, &q_vector->napi,
1378 iavf_napi_poll, NAPI_POLL_WEIGHT);
1385 * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1386 * @adapter: board private structure to initialize
1388 * This function frees the memory allocated to the q_vectors. In addition if
1389 * NAPI is enabled it will delete any references to the NAPI struct prior
1390 * to freeing the q_vector.
1392 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1394 int q_idx, num_q_vectors;
1397 if (!adapter->q_vectors)
1400 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1401 napi_vectors = adapter->num_active_queues;
1403 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1404 struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1406 if (q_idx < napi_vectors)
1407 netif_napi_del(&q_vector->napi);
1409 kfree(adapter->q_vectors);
1410 adapter->q_vectors = NULL;
1414 * iavf_reset_interrupt_capability - Reset MSIX setup
1415 * @adapter: board private structure
1418 void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1420 if (!adapter->msix_entries)
1423 pci_disable_msix(adapter->pdev);
1424 kfree(adapter->msix_entries);
1425 adapter->msix_entries = NULL;
1429 * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1430 * @adapter: board private structure to initialize
1433 int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1437 err = iavf_alloc_queues(adapter);
1439 dev_err(&adapter->pdev->dev,
1440 "Unable to allocate memory for queues\n");
1441 goto err_alloc_queues;
1445 err = iavf_set_interrupt_capability(adapter);
1448 dev_err(&adapter->pdev->dev,
1449 "Unable to setup interrupt capabilities\n");
1450 goto err_set_interrupt;
1453 err = iavf_alloc_q_vectors(adapter);
1455 dev_err(&adapter->pdev->dev,
1456 "Unable to allocate memory for queue vectors\n");
1457 goto err_alloc_q_vectors;
1460 /* If we've made it so far while ADq flag being ON, then we haven't
1461 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1462 * resources have been allocated in the reset path.
1463 * Now we can truly claim that ADq is enabled.
1465 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1467 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1470 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1471 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1472 adapter->num_active_queues);
1475 err_alloc_q_vectors:
1476 iavf_reset_interrupt_capability(adapter);
1478 iavf_free_queues(adapter);
1484 * iavf_free_rss - Free memory used by RSS structs
1485 * @adapter: board private structure
1487 static void iavf_free_rss(struct iavf_adapter *adapter)
1489 kfree(adapter->rss_key);
1490 adapter->rss_key = NULL;
1492 kfree(adapter->rss_lut);
1493 adapter->rss_lut = NULL;
1497 * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1498 * @adapter: board private structure
1500 * Returns 0 on success, negative on failure
1502 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter)
1504 struct net_device *netdev = adapter->netdev;
1507 if (netif_running(netdev))
1508 iavf_free_traffic_irqs(adapter);
1509 iavf_free_misc_irq(adapter);
1510 iavf_reset_interrupt_capability(adapter);
1511 iavf_free_q_vectors(adapter);
1512 iavf_free_queues(adapter);
1514 err = iavf_init_interrupt_scheme(adapter);
1518 netif_tx_stop_all_queues(netdev);
1520 err = iavf_request_misc_irq(adapter);
1524 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1526 iavf_map_rings_to_vectors(adapter);
1528 if (RSS_AQ(adapter))
1529 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
1531 err = iavf_init_rss(adapter);
1537 * iavf_watchdog_timer - Periodic call-back timer
1538 * @data: pointer to adapter disguised as unsigned long
1540 static void iavf_watchdog_timer(struct timer_list *t)
1542 struct iavf_adapter *adapter = from_timer(adapter, t,
1545 schedule_work(&adapter->watchdog_task);
1546 /* timer will be rescheduled in watchdog task */
1550 * iavf_watchdog_task - Periodic call-back task
1551 * @work: pointer to work_struct
1553 static void iavf_watchdog_task(struct work_struct *work)
1555 struct iavf_adapter *adapter = container_of(work,
1556 struct iavf_adapter,
1558 struct iavf_hw *hw = &adapter->hw;
1561 if (test_and_set_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section))
1562 goto restart_watchdog;
1564 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
1565 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
1566 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1567 if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
1568 (reg_val == VIRTCHNL_VFR_COMPLETED)) {
1569 /* A chance for redemption! */
1570 dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
1571 adapter->state = __IAVF_STARTUP;
1572 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1573 schedule_delayed_work(&adapter->init_task, 10);
1574 clear_bit(__IAVF_IN_CRITICAL_TASK,
1575 &adapter->crit_section);
1576 /* Don't reschedule the watchdog, since we've restarted
1577 * the init task. When init_task contacts the PF and
1578 * gets everything set up again, it'll restart the
1579 * watchdog for us. Down, boy. Sit. Stay. Woof.
1583 adapter->aq_required = 0;
1584 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1588 if ((adapter->state < __IAVF_DOWN) ||
1589 (adapter->flags & IAVF_FLAG_RESET_PENDING))
1592 /* check for reset */
1593 reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
1594 if (!(adapter->flags & IAVF_FLAG_RESET_PENDING) && !reg_val) {
1595 adapter->state = __IAVF_RESETTING;
1596 adapter->flags |= IAVF_FLAG_RESET_PENDING;
1597 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
1598 schedule_work(&adapter->reset_task);
1599 adapter->aq_required = 0;
1600 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1604 /* Process admin queue tasks. After init, everything gets done
1605 * here so we don't race on the admin queue.
1607 if (adapter->current_op) {
1608 if (!iavf_asq_done(hw)) {
1609 dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
1610 iavf_send_api_ver(adapter);
1614 if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG) {
1615 iavf_send_vf_config_msg(adapter);
1619 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
1620 iavf_disable_queues(adapter);
1624 if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
1625 iavf_map_queues(adapter);
1629 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
1630 iavf_add_ether_addrs(adapter);
1634 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
1635 iavf_add_vlans(adapter);
1639 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
1640 iavf_del_ether_addrs(adapter);
1644 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
1645 iavf_del_vlans(adapter);
1649 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1650 iavf_enable_vlan_stripping(adapter);
1654 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1655 iavf_disable_vlan_stripping(adapter);
1659 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
1660 iavf_configure_queues(adapter);
1664 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
1665 iavf_enable_queues(adapter);
1669 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
1670 /* This message goes straight to the firmware, not the
1671 * PF, so we don't have to set current_op as we will
1672 * not get a response through the ARQ.
1674 iavf_init_rss(adapter);
1675 adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
1678 if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
1679 iavf_get_hena(adapter);
1682 if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
1683 iavf_set_hena(adapter);
1686 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
1687 iavf_set_rss_key(adapter);
1690 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
1691 iavf_set_rss_lut(adapter);
1695 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_PROMISC) {
1696 iavf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1697 FLAG_VF_MULTICAST_PROMISC);
1701 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_ALLMULTI) {
1702 iavf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1706 if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) &&
1707 (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1708 iavf_set_promiscuous(adapter, 0);
1712 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
1713 iavf_enable_channels(adapter);
1717 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
1718 iavf_disable_channels(adapter);
1722 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1723 iavf_add_cloud_filter(adapter);
1727 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1728 iavf_del_cloud_filter(adapter);
1732 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
1734 if (adapter->state == __IAVF_RUNNING)
1735 iavf_request_stats(adapter);
1737 if (adapter->state == __IAVF_RUNNING)
1738 iavf_detect_recover_hung(&adapter->vsi);
1739 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1741 if (adapter->state == __IAVF_REMOVE)
1743 if (adapter->aq_required)
1744 mod_timer(&adapter->watchdog_timer,
1745 jiffies + msecs_to_jiffies(20));
1747 mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
1748 schedule_work(&adapter->adminq_task);
1751 static void iavf_disable_vf(struct iavf_adapter *adapter)
1753 struct iavf_mac_filter *f, *ftmp;
1754 struct iavf_vlan_filter *fv, *fvtmp;
1755 struct iavf_cloud_filter *cf, *cftmp;
1757 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
1759 /* We don't use netif_running() because it may be true prior to
1760 * ndo_open() returning, so we can't assume it means all our open
1761 * tasks have finished, since we're not holding the rtnl_lock here.
1763 if (adapter->state == __IAVF_RUNNING) {
1764 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1765 netif_carrier_off(adapter->netdev);
1766 netif_tx_disable(adapter->netdev);
1767 adapter->link_up = false;
1768 iavf_napi_disable_all(adapter);
1769 iavf_irq_disable(adapter);
1770 iavf_free_traffic_irqs(adapter);
1771 iavf_free_all_tx_resources(adapter);
1772 iavf_free_all_rx_resources(adapter);
1775 spin_lock_bh(&adapter->mac_vlan_list_lock);
1777 /* Delete all of the filters */
1778 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
1783 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
1784 list_del(&fv->list);
1788 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1790 spin_lock_bh(&adapter->cloud_filter_list_lock);
1791 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1792 list_del(&cf->list);
1794 adapter->num_cloud_filters--;
1796 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1798 iavf_free_misc_irq(adapter);
1799 iavf_reset_interrupt_capability(adapter);
1800 iavf_free_queues(adapter);
1801 iavf_free_q_vectors(adapter);
1802 kfree(adapter->vf_res);
1803 iavf_shutdown_adminq(&adapter->hw);
1804 adapter->netdev->flags &= ~IFF_UP;
1805 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
1806 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1807 adapter->state = __IAVF_DOWN;
1808 wake_up(&adapter->down_waitqueue);
1809 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
1812 #define IAVF_RESET_WAIT_MS 10
1813 #define IAVF_RESET_WAIT_COUNT 500
1815 * iavf_reset_task - Call-back task to handle hardware reset
1816 * @work: pointer to work_struct
1818 * During reset we need to shut down and reinitialize the admin queue
1819 * before we can use it to communicate with the PF again. We also clear
1820 * and reinit the rings because that context is lost as well.
1822 static void iavf_reset_task(struct work_struct *work)
1824 struct iavf_adapter *adapter = container_of(work,
1825 struct iavf_adapter,
1827 struct virtchnl_vf_resource *vfres = adapter->vf_res;
1828 struct net_device *netdev = adapter->netdev;
1829 struct iavf_hw *hw = &adapter->hw;
1830 struct iavf_vlan_filter *vlf;
1831 struct iavf_cloud_filter *cf;
1832 struct iavf_mac_filter *f;
1837 /* When device is being removed it doesn't make sense to run the reset
1838 * task, just return in such a case.
1840 if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
1843 while (test_and_set_bit(__IAVF_IN_CLIENT_TASK,
1844 &adapter->crit_section))
1845 usleep_range(500, 1000);
1846 if (CLIENT_ENABLED(adapter)) {
1847 adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
1848 IAVF_FLAG_CLIENT_NEEDS_CLOSE |
1849 IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
1850 IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
1851 cancel_delayed_work_sync(&adapter->client_task);
1852 iavf_notify_client_close(&adapter->vsi, true);
1854 iavf_misc_irq_disable(adapter);
1855 if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
1856 adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
1857 /* Restart the AQ here. If we have been reset but didn't
1858 * detect it, or if the PF had to reinit, our AQ will be hosed.
1860 iavf_shutdown_adminq(hw);
1861 iavf_init_adminq(hw);
1862 iavf_request_reset(adapter);
1864 adapter->flags |= IAVF_FLAG_RESET_PENDING;
1866 /* poll until we see the reset actually happen */
1867 for (i = 0; i < IAVF_RESET_WAIT_COUNT; i++) {
1868 reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
1869 IAVF_VF_ARQLEN1_ARQENABLE_MASK;
1872 usleep_range(5000, 10000);
1874 if (i == IAVF_RESET_WAIT_COUNT) {
1875 dev_info(&adapter->pdev->dev, "Never saw reset\n");
1876 goto continue_reset; /* act like the reset happened */
1879 /* wait until the reset is complete and the PF is responding to us */
1880 for (i = 0; i < IAVF_RESET_WAIT_COUNT; i++) {
1881 /* sleep first to make sure a minimum wait time is met */
1882 msleep(IAVF_RESET_WAIT_MS);
1884 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
1885 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1886 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
1890 pci_set_master(adapter->pdev);
1892 if (i == IAVF_RESET_WAIT_COUNT) {
1893 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
1895 iavf_disable_vf(adapter);
1896 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
1897 return; /* Do not attempt to reinit. It's dead, Jim. */
1901 /* We don't use netif_running() because it may be true prior to
1902 * ndo_open() returning, so we can't assume it means all our open
1903 * tasks have finished, since we're not holding the rtnl_lock here.
1905 running = ((adapter->state == __IAVF_RUNNING) ||
1906 (adapter->state == __IAVF_RESETTING));
1909 netif_carrier_off(netdev);
1910 netif_tx_stop_all_queues(netdev);
1911 adapter->link_up = false;
1912 iavf_napi_disable_all(adapter);
1914 iavf_irq_disable(adapter);
1916 adapter->state = __IAVF_RESETTING;
1917 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1919 /* free the Tx/Rx rings and descriptors, might be better to just
1920 * re-use them sometime in the future
1922 iavf_free_all_rx_resources(adapter);
1923 iavf_free_all_tx_resources(adapter);
1925 adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
1926 /* kill and reinit the admin queue */
1927 iavf_shutdown_adminq(hw);
1928 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1929 err = iavf_init_adminq(hw);
1931 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
1933 adapter->aq_required = 0;
1935 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
1936 err = iavf_reinit_interrupt_scheme(adapter);
1941 adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
1942 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
1944 spin_lock_bh(&adapter->mac_vlan_list_lock);
1946 /* re-add all MAC filters */
1947 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1950 /* re-add all VLAN filters */
1951 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1955 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1957 /* check if TCs are running and re-add all cloud filters */
1958 spin_lock_bh(&adapter->cloud_filter_list_lock);
1959 if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1961 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1965 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1967 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
1968 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
1969 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
1970 iavf_misc_irq_enable(adapter);
1972 mod_timer(&adapter->watchdog_timer, jiffies + 2);
1974 /* We were running when the reset started, so we need to restore some
1978 /* allocate transmit descriptors */
1979 err = iavf_setup_all_tx_resources(adapter);
1983 /* allocate receive descriptors */
1984 err = iavf_setup_all_rx_resources(adapter);
1988 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
1989 err = iavf_request_traffic_irqs(adapter, netdev->name);
1993 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1996 iavf_configure(adapter);
1998 iavf_up_complete(adapter);
2000 iavf_irq_enable(adapter, true);
2002 adapter->state = __IAVF_DOWN;
2003 wake_up(&adapter->down_waitqueue);
2005 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2006 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2010 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2011 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2012 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
2017 * iavf_adminq_task - worker thread to clean the admin queue
2018 * @work: pointer to work_struct containing our data
2020 static void iavf_adminq_task(struct work_struct *work)
2022 struct iavf_adapter *adapter =
2023 container_of(work, struct iavf_adapter, adminq_task);
2024 struct iavf_hw *hw = &adapter->hw;
2025 struct iavf_arq_event_info event;
2026 enum virtchnl_ops v_op;
2027 enum iavf_status ret, v_ret;
2031 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2034 event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
2035 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2040 ret = iavf_clean_arq_element(hw, &event, &pending);
2041 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2042 v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
2045 break; /* No event to process or error cleaning ARQ */
2047 iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2050 memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
2053 if ((adapter->flags &
2054 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED)) ||
2055 adapter->state == __IAVF_RESETTING)
2058 /* check for error indications */
2059 val = rd32(hw, hw->aq.arq.len);
2060 if (val == 0xdeadbeef) /* indicates device in reset */
2063 if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
2064 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2065 val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
2067 if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
2068 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2069 val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
2071 if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
2072 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2073 val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
2076 wr32(hw, hw->aq.arq.len, val);
2078 val = rd32(hw, hw->aq.asq.len);
2080 if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
2081 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2082 val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
2084 if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
2085 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2086 val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
2088 if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
2089 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2090 val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
2093 wr32(hw, hw->aq.asq.len, val);
2096 kfree(event.msg_buf);
2098 /* re-enable Admin queue interrupt cause */
2099 iavf_misc_irq_enable(adapter);
2103 * iavf_client_task - worker thread to perform client work
2104 * @work: pointer to work_struct containing our data
2106 * This task handles client interactions. Because client calls can be
2107 * reentrant, we can't handle them in the watchdog.
2109 static void iavf_client_task(struct work_struct *work)
2111 struct iavf_adapter *adapter =
2112 container_of(work, struct iavf_adapter, client_task.work);
2114 /* If we can't get the client bit, just give up. We'll be rescheduled
2118 if (test_and_set_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section))
2121 if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2122 iavf_client_subtask(adapter);
2123 adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
2126 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2127 iavf_notify_client_l2_params(&adapter->vsi);
2128 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2131 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_CLOSE) {
2132 iavf_notify_client_close(&adapter->vsi, false);
2133 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_CLOSE;
2136 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
2137 iavf_notify_client_open(&adapter->vsi);
2138 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
2141 clear_bit(__IAVF_IN_CLIENT_TASK, &adapter->crit_section);
2145 * iavf_free_all_tx_resources - Free Tx Resources for All Queues
2146 * @adapter: board private structure
2148 * Free all transmit software resources
2150 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
2154 if (!adapter->tx_rings)
2157 for (i = 0; i < adapter->num_active_queues; i++)
2158 if (adapter->tx_rings[i].desc)
2159 iavf_free_tx_resources(&adapter->tx_rings[i]);
2163 * iavf_setup_all_tx_resources - allocate all queues Tx resources
2164 * @adapter: board private structure
2166 * If this function returns with an error, then it's possible one or
2167 * more of the rings is populated (while the rest are not). It is the
2168 * callers duty to clean those orphaned rings.
2170 * Return 0 on success, negative on failure
2172 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
2176 for (i = 0; i < adapter->num_active_queues; i++) {
2177 adapter->tx_rings[i].count = adapter->tx_desc_count;
2178 err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
2181 dev_err(&adapter->pdev->dev,
2182 "Allocation for Tx Queue %u failed\n", i);
2190 * iavf_setup_all_rx_resources - allocate all queues Rx resources
2191 * @adapter: board private structure
2193 * If this function returns with an error, then it's possible one or
2194 * more of the rings is populated (while the rest are not). It is the
2195 * callers duty to clean those orphaned rings.
2197 * Return 0 on success, negative on failure
2199 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
2203 for (i = 0; i < adapter->num_active_queues; i++) {
2204 adapter->rx_rings[i].count = adapter->rx_desc_count;
2205 err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
2208 dev_err(&adapter->pdev->dev,
2209 "Allocation for Rx Queue %u failed\n", i);
2216 * iavf_free_all_rx_resources - Free Rx Resources for All Queues
2217 * @adapter: board private structure
2219 * Free all receive software resources
2221 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
2225 if (!adapter->rx_rings)
2228 for (i = 0; i < adapter->num_active_queues; i++)
2229 if (adapter->rx_rings[i].desc)
2230 iavf_free_rx_resources(&adapter->rx_rings[i]);
2234 * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
2235 * @adapter: board private structure
2236 * @max_tx_rate: max Tx bw for a tc
2238 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
2241 int speed = 0, ret = 0;
2243 switch (adapter->link_speed) {
2244 case IAVF_LINK_SPEED_40GB:
2247 case IAVF_LINK_SPEED_25GB:
2250 case IAVF_LINK_SPEED_20GB:
2253 case IAVF_LINK_SPEED_10GB:
2256 case IAVF_LINK_SPEED_1GB:
2259 case IAVF_LINK_SPEED_100MB:
2266 if (max_tx_rate > speed) {
2267 dev_err(&adapter->pdev->dev,
2268 "Invalid tx rate specified\n");
2276 * iavf_validate_channel_config - validate queue mapping info
2277 * @adapter: board private structure
2278 * @mqprio_qopt: queue parameters
2280 * This function validates if the config provided by the user to
2281 * configure queue channels is valid or not. Returns 0 on a valid
2284 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
2285 struct tc_mqprio_qopt_offload *mqprio_qopt)
2287 u64 total_max_rate = 0;
2292 if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
2293 mqprio_qopt->qopt.num_tc < 1)
2296 for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2297 if (!mqprio_qopt->qopt.count[i] ||
2298 mqprio_qopt->qopt.offset[i] != num_qps)
2300 if (mqprio_qopt->min_rate[i]) {
2301 dev_err(&adapter->pdev->dev,
2302 "Invalid min tx rate (greater than 0) specified\n");
2305 /*convert to Mbps */
2306 tx_rate = div_u64(mqprio_qopt->max_rate[i],
2308 total_max_rate += tx_rate;
2309 num_qps += mqprio_qopt->qopt.count[i];
2311 if (num_qps > IAVF_MAX_REQ_QUEUES)
2314 ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
2319 * iavf_del_all_cloud_filters - delete all cloud filters
2320 * on the traffic classes
2322 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
2324 struct iavf_cloud_filter *cf, *cftmp;
2326 spin_lock_bh(&adapter->cloud_filter_list_lock);
2327 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2329 list_del(&cf->list);
2331 adapter->num_cloud_filters--;
2333 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2337 * __iavf_setup_tc - configure multiple traffic classes
2338 * @netdev: network interface device structure
2339 * @type_date: tc offload data
2341 * This function processes the config information provided by the
2342 * user to configure traffic classes/queue channels and packages the
2343 * information to request the PF to setup traffic classes.
2345 * Returns 0 on success.
2347 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
2349 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2350 struct iavf_adapter *adapter = netdev_priv(netdev);
2351 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2352 u8 num_tc = 0, total_qps = 0;
2353 int ret = 0, netdev_tc = 0;
2358 num_tc = mqprio_qopt->qopt.num_tc;
2359 mode = mqprio_qopt->mode;
2361 /* delete queue_channel */
2362 if (!mqprio_qopt->qopt.hw) {
2363 if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
2364 /* reset the tc configuration */
2365 netdev_reset_tc(netdev);
2366 adapter->num_tc = 0;
2367 netif_tx_stop_all_queues(netdev);
2368 netif_tx_disable(netdev);
2369 iavf_del_all_cloud_filters(adapter);
2370 adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
2377 /* add queue channel */
2378 if (mode == TC_MQPRIO_MODE_CHANNEL) {
2379 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2380 dev_err(&adapter->pdev->dev, "ADq not supported\n");
2383 if (adapter->ch_config.state != __IAVF_TC_INVALID) {
2384 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2388 ret = iavf_validate_ch_config(adapter, mqprio_qopt);
2391 /* Return if same TC config is requested */
2392 if (adapter->num_tc == num_tc)
2394 adapter->num_tc = num_tc;
2396 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2398 adapter->ch_config.ch_info[i].count =
2399 mqprio_qopt->qopt.count[i];
2400 adapter->ch_config.ch_info[i].offset =
2401 mqprio_qopt->qopt.offset[i];
2402 total_qps += mqprio_qopt->qopt.count[i];
2403 max_tx_rate = mqprio_qopt->max_rate[i];
2404 /* convert to Mbps */
2405 max_tx_rate = div_u64(max_tx_rate,
2407 adapter->ch_config.ch_info[i].max_tx_rate =
2410 adapter->ch_config.ch_info[i].count = 1;
2411 adapter->ch_config.ch_info[i].offset = 0;
2414 adapter->ch_config.total_qps = total_qps;
2415 netif_tx_stop_all_queues(netdev);
2416 netif_tx_disable(netdev);
2417 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
2418 netdev_reset_tc(netdev);
2419 /* Report the tc mapping up the stack */
2420 netdev_set_num_tc(adapter->netdev, num_tc);
2421 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2422 u16 qcount = mqprio_qopt->qopt.count[i];
2423 u16 qoffset = mqprio_qopt->qopt.offset[i];
2426 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2435 * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
2436 * @adapter: board private structure
2437 * @cls_flower: pointer to struct tc_cls_flower_offload
2438 * @filter: pointer to cloud filter structure
2440 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
2441 struct tc_cls_flower_offload *f,
2442 struct iavf_cloud_filter *filter)
2444 struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
2445 struct flow_dissector *dissector = rule->match.dissector;
2446 u16 n_proto_mask = 0;
2447 u16 n_proto_key = 0;
2452 struct virtchnl_filter *vf = &filter->f;
2454 if (dissector->used_keys &
2455 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2456 BIT(FLOW_DISSECTOR_KEY_BASIC) |
2457 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2458 BIT(FLOW_DISSECTOR_KEY_VLAN) |
2459 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2460 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2461 BIT(FLOW_DISSECTOR_KEY_PORTS) |
2462 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2463 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2464 dissector->used_keys);
2468 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2469 struct flow_match_enc_keyid match;
2471 flow_rule_match_enc_keyid(rule, &match);
2472 if (match.mask->keyid != 0)
2473 field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
2476 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
2477 struct flow_match_basic match;
2479 flow_rule_match_basic(rule, &match);
2480 n_proto_key = ntohs(match.key->n_proto);
2481 n_proto_mask = ntohs(match.mask->n_proto);
2483 if (n_proto_key == ETH_P_ALL) {
2487 n_proto = n_proto_key & n_proto_mask;
2488 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2490 if (n_proto == ETH_P_IPV6) {
2491 /* specify flow type as TCP IPv6 */
2492 vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2495 if (match.key->ip_proto != IPPROTO_TCP) {
2496 dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2501 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2502 struct flow_match_eth_addrs match;
2504 flow_rule_match_eth_addrs(rule, &match);
2506 /* use is_broadcast and is_zero to check for all 0xf or 0 */
2507 if (!is_zero_ether_addr(match.mask->dst)) {
2508 if (is_broadcast_ether_addr(match.mask->dst)) {
2509 field_flags |= IAVF_CLOUD_FIELD_OMAC;
2511 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2513 return IAVF_ERR_CONFIG;
2517 if (!is_zero_ether_addr(match.mask->src)) {
2518 if (is_broadcast_ether_addr(match.mask->src)) {
2519 field_flags |= IAVF_CLOUD_FIELD_IMAC;
2521 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2523 return IAVF_ERR_CONFIG;
2527 if (!is_zero_ether_addr(match.key->dst))
2528 if (is_valid_ether_addr(match.key->dst) ||
2529 is_multicast_ether_addr(match.key->dst)) {
2530 /* set the mask if a valid dst_mac address */
2531 for (i = 0; i < ETH_ALEN; i++)
2532 vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2533 ether_addr_copy(vf->data.tcp_spec.dst_mac,
2537 if (!is_zero_ether_addr(match.key->src))
2538 if (is_valid_ether_addr(match.key->src) ||
2539 is_multicast_ether_addr(match.key->src)) {
2540 /* set the mask if a valid dst_mac address */
2541 for (i = 0; i < ETH_ALEN; i++)
2542 vf->mask.tcp_spec.src_mac[i] |= 0xff;
2543 ether_addr_copy(vf->data.tcp_spec.src_mac,
2548 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
2549 struct flow_match_vlan match;
2551 flow_rule_match_vlan(rule, &match);
2552 if (match.mask->vlan_id) {
2553 if (match.mask->vlan_id == VLAN_VID_MASK) {
2554 field_flags |= IAVF_CLOUD_FIELD_IVLAN;
2556 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2557 match.mask->vlan_id);
2558 return IAVF_ERR_CONFIG;
2561 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2562 vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
2565 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
2566 struct flow_match_control match;
2568 flow_rule_match_control(rule, &match);
2569 addr_type = match.key->addr_type;
2572 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2573 struct flow_match_ipv4_addrs match;
2575 flow_rule_match_ipv4_addrs(rule, &match);
2576 if (match.mask->dst) {
2577 if (match.mask->dst == cpu_to_be32(0xffffffff)) {
2578 field_flags |= IAVF_CLOUD_FIELD_IIP;
2580 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2581 be32_to_cpu(match.mask->dst));
2582 return IAVF_ERR_CONFIG;
2586 if (match.mask->src) {
2587 if (match.mask->src == cpu_to_be32(0xffffffff)) {
2588 field_flags |= IAVF_CLOUD_FIELD_IIP;
2590 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2591 be32_to_cpu(match.mask->dst));
2592 return IAVF_ERR_CONFIG;
2596 if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
2597 dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2598 return IAVF_ERR_CONFIG;
2600 if (match.key->dst) {
2601 vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2602 vf->data.tcp_spec.dst_ip[0] = match.key->dst;
2604 if (match.key->src) {
2605 vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2606 vf->data.tcp_spec.src_ip[0] = match.key->src;
2610 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2611 struct flow_match_ipv6_addrs match;
2613 flow_rule_match_ipv6_addrs(rule, &match);
2615 /* validate mask, make sure it is not IPV6_ADDR_ANY */
2616 if (ipv6_addr_any(&match.mask->dst)) {
2617 dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2619 return IAVF_ERR_CONFIG;
2622 /* src and dest IPv6 address should not be LOOPBACK
2623 * (0:0:0:0:0:0:0:1) which can be represented as ::1
2625 if (ipv6_addr_loopback(&match.key->dst) ||
2626 ipv6_addr_loopback(&match.key->src)) {
2627 dev_err(&adapter->pdev->dev,
2628 "ipv6 addr should not be loopback\n");
2629 return IAVF_ERR_CONFIG;
2631 if (!ipv6_addr_any(&match.mask->dst) ||
2632 !ipv6_addr_any(&match.mask->src))
2633 field_flags |= IAVF_CLOUD_FIELD_IIP;
2635 for (i = 0; i < 4; i++)
2636 vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2637 memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
2638 sizeof(vf->data.tcp_spec.dst_ip));
2639 for (i = 0; i < 4; i++)
2640 vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2641 memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
2642 sizeof(vf->data.tcp_spec.src_ip));
2644 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
2645 struct flow_match_ports match;
2647 flow_rule_match_ports(rule, &match);
2648 if (match.mask->src) {
2649 if (match.mask->src == cpu_to_be16(0xffff)) {
2650 field_flags |= IAVF_CLOUD_FIELD_IIP;
2652 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
2653 be16_to_cpu(match.mask->src));
2654 return IAVF_ERR_CONFIG;
2658 if (match.mask->dst) {
2659 if (match.mask->dst == cpu_to_be16(0xffff)) {
2660 field_flags |= IAVF_CLOUD_FIELD_IIP;
2662 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
2663 be16_to_cpu(match.mask->dst));
2664 return IAVF_ERR_CONFIG;
2667 if (match.key->dst) {
2668 vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
2669 vf->data.tcp_spec.dst_port = match.key->dst;
2672 if (match.key->src) {
2673 vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
2674 vf->data.tcp_spec.src_port = match.key->src;
2677 vf->field_flags = field_flags;
2683 * iavf_handle_tclass - Forward to a traffic class on the device
2684 * @adapter: board private structure
2685 * @tc: traffic class index on the device
2686 * @filter: pointer to cloud filter structure
2688 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
2689 struct iavf_cloud_filter *filter)
2693 if (tc < adapter->num_tc) {
2694 if (!filter->f.data.tcp_spec.dst_port) {
2695 dev_err(&adapter->pdev->dev,
2696 "Specify destination port to redirect to traffic class other than TC0\n");
2700 /* redirect to a traffic class on the same device */
2701 filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
2702 filter->f.action_meta = tc;
2707 * iavf_configure_clsflower - Add tc flower filters
2708 * @adapter: board private structure
2709 * @cls_flower: Pointer to struct tc_cls_flower_offload
2711 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
2712 struct tc_cls_flower_offload *cls_flower)
2714 int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
2715 struct iavf_cloud_filter *filter = NULL;
2716 int err = -EINVAL, count = 50;
2719 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
2723 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2727 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
2728 &adapter->crit_section)) {
2734 filter->cookie = cls_flower->cookie;
2736 /* set the mask to all zeroes to begin with */
2737 memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
2738 /* start out with flow type and eth type IPv4 to begin with */
2739 filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
2740 err = iavf_parse_cls_flower(adapter, cls_flower, filter);
2744 err = iavf_handle_tclass(adapter, tc, filter);
2748 /* add filter to the list */
2749 spin_lock_bh(&adapter->cloud_filter_list_lock);
2750 list_add_tail(&filter->list, &adapter->cloud_filter_list);
2751 adapter->num_cloud_filters++;
2753 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
2754 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2759 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2763 /* iavf_find_cf - Find the cloud filter in the list
2764 * @adapter: Board private structure
2765 * @cookie: filter specific cookie
2767 * Returns ptr to the filter object or NULL. Must be called while holding the
2768 * cloud_filter_list_lock.
2770 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
2771 unsigned long *cookie)
2773 struct iavf_cloud_filter *filter = NULL;
2778 list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
2779 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
2786 * iavf_delete_clsflower - Remove tc flower filters
2787 * @adapter: board private structure
2788 * @cls_flower: Pointer to struct tc_cls_flower_offload
2790 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
2791 struct tc_cls_flower_offload *cls_flower)
2793 struct iavf_cloud_filter *filter = NULL;
2796 spin_lock_bh(&adapter->cloud_filter_list_lock);
2797 filter = iavf_find_cf(adapter, &cls_flower->cookie);
2800 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
2804 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2810 * iavf_setup_tc_cls_flower - flower classifier offloads
2811 * @netdev: net device to configure
2812 * @type_data: offload data
2814 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
2815 struct tc_cls_flower_offload *cls_flower)
2817 if (cls_flower->common.chain_index)
2820 switch (cls_flower->command) {
2821 case TC_CLSFLOWER_REPLACE:
2822 return iavf_configure_clsflower(adapter, cls_flower);
2823 case TC_CLSFLOWER_DESTROY:
2824 return iavf_delete_clsflower(adapter, cls_flower);
2825 case TC_CLSFLOWER_STATS:
2833 * iavf_setup_tc_block_cb - block callback for tc
2834 * @type: type of offload
2835 * @type_data: offload data
2838 * This function is the block callback for traffic classes
2840 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2844 case TC_SETUP_CLSFLOWER:
2845 return iavf_setup_tc_cls_flower(cb_priv, type_data);
2852 * iavf_setup_tc_block - register callbacks for tc
2853 * @netdev: network interface device structure
2854 * @f: tc offload data
2856 * This function registers block callbacks for tc
2859 static int iavf_setup_tc_block(struct net_device *dev,
2860 struct tc_block_offload *f)
2862 struct iavf_adapter *adapter = netdev_priv(dev);
2864 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2867 switch (f->command) {
2869 return tcf_block_cb_register(f->block, iavf_setup_tc_block_cb,
2870 adapter, adapter, f->extack);
2871 case TC_BLOCK_UNBIND:
2872 tcf_block_cb_unregister(f->block, iavf_setup_tc_block_cb,
2881 * iavf_setup_tc - configure multiple traffic classes
2882 * @netdev: network interface device structure
2883 * @type: type of offload
2884 * @type_date: tc offload data
2886 * This function is the callback to ndo_setup_tc in the
2889 * Returns 0 on success
2891 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
2895 case TC_SETUP_QDISC_MQPRIO:
2896 return __iavf_setup_tc(netdev, type_data);
2897 case TC_SETUP_BLOCK:
2898 return iavf_setup_tc_block(netdev, type_data);
2905 * iavf_open - Called when a network interface is made active
2906 * @netdev: network interface device structure
2908 * Returns 0 on success, negative value on failure
2910 * The open entry point is called when a network interface is made
2911 * active by the system (IFF_UP). At this point all resources needed
2912 * for transmit and receive operations are allocated, the interrupt
2913 * handler is registered with the OS, the watchdog timer is started,
2914 * and the stack is notified that the interface is ready.
2916 static int iavf_open(struct net_device *netdev)
2918 struct iavf_adapter *adapter = netdev_priv(netdev);
2921 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
2922 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
2926 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
2927 &adapter->crit_section))
2928 usleep_range(500, 1000);
2930 if (adapter->state != __IAVF_DOWN) {
2935 /* allocate transmit descriptors */
2936 err = iavf_setup_all_tx_resources(adapter);
2940 /* allocate receive descriptors */
2941 err = iavf_setup_all_rx_resources(adapter);
2945 /* clear any pending interrupts, may auto mask */
2946 err = iavf_request_traffic_irqs(adapter, netdev->name);
2950 spin_lock_bh(&adapter->mac_vlan_list_lock);
2952 iavf_add_filter(adapter, adapter->hw.mac.addr);
2954 spin_unlock_bh(&adapter->mac_vlan_list_lock);
2956 iavf_configure(adapter);
2958 iavf_up_complete(adapter);
2960 iavf_irq_enable(adapter, true);
2962 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2968 iavf_free_traffic_irqs(adapter);
2970 iavf_free_all_rx_resources(adapter);
2972 iavf_free_all_tx_resources(adapter);
2974 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
2980 * iavf_close - Disables a network interface
2981 * @netdev: network interface device structure
2983 * Returns 0, this is not allowed to fail
2985 * The close entry point is called when an interface is de-activated
2986 * by the OS. The hardware is still under the drivers control, but
2987 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
2988 * are freed, along with all transmit and receive resources.
2990 static int iavf_close(struct net_device *netdev)
2992 struct iavf_adapter *adapter = netdev_priv(netdev);
2995 if (adapter->state <= __IAVF_DOWN_PENDING)
2998 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
2999 &adapter->crit_section))
3000 usleep_range(500, 1000);
3002 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3003 if (CLIENT_ENABLED(adapter))
3004 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3007 adapter->state = __IAVF_DOWN_PENDING;
3008 iavf_free_traffic_irqs(adapter);
3010 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3012 /* We explicitly don't free resources here because the hardware is
3013 * still active and can DMA into memory. Resources are cleared in
3014 * iavf_virtchnl_completion() after we get confirmation from the PF
3015 * driver that the rings have been stopped.
3017 * Also, we wait for state to transition to __IAVF_DOWN before
3018 * returning. State change occurs in iavf_virtchnl_completion() after
3019 * VF resources are released (which occurs after PF driver processes and
3020 * responds to admin queue commands).
3023 status = wait_event_timeout(adapter->down_waitqueue,
3024 adapter->state == __IAVF_DOWN,
3025 msecs_to_jiffies(200));
3027 netdev_warn(netdev, "Device resources not yet released\n");
3032 * iavf_change_mtu - Change the Maximum Transfer Unit
3033 * @netdev: network interface device structure
3034 * @new_mtu: new value for maximum frame size
3036 * Returns 0 on success, negative on failure
3038 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
3040 struct iavf_adapter *adapter = netdev_priv(netdev);
3042 netdev->mtu = new_mtu;
3043 if (CLIENT_ENABLED(adapter)) {
3044 iavf_notify_client_l2_params(&adapter->vsi);
3045 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3047 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
3048 schedule_work(&adapter->reset_task);
3054 * iavf_set_features - set the netdev feature flags
3055 * @netdev: ptr to the netdev being adjusted
3056 * @features: the feature set that the stack is suggesting
3057 * Note: expects to be called while under rtnl_lock()
3059 static int iavf_set_features(struct net_device *netdev,
3060 netdev_features_t features)
3062 struct iavf_adapter *adapter = netdev_priv(netdev);
3064 /* Don't allow changing VLAN_RX flag when adapter is not capable
3067 if (!VLAN_ALLOWED(adapter)) {
3068 if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX)
3070 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3071 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3072 adapter->aq_required |=
3073 IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3075 adapter->aq_required |=
3076 IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3083 * iavf_features_check - Validate encapsulated packet conforms to limits
3085 * @dev: This physical port's netdev
3086 * @features: Offload features that the stack believes apply
3088 static netdev_features_t iavf_features_check(struct sk_buff *skb,
3089 struct net_device *dev,
3090 netdev_features_t features)
3094 /* No point in doing any of this if neither checksum nor GSO are
3095 * being requested for this frame. We can rule out both by just
3096 * checking for CHECKSUM_PARTIAL
3098 if (skb->ip_summed != CHECKSUM_PARTIAL)
3101 /* We cannot support GSO if the MSS is going to be less than
3102 * 64 bytes. If it is then we need to drop support for GSO.
3104 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3105 features &= ~NETIF_F_GSO_MASK;
3107 /* MACLEN can support at most 63 words */
3108 len = skb_network_header(skb) - skb->data;
3109 if (len & ~(63 * 2))
3112 /* IPLEN and EIPLEN can support at most 127 dwords */
3113 len = skb_transport_header(skb) - skb_network_header(skb);
3114 if (len & ~(127 * 4))
3117 if (skb->encapsulation) {
3118 /* L4TUNLEN can support 127 words */
3119 len = skb_inner_network_header(skb) - skb_transport_header(skb);
3120 if (len & ~(127 * 2))
3123 /* IPLEN can support at most 127 dwords */
3124 len = skb_inner_transport_header(skb) -
3125 skb_inner_network_header(skb);
3126 if (len & ~(127 * 4))
3130 /* No need to validate L4LEN as TCP is the only protocol with a
3131 * a flexible value and we support all possible values supported
3132 * by TCP, which is at most 15 dwords
3137 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3141 * iavf_fix_features - fix up the netdev feature bits
3142 * @netdev: our net device
3143 * @features: desired feature bits
3145 * Returns fixed-up features bits
3147 static netdev_features_t iavf_fix_features(struct net_device *netdev,
3148 netdev_features_t features)
3150 struct iavf_adapter *adapter = netdev_priv(netdev);
3152 if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3153 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3154 NETIF_F_HW_VLAN_CTAG_RX |
3155 NETIF_F_HW_VLAN_CTAG_FILTER);
3160 static const struct net_device_ops iavf_netdev_ops = {
3161 .ndo_open = iavf_open,
3162 .ndo_stop = iavf_close,
3163 .ndo_start_xmit = iavf_xmit_frame,
3164 .ndo_set_rx_mode = iavf_set_rx_mode,
3165 .ndo_validate_addr = eth_validate_addr,
3166 .ndo_set_mac_address = iavf_set_mac,
3167 .ndo_change_mtu = iavf_change_mtu,
3168 .ndo_tx_timeout = iavf_tx_timeout,
3169 .ndo_vlan_rx_add_vid = iavf_vlan_rx_add_vid,
3170 .ndo_vlan_rx_kill_vid = iavf_vlan_rx_kill_vid,
3171 .ndo_features_check = iavf_features_check,
3172 .ndo_fix_features = iavf_fix_features,
3173 .ndo_set_features = iavf_set_features,
3174 .ndo_setup_tc = iavf_setup_tc,
3178 * iavf_check_reset_complete - check that VF reset is complete
3179 * @hw: pointer to hw struct
3181 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3183 static int iavf_check_reset_complete(struct iavf_hw *hw)
3188 for (i = 0; i < 100; i++) {
3189 rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
3190 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3191 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3192 (rstat == VIRTCHNL_VFR_COMPLETED))
3194 usleep_range(10, 20);
3200 * iavf_process_config - Process the config information we got from the PF
3201 * @adapter: board private structure
3203 * Verify that we have a valid config struct, and set up our netdev features
3204 * and our VSI struct.
3206 int iavf_process_config(struct iavf_adapter *adapter)
3208 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3209 int i, num_req_queues = adapter->num_req_queues;
3210 struct net_device *netdev = adapter->netdev;
3211 struct iavf_vsi *vsi = &adapter->vsi;
3212 netdev_features_t hw_enc_features;
3213 netdev_features_t hw_features;
3215 /* got VF config message back from PF, now we can parse it */
3216 for (i = 0; i < vfres->num_vsis; i++) {
3217 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3218 adapter->vsi_res = &vfres->vsi_res[i];
3220 if (!adapter->vsi_res) {
3221 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3225 if (num_req_queues &&
3226 num_req_queues != adapter->vsi_res->num_queue_pairs) {
3227 /* Problem. The PF gave us fewer queues than what we had
3228 * negotiated in our request. Need a reset to see if we can't
3229 * get back to a working state.
3231 dev_err(&adapter->pdev->dev,
3232 "Requested %d queues, but PF only gave us %d.\n",
3234 adapter->vsi_res->num_queue_pairs);
3235 adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
3236 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3237 iavf_schedule_reset(adapter);
3240 adapter->num_req_queues = 0;
3242 hw_enc_features = NETIF_F_SG |
3246 NETIF_F_SOFT_FEATURES |
3255 /* advertise to stack only if offloads for encapsulated packets is
3258 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3259 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
3261 NETIF_F_GSO_GRE_CSUM |
3262 NETIF_F_GSO_IPXIP4 |
3263 NETIF_F_GSO_IPXIP6 |
3264 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3265 NETIF_F_GSO_PARTIAL |
3268 if (!(vfres->vf_cap_flags &
3269 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3270 netdev->gso_partial_features |=
3271 NETIF_F_GSO_UDP_TUNNEL_CSUM;
3273 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3274 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3275 netdev->hw_enc_features |= hw_enc_features;
3277 /* record features VLANs can make use of */
3278 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3280 /* Write features and hw_features separately to avoid polluting
3281 * with, or dropping, features that are set when we registered.
3283 hw_features = hw_enc_features;
3285 /* Enable VLAN features if supported */
3286 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3287 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3288 NETIF_F_HW_VLAN_CTAG_RX);
3289 /* Enable cloud filter if ADQ is supported */
3290 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3291 hw_features |= NETIF_F_HW_TC;
3293 netdev->hw_features |= hw_features;
3295 netdev->features |= hw_features;
3297 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3298 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3300 netdev->priv_flags |= IFF_UNICAST_FLT;
3302 /* Do not turn on offloads when they are requested to be turned off.
3303 * TSO needs minimum 576 bytes to work correctly.
3305 if (netdev->wanted_features) {
3306 if (!(netdev->wanted_features & NETIF_F_TSO) ||
3308 netdev->features &= ~NETIF_F_TSO;
3309 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
3311 netdev->features &= ~NETIF_F_TSO6;
3312 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
3313 netdev->features &= ~NETIF_F_TSO_ECN;
3314 if (!(netdev->wanted_features & NETIF_F_GRO))
3315 netdev->features &= ~NETIF_F_GRO;
3316 if (!(netdev->wanted_features & NETIF_F_GSO))
3317 netdev->features &= ~NETIF_F_GSO;
3320 adapter->vsi.id = adapter->vsi_res->vsi_id;
3322 adapter->vsi.back = adapter;
3323 adapter->vsi.base_vector = 1;
3324 adapter->vsi.work_limit = IAVF_DEFAULT_IRQ_WORK;
3325 vsi->netdev = adapter->netdev;
3326 vsi->qs_handle = adapter->vsi_res->qset_handle;
3327 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3328 adapter->rss_key_size = vfres->rss_key_size;
3329 adapter->rss_lut_size = vfres->rss_lut_size;
3331 adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
3332 adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
3339 * iavf_init_task - worker thread to perform delayed initialization
3340 * @work: pointer to work_struct containing our data
3342 * This task completes the work that was begun in probe. Due to the nature
3343 * of VF-PF communications, we may need to wait tens of milliseconds to get
3344 * responses back from the PF. Rather than busy-wait in probe and bog down the
3345 * whole system, we'll do it in a task so we can sleep.
3346 * This task only runs during driver init. Once we've established
3347 * communications with the PF driver and set up our netdev, the watchdog
3350 static void iavf_init_task(struct work_struct *work)
3352 struct iavf_adapter *adapter = container_of(work,
3353 struct iavf_adapter,
3355 struct net_device *netdev = adapter->netdev;
3356 struct iavf_hw *hw = &adapter->hw;
3357 struct pci_dev *pdev = adapter->pdev;
3360 switch (adapter->state) {
3361 case __IAVF_STARTUP:
3362 /* driver loaded, probe complete */
3363 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
3364 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3365 err = iavf_set_mac_type(hw);
3367 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
3371 err = iavf_check_reset_complete(hw);
3373 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
3377 hw->aq.num_arq_entries = IAVF_AQ_LEN;
3378 hw->aq.num_asq_entries = IAVF_AQ_LEN;
3379 hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
3380 hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
3382 err = iavf_init_adminq(hw);
3384 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
3388 err = iavf_send_api_ver(adapter);
3390 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
3391 iavf_shutdown_adminq(hw);
3394 adapter->state = __IAVF_INIT_VERSION_CHECK;
3396 case __IAVF_INIT_VERSION_CHECK:
3397 if (!iavf_asq_done(hw)) {
3398 dev_err(&pdev->dev, "Admin queue command never completed\n");
3399 iavf_shutdown_adminq(hw);
3400 adapter->state = __IAVF_STARTUP;
3404 /* aq msg sent, awaiting reply */
3405 err = iavf_verify_api_ver(adapter);
3407 if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK)
3408 err = iavf_send_api_ver(adapter);
3410 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
3411 adapter->pf_version.major,
3412 adapter->pf_version.minor,
3413 VIRTCHNL_VERSION_MAJOR,
3414 VIRTCHNL_VERSION_MINOR);
3417 err = iavf_send_vf_config_msg(adapter);
3419 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
3423 adapter->state = __IAVF_INIT_GET_RESOURCES;
3425 case __IAVF_INIT_GET_RESOURCES:
3426 /* aq msg sent, awaiting reply */
3427 if (!adapter->vf_res) {
3428 adapter->vf_res = kzalloc(struct_size(adapter->vf_res,
3429 vsi_res, IAVF_MAX_VF_VSI),
3431 if (!adapter->vf_res)
3434 err = iavf_get_vf_config(adapter);
3435 if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK) {
3436 err = iavf_send_vf_config_msg(adapter);
3438 } else if (err == IAVF_ERR_PARAM) {
3439 /* We only get ERR_PARAM if the device is in a very bad
3440 * state or if we've been disabled for previous bad
3441 * behavior. Either way, we're done now.
3443 iavf_shutdown_adminq(hw);
3444 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
3448 dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
3452 adapter->state = __IAVF_INIT_SW;
3458 if (iavf_process_config(adapter))
3460 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3462 adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
3464 netdev->netdev_ops = &iavf_netdev_ops;
3465 iavf_set_ethtool_ops(netdev);
3466 netdev->watchdog_timeo = 5 * HZ;
3468 /* MTU range: 68 - 9710 */
3469 netdev->min_mtu = ETH_MIN_MTU;
3470 netdev->max_mtu = IAVF_MAX_RXBUFFER - IAVF_PACKET_HDR_PAD;
3472 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
3473 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
3474 adapter->hw.mac.addr);
3475 eth_hw_addr_random(netdev);
3476 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
3478 adapter->flags |= IAVF_FLAG_ADDR_SET_BY_PF;
3479 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
3480 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
3483 timer_setup(&adapter->watchdog_timer, iavf_watchdog_timer, 0);
3484 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3486 adapter->tx_desc_count = IAVF_DEFAULT_TXD;
3487 adapter->rx_desc_count = IAVF_DEFAULT_RXD;
3488 err = iavf_init_interrupt_scheme(adapter);
3491 iavf_map_rings_to_vectors(adapter);
3492 if (adapter->vf_res->vf_cap_flags &
3493 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
3494 adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
3496 err = iavf_request_misc_irq(adapter);
3500 netif_carrier_off(netdev);
3501 adapter->link_up = false;
3503 if (!adapter->netdev_registered) {
3504 err = register_netdev(netdev);
3509 adapter->netdev_registered = true;
3511 netif_tx_stop_all_queues(netdev);
3512 if (CLIENT_ALLOWED(adapter)) {
3513 err = iavf_lan_add_device(adapter);
3515 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
3519 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
3520 if (netdev->features & NETIF_F_GRO)
3521 dev_info(&pdev->dev, "GRO is enabled\n");
3523 adapter->state = __IAVF_DOWN;
3524 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3525 iavf_misc_irq_enable(adapter);
3526 wake_up(&adapter->down_waitqueue);
3528 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
3529 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
3530 if (!adapter->rss_key || !adapter->rss_lut)
3533 if (RSS_AQ(adapter)) {
3534 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
3535 mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
3537 iavf_init_rss(adapter);
3541 schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
3544 iavf_free_rss(adapter);
3546 iavf_free_misc_irq(adapter);
3548 iavf_reset_interrupt_capability(adapter);
3550 kfree(adapter->vf_res);
3551 adapter->vf_res = NULL;
3553 /* Things went into the weeds, so try again later */
3554 if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
3555 dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
3556 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
3557 iavf_shutdown_adminq(hw);
3558 adapter->state = __IAVF_STARTUP;
3559 schedule_delayed_work(&adapter->init_task, HZ * 5);
3562 schedule_delayed_work(&adapter->init_task, HZ);
3566 * iavf_shutdown - Shutdown the device in preparation for a reboot
3567 * @pdev: pci device structure
3569 static void iavf_shutdown(struct pci_dev *pdev)
3571 struct net_device *netdev = pci_get_drvdata(pdev);
3572 struct iavf_adapter *adapter = netdev_priv(netdev);
3574 netif_device_detach(netdev);
3576 if (netif_running(netdev))
3579 /* Prevent the watchdog from running. */
3580 adapter->state = __IAVF_REMOVE;
3581 adapter->aq_required = 0;
3584 pci_save_state(pdev);
3587 pci_disable_device(pdev);
3591 * iavf_probe - Device Initialization Routine
3592 * @pdev: PCI device information struct
3593 * @ent: entry in iavf_pci_tbl
3595 * Returns 0 on success, negative on failure
3597 * iavf_probe initializes an adapter identified by a pci_dev structure.
3598 * The OS initialization, configuring of the adapter private structure,
3599 * and a hardware reset occur.
3601 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3603 struct net_device *netdev;
3604 struct iavf_adapter *adapter = NULL;
3605 struct iavf_hw *hw = NULL;
3608 err = pci_enable_device(pdev);
3612 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3614 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3617 "DMA configuration failed: 0x%x\n", err);
3622 err = pci_request_regions(pdev, iavf_driver_name);
3625 "pci_request_regions failed 0x%x\n", err);
3629 pci_enable_pcie_error_reporting(pdev);
3631 pci_set_master(pdev);
3633 netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
3634 IAVF_MAX_REQ_QUEUES);
3637 goto err_alloc_etherdev;
3640 SET_NETDEV_DEV(netdev, &pdev->dev);
3642 pci_set_drvdata(pdev, netdev);
3643 adapter = netdev_priv(netdev);
3645 adapter->netdev = netdev;
3646 adapter->pdev = pdev;
3651 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3652 adapter->state = __IAVF_STARTUP;
3654 /* Call save state here because it relies on the adapter struct. */
3655 pci_save_state(pdev);
3657 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3658 pci_resource_len(pdev, 0));
3663 hw->vendor_id = pdev->vendor;
3664 hw->device_id = pdev->device;
3665 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3666 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3667 hw->subsystem_device_id = pdev->subsystem_device;
3668 hw->bus.device = PCI_SLOT(pdev->devfn);
3669 hw->bus.func = PCI_FUNC(pdev->devfn);
3670 hw->bus.bus_id = pdev->bus->number;
3672 /* set up the locks for the AQ, do this only once in probe
3673 * and destroy them only once in remove
3675 mutex_init(&hw->aq.asq_mutex);
3676 mutex_init(&hw->aq.arq_mutex);
3678 spin_lock_init(&adapter->mac_vlan_list_lock);
3679 spin_lock_init(&adapter->cloud_filter_list_lock);
3681 INIT_LIST_HEAD(&adapter->mac_filter_list);
3682 INIT_LIST_HEAD(&adapter->vlan_filter_list);
3683 INIT_LIST_HEAD(&adapter->cloud_filter_list);
3685 INIT_WORK(&adapter->reset_task, iavf_reset_task);
3686 INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
3687 INIT_WORK(&adapter->watchdog_task, iavf_watchdog_task);
3688 INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
3689 INIT_DELAYED_WORK(&adapter->init_task, iavf_init_task);
3690 schedule_delayed_work(&adapter->init_task,
3691 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3693 /* Setup the wait queue for indicating transition to down status */
3694 init_waitqueue_head(&adapter->down_waitqueue);
3699 free_netdev(netdev);
3701 pci_release_regions(pdev);
3704 pci_disable_device(pdev);
3710 * iavf_suspend - Power management suspend routine
3711 * @pdev: PCI device information struct
3714 * Called when the system (VM) is entering sleep/suspend.
3716 static int iavf_suspend(struct pci_dev *pdev, pm_message_t state)
3718 struct net_device *netdev = pci_get_drvdata(pdev);
3719 struct iavf_adapter *adapter = netdev_priv(netdev);
3722 netif_device_detach(netdev);
3724 while (test_and_set_bit(__IAVF_IN_CRITICAL_TASK,
3725 &adapter->crit_section))
3726 usleep_range(500, 1000);
3728 if (netif_running(netdev)) {
3733 iavf_free_misc_irq(adapter);
3734 iavf_reset_interrupt_capability(adapter);
3736 clear_bit(__IAVF_IN_CRITICAL_TASK, &adapter->crit_section);
3738 retval = pci_save_state(pdev);
3742 pci_disable_device(pdev);
3748 * iavf_resume - Power management resume routine
3749 * @pdev: PCI device information struct
3751 * Called when the system (VM) is resumed from sleep/suspend.
3753 static int iavf_resume(struct pci_dev *pdev)
3755 struct iavf_adapter *adapter = pci_get_drvdata(pdev);
3756 struct net_device *netdev = adapter->netdev;
3759 pci_set_power_state(pdev, PCI_D0);
3760 pci_restore_state(pdev);
3761 /* pci_restore_state clears dev->state_saved so call
3762 * pci_save_state to restore it.
3764 pci_save_state(pdev);
3766 err = pci_enable_device_mem(pdev);
3768 dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
3771 pci_set_master(pdev);
3774 err = iavf_set_interrupt_capability(adapter);
3777 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3780 err = iavf_request_misc_irq(adapter);
3783 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3787 schedule_work(&adapter->reset_task);
3789 netif_device_attach(netdev);
3794 #endif /* CONFIG_PM */
3796 * iavf_remove - Device Removal Routine
3797 * @pdev: PCI device information struct
3799 * iavf_remove is called by the PCI subsystem to alert the driver
3800 * that it should release a PCI device. The could be caused by a
3801 * Hot-Plug event, or because the driver is going to be removed from
3804 static void iavf_remove(struct pci_dev *pdev)
3806 struct net_device *netdev = pci_get_drvdata(pdev);
3807 struct iavf_adapter *adapter = netdev_priv(netdev);
3808 struct iavf_vlan_filter *vlf, *vlftmp;
3809 struct iavf_mac_filter *f, *ftmp;
3810 struct iavf_cloud_filter *cf, *cftmp;
3811 struct iavf_hw *hw = &adapter->hw;
3813 /* Indicate we are in remove and not to run reset_task */
3814 set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section);
3815 cancel_delayed_work_sync(&adapter->init_task);
3816 cancel_work_sync(&adapter->reset_task);
3817 cancel_delayed_work_sync(&adapter->client_task);
3818 if (adapter->netdev_registered) {
3819 unregister_netdev(netdev);
3820 adapter->netdev_registered = false;
3822 if (CLIENT_ALLOWED(adapter)) {
3823 err = iavf_lan_del_device(adapter);
3825 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3829 /* Shut down all the garbage mashers on the detention level */
3830 adapter->state = __IAVF_REMOVE;
3831 adapter->aq_required = 0;
3832 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3833 iavf_request_reset(adapter);
3835 /* If the FW isn't responding, kick it once, but only once. */
3836 if (!iavf_asq_done(hw)) {
3837 iavf_request_reset(adapter);
3840 iavf_free_all_tx_resources(adapter);
3841 iavf_free_all_rx_resources(adapter);
3842 iavf_misc_irq_disable(adapter);
3843 iavf_free_misc_irq(adapter);
3844 iavf_reset_interrupt_capability(adapter);
3845 iavf_free_q_vectors(adapter);
3847 if (adapter->watchdog_timer.function)
3848 del_timer_sync(&adapter->watchdog_timer);
3850 cancel_work_sync(&adapter->adminq_task);
3852 iavf_free_rss(adapter);
3854 if (hw->aq.asq.count)
3855 iavf_shutdown_adminq(hw);
3857 /* destroy the locks only once, here */
3858 mutex_destroy(&hw->aq.arq_mutex);
3859 mutex_destroy(&hw->aq.asq_mutex);
3861 iounmap(hw->hw_addr);
3862 pci_release_regions(pdev);
3863 iavf_free_all_tx_resources(adapter);
3864 iavf_free_all_rx_resources(adapter);
3865 iavf_free_queues(adapter);
3866 kfree(adapter->vf_res);
3867 spin_lock_bh(&adapter->mac_vlan_list_lock);
3868 /* If we got removed before an up/down sequence, we've got a filter
3869 * hanging out there that we need to get rid of.
3871 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3875 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
3877 list_del(&vlf->list);
3881 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3883 spin_lock_bh(&adapter->cloud_filter_list_lock);
3884 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3885 list_del(&cf->list);
3888 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3890 free_netdev(netdev);
3892 pci_disable_pcie_error_reporting(pdev);
3894 pci_disable_device(pdev);
3897 static struct pci_driver iavf_driver = {
3898 .name = iavf_driver_name,
3899 .id_table = iavf_pci_tbl,
3900 .probe = iavf_probe,
3901 .remove = iavf_remove,
3903 .suspend = iavf_suspend,
3904 .resume = iavf_resume,
3906 .shutdown = iavf_shutdown,
3910 * iavf_init_module - Driver Registration Routine
3912 * iavf_init_module is the first routine called when the driver is
3913 * loaded. All it does is register with the PCI subsystem.
3915 static int __init iavf_init_module(void)
3919 pr_info("iavf: %s - version %s\n", iavf_driver_string,
3920 iavf_driver_version);
3922 pr_info("%s\n", iavf_copyright);
3924 iavf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
3927 pr_err("%s: Failed to create workqueue\n", iavf_driver_name);
3930 ret = pci_register_driver(&iavf_driver);
3934 module_init(iavf_init_module);
3937 * iavf_exit_module - Driver Exit Cleanup Routine
3939 * iavf_exit_module is called just before the driver is removed
3942 static void __exit iavf_exit_module(void)
3944 pci_unregister_driver(&iavf_driver);
3945 destroy_workqueue(iavf_wq);
3948 module_exit(iavf_exit_module);