1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2018 Intel Corporation. */
4 /******************************************************************************
5 Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
6 ******************************************************************************/
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/types.h>
11 #include <linux/bitops.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/vmalloc.h>
16 #include <linux/string.h>
19 #include <linux/tcp.h>
20 #include <linux/sctp.h>
21 #include <linux/ipv6.h>
22 #include <linux/slab.h>
23 #include <net/checksum.h>
24 #include <net/ip6_checksum.h>
25 #include <linux/ethtool.h>
27 #include <linux/if_vlan.h>
28 #include <linux/prefetch.h>
30 #include <linux/bpf.h>
31 #include <linux/bpf_trace.h>
32 #include <linux/atomic.h>
36 const char ixgbevf_driver_name[] = "ixgbevf";
37 static const char ixgbevf_driver_string[] =
38 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
40 #define DRV_VERSION "4.1.0-k"
41 const char ixgbevf_driver_version[] = DRV_VERSION;
42 static char ixgbevf_copyright[] =
43 "Copyright (c) 2009 - 2015 Intel Corporation.";
45 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
46 [board_82599_vf] = &ixgbevf_82599_vf_info,
47 [board_82599_vf_hv] = &ixgbevf_82599_vf_hv_info,
48 [board_X540_vf] = &ixgbevf_X540_vf_info,
49 [board_X540_vf_hv] = &ixgbevf_X540_vf_hv_info,
50 [board_X550_vf] = &ixgbevf_X550_vf_info,
51 [board_X550_vf_hv] = &ixgbevf_X550_vf_hv_info,
52 [board_X550EM_x_vf] = &ixgbevf_X550EM_x_vf_info,
53 [board_X550EM_x_vf_hv] = &ixgbevf_X550EM_x_vf_hv_info,
54 [board_x550em_a_vf] = &ixgbevf_x550em_a_vf_info,
57 /* ixgbevf_pci_tbl - PCI Device ID Table
59 * Wildcard entries (PCI_ANY_ID) should come last
60 * Last entry must be all 0s
62 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
63 * Class, Class Mask, private data (not used) }
65 static const struct pci_device_id ixgbevf_pci_tbl[] = {
66 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
67 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF_HV), board_82599_vf_hv },
68 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
69 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF_HV), board_X540_vf_hv },
70 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF), board_X550_vf },
71 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF_HV), board_X550_vf_hv },
72 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF), board_X550EM_x_vf },
73 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF_HV), board_X550EM_x_vf_hv},
74 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_VF), board_x550em_a_vf },
75 /* required last entry */
78 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
80 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
81 MODULE_DESCRIPTION("Intel(R) 10 Gigabit Virtual Function Network Driver");
82 MODULE_LICENSE("GPL");
83 MODULE_VERSION(DRV_VERSION);
85 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
86 static int debug = -1;
87 module_param(debug, int, 0);
88 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
90 static struct workqueue_struct *ixgbevf_wq;
92 static void ixgbevf_service_event_schedule(struct ixgbevf_adapter *adapter)
94 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
95 !test_bit(__IXGBEVF_REMOVING, &adapter->state) &&
96 !test_and_set_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state))
97 queue_work(ixgbevf_wq, &adapter->service_task);
100 static void ixgbevf_service_event_complete(struct ixgbevf_adapter *adapter)
102 BUG_ON(!test_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state));
104 /* flush memory to make sure state is correct before next watchdog */
105 smp_mb__before_atomic();
106 clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
110 static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter);
111 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
112 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
113 static bool ixgbevf_can_reuse_rx_page(struct ixgbevf_rx_buffer *rx_buffer);
114 static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
115 struct ixgbevf_rx_buffer *old_buff);
117 static void ixgbevf_remove_adapter(struct ixgbe_hw *hw)
119 struct ixgbevf_adapter *adapter = hw->back;
124 dev_err(&adapter->pdev->dev, "Adapter removed\n");
125 if (test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
126 ixgbevf_service_event_schedule(adapter);
129 static void ixgbevf_check_remove(struct ixgbe_hw *hw, u32 reg)
133 /* The following check not only optimizes a bit by not
134 * performing a read on the status register when the
135 * register just read was a status register read that
136 * returned IXGBE_FAILED_READ_REG. It also blocks any
137 * potential recursion.
139 if (reg == IXGBE_VFSTATUS) {
140 ixgbevf_remove_adapter(hw);
143 value = ixgbevf_read_reg(hw, IXGBE_VFSTATUS);
144 if (value == IXGBE_FAILED_READ_REG)
145 ixgbevf_remove_adapter(hw);
148 u32 ixgbevf_read_reg(struct ixgbe_hw *hw, u32 reg)
150 u8 __iomem *reg_addr = READ_ONCE(hw->hw_addr);
153 if (IXGBE_REMOVED(reg_addr))
154 return IXGBE_FAILED_READ_REG;
155 value = readl(reg_addr + reg);
156 if (unlikely(value == IXGBE_FAILED_READ_REG))
157 ixgbevf_check_remove(hw, reg);
162 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
163 * @adapter: pointer to adapter struct
164 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
165 * @queue: queue to map the corresponding interrupt to
166 * @msix_vector: the vector to map to the corresponding queue
168 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
169 u8 queue, u8 msix_vector)
172 struct ixgbe_hw *hw = &adapter->hw;
174 if (direction == -1) {
176 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
177 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
180 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
182 /* Tx or Rx causes */
183 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
184 index = ((16 * (queue & 1)) + (8 * direction));
185 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
186 ivar &= ~(0xFF << index);
187 ivar |= (msix_vector << index);
188 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
192 static u64 ixgbevf_get_tx_completed(struct ixgbevf_ring *ring)
194 return ring->stats.packets;
197 static u32 ixgbevf_get_tx_pending(struct ixgbevf_ring *ring)
199 struct ixgbevf_adapter *adapter = netdev_priv(ring->netdev);
200 struct ixgbe_hw *hw = &adapter->hw;
202 u32 head = IXGBE_READ_REG(hw, IXGBE_VFTDH(ring->reg_idx));
203 u32 tail = IXGBE_READ_REG(hw, IXGBE_VFTDT(ring->reg_idx));
206 return (head < tail) ?
207 tail - head : (tail + ring->count - head);
212 static inline bool ixgbevf_check_tx_hang(struct ixgbevf_ring *tx_ring)
214 u32 tx_done = ixgbevf_get_tx_completed(tx_ring);
215 u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
216 u32 tx_pending = ixgbevf_get_tx_pending(tx_ring);
218 clear_check_for_tx_hang(tx_ring);
220 /* Check for a hung queue, but be thorough. This verifies
221 * that a transmit has been completed since the previous
222 * check AND there is at least one packet pending. The
223 * ARMED bit is set to indicate a potential hang.
225 if ((tx_done_old == tx_done) && tx_pending) {
226 /* make sure it is true for two checks in a row */
227 return test_and_set_bit(__IXGBEVF_HANG_CHECK_ARMED,
230 /* reset the countdown */
231 clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &tx_ring->state);
233 /* update completed stats and continue */
234 tx_ring->tx_stats.tx_done_old = tx_done;
239 static void ixgbevf_tx_timeout_reset(struct ixgbevf_adapter *adapter)
241 /* Do the reset outside of interrupt context */
242 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
243 set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
244 ixgbevf_service_event_schedule(adapter);
249 * ixgbevf_tx_timeout - Respond to a Tx Hang
250 * @netdev: network interface device structure
252 static void ixgbevf_tx_timeout(struct net_device *netdev)
254 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
256 ixgbevf_tx_timeout_reset(adapter);
260 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
261 * @q_vector: board private structure
262 * @tx_ring: tx ring to clean
263 * @napi_budget: Used to determine if we are in netpoll
265 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
266 struct ixgbevf_ring *tx_ring, int napi_budget)
268 struct ixgbevf_adapter *adapter = q_vector->adapter;
269 struct ixgbevf_tx_buffer *tx_buffer;
270 union ixgbe_adv_tx_desc *tx_desc;
271 unsigned int total_bytes = 0, total_packets = 0;
272 unsigned int budget = tx_ring->count / 2;
273 unsigned int i = tx_ring->next_to_clean;
275 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
278 tx_buffer = &tx_ring->tx_buffer_info[i];
279 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
283 union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
285 /* if next_to_watch is not set then there is no work pending */
289 /* prevent any other reads prior to eop_desc */
292 /* if DD is not set pending work has not been completed */
293 if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
296 /* clear next_to_watch to prevent false hangs */
297 tx_buffer->next_to_watch = NULL;
299 /* update the statistics for this packet */
300 total_bytes += tx_buffer->bytecount;
301 total_packets += tx_buffer->gso_segs;
304 if (ring_is_xdp(tx_ring))
305 page_frag_free(tx_buffer->data);
307 napi_consume_skb(tx_buffer->skb, napi_budget);
309 /* unmap skb header data */
310 dma_unmap_single(tx_ring->dev,
311 dma_unmap_addr(tx_buffer, dma),
312 dma_unmap_len(tx_buffer, len),
315 /* clear tx_buffer data */
316 dma_unmap_len_set(tx_buffer, len, 0);
318 /* unmap remaining buffers */
319 while (tx_desc != eop_desc) {
325 tx_buffer = tx_ring->tx_buffer_info;
326 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
329 /* unmap any remaining paged data */
330 if (dma_unmap_len(tx_buffer, len)) {
331 dma_unmap_page(tx_ring->dev,
332 dma_unmap_addr(tx_buffer, dma),
333 dma_unmap_len(tx_buffer, len),
335 dma_unmap_len_set(tx_buffer, len, 0);
339 /* move us one more past the eop_desc for start of next pkt */
345 tx_buffer = tx_ring->tx_buffer_info;
346 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
349 /* issue prefetch for next Tx descriptor */
352 /* update budget accounting */
354 } while (likely(budget));
357 tx_ring->next_to_clean = i;
358 u64_stats_update_begin(&tx_ring->syncp);
359 tx_ring->stats.bytes += total_bytes;
360 tx_ring->stats.packets += total_packets;
361 u64_stats_update_end(&tx_ring->syncp);
362 q_vector->tx.total_bytes += total_bytes;
363 q_vector->tx.total_packets += total_packets;
365 if (check_for_tx_hang(tx_ring) && ixgbevf_check_tx_hang(tx_ring)) {
366 struct ixgbe_hw *hw = &adapter->hw;
367 union ixgbe_adv_tx_desc *eop_desc;
369 eop_desc = tx_ring->tx_buffer_info[i].next_to_watch;
371 pr_err("Detected Tx Unit Hang%s\n"
373 " TDH, TDT <%x>, <%x>\n"
374 " next_to_use <%x>\n"
375 " next_to_clean <%x>\n"
376 "tx_buffer_info[next_to_clean]\n"
377 " next_to_watch <%p>\n"
378 " eop_desc->wb.status <%x>\n"
379 " time_stamp <%lx>\n"
381 ring_is_xdp(tx_ring) ? " XDP" : "",
382 tx_ring->queue_index,
383 IXGBE_READ_REG(hw, IXGBE_VFTDH(tx_ring->reg_idx)),
384 IXGBE_READ_REG(hw, IXGBE_VFTDT(tx_ring->reg_idx)),
385 tx_ring->next_to_use, i,
386 eop_desc, (eop_desc ? eop_desc->wb.status : 0),
387 tx_ring->tx_buffer_info[i].time_stamp, jiffies);
389 if (!ring_is_xdp(tx_ring))
390 netif_stop_subqueue(tx_ring->netdev,
391 tx_ring->queue_index);
393 /* schedule immediate reset if we believe we hung */
394 ixgbevf_tx_timeout_reset(adapter);
399 if (ring_is_xdp(tx_ring))
402 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
403 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
404 (ixgbevf_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
405 /* Make sure that anybody stopping the queue after this
406 * sees the new next_to_clean.
410 if (__netif_subqueue_stopped(tx_ring->netdev,
411 tx_ring->queue_index) &&
412 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
413 netif_wake_subqueue(tx_ring->netdev,
414 tx_ring->queue_index);
415 ++tx_ring->tx_stats.restart_queue;
423 * ixgbevf_rx_skb - Helper function to determine proper Rx method
424 * @q_vector: structure containing interrupt and ring information
425 * @skb: packet to send up
427 static void ixgbevf_rx_skb(struct ixgbevf_q_vector *q_vector,
430 napi_gro_receive(&q_vector->napi, skb);
433 #define IXGBE_RSS_L4_TYPES_MASK \
434 ((1ul << IXGBE_RXDADV_RSSTYPE_IPV4_TCP) | \
435 (1ul << IXGBE_RXDADV_RSSTYPE_IPV4_UDP) | \
436 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_TCP) | \
437 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_UDP))
439 static inline void ixgbevf_rx_hash(struct ixgbevf_ring *ring,
440 union ixgbe_adv_rx_desc *rx_desc,
445 if (!(ring->netdev->features & NETIF_F_RXHASH))
448 rss_type = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info) &
449 IXGBE_RXDADV_RSSTYPE_MASK;
454 skb_set_hash(skb, le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
455 (IXGBE_RSS_L4_TYPES_MASK & (1ul << rss_type)) ?
456 PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
460 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
461 * @ring: structure containig ring specific data
462 * @rx_desc: current Rx descriptor being processed
463 * @skb: skb currently being received and modified
465 static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
466 union ixgbe_adv_rx_desc *rx_desc,
469 skb_checksum_none_assert(skb);
471 /* Rx csum disabled */
472 if (!(ring->netdev->features & NETIF_F_RXCSUM))
475 /* if IP and error */
476 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
477 ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
478 ring->rx_stats.csum_err++;
482 if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
485 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
486 ring->rx_stats.csum_err++;
490 /* It must be a TCP or UDP packet with a valid checksum */
491 skb->ip_summed = CHECKSUM_UNNECESSARY;
495 * ixgbevf_process_skb_fields - Populate skb header fields from Rx descriptor
496 * @rx_ring: rx descriptor ring packet is being transacted on
497 * @rx_desc: pointer to the EOP Rx descriptor
498 * @skb: pointer to current skb being populated
500 * This function checks the ring, descriptor, and packet information in
501 * order to populate the checksum, VLAN, protocol, and other fields within
504 static void ixgbevf_process_skb_fields(struct ixgbevf_ring *rx_ring,
505 union ixgbe_adv_rx_desc *rx_desc,
508 ixgbevf_rx_hash(rx_ring, rx_desc, skb);
509 ixgbevf_rx_checksum(rx_ring, rx_desc, skb);
511 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
512 u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
513 unsigned long *active_vlans = netdev_priv(rx_ring->netdev);
515 if (test_bit(vid & VLAN_VID_MASK, active_vlans))
516 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
519 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
523 struct ixgbevf_rx_buffer *ixgbevf_get_rx_buffer(struct ixgbevf_ring *rx_ring,
524 const unsigned int size)
526 struct ixgbevf_rx_buffer *rx_buffer;
528 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
529 prefetchw(rx_buffer->page);
531 /* we are reusing so sync this buffer for CPU use */
532 dma_sync_single_range_for_cpu(rx_ring->dev,
534 rx_buffer->page_offset,
538 rx_buffer->pagecnt_bias--;
543 static void ixgbevf_put_rx_buffer(struct ixgbevf_ring *rx_ring,
544 struct ixgbevf_rx_buffer *rx_buffer,
547 if (ixgbevf_can_reuse_rx_page(rx_buffer)) {
548 /* hand second half of page back to the ring */
549 ixgbevf_reuse_rx_page(rx_ring, rx_buffer);
552 /* We are not reusing the buffer so unmap it and free
553 * any references we are holding to it
555 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
556 ixgbevf_rx_pg_size(rx_ring),
558 IXGBEVF_RX_DMA_ATTR);
559 __page_frag_cache_drain(rx_buffer->page,
560 rx_buffer->pagecnt_bias);
563 /* clear contents of rx_buffer */
564 rx_buffer->page = NULL;
568 * ixgbevf_is_non_eop - process handling of non-EOP buffers
569 * @rx_ring: Rx ring being processed
570 * @rx_desc: Rx descriptor for current buffer
572 * This function updates next to clean. If the buffer is an EOP buffer
573 * this function exits returning false, otherwise it will place the
574 * sk_buff in the next buffer to be chained and return true indicating
575 * that this is in fact a non-EOP buffer.
577 static bool ixgbevf_is_non_eop(struct ixgbevf_ring *rx_ring,
578 union ixgbe_adv_rx_desc *rx_desc)
580 u32 ntc = rx_ring->next_to_clean + 1;
582 /* fetch, update, and store next to clean */
583 ntc = (ntc < rx_ring->count) ? ntc : 0;
584 rx_ring->next_to_clean = ntc;
586 prefetch(IXGBEVF_RX_DESC(rx_ring, ntc));
588 if (likely(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
594 static inline unsigned int ixgbevf_rx_offset(struct ixgbevf_ring *rx_ring)
596 return ring_uses_build_skb(rx_ring) ? IXGBEVF_SKB_PAD : 0;
599 static bool ixgbevf_alloc_mapped_page(struct ixgbevf_ring *rx_ring,
600 struct ixgbevf_rx_buffer *bi)
602 struct page *page = bi->page;
605 /* since we are recycling buffers we should seldom need to alloc */
609 /* alloc new page for storage */
610 page = dev_alloc_pages(ixgbevf_rx_pg_order(rx_ring));
611 if (unlikely(!page)) {
612 rx_ring->rx_stats.alloc_rx_page_failed++;
616 /* map page for use */
617 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
618 ixgbevf_rx_pg_size(rx_ring),
619 DMA_FROM_DEVICE, IXGBEVF_RX_DMA_ATTR);
621 /* if mapping failed free memory back to system since
622 * there isn't much point in holding memory we can't use
624 if (dma_mapping_error(rx_ring->dev, dma)) {
625 __free_pages(page, ixgbevf_rx_pg_order(rx_ring));
627 rx_ring->rx_stats.alloc_rx_page_failed++;
633 bi->page_offset = ixgbevf_rx_offset(rx_ring);
634 bi->pagecnt_bias = 1;
635 rx_ring->rx_stats.alloc_rx_page++;
641 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
642 * @rx_ring: rx descriptor ring (for a specific queue) to setup buffers on
643 * @cleaned_count: number of buffers to replace
645 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
648 union ixgbe_adv_rx_desc *rx_desc;
649 struct ixgbevf_rx_buffer *bi;
650 unsigned int i = rx_ring->next_to_use;
652 /* nothing to do or no valid netdev defined */
653 if (!cleaned_count || !rx_ring->netdev)
656 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
657 bi = &rx_ring->rx_buffer_info[i];
661 if (!ixgbevf_alloc_mapped_page(rx_ring, bi))
664 /* sync the buffer for use by the device */
665 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
667 ixgbevf_rx_bufsz(rx_ring),
670 /* Refresh the desc even if pkt_addr didn't change
671 * because each write-back erases this info.
673 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
679 rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
680 bi = rx_ring->rx_buffer_info;
684 /* clear the length for the next_to_use descriptor */
685 rx_desc->wb.upper.length = 0;
688 } while (cleaned_count);
692 if (rx_ring->next_to_use != i) {
693 /* record the next descriptor to use */
694 rx_ring->next_to_use = i;
696 /* update next to alloc since we have filled the ring */
697 rx_ring->next_to_alloc = i;
699 /* Force memory writes to complete before letting h/w
700 * know there are new descriptors to fetch. (Only
701 * applicable for weak-ordered memory model archs,
705 ixgbevf_write_tail(rx_ring, i);
710 * ixgbevf_cleanup_headers - Correct corrupted or empty headers
711 * @rx_ring: rx descriptor ring packet is being transacted on
712 * @rx_desc: pointer to the EOP Rx descriptor
713 * @skb: pointer to current skb being fixed
715 * Check for corrupted packet headers caused by senders on the local L2
716 * embedded NIC switch not setting up their Tx Descriptors right. These
717 * should be very rare.
719 * Also address the case where we are pulling data in on pages only
720 * and as such no data is present in the skb header.
722 * In addition if skb is not at least 60 bytes we need to pad it so that
723 * it is large enough to qualify as a valid Ethernet frame.
725 * Returns true if an error was encountered and skb was freed.
727 static bool ixgbevf_cleanup_headers(struct ixgbevf_ring *rx_ring,
728 union ixgbe_adv_rx_desc *rx_desc,
731 /* XDP packets use error pointer so abort at this point */
735 /* verify that the packet does not have any known errors */
736 if (unlikely(ixgbevf_test_staterr(rx_desc,
737 IXGBE_RXDADV_ERR_FRAME_ERR_MASK))) {
738 struct net_device *netdev = rx_ring->netdev;
740 if (!(netdev->features & NETIF_F_RXALL)) {
741 dev_kfree_skb_any(skb);
746 /* if eth_skb_pad returns an error the skb was freed */
747 if (eth_skb_pad(skb))
754 * ixgbevf_reuse_rx_page - page flip buffer and store it back on the ring
755 * @rx_ring: rx descriptor ring to store buffers on
756 * @old_buff: donor buffer to have page reused
758 * Synchronizes page for reuse by the adapter
760 static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
761 struct ixgbevf_rx_buffer *old_buff)
763 struct ixgbevf_rx_buffer *new_buff;
764 u16 nta = rx_ring->next_to_alloc;
766 new_buff = &rx_ring->rx_buffer_info[nta];
768 /* update, and store next to alloc */
770 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
772 /* transfer page from old buffer to new buffer */
773 new_buff->page = old_buff->page;
774 new_buff->dma = old_buff->dma;
775 new_buff->page_offset = old_buff->page_offset;
776 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
779 static inline bool ixgbevf_page_is_reserved(struct page *page)
781 return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
784 static bool ixgbevf_can_reuse_rx_page(struct ixgbevf_rx_buffer *rx_buffer)
786 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
787 struct page *page = rx_buffer->page;
789 /* avoid re-using remote pages */
790 if (unlikely(ixgbevf_page_is_reserved(page)))
793 #if (PAGE_SIZE < 8192)
794 /* if we are only owner of page we can reuse it */
795 if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
798 #define IXGBEVF_LAST_OFFSET \
799 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IXGBEVF_RXBUFFER_2048)
801 if (rx_buffer->page_offset > IXGBEVF_LAST_OFFSET)
806 /* If we have drained the page fragment pool we need to update
807 * the pagecnt_bias and page count so that we fully restock the
808 * number of references the driver holds.
810 if (unlikely(!pagecnt_bias)) {
811 page_ref_add(page, USHRT_MAX);
812 rx_buffer->pagecnt_bias = USHRT_MAX;
819 * ixgbevf_add_rx_frag - Add contents of Rx buffer to sk_buff
820 * @rx_ring: rx descriptor ring to transact packets on
821 * @rx_buffer: buffer containing page to add
822 * @skb: sk_buff to place the data into
823 * @size: size of buffer to be added
825 * This function will add the data contained in rx_buffer->page to the skb.
827 static void ixgbevf_add_rx_frag(struct ixgbevf_ring *rx_ring,
828 struct ixgbevf_rx_buffer *rx_buffer,
832 #if (PAGE_SIZE < 8192)
833 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
835 unsigned int truesize = ring_uses_build_skb(rx_ring) ?
836 SKB_DATA_ALIGN(IXGBEVF_SKB_PAD + size) :
837 SKB_DATA_ALIGN(size);
839 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
840 rx_buffer->page_offset, size, truesize);
841 #if (PAGE_SIZE < 8192)
842 rx_buffer->page_offset ^= truesize;
844 rx_buffer->page_offset += truesize;
849 struct sk_buff *ixgbevf_construct_skb(struct ixgbevf_ring *rx_ring,
850 struct ixgbevf_rx_buffer *rx_buffer,
851 struct xdp_buff *xdp,
852 union ixgbe_adv_rx_desc *rx_desc)
854 unsigned int size = xdp->data_end - xdp->data;
855 #if (PAGE_SIZE < 8192)
856 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
858 unsigned int truesize = SKB_DATA_ALIGN(xdp->data_end -
859 xdp->data_hard_start);
861 unsigned int headlen;
864 /* prefetch first cache line of first page */
866 #if L1_CACHE_BYTES < 128
867 prefetch(xdp->data + L1_CACHE_BYTES);
869 /* Note, we get here by enabling legacy-rx via:
871 * ethtool --set-priv-flags <dev> legacy-rx on
873 * In this mode, we currently get 0 extra XDP headroom as
874 * opposed to having legacy-rx off, where we process XDP
875 * packets going to stack via ixgbevf_build_skb().
877 * For ixgbevf_construct_skb() mode it means that the
878 * xdp->data_meta will always point to xdp->data, since
879 * the helper cannot expand the head. Should this ever
880 * changed in future for legacy-rx mode on, then lets also
881 * add xdp->data_meta handling here.
884 /* allocate a skb to store the frags */
885 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IXGBEVF_RX_HDR_SIZE);
889 /* Determine available headroom for copy */
891 if (headlen > IXGBEVF_RX_HDR_SIZE)
892 headlen = eth_get_headlen(xdp->data, IXGBEVF_RX_HDR_SIZE);
894 /* align pull length to size of long to optimize memcpy performance */
895 memcpy(__skb_put(skb, headlen), xdp->data,
896 ALIGN(headlen, sizeof(long)));
898 /* update all of the pointers */
901 skb_add_rx_frag(skb, 0, rx_buffer->page,
902 (xdp->data + headlen) -
903 page_address(rx_buffer->page),
905 #if (PAGE_SIZE < 8192)
906 rx_buffer->page_offset ^= truesize;
908 rx_buffer->page_offset += truesize;
911 rx_buffer->pagecnt_bias++;
917 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
920 struct ixgbe_hw *hw = &adapter->hw;
922 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
925 static struct sk_buff *ixgbevf_build_skb(struct ixgbevf_ring *rx_ring,
926 struct ixgbevf_rx_buffer *rx_buffer,
927 struct xdp_buff *xdp,
928 union ixgbe_adv_rx_desc *rx_desc)
930 unsigned int metasize = xdp->data - xdp->data_meta;
931 #if (PAGE_SIZE < 8192)
932 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
934 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
935 SKB_DATA_ALIGN(xdp->data_end -
936 xdp->data_hard_start);
940 /* Prefetch first cache line of first page. If xdp->data_meta
941 * is unused, this points to xdp->data, otherwise, we likely
942 * have a consumer accessing first few bytes of meta data,
943 * and then actual data.
945 prefetch(xdp->data_meta);
946 #if L1_CACHE_BYTES < 128
947 prefetch(xdp->data_meta + L1_CACHE_BYTES);
950 /* build an skb around the page buffer */
951 skb = build_skb(xdp->data_hard_start, truesize);
955 /* update pointers within the skb to store the data */
956 skb_reserve(skb, xdp->data - xdp->data_hard_start);
957 __skb_put(skb, xdp->data_end - xdp->data);
959 skb_metadata_set(skb, metasize);
961 /* update buffer offset */
962 #if (PAGE_SIZE < 8192)
963 rx_buffer->page_offset ^= truesize;
965 rx_buffer->page_offset += truesize;
971 #define IXGBEVF_XDP_PASS 0
972 #define IXGBEVF_XDP_CONSUMED 1
973 #define IXGBEVF_XDP_TX 2
975 static int ixgbevf_xmit_xdp_ring(struct ixgbevf_ring *ring,
976 struct xdp_buff *xdp)
978 struct ixgbevf_tx_buffer *tx_buffer;
979 union ixgbe_adv_tx_desc *tx_desc;
984 len = xdp->data_end - xdp->data;
986 if (unlikely(!ixgbevf_desc_unused(ring)))
987 return IXGBEVF_XDP_CONSUMED;
989 dma = dma_map_single(ring->dev, xdp->data, len, DMA_TO_DEVICE);
990 if (dma_mapping_error(ring->dev, dma))
991 return IXGBEVF_XDP_CONSUMED;
993 /* record the location of the first descriptor for this packet */
994 i = ring->next_to_use;
995 tx_buffer = &ring->tx_buffer_info[i];
997 dma_unmap_len_set(tx_buffer, len, len);
998 dma_unmap_addr_set(tx_buffer, dma, dma);
999 tx_buffer->data = xdp->data;
1000 tx_buffer->bytecount = len;
1001 tx_buffer->gso_segs = 1;
1002 tx_buffer->protocol = 0;
1004 /* Populate minimal context descriptor that will provide for the
1005 * fact that we are expected to process Ethernet frames.
1007 if (!test_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state)) {
1008 struct ixgbe_adv_tx_context_desc *context_desc;
1010 set_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state);
1012 context_desc = IXGBEVF_TX_CTXTDESC(ring, 0);
1013 context_desc->vlan_macip_lens =
1014 cpu_to_le32(ETH_HLEN << IXGBE_ADVTXD_MACLEN_SHIFT);
1015 context_desc->seqnum_seed = 0;
1016 context_desc->type_tucmd_mlhl =
1017 cpu_to_le32(IXGBE_TXD_CMD_DEXT |
1018 IXGBE_ADVTXD_DTYP_CTXT);
1019 context_desc->mss_l4len_idx = 0;
1024 /* put descriptor type bits */
1025 cmd_type = IXGBE_ADVTXD_DTYP_DATA |
1026 IXGBE_ADVTXD_DCMD_DEXT |
1027 IXGBE_ADVTXD_DCMD_IFCS;
1028 cmd_type |= len | IXGBE_TXD_CMD;
1030 tx_desc = IXGBEVF_TX_DESC(ring, i);
1031 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1033 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1034 tx_desc->read.olinfo_status =
1035 cpu_to_le32((len << IXGBE_ADVTXD_PAYLEN_SHIFT) |
1038 /* Avoid any potential race with cleanup */
1041 /* set next_to_watch value indicating a packet is present */
1043 if (i == ring->count)
1046 tx_buffer->next_to_watch = tx_desc;
1047 ring->next_to_use = i;
1049 return IXGBEVF_XDP_TX;
1052 static struct sk_buff *ixgbevf_run_xdp(struct ixgbevf_adapter *adapter,
1053 struct ixgbevf_ring *rx_ring,
1054 struct xdp_buff *xdp)
1056 int result = IXGBEVF_XDP_PASS;
1057 struct ixgbevf_ring *xdp_ring;
1058 struct bpf_prog *xdp_prog;
1062 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
1067 act = bpf_prog_run_xdp(xdp_prog, xdp);
1072 xdp_ring = adapter->xdp_ring[rx_ring->queue_index];
1073 result = ixgbevf_xmit_xdp_ring(xdp_ring, xdp);
1074 if (result == IXGBEVF_XDP_CONSUMED)
1078 bpf_warn_invalid_xdp_action(act);
1082 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
1083 /* fallthrough -- handle aborts by dropping packet */
1085 result = IXGBEVF_XDP_CONSUMED;
1090 return ERR_PTR(-result);
1093 static void ixgbevf_rx_buffer_flip(struct ixgbevf_ring *rx_ring,
1094 struct ixgbevf_rx_buffer *rx_buffer,
1097 #if (PAGE_SIZE < 8192)
1098 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
1100 rx_buffer->page_offset ^= truesize;
1102 unsigned int truesize = ring_uses_build_skb(rx_ring) ?
1103 SKB_DATA_ALIGN(IXGBEVF_SKB_PAD + size) :
1104 SKB_DATA_ALIGN(size);
1106 rx_buffer->page_offset += truesize;
1110 static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
1111 struct ixgbevf_ring *rx_ring,
1114 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1115 struct ixgbevf_adapter *adapter = q_vector->adapter;
1116 u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
1117 struct sk_buff *skb = rx_ring->skb;
1118 bool xdp_xmit = false;
1119 struct xdp_buff xdp;
1121 xdp.rxq = &rx_ring->xdp_rxq;
1123 while (likely(total_rx_packets < budget)) {
1124 struct ixgbevf_rx_buffer *rx_buffer;
1125 union ixgbe_adv_rx_desc *rx_desc;
1128 /* return some buffers to hardware, one at a time is too slow */
1129 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
1130 ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
1134 rx_desc = IXGBEVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
1135 size = le16_to_cpu(rx_desc->wb.upper.length);
1139 /* This memory barrier is needed to keep us from reading
1140 * any other fields out of the rx_desc until we know the
1141 * RXD_STAT_DD bit is set
1145 rx_buffer = ixgbevf_get_rx_buffer(rx_ring, size);
1147 /* retrieve a buffer from the ring */
1149 xdp.data = page_address(rx_buffer->page) +
1150 rx_buffer->page_offset;
1151 xdp.data_meta = xdp.data;
1152 xdp.data_hard_start = xdp.data -
1153 ixgbevf_rx_offset(rx_ring);
1154 xdp.data_end = xdp.data + size;
1156 skb = ixgbevf_run_xdp(adapter, rx_ring, &xdp);
1160 if (PTR_ERR(skb) == -IXGBEVF_XDP_TX) {
1162 ixgbevf_rx_buffer_flip(rx_ring, rx_buffer,
1165 rx_buffer->pagecnt_bias++;
1168 total_rx_bytes += size;
1170 ixgbevf_add_rx_frag(rx_ring, rx_buffer, skb, size);
1171 } else if (ring_uses_build_skb(rx_ring)) {
1172 skb = ixgbevf_build_skb(rx_ring, rx_buffer,
1175 skb = ixgbevf_construct_skb(rx_ring, rx_buffer,
1179 /* exit if we failed to retrieve a buffer */
1181 rx_ring->rx_stats.alloc_rx_buff_failed++;
1182 rx_buffer->pagecnt_bias++;
1186 ixgbevf_put_rx_buffer(rx_ring, rx_buffer, skb);
1189 /* fetch next buffer in frame if non-eop */
1190 if (ixgbevf_is_non_eop(rx_ring, rx_desc))
1193 /* verify the packet layout is correct */
1194 if (ixgbevf_cleanup_headers(rx_ring, rx_desc, skb)) {
1199 /* probably a little skewed due to removing CRC */
1200 total_rx_bytes += skb->len;
1202 /* Workaround hardware that can't do proper VEPA multicast
1205 if ((skb->pkt_type == PACKET_BROADCAST ||
1206 skb->pkt_type == PACKET_MULTICAST) &&
1207 ether_addr_equal(rx_ring->netdev->dev_addr,
1208 eth_hdr(skb)->h_source)) {
1209 dev_kfree_skb_irq(skb);
1213 /* populate checksum, VLAN, and protocol */
1214 ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
1216 ixgbevf_rx_skb(q_vector, skb);
1218 /* reset skb pointer */
1221 /* update budget accounting */
1225 /* place incomplete frames back on ring for completion */
1229 struct ixgbevf_ring *xdp_ring =
1230 adapter->xdp_ring[rx_ring->queue_index];
1232 /* Force memory writes to complete before letting h/w
1233 * know there are new descriptors to fetch.
1236 ixgbevf_write_tail(xdp_ring, xdp_ring->next_to_use);
1239 u64_stats_update_begin(&rx_ring->syncp);
1240 rx_ring->stats.packets += total_rx_packets;
1241 rx_ring->stats.bytes += total_rx_bytes;
1242 u64_stats_update_end(&rx_ring->syncp);
1243 q_vector->rx.total_packets += total_rx_packets;
1244 q_vector->rx.total_bytes += total_rx_bytes;
1246 return total_rx_packets;
1250 * ixgbevf_poll - NAPI polling calback
1251 * @napi: napi struct with our devices info in it
1252 * @budget: amount of work driver is allowed to do this pass, in packets
1254 * This function will clean more than one or more rings associated with a
1257 static int ixgbevf_poll(struct napi_struct *napi, int budget)
1259 struct ixgbevf_q_vector *q_vector =
1260 container_of(napi, struct ixgbevf_q_vector, napi);
1261 struct ixgbevf_adapter *adapter = q_vector->adapter;
1262 struct ixgbevf_ring *ring;
1263 int per_ring_budget, work_done = 0;
1264 bool clean_complete = true;
1266 ixgbevf_for_each_ring(ring, q_vector->tx) {
1267 if (!ixgbevf_clean_tx_irq(q_vector, ring, budget))
1268 clean_complete = false;
1274 /* attempt to distribute budget to each queue fairly, but don't allow
1275 * the budget to go below 1 because we'll exit polling
1277 if (q_vector->rx.count > 1)
1278 per_ring_budget = max(budget/q_vector->rx.count, 1);
1280 per_ring_budget = budget;
1282 ixgbevf_for_each_ring(ring, q_vector->rx) {
1283 int cleaned = ixgbevf_clean_rx_irq(q_vector, ring,
1285 work_done += cleaned;
1286 if (cleaned >= per_ring_budget)
1287 clean_complete = false;
1290 /* If all work not completed, return budget and keep polling */
1291 if (!clean_complete)
1293 /* all work done, exit the polling mode */
1294 napi_complete_done(napi, work_done);
1295 if (adapter->rx_itr_setting == 1)
1296 ixgbevf_set_itr(q_vector);
1297 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
1298 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
1299 ixgbevf_irq_enable_queues(adapter,
1300 BIT(q_vector->v_idx));
1306 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
1307 * @q_vector: structure containing interrupt and ring information
1309 void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
1311 struct ixgbevf_adapter *adapter = q_vector->adapter;
1312 struct ixgbe_hw *hw = &adapter->hw;
1313 int v_idx = q_vector->v_idx;
1314 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
1316 /* set the WDIS bit to not clear the timer bits and cause an
1317 * immediate assertion of the interrupt
1319 itr_reg |= IXGBE_EITR_CNT_WDIS;
1321 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
1325 * ixgbevf_configure_msix - Configure MSI-X hardware
1326 * @adapter: board private structure
1328 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
1331 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
1333 struct ixgbevf_q_vector *q_vector;
1334 int q_vectors, v_idx;
1336 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1337 adapter->eims_enable_mask = 0;
1339 /* Populate the IVAR table and set the ITR values to the
1340 * corresponding register.
1342 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
1343 struct ixgbevf_ring *ring;
1345 q_vector = adapter->q_vector[v_idx];
1347 ixgbevf_for_each_ring(ring, q_vector->rx)
1348 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
1350 ixgbevf_for_each_ring(ring, q_vector->tx)
1351 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
1353 if (q_vector->tx.ring && !q_vector->rx.ring) {
1354 /* Tx only vector */
1355 if (adapter->tx_itr_setting == 1)
1356 q_vector->itr = IXGBE_12K_ITR;
1358 q_vector->itr = adapter->tx_itr_setting;
1360 /* Rx or Rx/Tx vector */
1361 if (adapter->rx_itr_setting == 1)
1362 q_vector->itr = IXGBE_20K_ITR;
1364 q_vector->itr = adapter->rx_itr_setting;
1367 /* add q_vector eims value to global eims_enable_mask */
1368 adapter->eims_enable_mask |= BIT(v_idx);
1370 ixgbevf_write_eitr(q_vector);
1373 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
1374 /* setup eims_other and add value to global eims_enable_mask */
1375 adapter->eims_other = BIT(v_idx);
1376 adapter->eims_enable_mask |= adapter->eims_other;
1379 enum latency_range {
1383 latency_invalid = 255
1387 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
1388 * @q_vector: structure containing interrupt and ring information
1389 * @ring_container: structure containing ring performance data
1391 * Stores a new ITR value based on packets and byte
1392 * counts during the last interrupt. The advantage of per interrupt
1393 * computation is faster updates and more accurate ITR for the current
1394 * traffic pattern. Constants in this function were computed
1395 * based on theoretical maximum wire speed and thresholds were set based
1396 * on testing data as well as attempting to minimize response time
1397 * while increasing bulk throughput.
1399 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
1400 struct ixgbevf_ring_container *ring_container)
1402 int bytes = ring_container->total_bytes;
1403 int packets = ring_container->total_packets;
1406 u8 itr_setting = ring_container->itr;
1411 /* simple throttle rate management
1412 * 0-20MB/s lowest (100000 ints/s)
1413 * 20-100MB/s low (20000 ints/s)
1414 * 100-1249MB/s bulk (12000 ints/s)
1416 /* what was last interrupt timeslice? */
1417 timepassed_us = q_vector->itr >> 2;
1418 bytes_perint = bytes / timepassed_us; /* bytes/usec */
1420 switch (itr_setting) {
1421 case lowest_latency:
1422 if (bytes_perint > 10)
1423 itr_setting = low_latency;
1426 if (bytes_perint > 20)
1427 itr_setting = bulk_latency;
1428 else if (bytes_perint <= 10)
1429 itr_setting = lowest_latency;
1432 if (bytes_perint <= 20)
1433 itr_setting = low_latency;
1437 /* clear work counters since we have the values we need */
1438 ring_container->total_bytes = 0;
1439 ring_container->total_packets = 0;
1441 /* write updated itr to ring container */
1442 ring_container->itr = itr_setting;
1445 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
1447 u32 new_itr = q_vector->itr;
1450 ixgbevf_update_itr(q_vector, &q_vector->tx);
1451 ixgbevf_update_itr(q_vector, &q_vector->rx);
1453 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
1455 switch (current_itr) {
1456 /* counts and packets in update_itr are dependent on these numbers */
1457 case lowest_latency:
1458 new_itr = IXGBE_100K_ITR;
1461 new_itr = IXGBE_20K_ITR;
1464 new_itr = IXGBE_12K_ITR;
1470 if (new_itr != q_vector->itr) {
1471 /* do an exponential smoothing */
1472 new_itr = (10 * new_itr * q_vector->itr) /
1473 ((9 * new_itr) + q_vector->itr);
1475 /* save the algorithm value here */
1476 q_vector->itr = new_itr;
1478 ixgbevf_write_eitr(q_vector);
1482 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
1484 struct ixgbevf_adapter *adapter = data;
1485 struct ixgbe_hw *hw = &adapter->hw;
1487 hw->mac.get_link_status = 1;
1489 ixgbevf_service_event_schedule(adapter);
1491 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
1497 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
1499 * @data: pointer to our q_vector struct for this interrupt vector
1501 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
1503 struct ixgbevf_q_vector *q_vector = data;
1505 /* EIAM disabled interrupts (on this vector) for us */
1506 if (q_vector->rx.ring || q_vector->tx.ring)
1507 napi_schedule_irqoff(&q_vector->napi);
1513 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1514 * @adapter: board private structure
1516 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1517 * interrupts from the kernel.
1519 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1521 struct net_device *netdev = adapter->netdev;
1522 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1523 unsigned int ri = 0, ti = 0;
1526 for (vector = 0; vector < q_vectors; vector++) {
1527 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
1528 struct msix_entry *entry = &adapter->msix_entries[vector];
1530 if (q_vector->tx.ring && q_vector->rx.ring) {
1531 snprintf(q_vector->name, sizeof(q_vector->name),
1532 "%s-TxRx-%u", netdev->name, ri++);
1534 } else if (q_vector->rx.ring) {
1535 snprintf(q_vector->name, sizeof(q_vector->name),
1536 "%s-rx-%u", netdev->name, ri++);
1537 } else if (q_vector->tx.ring) {
1538 snprintf(q_vector->name, sizeof(q_vector->name),
1539 "%s-tx-%u", netdev->name, ti++);
1541 /* skip this unused q_vector */
1544 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
1545 q_vector->name, q_vector);
1547 hw_dbg(&adapter->hw,
1548 "request_irq failed for MSIX interrupt Error: %d\n",
1550 goto free_queue_irqs;
1554 err = request_irq(adapter->msix_entries[vector].vector,
1555 &ixgbevf_msix_other, 0, netdev->name, adapter);
1557 hw_dbg(&adapter->hw, "request_irq for msix_other failed: %d\n",
1559 goto free_queue_irqs;
1567 free_irq(adapter->msix_entries[vector].vector,
1568 adapter->q_vector[vector]);
1570 /* This failure is non-recoverable - it indicates the system is
1571 * out of MSIX vector resources and the VF driver cannot run
1572 * without them. Set the number of msix vectors to zero
1573 * indicating that not enough can be allocated. The error
1574 * will be returned to the user indicating device open failed.
1575 * Any further attempts to force the driver to open will also
1576 * fail. The only way to recover is to unload the driver and
1577 * reload it again. If the system has recovered some MSIX
1578 * vectors then it may succeed.
1580 adapter->num_msix_vectors = 0;
1585 * ixgbevf_request_irq - initialize interrupts
1586 * @adapter: board private structure
1588 * Attempts to configure interrupts using the best available
1589 * capabilities of the hardware and kernel.
1591 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1593 int err = ixgbevf_request_msix_irqs(adapter);
1596 hw_dbg(&adapter->hw, "request_irq failed, Error %d\n", err);
1601 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1605 if (!adapter->msix_entries)
1608 q_vectors = adapter->num_msix_vectors;
1611 free_irq(adapter->msix_entries[i].vector, adapter);
1614 for (; i >= 0; i--) {
1615 /* free only the irqs that were actually requested */
1616 if (!adapter->q_vector[i]->rx.ring &&
1617 !adapter->q_vector[i]->tx.ring)
1620 free_irq(adapter->msix_entries[i].vector,
1621 adapter->q_vector[i]);
1626 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1627 * @adapter: board private structure
1629 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1631 struct ixgbe_hw *hw = &adapter->hw;
1634 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
1635 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1636 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
1638 IXGBE_WRITE_FLUSH(hw);
1640 for (i = 0; i < adapter->num_msix_vectors; i++)
1641 synchronize_irq(adapter->msix_entries[i].vector);
1645 * ixgbevf_irq_enable - Enable default interrupt generation settings
1646 * @adapter: board private structure
1648 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1650 struct ixgbe_hw *hw = &adapter->hw;
1652 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1653 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1654 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1658 * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
1659 * @adapter: board private structure
1660 * @ring: structure containing ring specific data
1662 * Configure the Tx descriptor ring after a reset.
1664 static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
1665 struct ixgbevf_ring *ring)
1667 struct ixgbe_hw *hw = &adapter->hw;
1668 u64 tdba = ring->dma;
1670 u32 txdctl = IXGBE_TXDCTL_ENABLE;
1671 u8 reg_idx = ring->reg_idx;
1673 /* disable queue to avoid issues while updating state */
1674 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
1675 IXGBE_WRITE_FLUSH(hw);
1677 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
1678 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
1679 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
1680 ring->count * sizeof(union ixgbe_adv_tx_desc));
1682 /* disable head writeback */
1683 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
1684 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
1686 /* enable relaxed ordering */
1687 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
1688 (IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1689 IXGBE_DCA_TXCTRL_DATA_RRO_EN));
1691 /* reset head and tail pointers */
1692 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
1693 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
1694 ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
1696 /* reset ntu and ntc to place SW in sync with hardwdare */
1697 ring->next_to_clean = 0;
1698 ring->next_to_use = 0;
1700 /* In order to avoid issues WTHRESH + PTHRESH should always be equal
1701 * to or less than the number of on chip descriptors, which is
1704 txdctl |= (8 << 16); /* WTHRESH = 8 */
1706 /* Setting PTHRESH to 32 both improves performance */
1707 txdctl |= (1u << 8) | /* HTHRESH = 1 */
1708 32; /* PTHRESH = 32 */
1710 /* reinitialize tx_buffer_info */
1711 memset(ring->tx_buffer_info, 0,
1712 sizeof(struct ixgbevf_tx_buffer) * ring->count);
1714 clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &ring->state);
1715 clear_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state);
1717 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
1719 /* poll to verify queue is enabled */
1721 usleep_range(1000, 2000);
1722 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
1723 } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
1725 hw_dbg(hw, "Could not enable Tx Queue %d\n", reg_idx);
1729 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1730 * @adapter: board private structure
1732 * Configure the Tx unit of the MAC after a reset.
1734 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1738 /* Setup the HW Tx Head and Tail descriptor pointers */
1739 for (i = 0; i < adapter->num_tx_queues; i++)
1740 ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
1741 for (i = 0; i < adapter->num_xdp_queues; i++)
1742 ixgbevf_configure_tx_ring(adapter, adapter->xdp_ring[i]);
1745 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1747 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter,
1748 struct ixgbevf_ring *ring, int index)
1750 struct ixgbe_hw *hw = &adapter->hw;
1753 srrctl = IXGBE_SRRCTL_DROP_EN;
1755 srrctl |= IXGBEVF_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT;
1756 if (ring_uses_large_buffer(ring))
1757 srrctl |= IXGBEVF_RXBUFFER_3072 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1759 srrctl |= IXGBEVF_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1760 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1762 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1765 static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1767 struct ixgbe_hw *hw = &adapter->hw;
1769 /* PSRTYPE must be initialized in 82599 */
1770 u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1771 IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1772 IXGBE_PSRTYPE_L2HDR;
1774 if (adapter->num_rx_queues > 1)
1777 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1780 #define IXGBEVF_MAX_RX_DESC_POLL 10
1781 static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1782 struct ixgbevf_ring *ring)
1784 struct ixgbe_hw *hw = &adapter->hw;
1785 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1787 u8 reg_idx = ring->reg_idx;
1789 if (IXGBE_REMOVED(hw->hw_addr))
1791 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1792 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1794 /* write value back with RXDCTL.ENABLE bit cleared */
1795 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1797 /* the hardware may take up to 100us to really disable the Rx queue */
1800 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1801 } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1804 pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
1808 static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1809 struct ixgbevf_ring *ring)
1811 struct ixgbe_hw *hw = &adapter->hw;
1812 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1814 u8 reg_idx = ring->reg_idx;
1816 if (IXGBE_REMOVED(hw->hw_addr))
1819 usleep_range(1000, 2000);
1820 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1821 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1824 pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
1829 * ixgbevf_init_rss_key - Initialize adapter RSS key
1830 * @adapter: device handle
1832 * Allocates and initializes the RSS key if it is not allocated.
1834 static inline int ixgbevf_init_rss_key(struct ixgbevf_adapter *adapter)
1838 if (!adapter->rss_key) {
1839 rss_key = kzalloc(IXGBEVF_RSS_HASH_KEY_SIZE, GFP_KERNEL);
1840 if (unlikely(!rss_key))
1843 netdev_rss_key_fill(rss_key, IXGBEVF_RSS_HASH_KEY_SIZE);
1844 adapter->rss_key = rss_key;
1850 static void ixgbevf_setup_vfmrqc(struct ixgbevf_adapter *adapter)
1852 struct ixgbe_hw *hw = &adapter->hw;
1853 u32 vfmrqc = 0, vfreta = 0;
1854 u16 rss_i = adapter->num_rx_queues;
1857 /* Fill out hash function seeds */
1858 for (i = 0; i < IXGBEVF_VFRSSRK_REGS; i++)
1859 IXGBE_WRITE_REG(hw, IXGBE_VFRSSRK(i), *(adapter->rss_key + i));
1861 for (i = 0, j = 0; i < IXGBEVF_X550_VFRETA_SIZE; i++, j++) {
1865 adapter->rss_indir_tbl[i] = j;
1867 vfreta |= j << (i & 0x3) * 8;
1869 IXGBE_WRITE_REG(hw, IXGBE_VFRETA(i >> 2), vfreta);
1874 /* Perform hash on these packet types */
1875 vfmrqc |= IXGBE_VFMRQC_RSS_FIELD_IPV4 |
1876 IXGBE_VFMRQC_RSS_FIELD_IPV4_TCP |
1877 IXGBE_VFMRQC_RSS_FIELD_IPV6 |
1878 IXGBE_VFMRQC_RSS_FIELD_IPV6_TCP;
1880 vfmrqc |= IXGBE_VFMRQC_RSSEN;
1882 IXGBE_WRITE_REG(hw, IXGBE_VFMRQC, vfmrqc);
1885 static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
1886 struct ixgbevf_ring *ring)
1888 struct ixgbe_hw *hw = &adapter->hw;
1889 union ixgbe_adv_rx_desc *rx_desc;
1890 u64 rdba = ring->dma;
1892 u8 reg_idx = ring->reg_idx;
1894 /* disable queue to avoid issues while updating state */
1895 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1896 ixgbevf_disable_rx_queue(adapter, ring);
1898 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1899 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
1900 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
1901 ring->count * sizeof(union ixgbe_adv_rx_desc));
1903 #ifndef CONFIG_SPARC
1904 /* enable relaxed ordering */
1905 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1906 IXGBE_DCA_RXCTRL_DESC_RRO_EN);
1908 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1909 IXGBE_DCA_RXCTRL_DESC_RRO_EN |
1910 IXGBE_DCA_RXCTRL_DATA_WRO_EN);
1913 /* reset head and tail pointers */
1914 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
1915 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
1916 ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
1918 /* initialize rx_buffer_info */
1919 memset(ring->rx_buffer_info, 0,
1920 sizeof(struct ixgbevf_rx_buffer) * ring->count);
1922 /* initialize Rx descriptor 0 */
1923 rx_desc = IXGBEVF_RX_DESC(ring, 0);
1924 rx_desc->wb.upper.length = 0;
1926 /* reset ntu and ntc to place SW in sync with hardwdare */
1927 ring->next_to_clean = 0;
1928 ring->next_to_use = 0;
1929 ring->next_to_alloc = 0;
1931 ixgbevf_configure_srrctl(adapter, ring, reg_idx);
1933 /* RXDCTL.RLPML does not work on 82599 */
1934 if (adapter->hw.mac.type != ixgbe_mac_82599_vf) {
1935 rxdctl &= ~(IXGBE_RXDCTL_RLPMLMASK |
1936 IXGBE_RXDCTL_RLPML_EN);
1938 #if (PAGE_SIZE < 8192)
1939 /* Limit the maximum frame size so we don't overrun the skb */
1940 if (ring_uses_build_skb(ring) &&
1941 !ring_uses_large_buffer(ring))
1942 rxdctl |= IXGBEVF_MAX_FRAME_BUILD_SKB |
1943 IXGBE_RXDCTL_RLPML_EN;
1947 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1948 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1950 ixgbevf_rx_desc_queue_enable(adapter, ring);
1951 ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
1954 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter,
1955 struct ixgbevf_ring *rx_ring)
1957 struct net_device *netdev = adapter->netdev;
1958 unsigned int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1960 /* set build_skb and buffer size flags */
1961 clear_ring_build_skb_enabled(rx_ring);
1962 clear_ring_uses_large_buffer(rx_ring);
1964 if (adapter->flags & IXGBEVF_FLAGS_LEGACY_RX)
1967 set_ring_build_skb_enabled(rx_ring);
1969 if (PAGE_SIZE < 8192) {
1970 if (max_frame <= IXGBEVF_MAX_FRAME_BUILD_SKB)
1973 set_ring_uses_large_buffer(rx_ring);
1978 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1979 * @adapter: board private structure
1981 * Configure the Rx unit of the MAC after a reset.
1983 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1985 struct ixgbe_hw *hw = &adapter->hw;
1986 struct net_device *netdev = adapter->netdev;
1989 ixgbevf_setup_psrtype(adapter);
1990 if (hw->mac.type >= ixgbe_mac_X550_vf)
1991 ixgbevf_setup_vfmrqc(adapter);
1993 spin_lock_bh(&adapter->mbx_lock);
1994 /* notify the PF of our intent to use this size of frame */
1995 ret = hw->mac.ops.set_rlpml(hw, netdev->mtu + ETH_HLEN + ETH_FCS_LEN);
1996 spin_unlock_bh(&adapter->mbx_lock);
1998 dev_err(&adapter->pdev->dev,
1999 "Failed to set MTU at %d\n", netdev->mtu);
2001 /* Setup the HW Rx Head and Tail Descriptor Pointers and
2002 * the Base and Length of the Rx Descriptor Ring
2004 for (i = 0; i < adapter->num_rx_queues; i++) {
2005 struct ixgbevf_ring *rx_ring = adapter->rx_ring[i];
2007 ixgbevf_set_rx_buffer_len(adapter, rx_ring);
2008 ixgbevf_configure_rx_ring(adapter, rx_ring);
2012 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
2013 __be16 proto, u16 vid)
2015 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2016 struct ixgbe_hw *hw = &adapter->hw;
2019 spin_lock_bh(&adapter->mbx_lock);
2021 /* add VID to filter table */
2022 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
2024 spin_unlock_bh(&adapter->mbx_lock);
2026 /* translate error return types so error makes sense */
2027 if (err == IXGBE_ERR_MBX)
2030 if (err == IXGBE_ERR_INVALID_ARGUMENT)
2033 set_bit(vid, adapter->active_vlans);
2038 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
2039 __be16 proto, u16 vid)
2041 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2042 struct ixgbe_hw *hw = &adapter->hw;
2045 spin_lock_bh(&adapter->mbx_lock);
2047 /* remove VID from filter table */
2048 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
2050 spin_unlock_bh(&adapter->mbx_lock);
2052 clear_bit(vid, adapter->active_vlans);
2057 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
2061 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
2062 ixgbevf_vlan_rx_add_vid(adapter->netdev,
2063 htons(ETH_P_8021Q), vid);
2066 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
2068 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2069 struct ixgbe_hw *hw = &adapter->hw;
2072 if (!netdev_uc_empty(netdev)) {
2073 struct netdev_hw_addr *ha;
2075 netdev_for_each_uc_addr(ha, netdev) {
2076 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
2080 /* If the list is empty then send message to PF driver to
2081 * clear all MAC VLANs on this VF.
2083 hw->mac.ops.set_uc_addr(hw, 0, NULL);
2090 * ixgbevf_set_rx_mode - Multicast and unicast set
2091 * @netdev: network interface device structure
2093 * The set_rx_method entry point is called whenever the multicast address
2094 * list, unicast address list or the network interface flags are updated.
2095 * This routine is responsible for configuring the hardware for proper
2096 * multicast mode and configuring requested unicast filters.
2098 static void ixgbevf_set_rx_mode(struct net_device *netdev)
2100 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2101 struct ixgbe_hw *hw = &adapter->hw;
2102 unsigned int flags = netdev->flags;
2105 /* request the most inclusive mode we need */
2106 if (flags & IFF_PROMISC)
2107 xcast_mode = IXGBEVF_XCAST_MODE_PROMISC;
2108 else if (flags & IFF_ALLMULTI)
2109 xcast_mode = IXGBEVF_XCAST_MODE_ALLMULTI;
2110 else if (flags & (IFF_BROADCAST | IFF_MULTICAST))
2111 xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
2113 xcast_mode = IXGBEVF_XCAST_MODE_NONE;
2115 spin_lock_bh(&adapter->mbx_lock);
2117 hw->mac.ops.update_xcast_mode(hw, xcast_mode);
2119 /* reprogram multicast list */
2120 hw->mac.ops.update_mc_addr_list(hw, netdev);
2122 ixgbevf_write_uc_addr_list(netdev);
2124 spin_unlock_bh(&adapter->mbx_lock);
2127 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
2130 struct ixgbevf_q_vector *q_vector;
2131 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2133 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
2134 q_vector = adapter->q_vector[q_idx];
2135 napi_enable(&q_vector->napi);
2139 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
2142 struct ixgbevf_q_vector *q_vector;
2143 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2145 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
2146 q_vector = adapter->q_vector[q_idx];
2147 napi_disable(&q_vector->napi);
2151 static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
2153 struct ixgbe_hw *hw = &adapter->hw;
2154 unsigned int def_q = 0;
2155 unsigned int num_tcs = 0;
2156 unsigned int num_rx_queues = adapter->num_rx_queues;
2157 unsigned int num_tx_queues = adapter->num_tx_queues;
2160 spin_lock_bh(&adapter->mbx_lock);
2162 /* fetch queue configuration from the PF */
2163 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2165 spin_unlock_bh(&adapter->mbx_lock);
2171 /* we need only one Tx queue */
2174 /* update default Tx ring register index */
2175 adapter->tx_ring[0]->reg_idx = def_q;
2177 /* we need as many queues as traffic classes */
2178 num_rx_queues = num_tcs;
2181 /* if we have a bad config abort request queue reset */
2182 if ((adapter->num_rx_queues != num_rx_queues) ||
2183 (adapter->num_tx_queues != num_tx_queues)) {
2184 /* force mailbox timeout to prevent further messages */
2185 hw->mbx.timeout = 0;
2187 /* wait for watchdog to come around and bail us out */
2188 set_bit(__IXGBEVF_QUEUE_RESET_REQUESTED, &adapter->state);
2194 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
2196 ixgbevf_configure_dcb(adapter);
2198 ixgbevf_set_rx_mode(adapter->netdev);
2200 ixgbevf_restore_vlan(adapter);
2202 ixgbevf_configure_tx(adapter);
2203 ixgbevf_configure_rx(adapter);
2206 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
2208 /* Only save pre-reset stats if there are some */
2209 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
2210 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
2211 adapter->stats.base_vfgprc;
2212 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
2213 adapter->stats.base_vfgptc;
2214 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
2215 adapter->stats.base_vfgorc;
2216 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
2217 adapter->stats.base_vfgotc;
2218 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
2219 adapter->stats.base_vfmprc;
2223 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
2225 struct ixgbe_hw *hw = &adapter->hw;
2227 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
2228 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
2229 adapter->stats.last_vfgorc |=
2230 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
2231 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
2232 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
2233 adapter->stats.last_vfgotc |=
2234 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
2235 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
2237 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
2238 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
2239 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
2240 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
2241 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
2244 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
2246 struct ixgbe_hw *hw = &adapter->hw;
2247 int api[] = { ixgbe_mbox_api_13,
2251 ixgbe_mbox_api_unknown };
2254 spin_lock_bh(&adapter->mbx_lock);
2256 while (api[idx] != ixgbe_mbox_api_unknown) {
2257 err = hw->mac.ops.negotiate_api_version(hw, api[idx]);
2263 spin_unlock_bh(&adapter->mbx_lock);
2266 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
2268 struct net_device *netdev = adapter->netdev;
2269 struct ixgbe_hw *hw = &adapter->hw;
2271 ixgbevf_configure_msix(adapter);
2273 spin_lock_bh(&adapter->mbx_lock);
2275 if (is_valid_ether_addr(hw->mac.addr))
2276 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
2278 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
2280 spin_unlock_bh(&adapter->mbx_lock);
2282 smp_mb__before_atomic();
2283 clear_bit(__IXGBEVF_DOWN, &adapter->state);
2284 ixgbevf_napi_enable_all(adapter);
2286 /* clear any pending interrupts, may auto mask */
2287 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2288 ixgbevf_irq_enable(adapter);
2290 /* enable transmits */
2291 netif_tx_start_all_queues(netdev);
2293 ixgbevf_save_reset_stats(adapter);
2294 ixgbevf_init_last_counter_stats(adapter);
2296 hw->mac.get_link_status = 1;
2297 mod_timer(&adapter->service_timer, jiffies);
2300 void ixgbevf_up(struct ixgbevf_adapter *adapter)
2302 ixgbevf_configure(adapter);
2304 ixgbevf_up_complete(adapter);
2308 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
2309 * @rx_ring: ring to free buffers from
2311 static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
2313 u16 i = rx_ring->next_to_clean;
2315 /* Free Rx ring sk_buff */
2317 dev_kfree_skb(rx_ring->skb);
2318 rx_ring->skb = NULL;
2321 /* Free all the Rx ring pages */
2322 while (i != rx_ring->next_to_alloc) {
2323 struct ixgbevf_rx_buffer *rx_buffer;
2325 rx_buffer = &rx_ring->rx_buffer_info[i];
2327 /* Invalidate cache lines that may have been written to by
2328 * device so that we avoid corrupting memory.
2330 dma_sync_single_range_for_cpu(rx_ring->dev,
2332 rx_buffer->page_offset,
2333 ixgbevf_rx_bufsz(rx_ring),
2336 /* free resources associated with mapping */
2337 dma_unmap_page_attrs(rx_ring->dev,
2339 ixgbevf_rx_pg_size(rx_ring),
2341 IXGBEVF_RX_DMA_ATTR);
2343 __page_frag_cache_drain(rx_buffer->page,
2344 rx_buffer->pagecnt_bias);
2347 if (i == rx_ring->count)
2351 rx_ring->next_to_alloc = 0;
2352 rx_ring->next_to_clean = 0;
2353 rx_ring->next_to_use = 0;
2357 * ixgbevf_clean_tx_ring - Free Tx Buffers
2358 * @tx_ring: ring to be cleaned
2360 static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
2362 u16 i = tx_ring->next_to_clean;
2363 struct ixgbevf_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
2365 while (i != tx_ring->next_to_use) {
2366 union ixgbe_adv_tx_desc *eop_desc, *tx_desc;
2368 /* Free all the Tx ring sk_buffs */
2369 if (ring_is_xdp(tx_ring))
2370 page_frag_free(tx_buffer->data);
2372 dev_kfree_skb_any(tx_buffer->skb);
2374 /* unmap skb header data */
2375 dma_unmap_single(tx_ring->dev,
2376 dma_unmap_addr(tx_buffer, dma),
2377 dma_unmap_len(tx_buffer, len),
2380 /* check for eop_desc to determine the end of the packet */
2381 eop_desc = tx_buffer->next_to_watch;
2382 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2384 /* unmap remaining buffers */
2385 while (tx_desc != eop_desc) {
2389 if (unlikely(i == tx_ring->count)) {
2391 tx_buffer = tx_ring->tx_buffer_info;
2392 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
2395 /* unmap any remaining paged data */
2396 if (dma_unmap_len(tx_buffer, len))
2397 dma_unmap_page(tx_ring->dev,
2398 dma_unmap_addr(tx_buffer, dma),
2399 dma_unmap_len(tx_buffer, len),
2403 /* move us one more past the eop_desc for start of next pkt */
2406 if (unlikely(i == tx_ring->count)) {
2408 tx_buffer = tx_ring->tx_buffer_info;
2412 /* reset next_to_use and next_to_clean */
2413 tx_ring->next_to_use = 0;
2414 tx_ring->next_to_clean = 0;
2419 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
2420 * @adapter: board private structure
2422 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
2426 for (i = 0; i < adapter->num_rx_queues; i++)
2427 ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
2431 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
2432 * @adapter: board private structure
2434 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
2438 for (i = 0; i < adapter->num_tx_queues; i++)
2439 ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
2440 for (i = 0; i < adapter->num_xdp_queues; i++)
2441 ixgbevf_clean_tx_ring(adapter->xdp_ring[i]);
2444 void ixgbevf_down(struct ixgbevf_adapter *adapter)
2446 struct net_device *netdev = adapter->netdev;
2447 struct ixgbe_hw *hw = &adapter->hw;
2450 /* signal that we are down to the interrupt handler */
2451 if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
2452 return; /* do nothing if already down */
2454 /* disable all enabled Rx queues */
2455 for (i = 0; i < adapter->num_rx_queues; i++)
2456 ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
2458 usleep_range(10000, 20000);
2460 netif_tx_stop_all_queues(netdev);
2462 /* call carrier off first to avoid false dev_watchdog timeouts */
2463 netif_carrier_off(netdev);
2464 netif_tx_disable(netdev);
2466 ixgbevf_irq_disable(adapter);
2468 ixgbevf_napi_disable_all(adapter);
2470 del_timer_sync(&adapter->service_timer);
2472 /* disable transmits in the hardware now that interrupts are off */
2473 for (i = 0; i < adapter->num_tx_queues; i++) {
2474 u8 reg_idx = adapter->tx_ring[i]->reg_idx;
2476 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2477 IXGBE_TXDCTL_SWFLSH);
2480 for (i = 0; i < adapter->num_xdp_queues; i++) {
2481 u8 reg_idx = adapter->xdp_ring[i]->reg_idx;
2483 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2484 IXGBE_TXDCTL_SWFLSH);
2487 if (!pci_channel_offline(adapter->pdev))
2488 ixgbevf_reset(adapter);
2490 ixgbevf_clean_all_tx_rings(adapter);
2491 ixgbevf_clean_all_rx_rings(adapter);
2494 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
2496 WARN_ON(in_interrupt());
2498 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
2501 ixgbevf_down(adapter);
2502 ixgbevf_up(adapter);
2504 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
2507 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
2509 struct ixgbe_hw *hw = &adapter->hw;
2510 struct net_device *netdev = adapter->netdev;
2512 if (hw->mac.ops.reset_hw(hw)) {
2513 hw_dbg(hw, "PF still resetting\n");
2515 hw->mac.ops.init_hw(hw);
2516 ixgbevf_negotiate_api(adapter);
2519 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
2520 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2521 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2524 adapter->last_reset = jiffies;
2527 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
2530 int vector_threshold;
2532 /* We'll want at least 2 (vector_threshold):
2533 * 1) TxQ[0] + RxQ[0] handler
2534 * 2) Other (Link Status Change, etc.)
2536 vector_threshold = MIN_MSIX_COUNT;
2538 /* The more we get, the more we will assign to Tx/Rx Cleanup
2539 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2540 * Right now, we simply care about how many we'll get; we'll
2541 * set them up later while requesting irq's.
2543 vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
2544 vector_threshold, vectors);
2547 dev_err(&adapter->pdev->dev,
2548 "Unable to allocate MSI-X interrupts\n");
2549 kfree(adapter->msix_entries);
2550 adapter->msix_entries = NULL;
2554 /* Adjust for only the vectors we'll use, which is minimum
2555 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
2556 * vectors we were allocated.
2558 adapter->num_msix_vectors = vectors;
2564 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
2565 * @adapter: board private structure to initialize
2567 * This is the top level queue allocation routine. The order here is very
2568 * important, starting with the "most" number of features turned on at once,
2569 * and ending with the smallest set of features. This way large combinations
2570 * can be allocated if they're turned on, and smaller combinations are the
2571 * fallthrough conditions.
2574 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
2576 struct ixgbe_hw *hw = &adapter->hw;
2577 unsigned int def_q = 0;
2578 unsigned int num_tcs = 0;
2581 /* Start with base case */
2582 adapter->num_rx_queues = 1;
2583 adapter->num_tx_queues = 1;
2584 adapter->num_xdp_queues = 0;
2586 spin_lock_bh(&adapter->mbx_lock);
2588 /* fetch queue configuration from the PF */
2589 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2591 spin_unlock_bh(&adapter->mbx_lock);
2596 /* we need as many queues as traffic classes */
2598 adapter->num_rx_queues = num_tcs;
2600 u16 rss = min_t(u16, num_online_cpus(), IXGBEVF_MAX_RSS_QUEUES);
2602 switch (hw->api_version) {
2603 case ixgbe_mbox_api_11:
2604 case ixgbe_mbox_api_12:
2605 case ixgbe_mbox_api_13:
2606 if (adapter->xdp_prog &&
2607 hw->mac.max_tx_queues == rss)
2608 rss = rss > 3 ? 2 : 1;
2610 adapter->num_rx_queues = rss;
2611 adapter->num_tx_queues = rss;
2612 adapter->num_xdp_queues = adapter->xdp_prog ? rss : 0;
2620 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2621 * @adapter: board private structure to initialize
2623 * Attempt to configure the interrupts using the best available
2624 * capabilities of the hardware and the kernel.
2626 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2628 int vector, v_budget;
2630 /* It's easy to be greedy for MSI-X vectors, but it really
2631 * doesn't do us much good if we have a lot more vectors
2632 * than CPU's. So let's be conservative and only ask for
2633 * (roughly) the same number of vectors as there are CPU's.
2634 * The default is to use pairs of vectors.
2636 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
2637 v_budget = min_t(int, v_budget, num_online_cpus());
2638 v_budget += NON_Q_VECTORS;
2640 adapter->msix_entries = kcalloc(v_budget,
2641 sizeof(struct msix_entry), GFP_KERNEL);
2642 if (!adapter->msix_entries)
2645 for (vector = 0; vector < v_budget; vector++)
2646 adapter->msix_entries[vector].entry = vector;
2648 /* A failure in MSI-X entry allocation isn't fatal, but the VF driver
2649 * does not support any other modes, so we will simply fail here. Note
2650 * that we clean up the msix_entries pointer else-where.
2652 return ixgbevf_acquire_msix_vectors(adapter, v_budget);
2655 static void ixgbevf_add_ring(struct ixgbevf_ring *ring,
2656 struct ixgbevf_ring_container *head)
2658 ring->next = head->ring;
2664 * ixgbevf_alloc_q_vector - Allocate memory for a single interrupt vector
2665 * @adapter: board private structure to initialize
2666 * @v_idx: index of vector in adapter struct
2667 * @txr_count: number of Tx rings for q vector
2668 * @txr_idx: index of first Tx ring to assign
2669 * @xdp_count: total number of XDP rings to allocate
2670 * @xdp_idx: index of first XDP ring to allocate
2671 * @rxr_count: number of Rx rings for q vector
2672 * @rxr_idx: index of first Rx ring to assign
2674 * We allocate one q_vector. If allocation fails we return -ENOMEM.
2676 static int ixgbevf_alloc_q_vector(struct ixgbevf_adapter *adapter, int v_idx,
2677 int txr_count, int txr_idx,
2678 int xdp_count, int xdp_idx,
2679 int rxr_count, int rxr_idx)
2681 struct ixgbevf_q_vector *q_vector;
2682 int reg_idx = txr_idx + xdp_idx;
2683 struct ixgbevf_ring *ring;
2684 int ring_count, size;
2686 ring_count = txr_count + xdp_count + rxr_count;
2687 size = sizeof(*q_vector) + (sizeof(*ring) * ring_count);
2689 /* allocate q_vector and rings */
2690 q_vector = kzalloc(size, GFP_KERNEL);
2694 /* initialize NAPI */
2695 netif_napi_add(adapter->netdev, &q_vector->napi, ixgbevf_poll, 64);
2697 /* tie q_vector and adapter together */
2698 adapter->q_vector[v_idx] = q_vector;
2699 q_vector->adapter = adapter;
2700 q_vector->v_idx = v_idx;
2702 /* initialize pointer to rings */
2703 ring = q_vector->ring;
2706 /* assign generic ring traits */
2707 ring->dev = &adapter->pdev->dev;
2708 ring->netdev = adapter->netdev;
2710 /* configure backlink on ring */
2711 ring->q_vector = q_vector;
2713 /* update q_vector Tx values */
2714 ixgbevf_add_ring(ring, &q_vector->tx);
2716 /* apply Tx specific ring traits */
2717 ring->count = adapter->tx_ring_count;
2718 ring->queue_index = txr_idx;
2719 ring->reg_idx = reg_idx;
2721 /* assign ring to adapter */
2722 adapter->tx_ring[txr_idx] = ring;
2724 /* update count and index */
2729 /* push pointer to next ring */
2734 /* assign generic ring traits */
2735 ring->dev = &adapter->pdev->dev;
2736 ring->netdev = adapter->netdev;
2738 /* configure backlink on ring */
2739 ring->q_vector = q_vector;
2741 /* update q_vector Tx values */
2742 ixgbevf_add_ring(ring, &q_vector->tx);
2744 /* apply Tx specific ring traits */
2745 ring->count = adapter->tx_ring_count;
2746 ring->queue_index = xdp_idx;
2747 ring->reg_idx = reg_idx;
2750 /* assign ring to adapter */
2751 adapter->xdp_ring[xdp_idx] = ring;
2753 /* update count and index */
2758 /* push pointer to next ring */
2763 /* assign generic ring traits */
2764 ring->dev = &adapter->pdev->dev;
2765 ring->netdev = adapter->netdev;
2767 /* configure backlink on ring */
2768 ring->q_vector = q_vector;
2770 /* update q_vector Rx values */
2771 ixgbevf_add_ring(ring, &q_vector->rx);
2773 /* apply Rx specific ring traits */
2774 ring->count = adapter->rx_ring_count;
2775 ring->queue_index = rxr_idx;
2776 ring->reg_idx = rxr_idx;
2778 /* assign ring to adapter */
2779 adapter->rx_ring[rxr_idx] = ring;
2781 /* update count and index */
2785 /* push pointer to next ring */
2793 * ixgbevf_free_q_vector - Free memory allocated for specific interrupt vector
2794 * @adapter: board private structure to initialize
2795 * @v_idx: index of vector in adapter struct
2797 * This function frees the memory allocated to the q_vector. In addition if
2798 * NAPI is enabled it will delete any references to the NAPI struct prior
2799 * to freeing the q_vector.
2801 static void ixgbevf_free_q_vector(struct ixgbevf_adapter *adapter, int v_idx)
2803 struct ixgbevf_q_vector *q_vector = adapter->q_vector[v_idx];
2804 struct ixgbevf_ring *ring;
2806 ixgbevf_for_each_ring(ring, q_vector->tx) {
2807 if (ring_is_xdp(ring))
2808 adapter->xdp_ring[ring->queue_index] = NULL;
2810 adapter->tx_ring[ring->queue_index] = NULL;
2813 ixgbevf_for_each_ring(ring, q_vector->rx)
2814 adapter->rx_ring[ring->queue_index] = NULL;
2816 adapter->q_vector[v_idx] = NULL;
2817 netif_napi_del(&q_vector->napi);
2819 /* ixgbevf_get_stats() might access the rings on this vector,
2820 * we must wait a grace period before freeing it.
2822 kfree_rcu(q_vector, rcu);
2826 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2827 * @adapter: board private structure to initialize
2829 * We allocate one q_vector per queue interrupt. If allocation fails we
2832 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2834 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2835 int rxr_remaining = adapter->num_rx_queues;
2836 int txr_remaining = adapter->num_tx_queues;
2837 int xdp_remaining = adapter->num_xdp_queues;
2838 int rxr_idx = 0, txr_idx = 0, xdp_idx = 0, v_idx = 0;
2841 if (q_vectors >= (rxr_remaining + txr_remaining + xdp_remaining)) {
2842 for (; rxr_remaining; v_idx++, q_vectors--) {
2843 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors);
2845 err = ixgbevf_alloc_q_vector(adapter, v_idx,
2846 0, 0, 0, 0, rqpv, rxr_idx);
2850 /* update counts and index */
2851 rxr_remaining -= rqpv;
2856 for (; q_vectors; v_idx++, q_vectors--) {
2857 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors);
2858 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors);
2859 int xqpv = DIV_ROUND_UP(xdp_remaining, q_vectors);
2861 err = ixgbevf_alloc_q_vector(adapter, v_idx,
2869 /* update counts and index */
2870 rxr_remaining -= rqpv;
2872 txr_remaining -= tqpv;
2874 xdp_remaining -= xqpv;
2883 ixgbevf_free_q_vector(adapter, v_idx);
2890 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2891 * @adapter: board private structure to initialize
2893 * This function frees the memory allocated to the q_vectors. In addition if
2894 * NAPI is enabled it will delete any references to the NAPI struct prior
2895 * to freeing the q_vector.
2897 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2899 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2903 ixgbevf_free_q_vector(adapter, q_vectors);
2908 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2909 * @adapter: board private structure
2912 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2914 if (!adapter->msix_entries)
2917 pci_disable_msix(adapter->pdev);
2918 kfree(adapter->msix_entries);
2919 adapter->msix_entries = NULL;
2923 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2924 * @adapter: board private structure to initialize
2927 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2931 /* Number of supported queues */
2932 ixgbevf_set_num_queues(adapter);
2934 err = ixgbevf_set_interrupt_capability(adapter);
2936 hw_dbg(&adapter->hw,
2937 "Unable to setup interrupt capabilities\n");
2938 goto err_set_interrupt;
2941 err = ixgbevf_alloc_q_vectors(adapter);
2943 hw_dbg(&adapter->hw, "Unable to allocate memory for queue vectors\n");
2944 goto err_alloc_q_vectors;
2947 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, Tx Queue count = %u XDP Queue count %u\n",
2948 (adapter->num_rx_queues > 1) ? "Enabled" : "Disabled",
2949 adapter->num_rx_queues, adapter->num_tx_queues,
2950 adapter->num_xdp_queues);
2952 set_bit(__IXGBEVF_DOWN, &adapter->state);
2955 err_alloc_q_vectors:
2956 ixgbevf_reset_interrupt_capability(adapter);
2962 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2963 * @adapter: board private structure to clear interrupt scheme on
2965 * We go through and clear interrupt specific resources and reset the structure
2966 * to pre-load conditions
2968 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2970 adapter->num_tx_queues = 0;
2971 adapter->num_xdp_queues = 0;
2972 adapter->num_rx_queues = 0;
2974 ixgbevf_free_q_vectors(adapter);
2975 ixgbevf_reset_interrupt_capability(adapter);
2979 * ixgbevf_sw_init - Initialize general software structures
2980 * @adapter: board private structure to initialize
2982 * ixgbevf_sw_init initializes the Adapter private data structure.
2983 * Fields are initialized based on PCI device information and
2984 * OS network device settings (MTU size).
2986 static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2988 struct ixgbe_hw *hw = &adapter->hw;
2989 struct pci_dev *pdev = adapter->pdev;
2990 struct net_device *netdev = adapter->netdev;
2993 /* PCI config space info */
2994 hw->vendor_id = pdev->vendor;
2995 hw->device_id = pdev->device;
2996 hw->revision_id = pdev->revision;
2997 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2998 hw->subsystem_device_id = pdev->subsystem_device;
3000 hw->mbx.ops.init_params(hw);
3002 if (hw->mac.type >= ixgbe_mac_X550_vf) {
3003 err = ixgbevf_init_rss_key(adapter);
3008 /* assume legacy case in which PF would only give VF 2 queues */
3009 hw->mac.max_tx_queues = 2;
3010 hw->mac.max_rx_queues = 2;
3012 /* lock to protect mailbox accesses */
3013 spin_lock_init(&adapter->mbx_lock);
3015 err = hw->mac.ops.reset_hw(hw);
3017 dev_info(&pdev->dev,
3018 "PF still in reset state. Is the PF interface up?\n");
3020 err = hw->mac.ops.init_hw(hw);
3022 pr_err("init_shared_code failed: %d\n", err);
3025 ixgbevf_negotiate_api(adapter);
3026 err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
3028 dev_info(&pdev->dev, "Error reading MAC address\n");
3029 else if (is_zero_ether_addr(adapter->hw.mac.addr))
3030 dev_info(&pdev->dev,
3031 "MAC address not assigned by administrator.\n");
3032 ether_addr_copy(netdev->dev_addr, hw->mac.addr);
3035 if (!is_valid_ether_addr(netdev->dev_addr)) {
3036 dev_info(&pdev->dev, "Assigning random MAC address\n");
3037 eth_hw_addr_random(netdev);
3038 ether_addr_copy(hw->mac.addr, netdev->dev_addr);
3039 ether_addr_copy(hw->mac.perm_addr, netdev->dev_addr);
3042 /* Enable dynamic interrupt throttling rates */
3043 adapter->rx_itr_setting = 1;
3044 adapter->tx_itr_setting = 1;
3046 /* set default ring sizes */
3047 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
3048 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
3050 set_bit(__IXGBEVF_DOWN, &adapter->state);
3057 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
3059 u32 current_counter = IXGBE_READ_REG(hw, reg); \
3060 if (current_counter < last_counter) \
3061 counter += 0x100000000LL; \
3062 last_counter = current_counter; \
3063 counter &= 0xFFFFFFFF00000000LL; \
3064 counter |= current_counter; \
3067 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
3069 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
3070 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
3071 u64 current_counter = (current_counter_msb << 32) | \
3072 current_counter_lsb; \
3073 if (current_counter < last_counter) \
3074 counter += 0x1000000000LL; \
3075 last_counter = current_counter; \
3076 counter &= 0xFFFFFFF000000000LL; \
3077 counter |= current_counter; \
3080 * ixgbevf_update_stats - Update the board statistics counters.
3081 * @adapter: board private structure
3083 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
3085 struct ixgbe_hw *hw = &adapter->hw;
3086 u64 alloc_rx_page_failed = 0, alloc_rx_buff_failed = 0;
3087 u64 alloc_rx_page = 0, hw_csum_rx_error = 0;
3090 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3091 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3094 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
3095 adapter->stats.vfgprc);
3096 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
3097 adapter->stats.vfgptc);
3098 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
3099 adapter->stats.last_vfgorc,
3100 adapter->stats.vfgorc);
3101 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
3102 adapter->stats.last_vfgotc,
3103 adapter->stats.vfgotc);
3104 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
3105 adapter->stats.vfmprc);
3107 for (i = 0; i < adapter->num_rx_queues; i++) {
3108 struct ixgbevf_ring *rx_ring = adapter->rx_ring[i];
3110 hw_csum_rx_error += rx_ring->rx_stats.csum_err;
3111 alloc_rx_page_failed += rx_ring->rx_stats.alloc_rx_page_failed;
3112 alloc_rx_buff_failed += rx_ring->rx_stats.alloc_rx_buff_failed;
3113 alloc_rx_page += rx_ring->rx_stats.alloc_rx_page;
3116 adapter->hw_csum_rx_error = hw_csum_rx_error;
3117 adapter->alloc_rx_page_failed = alloc_rx_page_failed;
3118 adapter->alloc_rx_buff_failed = alloc_rx_buff_failed;
3119 adapter->alloc_rx_page = alloc_rx_page;
3123 * ixgbevf_service_timer - Timer Call-back
3124 * @t: pointer to timer_list struct
3126 static void ixgbevf_service_timer(struct timer_list *t)
3128 struct ixgbevf_adapter *adapter = from_timer(adapter, t,
3131 /* Reset the timer */
3132 mod_timer(&adapter->service_timer, (HZ * 2) + jiffies);
3134 ixgbevf_service_event_schedule(adapter);
3137 static void ixgbevf_reset_subtask(struct ixgbevf_adapter *adapter)
3139 if (!test_and_clear_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state))
3143 /* If we're already down or resetting, just bail */
3144 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3145 test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
3146 test_bit(__IXGBEVF_RESETTING, &adapter->state)) {
3151 adapter->tx_timeout_count++;
3153 ixgbevf_reinit_locked(adapter);
3158 * ixgbevf_check_hang_subtask - check for hung queues and dropped interrupts
3159 * @adapter: pointer to the device adapter structure
3161 * This function serves two purposes. First it strobes the interrupt lines
3162 * in order to make certain interrupts are occurring. Secondly it sets the
3163 * bits needed to check for TX hangs. As a result we should immediately
3164 * determine if a hang has occurred.
3166 static void ixgbevf_check_hang_subtask(struct ixgbevf_adapter *adapter)
3168 struct ixgbe_hw *hw = &adapter->hw;
3172 /* If we're down or resetting, just bail */
3173 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3174 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3177 /* Force detection of hung controller */
3178 if (netif_carrier_ok(adapter->netdev)) {
3179 for (i = 0; i < adapter->num_tx_queues; i++)
3180 set_check_for_tx_hang(adapter->tx_ring[i]);
3181 for (i = 0; i < adapter->num_xdp_queues; i++)
3182 set_check_for_tx_hang(adapter->xdp_ring[i]);
3185 /* get one bit for every active Tx/Rx interrupt vector */
3186 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
3187 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
3189 if (qv->rx.ring || qv->tx.ring)
3193 /* Cause software interrupt to ensure rings are cleaned */
3194 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
3198 * ixgbevf_watchdog_update_link - update the link status
3199 * @adapter: pointer to the device adapter structure
3201 static void ixgbevf_watchdog_update_link(struct ixgbevf_adapter *adapter)
3203 struct ixgbe_hw *hw = &adapter->hw;
3204 u32 link_speed = adapter->link_speed;
3205 bool link_up = adapter->link_up;
3208 spin_lock_bh(&adapter->mbx_lock);
3210 err = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
3212 spin_unlock_bh(&adapter->mbx_lock);
3214 /* if check for link returns error we will need to reset */
3215 if (err && time_after(jiffies, adapter->last_reset + (10 * HZ))) {
3216 set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
3220 adapter->link_up = link_up;
3221 adapter->link_speed = link_speed;
3225 * ixgbevf_watchdog_link_is_up - update netif_carrier status and
3226 * print link up message
3227 * @adapter: pointer to the device adapter structure
3229 static void ixgbevf_watchdog_link_is_up(struct ixgbevf_adapter *adapter)
3231 struct net_device *netdev = adapter->netdev;
3233 /* only continue if link was previously down */
3234 if (netif_carrier_ok(netdev))
3237 dev_info(&adapter->pdev->dev, "NIC Link is Up %s\n",
3238 (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
3240 (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL) ?
3242 (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL) ?
3246 netif_carrier_on(netdev);
3250 * ixgbevf_watchdog_link_is_down - update netif_carrier status and
3251 * print link down message
3252 * @adapter: pointer to the adapter structure
3254 static void ixgbevf_watchdog_link_is_down(struct ixgbevf_adapter *adapter)
3256 struct net_device *netdev = adapter->netdev;
3258 adapter->link_speed = 0;
3260 /* only continue if link was up previously */
3261 if (!netif_carrier_ok(netdev))
3264 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
3266 netif_carrier_off(netdev);
3270 * ixgbevf_watchdog_subtask - worker thread to bring link up
3271 * @adapter: board private structure
3273 static void ixgbevf_watchdog_subtask(struct ixgbevf_adapter *adapter)
3275 /* if interface is down do nothing */
3276 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3277 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3280 ixgbevf_watchdog_update_link(adapter);
3282 if (adapter->link_up)
3283 ixgbevf_watchdog_link_is_up(adapter);
3285 ixgbevf_watchdog_link_is_down(adapter);
3287 ixgbevf_update_stats(adapter);
3291 * ixgbevf_service_task - manages and runs subtasks
3292 * @work: pointer to work_struct containing our data
3294 static void ixgbevf_service_task(struct work_struct *work)
3296 struct ixgbevf_adapter *adapter = container_of(work,
3297 struct ixgbevf_adapter,
3299 struct ixgbe_hw *hw = &adapter->hw;
3301 if (IXGBE_REMOVED(hw->hw_addr)) {
3302 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
3304 ixgbevf_down(adapter);
3310 ixgbevf_queue_reset_subtask(adapter);
3311 ixgbevf_reset_subtask(adapter);
3312 ixgbevf_watchdog_subtask(adapter);
3313 ixgbevf_check_hang_subtask(adapter);
3315 ixgbevf_service_event_complete(adapter);
3319 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
3320 * @tx_ring: Tx descriptor ring for a specific queue
3322 * Free all transmit software resources
3324 void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
3326 ixgbevf_clean_tx_ring(tx_ring);
3328 vfree(tx_ring->tx_buffer_info);
3329 tx_ring->tx_buffer_info = NULL;
3331 /* if not set, then don't free */
3335 dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
3338 tx_ring->desc = NULL;
3342 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
3343 * @adapter: board private structure
3345 * Free all transmit software resources
3347 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
3351 for (i = 0; i < adapter->num_tx_queues; i++)
3352 if (adapter->tx_ring[i]->desc)
3353 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
3354 for (i = 0; i < adapter->num_xdp_queues; i++)
3355 if (adapter->xdp_ring[i]->desc)
3356 ixgbevf_free_tx_resources(adapter->xdp_ring[i]);
3360 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
3361 * @tx_ring: Tx descriptor ring (for a specific queue) to setup
3363 * Return 0 on success, negative on failure
3365 int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
3367 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
3370 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
3371 tx_ring->tx_buffer_info = vmalloc(size);
3372 if (!tx_ring->tx_buffer_info)
3375 u64_stats_init(&tx_ring->syncp);
3377 /* round up to nearest 4K */
3378 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
3379 tx_ring->size = ALIGN(tx_ring->size, 4096);
3381 tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
3382 &tx_ring->dma, GFP_KERNEL);
3389 vfree(tx_ring->tx_buffer_info);
3390 tx_ring->tx_buffer_info = NULL;
3391 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit descriptor ring\n");
3396 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
3397 * @adapter: board private structure
3399 * If this function returns with an error, then it's possible one or
3400 * more of the rings is populated (while the rest are not). It is the
3401 * callers duty to clean those orphaned rings.
3403 * Return 0 on success, negative on failure
3405 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
3407 int i, j = 0, err = 0;
3409 for (i = 0; i < adapter->num_tx_queues; i++) {
3410 err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
3413 hw_dbg(&adapter->hw, "Allocation for Tx Queue %u failed\n", i);
3417 for (j = 0; j < adapter->num_xdp_queues; j++) {
3418 err = ixgbevf_setup_tx_resources(adapter->xdp_ring[j]);
3421 hw_dbg(&adapter->hw, "Allocation for XDP Queue %u failed\n", j);
3427 /* rewind the index freeing the rings as we go */
3429 ixgbevf_free_tx_resources(adapter->xdp_ring[j]);
3431 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
3437 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
3438 * @adapter: board private structure
3439 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
3441 * Returns 0 on success, negative on failure
3443 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
3444 struct ixgbevf_ring *rx_ring)
3448 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
3449 rx_ring->rx_buffer_info = vmalloc(size);
3450 if (!rx_ring->rx_buffer_info)
3453 u64_stats_init(&rx_ring->syncp);
3455 /* Round up to nearest 4K */
3456 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
3457 rx_ring->size = ALIGN(rx_ring->size, 4096);
3459 rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
3460 &rx_ring->dma, GFP_KERNEL);
3465 /* XDP RX-queue info */
3466 if (xdp_rxq_info_reg(&rx_ring->xdp_rxq, adapter->netdev,
3467 rx_ring->queue_index) < 0)
3470 rx_ring->xdp_prog = adapter->xdp_prog;
3474 vfree(rx_ring->rx_buffer_info);
3475 rx_ring->rx_buffer_info = NULL;
3476 dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
3481 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
3482 * @adapter: board private structure
3484 * If this function returns with an error, then it's possible one or
3485 * more of the rings is populated (while the rest are not). It is the
3486 * callers duty to clean those orphaned rings.
3488 * Return 0 on success, negative on failure
3490 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
3494 for (i = 0; i < adapter->num_rx_queues; i++) {
3495 err = ixgbevf_setup_rx_resources(adapter, adapter->rx_ring[i]);
3498 hw_dbg(&adapter->hw, "Allocation for Rx Queue %u failed\n", i);
3504 /* rewind the index freeing the rings as we go */
3506 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
3511 * ixgbevf_free_rx_resources - Free Rx Resources
3512 * @rx_ring: ring to clean the resources from
3514 * Free all receive software resources
3516 void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
3518 ixgbevf_clean_rx_ring(rx_ring);
3520 rx_ring->xdp_prog = NULL;
3521 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
3522 vfree(rx_ring->rx_buffer_info);
3523 rx_ring->rx_buffer_info = NULL;
3525 dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
3528 rx_ring->desc = NULL;
3532 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
3533 * @adapter: board private structure
3535 * Free all receive software resources
3537 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
3541 for (i = 0; i < adapter->num_rx_queues; i++)
3542 if (adapter->rx_ring[i]->desc)
3543 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
3547 * ixgbevf_open - Called when a network interface is made active
3548 * @netdev: network interface device structure
3550 * Returns 0 on success, negative value on failure
3552 * The open entry point is called when a network interface is made
3553 * active by the system (IFF_UP). At this point all resources needed
3554 * for transmit and receive operations are allocated, the interrupt
3555 * handler is registered with the OS, the watchdog timer is started,
3556 * and the stack is notified that the interface is ready.
3558 int ixgbevf_open(struct net_device *netdev)
3560 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3561 struct ixgbe_hw *hw = &adapter->hw;
3564 /* A previous failure to open the device because of a lack of
3565 * available MSIX vector resources may have reset the number
3566 * of msix vectors variable to zero. The only way to recover
3567 * is to unload/reload the driver and hope that the system has
3568 * been able to recover some MSIX vector resources.
3570 if (!adapter->num_msix_vectors)
3573 if (hw->adapter_stopped) {
3574 ixgbevf_reset(adapter);
3575 /* if adapter is still stopped then PF isn't up and
3576 * the VF can't start.
3578 if (hw->adapter_stopped) {
3579 err = IXGBE_ERR_MBX;
3580 pr_err("Unable to start - perhaps the PF Driver isn't up yet\n");
3581 goto err_setup_reset;
3585 /* disallow open during test */
3586 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
3589 netif_carrier_off(netdev);
3591 /* allocate transmit descriptors */
3592 err = ixgbevf_setup_all_tx_resources(adapter);
3596 /* allocate receive descriptors */
3597 err = ixgbevf_setup_all_rx_resources(adapter);
3601 ixgbevf_configure(adapter);
3603 err = ixgbevf_request_irq(adapter);
3607 /* Notify the stack of the actual queue counts. */
3608 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
3610 goto err_set_queues;
3612 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
3614 goto err_set_queues;
3616 ixgbevf_up_complete(adapter);
3621 ixgbevf_free_irq(adapter);
3623 ixgbevf_free_all_rx_resources(adapter);
3625 ixgbevf_free_all_tx_resources(adapter);
3627 ixgbevf_reset(adapter);
3634 * ixgbevf_close_suspend - actions necessary to both suspend and close flows
3635 * @adapter: the private adapter struct
3637 * This function should contain the necessary work common to both suspending
3638 * and closing of the device.
3640 static void ixgbevf_close_suspend(struct ixgbevf_adapter *adapter)
3642 ixgbevf_down(adapter);
3643 ixgbevf_free_irq(adapter);
3644 ixgbevf_free_all_tx_resources(adapter);
3645 ixgbevf_free_all_rx_resources(adapter);
3649 * ixgbevf_close - Disables a network interface
3650 * @netdev: network interface device structure
3652 * Returns 0, this is not allowed to fail
3654 * The close entry point is called when an interface is de-activated
3655 * by the OS. The hardware is still under the drivers control, but
3656 * needs to be disabled. A global MAC reset is issued to stop the
3657 * hardware, and all transmit and receive resources are freed.
3659 int ixgbevf_close(struct net_device *netdev)
3661 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3663 if (netif_device_present(netdev))
3664 ixgbevf_close_suspend(adapter);
3669 static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
3671 struct net_device *dev = adapter->netdev;
3673 if (!test_and_clear_bit(__IXGBEVF_QUEUE_RESET_REQUESTED,
3677 /* if interface is down do nothing */
3678 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3679 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3682 /* Hardware has to reinitialize queues and interrupts to
3683 * match packet buffer alignment. Unfortunately, the
3684 * hardware is not flexible enough to do this dynamically.
3688 if (netif_running(dev))
3691 ixgbevf_clear_interrupt_scheme(adapter);
3692 ixgbevf_init_interrupt_scheme(adapter);
3694 if (netif_running(dev))
3700 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
3701 u32 vlan_macip_lens, u32 type_tucmd,
3704 struct ixgbe_adv_tx_context_desc *context_desc;
3705 u16 i = tx_ring->next_to_use;
3707 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
3710 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3712 /* set bits to identify this as an advanced context descriptor */
3713 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
3715 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3716 context_desc->seqnum_seed = 0;
3717 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
3718 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3721 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
3722 struct ixgbevf_tx_buffer *first,
3725 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
3726 struct sk_buff *skb = first->skb;
3736 u32 paylen, l4_offset;
3739 if (skb->ip_summed != CHECKSUM_PARTIAL)
3742 if (!skb_is_gso(skb))
3745 err = skb_cow_head(skb, 0);
3749 if (eth_p_mpls(first->protocol))
3750 ip.hdr = skb_inner_network_header(skb);
3752 ip.hdr = skb_network_header(skb);
3753 l4.hdr = skb_checksum_start(skb);
3755 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3756 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3758 /* initialize outer IP header fields */
3759 if (ip.v4->version == 4) {
3760 unsigned char *csum_start = skb_checksum_start(skb);
3761 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
3763 /* IP header will have to cancel out any data that
3764 * is not a part of the outer IP header
3766 ip.v4->check = csum_fold(csum_partial(trans_start,
3767 csum_start - trans_start,
3769 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3772 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3773 IXGBE_TX_FLAGS_CSUM |
3774 IXGBE_TX_FLAGS_IPV4;
3776 ip.v6->payload_len = 0;
3777 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3778 IXGBE_TX_FLAGS_CSUM;
3781 /* determine offset of inner transport header */
3782 l4_offset = l4.hdr - skb->data;
3784 /* compute length of segmentation header */
3785 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
3787 /* remove payload length from inner checksum */
3788 paylen = skb->len - l4_offset;
3789 csum_replace_by_diff(&l4.tcp->check, htonl(paylen));
3791 /* update gso size and bytecount with header size */
3792 first->gso_segs = skb_shinfo(skb)->gso_segs;
3793 first->bytecount += (first->gso_segs - 1) * *hdr_len;
3795 /* mss_l4len_id: use 1 as index for TSO */
3796 mss_l4len_idx = (*hdr_len - l4_offset) << IXGBE_ADVTXD_L4LEN_SHIFT;
3797 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
3798 mss_l4len_idx |= (1u << IXGBE_ADVTXD_IDX_SHIFT);
3800 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
3801 vlan_macip_lens = l4.hdr - ip.hdr;
3802 vlan_macip_lens |= (ip.hdr - skb->data) << IXGBE_ADVTXD_MACLEN_SHIFT;
3803 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3805 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3806 type_tucmd, mss_l4len_idx);
3811 static inline bool ixgbevf_ipv6_csum_is_sctp(struct sk_buff *skb)
3813 unsigned int offset = 0;
3815 ipv6_find_hdr(skb, &offset, IPPROTO_SCTP, NULL, NULL);
3817 return offset == skb_checksum_start_offset(skb);
3820 static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
3821 struct ixgbevf_tx_buffer *first)
3823 struct sk_buff *skb = first->skb;
3824 u32 vlan_macip_lens = 0;
3827 if (skb->ip_summed != CHECKSUM_PARTIAL)
3830 switch (skb->csum_offset) {
3831 case offsetof(struct tcphdr, check):
3832 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3834 case offsetof(struct udphdr, check):
3836 case offsetof(struct sctphdr, checksum):
3837 /* validate that this is actually an SCTP request */
3838 if (((first->protocol == htons(ETH_P_IP)) &&
3839 (ip_hdr(skb)->protocol == IPPROTO_SCTP)) ||
3840 ((first->protocol == htons(ETH_P_IPV6)) &&
3841 ixgbevf_ipv6_csum_is_sctp(skb))) {
3842 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_SCTP;
3847 skb_checksum_help(skb);
3851 if (first->protocol == htons(ETH_P_IP))
3852 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3854 /* update TX checksum flag */
3855 first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
3856 vlan_macip_lens = skb_checksum_start_offset(skb) -
3857 skb_network_offset(skb);
3859 /* vlan_macip_lens: MACLEN, VLAN tag */
3860 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
3861 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3863 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, 0);
3866 static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
3868 /* set type for advanced descriptor with frame checksum insertion */
3869 __le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
3870 IXGBE_ADVTXD_DCMD_IFCS |
3871 IXGBE_ADVTXD_DCMD_DEXT);
3873 /* set HW VLAN bit if VLAN is present */
3874 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3875 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
3877 /* set segmentation enable bits for TSO/FSO */
3878 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3879 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
3884 static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
3885 u32 tx_flags, unsigned int paylen)
3887 __le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
3889 /* enable L4 checksum for TSO and TX checksum offload */
3890 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3891 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
3893 /* enble IPv4 checksum for TSO */
3894 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3895 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
3897 /* use index 1 context for TSO/FSO/FCOE */
3898 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3899 olinfo_status |= cpu_to_le32(1u << IXGBE_ADVTXD_IDX_SHIFT);
3901 /* Check Context must be set if Tx switch is enabled, which it
3902 * always is for case where virtual functions are running
3904 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
3906 tx_desc->read.olinfo_status = olinfo_status;
3909 static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
3910 struct ixgbevf_tx_buffer *first,
3913 struct sk_buff *skb = first->skb;
3914 struct ixgbevf_tx_buffer *tx_buffer;
3915 union ixgbe_adv_tx_desc *tx_desc;
3916 struct skb_frag_struct *frag;
3918 unsigned int data_len, size;
3919 u32 tx_flags = first->tx_flags;
3920 __le32 cmd_type = ixgbevf_tx_cmd_type(tx_flags);
3921 u16 i = tx_ring->next_to_use;
3923 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
3925 ixgbevf_tx_olinfo_status(tx_desc, tx_flags, skb->len - hdr_len);
3927 size = skb_headlen(skb);
3928 data_len = skb->data_len;
3930 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
3934 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
3935 if (dma_mapping_error(tx_ring->dev, dma))
3938 /* record length, and DMA address */
3939 dma_unmap_len_set(tx_buffer, len, size);
3940 dma_unmap_addr_set(tx_buffer, dma, dma);
3942 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3944 while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
3945 tx_desc->read.cmd_type_len =
3946 cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
3950 if (i == tx_ring->count) {
3951 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3954 tx_desc->read.olinfo_status = 0;
3956 dma += IXGBE_MAX_DATA_PER_TXD;
3957 size -= IXGBE_MAX_DATA_PER_TXD;
3959 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3962 if (likely(!data_len))
3965 tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
3969 if (i == tx_ring->count) {
3970 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3973 tx_desc->read.olinfo_status = 0;
3975 size = skb_frag_size(frag);
3978 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
3981 tx_buffer = &tx_ring->tx_buffer_info[i];
3984 /* write last descriptor with RS and EOP bits */
3985 cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
3986 tx_desc->read.cmd_type_len = cmd_type;
3988 /* set the timestamp */
3989 first->time_stamp = jiffies;
3991 /* Force memory writes to complete before letting h/w know there
3992 * are new descriptors to fetch. (Only applicable for weak-ordered
3993 * memory model archs, such as IA-64).
3995 * We also need this memory barrier (wmb) to make certain all of the
3996 * status bits have been updated before next_to_watch is written.
4000 /* set next_to_watch value indicating a packet is present */
4001 first->next_to_watch = tx_desc;
4004 if (i == tx_ring->count)
4007 tx_ring->next_to_use = i;
4009 /* notify HW of packet */
4010 ixgbevf_write_tail(tx_ring, i);
4014 dev_err(tx_ring->dev, "TX DMA map failed\n");
4015 tx_buffer = &tx_ring->tx_buffer_info[i];
4017 /* clear dma mappings for failed tx_buffer_info map */
4018 while (tx_buffer != first) {
4019 if (dma_unmap_len(tx_buffer, len))
4020 dma_unmap_page(tx_ring->dev,
4021 dma_unmap_addr(tx_buffer, dma),
4022 dma_unmap_len(tx_buffer, len),
4024 dma_unmap_len_set(tx_buffer, len, 0);
4027 i += tx_ring->count;
4028 tx_buffer = &tx_ring->tx_buffer_info[i];
4031 if (dma_unmap_len(tx_buffer, len))
4032 dma_unmap_single(tx_ring->dev,
4033 dma_unmap_addr(tx_buffer, dma),
4034 dma_unmap_len(tx_buffer, len),
4036 dma_unmap_len_set(tx_buffer, len, 0);
4038 dev_kfree_skb_any(tx_buffer->skb);
4039 tx_buffer->skb = NULL;
4041 tx_ring->next_to_use = i;
4044 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
4046 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
4047 /* Herbert's original patch had:
4048 * smp_mb__after_netif_stop_queue();
4049 * but since that doesn't exist yet, just open code it.
4053 /* We need to check again in a case another CPU has just
4054 * made room available.
4056 if (likely(ixgbevf_desc_unused(tx_ring) < size))
4059 /* A reprieve! - use start_queue because it doesn't call schedule */
4060 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
4061 ++tx_ring->tx_stats.restart_queue;
4066 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
4068 if (likely(ixgbevf_desc_unused(tx_ring) >= size))
4070 return __ixgbevf_maybe_stop_tx(tx_ring, size);
4073 static int ixgbevf_xmit_frame_ring(struct sk_buff *skb,
4074 struct ixgbevf_ring *tx_ring)
4076 struct ixgbevf_tx_buffer *first;
4079 u16 count = TXD_USE_COUNT(skb_headlen(skb));
4080 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
4084 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
4086 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
4087 dev_kfree_skb_any(skb);
4088 return NETDEV_TX_OK;
4091 /* need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
4092 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
4093 * + 2 desc gap to keep tail from touching head,
4094 * + 1 desc for context descriptor,
4095 * otherwise try next time
4097 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
4098 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
4099 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
4101 count += skb_shinfo(skb)->nr_frags;
4103 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
4104 tx_ring->tx_stats.tx_busy++;
4105 return NETDEV_TX_BUSY;
4108 /* record the location of the first descriptor for this packet */
4109 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4111 first->bytecount = skb->len;
4112 first->gso_segs = 1;
4114 if (skb_vlan_tag_present(skb)) {
4115 tx_flags |= skb_vlan_tag_get(skb);
4116 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
4117 tx_flags |= IXGBE_TX_FLAGS_VLAN;
4120 /* record initial flags and protocol */
4121 first->tx_flags = tx_flags;
4122 first->protocol = vlan_get_protocol(skb);
4124 tso = ixgbevf_tso(tx_ring, first, &hdr_len);
4128 ixgbevf_tx_csum(tx_ring, first);
4130 ixgbevf_tx_map(tx_ring, first, hdr_len);
4132 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
4134 return NETDEV_TX_OK;
4137 dev_kfree_skb_any(first->skb);
4140 return NETDEV_TX_OK;
4143 static netdev_tx_t ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
4145 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4146 struct ixgbevf_ring *tx_ring;
4148 if (skb->len <= 0) {
4149 dev_kfree_skb_any(skb);
4150 return NETDEV_TX_OK;
4153 /* The minimum packet size for olinfo paylen is 17 so pad the skb
4154 * in order to meet this minimum size requirement.
4156 if (skb->len < 17) {
4157 if (skb_padto(skb, 17))
4158 return NETDEV_TX_OK;
4162 tx_ring = adapter->tx_ring[skb->queue_mapping];
4163 return ixgbevf_xmit_frame_ring(skb, tx_ring);
4167 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
4168 * @netdev: network interface device structure
4169 * @p: pointer to an address structure
4171 * Returns 0 on success, negative on failure
4173 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
4175 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4176 struct ixgbe_hw *hw = &adapter->hw;
4177 struct sockaddr *addr = p;
4180 if (!is_valid_ether_addr(addr->sa_data))
4181 return -EADDRNOTAVAIL;
4183 spin_lock_bh(&adapter->mbx_lock);
4185 err = hw->mac.ops.set_rar(hw, 0, addr->sa_data, 0);
4187 spin_unlock_bh(&adapter->mbx_lock);
4192 ether_addr_copy(hw->mac.addr, addr->sa_data);
4193 ether_addr_copy(hw->mac.perm_addr, addr->sa_data);
4194 ether_addr_copy(netdev->dev_addr, addr->sa_data);
4200 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
4201 * @netdev: network interface device structure
4202 * @new_mtu: new value for maximum frame size
4204 * Returns 0 on success, negative on failure
4206 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
4208 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4209 struct ixgbe_hw *hw = &adapter->hw;
4210 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
4213 /* prevent MTU being changed to a size unsupported by XDP */
4214 if (adapter->xdp_prog) {
4215 dev_warn(&adapter->pdev->dev, "MTU cannot be changed while XDP program is loaded\n");
4219 spin_lock_bh(&adapter->mbx_lock);
4220 /* notify the PF of our intent to use this size of frame */
4221 ret = hw->mac.ops.set_rlpml(hw, max_frame);
4222 spin_unlock_bh(&adapter->mbx_lock);
4226 hw_dbg(hw, "changing MTU from %d to %d\n",
4227 netdev->mtu, new_mtu);
4229 /* must set new MTU before calling down or up */
4230 netdev->mtu = new_mtu;
4232 if (netif_running(netdev))
4233 ixgbevf_reinit_locked(adapter);
4238 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
4240 struct net_device *netdev = pci_get_drvdata(pdev);
4241 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4247 netif_device_detach(netdev);
4249 if (netif_running(netdev))
4250 ixgbevf_close_suspend(adapter);
4252 ixgbevf_clear_interrupt_scheme(adapter);
4256 retval = pci_save_state(pdev);
4261 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
4262 pci_disable_device(pdev);
4268 static int ixgbevf_resume(struct pci_dev *pdev)
4270 struct net_device *netdev = pci_get_drvdata(pdev);
4271 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4274 pci_restore_state(pdev);
4275 /* pci_restore_state clears dev->state_saved so call
4276 * pci_save_state to restore it.
4278 pci_save_state(pdev);
4280 err = pci_enable_device_mem(pdev);
4282 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
4286 adapter->hw.hw_addr = adapter->io_addr;
4287 smp_mb__before_atomic();
4288 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
4289 pci_set_master(pdev);
4291 ixgbevf_reset(adapter);
4294 err = ixgbevf_init_interrupt_scheme(adapter);
4295 if (!err && netif_running(netdev))
4296 err = ixgbevf_open(netdev);
4301 netif_device_attach(netdev);
4306 #endif /* CONFIG_PM */
4307 static void ixgbevf_shutdown(struct pci_dev *pdev)
4309 ixgbevf_suspend(pdev, PMSG_SUSPEND);
4312 static void ixgbevf_get_tx_ring_stats(struct rtnl_link_stats64 *stats,
4313 const struct ixgbevf_ring *ring)
4320 start = u64_stats_fetch_begin_irq(&ring->syncp);
4321 bytes = ring->stats.bytes;
4322 packets = ring->stats.packets;
4323 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4324 stats->tx_bytes += bytes;
4325 stats->tx_packets += packets;
4329 static void ixgbevf_get_stats(struct net_device *netdev,
4330 struct rtnl_link_stats64 *stats)
4332 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4335 const struct ixgbevf_ring *ring;
4338 ixgbevf_update_stats(adapter);
4340 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
4343 for (i = 0; i < adapter->num_rx_queues; i++) {
4344 ring = adapter->rx_ring[i];
4346 start = u64_stats_fetch_begin_irq(&ring->syncp);
4347 bytes = ring->stats.bytes;
4348 packets = ring->stats.packets;
4349 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4350 stats->rx_bytes += bytes;
4351 stats->rx_packets += packets;
4354 for (i = 0; i < adapter->num_tx_queues; i++) {
4355 ring = adapter->tx_ring[i];
4356 ixgbevf_get_tx_ring_stats(stats, ring);
4359 for (i = 0; i < adapter->num_xdp_queues; i++) {
4360 ring = adapter->xdp_ring[i];
4361 ixgbevf_get_tx_ring_stats(stats, ring);
4366 #define IXGBEVF_MAX_MAC_HDR_LEN 127
4367 #define IXGBEVF_MAX_NETWORK_HDR_LEN 511
4369 static netdev_features_t
4370 ixgbevf_features_check(struct sk_buff *skb, struct net_device *dev,
4371 netdev_features_t features)
4373 unsigned int network_hdr_len, mac_hdr_len;
4375 /* Make certain the headers can be described by a context descriptor */
4376 mac_hdr_len = skb_network_header(skb) - skb->data;
4377 if (unlikely(mac_hdr_len > IXGBEVF_MAX_MAC_HDR_LEN))
4378 return features & ~(NETIF_F_HW_CSUM |
4380 NETIF_F_HW_VLAN_CTAG_TX |
4384 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
4385 if (unlikely(network_hdr_len > IXGBEVF_MAX_NETWORK_HDR_LEN))
4386 return features & ~(NETIF_F_HW_CSUM |
4391 /* We can only support IPV4 TSO in tunnels if we can mangle the
4392 * inner IP ID field, so strip TSO if MANGLEID is not supported.
4394 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
4395 features &= ~NETIF_F_TSO;
4400 static int ixgbevf_xdp_setup(struct net_device *dev, struct bpf_prog *prog)
4402 int i, frame_size = dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4403 struct ixgbevf_adapter *adapter = netdev_priv(dev);
4404 struct bpf_prog *old_prog;
4406 /* verify ixgbevf ring attributes are sufficient for XDP */
4407 for (i = 0; i < adapter->num_rx_queues; i++) {
4408 struct ixgbevf_ring *ring = adapter->rx_ring[i];
4410 if (frame_size > ixgbevf_rx_bufsz(ring))
4414 old_prog = xchg(&adapter->xdp_prog, prog);
4416 /* If transitioning XDP modes reconfigure rings */
4417 if (!!prog != !!old_prog) {
4418 /* Hardware has to reinitialize queues and interrupts to
4419 * match packet buffer alignment. Unfortunately, the
4420 * hardware is not flexible enough to do this dynamically.
4422 if (netif_running(dev))
4425 ixgbevf_clear_interrupt_scheme(adapter);
4426 ixgbevf_init_interrupt_scheme(adapter);
4428 if (netif_running(dev))
4431 for (i = 0; i < adapter->num_rx_queues; i++)
4432 xchg(&adapter->rx_ring[i]->xdp_prog, adapter->xdp_prog);
4436 bpf_prog_put(old_prog);
4441 static int ixgbevf_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4443 struct ixgbevf_adapter *adapter = netdev_priv(dev);
4445 switch (xdp->command) {
4446 case XDP_SETUP_PROG:
4447 return ixgbevf_xdp_setup(dev, xdp->prog);
4448 case XDP_QUERY_PROG:
4449 xdp->prog_id = adapter->xdp_prog ?
4450 adapter->xdp_prog->aux->id : 0;
4457 static const struct net_device_ops ixgbevf_netdev_ops = {
4458 .ndo_open = ixgbevf_open,
4459 .ndo_stop = ixgbevf_close,
4460 .ndo_start_xmit = ixgbevf_xmit_frame,
4461 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
4462 .ndo_get_stats64 = ixgbevf_get_stats,
4463 .ndo_validate_addr = eth_validate_addr,
4464 .ndo_set_mac_address = ixgbevf_set_mac,
4465 .ndo_change_mtu = ixgbevf_change_mtu,
4466 .ndo_tx_timeout = ixgbevf_tx_timeout,
4467 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
4468 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
4469 .ndo_features_check = ixgbevf_features_check,
4470 .ndo_bpf = ixgbevf_xdp,
4473 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
4475 dev->netdev_ops = &ixgbevf_netdev_ops;
4476 ixgbevf_set_ethtool_ops(dev);
4477 dev->watchdog_timeo = 5 * HZ;
4481 * ixgbevf_probe - Device Initialization Routine
4482 * @pdev: PCI device information struct
4483 * @ent: entry in ixgbevf_pci_tbl
4485 * Returns 0 on success, negative on failure
4487 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
4488 * The OS initialization, configuring of the adapter private structure,
4489 * and a hardware reset occur.
4491 static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4493 struct net_device *netdev;
4494 struct ixgbevf_adapter *adapter = NULL;
4495 struct ixgbe_hw *hw = NULL;
4496 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
4497 int err, pci_using_dac;
4498 bool disable_dev = false;
4500 err = pci_enable_device(pdev);
4504 if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
4507 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
4509 dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
4515 err = pci_request_regions(pdev, ixgbevf_driver_name);
4517 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
4521 pci_set_master(pdev);
4523 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
4527 goto err_alloc_etherdev;
4530 SET_NETDEV_DEV(netdev, &pdev->dev);
4532 adapter = netdev_priv(netdev);
4534 adapter->netdev = netdev;
4535 adapter->pdev = pdev;
4538 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4540 /* call save state here in standalone driver because it relies on
4541 * adapter struct to exist, and needs to call netdev_priv
4543 pci_save_state(pdev);
4545 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4546 pci_resource_len(pdev, 0));
4547 adapter->io_addr = hw->hw_addr;
4553 ixgbevf_assign_netdev_ops(netdev);
4556 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
4557 hw->mac.type = ii->mac;
4559 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
4560 sizeof(struct ixgbe_mbx_operations));
4562 /* setup the private structure */
4563 err = ixgbevf_sw_init(adapter);
4567 /* The HW MAC address was set and/or determined in sw_init */
4568 if (!is_valid_ether_addr(netdev->dev_addr)) {
4569 pr_err("invalid MAC address\n");
4574 netdev->hw_features = NETIF_F_SG |
4581 #define IXGBEVF_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
4582 NETIF_F_GSO_GRE_CSUM | \
4583 NETIF_F_GSO_IPXIP4 | \
4584 NETIF_F_GSO_IPXIP6 | \
4585 NETIF_F_GSO_UDP_TUNNEL | \
4586 NETIF_F_GSO_UDP_TUNNEL_CSUM)
4588 netdev->gso_partial_features = IXGBEVF_GSO_PARTIAL_FEATURES;
4589 netdev->hw_features |= NETIF_F_GSO_PARTIAL |
4590 IXGBEVF_GSO_PARTIAL_FEATURES;
4592 netdev->features = netdev->hw_features;
4595 netdev->features |= NETIF_F_HIGHDMA;
4597 netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
4598 netdev->mpls_features |= NETIF_F_SG |
4602 netdev->mpls_features |= IXGBEVF_GSO_PARTIAL_FEATURES;
4603 netdev->hw_enc_features |= netdev->vlan_features;
4605 /* set this bit last since it cannot be part of vlan_features */
4606 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4607 NETIF_F_HW_VLAN_CTAG_RX |
4608 NETIF_F_HW_VLAN_CTAG_TX;
4610 netdev->priv_flags |= IFF_UNICAST_FLT;
4612 /* MTU range: 68 - 1504 or 9710 */
4613 netdev->min_mtu = ETH_MIN_MTU;
4614 switch (adapter->hw.api_version) {
4615 case ixgbe_mbox_api_11:
4616 case ixgbe_mbox_api_12:
4617 case ixgbe_mbox_api_13:
4618 netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4619 (ETH_HLEN + ETH_FCS_LEN);
4622 if (adapter->hw.mac.type != ixgbe_mac_82599_vf)
4623 netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4624 (ETH_HLEN + ETH_FCS_LEN);
4626 netdev->max_mtu = ETH_DATA_LEN + ETH_FCS_LEN;
4630 if (IXGBE_REMOVED(hw->hw_addr)) {
4635 timer_setup(&adapter->service_timer, ixgbevf_service_timer, 0);
4637 INIT_WORK(&adapter->service_task, ixgbevf_service_task);
4638 set_bit(__IXGBEVF_SERVICE_INITED, &adapter->state);
4639 clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
4641 err = ixgbevf_init_interrupt_scheme(adapter);
4645 strcpy(netdev->name, "eth%d");
4647 err = register_netdev(netdev);
4651 pci_set_drvdata(pdev, netdev);
4652 netif_carrier_off(netdev);
4654 ixgbevf_init_last_counter_stats(adapter);
4656 /* print the VF info */
4657 dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
4658 dev_info(&pdev->dev, "MAC: %d\n", hw->mac.type);
4660 switch (hw->mac.type) {
4661 case ixgbe_mac_X550_vf:
4662 dev_info(&pdev->dev, "Intel(R) X550 Virtual Function\n");
4664 case ixgbe_mac_X540_vf:
4665 dev_info(&pdev->dev, "Intel(R) X540 Virtual Function\n");
4667 case ixgbe_mac_82599_vf:
4669 dev_info(&pdev->dev, "Intel(R) 82599 Virtual Function\n");
4676 ixgbevf_clear_interrupt_scheme(adapter);
4678 ixgbevf_reset_interrupt_capability(adapter);
4679 iounmap(adapter->io_addr);
4680 kfree(adapter->rss_key);
4682 disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4683 free_netdev(netdev);
4685 pci_release_regions(pdev);
4688 if (!adapter || disable_dev)
4689 pci_disable_device(pdev);
4694 * ixgbevf_remove - Device Removal Routine
4695 * @pdev: PCI device information struct
4697 * ixgbevf_remove is called by the PCI subsystem to alert the driver
4698 * that it should release a PCI device. The could be caused by a
4699 * Hot-Plug event, or because the driver is going to be removed from
4702 static void ixgbevf_remove(struct pci_dev *pdev)
4704 struct net_device *netdev = pci_get_drvdata(pdev);
4705 struct ixgbevf_adapter *adapter;
4711 adapter = netdev_priv(netdev);
4713 set_bit(__IXGBEVF_REMOVING, &adapter->state);
4714 cancel_work_sync(&adapter->service_task);
4716 if (netdev->reg_state == NETREG_REGISTERED)
4717 unregister_netdev(netdev);
4719 ixgbevf_clear_interrupt_scheme(adapter);
4720 ixgbevf_reset_interrupt_capability(adapter);
4722 iounmap(adapter->io_addr);
4723 pci_release_regions(pdev);
4725 hw_dbg(&adapter->hw, "Remove complete\n");
4727 kfree(adapter->rss_key);
4728 disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4729 free_netdev(netdev);
4732 pci_disable_device(pdev);
4736 * ixgbevf_io_error_detected - called when PCI error is detected
4737 * @pdev: Pointer to PCI device
4738 * @state: The current pci connection state
4740 * This function is called after a PCI bus error affecting
4741 * this device has been detected.
4743 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
4744 pci_channel_state_t state)
4746 struct net_device *netdev = pci_get_drvdata(pdev);
4747 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4749 if (!test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
4750 return PCI_ERS_RESULT_DISCONNECT;
4753 netif_device_detach(netdev);
4755 if (netif_running(netdev))
4756 ixgbevf_close_suspend(adapter);
4758 if (state == pci_channel_io_perm_failure) {
4760 return PCI_ERS_RESULT_DISCONNECT;
4763 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
4764 pci_disable_device(pdev);
4767 /* Request a slot slot reset. */
4768 return PCI_ERS_RESULT_NEED_RESET;
4772 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
4773 * @pdev: Pointer to PCI device
4775 * Restart the card from scratch, as if from a cold-boot. Implementation
4776 * resembles the first-half of the ixgbevf_resume routine.
4778 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
4780 struct net_device *netdev = pci_get_drvdata(pdev);
4781 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4783 if (pci_enable_device_mem(pdev)) {
4785 "Cannot re-enable PCI device after reset.\n");
4786 return PCI_ERS_RESULT_DISCONNECT;
4789 adapter->hw.hw_addr = adapter->io_addr;
4790 smp_mb__before_atomic();
4791 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
4792 pci_set_master(pdev);
4794 ixgbevf_reset(adapter);
4796 return PCI_ERS_RESULT_RECOVERED;
4800 * ixgbevf_io_resume - called when traffic can start flowing again.
4801 * @pdev: Pointer to PCI device
4803 * This callback is called when the error recovery driver tells us that
4804 * its OK to resume normal operation. Implementation resembles the
4805 * second-half of the ixgbevf_resume routine.
4807 static void ixgbevf_io_resume(struct pci_dev *pdev)
4809 struct net_device *netdev = pci_get_drvdata(pdev);
4812 if (netif_running(netdev))
4813 ixgbevf_open(netdev);
4815 netif_device_attach(netdev);
4819 /* PCI Error Recovery (ERS) */
4820 static const struct pci_error_handlers ixgbevf_err_handler = {
4821 .error_detected = ixgbevf_io_error_detected,
4822 .slot_reset = ixgbevf_io_slot_reset,
4823 .resume = ixgbevf_io_resume,
4826 static struct pci_driver ixgbevf_driver = {
4827 .name = ixgbevf_driver_name,
4828 .id_table = ixgbevf_pci_tbl,
4829 .probe = ixgbevf_probe,
4830 .remove = ixgbevf_remove,
4832 /* Power Management Hooks */
4833 .suspend = ixgbevf_suspend,
4834 .resume = ixgbevf_resume,
4836 .shutdown = ixgbevf_shutdown,
4837 .err_handler = &ixgbevf_err_handler
4841 * ixgbevf_init_module - Driver Registration Routine
4843 * ixgbevf_init_module is the first routine called when the driver is
4844 * loaded. All it does is register with the PCI subsystem.
4846 static int __init ixgbevf_init_module(void)
4848 pr_info("%s - version %s\n", ixgbevf_driver_string,
4849 ixgbevf_driver_version);
4851 pr_info("%s\n", ixgbevf_copyright);
4852 ixgbevf_wq = create_singlethread_workqueue(ixgbevf_driver_name);
4854 pr_err("%s: Failed to create workqueue\n", ixgbevf_driver_name);
4858 return pci_register_driver(&ixgbevf_driver);
4861 module_init(ixgbevf_init_module);
4864 * ixgbevf_exit_module - Driver Exit Cleanup Routine
4866 * ixgbevf_exit_module is called just before the driver is removed
4869 static void __exit ixgbevf_exit_module(void)
4871 pci_unregister_driver(&ixgbevf_driver);
4873 destroy_workqueue(ixgbevf_wq);
4880 * ixgbevf_get_hw_dev_name - return device name string
4881 * used by hardware layer to print debugging information
4882 * @hw: pointer to private hardware struct
4884 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
4886 struct ixgbevf_adapter *adapter = hw->back;
4888 return adapter->netdev->name;
4892 module_exit(ixgbevf_exit_module);
4894 /* ixgbevf_main.c */