Merge branch 'gianfar'
[deliverable/linux.git] / drivers / net / ethernet / chelsio / cxgb4 / sge.c
CommitLineData
fd3a4790
DM
1/*
2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
3 *
ce100b8b 4 * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
fd3a4790
DM
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>
70c71606 42#include <linux/prefetch.h>
ee40fa06 43#include <linux/export.h>
fd3a4790
DM
44#include <net/ipv6.h>
45#include <net/tcp.h>
46#include "cxgb4.h"
47#include "t4_regs.h"
48#include "t4_msg.h"
49#include "t4fw_api.h"
50
51/*
52 * Rx buffer size. We use largish buffers if possible but settle for single
53 * pages under memory shortage.
54 */
55#if PAGE_SHIFT >= 16
56# define FL_PG_ORDER 0
57#else
58# define FL_PG_ORDER (16 - PAGE_SHIFT)
59#endif
60
61/* RX_PULL_LEN should be <= RX_COPY_THRES */
62#define RX_COPY_THRES 256
63#define RX_PULL_LEN 128
64
65/*
66 * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
67 * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
68 */
69#define RX_PKT_SKB_LEN 512
70
fd3a4790
DM
71/*
72 * Max number of Tx descriptors we clean up at a time. Should be modest as
73 * freeing skbs isn't cheap and it happens while holding locks. We just need
74 * to free packets faster than they arrive, we eventually catch up and keep
75 * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
76 */
77#define MAX_TX_RECLAIM 16
78
79/*
80 * Max number of Rx buffers we replenish at a time. Again keep this modest,
81 * allocating buffers isn't cheap either.
82 */
83#define MAX_RX_REFILL 16U
84
85/*
86 * Period of the Rx queue check timer. This timer is infrequent as it has
87 * something to do only when the system experiences severe memory shortage.
88 */
89#define RX_QCHECK_PERIOD (HZ / 2)
90
91/*
92 * Period of the Tx queue check timer.
93 */
94#define TX_QCHECK_PERIOD (HZ / 2)
95
0f4d201f
KS
96/* SGE Hung Ingress DMA Threshold Warning time (in Hz) and Warning Repeat Rate
97 * (in RX_QCHECK_PERIOD multiples). If we find one of the SGE Ingress DMA
98 * State Machines in the same state for this amount of time (in HZ) then we'll
99 * issue a warning about a potential hang. We'll repeat the warning as the
100 * SGE Ingress DMA Channel appears to be hung every N RX_QCHECK_PERIODs till
101 * the situation clears. If the situation clears, we'll note that as well.
102 */
103#define SGE_IDMA_WARN_THRESH (1 * HZ)
104#define SGE_IDMA_WARN_REPEAT (20 * RX_QCHECK_PERIOD)
105
fd3a4790
DM
106/*
107 * Max number of Tx descriptors to be reclaimed by the Tx timer.
108 */
109#define MAX_TIMER_TX_RECLAIM 100
110
111/*
112 * Timer index used when backing off due to memory shortage.
113 */
114#define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
115
116/*
117 * An FL with <= FL_STARVE_THRES buffers is starving and a periodic timer will
118 * attempt to refill it.
119 */
120#define FL_STARVE_THRES 4
121
122/*
123 * Suspend an Ethernet Tx queue with fewer available descriptors than this.
124 * This is the same as calc_tx_descs() for a TSO packet with
125 * nr_frags == MAX_SKB_FRAGS.
126 */
127#define ETHTXQ_STOP_THRES \
128 (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
129
130/*
131 * Suspension threshold for non-Ethernet Tx queues. We require enough room
132 * for a full sized WR.
133 */
134#define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
135
136/*
137 * Max Tx descriptor space we allow for an Ethernet packet to be inlined
138 * into a WR.
139 */
140#define MAX_IMM_TX_PKT_LEN 128
141
142/*
143 * Max size of a WR sent through a control Tx queue.
144 */
145#define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
146
fd3a4790
DM
147struct tx_sw_desc { /* SW state per Tx descriptor */
148 struct sk_buff *skb;
149 struct ulptx_sgl *sgl;
150};
151
152struct rx_sw_desc { /* SW state per Rx descriptor */
153 struct page *page;
154 dma_addr_t dma_addr;
155};
156
157/*
52367a76
VP
158 * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
159 * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
160 * We could easily support more but there doesn't seem to be much need for
161 * that ...
162 */
163#define FL_MTU_SMALL 1500
164#define FL_MTU_LARGE 9000
165
166static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
167 unsigned int mtu)
168{
169 struct sge *s = &adapter->sge;
170
171 return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
172}
173
174#define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
175#define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
176
177/*
178 * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
179 * these to specify the buffer size as an index into the SGE Free List Buffer
180 * Size register array. We also use bit 4, when the buffer has been unmapped
181 * for DMA, but this is of course never sent to the hardware and is only used
182 * to prevent double unmappings. All of the above requires that the Free List
183 * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
184 * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
185 * Free List Buffer alignment is 32 bytes, this works out for us ...
fd3a4790
DM
186 */
187enum {
52367a76
VP
188 RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
189 RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
190 RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
191
192 /*
193 * XXX We shouldn't depend on being able to use these indices.
194 * XXX Especially when some other Master PF has initialized the
195 * XXX adapter or we use the Firmware Configuration File. We
196 * XXX should really search through the Host Buffer Size register
197 * XXX array for the appropriately sized buffer indices.
198 */
199 RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
200 RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
201
202 RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
203 RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
fd3a4790
DM
204};
205
e553ec3f
HS
206static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
207#define MIN_NAPI_WORK 1
208
fd3a4790
DM
209static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
210{
52367a76 211 return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
fd3a4790
DM
212}
213
214static inline bool is_buf_mapped(const struct rx_sw_desc *d)
215{
216 return !(d->dma_addr & RX_UNMAPPED_BUF);
217}
218
219/**
220 * txq_avail - return the number of available slots in a Tx queue
221 * @q: the Tx queue
222 *
223 * Returns the number of descriptors in a Tx queue available to write new
224 * packets.
225 */
226static inline unsigned int txq_avail(const struct sge_txq *q)
227{
228 return q->size - 1 - q->in_use;
229}
230
231/**
232 * fl_cap - return the capacity of a free-buffer list
233 * @fl: the FL
234 *
235 * Returns the capacity of a free-buffer list. The capacity is less than
236 * the size because one descriptor needs to be left unpopulated, otherwise
237 * HW will think the FL is empty.
238 */
239static inline unsigned int fl_cap(const struct sge_fl *fl)
240{
241 return fl->size - 8; /* 1 descriptor = 8 buffers */
242}
243
244static inline bool fl_starving(const struct sge_fl *fl)
245{
246 return fl->avail - fl->pend_cred <= FL_STARVE_THRES;
247}
248
249static int map_skb(struct device *dev, const struct sk_buff *skb,
250 dma_addr_t *addr)
251{
252 const skb_frag_t *fp, *end;
253 const struct skb_shared_info *si;
254
255 *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
256 if (dma_mapping_error(dev, *addr))
257 goto out_err;
258
259 si = skb_shinfo(skb);
260 end = &si->frags[si->nr_frags];
261
262 for (fp = si->frags; fp < end; fp++) {
e91b0f24
IC
263 *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
264 DMA_TO_DEVICE);
fd3a4790
DM
265 if (dma_mapping_error(dev, *addr))
266 goto unwind;
267 }
268 return 0;
269
270unwind:
271 while (fp-- > si->frags)
9e903e08 272 dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
fd3a4790
DM
273
274 dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
275out_err:
276 return -ENOMEM;
277}
278
279#ifdef CONFIG_NEED_DMA_MAP_STATE
280static void unmap_skb(struct device *dev, const struct sk_buff *skb,
281 const dma_addr_t *addr)
282{
283 const skb_frag_t *fp, *end;
284 const struct skb_shared_info *si;
285
286 dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
287
288 si = skb_shinfo(skb);
289 end = &si->frags[si->nr_frags];
290 for (fp = si->frags; fp < end; fp++)
9e903e08 291 dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
fd3a4790
DM
292}
293
294/**
295 * deferred_unmap_destructor - unmap a packet when it is freed
296 * @skb: the packet
297 *
298 * This is the packet destructor used for Tx packets that need to remain
299 * mapped until they are freed rather than until their Tx descriptors are
300 * freed.
301 */
302static void deferred_unmap_destructor(struct sk_buff *skb)
303{
304 unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
305}
306#endif
307
308static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
309 const struct ulptx_sgl *sgl, const struct sge_txq *q)
310{
311 const struct ulptx_sge_pair *p;
312 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
313
314 if (likely(skb_headlen(skb)))
315 dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
316 DMA_TO_DEVICE);
317 else {
318 dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
319 DMA_TO_DEVICE);
320 nfrags--;
321 }
322
323 /*
324 * the complexity below is because of the possibility of a wrap-around
325 * in the middle of an SGL
326 */
327 for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
328 if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
329unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
330 ntohl(p->len[0]), DMA_TO_DEVICE);
331 dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
332 ntohl(p->len[1]), DMA_TO_DEVICE);
333 p++;
334 } else if ((u8 *)p == (u8 *)q->stat) {
335 p = (const struct ulptx_sge_pair *)q->desc;
336 goto unmap;
337 } else if ((u8 *)p + 8 == (u8 *)q->stat) {
338 const __be64 *addr = (const __be64 *)q->desc;
339
340 dma_unmap_page(dev, be64_to_cpu(addr[0]),
341 ntohl(p->len[0]), DMA_TO_DEVICE);
342 dma_unmap_page(dev, be64_to_cpu(addr[1]),
343 ntohl(p->len[1]), DMA_TO_DEVICE);
344 p = (const struct ulptx_sge_pair *)&addr[2];
345 } else {
346 const __be64 *addr = (const __be64 *)q->desc;
347
348 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
349 ntohl(p->len[0]), DMA_TO_DEVICE);
350 dma_unmap_page(dev, be64_to_cpu(addr[0]),
351 ntohl(p->len[1]), DMA_TO_DEVICE);
352 p = (const struct ulptx_sge_pair *)&addr[1];
353 }
354 }
355 if (nfrags) {
356 __be64 addr;
357
358 if ((u8 *)p == (u8 *)q->stat)
359 p = (const struct ulptx_sge_pair *)q->desc;
360 addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
361 *(const __be64 *)q->desc;
362 dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
363 DMA_TO_DEVICE);
364 }
365}
366
367/**
368 * free_tx_desc - reclaims Tx descriptors and their buffers
369 * @adapter: the adapter
370 * @q: the Tx queue to reclaim descriptors from
371 * @n: the number of descriptors to reclaim
372 * @unmap: whether the buffers should be unmapped for DMA
373 *
374 * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
375 * Tx buffers. Called with the Tx queue lock held.
376 */
377static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
378 unsigned int n, bool unmap)
379{
380 struct tx_sw_desc *d;
381 unsigned int cidx = q->cidx;
382 struct device *dev = adap->pdev_dev;
383
384 d = &q->sdesc[cidx];
385 while (n--) {
386 if (d->skb) { /* an SGL is present */
387 if (unmap)
388 unmap_sgl(dev, d->skb, d->sgl, q);
a7525198 389 dev_consume_skb_any(d->skb);
fd3a4790
DM
390 d->skb = NULL;
391 }
392 ++d;
393 if (++cidx == q->size) {
394 cidx = 0;
395 d = q->sdesc;
396 }
397 }
398 q->cidx = cidx;
399}
400
401/*
402 * Return the number of reclaimable descriptors in a Tx queue.
403 */
404static inline int reclaimable(const struct sge_txq *q)
405{
406 int hw_cidx = ntohs(q->stat->cidx);
407 hw_cidx -= q->cidx;
408 return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
409}
410
411/**
412 * reclaim_completed_tx - reclaims completed Tx descriptors
413 * @adap: the adapter
414 * @q: the Tx queue to reclaim completed descriptors from
415 * @unmap: whether the buffers should be unmapped for DMA
416 *
417 * Reclaims Tx descriptors that the SGE has indicated it has processed,
418 * and frees the associated buffers if possible. Called with the Tx
419 * queue locked.
420 */
421static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
422 bool unmap)
423{
424 int avail = reclaimable(q);
425
426 if (avail) {
427 /*
428 * Limit the amount of clean up work we do at a time to keep
429 * the Tx lock hold time O(1).
430 */
431 if (avail > MAX_TX_RECLAIM)
432 avail = MAX_TX_RECLAIM;
433
434 free_tx_desc(adap, q, avail, unmap);
435 q->in_use -= avail;
436 }
437}
438
52367a76
VP
439static inline int get_buf_size(struct adapter *adapter,
440 const struct rx_sw_desc *d)
fd3a4790 441{
52367a76
VP
442 struct sge *s = &adapter->sge;
443 unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
444 int buf_size;
445
446 switch (rx_buf_size_idx) {
447 case RX_SMALL_PG_BUF:
448 buf_size = PAGE_SIZE;
449 break;
450
451 case RX_LARGE_PG_BUF:
452 buf_size = PAGE_SIZE << s->fl_pg_order;
453 break;
454
455 case RX_SMALL_MTU_BUF:
456 buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
457 break;
458
459 case RX_LARGE_MTU_BUF:
460 buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
461 break;
462
463 default:
464 BUG_ON(1);
465 }
466
467 return buf_size;
fd3a4790
DM
468}
469
470/**
471 * free_rx_bufs - free the Rx buffers on an SGE free list
472 * @adap: the adapter
473 * @q: the SGE free list to free buffers from
474 * @n: how many buffers to free
475 *
476 * Release the next @n buffers on an SGE free-buffer Rx queue. The
477 * buffers must be made inaccessible to HW before calling this function.
478 */
479static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
480{
481 while (n--) {
482 struct rx_sw_desc *d = &q->sdesc[q->cidx];
483
484 if (is_buf_mapped(d))
485 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
52367a76
VP
486 get_buf_size(adap, d),
487 PCI_DMA_FROMDEVICE);
fd3a4790
DM
488 put_page(d->page);
489 d->page = NULL;
490 if (++q->cidx == q->size)
491 q->cidx = 0;
492 q->avail--;
493 }
494}
495
496/**
497 * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
498 * @adap: the adapter
499 * @q: the SGE free list
500 *
501 * Unmap the current buffer on an SGE free-buffer Rx queue. The
502 * buffer must be made inaccessible to HW before calling this function.
503 *
504 * This is similar to @free_rx_bufs above but does not free the buffer.
505 * Do note that the FL still loses any further access to the buffer.
506 */
507static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
508{
509 struct rx_sw_desc *d = &q->sdesc[q->cidx];
510
511 if (is_buf_mapped(d))
512 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
52367a76 513 get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
fd3a4790
DM
514 d->page = NULL;
515 if (++q->cidx == q->size)
516 q->cidx = 0;
517 q->avail--;
518}
519
520static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
521{
0a57a536 522 u32 val;
fd3a4790 523 if (q->pend_cred >= 8) {
0a57a536 524 val = PIDX(q->pend_cred / 8);
d14807dd 525 if (!is_t4(adap->params.chip))
0a57a536 526 val |= DBTYPE(1);
d63a6dcf 527 val |= DBPRIO(1);
fd3a4790 528 wmb();
d63a6dcf
HS
529
530 /* If we're on T4, use the old doorbell mechanism; otherwise
531 * use the new BAR2 mechanism.
532 */
533 if (is_t4(adap->params.chip)) {
534 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
535 val | QID(q->cntxt_id));
536 } else {
537 writel(val, adap->bar2 + q->udb + SGE_UDB_KDOORBELL);
538
539 /* This Write memory Barrier will force the write to
540 * the User Doorbell area to be flushed.
541 */
542 wmb();
543 }
fd3a4790
DM
544 q->pend_cred &= 7;
545 }
546}
547
548static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
549 dma_addr_t mapping)
550{
551 sd->page = pg;
552 sd->dma_addr = mapping; /* includes size low bits */
553}
554
555/**
556 * refill_fl - refill an SGE Rx buffer ring
557 * @adap: the adapter
558 * @q: the ring to refill
559 * @n: the number of new buffers to allocate
560 * @gfp: the gfp flags for the allocations
561 *
562 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
563 * allocated with the supplied gfp flags. The caller must assure that
564 * @n does not exceed the queue's capacity. If afterwards the queue is
565 * found critically low mark it as starving in the bitmap of starving FLs.
566 *
567 * Returns the number of buffers allocated.
568 */
569static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
570 gfp_t gfp)
571{
52367a76 572 struct sge *s = &adap->sge;
fd3a4790
DM
573 struct page *pg;
574 dma_addr_t mapping;
575 unsigned int cred = q->avail;
576 __be64 *d = &q->desc[q->pidx];
577 struct rx_sw_desc *sd = &q->sdesc[q->pidx];
578
1f2149c1 579 gfp |= __GFP_NOWARN | __GFP_COLD;
fd3a4790 580
52367a76
VP
581 if (s->fl_pg_order == 0)
582 goto alloc_small_pages;
583
fd3a4790
DM
584 /*
585 * Prefer large buffers
586 */
587 while (n) {
52367a76 588 pg = alloc_pages(gfp | __GFP_COMP, s->fl_pg_order);
fd3a4790
DM
589 if (unlikely(!pg)) {
590 q->large_alloc_failed++;
591 break; /* fall back to single pages */
592 }
593
594 mapping = dma_map_page(adap->pdev_dev, pg, 0,
52367a76 595 PAGE_SIZE << s->fl_pg_order,
fd3a4790
DM
596 PCI_DMA_FROMDEVICE);
597 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
52367a76 598 __free_pages(pg, s->fl_pg_order);
fd3a4790
DM
599 goto out; /* do not try small pages for this error */
600 }
52367a76 601 mapping |= RX_LARGE_PG_BUF;
fd3a4790
DM
602 *d++ = cpu_to_be64(mapping);
603
604 set_rx_sw_desc(sd, pg, mapping);
605 sd++;
606
607 q->avail++;
608 if (++q->pidx == q->size) {
609 q->pidx = 0;
610 sd = q->sdesc;
611 d = q->desc;
612 }
613 n--;
614 }
fd3a4790 615
52367a76 616alloc_small_pages:
fd3a4790 617 while (n--) {
0614002b 618 pg = __skb_alloc_page(gfp, NULL);
fd3a4790
DM
619 if (unlikely(!pg)) {
620 q->alloc_failed++;
621 break;
622 }
623
624 mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
625 PCI_DMA_FROMDEVICE);
626 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
1f2149c1 627 put_page(pg);
fd3a4790
DM
628 goto out;
629 }
630 *d++ = cpu_to_be64(mapping);
631
632 set_rx_sw_desc(sd, pg, mapping);
633 sd++;
634
635 q->avail++;
636 if (++q->pidx == q->size) {
637 q->pidx = 0;
638 sd = q->sdesc;
639 d = q->desc;
640 }
641 }
642
643out: cred = q->avail - cred;
644 q->pend_cred += cred;
645 ring_fl_db(adap, q);
646
647 if (unlikely(fl_starving(q))) {
648 smp_wmb();
e46dab4d
DM
649 set_bit(q->cntxt_id - adap->sge.egr_start,
650 adap->sge.starving_fl);
fd3a4790
DM
651 }
652
653 return cred;
654}
655
656static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
657{
658 refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
659 GFP_ATOMIC);
660}
661
662/**
663 * alloc_ring - allocate resources for an SGE descriptor ring
664 * @dev: the PCI device's core device
665 * @nelem: the number of descriptors
666 * @elem_size: the size of each descriptor
667 * @sw_size: the size of the SW state associated with each ring element
668 * @phys: the physical address of the allocated ring
669 * @metadata: address of the array holding the SW state for the ring
670 * @stat_size: extra space in HW ring for status information
ad6bad3e 671 * @node: preferred node for memory allocations
fd3a4790
DM
672 *
673 * Allocates resources for an SGE descriptor ring, such as Tx queues,
674 * free buffer lists, or response queues. Each SGE ring requires
675 * space for its HW descriptors plus, optionally, space for the SW state
676 * associated with each HW entry (the metadata). The function returns
677 * three values: the virtual address for the HW ring (the return value
678 * of the function), the bus address of the HW ring, and the address
679 * of the SW ring.
680 */
681static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
682 size_t sw_size, dma_addr_t *phys, void *metadata,
ad6bad3e 683 size_t stat_size, int node)
fd3a4790
DM
684{
685 size_t len = nelem * elem_size + stat_size;
686 void *s = NULL;
687 void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
688
689 if (!p)
690 return NULL;
691 if (sw_size) {
ad6bad3e 692 s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
fd3a4790
DM
693
694 if (!s) {
695 dma_free_coherent(dev, len, p, *phys);
696 return NULL;
697 }
698 }
699 if (metadata)
700 *(void **)metadata = s;
701 memset(p, 0, len);
702 return p;
703}
704
705/**
706 * sgl_len - calculates the size of an SGL of the given capacity
707 * @n: the number of SGL entries
708 *
709 * Calculates the number of flits needed for a scatter/gather list that
710 * can hold the given number of entries.
711 */
712static inline unsigned int sgl_len(unsigned int n)
713{
714 n--;
715 return (3 * n) / 2 + (n & 1) + 2;
716}
717
718/**
719 * flits_to_desc - returns the num of Tx descriptors for the given flits
720 * @n: the number of flits
721 *
722 * Returns the number of Tx descriptors needed for the supplied number
723 * of flits.
724 */
725static inline unsigned int flits_to_desc(unsigned int n)
726{
727 BUG_ON(n > SGE_MAX_WR_LEN / 8);
728 return DIV_ROUND_UP(n, 8);
729}
730
731/**
732 * is_eth_imm - can an Ethernet packet be sent as immediate data?
733 * @skb: the packet
734 *
735 * Returns whether an Ethernet packet is small enough to fit as
0034b298 736 * immediate data. Return value corresponds to headroom required.
fd3a4790
DM
737 */
738static inline int is_eth_imm(const struct sk_buff *skb)
739{
0034b298
KS
740 int hdrlen = skb_shinfo(skb)->gso_size ?
741 sizeof(struct cpl_tx_pkt_lso_core) : 0;
742
743 hdrlen += sizeof(struct cpl_tx_pkt);
744 if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
745 return hdrlen;
746 return 0;
fd3a4790
DM
747}
748
749/**
750 * calc_tx_flits - calculate the number of flits for a packet Tx WR
751 * @skb: the packet
752 *
753 * Returns the number of flits needed for a Tx WR for the given Ethernet
754 * packet, including the needed WR and CPL headers.
755 */
756static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
757{
758 unsigned int flits;
0034b298 759 int hdrlen = is_eth_imm(skb);
fd3a4790 760
0034b298
KS
761 if (hdrlen)
762 return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
fd3a4790
DM
763
764 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
765 if (skb_shinfo(skb)->gso_size)
766 flits += 2;
767 return flits;
768}
769
770/**
771 * calc_tx_descs - calculate the number of Tx descriptors for a packet
772 * @skb: the packet
773 *
774 * Returns the number of Tx descriptors needed for the given Ethernet
775 * packet, including the needed WR and CPL headers.
776 */
777static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
778{
779 return flits_to_desc(calc_tx_flits(skb));
780}
781
782/**
783 * write_sgl - populate a scatter/gather list for a packet
784 * @skb: the packet
785 * @q: the Tx queue we are writing into
786 * @sgl: starting location for writing the SGL
787 * @end: points right after the end of the SGL
788 * @start: start offset into skb main-body data to include in the SGL
789 * @addr: the list of bus addresses for the SGL elements
790 *
791 * Generates a gather list for the buffers that make up a packet.
792 * The caller must provide adequate space for the SGL that will be written.
793 * The SGL includes all of the packet's page fragments and the data in its
794 * main body except for the first @start bytes. @sgl must be 16-byte
795 * aligned and within a Tx descriptor with available space. @end points
796 * right after the end of the SGL but does not account for any potential
797 * wrap around, i.e., @end > @sgl.
798 */
799static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
800 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
801 const dma_addr_t *addr)
802{
803 unsigned int i, len;
804 struct ulptx_sge_pair *to;
805 const struct skb_shared_info *si = skb_shinfo(skb);
806 unsigned int nfrags = si->nr_frags;
807 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
808
809 len = skb_headlen(skb) - start;
810 if (likely(len)) {
811 sgl->len0 = htonl(len);
812 sgl->addr0 = cpu_to_be64(addr[0] + start);
813 nfrags++;
814 } else {
9e903e08 815 sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
fd3a4790
DM
816 sgl->addr0 = cpu_to_be64(addr[1]);
817 }
818
819 sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_NSGE(nfrags));
820 if (likely(--nfrags == 0))
821 return;
822 /*
823 * Most of the complexity below deals with the possibility we hit the
824 * end of the queue in the middle of writing the SGL. For this case
825 * only we create the SGL in a temporary buffer and then copy it.
826 */
827 to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
828
829 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
9e903e08
ED
830 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
831 to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
fd3a4790
DM
832 to->addr[0] = cpu_to_be64(addr[i]);
833 to->addr[1] = cpu_to_be64(addr[++i]);
834 }
835 if (nfrags) {
9e903e08 836 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
fd3a4790
DM
837 to->len[1] = cpu_to_be32(0);
838 to->addr[0] = cpu_to_be64(addr[i + 1]);
839 }
840 if (unlikely((u8 *)end > (u8 *)q->stat)) {
841 unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
842
843 if (likely(part0))
844 memcpy(sgl->sge, buf, part0);
845 part1 = (u8 *)end - (u8 *)q->stat;
846 memcpy(q->desc, (u8 *)buf + part0, part1);
847 end = (void *)q->desc + part1;
848 }
849 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
64699336 850 *end = 0;
fd3a4790
DM
851}
852
fb5ac0de
DC
853/* This function copies a tx_desc struct to memory mapped BAR2 space(user space
854 * writes). For coalesced WR SGE, fetches data from the FIFO instead of from
855 * Host.
22adfe0a 856 */
fb5ac0de 857static void cxgb_pio_copy(u64 __iomem *dst, struct tx_desc *desc)
22adfe0a 858{
fb5ac0de
DC
859 int count = sizeof(*desc) / sizeof(u64);
860 u64 *src = (u64 *)desc;
22adfe0a
SR
861
862 while (count) {
863 writeq(*src, dst);
864 src++;
865 dst++;
866 count--;
867 }
868}
869
fd3a4790
DM
870/**
871 * ring_tx_db - check and potentially ring a Tx queue's doorbell
872 * @adap: the adapter
873 * @q: the Tx queue
874 * @n: number of new descriptors to give to HW
875 *
876 * Ring the doorbel for a Tx queue.
877 */
878static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
879{
880 wmb(); /* write descriptors before telling HW */
d63a6dcf
HS
881
882 if (is_t4(adap->params.chip)) {
883 u32 val = PIDX(n);
884 unsigned long flags;
885
886 /* For T4 we need to participate in the Doorbell Recovery
887 * mechanism.
888 */
889 spin_lock_irqsave(&q->db_lock, flags);
890 if (!q->db_disabled)
22adfe0a 891 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
d63a6dcf
HS
892 QID(q->cntxt_id) | val);
893 else
894 q->db_pidx_inc += n;
895 q->db_pidx = q->pidx;
896 spin_unlock_irqrestore(&q->db_lock, flags);
897 } else {
898 u32 val = PIDX_T5(n);
899
900 /* T4 and later chips share the same PIDX field offset within
901 * the doorbell, but T5 and later shrank the field in order to
902 * gain a bit for Doorbell Priority. The field was absurdly
903 * large in the first place (14 bits) so we just use the T5
904 * and later limits and warn if a Queue ID is too large.
905 */
906 WARN_ON(val & DBPRIO(1));
907
908 /* For T5 and later we use the Write-Combine mapped BAR2 User
909 * Doorbell mechanism. If we're only writing a single TX
910 * Descriptor and TX Write Combining hasn't been disabled, we
911 * can use the Write Combining Gather Buffer; otherwise we use
912 * the simple doorbell.
913 */
914 if (n == 1) {
915 int index = (q->pidx
916 ? (q->pidx - 1)
917 : (q->size - 1));
d63a6dcf 918
fb5ac0de
DC
919 cxgb_pio_copy(adap->bar2 + q->udb + SGE_UDB_WCDOORBELL,
920 q->desc + index);
22adfe0a 921 } else {
d63a6dcf 922 writel(val, adap->bar2 + q->udb + SGE_UDB_KDOORBELL);
22adfe0a 923 }
d63a6dcf
HS
924
925 /* This Write Memory Barrier will force the write to the User
926 * Doorbell area to be flushed. This is needed to prevent
927 * writes on different CPUs for the same queue from hitting
928 * the adapter out of order. This is required when some Work
929 * Requests take the Write Combine Gather Buffer path (user
930 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
931 * take the traditional path where we simply increment the
932 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
933 * hardware DMA read the actual Work Request.
934 */
935 wmb();
936 }
fd3a4790
DM
937}
938
939/**
940 * inline_tx_skb - inline a packet's data into Tx descriptors
941 * @skb: the packet
942 * @q: the Tx queue where the packet will be inlined
943 * @pos: starting position in the Tx queue where to inline the packet
944 *
945 * Inline a packet's contents directly into Tx descriptors, starting at
946 * the given position within the Tx DMA ring.
947 * Most of the complexity of this operation is dealing with wrap arounds
948 * in the middle of the packet we want to inline.
949 */
950static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
951 void *pos)
952{
953 u64 *p;
954 int left = (void *)q->stat - pos;
955
956 if (likely(skb->len <= left)) {
957 if (likely(!skb->data_len))
958 skb_copy_from_linear_data(skb, pos, skb->len);
959 else
960 skb_copy_bits(skb, 0, pos, skb->len);
961 pos += skb->len;
962 } else {
963 skb_copy_bits(skb, 0, pos, left);
964 skb_copy_bits(skb, left, q->desc, skb->len - left);
965 pos = (void *)q->desc + (skb->len - left);
966 }
967
968 /* 0-pad to multiple of 16 */
969 p = PTR_ALIGN(pos, 8);
970 if ((uintptr_t)p & 8)
971 *p = 0;
972}
973
974/*
975 * Figure out what HW csum a packet wants and return the appropriate control
976 * bits.
977 */
978static u64 hwcsum(const struct sk_buff *skb)
979{
980 int csum_type;
981 const struct iphdr *iph = ip_hdr(skb);
982
983 if (iph->version == 4) {
984 if (iph->protocol == IPPROTO_TCP)
985 csum_type = TX_CSUM_TCPIP;
986 else if (iph->protocol == IPPROTO_UDP)
987 csum_type = TX_CSUM_UDPIP;
988 else {
989nocsum: /*
990 * unknown protocol, disable HW csum
991 * and hope a bad packet is detected
992 */
993 return TXPKT_L4CSUM_DIS;
994 }
995 } else {
996 /*
997 * this doesn't work with extension headers
998 */
999 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
1000
1001 if (ip6h->nexthdr == IPPROTO_TCP)
1002 csum_type = TX_CSUM_TCPIP6;
1003 else if (ip6h->nexthdr == IPPROTO_UDP)
1004 csum_type = TX_CSUM_UDPIP6;
1005 else
1006 goto nocsum;
1007 }
1008
1009 if (likely(csum_type >= TX_CSUM_TCPIP))
1010 return TXPKT_CSUM_TYPE(csum_type) |
1011 TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
1012 TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
1013 else {
1014 int start = skb_transport_offset(skb);
1015
1016 return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
1017 TXPKT_CSUM_LOC(start + skb->csum_offset);
1018 }
1019}
1020
1021static void eth_txq_stop(struct sge_eth_txq *q)
1022{
1023 netif_tx_stop_queue(q->txq);
1024 q->q.stops++;
1025}
1026
1027static inline void txq_advance(struct sge_txq *q, unsigned int n)
1028{
1029 q->in_use += n;
1030 q->pidx += n;
1031 if (q->pidx >= q->size)
1032 q->pidx -= q->size;
1033}
1034
1035/**
1036 * t4_eth_xmit - add a packet to an Ethernet Tx queue
1037 * @skb: the packet
1038 * @dev: the egress net device
1039 *
1040 * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
1041 */
1042netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1043{
0034b298 1044 int len;
fd3a4790
DM
1045 u32 wr_mid;
1046 u64 cntrl, *end;
1047 int qidx, credits;
1048 unsigned int flits, ndesc;
1049 struct adapter *adap;
1050 struct sge_eth_txq *q;
1051 const struct port_info *pi;
1052 struct fw_eth_tx_pkt_wr *wr;
1053 struct cpl_tx_pkt_core *cpl;
1054 const struct skb_shared_info *ssi;
1055 dma_addr_t addr[MAX_SKB_FRAGS + 1];
0034b298 1056 bool immediate = false;
fd3a4790
DM
1057
1058 /*
1059 * The chip min packet length is 10 octets but play safe and reject
1060 * anything shorter than an Ethernet header.
1061 */
1062 if (unlikely(skb->len < ETH_HLEN)) {
a7525198 1063out_free: dev_kfree_skb_any(skb);
fd3a4790
DM
1064 return NETDEV_TX_OK;
1065 }
1066
1067 pi = netdev_priv(dev);
1068 adap = pi->adapter;
1069 qidx = skb_get_queue_mapping(skb);
1070 q = &adap->sge.ethtxq[qidx + pi->first_qset];
1071
1072 reclaim_completed_tx(adap, &q->q, true);
1073
1074 flits = calc_tx_flits(skb);
1075 ndesc = flits_to_desc(flits);
1076 credits = txq_avail(&q->q) - ndesc;
1077
1078 if (unlikely(credits < 0)) {
1079 eth_txq_stop(q);
1080 dev_err(adap->pdev_dev,
1081 "%s: Tx ring %u full while queue awake!\n",
1082 dev->name, qidx);
1083 return NETDEV_TX_BUSY;
1084 }
1085
0034b298
KS
1086 if (is_eth_imm(skb))
1087 immediate = true;
1088
1089 if (!immediate &&
fd3a4790
DM
1090 unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
1091 q->mapping_err++;
1092 goto out_free;
1093 }
1094
1095 wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
1096 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1097 eth_txq_stop(q);
1098 wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
1099 }
1100
1101 wr = (void *)&q->q.desc[q->q.pidx];
1102 wr->equiq_to_len16 = htonl(wr_mid);
1103 wr->r3 = cpu_to_be64(0);
1104 end = (u64 *)wr + flits;
1105
0034b298 1106 len = immediate ? skb->len : 0;
fd3a4790
DM
1107 ssi = skb_shinfo(skb);
1108 if (ssi->gso_size) {
625ac6ae 1109 struct cpl_tx_pkt_lso *lso = (void *)wr;
fd3a4790
DM
1110 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1111 int l3hdr_len = skb_network_header_len(skb);
1112 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1113
0034b298 1114 len += sizeof(*lso);
fd3a4790 1115 wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
0034b298 1116 FW_WR_IMMDLEN(len));
625ac6ae
DM
1117 lso->c.lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
1118 LSO_FIRST_SLICE | LSO_LAST_SLICE |
1119 LSO_IPV6(v6) |
1120 LSO_ETHHDR_LEN(eth_xtra_len / 4) |
1121 LSO_IPHDR_LEN(l3hdr_len / 4) |
1122 LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
1123 lso->c.ipid_ofst = htons(0);
1124 lso->c.mss = htons(ssi->gso_size);
1125 lso->c.seqno_offset = htonl(0);
1126 lso->c.len = htonl(skb->len);
fd3a4790
DM
1127 cpl = (void *)(lso + 1);
1128 cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
1129 TXPKT_IPHDR_LEN(l3hdr_len) |
1130 TXPKT_ETHHDR_LEN(eth_xtra_len);
1131 q->tso++;
1132 q->tx_cso += ssi->gso_segs;
1133 } else {
ca71de6b 1134 len += sizeof(*cpl);
fd3a4790
DM
1135 wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
1136 FW_WR_IMMDLEN(len));
1137 cpl = (void *)(wr + 1);
1138 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1139 cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
1140 q->tx_cso++;
1141 } else
1142 cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
1143 }
1144
1145 if (vlan_tx_tag_present(skb)) {
1146 q->vlan_ins++;
1147 cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
1148 }
1149
1150 cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
1707aec9 1151 TXPKT_INTF(pi->tx_chan) | TXPKT_PF(adap->fn));
fd3a4790
DM
1152 cpl->pack = htons(0);
1153 cpl->len = htons(skb->len);
1154 cpl->ctrl1 = cpu_to_be64(cntrl);
1155
0034b298 1156 if (immediate) {
fd3a4790 1157 inline_tx_skb(skb, &q->q, cpl + 1);
a7525198 1158 dev_consume_skb_any(skb);
fd3a4790
DM
1159 } else {
1160 int last_desc;
1161
1162 write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
1163 addr);
1164 skb_orphan(skb);
1165
1166 last_desc = q->q.pidx + ndesc - 1;
1167 if (last_desc >= q->q.size)
1168 last_desc -= q->q.size;
1169 q->q.sdesc[last_desc].skb = skb;
1170 q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
1171 }
1172
1173 txq_advance(&q->q, ndesc);
1174
1175 ring_tx_db(adap, &q->q, ndesc);
1176 return NETDEV_TX_OK;
1177}
1178
1179/**
1180 * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
1181 * @q: the SGE control Tx queue
1182 *
1183 * This is a variant of reclaim_completed_tx() that is used for Tx queues
1184 * that send only immediate data (presently just the control queues) and
1185 * thus do not have any sk_buffs to release.
1186 */
1187static inline void reclaim_completed_tx_imm(struct sge_txq *q)
1188{
1189 int hw_cidx = ntohs(q->stat->cidx);
1190 int reclaim = hw_cidx - q->cidx;
1191
1192 if (reclaim < 0)
1193 reclaim += q->size;
1194
1195 q->in_use -= reclaim;
1196 q->cidx = hw_cidx;
1197}
1198
1199/**
1200 * is_imm - check whether a packet can be sent as immediate data
1201 * @skb: the packet
1202 *
1203 * Returns true if a packet can be sent as a WR with immediate data.
1204 */
1205static inline int is_imm(const struct sk_buff *skb)
1206{
1207 return skb->len <= MAX_CTRL_WR_LEN;
1208}
1209
1210/**
1211 * ctrlq_check_stop - check if a control queue is full and should stop
1212 * @q: the queue
1213 * @wr: most recent WR written to the queue
1214 *
1215 * Check if a control queue has become full and should be stopped.
1216 * We clean up control queue descriptors very lazily, only when we are out.
1217 * If the queue is still full after reclaiming any completed descriptors
1218 * we suspend it and have the last WR wake it up.
1219 */
1220static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
1221{
1222 reclaim_completed_tx_imm(&q->q);
1223 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1224 wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
1225 q->q.stops++;
1226 q->full = 1;
1227 }
1228}
1229
1230/**
1231 * ctrl_xmit - send a packet through an SGE control Tx queue
1232 * @q: the control queue
1233 * @skb: the packet
1234 *
1235 * Send a packet through an SGE control Tx queue. Packets sent through
1236 * a control queue must fit entirely as immediate data.
1237 */
1238static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
1239{
1240 unsigned int ndesc;
1241 struct fw_wr_hdr *wr;
1242
1243 if (unlikely(!is_imm(skb))) {
1244 WARN_ON(1);
1245 dev_kfree_skb(skb);
1246 return NET_XMIT_DROP;
1247 }
1248
1249 ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
1250 spin_lock(&q->sendq.lock);
1251
1252 if (unlikely(q->full)) {
1253 skb->priority = ndesc; /* save for restart */
1254 __skb_queue_tail(&q->sendq, skb);
1255 spin_unlock(&q->sendq.lock);
1256 return NET_XMIT_CN;
1257 }
1258
1259 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1260 inline_tx_skb(skb, &q->q, wr);
1261
1262 txq_advance(&q->q, ndesc);
1263 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
1264 ctrlq_check_stop(q, wr);
1265
1266 ring_tx_db(q->adap, &q->q, ndesc);
1267 spin_unlock(&q->sendq.lock);
1268
1269 kfree_skb(skb);
1270 return NET_XMIT_SUCCESS;
1271}
1272
1273/**
1274 * restart_ctrlq - restart a suspended control queue
1275 * @data: the control queue to restart
1276 *
1277 * Resumes transmission on a suspended Tx control queue.
1278 */
1279static void restart_ctrlq(unsigned long data)
1280{
1281 struct sk_buff *skb;
1282 unsigned int written = 0;
1283 struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
1284
1285 spin_lock(&q->sendq.lock);
1286 reclaim_completed_tx_imm(&q->q);
1287 BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
1288
1289 while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
1290 struct fw_wr_hdr *wr;
1291 unsigned int ndesc = skb->priority; /* previously saved */
1292
1293 /*
1294 * Write descriptors and free skbs outside the lock to limit
1295 * wait times. q->full is still set so new skbs will be queued.
1296 */
1297 spin_unlock(&q->sendq.lock);
1298
1299 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1300 inline_tx_skb(skb, &q->q, wr);
1301 kfree_skb(skb);
1302
1303 written += ndesc;
1304 txq_advance(&q->q, ndesc);
1305 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1306 unsigned long old = q->q.stops;
1307
1308 ctrlq_check_stop(q, wr);
1309 if (q->q.stops != old) { /* suspended anew */
1310 spin_lock(&q->sendq.lock);
1311 goto ringdb;
1312 }
1313 }
1314 if (written > 16) {
1315 ring_tx_db(q->adap, &q->q, written);
1316 written = 0;
1317 }
1318 spin_lock(&q->sendq.lock);
1319 }
1320 q->full = 0;
1321ringdb: if (written)
1322 ring_tx_db(q->adap, &q->q, written);
1323 spin_unlock(&q->sendq.lock);
1324}
1325
1326/**
1327 * t4_mgmt_tx - send a management message
1328 * @adap: the adapter
1329 * @skb: the packet containing the management message
1330 *
1331 * Send a management message through control queue 0.
1332 */
1333int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
1334{
1335 int ret;
1336
1337 local_bh_disable();
1338 ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
1339 local_bh_enable();
1340 return ret;
1341}
1342
1343/**
1344 * is_ofld_imm - check whether a packet can be sent as immediate data
1345 * @skb: the packet
1346 *
1347 * Returns true if a packet can be sent as an offload WR with immediate
1348 * data. We currently use the same limit as for Ethernet packets.
1349 */
1350static inline int is_ofld_imm(const struct sk_buff *skb)
1351{
1352 return skb->len <= MAX_IMM_TX_PKT_LEN;
1353}
1354
1355/**
1356 * calc_tx_flits_ofld - calculate # of flits for an offload packet
1357 * @skb: the packet
1358 *
1359 * Returns the number of flits needed for the given offload packet.
1360 * These packets are already fully constructed and no additional headers
1361 * will be added.
1362 */
1363static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
1364{
1365 unsigned int flits, cnt;
1366
1367 if (is_ofld_imm(skb))
1368 return DIV_ROUND_UP(skb->len, 8);
1369
1370 flits = skb_transport_offset(skb) / 8U; /* headers */
1371 cnt = skb_shinfo(skb)->nr_frags;
15dd16c2 1372 if (skb_tail_pointer(skb) != skb_transport_header(skb))
fd3a4790
DM
1373 cnt++;
1374 return flits + sgl_len(cnt);
1375}
1376
1377/**
1378 * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
1379 * @adap: the adapter
1380 * @q: the queue to stop
1381 *
1382 * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
1383 * inability to map packets. A periodic timer attempts to restart
1384 * queues so marked.
1385 */
1386static void txq_stop_maperr(struct sge_ofld_txq *q)
1387{
1388 q->mapping_err++;
1389 q->q.stops++;
e46dab4d
DM
1390 set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
1391 q->adap->sge.txq_maperr);
fd3a4790
DM
1392}
1393
1394/**
1395 * ofldtxq_stop - stop an offload Tx queue that has become full
1396 * @q: the queue to stop
1397 * @skb: the packet causing the queue to become full
1398 *
1399 * Stops an offload Tx queue that has become full and modifies the packet
1400 * being written to request a wakeup.
1401 */
1402static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
1403{
1404 struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
1405
1406 wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
1407 q->q.stops++;
1408 q->full = 1;
1409}
1410
1411/**
1412 * service_ofldq - restart a suspended offload queue
1413 * @q: the offload queue
1414 *
1415 * Services an offload Tx queue by moving packets from its packet queue
1416 * to the HW Tx ring. The function starts and ends with the queue locked.
1417 */
1418static void service_ofldq(struct sge_ofld_txq *q)
1419{
1420 u64 *pos;
1421 int credits;
1422 struct sk_buff *skb;
1423 unsigned int written = 0;
1424 unsigned int flits, ndesc;
1425
1426 while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
1427 /*
1428 * We drop the lock but leave skb on sendq, thus retaining
1429 * exclusive access to the state of the queue.
1430 */
1431 spin_unlock(&q->sendq.lock);
1432
1433 reclaim_completed_tx(q->adap, &q->q, false);
1434
1435 flits = skb->priority; /* previously saved */
1436 ndesc = flits_to_desc(flits);
1437 credits = txq_avail(&q->q) - ndesc;
1438 BUG_ON(credits < 0);
1439 if (unlikely(credits < TXQ_STOP_THRES))
1440 ofldtxq_stop(q, skb);
1441
1442 pos = (u64 *)&q->q.desc[q->q.pidx];
1443 if (is_ofld_imm(skb))
1444 inline_tx_skb(skb, &q->q, pos);
1445 else if (map_skb(q->adap->pdev_dev, skb,
1446 (dma_addr_t *)skb->head)) {
1447 txq_stop_maperr(q);
1448 spin_lock(&q->sendq.lock);
1449 break;
1450 } else {
1451 int last_desc, hdr_len = skb_transport_offset(skb);
1452
1453 memcpy(pos, skb->data, hdr_len);
1454 write_sgl(skb, &q->q, (void *)pos + hdr_len,
1455 pos + flits, hdr_len,
1456 (dma_addr_t *)skb->head);
1457#ifdef CONFIG_NEED_DMA_MAP_STATE
1458 skb->dev = q->adap->port[0];
1459 skb->destructor = deferred_unmap_destructor;
1460#endif
1461 last_desc = q->q.pidx + ndesc - 1;
1462 if (last_desc >= q->q.size)
1463 last_desc -= q->q.size;
1464 q->q.sdesc[last_desc].skb = skb;
1465 }
1466
1467 txq_advance(&q->q, ndesc);
1468 written += ndesc;
1469 if (unlikely(written > 32)) {
1470 ring_tx_db(q->adap, &q->q, written);
1471 written = 0;
1472 }
1473
1474 spin_lock(&q->sendq.lock);
1475 __skb_unlink(skb, &q->sendq);
1476 if (is_ofld_imm(skb))
1477 kfree_skb(skb);
1478 }
1479 if (likely(written))
1480 ring_tx_db(q->adap, &q->q, written);
1481}
1482
1483/**
1484 * ofld_xmit - send a packet through an offload queue
1485 * @q: the Tx offload queue
1486 * @skb: the packet
1487 *
1488 * Send an offload packet through an SGE offload queue.
1489 */
1490static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
1491{
1492 skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
1493 spin_lock(&q->sendq.lock);
1494 __skb_queue_tail(&q->sendq, skb);
1495 if (q->sendq.qlen == 1)
1496 service_ofldq(q);
1497 spin_unlock(&q->sendq.lock);
1498 return NET_XMIT_SUCCESS;
1499}
1500
1501/**
1502 * restart_ofldq - restart a suspended offload queue
1503 * @data: the offload queue to restart
1504 *
1505 * Resumes transmission on a suspended Tx offload queue.
1506 */
1507static void restart_ofldq(unsigned long data)
1508{
1509 struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
1510
1511 spin_lock(&q->sendq.lock);
1512 q->full = 0; /* the queue actually is completely empty now */
1513 service_ofldq(q);
1514 spin_unlock(&q->sendq.lock);
1515}
1516
1517/**
1518 * skb_txq - return the Tx queue an offload packet should use
1519 * @skb: the packet
1520 *
1521 * Returns the Tx queue an offload packet should use as indicated by bits
1522 * 1-15 in the packet's queue_mapping.
1523 */
1524static inline unsigned int skb_txq(const struct sk_buff *skb)
1525{
1526 return skb->queue_mapping >> 1;
1527}
1528
1529/**
1530 * is_ctrl_pkt - return whether an offload packet is a control packet
1531 * @skb: the packet
1532 *
1533 * Returns whether an offload packet should use an OFLD or a CTRL
1534 * Tx queue as indicated by bit 0 in the packet's queue_mapping.
1535 */
1536static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
1537{
1538 return skb->queue_mapping & 1;
1539}
1540
1541static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
1542{
1543 unsigned int idx = skb_txq(skb);
1544
4fe44dd7
KS
1545 if (unlikely(is_ctrl_pkt(skb))) {
1546 /* Single ctrl queue is a requirement for LE workaround path */
1547 if (adap->tids.nsftids)
1548 idx = 0;
fd3a4790 1549 return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
4fe44dd7 1550 }
fd3a4790
DM
1551 return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
1552}
1553
1554/**
1555 * t4_ofld_send - send an offload packet
1556 * @adap: the adapter
1557 * @skb: the packet
1558 *
1559 * Sends an offload packet. We use the packet queue_mapping to select the
1560 * appropriate Tx queue as follows: bit 0 indicates whether the packet
1561 * should be sent as regular or control, bits 1-15 select the queue.
1562 */
1563int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
1564{
1565 int ret;
1566
1567 local_bh_disable();
1568 ret = ofld_send(adap, skb);
1569 local_bh_enable();
1570 return ret;
1571}
1572
1573/**
1574 * cxgb4_ofld_send - send an offload packet
1575 * @dev: the net device
1576 * @skb: the packet
1577 *
1578 * Sends an offload packet. This is an exported version of @t4_ofld_send,
1579 * intended for ULDs.
1580 */
1581int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
1582{
1583 return t4_ofld_send(netdev2adap(dev), skb);
1584}
1585EXPORT_SYMBOL(cxgb4_ofld_send);
1586
e91b0f24 1587static inline void copy_frags(struct sk_buff *skb,
fd3a4790
DM
1588 const struct pkt_gl *gl, unsigned int offset)
1589{
e91b0f24 1590 int i;
fd3a4790
DM
1591
1592 /* usually there's just one frag */
e91b0f24
IC
1593 __skb_fill_page_desc(skb, 0, gl->frags[0].page,
1594 gl->frags[0].offset + offset,
1595 gl->frags[0].size - offset);
1596 skb_shinfo(skb)->nr_frags = gl->nfrags;
1597 for (i = 1; i < gl->nfrags; i++)
1598 __skb_fill_page_desc(skb, i, gl->frags[i].page,
1599 gl->frags[i].offset,
1600 gl->frags[i].size);
fd3a4790
DM
1601
1602 /* get a reference to the last page, we don't own it */
e91b0f24 1603 get_page(gl->frags[gl->nfrags - 1].page);
fd3a4790
DM
1604}
1605
1606/**
1607 * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
1608 * @gl: the gather list
1609 * @skb_len: size of sk_buff main body if it carries fragments
1610 * @pull_len: amount of data to move to the sk_buff's main body
1611 *
1612 * Builds an sk_buff from the given packet gather list. Returns the
1613 * sk_buff or %NULL if sk_buff allocation failed.
1614 */
1615struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
1616 unsigned int skb_len, unsigned int pull_len)
1617{
1618 struct sk_buff *skb;
1619
1620 /*
1621 * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
1622 * size, which is expected since buffers are at least PAGE_SIZEd.
1623 * In this case packets up to RX_COPY_THRES have only one fragment.
1624 */
1625 if (gl->tot_len <= RX_COPY_THRES) {
1626 skb = dev_alloc_skb(gl->tot_len);
1627 if (unlikely(!skb))
1628 goto out;
1629 __skb_put(skb, gl->tot_len);
1630 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1631 } else {
1632 skb = dev_alloc_skb(skb_len);
1633 if (unlikely(!skb))
1634 goto out;
1635 __skb_put(skb, pull_len);
1636 skb_copy_to_linear_data(skb, gl->va, pull_len);
1637
e91b0f24 1638 copy_frags(skb, gl, pull_len);
fd3a4790
DM
1639 skb->len = gl->tot_len;
1640 skb->data_len = skb->len - pull_len;
1641 skb->truesize += skb->data_len;
1642 }
1643out: return skb;
1644}
1645EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
1646
1647/**
1648 * t4_pktgl_free - free a packet gather list
1649 * @gl: the gather list
1650 *
1651 * Releases the pages of a packet gather list. We do not own the last
1652 * page on the list and do not free it.
1653 */
de498c89 1654static void t4_pktgl_free(const struct pkt_gl *gl)
fd3a4790
DM
1655{
1656 int n;
e91b0f24 1657 const struct page_frag *p;
fd3a4790
DM
1658
1659 for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
1660 put_page(p->page);
1661}
1662
1663/*
1664 * Process an MPS trace packet. Give it an unused protocol number so it won't
1665 * be delivered to anyone and send it to the stack for capture.
1666 */
1667static noinline int handle_trace_pkt(struct adapter *adap,
1668 const struct pkt_gl *gl)
1669{
1670 struct sk_buff *skb;
fd3a4790
DM
1671
1672 skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
1673 if (unlikely(!skb)) {
1674 t4_pktgl_free(gl);
1675 return 0;
1676 }
1677
d14807dd 1678 if (is_t4(adap->params.chip))
0a57a536
SR
1679 __skb_pull(skb, sizeof(struct cpl_trace_pkt));
1680 else
1681 __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
1682
fd3a4790
DM
1683 skb_reset_mac_header(skb);
1684 skb->protocol = htons(0xffff);
1685 skb->dev = adap->port[0];
1686 netif_receive_skb(skb);
1687 return 0;
1688}
1689
1690static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1691 const struct cpl_rx_pkt *pkt)
1692{
52367a76
VP
1693 struct adapter *adapter = rxq->rspq.adap;
1694 struct sge *s = &adapter->sge;
fd3a4790
DM
1695 int ret;
1696 struct sk_buff *skb;
1697
1698 skb = napi_get_frags(&rxq->rspq.napi);
1699 if (unlikely(!skb)) {
1700 t4_pktgl_free(gl);
1701 rxq->stats.rx_drops++;
1702 return;
1703 }
1704
52367a76
VP
1705 copy_frags(skb, gl, s->pktshift);
1706 skb->len = gl->tot_len - s->pktshift;
fd3a4790
DM
1707 skb->data_len = skb->len;
1708 skb->truesize += skb->data_len;
1709 skb->ip_summed = CHECKSUM_UNNECESSARY;
1710 skb_record_rx_queue(skb, rxq->rspq.idx);
87b6cf51 1711 if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
8264989c
TH
1712 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
1713 PKT_HASH_TYPE_L3);
fd3a4790
DM
1714
1715 if (unlikely(pkt->vlan_ex)) {
86a9bad3 1716 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
fd3a4790 1717 rxq->stats.vlan_ex++;
fd3a4790
DM
1718 }
1719 ret = napi_gro_frags(&rxq->rspq.napi);
19ecae2c 1720 if (ret == GRO_HELD)
fd3a4790
DM
1721 rxq->stats.lro_pkts++;
1722 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1723 rxq->stats.lro_merged++;
1724 rxq->stats.pkts++;
1725 rxq->stats.rx_cso++;
1726}
1727
1728/**
1729 * t4_ethrx_handler - process an ingress ethernet packet
1730 * @q: the response queue that received the packet
1731 * @rsp: the response queue descriptor holding the RX_PKT message
1732 * @si: the gather list of packet fragments
1733 *
1734 * Process an ingress ethernet packet and deliver it to the stack.
1735 */
1736int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
1737 const struct pkt_gl *si)
1738{
1739 bool csum_ok;
1740 struct sk_buff *skb;
fd3a4790
DM
1741 const struct cpl_rx_pkt *pkt;
1742 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
52367a76 1743 struct sge *s = &q->adap->sge;
d14807dd 1744 int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
0a57a536 1745 CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
fd3a4790 1746
0a57a536 1747 if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
fd3a4790
DM
1748 return handle_trace_pkt(q->adap, si);
1749
87b6cf51 1750 pkt = (const struct cpl_rx_pkt *)rsp;
cca2822d
HS
1751 csum_ok = pkt->csum_calc && !pkt->err_vec &&
1752 (q->netdev->features & NETIF_F_RXCSUM);
fd3a4790
DM
1753 if ((pkt->l2info & htonl(RXF_TCP)) &&
1754 (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
1755 do_gro(rxq, si, pkt);
1756 return 0;
1757 }
1758
1759 skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
1760 if (unlikely(!skb)) {
1761 t4_pktgl_free(si);
1762 rxq->stats.rx_drops++;
1763 return 0;
1764 }
1765
52367a76 1766 __skb_pull(skb, s->pktshift); /* remove ethernet header padding */
fd3a4790
DM
1767 skb->protocol = eth_type_trans(skb, q->netdev);
1768 skb_record_rx_queue(skb, q->idx);
87b6cf51 1769 if (skb->dev->features & NETIF_F_RXHASH)
8264989c
TH
1770 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
1771 PKT_HASH_TYPE_L3);
87b6cf51 1772
fd3a4790
DM
1773 rxq->stats.pkts++;
1774
cca2822d 1775 if (csum_ok && (pkt->l2info & htonl(RXF_UDP | RXF_TCP))) {
ba5d3c66 1776 if (!pkt->ip_frag) {
fd3a4790 1777 skb->ip_summed = CHECKSUM_UNNECESSARY;
ba5d3c66
DM
1778 rxq->stats.rx_cso++;
1779 } else if (pkt->l2info & htonl(RXF_IP)) {
fd3a4790
DM
1780 __sum16 c = (__force __sum16)pkt->csum;
1781 skb->csum = csum_unfold(c);
1782 skb->ip_summed = CHECKSUM_COMPLETE;
ba5d3c66 1783 rxq->stats.rx_cso++;
fd3a4790 1784 }
fd3a4790 1785 } else
bc8acf2c 1786 skb_checksum_none_assert(skb);
fd3a4790
DM
1787
1788 if (unlikely(pkt->vlan_ex)) {
86a9bad3 1789 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
fd3a4790 1790 rxq->stats.vlan_ex++;
19ecae2c
DM
1791 }
1792 netif_receive_skb(skb);
fd3a4790
DM
1793 return 0;
1794}
1795
1796/**
1797 * restore_rx_bufs - put back a packet's Rx buffers
1798 * @si: the packet gather list
1799 * @q: the SGE free list
1800 * @frags: number of FL buffers to restore
1801 *
1802 * Puts back on an FL the Rx buffers associated with @si. The buffers
1803 * have already been unmapped and are left unmapped, we mark them so to
1804 * prevent further unmapping attempts.
1805 *
1806 * This function undoes a series of @unmap_rx_buf calls when we find out
1807 * that the current packet can't be processed right away afterall and we
1808 * need to come back to it later. This is a very rare event and there's
1809 * no effort to make this particularly efficient.
1810 */
1811static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
1812 int frags)
1813{
1814 struct rx_sw_desc *d;
1815
1816 while (frags--) {
1817 if (q->cidx == 0)
1818 q->cidx = q->size - 1;
1819 else
1820 q->cidx--;
1821 d = &q->sdesc[q->cidx];
1822 d->page = si->frags[frags].page;
1823 d->dma_addr |= RX_UNMAPPED_BUF;
1824 q->avail++;
1825 }
1826}
1827
1828/**
1829 * is_new_response - check if a response is newly written
1830 * @r: the response descriptor
1831 * @q: the response queue
1832 *
1833 * Returns true if a response descriptor contains a yet unprocessed
1834 * response.
1835 */
1836static inline bool is_new_response(const struct rsp_ctrl *r,
1837 const struct sge_rspq *q)
1838{
1839 return RSPD_GEN(r->type_gen) == q->gen;
1840}
1841
1842/**
1843 * rspq_next - advance to the next entry in a response queue
1844 * @q: the queue
1845 *
1846 * Updates the state of a response queue to advance it to the next entry.
1847 */
1848static inline void rspq_next(struct sge_rspq *q)
1849{
1850 q->cur_desc = (void *)q->cur_desc + q->iqe_len;
1851 if (unlikely(++q->cidx == q->size)) {
1852 q->cidx = 0;
1853 q->gen ^= 1;
1854 q->cur_desc = q->desc;
1855 }
1856}
1857
1858/**
1859 * process_responses - process responses from an SGE response queue
1860 * @q: the ingress queue to process
1861 * @budget: how many responses can be processed in this round
1862 *
1863 * Process responses from an SGE response queue up to the supplied budget.
1864 * Responses include received packets as well as control messages from FW
1865 * or HW.
1866 *
1867 * Additionally choose the interrupt holdoff time for the next interrupt
1868 * on this queue. If the system is under memory shortage use a fairly
1869 * long delay to help recovery.
1870 */
1871static int process_responses(struct sge_rspq *q, int budget)
1872{
1873 int ret, rsp_type;
1874 int budget_left = budget;
1875 const struct rsp_ctrl *rc;
1876 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
52367a76
VP
1877 struct adapter *adapter = q->adap;
1878 struct sge *s = &adapter->sge;
fd3a4790
DM
1879
1880 while (likely(budget_left)) {
1881 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
1882 if (!is_new_response(rc, q))
1883 break;
1884
1885 rmb();
1886 rsp_type = RSPD_TYPE(rc->type_gen);
1887 if (likely(rsp_type == RSP_TYPE_FLBUF)) {
e91b0f24 1888 struct page_frag *fp;
fd3a4790
DM
1889 struct pkt_gl si;
1890 const struct rx_sw_desc *rsd;
1891 u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
1892
1893 if (len & RSPD_NEWBUF) {
1894 if (likely(q->offset > 0)) {
1895 free_rx_bufs(q->adap, &rxq->fl, 1);
1896 q->offset = 0;
1897 }
1704d748 1898 len = RSPD_LEN(len);
fd3a4790
DM
1899 }
1900 si.tot_len = len;
1901
1902 /* gather packet fragments */
1903 for (frags = 0, fp = si.frags; ; frags++, fp++) {
1904 rsd = &rxq->fl.sdesc[rxq->fl.cidx];
52367a76 1905 bufsz = get_buf_size(adapter, rsd);
fd3a4790 1906 fp->page = rsd->page;
e91b0f24
IC
1907 fp->offset = q->offset;
1908 fp->size = min(bufsz, len);
1909 len -= fp->size;
fd3a4790
DM
1910 if (!len)
1911 break;
1912 unmap_rx_buf(q->adap, &rxq->fl);
1913 }
1914
1915 /*
1916 * Last buffer remains mapped so explicitly make it
1917 * coherent for CPU access.
1918 */
1919 dma_sync_single_for_cpu(q->adap->pdev_dev,
1920 get_buf_addr(rsd),
e91b0f24 1921 fp->size, DMA_FROM_DEVICE);
fd3a4790
DM
1922
1923 si.va = page_address(si.frags[0].page) +
e91b0f24 1924 si.frags[0].offset;
fd3a4790
DM
1925 prefetch(si.va);
1926
1927 si.nfrags = frags + 1;
1928 ret = q->handler(q, q->cur_desc, &si);
1929 if (likely(ret == 0))
52367a76 1930 q->offset += ALIGN(fp->size, s->fl_align);
fd3a4790
DM
1931 else
1932 restore_rx_bufs(&si, &rxq->fl, frags);
1933 } else if (likely(rsp_type == RSP_TYPE_CPL)) {
1934 ret = q->handler(q, q->cur_desc, NULL);
1935 } else {
1936 ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
1937 }
1938
1939 if (unlikely(ret)) {
1940 /* couldn't process descriptor, back off for recovery */
1941 q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
1942 break;
1943 }
1944
1945 rspq_next(q);
1946 budget_left--;
1947 }
1948
1949 if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
1950 __refill_fl(q->adap, &rxq->fl);
1951 return budget - budget_left;
1952}
1953
1954/**
1955 * napi_rx_handler - the NAPI handler for Rx processing
1956 * @napi: the napi instance
1957 * @budget: how many packets we can process in this round
1958 *
1959 * Handler for new data events when using NAPI. This does not need any
1960 * locking or protection from interrupts as data interrupts are off at
1961 * this point and other adapter interrupts do not interfere (the latter
1962 * in not a concern at all with MSI-X as non-data interrupts then have
1963 * a separate handler).
1964 */
1965static int napi_rx_handler(struct napi_struct *napi, int budget)
1966{
1967 unsigned int params;
1968 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
1969 int work_done = process_responses(q, budget);
d63a6dcf 1970 u32 val;
fd3a4790
DM
1971
1972 if (likely(work_done < budget)) {
e553ec3f
HS
1973 int timer_index;
1974
fd3a4790 1975 napi_complete(napi);
e553ec3f
HS
1976 timer_index = QINTR_TIMER_IDX_GET(q->next_intr_params);
1977
1978 if (q->adaptive_rx) {
1979 if (work_done > max(timer_pkt_quota[timer_index],
1980 MIN_NAPI_WORK))
1981 timer_index = (timer_index + 1);
1982 else
1983 timer_index = timer_index - 1;
1984
1985 timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
1986 q->next_intr_params = QINTR_TIMER_IDX(timer_index) |
1987 V_QINTR_CNT_EN;
1988 params = q->next_intr_params;
1989 } else {
1990 params = q->next_intr_params;
1991 q->next_intr_params = q->intr_params;
1992 }
fd3a4790
DM
1993 } else
1994 params = QINTR_TIMER_IDX(7);
1995
d63a6dcf
HS
1996 val = CIDXINC(work_done) | SEINTARM(params);
1997 if (is_t4(q->adap->params.chip)) {
1998 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS),
1999 val | INGRESSQID((u32)q->cntxt_id));
2000 } else {
2001 writel(val, q->adap->bar2 + q->udb + SGE_UDB_GTS);
2002 wmb();
2003 }
fd3a4790
DM
2004 return work_done;
2005}
2006
2007/*
2008 * The MSI-X interrupt handler for an SGE response queue.
2009 */
2010irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
2011{
2012 struct sge_rspq *q = cookie;
2013
2014 napi_schedule(&q->napi);
2015 return IRQ_HANDLED;
2016}
2017
2018/*
2019 * Process the indirect interrupt entries in the interrupt queue and kick off
2020 * NAPI for each queue that has generated an entry.
2021 */
2022static unsigned int process_intrq(struct adapter *adap)
2023{
2024 unsigned int credits;
2025 const struct rsp_ctrl *rc;
2026 struct sge_rspq *q = &adap->sge.intrq;
d63a6dcf 2027 u32 val;
fd3a4790
DM
2028
2029 spin_lock(&adap->sge.intrq_lock);
2030 for (credits = 0; ; credits++) {
2031 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
2032 if (!is_new_response(rc, q))
2033 break;
2034
2035 rmb();
2036 if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
2037 unsigned int qid = ntohl(rc->pldbuflen_qid);
2038
e46dab4d 2039 qid -= adap->sge.ingr_start;
fd3a4790
DM
2040 napi_schedule(&adap->sge.ingr_map[qid]->napi);
2041 }
2042
2043 rspq_next(q);
2044 }
2045
d63a6dcf
HS
2046 val = CIDXINC(credits) | SEINTARM(q->intr_params);
2047 if (is_t4(adap->params.chip)) {
2048 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS),
2049 val | INGRESSQID(q->cntxt_id));
2050 } else {
2051 writel(val, adap->bar2 + q->udb + SGE_UDB_GTS);
2052 wmb();
2053 }
fd3a4790
DM
2054 spin_unlock(&adap->sge.intrq_lock);
2055 return credits;
2056}
2057
2058/*
2059 * The MSI interrupt handler, which handles data events from SGE response queues
2060 * as well as error and other async events as they all use the same MSI vector.
2061 */
2062static irqreturn_t t4_intr_msi(int irq, void *cookie)
2063{
2064 struct adapter *adap = cookie;
2065
2066 t4_slow_intr_handler(adap);
2067 process_intrq(adap);
2068 return IRQ_HANDLED;
2069}
2070
2071/*
2072 * Interrupt handler for legacy INTx interrupts.
2073 * Handles data events from SGE response queues as well as error and other
2074 * async events as they all use the same interrupt line.
2075 */
2076static irqreturn_t t4_intr_intx(int irq, void *cookie)
2077{
2078 struct adapter *adap = cookie;
2079
2080 t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI), 0);
2081 if (t4_slow_intr_handler(adap) | process_intrq(adap))
2082 return IRQ_HANDLED;
2083 return IRQ_NONE; /* probably shared interrupt */
2084}
2085
2086/**
2087 * t4_intr_handler - select the top-level interrupt handler
2088 * @adap: the adapter
2089 *
2090 * Selects the top-level interrupt handler based on the type of interrupts
2091 * (MSI-X, MSI, or INTx).
2092 */
2093irq_handler_t t4_intr_handler(struct adapter *adap)
2094{
2095 if (adap->flags & USING_MSIX)
2096 return t4_sge_intr_msix;
2097 if (adap->flags & USING_MSI)
2098 return t4_intr_msi;
2099 return t4_intr_intx;
2100}
2101
2102static void sge_rx_timer_cb(unsigned long data)
2103{
2104 unsigned long m;
0f4d201f 2105 unsigned int i, idma_same_state_cnt[2];
fd3a4790
DM
2106 struct adapter *adap = (struct adapter *)data;
2107 struct sge *s = &adap->sge;
2108
2109 for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++)
2110 for (m = s->starving_fl[i]; m; m &= m - 1) {
2111 struct sge_eth_rxq *rxq;
2112 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
2113 struct sge_fl *fl = s->egr_map[id];
2114
2115 clear_bit(id, s->starving_fl);
4e857c58 2116 smp_mb__after_atomic();
fd3a4790
DM
2117
2118 if (fl_starving(fl)) {
2119 rxq = container_of(fl, struct sge_eth_rxq, fl);
2120 if (napi_reschedule(&rxq->rspq.napi))
2121 fl->starving++;
2122 else
2123 set_bit(id, s->starving_fl);
2124 }
2125 }
2126
2127 t4_write_reg(adap, SGE_DEBUG_INDEX, 13);
0f4d201f
KS
2128 idma_same_state_cnt[0] = t4_read_reg(adap, SGE_DEBUG_DATA_HIGH);
2129 idma_same_state_cnt[1] = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
2130
2131 for (i = 0; i < 2; i++) {
2132 u32 debug0, debug11;
2133
2134 /* If the Ingress DMA Same State Counter ("timer") is less
2135 * than 1s, then we can reset our synthesized Stall Timer and
2136 * continue. If we have previously emitted warnings about a
2137 * potential stalled Ingress Queue, issue a note indicating
2138 * that the Ingress Queue has resumed forward progress.
2139 */
2140 if (idma_same_state_cnt[i] < s->idma_1s_thresh) {
2141 if (s->idma_stalled[i] >= SGE_IDMA_WARN_THRESH)
2142 CH_WARN(adap, "SGE idma%d, queue%u,resumed after %d sec\n",
2143 i, s->idma_qid[i],
2144 s->idma_stalled[i]/HZ);
2145 s->idma_stalled[i] = 0;
2146 continue;
2147 }
2148
2149 /* Synthesize an SGE Ingress DMA Same State Timer in the Hz
2150 * domain. The first time we get here it'll be because we
2151 * passed the 1s Threshold; each additional time it'll be
2152 * because the RX Timer Callback is being fired on its regular
2153 * schedule.
2154 *
2155 * If the stall is below our Potential Hung Ingress Queue
2156 * Warning Threshold, continue.
2157 */
2158 if (s->idma_stalled[i] == 0)
2159 s->idma_stalled[i] = HZ;
2160 else
2161 s->idma_stalled[i] += RX_QCHECK_PERIOD;
2162
2163 if (s->idma_stalled[i] < SGE_IDMA_WARN_THRESH)
2164 continue;
2165
2166 /* We'll issue a warning every SGE_IDMA_WARN_REPEAT Hz */
2167 if (((s->idma_stalled[i] - HZ) % SGE_IDMA_WARN_REPEAT) != 0)
2168 continue;
2169
2170 /* Read and save the SGE IDMA State and Queue ID information.
2171 * We do this every time in case it changes across time ...
2172 */
2173 t4_write_reg(adap, SGE_DEBUG_INDEX, 0);
2174 debug0 = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
2175 s->idma_state[i] = (debug0 >> (i * 9)) & 0x3f;
2176
2177 t4_write_reg(adap, SGE_DEBUG_INDEX, 11);
2178 debug11 = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
2179 s->idma_qid[i] = (debug11 >> (i * 16)) & 0xffff;
2180
2181 CH_WARN(adap, "SGE idma%u, queue%u, maybe stuck state%u %dsecs (debug0=%#x, debug11=%#x)\n",
2182 i, s->idma_qid[i], s->idma_state[i],
2183 s->idma_stalled[i]/HZ, debug0, debug11);
2184 t4_sge_decode_idma_state(adap, s->idma_state[i]);
2185 }
fd3a4790
DM
2186
2187 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
2188}
2189
2190static void sge_tx_timer_cb(unsigned long data)
2191{
2192 unsigned long m;
2193 unsigned int i, budget;
2194 struct adapter *adap = (struct adapter *)data;
2195 struct sge *s = &adap->sge;
2196
2197 for (i = 0; i < ARRAY_SIZE(s->txq_maperr); i++)
2198 for (m = s->txq_maperr[i]; m; m &= m - 1) {
2199 unsigned long id = __ffs(m) + i * BITS_PER_LONG;
2200 struct sge_ofld_txq *txq = s->egr_map[id];
2201
2202 clear_bit(id, s->txq_maperr);
2203 tasklet_schedule(&txq->qresume_tsk);
2204 }
2205
2206 budget = MAX_TIMER_TX_RECLAIM;
2207 i = s->ethtxq_rover;
2208 do {
2209 struct sge_eth_txq *q = &s->ethtxq[i];
2210
2211 if (q->q.in_use &&
2212 time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
2213 __netif_tx_trylock(q->txq)) {
2214 int avail = reclaimable(&q->q);
2215
2216 if (avail) {
2217 if (avail > budget)
2218 avail = budget;
2219
2220 free_tx_desc(adap, &q->q, avail, true);
2221 q->q.in_use -= avail;
2222 budget -= avail;
2223 }
2224 __netif_tx_unlock(q->txq);
2225 }
2226
2227 if (++i >= s->ethqsets)
2228 i = 0;
2229 } while (budget && i != s->ethtxq_rover);
2230 s->ethtxq_rover = i;
2231 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
2232}
2233
d63a6dcf
HS
2234/**
2235 * udb_address - return the BAR2 User Doorbell address for a Queue
2236 * @adap: the adapter
2237 * @cntxt_id: the Queue Context ID
2238 * @qpp: Queues Per Page (for all PFs)
2239 *
2240 * Returns the BAR2 address of the user Doorbell associated with the
2241 * indicated Queue Context ID. Note that this is only applicable
2242 * for T5 and later.
2243 */
2244static u64 udb_address(struct adapter *adap, unsigned int cntxt_id,
2245 unsigned int qpp)
2246{
2247 u64 udb;
2248 unsigned int s_qpp;
2249 unsigned short udb_density;
2250 unsigned long qpshift;
2251 int page;
2252
2253 BUG_ON(is_t4(adap->params.chip));
2254
2255 s_qpp = (QUEUESPERPAGEPF0 +
2256 (QUEUESPERPAGEPF1 - QUEUESPERPAGEPF0) * adap->fn);
2257 udb_density = 1 << ((qpp >> s_qpp) & QUEUESPERPAGEPF0_MASK);
2258 qpshift = PAGE_SHIFT - ilog2(udb_density);
9fef8478 2259 udb = (u64)cntxt_id << qpshift;
d63a6dcf
HS
2260 udb &= PAGE_MASK;
2261 page = udb / PAGE_SIZE;
2262 udb += (cntxt_id - (page * udb_density)) * SGE_UDB_SIZE;
2263
2264 return udb;
2265}
2266
2267static u64 udb_address_eq(struct adapter *adap, unsigned int cntxt_id)
2268{
2269 return udb_address(adap, cntxt_id,
2270 t4_read_reg(adap, SGE_EGRESS_QUEUES_PER_PAGE_PF));
2271}
2272
2273static u64 udb_address_iq(struct adapter *adap, unsigned int cntxt_id)
2274{
2275 return udb_address(adap, cntxt_id,
2276 t4_read_reg(adap, SGE_INGRESS_QUEUES_PER_PAGE_PF));
2277}
2278
fd3a4790
DM
2279int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
2280 struct net_device *dev, int intr_idx,
2281 struct sge_fl *fl, rspq_handler_t hnd)
2282{
2283 int ret, flsz = 0;
2284 struct fw_iq_cmd c;
52367a76 2285 struct sge *s = &adap->sge;
fd3a4790
DM
2286 struct port_info *pi = netdev_priv(dev);
2287
2288 /* Size needs to be multiple of 16, including status entry. */
2289 iq->size = roundup(iq->size, 16);
2290
2291 iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
ad6bad3e 2292 &iq->phys_addr, NULL, 0, NUMA_NO_NODE);
fd3a4790
DM
2293 if (!iq->desc)
2294 return -ENOMEM;
2295
2296 memset(&c, 0, sizeof(c));
2297 c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
2298 FW_CMD_WRITE | FW_CMD_EXEC |
060e0c75 2299 FW_IQ_CMD_PFN(adap->fn) | FW_IQ_CMD_VFN(0));
fd3a4790
DM
2300 c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC | FW_IQ_CMD_IQSTART(1) |
2301 FW_LEN16(c));
2302 c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
2303 FW_IQ_CMD_IQASYNCH(fwevtq) | FW_IQ_CMD_VIID(pi->viid) |
2304 FW_IQ_CMD_IQANDST(intr_idx < 0) | FW_IQ_CMD_IQANUD(1) |
2305 FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
2306 -intr_idx - 1));
2307 c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
2308 FW_IQ_CMD_IQGTSMODE |
2309 FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
2310 FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
2311 c.iqsize = htons(iq->size);
2312 c.iqaddr = cpu_to_be64(iq->phys_addr);
2313
2314 if (fl) {
2315 fl->size = roundup(fl->size, 8);
2316 fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
2317 sizeof(struct rx_sw_desc), &fl->addr,
52367a76 2318 &fl->sdesc, s->stat_len, NUMA_NO_NODE);
fd3a4790
DM
2319 if (!fl->desc)
2320 goto fl_nomem;
2321
52367a76 2322 flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
ce91a923 2323 c.iqns_to_fl0congen = htonl(FW_IQ_CMD_FL0PACKEN(1) |
ef306b50
DM
2324 FW_IQ_CMD_FL0FETCHRO(1) |
2325 FW_IQ_CMD_FL0DATARO(1) |
ce91a923 2326 FW_IQ_CMD_FL0PADEN(1));
fd3a4790
DM
2327 c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN(2) |
2328 FW_IQ_CMD_FL0FBMAX(3));
2329 c.fl0size = htons(flsz);
2330 c.fl0addr = cpu_to_be64(fl->addr);
2331 }
2332
060e0c75 2333 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
fd3a4790
DM
2334 if (ret)
2335 goto err;
2336
2337 netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
2338 iq->cur_desc = iq->desc;
2339 iq->cidx = 0;
2340 iq->gen = 1;
2341 iq->next_intr_params = iq->intr_params;
2342 iq->cntxt_id = ntohs(c.iqid);
2343 iq->abs_id = ntohs(c.physiqid);
d63a6dcf
HS
2344 if (!is_t4(adap->params.chip))
2345 iq->udb = udb_address_iq(adap, iq->cntxt_id);
fd3a4790 2346 iq->size--; /* subtract status entry */
fd3a4790
DM
2347 iq->netdev = dev;
2348 iq->handler = hnd;
2349
2350 /* set offset to -1 to distinguish ingress queues without FL */
2351 iq->offset = fl ? 0 : -1;
2352
e46dab4d 2353 adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
fd3a4790
DM
2354
2355 if (fl) {
62718b32 2356 fl->cntxt_id = ntohs(c.fl0id);
fd3a4790
DM
2357 fl->avail = fl->pend_cred = 0;
2358 fl->pidx = fl->cidx = 0;
2359 fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
e46dab4d 2360 adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
d63a6dcf
HS
2361
2362 /* Note, we must initialize the Free List User Doorbell
2363 * address before refilling the Free List!
2364 */
2365 if (!is_t4(adap->params.chip))
2366 fl->udb = udb_address_eq(adap, fl->cntxt_id);
fd3a4790
DM
2367 refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
2368 }
2369 return 0;
2370
2371fl_nomem:
2372 ret = -ENOMEM;
2373err:
2374 if (iq->desc) {
2375 dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
2376 iq->desc, iq->phys_addr);
2377 iq->desc = NULL;
2378 }
2379 if (fl && fl->desc) {
2380 kfree(fl->sdesc);
2381 fl->sdesc = NULL;
2382 dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
2383 fl->desc, fl->addr);
2384 fl->desc = NULL;
2385 }
2386 return ret;
2387}
2388
2389static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
2390{
22adfe0a 2391 q->cntxt_id = id;
d63a6dcf
HS
2392 if (!is_t4(adap->params.chip))
2393 q->udb = udb_address_eq(adap, q->cntxt_id);
22adfe0a 2394
fd3a4790
DM
2395 q->in_use = 0;
2396 q->cidx = q->pidx = 0;
2397 q->stops = q->restarts = 0;
2398 q->stat = (void *)&q->desc[q->size];
3069ee9b 2399 spin_lock_init(&q->db_lock);
e46dab4d 2400 adap->sge.egr_map[id - adap->sge.egr_start] = q;
fd3a4790
DM
2401}
2402
2403int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
2404 struct net_device *dev, struct netdev_queue *netdevq,
2405 unsigned int iqid)
2406{
2407 int ret, nentries;
2408 struct fw_eq_eth_cmd c;
52367a76 2409 struct sge *s = &adap->sge;
fd3a4790
DM
2410 struct port_info *pi = netdev_priv(dev);
2411
2412 /* Add status entries */
52367a76 2413 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
fd3a4790
DM
2414
2415 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2416 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
52367a76 2417 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
ad6bad3e 2418 netdev_queue_numa_node_read(netdevq));
fd3a4790
DM
2419 if (!txq->q.desc)
2420 return -ENOMEM;
2421
2422 memset(&c, 0, sizeof(c));
2423 c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
2424 FW_CMD_WRITE | FW_CMD_EXEC |
060e0c75 2425 FW_EQ_ETH_CMD_PFN(adap->fn) | FW_EQ_ETH_CMD_VFN(0));
fd3a4790
DM
2426 c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC |
2427 FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
08f1a1b9
HS
2428 c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE |
2429 FW_EQ_ETH_CMD_VIID(pi->viid));
fd3a4790
DM
2430 c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE(2) |
2431 FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
ef306b50 2432 FW_EQ_ETH_CMD_FETCHRO(1) |
fd3a4790
DM
2433 FW_EQ_ETH_CMD_IQID(iqid));
2434 c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN(2) |
2435 FW_EQ_ETH_CMD_FBMAX(3) |
2436 FW_EQ_ETH_CMD_CIDXFTHRESH(5) |
2437 FW_EQ_ETH_CMD_EQSIZE(nentries));
2438 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2439
060e0c75 2440 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
fd3a4790
DM
2441 if (ret) {
2442 kfree(txq->q.sdesc);
2443 txq->q.sdesc = NULL;
2444 dma_free_coherent(adap->pdev_dev,
2445 nentries * sizeof(struct tx_desc),
2446 txq->q.desc, txq->q.phys_addr);
2447 txq->q.desc = NULL;
2448 return ret;
2449 }
2450
2451 init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_GET(ntohl(c.eqid_pkd)));
2452 txq->txq = netdevq;
2453 txq->tso = txq->tx_cso = txq->vlan_ins = 0;
2454 txq->mapping_err = 0;
2455 return 0;
2456}
2457
2458int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
2459 struct net_device *dev, unsigned int iqid,
2460 unsigned int cmplqid)
2461{
2462 int ret, nentries;
2463 struct fw_eq_ctrl_cmd c;
52367a76 2464 struct sge *s = &adap->sge;
fd3a4790
DM
2465 struct port_info *pi = netdev_priv(dev);
2466
2467 /* Add status entries */
52367a76 2468 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
fd3a4790
DM
2469
2470 txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
2471 sizeof(struct tx_desc), 0, &txq->q.phys_addr,
ad6bad3e 2472 NULL, 0, NUMA_NO_NODE);
fd3a4790
DM
2473 if (!txq->q.desc)
2474 return -ENOMEM;
2475
2476 c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
2477 FW_CMD_WRITE | FW_CMD_EXEC |
060e0c75
DM
2478 FW_EQ_CTRL_CMD_PFN(adap->fn) |
2479 FW_EQ_CTRL_CMD_VFN(0));
fd3a4790
DM
2480 c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC |
2481 FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
2482 c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID(cmplqid));
2483 c.physeqid_pkd = htonl(0);
2484 c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE(2) |
2485 FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
ef306b50 2486 FW_EQ_CTRL_CMD_FETCHRO |
fd3a4790
DM
2487 FW_EQ_CTRL_CMD_IQID(iqid));
2488 c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN(2) |
2489 FW_EQ_CTRL_CMD_FBMAX(3) |
2490 FW_EQ_CTRL_CMD_CIDXFTHRESH(5) |
2491 FW_EQ_CTRL_CMD_EQSIZE(nentries));
2492 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2493
060e0c75 2494 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
fd3a4790
DM
2495 if (ret) {
2496 dma_free_coherent(adap->pdev_dev,
2497 nentries * sizeof(struct tx_desc),
2498 txq->q.desc, txq->q.phys_addr);
2499 txq->q.desc = NULL;
2500 return ret;
2501 }
2502
2503 init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_GET(ntohl(c.cmpliqid_eqid)));
2504 txq->adap = adap;
2505 skb_queue_head_init(&txq->sendq);
2506 tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
2507 txq->full = 0;
2508 return 0;
2509}
2510
2511int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
2512 struct net_device *dev, unsigned int iqid)
2513{
2514 int ret, nentries;
2515 struct fw_eq_ofld_cmd c;
52367a76 2516 struct sge *s = &adap->sge;
fd3a4790
DM
2517 struct port_info *pi = netdev_priv(dev);
2518
2519 /* Add status entries */
52367a76 2520 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
fd3a4790
DM
2521
2522 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2523 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
52367a76 2524 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
ad6bad3e 2525 NUMA_NO_NODE);
fd3a4790
DM
2526 if (!txq->q.desc)
2527 return -ENOMEM;
2528
2529 memset(&c, 0, sizeof(c));
2530 c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
2531 FW_CMD_WRITE | FW_CMD_EXEC |
060e0c75
DM
2532 FW_EQ_OFLD_CMD_PFN(adap->fn) |
2533 FW_EQ_OFLD_CMD_VFN(0));
fd3a4790
DM
2534 c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC |
2535 FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
2536 c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE(2) |
2537 FW_EQ_OFLD_CMD_PCIECHN(pi->tx_chan) |
ef306b50 2538 FW_EQ_OFLD_CMD_FETCHRO(1) |
fd3a4790
DM
2539 FW_EQ_OFLD_CMD_IQID(iqid));
2540 c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN(2) |
2541 FW_EQ_OFLD_CMD_FBMAX(3) |
2542 FW_EQ_OFLD_CMD_CIDXFTHRESH(5) |
2543 FW_EQ_OFLD_CMD_EQSIZE(nentries));
2544 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2545
060e0c75 2546 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
fd3a4790
DM
2547 if (ret) {
2548 kfree(txq->q.sdesc);
2549 txq->q.sdesc = NULL;
2550 dma_free_coherent(adap->pdev_dev,
2551 nentries * sizeof(struct tx_desc),
2552 txq->q.desc, txq->q.phys_addr);
2553 txq->q.desc = NULL;
2554 return ret;
2555 }
2556
2557 init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_GET(ntohl(c.eqid_pkd)));
2558 txq->adap = adap;
2559 skb_queue_head_init(&txq->sendq);
2560 tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
2561 txq->full = 0;
2562 txq->mapping_err = 0;
2563 return 0;
2564}
2565
2566static void free_txq(struct adapter *adap, struct sge_txq *q)
2567{
52367a76
VP
2568 struct sge *s = &adap->sge;
2569
fd3a4790 2570 dma_free_coherent(adap->pdev_dev,
52367a76 2571 q->size * sizeof(struct tx_desc) + s->stat_len,
fd3a4790
DM
2572 q->desc, q->phys_addr);
2573 q->cntxt_id = 0;
2574 q->sdesc = NULL;
2575 q->desc = NULL;
2576}
2577
2578static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
2579 struct sge_fl *fl)
2580{
52367a76 2581 struct sge *s = &adap->sge;
fd3a4790
DM
2582 unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
2583
e46dab4d 2584 adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
060e0c75
DM
2585 t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
2586 rq->cntxt_id, fl_id, 0xffff);
fd3a4790
DM
2587 dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
2588 rq->desc, rq->phys_addr);
2589 netif_napi_del(&rq->napi);
2590 rq->netdev = NULL;
2591 rq->cntxt_id = rq->abs_id = 0;
2592 rq->desc = NULL;
2593
2594 if (fl) {
2595 free_rx_bufs(adap, fl, fl->avail);
52367a76 2596 dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
fd3a4790
DM
2597 fl->desc, fl->addr);
2598 kfree(fl->sdesc);
2599 fl->sdesc = NULL;
2600 fl->cntxt_id = 0;
2601 fl->desc = NULL;
2602 }
2603}
2604
5fa76694
HS
2605/**
2606 * t4_free_ofld_rxqs - free a block of consecutive Rx queues
2607 * @adap: the adapter
2608 * @n: number of queues
2609 * @q: pointer to first queue
2610 *
2611 * Release the resources of a consecutive block of offload Rx queues.
2612 */
2613void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
2614{
2615 for ( ; n; n--, q++)
2616 if (q->rspq.desc)
2617 free_rspq_fl(adap, &q->rspq,
2618 q->fl.size ? &q->fl : NULL);
2619}
2620
fd3a4790
DM
2621/**
2622 * t4_free_sge_resources - free SGE resources
2623 * @adap: the adapter
2624 *
2625 * Frees resources used by the SGE queue sets.
2626 */
2627void t4_free_sge_resources(struct adapter *adap)
2628{
2629 int i;
2630 struct sge_eth_rxq *eq = adap->sge.ethrxq;
2631 struct sge_eth_txq *etq = adap->sge.ethtxq;
fd3a4790
DM
2632
2633 /* clean up Ethernet Tx/Rx queues */
2634 for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
2635 if (eq->rspq.desc)
5fa76694
HS
2636 free_rspq_fl(adap, &eq->rspq,
2637 eq->fl.size ? &eq->fl : NULL);
fd3a4790 2638 if (etq->q.desc) {
060e0c75
DM
2639 t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
2640 etq->q.cntxt_id);
fd3a4790
DM
2641 free_tx_desc(adap, &etq->q, etq->q.in_use, true);
2642 kfree(etq->q.sdesc);
2643 free_txq(adap, &etq->q);
2644 }
2645 }
2646
2647 /* clean up RDMA and iSCSI Rx queues */
5fa76694
HS
2648 t4_free_ofld_rxqs(adap, adap->sge.ofldqsets, adap->sge.ofldrxq);
2649 t4_free_ofld_rxqs(adap, adap->sge.rdmaqs, adap->sge.rdmarxq);
2650 t4_free_ofld_rxqs(adap, adap->sge.rdmaciqs, adap->sge.rdmaciq);
fd3a4790
DM
2651
2652 /* clean up offload Tx queues */
2653 for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
2654 struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
2655
2656 if (q->q.desc) {
2657 tasklet_kill(&q->qresume_tsk);
060e0c75
DM
2658 t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
2659 q->q.cntxt_id);
fd3a4790
DM
2660 free_tx_desc(adap, &q->q, q->q.in_use, false);
2661 kfree(q->q.sdesc);
2662 __skb_queue_purge(&q->sendq);
2663 free_txq(adap, &q->q);
2664 }
2665 }
2666
2667 /* clean up control Tx queues */
2668 for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
2669 struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
2670
2671 if (cq->q.desc) {
2672 tasklet_kill(&cq->qresume_tsk);
060e0c75
DM
2673 t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
2674 cq->q.cntxt_id);
fd3a4790
DM
2675 __skb_queue_purge(&cq->sendq);
2676 free_txq(adap, &cq->q);
2677 }
2678 }
2679
2680 if (adap->sge.fw_evtq.desc)
2681 free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
2682
2683 if (adap->sge.intrq.desc)
2684 free_rspq_fl(adap, &adap->sge.intrq, NULL);
2685
2686 /* clear the reverse egress queue map */
2687 memset(adap->sge.egr_map, 0, sizeof(adap->sge.egr_map));
2688}
2689
2690void t4_sge_start(struct adapter *adap)
2691{
2692 adap->sge.ethtxq_rover = 0;
2693 mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2694 mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2695}
2696
2697/**
2698 * t4_sge_stop - disable SGE operation
2699 * @adap: the adapter
2700 *
2701 * Stop tasklets and timers associated with the DMA engine. Note that
2702 * this is effective only if measures have been taken to disable any HW
2703 * events that may restart them.
2704 */
2705void t4_sge_stop(struct adapter *adap)
2706{
2707 int i;
2708 struct sge *s = &adap->sge;
2709
2710 if (in_interrupt()) /* actions below require waiting */
2711 return;
2712
2713 if (s->rx_timer.function)
2714 del_timer_sync(&s->rx_timer);
2715 if (s->tx_timer.function)
2716 del_timer_sync(&s->tx_timer);
2717
2718 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
2719 struct sge_ofld_txq *q = &s->ofldtxq[i];
2720
2721 if (q->q.desc)
2722 tasklet_kill(&q->qresume_tsk);
2723 }
2724 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
2725 struct sge_ctrl_txq *cq = &s->ctrlq[i];
2726
2727 if (cq->q.desc)
2728 tasklet_kill(&cq->qresume_tsk);
2729 }
2730}
2731
2732/**
2733 * t4_sge_init - initialize SGE
2734 * @adap: the adapter
2735 *
2736 * Performs SGE initialization needed every time after a chip reset.
2737 * We do not initialize any of the queues here, instead the driver
2738 * top-level must request them individually.
52367a76
VP
2739 *
2740 * Called in two different modes:
2741 *
2742 * 1. Perform actual hardware initialization and record hard-coded
2743 * parameters which were used. This gets used when we're the
2744 * Master PF and the Firmware Configuration File support didn't
2745 * work for some reason.
2746 *
2747 * 2. We're not the Master PF or initialization was performed with
2748 * a Firmware Configuration File. In this case we need to grab
2749 * any of the SGE operating parameters that we need to have in
2750 * order to do our job and make sure we can live with them ...
fd3a4790 2751 */
52367a76
VP
2752
2753static int t4_sge_init_soft(struct adapter *adap)
fd3a4790
DM
2754{
2755 struct sge *s = &adap->sge;
52367a76
VP
2756 u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
2757 u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
2758 u32 ingress_rx_threshold;
fd3a4790 2759
52367a76
VP
2760 /*
2761 * Verify that CPL messages are going to the Ingress Queue for
2762 * process_responses() and that only packet data is going to the
2763 * Free Lists.
2764 */
2765 if ((t4_read_reg(adap, SGE_CONTROL) & RXPKTCPLMODE_MASK) !=
2766 RXPKTCPLMODE(X_RXPKTCPLMODE_SPLIT)) {
2767 dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
2768 return -EINVAL;
2769 }
2770
2771 /*
2772 * Validate the Host Buffer Register Array indices that we want to
2773 * use ...
2774 *
2775 * XXX Note that we should really read through the Host Buffer Size
2776 * XXX register array and find the indices of the Buffer Sizes which
2777 * XXX meet our needs!
2778 */
2779 #define READ_FL_BUF(x) \
2780 t4_read_reg(adap, SGE_FL_BUFFER_SIZE0+(x)*sizeof(u32))
2781
2782 fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
2783 fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
2784 fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
2785 fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
2786
92ddcc7b
KS
2787 /* We only bother using the Large Page logic if the Large Page Buffer
2788 * is larger than our Page Size Buffer.
2789 */
2790 if (fl_large_pg <= fl_small_pg)
2791 fl_large_pg = 0;
2792
52367a76
VP
2793 #undef READ_FL_BUF
2794
92ddcc7b
KS
2795 /* The Page Size Buffer must be exactly equal to our Page Size and the
2796 * Large Page Size Buffer should be 0 (per above) or a power of 2.
2797 */
52367a76 2798 if (fl_small_pg != PAGE_SIZE ||
92ddcc7b 2799 (fl_large_pg & (fl_large_pg-1)) != 0) {
52367a76
VP
2800 dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
2801 fl_small_pg, fl_large_pg);
2802 return -EINVAL;
2803 }
2804 if (fl_large_pg)
2805 s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
2806
2807 if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
2808 fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
2809 dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
2810 fl_small_mtu, fl_large_mtu);
2811 return -EINVAL;
2812 }
2813
2814 /*
2815 * Retrieve our RX interrupt holdoff timer values and counter
2816 * threshold values from the SGE parameters.
2817 */
2818 timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1);
2819 timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3);
2820 timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5);
2821 s->timer_val[0] = core_ticks_to_us(adap,
2822 TIMERVALUE0_GET(timer_value_0_and_1));
2823 s->timer_val[1] = core_ticks_to_us(adap,
2824 TIMERVALUE1_GET(timer_value_0_and_1));
2825 s->timer_val[2] = core_ticks_to_us(adap,
2826 TIMERVALUE2_GET(timer_value_2_and_3));
2827 s->timer_val[3] = core_ticks_to_us(adap,
2828 TIMERVALUE3_GET(timer_value_2_and_3));
2829 s->timer_val[4] = core_ticks_to_us(adap,
2830 TIMERVALUE4_GET(timer_value_4_and_5));
2831 s->timer_val[5] = core_ticks_to_us(adap,
2832 TIMERVALUE5_GET(timer_value_4_and_5));
2833
2834 ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD);
2835 s->counter_val[0] = THRESHOLD_0_GET(ingress_rx_threshold);
2836 s->counter_val[1] = THRESHOLD_1_GET(ingress_rx_threshold);
2837 s->counter_val[2] = THRESHOLD_2_GET(ingress_rx_threshold);
2838 s->counter_val[3] = THRESHOLD_3_GET(ingress_rx_threshold);
2839
2840 return 0;
2841}
2842
2843static int t4_sge_init_hard(struct adapter *adap)
2844{
2845 struct sge *s = &adap->sge;
2846
2847 /*
2848 * Set up our basic SGE mode to deliver CPL messages to our Ingress
2849 * Queue and Packet Date to the Free List.
2850 */
2851 t4_set_reg_field(adap, SGE_CONTROL, RXPKTCPLMODE_MASK,
2852 RXPKTCPLMODE_MASK);
060e0c75 2853
3069ee9b
VP
2854 /*
2855 * Set up to drop DOORBELL writes when the DOORBELL FIFO overflows
2856 * and generate an interrupt when this occurs so we can recover.
2857 */
d14807dd 2858 if (is_t4(adap->params.chip)) {
0a57a536
SR
2859 t4_set_reg_field(adap, A_SGE_DBFIFO_STATUS,
2860 V_HP_INT_THRESH(M_HP_INT_THRESH) |
2861 V_LP_INT_THRESH(M_LP_INT_THRESH),
2862 V_HP_INT_THRESH(dbfifo_int_thresh) |
2863 V_LP_INT_THRESH(dbfifo_int_thresh));
2864 } else {
2865 t4_set_reg_field(adap, A_SGE_DBFIFO_STATUS,
2866 V_LP_INT_THRESH_T5(M_LP_INT_THRESH_T5),
2867 V_LP_INT_THRESH_T5(dbfifo_int_thresh));
2868 t4_set_reg_field(adap, SGE_DBFIFO_STATUS2,
2869 V_HP_INT_THRESH_T5(M_HP_INT_THRESH_T5),
2870 V_HP_INT_THRESH_T5(dbfifo_int_thresh));
2871 }
881806bc
VP
2872 t4_set_reg_field(adap, A_SGE_DOORBELL_CONTROL, F_ENABLE_DROP,
2873 F_ENABLE_DROP);
2874
52367a76
VP
2875 /*
2876 * SGE_FL_BUFFER_SIZE0 (RX_SMALL_PG_BUF) is set up by
2877 * t4_fixup_host_params().
2878 */
2879 s->fl_pg_order = FL_PG_ORDER;
2880 if (s->fl_pg_order)
2881 t4_write_reg(adap,
2882 SGE_FL_BUFFER_SIZE0+RX_LARGE_PG_BUF*sizeof(u32),
2883 PAGE_SIZE << FL_PG_ORDER);
2884 t4_write_reg(adap, SGE_FL_BUFFER_SIZE0+RX_SMALL_MTU_BUF*sizeof(u32),
2885 FL_MTU_SMALL_BUFSIZE(adap));
2886 t4_write_reg(adap, SGE_FL_BUFFER_SIZE0+RX_LARGE_MTU_BUF*sizeof(u32),
2887 FL_MTU_LARGE_BUFSIZE(adap));
2888
2889 /*
2890 * Note that the SGE Ingress Packet Count Interrupt Threshold and
2891 * Timer Holdoff values must be supplied by our caller.
2892 */
fd3a4790
DM
2893 t4_write_reg(adap, SGE_INGRESS_RX_THRESHOLD,
2894 THRESHOLD_0(s->counter_val[0]) |
2895 THRESHOLD_1(s->counter_val[1]) |
2896 THRESHOLD_2(s->counter_val[2]) |
2897 THRESHOLD_3(s->counter_val[3]));
2898 t4_write_reg(adap, SGE_TIMER_VALUE_0_AND_1,
2899 TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[0])) |
2900 TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[1])));
2901 t4_write_reg(adap, SGE_TIMER_VALUE_2_AND_3,
52367a76
VP
2902 TIMERVALUE2(us_to_core_ticks(adap, s->timer_val[2])) |
2903 TIMERVALUE3(us_to_core_ticks(adap, s->timer_val[3])));
fd3a4790 2904 t4_write_reg(adap, SGE_TIMER_VALUE_4_AND_5,
52367a76
VP
2905 TIMERVALUE4(us_to_core_ticks(adap, s->timer_val[4])) |
2906 TIMERVALUE5(us_to_core_ticks(adap, s->timer_val[5])));
2907
2908 return 0;
2909}
2910
2911int t4_sge_init(struct adapter *adap)
2912{
2913 struct sge *s = &adap->sge;
c2b955e0
KS
2914 u32 sge_control, sge_conm_ctrl;
2915 int ret, egress_threshold;
52367a76
VP
2916
2917 /*
2918 * Ingress Padding Boundary and Egress Status Page Size are set up by
2919 * t4_fixup_host_params().
2920 */
2921 sge_control = t4_read_reg(adap, SGE_CONTROL);
2922 s->pktshift = PKTSHIFT_GET(sge_control);
2923 s->stat_len = (sge_control & EGRSTATUSPAGESIZE_MASK) ? 128 : 64;
2924 s->fl_align = 1 << (INGPADBOUNDARY_GET(sge_control) +
2925 X_INGPADBOUNDARY_SHIFT);
2926
2927 if (adap->flags & USING_SOFT_PARAMS)
2928 ret = t4_sge_init_soft(adap);
2929 else
2930 ret = t4_sge_init_hard(adap);
2931 if (ret < 0)
2932 return ret;
2933
2934 /*
2935 * A FL with <= fl_starve_thres buffers is starving and a periodic
2936 * timer will attempt to refill it. This needs to be larger than the
2937 * SGE's Egress Congestion Threshold. If it isn't, then we can get
2938 * stuck waiting for new packets while the SGE is waiting for us to
2939 * give it more Free List entries. (Note that the SGE's Egress
c2b955e0
KS
2940 * Congestion Threshold is in units of 2 Free List pointers.) For T4,
2941 * there was only a single field to control this. For T5 there's the
2942 * original field which now only applies to Unpacked Mode Free List
2943 * buffers and a new field which only applies to Packed Mode Free List
2944 * buffers.
52367a76 2945 */
c2b955e0
KS
2946 sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL);
2947 if (is_t4(adap->params.chip))
2948 egress_threshold = EGRTHRESHOLD_GET(sge_conm_ctrl);
2949 else
2950 egress_threshold = EGRTHRESHOLDPACKING_GET(sge_conm_ctrl);
2951 s->fl_starve_thres = 2*egress_threshold + 1;
52367a76 2952
fd3a4790
DM
2953 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
2954 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
0f4d201f
KS
2955 s->idma_1s_thresh = core_ticks_per_usec(adap) * 1000000; /* 1 s */
2956 s->idma_stalled[0] = 0;
2957 s->idma_stalled[1] = 0;
fd3a4790 2958 spin_lock_init(&s->intrq_lock);
52367a76
VP
2959
2960 return 0;
fd3a4790 2961}
This page took 0.550175 seconds and 5 git commands to generate.