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