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