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