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