blob: 2e41d1541d73db5fdfad8e4be9f78a7c8b64b2cf [file] [log] [blame]
Casey Leedomc6e0d912010-06-25 12:13:28 +00001/*
2 * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
3 * driver for Linux.
4 *
5 * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 */
35
36#include <linux/skbuff.h>
37#include <linux/netdevice.h>
38#include <linux/etherdevice.h>
39#include <linux/if_vlan.h>
40#include <linux/ip.h>
41#include <net/ipv6.h>
42#include <net/tcp.h>
43#include <linux/dma-mapping.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040044#include <linux/prefetch.h>
Casey Leedomc6e0d912010-06-25 12:13:28 +000045
46#include "t4vf_common.h"
47#include "t4vf_defs.h"
48
49#include "../cxgb4/t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053050#include "../cxgb4/t4_values.h"
Casey Leedomc6e0d912010-06-25 12:13:28 +000051#include "../cxgb4/t4fw_api.h"
52#include "../cxgb4/t4_msg.h"
53
54/*
Casey Leedomc6e0d912010-06-25 12:13:28 +000055 * Constants ...
56 */
57enum {
58 /*
59 * Egress Queue sizes, producer and consumer indices are all in units
60 * of Egress Context Units bytes. Note that as far as the hardware is
61 * concerned, the free list is an Egress Queue (the host produces free
62 * buffers which the hardware consumes) and free list entries are
63 * 64-bit PCI DMA addresses.
64 */
65 EQ_UNIT = SGE_EQ_IDXSIZE,
66 FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
67 TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
68
69 /*
70 * Max number of TX descriptors we clean up at a time. Should be
71 * modest as freeing skbs isn't cheap and it happens while holding
72 * locks. We just need to free packets faster than they arrive, we
73 * eventually catch up and keep the amortized cost reasonable.
74 */
75 MAX_TX_RECLAIM = 16,
76
77 /*
78 * Max number of Rx buffers we replenish at a time. Again keep this
79 * modest, allocating buffers isn't cheap either.
80 */
81 MAX_RX_REFILL = 16,
82
83 /*
84 * Period of the Rx queue check timer. This timer is infrequent as it
85 * has something to do only when the system experiences severe memory
86 * shortage.
87 */
88 RX_QCHECK_PERIOD = (HZ / 2),
89
90 /*
91 * Period of the TX queue check timer and the maximum number of TX
92 * descriptors to be reclaimed by the TX timer.
93 */
94 TX_QCHECK_PERIOD = (HZ / 2),
95 MAX_TIMER_TX_RECLAIM = 100,
96
97 /*
Casey Leedomc6e0d912010-06-25 12:13:28 +000098 * Suspend an Ethernet TX queue with fewer available descriptors than
99 * this. We always want to have room for a maximum sized packet:
100 * inline immediate data + MAX_SKB_FRAGS. This is the same as
101 * calc_tx_flits() for a TSO packet with nr_frags == MAX_SKB_FRAGS
102 * (see that function and its helpers for a description of the
103 * calculation).
104 */
105 ETHTXQ_MAX_FRAGS = MAX_SKB_FRAGS + 1,
106 ETHTXQ_MAX_SGL_LEN = ((3 * (ETHTXQ_MAX_FRAGS-1))/2 +
107 ((ETHTXQ_MAX_FRAGS-1) & 1) +
108 2),
109 ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) +
110 sizeof(struct cpl_tx_pkt_lso_core) +
111 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64),
112 ETHTXQ_MAX_FLITS = ETHTXQ_MAX_SGL_LEN + ETHTXQ_MAX_HDR,
113
114 ETHTXQ_STOP_THRES = 1 + DIV_ROUND_UP(ETHTXQ_MAX_FLITS, TXD_PER_EQ_UNIT),
115
116 /*
117 * Max TX descriptor space we allow for an Ethernet packet to be
118 * inlined into a WR. This is limited by the maximum value which
119 * we can specify for immediate data in the firmware Ethernet TX
120 * Work Request.
121 */
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530122 MAX_IMM_TX_PKT_LEN = FW_WR_IMMDLEN_M,
Casey Leedomc6e0d912010-06-25 12:13:28 +0000123
124 /*
125 * Max size of a WR sent through a control TX queue.
126 */
127 MAX_CTRL_WR_LEN = 256,
128
129 /*
130 * Maximum amount of data which we'll ever need to inline into a
131 * TX ring: max(MAX_IMM_TX_PKT_LEN, MAX_CTRL_WR_LEN).
132 */
133 MAX_IMM_TX_LEN = (MAX_IMM_TX_PKT_LEN > MAX_CTRL_WR_LEN
134 ? MAX_IMM_TX_PKT_LEN
135 : MAX_CTRL_WR_LEN),
136
137 /*
138 * For incoming packets less than RX_COPY_THRES, we copy the data into
139 * an skb rather than referencing the data. We allocate enough
140 * in-line room in skb's to accommodate pulling in RX_PULL_LEN bytes
141 * of the data (header).
142 */
143 RX_COPY_THRES = 256,
144 RX_PULL_LEN = 128,
Casey Leedomc6e0d912010-06-25 12:13:28 +0000145
Casey Leedomeb6c5032010-11-11 09:06:50 +0000146 /*
147 * Main body length for sk_buffs used for RX Ethernet packets with
148 * fragments. Should be >= RX_PULL_LEN but possibly bigger to give
149 * pskb_may_pull() some room.
150 */
151 RX_SKB_LEN = 512,
152};
Casey Leedomc6e0d912010-06-25 12:13:28 +0000153
154/*
155 * Software state per TX descriptor.
156 */
157struct tx_sw_desc {
158 struct sk_buff *skb; /* socket buffer of TX data source */
159 struct ulptx_sgl *sgl; /* scatter/gather list in TX Queue */
160};
161
162/*
163 * Software state per RX Free List descriptor. We keep track of the allocated
164 * FL page, its size, and its PCI DMA address (if the page is mapped). The FL
165 * page size and its PCI DMA mapped state are stored in the low bits of the
166 * PCI DMA address as per below.
167 */
168struct rx_sw_desc {
169 struct page *page; /* Free List page buffer */
170 dma_addr_t dma_addr; /* PCI DMA address (if mapped) */
171 /* and flags (see below) */
172};
173
174/*
175 * The low bits of rx_sw_desc.dma_addr have special meaning. Note that the
176 * SGE also uses the low 4 bits to determine the size of the buffer. It uses
177 * those bits to index into the SGE_FL_BUFFER_SIZE[index] register array.
178 * Since we only use SGE_FL_BUFFER_SIZE0 and SGE_FL_BUFFER_SIZE1, these low 4
179 * bits can only contain a 0 or a 1 to indicate which size buffer we're giving
180 * to the SGE. Thus, our software state of "is the buffer mapped for DMA" is
181 * maintained in an inverse sense so the hardware never sees that bit high.
182 */
183enum {
184 RX_LARGE_BUF = 1 << 0, /* buffer is SGE_FL_BUFFER_SIZE[1] */
185 RX_UNMAPPED_BUF = 1 << 1, /* buffer is not mapped */
186};
187
188/**
189 * get_buf_addr - return DMA buffer address of software descriptor
190 * @sdesc: pointer to the software buffer descriptor
191 *
192 * Return the DMA buffer address of a software descriptor (stripping out
193 * our low-order flag bits).
194 */
195static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *sdesc)
196{
197 return sdesc->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF);
198}
199
200/**
201 * is_buf_mapped - is buffer mapped for DMA?
202 * @sdesc: pointer to the software buffer descriptor
203 *
204 * Determine whether the buffer associated with a software descriptor in
205 * mapped for DMA or not.
206 */
207static inline bool is_buf_mapped(const struct rx_sw_desc *sdesc)
208{
209 return !(sdesc->dma_addr & RX_UNMAPPED_BUF);
210}
211
212/**
213 * need_skb_unmap - does the platform need unmapping of sk_buffs?
214 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300215 * Returns true if the platform needs sk_buff unmapping. The compiler
216 * optimizes away unnecessary code if this returns true.
Casey Leedomc6e0d912010-06-25 12:13:28 +0000217 */
218static inline int need_skb_unmap(void)
219{
FUJITA Tomonori57b2eaf2010-07-07 23:52:37 +0000220#ifdef CONFIG_NEED_DMA_MAP_STATE
221 return 1;
222#else
223 return 0;
224#endif
Casey Leedomc6e0d912010-06-25 12:13:28 +0000225}
226
227/**
228 * txq_avail - return the number of available slots in a TX queue
229 * @tq: the TX queue
230 *
231 * Returns the number of available descriptors in a TX queue.
232 */
233static inline unsigned int txq_avail(const struct sge_txq *tq)
234{
235 return tq->size - 1 - tq->in_use;
236}
237
238/**
239 * fl_cap - return the capacity of a Free List
240 * @fl: the Free List
241 *
242 * Returns the capacity of a Free List. The capacity is less than the
243 * size because an Egress Queue Index Unit worth of descriptors needs to
244 * be left unpopulated, otherwise the Producer and Consumer indices PIDX
245 * and CIDX will match and the hardware will think the FL is empty.
246 */
247static inline unsigned int fl_cap(const struct sge_fl *fl)
248{
249 return fl->size - FL_PER_EQ_UNIT;
250}
251
252/**
253 * fl_starving - return whether a Free List is starving.
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530254 * @adapter: pointer to the adapter
Casey Leedomc6e0d912010-06-25 12:13:28 +0000255 * @fl: the Free List
256 *
257 * Tests specified Free List to see whether the number of buffers
258 * available to the hardware has falled below our "starvation"
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300259 * threshold.
Casey Leedomc6e0d912010-06-25 12:13:28 +0000260 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530261static inline bool fl_starving(const struct adapter *adapter,
262 const struct sge_fl *fl)
Casey Leedomc6e0d912010-06-25 12:13:28 +0000263{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530264 const struct sge *s = &adapter->sge;
265
266 return fl->avail - fl->pend_cred <= s->fl_starve_thres;
Casey Leedomc6e0d912010-06-25 12:13:28 +0000267}
268
269/**
270 * map_skb - map an skb for DMA to the device
271 * @dev: the egress net device
272 * @skb: the packet to map
273 * @addr: a pointer to the base of the DMA mapping array
274 *
275 * Map an skb for DMA to the device and return an array of DMA addresses.
276 */
277static int map_skb(struct device *dev, const struct sk_buff *skb,
278 dma_addr_t *addr)
279{
280 const skb_frag_t *fp, *end;
281 const struct skb_shared_info *si;
282
283 *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
284 if (dma_mapping_error(dev, *addr))
285 goto out_err;
286
287 si = skb_shinfo(skb);
288 end = &si->frags[si->nr_frags];
289 for (fp = si->frags; fp < end; fp++) {
Ian Campbella0006a82011-10-19 23:01:47 +0000290 *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
291 DMA_TO_DEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000292 if (dma_mapping_error(dev, *addr))
293 goto unwind;
294 }
295 return 0;
296
297unwind:
298 while (fp-- > si->frags)
Eric Dumazet9e903e02011-10-18 21:00:24 +0000299 dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000300 dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
301
302out_err:
303 return -ENOMEM;
304}
305
306static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
307 const struct ulptx_sgl *sgl, const struct sge_txq *tq)
308{
309 const struct ulptx_sge_pair *p;
310 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
311
312 if (likely(skb_headlen(skb)))
313 dma_unmap_single(dev, be64_to_cpu(sgl->addr0),
314 be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
315 else {
316 dma_unmap_page(dev, be64_to_cpu(sgl->addr0),
317 be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
318 nfrags--;
319 }
320
321 /*
322 * the complexity below is because of the possibility of a wrap-around
323 * in the middle of an SGL
324 */
325 for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
326 if (likely((u8 *)(p + 1) <= (u8 *)tq->stat)) {
327unmap:
328 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
329 be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
330 dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
331 be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
332 p++;
333 } else if ((u8 *)p == (u8 *)tq->stat) {
334 p = (const struct ulptx_sge_pair *)tq->desc;
335 goto unmap;
336 } else if ((u8 *)p + 8 == (u8 *)tq->stat) {
337 const __be64 *addr = (const __be64 *)tq->desc;
338
339 dma_unmap_page(dev, be64_to_cpu(addr[0]),
340 be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
341 dma_unmap_page(dev, be64_to_cpu(addr[1]),
342 be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
343 p = (const struct ulptx_sge_pair *)&addr[2];
344 } else {
345 const __be64 *addr = (const __be64 *)tq->desc;
346
347 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
348 be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
349 dma_unmap_page(dev, be64_to_cpu(addr[0]),
350 be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
351 p = (const struct ulptx_sge_pair *)&addr[1];
352 }
353 }
354 if (nfrags) {
355 __be64 addr;
356
357 if ((u8 *)p == (u8 *)tq->stat)
358 p = (const struct ulptx_sge_pair *)tq->desc;
359 addr = ((u8 *)p + 16 <= (u8 *)tq->stat
360 ? p->addr[0]
361 : *(const __be64 *)tq->desc);
362 dma_unmap_page(dev, be64_to_cpu(addr), be32_to_cpu(p->len[0]),
363 DMA_TO_DEVICE);
364 }
365}
366
367/**
368 * free_tx_desc - reclaims TX descriptors and their buffers
369 * @adapter: the adapter
370 * @tq: the TX queue to reclaim descriptors from
371 * @n: the number of descriptors to reclaim
372 * @unmap: whether the buffers should be unmapped for DMA
373 *
374 * Reclaims TX descriptors from an SGE TX queue and frees the associated
375 * TX buffers. Called with the TX queue lock held.
376 */
377static void free_tx_desc(struct adapter *adapter, struct sge_txq *tq,
378 unsigned int n, bool unmap)
379{
380 struct tx_sw_desc *sdesc;
381 unsigned int cidx = tq->cidx;
382 struct device *dev = adapter->pdev_dev;
383
384 const int need_unmap = need_skb_unmap() && unmap;
385
386 sdesc = &tq->sdesc[cidx];
387 while (n--) {
388 /*
389 * If we kept a reference to the original TX skb, we need to
390 * unmap it from PCI DMA space (if required) and free it.
391 */
392 if (sdesc->skb) {
393 if (need_unmap)
394 unmap_sgl(dev, sdesc->skb, sdesc->sgl, tq);
Eric W. Biederman42ffda52014-03-15 16:31:32 -0700395 dev_consume_skb_any(sdesc->skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000396 sdesc->skb = NULL;
397 }
398
399 sdesc++;
400 if (++cidx == tq->size) {
401 cidx = 0;
402 sdesc = tq->sdesc;
403 }
404 }
405 tq->cidx = cidx;
406}
407
408/*
409 * Return the number of reclaimable descriptors in a TX queue.
410 */
411static inline int reclaimable(const struct sge_txq *tq)
412{
413 int hw_cidx = be16_to_cpu(tq->stat->cidx);
414 int reclaimable = hw_cidx - tq->cidx;
415 if (reclaimable < 0)
416 reclaimable += tq->size;
417 return reclaimable;
418}
419
420/**
421 * reclaim_completed_tx - reclaims completed TX descriptors
422 * @adapter: the adapter
423 * @tq: the TX queue to reclaim completed descriptors from
424 * @unmap: whether the buffers should be unmapped for DMA
425 *
426 * Reclaims TX descriptors that the SGE has indicated it has processed,
427 * and frees the associated buffers if possible. Called with the TX
428 * queue locked.
429 */
430static inline void reclaim_completed_tx(struct adapter *adapter,
431 struct sge_txq *tq,
432 bool unmap)
433{
434 int avail = reclaimable(tq);
435
436 if (avail) {
437 /*
438 * Limit the amount of clean up work we do at a time to keep
439 * the TX lock hold time O(1).
440 */
441 if (avail > MAX_TX_RECLAIM)
442 avail = MAX_TX_RECLAIM;
443
444 free_tx_desc(adapter, tq, avail, unmap);
445 tq->in_use -= avail;
446 }
447}
448
449/**
450 * get_buf_size - return the size of an RX Free List buffer.
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530451 * @adapter: pointer to the associated adapter
Casey Leedomc6e0d912010-06-25 12:13:28 +0000452 * @sdesc: pointer to the software buffer descriptor
453 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530454static inline int get_buf_size(const struct adapter *adapter,
455 const struct rx_sw_desc *sdesc)
Casey Leedomc6e0d912010-06-25 12:13:28 +0000456{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530457 const struct sge *s = &adapter->sge;
458
459 return (s->fl_pg_order > 0 && (sdesc->dma_addr & RX_LARGE_BUF)
460 ? (PAGE_SIZE << s->fl_pg_order) : PAGE_SIZE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000461}
462
463/**
464 * free_rx_bufs - free RX buffers on an SGE Free List
465 * @adapter: the adapter
466 * @fl: the SGE Free List to free buffers from
467 * @n: how many buffers to free
468 *
469 * Release the next @n buffers on an SGE Free List RX queue. The
470 * buffers must be made inaccessible to hardware before calling this
471 * function.
472 */
473static void free_rx_bufs(struct adapter *adapter, struct sge_fl *fl, int n)
474{
475 while (n--) {
476 struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
477
478 if (is_buf_mapped(sdesc))
479 dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530480 get_buf_size(adapter, sdesc),
481 PCI_DMA_FROMDEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000482 put_page(sdesc->page);
483 sdesc->page = NULL;
484 if (++fl->cidx == fl->size)
485 fl->cidx = 0;
486 fl->avail--;
487 }
488}
489
490/**
491 * unmap_rx_buf - unmap the current RX buffer on an SGE Free List
492 * @adapter: the adapter
493 * @fl: the SGE Free List
494 *
495 * Unmap the current buffer on an SGE Free List RX queue. The
496 * buffer must be made inaccessible to HW before calling this function.
497 *
498 * This is similar to @free_rx_bufs above but does not free the buffer.
499 * Do note that the FL still loses any further access to the buffer.
500 * This is used predominantly to "transfer ownership" of an FL buffer
501 * to another entity (typically an skb's fragment list).
502 */
503static void unmap_rx_buf(struct adapter *adapter, struct sge_fl *fl)
504{
505 struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
506
507 if (is_buf_mapped(sdesc))
508 dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530509 get_buf_size(adapter, sdesc),
510 PCI_DMA_FROMDEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000511 sdesc->page = NULL;
512 if (++fl->cidx == fl->size)
513 fl->cidx = 0;
514 fl->avail--;
515}
516
517/**
518 * ring_fl_db - righ doorbell on free list
519 * @adapter: the adapter
520 * @fl: the Free List whose doorbell should be rung ...
521 *
522 * Tell the Scatter Gather Engine that there are new free list entries
523 * available.
524 */
525static inline void ring_fl_db(struct adapter *adapter, struct sge_fl *fl)
526{
Santosh Rastapur622c62b2013-03-14 05:08:57 +0000527 u32 val;
528
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530529 /* The SGE keeps track of its Producer and Consumer Indices in terms
Casey Leedomc6e0d912010-06-25 12:13:28 +0000530 * of Egress Queue Units so we can only tell it about integral numbers
531 * of multiples of Free List Entries per Egress Queue Units ...
532 */
533 if (fl->pend_cred >= FL_PER_EQ_UNIT) {
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530534 if (is_t4(adapter->params.chip))
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530535 val = PIDX_V(fl->pend_cred / FL_PER_EQ_UNIT);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530536 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530537 val = PIDX_T5_V(fl->pend_cred / FL_PER_EQ_UNIT) |
538 DBTYPE_F;
539 val |= DBPRIO_F;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530540
541 /* Make sure all memory writes to the Free List queue are
542 * committed before we tell the hardware about them.
543 */
Casey Leedomc6e0d912010-06-25 12:13:28 +0000544 wmb();
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530545
546 /* If we don't have access to the new User Doorbell (T5+), use
547 * the old doorbell mechanism; otherwise use the new BAR2
548 * mechanism.
549 */
550 if (unlikely(fl->bar2_addr == NULL)) {
551 t4_write_reg(adapter,
552 T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530553 QID_V(fl->cntxt_id) | val);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530554 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530555 writel(val | QID_V(fl->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530556 fl->bar2_addr + SGE_UDB_KDOORBELL);
557
558 /* This Write memory Barrier will force the write to
559 * the User Doorbell area to be flushed.
560 */
561 wmb();
562 }
Casey Leedomc6e0d912010-06-25 12:13:28 +0000563 fl->pend_cred %= FL_PER_EQ_UNIT;
564 }
565}
566
567/**
568 * set_rx_sw_desc - initialize software RX buffer descriptor
569 * @sdesc: pointer to the softwore RX buffer descriptor
570 * @page: pointer to the page data structure backing the RX buffer
571 * @dma_addr: PCI DMA address (possibly with low-bit flags)
572 */
573static inline void set_rx_sw_desc(struct rx_sw_desc *sdesc, struct page *page,
574 dma_addr_t dma_addr)
575{
576 sdesc->page = page;
577 sdesc->dma_addr = dma_addr;
578}
579
580/*
581 * Support for poisoning RX buffers ...
582 */
583#define POISON_BUF_VAL -1
584
585static inline void poison_buf(struct page *page, size_t sz)
586{
587#if POISON_BUF_VAL >= 0
588 memset(page_address(page), POISON_BUF_VAL, sz);
589#endif
590}
591
592/**
593 * refill_fl - refill an SGE RX buffer ring
594 * @adapter: the adapter
595 * @fl: the Free List ring to refill
596 * @n: the number of new buffers to allocate
597 * @gfp: the gfp flags for the allocations
598 *
599 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
600 * allocated with the supplied gfp flags. The caller must assure that
601 * @n does not exceed the queue's capacity -- i.e. (cidx == pidx) _IN
602 * EGRESS QUEUE UNITS_ indicates an empty Free List! Returns the number
603 * of buffers allocated. If afterwards the queue is found critically low,
604 * mark it as starving in the bitmap of starving FLs.
605 */
606static unsigned int refill_fl(struct adapter *adapter, struct sge_fl *fl,
607 int n, gfp_t gfp)
608{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530609 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +0000610 struct page *page;
611 dma_addr_t dma_addr;
612 unsigned int cred = fl->avail;
613 __be64 *d = &fl->desc[fl->pidx];
614 struct rx_sw_desc *sdesc = &fl->sdesc[fl->pidx];
615
616 /*
617 * Sanity: ensure that the result of adding n Free List buffers
618 * won't result in wrapping the SGE's Producer Index around to
619 * it's Consumer Index thereby indicating an empty Free List ...
620 */
621 BUG_ON(fl->avail + n > fl->size - FL_PER_EQ_UNIT);
622
Alexander Duyckaa9cd312014-11-11 09:26:42 -0800623 gfp |= __GFP_NOWARN;
624
Casey Leedomc6e0d912010-06-25 12:13:28 +0000625 /*
626 * If we support large pages, prefer large buffers and fail over to
627 * small pages if we can't allocate large pages to satisfy the refill.
628 * If we don't support large pages, drop directly into the small page
629 * allocation code.
630 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530631 if (s->fl_pg_order == 0)
Casey Leedomc6e0d912010-06-25 12:13:28 +0000632 goto alloc_small_pages;
633
634 while (n) {
David S. Miller076ce442014-11-14 01:01:12 -0500635 page = __dev_alloc_pages(gfp, s->fl_pg_order);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000636 if (unlikely(!page)) {
637 /*
638 * We've failed inour attempt to allocate a "large
639 * page". Fail over to the "small page" allocation
640 * below.
641 */
642 fl->large_alloc_failed++;
643 break;
644 }
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530645 poison_buf(page, PAGE_SIZE << s->fl_pg_order);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000646
647 dma_addr = dma_map_page(adapter->pdev_dev, page, 0,
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530648 PAGE_SIZE << s->fl_pg_order,
Casey Leedomc6e0d912010-06-25 12:13:28 +0000649 PCI_DMA_FROMDEVICE);
650 if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
651 /*
652 * We've run out of DMA mapping space. Free up the
653 * buffer and return with what we've managed to put
654 * into the free list. We don't want to fail over to
655 * the small page allocation below in this case
656 * because DMA mapping resources are typically
657 * critical resources once they become scarse.
658 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530659 __free_pages(page, s->fl_pg_order);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000660 goto out;
661 }
662 dma_addr |= RX_LARGE_BUF;
663 *d++ = cpu_to_be64(dma_addr);
664
665 set_rx_sw_desc(sdesc, page, dma_addr);
666 sdesc++;
667
668 fl->avail++;
669 if (++fl->pidx == fl->size) {
670 fl->pidx = 0;
671 sdesc = fl->sdesc;
672 d = fl->desc;
673 }
674 n--;
675 }
676
677alloc_small_pages:
678 while (n--) {
Alexander Duyckaa9cd312014-11-11 09:26:42 -0800679 page = __dev_alloc_page(gfp);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000680 if (unlikely(!page)) {
681 fl->alloc_failed++;
682 break;
683 }
684 poison_buf(page, PAGE_SIZE);
685
686 dma_addr = dma_map_page(adapter->pdev_dev, page, 0, PAGE_SIZE,
687 PCI_DMA_FROMDEVICE);
688 if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000689 put_page(page);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000690 break;
691 }
692 *d++ = cpu_to_be64(dma_addr);
693
694 set_rx_sw_desc(sdesc, page, dma_addr);
695 sdesc++;
696
697 fl->avail++;
698 if (++fl->pidx == fl->size) {
699 fl->pidx = 0;
700 sdesc = fl->sdesc;
701 d = fl->desc;
702 }
703 }
704
705out:
706 /*
707 * Update our accounting state to incorporate the new Free List
708 * buffers, tell the hardware about them and return the number of
Paul Bolle90802ed2011-12-05 13:00:34 +0100709 * buffers which we were able to allocate.
Casey Leedomc6e0d912010-06-25 12:13:28 +0000710 */
711 cred = fl->avail - cred;
712 fl->pend_cred += cred;
713 ring_fl_db(adapter, fl);
714
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530715 if (unlikely(fl_starving(adapter, fl))) {
Casey Leedomc6e0d912010-06-25 12:13:28 +0000716 smp_wmb();
717 set_bit(fl->cntxt_id, adapter->sge.starving_fl);
718 }
719
720 return cred;
721}
722
723/*
724 * Refill a Free List to its capacity or the Maximum Refill Increment,
725 * whichever is smaller ...
726 */
727static inline void __refill_fl(struct adapter *adapter, struct sge_fl *fl)
728{
729 refill_fl(adapter, fl,
730 min((unsigned int)MAX_RX_REFILL, fl_cap(fl) - fl->avail),
731 GFP_ATOMIC);
732}
733
734/**
735 * alloc_ring - allocate resources for an SGE descriptor ring
736 * @dev: the PCI device's core device
737 * @nelem: the number of descriptors
738 * @hwsize: the size of each hardware descriptor
739 * @swsize: the size of each software descriptor
740 * @busaddrp: the physical PCI bus address of the allocated ring
741 * @swringp: return address pointer for software ring
742 * @stat_size: extra space in hardware ring for status information
743 *
744 * Allocates resources for an SGE descriptor ring, such as TX queues,
745 * free buffer lists, response queues, etc. Each SGE ring requires
746 * space for its hardware descriptors plus, optionally, space for software
747 * state associated with each hardware entry (the metadata). The function
748 * returns three values: the virtual address for the hardware ring (the
749 * return value of the function), the PCI bus address of the hardware
750 * ring (in *busaddrp), and the address of the software ring (in swringp).
751 * Both the hardware and software rings are returned zeroed out.
752 */
753static void *alloc_ring(struct device *dev, size_t nelem, size_t hwsize,
754 size_t swsize, dma_addr_t *busaddrp, void *swringp,
755 size_t stat_size)
756{
757 /*
758 * Allocate the hardware ring and PCI DMA bus address space for said.
759 */
760 size_t hwlen = nelem * hwsize + stat_size;
761 void *hwring = dma_alloc_coherent(dev, hwlen, busaddrp, GFP_KERNEL);
762
763 if (!hwring)
764 return NULL;
765
766 /*
767 * If the caller wants a software ring, allocate it and return a
768 * pointer to it in *swringp.
769 */
770 BUG_ON((swsize != 0) != (swringp != NULL));
771 if (swsize) {
772 void *swring = kcalloc(nelem, swsize, GFP_KERNEL);
773
774 if (!swring) {
775 dma_free_coherent(dev, hwlen, hwring, *busaddrp);
776 return NULL;
777 }
778 *(void **)swringp = swring;
779 }
780
781 /*
782 * Zero out the hardware ring and return its address as our function
783 * value.
784 */
785 memset(hwring, 0, hwlen);
786 return hwring;
787}
788
789/**
790 * sgl_len - calculates the size of an SGL of the given capacity
791 * @n: the number of SGL entries
792 *
793 * Calculates the number of flits (8-byte units) needed for a Direct
794 * Scatter/Gather List that can hold the given number of entries.
795 */
796static inline unsigned int sgl_len(unsigned int n)
797{
798 /*
799 * A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
800 * addresses. The DSGL Work Request starts off with a 32-bit DSGL
801 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
802 * repeated sequences of { Length[i], Length[i+1], Address[i],
803 * Address[i+1] } (this ensures that all addresses are on 64-bit
804 * boundaries). If N is even, then Length[N+1] should be set to 0 and
805 * Address[N+1] is omitted.
806 *
807 * The following calculation incorporates all of the above. It's
808 * somewhat hard to follow but, briefly: the "+2" accounts for the
809 * first two flits which include the DSGL header, Length0 and
810 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
811 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
812 * finally the "+((n-1)&1)" adds the one remaining flit needed if
813 * (n-1) is odd ...
814 */
815 n--;
816 return (3 * n) / 2 + (n & 1) + 2;
817}
818
819/**
820 * flits_to_desc - returns the num of TX descriptors for the given flits
821 * @flits: the number of flits
822 *
823 * Returns the number of TX descriptors needed for the supplied number
824 * of flits.
825 */
826static inline unsigned int flits_to_desc(unsigned int flits)
827{
828 BUG_ON(flits > SGE_MAX_WR_LEN / sizeof(__be64));
829 return DIV_ROUND_UP(flits, TXD_PER_EQ_UNIT);
830}
831
832/**
833 * is_eth_imm - can an Ethernet packet be sent as immediate data?
834 * @skb: the packet
835 *
836 * Returns whether an Ethernet packet is small enough to fit completely as
837 * immediate data.
838 */
839static inline int is_eth_imm(const struct sk_buff *skb)
840{
841 /*
842 * The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request
843 * which does not accommodate immediate data. We could dike out all
844 * of the support code for immediate data but that would tie our hands
845 * too much if we ever want to enhace the firmware. It would also
846 * create more differences between the PF and VF Drivers.
847 */
848 return false;
849}
850
851/**
852 * calc_tx_flits - calculate the number of flits for a packet TX WR
853 * @skb: the packet
854 *
855 * Returns the number of flits needed for a TX Work Request for the
856 * given Ethernet packet, including the needed WR and CPL headers.
857 */
858static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
859{
860 unsigned int flits;
861
862 /*
863 * If the skb is small enough, we can pump it out as a work request
864 * with only immediate data. In that case we just have to have the
865 * TX Packet header plus the skb data in the Work Request.
866 */
867 if (is_eth_imm(skb))
868 return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt),
869 sizeof(__be64));
870
871 /*
872 * Otherwise, we're going to have to construct a Scatter gather list
873 * of the skb body and fragments. We also include the flits necessary
874 * for the TX Packet Work Request and CPL. We always have a firmware
875 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
876 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
877 * message or, if we're doing a Large Send Offload, an LSO CPL message
Joe Perchesdbedd442015-03-06 20:49:12 -0800878 * with an embedded TX Packet Write CPL message.
Casey Leedomc6e0d912010-06-25 12:13:28 +0000879 */
880 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
881 if (skb_shinfo(skb)->gso_size)
882 flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
883 sizeof(struct cpl_tx_pkt_lso_core) +
884 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
885 else
886 flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
887 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
888 return flits;
889}
890
891/**
892 * write_sgl - populate a Scatter/Gather List for a packet
893 * @skb: the packet
894 * @tq: the TX queue we are writing into
895 * @sgl: starting location for writing the SGL
896 * @end: points right after the end of the SGL
897 * @start: start offset into skb main-body data to include in the SGL
898 * @addr: the list of DMA bus addresses for the SGL elements
899 *
900 * Generates a Scatter/Gather List for the buffers that make up a packet.
901 * The caller must provide adequate space for the SGL that will be written.
902 * The SGL includes all of the packet's page fragments and the data in its
903 * main body except for the first @start bytes. @pos must be 16-byte
904 * aligned and within a TX descriptor with available space. @end points
905 * write after the end of the SGL but does not account for any potential
906 * wrap around, i.e., @end > @tq->stat.
907 */
908static void write_sgl(const struct sk_buff *skb, struct sge_txq *tq,
909 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
910 const dma_addr_t *addr)
911{
912 unsigned int i, len;
913 struct ulptx_sge_pair *to;
914 const struct skb_shared_info *si = skb_shinfo(skb);
915 unsigned int nfrags = si->nr_frags;
916 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
917
918 len = skb_headlen(skb) - start;
919 if (likely(len)) {
920 sgl->len0 = htonl(len);
921 sgl->addr0 = cpu_to_be64(addr[0] + start);
922 nfrags++;
923 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000924 sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
Casey Leedomc6e0d912010-06-25 12:13:28 +0000925 sgl->addr0 = cpu_to_be64(addr[1]);
926 }
927
Anish Bhattd7990b02014-11-12 17:15:57 -0800928 sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800929 ULPTX_NSGE_V(nfrags));
Casey Leedomc6e0d912010-06-25 12:13:28 +0000930 if (likely(--nfrags == 0))
931 return;
932 /*
933 * Most of the complexity below deals with the possibility we hit the
934 * end of the queue in the middle of writing the SGL. For this case
935 * only we create the SGL in a temporary buffer and then copy it.
936 */
937 to = (u8 *)end > (u8 *)tq->stat ? buf : sgl->sge;
938
939 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000940 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
941 to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
Casey Leedomc6e0d912010-06-25 12:13:28 +0000942 to->addr[0] = cpu_to_be64(addr[i]);
943 to->addr[1] = cpu_to_be64(addr[++i]);
944 }
945 if (nfrags) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000946 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
Casey Leedomc6e0d912010-06-25 12:13:28 +0000947 to->len[1] = cpu_to_be32(0);
948 to->addr[0] = cpu_to_be64(addr[i + 1]);
949 }
950 if (unlikely((u8 *)end > (u8 *)tq->stat)) {
951 unsigned int part0 = (u8 *)tq->stat - (u8 *)sgl->sge, part1;
952
953 if (likely(part0))
954 memcpy(sgl->sge, buf, part0);
955 part1 = (u8 *)end - (u8 *)tq->stat;
956 memcpy(tq->desc, (u8 *)buf + part0, part1);
957 end = (void *)tq->desc + part1;
958 }
959 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
Joe Perches64699332012-06-04 12:44:16 +0000960 *end = 0;
Casey Leedomc6e0d912010-06-25 12:13:28 +0000961}
962
963/**
964 * check_ring_tx_db - check and potentially ring a TX queue's doorbell
965 * @adapter: the adapter
966 * @tq: the TX queue
967 * @n: number of new descriptors to give to HW
968 *
969 * Ring the doorbel for a TX queue.
970 */
971static inline void ring_tx_db(struct adapter *adapter, struct sge_txq *tq,
972 int n)
973{
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530974 /* Make sure that all writes to the TX Descriptors are committed
975 * before we tell the hardware about them.
Casey Leedomc6e0d912010-06-25 12:13:28 +0000976 */
Casey Leedomc6e0d912010-06-25 12:13:28 +0000977 wmb();
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530978
979 /* If we don't have access to the new User Doorbell (T5+), use the old
980 * doorbell mechanism; otherwise use the new BAR2 mechanism.
981 */
982 if (unlikely(tq->bar2_addr == NULL)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530983 u32 val = PIDX_V(n);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530984
985 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530986 QID_V(tq->cntxt_id) | val);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530987 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530988 u32 val = PIDX_T5_V(n);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530989
990 /* T4 and later chips share the same PIDX field offset within
991 * the doorbell, but T5 and later shrank the field in order to
992 * gain a bit for Doorbell Priority. The field was absurdly
993 * large in the first place (14 bits) so we just use the T5
994 * and later limits and warn if a Queue ID is too large.
995 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530996 WARN_ON(val & DBPRIO_F);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +0530997
998 /* If we're only writing a single Egress Unit and the BAR2
999 * Queue ID is 0, we can use the Write Combining Doorbell
1000 * Gather Buffer; otherwise we use the simple doorbell.
1001 */
1002 if (n == 1 && tq->bar2_qid == 0) {
1003 unsigned int index = (tq->pidx
1004 ? (tq->pidx - 1)
1005 : (tq->size - 1));
1006 __be64 *src = (__be64 *)&tq->desc[index];
Hariprasad Shenai2ff2acf2015-03-27 11:01:18 +05301007 __be64 __iomem *dst = (__be64 __iomem *)(tq->bar2_addr +
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301008 SGE_UDB_WCDOORBELL);
1009 unsigned int count = EQ_UNIT / sizeof(__be64);
1010
1011 /* Copy the TX Descriptor in a tight loop in order to
1012 * try to get it to the adapter in a single Write
1013 * Combined transfer on the PCI-E Bus. If the Write
1014 * Combine fails (say because of an interrupt, etc.)
1015 * the hardware will simply take the last write as a
1016 * simple doorbell write with a PIDX Increment of 1
1017 * and will fetch the TX Descriptor from memory via
1018 * DMA.
1019 */
1020 while (count) {
Hariprasad Shenai2ff2acf2015-03-27 11:01:18 +05301021 /* the (__force u64) is because the compiler
1022 * doesn't understand the endian swizzling
1023 * going on
1024 */
1025 writeq((__force u64)*src, dst);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301026 src++;
1027 dst++;
1028 count--;
1029 }
1030 } else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301031 writel(val | QID_V(tq->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301032 tq->bar2_addr + SGE_UDB_KDOORBELL);
1033
1034 /* This Write Memory Barrier will force the write to the User
1035 * Doorbell area to be flushed. This is needed to prevent
1036 * writes on different CPUs for the same queue from hitting
1037 * the adapter out of order. This is required when some Work
1038 * Requests take the Write Combine Gather Buffer path (user
1039 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
1040 * take the traditional path where we simply increment the
1041 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
1042 * hardware DMA read the actual Work Request.
1043 */
1044 wmb();
1045 }
Casey Leedomc6e0d912010-06-25 12:13:28 +00001046}
1047
1048/**
1049 * inline_tx_skb - inline a packet's data into TX descriptors
1050 * @skb: the packet
1051 * @tq: the TX queue where the packet will be inlined
1052 * @pos: starting position in the TX queue to inline the packet
1053 *
1054 * Inline a packet's contents directly into TX descriptors, starting at
1055 * the given position within the TX DMA ring.
1056 * Most of the complexity of this operation is dealing with wrap arounds
1057 * in the middle of the packet we want to inline.
1058 */
1059static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *tq,
1060 void *pos)
1061{
1062 u64 *p;
1063 int left = (void *)tq->stat - pos;
1064
1065 if (likely(skb->len <= left)) {
1066 if (likely(!skb->data_len))
1067 skb_copy_from_linear_data(skb, pos, skb->len);
1068 else
1069 skb_copy_bits(skb, 0, pos, skb->len);
1070 pos += skb->len;
1071 } else {
1072 skb_copy_bits(skb, 0, pos, left);
1073 skb_copy_bits(skb, left, tq->desc, skb->len - left);
1074 pos = (void *)tq->desc + (skb->len - left);
1075 }
1076
1077 /* 0-pad to multiple of 16 */
1078 p = PTR_ALIGN(pos, 8);
1079 if ((uintptr_t)p & 8)
1080 *p = 0;
1081}
1082
1083/*
1084 * Figure out what HW csum a packet wants and return the appropriate control
1085 * bits.
1086 */
1087static u64 hwcsum(const struct sk_buff *skb)
1088{
1089 int csum_type;
1090 const struct iphdr *iph = ip_hdr(skb);
1091
1092 if (iph->version == 4) {
1093 if (iph->protocol == IPPROTO_TCP)
1094 csum_type = TX_CSUM_TCPIP;
1095 else if (iph->protocol == IPPROTO_UDP)
1096 csum_type = TX_CSUM_UDPIP;
1097 else {
1098nocsum:
1099 /*
1100 * unknown protocol, disable HW csum
1101 * and hope a bad packet is detected
1102 */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301103 return TXPKT_L4CSUM_DIS_F;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001104 }
1105 } else {
1106 /*
1107 * this doesn't work with extension headers
1108 */
1109 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
1110
1111 if (ip6h->nexthdr == IPPROTO_TCP)
1112 csum_type = TX_CSUM_TCPIP6;
1113 else if (ip6h->nexthdr == IPPROTO_UDP)
1114 csum_type = TX_CSUM_UDPIP6;
1115 else
1116 goto nocsum;
1117 }
1118
1119 if (likely(csum_type >= TX_CSUM_TCPIP))
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301120 return TXPKT_CSUM_TYPE_V(csum_type) |
1121 TXPKT_IPHDR_LEN_V(skb_network_header_len(skb)) |
1122 TXPKT_ETHHDR_LEN_V(skb_network_offset(skb) - ETH_HLEN);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001123 else {
1124 int start = skb_transport_offset(skb);
1125
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301126 return TXPKT_CSUM_TYPE_V(csum_type) |
1127 TXPKT_CSUM_START_V(start) |
1128 TXPKT_CSUM_LOC_V(start + skb->csum_offset);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001129 }
1130}
1131
1132/*
1133 * Stop an Ethernet TX queue and record that state change.
1134 */
1135static void txq_stop(struct sge_eth_txq *txq)
1136{
1137 netif_tx_stop_queue(txq->txq);
1138 txq->q.stops++;
1139}
1140
1141/*
1142 * Advance our software state for a TX queue by adding n in use descriptors.
1143 */
1144static inline void txq_advance(struct sge_txq *tq, unsigned int n)
1145{
1146 tq->in_use += n;
1147 tq->pidx += n;
1148 if (tq->pidx >= tq->size)
1149 tq->pidx -= tq->size;
1150}
1151
1152/**
1153 * t4vf_eth_xmit - add a packet to an Ethernet TX queue
1154 * @skb: the packet
1155 * @dev: the egress net device
1156 *
1157 * Add a packet to an SGE Ethernet TX queue. Runs with softirqs disabled.
1158 */
1159int t4vf_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1160{
Casey Leedom7f9dd2f2010-07-12 14:39:07 -07001161 u32 wr_mid;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001162 u64 cntrl, *end;
Hariprasad Shenai637d3e92015-05-05 14:59:56 +05301163 int qidx, credits, max_pkt_len;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001164 unsigned int flits, ndesc;
1165 struct adapter *adapter;
1166 struct sge_eth_txq *txq;
1167 const struct port_info *pi;
1168 struct fw_eth_tx_pkt_vm_wr *wr;
1169 struct cpl_tx_pkt_core *cpl;
1170 const struct skb_shared_info *ssi;
1171 dma_addr_t addr[MAX_SKB_FRAGS + 1];
1172 const size_t fw_hdr_copy_len = (sizeof(wr->ethmacdst) +
1173 sizeof(wr->ethmacsrc) +
1174 sizeof(wr->ethtype) +
1175 sizeof(wr->vlantci));
1176
1177 /*
1178 * The chip minimum packet length is 10 octets but the firmware
1179 * command that we are using requires that we copy the Ethernet header
1180 * (including the VLAN tag) into the header so we reject anything
1181 * smaller than that ...
1182 */
1183 if (unlikely(skb->len < fw_hdr_copy_len))
1184 goto out_free;
1185
Hariprasad Shenai637d3e92015-05-05 14:59:56 +05301186 /* Discard the packet if the length is greater than mtu */
1187 max_pkt_len = ETH_HLEN + dev->mtu;
1188 if (skb_vlan_tag_present(skb))
1189 max_pkt_len += VLAN_HLEN;
1190 if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
1191 goto out_free;
1192
Casey Leedomc6e0d912010-06-25 12:13:28 +00001193 /*
1194 * Figure out which TX Queue we're going to use.
1195 */
1196 pi = netdev_priv(dev);
1197 adapter = pi->adapter;
1198 qidx = skb_get_queue_mapping(skb);
1199 BUG_ON(qidx >= pi->nqsets);
1200 txq = &adapter->sge.ethtxq[pi->first_qset + qidx];
1201
1202 /*
1203 * Take this opportunity to reclaim any TX Descriptors whose DMA
1204 * transfers have completed.
1205 */
1206 reclaim_completed_tx(adapter, &txq->q, true);
1207
1208 /*
1209 * Calculate the number of flits and TX Descriptors we're going to
1210 * need along with how many TX Descriptors will be left over after
1211 * we inject our Work Request.
1212 */
1213 flits = calc_tx_flits(skb);
1214 ndesc = flits_to_desc(flits);
1215 credits = txq_avail(&txq->q) - ndesc;
1216
1217 if (unlikely(credits < 0)) {
1218 /*
1219 * Not enough room for this packet's Work Request. Stop the
1220 * TX Queue and return a "busy" condition. The queue will get
1221 * started later on when the firmware informs us that space
1222 * has opened up.
1223 */
1224 txq_stop(txq);
1225 dev_err(adapter->pdev_dev,
1226 "%s: TX ring %u full while queue awake!\n",
1227 dev->name, qidx);
1228 return NETDEV_TX_BUSY;
1229 }
1230
1231 if (!is_eth_imm(skb) &&
1232 unlikely(map_skb(adapter->pdev_dev, skb, addr) < 0)) {
1233 /*
1234 * We need to map the skb into PCI DMA space (because it can't
1235 * be in-lined directly into the Work Request) and the mapping
1236 * operation failed. Record the error and drop the packet.
1237 */
1238 txq->mapping_err++;
1239 goto out_free;
1240 }
1241
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301242 wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001243 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1244 /*
1245 * After we're done injecting the Work Request for this
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001246 * packet, we'll be below our "stop threshold" so stop the TX
Casey Leedom7f9dd2f2010-07-12 14:39:07 -07001247 * Queue now and schedule a request for an SGE Egress Queue
1248 * Update message. The queue will get started later on when
1249 * the firmware processes this Work Request and sends us an
1250 * Egress Queue Status Update message indicating that space
1251 * has opened up.
Casey Leedomc6e0d912010-06-25 12:13:28 +00001252 */
1253 txq_stop(txq);
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301254 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001255 }
1256
1257 /*
1258 * Start filling in our Work Request. Note that we do _not_ handle
1259 * the WR Header wrapping around the TX Descriptor Ring. If our
1260 * maximum header size ever exceeds one TX Descriptor, we'll need to
1261 * do something else here.
1262 */
1263 BUG_ON(DIV_ROUND_UP(ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1);
1264 wr = (void *)&txq->q.desc[txq->q.pidx];
Casey Leedom7f9dd2f2010-07-12 14:39:07 -07001265 wr->equiq_to_len16 = cpu_to_be32(wr_mid);
Hariprasad Shenai2ff2acf2015-03-27 11:01:18 +05301266 wr->r3[0] = cpu_to_be32(0);
1267 wr->r3[1] = cpu_to_be32(0);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001268 skb_copy_from_linear_data(skb, (void *)wr->ethmacdst, fw_hdr_copy_len);
1269 end = (u64 *)wr + flits;
1270
1271 /*
1272 * If this is a Large Send Offload packet we'll put in an LSO CPL
1273 * message with an encapsulated TX Packet CPL message. Otherwise we
1274 * just use a TX Packet CPL message.
1275 */
1276 ssi = skb_shinfo(skb);
1277 if (ssi->gso_size) {
1278 struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
1279 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1280 int l3hdr_len = skb_network_header_len(skb);
1281 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1282
1283 wr->op_immdlen =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301284 cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
1285 FW_WR_IMMDLEN_V(sizeof(*lso) +
1286 sizeof(*cpl)));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001287 /*
1288 * Fill in the LSO CPL message.
1289 */
1290 lso->lso_ctrl =
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301291 cpu_to_be32(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
1292 LSO_FIRST_SLICE_F |
1293 LSO_LAST_SLICE_F |
1294 LSO_IPV6_V(v6) |
1295 LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
1296 LSO_IPHDR_LEN_V(l3hdr_len / 4) |
1297 LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001298 lso->ipid_ofst = cpu_to_be16(0);
1299 lso->mss = cpu_to_be16(ssi->gso_size);
1300 lso->seqno_offset = cpu_to_be32(0);
Hariprasad Shenai7207c0d2014-10-09 05:48:45 +05301301 if (is_t4(adapter->params.chip))
1302 lso->len = cpu_to_be32(skb->len);
1303 else
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301304 lso->len = cpu_to_be32(LSO_T5_XFER_SIZE_V(skb->len));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001305
1306 /*
1307 * Set up TX Packet CPL pointer, control word and perform
1308 * accounting.
1309 */
1310 cpl = (void *)(lso + 1);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301311 cntrl = (TXPKT_CSUM_TYPE_V(v6 ?
1312 TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
1313 TXPKT_IPHDR_LEN_V(l3hdr_len) |
1314 TXPKT_ETHHDR_LEN_V(eth_xtra_len));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001315 txq->tso++;
1316 txq->tx_cso += ssi->gso_segs;
1317 } else {
1318 int len;
1319
1320 len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
1321 wr->op_immdlen =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301322 cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
1323 FW_WR_IMMDLEN_V(len));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001324
1325 /*
1326 * Set up TX Packet CPL pointer, control word and perform
1327 * accounting.
1328 */
1329 cpl = (void *)(wr + 1);
1330 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301331 cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS_F;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001332 txq->tx_cso++;
1333 } else
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301334 cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001335 }
1336
1337 /*
1338 * If there's a VLAN tag present, add that to the list of things to
1339 * do in this Work Request.
1340 */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001341 if (skb_vlan_tag_present(skb)) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00001342 txq->vlan_ins++;
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301343 cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001344 }
1345
1346 /*
1347 * Fill in the TX Packet CPL message header.
1348 */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301349 cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE_V(CPL_TX_PKT_XT) |
1350 TXPKT_INTF_V(pi->port_id) |
1351 TXPKT_PF_V(0));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001352 cpl->pack = cpu_to_be16(0);
1353 cpl->len = cpu_to_be16(skb->len);
1354 cpl->ctrl1 = cpu_to_be64(cntrl);
1355
1356#ifdef T4_TRACE
1357 T4_TRACE5(adapter->tb[txq->q.cntxt_id & 7],
1358 "eth_xmit: ndesc %u, credits %u, pidx %u, len %u, frags %u",
1359 ndesc, credits, txq->q.pidx, skb->len, ssi->nr_frags);
1360#endif
1361
1362 /*
1363 * Fill in the body of the TX Packet CPL message with either in-lined
1364 * data or a Scatter/Gather List.
1365 */
1366 if (is_eth_imm(skb)) {
1367 /*
1368 * In-line the packet's data and free the skb since we don't
1369 * need it any longer.
1370 */
1371 inline_tx_skb(skb, &txq->q, cpl + 1);
Eric W. Biederman42ffda52014-03-15 16:31:32 -07001372 dev_consume_skb_any(skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001373 } else {
1374 /*
1375 * Write the skb's Scatter/Gather list into the TX Packet CPL
1376 * message and retain a pointer to the skb so we can free it
1377 * later when its DMA completes. (We store the skb pointer
1378 * in the Software Descriptor corresponding to the last TX
1379 * Descriptor used by the Work Request.)
1380 *
1381 * The retained skb will be freed when the corresponding TX
1382 * Descriptors are reclaimed after their DMAs complete.
1383 * However, this could take quite a while since, in general,
1384 * the hardware is set up to be lazy about sending DMA
1385 * completion notifications to us and we mostly perform TX
1386 * reclaims in the transmit routine.
1387 *
1388 * This is good for performamce but means that we rely on new
1389 * TX packets arriving to run the destructors of completed
1390 * packets, which open up space in their sockets' send queues.
1391 * Sometimes we do not get such new packets causing TX to
1392 * stall. A single UDP transmitter is a good example of this
1393 * situation. We have a clean up timer that periodically
1394 * reclaims completed packets but it doesn't run often enough
1395 * (nor do we want it to) to prevent lengthy stalls. A
1396 * solution to this problem is to run the destructor early,
1397 * after the packet is queued but before it's DMAd. A con is
1398 * that we lie to socket memory accounting, but the amount of
1399 * extra memory is reasonable (limited by the number of TX
1400 * descriptors), the packets do actually get freed quickly by
1401 * new packets almost always, and for protocols like TCP that
1402 * wait for acks to really free up the data the extra memory
1403 * is even less. On the positive side we run the destructors
1404 * on the sending CPU rather than on a potentially different
Casey Leedom64bb3362010-06-29 12:53:39 +00001405 * completing CPU, usually a good thing.
Casey Leedomc6e0d912010-06-25 12:13:28 +00001406 *
1407 * Run the destructor before telling the DMA engine about the
1408 * packet to make sure it doesn't complete and get freed
1409 * prematurely.
1410 */
1411 struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1);
1412 struct sge_txq *tq = &txq->q;
1413 int last_desc;
1414
1415 /*
1416 * If the Work Request header was an exact multiple of our TX
1417 * Descriptor length, then it's possible that the starting SGL
1418 * pointer lines up exactly with the end of our TX Descriptor
1419 * ring. If that's the case, wrap around to the beginning
1420 * here ...
1421 */
1422 if (unlikely((void *)sgl == (void *)tq->stat)) {
1423 sgl = (void *)tq->desc;
Joe Perches64699332012-06-04 12:44:16 +00001424 end = ((void *)tq->desc + ((void *)end - (void *)tq->stat));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001425 }
1426
1427 write_sgl(skb, tq, sgl, end, 0, addr);
1428 skb_orphan(skb);
1429
1430 last_desc = tq->pidx + ndesc - 1;
1431 if (last_desc >= tq->size)
1432 last_desc -= tq->size;
1433 tq->sdesc[last_desc].skb = skb;
1434 tq->sdesc[last_desc].sgl = sgl;
1435 }
1436
1437 /*
1438 * Advance our internal TX Queue state, tell the hardware about
1439 * the new TX descriptors and return success.
1440 */
1441 txq_advance(&txq->q, ndesc);
1442 dev->trans_start = jiffies;
1443 ring_tx_db(adapter, &txq->q, ndesc);
1444 return NETDEV_TX_OK;
1445
1446out_free:
1447 /*
1448 * An error of some sort happened. Free the TX skb and tell the
1449 * OS that we've "dealt" with the packet ...
1450 */
Eric W. Biederman42ffda52014-03-15 16:31:32 -07001451 dev_kfree_skb_any(skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001452 return NETDEV_TX_OK;
1453}
1454
1455/**
Ian Campbella0006a82011-10-19 23:01:47 +00001456 * copy_frags - copy fragments from gather list into skb_shared_info
1457 * @skb: destination skb
1458 * @gl: source internal packet gather list
1459 * @offset: packet start offset in first page
1460 *
1461 * Copy an internal packet gather list into a Linux skb_shared_info
1462 * structure.
1463 */
1464static inline void copy_frags(struct sk_buff *skb,
1465 const struct pkt_gl *gl,
1466 unsigned int offset)
1467{
1468 int i;
1469
1470 /* usually there's just one frag */
1471 __skb_fill_page_desc(skb, 0, gl->frags[0].page,
1472 gl->frags[0].offset + offset,
1473 gl->frags[0].size - offset);
1474 skb_shinfo(skb)->nr_frags = gl->nfrags;
1475 for (i = 1; i < gl->nfrags; i++)
1476 __skb_fill_page_desc(skb, i, gl->frags[i].page,
1477 gl->frags[i].offset,
1478 gl->frags[i].size);
1479
1480 /* get a reference to the last page, we don't own it */
1481 get_page(gl->frags[gl->nfrags - 1].page);
1482}
1483
1484/**
Casey Leedomeb6c5032010-11-11 09:06:50 +00001485 * t4vf_pktgl_to_skb - build an sk_buff from a packet gather list
1486 * @gl: the gather list
1487 * @skb_len: size of sk_buff main body if it carries fragments
1488 * @pull_len: amount of data to move to the sk_buff's main body
1489 *
1490 * Builds an sk_buff from the given packet gather list. Returns the
1491 * sk_buff or %NULL if sk_buff allocation failed.
1492 */
Sachin Kamat8a67d1c2013-09-18 09:00:01 +05301493static struct sk_buff *t4vf_pktgl_to_skb(const struct pkt_gl *gl,
1494 unsigned int skb_len,
1495 unsigned int pull_len)
Casey Leedomeb6c5032010-11-11 09:06:50 +00001496{
1497 struct sk_buff *skb;
Casey Leedomeb6c5032010-11-11 09:06:50 +00001498
1499 /*
1500 * If the ingress packet is small enough, allocate an skb large enough
1501 * for all of the data and copy it inline. Otherwise, allocate an skb
1502 * with enough room to pull in the header and reference the rest of
1503 * the data via the skb fragment list.
1504 *
1505 * Below we rely on RX_COPY_THRES being less than the smallest Rx
1506 * buff! size, which is expected since buffers are at least
1507 * PAGE_SIZEd. In this case packets up to RX_COPY_THRES have only one
1508 * fragment.
1509 */
1510 if (gl->tot_len <= RX_COPY_THRES) {
1511 /* small packets have only one fragment */
1512 skb = alloc_skb(gl->tot_len, GFP_ATOMIC);
1513 if (unlikely(!skb))
1514 goto out;
1515 __skb_put(skb, gl->tot_len);
1516 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1517 } else {
1518 skb = alloc_skb(skb_len, GFP_ATOMIC);
1519 if (unlikely(!skb))
1520 goto out;
1521 __skb_put(skb, pull_len);
1522 skb_copy_to_linear_data(skb, gl->va, pull_len);
1523
Ian Campbella0006a82011-10-19 23:01:47 +00001524 copy_frags(skb, gl, pull_len);
Casey Leedomeb6c5032010-11-11 09:06:50 +00001525 skb->len = gl->tot_len;
1526 skb->data_len = skb->len - pull_len;
1527 skb->truesize += skb->data_len;
Casey Leedomeb6c5032010-11-11 09:06:50 +00001528 }
1529
1530out:
1531 return skb;
1532}
1533
1534/**
Casey Leedomc6e0d912010-06-25 12:13:28 +00001535 * t4vf_pktgl_free - free a packet gather list
1536 * @gl: the gather list
1537 *
1538 * Releases the pages of a packet gather list. We do not own the last
1539 * page on the list and do not free it.
1540 */
Sachin Kamat8a67d1c2013-09-18 09:00:01 +05301541static void t4vf_pktgl_free(const struct pkt_gl *gl)
Casey Leedomc6e0d912010-06-25 12:13:28 +00001542{
1543 int frag;
1544
1545 frag = gl->nfrags - 1;
1546 while (frag--)
1547 put_page(gl->frags[frag].page);
1548}
1549
1550/**
Casey Leedomc6e0d912010-06-25 12:13:28 +00001551 * do_gro - perform Generic Receive Offload ingress packet processing
1552 * @rxq: ingress RX Ethernet Queue
1553 * @gl: gather list for ingress packet
1554 * @pkt: CPL header for last packet fragment
1555 *
1556 * Perform Generic Receive Offload (GRO) ingress packet processing.
1557 * We use the standard Linux GRO interfaces for this.
1558 */
1559static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1560 const struct cpl_rx_pkt *pkt)
1561{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301562 struct adapter *adapter = rxq->rspq.adapter;
1563 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001564 int ret;
1565 struct sk_buff *skb;
1566
1567 skb = napi_get_frags(&rxq->rspq.napi);
1568 if (unlikely(!skb)) {
1569 t4vf_pktgl_free(gl);
1570 rxq->stats.rx_drops++;
1571 return;
1572 }
1573
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301574 copy_frags(skb, gl, s->pktshift);
1575 skb->len = gl->tot_len - s->pktshift;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001576 skb->data_len = skb->len;
1577 skb->truesize += skb->data_len;
1578 skb->ip_summed = CHECKSUM_UNNECESSARY;
1579 skb_record_rx_queue(skb, rxq->rspq.idx);
1580
Vipul Pandyaaf32de02013-02-12 00:36:21 +00001581 if (pkt->vlan_ex) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001582 __vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q),
1583 be16_to_cpu(pkt->vlan));
Vipul Pandyaaf32de02013-02-12 00:36:21 +00001584 rxq->stats.vlan_ex++;
1585 }
Casey Leedomc6e0d912010-06-25 12:13:28 +00001586 ret = napi_gro_frags(&rxq->rspq.napi);
1587
Casey Leedomc6e0d912010-06-25 12:13:28 +00001588 if (ret == GRO_HELD)
1589 rxq->stats.lro_pkts++;
1590 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1591 rxq->stats.lro_merged++;
1592 rxq->stats.pkts++;
1593 rxq->stats.rx_cso++;
1594}
1595
1596/**
1597 * t4vf_ethrx_handler - process an ingress ethernet packet
1598 * @rspq: the response queue that received the packet
1599 * @rsp: the response queue descriptor holding the RX_PKT message
1600 * @gl: the gather list of packet fragments
1601 *
1602 * Process an ingress ethernet packet and deliver it to the stack.
1603 */
1604int t4vf_ethrx_handler(struct sge_rspq *rspq, const __be64 *rsp,
1605 const struct pkt_gl *gl)
1606{
1607 struct sk_buff *skb;
Vipul Pandya8b9a4d52013-02-08 02:49:51 +00001608 const struct cpl_rx_pkt *pkt = (void *)rsp;
Hariprasad Shenaic3136f52014-05-07 18:01:04 +05301609 bool csum_ok = pkt->csum_calc && !pkt->err_vec &&
1610 (rspq->netdev->features & NETIF_F_RXCSUM);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001611 struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301612 struct adapter *adapter = rspq->adapter;
1613 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001614
1615 /*
1616 * If this is a good TCP packet and we have Generic Receive Offload
1617 * enabled, handle the packet in the GRO path.
1618 */
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001619 if ((pkt->l2info & cpu_to_be32(RXF_TCP_F)) &&
Casey Leedomc6e0d912010-06-25 12:13:28 +00001620 (rspq->netdev->features & NETIF_F_GRO) && csum_ok &&
1621 !pkt->ip_frag) {
1622 do_gro(rxq, gl, pkt);
1623 return 0;
1624 }
1625
1626 /*
Casey Leedomeb6c5032010-11-11 09:06:50 +00001627 * Convert the Packet Gather List into an skb.
Casey Leedomc6e0d912010-06-25 12:13:28 +00001628 */
Casey Leedomeb6c5032010-11-11 09:06:50 +00001629 skb = t4vf_pktgl_to_skb(gl, RX_SKB_LEN, RX_PULL_LEN);
1630 if (unlikely(!skb)) {
1631 t4vf_pktgl_free(gl);
1632 rxq->stats.rx_drops++;
1633 return 0;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001634 }
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301635 __skb_pull(skb, s->pktshift);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001636 skb->protocol = eth_type_trans(skb, rspq->netdev);
1637 skb_record_rx_queue(skb, rspq->idx);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001638 rxq->stats.pkts++;
1639
Hariprasad Shenaic3136f52014-05-07 18:01:04 +05301640 if (csum_ok && !pkt->err_vec &&
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001641 (be32_to_cpu(pkt->l2info) & (RXF_UDP_F | RXF_TCP_F))) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00001642 if (!pkt->ip_frag)
1643 skb->ip_summed = CHECKSUM_UNNECESSARY;
1644 else {
1645 __sum16 c = (__force __sum16)pkt->csum;
1646 skb->csum = csum_unfold(c);
1647 skb->ip_summed = CHECKSUM_COMPLETE;
1648 }
1649 rxq->stats.rx_cso++;
1650 } else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001651 skb_checksum_none_assert(skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001652
Jiri Pirko87737662011-07-20 04:54:16 +00001653 if (pkt->vlan_ex) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00001654 rxq->stats.vlan_ex++;
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001655 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), be16_to_cpu(pkt->vlan));
Jiri Pirko87737662011-07-20 04:54:16 +00001656 }
1657
1658 netif_receive_skb(skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001659
1660 return 0;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001661}
1662
1663/**
1664 * is_new_response - check if a response is newly written
1665 * @rc: the response control descriptor
1666 * @rspq: the response queue
1667 *
1668 * Returns true if a response descriptor contains a yet unprocessed
1669 * response.
1670 */
1671static inline bool is_new_response(const struct rsp_ctrl *rc,
1672 const struct sge_rspq *rspq)
1673{
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301674 return ((rc->type_gen >> RSPD_GEN_S) & 0x1) == rspq->gen;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001675}
1676
1677/**
1678 * restore_rx_bufs - put back a packet's RX buffers
1679 * @gl: the packet gather list
1680 * @fl: the SGE Free List
1681 * @nfrags: how many fragments in @si
1682 *
1683 * Called when we find out that the current packet, @si, can't be
1684 * processed right away for some reason. This is a very rare event and
1685 * there's no effort to make this suspension/resumption process
1686 * particularly efficient.
1687 *
1688 * We implement the suspension by putting all of the RX buffers associated
1689 * with the current packet back on the original Free List. The buffers
1690 * have already been unmapped and are left unmapped, we mark them as
1691 * unmapped in order to prevent further unmapping attempts. (Effectively
1692 * this function undoes the series of @unmap_rx_buf calls which were done
1693 * to create the current packet's gather list.) This leaves us ready to
1694 * restart processing of the packet the next time we start processing the
1695 * RX Queue ...
1696 */
1697static void restore_rx_bufs(const struct pkt_gl *gl, struct sge_fl *fl,
1698 int frags)
1699{
1700 struct rx_sw_desc *sdesc;
1701
1702 while (frags--) {
1703 if (fl->cidx == 0)
1704 fl->cidx = fl->size - 1;
1705 else
1706 fl->cidx--;
1707 sdesc = &fl->sdesc[fl->cidx];
1708 sdesc->page = gl->frags[frags].page;
1709 sdesc->dma_addr |= RX_UNMAPPED_BUF;
1710 fl->avail++;
1711 }
1712}
1713
1714/**
1715 * rspq_next - advance to the next entry in a response queue
1716 * @rspq: the queue
1717 *
1718 * Updates the state of a response queue to advance it to the next entry.
1719 */
1720static inline void rspq_next(struct sge_rspq *rspq)
1721{
1722 rspq->cur_desc = (void *)rspq->cur_desc + rspq->iqe_len;
1723 if (unlikely(++rspq->cidx == rspq->size)) {
1724 rspq->cidx = 0;
1725 rspq->gen ^= 1;
1726 rspq->cur_desc = rspq->desc;
1727 }
1728}
1729
1730/**
1731 * process_responses - process responses from an SGE response queue
1732 * @rspq: the ingress response queue to process
1733 * @budget: how many responses can be processed in this round
1734 *
1735 * Process responses from a Scatter Gather Engine response queue up to
1736 * the supplied budget. Responses include received packets as well as
1737 * control messages from firmware or hardware.
1738 *
1739 * Additionally choose the interrupt holdoff time for the next interrupt
1740 * on this queue. If the system is under memory shortage use a fairly
1741 * long delay to help recovery.
1742 */
Sachin Kamat8a67d1c2013-09-18 09:00:01 +05301743static int process_responses(struct sge_rspq *rspq, int budget)
Casey Leedomc6e0d912010-06-25 12:13:28 +00001744{
1745 struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301746 struct adapter *adapter = rspq->adapter;
1747 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001748 int budget_left = budget;
1749
1750 while (likely(budget_left)) {
1751 int ret, rsp_type;
1752 const struct rsp_ctrl *rc;
1753
1754 rc = (void *)rspq->cur_desc + (rspq->iqe_len - sizeof(*rc));
1755 if (!is_new_response(rc, rspq))
1756 break;
1757
1758 /*
1759 * Figure out what kind of response we've received from the
1760 * SGE.
1761 */
Alexander Duyck019be1c2015-04-08 18:49:29 -07001762 dma_rmb();
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301763 rsp_type = RSPD_TYPE_G(rc->type_gen);
1764 if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
Ian Campbella0006a82011-10-19 23:01:47 +00001765 struct page_frag *fp;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001766 struct pkt_gl gl;
1767 const struct rx_sw_desc *sdesc;
1768 u32 bufsz, frag;
1769 u32 len = be32_to_cpu(rc->pldbuflen_qid);
1770
1771 /*
1772 * If we get a "new buffer" message from the SGE we
1773 * need to move on to the next Free List buffer.
1774 */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301775 if (len & RSPD_NEWBUF_F) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00001776 /*
1777 * We get one "new buffer" message when we
1778 * first start up a queue so we need to ignore
1779 * it when our offset into the buffer is 0.
1780 */
1781 if (likely(rspq->offset > 0)) {
1782 free_rx_bufs(rspq->adapter, &rxq->fl,
1783 1);
1784 rspq->offset = 0;
1785 }
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301786 len = RSPD_LEN_G(len);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001787 }
Casey Leedomb94e72e2010-11-11 09:06:49 +00001788 gl.tot_len = len;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001789
1790 /*
1791 * Gather packet fragments.
1792 */
1793 for (frag = 0, fp = gl.frags; /**/; frag++, fp++) {
1794 BUG_ON(frag >= MAX_SKB_FRAGS);
1795 BUG_ON(rxq->fl.avail == 0);
1796 sdesc = &rxq->fl.sdesc[rxq->fl.cidx];
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301797 bufsz = get_buf_size(adapter, sdesc);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001798 fp->page = sdesc->page;
Ian Campbella0006a82011-10-19 23:01:47 +00001799 fp->offset = rspq->offset;
1800 fp->size = min(bufsz, len);
1801 len -= fp->size;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001802 if (!len)
1803 break;
1804 unmap_rx_buf(rspq->adapter, &rxq->fl);
1805 }
1806 gl.nfrags = frag+1;
1807
1808 /*
1809 * Last buffer remains mapped so explicitly make it
1810 * coherent for CPU access and start preloading first
1811 * cache line ...
1812 */
1813 dma_sync_single_for_cpu(rspq->adapter->pdev_dev,
1814 get_buf_addr(sdesc),
Ian Campbella0006a82011-10-19 23:01:47 +00001815 fp->size, DMA_FROM_DEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001816 gl.va = (page_address(gl.frags[0].page) +
Ian Campbella0006a82011-10-19 23:01:47 +00001817 gl.frags[0].offset);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001818 prefetch(gl.va);
1819
1820 /*
1821 * Hand the new ingress packet to the handler for
1822 * this Response Queue.
1823 */
1824 ret = rspq->handler(rspq, rspq->cur_desc, &gl);
1825 if (likely(ret == 0))
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301826 rspq->offset += ALIGN(fp->size, s->fl_align);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001827 else
1828 restore_rx_bufs(&gl, &rxq->fl, frag);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301829 } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00001830 ret = rspq->handler(rspq, rspq->cur_desc, NULL);
1831 } else {
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301832 WARN_ON(rsp_type > RSPD_TYPE_CPL_X);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001833 ret = 0;
1834 }
1835
1836 if (unlikely(ret)) {
1837 /*
1838 * Couldn't process descriptor, back off for recovery.
1839 * We use the SGE's last timer which has the longest
1840 * interrupt coalescing value ...
1841 */
1842 const int NOMEM_TIMER_IDX = SGE_NTIMERS-1;
1843 rspq->next_intr_params =
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301844 QINTR_TIMER_IDX_V(NOMEM_TIMER_IDX);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001845 break;
1846 }
1847
1848 rspq_next(rspq);
1849 budget_left--;
1850 }
1851
1852 /*
1853 * If this is a Response Queue with an associated Free List and
1854 * at least two Egress Queue units available in the Free List
1855 * for new buffer pointers, refill the Free List.
1856 */
1857 if (rspq->offset >= 0 &&
1858 rxq->fl.size - rxq->fl.avail >= 2*FL_PER_EQ_UNIT)
1859 __refill_fl(rspq->adapter, &rxq->fl);
1860 return budget - budget_left;
1861}
1862
1863/**
1864 * napi_rx_handler - the NAPI handler for RX processing
1865 * @napi: the napi instance
1866 * @budget: how many packets we can process in this round
1867 *
1868 * Handler for new data events when using NAPI. This does not need any
1869 * locking or protection from interrupts as data interrupts are off at
1870 * this point and other adapter interrupts do not interfere (the latter
1871 * in not a concern at all with MSI-X as non-data interrupts then have
1872 * a separate handler).
1873 */
1874static int napi_rx_handler(struct napi_struct *napi, int budget)
1875{
1876 unsigned int intr_params;
1877 struct sge_rspq *rspq = container_of(napi, struct sge_rspq, napi);
1878 int work_done = process_responses(rspq, budget);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301879 u32 val;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001880
1881 if (likely(work_done < budget)) {
1882 napi_complete(napi);
1883 intr_params = rspq->next_intr_params;
1884 rspq->next_intr_params = rspq->intr_params;
1885 } else
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301886 intr_params = QINTR_TIMER_IDX_V(SGE_TIMER_UPD_CIDX);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001887
Casey Leedom68dc9d32010-07-08 10:05:48 -07001888 if (unlikely(work_done == 0))
1889 rspq->unhandled_irqs++;
1890
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301891 val = CIDXINC_V(work_done) | SEINTARM_V(intr_params);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301892 if (is_t4(rspq->adapter->params.chip)) {
1893 t4_write_reg(rspq->adapter,
1894 T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301895 val | INGRESSQID_V((u32)rspq->cntxt_id));
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301896 } else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301897 writel(val | INGRESSQID_V(rspq->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301898 rspq->bar2_addr + SGE_UDB_GTS);
1899 wmb();
1900 }
Casey Leedomc6e0d912010-06-25 12:13:28 +00001901 return work_done;
1902}
1903
1904/*
1905 * The MSI-X interrupt handler for an SGE response queue for the NAPI case
1906 * (i.e., response queue serviced by NAPI polling).
1907 */
1908irqreturn_t t4vf_sge_intr_msix(int irq, void *cookie)
1909{
1910 struct sge_rspq *rspq = cookie;
1911
1912 napi_schedule(&rspq->napi);
1913 return IRQ_HANDLED;
1914}
1915
1916/*
1917 * Process the indirect interrupt entries in the interrupt queue and kick off
1918 * NAPI for each queue that has generated an entry.
1919 */
1920static unsigned int process_intrq(struct adapter *adapter)
1921{
1922 struct sge *s = &adapter->sge;
1923 struct sge_rspq *intrq = &s->intrq;
1924 unsigned int work_done;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301925 u32 val;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001926
1927 spin_lock(&adapter->sge.intrq_lock);
1928 for (work_done = 0; ; work_done++) {
1929 const struct rsp_ctrl *rc;
1930 unsigned int qid, iq_idx;
1931 struct sge_rspq *rspq;
1932
1933 /*
1934 * Grab the next response from the interrupt queue and bail
1935 * out if it's not a new response.
1936 */
1937 rc = (void *)intrq->cur_desc + (intrq->iqe_len - sizeof(*rc));
1938 if (!is_new_response(rc, intrq))
1939 break;
1940
1941 /*
1942 * If the response isn't a forwarded interrupt message issue a
1943 * error and go on to the next response message. This should
1944 * never happen ...
1945 */
Alexander Duyck019be1c2015-04-08 18:49:29 -07001946 dma_rmb();
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301947 if (unlikely(RSPD_TYPE_G(rc->type_gen) != RSPD_TYPE_INTR_X)) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00001948 dev_err(adapter->pdev_dev,
1949 "Unexpected INTRQ response type %d\n",
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301950 RSPD_TYPE_G(rc->type_gen));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001951 continue;
1952 }
1953
1954 /*
1955 * Extract the Queue ID from the interrupt message and perform
1956 * sanity checking to make sure it really refers to one of our
1957 * Ingress Queues which is active and matches the queue's ID.
1958 * None of these error conditions should ever happen so we may
1959 * want to either make them fatal and/or conditionalized under
1960 * DEBUG.
1961 */
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301962 qid = RSPD_QID_G(be32_to_cpu(rc->pldbuflen_qid));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001963 iq_idx = IQ_IDX(s, qid);
1964 if (unlikely(iq_idx >= MAX_INGQ)) {
1965 dev_err(adapter->pdev_dev,
1966 "Ingress QID %d out of range\n", qid);
1967 continue;
1968 }
1969 rspq = s->ingr_map[iq_idx];
1970 if (unlikely(rspq == NULL)) {
1971 dev_err(adapter->pdev_dev,
1972 "Ingress QID %d RSPQ=NULL\n", qid);
1973 continue;
1974 }
1975 if (unlikely(rspq->abs_id != qid)) {
1976 dev_err(adapter->pdev_dev,
1977 "Ingress QID %d refers to RSPQ %d\n",
1978 qid, rspq->abs_id);
1979 continue;
1980 }
1981
1982 /*
1983 * Schedule NAPI processing on the indicated Response Queue
1984 * and move on to the next entry in the Forwarded Interrupt
1985 * Queue.
1986 */
1987 napi_schedule(&rspq->napi);
1988 rspq_next(intrq);
1989 }
1990
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301991 val = CIDXINC_V(work_done) | SEINTARM_V(intrq->intr_params);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301992 if (is_t4(adapter->params.chip))
1993 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301994 val | INGRESSQID_V(intrq->cntxt_id));
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301995 else {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301996 writel(val | INGRESSQID_V(intrq->bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301997 intrq->bar2_addr + SGE_UDB_GTS);
1998 wmb();
1999 }
Casey Leedomc6e0d912010-06-25 12:13:28 +00002000
2001 spin_unlock(&adapter->sge.intrq_lock);
2002
2003 return work_done;
2004}
2005
2006/*
2007 * The MSI interrupt handler handles data events from SGE response queues as
2008 * well as error and other async events as they all use the same MSI vector.
2009 */
Sachin Kamat8a67d1c2013-09-18 09:00:01 +05302010static irqreturn_t t4vf_intr_msi(int irq, void *cookie)
Casey Leedomc6e0d912010-06-25 12:13:28 +00002011{
2012 struct adapter *adapter = cookie;
2013
2014 process_intrq(adapter);
2015 return IRQ_HANDLED;
2016}
2017
2018/**
2019 * t4vf_intr_handler - select the top-level interrupt handler
2020 * @adapter: the adapter
2021 *
2022 * Selects the top-level interrupt handler based on the type of interrupts
2023 * (MSI-X or MSI).
2024 */
2025irq_handler_t t4vf_intr_handler(struct adapter *adapter)
2026{
2027 BUG_ON((adapter->flags & (USING_MSIX|USING_MSI)) == 0);
2028 if (adapter->flags & USING_MSIX)
2029 return t4vf_sge_intr_msix;
2030 else
2031 return t4vf_intr_msi;
2032}
2033
2034/**
2035 * sge_rx_timer_cb - perform periodic maintenance of SGE RX queues
2036 * @data: the adapter
2037 *
2038 * Runs periodically from a timer to perform maintenance of SGE RX queues.
2039 *
2040 * a) Replenishes RX queues that have run out due to memory shortage.
2041 * Normally new RX buffers are added when existing ones are consumed but
2042 * when out of memory a queue can become empty. We schedule NAPI to do
2043 * the actual refill.
2044 */
2045static void sge_rx_timer_cb(unsigned long data)
2046{
2047 struct adapter *adapter = (struct adapter *)data;
2048 struct sge *s = &adapter->sge;
2049 unsigned int i;
2050
2051 /*
2052 * Scan the "Starving Free Lists" flag array looking for any Free
2053 * Lists in need of more free buffers. If we find one and it's not
2054 * being actively polled, then bump its "starving" counter and attempt
2055 * to refill it. If we're successful in adding enough buffers to push
2056 * the Free List over the starving threshold, then we can clear its
2057 * "starving" status.
2058 */
2059 for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++) {
2060 unsigned long m;
2061
2062 for (m = s->starving_fl[i]; m; m &= m - 1) {
2063 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
2064 struct sge_fl *fl = s->egr_map[id];
2065
2066 clear_bit(id, s->starving_fl);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002067 smp_mb__after_atomic();
Casey Leedomc6e0d912010-06-25 12:13:28 +00002068
2069 /*
2070 * Since we are accessing fl without a lock there's a
2071 * small probability of a false positive where we
2072 * schedule napi but the FL is no longer starving.
2073 * No biggie.
2074 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302075 if (fl_starving(adapter, fl)) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00002076 struct sge_eth_rxq *rxq;
2077
2078 rxq = container_of(fl, struct sge_eth_rxq, fl);
2079 if (napi_reschedule(&rxq->rspq.napi))
2080 fl->starving++;
2081 else
2082 set_bit(id, s->starving_fl);
2083 }
2084 }
2085 }
2086
2087 /*
2088 * Reschedule the next scan for starving Free Lists ...
2089 */
2090 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
2091}
2092
2093/**
2094 * sge_tx_timer_cb - perform periodic maintenance of SGE Tx queues
2095 * @data: the adapter
2096 *
2097 * Runs periodically from a timer to perform maintenance of SGE TX queues.
2098 *
2099 * b) Reclaims completed Tx packets for the Ethernet queues. Normally
2100 * packets are cleaned up by new Tx packets, this timer cleans up packets
2101 * when no new packets are being submitted. This is essential for pktgen,
2102 * at least.
2103 */
2104static void sge_tx_timer_cb(unsigned long data)
2105{
2106 struct adapter *adapter = (struct adapter *)data;
2107 struct sge *s = &adapter->sge;
2108 unsigned int i, budget;
2109
2110 budget = MAX_TIMER_TX_RECLAIM;
2111 i = s->ethtxq_rover;
2112 do {
2113 struct sge_eth_txq *txq = &s->ethtxq[i];
2114
2115 if (reclaimable(&txq->q) && __netif_tx_trylock(txq->txq)) {
2116 int avail = reclaimable(&txq->q);
2117
2118 if (avail > budget)
2119 avail = budget;
2120
2121 free_tx_desc(adapter, &txq->q, avail, true);
2122 txq->q.in_use -= avail;
2123 __netif_tx_unlock(txq->txq);
2124
2125 budget -= avail;
2126 if (!budget)
2127 break;
2128 }
2129
2130 i++;
2131 if (i >= s->ethqsets)
2132 i = 0;
2133 } while (i != s->ethtxq_rover);
2134 s->ethtxq_rover = i;
2135
2136 /*
2137 * If we found too many reclaimable packets schedule a timer in the
2138 * near future to continue where we left off. Otherwise the next timer
2139 * will be at its normal interval.
2140 */
2141 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
2142}
2143
2144/**
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302145 * bar2_address - return the BAR2 address for an SGE Queue's Registers
2146 * @adapter: the adapter
2147 * @qid: the SGE Queue ID
2148 * @qtype: the SGE Queue Type (Egress or Ingress)
2149 * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
2150 *
2151 * Returns the BAR2 address for the SGE Queue Registers associated with
2152 * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
2153 * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
2154 * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
2155 * Registers are supported (e.g. the Write Combining Doorbell Buffer).
2156 */
2157static void __iomem *bar2_address(struct adapter *adapter,
2158 unsigned int qid,
2159 enum t4_bar2_qtype qtype,
2160 unsigned int *pbar2_qid)
2161{
2162 u64 bar2_qoffset;
2163 int ret;
2164
2165 ret = t4_bar2_sge_qregs(adapter, qid, qtype,
2166 &bar2_qoffset, pbar2_qid);
2167 if (ret)
2168 return NULL;
2169
2170 return adapter->bar2 + bar2_qoffset;
2171}
2172
2173/**
Casey Leedomc6e0d912010-06-25 12:13:28 +00002174 * t4vf_sge_alloc_rxq - allocate an SGE RX Queue
2175 * @adapter: the adapter
2176 * @rspq: pointer to to the new rxq's Response Queue to be filled in
2177 * @iqasynch: if 0, a normal rspq; if 1, an asynchronous event queue
2178 * @dev: the network device associated with the new rspq
2179 * @intr_dest: MSI-X vector index (overriden in MSI mode)
2180 * @fl: pointer to the new rxq's Free List to be filled in
2181 * @hnd: the interrupt handler to invoke for the rspq
2182 */
2183int t4vf_sge_alloc_rxq(struct adapter *adapter, struct sge_rspq *rspq,
2184 bool iqasynch, struct net_device *dev,
2185 int intr_dest,
2186 struct sge_fl *fl, rspq_handler_t hnd)
2187{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302188 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00002189 struct port_info *pi = netdev_priv(dev);
2190 struct fw_iq_cmd cmd, rpl;
2191 int ret, iqandst, flsz = 0;
2192
2193 /*
2194 * If we're using MSI interrupts and we're not initializing the
2195 * Forwarded Interrupt Queue itself, then set up this queue for
2196 * indirect interrupts to the Forwarded Interrupt Queue. Obviously
2197 * the Forwarded Interrupt Queue must be set up before any other
2198 * ingress queue ...
2199 */
2200 if ((adapter->flags & USING_MSI) && rspq != &adapter->sge.intrq) {
2201 iqandst = SGE_INTRDST_IQ;
2202 intr_dest = adapter->sge.intrq.abs_id;
2203 } else
2204 iqandst = SGE_INTRDST_PCI;
2205
2206 /*
2207 * Allocate the hardware ring for the Response Queue. The size needs
2208 * to be a multiple of 16 which includes the mandatory status entry
2209 * (regardless of whether the Status Page capabilities are enabled or
2210 * not).
2211 */
2212 rspq->size = roundup(rspq->size, 16);
2213 rspq->desc = alloc_ring(adapter->pdev_dev, rspq->size, rspq->iqe_len,
2214 0, &rspq->phys_addr, NULL, 0);
2215 if (!rspq->desc)
2216 return -ENOMEM;
2217
2218 /*
2219 * Fill in the Ingress Queue Command. Note: Ideally this code would
2220 * be in t4vf_hw.c but there are so many parameters and dependencies
2221 * on our Linux SGE state that we would end up having to pass tons of
2222 * parameters. We'll have to think about how this might be migrated
2223 * into OS-independent common code ...
2224 */
2225 memset(&cmd, 0, sizeof(cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302226 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_IQ_CMD) |
2227 FW_CMD_REQUEST_F |
2228 FW_CMD_WRITE_F |
2229 FW_CMD_EXEC_F);
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302230 cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_ALLOC_F |
2231 FW_IQ_CMD_IQSTART_F |
Casey Leedomc6e0d912010-06-25 12:13:28 +00002232 FW_LEN16(cmd));
2233 cmd.type_to_iqandstindex =
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302234 cpu_to_be32(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
2235 FW_IQ_CMD_IQASYNCH_V(iqasynch) |
2236 FW_IQ_CMD_VIID_V(pi->viid) |
2237 FW_IQ_CMD_IQANDST_V(iqandst) |
2238 FW_IQ_CMD_IQANUS_V(1) |
2239 FW_IQ_CMD_IQANUD_V(SGE_UPDATEDEL_INTR) |
2240 FW_IQ_CMD_IQANDSTINDEX_V(intr_dest));
Casey Leedomc6e0d912010-06-25 12:13:28 +00002241 cmd.iqdroprss_to_iqesize =
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302242 cpu_to_be16(FW_IQ_CMD_IQPCIECH_V(pi->port_id) |
2243 FW_IQ_CMD_IQGTSMODE_F |
2244 FW_IQ_CMD_IQINTCNTTHRESH_V(rspq->pktcnt_idx) |
2245 FW_IQ_CMD_IQESIZE_V(ilog2(rspq->iqe_len) - 4));
Casey Leedomc6e0d912010-06-25 12:13:28 +00002246 cmd.iqsize = cpu_to_be16(rspq->size);
2247 cmd.iqaddr = cpu_to_be64(rspq->phys_addr);
2248
2249 if (fl) {
2250 /*
2251 * Allocate the ring for the hardware free list (with space
2252 * for its status page) along with the associated software
2253 * descriptor ring. The free list size needs to be a multiple
Hariprasad Shenai13432992015-05-05 14:59:51 +05302254 * of the Egress Queue Unit and at least 2 Egress Units larger
2255 * than the SGE's Egress Congrestion Threshold
2256 * (fl_starve_thres - 1).
Casey Leedomc6e0d912010-06-25 12:13:28 +00002257 */
Hariprasad Shenai13432992015-05-05 14:59:51 +05302258 if (fl->size < s->fl_starve_thres - 1 + 2 * FL_PER_EQ_UNIT)
2259 fl->size = s->fl_starve_thres - 1 + 2 * FL_PER_EQ_UNIT;
Casey Leedomc6e0d912010-06-25 12:13:28 +00002260 fl->size = roundup(fl->size, FL_PER_EQ_UNIT);
2261 fl->desc = alloc_ring(adapter->pdev_dev, fl->size,
2262 sizeof(__be64), sizeof(struct rx_sw_desc),
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302263 &fl->addr, &fl->sdesc, s->stat_len);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002264 if (!fl->desc) {
2265 ret = -ENOMEM;
2266 goto err;
2267 }
2268
2269 /*
2270 * Calculate the size of the hardware free list ring plus
Casey Leedomcaedda32010-11-11 09:30:40 +00002271 * Status Page (which the SGE will place after the end of the
Casey Leedomc6e0d912010-06-25 12:13:28 +00002272 * free list ring) in Egress Queue Units.
2273 */
2274 flsz = (fl->size / FL_PER_EQ_UNIT +
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302275 s->stat_len / EQ_UNIT);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002276
2277 /*
2278 * Fill in all the relevant firmware Ingress Queue Command
2279 * fields for the free list.
2280 */
2281 cmd.iqns_to_fl0congen =
2282 cpu_to_be32(
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302283 FW_IQ_CMD_FL0HOSTFCMODE_V(SGE_HOSTFCMODE_NONE) |
2284 FW_IQ_CMD_FL0PACKEN_F |
2285 FW_IQ_CMD_FL0PADEN_F);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002286 cmd.fl0dcaen_to_fl0cidxfthresh =
2287 cpu_to_be16(
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302288 FW_IQ_CMD_FL0FBMIN_V(SGE_FETCHBURSTMIN_64B) |
2289 FW_IQ_CMD_FL0FBMAX_V(SGE_FETCHBURSTMAX_512B));
Casey Leedomc6e0d912010-06-25 12:13:28 +00002290 cmd.fl0size = cpu_to_be16(flsz);
2291 cmd.fl0addr = cpu_to_be64(fl->addr);
2292 }
2293
2294 /*
2295 * Issue the firmware Ingress Queue Command and extract the results if
2296 * it completes successfully.
2297 */
2298 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
2299 if (ret)
2300 goto err;
2301
2302 netif_napi_add(dev, &rspq->napi, napi_rx_handler, 64);
2303 rspq->cur_desc = rspq->desc;
2304 rspq->cidx = 0;
2305 rspq->gen = 1;
2306 rspq->next_intr_params = rspq->intr_params;
2307 rspq->cntxt_id = be16_to_cpu(rpl.iqid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302308 rspq->bar2_addr = bar2_address(adapter,
2309 rspq->cntxt_id,
2310 T4_BAR2_QTYPE_INGRESS,
2311 &rspq->bar2_qid);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002312 rspq->abs_id = be16_to_cpu(rpl.physiqid);
2313 rspq->size--; /* subtract status entry */
2314 rspq->adapter = adapter;
2315 rspq->netdev = dev;
2316 rspq->handler = hnd;
2317
2318 /* set offset to -1 to distinguish ingress queues without FL */
2319 rspq->offset = fl ? 0 : -1;
2320
2321 if (fl) {
2322 fl->cntxt_id = be16_to_cpu(rpl.fl0id);
2323 fl->avail = 0;
2324 fl->pend_cred = 0;
2325 fl->pidx = 0;
2326 fl->cidx = 0;
2327 fl->alloc_failed = 0;
2328 fl->large_alloc_failed = 0;
2329 fl->starving = 0;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302330
2331 /* Note, we must initialize the BAR2 Free List User Doorbell
2332 * information before refilling the Free List!
2333 */
2334 fl->bar2_addr = bar2_address(adapter,
2335 fl->cntxt_id,
2336 T4_BAR2_QTYPE_EGRESS,
2337 &fl->bar2_qid);
2338
Casey Leedomc6e0d912010-06-25 12:13:28 +00002339 refill_fl(adapter, fl, fl_cap(fl), GFP_KERNEL);
2340 }
2341
2342 return 0;
2343
2344err:
2345 /*
2346 * An error occurred. Clean up our partial allocation state and
2347 * return the error.
2348 */
2349 if (rspq->desc) {
2350 dma_free_coherent(adapter->pdev_dev, rspq->size * rspq->iqe_len,
2351 rspq->desc, rspq->phys_addr);
2352 rspq->desc = NULL;
2353 }
2354 if (fl && fl->desc) {
2355 kfree(fl->sdesc);
2356 fl->sdesc = NULL;
2357 dma_free_coherent(adapter->pdev_dev, flsz * EQ_UNIT,
2358 fl->desc, fl->addr);
2359 fl->desc = NULL;
2360 }
2361 return ret;
2362}
2363
2364/**
2365 * t4vf_sge_alloc_eth_txq - allocate an SGE Ethernet TX Queue
2366 * @adapter: the adapter
2367 * @txq: pointer to the new txq to be filled in
2368 * @devq: the network TX queue associated with the new txq
2369 * @iqid: the relative ingress queue ID to which events relating to
2370 * the new txq should be directed
2371 */
2372int t4vf_sge_alloc_eth_txq(struct adapter *adapter, struct sge_eth_txq *txq,
2373 struct net_device *dev, struct netdev_queue *devq,
2374 unsigned int iqid)
2375{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302376 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00002377 int ret, nentries;
2378 struct fw_eq_eth_cmd cmd, rpl;
2379 struct port_info *pi = netdev_priv(dev);
2380
2381 /*
Casey Leedomcaedda32010-11-11 09:30:40 +00002382 * Calculate the size of the hardware TX Queue (including the Status
2383 * Page on the end of the TX Queue) in units of TX Descriptors.
Casey Leedomc6e0d912010-06-25 12:13:28 +00002384 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302385 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002386
2387 /*
2388 * Allocate the hardware ring for the TX ring (with space for its
2389 * status page) along with the associated software descriptor ring.
2390 */
2391 txq->q.desc = alloc_ring(adapter->pdev_dev, txq->q.size,
2392 sizeof(struct tx_desc),
2393 sizeof(struct tx_sw_desc),
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302394 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002395 if (!txq->q.desc)
2396 return -ENOMEM;
2397
2398 /*
2399 * Fill in the Egress Queue Command. Note: As with the direct use of
2400 * the firmware Ingress Queue COmmand above in our RXQ allocation
2401 * routine, ideally, this code would be in t4vf_hw.c. Again, we'll
2402 * have to see if there's some reasonable way to parameterize it
2403 * into the common code ...
2404 */
2405 memset(&cmd, 0, sizeof(cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302406 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_EQ_ETH_CMD) |
2407 FW_CMD_REQUEST_F |
2408 FW_CMD_WRITE_F |
2409 FW_CMD_EXEC_F);
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302410 cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_ALLOC_F |
2411 FW_EQ_ETH_CMD_EQSTART_F |
Casey Leedomc6e0d912010-06-25 12:13:28 +00002412 FW_LEN16(cmd));
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302413 cmd.viid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
2414 FW_EQ_ETH_CMD_VIID_V(pi->viid));
Casey Leedomc6e0d912010-06-25 12:13:28 +00002415 cmd.fetchszm_to_iqid =
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302416 cpu_to_be32(FW_EQ_ETH_CMD_HOSTFCMODE_V(SGE_HOSTFCMODE_STPG) |
2417 FW_EQ_ETH_CMD_PCIECHN_V(pi->port_id) |
2418 FW_EQ_ETH_CMD_IQID_V(iqid));
Casey Leedomc6e0d912010-06-25 12:13:28 +00002419 cmd.dcaen_to_eqsize =
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302420 cpu_to_be32(FW_EQ_ETH_CMD_FBMIN_V(SGE_FETCHBURSTMIN_64B) |
2421 FW_EQ_ETH_CMD_FBMAX_V(SGE_FETCHBURSTMAX_512B) |
2422 FW_EQ_ETH_CMD_CIDXFTHRESH_V(
2423 SGE_CIDXFLUSHTHRESH_32) |
2424 FW_EQ_ETH_CMD_EQSIZE_V(nentries));
Casey Leedomc6e0d912010-06-25 12:13:28 +00002425 cmd.eqaddr = cpu_to_be64(txq->q.phys_addr);
2426
2427 /*
2428 * Issue the firmware Egress Queue Command and extract the results if
2429 * it completes successfully.
2430 */
2431 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
2432 if (ret) {
2433 /*
2434 * The girmware Ingress Queue Command failed for some reason.
2435 * Free up our partial allocation state and return the error.
2436 */
2437 kfree(txq->q.sdesc);
2438 txq->q.sdesc = NULL;
2439 dma_free_coherent(adapter->pdev_dev,
2440 nentries * sizeof(struct tx_desc),
2441 txq->q.desc, txq->q.phys_addr);
2442 txq->q.desc = NULL;
2443 return ret;
2444 }
2445
2446 txq->q.in_use = 0;
2447 txq->q.cidx = 0;
2448 txq->q.pidx = 0;
2449 txq->q.stat = (void *)&txq->q.desc[txq->q.size];
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302450 txq->q.cntxt_id = FW_EQ_ETH_CMD_EQID_G(be32_to_cpu(rpl.eqid_pkd));
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302451 txq->q.bar2_addr = bar2_address(adapter,
2452 txq->q.cntxt_id,
2453 T4_BAR2_QTYPE_EGRESS,
2454 &txq->q.bar2_qid);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002455 txq->q.abs_id =
Hariprasad Shenai6e4b51a2014-11-21 12:52:03 +05302456 FW_EQ_ETH_CMD_PHYSEQID_G(be32_to_cpu(rpl.physeqid_pkd));
Casey Leedomc6e0d912010-06-25 12:13:28 +00002457 txq->txq = devq;
2458 txq->tso = 0;
2459 txq->tx_cso = 0;
2460 txq->vlan_ins = 0;
2461 txq->q.stops = 0;
2462 txq->q.restarts = 0;
2463 txq->mapping_err = 0;
2464 return 0;
2465}
2466
2467/*
2468 * Free the DMA map resources associated with a TX queue.
2469 */
2470static void free_txq(struct adapter *adapter, struct sge_txq *tq)
2471{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302472 struct sge *s = &adapter->sge;
2473
Casey Leedomc6e0d912010-06-25 12:13:28 +00002474 dma_free_coherent(adapter->pdev_dev,
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302475 tq->size * sizeof(*tq->desc) + s->stat_len,
Casey Leedomc6e0d912010-06-25 12:13:28 +00002476 tq->desc, tq->phys_addr);
2477 tq->cntxt_id = 0;
2478 tq->sdesc = NULL;
2479 tq->desc = NULL;
2480}
2481
2482/*
2483 * Free the resources associated with a response queue (possibly including a
2484 * free list).
2485 */
2486static void free_rspq_fl(struct adapter *adapter, struct sge_rspq *rspq,
2487 struct sge_fl *fl)
2488{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302489 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00002490 unsigned int flid = fl ? fl->cntxt_id : 0xffff;
2491
2492 t4vf_iq_free(adapter, FW_IQ_TYPE_FL_INT_CAP,
2493 rspq->cntxt_id, flid, 0xffff);
2494 dma_free_coherent(adapter->pdev_dev, (rspq->size + 1) * rspq->iqe_len,
2495 rspq->desc, rspq->phys_addr);
2496 netif_napi_del(&rspq->napi);
2497 rspq->netdev = NULL;
2498 rspq->cntxt_id = 0;
2499 rspq->abs_id = 0;
2500 rspq->desc = NULL;
2501
2502 if (fl) {
2503 free_rx_bufs(adapter, fl, fl->avail);
2504 dma_free_coherent(adapter->pdev_dev,
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302505 fl->size * sizeof(*fl->desc) + s->stat_len,
Casey Leedomc6e0d912010-06-25 12:13:28 +00002506 fl->desc, fl->addr);
2507 kfree(fl->sdesc);
2508 fl->sdesc = NULL;
2509 fl->cntxt_id = 0;
2510 fl->desc = NULL;
2511 }
2512}
2513
2514/**
2515 * t4vf_free_sge_resources - free SGE resources
2516 * @adapter: the adapter
2517 *
2518 * Frees resources used by the SGE queue sets.
2519 */
2520void t4vf_free_sge_resources(struct adapter *adapter)
2521{
2522 struct sge *s = &adapter->sge;
2523 struct sge_eth_rxq *rxq = s->ethrxq;
2524 struct sge_eth_txq *txq = s->ethtxq;
2525 struct sge_rspq *evtq = &s->fw_evtq;
2526 struct sge_rspq *intrq = &s->intrq;
2527 int qs;
2528
Casey Leedomb97d13a2010-07-15 22:47:06 -07002529 for (qs = 0; qs < adapter->sge.ethqsets; qs++, rxq++, txq++) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00002530 if (rxq->rspq.desc)
2531 free_rspq_fl(adapter, &rxq->rspq, &rxq->fl);
2532 if (txq->q.desc) {
2533 t4vf_eth_eq_free(adapter, txq->q.cntxt_id);
2534 free_tx_desc(adapter, &txq->q, txq->q.in_use, true);
2535 kfree(txq->q.sdesc);
2536 free_txq(adapter, &txq->q);
2537 }
2538 }
2539 if (evtq->desc)
2540 free_rspq_fl(adapter, evtq, NULL);
2541 if (intrq->desc)
2542 free_rspq_fl(adapter, intrq, NULL);
2543}
2544
2545/**
2546 * t4vf_sge_start - enable SGE operation
2547 * @adapter: the adapter
2548 *
2549 * Start tasklets and timers associated with the DMA engine.
2550 */
2551void t4vf_sge_start(struct adapter *adapter)
2552{
2553 adapter->sge.ethtxq_rover = 0;
2554 mod_timer(&adapter->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2555 mod_timer(&adapter->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2556}
2557
2558/**
2559 * t4vf_sge_stop - disable SGE operation
2560 * @adapter: the adapter
2561 *
2562 * Stop tasklets and timers associated with the DMA engine. Note that
2563 * this is effective only if measures have been taken to disable any HW
2564 * events that may restart them.
2565 */
2566void t4vf_sge_stop(struct adapter *adapter)
2567{
2568 struct sge *s = &adapter->sge;
2569
2570 if (s->rx_timer.function)
2571 del_timer_sync(&s->rx_timer);
2572 if (s->tx_timer.function)
2573 del_timer_sync(&s->tx_timer);
2574}
2575
2576/**
2577 * t4vf_sge_init - initialize SGE
2578 * @adapter: the adapter
2579 *
2580 * Performs SGE initialization needed every time after a chip reset.
2581 * We do not initialize any of the queue sets here, instead the driver
2582 * top-level must request those individually. We also do not enable DMA
2583 * here, that should be done after the queues have been set up.
2584 */
2585int t4vf_sge_init(struct adapter *adapter)
2586{
2587 struct sge_params *sge_params = &adapter->params.sge;
2588 u32 fl0 = sge_params->sge_fl_buffer_size[0];
2589 u32 fl1 = sge_params->sge_fl_buffer_size[1];
2590 struct sge *s = &adapter->sge;
Hariprasad Shenaice8f4072014-11-07 17:06:30 +05302591 unsigned int ingpadboundary, ingpackboundary;
Casey Leedomc6e0d912010-06-25 12:13:28 +00002592
2593 /*
2594 * Start by vetting the basic SGE parameters which have been set up by
2595 * the Physical Function Driver. Ideally we should be able to deal
2596 * with _any_ configuration. Practice is different ...
2597 */
2598 if (fl0 != PAGE_SIZE || (fl1 != 0 && fl1 <= fl0)) {
2599 dev_err(adapter->pdev_dev, "bad SGE FL buffer sizes [%d, %d]\n",
2600 fl0, fl1);
2601 return -EINVAL;
2602 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302603 if ((sge_params->sge_control & RXPKTCPLMODE_F) == 0) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00002604 dev_err(adapter->pdev_dev, "bad SGE CPL MODE\n");
2605 return -EINVAL;
2606 }
2607
2608 /*
2609 * Now translate the adapter parameters into our internal forms.
2610 */
2611 if (fl1)
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302612 s->fl_pg_order = ilog2(fl1) - PAGE_SHIFT;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302613 s->stat_len = ((sge_params->sge_control & EGRSTATUSPAGESIZE_F)
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302614 ? 128 : 64);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302615 s->pktshift = PKTSHIFT_G(sge_params->sge_control);
Hariprasad Shenaice8f4072014-11-07 17:06:30 +05302616
2617 /* T4 uses a single control field to specify both the PCIe Padding and
2618 * Packing Boundary. T5 introduced the ability to specify these
2619 * separately. The actual Ingress Packet Data alignment boundary
2620 * within Packed Buffer Mode is the maximum of these two
2621 * specifications. (Note that it makes no real practical sense to
2622 * have the Pading Boudary be larger than the Packing Boundary but you
2623 * could set the chip up that way and, in fact, legacy T4 code would
2624 * end doing this because it would initialize the Padding Boundary and
2625 * leave the Packing Boundary initialized to 0 (16 bytes).)
2626 */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302627 ingpadboundary = 1 << (INGPADBOUNDARY_G(sge_params->sge_control) +
2628 INGPADBOUNDARY_SHIFT_X);
Hariprasad Shenaice8f4072014-11-07 17:06:30 +05302629 if (is_t4(adapter->params.chip)) {
2630 s->fl_align = ingpadboundary;
2631 } else {
2632 /* T5 has a different interpretation of one of the PCIe Packing
2633 * Boundary values.
2634 */
2635 ingpackboundary = INGPACKBOUNDARY_G(sge_params->sge_control2);
2636 if (ingpackboundary == INGPACKBOUNDARY_16B_X)
2637 ingpackboundary = 16;
2638 else
2639 ingpackboundary = 1 << (ingpackboundary +
2640 INGPACKBOUNDARY_SHIFT_X);
2641
2642 s->fl_align = max(ingpadboundary, ingpackboundary);
2643 }
Casey Leedomc6e0d912010-06-25 12:13:28 +00002644
Hariprasad Shenai50d21a62014-11-07 17:06:31 +05302645 /* A FL with <= fl_starve_thres buffers is starving and a periodic
2646 * timer will attempt to refill it. This needs to be larger than the
2647 * SGE's Egress Congestion Threshold. If it isn't, then we can get
2648 * stuck waiting for new packets while the SGE is waiting for us to
2649 * give it more Free List entries. (Note that the SGE's Egress
2650 * Congestion Threshold is in units of 2 Free List pointers.)
2651 */
2652 s->fl_starve_thres
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302653 = EGRTHRESHOLD_G(sge_params->sge_congestion_control)*2 + 1;
Casey Leedomc6e0d912010-06-25 12:13:28 +00002654
2655 /*
2656 * Set up tasklet timers.
2657 */
2658 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adapter);
2659 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adapter);
2660
2661 /*
2662 * Initialize Forwarded Interrupt Queue lock.
2663 */
2664 spin_lock_init(&s->intrq_lock);
2665
2666 return 0;
2667}