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
36
37
38
39#include "common.h"
40
41#include <linux/types.h>
42#include <linux/errno.h>
43#include <linux/pci.h>
44#include <linux/ktime.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/if_vlan.h>
48#include <linux/skbuff.h>
49#include <linux/mm.h>
50#include <linux/tcp.h>
51#include <linux/ip.h>
52#include <linux/in.h>
53#include <linux/if_arp.h>
54#include <linux/slab.h>
55#include <linux/prefetch.h>
56
57#include "cpl5_cmd.h"
58#include "sge.h"
59#include "regs.h"
60#include "espi.h"
61
62
63#define ETH_P_CPL5 0xf
64
65#define SGE_CMDQ_N 2
66#define SGE_FREELQ_N 2
67#define SGE_CMDQ0_E_N 1024
68#define SGE_CMDQ1_E_N 128
69#define SGE_FREEL_SIZE 4096
70#define SGE_JUMBO_FREEL_SIZE 512
71#define SGE_FREEL_REFILL_THRESH 16
72#define SGE_RESPQ_E_N 1024
73#define SGE_INTRTIMER_NRES 1000
74#define SGE_RX_SM_BUF_SIZE 1536
75#define SGE_TX_DESC_MAX_PLEN 16384
76
77#define SGE_RESPQ_REPLENISH_THRES (SGE_RESPQ_E_N / 4)
78
79
80
81
82
83#define TX_RECLAIM_PERIOD (HZ / 4)
84
85#define M_CMD_LEN 0x7fffffff
86#define V_CMD_LEN(v) (v)
87#define G_CMD_LEN(v) ((v) & M_CMD_LEN)
88#define V_CMD_GEN1(v) ((v) << 31)
89#define V_CMD_GEN2(v) (v)
90#define F_CMD_DATAVALID (1 << 1)
91#define F_CMD_SOP (1 << 2)
92#define V_CMD_EOP(v) ((v) << 3)
93
94
95
96
97#if defined(__BIG_ENDIAN_BITFIELD)
98struct cmdQ_e {
99 u32 addr_lo;
100 u32 len_gen;
101 u32 flags;
102 u32 addr_hi;
103};
104
105struct freelQ_e {
106 u32 addr_lo;
107 u32 len_gen;
108 u32 gen2;
109 u32 addr_hi;
110};
111
112struct respQ_e {
113 u32 Qsleeping : 4;
114 u32 Cmdq1CreditReturn : 5;
115 u32 Cmdq1DmaComplete : 5;
116 u32 Cmdq0CreditReturn : 5;
117 u32 Cmdq0DmaComplete : 5;
118 u32 FreelistQid : 2;
119 u32 CreditValid : 1;
120 u32 DataValid : 1;
121 u32 Offload : 1;
122 u32 Eop : 1;
123 u32 Sop : 1;
124 u32 GenerationBit : 1;
125 u32 BufferLength;
126};
127#elif defined(__LITTLE_ENDIAN_BITFIELD)
128struct cmdQ_e {
129 u32 len_gen;
130 u32 addr_lo;
131 u32 addr_hi;
132 u32 flags;
133};
134
135struct freelQ_e {
136 u32 len_gen;
137 u32 addr_lo;
138 u32 addr_hi;
139 u32 gen2;
140};
141
142struct respQ_e {
143 u32 BufferLength;
144 u32 GenerationBit : 1;
145 u32 Sop : 1;
146 u32 Eop : 1;
147 u32 Offload : 1;
148 u32 DataValid : 1;
149 u32 CreditValid : 1;
150 u32 FreelistQid : 2;
151 u32 Cmdq0DmaComplete : 5;
152 u32 Cmdq0CreditReturn : 5;
153 u32 Cmdq1DmaComplete : 5;
154 u32 Cmdq1CreditReturn : 5;
155 u32 Qsleeping : 4;
156} ;
157#endif
158
159
160
161
162struct cmdQ_ce {
163 struct sk_buff *skb;
164 DEFINE_DMA_UNMAP_ADDR(dma_addr);
165 DEFINE_DMA_UNMAP_LEN(dma_len);
166};
167
168struct freelQ_ce {
169 struct sk_buff *skb;
170 DEFINE_DMA_UNMAP_ADDR(dma_addr);
171 DEFINE_DMA_UNMAP_LEN(dma_len);
172};
173
174
175
176
177struct cmdQ {
178 unsigned long status;
179 unsigned int in_use;
180 unsigned int size;
181 unsigned int processed;
182 unsigned int cleaned;
183 unsigned int stop_thres;
184 u16 pidx;
185 u16 cidx;
186 u8 genbit;
187 u8 sop;
188 struct cmdQ_e *entries;
189 struct cmdQ_ce *centries;
190 dma_addr_t dma_addr;
191 spinlock_t lock;
192};
193
194struct freelQ {
195 unsigned int credits;
196 unsigned int size;
197 u16 pidx;
198 u16 cidx;
199 u16 rx_buffer_size;
200 u16 dma_offset;
201 u16 recycleq_idx;
202 u8 genbit;
203 struct freelQ_e *entries;
204 struct freelQ_ce *centries;
205 dma_addr_t dma_addr;
206};
207
208struct respQ {
209 unsigned int credits;
210 unsigned int size;
211 u16 cidx;
212 u8 genbit;
213 struct respQ_e *entries;
214 dma_addr_t dma_addr;
215};
216
217
218enum {
219 CMDQ_STAT_RUNNING = 1,
220 CMDQ_STAT_LAST_PKT_DB = 2
221};
222
223
224
225
226struct sched_port {
227 unsigned int avail;
228 unsigned int drain_bits_per_1024ns;
229 unsigned int speed;
230 unsigned int mtu;
231 struct sk_buff_head skbq;
232};
233
234
235struct sched {
236 ktime_t last_updated;
237 unsigned int max_avail;
238 unsigned int port;
239 unsigned int num;
240 struct sched_port p[MAX_NPORTS];
241 struct tasklet_struct sched_tsk;
242 struct sge *sge;
243};
244
245static void restart_sched(struct tasklet_struct *t);
246
247
248
249
250
251
252
253
254
255
256struct sge {
257 struct adapter *adapter;
258 struct net_device *netdev;
259 struct freelQ freelQ[SGE_FREELQ_N];
260 struct respQ respQ;
261 unsigned long stopped_tx_queues;
262 unsigned int rx_pkt_pad;
263 unsigned int jumbo_fl;
264 unsigned int intrtimer_nres;
265 unsigned int fixed_intrtimer;
266 struct timer_list tx_reclaim_timer;
267 struct timer_list espibug_timer;
268 unsigned long espibug_timeout;
269 struct sk_buff *espibug_skb[MAX_NPORTS];
270 u32 sge_control;
271 struct sge_intr_counts stats;
272 struct sge_port_stats __percpu *port_stats[MAX_NPORTS];
273 struct sched *tx_sched;
274 struct cmdQ cmdQ[SGE_CMDQ_N] ____cacheline_aligned_in_smp;
275};
276
277static const u8 ch_mac_addr[ETH_ALEN] = {
278 0x0, 0x7, 0x43, 0x0, 0x0, 0x0
279};
280
281
282
283
284static void tx_sched_stop(struct sge *sge)
285{
286 struct sched *s = sge->tx_sched;
287 int i;
288
289 tasklet_kill(&s->sched_tsk);
290
291 for (i = 0; i < MAX_NPORTS; i++)
292 __skb_queue_purge(&s->p[s->port].skbq);
293}
294
295
296
297
298
299unsigned int t1_sched_update_parms(struct sge *sge, unsigned int port,
300 unsigned int mtu, unsigned int speed)
301{
302 struct sched *s = sge->tx_sched;
303 struct sched_port *p = &s->p[port];
304 unsigned int max_avail_segs;
305
306 pr_debug("%s mtu=%d speed=%d\n", __func__, mtu, speed);
307 if (speed)
308 p->speed = speed;
309 if (mtu)
310 p->mtu = mtu;
311
312 if (speed || mtu) {
313 unsigned long long drain = 1024ULL * p->speed * (p->mtu - 40);
314 do_div(drain, (p->mtu + 50) * 1000);
315 p->drain_bits_per_1024ns = (unsigned int) drain;
316
317 if (p->speed < 1000)
318 p->drain_bits_per_1024ns =
319 90 * p->drain_bits_per_1024ns / 100;
320 }
321
322 if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204) {
323 p->drain_bits_per_1024ns -= 16;
324 s->max_avail = max(4096U, p->mtu + 16 + 14 + 4);
325 max_avail_segs = max(1U, 4096 / (p->mtu - 40));
326 } else {
327 s->max_avail = 16384;
328 max_avail_segs = max(1U, 9000 / (p->mtu - 40));
329 }
330
331 pr_debug("t1_sched_update_parms: mtu %u speed %u max_avail %u "
332 "max_avail_segs %u drain_bits_per_1024ns %u\n", p->mtu,
333 p->speed, s->max_avail, max_avail_segs,
334 p->drain_bits_per_1024ns);
335
336 return max_avail_segs * (p->mtu - 40);
337}
338
339#if 0
340
341
342
343
344
345void t1_sched_set_max_avail_bytes(struct sge *sge, unsigned int val)
346{
347 struct sched *s = sge->tx_sched;
348 unsigned int i;
349
350 s->max_avail = val;
351 for (i = 0; i < MAX_NPORTS; i++)
352 t1_sched_update_parms(sge, i, 0, 0);
353}
354
355
356
357
358
359void t1_sched_set_drain_bits_per_us(struct sge *sge, unsigned int port,
360 unsigned int val)
361{
362 struct sched *s = sge->tx_sched;
363 struct sched_port *p = &s->p[port];
364 p->drain_bits_per_1024ns = val * 1024 / 1000;
365 t1_sched_update_parms(sge, port, 0, 0);
366}
367
368#endif
369
370
371
372
373static int tx_sched_init(struct sge *sge)
374{
375 struct sched *s;
376 int i;
377
378 s = kzalloc(sizeof (struct sched), GFP_KERNEL);
379 if (!s)
380 return -ENOMEM;
381
382 pr_debug("tx_sched_init\n");
383 tasklet_setup(&s->sched_tsk, restart_sched);
384 s->sge = sge;
385 sge->tx_sched = s;
386
387 for (i = 0; i < MAX_NPORTS; i++) {
388 skb_queue_head_init(&s->p[i].skbq);
389 t1_sched_update_parms(sge, i, 1500, 1000);
390 }
391
392 return 0;
393}
394
395
396
397
398
399
400static inline int sched_update_avail(struct sge *sge)
401{
402 struct sched *s = sge->tx_sched;
403 ktime_t now = ktime_get();
404 unsigned int i;
405 long long delta_time_ns;
406
407 delta_time_ns = ktime_to_ns(ktime_sub(now, s->last_updated));
408
409 pr_debug("sched_update_avail delta=%lld\n", delta_time_ns);
410 if (delta_time_ns < 15000)
411 return 0;
412
413 for (i = 0; i < MAX_NPORTS; i++) {
414 struct sched_port *p = &s->p[i];
415 unsigned int delta_avail;
416
417 delta_avail = (p->drain_bits_per_1024ns * delta_time_ns) >> 13;
418 p->avail = min(p->avail + delta_avail, s->max_avail);
419 }
420
421 s->last_updated = now;
422
423 return 1;
424}
425
426
427
428
429
430
431
432
433
434static struct sk_buff *sched_skb(struct sge *sge, struct sk_buff *skb,
435 unsigned int credits)
436{
437 struct sched *s = sge->tx_sched;
438 struct sk_buff_head *skbq;
439 unsigned int i, len, update = 1;
440
441 pr_debug("sched_skb %p\n", skb);
442 if (!skb) {
443 if (!s->num)
444 return NULL;
445 } else {
446 skbq = &s->p[skb->dev->if_port].skbq;
447 __skb_queue_tail(skbq, skb);
448 s->num++;
449 skb = NULL;
450 }
451
452 if (credits < MAX_SKB_FRAGS + 1)
453 goto out;
454
455again:
456 for (i = 0; i < MAX_NPORTS; i++) {
457 s->port = (s->port + 1) & (MAX_NPORTS - 1);
458 skbq = &s->p[s->port].skbq;
459
460 skb = skb_peek(skbq);
461
462 if (!skb)
463 continue;
464
465 len = skb->len;
466 if (len <= s->p[s->port].avail) {
467 s->p[s->port].avail -= len;
468 s->num--;
469 __skb_unlink(skb, skbq);
470 goto out;
471 }
472 skb = NULL;
473 }
474
475 if (update-- && sched_update_avail(sge))
476 goto again;
477
478out:
479
480
481
482 if (s->num && !skb) {
483 struct cmdQ *q = &sge->cmdQ[0];
484 clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
485 if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
486 set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
487 writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
488 }
489 }
490 pr_debug("sched_skb ret %p\n", skb);
491
492 return skb;
493}
494
495
496
497
498static inline void doorbell_pio(struct adapter *adapter, u32 val)
499{
500 wmb();
501 writel(val, adapter->regs + A_SG_DOORBELL);
502}
503
504
505
506
507
508static void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *q)
509{
510 unsigned int cidx = q->cidx;
511
512 while (q->credits--) {
513 struct freelQ_ce *ce = &q->centries[cidx];
514
515 dma_unmap_single(&pdev->dev, dma_unmap_addr(ce, dma_addr),
516 dma_unmap_len(ce, dma_len), DMA_FROM_DEVICE);
517 dev_kfree_skb(ce->skb);
518 ce->skb = NULL;
519 if (++cidx == q->size)
520 cidx = 0;
521 }
522}
523
524
525
526
527static void free_rx_resources(struct sge *sge)
528{
529 struct pci_dev *pdev = sge->adapter->pdev;
530 unsigned int size, i;
531
532 if (sge->respQ.entries) {
533 size = sizeof(struct respQ_e) * sge->respQ.size;
534 dma_free_coherent(&pdev->dev, size, sge->respQ.entries,
535 sge->respQ.dma_addr);
536 }
537
538 for (i = 0; i < SGE_FREELQ_N; i++) {
539 struct freelQ *q = &sge->freelQ[i];
540
541 if (q->centries) {
542 free_freelQ_buffers(pdev, q);
543 kfree(q->centries);
544 }
545 if (q->entries) {
546 size = sizeof(struct freelQ_e) * q->size;
547 dma_free_coherent(&pdev->dev, size, q->entries,
548 q->dma_addr);
549 }
550 }
551}
552
553
554
555
556
557static int alloc_rx_resources(struct sge *sge, struct sge_params *p)
558{
559 struct pci_dev *pdev = sge->adapter->pdev;
560 unsigned int size, i;
561
562 for (i = 0; i < SGE_FREELQ_N; i++) {
563 struct freelQ *q = &sge->freelQ[i];
564
565 q->genbit = 1;
566 q->size = p->freelQ_size[i];
567 q->dma_offset = sge->rx_pkt_pad ? 0 : NET_IP_ALIGN;
568 size = sizeof(struct freelQ_e) * q->size;
569 q->entries = dma_alloc_coherent(&pdev->dev, size,
570 &q->dma_addr, GFP_KERNEL);
571 if (!q->entries)
572 goto err_no_mem;
573
574 size = sizeof(struct freelQ_ce) * q->size;
575 q->centries = kzalloc(size, GFP_KERNEL);
576 if (!q->centries)
577 goto err_no_mem;
578 }
579
580
581
582
583
584
585
586
587 sge->freelQ[!sge->jumbo_fl].rx_buffer_size = SGE_RX_SM_BUF_SIZE +
588 sizeof(struct cpl_rx_data) +
589 sge->freelQ[!sge->jumbo_fl].dma_offset;
590
591 size = (16 * 1024) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
592
593 sge->freelQ[sge->jumbo_fl].rx_buffer_size = size;
594
595
596
597
598
599 sge->freelQ[!sge->jumbo_fl].recycleq_idx = 0;
600 sge->freelQ[sge->jumbo_fl].recycleq_idx = 1;
601
602 sge->respQ.genbit = 1;
603 sge->respQ.size = SGE_RESPQ_E_N;
604 sge->respQ.credits = 0;
605 size = sizeof(struct respQ_e) * sge->respQ.size;
606 sge->respQ.entries =
607 dma_alloc_coherent(&pdev->dev, size, &sge->respQ.dma_addr,
608 GFP_KERNEL);
609 if (!sge->respQ.entries)
610 goto err_no_mem;
611 return 0;
612
613err_no_mem:
614 free_rx_resources(sge);
615 return -ENOMEM;
616}
617
618
619
620
621static void free_cmdQ_buffers(struct sge *sge, struct cmdQ *q, unsigned int n)
622{
623 struct cmdQ_ce *ce;
624 struct pci_dev *pdev = sge->adapter->pdev;
625 unsigned int cidx = q->cidx;
626
627 q->in_use -= n;
628 ce = &q->centries[cidx];
629 while (n--) {
630 if (likely(dma_unmap_len(ce, dma_len))) {
631 dma_unmap_single(&pdev->dev,
632 dma_unmap_addr(ce, dma_addr),
633 dma_unmap_len(ce, dma_len),
634 DMA_TO_DEVICE);
635 if (q->sop)
636 q->sop = 0;
637 }
638 if (ce->skb) {
639 dev_kfree_skb_any(ce->skb);
640 q->sop = 1;
641 }
642 ce++;
643 if (++cidx == q->size) {
644 cidx = 0;
645 ce = q->centries;
646 }
647 }
648 q->cidx = cidx;
649}
650
651
652
653
654
655
656static void free_tx_resources(struct sge *sge)
657{
658 struct pci_dev *pdev = sge->adapter->pdev;
659 unsigned int size, i;
660
661 for (i = 0; i < SGE_CMDQ_N; i++) {
662 struct cmdQ *q = &sge->cmdQ[i];
663
664 if (q->centries) {
665 if (q->in_use)
666 free_cmdQ_buffers(sge, q, q->in_use);
667 kfree(q->centries);
668 }
669 if (q->entries) {
670 size = sizeof(struct cmdQ_e) * q->size;
671 dma_free_coherent(&pdev->dev, size, q->entries,
672 q->dma_addr);
673 }
674 }
675}
676
677
678
679
680static int alloc_tx_resources(struct sge *sge, struct sge_params *p)
681{
682 struct pci_dev *pdev = sge->adapter->pdev;
683 unsigned int size, i;
684
685 for (i = 0; i < SGE_CMDQ_N; i++) {
686 struct cmdQ *q = &sge->cmdQ[i];
687
688 q->genbit = 1;
689 q->sop = 1;
690 q->size = p->cmdQ_size[i];
691 q->in_use = 0;
692 q->status = 0;
693 q->processed = q->cleaned = 0;
694 q->stop_thres = 0;
695 spin_lock_init(&q->lock);
696 size = sizeof(struct cmdQ_e) * q->size;
697 q->entries = dma_alloc_coherent(&pdev->dev, size,
698 &q->dma_addr, GFP_KERNEL);
699 if (!q->entries)
700 goto err_no_mem;
701
702 size = sizeof(struct cmdQ_ce) * q->size;
703 q->centries = kzalloc(size, GFP_KERNEL);
704 if (!q->centries)
705 goto err_no_mem;
706 }
707
708
709
710
711
712
713
714
715 sge->cmdQ[0].stop_thres = sge->adapter->params.nports *
716 (MAX_SKB_FRAGS + 1);
717 return 0;
718
719err_no_mem:
720 free_tx_resources(sge);
721 return -ENOMEM;
722}
723
724static inline void setup_ring_params(struct adapter *adapter, u64 addr,
725 u32 size, int base_reg_lo,
726 int base_reg_hi, int size_reg)
727{
728 writel((u32)addr, adapter->regs + base_reg_lo);
729 writel(addr >> 32, adapter->regs + base_reg_hi);
730 writel(size, adapter->regs + size_reg);
731}
732
733
734
735
736void t1_vlan_mode(struct adapter *adapter, netdev_features_t features)
737{
738 struct sge *sge = adapter->sge;
739
740 if (features & NETIF_F_HW_VLAN_CTAG_RX)
741 sge->sge_control |= F_VLAN_XTRACT;
742 else
743 sge->sge_control &= ~F_VLAN_XTRACT;
744 if (adapter->open_device_map) {
745 writel(sge->sge_control, adapter->regs + A_SG_CONTROL);
746 readl(adapter->regs + A_SG_CONTROL);
747 }
748}
749
750
751
752
753
754static void configure_sge(struct sge *sge, struct sge_params *p)
755{
756 struct adapter *ap = sge->adapter;
757
758 writel(0, ap->regs + A_SG_CONTROL);
759 setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
760 A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
761 setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
762 A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
763 setup_ring_params(ap, sge->freelQ[0].dma_addr,
764 sge->freelQ[0].size, A_SG_FL0BASELWR,
765 A_SG_FL0BASEUPR, A_SG_FL0SIZE);
766 setup_ring_params(ap, sge->freelQ[1].dma_addr,
767 sge->freelQ[1].size, A_SG_FL1BASELWR,
768 A_SG_FL1BASEUPR, A_SG_FL1SIZE);
769
770
771 writel(SGE_RX_SM_BUF_SIZE + 1, ap->regs + A_SG_FLTHRESHOLD);
772
773 setup_ring_params(ap, sge->respQ.dma_addr, sge->respQ.size,
774 A_SG_RSPBASELWR, A_SG_RSPBASEUPR, A_SG_RSPSIZE);
775 writel((u32)sge->respQ.size - 1, ap->regs + A_SG_RSPQUEUECREDIT);
776
777 sge->sge_control = F_CMDQ0_ENABLE | F_CMDQ1_ENABLE | F_FL0_ENABLE |
778 F_FL1_ENABLE | F_CPL_ENABLE | F_RESPONSE_QUEUE_ENABLE |
779 V_CMDQ_PRIORITY(2) | F_DISABLE_CMDQ1_GTS | F_ISCSI_COALESCE |
780 V_RX_PKT_OFFSET(sge->rx_pkt_pad);
781
782#if defined(__BIG_ENDIAN_BITFIELD)
783 sge->sge_control |= F_ENABLE_BIG_ENDIAN;
784#endif
785
786
787 sge->intrtimer_nres = SGE_INTRTIMER_NRES * core_ticks_per_usec(ap);
788
789 t1_sge_set_coalesce_params(sge, p);
790}
791
792
793
794
795static inline unsigned int jumbo_payload_capacity(const struct sge *sge)
796{
797 return sge->freelQ[sge->jumbo_fl].rx_buffer_size -
798 sge->freelQ[sge->jumbo_fl].dma_offset -
799 sizeof(struct cpl_rx_data);
800}
801
802
803
804
805void t1_sge_destroy(struct sge *sge)
806{
807 int i;
808
809 for_each_port(sge->adapter, i)
810 free_percpu(sge->port_stats[i]);
811
812 kfree(sge->tx_sched);
813 free_tx_resources(sge);
814 free_rx_resources(sge);
815 kfree(sge);
816}
817
818
819
820
821
822
823
824
825
826
827
828
829
830static void refill_free_list(struct sge *sge, struct freelQ *q)
831{
832 struct pci_dev *pdev = sge->adapter->pdev;
833 struct freelQ_ce *ce = &q->centries[q->pidx];
834 struct freelQ_e *e = &q->entries[q->pidx];
835 unsigned int dma_len = q->rx_buffer_size - q->dma_offset;
836
837 while (q->credits < q->size) {
838 struct sk_buff *skb;
839 dma_addr_t mapping;
840
841 skb = dev_alloc_skb(q->rx_buffer_size);
842 if (!skb)
843 break;
844
845 skb_reserve(skb, q->dma_offset);
846 mapping = dma_map_single(&pdev->dev, skb->data, dma_len,
847 DMA_FROM_DEVICE);
848 skb_reserve(skb, sge->rx_pkt_pad);
849
850 ce->skb = skb;
851 dma_unmap_addr_set(ce, dma_addr, mapping);
852 dma_unmap_len_set(ce, dma_len, dma_len);
853 e->addr_lo = (u32)mapping;
854 e->addr_hi = (u64)mapping >> 32;
855 e->len_gen = V_CMD_LEN(dma_len) | V_CMD_GEN1(q->genbit);
856 wmb();
857 e->gen2 = V_CMD_GEN2(q->genbit);
858
859 e++;
860 ce++;
861 if (++q->pidx == q->size) {
862 q->pidx = 0;
863 q->genbit ^= 1;
864 ce = q->centries;
865 e = q->entries;
866 }
867 q->credits++;
868 }
869}
870
871
872
873
874
875
876static void freelQs_empty(struct sge *sge)
877{
878 struct adapter *adapter = sge->adapter;
879 u32 irq_reg = readl(adapter->regs + A_SG_INT_ENABLE);
880 u32 irqholdoff_reg;
881
882 refill_free_list(sge, &sge->freelQ[0]);
883 refill_free_list(sge, &sge->freelQ[1]);
884
885 if (sge->freelQ[0].credits > (sge->freelQ[0].size >> 2) &&
886 sge->freelQ[1].credits > (sge->freelQ[1].size >> 2)) {
887 irq_reg |= F_FL_EXHAUSTED;
888 irqholdoff_reg = sge->fixed_intrtimer;
889 } else {
890
891 irq_reg &= ~F_FL_EXHAUSTED;
892 irqholdoff_reg = sge->intrtimer_nres;
893 }
894 writel(irqholdoff_reg, adapter->regs + A_SG_INTRTIMER);
895 writel(irq_reg, adapter->regs + A_SG_INT_ENABLE);
896
897
898 doorbell_pio(adapter, F_FL0_ENABLE | F_FL1_ENABLE);
899}
900
901#define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
902#define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
903#define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
904 F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
905
906
907
908
909void t1_sge_intr_disable(struct sge *sge)
910{
911 u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
912
913 writel(val & ~SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
914 writel(0, sge->adapter->regs + A_SG_INT_ENABLE);
915}
916
917
918
919
920void t1_sge_intr_enable(struct sge *sge)
921{
922 u32 en = SGE_INT_ENABLE;
923 u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
924
925 if (sge->adapter->port[0].dev->hw_features & NETIF_F_TSO)
926 en &= ~F_PACKET_TOO_BIG;
927 writel(en, sge->adapter->regs + A_SG_INT_ENABLE);
928 writel(val | SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
929}
930
931
932
933
934void t1_sge_intr_clear(struct sge *sge)
935{
936 writel(SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_CAUSE);
937 writel(0xffffffff, sge->adapter->regs + A_SG_INT_CAUSE);
938}
939
940
941
942
943int t1_sge_intr_error_handler(struct sge *sge)
944{
945 struct adapter *adapter = sge->adapter;
946 u32 cause = readl(adapter->regs + A_SG_INT_CAUSE);
947
948 if (adapter->port[0].dev->hw_features & NETIF_F_TSO)
949 cause &= ~F_PACKET_TOO_BIG;
950 if (cause & F_RESPQ_EXHAUSTED)
951 sge->stats.respQ_empty++;
952 if (cause & F_RESPQ_OVERFLOW) {
953 sge->stats.respQ_overflow++;
954 pr_alert("%s: SGE response queue overflow\n",
955 adapter->name);
956 }
957 if (cause & F_FL_EXHAUSTED) {
958 sge->stats.freelistQ_empty++;
959 freelQs_empty(sge);
960 }
961 if (cause & F_PACKET_TOO_BIG) {
962 sge->stats.pkt_too_big++;
963 pr_alert("%s: SGE max packet size exceeded\n",
964 adapter->name);
965 }
966 if (cause & F_PACKET_MISMATCH) {
967 sge->stats.pkt_mismatch++;
968 pr_alert("%s: SGE packet mismatch\n", adapter->name);
969 }
970 if (cause & SGE_INT_FATAL)
971 t1_fatal_err(adapter);
972
973 writel(cause, adapter->regs + A_SG_INT_CAUSE);
974 return 0;
975}
976
977const struct sge_intr_counts *t1_sge_get_intr_counts(const struct sge *sge)
978{
979 return &sge->stats;
980}
981
982void t1_sge_get_port_stats(const struct sge *sge, int port,
983 struct sge_port_stats *ss)
984{
985 int cpu;
986
987 memset(ss, 0, sizeof(*ss));
988 for_each_possible_cpu(cpu) {
989 struct sge_port_stats *st = per_cpu_ptr(sge->port_stats[port], cpu);
990
991 ss->rx_cso_good += st->rx_cso_good;
992 ss->tx_cso += st->tx_cso;
993 ss->tx_tso += st->tx_tso;
994 ss->tx_need_hdrroom += st->tx_need_hdrroom;
995 ss->vlan_xtract += st->vlan_xtract;
996 ss->vlan_insert += st->vlan_insert;
997 }
998}
999
1000
1001
1002
1003
1004
1005
1006
1007
1008static void recycle_fl_buf(struct freelQ *fl, int idx)
1009{
1010 struct freelQ_e *from = &fl->entries[idx];
1011 struct freelQ_e *to = &fl->entries[fl->pidx];
1012
1013 fl->centries[fl->pidx] = fl->centries[idx];
1014 to->addr_lo = from->addr_lo;
1015 to->addr_hi = from->addr_hi;
1016 to->len_gen = G_CMD_LEN(from->len_gen) | V_CMD_GEN1(fl->genbit);
1017 wmb();
1018 to->gen2 = V_CMD_GEN2(fl->genbit);
1019 fl->credits++;
1020
1021 if (++fl->pidx == fl->size) {
1022 fl->pidx = 0;
1023 fl->genbit ^= 1;
1024 }
1025}
1026
1027static int copybreak __read_mostly = 256;
1028module_param(copybreak, int, 0);
1029MODULE_PARM_DESC(copybreak, "Receive copy threshold");
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045static inline struct sk_buff *get_packet(struct adapter *adapter,
1046 struct freelQ *fl, unsigned int len)
1047{
1048 const struct freelQ_ce *ce = &fl->centries[fl->cidx];
1049 struct pci_dev *pdev = adapter->pdev;
1050 struct sk_buff *skb;
1051
1052 if (len < copybreak) {
1053 skb = napi_alloc_skb(&adapter->napi, len);
1054 if (!skb)
1055 goto use_orig_buf;
1056
1057 skb_put(skb, len);
1058 dma_sync_single_for_cpu(&pdev->dev,
1059 dma_unmap_addr(ce, dma_addr),
1060 dma_unmap_len(ce, dma_len),
1061 DMA_FROM_DEVICE);
1062 skb_copy_from_linear_data(ce->skb, skb->data, len);
1063 dma_sync_single_for_device(&pdev->dev,
1064 dma_unmap_addr(ce, dma_addr),
1065 dma_unmap_len(ce, dma_len),
1066 DMA_FROM_DEVICE);
1067 recycle_fl_buf(fl, fl->cidx);
1068 return skb;
1069 }
1070
1071use_orig_buf:
1072 if (fl->credits < 2) {
1073 recycle_fl_buf(fl, fl->cidx);
1074 return NULL;
1075 }
1076
1077 dma_unmap_single(&pdev->dev, dma_unmap_addr(ce, dma_addr),
1078 dma_unmap_len(ce, dma_len), DMA_FROM_DEVICE);
1079 skb = ce->skb;
1080 prefetch(skb->data);
1081
1082 skb_put(skb, len);
1083 return skb;
1084}
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095static void unexpected_offload(struct adapter *adapter, struct freelQ *fl)
1096{
1097 struct freelQ_ce *ce = &fl->centries[fl->cidx];
1098 struct sk_buff *skb = ce->skb;
1099
1100 dma_sync_single_for_cpu(&adapter->pdev->dev,
1101 dma_unmap_addr(ce, dma_addr),
1102 dma_unmap_len(ce, dma_len), DMA_FROM_DEVICE);
1103 pr_err("%s: unexpected offload packet, cmd %u\n",
1104 adapter->name, *skb->data);
1105 recycle_fl_buf(fl, fl->cidx);
1106}
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118static inline unsigned int compute_large_page_tx_descs(struct sk_buff *skb)
1119{
1120 unsigned int count = 0;
1121
1122 if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
1123 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
1124 unsigned int i, len = skb_headlen(skb);
1125 while (len > SGE_TX_DESC_MAX_PLEN) {
1126 count++;
1127 len -= SGE_TX_DESC_MAX_PLEN;
1128 }
1129 for (i = 0; nfrags--; i++) {
1130 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1131 len = skb_frag_size(frag);
1132 while (len > SGE_TX_DESC_MAX_PLEN) {
1133 count++;
1134 len -= SGE_TX_DESC_MAX_PLEN;
1135 }
1136 }
1137 }
1138 return count;
1139}
1140
1141
1142
1143
1144
1145
1146
1147static inline void write_tx_desc(struct cmdQ_e *e, dma_addr_t mapping,
1148 unsigned int len, unsigned int gen,
1149 unsigned int eop)
1150{
1151 BUG_ON(len > SGE_TX_DESC_MAX_PLEN);
1152
1153 e->addr_lo = (u32)mapping;
1154 e->addr_hi = (u64)mapping >> 32;
1155 e->len_gen = V_CMD_LEN(len) | V_CMD_GEN1(gen);
1156 e->flags = F_CMD_DATAVALID | V_CMD_EOP(eop) | V_CMD_GEN2(gen);
1157}
1158
1159
1160
1161
1162
1163
1164
1165static inline unsigned int write_large_page_tx_descs(unsigned int pidx,
1166 struct cmdQ_e **e,
1167 struct cmdQ_ce **ce,
1168 unsigned int *gen,
1169 dma_addr_t *desc_mapping,
1170 unsigned int *desc_len,
1171 unsigned int nfrags,
1172 struct cmdQ *q)
1173{
1174 if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
1175 struct cmdQ_e *e1 = *e;
1176 struct cmdQ_ce *ce1 = *ce;
1177
1178 while (*desc_len > SGE_TX_DESC_MAX_PLEN) {
1179 *desc_len -= SGE_TX_DESC_MAX_PLEN;
1180 write_tx_desc(e1, *desc_mapping, SGE_TX_DESC_MAX_PLEN,
1181 *gen, nfrags == 0 && *desc_len == 0);
1182 ce1->skb = NULL;
1183 dma_unmap_len_set(ce1, dma_len, 0);
1184 *desc_mapping += SGE_TX_DESC_MAX_PLEN;
1185 if (*desc_len) {
1186 ce1++;
1187 e1++;
1188 if (++pidx == q->size) {
1189 pidx = 0;
1190 *gen ^= 1;
1191 ce1 = q->centries;
1192 e1 = q->entries;
1193 }
1194 }
1195 }
1196 *e = e1;
1197 *ce = ce1;
1198 }
1199 return pidx;
1200}
1201
1202
1203
1204
1205
1206static inline void write_tx_descs(struct adapter *adapter, struct sk_buff *skb,
1207 unsigned int pidx, unsigned int gen,
1208 struct cmdQ *q)
1209{
1210 dma_addr_t mapping, desc_mapping;
1211 struct cmdQ_e *e, *e1;
1212 struct cmdQ_ce *ce;
1213 unsigned int i, flags, first_desc_len, desc_len,
1214 nfrags = skb_shinfo(skb)->nr_frags;
1215
1216 e = e1 = &q->entries[pidx];
1217 ce = &q->centries[pidx];
1218
1219 mapping = dma_map_single(&adapter->pdev->dev, skb->data,
1220 skb_headlen(skb), DMA_TO_DEVICE);
1221
1222 desc_mapping = mapping;
1223 desc_len = skb_headlen(skb);
1224
1225 flags = F_CMD_DATAVALID | F_CMD_SOP |
1226 V_CMD_EOP(nfrags == 0 && desc_len <= SGE_TX_DESC_MAX_PLEN) |
1227 V_CMD_GEN2(gen);
1228 first_desc_len = (desc_len <= SGE_TX_DESC_MAX_PLEN) ?
1229 desc_len : SGE_TX_DESC_MAX_PLEN;
1230 e->addr_lo = (u32)desc_mapping;
1231 e->addr_hi = (u64)desc_mapping >> 32;
1232 e->len_gen = V_CMD_LEN(first_desc_len) | V_CMD_GEN1(gen);
1233 ce->skb = NULL;
1234 dma_unmap_len_set(ce, dma_len, 0);
1235
1236 if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN &&
1237 desc_len > SGE_TX_DESC_MAX_PLEN) {
1238 desc_mapping += first_desc_len;
1239 desc_len -= first_desc_len;
1240 e1++;
1241 ce++;
1242 if (++pidx == q->size) {
1243 pidx = 0;
1244 gen ^= 1;
1245 e1 = q->entries;
1246 ce = q->centries;
1247 }
1248 pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
1249 &desc_mapping, &desc_len,
1250 nfrags, q);
1251
1252 if (likely(desc_len))
1253 write_tx_desc(e1, desc_mapping, desc_len, gen,
1254 nfrags == 0);
1255 }
1256
1257 ce->skb = NULL;
1258 dma_unmap_addr_set(ce, dma_addr, mapping);
1259 dma_unmap_len_set(ce, dma_len, skb_headlen(skb));
1260
1261 for (i = 0; nfrags--; i++) {
1262 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1263 e1++;
1264 ce++;
1265 if (++pidx == q->size) {
1266 pidx = 0;
1267 gen ^= 1;
1268 e1 = q->entries;
1269 ce = q->centries;
1270 }
1271
1272 mapping = skb_frag_dma_map(&adapter->pdev->dev, frag, 0,
1273 skb_frag_size(frag), DMA_TO_DEVICE);
1274 desc_mapping = mapping;
1275 desc_len = skb_frag_size(frag);
1276
1277 pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
1278 &desc_mapping, &desc_len,
1279 nfrags, q);
1280 if (likely(desc_len))
1281 write_tx_desc(e1, desc_mapping, desc_len, gen,
1282 nfrags == 0);
1283 ce->skb = NULL;
1284 dma_unmap_addr_set(ce, dma_addr, mapping);
1285 dma_unmap_len_set(ce, dma_len, skb_frag_size(frag));
1286 }
1287 ce->skb = skb;
1288 wmb();
1289 e->flags = flags;
1290}
1291
1292
1293
1294
1295static inline void reclaim_completed_tx(struct sge *sge, struct cmdQ *q)
1296{
1297 unsigned int reclaim = q->processed - q->cleaned;
1298
1299 if (reclaim) {
1300 pr_debug("reclaim_completed_tx processed:%d cleaned:%d\n",
1301 q->processed, q->cleaned);
1302 free_cmdQ_buffers(sge, q, reclaim);
1303 q->cleaned += reclaim;
1304 }
1305}
1306
1307
1308
1309
1310
1311static void restart_sched(struct tasklet_struct *t)
1312{
1313 struct sched *s = from_tasklet(s, t, sched_tsk);
1314 struct sge *sge = s->sge;
1315 struct adapter *adapter = sge->adapter;
1316 struct cmdQ *q = &sge->cmdQ[0];
1317 struct sk_buff *skb;
1318 unsigned int credits, queued_skb = 0;
1319
1320 spin_lock(&q->lock);
1321 reclaim_completed_tx(sge, q);
1322
1323 credits = q->size - q->in_use;
1324 pr_debug("restart_sched credits=%d\n", credits);
1325 while ((skb = sched_skb(sge, NULL, credits)) != NULL) {
1326 unsigned int genbit, pidx, count;
1327 count = 1 + skb_shinfo(skb)->nr_frags;
1328 count += compute_large_page_tx_descs(skb);
1329 q->in_use += count;
1330 genbit = q->genbit;
1331 pidx = q->pidx;
1332 q->pidx += count;
1333 if (q->pidx >= q->size) {
1334 q->pidx -= q->size;
1335 q->genbit ^= 1;
1336 }
1337 write_tx_descs(adapter, skb, pidx, genbit, q);
1338 credits = q->size - q->in_use;
1339 queued_skb = 1;
1340 }
1341
1342 if (queued_skb) {
1343 clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1344 if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
1345 set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1346 writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1347 }
1348 }
1349 spin_unlock(&q->lock);
1350}
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360static void sge_rx(struct sge *sge, struct freelQ *fl, unsigned int len)
1361{
1362 struct sk_buff *skb;
1363 const struct cpl_rx_pkt *p;
1364 struct adapter *adapter = sge->adapter;
1365 struct sge_port_stats *st;
1366 struct net_device *dev;
1367
1368 skb = get_packet(adapter, fl, len - sge->rx_pkt_pad);
1369 if (unlikely(!skb)) {
1370 sge->stats.rx_drops++;
1371 return;
1372 }
1373
1374 p = (const struct cpl_rx_pkt *) skb->data;
1375 if (p->iff >= adapter->params.nports) {
1376 kfree_skb(skb);
1377 return;
1378 }
1379 __skb_pull(skb, sizeof(*p));
1380
1381 st = this_cpu_ptr(sge->port_stats[p->iff]);
1382 dev = adapter->port[p->iff].dev;
1383
1384 skb->protocol = eth_type_trans(skb, dev);
1385 if ((dev->features & NETIF_F_RXCSUM) && p->csum == 0xffff &&
1386 skb->protocol == htons(ETH_P_IP) &&
1387 (skb->data[9] == IPPROTO_TCP || skb->data[9] == IPPROTO_UDP)) {
1388 ++st->rx_cso_good;
1389 skb->ip_summed = CHECKSUM_UNNECESSARY;
1390 } else
1391 skb_checksum_none_assert(skb);
1392
1393 if (p->vlan_valid) {
1394 st->vlan_xtract++;
1395 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(p->vlan));
1396 }
1397 netif_receive_skb(skb);
1398}
1399
1400
1401
1402
1403
1404static inline int enough_free_Tx_descs(const struct cmdQ *q)
1405{
1406 unsigned int r = q->processed - q->cleaned;
1407
1408 return q->in_use - r < (q->size >> 1);
1409}
1410
1411
1412
1413
1414
1415static void restart_tx_queues(struct sge *sge)
1416{
1417 struct adapter *adap = sge->adapter;
1418 int i;
1419
1420 if (!enough_free_Tx_descs(&sge->cmdQ[0]))
1421 return;
1422
1423 for_each_port(adap, i) {
1424 struct net_device *nd = adap->port[i].dev;
1425
1426 if (test_and_clear_bit(nd->if_port, &sge->stopped_tx_queues) &&
1427 netif_running(nd)) {
1428 sge->stats.cmdQ_restarted[2]++;
1429 netif_wake_queue(nd);
1430 }
1431 }
1432}
1433
1434
1435
1436
1437
1438static unsigned int update_tx_info(struct adapter *adapter,
1439 unsigned int flags,
1440 unsigned int pr0)
1441{
1442 struct sge *sge = adapter->sge;
1443 struct cmdQ *cmdq = &sge->cmdQ[0];
1444
1445 cmdq->processed += pr0;
1446 if (flags & (F_FL0_ENABLE | F_FL1_ENABLE)) {
1447 freelQs_empty(sge);
1448 flags &= ~(F_FL0_ENABLE | F_FL1_ENABLE);
1449 }
1450 if (flags & F_CMDQ0_ENABLE) {
1451 clear_bit(CMDQ_STAT_RUNNING, &cmdq->status);
1452
1453 if (cmdq->cleaned + cmdq->in_use != cmdq->processed &&
1454 !test_and_set_bit(CMDQ_STAT_LAST_PKT_DB, &cmdq->status)) {
1455 set_bit(CMDQ_STAT_RUNNING, &cmdq->status);
1456 writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1457 }
1458 if (sge->tx_sched)
1459 tasklet_hi_schedule(&sge->tx_sched->sched_tsk);
1460
1461 flags &= ~F_CMDQ0_ENABLE;
1462 }
1463
1464 if (unlikely(sge->stopped_tx_queues != 0))
1465 restart_tx_queues(sge);
1466
1467 return flags;
1468}
1469
1470
1471
1472
1473
1474static int process_responses(struct adapter *adapter, int budget)
1475{
1476 struct sge *sge = adapter->sge;
1477 struct respQ *q = &sge->respQ;
1478 struct respQ_e *e = &q->entries[q->cidx];
1479 int done = 0;
1480 unsigned int flags = 0;
1481 unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1482
1483 while (done < budget && e->GenerationBit == q->genbit) {
1484 flags |= e->Qsleeping;
1485
1486 cmdq_processed[0] += e->Cmdq0CreditReturn;
1487 cmdq_processed[1] += e->Cmdq1CreditReturn;
1488
1489
1490
1491
1492
1493 if (unlikely((flags & F_CMDQ0_ENABLE) || cmdq_processed[0] > 64)) {
1494 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1495 cmdq_processed[0] = 0;
1496 }
1497
1498 if (unlikely(cmdq_processed[1] > 16)) {
1499 sge->cmdQ[1].processed += cmdq_processed[1];
1500 cmdq_processed[1] = 0;
1501 }
1502
1503 if (likely(e->DataValid)) {
1504 struct freelQ *fl = &sge->freelQ[e->FreelistQid];
1505
1506 BUG_ON(!e->Sop || !e->Eop);
1507 if (unlikely(e->Offload))
1508 unexpected_offload(adapter, fl);
1509 else
1510 sge_rx(sge, fl, e->BufferLength);
1511
1512 ++done;
1513
1514
1515
1516
1517
1518 if (++fl->cidx == fl->size)
1519 fl->cidx = 0;
1520 prefetch(fl->centries[fl->cidx].skb);
1521
1522 if (unlikely(--fl->credits <
1523 fl->size - SGE_FREEL_REFILL_THRESH))
1524 refill_free_list(sge, fl);
1525 } else
1526 sge->stats.pure_rsps++;
1527
1528 e++;
1529 if (unlikely(++q->cidx == q->size)) {
1530 q->cidx = 0;
1531 q->genbit ^= 1;
1532 e = q->entries;
1533 }
1534 prefetch(e);
1535
1536 if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
1537 writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
1538 q->credits = 0;
1539 }
1540 }
1541
1542 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1543 sge->cmdQ[1].processed += cmdq_processed[1];
1544
1545 return done;
1546}
1547
1548static inline int responses_pending(const struct adapter *adapter)
1549{
1550 const struct respQ *Q = &adapter->sge->respQ;
1551 const struct respQ_e *e = &Q->entries[Q->cidx];
1552
1553 return e->GenerationBit == Q->genbit;
1554}
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564static int process_pure_responses(struct adapter *adapter)
1565{
1566 struct sge *sge = adapter->sge;
1567 struct respQ *q = &sge->respQ;
1568 struct respQ_e *e = &q->entries[q->cidx];
1569 const struct freelQ *fl = &sge->freelQ[e->FreelistQid];
1570 unsigned int flags = 0;
1571 unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1572
1573 prefetch(fl->centries[fl->cidx].skb);
1574 if (e->DataValid)
1575 return 1;
1576
1577 do {
1578 flags |= e->Qsleeping;
1579
1580 cmdq_processed[0] += e->Cmdq0CreditReturn;
1581 cmdq_processed[1] += e->Cmdq1CreditReturn;
1582
1583 e++;
1584 if (unlikely(++q->cidx == q->size)) {
1585 q->cidx = 0;
1586 q->genbit ^= 1;
1587 e = q->entries;
1588 }
1589 prefetch(e);
1590
1591 if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
1592 writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
1593 q->credits = 0;
1594 }
1595 sge->stats.pure_rsps++;
1596 } while (e->GenerationBit == q->genbit && !e->DataValid);
1597
1598 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1599 sge->cmdQ[1].processed += cmdq_processed[1];
1600
1601 return e->GenerationBit == q->genbit;
1602}
1603
1604
1605
1606
1607
1608
1609int t1_poll(struct napi_struct *napi, int budget)
1610{
1611 struct adapter *adapter = container_of(napi, struct adapter, napi);
1612 int work_done = process_responses(adapter, budget);
1613
1614 if (likely(work_done < budget)) {
1615 napi_complete_done(napi, work_done);
1616 writel(adapter->sge->respQ.cidx,
1617 adapter->regs + A_SG_SLEEPING);
1618 }
1619 return work_done;
1620}
1621
1622irqreturn_t t1_interrupt(int irq, void *data)
1623{
1624 struct adapter *adapter = data;
1625 struct sge *sge = adapter->sge;
1626 int handled;
1627
1628 if (likely(responses_pending(adapter))) {
1629 writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
1630
1631 if (napi_schedule_prep(&adapter->napi)) {
1632 if (process_pure_responses(adapter))
1633 __napi_schedule(&adapter->napi);
1634 else {
1635
1636 writel(sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
1637
1638 napi_enable(&adapter->napi);
1639 }
1640 }
1641 return IRQ_HANDLED;
1642 }
1643
1644 spin_lock(&adapter->async_lock);
1645 handled = t1_slow_intr_handler(adapter);
1646 spin_unlock(&adapter->async_lock);
1647
1648 if (!handled)
1649 sge->stats.unhandled_irqs++;
1650
1651 return IRQ_RETVAL(handled != 0);
1652}
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
1668 unsigned int qid, struct net_device *dev)
1669{
1670 struct sge *sge = adapter->sge;
1671 struct cmdQ *q = &sge->cmdQ[qid];
1672 unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
1673
1674 spin_lock(&q->lock);
1675
1676 reclaim_completed_tx(sge, q);
1677
1678 pidx = q->pidx;
1679 credits = q->size - q->in_use;
1680 count = 1 + skb_shinfo(skb)->nr_frags;
1681 count += compute_large_page_tx_descs(skb);
1682
1683
1684 if (unlikely(credits < count)) {
1685 if (!netif_queue_stopped(dev)) {
1686 netif_stop_queue(dev);
1687 set_bit(dev->if_port, &sge->stopped_tx_queues);
1688 sge->stats.cmdQ_full[2]++;
1689 pr_err("%s: Tx ring full while queue awake!\n",
1690 adapter->name);
1691 }
1692 spin_unlock(&q->lock);
1693 return NETDEV_TX_BUSY;
1694 }
1695
1696 if (unlikely(credits - count < q->stop_thres)) {
1697 netif_stop_queue(dev);
1698 set_bit(dev->if_port, &sge->stopped_tx_queues);
1699 sge->stats.cmdQ_full[2]++;
1700 }
1701
1702
1703
1704
1705 if (sge->tx_sched && !qid && skb->dev) {
1706use_sched:
1707 use_sched_skb = 1;
1708
1709
1710
1711 skb = sched_skb(sge, skb, credits);
1712 if (!skb) {
1713 spin_unlock(&q->lock);
1714 return NETDEV_TX_OK;
1715 }
1716 pidx = q->pidx;
1717 count = 1 + skb_shinfo(skb)->nr_frags;
1718 count += compute_large_page_tx_descs(skb);
1719 }
1720
1721 q->in_use += count;
1722 genbit = q->genbit;
1723 pidx = q->pidx;
1724 q->pidx += count;
1725 if (q->pidx >= q->size) {
1726 q->pidx -= q->size;
1727 q->genbit ^= 1;
1728 }
1729 spin_unlock(&q->lock);
1730
1731 write_tx_descs(adapter, skb, pidx, genbit, q);
1732
1733
1734
1735
1736
1737
1738
1739
1740 if (qid)
1741 doorbell_pio(adapter, F_CMDQ1_ENABLE);
1742 else {
1743 clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1744 if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
1745 set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1746 writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1747 }
1748 }
1749
1750 if (use_sched_skb) {
1751 if (spin_trylock(&q->lock)) {
1752 credits = q->size - q->in_use;
1753 skb = NULL;
1754 goto use_sched;
1755 }
1756 }
1757 return NETDEV_TX_OK;
1758}
1759
1760#define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))
1761
1762
1763
1764
1765
1766
1767
1768static inline int eth_hdr_len(const void *data)
1769{
1770 const struct ethhdr *e = data;
1771
1772 return e->h_proto == htons(ETH_P_8021Q) ? VLAN_ETH_HLEN : ETH_HLEN;
1773}
1774
1775
1776
1777
1778netdev_tx_t t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
1779{
1780 struct adapter *adapter = dev->ml_priv;
1781 struct sge *sge = adapter->sge;
1782 struct sge_port_stats *st = this_cpu_ptr(sge->port_stats[dev->if_port]);
1783 struct cpl_tx_pkt *cpl;
1784 struct sk_buff *orig_skb = skb;
1785 int ret;
1786
1787 if (skb->protocol == htons(ETH_P_CPL5))
1788 goto send;
1789
1790
1791
1792
1793
1794 if (unlikely(skb_headroom(skb) < dev->hard_header_len - ETH_HLEN)) {
1795 skb = skb_realloc_headroom(skb, sizeof(struct cpl_tx_pkt_lso));
1796 ++st->tx_need_hdrroom;
1797 dev_kfree_skb_any(orig_skb);
1798 if (!skb)
1799 return NETDEV_TX_OK;
1800 }
1801
1802 if (skb_shinfo(skb)->gso_size) {
1803 int eth_type;
1804 struct cpl_tx_pkt_lso *hdr;
1805
1806 ++st->tx_tso;
1807
1808 eth_type = skb_network_offset(skb) == ETH_HLEN ?
1809 CPL_ETH_II : CPL_ETH_II_VLAN;
1810
1811 hdr = skb_push(skb, sizeof(*hdr));
1812 hdr->opcode = CPL_TX_PKT_LSO;
1813 hdr->ip_csum_dis = hdr->l4_csum_dis = 0;
1814 hdr->ip_hdr_words = ip_hdr(skb)->ihl;
1815 hdr->tcp_hdr_words = tcp_hdr(skb)->doff;
1816 hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
1817 skb_shinfo(skb)->gso_size));
1818 hdr->len = htonl(skb->len - sizeof(*hdr));
1819 cpl = (struct cpl_tx_pkt *)hdr;
1820 } else {
1821
1822
1823
1824
1825
1826
1827 if (unlikely(skb->len < ETH_HLEN ||
1828 skb->len > dev->mtu + eth_hdr_len(skb->data))) {
1829 netdev_dbg(dev, "packet size %d hdr %d mtu%d\n",
1830 skb->len, eth_hdr_len(skb->data), dev->mtu);
1831 dev_kfree_skb_any(skb);
1832 return NETDEV_TX_OK;
1833 }
1834
1835 if (skb->ip_summed == CHECKSUM_PARTIAL &&
1836 ip_hdr(skb)->protocol == IPPROTO_UDP) {
1837 if (unlikely(skb_checksum_help(skb))) {
1838 netdev_dbg(dev, "unable to do udp checksum\n");
1839 dev_kfree_skb_any(skb);
1840 return NETDEV_TX_OK;
1841 }
1842 }
1843
1844
1845
1846
1847 if ((unlikely(!adapter->sge->espibug_skb[dev->if_port]))) {
1848 if (skb->protocol == htons(ETH_P_ARP) &&
1849 arp_hdr(skb)->ar_op == htons(ARPOP_REQUEST)) {
1850 adapter->sge->espibug_skb[dev->if_port] = skb;
1851
1852
1853
1854
1855 skb = skb_get(skb);
1856 }
1857 }
1858
1859 cpl = __skb_push(skb, sizeof(*cpl));
1860 cpl->opcode = CPL_TX_PKT;
1861 cpl->ip_csum_dis = 1;
1862 cpl->l4_csum_dis = skb->ip_summed == CHECKSUM_PARTIAL ? 0 : 1;
1863
1864
1865 st->tx_cso += (skb->ip_summed == CHECKSUM_PARTIAL);
1866 }
1867 cpl->iff = dev->if_port;
1868
1869 if (skb_vlan_tag_present(skb)) {
1870 cpl->vlan_valid = 1;
1871 cpl->vlan = htons(skb_vlan_tag_get(skb));
1872 st->vlan_insert++;
1873 } else
1874 cpl->vlan_valid = 0;
1875
1876send:
1877 ret = t1_sge_tx(skb, adapter, 0, dev);
1878
1879
1880
1881
1882 if (unlikely(ret != NETDEV_TX_OK && skb != orig_skb)) {
1883 dev_kfree_skb_any(skb);
1884 ret = NETDEV_TX_OK;
1885 }
1886 return ret;
1887}
1888
1889
1890
1891
1892static void sge_tx_reclaim_cb(struct timer_list *t)
1893{
1894 int i;
1895 struct sge *sge = from_timer(sge, t, tx_reclaim_timer);
1896
1897 for (i = 0; i < SGE_CMDQ_N; ++i) {
1898 struct cmdQ *q = &sge->cmdQ[i];
1899
1900 if (!spin_trylock(&q->lock))
1901 continue;
1902
1903 reclaim_completed_tx(sge, q);
1904 if (i == 0 && q->in_use) {
1905 writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
1906 }
1907 spin_unlock(&q->lock);
1908 }
1909 mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
1910}
1911
1912
1913
1914
1915int t1_sge_set_coalesce_params(struct sge *sge, struct sge_params *p)
1916{
1917 sge->fixed_intrtimer = p->rx_coalesce_usecs *
1918 core_ticks_per_usec(sge->adapter);
1919 writel(sge->fixed_intrtimer, sge->adapter->regs + A_SG_INTRTIMER);
1920 return 0;
1921}
1922
1923
1924
1925
1926
1927int t1_sge_configure(struct sge *sge, struct sge_params *p)
1928{
1929 if (alloc_rx_resources(sge, p))
1930 return -ENOMEM;
1931 if (alloc_tx_resources(sge, p)) {
1932 free_rx_resources(sge);
1933 return -ENOMEM;
1934 }
1935 configure_sge(sge, p);
1936
1937
1938
1939
1940
1941
1942
1943 p->large_buf_capacity = jumbo_payload_capacity(sge);
1944 return 0;
1945}
1946
1947
1948
1949
1950void t1_sge_stop(struct sge *sge)
1951{
1952 int i;
1953 writel(0, sge->adapter->regs + A_SG_CONTROL);
1954 readl(sge->adapter->regs + A_SG_CONTROL);
1955
1956 if (is_T2(sge->adapter))
1957 del_timer_sync(&sge->espibug_timer);
1958
1959 del_timer_sync(&sge->tx_reclaim_timer);
1960 if (sge->tx_sched)
1961 tx_sched_stop(sge);
1962
1963 for (i = 0; i < MAX_NPORTS; i++)
1964 kfree_skb(sge->espibug_skb[i]);
1965}
1966
1967
1968
1969
1970void t1_sge_start(struct sge *sge)
1971{
1972 refill_free_list(sge, &sge->freelQ[0]);
1973 refill_free_list(sge, &sge->freelQ[1]);
1974
1975 writel(sge->sge_control, sge->adapter->regs + A_SG_CONTROL);
1976 doorbell_pio(sge->adapter, F_FL0_ENABLE | F_FL1_ENABLE);
1977 readl(sge->adapter->regs + A_SG_CONTROL);
1978
1979 mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
1980
1981 if (is_T2(sge->adapter))
1982 mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
1983}
1984
1985
1986
1987
1988static void espibug_workaround_t204(struct timer_list *t)
1989{
1990 struct sge *sge = from_timer(sge, t, espibug_timer);
1991 struct adapter *adapter = sge->adapter;
1992 unsigned int nports = adapter->params.nports;
1993 u32 seop[MAX_NPORTS];
1994
1995 if (adapter->open_device_map & PORT_MASK) {
1996 int i;
1997
1998 if (t1_espi_get_mon_t204(adapter, &(seop[0]), 0) < 0)
1999 return;
2000
2001 for (i = 0; i < nports; i++) {
2002 struct sk_buff *skb = sge->espibug_skb[i];
2003
2004 if (!netif_running(adapter->port[i].dev) ||
2005 netif_queue_stopped(adapter->port[i].dev) ||
2006 !seop[i] || ((seop[i] & 0xfff) != 0) || !skb)
2007 continue;
2008
2009 if (!skb->cb[0]) {
2010 skb_copy_to_linear_data_offset(skb,
2011 sizeof(struct cpl_tx_pkt),
2012 ch_mac_addr,
2013 ETH_ALEN);
2014 skb_copy_to_linear_data_offset(skb,
2015 skb->len - 10,
2016 ch_mac_addr,
2017 ETH_ALEN);
2018 skb->cb[0] = 0xff;
2019 }
2020
2021
2022
2023
2024 skb = skb_get(skb);
2025 t1_sge_tx(skb, adapter, 0, adapter->port[i].dev);
2026 }
2027 }
2028 mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
2029}
2030
2031static void espibug_workaround(struct timer_list *t)
2032{
2033 struct sge *sge = from_timer(sge, t, espibug_timer);
2034 struct adapter *adapter = sge->adapter;
2035
2036 if (netif_running(adapter->port[0].dev)) {
2037 struct sk_buff *skb = sge->espibug_skb[0];
2038 u32 seop = t1_espi_get_mon(adapter, 0x930, 0);
2039
2040 if ((seop & 0xfff0fff) == 0xfff && skb) {
2041 if (!skb->cb[0]) {
2042 skb_copy_to_linear_data_offset(skb,
2043 sizeof(struct cpl_tx_pkt),
2044 ch_mac_addr,
2045 ETH_ALEN);
2046 skb_copy_to_linear_data_offset(skb,
2047 skb->len - 10,
2048 ch_mac_addr,
2049 ETH_ALEN);
2050 skb->cb[0] = 0xff;
2051 }
2052
2053
2054
2055
2056 skb = skb_get(skb);
2057 t1_sge_tx(skb, adapter, 0, adapter->port[0].dev);
2058 }
2059 }
2060 mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
2061}
2062
2063
2064
2065
2066struct sge *t1_sge_create(struct adapter *adapter, struct sge_params *p)
2067{
2068 struct sge *sge = kzalloc(sizeof(*sge), GFP_KERNEL);
2069 int i;
2070
2071 if (!sge)
2072 return NULL;
2073
2074 sge->adapter = adapter;
2075 sge->netdev = adapter->port[0].dev;
2076 sge->rx_pkt_pad = t1_is_T1B(adapter) ? 0 : 2;
2077 sge->jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
2078
2079 for_each_port(adapter, i) {
2080 sge->port_stats[i] = alloc_percpu(struct sge_port_stats);
2081 if (!sge->port_stats[i])
2082 goto nomem_port;
2083 }
2084
2085 timer_setup(&sge->tx_reclaim_timer, sge_tx_reclaim_cb, 0);
2086
2087 if (is_T2(sge->adapter)) {
2088 timer_setup(&sge->espibug_timer,
2089 adapter->params.nports > 1 ? espibug_workaround_t204 : espibug_workaround,
2090 0);
2091
2092 if (adapter->params.nports > 1)
2093 tx_sched_init(sge);
2094
2095 sge->espibug_timeout = 1;
2096
2097 if (adapter->params.nports > 1)
2098 sge->espibug_timeout = HZ/100;
2099 }
2100
2101
2102 p->cmdQ_size[0] = SGE_CMDQ0_E_N;
2103 p->cmdQ_size[1] = SGE_CMDQ1_E_N;
2104 p->freelQ_size[!sge->jumbo_fl] = SGE_FREEL_SIZE;
2105 p->freelQ_size[sge->jumbo_fl] = SGE_JUMBO_FREEL_SIZE;
2106 if (sge->tx_sched) {
2107 if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204)
2108 p->rx_coalesce_usecs = 15;
2109 else
2110 p->rx_coalesce_usecs = 50;
2111 } else
2112 p->rx_coalesce_usecs = 50;
2113
2114 p->coalesce_enable = 0;
2115 p->sample_interval_usecs = 0;
2116
2117 return sge;
2118nomem_port:
2119 while (i >= 0) {
2120 free_percpu(sge->port_stats[i]);
2121 --i;
2122 }
2123 kfree(sge);
2124 return NULL;
2125
2126}
2127