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