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