2 * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
5 * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
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.
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
36 #include <linux/skbuff.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/if_vlan.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/prefetch.h>
46 #include "t4vf_common.h"
47 #include "t4vf_defs.h"
49 #include "../cxgb4/t4_regs.h"
50 #include "../cxgb4/t4fw_api.h"
51 #include "../cxgb4/t4_msg.h"
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.
64 EQ_UNIT = SGE_EQ_IDXSIZE,
65 FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
66 TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
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.
77 * Max number of Rx buffers we replenish at a time. Again keep this
78 * modest, allocating buffers isn't cheap either.
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
87 RX_QCHECK_PERIOD = (HZ / 2),
90 * Period of the TX queue check timer and the maximum number of TX
91 * descriptors to be reclaimed by the TX timer.
93 TX_QCHECK_PERIOD = (HZ / 2),
94 MAX_TIMER_TX_RECLAIM = 100,
97 * Suspend an Ethernet TX queue with fewer available descriptors than
98 * this. We always want to have room for a maximum sized packet:
99 * inline immediate data + MAX_SKB_FRAGS. This is the same as
100 * calc_tx_flits() for a TSO packet with nr_frags == MAX_SKB_FRAGS
101 * (see that function and its helpers for a description of the
104 ETHTXQ_MAX_FRAGS = MAX_SKB_FRAGS + 1,
105 ETHTXQ_MAX_SGL_LEN = ((3 * (ETHTXQ_MAX_FRAGS-1))/2 +
106 ((ETHTXQ_MAX_FRAGS-1) & 1) +
108 ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) +
109 sizeof(struct cpl_tx_pkt_lso_core) +
110 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64),
111 ETHTXQ_MAX_FLITS = ETHTXQ_MAX_SGL_LEN + ETHTXQ_MAX_HDR,
113 ETHTXQ_STOP_THRES = 1 + DIV_ROUND_UP(ETHTXQ_MAX_FLITS, TXD_PER_EQ_UNIT),
116 * Max TX descriptor space we allow for an Ethernet packet to be
117 * inlined into a WR. This is limited by the maximum value which
118 * we can specify for immediate data in the firmware Ethernet TX
121 MAX_IMM_TX_PKT_LEN = FW_WR_IMMDLEN_M,
124 * Max size of a WR sent through a control TX queue.
126 MAX_CTRL_WR_LEN = 256,
129 * Maximum amount of data which we'll ever need to inline into a
130 * TX ring: max(MAX_IMM_TX_PKT_LEN, MAX_CTRL_WR_LEN).
132 MAX_IMM_TX_LEN = (MAX_IMM_TX_PKT_LEN > MAX_CTRL_WR_LEN
137 * For incoming packets less than RX_COPY_THRES, we copy the data into
138 * an skb rather than referencing the data. We allocate enough
139 * in-line room in skb's to accommodate pulling in RX_PULL_LEN bytes
140 * of the data (header).
146 * Main body length for sk_buffs used for RX Ethernet packets with
147 * fragments. Should be >= RX_PULL_LEN but possibly bigger to give
148 * pskb_may_pull() some room.
154 * Software state per TX descriptor.
157 struct sk_buff *skb; /* socket buffer of TX data source */
158 struct ulptx_sgl *sgl; /* scatter/gather list in TX Queue */
162 * Software state per RX Free List descriptor. We keep track of the allocated
163 * FL page, its size, and its PCI DMA address (if the page is mapped). The FL
164 * page size and its PCI DMA mapped state are stored in the low bits of the
165 * PCI DMA address as per below.
168 struct page *page; /* Free List page buffer */
169 dma_addr_t dma_addr; /* PCI DMA address (if mapped) */
170 /* and flags (see below) */
174 * The low bits of rx_sw_desc.dma_addr have special meaning. Note that the
175 * SGE also uses the low 4 bits to determine the size of the buffer. It uses
176 * those bits to index into the SGE_FL_BUFFER_SIZE[index] register array.
177 * Since we only use SGE_FL_BUFFER_SIZE0 and SGE_FL_BUFFER_SIZE1, these low 4
178 * bits can only contain a 0 or a 1 to indicate which size buffer we're giving
179 * to the SGE. Thus, our software state of "is the buffer mapped for DMA" is
180 * maintained in an inverse sense so the hardware never sees that bit high.
183 RX_LARGE_BUF = 1 << 0, /* buffer is SGE_FL_BUFFER_SIZE[1] */
184 RX_UNMAPPED_BUF = 1 << 1, /* buffer is not mapped */
188 * get_buf_addr - return DMA buffer address of software descriptor
189 * @sdesc: pointer to the software buffer descriptor
191 * Return the DMA buffer address of a software descriptor (stripping out
192 * our low-order flag bits).
194 static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *sdesc)
196 return sdesc->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF);
200 * is_buf_mapped - is buffer mapped for DMA?
201 * @sdesc: pointer to the software buffer descriptor
203 * Determine whether the buffer associated with a software descriptor in
204 * mapped for DMA or not.
206 static inline bool is_buf_mapped(const struct rx_sw_desc *sdesc)
208 return !(sdesc->dma_addr & RX_UNMAPPED_BUF);
212 * need_skb_unmap - does the platform need unmapping of sk_buffs?
214 * Returns true if the platform needs sk_buff unmapping. The compiler
215 * optimizes away unnecessary code if this returns true.
217 static inline int need_skb_unmap(void)
219 #ifdef CONFIG_NEED_DMA_MAP_STATE
227 * txq_avail - return the number of available slots in a TX queue
230 * Returns the number of available descriptors in a TX queue.
232 static inline unsigned int txq_avail(const struct sge_txq *tq)
234 return tq->size - 1 - tq->in_use;
238 * fl_cap - return the capacity of a Free List
241 * Returns the capacity of a Free List. The capacity is less than the
242 * size because an Egress Queue Index Unit worth of descriptors needs to
243 * be left unpopulated, otherwise the Producer and Consumer indices PIDX
244 * and CIDX will match and the hardware will think the FL is empty.
246 static inline unsigned int fl_cap(const struct sge_fl *fl)
248 return fl->size - FL_PER_EQ_UNIT;
252 * fl_starving - return whether a Free List is starving.
253 * @adapter: pointer to the adapter
256 * Tests specified Free List to see whether the number of buffers
257 * available to the hardware has falled below our "starvation"
260 static inline bool fl_starving(const struct adapter *adapter,
261 const struct sge_fl *fl)
263 const struct sge *s = &adapter->sge;
265 return fl->avail - fl->pend_cred <= s->fl_starve_thres;
269 * map_skb - map an skb for DMA to the device
270 * @dev: the egress net device
271 * @skb: the packet to map
272 * @addr: a pointer to the base of the DMA mapping array
274 * Map an skb for DMA to the device and return an array of DMA addresses.
276 static int map_skb(struct device *dev, const struct sk_buff *skb,
279 const skb_frag_t *fp, *end;
280 const struct skb_shared_info *si;
282 *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
283 if (dma_mapping_error(dev, *addr))
286 si = skb_shinfo(skb);
287 end = &si->frags[si->nr_frags];
288 for (fp = si->frags; fp < end; fp++) {
289 *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
291 if (dma_mapping_error(dev, *addr))
297 while (fp-- > si->frags)
298 dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
299 dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
305 static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
306 const struct ulptx_sgl *sgl, const struct sge_txq *tq)
308 const struct ulptx_sge_pair *p;
309 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
311 if (likely(skb_headlen(skb)))
312 dma_unmap_single(dev, be64_to_cpu(sgl->addr0),
313 be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
315 dma_unmap_page(dev, be64_to_cpu(sgl->addr0),
316 be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
321 * the complexity below is because of the possibility of a wrap-around
322 * in the middle of an SGL
324 for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
325 if (likely((u8 *)(p + 1) <= (u8 *)tq->stat)) {
327 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
328 be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
329 dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
330 be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
332 } else if ((u8 *)p == (u8 *)tq->stat) {
333 p = (const struct ulptx_sge_pair *)tq->desc;
335 } else if ((u8 *)p + 8 == (u8 *)tq->stat) {
336 const __be64 *addr = (const __be64 *)tq->desc;
338 dma_unmap_page(dev, be64_to_cpu(addr[0]),
339 be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
340 dma_unmap_page(dev, be64_to_cpu(addr[1]),
341 be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
342 p = (const struct ulptx_sge_pair *)&addr[2];
344 const __be64 *addr = (const __be64 *)tq->desc;
346 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
347 be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
348 dma_unmap_page(dev, be64_to_cpu(addr[0]),
349 be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
350 p = (const struct ulptx_sge_pair *)&addr[1];
356 if ((u8 *)p == (u8 *)tq->stat)
357 p = (const struct ulptx_sge_pair *)tq->desc;
358 addr = ((u8 *)p + 16 <= (u8 *)tq->stat
360 : *(const __be64 *)tq->desc);
361 dma_unmap_page(dev, be64_to_cpu(addr), be32_to_cpu(p->len[0]),
367 * free_tx_desc - reclaims TX descriptors and their buffers
368 * @adapter: the adapter
369 * @tq: the TX queue to reclaim descriptors from
370 * @n: the number of descriptors to reclaim
371 * @unmap: whether the buffers should be unmapped for DMA
373 * Reclaims TX descriptors from an SGE TX queue and frees the associated
374 * TX buffers. Called with the TX queue lock held.
376 static void free_tx_desc(struct adapter *adapter, struct sge_txq *tq,
377 unsigned int n, bool unmap)
379 struct tx_sw_desc *sdesc;
380 unsigned int cidx = tq->cidx;
381 struct device *dev = adapter->pdev_dev;
383 const int need_unmap = need_skb_unmap() && unmap;
385 sdesc = &tq->sdesc[cidx];
388 * If we kept a reference to the original TX skb, we need to
389 * unmap it from PCI DMA space (if required) and free it.
393 unmap_sgl(dev, sdesc->skb, sdesc->sgl, tq);
394 dev_consume_skb_any(sdesc->skb);
399 if (++cidx == tq->size) {
408 * Return the number of reclaimable descriptors in a TX queue.
410 static inline int reclaimable(const struct sge_txq *tq)
412 int hw_cidx = be16_to_cpu(tq->stat->cidx);
413 int reclaimable = hw_cidx - tq->cidx;
415 reclaimable += tq->size;
420 * reclaim_completed_tx - reclaims completed TX descriptors
421 * @adapter: the adapter
422 * @tq: the TX queue to reclaim completed descriptors from
423 * @unmap: whether the buffers should be unmapped for DMA
425 * Reclaims TX descriptors that the SGE has indicated it has processed,
426 * and frees the associated buffers if possible. Called with the TX
429 static inline void reclaim_completed_tx(struct adapter *adapter,
433 int avail = reclaimable(tq);
437 * Limit the amount of clean up work we do at a time to keep
438 * the TX lock hold time O(1).
440 if (avail > MAX_TX_RECLAIM)
441 avail = MAX_TX_RECLAIM;
443 free_tx_desc(adapter, tq, avail, unmap);
449 * get_buf_size - return the size of an RX Free List buffer.
450 * @adapter: pointer to the associated adapter
451 * @sdesc: pointer to the software buffer descriptor
453 static inline int get_buf_size(const struct adapter *adapter,
454 const struct rx_sw_desc *sdesc)
456 const struct sge *s = &adapter->sge;
458 return (s->fl_pg_order > 0 && (sdesc->dma_addr & RX_LARGE_BUF)
459 ? (PAGE_SIZE << s->fl_pg_order) : PAGE_SIZE);
463 * free_rx_bufs - free RX buffers on an SGE Free List
464 * @adapter: the adapter
465 * @fl: the SGE Free List to free buffers from
466 * @n: how many buffers to free
468 * Release the next @n buffers on an SGE Free List RX queue. The
469 * buffers must be made inaccessible to hardware before calling this
472 static void free_rx_bufs(struct adapter *adapter, struct sge_fl *fl, int n)
475 struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
477 if (is_buf_mapped(sdesc))
478 dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
479 get_buf_size(adapter, sdesc),
481 put_page(sdesc->page);
483 if (++fl->cidx == fl->size)
490 * unmap_rx_buf - unmap the current RX buffer on an SGE Free List
491 * @adapter: the adapter
492 * @fl: the SGE Free List
494 * Unmap the current buffer on an SGE Free List RX queue. The
495 * buffer must be made inaccessible to HW before calling this function.
497 * This is similar to @free_rx_bufs above but does not free the buffer.
498 * Do note that the FL still loses any further access to the buffer.
499 * This is used predominantly to "transfer ownership" of an FL buffer
500 * to another entity (typically an skb's fragment list).
502 static void unmap_rx_buf(struct adapter *adapter, struct sge_fl *fl)
504 struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
506 if (is_buf_mapped(sdesc))
507 dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
508 get_buf_size(adapter, sdesc),
511 if (++fl->cidx == fl->size)
517 * ring_fl_db - righ doorbell on free list
518 * @adapter: the adapter
519 * @fl: the Free List whose doorbell should be rung ...
521 * Tell the Scatter Gather Engine that there are new free list entries
524 static inline void ring_fl_db(struct adapter *adapter, struct sge_fl *fl)
529 * The SGE keeps track of its Producer and Consumer Indices in terms
530 * of Egress Queue Units so we can only tell it about integral numbers
531 * of multiples of Free List Entries per Egress Queue Units ...
533 if (fl->pend_cred >= FL_PER_EQ_UNIT) {
534 val = PIDX(fl->pend_cred / FL_PER_EQ_UNIT);
535 if (!is_t4(adapter->params.chip))
538 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
540 QID(fl->cntxt_id) | val);
541 fl->pend_cred %= FL_PER_EQ_UNIT;
546 * set_rx_sw_desc - initialize software RX buffer descriptor
547 * @sdesc: pointer to the softwore RX buffer descriptor
548 * @page: pointer to the page data structure backing the RX buffer
549 * @dma_addr: PCI DMA address (possibly with low-bit flags)
551 static inline void set_rx_sw_desc(struct rx_sw_desc *sdesc, struct page *page,
555 sdesc->dma_addr = dma_addr;
559 * Support for poisoning RX buffers ...
561 #define POISON_BUF_VAL -1
563 static inline void poison_buf(struct page *page, size_t sz)
565 #if POISON_BUF_VAL >= 0
566 memset(page_address(page), POISON_BUF_VAL, sz);
571 * refill_fl - refill an SGE RX buffer ring
572 * @adapter: the adapter
573 * @fl: the Free List ring to refill
574 * @n: the number of new buffers to allocate
575 * @gfp: the gfp flags for the allocations
577 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
578 * allocated with the supplied gfp flags. The caller must assure that
579 * @n does not exceed the queue's capacity -- i.e. (cidx == pidx) _IN
580 * EGRESS QUEUE UNITS_ indicates an empty Free List! Returns the number
581 * of buffers allocated. If afterwards the queue is found critically low,
582 * mark it as starving in the bitmap of starving FLs.
584 static unsigned int refill_fl(struct adapter *adapter, struct sge_fl *fl,
587 struct sge *s = &adapter->sge;
590 unsigned int cred = fl->avail;
591 __be64 *d = &fl->desc[fl->pidx];
592 struct rx_sw_desc *sdesc = &fl->sdesc[fl->pidx];
595 * Sanity: ensure that the result of adding n Free List buffers
596 * won't result in wrapping the SGE's Producer Index around to
597 * it's Consumer Index thereby indicating an empty Free List ...
599 BUG_ON(fl->avail + n > fl->size - FL_PER_EQ_UNIT);
604 * If we support large pages, prefer large buffers and fail over to
605 * small pages if we can't allocate large pages to satisfy the refill.
606 * If we don't support large pages, drop directly into the small page
609 if (s->fl_pg_order == 0)
610 goto alloc_small_pages;
613 page = __dev_alloc_pages(gfp, s->fl_pg_order);
614 if (unlikely(!page)) {
616 * We've failed inour attempt to allocate a "large
617 * page". Fail over to the "small page" allocation
620 fl->large_alloc_failed++;
623 poison_buf(page, PAGE_SIZE << s->fl_pg_order);
625 dma_addr = dma_map_page(adapter->pdev_dev, page, 0,
626 PAGE_SIZE << s->fl_pg_order,
628 if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
630 * We've run out of DMA mapping space. Free up the
631 * buffer and return with what we've managed to put
632 * into the free list. We don't want to fail over to
633 * the small page allocation below in this case
634 * because DMA mapping resources are typically
635 * critical resources once they become scarse.
637 __free_pages(page, s->fl_pg_order);
640 dma_addr |= RX_LARGE_BUF;
641 *d++ = cpu_to_be64(dma_addr);
643 set_rx_sw_desc(sdesc, page, dma_addr);
647 if (++fl->pidx == fl->size) {
657 page = __dev_alloc_page(gfp);
658 if (unlikely(!page)) {
662 poison_buf(page, PAGE_SIZE);
664 dma_addr = dma_map_page(adapter->pdev_dev, page, 0, PAGE_SIZE,
666 if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
670 *d++ = cpu_to_be64(dma_addr);
672 set_rx_sw_desc(sdesc, page, dma_addr);
676 if (++fl->pidx == fl->size) {
685 * Update our accounting state to incorporate the new Free List
686 * buffers, tell the hardware about them and return the number of
687 * buffers which we were able to allocate.
689 cred = fl->avail - cred;
690 fl->pend_cred += cred;
691 ring_fl_db(adapter, fl);
693 if (unlikely(fl_starving(adapter, fl))) {
695 set_bit(fl->cntxt_id, adapter->sge.starving_fl);
702 * Refill a Free List to its capacity or the Maximum Refill Increment,
703 * whichever is smaller ...
705 static inline void __refill_fl(struct adapter *adapter, struct sge_fl *fl)
707 refill_fl(adapter, fl,
708 min((unsigned int)MAX_RX_REFILL, fl_cap(fl) - fl->avail),
713 * alloc_ring - allocate resources for an SGE descriptor ring
714 * @dev: the PCI device's core device
715 * @nelem: the number of descriptors
716 * @hwsize: the size of each hardware descriptor
717 * @swsize: the size of each software descriptor
718 * @busaddrp: the physical PCI bus address of the allocated ring
719 * @swringp: return address pointer for software ring
720 * @stat_size: extra space in hardware ring for status information
722 * Allocates resources for an SGE descriptor ring, such as TX queues,
723 * free buffer lists, response queues, etc. Each SGE ring requires
724 * space for its hardware descriptors plus, optionally, space for software
725 * state associated with each hardware entry (the metadata). The function
726 * returns three values: the virtual address for the hardware ring (the
727 * return value of the function), the PCI bus address of the hardware
728 * ring (in *busaddrp), and the address of the software ring (in swringp).
729 * Both the hardware and software rings are returned zeroed out.
731 static void *alloc_ring(struct device *dev, size_t nelem, size_t hwsize,
732 size_t swsize, dma_addr_t *busaddrp, void *swringp,
736 * Allocate the hardware ring and PCI DMA bus address space for said.
738 size_t hwlen = nelem * hwsize + stat_size;
739 void *hwring = dma_alloc_coherent(dev, hwlen, busaddrp, GFP_KERNEL);
745 * If the caller wants a software ring, allocate it and return a
746 * pointer to it in *swringp.
748 BUG_ON((swsize != 0) != (swringp != NULL));
750 void *swring = kcalloc(nelem, swsize, GFP_KERNEL);
753 dma_free_coherent(dev, hwlen, hwring, *busaddrp);
756 *(void **)swringp = swring;
760 * Zero out the hardware ring and return its address as our function
763 memset(hwring, 0, hwlen);
768 * sgl_len - calculates the size of an SGL of the given capacity
769 * @n: the number of SGL entries
771 * Calculates the number of flits (8-byte units) needed for a Direct
772 * Scatter/Gather List that can hold the given number of entries.
774 static inline unsigned int sgl_len(unsigned int n)
777 * A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
778 * addresses. The DSGL Work Request starts off with a 32-bit DSGL
779 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
780 * repeated sequences of { Length[i], Length[i+1], Address[i],
781 * Address[i+1] } (this ensures that all addresses are on 64-bit
782 * boundaries). If N is even, then Length[N+1] should be set to 0 and
783 * Address[N+1] is omitted.
785 * The following calculation incorporates all of the above. It's
786 * somewhat hard to follow but, briefly: the "+2" accounts for the
787 * first two flits which include the DSGL header, Length0 and
788 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
789 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
790 * finally the "+((n-1)&1)" adds the one remaining flit needed if
794 return (3 * n) / 2 + (n & 1) + 2;
798 * flits_to_desc - returns the num of TX descriptors for the given flits
799 * @flits: the number of flits
801 * Returns the number of TX descriptors needed for the supplied number
804 static inline unsigned int flits_to_desc(unsigned int flits)
806 BUG_ON(flits > SGE_MAX_WR_LEN / sizeof(__be64));
807 return DIV_ROUND_UP(flits, TXD_PER_EQ_UNIT);
811 * is_eth_imm - can an Ethernet packet be sent as immediate data?
814 * Returns whether an Ethernet packet is small enough to fit completely as
817 static inline int is_eth_imm(const struct sk_buff *skb)
820 * The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request
821 * which does not accommodate immediate data. We could dike out all
822 * of the support code for immediate data but that would tie our hands
823 * too much if we ever want to enhace the firmware. It would also
824 * create more differences between the PF and VF Drivers.
830 * calc_tx_flits - calculate the number of flits for a packet TX WR
833 * Returns the number of flits needed for a TX Work Request for the
834 * given Ethernet packet, including the needed WR and CPL headers.
836 static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
841 * If the skb is small enough, we can pump it out as a work request
842 * with only immediate data. In that case we just have to have the
843 * TX Packet header plus the skb data in the Work Request.
846 return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt),
850 * Otherwise, we're going to have to construct a Scatter gather list
851 * of the skb body and fragments. We also include the flits necessary
852 * for the TX Packet Work Request and CPL. We always have a firmware
853 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
854 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
855 * message or, if we're doing a Large Send Offload, an LSO CPL message
856 * with an embeded TX Packet Write CPL message.
858 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
859 if (skb_shinfo(skb)->gso_size)
860 flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
861 sizeof(struct cpl_tx_pkt_lso_core) +
862 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
864 flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
865 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
870 * write_sgl - populate a Scatter/Gather List for a packet
872 * @tq: the TX queue we are writing into
873 * @sgl: starting location for writing the SGL
874 * @end: points right after the end of the SGL
875 * @start: start offset into skb main-body data to include in the SGL
876 * @addr: the list of DMA bus addresses for the SGL elements
878 * Generates a Scatter/Gather List for the buffers that make up a packet.
879 * The caller must provide adequate space for the SGL that will be written.
880 * The SGL includes all of the packet's page fragments and the data in its
881 * main body except for the first @start bytes. @pos must be 16-byte
882 * aligned and within a TX descriptor with available space. @end points
883 * write after the end of the SGL but does not account for any potential
884 * wrap around, i.e., @end > @tq->stat.
886 static void write_sgl(const struct sk_buff *skb, struct sge_txq *tq,
887 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
888 const dma_addr_t *addr)
891 struct ulptx_sge_pair *to;
892 const struct skb_shared_info *si = skb_shinfo(skb);
893 unsigned int nfrags = si->nr_frags;
894 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
896 len = skb_headlen(skb) - start;
898 sgl->len0 = htonl(len);
899 sgl->addr0 = cpu_to_be64(addr[0] + start);
902 sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
903 sgl->addr0 = cpu_to_be64(addr[1]);
906 sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
908 if (likely(--nfrags == 0))
911 * Most of the complexity below deals with the possibility we hit the
912 * end of the queue in the middle of writing the SGL. For this case
913 * only we create the SGL in a temporary buffer and then copy it.
915 to = (u8 *)end > (u8 *)tq->stat ? buf : sgl->sge;
917 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
918 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
919 to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
920 to->addr[0] = cpu_to_be64(addr[i]);
921 to->addr[1] = cpu_to_be64(addr[++i]);
924 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
925 to->len[1] = cpu_to_be32(0);
926 to->addr[0] = cpu_to_be64(addr[i + 1]);
928 if (unlikely((u8 *)end > (u8 *)tq->stat)) {
929 unsigned int part0 = (u8 *)tq->stat - (u8 *)sgl->sge, part1;
932 memcpy(sgl->sge, buf, part0);
933 part1 = (u8 *)end - (u8 *)tq->stat;
934 memcpy(tq->desc, (u8 *)buf + part0, part1);
935 end = (void *)tq->desc + part1;
937 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
942 * check_ring_tx_db - check and potentially ring a TX queue's doorbell
943 * @adapter: the adapter
945 * @n: number of new descriptors to give to HW
947 * Ring the doorbel for a TX queue.
949 static inline void ring_tx_db(struct adapter *adapter, struct sge_txq *tq,
953 * Warn if we write doorbells with the wrong priority and write
954 * descriptors before telling HW.
956 WARN_ON((QID(tq->cntxt_id) | PIDX(n)) & DBPRIO(1));
958 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
959 QID(tq->cntxt_id) | PIDX(n));
963 * inline_tx_skb - inline a packet's data into TX descriptors
965 * @tq: the TX queue where the packet will be inlined
966 * @pos: starting position in the TX queue to inline the packet
968 * Inline a packet's contents directly into TX descriptors, starting at
969 * the given position within the TX DMA ring.
970 * Most of the complexity of this operation is dealing with wrap arounds
971 * in the middle of the packet we want to inline.
973 static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *tq,
977 int left = (void *)tq->stat - pos;
979 if (likely(skb->len <= left)) {
980 if (likely(!skb->data_len))
981 skb_copy_from_linear_data(skb, pos, skb->len);
983 skb_copy_bits(skb, 0, pos, skb->len);
986 skb_copy_bits(skb, 0, pos, left);
987 skb_copy_bits(skb, left, tq->desc, skb->len - left);
988 pos = (void *)tq->desc + (skb->len - left);
991 /* 0-pad to multiple of 16 */
992 p = PTR_ALIGN(pos, 8);
993 if ((uintptr_t)p & 8)
998 * Figure out what HW csum a packet wants and return the appropriate control
1001 static u64 hwcsum(const struct sk_buff *skb)
1004 const struct iphdr *iph = ip_hdr(skb);
1006 if (iph->version == 4) {
1007 if (iph->protocol == IPPROTO_TCP)
1008 csum_type = TX_CSUM_TCPIP;
1009 else if (iph->protocol == IPPROTO_UDP)
1010 csum_type = TX_CSUM_UDPIP;
1014 * unknown protocol, disable HW csum
1015 * and hope a bad packet is detected
1017 return TXPKT_L4CSUM_DIS;
1021 * this doesn't work with extension headers
1023 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
1025 if (ip6h->nexthdr == IPPROTO_TCP)
1026 csum_type = TX_CSUM_TCPIP6;
1027 else if (ip6h->nexthdr == IPPROTO_UDP)
1028 csum_type = TX_CSUM_UDPIP6;
1033 if (likely(csum_type >= TX_CSUM_TCPIP))
1034 return TXPKT_CSUM_TYPE(csum_type) |
1035 TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
1036 TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
1038 int start = skb_transport_offset(skb);
1040 return TXPKT_CSUM_TYPE(csum_type) |
1041 TXPKT_CSUM_START(start) |
1042 TXPKT_CSUM_LOC(start + skb->csum_offset);
1047 * Stop an Ethernet TX queue and record that state change.
1049 static void txq_stop(struct sge_eth_txq *txq)
1051 netif_tx_stop_queue(txq->txq);
1056 * Advance our software state for a TX queue by adding n in use descriptors.
1058 static inline void txq_advance(struct sge_txq *tq, unsigned int n)
1062 if (tq->pidx >= tq->size)
1063 tq->pidx -= tq->size;
1067 * t4vf_eth_xmit - add a packet to an Ethernet TX queue
1069 * @dev: the egress net device
1071 * Add a packet to an SGE Ethernet TX queue. Runs with softirqs disabled.
1073 int t4vf_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1078 unsigned int flits, ndesc;
1079 struct adapter *adapter;
1080 struct sge_eth_txq *txq;
1081 const struct port_info *pi;
1082 struct fw_eth_tx_pkt_vm_wr *wr;
1083 struct cpl_tx_pkt_core *cpl;
1084 const struct skb_shared_info *ssi;
1085 dma_addr_t addr[MAX_SKB_FRAGS + 1];
1086 const size_t fw_hdr_copy_len = (sizeof(wr->ethmacdst) +
1087 sizeof(wr->ethmacsrc) +
1088 sizeof(wr->ethtype) +
1089 sizeof(wr->vlantci));
1092 * The chip minimum packet length is 10 octets but the firmware
1093 * command that we are using requires that we copy the Ethernet header
1094 * (including the VLAN tag) into the header so we reject anything
1095 * smaller than that ...
1097 if (unlikely(skb->len < fw_hdr_copy_len))
1101 * Figure out which TX Queue we're going to use.
1103 pi = netdev_priv(dev);
1104 adapter = pi->adapter;
1105 qidx = skb_get_queue_mapping(skb);
1106 BUG_ON(qidx >= pi->nqsets);
1107 txq = &adapter->sge.ethtxq[pi->first_qset + qidx];
1110 * Take this opportunity to reclaim any TX Descriptors whose DMA
1111 * transfers have completed.
1113 reclaim_completed_tx(adapter, &txq->q, true);
1116 * Calculate the number of flits and TX Descriptors we're going to
1117 * need along with how many TX Descriptors will be left over after
1118 * we inject our Work Request.
1120 flits = calc_tx_flits(skb);
1121 ndesc = flits_to_desc(flits);
1122 credits = txq_avail(&txq->q) - ndesc;
1124 if (unlikely(credits < 0)) {
1126 * Not enough room for this packet's Work Request. Stop the
1127 * TX Queue and return a "busy" condition. The queue will get
1128 * started later on when the firmware informs us that space
1132 dev_err(adapter->pdev_dev,
1133 "%s: TX ring %u full while queue awake!\n",
1135 return NETDEV_TX_BUSY;
1138 if (!is_eth_imm(skb) &&
1139 unlikely(map_skb(adapter->pdev_dev, skb, addr) < 0)) {
1141 * We need to map the skb into PCI DMA space (because it can't
1142 * be in-lined directly into the Work Request) and the mapping
1143 * operation failed. Record the error and drop the packet.
1149 wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
1150 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1152 * After we're done injecting the Work Request for this
1153 * packet, we'll be below our "stop threshold" so stop the TX
1154 * Queue now and schedule a request for an SGE Egress Queue
1155 * Update message. The queue will get started later on when
1156 * the firmware processes this Work Request and sends us an
1157 * Egress Queue Status Update message indicating that space
1161 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
1165 * Start filling in our Work Request. Note that we do _not_ handle
1166 * the WR Header wrapping around the TX Descriptor Ring. If our
1167 * maximum header size ever exceeds one TX Descriptor, we'll need to
1168 * do something else here.
1170 BUG_ON(DIV_ROUND_UP(ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1);
1171 wr = (void *)&txq->q.desc[txq->q.pidx];
1172 wr->equiq_to_len16 = cpu_to_be32(wr_mid);
1173 wr->r3[0] = cpu_to_be64(0);
1174 wr->r3[1] = cpu_to_be64(0);
1175 skb_copy_from_linear_data(skb, (void *)wr->ethmacdst, fw_hdr_copy_len);
1176 end = (u64 *)wr + flits;
1179 * If this is a Large Send Offload packet we'll put in an LSO CPL
1180 * message with an encapsulated TX Packet CPL message. Otherwise we
1181 * just use a TX Packet CPL message.
1183 ssi = skb_shinfo(skb);
1184 if (ssi->gso_size) {
1185 struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
1186 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1187 int l3hdr_len = skb_network_header_len(skb);
1188 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1191 cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
1192 FW_WR_IMMDLEN_V(sizeof(*lso) +
1195 * Fill in the LSO CPL message.
1198 cpu_to_be32(LSO_OPCODE(CPL_TX_PKT_LSO) |
1202 LSO_ETHHDR_LEN(eth_xtra_len/4) |
1203 LSO_IPHDR_LEN(l3hdr_len/4) |
1204 LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
1205 lso->ipid_ofst = cpu_to_be16(0);
1206 lso->mss = cpu_to_be16(ssi->gso_size);
1207 lso->seqno_offset = cpu_to_be32(0);
1208 if (is_t4(adapter->params.chip))
1209 lso->len = cpu_to_be32(skb->len);
1211 lso->len = cpu_to_be32(LSO_T5_XFER_SIZE(skb->len));
1214 * Set up TX Packet CPL pointer, control word and perform
1217 cpl = (void *)(lso + 1);
1218 cntrl = (TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
1219 TXPKT_IPHDR_LEN(l3hdr_len) |
1220 TXPKT_ETHHDR_LEN(eth_xtra_len));
1222 txq->tx_cso += ssi->gso_segs;
1226 len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
1228 cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
1229 FW_WR_IMMDLEN_V(len));
1232 * Set up TX Packet CPL pointer, control word and perform
1235 cpl = (void *)(wr + 1);
1236 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1237 cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
1240 cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
1244 * If there's a VLAN tag present, add that to the list of things to
1245 * do in this Work Request.
1247 if (vlan_tx_tag_present(skb)) {
1249 cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
1253 * Fill in the TX Packet CPL message header.
1255 cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE(CPL_TX_PKT_XT) |
1256 TXPKT_INTF(pi->port_id) |
1258 cpl->pack = cpu_to_be16(0);
1259 cpl->len = cpu_to_be16(skb->len);
1260 cpl->ctrl1 = cpu_to_be64(cntrl);
1263 T4_TRACE5(adapter->tb[txq->q.cntxt_id & 7],
1264 "eth_xmit: ndesc %u, credits %u, pidx %u, len %u, frags %u",
1265 ndesc, credits, txq->q.pidx, skb->len, ssi->nr_frags);
1269 * Fill in the body of the TX Packet CPL message with either in-lined
1270 * data or a Scatter/Gather List.
1272 if (is_eth_imm(skb)) {
1274 * In-line the packet's data and free the skb since we don't
1275 * need it any longer.
1277 inline_tx_skb(skb, &txq->q, cpl + 1);
1278 dev_consume_skb_any(skb);
1281 * Write the skb's Scatter/Gather list into the TX Packet CPL
1282 * message and retain a pointer to the skb so we can free it
1283 * later when its DMA completes. (We store the skb pointer
1284 * in the Software Descriptor corresponding to the last TX
1285 * Descriptor used by the Work Request.)
1287 * The retained skb will be freed when the corresponding TX
1288 * Descriptors are reclaimed after their DMAs complete.
1289 * However, this could take quite a while since, in general,
1290 * the hardware is set up to be lazy about sending DMA
1291 * completion notifications to us and we mostly perform TX
1292 * reclaims in the transmit routine.
1294 * This is good for performamce but means that we rely on new
1295 * TX packets arriving to run the destructors of completed
1296 * packets, which open up space in their sockets' send queues.
1297 * Sometimes we do not get such new packets causing TX to
1298 * stall. A single UDP transmitter is a good example of this
1299 * situation. We have a clean up timer that periodically
1300 * reclaims completed packets but it doesn't run often enough
1301 * (nor do we want it to) to prevent lengthy stalls. A
1302 * solution to this problem is to run the destructor early,
1303 * after the packet is queued but before it's DMAd. A con is
1304 * that we lie to socket memory accounting, but the amount of
1305 * extra memory is reasonable (limited by the number of TX
1306 * descriptors), the packets do actually get freed quickly by
1307 * new packets almost always, and for protocols like TCP that
1308 * wait for acks to really free up the data the extra memory
1309 * is even less. On the positive side we run the destructors
1310 * on the sending CPU rather than on a potentially different
1311 * completing CPU, usually a good thing.
1313 * Run the destructor before telling the DMA engine about the
1314 * packet to make sure it doesn't complete and get freed
1317 struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1);
1318 struct sge_txq *tq = &txq->q;
1322 * If the Work Request header was an exact multiple of our TX
1323 * Descriptor length, then it's possible that the starting SGL
1324 * pointer lines up exactly with the end of our TX Descriptor
1325 * ring. If that's the case, wrap around to the beginning
1328 if (unlikely((void *)sgl == (void *)tq->stat)) {
1329 sgl = (void *)tq->desc;
1330 end = ((void *)tq->desc + ((void *)end - (void *)tq->stat));
1333 write_sgl(skb, tq, sgl, end, 0, addr);
1336 last_desc = tq->pidx + ndesc - 1;
1337 if (last_desc >= tq->size)
1338 last_desc -= tq->size;
1339 tq->sdesc[last_desc].skb = skb;
1340 tq->sdesc[last_desc].sgl = sgl;
1344 * Advance our internal TX Queue state, tell the hardware about
1345 * the new TX descriptors and return success.
1347 txq_advance(&txq->q, ndesc);
1348 dev->trans_start = jiffies;
1349 ring_tx_db(adapter, &txq->q, ndesc);
1350 return NETDEV_TX_OK;
1354 * An error of some sort happened. Free the TX skb and tell the
1355 * OS that we've "dealt" with the packet ...
1357 dev_kfree_skb_any(skb);
1358 return NETDEV_TX_OK;
1362 * copy_frags - copy fragments from gather list into skb_shared_info
1363 * @skb: destination skb
1364 * @gl: source internal packet gather list
1365 * @offset: packet start offset in first page
1367 * Copy an internal packet gather list into a Linux skb_shared_info
1370 static inline void copy_frags(struct sk_buff *skb,
1371 const struct pkt_gl *gl,
1372 unsigned int offset)
1376 /* usually there's just one frag */
1377 __skb_fill_page_desc(skb, 0, gl->frags[0].page,
1378 gl->frags[0].offset + offset,
1379 gl->frags[0].size - offset);
1380 skb_shinfo(skb)->nr_frags = gl->nfrags;
1381 for (i = 1; i < gl->nfrags; i++)
1382 __skb_fill_page_desc(skb, i, gl->frags[i].page,
1383 gl->frags[i].offset,
1386 /* get a reference to the last page, we don't own it */
1387 get_page(gl->frags[gl->nfrags - 1].page);
1391 * t4vf_pktgl_to_skb - build an sk_buff from a packet gather list
1392 * @gl: the gather list
1393 * @skb_len: size of sk_buff main body if it carries fragments
1394 * @pull_len: amount of data to move to the sk_buff's main body
1396 * Builds an sk_buff from the given packet gather list. Returns the
1397 * sk_buff or %NULL if sk_buff allocation failed.
1399 static struct sk_buff *t4vf_pktgl_to_skb(const struct pkt_gl *gl,
1400 unsigned int skb_len,
1401 unsigned int pull_len)
1403 struct sk_buff *skb;
1406 * If the ingress packet is small enough, allocate an skb large enough
1407 * for all of the data and copy it inline. Otherwise, allocate an skb
1408 * with enough room to pull in the header and reference the rest of
1409 * the data via the skb fragment list.
1411 * Below we rely on RX_COPY_THRES being less than the smallest Rx
1412 * buff! size, which is expected since buffers are at least
1413 * PAGE_SIZEd. In this case packets up to RX_COPY_THRES have only one
1416 if (gl->tot_len <= RX_COPY_THRES) {
1417 /* small packets have only one fragment */
1418 skb = alloc_skb(gl->tot_len, GFP_ATOMIC);
1421 __skb_put(skb, gl->tot_len);
1422 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1424 skb = alloc_skb(skb_len, GFP_ATOMIC);
1427 __skb_put(skb, pull_len);
1428 skb_copy_to_linear_data(skb, gl->va, pull_len);
1430 copy_frags(skb, gl, pull_len);
1431 skb->len = gl->tot_len;
1432 skb->data_len = skb->len - pull_len;
1433 skb->truesize += skb->data_len;
1441 * t4vf_pktgl_free - free a packet gather list
1442 * @gl: the gather list
1444 * Releases the pages of a packet gather list. We do not own the last
1445 * page on the list and do not free it.
1447 static void t4vf_pktgl_free(const struct pkt_gl *gl)
1451 frag = gl->nfrags - 1;
1453 put_page(gl->frags[frag].page);
1457 * do_gro - perform Generic Receive Offload ingress packet processing
1458 * @rxq: ingress RX Ethernet Queue
1459 * @gl: gather list for ingress packet
1460 * @pkt: CPL header for last packet fragment
1462 * Perform Generic Receive Offload (GRO) ingress packet processing.
1463 * We use the standard Linux GRO interfaces for this.
1465 static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1466 const struct cpl_rx_pkt *pkt)
1468 struct adapter *adapter = rxq->rspq.adapter;
1469 struct sge *s = &adapter->sge;
1471 struct sk_buff *skb;
1473 skb = napi_get_frags(&rxq->rspq.napi);
1474 if (unlikely(!skb)) {
1475 t4vf_pktgl_free(gl);
1476 rxq->stats.rx_drops++;
1480 copy_frags(skb, gl, s->pktshift);
1481 skb->len = gl->tot_len - s->pktshift;
1482 skb->data_len = skb->len;
1483 skb->truesize += skb->data_len;
1484 skb->ip_summed = CHECKSUM_UNNECESSARY;
1485 skb_record_rx_queue(skb, rxq->rspq.idx);
1488 __vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q),
1489 be16_to_cpu(pkt->vlan));
1490 rxq->stats.vlan_ex++;
1492 ret = napi_gro_frags(&rxq->rspq.napi);
1494 if (ret == GRO_HELD)
1495 rxq->stats.lro_pkts++;
1496 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1497 rxq->stats.lro_merged++;
1499 rxq->stats.rx_cso++;
1503 * t4vf_ethrx_handler - process an ingress ethernet packet
1504 * @rspq: the response queue that received the packet
1505 * @rsp: the response queue descriptor holding the RX_PKT message
1506 * @gl: the gather list of packet fragments
1508 * Process an ingress ethernet packet and deliver it to the stack.
1510 int t4vf_ethrx_handler(struct sge_rspq *rspq, const __be64 *rsp,
1511 const struct pkt_gl *gl)
1513 struct sk_buff *skb;
1514 const struct cpl_rx_pkt *pkt = (void *)rsp;
1515 bool csum_ok = pkt->csum_calc && !pkt->err_vec &&
1516 (rspq->netdev->features & NETIF_F_RXCSUM);
1517 struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
1518 struct adapter *adapter = rspq->adapter;
1519 struct sge *s = &adapter->sge;
1522 * If this is a good TCP packet and we have Generic Receive Offload
1523 * enabled, handle the packet in the GRO path.
1525 if ((pkt->l2info & cpu_to_be32(RXF_TCP)) &&
1526 (rspq->netdev->features & NETIF_F_GRO) && csum_ok &&
1528 do_gro(rxq, gl, pkt);
1533 * Convert the Packet Gather List into an skb.
1535 skb = t4vf_pktgl_to_skb(gl, RX_SKB_LEN, RX_PULL_LEN);
1536 if (unlikely(!skb)) {
1537 t4vf_pktgl_free(gl);
1538 rxq->stats.rx_drops++;
1541 __skb_pull(skb, s->pktshift);
1542 skb->protocol = eth_type_trans(skb, rspq->netdev);
1543 skb_record_rx_queue(skb, rspq->idx);
1546 if (csum_ok && !pkt->err_vec &&
1547 (be32_to_cpu(pkt->l2info) & (RXF_UDP|RXF_TCP))) {
1549 skb->ip_summed = CHECKSUM_UNNECESSARY;
1551 __sum16 c = (__force __sum16)pkt->csum;
1552 skb->csum = csum_unfold(c);
1553 skb->ip_summed = CHECKSUM_COMPLETE;
1555 rxq->stats.rx_cso++;
1557 skb_checksum_none_assert(skb);
1560 rxq->stats.vlan_ex++;
1561 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), be16_to_cpu(pkt->vlan));
1564 netif_receive_skb(skb);
1570 * is_new_response - check if a response is newly written
1571 * @rc: the response control descriptor
1572 * @rspq: the response queue
1574 * Returns true if a response descriptor contains a yet unprocessed
1577 static inline bool is_new_response(const struct rsp_ctrl *rc,
1578 const struct sge_rspq *rspq)
1580 return RSPD_GEN(rc->type_gen) == rspq->gen;
1584 * restore_rx_bufs - put back a packet's RX buffers
1585 * @gl: the packet gather list
1586 * @fl: the SGE Free List
1587 * @nfrags: how many fragments in @si
1589 * Called when we find out that the current packet, @si, can't be
1590 * processed right away for some reason. This is a very rare event and
1591 * there's no effort to make this suspension/resumption process
1592 * particularly efficient.
1594 * We implement the suspension by putting all of the RX buffers associated
1595 * with the current packet back on the original Free List. The buffers
1596 * have already been unmapped and are left unmapped, we mark them as
1597 * unmapped in order to prevent further unmapping attempts. (Effectively
1598 * this function undoes the series of @unmap_rx_buf calls which were done
1599 * to create the current packet's gather list.) This leaves us ready to
1600 * restart processing of the packet the next time we start processing the
1603 static void restore_rx_bufs(const struct pkt_gl *gl, struct sge_fl *fl,
1606 struct rx_sw_desc *sdesc;
1610 fl->cidx = fl->size - 1;
1613 sdesc = &fl->sdesc[fl->cidx];
1614 sdesc->page = gl->frags[frags].page;
1615 sdesc->dma_addr |= RX_UNMAPPED_BUF;
1621 * rspq_next - advance to the next entry in a response queue
1624 * Updates the state of a response queue to advance it to the next entry.
1626 static inline void rspq_next(struct sge_rspq *rspq)
1628 rspq->cur_desc = (void *)rspq->cur_desc + rspq->iqe_len;
1629 if (unlikely(++rspq->cidx == rspq->size)) {
1632 rspq->cur_desc = rspq->desc;
1637 * process_responses - process responses from an SGE response queue
1638 * @rspq: the ingress response queue to process
1639 * @budget: how many responses can be processed in this round
1641 * Process responses from a Scatter Gather Engine response queue up to
1642 * the supplied budget. Responses include received packets as well as
1643 * control messages from firmware or hardware.
1645 * Additionally choose the interrupt holdoff time for the next interrupt
1646 * on this queue. If the system is under memory shortage use a fairly
1647 * long delay to help recovery.
1649 static int process_responses(struct sge_rspq *rspq, int budget)
1651 struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
1652 struct adapter *adapter = rspq->adapter;
1653 struct sge *s = &adapter->sge;
1654 int budget_left = budget;
1656 while (likely(budget_left)) {
1658 const struct rsp_ctrl *rc;
1660 rc = (void *)rspq->cur_desc + (rspq->iqe_len - sizeof(*rc));
1661 if (!is_new_response(rc, rspq))
1665 * Figure out what kind of response we've received from the
1669 rsp_type = RSPD_TYPE(rc->type_gen);
1670 if (likely(rsp_type == RSP_TYPE_FLBUF)) {
1671 struct page_frag *fp;
1673 const struct rx_sw_desc *sdesc;
1675 u32 len = be32_to_cpu(rc->pldbuflen_qid);
1678 * If we get a "new buffer" message from the SGE we
1679 * need to move on to the next Free List buffer.
1681 if (len & RSPD_NEWBUF) {
1683 * We get one "new buffer" message when we
1684 * first start up a queue so we need to ignore
1685 * it when our offset into the buffer is 0.
1687 if (likely(rspq->offset > 0)) {
1688 free_rx_bufs(rspq->adapter, &rxq->fl,
1692 len = RSPD_LEN(len);
1697 * Gather packet fragments.
1699 for (frag = 0, fp = gl.frags; /**/; frag++, fp++) {
1700 BUG_ON(frag >= MAX_SKB_FRAGS);
1701 BUG_ON(rxq->fl.avail == 0);
1702 sdesc = &rxq->fl.sdesc[rxq->fl.cidx];
1703 bufsz = get_buf_size(adapter, sdesc);
1704 fp->page = sdesc->page;
1705 fp->offset = rspq->offset;
1706 fp->size = min(bufsz, len);
1710 unmap_rx_buf(rspq->adapter, &rxq->fl);
1715 * Last buffer remains mapped so explicitly make it
1716 * coherent for CPU access and start preloading first
1719 dma_sync_single_for_cpu(rspq->adapter->pdev_dev,
1720 get_buf_addr(sdesc),
1721 fp->size, DMA_FROM_DEVICE);
1722 gl.va = (page_address(gl.frags[0].page) +
1723 gl.frags[0].offset);
1727 * Hand the new ingress packet to the handler for
1728 * this Response Queue.
1730 ret = rspq->handler(rspq, rspq->cur_desc, &gl);
1731 if (likely(ret == 0))
1732 rspq->offset += ALIGN(fp->size, s->fl_align);
1734 restore_rx_bufs(&gl, &rxq->fl, frag);
1735 } else if (likely(rsp_type == RSP_TYPE_CPL)) {
1736 ret = rspq->handler(rspq, rspq->cur_desc, NULL);
1738 WARN_ON(rsp_type > RSP_TYPE_CPL);
1742 if (unlikely(ret)) {
1744 * Couldn't process descriptor, back off for recovery.
1745 * We use the SGE's last timer which has the longest
1746 * interrupt coalescing value ...
1748 const int NOMEM_TIMER_IDX = SGE_NTIMERS-1;
1749 rspq->next_intr_params =
1750 QINTR_TIMER_IDX(NOMEM_TIMER_IDX);
1759 * If this is a Response Queue with an associated Free List and
1760 * at least two Egress Queue units available in the Free List
1761 * for new buffer pointers, refill the Free List.
1763 if (rspq->offset >= 0 &&
1764 rxq->fl.size - rxq->fl.avail >= 2*FL_PER_EQ_UNIT)
1765 __refill_fl(rspq->adapter, &rxq->fl);
1766 return budget - budget_left;
1770 * napi_rx_handler - the NAPI handler for RX processing
1771 * @napi: the napi instance
1772 * @budget: how many packets we can process in this round
1774 * Handler for new data events when using NAPI. This does not need any
1775 * locking or protection from interrupts as data interrupts are off at
1776 * this point and other adapter interrupts do not interfere (the latter
1777 * in not a concern at all with MSI-X as non-data interrupts then have
1778 * a separate handler).
1780 static int napi_rx_handler(struct napi_struct *napi, int budget)
1782 unsigned int intr_params;
1783 struct sge_rspq *rspq = container_of(napi, struct sge_rspq, napi);
1784 int work_done = process_responses(rspq, budget);
1786 if (likely(work_done < budget)) {
1787 napi_complete(napi);
1788 intr_params = rspq->next_intr_params;
1789 rspq->next_intr_params = rspq->intr_params;
1791 intr_params = QINTR_TIMER_IDX(SGE_TIMER_UPD_CIDX);
1793 if (unlikely(work_done == 0))
1794 rspq->unhandled_irqs++;
1796 t4_write_reg(rspq->adapter,
1797 T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
1798 CIDXINC(work_done) |
1799 INGRESSQID((u32)rspq->cntxt_id) |
1800 SEINTARM(intr_params));
1805 * The MSI-X interrupt handler for an SGE response queue for the NAPI case
1806 * (i.e., response queue serviced by NAPI polling).
1808 irqreturn_t t4vf_sge_intr_msix(int irq, void *cookie)
1810 struct sge_rspq *rspq = cookie;
1812 napi_schedule(&rspq->napi);
1817 * Process the indirect interrupt entries in the interrupt queue and kick off
1818 * NAPI for each queue that has generated an entry.
1820 static unsigned int process_intrq(struct adapter *adapter)
1822 struct sge *s = &adapter->sge;
1823 struct sge_rspq *intrq = &s->intrq;
1824 unsigned int work_done;
1826 spin_lock(&adapter->sge.intrq_lock);
1827 for (work_done = 0; ; work_done++) {
1828 const struct rsp_ctrl *rc;
1829 unsigned int qid, iq_idx;
1830 struct sge_rspq *rspq;
1833 * Grab the next response from the interrupt queue and bail
1834 * out if it's not a new response.
1836 rc = (void *)intrq->cur_desc + (intrq->iqe_len - sizeof(*rc));
1837 if (!is_new_response(rc, intrq))
1841 * If the response isn't a forwarded interrupt message issue a
1842 * error and go on to the next response message. This should
1846 if (unlikely(RSPD_TYPE(rc->type_gen) != RSP_TYPE_INTR)) {
1847 dev_err(adapter->pdev_dev,
1848 "Unexpected INTRQ response type %d\n",
1849 RSPD_TYPE(rc->type_gen));
1854 * Extract the Queue ID from the interrupt message and perform
1855 * sanity checking to make sure it really refers to one of our
1856 * Ingress Queues which is active and matches the queue's ID.
1857 * None of these error conditions should ever happen so we may
1858 * want to either make them fatal and/or conditionalized under
1861 qid = RSPD_QID(be32_to_cpu(rc->pldbuflen_qid));
1862 iq_idx = IQ_IDX(s, qid);
1863 if (unlikely(iq_idx >= MAX_INGQ)) {
1864 dev_err(adapter->pdev_dev,
1865 "Ingress QID %d out of range\n", qid);
1868 rspq = s->ingr_map[iq_idx];
1869 if (unlikely(rspq == NULL)) {
1870 dev_err(adapter->pdev_dev,
1871 "Ingress QID %d RSPQ=NULL\n", qid);
1874 if (unlikely(rspq->abs_id != qid)) {
1875 dev_err(adapter->pdev_dev,
1876 "Ingress QID %d refers to RSPQ %d\n",
1882 * Schedule NAPI processing on the indicated Response Queue
1883 * and move on to the next entry in the Forwarded Interrupt
1886 napi_schedule(&rspq->napi);
1890 t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
1891 CIDXINC(work_done) |
1892 INGRESSQID(intrq->cntxt_id) |
1893 SEINTARM(intrq->intr_params));
1895 spin_unlock(&adapter->sge.intrq_lock);
1901 * The MSI interrupt handler handles data events from SGE response queues as
1902 * well as error and other async events as they all use the same MSI vector.
1904 static irqreturn_t t4vf_intr_msi(int irq, void *cookie)
1906 struct adapter *adapter = cookie;
1908 process_intrq(adapter);
1913 * t4vf_intr_handler - select the top-level interrupt handler
1914 * @adapter: the adapter
1916 * Selects the top-level interrupt handler based on the type of interrupts
1919 irq_handler_t t4vf_intr_handler(struct adapter *adapter)
1921 BUG_ON((adapter->flags & (USING_MSIX|USING_MSI)) == 0);
1922 if (adapter->flags & USING_MSIX)
1923 return t4vf_sge_intr_msix;
1925 return t4vf_intr_msi;
1929 * sge_rx_timer_cb - perform periodic maintenance of SGE RX queues
1930 * @data: the adapter
1932 * Runs periodically from a timer to perform maintenance of SGE RX queues.
1934 * a) Replenishes RX queues that have run out due to memory shortage.
1935 * Normally new RX buffers are added when existing ones are consumed but
1936 * when out of memory a queue can become empty. We schedule NAPI to do
1937 * the actual refill.
1939 static void sge_rx_timer_cb(unsigned long data)
1941 struct adapter *adapter = (struct adapter *)data;
1942 struct sge *s = &adapter->sge;
1946 * Scan the "Starving Free Lists" flag array looking for any Free
1947 * Lists in need of more free buffers. If we find one and it's not
1948 * being actively polled, then bump its "starving" counter and attempt
1949 * to refill it. If we're successful in adding enough buffers to push
1950 * the Free List over the starving threshold, then we can clear its
1951 * "starving" status.
1953 for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++) {
1956 for (m = s->starving_fl[i]; m; m &= m - 1) {
1957 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
1958 struct sge_fl *fl = s->egr_map[id];
1960 clear_bit(id, s->starving_fl);
1961 smp_mb__after_atomic();
1964 * Since we are accessing fl without a lock there's a
1965 * small probability of a false positive where we
1966 * schedule napi but the FL is no longer starving.
1969 if (fl_starving(adapter, fl)) {
1970 struct sge_eth_rxq *rxq;
1972 rxq = container_of(fl, struct sge_eth_rxq, fl);
1973 if (napi_reschedule(&rxq->rspq.napi))
1976 set_bit(id, s->starving_fl);
1982 * Reschedule the next scan for starving Free Lists ...
1984 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
1988 * sge_tx_timer_cb - perform periodic maintenance of SGE Tx queues
1989 * @data: the adapter
1991 * Runs periodically from a timer to perform maintenance of SGE TX queues.
1993 * b) Reclaims completed Tx packets for the Ethernet queues. Normally
1994 * packets are cleaned up by new Tx packets, this timer cleans up packets
1995 * when no new packets are being submitted. This is essential for pktgen,
1998 static void sge_tx_timer_cb(unsigned long data)
2000 struct adapter *adapter = (struct adapter *)data;
2001 struct sge *s = &adapter->sge;
2002 unsigned int i, budget;
2004 budget = MAX_TIMER_TX_RECLAIM;
2005 i = s->ethtxq_rover;
2007 struct sge_eth_txq *txq = &s->ethtxq[i];
2009 if (reclaimable(&txq->q) && __netif_tx_trylock(txq->txq)) {
2010 int avail = reclaimable(&txq->q);
2015 free_tx_desc(adapter, &txq->q, avail, true);
2016 txq->q.in_use -= avail;
2017 __netif_tx_unlock(txq->txq);
2025 if (i >= s->ethqsets)
2027 } while (i != s->ethtxq_rover);
2028 s->ethtxq_rover = i;
2031 * If we found too many reclaimable packets schedule a timer in the
2032 * near future to continue where we left off. Otherwise the next timer
2033 * will be at its normal interval.
2035 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
2039 * t4vf_sge_alloc_rxq - allocate an SGE RX Queue
2040 * @adapter: the adapter
2041 * @rspq: pointer to to the new rxq's Response Queue to be filled in
2042 * @iqasynch: if 0, a normal rspq; if 1, an asynchronous event queue
2043 * @dev: the network device associated with the new rspq
2044 * @intr_dest: MSI-X vector index (overriden in MSI mode)
2045 * @fl: pointer to the new rxq's Free List to be filled in
2046 * @hnd: the interrupt handler to invoke for the rspq
2048 int t4vf_sge_alloc_rxq(struct adapter *adapter, struct sge_rspq *rspq,
2049 bool iqasynch, struct net_device *dev,
2051 struct sge_fl *fl, rspq_handler_t hnd)
2053 struct sge *s = &adapter->sge;
2054 struct port_info *pi = netdev_priv(dev);
2055 struct fw_iq_cmd cmd, rpl;
2056 int ret, iqandst, flsz = 0;
2059 * If we're using MSI interrupts and we're not initializing the
2060 * Forwarded Interrupt Queue itself, then set up this queue for
2061 * indirect interrupts to the Forwarded Interrupt Queue. Obviously
2062 * the Forwarded Interrupt Queue must be set up before any other
2065 if ((adapter->flags & USING_MSI) && rspq != &adapter->sge.intrq) {
2066 iqandst = SGE_INTRDST_IQ;
2067 intr_dest = adapter->sge.intrq.abs_id;
2069 iqandst = SGE_INTRDST_PCI;
2072 * Allocate the hardware ring for the Response Queue. The size needs
2073 * to be a multiple of 16 which includes the mandatory status entry
2074 * (regardless of whether the Status Page capabilities are enabled or
2077 rspq->size = roundup(rspq->size, 16);
2078 rspq->desc = alloc_ring(adapter->pdev_dev, rspq->size, rspq->iqe_len,
2079 0, &rspq->phys_addr, NULL, 0);
2084 * Fill in the Ingress Queue Command. Note: Ideally this code would
2085 * be in t4vf_hw.c but there are so many parameters and dependencies
2086 * on our Linux SGE state that we would end up having to pass tons of
2087 * parameters. We'll have to think about how this might be migrated
2088 * into OS-independent common code ...
2090 memset(&cmd, 0, sizeof(cmd));
2091 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_IQ_CMD) |
2095 cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_ALLOC |
2096 FW_IQ_CMD_IQSTART(1) |
2098 cmd.type_to_iqandstindex =
2099 cpu_to_be32(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
2100 FW_IQ_CMD_IQASYNCH(iqasynch) |
2101 FW_IQ_CMD_VIID(pi->viid) |
2102 FW_IQ_CMD_IQANDST(iqandst) |
2103 FW_IQ_CMD_IQANUS(1) |
2104 FW_IQ_CMD_IQANUD(SGE_UPDATEDEL_INTR) |
2105 FW_IQ_CMD_IQANDSTINDEX(intr_dest));
2106 cmd.iqdroprss_to_iqesize =
2107 cpu_to_be16(FW_IQ_CMD_IQPCIECH(pi->port_id) |
2108 FW_IQ_CMD_IQGTSMODE |
2109 FW_IQ_CMD_IQINTCNTTHRESH(rspq->pktcnt_idx) |
2110 FW_IQ_CMD_IQESIZE(ilog2(rspq->iqe_len) - 4));
2111 cmd.iqsize = cpu_to_be16(rspq->size);
2112 cmd.iqaddr = cpu_to_be64(rspq->phys_addr);
2116 * Allocate the ring for the hardware free list (with space
2117 * for its status page) along with the associated software
2118 * descriptor ring. The free list size needs to be a multiple
2119 * of the Egress Queue Unit.
2121 fl->size = roundup(fl->size, FL_PER_EQ_UNIT);
2122 fl->desc = alloc_ring(adapter->pdev_dev, fl->size,
2123 sizeof(__be64), sizeof(struct rx_sw_desc),
2124 &fl->addr, &fl->sdesc, s->stat_len);
2131 * Calculate the size of the hardware free list ring plus
2132 * Status Page (which the SGE will place after the end of the
2133 * free list ring) in Egress Queue Units.
2135 flsz = (fl->size / FL_PER_EQ_UNIT +
2136 s->stat_len / EQ_UNIT);
2139 * Fill in all the relevant firmware Ingress Queue Command
2140 * fields for the free list.
2142 cmd.iqns_to_fl0congen =
2144 FW_IQ_CMD_FL0HOSTFCMODE(SGE_HOSTFCMODE_NONE) |
2145 FW_IQ_CMD_FL0PACKEN(1) |
2146 FW_IQ_CMD_FL0PADEN(1));
2147 cmd.fl0dcaen_to_fl0cidxfthresh =
2149 FW_IQ_CMD_FL0FBMIN(SGE_FETCHBURSTMIN_64B) |
2150 FW_IQ_CMD_FL0FBMAX(SGE_FETCHBURSTMAX_512B));
2151 cmd.fl0size = cpu_to_be16(flsz);
2152 cmd.fl0addr = cpu_to_be64(fl->addr);
2156 * Issue the firmware Ingress Queue Command and extract the results if
2157 * it completes successfully.
2159 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
2163 netif_napi_add(dev, &rspq->napi, napi_rx_handler, 64);
2164 rspq->cur_desc = rspq->desc;
2167 rspq->next_intr_params = rspq->intr_params;
2168 rspq->cntxt_id = be16_to_cpu(rpl.iqid);
2169 rspq->abs_id = be16_to_cpu(rpl.physiqid);
2170 rspq->size--; /* subtract status entry */
2171 rspq->adapter = adapter;
2173 rspq->handler = hnd;
2175 /* set offset to -1 to distinguish ingress queues without FL */
2176 rspq->offset = fl ? 0 : -1;
2179 fl->cntxt_id = be16_to_cpu(rpl.fl0id);
2184 fl->alloc_failed = 0;
2185 fl->large_alloc_failed = 0;
2187 refill_fl(adapter, fl, fl_cap(fl), GFP_KERNEL);
2194 * An error occurred. Clean up our partial allocation state and
2198 dma_free_coherent(adapter->pdev_dev, rspq->size * rspq->iqe_len,
2199 rspq->desc, rspq->phys_addr);
2202 if (fl && fl->desc) {
2205 dma_free_coherent(adapter->pdev_dev, flsz * EQ_UNIT,
2206 fl->desc, fl->addr);
2213 * t4vf_sge_alloc_eth_txq - allocate an SGE Ethernet TX Queue
2214 * @adapter: the adapter
2215 * @txq: pointer to the new txq to be filled in
2216 * @devq: the network TX queue associated with the new txq
2217 * @iqid: the relative ingress queue ID to which events relating to
2218 * the new txq should be directed
2220 int t4vf_sge_alloc_eth_txq(struct adapter *adapter, struct sge_eth_txq *txq,
2221 struct net_device *dev, struct netdev_queue *devq,
2224 struct sge *s = &adapter->sge;
2226 struct fw_eq_eth_cmd cmd, rpl;
2227 struct port_info *pi = netdev_priv(dev);
2230 * Calculate the size of the hardware TX Queue (including the Status
2231 * Page on the end of the TX Queue) in units of TX Descriptors.
2233 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
2236 * Allocate the hardware ring for the TX ring (with space for its
2237 * status page) along with the associated software descriptor ring.
2239 txq->q.desc = alloc_ring(adapter->pdev_dev, txq->q.size,
2240 sizeof(struct tx_desc),
2241 sizeof(struct tx_sw_desc),
2242 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len);
2247 * Fill in the Egress Queue Command. Note: As with the direct use of
2248 * the firmware Ingress Queue COmmand above in our RXQ allocation
2249 * routine, ideally, this code would be in t4vf_hw.c. Again, we'll
2250 * have to see if there's some reasonable way to parameterize it
2251 * into the common code ...
2253 memset(&cmd, 0, sizeof(cmd));
2254 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_EQ_ETH_CMD) |
2258 cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_ALLOC |
2259 FW_EQ_ETH_CMD_EQSTART |
2261 cmd.viid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_AUTOEQUEQE |
2262 FW_EQ_ETH_CMD_VIID(pi->viid));
2263 cmd.fetchszm_to_iqid =
2264 cpu_to_be32(FW_EQ_ETH_CMD_HOSTFCMODE(SGE_HOSTFCMODE_STPG) |
2265 FW_EQ_ETH_CMD_PCIECHN(pi->port_id) |
2266 FW_EQ_ETH_CMD_IQID(iqid));
2267 cmd.dcaen_to_eqsize =
2268 cpu_to_be32(FW_EQ_ETH_CMD_FBMIN(SGE_FETCHBURSTMIN_64B) |
2269 FW_EQ_ETH_CMD_FBMAX(SGE_FETCHBURSTMAX_512B) |
2270 FW_EQ_ETH_CMD_CIDXFTHRESH(SGE_CIDXFLUSHTHRESH_32) |
2271 FW_EQ_ETH_CMD_EQSIZE(nentries));
2272 cmd.eqaddr = cpu_to_be64(txq->q.phys_addr);
2275 * Issue the firmware Egress Queue Command and extract the results if
2276 * it completes successfully.
2278 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
2281 * The girmware Ingress Queue Command failed for some reason.
2282 * Free up our partial allocation state and return the error.
2284 kfree(txq->q.sdesc);
2285 txq->q.sdesc = NULL;
2286 dma_free_coherent(adapter->pdev_dev,
2287 nentries * sizeof(struct tx_desc),
2288 txq->q.desc, txq->q.phys_addr);
2296 txq->q.stat = (void *)&txq->q.desc[txq->q.size];
2297 txq->q.cntxt_id = FW_EQ_ETH_CMD_EQID_GET(be32_to_cpu(rpl.eqid_pkd));
2299 FW_EQ_ETH_CMD_PHYSEQID_GET(be32_to_cpu(rpl.physeqid_pkd));
2305 txq->q.restarts = 0;
2306 txq->mapping_err = 0;
2311 * Free the DMA map resources associated with a TX queue.
2313 static void free_txq(struct adapter *adapter, struct sge_txq *tq)
2315 struct sge *s = &adapter->sge;
2317 dma_free_coherent(adapter->pdev_dev,
2318 tq->size * sizeof(*tq->desc) + s->stat_len,
2319 tq->desc, tq->phys_addr);
2326 * Free the resources associated with a response queue (possibly including a
2329 static void free_rspq_fl(struct adapter *adapter, struct sge_rspq *rspq,
2332 struct sge *s = &adapter->sge;
2333 unsigned int flid = fl ? fl->cntxt_id : 0xffff;
2335 t4vf_iq_free(adapter, FW_IQ_TYPE_FL_INT_CAP,
2336 rspq->cntxt_id, flid, 0xffff);
2337 dma_free_coherent(adapter->pdev_dev, (rspq->size + 1) * rspq->iqe_len,
2338 rspq->desc, rspq->phys_addr);
2339 netif_napi_del(&rspq->napi);
2340 rspq->netdev = NULL;
2346 free_rx_bufs(adapter, fl, fl->avail);
2347 dma_free_coherent(adapter->pdev_dev,
2348 fl->size * sizeof(*fl->desc) + s->stat_len,
2349 fl->desc, fl->addr);
2358 * t4vf_free_sge_resources - free SGE resources
2359 * @adapter: the adapter
2361 * Frees resources used by the SGE queue sets.
2363 void t4vf_free_sge_resources(struct adapter *adapter)
2365 struct sge *s = &adapter->sge;
2366 struct sge_eth_rxq *rxq = s->ethrxq;
2367 struct sge_eth_txq *txq = s->ethtxq;
2368 struct sge_rspq *evtq = &s->fw_evtq;
2369 struct sge_rspq *intrq = &s->intrq;
2372 for (qs = 0; qs < adapter->sge.ethqsets; qs++, rxq++, txq++) {
2374 free_rspq_fl(adapter, &rxq->rspq, &rxq->fl);
2376 t4vf_eth_eq_free(adapter, txq->q.cntxt_id);
2377 free_tx_desc(adapter, &txq->q, txq->q.in_use, true);
2378 kfree(txq->q.sdesc);
2379 free_txq(adapter, &txq->q);
2383 free_rspq_fl(adapter, evtq, NULL);
2385 free_rspq_fl(adapter, intrq, NULL);
2389 * t4vf_sge_start - enable SGE operation
2390 * @adapter: the adapter
2392 * Start tasklets and timers associated with the DMA engine.
2394 void t4vf_sge_start(struct adapter *adapter)
2396 adapter->sge.ethtxq_rover = 0;
2397 mod_timer(&adapter->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2398 mod_timer(&adapter->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2402 * t4vf_sge_stop - disable SGE operation
2403 * @adapter: the adapter
2405 * Stop tasklets and timers associated with the DMA engine. Note that
2406 * this is effective only if measures have been taken to disable any HW
2407 * events that may restart them.
2409 void t4vf_sge_stop(struct adapter *adapter)
2411 struct sge *s = &adapter->sge;
2413 if (s->rx_timer.function)
2414 del_timer_sync(&s->rx_timer);
2415 if (s->tx_timer.function)
2416 del_timer_sync(&s->tx_timer);
2420 * t4vf_sge_init - initialize SGE
2421 * @adapter: the adapter
2423 * Performs SGE initialization needed every time after a chip reset.
2424 * We do not initialize any of the queue sets here, instead the driver
2425 * top-level must request those individually. We also do not enable DMA
2426 * here, that should be done after the queues have been set up.
2428 int t4vf_sge_init(struct adapter *adapter)
2430 struct sge_params *sge_params = &adapter->params.sge;
2431 u32 fl0 = sge_params->sge_fl_buffer_size[0];
2432 u32 fl1 = sge_params->sge_fl_buffer_size[1];
2433 struct sge *s = &adapter->sge;
2434 unsigned int ingpadboundary, ingpackboundary;
2437 * Start by vetting the basic SGE parameters which have been set up by
2438 * the Physical Function Driver. Ideally we should be able to deal
2439 * with _any_ configuration. Practice is different ...
2441 if (fl0 != PAGE_SIZE || (fl1 != 0 && fl1 <= fl0)) {
2442 dev_err(adapter->pdev_dev, "bad SGE FL buffer sizes [%d, %d]\n",
2446 if ((sge_params->sge_control & RXPKTCPLMODE_MASK) == 0) {
2447 dev_err(adapter->pdev_dev, "bad SGE CPL MODE\n");
2452 * Now translate the adapter parameters into our internal forms.
2455 s->fl_pg_order = ilog2(fl1) - PAGE_SHIFT;
2456 s->stat_len = ((sge_params->sge_control & EGRSTATUSPAGESIZE_MASK)
2458 s->pktshift = PKTSHIFT_GET(sge_params->sge_control);
2460 /* T4 uses a single control field to specify both the PCIe Padding and
2461 * Packing Boundary. T5 introduced the ability to specify these
2462 * separately. The actual Ingress Packet Data alignment boundary
2463 * within Packed Buffer Mode is the maximum of these two
2464 * specifications. (Note that it makes no real practical sense to
2465 * have the Pading Boudary be larger than the Packing Boundary but you
2466 * could set the chip up that way and, in fact, legacy T4 code would
2467 * end doing this because it would initialize the Padding Boundary and
2468 * leave the Packing Boundary initialized to 0 (16 bytes).)
2470 ingpadboundary = 1 << (INGPADBOUNDARY_GET(sge_params->sge_control) +
2471 X_INGPADBOUNDARY_SHIFT);
2472 if (is_t4(adapter->params.chip)) {
2473 s->fl_align = ingpadboundary;
2475 /* T5 has a different interpretation of one of the PCIe Packing
2478 ingpackboundary = INGPACKBOUNDARY_G(sge_params->sge_control2);
2479 if (ingpackboundary == INGPACKBOUNDARY_16B_X)
2480 ingpackboundary = 16;
2482 ingpackboundary = 1 << (ingpackboundary +
2483 INGPACKBOUNDARY_SHIFT_X);
2485 s->fl_align = max(ingpadboundary, ingpackboundary);
2488 /* A FL with <= fl_starve_thres buffers is starving and a periodic
2489 * timer will attempt to refill it. This needs to be larger than the
2490 * SGE's Egress Congestion Threshold. If it isn't, then we can get
2491 * stuck waiting for new packets while the SGE is waiting for us to
2492 * give it more Free List entries. (Note that the SGE's Egress
2493 * Congestion Threshold is in units of 2 Free List pointers.)
2496 = EGRTHRESHOLD_GET(sge_params->sge_congestion_control)*2 + 1;
2499 * Set up tasklet timers.
2501 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adapter);
2502 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adapter);
2505 * Initialize Forwarded Interrupt Queue lock.
2507 spin_lock_init(&s->intrq_lock);