blob: ad504d0db1ec77e3fe019703000654f903a9ce0c [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>
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +053046#ifdef CONFIG_NET_RX_BUSY_POLL
47#include <net/busy_poll.h>
48#endif /* CONFIG_NET_RX_BUSY_POLL */
Varun Prakash84a200b2015-03-24 19:14:46 +053049#ifdef CONFIG_CHELSIO_T4_FCOE
50#include <scsi/fc/fc_fcoe.h>
51#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +000052#include "cxgb4.h"
53#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053054#include "t4_values.h"
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +000055#include "t4_msg.h"
56#include "t4fw_api.h"
57
58/*
59 * Rx buffer size. We use largish buffers if possible but settle for single
60 * pages under memory shortage.
61 */
62#if PAGE_SHIFT >= 16
63# define FL_PG_ORDER 0
64#else
65# define FL_PG_ORDER (16 - PAGE_SHIFT)
66#endif
67
68/* RX_PULL_LEN should be <= RX_COPY_THRES */
69#define RX_COPY_THRES 256
70#define RX_PULL_LEN 128
71
72/*
73 * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
74 * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
75 */
76#define RX_PKT_SKB_LEN 512
77
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +000078/*
79 * Max number of Tx descriptors we clean up at a time. Should be modest as
80 * freeing skbs isn't cheap and it happens while holding locks. We just need
81 * to free packets faster than they arrive, we eventually catch up and keep
82 * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
83 */
84#define MAX_TX_RECLAIM 16
85
86/*
87 * Max number of Rx buffers we replenish at a time. Again keep this modest,
88 * allocating buffers isn't cheap either.
89 */
90#define MAX_RX_REFILL 16U
91
92/*
93 * Period of the Rx queue check timer. This timer is infrequent as it has
94 * something to do only when the system experiences severe memory shortage.
95 */
96#define RX_QCHECK_PERIOD (HZ / 2)
97
98/*
99 * Period of the Tx queue check timer.
100 */
101#define TX_QCHECK_PERIOD (HZ / 2)
102
103/*
104 * Max number of Tx descriptors to be reclaimed by the Tx timer.
105 */
106#define MAX_TIMER_TX_RECLAIM 100
107
108/*
109 * Timer index used when backing off due to memory shortage.
110 */
111#define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
112
113/*
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000114 * Suspend an Ethernet Tx queue with fewer available descriptors than this.
115 * This is the same as calc_tx_descs() for a TSO packet with
116 * nr_frags == MAX_SKB_FRAGS.
117 */
118#define ETHTXQ_STOP_THRES \
119 (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
120
121/*
122 * Suspension threshold for non-Ethernet Tx queues. We require enough room
123 * for a full sized WR.
124 */
125#define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
126
127/*
128 * Max Tx descriptor space we allow for an Ethernet packet to be inlined
129 * into a WR.
130 */
Hariprasad Shenai21dcfad2015-04-15 02:02:30 +0530131#define MAX_IMM_TX_PKT_LEN 256
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000132
133/*
134 * Max size of a WR sent through a control Tx queue.
135 */
136#define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
137
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000138struct tx_sw_desc { /* SW state per Tx descriptor */
139 struct sk_buff *skb;
140 struct ulptx_sgl *sgl;
141};
142
143struct rx_sw_desc { /* SW state per Rx descriptor */
144 struct page *page;
145 dma_addr_t dma_addr;
146};
147
148/*
Vipul Pandya52367a72012-09-26 02:39:38 +0000149 * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
150 * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
151 * We could easily support more but there doesn't seem to be much need for
152 * that ...
153 */
154#define FL_MTU_SMALL 1500
155#define FL_MTU_LARGE 9000
156
157static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
158 unsigned int mtu)
159{
160 struct sge *s = &adapter->sge;
161
162 return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
163}
164
165#define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
166#define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
167
168/*
169 * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
170 * these to specify the buffer size as an index into the SGE Free List Buffer
171 * Size register array. We also use bit 4, when the buffer has been unmapped
172 * for DMA, but this is of course never sent to the hardware and is only used
173 * to prevent double unmappings. All of the above requires that the Free List
174 * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
175 * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
176 * Free List Buffer alignment is 32 bytes, this works out for us ...
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000177 */
178enum {
Vipul Pandya52367a72012-09-26 02:39:38 +0000179 RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
180 RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
181 RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
182
183 /*
184 * XXX We shouldn't depend on being able to use these indices.
185 * XXX Especially when some other Master PF has initialized the
186 * XXX adapter or we use the Firmware Configuration File. We
187 * XXX should really search through the Host Buffer Size register
188 * XXX array for the appropriately sized buffer indices.
189 */
190 RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
191 RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
192
193 RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
194 RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000195};
196
Hariprasad Shenaie553ec32014-09-26 00:23:55 +0530197static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
198#define MIN_NAPI_WORK 1
199
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000200static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
201{
Vipul Pandya52367a72012-09-26 02:39:38 +0000202 return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000203}
204
205static inline bool is_buf_mapped(const struct rx_sw_desc *d)
206{
207 return !(d->dma_addr & RX_UNMAPPED_BUF);
208}
209
210/**
211 * txq_avail - return the number of available slots in a Tx queue
212 * @q: the Tx queue
213 *
214 * Returns the number of descriptors in a Tx queue available to write new
215 * packets.
216 */
217static inline unsigned int txq_avail(const struct sge_txq *q)
218{
219 return q->size - 1 - q->in_use;
220}
221
222/**
223 * fl_cap - return the capacity of a free-buffer list
224 * @fl: the FL
225 *
226 * Returns the capacity of a free-buffer list. The capacity is less than
227 * the size because one descriptor needs to be left unpopulated, otherwise
228 * HW will think the FL is empty.
229 */
230static inline unsigned int fl_cap(const struct sge_fl *fl)
231{
232 return fl->size - 8; /* 1 descriptor = 8 buffers */
233}
234
Hariprasad Shenaic098b022015-04-15 02:02:31 +0530235/**
236 * fl_starving - return whether a Free List is starving.
237 * @adapter: pointer to the adapter
238 * @fl: the Free List
239 *
240 * Tests specified Free List to see whether the number of buffers
241 * available to the hardware has falled below our "starvation"
242 * threshold.
243 */
244static inline bool fl_starving(const struct adapter *adapter,
245 const struct sge_fl *fl)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000246{
Hariprasad Shenaic098b022015-04-15 02:02:31 +0530247 const struct sge *s = &adapter->sge;
248
249 return fl->avail - fl->pend_cred <= s->fl_starve_thres;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000250}
251
252static int map_skb(struct device *dev, const struct sk_buff *skb,
253 dma_addr_t *addr)
254{
255 const skb_frag_t *fp, *end;
256 const struct skb_shared_info *si;
257
258 *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
259 if (dma_mapping_error(dev, *addr))
260 goto out_err;
261
262 si = skb_shinfo(skb);
263 end = &si->frags[si->nr_frags];
264
265 for (fp = si->frags; fp < end; fp++) {
Ian Campbelle91b0f22011-10-19 23:01:46 +0000266 *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
267 DMA_TO_DEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000268 if (dma_mapping_error(dev, *addr))
269 goto unwind;
270 }
271 return 0;
272
273unwind:
274 while (fp-- > si->frags)
Eric Dumazet9e903e02011-10-18 21:00:24 +0000275 dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000276
277 dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
278out_err:
279 return -ENOMEM;
280}
281
282#ifdef CONFIG_NEED_DMA_MAP_STATE
283static void unmap_skb(struct device *dev, const struct sk_buff *skb,
284 const dma_addr_t *addr)
285{
286 const skb_frag_t *fp, *end;
287 const struct skb_shared_info *si;
288
289 dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
290
291 si = skb_shinfo(skb);
292 end = &si->frags[si->nr_frags];
293 for (fp = si->frags; fp < end; fp++)
Eric Dumazet9e903e02011-10-18 21:00:24 +0000294 dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000295}
296
297/**
298 * deferred_unmap_destructor - unmap a packet when it is freed
299 * @skb: the packet
300 *
301 * This is the packet destructor used for Tx packets that need to remain
302 * mapped until they are freed rather than until their Tx descriptors are
303 * freed.
304 */
305static void deferred_unmap_destructor(struct sk_buff *skb)
306{
307 unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
308}
309#endif
310
311static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
312 const struct ulptx_sgl *sgl, const struct sge_txq *q)
313{
314 const struct ulptx_sge_pair *p;
315 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
316
317 if (likely(skb_headlen(skb)))
318 dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
319 DMA_TO_DEVICE);
320 else {
321 dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
322 DMA_TO_DEVICE);
323 nfrags--;
324 }
325
326 /*
327 * the complexity below is because of the possibility of a wrap-around
328 * in the middle of an SGL
329 */
330 for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
331 if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
332unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
333 ntohl(p->len[0]), DMA_TO_DEVICE);
334 dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
335 ntohl(p->len[1]), DMA_TO_DEVICE);
336 p++;
337 } else if ((u8 *)p == (u8 *)q->stat) {
338 p = (const struct ulptx_sge_pair *)q->desc;
339 goto unmap;
340 } else if ((u8 *)p + 8 == (u8 *)q->stat) {
341 const __be64 *addr = (const __be64 *)q->desc;
342
343 dma_unmap_page(dev, be64_to_cpu(addr[0]),
344 ntohl(p->len[0]), DMA_TO_DEVICE);
345 dma_unmap_page(dev, be64_to_cpu(addr[1]),
346 ntohl(p->len[1]), DMA_TO_DEVICE);
347 p = (const struct ulptx_sge_pair *)&addr[2];
348 } else {
349 const __be64 *addr = (const __be64 *)q->desc;
350
351 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
352 ntohl(p->len[0]), DMA_TO_DEVICE);
353 dma_unmap_page(dev, be64_to_cpu(addr[0]),
354 ntohl(p->len[1]), DMA_TO_DEVICE);
355 p = (const struct ulptx_sge_pair *)&addr[1];
356 }
357 }
358 if (nfrags) {
359 __be64 addr;
360
361 if ((u8 *)p == (u8 *)q->stat)
362 p = (const struct ulptx_sge_pair *)q->desc;
363 addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
364 *(const __be64 *)q->desc;
365 dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
366 DMA_TO_DEVICE);
367 }
368}
369
370/**
371 * free_tx_desc - reclaims Tx descriptors and their buffers
372 * @adapter: the adapter
373 * @q: the Tx queue to reclaim descriptors from
374 * @n: the number of descriptors to reclaim
375 * @unmap: whether the buffers should be unmapped for DMA
376 *
377 * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
378 * Tx buffers. Called with the Tx queue lock held.
379 */
380static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
381 unsigned int n, bool unmap)
382{
383 struct tx_sw_desc *d;
384 unsigned int cidx = q->cidx;
385 struct device *dev = adap->pdev_dev;
386
387 d = &q->sdesc[cidx];
388 while (n--) {
389 if (d->skb) { /* an SGL is present */
390 if (unmap)
391 unmap_sgl(dev, d->skb, d->sgl, q);
Eric W. Biedermana7525192014-03-15 16:29:49 -0700392 dev_consume_skb_any(d->skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000393 d->skb = NULL;
394 }
395 ++d;
396 if (++cidx == q->size) {
397 cidx = 0;
398 d = q->sdesc;
399 }
400 }
401 q->cidx = cidx;
402}
403
404/*
405 * Return the number of reclaimable descriptors in a Tx queue.
406 */
407static inline int reclaimable(const struct sge_txq *q)
408{
409 int hw_cidx = ntohs(q->stat->cidx);
410 hw_cidx -= q->cidx;
411 return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
412}
413
414/**
415 * reclaim_completed_tx - reclaims completed Tx descriptors
416 * @adap: the adapter
417 * @q: the Tx queue to reclaim completed descriptors from
418 * @unmap: whether the buffers should be unmapped for DMA
419 *
420 * Reclaims Tx descriptors that the SGE has indicated it has processed,
421 * and frees the associated buffers if possible. Called with the Tx
422 * queue locked.
423 */
424static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
425 bool unmap)
426{
427 int avail = reclaimable(q);
428
429 if (avail) {
430 /*
431 * Limit the amount of clean up work we do at a time to keep
432 * the Tx lock hold time O(1).
433 */
434 if (avail > MAX_TX_RECLAIM)
435 avail = MAX_TX_RECLAIM;
436
437 free_tx_desc(adap, q, avail, unmap);
438 q->in_use -= avail;
439 }
440}
441
Vipul Pandya52367a72012-09-26 02:39:38 +0000442static inline int get_buf_size(struct adapter *adapter,
443 const struct rx_sw_desc *d)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000444{
Vipul Pandya52367a72012-09-26 02:39:38 +0000445 struct sge *s = &adapter->sge;
446 unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
447 int buf_size;
448
449 switch (rx_buf_size_idx) {
450 case RX_SMALL_PG_BUF:
451 buf_size = PAGE_SIZE;
452 break;
453
454 case RX_LARGE_PG_BUF:
455 buf_size = PAGE_SIZE << s->fl_pg_order;
456 break;
457
458 case RX_SMALL_MTU_BUF:
459 buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
460 break;
461
462 case RX_LARGE_MTU_BUF:
463 buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
464 break;
465
466 default:
467 BUG_ON(1);
468 }
469
470 return buf_size;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000471}
472
473/**
474 * free_rx_bufs - free the Rx buffers on an SGE free list
475 * @adap: the adapter
476 * @q: the SGE free list to free buffers from
477 * @n: how many buffers to free
478 *
479 * Release the next @n buffers on an SGE free-buffer Rx queue. The
480 * buffers must be made inaccessible to HW before calling this function.
481 */
482static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
483{
484 while (n--) {
485 struct rx_sw_desc *d = &q->sdesc[q->cidx];
486
487 if (is_buf_mapped(d))
488 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
Vipul Pandya52367a72012-09-26 02:39:38 +0000489 get_buf_size(adap, d),
490 PCI_DMA_FROMDEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000491 put_page(d->page);
492 d->page = NULL;
493 if (++q->cidx == q->size)
494 q->cidx = 0;
495 q->avail--;
496 }
497}
498
499/**
500 * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
501 * @adap: the adapter
502 * @q: the SGE free list
503 *
504 * Unmap the current buffer on an SGE free-buffer Rx queue. The
505 * buffer must be made inaccessible to HW before calling this function.
506 *
507 * This is similar to @free_rx_bufs above but does not free the buffer.
508 * Do note that the FL still loses any further access to the buffer.
509 */
510static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
511{
512 struct rx_sw_desc *d = &q->sdesc[q->cidx];
513
514 if (is_buf_mapped(d))
515 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
Vipul Pandya52367a72012-09-26 02:39:38 +0000516 get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000517 d->page = NULL;
518 if (++q->cidx == q->size)
519 q->cidx = 0;
520 q->avail--;
521}
522
523static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
524{
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000525 u32 val;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000526 if (q->pend_cred >= 8) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530527 if (is_t4(adap->params.chip))
528 val = PIDX_V(q->pend_cred / 8);
529 else
530 val = PIDX_T5_V(q->pend_cred / 8) |
531 DBTYPE_F;
532 val |= DBPRIO_F;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000533 wmb();
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530534
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530535 /* If we don't have access to the new User Doorbell (T5+), use
536 * the old doorbell mechanism; otherwise use the new BAR2
537 * mechanism.
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530538 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530539 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530540 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
541 val | QID_V(q->cntxt_id));
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530542 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530543 writel(val | QID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530544 q->bar2_addr + SGE_UDB_KDOORBELL);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530545
546 /* This Write memory Barrier will force the write to
547 * the User Doorbell area to be flushed.
548 */
549 wmb();
550 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000551 q->pend_cred &= 7;
552 }
553}
554
555static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
556 dma_addr_t mapping)
557{
558 sd->page = pg;
559 sd->dma_addr = mapping; /* includes size low bits */
560}
561
562/**
563 * refill_fl - refill an SGE Rx buffer ring
564 * @adap: the adapter
565 * @q: the ring to refill
566 * @n: the number of new buffers to allocate
567 * @gfp: the gfp flags for the allocations
568 *
569 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
570 * allocated with the supplied gfp flags. The caller must assure that
571 * @n does not exceed the queue's capacity. If afterwards the queue is
572 * found critically low mark it as starving in the bitmap of starving FLs.
573 *
574 * Returns the number of buffers allocated.
575 */
576static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
577 gfp_t gfp)
578{
Vipul Pandya52367a72012-09-26 02:39:38 +0000579 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000580 struct page *pg;
581 dma_addr_t mapping;
582 unsigned int cred = q->avail;
583 __be64 *d = &q->desc[q->pidx];
584 struct rx_sw_desc *sd = &q->sdesc[q->pidx];
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530585 int node;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000586
Alexander Duyckaa9cd312014-11-11 09:26:42 -0800587 gfp |= __GFP_NOWARN;
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530588 node = dev_to_node(adap->pdev_dev);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000589
Vipul Pandya52367a72012-09-26 02:39:38 +0000590 if (s->fl_pg_order == 0)
591 goto alloc_small_pages;
592
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000593 /*
594 * Prefer large buffers
595 */
596 while (n) {
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530597 pg = alloc_pages_node(node, gfp | __GFP_COMP, s->fl_pg_order);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000598 if (unlikely(!pg)) {
599 q->large_alloc_failed++;
600 break; /* fall back to single pages */
601 }
602
603 mapping = dma_map_page(adap->pdev_dev, pg, 0,
Vipul Pandya52367a72012-09-26 02:39:38 +0000604 PAGE_SIZE << s->fl_pg_order,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000605 PCI_DMA_FROMDEVICE);
606 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
Vipul Pandya52367a72012-09-26 02:39:38 +0000607 __free_pages(pg, s->fl_pg_order);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000608 goto out; /* do not try small pages for this error */
609 }
Vipul Pandya52367a72012-09-26 02:39:38 +0000610 mapping |= RX_LARGE_PG_BUF;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000611 *d++ = cpu_to_be64(mapping);
612
613 set_rx_sw_desc(sd, pg, mapping);
614 sd++;
615
616 q->avail++;
617 if (++q->pidx == q->size) {
618 q->pidx = 0;
619 sd = q->sdesc;
620 d = q->desc;
621 }
622 n--;
623 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000624
Vipul Pandya52367a72012-09-26 02:39:38 +0000625alloc_small_pages:
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000626 while (n--) {
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530627 pg = alloc_pages_node(node, gfp, 0);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000628 if (unlikely(!pg)) {
629 q->alloc_failed++;
630 break;
631 }
632
633 mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
634 PCI_DMA_FROMDEVICE);
635 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000636 put_page(pg);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000637 goto out;
638 }
639 *d++ = cpu_to_be64(mapping);
640
641 set_rx_sw_desc(sd, pg, mapping);
642 sd++;
643
644 q->avail++;
645 if (++q->pidx == q->size) {
646 q->pidx = 0;
647 sd = q->sdesc;
648 d = q->desc;
649 }
650 }
651
652out: cred = q->avail - cred;
653 q->pend_cred += cred;
654 ring_fl_db(adap, q);
655
Hariprasad Shenaic098b022015-04-15 02:02:31 +0530656 if (unlikely(fl_starving(adap, q))) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000657 smp_wmb();
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000658 set_bit(q->cntxt_id - adap->sge.egr_start,
659 adap->sge.starving_fl);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000660 }
661
662 return cred;
663}
664
665static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
666{
667 refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
668 GFP_ATOMIC);
669}
670
671/**
672 * alloc_ring - allocate resources for an SGE descriptor ring
673 * @dev: the PCI device's core device
674 * @nelem: the number of descriptors
675 * @elem_size: the size of each descriptor
676 * @sw_size: the size of the SW state associated with each ring element
677 * @phys: the physical address of the allocated ring
678 * @metadata: address of the array holding the SW state for the ring
679 * @stat_size: extra space in HW ring for status information
Dimitris Michailidisad6bad32010-12-14 21:36:55 +0000680 * @node: preferred node for memory allocations
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000681 *
682 * Allocates resources for an SGE descriptor ring, such as Tx queues,
683 * free buffer lists, or response queues. Each SGE ring requires
684 * space for its HW descriptors plus, optionally, space for the SW state
685 * associated with each HW entry (the metadata). The function returns
686 * three values: the virtual address for the HW ring (the return value
687 * of the function), the bus address of the HW ring, and the address
688 * of the SW ring.
689 */
690static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
691 size_t sw_size, dma_addr_t *phys, void *metadata,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +0000692 size_t stat_size, int node)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000693{
694 size_t len = nelem * elem_size + stat_size;
695 void *s = NULL;
696 void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
697
698 if (!p)
699 return NULL;
700 if (sw_size) {
Dimitris Michailidisad6bad32010-12-14 21:36:55 +0000701 s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000702
703 if (!s) {
704 dma_free_coherent(dev, len, p, *phys);
705 return NULL;
706 }
707 }
708 if (metadata)
709 *(void **)metadata = s;
710 memset(p, 0, len);
711 return p;
712}
713
714/**
715 * sgl_len - calculates the size of an SGL of the given capacity
716 * @n: the number of SGL entries
717 *
718 * Calculates the number of flits needed for a scatter/gather list that
719 * can hold the given number of entries.
720 */
721static inline unsigned int sgl_len(unsigned int n)
722{
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530723 /* A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
724 * addresses. The DSGL Work Request starts off with a 32-bit DSGL
725 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
726 * repeated sequences of { Length[i], Length[i+1], Address[i],
727 * Address[i+1] } (this ensures that all addresses are on 64-bit
728 * boundaries). If N is even, then Length[N+1] should be set to 0 and
729 * Address[N+1] is omitted.
730 *
731 * The following calculation incorporates all of the above. It's
732 * somewhat hard to follow but, briefly: the "+2" accounts for the
733 * first two flits which include the DSGL header, Length0 and
734 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
735 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
736 * finally the "+((n-1)&1)" adds the one remaining flit needed if
737 * (n-1) is odd ...
738 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000739 n--;
740 return (3 * n) / 2 + (n & 1) + 2;
741}
742
743/**
744 * flits_to_desc - returns the num of Tx descriptors for the given flits
745 * @n: the number of flits
746 *
747 * Returns the number of Tx descriptors needed for the supplied number
748 * of flits.
749 */
750static inline unsigned int flits_to_desc(unsigned int n)
751{
752 BUG_ON(n > SGE_MAX_WR_LEN / 8);
753 return DIV_ROUND_UP(n, 8);
754}
755
756/**
757 * is_eth_imm - can an Ethernet packet be sent as immediate data?
758 * @skb: the packet
759 *
760 * Returns whether an Ethernet packet is small enough to fit as
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530761 * immediate data. Return value corresponds to headroom required.
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000762 */
763static inline int is_eth_imm(const struct sk_buff *skb)
764{
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530765 int hdrlen = skb_shinfo(skb)->gso_size ?
766 sizeof(struct cpl_tx_pkt_lso_core) : 0;
767
768 hdrlen += sizeof(struct cpl_tx_pkt);
769 if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
770 return hdrlen;
771 return 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000772}
773
774/**
775 * calc_tx_flits - calculate the number of flits for a packet Tx WR
776 * @skb: the packet
777 *
778 * Returns the number of flits needed for a Tx WR for the given Ethernet
779 * packet, including the needed WR and CPL headers.
780 */
781static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
782{
783 unsigned int flits;
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530784 int hdrlen = is_eth_imm(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000785
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530786 /* If the skb is small enough, we can pump it out as a work request
787 * with only immediate data. In that case we just have to have the
788 * TX Packet header plus the skb data in the Work Request.
789 */
790
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530791 if (hdrlen)
792 return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000793
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530794 /* Otherwise, we're going to have to construct a Scatter gather list
795 * of the skb body and fragments. We also include the flits necessary
796 * for the TX Packet Work Request and CPL. We always have a firmware
797 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
798 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
799 * message or, if we're doing a Large Send Offload, an LSO CPL message
800 * with an embedded TX Packet Write CPL message.
801 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000802 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
803 if (skb_shinfo(skb)->gso_size)
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530804 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
805 sizeof(struct cpl_tx_pkt_lso_core) +
806 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
807 else
808 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
809 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000810 return flits;
811}
812
813/**
814 * calc_tx_descs - calculate the number of Tx descriptors for a packet
815 * @skb: the packet
816 *
817 * Returns the number of Tx descriptors needed for the given Ethernet
818 * packet, including the needed WR and CPL headers.
819 */
820static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
821{
822 return flits_to_desc(calc_tx_flits(skb));
823}
824
825/**
826 * write_sgl - populate a scatter/gather list for a packet
827 * @skb: the packet
828 * @q: the Tx queue we are writing into
829 * @sgl: starting location for writing the SGL
830 * @end: points right after the end of the SGL
831 * @start: start offset into skb main-body data to include in the SGL
832 * @addr: the list of bus addresses for the SGL elements
833 *
834 * Generates a gather list for the buffers that make up a packet.
835 * The caller must provide adequate space for the SGL that will be written.
836 * The SGL includes all of the packet's page fragments and the data in its
837 * main body except for the first @start bytes. @sgl must be 16-byte
838 * aligned and within a Tx descriptor with available space. @end points
839 * right after the end of the SGL but does not account for any potential
840 * wrap around, i.e., @end > @sgl.
841 */
842static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
843 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
844 const dma_addr_t *addr)
845{
846 unsigned int i, len;
847 struct ulptx_sge_pair *to;
848 const struct skb_shared_info *si = skb_shinfo(skb);
849 unsigned int nfrags = si->nr_frags;
850 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
851
852 len = skb_headlen(skb) - start;
853 if (likely(len)) {
854 sgl->len0 = htonl(len);
855 sgl->addr0 = cpu_to_be64(addr[0] + start);
856 nfrags++;
857 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000858 sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000859 sgl->addr0 = cpu_to_be64(addr[1]);
860 }
861
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800862 sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
863 ULPTX_NSGE_V(nfrags));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000864 if (likely(--nfrags == 0))
865 return;
866 /*
867 * Most of the complexity below deals with the possibility we hit the
868 * end of the queue in the middle of writing the SGL. For this case
869 * only we create the SGL in a temporary buffer and then copy it.
870 */
871 to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
872
873 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000874 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
875 to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000876 to->addr[0] = cpu_to_be64(addr[i]);
877 to->addr[1] = cpu_to_be64(addr[++i]);
878 }
879 if (nfrags) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000880 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000881 to->len[1] = cpu_to_be32(0);
882 to->addr[0] = cpu_to_be64(addr[i + 1]);
883 }
884 if (unlikely((u8 *)end > (u8 *)q->stat)) {
885 unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
886
887 if (likely(part0))
888 memcpy(sgl->sge, buf, part0);
889 part1 = (u8 *)end - (u8 *)q->stat;
890 memcpy(q->desc, (u8 *)buf + part0, part1);
891 end = (void *)q->desc + part1;
892 }
893 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
Joe Perches64699332012-06-04 12:44:16 +0000894 *end = 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000895}
896
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530897/* This function copies 64 byte coalesced work request to
898 * memory mapped BAR2 space. For coalesced WR SGE fetches
899 * data from the FIFO instead of from Host.
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000900 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530901static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000902{
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530903 int count = 8;
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000904
905 while (count) {
906 writeq(*src, dst);
907 src++;
908 dst++;
909 count--;
910 }
911}
912
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000913/**
914 * ring_tx_db - check and potentially ring a Tx queue's doorbell
915 * @adap: the adapter
916 * @q: the Tx queue
917 * @n: number of new descriptors to give to HW
918 *
919 * Ring the doorbel for a Tx queue.
920 */
921static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
922{
923 wmb(); /* write descriptors before telling HW */
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530924
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530925 /* If we don't have access to the new User Doorbell (T5+), use the old
926 * doorbell mechanism; otherwise use the new BAR2 mechanism.
927 */
928 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530929 u32 val = PIDX_V(n);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530930 unsigned long flags;
931
932 /* For T4 we need to participate in the Doorbell Recovery
933 * mechanism.
934 */
935 spin_lock_irqsave(&q->db_lock, flags);
936 if (!q->db_disabled)
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530937 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
938 QID_V(q->cntxt_id) | val);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530939 else
940 q->db_pidx_inc += n;
941 q->db_pidx = q->pidx;
942 spin_unlock_irqrestore(&q->db_lock, flags);
943 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530944 u32 val = PIDX_T5_V(n);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530945
946 /* T4 and later chips share the same PIDX field offset within
947 * the doorbell, but T5 and later shrank the field in order to
948 * gain a bit for Doorbell Priority. The field was absurdly
949 * large in the first place (14 bits) so we just use the T5
950 * and later limits and warn if a Queue ID is too large.
951 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530952 WARN_ON(val & DBPRIO_F);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530953
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530954 /* If we're only writing a single TX Descriptor and we can use
955 * Inferred QID registers, we can use the Write Combining
956 * Gather Buffer; otherwise we use the simple doorbell.
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530957 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530958 if (n == 1 && q->bar2_qid == 0) {
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530959 int index = (q->pidx
960 ? (q->pidx - 1)
961 : (q->size - 1));
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530962 u64 *wr = (u64 *)&q->desc[index];
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530963
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530964 cxgb_pio_copy((u64 __iomem *)
965 (q->bar2_addr + SGE_UDB_WCDOORBELL),
966 wr);
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000967 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530968 writel(val | QID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530969 q->bar2_addr + SGE_UDB_KDOORBELL);
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000970 }
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530971
972 /* This Write Memory Barrier will force the write to the User
973 * Doorbell area to be flushed. This is needed to prevent
974 * writes on different CPUs for the same queue from hitting
975 * the adapter out of order. This is required when some Work
976 * Requests take the Write Combine Gather Buffer path (user
977 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
978 * take the traditional path where we simply increment the
979 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
980 * hardware DMA read the actual Work Request.
981 */
982 wmb();
983 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000984}
985
986/**
987 * inline_tx_skb - inline a packet's data into Tx descriptors
988 * @skb: the packet
989 * @q: the Tx queue where the packet will be inlined
990 * @pos: starting position in the Tx queue where to inline the packet
991 *
992 * Inline a packet's contents directly into Tx descriptors, starting at
993 * the given position within the Tx DMA ring.
994 * Most of the complexity of this operation is dealing with wrap arounds
995 * in the middle of the packet we want to inline.
996 */
997static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
998 void *pos)
999{
1000 u64 *p;
1001 int left = (void *)q->stat - pos;
1002
1003 if (likely(skb->len <= left)) {
1004 if (likely(!skb->data_len))
1005 skb_copy_from_linear_data(skb, pos, skb->len);
1006 else
1007 skb_copy_bits(skb, 0, pos, skb->len);
1008 pos += skb->len;
1009 } else {
1010 skb_copy_bits(skb, 0, pos, left);
1011 skb_copy_bits(skb, left, q->desc, skb->len - left);
1012 pos = (void *)q->desc + (skb->len - left);
1013 }
1014
1015 /* 0-pad to multiple of 16 */
1016 p = PTR_ALIGN(pos, 8);
1017 if ((uintptr_t)p & 8)
1018 *p = 0;
1019}
1020
1021/*
1022 * Figure out what HW csum a packet wants and return the appropriate control
1023 * bits.
1024 */
1025static u64 hwcsum(const struct sk_buff *skb)
1026{
1027 int csum_type;
1028 const struct iphdr *iph = ip_hdr(skb);
1029
1030 if (iph->version == 4) {
1031 if (iph->protocol == IPPROTO_TCP)
1032 csum_type = TX_CSUM_TCPIP;
1033 else if (iph->protocol == IPPROTO_UDP)
1034 csum_type = TX_CSUM_UDPIP;
1035 else {
1036nocsum: /*
1037 * unknown protocol, disable HW csum
1038 * and hope a bad packet is detected
1039 */
1040 return TXPKT_L4CSUM_DIS;
1041 }
1042 } else {
1043 /*
1044 * this doesn't work with extension headers
1045 */
1046 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
1047
1048 if (ip6h->nexthdr == IPPROTO_TCP)
1049 csum_type = TX_CSUM_TCPIP6;
1050 else if (ip6h->nexthdr == IPPROTO_UDP)
1051 csum_type = TX_CSUM_UDPIP6;
1052 else
1053 goto nocsum;
1054 }
1055
1056 if (likely(csum_type >= TX_CSUM_TCPIP))
1057 return TXPKT_CSUM_TYPE(csum_type) |
1058 TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
1059 TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
1060 else {
1061 int start = skb_transport_offset(skb);
1062
1063 return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
1064 TXPKT_CSUM_LOC(start + skb->csum_offset);
1065 }
1066}
1067
1068static void eth_txq_stop(struct sge_eth_txq *q)
1069{
1070 netif_tx_stop_queue(q->txq);
1071 q->q.stops++;
1072}
1073
1074static inline void txq_advance(struct sge_txq *q, unsigned int n)
1075{
1076 q->in_use += n;
1077 q->pidx += n;
1078 if (q->pidx >= q->size)
1079 q->pidx -= q->size;
1080}
1081
Varun Prakash84a200b2015-03-24 19:14:46 +05301082#ifdef CONFIG_CHELSIO_T4_FCOE
1083static inline int
1084cxgb_fcoe_offload(struct sk_buff *skb, struct adapter *adap,
1085 const struct port_info *pi, u64 *cntrl)
1086{
1087 const struct cxgb_fcoe *fcoe = &pi->fcoe;
1088
1089 if (!(fcoe->flags & CXGB_FCOE_ENABLED))
1090 return 0;
1091
1092 if (skb->protocol != htons(ETH_P_FCOE))
1093 return 0;
1094
1095 skb_reset_mac_header(skb);
1096 skb->mac_len = sizeof(struct ethhdr);
1097
1098 skb_set_network_header(skb, skb->mac_len);
1099 skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
1100
1101 if (!cxgb_fcoe_sof_eof_supported(adap, skb))
1102 return -ENOTSUPP;
1103
1104 /* FC CRC offload */
1105 *cntrl = TXPKT_CSUM_TYPE(TX_CSUM_FCOE) |
1106 TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS |
1107 TXPKT_CSUM_START(CXGB_FCOE_TXPKT_CSUM_START) |
1108 TXPKT_CSUM_END(CXGB_FCOE_TXPKT_CSUM_END) |
1109 TXPKT_CSUM_LOC(CXGB_FCOE_TXPKT_CSUM_END);
1110 return 0;
1111}
1112#endif /* CONFIG_CHELSIO_T4_FCOE */
1113
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001114/**
1115 * t4_eth_xmit - add a packet to an Ethernet Tx queue
1116 * @skb: the packet
1117 * @dev: the egress net device
1118 *
1119 * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
1120 */
1121netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1122{
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301123 int len;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001124 u32 wr_mid;
1125 u64 cntrl, *end;
1126 int qidx, credits;
1127 unsigned int flits, ndesc;
1128 struct adapter *adap;
1129 struct sge_eth_txq *q;
1130 const struct port_info *pi;
1131 struct fw_eth_tx_pkt_wr *wr;
1132 struct cpl_tx_pkt_core *cpl;
1133 const struct skb_shared_info *ssi;
1134 dma_addr_t addr[MAX_SKB_FRAGS + 1];
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301135 bool immediate = false;
Varun Prakash84a200b2015-03-24 19:14:46 +05301136#ifdef CONFIG_CHELSIO_T4_FCOE
1137 int err;
1138#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001139
1140 /*
1141 * The chip min packet length is 10 octets but play safe and reject
1142 * anything shorter than an Ethernet header.
1143 */
1144 if (unlikely(skb->len < ETH_HLEN)) {
Eric W. Biedermana7525192014-03-15 16:29:49 -07001145out_free: dev_kfree_skb_any(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001146 return NETDEV_TX_OK;
1147 }
1148
1149 pi = netdev_priv(dev);
1150 adap = pi->adapter;
1151 qidx = skb_get_queue_mapping(skb);
1152 q = &adap->sge.ethtxq[qidx + pi->first_qset];
1153
1154 reclaim_completed_tx(adap, &q->q, true);
Varun Prakash84a200b2015-03-24 19:14:46 +05301155 cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
1156
1157#ifdef CONFIG_CHELSIO_T4_FCOE
1158 err = cxgb_fcoe_offload(skb, adap, pi, &cntrl);
1159 if (unlikely(err == -ENOTSUPP))
1160 goto out_free;
1161#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001162
1163 flits = calc_tx_flits(skb);
1164 ndesc = flits_to_desc(flits);
1165 credits = txq_avail(&q->q) - ndesc;
1166
1167 if (unlikely(credits < 0)) {
1168 eth_txq_stop(q);
1169 dev_err(adap->pdev_dev,
1170 "%s: Tx ring %u full while queue awake!\n",
1171 dev->name, qidx);
1172 return NETDEV_TX_BUSY;
1173 }
1174
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301175 if (is_eth_imm(skb))
1176 immediate = true;
1177
1178 if (!immediate &&
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001179 unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
1180 q->mapping_err++;
1181 goto out_free;
1182 }
1183
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301184 wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001185 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1186 eth_txq_stop(q);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301187 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001188 }
1189
1190 wr = (void *)&q->q.desc[q->q.pidx];
1191 wr->equiq_to_len16 = htonl(wr_mid);
1192 wr->r3 = cpu_to_be64(0);
1193 end = (u64 *)wr + flits;
1194
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301195 len = immediate ? skb->len : 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001196 ssi = skb_shinfo(skb);
1197 if (ssi->gso_size) {
Dimitris Michailidis625ac6a2010-08-02 13:19:18 +00001198 struct cpl_tx_pkt_lso *lso = (void *)wr;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001199 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1200 int l3hdr_len = skb_network_header_len(skb);
1201 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1202
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301203 len += sizeof(*lso);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301204 wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
1205 FW_WR_IMMDLEN_V(len));
Dimitris Michailidis625ac6a2010-08-02 13:19:18 +00001206 lso->c.lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
1207 LSO_FIRST_SLICE | LSO_LAST_SLICE |
1208 LSO_IPV6(v6) |
1209 LSO_ETHHDR_LEN(eth_xtra_len / 4) |
1210 LSO_IPHDR_LEN(l3hdr_len / 4) |
1211 LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
1212 lso->c.ipid_ofst = htons(0);
1213 lso->c.mss = htons(ssi->gso_size);
1214 lso->c.seqno_offset = htonl(0);
Hariprasad Shenai7207c0d2014-10-09 05:48:45 +05301215 if (is_t4(adap->params.chip))
1216 lso->c.len = htonl(skb->len);
1217 else
1218 lso->c.len = htonl(LSO_T5_XFER_SIZE(skb->len));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001219 cpl = (void *)(lso + 1);
1220 cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
1221 TXPKT_IPHDR_LEN(l3hdr_len) |
1222 TXPKT_ETHHDR_LEN(eth_xtra_len);
1223 q->tso++;
1224 q->tx_cso += ssi->gso_segs;
1225 } else {
Kumar Sanghvica71de62014-03-13 20:50:50 +05301226 len += sizeof(*cpl);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301227 wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
1228 FW_WR_IMMDLEN_V(len));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001229 cpl = (void *)(wr + 1);
1230 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1231 cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
1232 q->tx_cso++;
Varun Prakash84a200b2015-03-24 19:14:46 +05301233 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001234 }
1235
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001236 if (skb_vlan_tag_present(skb)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001237 q->vlan_ins++;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001238 cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(skb_vlan_tag_get(skb));
Varun Prakash84a200b2015-03-24 19:14:46 +05301239#ifdef CONFIG_CHELSIO_T4_FCOE
1240 if (skb->protocol == htons(ETH_P_FCOE))
1241 cntrl |= TXPKT_VLAN(
1242 ((skb->priority & 0x7) << VLAN_PRIO_SHIFT));
1243#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001244 }
1245
1246 cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
Dimitris Michailidis1707aec2010-08-23 17:21:00 +00001247 TXPKT_INTF(pi->tx_chan) | TXPKT_PF(adap->fn));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001248 cpl->pack = htons(0);
1249 cpl->len = htons(skb->len);
1250 cpl->ctrl1 = cpu_to_be64(cntrl);
1251
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301252 if (immediate) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001253 inline_tx_skb(skb, &q->q, cpl + 1);
Eric W. Biedermana7525192014-03-15 16:29:49 -07001254 dev_consume_skb_any(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001255 } else {
1256 int last_desc;
1257
1258 write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
1259 addr);
1260 skb_orphan(skb);
1261
1262 last_desc = q->q.pidx + ndesc - 1;
1263 if (last_desc >= q->q.size)
1264 last_desc -= q->q.size;
1265 q->q.sdesc[last_desc].skb = skb;
1266 q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
1267 }
1268
1269 txq_advance(&q->q, ndesc);
1270
1271 ring_tx_db(adap, &q->q, ndesc);
1272 return NETDEV_TX_OK;
1273}
1274
1275/**
1276 * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
1277 * @q: the SGE control Tx queue
1278 *
1279 * This is a variant of reclaim_completed_tx() that is used for Tx queues
1280 * that send only immediate data (presently just the control queues) and
1281 * thus do not have any sk_buffs to release.
1282 */
1283static inline void reclaim_completed_tx_imm(struct sge_txq *q)
1284{
1285 int hw_cidx = ntohs(q->stat->cidx);
1286 int reclaim = hw_cidx - q->cidx;
1287
1288 if (reclaim < 0)
1289 reclaim += q->size;
1290
1291 q->in_use -= reclaim;
1292 q->cidx = hw_cidx;
1293}
1294
1295/**
1296 * is_imm - check whether a packet can be sent as immediate data
1297 * @skb: the packet
1298 *
1299 * Returns true if a packet can be sent as a WR with immediate data.
1300 */
1301static inline int is_imm(const struct sk_buff *skb)
1302{
1303 return skb->len <= MAX_CTRL_WR_LEN;
1304}
1305
1306/**
1307 * ctrlq_check_stop - check if a control queue is full and should stop
1308 * @q: the queue
1309 * @wr: most recent WR written to the queue
1310 *
1311 * Check if a control queue has become full and should be stopped.
1312 * We clean up control queue descriptors very lazily, only when we are out.
1313 * If the queue is still full after reclaiming any completed descriptors
1314 * we suspend it and have the last WR wake it up.
1315 */
1316static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
1317{
1318 reclaim_completed_tx_imm(&q->q);
1319 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301320 wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001321 q->q.stops++;
1322 q->full = 1;
1323 }
1324}
1325
1326/**
1327 * ctrl_xmit - send a packet through an SGE control Tx queue
1328 * @q: the control queue
1329 * @skb: the packet
1330 *
1331 * Send a packet through an SGE control Tx queue. Packets sent through
1332 * a control queue must fit entirely as immediate data.
1333 */
1334static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
1335{
1336 unsigned int ndesc;
1337 struct fw_wr_hdr *wr;
1338
1339 if (unlikely(!is_imm(skb))) {
1340 WARN_ON(1);
1341 dev_kfree_skb(skb);
1342 return NET_XMIT_DROP;
1343 }
1344
1345 ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
1346 spin_lock(&q->sendq.lock);
1347
1348 if (unlikely(q->full)) {
1349 skb->priority = ndesc; /* save for restart */
1350 __skb_queue_tail(&q->sendq, skb);
1351 spin_unlock(&q->sendq.lock);
1352 return NET_XMIT_CN;
1353 }
1354
1355 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1356 inline_tx_skb(skb, &q->q, wr);
1357
1358 txq_advance(&q->q, ndesc);
1359 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
1360 ctrlq_check_stop(q, wr);
1361
1362 ring_tx_db(q->adap, &q->q, ndesc);
1363 spin_unlock(&q->sendq.lock);
1364
1365 kfree_skb(skb);
1366 return NET_XMIT_SUCCESS;
1367}
1368
1369/**
1370 * restart_ctrlq - restart a suspended control queue
1371 * @data: the control queue to restart
1372 *
1373 * Resumes transmission on a suspended Tx control queue.
1374 */
1375static void restart_ctrlq(unsigned long data)
1376{
1377 struct sk_buff *skb;
1378 unsigned int written = 0;
1379 struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
1380
1381 spin_lock(&q->sendq.lock);
1382 reclaim_completed_tx_imm(&q->q);
1383 BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
1384
1385 while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
1386 struct fw_wr_hdr *wr;
1387 unsigned int ndesc = skb->priority; /* previously saved */
1388
1389 /*
1390 * Write descriptors and free skbs outside the lock to limit
1391 * wait times. q->full is still set so new skbs will be queued.
1392 */
1393 spin_unlock(&q->sendq.lock);
1394
1395 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1396 inline_tx_skb(skb, &q->q, wr);
1397 kfree_skb(skb);
1398
1399 written += ndesc;
1400 txq_advance(&q->q, ndesc);
1401 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1402 unsigned long old = q->q.stops;
1403
1404 ctrlq_check_stop(q, wr);
1405 if (q->q.stops != old) { /* suspended anew */
1406 spin_lock(&q->sendq.lock);
1407 goto ringdb;
1408 }
1409 }
1410 if (written > 16) {
1411 ring_tx_db(q->adap, &q->q, written);
1412 written = 0;
1413 }
1414 spin_lock(&q->sendq.lock);
1415 }
1416 q->full = 0;
1417ringdb: if (written)
1418 ring_tx_db(q->adap, &q->q, written);
1419 spin_unlock(&q->sendq.lock);
1420}
1421
1422/**
1423 * t4_mgmt_tx - send a management message
1424 * @adap: the adapter
1425 * @skb: the packet containing the management message
1426 *
1427 * Send a management message through control queue 0.
1428 */
1429int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
1430{
1431 int ret;
1432
1433 local_bh_disable();
1434 ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
1435 local_bh_enable();
1436 return ret;
1437}
1438
1439/**
1440 * is_ofld_imm - check whether a packet can be sent as immediate data
1441 * @skb: the packet
1442 *
1443 * Returns true if a packet can be sent as an offload WR with immediate
1444 * data. We currently use the same limit as for Ethernet packets.
1445 */
1446static inline int is_ofld_imm(const struct sk_buff *skb)
1447{
1448 return skb->len <= MAX_IMM_TX_PKT_LEN;
1449}
1450
1451/**
1452 * calc_tx_flits_ofld - calculate # of flits for an offload packet
1453 * @skb: the packet
1454 *
1455 * Returns the number of flits needed for the given offload packet.
1456 * These packets are already fully constructed and no additional headers
1457 * will be added.
1458 */
1459static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
1460{
1461 unsigned int flits, cnt;
1462
1463 if (is_ofld_imm(skb))
1464 return DIV_ROUND_UP(skb->len, 8);
1465
1466 flits = skb_transport_offset(skb) / 8U; /* headers */
1467 cnt = skb_shinfo(skb)->nr_frags;
Li RongQing15dd16c2013-06-03 22:11:16 +00001468 if (skb_tail_pointer(skb) != skb_transport_header(skb))
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001469 cnt++;
1470 return flits + sgl_len(cnt);
1471}
1472
1473/**
1474 * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
1475 * @adap: the adapter
1476 * @q: the queue to stop
1477 *
1478 * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
1479 * inability to map packets. A periodic timer attempts to restart
1480 * queues so marked.
1481 */
1482static void txq_stop_maperr(struct sge_ofld_txq *q)
1483{
1484 q->mapping_err++;
1485 q->q.stops++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001486 set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
1487 q->adap->sge.txq_maperr);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001488}
1489
1490/**
1491 * ofldtxq_stop - stop an offload Tx queue that has become full
1492 * @q: the queue to stop
1493 * @skb: the packet causing the queue to become full
1494 *
1495 * Stops an offload Tx queue that has become full and modifies the packet
1496 * being written to request a wakeup.
1497 */
1498static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
1499{
1500 struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
1501
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301502 wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001503 q->q.stops++;
1504 q->full = 1;
1505}
1506
1507/**
1508 * service_ofldq - restart a suspended offload queue
1509 * @q: the offload queue
1510 *
1511 * Services an offload Tx queue by moving packets from its packet queue
1512 * to the HW Tx ring. The function starts and ends with the queue locked.
1513 */
1514static void service_ofldq(struct sge_ofld_txq *q)
1515{
1516 u64 *pos;
1517 int credits;
1518 struct sk_buff *skb;
1519 unsigned int written = 0;
1520 unsigned int flits, ndesc;
1521
1522 while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
1523 /*
1524 * We drop the lock but leave skb on sendq, thus retaining
1525 * exclusive access to the state of the queue.
1526 */
1527 spin_unlock(&q->sendq.lock);
1528
1529 reclaim_completed_tx(q->adap, &q->q, false);
1530
1531 flits = skb->priority; /* previously saved */
1532 ndesc = flits_to_desc(flits);
1533 credits = txq_avail(&q->q) - ndesc;
1534 BUG_ON(credits < 0);
1535 if (unlikely(credits < TXQ_STOP_THRES))
1536 ofldtxq_stop(q, skb);
1537
1538 pos = (u64 *)&q->q.desc[q->q.pidx];
1539 if (is_ofld_imm(skb))
1540 inline_tx_skb(skb, &q->q, pos);
1541 else if (map_skb(q->adap->pdev_dev, skb,
1542 (dma_addr_t *)skb->head)) {
1543 txq_stop_maperr(q);
1544 spin_lock(&q->sendq.lock);
1545 break;
1546 } else {
1547 int last_desc, hdr_len = skb_transport_offset(skb);
1548
1549 memcpy(pos, skb->data, hdr_len);
1550 write_sgl(skb, &q->q, (void *)pos + hdr_len,
1551 pos + flits, hdr_len,
1552 (dma_addr_t *)skb->head);
1553#ifdef CONFIG_NEED_DMA_MAP_STATE
1554 skb->dev = q->adap->port[0];
1555 skb->destructor = deferred_unmap_destructor;
1556#endif
1557 last_desc = q->q.pidx + ndesc - 1;
1558 if (last_desc >= q->q.size)
1559 last_desc -= q->q.size;
1560 q->q.sdesc[last_desc].skb = skb;
1561 }
1562
1563 txq_advance(&q->q, ndesc);
1564 written += ndesc;
1565 if (unlikely(written > 32)) {
1566 ring_tx_db(q->adap, &q->q, written);
1567 written = 0;
1568 }
1569
1570 spin_lock(&q->sendq.lock);
1571 __skb_unlink(skb, &q->sendq);
1572 if (is_ofld_imm(skb))
1573 kfree_skb(skb);
1574 }
1575 if (likely(written))
1576 ring_tx_db(q->adap, &q->q, written);
1577}
1578
1579/**
1580 * ofld_xmit - send a packet through an offload queue
1581 * @q: the Tx offload queue
1582 * @skb: the packet
1583 *
1584 * Send an offload packet through an SGE offload queue.
1585 */
1586static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
1587{
1588 skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
1589 spin_lock(&q->sendq.lock);
1590 __skb_queue_tail(&q->sendq, skb);
1591 if (q->sendq.qlen == 1)
1592 service_ofldq(q);
1593 spin_unlock(&q->sendq.lock);
1594 return NET_XMIT_SUCCESS;
1595}
1596
1597/**
1598 * restart_ofldq - restart a suspended offload queue
1599 * @data: the offload queue to restart
1600 *
1601 * Resumes transmission on a suspended Tx offload queue.
1602 */
1603static void restart_ofldq(unsigned long data)
1604{
1605 struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
1606
1607 spin_lock(&q->sendq.lock);
1608 q->full = 0; /* the queue actually is completely empty now */
1609 service_ofldq(q);
1610 spin_unlock(&q->sendq.lock);
1611}
1612
1613/**
1614 * skb_txq - return the Tx queue an offload packet should use
1615 * @skb: the packet
1616 *
1617 * Returns the Tx queue an offload packet should use as indicated by bits
1618 * 1-15 in the packet's queue_mapping.
1619 */
1620static inline unsigned int skb_txq(const struct sk_buff *skb)
1621{
1622 return skb->queue_mapping >> 1;
1623}
1624
1625/**
1626 * is_ctrl_pkt - return whether an offload packet is a control packet
1627 * @skb: the packet
1628 *
1629 * Returns whether an offload packet should use an OFLD or a CTRL
1630 * Tx queue as indicated by bit 0 in the packet's queue_mapping.
1631 */
1632static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
1633{
1634 return skb->queue_mapping & 1;
1635}
1636
1637static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
1638{
1639 unsigned int idx = skb_txq(skb);
1640
Kumar Sanghvi4fe44dd2014-02-18 17:56:11 +05301641 if (unlikely(is_ctrl_pkt(skb))) {
1642 /* Single ctrl queue is a requirement for LE workaround path */
1643 if (adap->tids.nsftids)
1644 idx = 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001645 return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
Kumar Sanghvi4fe44dd2014-02-18 17:56:11 +05301646 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001647 return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
1648}
1649
1650/**
1651 * t4_ofld_send - send an offload packet
1652 * @adap: the adapter
1653 * @skb: the packet
1654 *
1655 * Sends an offload packet. We use the packet queue_mapping to select the
1656 * appropriate Tx queue as follows: bit 0 indicates whether the packet
1657 * should be sent as regular or control, bits 1-15 select the queue.
1658 */
1659int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
1660{
1661 int ret;
1662
1663 local_bh_disable();
1664 ret = ofld_send(adap, skb);
1665 local_bh_enable();
1666 return ret;
1667}
1668
1669/**
1670 * cxgb4_ofld_send - send an offload packet
1671 * @dev: the net device
1672 * @skb: the packet
1673 *
1674 * Sends an offload packet. This is an exported version of @t4_ofld_send,
1675 * intended for ULDs.
1676 */
1677int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
1678{
1679 return t4_ofld_send(netdev2adap(dev), skb);
1680}
1681EXPORT_SYMBOL(cxgb4_ofld_send);
1682
Ian Campbelle91b0f22011-10-19 23:01:46 +00001683static inline void copy_frags(struct sk_buff *skb,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001684 const struct pkt_gl *gl, unsigned int offset)
1685{
Ian Campbelle91b0f22011-10-19 23:01:46 +00001686 int i;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001687
1688 /* usually there's just one frag */
Ian Campbelle91b0f22011-10-19 23:01:46 +00001689 __skb_fill_page_desc(skb, 0, gl->frags[0].page,
1690 gl->frags[0].offset + offset,
1691 gl->frags[0].size - offset);
1692 skb_shinfo(skb)->nr_frags = gl->nfrags;
1693 for (i = 1; i < gl->nfrags; i++)
1694 __skb_fill_page_desc(skb, i, gl->frags[i].page,
1695 gl->frags[i].offset,
1696 gl->frags[i].size);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001697
1698 /* get a reference to the last page, we don't own it */
Ian Campbelle91b0f22011-10-19 23:01:46 +00001699 get_page(gl->frags[gl->nfrags - 1].page);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001700}
1701
1702/**
1703 * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
1704 * @gl: the gather list
1705 * @skb_len: size of sk_buff main body if it carries fragments
1706 * @pull_len: amount of data to move to the sk_buff's main body
1707 *
1708 * Builds an sk_buff from the given packet gather list. Returns the
1709 * sk_buff or %NULL if sk_buff allocation failed.
1710 */
1711struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
1712 unsigned int skb_len, unsigned int pull_len)
1713{
1714 struct sk_buff *skb;
1715
1716 /*
1717 * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
1718 * size, which is expected since buffers are at least PAGE_SIZEd.
1719 * In this case packets up to RX_COPY_THRES have only one fragment.
1720 */
1721 if (gl->tot_len <= RX_COPY_THRES) {
1722 skb = dev_alloc_skb(gl->tot_len);
1723 if (unlikely(!skb))
1724 goto out;
1725 __skb_put(skb, gl->tot_len);
1726 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1727 } else {
1728 skb = dev_alloc_skb(skb_len);
1729 if (unlikely(!skb))
1730 goto out;
1731 __skb_put(skb, pull_len);
1732 skb_copy_to_linear_data(skb, gl->va, pull_len);
1733
Ian Campbelle91b0f22011-10-19 23:01:46 +00001734 copy_frags(skb, gl, pull_len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001735 skb->len = gl->tot_len;
1736 skb->data_len = skb->len - pull_len;
1737 skb->truesize += skb->data_len;
1738 }
1739out: return skb;
1740}
1741EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
1742
1743/**
1744 * t4_pktgl_free - free a packet gather list
1745 * @gl: the gather list
1746 *
1747 * Releases the pages of a packet gather list. We do not own the last
1748 * page on the list and do not free it.
1749 */
Roland Dreierde498c82010-04-21 08:59:17 +00001750static void t4_pktgl_free(const struct pkt_gl *gl)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001751{
1752 int n;
Ian Campbelle91b0f22011-10-19 23:01:46 +00001753 const struct page_frag *p;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001754
1755 for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
1756 put_page(p->page);
1757}
1758
1759/*
1760 * Process an MPS trace packet. Give it an unused protocol number so it won't
1761 * be delivered to anyone and send it to the stack for capture.
1762 */
1763static noinline int handle_trace_pkt(struct adapter *adap,
1764 const struct pkt_gl *gl)
1765{
1766 struct sk_buff *skb;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001767
1768 skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
1769 if (unlikely(!skb)) {
1770 t4_pktgl_free(gl);
1771 return 0;
1772 }
1773
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301774 if (is_t4(adap->params.chip))
Santosh Rastapur0a57a532013-03-14 05:08:49 +00001775 __skb_pull(skb, sizeof(struct cpl_trace_pkt));
1776 else
1777 __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
1778
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001779 skb_reset_mac_header(skb);
1780 skb->protocol = htons(0xffff);
1781 skb->dev = adap->port[0];
1782 netif_receive_skb(skb);
1783 return 0;
1784}
1785
1786static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1787 const struct cpl_rx_pkt *pkt)
1788{
Vipul Pandya52367a72012-09-26 02:39:38 +00001789 struct adapter *adapter = rxq->rspq.adap;
1790 struct sge *s = &adapter->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001791 int ret;
1792 struct sk_buff *skb;
1793
1794 skb = napi_get_frags(&rxq->rspq.napi);
1795 if (unlikely(!skb)) {
1796 t4_pktgl_free(gl);
1797 rxq->stats.rx_drops++;
1798 return;
1799 }
1800
Vipul Pandya52367a72012-09-26 02:39:38 +00001801 copy_frags(skb, gl, s->pktshift);
1802 skb->len = gl->tot_len - s->pktshift;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001803 skb->data_len = skb->len;
1804 skb->truesize += skb->data_len;
1805 skb->ip_summed = CHECKSUM_UNNECESSARY;
1806 skb_record_rx_queue(skb, rxq->rspq.idx);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05301807 skb_mark_napi_id(skb, &rxq->rspq.napi);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001808 if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
Tom Herbert82649892013-12-17 23:23:29 -08001809 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
1810 PKT_HASH_TYPE_L3);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001811
1812 if (unlikely(pkt->vlan_ex)) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001813 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001814 rxq->stats.vlan_ex++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001815 }
1816 ret = napi_gro_frags(&rxq->rspq.napi);
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001817 if (ret == GRO_HELD)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001818 rxq->stats.lro_pkts++;
1819 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1820 rxq->stats.lro_merged++;
1821 rxq->stats.pkts++;
1822 rxq->stats.rx_cso++;
1823}
1824
1825/**
1826 * t4_ethrx_handler - process an ingress ethernet packet
1827 * @q: the response queue that received the packet
1828 * @rsp: the response queue descriptor holding the RX_PKT message
1829 * @si: the gather list of packet fragments
1830 *
1831 * Process an ingress ethernet packet and deliver it to the stack.
1832 */
1833int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
1834 const struct pkt_gl *si)
1835{
1836 bool csum_ok;
1837 struct sk_buff *skb;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001838 const struct cpl_rx_pkt *pkt;
1839 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
Vipul Pandya52367a72012-09-26 02:39:38 +00001840 struct sge *s = &q->adap->sge;
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301841 int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
Santosh Rastapur0a57a532013-03-14 05:08:49 +00001842 CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
Varun Prakash84a200b2015-03-24 19:14:46 +05301843#ifdef CONFIG_CHELSIO_T4_FCOE
1844 struct port_info *pi;
1845#endif
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001846
Santosh Rastapur0a57a532013-03-14 05:08:49 +00001847 if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001848 return handle_trace_pkt(q->adap, si);
1849
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001850 pkt = (const struct cpl_rx_pkt *)rsp;
Hariprasad Shenaicca28222014-05-07 18:01:03 +05301851 csum_ok = pkt->csum_calc && !pkt->err_vec &&
1852 (q->netdev->features & NETIF_F_RXCSUM);
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001853 if ((pkt->l2info & htonl(RXF_TCP_F)) &&
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05301854 !(cxgb_poll_busy_polling(q)) &&
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001855 (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
1856 do_gro(rxq, si, pkt);
1857 return 0;
1858 }
1859
1860 skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
1861 if (unlikely(!skb)) {
1862 t4_pktgl_free(si);
1863 rxq->stats.rx_drops++;
1864 return 0;
1865 }
1866
Vipul Pandya52367a72012-09-26 02:39:38 +00001867 __skb_pull(skb, s->pktshift); /* remove ethernet header padding */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001868 skb->protocol = eth_type_trans(skb, q->netdev);
1869 skb_record_rx_queue(skb, q->idx);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001870 if (skb->dev->features & NETIF_F_RXHASH)
Tom Herbert82649892013-12-17 23:23:29 -08001871 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
1872 PKT_HASH_TYPE_L3);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001873
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001874 rxq->stats.pkts++;
1875
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001876 if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00001877 if (!pkt->ip_frag) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001878 skb->ip_summed = CHECKSUM_UNNECESSARY;
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00001879 rxq->stats.rx_cso++;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001880 } else if (pkt->l2info & htonl(RXF_IP_F)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001881 __sum16 c = (__force __sum16)pkt->csum;
1882 skb->csum = csum_unfold(c);
1883 skb->ip_summed = CHECKSUM_COMPLETE;
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00001884 rxq->stats.rx_cso++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001885 }
Varun Prakash84a200b2015-03-24 19:14:46 +05301886 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001887 skb_checksum_none_assert(skb);
Varun Prakash84a200b2015-03-24 19:14:46 +05301888#ifdef CONFIG_CHELSIO_T4_FCOE
1889#define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
1890 RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
1891
1892 pi = netdev_priv(skb->dev);
1893 if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
1894 if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
1895 (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
1896 if (!(pkt->err_vec & cpu_to_be16(RXERR_CSUM_F)))
1897 skb->ip_summed = CHECKSUM_UNNECESSARY;
1898 }
1899 }
1900
1901#undef CPL_RX_PKT_FLAGS
1902#endif /* CONFIG_CHELSIO_T4_FCOE */
1903 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001904
1905 if (unlikely(pkt->vlan_ex)) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001906 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001907 rxq->stats.vlan_ex++;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001908 }
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05301909 skb_mark_napi_id(skb, &q->napi);
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001910 netif_receive_skb(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001911 return 0;
1912}
1913
1914/**
1915 * restore_rx_bufs - put back a packet's Rx buffers
1916 * @si: the packet gather list
1917 * @q: the SGE free list
1918 * @frags: number of FL buffers to restore
1919 *
1920 * Puts back on an FL the Rx buffers associated with @si. The buffers
1921 * have already been unmapped and are left unmapped, we mark them so to
1922 * prevent further unmapping attempts.
1923 *
1924 * This function undoes a series of @unmap_rx_buf calls when we find out
1925 * that the current packet can't be processed right away afterall and we
1926 * need to come back to it later. This is a very rare event and there's
1927 * no effort to make this particularly efficient.
1928 */
1929static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
1930 int frags)
1931{
1932 struct rx_sw_desc *d;
1933
1934 while (frags--) {
1935 if (q->cidx == 0)
1936 q->cidx = q->size - 1;
1937 else
1938 q->cidx--;
1939 d = &q->sdesc[q->cidx];
1940 d->page = si->frags[frags].page;
1941 d->dma_addr |= RX_UNMAPPED_BUF;
1942 q->avail++;
1943 }
1944}
1945
1946/**
1947 * is_new_response - check if a response is newly written
1948 * @r: the response descriptor
1949 * @q: the response queue
1950 *
1951 * Returns true if a response descriptor contains a yet unprocessed
1952 * response.
1953 */
1954static inline bool is_new_response(const struct rsp_ctrl *r,
1955 const struct sge_rspq *q)
1956{
1957 return RSPD_GEN(r->type_gen) == q->gen;
1958}
1959
1960/**
1961 * rspq_next - advance to the next entry in a response queue
1962 * @q: the queue
1963 *
1964 * Updates the state of a response queue to advance it to the next entry.
1965 */
1966static inline void rspq_next(struct sge_rspq *q)
1967{
1968 q->cur_desc = (void *)q->cur_desc + q->iqe_len;
1969 if (unlikely(++q->cidx == q->size)) {
1970 q->cidx = 0;
1971 q->gen ^= 1;
1972 q->cur_desc = q->desc;
1973 }
1974}
1975
1976/**
1977 * process_responses - process responses from an SGE response queue
1978 * @q: the ingress queue to process
1979 * @budget: how many responses can be processed in this round
1980 *
1981 * Process responses from an SGE response queue up to the supplied budget.
1982 * Responses include received packets as well as control messages from FW
1983 * or HW.
1984 *
1985 * Additionally choose the interrupt holdoff time for the next interrupt
1986 * on this queue. If the system is under memory shortage use a fairly
1987 * long delay to help recovery.
1988 */
1989static int process_responses(struct sge_rspq *q, int budget)
1990{
1991 int ret, rsp_type;
1992 int budget_left = budget;
1993 const struct rsp_ctrl *rc;
1994 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
Vipul Pandya52367a72012-09-26 02:39:38 +00001995 struct adapter *adapter = q->adap;
1996 struct sge *s = &adapter->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001997
1998 while (likely(budget_left)) {
1999 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
2000 if (!is_new_response(rc, q))
2001 break;
2002
Alexander Duyck019be1c2015-04-08 18:49:29 -07002003 dma_rmb();
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002004 rsp_type = RSPD_TYPE(rc->type_gen);
2005 if (likely(rsp_type == RSP_TYPE_FLBUF)) {
Ian Campbelle91b0f22011-10-19 23:01:46 +00002006 struct page_frag *fp;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002007 struct pkt_gl si;
2008 const struct rx_sw_desc *rsd;
2009 u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
2010
2011 if (len & RSPD_NEWBUF) {
2012 if (likely(q->offset > 0)) {
2013 free_rx_bufs(q->adap, &rxq->fl, 1);
2014 q->offset = 0;
2015 }
Casey Leedom1704d742010-06-25 12:09:38 +00002016 len = RSPD_LEN(len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002017 }
2018 si.tot_len = len;
2019
2020 /* gather packet fragments */
2021 for (frags = 0, fp = si.frags; ; frags++, fp++) {
2022 rsd = &rxq->fl.sdesc[rxq->fl.cidx];
Vipul Pandya52367a72012-09-26 02:39:38 +00002023 bufsz = get_buf_size(adapter, rsd);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002024 fp->page = rsd->page;
Ian Campbelle91b0f22011-10-19 23:01:46 +00002025 fp->offset = q->offset;
2026 fp->size = min(bufsz, len);
2027 len -= fp->size;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002028 if (!len)
2029 break;
2030 unmap_rx_buf(q->adap, &rxq->fl);
2031 }
2032
2033 /*
2034 * Last buffer remains mapped so explicitly make it
2035 * coherent for CPU access.
2036 */
2037 dma_sync_single_for_cpu(q->adap->pdev_dev,
2038 get_buf_addr(rsd),
Ian Campbelle91b0f22011-10-19 23:01:46 +00002039 fp->size, DMA_FROM_DEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002040
2041 si.va = page_address(si.frags[0].page) +
Ian Campbelle91b0f22011-10-19 23:01:46 +00002042 si.frags[0].offset;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002043 prefetch(si.va);
2044
2045 si.nfrags = frags + 1;
2046 ret = q->handler(q, q->cur_desc, &si);
2047 if (likely(ret == 0))
Vipul Pandya52367a72012-09-26 02:39:38 +00002048 q->offset += ALIGN(fp->size, s->fl_align);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002049 else
2050 restore_rx_bufs(&si, &rxq->fl, frags);
2051 } else if (likely(rsp_type == RSP_TYPE_CPL)) {
2052 ret = q->handler(q, q->cur_desc, NULL);
2053 } else {
2054 ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
2055 }
2056
2057 if (unlikely(ret)) {
2058 /* couldn't process descriptor, back off for recovery */
2059 q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
2060 break;
2061 }
2062
2063 rspq_next(q);
2064 budget_left--;
2065 }
2066
2067 if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
2068 __refill_fl(q->adap, &rxq->fl);
2069 return budget - budget_left;
2070}
2071
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302072#ifdef CONFIG_NET_RX_BUSY_POLL
2073int cxgb_busy_poll(struct napi_struct *napi)
2074{
2075 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
2076 unsigned int params, work_done;
2077 u32 val;
2078
2079 if (!cxgb_poll_lock_poll(q))
2080 return LL_FLUSH_BUSY;
2081
2082 work_done = process_responses(q, 4);
2083 params = QINTR_TIMER_IDX(TIMERREG_COUNTER0_X) | QINTR_CNT_EN;
2084 q->next_intr_params = params;
2085 val = CIDXINC_V(work_done) | SEINTARM_V(params);
2086
2087 /* If we don't have access to the new User GTS (T5+), use the old
2088 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2089 */
2090 if (unlikely(!q->bar2_addr))
2091 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
2092 val | INGRESSQID_V((u32)q->cntxt_id));
2093 else {
2094 writel(val | INGRESSQID_V(q->bar2_qid),
2095 q->bar2_addr + SGE_UDB_GTS);
2096 wmb();
2097 }
2098
2099 cxgb_poll_unlock_poll(q);
2100 return work_done;
2101}
2102#endif /* CONFIG_NET_RX_BUSY_POLL */
2103
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002104/**
2105 * napi_rx_handler - the NAPI handler for Rx processing
2106 * @napi: the napi instance
2107 * @budget: how many packets we can process in this round
2108 *
2109 * Handler for new data events when using NAPI. This does not need any
2110 * locking or protection from interrupts as data interrupts are off at
2111 * this point and other adapter interrupts do not interfere (the latter
2112 * in not a concern at all with MSI-X as non-data interrupts then have
2113 * a separate handler).
2114 */
2115static int napi_rx_handler(struct napi_struct *napi, int budget)
2116{
2117 unsigned int params;
2118 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302119 int work_done;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302120 u32 val;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002121
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302122 if (!cxgb_poll_lock_napi(q))
2123 return budget;
2124
2125 work_done = process_responses(q, budget);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002126 if (likely(work_done < budget)) {
Hariprasad Shenaie553ec32014-09-26 00:23:55 +05302127 int timer_index;
2128
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002129 napi_complete(napi);
Hariprasad Shenaie553ec32014-09-26 00:23:55 +05302130 timer_index = QINTR_TIMER_IDX_GET(q->next_intr_params);
2131
2132 if (q->adaptive_rx) {
2133 if (work_done > max(timer_pkt_quota[timer_index],
2134 MIN_NAPI_WORK))
2135 timer_index = (timer_index + 1);
2136 else
2137 timer_index = timer_index - 1;
2138
2139 timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
2140 q->next_intr_params = QINTR_TIMER_IDX(timer_index) |
2141 V_QINTR_CNT_EN;
2142 params = q->next_intr_params;
2143 } else {
2144 params = q->next_intr_params;
2145 q->next_intr_params = q->intr_params;
2146 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002147 } else
2148 params = QINTR_TIMER_IDX(7);
2149
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302150 val = CIDXINC_V(work_done) | SEINTARM_V(params);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302151
2152 /* If we don't have access to the new User GTS (T5+), use the old
2153 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2154 */
2155 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302156 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
2157 val | INGRESSQID_V((u32)q->cntxt_id));
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302158 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302159 writel(val | INGRESSQID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302160 q->bar2_addr + SGE_UDB_GTS);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302161 wmb();
2162 }
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302163 cxgb_poll_unlock_napi(q);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002164 return work_done;
2165}
2166
2167/*
2168 * The MSI-X interrupt handler for an SGE response queue.
2169 */
2170irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
2171{
2172 struct sge_rspq *q = cookie;
2173
2174 napi_schedule(&q->napi);
2175 return IRQ_HANDLED;
2176}
2177
2178/*
2179 * Process the indirect interrupt entries in the interrupt queue and kick off
2180 * NAPI for each queue that has generated an entry.
2181 */
2182static unsigned int process_intrq(struct adapter *adap)
2183{
2184 unsigned int credits;
2185 const struct rsp_ctrl *rc;
2186 struct sge_rspq *q = &adap->sge.intrq;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302187 u32 val;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002188
2189 spin_lock(&adap->sge.intrq_lock);
2190 for (credits = 0; ; credits++) {
2191 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
2192 if (!is_new_response(rc, q))
2193 break;
2194
Alexander Duyck019be1c2015-04-08 18:49:29 -07002195 dma_rmb();
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002196 if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
2197 unsigned int qid = ntohl(rc->pldbuflen_qid);
2198
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002199 qid -= adap->sge.ingr_start;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002200 napi_schedule(&adap->sge.ingr_map[qid]->napi);
2201 }
2202
2203 rspq_next(q);
2204 }
2205
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302206 val = CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302207
2208 /* If we don't have access to the new User GTS (T5+), use the old
2209 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2210 */
2211 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302212 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
2213 val | INGRESSQID_V(q->cntxt_id));
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302214 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302215 writel(val | INGRESSQID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302216 q->bar2_addr + SGE_UDB_GTS);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302217 wmb();
2218 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002219 spin_unlock(&adap->sge.intrq_lock);
2220 return credits;
2221}
2222
2223/*
2224 * The MSI interrupt handler, which handles data events from SGE response queues
2225 * as well as error and other async events as they all use the same MSI vector.
2226 */
2227static irqreturn_t t4_intr_msi(int irq, void *cookie)
2228{
2229 struct adapter *adap = cookie;
2230
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +05302231 if (adap->flags & MASTER_PF)
2232 t4_slow_intr_handler(adap);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002233 process_intrq(adap);
2234 return IRQ_HANDLED;
2235}
2236
2237/*
2238 * Interrupt handler for legacy INTx interrupts.
2239 * Handles data events from SGE response queues as well as error and other
2240 * async events as they all use the same interrupt line.
2241 */
2242static irqreturn_t t4_intr_intx(int irq, void *cookie)
2243{
2244 struct adapter *adap = cookie;
2245
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302246 t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +05302247 if (((adap->flags & MASTER_PF) && t4_slow_intr_handler(adap)) |
2248 process_intrq(adap))
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002249 return IRQ_HANDLED;
2250 return IRQ_NONE; /* probably shared interrupt */
2251}
2252
2253/**
2254 * t4_intr_handler - select the top-level interrupt handler
2255 * @adap: the adapter
2256 *
2257 * Selects the top-level interrupt handler based on the type of interrupts
2258 * (MSI-X, MSI, or INTx).
2259 */
2260irq_handler_t t4_intr_handler(struct adapter *adap)
2261{
2262 if (adap->flags & USING_MSIX)
2263 return t4_sge_intr_msix;
2264 if (adap->flags & USING_MSI)
2265 return t4_intr_msi;
2266 return t4_intr_intx;
2267}
2268
2269static void sge_rx_timer_cb(unsigned long data)
2270{
2271 unsigned long m;
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302272 unsigned int i;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002273 struct adapter *adap = (struct adapter *)data;
2274 struct sge *s = &adap->sge;
2275
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302276 for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002277 for (m = s->starving_fl[i]; m; m &= m - 1) {
2278 struct sge_eth_rxq *rxq;
2279 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
2280 struct sge_fl *fl = s->egr_map[id];
2281
2282 clear_bit(id, s->starving_fl);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002283 smp_mb__after_atomic();
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002284
Hariprasad Shenaic098b022015-04-15 02:02:31 +05302285 if (fl_starving(adap, fl)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002286 rxq = container_of(fl, struct sge_eth_rxq, fl);
2287 if (napi_reschedule(&rxq->rspq.napi))
2288 fl->starving++;
2289 else
2290 set_bit(id, s->starving_fl);
2291 }
2292 }
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302293 /* The remainder of the SGE RX Timer Callback routine is dedicated to
2294 * global Master PF activities like checking for chip ingress stalls,
2295 * etc.
2296 */
2297 if (!(adap->flags & MASTER_PF))
2298 goto done;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002299
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302300 t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002301
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302302done:
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002303 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
2304}
2305
2306static void sge_tx_timer_cb(unsigned long data)
2307{
2308 unsigned long m;
2309 unsigned int i, budget;
2310 struct adapter *adap = (struct adapter *)data;
2311 struct sge *s = &adap->sge;
2312
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302313 for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002314 for (m = s->txq_maperr[i]; m; m &= m - 1) {
2315 unsigned long id = __ffs(m) + i * BITS_PER_LONG;
2316 struct sge_ofld_txq *txq = s->egr_map[id];
2317
2318 clear_bit(id, s->txq_maperr);
2319 tasklet_schedule(&txq->qresume_tsk);
2320 }
2321
2322 budget = MAX_TIMER_TX_RECLAIM;
2323 i = s->ethtxq_rover;
2324 do {
2325 struct sge_eth_txq *q = &s->ethtxq[i];
2326
2327 if (q->q.in_use &&
2328 time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
2329 __netif_tx_trylock(q->txq)) {
2330 int avail = reclaimable(&q->q);
2331
2332 if (avail) {
2333 if (avail > budget)
2334 avail = budget;
2335
2336 free_tx_desc(adap, &q->q, avail, true);
2337 q->q.in_use -= avail;
2338 budget -= avail;
2339 }
2340 __netif_tx_unlock(q->txq);
2341 }
2342
2343 if (++i >= s->ethqsets)
2344 i = 0;
2345 } while (budget && i != s->ethtxq_rover);
2346 s->ethtxq_rover = i;
2347 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
2348}
2349
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302350/**
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302351 * bar2_address - return the BAR2 address for an SGE Queue's Registers
2352 * @adapter: the adapter
2353 * @qid: the SGE Queue ID
2354 * @qtype: the SGE Queue Type (Egress or Ingress)
2355 * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302356 *
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302357 * Returns the BAR2 address for the SGE Queue Registers associated with
2358 * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
2359 * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
2360 * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
2361 * Registers are supported (e.g. the Write Combining Doorbell Buffer).
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302362 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302363static void __iomem *bar2_address(struct adapter *adapter,
2364 unsigned int qid,
2365 enum t4_bar2_qtype qtype,
2366 unsigned int *pbar2_qid)
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302367{
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302368 u64 bar2_qoffset;
2369 int ret;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302370
Stephen Rothwelldd0bcc02014-12-10 19:48:02 +11002371 ret = cxgb4_t4_bar2_sge_qregs(adapter, qid, qtype,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302372 &bar2_qoffset, pbar2_qid);
2373 if (ret)
2374 return NULL;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302375
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302376 return adapter->bar2 + bar2_qoffset;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302377}
2378
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302379/* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
2380 * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
2381 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002382int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
2383 struct net_device *dev, int intr_idx,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302384 struct sge_fl *fl, rspq_handler_t hnd, int cong)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002385{
2386 int ret, flsz = 0;
2387 struct fw_iq_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002388 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002389 struct port_info *pi = netdev_priv(dev);
2390
2391 /* Size needs to be multiple of 16, including status entry. */
2392 iq->size = roundup(iq->size, 16);
2393
2394 iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +00002395 &iq->phys_addr, NULL, 0, NUMA_NO_NODE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002396 if (!iq->desc)
2397 return -ENOMEM;
2398
2399 memset(&c, 0, sizeof(c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302400 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
2401 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302402 FW_IQ_CMD_PFN_V(adap->fn) | FW_IQ_CMD_VFN_V(0));
2403 c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002404 FW_LEN16(c));
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302405 c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
2406 FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
2407 FW_IQ_CMD_IQANDST_V(intr_idx < 0) | FW_IQ_CMD_IQANUD_V(1) |
2408 FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002409 -intr_idx - 1));
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302410 c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
2411 FW_IQ_CMD_IQGTSMODE_F |
2412 FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
2413 FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002414 c.iqsize = htons(iq->size);
2415 c.iqaddr = cpu_to_be64(iq->phys_addr);
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302416 if (cong >= 0)
2417 c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002418
2419 if (fl) {
Hariprasad Shenai13432992015-05-05 14:59:51 +05302420 /* Allocate the ring for the hardware free list (with space
2421 * for its status page) along with the associated software
2422 * descriptor ring. The free list size needs to be a multiple
2423 * of the Egress Queue Unit and at least 2 Egress Units larger
2424 * than the SGE's Egress Congrestion Threshold
2425 * (fl_starve_thres - 1).
2426 */
2427 if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
2428 fl->size = s->fl_starve_thres - 1 + 2 * 8;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002429 fl->size = roundup(fl->size, 8);
2430 fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
2431 sizeof(struct rx_sw_desc), &fl->addr,
Vipul Pandya52367a72012-09-26 02:39:38 +00002432 &fl->sdesc, s->stat_len, NUMA_NO_NODE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002433 if (!fl->desc)
2434 goto fl_nomem;
2435
Vipul Pandya52367a72012-09-26 02:39:38 +00002436 flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302437 c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
2438 FW_IQ_CMD_FL0FETCHRO_F |
2439 FW_IQ_CMD_FL0DATARO_F |
2440 FW_IQ_CMD_FL0PADEN_F);
2441 if (cong >= 0)
2442 c.iqns_to_fl0congen |=
2443 htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
2444 FW_IQ_CMD_FL0CONGCIF_F |
2445 FW_IQ_CMD_FL0CONGEN_F);
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302446 c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN_V(2) |
2447 FW_IQ_CMD_FL0FBMAX_V(3));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002448 c.fl0size = htons(flsz);
2449 c.fl0addr = cpu_to_be64(fl->addr);
2450 }
2451
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002452 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002453 if (ret)
2454 goto err;
2455
2456 netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302457 napi_hash_add(&iq->napi);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002458 iq->cur_desc = iq->desc;
2459 iq->cidx = 0;
2460 iq->gen = 1;
2461 iq->next_intr_params = iq->intr_params;
2462 iq->cntxt_id = ntohs(c.iqid);
2463 iq->abs_id = ntohs(c.physiqid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302464 iq->bar2_addr = bar2_address(adap,
2465 iq->cntxt_id,
2466 T4_BAR2_QTYPE_INGRESS,
2467 &iq->bar2_qid);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002468 iq->size--; /* subtract status entry */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002469 iq->netdev = dev;
2470 iq->handler = hnd;
2471
2472 /* set offset to -1 to distinguish ingress queues without FL */
2473 iq->offset = fl ? 0 : -1;
2474
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002475 adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002476
2477 if (fl) {
Roland Dreier62718b32010-04-21 08:09:21 +00002478 fl->cntxt_id = ntohs(c.fl0id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002479 fl->avail = fl->pend_cred = 0;
2480 fl->pidx = fl->cidx = 0;
2481 fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002482 adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302483
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302484 /* Note, we must initialize the BAR2 Free List User Doorbell
2485 * information before refilling the Free List!
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302486 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302487 fl->bar2_addr = bar2_address(adap,
2488 fl->cntxt_id,
2489 T4_BAR2_QTYPE_EGRESS,
2490 &fl->bar2_qid);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002491 refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
2492 }
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302493
2494 /* For T5 and later we attempt to set up the Congestion Manager values
2495 * of the new RX Ethernet Queue. This should really be handled by
2496 * firmware because it's more complex than any host driver wants to
2497 * get involved with and it's different per chip and this is almost
2498 * certainly wrong. Firmware would be wrong as well, but it would be
2499 * a lot easier to fix in one place ... For now we do something very
2500 * simple (and hopefully less wrong).
2501 */
2502 if (!is_t4(adap->params.chip) && cong >= 0) {
2503 u32 param, val;
2504 int i;
2505
2506 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
2507 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
2508 FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
2509 if (cong == 0) {
2510 val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
2511 } else {
2512 val =
2513 CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
2514 for (i = 0; i < 4; i++) {
2515 if (cong & (1 << i))
2516 val |=
2517 CONMCTXT_CNGCHMAP_V(1 << (i << 2));
2518 }
2519 }
2520 ret = t4_set_params(adap, adap->mbox, adap->fn, 0, 1,
2521 &param, &val);
2522 if (ret)
2523 dev_warn(adap->pdev_dev, "Failed to set Congestion"
2524 " Manager Context for Ingress Queue %d: %d\n",
2525 iq->cntxt_id, -ret);
2526 }
2527
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002528 return 0;
2529
2530fl_nomem:
2531 ret = -ENOMEM;
2532err:
2533 if (iq->desc) {
2534 dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
2535 iq->desc, iq->phys_addr);
2536 iq->desc = NULL;
2537 }
2538 if (fl && fl->desc) {
2539 kfree(fl->sdesc);
2540 fl->sdesc = NULL;
2541 dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
2542 fl->desc, fl->addr);
2543 fl->desc = NULL;
2544 }
2545 return ret;
2546}
2547
2548static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
2549{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00002550 q->cntxt_id = id;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302551 q->bar2_addr = bar2_address(adap,
2552 q->cntxt_id,
2553 T4_BAR2_QTYPE_EGRESS,
2554 &q->bar2_qid);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002555 q->in_use = 0;
2556 q->cidx = q->pidx = 0;
2557 q->stops = q->restarts = 0;
2558 q->stat = (void *)&q->desc[q->size];
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302559 spin_lock_init(&q->db_lock);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002560 adap->sge.egr_map[id - adap->sge.egr_start] = q;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002561}
2562
2563int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
2564 struct net_device *dev, struct netdev_queue *netdevq,
2565 unsigned int iqid)
2566{
2567 int ret, nentries;
2568 struct fw_eq_eth_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002569 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002570 struct port_info *pi = netdev_priv(dev);
2571
2572 /* Add status entries */
Vipul Pandya52367a72012-09-26 02:39:38 +00002573 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002574
2575 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2576 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
Vipul Pandya52367a72012-09-26 02:39:38 +00002577 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +00002578 netdev_queue_numa_node_read(netdevq));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002579 if (!txq->q.desc)
2580 return -ENOMEM;
2581
2582 memset(&c, 0, sizeof(c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302583 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
2584 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302585 FW_EQ_ETH_CMD_PFN_V(adap->fn) |
2586 FW_EQ_ETH_CMD_VFN_V(0));
2587 c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
2588 FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
2589 c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
2590 FW_EQ_ETH_CMD_VIID_V(pi->viid));
2591 c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V(2) |
2592 FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
2593 FW_EQ_ETH_CMD_FETCHRO_V(1) |
2594 FW_EQ_ETH_CMD_IQID_V(iqid));
2595 c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN_V(2) |
2596 FW_EQ_ETH_CMD_FBMAX_V(3) |
2597 FW_EQ_ETH_CMD_CIDXFTHRESH_V(5) |
2598 FW_EQ_ETH_CMD_EQSIZE_V(nentries));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002599 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2600
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002601 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002602 if (ret) {
2603 kfree(txq->q.sdesc);
2604 txq->q.sdesc = NULL;
2605 dma_free_coherent(adap->pdev_dev,
2606 nentries * sizeof(struct tx_desc),
2607 txq->q.desc, txq->q.phys_addr);
2608 txq->q.desc = NULL;
2609 return ret;
2610 }
2611
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302612 init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002613 txq->txq = netdevq;
2614 txq->tso = txq->tx_cso = txq->vlan_ins = 0;
2615 txq->mapping_err = 0;
2616 return 0;
2617}
2618
2619int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
2620 struct net_device *dev, unsigned int iqid,
2621 unsigned int cmplqid)
2622{
2623 int ret, nentries;
2624 struct fw_eq_ctrl_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002625 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002626 struct port_info *pi = netdev_priv(dev);
2627
2628 /* Add status entries */
Vipul Pandya52367a72012-09-26 02:39:38 +00002629 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002630
2631 txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
2632 sizeof(struct tx_desc), 0, &txq->q.phys_addr,
Hariprasad Shenai982b81e2015-05-05 14:59:54 +05302633 NULL, 0, dev_to_node(adap->pdev_dev));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002634 if (!txq->q.desc)
2635 return -ENOMEM;
2636
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302637 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
2638 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302639 FW_EQ_CTRL_CMD_PFN_V(adap->fn) |
2640 FW_EQ_CTRL_CMD_VFN_V(0));
2641 c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
2642 FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
2643 c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002644 c.physeqid_pkd = htonl(0);
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302645 c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(2) |
2646 FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
2647 FW_EQ_CTRL_CMD_FETCHRO_F |
2648 FW_EQ_CTRL_CMD_IQID_V(iqid));
2649 c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN_V(2) |
2650 FW_EQ_CTRL_CMD_FBMAX_V(3) |
2651 FW_EQ_CTRL_CMD_CIDXFTHRESH_V(5) |
2652 FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002653 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2654
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002655 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002656 if (ret) {
2657 dma_free_coherent(adap->pdev_dev,
2658 nentries * sizeof(struct tx_desc),
2659 txq->q.desc, txq->q.phys_addr);
2660 txq->q.desc = NULL;
2661 return ret;
2662 }
2663
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302664 init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002665 txq->adap = adap;
2666 skb_queue_head_init(&txq->sendq);
2667 tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
2668 txq->full = 0;
2669 return 0;
2670}
2671
2672int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
2673 struct net_device *dev, unsigned int iqid)
2674{
2675 int ret, nentries;
2676 struct fw_eq_ofld_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002677 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002678 struct port_info *pi = netdev_priv(dev);
2679
2680 /* Add status entries */
Vipul Pandya52367a72012-09-26 02:39:38 +00002681 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002682
2683 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2684 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
Vipul Pandya52367a72012-09-26 02:39:38 +00002685 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +00002686 NUMA_NO_NODE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002687 if (!txq->q.desc)
2688 return -ENOMEM;
2689
2690 memset(&c, 0, sizeof(c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302691 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST_F |
2692 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302693 FW_EQ_OFLD_CMD_PFN_V(adap->fn) |
2694 FW_EQ_OFLD_CMD_VFN_V(0));
2695 c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
2696 FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
2697 c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(2) |
2698 FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
2699 FW_EQ_OFLD_CMD_FETCHRO_F |
2700 FW_EQ_OFLD_CMD_IQID_V(iqid));
2701 c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN_V(2) |
2702 FW_EQ_OFLD_CMD_FBMAX_V(3) |
2703 FW_EQ_OFLD_CMD_CIDXFTHRESH_V(5) |
2704 FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002705 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2706
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002707 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002708 if (ret) {
2709 kfree(txq->q.sdesc);
2710 txq->q.sdesc = NULL;
2711 dma_free_coherent(adap->pdev_dev,
2712 nentries * sizeof(struct tx_desc),
2713 txq->q.desc, txq->q.phys_addr);
2714 txq->q.desc = NULL;
2715 return ret;
2716 }
2717
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302718 init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002719 txq->adap = adap;
2720 skb_queue_head_init(&txq->sendq);
2721 tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
2722 txq->full = 0;
2723 txq->mapping_err = 0;
2724 return 0;
2725}
2726
2727static void free_txq(struct adapter *adap, struct sge_txq *q)
2728{
Vipul Pandya52367a72012-09-26 02:39:38 +00002729 struct sge *s = &adap->sge;
2730
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002731 dma_free_coherent(adap->pdev_dev,
Vipul Pandya52367a72012-09-26 02:39:38 +00002732 q->size * sizeof(struct tx_desc) + s->stat_len,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002733 q->desc, q->phys_addr);
2734 q->cntxt_id = 0;
2735 q->sdesc = NULL;
2736 q->desc = NULL;
2737}
2738
2739static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
2740 struct sge_fl *fl)
2741{
Vipul Pandya52367a72012-09-26 02:39:38 +00002742 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002743 unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
2744
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002745 adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002746 t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
2747 rq->cntxt_id, fl_id, 0xffff);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002748 dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
2749 rq->desc, rq->phys_addr);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302750 napi_hash_del(&rq->napi);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002751 netif_napi_del(&rq->napi);
2752 rq->netdev = NULL;
2753 rq->cntxt_id = rq->abs_id = 0;
2754 rq->desc = NULL;
2755
2756 if (fl) {
2757 free_rx_bufs(adap, fl, fl->avail);
Vipul Pandya52367a72012-09-26 02:39:38 +00002758 dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002759 fl->desc, fl->addr);
2760 kfree(fl->sdesc);
2761 fl->sdesc = NULL;
2762 fl->cntxt_id = 0;
2763 fl->desc = NULL;
2764 }
2765}
2766
2767/**
Hariprasad Shenai5fa76692014-08-04 17:01:30 +05302768 * t4_free_ofld_rxqs - free a block of consecutive Rx queues
2769 * @adap: the adapter
2770 * @n: number of queues
2771 * @q: pointer to first queue
2772 *
2773 * Release the resources of a consecutive block of offload Rx queues.
2774 */
2775void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
2776{
2777 for ( ; n; n--, q++)
2778 if (q->rspq.desc)
2779 free_rspq_fl(adap, &q->rspq,
2780 q->fl.size ? &q->fl : NULL);
2781}
2782
2783/**
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002784 * t4_free_sge_resources - free SGE resources
2785 * @adap: the adapter
2786 *
2787 * Frees resources used by the SGE queue sets.
2788 */
2789void t4_free_sge_resources(struct adapter *adap)
2790{
2791 int i;
2792 struct sge_eth_rxq *eq = adap->sge.ethrxq;
2793 struct sge_eth_txq *etq = adap->sge.ethtxq;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002794
2795 /* clean up Ethernet Tx/Rx queues */
2796 for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
2797 if (eq->rspq.desc)
Hariprasad Shenai5fa76692014-08-04 17:01:30 +05302798 free_rspq_fl(adap, &eq->rspq,
2799 eq->fl.size ? &eq->fl : NULL);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002800 if (etq->q.desc) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002801 t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
2802 etq->q.cntxt_id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002803 free_tx_desc(adap, &etq->q, etq->q.in_use, true);
2804 kfree(etq->q.sdesc);
2805 free_txq(adap, &etq->q);
2806 }
2807 }
2808
2809 /* clean up RDMA and iSCSI Rx queues */
Hariprasad Shenai5fa76692014-08-04 17:01:30 +05302810 t4_free_ofld_rxqs(adap, adap->sge.ofldqsets, adap->sge.ofldrxq);
2811 t4_free_ofld_rxqs(adap, adap->sge.rdmaqs, adap->sge.rdmarxq);
2812 t4_free_ofld_rxqs(adap, adap->sge.rdmaciqs, adap->sge.rdmaciq);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002813
2814 /* clean up offload Tx queues */
2815 for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
2816 struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
2817
2818 if (q->q.desc) {
2819 tasklet_kill(&q->qresume_tsk);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002820 t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
2821 q->q.cntxt_id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002822 free_tx_desc(adap, &q->q, q->q.in_use, false);
2823 kfree(q->q.sdesc);
2824 __skb_queue_purge(&q->sendq);
2825 free_txq(adap, &q->q);
2826 }
2827 }
2828
2829 /* clean up control Tx queues */
2830 for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
2831 struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
2832
2833 if (cq->q.desc) {
2834 tasklet_kill(&cq->qresume_tsk);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002835 t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
2836 cq->q.cntxt_id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002837 __skb_queue_purge(&cq->sendq);
2838 free_txq(adap, &cq->q);
2839 }
2840 }
2841
2842 if (adap->sge.fw_evtq.desc)
2843 free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
2844
2845 if (adap->sge.intrq.desc)
2846 free_rspq_fl(adap, &adap->sge.intrq, NULL);
2847
2848 /* clear the reverse egress queue map */
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302849 memset(adap->sge.egr_map, 0,
2850 adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002851}
2852
2853void t4_sge_start(struct adapter *adap)
2854{
2855 adap->sge.ethtxq_rover = 0;
2856 mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2857 mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2858}
2859
2860/**
2861 * t4_sge_stop - disable SGE operation
2862 * @adap: the adapter
2863 *
2864 * Stop tasklets and timers associated with the DMA engine. Note that
2865 * this is effective only if measures have been taken to disable any HW
2866 * events that may restart them.
2867 */
2868void t4_sge_stop(struct adapter *adap)
2869{
2870 int i;
2871 struct sge *s = &adap->sge;
2872
2873 if (in_interrupt()) /* actions below require waiting */
2874 return;
2875
2876 if (s->rx_timer.function)
2877 del_timer_sync(&s->rx_timer);
2878 if (s->tx_timer.function)
2879 del_timer_sync(&s->tx_timer);
2880
2881 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
2882 struct sge_ofld_txq *q = &s->ofldtxq[i];
2883
2884 if (q->q.desc)
2885 tasklet_kill(&q->qresume_tsk);
2886 }
2887 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
2888 struct sge_ctrl_txq *cq = &s->ctrlq[i];
2889
2890 if (cq->q.desc)
2891 tasklet_kill(&cq->qresume_tsk);
2892 }
2893}
2894
2895/**
Hariprasad Shenai06640312015-01-13 15:19:25 +05302896 * t4_sge_init_soft - grab core SGE values needed by SGE code
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002897 * @adap: the adapter
2898 *
Hariprasad Shenai06640312015-01-13 15:19:25 +05302899 * We need to grab the SGE operating parameters that we need to have
2900 * in order to do our job and make sure we can live with them.
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002901 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002902
Vipul Pandya52367a72012-09-26 02:39:38 +00002903static int t4_sge_init_soft(struct adapter *adap)
2904{
2905 struct sge *s = &adap->sge;
2906 u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
2907 u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
2908 u32 ingress_rx_threshold;
2909
2910 /*
2911 * Verify that CPL messages are going to the Ingress Queue for
2912 * process_responses() and that only packet data is going to the
2913 * Free Lists.
2914 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302915 if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
2916 RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
Vipul Pandya52367a72012-09-26 02:39:38 +00002917 dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
2918 return -EINVAL;
2919 }
2920
2921 /*
2922 * Validate the Host Buffer Register Array indices that we want to
2923 * use ...
2924 *
2925 * XXX Note that we should really read through the Host Buffer Size
2926 * XXX register array and find the indices of the Buffer Sizes which
2927 * XXX meet our needs!
2928 */
2929 #define READ_FL_BUF(x) \
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302930 t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
Vipul Pandya52367a72012-09-26 02:39:38 +00002931
2932 fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
2933 fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
2934 fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
2935 fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
2936
Kumar Sanghvi92ddcc72014-03-13 20:50:46 +05302937 /* We only bother using the Large Page logic if the Large Page Buffer
2938 * is larger than our Page Size Buffer.
2939 */
2940 if (fl_large_pg <= fl_small_pg)
2941 fl_large_pg = 0;
2942
Vipul Pandya52367a72012-09-26 02:39:38 +00002943 #undef READ_FL_BUF
2944
Kumar Sanghvi92ddcc72014-03-13 20:50:46 +05302945 /* The Page Size Buffer must be exactly equal to our Page Size and the
2946 * Large Page Size Buffer should be 0 (per above) or a power of 2.
2947 */
Vipul Pandya52367a72012-09-26 02:39:38 +00002948 if (fl_small_pg != PAGE_SIZE ||
Kumar Sanghvi92ddcc72014-03-13 20:50:46 +05302949 (fl_large_pg & (fl_large_pg-1)) != 0) {
Vipul Pandya52367a72012-09-26 02:39:38 +00002950 dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
2951 fl_small_pg, fl_large_pg);
2952 return -EINVAL;
2953 }
2954 if (fl_large_pg)
2955 s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
2956
2957 if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
2958 fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
2959 dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
2960 fl_small_mtu, fl_large_mtu);
2961 return -EINVAL;
2962 }
2963
2964 /*
2965 * Retrieve our RX interrupt holdoff timer values and counter
2966 * threshold values from the SGE parameters.
2967 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302968 timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
2969 timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
2970 timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
Vipul Pandya52367a72012-09-26 02:39:38 +00002971 s->timer_val[0] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302972 TIMERVALUE0_G(timer_value_0_and_1));
Vipul Pandya52367a72012-09-26 02:39:38 +00002973 s->timer_val[1] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302974 TIMERVALUE1_G(timer_value_0_and_1));
Vipul Pandya52367a72012-09-26 02:39:38 +00002975 s->timer_val[2] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302976 TIMERVALUE2_G(timer_value_2_and_3));
Vipul Pandya52367a72012-09-26 02:39:38 +00002977 s->timer_val[3] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302978 TIMERVALUE3_G(timer_value_2_and_3));
Vipul Pandya52367a72012-09-26 02:39:38 +00002979 s->timer_val[4] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302980 TIMERVALUE4_G(timer_value_4_and_5));
Vipul Pandya52367a72012-09-26 02:39:38 +00002981 s->timer_val[5] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302982 TIMERVALUE5_G(timer_value_4_and_5));
Vipul Pandya52367a72012-09-26 02:39:38 +00002983
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302984 ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
2985 s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
2986 s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
2987 s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
2988 s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
Vipul Pandya52367a72012-09-26 02:39:38 +00002989
2990 return 0;
2991}
2992
Hariprasad Shenai06640312015-01-13 15:19:25 +05302993/**
2994 * t4_sge_init - initialize SGE
2995 * @adap: the adapter
2996 *
2997 * Perform low-level SGE code initialization needed every time after a
2998 * chip reset.
2999 */
Vipul Pandya52367a72012-09-26 02:39:38 +00003000int t4_sge_init(struct adapter *adap)
3001{
3002 struct sge *s = &adap->sge;
Hariprasad Shenaice8f4072014-11-07 17:06:30 +05303003 u32 sge_control, sge_control2, sge_conm_ctrl;
3004 unsigned int ingpadboundary, ingpackboundary;
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303005 int ret, egress_threshold;
Vipul Pandya52367a72012-09-26 02:39:38 +00003006
3007 /*
3008 * Ingress Padding Boundary and Egress Status Page Size are set up by
3009 * t4_fixup_host_params().
3010 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303011 sge_control = t4_read_reg(adap, SGE_CONTROL_A);
3012 s->pktshift = PKTSHIFT_G(sge_control);
3013 s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
Hariprasad Shenaice8f4072014-11-07 17:06:30 +05303014
3015 /* T4 uses a single control field to specify both the PCIe Padding and
3016 * Packing Boundary. T5 introduced the ability to specify these
3017 * separately. The actual Ingress Packet Data alignment boundary
3018 * within Packed Buffer Mode is the maximum of these two
3019 * specifications.
3020 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303021 ingpadboundary = 1 << (INGPADBOUNDARY_G(sge_control) +
3022 INGPADBOUNDARY_SHIFT_X);
Hariprasad Shenaice8f4072014-11-07 17:06:30 +05303023 if (is_t4(adap->params.chip)) {
3024 s->fl_align = ingpadboundary;
3025 } else {
3026 /* T5 has a different interpretation of one of the PCIe Packing
3027 * Boundary values.
3028 */
3029 sge_control2 = t4_read_reg(adap, SGE_CONTROL2_A);
3030 ingpackboundary = INGPACKBOUNDARY_G(sge_control2);
3031 if (ingpackboundary == INGPACKBOUNDARY_16B_X)
3032 ingpackboundary = 16;
3033 else
3034 ingpackboundary = 1 << (ingpackboundary +
3035 INGPACKBOUNDARY_SHIFT_X);
3036
3037 s->fl_align = max(ingpadboundary, ingpackboundary);
3038 }
Vipul Pandya52367a72012-09-26 02:39:38 +00003039
Hariprasad Shenai06640312015-01-13 15:19:25 +05303040 ret = t4_sge_init_soft(adap);
Vipul Pandya52367a72012-09-26 02:39:38 +00003041 if (ret < 0)
3042 return ret;
3043
3044 /*
3045 * A FL with <= fl_starve_thres buffers is starving and a periodic
3046 * timer will attempt to refill it. This needs to be larger than the
3047 * SGE's Egress Congestion Threshold. If it isn't, then we can get
3048 * stuck waiting for new packets while the SGE is waiting for us to
3049 * give it more Free List entries. (Note that the SGE's Egress
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303050 * Congestion Threshold is in units of 2 Free List pointers.) For T4,
3051 * there was only a single field to control this. For T5 there's the
3052 * original field which now only applies to Unpacked Mode Free List
3053 * buffers and a new field which only applies to Packed Mode Free List
3054 * buffers.
Vipul Pandya52367a72012-09-26 02:39:38 +00003055 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303056 sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303057 if (is_t4(adap->params.chip))
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303058 egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303059 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303060 egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303061 s->fl_starve_thres = 2*egress_threshold + 1;
Vipul Pandya52367a72012-09-26 02:39:38 +00003062
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05303063 t4_idma_monitor_init(adap, &s->idma_monitor);
3064
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003065 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
3066 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05303067
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003068 spin_lock_init(&s->intrq_lock);
Vipul Pandya52367a72012-09-26 02:39:38 +00003069
3070 return 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003071}