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