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