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