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