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