1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/types.h>
22#include <linux/module.h>
23#include <net/ipv6.h>
24#include <net/ip.h>
25#include <net/tcp.h>
26#include <linux/if_macvlan.h>
27#include <linux/prefetch.h>
28
29#include "fm10k.h"
30
31#define DRV_VERSION "0.21.7-k"
32#define DRV_SUMMARY "Intel(R) Ethernet Switch Host Interface Driver"
33const char fm10k_driver_version[] = DRV_VERSION;
34char fm10k_driver_name[] = "fm10k";
35static const char fm10k_driver_string[] = DRV_SUMMARY;
36static const char fm10k_copyright[] =
37 "Copyright (c) 2013 - 2016 Intel Corporation.";
38
39MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
40MODULE_DESCRIPTION(DRV_SUMMARY);
41MODULE_LICENSE("GPL");
42MODULE_VERSION(DRV_VERSION);
43
44
45struct workqueue_struct *fm10k_workqueue;
46
47
48
49
50
51
52
53static int __init fm10k_init_module(void)
54{
55 pr_info("%s - version %s\n", fm10k_driver_string, fm10k_driver_version);
56 pr_info("%s\n", fm10k_copyright);
57
58
59 fm10k_workqueue = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0,
60 fm10k_driver_name);
61
62 fm10k_dbg_init();
63
64 return fm10k_register_pci_driver();
65}
66module_init(fm10k_init_module);
67
68
69
70
71
72
73
74static void __exit fm10k_exit_module(void)
75{
76 fm10k_unregister_pci_driver();
77
78 fm10k_dbg_exit();
79
80
81 destroy_workqueue(fm10k_workqueue);
82}
83module_exit(fm10k_exit_module);
84
85static bool fm10k_alloc_mapped_page(struct fm10k_ring *rx_ring,
86 struct fm10k_rx_buffer *bi)
87{
88 struct page *page = bi->page;
89 dma_addr_t dma;
90
91
92 if (likely(page))
93 return true;
94
95
96 page = dev_alloc_page();
97 if (unlikely(!page)) {
98 rx_ring->rx_stats.alloc_failed++;
99 return false;
100 }
101
102
103 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
104
105
106
107
108 if (dma_mapping_error(rx_ring->dev, dma)) {
109 __free_page(page);
110
111 rx_ring->rx_stats.alloc_failed++;
112 return false;
113 }
114
115 bi->dma = dma;
116 bi->page = page;
117 bi->page_offset = 0;
118
119 return true;
120}
121
122
123
124
125
126
127void fm10k_alloc_rx_buffers(struct fm10k_ring *rx_ring, u16 cleaned_count)
128{
129 union fm10k_rx_desc *rx_desc;
130 struct fm10k_rx_buffer *bi;
131 u16 i = rx_ring->next_to_use;
132
133
134 if (!cleaned_count)
135 return;
136
137 rx_desc = FM10K_RX_DESC(rx_ring, i);
138 bi = &rx_ring->rx_buffer[i];
139 i -= rx_ring->count;
140
141 do {
142 if (!fm10k_alloc_mapped_page(rx_ring, bi))
143 break;
144
145
146
147
148 rx_desc->q.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
149
150 rx_desc++;
151 bi++;
152 i++;
153 if (unlikely(!i)) {
154 rx_desc = FM10K_RX_DESC(rx_ring, 0);
155 bi = rx_ring->rx_buffer;
156 i -= rx_ring->count;
157 }
158
159
160 rx_desc->d.staterr = 0;
161
162 cleaned_count--;
163 } while (cleaned_count);
164
165 i += rx_ring->count;
166
167 if (rx_ring->next_to_use != i) {
168
169 rx_ring->next_to_use = i;
170
171
172 rx_ring->next_to_alloc = i;
173
174
175
176
177
178
179 wmb();
180
181
182 writel(i, rx_ring->tail);
183 }
184}
185
186
187
188
189
190
191
192
193static void fm10k_reuse_rx_page(struct fm10k_ring *rx_ring,
194 struct fm10k_rx_buffer *old_buff)
195{
196 struct fm10k_rx_buffer *new_buff;
197 u16 nta = rx_ring->next_to_alloc;
198
199 new_buff = &rx_ring->rx_buffer[nta];
200
201
202 nta++;
203 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
204
205
206 *new_buff = *old_buff;
207
208
209 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
210 old_buff->page_offset,
211 FM10K_RX_BUFSZ,
212 DMA_FROM_DEVICE);
213}
214
215static inline bool fm10k_page_is_reserved(struct page *page)
216{
217 return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
218}
219
220static bool fm10k_can_reuse_rx_page(struct fm10k_rx_buffer *rx_buffer,
221 struct page *page,
222 unsigned int __maybe_unused truesize)
223{
224
225 if (unlikely(fm10k_page_is_reserved(page)))
226 return false;
227
228#if (PAGE_SIZE < 8192)
229
230 if (unlikely(page_count(page) != 1))
231 return false;
232
233
234 rx_buffer->page_offset ^= FM10K_RX_BUFSZ;
235#else
236
237 rx_buffer->page_offset += truesize;
238
239 if (rx_buffer->page_offset > (PAGE_SIZE - FM10K_RX_BUFSZ))
240 return false;
241#endif
242
243
244
245
246 page_ref_inc(page);
247
248 return true;
249}
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266static bool fm10k_add_rx_frag(struct fm10k_rx_buffer *rx_buffer,
267 unsigned int size,
268 union fm10k_rx_desc *rx_desc,
269 struct sk_buff *skb)
270{
271 struct page *page = rx_buffer->page;
272 unsigned char *va = page_address(page) + rx_buffer->page_offset;
273#if (PAGE_SIZE < 8192)
274 unsigned int truesize = FM10K_RX_BUFSZ;
275#else
276 unsigned int truesize = ALIGN(size, 512);
277#endif
278 unsigned int pull_len;
279
280 if (unlikely(skb_is_nonlinear(skb)))
281 goto add_tail_frag;
282
283 if (likely(size <= FM10K_RX_HDR_LEN)) {
284 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
285
286
287 if (likely(!fm10k_page_is_reserved(page)))
288 return true;
289
290
291 __free_page(page);
292 return false;
293 }
294
295
296
297
298 pull_len = eth_get_headlen(va, FM10K_RX_HDR_LEN);
299
300
301 memcpy(__skb_put(skb, pull_len), va, ALIGN(pull_len, sizeof(long)));
302
303
304 va += pull_len;
305 size -= pull_len;
306
307add_tail_frag:
308 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
309 (unsigned long)va & ~PAGE_MASK, size, truesize);
310
311 return fm10k_can_reuse_rx_page(rx_buffer, page, truesize);
312}
313
314static struct sk_buff *fm10k_fetch_rx_buffer(struct fm10k_ring *rx_ring,
315 union fm10k_rx_desc *rx_desc,
316 struct sk_buff *skb)
317{
318 unsigned int size = le16_to_cpu(rx_desc->w.length);
319 struct fm10k_rx_buffer *rx_buffer;
320 struct page *page;
321
322 rx_buffer = &rx_ring->rx_buffer[rx_ring->next_to_clean];
323 page = rx_buffer->page;
324 prefetchw(page);
325
326 if (likely(!skb)) {
327 void *page_addr = page_address(page) +
328 rx_buffer->page_offset;
329
330
331 prefetch(page_addr);
332#if L1_CACHE_BYTES < 128
333 prefetch(page_addr + L1_CACHE_BYTES);
334#endif
335
336
337 skb = napi_alloc_skb(&rx_ring->q_vector->napi,
338 FM10K_RX_HDR_LEN);
339 if (unlikely(!skb)) {
340 rx_ring->rx_stats.alloc_failed++;
341 return NULL;
342 }
343
344
345
346
347
348 prefetchw(skb->data);
349 }
350
351
352 dma_sync_single_range_for_cpu(rx_ring->dev,
353 rx_buffer->dma,
354 rx_buffer->page_offset,
355 size,
356 DMA_FROM_DEVICE);
357
358
359 if (fm10k_add_rx_frag(rx_buffer, size, rx_desc, skb)) {
360
361 fm10k_reuse_rx_page(rx_ring, rx_buffer);
362 } else {
363
364 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
365 PAGE_SIZE, DMA_FROM_DEVICE);
366 }
367
368
369 rx_buffer->page = NULL;
370
371 return skb;
372}
373
374static inline void fm10k_rx_checksum(struct fm10k_ring *ring,
375 union fm10k_rx_desc *rx_desc,
376 struct sk_buff *skb)
377{
378 skb_checksum_none_assert(skb);
379
380
381 if (!(ring->netdev->features & NETIF_F_RXCSUM))
382 return;
383
384
385 if (fm10k_test_staterr(rx_desc,
386 FM10K_RXD_STATUS_L4E |
387 FM10K_RXD_STATUS_L4E2 |
388 FM10K_RXD_STATUS_IPE |
389 FM10K_RXD_STATUS_IPE2)) {
390 ring->rx_stats.csum_err++;
391 return;
392 }
393
394
395 if (fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_L4CS2))
396 skb->encapsulation = true;
397 else if (!fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_L4CS))
398 return;
399
400 skb->ip_summed = CHECKSUM_UNNECESSARY;
401
402 ring->rx_stats.csum_good++;
403}
404
405#define FM10K_RSS_L4_TYPES_MASK \
406 (BIT(FM10K_RSSTYPE_IPV4_TCP) | \
407 BIT(FM10K_RSSTYPE_IPV4_UDP) | \
408 BIT(FM10K_RSSTYPE_IPV6_TCP) | \
409 BIT(FM10K_RSSTYPE_IPV6_UDP))
410
411static inline void fm10k_rx_hash(struct fm10k_ring *ring,
412 union fm10k_rx_desc *rx_desc,
413 struct sk_buff *skb)
414{
415 u16 rss_type;
416
417 if (!(ring->netdev->features & NETIF_F_RXHASH))
418 return;
419
420 rss_type = le16_to_cpu(rx_desc->w.pkt_info) & FM10K_RXD_RSSTYPE_MASK;
421 if (!rss_type)
422 return;
423
424 skb_set_hash(skb, le32_to_cpu(rx_desc->d.rss),
425 (BIT(rss_type) & FM10K_RSS_L4_TYPES_MASK) ?
426 PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
427}
428
429static void fm10k_type_trans(struct fm10k_ring *rx_ring,
430 union fm10k_rx_desc __maybe_unused *rx_desc,
431 struct sk_buff *skb)
432{
433 struct net_device *dev = rx_ring->netdev;
434 struct fm10k_l2_accel *l2_accel = rcu_dereference_bh(rx_ring->l2_accel);
435
436
437 if (l2_accel) {
438 u16 idx = le16_to_cpu(FM10K_CB(skb)->fi.w.dglort) - 1;
439
440 idx -= l2_accel->dglort;
441 if (idx < l2_accel->size && l2_accel->macvlan[idx])
442 dev = l2_accel->macvlan[idx];
443 else
444 l2_accel = NULL;
445 }
446
447 skb->protocol = eth_type_trans(skb, dev);
448
449 if (!l2_accel)
450 return;
451
452
453 macvlan_count_rx(netdev_priv(dev), skb->len + ETH_HLEN, 1,
454 !!(rx_desc->w.hdr_info &
455 cpu_to_le16(FM10K_RXD_HDR_INFO_XC_MASK)));
456}
457
458
459
460
461
462
463
464
465
466
467
468static unsigned int fm10k_process_skb_fields(struct fm10k_ring *rx_ring,
469 union fm10k_rx_desc *rx_desc,
470 struct sk_buff *skb)
471{
472 unsigned int len = skb->len;
473
474 fm10k_rx_hash(rx_ring, rx_desc, skb);
475
476 fm10k_rx_checksum(rx_ring, rx_desc, skb);
477
478 FM10K_CB(skb)->tstamp = rx_desc->q.timestamp;
479
480 FM10K_CB(skb)->fi.w.vlan = rx_desc->w.vlan;
481
482 skb_record_rx_queue(skb, rx_ring->queue_index);
483
484 FM10K_CB(skb)->fi.d.glort = rx_desc->d.glort;
485
486 if (rx_desc->w.vlan) {
487 u16 vid = le16_to_cpu(rx_desc->w.vlan);
488
489 if ((vid & VLAN_VID_MASK) != rx_ring->vid)
490 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
491 else if (vid & VLAN_PRIO_MASK)
492 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
493 vid & VLAN_PRIO_MASK);
494 }
495
496 fm10k_type_trans(rx_ring, rx_desc, skb);
497
498 return len;
499}
500
501
502
503
504
505
506
507
508
509
510
511static bool fm10k_is_non_eop(struct fm10k_ring *rx_ring,
512 union fm10k_rx_desc *rx_desc)
513{
514 u32 ntc = rx_ring->next_to_clean + 1;
515
516
517 ntc = (ntc < rx_ring->count) ? ntc : 0;
518 rx_ring->next_to_clean = ntc;
519
520 prefetch(FM10K_RX_DESC(rx_ring, ntc));
521
522 if (likely(fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_EOP)))
523 return false;
524
525 return true;
526}
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542static bool fm10k_cleanup_headers(struct fm10k_ring *rx_ring,
543 union fm10k_rx_desc *rx_desc,
544 struct sk_buff *skb)
545{
546 if (unlikely((fm10k_test_staterr(rx_desc,
547 FM10K_RXD_STATUS_RXE)))) {
548#define FM10K_TEST_RXD_BIT(rxd, bit) \
549 ((rxd)->w.csum_err & cpu_to_le16(bit))
550 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_SWITCH_ERROR))
551 rx_ring->rx_stats.switch_errors++;
552 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_NO_DESCRIPTOR))
553 rx_ring->rx_stats.drops++;
554 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_PP_ERROR))
555 rx_ring->rx_stats.pp_errors++;
556 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_SWITCH_READY))
557 rx_ring->rx_stats.link_errors++;
558 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_TOO_BIG))
559 rx_ring->rx_stats.length_errors++;
560 dev_kfree_skb_any(skb);
561 rx_ring->rx_stats.errors++;
562 return true;
563 }
564
565
566 if (eth_skb_pad(skb))
567 return true;
568
569 return false;
570}
571
572
573
574
575
576
577static void fm10k_receive_skb(struct fm10k_q_vector *q_vector,
578 struct sk_buff *skb)
579{
580 napi_gro_receive(&q_vector->napi, skb);
581}
582
583static int fm10k_clean_rx_irq(struct fm10k_q_vector *q_vector,
584 struct fm10k_ring *rx_ring,
585 int budget)
586{
587 struct sk_buff *skb = rx_ring->skb;
588 unsigned int total_bytes = 0, total_packets = 0;
589 u16 cleaned_count = fm10k_desc_unused(rx_ring);
590
591 while (likely(total_packets < budget)) {
592 union fm10k_rx_desc *rx_desc;
593
594
595 if (cleaned_count >= FM10K_RX_BUFFER_WRITE) {
596 fm10k_alloc_rx_buffers(rx_ring, cleaned_count);
597 cleaned_count = 0;
598 }
599
600 rx_desc = FM10K_RX_DESC(rx_ring, rx_ring->next_to_clean);
601
602 if (!rx_desc->d.staterr)
603 break;
604
605
606
607
608
609 dma_rmb();
610
611
612 skb = fm10k_fetch_rx_buffer(rx_ring, rx_desc, skb);
613
614
615 if (!skb)
616 break;
617
618 cleaned_count++;
619
620
621 if (fm10k_is_non_eop(rx_ring, rx_desc))
622 continue;
623
624
625 if (fm10k_cleanup_headers(rx_ring, rx_desc, skb)) {
626 skb = NULL;
627 continue;
628 }
629
630
631 total_bytes += fm10k_process_skb_fields(rx_ring, rx_desc, skb);
632
633 fm10k_receive_skb(q_vector, skb);
634
635
636 skb = NULL;
637
638
639 total_packets++;
640 }
641
642
643 rx_ring->skb = skb;
644
645 u64_stats_update_begin(&rx_ring->syncp);
646 rx_ring->stats.packets += total_packets;
647 rx_ring->stats.bytes += total_bytes;
648 u64_stats_update_end(&rx_ring->syncp);
649 q_vector->rx.total_packets += total_packets;
650 q_vector->rx.total_bytes += total_bytes;
651
652 return total_packets;
653}
654
655#define VXLAN_HLEN (sizeof(struct udphdr) + 8)
656static struct ethhdr *fm10k_port_is_vxlan(struct sk_buff *skb)
657{
658 struct fm10k_intfc *interface = netdev_priv(skb->dev);
659 struct fm10k_udp_port *vxlan_port;
660
661
662 vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
663 struct fm10k_udp_port, list);
664
665 if (!vxlan_port)
666 return NULL;
667 if (vxlan_port->port != udp_hdr(skb)->dest)
668 return NULL;
669
670
671 return (struct ethhdr *)(skb_transport_header(skb) + VXLAN_HLEN);
672}
673
674#define FM10K_NVGRE_RESERVED0_FLAGS htons(0x9FFF)
675#define NVGRE_TNI htons(0x2000)
676struct fm10k_nvgre_hdr {
677 __be16 flags;
678 __be16 proto;
679 __be32 tni;
680};
681
682static struct ethhdr *fm10k_gre_is_nvgre(struct sk_buff *skb)
683{
684 struct fm10k_nvgre_hdr *nvgre_hdr;
685 int hlen = ip_hdrlen(skb);
686
687
688 if (vlan_get_protocol(skb) != htons(ETH_P_IP))
689 return NULL;
690
691
692 nvgre_hdr = (struct fm10k_nvgre_hdr *)(skb_network_header(skb) + hlen);
693
694
695 if (nvgre_hdr->flags & FM10K_NVGRE_RESERVED0_FLAGS)
696 return NULL;
697
698
699 if (nvgre_hdr->flags & NVGRE_TNI)
700 return (struct ethhdr *)(nvgre_hdr + 1);
701
702 return (struct ethhdr *)(&nvgre_hdr->tni);
703}
704
705__be16 fm10k_tx_encap_offload(struct sk_buff *skb)
706{
707 u8 l4_hdr = 0, inner_l4_hdr = 0, inner_l4_hlen;
708 struct ethhdr *eth_hdr;
709
710 if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
711 skb->inner_protocol != htons(ETH_P_TEB))
712 return 0;
713
714 switch (vlan_get_protocol(skb)) {
715 case htons(ETH_P_IP):
716 l4_hdr = ip_hdr(skb)->protocol;
717 break;
718 case htons(ETH_P_IPV6):
719 l4_hdr = ipv6_hdr(skb)->nexthdr;
720 break;
721 default:
722 return 0;
723 }
724
725 switch (l4_hdr) {
726 case IPPROTO_UDP:
727 eth_hdr = fm10k_port_is_vxlan(skb);
728 break;
729 case IPPROTO_GRE:
730 eth_hdr = fm10k_gre_is_nvgre(skb);
731 break;
732 default:
733 return 0;
734 }
735
736 if (!eth_hdr)
737 return 0;
738
739 switch (eth_hdr->h_proto) {
740 case htons(ETH_P_IP):
741 inner_l4_hdr = inner_ip_hdr(skb)->protocol;
742 break;
743 case htons(ETH_P_IPV6):
744 inner_l4_hdr = inner_ipv6_hdr(skb)->nexthdr;
745 break;
746 default:
747 return 0;
748 }
749
750 switch (inner_l4_hdr) {
751 case IPPROTO_TCP:
752 inner_l4_hlen = inner_tcp_hdrlen(skb);
753 break;
754 case IPPROTO_UDP:
755 inner_l4_hlen = 8;
756 break;
757 default:
758 return 0;
759 }
760
761
762
763
764 if (skb_inner_transport_header(skb) + inner_l4_hlen -
765 skb_mac_header(skb) > FM10K_TUNNEL_HEADER_LENGTH)
766 return 0;
767
768 return eth_hdr->h_proto;
769}
770
771static int fm10k_tso(struct fm10k_ring *tx_ring,
772 struct fm10k_tx_buffer *first)
773{
774 struct sk_buff *skb = first->skb;
775 struct fm10k_tx_desc *tx_desc;
776 unsigned char *th;
777 u8 hdrlen;
778
779 if (skb->ip_summed != CHECKSUM_PARTIAL)
780 return 0;
781
782 if (!skb_is_gso(skb))
783 return 0;
784
785
786 if (skb->encapsulation) {
787 if (!fm10k_tx_encap_offload(skb))
788 goto err_vxlan;
789 th = skb_inner_transport_header(skb);
790 } else {
791 th = skb_transport_header(skb);
792 }
793
794
795 hdrlen = (th - skb->data) + (((struct tcphdr *)th)->doff << 2);
796
797 first->tx_flags |= FM10K_TX_FLAGS_CSUM;
798
799
800 first->gso_segs = skb_shinfo(skb)->gso_segs;
801 first->bytecount += (first->gso_segs - 1) * hdrlen;
802
803
804 tx_desc = FM10K_TX_DESC(tx_ring, tx_ring->next_to_use);
805 tx_desc->hdrlen = hdrlen;
806 tx_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
807
808 return 1;
809err_vxlan:
810 tx_ring->netdev->features &= ~NETIF_F_GSO_UDP_TUNNEL;
811 if (!net_ratelimit())
812 netdev_err(tx_ring->netdev,
813 "TSO requested for unsupported tunnel, disabling offload\n");
814 return -1;
815}
816
817static void fm10k_tx_csum(struct fm10k_ring *tx_ring,
818 struct fm10k_tx_buffer *first)
819{
820 struct sk_buff *skb = first->skb;
821 struct fm10k_tx_desc *tx_desc;
822 union {
823 struct iphdr *ipv4;
824 struct ipv6hdr *ipv6;
825 u8 *raw;
826 } network_hdr;
827 u8 *transport_hdr;
828 __be16 frag_off;
829 __be16 protocol;
830 u8 l4_hdr = 0;
831
832 if (skb->ip_summed != CHECKSUM_PARTIAL)
833 goto no_csum;
834
835 if (skb->encapsulation) {
836 protocol = fm10k_tx_encap_offload(skb);
837 if (!protocol) {
838 if (skb_checksum_help(skb)) {
839 dev_warn(tx_ring->dev,
840 "failed to offload encap csum!\n");
841 tx_ring->tx_stats.csum_err++;
842 }
843 goto no_csum;
844 }
845 network_hdr.raw = skb_inner_network_header(skb);
846 transport_hdr = skb_inner_transport_header(skb);
847 } else {
848 protocol = vlan_get_protocol(skb);
849 network_hdr.raw = skb_network_header(skb);
850 transport_hdr = skb_transport_header(skb);
851 }
852
853 switch (protocol) {
854 case htons(ETH_P_IP):
855 l4_hdr = network_hdr.ipv4->protocol;
856 break;
857 case htons(ETH_P_IPV6):
858 l4_hdr = network_hdr.ipv6->nexthdr;
859 if (likely((transport_hdr - network_hdr.raw) ==
860 sizeof(struct ipv6hdr)))
861 break;
862 ipv6_skip_exthdr(skb, network_hdr.raw - skb->data +
863 sizeof(struct ipv6hdr),
864 &l4_hdr, &frag_off);
865 if (unlikely(frag_off))
866 l4_hdr = NEXTHDR_FRAGMENT;
867 break;
868 default:
869 break;
870 }
871
872 switch (l4_hdr) {
873 case IPPROTO_TCP:
874 case IPPROTO_UDP:
875 break;
876 case IPPROTO_GRE:
877 if (skb->encapsulation)
878 break;
879 default:
880 if (unlikely(net_ratelimit())) {
881 dev_warn(tx_ring->dev,
882 "partial checksum, version=%d l4 proto=%x\n",
883 protocol, l4_hdr);
884 }
885 skb_checksum_help(skb);
886 tx_ring->tx_stats.csum_err++;
887 goto no_csum;
888 }
889
890
891 first->tx_flags |= FM10K_TX_FLAGS_CSUM;
892 tx_ring->tx_stats.csum_good++;
893
894no_csum:
895
896 tx_desc = FM10K_TX_DESC(tx_ring, tx_ring->next_to_use);
897 tx_desc->hdrlen = 0;
898 tx_desc->mss = 0;
899}
900
901#define FM10K_SET_FLAG(_input, _flag, _result) \
902 ((_flag <= _result) ? \
903 ((u32)(_input & _flag) * (_result / _flag)) : \
904 ((u32)(_input & _flag) / (_flag / _result)))
905
906static u8 fm10k_tx_desc_flags(struct sk_buff *skb, u32 tx_flags)
907{
908
909 u32 desc_flags = 0;
910
911
912 desc_flags |= FM10K_SET_FLAG(tx_flags, FM10K_TX_FLAGS_CSUM,
913 FM10K_TXD_FLAG_CSUM);
914
915 return desc_flags;
916}
917
918static bool fm10k_tx_desc_push(struct fm10k_ring *tx_ring,
919 struct fm10k_tx_desc *tx_desc, u16 i,
920 dma_addr_t dma, unsigned int size, u8 desc_flags)
921{
922
923 if ((++i & (FM10K_TXD_WB_FIFO_SIZE - 1)) == 0)
924 desc_flags |= FM10K_TXD_FLAG_RS | FM10K_TXD_FLAG_INT;
925
926
927 tx_desc->buffer_addr = cpu_to_le64(dma);
928 tx_desc->flags = desc_flags;
929 tx_desc->buflen = cpu_to_le16(size);
930
931
932 return i == tx_ring->count;
933}
934
935static int __fm10k_maybe_stop_tx(struct fm10k_ring *tx_ring, u16 size)
936{
937 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
938
939
940 smp_mb();
941
942
943 if (likely(fm10k_desc_unused(tx_ring) < size))
944 return -EBUSY;
945
946
947 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
948 ++tx_ring->tx_stats.restart_queue;
949 return 0;
950}
951
952static inline int fm10k_maybe_stop_tx(struct fm10k_ring *tx_ring, u16 size)
953{
954 if (likely(fm10k_desc_unused(tx_ring) >= size))
955 return 0;
956 return __fm10k_maybe_stop_tx(tx_ring, size);
957}
958
959static void fm10k_tx_map(struct fm10k_ring *tx_ring,
960 struct fm10k_tx_buffer *first)
961{
962 struct sk_buff *skb = first->skb;
963 struct fm10k_tx_buffer *tx_buffer;
964 struct fm10k_tx_desc *tx_desc;
965 struct skb_frag_struct *frag;
966 unsigned char *data;
967 dma_addr_t dma;
968 unsigned int data_len, size;
969 u32 tx_flags = first->tx_flags;
970 u16 i = tx_ring->next_to_use;
971 u8 flags = fm10k_tx_desc_flags(skb, tx_flags);
972
973 tx_desc = FM10K_TX_DESC(tx_ring, i);
974
975
976 if (skb_vlan_tag_present(skb))
977 tx_desc->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
978 else
979 tx_desc->vlan = 0;
980
981 size = skb_headlen(skb);
982 data = skb->data;
983
984 dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
985
986 data_len = skb->data_len;
987 tx_buffer = first;
988
989 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
990 if (dma_mapping_error(tx_ring->dev, dma))
991 goto dma_error;
992
993
994 dma_unmap_len_set(tx_buffer, len, size);
995 dma_unmap_addr_set(tx_buffer, dma, dma);
996
997 while (unlikely(size > FM10K_MAX_DATA_PER_TXD)) {
998 if (fm10k_tx_desc_push(tx_ring, tx_desc++, i++, dma,
999 FM10K_MAX_DATA_PER_TXD, flags)) {
1000 tx_desc = FM10K_TX_DESC(tx_ring, 0);
1001 i = 0;
1002 }
1003
1004 dma += FM10K_MAX_DATA_PER_TXD;
1005 size -= FM10K_MAX_DATA_PER_TXD;
1006 }
1007
1008 if (likely(!data_len))
1009 break;
1010
1011 if (fm10k_tx_desc_push(tx_ring, tx_desc++, i++,
1012 dma, size, flags)) {
1013 tx_desc = FM10K_TX_DESC(tx_ring, 0);
1014 i = 0;
1015 }
1016
1017 size = skb_frag_size(frag);
1018 data_len -= size;
1019
1020 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1021 DMA_TO_DEVICE);
1022
1023 tx_buffer = &tx_ring->tx_buffer[i];
1024 }
1025
1026
1027 flags |= FM10K_TXD_FLAG_LAST;
1028
1029 if (fm10k_tx_desc_push(tx_ring, tx_desc, i++, dma, size, flags))
1030 i = 0;
1031
1032
1033 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
1034
1035
1036 skb_tx_timestamp(first->skb);
1037
1038
1039
1040
1041
1042
1043
1044
1045 wmb();
1046
1047
1048 first->next_to_watch = tx_desc;
1049
1050 tx_ring->next_to_use = i;
1051
1052
1053 fm10k_maybe_stop_tx(tx_ring, DESC_NEEDED);
1054
1055
1056 if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
1057 writel(i, tx_ring->tail);
1058
1059
1060
1061
1062 mmiowb();
1063 }
1064
1065 return;
1066dma_error:
1067 dev_err(tx_ring->dev, "TX DMA map failed\n");
1068
1069
1070 for (;;) {
1071 tx_buffer = &tx_ring->tx_buffer[i];
1072 fm10k_unmap_and_free_tx_resource(tx_ring, tx_buffer);
1073 if (tx_buffer == first)
1074 break;
1075 if (i == 0)
1076 i = tx_ring->count;
1077 i--;
1078 }
1079
1080 tx_ring->next_to_use = i;
1081}
1082
1083netdev_tx_t fm10k_xmit_frame_ring(struct sk_buff *skb,
1084 struct fm10k_ring *tx_ring)
1085{
1086 u16 count = TXD_USE_COUNT(skb_headlen(skb));
1087 struct fm10k_tx_buffer *first;
1088 unsigned short f;
1089 u32 tx_flags = 0;
1090 int tso;
1091
1092
1093
1094
1095
1096
1097 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1098 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
1099
1100 if (fm10k_maybe_stop_tx(tx_ring, count + 3)) {
1101 tx_ring->tx_stats.tx_busy++;
1102 return NETDEV_TX_BUSY;
1103 }
1104
1105
1106 first = &tx_ring->tx_buffer[tx_ring->next_to_use];
1107 first->skb = skb;
1108 first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN);
1109 first->gso_segs = 1;
1110
1111
1112 first->tx_flags = tx_flags;
1113
1114 tso = fm10k_tso(tx_ring, first);
1115 if (tso < 0)
1116 goto out_drop;
1117 else if (!tso)
1118 fm10k_tx_csum(tx_ring, first);
1119
1120 fm10k_tx_map(tx_ring, first);
1121
1122 return NETDEV_TX_OK;
1123
1124out_drop:
1125 dev_kfree_skb_any(first->skb);
1126 first->skb = NULL;
1127
1128 return NETDEV_TX_OK;
1129}
1130
1131static u64 fm10k_get_tx_completed(struct fm10k_ring *ring)
1132{
1133 return ring->stats.packets;
1134}
1135
1136
1137
1138
1139
1140
1141u64 fm10k_get_tx_pending(struct fm10k_ring *ring, bool in_sw)
1142{
1143 struct fm10k_intfc *interface = ring->q_vector->interface;
1144 struct fm10k_hw *hw = &interface->hw;
1145 u32 head, tail;
1146
1147 if (likely(in_sw)) {
1148 head = ring->next_to_clean;
1149 tail = ring->next_to_use;
1150 } else {
1151 head = fm10k_read_reg(hw, FM10K_TDH(ring->reg_idx));
1152 tail = fm10k_read_reg(hw, FM10K_TDT(ring->reg_idx));
1153 }
1154
1155 return ((head <= tail) ? tail : tail + ring->count) - head;
1156}
1157
1158bool fm10k_check_tx_hang(struct fm10k_ring *tx_ring)
1159{
1160 u32 tx_done = fm10k_get_tx_completed(tx_ring);
1161 u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
1162 u32 tx_pending = fm10k_get_tx_pending(tx_ring, true);
1163
1164 clear_check_for_tx_hang(tx_ring);
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174 if (!tx_pending || (tx_done_old != tx_done)) {
1175
1176 tx_ring->tx_stats.tx_done_old = tx_done;
1177
1178 clear_bit(__FM10K_HANG_CHECK_ARMED, &tx_ring->state);
1179
1180 return false;
1181 }
1182
1183
1184 return test_and_set_bit(__FM10K_HANG_CHECK_ARMED, &tx_ring->state);
1185}
1186
1187
1188
1189
1190
1191void fm10k_tx_timeout_reset(struct fm10k_intfc *interface)
1192{
1193
1194 if (!test_bit(__FM10K_DOWN, &interface->state)) {
1195 interface->tx_timeout_count++;
1196 interface->flags |= FM10K_FLAG_RESET_REQUESTED;
1197 fm10k_service_event_schedule(interface);
1198 }
1199}
1200
1201
1202
1203
1204
1205
1206
1207static bool fm10k_clean_tx_irq(struct fm10k_q_vector *q_vector,
1208 struct fm10k_ring *tx_ring, int napi_budget)
1209{
1210 struct fm10k_intfc *interface = q_vector->interface;
1211 struct fm10k_tx_buffer *tx_buffer;
1212 struct fm10k_tx_desc *tx_desc;
1213 unsigned int total_bytes = 0, total_packets = 0;
1214 unsigned int budget = q_vector->tx.work_limit;
1215 unsigned int i = tx_ring->next_to_clean;
1216
1217 if (test_bit(__FM10K_DOWN, &interface->state))
1218 return true;
1219
1220 tx_buffer = &tx_ring->tx_buffer[i];
1221 tx_desc = FM10K_TX_DESC(tx_ring, i);
1222 i -= tx_ring->count;
1223
1224 do {
1225 struct fm10k_tx_desc *eop_desc = tx_buffer->next_to_watch;
1226
1227
1228 if (!eop_desc)
1229 break;
1230
1231
1232 read_barrier_depends();
1233
1234
1235 if (!(eop_desc->flags & FM10K_TXD_FLAG_DONE))
1236 break;
1237
1238
1239 tx_buffer->next_to_watch = NULL;
1240
1241
1242 total_bytes += tx_buffer->bytecount;
1243 total_packets += tx_buffer->gso_segs;
1244
1245
1246 napi_consume_skb(tx_buffer->skb, napi_budget);
1247
1248
1249 dma_unmap_single(tx_ring->dev,
1250 dma_unmap_addr(tx_buffer, dma),
1251 dma_unmap_len(tx_buffer, len),
1252 DMA_TO_DEVICE);
1253
1254
1255 tx_buffer->skb = NULL;
1256 dma_unmap_len_set(tx_buffer, len, 0);
1257
1258
1259 while (tx_desc != eop_desc) {
1260 tx_buffer++;
1261 tx_desc++;
1262 i++;
1263 if (unlikely(!i)) {
1264 i -= tx_ring->count;
1265 tx_buffer = tx_ring->tx_buffer;
1266 tx_desc = FM10K_TX_DESC(tx_ring, 0);
1267 }
1268
1269
1270 if (dma_unmap_len(tx_buffer, len)) {
1271 dma_unmap_page(tx_ring->dev,
1272 dma_unmap_addr(tx_buffer, dma),
1273 dma_unmap_len(tx_buffer, len),
1274 DMA_TO_DEVICE);
1275 dma_unmap_len_set(tx_buffer, len, 0);
1276 }
1277 }
1278
1279
1280 tx_buffer++;
1281 tx_desc++;
1282 i++;
1283 if (unlikely(!i)) {
1284 i -= tx_ring->count;
1285 tx_buffer = tx_ring->tx_buffer;
1286 tx_desc = FM10K_TX_DESC(tx_ring, 0);
1287 }
1288
1289
1290 prefetch(tx_desc);
1291
1292
1293 budget--;
1294 } while (likely(budget));
1295
1296 i += tx_ring->count;
1297 tx_ring->next_to_clean = i;
1298 u64_stats_update_begin(&tx_ring->syncp);
1299 tx_ring->stats.bytes += total_bytes;
1300 tx_ring->stats.packets += total_packets;
1301 u64_stats_update_end(&tx_ring->syncp);
1302 q_vector->tx.total_bytes += total_bytes;
1303 q_vector->tx.total_packets += total_packets;
1304
1305 if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring)) {
1306
1307 struct fm10k_hw *hw = &interface->hw;
1308
1309 netif_err(interface, drv, tx_ring->netdev,
1310 "Detected Tx Unit Hang\n"
1311 " Tx Queue <%d>\n"
1312 " TDH, TDT <%x>, <%x>\n"
1313 " next_to_use <%x>\n"
1314 " next_to_clean <%x>\n",
1315 tx_ring->queue_index,
1316 fm10k_read_reg(hw, FM10K_TDH(tx_ring->reg_idx)),
1317 fm10k_read_reg(hw, FM10K_TDT(tx_ring->reg_idx)),
1318 tx_ring->next_to_use, i);
1319
1320 netif_stop_subqueue(tx_ring->netdev,
1321 tx_ring->queue_index);
1322
1323 netif_info(interface, probe, tx_ring->netdev,
1324 "tx hang %d detected on queue %d, resetting interface\n",
1325 interface->tx_timeout_count + 1,
1326 tx_ring->queue_index);
1327
1328 fm10k_tx_timeout_reset(interface);
1329
1330
1331 return true;
1332 }
1333
1334
1335 netdev_tx_completed_queue(txring_txq(tx_ring),
1336 total_packets, total_bytes);
1337
1338#define TX_WAKE_THRESHOLD min_t(u16, FM10K_MIN_TXD - 1, DESC_NEEDED * 2)
1339 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
1340 (fm10k_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
1341
1342
1343
1344 smp_mb();
1345 if (__netif_subqueue_stopped(tx_ring->netdev,
1346 tx_ring->queue_index) &&
1347 !test_bit(__FM10K_DOWN, &interface->state)) {
1348 netif_wake_subqueue(tx_ring->netdev,
1349 tx_ring->queue_index);
1350 ++tx_ring->tx_stats.restart_queue;
1351 }
1352 }
1353
1354 return !!budget;
1355}
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367static void fm10k_update_itr(struct fm10k_ring_container *ring_container)
1368{
1369 unsigned int avg_wire_size, packets, itr_round;
1370
1371
1372 if (!ITR_IS_ADAPTIVE(ring_container->itr))
1373 goto clear_counts;
1374
1375 packets = ring_container->total_packets;
1376 if (!packets)
1377 goto clear_counts;
1378
1379 avg_wire_size = ring_container->total_bytes / packets;
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396 if (avg_wire_size <= 360) {
1397
1398 avg_wire_size *= 8;
1399 avg_wire_size += 376;
1400 } else if (avg_wire_size <= 1152) {
1401
1402 avg_wire_size *= 3;
1403 avg_wire_size += 2176;
1404 } else if (avg_wire_size <= 1920) {
1405
1406 avg_wire_size += 4480;
1407 } else {
1408
1409 avg_wire_size = 6656;
1410 }
1411
1412
1413
1414
1415
1416 itr_round = READ_ONCE(ring_container->itr_scale) + 8;
1417 avg_wire_size += BIT(itr_round) - 1;
1418 avg_wire_size >>= itr_round;
1419
1420
1421 ring_container->itr = avg_wire_size | FM10K_ITR_ADAPTIVE;
1422
1423clear_counts:
1424 ring_container->total_bytes = 0;
1425 ring_container->total_packets = 0;
1426}
1427
1428static void fm10k_qv_enable(struct fm10k_q_vector *q_vector)
1429{
1430
1431 u32 itr = FM10K_ITR_ENABLE;
1432
1433
1434 fm10k_update_itr(&q_vector->tx);
1435
1436
1437 fm10k_update_itr(&q_vector->rx);
1438
1439
1440 itr |= (q_vector->tx.itr & FM10K_ITR_MAX);
1441
1442
1443 itr |= (q_vector->rx.itr & FM10K_ITR_MAX) << FM10K_ITR_INTERVAL1_SHIFT;
1444
1445
1446 writel(itr, q_vector->itr);
1447}
1448
1449static int fm10k_poll(struct napi_struct *napi, int budget)
1450{
1451 struct fm10k_q_vector *q_vector =
1452 container_of(napi, struct fm10k_q_vector, napi);
1453 struct fm10k_ring *ring;
1454 int per_ring_budget, work_done = 0;
1455 bool clean_complete = true;
1456
1457 fm10k_for_each_ring(ring, q_vector->tx) {
1458 if (!fm10k_clean_tx_irq(q_vector, ring, budget))
1459 clean_complete = false;
1460 }
1461
1462
1463 if (budget <= 0)
1464 return budget;
1465
1466
1467
1468
1469 if (q_vector->rx.count > 1)
1470 per_ring_budget = max(budget / q_vector->rx.count, 1);
1471 else
1472 per_ring_budget = budget;
1473
1474 fm10k_for_each_ring(ring, q_vector->rx) {
1475 int work = fm10k_clean_rx_irq(q_vector, ring, per_ring_budget);
1476
1477 work_done += work;
1478 if (work >= per_ring_budget)
1479 clean_complete = false;
1480 }
1481
1482
1483 if (!clean_complete)
1484 return budget;
1485
1486
1487 napi_complete_done(napi, work_done);
1488
1489
1490 fm10k_qv_enable(q_vector);
1491
1492 return min(work_done, budget - 1);
1493}
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506static bool fm10k_set_qos_queues(struct fm10k_intfc *interface)
1507{
1508 struct net_device *dev = interface->netdev;
1509 struct fm10k_ring_feature *f;
1510 int rss_i, i;
1511 int pcs;
1512
1513
1514 pcs = netdev_get_num_tc(dev);
1515
1516 if (pcs <= 1)
1517 return false;
1518
1519
1520 f = &interface->ring_feature[RING_F_QOS];
1521 f->indices = pcs;
1522 f->mask = BIT(fls(pcs - 1)) - 1;
1523
1524
1525 rss_i = interface->hw.mac.max_queues / pcs;
1526 rss_i = BIT(fls(rss_i) - 1);
1527
1528
1529 f = &interface->ring_feature[RING_F_RSS];
1530 rss_i = min_t(u16, rss_i, f->limit);
1531 f->indices = rss_i;
1532 f->mask = BIT(fls(rss_i - 1)) - 1;
1533
1534
1535 for (i = 0; i < pcs; i++)
1536 netdev_set_tc_queue(dev, i, rss_i, rss_i * i);
1537
1538 interface->num_rx_queues = rss_i * pcs;
1539 interface->num_tx_queues = rss_i * pcs;
1540
1541 return true;
1542}
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552static bool fm10k_set_rss_queues(struct fm10k_intfc *interface)
1553{
1554 struct fm10k_ring_feature *f;
1555 u16 rss_i;
1556
1557 f = &interface->ring_feature[RING_F_RSS];
1558 rss_i = min_t(u16, interface->hw.mac.max_queues, f->limit);
1559
1560
1561 f->indices = rss_i;
1562 f->mask = BIT(fls(rss_i - 1)) - 1;
1563
1564 interface->num_rx_queues = rss_i;
1565 interface->num_tx_queues = rss_i;
1566
1567 return true;
1568}
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581static void fm10k_set_num_queues(struct fm10k_intfc *interface)
1582{
1583
1584 if (fm10k_set_qos_queues(interface))
1585 return;
1586
1587
1588 fm10k_set_rss_queues(interface);
1589}
1590
1591
1592
1593
1594
1595
1596
1597
1598static void fm10k_reset_num_queues(struct fm10k_intfc *interface)
1599{
1600 interface->num_tx_queues = 0;
1601 interface->num_rx_queues = 0;
1602 interface->num_q_vectors = 0;
1603}
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617static int fm10k_alloc_q_vector(struct fm10k_intfc *interface,
1618 unsigned int v_count, unsigned int v_idx,
1619 unsigned int txr_count, unsigned int txr_idx,
1620 unsigned int rxr_count, unsigned int rxr_idx)
1621{
1622 struct fm10k_q_vector *q_vector;
1623 struct fm10k_ring *ring;
1624 int ring_count, size;
1625
1626 ring_count = txr_count + rxr_count;
1627 size = sizeof(struct fm10k_q_vector) +
1628 (sizeof(struct fm10k_ring) * ring_count);
1629
1630
1631 q_vector = kzalloc(size, GFP_KERNEL);
1632 if (!q_vector)
1633 return -ENOMEM;
1634
1635
1636 netif_napi_add(interface->netdev, &q_vector->napi,
1637 fm10k_poll, NAPI_POLL_WEIGHT);
1638
1639
1640 interface->q_vector[v_idx] = q_vector;
1641 q_vector->interface = interface;
1642 q_vector->v_idx = v_idx;
1643
1644
1645 ring = q_vector->ring;
1646
1647
1648 q_vector->tx.ring = ring;
1649 q_vector->tx.work_limit = FM10K_DEFAULT_TX_WORK;
1650 q_vector->tx.itr = interface->tx_itr;
1651 q_vector->tx.itr_scale = interface->hw.mac.itr_scale;
1652 q_vector->tx.count = txr_count;
1653
1654 while (txr_count) {
1655
1656 ring->dev = &interface->pdev->dev;
1657 ring->netdev = interface->netdev;
1658
1659
1660 ring->q_vector = q_vector;
1661
1662
1663 ring->count = interface->tx_ring_count;
1664 ring->queue_index = txr_idx;
1665
1666
1667 interface->tx_ring[txr_idx] = ring;
1668
1669
1670 txr_count--;
1671 txr_idx += v_count;
1672
1673
1674 ring++;
1675 }
1676
1677
1678 q_vector->rx.ring = ring;
1679 q_vector->rx.itr = interface->rx_itr;
1680 q_vector->rx.itr_scale = interface->hw.mac.itr_scale;
1681 q_vector->rx.count = rxr_count;
1682
1683 while (rxr_count) {
1684
1685 ring->dev = &interface->pdev->dev;
1686 ring->netdev = interface->netdev;
1687 rcu_assign_pointer(ring->l2_accel, interface->l2_accel);
1688
1689
1690 ring->q_vector = q_vector;
1691
1692
1693 ring->count = interface->rx_ring_count;
1694 ring->queue_index = rxr_idx;
1695
1696
1697 interface->rx_ring[rxr_idx] = ring;
1698
1699
1700 rxr_count--;
1701 rxr_idx += v_count;
1702
1703
1704 ring++;
1705 }
1706
1707 fm10k_dbg_q_vector_init(q_vector);
1708
1709 return 0;
1710}
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721static void fm10k_free_q_vector(struct fm10k_intfc *interface, int v_idx)
1722{
1723 struct fm10k_q_vector *q_vector = interface->q_vector[v_idx];
1724 struct fm10k_ring *ring;
1725
1726 fm10k_dbg_q_vector_exit(q_vector);
1727
1728 fm10k_for_each_ring(ring, q_vector->tx)
1729 interface->tx_ring[ring->queue_index] = NULL;
1730
1731 fm10k_for_each_ring(ring, q_vector->rx)
1732 interface->rx_ring[ring->queue_index] = NULL;
1733
1734 interface->q_vector[v_idx] = NULL;
1735 netif_napi_del(&q_vector->napi);
1736 kfree_rcu(q_vector, rcu);
1737}
1738
1739
1740
1741
1742
1743
1744
1745
1746static int fm10k_alloc_q_vectors(struct fm10k_intfc *interface)
1747{
1748 unsigned int q_vectors = interface->num_q_vectors;
1749 unsigned int rxr_remaining = interface->num_rx_queues;
1750 unsigned int txr_remaining = interface->num_tx_queues;
1751 unsigned int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1752 int err;
1753
1754 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1755 for (; rxr_remaining; v_idx++) {
1756 err = fm10k_alloc_q_vector(interface, q_vectors, v_idx,
1757 0, 0, 1, rxr_idx);
1758 if (err)
1759 goto err_out;
1760
1761
1762 rxr_remaining--;
1763 rxr_idx++;
1764 }
1765 }
1766
1767 for (; v_idx < q_vectors; v_idx++) {
1768 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1769 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1770
1771 err = fm10k_alloc_q_vector(interface, q_vectors, v_idx,
1772 tqpv, txr_idx,
1773 rqpv, rxr_idx);
1774
1775 if (err)
1776 goto err_out;
1777
1778
1779 rxr_remaining -= rqpv;
1780 txr_remaining -= tqpv;
1781 rxr_idx++;
1782 txr_idx++;
1783 }
1784
1785 return 0;
1786
1787err_out:
1788 fm10k_reset_num_queues(interface);
1789
1790 while (v_idx--)
1791 fm10k_free_q_vector(interface, v_idx);
1792
1793 return -ENOMEM;
1794}
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804static void fm10k_free_q_vectors(struct fm10k_intfc *interface)
1805{
1806 int v_idx = interface->num_q_vectors;
1807
1808 fm10k_reset_num_queues(interface);
1809
1810 while (v_idx--)
1811 fm10k_free_q_vector(interface, v_idx);
1812}
1813
1814
1815
1816
1817
1818
1819
1820static void fm10k_reset_msix_capability(struct fm10k_intfc *interface)
1821{
1822 pci_disable_msix(interface->pdev);
1823 kfree(interface->msix_entries);
1824 interface->msix_entries = NULL;
1825}
1826
1827
1828
1829
1830
1831
1832
1833
1834static int fm10k_init_msix_capability(struct fm10k_intfc *interface)
1835{
1836 struct fm10k_hw *hw = &interface->hw;
1837 int v_budget, vector;
1838
1839
1840
1841
1842
1843
1844
1845 v_budget = max(interface->num_rx_queues, interface->num_tx_queues);
1846 v_budget = min_t(u16, v_budget, num_online_cpus());
1847
1848
1849 v_budget += NON_Q_VECTORS(hw);
1850
1851
1852
1853
1854
1855
1856
1857 v_budget = min_t(int, v_budget, hw->mac.max_msix_vectors);
1858
1859
1860 interface->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
1861 GFP_KERNEL);
1862 if (!interface->msix_entries)
1863 return -ENOMEM;
1864
1865
1866 for (vector = 0; vector < v_budget; vector++)
1867 interface->msix_entries[vector].entry = vector;
1868
1869
1870 v_budget = pci_enable_msix_range(interface->pdev,
1871 interface->msix_entries,
1872 MIN_MSIX_COUNT(hw),
1873 v_budget);
1874 if (v_budget < 0) {
1875 kfree(interface->msix_entries);
1876 interface->msix_entries = NULL;
1877 return v_budget;
1878 }
1879
1880
1881 interface->num_q_vectors = v_budget - NON_Q_VECTORS(hw);
1882
1883 return 0;
1884}
1885
1886
1887
1888
1889
1890
1891
1892static bool fm10k_cache_ring_qos(struct fm10k_intfc *interface)
1893{
1894 struct net_device *dev = interface->netdev;
1895 int pc, offset, rss_i, i, q_idx;
1896 u16 pc_stride = interface->ring_feature[RING_F_QOS].mask + 1;
1897 u8 num_pcs = netdev_get_num_tc(dev);
1898
1899 if (num_pcs <= 1)
1900 return false;
1901
1902 rss_i = interface->ring_feature[RING_F_RSS].indices;
1903
1904 for (pc = 0, offset = 0; pc < num_pcs; pc++, offset += rss_i) {
1905 q_idx = pc;
1906 for (i = 0; i < rss_i; i++) {
1907 interface->tx_ring[offset + i]->reg_idx = q_idx;
1908 interface->tx_ring[offset + i]->qos_pc = pc;
1909 interface->rx_ring[offset + i]->reg_idx = q_idx;
1910 interface->rx_ring[offset + i]->qos_pc = pc;
1911 q_idx += pc_stride;
1912 }
1913 }
1914
1915 return true;
1916}
1917
1918
1919
1920
1921
1922
1923
1924static void fm10k_cache_ring_rss(struct fm10k_intfc *interface)
1925{
1926 int i;
1927
1928 for (i = 0; i < interface->num_rx_queues; i++)
1929 interface->rx_ring[i]->reg_idx = i;
1930
1931 for (i = 0; i < interface->num_tx_queues; i++)
1932 interface->tx_ring[i]->reg_idx = i;
1933}
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943static void fm10k_assign_rings(struct fm10k_intfc *interface)
1944{
1945 if (fm10k_cache_ring_qos(interface))
1946 return;
1947
1948 fm10k_cache_ring_rss(interface);
1949}
1950
1951static void fm10k_init_reta(struct fm10k_intfc *interface)
1952{
1953 u16 i, rss_i = interface->ring_feature[RING_F_RSS].indices;
1954 u32 reta;
1955
1956
1957
1958
1959 if (netif_is_rxfh_configured(interface->netdev)) {
1960 for (i = FM10K_RETA_SIZE; i--;) {
1961 reta = interface->reta[i];
1962 if ((((reta << 24) >> 24) < rss_i) &&
1963 (((reta << 16) >> 24) < rss_i) &&
1964 (((reta << 8) >> 24) < rss_i) &&
1965 (((reta) >> 24) < rss_i))
1966 continue;
1967
1968
1969 dev_err(&interface->pdev->dev,
1970 "RSS indirection table assigned flows out of queue bounds. Reconfiguring.\n");
1971 goto repopulate_reta;
1972 }
1973
1974
1975 return;
1976 }
1977
1978repopulate_reta:
1979 fm10k_write_reta(interface, NULL);
1980}
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990int fm10k_init_queueing_scheme(struct fm10k_intfc *interface)
1991{
1992 int err;
1993
1994
1995 fm10k_set_num_queues(interface);
1996
1997
1998 err = fm10k_init_msix_capability(interface);
1999 if (err) {
2000 dev_err(&interface->pdev->dev,
2001 "Unable to initialize MSI-X capability\n");
2002 goto err_init_msix;
2003 }
2004
2005
2006 err = fm10k_alloc_q_vectors(interface);
2007 if (err) {
2008 dev_err(&interface->pdev->dev,
2009 "Unable to allocate queue vectors\n");
2010 goto err_alloc_q_vectors;
2011 }
2012
2013
2014 fm10k_assign_rings(interface);
2015
2016
2017 fm10k_init_reta(interface);
2018
2019 return 0;
2020
2021err_alloc_q_vectors:
2022 fm10k_reset_msix_capability(interface);
2023err_init_msix:
2024 fm10k_reset_num_queues(interface);
2025 return err;
2026}
2027
2028
2029
2030
2031
2032
2033
2034
2035void fm10k_clear_queueing_scheme(struct fm10k_intfc *interface)
2036{
2037 fm10k_free_q_vectors(interface);
2038 fm10k_reset_msix_capability(interface);
2039}
2040