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