blob: 6ddb3bb0ce67665bb2cc7e6b1632cd2953a22598 [file] [log] [blame]
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001/*
2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
3 *
4 * Copyright (c) 2003-2010 Chelsio Communications, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
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 * Rx buffer size. We use largish buffers if possible but settle for single
51 * pages under memory shortage.
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/* RX_PULL_LEN should be <= RX_COPY_THRES */
60#define RX_COPY_THRES 256
61#define RX_PULL_LEN 128
62
63/*
64 * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
65 * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
66 */
67#define RX_PKT_SKB_LEN 512
68
69/* Ethernet header padding prepended to RX_PKTs */
70#define RX_PKT_PAD 2
71
72/*
73 * Max number of Tx descriptors we clean up at a time. Should be modest as
74 * freeing skbs isn't cheap and it happens while holding locks. We just need
75 * to free packets faster than they arrive, we eventually catch up and keep
76 * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
77 */
78#define MAX_TX_RECLAIM 16
79
80/*
81 * Max number of Rx buffers we replenish at a time. Again keep this modest,
82 * allocating buffers isn't cheap either.
83 */
84#define MAX_RX_REFILL 16U
85
86/*
87 * Period of the Rx queue check timer. This timer is infrequent as it has
88 * something to do only when the system experiences severe memory shortage.
89 */
90#define RX_QCHECK_PERIOD (HZ / 2)
91
92/*
93 * Period of the Tx queue check timer.
94 */
95#define TX_QCHECK_PERIOD (HZ / 2)
96
97/*
98 * Max number of Tx descriptors to be reclaimed by the Tx timer.
99 */
100#define MAX_TIMER_TX_RECLAIM 100
101
102/*
103 * Timer index used when backing off due to memory shortage.
104 */
105#define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
106
107/*
108 * An FL with <= FL_STARVE_THRES buffers is starving and a periodic timer will
109 * attempt to refill it.
110 */
111#define FL_STARVE_THRES 4
112
113/*
114 * Suspend an Ethernet Tx queue with fewer available descriptors than this.
115 * This is the same as calc_tx_descs() for a TSO packet with
116 * nr_frags == MAX_SKB_FRAGS.
117 */
118#define ETHTXQ_STOP_THRES \
119 (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
120
121/*
122 * Suspension threshold for non-Ethernet Tx queues. We require enough room
123 * for a full sized WR.
124 */
125#define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
126
127/*
128 * Max Tx descriptor space we allow for an Ethernet packet to be inlined
129 * into a WR.
130 */
131#define MAX_IMM_TX_PKT_LEN 128
132
133/*
134 * Max size of a WR sent through a control Tx queue.
135 */
136#define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
137
138enum {
139 /* packet alignment in FL buffers */
140 FL_ALIGN = L1_CACHE_BYTES < 32 ? 32 : L1_CACHE_BYTES,
141 /* egress status entry size */
142 STAT_LEN = L1_CACHE_BYTES > 64 ? 128 : 64
143};
144
145struct tx_sw_desc { /* SW state per Tx descriptor */
146 struct sk_buff *skb;
147 struct ulptx_sgl *sgl;
148};
149
150struct rx_sw_desc { /* SW state per Rx descriptor */
151 struct page *page;
152 dma_addr_t dma_addr;
153};
154
155/*
156 * The low bits of rx_sw_desc.dma_addr have special meaning.
157 */
158enum {
159 RX_LARGE_BUF = 1 << 0, /* buffer is larger than PAGE_SIZE */
160 RX_UNMAPPED_BUF = 1 << 1, /* buffer is not mapped */
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 * txq_avail - return the number of available slots in a Tx queue
175 * @q: the Tx queue
176 *
177 * Returns the number of descriptors in a Tx queue available to write new
178 * packets.
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 * fl_cap - return the capacity of a free-buffer list
187 * @fl: the FL
188 *
189 * Returns the capacity of a free-buffer list. The capacity is less than
190 * the size because one descriptor needs to be left unpopulated, otherwise
191 * HW will think the FL is empty.
192 */
193static inline unsigned int fl_cap(const struct sge_fl *fl)
194{
195 return fl->size - 8; /* 1 descriptor = 8 buffers */
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 * deferred_unmap_destructor - unmap a packet when it is freed
250 * @skb: the packet
251 *
252 * This is the packet destructor used for Tx packets that need to remain
253 * mapped until they are freed rather than until their Tx descriptors are
254 * freed.
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 * the complexity below is because of the possibility of a wrap-around
279 * in the middle of an SGL
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 * free_tx_desc - reclaims Tx descriptors and their buffers
323 * @adapter: the adapter
324 * @q: the Tx queue to reclaim descriptors from
325 * @n: the number of descriptors to reclaim
326 * @unmap: whether the buffers should be unmapped for DMA
327 *
328 * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
329 * Tx buffers. Called with the Tx queue lock held.
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) { /* an SGL is present */
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 * Return the number of reclaimable descriptors in a Tx queue.
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 * reclaim_completed_tx - reclaims completed Tx descriptors
367 * @adap: the adapter
368 * @q: the Tx queue to reclaim completed descriptors from
369 * @unmap: whether the buffers should be unmapped for DMA
370 *
371 * Reclaims Tx descriptors that the SGE has indicated it has processed,
372 * and frees the associated buffers if possible. Called with the Tx
373 * queue locked.
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 * Limit the amount of clean up work we do at a time to keep
383 * the Tx lock hold time O(1).
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 * free_rx_bufs - free the Rx buffers on an SGE free list
405 * @adap: the adapter
406 * @q: the SGE free list to free buffers from
407 * @n: how many buffers to free
408 *
409 * Release the next @n buffers on an SGE free-buffer Rx queue. The
410 * buffers must be made inaccessible to HW before calling this function.
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 * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
430 * @adap: the adapter
431 * @q: the SGE free list
432 *
433 * Unmap the current buffer on an SGE free-buffer Rx queue. The
434 * buffer must be made inaccessible to HW before calling this function.
435 *
436 * This is similar to @free_rx_bufs above but does not free the buffer.
437 * Do note that the FL still loses any further access to the buffer.
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; /* includes size low bits */
467}
468
469/**
470 * refill_fl - refill an SGE Rx buffer ring
471 * @adap: the adapter
472 * @q: the ring to refill
473 * @n: the number of new buffers to allocate
474 * @gfp: the gfp flags for the allocations
475 *
476 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
477 * allocated with the supplied gfp flags. The caller must assure that
478 * @n does not exceed the queue's capacity. If afterwards the queue is
479 * found critically low mark it as starving in the bitmap of starving FLs.
480 *
481 * Returns the number of buffers allocated.
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; /* failures are expected */
493
494#if FL_PG_ORDER > 0
495 /*
496 * Prefer large buffers
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; /* fall back to single pages */
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; /* do not try small pages for this error */
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();
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000560 set_bit(q->cntxt_id - adap->sge.egr_start,
561 adap->sge.starving_fl);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000562 }
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 * alloc_ring - allocate resources for an SGE descriptor ring
575 * @dev: the PCI device's core device
576 * @nelem: the number of descriptors
577 * @elem_size: the size of each descriptor
578 * @sw_size: the size of the SW state associated with each ring element
579 * @phys: the physical address of the allocated ring
580 * @metadata: address of the array holding the SW state for the ring
581 * @stat_size: extra space in HW ring for status information
582 *
583 * Allocates resources for an SGE descriptor ring, such as Tx queues,
584 * free buffer lists, or response queues. Each SGE ring requires
585 * space for its HW descriptors plus, optionally, space for the SW state
586 * associated with each HW entry (the metadata). The function returns
587 * three values: the virtual address for the HW ring (the return value
588 * of the function), the bus address of the HW ring, and the address
589 * of the SW ring.
590 */
591static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
592 size_t sw_size, dma_addr_t *phys, void *metadata,
593 size_t stat_size)
594{
595 size_t len = nelem * elem_size + stat_size;
596 void *s = NULL;
597 void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
598
599 if (!p)
600 return NULL;
601 if (sw_size) {
602 s = kcalloc(nelem, sw_size, GFP_KERNEL);
603
604 if (!s) {
605 dma_free_coherent(dev, len, p, *phys);
606 return NULL;
607 }
608 }
609 if (metadata)
610 *(void **)metadata = s;
611 memset(p, 0, len);
612 return p;
613}
614
615/**
616 * sgl_len - calculates the size of an SGL of the given capacity
617 * @n: the number of SGL entries
618 *
619 * Calculates the number of flits needed for a scatter/gather list that
620 * can hold the given number of entries.
621 */
622static inline unsigned int sgl_len(unsigned int n)
623{
624 n--;
625 return (3 * n) / 2 + (n & 1) + 2;
626}
627
628/**
629 * flits_to_desc - returns the num of Tx descriptors for the given flits
630 * @n: the number of flits
631 *
632 * Returns the number of Tx descriptors needed for the supplied number
633 * of flits.
634 */
635static inline unsigned int flits_to_desc(unsigned int n)
636{
637 BUG_ON(n > SGE_MAX_WR_LEN / 8);
638 return DIV_ROUND_UP(n, 8);
639}
640
641/**
642 * is_eth_imm - can an Ethernet packet be sent as immediate data?
643 * @skb: the packet
644 *
645 * Returns whether an Ethernet packet is small enough to fit as
646 * immediate data.
647 */
648static inline int is_eth_imm(const struct sk_buff *skb)
649{
650 return skb->len <= MAX_IMM_TX_PKT_LEN - sizeof(struct cpl_tx_pkt);
651}
652
653/**
654 * calc_tx_flits - calculate the number of flits for a packet Tx WR
655 * @skb: the packet
656 *
657 * Returns the number of flits needed for a Tx WR for the given Ethernet
658 * packet, including the needed WR and CPL headers.
659 */
660static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
661{
662 unsigned int flits;
663
664 if (is_eth_imm(skb))
665 return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt), 8);
666
667 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
668 if (skb_shinfo(skb)->gso_size)
669 flits += 2;
670 return flits;
671}
672
673/**
674 * calc_tx_descs - calculate the number of Tx descriptors for a packet
675 * @skb: the packet
676 *
677 * Returns the number of Tx descriptors needed for the given Ethernet
678 * packet, including the needed WR and CPL headers.
679 */
680static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
681{
682 return flits_to_desc(calc_tx_flits(skb));
683}
684
685/**
686 * write_sgl - populate a scatter/gather list for a packet
687 * @skb: the packet
688 * @q: the Tx queue we are writing into
689 * @sgl: starting location for writing the SGL
690 * @end: points right after the end of the SGL
691 * @start: start offset into skb main-body data to include in the SGL
692 * @addr: the list of bus addresses for the SGL elements
693 *
694 * Generates a gather list for the buffers that make up a packet.
695 * The caller must provide adequate space for the SGL that will be written.
696 * The SGL includes all of the packet's page fragments and the data in its
697 * main body except for the first @start bytes. @sgl must be 16-byte
698 * aligned and within a Tx descriptor with available space. @end points
699 * right after the end of the SGL but does not account for any potential
700 * wrap around, i.e., @end > @sgl.
701 */
702static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
703 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
704 const dma_addr_t *addr)
705{
706 unsigned int i, len;
707 struct ulptx_sge_pair *to;
708 const struct skb_shared_info *si = skb_shinfo(skb);
709 unsigned int nfrags = si->nr_frags;
710 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
711
712 len = skb_headlen(skb) - start;
713 if (likely(len)) {
714 sgl->len0 = htonl(len);
715 sgl->addr0 = cpu_to_be64(addr[0] + start);
716 nfrags++;
717 } else {
718 sgl->len0 = htonl(si->frags[0].size);
719 sgl->addr0 = cpu_to_be64(addr[1]);
720 }
721
722 sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_NSGE(nfrags));
723 if (likely(--nfrags == 0))
724 return;
725 /*
726 * Most of the complexity below deals with the possibility we hit the
727 * end of the queue in the middle of writing the SGL. For this case
728 * only we create the SGL in a temporary buffer and then copy it.
729 */
730 to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
731
732 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
733 to->len[0] = cpu_to_be32(si->frags[i].size);
734 to->len[1] = cpu_to_be32(si->frags[++i].size);
735 to->addr[0] = cpu_to_be64(addr[i]);
736 to->addr[1] = cpu_to_be64(addr[++i]);
737 }
738 if (nfrags) {
739 to->len[0] = cpu_to_be32(si->frags[i].size);
740 to->len[1] = cpu_to_be32(0);
741 to->addr[0] = cpu_to_be64(addr[i + 1]);
742 }
743 if (unlikely((u8 *)end > (u8 *)q->stat)) {
744 unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
745
746 if (likely(part0))
747 memcpy(sgl->sge, buf, part0);
748 part1 = (u8 *)end - (u8 *)q->stat;
749 memcpy(q->desc, (u8 *)buf + part0, part1);
750 end = (void *)q->desc + part1;
751 }
752 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
753 *(u64 *)end = 0;
754}
755
756/**
757 * ring_tx_db - check and potentially ring a Tx queue's doorbell
758 * @adap: the adapter
759 * @q: the Tx queue
760 * @n: number of new descriptors to give to HW
761 *
762 * Ring the doorbel for a Tx queue.
763 */
764static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
765{
766 wmb(); /* write descriptors before telling HW */
767 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
768 QID(q->cntxt_id) | PIDX(n));
769}
770
771/**
772 * inline_tx_skb - inline a packet's data into Tx descriptors
773 * @skb: the packet
774 * @q: the Tx queue where the packet will be inlined
775 * @pos: starting position in the Tx queue where to inline the packet
776 *
777 * Inline a packet's contents directly into Tx descriptors, starting at
778 * the given position within the Tx DMA ring.
779 * Most of the complexity of this operation is dealing with wrap arounds
780 * in the middle of the packet we want to inline.
781 */
782static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
783 void *pos)
784{
785 u64 *p;
786 int left = (void *)q->stat - pos;
787
788 if (likely(skb->len <= left)) {
789 if (likely(!skb->data_len))
790 skb_copy_from_linear_data(skb, pos, skb->len);
791 else
792 skb_copy_bits(skb, 0, pos, skb->len);
793 pos += skb->len;
794 } else {
795 skb_copy_bits(skb, 0, pos, left);
796 skb_copy_bits(skb, left, q->desc, skb->len - left);
797 pos = (void *)q->desc + (skb->len - left);
798 }
799
800 /* 0-pad to multiple of 16 */
801 p = PTR_ALIGN(pos, 8);
802 if ((uintptr_t)p & 8)
803 *p = 0;
804}
805
806/*
807 * Figure out what HW csum a packet wants and return the appropriate control
808 * bits.
809 */
810static u64 hwcsum(const struct sk_buff *skb)
811{
812 int csum_type;
813 const struct iphdr *iph = ip_hdr(skb);
814
815 if (iph->version == 4) {
816 if (iph->protocol == IPPROTO_TCP)
817 csum_type = TX_CSUM_TCPIP;
818 else if (iph->protocol == IPPROTO_UDP)
819 csum_type = TX_CSUM_UDPIP;
820 else {
821nocsum: /*
822 * unknown protocol, disable HW csum
823 * and hope a bad packet is detected
824 */
825 return TXPKT_L4CSUM_DIS;
826 }
827 } else {
828 /*
829 * this doesn't work with extension headers
830 */
831 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
832
833 if (ip6h->nexthdr == IPPROTO_TCP)
834 csum_type = TX_CSUM_TCPIP6;
835 else if (ip6h->nexthdr == IPPROTO_UDP)
836 csum_type = TX_CSUM_UDPIP6;
837 else
838 goto nocsum;
839 }
840
841 if (likely(csum_type >= TX_CSUM_TCPIP))
842 return TXPKT_CSUM_TYPE(csum_type) |
843 TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
844 TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
845 else {
846 int start = skb_transport_offset(skb);
847
848 return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
849 TXPKT_CSUM_LOC(start + skb->csum_offset);
850 }
851}
852
853static void eth_txq_stop(struct sge_eth_txq *q)
854{
855 netif_tx_stop_queue(q->txq);
856 q->q.stops++;
857}
858
859static inline void txq_advance(struct sge_txq *q, unsigned int n)
860{
861 q->in_use += n;
862 q->pidx += n;
863 if (q->pidx >= q->size)
864 q->pidx -= q->size;
865}
866
867/**
868 * t4_eth_xmit - add a packet to an Ethernet Tx queue
869 * @skb: the packet
870 * @dev: the egress net device
871 *
872 * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
873 */
874netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
875{
876 u32 wr_mid;
877 u64 cntrl, *end;
878 int qidx, credits;
879 unsigned int flits, ndesc;
880 struct adapter *adap;
881 struct sge_eth_txq *q;
882 const struct port_info *pi;
883 struct fw_eth_tx_pkt_wr *wr;
884 struct cpl_tx_pkt_core *cpl;
885 const struct skb_shared_info *ssi;
886 dma_addr_t addr[MAX_SKB_FRAGS + 1];
887
888 /*
889 * The chip min packet length is 10 octets but play safe and reject
890 * anything shorter than an Ethernet header.
891 */
892 if (unlikely(skb->len < ETH_HLEN)) {
893out_free: dev_kfree_skb(skb);
894 return NETDEV_TX_OK;
895 }
896
897 pi = netdev_priv(dev);
898 adap = pi->adapter;
899 qidx = skb_get_queue_mapping(skb);
900 q = &adap->sge.ethtxq[qidx + pi->first_qset];
901
902 reclaim_completed_tx(adap, &q->q, true);
903
904 flits = calc_tx_flits(skb);
905 ndesc = flits_to_desc(flits);
906 credits = txq_avail(&q->q) - ndesc;
907
908 if (unlikely(credits < 0)) {
909 eth_txq_stop(q);
910 dev_err(adap->pdev_dev,
911 "%s: Tx ring %u full while queue awake!\n",
912 dev->name, qidx);
913 return NETDEV_TX_BUSY;
914 }
915
916 if (!is_eth_imm(skb) &&
917 unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
918 q->mapping_err++;
919 goto out_free;
920 }
921
922 wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
923 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
924 eth_txq_stop(q);
925 wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
926 }
927
928 wr = (void *)&q->q.desc[q->q.pidx];
929 wr->equiq_to_len16 = htonl(wr_mid);
930 wr->r3 = cpu_to_be64(0);
931 end = (u64 *)wr + flits;
932
933 ssi = skb_shinfo(skb);
934 if (ssi->gso_size) {
Dimitris Michailidis625ac6a2010-08-02 13:19:18 +0000935 struct cpl_tx_pkt_lso *lso = (void *)wr;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000936 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
937 int l3hdr_len = skb_network_header_len(skb);
938 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
939
940 wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
941 FW_WR_IMMDLEN(sizeof(*lso)));
Dimitris Michailidis625ac6a2010-08-02 13:19:18 +0000942 lso->c.lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
943 LSO_FIRST_SLICE | LSO_LAST_SLICE |
944 LSO_IPV6(v6) |
945 LSO_ETHHDR_LEN(eth_xtra_len / 4) |
946 LSO_IPHDR_LEN(l3hdr_len / 4) |
947 LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
948 lso->c.ipid_ofst = htons(0);
949 lso->c.mss = htons(ssi->gso_size);
950 lso->c.seqno_offset = htonl(0);
951 lso->c.len = htonl(skb->len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000952 cpl = (void *)(lso + 1);
953 cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
954 TXPKT_IPHDR_LEN(l3hdr_len) |
955 TXPKT_ETHHDR_LEN(eth_xtra_len);
956 q->tso++;
957 q->tx_cso += ssi->gso_segs;
958 } else {
959 int len;
960
961 len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
962 wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
963 FW_WR_IMMDLEN(len));
964 cpl = (void *)(wr + 1);
965 if (skb->ip_summed == CHECKSUM_PARTIAL) {
966 cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
967 q->tx_cso++;
968 } else
969 cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
970 }
971
972 if (vlan_tx_tag_present(skb)) {
973 q->vlan_ins++;
974 cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
975 }
976
977 cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
Dimitris Michailidis1707aec2010-08-23 17:21:00 +0000978 TXPKT_INTF(pi->tx_chan) | TXPKT_PF(adap->fn));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000979 cpl->pack = htons(0);
980 cpl->len = htons(skb->len);
981 cpl->ctrl1 = cpu_to_be64(cntrl);
982
983 if (is_eth_imm(skb)) {
984 inline_tx_skb(skb, &q->q, cpl + 1);
985 dev_kfree_skb(skb);
986 } else {
987 int last_desc;
988
989 write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
990 addr);
991 skb_orphan(skb);
992
993 last_desc = q->q.pidx + ndesc - 1;
994 if (last_desc >= q->q.size)
995 last_desc -= q->q.size;
996 q->q.sdesc[last_desc].skb = skb;
997 q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
998 }
999
1000 txq_advance(&q->q, ndesc);
1001
1002 ring_tx_db(adap, &q->q, ndesc);
1003 return NETDEV_TX_OK;
1004}
1005
1006/**
1007 * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
1008 * @q: the SGE control Tx queue
1009 *
1010 * This is a variant of reclaim_completed_tx() that is used for Tx queues
1011 * that send only immediate data (presently just the control queues) and
1012 * thus do not have any sk_buffs to release.
1013 */
1014static inline void reclaim_completed_tx_imm(struct sge_txq *q)
1015{
1016 int hw_cidx = ntohs(q->stat->cidx);
1017 int reclaim = hw_cidx - q->cidx;
1018
1019 if (reclaim < 0)
1020 reclaim += q->size;
1021
1022 q->in_use -= reclaim;
1023 q->cidx = hw_cidx;
1024}
1025
1026/**
1027 * is_imm - check whether a packet can be sent as immediate data
1028 * @skb: the packet
1029 *
1030 * Returns true if a packet can be sent as a WR with immediate data.
1031 */
1032static inline int is_imm(const struct sk_buff *skb)
1033{
1034 return skb->len <= MAX_CTRL_WR_LEN;
1035}
1036
1037/**
1038 * ctrlq_check_stop - check if a control queue is full and should stop
1039 * @q: the queue
1040 * @wr: most recent WR written to the queue
1041 *
1042 * Check if a control queue has become full and should be stopped.
1043 * We clean up control queue descriptors very lazily, only when we are out.
1044 * If the queue is still full after reclaiming any completed descriptors
1045 * we suspend it and have the last WR wake it up.
1046 */
1047static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
1048{
1049 reclaim_completed_tx_imm(&q->q);
1050 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1051 wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
1052 q->q.stops++;
1053 q->full = 1;
1054 }
1055}
1056
1057/**
1058 * ctrl_xmit - send a packet through an SGE control Tx queue
1059 * @q: the control queue
1060 * @skb: the packet
1061 *
1062 * Send a packet through an SGE control Tx queue. Packets sent through
1063 * a control queue must fit entirely as immediate data.
1064 */
1065static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
1066{
1067 unsigned int ndesc;
1068 struct fw_wr_hdr *wr;
1069
1070 if (unlikely(!is_imm(skb))) {
1071 WARN_ON(1);
1072 dev_kfree_skb(skb);
1073 return NET_XMIT_DROP;
1074 }
1075
1076 ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
1077 spin_lock(&q->sendq.lock);
1078
1079 if (unlikely(q->full)) {
1080 skb->priority = ndesc; /* save for restart */
1081 __skb_queue_tail(&q->sendq, skb);
1082 spin_unlock(&q->sendq.lock);
1083 return NET_XMIT_CN;
1084 }
1085
1086 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1087 inline_tx_skb(skb, &q->q, wr);
1088
1089 txq_advance(&q->q, ndesc);
1090 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
1091 ctrlq_check_stop(q, wr);
1092
1093 ring_tx_db(q->adap, &q->q, ndesc);
1094 spin_unlock(&q->sendq.lock);
1095
1096 kfree_skb(skb);
1097 return NET_XMIT_SUCCESS;
1098}
1099
1100/**
1101 * restart_ctrlq - restart a suspended control queue
1102 * @data: the control queue to restart
1103 *
1104 * Resumes transmission on a suspended Tx control queue.
1105 */
1106static void restart_ctrlq(unsigned long data)
1107{
1108 struct sk_buff *skb;
1109 unsigned int written = 0;
1110 struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
1111
1112 spin_lock(&q->sendq.lock);
1113 reclaim_completed_tx_imm(&q->q);
1114 BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
1115
1116 while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
1117 struct fw_wr_hdr *wr;
1118 unsigned int ndesc = skb->priority; /* previously saved */
1119
1120 /*
1121 * Write descriptors and free skbs outside the lock to limit
1122 * wait times. q->full is still set so new skbs will be queued.
1123 */
1124 spin_unlock(&q->sendq.lock);
1125
1126 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1127 inline_tx_skb(skb, &q->q, wr);
1128 kfree_skb(skb);
1129
1130 written += ndesc;
1131 txq_advance(&q->q, ndesc);
1132 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1133 unsigned long old = q->q.stops;
1134
1135 ctrlq_check_stop(q, wr);
1136 if (q->q.stops != old) { /* suspended anew */
1137 spin_lock(&q->sendq.lock);
1138 goto ringdb;
1139 }
1140 }
1141 if (written > 16) {
1142 ring_tx_db(q->adap, &q->q, written);
1143 written = 0;
1144 }
1145 spin_lock(&q->sendq.lock);
1146 }
1147 q->full = 0;
1148ringdb: if (written)
1149 ring_tx_db(q->adap, &q->q, written);
1150 spin_unlock(&q->sendq.lock);
1151}
1152
1153/**
1154 * t4_mgmt_tx - send a management message
1155 * @adap: the adapter
1156 * @skb: the packet containing the management message
1157 *
1158 * Send a management message through control queue 0.
1159 */
1160int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
1161{
1162 int ret;
1163
1164 local_bh_disable();
1165 ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
1166 local_bh_enable();
1167 return ret;
1168}
1169
1170/**
1171 * is_ofld_imm - check whether a packet can be sent as immediate data
1172 * @skb: the packet
1173 *
1174 * Returns true if a packet can be sent as an offload WR with immediate
1175 * data. We currently use the same limit as for Ethernet packets.
1176 */
1177static inline int is_ofld_imm(const struct sk_buff *skb)
1178{
1179 return skb->len <= MAX_IMM_TX_PKT_LEN;
1180}
1181
1182/**
1183 * calc_tx_flits_ofld - calculate # of flits for an offload packet
1184 * @skb: the packet
1185 *
1186 * Returns the number of flits needed for the given offload packet.
1187 * These packets are already fully constructed and no additional headers
1188 * will be added.
1189 */
1190static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
1191{
1192 unsigned int flits, cnt;
1193
1194 if (is_ofld_imm(skb))
1195 return DIV_ROUND_UP(skb->len, 8);
1196
1197 flits = skb_transport_offset(skb) / 8U; /* headers */
1198 cnt = skb_shinfo(skb)->nr_frags;
1199 if (skb->tail != skb->transport_header)
1200 cnt++;
1201 return flits + sgl_len(cnt);
1202}
1203
1204/**
1205 * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
1206 * @adap: the adapter
1207 * @q: the queue to stop
1208 *
1209 * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
1210 * inability to map packets. A periodic timer attempts to restart
1211 * queues so marked.
1212 */
1213static void txq_stop_maperr(struct sge_ofld_txq *q)
1214{
1215 q->mapping_err++;
1216 q->q.stops++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001217 set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
1218 q->adap->sge.txq_maperr);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001219}
1220
1221/**
1222 * ofldtxq_stop - stop an offload Tx queue that has become full
1223 * @q: the queue to stop
1224 * @skb: the packet causing the queue to become full
1225 *
1226 * Stops an offload Tx queue that has become full and modifies the packet
1227 * being written to request a wakeup.
1228 */
1229static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
1230{
1231 struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
1232
1233 wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
1234 q->q.stops++;
1235 q->full = 1;
1236}
1237
1238/**
1239 * service_ofldq - restart a suspended offload queue
1240 * @q: the offload queue
1241 *
1242 * Services an offload Tx queue by moving packets from its packet queue
1243 * to the HW Tx ring. The function starts and ends with the queue locked.
1244 */
1245static void service_ofldq(struct sge_ofld_txq *q)
1246{
1247 u64 *pos;
1248 int credits;
1249 struct sk_buff *skb;
1250 unsigned int written = 0;
1251 unsigned int flits, ndesc;
1252
1253 while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
1254 /*
1255 * We drop the lock but leave skb on sendq, thus retaining
1256 * exclusive access to the state of the queue.
1257 */
1258 spin_unlock(&q->sendq.lock);
1259
1260 reclaim_completed_tx(q->adap, &q->q, false);
1261
1262 flits = skb->priority; /* previously saved */
1263 ndesc = flits_to_desc(flits);
1264 credits = txq_avail(&q->q) - ndesc;
1265 BUG_ON(credits < 0);
1266 if (unlikely(credits < TXQ_STOP_THRES))
1267 ofldtxq_stop(q, skb);
1268
1269 pos = (u64 *)&q->q.desc[q->q.pidx];
1270 if (is_ofld_imm(skb))
1271 inline_tx_skb(skb, &q->q, pos);
1272 else if (map_skb(q->adap->pdev_dev, skb,
1273 (dma_addr_t *)skb->head)) {
1274 txq_stop_maperr(q);
1275 spin_lock(&q->sendq.lock);
1276 break;
1277 } else {
1278 int last_desc, hdr_len = skb_transport_offset(skb);
1279
1280 memcpy(pos, skb->data, hdr_len);
1281 write_sgl(skb, &q->q, (void *)pos + hdr_len,
1282 pos + flits, hdr_len,
1283 (dma_addr_t *)skb->head);
1284#ifdef CONFIG_NEED_DMA_MAP_STATE
1285 skb->dev = q->adap->port[0];
1286 skb->destructor = deferred_unmap_destructor;
1287#endif
1288 last_desc = q->q.pidx + ndesc - 1;
1289 if (last_desc >= q->q.size)
1290 last_desc -= q->q.size;
1291 q->q.sdesc[last_desc].skb = skb;
1292 }
1293
1294 txq_advance(&q->q, ndesc);
1295 written += ndesc;
1296 if (unlikely(written > 32)) {
1297 ring_tx_db(q->adap, &q->q, written);
1298 written = 0;
1299 }
1300
1301 spin_lock(&q->sendq.lock);
1302 __skb_unlink(skb, &q->sendq);
1303 if (is_ofld_imm(skb))
1304 kfree_skb(skb);
1305 }
1306 if (likely(written))
1307 ring_tx_db(q->adap, &q->q, written);
1308}
1309
1310/**
1311 * ofld_xmit - send a packet through an offload queue
1312 * @q: the Tx offload queue
1313 * @skb: the packet
1314 *
1315 * Send an offload packet through an SGE offload queue.
1316 */
1317static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
1318{
1319 skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
1320 spin_lock(&q->sendq.lock);
1321 __skb_queue_tail(&q->sendq, skb);
1322 if (q->sendq.qlen == 1)
1323 service_ofldq(q);
1324 spin_unlock(&q->sendq.lock);
1325 return NET_XMIT_SUCCESS;
1326}
1327
1328/**
1329 * restart_ofldq - restart a suspended offload queue
1330 * @data: the offload queue to restart
1331 *
1332 * Resumes transmission on a suspended Tx offload queue.
1333 */
1334static void restart_ofldq(unsigned long data)
1335{
1336 struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
1337
1338 spin_lock(&q->sendq.lock);
1339 q->full = 0; /* the queue actually is completely empty now */
1340 service_ofldq(q);
1341 spin_unlock(&q->sendq.lock);
1342}
1343
1344/**
1345 * skb_txq - return the Tx queue an offload packet should use
1346 * @skb: the packet
1347 *
1348 * Returns the Tx queue an offload packet should use as indicated by bits
1349 * 1-15 in the packet's queue_mapping.
1350 */
1351static inline unsigned int skb_txq(const struct sk_buff *skb)
1352{
1353 return skb->queue_mapping >> 1;
1354}
1355
1356/**
1357 * is_ctrl_pkt - return whether an offload packet is a control packet
1358 * @skb: the packet
1359 *
1360 * Returns whether an offload packet should use an OFLD or a CTRL
1361 * Tx queue as indicated by bit 0 in the packet's queue_mapping.
1362 */
1363static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
1364{
1365 return skb->queue_mapping & 1;
1366}
1367
1368static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
1369{
1370 unsigned int idx = skb_txq(skb);
1371
1372 if (unlikely(is_ctrl_pkt(skb)))
1373 return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
1374 return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
1375}
1376
1377/**
1378 * t4_ofld_send - send an offload packet
1379 * @adap: the adapter
1380 * @skb: the packet
1381 *
1382 * Sends an offload packet. We use the packet queue_mapping to select the
1383 * appropriate Tx queue as follows: bit 0 indicates whether the packet
1384 * should be sent as regular or control, bits 1-15 select the queue.
1385 */
1386int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
1387{
1388 int ret;
1389
1390 local_bh_disable();
1391 ret = ofld_send(adap, skb);
1392 local_bh_enable();
1393 return ret;
1394}
1395
1396/**
1397 * cxgb4_ofld_send - send an offload packet
1398 * @dev: the net device
1399 * @skb: the packet
1400 *
1401 * Sends an offload packet. This is an exported version of @t4_ofld_send,
1402 * intended for ULDs.
1403 */
1404int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
1405{
1406 return t4_ofld_send(netdev2adap(dev), skb);
1407}
1408EXPORT_SYMBOL(cxgb4_ofld_send);
1409
1410static inline void copy_frags(struct skb_shared_info *ssi,
1411 const struct pkt_gl *gl, unsigned int offset)
1412{
1413 unsigned int n;
1414
1415 /* usually there's just one frag */
1416 ssi->frags[0].page = gl->frags[0].page;
1417 ssi->frags[0].page_offset = gl->frags[0].page_offset + offset;
1418 ssi->frags[0].size = gl->frags[0].size - offset;
1419 ssi->nr_frags = gl->nfrags;
1420 n = gl->nfrags - 1;
1421 if (n)
1422 memcpy(&ssi->frags[1], &gl->frags[1], n * sizeof(skb_frag_t));
1423
1424 /* get a reference to the last page, we don't own it */
1425 get_page(gl->frags[n].page);
1426}
1427
1428/**
1429 * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
1430 * @gl: the gather list
1431 * @skb_len: size of sk_buff main body if it carries fragments
1432 * @pull_len: amount of data to move to the sk_buff's main body
1433 *
1434 * Builds an sk_buff from the given packet gather list. Returns the
1435 * sk_buff or %NULL if sk_buff allocation failed.
1436 */
1437struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
1438 unsigned int skb_len, unsigned int pull_len)
1439{
1440 struct sk_buff *skb;
1441
1442 /*
1443 * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
1444 * size, which is expected since buffers are at least PAGE_SIZEd.
1445 * In this case packets up to RX_COPY_THRES have only one fragment.
1446 */
1447 if (gl->tot_len <= RX_COPY_THRES) {
1448 skb = dev_alloc_skb(gl->tot_len);
1449 if (unlikely(!skb))
1450 goto out;
1451 __skb_put(skb, gl->tot_len);
1452 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1453 } else {
1454 skb = dev_alloc_skb(skb_len);
1455 if (unlikely(!skb))
1456 goto out;
1457 __skb_put(skb, pull_len);
1458 skb_copy_to_linear_data(skb, gl->va, pull_len);
1459
1460 copy_frags(skb_shinfo(skb), gl, pull_len);
1461 skb->len = gl->tot_len;
1462 skb->data_len = skb->len - pull_len;
1463 skb->truesize += skb->data_len;
1464 }
1465out: return skb;
1466}
1467EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
1468
1469/**
1470 * t4_pktgl_free - free a packet gather list
1471 * @gl: the gather list
1472 *
1473 * Releases the pages of a packet gather list. We do not own the last
1474 * page on the list and do not free it.
1475 */
Roland Dreierde498c82010-04-21 08:59:17 +00001476static void t4_pktgl_free(const struct pkt_gl *gl)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001477{
1478 int n;
1479 const skb_frag_t *p;
1480
1481 for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
1482 put_page(p->page);
1483}
1484
1485/*
1486 * Process an MPS trace packet. Give it an unused protocol number so it won't
1487 * be delivered to anyone and send it to the stack for capture.
1488 */
1489static noinline int handle_trace_pkt(struct adapter *adap,
1490 const struct pkt_gl *gl)
1491{
1492 struct sk_buff *skb;
1493 struct cpl_trace_pkt *p;
1494
1495 skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
1496 if (unlikely(!skb)) {
1497 t4_pktgl_free(gl);
1498 return 0;
1499 }
1500
1501 p = (struct cpl_trace_pkt *)skb->data;
1502 __skb_pull(skb, sizeof(*p));
1503 skb_reset_mac_header(skb);
1504 skb->protocol = htons(0xffff);
1505 skb->dev = adap->port[0];
1506 netif_receive_skb(skb);
1507 return 0;
1508}
1509
1510static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1511 const struct cpl_rx_pkt *pkt)
1512{
1513 int ret;
1514 struct sk_buff *skb;
1515
1516 skb = napi_get_frags(&rxq->rspq.napi);
1517 if (unlikely(!skb)) {
1518 t4_pktgl_free(gl);
1519 rxq->stats.rx_drops++;
1520 return;
1521 }
1522
1523 copy_frags(skb_shinfo(skb), gl, RX_PKT_PAD);
1524 skb->len = gl->tot_len - RX_PKT_PAD;
1525 skb->data_len = skb->len;
1526 skb->truesize += skb->data_len;
1527 skb->ip_summed = CHECKSUM_UNNECESSARY;
1528 skb_record_rx_queue(skb, rxq->rspq.idx);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001529 if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
1530 skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001531
1532 if (unlikely(pkt->vlan_ex)) {
1533 struct port_info *pi = netdev_priv(rxq->rspq.netdev);
1534 struct vlan_group *grp = pi->vlan_grp;
1535
1536 rxq->stats.vlan_ex++;
1537 if (likely(grp)) {
1538 ret = vlan_gro_frags(&rxq->rspq.napi, grp,
1539 ntohs(pkt->vlan));
1540 goto stats;
1541 }
1542 }
1543 ret = napi_gro_frags(&rxq->rspq.napi);
1544stats: if (ret == GRO_HELD)
1545 rxq->stats.lro_pkts++;
1546 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1547 rxq->stats.lro_merged++;
1548 rxq->stats.pkts++;
1549 rxq->stats.rx_cso++;
1550}
1551
1552/**
1553 * t4_ethrx_handler - process an ingress ethernet packet
1554 * @q: the response queue that received the packet
1555 * @rsp: the response queue descriptor holding the RX_PKT message
1556 * @si: the gather list of packet fragments
1557 *
1558 * Process an ingress ethernet packet and deliver it to the stack.
1559 */
1560int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
1561 const struct pkt_gl *si)
1562{
1563 bool csum_ok;
1564 struct sk_buff *skb;
1565 struct port_info *pi;
1566 const struct cpl_rx_pkt *pkt;
1567 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1568
1569 if (unlikely(*(u8 *)rsp == CPL_TRACE_PKT))
1570 return handle_trace_pkt(q->adap, si);
1571
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001572 pkt = (const struct cpl_rx_pkt *)rsp;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001573 csum_ok = pkt->csum_calc && !pkt->err_vec;
1574 if ((pkt->l2info & htonl(RXF_TCP)) &&
1575 (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
1576 do_gro(rxq, si, pkt);
1577 return 0;
1578 }
1579
1580 skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
1581 if (unlikely(!skb)) {
1582 t4_pktgl_free(si);
1583 rxq->stats.rx_drops++;
1584 return 0;
1585 }
1586
1587 __skb_pull(skb, RX_PKT_PAD); /* remove ethernet header padding */
1588 skb->protocol = eth_type_trans(skb, q->netdev);
1589 skb_record_rx_queue(skb, q->idx);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001590 if (skb->dev->features & NETIF_F_RXHASH)
1591 skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
1592
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001593 pi = netdev_priv(skb->dev);
1594 rxq->stats.pkts++;
1595
1596 if (csum_ok && (pi->rx_offload & RX_CSO) &&
1597 (pkt->l2info & htonl(RXF_UDP | RXF_TCP))) {
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00001598 if (!pkt->ip_frag) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001599 skb->ip_summed = CHECKSUM_UNNECESSARY;
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00001600 rxq->stats.rx_cso++;
1601 } else if (pkt->l2info & htonl(RXF_IP)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001602 __sum16 c = (__force __sum16)pkt->csum;
1603 skb->csum = csum_unfold(c);
1604 skb->ip_summed = CHECKSUM_COMPLETE;
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00001605 rxq->stats.rx_cso++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001606 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001607 } else
1608 skb->ip_summed = CHECKSUM_NONE;
1609
1610 if (unlikely(pkt->vlan_ex)) {
1611 struct vlan_group *grp = pi->vlan_grp;
1612
1613 rxq->stats.vlan_ex++;
1614 if (likely(grp))
1615 vlan_hwaccel_receive_skb(skb, grp, ntohs(pkt->vlan));
1616 else
1617 dev_kfree_skb_any(skb);
1618 } else
1619 netif_receive_skb(skb);
1620
1621 return 0;
1622}
1623
1624/**
1625 * restore_rx_bufs - put back a packet's Rx buffers
1626 * @si: the packet gather list
1627 * @q: the SGE free list
1628 * @frags: number of FL buffers to restore
1629 *
1630 * Puts back on an FL the Rx buffers associated with @si. The buffers
1631 * have already been unmapped and are left unmapped, we mark them so to
1632 * prevent further unmapping attempts.
1633 *
1634 * This function undoes a series of @unmap_rx_buf calls when we find out
1635 * that the current packet can't be processed right away afterall and we
1636 * need to come back to it later. This is a very rare event and there's
1637 * no effort to make this particularly efficient.
1638 */
1639static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
1640 int frags)
1641{
1642 struct rx_sw_desc *d;
1643
1644 while (frags--) {
1645 if (q->cidx == 0)
1646 q->cidx = q->size - 1;
1647 else
1648 q->cidx--;
1649 d = &q->sdesc[q->cidx];
1650 d->page = si->frags[frags].page;
1651 d->dma_addr |= RX_UNMAPPED_BUF;
1652 q->avail++;
1653 }
1654}
1655
1656/**
1657 * is_new_response - check if a response is newly written
1658 * @r: the response descriptor
1659 * @q: the response queue
1660 *
1661 * Returns true if a response descriptor contains a yet unprocessed
1662 * response.
1663 */
1664static inline bool is_new_response(const struct rsp_ctrl *r,
1665 const struct sge_rspq *q)
1666{
1667 return RSPD_GEN(r->type_gen) == q->gen;
1668}
1669
1670/**
1671 * rspq_next - advance to the next entry in a response queue
1672 * @q: the queue
1673 *
1674 * Updates the state of a response queue to advance it to the next entry.
1675 */
1676static inline void rspq_next(struct sge_rspq *q)
1677{
1678 q->cur_desc = (void *)q->cur_desc + q->iqe_len;
1679 if (unlikely(++q->cidx == q->size)) {
1680 q->cidx = 0;
1681 q->gen ^= 1;
1682 q->cur_desc = q->desc;
1683 }
1684}
1685
1686/**
1687 * process_responses - process responses from an SGE response queue
1688 * @q: the ingress queue to process
1689 * @budget: how many responses can be processed in this round
1690 *
1691 * Process responses from an SGE response queue up to the supplied budget.
1692 * Responses include received packets as well as control messages from FW
1693 * or HW.
1694 *
1695 * Additionally choose the interrupt holdoff time for the next interrupt
1696 * on this queue. If the system is under memory shortage use a fairly
1697 * long delay to help recovery.
1698 */
1699static int process_responses(struct sge_rspq *q, int budget)
1700{
1701 int ret, rsp_type;
1702 int budget_left = budget;
1703 const struct rsp_ctrl *rc;
1704 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1705
1706 while (likely(budget_left)) {
1707 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
1708 if (!is_new_response(rc, q))
1709 break;
1710
1711 rmb();
1712 rsp_type = RSPD_TYPE(rc->type_gen);
1713 if (likely(rsp_type == RSP_TYPE_FLBUF)) {
1714 skb_frag_t *fp;
1715 struct pkt_gl si;
1716 const struct rx_sw_desc *rsd;
1717 u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
1718
1719 if (len & RSPD_NEWBUF) {
1720 if (likely(q->offset > 0)) {
1721 free_rx_bufs(q->adap, &rxq->fl, 1);
1722 q->offset = 0;
1723 }
Casey Leedom1704d742010-06-25 12:09:38 +00001724 len = RSPD_LEN(len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001725 }
1726 si.tot_len = len;
1727
1728 /* gather packet fragments */
1729 for (frags = 0, fp = si.frags; ; frags++, fp++) {
1730 rsd = &rxq->fl.sdesc[rxq->fl.cidx];
1731 bufsz = get_buf_size(rsd);
1732 fp->page = rsd->page;
1733 fp->page_offset = q->offset;
1734 fp->size = min(bufsz, len);
1735 len -= fp->size;
1736 if (!len)
1737 break;
1738 unmap_rx_buf(q->adap, &rxq->fl);
1739 }
1740
1741 /*
1742 * Last buffer remains mapped so explicitly make it
1743 * coherent for CPU access.
1744 */
1745 dma_sync_single_for_cpu(q->adap->pdev_dev,
1746 get_buf_addr(rsd),
1747 fp->size, DMA_FROM_DEVICE);
1748
1749 si.va = page_address(si.frags[0].page) +
1750 si.frags[0].page_offset;
1751 prefetch(si.va);
1752
1753 si.nfrags = frags + 1;
1754 ret = q->handler(q, q->cur_desc, &si);
1755 if (likely(ret == 0))
1756 q->offset += ALIGN(fp->size, FL_ALIGN);
1757 else
1758 restore_rx_bufs(&si, &rxq->fl, frags);
1759 } else if (likely(rsp_type == RSP_TYPE_CPL)) {
1760 ret = q->handler(q, q->cur_desc, NULL);
1761 } else {
1762 ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
1763 }
1764
1765 if (unlikely(ret)) {
1766 /* couldn't process descriptor, back off for recovery */
1767 q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
1768 break;
1769 }
1770
1771 rspq_next(q);
1772 budget_left--;
1773 }
1774
1775 if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
1776 __refill_fl(q->adap, &rxq->fl);
1777 return budget - budget_left;
1778}
1779
1780/**
1781 * napi_rx_handler - the NAPI handler for Rx processing
1782 * @napi: the napi instance
1783 * @budget: how many packets we can process in this round
1784 *
1785 * Handler for new data events when using NAPI. This does not need any
1786 * locking or protection from interrupts as data interrupts are off at
1787 * this point and other adapter interrupts do not interfere (the latter
1788 * in not a concern at all with MSI-X as non-data interrupts then have
1789 * a separate handler).
1790 */
1791static int napi_rx_handler(struct napi_struct *napi, int budget)
1792{
1793 unsigned int params;
1794 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
1795 int work_done = process_responses(q, budget);
1796
1797 if (likely(work_done < budget)) {
1798 napi_complete(napi);
1799 params = q->next_intr_params;
1800 q->next_intr_params = q->intr_params;
1801 } else
1802 params = QINTR_TIMER_IDX(7);
1803
1804 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS), CIDXINC(work_done) |
1805 INGRESSQID((u32)q->cntxt_id) | SEINTARM(params));
1806 return work_done;
1807}
1808
1809/*
1810 * The MSI-X interrupt handler for an SGE response queue.
1811 */
1812irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
1813{
1814 struct sge_rspq *q = cookie;
1815
1816 napi_schedule(&q->napi);
1817 return IRQ_HANDLED;
1818}
1819
1820/*
1821 * Process the indirect interrupt entries in the interrupt queue and kick off
1822 * NAPI for each queue that has generated an entry.
1823 */
1824static unsigned int process_intrq(struct adapter *adap)
1825{
1826 unsigned int credits;
1827 const struct rsp_ctrl *rc;
1828 struct sge_rspq *q = &adap->sge.intrq;
1829
1830 spin_lock(&adap->sge.intrq_lock);
1831 for (credits = 0; ; credits++) {
1832 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
1833 if (!is_new_response(rc, q))
1834 break;
1835
1836 rmb();
1837 if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
1838 unsigned int qid = ntohl(rc->pldbuflen_qid);
1839
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001840 qid -= adap->sge.ingr_start;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001841 napi_schedule(&adap->sge.ingr_map[qid]->napi);
1842 }
1843
1844 rspq_next(q);
1845 }
1846
1847 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS), CIDXINC(credits) |
1848 INGRESSQID(q->cntxt_id) | SEINTARM(q->intr_params));
1849 spin_unlock(&adap->sge.intrq_lock);
1850 return credits;
1851}
1852
1853/*
1854 * The MSI interrupt handler, which handles data events from SGE response queues
1855 * as well as error and other async events as they all use the same MSI vector.
1856 */
1857static irqreturn_t t4_intr_msi(int irq, void *cookie)
1858{
1859 struct adapter *adap = cookie;
1860
1861 t4_slow_intr_handler(adap);
1862 process_intrq(adap);
1863 return IRQ_HANDLED;
1864}
1865
1866/*
1867 * Interrupt handler for legacy INTx interrupts.
1868 * Handles data events from SGE response queues as well as error and other
1869 * async events as they all use the same interrupt line.
1870 */
1871static irqreturn_t t4_intr_intx(int irq, void *cookie)
1872{
1873 struct adapter *adap = cookie;
1874
1875 t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI), 0);
1876 if (t4_slow_intr_handler(adap) | process_intrq(adap))
1877 return IRQ_HANDLED;
1878 return IRQ_NONE; /* probably shared interrupt */
1879}
1880
1881/**
1882 * t4_intr_handler - select the top-level interrupt handler
1883 * @adap: the adapter
1884 *
1885 * Selects the top-level interrupt handler based on the type of interrupts
1886 * (MSI-X, MSI, or INTx).
1887 */
1888irq_handler_t t4_intr_handler(struct adapter *adap)
1889{
1890 if (adap->flags & USING_MSIX)
1891 return t4_sge_intr_msix;
1892 if (adap->flags & USING_MSI)
1893 return t4_intr_msi;
1894 return t4_intr_intx;
1895}
1896
1897static void sge_rx_timer_cb(unsigned long data)
1898{
1899 unsigned long m;
1900 unsigned int i, cnt[2];
1901 struct adapter *adap = (struct adapter *)data;
1902 struct sge *s = &adap->sge;
1903
1904 for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++)
1905 for (m = s->starving_fl[i]; m; m &= m - 1) {
1906 struct sge_eth_rxq *rxq;
1907 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
1908 struct sge_fl *fl = s->egr_map[id];
1909
1910 clear_bit(id, s->starving_fl);
1911 smp_mb__after_clear_bit();
1912
1913 if (fl_starving(fl)) {
1914 rxq = container_of(fl, struct sge_eth_rxq, fl);
1915 if (napi_reschedule(&rxq->rspq.napi))
1916 fl->starving++;
1917 else
1918 set_bit(id, s->starving_fl);
1919 }
1920 }
1921
1922 t4_write_reg(adap, SGE_DEBUG_INDEX, 13);
1923 cnt[0] = t4_read_reg(adap, SGE_DEBUG_DATA_HIGH);
1924 cnt[1] = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
1925
1926 for (i = 0; i < 2; i++)
1927 if (cnt[i] >= s->starve_thres) {
1928 if (s->idma_state[i] || cnt[i] == 0xffffffff)
1929 continue;
1930 s->idma_state[i] = 1;
1931 t4_write_reg(adap, SGE_DEBUG_INDEX, 11);
1932 m = t4_read_reg(adap, SGE_DEBUG_DATA_LOW) >> (i * 16);
1933 dev_warn(adap->pdev_dev,
1934 "SGE idma%u starvation detected for "
1935 "queue %lu\n", i, m & 0xffff);
1936 } else if (s->idma_state[i])
1937 s->idma_state[i] = 0;
1938
1939 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
1940}
1941
1942static void sge_tx_timer_cb(unsigned long data)
1943{
1944 unsigned long m;
1945 unsigned int i, budget;
1946 struct adapter *adap = (struct adapter *)data;
1947 struct sge *s = &adap->sge;
1948
1949 for (i = 0; i < ARRAY_SIZE(s->txq_maperr); i++)
1950 for (m = s->txq_maperr[i]; m; m &= m - 1) {
1951 unsigned long id = __ffs(m) + i * BITS_PER_LONG;
1952 struct sge_ofld_txq *txq = s->egr_map[id];
1953
1954 clear_bit(id, s->txq_maperr);
1955 tasklet_schedule(&txq->qresume_tsk);
1956 }
1957
1958 budget = MAX_TIMER_TX_RECLAIM;
1959 i = s->ethtxq_rover;
1960 do {
1961 struct sge_eth_txq *q = &s->ethtxq[i];
1962
1963 if (q->q.in_use &&
1964 time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
1965 __netif_tx_trylock(q->txq)) {
1966 int avail = reclaimable(&q->q);
1967
1968 if (avail) {
1969 if (avail > budget)
1970 avail = budget;
1971
1972 free_tx_desc(adap, &q->q, avail, true);
1973 q->q.in_use -= avail;
1974 budget -= avail;
1975 }
1976 __netif_tx_unlock(q->txq);
1977 }
1978
1979 if (++i >= s->ethqsets)
1980 i = 0;
1981 } while (budget && i != s->ethtxq_rover);
1982 s->ethtxq_rover = i;
1983 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
1984}
1985
1986int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
1987 struct net_device *dev, int intr_idx,
1988 struct sge_fl *fl, rspq_handler_t hnd)
1989{
1990 int ret, flsz = 0;
1991 struct fw_iq_cmd c;
1992 struct port_info *pi = netdev_priv(dev);
1993
1994 /* Size needs to be multiple of 16, including status entry. */
1995 iq->size = roundup(iq->size, 16);
1996
1997 iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
1998 &iq->phys_addr, NULL, 0);
1999 if (!iq->desc)
2000 return -ENOMEM;
2001
2002 memset(&c, 0, sizeof(c));
2003 c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
2004 FW_CMD_WRITE | FW_CMD_EXEC |
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002005 FW_IQ_CMD_PFN(adap->fn) | FW_IQ_CMD_VFN(0));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002006 c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC | FW_IQ_CMD_IQSTART(1) |
2007 FW_LEN16(c));
2008 c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
2009 FW_IQ_CMD_IQASYNCH(fwevtq) | FW_IQ_CMD_VIID(pi->viid) |
2010 FW_IQ_CMD_IQANDST(intr_idx < 0) | FW_IQ_CMD_IQANUD(1) |
2011 FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
2012 -intr_idx - 1));
2013 c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
2014 FW_IQ_CMD_IQGTSMODE |
2015 FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
2016 FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
2017 c.iqsize = htons(iq->size);
2018 c.iqaddr = cpu_to_be64(iq->phys_addr);
2019
2020 if (fl) {
2021 fl->size = roundup(fl->size, 8);
2022 fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
2023 sizeof(struct rx_sw_desc), &fl->addr,
2024 &fl->sdesc, STAT_LEN);
2025 if (!fl->desc)
2026 goto fl_nomem;
2027
2028 flsz = fl->size / 8 + STAT_LEN / sizeof(struct tx_desc);
2029 c.iqns_to_fl0congen = htonl(FW_IQ_CMD_FL0PACKEN |
2030 FW_IQ_CMD_FL0PADEN);
2031 c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN(2) |
2032 FW_IQ_CMD_FL0FBMAX(3));
2033 c.fl0size = htons(flsz);
2034 c.fl0addr = cpu_to_be64(fl->addr);
2035 }
2036
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002037 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002038 if (ret)
2039 goto err;
2040
2041 netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
2042 iq->cur_desc = iq->desc;
2043 iq->cidx = 0;
2044 iq->gen = 1;
2045 iq->next_intr_params = iq->intr_params;
2046 iq->cntxt_id = ntohs(c.iqid);
2047 iq->abs_id = ntohs(c.physiqid);
2048 iq->size--; /* subtract status entry */
2049 iq->adap = adap;
2050 iq->netdev = dev;
2051 iq->handler = hnd;
2052
2053 /* set offset to -1 to distinguish ingress queues without FL */
2054 iq->offset = fl ? 0 : -1;
2055
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002056 adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002057
2058 if (fl) {
Roland Dreier62718b32010-04-21 08:09:21 +00002059 fl->cntxt_id = ntohs(c.fl0id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002060 fl->avail = fl->pend_cred = 0;
2061 fl->pidx = fl->cidx = 0;
2062 fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002063 adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002064 refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
2065 }
2066 return 0;
2067
2068fl_nomem:
2069 ret = -ENOMEM;
2070err:
2071 if (iq->desc) {
2072 dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
2073 iq->desc, iq->phys_addr);
2074 iq->desc = NULL;
2075 }
2076 if (fl && fl->desc) {
2077 kfree(fl->sdesc);
2078 fl->sdesc = NULL;
2079 dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
2080 fl->desc, fl->addr);
2081 fl->desc = NULL;
2082 }
2083 return ret;
2084}
2085
2086static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
2087{
2088 q->in_use = 0;
2089 q->cidx = q->pidx = 0;
2090 q->stops = q->restarts = 0;
2091 q->stat = (void *)&q->desc[q->size];
2092 q->cntxt_id = id;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002093 adap->sge.egr_map[id - adap->sge.egr_start] = q;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002094}
2095
2096int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
2097 struct net_device *dev, struct netdev_queue *netdevq,
2098 unsigned int iqid)
2099{
2100 int ret, nentries;
2101 struct fw_eq_eth_cmd c;
2102 struct port_info *pi = netdev_priv(dev);
2103
2104 /* Add status entries */
2105 nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
2106
2107 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2108 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
2109 &txq->q.phys_addr, &txq->q.sdesc, STAT_LEN);
2110 if (!txq->q.desc)
2111 return -ENOMEM;
2112
2113 memset(&c, 0, sizeof(c));
2114 c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
2115 FW_CMD_WRITE | FW_CMD_EXEC |
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002116 FW_EQ_ETH_CMD_PFN(adap->fn) | FW_EQ_ETH_CMD_VFN(0));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002117 c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC |
2118 FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
2119 c.viid_pkd = htonl(FW_EQ_ETH_CMD_VIID(pi->viid));
2120 c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE(2) |
2121 FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
2122 FW_EQ_ETH_CMD_IQID(iqid));
2123 c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN(2) |
2124 FW_EQ_ETH_CMD_FBMAX(3) |
2125 FW_EQ_ETH_CMD_CIDXFTHRESH(5) |
2126 FW_EQ_ETH_CMD_EQSIZE(nentries));
2127 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2128
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002129 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002130 if (ret) {
2131 kfree(txq->q.sdesc);
2132 txq->q.sdesc = NULL;
2133 dma_free_coherent(adap->pdev_dev,
2134 nentries * sizeof(struct tx_desc),
2135 txq->q.desc, txq->q.phys_addr);
2136 txq->q.desc = NULL;
2137 return ret;
2138 }
2139
2140 init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_GET(ntohl(c.eqid_pkd)));
2141 txq->txq = netdevq;
2142 txq->tso = txq->tx_cso = txq->vlan_ins = 0;
2143 txq->mapping_err = 0;
2144 return 0;
2145}
2146
2147int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
2148 struct net_device *dev, unsigned int iqid,
2149 unsigned int cmplqid)
2150{
2151 int ret, nentries;
2152 struct fw_eq_ctrl_cmd c;
2153 struct port_info *pi = netdev_priv(dev);
2154
2155 /* Add status entries */
2156 nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
2157
2158 txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
2159 sizeof(struct tx_desc), 0, &txq->q.phys_addr,
2160 NULL, 0);
2161 if (!txq->q.desc)
2162 return -ENOMEM;
2163
2164 c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
2165 FW_CMD_WRITE | FW_CMD_EXEC |
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002166 FW_EQ_CTRL_CMD_PFN(adap->fn) |
2167 FW_EQ_CTRL_CMD_VFN(0));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002168 c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC |
2169 FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
2170 c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID(cmplqid));
2171 c.physeqid_pkd = htonl(0);
2172 c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE(2) |
2173 FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
2174 FW_EQ_CTRL_CMD_IQID(iqid));
2175 c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN(2) |
2176 FW_EQ_CTRL_CMD_FBMAX(3) |
2177 FW_EQ_CTRL_CMD_CIDXFTHRESH(5) |
2178 FW_EQ_CTRL_CMD_EQSIZE(nentries));
2179 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2180
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002181 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002182 if (ret) {
2183 dma_free_coherent(adap->pdev_dev,
2184 nentries * sizeof(struct tx_desc),
2185 txq->q.desc, txq->q.phys_addr);
2186 txq->q.desc = NULL;
2187 return ret;
2188 }
2189
2190 init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_GET(ntohl(c.cmpliqid_eqid)));
2191 txq->adap = adap;
2192 skb_queue_head_init(&txq->sendq);
2193 tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
2194 txq->full = 0;
2195 return 0;
2196}
2197
2198int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
2199 struct net_device *dev, unsigned int iqid)
2200{
2201 int ret, nentries;
2202 struct fw_eq_ofld_cmd c;
2203 struct port_info *pi = netdev_priv(dev);
2204
2205 /* Add status entries */
2206 nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
2207
2208 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2209 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
2210 &txq->q.phys_addr, &txq->q.sdesc, STAT_LEN);
2211 if (!txq->q.desc)
2212 return -ENOMEM;
2213
2214 memset(&c, 0, sizeof(c));
2215 c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
2216 FW_CMD_WRITE | FW_CMD_EXEC |
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002217 FW_EQ_OFLD_CMD_PFN(adap->fn) |
2218 FW_EQ_OFLD_CMD_VFN(0));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002219 c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC |
2220 FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
2221 c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE(2) |
2222 FW_EQ_OFLD_CMD_PCIECHN(pi->tx_chan) |
2223 FW_EQ_OFLD_CMD_IQID(iqid));
2224 c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN(2) |
2225 FW_EQ_OFLD_CMD_FBMAX(3) |
2226 FW_EQ_OFLD_CMD_CIDXFTHRESH(5) |
2227 FW_EQ_OFLD_CMD_EQSIZE(nentries));
2228 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2229
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002230 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002231 if (ret) {
2232 kfree(txq->q.sdesc);
2233 txq->q.sdesc = NULL;
2234 dma_free_coherent(adap->pdev_dev,
2235 nentries * sizeof(struct tx_desc),
2236 txq->q.desc, txq->q.phys_addr);
2237 txq->q.desc = NULL;
2238 return ret;
2239 }
2240
2241 init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_GET(ntohl(c.eqid_pkd)));
2242 txq->adap = adap;
2243 skb_queue_head_init(&txq->sendq);
2244 tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
2245 txq->full = 0;
2246 txq->mapping_err = 0;
2247 return 0;
2248}
2249
2250static void free_txq(struct adapter *adap, struct sge_txq *q)
2251{
2252 dma_free_coherent(adap->pdev_dev,
2253 q->size * sizeof(struct tx_desc) + STAT_LEN,
2254 q->desc, q->phys_addr);
2255 q->cntxt_id = 0;
2256 q->sdesc = NULL;
2257 q->desc = NULL;
2258}
2259
2260static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
2261 struct sge_fl *fl)
2262{
2263 unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
2264
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002265 adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002266 t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
2267 rq->cntxt_id, fl_id, 0xffff);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002268 dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
2269 rq->desc, rq->phys_addr);
2270 netif_napi_del(&rq->napi);
2271 rq->netdev = NULL;
2272 rq->cntxt_id = rq->abs_id = 0;
2273 rq->desc = NULL;
2274
2275 if (fl) {
2276 free_rx_bufs(adap, fl, fl->avail);
2277 dma_free_coherent(adap->pdev_dev, fl->size * 8 + STAT_LEN,
2278 fl->desc, fl->addr);
2279 kfree(fl->sdesc);
2280 fl->sdesc = NULL;
2281 fl->cntxt_id = 0;
2282 fl->desc = NULL;
2283 }
2284}
2285
2286/**
2287 * t4_free_sge_resources - free SGE resources
2288 * @adap: the adapter
2289 *
2290 * Frees resources used by the SGE queue sets.
2291 */
2292void t4_free_sge_resources(struct adapter *adap)
2293{
2294 int i;
2295 struct sge_eth_rxq *eq = adap->sge.ethrxq;
2296 struct sge_eth_txq *etq = adap->sge.ethtxq;
2297 struct sge_ofld_rxq *oq = adap->sge.ofldrxq;
2298
2299 /* clean up Ethernet Tx/Rx queues */
2300 for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
2301 if (eq->rspq.desc)
2302 free_rspq_fl(adap, &eq->rspq, &eq->fl);
2303 if (etq->q.desc) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002304 t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
2305 etq->q.cntxt_id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002306 free_tx_desc(adap, &etq->q, etq->q.in_use, true);
2307 kfree(etq->q.sdesc);
2308 free_txq(adap, &etq->q);
2309 }
2310 }
2311
2312 /* clean up RDMA and iSCSI Rx queues */
2313 for (i = 0; i < adap->sge.ofldqsets; i++, oq++) {
2314 if (oq->rspq.desc)
2315 free_rspq_fl(adap, &oq->rspq, &oq->fl);
2316 }
2317 for (i = 0, oq = adap->sge.rdmarxq; i < adap->sge.rdmaqs; i++, oq++) {
2318 if (oq->rspq.desc)
2319 free_rspq_fl(adap, &oq->rspq, &oq->fl);
2320 }
2321
2322 /* clean up offload Tx queues */
2323 for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
2324 struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
2325
2326 if (q->q.desc) {
2327 tasklet_kill(&q->qresume_tsk);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002328 t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
2329 q->q.cntxt_id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002330 free_tx_desc(adap, &q->q, q->q.in_use, false);
2331 kfree(q->q.sdesc);
2332 __skb_queue_purge(&q->sendq);
2333 free_txq(adap, &q->q);
2334 }
2335 }
2336
2337 /* clean up control Tx queues */
2338 for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
2339 struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
2340
2341 if (cq->q.desc) {
2342 tasklet_kill(&cq->qresume_tsk);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002343 t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
2344 cq->q.cntxt_id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002345 __skb_queue_purge(&cq->sendq);
2346 free_txq(adap, &cq->q);
2347 }
2348 }
2349
2350 if (adap->sge.fw_evtq.desc)
2351 free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
2352
2353 if (adap->sge.intrq.desc)
2354 free_rspq_fl(adap, &adap->sge.intrq, NULL);
2355
2356 /* clear the reverse egress queue map */
2357 memset(adap->sge.egr_map, 0, sizeof(adap->sge.egr_map));
2358}
2359
2360void t4_sge_start(struct adapter *adap)
2361{
2362 adap->sge.ethtxq_rover = 0;
2363 mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2364 mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2365}
2366
2367/**
2368 * t4_sge_stop - disable SGE operation
2369 * @adap: the adapter
2370 *
2371 * Stop tasklets and timers associated with the DMA engine. Note that
2372 * this is effective only if measures have been taken to disable any HW
2373 * events that may restart them.
2374 */
2375void t4_sge_stop(struct adapter *adap)
2376{
2377 int i;
2378 struct sge *s = &adap->sge;
2379
2380 if (in_interrupt()) /* actions below require waiting */
2381 return;
2382
2383 if (s->rx_timer.function)
2384 del_timer_sync(&s->rx_timer);
2385 if (s->tx_timer.function)
2386 del_timer_sync(&s->tx_timer);
2387
2388 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
2389 struct sge_ofld_txq *q = &s->ofldtxq[i];
2390
2391 if (q->q.desc)
2392 tasklet_kill(&q->qresume_tsk);
2393 }
2394 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
2395 struct sge_ctrl_txq *cq = &s->ctrlq[i];
2396
2397 if (cq->q.desc)
2398 tasklet_kill(&cq->qresume_tsk);
2399 }
2400}
2401
2402/**
2403 * t4_sge_init - initialize SGE
2404 * @adap: the adapter
2405 *
2406 * Performs SGE initialization needed every time after a chip reset.
2407 * We do not initialize any of the queues here, instead the driver
2408 * top-level must request them individually.
2409 */
2410void t4_sge_init(struct adapter *adap)
2411{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002412 unsigned int i, v;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002413 struct sge *s = &adap->sge;
2414 unsigned int fl_align_log = ilog2(FL_ALIGN);
2415
2416 t4_set_reg_field(adap, SGE_CONTROL, PKTSHIFT_MASK |
2417 INGPADBOUNDARY_MASK | EGRSTATUSPAGESIZE,
2418 INGPADBOUNDARY(fl_align_log - 5) | PKTSHIFT(2) |
2419 RXPKTCPLMODE |
2420 (STAT_LEN == 128 ? EGRSTATUSPAGESIZE : 0));
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002421
2422 for (i = v = 0; i < 32; i += 4)
2423 v |= (PAGE_SHIFT - 10) << i;
2424 t4_write_reg(adap, SGE_HOST_PAGE_SIZE, v);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002425 t4_write_reg(adap, SGE_FL_BUFFER_SIZE0, PAGE_SIZE);
2426#if FL_PG_ORDER > 0
2427 t4_write_reg(adap, SGE_FL_BUFFER_SIZE1, PAGE_SIZE << FL_PG_ORDER);
2428#endif
2429 t4_write_reg(adap, SGE_INGRESS_RX_THRESHOLD,
2430 THRESHOLD_0(s->counter_val[0]) |
2431 THRESHOLD_1(s->counter_val[1]) |
2432 THRESHOLD_2(s->counter_val[2]) |
2433 THRESHOLD_3(s->counter_val[3]));
2434 t4_write_reg(adap, SGE_TIMER_VALUE_0_AND_1,
2435 TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[0])) |
2436 TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[1])));
2437 t4_write_reg(adap, SGE_TIMER_VALUE_2_AND_3,
2438 TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[2])) |
2439 TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[3])));
2440 t4_write_reg(adap, SGE_TIMER_VALUE_4_AND_5,
2441 TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[4])) |
2442 TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[5])));
2443 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
2444 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
2445 s->starve_thres = core_ticks_per_usec(adap) * 1000000; /* 1 s */
2446 s->idma_state[0] = s->idma_state[1] = 0;
2447 spin_lock_init(&s->intrq_lock);
2448}