blob: a18830d8aa6d10880b283da8a6022c23e26c3a67 [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"
50#include "../cxgb4/t4fw_api.h"
51#include "../cxgb4/t4_msg.h"
52
53/*
Casey Leedomc6e0d912010-06-25 12:13:28 +000054 * Constants ...
55 */
56enum {
57 /*
58 * Egress Queue sizes, producer and consumer indices are all in units
59 * of Egress Context Units bytes. Note that as far as the hardware is
60 * concerned, the free list is an Egress Queue (the host produces free
61 * buffers which the hardware consumes) and free list entries are
62 * 64-bit PCI DMA addresses.
63 */
64 EQ_UNIT = SGE_EQ_IDXSIZE,
65 FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
66 TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
67
68 /*
69 * Max number of TX descriptors we clean up at a time. Should be
70 * modest as freeing skbs isn't cheap and it happens while holding
71 * locks. We just need to free packets faster than they arrive, we
72 * eventually catch up and keep the amortized cost reasonable.
73 */
74 MAX_TX_RECLAIM = 16,
75
76 /*
77 * Max number of Rx buffers we replenish at a time. Again keep this
78 * modest, allocating buffers isn't cheap either.
79 */
80 MAX_RX_REFILL = 16,
81
82 /*
83 * Period of the Rx queue check timer. This timer is infrequent as it
84 * has something to do only when the system experiences severe memory
85 * shortage.
86 */
87 RX_QCHECK_PERIOD = (HZ / 2),
88
89 /*
90 * Period of the TX queue check timer and the maximum number of TX
91 * descriptors to be reclaimed by the TX timer.
92 */
93 TX_QCHECK_PERIOD = (HZ / 2),
94 MAX_TIMER_TX_RECLAIM = 100,
95
96 /*
97 * An FL with <= FL_STARVE_THRES buffers is starving and a periodic
98 * timer will attempt to refill it.
99 */
100 FL_STARVE_THRES = 4,
101
102 /*
103 * Suspend an Ethernet TX queue with fewer available descriptors than
104 * this. We always want to have room for a maximum sized packet:
105 * inline immediate data + MAX_SKB_FRAGS. This is the same as
106 * calc_tx_flits() for a TSO packet with nr_frags == MAX_SKB_FRAGS
107 * (see that function and its helpers for a description of the
108 * calculation).
109 */
110 ETHTXQ_MAX_FRAGS = MAX_SKB_FRAGS + 1,
111 ETHTXQ_MAX_SGL_LEN = ((3 * (ETHTXQ_MAX_FRAGS-1))/2 +
112 ((ETHTXQ_MAX_FRAGS-1) & 1) +
113 2),
114 ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) +
115 sizeof(struct cpl_tx_pkt_lso_core) +
116 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64),
117 ETHTXQ_MAX_FLITS = ETHTXQ_MAX_SGL_LEN + ETHTXQ_MAX_HDR,
118
119 ETHTXQ_STOP_THRES = 1 + DIV_ROUND_UP(ETHTXQ_MAX_FLITS, TXD_PER_EQ_UNIT),
120
121 /*
122 * Max TX descriptor space we allow for an Ethernet packet to be
123 * inlined into a WR. This is limited by the maximum value which
124 * we can specify for immediate data in the firmware Ethernet TX
125 * Work Request.
126 */
127 MAX_IMM_TX_PKT_LEN = FW_WR_IMMDLEN_MASK,
128
129 /*
130 * Max size of a WR sent through a control TX queue.
131 */
132 MAX_CTRL_WR_LEN = 256,
133
134 /*
135 * Maximum amount of data which we'll ever need to inline into a
136 * TX ring: max(MAX_IMM_TX_PKT_LEN, MAX_CTRL_WR_LEN).
137 */
138 MAX_IMM_TX_LEN = (MAX_IMM_TX_PKT_LEN > MAX_CTRL_WR_LEN
139 ? MAX_IMM_TX_PKT_LEN
140 : MAX_CTRL_WR_LEN),
141
142 /*
143 * For incoming packets less than RX_COPY_THRES, we copy the data into
144 * an skb rather than referencing the data. We allocate enough
145 * in-line room in skb's to accommodate pulling in RX_PULL_LEN bytes
146 * of the data (header).
147 */
148 RX_COPY_THRES = 256,
149 RX_PULL_LEN = 128,
Casey Leedomc6e0d912010-06-25 12:13:28 +0000150
Casey Leedomeb6c5032010-11-11 09:06:50 +0000151 /*
152 * Main body length for sk_buffs used for RX Ethernet packets with
153 * fragments. Should be >= RX_PULL_LEN but possibly bigger to give
154 * pskb_may_pull() some room.
155 */
156 RX_SKB_LEN = 512,
157};
Casey Leedomc6e0d912010-06-25 12:13:28 +0000158
159/*
160 * Software state per TX descriptor.
161 */
162struct tx_sw_desc {
163 struct sk_buff *skb; /* socket buffer of TX data source */
164 struct ulptx_sgl *sgl; /* scatter/gather list in TX Queue */
165};
166
167/*
168 * Software state per RX Free List descriptor. We keep track of the allocated
169 * FL page, its size, and its PCI DMA address (if the page is mapped). The FL
170 * page size and its PCI DMA mapped state are stored in the low bits of the
171 * PCI DMA address as per below.
172 */
173struct rx_sw_desc {
174 struct page *page; /* Free List page buffer */
175 dma_addr_t dma_addr; /* PCI DMA address (if mapped) */
176 /* and flags (see below) */
177};
178
179/*
180 * The low bits of rx_sw_desc.dma_addr have special meaning. Note that the
181 * SGE also uses the low 4 bits to determine the size of the buffer. It uses
182 * those bits to index into the SGE_FL_BUFFER_SIZE[index] register array.
183 * Since we only use SGE_FL_BUFFER_SIZE0 and SGE_FL_BUFFER_SIZE1, these low 4
184 * bits can only contain a 0 or a 1 to indicate which size buffer we're giving
185 * to the SGE. Thus, our software state of "is the buffer mapped for DMA" is
186 * maintained in an inverse sense so the hardware never sees that bit high.
187 */
188enum {
189 RX_LARGE_BUF = 1 << 0, /* buffer is SGE_FL_BUFFER_SIZE[1] */
190 RX_UNMAPPED_BUF = 1 << 1, /* buffer is not mapped */
191};
192
193/**
194 * get_buf_addr - return DMA buffer address of software descriptor
195 * @sdesc: pointer to the software buffer descriptor
196 *
197 * Return the DMA buffer address of a software descriptor (stripping out
198 * our low-order flag bits).
199 */
200static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *sdesc)
201{
202 return sdesc->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF);
203}
204
205/**
206 * is_buf_mapped - is buffer mapped for DMA?
207 * @sdesc: pointer to the software buffer descriptor
208 *
209 * Determine whether the buffer associated with a software descriptor in
210 * mapped for DMA or not.
211 */
212static inline bool is_buf_mapped(const struct rx_sw_desc *sdesc)
213{
214 return !(sdesc->dma_addr & RX_UNMAPPED_BUF);
215}
216
217/**
218 * need_skb_unmap - does the platform need unmapping of sk_buffs?
219 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300220 * Returns true if the platform needs sk_buff unmapping. The compiler
221 * optimizes away unnecessary code if this returns true.
Casey Leedomc6e0d912010-06-25 12:13:28 +0000222 */
223static inline int need_skb_unmap(void)
224{
FUJITA Tomonori57b2eaf2010-07-07 23:52:37 +0000225#ifdef CONFIG_NEED_DMA_MAP_STATE
226 return 1;
227#else
228 return 0;
229#endif
Casey Leedomc6e0d912010-06-25 12:13:28 +0000230}
231
232/**
233 * txq_avail - return the number of available slots in a TX queue
234 * @tq: the TX queue
235 *
236 * Returns the number of available descriptors in a TX queue.
237 */
238static inline unsigned int txq_avail(const struct sge_txq *tq)
239{
240 return tq->size - 1 - tq->in_use;
241}
242
243/**
244 * fl_cap - return the capacity of a Free List
245 * @fl: the Free List
246 *
247 * Returns the capacity of a Free List. The capacity is less than the
248 * size because an Egress Queue Index Unit worth of descriptors needs to
249 * be left unpopulated, otherwise the Producer and Consumer indices PIDX
250 * and CIDX will match and the hardware will think the FL is empty.
251 */
252static inline unsigned int fl_cap(const struct sge_fl *fl)
253{
254 return fl->size - FL_PER_EQ_UNIT;
255}
256
257/**
258 * fl_starving - return whether a Free List is starving.
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530259 * @adapter: pointer to the adapter
Casey Leedomc6e0d912010-06-25 12:13:28 +0000260 * @fl: the Free List
261 *
262 * Tests specified Free List to see whether the number of buffers
263 * available to the hardware has falled below our "starvation"
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300264 * threshold.
Casey Leedomc6e0d912010-06-25 12:13:28 +0000265 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530266static inline bool fl_starving(const struct adapter *adapter,
267 const struct sge_fl *fl)
Casey Leedomc6e0d912010-06-25 12:13:28 +0000268{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530269 const struct sge *s = &adapter->sge;
270
271 return fl->avail - fl->pend_cred <= s->fl_starve_thres;
Casey Leedomc6e0d912010-06-25 12:13:28 +0000272}
273
274/**
275 * map_skb - map an skb for DMA to the device
276 * @dev: the egress net device
277 * @skb: the packet to map
278 * @addr: a pointer to the base of the DMA mapping array
279 *
280 * Map an skb for DMA to the device and return an array of DMA addresses.
281 */
282static int map_skb(struct device *dev, const struct sk_buff *skb,
283 dma_addr_t *addr)
284{
285 const skb_frag_t *fp, *end;
286 const struct skb_shared_info *si;
287
288 *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
289 if (dma_mapping_error(dev, *addr))
290 goto out_err;
291
292 si = skb_shinfo(skb);
293 end = &si->frags[si->nr_frags];
294 for (fp = si->frags; fp < end; fp++) {
Ian Campbella0006a82011-10-19 23:01:47 +0000295 *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
296 DMA_TO_DEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000297 if (dma_mapping_error(dev, *addr))
298 goto unwind;
299 }
300 return 0;
301
302unwind:
303 while (fp-- > si->frags)
Eric Dumazet9e903e02011-10-18 21:00:24 +0000304 dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000305 dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
306
307out_err:
308 return -ENOMEM;
309}
310
311static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
312 const struct ulptx_sgl *sgl, const struct sge_txq *tq)
313{
314 const struct ulptx_sge_pair *p;
315 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
316
317 if (likely(skb_headlen(skb)))
318 dma_unmap_single(dev, be64_to_cpu(sgl->addr0),
319 be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
320 else {
321 dma_unmap_page(dev, be64_to_cpu(sgl->addr0),
322 be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
323 nfrags--;
324 }
325
326 /*
327 * the complexity below is because of the possibility of a wrap-around
328 * in the middle of an SGL
329 */
330 for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
331 if (likely((u8 *)(p + 1) <= (u8 *)tq->stat)) {
332unmap:
333 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
334 be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
335 dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
336 be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
337 p++;
338 } else if ((u8 *)p == (u8 *)tq->stat) {
339 p = (const struct ulptx_sge_pair *)tq->desc;
340 goto unmap;
341 } else if ((u8 *)p + 8 == (u8 *)tq->stat) {
342 const __be64 *addr = (const __be64 *)tq->desc;
343
344 dma_unmap_page(dev, be64_to_cpu(addr[0]),
345 be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
346 dma_unmap_page(dev, be64_to_cpu(addr[1]),
347 be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
348 p = (const struct ulptx_sge_pair *)&addr[2];
349 } else {
350 const __be64 *addr = (const __be64 *)tq->desc;
351
352 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
353 be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
354 dma_unmap_page(dev, be64_to_cpu(addr[0]),
355 be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
356 p = (const struct ulptx_sge_pair *)&addr[1];
357 }
358 }
359 if (nfrags) {
360 __be64 addr;
361
362 if ((u8 *)p == (u8 *)tq->stat)
363 p = (const struct ulptx_sge_pair *)tq->desc;
364 addr = ((u8 *)p + 16 <= (u8 *)tq->stat
365 ? p->addr[0]
366 : *(const __be64 *)tq->desc);
367 dma_unmap_page(dev, be64_to_cpu(addr), be32_to_cpu(p->len[0]),
368 DMA_TO_DEVICE);
369 }
370}
371
372/**
373 * free_tx_desc - reclaims TX descriptors and their buffers
374 * @adapter: the adapter
375 * @tq: the TX queue to reclaim descriptors from
376 * @n: the number of descriptors to reclaim
377 * @unmap: whether the buffers should be unmapped for DMA
378 *
379 * Reclaims TX descriptors from an SGE TX queue and frees the associated
380 * TX buffers. Called with the TX queue lock held.
381 */
382static void free_tx_desc(struct adapter *adapter, struct sge_txq *tq,
383 unsigned int n, bool unmap)
384{
385 struct tx_sw_desc *sdesc;
386 unsigned int cidx = tq->cidx;
387 struct device *dev = adapter->pdev_dev;
388
389 const int need_unmap = need_skb_unmap() && unmap;
390
391 sdesc = &tq->sdesc[cidx];
392 while (n--) {
393 /*
394 * If we kept a reference to the original TX skb, we need to
395 * unmap it from PCI DMA space (if required) and free it.
396 */
397 if (sdesc->skb) {
398 if (need_unmap)
399 unmap_sgl(dev, sdesc->skb, sdesc->sgl, tq);
Eric W. Biederman42ffda52014-03-15 16:31:32 -0700400 dev_consume_skb_any(sdesc->skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000401 sdesc->skb = NULL;
402 }
403
404 sdesc++;
405 if (++cidx == tq->size) {
406 cidx = 0;
407 sdesc = tq->sdesc;
408 }
409 }
410 tq->cidx = cidx;
411}
412
413/*
414 * Return the number of reclaimable descriptors in a TX queue.
415 */
416static inline int reclaimable(const struct sge_txq *tq)
417{
418 int hw_cidx = be16_to_cpu(tq->stat->cidx);
419 int reclaimable = hw_cidx - tq->cidx;
420 if (reclaimable < 0)
421 reclaimable += tq->size;
422 return reclaimable;
423}
424
425/**
426 * reclaim_completed_tx - reclaims completed TX descriptors
427 * @adapter: the adapter
428 * @tq: the TX queue to reclaim completed descriptors from
429 * @unmap: whether the buffers should be unmapped for DMA
430 *
431 * Reclaims TX descriptors that the SGE has indicated it has processed,
432 * and frees the associated buffers if possible. Called with the TX
433 * queue locked.
434 */
435static inline void reclaim_completed_tx(struct adapter *adapter,
436 struct sge_txq *tq,
437 bool unmap)
438{
439 int avail = reclaimable(tq);
440
441 if (avail) {
442 /*
443 * Limit the amount of clean up work we do at a time to keep
444 * the TX lock hold time O(1).
445 */
446 if (avail > MAX_TX_RECLAIM)
447 avail = MAX_TX_RECLAIM;
448
449 free_tx_desc(adapter, tq, avail, unmap);
450 tq->in_use -= avail;
451 }
452}
453
454/**
455 * get_buf_size - return the size of an RX Free List buffer.
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530456 * @adapter: pointer to the associated adapter
Casey Leedomc6e0d912010-06-25 12:13:28 +0000457 * @sdesc: pointer to the software buffer descriptor
458 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530459static inline int get_buf_size(const struct adapter *adapter,
460 const struct rx_sw_desc *sdesc)
Casey Leedomc6e0d912010-06-25 12:13:28 +0000461{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530462 const struct sge *s = &adapter->sge;
463
464 return (s->fl_pg_order > 0 && (sdesc->dma_addr & RX_LARGE_BUF)
465 ? (PAGE_SIZE << s->fl_pg_order) : PAGE_SIZE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000466}
467
468/**
469 * free_rx_bufs - free RX buffers on an SGE Free List
470 * @adapter: the adapter
471 * @fl: the SGE Free List to free buffers from
472 * @n: how many buffers to free
473 *
474 * Release the next @n buffers on an SGE Free List RX queue. The
475 * buffers must be made inaccessible to hardware before calling this
476 * function.
477 */
478static void free_rx_bufs(struct adapter *adapter, struct sge_fl *fl, int n)
479{
480 while (n--) {
481 struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
482
483 if (is_buf_mapped(sdesc))
484 dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530485 get_buf_size(adapter, sdesc),
486 PCI_DMA_FROMDEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000487 put_page(sdesc->page);
488 sdesc->page = NULL;
489 if (++fl->cidx == fl->size)
490 fl->cidx = 0;
491 fl->avail--;
492 }
493}
494
495/**
496 * unmap_rx_buf - unmap the current RX buffer on an SGE Free List
497 * @adapter: the adapter
498 * @fl: the SGE Free List
499 *
500 * Unmap the current buffer on an SGE Free List RX queue. The
501 * buffer must be made inaccessible to HW before calling this function.
502 *
503 * This is similar to @free_rx_bufs above but does not free the buffer.
504 * Do note that the FL still loses any further access to the buffer.
505 * This is used predominantly to "transfer ownership" of an FL buffer
506 * to another entity (typically an skb's fragment list).
507 */
508static void unmap_rx_buf(struct adapter *adapter, struct sge_fl *fl)
509{
510 struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
511
512 if (is_buf_mapped(sdesc))
513 dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530514 get_buf_size(adapter, sdesc),
515 PCI_DMA_FROMDEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000516 sdesc->page = NULL;
517 if (++fl->cidx == fl->size)
518 fl->cidx = 0;
519 fl->avail--;
520}
521
522/**
523 * ring_fl_db - righ doorbell on free list
524 * @adapter: the adapter
525 * @fl: the Free List whose doorbell should be rung ...
526 *
527 * Tell the Scatter Gather Engine that there are new free list entries
528 * available.
529 */
530static inline void ring_fl_db(struct adapter *adapter, struct sge_fl *fl)
531{
Santosh Rastapur622c62b2013-03-14 05:08:57 +0000532 u32 val;
533
Casey Leedomc6e0d912010-06-25 12:13:28 +0000534 /*
535 * The SGE keeps track of its Producer and Consumer Indices in terms
536 * of Egress Queue Units so we can only tell it about integral numbers
537 * of multiples of Free List Entries per Egress Queue Units ...
538 */
539 if (fl->pend_cred >= FL_PER_EQ_UNIT) {
Santosh Rastapur622c62b2013-03-14 05:08:57 +0000540 val = PIDX(fl->pend_cred / FL_PER_EQ_UNIT);
Hariprasad Shenai70ee3662013-12-03 17:05:57 +0530541 if (!is_t4(adapter->params.chip))
Santosh Rastapur622c62b2013-03-14 05:08:57 +0000542 val |= DBTYPE(1);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000543 wmb();
544 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
Naresh Kumar Innace91a922012-11-15 22:41:17 +0530545 DBPRIO(1) |
Santosh Rastapur622c62b2013-03-14 05:08:57 +0000546 QID(fl->cntxt_id) | val);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000547 fl->pend_cred %= FL_PER_EQ_UNIT;
548 }
549}
550
551/**
552 * set_rx_sw_desc - initialize software RX buffer descriptor
553 * @sdesc: pointer to the softwore RX buffer descriptor
554 * @page: pointer to the page data structure backing the RX buffer
555 * @dma_addr: PCI DMA address (possibly with low-bit flags)
556 */
557static inline void set_rx_sw_desc(struct rx_sw_desc *sdesc, struct page *page,
558 dma_addr_t dma_addr)
559{
560 sdesc->page = page;
561 sdesc->dma_addr = dma_addr;
562}
563
564/*
565 * Support for poisoning RX buffers ...
566 */
567#define POISON_BUF_VAL -1
568
569static inline void poison_buf(struct page *page, size_t sz)
570{
571#if POISON_BUF_VAL >= 0
572 memset(page_address(page), POISON_BUF_VAL, sz);
573#endif
574}
575
576/**
577 * refill_fl - refill an SGE RX buffer ring
578 * @adapter: the adapter
579 * @fl: the Free List ring to refill
580 * @n: the number of new buffers to allocate
581 * @gfp: the gfp flags for the allocations
582 *
583 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
584 * allocated with the supplied gfp flags. The caller must assure that
585 * @n does not exceed the queue's capacity -- i.e. (cidx == pidx) _IN
586 * EGRESS QUEUE UNITS_ indicates an empty Free List! Returns the number
587 * of buffers allocated. If afterwards the queue is found critically low,
588 * mark it as starving in the bitmap of starving FLs.
589 */
590static unsigned int refill_fl(struct adapter *adapter, struct sge_fl *fl,
591 int n, gfp_t gfp)
592{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530593 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +0000594 struct page *page;
595 dma_addr_t dma_addr;
596 unsigned int cred = fl->avail;
597 __be64 *d = &fl->desc[fl->pidx];
598 struct rx_sw_desc *sdesc = &fl->sdesc[fl->pidx];
599
600 /*
601 * Sanity: ensure that the result of adding n Free List buffers
602 * won't result in wrapping the SGE's Producer Index around to
603 * it's Consumer Index thereby indicating an empty Free List ...
604 */
605 BUG_ON(fl->avail + n > fl->size - FL_PER_EQ_UNIT);
606
607 /*
608 * If we support large pages, prefer large buffers and fail over to
609 * small pages if we can't allocate large pages to satisfy the refill.
610 * If we don't support large pages, drop directly into the small page
611 * allocation code.
612 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530613 if (s->fl_pg_order == 0)
Casey Leedomc6e0d912010-06-25 12:13:28 +0000614 goto alloc_small_pages;
615
616 while (n) {
617 page = alloc_pages(gfp | __GFP_COMP | __GFP_NOWARN,
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530618 s->fl_pg_order);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000619 if (unlikely(!page)) {
620 /*
621 * We've failed inour attempt to allocate a "large
622 * page". Fail over to the "small page" allocation
623 * below.
624 */
625 fl->large_alloc_failed++;
626 break;
627 }
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530628 poison_buf(page, PAGE_SIZE << s->fl_pg_order);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000629
630 dma_addr = dma_map_page(adapter->pdev_dev, page, 0,
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530631 PAGE_SIZE << s->fl_pg_order,
Casey Leedomc6e0d912010-06-25 12:13:28 +0000632 PCI_DMA_FROMDEVICE);
633 if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
634 /*
635 * We've run out of DMA mapping space. Free up the
636 * buffer and return with what we've managed to put
637 * into the free list. We don't want to fail over to
638 * the small page allocation below in this case
639 * because DMA mapping resources are typically
640 * critical resources once they become scarse.
641 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530642 __free_pages(page, s->fl_pg_order);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000643 goto out;
644 }
645 dma_addr |= RX_LARGE_BUF;
646 *d++ = cpu_to_be64(dma_addr);
647
648 set_rx_sw_desc(sdesc, page, dma_addr);
649 sdesc++;
650
651 fl->avail++;
652 if (++fl->pidx == fl->size) {
653 fl->pidx = 0;
654 sdesc = fl->sdesc;
655 d = fl->desc;
656 }
657 n--;
658 }
659
660alloc_small_pages:
661 while (n--) {
Mel Gorman06140022012-07-31 16:44:24 -0700662 page = __skb_alloc_page(gfp | __GFP_NOWARN, NULL);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000663 if (unlikely(!page)) {
664 fl->alloc_failed++;
665 break;
666 }
667 poison_buf(page, PAGE_SIZE);
668
669 dma_addr = dma_map_page(adapter->pdev_dev, page, 0, PAGE_SIZE,
670 PCI_DMA_FROMDEVICE);
671 if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
Eric Dumazet1f2149c2011-11-22 10:57:41 +0000672 put_page(page);
Casey Leedomc6e0d912010-06-25 12:13:28 +0000673 break;
674 }
675 *d++ = cpu_to_be64(dma_addr);
676
677 set_rx_sw_desc(sdesc, page, dma_addr);
678 sdesc++;
679
680 fl->avail++;
681 if (++fl->pidx == fl->size) {
682 fl->pidx = 0;
683 sdesc = fl->sdesc;
684 d = fl->desc;
685 }
686 }
687
688out:
689 /*
690 * Update our accounting state to incorporate the new Free List
691 * buffers, tell the hardware about them and return the number of
Paul Bolle90802ed2011-12-05 13:00:34 +0100692 * buffers which we were able to allocate.
Casey Leedomc6e0d912010-06-25 12:13:28 +0000693 */
694 cred = fl->avail - cred;
695 fl->pend_cred += cred;
696 ring_fl_db(adapter, fl);
697
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +0530698 if (unlikely(fl_starving(adapter, fl))) {
Casey Leedomc6e0d912010-06-25 12:13:28 +0000699 smp_wmb();
700 set_bit(fl->cntxt_id, adapter->sge.starving_fl);
701 }
702
703 return cred;
704}
705
706/*
707 * Refill a Free List to its capacity or the Maximum Refill Increment,
708 * whichever is smaller ...
709 */
710static inline void __refill_fl(struct adapter *adapter, struct sge_fl *fl)
711{
712 refill_fl(adapter, fl,
713 min((unsigned int)MAX_RX_REFILL, fl_cap(fl) - fl->avail),
714 GFP_ATOMIC);
715}
716
717/**
718 * alloc_ring - allocate resources for an SGE descriptor ring
719 * @dev: the PCI device's core device
720 * @nelem: the number of descriptors
721 * @hwsize: the size of each hardware descriptor
722 * @swsize: the size of each software descriptor
723 * @busaddrp: the physical PCI bus address of the allocated ring
724 * @swringp: return address pointer for software ring
725 * @stat_size: extra space in hardware ring for status information
726 *
727 * Allocates resources for an SGE descriptor ring, such as TX queues,
728 * free buffer lists, response queues, etc. Each SGE ring requires
729 * space for its hardware descriptors plus, optionally, space for software
730 * state associated with each hardware entry (the metadata). The function
731 * returns three values: the virtual address for the hardware ring (the
732 * return value of the function), the PCI bus address of the hardware
733 * ring (in *busaddrp), and the address of the software ring (in swringp).
734 * Both the hardware and software rings are returned zeroed out.
735 */
736static void *alloc_ring(struct device *dev, size_t nelem, size_t hwsize,
737 size_t swsize, dma_addr_t *busaddrp, void *swringp,
738 size_t stat_size)
739{
740 /*
741 * Allocate the hardware ring and PCI DMA bus address space for said.
742 */
743 size_t hwlen = nelem * hwsize + stat_size;
744 void *hwring = dma_alloc_coherent(dev, hwlen, busaddrp, GFP_KERNEL);
745
746 if (!hwring)
747 return NULL;
748
749 /*
750 * If the caller wants a software ring, allocate it and return a
751 * pointer to it in *swringp.
752 */
753 BUG_ON((swsize != 0) != (swringp != NULL));
754 if (swsize) {
755 void *swring = kcalloc(nelem, swsize, GFP_KERNEL);
756
757 if (!swring) {
758 dma_free_coherent(dev, hwlen, hwring, *busaddrp);
759 return NULL;
760 }
761 *(void **)swringp = swring;
762 }
763
764 /*
765 * Zero out the hardware ring and return its address as our function
766 * value.
767 */
768 memset(hwring, 0, hwlen);
769 return hwring;
770}
771
772/**
773 * sgl_len - calculates the size of an SGL of the given capacity
774 * @n: the number of SGL entries
775 *
776 * Calculates the number of flits (8-byte units) needed for a Direct
777 * Scatter/Gather List that can hold the given number of entries.
778 */
779static inline unsigned int sgl_len(unsigned int n)
780{
781 /*
782 * A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
783 * addresses. The DSGL Work Request starts off with a 32-bit DSGL
784 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
785 * repeated sequences of { Length[i], Length[i+1], Address[i],
786 * Address[i+1] } (this ensures that all addresses are on 64-bit
787 * boundaries). If N is even, then Length[N+1] should be set to 0 and
788 * Address[N+1] is omitted.
789 *
790 * The following calculation incorporates all of the above. It's
791 * somewhat hard to follow but, briefly: the "+2" accounts for the
792 * first two flits which include the DSGL header, Length0 and
793 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
794 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
795 * finally the "+((n-1)&1)" adds the one remaining flit needed if
796 * (n-1) is odd ...
797 */
798 n--;
799 return (3 * n) / 2 + (n & 1) + 2;
800}
801
802/**
803 * flits_to_desc - returns the num of TX descriptors for the given flits
804 * @flits: the number of flits
805 *
806 * Returns the number of TX descriptors needed for the supplied number
807 * of flits.
808 */
809static inline unsigned int flits_to_desc(unsigned int flits)
810{
811 BUG_ON(flits > SGE_MAX_WR_LEN / sizeof(__be64));
812 return DIV_ROUND_UP(flits, TXD_PER_EQ_UNIT);
813}
814
815/**
816 * is_eth_imm - can an Ethernet packet be sent as immediate data?
817 * @skb: the packet
818 *
819 * Returns whether an Ethernet packet is small enough to fit completely as
820 * immediate data.
821 */
822static inline int is_eth_imm(const struct sk_buff *skb)
823{
824 /*
825 * The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request
826 * which does not accommodate immediate data. We could dike out all
827 * of the support code for immediate data but that would tie our hands
828 * too much if we ever want to enhace the firmware. It would also
829 * create more differences between the PF and VF Drivers.
830 */
831 return false;
832}
833
834/**
835 * calc_tx_flits - calculate the number of flits for a packet TX WR
836 * @skb: the packet
837 *
838 * Returns the number of flits needed for a TX Work Request for the
839 * given Ethernet packet, including the needed WR and CPL headers.
840 */
841static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
842{
843 unsigned int flits;
844
845 /*
846 * If the skb is small enough, we can pump it out as a work request
847 * with only immediate data. In that case we just have to have the
848 * TX Packet header plus the skb data in the Work Request.
849 */
850 if (is_eth_imm(skb))
851 return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt),
852 sizeof(__be64));
853
854 /*
855 * Otherwise, we're going to have to construct a Scatter gather list
856 * of the skb body and fragments. We also include the flits necessary
857 * for the TX Packet Work Request and CPL. We always have a firmware
858 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
859 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
860 * message or, if we're doing a Large Send Offload, an LSO CPL message
861 * with an embeded TX Packet Write CPL message.
862 */
863 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
864 if (skb_shinfo(skb)->gso_size)
865 flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
866 sizeof(struct cpl_tx_pkt_lso_core) +
867 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
868 else
869 flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
870 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
871 return flits;
872}
873
874/**
875 * write_sgl - populate a Scatter/Gather List for a packet
876 * @skb: the packet
877 * @tq: the TX queue we are writing into
878 * @sgl: starting location for writing the SGL
879 * @end: points right after the end of the SGL
880 * @start: start offset into skb main-body data to include in the SGL
881 * @addr: the list of DMA bus addresses for the SGL elements
882 *
883 * Generates a Scatter/Gather List for the buffers that make up a packet.
884 * The caller must provide adequate space for the SGL that will be written.
885 * The SGL includes all of the packet's page fragments and the data in its
886 * main body except for the first @start bytes. @pos must be 16-byte
887 * aligned and within a TX descriptor with available space. @end points
888 * write after the end of the SGL but does not account for any potential
889 * wrap around, i.e., @end > @tq->stat.
890 */
891static void write_sgl(const struct sk_buff *skb, struct sge_txq *tq,
892 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
893 const dma_addr_t *addr)
894{
895 unsigned int i, len;
896 struct ulptx_sge_pair *to;
897 const struct skb_shared_info *si = skb_shinfo(skb);
898 unsigned int nfrags = si->nr_frags;
899 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
900
901 len = skb_headlen(skb) - start;
902 if (likely(len)) {
903 sgl->len0 = htonl(len);
904 sgl->addr0 = cpu_to_be64(addr[0] + start);
905 nfrags++;
906 } else {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000907 sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
Casey Leedomc6e0d912010-06-25 12:13:28 +0000908 sgl->addr0 = cpu_to_be64(addr[1]);
909 }
910
911 sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) |
912 ULPTX_NSGE(nfrags));
913 if (likely(--nfrags == 0))
914 return;
915 /*
916 * Most of the complexity below deals with the possibility we hit the
917 * end of the queue in the middle of writing the SGL. For this case
918 * only we create the SGL in a temporary buffer and then copy it.
919 */
920 to = (u8 *)end > (u8 *)tq->stat ? buf : sgl->sge;
921
922 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000923 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
924 to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
Casey Leedomc6e0d912010-06-25 12:13:28 +0000925 to->addr[0] = cpu_to_be64(addr[i]);
926 to->addr[1] = cpu_to_be64(addr[++i]);
927 }
928 if (nfrags) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000929 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
Casey Leedomc6e0d912010-06-25 12:13:28 +0000930 to->len[1] = cpu_to_be32(0);
931 to->addr[0] = cpu_to_be64(addr[i + 1]);
932 }
933 if (unlikely((u8 *)end > (u8 *)tq->stat)) {
934 unsigned int part0 = (u8 *)tq->stat - (u8 *)sgl->sge, part1;
935
936 if (likely(part0))
937 memcpy(sgl->sge, buf, part0);
938 part1 = (u8 *)end - (u8 *)tq->stat;
939 memcpy(tq->desc, (u8 *)buf + part0, part1);
940 end = (void *)tq->desc + part1;
941 }
942 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
Joe Perches64699332012-06-04 12:44:16 +0000943 *end = 0;
Casey Leedomc6e0d912010-06-25 12:13:28 +0000944}
945
946/**
947 * check_ring_tx_db - check and potentially ring a TX queue's doorbell
948 * @adapter: the adapter
949 * @tq: the TX queue
950 * @n: number of new descriptors to give to HW
951 *
952 * Ring the doorbel for a TX queue.
953 */
954static inline void ring_tx_db(struct adapter *adapter, struct sge_txq *tq,
955 int n)
956{
957 /*
958 * Warn if we write doorbells with the wrong priority and write
959 * descriptors before telling HW.
960 */
Naresh Kumar Innace91a922012-11-15 22:41:17 +0530961 WARN_ON((QID(tq->cntxt_id) | PIDX(n)) & DBPRIO(1));
Casey Leedomc6e0d912010-06-25 12:13:28 +0000962 wmb();
963 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
964 QID(tq->cntxt_id) | PIDX(n));
965}
966
967/**
968 * inline_tx_skb - inline a packet's data into TX descriptors
969 * @skb: the packet
970 * @tq: the TX queue where the packet will be inlined
971 * @pos: starting position in the TX queue to inline the packet
972 *
973 * Inline a packet's contents directly into TX descriptors, starting at
974 * the given position within the TX DMA ring.
975 * Most of the complexity of this operation is dealing with wrap arounds
976 * in the middle of the packet we want to inline.
977 */
978static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *tq,
979 void *pos)
980{
981 u64 *p;
982 int left = (void *)tq->stat - pos;
983
984 if (likely(skb->len <= left)) {
985 if (likely(!skb->data_len))
986 skb_copy_from_linear_data(skb, pos, skb->len);
987 else
988 skb_copy_bits(skb, 0, pos, skb->len);
989 pos += skb->len;
990 } else {
991 skb_copy_bits(skb, 0, pos, left);
992 skb_copy_bits(skb, left, tq->desc, skb->len - left);
993 pos = (void *)tq->desc + (skb->len - left);
994 }
995
996 /* 0-pad to multiple of 16 */
997 p = PTR_ALIGN(pos, 8);
998 if ((uintptr_t)p & 8)
999 *p = 0;
1000}
1001
1002/*
1003 * Figure out what HW csum a packet wants and return the appropriate control
1004 * bits.
1005 */
1006static u64 hwcsum(const struct sk_buff *skb)
1007{
1008 int csum_type;
1009 const struct iphdr *iph = ip_hdr(skb);
1010
1011 if (iph->version == 4) {
1012 if (iph->protocol == IPPROTO_TCP)
1013 csum_type = TX_CSUM_TCPIP;
1014 else if (iph->protocol == IPPROTO_UDP)
1015 csum_type = TX_CSUM_UDPIP;
1016 else {
1017nocsum:
1018 /*
1019 * unknown protocol, disable HW csum
1020 * and hope a bad packet is detected
1021 */
1022 return TXPKT_L4CSUM_DIS;
1023 }
1024 } else {
1025 /*
1026 * this doesn't work with extension headers
1027 */
1028 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
1029
1030 if (ip6h->nexthdr == IPPROTO_TCP)
1031 csum_type = TX_CSUM_TCPIP6;
1032 else if (ip6h->nexthdr == IPPROTO_UDP)
1033 csum_type = TX_CSUM_UDPIP6;
1034 else
1035 goto nocsum;
1036 }
1037
1038 if (likely(csum_type >= TX_CSUM_TCPIP))
1039 return TXPKT_CSUM_TYPE(csum_type) |
1040 TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
1041 TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
1042 else {
1043 int start = skb_transport_offset(skb);
1044
1045 return TXPKT_CSUM_TYPE(csum_type) |
1046 TXPKT_CSUM_START(start) |
1047 TXPKT_CSUM_LOC(start + skb->csum_offset);
1048 }
1049}
1050
1051/*
1052 * Stop an Ethernet TX queue and record that state change.
1053 */
1054static void txq_stop(struct sge_eth_txq *txq)
1055{
1056 netif_tx_stop_queue(txq->txq);
1057 txq->q.stops++;
1058}
1059
1060/*
1061 * Advance our software state for a TX queue by adding n in use descriptors.
1062 */
1063static inline void txq_advance(struct sge_txq *tq, unsigned int n)
1064{
1065 tq->in_use += n;
1066 tq->pidx += n;
1067 if (tq->pidx >= tq->size)
1068 tq->pidx -= tq->size;
1069}
1070
1071/**
1072 * t4vf_eth_xmit - add a packet to an Ethernet TX queue
1073 * @skb: the packet
1074 * @dev: the egress net device
1075 *
1076 * Add a packet to an SGE Ethernet TX queue. Runs with softirqs disabled.
1077 */
1078int t4vf_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1079{
Casey Leedom7f9dd2f2010-07-12 14:39:07 -07001080 u32 wr_mid;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001081 u64 cntrl, *end;
1082 int qidx, credits;
1083 unsigned int flits, ndesc;
1084 struct adapter *adapter;
1085 struct sge_eth_txq *txq;
1086 const struct port_info *pi;
1087 struct fw_eth_tx_pkt_vm_wr *wr;
1088 struct cpl_tx_pkt_core *cpl;
1089 const struct skb_shared_info *ssi;
1090 dma_addr_t addr[MAX_SKB_FRAGS + 1];
1091 const size_t fw_hdr_copy_len = (sizeof(wr->ethmacdst) +
1092 sizeof(wr->ethmacsrc) +
1093 sizeof(wr->ethtype) +
1094 sizeof(wr->vlantci));
1095
1096 /*
1097 * The chip minimum packet length is 10 octets but the firmware
1098 * command that we are using requires that we copy the Ethernet header
1099 * (including the VLAN tag) into the header so we reject anything
1100 * smaller than that ...
1101 */
1102 if (unlikely(skb->len < fw_hdr_copy_len))
1103 goto out_free;
1104
1105 /*
1106 * Figure out which TX Queue we're going to use.
1107 */
1108 pi = netdev_priv(dev);
1109 adapter = pi->adapter;
1110 qidx = skb_get_queue_mapping(skb);
1111 BUG_ON(qidx >= pi->nqsets);
1112 txq = &adapter->sge.ethtxq[pi->first_qset + qidx];
1113
1114 /*
1115 * Take this opportunity to reclaim any TX Descriptors whose DMA
1116 * transfers have completed.
1117 */
1118 reclaim_completed_tx(adapter, &txq->q, true);
1119
1120 /*
1121 * Calculate the number of flits and TX Descriptors we're going to
1122 * need along with how many TX Descriptors will be left over after
1123 * we inject our Work Request.
1124 */
1125 flits = calc_tx_flits(skb);
1126 ndesc = flits_to_desc(flits);
1127 credits = txq_avail(&txq->q) - ndesc;
1128
1129 if (unlikely(credits < 0)) {
1130 /*
1131 * Not enough room for this packet's Work Request. Stop the
1132 * TX Queue and return a "busy" condition. The queue will get
1133 * started later on when the firmware informs us that space
1134 * has opened up.
1135 */
1136 txq_stop(txq);
1137 dev_err(adapter->pdev_dev,
1138 "%s: TX ring %u full while queue awake!\n",
1139 dev->name, qidx);
1140 return NETDEV_TX_BUSY;
1141 }
1142
1143 if (!is_eth_imm(skb) &&
1144 unlikely(map_skb(adapter->pdev_dev, skb, addr) < 0)) {
1145 /*
1146 * We need to map the skb into PCI DMA space (because it can't
1147 * be in-lined directly into the Work Request) and the mapping
1148 * operation failed. Record the error and drop the packet.
1149 */
1150 txq->mapping_err++;
1151 goto out_free;
1152 }
1153
Casey Leedom7f9dd2f2010-07-12 14:39:07 -07001154 wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001155 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1156 /*
1157 * After we're done injecting the Work Request for this
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001158 * packet, we'll be below our "stop threshold" so stop the TX
Casey Leedom7f9dd2f2010-07-12 14:39:07 -07001159 * Queue now and schedule a request for an SGE Egress Queue
1160 * Update message. The queue will get started later on when
1161 * the firmware processes this Work Request and sends us an
1162 * Egress Queue Status Update message indicating that space
1163 * has opened up.
Casey Leedomc6e0d912010-06-25 12:13:28 +00001164 */
1165 txq_stop(txq);
Casey Leedom7f9dd2f2010-07-12 14:39:07 -07001166 wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001167 }
1168
1169 /*
1170 * Start filling in our Work Request. Note that we do _not_ handle
1171 * the WR Header wrapping around the TX Descriptor Ring. If our
1172 * maximum header size ever exceeds one TX Descriptor, we'll need to
1173 * do something else here.
1174 */
1175 BUG_ON(DIV_ROUND_UP(ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1);
1176 wr = (void *)&txq->q.desc[txq->q.pidx];
Casey Leedom7f9dd2f2010-07-12 14:39:07 -07001177 wr->equiq_to_len16 = cpu_to_be32(wr_mid);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001178 wr->r3[0] = cpu_to_be64(0);
1179 wr->r3[1] = cpu_to_be64(0);
1180 skb_copy_from_linear_data(skb, (void *)wr->ethmacdst, fw_hdr_copy_len);
1181 end = (u64 *)wr + flits;
1182
1183 /*
1184 * If this is a Large Send Offload packet we'll put in an LSO CPL
1185 * message with an encapsulated TX Packet CPL message. Otherwise we
1186 * just use a TX Packet CPL message.
1187 */
1188 ssi = skb_shinfo(skb);
1189 if (ssi->gso_size) {
1190 struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
1191 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1192 int l3hdr_len = skb_network_header_len(skb);
1193 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1194
1195 wr->op_immdlen =
1196 cpu_to_be32(FW_WR_OP(FW_ETH_TX_PKT_VM_WR) |
1197 FW_WR_IMMDLEN(sizeof(*lso) +
1198 sizeof(*cpl)));
1199 /*
1200 * Fill in the LSO CPL message.
1201 */
1202 lso->lso_ctrl =
1203 cpu_to_be32(LSO_OPCODE(CPL_TX_PKT_LSO) |
1204 LSO_FIRST_SLICE |
1205 LSO_LAST_SLICE |
1206 LSO_IPV6(v6) |
1207 LSO_ETHHDR_LEN(eth_xtra_len/4) |
1208 LSO_IPHDR_LEN(l3hdr_len/4) |
1209 LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
1210 lso->ipid_ofst = cpu_to_be16(0);
1211 lso->mss = cpu_to_be16(ssi->gso_size);
1212 lso->seqno_offset = cpu_to_be32(0);
Hariprasad Shenai7207c0d2014-10-09 05:48:45 +05301213 if (is_t4(adapter->params.chip))
1214 lso->len = cpu_to_be32(skb->len);
1215 else
1216 lso->len = cpu_to_be32(LSO_T5_XFER_SIZE(skb->len));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001217
1218 /*
1219 * Set up TX Packet CPL pointer, control word and perform
1220 * accounting.
1221 */
1222 cpl = (void *)(lso + 1);
1223 cntrl = (TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
1224 TXPKT_IPHDR_LEN(l3hdr_len) |
1225 TXPKT_ETHHDR_LEN(eth_xtra_len));
1226 txq->tso++;
1227 txq->tx_cso += ssi->gso_segs;
1228 } else {
1229 int len;
1230
1231 len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
1232 wr->op_immdlen =
1233 cpu_to_be32(FW_WR_OP(FW_ETH_TX_PKT_VM_WR) |
1234 FW_WR_IMMDLEN(len));
1235
1236 /*
1237 * Set up TX Packet CPL pointer, control word and perform
1238 * accounting.
1239 */
1240 cpl = (void *)(wr + 1);
1241 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1242 cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
1243 txq->tx_cso++;
1244 } else
1245 cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
1246 }
1247
1248 /*
1249 * If there's a VLAN tag present, add that to the list of things to
1250 * do in this Work Request.
1251 */
1252 if (vlan_tx_tag_present(skb)) {
1253 txq->vlan_ins++;
1254 cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
1255 }
1256
1257 /*
1258 * Fill in the TX Packet CPL message header.
1259 */
1260 cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE(CPL_TX_PKT_XT) |
1261 TXPKT_INTF(pi->port_id) |
1262 TXPKT_PF(0));
1263 cpl->pack = cpu_to_be16(0);
1264 cpl->len = cpu_to_be16(skb->len);
1265 cpl->ctrl1 = cpu_to_be64(cntrl);
1266
1267#ifdef T4_TRACE
1268 T4_TRACE5(adapter->tb[txq->q.cntxt_id & 7],
1269 "eth_xmit: ndesc %u, credits %u, pidx %u, len %u, frags %u",
1270 ndesc, credits, txq->q.pidx, skb->len, ssi->nr_frags);
1271#endif
1272
1273 /*
1274 * Fill in the body of the TX Packet CPL message with either in-lined
1275 * data or a Scatter/Gather List.
1276 */
1277 if (is_eth_imm(skb)) {
1278 /*
1279 * In-line the packet's data and free the skb since we don't
1280 * need it any longer.
1281 */
1282 inline_tx_skb(skb, &txq->q, cpl + 1);
Eric W. Biederman42ffda52014-03-15 16:31:32 -07001283 dev_consume_skb_any(skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001284 } else {
1285 /*
1286 * Write the skb's Scatter/Gather list into the TX Packet CPL
1287 * message and retain a pointer to the skb so we can free it
1288 * later when its DMA completes. (We store the skb pointer
1289 * in the Software Descriptor corresponding to the last TX
1290 * Descriptor used by the Work Request.)
1291 *
1292 * The retained skb will be freed when the corresponding TX
1293 * Descriptors are reclaimed after their DMAs complete.
1294 * However, this could take quite a while since, in general,
1295 * the hardware is set up to be lazy about sending DMA
1296 * completion notifications to us and we mostly perform TX
1297 * reclaims in the transmit routine.
1298 *
1299 * This is good for performamce but means that we rely on new
1300 * TX packets arriving to run the destructors of completed
1301 * packets, which open up space in their sockets' send queues.
1302 * Sometimes we do not get such new packets causing TX to
1303 * stall. A single UDP transmitter is a good example of this
1304 * situation. We have a clean up timer that periodically
1305 * reclaims completed packets but it doesn't run often enough
1306 * (nor do we want it to) to prevent lengthy stalls. A
1307 * solution to this problem is to run the destructor early,
1308 * after the packet is queued but before it's DMAd. A con is
1309 * that we lie to socket memory accounting, but the amount of
1310 * extra memory is reasonable (limited by the number of TX
1311 * descriptors), the packets do actually get freed quickly by
1312 * new packets almost always, and for protocols like TCP that
1313 * wait for acks to really free up the data the extra memory
1314 * is even less. On the positive side we run the destructors
1315 * on the sending CPU rather than on a potentially different
Casey Leedom64bb3362010-06-29 12:53:39 +00001316 * completing CPU, usually a good thing.
Casey Leedomc6e0d912010-06-25 12:13:28 +00001317 *
1318 * Run the destructor before telling the DMA engine about the
1319 * packet to make sure it doesn't complete and get freed
1320 * prematurely.
1321 */
1322 struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1);
1323 struct sge_txq *tq = &txq->q;
1324 int last_desc;
1325
1326 /*
1327 * If the Work Request header was an exact multiple of our TX
1328 * Descriptor length, then it's possible that the starting SGL
1329 * pointer lines up exactly with the end of our TX Descriptor
1330 * ring. If that's the case, wrap around to the beginning
1331 * here ...
1332 */
1333 if (unlikely((void *)sgl == (void *)tq->stat)) {
1334 sgl = (void *)tq->desc;
Joe Perches64699332012-06-04 12:44:16 +00001335 end = ((void *)tq->desc + ((void *)end - (void *)tq->stat));
Casey Leedomc6e0d912010-06-25 12:13:28 +00001336 }
1337
1338 write_sgl(skb, tq, sgl, end, 0, addr);
1339 skb_orphan(skb);
1340
1341 last_desc = tq->pidx + ndesc - 1;
1342 if (last_desc >= tq->size)
1343 last_desc -= tq->size;
1344 tq->sdesc[last_desc].skb = skb;
1345 tq->sdesc[last_desc].sgl = sgl;
1346 }
1347
1348 /*
1349 * Advance our internal TX Queue state, tell the hardware about
1350 * the new TX descriptors and return success.
1351 */
1352 txq_advance(&txq->q, ndesc);
1353 dev->trans_start = jiffies;
1354 ring_tx_db(adapter, &txq->q, ndesc);
1355 return NETDEV_TX_OK;
1356
1357out_free:
1358 /*
1359 * An error of some sort happened. Free the TX skb and tell the
1360 * OS that we've "dealt" with the packet ...
1361 */
Eric W. Biederman42ffda52014-03-15 16:31:32 -07001362 dev_kfree_skb_any(skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001363 return NETDEV_TX_OK;
1364}
1365
1366/**
Ian Campbella0006a82011-10-19 23:01:47 +00001367 * copy_frags - copy fragments from gather list into skb_shared_info
1368 * @skb: destination skb
1369 * @gl: source internal packet gather list
1370 * @offset: packet start offset in first page
1371 *
1372 * Copy an internal packet gather list into a Linux skb_shared_info
1373 * structure.
1374 */
1375static inline void copy_frags(struct sk_buff *skb,
1376 const struct pkt_gl *gl,
1377 unsigned int offset)
1378{
1379 int i;
1380
1381 /* usually there's just one frag */
1382 __skb_fill_page_desc(skb, 0, gl->frags[0].page,
1383 gl->frags[0].offset + offset,
1384 gl->frags[0].size - offset);
1385 skb_shinfo(skb)->nr_frags = gl->nfrags;
1386 for (i = 1; i < gl->nfrags; i++)
1387 __skb_fill_page_desc(skb, i, gl->frags[i].page,
1388 gl->frags[i].offset,
1389 gl->frags[i].size);
1390
1391 /* get a reference to the last page, we don't own it */
1392 get_page(gl->frags[gl->nfrags - 1].page);
1393}
1394
1395/**
Casey Leedomeb6c5032010-11-11 09:06:50 +00001396 * t4vf_pktgl_to_skb - build an sk_buff from a packet gather list
1397 * @gl: the gather list
1398 * @skb_len: size of sk_buff main body if it carries fragments
1399 * @pull_len: amount of data to move to the sk_buff's main body
1400 *
1401 * Builds an sk_buff from the given packet gather list. Returns the
1402 * sk_buff or %NULL if sk_buff allocation failed.
1403 */
Sachin Kamat8a67d1c2013-09-18 09:00:01 +05301404static struct sk_buff *t4vf_pktgl_to_skb(const struct pkt_gl *gl,
1405 unsigned int skb_len,
1406 unsigned int pull_len)
Casey Leedomeb6c5032010-11-11 09:06:50 +00001407{
1408 struct sk_buff *skb;
Casey Leedomeb6c5032010-11-11 09:06:50 +00001409
1410 /*
1411 * If the ingress packet is small enough, allocate an skb large enough
1412 * for all of the data and copy it inline. Otherwise, allocate an skb
1413 * with enough room to pull in the header and reference the rest of
1414 * the data via the skb fragment list.
1415 *
1416 * Below we rely on RX_COPY_THRES being less than the smallest Rx
1417 * buff! size, which is expected since buffers are at least
1418 * PAGE_SIZEd. In this case packets up to RX_COPY_THRES have only one
1419 * fragment.
1420 */
1421 if (gl->tot_len <= RX_COPY_THRES) {
1422 /* small packets have only one fragment */
1423 skb = alloc_skb(gl->tot_len, GFP_ATOMIC);
1424 if (unlikely(!skb))
1425 goto out;
1426 __skb_put(skb, gl->tot_len);
1427 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1428 } else {
1429 skb = alloc_skb(skb_len, GFP_ATOMIC);
1430 if (unlikely(!skb))
1431 goto out;
1432 __skb_put(skb, pull_len);
1433 skb_copy_to_linear_data(skb, gl->va, pull_len);
1434
Ian Campbella0006a82011-10-19 23:01:47 +00001435 copy_frags(skb, gl, pull_len);
Casey Leedomeb6c5032010-11-11 09:06:50 +00001436 skb->len = gl->tot_len;
1437 skb->data_len = skb->len - pull_len;
1438 skb->truesize += skb->data_len;
Casey Leedomeb6c5032010-11-11 09:06:50 +00001439 }
1440
1441out:
1442 return skb;
1443}
1444
1445/**
Casey Leedomc6e0d912010-06-25 12:13:28 +00001446 * t4vf_pktgl_free - free a packet gather list
1447 * @gl: the gather list
1448 *
1449 * Releases the pages of a packet gather list. We do not own the last
1450 * page on the list and do not free it.
1451 */
Sachin Kamat8a67d1c2013-09-18 09:00:01 +05301452static void t4vf_pktgl_free(const struct pkt_gl *gl)
Casey Leedomc6e0d912010-06-25 12:13:28 +00001453{
1454 int frag;
1455
1456 frag = gl->nfrags - 1;
1457 while (frag--)
1458 put_page(gl->frags[frag].page);
1459}
1460
1461/**
Casey Leedomc6e0d912010-06-25 12:13:28 +00001462 * do_gro - perform Generic Receive Offload ingress packet processing
1463 * @rxq: ingress RX Ethernet Queue
1464 * @gl: gather list for ingress packet
1465 * @pkt: CPL header for last packet fragment
1466 *
1467 * Perform Generic Receive Offload (GRO) ingress packet processing.
1468 * We use the standard Linux GRO interfaces for this.
1469 */
1470static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1471 const struct cpl_rx_pkt *pkt)
1472{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301473 struct adapter *adapter = rxq->rspq.adapter;
1474 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001475 int ret;
1476 struct sk_buff *skb;
1477
1478 skb = napi_get_frags(&rxq->rspq.napi);
1479 if (unlikely(!skb)) {
1480 t4vf_pktgl_free(gl);
1481 rxq->stats.rx_drops++;
1482 return;
1483 }
1484
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301485 copy_frags(skb, gl, s->pktshift);
1486 skb->len = gl->tot_len - s->pktshift;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001487 skb->data_len = skb->len;
1488 skb->truesize += skb->data_len;
1489 skb->ip_summed = CHECKSUM_UNNECESSARY;
1490 skb_record_rx_queue(skb, rxq->rspq.idx);
1491
Vipul Pandyaaf32de02013-02-12 00:36:21 +00001492 if (pkt->vlan_ex) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001493 __vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q),
1494 be16_to_cpu(pkt->vlan));
Vipul Pandyaaf32de02013-02-12 00:36:21 +00001495 rxq->stats.vlan_ex++;
1496 }
Casey Leedomc6e0d912010-06-25 12:13:28 +00001497 ret = napi_gro_frags(&rxq->rspq.napi);
1498
Casey Leedomc6e0d912010-06-25 12:13:28 +00001499 if (ret == GRO_HELD)
1500 rxq->stats.lro_pkts++;
1501 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1502 rxq->stats.lro_merged++;
1503 rxq->stats.pkts++;
1504 rxq->stats.rx_cso++;
1505}
1506
1507/**
1508 * t4vf_ethrx_handler - process an ingress ethernet packet
1509 * @rspq: the response queue that received the packet
1510 * @rsp: the response queue descriptor holding the RX_PKT message
1511 * @gl: the gather list of packet fragments
1512 *
1513 * Process an ingress ethernet packet and deliver it to the stack.
1514 */
1515int t4vf_ethrx_handler(struct sge_rspq *rspq, const __be64 *rsp,
1516 const struct pkt_gl *gl)
1517{
1518 struct sk_buff *skb;
Vipul Pandya8b9a4d52013-02-08 02:49:51 +00001519 const struct cpl_rx_pkt *pkt = (void *)rsp;
Hariprasad Shenaic3136f52014-05-07 18:01:04 +05301520 bool csum_ok = pkt->csum_calc && !pkt->err_vec &&
1521 (rspq->netdev->features & NETIF_F_RXCSUM);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001522 struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301523 struct adapter *adapter = rspq->adapter;
1524 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001525
1526 /*
1527 * If this is a good TCP packet and we have Generic Receive Offload
1528 * enabled, handle the packet in the GRO path.
1529 */
1530 if ((pkt->l2info & cpu_to_be32(RXF_TCP)) &&
1531 (rspq->netdev->features & NETIF_F_GRO) && csum_ok &&
1532 !pkt->ip_frag) {
1533 do_gro(rxq, gl, pkt);
1534 return 0;
1535 }
1536
1537 /*
Casey Leedomeb6c5032010-11-11 09:06:50 +00001538 * Convert the Packet Gather List into an skb.
Casey Leedomc6e0d912010-06-25 12:13:28 +00001539 */
Casey Leedomeb6c5032010-11-11 09:06:50 +00001540 skb = t4vf_pktgl_to_skb(gl, RX_SKB_LEN, RX_PULL_LEN);
1541 if (unlikely(!skb)) {
1542 t4vf_pktgl_free(gl);
1543 rxq->stats.rx_drops++;
1544 return 0;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001545 }
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301546 __skb_pull(skb, s->pktshift);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001547 skb->protocol = eth_type_trans(skb, rspq->netdev);
1548 skb_record_rx_queue(skb, rspq->idx);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001549 rxq->stats.pkts++;
1550
Hariprasad Shenaic3136f52014-05-07 18:01:04 +05301551 if (csum_ok && !pkt->err_vec &&
1552 (be32_to_cpu(pkt->l2info) & (RXF_UDP|RXF_TCP))) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00001553 if (!pkt->ip_frag)
1554 skb->ip_summed = CHECKSUM_UNNECESSARY;
1555 else {
1556 __sum16 c = (__force __sum16)pkt->csum;
1557 skb->csum = csum_unfold(c);
1558 skb->ip_summed = CHECKSUM_COMPLETE;
1559 }
1560 rxq->stats.rx_cso++;
1561 } else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001562 skb_checksum_none_assert(skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001563
Jiri Pirko87737662011-07-20 04:54:16 +00001564 if (pkt->vlan_ex) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00001565 rxq->stats.vlan_ex++;
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001566 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), be16_to_cpu(pkt->vlan));
Jiri Pirko87737662011-07-20 04:54:16 +00001567 }
1568
1569 netif_receive_skb(skb);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001570
1571 return 0;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001572}
1573
1574/**
1575 * is_new_response - check if a response is newly written
1576 * @rc: the response control descriptor
1577 * @rspq: the response queue
1578 *
1579 * Returns true if a response descriptor contains a yet unprocessed
1580 * response.
1581 */
1582static inline bool is_new_response(const struct rsp_ctrl *rc,
1583 const struct sge_rspq *rspq)
1584{
1585 return RSPD_GEN(rc->type_gen) == rspq->gen;
1586}
1587
1588/**
1589 * restore_rx_bufs - put back a packet's RX buffers
1590 * @gl: the packet gather list
1591 * @fl: the SGE Free List
1592 * @nfrags: how many fragments in @si
1593 *
1594 * Called when we find out that the current packet, @si, can't be
1595 * processed right away for some reason. This is a very rare event and
1596 * there's no effort to make this suspension/resumption process
1597 * particularly efficient.
1598 *
1599 * We implement the suspension by putting all of the RX buffers associated
1600 * with the current packet back on the original Free List. The buffers
1601 * have already been unmapped and are left unmapped, we mark them as
1602 * unmapped in order to prevent further unmapping attempts. (Effectively
1603 * this function undoes the series of @unmap_rx_buf calls which were done
1604 * to create the current packet's gather list.) This leaves us ready to
1605 * restart processing of the packet the next time we start processing the
1606 * RX Queue ...
1607 */
1608static void restore_rx_bufs(const struct pkt_gl *gl, struct sge_fl *fl,
1609 int frags)
1610{
1611 struct rx_sw_desc *sdesc;
1612
1613 while (frags--) {
1614 if (fl->cidx == 0)
1615 fl->cidx = fl->size - 1;
1616 else
1617 fl->cidx--;
1618 sdesc = &fl->sdesc[fl->cidx];
1619 sdesc->page = gl->frags[frags].page;
1620 sdesc->dma_addr |= RX_UNMAPPED_BUF;
1621 fl->avail++;
1622 }
1623}
1624
1625/**
1626 * rspq_next - advance to the next entry in a response queue
1627 * @rspq: the queue
1628 *
1629 * Updates the state of a response queue to advance it to the next entry.
1630 */
1631static inline void rspq_next(struct sge_rspq *rspq)
1632{
1633 rspq->cur_desc = (void *)rspq->cur_desc + rspq->iqe_len;
1634 if (unlikely(++rspq->cidx == rspq->size)) {
1635 rspq->cidx = 0;
1636 rspq->gen ^= 1;
1637 rspq->cur_desc = rspq->desc;
1638 }
1639}
1640
1641/**
1642 * process_responses - process responses from an SGE response queue
1643 * @rspq: the ingress response queue to process
1644 * @budget: how many responses can be processed in this round
1645 *
1646 * Process responses from a Scatter Gather Engine response queue up to
1647 * the supplied budget. Responses include received packets as well as
1648 * control messages from firmware or hardware.
1649 *
1650 * Additionally choose the interrupt holdoff time for the next interrupt
1651 * on this queue. If the system is under memory shortage use a fairly
1652 * long delay to help recovery.
1653 */
Sachin Kamat8a67d1c2013-09-18 09:00:01 +05301654static int process_responses(struct sge_rspq *rspq, int budget)
Casey Leedomc6e0d912010-06-25 12:13:28 +00001655{
1656 struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301657 struct adapter *adapter = rspq->adapter;
1658 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001659 int budget_left = budget;
1660
1661 while (likely(budget_left)) {
1662 int ret, rsp_type;
1663 const struct rsp_ctrl *rc;
1664
1665 rc = (void *)rspq->cur_desc + (rspq->iqe_len - sizeof(*rc));
1666 if (!is_new_response(rc, rspq))
1667 break;
1668
1669 /*
1670 * Figure out what kind of response we've received from the
1671 * SGE.
1672 */
1673 rmb();
1674 rsp_type = RSPD_TYPE(rc->type_gen);
1675 if (likely(rsp_type == RSP_TYPE_FLBUF)) {
Ian Campbella0006a82011-10-19 23:01:47 +00001676 struct page_frag *fp;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001677 struct pkt_gl gl;
1678 const struct rx_sw_desc *sdesc;
1679 u32 bufsz, frag;
1680 u32 len = be32_to_cpu(rc->pldbuflen_qid);
1681
1682 /*
1683 * If we get a "new buffer" message from the SGE we
1684 * need to move on to the next Free List buffer.
1685 */
1686 if (len & RSPD_NEWBUF) {
1687 /*
1688 * We get one "new buffer" message when we
1689 * first start up a queue so we need to ignore
1690 * it when our offset into the buffer is 0.
1691 */
1692 if (likely(rspq->offset > 0)) {
1693 free_rx_bufs(rspq->adapter, &rxq->fl,
1694 1);
1695 rspq->offset = 0;
1696 }
1697 len = RSPD_LEN(len);
1698 }
Casey Leedomb94e72e2010-11-11 09:06:49 +00001699 gl.tot_len = len;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001700
1701 /*
1702 * Gather packet fragments.
1703 */
1704 for (frag = 0, fp = gl.frags; /**/; frag++, fp++) {
1705 BUG_ON(frag >= MAX_SKB_FRAGS);
1706 BUG_ON(rxq->fl.avail == 0);
1707 sdesc = &rxq->fl.sdesc[rxq->fl.cidx];
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301708 bufsz = get_buf_size(adapter, sdesc);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001709 fp->page = sdesc->page;
Ian Campbella0006a82011-10-19 23:01:47 +00001710 fp->offset = rspq->offset;
1711 fp->size = min(bufsz, len);
1712 len -= fp->size;
Casey Leedomc6e0d912010-06-25 12:13:28 +00001713 if (!len)
1714 break;
1715 unmap_rx_buf(rspq->adapter, &rxq->fl);
1716 }
1717 gl.nfrags = frag+1;
1718
1719 /*
1720 * Last buffer remains mapped so explicitly make it
1721 * coherent for CPU access and start preloading first
1722 * cache line ...
1723 */
1724 dma_sync_single_for_cpu(rspq->adapter->pdev_dev,
1725 get_buf_addr(sdesc),
Ian Campbella0006a82011-10-19 23:01:47 +00001726 fp->size, DMA_FROM_DEVICE);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001727 gl.va = (page_address(gl.frags[0].page) +
Ian Campbella0006a82011-10-19 23:01:47 +00001728 gl.frags[0].offset);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001729 prefetch(gl.va);
1730
1731 /*
1732 * Hand the new ingress packet to the handler for
1733 * this Response Queue.
1734 */
1735 ret = rspq->handler(rspq, rspq->cur_desc, &gl);
1736 if (likely(ret == 0))
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301737 rspq->offset += ALIGN(fp->size, s->fl_align);
Casey Leedomc6e0d912010-06-25 12:13:28 +00001738 else
1739 restore_rx_bufs(&gl, &rxq->fl, frag);
1740 } else if (likely(rsp_type == RSP_TYPE_CPL)) {
1741 ret = rspq->handler(rspq, rspq->cur_desc, NULL);
1742 } else {
1743 WARN_ON(rsp_type > RSP_TYPE_CPL);
1744 ret = 0;
1745 }
1746
1747 if (unlikely(ret)) {
1748 /*
1749 * Couldn't process descriptor, back off for recovery.
1750 * We use the SGE's last timer which has the longest
1751 * interrupt coalescing value ...
1752 */
1753 const int NOMEM_TIMER_IDX = SGE_NTIMERS-1;
1754 rspq->next_intr_params =
1755 QINTR_TIMER_IDX(NOMEM_TIMER_IDX);
1756 break;
1757 }
1758
1759 rspq_next(rspq);
1760 budget_left--;
1761 }
1762
1763 /*
1764 * If this is a Response Queue with an associated Free List and
1765 * at least two Egress Queue units available in the Free List
1766 * for new buffer pointers, refill the Free List.
1767 */
1768 if (rspq->offset >= 0 &&
1769 rxq->fl.size - rxq->fl.avail >= 2*FL_PER_EQ_UNIT)
1770 __refill_fl(rspq->adapter, &rxq->fl);
1771 return budget - budget_left;
1772}
1773
1774/**
1775 * napi_rx_handler - the NAPI handler for RX processing
1776 * @napi: the napi instance
1777 * @budget: how many packets we can process in this round
1778 *
1779 * Handler for new data events when using NAPI. This does not need any
1780 * locking or protection from interrupts as data interrupts are off at
1781 * this point and other adapter interrupts do not interfere (the latter
1782 * in not a concern at all with MSI-X as non-data interrupts then have
1783 * a separate handler).
1784 */
1785static int napi_rx_handler(struct napi_struct *napi, int budget)
1786{
1787 unsigned int intr_params;
1788 struct sge_rspq *rspq = container_of(napi, struct sge_rspq, napi);
1789 int work_done = process_responses(rspq, budget);
1790
1791 if (likely(work_done < budget)) {
1792 napi_complete(napi);
1793 intr_params = rspq->next_intr_params;
1794 rspq->next_intr_params = rspq->intr_params;
1795 } else
1796 intr_params = QINTR_TIMER_IDX(SGE_TIMER_UPD_CIDX);
1797
Casey Leedom68dc9d32010-07-08 10:05:48 -07001798 if (unlikely(work_done == 0))
1799 rspq->unhandled_irqs++;
1800
Casey Leedomc6e0d912010-06-25 12:13:28 +00001801 t4_write_reg(rspq->adapter,
1802 T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
1803 CIDXINC(work_done) |
1804 INGRESSQID((u32)rspq->cntxt_id) |
1805 SEINTARM(intr_params));
1806 return work_done;
1807}
1808
1809/*
1810 * The MSI-X interrupt handler for an SGE response queue for the NAPI case
1811 * (i.e., response queue serviced by NAPI polling).
1812 */
1813irqreturn_t t4vf_sge_intr_msix(int irq, void *cookie)
1814{
1815 struct sge_rspq *rspq = cookie;
1816
1817 napi_schedule(&rspq->napi);
1818 return IRQ_HANDLED;
1819}
1820
1821/*
1822 * Process the indirect interrupt entries in the interrupt queue and kick off
1823 * NAPI for each queue that has generated an entry.
1824 */
1825static unsigned int process_intrq(struct adapter *adapter)
1826{
1827 struct sge *s = &adapter->sge;
1828 struct sge_rspq *intrq = &s->intrq;
1829 unsigned int work_done;
1830
1831 spin_lock(&adapter->sge.intrq_lock);
1832 for (work_done = 0; ; work_done++) {
1833 const struct rsp_ctrl *rc;
1834 unsigned int qid, iq_idx;
1835 struct sge_rspq *rspq;
1836
1837 /*
1838 * Grab the next response from the interrupt queue and bail
1839 * out if it's not a new response.
1840 */
1841 rc = (void *)intrq->cur_desc + (intrq->iqe_len - sizeof(*rc));
1842 if (!is_new_response(rc, intrq))
1843 break;
1844
1845 /*
1846 * If the response isn't a forwarded interrupt message issue a
1847 * error and go on to the next response message. This should
1848 * never happen ...
1849 */
1850 rmb();
1851 if (unlikely(RSPD_TYPE(rc->type_gen) != RSP_TYPE_INTR)) {
1852 dev_err(adapter->pdev_dev,
1853 "Unexpected INTRQ response type %d\n",
1854 RSPD_TYPE(rc->type_gen));
1855 continue;
1856 }
1857
1858 /*
1859 * Extract the Queue ID from the interrupt message and perform
1860 * sanity checking to make sure it really refers to one of our
1861 * Ingress Queues which is active and matches the queue's ID.
1862 * None of these error conditions should ever happen so we may
1863 * want to either make them fatal and/or conditionalized under
1864 * DEBUG.
1865 */
1866 qid = RSPD_QID(be32_to_cpu(rc->pldbuflen_qid));
1867 iq_idx = IQ_IDX(s, qid);
1868 if (unlikely(iq_idx >= MAX_INGQ)) {
1869 dev_err(adapter->pdev_dev,
1870 "Ingress QID %d out of range\n", qid);
1871 continue;
1872 }
1873 rspq = s->ingr_map[iq_idx];
1874 if (unlikely(rspq == NULL)) {
1875 dev_err(adapter->pdev_dev,
1876 "Ingress QID %d RSPQ=NULL\n", qid);
1877 continue;
1878 }
1879 if (unlikely(rspq->abs_id != qid)) {
1880 dev_err(adapter->pdev_dev,
1881 "Ingress QID %d refers to RSPQ %d\n",
1882 qid, rspq->abs_id);
1883 continue;
1884 }
1885
1886 /*
1887 * Schedule NAPI processing on the indicated Response Queue
1888 * and move on to the next entry in the Forwarded Interrupt
1889 * Queue.
1890 */
1891 napi_schedule(&rspq->napi);
1892 rspq_next(intrq);
1893 }
1894
1895 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
1896 CIDXINC(work_done) |
1897 INGRESSQID(intrq->cntxt_id) |
1898 SEINTARM(intrq->intr_params));
1899
1900 spin_unlock(&adapter->sge.intrq_lock);
1901
1902 return work_done;
1903}
1904
1905/*
1906 * The MSI interrupt handler handles data events from SGE response queues as
1907 * well as error and other async events as they all use the same MSI vector.
1908 */
Sachin Kamat8a67d1c2013-09-18 09:00:01 +05301909static irqreturn_t t4vf_intr_msi(int irq, void *cookie)
Casey Leedomc6e0d912010-06-25 12:13:28 +00001910{
1911 struct adapter *adapter = cookie;
1912
1913 process_intrq(adapter);
1914 return IRQ_HANDLED;
1915}
1916
1917/**
1918 * t4vf_intr_handler - select the top-level interrupt handler
1919 * @adapter: the adapter
1920 *
1921 * Selects the top-level interrupt handler based on the type of interrupts
1922 * (MSI-X or MSI).
1923 */
1924irq_handler_t t4vf_intr_handler(struct adapter *adapter)
1925{
1926 BUG_ON((adapter->flags & (USING_MSIX|USING_MSI)) == 0);
1927 if (adapter->flags & USING_MSIX)
1928 return t4vf_sge_intr_msix;
1929 else
1930 return t4vf_intr_msi;
1931}
1932
1933/**
1934 * sge_rx_timer_cb - perform periodic maintenance of SGE RX queues
1935 * @data: the adapter
1936 *
1937 * Runs periodically from a timer to perform maintenance of SGE RX queues.
1938 *
1939 * a) Replenishes RX queues that have run out due to memory shortage.
1940 * Normally new RX buffers are added when existing ones are consumed but
1941 * when out of memory a queue can become empty. We schedule NAPI to do
1942 * the actual refill.
1943 */
1944static void sge_rx_timer_cb(unsigned long data)
1945{
1946 struct adapter *adapter = (struct adapter *)data;
1947 struct sge *s = &adapter->sge;
1948 unsigned int i;
1949
1950 /*
1951 * Scan the "Starving Free Lists" flag array looking for any Free
1952 * Lists in need of more free buffers. If we find one and it's not
1953 * being actively polled, then bump its "starving" counter and attempt
1954 * to refill it. If we're successful in adding enough buffers to push
1955 * the Free List over the starving threshold, then we can clear its
1956 * "starving" status.
1957 */
1958 for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++) {
1959 unsigned long m;
1960
1961 for (m = s->starving_fl[i]; m; m &= m - 1) {
1962 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
1963 struct sge_fl *fl = s->egr_map[id];
1964
1965 clear_bit(id, s->starving_fl);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001966 smp_mb__after_atomic();
Casey Leedomc6e0d912010-06-25 12:13:28 +00001967
1968 /*
1969 * Since we are accessing fl without a lock there's a
1970 * small probability of a false positive where we
1971 * schedule napi but the FL is no longer starving.
1972 * No biggie.
1973 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05301974 if (fl_starving(adapter, fl)) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00001975 struct sge_eth_rxq *rxq;
1976
1977 rxq = container_of(fl, struct sge_eth_rxq, fl);
1978 if (napi_reschedule(&rxq->rspq.napi))
1979 fl->starving++;
1980 else
1981 set_bit(id, s->starving_fl);
1982 }
1983 }
1984 }
1985
1986 /*
1987 * Reschedule the next scan for starving Free Lists ...
1988 */
1989 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
1990}
1991
1992/**
1993 * sge_tx_timer_cb - perform periodic maintenance of SGE Tx queues
1994 * @data: the adapter
1995 *
1996 * Runs periodically from a timer to perform maintenance of SGE TX queues.
1997 *
1998 * b) Reclaims completed Tx packets for the Ethernet queues. Normally
1999 * packets are cleaned up by new Tx packets, this timer cleans up packets
2000 * when no new packets are being submitted. This is essential for pktgen,
2001 * at least.
2002 */
2003static void sge_tx_timer_cb(unsigned long data)
2004{
2005 struct adapter *adapter = (struct adapter *)data;
2006 struct sge *s = &adapter->sge;
2007 unsigned int i, budget;
2008
2009 budget = MAX_TIMER_TX_RECLAIM;
2010 i = s->ethtxq_rover;
2011 do {
2012 struct sge_eth_txq *txq = &s->ethtxq[i];
2013
2014 if (reclaimable(&txq->q) && __netif_tx_trylock(txq->txq)) {
2015 int avail = reclaimable(&txq->q);
2016
2017 if (avail > budget)
2018 avail = budget;
2019
2020 free_tx_desc(adapter, &txq->q, avail, true);
2021 txq->q.in_use -= avail;
2022 __netif_tx_unlock(txq->txq);
2023
2024 budget -= avail;
2025 if (!budget)
2026 break;
2027 }
2028
2029 i++;
2030 if (i >= s->ethqsets)
2031 i = 0;
2032 } while (i != s->ethtxq_rover);
2033 s->ethtxq_rover = i;
2034
2035 /*
2036 * If we found too many reclaimable packets schedule a timer in the
2037 * near future to continue where we left off. Otherwise the next timer
2038 * will be at its normal interval.
2039 */
2040 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
2041}
2042
2043/**
2044 * t4vf_sge_alloc_rxq - allocate an SGE RX Queue
2045 * @adapter: the adapter
2046 * @rspq: pointer to to the new rxq's Response Queue to be filled in
2047 * @iqasynch: if 0, a normal rspq; if 1, an asynchronous event queue
2048 * @dev: the network device associated with the new rspq
2049 * @intr_dest: MSI-X vector index (overriden in MSI mode)
2050 * @fl: pointer to the new rxq's Free List to be filled in
2051 * @hnd: the interrupt handler to invoke for the rspq
2052 */
2053int t4vf_sge_alloc_rxq(struct adapter *adapter, struct sge_rspq *rspq,
2054 bool iqasynch, struct net_device *dev,
2055 int intr_dest,
2056 struct sge_fl *fl, rspq_handler_t hnd)
2057{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302058 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00002059 struct port_info *pi = netdev_priv(dev);
2060 struct fw_iq_cmd cmd, rpl;
2061 int ret, iqandst, flsz = 0;
2062
2063 /*
2064 * If we're using MSI interrupts and we're not initializing the
2065 * Forwarded Interrupt Queue itself, then set up this queue for
2066 * indirect interrupts to the Forwarded Interrupt Queue. Obviously
2067 * the Forwarded Interrupt Queue must be set up before any other
2068 * ingress queue ...
2069 */
2070 if ((adapter->flags & USING_MSI) && rspq != &adapter->sge.intrq) {
2071 iqandst = SGE_INTRDST_IQ;
2072 intr_dest = adapter->sge.intrq.abs_id;
2073 } else
2074 iqandst = SGE_INTRDST_PCI;
2075
2076 /*
2077 * Allocate the hardware ring for the Response Queue. The size needs
2078 * to be a multiple of 16 which includes the mandatory status entry
2079 * (regardless of whether the Status Page capabilities are enabled or
2080 * not).
2081 */
2082 rspq->size = roundup(rspq->size, 16);
2083 rspq->desc = alloc_ring(adapter->pdev_dev, rspq->size, rspq->iqe_len,
2084 0, &rspq->phys_addr, NULL, 0);
2085 if (!rspq->desc)
2086 return -ENOMEM;
2087
2088 /*
2089 * Fill in the Ingress Queue Command. Note: Ideally this code would
2090 * be in t4vf_hw.c but there are so many parameters and dependencies
2091 * on our Linux SGE state that we would end up having to pass tons of
2092 * parameters. We'll have to think about how this might be migrated
2093 * into OS-independent common code ...
2094 */
2095 memset(&cmd, 0, sizeof(cmd));
2096 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_IQ_CMD) |
2097 FW_CMD_REQUEST |
2098 FW_CMD_WRITE |
2099 FW_CMD_EXEC);
2100 cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_ALLOC |
2101 FW_IQ_CMD_IQSTART(1) |
2102 FW_LEN16(cmd));
2103 cmd.type_to_iqandstindex =
2104 cpu_to_be32(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
2105 FW_IQ_CMD_IQASYNCH(iqasynch) |
2106 FW_IQ_CMD_VIID(pi->viid) |
2107 FW_IQ_CMD_IQANDST(iqandst) |
2108 FW_IQ_CMD_IQANUS(1) |
2109 FW_IQ_CMD_IQANUD(SGE_UPDATEDEL_INTR) |
2110 FW_IQ_CMD_IQANDSTINDEX(intr_dest));
2111 cmd.iqdroprss_to_iqesize =
2112 cpu_to_be16(FW_IQ_CMD_IQPCIECH(pi->port_id) |
2113 FW_IQ_CMD_IQGTSMODE |
2114 FW_IQ_CMD_IQINTCNTTHRESH(rspq->pktcnt_idx) |
2115 FW_IQ_CMD_IQESIZE(ilog2(rspq->iqe_len) - 4));
2116 cmd.iqsize = cpu_to_be16(rspq->size);
2117 cmd.iqaddr = cpu_to_be64(rspq->phys_addr);
2118
2119 if (fl) {
2120 /*
2121 * Allocate the ring for the hardware free list (with space
2122 * for its status page) along with the associated software
2123 * descriptor ring. The free list size needs to be a multiple
2124 * of the Egress Queue Unit.
2125 */
2126 fl->size = roundup(fl->size, FL_PER_EQ_UNIT);
2127 fl->desc = alloc_ring(adapter->pdev_dev, fl->size,
2128 sizeof(__be64), sizeof(struct rx_sw_desc),
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302129 &fl->addr, &fl->sdesc, s->stat_len);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002130 if (!fl->desc) {
2131 ret = -ENOMEM;
2132 goto err;
2133 }
2134
2135 /*
2136 * Calculate the size of the hardware free list ring plus
Casey Leedomcaedda32010-11-11 09:30:40 +00002137 * Status Page (which the SGE will place after the end of the
Casey Leedomc6e0d912010-06-25 12:13:28 +00002138 * free list ring) in Egress Queue Units.
2139 */
2140 flsz = (fl->size / FL_PER_EQ_UNIT +
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302141 s->stat_len / EQ_UNIT);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002142
2143 /*
2144 * Fill in all the relevant firmware Ingress Queue Command
2145 * fields for the free list.
2146 */
2147 cmd.iqns_to_fl0congen =
2148 cpu_to_be32(
2149 FW_IQ_CMD_FL0HOSTFCMODE(SGE_HOSTFCMODE_NONE) |
Naresh Kumar Innace91a922012-11-15 22:41:17 +05302150 FW_IQ_CMD_FL0PACKEN(1) |
2151 FW_IQ_CMD_FL0PADEN(1));
Casey Leedomc6e0d912010-06-25 12:13:28 +00002152 cmd.fl0dcaen_to_fl0cidxfthresh =
2153 cpu_to_be16(
2154 FW_IQ_CMD_FL0FBMIN(SGE_FETCHBURSTMIN_64B) |
2155 FW_IQ_CMD_FL0FBMAX(SGE_FETCHBURSTMAX_512B));
2156 cmd.fl0size = cpu_to_be16(flsz);
2157 cmd.fl0addr = cpu_to_be64(fl->addr);
2158 }
2159
2160 /*
2161 * Issue the firmware Ingress Queue Command and extract the results if
2162 * it completes successfully.
2163 */
2164 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
2165 if (ret)
2166 goto err;
2167
2168 netif_napi_add(dev, &rspq->napi, napi_rx_handler, 64);
2169 rspq->cur_desc = rspq->desc;
2170 rspq->cidx = 0;
2171 rspq->gen = 1;
2172 rspq->next_intr_params = rspq->intr_params;
2173 rspq->cntxt_id = be16_to_cpu(rpl.iqid);
2174 rspq->abs_id = be16_to_cpu(rpl.physiqid);
2175 rspq->size--; /* subtract status entry */
2176 rspq->adapter = adapter;
2177 rspq->netdev = dev;
2178 rspq->handler = hnd;
2179
2180 /* set offset to -1 to distinguish ingress queues without FL */
2181 rspq->offset = fl ? 0 : -1;
2182
2183 if (fl) {
2184 fl->cntxt_id = be16_to_cpu(rpl.fl0id);
2185 fl->avail = 0;
2186 fl->pend_cred = 0;
2187 fl->pidx = 0;
2188 fl->cidx = 0;
2189 fl->alloc_failed = 0;
2190 fl->large_alloc_failed = 0;
2191 fl->starving = 0;
2192 refill_fl(adapter, fl, fl_cap(fl), GFP_KERNEL);
2193 }
2194
2195 return 0;
2196
2197err:
2198 /*
2199 * An error occurred. Clean up our partial allocation state and
2200 * return the error.
2201 */
2202 if (rspq->desc) {
2203 dma_free_coherent(adapter->pdev_dev, rspq->size * rspq->iqe_len,
2204 rspq->desc, rspq->phys_addr);
2205 rspq->desc = NULL;
2206 }
2207 if (fl && fl->desc) {
2208 kfree(fl->sdesc);
2209 fl->sdesc = NULL;
2210 dma_free_coherent(adapter->pdev_dev, flsz * EQ_UNIT,
2211 fl->desc, fl->addr);
2212 fl->desc = NULL;
2213 }
2214 return ret;
2215}
2216
2217/**
2218 * t4vf_sge_alloc_eth_txq - allocate an SGE Ethernet TX Queue
2219 * @adapter: the adapter
2220 * @txq: pointer to the new txq to be filled in
2221 * @devq: the network TX queue associated with the new txq
2222 * @iqid: the relative ingress queue ID to which events relating to
2223 * the new txq should be directed
2224 */
2225int t4vf_sge_alloc_eth_txq(struct adapter *adapter, struct sge_eth_txq *txq,
2226 struct net_device *dev, struct netdev_queue *devq,
2227 unsigned int iqid)
2228{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302229 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00002230 int ret, nentries;
2231 struct fw_eq_eth_cmd cmd, rpl;
2232 struct port_info *pi = netdev_priv(dev);
2233
2234 /*
Casey Leedomcaedda32010-11-11 09:30:40 +00002235 * Calculate the size of the hardware TX Queue (including the Status
2236 * Page on the end of the TX Queue) in units of TX Descriptors.
Casey Leedomc6e0d912010-06-25 12:13:28 +00002237 */
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302238 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002239
2240 /*
2241 * Allocate the hardware ring for the TX ring (with space for its
2242 * status page) along with the associated software descriptor ring.
2243 */
2244 txq->q.desc = alloc_ring(adapter->pdev_dev, txq->q.size,
2245 sizeof(struct tx_desc),
2246 sizeof(struct tx_sw_desc),
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302247 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002248 if (!txq->q.desc)
2249 return -ENOMEM;
2250
2251 /*
2252 * Fill in the Egress Queue Command. Note: As with the direct use of
2253 * the firmware Ingress Queue COmmand above in our RXQ allocation
2254 * routine, ideally, this code would be in t4vf_hw.c. Again, we'll
2255 * have to see if there's some reasonable way to parameterize it
2256 * into the common code ...
2257 */
2258 memset(&cmd, 0, sizeof(cmd));
2259 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_EQ_ETH_CMD) |
2260 FW_CMD_REQUEST |
2261 FW_CMD_WRITE |
2262 FW_CMD_EXEC);
2263 cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_ALLOC |
2264 FW_EQ_ETH_CMD_EQSTART |
2265 FW_LEN16(cmd));
Hariprasad Shenai08f1a1b2014-08-21 17:04:46 +05302266 cmd.viid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_AUTOEQUEQE |
2267 FW_EQ_ETH_CMD_VIID(pi->viid));
Casey Leedomc6e0d912010-06-25 12:13:28 +00002268 cmd.fetchszm_to_iqid =
2269 cpu_to_be32(FW_EQ_ETH_CMD_HOSTFCMODE(SGE_HOSTFCMODE_STPG) |
2270 FW_EQ_ETH_CMD_PCIECHN(pi->port_id) |
2271 FW_EQ_ETH_CMD_IQID(iqid));
2272 cmd.dcaen_to_eqsize =
2273 cpu_to_be32(FW_EQ_ETH_CMD_FBMIN(SGE_FETCHBURSTMIN_64B) |
2274 FW_EQ_ETH_CMD_FBMAX(SGE_FETCHBURSTMAX_512B) |
2275 FW_EQ_ETH_CMD_CIDXFTHRESH(SGE_CIDXFLUSHTHRESH_32) |
2276 FW_EQ_ETH_CMD_EQSIZE(nentries));
2277 cmd.eqaddr = cpu_to_be64(txq->q.phys_addr);
2278
2279 /*
2280 * Issue the firmware Egress Queue Command and extract the results if
2281 * it completes successfully.
2282 */
2283 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
2284 if (ret) {
2285 /*
2286 * The girmware Ingress Queue Command failed for some reason.
2287 * Free up our partial allocation state and return the error.
2288 */
2289 kfree(txq->q.sdesc);
2290 txq->q.sdesc = NULL;
2291 dma_free_coherent(adapter->pdev_dev,
2292 nentries * sizeof(struct tx_desc),
2293 txq->q.desc, txq->q.phys_addr);
2294 txq->q.desc = NULL;
2295 return ret;
2296 }
2297
2298 txq->q.in_use = 0;
2299 txq->q.cidx = 0;
2300 txq->q.pidx = 0;
2301 txq->q.stat = (void *)&txq->q.desc[txq->q.size];
2302 txq->q.cntxt_id = FW_EQ_ETH_CMD_EQID_GET(be32_to_cpu(rpl.eqid_pkd));
2303 txq->q.abs_id =
2304 FW_EQ_ETH_CMD_PHYSEQID_GET(be32_to_cpu(rpl.physeqid_pkd));
2305 txq->txq = devq;
2306 txq->tso = 0;
2307 txq->tx_cso = 0;
2308 txq->vlan_ins = 0;
2309 txq->q.stops = 0;
2310 txq->q.restarts = 0;
2311 txq->mapping_err = 0;
2312 return 0;
2313}
2314
2315/*
2316 * Free the DMA map resources associated with a TX queue.
2317 */
2318static void free_txq(struct adapter *adapter, struct sge_txq *tq)
2319{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302320 struct sge *s = &adapter->sge;
2321
Casey Leedomc6e0d912010-06-25 12:13:28 +00002322 dma_free_coherent(adapter->pdev_dev,
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302323 tq->size * sizeof(*tq->desc) + s->stat_len,
Casey Leedomc6e0d912010-06-25 12:13:28 +00002324 tq->desc, tq->phys_addr);
2325 tq->cntxt_id = 0;
2326 tq->sdesc = NULL;
2327 tq->desc = NULL;
2328}
2329
2330/*
2331 * Free the resources associated with a response queue (possibly including a
2332 * free list).
2333 */
2334static void free_rspq_fl(struct adapter *adapter, struct sge_rspq *rspq,
2335 struct sge_fl *fl)
2336{
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302337 struct sge *s = &adapter->sge;
Casey Leedomc6e0d912010-06-25 12:13:28 +00002338 unsigned int flid = fl ? fl->cntxt_id : 0xffff;
2339
2340 t4vf_iq_free(adapter, FW_IQ_TYPE_FL_INT_CAP,
2341 rspq->cntxt_id, flid, 0xffff);
2342 dma_free_coherent(adapter->pdev_dev, (rspq->size + 1) * rspq->iqe_len,
2343 rspq->desc, rspq->phys_addr);
2344 netif_napi_del(&rspq->napi);
2345 rspq->netdev = NULL;
2346 rspq->cntxt_id = 0;
2347 rspq->abs_id = 0;
2348 rspq->desc = NULL;
2349
2350 if (fl) {
2351 free_rx_bufs(adapter, fl, fl->avail);
2352 dma_free_coherent(adapter->pdev_dev,
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302353 fl->size * sizeof(*fl->desc) + s->stat_len,
Casey Leedomc6e0d912010-06-25 12:13:28 +00002354 fl->desc, fl->addr);
2355 kfree(fl->sdesc);
2356 fl->sdesc = NULL;
2357 fl->cntxt_id = 0;
2358 fl->desc = NULL;
2359 }
2360}
2361
2362/**
2363 * t4vf_free_sge_resources - free SGE resources
2364 * @adapter: the adapter
2365 *
2366 * Frees resources used by the SGE queue sets.
2367 */
2368void t4vf_free_sge_resources(struct adapter *adapter)
2369{
2370 struct sge *s = &adapter->sge;
2371 struct sge_eth_rxq *rxq = s->ethrxq;
2372 struct sge_eth_txq *txq = s->ethtxq;
2373 struct sge_rspq *evtq = &s->fw_evtq;
2374 struct sge_rspq *intrq = &s->intrq;
2375 int qs;
2376
Casey Leedomb97d13a2010-07-15 22:47:06 -07002377 for (qs = 0; qs < adapter->sge.ethqsets; qs++, rxq++, txq++) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00002378 if (rxq->rspq.desc)
2379 free_rspq_fl(adapter, &rxq->rspq, &rxq->fl);
2380 if (txq->q.desc) {
2381 t4vf_eth_eq_free(adapter, txq->q.cntxt_id);
2382 free_tx_desc(adapter, &txq->q, txq->q.in_use, true);
2383 kfree(txq->q.sdesc);
2384 free_txq(adapter, &txq->q);
2385 }
2386 }
2387 if (evtq->desc)
2388 free_rspq_fl(adapter, evtq, NULL);
2389 if (intrq->desc)
2390 free_rspq_fl(adapter, intrq, NULL);
2391}
2392
2393/**
2394 * t4vf_sge_start - enable SGE operation
2395 * @adapter: the adapter
2396 *
2397 * Start tasklets and timers associated with the DMA engine.
2398 */
2399void t4vf_sge_start(struct adapter *adapter)
2400{
2401 adapter->sge.ethtxq_rover = 0;
2402 mod_timer(&adapter->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2403 mod_timer(&adapter->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2404}
2405
2406/**
2407 * t4vf_sge_stop - disable SGE operation
2408 * @adapter: the adapter
2409 *
2410 * Stop tasklets and timers associated with the DMA engine. Note that
2411 * this is effective only if measures have been taken to disable any HW
2412 * events that may restart them.
2413 */
2414void t4vf_sge_stop(struct adapter *adapter)
2415{
2416 struct sge *s = &adapter->sge;
2417
2418 if (s->rx_timer.function)
2419 del_timer_sync(&s->rx_timer);
2420 if (s->tx_timer.function)
2421 del_timer_sync(&s->tx_timer);
2422}
2423
2424/**
2425 * t4vf_sge_init - initialize SGE
2426 * @adapter: the adapter
2427 *
2428 * Performs SGE initialization needed every time after a chip reset.
2429 * We do not initialize any of the queue sets here, instead the driver
2430 * top-level must request those individually. We also do not enable DMA
2431 * here, that should be done after the queues have been set up.
2432 */
2433int t4vf_sge_init(struct adapter *adapter)
2434{
2435 struct sge_params *sge_params = &adapter->params.sge;
2436 u32 fl0 = sge_params->sge_fl_buffer_size[0];
2437 u32 fl1 = sge_params->sge_fl_buffer_size[1];
2438 struct sge *s = &adapter->sge;
2439
2440 /*
2441 * Start by vetting the basic SGE parameters which have been set up by
2442 * the Physical Function Driver. Ideally we should be able to deal
2443 * with _any_ configuration. Practice is different ...
2444 */
2445 if (fl0 != PAGE_SIZE || (fl1 != 0 && fl1 <= fl0)) {
2446 dev_err(adapter->pdev_dev, "bad SGE FL buffer sizes [%d, %d]\n",
2447 fl0, fl1);
2448 return -EINVAL;
2449 }
Vipul Pandya52367a72012-09-26 02:39:38 +00002450 if ((sge_params->sge_control & RXPKTCPLMODE_MASK) == 0) {
Casey Leedomc6e0d912010-06-25 12:13:28 +00002451 dev_err(adapter->pdev_dev, "bad SGE CPL MODE\n");
2452 return -EINVAL;
2453 }
2454
2455 /*
2456 * Now translate the adapter parameters into our internal forms.
2457 */
2458 if (fl1)
Hariprasad Shenai65f6ecc2014-11-07 17:06:29 +05302459 s->fl_pg_order = ilog2(fl1) - PAGE_SHIFT;
2460 s->stat_len = ((sge_params->sge_control & EGRSTATUSPAGESIZE_MASK)
2461 ? 128 : 64);
2462 s->pktshift = PKTSHIFT_GET(sge_params->sge_control);
2463 s->fl_align = 1 << (INGPADBOUNDARY_GET(sge_params->sge_control) +
2464 SGE_INGPADBOUNDARY_SHIFT);
Casey Leedomc6e0d912010-06-25 12:13:28 +00002465
2466 /*
2467 * Set up tasklet timers.
2468 */
2469 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adapter);
2470 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adapter);
2471
2472 /*
2473 * Initialize Forwarded Interrupt Queue lock.
2474 */
2475 spin_lock_init(&s->intrq_lock);
2476
2477 return 0;
2478}