xref: /linux/drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c (revision d0f482bb06f9447d44d2cae0386a0bd768c3cc16)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2018 Intel Corporation. */
3 
4 /******************************************************************************
5  Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
6 ******************************************************************************/
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
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>
17 #include <linux/in.h>
18 #include <linux/ip.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>
26 #include <linux/if.h>
27 #include <linux/if_vlan.h>
28 #include <linux/prefetch.h>
29 #include <net/mpls.h>
30 #include <linux/bpf.h>
31 #include <linux/bpf_trace.h>
32 #include <linux/atomic.h>
33 #include <net/xfrm.h>
34 
35 #include "ixgbevf.h"
36 
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";
40 
41 static char ixgbevf_copyright[] =
42 	"Copyright (c) 2009 - 2018 Intel Corporation.";
43 
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,
54 };
55 
56 /* ixgbevf_pci_tbl - PCI Device ID Table
57  *
58  * Wildcard entries (PCI_ANY_ID) should come last
59  * Last entry must be all 0s
60  *
61  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
62  *   Class, Class Mask, private data (not used) }
63  */
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 */
75 	{0, }
76 };
77 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
78 
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");
82 
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)");
87 
88 static struct workqueue_struct *ixgbevf_wq;
89 
90 static void ixgbevf_service_event_schedule(struct ixgbevf_adapter *adapter)
91 {
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);
96 }
97 
98 static void ixgbevf_service_event_complete(struct ixgbevf_adapter *adapter)
99 {
100 	BUG_ON(!test_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state));
101 
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);
105 }
106 
107 /* forward decls */
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);
114 
115 static void ixgbevf_remove_adapter(struct ixgbe_hw *hw)
116 {
117 	struct ixgbevf_adapter *adapter = hw->back;
118 
119 	if (!hw->hw_addr)
120 		return;
121 	hw->hw_addr = NULL;
122 	dev_err(&adapter->pdev->dev, "Adapter removed\n");
123 	if (test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
124 		ixgbevf_service_event_schedule(adapter);
125 }
126 
127 static void ixgbevf_check_remove(struct ixgbe_hw *hw, u32 reg)
128 {
129 	u32 value;
130 
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.
136 	 */
137 	if (reg == IXGBE_VFSTATUS) {
138 		ixgbevf_remove_adapter(hw);
139 		return;
140 	}
141 	value = ixgbevf_read_reg(hw, IXGBE_VFSTATUS);
142 	if (value == IXGBE_FAILED_READ_REG)
143 		ixgbevf_remove_adapter(hw);
144 }
145 
146 u32 ixgbevf_read_reg(struct ixgbe_hw *hw, u32 reg)
147 {
148 	u8 __iomem *reg_addr = READ_ONCE(hw->hw_addr);
149 	u32 value;
150 
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);
156 	return value;
157 }
158 
159 /**
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
165  **/
166 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
167 			     u8 queue, u8 msix_vector)
168 {
169 	u32 ivar, index;
170 	struct ixgbe_hw *hw = &adapter->hw;
171 
172 	if (direction == -1) {
173 		/* other causes */
174 		msix_vector |= IXGBE_IVAR_ALLOC_VAL;
175 		ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
176 		ivar &= ~0xFF;
177 		ivar |= msix_vector;
178 		IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
179 	} else {
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);
187 	}
188 }
189 
190 static u64 ixgbevf_get_tx_completed(struct ixgbevf_ring *ring)
191 {
192 	return ring->stats.packets;
193 }
194 
195 static u32 ixgbevf_get_tx_pending(struct ixgbevf_ring *ring)
196 {
197 	struct ixgbevf_adapter *adapter = netdev_priv(ring->netdev);
198 	struct ixgbe_hw *hw = &adapter->hw;
199 
200 	u32 head = IXGBE_READ_REG(hw, IXGBE_VFTDH(ring->reg_idx));
201 	u32 tail = IXGBE_READ_REG(hw, IXGBE_VFTDT(ring->reg_idx));
202 
203 	if (head != tail)
204 		return (head < tail) ?
205 			tail - head : (tail + ring->count - head);
206 
207 	return 0;
208 }
209 
210 static inline bool ixgbevf_check_tx_hang(struct ixgbevf_ring *tx_ring)
211 {
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);
215 
216 	clear_check_for_tx_hang(tx_ring);
217 
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.
222 	 */
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,
226 					&tx_ring->state);
227 	}
228 	/* reset the countdown */
229 	clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &tx_ring->state);
230 
231 	/* update completed stats and continue */
232 	tx_ring->tx_stats.tx_done_old = tx_done;
233 
234 	return false;
235 }
236 
237 static void ixgbevf_tx_timeout_reset(struct ixgbevf_adapter *adapter)
238 {
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);
243 	}
244 }
245 
246 /**
247  * ixgbevf_tx_timeout - Respond to a Tx Hang
248  * @netdev: network interface device structure
249  * @txqueue: transmit queue hanging (unused)
250  **/
251 static void ixgbevf_tx_timeout(struct net_device *netdev, unsigned int __always_unused txqueue)
252 {
253 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
254 
255 	ixgbevf_tx_timeout_reset(adapter);
256 }
257 
258 /**
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
263  **/
264 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
265 				 struct ixgbevf_ring *tx_ring, int napi_budget)
266 {
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;
273 
274 	if (test_bit(__IXGBEVF_DOWN, &adapter->state))
275 		return true;
276 
277 	tx_buffer = &tx_ring->tx_buffer_info[i];
278 	tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
279 	i -= tx_ring->count;
280 
281 	do {
282 		union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
283 
284 		/* if next_to_watch is not set then there is no work pending */
285 		if (!eop_desc)
286 			break;
287 
288 		/* prevent any other reads prior to eop_desc */
289 		smp_rmb();
290 
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)))
293 			break;
294 
295 		/* clear next_to_watch to prevent false hangs */
296 		tx_buffer->next_to_watch = NULL;
297 
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)
302 			total_ipsec++;
303 
304 		/* free the skb */
305 		if (ring_is_xdp(tx_ring))
306 			page_frag_free(tx_buffer->data);
307 		else
308 			napi_consume_skb(tx_buffer->skb, napi_budget);
309 
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),
314 				 DMA_TO_DEVICE);
315 
316 		/* clear tx_buffer data */
317 		dma_unmap_len_set(tx_buffer, len, 0);
318 
319 		/* unmap remaining buffers */
320 		while (tx_desc != eop_desc) {
321 			tx_buffer++;
322 			tx_desc++;
323 			i++;
324 			if (unlikely(!i)) {
325 				i -= tx_ring->count;
326 				tx_buffer = tx_ring->tx_buffer_info;
327 				tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
328 			}
329 
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),
335 					       DMA_TO_DEVICE);
336 				dma_unmap_len_set(tx_buffer, len, 0);
337 			}
338 		}
339 
340 		/* move us one more past the eop_desc for start of next pkt */
341 		tx_buffer++;
342 		tx_desc++;
343 		i++;
344 		if (unlikely(!i)) {
345 			i -= tx_ring->count;
346 			tx_buffer = tx_ring->tx_buffer_info;
347 			tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
348 		}
349 
350 		/* issue prefetch for next Tx descriptor */
351 		prefetch(tx_desc);
352 
353 		/* update budget accounting */
354 		budget--;
355 	} while (likely(budget));
356 
357 	i += tx_ring->count;
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;
366 
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;
370 
371 		eop_desc = tx_ring->tx_buffer_info[i].next_to_watch;
372 
373 		pr_err("Detected Tx Unit Hang%s\n"
374 		       "  Tx Queue             <%d>\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"
382 		       "  jiffies              <%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);
390 
391 		if (!ring_is_xdp(tx_ring))
392 			netif_stop_subqueue(tx_ring->netdev,
393 					    tx_ring->queue_index);
394 
395 		/* schedule immediate reset if we believe we hung */
396 		ixgbevf_tx_timeout_reset(adapter);
397 
398 		return true;
399 	}
400 
401 	if (ring_is_xdp(tx_ring))
402 		return !!budget;
403 
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.
409 		 */
410 		smp_mb();
411 
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;
418 		}
419 	}
420 
421 	return !!budget;
422 }
423 
424 /**
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
428  **/
429 static void ixgbevf_rx_skb(struct ixgbevf_q_vector *q_vector,
430 			   struct sk_buff *skb)
431 {
432 	napi_gro_receive(&q_vector->napi, skb);
433 }
434 
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))
440 
441 static inline void ixgbevf_rx_hash(struct ixgbevf_ring *ring,
442 				   union ixgbe_adv_rx_desc *rx_desc,
443 				   struct sk_buff *skb)
444 {
445 	u16 rss_type;
446 
447 	if (!(ring->netdev->features & NETIF_F_RXHASH))
448 		return;
449 
450 	rss_type = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info) &
451 		   IXGBE_RXDADV_RSSTYPE_MASK;
452 
453 	if (!rss_type)
454 		return;
455 
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);
459 }
460 
461 /**
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
466  **/
467 static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
468 				       union ixgbe_adv_rx_desc *rx_desc,
469 				       struct sk_buff *skb)
470 {
471 	skb_checksum_none_assert(skb);
472 
473 	/* Rx csum disabled */
474 	if (!(ring->netdev->features & NETIF_F_RXCSUM))
475 		return;
476 
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++;
481 		return;
482 	}
483 
484 	if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
485 		return;
486 
487 	if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
488 		ring->rx_stats.csum_err++;
489 		return;
490 	}
491 
492 	/* It must be a TCP or UDP packet with a valid checksum */
493 	skb->ip_summed = CHECKSUM_UNNECESSARY;
494 }
495 
496 /**
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
501  *
502  * This function checks the ring, descriptor, and packet information in
503  * order to populate the checksum, VLAN, protocol, and other fields within
504  * the skb.
505  **/
506 static void ixgbevf_process_skb_fields(struct ixgbevf_ring *rx_ring,
507 				       union ixgbe_adv_rx_desc *rx_desc,
508 				       struct sk_buff *skb)
509 {
510 	ixgbevf_rx_hash(rx_ring, rx_desc, skb);
511 	ixgbevf_rx_checksum(rx_ring, rx_desc, skb);
512 
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);
516 
517 		if (test_bit(vid & VLAN_VID_MASK, active_vlans))
518 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
519 	}
520 
521 	if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_STAT_SECP))
522 		ixgbevf_ipsec_rx(rx_ring, rx_desc, skb);
523 
524 	skb->protocol = eth_type_trans(skb, rx_ring->netdev);
525 }
526 
527 static
528 struct ixgbevf_rx_buffer *ixgbevf_get_rx_buffer(struct ixgbevf_ring *rx_ring,
529 						const unsigned int size)
530 {
531 	struct ixgbevf_rx_buffer *rx_buffer;
532 
533 	rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
534 	prefetchw(rx_buffer->page);
535 
536 	/* we are reusing so sync this buffer for CPU use */
537 	dma_sync_single_range_for_cpu(rx_ring->dev,
538 				      rx_buffer->dma,
539 				      rx_buffer->page_offset,
540 				      size,
541 				      DMA_FROM_DEVICE);
542 
543 	rx_buffer->pagecnt_bias--;
544 
545 	return rx_buffer;
546 }
547 
548 static void ixgbevf_put_rx_buffer(struct ixgbevf_ring *rx_ring,
549 				  struct ixgbevf_rx_buffer *rx_buffer,
550 				  struct sk_buff *skb)
551 {
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);
555 	} else {
556 		if (IS_ERR(skb))
557 			/* We are not reusing the buffer so unmap it and free
558 			 * any references we are holding to it
559 			 */
560 			dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
561 					     ixgbevf_rx_pg_size(rx_ring),
562 					     DMA_FROM_DEVICE,
563 					     IXGBEVF_RX_DMA_ATTR);
564 		__page_frag_cache_drain(rx_buffer->page,
565 					rx_buffer->pagecnt_bias);
566 	}
567 
568 	/* clear contents of rx_buffer */
569 	rx_buffer->page = NULL;
570 }
571 
572 /**
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
576  *
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.
581  **/
582 static bool ixgbevf_is_non_eop(struct ixgbevf_ring *rx_ring,
583 			       union ixgbe_adv_rx_desc *rx_desc)
584 {
585 	u32 ntc = rx_ring->next_to_clean + 1;
586 
587 	/* fetch, update, and store next to clean */
588 	ntc = (ntc < rx_ring->count) ? ntc : 0;
589 	rx_ring->next_to_clean = ntc;
590 
591 	prefetch(IXGBEVF_RX_DESC(rx_ring, ntc));
592 
593 	if (likely(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
594 		return false;
595 
596 	return true;
597 }
598 
599 static inline unsigned int ixgbevf_rx_offset(struct ixgbevf_ring *rx_ring)
600 {
601 	return ring_uses_build_skb(rx_ring) ? IXGBEVF_SKB_PAD : 0;
602 }
603 
604 static bool ixgbevf_alloc_mapped_page(struct ixgbevf_ring *rx_ring,
605 				      struct ixgbevf_rx_buffer *bi)
606 {
607 	struct page *page = bi->page;
608 	dma_addr_t dma;
609 
610 	/* since we are recycling buffers we should seldom need to alloc */
611 	if (likely(page))
612 		return true;
613 
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++;
618 		return false;
619 	}
620 
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);
625 
626 	/* if mapping failed free memory back to system since
627 	 * there isn't much point in holding memory we can't use
628 	 */
629 	if (dma_mapping_error(rx_ring->dev, dma)) {
630 		__free_pages(page, ixgbevf_rx_pg_order(rx_ring));
631 
632 		rx_ring->rx_stats.alloc_rx_page_failed++;
633 		return false;
634 	}
635 
636 	bi->dma = dma;
637 	bi->page = page;
638 	bi->page_offset = ixgbevf_rx_offset(rx_ring);
639 	bi->pagecnt_bias = 1;
640 	rx_ring->rx_stats.alloc_rx_page++;
641 
642 	return true;
643 }
644 
645 /**
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
649  **/
650 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
651 				     u16 cleaned_count)
652 {
653 	union ixgbe_adv_rx_desc *rx_desc;
654 	struct ixgbevf_rx_buffer *bi;
655 	unsigned int i = rx_ring->next_to_use;
656 
657 	/* nothing to do or no valid netdev defined */
658 	if (!cleaned_count || !rx_ring->netdev)
659 		return;
660 
661 	rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
662 	bi = &rx_ring->rx_buffer_info[i];
663 	i -= rx_ring->count;
664 
665 	do {
666 		if (!ixgbevf_alloc_mapped_page(rx_ring, bi))
667 			break;
668 
669 		/* sync the buffer for use by the device */
670 		dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
671 						 bi->page_offset,
672 						 ixgbevf_rx_bufsz(rx_ring),
673 						 DMA_FROM_DEVICE);
674 
675 		/* Refresh the desc even if pkt_addr didn't change
676 		 * because each write-back erases this info.
677 		 */
678 		rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
679 
680 		rx_desc++;
681 		bi++;
682 		i++;
683 		if (unlikely(!i)) {
684 			rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
685 			bi = rx_ring->rx_buffer_info;
686 			i -= rx_ring->count;
687 		}
688 
689 		/* clear the length for the next_to_use descriptor */
690 		rx_desc->wb.upper.length = 0;
691 
692 		cleaned_count--;
693 	} while (cleaned_count);
694 
695 	i += rx_ring->count;
696 
697 	if (rx_ring->next_to_use != i) {
698 		/* record the next descriptor to use */
699 		rx_ring->next_to_use = i;
700 
701 		/* update next to alloc since we have filled the ring */
702 		rx_ring->next_to_alloc = i;
703 
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,
707 		 * such as IA-64).
708 		 */
709 		wmb();
710 		ixgbevf_write_tail(rx_ring, i);
711 	}
712 }
713 
714 /**
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
719  *
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.
723  *
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.
726  *
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.
729  *
730  * Returns true if an error was encountered and skb was freed.
731  **/
732 static bool ixgbevf_cleanup_headers(struct ixgbevf_ring *rx_ring,
733 				    union ixgbe_adv_rx_desc *rx_desc,
734 				    struct sk_buff *skb)
735 {
736 	/* XDP packets use error pointer so abort at this point */
737 	if (IS_ERR(skb))
738 		return true;
739 
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;
744 
745 		if (!(netdev->features & NETIF_F_RXALL)) {
746 			dev_kfree_skb_any(skb);
747 			return true;
748 		}
749 	}
750 
751 	/* if eth_skb_pad returns an error the skb was freed */
752 	if (eth_skb_pad(skb))
753 		return true;
754 
755 	return false;
756 }
757 
758 /**
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
762  *
763  * Synchronizes page for reuse by the adapter
764  **/
765 static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
766 				  struct ixgbevf_rx_buffer *old_buff)
767 {
768 	struct ixgbevf_rx_buffer *new_buff;
769 	u16 nta = rx_ring->next_to_alloc;
770 
771 	new_buff = &rx_ring->rx_buffer_info[nta];
772 
773 	/* update, and store next to alloc */
774 	nta++;
775 	rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
776 
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;
782 }
783 
784 static bool ixgbevf_can_reuse_rx_page(struct ixgbevf_rx_buffer *rx_buffer)
785 {
786 	unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
787 	struct page *page = rx_buffer->page;
788 
789 	/* avoid re-using remote and pfmemalloc pages */
790 	if (!dev_page_is_reusable(page))
791 		return false;
792 
793 #if (PAGE_SIZE < 8192)
794 	/* if we are only owner of page we can reuse it */
795 	if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
796 		return false;
797 #else
798 #define IXGBEVF_LAST_OFFSET \
799 	(SKB_WITH_OVERHEAD(PAGE_SIZE) - IXGBEVF_RXBUFFER_2048)
800 
801 	if (rx_buffer->page_offset > IXGBEVF_LAST_OFFSET)
802 		return false;
803 
804 #endif
805 
806 	/* If we have drained the page fragment pool we need to update
807 	 * the pagecnt_bias and page count so that we fully restock the
808 	 * number of references the driver holds.
809 	 */
810 	if (unlikely(!pagecnt_bias)) {
811 		page_ref_add(page, USHRT_MAX);
812 		rx_buffer->pagecnt_bias = USHRT_MAX;
813 	}
814 
815 	return true;
816 }
817 
818 /**
819  * ixgbevf_add_rx_frag - Add contents of Rx buffer to sk_buff
820  * @rx_ring: rx descriptor ring to transact packets on
821  * @rx_buffer: buffer containing page to add
822  * @skb: sk_buff to place the data into
823  * @size: size of buffer to be added
824  *
825  * This function will add the data contained in rx_buffer->page to the skb.
826  **/
827 static void ixgbevf_add_rx_frag(struct ixgbevf_ring *rx_ring,
828 				struct ixgbevf_rx_buffer *rx_buffer,
829 				struct sk_buff *skb,
830 				unsigned int size)
831 {
832 #if (PAGE_SIZE < 8192)
833 	unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
834 #else
835 	unsigned int truesize = ring_uses_build_skb(rx_ring) ?
836 				SKB_DATA_ALIGN(IXGBEVF_SKB_PAD + size) :
837 				SKB_DATA_ALIGN(size);
838 #endif
839 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
840 			rx_buffer->page_offset, size, truesize);
841 #if (PAGE_SIZE < 8192)
842 	rx_buffer->page_offset ^= truesize;
843 #else
844 	rx_buffer->page_offset += truesize;
845 #endif
846 }
847 
848 static
849 struct sk_buff *ixgbevf_construct_skb(struct ixgbevf_ring *rx_ring,
850 				      struct ixgbevf_rx_buffer *rx_buffer,
851 				      struct xdp_buff *xdp,
852 				      union ixgbe_adv_rx_desc *rx_desc)
853 {
854 	unsigned int size = xdp->data_end - xdp->data;
855 #if (PAGE_SIZE < 8192)
856 	unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
857 #else
858 	unsigned int truesize = SKB_DATA_ALIGN(xdp->data_end -
859 					       xdp->data_hard_start);
860 #endif
861 	unsigned int headlen;
862 	struct sk_buff *skb;
863 
864 	/* prefetch first cache line of first page */
865 	net_prefetch(xdp->data);
866 
867 	/* Note, we get here by enabling legacy-rx via:
868 	 *
869 	 *    ethtool --set-priv-flags <dev> legacy-rx on
870 	 *
871 	 * In this mode, we currently get 0 extra XDP headroom as
872 	 * opposed to having legacy-rx off, where we process XDP
873 	 * packets going to stack via ixgbevf_build_skb().
874 	 *
875 	 * For ixgbevf_construct_skb() mode it means that the
876 	 * xdp->data_meta will always point to xdp->data, since
877 	 * the helper cannot expand the head. Should this ever
878 	 * changed in future for legacy-rx mode on, then lets also
879 	 * add xdp->data_meta handling here.
880 	 */
881 
882 	/* allocate a skb to store the frags */
883 	skb = napi_alloc_skb(&rx_ring->q_vector->napi, IXGBEVF_RX_HDR_SIZE);
884 	if (unlikely(!skb))
885 		return NULL;
886 
887 	/* Determine available headroom for copy */
888 	headlen = size;
889 	if (headlen > IXGBEVF_RX_HDR_SIZE)
890 		headlen = eth_get_headlen(skb->dev, xdp->data,
891 					  IXGBEVF_RX_HDR_SIZE);
892 
893 	/* align pull length to size of long to optimize memcpy performance */
894 	memcpy(__skb_put(skb, headlen), xdp->data,
895 	       ALIGN(headlen, sizeof(long)));
896 
897 	/* update all of the pointers */
898 	size -= headlen;
899 	if (size) {
900 		skb_add_rx_frag(skb, 0, rx_buffer->page,
901 				(xdp->data + headlen) -
902 					page_address(rx_buffer->page),
903 				size, truesize);
904 #if (PAGE_SIZE < 8192)
905 		rx_buffer->page_offset ^= truesize;
906 #else
907 		rx_buffer->page_offset += truesize;
908 #endif
909 	} else {
910 		rx_buffer->pagecnt_bias++;
911 	}
912 
913 	return skb;
914 }
915 
916 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
917 					     u32 qmask)
918 {
919 	struct ixgbe_hw *hw = &adapter->hw;
920 
921 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
922 }
923 
924 static struct sk_buff *ixgbevf_build_skb(struct ixgbevf_ring *rx_ring,
925 					 struct ixgbevf_rx_buffer *rx_buffer,
926 					 struct xdp_buff *xdp,
927 					 union ixgbe_adv_rx_desc *rx_desc)
928 {
929 	unsigned int metasize = xdp->data - xdp->data_meta;
930 #if (PAGE_SIZE < 8192)
931 	unsigned int truesize = ixgbevf_rx_pg_size(rx_ring) / 2;
932 #else
933 	unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
934 				SKB_DATA_ALIGN(xdp->data_end -
935 					       xdp->data_hard_start);
936 #endif
937 	struct sk_buff *skb;
938 
939 	/* Prefetch first cache line of first page. If xdp->data_meta
940 	 * is unused, this points to xdp->data, otherwise, we likely
941 	 * have a consumer accessing first few bytes of meta data,
942 	 * and then actual data.
943 	 */
944 	net_prefetch(xdp->data_meta);
945 
946 	/* build an skb around the page buffer */
947 	skb = build_skb(xdp->data_hard_start, truesize);
948 	if (unlikely(!skb))
949 		return NULL;
950 
951 	/* update pointers within the skb to store the data */
952 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
953 	__skb_put(skb, xdp->data_end - xdp->data);
954 	if (metasize)
955 		skb_metadata_set(skb, metasize);
956 
957 	/* update buffer offset */
958 #if (PAGE_SIZE < 8192)
959 	rx_buffer->page_offset ^= truesize;
960 #else
961 	rx_buffer->page_offset += truesize;
962 #endif
963 
964 	return skb;
965 }
966 
967 #define IXGBEVF_XDP_PASS 0
968 #define IXGBEVF_XDP_CONSUMED 1
969 #define IXGBEVF_XDP_TX 2
970 
971 static int ixgbevf_xmit_xdp_ring(struct ixgbevf_ring *ring,
972 				 struct xdp_buff *xdp)
973 {
974 	struct ixgbevf_tx_buffer *tx_buffer;
975 	union ixgbe_adv_tx_desc *tx_desc;
976 	u32 len, cmd_type;
977 	dma_addr_t dma;
978 	u16 i;
979 
980 	len = xdp->data_end - xdp->data;
981 
982 	if (unlikely(!ixgbevf_desc_unused(ring)))
983 		return IXGBEVF_XDP_CONSUMED;
984 
985 	dma = dma_map_single(ring->dev, xdp->data, len, DMA_TO_DEVICE);
986 	if (dma_mapping_error(ring->dev, dma))
987 		return IXGBEVF_XDP_CONSUMED;
988 
989 	/* record the location of the first descriptor for this packet */
990 	i = ring->next_to_use;
991 	tx_buffer = &ring->tx_buffer_info[i];
992 
993 	dma_unmap_len_set(tx_buffer, len, len);
994 	dma_unmap_addr_set(tx_buffer, dma, dma);
995 	tx_buffer->data = xdp->data;
996 	tx_buffer->bytecount = len;
997 	tx_buffer->gso_segs = 1;
998 	tx_buffer->protocol = 0;
999 
1000 	/* Populate minimal context descriptor that will provide for the
1001 	 * fact that we are expected to process Ethernet frames.
1002 	 */
1003 	if (!test_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state)) {
1004 		struct ixgbe_adv_tx_context_desc *context_desc;
1005 
1006 		set_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state);
1007 
1008 		context_desc = IXGBEVF_TX_CTXTDESC(ring, 0);
1009 		context_desc->vlan_macip_lens	=
1010 			cpu_to_le32(ETH_HLEN << IXGBE_ADVTXD_MACLEN_SHIFT);
1011 		context_desc->fceof_saidx	= 0;
1012 		context_desc->type_tucmd_mlhl	=
1013 			cpu_to_le32(IXGBE_TXD_CMD_DEXT |
1014 				    IXGBE_ADVTXD_DTYP_CTXT);
1015 		context_desc->mss_l4len_idx	= 0;
1016 
1017 		i = 1;
1018 	}
1019 
1020 	/* put descriptor type bits */
1021 	cmd_type = IXGBE_ADVTXD_DTYP_DATA |
1022 		   IXGBE_ADVTXD_DCMD_DEXT |
1023 		   IXGBE_ADVTXD_DCMD_IFCS;
1024 	cmd_type |= len | IXGBE_TXD_CMD;
1025 
1026 	tx_desc = IXGBEVF_TX_DESC(ring, i);
1027 	tx_desc->read.buffer_addr = cpu_to_le64(dma);
1028 
1029 	tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1030 	tx_desc->read.olinfo_status =
1031 			cpu_to_le32((len << IXGBE_ADVTXD_PAYLEN_SHIFT) |
1032 				    IXGBE_ADVTXD_CC);
1033 
1034 	/* Avoid any potential race with cleanup */
1035 	smp_wmb();
1036 
1037 	/* set next_to_watch value indicating a packet is present */
1038 	i++;
1039 	if (i == ring->count)
1040 		i = 0;
1041 
1042 	tx_buffer->next_to_watch = tx_desc;
1043 	ring->next_to_use = i;
1044 
1045 	return IXGBEVF_XDP_TX;
1046 }
1047 
1048 static struct sk_buff *ixgbevf_run_xdp(struct ixgbevf_adapter *adapter,
1049 				       struct ixgbevf_ring  *rx_ring,
1050 				       struct xdp_buff *xdp)
1051 {
1052 	int result = IXGBEVF_XDP_PASS;
1053 	struct ixgbevf_ring *xdp_ring;
1054 	struct bpf_prog *xdp_prog;
1055 	u32 act;
1056 
1057 	rcu_read_lock();
1058 	xdp_prog = READ_ONCE(rx_ring->xdp_prog);
1059 
1060 	if (!xdp_prog)
1061 		goto xdp_out;
1062 
1063 	act = bpf_prog_run_xdp(xdp_prog, xdp);
1064 	switch (act) {
1065 	case XDP_PASS:
1066 		break;
1067 	case XDP_TX:
1068 		xdp_ring = adapter->xdp_ring[rx_ring->queue_index];
1069 		result = ixgbevf_xmit_xdp_ring(xdp_ring, xdp);
1070 		if (result == IXGBEVF_XDP_CONSUMED)
1071 			goto out_failure;
1072 		break;
1073 	default:
1074 		bpf_warn_invalid_xdp_action(act);
1075 		fallthrough;
1076 	case XDP_ABORTED:
1077 out_failure:
1078 		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
1079 		fallthrough; /* handle aborts by dropping packet */
1080 	case XDP_DROP:
1081 		result = IXGBEVF_XDP_CONSUMED;
1082 		break;
1083 	}
1084 xdp_out:
1085 	rcu_read_unlock();
1086 	return ERR_PTR(-result);
1087 }
1088 
1089 static unsigned int ixgbevf_rx_frame_truesize(struct ixgbevf_ring *rx_ring,
1090 					      unsigned int size)
1091 {
1092 	unsigned int truesize;
1093 
1094 #if (PAGE_SIZE < 8192)
1095 	truesize = ixgbevf_rx_pg_size(rx_ring) / 2; /* Must be power-of-2 */
1096 #else
1097 	truesize = ring_uses_build_skb(rx_ring) ?
1098 		SKB_DATA_ALIGN(IXGBEVF_SKB_PAD + size) +
1099 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) :
1100 		SKB_DATA_ALIGN(size);
1101 #endif
1102 	return truesize;
1103 }
1104 
1105 static void ixgbevf_rx_buffer_flip(struct ixgbevf_ring *rx_ring,
1106 				   struct ixgbevf_rx_buffer *rx_buffer,
1107 				   unsigned int size)
1108 {
1109 	unsigned int truesize = ixgbevf_rx_frame_truesize(rx_ring, size);
1110 
1111 #if (PAGE_SIZE < 8192)
1112 	rx_buffer->page_offset ^= truesize;
1113 #else
1114 	rx_buffer->page_offset += truesize;
1115 #endif
1116 }
1117 
1118 static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
1119 				struct ixgbevf_ring *rx_ring,
1120 				int budget)
1121 {
1122 	unsigned int total_rx_bytes = 0, total_rx_packets = 0, frame_sz = 0;
1123 	struct ixgbevf_adapter *adapter = q_vector->adapter;
1124 	u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
1125 	struct sk_buff *skb = rx_ring->skb;
1126 	bool xdp_xmit = false;
1127 	struct xdp_buff xdp;
1128 
1129 	/* Frame size depend on rx_ring setup when PAGE_SIZE=4K */
1130 #if (PAGE_SIZE < 8192)
1131 	frame_sz = ixgbevf_rx_frame_truesize(rx_ring, 0);
1132 #endif
1133 	xdp_init_buff(&xdp, frame_sz, &rx_ring->xdp_rxq);
1134 
1135 	while (likely(total_rx_packets < budget)) {
1136 		struct ixgbevf_rx_buffer *rx_buffer;
1137 		union ixgbe_adv_rx_desc *rx_desc;
1138 		unsigned int size;
1139 
1140 		/* return some buffers to hardware, one at a time is too slow */
1141 		if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
1142 			ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
1143 			cleaned_count = 0;
1144 		}
1145 
1146 		rx_desc = IXGBEVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
1147 		size = le16_to_cpu(rx_desc->wb.upper.length);
1148 		if (!size)
1149 			break;
1150 
1151 		/* This memory barrier is needed to keep us from reading
1152 		 * any other fields out of the rx_desc until we know the
1153 		 * RXD_STAT_DD bit is set
1154 		 */
1155 		rmb();
1156 
1157 		rx_buffer = ixgbevf_get_rx_buffer(rx_ring, size);
1158 
1159 		/* retrieve a buffer from the ring */
1160 		if (!skb) {
1161 			unsigned int offset = ixgbevf_rx_offset(rx_ring);
1162 			unsigned char *hard_start;
1163 
1164 			hard_start = page_address(rx_buffer->page) +
1165 				     rx_buffer->page_offset - offset;
1166 			xdp_prepare_buff(&xdp, hard_start, offset, size, true);
1167 #if (PAGE_SIZE > 4096)
1168 			/* At larger PAGE_SIZE, frame_sz depend on len size */
1169 			xdp.frame_sz = ixgbevf_rx_frame_truesize(rx_ring, size);
1170 #endif
1171 			skb = ixgbevf_run_xdp(adapter, rx_ring, &xdp);
1172 		}
1173 
1174 		if (IS_ERR(skb)) {
1175 			if (PTR_ERR(skb) == -IXGBEVF_XDP_TX) {
1176 				xdp_xmit = true;
1177 				ixgbevf_rx_buffer_flip(rx_ring, rx_buffer,
1178 						       size);
1179 			} else {
1180 				rx_buffer->pagecnt_bias++;
1181 			}
1182 			total_rx_packets++;
1183 			total_rx_bytes += size;
1184 		} else if (skb) {
1185 			ixgbevf_add_rx_frag(rx_ring, rx_buffer, skb, size);
1186 		} else if (ring_uses_build_skb(rx_ring)) {
1187 			skb = ixgbevf_build_skb(rx_ring, rx_buffer,
1188 						&xdp, rx_desc);
1189 		} else {
1190 			skb = ixgbevf_construct_skb(rx_ring, rx_buffer,
1191 						    &xdp, rx_desc);
1192 		}
1193 
1194 		/* exit if we failed to retrieve a buffer */
1195 		if (!skb) {
1196 			rx_ring->rx_stats.alloc_rx_buff_failed++;
1197 			rx_buffer->pagecnt_bias++;
1198 			break;
1199 		}
1200 
1201 		ixgbevf_put_rx_buffer(rx_ring, rx_buffer, skb);
1202 		cleaned_count++;
1203 
1204 		/* fetch next buffer in frame if non-eop */
1205 		if (ixgbevf_is_non_eop(rx_ring, rx_desc))
1206 			continue;
1207 
1208 		/* verify the packet layout is correct */
1209 		if (ixgbevf_cleanup_headers(rx_ring, rx_desc, skb)) {
1210 			skb = NULL;
1211 			continue;
1212 		}
1213 
1214 		/* probably a little skewed due to removing CRC */
1215 		total_rx_bytes += skb->len;
1216 
1217 		/* Workaround hardware that can't do proper VEPA multicast
1218 		 * source pruning.
1219 		 */
1220 		if ((skb->pkt_type == PACKET_BROADCAST ||
1221 		     skb->pkt_type == PACKET_MULTICAST) &&
1222 		    ether_addr_equal(rx_ring->netdev->dev_addr,
1223 				     eth_hdr(skb)->h_source)) {
1224 			dev_kfree_skb_irq(skb);
1225 			continue;
1226 		}
1227 
1228 		/* populate checksum, VLAN, and protocol */
1229 		ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
1230 
1231 		ixgbevf_rx_skb(q_vector, skb);
1232 
1233 		/* reset skb pointer */
1234 		skb = NULL;
1235 
1236 		/* update budget accounting */
1237 		total_rx_packets++;
1238 	}
1239 
1240 	/* place incomplete frames back on ring for completion */
1241 	rx_ring->skb = skb;
1242 
1243 	if (xdp_xmit) {
1244 		struct ixgbevf_ring *xdp_ring =
1245 			adapter->xdp_ring[rx_ring->queue_index];
1246 
1247 		/* Force memory writes to complete before letting h/w
1248 		 * know there are new descriptors to fetch.
1249 		 */
1250 		wmb();
1251 		ixgbevf_write_tail(xdp_ring, xdp_ring->next_to_use);
1252 	}
1253 
1254 	u64_stats_update_begin(&rx_ring->syncp);
1255 	rx_ring->stats.packets += total_rx_packets;
1256 	rx_ring->stats.bytes += total_rx_bytes;
1257 	u64_stats_update_end(&rx_ring->syncp);
1258 	q_vector->rx.total_packets += total_rx_packets;
1259 	q_vector->rx.total_bytes += total_rx_bytes;
1260 
1261 	return total_rx_packets;
1262 }
1263 
1264 /**
1265  * ixgbevf_poll - NAPI polling calback
1266  * @napi: napi struct with our devices info in it
1267  * @budget: amount of work driver is allowed to do this pass, in packets
1268  *
1269  * This function will clean more than one or more rings associated with a
1270  * q_vector.
1271  **/
1272 static int ixgbevf_poll(struct napi_struct *napi, int budget)
1273 {
1274 	struct ixgbevf_q_vector *q_vector =
1275 		container_of(napi, struct ixgbevf_q_vector, napi);
1276 	struct ixgbevf_adapter *adapter = q_vector->adapter;
1277 	struct ixgbevf_ring *ring;
1278 	int per_ring_budget, work_done = 0;
1279 	bool clean_complete = true;
1280 
1281 	ixgbevf_for_each_ring(ring, q_vector->tx) {
1282 		if (!ixgbevf_clean_tx_irq(q_vector, ring, budget))
1283 			clean_complete = false;
1284 	}
1285 
1286 	if (budget <= 0)
1287 		return budget;
1288 
1289 	/* attempt to distribute budget to each queue fairly, but don't allow
1290 	 * the budget to go below 1 because we'll exit polling
1291 	 */
1292 	if (q_vector->rx.count > 1)
1293 		per_ring_budget = max(budget/q_vector->rx.count, 1);
1294 	else
1295 		per_ring_budget = budget;
1296 
1297 	ixgbevf_for_each_ring(ring, q_vector->rx) {
1298 		int cleaned = ixgbevf_clean_rx_irq(q_vector, ring,
1299 						   per_ring_budget);
1300 		work_done += cleaned;
1301 		if (cleaned >= per_ring_budget)
1302 			clean_complete = false;
1303 	}
1304 
1305 	/* If all work not completed, return budget and keep polling */
1306 	if (!clean_complete)
1307 		return budget;
1308 
1309 	/* Exit the polling mode, but don't re-enable interrupts if stack might
1310 	 * poll us due to busy-polling
1311 	 */
1312 	if (likely(napi_complete_done(napi, work_done))) {
1313 		if (adapter->rx_itr_setting == 1)
1314 			ixgbevf_set_itr(q_vector);
1315 		if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
1316 		    !test_bit(__IXGBEVF_REMOVING, &adapter->state))
1317 			ixgbevf_irq_enable_queues(adapter,
1318 						  BIT(q_vector->v_idx));
1319 	}
1320 
1321 	return min(work_done, budget - 1);
1322 }
1323 
1324 /**
1325  * ixgbevf_write_eitr - write VTEITR register in hardware specific way
1326  * @q_vector: structure containing interrupt and ring information
1327  **/
1328 void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
1329 {
1330 	struct ixgbevf_adapter *adapter = q_vector->adapter;
1331 	struct ixgbe_hw *hw = &adapter->hw;
1332 	int v_idx = q_vector->v_idx;
1333 	u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
1334 
1335 	/* set the WDIS bit to not clear the timer bits and cause an
1336 	 * immediate assertion of the interrupt
1337 	 */
1338 	itr_reg |= IXGBE_EITR_CNT_WDIS;
1339 
1340 	IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
1341 }
1342 
1343 /**
1344  * ixgbevf_configure_msix - Configure MSI-X hardware
1345  * @adapter: board private structure
1346  *
1347  * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
1348  * interrupts.
1349  **/
1350 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
1351 {
1352 	struct ixgbevf_q_vector *q_vector;
1353 	int q_vectors, v_idx;
1354 
1355 	q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1356 	adapter->eims_enable_mask = 0;
1357 
1358 	/* Populate the IVAR table and set the ITR values to the
1359 	 * corresponding register.
1360 	 */
1361 	for (v_idx = 0; v_idx < q_vectors; v_idx++) {
1362 		struct ixgbevf_ring *ring;
1363 
1364 		q_vector = adapter->q_vector[v_idx];
1365 
1366 		ixgbevf_for_each_ring(ring, q_vector->rx)
1367 			ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
1368 
1369 		ixgbevf_for_each_ring(ring, q_vector->tx)
1370 			ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
1371 
1372 		if (q_vector->tx.ring && !q_vector->rx.ring) {
1373 			/* Tx only vector */
1374 			if (adapter->tx_itr_setting == 1)
1375 				q_vector->itr = IXGBE_12K_ITR;
1376 			else
1377 				q_vector->itr = adapter->tx_itr_setting;
1378 		} else {
1379 			/* Rx or Rx/Tx vector */
1380 			if (adapter->rx_itr_setting == 1)
1381 				q_vector->itr = IXGBE_20K_ITR;
1382 			else
1383 				q_vector->itr = adapter->rx_itr_setting;
1384 		}
1385 
1386 		/* add q_vector eims value to global eims_enable_mask */
1387 		adapter->eims_enable_mask |= BIT(v_idx);
1388 
1389 		ixgbevf_write_eitr(q_vector);
1390 	}
1391 
1392 	ixgbevf_set_ivar(adapter, -1, 1, v_idx);
1393 	/* setup eims_other and add value to global eims_enable_mask */
1394 	adapter->eims_other = BIT(v_idx);
1395 	adapter->eims_enable_mask |= adapter->eims_other;
1396 }
1397 
1398 enum latency_range {
1399 	lowest_latency = 0,
1400 	low_latency = 1,
1401 	bulk_latency = 2,
1402 	latency_invalid = 255
1403 };
1404 
1405 /**
1406  * ixgbevf_update_itr - update the dynamic ITR value based on statistics
1407  * @q_vector: structure containing interrupt and ring information
1408  * @ring_container: structure containing ring performance data
1409  *
1410  * Stores a new ITR value based on packets and byte
1411  * counts during the last interrupt.  The advantage of per interrupt
1412  * computation is faster updates and more accurate ITR for the current
1413  * traffic pattern.  Constants in this function were computed
1414  * based on theoretical maximum wire speed and thresholds were set based
1415  * on testing data as well as attempting to minimize response time
1416  * while increasing bulk throughput.
1417  **/
1418 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
1419 			       struct ixgbevf_ring_container *ring_container)
1420 {
1421 	int bytes = ring_container->total_bytes;
1422 	int packets = ring_container->total_packets;
1423 	u32 timepassed_us;
1424 	u64 bytes_perint;
1425 	u8 itr_setting = ring_container->itr;
1426 
1427 	if (packets == 0)
1428 		return;
1429 
1430 	/* simple throttle rate management
1431 	 *    0-20MB/s lowest (100000 ints/s)
1432 	 *   20-100MB/s low   (20000 ints/s)
1433 	 *  100-1249MB/s bulk (12000 ints/s)
1434 	 */
1435 	/* what was last interrupt timeslice? */
1436 	timepassed_us = q_vector->itr >> 2;
1437 	if (timepassed_us == 0)
1438 		return;
1439 
1440 	bytes_perint = bytes / timepassed_us; /* bytes/usec */
1441 
1442 	switch (itr_setting) {
1443 	case lowest_latency:
1444 		if (bytes_perint > 10)
1445 			itr_setting = low_latency;
1446 		break;
1447 	case low_latency:
1448 		if (bytes_perint > 20)
1449 			itr_setting = bulk_latency;
1450 		else if (bytes_perint <= 10)
1451 			itr_setting = lowest_latency;
1452 		break;
1453 	case bulk_latency:
1454 		if (bytes_perint <= 20)
1455 			itr_setting = low_latency;
1456 		break;
1457 	}
1458 
1459 	/* clear work counters since we have the values we need */
1460 	ring_container->total_bytes = 0;
1461 	ring_container->total_packets = 0;
1462 
1463 	/* write updated itr to ring container */
1464 	ring_container->itr = itr_setting;
1465 }
1466 
1467 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
1468 {
1469 	u32 new_itr = q_vector->itr;
1470 	u8 current_itr;
1471 
1472 	ixgbevf_update_itr(q_vector, &q_vector->tx);
1473 	ixgbevf_update_itr(q_vector, &q_vector->rx);
1474 
1475 	current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
1476 
1477 	switch (current_itr) {
1478 	/* counts and packets in update_itr are dependent on these numbers */
1479 	case lowest_latency:
1480 		new_itr = IXGBE_100K_ITR;
1481 		break;
1482 	case low_latency:
1483 		new_itr = IXGBE_20K_ITR;
1484 		break;
1485 	case bulk_latency:
1486 		new_itr = IXGBE_12K_ITR;
1487 		break;
1488 	default:
1489 		break;
1490 	}
1491 
1492 	if (new_itr != q_vector->itr) {
1493 		/* do an exponential smoothing */
1494 		new_itr = (10 * new_itr * q_vector->itr) /
1495 			  ((9 * new_itr) + q_vector->itr);
1496 
1497 		/* save the algorithm value here */
1498 		q_vector->itr = new_itr;
1499 
1500 		ixgbevf_write_eitr(q_vector);
1501 	}
1502 }
1503 
1504 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
1505 {
1506 	struct ixgbevf_adapter *adapter = data;
1507 	struct ixgbe_hw *hw = &adapter->hw;
1508 
1509 	hw->mac.get_link_status = 1;
1510 
1511 	ixgbevf_service_event_schedule(adapter);
1512 
1513 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
1514 
1515 	return IRQ_HANDLED;
1516 }
1517 
1518 /**
1519  * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
1520  * @irq: unused
1521  * @data: pointer to our q_vector struct for this interrupt vector
1522  **/
1523 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
1524 {
1525 	struct ixgbevf_q_vector *q_vector = data;
1526 
1527 	/* EIAM disabled interrupts (on this vector) for us */
1528 	if (q_vector->rx.ring || q_vector->tx.ring)
1529 		napi_schedule_irqoff(&q_vector->napi);
1530 
1531 	return IRQ_HANDLED;
1532 }
1533 
1534 /**
1535  * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1536  * @adapter: board private structure
1537  *
1538  * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1539  * interrupts from the kernel.
1540  **/
1541 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1542 {
1543 	struct net_device *netdev = adapter->netdev;
1544 	int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1545 	unsigned int ri = 0, ti = 0;
1546 	int vector, err;
1547 
1548 	for (vector = 0; vector < q_vectors; vector++) {
1549 		struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
1550 		struct msix_entry *entry = &adapter->msix_entries[vector];
1551 
1552 		if (q_vector->tx.ring && q_vector->rx.ring) {
1553 			snprintf(q_vector->name, sizeof(q_vector->name),
1554 				 "%s-TxRx-%u", netdev->name, ri++);
1555 			ti++;
1556 		} else if (q_vector->rx.ring) {
1557 			snprintf(q_vector->name, sizeof(q_vector->name),
1558 				 "%s-rx-%u", netdev->name, ri++);
1559 		} else if (q_vector->tx.ring) {
1560 			snprintf(q_vector->name, sizeof(q_vector->name),
1561 				 "%s-tx-%u", netdev->name, ti++);
1562 		} else {
1563 			/* skip this unused q_vector */
1564 			continue;
1565 		}
1566 		err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
1567 				  q_vector->name, q_vector);
1568 		if (err) {
1569 			hw_dbg(&adapter->hw,
1570 			       "request_irq failed for MSIX interrupt Error: %d\n",
1571 			       err);
1572 			goto free_queue_irqs;
1573 		}
1574 	}
1575 
1576 	err = request_irq(adapter->msix_entries[vector].vector,
1577 			  &ixgbevf_msix_other, 0, netdev->name, adapter);
1578 	if (err) {
1579 		hw_dbg(&adapter->hw, "request_irq for msix_other failed: %d\n",
1580 		       err);
1581 		goto free_queue_irqs;
1582 	}
1583 
1584 	return 0;
1585 
1586 free_queue_irqs:
1587 	while (vector) {
1588 		vector--;
1589 		free_irq(adapter->msix_entries[vector].vector,
1590 			 adapter->q_vector[vector]);
1591 	}
1592 	/* This failure is non-recoverable - it indicates the system is
1593 	 * out of MSIX vector resources and the VF driver cannot run
1594 	 * without them.  Set the number of msix vectors to zero
1595 	 * indicating that not enough can be allocated.  The error
1596 	 * will be returned to the user indicating device open failed.
1597 	 * Any further attempts to force the driver to open will also
1598 	 * fail.  The only way to recover is to unload the driver and
1599 	 * reload it again.  If the system has recovered some MSIX
1600 	 * vectors then it may succeed.
1601 	 */
1602 	adapter->num_msix_vectors = 0;
1603 	return err;
1604 }
1605 
1606 /**
1607  * ixgbevf_request_irq - initialize interrupts
1608  * @adapter: board private structure
1609  *
1610  * Attempts to configure interrupts using the best available
1611  * capabilities of the hardware and kernel.
1612  **/
1613 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1614 {
1615 	int err = ixgbevf_request_msix_irqs(adapter);
1616 
1617 	if (err)
1618 		hw_dbg(&adapter->hw, "request_irq failed, Error %d\n", err);
1619 
1620 	return err;
1621 }
1622 
1623 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1624 {
1625 	int i, q_vectors;
1626 
1627 	if (!adapter->msix_entries)
1628 		return;
1629 
1630 	q_vectors = adapter->num_msix_vectors;
1631 	i = q_vectors - 1;
1632 
1633 	free_irq(adapter->msix_entries[i].vector, adapter);
1634 	i--;
1635 
1636 	for (; i >= 0; i--) {
1637 		/* free only the irqs that were actually requested */
1638 		if (!adapter->q_vector[i]->rx.ring &&
1639 		    !adapter->q_vector[i]->tx.ring)
1640 			continue;
1641 
1642 		free_irq(adapter->msix_entries[i].vector,
1643 			 adapter->q_vector[i]);
1644 	}
1645 }
1646 
1647 /**
1648  * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1649  * @adapter: board private structure
1650  **/
1651 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1652 {
1653 	struct ixgbe_hw *hw = &adapter->hw;
1654 	int i;
1655 
1656 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
1657 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1658 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
1659 
1660 	IXGBE_WRITE_FLUSH(hw);
1661 
1662 	for (i = 0; i < adapter->num_msix_vectors; i++)
1663 		synchronize_irq(adapter->msix_entries[i].vector);
1664 }
1665 
1666 /**
1667  * ixgbevf_irq_enable - Enable default interrupt generation settings
1668  * @adapter: board private structure
1669  **/
1670 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1671 {
1672 	struct ixgbe_hw *hw = &adapter->hw;
1673 
1674 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1675 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1676 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1677 }
1678 
1679 /**
1680  * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
1681  * @adapter: board private structure
1682  * @ring: structure containing ring specific data
1683  *
1684  * Configure the Tx descriptor ring after a reset.
1685  **/
1686 static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
1687 				      struct ixgbevf_ring *ring)
1688 {
1689 	struct ixgbe_hw *hw = &adapter->hw;
1690 	u64 tdba = ring->dma;
1691 	int wait_loop = 10;
1692 	u32 txdctl = IXGBE_TXDCTL_ENABLE;
1693 	u8 reg_idx = ring->reg_idx;
1694 
1695 	/* disable queue to avoid issues while updating state */
1696 	IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
1697 	IXGBE_WRITE_FLUSH(hw);
1698 
1699 	IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
1700 	IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
1701 	IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
1702 			ring->count * sizeof(union ixgbe_adv_tx_desc));
1703 
1704 	/* disable head writeback */
1705 	IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
1706 	IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
1707 
1708 	/* enable relaxed ordering */
1709 	IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
1710 			(IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1711 			 IXGBE_DCA_TXCTRL_DATA_RRO_EN));
1712 
1713 	/* reset head and tail pointers */
1714 	IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
1715 	IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
1716 	ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
1717 
1718 	/* reset ntu and ntc to place SW in sync with hardwdare */
1719 	ring->next_to_clean = 0;
1720 	ring->next_to_use = 0;
1721 
1722 	/* In order to avoid issues WTHRESH + PTHRESH should always be equal
1723 	 * to or less than the number of on chip descriptors, which is
1724 	 * currently 40.
1725 	 */
1726 	txdctl |= (8 << 16);    /* WTHRESH = 8 */
1727 
1728 	/* Setting PTHRESH to 32 both improves performance */
1729 	txdctl |= (1u << 8) |    /* HTHRESH = 1 */
1730 		   32;           /* PTHRESH = 32 */
1731 
1732 	/* reinitialize tx_buffer_info */
1733 	memset(ring->tx_buffer_info, 0,
1734 	       sizeof(struct ixgbevf_tx_buffer) * ring->count);
1735 
1736 	clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &ring->state);
1737 	clear_bit(__IXGBEVF_TX_XDP_RING_PRIMED, &ring->state);
1738 
1739 	IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
1740 
1741 	/* poll to verify queue is enabled */
1742 	do {
1743 		usleep_range(1000, 2000);
1744 		txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
1745 	}  while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
1746 	if (!wait_loop)
1747 		hw_dbg(hw, "Could not enable Tx Queue %d\n", reg_idx);
1748 }
1749 
1750 /**
1751  * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1752  * @adapter: board private structure
1753  *
1754  * Configure the Tx unit of the MAC after a reset.
1755  **/
1756 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1757 {
1758 	u32 i;
1759 
1760 	/* Setup the HW Tx Head and Tail descriptor pointers */
1761 	for (i = 0; i < adapter->num_tx_queues; i++)
1762 		ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
1763 	for (i = 0; i < adapter->num_xdp_queues; i++)
1764 		ixgbevf_configure_tx_ring(adapter, adapter->xdp_ring[i]);
1765 }
1766 
1767 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT	2
1768 
1769 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter,
1770 				     struct ixgbevf_ring *ring, int index)
1771 {
1772 	struct ixgbe_hw *hw = &adapter->hw;
1773 	u32 srrctl;
1774 
1775 	srrctl = IXGBE_SRRCTL_DROP_EN;
1776 
1777 	srrctl |= IXGBEVF_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT;
1778 	if (ring_uses_large_buffer(ring))
1779 		srrctl |= IXGBEVF_RXBUFFER_3072 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1780 	else
1781 		srrctl |= IXGBEVF_RXBUFFER_2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1782 	srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1783 
1784 	IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1785 }
1786 
1787 static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1788 {
1789 	struct ixgbe_hw *hw = &adapter->hw;
1790 
1791 	/* PSRTYPE must be initialized in 82599 */
1792 	u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1793 		      IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1794 		      IXGBE_PSRTYPE_L2HDR;
1795 
1796 	if (adapter->num_rx_queues > 1)
1797 		psrtype |= BIT(29);
1798 
1799 	IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1800 }
1801 
1802 #define IXGBEVF_MAX_RX_DESC_POLL 10
1803 static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1804 				     struct ixgbevf_ring *ring)
1805 {
1806 	struct ixgbe_hw *hw = &adapter->hw;
1807 	int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1808 	u32 rxdctl;
1809 	u8 reg_idx = ring->reg_idx;
1810 
1811 	if (IXGBE_REMOVED(hw->hw_addr))
1812 		return;
1813 	rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1814 	rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1815 
1816 	/* write value back with RXDCTL.ENABLE bit cleared */
1817 	IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1818 
1819 	/* the hardware may take up to 100us to really disable the Rx queue */
1820 	do {
1821 		udelay(10);
1822 		rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1823 	} while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1824 
1825 	if (!wait_loop)
1826 		pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
1827 		       reg_idx);
1828 }
1829 
1830 static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1831 					 struct ixgbevf_ring *ring)
1832 {
1833 	struct ixgbe_hw *hw = &adapter->hw;
1834 	int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1835 	u32 rxdctl;
1836 	u8 reg_idx = ring->reg_idx;
1837 
1838 	if (IXGBE_REMOVED(hw->hw_addr))
1839 		return;
1840 	do {
1841 		usleep_range(1000, 2000);
1842 		rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1843 	} while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1844 
1845 	if (!wait_loop)
1846 		pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
1847 		       reg_idx);
1848 }
1849 
1850 /**
1851  * ixgbevf_init_rss_key - Initialize adapter RSS key
1852  * @adapter: device handle
1853  *
1854  * Allocates and initializes the RSS key if it is not allocated.
1855  **/
1856 static inline int ixgbevf_init_rss_key(struct ixgbevf_adapter *adapter)
1857 {
1858 	u32 *rss_key;
1859 
1860 	if (!adapter->rss_key) {
1861 		rss_key = kzalloc(IXGBEVF_RSS_HASH_KEY_SIZE, GFP_KERNEL);
1862 		if (unlikely(!rss_key))
1863 			return -ENOMEM;
1864 
1865 		netdev_rss_key_fill(rss_key, IXGBEVF_RSS_HASH_KEY_SIZE);
1866 		adapter->rss_key = rss_key;
1867 	}
1868 
1869 	return 0;
1870 }
1871 
1872 static void ixgbevf_setup_vfmrqc(struct ixgbevf_adapter *adapter)
1873 {
1874 	struct ixgbe_hw *hw = &adapter->hw;
1875 	u32 vfmrqc = 0, vfreta = 0;
1876 	u16 rss_i = adapter->num_rx_queues;
1877 	u8 i, j;
1878 
1879 	/* Fill out hash function seeds */
1880 	for (i = 0; i < IXGBEVF_VFRSSRK_REGS; i++)
1881 		IXGBE_WRITE_REG(hw, IXGBE_VFRSSRK(i), *(adapter->rss_key + i));
1882 
1883 	for (i = 0, j = 0; i < IXGBEVF_X550_VFRETA_SIZE; i++, j++) {
1884 		if (j == rss_i)
1885 			j = 0;
1886 
1887 		adapter->rss_indir_tbl[i] = j;
1888 
1889 		vfreta |= j << (i & 0x3) * 8;
1890 		if ((i & 3) == 3) {
1891 			IXGBE_WRITE_REG(hw, IXGBE_VFRETA(i >> 2), vfreta);
1892 			vfreta = 0;
1893 		}
1894 	}
1895 
1896 	/* Perform hash on these packet types */
1897 	vfmrqc |= IXGBE_VFMRQC_RSS_FIELD_IPV4 |
1898 		IXGBE_VFMRQC_RSS_FIELD_IPV4_TCP |
1899 		IXGBE_VFMRQC_RSS_FIELD_IPV6 |
1900 		IXGBE_VFMRQC_RSS_FIELD_IPV6_TCP;
1901 
1902 	vfmrqc |= IXGBE_VFMRQC_RSSEN;
1903 
1904 	IXGBE_WRITE_REG(hw, IXGBE_VFMRQC, vfmrqc);
1905 }
1906 
1907 static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
1908 				      struct ixgbevf_ring *ring)
1909 {
1910 	struct ixgbe_hw *hw = &adapter->hw;
1911 	union ixgbe_adv_rx_desc *rx_desc;
1912 	u64 rdba = ring->dma;
1913 	u32 rxdctl;
1914 	u8 reg_idx = ring->reg_idx;
1915 
1916 	/* disable queue to avoid issues while updating state */
1917 	rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1918 	ixgbevf_disable_rx_queue(adapter, ring);
1919 
1920 	IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1921 	IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
1922 	IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
1923 			ring->count * sizeof(union ixgbe_adv_rx_desc));
1924 
1925 #ifndef CONFIG_SPARC
1926 	/* enable relaxed ordering */
1927 	IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1928 			IXGBE_DCA_RXCTRL_DESC_RRO_EN);
1929 #else
1930 	IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1931 			IXGBE_DCA_RXCTRL_DESC_RRO_EN |
1932 			IXGBE_DCA_RXCTRL_DATA_WRO_EN);
1933 #endif
1934 
1935 	/* reset head and tail pointers */
1936 	IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
1937 	IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
1938 	ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
1939 
1940 	/* initialize rx_buffer_info */
1941 	memset(ring->rx_buffer_info, 0,
1942 	       sizeof(struct ixgbevf_rx_buffer) * ring->count);
1943 
1944 	/* initialize Rx descriptor 0 */
1945 	rx_desc = IXGBEVF_RX_DESC(ring, 0);
1946 	rx_desc->wb.upper.length = 0;
1947 
1948 	/* reset ntu and ntc to place SW in sync with hardwdare */
1949 	ring->next_to_clean = 0;
1950 	ring->next_to_use = 0;
1951 	ring->next_to_alloc = 0;
1952 
1953 	ixgbevf_configure_srrctl(adapter, ring, reg_idx);
1954 
1955 	/* RXDCTL.RLPML does not work on 82599 */
1956 	if (adapter->hw.mac.type != ixgbe_mac_82599_vf) {
1957 		rxdctl &= ~(IXGBE_RXDCTL_RLPMLMASK |
1958 			    IXGBE_RXDCTL_RLPML_EN);
1959 
1960 #if (PAGE_SIZE < 8192)
1961 		/* Limit the maximum frame size so we don't overrun the skb */
1962 		if (ring_uses_build_skb(ring) &&
1963 		    !ring_uses_large_buffer(ring))
1964 			rxdctl |= IXGBEVF_MAX_FRAME_BUILD_SKB |
1965 				  IXGBE_RXDCTL_RLPML_EN;
1966 #endif
1967 	}
1968 
1969 	rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1970 	IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1971 
1972 	ixgbevf_rx_desc_queue_enable(adapter, ring);
1973 	ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
1974 }
1975 
1976 static void ixgbevf_set_rx_buffer_len(struct ixgbevf_adapter *adapter,
1977 				      struct ixgbevf_ring *rx_ring)
1978 {
1979 	struct net_device *netdev = adapter->netdev;
1980 	unsigned int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
1981 
1982 	/* set build_skb and buffer size flags */
1983 	clear_ring_build_skb_enabled(rx_ring);
1984 	clear_ring_uses_large_buffer(rx_ring);
1985 
1986 	if (adapter->flags & IXGBEVF_FLAGS_LEGACY_RX)
1987 		return;
1988 
1989 	set_ring_build_skb_enabled(rx_ring);
1990 
1991 	if (PAGE_SIZE < 8192) {
1992 		if (max_frame <= IXGBEVF_MAX_FRAME_BUILD_SKB)
1993 			return;
1994 
1995 		set_ring_uses_large_buffer(rx_ring);
1996 	}
1997 }
1998 
1999 /**
2000  * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
2001  * @adapter: board private structure
2002  *
2003  * Configure the Rx unit of the MAC after a reset.
2004  **/
2005 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
2006 {
2007 	struct ixgbe_hw *hw = &adapter->hw;
2008 	struct net_device *netdev = adapter->netdev;
2009 	int i, ret;
2010 
2011 	ixgbevf_setup_psrtype(adapter);
2012 	if (hw->mac.type >= ixgbe_mac_X550_vf)
2013 		ixgbevf_setup_vfmrqc(adapter);
2014 
2015 	spin_lock_bh(&adapter->mbx_lock);
2016 	/* notify the PF of our intent to use this size of frame */
2017 	ret = hw->mac.ops.set_rlpml(hw, netdev->mtu + ETH_HLEN + ETH_FCS_LEN);
2018 	spin_unlock_bh(&adapter->mbx_lock);
2019 	if (ret)
2020 		dev_err(&adapter->pdev->dev,
2021 			"Failed to set MTU at %d\n", netdev->mtu);
2022 
2023 	/* Setup the HW Rx Head and Tail Descriptor Pointers and
2024 	 * the Base and Length of the Rx Descriptor Ring
2025 	 */
2026 	for (i = 0; i < adapter->num_rx_queues; i++) {
2027 		struct ixgbevf_ring *rx_ring = adapter->rx_ring[i];
2028 
2029 		ixgbevf_set_rx_buffer_len(adapter, rx_ring);
2030 		ixgbevf_configure_rx_ring(adapter, rx_ring);
2031 	}
2032 }
2033 
2034 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
2035 				   __be16 proto, u16 vid)
2036 {
2037 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2038 	struct ixgbe_hw *hw = &adapter->hw;
2039 	int err;
2040 
2041 	spin_lock_bh(&adapter->mbx_lock);
2042 
2043 	/* add VID to filter table */
2044 	err = hw->mac.ops.set_vfta(hw, vid, 0, true);
2045 
2046 	spin_unlock_bh(&adapter->mbx_lock);
2047 
2048 	/* translate error return types so error makes sense */
2049 	if (err == IXGBE_ERR_MBX)
2050 		return -EIO;
2051 
2052 	if (err == IXGBE_ERR_INVALID_ARGUMENT)
2053 		return -EACCES;
2054 
2055 	set_bit(vid, adapter->active_vlans);
2056 
2057 	return err;
2058 }
2059 
2060 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
2061 				    __be16 proto, u16 vid)
2062 {
2063 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2064 	struct ixgbe_hw *hw = &adapter->hw;
2065 	int err;
2066 
2067 	spin_lock_bh(&adapter->mbx_lock);
2068 
2069 	/* remove VID from filter table */
2070 	err = hw->mac.ops.set_vfta(hw, vid, 0, false);
2071 
2072 	spin_unlock_bh(&adapter->mbx_lock);
2073 
2074 	clear_bit(vid, adapter->active_vlans);
2075 
2076 	return err;
2077 }
2078 
2079 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
2080 {
2081 	u16 vid;
2082 
2083 	for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
2084 		ixgbevf_vlan_rx_add_vid(adapter->netdev,
2085 					htons(ETH_P_8021Q), vid);
2086 }
2087 
2088 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
2089 {
2090 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2091 	struct ixgbe_hw *hw = &adapter->hw;
2092 	int count = 0;
2093 
2094 	if (!netdev_uc_empty(netdev)) {
2095 		struct netdev_hw_addr *ha;
2096 
2097 		netdev_for_each_uc_addr(ha, netdev) {
2098 			hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
2099 			udelay(200);
2100 		}
2101 	} else {
2102 		/* If the list is empty then send message to PF driver to
2103 		 * clear all MAC VLANs on this VF.
2104 		 */
2105 		hw->mac.ops.set_uc_addr(hw, 0, NULL);
2106 	}
2107 
2108 	return count;
2109 }
2110 
2111 /**
2112  * ixgbevf_set_rx_mode - Multicast and unicast set
2113  * @netdev: network interface device structure
2114  *
2115  * The set_rx_method entry point is called whenever the multicast address
2116  * list, unicast address list or the network interface flags are updated.
2117  * This routine is responsible for configuring the hardware for proper
2118  * multicast mode and configuring requested unicast filters.
2119  **/
2120 static void ixgbevf_set_rx_mode(struct net_device *netdev)
2121 {
2122 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
2123 	struct ixgbe_hw *hw = &adapter->hw;
2124 	unsigned int flags = netdev->flags;
2125 	int xcast_mode;
2126 
2127 	/* request the most inclusive mode we need */
2128 	if (flags & IFF_PROMISC)
2129 		xcast_mode = IXGBEVF_XCAST_MODE_PROMISC;
2130 	else if (flags & IFF_ALLMULTI)
2131 		xcast_mode = IXGBEVF_XCAST_MODE_ALLMULTI;
2132 	else if (flags & (IFF_BROADCAST | IFF_MULTICAST))
2133 		xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
2134 	else
2135 		xcast_mode = IXGBEVF_XCAST_MODE_NONE;
2136 
2137 	spin_lock_bh(&adapter->mbx_lock);
2138 
2139 	hw->mac.ops.update_xcast_mode(hw, xcast_mode);
2140 
2141 	/* reprogram multicast list */
2142 	hw->mac.ops.update_mc_addr_list(hw, netdev);
2143 
2144 	ixgbevf_write_uc_addr_list(netdev);
2145 
2146 	spin_unlock_bh(&adapter->mbx_lock);
2147 }
2148 
2149 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
2150 {
2151 	int q_idx;
2152 	struct ixgbevf_q_vector *q_vector;
2153 	int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2154 
2155 	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
2156 		q_vector = adapter->q_vector[q_idx];
2157 		napi_enable(&q_vector->napi);
2158 	}
2159 }
2160 
2161 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
2162 {
2163 	int q_idx;
2164 	struct ixgbevf_q_vector *q_vector;
2165 	int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2166 
2167 	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
2168 		q_vector = adapter->q_vector[q_idx];
2169 		napi_disable(&q_vector->napi);
2170 	}
2171 }
2172 
2173 static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
2174 {
2175 	struct ixgbe_hw *hw = &adapter->hw;
2176 	unsigned int def_q = 0;
2177 	unsigned int num_tcs = 0;
2178 	unsigned int num_rx_queues = adapter->num_rx_queues;
2179 	unsigned int num_tx_queues = adapter->num_tx_queues;
2180 	int err;
2181 
2182 	spin_lock_bh(&adapter->mbx_lock);
2183 
2184 	/* fetch queue configuration from the PF */
2185 	err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2186 
2187 	spin_unlock_bh(&adapter->mbx_lock);
2188 
2189 	if (err)
2190 		return err;
2191 
2192 	if (num_tcs > 1) {
2193 		/* we need only one Tx queue */
2194 		num_tx_queues = 1;
2195 
2196 		/* update default Tx ring register index */
2197 		adapter->tx_ring[0]->reg_idx = def_q;
2198 
2199 		/* we need as many queues as traffic classes */
2200 		num_rx_queues = num_tcs;
2201 	}
2202 
2203 	/* if we have a bad config abort request queue reset */
2204 	if ((adapter->num_rx_queues != num_rx_queues) ||
2205 	    (adapter->num_tx_queues != num_tx_queues)) {
2206 		/* force mailbox timeout to prevent further messages */
2207 		hw->mbx.timeout = 0;
2208 
2209 		/* wait for watchdog to come around and bail us out */
2210 		set_bit(__IXGBEVF_QUEUE_RESET_REQUESTED, &adapter->state);
2211 	}
2212 
2213 	return 0;
2214 }
2215 
2216 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
2217 {
2218 	ixgbevf_configure_dcb(adapter);
2219 
2220 	ixgbevf_set_rx_mode(adapter->netdev);
2221 
2222 	ixgbevf_restore_vlan(adapter);
2223 	ixgbevf_ipsec_restore(adapter);
2224 
2225 	ixgbevf_configure_tx(adapter);
2226 	ixgbevf_configure_rx(adapter);
2227 }
2228 
2229 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
2230 {
2231 	/* Only save pre-reset stats if there are some */
2232 	if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
2233 		adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
2234 			adapter->stats.base_vfgprc;
2235 		adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
2236 			adapter->stats.base_vfgptc;
2237 		adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
2238 			adapter->stats.base_vfgorc;
2239 		adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
2240 			adapter->stats.base_vfgotc;
2241 		adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
2242 			adapter->stats.base_vfmprc;
2243 	}
2244 }
2245 
2246 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
2247 {
2248 	struct ixgbe_hw *hw = &adapter->hw;
2249 
2250 	adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
2251 	adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
2252 	adapter->stats.last_vfgorc |=
2253 		(((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
2254 	adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
2255 	adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
2256 	adapter->stats.last_vfgotc |=
2257 		(((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
2258 	adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
2259 
2260 	adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
2261 	adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
2262 	adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
2263 	adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
2264 	adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
2265 }
2266 
2267 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
2268 {
2269 	struct ixgbe_hw *hw = &adapter->hw;
2270 	static const int api[] = {
2271 		ixgbe_mbox_api_14,
2272 		ixgbe_mbox_api_13,
2273 		ixgbe_mbox_api_12,
2274 		ixgbe_mbox_api_11,
2275 		ixgbe_mbox_api_10,
2276 		ixgbe_mbox_api_unknown
2277 	};
2278 	int err, idx = 0;
2279 
2280 	spin_lock_bh(&adapter->mbx_lock);
2281 
2282 	while (api[idx] != ixgbe_mbox_api_unknown) {
2283 		err = hw->mac.ops.negotiate_api_version(hw, api[idx]);
2284 		if (!err)
2285 			break;
2286 		idx++;
2287 	}
2288 
2289 	spin_unlock_bh(&adapter->mbx_lock);
2290 }
2291 
2292 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
2293 {
2294 	struct net_device *netdev = adapter->netdev;
2295 	struct ixgbe_hw *hw = &adapter->hw;
2296 
2297 	ixgbevf_configure_msix(adapter);
2298 
2299 	spin_lock_bh(&adapter->mbx_lock);
2300 
2301 	if (is_valid_ether_addr(hw->mac.addr))
2302 		hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
2303 	else
2304 		hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
2305 
2306 	spin_unlock_bh(&adapter->mbx_lock);
2307 
2308 	smp_mb__before_atomic();
2309 	clear_bit(__IXGBEVF_DOWN, &adapter->state);
2310 	ixgbevf_napi_enable_all(adapter);
2311 
2312 	/* clear any pending interrupts, may auto mask */
2313 	IXGBE_READ_REG(hw, IXGBE_VTEICR);
2314 	ixgbevf_irq_enable(adapter);
2315 
2316 	/* enable transmits */
2317 	netif_tx_start_all_queues(netdev);
2318 
2319 	ixgbevf_save_reset_stats(adapter);
2320 	ixgbevf_init_last_counter_stats(adapter);
2321 
2322 	hw->mac.get_link_status = 1;
2323 	mod_timer(&adapter->service_timer, jiffies);
2324 }
2325 
2326 void ixgbevf_up(struct ixgbevf_adapter *adapter)
2327 {
2328 	ixgbevf_configure(adapter);
2329 
2330 	ixgbevf_up_complete(adapter);
2331 }
2332 
2333 /**
2334  * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
2335  * @rx_ring: ring to free buffers from
2336  **/
2337 static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
2338 {
2339 	u16 i = rx_ring->next_to_clean;
2340 
2341 	/* Free Rx ring sk_buff */
2342 	if (rx_ring->skb) {
2343 		dev_kfree_skb(rx_ring->skb);
2344 		rx_ring->skb = NULL;
2345 	}
2346 
2347 	/* Free all the Rx ring pages */
2348 	while (i != rx_ring->next_to_alloc) {
2349 		struct ixgbevf_rx_buffer *rx_buffer;
2350 
2351 		rx_buffer = &rx_ring->rx_buffer_info[i];
2352 
2353 		/* Invalidate cache lines that may have been written to by
2354 		 * device so that we avoid corrupting memory.
2355 		 */
2356 		dma_sync_single_range_for_cpu(rx_ring->dev,
2357 					      rx_buffer->dma,
2358 					      rx_buffer->page_offset,
2359 					      ixgbevf_rx_bufsz(rx_ring),
2360 					      DMA_FROM_DEVICE);
2361 
2362 		/* free resources associated with mapping */
2363 		dma_unmap_page_attrs(rx_ring->dev,
2364 				     rx_buffer->dma,
2365 				     ixgbevf_rx_pg_size(rx_ring),
2366 				     DMA_FROM_DEVICE,
2367 				     IXGBEVF_RX_DMA_ATTR);
2368 
2369 		__page_frag_cache_drain(rx_buffer->page,
2370 					rx_buffer->pagecnt_bias);
2371 
2372 		i++;
2373 		if (i == rx_ring->count)
2374 			i = 0;
2375 	}
2376 
2377 	rx_ring->next_to_alloc = 0;
2378 	rx_ring->next_to_clean = 0;
2379 	rx_ring->next_to_use = 0;
2380 }
2381 
2382 /**
2383  * ixgbevf_clean_tx_ring - Free Tx Buffers
2384  * @tx_ring: ring to be cleaned
2385  **/
2386 static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
2387 {
2388 	u16 i = tx_ring->next_to_clean;
2389 	struct ixgbevf_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
2390 
2391 	while (i != tx_ring->next_to_use) {
2392 		union ixgbe_adv_tx_desc *eop_desc, *tx_desc;
2393 
2394 		/* Free all the Tx ring sk_buffs */
2395 		if (ring_is_xdp(tx_ring))
2396 			page_frag_free(tx_buffer->data);
2397 		else
2398 			dev_kfree_skb_any(tx_buffer->skb);
2399 
2400 		/* unmap skb header data */
2401 		dma_unmap_single(tx_ring->dev,
2402 				 dma_unmap_addr(tx_buffer, dma),
2403 				 dma_unmap_len(tx_buffer, len),
2404 				 DMA_TO_DEVICE);
2405 
2406 		/* check for eop_desc to determine the end of the packet */
2407 		eop_desc = tx_buffer->next_to_watch;
2408 		tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2409 
2410 		/* unmap remaining buffers */
2411 		while (tx_desc != eop_desc) {
2412 			tx_buffer++;
2413 			tx_desc++;
2414 			i++;
2415 			if (unlikely(i == tx_ring->count)) {
2416 				i = 0;
2417 				tx_buffer = tx_ring->tx_buffer_info;
2418 				tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
2419 			}
2420 
2421 			/* unmap any remaining paged data */
2422 			if (dma_unmap_len(tx_buffer, len))
2423 				dma_unmap_page(tx_ring->dev,
2424 					       dma_unmap_addr(tx_buffer, dma),
2425 					       dma_unmap_len(tx_buffer, len),
2426 					       DMA_TO_DEVICE);
2427 		}
2428 
2429 		/* move us one more past the eop_desc for start of next pkt */
2430 		tx_buffer++;
2431 		i++;
2432 		if (unlikely(i == tx_ring->count)) {
2433 			i = 0;
2434 			tx_buffer = tx_ring->tx_buffer_info;
2435 		}
2436 	}
2437 
2438 	/* reset next_to_use and next_to_clean */
2439 	tx_ring->next_to_use = 0;
2440 	tx_ring->next_to_clean = 0;
2441 
2442 }
2443 
2444 /**
2445  * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
2446  * @adapter: board private structure
2447  **/
2448 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
2449 {
2450 	int i;
2451 
2452 	for (i = 0; i < adapter->num_rx_queues; i++)
2453 		ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
2454 }
2455 
2456 /**
2457  * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
2458  * @adapter: board private structure
2459  **/
2460 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
2461 {
2462 	int i;
2463 
2464 	for (i = 0; i < adapter->num_tx_queues; i++)
2465 		ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
2466 	for (i = 0; i < adapter->num_xdp_queues; i++)
2467 		ixgbevf_clean_tx_ring(adapter->xdp_ring[i]);
2468 }
2469 
2470 void ixgbevf_down(struct ixgbevf_adapter *adapter)
2471 {
2472 	struct net_device *netdev = adapter->netdev;
2473 	struct ixgbe_hw *hw = &adapter->hw;
2474 	int i;
2475 
2476 	/* signal that we are down to the interrupt handler */
2477 	if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
2478 		return; /* do nothing if already down */
2479 
2480 	/* disable all enabled Rx queues */
2481 	for (i = 0; i < adapter->num_rx_queues; i++)
2482 		ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
2483 
2484 	usleep_range(10000, 20000);
2485 
2486 	netif_tx_stop_all_queues(netdev);
2487 
2488 	/* call carrier off first to avoid false dev_watchdog timeouts */
2489 	netif_carrier_off(netdev);
2490 	netif_tx_disable(netdev);
2491 
2492 	ixgbevf_irq_disable(adapter);
2493 
2494 	ixgbevf_napi_disable_all(adapter);
2495 
2496 	del_timer_sync(&adapter->service_timer);
2497 
2498 	/* disable transmits in the hardware now that interrupts are off */
2499 	for (i = 0; i < adapter->num_tx_queues; i++) {
2500 		u8 reg_idx = adapter->tx_ring[i]->reg_idx;
2501 
2502 		IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2503 				IXGBE_TXDCTL_SWFLSH);
2504 	}
2505 
2506 	for (i = 0; i < adapter->num_xdp_queues; i++) {
2507 		u8 reg_idx = adapter->xdp_ring[i]->reg_idx;
2508 
2509 		IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2510 				IXGBE_TXDCTL_SWFLSH);
2511 	}
2512 
2513 	if (!pci_channel_offline(adapter->pdev))
2514 		ixgbevf_reset(adapter);
2515 
2516 	ixgbevf_clean_all_tx_rings(adapter);
2517 	ixgbevf_clean_all_rx_rings(adapter);
2518 }
2519 
2520 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
2521 {
2522 	while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
2523 		msleep(1);
2524 
2525 	ixgbevf_down(adapter);
2526 	pci_set_master(adapter->pdev);
2527 	ixgbevf_up(adapter);
2528 
2529 	clear_bit(__IXGBEVF_RESETTING, &adapter->state);
2530 }
2531 
2532 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
2533 {
2534 	struct ixgbe_hw *hw = &adapter->hw;
2535 	struct net_device *netdev = adapter->netdev;
2536 
2537 	if (hw->mac.ops.reset_hw(hw)) {
2538 		hw_dbg(hw, "PF still resetting\n");
2539 	} else {
2540 		hw->mac.ops.init_hw(hw);
2541 		ixgbevf_negotiate_api(adapter);
2542 	}
2543 
2544 	if (is_valid_ether_addr(adapter->hw.mac.addr)) {
2545 		ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2546 		ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2547 	}
2548 
2549 	adapter->last_reset = jiffies;
2550 }
2551 
2552 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
2553 					int vectors)
2554 {
2555 	int vector_threshold;
2556 
2557 	/* We'll want at least 2 (vector_threshold):
2558 	 * 1) TxQ[0] + RxQ[0] handler
2559 	 * 2) Other (Link Status Change, etc.)
2560 	 */
2561 	vector_threshold = MIN_MSIX_COUNT;
2562 
2563 	/* The more we get, the more we will assign to Tx/Rx Cleanup
2564 	 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2565 	 * Right now, we simply care about how many we'll get; we'll
2566 	 * set them up later while requesting irq's.
2567 	 */
2568 	vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
2569 					vector_threshold, vectors);
2570 
2571 	if (vectors < 0) {
2572 		dev_err(&adapter->pdev->dev,
2573 			"Unable to allocate MSI-X interrupts\n");
2574 		kfree(adapter->msix_entries);
2575 		adapter->msix_entries = NULL;
2576 		return vectors;
2577 	}
2578 
2579 	/* Adjust for only the vectors we'll use, which is minimum
2580 	 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
2581 	 * vectors we were allocated.
2582 	 */
2583 	adapter->num_msix_vectors = vectors;
2584 
2585 	return 0;
2586 }
2587 
2588 /**
2589  * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
2590  * @adapter: board private structure to initialize
2591  *
2592  * This is the top level queue allocation routine.  The order here is very
2593  * important, starting with the "most" number of features turned on at once,
2594  * and ending with the smallest set of features.  This way large combinations
2595  * can be allocated if they're turned on, and smaller combinations are the
2596  * fall through conditions.
2597  *
2598  **/
2599 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
2600 {
2601 	struct ixgbe_hw *hw = &adapter->hw;
2602 	unsigned int def_q = 0;
2603 	unsigned int num_tcs = 0;
2604 	int err;
2605 
2606 	/* Start with base case */
2607 	adapter->num_rx_queues = 1;
2608 	adapter->num_tx_queues = 1;
2609 	adapter->num_xdp_queues = 0;
2610 
2611 	spin_lock_bh(&adapter->mbx_lock);
2612 
2613 	/* fetch queue configuration from the PF */
2614 	err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2615 
2616 	spin_unlock_bh(&adapter->mbx_lock);
2617 
2618 	if (err)
2619 		return;
2620 
2621 	/* we need as many queues as traffic classes */
2622 	if (num_tcs > 1) {
2623 		adapter->num_rx_queues = num_tcs;
2624 	} else {
2625 		u16 rss = min_t(u16, num_online_cpus(), IXGBEVF_MAX_RSS_QUEUES);
2626 
2627 		switch (hw->api_version) {
2628 		case ixgbe_mbox_api_11:
2629 		case ixgbe_mbox_api_12:
2630 		case ixgbe_mbox_api_13:
2631 		case ixgbe_mbox_api_14:
2632 			if (adapter->xdp_prog &&
2633 			    hw->mac.max_tx_queues == rss)
2634 				rss = rss > 3 ? 2 : 1;
2635 
2636 			adapter->num_rx_queues = rss;
2637 			adapter->num_tx_queues = rss;
2638 			adapter->num_xdp_queues = adapter->xdp_prog ? rss : 0;
2639 			break;
2640 		default:
2641 			break;
2642 		}
2643 	}
2644 }
2645 
2646 /**
2647  * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2648  * @adapter: board private structure to initialize
2649  *
2650  * Attempt to configure the interrupts using the best available
2651  * capabilities of the hardware and the kernel.
2652  **/
2653 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2654 {
2655 	int vector, v_budget;
2656 
2657 	/* It's easy to be greedy for MSI-X vectors, but it really
2658 	 * doesn't do us much good if we have a lot more vectors
2659 	 * than CPU's.  So let's be conservative and only ask for
2660 	 * (roughly) the same number of vectors as there are CPU's.
2661 	 * The default is to use pairs of vectors.
2662 	 */
2663 	v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
2664 	v_budget = min_t(int, v_budget, num_online_cpus());
2665 	v_budget += NON_Q_VECTORS;
2666 
2667 	adapter->msix_entries = kcalloc(v_budget,
2668 					sizeof(struct msix_entry), GFP_KERNEL);
2669 	if (!adapter->msix_entries)
2670 		return -ENOMEM;
2671 
2672 	for (vector = 0; vector < v_budget; vector++)
2673 		adapter->msix_entries[vector].entry = vector;
2674 
2675 	/* A failure in MSI-X entry allocation isn't fatal, but the VF driver
2676 	 * does not support any other modes, so we will simply fail here. Note
2677 	 * that we clean up the msix_entries pointer else-where.
2678 	 */
2679 	return ixgbevf_acquire_msix_vectors(adapter, v_budget);
2680 }
2681 
2682 static void ixgbevf_add_ring(struct ixgbevf_ring *ring,
2683 			     struct ixgbevf_ring_container *head)
2684 {
2685 	ring->next = head->ring;
2686 	head->ring = ring;
2687 	head->count++;
2688 }
2689 
2690 /**
2691  * ixgbevf_alloc_q_vector - Allocate memory for a single interrupt vector
2692  * @adapter: board private structure to initialize
2693  * @v_idx: index of vector in adapter struct
2694  * @txr_count: number of Tx rings for q vector
2695  * @txr_idx: index of first Tx ring to assign
2696  * @xdp_count: total number of XDP rings to allocate
2697  * @xdp_idx: index of first XDP ring to allocate
2698  * @rxr_count: number of Rx rings for q vector
2699  * @rxr_idx: index of first Rx ring to assign
2700  *
2701  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
2702  **/
2703 static int ixgbevf_alloc_q_vector(struct ixgbevf_adapter *adapter, int v_idx,
2704 				  int txr_count, int txr_idx,
2705 				  int xdp_count, int xdp_idx,
2706 				  int rxr_count, int rxr_idx)
2707 {
2708 	struct ixgbevf_q_vector *q_vector;
2709 	int reg_idx = txr_idx + xdp_idx;
2710 	struct ixgbevf_ring *ring;
2711 	int ring_count, size;
2712 
2713 	ring_count = txr_count + xdp_count + rxr_count;
2714 	size = sizeof(*q_vector) + (sizeof(*ring) * ring_count);
2715 
2716 	/* allocate q_vector and rings */
2717 	q_vector = kzalloc(size, GFP_KERNEL);
2718 	if (!q_vector)
2719 		return -ENOMEM;
2720 
2721 	/* initialize NAPI */
2722 	netif_napi_add(adapter->netdev, &q_vector->napi, ixgbevf_poll, 64);
2723 
2724 	/* tie q_vector and adapter together */
2725 	adapter->q_vector[v_idx] = q_vector;
2726 	q_vector->adapter = adapter;
2727 	q_vector->v_idx = v_idx;
2728 
2729 	/* initialize pointer to rings */
2730 	ring = q_vector->ring;
2731 
2732 	while (txr_count) {
2733 		/* assign generic ring traits */
2734 		ring->dev = &adapter->pdev->dev;
2735 		ring->netdev = adapter->netdev;
2736 
2737 		/* configure backlink on ring */
2738 		ring->q_vector = q_vector;
2739 
2740 		/* update q_vector Tx values */
2741 		ixgbevf_add_ring(ring, &q_vector->tx);
2742 
2743 		/* apply Tx specific ring traits */
2744 		ring->count = adapter->tx_ring_count;
2745 		ring->queue_index = txr_idx;
2746 		ring->reg_idx = reg_idx;
2747 
2748 		/* assign ring to adapter */
2749 		 adapter->tx_ring[txr_idx] = ring;
2750 
2751 		/* update count and index */
2752 		txr_count--;
2753 		txr_idx++;
2754 		reg_idx++;
2755 
2756 		/* push pointer to next ring */
2757 		ring++;
2758 	}
2759 
2760 	while (xdp_count) {
2761 		/* assign generic ring traits */
2762 		ring->dev = &adapter->pdev->dev;
2763 		ring->netdev = adapter->netdev;
2764 
2765 		/* configure backlink on ring */
2766 		ring->q_vector = q_vector;
2767 
2768 		/* update q_vector Tx values */
2769 		ixgbevf_add_ring(ring, &q_vector->tx);
2770 
2771 		/* apply Tx specific ring traits */
2772 		ring->count = adapter->tx_ring_count;
2773 		ring->queue_index = xdp_idx;
2774 		ring->reg_idx = reg_idx;
2775 		set_ring_xdp(ring);
2776 
2777 		/* assign ring to adapter */
2778 		adapter->xdp_ring[xdp_idx] = ring;
2779 
2780 		/* update count and index */
2781 		xdp_count--;
2782 		xdp_idx++;
2783 		reg_idx++;
2784 
2785 		/* push pointer to next ring */
2786 		ring++;
2787 	}
2788 
2789 	while (rxr_count) {
2790 		/* assign generic ring traits */
2791 		ring->dev = &adapter->pdev->dev;
2792 		ring->netdev = adapter->netdev;
2793 
2794 		/* configure backlink on ring */
2795 		ring->q_vector = q_vector;
2796 
2797 		/* update q_vector Rx values */
2798 		ixgbevf_add_ring(ring, &q_vector->rx);
2799 
2800 		/* apply Rx specific ring traits */
2801 		ring->count = adapter->rx_ring_count;
2802 		ring->queue_index = rxr_idx;
2803 		ring->reg_idx = rxr_idx;
2804 
2805 		/* assign ring to adapter */
2806 		adapter->rx_ring[rxr_idx] = ring;
2807 
2808 		/* update count and index */
2809 		rxr_count--;
2810 		rxr_idx++;
2811 
2812 		/* push pointer to next ring */
2813 		ring++;
2814 	}
2815 
2816 	return 0;
2817 }
2818 
2819 /**
2820  * ixgbevf_free_q_vector - Free memory allocated for specific interrupt vector
2821  * @adapter: board private structure to initialize
2822  * @v_idx: index of vector in adapter struct
2823  *
2824  * This function frees the memory allocated to the q_vector.  In addition if
2825  * NAPI is enabled it will delete any references to the NAPI struct prior
2826  * to freeing the q_vector.
2827  **/
2828 static void ixgbevf_free_q_vector(struct ixgbevf_adapter *adapter, int v_idx)
2829 {
2830 	struct ixgbevf_q_vector *q_vector = adapter->q_vector[v_idx];
2831 	struct ixgbevf_ring *ring;
2832 
2833 	ixgbevf_for_each_ring(ring, q_vector->tx) {
2834 		if (ring_is_xdp(ring))
2835 			adapter->xdp_ring[ring->queue_index] = NULL;
2836 		else
2837 			adapter->tx_ring[ring->queue_index] = NULL;
2838 	}
2839 
2840 	ixgbevf_for_each_ring(ring, q_vector->rx)
2841 		adapter->rx_ring[ring->queue_index] = NULL;
2842 
2843 	adapter->q_vector[v_idx] = NULL;
2844 	netif_napi_del(&q_vector->napi);
2845 
2846 	/* ixgbevf_get_stats() might access the rings on this vector,
2847 	 * we must wait a grace period before freeing it.
2848 	 */
2849 	kfree_rcu(q_vector, rcu);
2850 }
2851 
2852 /**
2853  * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2854  * @adapter: board private structure to initialize
2855  *
2856  * We allocate one q_vector per queue interrupt.  If allocation fails we
2857  * return -ENOMEM.
2858  **/
2859 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2860 {
2861 	int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2862 	int rxr_remaining = adapter->num_rx_queues;
2863 	int txr_remaining = adapter->num_tx_queues;
2864 	int xdp_remaining = adapter->num_xdp_queues;
2865 	int rxr_idx = 0, txr_idx = 0, xdp_idx = 0, v_idx = 0;
2866 	int err;
2867 
2868 	if (q_vectors >= (rxr_remaining + txr_remaining + xdp_remaining)) {
2869 		for (; rxr_remaining; v_idx++, q_vectors--) {
2870 			int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors);
2871 
2872 			err = ixgbevf_alloc_q_vector(adapter, v_idx,
2873 						     0, 0, 0, 0, rqpv, rxr_idx);
2874 			if (err)
2875 				goto err_out;
2876 
2877 			/* update counts and index */
2878 			rxr_remaining -= rqpv;
2879 			rxr_idx += rqpv;
2880 		}
2881 	}
2882 
2883 	for (; q_vectors; v_idx++, q_vectors--) {
2884 		int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors);
2885 		int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors);
2886 		int xqpv = DIV_ROUND_UP(xdp_remaining, q_vectors);
2887 
2888 		err = ixgbevf_alloc_q_vector(adapter, v_idx,
2889 					     tqpv, txr_idx,
2890 					     xqpv, xdp_idx,
2891 					     rqpv, rxr_idx);
2892 
2893 		if (err)
2894 			goto err_out;
2895 
2896 		/* update counts and index */
2897 		rxr_remaining -= rqpv;
2898 		rxr_idx += rqpv;
2899 		txr_remaining -= tqpv;
2900 		txr_idx += tqpv;
2901 		xdp_remaining -= xqpv;
2902 		xdp_idx += xqpv;
2903 	}
2904 
2905 	return 0;
2906 
2907 err_out:
2908 	while (v_idx) {
2909 		v_idx--;
2910 		ixgbevf_free_q_vector(adapter, v_idx);
2911 	}
2912 
2913 	return -ENOMEM;
2914 }
2915 
2916 /**
2917  * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2918  * @adapter: board private structure to initialize
2919  *
2920  * This function frees the memory allocated to the q_vectors.  In addition if
2921  * NAPI is enabled it will delete any references to the NAPI struct prior
2922  * to freeing the q_vector.
2923  **/
2924 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2925 {
2926 	int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2927 
2928 	while (q_vectors) {
2929 		q_vectors--;
2930 		ixgbevf_free_q_vector(adapter, q_vectors);
2931 	}
2932 }
2933 
2934 /**
2935  * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2936  * @adapter: board private structure
2937  *
2938  **/
2939 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2940 {
2941 	if (!adapter->msix_entries)
2942 		return;
2943 
2944 	pci_disable_msix(adapter->pdev);
2945 	kfree(adapter->msix_entries);
2946 	adapter->msix_entries = NULL;
2947 }
2948 
2949 /**
2950  * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2951  * @adapter: board private structure to initialize
2952  *
2953  **/
2954 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2955 {
2956 	int err;
2957 
2958 	/* Number of supported queues */
2959 	ixgbevf_set_num_queues(adapter);
2960 
2961 	err = ixgbevf_set_interrupt_capability(adapter);
2962 	if (err) {
2963 		hw_dbg(&adapter->hw,
2964 		       "Unable to setup interrupt capabilities\n");
2965 		goto err_set_interrupt;
2966 	}
2967 
2968 	err = ixgbevf_alloc_q_vectors(adapter);
2969 	if (err) {
2970 		hw_dbg(&adapter->hw, "Unable to allocate memory for queue vectors\n");
2971 		goto err_alloc_q_vectors;
2972 	}
2973 
2974 	hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, Tx Queue count = %u XDP Queue count %u\n",
2975 	       (adapter->num_rx_queues > 1) ? "Enabled" : "Disabled",
2976 	       adapter->num_rx_queues, adapter->num_tx_queues,
2977 	       adapter->num_xdp_queues);
2978 
2979 	set_bit(__IXGBEVF_DOWN, &adapter->state);
2980 
2981 	return 0;
2982 err_alloc_q_vectors:
2983 	ixgbevf_reset_interrupt_capability(adapter);
2984 err_set_interrupt:
2985 	return err;
2986 }
2987 
2988 /**
2989  * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2990  * @adapter: board private structure to clear interrupt scheme on
2991  *
2992  * We go through and clear interrupt specific resources and reset the structure
2993  * to pre-load conditions
2994  **/
2995 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2996 {
2997 	adapter->num_tx_queues = 0;
2998 	adapter->num_xdp_queues = 0;
2999 	adapter->num_rx_queues = 0;
3000 
3001 	ixgbevf_free_q_vectors(adapter);
3002 	ixgbevf_reset_interrupt_capability(adapter);
3003 }
3004 
3005 /**
3006  * ixgbevf_sw_init - Initialize general software structures
3007  * @adapter: board private structure to initialize
3008  *
3009  * ixgbevf_sw_init initializes the Adapter private data structure.
3010  * Fields are initialized based on PCI device information and
3011  * OS network device settings (MTU size).
3012  **/
3013 static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
3014 {
3015 	struct ixgbe_hw *hw = &adapter->hw;
3016 	struct pci_dev *pdev = adapter->pdev;
3017 	struct net_device *netdev = adapter->netdev;
3018 	int err;
3019 
3020 	/* PCI config space info */
3021 	hw->vendor_id = pdev->vendor;
3022 	hw->device_id = pdev->device;
3023 	hw->revision_id = pdev->revision;
3024 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
3025 	hw->subsystem_device_id = pdev->subsystem_device;
3026 
3027 	hw->mbx.ops.init_params(hw);
3028 
3029 	if (hw->mac.type >= ixgbe_mac_X550_vf) {
3030 		err = ixgbevf_init_rss_key(adapter);
3031 		if (err)
3032 			goto out;
3033 	}
3034 
3035 	/* assume legacy case in which PF would only give VF 2 queues */
3036 	hw->mac.max_tx_queues = 2;
3037 	hw->mac.max_rx_queues = 2;
3038 
3039 	/* lock to protect mailbox accesses */
3040 	spin_lock_init(&adapter->mbx_lock);
3041 
3042 	err = hw->mac.ops.reset_hw(hw);
3043 	if (err) {
3044 		dev_info(&pdev->dev,
3045 			 "PF still in reset state.  Is the PF interface up?\n");
3046 	} else {
3047 		err = hw->mac.ops.init_hw(hw);
3048 		if (err) {
3049 			pr_err("init_shared_code failed: %d\n", err);
3050 			goto out;
3051 		}
3052 		ixgbevf_negotiate_api(adapter);
3053 		err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
3054 		if (err)
3055 			dev_info(&pdev->dev, "Error reading MAC address\n");
3056 		else if (is_zero_ether_addr(adapter->hw.mac.addr))
3057 			dev_info(&pdev->dev,
3058 				 "MAC address not assigned by administrator.\n");
3059 		ether_addr_copy(netdev->dev_addr, hw->mac.addr);
3060 	}
3061 
3062 	if (!is_valid_ether_addr(netdev->dev_addr)) {
3063 		dev_info(&pdev->dev, "Assigning random MAC address\n");
3064 		eth_hw_addr_random(netdev);
3065 		ether_addr_copy(hw->mac.addr, netdev->dev_addr);
3066 		ether_addr_copy(hw->mac.perm_addr, netdev->dev_addr);
3067 	}
3068 
3069 	/* Enable dynamic interrupt throttling rates */
3070 	adapter->rx_itr_setting = 1;
3071 	adapter->tx_itr_setting = 1;
3072 
3073 	/* set default ring sizes */
3074 	adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
3075 	adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
3076 
3077 	set_bit(__IXGBEVF_DOWN, &adapter->state);
3078 	return 0;
3079 
3080 out:
3081 	return err;
3082 }
3083 
3084 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter)	\
3085 	{							\
3086 		u32 current_counter = IXGBE_READ_REG(hw, reg);	\
3087 		if (current_counter < last_counter)		\
3088 			counter += 0x100000000LL;		\
3089 		last_counter = current_counter;			\
3090 		counter &= 0xFFFFFFFF00000000LL;		\
3091 		counter |= current_counter;			\
3092 	}
3093 
3094 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
3095 	{								 \
3096 		u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb);	 \
3097 		u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb);	 \
3098 		u64 current_counter = (current_counter_msb << 32) |	 \
3099 			current_counter_lsb;				 \
3100 		if (current_counter < last_counter)			 \
3101 			counter += 0x1000000000LL;			 \
3102 		last_counter = current_counter;				 \
3103 		counter &= 0xFFFFFFF000000000LL;			 \
3104 		counter |= current_counter;				 \
3105 	}
3106 /**
3107  * ixgbevf_update_stats - Update the board statistics counters.
3108  * @adapter: board private structure
3109  **/
3110 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
3111 {
3112 	struct ixgbe_hw *hw = &adapter->hw;
3113 	u64 alloc_rx_page_failed = 0, alloc_rx_buff_failed = 0;
3114 	u64 alloc_rx_page = 0, hw_csum_rx_error = 0;
3115 	int i;
3116 
3117 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3118 	    test_bit(__IXGBEVF_RESETTING, &adapter->state))
3119 		return;
3120 
3121 	UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
3122 				adapter->stats.vfgprc);
3123 	UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
3124 				adapter->stats.vfgptc);
3125 	UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
3126 				adapter->stats.last_vfgorc,
3127 				adapter->stats.vfgorc);
3128 	UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
3129 				adapter->stats.last_vfgotc,
3130 				adapter->stats.vfgotc);
3131 	UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
3132 				adapter->stats.vfmprc);
3133 
3134 	for (i = 0;  i  < adapter->num_rx_queues;  i++) {
3135 		struct ixgbevf_ring *rx_ring = adapter->rx_ring[i];
3136 
3137 		hw_csum_rx_error += rx_ring->rx_stats.csum_err;
3138 		alloc_rx_page_failed += rx_ring->rx_stats.alloc_rx_page_failed;
3139 		alloc_rx_buff_failed += rx_ring->rx_stats.alloc_rx_buff_failed;
3140 		alloc_rx_page += rx_ring->rx_stats.alloc_rx_page;
3141 	}
3142 
3143 	adapter->hw_csum_rx_error = hw_csum_rx_error;
3144 	adapter->alloc_rx_page_failed = alloc_rx_page_failed;
3145 	adapter->alloc_rx_buff_failed = alloc_rx_buff_failed;
3146 	adapter->alloc_rx_page = alloc_rx_page;
3147 }
3148 
3149 /**
3150  * ixgbevf_service_timer - Timer Call-back
3151  * @t: pointer to timer_list struct
3152  **/
3153 static void ixgbevf_service_timer(struct timer_list *t)
3154 {
3155 	struct ixgbevf_adapter *adapter = from_timer(adapter, t,
3156 						     service_timer);
3157 
3158 	/* Reset the timer */
3159 	mod_timer(&adapter->service_timer, (HZ * 2) + jiffies);
3160 
3161 	ixgbevf_service_event_schedule(adapter);
3162 }
3163 
3164 static void ixgbevf_reset_subtask(struct ixgbevf_adapter *adapter)
3165 {
3166 	if (!test_and_clear_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state))
3167 		return;
3168 
3169 	rtnl_lock();
3170 	/* If we're already down or resetting, just bail */
3171 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3172 	    test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
3173 	    test_bit(__IXGBEVF_RESETTING, &adapter->state)) {
3174 		rtnl_unlock();
3175 		return;
3176 	}
3177 
3178 	adapter->tx_timeout_count++;
3179 
3180 	ixgbevf_reinit_locked(adapter);
3181 	rtnl_unlock();
3182 }
3183 
3184 /**
3185  * ixgbevf_check_hang_subtask - check for hung queues and dropped interrupts
3186  * @adapter: pointer to the device adapter structure
3187  *
3188  * This function serves two purposes.  First it strobes the interrupt lines
3189  * in order to make certain interrupts are occurring.  Secondly it sets the
3190  * bits needed to check for TX hangs.  As a result we should immediately
3191  * determine if a hang has occurred.
3192  **/
3193 static void ixgbevf_check_hang_subtask(struct ixgbevf_adapter *adapter)
3194 {
3195 	struct ixgbe_hw *hw = &adapter->hw;
3196 	u32 eics = 0;
3197 	int i;
3198 
3199 	/* If we're down or resetting, just bail */
3200 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3201 	    test_bit(__IXGBEVF_RESETTING, &adapter->state))
3202 		return;
3203 
3204 	/* Force detection of hung controller */
3205 	if (netif_carrier_ok(adapter->netdev)) {
3206 		for (i = 0; i < adapter->num_tx_queues; i++)
3207 			set_check_for_tx_hang(adapter->tx_ring[i]);
3208 		for (i = 0; i < adapter->num_xdp_queues; i++)
3209 			set_check_for_tx_hang(adapter->xdp_ring[i]);
3210 	}
3211 
3212 	/* get one bit for every active Tx/Rx interrupt vector */
3213 	for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
3214 		struct ixgbevf_q_vector *qv = adapter->q_vector[i];
3215 
3216 		if (qv->rx.ring || qv->tx.ring)
3217 			eics |= BIT(i);
3218 	}
3219 
3220 	/* Cause software interrupt to ensure rings are cleaned */
3221 	IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
3222 }
3223 
3224 /**
3225  * ixgbevf_watchdog_update_link - update the link status
3226  * @adapter: pointer to the device adapter structure
3227  **/
3228 static void ixgbevf_watchdog_update_link(struct ixgbevf_adapter *adapter)
3229 {
3230 	struct ixgbe_hw *hw = &adapter->hw;
3231 	u32 link_speed = adapter->link_speed;
3232 	bool link_up = adapter->link_up;
3233 	s32 err;
3234 
3235 	spin_lock_bh(&adapter->mbx_lock);
3236 
3237 	err = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
3238 
3239 	spin_unlock_bh(&adapter->mbx_lock);
3240 
3241 	/* if check for link returns error we will need to reset */
3242 	if (err && time_after(jiffies, adapter->last_reset + (10 * HZ))) {
3243 		set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
3244 		link_up = false;
3245 	}
3246 
3247 	adapter->link_up = link_up;
3248 	adapter->link_speed = link_speed;
3249 }
3250 
3251 /**
3252  * ixgbevf_watchdog_link_is_up - update netif_carrier status and
3253  *				 print link up message
3254  * @adapter: pointer to the device adapter structure
3255  **/
3256 static void ixgbevf_watchdog_link_is_up(struct ixgbevf_adapter *adapter)
3257 {
3258 	struct net_device *netdev = adapter->netdev;
3259 
3260 	/* only continue if link was previously down */
3261 	if (netif_carrier_ok(netdev))
3262 		return;
3263 
3264 	dev_info(&adapter->pdev->dev, "NIC Link is Up %s\n",
3265 		 (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
3266 		 "10 Gbps" :
3267 		 (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL) ?
3268 		 "1 Gbps" :
3269 		 (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL) ?
3270 		 "100 Mbps" :
3271 		 "unknown speed");
3272 
3273 	netif_carrier_on(netdev);
3274 }
3275 
3276 /**
3277  * ixgbevf_watchdog_link_is_down - update netif_carrier status and
3278  *				   print link down message
3279  * @adapter: pointer to the adapter structure
3280  **/
3281 static void ixgbevf_watchdog_link_is_down(struct ixgbevf_adapter *adapter)
3282 {
3283 	struct net_device *netdev = adapter->netdev;
3284 
3285 	adapter->link_speed = 0;
3286 
3287 	/* only continue if link was up previously */
3288 	if (!netif_carrier_ok(netdev))
3289 		return;
3290 
3291 	dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
3292 
3293 	netif_carrier_off(netdev);
3294 }
3295 
3296 /**
3297  * ixgbevf_watchdog_subtask - worker thread to bring link up
3298  * @adapter: board private structure
3299  **/
3300 static void ixgbevf_watchdog_subtask(struct ixgbevf_adapter *adapter)
3301 {
3302 	/* if interface is down do nothing */
3303 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3304 	    test_bit(__IXGBEVF_RESETTING, &adapter->state))
3305 		return;
3306 
3307 	ixgbevf_watchdog_update_link(adapter);
3308 
3309 	if (adapter->link_up)
3310 		ixgbevf_watchdog_link_is_up(adapter);
3311 	else
3312 		ixgbevf_watchdog_link_is_down(adapter);
3313 
3314 	ixgbevf_update_stats(adapter);
3315 }
3316 
3317 /**
3318  * ixgbevf_service_task - manages and runs subtasks
3319  * @work: pointer to work_struct containing our data
3320  **/
3321 static void ixgbevf_service_task(struct work_struct *work)
3322 {
3323 	struct ixgbevf_adapter *adapter = container_of(work,
3324 						       struct ixgbevf_adapter,
3325 						       service_task);
3326 	struct ixgbe_hw *hw = &adapter->hw;
3327 
3328 	if (IXGBE_REMOVED(hw->hw_addr)) {
3329 		if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
3330 			rtnl_lock();
3331 			ixgbevf_down(adapter);
3332 			rtnl_unlock();
3333 		}
3334 		return;
3335 	}
3336 
3337 	ixgbevf_queue_reset_subtask(adapter);
3338 	ixgbevf_reset_subtask(adapter);
3339 	ixgbevf_watchdog_subtask(adapter);
3340 	ixgbevf_check_hang_subtask(adapter);
3341 
3342 	ixgbevf_service_event_complete(adapter);
3343 }
3344 
3345 /**
3346  * ixgbevf_free_tx_resources - Free Tx Resources per Queue
3347  * @tx_ring: Tx descriptor ring for a specific queue
3348  *
3349  * Free all transmit software resources
3350  **/
3351 void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
3352 {
3353 	ixgbevf_clean_tx_ring(tx_ring);
3354 
3355 	vfree(tx_ring->tx_buffer_info);
3356 	tx_ring->tx_buffer_info = NULL;
3357 
3358 	/* if not set, then don't free */
3359 	if (!tx_ring->desc)
3360 		return;
3361 
3362 	dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
3363 			  tx_ring->dma);
3364 
3365 	tx_ring->desc = NULL;
3366 }
3367 
3368 /**
3369  * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
3370  * @adapter: board private structure
3371  *
3372  * Free all transmit software resources
3373  **/
3374 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
3375 {
3376 	int i;
3377 
3378 	for (i = 0; i < adapter->num_tx_queues; i++)
3379 		if (adapter->tx_ring[i]->desc)
3380 			ixgbevf_free_tx_resources(adapter->tx_ring[i]);
3381 	for (i = 0; i < adapter->num_xdp_queues; i++)
3382 		if (adapter->xdp_ring[i]->desc)
3383 			ixgbevf_free_tx_resources(adapter->xdp_ring[i]);
3384 }
3385 
3386 /**
3387  * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
3388  * @tx_ring: Tx descriptor ring (for a specific queue) to setup
3389  *
3390  * Return 0 on success, negative on failure
3391  **/
3392 int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
3393 {
3394 	struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
3395 	int size;
3396 
3397 	size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
3398 	tx_ring->tx_buffer_info = vmalloc(size);
3399 	if (!tx_ring->tx_buffer_info)
3400 		goto err;
3401 
3402 	u64_stats_init(&tx_ring->syncp);
3403 
3404 	/* round up to nearest 4K */
3405 	tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
3406 	tx_ring->size = ALIGN(tx_ring->size, 4096);
3407 
3408 	tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
3409 					   &tx_ring->dma, GFP_KERNEL);
3410 	if (!tx_ring->desc)
3411 		goto err;
3412 
3413 	return 0;
3414 
3415 err:
3416 	vfree(tx_ring->tx_buffer_info);
3417 	tx_ring->tx_buffer_info = NULL;
3418 	hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit descriptor ring\n");
3419 	return -ENOMEM;
3420 }
3421 
3422 /**
3423  * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
3424  * @adapter: board private structure
3425  *
3426  * If this function returns with an error, then it's possible one or
3427  * more of the rings is populated (while the rest are not).  It is the
3428  * callers duty to clean those orphaned rings.
3429  *
3430  * Return 0 on success, negative on failure
3431  **/
3432 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
3433 {
3434 	int i, j = 0, err = 0;
3435 
3436 	for (i = 0; i < adapter->num_tx_queues; i++) {
3437 		err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
3438 		if (!err)
3439 			continue;
3440 		hw_dbg(&adapter->hw, "Allocation for Tx Queue %u failed\n", i);
3441 		goto err_setup_tx;
3442 	}
3443 
3444 	for (j = 0; j < adapter->num_xdp_queues; j++) {
3445 		err = ixgbevf_setup_tx_resources(adapter->xdp_ring[j]);
3446 		if (!err)
3447 			continue;
3448 		hw_dbg(&adapter->hw, "Allocation for XDP Queue %u failed\n", j);
3449 		goto err_setup_tx;
3450 	}
3451 
3452 	return 0;
3453 err_setup_tx:
3454 	/* rewind the index freeing the rings as we go */
3455 	while (j--)
3456 		ixgbevf_free_tx_resources(adapter->xdp_ring[j]);
3457 	while (i--)
3458 		ixgbevf_free_tx_resources(adapter->tx_ring[i]);
3459 
3460 	return err;
3461 }
3462 
3463 /**
3464  * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
3465  * @adapter: board private structure
3466  * @rx_ring: Rx descriptor ring (for a specific queue) to setup
3467  *
3468  * Returns 0 on success, negative on failure
3469  **/
3470 int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
3471 			       struct ixgbevf_ring *rx_ring)
3472 {
3473 	int size;
3474 
3475 	size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
3476 	rx_ring->rx_buffer_info = vmalloc(size);
3477 	if (!rx_ring->rx_buffer_info)
3478 		goto err;
3479 
3480 	u64_stats_init(&rx_ring->syncp);
3481 
3482 	/* Round up to nearest 4K */
3483 	rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
3484 	rx_ring->size = ALIGN(rx_ring->size, 4096);
3485 
3486 	rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
3487 					   &rx_ring->dma, GFP_KERNEL);
3488 
3489 	if (!rx_ring->desc)
3490 		goto err;
3491 
3492 	/* XDP RX-queue info */
3493 	if (xdp_rxq_info_reg(&rx_ring->xdp_rxq, adapter->netdev,
3494 			     rx_ring->queue_index, 0) < 0)
3495 		goto err;
3496 
3497 	rx_ring->xdp_prog = adapter->xdp_prog;
3498 
3499 	return 0;
3500 err:
3501 	vfree(rx_ring->rx_buffer_info);
3502 	rx_ring->rx_buffer_info = NULL;
3503 	dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
3504 	return -ENOMEM;
3505 }
3506 
3507 /**
3508  * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
3509  * @adapter: board private structure
3510  *
3511  * If this function returns with an error, then it's possible one or
3512  * more of the rings is populated (while the rest are not).  It is the
3513  * callers duty to clean those orphaned rings.
3514  *
3515  * Return 0 on success, negative on failure
3516  **/
3517 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
3518 {
3519 	int i, err = 0;
3520 
3521 	for (i = 0; i < adapter->num_rx_queues; i++) {
3522 		err = ixgbevf_setup_rx_resources(adapter, adapter->rx_ring[i]);
3523 		if (!err)
3524 			continue;
3525 		hw_dbg(&adapter->hw, "Allocation for Rx Queue %u failed\n", i);
3526 		goto err_setup_rx;
3527 	}
3528 
3529 	return 0;
3530 err_setup_rx:
3531 	/* rewind the index freeing the rings as we go */
3532 	while (i--)
3533 		ixgbevf_free_rx_resources(adapter->rx_ring[i]);
3534 	return err;
3535 }
3536 
3537 /**
3538  * ixgbevf_free_rx_resources - Free Rx Resources
3539  * @rx_ring: ring to clean the resources from
3540  *
3541  * Free all receive software resources
3542  **/
3543 void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
3544 {
3545 	ixgbevf_clean_rx_ring(rx_ring);
3546 
3547 	rx_ring->xdp_prog = NULL;
3548 	xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
3549 	vfree(rx_ring->rx_buffer_info);
3550 	rx_ring->rx_buffer_info = NULL;
3551 
3552 	dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
3553 			  rx_ring->dma);
3554 
3555 	rx_ring->desc = NULL;
3556 }
3557 
3558 /**
3559  * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
3560  * @adapter: board private structure
3561  *
3562  * Free all receive software resources
3563  **/
3564 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
3565 {
3566 	int i;
3567 
3568 	for (i = 0; i < adapter->num_rx_queues; i++)
3569 		if (adapter->rx_ring[i]->desc)
3570 			ixgbevf_free_rx_resources(adapter->rx_ring[i]);
3571 }
3572 
3573 /**
3574  * ixgbevf_open - Called when a network interface is made active
3575  * @netdev: network interface device structure
3576  *
3577  * Returns 0 on success, negative value on failure
3578  *
3579  * The open entry point is called when a network interface is made
3580  * active by the system (IFF_UP).  At this point all resources needed
3581  * for transmit and receive operations are allocated, the interrupt
3582  * handler is registered with the OS, the watchdog timer is started,
3583  * and the stack is notified that the interface is ready.
3584  **/
3585 int ixgbevf_open(struct net_device *netdev)
3586 {
3587 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3588 	struct ixgbe_hw *hw = &adapter->hw;
3589 	int err;
3590 
3591 	/* A previous failure to open the device because of a lack of
3592 	 * available MSIX vector resources may have reset the number
3593 	 * of msix vectors variable to zero.  The only way to recover
3594 	 * is to unload/reload the driver and hope that the system has
3595 	 * been able to recover some MSIX vector resources.
3596 	 */
3597 	if (!adapter->num_msix_vectors)
3598 		return -ENOMEM;
3599 
3600 	if (hw->adapter_stopped) {
3601 		ixgbevf_reset(adapter);
3602 		/* if adapter is still stopped then PF isn't up and
3603 		 * the VF can't start.
3604 		 */
3605 		if (hw->adapter_stopped) {
3606 			err = IXGBE_ERR_MBX;
3607 			pr_err("Unable to start - perhaps the PF Driver isn't up yet\n");
3608 			goto err_setup_reset;
3609 		}
3610 	}
3611 
3612 	/* disallow open during test */
3613 	if (test_bit(__IXGBEVF_TESTING, &adapter->state))
3614 		return -EBUSY;
3615 
3616 	netif_carrier_off(netdev);
3617 
3618 	/* allocate transmit descriptors */
3619 	err = ixgbevf_setup_all_tx_resources(adapter);
3620 	if (err)
3621 		goto err_setup_tx;
3622 
3623 	/* allocate receive descriptors */
3624 	err = ixgbevf_setup_all_rx_resources(adapter);
3625 	if (err)
3626 		goto err_setup_rx;
3627 
3628 	ixgbevf_configure(adapter);
3629 
3630 	err = ixgbevf_request_irq(adapter);
3631 	if (err)
3632 		goto err_req_irq;
3633 
3634 	/* Notify the stack of the actual queue counts. */
3635 	err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
3636 	if (err)
3637 		goto err_set_queues;
3638 
3639 	err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
3640 	if (err)
3641 		goto err_set_queues;
3642 
3643 	ixgbevf_up_complete(adapter);
3644 
3645 	return 0;
3646 
3647 err_set_queues:
3648 	ixgbevf_free_irq(adapter);
3649 err_req_irq:
3650 	ixgbevf_free_all_rx_resources(adapter);
3651 err_setup_rx:
3652 	ixgbevf_free_all_tx_resources(adapter);
3653 err_setup_tx:
3654 	ixgbevf_reset(adapter);
3655 err_setup_reset:
3656 
3657 	return err;
3658 }
3659 
3660 /**
3661  * ixgbevf_close_suspend - actions necessary to both suspend and close flows
3662  * @adapter: the private adapter struct
3663  *
3664  * This function should contain the necessary work common to both suspending
3665  * and closing of the device.
3666  */
3667 static void ixgbevf_close_suspend(struct ixgbevf_adapter *adapter)
3668 {
3669 	ixgbevf_down(adapter);
3670 	ixgbevf_free_irq(adapter);
3671 	ixgbevf_free_all_tx_resources(adapter);
3672 	ixgbevf_free_all_rx_resources(adapter);
3673 }
3674 
3675 /**
3676  * ixgbevf_close - Disables a network interface
3677  * @netdev: network interface device structure
3678  *
3679  * Returns 0, this is not allowed to fail
3680  *
3681  * The close entry point is called when an interface is de-activated
3682  * by the OS.  The hardware is still under the drivers control, but
3683  * needs to be disabled.  A global MAC reset is issued to stop the
3684  * hardware, and all transmit and receive resources are freed.
3685  **/
3686 int ixgbevf_close(struct net_device *netdev)
3687 {
3688 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3689 
3690 	if (netif_device_present(netdev))
3691 		ixgbevf_close_suspend(adapter);
3692 
3693 	return 0;
3694 }
3695 
3696 static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
3697 {
3698 	struct net_device *dev = adapter->netdev;
3699 
3700 	if (!test_and_clear_bit(__IXGBEVF_QUEUE_RESET_REQUESTED,
3701 				&adapter->state))
3702 		return;
3703 
3704 	/* if interface is down do nothing */
3705 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3706 	    test_bit(__IXGBEVF_RESETTING, &adapter->state))
3707 		return;
3708 
3709 	/* Hardware has to reinitialize queues and interrupts to
3710 	 * match packet buffer alignment. Unfortunately, the
3711 	 * hardware is not flexible enough to do this dynamically.
3712 	 */
3713 	rtnl_lock();
3714 
3715 	if (netif_running(dev))
3716 		ixgbevf_close(dev);
3717 
3718 	ixgbevf_clear_interrupt_scheme(adapter);
3719 	ixgbevf_init_interrupt_scheme(adapter);
3720 
3721 	if (netif_running(dev))
3722 		ixgbevf_open(dev);
3723 
3724 	rtnl_unlock();
3725 }
3726 
3727 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
3728 				u32 vlan_macip_lens, u32 fceof_saidx,
3729 				u32 type_tucmd, u32 mss_l4len_idx)
3730 {
3731 	struct ixgbe_adv_tx_context_desc *context_desc;
3732 	u16 i = tx_ring->next_to_use;
3733 
3734 	context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
3735 
3736 	i++;
3737 	tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3738 
3739 	/* set bits to identify this as an advanced context descriptor */
3740 	type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
3741 
3742 	context_desc->vlan_macip_lens	= cpu_to_le32(vlan_macip_lens);
3743 	context_desc->fceof_saidx	= cpu_to_le32(fceof_saidx);
3744 	context_desc->type_tucmd_mlhl	= cpu_to_le32(type_tucmd);
3745 	context_desc->mss_l4len_idx	= cpu_to_le32(mss_l4len_idx);
3746 }
3747 
3748 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
3749 		       struct ixgbevf_tx_buffer *first,
3750 		       u8 *hdr_len,
3751 		       struct ixgbevf_ipsec_tx_data *itd)
3752 {
3753 	u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
3754 	struct sk_buff *skb = first->skb;
3755 	union {
3756 		struct iphdr *v4;
3757 		struct ipv6hdr *v6;
3758 		unsigned char *hdr;
3759 	} ip;
3760 	union {
3761 		struct tcphdr *tcp;
3762 		unsigned char *hdr;
3763 	} l4;
3764 	u32 paylen, l4_offset;
3765 	u32 fceof_saidx = 0;
3766 	int err;
3767 
3768 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3769 		return 0;
3770 
3771 	if (!skb_is_gso(skb))
3772 		return 0;
3773 
3774 	err = skb_cow_head(skb, 0);
3775 	if (err < 0)
3776 		return err;
3777 
3778 	if (eth_p_mpls(first->protocol))
3779 		ip.hdr = skb_inner_network_header(skb);
3780 	else
3781 		ip.hdr = skb_network_header(skb);
3782 	l4.hdr = skb_checksum_start(skb);
3783 
3784 	/* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3785 	type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3786 
3787 	/* initialize outer IP header fields */
3788 	if (ip.v4->version == 4) {
3789 		unsigned char *csum_start = skb_checksum_start(skb);
3790 		unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
3791 		int len = csum_start - trans_start;
3792 
3793 		/* IP header will have to cancel out any data that
3794 		 * is not a part of the outer IP header, so set to
3795 		 * a reverse csum if needed, else init check to 0.
3796 		 */
3797 		ip.v4->check = (skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) ?
3798 					   csum_fold(csum_partial(trans_start,
3799 								  len, 0)) : 0;
3800 		type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3801 
3802 		ip.v4->tot_len = 0;
3803 		first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3804 				   IXGBE_TX_FLAGS_CSUM |
3805 				   IXGBE_TX_FLAGS_IPV4;
3806 	} else {
3807 		ip.v6->payload_len = 0;
3808 		first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3809 				   IXGBE_TX_FLAGS_CSUM;
3810 	}
3811 
3812 	/* determine offset of inner transport header */
3813 	l4_offset = l4.hdr - skb->data;
3814 
3815 	/* compute length of segmentation header */
3816 	*hdr_len = (l4.tcp->doff * 4) + l4_offset;
3817 
3818 	/* remove payload length from inner checksum */
3819 	paylen = skb->len - l4_offset;
3820 	csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
3821 
3822 	/* update gso size and bytecount with header size */
3823 	first->gso_segs = skb_shinfo(skb)->gso_segs;
3824 	first->bytecount += (first->gso_segs - 1) * *hdr_len;
3825 
3826 	/* mss_l4len_id: use 1 as index for TSO */
3827 	mss_l4len_idx = (*hdr_len - l4_offset) << IXGBE_ADVTXD_L4LEN_SHIFT;
3828 	mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
3829 	mss_l4len_idx |= (1u << IXGBE_ADVTXD_IDX_SHIFT);
3830 
3831 	fceof_saidx |= itd->pfsa;
3832 	type_tucmd |= itd->flags | itd->trailer_len;
3833 
3834 	/* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
3835 	vlan_macip_lens = l4.hdr - ip.hdr;
3836 	vlan_macip_lens |= (ip.hdr - skb->data) << IXGBE_ADVTXD_MACLEN_SHIFT;
3837 	vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3838 
3839 	ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens, fceof_saidx, type_tucmd,
3840 			    mss_l4len_idx);
3841 
3842 	return 1;
3843 }
3844 
3845 static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
3846 			    struct ixgbevf_tx_buffer *first,
3847 			    struct ixgbevf_ipsec_tx_data *itd)
3848 {
3849 	struct sk_buff *skb = first->skb;
3850 	u32 vlan_macip_lens = 0;
3851 	u32 fceof_saidx = 0;
3852 	u32 type_tucmd = 0;
3853 
3854 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3855 		goto no_csum;
3856 
3857 	switch (skb->csum_offset) {
3858 	case offsetof(struct tcphdr, check):
3859 		type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3860 		fallthrough;
3861 	case offsetof(struct udphdr, check):
3862 		break;
3863 	case offsetof(struct sctphdr, checksum):
3864 		/* validate that this is actually an SCTP request */
3865 		if (skb_csum_is_sctp(skb)) {
3866 			type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_SCTP;
3867 			break;
3868 		}
3869 		fallthrough;
3870 	default:
3871 		skb_checksum_help(skb);
3872 		goto no_csum;
3873 	}
3874 
3875 	if (first->protocol == htons(ETH_P_IP))
3876 		type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3877 
3878 	/* update TX checksum flag */
3879 	first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
3880 	vlan_macip_lens = skb_checksum_start_offset(skb) -
3881 			  skb_network_offset(skb);
3882 no_csum:
3883 	/* vlan_macip_lens: MACLEN, VLAN tag */
3884 	vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
3885 	vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3886 
3887 	fceof_saidx |= itd->pfsa;
3888 	type_tucmd |= itd->flags | itd->trailer_len;
3889 
3890 	ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3891 			    fceof_saidx, type_tucmd, 0);
3892 }
3893 
3894 static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
3895 {
3896 	/* set type for advanced descriptor with frame checksum insertion */
3897 	__le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
3898 				      IXGBE_ADVTXD_DCMD_IFCS |
3899 				      IXGBE_ADVTXD_DCMD_DEXT);
3900 
3901 	/* set HW VLAN bit if VLAN is present */
3902 	if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3903 		cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
3904 
3905 	/* set segmentation enable bits for TSO/FSO */
3906 	if (tx_flags & IXGBE_TX_FLAGS_TSO)
3907 		cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
3908 
3909 	return cmd_type;
3910 }
3911 
3912 static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
3913 				     u32 tx_flags, unsigned int paylen)
3914 {
3915 	__le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
3916 
3917 	/* enable L4 checksum for TSO and TX checksum offload */
3918 	if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3919 		olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
3920 
3921 	/* enble IPv4 checksum for TSO */
3922 	if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3923 		olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
3924 
3925 	/* enable IPsec */
3926 	if (tx_flags & IXGBE_TX_FLAGS_IPSEC)
3927 		olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IPSEC);
3928 
3929 	/* use index 1 context for TSO/FSO/FCOE/IPSEC */
3930 	if (tx_flags & (IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_IPSEC))
3931 		olinfo_status |= cpu_to_le32(1u << IXGBE_ADVTXD_IDX_SHIFT);
3932 
3933 	/* Check Context must be set if Tx switch is enabled, which it
3934 	 * always is for case where virtual functions are running
3935 	 */
3936 	olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
3937 
3938 	tx_desc->read.olinfo_status = olinfo_status;
3939 }
3940 
3941 static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
3942 			   struct ixgbevf_tx_buffer *first,
3943 			   const u8 hdr_len)
3944 {
3945 	struct sk_buff *skb = first->skb;
3946 	struct ixgbevf_tx_buffer *tx_buffer;
3947 	union ixgbe_adv_tx_desc *tx_desc;
3948 	skb_frag_t *frag;
3949 	dma_addr_t dma;
3950 	unsigned int data_len, size;
3951 	u32 tx_flags = first->tx_flags;
3952 	__le32 cmd_type = ixgbevf_tx_cmd_type(tx_flags);
3953 	u16 i = tx_ring->next_to_use;
3954 
3955 	tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
3956 
3957 	ixgbevf_tx_olinfo_status(tx_desc, tx_flags, skb->len - hdr_len);
3958 
3959 	size = skb_headlen(skb);
3960 	data_len = skb->data_len;
3961 
3962 	dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
3963 
3964 	tx_buffer = first;
3965 
3966 	for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
3967 		if (dma_mapping_error(tx_ring->dev, dma))
3968 			goto dma_error;
3969 
3970 		/* record length, and DMA address */
3971 		dma_unmap_len_set(tx_buffer, len, size);
3972 		dma_unmap_addr_set(tx_buffer, dma, dma);
3973 
3974 		tx_desc->read.buffer_addr = cpu_to_le64(dma);
3975 
3976 		while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
3977 			tx_desc->read.cmd_type_len =
3978 				cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
3979 
3980 			i++;
3981 			tx_desc++;
3982 			if (i == tx_ring->count) {
3983 				tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3984 				i = 0;
3985 			}
3986 			tx_desc->read.olinfo_status = 0;
3987 
3988 			dma += IXGBE_MAX_DATA_PER_TXD;
3989 			size -= IXGBE_MAX_DATA_PER_TXD;
3990 
3991 			tx_desc->read.buffer_addr = cpu_to_le64(dma);
3992 		}
3993 
3994 		if (likely(!data_len))
3995 			break;
3996 
3997 		tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
3998 
3999 		i++;
4000 		tx_desc++;
4001 		if (i == tx_ring->count) {
4002 			tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
4003 			i = 0;
4004 		}
4005 		tx_desc->read.olinfo_status = 0;
4006 
4007 		size = skb_frag_size(frag);
4008 		data_len -= size;
4009 
4010 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
4011 				       DMA_TO_DEVICE);
4012 
4013 		tx_buffer = &tx_ring->tx_buffer_info[i];
4014 	}
4015 
4016 	/* write last descriptor with RS and EOP bits */
4017 	cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
4018 	tx_desc->read.cmd_type_len = cmd_type;
4019 
4020 	/* set the timestamp */
4021 	first->time_stamp = jiffies;
4022 
4023 	skb_tx_timestamp(skb);
4024 
4025 	/* Force memory writes to complete before letting h/w know there
4026 	 * are new descriptors to fetch.  (Only applicable for weak-ordered
4027 	 * memory model archs, such as IA-64).
4028 	 *
4029 	 * We also need this memory barrier (wmb) to make certain all of the
4030 	 * status bits have been updated before next_to_watch is written.
4031 	 */
4032 	wmb();
4033 
4034 	/* set next_to_watch value indicating a packet is present */
4035 	first->next_to_watch = tx_desc;
4036 
4037 	i++;
4038 	if (i == tx_ring->count)
4039 		i = 0;
4040 
4041 	tx_ring->next_to_use = i;
4042 
4043 	/* notify HW of packet */
4044 	ixgbevf_write_tail(tx_ring, i);
4045 
4046 	return;
4047 dma_error:
4048 	dev_err(tx_ring->dev, "TX DMA map failed\n");
4049 	tx_buffer = &tx_ring->tx_buffer_info[i];
4050 
4051 	/* clear dma mappings for failed tx_buffer_info map */
4052 	while (tx_buffer != first) {
4053 		if (dma_unmap_len(tx_buffer, len))
4054 			dma_unmap_page(tx_ring->dev,
4055 				       dma_unmap_addr(tx_buffer, dma),
4056 				       dma_unmap_len(tx_buffer, len),
4057 				       DMA_TO_DEVICE);
4058 		dma_unmap_len_set(tx_buffer, len, 0);
4059 
4060 		if (i-- == 0)
4061 			i += tx_ring->count;
4062 		tx_buffer = &tx_ring->tx_buffer_info[i];
4063 	}
4064 
4065 	if (dma_unmap_len(tx_buffer, len))
4066 		dma_unmap_single(tx_ring->dev,
4067 				 dma_unmap_addr(tx_buffer, dma),
4068 				 dma_unmap_len(tx_buffer, len),
4069 				 DMA_TO_DEVICE);
4070 	dma_unmap_len_set(tx_buffer, len, 0);
4071 
4072 	dev_kfree_skb_any(tx_buffer->skb);
4073 	tx_buffer->skb = NULL;
4074 
4075 	tx_ring->next_to_use = i;
4076 }
4077 
4078 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
4079 {
4080 	netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
4081 	/* Herbert's original patch had:
4082 	 *  smp_mb__after_netif_stop_queue();
4083 	 * but since that doesn't exist yet, just open code it.
4084 	 */
4085 	smp_mb();
4086 
4087 	/* We need to check again in a case another CPU has just
4088 	 * made room available.
4089 	 */
4090 	if (likely(ixgbevf_desc_unused(tx_ring) < size))
4091 		return -EBUSY;
4092 
4093 	/* A reprieve! - use start_queue because it doesn't call schedule */
4094 	netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
4095 	++tx_ring->tx_stats.restart_queue;
4096 
4097 	return 0;
4098 }
4099 
4100 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
4101 {
4102 	if (likely(ixgbevf_desc_unused(tx_ring) >= size))
4103 		return 0;
4104 	return __ixgbevf_maybe_stop_tx(tx_ring, size);
4105 }
4106 
4107 static int ixgbevf_xmit_frame_ring(struct sk_buff *skb,
4108 				   struct ixgbevf_ring *tx_ring)
4109 {
4110 	struct ixgbevf_tx_buffer *first;
4111 	int tso;
4112 	u32 tx_flags = 0;
4113 	u16 count = TXD_USE_COUNT(skb_headlen(skb));
4114 	struct ixgbevf_ipsec_tx_data ipsec_tx = { 0 };
4115 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
4116 	unsigned short f;
4117 #endif
4118 	u8 hdr_len = 0;
4119 	u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
4120 
4121 	if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
4122 		dev_kfree_skb_any(skb);
4123 		return NETDEV_TX_OK;
4124 	}
4125 
4126 	/* need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
4127 	 *       + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
4128 	 *       + 2 desc gap to keep tail from touching head,
4129 	 *       + 1 desc for context descriptor,
4130 	 * otherwise try next time
4131 	 */
4132 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
4133 	for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
4134 		skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
4135 
4136 		count += TXD_USE_COUNT(skb_frag_size(frag));
4137 	}
4138 #else
4139 	count += skb_shinfo(skb)->nr_frags;
4140 #endif
4141 	if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
4142 		tx_ring->tx_stats.tx_busy++;
4143 		return NETDEV_TX_BUSY;
4144 	}
4145 
4146 	/* record the location of the first descriptor for this packet */
4147 	first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4148 	first->skb = skb;
4149 	first->bytecount = skb->len;
4150 	first->gso_segs = 1;
4151 
4152 	if (skb_vlan_tag_present(skb)) {
4153 		tx_flags |= skb_vlan_tag_get(skb);
4154 		tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
4155 		tx_flags |= IXGBE_TX_FLAGS_VLAN;
4156 	}
4157 
4158 	/* record initial flags and protocol */
4159 	first->tx_flags = tx_flags;
4160 	first->protocol = vlan_get_protocol(skb);
4161 
4162 #ifdef CONFIG_IXGBEVF_IPSEC
4163 	if (xfrm_offload(skb) && !ixgbevf_ipsec_tx(tx_ring, first, &ipsec_tx))
4164 		goto out_drop;
4165 #endif
4166 	tso = ixgbevf_tso(tx_ring, first, &hdr_len, &ipsec_tx);
4167 	if (tso < 0)
4168 		goto out_drop;
4169 	else if (!tso)
4170 		ixgbevf_tx_csum(tx_ring, first, &ipsec_tx);
4171 
4172 	ixgbevf_tx_map(tx_ring, first, hdr_len);
4173 
4174 	ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
4175 
4176 	return NETDEV_TX_OK;
4177 
4178 out_drop:
4179 	dev_kfree_skb_any(first->skb);
4180 	first->skb = NULL;
4181 
4182 	return NETDEV_TX_OK;
4183 }
4184 
4185 static netdev_tx_t ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
4186 {
4187 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4188 	struct ixgbevf_ring *tx_ring;
4189 
4190 	if (skb->len <= 0) {
4191 		dev_kfree_skb_any(skb);
4192 		return NETDEV_TX_OK;
4193 	}
4194 
4195 	/* The minimum packet size for olinfo paylen is 17 so pad the skb
4196 	 * in order to meet this minimum size requirement.
4197 	 */
4198 	if (skb->len < 17) {
4199 		if (skb_padto(skb, 17))
4200 			return NETDEV_TX_OK;
4201 		skb->len = 17;
4202 	}
4203 
4204 	tx_ring = adapter->tx_ring[skb->queue_mapping];
4205 	return ixgbevf_xmit_frame_ring(skb, tx_ring);
4206 }
4207 
4208 /**
4209  * ixgbevf_set_mac - Change the Ethernet Address of the NIC
4210  * @netdev: network interface device structure
4211  * @p: pointer to an address structure
4212  *
4213  * Returns 0 on success, negative on failure
4214  **/
4215 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
4216 {
4217 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4218 	struct ixgbe_hw *hw = &adapter->hw;
4219 	struct sockaddr *addr = p;
4220 	int err;
4221 
4222 	if (!is_valid_ether_addr(addr->sa_data))
4223 		return -EADDRNOTAVAIL;
4224 
4225 	spin_lock_bh(&adapter->mbx_lock);
4226 
4227 	err = hw->mac.ops.set_rar(hw, 0, addr->sa_data, 0);
4228 
4229 	spin_unlock_bh(&adapter->mbx_lock);
4230 
4231 	if (err)
4232 		return -EPERM;
4233 
4234 	ether_addr_copy(hw->mac.addr, addr->sa_data);
4235 	ether_addr_copy(hw->mac.perm_addr, addr->sa_data);
4236 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
4237 
4238 	return 0;
4239 }
4240 
4241 /**
4242  * ixgbevf_change_mtu - Change the Maximum Transfer Unit
4243  * @netdev: network interface device structure
4244  * @new_mtu: new value for maximum frame size
4245  *
4246  * Returns 0 on success, negative on failure
4247  **/
4248 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
4249 {
4250 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4251 	struct ixgbe_hw *hw = &adapter->hw;
4252 	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
4253 	int ret;
4254 
4255 	/* prevent MTU being changed to a size unsupported by XDP */
4256 	if (adapter->xdp_prog) {
4257 		dev_warn(&adapter->pdev->dev, "MTU cannot be changed while XDP program is loaded\n");
4258 		return -EPERM;
4259 	}
4260 
4261 	spin_lock_bh(&adapter->mbx_lock);
4262 	/* notify the PF of our intent to use this size of frame */
4263 	ret = hw->mac.ops.set_rlpml(hw, max_frame);
4264 	spin_unlock_bh(&adapter->mbx_lock);
4265 	if (ret)
4266 		return -EINVAL;
4267 
4268 	hw_dbg(hw, "changing MTU from %d to %d\n",
4269 	       netdev->mtu, new_mtu);
4270 
4271 	/* must set new MTU before calling down or up */
4272 	netdev->mtu = new_mtu;
4273 
4274 	if (netif_running(netdev))
4275 		ixgbevf_reinit_locked(adapter);
4276 
4277 	return 0;
4278 }
4279 
4280 static int __maybe_unused ixgbevf_suspend(struct device *dev_d)
4281 {
4282 	struct net_device *netdev = dev_get_drvdata(dev_d);
4283 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4284 
4285 	rtnl_lock();
4286 	netif_device_detach(netdev);
4287 
4288 	if (netif_running(netdev))
4289 		ixgbevf_close_suspend(adapter);
4290 
4291 	ixgbevf_clear_interrupt_scheme(adapter);
4292 	rtnl_unlock();
4293 
4294 	return 0;
4295 }
4296 
4297 static int __maybe_unused ixgbevf_resume(struct device *dev_d)
4298 {
4299 	struct pci_dev *pdev = to_pci_dev(dev_d);
4300 	struct net_device *netdev = pci_get_drvdata(pdev);
4301 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4302 	u32 err;
4303 
4304 	adapter->hw.hw_addr = adapter->io_addr;
4305 	smp_mb__before_atomic();
4306 	clear_bit(__IXGBEVF_DISABLED, &adapter->state);
4307 	pci_set_master(pdev);
4308 
4309 	ixgbevf_reset(adapter);
4310 
4311 	rtnl_lock();
4312 	err = ixgbevf_init_interrupt_scheme(adapter);
4313 	if (!err && netif_running(netdev))
4314 		err = ixgbevf_open(netdev);
4315 	rtnl_unlock();
4316 	if (err)
4317 		return err;
4318 
4319 	netif_device_attach(netdev);
4320 
4321 	return err;
4322 }
4323 
4324 static void ixgbevf_shutdown(struct pci_dev *pdev)
4325 {
4326 	ixgbevf_suspend(&pdev->dev);
4327 }
4328 
4329 static void ixgbevf_get_tx_ring_stats(struct rtnl_link_stats64 *stats,
4330 				      const struct ixgbevf_ring *ring)
4331 {
4332 	u64 bytes, packets;
4333 	unsigned int start;
4334 
4335 	if (ring) {
4336 		do {
4337 			start = u64_stats_fetch_begin_irq(&ring->syncp);
4338 			bytes = ring->stats.bytes;
4339 			packets = ring->stats.packets;
4340 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4341 		stats->tx_bytes += bytes;
4342 		stats->tx_packets += packets;
4343 	}
4344 }
4345 
4346 static void ixgbevf_get_stats(struct net_device *netdev,
4347 			      struct rtnl_link_stats64 *stats)
4348 {
4349 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4350 	unsigned int start;
4351 	u64 bytes, packets;
4352 	const struct ixgbevf_ring *ring;
4353 	int i;
4354 
4355 	ixgbevf_update_stats(adapter);
4356 
4357 	stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
4358 
4359 	rcu_read_lock();
4360 	for (i = 0; i < adapter->num_rx_queues; i++) {
4361 		ring = adapter->rx_ring[i];
4362 		do {
4363 			start = u64_stats_fetch_begin_irq(&ring->syncp);
4364 			bytes = ring->stats.bytes;
4365 			packets = ring->stats.packets;
4366 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4367 		stats->rx_bytes += bytes;
4368 		stats->rx_packets += packets;
4369 	}
4370 
4371 	for (i = 0; i < adapter->num_tx_queues; i++) {
4372 		ring = adapter->tx_ring[i];
4373 		ixgbevf_get_tx_ring_stats(stats, ring);
4374 	}
4375 
4376 	for (i = 0; i < adapter->num_xdp_queues; i++) {
4377 		ring = adapter->xdp_ring[i];
4378 		ixgbevf_get_tx_ring_stats(stats, ring);
4379 	}
4380 	rcu_read_unlock();
4381 }
4382 
4383 #define IXGBEVF_MAX_MAC_HDR_LEN		127
4384 #define IXGBEVF_MAX_NETWORK_HDR_LEN	511
4385 
4386 static netdev_features_t
4387 ixgbevf_features_check(struct sk_buff *skb, struct net_device *dev,
4388 		       netdev_features_t features)
4389 {
4390 	unsigned int network_hdr_len, mac_hdr_len;
4391 
4392 	/* Make certain the headers can be described by a context descriptor */
4393 	mac_hdr_len = skb_network_header(skb) - skb->data;
4394 	if (unlikely(mac_hdr_len > IXGBEVF_MAX_MAC_HDR_LEN))
4395 		return features & ~(NETIF_F_HW_CSUM |
4396 				    NETIF_F_SCTP_CRC |
4397 				    NETIF_F_HW_VLAN_CTAG_TX |
4398 				    NETIF_F_TSO |
4399 				    NETIF_F_TSO6);
4400 
4401 	network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
4402 	if (unlikely(network_hdr_len >  IXGBEVF_MAX_NETWORK_HDR_LEN))
4403 		return features & ~(NETIF_F_HW_CSUM |
4404 				    NETIF_F_SCTP_CRC |
4405 				    NETIF_F_TSO |
4406 				    NETIF_F_TSO6);
4407 
4408 	/* We can only support IPV4 TSO in tunnels if we can mangle the
4409 	 * inner IP ID field, so strip TSO if MANGLEID is not supported.
4410 	 */
4411 	if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
4412 		features &= ~NETIF_F_TSO;
4413 
4414 	return features;
4415 }
4416 
4417 static int ixgbevf_xdp_setup(struct net_device *dev, struct bpf_prog *prog)
4418 {
4419 	int i, frame_size = dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4420 	struct ixgbevf_adapter *adapter = netdev_priv(dev);
4421 	struct bpf_prog *old_prog;
4422 
4423 	/* verify ixgbevf ring attributes are sufficient for XDP */
4424 	for (i = 0; i < adapter->num_rx_queues; i++) {
4425 		struct ixgbevf_ring *ring = adapter->rx_ring[i];
4426 
4427 		if (frame_size > ixgbevf_rx_bufsz(ring))
4428 			return -EINVAL;
4429 	}
4430 
4431 	old_prog = xchg(&adapter->xdp_prog, prog);
4432 
4433 	/* If transitioning XDP modes reconfigure rings */
4434 	if (!!prog != !!old_prog) {
4435 		/* Hardware has to reinitialize queues and interrupts to
4436 		 * match packet buffer alignment. Unfortunately, the
4437 		 * hardware is not flexible enough to do this dynamically.
4438 		 */
4439 		if (netif_running(dev))
4440 			ixgbevf_close(dev);
4441 
4442 		ixgbevf_clear_interrupt_scheme(adapter);
4443 		ixgbevf_init_interrupt_scheme(adapter);
4444 
4445 		if (netif_running(dev))
4446 			ixgbevf_open(dev);
4447 	} else {
4448 		for (i = 0; i < adapter->num_rx_queues; i++)
4449 			xchg(&adapter->rx_ring[i]->xdp_prog, adapter->xdp_prog);
4450 	}
4451 
4452 	if (old_prog)
4453 		bpf_prog_put(old_prog);
4454 
4455 	return 0;
4456 }
4457 
4458 static int ixgbevf_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4459 {
4460 	switch (xdp->command) {
4461 	case XDP_SETUP_PROG:
4462 		return ixgbevf_xdp_setup(dev, xdp->prog);
4463 	default:
4464 		return -EINVAL;
4465 	}
4466 }
4467 
4468 static const struct net_device_ops ixgbevf_netdev_ops = {
4469 	.ndo_open		= ixgbevf_open,
4470 	.ndo_stop		= ixgbevf_close,
4471 	.ndo_start_xmit		= ixgbevf_xmit_frame,
4472 	.ndo_set_rx_mode	= ixgbevf_set_rx_mode,
4473 	.ndo_get_stats64	= ixgbevf_get_stats,
4474 	.ndo_validate_addr	= eth_validate_addr,
4475 	.ndo_set_mac_address	= ixgbevf_set_mac,
4476 	.ndo_change_mtu		= ixgbevf_change_mtu,
4477 	.ndo_tx_timeout		= ixgbevf_tx_timeout,
4478 	.ndo_vlan_rx_add_vid	= ixgbevf_vlan_rx_add_vid,
4479 	.ndo_vlan_rx_kill_vid	= ixgbevf_vlan_rx_kill_vid,
4480 	.ndo_features_check	= ixgbevf_features_check,
4481 	.ndo_bpf		= ixgbevf_xdp,
4482 };
4483 
4484 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
4485 {
4486 	dev->netdev_ops = &ixgbevf_netdev_ops;
4487 	ixgbevf_set_ethtool_ops(dev);
4488 	dev->watchdog_timeo = 5 * HZ;
4489 }
4490 
4491 /**
4492  * ixgbevf_probe - Device Initialization Routine
4493  * @pdev: PCI device information struct
4494  * @ent: entry in ixgbevf_pci_tbl
4495  *
4496  * Returns 0 on success, negative on failure
4497  *
4498  * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
4499  * The OS initialization, configuring of the adapter private structure,
4500  * and a hardware reset occur.
4501  **/
4502 static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4503 {
4504 	struct net_device *netdev;
4505 	struct ixgbevf_adapter *adapter = NULL;
4506 	struct ixgbe_hw *hw = NULL;
4507 	const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
4508 	int err, pci_using_dac;
4509 	bool disable_dev = false;
4510 
4511 	err = pci_enable_device(pdev);
4512 	if (err)
4513 		return err;
4514 
4515 	if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
4516 		pci_using_dac = 1;
4517 	} else {
4518 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
4519 		if (err) {
4520 			dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
4521 			goto err_dma;
4522 		}
4523 		pci_using_dac = 0;
4524 	}
4525 
4526 	err = pci_request_regions(pdev, ixgbevf_driver_name);
4527 	if (err) {
4528 		dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
4529 		goto err_pci_reg;
4530 	}
4531 
4532 	pci_set_master(pdev);
4533 
4534 	netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
4535 				   MAX_TX_QUEUES);
4536 	if (!netdev) {
4537 		err = -ENOMEM;
4538 		goto err_alloc_etherdev;
4539 	}
4540 
4541 	SET_NETDEV_DEV(netdev, &pdev->dev);
4542 
4543 	adapter = netdev_priv(netdev);
4544 
4545 	adapter->netdev = netdev;
4546 	adapter->pdev = pdev;
4547 	hw = &adapter->hw;
4548 	hw->back = adapter;
4549 	adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4550 
4551 	/* call save state here in standalone driver because it relies on
4552 	 * adapter struct to exist, and needs to call netdev_priv
4553 	 */
4554 	pci_save_state(pdev);
4555 
4556 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4557 			      pci_resource_len(pdev, 0));
4558 	adapter->io_addr = hw->hw_addr;
4559 	if (!hw->hw_addr) {
4560 		err = -EIO;
4561 		goto err_ioremap;
4562 	}
4563 
4564 	ixgbevf_assign_netdev_ops(netdev);
4565 
4566 	/* Setup HW API */
4567 	memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
4568 	hw->mac.type  = ii->mac;
4569 
4570 	memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
4571 	       sizeof(struct ixgbe_mbx_operations));
4572 
4573 	/* setup the private structure */
4574 	err = ixgbevf_sw_init(adapter);
4575 	if (err)
4576 		goto err_sw_init;
4577 
4578 	/* The HW MAC address was set and/or determined in sw_init */
4579 	if (!is_valid_ether_addr(netdev->dev_addr)) {
4580 		pr_err("invalid MAC address\n");
4581 		err = -EIO;
4582 		goto err_sw_init;
4583 	}
4584 
4585 	netdev->hw_features = NETIF_F_SG |
4586 			      NETIF_F_TSO |
4587 			      NETIF_F_TSO6 |
4588 			      NETIF_F_RXCSUM |
4589 			      NETIF_F_HW_CSUM |
4590 			      NETIF_F_SCTP_CRC;
4591 
4592 #define IXGBEVF_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
4593 				      NETIF_F_GSO_GRE_CSUM | \
4594 				      NETIF_F_GSO_IPXIP4 | \
4595 				      NETIF_F_GSO_IPXIP6 | \
4596 				      NETIF_F_GSO_UDP_TUNNEL | \
4597 				      NETIF_F_GSO_UDP_TUNNEL_CSUM)
4598 
4599 	netdev->gso_partial_features = IXGBEVF_GSO_PARTIAL_FEATURES;
4600 	netdev->hw_features |= NETIF_F_GSO_PARTIAL |
4601 			       IXGBEVF_GSO_PARTIAL_FEATURES;
4602 
4603 	netdev->features = netdev->hw_features;
4604 
4605 	if (pci_using_dac)
4606 		netdev->features |= NETIF_F_HIGHDMA;
4607 
4608 	netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
4609 	netdev->mpls_features |= NETIF_F_SG |
4610 				 NETIF_F_TSO |
4611 				 NETIF_F_TSO6 |
4612 				 NETIF_F_HW_CSUM;
4613 	netdev->mpls_features |= IXGBEVF_GSO_PARTIAL_FEATURES;
4614 	netdev->hw_enc_features |= netdev->vlan_features;
4615 
4616 	/* set this bit last since it cannot be part of vlan_features */
4617 	netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4618 			    NETIF_F_HW_VLAN_CTAG_RX |
4619 			    NETIF_F_HW_VLAN_CTAG_TX;
4620 
4621 	netdev->priv_flags |= IFF_UNICAST_FLT;
4622 
4623 	/* MTU range: 68 - 1504 or 9710 */
4624 	netdev->min_mtu = ETH_MIN_MTU;
4625 	switch (adapter->hw.api_version) {
4626 	case ixgbe_mbox_api_11:
4627 	case ixgbe_mbox_api_12:
4628 	case ixgbe_mbox_api_13:
4629 	case ixgbe_mbox_api_14:
4630 		netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4631 				  (ETH_HLEN + ETH_FCS_LEN);
4632 		break;
4633 	default:
4634 		if (adapter->hw.mac.type != ixgbe_mac_82599_vf)
4635 			netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4636 					  (ETH_HLEN + ETH_FCS_LEN);
4637 		else
4638 			netdev->max_mtu = ETH_DATA_LEN + ETH_FCS_LEN;
4639 		break;
4640 	}
4641 
4642 	if (IXGBE_REMOVED(hw->hw_addr)) {
4643 		err = -EIO;
4644 		goto err_sw_init;
4645 	}
4646 
4647 	timer_setup(&adapter->service_timer, ixgbevf_service_timer, 0);
4648 
4649 	INIT_WORK(&adapter->service_task, ixgbevf_service_task);
4650 	set_bit(__IXGBEVF_SERVICE_INITED, &adapter->state);
4651 	clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
4652 
4653 	err = ixgbevf_init_interrupt_scheme(adapter);
4654 	if (err)
4655 		goto err_sw_init;
4656 
4657 	strcpy(netdev->name, "eth%d");
4658 
4659 	err = register_netdev(netdev);
4660 	if (err)
4661 		goto err_register;
4662 
4663 	pci_set_drvdata(pdev, netdev);
4664 	netif_carrier_off(netdev);
4665 	ixgbevf_init_ipsec_offload(adapter);
4666 
4667 	ixgbevf_init_last_counter_stats(adapter);
4668 
4669 	/* print the VF info */
4670 	dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
4671 	dev_info(&pdev->dev, "MAC: %d\n", hw->mac.type);
4672 
4673 	switch (hw->mac.type) {
4674 	case ixgbe_mac_X550_vf:
4675 		dev_info(&pdev->dev, "Intel(R) X550 Virtual Function\n");
4676 		break;
4677 	case ixgbe_mac_X540_vf:
4678 		dev_info(&pdev->dev, "Intel(R) X540 Virtual Function\n");
4679 		break;
4680 	case ixgbe_mac_82599_vf:
4681 	default:
4682 		dev_info(&pdev->dev, "Intel(R) 82599 Virtual Function\n");
4683 		break;
4684 	}
4685 
4686 	return 0;
4687 
4688 err_register:
4689 	ixgbevf_clear_interrupt_scheme(adapter);
4690 err_sw_init:
4691 	ixgbevf_reset_interrupt_capability(adapter);
4692 	iounmap(adapter->io_addr);
4693 	kfree(adapter->rss_key);
4694 err_ioremap:
4695 	disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4696 	free_netdev(netdev);
4697 err_alloc_etherdev:
4698 	pci_release_regions(pdev);
4699 err_pci_reg:
4700 err_dma:
4701 	if (!adapter || disable_dev)
4702 		pci_disable_device(pdev);
4703 	return err;
4704 }
4705 
4706 /**
4707  * ixgbevf_remove - Device Removal Routine
4708  * @pdev: PCI device information struct
4709  *
4710  * ixgbevf_remove is called by the PCI subsystem to alert the driver
4711  * that it should release a PCI device.  The could be caused by a
4712  * Hot-Plug event, or because the driver is going to be removed from
4713  * memory.
4714  **/
4715 static void ixgbevf_remove(struct pci_dev *pdev)
4716 {
4717 	struct net_device *netdev = pci_get_drvdata(pdev);
4718 	struct ixgbevf_adapter *adapter;
4719 	bool disable_dev;
4720 
4721 	if (!netdev)
4722 		return;
4723 
4724 	adapter = netdev_priv(netdev);
4725 
4726 	set_bit(__IXGBEVF_REMOVING, &adapter->state);
4727 	cancel_work_sync(&adapter->service_task);
4728 
4729 	if (netdev->reg_state == NETREG_REGISTERED)
4730 		unregister_netdev(netdev);
4731 
4732 	ixgbevf_stop_ipsec_offload(adapter);
4733 	ixgbevf_clear_interrupt_scheme(adapter);
4734 	ixgbevf_reset_interrupt_capability(adapter);
4735 
4736 	iounmap(adapter->io_addr);
4737 	pci_release_regions(pdev);
4738 
4739 	hw_dbg(&adapter->hw, "Remove complete\n");
4740 
4741 	kfree(adapter->rss_key);
4742 	disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4743 	free_netdev(netdev);
4744 
4745 	if (disable_dev)
4746 		pci_disable_device(pdev);
4747 }
4748 
4749 /**
4750  * ixgbevf_io_error_detected - called when PCI error is detected
4751  * @pdev: Pointer to PCI device
4752  * @state: The current pci connection state
4753  *
4754  * This function is called after a PCI bus error affecting
4755  * this device has been detected.
4756  **/
4757 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
4758 						  pci_channel_state_t state)
4759 {
4760 	struct net_device *netdev = pci_get_drvdata(pdev);
4761 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4762 
4763 	if (!test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
4764 		return PCI_ERS_RESULT_DISCONNECT;
4765 
4766 	rtnl_lock();
4767 	netif_device_detach(netdev);
4768 
4769 	if (netif_running(netdev))
4770 		ixgbevf_close_suspend(adapter);
4771 
4772 	if (state == pci_channel_io_perm_failure) {
4773 		rtnl_unlock();
4774 		return PCI_ERS_RESULT_DISCONNECT;
4775 	}
4776 
4777 	if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
4778 		pci_disable_device(pdev);
4779 	rtnl_unlock();
4780 
4781 	/* Request a slot slot reset. */
4782 	return PCI_ERS_RESULT_NEED_RESET;
4783 }
4784 
4785 /**
4786  * ixgbevf_io_slot_reset - called after the pci bus has been reset.
4787  * @pdev: Pointer to PCI device
4788  *
4789  * Restart the card from scratch, as if from a cold-boot. Implementation
4790  * resembles the first-half of the ixgbevf_resume routine.
4791  **/
4792 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
4793 {
4794 	struct net_device *netdev = pci_get_drvdata(pdev);
4795 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4796 
4797 	if (pci_enable_device_mem(pdev)) {
4798 		dev_err(&pdev->dev,
4799 			"Cannot re-enable PCI device after reset.\n");
4800 		return PCI_ERS_RESULT_DISCONNECT;
4801 	}
4802 
4803 	adapter->hw.hw_addr = adapter->io_addr;
4804 	smp_mb__before_atomic();
4805 	clear_bit(__IXGBEVF_DISABLED, &adapter->state);
4806 	pci_set_master(pdev);
4807 
4808 	ixgbevf_reset(adapter);
4809 
4810 	return PCI_ERS_RESULT_RECOVERED;
4811 }
4812 
4813 /**
4814  * ixgbevf_io_resume - called when traffic can start flowing again.
4815  * @pdev: Pointer to PCI device
4816  *
4817  * This callback is called when the error recovery driver tells us that
4818  * its OK to resume normal operation. Implementation resembles the
4819  * second-half of the ixgbevf_resume routine.
4820  **/
4821 static void ixgbevf_io_resume(struct pci_dev *pdev)
4822 {
4823 	struct net_device *netdev = pci_get_drvdata(pdev);
4824 
4825 	rtnl_lock();
4826 	if (netif_running(netdev))
4827 		ixgbevf_open(netdev);
4828 
4829 	netif_device_attach(netdev);
4830 	rtnl_unlock();
4831 }
4832 
4833 /* PCI Error Recovery (ERS) */
4834 static const struct pci_error_handlers ixgbevf_err_handler = {
4835 	.error_detected = ixgbevf_io_error_detected,
4836 	.slot_reset = ixgbevf_io_slot_reset,
4837 	.resume = ixgbevf_io_resume,
4838 };
4839 
4840 static SIMPLE_DEV_PM_OPS(ixgbevf_pm_ops, ixgbevf_suspend, ixgbevf_resume);
4841 
4842 static struct pci_driver ixgbevf_driver = {
4843 	.name		= ixgbevf_driver_name,
4844 	.id_table	= ixgbevf_pci_tbl,
4845 	.probe		= ixgbevf_probe,
4846 	.remove		= ixgbevf_remove,
4847 
4848 	/* Power Management Hooks */
4849 	.driver.pm	= &ixgbevf_pm_ops,
4850 
4851 	.shutdown	= ixgbevf_shutdown,
4852 	.err_handler	= &ixgbevf_err_handler
4853 };
4854 
4855 /**
4856  * ixgbevf_init_module - Driver Registration Routine
4857  *
4858  * ixgbevf_init_module is the first routine called when the driver is
4859  * loaded. All it does is register with the PCI subsystem.
4860  **/
4861 static int __init ixgbevf_init_module(void)
4862 {
4863 	pr_info("%s\n", ixgbevf_driver_string);
4864 	pr_info("%s\n", ixgbevf_copyright);
4865 	ixgbevf_wq = create_singlethread_workqueue(ixgbevf_driver_name);
4866 	if (!ixgbevf_wq) {
4867 		pr_err("%s: Failed to create workqueue\n", ixgbevf_driver_name);
4868 		return -ENOMEM;
4869 	}
4870 
4871 	return pci_register_driver(&ixgbevf_driver);
4872 }
4873 
4874 module_init(ixgbevf_init_module);
4875 
4876 /**
4877  * ixgbevf_exit_module - Driver Exit Cleanup Routine
4878  *
4879  * ixgbevf_exit_module is called just before the driver is removed
4880  * from memory.
4881  **/
4882 static void __exit ixgbevf_exit_module(void)
4883 {
4884 	pci_unregister_driver(&ixgbevf_driver);
4885 	if (ixgbevf_wq) {
4886 		destroy_workqueue(ixgbevf_wq);
4887 		ixgbevf_wq = NULL;
4888 	}
4889 }
4890 
4891 #ifdef DEBUG
4892 /**
4893  * ixgbevf_get_hw_dev_name - return device name string
4894  * used by hardware layer to print debugging information
4895  * @hw: pointer to private hardware struct
4896  **/
4897 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
4898 {
4899 	struct ixgbevf_adapter *adapter = hw->back;
4900 
4901 	return adapter->netdev->name;
4902 }
4903 
4904 #endif
4905 module_exit(ixgbevf_exit_module);
4906 
4907 /* ixgbevf_main.c */
4908