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