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