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