blob: 4ef68f69b58c45322d65f414e06d068ade4ab22d [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#include <net/busy_poll.h>
Varun Prakash84a200b2015-03-24 19:14:46 +053047#ifdef CONFIG_CHELSIO_T4_FCOE
48#include <scsi/fc/fc_fcoe.h>
49#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +000050#include "cxgb4.h"
51#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053052#include "t4_values.h"
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +000053#include "t4_msg.h"
54#include "t4fw_api.h"
Atul Guptaa45695042017-07-04 16:46:20 +053055#include "cxgb4_ptp.h"
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +000056
57/*
58 * Rx buffer size. We use largish buffers if possible but settle for single
59 * pages under memory shortage.
60 */
61#if PAGE_SHIFT >= 16
62# define FL_PG_ORDER 0
63#else
64# define FL_PG_ORDER (16 - PAGE_SHIFT)
65#endif
66
67/* RX_PULL_LEN should be <= RX_COPY_THRES */
68#define RX_COPY_THRES 256
69#define RX_PULL_LEN 128
70
71/*
72 * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
73 * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
74 */
75#define RX_PKT_SKB_LEN 512
76
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +000077/*
78 * Max number of Tx descriptors we clean up at a time. Should be modest as
79 * freeing skbs isn't cheap and it happens while holding locks. We just need
80 * to free packets faster than they arrive, we eventually catch up and keep
81 * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
82 */
83#define MAX_TX_RECLAIM 16
84
85/*
86 * Max number of Rx buffers we replenish at a time. Again keep this modest,
87 * allocating buffers isn't cheap either.
88 */
89#define MAX_RX_REFILL 16U
90
91/*
92 * Period of the Rx queue check timer. This timer is infrequent as it has
93 * something to do only when the system experiences severe memory shortage.
94 */
95#define RX_QCHECK_PERIOD (HZ / 2)
96
97/*
98 * Period of the Tx queue check timer.
99 */
100#define TX_QCHECK_PERIOD (HZ / 2)
101
102/*
103 * Max number of Tx descriptors to be reclaimed by the Tx timer.
104 */
105#define MAX_TIMER_TX_RECLAIM 100
106
107/*
108 * Timer index used when backing off due to memory shortage.
109 */
110#define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
111
112/*
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000113 * Suspend an Ethernet Tx queue with fewer available descriptors than this.
114 * This is the same as calc_tx_descs() for a TSO packet with
115 * nr_frags == MAX_SKB_FRAGS.
116 */
117#define ETHTXQ_STOP_THRES \
118 (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
119
120/*
121 * Suspension threshold for non-Ethernet Tx queues. We require enough room
122 * for a full sized WR.
123 */
124#define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
125
126/*
127 * Max Tx descriptor space we allow for an Ethernet packet to be inlined
128 * into a WR.
129 */
Hariprasad Shenai21dcfad2015-04-15 02:02:30 +0530130#define MAX_IMM_TX_PKT_LEN 256
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000131
132/*
133 * Max size of a WR sent through a control Tx queue.
134 */
135#define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
136
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000137struct tx_sw_desc { /* SW state per Tx descriptor */
138 struct sk_buff *skb;
139 struct ulptx_sgl *sgl;
140};
141
142struct rx_sw_desc { /* SW state per Rx descriptor */
143 struct page *page;
144 dma_addr_t dma_addr;
145};
146
147/*
Vipul Pandya52367a72012-09-26 02:39:38 +0000148 * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
149 * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
150 * We could easily support more but there doesn't seem to be much need for
151 * that ...
152 */
153#define FL_MTU_SMALL 1500
154#define FL_MTU_LARGE 9000
155
156static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
157 unsigned int mtu)
158{
159 struct sge *s = &adapter->sge;
160
161 return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
162}
163
164#define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
165#define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
166
167/*
168 * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
169 * these to specify the buffer size as an index into the SGE Free List Buffer
170 * Size register array. We also use bit 4, when the buffer has been unmapped
171 * for DMA, but this is of course never sent to the hardware and is only used
172 * to prevent double unmappings. All of the above requires that the Free List
173 * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
174 * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
175 * Free List Buffer alignment is 32 bytes, this works out for us ...
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000176 */
177enum {
Vipul Pandya52367a72012-09-26 02:39:38 +0000178 RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
179 RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
180 RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
181
182 /*
183 * XXX We shouldn't depend on being able to use these indices.
184 * XXX Especially when some other Master PF has initialized the
185 * XXX adapter or we use the Firmware Configuration File. We
186 * XXX should really search through the Host Buffer Size register
187 * XXX array for the appropriately sized buffer indices.
188 */
189 RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
190 RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
191
192 RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
193 RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000194};
195
Hariprasad Shenaie553ec32014-09-26 00:23:55 +0530196static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
197#define MIN_NAPI_WORK 1
198
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000199static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
200{
Vipul Pandya52367a72012-09-26 02:39:38 +0000201 return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000202}
203
204static inline bool is_buf_mapped(const struct rx_sw_desc *d)
205{
206 return !(d->dma_addr & RX_UNMAPPED_BUF);
207}
208
209/**
210 * txq_avail - return the number of available slots in a Tx queue
211 * @q: the Tx queue
212 *
213 * Returns the number of descriptors in a Tx queue available to write new
214 * packets.
215 */
216static inline unsigned int txq_avail(const struct sge_txq *q)
217{
218 return q->size - 1 - q->in_use;
219}
220
221/**
222 * fl_cap - return the capacity of a free-buffer list
223 * @fl: the FL
224 *
225 * Returns the capacity of a free-buffer list. The capacity is less than
226 * the size because one descriptor needs to be left unpopulated, otherwise
227 * HW will think the FL is empty.
228 */
229static inline unsigned int fl_cap(const struct sge_fl *fl)
230{
231 return fl->size - 8; /* 1 descriptor = 8 buffers */
232}
233
Hariprasad Shenaic098b022015-04-15 02:02:31 +0530234/**
235 * fl_starving - return whether a Free List is starving.
236 * @adapter: pointer to the adapter
237 * @fl: the Free List
238 *
239 * Tests specified Free List to see whether the number of buffers
240 * available to the hardware has falled below our "starvation"
241 * threshold.
242 */
243static inline bool fl_starving(const struct adapter *adapter,
244 const struct sge_fl *fl)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000245{
Hariprasad Shenaic098b022015-04-15 02:02:31 +0530246 const struct sge *s = &adapter->sge;
247
248 return fl->avail - fl->pend_cred <= s->fl_starve_thres;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000249}
250
251static int map_skb(struct device *dev, const struct sk_buff *skb,
252 dma_addr_t *addr)
253{
254 const skb_frag_t *fp, *end;
255 const struct skb_shared_info *si;
256
257 *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
258 if (dma_mapping_error(dev, *addr))
259 goto out_err;
260
261 si = skb_shinfo(skb);
262 end = &si->frags[si->nr_frags];
263
264 for (fp = si->frags; fp < end; fp++) {
Ian Campbelle91b0f22011-10-19 23:01:46 +0000265 *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
266 DMA_TO_DEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000267 if (dma_mapping_error(dev, *addr))
268 goto unwind;
269 }
270 return 0;
271
272unwind:
273 while (fp-- > si->frags)
Eric Dumazet9e903e02011-10-18 21:00:24 +0000274 dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000275
276 dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
277out_err:
278 return -ENOMEM;
279}
280
281#ifdef CONFIG_NEED_DMA_MAP_STATE
282static void unmap_skb(struct device *dev, const struct sk_buff *skb,
283 const dma_addr_t *addr)
284{
285 const skb_frag_t *fp, *end;
286 const struct skb_shared_info *si;
287
288 dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
289
290 si = skb_shinfo(skb);
291 end = &si->frags[si->nr_frags];
292 for (fp = si->frags; fp < end; fp++)
Eric Dumazet9e903e02011-10-18 21:00:24 +0000293 dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000294}
295
296/**
297 * deferred_unmap_destructor - unmap a packet when it is freed
298 * @skb: the packet
299 *
300 * This is the packet destructor used for Tx packets that need to remain
301 * mapped until they are freed rather than until their Tx descriptors are
302 * freed.
303 */
304static void deferred_unmap_destructor(struct sk_buff *skb)
305{
306 unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
307}
308#endif
309
310static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
311 const struct ulptx_sgl *sgl, const struct sge_txq *q)
312{
313 const struct ulptx_sge_pair *p;
314 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
315
316 if (likely(skb_headlen(skb)))
317 dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
318 DMA_TO_DEVICE);
319 else {
320 dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
321 DMA_TO_DEVICE);
322 nfrags--;
323 }
324
325 /*
326 * the complexity below is because of the possibility of a wrap-around
327 * in the middle of an SGL
328 */
329 for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
330 if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
331unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
332 ntohl(p->len[0]), DMA_TO_DEVICE);
333 dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
334 ntohl(p->len[1]), DMA_TO_DEVICE);
335 p++;
336 } else if ((u8 *)p == (u8 *)q->stat) {
337 p = (const struct ulptx_sge_pair *)q->desc;
338 goto unmap;
339 } else if ((u8 *)p + 8 == (u8 *)q->stat) {
340 const __be64 *addr = (const __be64 *)q->desc;
341
342 dma_unmap_page(dev, be64_to_cpu(addr[0]),
343 ntohl(p->len[0]), DMA_TO_DEVICE);
344 dma_unmap_page(dev, be64_to_cpu(addr[1]),
345 ntohl(p->len[1]), DMA_TO_DEVICE);
346 p = (const struct ulptx_sge_pair *)&addr[2];
347 } else {
348 const __be64 *addr = (const __be64 *)q->desc;
349
350 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
351 ntohl(p->len[0]), DMA_TO_DEVICE);
352 dma_unmap_page(dev, be64_to_cpu(addr[0]),
353 ntohl(p->len[1]), DMA_TO_DEVICE);
354 p = (const struct ulptx_sge_pair *)&addr[1];
355 }
356 }
357 if (nfrags) {
358 __be64 addr;
359
360 if ((u8 *)p == (u8 *)q->stat)
361 p = (const struct ulptx_sge_pair *)q->desc;
362 addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
363 *(const __be64 *)q->desc;
364 dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
365 DMA_TO_DEVICE);
366 }
367}
368
369/**
370 * free_tx_desc - reclaims Tx descriptors and their buffers
371 * @adapter: the adapter
372 * @q: the Tx queue to reclaim descriptors from
373 * @n: the number of descriptors to reclaim
374 * @unmap: whether the buffers should be unmapped for DMA
375 *
376 * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
377 * Tx buffers. Called with the Tx queue lock held.
378 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530379void free_tx_desc(struct adapter *adap, struct sge_txq *q,
380 unsigned int n, bool unmap)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000381{
382 struct tx_sw_desc *d;
383 unsigned int cidx = q->cidx;
384 struct device *dev = adap->pdev_dev;
385
386 d = &q->sdesc[cidx];
387 while (n--) {
388 if (d->skb) { /* an SGL is present */
389 if (unmap)
390 unmap_sgl(dev, d->skb, d->sgl, q);
Eric W. Biedermana7525192014-03-15 16:29:49 -0700391 dev_consume_skb_any(d->skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000392 d->skb = NULL;
393 }
394 ++d;
395 if (++cidx == q->size) {
396 cidx = 0;
397 d = q->sdesc;
398 }
399 }
400 q->cidx = cidx;
401}
402
403/*
404 * Return the number of reclaimable descriptors in a Tx queue.
405 */
406static inline int reclaimable(const struct sge_txq *q)
407{
Hariprasad Shenai632be192015-12-08 10:09:13 +0530408 int hw_cidx = ntohs(ACCESS_ONCE(q->stat->cidx));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000409 hw_cidx -= q->cidx;
410 return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
411}
412
413/**
414 * reclaim_completed_tx - reclaims completed Tx descriptors
415 * @adap: the adapter
416 * @q: the Tx queue to reclaim completed descriptors from
417 * @unmap: whether the buffers should be unmapped for DMA
418 *
419 * Reclaims Tx descriptors that the SGE has indicated it has processed,
420 * and frees the associated buffers if possible. Called with the Tx
421 * queue locked.
422 */
423static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
424 bool unmap)
425{
426 int avail = reclaimable(q);
427
428 if (avail) {
429 /*
430 * Limit the amount of clean up work we do at a time to keep
431 * the Tx lock hold time O(1).
432 */
433 if (avail > MAX_TX_RECLAIM)
434 avail = MAX_TX_RECLAIM;
435
436 free_tx_desc(adap, q, avail, unmap);
437 q->in_use -= avail;
438 }
439}
440
Vipul Pandya52367a72012-09-26 02:39:38 +0000441static inline int get_buf_size(struct adapter *adapter,
442 const struct rx_sw_desc *d)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000443{
Vipul Pandya52367a72012-09-26 02:39:38 +0000444 struct sge *s = &adapter->sge;
445 unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
446 int buf_size;
447
448 switch (rx_buf_size_idx) {
449 case RX_SMALL_PG_BUF:
450 buf_size = PAGE_SIZE;
451 break;
452
453 case RX_LARGE_PG_BUF:
454 buf_size = PAGE_SIZE << s->fl_pg_order;
455 break;
456
457 case RX_SMALL_MTU_BUF:
458 buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
459 break;
460
461 case RX_LARGE_MTU_BUF:
462 buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
463 break;
464
465 default:
466 BUG_ON(1);
467 }
468
469 return buf_size;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000470}
471
472/**
473 * free_rx_bufs - free the Rx buffers on an SGE free list
474 * @adap: the adapter
475 * @q: the SGE free list to free buffers from
476 * @n: how many buffers to free
477 *
478 * Release the next @n buffers on an SGE free-buffer Rx queue. The
479 * buffers must be made inaccessible to HW before calling this function.
480 */
481static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
482{
483 while (n--) {
484 struct rx_sw_desc *d = &q->sdesc[q->cidx];
485
486 if (is_buf_mapped(d))
487 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
Vipul Pandya52367a72012-09-26 02:39:38 +0000488 get_buf_size(adap, d),
489 PCI_DMA_FROMDEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000490 put_page(d->page);
491 d->page = NULL;
492 if (++q->cidx == q->size)
493 q->cidx = 0;
494 q->avail--;
495 }
496}
497
498/**
499 * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
500 * @adap: the adapter
501 * @q: the SGE free list
502 *
503 * Unmap the current buffer on an SGE free-buffer Rx queue. The
504 * buffer must be made inaccessible to HW before calling this function.
505 *
506 * This is similar to @free_rx_bufs above but does not free the buffer.
507 * Do note that the FL still loses any further access to the buffer.
508 */
509static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
510{
511 struct rx_sw_desc *d = &q->sdesc[q->cidx];
512
513 if (is_buf_mapped(d))
514 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
Vipul Pandya52367a72012-09-26 02:39:38 +0000515 get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000516 d->page = NULL;
517 if (++q->cidx == q->size)
518 q->cidx = 0;
519 q->avail--;
520}
521
522static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
523{
524 if (q->pend_cred >= 8) {
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530525 u32 val = adap->params.arch.sge_fl_db;
526
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530527 if (is_t4(adap->params.chip))
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530528 val |= PIDX_V(q->pend_cred / 8);
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530529 else
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530530 val |= PIDX_T5_V(q->pend_cred / 8);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +0530531
532 /* Make sure all memory writes to the Free List queue are
533 * committed before we tell the hardware about them.
534 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000535 wmb();
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530536
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530537 /* If we don't have access to the new User Doorbell (T5+), use
538 * the old doorbell mechanism; otherwise use the new BAR2
539 * mechanism.
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530540 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530541 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530542 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
543 val | QID_V(q->cntxt_id));
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530544 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530545 writel(val | QID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530546 q->bar2_addr + SGE_UDB_KDOORBELL);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530547
548 /* This Write memory Barrier will force the write to
549 * the User Doorbell area to be flushed.
550 */
551 wmb();
552 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000553 q->pend_cred &= 7;
554 }
555}
556
557static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
558 dma_addr_t mapping)
559{
560 sd->page = pg;
561 sd->dma_addr = mapping; /* includes size low bits */
562}
563
564/**
565 * refill_fl - refill an SGE Rx buffer ring
566 * @adap: the adapter
567 * @q: the ring to refill
568 * @n: the number of new buffers to allocate
569 * @gfp: the gfp flags for the allocations
570 *
571 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
572 * allocated with the supplied gfp flags. The caller must assure that
573 * @n does not exceed the queue's capacity. If afterwards the queue is
574 * found critically low mark it as starving in the bitmap of starving FLs.
575 *
576 * Returns the number of buffers allocated.
577 */
578static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
579 gfp_t gfp)
580{
Vipul Pandya52367a72012-09-26 02:39:38 +0000581 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000582 struct page *pg;
583 dma_addr_t mapping;
584 unsigned int cred = q->avail;
585 __be64 *d = &q->desc[q->pidx];
586 struct rx_sw_desc *sd = &q->sdesc[q->pidx];
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530587 int node;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000588
Hariprasad Shenai5b377d12015-05-27 22:30:23 +0530589#ifdef CONFIG_DEBUG_FS
590 if (test_bit(q->cntxt_id - adap->sge.egr_start, adap->sge.blocked_fl))
591 goto out;
592#endif
593
Alexander Duyckaa9cd312014-11-11 09:26:42 -0800594 gfp |= __GFP_NOWARN;
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530595 node = dev_to_node(adap->pdev_dev);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000596
Vipul Pandya52367a72012-09-26 02:39:38 +0000597 if (s->fl_pg_order == 0)
598 goto alloc_small_pages;
599
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000600 /*
601 * Prefer large buffers
602 */
603 while (n) {
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530604 pg = alloc_pages_node(node, gfp | __GFP_COMP, s->fl_pg_order);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000605 if (unlikely(!pg)) {
606 q->large_alloc_failed++;
607 break; /* fall back to single pages */
608 }
609
610 mapping = dma_map_page(adap->pdev_dev, pg, 0,
Vipul Pandya52367a72012-09-26 02:39:38 +0000611 PAGE_SIZE << s->fl_pg_order,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000612 PCI_DMA_FROMDEVICE);
613 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
Vipul Pandya52367a72012-09-26 02:39:38 +0000614 __free_pages(pg, s->fl_pg_order);
Hariprasad Shenai70055dd2015-12-08 10:09:16 +0530615 q->mapping_err++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000616 goto out; /* do not try small pages for this error */
617 }
Vipul Pandya52367a72012-09-26 02:39:38 +0000618 mapping |= RX_LARGE_PG_BUF;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000619 *d++ = cpu_to_be64(mapping);
620
621 set_rx_sw_desc(sd, pg, mapping);
622 sd++;
623
624 q->avail++;
625 if (++q->pidx == q->size) {
626 q->pidx = 0;
627 sd = q->sdesc;
628 d = q->desc;
629 }
630 n--;
631 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000632
Vipul Pandya52367a72012-09-26 02:39:38 +0000633alloc_small_pages:
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000634 while (n--) {
Hariprasad Shenaid52ce922015-04-15 02:02:32 +0530635 pg = alloc_pages_node(node, gfp, 0);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000636 if (unlikely(!pg)) {
637 q->alloc_failed++;
638 break;
639 }
640
641 mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
642 PCI_DMA_FROMDEVICE);
643 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000644 put_page(pg);
Hariprasad Shenai70055dd2015-12-08 10:09:16 +0530645 q->mapping_err++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000646 goto out;
647 }
648 *d++ = cpu_to_be64(mapping);
649
650 set_rx_sw_desc(sd, pg, mapping);
651 sd++;
652
653 q->avail++;
654 if (++q->pidx == q->size) {
655 q->pidx = 0;
656 sd = q->sdesc;
657 d = q->desc;
658 }
659 }
660
661out: cred = q->avail - cred;
662 q->pend_cred += cred;
663 ring_fl_db(adap, q);
664
Hariprasad Shenaic098b022015-04-15 02:02:31 +0530665 if (unlikely(fl_starving(adap, q))) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000666 smp_wmb();
Hariprasad Shenai70055dd2015-12-08 10:09:16 +0530667 q->low++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000668 set_bit(q->cntxt_id - adap->sge.egr_start,
669 adap->sge.starving_fl);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000670 }
671
672 return cred;
673}
674
675static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
676{
677 refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
678 GFP_ATOMIC);
679}
680
681/**
682 * alloc_ring - allocate resources for an SGE descriptor ring
683 * @dev: the PCI device's core device
684 * @nelem: the number of descriptors
685 * @elem_size: the size of each descriptor
686 * @sw_size: the size of the SW state associated with each ring element
687 * @phys: the physical address of the allocated ring
688 * @metadata: address of the array holding the SW state for the ring
689 * @stat_size: extra space in HW ring for status information
Dimitris Michailidisad6bad32010-12-14 21:36:55 +0000690 * @node: preferred node for memory allocations
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000691 *
692 * Allocates resources for an SGE descriptor ring, such as Tx queues,
693 * free buffer lists, or response queues. Each SGE ring requires
694 * space for its HW descriptors plus, optionally, space for the SW state
695 * associated with each HW entry (the metadata). The function returns
696 * three values: the virtual address for the HW ring (the return value
697 * of the function), the bus address of the HW ring, and the address
698 * of the SW ring.
699 */
700static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
701 size_t sw_size, dma_addr_t *phys, void *metadata,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +0000702 size_t stat_size, int node)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000703{
704 size_t len = nelem * elem_size + stat_size;
705 void *s = NULL;
706 void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
707
708 if (!p)
709 return NULL;
710 if (sw_size) {
Dimitris Michailidisad6bad32010-12-14 21:36:55 +0000711 s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000712
713 if (!s) {
714 dma_free_coherent(dev, len, p, *phys);
715 return NULL;
716 }
717 }
718 if (metadata)
719 *(void **)metadata = s;
720 memset(p, 0, len);
721 return p;
722}
723
724/**
725 * sgl_len - calculates the size of an SGL of the given capacity
726 * @n: the number of SGL entries
727 *
728 * Calculates the number of flits needed for a scatter/gather list that
729 * can hold the given number of entries.
730 */
731static inline unsigned int sgl_len(unsigned int n)
732{
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530733 /* A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
734 * addresses. The DSGL Work Request starts off with a 32-bit DSGL
735 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
736 * repeated sequences of { Length[i], Length[i+1], Address[i],
737 * Address[i+1] } (this ensures that all addresses are on 64-bit
738 * boundaries). If N is even, then Length[N+1] should be set to 0 and
739 * Address[N+1] is omitted.
740 *
741 * The following calculation incorporates all of the above. It's
742 * somewhat hard to follow but, briefly: the "+2" accounts for the
743 * first two flits which include the DSGL header, Length0 and
744 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
745 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
746 * finally the "+((n-1)&1)" adds the one remaining flit needed if
747 * (n-1) is odd ...
748 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000749 n--;
750 return (3 * n) / 2 + (n & 1) + 2;
751}
752
753/**
754 * flits_to_desc - returns the num of Tx descriptors for the given flits
755 * @n: the number of flits
756 *
757 * Returns the number of Tx descriptors needed for the supplied number
758 * of flits.
759 */
760static inline unsigned int flits_to_desc(unsigned int n)
761{
762 BUG_ON(n > SGE_MAX_WR_LEN / 8);
763 return DIV_ROUND_UP(n, 8);
764}
765
766/**
767 * is_eth_imm - can an Ethernet packet be sent as immediate data?
768 * @skb: the packet
769 *
770 * Returns whether an Ethernet packet is small enough to fit as
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530771 * immediate data. Return value corresponds to headroom required.
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000772 */
773static inline int is_eth_imm(const struct sk_buff *skb)
774{
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530775 int hdrlen = skb_shinfo(skb)->gso_size ?
776 sizeof(struct cpl_tx_pkt_lso_core) : 0;
777
778 hdrlen += sizeof(struct cpl_tx_pkt);
779 if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
780 return hdrlen;
781 return 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000782}
783
784/**
785 * calc_tx_flits - calculate the number of flits for a packet Tx WR
786 * @skb: the packet
787 *
788 * Returns the number of flits needed for a Tx WR for the given Ethernet
789 * packet, including the needed WR and CPL headers.
790 */
791static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
792{
793 unsigned int flits;
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530794 int hdrlen = is_eth_imm(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000795
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530796 /* If the skb is small enough, we can pump it out as a work request
797 * with only immediate data. In that case we just have to have the
798 * TX Packet header plus the skb data in the Work Request.
799 */
800
Kumar Sanghvi0034b292014-02-18 17:56:14 +0530801 if (hdrlen)
802 return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000803
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530804 /* Otherwise, we're going to have to construct a Scatter gather list
805 * of the skb body and fragments. We also include the flits necessary
806 * for the TX Packet Work Request and CPL. We always have a firmware
807 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
808 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
809 * message or, if we're doing a Large Send Offload, an LSO CPL message
810 * with an embedded TX Packet Write CPL message.
811 */
Hariprasad Shenaifd1754f2015-09-08 16:25:39 +0530812 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000813 if (skb_shinfo(skb)->gso_size)
Hariprasad Shenai0aac3f52015-04-15 02:02:33 +0530814 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
815 sizeof(struct cpl_tx_pkt_lso_core) +
816 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
817 else
818 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
819 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000820 return flits;
821}
822
823/**
824 * calc_tx_descs - calculate the number of Tx descriptors for a packet
825 * @skb: the packet
826 *
827 * Returns the number of Tx descriptors needed for the given Ethernet
828 * packet, including the needed WR and CPL headers.
829 */
830static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
831{
832 return flits_to_desc(calc_tx_flits(skb));
833}
834
835/**
836 * write_sgl - populate a scatter/gather list for a packet
837 * @skb: the packet
838 * @q: the Tx queue we are writing into
839 * @sgl: starting location for writing the SGL
840 * @end: points right after the end of the SGL
841 * @start: start offset into skb main-body data to include in the SGL
842 * @addr: the list of bus addresses for the SGL elements
843 *
844 * Generates a gather list for the buffers that make up a packet.
845 * The caller must provide adequate space for the SGL that will be written.
846 * The SGL includes all of the packet's page fragments and the data in its
847 * main body except for the first @start bytes. @sgl must be 16-byte
848 * aligned and within a Tx descriptor with available space. @end points
849 * right after the end of the SGL but does not account for any potential
850 * wrap around, i.e., @end > @sgl.
851 */
852static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
853 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
854 const dma_addr_t *addr)
855{
856 unsigned int i, len;
857 struct ulptx_sge_pair *to;
858 const struct skb_shared_info *si = skb_shinfo(skb);
859 unsigned int nfrags = si->nr_frags;
860 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
861
862 len = skb_headlen(skb) - start;
863 if (likely(len)) {
864 sgl->len0 = htonl(len);
865 sgl->addr0 = cpu_to_be64(addr[0] + start);
866 nfrags++;
867 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000868 sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000869 sgl->addr0 = cpu_to_be64(addr[1]);
870 }
871
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800872 sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
873 ULPTX_NSGE_V(nfrags));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000874 if (likely(--nfrags == 0))
875 return;
876 /*
877 * Most of the complexity below deals with the possibility we hit the
878 * end of the queue in the middle of writing the SGL. For this case
879 * only we create the SGL in a temporary buffer and then copy it.
880 */
881 to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
882
883 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000884 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
885 to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000886 to->addr[0] = cpu_to_be64(addr[i]);
887 to->addr[1] = cpu_to_be64(addr[++i]);
888 }
889 if (nfrags) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000890 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000891 to->len[1] = cpu_to_be32(0);
892 to->addr[0] = cpu_to_be64(addr[i + 1]);
893 }
894 if (unlikely((u8 *)end > (u8 *)q->stat)) {
895 unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
896
897 if (likely(part0))
898 memcpy(sgl->sge, buf, part0);
899 part1 = (u8 *)end - (u8 *)q->stat;
900 memcpy(q->desc, (u8 *)buf + part0, part1);
901 end = (void *)q->desc + part1;
902 }
903 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
Joe Perches64699332012-06-04 12:44:16 +0000904 *end = 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000905}
906
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530907/* This function copies 64 byte coalesced work request to
908 * memory mapped BAR2 space. For coalesced WR SGE fetches
909 * data from the FIFO instead of from Host.
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000910 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530911static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000912{
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530913 int count = 8;
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000914
915 while (count) {
916 writeq(*src, dst);
917 src++;
918 dst++;
919 count--;
920 }
921}
922
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000923/**
924 * ring_tx_db - check and potentially ring a Tx queue's doorbell
925 * @adap: the adapter
926 * @q: the Tx queue
927 * @n: number of new descriptors to give to HW
928 *
929 * Ring the doorbel for a Tx queue.
930 */
931static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
932{
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +0530933 /* Make sure that all writes to the TX Descriptors are committed
934 * before we tell the hardware about them.
935 */
936 wmb();
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530937
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530938 /* If we don't have access to the new User Doorbell (T5+), use the old
939 * doorbell mechanism; otherwise use the new BAR2 mechanism.
940 */
941 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530942 u32 val = PIDX_V(n);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530943 unsigned long flags;
944
945 /* For T4 we need to participate in the Doorbell Recovery
946 * mechanism.
947 */
948 spin_lock_irqsave(&q->db_lock, flags);
949 if (!q->db_disabled)
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530950 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
951 QID_V(q->cntxt_id) | val);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530952 else
953 q->db_pidx_inc += n;
954 q->db_pidx = q->pidx;
955 spin_unlock_irqrestore(&q->db_lock, flags);
956 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530957 u32 val = PIDX_T5_V(n);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530958
959 /* T4 and later chips share the same PIDX field offset within
960 * the doorbell, but T5 and later shrank the field in order to
961 * gain a bit for Doorbell Priority. The field was absurdly
962 * large in the first place (14 bits) so we just use the T5
963 * and later limits and warn if a Queue ID is too large.
964 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530965 WARN_ON(val & DBPRIO_F);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530966
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530967 /* If we're only writing a single TX Descriptor and we can use
968 * Inferred QID registers, we can use the Write Combining
969 * Gather Buffer; otherwise we use the simple doorbell.
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530970 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530971 if (n == 1 && q->bar2_qid == 0) {
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530972 int index = (q->pidx
973 ? (q->pidx - 1)
974 : (q->size - 1));
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530975 u64 *wr = (u64 *)&q->desc[index];
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530976
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530977 cxgb_pio_copy((u64 __iomem *)
978 (q->bar2_addr + SGE_UDB_WCDOORBELL),
979 wr);
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000980 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530981 writel(val | QID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530982 q->bar2_addr + SGE_UDB_KDOORBELL);
Santosh Rastapur22adfe02013-03-14 05:08:51 +0000983 }
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +0530984
985 /* This Write Memory Barrier will force the write to the User
986 * Doorbell area to be flushed. This is needed to prevent
987 * writes on different CPUs for the same queue from hitting
988 * the adapter out of order. This is required when some Work
989 * Requests take the Write Combine Gather Buffer path (user
990 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
991 * take the traditional path where we simply increment the
992 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
993 * hardware DMA read the actual Work Request.
994 */
995 wmb();
996 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +0000997}
998
999/**
1000 * inline_tx_skb - inline a packet's data into Tx descriptors
1001 * @skb: the packet
1002 * @q: the Tx queue where the packet will be inlined
1003 * @pos: starting position in the Tx queue where to inline the packet
1004 *
1005 * Inline a packet's contents directly into Tx descriptors, starting at
1006 * the given position within the Tx DMA ring.
1007 * Most of the complexity of this operation is dealing with wrap arounds
1008 * in the middle of the packet we want to inline.
1009 */
1010static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
1011 void *pos)
1012{
1013 u64 *p;
1014 int left = (void *)q->stat - pos;
1015
1016 if (likely(skb->len <= left)) {
1017 if (likely(!skb->data_len))
1018 skb_copy_from_linear_data(skb, pos, skb->len);
1019 else
1020 skb_copy_bits(skb, 0, pos, skb->len);
1021 pos += skb->len;
1022 } else {
1023 skb_copy_bits(skb, 0, pos, left);
1024 skb_copy_bits(skb, left, q->desc, skb->len - left);
1025 pos = (void *)q->desc + (skb->len - left);
1026 }
1027
1028 /* 0-pad to multiple of 16 */
1029 p = PTR_ALIGN(pos, 8);
1030 if ((uintptr_t)p & 8)
1031 *p = 0;
1032}
1033
Hariprasad Shenai8d0557d2015-12-08 10:09:15 +05301034static void *inline_tx_skb_header(const struct sk_buff *skb,
1035 const struct sge_txq *q, void *pos,
1036 int length)
1037{
1038 u64 *p;
1039 int left = (void *)q->stat - pos;
1040
1041 if (likely(length <= left)) {
1042 memcpy(pos, skb->data, length);
1043 pos += length;
1044 } else {
1045 memcpy(pos, skb->data, left);
1046 memcpy(q->desc, skb->data + left, length - left);
1047 pos = (void *)q->desc + (length - left);
1048 }
1049 /* 0-pad to multiple of 16 */
1050 p = PTR_ALIGN(pos, 8);
1051 if ((uintptr_t)p & 8) {
1052 *p = 0;
1053 return p + 1;
1054 }
1055 return p;
1056}
1057
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001058/*
1059 * Figure out what HW csum a packet wants and return the appropriate control
1060 * bits.
1061 */
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301062static u64 hwcsum(enum chip_type chip, const struct sk_buff *skb)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001063{
1064 int csum_type;
1065 const struct iphdr *iph = ip_hdr(skb);
1066
1067 if (iph->version == 4) {
1068 if (iph->protocol == IPPROTO_TCP)
1069 csum_type = TX_CSUM_TCPIP;
1070 else if (iph->protocol == IPPROTO_UDP)
1071 csum_type = TX_CSUM_UDPIP;
1072 else {
1073nocsum: /*
1074 * unknown protocol, disable HW csum
1075 * and hope a bad packet is detected
1076 */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301077 return TXPKT_L4CSUM_DIS_F;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001078 }
1079 } else {
1080 /*
1081 * this doesn't work with extension headers
1082 */
1083 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
1084
1085 if (ip6h->nexthdr == IPPROTO_TCP)
1086 csum_type = TX_CSUM_TCPIP6;
1087 else if (ip6h->nexthdr == IPPROTO_UDP)
1088 csum_type = TX_CSUM_UDPIP6;
1089 else
1090 goto nocsum;
1091 }
1092
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301093 if (likely(csum_type >= TX_CSUM_TCPIP)) {
1094 u64 hdr_len = TXPKT_IPHDR_LEN_V(skb_network_header_len(skb));
1095 int eth_hdr_len = skb_network_offset(skb) - ETH_HLEN;
1096
1097 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1098 hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len);
1099 else
1100 hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len);
1101 return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len;
1102 } else {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001103 int start = skb_transport_offset(skb);
1104
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301105 return TXPKT_CSUM_TYPE_V(csum_type) |
1106 TXPKT_CSUM_START_V(start) |
1107 TXPKT_CSUM_LOC_V(start + skb->csum_offset);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001108 }
1109}
1110
1111static void eth_txq_stop(struct sge_eth_txq *q)
1112{
1113 netif_tx_stop_queue(q->txq);
1114 q->q.stops++;
1115}
1116
1117static inline void txq_advance(struct sge_txq *q, unsigned int n)
1118{
1119 q->in_use += n;
1120 q->pidx += n;
1121 if (q->pidx >= q->size)
1122 q->pidx -= q->size;
1123}
1124
Varun Prakash84a200b2015-03-24 19:14:46 +05301125#ifdef CONFIG_CHELSIO_T4_FCOE
1126static inline int
1127cxgb_fcoe_offload(struct sk_buff *skb, struct adapter *adap,
1128 const struct port_info *pi, u64 *cntrl)
1129{
1130 const struct cxgb_fcoe *fcoe = &pi->fcoe;
1131
1132 if (!(fcoe->flags & CXGB_FCOE_ENABLED))
1133 return 0;
1134
1135 if (skb->protocol != htons(ETH_P_FCOE))
1136 return 0;
1137
1138 skb_reset_mac_header(skb);
1139 skb->mac_len = sizeof(struct ethhdr);
1140
1141 skb_set_network_header(skb, skb->mac_len);
1142 skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
1143
1144 if (!cxgb_fcoe_sof_eof_supported(adap, skb))
1145 return -ENOTSUPP;
1146
1147 /* FC CRC offload */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301148 *cntrl = TXPKT_CSUM_TYPE_V(TX_CSUM_FCOE) |
1149 TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F |
1150 TXPKT_CSUM_START_V(CXGB_FCOE_TXPKT_CSUM_START) |
1151 TXPKT_CSUM_END_V(CXGB_FCOE_TXPKT_CSUM_END) |
1152 TXPKT_CSUM_LOC_V(CXGB_FCOE_TXPKT_CSUM_END);
Varun Prakash84a200b2015-03-24 19:14:46 +05301153 return 0;
1154}
1155#endif /* CONFIG_CHELSIO_T4_FCOE */
1156
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001157/**
1158 * t4_eth_xmit - add a packet to an Ethernet Tx queue
1159 * @skb: the packet
1160 * @dev: the egress net device
1161 *
1162 * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
1163 */
1164netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1165{
Atul Guptaa45695042017-07-04 16:46:20 +05301166 u32 wr_mid, ctrl0, op;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001167 u64 cntrl, *end;
1168 int qidx, credits;
1169 unsigned int flits, ndesc;
1170 struct adapter *adap;
1171 struct sge_eth_txq *q;
1172 const struct port_info *pi;
1173 struct fw_eth_tx_pkt_wr *wr;
1174 struct cpl_tx_pkt_core *cpl;
1175 const struct skb_shared_info *ssi;
1176 dma_addr_t addr[MAX_SKB_FRAGS + 1];
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301177 bool immediate = false;
Hariprasad Shenai637d3e92015-05-05 14:59:56 +05301178 int len, max_pkt_len;
Atul Guptaa45695042017-07-04 16:46:20 +05301179 bool ptp_enabled = is_ptp_enabled(skb, dev);
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);
Atul Guptaa45695042017-07-04 16:46:20 +05301203 if (ptp_enabled) {
1204 spin_lock(&adap->ptp_lock);
1205 if (!(adap->ptp_tx_skb)) {
1206 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1207 adap->ptp_tx_skb = skb_get(skb);
1208 } else {
1209 spin_unlock(&adap->ptp_lock);
1210 goto out_free;
1211 }
1212 q = &adap->sge.ptptxq;
1213 } else {
1214 q = &adap->sge.ethtxq[qidx + pi->first_qset];
1215 }
1216 skb_tx_timestamp(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001217
1218 reclaim_completed_tx(adap, &q->q, true);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301219 cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
Varun Prakash84a200b2015-03-24 19:14:46 +05301220
1221#ifdef CONFIG_CHELSIO_T4_FCOE
1222 err = cxgb_fcoe_offload(skb, adap, pi, &cntrl);
Atul Guptaa45695042017-07-04 16:46:20 +05301223 if (unlikely(err == -ENOTSUPP)) {
1224 if (ptp_enabled)
1225 spin_unlock(&adap->ptp_lock);
Varun Prakash84a200b2015-03-24 19:14:46 +05301226 goto out_free;
Atul Guptaa45695042017-07-04 16:46:20 +05301227 }
Varun Prakash84a200b2015-03-24 19:14:46 +05301228#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001229
1230 flits = calc_tx_flits(skb);
1231 ndesc = flits_to_desc(flits);
1232 credits = txq_avail(&q->q) - ndesc;
1233
1234 if (unlikely(credits < 0)) {
1235 eth_txq_stop(q);
1236 dev_err(adap->pdev_dev,
1237 "%s: Tx ring %u full while queue awake!\n",
1238 dev->name, qidx);
Atul Guptaa45695042017-07-04 16:46:20 +05301239 if (ptp_enabled)
1240 spin_unlock(&adap->ptp_lock);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001241 return NETDEV_TX_BUSY;
1242 }
1243
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301244 if (is_eth_imm(skb))
1245 immediate = true;
1246
1247 if (!immediate &&
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001248 unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
1249 q->mapping_err++;
Atul Guptaa45695042017-07-04 16:46:20 +05301250 if (ptp_enabled)
1251 spin_unlock(&adap->ptp_lock);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001252 goto out_free;
1253 }
1254
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301255 wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001256 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1257 eth_txq_stop(q);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301258 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001259 }
1260
1261 wr = (void *)&q->q.desc[q->q.pidx];
1262 wr->equiq_to_len16 = htonl(wr_mid);
1263 wr->r3 = cpu_to_be64(0);
1264 end = (u64 *)wr + flits;
1265
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301266 len = immediate ? skb->len : 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001267 ssi = skb_shinfo(skb);
1268 if (ssi->gso_size) {
Dimitris Michailidis625ac6a2010-08-02 13:19:18 +00001269 struct cpl_tx_pkt_lso *lso = (void *)wr;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001270 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1271 int l3hdr_len = skb_network_header_len(skb);
1272 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1273
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301274 len += sizeof(*lso);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301275 wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
1276 FW_WR_IMMDLEN_V(len));
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301277 lso->c.lso_ctrl = htonl(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
1278 LSO_FIRST_SLICE_F | LSO_LAST_SLICE_F |
1279 LSO_IPV6_V(v6) |
1280 LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
1281 LSO_IPHDR_LEN_V(l3hdr_len / 4) |
1282 LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
Dimitris Michailidis625ac6a2010-08-02 13:19:18 +00001283 lso->c.ipid_ofst = htons(0);
1284 lso->c.mss = htons(ssi->gso_size);
1285 lso->c.seqno_offset = htonl(0);
Hariprasad Shenai7207c0d2014-10-09 05:48:45 +05301286 if (is_t4(adap->params.chip))
1287 lso->c.len = htonl(skb->len);
1288 else
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301289 lso->c.len = htonl(LSO_T5_XFER_SIZE_V(skb->len));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001290 cpl = (void *)(lso + 1);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301291
1292 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1293 cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
1294 else
1295 cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
1296
1297 cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
1298 TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
1299 TXPKT_IPHDR_LEN_V(l3hdr_len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001300 q->tso++;
1301 q->tx_cso += ssi->gso_segs;
1302 } else {
Kumar Sanghvica71de62014-03-13 20:50:50 +05301303 len += sizeof(*cpl);
Atul Guptaa45695042017-07-04 16:46:20 +05301304 if (ptp_enabled)
1305 op = FW_PTP_TX_PKT_WR;
1306 else
1307 op = FW_ETH_TX_PKT_WR;
1308 wr->op_immdlen = htonl(FW_WR_OP_V(op) |
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301309 FW_WR_IMMDLEN_V(len));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001310 cpl = (void *)(wr + 1);
1311 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301312 cntrl = hwcsum(adap->params.chip, skb) |
1313 TXPKT_IPCSUM_DIS_F;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001314 q->tx_cso++;
Varun Prakash84a200b2015-03-24 19:14:46 +05301315 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001316 }
1317
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001318 if (skb_vlan_tag_present(skb)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001319 q->vlan_ins++;
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301320 cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
Varun Prakash84a200b2015-03-24 19:14:46 +05301321#ifdef CONFIG_CHELSIO_T4_FCOE
1322 if (skb->protocol == htons(ETH_P_FCOE))
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301323 cntrl |= TXPKT_VLAN_V(
Varun Prakash84a200b2015-03-24 19:14:46 +05301324 ((skb->priority & 0x7) << VLAN_PRIO_SHIFT));
1325#endif /* CONFIG_CHELSIO_T4_FCOE */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001326 }
1327
Anish Bhatt397665d2015-07-17 13:12:33 -07001328 ctrl0 = TXPKT_OPCODE_V(CPL_TX_PKT_XT) | TXPKT_INTF_V(pi->tx_chan) |
1329 TXPKT_PF_V(adap->pf);
Atul Guptaa45695042017-07-04 16:46:20 +05301330 if (ptp_enabled)
1331 ctrl0 |= TXPKT_TSTAMP_F;
Anish Bhatt397665d2015-07-17 13:12:33 -07001332#ifdef CONFIG_CHELSIO_T4_DCB
1333 if (is_t4(adap->params.chip))
1334 ctrl0 |= TXPKT_OVLAN_IDX_V(q->dcb_prio);
1335 else
1336 ctrl0 |= TXPKT_T5_OVLAN_IDX_V(q->dcb_prio);
1337#endif
1338 cpl->ctrl0 = htonl(ctrl0);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001339 cpl->pack = htons(0);
1340 cpl->len = htons(skb->len);
1341 cpl->ctrl1 = cpu_to_be64(cntrl);
1342
Kumar Sanghvi0034b292014-02-18 17:56:14 +05301343 if (immediate) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001344 inline_tx_skb(skb, &q->q, cpl + 1);
Eric W. Biedermana7525192014-03-15 16:29:49 -07001345 dev_consume_skb_any(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001346 } else {
1347 int last_desc;
1348
1349 write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
1350 addr);
1351 skb_orphan(skb);
1352
1353 last_desc = q->q.pidx + ndesc - 1;
1354 if (last_desc >= q->q.size)
1355 last_desc -= q->q.size;
1356 q->q.sdesc[last_desc].skb = skb;
1357 q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
1358 }
1359
1360 txq_advance(&q->q, ndesc);
1361
1362 ring_tx_db(adap, &q->q, ndesc);
Atul Guptaa45695042017-07-04 16:46:20 +05301363 if (ptp_enabled)
1364 spin_unlock(&adap->ptp_lock);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001365 return NETDEV_TX_OK;
1366}
1367
1368/**
1369 * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
1370 * @q: the SGE control Tx queue
1371 *
1372 * This is a variant of reclaim_completed_tx() that is used for Tx queues
1373 * that send only immediate data (presently just the control queues) and
1374 * thus do not have any sk_buffs to release.
1375 */
1376static inline void reclaim_completed_tx_imm(struct sge_txq *q)
1377{
Hariprasad Shenai632be192015-12-08 10:09:13 +05301378 int hw_cidx = ntohs(ACCESS_ONCE(q->stat->cidx));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001379 int reclaim = hw_cidx - q->cidx;
1380
1381 if (reclaim < 0)
1382 reclaim += q->size;
1383
1384 q->in_use -= reclaim;
1385 q->cidx = hw_cidx;
1386}
1387
1388/**
1389 * is_imm - check whether a packet can be sent as immediate data
1390 * @skb: the packet
1391 *
1392 * Returns true if a packet can be sent as a WR with immediate data.
1393 */
1394static inline int is_imm(const struct sk_buff *skb)
1395{
1396 return skb->len <= MAX_CTRL_WR_LEN;
1397}
1398
1399/**
1400 * ctrlq_check_stop - check if a control queue is full and should stop
1401 * @q: the queue
1402 * @wr: most recent WR written to the queue
1403 *
1404 * Check if a control queue has become full and should be stopped.
1405 * We clean up control queue descriptors very lazily, only when we are out.
1406 * If the queue is still full after reclaiming any completed descriptors
1407 * we suspend it and have the last WR wake it up.
1408 */
1409static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
1410{
1411 reclaim_completed_tx_imm(&q->q);
1412 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301413 wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001414 q->q.stops++;
1415 q->full = 1;
1416 }
1417}
1418
1419/**
1420 * ctrl_xmit - send a packet through an SGE control Tx queue
1421 * @q: the control queue
1422 * @skb: the packet
1423 *
1424 * Send a packet through an SGE control Tx queue. Packets sent through
1425 * a control queue must fit entirely as immediate data.
1426 */
1427static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
1428{
1429 unsigned int ndesc;
1430 struct fw_wr_hdr *wr;
1431
1432 if (unlikely(!is_imm(skb))) {
1433 WARN_ON(1);
1434 dev_kfree_skb(skb);
1435 return NET_XMIT_DROP;
1436 }
1437
1438 ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
1439 spin_lock(&q->sendq.lock);
1440
1441 if (unlikely(q->full)) {
1442 skb->priority = ndesc; /* save for restart */
1443 __skb_queue_tail(&q->sendq, skb);
1444 spin_unlock(&q->sendq.lock);
1445 return NET_XMIT_CN;
1446 }
1447
1448 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1449 inline_tx_skb(skb, &q->q, wr);
1450
1451 txq_advance(&q->q, ndesc);
1452 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
1453 ctrlq_check_stop(q, wr);
1454
1455 ring_tx_db(q->adap, &q->q, ndesc);
1456 spin_unlock(&q->sendq.lock);
1457
1458 kfree_skb(skb);
1459 return NET_XMIT_SUCCESS;
1460}
1461
1462/**
1463 * restart_ctrlq - restart a suspended control queue
1464 * @data: the control queue to restart
1465 *
1466 * Resumes transmission on a suspended Tx control queue.
1467 */
1468static void restart_ctrlq(unsigned long data)
1469{
1470 struct sk_buff *skb;
1471 unsigned int written = 0;
1472 struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
1473
1474 spin_lock(&q->sendq.lock);
1475 reclaim_completed_tx_imm(&q->q);
1476 BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
1477
1478 while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
1479 struct fw_wr_hdr *wr;
1480 unsigned int ndesc = skb->priority; /* previously saved */
1481
Hariprasad Shenaia4011fd2015-08-12 16:55:07 +05301482 written += ndesc;
1483 /* Write descriptors and free skbs outside the lock to limit
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001484 * wait times. q->full is still set so new skbs will be queued.
1485 */
Hariprasad Shenaia4011fd2015-08-12 16:55:07 +05301486 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1487 txq_advance(&q->q, ndesc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001488 spin_unlock(&q->sendq.lock);
1489
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001490 inline_tx_skb(skb, &q->q, wr);
1491 kfree_skb(skb);
1492
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001493 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1494 unsigned long old = q->q.stops;
1495
1496 ctrlq_check_stop(q, wr);
1497 if (q->q.stops != old) { /* suspended anew */
1498 spin_lock(&q->sendq.lock);
1499 goto ringdb;
1500 }
1501 }
1502 if (written > 16) {
1503 ring_tx_db(q->adap, &q->q, written);
1504 written = 0;
1505 }
1506 spin_lock(&q->sendq.lock);
1507 }
1508 q->full = 0;
1509ringdb: if (written)
1510 ring_tx_db(q->adap, &q->q, written);
1511 spin_unlock(&q->sendq.lock);
1512}
1513
1514/**
1515 * t4_mgmt_tx - send a management message
1516 * @adap: the adapter
1517 * @skb: the packet containing the management message
1518 *
1519 * Send a management message through control queue 0.
1520 */
1521int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
1522{
1523 int ret;
1524
1525 local_bh_disable();
1526 ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
1527 local_bh_enable();
1528 return ret;
1529}
1530
1531/**
1532 * is_ofld_imm - check whether a packet can be sent as immediate data
1533 * @skb: the packet
1534 *
1535 * Returns true if a packet can be sent as an offload WR with immediate
1536 * data. We currently use the same limit as for Ethernet packets.
1537 */
1538static inline int is_ofld_imm(const struct sk_buff *skb)
1539{
1540 return skb->len <= MAX_IMM_TX_PKT_LEN;
1541}
1542
1543/**
1544 * calc_tx_flits_ofld - calculate # of flits for an offload packet
1545 * @skb: the packet
1546 *
1547 * Returns the number of flits needed for the given offload packet.
1548 * These packets are already fully constructed and no additional headers
1549 * will be added.
1550 */
1551static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
1552{
1553 unsigned int flits, cnt;
1554
1555 if (is_ofld_imm(skb))
1556 return DIV_ROUND_UP(skb->len, 8);
1557
1558 flits = skb_transport_offset(skb) / 8U; /* headers */
1559 cnt = skb_shinfo(skb)->nr_frags;
Li RongQing15dd16c2013-06-03 22:11:16 +00001560 if (skb_tail_pointer(skb) != skb_transport_header(skb))
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001561 cnt++;
1562 return flits + sgl_len(cnt);
1563}
1564
1565/**
1566 * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
1567 * @adap: the adapter
1568 * @q: the queue to stop
1569 *
1570 * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
1571 * inability to map packets. A periodic timer attempts to restart
1572 * queues so marked.
1573 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301574static void txq_stop_maperr(struct sge_uld_txq *q)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001575{
1576 q->mapping_err++;
1577 q->q.stops++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001578 set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
1579 q->adap->sge.txq_maperr);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001580}
1581
1582/**
1583 * ofldtxq_stop - stop an offload Tx queue that has become full
1584 * @q: the queue to stop
1585 * @skb: the packet causing the queue to become full
1586 *
1587 * Stops an offload Tx queue that has become full and modifies the packet
1588 * being written to request a wakeup.
1589 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301590static void ofldtxq_stop(struct sge_uld_txq *q, struct sk_buff *skb)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001591{
1592 struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
1593
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301594 wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001595 q->q.stops++;
1596 q->full = 1;
1597}
1598
1599/**
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301600 * service_ofldq - service/restart a suspended offload queue
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001601 * @q: the offload queue
1602 *
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301603 * Services an offload Tx queue by moving packets from its Pending Send
1604 * Queue to the Hardware TX ring. The function starts and ends with the
1605 * Send Queue locked, but drops the lock while putting the skb at the
1606 * head of the Send Queue onto the Hardware TX Ring. Dropping the lock
1607 * allows more skbs to be added to the Send Queue by other threads.
1608 * The packet being processed at the head of the Pending Send Queue is
1609 * left on the queue in case we experience DMA Mapping errors, etc.
1610 * and need to give up and restart later.
1611 *
1612 * service_ofldq() can be thought of as a task which opportunistically
1613 * uses other threads execution contexts. We use the Offload Queue
1614 * boolean "service_ofldq_running" to make sure that only one instance
1615 * is ever running at a time ...
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001616 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301617static void service_ofldq(struct sge_uld_txq *q)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001618{
Hariprasad Shenai8d0557d2015-12-08 10:09:15 +05301619 u64 *pos, *before, *end;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001620 int credits;
1621 struct sk_buff *skb;
Hariprasad Shenai8d0557d2015-12-08 10:09:15 +05301622 struct sge_txq *txq;
1623 unsigned int left;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001624 unsigned int written = 0;
1625 unsigned int flits, ndesc;
1626
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301627 /* If another thread is currently in service_ofldq() processing the
1628 * Pending Send Queue then there's nothing to do. Otherwise, flag
1629 * that we're doing the work and continue. Examining/modifying
1630 * the Offload Queue boolean "service_ofldq_running" must be done
1631 * while holding the Pending Send Queue Lock.
1632 */
1633 if (q->service_ofldq_running)
1634 return;
1635 q->service_ofldq_running = true;
1636
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001637 while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301638 /* We drop the lock while we're working with the skb at the
1639 * head of the Pending Send Queue. This allows more skbs to
1640 * be added to the Pending Send Queue while we're working on
1641 * this one. We don't need to lock to guard the TX Ring
1642 * updates because only one thread of execution is ever
1643 * allowed into service_ofldq() at a time.
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001644 */
1645 spin_unlock(&q->sendq.lock);
1646
1647 reclaim_completed_tx(q->adap, &q->q, false);
1648
1649 flits = skb->priority; /* previously saved */
1650 ndesc = flits_to_desc(flits);
1651 credits = txq_avail(&q->q) - ndesc;
1652 BUG_ON(credits < 0);
1653 if (unlikely(credits < TXQ_STOP_THRES))
1654 ofldtxq_stop(q, skb);
1655
1656 pos = (u64 *)&q->q.desc[q->q.pidx];
1657 if (is_ofld_imm(skb))
1658 inline_tx_skb(skb, &q->q, pos);
1659 else if (map_skb(q->adap->pdev_dev, skb,
1660 (dma_addr_t *)skb->head)) {
1661 txq_stop_maperr(q);
1662 spin_lock(&q->sendq.lock);
1663 break;
1664 } else {
1665 int last_desc, hdr_len = skb_transport_offset(skb);
1666
Hariprasad Shenai8d0557d2015-12-08 10:09:15 +05301667 /* The WR headers may not fit within one descriptor.
1668 * So we need to deal with wrap-around here.
1669 */
1670 before = (u64 *)pos;
1671 end = (u64 *)pos + flits;
1672 txq = &q->q;
1673 pos = (void *)inline_tx_skb_header(skb, &q->q,
1674 (void *)pos,
1675 hdr_len);
1676 if (before > (u64 *)pos) {
1677 left = (u8 *)end - (u8 *)txq->stat;
1678 end = (void *)txq->desc + left;
1679 }
1680
1681 /* If current position is already at the end of the
1682 * ofld queue, reset the current to point to
1683 * start of the queue and update the end ptr as well.
1684 */
1685 if (pos == (u64 *)txq->stat) {
1686 left = (u8 *)end - (u8 *)txq->stat;
1687 end = (void *)txq->desc + left;
1688 pos = (void *)txq->desc;
1689 }
1690
1691 write_sgl(skb, &q->q, (void *)pos,
1692 end, hdr_len,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001693 (dma_addr_t *)skb->head);
1694#ifdef CONFIG_NEED_DMA_MAP_STATE
1695 skb->dev = q->adap->port[0];
1696 skb->destructor = deferred_unmap_destructor;
1697#endif
1698 last_desc = q->q.pidx + ndesc - 1;
1699 if (last_desc >= q->q.size)
1700 last_desc -= q->q.size;
1701 q->q.sdesc[last_desc].skb = skb;
1702 }
1703
1704 txq_advance(&q->q, ndesc);
1705 written += ndesc;
1706 if (unlikely(written > 32)) {
1707 ring_tx_db(q->adap, &q->q, written);
1708 written = 0;
1709 }
1710
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301711 /* Reacquire the Pending Send Queue Lock so we can unlink the
1712 * skb we've just successfully transferred to the TX Ring and
1713 * loop for the next skb which may be at the head of the
1714 * Pending Send Queue.
1715 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001716 spin_lock(&q->sendq.lock);
1717 __skb_unlink(skb, &q->sendq);
1718 if (is_ofld_imm(skb))
1719 kfree_skb(skb);
1720 }
1721 if (likely(written))
1722 ring_tx_db(q->adap, &q->q, written);
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301723
1724 /*Indicate that no thread is processing the Pending Send Queue
1725 * currently.
1726 */
1727 q->service_ofldq_running = false;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001728}
1729
1730/**
1731 * ofld_xmit - send a packet through an offload queue
1732 * @q: the Tx offload queue
1733 * @skb: the packet
1734 *
1735 * Send an offload packet through an SGE offload queue.
1736 */
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301737static int ofld_xmit(struct sge_uld_txq *q, struct sk_buff *skb)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001738{
1739 skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
1740 spin_lock(&q->sendq.lock);
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301741
1742 /* Queue the new skb onto the Offload Queue's Pending Send Queue. If
1743 * that results in this new skb being the only one on the queue, start
1744 * servicing it. If there are other skbs already on the list, then
1745 * either the queue is currently being processed or it's been stopped
1746 * for some reason and it'll be restarted at a later time. Restart
1747 * paths are triggered by events like experiencing a DMA Mapping Error
1748 * or filling the Hardware TX Ring.
1749 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001750 __skb_queue_tail(&q->sendq, skb);
1751 if (q->sendq.qlen == 1)
1752 service_ofldq(q);
Hariprasad Shenai126fca62015-12-08 10:09:14 +05301753
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001754 spin_unlock(&q->sendq.lock);
1755 return NET_XMIT_SUCCESS;
1756}
1757
1758/**
1759 * restart_ofldq - restart a suspended offload queue
1760 * @data: the offload queue to restart
1761 *
1762 * Resumes transmission on a suspended Tx offload queue.
1763 */
1764static void restart_ofldq(unsigned long data)
1765{
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301766 struct sge_uld_txq *q = (struct sge_uld_txq *)data;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001767
1768 spin_lock(&q->sendq.lock);
1769 q->full = 0; /* the queue actually is completely empty now */
1770 service_ofldq(q);
1771 spin_unlock(&q->sendq.lock);
1772}
1773
1774/**
1775 * skb_txq - return the Tx queue an offload packet should use
1776 * @skb: the packet
1777 *
1778 * Returns the Tx queue an offload packet should use as indicated by bits
1779 * 1-15 in the packet's queue_mapping.
1780 */
1781static inline unsigned int skb_txq(const struct sk_buff *skb)
1782{
1783 return skb->queue_mapping >> 1;
1784}
1785
1786/**
1787 * is_ctrl_pkt - return whether an offload packet is a control packet
1788 * @skb: the packet
1789 *
1790 * Returns whether an offload packet should use an OFLD or a CTRL
1791 * Tx queue as indicated by bit 0 in the packet's queue_mapping.
1792 */
1793static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
1794{
1795 return skb->queue_mapping & 1;
1796}
1797
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301798static inline int uld_send(struct adapter *adap, struct sk_buff *skb,
1799 unsigned int tx_uld_type)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001800{
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301801 struct sge_uld_txq_info *txq_info;
1802 struct sge_uld_txq *txq;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001803 unsigned int idx = skb_txq(skb);
1804
Kumar Sanghvi4fe44dd2014-02-18 17:56:11 +05301805 if (unlikely(is_ctrl_pkt(skb))) {
1806 /* Single ctrl queue is a requirement for LE workaround path */
1807 if (adap->tids.nsftids)
1808 idx = 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001809 return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
Kumar Sanghvi4fe44dd2014-02-18 17:56:11 +05301810 }
Arjun V0d4b7292017-02-02 12:43:29 +05301811
1812 txq_info = adap->sge.uld_txq_info[tx_uld_type];
1813 if (unlikely(!txq_info)) {
1814 WARN_ON(true);
1815 return NET_XMIT_DROP;
1816 }
1817
1818 txq = &txq_info->uldtxq[idx];
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301819 return ofld_xmit(txq, skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001820}
1821
1822/**
1823 * t4_ofld_send - send an offload packet
1824 * @adap: the adapter
1825 * @skb: the packet
1826 *
1827 * Sends an offload 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 */
1831int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
1832{
1833 int ret;
1834
1835 local_bh_disable();
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301836 ret = uld_send(adap, skb, CXGB4_TX_OFLD);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001837 local_bh_enable();
1838 return ret;
1839}
1840
1841/**
1842 * cxgb4_ofld_send - send an offload packet
1843 * @dev: the net device
1844 * @skb: the packet
1845 *
1846 * Sends an offload packet. This is an exported version of @t4_ofld_send,
1847 * intended for ULDs.
1848 */
1849int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
1850{
1851 return t4_ofld_send(netdev2adap(dev), skb);
1852}
1853EXPORT_SYMBOL(cxgb4_ofld_send);
1854
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301855/**
1856 * t4_crypto_send - send crypto packet
1857 * @adap: the adapter
1858 * @skb: the packet
1859 *
1860 * Sends crypto packet. We use the packet queue_mapping to select the
1861 * appropriate Tx queue as follows: bit 0 indicates whether the packet
1862 * should be sent as regular or control, bits 1-15 select the queue.
1863 */
1864static int t4_crypto_send(struct adapter *adap, struct sk_buff *skb)
1865{
1866 int ret;
1867
1868 local_bh_disable();
1869 ret = uld_send(adap, skb, CXGB4_TX_CRYPTO);
1870 local_bh_enable();
1871 return ret;
1872}
1873
1874/**
1875 * cxgb4_crypto_send - send crypto packet
1876 * @dev: the net device
1877 * @skb: the packet
1878 *
1879 * Sends crypto packet. This is an exported version of @t4_crypto_send,
1880 * intended for ULDs.
1881 */
1882int cxgb4_crypto_send(struct net_device *dev, struct sk_buff *skb)
1883{
1884 return t4_crypto_send(netdev2adap(dev), skb);
1885}
1886EXPORT_SYMBOL(cxgb4_crypto_send);
1887
Ian Campbelle91b0f22011-10-19 23:01:46 +00001888static inline void copy_frags(struct sk_buff *skb,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001889 const struct pkt_gl *gl, unsigned int offset)
1890{
Ian Campbelle91b0f22011-10-19 23:01:46 +00001891 int i;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001892
1893 /* usually there's just one frag */
Ian Campbelle91b0f22011-10-19 23:01:46 +00001894 __skb_fill_page_desc(skb, 0, gl->frags[0].page,
1895 gl->frags[0].offset + offset,
1896 gl->frags[0].size - offset);
1897 skb_shinfo(skb)->nr_frags = gl->nfrags;
1898 for (i = 1; i < gl->nfrags; i++)
1899 __skb_fill_page_desc(skb, i, gl->frags[i].page,
1900 gl->frags[i].offset,
1901 gl->frags[i].size);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001902
1903 /* get a reference to the last page, we don't own it */
Ian Campbelle91b0f22011-10-19 23:01:46 +00001904 get_page(gl->frags[gl->nfrags - 1].page);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001905}
1906
1907/**
1908 * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
1909 * @gl: the gather list
1910 * @skb_len: size of sk_buff main body if it carries fragments
1911 * @pull_len: amount of data to move to the sk_buff's main body
1912 *
1913 * Builds an sk_buff from the given packet gather list. Returns the
1914 * sk_buff or %NULL if sk_buff allocation failed.
1915 */
1916struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
1917 unsigned int skb_len, unsigned int pull_len)
1918{
1919 struct sk_buff *skb;
1920
1921 /*
1922 * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
1923 * size, which is expected since buffers are at least PAGE_SIZEd.
1924 * In this case packets up to RX_COPY_THRES have only one fragment.
1925 */
1926 if (gl->tot_len <= RX_COPY_THRES) {
1927 skb = dev_alloc_skb(gl->tot_len);
1928 if (unlikely(!skb))
1929 goto out;
1930 __skb_put(skb, gl->tot_len);
1931 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1932 } else {
1933 skb = dev_alloc_skb(skb_len);
1934 if (unlikely(!skb))
1935 goto out;
1936 __skb_put(skb, pull_len);
1937 skb_copy_to_linear_data(skb, gl->va, pull_len);
1938
Ian Campbelle91b0f22011-10-19 23:01:46 +00001939 copy_frags(skb, gl, pull_len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001940 skb->len = gl->tot_len;
1941 skb->data_len = skb->len - pull_len;
1942 skb->truesize += skb->data_len;
1943 }
1944out: return skb;
1945}
1946EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
1947
1948/**
1949 * t4_pktgl_free - free a packet gather list
1950 * @gl: the gather list
1951 *
1952 * Releases the pages of a packet gather list. We do not own the last
1953 * page on the list and do not free it.
1954 */
Roland Dreierde498c82010-04-21 08:59:17 +00001955static void t4_pktgl_free(const struct pkt_gl *gl)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001956{
1957 int n;
Ian Campbelle91b0f22011-10-19 23:01:46 +00001958 const struct page_frag *p;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001959
1960 for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
1961 put_page(p->page);
1962}
1963
1964/*
1965 * Process an MPS trace packet. Give it an unused protocol number so it won't
1966 * be delivered to anyone and send it to the stack for capture.
1967 */
1968static noinline int handle_trace_pkt(struct adapter *adap,
1969 const struct pkt_gl *gl)
1970{
1971 struct sk_buff *skb;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001972
1973 skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
1974 if (unlikely(!skb)) {
1975 t4_pktgl_free(gl);
1976 return 0;
1977 }
1978
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301979 if (is_t4(adap->params.chip))
Santosh Rastapur0a57a532013-03-14 05:08:49 +00001980 __skb_pull(skb, sizeof(struct cpl_trace_pkt));
1981 else
1982 __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
1983
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00001984 skb_reset_mac_header(skb);
1985 skb->protocol = htons(0xffff);
1986 skb->dev = adap->port[0];
1987 netif_receive_skb(skb);
1988 return 0;
1989}
1990
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05301991/**
1992 * cxgb4_sgetim_to_hwtstamp - convert sge time stamp to hw time stamp
1993 * @adap: the adapter
1994 * @hwtstamps: time stamp structure to update
1995 * @sgetstamp: 60bit iqe timestamp
1996 *
1997 * Every ingress queue entry has the 60-bit timestamp, convert that timestamp
1998 * which is in Core Clock ticks into ktime_t and assign it
1999 **/
2000static void cxgb4_sgetim_to_hwtstamp(struct adapter *adap,
2001 struct skb_shared_hwtstamps *hwtstamps,
2002 u64 sgetstamp)
2003{
2004 u64 ns;
2005 u64 tmp = (sgetstamp * 1000 * 1000 + adap->params.vpd.cclk / 2);
2006
2007 ns = div_u64(tmp, adap->params.vpd.cclk);
2008
2009 memset(hwtstamps, 0, sizeof(*hwtstamps));
2010 hwtstamps->hwtstamp = ns_to_ktime(ns);
2011}
2012
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002013static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
2014 const struct cpl_rx_pkt *pkt)
2015{
Vipul Pandya52367a72012-09-26 02:39:38 +00002016 struct adapter *adapter = rxq->rspq.adap;
2017 struct sge *s = &adapter->sge;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302018 struct port_info *pi;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002019 int ret;
2020 struct sk_buff *skb;
2021
2022 skb = napi_get_frags(&rxq->rspq.napi);
2023 if (unlikely(!skb)) {
2024 t4_pktgl_free(gl);
2025 rxq->stats.rx_drops++;
2026 return;
2027 }
2028
Vipul Pandya52367a72012-09-26 02:39:38 +00002029 copy_frags(skb, gl, s->pktshift);
2030 skb->len = gl->tot_len - s->pktshift;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002031 skb->data_len = skb->len;
2032 skb->truesize += skb->data_len;
2033 skb->ip_summed = CHECKSUM_UNNECESSARY;
2034 skb_record_rx_queue(skb, rxq->rspq.idx);
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302035 pi = netdev_priv(skb->dev);
2036 if (pi->rxtstamp)
2037 cxgb4_sgetim_to_hwtstamp(adapter, skb_hwtstamps(skb),
2038 gl->sgetstamp);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07002039 if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
Tom Herbert82649892013-12-17 23:23:29 -08002040 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
2041 PKT_HASH_TYPE_L3);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002042
2043 if (unlikely(pkt->vlan_ex)) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002044 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002045 rxq->stats.vlan_ex++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002046 }
2047 ret = napi_gro_frags(&rxq->rspq.napi);
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00002048 if (ret == GRO_HELD)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002049 rxq->stats.lro_pkts++;
2050 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
2051 rxq->stats.lro_merged++;
2052 rxq->stats.pkts++;
2053 rxq->stats.rx_cso++;
2054}
2055
Atul Guptaa45695042017-07-04 16:46:20 +05302056enum {
2057 RX_NON_PTP_PKT = 0,
2058 RX_PTP_PKT_SUC = 1,
2059 RX_PTP_PKT_ERR = 2
2060};
2061
2062/**
2063 * t4_systim_to_hwstamp - read hardware time stamp
2064 * @adap: the adapter
2065 * @skb: the packet
2066 *
2067 * Read Time Stamp from MPS packet and insert in skb which
2068 * is forwarded to PTP application
2069 */
2070static noinline int t4_systim_to_hwstamp(struct adapter *adapter,
2071 struct sk_buff *skb)
2072{
2073 struct skb_shared_hwtstamps *hwtstamps;
2074 struct cpl_rx_mps_pkt *cpl = NULL;
2075 unsigned char *data;
2076 int offset;
2077
2078 cpl = (struct cpl_rx_mps_pkt *)skb->data;
2079 if (!(CPL_RX_MPS_PKT_TYPE_G(ntohl(cpl->op_to_r1_hi)) &
2080 X_CPL_RX_MPS_PKT_TYPE_PTP))
2081 return RX_PTP_PKT_ERR;
2082
2083 data = skb->data + sizeof(*cpl);
2084 skb_pull(skb, 2 * sizeof(u64) + sizeof(struct cpl_rx_mps_pkt));
2085 offset = ETH_HLEN + IPV4_HLEN(skb->data) + UDP_HLEN;
2086 if (skb->len < offset + OFF_PTP_SEQUENCE_ID + sizeof(short))
2087 return RX_PTP_PKT_ERR;
2088
2089 hwtstamps = skb_hwtstamps(skb);
2090 memset(hwtstamps, 0, sizeof(*hwtstamps));
2091 hwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*((u64 *)data)));
2092
2093 return RX_PTP_PKT_SUC;
2094}
2095
2096/**
2097 * t4_rx_hststamp - Recv PTP Event Message
2098 * @adap: the adapter
2099 * @rsp: the response queue descriptor holding the RX_PKT message
2100 * @skb: the packet
2101 *
2102 * PTP enabled and MPS packet, read HW timestamp
2103 */
2104static int t4_rx_hststamp(struct adapter *adapter, const __be64 *rsp,
2105 struct sge_eth_rxq *rxq, struct sk_buff *skb)
2106{
2107 int ret;
2108
2109 if (unlikely((*(u8 *)rsp == CPL_RX_MPS_PKT) &&
2110 !is_t4(adapter->params.chip))) {
2111 ret = t4_systim_to_hwstamp(adapter, skb);
2112 if (ret == RX_PTP_PKT_ERR) {
2113 kfree_skb(skb);
2114 rxq->stats.rx_drops++;
2115 }
2116 return ret;
2117 }
2118 return RX_NON_PTP_PKT;
2119}
2120
2121/**
2122 * t4_tx_hststamp - Loopback PTP Transmit Event Message
2123 * @adap: the adapter
2124 * @skb: the packet
2125 * @dev: the ingress net device
2126 *
2127 * Read hardware timestamp for the loopback PTP Tx event message
2128 */
2129static int t4_tx_hststamp(struct adapter *adapter, struct sk_buff *skb,
2130 struct net_device *dev)
2131{
2132 struct port_info *pi = netdev_priv(dev);
2133
2134 if (!is_t4(adapter->params.chip) && adapter->ptp_tx_skb) {
2135 cxgb4_ptp_read_hwstamp(adapter, pi);
2136 kfree_skb(skb);
2137 return 0;
2138 }
2139 return 1;
2140}
2141
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002142/**
2143 * t4_ethrx_handler - process an ingress ethernet packet
2144 * @q: the response queue that received the packet
2145 * @rsp: the response queue descriptor holding the RX_PKT message
2146 * @si: the gather list of packet fragments
2147 *
2148 * Process an ingress ethernet packet and deliver it to the stack.
2149 */
2150int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
2151 const struct pkt_gl *si)
2152{
2153 bool csum_ok;
2154 struct sk_buff *skb;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002155 const struct cpl_rx_pkt *pkt;
2156 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
Atul Guptaa45695042017-07-04 16:46:20 +05302157 struct adapter *adapter = q->adap;
Vipul Pandya52367a72012-09-26 02:39:38 +00002158 struct sge *s = &q->adap->sge;
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302159 int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
Santosh Rastapur0a57a532013-03-14 05:08:49 +00002160 CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
Arjun V8eb9f2f2017-01-04 19:04:20 +05302161 u16 err_vec;
Varun Prakash84a200b2015-03-24 19:14:46 +05302162 struct port_info *pi;
Atul Guptaa45695042017-07-04 16:46:20 +05302163 int ret = 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002164
Santosh Rastapur0a57a532013-03-14 05:08:49 +00002165 if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002166 return handle_trace_pkt(q->adap, si);
2167
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07002168 pkt = (const struct cpl_rx_pkt *)rsp;
Arjun V8eb9f2f2017-01-04 19:04:20 +05302169 /* Compressed error vector is enabled for T6 only */
2170 if (q->adap->params.tp.rx_pkt_encap)
2171 err_vec = T6_COMPR_RXERR_VEC_G(be16_to_cpu(pkt->err_vec));
2172 else
2173 err_vec = be16_to_cpu(pkt->err_vec);
2174
2175 csum_ok = pkt->csum_calc && !err_vec &&
Hariprasad Shenaicca28222014-05-07 18:01:03 +05302176 (q->netdev->features & NETIF_F_RXCSUM);
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08002177 if ((pkt->l2info & htonl(RXF_TCP_F)) &&
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002178 (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
2179 do_gro(rxq, si, pkt);
2180 return 0;
2181 }
2182
2183 skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
2184 if (unlikely(!skb)) {
2185 t4_pktgl_free(si);
2186 rxq->stats.rx_drops++;
2187 return 0;
2188 }
Atul Guptaa45695042017-07-04 16:46:20 +05302189 pi = netdev_priv(q->netdev);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002190
Atul Guptaa45695042017-07-04 16:46:20 +05302191 /* Handle PTP Event Rx packet */
2192 if (unlikely(pi->ptp_enable)) {
2193 ret = t4_rx_hststamp(adapter, rsp, rxq, skb);
2194 if (ret == RX_PTP_PKT_ERR)
2195 return 0;
2196 }
2197 if (likely(!ret))
2198 __skb_pull(skb, s->pktshift); /* remove ethernet header pad */
2199
2200 /* Handle the PTP Event Tx Loopback packet */
2201 if (unlikely(pi->ptp_enable && !ret &&
2202 (pkt->l2info & htonl(RXF_UDP_F)) &&
2203 cxgb4_ptp_is_ptp_rx(skb))) {
2204 if (!t4_tx_hststamp(adapter, skb, q->netdev))
2205 return 0;
2206 }
2207
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002208 skb->protocol = eth_type_trans(skb, q->netdev);
2209 skb_record_rx_queue(skb, q->idx);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07002210 if (skb->dev->features & NETIF_F_RXHASH)
Tom Herbert82649892013-12-17 23:23:29 -08002211 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
2212 PKT_HASH_TYPE_L3);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07002213
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002214 rxq->stats.pkts++;
2215
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302216 if (pi->rxtstamp)
2217 cxgb4_sgetim_to_hwtstamp(q->adap, skb_hwtstamps(skb),
2218 si->sgetstamp);
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08002219 if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00002220 if (!pkt->ip_frag) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002221 skb->ip_summed = CHECKSUM_UNNECESSARY;
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00002222 rxq->stats.rx_cso++;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08002223 } else if (pkt->l2info & htonl(RXF_IP_F)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002224 __sum16 c = (__force __sum16)pkt->csum;
2225 skb->csum = csum_unfold(c);
2226 skb->ip_summed = CHECKSUM_COMPLETE;
Dimitris Michailidisba5d3c62010-08-02 13:19:17 +00002227 rxq->stats.rx_cso++;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002228 }
Varun Prakash84a200b2015-03-24 19:14:46 +05302229 } else {
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002230 skb_checksum_none_assert(skb);
Varun Prakash84a200b2015-03-24 19:14:46 +05302231#ifdef CONFIG_CHELSIO_T4_FCOE
2232#define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
2233 RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
2234
Varun Prakash84a200b2015-03-24 19:14:46 +05302235 if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
2236 if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
2237 (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
Arjun V8eb9f2f2017-01-04 19:04:20 +05302238 if (q->adap->params.tp.rx_pkt_encap)
2239 csum_ok = err_vec &
2240 T6_COMPR_RXERR_SUM_F;
2241 else
2242 csum_ok = err_vec & RXERR_CSUM_F;
2243 if (!csum_ok)
Varun Prakash84a200b2015-03-24 19:14:46 +05302244 skb->ip_summed = CHECKSUM_UNNECESSARY;
2245 }
2246 }
2247
2248#undef CPL_RX_PKT_FLAGS
2249#endif /* CONFIG_CHELSIO_T4_FCOE */
2250 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002251
2252 if (unlikely(pkt->vlan_ex)) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002253 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002254 rxq->stats.vlan_ex++;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00002255 }
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302256 skb_mark_napi_id(skb, &q->napi);
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00002257 netif_receive_skb(skb);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002258 return 0;
2259}
2260
2261/**
2262 * restore_rx_bufs - put back a packet's Rx buffers
2263 * @si: the packet gather list
2264 * @q: the SGE free list
2265 * @frags: number of FL buffers to restore
2266 *
2267 * Puts back on an FL the Rx buffers associated with @si. The buffers
2268 * have already been unmapped and are left unmapped, we mark them so to
2269 * prevent further unmapping attempts.
2270 *
2271 * This function undoes a series of @unmap_rx_buf calls when we find out
2272 * that the current packet can't be processed right away afterall and we
2273 * need to come back to it later. This is a very rare event and there's
2274 * no effort to make this particularly efficient.
2275 */
2276static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
2277 int frags)
2278{
2279 struct rx_sw_desc *d;
2280
2281 while (frags--) {
2282 if (q->cidx == 0)
2283 q->cidx = q->size - 1;
2284 else
2285 q->cidx--;
2286 d = &q->sdesc[q->cidx];
2287 d->page = si->frags[frags].page;
2288 d->dma_addr |= RX_UNMAPPED_BUF;
2289 q->avail++;
2290 }
2291}
2292
2293/**
2294 * is_new_response - check if a response is newly written
2295 * @r: the response descriptor
2296 * @q: the response queue
2297 *
2298 * Returns true if a response descriptor contains a yet unprocessed
2299 * response.
2300 */
2301static inline bool is_new_response(const struct rsp_ctrl *r,
2302 const struct sge_rspq *q)
2303{
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302304 return (r->type_gen >> RSPD_GEN_S) == q->gen;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002305}
2306
2307/**
2308 * rspq_next - advance to the next entry in a response queue
2309 * @q: the queue
2310 *
2311 * Updates the state of a response queue to advance it to the next entry.
2312 */
2313static inline void rspq_next(struct sge_rspq *q)
2314{
2315 q->cur_desc = (void *)q->cur_desc + q->iqe_len;
2316 if (unlikely(++q->cidx == q->size)) {
2317 q->cidx = 0;
2318 q->gen ^= 1;
2319 q->cur_desc = q->desc;
2320 }
2321}
2322
2323/**
2324 * process_responses - process responses from an SGE response queue
2325 * @q: the ingress queue to process
2326 * @budget: how many responses can be processed in this round
2327 *
2328 * Process responses from an SGE response queue up to the supplied budget.
2329 * Responses include received packets as well as control messages from FW
2330 * or HW.
2331 *
2332 * Additionally choose the interrupt holdoff time for the next interrupt
2333 * on this queue. If the system is under memory shortage use a fairly
2334 * long delay to help recovery.
2335 */
2336static int process_responses(struct sge_rspq *q, int budget)
2337{
2338 int ret, rsp_type;
2339 int budget_left = budget;
2340 const struct rsp_ctrl *rc;
2341 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
Vipul Pandya52367a72012-09-26 02:39:38 +00002342 struct adapter *adapter = q->adap;
2343 struct sge *s = &adapter->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002344
2345 while (likely(budget_left)) {
2346 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
Varun Prakash2337ba42016-02-14 23:02:41 +05302347 if (!is_new_response(rc, q)) {
2348 if (q->flush_handler)
2349 q->flush_handler(q);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002350 break;
Varun Prakash2337ba42016-02-14 23:02:41 +05302351 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002352
Alexander Duyck019be1c2015-04-08 18:49:29 -07002353 dma_rmb();
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302354 rsp_type = RSPD_TYPE_G(rc->type_gen);
2355 if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
Ian Campbelle91b0f22011-10-19 23:01:46 +00002356 struct page_frag *fp;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002357 struct pkt_gl si;
2358 const struct rx_sw_desc *rsd;
2359 u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
2360
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302361 if (len & RSPD_NEWBUF_F) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002362 if (likely(q->offset > 0)) {
2363 free_rx_bufs(q->adap, &rxq->fl, 1);
2364 q->offset = 0;
2365 }
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302366 len = RSPD_LEN_G(len);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002367 }
2368 si.tot_len = len;
2369
2370 /* gather packet fragments */
2371 for (frags = 0, fp = si.frags; ; frags++, fp++) {
2372 rsd = &rxq->fl.sdesc[rxq->fl.cidx];
Vipul Pandya52367a72012-09-26 02:39:38 +00002373 bufsz = get_buf_size(adapter, rsd);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002374 fp->page = rsd->page;
Ian Campbelle91b0f22011-10-19 23:01:46 +00002375 fp->offset = q->offset;
2376 fp->size = min(bufsz, len);
2377 len -= fp->size;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002378 if (!len)
2379 break;
2380 unmap_rx_buf(q->adap, &rxq->fl);
2381 }
2382
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302383 si.sgetstamp = SGE_TIMESTAMP_G(
2384 be64_to_cpu(rc->last_flit));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002385 /*
2386 * Last buffer remains mapped so explicitly make it
2387 * coherent for CPU access.
2388 */
2389 dma_sync_single_for_cpu(q->adap->pdev_dev,
2390 get_buf_addr(rsd),
Ian Campbelle91b0f22011-10-19 23:01:46 +00002391 fp->size, DMA_FROM_DEVICE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002392
2393 si.va = page_address(si.frags[0].page) +
Ian Campbelle91b0f22011-10-19 23:01:46 +00002394 si.frags[0].offset;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002395 prefetch(si.va);
2396
2397 si.nfrags = frags + 1;
2398 ret = q->handler(q, q->cur_desc, &si);
2399 if (likely(ret == 0))
Vipul Pandya52367a72012-09-26 02:39:38 +00002400 q->offset += ALIGN(fp->size, s->fl_align);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002401 else
2402 restore_rx_bufs(&si, &rxq->fl, frags);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302403 } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002404 ret = q->handler(q, q->cur_desc, NULL);
2405 } else {
2406 ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
2407 }
2408
2409 if (unlikely(ret)) {
2410 /* couldn't process descriptor, back off for recovery */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302411 q->next_intr_params = QINTR_TIMER_IDX_V(NOMEM_TMR_IDX);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002412 break;
2413 }
2414
2415 rspq_next(q);
2416 budget_left--;
2417 }
2418
Hariprasad Shenaida08e422016-03-01 17:19:32 +05302419 if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 16)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002420 __refill_fl(q->adap, &rxq->fl);
2421 return budget - budget_left;
2422}
2423
2424/**
2425 * napi_rx_handler - the NAPI handler for Rx processing
2426 * @napi: the napi instance
2427 * @budget: how many packets we can process in this round
2428 *
2429 * Handler for new data events when using NAPI. This does not need any
2430 * locking or protection from interrupts as data interrupts are off at
2431 * this point and other adapter interrupts do not interfere (the latter
2432 * in not a concern at all with MSI-X as non-data interrupts then have
2433 * a separate handler).
2434 */
2435static int napi_rx_handler(struct napi_struct *napi, int budget)
2436{
2437 unsigned int params;
2438 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302439 int work_done;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302440 u32 val;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002441
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302442 work_done = process_responses(q, budget);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002443 if (likely(work_done < budget)) {
Hariprasad Shenaie553ec32014-09-26 00:23:55 +05302444 int timer_index;
2445
Hariprasad Shenai812787b2015-12-23 11:29:56 +05302446 napi_complete_done(napi, work_done);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302447 timer_index = QINTR_TIMER_IDX_G(q->next_intr_params);
Hariprasad Shenaie553ec32014-09-26 00:23:55 +05302448
2449 if (q->adaptive_rx) {
2450 if (work_done > max(timer_pkt_quota[timer_index],
2451 MIN_NAPI_WORK))
2452 timer_index = (timer_index + 1);
2453 else
2454 timer_index = timer_index - 1;
2455
2456 timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302457 q->next_intr_params =
2458 QINTR_TIMER_IDX_V(timer_index) |
2459 QINTR_CNT_EN_V(0);
Hariprasad Shenaie553ec32014-09-26 00:23:55 +05302460 params = q->next_intr_params;
2461 } else {
2462 params = q->next_intr_params;
2463 q->next_intr_params = q->intr_params;
2464 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002465 } else
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302466 params = QINTR_TIMER_IDX_V(7);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002467
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302468 val = CIDXINC_V(work_done) | SEINTARM_V(params);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302469
2470 /* If we don't have access to the new User GTS (T5+), use the old
2471 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2472 */
2473 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302474 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
2475 val | INGRESSQID_V((u32)q->cntxt_id));
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302476 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302477 writel(val | INGRESSQID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302478 q->bar2_addr + SGE_UDB_GTS);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302479 wmb();
2480 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002481 return work_done;
2482}
2483
2484/*
2485 * The MSI-X interrupt handler for an SGE response queue.
2486 */
2487irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
2488{
2489 struct sge_rspq *q = cookie;
2490
2491 napi_schedule(&q->napi);
2492 return IRQ_HANDLED;
2493}
2494
2495/*
2496 * Process the indirect interrupt entries in the interrupt queue and kick off
2497 * NAPI for each queue that has generated an entry.
2498 */
2499static unsigned int process_intrq(struct adapter *adap)
2500{
2501 unsigned int credits;
2502 const struct rsp_ctrl *rc;
2503 struct sge_rspq *q = &adap->sge.intrq;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302504 u32 val;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002505
2506 spin_lock(&adap->sge.intrq_lock);
2507 for (credits = 0; ; credits++) {
2508 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
2509 if (!is_new_response(rc, q))
2510 break;
2511
Alexander Duyck019be1c2015-04-08 18:49:29 -07002512 dma_rmb();
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302513 if (RSPD_TYPE_G(rc->type_gen) == RSPD_TYPE_INTR_X) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002514 unsigned int qid = ntohl(rc->pldbuflen_qid);
2515
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002516 qid -= adap->sge.ingr_start;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002517 napi_schedule(&adap->sge.ingr_map[qid]->napi);
2518 }
2519
2520 rspq_next(q);
2521 }
2522
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302523 val = CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302524
2525 /* If we don't have access to the new User GTS (T5+), use the old
2526 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2527 */
2528 if (unlikely(q->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302529 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
2530 val | INGRESSQID_V(q->cntxt_id));
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302531 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302532 writel(val | INGRESSQID_V(q->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302533 q->bar2_addr + SGE_UDB_GTS);
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302534 wmb();
2535 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002536 spin_unlock(&adap->sge.intrq_lock);
2537 return credits;
2538}
2539
2540/*
2541 * The MSI interrupt handler, which handles data events from SGE response queues
2542 * as well as error and other async events as they all use the same MSI vector.
2543 */
2544static irqreturn_t t4_intr_msi(int irq, void *cookie)
2545{
2546 struct adapter *adap = cookie;
2547
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +05302548 if (adap->flags & MASTER_PF)
2549 t4_slow_intr_handler(adap);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002550 process_intrq(adap);
2551 return IRQ_HANDLED;
2552}
2553
2554/*
2555 * Interrupt handler for legacy INTx interrupts.
2556 * Handles data events from SGE response queues as well as error and other
2557 * async events as they all use the same interrupt line.
2558 */
2559static irqreturn_t t4_intr_intx(int irq, void *cookie)
2560{
2561 struct adapter *adap = cookie;
2562
Hariprasad Shenaif061de42015-01-05 16:30:44 +05302563 t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +05302564 if (((adap->flags & MASTER_PF) && t4_slow_intr_handler(adap)) |
2565 process_intrq(adap))
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002566 return IRQ_HANDLED;
2567 return IRQ_NONE; /* probably shared interrupt */
2568}
2569
2570/**
2571 * t4_intr_handler - select the top-level interrupt handler
2572 * @adap: the adapter
2573 *
2574 * Selects the top-level interrupt handler based on the type of interrupts
2575 * (MSI-X, MSI, or INTx).
2576 */
2577irq_handler_t t4_intr_handler(struct adapter *adap)
2578{
2579 if (adap->flags & USING_MSIX)
2580 return t4_sge_intr_msix;
2581 if (adap->flags & USING_MSI)
2582 return t4_intr_msi;
2583 return t4_intr_intx;
2584}
2585
2586static void sge_rx_timer_cb(unsigned long data)
2587{
2588 unsigned long m;
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302589 unsigned int i;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002590 struct adapter *adap = (struct adapter *)data;
2591 struct sge *s = &adap->sge;
2592
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302593 for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002594 for (m = s->starving_fl[i]; m; m &= m - 1) {
2595 struct sge_eth_rxq *rxq;
2596 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
2597 struct sge_fl *fl = s->egr_map[id];
2598
2599 clear_bit(id, s->starving_fl);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002600 smp_mb__after_atomic();
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002601
Hariprasad Shenaic098b022015-04-15 02:02:31 +05302602 if (fl_starving(adap, fl)) {
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002603 rxq = container_of(fl, struct sge_eth_rxq, fl);
2604 if (napi_reschedule(&rxq->rspq.napi))
2605 fl->starving++;
2606 else
2607 set_bit(id, s->starving_fl);
2608 }
2609 }
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302610 /* The remainder of the SGE RX Timer Callback routine is dedicated to
2611 * global Master PF activities like checking for chip ingress stalls,
2612 * etc.
2613 */
2614 if (!(adap->flags & MASTER_PF))
2615 goto done;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002616
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302617 t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002618
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05302619done:
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002620 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
2621}
2622
2623static void sge_tx_timer_cb(unsigned long data)
2624{
2625 unsigned long m;
2626 unsigned int i, budget;
2627 struct adapter *adap = (struct adapter *)data;
2628 struct sge *s = &adap->sge;
2629
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302630 for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002631 for (m = s->txq_maperr[i]; m; m &= m - 1) {
2632 unsigned long id = __ffs(m) + i * BITS_PER_LONG;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302633 struct sge_uld_txq *txq = s->egr_map[id];
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002634
2635 clear_bit(id, s->txq_maperr);
2636 tasklet_schedule(&txq->qresume_tsk);
2637 }
2638
Atul Guptaa45695042017-07-04 16:46:20 +05302639 if (!is_t4(adap->params.chip)) {
2640 struct sge_eth_txq *q = &s->ptptxq;
2641 int avail;
2642
2643 spin_lock(&adap->ptp_lock);
2644 avail = reclaimable(&q->q);
2645
2646 if (avail) {
2647 free_tx_desc(adap, &q->q, avail, false);
2648 q->q.in_use -= avail;
2649 }
2650 spin_unlock(&adap->ptp_lock);
2651 }
2652
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002653 budget = MAX_TIMER_TX_RECLAIM;
2654 i = s->ethtxq_rover;
2655 do {
2656 struct sge_eth_txq *q = &s->ethtxq[i];
2657
2658 if (q->q.in_use &&
2659 time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
2660 __netif_tx_trylock(q->txq)) {
2661 int avail = reclaimable(&q->q);
2662
2663 if (avail) {
2664 if (avail > budget)
2665 avail = budget;
2666
2667 free_tx_desc(adap, &q->q, avail, true);
2668 q->q.in_use -= avail;
2669 budget -= avail;
2670 }
2671 __netif_tx_unlock(q->txq);
2672 }
2673
2674 if (++i >= s->ethqsets)
2675 i = 0;
2676 } while (budget && i != s->ethtxq_rover);
2677 s->ethtxq_rover = i;
2678 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
2679}
2680
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302681/**
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302682 * bar2_address - return the BAR2 address for an SGE Queue's Registers
2683 * @adapter: the adapter
2684 * @qid: the SGE Queue ID
2685 * @qtype: the SGE Queue Type (Egress or Ingress)
2686 * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302687 *
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302688 * Returns the BAR2 address for the SGE Queue Registers associated with
2689 * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
2690 * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
2691 * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
2692 * Registers are supported (e.g. the Write Combining Doorbell Buffer).
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302693 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302694static void __iomem *bar2_address(struct adapter *adapter,
2695 unsigned int qid,
2696 enum t4_bar2_qtype qtype,
2697 unsigned int *pbar2_qid)
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302698{
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302699 u64 bar2_qoffset;
2700 int ret;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302701
Linus Torvaldse0456712015-06-24 16:49:49 -07002702 ret = t4_bar2_sge_qregs(adapter, qid, qtype, 0,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302703 &bar2_qoffset, pbar2_qid);
2704 if (ret)
2705 return NULL;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302706
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302707 return adapter->bar2 + bar2_qoffset;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302708}
2709
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302710/* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
2711 * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
2712 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002713int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
2714 struct net_device *dev, int intr_idx,
Varun Prakash2337ba42016-02-14 23:02:41 +05302715 struct sge_fl *fl, rspq_handler_t hnd,
2716 rspq_flush_handler_t flush_hnd, int cong)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002717{
2718 int ret, flsz = 0;
2719 struct fw_iq_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002720 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002721 struct port_info *pi = netdev_priv(dev);
Casey Leedomb0ba9d52017-08-15 11:23:26 +08002722 int relaxed = !(adap->flags & ROOT_NO_RELAXED_ORDERING);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002723
2724 /* Size needs to be multiple of 16, including status entry. */
2725 iq->size = roundup(iq->size, 16);
2726
2727 iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
Hariprasad Shenai0ac5b702015-12-23 11:29:55 +05302728 &iq->phys_addr, NULL, 0,
2729 dev_to_node(adap->pdev_dev));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002730 if (!iq->desc)
2731 return -ENOMEM;
2732
2733 memset(&c, 0, sizeof(c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302734 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
2735 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302736 FW_IQ_CMD_PFN_V(adap->pf) | FW_IQ_CMD_VFN_V(0));
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302737 c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002738 FW_LEN16(c));
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302739 c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
2740 FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302741 FW_IQ_CMD_IQANDST_V(intr_idx < 0) |
2742 FW_IQ_CMD_IQANUD_V(UPDATEDELIVERY_INTERRUPT_X) |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302743 FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002744 -intr_idx - 1));
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302745 c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
2746 FW_IQ_CMD_IQGTSMODE_F |
2747 FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
2748 FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002749 c.iqsize = htons(iq->size);
2750 c.iqaddr = cpu_to_be64(iq->phys_addr);
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302751 if (cong >= 0)
2752 c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002753
2754 if (fl) {
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302755 enum chip_type chip = CHELSIO_CHIP_VERSION(adap->params.chip);
2756
Hariprasad Shenai13432992015-05-05 14:59:51 +05302757 /* Allocate the ring for the hardware free list (with space
2758 * for its status page) along with the associated software
2759 * descriptor ring. The free list size needs to be a multiple
2760 * of the Egress Queue Unit and at least 2 Egress Units larger
2761 * than the SGE's Egress Congrestion Threshold
2762 * (fl_starve_thres - 1).
2763 */
2764 if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
2765 fl->size = s->fl_starve_thres - 1 + 2 * 8;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002766 fl->size = roundup(fl->size, 8);
2767 fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
2768 sizeof(struct rx_sw_desc), &fl->addr,
Hariprasad Shenai0ac5b702015-12-23 11:29:55 +05302769 &fl->sdesc, s->stat_len,
2770 dev_to_node(adap->pdev_dev));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002771 if (!fl->desc)
2772 goto fl_nomem;
2773
Vipul Pandya52367a72012-09-26 02:39:38 +00002774 flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302775 c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
Casey Leedomb0ba9d52017-08-15 11:23:26 +08002776 FW_IQ_CMD_FL0FETCHRO_V(relaxed) |
2777 FW_IQ_CMD_FL0DATARO_V(relaxed) |
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05302778 FW_IQ_CMD_FL0PADEN_F);
2779 if (cong >= 0)
2780 c.iqns_to_fl0congen |=
2781 htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
2782 FW_IQ_CMD_FL0CONGCIF_F |
2783 FW_IQ_CMD_FL0CONGEN_F);
Hariprasad Shenaiedadad82016-03-01 17:19:33 +05302784 /* In T6, for egress queue type FL there is internal overhead
2785 * of 16B for header going into FLM module. Hence the maximum
2786 * allowed burst size is 448 bytes. For T4/T5, the hardware
2787 * doesn't coalesce fetch requests if more than 64 bytes of
2788 * Free List pointers are provided, so we use a 128-byte Fetch
2789 * Burst Minimum there (T6 implements coalescing so we can use
2790 * the smaller 64-byte value there).
2791 */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302792 c.fl0dcaen_to_fl0cidxfthresh =
Hariprasad Shenaiedadad82016-03-01 17:19:33 +05302793 htons(FW_IQ_CMD_FL0FBMIN_V(chip <= CHELSIO_T5 ?
2794 FETCHBURSTMIN_128B_X :
2795 FETCHBURSTMIN_64B_X) |
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302796 FW_IQ_CMD_FL0FBMAX_V((chip <= CHELSIO_T5) ?
2797 FETCHBURSTMAX_512B_X :
2798 FETCHBURSTMAX_256B_X));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002799 c.fl0size = htons(flsz);
2800 c.fl0addr = cpu_to_be64(fl->addr);
2801 }
2802
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302803 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002804 if (ret)
2805 goto err;
2806
2807 netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
2808 iq->cur_desc = iq->desc;
2809 iq->cidx = 0;
2810 iq->gen = 1;
2811 iq->next_intr_params = iq->intr_params;
2812 iq->cntxt_id = ntohs(c.iqid);
2813 iq->abs_id = ntohs(c.physiqid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302814 iq->bar2_addr = bar2_address(adap,
2815 iq->cntxt_id,
2816 T4_BAR2_QTYPE_INGRESS,
2817 &iq->bar2_qid);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002818 iq->size--; /* subtract status entry */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002819 iq->netdev = dev;
2820 iq->handler = hnd;
Varun Prakash2337ba42016-02-14 23:02:41 +05302821 iq->flush_handler = flush_hnd;
2822
2823 memset(&iq->lro_mgr, 0, sizeof(struct t4_lro_mgr));
2824 skb_queue_head_init(&iq->lro_mgr.lroq);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002825
2826 /* set offset to -1 to distinguish ingress queues without FL */
2827 iq->offset = fl ? 0 : -1;
2828
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002829 adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002830
2831 if (fl) {
Roland Dreier62718b32010-04-21 08:09:21 +00002832 fl->cntxt_id = ntohs(c.fl0id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002833 fl->avail = fl->pend_cred = 0;
2834 fl->pidx = fl->cidx = 0;
2835 fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002836 adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302837
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302838 /* Note, we must initialize the BAR2 Free List User Doorbell
2839 * information before refilling the Free List!
Hariprasad Shenaid63a6dc2014-09-26 00:23:52 +05302840 */
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302841 fl->bar2_addr = bar2_address(adap,
2842 fl->cntxt_id,
2843 T4_BAR2_QTYPE_EGRESS,
2844 &fl->bar2_qid);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002845 refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
2846 }
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302847
2848 /* For T5 and later we attempt to set up the Congestion Manager values
2849 * of the new RX Ethernet Queue. This should really be handled by
2850 * firmware because it's more complex than any host driver wants to
2851 * get involved with and it's different per chip and this is almost
2852 * certainly wrong. Firmware would be wrong as well, but it would be
2853 * a lot easier to fix in one place ... For now we do something very
2854 * simple (and hopefully less wrong).
2855 */
2856 if (!is_t4(adap->params.chip) && cong >= 0) {
Hariprasad Shenai2216d012015-12-23 22:47:18 +05302857 u32 param, val, ch_map = 0;
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302858 int i;
Hariprasad Shenai2216d012015-12-23 22:47:18 +05302859 u16 cng_ch_bits_log = adap->params.arch.cng_ch_bits_log;
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302860
2861 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
2862 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
2863 FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
2864 if (cong == 0) {
2865 val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
2866 } else {
2867 val =
2868 CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
2869 for (i = 0; i < 4; i++) {
2870 if (cong & (1 << i))
Hariprasad Shenai2216d012015-12-23 22:47:18 +05302871 ch_map |= 1 << (i << cng_ch_bits_log);
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302872 }
Hariprasad Shenai2216d012015-12-23 22:47:18 +05302873 val |= CONMCTXT_CNGCHMAP_V(ch_map);
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302874 }
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302875 ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenaib8b1ae92015-05-05 14:59:53 +05302876 &param, &val);
2877 if (ret)
2878 dev_warn(adap->pdev_dev, "Failed to set Congestion"
2879 " Manager Context for Ingress Queue %d: %d\n",
2880 iq->cntxt_id, -ret);
2881 }
2882
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002883 return 0;
2884
2885fl_nomem:
2886 ret = -ENOMEM;
2887err:
2888 if (iq->desc) {
2889 dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
2890 iq->desc, iq->phys_addr);
2891 iq->desc = NULL;
2892 }
2893 if (fl && fl->desc) {
2894 kfree(fl->sdesc);
2895 fl->sdesc = NULL;
2896 dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
2897 fl->desc, fl->addr);
2898 fl->desc = NULL;
2899 }
2900 return ret;
2901}
2902
2903static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
2904{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00002905 q->cntxt_id = id;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302906 q->bar2_addr = bar2_address(adap,
2907 q->cntxt_id,
2908 T4_BAR2_QTYPE_EGRESS,
2909 &q->bar2_qid);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002910 q->in_use = 0;
2911 q->cidx = q->pidx = 0;
2912 q->stops = q->restarts = 0;
2913 q->stat = (void *)&q->desc[q->size];
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302914 spin_lock_init(&q->db_lock);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002915 adap->sge.egr_map[id - adap->sge.egr_start] = q;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002916}
2917
2918int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
2919 struct net_device *dev, struct netdev_queue *netdevq,
2920 unsigned int iqid)
2921{
2922 int ret, nentries;
2923 struct fw_eq_eth_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002924 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002925 struct port_info *pi = netdev_priv(dev);
2926
2927 /* Add status entries */
Vipul Pandya52367a72012-09-26 02:39:38 +00002928 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002929
2930 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2931 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
Vipul Pandya52367a72012-09-26 02:39:38 +00002932 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +00002933 netdev_queue_numa_node_read(netdevq));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002934 if (!txq->q.desc)
2935 return -ENOMEM;
2936
2937 memset(&c, 0, sizeof(c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302938 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
2939 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302940 FW_EQ_ETH_CMD_PFN_V(adap->pf) |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302941 FW_EQ_ETH_CMD_VFN_V(0));
2942 c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
2943 FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
2944 c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
2945 FW_EQ_ETH_CMD_VIID_V(pi->viid));
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05302946 c.fetchszm_to_iqid =
2947 htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
2948 FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
2949 FW_EQ_ETH_CMD_FETCHRO_F | FW_EQ_ETH_CMD_IQID_V(iqid));
2950 c.dcaen_to_eqsize =
2951 htonl(FW_EQ_ETH_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
2952 FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
2953 FW_EQ_ETH_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
2954 FW_EQ_ETH_CMD_EQSIZE_V(nentries));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002955 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2956
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302957 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002958 if (ret) {
2959 kfree(txq->q.sdesc);
2960 txq->q.sdesc = NULL;
2961 dma_free_coherent(adap->pdev_dev,
2962 nentries * sizeof(struct tx_desc),
2963 txq->q.desc, txq->q.phys_addr);
2964 txq->q.desc = NULL;
2965 return ret;
2966 }
2967
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302968 txq->q.q_type = CXGB4_TXQ_ETH;
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302969 init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002970 txq->txq = netdevq;
2971 txq->tso = txq->tx_cso = txq->vlan_ins = 0;
2972 txq->mapping_err = 0;
2973 return 0;
2974}
2975
2976int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
2977 struct net_device *dev, unsigned int iqid,
2978 unsigned int cmplqid)
2979{
2980 int ret, nentries;
2981 struct fw_eq_ctrl_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00002982 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002983 struct port_info *pi = netdev_priv(dev);
2984
2985 /* Add status entries */
Vipul Pandya52367a72012-09-26 02:39:38 +00002986 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002987
2988 txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
2989 sizeof(struct tx_desc), 0, &txq->q.phys_addr,
Hariprasad Shenai982b81e2015-05-05 14:59:54 +05302990 NULL, 0, dev_to_node(adap->pdev_dev));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00002991 if (!txq->q.desc)
2992 return -ENOMEM;
2993
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302994 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
2995 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302996 FW_EQ_CTRL_CMD_PFN_V(adap->pf) |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302997 FW_EQ_CTRL_CMD_VFN_V(0));
2998 c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
2999 FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
3000 c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003001 c.physeqid_pkd = htonl(0);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05303002 c.fetchszm_to_iqid =
3003 htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
3004 FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
3005 FW_EQ_CTRL_CMD_FETCHRO_F | FW_EQ_CTRL_CMD_IQID_V(iqid));
3006 c.dcaen_to_eqsize =
3007 htonl(FW_EQ_CTRL_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
3008 FW_EQ_CTRL_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
3009 FW_EQ_CTRL_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
3010 FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003011 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
3012
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303013 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003014 if (ret) {
3015 dma_free_coherent(adap->pdev_dev,
3016 nentries * sizeof(struct tx_desc),
3017 txq->q.desc, txq->q.phys_addr);
3018 txq->q.desc = NULL;
3019 return ret;
3020 }
3021
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303022 txq->q.q_type = CXGB4_TXQ_CTRL;
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05303023 init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003024 txq->adap = adap;
3025 skb_queue_head_init(&txq->sendq);
3026 tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
3027 txq->full = 0;
3028 return 0;
3029}
3030
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303031int t4_sge_mod_ctrl_txq(struct adapter *adap, unsigned int eqid,
3032 unsigned int cmplqid)
3033{
3034 u32 param, val;
3035
3036 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
3037 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL) |
3038 FW_PARAMS_PARAM_YZ_V(eqid));
3039 val = cmplqid;
3040 return t4_set_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
3041}
3042
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303043int t4_sge_alloc_uld_txq(struct adapter *adap, struct sge_uld_txq *txq,
3044 struct net_device *dev, unsigned int iqid,
3045 unsigned int uld_type)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003046{
3047 int ret, nentries;
3048 struct fw_eq_ofld_cmd c;
Vipul Pandya52367a72012-09-26 02:39:38 +00003049 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003050 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303051 int cmd = FW_EQ_OFLD_CMD;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003052
3053 /* Add status entries */
Vipul Pandya52367a72012-09-26 02:39:38 +00003054 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003055
3056 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
3057 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
Vipul Pandya52367a72012-09-26 02:39:38 +00003058 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
Dimitris Michailidisad6bad32010-12-14 21:36:55 +00003059 NUMA_NO_NODE);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003060 if (!txq->q.desc)
3061 return -ENOMEM;
3062
3063 memset(&c, 0, sizeof(c));
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303064 if (unlikely(uld_type == CXGB4_TX_CRYPTO))
3065 cmd = FW_EQ_CTRL_CMD;
3066 c.op_to_vfn = htonl(FW_CMD_OP_V(cmd) | FW_CMD_REQUEST_F |
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303067 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303068 FW_EQ_OFLD_CMD_PFN_V(adap->pf) |
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05303069 FW_EQ_OFLD_CMD_VFN_V(0));
3070 c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
3071 FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05303072 c.fetchszm_to_iqid =
3073 htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
3074 FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
3075 FW_EQ_OFLD_CMD_FETCHRO_F | FW_EQ_OFLD_CMD_IQID_V(iqid));
3076 c.dcaen_to_eqsize =
3077 htonl(FW_EQ_OFLD_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
3078 FW_EQ_OFLD_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
3079 FW_EQ_OFLD_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
3080 FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003081 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
3082
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303083 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003084 if (ret) {
3085 kfree(txq->q.sdesc);
3086 txq->q.sdesc = NULL;
3087 dma_free_coherent(adap->pdev_dev,
3088 nentries * sizeof(struct tx_desc),
3089 txq->q.desc, txq->q.phys_addr);
3090 txq->q.desc = NULL;
3091 return ret;
3092 }
3093
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303094 txq->q.q_type = CXGB4_TXQ_ULD;
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05303095 init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003096 txq->adap = adap;
3097 skb_queue_head_init(&txq->sendq);
3098 tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
3099 txq->full = 0;
3100 txq->mapping_err = 0;
3101 return 0;
3102}
3103
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303104void free_txq(struct adapter *adap, struct sge_txq *q)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003105{
Vipul Pandya52367a72012-09-26 02:39:38 +00003106 struct sge *s = &adap->sge;
3107
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003108 dma_free_coherent(adap->pdev_dev,
Vipul Pandya52367a72012-09-26 02:39:38 +00003109 q->size * sizeof(struct tx_desc) + s->stat_len,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003110 q->desc, q->phys_addr);
3111 q->cntxt_id = 0;
3112 q->sdesc = NULL;
3113 q->desc = NULL;
3114}
3115
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05303116void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
3117 struct sge_fl *fl)
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003118{
Vipul Pandya52367a72012-09-26 02:39:38 +00003119 struct sge *s = &adap->sge;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003120 unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
3121
Dimitris Michailidise46dab42010-08-23 17:20:58 +00003122 adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303123 t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003124 rq->cntxt_id, fl_id, 0xffff);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003125 dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
3126 rq->desc, rq->phys_addr);
3127 netif_napi_del(&rq->napi);
3128 rq->netdev = NULL;
3129 rq->cntxt_id = rq->abs_id = 0;
3130 rq->desc = NULL;
3131
3132 if (fl) {
3133 free_rx_bufs(adap, fl, fl->avail);
Vipul Pandya52367a72012-09-26 02:39:38 +00003134 dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003135 fl->desc, fl->addr);
3136 kfree(fl->sdesc);
3137 fl->sdesc = NULL;
3138 fl->cntxt_id = 0;
3139 fl->desc = NULL;
3140 }
3141}
3142
3143/**
Hariprasad Shenai5fa76692014-08-04 17:01:30 +05303144 * t4_free_ofld_rxqs - free a block of consecutive Rx queues
3145 * @adap: the adapter
3146 * @n: number of queues
3147 * @q: pointer to first queue
3148 *
3149 * Release the resources of a consecutive block of offload Rx queues.
3150 */
3151void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
3152{
3153 for ( ; n; n--, q++)
3154 if (q->rspq.desc)
3155 free_rspq_fl(adap, &q->rspq,
3156 q->fl.size ? &q->fl : NULL);
3157}
3158
3159/**
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003160 * t4_free_sge_resources - free SGE resources
3161 * @adap: the adapter
3162 *
3163 * Frees resources used by the SGE queue sets.
3164 */
3165void t4_free_sge_resources(struct adapter *adap)
3166{
3167 int i;
Hariprasad Shenaiebf4dc22016-04-11 11:07:58 +05303168 struct sge_eth_rxq *eq;
3169 struct sge_eth_txq *etq;
3170
3171 /* stop all Rx queues in order to start them draining */
3172 for (i = 0; i < adap->sge.ethqsets; i++) {
3173 eq = &adap->sge.ethrxq[i];
3174 if (eq->rspq.desc)
3175 t4_iq_stop(adap, adap->mbox, adap->pf, 0,
3176 FW_IQ_TYPE_FL_INT_CAP,
3177 eq->rspq.cntxt_id,
3178 eq->fl.size ? eq->fl.cntxt_id : 0xffff,
3179 0xffff);
3180 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003181
3182 /* clean up Ethernet Tx/Rx queues */
Hariprasad Shenaiebf4dc22016-04-11 11:07:58 +05303183 for (i = 0; i < adap->sge.ethqsets; i++) {
3184 eq = &adap->sge.ethrxq[i];
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003185 if (eq->rspq.desc)
Hariprasad Shenai5fa76692014-08-04 17:01:30 +05303186 free_rspq_fl(adap, &eq->rspq,
3187 eq->fl.size ? &eq->fl : NULL);
Hariprasad Shenaiebf4dc22016-04-11 11:07:58 +05303188
3189 etq = &adap->sge.ethtxq[i];
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003190 if (etq->q.desc) {
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303191 t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003192 etq->q.cntxt_id);
Hariprasad Shenaifbe80772016-04-26 20:10:24 +05303193 __netif_tx_lock_bh(etq->txq);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003194 free_tx_desc(adap, &etq->q, etq->q.in_use, true);
Hariprasad Shenaifbe80772016-04-26 20:10:24 +05303195 __netif_tx_unlock_bh(etq->txq);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003196 kfree(etq->q.sdesc);
3197 free_txq(adap, &etq->q);
3198 }
3199 }
3200
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003201 /* clean up control Tx queues */
3202 for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
3203 struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
3204
3205 if (cq->q.desc) {
3206 tasklet_kill(&cq->qresume_tsk);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303207 t4_ctrl_eq_free(adap, adap->mbox, adap->pf, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003208 cq->q.cntxt_id);
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003209 __skb_queue_purge(&cq->sendq);
3210 free_txq(adap, &cq->q);
3211 }
3212 }
3213
3214 if (adap->sge.fw_evtq.desc)
3215 free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
3216
3217 if (adap->sge.intrq.desc)
3218 free_rspq_fl(adap, &adap->sge.intrq, NULL);
3219
Atul Guptaa45695042017-07-04 16:46:20 +05303220 if (!is_t4(adap->params.chip)) {
3221 etq = &adap->sge.ptptxq;
3222 if (etq->q.desc) {
3223 t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
3224 etq->q.cntxt_id);
3225 spin_lock_bh(&adap->ptp_lock);
3226 free_tx_desc(adap, &etq->q, etq->q.in_use, true);
3227 spin_unlock_bh(&adap->ptp_lock);
3228 kfree(etq->q.sdesc);
3229 free_txq(adap, &etq->q);
3230 }
3231 }
3232
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003233 /* clear the reverse egress queue map */
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303234 memset(adap->sge.egr_map, 0,
3235 adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003236}
3237
3238void t4_sge_start(struct adapter *adap)
3239{
3240 adap->sge.ethtxq_rover = 0;
3241 mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
3242 mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
3243}
3244
3245/**
3246 * t4_sge_stop - disable SGE operation
3247 * @adap: the adapter
3248 *
3249 * Stop tasklets and timers associated with the DMA engine. Note that
3250 * this is effective only if measures have been taken to disable any HW
3251 * events that may restart them.
3252 */
3253void t4_sge_stop(struct adapter *adap)
3254{
3255 int i;
3256 struct sge *s = &adap->sge;
3257
3258 if (in_interrupt()) /* actions below require waiting */
3259 return;
3260
3261 if (s->rx_timer.function)
3262 del_timer_sync(&s->rx_timer);
3263 if (s->tx_timer.function)
3264 del_timer_sync(&s->tx_timer);
3265
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303266 if (is_offload(adap)) {
3267 struct sge_uld_txq_info *txq_info;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003268
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303269 txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
3270 if (txq_info) {
3271 struct sge_uld_txq *txq = txq_info->uldtxq;
3272
3273 for_each_ofldtxq(&adap->sge, i) {
3274 if (txq->q.desc)
3275 tasklet_kill(&txq->qresume_tsk);
3276 }
3277 }
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003278 }
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05303279
3280 if (is_pci_uld(adap)) {
3281 struct sge_uld_txq_info *txq_info;
3282
3283 txq_info = adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
3284 if (txq_info) {
3285 struct sge_uld_txq *txq = txq_info->uldtxq;
3286
3287 for_each_ofldtxq(&adap->sge, i) {
3288 if (txq->q.desc)
3289 tasklet_kill(&txq->qresume_tsk);
3290 }
3291 }
3292 }
3293
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003294 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
3295 struct sge_ctrl_txq *cq = &s->ctrlq[i];
3296
3297 if (cq->q.desc)
3298 tasklet_kill(&cq->qresume_tsk);
3299 }
3300}
3301
3302/**
Hariprasad Shenai06640312015-01-13 15:19:25 +05303303 * t4_sge_init_soft - grab core SGE values needed by SGE code
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003304 * @adap: the adapter
3305 *
Hariprasad Shenai06640312015-01-13 15:19:25 +05303306 * We need to grab the SGE operating parameters that we need to have
3307 * in order to do our job and make sure we can live with them.
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003308 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003309
Vipul Pandya52367a72012-09-26 02:39:38 +00003310static int t4_sge_init_soft(struct adapter *adap)
3311{
3312 struct sge *s = &adap->sge;
3313 u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
3314 u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
3315 u32 ingress_rx_threshold;
3316
3317 /*
3318 * Verify that CPL messages are going to the Ingress Queue for
3319 * process_responses() and that only packet data is going to the
3320 * Free Lists.
3321 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303322 if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
3323 RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
Vipul Pandya52367a72012-09-26 02:39:38 +00003324 dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
3325 return -EINVAL;
3326 }
3327
3328 /*
3329 * Validate the Host Buffer Register Array indices that we want to
3330 * use ...
3331 *
3332 * XXX Note that we should really read through the Host Buffer Size
3333 * XXX register array and find the indices of the Buffer Sizes which
3334 * XXX meet our needs!
3335 */
3336 #define READ_FL_BUF(x) \
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303337 t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
Vipul Pandya52367a72012-09-26 02:39:38 +00003338
3339 fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
3340 fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
3341 fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
3342 fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
3343
Kumar Sanghvi92ddcc72014-03-13 20:50:46 +05303344 /* We only bother using the Large Page logic if the Large Page Buffer
3345 * is larger than our Page Size Buffer.
3346 */
3347 if (fl_large_pg <= fl_small_pg)
3348 fl_large_pg = 0;
3349
Vipul Pandya52367a72012-09-26 02:39:38 +00003350 #undef READ_FL_BUF
3351
Kumar Sanghvi92ddcc72014-03-13 20:50:46 +05303352 /* The Page Size Buffer must be exactly equal to our Page Size and the
3353 * Large Page Size Buffer should be 0 (per above) or a power of 2.
3354 */
Vipul Pandya52367a72012-09-26 02:39:38 +00003355 if (fl_small_pg != PAGE_SIZE ||
Kumar Sanghvi92ddcc72014-03-13 20:50:46 +05303356 (fl_large_pg & (fl_large_pg-1)) != 0) {
Vipul Pandya52367a72012-09-26 02:39:38 +00003357 dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
3358 fl_small_pg, fl_large_pg);
3359 return -EINVAL;
3360 }
3361 if (fl_large_pg)
3362 s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
3363
3364 if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
3365 fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
3366 dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
3367 fl_small_mtu, fl_large_mtu);
3368 return -EINVAL;
3369 }
3370
3371 /*
3372 * Retrieve our RX interrupt holdoff timer values and counter
3373 * threshold values from the SGE parameters.
3374 */
Hariprasad Shenaif061de42015-01-05 16:30:44 +05303375 timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
3376 timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
3377 timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
Vipul Pandya52367a72012-09-26 02:39:38 +00003378 s->timer_val[0] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05303379 TIMERVALUE0_G(timer_value_0_and_1));
Vipul Pandya52367a72012-09-26 02:39:38 +00003380 s->timer_val[1] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05303381 TIMERVALUE1_G(timer_value_0_and_1));
Vipul Pandya52367a72012-09-26 02:39:38 +00003382 s->timer_val[2] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05303383 TIMERVALUE2_G(timer_value_2_and_3));
Vipul Pandya52367a72012-09-26 02:39:38 +00003384 s->timer_val[3] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05303385 TIMERVALUE3_G(timer_value_2_and_3));
Vipul Pandya52367a72012-09-26 02:39:38 +00003386 s->timer_val[4] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05303387 TIMERVALUE4_G(timer_value_4_and_5));
Vipul Pandya52367a72012-09-26 02:39:38 +00003388 s->timer_val[5] = core_ticks_to_us(adap,
Hariprasad Shenaif061de42015-01-05 16:30:44 +05303389 TIMERVALUE5_G(timer_value_4_and_5));
Vipul Pandya52367a72012-09-26 02:39:38 +00003390
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303391 ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
3392 s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
3393 s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
3394 s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
3395 s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
Vipul Pandya52367a72012-09-26 02:39:38 +00003396
3397 return 0;
3398}
3399
Hariprasad Shenai06640312015-01-13 15:19:25 +05303400/**
3401 * t4_sge_init - initialize SGE
3402 * @adap: the adapter
3403 *
3404 * Perform low-level SGE code initialization needed every time after a
3405 * chip reset.
3406 */
Vipul Pandya52367a72012-09-26 02:39:38 +00003407int t4_sge_init(struct adapter *adap)
3408{
3409 struct sge *s = &adap->sge;
Hariprasad Shenaiacac5962015-12-23 22:47:13 +05303410 u32 sge_control, sge_conm_ctrl;
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303411 int ret, egress_threshold;
Vipul Pandya52367a72012-09-26 02:39:38 +00003412
3413 /*
3414 * Ingress Padding Boundary and Egress Status Page Size are set up by
3415 * t4_fixup_host_params().
3416 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303417 sge_control = t4_read_reg(adap, SGE_CONTROL_A);
3418 s->pktshift = PKTSHIFT_G(sge_control);
3419 s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
Hariprasad Shenaice8f4072014-11-07 17:06:30 +05303420
Hariprasad Shenaiacac5962015-12-23 22:47:13 +05303421 s->fl_align = t4_fl_pkt_align(adap);
Hariprasad Shenai06640312015-01-13 15:19:25 +05303422 ret = t4_sge_init_soft(adap);
Vipul Pandya52367a72012-09-26 02:39:38 +00003423 if (ret < 0)
3424 return ret;
3425
3426 /*
3427 * A FL with <= fl_starve_thres buffers is starving and a periodic
3428 * timer will attempt to refill it. This needs to be larger than the
3429 * SGE's Egress Congestion Threshold. If it isn't, then we can get
3430 * stuck waiting for new packets while the SGE is waiting for us to
3431 * give it more Free List entries. (Note that the SGE's Egress
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303432 * Congestion Threshold is in units of 2 Free List pointers.) For T4,
3433 * there was only a single field to control this. For T5 there's the
3434 * original field which now only applies to Unpacked Mode Free List
3435 * buffers and a new field which only applies to Packed Mode Free List
3436 * buffers.
Vipul Pandya52367a72012-09-26 02:39:38 +00003437 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303438 sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05303439 switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
3440 case CHELSIO_T4:
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303441 egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05303442 break;
3443 case CHELSIO_T5:
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303444 egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05303445 break;
3446 case CHELSIO_T6:
3447 egress_threshold = T6_EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
3448 break;
3449 default:
3450 dev_err(adap->pdev_dev, "Unsupported Chip version %d\n",
3451 CHELSIO_CHIP_VERSION(adap->params.chip));
3452 return -EINVAL;
3453 }
Kumar Sanghvic2b955e2014-03-13 20:50:49 +05303454 s->fl_starve_thres = 2*egress_threshold + 1;
Vipul Pandya52367a72012-09-26 02:39:38 +00003455
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05303456 t4_idma_monitor_init(adap, &s->idma_monitor);
3457
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05303458 /* Set up timers used for recuring callbacks to process RX and TX
3459 * administrative tasks.
3460 */
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003461 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
3462 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
Hariprasad Shenaia3bfb612015-05-05 14:59:55 +05303463
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003464 spin_lock_init(&s->intrq_lock);
Vipul Pandya52367a72012-09-26 02:39:38 +00003465
3466 return 0;
Dimitris Michailidisfd3a4792010-04-01 15:28:24 +00003467}