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