blob: 0fe04b482c382cf1e0c4333fe371d6a5e3efa6e1 [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 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530380void free_tx_desc(struct adapter *adap, struct sge_txq *q,
381 unsigned int n, bool unmap)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000382{
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{
Hariprasad Shenai632be192015-12-08 10:09:13 +0530409 int hw_cidx = ntohs(ACCESS_ONCE(q->stat->cidx));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000410 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{
525 if (q->pend_cred >= 8) {
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530526 u32 val = adap->params.arch.sge_fl_db;
527
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530528 if (is_t4(adap->params.chip))
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530529 val |= PIDX_V(q->pend_cred / 8);
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530530 else
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530531 val |= PIDX_T5_V(q->pend_cred / 8);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +0530532
533 /* Make sure all memory writes to the Free List queue are
534 * committed before we tell the hardware about them.
535 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000536 wmb();
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530537
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530538 /* If we don't have access to the new User Doorbell (T5+), use
539 * the old doorbell mechanism; otherwise use the new BAR2
540 * mechanism.
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530541 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530542 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530543 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
544 val | QID_V(q->cntxt_id));
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530545 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530546 writel(val | QID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530547 q->bar2_addr + SGE_UDB_KDOORBELL);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530548
549 /* This Write memory Barrier will force the write to
550 * the User Doorbell area to be flushed.
551 */
552 wmb();
553 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000554 q->pend_cred &= 7;
555 }
556}
557
558static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
559 dma_addr_t mapping)
560{
561 sd->page = pg;
562 sd->dma_addr = mapping; /* includes size low bits */
563}
564
565/**
566 * refill_fl - refill an SGE Rx buffer ring
567 * @adap: the adapter
568 * @q: the ring to refill
569 * @n: the number of new buffers to allocate
570 * @gfp: the gfp flags for the allocations
571 *
572 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
573 * allocated with the supplied gfp flags. The caller must assure that
574 * @n does not exceed the queue's capacity. If afterwards the queue is
575 * found critically low mark it as starving in the bitmap of starving FLs.
576 *
577 * Returns the number of buffers allocated.
578 */
579static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
580 gfp_t gfp)
581{
Vipul Pandya52367a72012-09-26 02:39:38 +0000582 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000583 struct page *pg;
584 dma_addr_t mapping;
585 unsigned int cred = q->avail;
586 __be64 *d = &q->desc[q->pidx];
587 struct rx_sw_desc *sd = &q->sdesc[q->pidx];
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530588 int node;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000589
Hariprasad Shenai5b377d12015-05-27 22:30:23 +0530590#ifdef CONFIG_DEBUG_FS
591 if (test_bit(q->cntxt_id - adap->sge.egr_start, adap->sge.blocked_fl))
592 goto out;
593#endif
594
Alexander Duyckaa9cd312014-11-11 09:26:42 -0800595 gfp |= __GFP_NOWARN;
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530596 node = dev_to_node(adap->pdev_dev);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000597
Vipul Pandya52367a72012-09-26 02:39:38 +0000598 if (s->fl_pg_order == 0)
599 goto alloc_small_pages;
600
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000601 /*
602 * Prefer large buffers
603 */
604 while (n) {
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530605 pg = alloc_pages_node(node, gfp | __GFP_COMP, s->fl_pg_order);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000606 if (unlikely(!pg)) {
607 q->large_alloc_failed++;
608 break; /* fall back to single pages */
609 }
610
611 mapping = dma_map_page(adap->pdev_dev, pg, 0,
Vipul Pandya52367a72012-09-26 02:39:38 +0000612 PAGE_SIZE << s->fl_pg_order,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000613 PCI_DMA_FROMDEVICE);
614 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
Vipul Pandya52367a72012-09-26 02:39:38 +0000615 __free_pages(pg, s->fl_pg_order);
Hariprasad Shenai70055dd2015-12-08 10:09:16 +0530616 q->mapping_err++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000617 goto out; /* do not try small pages for this error */
618 }
Vipul Pandya52367a72012-09-26 02:39:38 +0000619 mapping |= RX_LARGE_PG_BUF;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000620 *d++ = cpu_to_be64(mapping);
621
622 set_rx_sw_desc(sd, pg, mapping);
623 sd++;
624
625 q->avail++;
626 if (++q->pidx == q->size) {
627 q->pidx = 0;
628 sd = q->sdesc;
629 d = q->desc;
630 }
631 n--;
632 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000633
Vipul Pandya52367a72012-09-26 02:39:38 +0000634alloc_small_pages:
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000635 while (n--) {
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530636 pg = alloc_pages_node(node, gfp, 0);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000637 if (unlikely(!pg)) {
638 q->alloc_failed++;
639 break;
640 }
641
642 mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
643 PCI_DMA_FROMDEVICE);
644 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000645 put_page(pg);
Hariprasad Shenai70055dd2015-12-08 10:09:16 +0530646 q->mapping_err++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000647 goto out;
648 }
649 *d++ = cpu_to_be64(mapping);
650
651 set_rx_sw_desc(sd, pg, mapping);
652 sd++;
653
654 q->avail++;
655 if (++q->pidx == q->size) {
656 q->pidx = 0;
657 sd = q->sdesc;
658 d = q->desc;
659 }
660 }
661
662out: cred = q->avail - cred;
663 q->pend_cred += cred;
664 ring_fl_db(adap, q);
665
Hariprasad Shenaic098b022015-04-15 02:02:31 +0530666 if (unlikely(fl_starving(adap, q))) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000667 smp_wmb();
Hariprasad Shenai70055dd2015-12-08 10:09:16 +0530668 q->low++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000669 set_bit(q->cntxt_id - adap->sge.egr_start,
670 adap->sge.starving_fl);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000671 }
672
673 return cred;
674}
675
676static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
677{
678 refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
679 GFP_ATOMIC);
680}
681
682/**
683 * alloc_ring - allocate resources for an SGE descriptor ring
684 * @dev: the PCI device's core device
685 * @nelem: the number of descriptors
686 * @elem_size: the size of each descriptor
687 * @sw_size: the size of the SW state associated with each ring element
688 * @phys: the physical address of the allocated ring
689 * @metadata: address of the array holding the SW state for the ring
690 * @stat_size: extra space in HW ring for status information
Dimitris Michailidisad6bad32010-12-14 21:36:55 +0000691 * @node: preferred node for memory allocations
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000692 *
693 * Allocates resources for an SGE descriptor ring, such as Tx queues,
694 * free buffer lists, or response queues. Each SGE ring requires
695 * space for its HW descriptors plus, optionally, space for the SW state
696 * associated with each HW entry (the metadata). The function returns
697 * three values: the virtual address for the HW ring (the return value
698 * of the function), the bus address of the HW ring, and the address
699 * of the SW ring.
700 */
701static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
702 size_t sw_size, dma_addr_t *phys, void *metadata,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +0000703 size_t stat_size, int node)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000704{
705 size_t len = nelem * elem_size + stat_size;
706 void *s = NULL;
707 void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
708
709 if (!p)
710 return NULL;
711 if (sw_size) {
Dimitris Michailidisad6bad32010-12-14 21:36:55 +0000712 s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000713
714 if (!s) {
715 dma_free_coherent(dev, len, p, *phys);
716 return NULL;
717 }
718 }
719 if (metadata)
720 *(void **)metadata = s;
721 memset(p, 0, len);
722 return p;
723}
724
725/**
726 * sgl_len - calculates the size of an SGL of the given capacity
727 * @n: the number of SGL entries
728 *
729 * Calculates the number of flits needed for a scatter/gather list that
730 * can hold the given number of entries.
731 */
732static inline unsigned int sgl_len(unsigned int n)
733{
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530734 /* A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
735 * addresses. The DSGL Work Request starts off with a 32-bit DSGL
736 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
737 * repeated sequences of { Length[i], Length[i+1], Address[i],
738 * Address[i+1] } (this ensures that all addresses are on 64-bit
739 * boundaries). If N is even, then Length[N+1] should be set to 0 and
740 * Address[N+1] is omitted.
741 *
742 * The following calculation incorporates all of the above. It's
743 * somewhat hard to follow but, briefly: the "+2" accounts for the
744 * first two flits which include the DSGL header, Length0 and
745 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
746 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
747 * finally the "+((n-1)&1)" adds the one remaining flit needed if
748 * (n-1) is odd ...
749 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000750 n--;
751 return (3 * n) / 2 + (n & 1) + 2;
752}
753
754/**
755 * flits_to_desc - returns the num of Tx descriptors for the given flits
756 * @n: the number of flits
757 *
758 * Returns the number of Tx descriptors needed for the supplied number
759 * of flits.
760 */
761static inline unsigned int flits_to_desc(unsigned int n)
762{
763 BUG_ON(n > SGE_MAX_WR_LEN / 8);
764 return DIV_ROUND_UP(n, 8);
765}
766
767/**
768 * is_eth_imm - can an Ethernet packet be sent as immediate data?
769 * @skb: the packet
770 *
771 * Returns whether an Ethernet packet is small enough to fit as
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530772 * immediate data. Return value corresponds to headroom required.
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000773 */
774static inline int is_eth_imm(const struct sk_buff *skb)
775{
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530776 int hdrlen = skb_shinfo(skb)->gso_size ?
777 sizeof(struct cpl_tx_pkt_lso_core) : 0;
778
779 hdrlen += sizeof(struct cpl_tx_pkt);
780 if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
781 return hdrlen;
782 return 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000783}
784
785/**
786 * calc_tx_flits - calculate the number of flits for a packet Tx WR
787 * @skb: the packet
788 *
789 * Returns the number of flits needed for a Tx WR for the given Ethernet
790 * packet, including the needed WR and CPL headers.
791 */
792static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
793{
794 unsigned int flits;
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530795 int hdrlen = is_eth_imm(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000796
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530797 /* If the skb is small enough, we can pump it out as a work request
798 * with only immediate data. In that case we just have to have the
799 * TX Packet header plus the skb data in the Work Request.
800 */
801
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530802 if (hdrlen)
803 return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000804
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530805 /* Otherwise, we're going to have to construct a Scatter gather list
806 * of the skb body and fragments. We also include the flits necessary
807 * for the TX Packet Work Request and CPL. We always have a firmware
808 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
809 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
810 * message or, if we're doing a Large Send Offload, an LSO CPL message
811 * with an embedded TX Packet Write CPL message.
812 */
Hariprasad Shenaifd1754f2015-09-08 16:25:39 +0530813 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000814 if (skb_shinfo(skb)->gso_size)
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530815 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
816 sizeof(struct cpl_tx_pkt_lso_core) +
817 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
818 else
819 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
820 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000821 return flits;
822}
823
824/**
825 * calc_tx_descs - calculate the number of Tx descriptors for a packet
826 * @skb: the packet
827 *
828 * Returns the number of Tx descriptors needed for the given Ethernet
829 * packet, including the needed WR and CPL headers.
830 */
831static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
832{
833 return flits_to_desc(calc_tx_flits(skb));
834}
835
836/**
837 * write_sgl - populate a scatter/gather list for a packet
838 * @skb: the packet
839 * @q: the Tx queue we are writing into
840 * @sgl: starting location for writing the SGL
841 * @end: points right after the end of the SGL
842 * @start: start offset into skb main-body data to include in the SGL
843 * @addr: the list of bus addresses for the SGL elements
844 *
845 * Generates a gather list for the buffers that make up a packet.
846 * The caller must provide adequate space for the SGL that will be written.
847 * The SGL includes all of the packet's page fragments and the data in its
848 * main body except for the first @start bytes. @sgl must be 16-byte
849 * aligned and within a Tx descriptor with available space. @end points
850 * right after the end of the SGL but does not account for any potential
851 * wrap around, i.e., @end > @sgl.
852 */
853static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
854 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
855 const dma_addr_t *addr)
856{
857 unsigned int i, len;
858 struct ulptx_sge_pair *to;
859 const struct skb_shared_info *si = skb_shinfo(skb);
860 unsigned int nfrags = si->nr_frags;
861 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
862
863 len = skb_headlen(skb) - start;
864 if (likely(len)) {
865 sgl->len0 = htonl(len);
866 sgl->addr0 = cpu_to_be64(addr[0] + start);
867 nfrags++;
868 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000869 sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000870 sgl->addr0 = cpu_to_be64(addr[1]);
871 }
872
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800873 sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
874 ULPTX_NSGE_V(nfrags));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000875 if (likely(--nfrags == 0))
876 return;
877 /*
878 * Most of the complexity below deals with the possibility we hit the
879 * end of the queue in the middle of writing the SGL. For this case
880 * only we create the SGL in a temporary buffer and then copy it.
881 */
882 to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
883
884 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000885 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
886 to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000887 to->addr[0] = cpu_to_be64(addr[i]);
888 to->addr[1] = cpu_to_be64(addr[++i]);
889 }
890 if (nfrags) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000891 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000892 to->len[1] = cpu_to_be32(0);
893 to->addr[0] = cpu_to_be64(addr[i + 1]);
894 }
895 if (unlikely((u8 *)end > (u8 *)q->stat)) {
896 unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
897
898 if (likely(part0))
899 memcpy(sgl->sge, buf, part0);
900 part1 = (u8 *)end - (u8 *)q->stat;
901 memcpy(q->desc, (u8 *)buf + part0, part1);
902 end = (void *)q->desc + part1;
903 }
904 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
Joe Perches64699332012-06-04 12:44:16 +0000905 *end = 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000906}
907
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530908/* This function copies 64 byte coalesced work request to
909 * memory mapped BAR2 space. For coalesced WR SGE fetches
910 * data from the FIFO instead of from Host.
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000911 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530912static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000913{
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530914 int count = 8;
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000915
916 while (count) {
917 writeq(*src, dst);
918 src++;
919 dst++;
920 count--;
921 }
922}
923
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000924/**
925 * ring_tx_db - check and potentially ring a Tx queue's doorbell
926 * @adap: the adapter
927 * @q: the Tx queue
928 * @n: number of new descriptors to give to HW
929 *
930 * Ring the doorbel for a Tx queue.
931 */
932static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
933{
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +0530934 /* Make sure that all writes to the TX Descriptors are committed
935 * before we tell the hardware about them.
936 */
937 wmb();
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530938
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530939 /* If we don't have access to the new User Doorbell (T5+), use the old
940 * doorbell mechanism; otherwise use the new BAR2 mechanism.
941 */
942 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530943 u32 val = PIDX_V(n);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530944 unsigned long flags;
945
946 /* For T4 we need to participate in the Doorbell Recovery
947 * mechanism.
948 */
949 spin_lock_irqsave(&q->db_lock, flags);
950 if (!q->db_disabled)
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530951 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
952 QID_V(q->cntxt_id) | val);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530953 else
954 q->db_pidx_inc += n;
955 q->db_pidx = q->pidx;
956 spin_unlock_irqrestore(&q->db_lock, flags);
957 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530958 u32 val = PIDX_T5_V(n);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530959
960 /* T4 and later chips share the same PIDX field offset within
961 * the doorbell, but T5 and later shrank the field in order to
962 * gain a bit for Doorbell Priority. The field was absurdly
963 * large in the first place (14 bits) so we just use the T5
964 * and later limits and warn if a Queue ID is too large.
965 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530966 WARN_ON(val & DBPRIO_F);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530967
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530968 /* If we're only writing a single TX Descriptor and we can use
969 * Inferred QID registers, we can use the Write Combining
970 * Gather Buffer; otherwise we use the simple doorbell.
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530971 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530972 if (n == 1 && q->bar2_qid == 0) {
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530973 int index = (q->pidx
974 ? (q->pidx - 1)
975 : (q->size - 1));
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530976 u64 *wr = (u64 *)&q->desc[index];
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530977
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530978 cxgb_pio_copy((u64 __iomem *)
979 (q->bar2_addr + SGE_UDB_WCDOORBELL),
980 wr);
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000981 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530982 writel(val | QID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530983 q->bar2_addr + SGE_UDB_KDOORBELL);
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000984 }
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530985
986 /* This Write Memory Barrier will force the write to the User
987 * Doorbell area to be flushed. This is needed to prevent
988 * writes on different CPUs for the same queue from hitting
989 * the adapter out of order. This is required when some Work
990 * Requests take the Write Combine Gather Buffer path (user
991 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
992 * take the traditional path where we simply increment the
993 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
994 * hardware DMA read the actual Work Request.
995 */
996 wmb();
997 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000998}
999
1000/**
1001 * inline_tx_skb - inline a packet's data into Tx descriptors
1002 * @skb: the packet
1003 * @q: the Tx queue where the packet will be inlined
1004 * @pos: starting position in the Tx queue where to inline the packet
1005 *
1006 * Inline a packet's contents directly into Tx descriptors, starting at
1007 * the given position within the Tx DMA ring.
1008 * Most of the complexity of this operation is dealing with wrap arounds
1009 * in the middle of the packet we want to inline.
1010 */
1011static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
1012 void *pos)
1013{
1014 u64 *p;
1015 int left = (void *)q->stat - pos;
1016
1017 if (likely(skb->len <= left)) {
1018 if (likely(!skb->data_len))
1019 skb_copy_from_linear_data(skb, pos, skb->len);
1020 else
1021 skb_copy_bits(skb, 0, pos, skb->len);
1022 pos += skb->len;
1023 } else {
1024 skb_copy_bits(skb, 0, pos, left);
1025 skb_copy_bits(skb, left, q->desc, skb->len - left);
1026 pos = (void *)q->desc + (skb->len - left);
1027 }
1028
1029 /* 0-pad to multiple of 16 */
1030 p = PTR_ALIGN(pos, 8);
1031 if ((uintptr_t)p & 8)
1032 *p = 0;
1033}
1034
Hariprasad Shenai8d0557d2015-12-08 10:09:15 +05301035static void *inline_tx_skb_header(const struct sk_buff *skb,
1036 const struct sge_txq *q, void *pos,
1037 int length)
1038{
1039 u64 *p;
1040 int left = (void *)q->stat - pos;
1041
1042 if (likely(length <= left)) {
1043 memcpy(pos, skb->data, length);
1044 pos += length;
1045 } else {
1046 memcpy(pos, skb->data, left);
1047 memcpy(q->desc, skb->data + left, length - left);
1048 pos = (void *)q->desc + (length - left);
1049 }
1050 /* 0-pad to multiple of 16 */
1051 p = PTR_ALIGN(pos, 8);
1052 if ((uintptr_t)p & 8) {
1053 *p = 0;
1054 return p + 1;
1055 }
1056 return p;
1057}
1058
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001059/*
1060 * Figure out what HW csum a packet wants and return the appropriate control
1061 * bits.
1062 */
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301063static u64 hwcsum(enum chip_type chip, const struct sk_buff *skb)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001064{
1065 int csum_type;
1066 const struct iphdr *iph = ip_hdr(skb);
1067
1068 if (iph->version == 4) {
1069 if (iph->protocol == IPPROTO_TCP)
1070 csum_type = TX_CSUM_TCPIP;
1071 else if (iph->protocol == IPPROTO_UDP)
1072 csum_type = TX_CSUM_UDPIP;
1073 else {
1074nocsum: /*
1075 * unknown protocol, disable HW csum
1076 * and hope a bad packet is detected
1077 */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301078 return TXPKT_L4CSUM_DIS_F;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001079 }
1080 } else {
1081 /*
1082 * this doesn't work with extension headers
1083 */
1084 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
1085
1086 if (ip6h->nexthdr == IPPROTO_TCP)
1087 csum_type = TX_CSUM_TCPIP6;
1088 else if (ip6h->nexthdr == IPPROTO_UDP)
1089 csum_type = TX_CSUM_UDPIP6;
1090 else
1091 goto nocsum;
1092 }
1093
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301094 if (likely(csum_type >= TX_CSUM_TCPIP)) {
1095 u64 hdr_len = TXPKT_IPHDR_LEN_V(skb_network_header_len(skb));
1096 int eth_hdr_len = skb_network_offset(skb) - ETH_HLEN;
1097
1098 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1099 hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len);
1100 else
1101 hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len);
1102 return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len;
1103 } else {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001104 int start = skb_transport_offset(skb);
1105
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301106 return TXPKT_CSUM_TYPE_V(csum_type) |
1107 TXPKT_CSUM_START_V(start) |
1108 TXPKT_CSUM_LOC_V(start + skb->csum_offset);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001109 }
1110}
1111
1112static void eth_txq_stop(struct sge_eth_txq *q)
1113{
1114 netif_tx_stop_queue(q->txq);
1115 q->q.stops++;
1116}
1117
1118static inline void txq_advance(struct sge_txq *q, unsigned int n)
1119{
1120 q->in_use += n;
1121 q->pidx += n;
1122 if (q->pidx >= q->size)
1123 q->pidx -= q->size;
1124}
1125
Varun Prakash84a200b2015-03-24 19:14:46 +05301126#ifdef CONFIG_CHELSIO_T4_FCOE
1127static inline int
1128cxgb_fcoe_offload(struct sk_buff *skb, struct adapter *adap,
1129 const struct port_info *pi, u64 *cntrl)
1130{
1131 const struct cxgb_fcoe *fcoe = &pi->fcoe;
1132
1133 if (!(fcoe->flags & CXGB_FCOE_ENABLED))
1134 return 0;
1135
1136 if (skb->protocol != htons(ETH_P_FCOE))
1137 return 0;
1138
1139 skb_reset_mac_header(skb);
1140 skb->mac_len = sizeof(struct ethhdr);
1141
1142 skb_set_network_header(skb, skb->mac_len);
1143 skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
1144
1145 if (!cxgb_fcoe_sof_eof_supported(adap, skb))
1146 return -ENOTSUPP;
1147
1148 /* FC CRC offload */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301149 *cntrl = TXPKT_CSUM_TYPE_V(TX_CSUM_FCOE) |
1150 TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F |
1151 TXPKT_CSUM_START_V(CXGB_FCOE_TXPKT_CSUM_START) |
1152 TXPKT_CSUM_END_V(CXGB_FCOE_TXPKT_CSUM_END) |
1153 TXPKT_CSUM_LOC_V(CXGB_FCOE_TXPKT_CSUM_END);
Varun Prakash84a200b2015-03-24 19:14:46 +05301154 return 0;
1155}
1156#endif /* CONFIG_CHELSIO_T4_FCOE */
1157
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001158/**
1159 * t4_eth_xmit - add a packet to an Ethernet Tx queue
1160 * @skb: the packet
1161 * @dev: the egress net device
1162 *
1163 * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
1164 */
1165netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1166{
Anish Bhatt397665d2015-07-17 13:12:33 -07001167 u32 wr_mid, ctrl0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001168 u64 cntrl, *end;
1169 int qidx, credits;
1170 unsigned int flits, ndesc;
1171 struct adapter *adap;
1172 struct sge_eth_txq *q;
1173 const struct port_info *pi;
1174 struct fw_eth_tx_pkt_wr *wr;
1175 struct cpl_tx_pkt_core *cpl;
1176 const struct skb_shared_info *ssi;
1177 dma_addr_t addr[MAX_SKB_FRAGS + 1];
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301178 bool immediate = false;
Hariprasad Shenai637d3e92015-05-05 14:59:56 +05301179 int len, max_pkt_len;
Varun Prakash84a200b2015-03-24 19:14:46 +05301180#ifdef CONFIG_CHELSIO_T4_FCOE
1181 int err;
1182#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001183
1184 /*
1185 * The chip min packet length is 10 octets but play safe and reject
1186 * anything shorter than an Ethernet header.
1187 */
1188 if (unlikely(skb->len < ETH_HLEN)) {
Eric W. Biedermana7525192014-03-15 16:29:49 -07001189out_free: dev_kfree_skb_any(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001190 return NETDEV_TX_OK;
1191 }
1192
Hariprasad Shenai637d3e92015-05-05 14:59:56 +05301193 /* Discard the packet if the length is greater than mtu */
1194 max_pkt_len = ETH_HLEN + dev->mtu;
Hariprasad Shenai8d09e6b2016-07-28 13:28:57 +05301195 if (skb_vlan_tagged(skb))
Hariprasad Shenai637d3e92015-05-05 14:59:56 +05301196 max_pkt_len += VLAN_HLEN;
1197 if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
1198 goto out_free;
1199
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001200 pi = netdev_priv(dev);
1201 adap = pi->adapter;
1202 qidx = skb_get_queue_mapping(skb);
1203 q = &adap->sge.ethtxq[qidx + pi->first_qset];
1204
1205 reclaim_completed_tx(adap, &q->q, true);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301206 cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
Varun Prakash84a200b2015-03-24 19:14:46 +05301207
1208#ifdef CONFIG_CHELSIO_T4_FCOE
1209 err = cxgb_fcoe_offload(skb, adap, pi, &cntrl);
1210 if (unlikely(err == -ENOTSUPP))
1211 goto out_free;
1212#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001213
1214 flits = calc_tx_flits(skb);
1215 ndesc = flits_to_desc(flits);
1216 credits = txq_avail(&q->q) - ndesc;
1217
1218 if (unlikely(credits < 0)) {
1219 eth_txq_stop(q);
1220 dev_err(adap->pdev_dev,
1221 "%s: Tx ring %u full while queue awake!\n",
1222 dev->name, qidx);
1223 return NETDEV_TX_BUSY;
1224 }
1225
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301226 if (is_eth_imm(skb))
1227 immediate = true;
1228
1229 if (!immediate &&
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001230 unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
1231 q->mapping_err++;
1232 goto out_free;
1233 }
1234
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301235 wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001236 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1237 eth_txq_stop(q);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301238 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001239 }
1240
1241 wr = (void *)&q->q.desc[q->q.pidx];
1242 wr->equiq_to_len16 = htonl(wr_mid);
1243 wr->r3 = cpu_to_be64(0);
1244 end = (u64 *)wr + flits;
1245
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301246 len = immediate ? skb->len : 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001247 ssi = skb_shinfo(skb);
1248 if (ssi->gso_size) {
Dimitris Michailidis625ac6a2010-08-02 13:19:18 +00001249 struct cpl_tx_pkt_lso *lso = (void *)wr;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001250 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1251 int l3hdr_len = skb_network_header_len(skb);
1252 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1253
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301254 len += sizeof(*lso);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301255 wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
1256 FW_WR_IMMDLEN_V(len));
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301257 lso->c.lso_ctrl = htonl(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
1258 LSO_FIRST_SLICE_F | LSO_LAST_SLICE_F |
1259 LSO_IPV6_V(v6) |
1260 LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
1261 LSO_IPHDR_LEN_V(l3hdr_len / 4) |
1262 LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
Dimitris Michailidis625ac6a2010-08-02 13:19:18 +00001263 lso->c.ipid_ofst = htons(0);
1264 lso->c.mss = htons(ssi->gso_size);
1265 lso->c.seqno_offset = htonl(0);
Hariprasad Shenai7207c0d2014-10-09 05:48:45 +05301266 if (is_t4(adap->params.chip))
1267 lso->c.len = htonl(skb->len);
1268 else
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301269 lso->c.len = htonl(LSO_T5_XFER_SIZE_V(skb->len));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001270 cpl = (void *)(lso + 1);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301271
1272 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1273 cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
1274 else
1275 cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
1276
1277 cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
1278 TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
1279 TXPKT_IPHDR_LEN_V(l3hdr_len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001280 q->tso++;
1281 q->tx_cso += ssi->gso_segs;
1282 } else {
Kumar Sanghvica71de62014-03-13 20:50:50 +05301283 len += sizeof(*cpl);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301284 wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
1285 FW_WR_IMMDLEN_V(len));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001286 cpl = (void *)(wr + 1);
1287 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301288 cntrl = hwcsum(adap->params.chip, skb) |
1289 TXPKT_IPCSUM_DIS_F;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001290 q->tx_cso++;
Varun Prakash84a200b2015-03-24 19:14:46 +05301291 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001292 }
1293
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001294 if (skb_vlan_tag_present(skb)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001295 q->vlan_ins++;
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301296 cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
Varun Prakash84a200b2015-03-24 19:14:46 +05301297#ifdef CONFIG_CHELSIO_T4_FCOE
1298 if (skb->protocol == htons(ETH_P_FCOE))
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301299 cntrl |= TXPKT_VLAN_V(
Varun Prakash84a200b2015-03-24 19:14:46 +05301300 ((skb->priority & 0x7) << VLAN_PRIO_SHIFT));
1301#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001302 }
1303
Anish Bhatt397665d2015-07-17 13:12:33 -07001304 ctrl0 = TXPKT_OPCODE_V(CPL_TX_PKT_XT) | TXPKT_INTF_V(pi->tx_chan) |
1305 TXPKT_PF_V(adap->pf);
1306#ifdef CONFIG_CHELSIO_T4_DCB
1307 if (is_t4(adap->params.chip))
1308 ctrl0 |= TXPKT_OVLAN_IDX_V(q->dcb_prio);
1309 else
1310 ctrl0 |= TXPKT_T5_OVLAN_IDX_V(q->dcb_prio);
1311#endif
1312 cpl->ctrl0 = htonl(ctrl0);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001313 cpl->pack = htons(0);
1314 cpl->len = htons(skb->len);
1315 cpl->ctrl1 = cpu_to_be64(cntrl);
1316
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301317 if (immediate) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001318 inline_tx_skb(skb, &q->q, cpl + 1);
Eric W. Biedermana7525192014-03-15 16:29:49 -07001319 dev_consume_skb_any(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001320 } else {
1321 int last_desc;
1322
1323 write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
1324 addr);
1325 skb_orphan(skb);
1326
1327 last_desc = q->q.pidx + ndesc - 1;
1328 if (last_desc >= q->q.size)
1329 last_desc -= q->q.size;
1330 q->q.sdesc[last_desc].skb = skb;
1331 q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
1332 }
1333
1334 txq_advance(&q->q, ndesc);
1335
1336 ring_tx_db(adap, &q->q, ndesc);
1337 return NETDEV_TX_OK;
1338}
1339
1340/**
1341 * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
1342 * @q: the SGE control Tx queue
1343 *
1344 * This is a variant of reclaim_completed_tx() that is used for Tx queues
1345 * that send only immediate data (presently just the control queues) and
1346 * thus do not have any sk_buffs to release.
1347 */
1348static inline void reclaim_completed_tx_imm(struct sge_txq *q)
1349{
Hariprasad Shenai632be192015-12-08 10:09:13 +05301350 int hw_cidx = ntohs(ACCESS_ONCE(q->stat->cidx));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001351 int reclaim = hw_cidx - q->cidx;
1352
1353 if (reclaim < 0)
1354 reclaim += q->size;
1355
1356 q->in_use -= reclaim;
1357 q->cidx = hw_cidx;
1358}
1359
1360/**
1361 * is_imm - check whether a packet can be sent as immediate data
1362 * @skb: the packet
1363 *
1364 * Returns true if a packet can be sent as a WR with immediate data.
1365 */
1366static inline int is_imm(const struct sk_buff *skb)
1367{
1368 return skb->len <= MAX_CTRL_WR_LEN;
1369}
1370
1371/**
1372 * ctrlq_check_stop - check if a control queue is full and should stop
1373 * @q: the queue
1374 * @wr: most recent WR written to the queue
1375 *
1376 * Check if a control queue has become full and should be stopped.
1377 * We clean up control queue descriptors very lazily, only when we are out.
1378 * If the queue is still full after reclaiming any completed descriptors
1379 * we suspend it and have the last WR wake it up.
1380 */
1381static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
1382{
1383 reclaim_completed_tx_imm(&q->q);
1384 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301385 wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001386 q->q.stops++;
1387 q->full = 1;
1388 }
1389}
1390
1391/**
1392 * ctrl_xmit - send a packet through an SGE control Tx queue
1393 * @q: the control queue
1394 * @skb: the packet
1395 *
1396 * Send a packet through an SGE control Tx queue. Packets sent through
1397 * a control queue must fit entirely as immediate data.
1398 */
1399static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
1400{
1401 unsigned int ndesc;
1402 struct fw_wr_hdr *wr;
1403
1404 if (unlikely(!is_imm(skb))) {
1405 WARN_ON(1);
1406 dev_kfree_skb(skb);
1407 return NET_XMIT_DROP;
1408 }
1409
1410 ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
1411 spin_lock(&q->sendq.lock);
1412
1413 if (unlikely(q->full)) {
1414 skb->priority = ndesc; /* save for restart */
1415 __skb_queue_tail(&q->sendq, skb);
1416 spin_unlock(&q->sendq.lock);
1417 return NET_XMIT_CN;
1418 }
1419
1420 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1421 inline_tx_skb(skb, &q->q, wr);
1422
1423 txq_advance(&q->q, ndesc);
1424 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
1425 ctrlq_check_stop(q, wr);
1426
1427 ring_tx_db(q->adap, &q->q, ndesc);
1428 spin_unlock(&q->sendq.lock);
1429
1430 kfree_skb(skb);
1431 return NET_XMIT_SUCCESS;
1432}
1433
1434/**
1435 * restart_ctrlq - restart a suspended control queue
1436 * @data: the control queue to restart
1437 *
1438 * Resumes transmission on a suspended Tx control queue.
1439 */
1440static void restart_ctrlq(unsigned long data)
1441{
1442 struct sk_buff *skb;
1443 unsigned int written = 0;
1444 struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
1445
1446 spin_lock(&q->sendq.lock);
1447 reclaim_completed_tx_imm(&q->q);
1448 BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
1449
1450 while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
1451 struct fw_wr_hdr *wr;
1452 unsigned int ndesc = skb->priority; /* previously saved */
1453
Hariprasad Shenaia4011fd2015-08-12 16:55:07 +05301454 written += ndesc;
1455 /* Write descriptors and free skbs outside the lock to limit
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001456 * wait times. q->full is still set so new skbs will be queued.
1457 */
Hariprasad Shenaia4011fd2015-08-12 16:55:07 +05301458 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1459 txq_advance(&q->q, ndesc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001460 spin_unlock(&q->sendq.lock);
1461
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001462 inline_tx_skb(skb, &q->q, wr);
1463 kfree_skb(skb);
1464
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001465 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1466 unsigned long old = q->q.stops;
1467
1468 ctrlq_check_stop(q, wr);
1469 if (q->q.stops != old) { /* suspended anew */
1470 spin_lock(&q->sendq.lock);
1471 goto ringdb;
1472 }
1473 }
1474 if (written > 16) {
1475 ring_tx_db(q->adap, &q->q, written);
1476 written = 0;
1477 }
1478 spin_lock(&q->sendq.lock);
1479 }
1480 q->full = 0;
1481ringdb: if (written)
1482 ring_tx_db(q->adap, &q->q, written);
1483 spin_unlock(&q->sendq.lock);
1484}
1485
1486/**
1487 * t4_mgmt_tx - send a management message
1488 * @adap: the adapter
1489 * @skb: the packet containing the management message
1490 *
1491 * Send a management message through control queue 0.
1492 */
1493int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
1494{
1495 int ret;
1496
1497 local_bh_disable();
1498 ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
1499 local_bh_enable();
1500 return ret;
1501}
1502
1503/**
1504 * is_ofld_imm - check whether a packet can be sent as immediate data
1505 * @skb: the packet
1506 *
1507 * Returns true if a packet can be sent as an offload WR with immediate
1508 * data. We currently use the same limit as for Ethernet packets.
1509 */
1510static inline int is_ofld_imm(const struct sk_buff *skb)
1511{
1512 return skb->len <= MAX_IMM_TX_PKT_LEN;
1513}
1514
1515/**
1516 * calc_tx_flits_ofld - calculate # of flits for an offload packet
1517 * @skb: the packet
1518 *
1519 * Returns the number of flits needed for the given offload packet.
1520 * These packets are already fully constructed and no additional headers
1521 * will be added.
1522 */
1523static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
1524{
1525 unsigned int flits, cnt;
1526
1527 if (is_ofld_imm(skb))
1528 return DIV_ROUND_UP(skb->len, 8);
1529
1530 flits = skb_transport_offset(skb) / 8U; /* headers */
1531 cnt = skb_shinfo(skb)->nr_frags;
Li RongQing15dd16c2013-06-03 22:11:16 +00001532 if (skb_tail_pointer(skb) != skb_transport_header(skb))
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001533 cnt++;
1534 return flits + sgl_len(cnt);
1535}
1536
1537/**
1538 * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
1539 * @adap: the adapter
1540 * @q: the queue to stop
1541 *
1542 * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
1543 * inability to map packets. A periodic timer attempts to restart
1544 * queues so marked.
1545 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301546static void txq_stop_maperr(struct sge_uld_txq *q)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001547{
1548 q->mapping_err++;
1549 q->q.stops++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001550 set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
1551 q->adap->sge.txq_maperr);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001552}
1553
1554/**
1555 * ofldtxq_stop - stop an offload Tx queue that has become full
1556 * @q: the queue to stop
1557 * @skb: the packet causing the queue to become full
1558 *
1559 * Stops an offload Tx queue that has become full and modifies the packet
1560 * being written to request a wakeup.
1561 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301562static void ofldtxq_stop(struct sge_uld_txq *q, struct sk_buff *skb)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001563{
1564 struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
1565
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301566 wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001567 q->q.stops++;
1568 q->full = 1;
1569}
1570
1571/**
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301572 * service_ofldq - service/restart a suspended offload queue
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001573 * @q: the offload queue
1574 *
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301575 * Services an offload Tx queue by moving packets from its Pending Send
1576 * Queue to the Hardware TX ring. The function starts and ends with the
1577 * Send Queue locked, but drops the lock while putting the skb at the
1578 * head of the Send Queue onto the Hardware TX Ring. Dropping the lock
1579 * allows more skbs to be added to the Send Queue by other threads.
1580 * The packet being processed at the head of the Pending Send Queue is
1581 * left on the queue in case we experience DMA Mapping errors, etc.
1582 * and need to give up and restart later.
1583 *
1584 * service_ofldq() can be thought of as a task which opportunistically
1585 * uses other threads execution contexts. We use the Offload Queue
1586 * boolean "service_ofldq_running" to make sure that only one instance
1587 * is ever running at a time ...
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001588 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301589static void service_ofldq(struct sge_uld_txq *q)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001590{
Hariprasad Shenai8d0557d2015-12-08 10:09:15 +05301591 u64 *pos, *before, *end;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001592 int credits;
1593 struct sk_buff *skb;
Hariprasad Shenai8d0557d2015-12-08 10:09:15 +05301594 struct sge_txq *txq;
1595 unsigned int left;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001596 unsigned int written = 0;
1597 unsigned int flits, ndesc;
1598
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301599 /* If another thread is currently in service_ofldq() processing the
1600 * Pending Send Queue then there's nothing to do. Otherwise, flag
1601 * that we're doing the work and continue. Examining/modifying
1602 * the Offload Queue boolean "service_ofldq_running" must be done
1603 * while holding the Pending Send Queue Lock.
1604 */
1605 if (q->service_ofldq_running)
1606 return;
1607 q->service_ofldq_running = true;
1608
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001609 while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301610 /* We drop the lock while we're working with the skb at the
1611 * head of the Pending Send Queue. This allows more skbs to
1612 * be added to the Pending Send Queue while we're working on
1613 * this one. We don't need to lock to guard the TX Ring
1614 * updates because only one thread of execution is ever
1615 * allowed into service_ofldq() at a time.
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001616 */
1617 spin_unlock(&q->sendq.lock);
1618
1619 reclaim_completed_tx(q->adap, &q->q, false);
1620
1621 flits = skb->priority; /* previously saved */
1622 ndesc = flits_to_desc(flits);
1623 credits = txq_avail(&q->q) - ndesc;
1624 BUG_ON(credits < 0);
1625 if (unlikely(credits < TXQ_STOP_THRES))
1626 ofldtxq_stop(q, skb);
1627
1628 pos = (u64 *)&q->q.desc[q->q.pidx];
1629 if (is_ofld_imm(skb))
1630 inline_tx_skb(skb, &q->q, pos);
1631 else if (map_skb(q->adap->pdev_dev, skb,
1632 (dma_addr_t *)skb->head)) {
1633 txq_stop_maperr(q);
1634 spin_lock(&q->sendq.lock);
1635 break;
1636 } else {
1637 int last_desc, hdr_len = skb_transport_offset(skb);
1638
Hariprasad Shenai8d0557d2015-12-08 10:09:15 +05301639 /* The WR headers may not fit within one descriptor.
1640 * So we need to deal with wrap-around here.
1641 */
1642 before = (u64 *)pos;
1643 end = (u64 *)pos + flits;
1644 txq = &q->q;
1645 pos = (void *)inline_tx_skb_header(skb, &q->q,
1646 (void *)pos,
1647 hdr_len);
1648 if (before > (u64 *)pos) {
1649 left = (u8 *)end - (u8 *)txq->stat;
1650 end = (void *)txq->desc + left;
1651 }
1652
1653 /* If current position is already at the end of the
1654 * ofld queue, reset the current to point to
1655 * start of the queue and update the end ptr as well.
1656 */
1657 if (pos == (u64 *)txq->stat) {
1658 left = (u8 *)end - (u8 *)txq->stat;
1659 end = (void *)txq->desc + left;
1660 pos = (void *)txq->desc;
1661 }
1662
1663 write_sgl(skb, &q->q, (void *)pos,
1664 end, hdr_len,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001665 (dma_addr_t *)skb->head);
1666#ifdef CONFIG_NEED_DMA_MAP_STATE
1667 skb->dev = q->adap->port[0];
1668 skb->destructor = deferred_unmap_destructor;
1669#endif
1670 last_desc = q->q.pidx + ndesc - 1;
1671 if (last_desc >= q->q.size)
1672 last_desc -= q->q.size;
1673 q->q.sdesc[last_desc].skb = skb;
1674 }
1675
1676 txq_advance(&q->q, ndesc);
1677 written += ndesc;
1678 if (unlikely(written > 32)) {
1679 ring_tx_db(q->adap, &q->q, written);
1680 written = 0;
1681 }
1682
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301683 /* Reacquire the Pending Send Queue Lock so we can unlink the
1684 * skb we've just successfully transferred to the TX Ring and
1685 * loop for the next skb which may be at the head of the
1686 * Pending Send Queue.
1687 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001688 spin_lock(&q->sendq.lock);
1689 __skb_unlink(skb, &q->sendq);
1690 if (is_ofld_imm(skb))
1691 kfree_skb(skb);
1692 }
1693 if (likely(written))
1694 ring_tx_db(q->adap, &q->q, written);
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301695
1696 /*Indicate that no thread is processing the Pending Send Queue
1697 * currently.
1698 */
1699 q->service_ofldq_running = false;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001700}
1701
1702/**
1703 * ofld_xmit - send a packet through an offload queue
1704 * @q: the Tx offload queue
1705 * @skb: the packet
1706 *
1707 * Send an offload packet through an SGE offload queue.
1708 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301709static int ofld_xmit(struct sge_uld_txq *q, struct sk_buff *skb)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001710{
1711 skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
1712 spin_lock(&q->sendq.lock);
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301713
1714 /* Queue the new skb onto the Offload Queue's Pending Send Queue. If
1715 * that results in this new skb being the only one on the queue, start
1716 * servicing it. If there are other skbs already on the list, then
1717 * either the queue is currently being processed or it's been stopped
1718 * for some reason and it'll be restarted at a later time. Restart
1719 * paths are triggered by events like experiencing a DMA Mapping Error
1720 * or filling the Hardware TX Ring.
1721 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001722 __skb_queue_tail(&q->sendq, skb);
1723 if (q->sendq.qlen == 1)
1724 service_ofldq(q);
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301725
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001726 spin_unlock(&q->sendq.lock);
1727 return NET_XMIT_SUCCESS;
1728}
1729
1730/**
1731 * restart_ofldq - restart a suspended offload queue
1732 * @data: the offload queue to restart
1733 *
1734 * Resumes transmission on a suspended Tx offload queue.
1735 */
1736static void restart_ofldq(unsigned long data)
1737{
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301738 struct sge_uld_txq *q = (struct sge_uld_txq *)data;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001739
1740 spin_lock(&q->sendq.lock);
1741 q->full = 0; /* the queue actually is completely empty now */
1742 service_ofldq(q);
1743 spin_unlock(&q->sendq.lock);
1744}
1745
1746/**
1747 * skb_txq - return the Tx queue an offload packet should use
1748 * @skb: the packet
1749 *
1750 * Returns the Tx queue an offload packet should use as indicated by bits
1751 * 1-15 in the packet's queue_mapping.
1752 */
1753static inline unsigned int skb_txq(const struct sk_buff *skb)
1754{
1755 return skb->queue_mapping >> 1;
1756}
1757
1758/**
1759 * is_ctrl_pkt - return whether an offload packet is a control packet
1760 * @skb: the packet
1761 *
1762 * Returns whether an offload packet should use an OFLD or a CTRL
1763 * Tx queue as indicated by bit 0 in the packet's queue_mapping.
1764 */
1765static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
1766{
1767 return skb->queue_mapping & 1;
1768}
1769
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301770static inline int uld_send(struct adapter *adap, struct sk_buff *skb,
1771 unsigned int tx_uld_type)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001772{
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301773 struct sge_uld_txq_info *txq_info;
1774 struct sge_uld_txq *txq;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001775 unsigned int idx = skb_txq(skb);
1776
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301777 txq_info = adap->sge.uld_txq_info[tx_uld_type];
1778 txq = &txq_info->uldtxq[idx];
1779
Kumar Sanghvi4fe44dd2014-02-18 17:56:11 +05301780 if (unlikely(is_ctrl_pkt(skb))) {
1781 /* Single ctrl queue is a requirement for LE workaround path */
1782 if (adap->tids.nsftids)
1783 idx = 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001784 return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
Kumar Sanghvi4fe44dd2014-02-18 17:56:11 +05301785 }
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301786 return ofld_xmit(txq, skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001787}
1788
1789/**
1790 * t4_ofld_send - send an offload packet
1791 * @adap: the adapter
1792 * @skb: the packet
1793 *
1794 * Sends an offload packet. We use the packet queue_mapping to select the
1795 * appropriate Tx queue as follows: bit 0 indicates whether the packet
1796 * should be sent as regular or control, bits 1-15 select the queue.
1797 */
1798int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
1799{
1800 int ret;
1801
1802 local_bh_disable();
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301803 ret = uld_send(adap, skb, CXGB4_TX_OFLD);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001804 local_bh_enable();
1805 return ret;
1806}
1807
1808/**
1809 * cxgb4_ofld_send - send an offload packet
1810 * @dev: the net device
1811 * @skb: the packet
1812 *
1813 * Sends an offload packet. This is an exported version of @t4_ofld_send,
1814 * intended for ULDs.
1815 */
1816int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
1817{
1818 return t4_ofld_send(netdev2adap(dev), skb);
1819}
1820EXPORT_SYMBOL(cxgb4_ofld_send);
1821
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301822/**
1823 * t4_crypto_send - send crypto packet
1824 * @adap: the adapter
1825 * @skb: the packet
1826 *
1827 * Sends crypto packet. We use the packet queue_mapping to select the
1828 * appropriate Tx queue as follows: bit 0 indicates whether the packet
1829 * should be sent as regular or control, bits 1-15 select the queue.
1830 */
1831static int t4_crypto_send(struct adapter *adap, struct sk_buff *skb)
1832{
1833 int ret;
1834
1835 local_bh_disable();
1836 ret = uld_send(adap, skb, CXGB4_TX_CRYPTO);
1837 local_bh_enable();
1838 return ret;
1839}
1840
1841/**
1842 * cxgb4_crypto_send - send crypto packet
1843 * @dev: the net device
1844 * @skb: the packet
1845 *
1846 * Sends crypto packet. This is an exported version of @t4_crypto_send,
1847 * intended for ULDs.
1848 */
1849int cxgb4_crypto_send(struct net_device *dev, struct sk_buff *skb)
1850{
1851 return t4_crypto_send(netdev2adap(dev), skb);
1852}
1853EXPORT_SYMBOL(cxgb4_crypto_send);
1854
Ian Campbelle91b0f22011-10-19 23:01:46 +00001855static inline void copy_frags(struct sk_buff *skb,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001856 const struct pkt_gl *gl, unsigned int offset)
1857{
Ian Campbelle91b0f22011-10-19 23:01:46 +00001858 int i;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001859
1860 /* usually there's just one frag */
Ian Campbelle91b0f22011-10-19 23:01:46 +00001861 __skb_fill_page_desc(skb, 0, gl->frags[0].page,
1862 gl->frags[0].offset + offset,
1863 gl->frags[0].size - offset);
1864 skb_shinfo(skb)->nr_frags = gl->nfrags;
1865 for (i = 1; i < gl->nfrags; i++)
1866 __skb_fill_page_desc(skb, i, gl->frags[i].page,
1867 gl->frags[i].offset,
1868 gl->frags[i].size);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001869
1870 /* get a reference to the last page, we don't own it */
Ian Campbelle91b0f22011-10-19 23:01:46 +00001871 get_page(gl->frags[gl->nfrags - 1].page);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001872}
1873
1874/**
1875 * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
1876 * @gl: the gather list
1877 * @skb_len: size of sk_buff main body if it carries fragments
1878 * @pull_len: amount of data to move to the sk_buff's main body
1879 *
1880 * Builds an sk_buff from the given packet gather list. Returns the
1881 * sk_buff or %NULL if sk_buff allocation failed.
1882 */
1883struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
1884 unsigned int skb_len, unsigned int pull_len)
1885{
1886 struct sk_buff *skb;
1887
1888 /*
1889 * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
1890 * size, which is expected since buffers are at least PAGE_SIZEd.
1891 * In this case packets up to RX_COPY_THRES have only one fragment.
1892 */
1893 if (gl->tot_len <= RX_COPY_THRES) {
1894 skb = dev_alloc_skb(gl->tot_len);
1895 if (unlikely(!skb))
1896 goto out;
1897 __skb_put(skb, gl->tot_len);
1898 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1899 } else {
1900 skb = dev_alloc_skb(skb_len);
1901 if (unlikely(!skb))
1902 goto out;
1903 __skb_put(skb, pull_len);
1904 skb_copy_to_linear_data(skb, gl->va, pull_len);
1905
Ian Campbelle91b0f22011-10-19 23:01:46 +00001906 copy_frags(skb, gl, pull_len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001907 skb->len = gl->tot_len;
1908 skb->data_len = skb->len - pull_len;
1909 skb->truesize += skb->data_len;
1910 }
1911out: return skb;
1912}
1913EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
1914
1915/**
1916 * t4_pktgl_free - free a packet gather list
1917 * @gl: the gather list
1918 *
1919 * Releases the pages of a packet gather list. We do not own the last
1920 * page on the list and do not free it.
1921 */
Roland Dreierde498c82010-04-21 08:59:17 +00001922static void t4_pktgl_free(const struct pkt_gl *gl)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001923{
1924 int n;
Ian Campbelle91b0f22011-10-19 23:01:46 +00001925 const struct page_frag *p;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001926
1927 for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
1928 put_page(p->page);
1929}
1930
1931/*
1932 * Process an MPS trace packet. Give it an unused protocol number so it won't
1933 * be delivered to anyone and send it to the stack for capture.
1934 */
1935static noinline int handle_trace_pkt(struct adapter *adap,
1936 const struct pkt_gl *gl)
1937{
1938 struct sk_buff *skb;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001939
1940 skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
1941 if (unlikely(!skb)) {
1942 t4_pktgl_free(gl);
1943 return 0;
1944 }
1945
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301946 if (is_t4(adap->params.chip))
Santosh Rastapur0a57a532013-03-14 05:08:49 +00001947 __skb_pull(skb, sizeof(struct cpl_trace_pkt));
1948 else
1949 __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
1950
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001951 skb_reset_mac_header(skb);
1952 skb->protocol = htons(0xffff);
1953 skb->dev = adap->port[0];
1954 netif_receive_skb(skb);
1955 return 0;
1956}
1957
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05301958/**
1959 * cxgb4_sgetim_to_hwtstamp - convert sge time stamp to hw time stamp
1960 * @adap: the adapter
1961 * @hwtstamps: time stamp structure to update
1962 * @sgetstamp: 60bit iqe timestamp
1963 *
1964 * Every ingress queue entry has the 60-bit timestamp, convert that timestamp
1965 * which is in Core Clock ticks into ktime_t and assign it
1966 **/
1967static void cxgb4_sgetim_to_hwtstamp(struct adapter *adap,
1968 struct skb_shared_hwtstamps *hwtstamps,
1969 u64 sgetstamp)
1970{
1971 u64 ns;
1972 u64 tmp = (sgetstamp * 1000 * 1000 + adap->params.vpd.cclk / 2);
1973
1974 ns = div_u64(tmp, adap->params.vpd.cclk);
1975
1976 memset(hwtstamps, 0, sizeof(*hwtstamps));
1977 hwtstamps->hwtstamp = ns_to_ktime(ns);
1978}
1979
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001980static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1981 const struct cpl_rx_pkt *pkt)
1982{
Vipul Pandya52367a72012-09-26 02:39:38 +00001983 struct adapter *adapter = rxq->rspq.adap;
1984 struct sge *s = &adapter->sge;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05301985 struct port_info *pi;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001986 int ret;
1987 struct sk_buff *skb;
1988
1989 skb = napi_get_frags(&rxq->rspq.napi);
1990 if (unlikely(!skb)) {
1991 t4_pktgl_free(gl);
1992 rxq->stats.rx_drops++;
1993 return;
1994 }
1995
Vipul Pandya52367a72012-09-26 02:39:38 +00001996 copy_frags(skb, gl, s->pktshift);
1997 skb->len = gl->tot_len - s->pktshift;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001998 skb->data_len = skb->len;
1999 skb->truesize += skb->data_len;
2000 skb->ip_summed = CHECKSUM_UNNECESSARY;
2001 skb_record_rx_queue(skb, rxq->rspq.idx);
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302002 pi = netdev_priv(skb->dev);
2003 if (pi->rxtstamp)
2004 cxgb4_sgetim_to_hwtstamp(adapter, skb_hwtstamps(skb),
2005 gl->sgetstamp);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07002006 if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
Tom Herbert82649892013-12-17 23:23:29 -08002007 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
2008 PKT_HASH_TYPE_L3);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002009
2010 if (unlikely(pkt->vlan_ex)) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002011 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002012 rxq->stats.vlan_ex++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002013 }
2014 ret = napi_gro_frags(&rxq->rspq.napi);
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00002015 if (ret == GRO_HELD)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002016 rxq->stats.lro_pkts++;
2017 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
2018 rxq->stats.lro_merged++;
2019 rxq->stats.pkts++;
2020 rxq->stats.rx_cso++;
2021}
2022
2023/**
2024 * t4_ethrx_handler - process an ingress ethernet packet
2025 * @q: the response queue that received the packet
2026 * @rsp: the response queue descriptor holding the RX_PKT message
2027 * @si: the gather list of packet fragments
2028 *
2029 * Process an ingress ethernet packet and deliver it to the stack.
2030 */
2031int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
2032 const struct pkt_gl *si)
2033{
2034 bool csum_ok;
2035 struct sk_buff *skb;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002036 const struct cpl_rx_pkt *pkt;
2037 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
Vipul Pandya52367a72012-09-26 02:39:38 +00002038 struct sge *s = &q->adap->sge;
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302039 int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
Santosh Rastapur0a57a532013-03-14 05:08:49 +00002040 CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
Arjun V8eb9f2f2017-01-04 19:04:20 +05302041 u16 err_vec;
Varun Prakash84a200b2015-03-24 19:14:46 +05302042 struct port_info *pi;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002043
Santosh Rastapur0a57a532013-03-14 05:08:49 +00002044 if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002045 return handle_trace_pkt(q->adap, si);
2046
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07002047 pkt = (const struct cpl_rx_pkt *)rsp;
Arjun V8eb9f2f2017-01-04 19:04:20 +05302048 /* Compressed error vector is enabled for T6 only */
2049 if (q->adap->params.tp.rx_pkt_encap)
2050 err_vec = T6_COMPR_RXERR_VEC_G(be16_to_cpu(pkt->err_vec));
2051 else
2052 err_vec = be16_to_cpu(pkt->err_vec);
2053
2054 csum_ok = pkt->csum_calc && !err_vec &&
Hariprasad Shenaicca28222014-05-07 18:01:03 +05302055 (q->netdev->features & NETIF_F_RXCSUM);
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08002056 if ((pkt->l2info & htonl(RXF_TCP_F)) &&
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302057 !(cxgb_poll_busy_polling(q)) &&
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002058 (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
2059 do_gro(rxq, si, pkt);
2060 return 0;
2061 }
2062
2063 skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
2064 if (unlikely(!skb)) {
2065 t4_pktgl_free(si);
2066 rxq->stats.rx_drops++;
2067 return 0;
2068 }
2069
Vipul Pandya52367a72012-09-26 02:39:38 +00002070 __skb_pull(skb, s->pktshift); /* remove ethernet header padding */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002071 skb->protocol = eth_type_trans(skb, q->netdev);
2072 skb_record_rx_queue(skb, q->idx);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07002073 if (skb->dev->features & NETIF_F_RXHASH)
Tom Herbert82649892013-12-17 23:23:29 -08002074 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
2075 PKT_HASH_TYPE_L3);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07002076
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002077 rxq->stats.pkts++;
2078
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302079 pi = netdev_priv(skb->dev);
2080 if (pi->rxtstamp)
2081 cxgb4_sgetim_to_hwtstamp(q->adap, skb_hwtstamps(skb),
2082 si->sgetstamp);
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08002083 if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00002084 if (!pkt->ip_frag) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002085 skb->ip_summed = CHECKSUM_UNNECESSARY;
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00002086 rxq->stats.rx_cso++;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08002087 } else if (pkt->l2info & htonl(RXF_IP_F)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002088 __sum16 c = (__force __sum16)pkt->csum;
2089 skb->csum = csum_unfold(c);
2090 skb->ip_summed = CHECKSUM_COMPLETE;
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00002091 rxq->stats.rx_cso++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002092 }
Varun Prakash84a200b2015-03-24 19:14:46 +05302093 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002094 skb_checksum_none_assert(skb);
Varun Prakash84a200b2015-03-24 19:14:46 +05302095#ifdef CONFIG_CHELSIO_T4_FCOE
2096#define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
2097 RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
2098
Varun Prakash84a200b2015-03-24 19:14:46 +05302099 if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
2100 if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
2101 (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
Arjun V8eb9f2f2017-01-04 19:04:20 +05302102 if (q->adap->params.tp.rx_pkt_encap)
2103 csum_ok = err_vec &
2104 T6_COMPR_RXERR_SUM_F;
2105 else
2106 csum_ok = err_vec & RXERR_CSUM_F;
2107 if (!csum_ok)
Varun Prakash84a200b2015-03-24 19:14:46 +05302108 skb->ip_summed = CHECKSUM_UNNECESSARY;
2109 }
2110 }
2111
2112#undef CPL_RX_PKT_FLAGS
2113#endif /* CONFIG_CHELSIO_T4_FCOE */
2114 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002115
2116 if (unlikely(pkt->vlan_ex)) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002117 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002118 rxq->stats.vlan_ex++;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00002119 }
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302120 skb_mark_napi_id(skb, &q->napi);
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00002121 netif_receive_skb(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002122 return 0;
2123}
2124
2125/**
2126 * restore_rx_bufs - put back a packet's Rx buffers
2127 * @si: the packet gather list
2128 * @q: the SGE free list
2129 * @frags: number of FL buffers to restore
2130 *
2131 * Puts back on an FL the Rx buffers associated with @si. The buffers
2132 * have already been unmapped and are left unmapped, we mark them so to
2133 * prevent further unmapping attempts.
2134 *
2135 * This function undoes a series of @unmap_rx_buf calls when we find out
2136 * that the current packet can't be processed right away afterall and we
2137 * need to come back to it later. This is a very rare event and there's
2138 * no effort to make this particularly efficient.
2139 */
2140static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
2141 int frags)
2142{
2143 struct rx_sw_desc *d;
2144
2145 while (frags--) {
2146 if (q->cidx == 0)
2147 q->cidx = q->size - 1;
2148 else
2149 q->cidx--;
2150 d = &q->sdesc[q->cidx];
2151 d->page = si->frags[frags].page;
2152 d->dma_addr |= RX_UNMAPPED_BUF;
2153 q->avail++;
2154 }
2155}
2156
2157/**
2158 * is_new_response - check if a response is newly written
2159 * @r: the response descriptor
2160 * @q: the response queue
2161 *
2162 * Returns true if a response descriptor contains a yet unprocessed
2163 * response.
2164 */
2165static inline bool is_new_response(const struct rsp_ctrl *r,
2166 const struct sge_rspq *q)
2167{
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302168 return (r->type_gen >> RSPD_GEN_S) == q->gen;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002169}
2170
2171/**
2172 * rspq_next - advance to the next entry in a response queue
2173 * @q: the queue
2174 *
2175 * Updates the state of a response queue to advance it to the next entry.
2176 */
2177static inline void rspq_next(struct sge_rspq *q)
2178{
2179 q->cur_desc = (void *)q->cur_desc + q->iqe_len;
2180 if (unlikely(++q->cidx == q->size)) {
2181 q->cidx = 0;
2182 q->gen ^= 1;
2183 q->cur_desc = q->desc;
2184 }
2185}
2186
2187/**
2188 * process_responses - process responses from an SGE response queue
2189 * @q: the ingress queue to process
2190 * @budget: how many responses can be processed in this round
2191 *
2192 * Process responses from an SGE response queue up to the supplied budget.
2193 * Responses include received packets as well as control messages from FW
2194 * or HW.
2195 *
2196 * Additionally choose the interrupt holdoff time for the next interrupt
2197 * on this queue. If the system is under memory shortage use a fairly
2198 * long delay to help recovery.
2199 */
2200static int process_responses(struct sge_rspq *q, int budget)
2201{
2202 int ret, rsp_type;
2203 int budget_left = budget;
2204 const struct rsp_ctrl *rc;
2205 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
Vipul Pandya52367a72012-09-26 02:39:38 +00002206 struct adapter *adapter = q->adap;
2207 struct sge *s = &adapter->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002208
2209 while (likely(budget_left)) {
2210 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
Varun Prakash2337ba42016-02-14 23:02:41 +05302211 if (!is_new_response(rc, q)) {
2212 if (q->flush_handler)
2213 q->flush_handler(q);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002214 break;
Varun Prakash2337ba42016-02-14 23:02:41 +05302215 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002216
Alexander Duyck019be1c2015-04-08 18:49:29 -07002217 dma_rmb();
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302218 rsp_type = RSPD_TYPE_G(rc->type_gen);
2219 if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
Ian Campbelle91b0f22011-10-19 23:01:46 +00002220 struct page_frag *fp;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002221 struct pkt_gl si;
2222 const struct rx_sw_desc *rsd;
2223 u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
2224
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302225 if (len & RSPD_NEWBUF_F) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002226 if (likely(q->offset > 0)) {
2227 free_rx_bufs(q->adap, &rxq->fl, 1);
2228 q->offset = 0;
2229 }
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302230 len = RSPD_LEN_G(len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002231 }
2232 si.tot_len = len;
2233
2234 /* gather packet fragments */
2235 for (frags = 0, fp = si.frags; ; frags++, fp++) {
2236 rsd = &rxq->fl.sdesc[rxq->fl.cidx];
Vipul Pandya52367a72012-09-26 02:39:38 +00002237 bufsz = get_buf_size(adapter, rsd);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002238 fp->page = rsd->page;
Ian Campbelle91b0f22011-10-19 23:01:46 +00002239 fp->offset = q->offset;
2240 fp->size = min(bufsz, len);
2241 len -= fp->size;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002242 if (!len)
2243 break;
2244 unmap_rx_buf(q->adap, &rxq->fl);
2245 }
2246
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302247 si.sgetstamp = SGE_TIMESTAMP_G(
2248 be64_to_cpu(rc->last_flit));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002249 /*
2250 * Last buffer remains mapped so explicitly make it
2251 * coherent for CPU access.
2252 */
2253 dma_sync_single_for_cpu(q->adap->pdev_dev,
2254 get_buf_addr(rsd),
Ian Campbelle91b0f22011-10-19 23:01:46 +00002255 fp->size, DMA_FROM_DEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002256
2257 si.va = page_address(si.frags[0].page) +
Ian Campbelle91b0f22011-10-19 23:01:46 +00002258 si.frags[0].offset;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002259 prefetch(si.va);
2260
2261 si.nfrags = frags + 1;
2262 ret = q->handler(q, q->cur_desc, &si);
2263 if (likely(ret == 0))
Vipul Pandya52367a72012-09-26 02:39:38 +00002264 q->offset += ALIGN(fp->size, s->fl_align);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002265 else
2266 restore_rx_bufs(&si, &rxq->fl, frags);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302267 } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002268 ret = q->handler(q, q->cur_desc, NULL);
2269 } else {
2270 ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
2271 }
2272
2273 if (unlikely(ret)) {
2274 /* couldn't process descriptor, back off for recovery */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302275 q->next_intr_params = QINTR_TIMER_IDX_V(NOMEM_TMR_IDX);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002276 break;
2277 }
2278
2279 rspq_next(q);
2280 budget_left--;
2281 }
2282
Hariprasad Shenaida08e422016-03-01 17:19:32 +05302283 if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 16)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002284 __refill_fl(q->adap, &rxq->fl);
2285 return budget - budget_left;
2286}
2287
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302288#ifdef CONFIG_NET_RX_BUSY_POLL
2289int cxgb_busy_poll(struct napi_struct *napi)
2290{
2291 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
2292 unsigned int params, work_done;
2293 u32 val;
2294
2295 if (!cxgb_poll_lock_poll(q))
2296 return LL_FLUSH_BUSY;
2297
2298 work_done = process_responses(q, 4);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302299 params = QINTR_TIMER_IDX_V(TIMERREG_COUNTER0_X) | QINTR_CNT_EN_V(1);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302300 q->next_intr_params = params;
2301 val = CIDXINC_V(work_done) | SEINTARM_V(params);
2302
2303 /* If we don't have access to the new User GTS (T5+), use the old
2304 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2305 */
2306 if (unlikely(!q->bar2_addr))
2307 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
2308 val | INGRESSQID_V((u32)q->cntxt_id));
2309 else {
2310 writel(val | INGRESSQID_V(q->bar2_qid),
2311 q->bar2_addr + SGE_UDB_GTS);
2312 wmb();
2313 }
2314
2315 cxgb_poll_unlock_poll(q);
2316 return work_done;
2317}
2318#endif /* CONFIG_NET_RX_BUSY_POLL */
2319
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002320/**
2321 * napi_rx_handler - the NAPI handler for Rx processing
2322 * @napi: the napi instance
2323 * @budget: how many packets we can process in this round
2324 *
2325 * Handler for new data events when using NAPI. This does not need any
2326 * locking or protection from interrupts as data interrupts are off at
2327 * this point and other adapter interrupts do not interfere (the latter
2328 * in not a concern at all with MSI-X as non-data interrupts then have
2329 * a separate handler).
2330 */
2331static int napi_rx_handler(struct napi_struct *napi, int budget)
2332{
2333 unsigned int params;
2334 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302335 int work_done;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302336 u32 val;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002337
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302338 if (!cxgb_poll_lock_napi(q))
2339 return budget;
2340
2341 work_done = process_responses(q, budget);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002342 if (likely(work_done < budget)) {
Hariprasad Shenaie553ec32014-09-26 00:23:55 +05302343 int timer_index;
2344
Hariprasad Shenai812787b2015-12-23 11:29:56 +05302345 napi_complete_done(napi, work_done);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302346 timer_index = QINTR_TIMER_IDX_G(q->next_intr_params);
Hariprasad Shenaie553ec32014-09-26 00:23:55 +05302347
2348 if (q->adaptive_rx) {
2349 if (work_done > max(timer_pkt_quota[timer_index],
2350 MIN_NAPI_WORK))
2351 timer_index = (timer_index + 1);
2352 else
2353 timer_index = timer_index - 1;
2354
2355 timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302356 q->next_intr_params =
2357 QINTR_TIMER_IDX_V(timer_index) |
2358 QINTR_CNT_EN_V(0);
Hariprasad Shenaie553ec32014-09-26 00:23:55 +05302359 params = q->next_intr_params;
2360 } else {
2361 params = q->next_intr_params;
2362 q->next_intr_params = q->intr_params;
2363 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002364 } else
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302365 params = QINTR_TIMER_IDX_V(7);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002366
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302367 val = CIDXINC_V(work_done) | SEINTARM_V(params);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302368
2369 /* If we don't have access to the new User GTS (T5+), use the old
2370 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2371 */
2372 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302373 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
2374 val | INGRESSQID_V((u32)q->cntxt_id));
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302375 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302376 writel(val | INGRESSQID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302377 q->bar2_addr + SGE_UDB_GTS);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302378 wmb();
2379 }
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302380 cxgb_poll_unlock_napi(q);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002381 return work_done;
2382}
2383
2384/*
2385 * The MSI-X interrupt handler for an SGE response queue.
2386 */
2387irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
2388{
2389 struct sge_rspq *q = cookie;
2390
2391 napi_schedule(&q->napi);
2392 return IRQ_HANDLED;
2393}
2394
2395/*
2396 * Process the indirect interrupt entries in the interrupt queue and kick off
2397 * NAPI for each queue that has generated an entry.
2398 */
2399static unsigned int process_intrq(struct adapter *adap)
2400{
2401 unsigned int credits;
2402 const struct rsp_ctrl *rc;
2403 struct sge_rspq *q = &adap->sge.intrq;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302404 u32 val;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002405
2406 spin_lock(&adap->sge.intrq_lock);
2407 for (credits = 0; ; credits++) {
2408 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
2409 if (!is_new_response(rc, q))
2410 break;
2411
Alexander Duyck019be1c2015-04-08 18:49:29 -07002412 dma_rmb();
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302413 if (RSPD_TYPE_G(rc->type_gen) == RSPD_TYPE_INTR_X) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002414 unsigned int qid = ntohl(rc->pldbuflen_qid);
2415
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002416 qid -= adap->sge.ingr_start;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002417 napi_schedule(&adap->sge.ingr_map[qid]->napi);
2418 }
2419
2420 rspq_next(q);
2421 }
2422
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302423 val = CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302424
2425 /* If we don't have access to the new User GTS (T5+), use the old
2426 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2427 */
2428 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302429 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
2430 val | INGRESSQID_V(q->cntxt_id));
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302431 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302432 writel(val | INGRESSQID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302433 q->bar2_addr + SGE_UDB_GTS);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302434 wmb();
2435 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002436 spin_unlock(&adap->sge.intrq_lock);
2437 return credits;
2438}
2439
2440/*
2441 * The MSI interrupt handler, which handles data events from SGE response queues
2442 * as well as error and other async events as they all use the same MSI vector.
2443 */
2444static irqreturn_t t4_intr_msi(int irq, void *cookie)
2445{
2446 struct adapter *adap = cookie;
2447
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +05302448 if (adap->flags & MASTER_PF)
2449 t4_slow_intr_handler(adap);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002450 process_intrq(adap);
2451 return IRQ_HANDLED;
2452}
2453
2454/*
2455 * Interrupt handler for legacy INTx interrupts.
2456 * Handles data events from SGE response queues as well as error and other
2457 * async events as they all use the same interrupt line.
2458 */
2459static irqreturn_t t4_intr_intx(int irq, void *cookie)
2460{
2461 struct adapter *adap = cookie;
2462
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302463 t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +05302464 if (((adap->flags & MASTER_PF) && t4_slow_intr_handler(adap)) |
2465 process_intrq(adap))
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002466 return IRQ_HANDLED;
2467 return IRQ_NONE; /* probably shared interrupt */
2468}
2469
2470/**
2471 * t4_intr_handler - select the top-level interrupt handler
2472 * @adap: the adapter
2473 *
2474 * Selects the top-level interrupt handler based on the type of interrupts
2475 * (MSI-X, MSI, or INTx).
2476 */
2477irq_handler_t t4_intr_handler(struct adapter *adap)
2478{
2479 if (adap->flags & USING_MSIX)
2480 return t4_sge_intr_msix;
2481 if (adap->flags & USING_MSI)
2482 return t4_intr_msi;
2483 return t4_intr_intx;
2484}
2485
2486static void sge_rx_timer_cb(unsigned long data)
2487{
2488 unsigned long m;
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302489 unsigned int i;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002490 struct adapter *adap = (struct adapter *)data;
2491 struct sge *s = &adap->sge;
2492
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302493 for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002494 for (m = s->starving_fl[i]; m; m &= m - 1) {
2495 struct sge_eth_rxq *rxq;
2496 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
2497 struct sge_fl *fl = s->egr_map[id];
2498
2499 clear_bit(id, s->starving_fl);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002500 smp_mb__after_atomic();
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002501
Hariprasad Shenaic098b022015-04-15 02:02:31 +05302502 if (fl_starving(adap, fl)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002503 rxq = container_of(fl, struct sge_eth_rxq, fl);
2504 if (napi_reschedule(&rxq->rspq.napi))
2505 fl->starving++;
2506 else
2507 set_bit(id, s->starving_fl);
2508 }
2509 }
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302510 /* The remainder of the SGE RX Timer Callback routine is dedicated to
2511 * global Master PF activities like checking for chip ingress stalls,
2512 * etc.
2513 */
2514 if (!(adap->flags & MASTER_PF))
2515 goto done;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002516
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302517 t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002518
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302519done:
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002520 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
2521}
2522
2523static void sge_tx_timer_cb(unsigned long data)
2524{
2525 unsigned long m;
2526 unsigned int i, budget;
2527 struct adapter *adap = (struct adapter *)data;
2528 struct sge *s = &adap->sge;
2529
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302530 for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002531 for (m = s->txq_maperr[i]; m; m &= m - 1) {
2532 unsigned long id = __ffs(m) + i * BITS_PER_LONG;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302533 struct sge_uld_txq *txq = s->egr_map[id];
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002534
2535 clear_bit(id, s->txq_maperr);
2536 tasklet_schedule(&txq->qresume_tsk);
2537 }
2538
2539 budget = MAX_TIMER_TX_RECLAIM;
2540 i = s->ethtxq_rover;
2541 do {
2542 struct sge_eth_txq *q = &s->ethtxq[i];
2543
2544 if (q->q.in_use &&
2545 time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
2546 __netif_tx_trylock(q->txq)) {
2547 int avail = reclaimable(&q->q);
2548
2549 if (avail) {
2550 if (avail > budget)
2551 avail = budget;
2552
2553 free_tx_desc(adap, &q->q, avail, true);
2554 q->q.in_use -= avail;
2555 budget -= avail;
2556 }
2557 __netif_tx_unlock(q->txq);
2558 }
2559
2560 if (++i >= s->ethqsets)
2561 i = 0;
2562 } while (budget && i != s->ethtxq_rover);
2563 s->ethtxq_rover = i;
2564 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
2565}
2566
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302567/**
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302568 * bar2_address - return the BAR2 address for an SGE Queue's Registers
2569 * @adapter: the adapter
2570 * @qid: the SGE Queue ID
2571 * @qtype: the SGE Queue Type (Egress or Ingress)
2572 * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302573 *
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302574 * Returns the BAR2 address for the SGE Queue Registers associated with
2575 * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
2576 * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
2577 * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
2578 * Registers are supported (e.g. the Write Combining Doorbell Buffer).
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302579 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302580static void __iomem *bar2_address(struct adapter *adapter,
2581 unsigned int qid,
2582 enum t4_bar2_qtype qtype,
2583 unsigned int *pbar2_qid)
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302584{
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302585 u64 bar2_qoffset;
2586 int ret;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302587
Linus Torvaldse0456712015-06-24 16:49:49 -07002588 ret = t4_bar2_sge_qregs(adapter, qid, qtype, 0,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302589 &bar2_qoffset, pbar2_qid);
2590 if (ret)
2591 return NULL;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302592
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302593 return adapter->bar2 + bar2_qoffset;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302594}
2595
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302596/* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
2597 * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
2598 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002599int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
2600 struct net_device *dev, int intr_idx,
Varun Prakash2337ba42016-02-14 23:02:41 +05302601 struct sge_fl *fl, rspq_handler_t hnd,
2602 rspq_flush_handler_t flush_hnd, int cong)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002603{
2604 int ret, flsz = 0;
2605 struct fw_iq_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002606 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002607 struct port_info *pi = netdev_priv(dev);
2608
2609 /* Size needs to be multiple of 16, including status entry. */
2610 iq->size = roundup(iq->size, 16);
2611
2612 iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
Hariprasad Shenai0ac5b702015-12-23 11:29:55 +05302613 &iq->phys_addr, NULL, 0,
2614 dev_to_node(adap->pdev_dev));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002615 if (!iq->desc)
2616 return -ENOMEM;
2617
2618 memset(&c, 0, sizeof(c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302619 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
2620 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302621 FW_IQ_CMD_PFN_V(adap->pf) | FW_IQ_CMD_VFN_V(0));
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302622 c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002623 FW_LEN16(c));
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302624 c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
2625 FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302626 FW_IQ_CMD_IQANDST_V(intr_idx < 0) |
2627 FW_IQ_CMD_IQANUD_V(UPDATEDELIVERY_INTERRUPT_X) |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302628 FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002629 -intr_idx - 1));
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302630 c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
2631 FW_IQ_CMD_IQGTSMODE_F |
2632 FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
2633 FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002634 c.iqsize = htons(iq->size);
2635 c.iqaddr = cpu_to_be64(iq->phys_addr);
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302636 if (cong >= 0)
2637 c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002638
2639 if (fl) {
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302640 enum chip_type chip = CHELSIO_CHIP_VERSION(adap->params.chip);
2641
Hariprasad Shenai13432992015-05-05 14:59:51 +05302642 /* Allocate the ring for the hardware free list (with space
2643 * for its status page) along with the associated software
2644 * descriptor ring. The free list size needs to be a multiple
2645 * of the Egress Queue Unit and at least 2 Egress Units larger
2646 * than the SGE's Egress Congrestion Threshold
2647 * (fl_starve_thres - 1).
2648 */
2649 if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
2650 fl->size = s->fl_starve_thres - 1 + 2 * 8;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002651 fl->size = roundup(fl->size, 8);
2652 fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
2653 sizeof(struct rx_sw_desc), &fl->addr,
Hariprasad Shenai0ac5b702015-12-23 11:29:55 +05302654 &fl->sdesc, s->stat_len,
2655 dev_to_node(adap->pdev_dev));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002656 if (!fl->desc)
2657 goto fl_nomem;
2658
Vipul Pandya52367a72012-09-26 02:39:38 +00002659 flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302660 c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
2661 FW_IQ_CMD_FL0FETCHRO_F |
2662 FW_IQ_CMD_FL0DATARO_F |
2663 FW_IQ_CMD_FL0PADEN_F);
2664 if (cong >= 0)
2665 c.iqns_to_fl0congen |=
2666 htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
2667 FW_IQ_CMD_FL0CONGCIF_F |
2668 FW_IQ_CMD_FL0CONGEN_F);
Hariprasad Shenaiedadad82016-03-01 17:19:33 +05302669 /* In T6, for egress queue type FL there is internal overhead
2670 * of 16B for header going into FLM module. Hence the maximum
2671 * allowed burst size is 448 bytes. For T4/T5, the hardware
2672 * doesn't coalesce fetch requests if more than 64 bytes of
2673 * Free List pointers are provided, so we use a 128-byte Fetch
2674 * Burst Minimum there (T6 implements coalescing so we can use
2675 * the smaller 64-byte value there).
2676 */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302677 c.fl0dcaen_to_fl0cidxfthresh =
Hariprasad Shenaiedadad82016-03-01 17:19:33 +05302678 htons(FW_IQ_CMD_FL0FBMIN_V(chip <= CHELSIO_T5 ?
2679 FETCHBURSTMIN_128B_X :
2680 FETCHBURSTMIN_64B_X) |
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302681 FW_IQ_CMD_FL0FBMAX_V((chip <= CHELSIO_T5) ?
2682 FETCHBURSTMAX_512B_X :
2683 FETCHBURSTMAX_256B_X));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002684 c.fl0size = htons(flsz);
2685 c.fl0addr = cpu_to_be64(fl->addr);
2686 }
2687
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302688 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002689 if (ret)
2690 goto err;
2691
2692 netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
2693 iq->cur_desc = iq->desc;
2694 iq->cidx = 0;
2695 iq->gen = 1;
2696 iq->next_intr_params = iq->intr_params;
2697 iq->cntxt_id = ntohs(c.iqid);
2698 iq->abs_id = ntohs(c.physiqid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302699 iq->bar2_addr = bar2_address(adap,
2700 iq->cntxt_id,
2701 T4_BAR2_QTYPE_INGRESS,
2702 &iq->bar2_qid);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002703 iq->size--; /* subtract status entry */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002704 iq->netdev = dev;
2705 iq->handler = hnd;
Varun Prakash2337ba42016-02-14 23:02:41 +05302706 iq->flush_handler = flush_hnd;
2707
2708 memset(&iq->lro_mgr, 0, sizeof(struct t4_lro_mgr));
2709 skb_queue_head_init(&iq->lro_mgr.lroq);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002710
2711 /* set offset to -1 to distinguish ingress queues without FL */
2712 iq->offset = fl ? 0 : -1;
2713
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002714 adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002715
2716 if (fl) {
Roland Dreier62718b32010-04-21 08:09:21 +00002717 fl->cntxt_id = ntohs(c.fl0id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002718 fl->avail = fl->pend_cred = 0;
2719 fl->pidx = fl->cidx = 0;
2720 fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002721 adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302722
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302723 /* Note, we must initialize the BAR2 Free List User Doorbell
2724 * information before refilling the Free List!
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302725 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302726 fl->bar2_addr = bar2_address(adap,
2727 fl->cntxt_id,
2728 T4_BAR2_QTYPE_EGRESS,
2729 &fl->bar2_qid);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002730 refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
2731 }
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302732
2733 /* For T5 and later we attempt to set up the Congestion Manager values
2734 * of the new RX Ethernet Queue. This should really be handled by
2735 * firmware because it's more complex than any host driver wants to
2736 * get involved with and it's different per chip and this is almost
2737 * certainly wrong. Firmware would be wrong as well, but it would be
2738 * a lot easier to fix in one place ... For now we do something very
2739 * simple (and hopefully less wrong).
2740 */
2741 if (!is_t4(adap->params.chip) && cong >= 0) {
Hariprasad Shenai2216d012015-12-23 22:47:18 +05302742 u32 param, val, ch_map = 0;
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302743 int i;
Hariprasad Shenai2216d012015-12-23 22:47:18 +05302744 u16 cng_ch_bits_log = adap->params.arch.cng_ch_bits_log;
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302745
2746 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
2747 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
2748 FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
2749 if (cong == 0) {
2750 val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
2751 } else {
2752 val =
2753 CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
2754 for (i = 0; i < 4; i++) {
2755 if (cong & (1 << i))
Hariprasad Shenai2216d012015-12-23 22:47:18 +05302756 ch_map |= 1 << (i << cng_ch_bits_log);
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302757 }
Hariprasad Shenai2216d012015-12-23 22:47:18 +05302758 val |= CONMCTXT_CNGCHMAP_V(ch_map);
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302759 }
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302760 ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302761 &param, &val);
2762 if (ret)
2763 dev_warn(adap->pdev_dev, "Failed to set Congestion"
2764 " Manager Context for Ingress Queue %d: %d\n",
2765 iq->cntxt_id, -ret);
2766 }
2767
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002768 return 0;
2769
2770fl_nomem:
2771 ret = -ENOMEM;
2772err:
2773 if (iq->desc) {
2774 dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
2775 iq->desc, iq->phys_addr);
2776 iq->desc = NULL;
2777 }
2778 if (fl && fl->desc) {
2779 kfree(fl->sdesc);
2780 fl->sdesc = NULL;
2781 dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
2782 fl->desc, fl->addr);
2783 fl->desc = NULL;
2784 }
2785 return ret;
2786}
2787
2788static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
2789{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00002790 q->cntxt_id = id;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302791 q->bar2_addr = bar2_address(adap,
2792 q->cntxt_id,
2793 T4_BAR2_QTYPE_EGRESS,
2794 &q->bar2_qid);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002795 q->in_use = 0;
2796 q->cidx = q->pidx = 0;
2797 q->stops = q->restarts = 0;
2798 q->stat = (void *)&q->desc[q->size];
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302799 spin_lock_init(&q->db_lock);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002800 adap->sge.egr_map[id - adap->sge.egr_start] = q;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002801}
2802
2803int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
2804 struct net_device *dev, struct netdev_queue *netdevq,
2805 unsigned int iqid)
2806{
2807 int ret, nentries;
2808 struct fw_eq_eth_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002809 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002810 struct port_info *pi = netdev_priv(dev);
2811
2812 /* Add status entries */
Vipul Pandya52367a72012-09-26 02:39:38 +00002813 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002814
2815 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2816 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
Vipul Pandya52367a72012-09-26 02:39:38 +00002817 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +00002818 netdev_queue_numa_node_read(netdevq));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002819 if (!txq->q.desc)
2820 return -ENOMEM;
2821
2822 memset(&c, 0, sizeof(c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302823 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
2824 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302825 FW_EQ_ETH_CMD_PFN_V(adap->pf) |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302826 FW_EQ_ETH_CMD_VFN_V(0));
2827 c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
2828 FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
2829 c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
2830 FW_EQ_ETH_CMD_VIID_V(pi->viid));
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302831 c.fetchszm_to_iqid =
2832 htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
2833 FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
2834 FW_EQ_ETH_CMD_FETCHRO_F | FW_EQ_ETH_CMD_IQID_V(iqid));
2835 c.dcaen_to_eqsize =
2836 htonl(FW_EQ_ETH_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
2837 FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
2838 FW_EQ_ETH_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
2839 FW_EQ_ETH_CMD_EQSIZE_V(nentries));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002840 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2841
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302842 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002843 if (ret) {
2844 kfree(txq->q.sdesc);
2845 txq->q.sdesc = NULL;
2846 dma_free_coherent(adap->pdev_dev,
2847 nentries * sizeof(struct tx_desc),
2848 txq->q.desc, txq->q.phys_addr);
2849 txq->q.desc = NULL;
2850 return ret;
2851 }
2852
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302853 txq->q.q_type = CXGB4_TXQ_ETH;
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302854 init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002855 txq->txq = netdevq;
2856 txq->tso = txq->tx_cso = txq->vlan_ins = 0;
2857 txq->mapping_err = 0;
2858 return 0;
2859}
2860
2861int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
2862 struct net_device *dev, unsigned int iqid,
2863 unsigned int cmplqid)
2864{
2865 int ret, nentries;
2866 struct fw_eq_ctrl_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002867 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002868 struct port_info *pi = netdev_priv(dev);
2869
2870 /* Add status entries */
Vipul Pandya52367a72012-09-26 02:39:38 +00002871 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002872
2873 txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
2874 sizeof(struct tx_desc), 0, &txq->q.phys_addr,
Hariprasad Shenai982b81e2015-05-05 14:59:54 +05302875 NULL, 0, dev_to_node(adap->pdev_dev));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002876 if (!txq->q.desc)
2877 return -ENOMEM;
2878
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302879 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
2880 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302881 FW_EQ_CTRL_CMD_PFN_V(adap->pf) |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302882 FW_EQ_CTRL_CMD_VFN_V(0));
2883 c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
2884 FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
2885 c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002886 c.physeqid_pkd = htonl(0);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302887 c.fetchszm_to_iqid =
2888 htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
2889 FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
2890 FW_EQ_CTRL_CMD_FETCHRO_F | FW_EQ_CTRL_CMD_IQID_V(iqid));
2891 c.dcaen_to_eqsize =
2892 htonl(FW_EQ_CTRL_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
2893 FW_EQ_CTRL_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
2894 FW_EQ_CTRL_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
2895 FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002896 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2897
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302898 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002899 if (ret) {
2900 dma_free_coherent(adap->pdev_dev,
2901 nentries * sizeof(struct tx_desc),
2902 txq->q.desc, txq->q.phys_addr);
2903 txq->q.desc = NULL;
2904 return ret;
2905 }
2906
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302907 txq->q.q_type = CXGB4_TXQ_CTRL;
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302908 init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002909 txq->adap = adap;
2910 skb_queue_head_init(&txq->sendq);
2911 tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
2912 txq->full = 0;
2913 return 0;
2914}
2915
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302916int t4_sge_mod_ctrl_txq(struct adapter *adap, unsigned int eqid,
2917 unsigned int cmplqid)
2918{
2919 u32 param, val;
2920
2921 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
2922 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL) |
2923 FW_PARAMS_PARAM_YZ_V(eqid));
2924 val = cmplqid;
2925 return t4_set_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
2926}
2927
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302928int t4_sge_alloc_uld_txq(struct adapter *adap, struct sge_uld_txq *txq,
2929 struct net_device *dev, unsigned int iqid,
2930 unsigned int uld_type)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002931{
2932 int ret, nentries;
2933 struct fw_eq_ofld_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002934 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002935 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302936 int cmd = FW_EQ_OFLD_CMD;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002937
2938 /* Add status entries */
Vipul Pandya52367a72012-09-26 02:39:38 +00002939 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002940
2941 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2942 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
Vipul Pandya52367a72012-09-26 02:39:38 +00002943 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +00002944 NUMA_NO_NODE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002945 if (!txq->q.desc)
2946 return -ENOMEM;
2947
2948 memset(&c, 0, sizeof(c));
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302949 if (unlikely(uld_type == CXGB4_TX_CRYPTO))
2950 cmd = FW_EQ_CTRL_CMD;
2951 c.op_to_vfn = htonl(FW_CMD_OP_V(cmd) | FW_CMD_REQUEST_F |
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302952 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302953 FW_EQ_OFLD_CMD_PFN_V(adap->pf) |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302954 FW_EQ_OFLD_CMD_VFN_V(0));
2955 c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
2956 FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302957 c.fetchszm_to_iqid =
2958 htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
2959 FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
2960 FW_EQ_OFLD_CMD_FETCHRO_F | FW_EQ_OFLD_CMD_IQID_V(iqid));
2961 c.dcaen_to_eqsize =
2962 htonl(FW_EQ_OFLD_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
2963 FW_EQ_OFLD_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
2964 FW_EQ_OFLD_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
2965 FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002966 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2967
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302968 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002969 if (ret) {
2970 kfree(txq->q.sdesc);
2971 txq->q.sdesc = NULL;
2972 dma_free_coherent(adap->pdev_dev,
2973 nentries * sizeof(struct tx_desc),
2974 txq->q.desc, txq->q.phys_addr);
2975 txq->q.desc = NULL;
2976 return ret;
2977 }
2978
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302979 txq->q.q_type = CXGB4_TXQ_ULD;
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302980 init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002981 txq->adap = adap;
2982 skb_queue_head_init(&txq->sendq);
2983 tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
2984 txq->full = 0;
2985 txq->mapping_err = 0;
2986 return 0;
2987}
2988
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302989void free_txq(struct adapter *adap, struct sge_txq *q)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002990{
Vipul Pandya52367a72012-09-26 02:39:38 +00002991 struct sge *s = &adap->sge;
2992
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002993 dma_free_coherent(adap->pdev_dev,
Vipul Pandya52367a72012-09-26 02:39:38 +00002994 q->size * sizeof(struct tx_desc) + s->stat_len,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002995 q->desc, q->phys_addr);
2996 q->cntxt_id = 0;
2997 q->sdesc = NULL;
2998 q->desc = NULL;
2999}
3000
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05303001void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
3002 struct sge_fl *fl)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003003{
Vipul Pandya52367a72012-09-26 02:39:38 +00003004 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003005 unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
3006
Dimitris Michailidise46dab42010-08-23 17:20:58 +00003007 adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303008 t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003009 rq->cntxt_id, fl_id, 0xffff);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003010 dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
3011 rq->desc, rq->phys_addr);
3012 netif_napi_del(&rq->napi);
3013 rq->netdev = NULL;
3014 rq->cntxt_id = rq->abs_id = 0;
3015 rq->desc = NULL;
3016
3017 if (fl) {
3018 free_rx_bufs(adap, fl, fl->avail);
Vipul Pandya52367a72012-09-26 02:39:38 +00003019 dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003020 fl->desc, fl->addr);
3021 kfree(fl->sdesc);
3022 fl->sdesc = NULL;
3023 fl->cntxt_id = 0;
3024 fl->desc = NULL;
3025 }
3026}
3027
3028/**
Hariprasad Shenai5fa76692014-08-04 17:01:30 +05303029 * t4_free_ofld_rxqs - free a block of consecutive Rx queues
3030 * @adap: the adapter
3031 * @n: number of queues
3032 * @q: pointer to first queue
3033 *
3034 * Release the resources of a consecutive block of offload Rx queues.
3035 */
3036void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
3037{
3038 for ( ; n; n--, q++)
3039 if (q->rspq.desc)
3040 free_rspq_fl(adap, &q->rspq,
3041 q->fl.size ? &q->fl : NULL);
3042}
3043
3044/**
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003045 * t4_free_sge_resources - free SGE resources
3046 * @adap: the adapter
3047 *
3048 * Frees resources used by the SGE queue sets.
3049 */
3050void t4_free_sge_resources(struct adapter *adap)
3051{
3052 int i;
Hariprasad Shenaiebf4dc22016-04-11 11:07:58 +05303053 struct sge_eth_rxq *eq;
3054 struct sge_eth_txq *etq;
3055
3056 /* stop all Rx queues in order to start them draining */
3057 for (i = 0; i < adap->sge.ethqsets; i++) {
3058 eq = &adap->sge.ethrxq[i];
3059 if (eq->rspq.desc)
3060 t4_iq_stop(adap, adap->mbox, adap->pf, 0,
3061 FW_IQ_TYPE_FL_INT_CAP,
3062 eq->rspq.cntxt_id,
3063 eq->fl.size ? eq->fl.cntxt_id : 0xffff,
3064 0xffff);
3065 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003066
3067 /* clean up Ethernet Tx/Rx queues */
Hariprasad Shenaiebf4dc22016-04-11 11:07:58 +05303068 for (i = 0; i < adap->sge.ethqsets; i++) {
3069 eq = &adap->sge.ethrxq[i];
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003070 if (eq->rspq.desc)
Hariprasad Shenai5fa76692014-08-04 17:01:30 +05303071 free_rspq_fl(adap, &eq->rspq,
3072 eq->fl.size ? &eq->fl : NULL);
Hariprasad Shenaiebf4dc22016-04-11 11:07:58 +05303073
3074 etq = &adap->sge.ethtxq[i];
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003075 if (etq->q.desc) {
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303076 t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003077 etq->q.cntxt_id);
Hariprasad Shenaifbe80772016-04-26 20:10:24 +05303078 __netif_tx_lock_bh(etq->txq);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003079 free_tx_desc(adap, &etq->q, etq->q.in_use, true);
Hariprasad Shenaifbe80772016-04-26 20:10:24 +05303080 __netif_tx_unlock_bh(etq->txq);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003081 kfree(etq->q.sdesc);
3082 free_txq(adap, &etq->q);
3083 }
3084 }
3085
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003086 /* clean up control Tx queues */
3087 for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
3088 struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
3089
3090 if (cq->q.desc) {
3091 tasklet_kill(&cq->qresume_tsk);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303092 t4_ctrl_eq_free(adap, adap->mbox, adap->pf, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003093 cq->q.cntxt_id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003094 __skb_queue_purge(&cq->sendq);
3095 free_txq(adap, &cq->q);
3096 }
3097 }
3098
3099 if (adap->sge.fw_evtq.desc)
3100 free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
3101
3102 if (adap->sge.intrq.desc)
3103 free_rspq_fl(adap, &adap->sge.intrq, NULL);
3104
3105 /* clear the reverse egress queue map */
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303106 memset(adap->sge.egr_map, 0,
3107 adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003108}
3109
3110void t4_sge_start(struct adapter *adap)
3111{
3112 adap->sge.ethtxq_rover = 0;
3113 mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
3114 mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
3115}
3116
3117/**
3118 * t4_sge_stop - disable SGE operation
3119 * @adap: the adapter
3120 *
3121 * Stop tasklets and timers associated with the DMA engine. Note that
3122 * this is effective only if measures have been taken to disable any HW
3123 * events that may restart them.
3124 */
3125void t4_sge_stop(struct adapter *adap)
3126{
3127 int i;
3128 struct sge *s = &adap->sge;
3129
3130 if (in_interrupt()) /* actions below require waiting */
3131 return;
3132
3133 if (s->rx_timer.function)
3134 del_timer_sync(&s->rx_timer);
3135 if (s->tx_timer.function)
3136 del_timer_sync(&s->tx_timer);
3137
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303138 if (is_offload(adap)) {
3139 struct sge_uld_txq_info *txq_info;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003140
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303141 txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
3142 if (txq_info) {
3143 struct sge_uld_txq *txq = txq_info->uldtxq;
3144
3145 for_each_ofldtxq(&adap->sge, i) {
3146 if (txq->q.desc)
3147 tasklet_kill(&txq->qresume_tsk);
3148 }
3149 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003150 }
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303151
3152 if (is_pci_uld(adap)) {
3153 struct sge_uld_txq_info *txq_info;
3154
3155 txq_info = adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
3156 if (txq_info) {
3157 struct sge_uld_txq *txq = txq_info->uldtxq;
3158
3159 for_each_ofldtxq(&adap->sge, i) {
3160 if (txq->q.desc)
3161 tasklet_kill(&txq->qresume_tsk);
3162 }
3163 }
3164 }
3165
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003166 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
3167 struct sge_ctrl_txq *cq = &s->ctrlq[i];
3168
3169 if (cq->q.desc)
3170 tasklet_kill(&cq->qresume_tsk);
3171 }
3172}
3173
3174/**
Hariprasad Shenai06640312015-01-13 15:19:25 +05303175 * t4_sge_init_soft - grab core SGE values needed by SGE code
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003176 * @adap: the adapter
3177 *
Hariprasad Shenai06640312015-01-13 15:19:25 +05303178 * We need to grab the SGE operating parameters that we need to have
3179 * in order to do our job and make sure we can live with them.
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003180 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003181
Vipul Pandya52367a72012-09-26 02:39:38 +00003182static int t4_sge_init_soft(struct adapter *adap)
3183{
3184 struct sge *s = &adap->sge;
3185 u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
3186 u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
3187 u32 ingress_rx_threshold;
3188
3189 /*
3190 * Verify that CPL messages are going to the Ingress Queue for
3191 * process_responses() and that only packet data is going to the
3192 * Free Lists.
3193 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303194 if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
3195 RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
Vipul Pandya52367a72012-09-26 02:39:38 +00003196 dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
3197 return -EINVAL;
3198 }
3199
3200 /*
3201 * Validate the Host Buffer Register Array indices that we want to
3202 * use ...
3203 *
3204 * XXX Note that we should really read through the Host Buffer Size
3205 * XXX register array and find the indices of the Buffer Sizes which
3206 * XXX meet our needs!
3207 */
3208 #define READ_FL_BUF(x) \
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303209 t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
Vipul Pandya52367a72012-09-26 02:39:38 +00003210
3211 fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
3212 fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
3213 fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
3214 fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
3215
Kumar Sanghvi92ddcc72014-03-13 20:50:46 +05303216 /* We only bother using the Large Page logic if the Large Page Buffer
3217 * is larger than our Page Size Buffer.
3218 */
3219 if (fl_large_pg <= fl_small_pg)
3220 fl_large_pg = 0;
3221
Vipul Pandya52367a72012-09-26 02:39:38 +00003222 #undef READ_FL_BUF
3223
Kumar Sanghvi92ddcc72014-03-13 20:50:46 +05303224 /* The Page Size Buffer must be exactly equal to our Page Size and the
3225 * Large Page Size Buffer should be 0 (per above) or a power of 2.
3226 */
Vipul Pandya52367a72012-09-26 02:39:38 +00003227 if (fl_small_pg != PAGE_SIZE ||
Kumar Sanghvi92ddcc72014-03-13 20:50:46 +05303228 (fl_large_pg & (fl_large_pg-1)) != 0) {
Vipul Pandya52367a72012-09-26 02:39:38 +00003229 dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
3230 fl_small_pg, fl_large_pg);
3231 return -EINVAL;
3232 }
3233 if (fl_large_pg)
3234 s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
3235
3236 if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
3237 fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
3238 dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
3239 fl_small_mtu, fl_large_mtu);
3240 return -EINVAL;
3241 }
3242
3243 /*
3244 * Retrieve our RX interrupt holdoff timer values and counter
3245 * threshold values from the SGE parameters.
3246 */
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303247 timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
3248 timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
3249 timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
Vipul Pandya52367a72012-09-26 02:39:38 +00003250 s->timer_val[0] = core_ticks_to_us(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303251 TIMERVALUE0_G(timer_value_0_and_1));
Vipul Pandya52367a72012-09-26 02:39:38 +00003252 s->timer_val[1] = core_ticks_to_us(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303253 TIMERVALUE1_G(timer_value_0_and_1));
Vipul Pandya52367a72012-09-26 02:39:38 +00003254 s->timer_val[2] = core_ticks_to_us(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303255 TIMERVALUE2_G(timer_value_2_and_3));
Vipul Pandya52367a72012-09-26 02:39:38 +00003256 s->timer_val[3] = core_ticks_to_us(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303257 TIMERVALUE3_G(timer_value_2_and_3));
Vipul Pandya52367a72012-09-26 02:39:38 +00003258 s->timer_val[4] = core_ticks_to_us(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303259 TIMERVALUE4_G(timer_value_4_and_5));
Vipul Pandya52367a72012-09-26 02:39:38 +00003260 s->timer_val[5] = core_ticks_to_us(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303261 TIMERVALUE5_G(timer_value_4_and_5));
Vipul Pandya52367a72012-09-26 02:39:38 +00003262
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303263 ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
3264 s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
3265 s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
3266 s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
3267 s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
Vipul Pandya52367a72012-09-26 02:39:38 +00003268
3269 return 0;
3270}
3271
Hariprasad Shenai06640312015-01-13 15:19:25 +05303272/**
3273 * t4_sge_init - initialize SGE
3274 * @adap: the adapter
3275 *
3276 * Perform low-level SGE code initialization needed every time after a
3277 * chip reset.
3278 */
Vipul Pandya52367a72012-09-26 02:39:38 +00003279int t4_sge_init(struct adapter *adap)
3280{
3281 struct sge *s = &adap->sge;
Hariprasad Shenaiacac5962015-12-23 22:47:13 +05303282 u32 sge_control, sge_conm_ctrl;
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303283 int ret, egress_threshold;
Vipul Pandya52367a72012-09-26 02:39:38 +00003284
3285 /*
3286 * Ingress Padding Boundary and Egress Status Page Size are set up by
3287 * t4_fixup_host_params().
3288 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303289 sge_control = t4_read_reg(adap, SGE_CONTROL_A);
3290 s->pktshift = PKTSHIFT_G(sge_control);
3291 s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
Hariprasad Shenaice8f4072014-11-07 17:06:30 +05303292
Hariprasad Shenaiacac5962015-12-23 22:47:13 +05303293 s->fl_align = t4_fl_pkt_align(adap);
Hariprasad Shenai06640312015-01-13 15:19:25 +05303294 ret = t4_sge_init_soft(adap);
Vipul Pandya52367a72012-09-26 02:39:38 +00003295 if (ret < 0)
3296 return ret;
3297
3298 /*
3299 * A FL with <= fl_starve_thres buffers is starving and a periodic
3300 * timer will attempt to refill it. This needs to be larger than the
3301 * SGE's Egress Congestion Threshold. If it isn't, then we can get
3302 * stuck waiting for new packets while the SGE is waiting for us to
3303 * give it more Free List entries. (Note that the SGE's Egress
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303304 * Congestion Threshold is in units of 2 Free List pointers.) For T4,
3305 * there was only a single field to control this. For T5 there's the
3306 * original field which now only applies to Unpacked Mode Free List
3307 * buffers and a new field which only applies to Packed Mode Free List
3308 * buffers.
Vipul Pandya52367a72012-09-26 02:39:38 +00003309 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303310 sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05303311 switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
3312 case CHELSIO_T4:
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303313 egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05303314 break;
3315 case CHELSIO_T5:
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303316 egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05303317 break;
3318 case CHELSIO_T6:
3319 egress_threshold = T6_EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
3320 break;
3321 default:
3322 dev_err(adap->pdev_dev, "Unsupported Chip version %d\n",
3323 CHELSIO_CHIP_VERSION(adap->params.chip));
3324 return -EINVAL;
3325 }
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303326 s->fl_starve_thres = 2*egress_threshold + 1;
Vipul Pandya52367a72012-09-26 02:39:38 +00003327
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05303328 t4_idma_monitor_init(adap, &s->idma_monitor);
3329
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05303330 /* Set up timers used for recuring callbacks to process RX and TX
3331 * administrative tasks.
3332 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003333 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
3334 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05303335
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003336 spin_lock_init(&s->intrq_lock);
Vipul Pandya52367a72012-09-26 02:39:38 +00003337
3338 return 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003339}