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