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