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