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>
37 const char ixgbevf_driver_name[] = "ixgbevf";
38 static const char ixgbevf_driver_string[] =
39 "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
41 static char ixgbevf_copyright[] =
42 "Copyright (c) 2009 - 2018 Intel Corporation.";
44 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
45 [board_82599_vf] = &ixgbevf_82599_vf_info,
46 [board_82599_vf_hv] = &ixgbevf_82599_vf_hv_info,
47 [board_X540_vf] = &ixgbevf_X540_vf_info,
48 [board_X540_vf_hv] = &ixgbevf_X540_vf_hv_info,
49 [board_X550_vf] = &ixgbevf_X550_vf_info,
50 [board_X550_vf_hv] = &ixgbevf_X550_vf_hv_info,
51 [board_X550EM_x_vf] = &ixgbevf_X550EM_x_vf_info,
52 [board_X550EM_x_vf_hv] = &ixgbevf_X550EM_x_vf_hv_info,
53 [board_x550em_a_vf] = &ixgbevf_x550em_a_vf_info,
56 /* ixgbevf_pci_tbl - PCI Device ID Table
58 * Wildcard entries (PCI_ANY_ID) should come last
59 * Last entry must be all 0s
61 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
62 * Class, Class Mask, private data (not used) }
64 static const struct pci_device_id ixgbevf_pci_tbl[] = {
65 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
66 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF_HV), board_82599_vf_hv },
67 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
68 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF_HV), board_X540_vf_hv },
69 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF), board_X550_vf },
70 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF_HV), board_X550_vf_hv },
71 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF), board_X550EM_x_vf },
72 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF_HV), board_X550EM_x_vf_hv},
73 {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_VF), board_x550em_a_vf },
74 /* required last entry */
77 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
79 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
80 MODULE_DESCRIPTION("Intel(R) 10 Gigabit Virtual Function Network Driver");
81 MODULE_LICENSE("GPL v2");
83 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
84 static int debug = -1;
85 module_param(debug, int, 0);
86 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
88 static struct workqueue_struct *ixgbevf_wq;
90 static void ixgbevf_service_event_schedule(struct ixgbevf_adapter *adapter)
92 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
93 !test_bit(__IXGBEVF_REMOVING, &adapter->state) &&
94 !test_and_set_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state))
95 queue_work(ixgbevf_wq, &adapter->service_task);
98 static void ixgbevf_service_event_complete(struct ixgbevf_adapter *adapter)
100 BUG_ON(!test_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state));
102 /* flush memory to make sure state is correct before next watchdog */
103 smp_mb__before_atomic();
104 clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
108 static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter);
109 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
110 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
111 static bool ixgbevf_can_reuse_rx_page(struct ixgbevf_rx_buffer *rx_buffer);
112 static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
113 struct ixgbevf_rx_buffer *old_buff);
115 static void ixgbevf_remove_adapter(struct ixgbe_hw *hw)
117 struct ixgbevf_adapter *adapter = hw->back;
122 dev_err(&adapter->pdev->dev, "Adapter removed\n");
123 if (test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
124 ixgbevf_service_event_schedule(adapter);
127 static void ixgbevf_check_remove(struct ixgbe_hw *hw, u32 reg)
131 /* The following check not only optimizes a bit by not
132 * performing a read on the status register when the
133 * register just read was a status register read that
134 * returned IXGBE_FAILED_READ_REG. It also blocks any
135 * potential recursion.
137 if (reg == IXGBE_VFSTATUS) {
138 ixgbevf_remove_adapter(hw);
141 value = ixgbevf_read_reg(hw, IXGBE_VFSTATUS);
142 if (value == IXGBE_FAILED_READ_REG)
143 ixgbevf_remove_adapter(hw);
146 u32 ixgbevf_read_reg(struct ixgbe_hw *hw, u32 reg)
148 u8 __iomem *reg_addr = READ_ONCE(hw->hw_addr);
151 if (IXGBE_REMOVED(reg_addr))
152 return IXGBE_FAILED_READ_REG;
153 value = readl(reg_addr + reg);
154 if (unlikely(value == IXGBE_FAILED_READ_REG))
155 ixgbevf_check_remove(hw, reg);
160 * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
161 * @adapter: pointer to adapter struct
162 * @direction: 0 for Rx, 1 for Tx, -1 for other causes
163 * @queue: queue to map the corresponding interrupt to
164 * @msix_vector: the vector to map to the corresponding queue
166 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
167 u8 queue, u8 msix_vector)
170 struct ixgbe_hw *hw = &adapter->hw;
172 if (direction == -1) {
174 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
175 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
178 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
180 /* Tx or Rx causes */
181 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
182 index = ((16 * (queue & 1)) + (8 * direction));
183 ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
184 ivar &= ~(0xFF << index);
185 ivar |= (msix_vector << index);
186 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
190 static u64 ixgbevf_get_tx_completed(struct ixgbevf_ring *ring)
192 return ring->stats.packets;
195 static u32 ixgbevf_get_tx_pending(struct ixgbevf_ring *ring)
197 struct ixgbevf_adapter *adapter = netdev_priv(ring->netdev);
198 struct ixgbe_hw *hw = &adapter->hw;
200 u32 head = IXGBE_READ_REG(hw, IXGBE_VFTDH(ring->reg_idx));
201 u32 tail = IXGBE_READ_REG(hw, IXGBE_VFTDT(ring->reg_idx));
204 return (head < tail) ?
205 tail - head : (tail + ring->count - head);
210 static inline bool ixgbevf_check_tx_hang(struct ixgbevf_ring *tx_ring)
212 u32 tx_done = ixgbevf_get_tx_completed(tx_ring);
213 u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
214 u32 tx_pending = ixgbevf_get_tx_pending(tx_ring);
216 clear_check_for_tx_hang(tx_ring);
218 /* Check for a hung queue, but be thorough. This verifies
219 * that a transmit has been completed since the previous
220 * check AND there is at least one packet pending. The
221 * ARMED bit is set to indicate a potential hang.
223 if ((tx_done_old == tx_done) && tx_pending) {
224 /* make sure it is true for two checks in a row */
225 return test_and_set_bit(__IXGBEVF_HANG_CHECK_ARMED,
228 /* reset the countdown */
229 clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &tx_ring->state);
231 /* update completed stats and continue */
232 tx_ring->tx_stats.tx_done_old = tx_done;
237 static void ixgbevf_tx_timeout_reset(struct ixgbevf_adapter *adapter)
239 /* Do the reset outside of interrupt context */
240 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
241 set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
242 ixgbevf_service_event_schedule(adapter);
247 * ixgbevf_tx_timeout - Respond to a Tx Hang
248 * @netdev: network interface device structure
249 * @txqueue: transmit queue hanging (unused)
251 static void ixgbevf_tx_timeout(struct net_device *netdev, unsigned int __always_unused txqueue)
253 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
255 ixgbevf_tx_timeout_reset(adapter);
259 * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
260 * @q_vector: board private structure
261 * @tx_ring: tx ring to clean
262 * @napi_budget: Used to determine if we are in netpoll
264 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
265 struct ixgbevf_ring *tx_ring, int napi_budget)
267 struct ixgbevf_adapter *adapter = q_vector->adapter;
268 struct ixgbevf_tx_buffer *tx_buffer;
269 union ixgbe_adv_tx_desc *tx_desc;
270 unsigned int total_bytes = 0, total_packets = 0, total_ipsec = 0;
271 unsigned int budget = tx_ring->count / 2;
272 unsigned int i = tx_ring->next_to_clean;
274 if (test_bit(__IXGBEVF_DOWN, &adapter->state))
277 tx_buffer = &tx_ring->tx_buffer_info[i];
278 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
282 union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
284 /* if next_to_watch is not set then there is no work pending */
288 /* prevent any other reads prior to eop_desc */
291 /* if DD is not set pending work has not been completed */
292 if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
295 /* clear next_to_watch to prevent false hangs */
296 tx_buffer->next_to_watch = NULL;
298 /* update the statistics for this packet */
299 total_bytes += tx_buffer->bytecount;
300 total_packets += tx_buffer->gso_segs;
301 if (tx_buffer->tx_flags & IXGBE_TX_FLAGS_IPSEC)
305 if (ring_is_xdp(tx_ring))
306 page_frag_free(tx_buffer->data);
308 napi_consume_skb(tx_buffer->skb, napi_budget);
310 /* unmap skb header data */
311 dma_unmap_single(tx_ring->dev,
312 dma_unmap_addr(tx_buffer, dma),
313 dma_unmap_len(tx_buffer, len),
316 /* clear tx_buffer data */
317 dma_unmap_len_set(tx_buffer, len, 0);
319 /* unmap remaining buffers */
320 while (tx_desc != eop_desc) {
326 tx_buffer = tx_ring->tx_buffer_info;
327 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
330 /* unmap any remaining paged data */
331 if (dma_unmap_len(tx_buffer, len)) {
332 dma_unmap_page(tx_ring->dev,
333 dma_unmap_addr(tx_buffer, dma),
334 dma_unmap_len(tx_buffer, len),
336 dma_unmap_len_set(tx_buffer, len, 0);
340 /* move us one more past the eop_desc for start of next pkt */
346 tx_buffer = tx_ring->tx_buffer_info;
347 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
350 /* issue prefetch for next Tx descriptor */
353 /* update budget accounting */
355 } while (likely(budget));
358 tx_ring->next_to_clean = i;
359 u64_stats_update_begin(&tx_ring->syncp);
360 tx_ring->stats.bytes += total_bytes;
361 tx_ring->stats.packets += total_packets;
362 u64_stats_update_end(&tx_ring->syncp);
363 q_vector->tx.total_bytes += total_bytes;
364 q_vector->tx.total_packets += total_packets;
365 adapter->tx_ipsec += total_ipsec;
367 if (check_for_tx_hang(tx_ring) && ixgbevf_check_tx_hang(tx_ring)) {
368 struct ixgbe_hw *hw = &adapter->hw;
369 union ixgbe_adv_tx_desc *eop_desc;
371 eop_desc = tx_ring->tx_buffer_info[i].next_to_watch;
373 pr_err("Detected Tx Unit Hang%s\n"
375 " TDH, TDT <%x>, <%x>\n"
376 " next_to_use <%x>\n"
377 " next_to_clean <%x>\n"
378 "tx_buffer_info[next_to_clean]\n"
379 " next_to_watch <%p>\n"
380 " eop_desc->wb.status <%x>\n"
381 " time_stamp <%lx>\n"
383 ring_is_xdp(tx_ring) ? " XDP" : "",
384 tx_ring->queue_index,
385 IXGBE_READ_REG(hw, IXGBE_VFTDH(tx_ring->reg_idx)),
386 IXGBE_READ_REG(hw, IXGBE_VFTDT(tx_ring->reg_idx)),
387 tx_ring->next_to_use, i,
388 eop_desc, (eop_desc ? eop_desc->wb.status : 0),
389 tx_ring->tx_buffer_info[i].time_stamp, jiffies);
391 if (!ring_is_xdp(tx_ring))
392 netif_stop_subqueue(tx_ring->netdev,
393 tx_ring->queue_index);
395 /* schedule immediate reset if we believe we hung */
396 ixgbevf_tx_timeout_reset(adapter);
401 if (ring_is_xdp(tx_ring))
404 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
405 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
406 (ixgbevf_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
407 /* Make sure that anybody stopping the queue after this
408 * sees the new next_to_clean.
412 if (__netif_subqueue_stopped(tx_ring->netdev,
413 tx_ring->queue_index) &&
414 !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
415 netif_wake_subqueue(tx_ring->netdev,
416 tx_ring->queue_index);
417 ++tx_ring->tx_stats.restart_queue;
425 * ixgbevf_rx_skb - Helper function to determine proper Rx method
426 * @q_vector: structure containing interrupt and ring information
427 * @skb: packet to send up
429 static void ixgbevf_rx_skb(struct ixgbevf_q_vector *q_vector,
432 napi_gro_receive(&q_vector->napi, skb);
435 #define IXGBE_RSS_L4_TYPES_MASK \
436 ((1ul << IXGBE_RXDADV_RSSTYPE_IPV4_TCP) | \
437 (1ul << IXGBE_RXDADV_RSSTYPE_IPV4_UDP) | \
438 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_TCP) | \
439 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_UDP))
441 static inline void ixgbevf_rx_hash(struct ixgbevf_ring *ring,
442 union ixgbe_adv_rx_desc *rx_desc,
447 if (!(ring->netdev->features & NETIF_F_RXHASH))
450 rss_type = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info) &
451 IXGBE_RXDADV_RSSTYPE_MASK;
456 skb_set_hash(skb, le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
457 (IXGBE_RSS_L4_TYPES_MASK & (1ul << rss_type)) ?
458 PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
462 * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
463 * @ring: structure containig ring specific data
464 * @rx_desc: current Rx descriptor being processed
465 * @skb: skb currently being received and modified
467 static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
468 union ixgbe_adv_rx_desc *rx_desc,
471 skb_checksum_none_assert(skb);
473 /* Rx csum disabled */
474 if (!(ring->netdev->features & NETIF_F_RXCSUM))
477 /* if IP and error */
478 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
479 ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
480 ring->rx_stats.csum_err++;
484 if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
487 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
488 ring->rx_stats.csum_err++;
492 /* It must be a TCP or UDP packet with a valid checksum */
493 skb->ip_summed = CHECKSUM_UNNECESSARY;
497 * ixgbevf_process_skb_fields - Populate skb header fields from Rx descriptor
498 * @rx_ring: rx descriptor ring packet is being transacted on
499 * @rx_desc: pointer to the EOP Rx descriptor
500 * @skb: pointer to current skb being populated
502 * This function checks the ring, descriptor, and packet information in
503 * order to populate the checksum, VLAN, protocol, and other fields within
506 static void ixgbevf_process_skb_fields(struct ixgbevf_ring *rx_ring,
507 union ixgbe_adv_rx_desc *rx_desc,
510 ixgbevf_rx_hash(rx_ring, rx_desc, skb);
511 ixgbevf_rx_checksum(rx_ring, rx_desc, skb);
513 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
514 u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
515 unsigned long *active_vlans = netdev_priv(rx_ring->netdev);
517 if (test_bit(vid & VLAN_VID_MASK, active_vlans))
518 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
521 if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_STAT_SECP))
522 ixgbevf_ipsec_rx(rx_ring, rx_desc, skb);
524 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
528 struct ixgbevf_rx_buffer *ixgbevf_get_rx_buffer(struct ixgbevf_ring *rx_ring,
529 const unsigned int size)
531 struct ixgbevf_rx_buffer *rx_buffer;
533 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
534 prefetchw(rx_buffer->page);
536 /* we are reusing so sync this buffer for CPU use */
537 dma_sync_single_range_for_cpu(rx_ring->dev,
539 rx_buffer->page_offset,
543 rx_buffer->pagecnt_bias--;
548 static void ixgbevf_put_rx_buffer(struct ixgbevf_ring *rx_ring,
549 struct ixgbevf_rx_buffer *rx_buffer,
552 if (ixgbevf_can_reuse_rx_page(rx_buffer)) {
553 /* hand second half of page back to the ring */
554 ixgbevf_reuse_rx_page(rx_ring, rx_buffer);
557 /* We are not reusing the buffer so unmap it and free
558 * any references we are holding to it
560 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
561 ixgbevf_rx_pg_size(rx_ring),
563 IXGBEVF_RX_DMA_ATTR);
564 __page_frag_cache_drain(rx_buffer->page,
565 rx_buffer->pagecnt_bias);
568 /* clear contents of rx_buffer */
569 rx_buffer->page = NULL;
573 * ixgbevf_is_non_eop - process handling of non-EOP buffers
574 * @rx_ring: Rx ring being processed
575 * @rx_desc: Rx descriptor for current buffer
577 * This function updates next to clean. If the buffer is an EOP buffer
578 * this function exits returning false, otherwise it will place the
579 * sk_buff in the next buffer to be chained and return true indicating
580 * that this is in fact a non-EOP buffer.
582 static bool ixgbevf_is_non_eop(struct ixgbevf_ring *rx_ring,
583 union ixgbe_adv_rx_desc *rx_desc)
585 u32 ntc = rx_ring->next_to_clean + 1;
587 /* fetch, update, and store next to clean */
588 ntc = (ntc < rx_ring->count) ? ntc : 0;
589 rx_ring->next_to_clean = ntc;
591 prefetch(IXGBEVF_RX_DESC(rx_ring, ntc));
593 if (likely(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
599 static inline unsigned int ixgbevf_rx_offset(struct ixgbevf_ring *rx_ring)
601 return ring_uses_build_skb(rx_ring) ? IXGBEVF_SKB_PAD : 0;
604 static bool ixgbevf_alloc_mapped_page(struct ixgbevf_ring *rx_ring,
605 struct ixgbevf_rx_buffer *bi)
607 struct page *page = bi->page;
610 /* since we are recycling buffers we should seldom need to alloc */
614 /* alloc new page for storage */
615 page = dev_alloc_pages(ixgbevf_rx_pg_order(rx_ring));
616 if (unlikely(!page)) {
617 rx_ring->rx_stats.alloc_rx_page_failed++;
621 /* map page for use */
622 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
623 ixgbevf_rx_pg_size(rx_ring),
624 DMA_FROM_DEVICE, IXGBEVF_RX_DMA_ATTR);
626 /* if mapping failed free memory back to system since
627 * there isn't much point in holding memory we can't use
629 if (dma_mapping_error(rx_ring->dev, dma)) {
630 __free_pages(page, ixgbevf_rx_pg_order(rx_ring));
632 rx_ring->rx_stats.alloc_rx_page_failed++;
638 bi->page_offset = ixgbevf_rx_offset(rx_ring);
639 bi->pagecnt_bias = 1;
640 rx_ring->rx_stats.alloc_rx_page++;
646 * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
647 * @rx_ring: rx descriptor ring (for a specific queue) to setup buffers on
648 * @cleaned_count: number of buffers to replace
650 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
653 union ixgbe_adv_rx_desc *rx_desc;
654 struct ixgbevf_rx_buffer *bi;
655 unsigned int i = rx_ring->next_to_use;
657 /* nothing to do or no valid netdev defined */
658 if (!cleaned_count || !rx_ring->netdev)
661 rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
662 bi = &rx_ring->rx_buffer_info[i];
666 if (!ixgbevf_alloc_mapped_page(rx_ring, bi))
669 /* sync the buffer for use by the device */
670 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
672 ixgbevf_rx_bufsz(rx_ring),
675 /* Refresh the desc even if pkt_addr didn't change
676 * because each write-back erases this info.
678 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
684 rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
685 bi = rx_ring->rx_buffer_info;
689 /* clear the length for the next_to_use descriptor */
690 rx_desc->wb.upper.length = 0;
693 } while (cleaned_count);
697 if (rx_ring->next_to_use != i) {
698 /* record the next descriptor to use */
699 rx_ring->next_to_use = i;
701 /* update next to alloc since we have filled the ring */
702 rx_ring->next_to_alloc = i;
704 /* Force memory writes to complete before letting h/w
705 * know there are new descriptors to fetch. (Only
706 * applicable for weak-ordered memory model archs,
710 ixgbevf_write_tail(rx_ring, i);
715 * ixgbevf_cleanup_headers - Correct corrupted or empty headers
716 * @rx_ring: rx descriptor ring packet is being transacted on
717 * @rx_desc: pointer to the EOP Rx descriptor
718 * @skb: pointer to current skb being fixed
720 * Check for corrupted packet headers caused by senders on the local L2
721 * embedded NIC switch not setting up their Tx Descriptors right. These
722 * should be very rare.
724 * Also address the case where we are pulling data in on pages only
725 * and as such no data is present in the skb header.
727 * In addition if skb is not at least 60 bytes we need to pad it so that
728 * it is large enough to qualify as a valid Ethernet frame.
730 * Returns true if an error was encountered and skb was freed.
732 static bool ixgbevf_cleanup_headers(struct ixgbevf_ring *rx_ring,
733 union ixgbe_adv_rx_desc *rx_desc,
736 /* XDP packets use error pointer so abort at this point */
740 /* verify that the packet does not have any known errors */
741 if (unlikely(ixgbevf_test_staterr(rx_desc,
742 IXGBE_RXDADV_ERR_FRAME_ERR_MASK))) {
743 struct net_device *netdev = rx_ring->netdev;
745 if (!(netdev->features & NETIF_F_RXALL)) {
746 dev_kfree_skb_any(skb);
751 /* if eth_skb_pad returns an error the skb was freed */
752 if (eth_skb_pad(skb))
759 * ixgbevf_reuse_rx_page - page flip buffer and store it back on the ring
760 * @rx_ring: rx descriptor ring to store buffers on
761 * @old_buff: donor buffer to have page reused
763 * Synchronizes page for reuse by the adapter
765 static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
766 struct ixgbevf_rx_buffer *old_buff)
768 struct ixgbevf_rx_buffer *new_buff;
769 u16 nta = rx_ring->next_to_alloc;
771 new_buff = &rx_ring->rx_buffer_info[nta];
773 /* update, and store next to alloc */
775 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
777 /* transfer page from old buffer to new buffer */
778 new_buff->page = old_buff->page;
779 new_buff->dma = old_buff->dma;
780 new_buff->page_offset = old_buff->page_offset;
781 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
784 static inline bool ixgbevf_page_is_reserved(struct page *page)
786 return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
789 static bool ixgbevf_can_reuse_rx_page(struct ixgbevf_rx_buffer *rx_buffer)
791 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
792 struct page *page = rx_buffer->page;
794 /* avoid re-using remote pages */
795 if (unlikely(ixgbevf_page_is_reserved(page)))
798 #if (PAGE_SIZE < 8192)
799 /* if we are only owner of page we can reuse it */
800 if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
803 #define IXGBEVF_LAST_OFFSET \
804 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IXGBEVF_RXBUFFER_2048)
806 if (rx_buffer->page_offset > IXGBEVF_LAST_OFFSET)
811 /* If we have drained the page fragment pool we need to update
812 * the pagecnt_bias and page count so that we fully restock the
813 * number of references the driver holds.
815 if (unlikely(!pagecnt_bias)) {
816 page_ref_add(page, USHRT_MAX);
817 rx_buffer->pagecnt_bias = USHRT_MAX;
824 * ixgbevf_add_rx_frag - Add contents of Rx buffer to sk_buff
825 * @rx_ring: rx descriptor ring to transact packets on
826 * @rx_buffer: buffer containing page to add
827 * @skb: sk_buff to place the data into
828 * @size: size of buffer to be added
830 * This function will add the data contained in rx_buffer->page to the skb.
832 static void ixgbevf_add_rx_frag(struct ixgbevf_ring *rx_ring,
833 struct ixgbevf_rx_buffer *rx_buffer,
837 #if (PAGE_SIZE < 8192)
838 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
840 unsigned int truesize = ring_uses_build_skb(rx_ring) ?
841 SKB_DATA_ALIGN(IXGBEVF_SKB_PAD + size) :
842 SKB_DATA_ALIGN(size);
844 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
845 rx_buffer->page_offset, size, truesize);
846 #if (PAGE_SIZE < 8192)
847 rx_buffer->page_offset ^= truesize;
849 rx_buffer->page_offset += truesize;
854 struct sk_buff *ixgbevf_construct_skb(struct ixgbevf_ring *rx_ring,
855 struct ixgbevf_rx_buffer *rx_buffer,
856 struct xdp_buff *xdp,
857 union ixgbe_adv_rx_desc *rx_desc)
859 unsigned int size = xdp->data_end - xdp->data;
860 #if (PAGE_SIZE < 8192)
861 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
863 unsigned int truesize = SKB_DATA_ALIGN(xdp->data_end -
864 xdp->data_hard_start);
866 unsigned int headlen;
869 /* prefetch first cache line of first page */
870 net_prefetch(xdp->data);
872 /* Note, we get here by enabling legacy-rx via:
874 * ethtool --set-priv-flags <dev> legacy-rx on
876 * In this mode, we currently get 0 extra XDP headroom as
877 * opposed to having legacy-rx off, where we process XDP
878 * packets going to stack via ixgbevf_build_skb().
880 * For ixgbevf_construct_skb() mode it means that the
881 * xdp->data_meta will always point to xdp->data, since
882 * the helper cannot expand the head. Should this ever
883 * changed in future for legacy-rx mode on, then lets also
884 * add xdp->data_meta handling here.
887 /* allocate a skb to store the frags */
888 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IXGBEVF_RX_HDR_SIZE);
892 /* Determine available headroom for copy */
894 if (headlen > IXGBEVF_RX_HDR_SIZE)
895 headlen = eth_get_headlen(skb->dev, xdp->data,
896 IXGBEVF_RX_HDR_SIZE);
898 /* align pull length to size of long to optimize memcpy performance */
899 memcpy(__skb_put(skb, headlen), xdp->data,
900 ALIGN(headlen, sizeof(long)));
902 /* update all of the pointers */
905 skb_add_rx_frag(skb, 0, rx_buffer->page,
906 (xdp->data + headlen) -
907 page_address(rx_buffer->page),
909 #if (PAGE_SIZE < 8192)
910 rx_buffer->page_offset ^= truesize;
912 rx_buffer->page_offset += truesize;
915 rx_buffer->pagecnt_bias++;
921 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
924 struct ixgbe_hw *hw = &adapter->hw;
926 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
929 static struct sk_buff *ixgbevf_build_skb(struct ixgbevf_ring *rx_ring,
930 struct ixgbevf_rx_buffer *rx_buffer,
931 struct xdp_buff *xdp,
932 union ixgbe_adv_rx_desc *rx_desc)
934 unsigned int metasize = xdp->data - xdp->data_meta;
935 #if (PAGE_SIZE < 8192)
936 unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
938 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
939 SKB_DATA_ALIGN(xdp->data_end -
940 xdp->data_hard_start);
944 /* Prefetch first cache line of first page. If xdp->data_meta
945 * is unused, this points to xdp->data, otherwise, we likely
946 * have a consumer accessing first few bytes of meta data,
947 * and then actual data.
949 net_prefetch(xdp->data_meta);
951 /* build an skb around the page buffer */
952 skb = build_skb(xdp->data_hard_start, truesize);
956 /* update pointers within the skb to store the data */
957 skb_reserve(skb, xdp->data - xdp->data_hard_start);
958 __skb_put(skb, xdp->data_end - xdp->data);
960 skb_metadata_set(skb, metasize);
962 /* update buffer offset */
963 #if (PAGE_SIZE < 8192)
964 rx_buffer->page_offset ^= truesize;
966 rx_buffer->page_offset += truesize;
972 #define IXGBEVF_XDP_PASS 0
973 #define IXGBEVF_XDP_CONSUMED 1
974 #define IXGBEVF_XDP_TX 2
976 static int ixgbevf_xmit_xdp_ring(struct ixgbevf_ring *ring,
977 struct xdp_buff *xdp)
979 struct ixgbevf_tx_buffer *tx_buffer;
980 union ixgbe_adv_tx_desc *tx_desc;
985 len = xdp->data_end - xdp->data;
987 if (unlikely(!ixgbevf_desc_unused(ring)))
988 return IXGBEVF_XDP_CONSUMED;
990 dma = dma_map_single(ring->dev, xdp->data, len, DMA_TO_DEVICE);
991 if (dma_mapping_error(ring->dev, dma))
992 return IXGBEVF_XDP_CONSUMED;
994 /* record the location of the first descriptor for this packet */
995 i = ring->next_to_use;
996 tx_buffer = &ring->tx_buffer_info[i];
998 dma_unmap_len_set(tx_buffer, len, len);
999 dma_unmap_addr_set(tx_buffer, dma, dma);
1000 tx_buffer->data = xdp->data;
1001 tx_buffer->bytecount = len;
1002 tx_buffer->gso_segs = 1;
1003 tx_buffer->protocol = 0;
1005 /* Populate minimal context descriptor that will provide for the
1006 * fact that we are expected to process Ethernet frames.
1008 if (!test_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state)) {
1009 struct ixgbe_adv_tx_context_desc *context_desc;
1011 set_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state);
1013 context_desc = IXGBEVF_TX_CTXTDESC(ring, 0);
1014 context_desc->vlan_macip_lens =
1015 cpu_to_le32(ETH_HLEN << IXGBE_ADVTXD_MACLEN_SHIFT);
1016 context_desc->fceof_saidx = 0;
1017 context_desc->type_tucmd_mlhl =
1018 cpu_to_le32(IXGBE_TXD_CMD_DEXT |
1019 IXGBE_ADVTXD_DTYP_CTXT);
1020 context_desc->mss_l4len_idx = 0;
1025 /* put descriptor type bits */
1026 cmd_type = IXGBE_ADVTXD_DTYP_DATA |
1027 IXGBE_ADVTXD_DCMD_DEXT |
1028 IXGBE_ADVTXD_DCMD_IFCS;
1029 cmd_type |= len | IXGBE_TXD_CMD;
1031 tx_desc = IXGBEVF_TX_DESC(ring, i);
1032 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1034 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1035 tx_desc->read.olinfo_status =
1036 cpu_to_le32((len << IXGBE_ADVTXD_PAYLEN_SHIFT) |
1039 /* Avoid any potential race with cleanup */
1042 /* set next_to_watch value indicating a packet is present */
1044 if (i == ring->count)
1047 tx_buffer->next_to_watch = tx_desc;
1048 ring->next_to_use = i;
1050 return IXGBEVF_XDP_TX;
1053 static struct sk_buff *ixgbevf_run_xdp(struct ixgbevf_adapter *adapter,
1054 struct ixgbevf_ring *rx_ring,
1055 struct xdp_buff *xdp)
1057 int result = IXGBEVF_XDP_PASS;
1058 struct ixgbevf_ring *xdp_ring;
1059 struct bpf_prog *xdp_prog;
1063 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
1068 act = bpf_prog_run_xdp(xdp_prog, xdp);
1073 xdp_ring = adapter->xdp_ring[rx_ring->queue_index];
1074 result = ixgbevf_xmit_xdp_ring(xdp_ring, xdp);
1075 if (result == IXGBEVF_XDP_CONSUMED)
1079 bpf_warn_invalid_xdp_action(act);
1083 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
1084 fallthrough; /* handle aborts by dropping packet */
1086 result = IXGBEVF_XDP_CONSUMED;
1091 return ERR_PTR(-result);
1094 static unsigned int ixgbevf_rx_frame_truesize(struct ixgbevf_ring *rx_ring,
1097 unsigned int truesize;
1099 #if (PAGE_SIZE < 8192)
1100 truesize = ixgbevf_rx_pg_size(rx_ring) / 2; /* Must be power-of-2 */
1102 truesize = ring_uses_build_skb(rx_ring) ?
1103 SKB_DATA_ALIGN(IXGBEVF_SKB_PAD + size) +
1104 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) :
1105 SKB_DATA_ALIGN(size);
1110 static void ixgbevf_rx_buffer_flip(struct ixgbevf_ring *rx_ring,
1111 struct ixgbevf_rx_buffer *rx_buffer,
1114 unsigned int truesize = ixgbevf_rx_frame_truesize(rx_ring, size);
1116 #if (PAGE_SIZE < 8192)
1117 rx_buffer->page_offset ^= truesize;
1119 rx_buffer->page_offset += truesize;
1123 static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
1124 struct ixgbevf_ring *rx_ring,
1127 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1128 struct ixgbevf_adapter *adapter = q_vector->adapter;
1129 u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
1130 struct sk_buff *skb = rx_ring->skb;
1131 bool xdp_xmit = false;
1132 struct xdp_buff xdp;
1134 xdp.rxq = &rx_ring->xdp_rxq;
1136 /* Frame size depend on rx_ring setup when PAGE_SIZE=4K */
1137 #if (PAGE_SIZE < 8192)
1138 xdp.frame_sz = ixgbevf_rx_frame_truesize(rx_ring, 0);
1141 while (likely(total_rx_packets < budget)) {
1142 struct ixgbevf_rx_buffer *rx_buffer;
1143 union ixgbe_adv_rx_desc *rx_desc;
1146 /* return some buffers to hardware, one at a time is too slow */
1147 if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
1148 ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
1152 rx_desc = IXGBEVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
1153 size = le16_to_cpu(rx_desc->wb.upper.length);
1157 /* This memory barrier is needed to keep us from reading
1158 * any other fields out of the rx_desc until we know the
1159 * RXD_STAT_DD bit is set
1163 rx_buffer = ixgbevf_get_rx_buffer(rx_ring, size);
1165 /* retrieve a buffer from the ring */
1167 xdp.data = page_address(rx_buffer->page) +
1168 rx_buffer->page_offset;
1169 xdp.data_meta = xdp.data;
1170 xdp.data_hard_start = xdp.data -
1171 ixgbevf_rx_offset(rx_ring);
1172 xdp.data_end = xdp.data + size;
1173 #if (PAGE_SIZE > 4096)
1174 /* At larger PAGE_SIZE, frame_sz depend on len size */
1175 xdp.frame_sz = ixgbevf_rx_frame_truesize(rx_ring, size);
1177 skb = ixgbevf_run_xdp(adapter, rx_ring, &xdp);
1181 if (PTR_ERR(skb) == -IXGBEVF_XDP_TX) {
1183 ixgbevf_rx_buffer_flip(rx_ring, rx_buffer,
1186 rx_buffer->pagecnt_bias++;
1189 total_rx_bytes += size;
1191 ixgbevf_add_rx_frag(rx_ring, rx_buffer, skb, size);
1192 } else if (ring_uses_build_skb(rx_ring)) {
1193 skb = ixgbevf_build_skb(rx_ring, rx_buffer,
1196 skb = ixgbevf_construct_skb(rx_ring, rx_buffer,
1200 /* exit if we failed to retrieve a buffer */
1202 rx_ring->rx_stats.alloc_rx_buff_failed++;
1203 rx_buffer->pagecnt_bias++;
1207 ixgbevf_put_rx_buffer(rx_ring, rx_buffer, skb);
1210 /* fetch next buffer in frame if non-eop */
1211 if (ixgbevf_is_non_eop(rx_ring, rx_desc))
1214 /* verify the packet layout is correct */
1215 if (ixgbevf_cleanup_headers(rx_ring, rx_desc, skb)) {
1220 /* probably a little skewed due to removing CRC */
1221 total_rx_bytes += skb->len;
1223 /* Workaround hardware that can't do proper VEPA multicast
1226 if ((skb->pkt_type == PACKET_BROADCAST ||
1227 skb->pkt_type == PACKET_MULTICAST) &&
1228 ether_addr_equal(rx_ring->netdev->dev_addr,
1229 eth_hdr(skb)->h_source)) {
1230 dev_kfree_skb_irq(skb);
1234 /* populate checksum, VLAN, and protocol */
1235 ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
1237 ixgbevf_rx_skb(q_vector, skb);
1239 /* reset skb pointer */
1242 /* update budget accounting */
1246 /* place incomplete frames back on ring for completion */
1250 struct ixgbevf_ring *xdp_ring =
1251 adapter->xdp_ring[rx_ring->queue_index];
1253 /* Force memory writes to complete before letting h/w
1254 * know there are new descriptors to fetch.
1257 ixgbevf_write_tail(xdp_ring, xdp_ring->next_to_use);
1260 u64_stats_update_begin(&rx_ring->syncp);
1261 rx_ring->stats.packets += total_rx_packets;
1262 rx_ring->stats.bytes += total_rx_bytes;
1263 u64_stats_update_end(&rx_ring->syncp);
1264 q_vector->rx.total_packets += total_rx_packets;
1265 q_vector->rx.total_bytes += total_rx_bytes;
1267 return total_rx_packets;
1271 * ixgbevf_poll - NAPI polling calback
1272 * @napi: napi struct with our devices info in it
1273 * @budget: amount of work driver is allowed to do this pass, in packets
1275 * This function will clean more than one or more rings associated with a
1278 static int ixgbevf_poll(struct napi_struct *napi, int budget)
1280 struct ixgbevf_q_vector *q_vector =
1281 container_of(napi, struct ixgbevf_q_vector, napi);
1282 struct ixgbevf_adapter *adapter = q_vector->adapter;
1283 struct ixgbevf_ring *ring;
1284 int per_ring_budget, work_done = 0;
1285 bool clean_complete = true;
1287 ixgbevf_for_each_ring(ring, q_vector->tx) {
1288 if (!ixgbevf_clean_tx_irq(q_vector, ring, budget))
1289 clean_complete = false;
1295 /* attempt to distribute budget to each queue fairly, but don't allow
1296 * the budget to go below 1 because we'll exit polling
1298 if (q_vector->rx.count > 1)
1299 per_ring_budget = max(budget/q_vector->rx.count, 1);
1301 per_ring_budget = budget;
1303 ixgbevf_for_each_ring(ring, q_vector->rx) {
1304 int cleaned = ixgbevf_clean_rx_irq(q_vector, ring,
1306 work_done += cleaned;
1307 if (cleaned >= per_ring_budget)
1308 clean_complete = false;
1311 /* If all work not completed, return budget and keep polling */
1312 if (!clean_complete)
1315 /* Exit the polling mode, but don't re-enable interrupts if stack might
1316 * poll us due to busy-polling
1318 if (likely(napi_complete_done(napi, work_done))) {
1319 if (adapter->rx_itr_setting == 1)
1320 ixgbevf_set_itr(q_vector);
1321 if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
1322 !test_bit(__IXGBEVF_REMOVING, &adapter->state))
1323 ixgbevf_irq_enable_queues(adapter,
1324 BIT(q_vector->v_idx));
1327 return min(work_done, budget - 1);
1331 * ixgbevf_write_eitr - write VTEITR register in hardware specific way
1332 * @q_vector: structure containing interrupt and ring information
1334 void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
1336 struct ixgbevf_adapter *adapter = q_vector->adapter;
1337 struct ixgbe_hw *hw = &adapter->hw;
1338 int v_idx = q_vector->v_idx;
1339 u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
1341 /* set the WDIS bit to not clear the timer bits and cause an
1342 * immediate assertion of the interrupt
1344 itr_reg |= IXGBE_EITR_CNT_WDIS;
1346 IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
1350 * ixgbevf_configure_msix - Configure MSI-X hardware
1351 * @adapter: board private structure
1353 * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
1356 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
1358 struct ixgbevf_q_vector *q_vector;
1359 int q_vectors, v_idx;
1361 q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1362 adapter->eims_enable_mask = 0;
1364 /* Populate the IVAR table and set the ITR values to the
1365 * corresponding register.
1367 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
1368 struct ixgbevf_ring *ring;
1370 q_vector = adapter->q_vector[v_idx];
1372 ixgbevf_for_each_ring(ring, q_vector->rx)
1373 ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
1375 ixgbevf_for_each_ring(ring, q_vector->tx)
1376 ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
1378 if (q_vector->tx.ring && !q_vector->rx.ring) {
1379 /* Tx only vector */
1380 if (adapter->tx_itr_setting == 1)
1381 q_vector->itr = IXGBE_12K_ITR;
1383 q_vector->itr = adapter->tx_itr_setting;
1385 /* Rx or Rx/Tx vector */
1386 if (adapter->rx_itr_setting == 1)
1387 q_vector->itr = IXGBE_20K_ITR;
1389 q_vector->itr = adapter->rx_itr_setting;
1392 /* add q_vector eims value to global eims_enable_mask */
1393 adapter->eims_enable_mask |= BIT(v_idx);
1395 ixgbevf_write_eitr(q_vector);
1398 ixgbevf_set_ivar(adapter, -1, 1, v_idx);
1399 /* setup eims_other and add value to global eims_enable_mask */
1400 adapter->eims_other = BIT(v_idx);
1401 adapter->eims_enable_mask |= adapter->eims_other;
1404 enum latency_range {
1408 latency_invalid = 255
1412 * ixgbevf_update_itr - update the dynamic ITR value based on statistics
1413 * @q_vector: structure containing interrupt and ring information
1414 * @ring_container: structure containing ring performance data
1416 * Stores a new ITR value based on packets and byte
1417 * counts during the last interrupt. The advantage of per interrupt
1418 * computation is faster updates and more accurate ITR for the current
1419 * traffic pattern. Constants in this function were computed
1420 * based on theoretical maximum wire speed and thresholds were set based
1421 * on testing data as well as attempting to minimize response time
1422 * while increasing bulk throughput.
1424 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
1425 struct ixgbevf_ring_container *ring_container)
1427 int bytes = ring_container->total_bytes;
1428 int packets = ring_container->total_packets;
1431 u8 itr_setting = ring_container->itr;
1436 /* simple throttle rate management
1437 * 0-20MB/s lowest (100000 ints/s)
1438 * 20-100MB/s low (20000 ints/s)
1439 * 100-1249MB/s bulk (12000 ints/s)
1441 /* what was last interrupt timeslice? */
1442 timepassed_us = q_vector->itr >> 2;
1443 if (timepassed_us == 0)
1446 bytes_perint = bytes / timepassed_us; /* bytes/usec */
1448 switch (itr_setting) {
1449 case lowest_latency:
1450 if (bytes_perint > 10)
1451 itr_setting = low_latency;
1454 if (bytes_perint > 20)
1455 itr_setting = bulk_latency;
1456 else if (bytes_perint <= 10)
1457 itr_setting = lowest_latency;
1460 if (bytes_perint <= 20)
1461 itr_setting = low_latency;
1465 /* clear work counters since we have the values we need */
1466 ring_container->total_bytes = 0;
1467 ring_container->total_packets = 0;
1469 /* write updated itr to ring container */
1470 ring_container->itr = itr_setting;
1473 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
1475 u32 new_itr = q_vector->itr;
1478 ixgbevf_update_itr(q_vector, &q_vector->tx);
1479 ixgbevf_update_itr(q_vector, &q_vector->rx);
1481 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
1483 switch (current_itr) {
1484 /* counts and packets in update_itr are dependent on these numbers */
1485 case lowest_latency:
1486 new_itr = IXGBE_100K_ITR;
1489 new_itr = IXGBE_20K_ITR;
1492 new_itr = IXGBE_12K_ITR;
1498 if (new_itr != q_vector->itr) {
1499 /* do an exponential smoothing */
1500 new_itr = (10 * new_itr * q_vector->itr) /
1501 ((9 * new_itr) + q_vector->itr);
1503 /* save the algorithm value here */
1504 q_vector->itr = new_itr;
1506 ixgbevf_write_eitr(q_vector);
1510 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
1512 struct ixgbevf_adapter *adapter = data;
1513 struct ixgbe_hw *hw = &adapter->hw;
1515 hw->mac.get_link_status = 1;
1517 ixgbevf_service_event_schedule(adapter);
1519 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
1525 * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
1527 * @data: pointer to our q_vector struct for this interrupt vector
1529 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
1531 struct ixgbevf_q_vector *q_vector = data;
1533 /* EIAM disabled interrupts (on this vector) for us */
1534 if (q_vector->rx.ring || q_vector->tx.ring)
1535 napi_schedule_irqoff(&q_vector->napi);
1541 * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1542 * @adapter: board private structure
1544 * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1545 * interrupts from the kernel.
1547 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1549 struct net_device *netdev = adapter->netdev;
1550 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1551 unsigned int ri = 0, ti = 0;
1554 for (vector = 0; vector < q_vectors; vector++) {
1555 struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
1556 struct msix_entry *entry = &adapter->msix_entries[vector];
1558 if (q_vector->tx.ring && q_vector->rx.ring) {
1559 snprintf(q_vector->name, sizeof(q_vector->name),
1560 "%s-TxRx-%u", netdev->name, ri++);
1562 } else if (q_vector->rx.ring) {
1563 snprintf(q_vector->name, sizeof(q_vector->name),
1564 "%s-rx-%u", netdev->name, ri++);
1565 } else if (q_vector->tx.ring) {
1566 snprintf(q_vector->name, sizeof(q_vector->name),
1567 "%s-tx-%u", netdev->name, ti++);
1569 /* skip this unused q_vector */
1572 err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
1573 q_vector->name, q_vector);
1575 hw_dbg(&adapter->hw,
1576 "request_irq failed for MSIX interrupt Error: %d\n",
1578 goto free_queue_irqs;
1582 err = request_irq(adapter->msix_entries[vector].vector,
1583 &ixgbevf_msix_other, 0, netdev->name, adapter);
1585 hw_dbg(&adapter->hw, "request_irq for msix_other failed: %d\n",
1587 goto free_queue_irqs;
1595 free_irq(adapter->msix_entries[vector].vector,
1596 adapter->q_vector[vector]);
1598 /* This failure is non-recoverable - it indicates the system is
1599 * out of MSIX vector resources and the VF driver cannot run
1600 * without them. Set the number of msix vectors to zero
1601 * indicating that not enough can be allocated. The error
1602 * will be returned to the user indicating device open failed.
1603 * Any further attempts to force the driver to open will also
1604 * fail. The only way to recover is to unload the driver and
1605 * reload it again. If the system has recovered some MSIX
1606 * vectors then it may succeed.
1608 adapter->num_msix_vectors = 0;
1613 * ixgbevf_request_irq - initialize interrupts
1614 * @adapter: board private structure
1616 * Attempts to configure interrupts using the best available
1617 * capabilities of the hardware and kernel.
1619 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1621 int err = ixgbevf_request_msix_irqs(adapter);
1624 hw_dbg(&adapter->hw, "request_irq failed, Error %d\n", err);
1629 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1633 if (!adapter->msix_entries)
1636 q_vectors = adapter->num_msix_vectors;
1639 free_irq(adapter->msix_entries[i].vector, adapter);
1642 for (; i >= 0; i--) {
1643 /* free only the irqs that were actually requested */
1644 if (!adapter->q_vector[i]->rx.ring &&
1645 !adapter->q_vector[i]->tx.ring)
1648 free_irq(adapter->msix_entries[i].vector,
1649 adapter->q_vector[i]);
1654 * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1655 * @adapter: board private structure
1657 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1659 struct ixgbe_hw *hw = &adapter->hw;
1662 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
1663 IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1664 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
1666 IXGBE_WRITE_FLUSH(hw);
1668 for (i = 0; i < adapter->num_msix_vectors; i++)
1669 synchronize_irq(adapter->msix_entries[i].vector);
1673 * ixgbevf_irq_enable - Enable default interrupt generation settings
1674 * @adapter: board private structure
1676 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1678 struct ixgbe_hw *hw = &adapter->hw;
1680 IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1681 IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1682 IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1686 * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
1687 * @adapter: board private structure
1688 * @ring: structure containing ring specific data
1690 * Configure the Tx descriptor ring after a reset.
1692 static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
1693 struct ixgbevf_ring *ring)
1695 struct ixgbe_hw *hw = &adapter->hw;
1696 u64 tdba = ring->dma;
1698 u32 txdctl = IXGBE_TXDCTL_ENABLE;
1699 u8 reg_idx = ring->reg_idx;
1701 /* disable queue to avoid issues while updating state */
1702 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
1703 IXGBE_WRITE_FLUSH(hw);
1705 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
1706 IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
1707 IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
1708 ring->count * sizeof(union ixgbe_adv_tx_desc));
1710 /* disable head writeback */
1711 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
1712 IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
1714 /* enable relaxed ordering */
1715 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
1716 (IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1717 IXGBE_DCA_TXCTRL_DATA_RRO_EN));
1719 /* reset head and tail pointers */
1720 IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
1721 IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
1722 ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
1724 /* reset ntu and ntc to place SW in sync with hardwdare */
1725 ring->next_to_clean = 0;
1726 ring->next_to_use = 0;
1728 /* In order to avoid issues WTHRESH + PTHRESH should always be equal
1729 * to or less than the number of on chip descriptors, which is
1732 txdctl |= (8 << 16); /* WTHRESH = 8 */
1734 /* Setting PTHRESH to 32 both improves performance */
1735 txdctl |= (1u << 8) | /* HTHRESH = 1 */
1736 32; /* PTHRESH = 32 */
1738 /* reinitialize tx_buffer_info */
1739 memset(ring->tx_buffer_info, 0,
1740 sizeof(struct ixgbevf_tx_buffer) * ring->count);
1742 clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &ring->state);
1743 clear_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state);
1745 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
1747 /* poll to verify queue is enabled */
1749 usleep_range(1000, 2000);
1750 txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
1751 } while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
1753 hw_dbg(hw, "Could not enable Tx Queue %d\n", reg_idx);
1757 * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1758 * @adapter: board private structure
1760 * Configure the Tx unit of the MAC after a reset.
1762 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1766 /* Setup the HW Tx Head and Tail descriptor pointers */
1767 for (i = 0; i < adapter->num_tx_queues; i++)
1768 ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
1769 for (i = 0; i < adapter->num_xdp_queues; i++)
1770 ixgbevf_configure_tx_ring(adapter, adapter->xdp_ring[i]);
1773 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1775 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter,
1776 struct ixgbevf_ring *ring, int index)
1778 struct ixgbe_hw *hw = &adapter->hw;
1781 srrctl = IXGBE_SRRCTL_DROP_EN;
1783 srrctl |= IXGBEVF_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT;
1784 if (ring_uses_large_buffer(ring))
1785 srrctl |= IXGBEVF_RXBUFFER_3072 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1787 srrctl |= IXGBEVF_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1788 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1790 IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1793 static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1795 struct ixgbe_hw *hw = &adapter->hw;
1797 /* PSRTYPE must be initialized in 82599 */
1798 u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1799 IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1800 IXGBE_PSRTYPE_L2HDR;
1802 if (adapter->num_rx_queues > 1)
1805 IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1808 #define IXGBEVF_MAX_RX_DESC_POLL 10
1809 static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1810 struct ixgbevf_ring *ring)
1812 struct ixgbe_hw *hw = &adapter->hw;
1813 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1815 u8 reg_idx = ring->reg_idx;
1817 if (IXGBE_REMOVED(hw->hw_addr))
1819 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1820 rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1822 /* write value back with RXDCTL.ENABLE bit cleared */
1823 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1825 /* the hardware may take up to 100us to really disable the Rx queue */
1828 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1829 } while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1832 pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
1836 static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1837 struct ixgbevf_ring *ring)
1839 struct ixgbe_hw *hw = &adapter->hw;
1840 int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1842 u8 reg_idx = ring->reg_idx;
1844 if (IXGBE_REMOVED(hw->hw_addr))
1847 usleep_range(1000, 2000);
1848 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1849 } while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1852 pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
1857 * ixgbevf_init_rss_key - Initialize adapter RSS key
1858 * @adapter: device handle
1860 * Allocates and initializes the RSS key if it is not allocated.
1862 static inline int ixgbevf_init_rss_key(struct ixgbevf_adapter *adapter)
1866 if (!adapter->rss_key) {
1867 rss_key = kzalloc(IXGBEVF_RSS_HASH_KEY_SIZE, GFP_KERNEL);
1868 if (unlikely(!rss_key))
1871 netdev_rss_key_fill(rss_key, IXGBEVF_RSS_HASH_KEY_SIZE);
1872 adapter->rss_key = rss_key;
1878 static void ixgbevf_setup_vfmrqc(struct ixgbevf_adapter *adapter)
1880 struct ixgbe_hw *hw = &adapter->hw;
1881 u32 vfmrqc = 0, vfreta = 0;
1882 u16 rss_i = adapter->num_rx_queues;
1885 /* Fill out hash function seeds */
1886 for (i = 0; i < IXGBEVF_VFRSSRK_REGS; i++)
1887 IXGBE_WRITE_REG(hw, IXGBE_VFRSSRK(i), *(adapter->rss_key + i));
1889 for (i = 0, j = 0; i < IXGBEVF_X550_VFRETA_SIZE; i++, j++) {
1893 adapter->rss_indir_tbl[i] = j;
1895 vfreta |= j << (i & 0x3) * 8;
1897 IXGBE_WRITE_REG(hw, IXGBE_VFRETA(i >> 2), vfreta);
1902 /* Perform hash on these packet types */
1903 vfmrqc |= IXGBE_VFMRQC_RSS_FIELD_IPV4 |
1904 IXGBE_VFMRQC_RSS_FIELD_IPV4_TCP |
1905 IXGBE_VFMRQC_RSS_FIELD_IPV6 |
1906 IXGBE_VFMRQC_RSS_FIELD_IPV6_TCP;
1908 vfmrqc |= IXGBE_VFMRQC_RSSEN;
1910 IXGBE_WRITE_REG(hw, IXGBE_VFMRQC, vfmrqc);
1913 static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
1914 struct ixgbevf_ring *ring)
1916 struct ixgbe_hw *hw = &adapter->hw;
1917 union ixgbe_adv_rx_desc *rx_desc;
1918 u64 rdba = ring->dma;
1920 u8 reg_idx = ring->reg_idx;
1922 /* disable queue to avoid issues while updating state */
1923 rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1924 ixgbevf_disable_rx_queue(adapter, ring);
1926 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1927 IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
1928 IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
1929 ring->count * sizeof(union ixgbe_adv_rx_desc));
1931 #ifndef CONFIG_SPARC
1932 /* enable relaxed ordering */
1933 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1934 IXGBE_DCA_RXCTRL_DESC_RRO_EN);
1936 IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1937 IXGBE_DCA_RXCTRL_DESC_RRO_EN |
1938 IXGBE_DCA_RXCTRL_DATA_WRO_EN);
1941 /* reset head and tail pointers */
1942 IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
1943 IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
1944 ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
1946 /* initialize rx_buffer_info */
1947 memset(ring->rx_buffer_info, 0,
1948 sizeof(struct ixgbevf_rx_buffer) * ring->count);
1950 /* initialize Rx descriptor 0 */
1951 rx_desc = IXGBEVF_RX_DESC(ring, 0);
1952 rx_desc->wb.upper.length = 0;
1954 /* reset ntu and ntc to place SW in sync with hardwdare */
1955 ring->next_to_clean = 0;
1956 ring->next_to_use = 0;
1957 ring->next_to_alloc = 0;
1959 ixgbevf_configure_srrctl(adapter, ring, reg_idx);
1961 /* RXDCTL.RLPML does not work on 82599 */
1962 if (adapter->hw.mac.type != ixgbe_mac_82599_vf) {
1963 rxdctl &= ~(IXGBE_RXDCTL_RLPMLMASK |
1964 IXGBE_RXDCTL_RLPML_EN);
1966 #if (PAGE_SIZE < 8192)
1967 /* Limit the maximum frame size so we don't overrun the skb */
1968 if (ring_uses_build_skb(ring) &&
1969 !ring_uses_large_buffer(ring))
1970 rxdctl |= IXGBEVF_MAX_FRAME_BUILD_SKB |
1971 IXGBE_RXDCTL_RLPML_EN;
1975 rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1976 IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1978 ixgbevf_rx_desc_queue_enable(adapter, ring);
1979 ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
1982 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter,
1983 struct ixgbevf_ring *rx_ring)
1985 struct net_device *netdev = adapter->netdev;
1986 unsigned int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1988 /* set build_skb and buffer size flags */
1989 clear_ring_build_skb_enabled(rx_ring);
1990 clear_ring_uses_large_buffer(rx_ring);
1992 if (adapter->flags & IXGBEVF_FLAGS_LEGACY_RX)
1995 if (PAGE_SIZE < 8192)
1996 if (max_frame > IXGBEVF_MAX_FRAME_BUILD_SKB)
1997 set_ring_uses_large_buffer(rx_ring);
1999 /* 82599 can't rely on RXDCTL.RLPML to restrict the size of the frame */
2000 if (adapter->hw.mac.type == ixgbe_mac_82599_vf && !ring_uses_large_buffer(rx_ring))
2003 set_ring_build_skb_enabled(rx_ring);
2007 * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
2008 * @adapter: board private structure
2010 * Configure the Rx unit of the MAC after a reset.
2012 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
2014 struct ixgbe_hw *hw = &adapter->hw;
2015 struct net_device *netdev = adapter->netdev;
2018 ixgbevf_setup_psrtype(adapter);
2019 if (hw->mac.type >= ixgbe_mac_X550_vf)
2020 ixgbevf_setup_vfmrqc(adapter);
2022 spin_lock_bh(&adapter->mbx_lock);
2023 /* notify the PF of our intent to use this size of frame */
2024 ret = hw->mac.ops.set_rlpml(hw, netdev->mtu + ETH_HLEN + ETH_FCS_LEN);
2025 spin_unlock_bh(&adapter->mbx_lock);
2027 dev_err(&adapter->pdev->dev,
2028 "Failed to set MTU at %d\n", netdev->mtu);
2030 /* Setup the HW Rx Head and Tail Descriptor Pointers and
2031 * the Base and Length of the Rx Descriptor Ring
2033 for (i = 0; i < adapter->num_rx_queues; i++) {
2034 struct ixgbevf_ring *rx_ring = adapter->rx_ring[i];
2036 ixgbevf_set_rx_buffer_len(adapter, rx_ring);
2037 ixgbevf_configure_rx_ring(adapter, rx_ring);
2041 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
2042 __be16 proto, u16 vid)
2044 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2045 struct ixgbe_hw *hw = &adapter->hw;
2048 spin_lock_bh(&adapter->mbx_lock);
2050 /* add VID to filter table */
2051 err = hw->mac.ops.set_vfta(hw, vid, 0, true);
2053 spin_unlock_bh(&adapter->mbx_lock);
2055 /* translate error return types so error makes sense */
2056 if (err == IXGBE_ERR_MBX)
2059 if (err == IXGBE_ERR_INVALID_ARGUMENT)
2062 set_bit(vid, adapter->active_vlans);
2067 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
2068 __be16 proto, u16 vid)
2070 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2071 struct ixgbe_hw *hw = &adapter->hw;
2074 spin_lock_bh(&adapter->mbx_lock);
2076 /* remove VID from filter table */
2077 err = hw->mac.ops.set_vfta(hw, vid, 0, false);
2079 spin_unlock_bh(&adapter->mbx_lock);
2081 clear_bit(vid, adapter->active_vlans);
2086 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
2090 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
2091 ixgbevf_vlan_rx_add_vid(adapter->netdev,
2092 htons(ETH_P_8021Q), vid);
2095 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
2097 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2098 struct ixgbe_hw *hw = &adapter->hw;
2101 if (!netdev_uc_empty(netdev)) {
2102 struct netdev_hw_addr *ha;
2104 netdev_for_each_uc_addr(ha, netdev) {
2105 hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
2109 /* If the list is empty then send message to PF driver to
2110 * clear all MAC VLANs on this VF.
2112 hw->mac.ops.set_uc_addr(hw, 0, NULL);
2119 * ixgbevf_set_rx_mode - Multicast and unicast set
2120 * @netdev: network interface device structure
2122 * The set_rx_method entry point is called whenever the multicast address
2123 * list, unicast address list or the network interface flags are updated.
2124 * This routine is responsible for configuring the hardware for proper
2125 * multicast mode and configuring requested unicast filters.
2127 static void ixgbevf_set_rx_mode(struct net_device *netdev)
2129 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2130 struct ixgbe_hw *hw = &adapter->hw;
2131 unsigned int flags = netdev->flags;
2134 /* request the most inclusive mode we need */
2135 if (flags & IFF_PROMISC)
2136 xcast_mode = IXGBEVF_XCAST_MODE_PROMISC;
2137 else if (flags & IFF_ALLMULTI)
2138 xcast_mode = IXGBEVF_XCAST_MODE_ALLMULTI;
2139 else if (flags & (IFF_BROADCAST | IFF_MULTICAST))
2140 xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
2142 xcast_mode = IXGBEVF_XCAST_MODE_NONE;
2144 spin_lock_bh(&adapter->mbx_lock);
2146 hw->mac.ops.update_xcast_mode(hw, xcast_mode);
2148 /* reprogram multicast list */
2149 hw->mac.ops.update_mc_addr_list(hw, netdev);
2151 ixgbevf_write_uc_addr_list(netdev);
2153 spin_unlock_bh(&adapter->mbx_lock);
2156 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
2159 struct ixgbevf_q_vector *q_vector;
2160 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2162 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
2163 q_vector = adapter->q_vector[q_idx];
2164 napi_enable(&q_vector->napi);
2168 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
2171 struct ixgbevf_q_vector *q_vector;
2172 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2174 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
2175 q_vector = adapter->q_vector[q_idx];
2176 napi_disable(&q_vector->napi);
2180 static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
2182 struct ixgbe_hw *hw = &adapter->hw;
2183 unsigned int def_q = 0;
2184 unsigned int num_tcs = 0;
2185 unsigned int num_rx_queues = adapter->num_rx_queues;
2186 unsigned int num_tx_queues = adapter->num_tx_queues;
2189 spin_lock_bh(&adapter->mbx_lock);
2191 /* fetch queue configuration from the PF */
2192 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2194 spin_unlock_bh(&adapter->mbx_lock);
2200 /* we need only one Tx queue */
2203 /* update default Tx ring register index */
2204 adapter->tx_ring[0]->reg_idx = def_q;
2206 /* we need as many queues as traffic classes */
2207 num_rx_queues = num_tcs;
2210 /* if we have a bad config abort request queue reset */
2211 if ((adapter->num_rx_queues != num_rx_queues) ||
2212 (adapter->num_tx_queues != num_tx_queues)) {
2213 /* force mailbox timeout to prevent further messages */
2214 hw->mbx.timeout = 0;
2216 /* wait for watchdog to come around and bail us out */
2217 set_bit(__IXGBEVF_QUEUE_RESET_REQUESTED, &adapter->state);
2223 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
2225 ixgbevf_configure_dcb(adapter);
2227 ixgbevf_set_rx_mode(adapter->netdev);
2229 ixgbevf_restore_vlan(adapter);
2230 ixgbevf_ipsec_restore(adapter);
2232 ixgbevf_configure_tx(adapter);
2233 ixgbevf_configure_rx(adapter);
2236 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
2238 /* Only save pre-reset stats if there are some */
2239 if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
2240 adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
2241 adapter->stats.base_vfgprc;
2242 adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
2243 adapter->stats.base_vfgptc;
2244 adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
2245 adapter->stats.base_vfgorc;
2246 adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
2247 adapter->stats.base_vfgotc;
2248 adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
2249 adapter->stats.base_vfmprc;
2253 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
2255 struct ixgbe_hw *hw = &adapter->hw;
2257 adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
2258 adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
2259 adapter->stats.last_vfgorc |=
2260 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
2261 adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
2262 adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
2263 adapter->stats.last_vfgotc |=
2264 (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
2265 adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
2267 adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
2268 adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
2269 adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
2270 adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
2271 adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
2274 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
2276 struct ixgbe_hw *hw = &adapter->hw;
2277 static const int api[] = {
2283 ixgbe_mbox_api_unknown
2287 spin_lock_bh(&adapter->mbx_lock);
2289 while (api[idx] != ixgbe_mbox_api_unknown) {
2290 err = hw->mac.ops.negotiate_api_version(hw, api[idx]);
2296 spin_unlock_bh(&adapter->mbx_lock);
2299 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
2301 struct net_device *netdev = adapter->netdev;
2302 struct ixgbe_hw *hw = &adapter->hw;
2304 ixgbevf_configure_msix(adapter);
2306 spin_lock_bh(&adapter->mbx_lock);
2308 if (is_valid_ether_addr(hw->mac.addr))
2309 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
2311 hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
2313 spin_unlock_bh(&adapter->mbx_lock);
2315 smp_mb__before_atomic();
2316 clear_bit(__IXGBEVF_DOWN, &adapter->state);
2317 ixgbevf_napi_enable_all(adapter);
2319 /* clear any pending interrupts, may auto mask */
2320 IXGBE_READ_REG(hw, IXGBE_VTEICR);
2321 ixgbevf_irq_enable(adapter);
2323 /* enable transmits */
2324 netif_tx_start_all_queues(netdev);
2326 ixgbevf_save_reset_stats(adapter);
2327 ixgbevf_init_last_counter_stats(adapter);
2329 hw->mac.get_link_status = 1;
2330 mod_timer(&adapter->service_timer, jiffies);
2333 void ixgbevf_up(struct ixgbevf_adapter *adapter)
2335 ixgbevf_configure(adapter);
2337 ixgbevf_up_complete(adapter);
2341 * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
2342 * @rx_ring: ring to free buffers from
2344 static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
2346 u16 i = rx_ring->next_to_clean;
2348 /* Free Rx ring sk_buff */
2350 dev_kfree_skb(rx_ring->skb);
2351 rx_ring->skb = NULL;
2354 /* Free all the Rx ring pages */
2355 while (i != rx_ring->next_to_alloc) {
2356 struct ixgbevf_rx_buffer *rx_buffer;
2358 rx_buffer = &rx_ring->rx_buffer_info[i];
2360 /* Invalidate cache lines that may have been written to by
2361 * device so that we avoid corrupting memory.
2363 dma_sync_single_range_for_cpu(rx_ring->dev,
2365 rx_buffer->page_offset,
2366 ixgbevf_rx_bufsz(rx_ring),
2369 /* free resources associated with mapping */
2370 dma_unmap_page_attrs(rx_ring->dev,
2372 ixgbevf_rx_pg_size(rx_ring),
2374 IXGBEVF_RX_DMA_ATTR);
2376 __page_frag_cache_drain(rx_buffer->page,
2377 rx_buffer->pagecnt_bias);
2380 if (i == rx_ring->count)
2384 rx_ring->next_to_alloc = 0;
2385 rx_ring->next_to_clean = 0;
2386 rx_ring->next_to_use = 0;
2390 * ixgbevf_clean_tx_ring - Free Tx Buffers
2391 * @tx_ring: ring to be cleaned
2393 static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
2395 u16 i = tx_ring->next_to_clean;
2396 struct ixgbevf_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
2398 while (i != tx_ring->next_to_use) {
2399 union ixgbe_adv_tx_desc *eop_desc, *tx_desc;
2401 /* Free all the Tx ring sk_buffs */
2402 if (ring_is_xdp(tx_ring))
2403 page_frag_free(tx_buffer->data);
2405 dev_kfree_skb_any(tx_buffer->skb);
2407 /* unmap skb header data */
2408 dma_unmap_single(tx_ring->dev,
2409 dma_unmap_addr(tx_buffer, dma),
2410 dma_unmap_len(tx_buffer, len),
2413 /* check for eop_desc to determine the end of the packet */
2414 eop_desc = tx_buffer->next_to_watch;
2415 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2417 /* unmap remaining buffers */
2418 while (tx_desc != eop_desc) {
2422 if (unlikely(i == tx_ring->count)) {
2424 tx_buffer = tx_ring->tx_buffer_info;
2425 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
2428 /* unmap any remaining paged data */
2429 if (dma_unmap_len(tx_buffer, len))
2430 dma_unmap_page(tx_ring->dev,
2431 dma_unmap_addr(tx_buffer, dma),
2432 dma_unmap_len(tx_buffer, len),
2436 /* move us one more past the eop_desc for start of next pkt */
2439 if (unlikely(i == tx_ring->count)) {
2441 tx_buffer = tx_ring->tx_buffer_info;
2445 /* reset next_to_use and next_to_clean */
2446 tx_ring->next_to_use = 0;
2447 tx_ring->next_to_clean = 0;
2452 * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
2453 * @adapter: board private structure
2455 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
2459 for (i = 0; i < adapter->num_rx_queues; i++)
2460 ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
2464 * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
2465 * @adapter: board private structure
2467 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
2471 for (i = 0; i < adapter->num_tx_queues; i++)
2472 ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
2473 for (i = 0; i < adapter->num_xdp_queues; i++)
2474 ixgbevf_clean_tx_ring(adapter->xdp_ring[i]);
2477 void ixgbevf_down(struct ixgbevf_adapter *adapter)
2479 struct net_device *netdev = adapter->netdev;
2480 struct ixgbe_hw *hw = &adapter->hw;
2483 /* signal that we are down to the interrupt handler */
2484 if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
2485 return; /* do nothing if already down */
2487 /* disable all enabled Rx queues */
2488 for (i = 0; i < adapter->num_rx_queues; i++)
2489 ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
2491 usleep_range(10000, 20000);
2493 netif_tx_stop_all_queues(netdev);
2495 /* call carrier off first to avoid false dev_watchdog timeouts */
2496 netif_carrier_off(netdev);
2497 netif_tx_disable(netdev);
2499 ixgbevf_irq_disable(adapter);
2501 ixgbevf_napi_disable_all(adapter);
2503 del_timer_sync(&adapter->service_timer);
2505 /* disable transmits in the hardware now that interrupts are off */
2506 for (i = 0; i < adapter->num_tx_queues; i++) {
2507 u8 reg_idx = adapter->tx_ring[i]->reg_idx;
2509 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2510 IXGBE_TXDCTL_SWFLSH);
2513 for (i = 0; i < adapter->num_xdp_queues; i++) {
2514 u8 reg_idx = adapter->xdp_ring[i]->reg_idx;
2516 IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2517 IXGBE_TXDCTL_SWFLSH);
2520 if (!pci_channel_offline(adapter->pdev))
2521 ixgbevf_reset(adapter);
2523 ixgbevf_clean_all_tx_rings(adapter);
2524 ixgbevf_clean_all_rx_rings(adapter);
2527 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
2529 while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
2532 ixgbevf_down(adapter);
2533 pci_set_master(adapter->pdev);
2534 ixgbevf_up(adapter);
2536 clear_bit(__IXGBEVF_RESETTING, &adapter->state);
2539 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
2541 struct ixgbe_hw *hw = &adapter->hw;
2542 struct net_device *netdev = adapter->netdev;
2544 if (hw->mac.ops.reset_hw(hw)) {
2545 hw_dbg(hw, "PF still resetting\n");
2547 hw->mac.ops.init_hw(hw);
2548 ixgbevf_negotiate_api(adapter);
2551 if (is_valid_ether_addr(adapter->hw.mac.addr)) {
2552 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2553 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2556 adapter->last_reset = jiffies;
2559 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
2562 int vector_threshold;
2564 /* We'll want at least 2 (vector_threshold):
2565 * 1) TxQ[0] + RxQ[0] handler
2566 * 2) Other (Link Status Change, etc.)
2568 vector_threshold = MIN_MSIX_COUNT;
2570 /* The more we get, the more we will assign to Tx/Rx Cleanup
2571 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2572 * Right now, we simply care about how many we'll get; we'll
2573 * set them up later while requesting irq's.
2575 vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
2576 vector_threshold, vectors);
2579 dev_err(&adapter->pdev->dev,
2580 "Unable to allocate MSI-X interrupts\n");
2581 kfree(adapter->msix_entries);
2582 adapter->msix_entries = NULL;
2586 /* Adjust for only the vectors we'll use, which is minimum
2587 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
2588 * vectors we were allocated.
2590 adapter->num_msix_vectors = vectors;
2596 * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
2597 * @adapter: board private structure to initialize
2599 * This is the top level queue allocation routine. The order here is very
2600 * important, starting with the "most" number of features turned on at once,
2601 * and ending with the smallest set of features. This way large combinations
2602 * can be allocated if they're turned on, and smaller combinations are the
2603 * fall through conditions.
2606 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
2608 struct ixgbe_hw *hw = &adapter->hw;
2609 unsigned int def_q = 0;
2610 unsigned int num_tcs = 0;
2613 /* Start with base case */
2614 adapter->num_rx_queues = 1;
2615 adapter->num_tx_queues = 1;
2616 adapter->num_xdp_queues = 0;
2618 spin_lock_bh(&adapter->mbx_lock);
2620 /* fetch queue configuration from the PF */
2621 err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2623 spin_unlock_bh(&adapter->mbx_lock);
2628 /* we need as many queues as traffic classes */
2630 adapter->num_rx_queues = num_tcs;
2632 u16 rss = min_t(u16, num_online_cpus(), IXGBEVF_MAX_RSS_QUEUES);
2634 switch (hw->api_version) {
2635 case ixgbe_mbox_api_11:
2636 case ixgbe_mbox_api_12:
2637 case ixgbe_mbox_api_13:
2638 case ixgbe_mbox_api_14:
2639 if (adapter->xdp_prog &&
2640 hw->mac.max_tx_queues == rss)
2641 rss = rss > 3 ? 2 : 1;
2643 adapter->num_rx_queues = rss;
2644 adapter->num_tx_queues = rss;
2645 adapter->num_xdp_queues = adapter->xdp_prog ? rss : 0;
2653 * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2654 * @adapter: board private structure to initialize
2656 * Attempt to configure the interrupts using the best available
2657 * capabilities of the hardware and the kernel.
2659 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2661 int vector, v_budget;
2663 /* It's easy to be greedy for MSI-X vectors, but it really
2664 * doesn't do us much good if we have a lot more vectors
2665 * than CPU's. So let's be conservative and only ask for
2666 * (roughly) the same number of vectors as there are CPU's.
2667 * The default is to use pairs of vectors.
2669 v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
2670 v_budget = min_t(int, v_budget, num_online_cpus());
2671 v_budget += NON_Q_VECTORS;
2673 adapter->msix_entries = kcalloc(v_budget,
2674 sizeof(struct msix_entry), GFP_KERNEL);
2675 if (!adapter->msix_entries)
2678 for (vector = 0; vector < v_budget; vector++)
2679 adapter->msix_entries[vector].entry = vector;
2681 /* A failure in MSI-X entry allocation isn't fatal, but the VF driver
2682 * does not support any other modes, so we will simply fail here. Note
2683 * that we clean up the msix_entries pointer else-where.
2685 return ixgbevf_acquire_msix_vectors(adapter, v_budget);
2688 static void ixgbevf_add_ring(struct ixgbevf_ring *ring,
2689 struct ixgbevf_ring_container *head)
2691 ring->next = head->ring;
2697 * ixgbevf_alloc_q_vector - Allocate memory for a single interrupt vector
2698 * @adapter: board private structure to initialize
2699 * @v_idx: index of vector in adapter struct
2700 * @txr_count: number of Tx rings for q vector
2701 * @txr_idx: index of first Tx ring to assign
2702 * @xdp_count: total number of XDP rings to allocate
2703 * @xdp_idx: index of first XDP ring to allocate
2704 * @rxr_count: number of Rx rings for q vector
2705 * @rxr_idx: index of first Rx ring to assign
2707 * We allocate one q_vector. If allocation fails we return -ENOMEM.
2709 static int ixgbevf_alloc_q_vector(struct ixgbevf_adapter *adapter, int v_idx,
2710 int txr_count, int txr_idx,
2711 int xdp_count, int xdp_idx,
2712 int rxr_count, int rxr_idx)
2714 struct ixgbevf_q_vector *q_vector;
2715 int reg_idx = txr_idx + xdp_idx;
2716 struct ixgbevf_ring *ring;
2717 int ring_count, size;
2719 ring_count = txr_count + xdp_count + rxr_count;
2720 size = sizeof(*q_vector) + (sizeof(*ring) * ring_count);
2722 /* allocate q_vector and rings */
2723 q_vector = kzalloc(size, GFP_KERNEL);
2727 /* initialize NAPI */
2728 netif_napi_add(adapter->netdev, &q_vector->napi, ixgbevf_poll, 64);
2730 /* tie q_vector and adapter together */
2731 adapter->q_vector[v_idx] = q_vector;
2732 q_vector->adapter = adapter;
2733 q_vector->v_idx = v_idx;
2735 /* initialize pointer to rings */
2736 ring = q_vector->ring;
2739 /* assign generic ring traits */
2740 ring->dev = &adapter->pdev->dev;
2741 ring->netdev = adapter->netdev;
2743 /* configure backlink on ring */
2744 ring->q_vector = q_vector;
2746 /* update q_vector Tx values */
2747 ixgbevf_add_ring(ring, &q_vector->tx);
2749 /* apply Tx specific ring traits */
2750 ring->count = adapter->tx_ring_count;
2751 ring->queue_index = txr_idx;
2752 ring->reg_idx = reg_idx;
2754 /* assign ring to adapter */
2755 adapter->tx_ring[txr_idx] = ring;
2757 /* update count and index */
2762 /* push pointer to next ring */
2767 /* assign generic ring traits */
2768 ring->dev = &adapter->pdev->dev;
2769 ring->netdev = adapter->netdev;
2771 /* configure backlink on ring */
2772 ring->q_vector = q_vector;
2774 /* update q_vector Tx values */
2775 ixgbevf_add_ring(ring, &q_vector->tx);
2777 /* apply Tx specific ring traits */
2778 ring->count = adapter->tx_ring_count;
2779 ring->queue_index = xdp_idx;
2780 ring->reg_idx = reg_idx;
2783 /* assign ring to adapter */
2784 adapter->xdp_ring[xdp_idx] = ring;
2786 /* update count and index */
2791 /* push pointer to next ring */
2796 /* assign generic ring traits */
2797 ring->dev = &adapter->pdev->dev;
2798 ring->netdev = adapter->netdev;
2800 /* configure backlink on ring */
2801 ring->q_vector = q_vector;
2803 /* update q_vector Rx values */
2804 ixgbevf_add_ring(ring, &q_vector->rx);
2806 /* apply Rx specific ring traits */
2807 ring->count = adapter->rx_ring_count;
2808 ring->queue_index = rxr_idx;
2809 ring->reg_idx = rxr_idx;
2811 /* assign ring to adapter */
2812 adapter->rx_ring[rxr_idx] = ring;
2814 /* update count and index */
2818 /* push pointer to next ring */
2826 * ixgbevf_free_q_vector - Free memory allocated for specific interrupt vector
2827 * @adapter: board private structure to initialize
2828 * @v_idx: index of vector in adapter struct
2830 * This function frees the memory allocated to the q_vector. In addition if
2831 * NAPI is enabled it will delete any references to the NAPI struct prior
2832 * to freeing the q_vector.
2834 static void ixgbevf_free_q_vector(struct ixgbevf_adapter *adapter, int v_idx)
2836 struct ixgbevf_q_vector *q_vector = adapter->q_vector[v_idx];
2837 struct ixgbevf_ring *ring;
2839 ixgbevf_for_each_ring(ring, q_vector->tx) {
2840 if (ring_is_xdp(ring))
2841 adapter->xdp_ring[ring->queue_index] = NULL;
2843 adapter->tx_ring[ring->queue_index] = NULL;
2846 ixgbevf_for_each_ring(ring, q_vector->rx)
2847 adapter->rx_ring[ring->queue_index] = NULL;
2849 adapter->q_vector[v_idx] = NULL;
2850 netif_napi_del(&q_vector->napi);
2852 /* ixgbevf_get_stats() might access the rings on this vector,
2853 * we must wait a grace period before freeing it.
2855 kfree_rcu(q_vector, rcu);
2859 * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2860 * @adapter: board private structure to initialize
2862 * We allocate one q_vector per queue interrupt. If allocation fails we
2865 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2867 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2868 int rxr_remaining = adapter->num_rx_queues;
2869 int txr_remaining = adapter->num_tx_queues;
2870 int xdp_remaining = adapter->num_xdp_queues;
2871 int rxr_idx = 0, txr_idx = 0, xdp_idx = 0, v_idx = 0;
2874 if (q_vectors >= (rxr_remaining + txr_remaining + xdp_remaining)) {
2875 for (; rxr_remaining; v_idx++, q_vectors--) {
2876 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors);
2878 err = ixgbevf_alloc_q_vector(adapter, v_idx,
2879 0, 0, 0, 0, rqpv, rxr_idx);
2883 /* update counts and index */
2884 rxr_remaining -= rqpv;
2889 for (; q_vectors; v_idx++, q_vectors--) {
2890 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors);
2891 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors);
2892 int xqpv = DIV_ROUND_UP(xdp_remaining, q_vectors);
2894 err = ixgbevf_alloc_q_vector(adapter, v_idx,
2902 /* update counts and index */
2903 rxr_remaining -= rqpv;
2905 txr_remaining -= tqpv;
2907 xdp_remaining -= xqpv;
2916 ixgbevf_free_q_vector(adapter, v_idx);
2923 * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2924 * @adapter: board private structure to initialize
2926 * This function frees the memory allocated to the q_vectors. In addition if
2927 * NAPI is enabled it will delete any references to the NAPI struct prior
2928 * to freeing the q_vector.
2930 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2932 int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2936 ixgbevf_free_q_vector(adapter, q_vectors);
2941 * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2942 * @adapter: board private structure
2945 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2947 if (!adapter->msix_entries)
2950 pci_disable_msix(adapter->pdev);
2951 kfree(adapter->msix_entries);
2952 adapter->msix_entries = NULL;
2956 * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2957 * @adapter: board private structure to initialize
2960 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2964 /* Number of supported queues */
2965 ixgbevf_set_num_queues(adapter);
2967 err = ixgbevf_set_interrupt_capability(adapter);
2969 hw_dbg(&adapter->hw,
2970 "Unable to setup interrupt capabilities\n");
2971 goto err_set_interrupt;
2974 err = ixgbevf_alloc_q_vectors(adapter);
2976 hw_dbg(&adapter->hw, "Unable to allocate memory for queue vectors\n");
2977 goto err_alloc_q_vectors;
2980 hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, Tx Queue count = %u XDP Queue count %u\n",
2981 (adapter->num_rx_queues > 1) ? "Enabled" : "Disabled",
2982 adapter->num_rx_queues, adapter->num_tx_queues,
2983 adapter->num_xdp_queues);
2985 set_bit(__IXGBEVF_DOWN, &adapter->state);
2988 err_alloc_q_vectors:
2989 ixgbevf_reset_interrupt_capability(adapter);
2995 * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2996 * @adapter: board private structure to clear interrupt scheme on
2998 * We go through and clear interrupt specific resources and reset the structure
2999 * to pre-load conditions
3001 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
3003 adapter->num_tx_queues = 0;
3004 adapter->num_xdp_queues = 0;
3005 adapter->num_rx_queues = 0;
3007 ixgbevf_free_q_vectors(adapter);
3008 ixgbevf_reset_interrupt_capability(adapter);
3012 * ixgbevf_sw_init - Initialize general software structures
3013 * @adapter: board private structure to initialize
3015 * ixgbevf_sw_init initializes the Adapter private data structure.
3016 * Fields are initialized based on PCI device information and
3017 * OS network device settings (MTU size).
3019 static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
3021 struct ixgbe_hw *hw = &adapter->hw;
3022 struct pci_dev *pdev = adapter->pdev;
3023 struct net_device *netdev = adapter->netdev;
3026 /* PCI config space info */
3027 hw->vendor_id = pdev->vendor;
3028 hw->device_id = pdev->device;
3029 hw->revision_id = pdev->revision;
3030 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3031 hw->subsystem_device_id = pdev->subsystem_device;
3033 hw->mbx.ops.init_params(hw);
3035 if (hw->mac.type >= ixgbe_mac_X550_vf) {
3036 err = ixgbevf_init_rss_key(adapter);
3041 /* assume legacy case in which PF would only give VF 2 queues */
3042 hw->mac.max_tx_queues = 2;
3043 hw->mac.max_rx_queues = 2;
3045 /* lock to protect mailbox accesses */
3046 spin_lock_init(&adapter->mbx_lock);
3048 err = hw->mac.ops.reset_hw(hw);
3050 dev_info(&pdev->dev,
3051 "PF still in reset state. Is the PF interface up?\n");
3053 err = hw->mac.ops.init_hw(hw);
3055 pr_err("init_shared_code failed: %d\n", err);
3058 ixgbevf_negotiate_api(adapter);
3059 err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
3061 dev_info(&pdev->dev, "Error reading MAC address\n");
3062 else if (is_zero_ether_addr(adapter->hw.mac.addr))
3063 dev_info(&pdev->dev,
3064 "MAC address not assigned by administrator.\n");
3065 ether_addr_copy(netdev->dev_addr, hw->mac.addr);
3068 if (!is_valid_ether_addr(netdev->dev_addr)) {
3069 dev_info(&pdev->dev, "Assigning random MAC address\n");
3070 eth_hw_addr_random(netdev);
3071 ether_addr_copy(hw->mac.addr, netdev->dev_addr);
3072 ether_addr_copy(hw->mac.perm_addr, netdev->dev_addr);
3075 /* Enable dynamic interrupt throttling rates */
3076 adapter->rx_itr_setting = 1;
3077 adapter->tx_itr_setting = 1;
3079 /* set default ring sizes */
3080 adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
3081 adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
3083 set_bit(__IXGBEVF_DOWN, &adapter->state);
3090 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
3092 u32 current_counter = IXGBE_READ_REG(hw, reg); \
3093 if (current_counter < last_counter) \
3094 counter += 0x100000000LL; \
3095 last_counter = current_counter; \
3096 counter &= 0xFFFFFFFF00000000LL; \
3097 counter |= current_counter; \
3100 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
3102 u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
3103 u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
3104 u64 current_counter = (current_counter_msb << 32) | \
3105 current_counter_lsb; \
3106 if (current_counter < last_counter) \
3107 counter += 0x1000000000LL; \
3108 last_counter = current_counter; \
3109 counter &= 0xFFFFFFF000000000LL; \
3110 counter |= current_counter; \
3113 * ixgbevf_update_stats - Update the board statistics counters.
3114 * @adapter: board private structure
3116 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
3118 struct ixgbe_hw *hw = &adapter->hw;
3119 u64 alloc_rx_page_failed = 0, alloc_rx_buff_failed = 0;
3120 u64 alloc_rx_page = 0, hw_csum_rx_error = 0;
3123 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3124 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3127 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
3128 adapter->stats.vfgprc);
3129 UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
3130 adapter->stats.vfgptc);
3131 UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
3132 adapter->stats.last_vfgorc,
3133 adapter->stats.vfgorc);
3134 UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
3135 adapter->stats.last_vfgotc,
3136 adapter->stats.vfgotc);
3137 UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
3138 adapter->stats.vfmprc);
3140 for (i = 0; i < adapter->num_rx_queues; i++) {
3141 struct ixgbevf_ring *rx_ring = adapter->rx_ring[i];
3143 hw_csum_rx_error += rx_ring->rx_stats.csum_err;
3144 alloc_rx_page_failed += rx_ring->rx_stats.alloc_rx_page_failed;
3145 alloc_rx_buff_failed += rx_ring->rx_stats.alloc_rx_buff_failed;
3146 alloc_rx_page += rx_ring->rx_stats.alloc_rx_page;
3149 adapter->hw_csum_rx_error = hw_csum_rx_error;
3150 adapter->alloc_rx_page_failed = alloc_rx_page_failed;
3151 adapter->alloc_rx_buff_failed = alloc_rx_buff_failed;
3152 adapter->alloc_rx_page = alloc_rx_page;
3156 * ixgbevf_service_timer - Timer Call-back
3157 * @t: pointer to timer_list struct
3159 static void ixgbevf_service_timer(struct timer_list *t)
3161 struct ixgbevf_adapter *adapter = from_timer(adapter, t,
3164 /* Reset the timer */
3165 mod_timer(&adapter->service_timer, (HZ * 2) + jiffies);
3167 ixgbevf_service_event_schedule(adapter);
3170 static void ixgbevf_reset_subtask(struct ixgbevf_adapter *adapter)
3172 if (!test_and_clear_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state))
3176 /* If we're already down or resetting, just bail */
3177 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3178 test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
3179 test_bit(__IXGBEVF_RESETTING, &adapter->state)) {
3184 adapter->tx_timeout_count++;
3186 ixgbevf_reinit_locked(adapter);
3191 * ixgbevf_check_hang_subtask - check for hung queues and dropped interrupts
3192 * @adapter: pointer to the device adapter structure
3194 * This function serves two purposes. First it strobes the interrupt lines
3195 * in order to make certain interrupts are occurring. Secondly it sets the
3196 * bits needed to check for TX hangs. As a result we should immediately
3197 * determine if a hang has occurred.
3199 static void ixgbevf_check_hang_subtask(struct ixgbevf_adapter *adapter)
3201 struct ixgbe_hw *hw = &adapter->hw;
3205 /* If we're down or resetting, just bail */
3206 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3207 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3210 /* Force detection of hung controller */
3211 if (netif_carrier_ok(adapter->netdev)) {
3212 for (i = 0; i < adapter->num_tx_queues; i++)
3213 set_check_for_tx_hang(adapter->tx_ring[i]);
3214 for (i = 0; i < adapter->num_xdp_queues; i++)
3215 set_check_for_tx_hang(adapter->xdp_ring[i]);
3218 /* get one bit for every active Tx/Rx interrupt vector */
3219 for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
3220 struct ixgbevf_q_vector *qv = adapter->q_vector[i];
3222 if (qv->rx.ring || qv->tx.ring)
3226 /* Cause software interrupt to ensure rings are cleaned */
3227 IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
3231 * ixgbevf_watchdog_update_link - update the link status
3232 * @adapter: pointer to the device adapter structure
3234 static void ixgbevf_watchdog_update_link(struct ixgbevf_adapter *adapter)
3236 struct ixgbe_hw *hw = &adapter->hw;
3237 u32 link_speed = adapter->link_speed;
3238 bool link_up = adapter->link_up;
3241 spin_lock_bh(&adapter->mbx_lock);
3243 err = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
3245 spin_unlock_bh(&adapter->mbx_lock);
3247 /* if check for link returns error we will need to reset */
3248 if (err && time_after(jiffies, adapter->last_reset + (10 * HZ))) {
3249 set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
3253 adapter->link_up = link_up;
3254 adapter->link_speed = link_speed;
3258 * ixgbevf_watchdog_link_is_up - update netif_carrier status and
3259 * print link up message
3260 * @adapter: pointer to the device adapter structure
3262 static void ixgbevf_watchdog_link_is_up(struct ixgbevf_adapter *adapter)
3264 struct net_device *netdev = adapter->netdev;
3266 /* only continue if link was previously down */
3267 if (netif_carrier_ok(netdev))
3270 dev_info(&adapter->pdev->dev, "NIC Link is Up %s\n",
3271 (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
3273 (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL) ?
3275 (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL) ?
3279 netif_carrier_on(netdev);
3283 * ixgbevf_watchdog_link_is_down - update netif_carrier status and
3284 * print link down message
3285 * @adapter: pointer to the adapter structure
3287 static void ixgbevf_watchdog_link_is_down(struct ixgbevf_adapter *adapter)
3289 struct net_device *netdev = adapter->netdev;
3291 adapter->link_speed = 0;
3293 /* only continue if link was up previously */
3294 if (!netif_carrier_ok(netdev))
3297 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
3299 netif_carrier_off(netdev);
3303 * ixgbevf_watchdog_subtask - worker thread to bring link up
3304 * @adapter: board private structure
3306 static void ixgbevf_watchdog_subtask(struct ixgbevf_adapter *adapter)
3308 /* if interface is down do nothing */
3309 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3310 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3313 ixgbevf_watchdog_update_link(adapter);
3315 if (adapter->link_up)
3316 ixgbevf_watchdog_link_is_up(adapter);
3318 ixgbevf_watchdog_link_is_down(adapter);
3320 ixgbevf_update_stats(adapter);
3324 * ixgbevf_service_task - manages and runs subtasks
3325 * @work: pointer to work_struct containing our data
3327 static void ixgbevf_service_task(struct work_struct *work)
3329 struct ixgbevf_adapter *adapter = container_of(work,
3330 struct ixgbevf_adapter,
3332 struct ixgbe_hw *hw = &adapter->hw;
3334 if (IXGBE_REMOVED(hw->hw_addr)) {
3335 if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
3337 ixgbevf_down(adapter);
3343 ixgbevf_queue_reset_subtask(adapter);
3344 ixgbevf_reset_subtask(adapter);
3345 ixgbevf_watchdog_subtask(adapter);
3346 ixgbevf_check_hang_subtask(adapter);
3348 ixgbevf_service_event_complete(adapter);
3352 * ixgbevf_free_tx_resources - Free Tx Resources per Queue
3353 * @tx_ring: Tx descriptor ring for a specific queue
3355 * Free all transmit software resources
3357 void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
3359 ixgbevf_clean_tx_ring(tx_ring);
3361 vfree(tx_ring->tx_buffer_info);
3362 tx_ring->tx_buffer_info = NULL;
3364 /* if not set, then don't free */
3368 dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
3371 tx_ring->desc = NULL;
3375 * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
3376 * @adapter: board private structure
3378 * Free all transmit software resources
3380 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
3384 for (i = 0; i < adapter->num_tx_queues; i++)
3385 if (adapter->tx_ring[i]->desc)
3386 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
3387 for (i = 0; i < adapter->num_xdp_queues; i++)
3388 if (adapter->xdp_ring[i]->desc)
3389 ixgbevf_free_tx_resources(adapter->xdp_ring[i]);
3393 * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
3394 * @tx_ring: Tx descriptor ring (for a specific queue) to setup
3396 * Return 0 on success, negative on failure
3398 int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
3400 struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
3403 size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
3404 tx_ring->tx_buffer_info = vmalloc(size);
3405 if (!tx_ring->tx_buffer_info)
3408 u64_stats_init(&tx_ring->syncp);
3410 /* round up to nearest 4K */
3411 tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
3412 tx_ring->size = ALIGN(tx_ring->size, 4096);
3414 tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
3415 &tx_ring->dma, GFP_KERNEL);
3422 vfree(tx_ring->tx_buffer_info);
3423 tx_ring->tx_buffer_info = NULL;
3424 hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit descriptor ring\n");
3429 * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
3430 * @adapter: board private structure
3432 * If this function returns with an error, then it's possible one or
3433 * more of the rings is populated (while the rest are not). It is the
3434 * callers duty to clean those orphaned rings.
3436 * Return 0 on success, negative on failure
3438 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
3440 int i, j = 0, err = 0;
3442 for (i = 0; i < adapter->num_tx_queues; i++) {
3443 err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
3446 hw_dbg(&adapter->hw, "Allocation for Tx Queue %u failed\n", i);
3450 for (j = 0; j < adapter->num_xdp_queues; j++) {
3451 err = ixgbevf_setup_tx_resources(adapter->xdp_ring[j]);
3454 hw_dbg(&adapter->hw, "Allocation for XDP Queue %u failed\n", j);
3460 /* rewind the index freeing the rings as we go */
3462 ixgbevf_free_tx_resources(adapter->xdp_ring[j]);
3464 ixgbevf_free_tx_resources(adapter->tx_ring[i]);
3470 * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
3471 * @adapter: board private structure
3472 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
3474 * Returns 0 on success, negative on failure
3476 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
3477 struct ixgbevf_ring *rx_ring)
3481 size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
3482 rx_ring->rx_buffer_info = vmalloc(size);
3483 if (!rx_ring->rx_buffer_info)
3486 u64_stats_init(&rx_ring->syncp);
3488 /* Round up to nearest 4K */
3489 rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
3490 rx_ring->size = ALIGN(rx_ring->size, 4096);
3492 rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
3493 &rx_ring->dma, GFP_KERNEL);
3498 /* XDP RX-queue info */
3499 if (xdp_rxq_info_reg(&rx_ring->xdp_rxq, adapter->netdev,
3500 rx_ring->queue_index) < 0)
3503 rx_ring->xdp_prog = adapter->xdp_prog;
3507 vfree(rx_ring->rx_buffer_info);
3508 rx_ring->rx_buffer_info = NULL;
3509 dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
3514 * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
3515 * @adapter: board private structure
3517 * If this function returns with an error, then it's possible one or
3518 * more of the rings is populated (while the rest are not). It is the
3519 * callers duty to clean those orphaned rings.
3521 * Return 0 on success, negative on failure
3523 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
3527 for (i = 0; i < adapter->num_rx_queues; i++) {
3528 err = ixgbevf_setup_rx_resources(adapter, adapter->rx_ring[i]);
3531 hw_dbg(&adapter->hw, "Allocation for Rx Queue %u failed\n", i);
3537 /* rewind the index freeing the rings as we go */
3539 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
3544 * ixgbevf_free_rx_resources - Free Rx Resources
3545 * @rx_ring: ring to clean the resources from
3547 * Free all receive software resources
3549 void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
3551 ixgbevf_clean_rx_ring(rx_ring);
3553 rx_ring->xdp_prog = NULL;
3554 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
3555 vfree(rx_ring->rx_buffer_info);
3556 rx_ring->rx_buffer_info = NULL;
3558 dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
3561 rx_ring->desc = NULL;
3565 * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
3566 * @adapter: board private structure
3568 * Free all receive software resources
3570 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
3574 for (i = 0; i < adapter->num_rx_queues; i++)
3575 if (adapter->rx_ring[i]->desc)
3576 ixgbevf_free_rx_resources(adapter->rx_ring[i]);
3580 * ixgbevf_open - Called when a network interface is made active
3581 * @netdev: network interface device structure
3583 * Returns 0 on success, negative value on failure
3585 * The open entry point is called when a network interface is made
3586 * active by the system (IFF_UP). At this point all resources needed
3587 * for transmit and receive operations are allocated, the interrupt
3588 * handler is registered with the OS, the watchdog timer is started,
3589 * and the stack is notified that the interface is ready.
3591 int ixgbevf_open(struct net_device *netdev)
3593 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3594 struct ixgbe_hw *hw = &adapter->hw;
3597 /* A previous failure to open the device because of a lack of
3598 * available MSIX vector resources may have reset the number
3599 * of msix vectors variable to zero. The only way to recover
3600 * is to unload/reload the driver and hope that the system has
3601 * been able to recover some MSIX vector resources.
3603 if (!adapter->num_msix_vectors)
3606 if (hw->adapter_stopped) {
3607 ixgbevf_reset(adapter);
3608 /* if adapter is still stopped then PF isn't up and
3609 * the VF can't start.
3611 if (hw->adapter_stopped) {
3612 err = IXGBE_ERR_MBX;
3613 pr_err("Unable to start - perhaps the PF Driver isn't up yet\n");
3614 goto err_setup_reset;
3618 /* disallow open during test */
3619 if (test_bit(__IXGBEVF_TESTING, &adapter->state))
3622 netif_carrier_off(netdev);
3624 /* allocate transmit descriptors */
3625 err = ixgbevf_setup_all_tx_resources(adapter);
3629 /* allocate receive descriptors */
3630 err = ixgbevf_setup_all_rx_resources(adapter);
3634 ixgbevf_configure(adapter);
3636 err = ixgbevf_request_irq(adapter);
3640 /* Notify the stack of the actual queue counts. */
3641 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
3643 goto err_set_queues;
3645 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
3647 goto err_set_queues;
3649 ixgbevf_up_complete(adapter);
3654 ixgbevf_free_irq(adapter);
3656 ixgbevf_free_all_rx_resources(adapter);
3658 ixgbevf_free_all_tx_resources(adapter);
3660 ixgbevf_reset(adapter);
3667 * ixgbevf_close_suspend - actions necessary to both suspend and close flows
3668 * @adapter: the private adapter struct
3670 * This function should contain the necessary work common to both suspending
3671 * and closing of the device.
3673 static void ixgbevf_close_suspend(struct ixgbevf_adapter *adapter)
3675 ixgbevf_down(adapter);
3676 ixgbevf_free_irq(adapter);
3677 ixgbevf_free_all_tx_resources(adapter);
3678 ixgbevf_free_all_rx_resources(adapter);
3682 * ixgbevf_close - Disables a network interface
3683 * @netdev: network interface device structure
3685 * Returns 0, this is not allowed to fail
3687 * The close entry point is called when an interface is de-activated
3688 * by the OS. The hardware is still under the drivers control, but
3689 * needs to be disabled. A global MAC reset is issued to stop the
3690 * hardware, and all transmit and receive resources are freed.
3692 int ixgbevf_close(struct net_device *netdev)
3694 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3696 if (netif_device_present(netdev))
3697 ixgbevf_close_suspend(adapter);
3702 static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
3704 struct net_device *dev = adapter->netdev;
3706 if (!test_and_clear_bit(__IXGBEVF_QUEUE_RESET_REQUESTED,
3710 /* if interface is down do nothing */
3711 if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3712 test_bit(__IXGBEVF_RESETTING, &adapter->state))
3715 /* Hardware has to reinitialize queues and interrupts to
3716 * match packet buffer alignment. Unfortunately, the
3717 * hardware is not flexible enough to do this dynamically.
3721 if (netif_running(dev))
3724 ixgbevf_clear_interrupt_scheme(adapter);
3725 ixgbevf_init_interrupt_scheme(adapter);
3727 if (netif_running(dev))
3733 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
3734 u32 vlan_macip_lens, u32 fceof_saidx,
3735 u32 type_tucmd, u32 mss_l4len_idx)
3737 struct ixgbe_adv_tx_context_desc *context_desc;
3738 u16 i = tx_ring->next_to_use;
3740 context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
3743 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3745 /* set bits to identify this as an advanced context descriptor */
3746 type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
3748 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3749 context_desc->fceof_saidx = cpu_to_le32(fceof_saidx);
3750 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
3751 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3754 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
3755 struct ixgbevf_tx_buffer *first,
3757 struct ixgbevf_ipsec_tx_data *itd)
3759 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
3760 struct sk_buff *skb = first->skb;
3770 u32 paylen, l4_offset;
3771 u32 fceof_saidx = 0;
3774 if (skb->ip_summed != CHECKSUM_PARTIAL)
3777 if (!skb_is_gso(skb))
3780 err = skb_cow_head(skb, 0);
3784 if (eth_p_mpls(first->protocol))
3785 ip.hdr = skb_inner_network_header(skb);
3787 ip.hdr = skb_network_header(skb);
3788 l4.hdr = skb_checksum_start(skb);
3790 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3791 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3793 /* initialize outer IP header fields */
3794 if (ip.v4->version == 4) {
3795 unsigned char *csum_start = skb_checksum_start(skb);
3796 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
3797 int len = csum_start - trans_start;
3799 /* IP header will have to cancel out any data that
3800 * is not a part of the outer IP header, so set to
3801 * a reverse csum if needed, else init check to 0.
3803 ip.v4->check = (skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) ?
3804 csum_fold(csum_partial(trans_start,
3806 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3809 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3810 IXGBE_TX_FLAGS_CSUM |
3811 IXGBE_TX_FLAGS_IPV4;
3813 ip.v6->payload_len = 0;
3814 first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3815 IXGBE_TX_FLAGS_CSUM;
3818 /* determine offset of inner transport header */
3819 l4_offset = l4.hdr - skb->data;
3821 /* compute length of segmentation header */
3822 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
3824 /* remove payload length from inner checksum */
3825 paylen = skb->len - l4_offset;
3826 csum_replace_by_diff(&l4.tcp->check, htonl(paylen));
3828 /* update gso size and bytecount with header size */
3829 first->gso_segs = skb_shinfo(skb)->gso_segs;
3830 first->bytecount += (first->gso_segs - 1) * *hdr_len;
3832 /* mss_l4len_id: use 1 as index for TSO */
3833 mss_l4len_idx = (*hdr_len - l4_offset) << IXGBE_ADVTXD_L4LEN_SHIFT;
3834 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
3835 mss_l4len_idx |= (1u << IXGBE_ADVTXD_IDX_SHIFT);
3837 fceof_saidx |= itd->pfsa;
3838 type_tucmd |= itd->flags | itd->trailer_len;
3840 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
3841 vlan_macip_lens = l4.hdr - ip.hdr;
3842 vlan_macip_lens |= (ip.hdr - skb->data) << IXGBE_ADVTXD_MACLEN_SHIFT;
3843 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3845 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens, fceof_saidx, type_tucmd,
3851 static inline bool ixgbevf_ipv6_csum_is_sctp(struct sk_buff *skb)
3853 unsigned int offset = 0;
3855 ipv6_find_hdr(skb, &offset, IPPROTO_SCTP, NULL, NULL);
3857 return offset == skb_checksum_start_offset(skb);
3860 static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
3861 struct ixgbevf_tx_buffer *first,
3862 struct ixgbevf_ipsec_tx_data *itd)
3864 struct sk_buff *skb = first->skb;
3865 u32 vlan_macip_lens = 0;
3866 u32 fceof_saidx = 0;
3869 if (skb->ip_summed != CHECKSUM_PARTIAL)
3872 switch (skb->csum_offset) {
3873 case offsetof(struct tcphdr, check):
3874 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3876 case offsetof(struct udphdr, check):
3878 case offsetof(struct sctphdr, checksum):
3879 /* validate that this is actually an SCTP request */
3880 if (((first->protocol == htons(ETH_P_IP)) &&
3881 (ip_hdr(skb)->protocol == IPPROTO_SCTP)) ||
3882 ((first->protocol == htons(ETH_P_IPV6)) &&
3883 ixgbevf_ipv6_csum_is_sctp(skb))) {
3884 type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_SCTP;
3889 skb_checksum_help(skb);
3893 if (first->protocol == htons(ETH_P_IP))
3894 type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3896 /* update TX checksum flag */
3897 first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
3898 vlan_macip_lens = skb_checksum_start_offset(skb) -
3899 skb_network_offset(skb);
3901 /* vlan_macip_lens: MACLEN, VLAN tag */
3902 vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
3903 vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3905 fceof_saidx |= itd->pfsa;
3906 type_tucmd |= itd->flags | itd->trailer_len;
3908 ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3909 fceof_saidx, type_tucmd, 0);
3912 static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
3914 /* set type for advanced descriptor with frame checksum insertion */
3915 __le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
3916 IXGBE_ADVTXD_DCMD_IFCS |
3917 IXGBE_ADVTXD_DCMD_DEXT);
3919 /* set HW VLAN bit if VLAN is present */
3920 if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3921 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
3923 /* set segmentation enable bits for TSO/FSO */
3924 if (tx_flags & IXGBE_TX_FLAGS_TSO)
3925 cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
3930 static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
3931 u32 tx_flags, unsigned int paylen)
3933 __le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
3935 /* enable L4 checksum for TSO and TX checksum offload */
3936 if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3937 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
3939 /* enble IPv4 checksum for TSO */
3940 if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3941 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
3944 if (tx_flags & IXGBE_TX_FLAGS_IPSEC)
3945 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IPSEC);
3947 /* use index 1 context for TSO/FSO/FCOE/IPSEC */
3948 if (tx_flags & (IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_IPSEC))
3949 olinfo_status |= cpu_to_le32(1u << IXGBE_ADVTXD_IDX_SHIFT);
3951 /* Check Context must be set if Tx switch is enabled, which it
3952 * always is for case where virtual functions are running
3954 olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
3956 tx_desc->read.olinfo_status = olinfo_status;
3959 static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
3960 struct ixgbevf_tx_buffer *first,
3963 struct sk_buff *skb = first->skb;
3964 struct ixgbevf_tx_buffer *tx_buffer;
3965 union ixgbe_adv_tx_desc *tx_desc;
3968 unsigned int data_len, size;
3969 u32 tx_flags = first->tx_flags;
3970 __le32 cmd_type = ixgbevf_tx_cmd_type(tx_flags);
3971 u16 i = tx_ring->next_to_use;
3973 tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
3975 ixgbevf_tx_olinfo_status(tx_desc, tx_flags, skb->len - hdr_len);
3977 size = skb_headlen(skb);
3978 data_len = skb->data_len;
3980 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
3984 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
3985 if (dma_mapping_error(tx_ring->dev, dma))
3988 /* record length, and DMA address */
3989 dma_unmap_len_set(tx_buffer, len, size);
3990 dma_unmap_addr_set(tx_buffer, dma, dma);
3992 tx_desc->read.buffer_addr = cpu_to_le64(dma);
3994 while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
3995 tx_desc->read.cmd_type_len =
3996 cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
4000 if (i == tx_ring->count) {
4001 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
4004 tx_desc->read.olinfo_status = 0;
4006 dma += IXGBE_MAX_DATA_PER_TXD;
4007 size -= IXGBE_MAX_DATA_PER_TXD;
4009 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4012 if (likely(!data_len))
4015 tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
4019 if (i == tx_ring->count) {
4020 tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
4023 tx_desc->read.olinfo_status = 0;
4025 size = skb_frag_size(frag);
4028 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
4031 tx_buffer = &tx_ring->tx_buffer_info[i];
4034 /* write last descriptor with RS and EOP bits */
4035 cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
4036 tx_desc->read.cmd_type_len = cmd_type;
4038 /* set the timestamp */
4039 first->time_stamp = jiffies;
4041 skb_tx_timestamp(skb);
4043 /* Force memory writes to complete before letting h/w know there
4044 * are new descriptors to fetch. (Only applicable for weak-ordered
4045 * memory model archs, such as IA-64).
4047 * We also need this memory barrier (wmb) to make certain all of the
4048 * status bits have been updated before next_to_watch is written.
4052 /* set next_to_watch value indicating a packet is present */
4053 first->next_to_watch = tx_desc;
4056 if (i == tx_ring->count)
4059 tx_ring->next_to_use = i;
4061 /* notify HW of packet */
4062 ixgbevf_write_tail(tx_ring, i);
4066 dev_err(tx_ring->dev, "TX DMA map failed\n");
4067 tx_buffer = &tx_ring->tx_buffer_info[i];
4069 /* clear dma mappings for failed tx_buffer_info map */
4070 while (tx_buffer != first) {
4071 if (dma_unmap_len(tx_buffer, len))
4072 dma_unmap_page(tx_ring->dev,
4073 dma_unmap_addr(tx_buffer, dma),
4074 dma_unmap_len(tx_buffer, len),
4076 dma_unmap_len_set(tx_buffer, len, 0);
4079 i += tx_ring->count;
4080 tx_buffer = &tx_ring->tx_buffer_info[i];
4083 if (dma_unmap_len(tx_buffer, len))
4084 dma_unmap_single(tx_ring->dev,
4085 dma_unmap_addr(tx_buffer, dma),
4086 dma_unmap_len(tx_buffer, len),
4088 dma_unmap_len_set(tx_buffer, len, 0);
4090 dev_kfree_skb_any(tx_buffer->skb);
4091 tx_buffer->skb = NULL;
4093 tx_ring->next_to_use = i;
4096 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
4098 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
4099 /* Herbert's original patch had:
4100 * smp_mb__after_netif_stop_queue();
4101 * but since that doesn't exist yet, just open code it.
4105 /* We need to check again in a case another CPU has just
4106 * made room available.
4108 if (likely(ixgbevf_desc_unused(tx_ring) < size))
4111 /* A reprieve! - use start_queue because it doesn't call schedule */
4112 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
4113 ++tx_ring->tx_stats.restart_queue;
4118 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
4120 if (likely(ixgbevf_desc_unused(tx_ring) >= size))
4122 return __ixgbevf_maybe_stop_tx(tx_ring, size);
4125 static int ixgbevf_xmit_frame_ring(struct sk_buff *skb,
4126 struct ixgbevf_ring *tx_ring)
4128 struct ixgbevf_tx_buffer *first;
4131 u16 count = TXD_USE_COUNT(skb_headlen(skb));
4132 struct ixgbevf_ipsec_tx_data ipsec_tx = { 0 };
4133 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
4137 u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
4139 if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
4140 dev_kfree_skb_any(skb);
4141 return NETDEV_TX_OK;
4144 /* need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
4145 * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
4146 * + 2 desc gap to keep tail from touching head,
4147 * + 1 desc for context descriptor,
4148 * otherwise try next time
4150 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
4151 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
4152 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
4154 count += TXD_USE_COUNT(skb_frag_size(frag));
4157 count += skb_shinfo(skb)->nr_frags;
4159 if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
4160 tx_ring->tx_stats.tx_busy++;
4161 return NETDEV_TX_BUSY;
4164 /* record the location of the first descriptor for this packet */
4165 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4167 first->bytecount = skb->len;
4168 first->gso_segs = 1;
4170 if (skb_vlan_tag_present(skb)) {
4171 tx_flags |= skb_vlan_tag_get(skb);
4172 tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
4173 tx_flags |= IXGBE_TX_FLAGS_VLAN;
4176 /* record initial flags and protocol */
4177 first->tx_flags = tx_flags;
4178 first->protocol = vlan_get_protocol(skb);
4180 #ifdef CONFIG_IXGBEVF_IPSEC
4181 if (xfrm_offload(skb) && !ixgbevf_ipsec_tx(tx_ring, first, &ipsec_tx))
4184 tso = ixgbevf_tso(tx_ring, first, &hdr_len, &ipsec_tx);
4188 ixgbevf_tx_csum(tx_ring, first, &ipsec_tx);
4190 ixgbevf_tx_map(tx_ring, first, hdr_len);
4192 ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
4194 return NETDEV_TX_OK;
4197 dev_kfree_skb_any(first->skb);
4200 return NETDEV_TX_OK;
4203 static netdev_tx_t ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
4205 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4206 struct ixgbevf_ring *tx_ring;
4208 if (skb->len <= 0) {
4209 dev_kfree_skb_any(skb);
4210 return NETDEV_TX_OK;
4213 /* The minimum packet size for olinfo paylen is 17 so pad the skb
4214 * in order to meet this minimum size requirement.
4216 if (skb->len < 17) {
4217 if (skb_padto(skb, 17))
4218 return NETDEV_TX_OK;
4222 tx_ring = adapter->tx_ring[skb->queue_mapping];
4223 return ixgbevf_xmit_frame_ring(skb, tx_ring);
4227 * ixgbevf_set_mac - Change the Ethernet Address of the NIC
4228 * @netdev: network interface device structure
4229 * @p: pointer to an address structure
4231 * Returns 0 on success, negative on failure
4233 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
4235 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4236 struct ixgbe_hw *hw = &adapter->hw;
4237 struct sockaddr *addr = p;
4240 if (!is_valid_ether_addr(addr->sa_data))
4241 return -EADDRNOTAVAIL;
4243 spin_lock_bh(&adapter->mbx_lock);
4245 err = hw->mac.ops.set_rar(hw, 0, addr->sa_data, 0);
4247 spin_unlock_bh(&adapter->mbx_lock);
4252 ether_addr_copy(hw->mac.addr, addr->sa_data);
4253 ether_addr_copy(hw->mac.perm_addr, addr->sa_data);
4254 ether_addr_copy(netdev->dev_addr, addr->sa_data);
4260 * ixgbevf_change_mtu - Change the Maximum Transfer Unit
4261 * @netdev: network interface device structure
4262 * @new_mtu: new value for maximum frame size
4264 * Returns 0 on success, negative on failure
4266 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
4268 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4269 struct ixgbe_hw *hw = &adapter->hw;
4270 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
4273 /* prevent MTU being changed to a size unsupported by XDP */
4274 if (adapter->xdp_prog) {
4275 dev_warn(&adapter->pdev->dev, "MTU cannot be changed while XDP program is loaded\n");
4279 spin_lock_bh(&adapter->mbx_lock);
4280 /* notify the PF of our intent to use this size of frame */
4281 ret = hw->mac.ops.set_rlpml(hw, max_frame);
4282 spin_unlock_bh(&adapter->mbx_lock);
4286 hw_dbg(hw, "changing MTU from %d to %d\n",
4287 netdev->mtu, new_mtu);
4289 /* must set new MTU before calling down or up */
4290 netdev->mtu = new_mtu;
4292 if (netif_running(netdev))
4293 ixgbevf_reinit_locked(adapter);
4298 static int __maybe_unused ixgbevf_suspend(struct device *dev_d)
4300 struct net_device *netdev = dev_get_drvdata(dev_d);
4301 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4304 netif_device_detach(netdev);
4306 if (netif_running(netdev))
4307 ixgbevf_close_suspend(adapter);
4309 ixgbevf_clear_interrupt_scheme(adapter);
4315 static int __maybe_unused ixgbevf_resume(struct device *dev_d)
4317 struct pci_dev *pdev = to_pci_dev(dev_d);
4318 struct net_device *netdev = pci_get_drvdata(pdev);
4319 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4322 adapter->hw.hw_addr = adapter->io_addr;
4323 smp_mb__before_atomic();
4324 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
4325 pci_set_master(pdev);
4327 ixgbevf_reset(adapter);
4330 err = ixgbevf_init_interrupt_scheme(adapter);
4331 if (!err && netif_running(netdev))
4332 err = ixgbevf_open(netdev);
4337 netif_device_attach(netdev);
4342 static void ixgbevf_shutdown(struct pci_dev *pdev)
4344 ixgbevf_suspend(&pdev->dev);
4347 static void ixgbevf_get_tx_ring_stats(struct rtnl_link_stats64 *stats,
4348 const struct ixgbevf_ring *ring)
4355 start = u64_stats_fetch_begin_irq(&ring->syncp);
4356 bytes = ring->stats.bytes;
4357 packets = ring->stats.packets;
4358 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4359 stats->tx_bytes += bytes;
4360 stats->tx_packets += packets;
4364 static void ixgbevf_get_stats(struct net_device *netdev,
4365 struct rtnl_link_stats64 *stats)
4367 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4370 const struct ixgbevf_ring *ring;
4373 ixgbevf_update_stats(adapter);
4375 stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
4378 for (i = 0; i < adapter->num_rx_queues; i++) {
4379 ring = adapter->rx_ring[i];
4381 start = u64_stats_fetch_begin_irq(&ring->syncp);
4382 bytes = ring->stats.bytes;
4383 packets = ring->stats.packets;
4384 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4385 stats->rx_bytes += bytes;
4386 stats->rx_packets += packets;
4389 for (i = 0; i < adapter->num_tx_queues; i++) {
4390 ring = adapter->tx_ring[i];
4391 ixgbevf_get_tx_ring_stats(stats, ring);
4394 for (i = 0; i < adapter->num_xdp_queues; i++) {
4395 ring = adapter->xdp_ring[i];
4396 ixgbevf_get_tx_ring_stats(stats, ring);
4401 #define IXGBEVF_MAX_MAC_HDR_LEN 127
4402 #define IXGBEVF_MAX_NETWORK_HDR_LEN 511
4404 static netdev_features_t
4405 ixgbevf_features_check(struct sk_buff *skb, struct net_device *dev,
4406 netdev_features_t features)
4408 unsigned int network_hdr_len, mac_hdr_len;
4410 /* Make certain the headers can be described by a context descriptor */
4411 mac_hdr_len = skb_network_header(skb) - skb->data;
4412 if (unlikely(mac_hdr_len > IXGBEVF_MAX_MAC_HDR_LEN))
4413 return features & ~(NETIF_F_HW_CSUM |
4415 NETIF_F_HW_VLAN_CTAG_TX |
4419 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
4420 if (unlikely(network_hdr_len > IXGBEVF_MAX_NETWORK_HDR_LEN))
4421 return features & ~(NETIF_F_HW_CSUM |
4426 /* We can only support IPV4 TSO in tunnels if we can mangle the
4427 * inner IP ID field, so strip TSO if MANGLEID is not supported.
4429 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
4430 features &= ~NETIF_F_TSO;
4435 static int ixgbevf_xdp_setup(struct net_device *dev, struct bpf_prog *prog)
4437 int i, frame_size = dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4438 struct ixgbevf_adapter *adapter = netdev_priv(dev);
4439 struct bpf_prog *old_prog;
4441 /* verify ixgbevf ring attributes are sufficient for XDP */
4442 for (i = 0; i < adapter->num_rx_queues; i++) {
4443 struct ixgbevf_ring *ring = adapter->rx_ring[i];
4445 if (frame_size > ixgbevf_rx_bufsz(ring))
4449 old_prog = xchg(&adapter->xdp_prog, prog);
4451 /* If transitioning XDP modes reconfigure rings */
4452 if (!!prog != !!old_prog) {
4453 /* Hardware has to reinitialize queues and interrupts to
4454 * match packet buffer alignment. Unfortunately, the
4455 * hardware is not flexible enough to do this dynamically.
4457 if (netif_running(dev))
4460 ixgbevf_clear_interrupt_scheme(adapter);
4461 ixgbevf_init_interrupt_scheme(adapter);
4463 if (netif_running(dev))
4466 for (i = 0; i < adapter->num_rx_queues; i++)
4467 xchg(&adapter->rx_ring[i]->xdp_prog, adapter->xdp_prog);
4471 bpf_prog_put(old_prog);
4476 static int ixgbevf_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4478 switch (xdp->command) {
4479 case XDP_SETUP_PROG:
4480 return ixgbevf_xdp_setup(dev, xdp->prog);
4486 static const struct net_device_ops ixgbevf_netdev_ops = {
4487 .ndo_open = ixgbevf_open,
4488 .ndo_stop = ixgbevf_close,
4489 .ndo_start_xmit = ixgbevf_xmit_frame,
4490 .ndo_set_rx_mode = ixgbevf_set_rx_mode,
4491 .ndo_get_stats64 = ixgbevf_get_stats,
4492 .ndo_validate_addr = eth_validate_addr,
4493 .ndo_set_mac_address = ixgbevf_set_mac,
4494 .ndo_change_mtu = ixgbevf_change_mtu,
4495 .ndo_tx_timeout = ixgbevf_tx_timeout,
4496 .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
4497 .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
4498 .ndo_features_check = ixgbevf_features_check,
4499 .ndo_bpf = ixgbevf_xdp,
4502 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
4504 dev->netdev_ops = &ixgbevf_netdev_ops;
4505 ixgbevf_set_ethtool_ops(dev);
4506 dev->watchdog_timeo = 5 * HZ;
4510 * ixgbevf_probe - Device Initialization Routine
4511 * @pdev: PCI device information struct
4512 * @ent: entry in ixgbevf_pci_tbl
4514 * Returns 0 on success, negative on failure
4516 * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
4517 * The OS initialization, configuring of the adapter private structure,
4518 * and a hardware reset occur.
4520 static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4522 struct net_device *netdev;
4523 struct ixgbevf_adapter *adapter = NULL;
4524 struct ixgbe_hw *hw = NULL;
4525 const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
4526 int err, pci_using_dac;
4527 bool disable_dev = false;
4529 err = pci_enable_device(pdev);
4533 if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
4536 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
4538 dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
4544 err = pci_request_regions(pdev, ixgbevf_driver_name);
4546 dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
4550 pci_set_master(pdev);
4552 netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
4556 goto err_alloc_etherdev;
4559 SET_NETDEV_DEV(netdev, &pdev->dev);
4561 adapter = netdev_priv(netdev);
4563 adapter->netdev = netdev;
4564 adapter->pdev = pdev;
4567 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4569 /* call save state here in standalone driver because it relies on
4570 * adapter struct to exist, and needs to call netdev_priv
4572 pci_save_state(pdev);
4574 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4575 pci_resource_len(pdev, 0));
4576 adapter->io_addr = hw->hw_addr;
4582 ixgbevf_assign_netdev_ops(netdev);
4585 memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
4586 hw->mac.type = ii->mac;
4588 memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
4589 sizeof(struct ixgbe_mbx_operations));
4591 /* setup the private structure */
4592 err = ixgbevf_sw_init(adapter);
4596 /* The HW MAC address was set and/or determined in sw_init */
4597 if (!is_valid_ether_addr(netdev->dev_addr)) {
4598 pr_err("invalid MAC address\n");
4603 netdev->hw_features = NETIF_F_SG |
4610 #define IXGBEVF_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
4611 NETIF_F_GSO_GRE_CSUM | \
4612 NETIF_F_GSO_IPXIP4 | \
4613 NETIF_F_GSO_IPXIP6 | \
4614 NETIF_F_GSO_UDP_TUNNEL | \
4615 NETIF_F_GSO_UDP_TUNNEL_CSUM)
4617 netdev->gso_partial_features = IXGBEVF_GSO_PARTIAL_FEATURES;
4618 netdev->hw_features |= NETIF_F_GSO_PARTIAL |
4619 IXGBEVF_GSO_PARTIAL_FEATURES;
4621 netdev->features = netdev->hw_features;
4624 netdev->features |= NETIF_F_HIGHDMA;
4626 netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
4627 netdev->mpls_features |= NETIF_F_SG |
4631 netdev->mpls_features |= IXGBEVF_GSO_PARTIAL_FEATURES;
4632 netdev->hw_enc_features |= netdev->vlan_features;
4634 /* set this bit last since it cannot be part of vlan_features */
4635 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4636 NETIF_F_HW_VLAN_CTAG_RX |
4637 NETIF_F_HW_VLAN_CTAG_TX;
4639 netdev->priv_flags |= IFF_UNICAST_FLT;
4641 /* MTU range: 68 - 1504 or 9710 */
4642 netdev->min_mtu = ETH_MIN_MTU;
4643 switch (adapter->hw.api_version) {
4644 case ixgbe_mbox_api_11:
4645 case ixgbe_mbox_api_12:
4646 case ixgbe_mbox_api_13:
4647 case ixgbe_mbox_api_14:
4648 netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4649 (ETH_HLEN + ETH_FCS_LEN);
4652 if (adapter->hw.mac.type != ixgbe_mac_82599_vf)
4653 netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4654 (ETH_HLEN + ETH_FCS_LEN);
4656 netdev->max_mtu = ETH_DATA_LEN + ETH_FCS_LEN;
4660 if (IXGBE_REMOVED(hw->hw_addr)) {
4665 timer_setup(&adapter->service_timer, ixgbevf_service_timer, 0);
4667 INIT_WORK(&adapter->service_task, ixgbevf_service_task);
4668 set_bit(__IXGBEVF_SERVICE_INITED, &adapter->state);
4669 clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
4671 err = ixgbevf_init_interrupt_scheme(adapter);
4675 strcpy(netdev->name, "eth%d");
4677 err = register_netdev(netdev);
4681 pci_set_drvdata(pdev, netdev);
4682 netif_carrier_off(netdev);
4683 ixgbevf_init_ipsec_offload(adapter);
4685 ixgbevf_init_last_counter_stats(adapter);
4687 /* print the VF info */
4688 dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
4689 dev_info(&pdev->dev, "MAC: %d\n", hw->mac.type);
4691 switch (hw->mac.type) {
4692 case ixgbe_mac_X550_vf:
4693 dev_info(&pdev->dev, "Intel(R) X550 Virtual Function\n");
4695 case ixgbe_mac_X540_vf:
4696 dev_info(&pdev->dev, "Intel(R) X540 Virtual Function\n");
4698 case ixgbe_mac_82599_vf:
4700 dev_info(&pdev->dev, "Intel(R) 82599 Virtual Function\n");
4707 ixgbevf_clear_interrupt_scheme(adapter);
4709 ixgbevf_reset_interrupt_capability(adapter);
4710 iounmap(adapter->io_addr);
4711 kfree(adapter->rss_key);
4713 disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4714 free_netdev(netdev);
4716 pci_release_regions(pdev);
4719 if (!adapter || disable_dev)
4720 pci_disable_device(pdev);
4725 * ixgbevf_remove - Device Removal Routine
4726 * @pdev: PCI device information struct
4728 * ixgbevf_remove is called by the PCI subsystem to alert the driver
4729 * that it should release a PCI device. The could be caused by a
4730 * Hot-Plug event, or because the driver is going to be removed from
4733 static void ixgbevf_remove(struct pci_dev *pdev)
4735 struct net_device *netdev = pci_get_drvdata(pdev);
4736 struct ixgbevf_adapter *adapter;
4742 adapter = netdev_priv(netdev);
4744 set_bit(__IXGBEVF_REMOVING, &adapter->state);
4745 cancel_work_sync(&adapter->service_task);
4747 if (netdev->reg_state == NETREG_REGISTERED)
4748 unregister_netdev(netdev);
4750 ixgbevf_stop_ipsec_offload(adapter);
4751 ixgbevf_clear_interrupt_scheme(adapter);
4752 ixgbevf_reset_interrupt_capability(adapter);
4754 iounmap(adapter->io_addr);
4755 pci_release_regions(pdev);
4757 hw_dbg(&adapter->hw, "Remove complete\n");
4759 kfree(adapter->rss_key);
4760 disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4761 free_netdev(netdev);
4764 pci_disable_device(pdev);
4768 * ixgbevf_io_error_detected - called when PCI error is detected
4769 * @pdev: Pointer to PCI device
4770 * @state: The current pci connection state
4772 * This function is called after a PCI bus error affecting
4773 * this device has been detected.
4775 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
4776 pci_channel_state_t state)
4778 struct net_device *netdev = pci_get_drvdata(pdev);
4779 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4781 if (!test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
4782 return PCI_ERS_RESULT_DISCONNECT;
4785 netif_device_detach(netdev);
4787 if (netif_running(netdev))
4788 ixgbevf_close_suspend(adapter);
4790 if (state == pci_channel_io_perm_failure) {
4792 return PCI_ERS_RESULT_DISCONNECT;
4795 if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
4796 pci_disable_device(pdev);
4799 /* Request a slot slot reset. */
4800 return PCI_ERS_RESULT_NEED_RESET;
4804 * ixgbevf_io_slot_reset - called after the pci bus has been reset.
4805 * @pdev: Pointer to PCI device
4807 * Restart the card from scratch, as if from a cold-boot. Implementation
4808 * resembles the first-half of the ixgbevf_resume routine.
4810 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
4812 struct net_device *netdev = pci_get_drvdata(pdev);
4813 struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4815 if (pci_enable_device_mem(pdev)) {
4817 "Cannot re-enable PCI device after reset.\n");
4818 return PCI_ERS_RESULT_DISCONNECT;
4821 adapter->hw.hw_addr = adapter->io_addr;
4822 smp_mb__before_atomic();
4823 clear_bit(__IXGBEVF_DISABLED, &adapter->state);
4824 pci_set_master(pdev);
4826 ixgbevf_reset(adapter);
4828 return PCI_ERS_RESULT_RECOVERED;
4832 * ixgbevf_io_resume - called when traffic can start flowing again.
4833 * @pdev: Pointer to PCI device
4835 * This callback is called when the error recovery driver tells us that
4836 * its OK to resume normal operation. Implementation resembles the
4837 * second-half of the ixgbevf_resume routine.
4839 static void ixgbevf_io_resume(struct pci_dev *pdev)
4841 struct net_device *netdev = pci_get_drvdata(pdev);
4844 if (netif_running(netdev))
4845 ixgbevf_open(netdev);
4847 netif_device_attach(netdev);
4851 /* PCI Error Recovery (ERS) */
4852 static const struct pci_error_handlers ixgbevf_err_handler = {
4853 .error_detected = ixgbevf_io_error_detected,
4854 .slot_reset = ixgbevf_io_slot_reset,
4855 .resume = ixgbevf_io_resume,
4858 static SIMPLE_DEV_PM_OPS(ixgbevf_pm_ops, ixgbevf_suspend, ixgbevf_resume);
4860 static struct pci_driver ixgbevf_driver = {
4861 .name = ixgbevf_driver_name,
4862 .id_table = ixgbevf_pci_tbl,
4863 .probe = ixgbevf_probe,
4864 .remove = ixgbevf_remove,
4866 /* Power Management Hooks */
4867 .driver.pm = &ixgbevf_pm_ops,
4869 .shutdown = ixgbevf_shutdown,
4870 .err_handler = &ixgbevf_err_handler
4874 * ixgbevf_init_module - Driver Registration Routine
4876 * ixgbevf_init_module is the first routine called when the driver is
4877 * loaded. All it does is register with the PCI subsystem.
4879 static int __init ixgbevf_init_module(void)
4883 pr_info("%s\n", ixgbevf_driver_string);
4884 pr_info("%s\n", ixgbevf_copyright);
4885 ixgbevf_wq = create_singlethread_workqueue(ixgbevf_driver_name);
4887 pr_err("%s: Failed to create workqueue\n", ixgbevf_driver_name);
4891 err = pci_register_driver(&ixgbevf_driver);
4893 destroy_workqueue(ixgbevf_wq);
4900 module_init(ixgbevf_init_module);
4903 * ixgbevf_exit_module - Driver Exit Cleanup Routine
4905 * ixgbevf_exit_module is called just before the driver is removed
4908 static void __exit ixgbevf_exit_module(void)
4910 pci_unregister_driver(&ixgbevf_driver);
4912 destroy_workqueue(ixgbevf_wq);
4919 * ixgbevf_get_hw_dev_name - return device name string
4920 * used by hardware layer to print debugging information
4921 * @hw: pointer to private hardware struct
4923 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
4925 struct ixgbevf_adapter *adapter = hw->back;
4927 return adapter->netdev->name;
4931 module_exit(ixgbevf_exit_module);
4933 /* ixgbevf_main.c */