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