xref: /linux/drivers/net/ethernet/intel/igc/igc_main.c (revision 84b9b44b)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c)  2018 Intel Corporation */
3 
4 #include <linux/module.h>
5 #include <linux/types.h>
6 #include <linux/if_vlan.h>
7 #include <linux/tcp.h>
8 #include <linux/udp.h>
9 #include <linux/ip.h>
10 #include <linux/pm_runtime.h>
11 #include <net/pkt_sched.h>
12 #include <linux/bpf_trace.h>
13 #include <net/xdp_sock_drv.h>
14 #include <linux/pci.h>
15 
16 #include <net/ipv6.h>
17 
18 #include "igc.h"
19 #include "igc_hw.h"
20 #include "igc_tsn.h"
21 #include "igc_xdp.h"
22 
23 #define DRV_SUMMARY	"Intel(R) 2.5G Ethernet Linux Driver"
24 
25 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
26 
27 #define IGC_XDP_PASS		0
28 #define IGC_XDP_CONSUMED	BIT(0)
29 #define IGC_XDP_TX		BIT(1)
30 #define IGC_XDP_REDIRECT	BIT(2)
31 
32 static int debug = -1;
33 
34 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
35 MODULE_DESCRIPTION(DRV_SUMMARY);
36 MODULE_LICENSE("GPL v2");
37 module_param(debug, int, 0);
38 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
39 
40 char igc_driver_name[] = "igc";
41 static const char igc_driver_string[] = DRV_SUMMARY;
42 static const char igc_copyright[] =
43 	"Copyright(c) 2018 Intel Corporation.";
44 
45 static const struct igc_info *igc_info_tbl[] = {
46 	[board_base] = &igc_base_info,
47 };
48 
49 static const struct pci_device_id igc_pci_tbl[] = {
50 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LM), board_base },
51 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_V), board_base },
52 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_I), board_base },
53 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I220_V), board_base },
54 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_K), board_base },
55 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_K2), board_base },
56 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_K), board_base },
57 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LMVP), board_base },
58 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_LMVP), board_base },
59 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_IT), board_base },
60 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_LM), board_base },
61 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_V), board_base },
62 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_IT), board_base },
63 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I221_V), board_base },
64 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_BLANK_NVM), board_base },
65 	{ PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_BLANK_NVM), board_base },
66 	/* required last entry */
67 	{0, }
68 };
69 
70 MODULE_DEVICE_TABLE(pci, igc_pci_tbl);
71 
72 enum latency_range {
73 	lowest_latency = 0,
74 	low_latency = 1,
75 	bulk_latency = 2,
76 	latency_invalid = 255
77 };
78 
79 void igc_reset(struct igc_adapter *adapter)
80 {
81 	struct net_device *dev = adapter->netdev;
82 	struct igc_hw *hw = &adapter->hw;
83 	struct igc_fc_info *fc = &hw->fc;
84 	u32 pba, hwm;
85 
86 	/* Repartition PBA for greater than 9k MTU if required */
87 	pba = IGC_PBA_34K;
88 
89 	/* flow control settings
90 	 * The high water mark must be low enough to fit one full frame
91 	 * after transmitting the pause frame.  As such we must have enough
92 	 * space to allow for us to complete our current transmit and then
93 	 * receive the frame that is in progress from the link partner.
94 	 * Set it to:
95 	 * - the full Rx FIFO size minus one full Tx plus one full Rx frame
96 	 */
97 	hwm = (pba << 10) - (adapter->max_frame_size + MAX_JUMBO_FRAME_SIZE);
98 
99 	fc->high_water = hwm & 0xFFFFFFF0;	/* 16-byte granularity */
100 	fc->low_water = fc->high_water - 16;
101 	fc->pause_time = 0xFFFF;
102 	fc->send_xon = 1;
103 	fc->current_mode = fc->requested_mode;
104 
105 	hw->mac.ops.reset_hw(hw);
106 
107 	if (hw->mac.ops.init_hw(hw))
108 		netdev_err(dev, "Error on hardware initialization\n");
109 
110 	/* Re-establish EEE setting */
111 	igc_set_eee_i225(hw, true, true, true);
112 
113 	if (!netif_running(adapter->netdev))
114 		igc_power_down_phy_copper_base(&adapter->hw);
115 
116 	/* Enable HW to recognize an 802.1Q VLAN Ethernet packet */
117 	wr32(IGC_VET, ETH_P_8021Q);
118 
119 	/* Re-enable PTP, where applicable. */
120 	igc_ptp_reset(adapter);
121 
122 	/* Re-enable TSN offloading, where applicable. */
123 	igc_tsn_reset(adapter);
124 
125 	igc_get_phy_info(hw);
126 }
127 
128 /**
129  * igc_power_up_link - Power up the phy link
130  * @adapter: address of board private structure
131  */
132 static void igc_power_up_link(struct igc_adapter *adapter)
133 {
134 	igc_reset_phy(&adapter->hw);
135 
136 	igc_power_up_phy_copper(&adapter->hw);
137 
138 	igc_setup_link(&adapter->hw);
139 }
140 
141 /**
142  * igc_release_hw_control - release control of the h/w to f/w
143  * @adapter: address of board private structure
144  *
145  * igc_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
146  * For ASF and Pass Through versions of f/w this means that the
147  * driver is no longer loaded.
148  */
149 static void igc_release_hw_control(struct igc_adapter *adapter)
150 {
151 	struct igc_hw *hw = &adapter->hw;
152 	u32 ctrl_ext;
153 
154 	if (!pci_device_is_present(adapter->pdev))
155 		return;
156 
157 	/* Let firmware take over control of h/w */
158 	ctrl_ext = rd32(IGC_CTRL_EXT);
159 	wr32(IGC_CTRL_EXT,
160 	     ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD);
161 }
162 
163 /**
164  * igc_get_hw_control - get control of the h/w from f/w
165  * @adapter: address of board private structure
166  *
167  * igc_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
168  * For ASF and Pass Through versions of f/w this means that
169  * the driver is loaded.
170  */
171 static void igc_get_hw_control(struct igc_adapter *adapter)
172 {
173 	struct igc_hw *hw = &adapter->hw;
174 	u32 ctrl_ext;
175 
176 	/* Let firmware know the driver has taken over */
177 	ctrl_ext = rd32(IGC_CTRL_EXT);
178 	wr32(IGC_CTRL_EXT,
179 	     ctrl_ext | IGC_CTRL_EXT_DRV_LOAD);
180 }
181 
182 static void igc_unmap_tx_buffer(struct device *dev, struct igc_tx_buffer *buf)
183 {
184 	dma_unmap_single(dev, dma_unmap_addr(buf, dma),
185 			 dma_unmap_len(buf, len), DMA_TO_DEVICE);
186 
187 	dma_unmap_len_set(buf, len, 0);
188 }
189 
190 /**
191  * igc_clean_tx_ring - Free Tx Buffers
192  * @tx_ring: ring to be cleaned
193  */
194 static void igc_clean_tx_ring(struct igc_ring *tx_ring)
195 {
196 	u16 i = tx_ring->next_to_clean;
197 	struct igc_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
198 	u32 xsk_frames = 0;
199 
200 	while (i != tx_ring->next_to_use) {
201 		union igc_adv_tx_desc *eop_desc, *tx_desc;
202 
203 		switch (tx_buffer->type) {
204 		case IGC_TX_BUFFER_TYPE_XSK:
205 			xsk_frames++;
206 			break;
207 		case IGC_TX_BUFFER_TYPE_XDP:
208 			xdp_return_frame(tx_buffer->xdpf);
209 			igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
210 			break;
211 		case IGC_TX_BUFFER_TYPE_SKB:
212 			dev_kfree_skb_any(tx_buffer->skb);
213 			igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
214 			break;
215 		default:
216 			netdev_warn_once(tx_ring->netdev, "Unknown Tx buffer type\n");
217 			break;
218 		}
219 
220 		/* check for eop_desc to determine the end of the packet */
221 		eop_desc = tx_buffer->next_to_watch;
222 		tx_desc = IGC_TX_DESC(tx_ring, i);
223 
224 		/* unmap remaining buffers */
225 		while (tx_desc != eop_desc) {
226 			tx_buffer++;
227 			tx_desc++;
228 			i++;
229 			if (unlikely(i == tx_ring->count)) {
230 				i = 0;
231 				tx_buffer = tx_ring->tx_buffer_info;
232 				tx_desc = IGC_TX_DESC(tx_ring, 0);
233 			}
234 
235 			/* unmap any remaining paged data */
236 			if (dma_unmap_len(tx_buffer, len))
237 				igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
238 		}
239 
240 		tx_buffer->next_to_watch = NULL;
241 
242 		/* move us one more past the eop_desc for start of next pkt */
243 		tx_buffer++;
244 		i++;
245 		if (unlikely(i == tx_ring->count)) {
246 			i = 0;
247 			tx_buffer = tx_ring->tx_buffer_info;
248 		}
249 	}
250 
251 	if (tx_ring->xsk_pool && xsk_frames)
252 		xsk_tx_completed(tx_ring->xsk_pool, xsk_frames);
253 
254 	/* reset BQL for queue */
255 	netdev_tx_reset_queue(txring_txq(tx_ring));
256 
257 	/* reset next_to_use and next_to_clean */
258 	tx_ring->next_to_use = 0;
259 	tx_ring->next_to_clean = 0;
260 }
261 
262 /**
263  * igc_free_tx_resources - Free Tx Resources per Queue
264  * @tx_ring: Tx descriptor ring for a specific queue
265  *
266  * Free all transmit software resources
267  */
268 void igc_free_tx_resources(struct igc_ring *tx_ring)
269 {
270 	igc_clean_tx_ring(tx_ring);
271 
272 	vfree(tx_ring->tx_buffer_info);
273 	tx_ring->tx_buffer_info = NULL;
274 
275 	/* if not set, then don't free */
276 	if (!tx_ring->desc)
277 		return;
278 
279 	dma_free_coherent(tx_ring->dev, tx_ring->size,
280 			  tx_ring->desc, tx_ring->dma);
281 
282 	tx_ring->desc = NULL;
283 }
284 
285 /**
286  * igc_free_all_tx_resources - Free Tx Resources for All Queues
287  * @adapter: board private structure
288  *
289  * Free all transmit software resources
290  */
291 static void igc_free_all_tx_resources(struct igc_adapter *adapter)
292 {
293 	int i;
294 
295 	for (i = 0; i < adapter->num_tx_queues; i++)
296 		igc_free_tx_resources(adapter->tx_ring[i]);
297 }
298 
299 /**
300  * igc_clean_all_tx_rings - Free Tx Buffers for all queues
301  * @adapter: board private structure
302  */
303 static void igc_clean_all_tx_rings(struct igc_adapter *adapter)
304 {
305 	int i;
306 
307 	for (i = 0; i < adapter->num_tx_queues; i++)
308 		if (adapter->tx_ring[i])
309 			igc_clean_tx_ring(adapter->tx_ring[i]);
310 }
311 
312 /**
313  * igc_setup_tx_resources - allocate Tx resources (Descriptors)
314  * @tx_ring: tx descriptor ring (for a specific queue) to setup
315  *
316  * Return 0 on success, negative on failure
317  */
318 int igc_setup_tx_resources(struct igc_ring *tx_ring)
319 {
320 	struct net_device *ndev = tx_ring->netdev;
321 	struct device *dev = tx_ring->dev;
322 	int size = 0;
323 
324 	size = sizeof(struct igc_tx_buffer) * tx_ring->count;
325 	tx_ring->tx_buffer_info = vzalloc(size);
326 	if (!tx_ring->tx_buffer_info)
327 		goto err;
328 
329 	/* round up to nearest 4K */
330 	tx_ring->size = tx_ring->count * sizeof(union igc_adv_tx_desc);
331 	tx_ring->size = ALIGN(tx_ring->size, 4096);
332 
333 	tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
334 					   &tx_ring->dma, GFP_KERNEL);
335 
336 	if (!tx_ring->desc)
337 		goto err;
338 
339 	tx_ring->next_to_use = 0;
340 	tx_ring->next_to_clean = 0;
341 
342 	return 0;
343 
344 err:
345 	vfree(tx_ring->tx_buffer_info);
346 	netdev_err(ndev, "Unable to allocate memory for Tx descriptor ring\n");
347 	return -ENOMEM;
348 }
349 
350 /**
351  * igc_setup_all_tx_resources - wrapper to allocate Tx resources for all queues
352  * @adapter: board private structure
353  *
354  * Return 0 on success, negative on failure
355  */
356 static int igc_setup_all_tx_resources(struct igc_adapter *adapter)
357 {
358 	struct net_device *dev = adapter->netdev;
359 	int i, err = 0;
360 
361 	for (i = 0; i < adapter->num_tx_queues; i++) {
362 		err = igc_setup_tx_resources(adapter->tx_ring[i]);
363 		if (err) {
364 			netdev_err(dev, "Error on Tx queue %u setup\n", i);
365 			for (i--; i >= 0; i--)
366 				igc_free_tx_resources(adapter->tx_ring[i]);
367 			break;
368 		}
369 	}
370 
371 	return err;
372 }
373 
374 static void igc_clean_rx_ring_page_shared(struct igc_ring *rx_ring)
375 {
376 	u16 i = rx_ring->next_to_clean;
377 
378 	dev_kfree_skb(rx_ring->skb);
379 	rx_ring->skb = NULL;
380 
381 	/* Free all the Rx ring sk_buffs */
382 	while (i != rx_ring->next_to_alloc) {
383 		struct igc_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
384 
385 		/* Invalidate cache lines that may have been written to by
386 		 * device so that we avoid corrupting memory.
387 		 */
388 		dma_sync_single_range_for_cpu(rx_ring->dev,
389 					      buffer_info->dma,
390 					      buffer_info->page_offset,
391 					      igc_rx_bufsz(rx_ring),
392 					      DMA_FROM_DEVICE);
393 
394 		/* free resources associated with mapping */
395 		dma_unmap_page_attrs(rx_ring->dev,
396 				     buffer_info->dma,
397 				     igc_rx_pg_size(rx_ring),
398 				     DMA_FROM_DEVICE,
399 				     IGC_RX_DMA_ATTR);
400 		__page_frag_cache_drain(buffer_info->page,
401 					buffer_info->pagecnt_bias);
402 
403 		i++;
404 		if (i == rx_ring->count)
405 			i = 0;
406 	}
407 }
408 
409 static void igc_clean_rx_ring_xsk_pool(struct igc_ring *ring)
410 {
411 	struct igc_rx_buffer *bi;
412 	u16 i;
413 
414 	for (i = 0; i < ring->count; i++) {
415 		bi = &ring->rx_buffer_info[i];
416 		if (!bi->xdp)
417 			continue;
418 
419 		xsk_buff_free(bi->xdp);
420 		bi->xdp = NULL;
421 	}
422 }
423 
424 /**
425  * igc_clean_rx_ring - Free Rx Buffers per Queue
426  * @ring: ring to free buffers from
427  */
428 static void igc_clean_rx_ring(struct igc_ring *ring)
429 {
430 	if (ring->xsk_pool)
431 		igc_clean_rx_ring_xsk_pool(ring);
432 	else
433 		igc_clean_rx_ring_page_shared(ring);
434 
435 	clear_ring_uses_large_buffer(ring);
436 
437 	ring->next_to_alloc = 0;
438 	ring->next_to_clean = 0;
439 	ring->next_to_use = 0;
440 }
441 
442 /**
443  * igc_clean_all_rx_rings - Free Rx Buffers for all queues
444  * @adapter: board private structure
445  */
446 static void igc_clean_all_rx_rings(struct igc_adapter *adapter)
447 {
448 	int i;
449 
450 	for (i = 0; i < adapter->num_rx_queues; i++)
451 		if (adapter->rx_ring[i])
452 			igc_clean_rx_ring(adapter->rx_ring[i]);
453 }
454 
455 /**
456  * igc_free_rx_resources - Free Rx Resources
457  * @rx_ring: ring to clean the resources from
458  *
459  * Free all receive software resources
460  */
461 void igc_free_rx_resources(struct igc_ring *rx_ring)
462 {
463 	igc_clean_rx_ring(rx_ring);
464 
465 	xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
466 
467 	vfree(rx_ring->rx_buffer_info);
468 	rx_ring->rx_buffer_info = NULL;
469 
470 	/* if not set, then don't free */
471 	if (!rx_ring->desc)
472 		return;
473 
474 	dma_free_coherent(rx_ring->dev, rx_ring->size,
475 			  rx_ring->desc, rx_ring->dma);
476 
477 	rx_ring->desc = NULL;
478 }
479 
480 /**
481  * igc_free_all_rx_resources - Free Rx Resources for All Queues
482  * @adapter: board private structure
483  *
484  * Free all receive software resources
485  */
486 static void igc_free_all_rx_resources(struct igc_adapter *adapter)
487 {
488 	int i;
489 
490 	for (i = 0; i < adapter->num_rx_queues; i++)
491 		igc_free_rx_resources(adapter->rx_ring[i]);
492 }
493 
494 /**
495  * igc_setup_rx_resources - allocate Rx resources (Descriptors)
496  * @rx_ring:    rx descriptor ring (for a specific queue) to setup
497  *
498  * Returns 0 on success, negative on failure
499  */
500 int igc_setup_rx_resources(struct igc_ring *rx_ring)
501 {
502 	struct net_device *ndev = rx_ring->netdev;
503 	struct device *dev = rx_ring->dev;
504 	u8 index = rx_ring->queue_index;
505 	int size, desc_len, res;
506 
507 	/* XDP RX-queue info */
508 	if (xdp_rxq_info_is_reg(&rx_ring->xdp_rxq))
509 		xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
510 	res = xdp_rxq_info_reg(&rx_ring->xdp_rxq, ndev, index,
511 			       rx_ring->q_vector->napi.napi_id);
512 	if (res < 0) {
513 		netdev_err(ndev, "Failed to register xdp_rxq index %u\n",
514 			   index);
515 		return res;
516 	}
517 
518 	size = sizeof(struct igc_rx_buffer) * rx_ring->count;
519 	rx_ring->rx_buffer_info = vzalloc(size);
520 	if (!rx_ring->rx_buffer_info)
521 		goto err;
522 
523 	desc_len = sizeof(union igc_adv_rx_desc);
524 
525 	/* Round up to nearest 4K */
526 	rx_ring->size = rx_ring->count * desc_len;
527 	rx_ring->size = ALIGN(rx_ring->size, 4096);
528 
529 	rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
530 					   &rx_ring->dma, GFP_KERNEL);
531 
532 	if (!rx_ring->desc)
533 		goto err;
534 
535 	rx_ring->next_to_alloc = 0;
536 	rx_ring->next_to_clean = 0;
537 	rx_ring->next_to_use = 0;
538 
539 	return 0;
540 
541 err:
542 	xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
543 	vfree(rx_ring->rx_buffer_info);
544 	rx_ring->rx_buffer_info = NULL;
545 	netdev_err(ndev, "Unable to allocate memory for Rx descriptor ring\n");
546 	return -ENOMEM;
547 }
548 
549 /**
550  * igc_setup_all_rx_resources - wrapper to allocate Rx resources
551  *                                (Descriptors) for all queues
552  * @adapter: board private structure
553  *
554  * Return 0 on success, negative on failure
555  */
556 static int igc_setup_all_rx_resources(struct igc_adapter *adapter)
557 {
558 	struct net_device *dev = adapter->netdev;
559 	int i, err = 0;
560 
561 	for (i = 0; i < adapter->num_rx_queues; i++) {
562 		err = igc_setup_rx_resources(adapter->rx_ring[i]);
563 		if (err) {
564 			netdev_err(dev, "Error on Rx queue %u setup\n", i);
565 			for (i--; i >= 0; i--)
566 				igc_free_rx_resources(adapter->rx_ring[i]);
567 			break;
568 		}
569 	}
570 
571 	return err;
572 }
573 
574 static struct xsk_buff_pool *igc_get_xsk_pool(struct igc_adapter *adapter,
575 					      struct igc_ring *ring)
576 {
577 	if (!igc_xdp_is_enabled(adapter) ||
578 	    !test_bit(IGC_RING_FLAG_AF_XDP_ZC, &ring->flags))
579 		return NULL;
580 
581 	return xsk_get_pool_from_qid(ring->netdev, ring->queue_index);
582 }
583 
584 /**
585  * igc_configure_rx_ring - Configure a receive ring after Reset
586  * @adapter: board private structure
587  * @ring: receive ring to be configured
588  *
589  * Configure the Rx unit of the MAC after a reset.
590  */
591 static void igc_configure_rx_ring(struct igc_adapter *adapter,
592 				  struct igc_ring *ring)
593 {
594 	struct igc_hw *hw = &adapter->hw;
595 	union igc_adv_rx_desc *rx_desc;
596 	int reg_idx = ring->reg_idx;
597 	u32 srrctl = 0, rxdctl = 0;
598 	u64 rdba = ring->dma;
599 	u32 buf_size;
600 
601 	xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
602 	ring->xsk_pool = igc_get_xsk_pool(adapter, ring);
603 	if (ring->xsk_pool) {
604 		WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
605 						   MEM_TYPE_XSK_BUFF_POOL,
606 						   NULL));
607 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
608 	} else {
609 		WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
610 						   MEM_TYPE_PAGE_SHARED,
611 						   NULL));
612 	}
613 
614 	if (igc_xdp_is_enabled(adapter))
615 		set_ring_uses_large_buffer(ring);
616 
617 	/* disable the queue */
618 	wr32(IGC_RXDCTL(reg_idx), 0);
619 
620 	/* Set DMA base address registers */
621 	wr32(IGC_RDBAL(reg_idx),
622 	     rdba & 0x00000000ffffffffULL);
623 	wr32(IGC_RDBAH(reg_idx), rdba >> 32);
624 	wr32(IGC_RDLEN(reg_idx),
625 	     ring->count * sizeof(union igc_adv_rx_desc));
626 
627 	/* initialize head and tail */
628 	ring->tail = adapter->io_addr + IGC_RDT(reg_idx);
629 	wr32(IGC_RDH(reg_idx), 0);
630 	writel(0, ring->tail);
631 
632 	/* reset next-to- use/clean to place SW in sync with hardware */
633 	ring->next_to_clean = 0;
634 	ring->next_to_use = 0;
635 
636 	if (ring->xsk_pool)
637 		buf_size = xsk_pool_get_rx_frame_size(ring->xsk_pool);
638 	else if (ring_uses_large_buffer(ring))
639 		buf_size = IGC_RXBUFFER_3072;
640 	else
641 		buf_size = IGC_RXBUFFER_2048;
642 
643 	srrctl = rd32(IGC_SRRCTL(reg_idx));
644 	srrctl &= ~(IGC_SRRCTL_BSIZEPKT_MASK | IGC_SRRCTL_BSIZEHDR_MASK |
645 		    IGC_SRRCTL_DESCTYPE_MASK);
646 	srrctl |= IGC_SRRCTL_BSIZEHDR(IGC_RX_HDR_LEN);
647 	srrctl |= IGC_SRRCTL_BSIZEPKT(buf_size);
648 	srrctl |= IGC_SRRCTL_DESCTYPE_ADV_ONEBUF;
649 
650 	wr32(IGC_SRRCTL(reg_idx), srrctl);
651 
652 	rxdctl |= IGC_RX_PTHRESH;
653 	rxdctl |= IGC_RX_HTHRESH << 8;
654 	rxdctl |= IGC_RX_WTHRESH << 16;
655 
656 	/* initialize rx_buffer_info */
657 	memset(ring->rx_buffer_info, 0,
658 	       sizeof(struct igc_rx_buffer) * ring->count);
659 
660 	/* initialize Rx descriptor 0 */
661 	rx_desc = IGC_RX_DESC(ring, 0);
662 	rx_desc->wb.upper.length = 0;
663 
664 	/* enable receive descriptor fetching */
665 	rxdctl |= IGC_RXDCTL_QUEUE_ENABLE;
666 
667 	wr32(IGC_RXDCTL(reg_idx), rxdctl);
668 }
669 
670 /**
671  * igc_configure_rx - Configure receive Unit after Reset
672  * @adapter: board private structure
673  *
674  * Configure the Rx unit of the MAC after a reset.
675  */
676 static void igc_configure_rx(struct igc_adapter *adapter)
677 {
678 	int i;
679 
680 	/* Setup the HW Rx Head and Tail Descriptor Pointers and
681 	 * the Base and Length of the Rx Descriptor Ring
682 	 */
683 	for (i = 0; i < adapter->num_rx_queues; i++)
684 		igc_configure_rx_ring(adapter, adapter->rx_ring[i]);
685 }
686 
687 /**
688  * igc_configure_tx_ring - Configure transmit ring after Reset
689  * @adapter: board private structure
690  * @ring: tx ring to configure
691  *
692  * Configure a transmit ring after a reset.
693  */
694 static void igc_configure_tx_ring(struct igc_adapter *adapter,
695 				  struct igc_ring *ring)
696 {
697 	struct igc_hw *hw = &adapter->hw;
698 	int reg_idx = ring->reg_idx;
699 	u64 tdba = ring->dma;
700 	u32 txdctl = 0;
701 
702 	ring->xsk_pool = igc_get_xsk_pool(adapter, ring);
703 
704 	/* disable the queue */
705 	wr32(IGC_TXDCTL(reg_idx), 0);
706 	wrfl();
707 	mdelay(10);
708 
709 	wr32(IGC_TDLEN(reg_idx),
710 	     ring->count * sizeof(union igc_adv_tx_desc));
711 	wr32(IGC_TDBAL(reg_idx),
712 	     tdba & 0x00000000ffffffffULL);
713 	wr32(IGC_TDBAH(reg_idx), tdba >> 32);
714 
715 	ring->tail = adapter->io_addr + IGC_TDT(reg_idx);
716 	wr32(IGC_TDH(reg_idx), 0);
717 	writel(0, ring->tail);
718 
719 	txdctl |= IGC_TX_PTHRESH;
720 	txdctl |= IGC_TX_HTHRESH << 8;
721 	txdctl |= IGC_TX_WTHRESH << 16;
722 
723 	txdctl |= IGC_TXDCTL_QUEUE_ENABLE;
724 	wr32(IGC_TXDCTL(reg_idx), txdctl);
725 }
726 
727 /**
728  * igc_configure_tx - Configure transmit Unit after Reset
729  * @adapter: board private structure
730  *
731  * Configure the Tx unit of the MAC after a reset.
732  */
733 static void igc_configure_tx(struct igc_adapter *adapter)
734 {
735 	int i;
736 
737 	for (i = 0; i < adapter->num_tx_queues; i++)
738 		igc_configure_tx_ring(adapter, adapter->tx_ring[i]);
739 }
740 
741 /**
742  * igc_setup_mrqc - configure the multiple receive queue control registers
743  * @adapter: Board private structure
744  */
745 static void igc_setup_mrqc(struct igc_adapter *adapter)
746 {
747 	struct igc_hw *hw = &adapter->hw;
748 	u32 j, num_rx_queues;
749 	u32 mrqc, rxcsum;
750 	u32 rss_key[10];
751 
752 	netdev_rss_key_fill(rss_key, sizeof(rss_key));
753 	for (j = 0; j < 10; j++)
754 		wr32(IGC_RSSRK(j), rss_key[j]);
755 
756 	num_rx_queues = adapter->rss_queues;
757 
758 	if (adapter->rss_indir_tbl_init != num_rx_queues) {
759 		for (j = 0; j < IGC_RETA_SIZE; j++)
760 			adapter->rss_indir_tbl[j] =
761 			(j * num_rx_queues) / IGC_RETA_SIZE;
762 		adapter->rss_indir_tbl_init = num_rx_queues;
763 	}
764 	igc_write_rss_indir_tbl(adapter);
765 
766 	/* Disable raw packet checksumming so that RSS hash is placed in
767 	 * descriptor on writeback.  No need to enable TCP/UDP/IP checksum
768 	 * offloads as they are enabled by default
769 	 */
770 	rxcsum = rd32(IGC_RXCSUM);
771 	rxcsum |= IGC_RXCSUM_PCSD;
772 
773 	/* Enable Receive Checksum Offload for SCTP */
774 	rxcsum |= IGC_RXCSUM_CRCOFL;
775 
776 	/* Don't need to set TUOFL or IPOFL, they default to 1 */
777 	wr32(IGC_RXCSUM, rxcsum);
778 
779 	/* Generate RSS hash based on packet types, TCP/UDP
780 	 * port numbers and/or IPv4/v6 src and dst addresses
781 	 */
782 	mrqc = IGC_MRQC_RSS_FIELD_IPV4 |
783 	       IGC_MRQC_RSS_FIELD_IPV4_TCP |
784 	       IGC_MRQC_RSS_FIELD_IPV6 |
785 	       IGC_MRQC_RSS_FIELD_IPV6_TCP |
786 	       IGC_MRQC_RSS_FIELD_IPV6_TCP_EX;
787 
788 	if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV4_UDP)
789 		mrqc |= IGC_MRQC_RSS_FIELD_IPV4_UDP;
790 	if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
791 		mrqc |= IGC_MRQC_RSS_FIELD_IPV6_UDP;
792 
793 	mrqc |= IGC_MRQC_ENABLE_RSS_MQ;
794 
795 	wr32(IGC_MRQC, mrqc);
796 }
797 
798 /**
799  * igc_setup_rctl - configure the receive control registers
800  * @adapter: Board private structure
801  */
802 static void igc_setup_rctl(struct igc_adapter *adapter)
803 {
804 	struct igc_hw *hw = &adapter->hw;
805 	u32 rctl;
806 
807 	rctl = rd32(IGC_RCTL);
808 
809 	rctl &= ~(3 << IGC_RCTL_MO_SHIFT);
810 	rctl &= ~(IGC_RCTL_LBM_TCVR | IGC_RCTL_LBM_MAC);
811 
812 	rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_RDMTS_HALF |
813 		(hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT);
814 
815 	/* enable stripping of CRC. Newer features require
816 	 * that the HW strips the CRC.
817 	 */
818 	rctl |= IGC_RCTL_SECRC;
819 
820 	/* disable store bad packets and clear size bits. */
821 	rctl &= ~(IGC_RCTL_SBP | IGC_RCTL_SZ_256);
822 
823 	/* enable LPE to allow for reception of jumbo frames */
824 	rctl |= IGC_RCTL_LPE;
825 
826 	/* disable queue 0 to prevent tail write w/o re-config */
827 	wr32(IGC_RXDCTL(0), 0);
828 
829 	/* This is useful for sniffing bad packets. */
830 	if (adapter->netdev->features & NETIF_F_RXALL) {
831 		/* UPE and MPE will be handled by normal PROMISC logic
832 		 * in set_rx_mode
833 		 */
834 		rctl |= (IGC_RCTL_SBP | /* Receive bad packets */
835 			 IGC_RCTL_BAM | /* RX All Bcast Pkts */
836 			 IGC_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
837 
838 		rctl &= ~(IGC_RCTL_DPF | /* Allow filtered pause */
839 			  IGC_RCTL_CFIEN); /* Disable VLAN CFIEN Filter */
840 	}
841 
842 	wr32(IGC_RCTL, rctl);
843 }
844 
845 /**
846  * igc_setup_tctl - configure the transmit control registers
847  * @adapter: Board private structure
848  */
849 static void igc_setup_tctl(struct igc_adapter *adapter)
850 {
851 	struct igc_hw *hw = &adapter->hw;
852 	u32 tctl;
853 
854 	/* disable queue 0 which icould be enabled by default */
855 	wr32(IGC_TXDCTL(0), 0);
856 
857 	/* Program the Transmit Control Register */
858 	tctl = rd32(IGC_TCTL);
859 	tctl &= ~IGC_TCTL_CT;
860 	tctl |= IGC_TCTL_PSP | IGC_TCTL_RTLC |
861 		(IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT);
862 
863 	/* Enable transmits */
864 	tctl |= IGC_TCTL_EN;
865 
866 	wr32(IGC_TCTL, tctl);
867 }
868 
869 /**
870  * igc_set_mac_filter_hw() - Set MAC address filter in hardware
871  * @adapter: Pointer to adapter where the filter should be set
872  * @index: Filter index
873  * @type: MAC address filter type (source or destination)
874  * @addr: MAC address
875  * @queue: If non-negative, queue assignment feature is enabled and frames
876  *         matching the filter are enqueued onto 'queue'. Otherwise, queue
877  *         assignment is disabled.
878  */
879 static void igc_set_mac_filter_hw(struct igc_adapter *adapter, int index,
880 				  enum igc_mac_filter_type type,
881 				  const u8 *addr, int queue)
882 {
883 	struct net_device *dev = adapter->netdev;
884 	struct igc_hw *hw = &adapter->hw;
885 	u32 ral, rah;
886 
887 	if (WARN_ON(index >= hw->mac.rar_entry_count))
888 		return;
889 
890 	ral = le32_to_cpup((__le32 *)(addr));
891 	rah = le16_to_cpup((__le16 *)(addr + 4));
892 
893 	if (type == IGC_MAC_FILTER_TYPE_SRC) {
894 		rah &= ~IGC_RAH_ASEL_MASK;
895 		rah |= IGC_RAH_ASEL_SRC_ADDR;
896 	}
897 
898 	if (queue >= 0) {
899 		rah &= ~IGC_RAH_QSEL_MASK;
900 		rah |= (queue << IGC_RAH_QSEL_SHIFT);
901 		rah |= IGC_RAH_QSEL_ENABLE;
902 	}
903 
904 	rah |= IGC_RAH_AV;
905 
906 	wr32(IGC_RAL(index), ral);
907 	wr32(IGC_RAH(index), rah);
908 
909 	netdev_dbg(dev, "MAC address filter set in HW: index %d", index);
910 }
911 
912 /**
913  * igc_clear_mac_filter_hw() - Clear MAC address filter in hardware
914  * @adapter: Pointer to adapter where the filter should be cleared
915  * @index: Filter index
916  */
917 static void igc_clear_mac_filter_hw(struct igc_adapter *adapter, int index)
918 {
919 	struct net_device *dev = adapter->netdev;
920 	struct igc_hw *hw = &adapter->hw;
921 
922 	if (WARN_ON(index >= hw->mac.rar_entry_count))
923 		return;
924 
925 	wr32(IGC_RAL(index), 0);
926 	wr32(IGC_RAH(index), 0);
927 
928 	netdev_dbg(dev, "MAC address filter cleared in HW: index %d", index);
929 }
930 
931 /* Set default MAC address for the PF in the first RAR entry */
932 static void igc_set_default_mac_filter(struct igc_adapter *adapter)
933 {
934 	struct net_device *dev = adapter->netdev;
935 	u8 *addr = adapter->hw.mac.addr;
936 
937 	netdev_dbg(dev, "Set default MAC address filter: address %pM", addr);
938 
939 	igc_set_mac_filter_hw(adapter, 0, IGC_MAC_FILTER_TYPE_DST, addr, -1);
940 }
941 
942 /**
943  * igc_set_mac - Change the Ethernet Address of the NIC
944  * @netdev: network interface device structure
945  * @p: pointer to an address structure
946  *
947  * Returns 0 on success, negative on failure
948  */
949 static int igc_set_mac(struct net_device *netdev, void *p)
950 {
951 	struct igc_adapter *adapter = netdev_priv(netdev);
952 	struct igc_hw *hw = &adapter->hw;
953 	struct sockaddr *addr = p;
954 
955 	if (!is_valid_ether_addr(addr->sa_data))
956 		return -EADDRNOTAVAIL;
957 
958 	eth_hw_addr_set(netdev, addr->sa_data);
959 	memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
960 
961 	/* set the correct pool for the new PF MAC address in entry 0 */
962 	igc_set_default_mac_filter(adapter);
963 
964 	return 0;
965 }
966 
967 /**
968  *  igc_write_mc_addr_list - write multicast addresses to MTA
969  *  @netdev: network interface device structure
970  *
971  *  Writes multicast address list to the MTA hash table.
972  *  Returns: -ENOMEM on failure
973  *           0 on no addresses written
974  *           X on writing X addresses to MTA
975  **/
976 static int igc_write_mc_addr_list(struct net_device *netdev)
977 {
978 	struct igc_adapter *adapter = netdev_priv(netdev);
979 	struct igc_hw *hw = &adapter->hw;
980 	struct netdev_hw_addr *ha;
981 	u8  *mta_list;
982 	int i;
983 
984 	if (netdev_mc_empty(netdev)) {
985 		/* nothing to program, so clear mc list */
986 		igc_update_mc_addr_list(hw, NULL, 0);
987 		return 0;
988 	}
989 
990 	mta_list = kcalloc(netdev_mc_count(netdev), 6, GFP_ATOMIC);
991 	if (!mta_list)
992 		return -ENOMEM;
993 
994 	/* The shared function expects a packed array of only addresses. */
995 	i = 0;
996 	netdev_for_each_mc_addr(ha, netdev)
997 		memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
998 
999 	igc_update_mc_addr_list(hw, mta_list, i);
1000 	kfree(mta_list);
1001 
1002 	return netdev_mc_count(netdev);
1003 }
1004 
1005 static __le32 igc_tx_launchtime(struct igc_ring *ring, ktime_t txtime,
1006 				bool *first_flag, bool *insert_empty)
1007 {
1008 	struct igc_adapter *adapter = netdev_priv(ring->netdev);
1009 	ktime_t cycle_time = adapter->cycle_time;
1010 	ktime_t base_time = adapter->base_time;
1011 	ktime_t now = ktime_get_clocktai();
1012 	ktime_t baset_est, end_of_cycle;
1013 	u32 launchtime;
1014 	s64 n;
1015 
1016 	n = div64_s64(ktime_sub_ns(now, base_time), cycle_time);
1017 
1018 	baset_est = ktime_add_ns(base_time, cycle_time * (n));
1019 	end_of_cycle = ktime_add_ns(baset_est, cycle_time);
1020 
1021 	if (ktime_compare(txtime, end_of_cycle) >= 0) {
1022 		if (baset_est != ring->last_ff_cycle) {
1023 			*first_flag = true;
1024 			ring->last_ff_cycle = baset_est;
1025 
1026 			if (ktime_compare(txtime, ring->last_tx_cycle) > 0)
1027 				*insert_empty = true;
1028 		}
1029 	}
1030 
1031 	/* Introducing a window at end of cycle on which packets
1032 	 * potentially not honor launchtime. Window of 5us chosen
1033 	 * considering software update the tail pointer and packets
1034 	 * are dma'ed to packet buffer.
1035 	 */
1036 	if ((ktime_sub_ns(end_of_cycle, now) < 5 * NSEC_PER_USEC))
1037 		netdev_warn(ring->netdev, "Packet with txtime=%llu may not be honoured\n",
1038 			    txtime);
1039 
1040 	ring->last_tx_cycle = end_of_cycle;
1041 
1042 	launchtime = ktime_sub_ns(txtime, baset_est);
1043 	if (launchtime > 0)
1044 		div_s64_rem(launchtime, cycle_time, &launchtime);
1045 	else
1046 		launchtime = 0;
1047 
1048 	return cpu_to_le32(launchtime);
1049 }
1050 
1051 static int igc_init_empty_frame(struct igc_ring *ring,
1052 				struct igc_tx_buffer *buffer,
1053 				struct sk_buff *skb)
1054 {
1055 	unsigned int size;
1056 	dma_addr_t dma;
1057 
1058 	size = skb_headlen(skb);
1059 
1060 	dma = dma_map_single(ring->dev, skb->data, size, DMA_TO_DEVICE);
1061 	if (dma_mapping_error(ring->dev, dma)) {
1062 		netdev_err_once(ring->netdev, "Failed to map DMA for TX\n");
1063 		return -ENOMEM;
1064 	}
1065 
1066 	buffer->skb = skb;
1067 	buffer->protocol = 0;
1068 	buffer->bytecount = skb->len;
1069 	buffer->gso_segs = 1;
1070 	buffer->time_stamp = jiffies;
1071 	dma_unmap_len_set(buffer, len, skb->len);
1072 	dma_unmap_addr_set(buffer, dma, dma);
1073 
1074 	return 0;
1075 }
1076 
1077 static int igc_init_tx_empty_descriptor(struct igc_ring *ring,
1078 					struct sk_buff *skb,
1079 					struct igc_tx_buffer *first)
1080 {
1081 	union igc_adv_tx_desc *desc;
1082 	u32 cmd_type, olinfo_status;
1083 	int err;
1084 
1085 	if (!igc_desc_unused(ring))
1086 		return -EBUSY;
1087 
1088 	err = igc_init_empty_frame(ring, first, skb);
1089 	if (err)
1090 		return err;
1091 
1092 	cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT |
1093 		   IGC_ADVTXD_DCMD_IFCS | IGC_TXD_DCMD |
1094 		   first->bytecount;
1095 	olinfo_status = first->bytecount << IGC_ADVTXD_PAYLEN_SHIFT;
1096 
1097 	desc = IGC_TX_DESC(ring, ring->next_to_use);
1098 	desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1099 	desc->read.olinfo_status = cpu_to_le32(olinfo_status);
1100 	desc->read.buffer_addr = cpu_to_le64(dma_unmap_addr(first, dma));
1101 
1102 	netdev_tx_sent_queue(txring_txq(ring), skb->len);
1103 
1104 	first->next_to_watch = desc;
1105 
1106 	ring->next_to_use++;
1107 	if (ring->next_to_use == ring->count)
1108 		ring->next_to_use = 0;
1109 
1110 	return 0;
1111 }
1112 
1113 #define IGC_EMPTY_FRAME_SIZE 60
1114 
1115 static void igc_tx_ctxtdesc(struct igc_ring *tx_ring,
1116 			    __le32 launch_time, bool first_flag,
1117 			    u32 vlan_macip_lens, u32 type_tucmd,
1118 			    u32 mss_l4len_idx)
1119 {
1120 	struct igc_adv_tx_context_desc *context_desc;
1121 	u16 i = tx_ring->next_to_use;
1122 
1123 	context_desc = IGC_TX_CTXTDESC(tx_ring, i);
1124 
1125 	i++;
1126 	tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
1127 
1128 	/* set bits to identify this as an advanced context descriptor */
1129 	type_tucmd |= IGC_TXD_CMD_DEXT | IGC_ADVTXD_DTYP_CTXT;
1130 
1131 	/* For i225, context index must be unique per ring. */
1132 	if (test_bit(IGC_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
1133 		mss_l4len_idx |= tx_ring->reg_idx << 4;
1134 
1135 	if (first_flag)
1136 		mss_l4len_idx |= IGC_ADVTXD_TSN_CNTX_FIRST;
1137 
1138 	context_desc->vlan_macip_lens	= cpu_to_le32(vlan_macip_lens);
1139 	context_desc->type_tucmd_mlhl	= cpu_to_le32(type_tucmd);
1140 	context_desc->mss_l4len_idx	= cpu_to_le32(mss_l4len_idx);
1141 	context_desc->launch_time	= launch_time;
1142 }
1143 
1144 static void igc_tx_csum(struct igc_ring *tx_ring, struct igc_tx_buffer *first,
1145 			__le32 launch_time, bool first_flag)
1146 {
1147 	struct sk_buff *skb = first->skb;
1148 	u32 vlan_macip_lens = 0;
1149 	u32 type_tucmd = 0;
1150 
1151 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
1152 csum_failed:
1153 		if (!(first->tx_flags & IGC_TX_FLAGS_VLAN) &&
1154 		    !tx_ring->launchtime_enable)
1155 			return;
1156 		goto no_csum;
1157 	}
1158 
1159 	switch (skb->csum_offset) {
1160 	case offsetof(struct tcphdr, check):
1161 		type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP;
1162 		fallthrough;
1163 	case offsetof(struct udphdr, check):
1164 		break;
1165 	case offsetof(struct sctphdr, checksum):
1166 		/* validate that this is actually an SCTP request */
1167 		if (skb_csum_is_sctp(skb)) {
1168 			type_tucmd = IGC_ADVTXD_TUCMD_L4T_SCTP;
1169 			break;
1170 		}
1171 		fallthrough;
1172 	default:
1173 		skb_checksum_help(skb);
1174 		goto csum_failed;
1175 	}
1176 
1177 	/* update TX checksum flag */
1178 	first->tx_flags |= IGC_TX_FLAGS_CSUM;
1179 	vlan_macip_lens = skb_checksum_start_offset(skb) -
1180 			  skb_network_offset(skb);
1181 no_csum:
1182 	vlan_macip_lens |= skb_network_offset(skb) << IGC_ADVTXD_MACLEN_SHIFT;
1183 	vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK;
1184 
1185 	igc_tx_ctxtdesc(tx_ring, launch_time, first_flag,
1186 			vlan_macip_lens, type_tucmd, 0);
1187 }
1188 
1189 static int __igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
1190 {
1191 	struct net_device *netdev = tx_ring->netdev;
1192 
1193 	netif_stop_subqueue(netdev, tx_ring->queue_index);
1194 
1195 	/* memory barriier comment */
1196 	smp_mb();
1197 
1198 	/* We need to check again in a case another CPU has just
1199 	 * made room available.
1200 	 */
1201 	if (igc_desc_unused(tx_ring) < size)
1202 		return -EBUSY;
1203 
1204 	/* A reprieve! */
1205 	netif_wake_subqueue(netdev, tx_ring->queue_index);
1206 
1207 	u64_stats_update_begin(&tx_ring->tx_syncp2);
1208 	tx_ring->tx_stats.restart_queue2++;
1209 	u64_stats_update_end(&tx_ring->tx_syncp2);
1210 
1211 	return 0;
1212 }
1213 
1214 static inline int igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
1215 {
1216 	if (igc_desc_unused(tx_ring) >= size)
1217 		return 0;
1218 	return __igc_maybe_stop_tx(tx_ring, size);
1219 }
1220 
1221 #define IGC_SET_FLAG(_input, _flag, _result) \
1222 	(((_flag) <= (_result)) ?				\
1223 	 ((u32)((_input) & (_flag)) * ((_result) / (_flag))) :	\
1224 	 ((u32)((_input) & (_flag)) / ((_flag) / (_result))))
1225 
1226 static u32 igc_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
1227 {
1228 	/* set type for advanced descriptor with frame checksum insertion */
1229 	u32 cmd_type = IGC_ADVTXD_DTYP_DATA |
1230 		       IGC_ADVTXD_DCMD_DEXT |
1231 		       IGC_ADVTXD_DCMD_IFCS;
1232 
1233 	/* set HW vlan bit if vlan is present */
1234 	cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_VLAN,
1235 				 IGC_ADVTXD_DCMD_VLE);
1236 
1237 	/* set segmentation bits for TSO */
1238 	cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSO,
1239 				 (IGC_ADVTXD_DCMD_TSE));
1240 
1241 	/* set timestamp bit if present */
1242 	cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSTAMP,
1243 				 (IGC_ADVTXD_MAC_TSTAMP));
1244 
1245 	/* insert frame checksum */
1246 	cmd_type ^= IGC_SET_FLAG(skb->no_fcs, 1, IGC_ADVTXD_DCMD_IFCS);
1247 
1248 	return cmd_type;
1249 }
1250 
1251 static void igc_tx_olinfo_status(struct igc_ring *tx_ring,
1252 				 union igc_adv_tx_desc *tx_desc,
1253 				 u32 tx_flags, unsigned int paylen)
1254 {
1255 	u32 olinfo_status = paylen << IGC_ADVTXD_PAYLEN_SHIFT;
1256 
1257 	/* insert L4 checksum */
1258 	olinfo_status |= (tx_flags & IGC_TX_FLAGS_CSUM) *
1259 			  ((IGC_TXD_POPTS_TXSM << 8) /
1260 			  IGC_TX_FLAGS_CSUM);
1261 
1262 	/* insert IPv4 checksum */
1263 	olinfo_status |= (tx_flags & IGC_TX_FLAGS_IPV4) *
1264 			  (((IGC_TXD_POPTS_IXSM << 8)) /
1265 			  IGC_TX_FLAGS_IPV4);
1266 
1267 	tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
1268 }
1269 
1270 static int igc_tx_map(struct igc_ring *tx_ring,
1271 		      struct igc_tx_buffer *first,
1272 		      const u8 hdr_len)
1273 {
1274 	struct sk_buff *skb = first->skb;
1275 	struct igc_tx_buffer *tx_buffer;
1276 	union igc_adv_tx_desc *tx_desc;
1277 	u32 tx_flags = first->tx_flags;
1278 	skb_frag_t *frag;
1279 	u16 i = tx_ring->next_to_use;
1280 	unsigned int data_len, size;
1281 	dma_addr_t dma;
1282 	u32 cmd_type;
1283 
1284 	cmd_type = igc_tx_cmd_type(skb, tx_flags);
1285 	tx_desc = IGC_TX_DESC(tx_ring, i);
1286 
1287 	igc_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
1288 
1289 	size = skb_headlen(skb);
1290 	data_len = skb->data_len;
1291 
1292 	dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1293 
1294 	tx_buffer = first;
1295 
1296 	for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1297 		if (dma_mapping_error(tx_ring->dev, dma))
1298 			goto dma_error;
1299 
1300 		/* record length, and DMA address */
1301 		dma_unmap_len_set(tx_buffer, len, size);
1302 		dma_unmap_addr_set(tx_buffer, dma, dma);
1303 
1304 		tx_desc->read.buffer_addr = cpu_to_le64(dma);
1305 
1306 		while (unlikely(size > IGC_MAX_DATA_PER_TXD)) {
1307 			tx_desc->read.cmd_type_len =
1308 				cpu_to_le32(cmd_type ^ IGC_MAX_DATA_PER_TXD);
1309 
1310 			i++;
1311 			tx_desc++;
1312 			if (i == tx_ring->count) {
1313 				tx_desc = IGC_TX_DESC(tx_ring, 0);
1314 				i = 0;
1315 			}
1316 			tx_desc->read.olinfo_status = 0;
1317 
1318 			dma += IGC_MAX_DATA_PER_TXD;
1319 			size -= IGC_MAX_DATA_PER_TXD;
1320 
1321 			tx_desc->read.buffer_addr = cpu_to_le64(dma);
1322 		}
1323 
1324 		if (likely(!data_len))
1325 			break;
1326 
1327 		tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
1328 
1329 		i++;
1330 		tx_desc++;
1331 		if (i == tx_ring->count) {
1332 			tx_desc = IGC_TX_DESC(tx_ring, 0);
1333 			i = 0;
1334 		}
1335 		tx_desc->read.olinfo_status = 0;
1336 
1337 		size = skb_frag_size(frag);
1338 		data_len -= size;
1339 
1340 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
1341 				       size, DMA_TO_DEVICE);
1342 
1343 		tx_buffer = &tx_ring->tx_buffer_info[i];
1344 	}
1345 
1346 	/* write last descriptor with RS and EOP bits */
1347 	cmd_type |= size | IGC_TXD_DCMD;
1348 	tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1349 
1350 	netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
1351 
1352 	/* set the timestamp */
1353 	first->time_stamp = jiffies;
1354 
1355 	skb_tx_timestamp(skb);
1356 
1357 	/* Force memory writes to complete before letting h/w know there
1358 	 * are new descriptors to fetch.  (Only applicable for weak-ordered
1359 	 * memory model archs, such as IA-64).
1360 	 *
1361 	 * We also need this memory barrier to make certain all of the
1362 	 * status bits have been updated before next_to_watch is written.
1363 	 */
1364 	wmb();
1365 
1366 	/* set next_to_watch value indicating a packet is present */
1367 	first->next_to_watch = tx_desc;
1368 
1369 	i++;
1370 	if (i == tx_ring->count)
1371 		i = 0;
1372 
1373 	tx_ring->next_to_use = i;
1374 
1375 	/* Make sure there is space in the ring for the next send. */
1376 	igc_maybe_stop_tx(tx_ring, DESC_NEEDED);
1377 
1378 	if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) {
1379 		writel(i, tx_ring->tail);
1380 	}
1381 
1382 	return 0;
1383 dma_error:
1384 	netdev_err(tx_ring->netdev, "TX DMA map failed\n");
1385 	tx_buffer = &tx_ring->tx_buffer_info[i];
1386 
1387 	/* clear dma mappings for failed tx_buffer_info map */
1388 	while (tx_buffer != first) {
1389 		if (dma_unmap_len(tx_buffer, len))
1390 			igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
1391 
1392 		if (i-- == 0)
1393 			i += tx_ring->count;
1394 		tx_buffer = &tx_ring->tx_buffer_info[i];
1395 	}
1396 
1397 	if (dma_unmap_len(tx_buffer, len))
1398 		igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
1399 
1400 	dev_kfree_skb_any(tx_buffer->skb);
1401 	tx_buffer->skb = NULL;
1402 
1403 	tx_ring->next_to_use = i;
1404 
1405 	return -1;
1406 }
1407 
1408 static int igc_tso(struct igc_ring *tx_ring,
1409 		   struct igc_tx_buffer *first,
1410 		   __le32 launch_time, bool first_flag,
1411 		   u8 *hdr_len)
1412 {
1413 	u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
1414 	struct sk_buff *skb = first->skb;
1415 	union {
1416 		struct iphdr *v4;
1417 		struct ipv6hdr *v6;
1418 		unsigned char *hdr;
1419 	} ip;
1420 	union {
1421 		struct tcphdr *tcp;
1422 		struct udphdr *udp;
1423 		unsigned char *hdr;
1424 	} l4;
1425 	u32 paylen, l4_offset;
1426 	int err;
1427 
1428 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1429 		return 0;
1430 
1431 	if (!skb_is_gso(skb))
1432 		return 0;
1433 
1434 	err = skb_cow_head(skb, 0);
1435 	if (err < 0)
1436 		return err;
1437 
1438 	ip.hdr = skb_network_header(skb);
1439 	l4.hdr = skb_checksum_start(skb);
1440 
1441 	/* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
1442 	type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP;
1443 
1444 	/* initialize outer IP header fields */
1445 	if (ip.v4->version == 4) {
1446 		unsigned char *csum_start = skb_checksum_start(skb);
1447 		unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
1448 
1449 		/* IP header will have to cancel out any data that
1450 		 * is not a part of the outer IP header
1451 		 */
1452 		ip.v4->check = csum_fold(csum_partial(trans_start,
1453 						      csum_start - trans_start,
1454 						      0));
1455 		type_tucmd |= IGC_ADVTXD_TUCMD_IPV4;
1456 
1457 		ip.v4->tot_len = 0;
1458 		first->tx_flags |= IGC_TX_FLAGS_TSO |
1459 				   IGC_TX_FLAGS_CSUM |
1460 				   IGC_TX_FLAGS_IPV4;
1461 	} else {
1462 		ip.v6->payload_len = 0;
1463 		first->tx_flags |= IGC_TX_FLAGS_TSO |
1464 				   IGC_TX_FLAGS_CSUM;
1465 	}
1466 
1467 	/* determine offset of inner transport header */
1468 	l4_offset = l4.hdr - skb->data;
1469 
1470 	/* remove payload length from inner checksum */
1471 	paylen = skb->len - l4_offset;
1472 	if (type_tucmd & IGC_ADVTXD_TUCMD_L4T_TCP) {
1473 		/* compute length of segmentation header */
1474 		*hdr_len = (l4.tcp->doff * 4) + l4_offset;
1475 		csum_replace_by_diff(&l4.tcp->check,
1476 				     (__force __wsum)htonl(paylen));
1477 	} else {
1478 		/* compute length of segmentation header */
1479 		*hdr_len = sizeof(*l4.udp) + l4_offset;
1480 		csum_replace_by_diff(&l4.udp->check,
1481 				     (__force __wsum)htonl(paylen));
1482 	}
1483 
1484 	/* update gso size and bytecount with header size */
1485 	first->gso_segs = skb_shinfo(skb)->gso_segs;
1486 	first->bytecount += (first->gso_segs - 1) * *hdr_len;
1487 
1488 	/* MSS L4LEN IDX */
1489 	mss_l4len_idx = (*hdr_len - l4_offset) << IGC_ADVTXD_L4LEN_SHIFT;
1490 	mss_l4len_idx |= skb_shinfo(skb)->gso_size << IGC_ADVTXD_MSS_SHIFT;
1491 
1492 	/* VLAN MACLEN IPLEN */
1493 	vlan_macip_lens = l4.hdr - ip.hdr;
1494 	vlan_macip_lens |= (ip.hdr - skb->data) << IGC_ADVTXD_MACLEN_SHIFT;
1495 	vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK;
1496 
1497 	igc_tx_ctxtdesc(tx_ring, launch_time, first_flag,
1498 			vlan_macip_lens, type_tucmd, mss_l4len_idx);
1499 
1500 	return 1;
1501 }
1502 
1503 static netdev_tx_t igc_xmit_frame_ring(struct sk_buff *skb,
1504 				       struct igc_ring *tx_ring)
1505 {
1506 	struct igc_adapter *adapter = netdev_priv(tx_ring->netdev);
1507 	bool first_flag = false, insert_empty = false;
1508 	u16 count = TXD_USE_COUNT(skb_headlen(skb));
1509 	__be16 protocol = vlan_get_protocol(skb);
1510 	struct igc_tx_buffer *first;
1511 	__le32 launch_time = 0;
1512 	u32 tx_flags = 0;
1513 	unsigned short f;
1514 	ktime_t txtime;
1515 	u8 hdr_len = 0;
1516 	int tso = 0;
1517 
1518 	/* need: 1 descriptor per page * PAGE_SIZE/IGC_MAX_DATA_PER_TXD,
1519 	 *	+ 1 desc for skb_headlen/IGC_MAX_DATA_PER_TXD,
1520 	 *	+ 2 desc gap to keep tail from touching head,
1521 	 *	+ 1 desc for context descriptor,
1522 	 * otherwise try next time
1523 	 */
1524 	for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1525 		count += TXD_USE_COUNT(skb_frag_size(
1526 						&skb_shinfo(skb)->frags[f]));
1527 
1528 	if (igc_maybe_stop_tx(tx_ring, count + 5)) {
1529 		/* this is a hard error */
1530 		return NETDEV_TX_BUSY;
1531 	}
1532 
1533 	if (!tx_ring->launchtime_enable)
1534 		goto done;
1535 
1536 	txtime = skb->tstamp;
1537 	skb->tstamp = ktime_set(0, 0);
1538 	launch_time = igc_tx_launchtime(tx_ring, txtime, &first_flag, &insert_empty);
1539 
1540 	if (insert_empty) {
1541 		struct igc_tx_buffer *empty_info;
1542 		struct sk_buff *empty;
1543 		void *data;
1544 
1545 		empty_info = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
1546 		empty = alloc_skb(IGC_EMPTY_FRAME_SIZE, GFP_ATOMIC);
1547 		if (!empty)
1548 			goto done;
1549 
1550 		data = skb_put(empty, IGC_EMPTY_FRAME_SIZE);
1551 		memset(data, 0, IGC_EMPTY_FRAME_SIZE);
1552 
1553 		igc_tx_ctxtdesc(tx_ring, 0, false, 0, 0, 0);
1554 
1555 		if (igc_init_tx_empty_descriptor(tx_ring,
1556 						 empty,
1557 						 empty_info) < 0)
1558 			dev_kfree_skb_any(empty);
1559 	}
1560 
1561 done:
1562 	/* record the location of the first descriptor for this packet */
1563 	first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
1564 	first->type = IGC_TX_BUFFER_TYPE_SKB;
1565 	first->skb = skb;
1566 	first->bytecount = skb->len;
1567 	first->gso_segs = 1;
1568 
1569 	if (tx_ring->max_sdu > 0) {
1570 		u32 max_sdu = 0;
1571 
1572 		max_sdu = tx_ring->max_sdu +
1573 			  (skb_vlan_tagged(first->skb) ? VLAN_HLEN : 0);
1574 
1575 		if (first->bytecount > max_sdu) {
1576 			adapter->stats.txdrop++;
1577 			goto out_drop;
1578 		}
1579 	}
1580 
1581 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
1582 		/* FIXME: add support for retrieving timestamps from
1583 		 * the other timer registers before skipping the
1584 		 * timestamping request.
1585 		 */
1586 		if (adapter->tstamp_config.tx_type == HWTSTAMP_TX_ON &&
1587 		    !test_and_set_bit_lock(__IGC_PTP_TX_IN_PROGRESS,
1588 					   &adapter->state)) {
1589 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1590 			tx_flags |= IGC_TX_FLAGS_TSTAMP;
1591 
1592 			adapter->ptp_tx_skb = skb_get(skb);
1593 			adapter->ptp_tx_start = jiffies;
1594 		} else {
1595 			adapter->tx_hwtstamp_skipped++;
1596 		}
1597 	}
1598 
1599 	if (skb_vlan_tag_present(skb)) {
1600 		tx_flags |= IGC_TX_FLAGS_VLAN;
1601 		tx_flags |= (skb_vlan_tag_get(skb) << IGC_TX_FLAGS_VLAN_SHIFT);
1602 	}
1603 
1604 	/* record initial flags and protocol */
1605 	first->tx_flags = tx_flags;
1606 	first->protocol = protocol;
1607 
1608 	tso = igc_tso(tx_ring, first, launch_time, first_flag, &hdr_len);
1609 	if (tso < 0)
1610 		goto out_drop;
1611 	else if (!tso)
1612 		igc_tx_csum(tx_ring, first, launch_time, first_flag);
1613 
1614 	igc_tx_map(tx_ring, first, hdr_len);
1615 
1616 	return NETDEV_TX_OK;
1617 
1618 out_drop:
1619 	dev_kfree_skb_any(first->skb);
1620 	first->skb = NULL;
1621 
1622 	return NETDEV_TX_OK;
1623 }
1624 
1625 static inline struct igc_ring *igc_tx_queue_mapping(struct igc_adapter *adapter,
1626 						    struct sk_buff *skb)
1627 {
1628 	unsigned int r_idx = skb->queue_mapping;
1629 
1630 	if (r_idx >= adapter->num_tx_queues)
1631 		r_idx = r_idx % adapter->num_tx_queues;
1632 
1633 	return adapter->tx_ring[r_idx];
1634 }
1635 
1636 static netdev_tx_t igc_xmit_frame(struct sk_buff *skb,
1637 				  struct net_device *netdev)
1638 {
1639 	struct igc_adapter *adapter = netdev_priv(netdev);
1640 
1641 	/* The minimum packet size with TCTL.PSP set is 17 so pad the skb
1642 	 * in order to meet this minimum size requirement.
1643 	 */
1644 	if (skb->len < 17) {
1645 		if (skb_padto(skb, 17))
1646 			return NETDEV_TX_OK;
1647 		skb->len = 17;
1648 	}
1649 
1650 	return igc_xmit_frame_ring(skb, igc_tx_queue_mapping(adapter, skb));
1651 }
1652 
1653 static void igc_rx_checksum(struct igc_ring *ring,
1654 			    union igc_adv_rx_desc *rx_desc,
1655 			    struct sk_buff *skb)
1656 {
1657 	skb_checksum_none_assert(skb);
1658 
1659 	/* Ignore Checksum bit is set */
1660 	if (igc_test_staterr(rx_desc, IGC_RXD_STAT_IXSM))
1661 		return;
1662 
1663 	/* Rx checksum disabled via ethtool */
1664 	if (!(ring->netdev->features & NETIF_F_RXCSUM))
1665 		return;
1666 
1667 	/* TCP/UDP checksum error bit is set */
1668 	if (igc_test_staterr(rx_desc,
1669 			     IGC_RXDEXT_STATERR_L4E |
1670 			     IGC_RXDEXT_STATERR_IPE)) {
1671 		/* work around errata with sctp packets where the TCPE aka
1672 		 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
1673 		 * packets (aka let the stack check the crc32c)
1674 		 */
1675 		if (!(skb->len == 60 &&
1676 		      test_bit(IGC_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
1677 			u64_stats_update_begin(&ring->rx_syncp);
1678 			ring->rx_stats.csum_err++;
1679 			u64_stats_update_end(&ring->rx_syncp);
1680 		}
1681 		/* let the stack verify checksum errors */
1682 		return;
1683 	}
1684 	/* It must be a TCP or UDP packet with a valid checksum */
1685 	if (igc_test_staterr(rx_desc, IGC_RXD_STAT_TCPCS |
1686 				      IGC_RXD_STAT_UDPCS))
1687 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1688 
1689 	netdev_dbg(ring->netdev, "cksum success: bits %08X\n",
1690 		   le32_to_cpu(rx_desc->wb.upper.status_error));
1691 }
1692 
1693 static inline void igc_rx_hash(struct igc_ring *ring,
1694 			       union igc_adv_rx_desc *rx_desc,
1695 			       struct sk_buff *skb)
1696 {
1697 	if (ring->netdev->features & NETIF_F_RXHASH)
1698 		skb_set_hash(skb,
1699 			     le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
1700 			     PKT_HASH_TYPE_L3);
1701 }
1702 
1703 static void igc_rx_vlan(struct igc_ring *rx_ring,
1704 			union igc_adv_rx_desc *rx_desc,
1705 			struct sk_buff *skb)
1706 {
1707 	struct net_device *dev = rx_ring->netdev;
1708 	u16 vid;
1709 
1710 	if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1711 	    igc_test_staterr(rx_desc, IGC_RXD_STAT_VP)) {
1712 		if (igc_test_staterr(rx_desc, IGC_RXDEXT_STATERR_LB) &&
1713 		    test_bit(IGC_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
1714 			vid = be16_to_cpu((__force __be16)rx_desc->wb.upper.vlan);
1715 		else
1716 			vid = le16_to_cpu(rx_desc->wb.upper.vlan);
1717 
1718 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1719 	}
1720 }
1721 
1722 /**
1723  * igc_process_skb_fields - Populate skb header fields from Rx descriptor
1724  * @rx_ring: rx descriptor ring packet is being transacted on
1725  * @rx_desc: pointer to the EOP Rx descriptor
1726  * @skb: pointer to current skb being populated
1727  *
1728  * This function checks the ring, descriptor, and packet information in order
1729  * to populate the hash, checksum, VLAN, protocol, and other fields within the
1730  * skb.
1731  */
1732 static void igc_process_skb_fields(struct igc_ring *rx_ring,
1733 				   union igc_adv_rx_desc *rx_desc,
1734 				   struct sk_buff *skb)
1735 {
1736 	igc_rx_hash(rx_ring, rx_desc, skb);
1737 
1738 	igc_rx_checksum(rx_ring, rx_desc, skb);
1739 
1740 	igc_rx_vlan(rx_ring, rx_desc, skb);
1741 
1742 	skb_record_rx_queue(skb, rx_ring->queue_index);
1743 
1744 	skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1745 }
1746 
1747 static void igc_vlan_mode(struct net_device *netdev, netdev_features_t features)
1748 {
1749 	bool enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
1750 	struct igc_adapter *adapter = netdev_priv(netdev);
1751 	struct igc_hw *hw = &adapter->hw;
1752 	u32 ctrl;
1753 
1754 	ctrl = rd32(IGC_CTRL);
1755 
1756 	if (enable) {
1757 		/* enable VLAN tag insert/strip */
1758 		ctrl |= IGC_CTRL_VME;
1759 	} else {
1760 		/* disable VLAN tag insert/strip */
1761 		ctrl &= ~IGC_CTRL_VME;
1762 	}
1763 	wr32(IGC_CTRL, ctrl);
1764 }
1765 
1766 static void igc_restore_vlan(struct igc_adapter *adapter)
1767 {
1768 	igc_vlan_mode(adapter->netdev, adapter->netdev->features);
1769 }
1770 
1771 static struct igc_rx_buffer *igc_get_rx_buffer(struct igc_ring *rx_ring,
1772 					       const unsigned int size,
1773 					       int *rx_buffer_pgcnt)
1774 {
1775 	struct igc_rx_buffer *rx_buffer;
1776 
1777 	rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
1778 	*rx_buffer_pgcnt =
1779 #if (PAGE_SIZE < 8192)
1780 		page_count(rx_buffer->page);
1781 #else
1782 		0;
1783 #endif
1784 	prefetchw(rx_buffer->page);
1785 
1786 	/* we are reusing so sync this buffer for CPU use */
1787 	dma_sync_single_range_for_cpu(rx_ring->dev,
1788 				      rx_buffer->dma,
1789 				      rx_buffer->page_offset,
1790 				      size,
1791 				      DMA_FROM_DEVICE);
1792 
1793 	rx_buffer->pagecnt_bias--;
1794 
1795 	return rx_buffer;
1796 }
1797 
1798 static void igc_rx_buffer_flip(struct igc_rx_buffer *buffer,
1799 			       unsigned int truesize)
1800 {
1801 #if (PAGE_SIZE < 8192)
1802 	buffer->page_offset ^= truesize;
1803 #else
1804 	buffer->page_offset += truesize;
1805 #endif
1806 }
1807 
1808 static unsigned int igc_get_rx_frame_truesize(struct igc_ring *ring,
1809 					      unsigned int size)
1810 {
1811 	unsigned int truesize;
1812 
1813 #if (PAGE_SIZE < 8192)
1814 	truesize = igc_rx_pg_size(ring) / 2;
1815 #else
1816 	truesize = ring_uses_build_skb(ring) ?
1817 		   SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1818 		   SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1819 		   SKB_DATA_ALIGN(size);
1820 #endif
1821 	return truesize;
1822 }
1823 
1824 /**
1825  * igc_add_rx_frag - Add contents of Rx buffer to sk_buff
1826  * @rx_ring: rx descriptor ring to transact packets on
1827  * @rx_buffer: buffer containing page to add
1828  * @skb: sk_buff to place the data into
1829  * @size: size of buffer to be added
1830  *
1831  * This function will add the data contained in rx_buffer->page to the skb.
1832  */
1833 static void igc_add_rx_frag(struct igc_ring *rx_ring,
1834 			    struct igc_rx_buffer *rx_buffer,
1835 			    struct sk_buff *skb,
1836 			    unsigned int size)
1837 {
1838 	unsigned int truesize;
1839 
1840 #if (PAGE_SIZE < 8192)
1841 	truesize = igc_rx_pg_size(rx_ring) / 2;
1842 #else
1843 	truesize = ring_uses_build_skb(rx_ring) ?
1844 		   SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1845 		   SKB_DATA_ALIGN(size);
1846 #endif
1847 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1848 			rx_buffer->page_offset, size, truesize);
1849 
1850 	igc_rx_buffer_flip(rx_buffer, truesize);
1851 }
1852 
1853 static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring,
1854 				     struct igc_rx_buffer *rx_buffer,
1855 				     struct xdp_buff *xdp)
1856 {
1857 	unsigned int size = xdp->data_end - xdp->data;
1858 	unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size);
1859 	unsigned int metasize = xdp->data - xdp->data_meta;
1860 	struct sk_buff *skb;
1861 
1862 	/* prefetch first cache line of first page */
1863 	net_prefetch(xdp->data_meta);
1864 
1865 	/* build an skb around the page buffer */
1866 	skb = napi_build_skb(xdp->data_hard_start, truesize);
1867 	if (unlikely(!skb))
1868 		return NULL;
1869 
1870 	/* update pointers within the skb to store the data */
1871 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
1872 	__skb_put(skb, size);
1873 	if (metasize)
1874 		skb_metadata_set(skb, metasize);
1875 
1876 	igc_rx_buffer_flip(rx_buffer, truesize);
1877 	return skb;
1878 }
1879 
1880 static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
1881 					 struct igc_rx_buffer *rx_buffer,
1882 					 struct xdp_buff *xdp,
1883 					 ktime_t timestamp)
1884 {
1885 	unsigned int metasize = xdp->data - xdp->data_meta;
1886 	unsigned int size = xdp->data_end - xdp->data;
1887 	unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size);
1888 	void *va = xdp->data;
1889 	unsigned int headlen;
1890 	struct sk_buff *skb;
1891 
1892 	/* prefetch first cache line of first page */
1893 	net_prefetch(xdp->data_meta);
1894 
1895 	/* allocate a skb to store the frags */
1896 	skb = napi_alloc_skb(&rx_ring->q_vector->napi,
1897 			     IGC_RX_HDR_LEN + metasize);
1898 	if (unlikely(!skb))
1899 		return NULL;
1900 
1901 	if (timestamp)
1902 		skb_hwtstamps(skb)->hwtstamp = timestamp;
1903 
1904 	/* Determine available headroom for copy */
1905 	headlen = size;
1906 	if (headlen > IGC_RX_HDR_LEN)
1907 		headlen = eth_get_headlen(skb->dev, va, IGC_RX_HDR_LEN);
1908 
1909 	/* align pull length to size of long to optimize memcpy performance */
1910 	memcpy(__skb_put(skb, headlen + metasize), xdp->data_meta,
1911 	       ALIGN(headlen + metasize, sizeof(long)));
1912 
1913 	if (metasize) {
1914 		skb_metadata_set(skb, metasize);
1915 		__skb_pull(skb, metasize);
1916 	}
1917 
1918 	/* update all of the pointers */
1919 	size -= headlen;
1920 	if (size) {
1921 		skb_add_rx_frag(skb, 0, rx_buffer->page,
1922 				(va + headlen) - page_address(rx_buffer->page),
1923 				size, truesize);
1924 		igc_rx_buffer_flip(rx_buffer, truesize);
1925 	} else {
1926 		rx_buffer->pagecnt_bias++;
1927 	}
1928 
1929 	return skb;
1930 }
1931 
1932 /**
1933  * igc_reuse_rx_page - page flip buffer and store it back on the ring
1934  * @rx_ring: rx descriptor ring to store buffers on
1935  * @old_buff: donor buffer to have page reused
1936  *
1937  * Synchronizes page for reuse by the adapter
1938  */
1939 static void igc_reuse_rx_page(struct igc_ring *rx_ring,
1940 			      struct igc_rx_buffer *old_buff)
1941 {
1942 	u16 nta = rx_ring->next_to_alloc;
1943 	struct igc_rx_buffer *new_buff;
1944 
1945 	new_buff = &rx_ring->rx_buffer_info[nta];
1946 
1947 	/* update, and store next to alloc */
1948 	nta++;
1949 	rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1950 
1951 	/* Transfer page from old buffer to new buffer.
1952 	 * Move each member individually to avoid possible store
1953 	 * forwarding stalls.
1954 	 */
1955 	new_buff->dma		= old_buff->dma;
1956 	new_buff->page		= old_buff->page;
1957 	new_buff->page_offset	= old_buff->page_offset;
1958 	new_buff->pagecnt_bias	= old_buff->pagecnt_bias;
1959 }
1960 
1961 static bool igc_can_reuse_rx_page(struct igc_rx_buffer *rx_buffer,
1962 				  int rx_buffer_pgcnt)
1963 {
1964 	unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1965 	struct page *page = rx_buffer->page;
1966 
1967 	/* avoid re-using remote and pfmemalloc pages */
1968 	if (!dev_page_is_reusable(page))
1969 		return false;
1970 
1971 #if (PAGE_SIZE < 8192)
1972 	/* if we are only owner of page we can reuse it */
1973 	if (unlikely((rx_buffer_pgcnt - pagecnt_bias) > 1))
1974 		return false;
1975 #else
1976 #define IGC_LAST_OFFSET \
1977 	(SKB_WITH_OVERHEAD(PAGE_SIZE) - IGC_RXBUFFER_2048)
1978 
1979 	if (rx_buffer->page_offset > IGC_LAST_OFFSET)
1980 		return false;
1981 #endif
1982 
1983 	/* If we have drained the page fragment pool we need to update
1984 	 * the pagecnt_bias and page count so that we fully restock the
1985 	 * number of references the driver holds.
1986 	 */
1987 	if (unlikely(pagecnt_bias == 1)) {
1988 		page_ref_add(page, USHRT_MAX - 1);
1989 		rx_buffer->pagecnt_bias = USHRT_MAX;
1990 	}
1991 
1992 	return true;
1993 }
1994 
1995 /**
1996  * igc_is_non_eop - process handling of non-EOP buffers
1997  * @rx_ring: Rx ring being processed
1998  * @rx_desc: Rx descriptor for current buffer
1999  *
2000  * This function updates next to clean.  If the buffer is an EOP buffer
2001  * this function exits returning false, otherwise it will place the
2002  * sk_buff in the next buffer to be chained and return true indicating
2003  * that this is in fact a non-EOP buffer.
2004  */
2005 static bool igc_is_non_eop(struct igc_ring *rx_ring,
2006 			   union igc_adv_rx_desc *rx_desc)
2007 {
2008 	u32 ntc = rx_ring->next_to_clean + 1;
2009 
2010 	/* fetch, update, and store next to clean */
2011 	ntc = (ntc < rx_ring->count) ? ntc : 0;
2012 	rx_ring->next_to_clean = ntc;
2013 
2014 	prefetch(IGC_RX_DESC(rx_ring, ntc));
2015 
2016 	if (likely(igc_test_staterr(rx_desc, IGC_RXD_STAT_EOP)))
2017 		return false;
2018 
2019 	return true;
2020 }
2021 
2022 /**
2023  * igc_cleanup_headers - Correct corrupted or empty headers
2024  * @rx_ring: rx descriptor ring packet is being transacted on
2025  * @rx_desc: pointer to the EOP Rx descriptor
2026  * @skb: pointer to current skb being fixed
2027  *
2028  * Address the case where we are pulling data in on pages only
2029  * and as such no data is present in the skb header.
2030  *
2031  * In addition if skb is not at least 60 bytes we need to pad it so that
2032  * it is large enough to qualify as a valid Ethernet frame.
2033  *
2034  * Returns true if an error was encountered and skb was freed.
2035  */
2036 static bool igc_cleanup_headers(struct igc_ring *rx_ring,
2037 				union igc_adv_rx_desc *rx_desc,
2038 				struct sk_buff *skb)
2039 {
2040 	/* XDP packets use error pointer so abort at this point */
2041 	if (IS_ERR(skb))
2042 		return true;
2043 
2044 	if (unlikely(igc_test_staterr(rx_desc, IGC_RXDEXT_STATERR_RXE))) {
2045 		struct net_device *netdev = rx_ring->netdev;
2046 
2047 		if (!(netdev->features & NETIF_F_RXALL)) {
2048 			dev_kfree_skb_any(skb);
2049 			return true;
2050 		}
2051 	}
2052 
2053 	/* if eth_skb_pad returns an error the skb was freed */
2054 	if (eth_skb_pad(skb))
2055 		return true;
2056 
2057 	return false;
2058 }
2059 
2060 static void igc_put_rx_buffer(struct igc_ring *rx_ring,
2061 			      struct igc_rx_buffer *rx_buffer,
2062 			      int rx_buffer_pgcnt)
2063 {
2064 	if (igc_can_reuse_rx_page(rx_buffer, rx_buffer_pgcnt)) {
2065 		/* hand second half of page back to the ring */
2066 		igc_reuse_rx_page(rx_ring, rx_buffer);
2067 	} else {
2068 		/* We are not reusing the buffer so unmap it and free
2069 		 * any references we are holding to it
2070 		 */
2071 		dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
2072 				     igc_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
2073 				     IGC_RX_DMA_ATTR);
2074 		__page_frag_cache_drain(rx_buffer->page,
2075 					rx_buffer->pagecnt_bias);
2076 	}
2077 
2078 	/* clear contents of rx_buffer */
2079 	rx_buffer->page = NULL;
2080 }
2081 
2082 static inline unsigned int igc_rx_offset(struct igc_ring *rx_ring)
2083 {
2084 	struct igc_adapter *adapter = rx_ring->q_vector->adapter;
2085 
2086 	if (ring_uses_build_skb(rx_ring))
2087 		return IGC_SKB_PAD;
2088 	if (igc_xdp_is_enabled(adapter))
2089 		return XDP_PACKET_HEADROOM;
2090 
2091 	return 0;
2092 }
2093 
2094 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
2095 				  struct igc_rx_buffer *bi)
2096 {
2097 	struct page *page = bi->page;
2098 	dma_addr_t dma;
2099 
2100 	/* since we are recycling buffers we should seldom need to alloc */
2101 	if (likely(page))
2102 		return true;
2103 
2104 	/* alloc new page for storage */
2105 	page = dev_alloc_pages(igc_rx_pg_order(rx_ring));
2106 	if (unlikely(!page)) {
2107 		rx_ring->rx_stats.alloc_failed++;
2108 		return false;
2109 	}
2110 
2111 	/* map page for use */
2112 	dma = dma_map_page_attrs(rx_ring->dev, page, 0,
2113 				 igc_rx_pg_size(rx_ring),
2114 				 DMA_FROM_DEVICE,
2115 				 IGC_RX_DMA_ATTR);
2116 
2117 	/* if mapping failed free memory back to system since
2118 	 * there isn't much point in holding memory we can't use
2119 	 */
2120 	if (dma_mapping_error(rx_ring->dev, dma)) {
2121 		__free_page(page);
2122 
2123 		rx_ring->rx_stats.alloc_failed++;
2124 		return false;
2125 	}
2126 
2127 	bi->dma = dma;
2128 	bi->page = page;
2129 	bi->page_offset = igc_rx_offset(rx_ring);
2130 	page_ref_add(page, USHRT_MAX - 1);
2131 	bi->pagecnt_bias = USHRT_MAX;
2132 
2133 	return true;
2134 }
2135 
2136 /**
2137  * igc_alloc_rx_buffers - Replace used receive buffers; packet split
2138  * @rx_ring: rx descriptor ring
2139  * @cleaned_count: number of buffers to clean
2140  */
2141 static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count)
2142 {
2143 	union igc_adv_rx_desc *rx_desc;
2144 	u16 i = rx_ring->next_to_use;
2145 	struct igc_rx_buffer *bi;
2146 	u16 bufsz;
2147 
2148 	/* nothing to do */
2149 	if (!cleaned_count)
2150 		return;
2151 
2152 	rx_desc = IGC_RX_DESC(rx_ring, i);
2153 	bi = &rx_ring->rx_buffer_info[i];
2154 	i -= rx_ring->count;
2155 
2156 	bufsz = igc_rx_bufsz(rx_ring);
2157 
2158 	do {
2159 		if (!igc_alloc_mapped_page(rx_ring, bi))
2160 			break;
2161 
2162 		/* sync the buffer for use by the device */
2163 		dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
2164 						 bi->page_offset, bufsz,
2165 						 DMA_FROM_DEVICE);
2166 
2167 		/* Refresh the desc even if buffer_addrs didn't change
2168 		 * because each write-back erases this info.
2169 		 */
2170 		rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
2171 
2172 		rx_desc++;
2173 		bi++;
2174 		i++;
2175 		if (unlikely(!i)) {
2176 			rx_desc = IGC_RX_DESC(rx_ring, 0);
2177 			bi = rx_ring->rx_buffer_info;
2178 			i -= rx_ring->count;
2179 		}
2180 
2181 		/* clear the length for the next_to_use descriptor */
2182 		rx_desc->wb.upper.length = 0;
2183 
2184 		cleaned_count--;
2185 	} while (cleaned_count);
2186 
2187 	i += rx_ring->count;
2188 
2189 	if (rx_ring->next_to_use != i) {
2190 		/* record the next descriptor to use */
2191 		rx_ring->next_to_use = i;
2192 
2193 		/* update next to alloc since we have filled the ring */
2194 		rx_ring->next_to_alloc = i;
2195 
2196 		/* Force memory writes to complete before letting h/w
2197 		 * know there are new descriptors to fetch.  (Only
2198 		 * applicable for weak-ordered memory model archs,
2199 		 * such as IA-64).
2200 		 */
2201 		wmb();
2202 		writel(i, rx_ring->tail);
2203 	}
2204 }
2205 
2206 static bool igc_alloc_rx_buffers_zc(struct igc_ring *ring, u16 count)
2207 {
2208 	union igc_adv_rx_desc *desc;
2209 	u16 i = ring->next_to_use;
2210 	struct igc_rx_buffer *bi;
2211 	dma_addr_t dma;
2212 	bool ok = true;
2213 
2214 	if (!count)
2215 		return ok;
2216 
2217 	desc = IGC_RX_DESC(ring, i);
2218 	bi = &ring->rx_buffer_info[i];
2219 	i -= ring->count;
2220 
2221 	do {
2222 		bi->xdp = xsk_buff_alloc(ring->xsk_pool);
2223 		if (!bi->xdp) {
2224 			ok = false;
2225 			break;
2226 		}
2227 
2228 		dma = xsk_buff_xdp_get_dma(bi->xdp);
2229 		desc->read.pkt_addr = cpu_to_le64(dma);
2230 
2231 		desc++;
2232 		bi++;
2233 		i++;
2234 		if (unlikely(!i)) {
2235 			desc = IGC_RX_DESC(ring, 0);
2236 			bi = ring->rx_buffer_info;
2237 			i -= ring->count;
2238 		}
2239 
2240 		/* Clear the length for the next_to_use descriptor. */
2241 		desc->wb.upper.length = 0;
2242 
2243 		count--;
2244 	} while (count);
2245 
2246 	i += ring->count;
2247 
2248 	if (ring->next_to_use != i) {
2249 		ring->next_to_use = i;
2250 
2251 		/* Force memory writes to complete before letting h/w
2252 		 * know there are new descriptors to fetch.  (Only
2253 		 * applicable for weak-ordered memory model archs,
2254 		 * such as IA-64).
2255 		 */
2256 		wmb();
2257 		writel(i, ring->tail);
2258 	}
2259 
2260 	return ok;
2261 }
2262 
2263 /* This function requires __netif_tx_lock is held by the caller. */
2264 static int igc_xdp_init_tx_descriptor(struct igc_ring *ring,
2265 				      struct xdp_frame *xdpf)
2266 {
2267 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_frame(xdpf);
2268 	u8 nr_frags = unlikely(xdp_frame_has_frags(xdpf)) ? sinfo->nr_frags : 0;
2269 	u16 count, index = ring->next_to_use;
2270 	struct igc_tx_buffer *head = &ring->tx_buffer_info[index];
2271 	struct igc_tx_buffer *buffer = head;
2272 	union igc_adv_tx_desc *desc = IGC_TX_DESC(ring, index);
2273 	u32 olinfo_status, len = xdpf->len, cmd_type;
2274 	void *data = xdpf->data;
2275 	u16 i;
2276 
2277 	count = TXD_USE_COUNT(len);
2278 	for (i = 0; i < nr_frags; i++)
2279 		count += TXD_USE_COUNT(skb_frag_size(&sinfo->frags[i]));
2280 
2281 	if (igc_maybe_stop_tx(ring, count + 3)) {
2282 		/* this is a hard error */
2283 		return -EBUSY;
2284 	}
2285 
2286 	i = 0;
2287 	head->bytecount = xdp_get_frame_len(xdpf);
2288 	head->type = IGC_TX_BUFFER_TYPE_XDP;
2289 	head->gso_segs = 1;
2290 	head->xdpf = xdpf;
2291 
2292 	olinfo_status = head->bytecount << IGC_ADVTXD_PAYLEN_SHIFT;
2293 	desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2294 
2295 	for (;;) {
2296 		dma_addr_t dma;
2297 
2298 		dma = dma_map_single(ring->dev, data, len, DMA_TO_DEVICE);
2299 		if (dma_mapping_error(ring->dev, dma)) {
2300 			netdev_err_once(ring->netdev,
2301 					"Failed to map DMA for TX\n");
2302 			goto unmap;
2303 		}
2304 
2305 		dma_unmap_len_set(buffer, len, len);
2306 		dma_unmap_addr_set(buffer, dma, dma);
2307 
2308 		cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT |
2309 			   IGC_ADVTXD_DCMD_IFCS | len;
2310 
2311 		desc->read.cmd_type_len = cpu_to_le32(cmd_type);
2312 		desc->read.buffer_addr = cpu_to_le64(dma);
2313 
2314 		buffer->protocol = 0;
2315 
2316 		if (++index == ring->count)
2317 			index = 0;
2318 
2319 		if (i == nr_frags)
2320 			break;
2321 
2322 		buffer = &ring->tx_buffer_info[index];
2323 		desc = IGC_TX_DESC(ring, index);
2324 		desc->read.olinfo_status = 0;
2325 
2326 		data = skb_frag_address(&sinfo->frags[i]);
2327 		len = skb_frag_size(&sinfo->frags[i]);
2328 		i++;
2329 	}
2330 	desc->read.cmd_type_len |= cpu_to_le32(IGC_TXD_DCMD);
2331 
2332 	netdev_tx_sent_queue(txring_txq(ring), head->bytecount);
2333 	/* set the timestamp */
2334 	head->time_stamp = jiffies;
2335 	/* set next_to_watch value indicating a packet is present */
2336 	head->next_to_watch = desc;
2337 	ring->next_to_use = index;
2338 
2339 	return 0;
2340 
2341 unmap:
2342 	for (;;) {
2343 		buffer = &ring->tx_buffer_info[index];
2344 		if (dma_unmap_len(buffer, len))
2345 			dma_unmap_page(ring->dev,
2346 				       dma_unmap_addr(buffer, dma),
2347 				       dma_unmap_len(buffer, len),
2348 				       DMA_TO_DEVICE);
2349 		dma_unmap_len_set(buffer, len, 0);
2350 		if (buffer == head)
2351 			break;
2352 
2353 		if (!index)
2354 			index += ring->count;
2355 		index--;
2356 	}
2357 
2358 	return -ENOMEM;
2359 }
2360 
2361 static struct igc_ring *igc_xdp_get_tx_ring(struct igc_adapter *adapter,
2362 					    int cpu)
2363 {
2364 	int index = cpu;
2365 
2366 	if (unlikely(index < 0))
2367 		index = 0;
2368 
2369 	while (index >= adapter->num_tx_queues)
2370 		index -= adapter->num_tx_queues;
2371 
2372 	return adapter->tx_ring[index];
2373 }
2374 
2375 static int igc_xdp_xmit_back(struct igc_adapter *adapter, struct xdp_buff *xdp)
2376 {
2377 	struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
2378 	int cpu = smp_processor_id();
2379 	struct netdev_queue *nq;
2380 	struct igc_ring *ring;
2381 	int res;
2382 
2383 	if (unlikely(!xdpf))
2384 		return -EFAULT;
2385 
2386 	ring = igc_xdp_get_tx_ring(adapter, cpu);
2387 	nq = txring_txq(ring);
2388 
2389 	__netif_tx_lock(nq, cpu);
2390 	res = igc_xdp_init_tx_descriptor(ring, xdpf);
2391 	__netif_tx_unlock(nq);
2392 	return res;
2393 }
2394 
2395 /* This function assumes rcu_read_lock() is held by the caller. */
2396 static int __igc_xdp_run_prog(struct igc_adapter *adapter,
2397 			      struct bpf_prog *prog,
2398 			      struct xdp_buff *xdp)
2399 {
2400 	u32 act = bpf_prog_run_xdp(prog, xdp);
2401 
2402 	switch (act) {
2403 	case XDP_PASS:
2404 		return IGC_XDP_PASS;
2405 	case XDP_TX:
2406 		if (igc_xdp_xmit_back(adapter, xdp) < 0)
2407 			goto out_failure;
2408 		return IGC_XDP_TX;
2409 	case XDP_REDIRECT:
2410 		if (xdp_do_redirect(adapter->netdev, xdp, prog) < 0)
2411 			goto out_failure;
2412 		return IGC_XDP_REDIRECT;
2413 		break;
2414 	default:
2415 		bpf_warn_invalid_xdp_action(adapter->netdev, prog, act);
2416 		fallthrough;
2417 	case XDP_ABORTED:
2418 out_failure:
2419 		trace_xdp_exception(adapter->netdev, prog, act);
2420 		fallthrough;
2421 	case XDP_DROP:
2422 		return IGC_XDP_CONSUMED;
2423 	}
2424 }
2425 
2426 static struct sk_buff *igc_xdp_run_prog(struct igc_adapter *adapter,
2427 					struct xdp_buff *xdp)
2428 {
2429 	struct bpf_prog *prog;
2430 	int res;
2431 
2432 	prog = READ_ONCE(adapter->xdp_prog);
2433 	if (!prog) {
2434 		res = IGC_XDP_PASS;
2435 		goto out;
2436 	}
2437 
2438 	res = __igc_xdp_run_prog(adapter, prog, xdp);
2439 
2440 out:
2441 	return ERR_PTR(-res);
2442 }
2443 
2444 /* This function assumes __netif_tx_lock is held by the caller. */
2445 static void igc_flush_tx_descriptors(struct igc_ring *ring)
2446 {
2447 	/* Once tail pointer is updated, hardware can fetch the descriptors
2448 	 * any time so we issue a write membar here to ensure all memory
2449 	 * writes are complete before the tail pointer is updated.
2450 	 */
2451 	wmb();
2452 	writel(ring->next_to_use, ring->tail);
2453 }
2454 
2455 static void igc_finalize_xdp(struct igc_adapter *adapter, int status)
2456 {
2457 	int cpu = smp_processor_id();
2458 	struct netdev_queue *nq;
2459 	struct igc_ring *ring;
2460 
2461 	if (status & IGC_XDP_TX) {
2462 		ring = igc_xdp_get_tx_ring(adapter, cpu);
2463 		nq = txring_txq(ring);
2464 
2465 		__netif_tx_lock(nq, cpu);
2466 		igc_flush_tx_descriptors(ring);
2467 		__netif_tx_unlock(nq);
2468 	}
2469 
2470 	if (status & IGC_XDP_REDIRECT)
2471 		xdp_do_flush();
2472 }
2473 
2474 static void igc_update_rx_stats(struct igc_q_vector *q_vector,
2475 				unsigned int packets, unsigned int bytes)
2476 {
2477 	struct igc_ring *ring = q_vector->rx.ring;
2478 
2479 	u64_stats_update_begin(&ring->rx_syncp);
2480 	ring->rx_stats.packets += packets;
2481 	ring->rx_stats.bytes += bytes;
2482 	u64_stats_update_end(&ring->rx_syncp);
2483 
2484 	q_vector->rx.total_packets += packets;
2485 	q_vector->rx.total_bytes += bytes;
2486 }
2487 
2488 static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
2489 {
2490 	unsigned int total_bytes = 0, total_packets = 0;
2491 	struct igc_adapter *adapter = q_vector->adapter;
2492 	struct igc_ring *rx_ring = q_vector->rx.ring;
2493 	struct sk_buff *skb = rx_ring->skb;
2494 	u16 cleaned_count = igc_desc_unused(rx_ring);
2495 	int xdp_status = 0, rx_buffer_pgcnt;
2496 
2497 	while (likely(total_packets < budget)) {
2498 		union igc_adv_rx_desc *rx_desc;
2499 		struct igc_rx_buffer *rx_buffer;
2500 		unsigned int size, truesize;
2501 		ktime_t timestamp = 0;
2502 		struct xdp_buff xdp;
2503 		int pkt_offset = 0;
2504 		void *pktbuf;
2505 
2506 		/* return some buffers to hardware, one at a time is too slow */
2507 		if (cleaned_count >= IGC_RX_BUFFER_WRITE) {
2508 			igc_alloc_rx_buffers(rx_ring, cleaned_count);
2509 			cleaned_count = 0;
2510 		}
2511 
2512 		rx_desc = IGC_RX_DESC(rx_ring, rx_ring->next_to_clean);
2513 		size = le16_to_cpu(rx_desc->wb.upper.length);
2514 		if (!size)
2515 			break;
2516 
2517 		/* This memory barrier is needed to keep us from reading
2518 		 * any other fields out of the rx_desc until we know the
2519 		 * descriptor has been written back
2520 		 */
2521 		dma_rmb();
2522 
2523 		rx_buffer = igc_get_rx_buffer(rx_ring, size, &rx_buffer_pgcnt);
2524 		truesize = igc_get_rx_frame_truesize(rx_ring, size);
2525 
2526 		pktbuf = page_address(rx_buffer->page) + rx_buffer->page_offset;
2527 
2528 		if (igc_test_staterr(rx_desc, IGC_RXDADV_STAT_TSIP)) {
2529 			timestamp = igc_ptp_rx_pktstamp(q_vector->adapter,
2530 							pktbuf);
2531 			pkt_offset = IGC_TS_HDR_LEN;
2532 			size -= IGC_TS_HDR_LEN;
2533 		}
2534 
2535 		if (!skb) {
2536 			xdp_init_buff(&xdp, truesize, &rx_ring->xdp_rxq);
2537 			xdp_prepare_buff(&xdp, pktbuf - igc_rx_offset(rx_ring),
2538 					 igc_rx_offset(rx_ring) + pkt_offset,
2539 					 size, true);
2540 			xdp_buff_clear_frags_flag(&xdp);
2541 
2542 			skb = igc_xdp_run_prog(adapter, &xdp);
2543 		}
2544 
2545 		if (IS_ERR(skb)) {
2546 			unsigned int xdp_res = -PTR_ERR(skb);
2547 
2548 			switch (xdp_res) {
2549 			case IGC_XDP_CONSUMED:
2550 				rx_buffer->pagecnt_bias++;
2551 				break;
2552 			case IGC_XDP_TX:
2553 			case IGC_XDP_REDIRECT:
2554 				igc_rx_buffer_flip(rx_buffer, truesize);
2555 				xdp_status |= xdp_res;
2556 				break;
2557 			}
2558 
2559 			total_packets++;
2560 			total_bytes += size;
2561 		} else if (skb)
2562 			igc_add_rx_frag(rx_ring, rx_buffer, skb, size);
2563 		else if (ring_uses_build_skb(rx_ring))
2564 			skb = igc_build_skb(rx_ring, rx_buffer, &xdp);
2565 		else
2566 			skb = igc_construct_skb(rx_ring, rx_buffer, &xdp,
2567 						timestamp);
2568 
2569 		/* exit if we failed to retrieve a buffer */
2570 		if (!skb) {
2571 			rx_ring->rx_stats.alloc_failed++;
2572 			rx_buffer->pagecnt_bias++;
2573 			break;
2574 		}
2575 
2576 		igc_put_rx_buffer(rx_ring, rx_buffer, rx_buffer_pgcnt);
2577 		cleaned_count++;
2578 
2579 		/* fetch next buffer in frame if non-eop */
2580 		if (igc_is_non_eop(rx_ring, rx_desc))
2581 			continue;
2582 
2583 		/* verify the packet layout is correct */
2584 		if (igc_cleanup_headers(rx_ring, rx_desc, skb)) {
2585 			skb = NULL;
2586 			continue;
2587 		}
2588 
2589 		/* probably a little skewed due to removing CRC */
2590 		total_bytes += skb->len;
2591 
2592 		/* populate checksum, VLAN, and protocol */
2593 		igc_process_skb_fields(rx_ring, rx_desc, skb);
2594 
2595 		napi_gro_receive(&q_vector->napi, skb);
2596 
2597 		/* reset skb pointer */
2598 		skb = NULL;
2599 
2600 		/* update budget accounting */
2601 		total_packets++;
2602 	}
2603 
2604 	if (xdp_status)
2605 		igc_finalize_xdp(adapter, xdp_status);
2606 
2607 	/* place incomplete frames back on ring for completion */
2608 	rx_ring->skb = skb;
2609 
2610 	igc_update_rx_stats(q_vector, total_packets, total_bytes);
2611 
2612 	if (cleaned_count)
2613 		igc_alloc_rx_buffers(rx_ring, cleaned_count);
2614 
2615 	return total_packets;
2616 }
2617 
2618 static struct sk_buff *igc_construct_skb_zc(struct igc_ring *ring,
2619 					    struct xdp_buff *xdp)
2620 {
2621 	unsigned int totalsize = xdp->data_end - xdp->data_meta;
2622 	unsigned int metasize = xdp->data - xdp->data_meta;
2623 	struct sk_buff *skb;
2624 
2625 	net_prefetch(xdp->data_meta);
2626 
2627 	skb = __napi_alloc_skb(&ring->q_vector->napi, totalsize,
2628 			       GFP_ATOMIC | __GFP_NOWARN);
2629 	if (unlikely(!skb))
2630 		return NULL;
2631 
2632 	memcpy(__skb_put(skb, totalsize), xdp->data_meta,
2633 	       ALIGN(totalsize, sizeof(long)));
2634 
2635 	if (metasize) {
2636 		skb_metadata_set(skb, metasize);
2637 		__skb_pull(skb, metasize);
2638 	}
2639 
2640 	return skb;
2641 }
2642 
2643 static void igc_dispatch_skb_zc(struct igc_q_vector *q_vector,
2644 				union igc_adv_rx_desc *desc,
2645 				struct xdp_buff *xdp,
2646 				ktime_t timestamp)
2647 {
2648 	struct igc_ring *ring = q_vector->rx.ring;
2649 	struct sk_buff *skb;
2650 
2651 	skb = igc_construct_skb_zc(ring, xdp);
2652 	if (!skb) {
2653 		ring->rx_stats.alloc_failed++;
2654 		return;
2655 	}
2656 
2657 	if (timestamp)
2658 		skb_hwtstamps(skb)->hwtstamp = timestamp;
2659 
2660 	if (igc_cleanup_headers(ring, desc, skb))
2661 		return;
2662 
2663 	igc_process_skb_fields(ring, desc, skb);
2664 	napi_gro_receive(&q_vector->napi, skb);
2665 }
2666 
2667 static int igc_clean_rx_irq_zc(struct igc_q_vector *q_vector, const int budget)
2668 {
2669 	struct igc_adapter *adapter = q_vector->adapter;
2670 	struct igc_ring *ring = q_vector->rx.ring;
2671 	u16 cleaned_count = igc_desc_unused(ring);
2672 	int total_bytes = 0, total_packets = 0;
2673 	u16 ntc = ring->next_to_clean;
2674 	struct bpf_prog *prog;
2675 	bool failure = false;
2676 	int xdp_status = 0;
2677 
2678 	rcu_read_lock();
2679 
2680 	prog = READ_ONCE(adapter->xdp_prog);
2681 
2682 	while (likely(total_packets < budget)) {
2683 		union igc_adv_rx_desc *desc;
2684 		struct igc_rx_buffer *bi;
2685 		ktime_t timestamp = 0;
2686 		unsigned int size;
2687 		int res;
2688 
2689 		desc = IGC_RX_DESC(ring, ntc);
2690 		size = le16_to_cpu(desc->wb.upper.length);
2691 		if (!size)
2692 			break;
2693 
2694 		/* This memory barrier is needed to keep us from reading
2695 		 * any other fields out of the rx_desc until we know the
2696 		 * descriptor has been written back
2697 		 */
2698 		dma_rmb();
2699 
2700 		bi = &ring->rx_buffer_info[ntc];
2701 
2702 		if (igc_test_staterr(desc, IGC_RXDADV_STAT_TSIP)) {
2703 			timestamp = igc_ptp_rx_pktstamp(q_vector->adapter,
2704 							bi->xdp->data);
2705 
2706 			bi->xdp->data += IGC_TS_HDR_LEN;
2707 
2708 			/* HW timestamp has been copied into local variable. Metadata
2709 			 * length when XDP program is called should be 0.
2710 			 */
2711 			bi->xdp->data_meta += IGC_TS_HDR_LEN;
2712 			size -= IGC_TS_HDR_LEN;
2713 		}
2714 
2715 		bi->xdp->data_end = bi->xdp->data + size;
2716 		xsk_buff_dma_sync_for_cpu(bi->xdp, ring->xsk_pool);
2717 
2718 		res = __igc_xdp_run_prog(adapter, prog, bi->xdp);
2719 		switch (res) {
2720 		case IGC_XDP_PASS:
2721 			igc_dispatch_skb_zc(q_vector, desc, bi->xdp, timestamp);
2722 			fallthrough;
2723 		case IGC_XDP_CONSUMED:
2724 			xsk_buff_free(bi->xdp);
2725 			break;
2726 		case IGC_XDP_TX:
2727 		case IGC_XDP_REDIRECT:
2728 			xdp_status |= res;
2729 			break;
2730 		}
2731 
2732 		bi->xdp = NULL;
2733 		total_bytes += size;
2734 		total_packets++;
2735 		cleaned_count++;
2736 		ntc++;
2737 		if (ntc == ring->count)
2738 			ntc = 0;
2739 	}
2740 
2741 	ring->next_to_clean = ntc;
2742 	rcu_read_unlock();
2743 
2744 	if (cleaned_count >= IGC_RX_BUFFER_WRITE)
2745 		failure = !igc_alloc_rx_buffers_zc(ring, cleaned_count);
2746 
2747 	if (xdp_status)
2748 		igc_finalize_xdp(adapter, xdp_status);
2749 
2750 	igc_update_rx_stats(q_vector, total_packets, total_bytes);
2751 
2752 	if (xsk_uses_need_wakeup(ring->xsk_pool)) {
2753 		if (failure || ring->next_to_clean == ring->next_to_use)
2754 			xsk_set_rx_need_wakeup(ring->xsk_pool);
2755 		else
2756 			xsk_clear_rx_need_wakeup(ring->xsk_pool);
2757 		return total_packets;
2758 	}
2759 
2760 	return failure ? budget : total_packets;
2761 }
2762 
2763 static void igc_update_tx_stats(struct igc_q_vector *q_vector,
2764 				unsigned int packets, unsigned int bytes)
2765 {
2766 	struct igc_ring *ring = q_vector->tx.ring;
2767 
2768 	u64_stats_update_begin(&ring->tx_syncp);
2769 	ring->tx_stats.bytes += bytes;
2770 	ring->tx_stats.packets += packets;
2771 	u64_stats_update_end(&ring->tx_syncp);
2772 
2773 	q_vector->tx.total_bytes += bytes;
2774 	q_vector->tx.total_packets += packets;
2775 }
2776 
2777 static void igc_xdp_xmit_zc(struct igc_ring *ring)
2778 {
2779 	struct xsk_buff_pool *pool = ring->xsk_pool;
2780 	struct netdev_queue *nq = txring_txq(ring);
2781 	union igc_adv_tx_desc *tx_desc = NULL;
2782 	int cpu = smp_processor_id();
2783 	u16 ntu = ring->next_to_use;
2784 	struct xdp_desc xdp_desc;
2785 	u16 budget;
2786 
2787 	if (!netif_carrier_ok(ring->netdev))
2788 		return;
2789 
2790 	__netif_tx_lock(nq, cpu);
2791 
2792 	budget = igc_desc_unused(ring);
2793 
2794 	while (xsk_tx_peek_desc(pool, &xdp_desc) && budget--) {
2795 		u32 cmd_type, olinfo_status;
2796 		struct igc_tx_buffer *bi;
2797 		dma_addr_t dma;
2798 
2799 		cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT |
2800 			   IGC_ADVTXD_DCMD_IFCS | IGC_TXD_DCMD |
2801 			   xdp_desc.len;
2802 		olinfo_status = xdp_desc.len << IGC_ADVTXD_PAYLEN_SHIFT;
2803 
2804 		dma = xsk_buff_raw_get_dma(pool, xdp_desc.addr);
2805 		xsk_buff_raw_dma_sync_for_device(pool, dma, xdp_desc.len);
2806 
2807 		tx_desc = IGC_TX_DESC(ring, ntu);
2808 		tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
2809 		tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2810 		tx_desc->read.buffer_addr = cpu_to_le64(dma);
2811 
2812 		bi = &ring->tx_buffer_info[ntu];
2813 		bi->type = IGC_TX_BUFFER_TYPE_XSK;
2814 		bi->protocol = 0;
2815 		bi->bytecount = xdp_desc.len;
2816 		bi->gso_segs = 1;
2817 		bi->time_stamp = jiffies;
2818 		bi->next_to_watch = tx_desc;
2819 
2820 		netdev_tx_sent_queue(txring_txq(ring), xdp_desc.len);
2821 
2822 		ntu++;
2823 		if (ntu == ring->count)
2824 			ntu = 0;
2825 	}
2826 
2827 	ring->next_to_use = ntu;
2828 	if (tx_desc) {
2829 		igc_flush_tx_descriptors(ring);
2830 		xsk_tx_release(pool);
2831 	}
2832 
2833 	__netif_tx_unlock(nq);
2834 }
2835 
2836 /**
2837  * igc_clean_tx_irq - Reclaim resources after transmit completes
2838  * @q_vector: pointer to q_vector containing needed info
2839  * @napi_budget: Used to determine if we are in netpoll
2840  *
2841  * returns true if ring is completely cleaned
2842  */
2843 static bool igc_clean_tx_irq(struct igc_q_vector *q_vector, int napi_budget)
2844 {
2845 	struct igc_adapter *adapter = q_vector->adapter;
2846 	unsigned int total_bytes = 0, total_packets = 0;
2847 	unsigned int budget = q_vector->tx.work_limit;
2848 	struct igc_ring *tx_ring = q_vector->tx.ring;
2849 	unsigned int i = tx_ring->next_to_clean;
2850 	struct igc_tx_buffer *tx_buffer;
2851 	union igc_adv_tx_desc *tx_desc;
2852 	u32 xsk_frames = 0;
2853 
2854 	if (test_bit(__IGC_DOWN, &adapter->state))
2855 		return true;
2856 
2857 	tx_buffer = &tx_ring->tx_buffer_info[i];
2858 	tx_desc = IGC_TX_DESC(tx_ring, i);
2859 	i -= tx_ring->count;
2860 
2861 	do {
2862 		union igc_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
2863 
2864 		/* if next_to_watch is not set then there is no work pending */
2865 		if (!eop_desc)
2866 			break;
2867 
2868 		/* prevent any other reads prior to eop_desc */
2869 		smp_rmb();
2870 
2871 		/* if DD is not set pending work has not been completed */
2872 		if (!(eop_desc->wb.status & cpu_to_le32(IGC_TXD_STAT_DD)))
2873 			break;
2874 
2875 		/* clear next_to_watch to prevent false hangs */
2876 		tx_buffer->next_to_watch = NULL;
2877 
2878 		/* update the statistics for this packet */
2879 		total_bytes += tx_buffer->bytecount;
2880 		total_packets += tx_buffer->gso_segs;
2881 
2882 		switch (tx_buffer->type) {
2883 		case IGC_TX_BUFFER_TYPE_XSK:
2884 			xsk_frames++;
2885 			break;
2886 		case IGC_TX_BUFFER_TYPE_XDP:
2887 			xdp_return_frame(tx_buffer->xdpf);
2888 			igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
2889 			break;
2890 		case IGC_TX_BUFFER_TYPE_SKB:
2891 			napi_consume_skb(tx_buffer->skb, napi_budget);
2892 			igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
2893 			break;
2894 		default:
2895 			netdev_warn_once(tx_ring->netdev, "Unknown Tx buffer type\n");
2896 			break;
2897 		}
2898 
2899 		/* clear last DMA location and unmap remaining buffers */
2900 		while (tx_desc != eop_desc) {
2901 			tx_buffer++;
2902 			tx_desc++;
2903 			i++;
2904 			if (unlikely(!i)) {
2905 				i -= tx_ring->count;
2906 				tx_buffer = tx_ring->tx_buffer_info;
2907 				tx_desc = IGC_TX_DESC(tx_ring, 0);
2908 			}
2909 
2910 			/* unmap any remaining paged data */
2911 			if (dma_unmap_len(tx_buffer, len))
2912 				igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
2913 		}
2914 
2915 		/* move us one more past the eop_desc for start of next pkt */
2916 		tx_buffer++;
2917 		tx_desc++;
2918 		i++;
2919 		if (unlikely(!i)) {
2920 			i -= tx_ring->count;
2921 			tx_buffer = tx_ring->tx_buffer_info;
2922 			tx_desc = IGC_TX_DESC(tx_ring, 0);
2923 		}
2924 
2925 		/* issue prefetch for next Tx descriptor */
2926 		prefetch(tx_desc);
2927 
2928 		/* update budget accounting */
2929 		budget--;
2930 	} while (likely(budget));
2931 
2932 	netdev_tx_completed_queue(txring_txq(tx_ring),
2933 				  total_packets, total_bytes);
2934 
2935 	i += tx_ring->count;
2936 	tx_ring->next_to_clean = i;
2937 
2938 	igc_update_tx_stats(q_vector, total_packets, total_bytes);
2939 
2940 	if (tx_ring->xsk_pool) {
2941 		if (xsk_frames)
2942 			xsk_tx_completed(tx_ring->xsk_pool, xsk_frames);
2943 		if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
2944 			xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
2945 		igc_xdp_xmit_zc(tx_ring);
2946 	}
2947 
2948 	if (test_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
2949 		struct igc_hw *hw = &adapter->hw;
2950 
2951 		/* Detect a transmit hang in hardware, this serializes the
2952 		 * check with the clearing of time_stamp and movement of i
2953 		 */
2954 		clear_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
2955 		if (tx_buffer->next_to_watch &&
2956 		    time_after(jiffies, tx_buffer->time_stamp +
2957 		    (adapter->tx_timeout_factor * HZ)) &&
2958 		    !(rd32(IGC_STATUS) & IGC_STATUS_TXOFF) &&
2959 		    (rd32(IGC_TDH(tx_ring->reg_idx)) !=
2960 		     readl(tx_ring->tail))) {
2961 			/* detected Tx unit hang */
2962 			netdev_err(tx_ring->netdev,
2963 				   "Detected Tx Unit Hang\n"
2964 				   "  Tx Queue             <%d>\n"
2965 				   "  TDH                  <%x>\n"
2966 				   "  TDT                  <%x>\n"
2967 				   "  next_to_use          <%x>\n"
2968 				   "  next_to_clean        <%x>\n"
2969 				   "buffer_info[next_to_clean]\n"
2970 				   "  time_stamp           <%lx>\n"
2971 				   "  next_to_watch        <%p>\n"
2972 				   "  jiffies              <%lx>\n"
2973 				   "  desc.status          <%x>\n",
2974 				   tx_ring->queue_index,
2975 				   rd32(IGC_TDH(tx_ring->reg_idx)),
2976 				   readl(tx_ring->tail),
2977 				   tx_ring->next_to_use,
2978 				   tx_ring->next_to_clean,
2979 				   tx_buffer->time_stamp,
2980 				   tx_buffer->next_to_watch,
2981 				   jiffies,
2982 				   tx_buffer->next_to_watch->wb.status);
2983 			netif_stop_subqueue(tx_ring->netdev,
2984 					    tx_ring->queue_index);
2985 
2986 			/* we are about to reset, no point in enabling stuff */
2987 			return true;
2988 		}
2989 	}
2990 
2991 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
2992 	if (unlikely(total_packets &&
2993 		     netif_carrier_ok(tx_ring->netdev) &&
2994 		     igc_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
2995 		/* Make sure that anybody stopping the queue after this
2996 		 * sees the new next_to_clean.
2997 		 */
2998 		smp_mb();
2999 		if (__netif_subqueue_stopped(tx_ring->netdev,
3000 					     tx_ring->queue_index) &&
3001 		    !(test_bit(__IGC_DOWN, &adapter->state))) {
3002 			netif_wake_subqueue(tx_ring->netdev,
3003 					    tx_ring->queue_index);
3004 
3005 			u64_stats_update_begin(&tx_ring->tx_syncp);
3006 			tx_ring->tx_stats.restart_queue++;
3007 			u64_stats_update_end(&tx_ring->tx_syncp);
3008 		}
3009 	}
3010 
3011 	return !!budget;
3012 }
3013 
3014 static int igc_find_mac_filter(struct igc_adapter *adapter,
3015 			       enum igc_mac_filter_type type, const u8 *addr)
3016 {
3017 	struct igc_hw *hw = &adapter->hw;
3018 	int max_entries = hw->mac.rar_entry_count;
3019 	u32 ral, rah;
3020 	int i;
3021 
3022 	for (i = 0; i < max_entries; i++) {
3023 		ral = rd32(IGC_RAL(i));
3024 		rah = rd32(IGC_RAH(i));
3025 
3026 		if (!(rah & IGC_RAH_AV))
3027 			continue;
3028 		if (!!(rah & IGC_RAH_ASEL_SRC_ADDR) != type)
3029 			continue;
3030 		if ((rah & IGC_RAH_RAH_MASK) !=
3031 		    le16_to_cpup((__le16 *)(addr + 4)))
3032 			continue;
3033 		if (ral != le32_to_cpup((__le32 *)(addr)))
3034 			continue;
3035 
3036 		return i;
3037 	}
3038 
3039 	return -1;
3040 }
3041 
3042 static int igc_get_avail_mac_filter_slot(struct igc_adapter *adapter)
3043 {
3044 	struct igc_hw *hw = &adapter->hw;
3045 	int max_entries = hw->mac.rar_entry_count;
3046 	u32 rah;
3047 	int i;
3048 
3049 	for (i = 0; i < max_entries; i++) {
3050 		rah = rd32(IGC_RAH(i));
3051 
3052 		if (!(rah & IGC_RAH_AV))
3053 			return i;
3054 	}
3055 
3056 	return -1;
3057 }
3058 
3059 /**
3060  * igc_add_mac_filter() - Add MAC address filter
3061  * @adapter: Pointer to adapter where the filter should be added
3062  * @type: MAC address filter type (source or destination)
3063  * @addr: MAC address
3064  * @queue: If non-negative, queue assignment feature is enabled and frames
3065  *         matching the filter are enqueued onto 'queue'. Otherwise, queue
3066  *         assignment is disabled.
3067  *
3068  * Return: 0 in case of success, negative errno code otherwise.
3069  */
3070 static int igc_add_mac_filter(struct igc_adapter *adapter,
3071 			      enum igc_mac_filter_type type, const u8 *addr,
3072 			      int queue)
3073 {
3074 	struct net_device *dev = adapter->netdev;
3075 	int index;
3076 
3077 	index = igc_find_mac_filter(adapter, type, addr);
3078 	if (index >= 0)
3079 		goto update_filter;
3080 
3081 	index = igc_get_avail_mac_filter_slot(adapter);
3082 	if (index < 0)
3083 		return -ENOSPC;
3084 
3085 	netdev_dbg(dev, "Add MAC address filter: index %d type %s address %pM queue %d\n",
3086 		   index, type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src",
3087 		   addr, queue);
3088 
3089 update_filter:
3090 	igc_set_mac_filter_hw(adapter, index, type, addr, queue);
3091 	return 0;
3092 }
3093 
3094 /**
3095  * igc_del_mac_filter() - Delete MAC address filter
3096  * @adapter: Pointer to adapter where the filter should be deleted from
3097  * @type: MAC address filter type (source or destination)
3098  * @addr: MAC address
3099  */
3100 static void igc_del_mac_filter(struct igc_adapter *adapter,
3101 			       enum igc_mac_filter_type type, const u8 *addr)
3102 {
3103 	struct net_device *dev = adapter->netdev;
3104 	int index;
3105 
3106 	index = igc_find_mac_filter(adapter, type, addr);
3107 	if (index < 0)
3108 		return;
3109 
3110 	if (index == 0) {
3111 		/* If this is the default filter, we don't actually delete it.
3112 		 * We just reset to its default value i.e. disable queue
3113 		 * assignment.
3114 		 */
3115 		netdev_dbg(dev, "Disable default MAC filter queue assignment");
3116 
3117 		igc_set_mac_filter_hw(adapter, 0, type, addr, -1);
3118 	} else {
3119 		netdev_dbg(dev, "Delete MAC address filter: index %d type %s address %pM\n",
3120 			   index,
3121 			   type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src",
3122 			   addr);
3123 
3124 		igc_clear_mac_filter_hw(adapter, index);
3125 	}
3126 }
3127 
3128 /**
3129  * igc_add_vlan_prio_filter() - Add VLAN priority filter
3130  * @adapter: Pointer to adapter where the filter should be added
3131  * @prio: VLAN priority value
3132  * @queue: Queue number which matching frames are assigned to
3133  *
3134  * Return: 0 in case of success, negative errno code otherwise.
3135  */
3136 static int igc_add_vlan_prio_filter(struct igc_adapter *adapter, int prio,
3137 				    int queue)
3138 {
3139 	struct net_device *dev = adapter->netdev;
3140 	struct igc_hw *hw = &adapter->hw;
3141 	u32 vlanpqf;
3142 
3143 	vlanpqf = rd32(IGC_VLANPQF);
3144 
3145 	if (vlanpqf & IGC_VLANPQF_VALID(prio)) {
3146 		netdev_dbg(dev, "VLAN priority filter already in use\n");
3147 		return -EEXIST;
3148 	}
3149 
3150 	vlanpqf |= IGC_VLANPQF_QSEL(prio, queue);
3151 	vlanpqf |= IGC_VLANPQF_VALID(prio);
3152 
3153 	wr32(IGC_VLANPQF, vlanpqf);
3154 
3155 	netdev_dbg(dev, "Add VLAN priority filter: prio %d queue %d\n",
3156 		   prio, queue);
3157 	return 0;
3158 }
3159 
3160 /**
3161  * igc_del_vlan_prio_filter() - Delete VLAN priority filter
3162  * @adapter: Pointer to adapter where the filter should be deleted from
3163  * @prio: VLAN priority value
3164  */
3165 static void igc_del_vlan_prio_filter(struct igc_adapter *adapter, int prio)
3166 {
3167 	struct igc_hw *hw = &adapter->hw;
3168 	u32 vlanpqf;
3169 
3170 	vlanpqf = rd32(IGC_VLANPQF);
3171 
3172 	vlanpqf &= ~IGC_VLANPQF_VALID(prio);
3173 	vlanpqf &= ~IGC_VLANPQF_QSEL(prio, IGC_VLANPQF_QUEUE_MASK);
3174 
3175 	wr32(IGC_VLANPQF, vlanpqf);
3176 
3177 	netdev_dbg(adapter->netdev, "Delete VLAN priority filter: prio %d\n",
3178 		   prio);
3179 }
3180 
3181 static int igc_get_avail_etype_filter_slot(struct igc_adapter *adapter)
3182 {
3183 	struct igc_hw *hw = &adapter->hw;
3184 	int i;
3185 
3186 	for (i = 0; i < MAX_ETYPE_FILTER; i++) {
3187 		u32 etqf = rd32(IGC_ETQF(i));
3188 
3189 		if (!(etqf & IGC_ETQF_FILTER_ENABLE))
3190 			return i;
3191 	}
3192 
3193 	return -1;
3194 }
3195 
3196 /**
3197  * igc_add_etype_filter() - Add ethertype filter
3198  * @adapter: Pointer to adapter where the filter should be added
3199  * @etype: Ethertype value
3200  * @queue: If non-negative, queue assignment feature is enabled and frames
3201  *         matching the filter are enqueued onto 'queue'. Otherwise, queue
3202  *         assignment is disabled.
3203  *
3204  * Return: 0 in case of success, negative errno code otherwise.
3205  */
3206 static int igc_add_etype_filter(struct igc_adapter *adapter, u16 etype,
3207 				int queue)
3208 {
3209 	struct igc_hw *hw = &adapter->hw;
3210 	int index;
3211 	u32 etqf;
3212 
3213 	index = igc_get_avail_etype_filter_slot(adapter);
3214 	if (index < 0)
3215 		return -ENOSPC;
3216 
3217 	etqf = rd32(IGC_ETQF(index));
3218 
3219 	etqf &= ~IGC_ETQF_ETYPE_MASK;
3220 	etqf |= etype;
3221 
3222 	if (queue >= 0) {
3223 		etqf &= ~IGC_ETQF_QUEUE_MASK;
3224 		etqf |= (queue << IGC_ETQF_QUEUE_SHIFT);
3225 		etqf |= IGC_ETQF_QUEUE_ENABLE;
3226 	}
3227 
3228 	etqf |= IGC_ETQF_FILTER_ENABLE;
3229 
3230 	wr32(IGC_ETQF(index), etqf);
3231 
3232 	netdev_dbg(adapter->netdev, "Add ethertype filter: etype %04x queue %d\n",
3233 		   etype, queue);
3234 	return 0;
3235 }
3236 
3237 static int igc_find_etype_filter(struct igc_adapter *adapter, u16 etype)
3238 {
3239 	struct igc_hw *hw = &adapter->hw;
3240 	int i;
3241 
3242 	for (i = 0; i < MAX_ETYPE_FILTER; i++) {
3243 		u32 etqf = rd32(IGC_ETQF(i));
3244 
3245 		if ((etqf & IGC_ETQF_ETYPE_MASK) == etype)
3246 			return i;
3247 	}
3248 
3249 	return -1;
3250 }
3251 
3252 /**
3253  * igc_del_etype_filter() - Delete ethertype filter
3254  * @adapter: Pointer to adapter where the filter should be deleted from
3255  * @etype: Ethertype value
3256  */
3257 static void igc_del_etype_filter(struct igc_adapter *adapter, u16 etype)
3258 {
3259 	struct igc_hw *hw = &adapter->hw;
3260 	int index;
3261 
3262 	index = igc_find_etype_filter(adapter, etype);
3263 	if (index < 0)
3264 		return;
3265 
3266 	wr32(IGC_ETQF(index), 0);
3267 
3268 	netdev_dbg(adapter->netdev, "Delete ethertype filter: etype %04x\n",
3269 		   etype);
3270 }
3271 
3272 static int igc_flex_filter_select(struct igc_adapter *adapter,
3273 				  struct igc_flex_filter *input,
3274 				  u32 *fhft)
3275 {
3276 	struct igc_hw *hw = &adapter->hw;
3277 	u8 fhft_index;
3278 	u32 fhftsl;
3279 
3280 	if (input->index >= MAX_FLEX_FILTER) {
3281 		dev_err(&adapter->pdev->dev, "Wrong Flex Filter index selected!\n");
3282 		return -EINVAL;
3283 	}
3284 
3285 	/* Indirect table select register */
3286 	fhftsl = rd32(IGC_FHFTSL);
3287 	fhftsl &= ~IGC_FHFTSL_FTSL_MASK;
3288 	switch (input->index) {
3289 	case 0 ... 7:
3290 		fhftsl |= 0x00;
3291 		break;
3292 	case 8 ... 15:
3293 		fhftsl |= 0x01;
3294 		break;
3295 	case 16 ... 23:
3296 		fhftsl |= 0x02;
3297 		break;
3298 	case 24 ... 31:
3299 		fhftsl |= 0x03;
3300 		break;
3301 	}
3302 	wr32(IGC_FHFTSL, fhftsl);
3303 
3304 	/* Normalize index down to host table register */
3305 	fhft_index = input->index % 8;
3306 
3307 	*fhft = (fhft_index < 4) ? IGC_FHFT(fhft_index) :
3308 		IGC_FHFT_EXT(fhft_index - 4);
3309 
3310 	return 0;
3311 }
3312 
3313 static int igc_write_flex_filter_ll(struct igc_adapter *adapter,
3314 				    struct igc_flex_filter *input)
3315 {
3316 	struct device *dev = &adapter->pdev->dev;
3317 	struct igc_hw *hw = &adapter->hw;
3318 	u8 *data = input->data;
3319 	u8 *mask = input->mask;
3320 	u32 queuing;
3321 	u32 fhft;
3322 	u32 wufc;
3323 	int ret;
3324 	int i;
3325 
3326 	/* Length has to be aligned to 8. Otherwise the filter will fail. Bail
3327 	 * out early to avoid surprises later.
3328 	 */
3329 	if (input->length % 8 != 0) {
3330 		dev_err(dev, "The length of a flex filter has to be 8 byte aligned!\n");
3331 		return -EINVAL;
3332 	}
3333 
3334 	/* Select corresponding flex filter register and get base for host table. */
3335 	ret = igc_flex_filter_select(adapter, input, &fhft);
3336 	if (ret)
3337 		return ret;
3338 
3339 	/* When adding a filter globally disable flex filter feature. That is
3340 	 * recommended within the datasheet.
3341 	 */
3342 	wufc = rd32(IGC_WUFC);
3343 	wufc &= ~IGC_WUFC_FLEX_HQ;
3344 	wr32(IGC_WUFC, wufc);
3345 
3346 	/* Configure filter */
3347 	queuing = input->length & IGC_FHFT_LENGTH_MASK;
3348 	queuing |= (input->rx_queue << IGC_FHFT_QUEUE_SHIFT) & IGC_FHFT_QUEUE_MASK;
3349 	queuing |= (input->prio << IGC_FHFT_PRIO_SHIFT) & IGC_FHFT_PRIO_MASK;
3350 
3351 	if (input->immediate_irq)
3352 		queuing |= IGC_FHFT_IMM_INT;
3353 
3354 	if (input->drop)
3355 		queuing |= IGC_FHFT_DROP;
3356 
3357 	wr32(fhft + 0xFC, queuing);
3358 
3359 	/* Write data (128 byte) and mask (128 bit) */
3360 	for (i = 0; i < 16; ++i) {
3361 		const size_t data_idx = i * 8;
3362 		const size_t row_idx = i * 16;
3363 		u32 dw0 =
3364 			(data[data_idx + 0] << 0) |
3365 			(data[data_idx + 1] << 8) |
3366 			(data[data_idx + 2] << 16) |
3367 			(data[data_idx + 3] << 24);
3368 		u32 dw1 =
3369 			(data[data_idx + 4] << 0) |
3370 			(data[data_idx + 5] << 8) |
3371 			(data[data_idx + 6] << 16) |
3372 			(data[data_idx + 7] << 24);
3373 		u32 tmp;
3374 
3375 		/* Write row: dw0, dw1 and mask */
3376 		wr32(fhft + row_idx, dw0);
3377 		wr32(fhft + row_idx + 4, dw1);
3378 
3379 		/* mask is only valid for MASK(7, 0) */
3380 		tmp = rd32(fhft + row_idx + 8);
3381 		tmp &= ~GENMASK(7, 0);
3382 		tmp |= mask[i];
3383 		wr32(fhft + row_idx + 8, tmp);
3384 	}
3385 
3386 	/* Enable filter. */
3387 	wufc |= IGC_WUFC_FLEX_HQ;
3388 	if (input->index > 8) {
3389 		/* Filter 0-7 are enabled via WUFC. The other 24 filters are not. */
3390 		u32 wufc_ext = rd32(IGC_WUFC_EXT);
3391 
3392 		wufc_ext |= (IGC_WUFC_EXT_FLX8 << (input->index - 8));
3393 
3394 		wr32(IGC_WUFC_EXT, wufc_ext);
3395 	} else {
3396 		wufc |= (IGC_WUFC_FLX0 << input->index);
3397 	}
3398 	wr32(IGC_WUFC, wufc);
3399 
3400 	dev_dbg(&adapter->pdev->dev, "Added flex filter %u to HW.\n",
3401 		input->index);
3402 
3403 	return 0;
3404 }
3405 
3406 static void igc_flex_filter_add_field(struct igc_flex_filter *flex,
3407 				      const void *src, unsigned int offset,
3408 				      size_t len, const void *mask)
3409 {
3410 	int i;
3411 
3412 	/* data */
3413 	memcpy(&flex->data[offset], src, len);
3414 
3415 	/* mask */
3416 	for (i = 0; i < len; ++i) {
3417 		const unsigned int idx = i + offset;
3418 		const u8 *ptr = mask;
3419 
3420 		if (mask) {
3421 			if (ptr[i] & 0xff)
3422 				flex->mask[idx / 8] |= BIT(idx % 8);
3423 
3424 			continue;
3425 		}
3426 
3427 		flex->mask[idx / 8] |= BIT(idx % 8);
3428 	}
3429 }
3430 
3431 static int igc_find_avail_flex_filter_slot(struct igc_adapter *adapter)
3432 {
3433 	struct igc_hw *hw = &adapter->hw;
3434 	u32 wufc, wufc_ext;
3435 	int i;
3436 
3437 	wufc = rd32(IGC_WUFC);
3438 	wufc_ext = rd32(IGC_WUFC_EXT);
3439 
3440 	for (i = 0; i < MAX_FLEX_FILTER; i++) {
3441 		if (i < 8) {
3442 			if (!(wufc & (IGC_WUFC_FLX0 << i)))
3443 				return i;
3444 		} else {
3445 			if (!(wufc_ext & (IGC_WUFC_EXT_FLX8 << (i - 8))))
3446 				return i;
3447 		}
3448 	}
3449 
3450 	return -ENOSPC;
3451 }
3452 
3453 static bool igc_flex_filter_in_use(struct igc_adapter *adapter)
3454 {
3455 	struct igc_hw *hw = &adapter->hw;
3456 	u32 wufc, wufc_ext;
3457 
3458 	wufc = rd32(IGC_WUFC);
3459 	wufc_ext = rd32(IGC_WUFC_EXT);
3460 
3461 	if (wufc & IGC_WUFC_FILTER_MASK)
3462 		return true;
3463 
3464 	if (wufc_ext & IGC_WUFC_EXT_FILTER_MASK)
3465 		return true;
3466 
3467 	return false;
3468 }
3469 
3470 static int igc_add_flex_filter(struct igc_adapter *adapter,
3471 			       struct igc_nfc_rule *rule)
3472 {
3473 	struct igc_flex_filter flex = { };
3474 	struct igc_nfc_filter *filter = &rule->filter;
3475 	unsigned int eth_offset, user_offset;
3476 	int ret, index;
3477 	bool vlan;
3478 
3479 	index = igc_find_avail_flex_filter_slot(adapter);
3480 	if (index < 0)
3481 		return -ENOSPC;
3482 
3483 	/* Construct the flex filter:
3484 	 *  -> dest_mac [6]
3485 	 *  -> src_mac [6]
3486 	 *  -> tpid [2]
3487 	 *  -> vlan tci [2]
3488 	 *  -> ether type [2]
3489 	 *  -> user data [8]
3490 	 *  -> = 26 bytes => 32 length
3491 	 */
3492 	flex.index    = index;
3493 	flex.length   = 32;
3494 	flex.rx_queue = rule->action;
3495 
3496 	vlan = rule->filter.vlan_tci || rule->filter.vlan_etype;
3497 	eth_offset = vlan ? 16 : 12;
3498 	user_offset = vlan ? 18 : 14;
3499 
3500 	/* Add destination MAC  */
3501 	if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR)
3502 		igc_flex_filter_add_field(&flex, &filter->dst_addr, 0,
3503 					  ETH_ALEN, NULL);
3504 
3505 	/* Add source MAC */
3506 	if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR)
3507 		igc_flex_filter_add_field(&flex, &filter->src_addr, 6,
3508 					  ETH_ALEN, NULL);
3509 
3510 	/* Add VLAN etype */
3511 	if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_ETYPE)
3512 		igc_flex_filter_add_field(&flex, &filter->vlan_etype, 12,
3513 					  sizeof(filter->vlan_etype),
3514 					  NULL);
3515 
3516 	/* Add VLAN TCI */
3517 	if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI)
3518 		igc_flex_filter_add_field(&flex, &filter->vlan_tci, 14,
3519 					  sizeof(filter->vlan_tci), NULL);
3520 
3521 	/* Add Ether type */
3522 	if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) {
3523 		__be16 etype = cpu_to_be16(filter->etype);
3524 
3525 		igc_flex_filter_add_field(&flex, &etype, eth_offset,
3526 					  sizeof(etype), NULL);
3527 	}
3528 
3529 	/* Add user data */
3530 	if (rule->filter.match_flags & IGC_FILTER_FLAG_USER_DATA)
3531 		igc_flex_filter_add_field(&flex, &filter->user_data,
3532 					  user_offset,
3533 					  sizeof(filter->user_data),
3534 					  filter->user_mask);
3535 
3536 	/* Add it down to the hardware and enable it. */
3537 	ret = igc_write_flex_filter_ll(adapter, &flex);
3538 	if (ret)
3539 		return ret;
3540 
3541 	filter->flex_index = index;
3542 
3543 	return 0;
3544 }
3545 
3546 static void igc_del_flex_filter(struct igc_adapter *adapter,
3547 				u16 reg_index)
3548 {
3549 	struct igc_hw *hw = &adapter->hw;
3550 	u32 wufc;
3551 
3552 	/* Just disable the filter. The filter table itself is kept
3553 	 * intact. Another flex_filter_add() should override the "old" data
3554 	 * then.
3555 	 */
3556 	if (reg_index > 8) {
3557 		u32 wufc_ext = rd32(IGC_WUFC_EXT);
3558 
3559 		wufc_ext &= ~(IGC_WUFC_EXT_FLX8 << (reg_index - 8));
3560 		wr32(IGC_WUFC_EXT, wufc_ext);
3561 	} else {
3562 		wufc = rd32(IGC_WUFC);
3563 
3564 		wufc &= ~(IGC_WUFC_FLX0 << reg_index);
3565 		wr32(IGC_WUFC, wufc);
3566 	}
3567 
3568 	if (igc_flex_filter_in_use(adapter))
3569 		return;
3570 
3571 	/* No filters are in use, we may disable flex filters */
3572 	wufc = rd32(IGC_WUFC);
3573 	wufc &= ~IGC_WUFC_FLEX_HQ;
3574 	wr32(IGC_WUFC, wufc);
3575 }
3576 
3577 static int igc_enable_nfc_rule(struct igc_adapter *adapter,
3578 			       struct igc_nfc_rule *rule)
3579 {
3580 	int err;
3581 
3582 	if (rule->flex) {
3583 		return igc_add_flex_filter(adapter, rule);
3584 	}
3585 
3586 	if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) {
3587 		err = igc_add_etype_filter(adapter, rule->filter.etype,
3588 					   rule->action);
3589 		if (err)
3590 			return err;
3591 	}
3592 
3593 	if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR) {
3594 		err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC,
3595 					 rule->filter.src_addr, rule->action);
3596 		if (err)
3597 			return err;
3598 	}
3599 
3600 	if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR) {
3601 		err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST,
3602 					 rule->filter.dst_addr, rule->action);
3603 		if (err)
3604 			return err;
3605 	}
3606 
3607 	if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) {
3608 		int prio = (rule->filter.vlan_tci & VLAN_PRIO_MASK) >>
3609 			   VLAN_PRIO_SHIFT;
3610 
3611 		err = igc_add_vlan_prio_filter(adapter, prio, rule->action);
3612 		if (err)
3613 			return err;
3614 	}
3615 
3616 	return 0;
3617 }
3618 
3619 static void igc_disable_nfc_rule(struct igc_adapter *adapter,
3620 				 const struct igc_nfc_rule *rule)
3621 {
3622 	if (rule->flex) {
3623 		igc_del_flex_filter(adapter, rule->filter.flex_index);
3624 		return;
3625 	}
3626 
3627 	if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE)
3628 		igc_del_etype_filter(adapter, rule->filter.etype);
3629 
3630 	if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) {
3631 		int prio = (rule->filter.vlan_tci & VLAN_PRIO_MASK) >>
3632 			   VLAN_PRIO_SHIFT;
3633 
3634 		igc_del_vlan_prio_filter(adapter, prio);
3635 	}
3636 
3637 	if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR)
3638 		igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC,
3639 				   rule->filter.src_addr);
3640 
3641 	if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR)
3642 		igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST,
3643 				   rule->filter.dst_addr);
3644 }
3645 
3646 /**
3647  * igc_get_nfc_rule() - Get NFC rule
3648  * @adapter: Pointer to adapter
3649  * @location: Rule location
3650  *
3651  * Context: Expects adapter->nfc_rule_lock to be held by caller.
3652  *
3653  * Return: Pointer to NFC rule at @location. If not found, NULL.
3654  */
3655 struct igc_nfc_rule *igc_get_nfc_rule(struct igc_adapter *adapter,
3656 				      u32 location)
3657 {
3658 	struct igc_nfc_rule *rule;
3659 
3660 	list_for_each_entry(rule, &adapter->nfc_rule_list, list) {
3661 		if (rule->location == location)
3662 			return rule;
3663 		if (rule->location > location)
3664 			break;
3665 	}
3666 
3667 	return NULL;
3668 }
3669 
3670 /**
3671  * igc_del_nfc_rule() - Delete NFC rule
3672  * @adapter: Pointer to adapter
3673  * @rule: Pointer to rule to be deleted
3674  *
3675  * Disable NFC rule in hardware and delete it from adapter.
3676  *
3677  * Context: Expects adapter->nfc_rule_lock to be held by caller.
3678  */
3679 void igc_del_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule)
3680 {
3681 	igc_disable_nfc_rule(adapter, rule);
3682 
3683 	list_del(&rule->list);
3684 	adapter->nfc_rule_count--;
3685 
3686 	kfree(rule);
3687 }
3688 
3689 static void igc_flush_nfc_rules(struct igc_adapter *adapter)
3690 {
3691 	struct igc_nfc_rule *rule, *tmp;
3692 
3693 	mutex_lock(&adapter->nfc_rule_lock);
3694 
3695 	list_for_each_entry_safe(rule, tmp, &adapter->nfc_rule_list, list)
3696 		igc_del_nfc_rule(adapter, rule);
3697 
3698 	mutex_unlock(&adapter->nfc_rule_lock);
3699 }
3700 
3701 /**
3702  * igc_add_nfc_rule() - Add NFC rule
3703  * @adapter: Pointer to adapter
3704  * @rule: Pointer to rule to be added
3705  *
3706  * Enable NFC rule in hardware and add it to adapter.
3707  *
3708  * Context: Expects adapter->nfc_rule_lock to be held by caller.
3709  *
3710  * Return: 0 on success, negative errno on failure.
3711  */
3712 int igc_add_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule)
3713 {
3714 	struct igc_nfc_rule *pred, *cur;
3715 	int err;
3716 
3717 	err = igc_enable_nfc_rule(adapter, rule);
3718 	if (err)
3719 		return err;
3720 
3721 	pred = NULL;
3722 	list_for_each_entry(cur, &adapter->nfc_rule_list, list) {
3723 		if (cur->location >= rule->location)
3724 			break;
3725 		pred = cur;
3726 	}
3727 
3728 	list_add(&rule->list, pred ? &pred->list : &adapter->nfc_rule_list);
3729 	adapter->nfc_rule_count++;
3730 	return 0;
3731 }
3732 
3733 static void igc_restore_nfc_rules(struct igc_adapter *adapter)
3734 {
3735 	struct igc_nfc_rule *rule;
3736 
3737 	mutex_lock(&adapter->nfc_rule_lock);
3738 
3739 	list_for_each_entry_reverse(rule, &adapter->nfc_rule_list, list)
3740 		igc_enable_nfc_rule(adapter, rule);
3741 
3742 	mutex_unlock(&adapter->nfc_rule_lock);
3743 }
3744 
3745 static int igc_uc_sync(struct net_device *netdev, const unsigned char *addr)
3746 {
3747 	struct igc_adapter *adapter = netdev_priv(netdev);
3748 
3749 	return igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr, -1);
3750 }
3751 
3752 static int igc_uc_unsync(struct net_device *netdev, const unsigned char *addr)
3753 {
3754 	struct igc_adapter *adapter = netdev_priv(netdev);
3755 
3756 	igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr);
3757 	return 0;
3758 }
3759 
3760 /**
3761  * igc_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
3762  * @netdev: network interface device structure
3763  *
3764  * The set_rx_mode entry point is called whenever the unicast or multicast
3765  * address lists or the network interface flags are updated.  This routine is
3766  * responsible for configuring the hardware for proper unicast, multicast,
3767  * promiscuous mode, and all-multi behavior.
3768  */
3769 static void igc_set_rx_mode(struct net_device *netdev)
3770 {
3771 	struct igc_adapter *adapter = netdev_priv(netdev);
3772 	struct igc_hw *hw = &adapter->hw;
3773 	u32 rctl = 0, rlpml = MAX_JUMBO_FRAME_SIZE;
3774 	int count;
3775 
3776 	/* Check for Promiscuous and All Multicast modes */
3777 	if (netdev->flags & IFF_PROMISC) {
3778 		rctl |= IGC_RCTL_UPE | IGC_RCTL_MPE;
3779 	} else {
3780 		if (netdev->flags & IFF_ALLMULTI) {
3781 			rctl |= IGC_RCTL_MPE;
3782 		} else {
3783 			/* Write addresses to the MTA, if the attempt fails
3784 			 * then we should just turn on promiscuous mode so
3785 			 * that we can at least receive multicast traffic
3786 			 */
3787 			count = igc_write_mc_addr_list(netdev);
3788 			if (count < 0)
3789 				rctl |= IGC_RCTL_MPE;
3790 		}
3791 	}
3792 
3793 	/* Write addresses to available RAR registers, if there is not
3794 	 * sufficient space to store all the addresses then enable
3795 	 * unicast promiscuous mode
3796 	 */
3797 	if (__dev_uc_sync(netdev, igc_uc_sync, igc_uc_unsync))
3798 		rctl |= IGC_RCTL_UPE;
3799 
3800 	/* update state of unicast and multicast */
3801 	rctl |= rd32(IGC_RCTL) & ~(IGC_RCTL_UPE | IGC_RCTL_MPE);
3802 	wr32(IGC_RCTL, rctl);
3803 
3804 #if (PAGE_SIZE < 8192)
3805 	if (adapter->max_frame_size <= IGC_MAX_FRAME_BUILD_SKB)
3806 		rlpml = IGC_MAX_FRAME_BUILD_SKB;
3807 #endif
3808 	wr32(IGC_RLPML, rlpml);
3809 }
3810 
3811 /**
3812  * igc_configure - configure the hardware for RX and TX
3813  * @adapter: private board structure
3814  */
3815 static void igc_configure(struct igc_adapter *adapter)
3816 {
3817 	struct net_device *netdev = adapter->netdev;
3818 	int i = 0;
3819 
3820 	igc_get_hw_control(adapter);
3821 	igc_set_rx_mode(netdev);
3822 
3823 	igc_restore_vlan(adapter);
3824 
3825 	igc_setup_tctl(adapter);
3826 	igc_setup_mrqc(adapter);
3827 	igc_setup_rctl(adapter);
3828 
3829 	igc_set_default_mac_filter(adapter);
3830 	igc_restore_nfc_rules(adapter);
3831 
3832 	igc_configure_tx(adapter);
3833 	igc_configure_rx(adapter);
3834 
3835 	igc_rx_fifo_flush_base(&adapter->hw);
3836 
3837 	/* call igc_desc_unused which always leaves
3838 	 * at least 1 descriptor unused to make sure
3839 	 * next_to_use != next_to_clean
3840 	 */
3841 	for (i = 0; i < adapter->num_rx_queues; i++) {
3842 		struct igc_ring *ring = adapter->rx_ring[i];
3843 
3844 		if (ring->xsk_pool)
3845 			igc_alloc_rx_buffers_zc(ring, igc_desc_unused(ring));
3846 		else
3847 			igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
3848 	}
3849 }
3850 
3851 /**
3852  * igc_write_ivar - configure ivar for given MSI-X vector
3853  * @hw: pointer to the HW structure
3854  * @msix_vector: vector number we are allocating to a given ring
3855  * @index: row index of IVAR register to write within IVAR table
3856  * @offset: column offset of in IVAR, should be multiple of 8
3857  *
3858  * The IVAR table consists of 2 columns,
3859  * each containing an cause allocation for an Rx and Tx ring, and a
3860  * variable number of rows depending on the number of queues supported.
3861  */
3862 static void igc_write_ivar(struct igc_hw *hw, int msix_vector,
3863 			   int index, int offset)
3864 {
3865 	u32 ivar = array_rd32(IGC_IVAR0, index);
3866 
3867 	/* clear any bits that are currently set */
3868 	ivar &= ~((u32)0xFF << offset);
3869 
3870 	/* write vector and valid bit */
3871 	ivar |= (msix_vector | IGC_IVAR_VALID) << offset;
3872 
3873 	array_wr32(IGC_IVAR0, index, ivar);
3874 }
3875 
3876 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector)
3877 {
3878 	struct igc_adapter *adapter = q_vector->adapter;
3879 	struct igc_hw *hw = &adapter->hw;
3880 	int rx_queue = IGC_N0_QUEUE;
3881 	int tx_queue = IGC_N0_QUEUE;
3882 
3883 	if (q_vector->rx.ring)
3884 		rx_queue = q_vector->rx.ring->reg_idx;
3885 	if (q_vector->tx.ring)
3886 		tx_queue = q_vector->tx.ring->reg_idx;
3887 
3888 	switch (hw->mac.type) {
3889 	case igc_i225:
3890 		if (rx_queue > IGC_N0_QUEUE)
3891 			igc_write_ivar(hw, msix_vector,
3892 				       rx_queue >> 1,
3893 				       (rx_queue & 0x1) << 4);
3894 		if (tx_queue > IGC_N0_QUEUE)
3895 			igc_write_ivar(hw, msix_vector,
3896 				       tx_queue >> 1,
3897 				       ((tx_queue & 0x1) << 4) + 8);
3898 		q_vector->eims_value = BIT(msix_vector);
3899 		break;
3900 	default:
3901 		WARN_ONCE(hw->mac.type != igc_i225, "Wrong MAC type\n");
3902 		break;
3903 	}
3904 
3905 	/* add q_vector eims value to global eims_enable_mask */
3906 	adapter->eims_enable_mask |= q_vector->eims_value;
3907 
3908 	/* configure q_vector to set itr on first interrupt */
3909 	q_vector->set_itr = 1;
3910 }
3911 
3912 /**
3913  * igc_configure_msix - Configure MSI-X hardware
3914  * @adapter: Pointer to adapter structure
3915  *
3916  * igc_configure_msix sets up the hardware to properly
3917  * generate MSI-X interrupts.
3918  */
3919 static void igc_configure_msix(struct igc_adapter *adapter)
3920 {
3921 	struct igc_hw *hw = &adapter->hw;
3922 	int i, vector = 0;
3923 	u32 tmp;
3924 
3925 	adapter->eims_enable_mask = 0;
3926 
3927 	/* set vector for other causes, i.e. link changes */
3928 	switch (hw->mac.type) {
3929 	case igc_i225:
3930 		/* Turn on MSI-X capability first, or our settings
3931 		 * won't stick.  And it will take days to debug.
3932 		 */
3933 		wr32(IGC_GPIE, IGC_GPIE_MSIX_MODE |
3934 		     IGC_GPIE_PBA | IGC_GPIE_EIAME |
3935 		     IGC_GPIE_NSICR);
3936 
3937 		/* enable msix_other interrupt */
3938 		adapter->eims_other = BIT(vector);
3939 		tmp = (vector++ | IGC_IVAR_VALID) << 8;
3940 
3941 		wr32(IGC_IVAR_MISC, tmp);
3942 		break;
3943 	default:
3944 		/* do nothing, since nothing else supports MSI-X */
3945 		break;
3946 	} /* switch (hw->mac.type) */
3947 
3948 	adapter->eims_enable_mask |= adapter->eims_other;
3949 
3950 	for (i = 0; i < adapter->num_q_vectors; i++)
3951 		igc_assign_vector(adapter->q_vector[i], vector++);
3952 
3953 	wrfl();
3954 }
3955 
3956 /**
3957  * igc_irq_enable - Enable default interrupt generation settings
3958  * @adapter: board private structure
3959  */
3960 static void igc_irq_enable(struct igc_adapter *adapter)
3961 {
3962 	struct igc_hw *hw = &adapter->hw;
3963 
3964 	if (adapter->msix_entries) {
3965 		u32 ims = IGC_IMS_LSC | IGC_IMS_DOUTSYNC | IGC_IMS_DRSTA;
3966 		u32 regval = rd32(IGC_EIAC);
3967 
3968 		wr32(IGC_EIAC, regval | adapter->eims_enable_mask);
3969 		regval = rd32(IGC_EIAM);
3970 		wr32(IGC_EIAM, regval | adapter->eims_enable_mask);
3971 		wr32(IGC_EIMS, adapter->eims_enable_mask);
3972 		wr32(IGC_IMS, ims);
3973 	} else {
3974 		wr32(IGC_IMS, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3975 		wr32(IGC_IAM, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3976 	}
3977 }
3978 
3979 /**
3980  * igc_irq_disable - Mask off interrupt generation on the NIC
3981  * @adapter: board private structure
3982  */
3983 static void igc_irq_disable(struct igc_adapter *adapter)
3984 {
3985 	struct igc_hw *hw = &adapter->hw;
3986 
3987 	if (adapter->msix_entries) {
3988 		u32 regval = rd32(IGC_EIAM);
3989 
3990 		wr32(IGC_EIAM, regval & ~adapter->eims_enable_mask);
3991 		wr32(IGC_EIMC, adapter->eims_enable_mask);
3992 		regval = rd32(IGC_EIAC);
3993 		wr32(IGC_EIAC, regval & ~adapter->eims_enable_mask);
3994 	}
3995 
3996 	wr32(IGC_IAM, 0);
3997 	wr32(IGC_IMC, ~0);
3998 	wrfl();
3999 
4000 	if (adapter->msix_entries) {
4001 		int vector = 0, i;
4002 
4003 		synchronize_irq(adapter->msix_entries[vector++].vector);
4004 
4005 		for (i = 0; i < adapter->num_q_vectors; i++)
4006 			synchronize_irq(adapter->msix_entries[vector++].vector);
4007 	} else {
4008 		synchronize_irq(adapter->pdev->irq);
4009 	}
4010 }
4011 
4012 void igc_set_flag_queue_pairs(struct igc_adapter *adapter,
4013 			      const u32 max_rss_queues)
4014 {
4015 	/* Determine if we need to pair queues. */
4016 	/* If rss_queues > half of max_rss_queues, pair the queues in
4017 	 * order to conserve interrupts due to limited supply.
4018 	 */
4019 	if (adapter->rss_queues > (max_rss_queues / 2))
4020 		adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
4021 	else
4022 		adapter->flags &= ~IGC_FLAG_QUEUE_PAIRS;
4023 }
4024 
4025 unsigned int igc_get_max_rss_queues(struct igc_adapter *adapter)
4026 {
4027 	return IGC_MAX_RX_QUEUES;
4028 }
4029 
4030 static void igc_init_queue_configuration(struct igc_adapter *adapter)
4031 {
4032 	u32 max_rss_queues;
4033 
4034 	max_rss_queues = igc_get_max_rss_queues(adapter);
4035 	adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
4036 
4037 	igc_set_flag_queue_pairs(adapter, max_rss_queues);
4038 }
4039 
4040 /**
4041  * igc_reset_q_vector - Reset config for interrupt vector
4042  * @adapter: board private structure to initialize
4043  * @v_idx: Index of vector to be reset
4044  *
4045  * If NAPI is enabled it will delete any references to the
4046  * NAPI struct. This is preparation for igc_free_q_vector.
4047  */
4048 static void igc_reset_q_vector(struct igc_adapter *adapter, int v_idx)
4049 {
4050 	struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
4051 
4052 	/* if we're coming from igc_set_interrupt_capability, the vectors are
4053 	 * not yet allocated
4054 	 */
4055 	if (!q_vector)
4056 		return;
4057 
4058 	if (q_vector->tx.ring)
4059 		adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
4060 
4061 	if (q_vector->rx.ring)
4062 		adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
4063 
4064 	netif_napi_del(&q_vector->napi);
4065 }
4066 
4067 /**
4068  * igc_free_q_vector - Free memory allocated for specific interrupt vector
4069  * @adapter: board private structure to initialize
4070  * @v_idx: Index of vector to be freed
4071  *
4072  * This function frees the memory allocated to the q_vector.
4073  */
4074 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx)
4075 {
4076 	struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
4077 
4078 	adapter->q_vector[v_idx] = NULL;
4079 
4080 	/* igc_get_stats64() might access the rings on this vector,
4081 	 * we must wait a grace period before freeing it.
4082 	 */
4083 	if (q_vector)
4084 		kfree_rcu(q_vector, rcu);
4085 }
4086 
4087 /**
4088  * igc_free_q_vectors - Free memory allocated for interrupt vectors
4089  * @adapter: board private structure to initialize
4090  *
4091  * This function frees the memory allocated to the q_vectors.  In addition if
4092  * NAPI is enabled it will delete any references to the NAPI struct prior
4093  * to freeing the q_vector.
4094  */
4095 static void igc_free_q_vectors(struct igc_adapter *adapter)
4096 {
4097 	int v_idx = adapter->num_q_vectors;
4098 
4099 	adapter->num_tx_queues = 0;
4100 	adapter->num_rx_queues = 0;
4101 	adapter->num_q_vectors = 0;
4102 
4103 	while (v_idx--) {
4104 		igc_reset_q_vector(adapter, v_idx);
4105 		igc_free_q_vector(adapter, v_idx);
4106 	}
4107 }
4108 
4109 /**
4110  * igc_update_itr - update the dynamic ITR value based on statistics
4111  * @q_vector: pointer to q_vector
4112  * @ring_container: ring info to update the itr for
4113  *
4114  * Stores a new ITR value based on packets and byte
4115  * counts during the last interrupt.  The advantage of per interrupt
4116  * computation is faster updates and more accurate ITR for the current
4117  * traffic pattern.  Constants in this function were computed
4118  * based on theoretical maximum wire speed and thresholds were set based
4119  * on testing data as well as attempting to minimize response time
4120  * while increasing bulk throughput.
4121  * NOTE: These calculations are only valid when operating in a single-
4122  * queue environment.
4123  */
4124 static void igc_update_itr(struct igc_q_vector *q_vector,
4125 			   struct igc_ring_container *ring_container)
4126 {
4127 	unsigned int packets = ring_container->total_packets;
4128 	unsigned int bytes = ring_container->total_bytes;
4129 	u8 itrval = ring_container->itr;
4130 
4131 	/* no packets, exit with status unchanged */
4132 	if (packets == 0)
4133 		return;
4134 
4135 	switch (itrval) {
4136 	case lowest_latency:
4137 		/* handle TSO and jumbo frames */
4138 		if (bytes / packets > 8000)
4139 			itrval = bulk_latency;
4140 		else if ((packets < 5) && (bytes > 512))
4141 			itrval = low_latency;
4142 		break;
4143 	case low_latency:  /* 50 usec aka 20000 ints/s */
4144 		if (bytes > 10000) {
4145 			/* this if handles the TSO accounting */
4146 			if (bytes / packets > 8000)
4147 				itrval = bulk_latency;
4148 			else if ((packets < 10) || ((bytes / packets) > 1200))
4149 				itrval = bulk_latency;
4150 			else if ((packets > 35))
4151 				itrval = lowest_latency;
4152 		} else if (bytes / packets > 2000) {
4153 			itrval = bulk_latency;
4154 		} else if (packets <= 2 && bytes < 512) {
4155 			itrval = lowest_latency;
4156 		}
4157 		break;
4158 	case bulk_latency: /* 250 usec aka 4000 ints/s */
4159 		if (bytes > 25000) {
4160 			if (packets > 35)
4161 				itrval = low_latency;
4162 		} else if (bytes < 1500) {
4163 			itrval = low_latency;
4164 		}
4165 		break;
4166 	}
4167 
4168 	/* clear work counters since we have the values we need */
4169 	ring_container->total_bytes = 0;
4170 	ring_container->total_packets = 0;
4171 
4172 	/* write updated itr to ring container */
4173 	ring_container->itr = itrval;
4174 }
4175 
4176 static void igc_set_itr(struct igc_q_vector *q_vector)
4177 {
4178 	struct igc_adapter *adapter = q_vector->adapter;
4179 	u32 new_itr = q_vector->itr_val;
4180 	u8 current_itr = 0;
4181 
4182 	/* for non-gigabit speeds, just fix the interrupt rate at 4000 */
4183 	switch (adapter->link_speed) {
4184 	case SPEED_10:
4185 	case SPEED_100:
4186 		current_itr = 0;
4187 		new_itr = IGC_4K_ITR;
4188 		goto set_itr_now;
4189 	default:
4190 		break;
4191 	}
4192 
4193 	igc_update_itr(q_vector, &q_vector->tx);
4194 	igc_update_itr(q_vector, &q_vector->rx);
4195 
4196 	current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
4197 
4198 	/* conservative mode (itr 3) eliminates the lowest_latency setting */
4199 	if (current_itr == lowest_latency &&
4200 	    ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4201 	    (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4202 		current_itr = low_latency;
4203 
4204 	switch (current_itr) {
4205 	/* counts and packets in update_itr are dependent on these numbers */
4206 	case lowest_latency:
4207 		new_itr = IGC_70K_ITR; /* 70,000 ints/sec */
4208 		break;
4209 	case low_latency:
4210 		new_itr = IGC_20K_ITR; /* 20,000 ints/sec */
4211 		break;
4212 	case bulk_latency:
4213 		new_itr = IGC_4K_ITR;  /* 4,000 ints/sec */
4214 		break;
4215 	default:
4216 		break;
4217 	}
4218 
4219 set_itr_now:
4220 	if (new_itr != q_vector->itr_val) {
4221 		/* this attempts to bias the interrupt rate towards Bulk
4222 		 * by adding intermediate steps when interrupt rate is
4223 		 * increasing
4224 		 */
4225 		new_itr = new_itr > q_vector->itr_val ?
4226 			  max((new_itr * q_vector->itr_val) /
4227 			  (new_itr + (q_vector->itr_val >> 2)),
4228 			  new_itr) : new_itr;
4229 		/* Don't write the value here; it resets the adapter's
4230 		 * internal timer, and causes us to delay far longer than
4231 		 * we should between interrupts.  Instead, we write the ITR
4232 		 * value at the beginning of the next interrupt so the timing
4233 		 * ends up being correct.
4234 		 */
4235 		q_vector->itr_val = new_itr;
4236 		q_vector->set_itr = 1;
4237 	}
4238 }
4239 
4240 static void igc_reset_interrupt_capability(struct igc_adapter *adapter)
4241 {
4242 	int v_idx = adapter->num_q_vectors;
4243 
4244 	if (adapter->msix_entries) {
4245 		pci_disable_msix(adapter->pdev);
4246 		kfree(adapter->msix_entries);
4247 		adapter->msix_entries = NULL;
4248 	} else if (adapter->flags & IGC_FLAG_HAS_MSI) {
4249 		pci_disable_msi(adapter->pdev);
4250 	}
4251 
4252 	while (v_idx--)
4253 		igc_reset_q_vector(adapter, v_idx);
4254 }
4255 
4256 /**
4257  * igc_set_interrupt_capability - set MSI or MSI-X if supported
4258  * @adapter: Pointer to adapter structure
4259  * @msix: boolean value for MSI-X capability
4260  *
4261  * Attempt to configure interrupts using the best available
4262  * capabilities of the hardware and kernel.
4263  */
4264 static void igc_set_interrupt_capability(struct igc_adapter *adapter,
4265 					 bool msix)
4266 {
4267 	int numvecs, i;
4268 	int err;
4269 
4270 	if (!msix)
4271 		goto msi_only;
4272 	adapter->flags |= IGC_FLAG_HAS_MSIX;
4273 
4274 	/* Number of supported queues. */
4275 	adapter->num_rx_queues = adapter->rss_queues;
4276 
4277 	adapter->num_tx_queues = adapter->rss_queues;
4278 
4279 	/* start with one vector for every Rx queue */
4280 	numvecs = adapter->num_rx_queues;
4281 
4282 	/* if Tx handler is separate add 1 for every Tx queue */
4283 	if (!(adapter->flags & IGC_FLAG_QUEUE_PAIRS))
4284 		numvecs += adapter->num_tx_queues;
4285 
4286 	/* store the number of vectors reserved for queues */
4287 	adapter->num_q_vectors = numvecs;
4288 
4289 	/* add 1 vector for link status interrupts */
4290 	numvecs++;
4291 
4292 	adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
4293 					GFP_KERNEL);
4294 
4295 	if (!adapter->msix_entries)
4296 		return;
4297 
4298 	/* populate entry values */
4299 	for (i = 0; i < numvecs; i++)
4300 		adapter->msix_entries[i].entry = i;
4301 
4302 	err = pci_enable_msix_range(adapter->pdev,
4303 				    adapter->msix_entries,
4304 				    numvecs,
4305 				    numvecs);
4306 	if (err > 0)
4307 		return;
4308 
4309 	kfree(adapter->msix_entries);
4310 	adapter->msix_entries = NULL;
4311 
4312 	igc_reset_interrupt_capability(adapter);
4313 
4314 msi_only:
4315 	adapter->flags &= ~IGC_FLAG_HAS_MSIX;
4316 
4317 	adapter->rss_queues = 1;
4318 	adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
4319 	adapter->num_rx_queues = 1;
4320 	adapter->num_tx_queues = 1;
4321 	adapter->num_q_vectors = 1;
4322 	if (!pci_enable_msi(adapter->pdev))
4323 		adapter->flags |= IGC_FLAG_HAS_MSI;
4324 }
4325 
4326 /**
4327  * igc_update_ring_itr - update the dynamic ITR value based on packet size
4328  * @q_vector: pointer to q_vector
4329  *
4330  * Stores a new ITR value based on strictly on packet size.  This
4331  * algorithm is less sophisticated than that used in igc_update_itr,
4332  * due to the difficulty of synchronizing statistics across multiple
4333  * receive rings.  The divisors and thresholds used by this function
4334  * were determined based on theoretical maximum wire speed and testing
4335  * data, in order to minimize response time while increasing bulk
4336  * throughput.
4337  * NOTE: This function is called only when operating in a multiqueue
4338  * receive environment.
4339  */
4340 static void igc_update_ring_itr(struct igc_q_vector *q_vector)
4341 {
4342 	struct igc_adapter *adapter = q_vector->adapter;
4343 	int new_val = q_vector->itr_val;
4344 	int avg_wire_size = 0;
4345 	unsigned int packets;
4346 
4347 	/* For non-gigabit speeds, just fix the interrupt rate at 4000
4348 	 * ints/sec - ITR timer value of 120 ticks.
4349 	 */
4350 	switch (adapter->link_speed) {
4351 	case SPEED_10:
4352 	case SPEED_100:
4353 		new_val = IGC_4K_ITR;
4354 		goto set_itr_val;
4355 	default:
4356 		break;
4357 	}
4358 
4359 	packets = q_vector->rx.total_packets;
4360 	if (packets)
4361 		avg_wire_size = q_vector->rx.total_bytes / packets;
4362 
4363 	packets = q_vector->tx.total_packets;
4364 	if (packets)
4365 		avg_wire_size = max_t(u32, avg_wire_size,
4366 				      q_vector->tx.total_bytes / packets);
4367 
4368 	/* if avg_wire_size isn't set no work was done */
4369 	if (!avg_wire_size)
4370 		goto clear_counts;
4371 
4372 	/* Add 24 bytes to size to account for CRC, preamble, and gap */
4373 	avg_wire_size += 24;
4374 
4375 	/* Don't starve jumbo frames */
4376 	avg_wire_size = min(avg_wire_size, 3000);
4377 
4378 	/* Give a little boost to mid-size frames */
4379 	if (avg_wire_size > 300 && avg_wire_size < 1200)
4380 		new_val = avg_wire_size / 3;
4381 	else
4382 		new_val = avg_wire_size / 2;
4383 
4384 	/* conservative mode (itr 3) eliminates the lowest_latency setting */
4385 	if (new_val < IGC_20K_ITR &&
4386 	    ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4387 	    (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4388 		new_val = IGC_20K_ITR;
4389 
4390 set_itr_val:
4391 	if (new_val != q_vector->itr_val) {
4392 		q_vector->itr_val = new_val;
4393 		q_vector->set_itr = 1;
4394 	}
4395 clear_counts:
4396 	q_vector->rx.total_bytes = 0;
4397 	q_vector->rx.total_packets = 0;
4398 	q_vector->tx.total_bytes = 0;
4399 	q_vector->tx.total_packets = 0;
4400 }
4401 
4402 static void igc_ring_irq_enable(struct igc_q_vector *q_vector)
4403 {
4404 	struct igc_adapter *adapter = q_vector->adapter;
4405 	struct igc_hw *hw = &adapter->hw;
4406 
4407 	if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
4408 	    (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
4409 		if (adapter->num_q_vectors == 1)
4410 			igc_set_itr(q_vector);
4411 		else
4412 			igc_update_ring_itr(q_vector);
4413 	}
4414 
4415 	if (!test_bit(__IGC_DOWN, &adapter->state)) {
4416 		if (adapter->msix_entries)
4417 			wr32(IGC_EIMS, q_vector->eims_value);
4418 		else
4419 			igc_irq_enable(adapter);
4420 	}
4421 }
4422 
4423 static void igc_add_ring(struct igc_ring *ring,
4424 			 struct igc_ring_container *head)
4425 {
4426 	head->ring = ring;
4427 	head->count++;
4428 }
4429 
4430 /**
4431  * igc_cache_ring_register - Descriptor ring to register mapping
4432  * @adapter: board private structure to initialize
4433  *
4434  * Once we know the feature-set enabled for the device, we'll cache
4435  * the register offset the descriptor ring is assigned to.
4436  */
4437 static void igc_cache_ring_register(struct igc_adapter *adapter)
4438 {
4439 	int i = 0, j = 0;
4440 
4441 	switch (adapter->hw.mac.type) {
4442 	case igc_i225:
4443 	default:
4444 		for (; i < adapter->num_rx_queues; i++)
4445 			adapter->rx_ring[i]->reg_idx = i;
4446 		for (; j < adapter->num_tx_queues; j++)
4447 			adapter->tx_ring[j]->reg_idx = j;
4448 		break;
4449 	}
4450 }
4451 
4452 /**
4453  * igc_poll - NAPI Rx polling callback
4454  * @napi: napi polling structure
4455  * @budget: count of how many packets we should handle
4456  */
4457 static int igc_poll(struct napi_struct *napi, int budget)
4458 {
4459 	struct igc_q_vector *q_vector = container_of(napi,
4460 						     struct igc_q_vector,
4461 						     napi);
4462 	struct igc_ring *rx_ring = q_vector->rx.ring;
4463 	bool clean_complete = true;
4464 	int work_done = 0;
4465 
4466 	if (q_vector->tx.ring)
4467 		clean_complete = igc_clean_tx_irq(q_vector, budget);
4468 
4469 	if (rx_ring) {
4470 		int cleaned = rx_ring->xsk_pool ?
4471 			      igc_clean_rx_irq_zc(q_vector, budget) :
4472 			      igc_clean_rx_irq(q_vector, budget);
4473 
4474 		work_done += cleaned;
4475 		if (cleaned >= budget)
4476 			clean_complete = false;
4477 	}
4478 
4479 	/* If all work not completed, return budget and keep polling */
4480 	if (!clean_complete)
4481 		return budget;
4482 
4483 	/* Exit the polling mode, but don't re-enable interrupts if stack might
4484 	 * poll us due to busy-polling
4485 	 */
4486 	if (likely(napi_complete_done(napi, work_done)))
4487 		igc_ring_irq_enable(q_vector);
4488 
4489 	return min(work_done, budget - 1);
4490 }
4491 
4492 /**
4493  * igc_alloc_q_vector - Allocate memory for a single interrupt vector
4494  * @adapter: board private structure to initialize
4495  * @v_count: q_vectors allocated on adapter, used for ring interleaving
4496  * @v_idx: index of vector in adapter struct
4497  * @txr_count: total number of Tx rings to allocate
4498  * @txr_idx: index of first Tx ring to allocate
4499  * @rxr_count: total number of Rx rings to allocate
4500  * @rxr_idx: index of first Rx ring to allocate
4501  *
4502  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
4503  */
4504 static int igc_alloc_q_vector(struct igc_adapter *adapter,
4505 			      unsigned int v_count, unsigned int v_idx,
4506 			      unsigned int txr_count, unsigned int txr_idx,
4507 			      unsigned int rxr_count, unsigned int rxr_idx)
4508 {
4509 	struct igc_q_vector *q_vector;
4510 	struct igc_ring *ring;
4511 	int ring_count;
4512 
4513 	/* igc only supports 1 Tx and/or 1 Rx queue per vector */
4514 	if (txr_count > 1 || rxr_count > 1)
4515 		return -ENOMEM;
4516 
4517 	ring_count = txr_count + rxr_count;
4518 
4519 	/* allocate q_vector and rings */
4520 	q_vector = adapter->q_vector[v_idx];
4521 	if (!q_vector)
4522 		q_vector = kzalloc(struct_size(q_vector, ring, ring_count),
4523 				   GFP_KERNEL);
4524 	else
4525 		memset(q_vector, 0, struct_size(q_vector, ring, ring_count));
4526 	if (!q_vector)
4527 		return -ENOMEM;
4528 
4529 	/* initialize NAPI */
4530 	netif_napi_add(adapter->netdev, &q_vector->napi, igc_poll);
4531 
4532 	/* tie q_vector and adapter together */
4533 	adapter->q_vector[v_idx] = q_vector;
4534 	q_vector->adapter = adapter;
4535 
4536 	/* initialize work limits */
4537 	q_vector->tx.work_limit = adapter->tx_work_limit;
4538 
4539 	/* initialize ITR configuration */
4540 	q_vector->itr_register = adapter->io_addr + IGC_EITR(0);
4541 	q_vector->itr_val = IGC_START_ITR;
4542 
4543 	/* initialize pointer to rings */
4544 	ring = q_vector->ring;
4545 
4546 	/* initialize ITR */
4547 	if (rxr_count) {
4548 		/* rx or rx/tx vector */
4549 		if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
4550 			q_vector->itr_val = adapter->rx_itr_setting;
4551 	} else {
4552 		/* tx only vector */
4553 		if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
4554 			q_vector->itr_val = adapter->tx_itr_setting;
4555 	}
4556 
4557 	if (txr_count) {
4558 		/* assign generic ring traits */
4559 		ring->dev = &adapter->pdev->dev;
4560 		ring->netdev = adapter->netdev;
4561 
4562 		/* configure backlink on ring */
4563 		ring->q_vector = q_vector;
4564 
4565 		/* update q_vector Tx values */
4566 		igc_add_ring(ring, &q_vector->tx);
4567 
4568 		/* apply Tx specific ring traits */
4569 		ring->count = adapter->tx_ring_count;
4570 		ring->queue_index = txr_idx;
4571 
4572 		/* assign ring to adapter */
4573 		adapter->tx_ring[txr_idx] = ring;
4574 
4575 		/* push pointer to next ring */
4576 		ring++;
4577 	}
4578 
4579 	if (rxr_count) {
4580 		/* assign generic ring traits */
4581 		ring->dev = &adapter->pdev->dev;
4582 		ring->netdev = adapter->netdev;
4583 
4584 		/* configure backlink on ring */
4585 		ring->q_vector = q_vector;
4586 
4587 		/* update q_vector Rx values */
4588 		igc_add_ring(ring, &q_vector->rx);
4589 
4590 		/* apply Rx specific ring traits */
4591 		ring->count = adapter->rx_ring_count;
4592 		ring->queue_index = rxr_idx;
4593 
4594 		/* assign ring to adapter */
4595 		adapter->rx_ring[rxr_idx] = ring;
4596 	}
4597 
4598 	return 0;
4599 }
4600 
4601 /**
4602  * igc_alloc_q_vectors - Allocate memory for interrupt vectors
4603  * @adapter: board private structure to initialize
4604  *
4605  * We allocate one q_vector per queue interrupt.  If allocation fails we
4606  * return -ENOMEM.
4607  */
4608 static int igc_alloc_q_vectors(struct igc_adapter *adapter)
4609 {
4610 	int rxr_remaining = adapter->num_rx_queues;
4611 	int txr_remaining = adapter->num_tx_queues;
4612 	int rxr_idx = 0, txr_idx = 0, v_idx = 0;
4613 	int q_vectors = adapter->num_q_vectors;
4614 	int err;
4615 
4616 	if (q_vectors >= (rxr_remaining + txr_remaining)) {
4617 		for (; rxr_remaining; v_idx++) {
4618 			err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
4619 						 0, 0, 1, rxr_idx);
4620 
4621 			if (err)
4622 				goto err_out;
4623 
4624 			/* update counts and index */
4625 			rxr_remaining--;
4626 			rxr_idx++;
4627 		}
4628 	}
4629 
4630 	for (; v_idx < q_vectors; v_idx++) {
4631 		int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
4632 		int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
4633 
4634 		err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
4635 					 tqpv, txr_idx, rqpv, rxr_idx);
4636 
4637 		if (err)
4638 			goto err_out;
4639 
4640 		/* update counts and index */
4641 		rxr_remaining -= rqpv;
4642 		txr_remaining -= tqpv;
4643 		rxr_idx++;
4644 		txr_idx++;
4645 	}
4646 
4647 	return 0;
4648 
4649 err_out:
4650 	adapter->num_tx_queues = 0;
4651 	adapter->num_rx_queues = 0;
4652 	adapter->num_q_vectors = 0;
4653 
4654 	while (v_idx--)
4655 		igc_free_q_vector(adapter, v_idx);
4656 
4657 	return -ENOMEM;
4658 }
4659 
4660 /**
4661  * igc_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
4662  * @adapter: Pointer to adapter structure
4663  * @msix: boolean for MSI-X capability
4664  *
4665  * This function initializes the interrupts and allocates all of the queues.
4666  */
4667 static int igc_init_interrupt_scheme(struct igc_adapter *adapter, bool msix)
4668 {
4669 	struct net_device *dev = adapter->netdev;
4670 	int err = 0;
4671 
4672 	igc_set_interrupt_capability(adapter, msix);
4673 
4674 	err = igc_alloc_q_vectors(adapter);
4675 	if (err) {
4676 		netdev_err(dev, "Unable to allocate memory for vectors\n");
4677 		goto err_alloc_q_vectors;
4678 	}
4679 
4680 	igc_cache_ring_register(adapter);
4681 
4682 	return 0;
4683 
4684 err_alloc_q_vectors:
4685 	igc_reset_interrupt_capability(adapter);
4686 	return err;
4687 }
4688 
4689 /**
4690  * igc_sw_init - Initialize general software structures (struct igc_adapter)
4691  * @adapter: board private structure to initialize
4692  *
4693  * igc_sw_init initializes the Adapter private data structure.
4694  * Fields are initialized based on PCI device information and
4695  * OS network device settings (MTU size).
4696  */
4697 static int igc_sw_init(struct igc_adapter *adapter)
4698 {
4699 	struct net_device *netdev = adapter->netdev;
4700 	struct pci_dev *pdev = adapter->pdev;
4701 	struct igc_hw *hw = &adapter->hw;
4702 
4703 	pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
4704 
4705 	/* set default ring sizes */
4706 	adapter->tx_ring_count = IGC_DEFAULT_TXD;
4707 	adapter->rx_ring_count = IGC_DEFAULT_RXD;
4708 
4709 	/* set default ITR values */
4710 	adapter->rx_itr_setting = IGC_DEFAULT_ITR;
4711 	adapter->tx_itr_setting = IGC_DEFAULT_ITR;
4712 
4713 	/* set default work limits */
4714 	adapter->tx_work_limit = IGC_DEFAULT_TX_WORK;
4715 
4716 	/* adjust max frame to be at least the size of a standard frame */
4717 	adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
4718 				VLAN_HLEN;
4719 	adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
4720 
4721 	mutex_init(&adapter->nfc_rule_lock);
4722 	INIT_LIST_HEAD(&adapter->nfc_rule_list);
4723 	adapter->nfc_rule_count = 0;
4724 
4725 	spin_lock_init(&adapter->stats64_lock);
4726 	/* Assume MSI-X interrupts, will be checked during IRQ allocation */
4727 	adapter->flags |= IGC_FLAG_HAS_MSIX;
4728 
4729 	igc_init_queue_configuration(adapter);
4730 
4731 	/* This call may decrease the number of queues */
4732 	if (igc_init_interrupt_scheme(adapter, true)) {
4733 		netdev_err(netdev, "Unable to allocate memory for queues\n");
4734 		return -ENOMEM;
4735 	}
4736 
4737 	/* Explicitly disable IRQ since the NIC can be in any state. */
4738 	igc_irq_disable(adapter);
4739 
4740 	set_bit(__IGC_DOWN, &adapter->state);
4741 
4742 	return 0;
4743 }
4744 
4745 /**
4746  * igc_up - Open the interface and prepare it to handle traffic
4747  * @adapter: board private structure
4748  */
4749 void igc_up(struct igc_adapter *adapter)
4750 {
4751 	struct igc_hw *hw = &adapter->hw;
4752 	int i = 0;
4753 
4754 	/* hardware has been reset, we need to reload some things */
4755 	igc_configure(adapter);
4756 
4757 	clear_bit(__IGC_DOWN, &adapter->state);
4758 
4759 	for (i = 0; i < adapter->num_q_vectors; i++)
4760 		napi_enable(&adapter->q_vector[i]->napi);
4761 
4762 	if (adapter->msix_entries)
4763 		igc_configure_msix(adapter);
4764 	else
4765 		igc_assign_vector(adapter->q_vector[0], 0);
4766 
4767 	/* Clear any pending interrupts. */
4768 	rd32(IGC_ICR);
4769 	igc_irq_enable(adapter);
4770 
4771 	netif_tx_start_all_queues(adapter->netdev);
4772 
4773 	/* start the watchdog. */
4774 	hw->mac.get_link_status = true;
4775 	schedule_work(&adapter->watchdog_task);
4776 }
4777 
4778 /**
4779  * igc_update_stats - Update the board statistics counters
4780  * @adapter: board private structure
4781  */
4782 void igc_update_stats(struct igc_adapter *adapter)
4783 {
4784 	struct rtnl_link_stats64 *net_stats = &adapter->stats64;
4785 	struct pci_dev *pdev = adapter->pdev;
4786 	struct igc_hw *hw = &adapter->hw;
4787 	u64 _bytes, _packets;
4788 	u64 bytes, packets;
4789 	unsigned int start;
4790 	u32 mpc;
4791 	int i;
4792 
4793 	/* Prevent stats update while adapter is being reset, or if the pci
4794 	 * connection is down.
4795 	 */
4796 	if (adapter->link_speed == 0)
4797 		return;
4798 	if (pci_channel_offline(pdev))
4799 		return;
4800 
4801 	packets = 0;
4802 	bytes = 0;
4803 
4804 	rcu_read_lock();
4805 	for (i = 0; i < adapter->num_rx_queues; i++) {
4806 		struct igc_ring *ring = adapter->rx_ring[i];
4807 		u32 rqdpc = rd32(IGC_RQDPC(i));
4808 
4809 		if (hw->mac.type >= igc_i225)
4810 			wr32(IGC_RQDPC(i), 0);
4811 
4812 		if (rqdpc) {
4813 			ring->rx_stats.drops += rqdpc;
4814 			net_stats->rx_fifo_errors += rqdpc;
4815 		}
4816 
4817 		do {
4818 			start = u64_stats_fetch_begin(&ring->rx_syncp);
4819 			_bytes = ring->rx_stats.bytes;
4820 			_packets = ring->rx_stats.packets;
4821 		} while (u64_stats_fetch_retry(&ring->rx_syncp, start));
4822 		bytes += _bytes;
4823 		packets += _packets;
4824 	}
4825 
4826 	net_stats->rx_bytes = bytes;
4827 	net_stats->rx_packets = packets;
4828 
4829 	packets = 0;
4830 	bytes = 0;
4831 	for (i = 0; i < adapter->num_tx_queues; i++) {
4832 		struct igc_ring *ring = adapter->tx_ring[i];
4833 
4834 		do {
4835 			start = u64_stats_fetch_begin(&ring->tx_syncp);
4836 			_bytes = ring->tx_stats.bytes;
4837 			_packets = ring->tx_stats.packets;
4838 		} while (u64_stats_fetch_retry(&ring->tx_syncp, start));
4839 		bytes += _bytes;
4840 		packets += _packets;
4841 	}
4842 	net_stats->tx_bytes = bytes;
4843 	net_stats->tx_packets = packets;
4844 	rcu_read_unlock();
4845 
4846 	/* read stats registers */
4847 	adapter->stats.crcerrs += rd32(IGC_CRCERRS);
4848 	adapter->stats.gprc += rd32(IGC_GPRC);
4849 	adapter->stats.gorc += rd32(IGC_GORCL);
4850 	rd32(IGC_GORCH); /* clear GORCL */
4851 	adapter->stats.bprc += rd32(IGC_BPRC);
4852 	adapter->stats.mprc += rd32(IGC_MPRC);
4853 	adapter->stats.roc += rd32(IGC_ROC);
4854 
4855 	adapter->stats.prc64 += rd32(IGC_PRC64);
4856 	adapter->stats.prc127 += rd32(IGC_PRC127);
4857 	adapter->stats.prc255 += rd32(IGC_PRC255);
4858 	adapter->stats.prc511 += rd32(IGC_PRC511);
4859 	adapter->stats.prc1023 += rd32(IGC_PRC1023);
4860 	adapter->stats.prc1522 += rd32(IGC_PRC1522);
4861 	adapter->stats.tlpic += rd32(IGC_TLPIC);
4862 	adapter->stats.rlpic += rd32(IGC_RLPIC);
4863 	adapter->stats.hgptc += rd32(IGC_HGPTC);
4864 
4865 	mpc = rd32(IGC_MPC);
4866 	adapter->stats.mpc += mpc;
4867 	net_stats->rx_fifo_errors += mpc;
4868 	adapter->stats.scc += rd32(IGC_SCC);
4869 	adapter->stats.ecol += rd32(IGC_ECOL);
4870 	adapter->stats.mcc += rd32(IGC_MCC);
4871 	adapter->stats.latecol += rd32(IGC_LATECOL);
4872 	adapter->stats.dc += rd32(IGC_DC);
4873 	adapter->stats.rlec += rd32(IGC_RLEC);
4874 	adapter->stats.xonrxc += rd32(IGC_XONRXC);
4875 	adapter->stats.xontxc += rd32(IGC_XONTXC);
4876 	adapter->stats.xoffrxc += rd32(IGC_XOFFRXC);
4877 	adapter->stats.xofftxc += rd32(IGC_XOFFTXC);
4878 	adapter->stats.fcruc += rd32(IGC_FCRUC);
4879 	adapter->stats.gptc += rd32(IGC_GPTC);
4880 	adapter->stats.gotc += rd32(IGC_GOTCL);
4881 	rd32(IGC_GOTCH); /* clear GOTCL */
4882 	adapter->stats.rnbc += rd32(IGC_RNBC);
4883 	adapter->stats.ruc += rd32(IGC_RUC);
4884 	adapter->stats.rfc += rd32(IGC_RFC);
4885 	adapter->stats.rjc += rd32(IGC_RJC);
4886 	adapter->stats.tor += rd32(IGC_TORH);
4887 	adapter->stats.tot += rd32(IGC_TOTH);
4888 	adapter->stats.tpr += rd32(IGC_TPR);
4889 
4890 	adapter->stats.ptc64 += rd32(IGC_PTC64);
4891 	adapter->stats.ptc127 += rd32(IGC_PTC127);
4892 	adapter->stats.ptc255 += rd32(IGC_PTC255);
4893 	adapter->stats.ptc511 += rd32(IGC_PTC511);
4894 	adapter->stats.ptc1023 += rd32(IGC_PTC1023);
4895 	adapter->stats.ptc1522 += rd32(IGC_PTC1522);
4896 
4897 	adapter->stats.mptc += rd32(IGC_MPTC);
4898 	adapter->stats.bptc += rd32(IGC_BPTC);
4899 
4900 	adapter->stats.tpt += rd32(IGC_TPT);
4901 	adapter->stats.colc += rd32(IGC_COLC);
4902 	adapter->stats.colc += rd32(IGC_RERC);
4903 
4904 	adapter->stats.algnerrc += rd32(IGC_ALGNERRC);
4905 
4906 	adapter->stats.tsctc += rd32(IGC_TSCTC);
4907 
4908 	adapter->stats.iac += rd32(IGC_IAC);
4909 
4910 	/* Fill out the OS statistics structure */
4911 	net_stats->multicast = adapter->stats.mprc;
4912 	net_stats->collisions = adapter->stats.colc;
4913 
4914 	/* Rx Errors */
4915 
4916 	/* RLEC on some newer hardware can be incorrect so build
4917 	 * our own version based on RUC and ROC
4918 	 */
4919 	net_stats->rx_errors = adapter->stats.rxerrc +
4920 		adapter->stats.crcerrs + adapter->stats.algnerrc +
4921 		adapter->stats.ruc + adapter->stats.roc +
4922 		adapter->stats.cexterr;
4923 	net_stats->rx_length_errors = adapter->stats.ruc +
4924 				      adapter->stats.roc;
4925 	net_stats->rx_crc_errors = adapter->stats.crcerrs;
4926 	net_stats->rx_frame_errors = adapter->stats.algnerrc;
4927 	net_stats->rx_missed_errors = adapter->stats.mpc;
4928 
4929 	/* Tx Errors */
4930 	net_stats->tx_errors = adapter->stats.ecol +
4931 			       adapter->stats.latecol;
4932 	net_stats->tx_aborted_errors = adapter->stats.ecol;
4933 	net_stats->tx_window_errors = adapter->stats.latecol;
4934 	net_stats->tx_carrier_errors = adapter->stats.tncrs;
4935 
4936 	/* Tx Dropped */
4937 	net_stats->tx_dropped = adapter->stats.txdrop;
4938 
4939 	/* Management Stats */
4940 	adapter->stats.mgptc += rd32(IGC_MGTPTC);
4941 	adapter->stats.mgprc += rd32(IGC_MGTPRC);
4942 	adapter->stats.mgpdc += rd32(IGC_MGTPDC);
4943 }
4944 
4945 /**
4946  * igc_down - Close the interface
4947  * @adapter: board private structure
4948  */
4949 void igc_down(struct igc_adapter *adapter)
4950 {
4951 	struct net_device *netdev = adapter->netdev;
4952 	struct igc_hw *hw = &adapter->hw;
4953 	u32 tctl, rctl;
4954 	int i = 0;
4955 
4956 	set_bit(__IGC_DOWN, &adapter->state);
4957 
4958 	igc_ptp_suspend(adapter);
4959 
4960 	if (pci_device_is_present(adapter->pdev)) {
4961 		/* disable receives in the hardware */
4962 		rctl = rd32(IGC_RCTL);
4963 		wr32(IGC_RCTL, rctl & ~IGC_RCTL_EN);
4964 		/* flush and sleep below */
4965 	}
4966 	/* set trans_start so we don't get spurious watchdogs during reset */
4967 	netif_trans_update(netdev);
4968 
4969 	netif_carrier_off(netdev);
4970 	netif_tx_stop_all_queues(netdev);
4971 
4972 	if (pci_device_is_present(adapter->pdev)) {
4973 		/* disable transmits in the hardware */
4974 		tctl = rd32(IGC_TCTL);
4975 		tctl &= ~IGC_TCTL_EN;
4976 		wr32(IGC_TCTL, tctl);
4977 		/* flush both disables and wait for them to finish */
4978 		wrfl();
4979 		usleep_range(10000, 20000);
4980 
4981 		igc_irq_disable(adapter);
4982 	}
4983 
4984 	adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
4985 
4986 	for (i = 0; i < adapter->num_q_vectors; i++) {
4987 		if (adapter->q_vector[i]) {
4988 			napi_synchronize(&adapter->q_vector[i]->napi);
4989 			napi_disable(&adapter->q_vector[i]->napi);
4990 		}
4991 	}
4992 
4993 	del_timer_sync(&adapter->watchdog_timer);
4994 	del_timer_sync(&adapter->phy_info_timer);
4995 
4996 	/* record the stats before reset*/
4997 	spin_lock(&adapter->stats64_lock);
4998 	igc_update_stats(adapter);
4999 	spin_unlock(&adapter->stats64_lock);
5000 
5001 	adapter->link_speed = 0;
5002 	adapter->link_duplex = 0;
5003 
5004 	if (!pci_channel_offline(adapter->pdev))
5005 		igc_reset(adapter);
5006 
5007 	/* clear VLAN promisc flag so VFTA will be updated if necessary */
5008 	adapter->flags &= ~IGC_FLAG_VLAN_PROMISC;
5009 
5010 	igc_clean_all_tx_rings(adapter);
5011 	igc_clean_all_rx_rings(adapter);
5012 }
5013 
5014 void igc_reinit_locked(struct igc_adapter *adapter)
5015 {
5016 	while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
5017 		usleep_range(1000, 2000);
5018 	igc_down(adapter);
5019 	igc_up(adapter);
5020 	clear_bit(__IGC_RESETTING, &adapter->state);
5021 }
5022 
5023 static void igc_reset_task(struct work_struct *work)
5024 {
5025 	struct igc_adapter *adapter;
5026 
5027 	adapter = container_of(work, struct igc_adapter, reset_task);
5028 
5029 	rtnl_lock();
5030 	/* If we're already down or resetting, just bail */
5031 	if (test_bit(__IGC_DOWN, &adapter->state) ||
5032 	    test_bit(__IGC_RESETTING, &adapter->state)) {
5033 		rtnl_unlock();
5034 		return;
5035 	}
5036 
5037 	igc_rings_dump(adapter);
5038 	igc_regs_dump(adapter);
5039 	netdev_err(adapter->netdev, "Reset adapter\n");
5040 	igc_reinit_locked(adapter);
5041 	rtnl_unlock();
5042 }
5043 
5044 /**
5045  * igc_change_mtu - Change the Maximum Transfer Unit
5046  * @netdev: network interface device structure
5047  * @new_mtu: new value for maximum frame size
5048  *
5049  * Returns 0 on success, negative on failure
5050  */
5051 static int igc_change_mtu(struct net_device *netdev, int new_mtu)
5052 {
5053 	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
5054 	struct igc_adapter *adapter = netdev_priv(netdev);
5055 
5056 	if (igc_xdp_is_enabled(adapter) && new_mtu > ETH_DATA_LEN) {
5057 		netdev_dbg(netdev, "Jumbo frames not supported with XDP");
5058 		return -EINVAL;
5059 	}
5060 
5061 	/* adjust max frame to be at least the size of a standard frame */
5062 	if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
5063 		max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
5064 
5065 	while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
5066 		usleep_range(1000, 2000);
5067 
5068 	/* igc_down has a dependency on max_frame_size */
5069 	adapter->max_frame_size = max_frame;
5070 
5071 	if (netif_running(netdev))
5072 		igc_down(adapter);
5073 
5074 	netdev_dbg(netdev, "changing MTU from %d to %d\n", netdev->mtu, new_mtu);
5075 	netdev->mtu = new_mtu;
5076 
5077 	if (netif_running(netdev))
5078 		igc_up(adapter);
5079 	else
5080 		igc_reset(adapter);
5081 
5082 	clear_bit(__IGC_RESETTING, &adapter->state);
5083 
5084 	return 0;
5085 }
5086 
5087 /**
5088  * igc_tx_timeout - Respond to a Tx Hang
5089  * @netdev: network interface device structure
5090  * @txqueue: queue number that timed out
5091  **/
5092 static void igc_tx_timeout(struct net_device *netdev,
5093 			   unsigned int __always_unused txqueue)
5094 {
5095 	struct igc_adapter *adapter = netdev_priv(netdev);
5096 	struct igc_hw *hw = &adapter->hw;
5097 
5098 	/* Do the reset outside of interrupt context */
5099 	adapter->tx_timeout_count++;
5100 	schedule_work(&adapter->reset_task);
5101 	wr32(IGC_EICS,
5102 	     (adapter->eims_enable_mask & ~adapter->eims_other));
5103 }
5104 
5105 /**
5106  * igc_get_stats64 - Get System Network Statistics
5107  * @netdev: network interface device structure
5108  * @stats: rtnl_link_stats64 pointer
5109  *
5110  * Returns the address of the device statistics structure.
5111  * The statistics are updated here and also from the timer callback.
5112  */
5113 static void igc_get_stats64(struct net_device *netdev,
5114 			    struct rtnl_link_stats64 *stats)
5115 {
5116 	struct igc_adapter *adapter = netdev_priv(netdev);
5117 
5118 	spin_lock(&adapter->stats64_lock);
5119 	if (!test_bit(__IGC_RESETTING, &adapter->state))
5120 		igc_update_stats(adapter);
5121 	memcpy(stats, &adapter->stats64, sizeof(*stats));
5122 	spin_unlock(&adapter->stats64_lock);
5123 }
5124 
5125 static netdev_features_t igc_fix_features(struct net_device *netdev,
5126 					  netdev_features_t features)
5127 {
5128 	/* Since there is no support for separate Rx/Tx vlan accel
5129 	 * enable/disable make sure Tx flag is always in same state as Rx.
5130 	 */
5131 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
5132 		features |= NETIF_F_HW_VLAN_CTAG_TX;
5133 	else
5134 		features &= ~NETIF_F_HW_VLAN_CTAG_TX;
5135 
5136 	return features;
5137 }
5138 
5139 static int igc_set_features(struct net_device *netdev,
5140 			    netdev_features_t features)
5141 {
5142 	netdev_features_t changed = netdev->features ^ features;
5143 	struct igc_adapter *adapter = netdev_priv(netdev);
5144 
5145 	if (changed & NETIF_F_HW_VLAN_CTAG_RX)
5146 		igc_vlan_mode(netdev, features);
5147 
5148 	/* Add VLAN support */
5149 	if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE)))
5150 		return 0;
5151 
5152 	if (!(features & NETIF_F_NTUPLE))
5153 		igc_flush_nfc_rules(adapter);
5154 
5155 	netdev->features = features;
5156 
5157 	if (netif_running(netdev))
5158 		igc_reinit_locked(adapter);
5159 	else
5160 		igc_reset(adapter);
5161 
5162 	return 1;
5163 }
5164 
5165 static netdev_features_t
5166 igc_features_check(struct sk_buff *skb, struct net_device *dev,
5167 		   netdev_features_t features)
5168 {
5169 	unsigned int network_hdr_len, mac_hdr_len;
5170 
5171 	/* Make certain the headers can be described by a context descriptor */
5172 	mac_hdr_len = skb_network_header(skb) - skb->data;
5173 	if (unlikely(mac_hdr_len > IGC_MAX_MAC_HDR_LEN))
5174 		return features & ~(NETIF_F_HW_CSUM |
5175 				    NETIF_F_SCTP_CRC |
5176 				    NETIF_F_HW_VLAN_CTAG_TX |
5177 				    NETIF_F_TSO |
5178 				    NETIF_F_TSO6);
5179 
5180 	network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
5181 	if (unlikely(network_hdr_len >  IGC_MAX_NETWORK_HDR_LEN))
5182 		return features & ~(NETIF_F_HW_CSUM |
5183 				    NETIF_F_SCTP_CRC |
5184 				    NETIF_F_TSO |
5185 				    NETIF_F_TSO6);
5186 
5187 	/* We can only support IPv4 TSO in tunnels if we can mangle the
5188 	 * inner IP ID field, so strip TSO if MANGLEID is not supported.
5189 	 */
5190 	if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
5191 		features &= ~NETIF_F_TSO;
5192 
5193 	return features;
5194 }
5195 
5196 static void igc_tsync_interrupt(struct igc_adapter *adapter)
5197 {
5198 	u32 ack, tsauxc, sec, nsec, tsicr;
5199 	struct igc_hw *hw = &adapter->hw;
5200 	struct ptp_clock_event event;
5201 	struct timespec64 ts;
5202 
5203 	tsicr = rd32(IGC_TSICR);
5204 	ack = 0;
5205 
5206 	if (tsicr & IGC_TSICR_SYS_WRAP) {
5207 		event.type = PTP_CLOCK_PPS;
5208 		if (adapter->ptp_caps.pps)
5209 			ptp_clock_event(adapter->ptp_clock, &event);
5210 		ack |= IGC_TSICR_SYS_WRAP;
5211 	}
5212 
5213 	if (tsicr & IGC_TSICR_TXTS) {
5214 		/* retrieve hardware timestamp */
5215 		schedule_work(&adapter->ptp_tx_work);
5216 		ack |= IGC_TSICR_TXTS;
5217 	}
5218 
5219 	if (tsicr & IGC_TSICR_TT0) {
5220 		spin_lock(&adapter->tmreg_lock);
5221 		ts = timespec64_add(adapter->perout[0].start,
5222 				    adapter->perout[0].period);
5223 		wr32(IGC_TRGTTIML0, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0);
5224 		wr32(IGC_TRGTTIMH0, (u32)ts.tv_sec);
5225 		tsauxc = rd32(IGC_TSAUXC);
5226 		tsauxc |= IGC_TSAUXC_EN_TT0;
5227 		wr32(IGC_TSAUXC, tsauxc);
5228 		adapter->perout[0].start = ts;
5229 		spin_unlock(&adapter->tmreg_lock);
5230 		ack |= IGC_TSICR_TT0;
5231 	}
5232 
5233 	if (tsicr & IGC_TSICR_TT1) {
5234 		spin_lock(&adapter->tmreg_lock);
5235 		ts = timespec64_add(adapter->perout[1].start,
5236 				    adapter->perout[1].period);
5237 		wr32(IGC_TRGTTIML1, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0);
5238 		wr32(IGC_TRGTTIMH1, (u32)ts.tv_sec);
5239 		tsauxc = rd32(IGC_TSAUXC);
5240 		tsauxc |= IGC_TSAUXC_EN_TT1;
5241 		wr32(IGC_TSAUXC, tsauxc);
5242 		adapter->perout[1].start = ts;
5243 		spin_unlock(&adapter->tmreg_lock);
5244 		ack |= IGC_TSICR_TT1;
5245 	}
5246 
5247 	if (tsicr & IGC_TSICR_AUTT0) {
5248 		nsec = rd32(IGC_AUXSTMPL0);
5249 		sec  = rd32(IGC_AUXSTMPH0);
5250 		event.type = PTP_CLOCK_EXTTS;
5251 		event.index = 0;
5252 		event.timestamp = sec * NSEC_PER_SEC + nsec;
5253 		ptp_clock_event(adapter->ptp_clock, &event);
5254 		ack |= IGC_TSICR_AUTT0;
5255 	}
5256 
5257 	if (tsicr & IGC_TSICR_AUTT1) {
5258 		nsec = rd32(IGC_AUXSTMPL1);
5259 		sec  = rd32(IGC_AUXSTMPH1);
5260 		event.type = PTP_CLOCK_EXTTS;
5261 		event.index = 1;
5262 		event.timestamp = sec * NSEC_PER_SEC + nsec;
5263 		ptp_clock_event(adapter->ptp_clock, &event);
5264 		ack |= IGC_TSICR_AUTT1;
5265 	}
5266 
5267 	/* acknowledge the interrupts */
5268 	wr32(IGC_TSICR, ack);
5269 }
5270 
5271 /**
5272  * igc_msix_other - msix other interrupt handler
5273  * @irq: interrupt number
5274  * @data: pointer to a q_vector
5275  */
5276 static irqreturn_t igc_msix_other(int irq, void *data)
5277 {
5278 	struct igc_adapter *adapter = data;
5279 	struct igc_hw *hw = &adapter->hw;
5280 	u32 icr = rd32(IGC_ICR);
5281 
5282 	/* reading ICR causes bit 31 of EICR to be cleared */
5283 	if (icr & IGC_ICR_DRSTA)
5284 		schedule_work(&adapter->reset_task);
5285 
5286 	if (icr & IGC_ICR_DOUTSYNC) {
5287 		/* HW is reporting DMA is out of sync */
5288 		adapter->stats.doosync++;
5289 	}
5290 
5291 	if (icr & IGC_ICR_LSC) {
5292 		hw->mac.get_link_status = true;
5293 		/* guard against interrupt when we're going down */
5294 		if (!test_bit(__IGC_DOWN, &adapter->state))
5295 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
5296 	}
5297 
5298 	if (icr & IGC_ICR_TS)
5299 		igc_tsync_interrupt(adapter);
5300 
5301 	wr32(IGC_EIMS, adapter->eims_other);
5302 
5303 	return IRQ_HANDLED;
5304 }
5305 
5306 static void igc_write_itr(struct igc_q_vector *q_vector)
5307 {
5308 	u32 itr_val = q_vector->itr_val & IGC_QVECTOR_MASK;
5309 
5310 	if (!q_vector->set_itr)
5311 		return;
5312 
5313 	if (!itr_val)
5314 		itr_val = IGC_ITR_VAL_MASK;
5315 
5316 	itr_val |= IGC_EITR_CNT_IGNR;
5317 
5318 	writel(itr_val, q_vector->itr_register);
5319 	q_vector->set_itr = 0;
5320 }
5321 
5322 static irqreturn_t igc_msix_ring(int irq, void *data)
5323 {
5324 	struct igc_q_vector *q_vector = data;
5325 
5326 	/* Write the ITR value calculated from the previous interrupt. */
5327 	igc_write_itr(q_vector);
5328 
5329 	napi_schedule(&q_vector->napi);
5330 
5331 	return IRQ_HANDLED;
5332 }
5333 
5334 /**
5335  * igc_request_msix - Initialize MSI-X interrupts
5336  * @adapter: Pointer to adapter structure
5337  *
5338  * igc_request_msix allocates MSI-X vectors and requests interrupts from the
5339  * kernel.
5340  */
5341 static int igc_request_msix(struct igc_adapter *adapter)
5342 {
5343 	unsigned int num_q_vectors = adapter->num_q_vectors;
5344 	int i = 0, err = 0, vector = 0, free_vector = 0;
5345 	struct net_device *netdev = adapter->netdev;
5346 
5347 	err = request_irq(adapter->msix_entries[vector].vector,
5348 			  &igc_msix_other, 0, netdev->name, adapter);
5349 	if (err)
5350 		goto err_out;
5351 
5352 	if (num_q_vectors > MAX_Q_VECTORS) {
5353 		num_q_vectors = MAX_Q_VECTORS;
5354 		dev_warn(&adapter->pdev->dev,
5355 			 "The number of queue vectors (%d) is higher than max allowed (%d)\n",
5356 			 adapter->num_q_vectors, MAX_Q_VECTORS);
5357 	}
5358 	for (i = 0; i < num_q_vectors; i++) {
5359 		struct igc_q_vector *q_vector = adapter->q_vector[i];
5360 
5361 		vector++;
5362 
5363 		q_vector->itr_register = adapter->io_addr + IGC_EITR(vector);
5364 
5365 		if (q_vector->rx.ring && q_vector->tx.ring)
5366 			sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
5367 				q_vector->rx.ring->queue_index);
5368 		else if (q_vector->tx.ring)
5369 			sprintf(q_vector->name, "%s-tx-%u", netdev->name,
5370 				q_vector->tx.ring->queue_index);
5371 		else if (q_vector->rx.ring)
5372 			sprintf(q_vector->name, "%s-rx-%u", netdev->name,
5373 				q_vector->rx.ring->queue_index);
5374 		else
5375 			sprintf(q_vector->name, "%s-unused", netdev->name);
5376 
5377 		err = request_irq(adapter->msix_entries[vector].vector,
5378 				  igc_msix_ring, 0, q_vector->name,
5379 				  q_vector);
5380 		if (err)
5381 			goto err_free;
5382 	}
5383 
5384 	igc_configure_msix(adapter);
5385 	return 0;
5386 
5387 err_free:
5388 	/* free already assigned IRQs */
5389 	free_irq(adapter->msix_entries[free_vector++].vector, adapter);
5390 
5391 	vector--;
5392 	for (i = 0; i < vector; i++) {
5393 		free_irq(adapter->msix_entries[free_vector++].vector,
5394 			 adapter->q_vector[i]);
5395 	}
5396 err_out:
5397 	return err;
5398 }
5399 
5400 /**
5401  * igc_clear_interrupt_scheme - reset the device to a state of no interrupts
5402  * @adapter: Pointer to adapter structure
5403  *
5404  * This function resets the device so that it has 0 rx queues, tx queues, and
5405  * MSI-X interrupts allocated.
5406  */
5407 static void igc_clear_interrupt_scheme(struct igc_adapter *adapter)
5408 {
5409 	igc_free_q_vectors(adapter);
5410 	igc_reset_interrupt_capability(adapter);
5411 }
5412 
5413 /* Need to wait a few seconds after link up to get diagnostic information from
5414  * the phy
5415  */
5416 static void igc_update_phy_info(struct timer_list *t)
5417 {
5418 	struct igc_adapter *adapter = from_timer(adapter, t, phy_info_timer);
5419 
5420 	igc_get_phy_info(&adapter->hw);
5421 }
5422 
5423 /**
5424  * igc_has_link - check shared code for link and determine up/down
5425  * @adapter: pointer to driver private info
5426  */
5427 bool igc_has_link(struct igc_adapter *adapter)
5428 {
5429 	struct igc_hw *hw = &adapter->hw;
5430 	bool link_active = false;
5431 
5432 	/* get_link_status is set on LSC (link status) interrupt or
5433 	 * rx sequence error interrupt.  get_link_status will stay
5434 	 * false until the igc_check_for_link establishes link
5435 	 * for copper adapters ONLY
5436 	 */
5437 	if (!hw->mac.get_link_status)
5438 		return true;
5439 	hw->mac.ops.check_for_link(hw);
5440 	link_active = !hw->mac.get_link_status;
5441 
5442 	if (hw->mac.type == igc_i225) {
5443 		if (!netif_carrier_ok(adapter->netdev)) {
5444 			adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
5445 		} else if (!(adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)) {
5446 			adapter->flags |= IGC_FLAG_NEED_LINK_UPDATE;
5447 			adapter->link_check_timeout = jiffies;
5448 		}
5449 	}
5450 
5451 	return link_active;
5452 }
5453 
5454 /**
5455  * igc_watchdog - Timer Call-back
5456  * @t: timer for the watchdog
5457  */
5458 static void igc_watchdog(struct timer_list *t)
5459 {
5460 	struct igc_adapter *adapter = from_timer(adapter, t, watchdog_timer);
5461 	/* Do the rest outside of interrupt context */
5462 	schedule_work(&adapter->watchdog_task);
5463 }
5464 
5465 static void igc_watchdog_task(struct work_struct *work)
5466 {
5467 	struct igc_adapter *adapter = container_of(work,
5468 						   struct igc_adapter,
5469 						   watchdog_task);
5470 	struct net_device *netdev = adapter->netdev;
5471 	struct igc_hw *hw = &adapter->hw;
5472 	struct igc_phy_info *phy = &hw->phy;
5473 	u16 phy_data, retry_count = 20;
5474 	u32 link;
5475 	int i;
5476 
5477 	link = igc_has_link(adapter);
5478 
5479 	if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE) {
5480 		if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
5481 			adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
5482 		else
5483 			link = false;
5484 	}
5485 
5486 	if (link) {
5487 		/* Cancel scheduled suspend requests. */
5488 		pm_runtime_resume(netdev->dev.parent);
5489 
5490 		if (!netif_carrier_ok(netdev)) {
5491 			u32 ctrl;
5492 
5493 			hw->mac.ops.get_speed_and_duplex(hw,
5494 							 &adapter->link_speed,
5495 							 &adapter->link_duplex);
5496 
5497 			ctrl = rd32(IGC_CTRL);
5498 			/* Link status message must follow this format */
5499 			netdev_info(netdev,
5500 				    "NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
5501 				    adapter->link_speed,
5502 				    adapter->link_duplex == FULL_DUPLEX ?
5503 				    "Full" : "Half",
5504 				    (ctrl & IGC_CTRL_TFCE) &&
5505 				    (ctrl & IGC_CTRL_RFCE) ? "RX/TX" :
5506 				    (ctrl & IGC_CTRL_RFCE) ?  "RX" :
5507 				    (ctrl & IGC_CTRL_TFCE) ?  "TX" : "None");
5508 
5509 			/* disable EEE if enabled */
5510 			if ((adapter->flags & IGC_FLAG_EEE) &&
5511 			    adapter->link_duplex == HALF_DUPLEX) {
5512 				netdev_info(netdev,
5513 					    "EEE Disabled: unsupported at half duplex. Re-enable using ethtool when at full duplex\n");
5514 				adapter->hw.dev_spec._base.eee_enable = false;
5515 				adapter->flags &= ~IGC_FLAG_EEE;
5516 			}
5517 
5518 			/* check if SmartSpeed worked */
5519 			igc_check_downshift(hw);
5520 			if (phy->speed_downgraded)
5521 				netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
5522 
5523 			/* adjust timeout factor according to speed/duplex */
5524 			adapter->tx_timeout_factor = 1;
5525 			switch (adapter->link_speed) {
5526 			case SPEED_10:
5527 				adapter->tx_timeout_factor = 14;
5528 				break;
5529 			case SPEED_100:
5530 			case SPEED_1000:
5531 			case SPEED_2500:
5532 				adapter->tx_timeout_factor = 1;
5533 				break;
5534 			}
5535 
5536 			/* Once the launch time has been set on the wire, there
5537 			 * is a delay before the link speed can be determined
5538 			 * based on link-up activity. Write into the register
5539 			 * as soon as we know the correct link speed.
5540 			 */
5541 			igc_tsn_adjust_txtime_offset(adapter);
5542 
5543 			if (adapter->link_speed != SPEED_1000)
5544 				goto no_wait;
5545 
5546 			/* wait for Remote receiver status OK */
5547 retry_read_status:
5548 			if (!igc_read_phy_reg(hw, PHY_1000T_STATUS,
5549 					      &phy_data)) {
5550 				if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
5551 				    retry_count) {
5552 					msleep(100);
5553 					retry_count--;
5554 					goto retry_read_status;
5555 				} else if (!retry_count) {
5556 					netdev_err(netdev, "exceed max 2 second\n");
5557 				}
5558 			} else {
5559 				netdev_err(netdev, "read 1000Base-T Status Reg\n");
5560 			}
5561 no_wait:
5562 			netif_carrier_on(netdev);
5563 
5564 			/* link state has changed, schedule phy info update */
5565 			if (!test_bit(__IGC_DOWN, &adapter->state))
5566 				mod_timer(&adapter->phy_info_timer,
5567 					  round_jiffies(jiffies + 2 * HZ));
5568 		}
5569 	} else {
5570 		if (netif_carrier_ok(netdev)) {
5571 			adapter->link_speed = 0;
5572 			adapter->link_duplex = 0;
5573 
5574 			/* Links status message must follow this format */
5575 			netdev_info(netdev, "NIC Link is Down\n");
5576 			netif_carrier_off(netdev);
5577 
5578 			/* link state has changed, schedule phy info update */
5579 			if (!test_bit(__IGC_DOWN, &adapter->state))
5580 				mod_timer(&adapter->phy_info_timer,
5581 					  round_jiffies(jiffies + 2 * HZ));
5582 
5583 			pm_schedule_suspend(netdev->dev.parent,
5584 					    MSEC_PER_SEC * 5);
5585 		}
5586 	}
5587 
5588 	spin_lock(&adapter->stats64_lock);
5589 	igc_update_stats(adapter);
5590 	spin_unlock(&adapter->stats64_lock);
5591 
5592 	for (i = 0; i < adapter->num_tx_queues; i++) {
5593 		struct igc_ring *tx_ring = adapter->tx_ring[i];
5594 
5595 		if (!netif_carrier_ok(netdev)) {
5596 			/* We've lost link, so the controller stops DMA,
5597 			 * but we've got queued Tx work that's never going
5598 			 * to get done, so reset controller to flush Tx.
5599 			 * (Do the reset outside of interrupt context).
5600 			 */
5601 			if (igc_desc_unused(tx_ring) + 1 < tx_ring->count) {
5602 				adapter->tx_timeout_count++;
5603 				schedule_work(&adapter->reset_task);
5604 				/* return immediately since reset is imminent */
5605 				return;
5606 			}
5607 		}
5608 
5609 		/* Force detection of hung controller every watchdog period */
5610 		set_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
5611 	}
5612 
5613 	/* Cause software interrupt to ensure Rx ring is cleaned */
5614 	if (adapter->flags & IGC_FLAG_HAS_MSIX) {
5615 		u32 eics = 0;
5616 
5617 		for (i = 0; i < adapter->num_q_vectors; i++)
5618 			eics |= adapter->q_vector[i]->eims_value;
5619 		wr32(IGC_EICS, eics);
5620 	} else {
5621 		wr32(IGC_ICS, IGC_ICS_RXDMT0);
5622 	}
5623 
5624 	igc_ptp_tx_hang(adapter);
5625 
5626 	/* Reset the timer */
5627 	if (!test_bit(__IGC_DOWN, &adapter->state)) {
5628 		if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)
5629 			mod_timer(&adapter->watchdog_timer,
5630 				  round_jiffies(jiffies +  HZ));
5631 		else
5632 			mod_timer(&adapter->watchdog_timer,
5633 				  round_jiffies(jiffies + 2 * HZ));
5634 	}
5635 }
5636 
5637 /**
5638  * igc_intr_msi - Interrupt Handler
5639  * @irq: interrupt number
5640  * @data: pointer to a network interface device structure
5641  */
5642 static irqreturn_t igc_intr_msi(int irq, void *data)
5643 {
5644 	struct igc_adapter *adapter = data;
5645 	struct igc_q_vector *q_vector = adapter->q_vector[0];
5646 	struct igc_hw *hw = &adapter->hw;
5647 	/* read ICR disables interrupts using IAM */
5648 	u32 icr = rd32(IGC_ICR);
5649 
5650 	igc_write_itr(q_vector);
5651 
5652 	if (icr & IGC_ICR_DRSTA)
5653 		schedule_work(&adapter->reset_task);
5654 
5655 	if (icr & IGC_ICR_DOUTSYNC) {
5656 		/* HW is reporting DMA is out of sync */
5657 		adapter->stats.doosync++;
5658 	}
5659 
5660 	if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
5661 		hw->mac.get_link_status = true;
5662 		if (!test_bit(__IGC_DOWN, &adapter->state))
5663 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
5664 	}
5665 
5666 	if (icr & IGC_ICR_TS)
5667 		igc_tsync_interrupt(adapter);
5668 
5669 	napi_schedule(&q_vector->napi);
5670 
5671 	return IRQ_HANDLED;
5672 }
5673 
5674 /**
5675  * igc_intr - Legacy Interrupt Handler
5676  * @irq: interrupt number
5677  * @data: pointer to a network interface device structure
5678  */
5679 static irqreturn_t igc_intr(int irq, void *data)
5680 {
5681 	struct igc_adapter *adapter = data;
5682 	struct igc_q_vector *q_vector = adapter->q_vector[0];
5683 	struct igc_hw *hw = &adapter->hw;
5684 	/* Interrupt Auto-Mask...upon reading ICR, interrupts are masked.  No
5685 	 * need for the IMC write
5686 	 */
5687 	u32 icr = rd32(IGC_ICR);
5688 
5689 	/* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5690 	 * not set, then the adapter didn't send an interrupt
5691 	 */
5692 	if (!(icr & IGC_ICR_INT_ASSERTED))
5693 		return IRQ_NONE;
5694 
5695 	igc_write_itr(q_vector);
5696 
5697 	if (icr & IGC_ICR_DRSTA)
5698 		schedule_work(&adapter->reset_task);
5699 
5700 	if (icr & IGC_ICR_DOUTSYNC) {
5701 		/* HW is reporting DMA is out of sync */
5702 		adapter->stats.doosync++;
5703 	}
5704 
5705 	if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
5706 		hw->mac.get_link_status = true;
5707 		/* guard against interrupt when we're going down */
5708 		if (!test_bit(__IGC_DOWN, &adapter->state))
5709 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
5710 	}
5711 
5712 	if (icr & IGC_ICR_TS)
5713 		igc_tsync_interrupt(adapter);
5714 
5715 	napi_schedule(&q_vector->napi);
5716 
5717 	return IRQ_HANDLED;
5718 }
5719 
5720 static void igc_free_irq(struct igc_adapter *adapter)
5721 {
5722 	if (adapter->msix_entries) {
5723 		int vector = 0, i;
5724 
5725 		free_irq(adapter->msix_entries[vector++].vector, adapter);
5726 
5727 		for (i = 0; i < adapter->num_q_vectors; i++)
5728 			free_irq(adapter->msix_entries[vector++].vector,
5729 				 adapter->q_vector[i]);
5730 	} else {
5731 		free_irq(adapter->pdev->irq, adapter);
5732 	}
5733 }
5734 
5735 /**
5736  * igc_request_irq - initialize interrupts
5737  * @adapter: Pointer to adapter structure
5738  *
5739  * Attempts to configure interrupts using the best available
5740  * capabilities of the hardware and kernel.
5741  */
5742 static int igc_request_irq(struct igc_adapter *adapter)
5743 {
5744 	struct net_device *netdev = adapter->netdev;
5745 	struct pci_dev *pdev = adapter->pdev;
5746 	int err = 0;
5747 
5748 	if (adapter->flags & IGC_FLAG_HAS_MSIX) {
5749 		err = igc_request_msix(adapter);
5750 		if (!err)
5751 			goto request_done;
5752 		/* fall back to MSI */
5753 		igc_free_all_tx_resources(adapter);
5754 		igc_free_all_rx_resources(adapter);
5755 
5756 		igc_clear_interrupt_scheme(adapter);
5757 		err = igc_init_interrupt_scheme(adapter, false);
5758 		if (err)
5759 			goto request_done;
5760 		igc_setup_all_tx_resources(adapter);
5761 		igc_setup_all_rx_resources(adapter);
5762 		igc_configure(adapter);
5763 	}
5764 
5765 	igc_assign_vector(adapter->q_vector[0], 0);
5766 
5767 	if (adapter->flags & IGC_FLAG_HAS_MSI) {
5768 		err = request_irq(pdev->irq, &igc_intr_msi, 0,
5769 				  netdev->name, adapter);
5770 		if (!err)
5771 			goto request_done;
5772 
5773 		/* fall back to legacy interrupts */
5774 		igc_reset_interrupt_capability(adapter);
5775 		adapter->flags &= ~IGC_FLAG_HAS_MSI;
5776 	}
5777 
5778 	err = request_irq(pdev->irq, &igc_intr, IRQF_SHARED,
5779 			  netdev->name, adapter);
5780 
5781 	if (err)
5782 		netdev_err(netdev, "Error %d getting interrupt\n", err);
5783 
5784 request_done:
5785 	return err;
5786 }
5787 
5788 /**
5789  * __igc_open - Called when a network interface is made active
5790  * @netdev: network interface device structure
5791  * @resuming: boolean indicating if the device is resuming
5792  *
5793  * Returns 0 on success, negative value on failure
5794  *
5795  * The open entry point is called when a network interface is made
5796  * active by the system (IFF_UP).  At this point all resources needed
5797  * for transmit and receive operations are allocated, the interrupt
5798  * handler is registered with the OS, the watchdog timer is started,
5799  * and the stack is notified that the interface is ready.
5800  */
5801 static int __igc_open(struct net_device *netdev, bool resuming)
5802 {
5803 	struct igc_adapter *adapter = netdev_priv(netdev);
5804 	struct pci_dev *pdev = adapter->pdev;
5805 	struct igc_hw *hw = &adapter->hw;
5806 	int err = 0;
5807 	int i = 0;
5808 
5809 	/* disallow open during test */
5810 
5811 	if (test_bit(__IGC_TESTING, &adapter->state)) {
5812 		WARN_ON(resuming);
5813 		return -EBUSY;
5814 	}
5815 
5816 	if (!resuming)
5817 		pm_runtime_get_sync(&pdev->dev);
5818 
5819 	netif_carrier_off(netdev);
5820 
5821 	/* allocate transmit descriptors */
5822 	err = igc_setup_all_tx_resources(adapter);
5823 	if (err)
5824 		goto err_setup_tx;
5825 
5826 	/* allocate receive descriptors */
5827 	err = igc_setup_all_rx_resources(adapter);
5828 	if (err)
5829 		goto err_setup_rx;
5830 
5831 	igc_power_up_link(adapter);
5832 
5833 	igc_configure(adapter);
5834 
5835 	err = igc_request_irq(adapter);
5836 	if (err)
5837 		goto err_req_irq;
5838 
5839 	/* Notify the stack of the actual queue counts. */
5840 	err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
5841 	if (err)
5842 		goto err_set_queues;
5843 
5844 	err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
5845 	if (err)
5846 		goto err_set_queues;
5847 
5848 	clear_bit(__IGC_DOWN, &adapter->state);
5849 
5850 	for (i = 0; i < adapter->num_q_vectors; i++)
5851 		napi_enable(&adapter->q_vector[i]->napi);
5852 
5853 	/* Clear any pending interrupts. */
5854 	rd32(IGC_ICR);
5855 	igc_irq_enable(adapter);
5856 
5857 	if (!resuming)
5858 		pm_runtime_put(&pdev->dev);
5859 
5860 	netif_tx_start_all_queues(netdev);
5861 
5862 	/* start the watchdog. */
5863 	hw->mac.get_link_status = true;
5864 	schedule_work(&adapter->watchdog_task);
5865 
5866 	return IGC_SUCCESS;
5867 
5868 err_set_queues:
5869 	igc_free_irq(adapter);
5870 err_req_irq:
5871 	igc_release_hw_control(adapter);
5872 	igc_power_down_phy_copper_base(&adapter->hw);
5873 	igc_free_all_rx_resources(adapter);
5874 err_setup_rx:
5875 	igc_free_all_tx_resources(adapter);
5876 err_setup_tx:
5877 	igc_reset(adapter);
5878 	if (!resuming)
5879 		pm_runtime_put(&pdev->dev);
5880 
5881 	return err;
5882 }
5883 
5884 int igc_open(struct net_device *netdev)
5885 {
5886 	return __igc_open(netdev, false);
5887 }
5888 
5889 /**
5890  * __igc_close - Disables a network interface
5891  * @netdev: network interface device structure
5892  * @suspending: boolean indicating the device is suspending
5893  *
5894  * Returns 0, this is not allowed to fail
5895  *
5896  * The close entry point is called when an interface is de-activated
5897  * by the OS.  The hardware is still under the driver's control, but
5898  * needs to be disabled.  A global MAC reset is issued to stop the
5899  * hardware, and all transmit and receive resources are freed.
5900  */
5901 static int __igc_close(struct net_device *netdev, bool suspending)
5902 {
5903 	struct igc_adapter *adapter = netdev_priv(netdev);
5904 	struct pci_dev *pdev = adapter->pdev;
5905 
5906 	WARN_ON(test_bit(__IGC_RESETTING, &adapter->state));
5907 
5908 	if (!suspending)
5909 		pm_runtime_get_sync(&pdev->dev);
5910 
5911 	igc_down(adapter);
5912 
5913 	igc_release_hw_control(adapter);
5914 
5915 	igc_free_irq(adapter);
5916 
5917 	igc_free_all_tx_resources(adapter);
5918 	igc_free_all_rx_resources(adapter);
5919 
5920 	if (!suspending)
5921 		pm_runtime_put_sync(&pdev->dev);
5922 
5923 	return 0;
5924 }
5925 
5926 int igc_close(struct net_device *netdev)
5927 {
5928 	if (netif_device_present(netdev) || netdev->dismantle)
5929 		return __igc_close(netdev, false);
5930 	return 0;
5931 }
5932 
5933 /**
5934  * igc_ioctl - Access the hwtstamp interface
5935  * @netdev: network interface device structure
5936  * @ifr: interface request data
5937  * @cmd: ioctl command
5938  **/
5939 static int igc_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
5940 {
5941 	switch (cmd) {
5942 	case SIOCGHWTSTAMP:
5943 		return igc_ptp_get_ts_config(netdev, ifr);
5944 	case SIOCSHWTSTAMP:
5945 		return igc_ptp_set_ts_config(netdev, ifr);
5946 	default:
5947 		return -EOPNOTSUPP;
5948 	}
5949 }
5950 
5951 static int igc_save_launchtime_params(struct igc_adapter *adapter, int queue,
5952 				      bool enable)
5953 {
5954 	struct igc_ring *ring;
5955 
5956 	if (queue < 0 || queue >= adapter->num_tx_queues)
5957 		return -EINVAL;
5958 
5959 	ring = adapter->tx_ring[queue];
5960 	ring->launchtime_enable = enable;
5961 
5962 	return 0;
5963 }
5964 
5965 static bool is_base_time_past(ktime_t base_time, const struct timespec64 *now)
5966 {
5967 	struct timespec64 b;
5968 
5969 	b = ktime_to_timespec64(base_time);
5970 
5971 	return timespec64_compare(now, &b) > 0;
5972 }
5973 
5974 static bool validate_schedule(struct igc_adapter *adapter,
5975 			      const struct tc_taprio_qopt_offload *qopt)
5976 {
5977 	int queue_uses[IGC_MAX_TX_QUEUES] = { };
5978 	struct igc_hw *hw = &adapter->hw;
5979 	struct timespec64 now;
5980 	size_t n;
5981 
5982 	if (qopt->cycle_time_extension)
5983 		return false;
5984 
5985 	igc_ptp_read(adapter, &now);
5986 
5987 	/* If we program the controller's BASET registers with a time
5988 	 * in the future, it will hold all the packets until that
5989 	 * time, causing a lot of TX Hangs, so to avoid that, we
5990 	 * reject schedules that would start in the future.
5991 	 * Note: Limitation above is no longer in i226.
5992 	 */
5993 	if (!is_base_time_past(qopt->base_time, &now) &&
5994 	    igc_is_device_id_i225(hw))
5995 		return false;
5996 
5997 	for (n = 0; n < qopt->num_entries; n++) {
5998 		const struct tc_taprio_sched_entry *e, *prev;
5999 		int i;
6000 
6001 		prev = n ? &qopt->entries[n - 1] : NULL;
6002 		e = &qopt->entries[n];
6003 
6004 		/* i225 only supports "global" frame preemption
6005 		 * settings.
6006 		 */
6007 		if (e->command != TC_TAPRIO_CMD_SET_GATES)
6008 			return false;
6009 
6010 		for (i = 0; i < adapter->num_tx_queues; i++)
6011 			if (e->gate_mask & BIT(i)) {
6012 				queue_uses[i]++;
6013 
6014 				/* There are limitations: A single queue cannot
6015 				 * be opened and closed multiple times per cycle
6016 				 * unless the gate stays open. Check for it.
6017 				 */
6018 				if (queue_uses[i] > 1 &&
6019 				    !(prev->gate_mask & BIT(i)))
6020 					return false;
6021 			}
6022 	}
6023 
6024 	return true;
6025 }
6026 
6027 static int igc_tsn_enable_launchtime(struct igc_adapter *adapter,
6028 				     struct tc_etf_qopt_offload *qopt)
6029 {
6030 	struct igc_hw *hw = &adapter->hw;
6031 	int err;
6032 
6033 	if (hw->mac.type != igc_i225)
6034 		return -EOPNOTSUPP;
6035 
6036 	err = igc_save_launchtime_params(adapter, qopt->queue, qopt->enable);
6037 	if (err)
6038 		return err;
6039 
6040 	return igc_tsn_offload_apply(adapter);
6041 }
6042 
6043 static int igc_tsn_clear_schedule(struct igc_adapter *adapter)
6044 {
6045 	int i;
6046 
6047 	adapter->base_time = 0;
6048 	adapter->cycle_time = NSEC_PER_SEC;
6049 	adapter->qbv_config_change_errors = 0;
6050 
6051 	for (i = 0; i < adapter->num_tx_queues; i++) {
6052 		struct igc_ring *ring = adapter->tx_ring[i];
6053 
6054 		ring->start_time = 0;
6055 		ring->end_time = NSEC_PER_SEC;
6056 		ring->max_sdu = 0;
6057 	}
6058 
6059 	return 0;
6060 }
6061 
6062 static int igc_save_qbv_schedule(struct igc_adapter *adapter,
6063 				 struct tc_taprio_qopt_offload *qopt)
6064 {
6065 	bool queue_configured[IGC_MAX_TX_QUEUES] = { };
6066 	struct igc_hw *hw = &adapter->hw;
6067 	u32 start_time = 0, end_time = 0;
6068 	size_t n;
6069 	int i;
6070 
6071 	adapter->qbv_enable = qopt->enable;
6072 
6073 	if (!qopt->enable)
6074 		return igc_tsn_clear_schedule(adapter);
6075 
6076 	if (qopt->base_time < 0)
6077 		return -ERANGE;
6078 
6079 	if (igc_is_device_id_i225(hw) && adapter->base_time)
6080 		return -EALREADY;
6081 
6082 	if (!validate_schedule(adapter, qopt))
6083 		return -EINVAL;
6084 
6085 	adapter->cycle_time = qopt->cycle_time;
6086 	adapter->base_time = qopt->base_time;
6087 
6088 	for (n = 0; n < qopt->num_entries; n++) {
6089 		struct tc_taprio_sched_entry *e = &qopt->entries[n];
6090 
6091 		end_time += e->interval;
6092 
6093 		/* If any of the conditions below are true, we need to manually
6094 		 * control the end time of the cycle.
6095 		 * 1. Qbv users can specify a cycle time that is not equal
6096 		 * to the total GCL intervals. Hence, recalculation is
6097 		 * necessary here to exclude the time interval that
6098 		 * exceeds the cycle time.
6099 		 * 2. According to IEEE Std. 802.1Q-2018 section 8.6.9.2,
6100 		 * once the end of the list is reached, it will switch
6101 		 * to the END_OF_CYCLE state and leave the gates in the
6102 		 * same state until the next cycle is started.
6103 		 */
6104 		if (end_time > adapter->cycle_time ||
6105 		    n + 1 == qopt->num_entries)
6106 			end_time = adapter->cycle_time;
6107 
6108 		for (i = 0; i < adapter->num_tx_queues; i++) {
6109 			struct igc_ring *ring = adapter->tx_ring[i];
6110 
6111 			if (!(e->gate_mask & BIT(i)))
6112 				continue;
6113 
6114 			/* Check whether a queue stays open for more than one
6115 			 * entry. If so, keep the start and advance the end
6116 			 * time.
6117 			 */
6118 			if (!queue_configured[i])
6119 				ring->start_time = start_time;
6120 			ring->end_time = end_time;
6121 
6122 			queue_configured[i] = true;
6123 		}
6124 
6125 		start_time += e->interval;
6126 	}
6127 
6128 	/* Check whether a queue gets configured.
6129 	 * If not, set the start and end time to be end time.
6130 	 */
6131 	for (i = 0; i < adapter->num_tx_queues; i++) {
6132 		if (!queue_configured[i]) {
6133 			struct igc_ring *ring = adapter->tx_ring[i];
6134 
6135 			ring->start_time = end_time;
6136 			ring->end_time = end_time;
6137 		}
6138 	}
6139 
6140 	for (i = 0; i < adapter->num_tx_queues; i++) {
6141 		struct igc_ring *ring = adapter->tx_ring[i];
6142 		struct net_device *dev = adapter->netdev;
6143 
6144 		if (qopt->max_sdu[i])
6145 			ring->max_sdu = qopt->max_sdu[i] + dev->hard_header_len;
6146 		else
6147 			ring->max_sdu = 0;
6148 	}
6149 
6150 	return 0;
6151 }
6152 
6153 static int igc_tsn_enable_qbv_scheduling(struct igc_adapter *adapter,
6154 					 struct tc_taprio_qopt_offload *qopt)
6155 {
6156 	struct igc_hw *hw = &adapter->hw;
6157 	int err;
6158 
6159 	if (hw->mac.type != igc_i225)
6160 		return -EOPNOTSUPP;
6161 
6162 	err = igc_save_qbv_schedule(adapter, qopt);
6163 	if (err)
6164 		return err;
6165 
6166 	return igc_tsn_offload_apply(adapter);
6167 }
6168 
6169 static int igc_save_cbs_params(struct igc_adapter *adapter, int queue,
6170 			       bool enable, int idleslope, int sendslope,
6171 			       int hicredit, int locredit)
6172 {
6173 	bool cbs_status[IGC_MAX_SR_QUEUES] = { false };
6174 	struct net_device *netdev = adapter->netdev;
6175 	struct igc_ring *ring;
6176 	int i;
6177 
6178 	/* i225 has two sets of credit-based shaper logic.
6179 	 * Supporting it only on the top two priority queues
6180 	 */
6181 	if (queue < 0 || queue > 1)
6182 		return -EINVAL;
6183 
6184 	ring = adapter->tx_ring[queue];
6185 
6186 	for (i = 0; i < IGC_MAX_SR_QUEUES; i++)
6187 		if (adapter->tx_ring[i])
6188 			cbs_status[i] = adapter->tx_ring[i]->cbs_enable;
6189 
6190 	/* CBS should be enabled on the highest priority queue first in order
6191 	 * for the CBS algorithm to operate as intended.
6192 	 */
6193 	if (enable) {
6194 		if (queue == 1 && !cbs_status[0]) {
6195 			netdev_err(netdev,
6196 				   "Enabling CBS on queue1 before queue0\n");
6197 			return -EINVAL;
6198 		}
6199 	} else {
6200 		if (queue == 0 && cbs_status[1]) {
6201 			netdev_err(netdev,
6202 				   "Disabling CBS on queue0 before queue1\n");
6203 			return -EINVAL;
6204 		}
6205 	}
6206 
6207 	ring->cbs_enable = enable;
6208 	ring->idleslope = idleslope;
6209 	ring->sendslope = sendslope;
6210 	ring->hicredit = hicredit;
6211 	ring->locredit = locredit;
6212 
6213 	return 0;
6214 }
6215 
6216 static int igc_tsn_enable_cbs(struct igc_adapter *adapter,
6217 			      struct tc_cbs_qopt_offload *qopt)
6218 {
6219 	struct igc_hw *hw = &adapter->hw;
6220 	int err;
6221 
6222 	if (hw->mac.type != igc_i225)
6223 		return -EOPNOTSUPP;
6224 
6225 	if (qopt->queue < 0 || qopt->queue > 1)
6226 		return -EINVAL;
6227 
6228 	err = igc_save_cbs_params(adapter, qopt->queue, qopt->enable,
6229 				  qopt->idleslope, qopt->sendslope,
6230 				  qopt->hicredit, qopt->locredit);
6231 	if (err)
6232 		return err;
6233 
6234 	return igc_tsn_offload_apply(adapter);
6235 }
6236 
6237 static int igc_tc_query_caps(struct igc_adapter *adapter,
6238 			     struct tc_query_caps_base *base)
6239 {
6240 	struct igc_hw *hw = &adapter->hw;
6241 
6242 	switch (base->type) {
6243 	case TC_SETUP_QDISC_TAPRIO: {
6244 		struct tc_taprio_caps *caps = base->caps;
6245 
6246 		caps->broken_mqprio = true;
6247 
6248 		if (hw->mac.type == igc_i225) {
6249 			caps->supports_queue_max_sdu = true;
6250 			caps->gate_mask_per_txq = true;
6251 		}
6252 
6253 		return 0;
6254 	}
6255 	default:
6256 		return -EOPNOTSUPP;
6257 	}
6258 }
6259 
6260 static int igc_setup_tc(struct net_device *dev, enum tc_setup_type type,
6261 			void *type_data)
6262 {
6263 	struct igc_adapter *adapter = netdev_priv(dev);
6264 
6265 	switch (type) {
6266 	case TC_QUERY_CAPS:
6267 		return igc_tc_query_caps(adapter, type_data);
6268 	case TC_SETUP_QDISC_TAPRIO:
6269 		return igc_tsn_enable_qbv_scheduling(adapter, type_data);
6270 
6271 	case TC_SETUP_QDISC_ETF:
6272 		return igc_tsn_enable_launchtime(adapter, type_data);
6273 
6274 	case TC_SETUP_QDISC_CBS:
6275 		return igc_tsn_enable_cbs(adapter, type_data);
6276 
6277 	default:
6278 		return -EOPNOTSUPP;
6279 	}
6280 }
6281 
6282 static int igc_bpf(struct net_device *dev, struct netdev_bpf *bpf)
6283 {
6284 	struct igc_adapter *adapter = netdev_priv(dev);
6285 
6286 	switch (bpf->command) {
6287 	case XDP_SETUP_PROG:
6288 		return igc_xdp_set_prog(adapter, bpf->prog, bpf->extack);
6289 	case XDP_SETUP_XSK_POOL:
6290 		return igc_xdp_setup_pool(adapter, bpf->xsk.pool,
6291 					  bpf->xsk.queue_id);
6292 	default:
6293 		return -EOPNOTSUPP;
6294 	}
6295 }
6296 
6297 static int igc_xdp_xmit(struct net_device *dev, int num_frames,
6298 			struct xdp_frame **frames, u32 flags)
6299 {
6300 	struct igc_adapter *adapter = netdev_priv(dev);
6301 	int cpu = smp_processor_id();
6302 	struct netdev_queue *nq;
6303 	struct igc_ring *ring;
6304 	int i, drops;
6305 
6306 	if (unlikely(test_bit(__IGC_DOWN, &adapter->state)))
6307 		return -ENETDOWN;
6308 
6309 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
6310 		return -EINVAL;
6311 
6312 	ring = igc_xdp_get_tx_ring(adapter, cpu);
6313 	nq = txring_txq(ring);
6314 
6315 	__netif_tx_lock(nq, cpu);
6316 
6317 	drops = 0;
6318 	for (i = 0; i < num_frames; i++) {
6319 		int err;
6320 		struct xdp_frame *xdpf = frames[i];
6321 
6322 		err = igc_xdp_init_tx_descriptor(ring, xdpf);
6323 		if (err) {
6324 			xdp_return_frame_rx_napi(xdpf);
6325 			drops++;
6326 		}
6327 	}
6328 
6329 	if (flags & XDP_XMIT_FLUSH)
6330 		igc_flush_tx_descriptors(ring);
6331 
6332 	__netif_tx_unlock(nq);
6333 
6334 	return num_frames - drops;
6335 }
6336 
6337 static void igc_trigger_rxtxq_interrupt(struct igc_adapter *adapter,
6338 					struct igc_q_vector *q_vector)
6339 {
6340 	struct igc_hw *hw = &adapter->hw;
6341 	u32 eics = 0;
6342 
6343 	eics |= q_vector->eims_value;
6344 	wr32(IGC_EICS, eics);
6345 }
6346 
6347 int igc_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
6348 {
6349 	struct igc_adapter *adapter = netdev_priv(dev);
6350 	struct igc_q_vector *q_vector;
6351 	struct igc_ring *ring;
6352 
6353 	if (test_bit(__IGC_DOWN, &adapter->state))
6354 		return -ENETDOWN;
6355 
6356 	if (!igc_xdp_is_enabled(adapter))
6357 		return -ENXIO;
6358 
6359 	if (queue_id >= adapter->num_rx_queues)
6360 		return -EINVAL;
6361 
6362 	ring = adapter->rx_ring[queue_id];
6363 
6364 	if (!ring->xsk_pool)
6365 		return -ENXIO;
6366 
6367 	q_vector = adapter->q_vector[queue_id];
6368 	if (!napi_if_scheduled_mark_missed(&q_vector->napi))
6369 		igc_trigger_rxtxq_interrupt(adapter, q_vector);
6370 
6371 	return 0;
6372 }
6373 
6374 static const struct net_device_ops igc_netdev_ops = {
6375 	.ndo_open		= igc_open,
6376 	.ndo_stop		= igc_close,
6377 	.ndo_start_xmit		= igc_xmit_frame,
6378 	.ndo_set_rx_mode	= igc_set_rx_mode,
6379 	.ndo_set_mac_address	= igc_set_mac,
6380 	.ndo_change_mtu		= igc_change_mtu,
6381 	.ndo_tx_timeout		= igc_tx_timeout,
6382 	.ndo_get_stats64	= igc_get_stats64,
6383 	.ndo_fix_features	= igc_fix_features,
6384 	.ndo_set_features	= igc_set_features,
6385 	.ndo_features_check	= igc_features_check,
6386 	.ndo_eth_ioctl		= igc_ioctl,
6387 	.ndo_setup_tc		= igc_setup_tc,
6388 	.ndo_bpf		= igc_bpf,
6389 	.ndo_xdp_xmit		= igc_xdp_xmit,
6390 	.ndo_xsk_wakeup		= igc_xsk_wakeup,
6391 };
6392 
6393 /* PCIe configuration access */
6394 void igc_read_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
6395 {
6396 	struct igc_adapter *adapter = hw->back;
6397 
6398 	pci_read_config_word(adapter->pdev, reg, value);
6399 }
6400 
6401 void igc_write_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
6402 {
6403 	struct igc_adapter *adapter = hw->back;
6404 
6405 	pci_write_config_word(adapter->pdev, reg, *value);
6406 }
6407 
6408 s32 igc_read_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
6409 {
6410 	struct igc_adapter *adapter = hw->back;
6411 
6412 	if (!pci_is_pcie(adapter->pdev))
6413 		return -IGC_ERR_CONFIG;
6414 
6415 	pcie_capability_read_word(adapter->pdev, reg, value);
6416 
6417 	return IGC_SUCCESS;
6418 }
6419 
6420 s32 igc_write_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
6421 {
6422 	struct igc_adapter *adapter = hw->back;
6423 
6424 	if (!pci_is_pcie(adapter->pdev))
6425 		return -IGC_ERR_CONFIG;
6426 
6427 	pcie_capability_write_word(adapter->pdev, reg, *value);
6428 
6429 	return IGC_SUCCESS;
6430 }
6431 
6432 u32 igc_rd32(struct igc_hw *hw, u32 reg)
6433 {
6434 	struct igc_adapter *igc = container_of(hw, struct igc_adapter, hw);
6435 	u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
6436 	u32 value = 0;
6437 
6438 	if (IGC_REMOVED(hw_addr))
6439 		return ~value;
6440 
6441 	value = readl(&hw_addr[reg]);
6442 
6443 	/* reads should not return all F's */
6444 	if (!(~value) && (!reg || !(~readl(hw_addr)))) {
6445 		struct net_device *netdev = igc->netdev;
6446 
6447 		hw->hw_addr = NULL;
6448 		netif_device_detach(netdev);
6449 		netdev_err(netdev, "PCIe link lost, device now detached\n");
6450 		WARN(pci_device_is_present(igc->pdev),
6451 		     "igc: Failed to read reg 0x%x!\n", reg);
6452 	}
6453 
6454 	return value;
6455 }
6456 
6457 /**
6458  * igc_probe - Device Initialization Routine
6459  * @pdev: PCI device information struct
6460  * @ent: entry in igc_pci_tbl
6461  *
6462  * Returns 0 on success, negative on failure
6463  *
6464  * igc_probe initializes an adapter identified by a pci_dev structure.
6465  * The OS initialization, configuring the adapter private structure,
6466  * and a hardware reset occur.
6467  */
6468 static int igc_probe(struct pci_dev *pdev,
6469 		     const struct pci_device_id *ent)
6470 {
6471 	struct igc_adapter *adapter;
6472 	struct net_device *netdev;
6473 	struct igc_hw *hw;
6474 	const struct igc_info *ei = igc_info_tbl[ent->driver_data];
6475 	int err;
6476 
6477 	err = pci_enable_device_mem(pdev);
6478 	if (err)
6479 		return err;
6480 
6481 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
6482 	if (err) {
6483 		dev_err(&pdev->dev,
6484 			"No usable DMA configuration, aborting\n");
6485 		goto err_dma;
6486 	}
6487 
6488 	err = pci_request_mem_regions(pdev, igc_driver_name);
6489 	if (err)
6490 		goto err_pci_reg;
6491 
6492 	err = pci_enable_ptm(pdev, NULL);
6493 	if (err < 0)
6494 		dev_info(&pdev->dev, "PCIe PTM not supported by PCIe bus/controller\n");
6495 
6496 	pci_set_master(pdev);
6497 
6498 	err = -ENOMEM;
6499 	netdev = alloc_etherdev_mq(sizeof(struct igc_adapter),
6500 				   IGC_MAX_TX_QUEUES);
6501 
6502 	if (!netdev)
6503 		goto err_alloc_etherdev;
6504 
6505 	SET_NETDEV_DEV(netdev, &pdev->dev);
6506 
6507 	pci_set_drvdata(pdev, netdev);
6508 	adapter = netdev_priv(netdev);
6509 	adapter->netdev = netdev;
6510 	adapter->pdev = pdev;
6511 	hw = &adapter->hw;
6512 	hw->back = adapter;
6513 	adapter->port_num = hw->bus.func;
6514 	adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
6515 
6516 	err = pci_save_state(pdev);
6517 	if (err)
6518 		goto err_ioremap;
6519 
6520 	err = -EIO;
6521 	adapter->io_addr = ioremap(pci_resource_start(pdev, 0),
6522 				   pci_resource_len(pdev, 0));
6523 	if (!adapter->io_addr)
6524 		goto err_ioremap;
6525 
6526 	/* hw->hw_addr can be zeroed, so use adapter->io_addr for unmap */
6527 	hw->hw_addr = adapter->io_addr;
6528 
6529 	netdev->netdev_ops = &igc_netdev_ops;
6530 	igc_ethtool_set_ops(netdev);
6531 	netdev->watchdog_timeo = 5 * HZ;
6532 
6533 	netdev->mem_start = pci_resource_start(pdev, 0);
6534 	netdev->mem_end = pci_resource_end(pdev, 0);
6535 
6536 	/* PCI config space info */
6537 	hw->vendor_id = pdev->vendor;
6538 	hw->device_id = pdev->device;
6539 	hw->revision_id = pdev->revision;
6540 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
6541 	hw->subsystem_device_id = pdev->subsystem_device;
6542 
6543 	/* Copy the default MAC and PHY function pointers */
6544 	memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
6545 	memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
6546 
6547 	/* Initialize skew-specific constants */
6548 	err = ei->get_invariants(hw);
6549 	if (err)
6550 		goto err_sw_init;
6551 
6552 	/* Add supported features to the features list*/
6553 	netdev->features |= NETIF_F_SG;
6554 	netdev->features |= NETIF_F_TSO;
6555 	netdev->features |= NETIF_F_TSO6;
6556 	netdev->features |= NETIF_F_TSO_ECN;
6557 	netdev->features |= NETIF_F_RXCSUM;
6558 	netdev->features |= NETIF_F_HW_CSUM;
6559 	netdev->features |= NETIF_F_SCTP_CRC;
6560 	netdev->features |= NETIF_F_HW_TC;
6561 
6562 #define IGC_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
6563 				  NETIF_F_GSO_GRE_CSUM | \
6564 				  NETIF_F_GSO_IPXIP4 | \
6565 				  NETIF_F_GSO_IPXIP6 | \
6566 				  NETIF_F_GSO_UDP_TUNNEL | \
6567 				  NETIF_F_GSO_UDP_TUNNEL_CSUM)
6568 
6569 	netdev->gso_partial_features = IGC_GSO_PARTIAL_FEATURES;
6570 	netdev->features |= NETIF_F_GSO_PARTIAL | IGC_GSO_PARTIAL_FEATURES;
6571 
6572 	/* setup the private structure */
6573 	err = igc_sw_init(adapter);
6574 	if (err)
6575 		goto err_sw_init;
6576 
6577 	/* copy netdev features into list of user selectable features */
6578 	netdev->hw_features |= NETIF_F_NTUPLE;
6579 	netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
6580 	netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
6581 	netdev->hw_features |= netdev->features;
6582 
6583 	netdev->features |= NETIF_F_HIGHDMA;
6584 
6585 	netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
6586 	netdev->mpls_features |= NETIF_F_HW_CSUM;
6587 	netdev->hw_enc_features |= netdev->vlan_features;
6588 
6589 	netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
6590 			       NETDEV_XDP_ACT_XSK_ZEROCOPY;
6591 
6592 	/* MTU range: 68 - 9216 */
6593 	netdev->min_mtu = ETH_MIN_MTU;
6594 	netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
6595 
6596 	/* before reading the NVM, reset the controller to put the device in a
6597 	 * known good starting state
6598 	 */
6599 	hw->mac.ops.reset_hw(hw);
6600 
6601 	if (igc_get_flash_presence_i225(hw)) {
6602 		if (hw->nvm.ops.validate(hw) < 0) {
6603 			dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
6604 			err = -EIO;
6605 			goto err_eeprom;
6606 		}
6607 	}
6608 
6609 	if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
6610 		/* copy the MAC address out of the NVM */
6611 		if (hw->mac.ops.read_mac_addr(hw))
6612 			dev_err(&pdev->dev, "NVM Read Error\n");
6613 	}
6614 
6615 	eth_hw_addr_set(netdev, hw->mac.addr);
6616 
6617 	if (!is_valid_ether_addr(netdev->dev_addr)) {
6618 		dev_err(&pdev->dev, "Invalid MAC Address\n");
6619 		err = -EIO;
6620 		goto err_eeprom;
6621 	}
6622 
6623 	/* configure RXPBSIZE and TXPBSIZE */
6624 	wr32(IGC_RXPBS, I225_RXPBSIZE_DEFAULT);
6625 	wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT);
6626 
6627 	timer_setup(&adapter->watchdog_timer, igc_watchdog, 0);
6628 	timer_setup(&adapter->phy_info_timer, igc_update_phy_info, 0);
6629 
6630 	INIT_WORK(&adapter->reset_task, igc_reset_task);
6631 	INIT_WORK(&adapter->watchdog_task, igc_watchdog_task);
6632 
6633 	/* Initialize link properties that are user-changeable */
6634 	adapter->fc_autoneg = true;
6635 	hw->mac.autoneg = true;
6636 	hw->phy.autoneg_advertised = 0xaf;
6637 
6638 	hw->fc.requested_mode = igc_fc_default;
6639 	hw->fc.current_mode = igc_fc_default;
6640 
6641 	/* By default, support wake on port A */
6642 	adapter->flags |= IGC_FLAG_WOL_SUPPORTED;
6643 
6644 	/* initialize the wol settings based on the eeprom settings */
6645 	if (adapter->flags & IGC_FLAG_WOL_SUPPORTED)
6646 		adapter->wol |= IGC_WUFC_MAG;
6647 
6648 	device_set_wakeup_enable(&adapter->pdev->dev,
6649 				 adapter->flags & IGC_FLAG_WOL_SUPPORTED);
6650 
6651 	igc_ptp_init(adapter);
6652 
6653 	igc_tsn_clear_schedule(adapter);
6654 
6655 	/* reset the hardware with the new settings */
6656 	igc_reset(adapter);
6657 
6658 	/* let the f/w know that the h/w is now under the control of the
6659 	 * driver.
6660 	 */
6661 	igc_get_hw_control(adapter);
6662 
6663 	strncpy(netdev->name, "eth%d", IFNAMSIZ);
6664 	err = register_netdev(netdev);
6665 	if (err)
6666 		goto err_register;
6667 
6668 	 /* carrier off reporting is important to ethtool even BEFORE open */
6669 	netif_carrier_off(netdev);
6670 
6671 	/* Check if Media Autosense is enabled */
6672 	adapter->ei = *ei;
6673 
6674 	/* print pcie link status and MAC address */
6675 	pcie_print_link_status(pdev);
6676 	netdev_info(netdev, "MAC: %pM\n", netdev->dev_addr);
6677 
6678 	dev_pm_set_driver_flags(&pdev->dev, DPM_FLAG_NO_DIRECT_COMPLETE);
6679 	/* Disable EEE for internal PHY devices */
6680 	hw->dev_spec._base.eee_enable = false;
6681 	adapter->flags &= ~IGC_FLAG_EEE;
6682 	igc_set_eee_i225(hw, false, false, false);
6683 
6684 	pm_runtime_put_noidle(&pdev->dev);
6685 
6686 	return 0;
6687 
6688 err_register:
6689 	igc_release_hw_control(adapter);
6690 err_eeprom:
6691 	if (!igc_check_reset_block(hw))
6692 		igc_reset_phy(hw);
6693 err_sw_init:
6694 	igc_clear_interrupt_scheme(adapter);
6695 	iounmap(adapter->io_addr);
6696 err_ioremap:
6697 	free_netdev(netdev);
6698 err_alloc_etherdev:
6699 	pci_release_mem_regions(pdev);
6700 err_pci_reg:
6701 err_dma:
6702 	pci_disable_device(pdev);
6703 	return err;
6704 }
6705 
6706 /**
6707  * igc_remove - Device Removal Routine
6708  * @pdev: PCI device information struct
6709  *
6710  * igc_remove is called by the PCI subsystem to alert the driver
6711  * that it should release a PCI device.  This could be caused by a
6712  * Hot-Plug event, or because the driver is going to be removed from
6713  * memory.
6714  */
6715 static void igc_remove(struct pci_dev *pdev)
6716 {
6717 	struct net_device *netdev = pci_get_drvdata(pdev);
6718 	struct igc_adapter *adapter = netdev_priv(netdev);
6719 
6720 	pm_runtime_get_noresume(&pdev->dev);
6721 
6722 	igc_flush_nfc_rules(adapter);
6723 
6724 	igc_ptp_stop(adapter);
6725 
6726 	set_bit(__IGC_DOWN, &adapter->state);
6727 
6728 	del_timer_sync(&adapter->watchdog_timer);
6729 	del_timer_sync(&adapter->phy_info_timer);
6730 
6731 	cancel_work_sync(&adapter->reset_task);
6732 	cancel_work_sync(&adapter->watchdog_task);
6733 
6734 	/* Release control of h/w to f/w.  If f/w is AMT enabled, this
6735 	 * would have already happened in close and is redundant.
6736 	 */
6737 	igc_release_hw_control(adapter);
6738 	unregister_netdev(netdev);
6739 
6740 	igc_clear_interrupt_scheme(adapter);
6741 	pci_iounmap(pdev, adapter->io_addr);
6742 	pci_release_mem_regions(pdev);
6743 
6744 	free_netdev(netdev);
6745 
6746 	pci_disable_device(pdev);
6747 }
6748 
6749 static int __igc_shutdown(struct pci_dev *pdev, bool *enable_wake,
6750 			  bool runtime)
6751 {
6752 	struct net_device *netdev = pci_get_drvdata(pdev);
6753 	struct igc_adapter *adapter = netdev_priv(netdev);
6754 	u32 wufc = runtime ? IGC_WUFC_LNKC : adapter->wol;
6755 	struct igc_hw *hw = &adapter->hw;
6756 	u32 ctrl, rctl, status;
6757 	bool wake;
6758 
6759 	rtnl_lock();
6760 	netif_device_detach(netdev);
6761 
6762 	if (netif_running(netdev))
6763 		__igc_close(netdev, true);
6764 
6765 	igc_ptp_suspend(adapter);
6766 
6767 	igc_clear_interrupt_scheme(adapter);
6768 	rtnl_unlock();
6769 
6770 	status = rd32(IGC_STATUS);
6771 	if (status & IGC_STATUS_LU)
6772 		wufc &= ~IGC_WUFC_LNKC;
6773 
6774 	if (wufc) {
6775 		igc_setup_rctl(adapter);
6776 		igc_set_rx_mode(netdev);
6777 
6778 		/* turn on all-multi mode if wake on multicast is enabled */
6779 		if (wufc & IGC_WUFC_MC) {
6780 			rctl = rd32(IGC_RCTL);
6781 			rctl |= IGC_RCTL_MPE;
6782 			wr32(IGC_RCTL, rctl);
6783 		}
6784 
6785 		ctrl = rd32(IGC_CTRL);
6786 		ctrl |= IGC_CTRL_ADVD3WUC;
6787 		wr32(IGC_CTRL, ctrl);
6788 
6789 		/* Allow time for pending master requests to run */
6790 		igc_disable_pcie_master(hw);
6791 
6792 		wr32(IGC_WUC, IGC_WUC_PME_EN);
6793 		wr32(IGC_WUFC, wufc);
6794 	} else {
6795 		wr32(IGC_WUC, 0);
6796 		wr32(IGC_WUFC, 0);
6797 	}
6798 
6799 	wake = wufc || adapter->en_mng_pt;
6800 	if (!wake)
6801 		igc_power_down_phy_copper_base(&adapter->hw);
6802 	else
6803 		igc_power_up_link(adapter);
6804 
6805 	if (enable_wake)
6806 		*enable_wake = wake;
6807 
6808 	/* Release control of h/w to f/w.  If f/w is AMT enabled, this
6809 	 * would have already happened in close and is redundant.
6810 	 */
6811 	igc_release_hw_control(adapter);
6812 
6813 	pci_disable_device(pdev);
6814 
6815 	return 0;
6816 }
6817 
6818 #ifdef CONFIG_PM
6819 static int __maybe_unused igc_runtime_suspend(struct device *dev)
6820 {
6821 	return __igc_shutdown(to_pci_dev(dev), NULL, 1);
6822 }
6823 
6824 static void igc_deliver_wake_packet(struct net_device *netdev)
6825 {
6826 	struct igc_adapter *adapter = netdev_priv(netdev);
6827 	struct igc_hw *hw = &adapter->hw;
6828 	struct sk_buff *skb;
6829 	u32 wupl;
6830 
6831 	wupl = rd32(IGC_WUPL) & IGC_WUPL_MASK;
6832 
6833 	/* WUPM stores only the first 128 bytes of the wake packet.
6834 	 * Read the packet only if we have the whole thing.
6835 	 */
6836 	if (wupl == 0 || wupl > IGC_WUPM_BYTES)
6837 		return;
6838 
6839 	skb = netdev_alloc_skb_ip_align(netdev, IGC_WUPM_BYTES);
6840 	if (!skb)
6841 		return;
6842 
6843 	skb_put(skb, wupl);
6844 
6845 	/* Ensure reads are 32-bit aligned */
6846 	wupl = roundup(wupl, 4);
6847 
6848 	memcpy_fromio(skb->data, hw->hw_addr + IGC_WUPM_REG(0), wupl);
6849 
6850 	skb->protocol = eth_type_trans(skb, netdev);
6851 	netif_rx(skb);
6852 }
6853 
6854 static int __maybe_unused igc_resume(struct device *dev)
6855 {
6856 	struct pci_dev *pdev = to_pci_dev(dev);
6857 	struct net_device *netdev = pci_get_drvdata(pdev);
6858 	struct igc_adapter *adapter = netdev_priv(netdev);
6859 	struct igc_hw *hw = &adapter->hw;
6860 	u32 err, val;
6861 
6862 	pci_set_power_state(pdev, PCI_D0);
6863 	pci_restore_state(pdev);
6864 	pci_save_state(pdev);
6865 
6866 	if (!pci_device_is_present(pdev))
6867 		return -ENODEV;
6868 	err = pci_enable_device_mem(pdev);
6869 	if (err) {
6870 		netdev_err(netdev, "Cannot enable PCI device from suspend\n");
6871 		return err;
6872 	}
6873 	pci_set_master(pdev);
6874 
6875 	pci_enable_wake(pdev, PCI_D3hot, 0);
6876 	pci_enable_wake(pdev, PCI_D3cold, 0);
6877 
6878 	if (igc_init_interrupt_scheme(adapter, true)) {
6879 		netdev_err(netdev, "Unable to allocate memory for queues\n");
6880 		return -ENOMEM;
6881 	}
6882 
6883 	igc_reset(adapter);
6884 
6885 	/* let the f/w know that the h/w is now under the control of the
6886 	 * driver.
6887 	 */
6888 	igc_get_hw_control(adapter);
6889 
6890 	val = rd32(IGC_WUS);
6891 	if (val & WAKE_PKT_WUS)
6892 		igc_deliver_wake_packet(netdev);
6893 
6894 	wr32(IGC_WUS, ~0);
6895 
6896 	rtnl_lock();
6897 	if (!err && netif_running(netdev))
6898 		err = __igc_open(netdev, true);
6899 
6900 	if (!err)
6901 		netif_device_attach(netdev);
6902 	rtnl_unlock();
6903 
6904 	return err;
6905 }
6906 
6907 static int __maybe_unused igc_runtime_resume(struct device *dev)
6908 {
6909 	return igc_resume(dev);
6910 }
6911 
6912 static int __maybe_unused igc_suspend(struct device *dev)
6913 {
6914 	return __igc_shutdown(to_pci_dev(dev), NULL, 0);
6915 }
6916 
6917 static int __maybe_unused igc_runtime_idle(struct device *dev)
6918 {
6919 	struct net_device *netdev = dev_get_drvdata(dev);
6920 	struct igc_adapter *adapter = netdev_priv(netdev);
6921 
6922 	if (!igc_has_link(adapter))
6923 		pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
6924 
6925 	return -EBUSY;
6926 }
6927 #endif /* CONFIG_PM */
6928 
6929 static void igc_shutdown(struct pci_dev *pdev)
6930 {
6931 	bool wake;
6932 
6933 	__igc_shutdown(pdev, &wake, 0);
6934 
6935 	if (system_state == SYSTEM_POWER_OFF) {
6936 		pci_wake_from_d3(pdev, wake);
6937 		pci_set_power_state(pdev, PCI_D3hot);
6938 	}
6939 }
6940 
6941 /**
6942  *  igc_io_error_detected - called when PCI error is detected
6943  *  @pdev: Pointer to PCI device
6944  *  @state: The current PCI connection state
6945  *
6946  *  This function is called after a PCI bus error affecting
6947  *  this device has been detected.
6948  **/
6949 static pci_ers_result_t igc_io_error_detected(struct pci_dev *pdev,
6950 					      pci_channel_state_t state)
6951 {
6952 	struct net_device *netdev = pci_get_drvdata(pdev);
6953 	struct igc_adapter *adapter = netdev_priv(netdev);
6954 
6955 	netif_device_detach(netdev);
6956 
6957 	if (state == pci_channel_io_perm_failure)
6958 		return PCI_ERS_RESULT_DISCONNECT;
6959 
6960 	if (netif_running(netdev))
6961 		igc_down(adapter);
6962 	pci_disable_device(pdev);
6963 
6964 	/* Request a slot reset. */
6965 	return PCI_ERS_RESULT_NEED_RESET;
6966 }
6967 
6968 /**
6969  *  igc_io_slot_reset - called after the PCI bus has been reset.
6970  *  @pdev: Pointer to PCI device
6971  *
6972  *  Restart the card from scratch, as if from a cold-boot. Implementation
6973  *  resembles the first-half of the igc_resume routine.
6974  **/
6975 static pci_ers_result_t igc_io_slot_reset(struct pci_dev *pdev)
6976 {
6977 	struct net_device *netdev = pci_get_drvdata(pdev);
6978 	struct igc_adapter *adapter = netdev_priv(netdev);
6979 	struct igc_hw *hw = &adapter->hw;
6980 	pci_ers_result_t result;
6981 
6982 	if (pci_enable_device_mem(pdev)) {
6983 		netdev_err(netdev, "Could not re-enable PCI device after reset\n");
6984 		result = PCI_ERS_RESULT_DISCONNECT;
6985 	} else {
6986 		pci_set_master(pdev);
6987 		pci_restore_state(pdev);
6988 		pci_save_state(pdev);
6989 
6990 		pci_enable_wake(pdev, PCI_D3hot, 0);
6991 		pci_enable_wake(pdev, PCI_D3cold, 0);
6992 
6993 		/* In case of PCI error, adapter loses its HW address
6994 		 * so we should re-assign it here.
6995 		 */
6996 		hw->hw_addr = adapter->io_addr;
6997 
6998 		igc_reset(adapter);
6999 		wr32(IGC_WUS, ~0);
7000 		result = PCI_ERS_RESULT_RECOVERED;
7001 	}
7002 
7003 	return result;
7004 }
7005 
7006 /**
7007  *  igc_io_resume - called when traffic can start to flow again.
7008  *  @pdev: Pointer to PCI device
7009  *
7010  *  This callback is called when the error recovery driver tells us that
7011  *  its OK to resume normal operation. Implementation resembles the
7012  *  second-half of the igc_resume routine.
7013  */
7014 static void igc_io_resume(struct pci_dev *pdev)
7015 {
7016 	struct net_device *netdev = pci_get_drvdata(pdev);
7017 	struct igc_adapter *adapter = netdev_priv(netdev);
7018 
7019 	rtnl_lock();
7020 	if (netif_running(netdev)) {
7021 		if (igc_open(netdev)) {
7022 			netdev_err(netdev, "igc_open failed after reset\n");
7023 			return;
7024 		}
7025 	}
7026 
7027 	netif_device_attach(netdev);
7028 
7029 	/* let the f/w know that the h/w is now under the control of the
7030 	 * driver.
7031 	 */
7032 	igc_get_hw_control(adapter);
7033 	rtnl_unlock();
7034 }
7035 
7036 static const struct pci_error_handlers igc_err_handler = {
7037 	.error_detected = igc_io_error_detected,
7038 	.slot_reset = igc_io_slot_reset,
7039 	.resume = igc_io_resume,
7040 };
7041 
7042 #ifdef CONFIG_PM
7043 static const struct dev_pm_ops igc_pm_ops = {
7044 	SET_SYSTEM_SLEEP_PM_OPS(igc_suspend, igc_resume)
7045 	SET_RUNTIME_PM_OPS(igc_runtime_suspend, igc_runtime_resume,
7046 			   igc_runtime_idle)
7047 };
7048 #endif
7049 
7050 static struct pci_driver igc_driver = {
7051 	.name     = igc_driver_name,
7052 	.id_table = igc_pci_tbl,
7053 	.probe    = igc_probe,
7054 	.remove   = igc_remove,
7055 #ifdef CONFIG_PM
7056 	.driver.pm = &igc_pm_ops,
7057 #endif
7058 	.shutdown = igc_shutdown,
7059 	.err_handler = &igc_err_handler,
7060 };
7061 
7062 /**
7063  * igc_reinit_queues - return error
7064  * @adapter: pointer to adapter structure
7065  */
7066 int igc_reinit_queues(struct igc_adapter *adapter)
7067 {
7068 	struct net_device *netdev = adapter->netdev;
7069 	int err = 0;
7070 
7071 	if (netif_running(netdev))
7072 		igc_close(netdev);
7073 
7074 	igc_reset_interrupt_capability(adapter);
7075 
7076 	if (igc_init_interrupt_scheme(adapter, true)) {
7077 		netdev_err(netdev, "Unable to allocate memory for queues\n");
7078 		return -ENOMEM;
7079 	}
7080 
7081 	if (netif_running(netdev))
7082 		err = igc_open(netdev);
7083 
7084 	return err;
7085 }
7086 
7087 /**
7088  * igc_get_hw_dev - return device
7089  * @hw: pointer to hardware structure
7090  *
7091  * used by hardware layer to print debugging information
7092  */
7093 struct net_device *igc_get_hw_dev(struct igc_hw *hw)
7094 {
7095 	struct igc_adapter *adapter = hw->back;
7096 
7097 	return adapter->netdev;
7098 }
7099 
7100 static void igc_disable_rx_ring_hw(struct igc_ring *ring)
7101 {
7102 	struct igc_hw *hw = &ring->q_vector->adapter->hw;
7103 	u8 idx = ring->reg_idx;
7104 	u32 rxdctl;
7105 
7106 	rxdctl = rd32(IGC_RXDCTL(idx));
7107 	rxdctl &= ~IGC_RXDCTL_QUEUE_ENABLE;
7108 	rxdctl |= IGC_RXDCTL_SWFLUSH;
7109 	wr32(IGC_RXDCTL(idx), rxdctl);
7110 }
7111 
7112 void igc_disable_rx_ring(struct igc_ring *ring)
7113 {
7114 	igc_disable_rx_ring_hw(ring);
7115 	igc_clean_rx_ring(ring);
7116 }
7117 
7118 void igc_enable_rx_ring(struct igc_ring *ring)
7119 {
7120 	struct igc_adapter *adapter = ring->q_vector->adapter;
7121 
7122 	igc_configure_rx_ring(adapter, ring);
7123 
7124 	if (ring->xsk_pool)
7125 		igc_alloc_rx_buffers_zc(ring, igc_desc_unused(ring));
7126 	else
7127 		igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
7128 }
7129 
7130 static void igc_disable_tx_ring_hw(struct igc_ring *ring)
7131 {
7132 	struct igc_hw *hw = &ring->q_vector->adapter->hw;
7133 	u8 idx = ring->reg_idx;
7134 	u32 txdctl;
7135 
7136 	txdctl = rd32(IGC_TXDCTL(idx));
7137 	txdctl &= ~IGC_TXDCTL_QUEUE_ENABLE;
7138 	txdctl |= IGC_TXDCTL_SWFLUSH;
7139 	wr32(IGC_TXDCTL(idx), txdctl);
7140 }
7141 
7142 void igc_disable_tx_ring(struct igc_ring *ring)
7143 {
7144 	igc_disable_tx_ring_hw(ring);
7145 	igc_clean_tx_ring(ring);
7146 }
7147 
7148 void igc_enable_tx_ring(struct igc_ring *ring)
7149 {
7150 	struct igc_adapter *adapter = ring->q_vector->adapter;
7151 
7152 	igc_configure_tx_ring(adapter, ring);
7153 }
7154 
7155 /**
7156  * igc_init_module - Driver Registration Routine
7157  *
7158  * igc_init_module is the first routine called when the driver is
7159  * loaded. All it does is register with the PCI subsystem.
7160  */
7161 static int __init igc_init_module(void)
7162 {
7163 	int ret;
7164 
7165 	pr_info("%s\n", igc_driver_string);
7166 	pr_info("%s\n", igc_copyright);
7167 
7168 	ret = pci_register_driver(&igc_driver);
7169 	return ret;
7170 }
7171 
7172 module_init(igc_init_module);
7173 
7174 /**
7175  * igc_exit_module - Driver Exit Cleanup Routine
7176  *
7177  * igc_exit_module is called just before the driver is removed
7178  * from memory.
7179  */
7180 static void __exit igc_exit_module(void)
7181 {
7182 	pci_unregister_driver(&igc_driver);
7183 }
7184 
7185 module_exit(igc_exit_module);
7186 /* igc_main.c */
7187