1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/skbuff.h>
36#include <linux/netdevice.h>
37#include <linux/etherdevice.h>
38#include <linux/if_vlan.h>
39#include <linux/ip.h>
40#include <linux/dma-mapping.h>
41#include <linux/jiffies.h>
42#include <net/ipv6.h>
43#include <net/tcp.h>
44#include "cxgb4.h"
45#include "t4_regs.h"
46#include "t4_msg.h"
47#include "t4fw_api.h"
48
49
50
51
52
53#if PAGE_SHIFT >= 16
54# define FL_PG_ORDER 0
55#else
56# define FL_PG_ORDER (16 - PAGE_SHIFT)
57#endif
58
59
60#define RX_COPY_THRES 256
61#define RX_PULL_LEN 128
62
63
64
65
66
67#define RX_PKT_SKB_LEN 512
68
69
70#define RX_PKT_PAD 2
71
72
73
74
75
76
77
78#define MAX_TX_RECLAIM 16
79
80
81
82
83
84#define MAX_RX_REFILL 16U
85
86
87
88
89
90#define RX_QCHECK_PERIOD (HZ / 2)
91
92
93
94
95#define TX_QCHECK_PERIOD (HZ / 2)
96
97
98
99
100#define MAX_TIMER_TX_RECLAIM 100
101
102
103
104
105#define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
106
107
108
109
110
111#define FL_STARVE_THRES 4
112
113
114
115
116
117
118#define ETHTXQ_STOP_THRES \
119 (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
120
121
122
123
124
125#define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
126
127
128
129
130
131#define MAX_IMM_TX_PKT_LEN 128
132
133
134
135
136#define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
137
138enum {
139
140 FL_ALIGN = L1_CACHE_BYTES < 32 ? 32 : L1_CACHE_BYTES,
141
142 STAT_LEN = L1_CACHE_BYTES > 64 ? 128 : 64
143};
144
145struct tx_sw_desc {
146 struct sk_buff *skb;
147 struct ulptx_sgl *sgl;
148};
149
150struct rx_sw_desc {
151 struct page *page;
152 dma_addr_t dma_addr;
153};
154
155
156
157
158enum {
159 RX_LARGE_BUF = 1 << 0,
160 RX_UNMAPPED_BUF = 1 << 1,
161};
162
163static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
164{
165 return d->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF);
166}
167
168static inline bool is_buf_mapped(const struct rx_sw_desc *d)
169{
170 return !(d->dma_addr & RX_UNMAPPED_BUF);
171}
172
173
174
175
176
177
178
179
180static inline unsigned int txq_avail(const struct sge_txq *q)
181{
182 return q->size - 1 - q->in_use;
183}
184
185
186
187
188
189
190
191
192
193static inline unsigned int fl_cap(const struct sge_fl *fl)
194{
195 return fl->size - 8;
196}
197
198static inline bool fl_starving(const struct sge_fl *fl)
199{
200 return fl->avail - fl->pend_cred <= FL_STARVE_THRES;
201}
202
203static int map_skb(struct device *dev, const struct sk_buff *skb,
204 dma_addr_t *addr)
205{
206 const skb_frag_t *fp, *end;
207 const struct skb_shared_info *si;
208
209 *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
210 if (dma_mapping_error(dev, *addr))
211 goto out_err;
212
213 si = skb_shinfo(skb);
214 end = &si->frags[si->nr_frags];
215
216 for (fp = si->frags; fp < end; fp++) {
217 *++addr = dma_map_page(dev, fp->page, fp->page_offset, fp->size,
218 DMA_TO_DEVICE);
219 if (dma_mapping_error(dev, *addr))
220 goto unwind;
221 }
222 return 0;
223
224unwind:
225 while (fp-- > si->frags)
226 dma_unmap_page(dev, *--addr, fp->size, DMA_TO_DEVICE);
227
228 dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
229out_err:
230 return -ENOMEM;
231}
232
233#ifdef CONFIG_NEED_DMA_MAP_STATE
234static void unmap_skb(struct device *dev, const struct sk_buff *skb,
235 const dma_addr_t *addr)
236{
237 const skb_frag_t *fp, *end;
238 const struct skb_shared_info *si;
239
240 dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
241
242 si = skb_shinfo(skb);
243 end = &si->frags[si->nr_frags];
244 for (fp = si->frags; fp < end; fp++)
245 dma_unmap_page(dev, *addr++, fp->size, DMA_TO_DEVICE);
246}
247
248
249
250
251
252
253
254
255
256static void deferred_unmap_destructor(struct sk_buff *skb)
257{
258 unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
259}
260#endif
261
262static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
263 const struct ulptx_sgl *sgl, const struct sge_txq *q)
264{
265 const struct ulptx_sge_pair *p;
266 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
267
268 if (likely(skb_headlen(skb)))
269 dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
270 DMA_TO_DEVICE);
271 else {
272 dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
273 DMA_TO_DEVICE);
274 nfrags--;
275 }
276
277
278
279
280
281 for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
282 if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
283unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
284 ntohl(p->len[0]), DMA_TO_DEVICE);
285 dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
286 ntohl(p->len[1]), DMA_TO_DEVICE);
287 p++;
288 } else if ((u8 *)p == (u8 *)q->stat) {
289 p = (const struct ulptx_sge_pair *)q->desc;
290 goto unmap;
291 } else if ((u8 *)p + 8 == (u8 *)q->stat) {
292 const __be64 *addr = (const __be64 *)q->desc;
293
294 dma_unmap_page(dev, be64_to_cpu(addr[0]),
295 ntohl(p->len[0]), DMA_TO_DEVICE);
296 dma_unmap_page(dev, be64_to_cpu(addr[1]),
297 ntohl(p->len[1]), DMA_TO_DEVICE);
298 p = (const struct ulptx_sge_pair *)&addr[2];
299 } else {
300 const __be64 *addr = (const __be64 *)q->desc;
301
302 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
303 ntohl(p->len[0]), DMA_TO_DEVICE);
304 dma_unmap_page(dev, be64_to_cpu(addr[0]),
305 ntohl(p->len[1]), DMA_TO_DEVICE);
306 p = (const struct ulptx_sge_pair *)&addr[1];
307 }
308 }
309 if (nfrags) {
310 __be64 addr;
311
312 if ((u8 *)p == (u8 *)q->stat)
313 p = (const struct ulptx_sge_pair *)q->desc;
314 addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
315 *(const __be64 *)q->desc;
316 dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
317 DMA_TO_DEVICE);
318 }
319}
320
321
322
323
324
325
326
327
328
329
330
331static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
332 unsigned int n, bool unmap)
333{
334 struct tx_sw_desc *d;
335 unsigned int cidx = q->cidx;
336 struct device *dev = adap->pdev_dev;
337
338 d = &q->sdesc[cidx];
339 while (n--) {
340 if (d->skb) {
341 if (unmap)
342 unmap_sgl(dev, d->skb, d->sgl, q);
343 kfree_skb(d->skb);
344 d->skb = NULL;
345 }
346 ++d;
347 if (++cidx == q->size) {
348 cidx = 0;
349 d = q->sdesc;
350 }
351 }
352 q->cidx = cidx;
353}
354
355
356
357
358static inline int reclaimable(const struct sge_txq *q)
359{
360 int hw_cidx = ntohs(q->stat->cidx);
361 hw_cidx -= q->cidx;
362 return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
363}
364
365
366
367
368
369
370
371
372
373
374
375static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
376 bool unmap)
377{
378 int avail = reclaimable(q);
379
380 if (avail) {
381
382
383
384
385 if (avail > MAX_TX_RECLAIM)
386 avail = MAX_TX_RECLAIM;
387
388 free_tx_desc(adap, q, avail, unmap);
389 q->in_use -= avail;
390 }
391}
392
393static inline int get_buf_size(const struct rx_sw_desc *d)
394{
395#if FL_PG_ORDER > 0
396 return (d->dma_addr & RX_LARGE_BUF) ? (PAGE_SIZE << FL_PG_ORDER) :
397 PAGE_SIZE;
398#else
399 return PAGE_SIZE;
400#endif
401}
402
403
404
405
406
407
408
409
410
411
412static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
413{
414 while (n--) {
415 struct rx_sw_desc *d = &q->sdesc[q->cidx];
416
417 if (is_buf_mapped(d))
418 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
419 get_buf_size(d), PCI_DMA_FROMDEVICE);
420 put_page(d->page);
421 d->page = NULL;
422 if (++q->cidx == q->size)
423 q->cidx = 0;
424 q->avail--;
425 }
426}
427
428
429
430
431
432
433
434
435
436
437
438
439static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
440{
441 struct rx_sw_desc *d = &q->sdesc[q->cidx];
442
443 if (is_buf_mapped(d))
444 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
445 get_buf_size(d), PCI_DMA_FROMDEVICE);
446 d->page = NULL;
447 if (++q->cidx == q->size)
448 q->cidx = 0;
449 q->avail--;
450}
451
452static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
453{
454 if (q->pend_cred >= 8) {
455 wmb();
456 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL), DBPRIO |
457 QID(q->cntxt_id) | PIDX(q->pend_cred / 8));
458 q->pend_cred &= 7;
459 }
460}
461
462static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
463 dma_addr_t mapping)
464{
465 sd->page = pg;
466 sd->dma_addr = mapping;
467}
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
484 gfp_t gfp)
485{
486 struct page *pg;
487 dma_addr_t mapping;
488 unsigned int cred = q->avail;
489 __be64 *d = &q->desc[q->pidx];
490 struct rx_sw_desc *sd = &q->sdesc[q->pidx];
491
492 gfp |= __GFP_NOWARN;
493
494#if FL_PG_ORDER > 0
495
496
497
498 while (n) {
499 pg = alloc_pages(gfp | __GFP_COMP, FL_PG_ORDER);
500 if (unlikely(!pg)) {
501 q->large_alloc_failed++;
502 break;
503 }
504
505 mapping = dma_map_page(adap->pdev_dev, pg, 0,
506 PAGE_SIZE << FL_PG_ORDER,
507 PCI_DMA_FROMDEVICE);
508 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
509 __free_pages(pg, FL_PG_ORDER);
510 goto out;
511 }
512 mapping |= RX_LARGE_BUF;
513 *d++ = cpu_to_be64(mapping);
514
515 set_rx_sw_desc(sd, pg, mapping);
516 sd++;
517
518 q->avail++;
519 if (++q->pidx == q->size) {
520 q->pidx = 0;
521 sd = q->sdesc;
522 d = q->desc;
523 }
524 n--;
525 }
526#endif
527
528 while (n--) {
529 pg = __netdev_alloc_page(adap->port[0], gfp);
530 if (unlikely(!pg)) {
531 q->alloc_failed++;
532 break;
533 }
534
535 mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
536 PCI_DMA_FROMDEVICE);
537 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
538 netdev_free_page(adap->port[0], pg);
539 goto out;
540 }
541 *d++ = cpu_to_be64(mapping);
542
543 set_rx_sw_desc(sd, pg, mapping);
544 sd++;
545
546 q->avail++;
547 if (++q->pidx == q->size) {
548 q->pidx = 0;
549 sd = q->sdesc;
550 d = q->desc;
551 }
552 }
553
554out: cred = q->avail - cred;
555 q->pend_cred += cred;
556 ring_fl_db(adap, q);
557
558 if (unlikely(fl_starving(q))) {
559 smp_wmb();
560 set_bit(q->cntxt_id - adap->sge.egr_start,
561 adap->sge.starving_fl);
562 }
563
564 return cred;
565}
566
567static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
568{
569 refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
570 GFP_ATOMIC);
571}
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
593 size_t sw_size, dma_addr_t *phys, void *metadata,
594 size_t stat_size, int node)
595{
596 size_t len = nelem * elem_size + stat_size;
597 void *s = NULL;
598 void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
599
600 if (!p)
601 return NULL;
602 if (sw_size) {
603 s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
604
605 if (!s) {
606 dma_free_coherent(dev, len, p, *phys);
607 return NULL;
608 }
609 }
610 if (metadata)
611 *(void **)metadata = s;
612 memset(p, 0, len);
613 return p;
614}
615
616
617
618
619
620
621
622
623static inline unsigned int sgl_len(unsigned int n)
624{
625 n--;
626 return (3 * n) / 2 + (n & 1) + 2;
627}
628
629
630
631
632
633
634
635
636static inline unsigned int flits_to_desc(unsigned int n)
637{
638 BUG_ON(n > SGE_MAX_WR_LEN / 8);
639 return DIV_ROUND_UP(n, 8);
640}
641
642
643
644
645
646
647
648
649static inline int is_eth_imm(const struct sk_buff *skb)
650{
651 return skb->len <= MAX_IMM_TX_PKT_LEN - sizeof(struct cpl_tx_pkt);
652}
653
654
655
656
657
658
659
660
661static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
662{
663 unsigned int flits;
664
665 if (is_eth_imm(skb))
666 return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt), 8);
667
668 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
669 if (skb_shinfo(skb)->gso_size)
670 flits += 2;
671 return flits;
672}
673
674
675
676
677
678
679
680
681static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
682{
683 return flits_to_desc(calc_tx_flits(skb));
684}
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
704 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
705 const dma_addr_t *addr)
706{
707 unsigned int i, len;
708 struct ulptx_sge_pair *to;
709 const struct skb_shared_info *si = skb_shinfo(skb);
710 unsigned int nfrags = si->nr_frags;
711 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
712
713 len = skb_headlen(skb) - start;
714 if (likely(len)) {
715 sgl->len0 = htonl(len);
716 sgl->addr0 = cpu_to_be64(addr[0] + start);
717 nfrags++;
718 } else {
719 sgl->len0 = htonl(si->frags[0].size);
720 sgl->addr0 = cpu_to_be64(addr[1]);
721 }
722
723 sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_NSGE(nfrags));
724 if (likely(--nfrags == 0))
725 return;
726
727
728
729
730
731 to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
732
733 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
734 to->len[0] = cpu_to_be32(si->frags[i].size);
735 to->len[1] = cpu_to_be32(si->frags[++i].size);
736 to->addr[0] = cpu_to_be64(addr[i]);
737 to->addr[1] = cpu_to_be64(addr[++i]);
738 }
739 if (nfrags) {
740 to->len[0] = cpu_to_be32(si->frags[i].size);
741 to->len[1] = cpu_to_be32(0);
742 to->addr[0] = cpu_to_be64(addr[i + 1]);
743 }
744 if (unlikely((u8 *)end > (u8 *)q->stat)) {
745 unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
746
747 if (likely(part0))
748 memcpy(sgl->sge, buf, part0);
749 part1 = (u8 *)end - (u8 *)q->stat;
750 memcpy(q->desc, (u8 *)buf + part0, part1);
751 end = (void *)q->desc + part1;
752 }
753 if ((uintptr_t)end & 8)
754 *(u64 *)end = 0;
755}
756
757
758
759
760
761
762
763
764
765static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
766{
767 wmb();
768 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
769 QID(q->cntxt_id) | PIDX(n));
770}
771
772
773
774
775
776
777
778
779
780
781
782
783static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
784 void *pos)
785{
786 u64 *p;
787 int left = (void *)q->stat - pos;
788
789 if (likely(skb->len <= left)) {
790 if (likely(!skb->data_len))
791 skb_copy_from_linear_data(skb, pos, skb->len);
792 else
793 skb_copy_bits(skb, 0, pos, skb->len);
794 pos += skb->len;
795 } else {
796 skb_copy_bits(skb, 0, pos, left);
797 skb_copy_bits(skb, left, q->desc, skb->len - left);
798 pos = (void *)q->desc + (skb->len - left);
799 }
800
801
802 p = PTR_ALIGN(pos, 8);
803 if ((uintptr_t)p & 8)
804 *p = 0;
805}
806
807
808
809
810
811static u64 hwcsum(const struct sk_buff *skb)
812{
813 int csum_type;
814 const struct iphdr *iph = ip_hdr(skb);
815
816 if (iph->version == 4) {
817 if (iph->protocol == IPPROTO_TCP)
818 csum_type = TX_CSUM_TCPIP;
819 else if (iph->protocol == IPPROTO_UDP)
820 csum_type = TX_CSUM_UDPIP;
821 else {
822nocsum:
823
824
825
826 return TXPKT_L4CSUM_DIS;
827 }
828 } else {
829
830
831
832 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
833
834 if (ip6h->nexthdr == IPPROTO_TCP)
835 csum_type = TX_CSUM_TCPIP6;
836 else if (ip6h->nexthdr == IPPROTO_UDP)
837 csum_type = TX_CSUM_UDPIP6;
838 else
839 goto nocsum;
840 }
841
842 if (likely(csum_type >= TX_CSUM_TCPIP))
843 return TXPKT_CSUM_TYPE(csum_type) |
844 TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
845 TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
846 else {
847 int start = skb_transport_offset(skb);
848
849 return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
850 TXPKT_CSUM_LOC(start + skb->csum_offset);
851 }
852}
853
854static void eth_txq_stop(struct sge_eth_txq *q)
855{
856 netif_tx_stop_queue(q->txq);
857 q->q.stops++;
858}
859
860static inline void txq_advance(struct sge_txq *q, unsigned int n)
861{
862 q->in_use += n;
863 q->pidx += n;
864 if (q->pidx >= q->size)
865 q->pidx -= q->size;
866}
867
868
869
870
871
872
873
874
875netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
876{
877 u32 wr_mid;
878 u64 cntrl, *end;
879 int qidx, credits;
880 unsigned int flits, ndesc;
881 struct adapter *adap;
882 struct sge_eth_txq *q;
883 const struct port_info *pi;
884 struct fw_eth_tx_pkt_wr *wr;
885 struct cpl_tx_pkt_core *cpl;
886 const struct skb_shared_info *ssi;
887 dma_addr_t addr[MAX_SKB_FRAGS + 1];
888
889
890
891
892
893 if (unlikely(skb->len < ETH_HLEN)) {
894out_free: dev_kfree_skb(skb);
895 return NETDEV_TX_OK;
896 }
897
898 pi = netdev_priv(dev);
899 adap = pi->adapter;
900 qidx = skb_get_queue_mapping(skb);
901 q = &adap->sge.ethtxq[qidx + pi->first_qset];
902
903 reclaim_completed_tx(adap, &q->q, true);
904
905 flits = calc_tx_flits(skb);
906 ndesc = flits_to_desc(flits);
907 credits = txq_avail(&q->q) - ndesc;
908
909 if (unlikely(credits < 0)) {
910 eth_txq_stop(q);
911 dev_err(adap->pdev_dev,
912 "%s: Tx ring %u full while queue awake!\n",
913 dev->name, qidx);
914 return NETDEV_TX_BUSY;
915 }
916
917 if (!is_eth_imm(skb) &&
918 unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
919 q->mapping_err++;
920 goto out_free;
921 }
922
923 wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
924 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
925 eth_txq_stop(q);
926 wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
927 }
928
929 wr = (void *)&q->q.desc[q->q.pidx];
930 wr->equiq_to_len16 = htonl(wr_mid);
931 wr->r3 = cpu_to_be64(0);
932 end = (u64 *)wr + flits;
933
934 ssi = skb_shinfo(skb);
935 if (ssi->gso_size) {
936 struct cpl_tx_pkt_lso *lso = (void *)wr;
937 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
938 int l3hdr_len = skb_network_header_len(skb);
939 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
940
941 wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
942 FW_WR_IMMDLEN(sizeof(*lso)));
943 lso->c.lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
944 LSO_FIRST_SLICE | LSO_LAST_SLICE |
945 LSO_IPV6(v6) |
946 LSO_ETHHDR_LEN(eth_xtra_len / 4) |
947 LSO_IPHDR_LEN(l3hdr_len / 4) |
948 LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
949 lso->c.ipid_ofst = htons(0);
950 lso->c.mss = htons(ssi->gso_size);
951 lso->c.seqno_offset = htonl(0);
952 lso->c.len = htonl(skb->len);
953 cpl = (void *)(lso + 1);
954 cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
955 TXPKT_IPHDR_LEN(l3hdr_len) |
956 TXPKT_ETHHDR_LEN(eth_xtra_len);
957 q->tso++;
958 q->tx_cso += ssi->gso_segs;
959 } else {
960 int len;
961
962 len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
963 wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
964 FW_WR_IMMDLEN(len));
965 cpl = (void *)(wr + 1);
966 if (skb->ip_summed == CHECKSUM_PARTIAL) {
967 cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
968 q->tx_cso++;
969 } else
970 cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
971 }
972
973 if (vlan_tx_tag_present(skb)) {
974 q->vlan_ins++;
975 cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
976 }
977
978 cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
979 TXPKT_INTF(pi->tx_chan) | TXPKT_PF(adap->fn));
980 cpl->pack = htons(0);
981 cpl->len = htons(skb->len);
982 cpl->ctrl1 = cpu_to_be64(cntrl);
983
984 if (is_eth_imm(skb)) {
985 inline_tx_skb(skb, &q->q, cpl + 1);
986 dev_kfree_skb(skb);
987 } else {
988 int last_desc;
989
990 write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
991 addr);
992 skb_orphan(skb);
993
994 last_desc = q->q.pidx + ndesc - 1;
995 if (last_desc >= q->q.size)
996 last_desc -= q->q.size;
997 q->q.sdesc[last_desc].skb = skb;
998 q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
999 }
1000
1001 txq_advance(&q->q, ndesc);
1002
1003 ring_tx_db(adap, &q->q, ndesc);
1004 return NETDEV_TX_OK;
1005}
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015static inline void reclaim_completed_tx_imm(struct sge_txq *q)
1016{
1017 int hw_cidx = ntohs(q->stat->cidx);
1018 int reclaim = hw_cidx - q->cidx;
1019
1020 if (reclaim < 0)
1021 reclaim += q->size;
1022
1023 q->in_use -= reclaim;
1024 q->cidx = hw_cidx;
1025}
1026
1027
1028
1029
1030
1031
1032
1033static inline int is_imm(const struct sk_buff *skb)
1034{
1035 return skb->len <= MAX_CTRL_WR_LEN;
1036}
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
1049{
1050 reclaim_completed_tx_imm(&q->q);
1051 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1052 wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
1053 q->q.stops++;
1054 q->full = 1;
1055 }
1056}
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
1067{
1068 unsigned int ndesc;
1069 struct fw_wr_hdr *wr;
1070
1071 if (unlikely(!is_imm(skb))) {
1072 WARN_ON(1);
1073 dev_kfree_skb(skb);
1074 return NET_XMIT_DROP;
1075 }
1076
1077 ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
1078 spin_lock(&q->sendq.lock);
1079
1080 if (unlikely(q->full)) {
1081 skb->priority = ndesc;
1082 __skb_queue_tail(&q->sendq, skb);
1083 spin_unlock(&q->sendq.lock);
1084 return NET_XMIT_CN;
1085 }
1086
1087 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1088 inline_tx_skb(skb, &q->q, wr);
1089
1090 txq_advance(&q->q, ndesc);
1091 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
1092 ctrlq_check_stop(q, wr);
1093
1094 ring_tx_db(q->adap, &q->q, ndesc);
1095 spin_unlock(&q->sendq.lock);
1096
1097 kfree_skb(skb);
1098 return NET_XMIT_SUCCESS;
1099}
1100
1101
1102
1103
1104
1105
1106
1107static void restart_ctrlq(unsigned long data)
1108{
1109 struct sk_buff *skb;
1110 unsigned int written = 0;
1111 struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
1112
1113 spin_lock(&q->sendq.lock);
1114 reclaim_completed_tx_imm(&q->q);
1115 BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES);
1116
1117 while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
1118 struct fw_wr_hdr *wr;
1119 unsigned int ndesc = skb->priority;
1120
1121
1122
1123
1124
1125 spin_unlock(&q->sendq.lock);
1126
1127 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1128 inline_tx_skb(skb, &q->q, wr);
1129 kfree_skb(skb);
1130
1131 written += ndesc;
1132 txq_advance(&q->q, ndesc);
1133 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1134 unsigned long old = q->q.stops;
1135
1136 ctrlq_check_stop(q, wr);
1137 if (q->q.stops != old) {
1138 spin_lock(&q->sendq.lock);
1139 goto ringdb;
1140 }
1141 }
1142 if (written > 16) {
1143 ring_tx_db(q->adap, &q->q, written);
1144 written = 0;
1145 }
1146 spin_lock(&q->sendq.lock);
1147 }
1148 q->full = 0;
1149ringdb: if (written)
1150 ring_tx_db(q->adap, &q->q, written);
1151 spin_unlock(&q->sendq.lock);
1152}
1153
1154
1155
1156
1157
1158
1159
1160
1161int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
1162{
1163 int ret;
1164
1165 local_bh_disable();
1166 ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
1167 local_bh_enable();
1168 return ret;
1169}
1170
1171
1172
1173
1174
1175
1176
1177
1178static inline int is_ofld_imm(const struct sk_buff *skb)
1179{
1180 return skb->len <= MAX_IMM_TX_PKT_LEN;
1181}
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
1192{
1193 unsigned int flits, cnt;
1194
1195 if (is_ofld_imm(skb))
1196 return DIV_ROUND_UP(skb->len, 8);
1197
1198 flits = skb_transport_offset(skb) / 8U;
1199 cnt = skb_shinfo(skb)->nr_frags;
1200 if (skb->tail != skb->transport_header)
1201 cnt++;
1202 return flits + sgl_len(cnt);
1203}
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214static void txq_stop_maperr(struct sge_ofld_txq *q)
1215{
1216 q->mapping_err++;
1217 q->q.stops++;
1218 set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
1219 q->adap->sge.txq_maperr);
1220}
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
1231{
1232 struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
1233
1234 wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
1235 q->q.stops++;
1236 q->full = 1;
1237}
1238
1239
1240
1241
1242
1243
1244
1245
1246static void service_ofldq(struct sge_ofld_txq *q)
1247{
1248 u64 *pos;
1249 int credits;
1250 struct sk_buff *skb;
1251 unsigned int written = 0;
1252 unsigned int flits, ndesc;
1253
1254 while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
1255
1256
1257
1258
1259 spin_unlock(&q->sendq.lock);
1260
1261 reclaim_completed_tx(q->adap, &q->q, false);
1262
1263 flits = skb->priority;
1264 ndesc = flits_to_desc(flits);
1265 credits = txq_avail(&q->q) - ndesc;
1266 BUG_ON(credits < 0);
1267 if (unlikely(credits < TXQ_STOP_THRES))
1268 ofldtxq_stop(q, skb);
1269
1270 pos = (u64 *)&q->q.desc[q->q.pidx];
1271 if (is_ofld_imm(skb))
1272 inline_tx_skb(skb, &q->q, pos);
1273 else if (map_skb(q->adap->pdev_dev, skb,
1274 (dma_addr_t *)skb->head)) {
1275 txq_stop_maperr(q);
1276 spin_lock(&q->sendq.lock);
1277 break;
1278 } else {
1279 int last_desc, hdr_len = skb_transport_offset(skb);
1280
1281 memcpy(pos, skb->data, hdr_len);
1282 write_sgl(skb, &q->q, (void *)pos + hdr_len,
1283 pos + flits, hdr_len,
1284 (dma_addr_t *)skb->head);
1285#ifdef CONFIG_NEED_DMA_MAP_STATE
1286 skb->dev = q->adap->port[0];
1287 skb->destructor = deferred_unmap_destructor;
1288#endif
1289 last_desc = q->q.pidx + ndesc - 1;
1290 if (last_desc >= q->q.size)
1291 last_desc -= q->q.size;
1292 q->q.sdesc[last_desc].skb = skb;
1293 }
1294
1295 txq_advance(&q->q, ndesc);
1296 written += ndesc;
1297 if (unlikely(written > 32)) {
1298 ring_tx_db(q->adap, &q->q, written);
1299 written = 0;
1300 }
1301
1302 spin_lock(&q->sendq.lock);
1303 __skb_unlink(skb, &q->sendq);
1304 if (is_ofld_imm(skb))
1305 kfree_skb(skb);
1306 }
1307 if (likely(written))
1308 ring_tx_db(q->adap, &q->q, written);
1309}
1310
1311
1312
1313
1314
1315
1316
1317
1318static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
1319{
1320 skb->priority = calc_tx_flits_ofld(skb);
1321 spin_lock(&q->sendq.lock);
1322 __skb_queue_tail(&q->sendq, skb);
1323 if (q->sendq.qlen == 1)
1324 service_ofldq(q);
1325 spin_unlock(&q->sendq.lock);
1326 return NET_XMIT_SUCCESS;
1327}
1328
1329
1330
1331
1332
1333
1334
1335static void restart_ofldq(unsigned long data)
1336{
1337 struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
1338
1339 spin_lock(&q->sendq.lock);
1340 q->full = 0;
1341 service_ofldq(q);
1342 spin_unlock(&q->sendq.lock);
1343}
1344
1345
1346
1347
1348
1349
1350
1351
1352static inline unsigned int skb_txq(const struct sk_buff *skb)
1353{
1354 return skb->queue_mapping >> 1;
1355}
1356
1357
1358
1359
1360
1361
1362
1363
1364static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
1365{
1366 return skb->queue_mapping & 1;
1367}
1368
1369static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
1370{
1371 unsigned int idx = skb_txq(skb);
1372
1373 if (unlikely(is_ctrl_pkt(skb)))
1374 return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
1375 return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
1376}
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
1388{
1389 int ret;
1390
1391 local_bh_disable();
1392 ret = ofld_send(adap, skb);
1393 local_bh_enable();
1394 return ret;
1395}
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
1406{
1407 return t4_ofld_send(netdev2adap(dev), skb);
1408}
1409EXPORT_SYMBOL(cxgb4_ofld_send);
1410
1411static inline void copy_frags(struct skb_shared_info *ssi,
1412 const struct pkt_gl *gl, unsigned int offset)
1413{
1414 unsigned int n;
1415
1416
1417 ssi->frags[0].page = gl->frags[0].page;
1418 ssi->frags[0].page_offset = gl->frags[0].page_offset + offset;
1419 ssi->frags[0].size = gl->frags[0].size - offset;
1420 ssi->nr_frags = gl->nfrags;
1421 n = gl->nfrags - 1;
1422 if (n)
1423 memcpy(&ssi->frags[1], &gl->frags[1], n * sizeof(skb_frag_t));
1424
1425
1426 get_page(gl->frags[n].page);
1427}
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
1439 unsigned int skb_len, unsigned int pull_len)
1440{
1441 struct sk_buff *skb;
1442
1443
1444
1445
1446
1447
1448 if (gl->tot_len <= RX_COPY_THRES) {
1449 skb = dev_alloc_skb(gl->tot_len);
1450 if (unlikely(!skb))
1451 goto out;
1452 __skb_put(skb, gl->tot_len);
1453 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1454 } else {
1455 skb = dev_alloc_skb(skb_len);
1456 if (unlikely(!skb))
1457 goto out;
1458 __skb_put(skb, pull_len);
1459 skb_copy_to_linear_data(skb, gl->va, pull_len);
1460
1461 copy_frags(skb_shinfo(skb), gl, pull_len);
1462 skb->len = gl->tot_len;
1463 skb->data_len = skb->len - pull_len;
1464 skb->truesize += skb->data_len;
1465 }
1466out: return skb;
1467}
1468EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
1469
1470
1471
1472
1473
1474
1475
1476
1477static void t4_pktgl_free(const struct pkt_gl *gl)
1478{
1479 int n;
1480 const skb_frag_t *p;
1481
1482 for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
1483 put_page(p->page);
1484}
1485
1486
1487
1488
1489
1490static noinline int handle_trace_pkt(struct adapter *adap,
1491 const struct pkt_gl *gl)
1492{
1493 struct sk_buff *skb;
1494 struct cpl_trace_pkt *p;
1495
1496 skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
1497 if (unlikely(!skb)) {
1498 t4_pktgl_free(gl);
1499 return 0;
1500 }
1501
1502 p = (struct cpl_trace_pkt *)skb->data;
1503 __skb_pull(skb, sizeof(*p));
1504 skb_reset_mac_header(skb);
1505 skb->protocol = htons(0xffff);
1506 skb->dev = adap->port[0];
1507 netif_receive_skb(skb);
1508 return 0;
1509}
1510
1511static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1512 const struct cpl_rx_pkt *pkt)
1513{
1514 int ret;
1515 struct sk_buff *skb;
1516
1517 skb = napi_get_frags(&rxq->rspq.napi);
1518 if (unlikely(!skb)) {
1519 t4_pktgl_free(gl);
1520 rxq->stats.rx_drops++;
1521 return;
1522 }
1523
1524 copy_frags(skb_shinfo(skb), gl, RX_PKT_PAD);
1525 skb->len = gl->tot_len - RX_PKT_PAD;
1526 skb->data_len = skb->len;
1527 skb->truesize += skb->data_len;
1528 skb->ip_summed = CHECKSUM_UNNECESSARY;
1529 skb_record_rx_queue(skb, rxq->rspq.idx);
1530 if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
1531 skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
1532
1533 if (unlikely(pkt->vlan_ex)) {
1534 __vlan_hwaccel_put_tag(skb, ntohs(pkt->vlan));
1535 rxq->stats.vlan_ex++;
1536 }
1537 ret = napi_gro_frags(&rxq->rspq.napi);
1538 if (ret == GRO_HELD)
1539 rxq->stats.lro_pkts++;
1540 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1541 rxq->stats.lro_merged++;
1542 rxq->stats.pkts++;
1543 rxq->stats.rx_cso++;
1544}
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
1555 const struct pkt_gl *si)
1556{
1557 bool csum_ok;
1558 struct sk_buff *skb;
1559 struct port_info *pi;
1560 const struct cpl_rx_pkt *pkt;
1561 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1562
1563 if (unlikely(*(u8 *)rsp == CPL_TRACE_PKT))
1564 return handle_trace_pkt(q->adap, si);
1565
1566 pkt = (const struct cpl_rx_pkt *)rsp;
1567 csum_ok = pkt->csum_calc && !pkt->err_vec;
1568 if ((pkt->l2info & htonl(RXF_TCP)) &&
1569 (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
1570 do_gro(rxq, si, pkt);
1571 return 0;
1572 }
1573
1574 skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
1575 if (unlikely(!skb)) {
1576 t4_pktgl_free(si);
1577 rxq->stats.rx_drops++;
1578 return 0;
1579 }
1580
1581 __skb_pull(skb, RX_PKT_PAD);
1582 skb->protocol = eth_type_trans(skb, q->netdev);
1583 skb_record_rx_queue(skb, q->idx);
1584 if (skb->dev->features & NETIF_F_RXHASH)
1585 skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
1586
1587 pi = netdev_priv(skb->dev);
1588 rxq->stats.pkts++;
1589
1590 if (csum_ok && (pi->rx_offload & RX_CSO) &&
1591 (pkt->l2info & htonl(RXF_UDP | RXF_TCP))) {
1592 if (!pkt->ip_frag) {
1593 skb->ip_summed = CHECKSUM_UNNECESSARY;
1594 rxq->stats.rx_cso++;
1595 } else if (pkt->l2info & htonl(RXF_IP)) {
1596 __sum16 c = (__force __sum16)pkt->csum;
1597 skb->csum = csum_unfold(c);
1598 skb->ip_summed = CHECKSUM_COMPLETE;
1599 rxq->stats.rx_cso++;
1600 }
1601 } else
1602 skb_checksum_none_assert(skb);
1603
1604 if (unlikely(pkt->vlan_ex)) {
1605 __vlan_hwaccel_put_tag(skb, ntohs(pkt->vlan));
1606 rxq->stats.vlan_ex++;
1607 }
1608 netif_receive_skb(skb);
1609 return 0;
1610}
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
1628 int frags)
1629{
1630 struct rx_sw_desc *d;
1631
1632 while (frags--) {
1633 if (q->cidx == 0)
1634 q->cidx = q->size - 1;
1635 else
1636 q->cidx--;
1637 d = &q->sdesc[q->cidx];
1638 d->page = si->frags[frags].page;
1639 d->dma_addr |= RX_UNMAPPED_BUF;
1640 q->avail++;
1641 }
1642}
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652static inline bool is_new_response(const struct rsp_ctrl *r,
1653 const struct sge_rspq *q)
1654{
1655 return RSPD_GEN(r->type_gen) == q->gen;
1656}
1657
1658
1659
1660
1661
1662
1663
1664static inline void rspq_next(struct sge_rspq *q)
1665{
1666 q->cur_desc = (void *)q->cur_desc + q->iqe_len;
1667 if (unlikely(++q->cidx == q->size)) {
1668 q->cidx = 0;
1669 q->gen ^= 1;
1670 q->cur_desc = q->desc;
1671 }
1672}
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687static int process_responses(struct sge_rspq *q, int budget)
1688{
1689 int ret, rsp_type;
1690 int budget_left = budget;
1691 const struct rsp_ctrl *rc;
1692 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1693
1694 while (likely(budget_left)) {
1695 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
1696 if (!is_new_response(rc, q))
1697 break;
1698
1699 rmb();
1700 rsp_type = RSPD_TYPE(rc->type_gen);
1701 if (likely(rsp_type == RSP_TYPE_FLBUF)) {
1702 skb_frag_t *fp;
1703 struct pkt_gl si;
1704 const struct rx_sw_desc *rsd;
1705 u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
1706
1707 if (len & RSPD_NEWBUF) {
1708 if (likely(q->offset > 0)) {
1709 free_rx_bufs(q->adap, &rxq->fl, 1);
1710 q->offset = 0;
1711 }
1712 len = RSPD_LEN(len);
1713 }
1714 si.tot_len = len;
1715
1716
1717 for (frags = 0, fp = si.frags; ; frags++, fp++) {
1718 rsd = &rxq->fl.sdesc[rxq->fl.cidx];
1719 bufsz = get_buf_size(rsd);
1720 fp->page = rsd->page;
1721 fp->page_offset = q->offset;
1722 fp->size = min(bufsz, len);
1723 len -= fp->size;
1724 if (!len)
1725 break;
1726 unmap_rx_buf(q->adap, &rxq->fl);
1727 }
1728
1729
1730
1731
1732
1733 dma_sync_single_for_cpu(q->adap->pdev_dev,
1734 get_buf_addr(rsd),
1735 fp->size, DMA_FROM_DEVICE);
1736
1737 si.va = page_address(si.frags[0].page) +
1738 si.frags[0].page_offset;
1739 prefetch(si.va);
1740
1741 si.nfrags = frags + 1;
1742 ret = q->handler(q, q->cur_desc, &si);
1743 if (likely(ret == 0))
1744 q->offset += ALIGN(fp->size, FL_ALIGN);
1745 else
1746 restore_rx_bufs(&si, &rxq->fl, frags);
1747 } else if (likely(rsp_type == RSP_TYPE_CPL)) {
1748 ret = q->handler(q, q->cur_desc, NULL);
1749 } else {
1750 ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
1751 }
1752
1753 if (unlikely(ret)) {
1754
1755 q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
1756 break;
1757 }
1758
1759 rspq_next(q);
1760 budget_left--;
1761 }
1762
1763 if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
1764 __refill_fl(q->adap, &rxq->fl);
1765 return budget - budget_left;
1766}
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779static int napi_rx_handler(struct napi_struct *napi, int budget)
1780{
1781 unsigned int params;
1782 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
1783 int work_done = process_responses(q, budget);
1784
1785 if (likely(work_done < budget)) {
1786 napi_complete(napi);
1787 params = q->next_intr_params;
1788 q->next_intr_params = q->intr_params;
1789 } else
1790 params = QINTR_TIMER_IDX(7);
1791
1792 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS), CIDXINC(work_done) |
1793 INGRESSQID((u32)q->cntxt_id) | SEINTARM(params));
1794 return work_done;
1795}
1796
1797
1798
1799
1800irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
1801{
1802 struct sge_rspq *q = cookie;
1803
1804 napi_schedule(&q->napi);
1805 return IRQ_HANDLED;
1806}
1807
1808
1809
1810
1811
1812static unsigned int process_intrq(struct adapter *adap)
1813{
1814 unsigned int credits;
1815 const struct rsp_ctrl *rc;
1816 struct sge_rspq *q = &adap->sge.intrq;
1817
1818 spin_lock(&adap->sge.intrq_lock);
1819 for (credits = 0; ; credits++) {
1820 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
1821 if (!is_new_response(rc, q))
1822 break;
1823
1824 rmb();
1825 if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
1826 unsigned int qid = ntohl(rc->pldbuflen_qid);
1827
1828 qid -= adap->sge.ingr_start;
1829 napi_schedule(&adap->sge.ingr_map[qid]->napi);
1830 }
1831
1832 rspq_next(q);
1833 }
1834
1835 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS), CIDXINC(credits) |
1836 INGRESSQID(q->cntxt_id) | SEINTARM(q->intr_params));
1837 spin_unlock(&adap->sge.intrq_lock);
1838 return credits;
1839}
1840
1841
1842
1843
1844
1845static irqreturn_t t4_intr_msi(int irq, void *cookie)
1846{
1847 struct adapter *adap = cookie;
1848
1849 t4_slow_intr_handler(adap);
1850 process_intrq(adap);
1851 return IRQ_HANDLED;
1852}
1853
1854
1855
1856
1857
1858
1859static irqreturn_t t4_intr_intx(int irq, void *cookie)
1860{
1861 struct adapter *adap = cookie;
1862
1863 t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI), 0);
1864 if (t4_slow_intr_handler(adap) | process_intrq(adap))
1865 return IRQ_HANDLED;
1866 return IRQ_NONE;
1867}
1868
1869
1870
1871
1872
1873
1874
1875
1876irq_handler_t t4_intr_handler(struct adapter *adap)
1877{
1878 if (adap->flags & USING_MSIX)
1879 return t4_sge_intr_msix;
1880 if (adap->flags & USING_MSI)
1881 return t4_intr_msi;
1882 return t4_intr_intx;
1883}
1884
1885static void sge_rx_timer_cb(unsigned long data)
1886{
1887 unsigned long m;
1888 unsigned int i, cnt[2];
1889 struct adapter *adap = (struct adapter *)data;
1890 struct sge *s = &adap->sge;
1891
1892 for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++)
1893 for (m = s->starving_fl[i]; m; m &= m - 1) {
1894 struct sge_eth_rxq *rxq;
1895 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
1896 struct sge_fl *fl = s->egr_map[id];
1897
1898 clear_bit(id, s->starving_fl);
1899 smp_mb__after_clear_bit();
1900
1901 if (fl_starving(fl)) {
1902 rxq = container_of(fl, struct sge_eth_rxq, fl);
1903 if (napi_reschedule(&rxq->rspq.napi))
1904 fl->starving++;
1905 else
1906 set_bit(id, s->starving_fl);
1907 }
1908 }
1909
1910 t4_write_reg(adap, SGE_DEBUG_INDEX, 13);
1911 cnt[0] = t4_read_reg(adap, SGE_DEBUG_DATA_HIGH);
1912 cnt[1] = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
1913
1914 for (i = 0; i < 2; i++)
1915 if (cnt[i] >= s->starve_thres) {
1916 if (s->idma_state[i] || cnt[i] == 0xffffffff)
1917 continue;
1918 s->idma_state[i] = 1;
1919 t4_write_reg(adap, SGE_DEBUG_INDEX, 11);
1920 m = t4_read_reg(adap, SGE_DEBUG_DATA_LOW) >> (i * 16);
1921 dev_warn(adap->pdev_dev,
1922 "SGE idma%u starvation detected for "
1923 "queue %lu\n", i, m & 0xffff);
1924 } else if (s->idma_state[i])
1925 s->idma_state[i] = 0;
1926
1927 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
1928}
1929
1930static void sge_tx_timer_cb(unsigned long data)
1931{
1932 unsigned long m;
1933 unsigned int i, budget;
1934 struct adapter *adap = (struct adapter *)data;
1935 struct sge *s = &adap->sge;
1936
1937 for (i = 0; i < ARRAY_SIZE(s->txq_maperr); i++)
1938 for (m = s->txq_maperr[i]; m; m &= m - 1) {
1939 unsigned long id = __ffs(m) + i * BITS_PER_LONG;
1940 struct sge_ofld_txq *txq = s->egr_map[id];
1941
1942 clear_bit(id, s->txq_maperr);
1943 tasklet_schedule(&txq->qresume_tsk);
1944 }
1945
1946 budget = MAX_TIMER_TX_RECLAIM;
1947 i = s->ethtxq_rover;
1948 do {
1949 struct sge_eth_txq *q = &s->ethtxq[i];
1950
1951 if (q->q.in_use &&
1952 time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
1953 __netif_tx_trylock(q->txq)) {
1954 int avail = reclaimable(&q->q);
1955
1956 if (avail) {
1957 if (avail > budget)
1958 avail = budget;
1959
1960 free_tx_desc(adap, &q->q, avail, true);
1961 q->q.in_use -= avail;
1962 budget -= avail;
1963 }
1964 __netif_tx_unlock(q->txq);
1965 }
1966
1967 if (++i >= s->ethqsets)
1968 i = 0;
1969 } while (budget && i != s->ethtxq_rover);
1970 s->ethtxq_rover = i;
1971 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
1972}
1973
1974int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
1975 struct net_device *dev, int intr_idx,
1976 struct sge_fl *fl, rspq_handler_t hnd)
1977{
1978 int ret, flsz = 0;
1979 struct fw_iq_cmd c;
1980 struct port_info *pi = netdev_priv(dev);
1981
1982
1983 iq->size = roundup(iq->size, 16);
1984
1985 iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
1986 &iq->phys_addr, NULL, 0, NUMA_NO_NODE);
1987 if (!iq->desc)
1988 return -ENOMEM;
1989
1990 memset(&c, 0, sizeof(c));
1991 c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
1992 FW_CMD_WRITE | FW_CMD_EXEC |
1993 FW_IQ_CMD_PFN(adap->fn) | FW_IQ_CMD_VFN(0));
1994 c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC | FW_IQ_CMD_IQSTART(1) |
1995 FW_LEN16(c));
1996 c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
1997 FW_IQ_CMD_IQASYNCH(fwevtq) | FW_IQ_CMD_VIID(pi->viid) |
1998 FW_IQ_CMD_IQANDST(intr_idx < 0) | FW_IQ_CMD_IQANUD(1) |
1999 FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
2000 -intr_idx - 1));
2001 c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
2002 FW_IQ_CMD_IQGTSMODE |
2003 FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
2004 FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
2005 c.iqsize = htons(iq->size);
2006 c.iqaddr = cpu_to_be64(iq->phys_addr);
2007
2008 if (fl) {
2009 fl->size = roundup(fl->size, 8);
2010 fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
2011 sizeof(struct rx_sw_desc), &fl->addr,
2012 &fl->sdesc, STAT_LEN, NUMA_NO_NODE);
2013 if (!fl->desc)
2014 goto fl_nomem;
2015
2016 flsz = fl->size / 8 + STAT_LEN / sizeof(struct tx_desc);
2017 c.iqns_to_fl0congen = htonl(FW_IQ_CMD_FL0PACKEN |
2018 FW_IQ_CMD_FL0FETCHRO(1) |
2019 FW_IQ_CMD_FL0DATARO(1) |
2020 FW_IQ_CMD_FL0PADEN);
2021 c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN(2) |
2022 FW_IQ_CMD_FL0FBMAX(3));
2023 c.fl0size = htons(flsz);
2024 c.fl0addr = cpu_to_be64(fl->addr);
2025 }
2026
2027 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2028 if (ret)
2029 goto err;
2030
2031 netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
2032 iq->cur_desc = iq->desc;
2033 iq->cidx = 0;
2034 iq->gen = 1;
2035 iq->next_intr_params = iq->intr_params;
2036 iq->cntxt_id = ntohs(c.iqid);
2037 iq->abs_id = ntohs(c.physiqid);
2038 iq->size--;
2039 iq->adap = adap;
2040 iq->netdev = dev;
2041 iq->handler = hnd;
2042
2043
2044 iq->offset = fl ? 0 : -1;
2045
2046 adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
2047
2048 if (fl) {
2049 fl->cntxt_id = ntohs(c.fl0id);
2050 fl->avail = fl->pend_cred = 0;
2051 fl->pidx = fl->cidx = 0;
2052 fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
2053 adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
2054 refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
2055 }
2056 return 0;
2057
2058fl_nomem:
2059 ret = -ENOMEM;
2060err:
2061 if (iq->desc) {
2062 dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
2063 iq->desc, iq->phys_addr);
2064 iq->desc = NULL;
2065 }
2066 if (fl && fl->desc) {
2067 kfree(fl->sdesc);
2068 fl->sdesc = NULL;
2069 dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
2070 fl->desc, fl->addr);
2071 fl->desc = NULL;
2072 }
2073 return ret;
2074}
2075
2076static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
2077{
2078 q->in_use = 0;
2079 q->cidx = q->pidx = 0;
2080 q->stops = q->restarts = 0;
2081 q->stat = (void *)&q->desc[q->size];
2082 q->cntxt_id = id;
2083 adap->sge.egr_map[id - adap->sge.egr_start] = q;
2084}
2085
2086int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
2087 struct net_device *dev, struct netdev_queue *netdevq,
2088 unsigned int iqid)
2089{
2090 int ret, nentries;
2091 struct fw_eq_eth_cmd c;
2092 struct port_info *pi = netdev_priv(dev);
2093
2094
2095 nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
2096
2097 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2098 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
2099 &txq->q.phys_addr, &txq->q.sdesc, STAT_LEN,
2100 netdev_queue_numa_node_read(netdevq));
2101 if (!txq->q.desc)
2102 return -ENOMEM;
2103
2104 memset(&c, 0, sizeof(c));
2105 c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
2106 FW_CMD_WRITE | FW_CMD_EXEC |
2107 FW_EQ_ETH_CMD_PFN(adap->fn) | FW_EQ_ETH_CMD_VFN(0));
2108 c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC |
2109 FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
2110 c.viid_pkd = htonl(FW_EQ_ETH_CMD_VIID(pi->viid));
2111 c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE(2) |
2112 FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
2113 FW_EQ_ETH_CMD_FETCHRO(1) |
2114 FW_EQ_ETH_CMD_IQID(iqid));
2115 c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN(2) |
2116 FW_EQ_ETH_CMD_FBMAX(3) |
2117 FW_EQ_ETH_CMD_CIDXFTHRESH(5) |
2118 FW_EQ_ETH_CMD_EQSIZE(nentries));
2119 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2120
2121 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2122 if (ret) {
2123 kfree(txq->q.sdesc);
2124 txq->q.sdesc = NULL;
2125 dma_free_coherent(adap->pdev_dev,
2126 nentries * sizeof(struct tx_desc),
2127 txq->q.desc, txq->q.phys_addr);
2128 txq->q.desc = NULL;
2129 return ret;
2130 }
2131
2132 init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_GET(ntohl(c.eqid_pkd)));
2133 txq->txq = netdevq;
2134 txq->tso = txq->tx_cso = txq->vlan_ins = 0;
2135 txq->mapping_err = 0;
2136 return 0;
2137}
2138
2139int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
2140 struct net_device *dev, unsigned int iqid,
2141 unsigned int cmplqid)
2142{
2143 int ret, nentries;
2144 struct fw_eq_ctrl_cmd c;
2145 struct port_info *pi = netdev_priv(dev);
2146
2147
2148 nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
2149
2150 txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
2151 sizeof(struct tx_desc), 0, &txq->q.phys_addr,
2152 NULL, 0, NUMA_NO_NODE);
2153 if (!txq->q.desc)
2154 return -ENOMEM;
2155
2156 c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
2157 FW_CMD_WRITE | FW_CMD_EXEC |
2158 FW_EQ_CTRL_CMD_PFN(adap->fn) |
2159 FW_EQ_CTRL_CMD_VFN(0));
2160 c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC |
2161 FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
2162 c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID(cmplqid));
2163 c.physeqid_pkd = htonl(0);
2164 c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE(2) |
2165 FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
2166 FW_EQ_CTRL_CMD_FETCHRO |
2167 FW_EQ_CTRL_CMD_IQID(iqid));
2168 c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN(2) |
2169 FW_EQ_CTRL_CMD_FBMAX(3) |
2170 FW_EQ_CTRL_CMD_CIDXFTHRESH(5) |
2171 FW_EQ_CTRL_CMD_EQSIZE(nentries));
2172 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2173
2174 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2175 if (ret) {
2176 dma_free_coherent(adap->pdev_dev,
2177 nentries * sizeof(struct tx_desc),
2178 txq->q.desc, txq->q.phys_addr);
2179 txq->q.desc = NULL;
2180 return ret;
2181 }
2182
2183 init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_GET(ntohl(c.cmpliqid_eqid)));
2184 txq->adap = adap;
2185 skb_queue_head_init(&txq->sendq);
2186 tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
2187 txq->full = 0;
2188 return 0;
2189}
2190
2191int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
2192 struct net_device *dev, unsigned int iqid)
2193{
2194 int ret, nentries;
2195 struct fw_eq_ofld_cmd c;
2196 struct port_info *pi = netdev_priv(dev);
2197
2198
2199 nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
2200
2201 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2202 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
2203 &txq->q.phys_addr, &txq->q.sdesc, STAT_LEN,
2204 NUMA_NO_NODE);
2205 if (!txq->q.desc)
2206 return -ENOMEM;
2207
2208 memset(&c, 0, sizeof(c));
2209 c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
2210 FW_CMD_WRITE | FW_CMD_EXEC |
2211 FW_EQ_OFLD_CMD_PFN(adap->fn) |
2212 FW_EQ_OFLD_CMD_VFN(0));
2213 c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC |
2214 FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
2215 c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE(2) |
2216 FW_EQ_OFLD_CMD_PCIECHN(pi->tx_chan) |
2217 FW_EQ_OFLD_CMD_FETCHRO(1) |
2218 FW_EQ_OFLD_CMD_IQID(iqid));
2219 c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN(2) |
2220 FW_EQ_OFLD_CMD_FBMAX(3) |
2221 FW_EQ_OFLD_CMD_CIDXFTHRESH(5) |
2222 FW_EQ_OFLD_CMD_EQSIZE(nentries));
2223 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2224
2225 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2226 if (ret) {
2227 kfree(txq->q.sdesc);
2228 txq->q.sdesc = NULL;
2229 dma_free_coherent(adap->pdev_dev,
2230 nentries * sizeof(struct tx_desc),
2231 txq->q.desc, txq->q.phys_addr);
2232 txq->q.desc = NULL;
2233 return ret;
2234 }
2235
2236 init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_GET(ntohl(c.eqid_pkd)));
2237 txq->adap = adap;
2238 skb_queue_head_init(&txq->sendq);
2239 tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
2240 txq->full = 0;
2241 txq->mapping_err = 0;
2242 return 0;
2243}
2244
2245static void free_txq(struct adapter *adap, struct sge_txq *q)
2246{
2247 dma_free_coherent(adap->pdev_dev,
2248 q->size * sizeof(struct tx_desc) + STAT_LEN,
2249 q->desc, q->phys_addr);
2250 q->cntxt_id = 0;
2251 q->sdesc = NULL;
2252 q->desc = NULL;
2253}
2254
2255static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
2256 struct sge_fl *fl)
2257{
2258 unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
2259
2260 adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
2261 t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
2262 rq->cntxt_id, fl_id, 0xffff);
2263 dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
2264 rq->desc, rq->phys_addr);
2265 netif_napi_del(&rq->napi);
2266 rq->netdev = NULL;
2267 rq->cntxt_id = rq->abs_id = 0;
2268 rq->desc = NULL;
2269
2270 if (fl) {
2271 free_rx_bufs(adap, fl, fl->avail);
2272 dma_free_coherent(adap->pdev_dev, fl->size * 8 + STAT_LEN,
2273 fl->desc, fl->addr);
2274 kfree(fl->sdesc);
2275 fl->sdesc = NULL;
2276 fl->cntxt_id = 0;
2277 fl->desc = NULL;
2278 }
2279}
2280
2281
2282
2283
2284
2285
2286
2287void t4_free_sge_resources(struct adapter *adap)
2288{
2289 int i;
2290 struct sge_eth_rxq *eq = adap->sge.ethrxq;
2291 struct sge_eth_txq *etq = adap->sge.ethtxq;
2292 struct sge_ofld_rxq *oq = adap->sge.ofldrxq;
2293
2294
2295 for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
2296 if (eq->rspq.desc)
2297 free_rspq_fl(adap, &eq->rspq, &eq->fl);
2298 if (etq->q.desc) {
2299 t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
2300 etq->q.cntxt_id);
2301 free_tx_desc(adap, &etq->q, etq->q.in_use, true);
2302 kfree(etq->q.sdesc);
2303 free_txq(adap, &etq->q);
2304 }
2305 }
2306
2307
2308 for (i = 0; i < adap->sge.ofldqsets; i++, oq++) {
2309 if (oq->rspq.desc)
2310 free_rspq_fl(adap, &oq->rspq, &oq->fl);
2311 }
2312 for (i = 0, oq = adap->sge.rdmarxq; i < adap->sge.rdmaqs; i++, oq++) {
2313 if (oq->rspq.desc)
2314 free_rspq_fl(adap, &oq->rspq, &oq->fl);
2315 }
2316
2317
2318 for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
2319 struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
2320
2321 if (q->q.desc) {
2322 tasklet_kill(&q->qresume_tsk);
2323 t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
2324 q->q.cntxt_id);
2325 free_tx_desc(adap, &q->q, q->q.in_use, false);
2326 kfree(q->q.sdesc);
2327 __skb_queue_purge(&q->sendq);
2328 free_txq(adap, &q->q);
2329 }
2330 }
2331
2332
2333 for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
2334 struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
2335
2336 if (cq->q.desc) {
2337 tasklet_kill(&cq->qresume_tsk);
2338 t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
2339 cq->q.cntxt_id);
2340 __skb_queue_purge(&cq->sendq);
2341 free_txq(adap, &cq->q);
2342 }
2343 }
2344
2345 if (adap->sge.fw_evtq.desc)
2346 free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
2347
2348 if (adap->sge.intrq.desc)
2349 free_rspq_fl(adap, &adap->sge.intrq, NULL);
2350
2351
2352 memset(adap->sge.egr_map, 0, sizeof(adap->sge.egr_map));
2353}
2354
2355void t4_sge_start(struct adapter *adap)
2356{
2357 adap->sge.ethtxq_rover = 0;
2358 mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2359 mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2360}
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370void t4_sge_stop(struct adapter *adap)
2371{
2372 int i;
2373 struct sge *s = &adap->sge;
2374
2375 if (in_interrupt())
2376 return;
2377
2378 if (s->rx_timer.function)
2379 del_timer_sync(&s->rx_timer);
2380 if (s->tx_timer.function)
2381 del_timer_sync(&s->tx_timer);
2382
2383 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
2384 struct sge_ofld_txq *q = &s->ofldtxq[i];
2385
2386 if (q->q.desc)
2387 tasklet_kill(&q->qresume_tsk);
2388 }
2389 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
2390 struct sge_ctrl_txq *cq = &s->ctrlq[i];
2391
2392 if (cq->q.desc)
2393 tasklet_kill(&cq->qresume_tsk);
2394 }
2395}
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405void t4_sge_init(struct adapter *adap)
2406{
2407 unsigned int i, v;
2408 struct sge *s = &adap->sge;
2409 unsigned int fl_align_log = ilog2(FL_ALIGN);
2410
2411 t4_set_reg_field(adap, SGE_CONTROL, PKTSHIFT_MASK |
2412 INGPADBOUNDARY_MASK | EGRSTATUSPAGESIZE,
2413 INGPADBOUNDARY(fl_align_log - 5) | PKTSHIFT(2) |
2414 RXPKTCPLMODE |
2415 (STAT_LEN == 128 ? EGRSTATUSPAGESIZE : 0));
2416
2417 for (i = v = 0; i < 32; i += 4)
2418 v |= (PAGE_SHIFT - 10) << i;
2419 t4_write_reg(adap, SGE_HOST_PAGE_SIZE, v);
2420 t4_write_reg(adap, SGE_FL_BUFFER_SIZE0, PAGE_SIZE);
2421#if FL_PG_ORDER > 0
2422 t4_write_reg(adap, SGE_FL_BUFFER_SIZE1, PAGE_SIZE << FL_PG_ORDER);
2423#endif
2424 t4_write_reg(adap, SGE_INGRESS_RX_THRESHOLD,
2425 THRESHOLD_0(s->counter_val[0]) |
2426 THRESHOLD_1(s->counter_val[1]) |
2427 THRESHOLD_2(s->counter_val[2]) |
2428 THRESHOLD_3(s->counter_val[3]));
2429 t4_write_reg(adap, SGE_TIMER_VALUE_0_AND_1,
2430 TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[0])) |
2431 TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[1])));
2432 t4_write_reg(adap, SGE_TIMER_VALUE_2_AND_3,
2433 TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[2])) |
2434 TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[3])));
2435 t4_write_reg(adap, SGE_TIMER_VALUE_4_AND_5,
2436 TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[4])) |
2437 TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[5])));
2438 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
2439 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
2440 s->starve_thres = core_ticks_per_usec(adap) * 1000000;
2441 s->idma_state[0] = s->idma_state[1] = 0;
2442 spin_lock_init(&s->intrq_lock);
2443}
2444