2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
4 * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/if_vlan.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/jiffies.h>
42 #include <linux/prefetch.h>
43 #include <linux/export.h>
46 #ifdef CONFIG_NET_RX_BUSY_POLL
47 #include <net/busy_poll.h>
48 #endif /* CONFIG_NET_RX_BUSY_POLL */
49 #ifdef CONFIG_CHELSIO_T4_FCOE
50 #include <scsi/fc/fc_fcoe.h>
51 #endif /* CONFIG_CHELSIO_T4_FCOE */
54 #include "t4_values.h"
59 * Rx buffer size. We use largish buffers if possible but settle for single
60 * pages under memory shortage.
63 # define FL_PG_ORDER 0
65 # define FL_PG_ORDER (16 - PAGE_SHIFT)
68 /* RX_PULL_LEN should be <= RX_COPY_THRES */
69 #define RX_COPY_THRES 256
70 #define RX_PULL_LEN 128
73 * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
74 * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
76 #define RX_PKT_SKB_LEN 512
79 * Max number of Tx descriptors we clean up at a time. Should be modest as
80 * freeing skbs isn't cheap and it happens while holding locks. We just need
81 * to free packets faster than they arrive, we eventually catch up and keep
82 * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
84 #define MAX_TX_RECLAIM 16
87 * Max number of Rx buffers we replenish at a time. Again keep this modest,
88 * allocating buffers isn't cheap either.
90 #define MAX_RX_REFILL 16U
93 * Period of the Rx queue check timer. This timer is infrequent as it has
94 * something to do only when the system experiences severe memory shortage.
96 #define RX_QCHECK_PERIOD (HZ / 2)
99 * Period of the Tx queue check timer.
101 #define TX_QCHECK_PERIOD (HZ / 2)
104 * Max number of Tx descriptors to be reclaimed by the Tx timer.
106 #define MAX_TIMER_TX_RECLAIM 100
109 * Timer index used when backing off due to memory shortage.
111 #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
114 * Suspend an Ethernet Tx queue with fewer available descriptors than this.
115 * This is the same as calc_tx_descs() for a TSO packet with
116 * nr_frags == MAX_SKB_FRAGS.
118 #define ETHTXQ_STOP_THRES \
119 (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
122 * Suspension threshold for non-Ethernet Tx queues. We require enough room
123 * for a full sized WR.
125 #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
128 * Max Tx descriptor space we allow for an Ethernet packet to be inlined
131 #define MAX_IMM_TX_PKT_LEN 256
134 * Max size of a WR sent through a control Tx queue.
136 #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
138 struct tx_sw_desc { /* SW state per Tx descriptor */
140 struct ulptx_sgl *sgl;
143 struct rx_sw_desc { /* SW state per Rx descriptor */
149 * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
150 * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
151 * We could easily support more but there doesn't seem to be much need for
154 #define FL_MTU_SMALL 1500
155 #define FL_MTU_LARGE 9000
157 static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
160 struct sge *s = &adapter->sge;
162 return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
165 #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
166 #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
169 * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
170 * these to specify the buffer size as an index into the SGE Free List Buffer
171 * Size register array. We also use bit 4, when the buffer has been unmapped
172 * for DMA, but this is of course never sent to the hardware and is only used
173 * to prevent double unmappings. All of the above requires that the Free List
174 * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
175 * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
176 * Free List Buffer alignment is 32 bytes, this works out for us ...
179 RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
180 RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
181 RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
184 * XXX We shouldn't depend on being able to use these indices.
185 * XXX Especially when some other Master PF has initialized the
186 * XXX adapter or we use the Firmware Configuration File. We
187 * XXX should really search through the Host Buffer Size register
188 * XXX array for the appropriately sized buffer indices.
190 RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
191 RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
193 RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
194 RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
197 static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
198 #define MIN_NAPI_WORK 1
200 static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
202 return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
205 static inline bool is_buf_mapped(const struct rx_sw_desc *d)
207 return !(d->dma_addr & RX_UNMAPPED_BUF);
211 * txq_avail - return the number of available slots in a Tx queue
214 * Returns the number of descriptors in a Tx queue available to write new
217 static inline unsigned int txq_avail(const struct sge_txq *q)
219 return q->size - 1 - q->in_use;
223 * fl_cap - return the capacity of a free-buffer list
226 * Returns the capacity of a free-buffer list. The capacity is less than
227 * the size because one descriptor needs to be left unpopulated, otherwise
228 * HW will think the FL is empty.
230 static inline unsigned int fl_cap(const struct sge_fl *fl)
232 return fl->size - 8; /* 1 descriptor = 8 buffers */
236 * fl_starving - return whether a Free List is starving.
237 * @adapter: pointer to the adapter
240 * Tests specified Free List to see whether the number of buffers
241 * available to the hardware has falled below our "starvation"
244 static inline bool fl_starving(const struct adapter *adapter,
245 const struct sge_fl *fl)
247 const struct sge *s = &adapter->sge;
249 return fl->avail - fl->pend_cred <= s->fl_starve_thres;
252 static int map_skb(struct device *dev, const struct sk_buff *skb,
255 const skb_frag_t *fp, *end;
256 const struct skb_shared_info *si;
258 *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
259 if (dma_mapping_error(dev, *addr))
262 si = skb_shinfo(skb);
263 end = &si->frags[si->nr_frags];
265 for (fp = si->frags; fp < end; fp++) {
266 *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
268 if (dma_mapping_error(dev, *addr))
274 while (fp-- > si->frags)
275 dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
277 dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
282 #ifdef CONFIG_NEED_DMA_MAP_STATE
283 static void unmap_skb(struct device *dev, const struct sk_buff *skb,
284 const dma_addr_t *addr)
286 const skb_frag_t *fp, *end;
287 const struct skb_shared_info *si;
289 dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
291 si = skb_shinfo(skb);
292 end = &si->frags[si->nr_frags];
293 for (fp = si->frags; fp < end; fp++)
294 dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
298 * deferred_unmap_destructor - unmap a packet when it is freed
301 * This is the packet destructor used for Tx packets that need to remain
302 * mapped until they are freed rather than until their Tx descriptors are
305 static void deferred_unmap_destructor(struct sk_buff *skb)
307 unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
311 static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
312 const struct ulptx_sgl *sgl, const struct sge_txq *q)
314 const struct ulptx_sge_pair *p;
315 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
317 if (likely(skb_headlen(skb)))
318 dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
321 dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
327 * the complexity below is because of the possibility of a wrap-around
328 * in the middle of an SGL
330 for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
331 if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
332 unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
333 ntohl(p->len[0]), DMA_TO_DEVICE);
334 dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
335 ntohl(p->len[1]), DMA_TO_DEVICE);
337 } else if ((u8 *)p == (u8 *)q->stat) {
338 p = (const struct ulptx_sge_pair *)q->desc;
340 } else if ((u8 *)p + 8 == (u8 *)q->stat) {
341 const __be64 *addr = (const __be64 *)q->desc;
343 dma_unmap_page(dev, be64_to_cpu(addr[0]),
344 ntohl(p->len[0]), DMA_TO_DEVICE);
345 dma_unmap_page(dev, be64_to_cpu(addr[1]),
346 ntohl(p->len[1]), DMA_TO_DEVICE);
347 p = (const struct ulptx_sge_pair *)&addr[2];
349 const __be64 *addr = (const __be64 *)q->desc;
351 dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
352 ntohl(p->len[0]), DMA_TO_DEVICE);
353 dma_unmap_page(dev, be64_to_cpu(addr[0]),
354 ntohl(p->len[1]), DMA_TO_DEVICE);
355 p = (const struct ulptx_sge_pair *)&addr[1];
361 if ((u8 *)p == (u8 *)q->stat)
362 p = (const struct ulptx_sge_pair *)q->desc;
363 addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
364 *(const __be64 *)q->desc;
365 dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
371 * free_tx_desc - reclaims Tx descriptors and their buffers
372 * @adapter: the adapter
373 * @q: the Tx queue to reclaim descriptors from
374 * @n: the number of descriptors to reclaim
375 * @unmap: whether the buffers should be unmapped for DMA
377 * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
378 * Tx buffers. Called with the Tx queue lock held.
380 static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
381 unsigned int n, bool unmap)
383 struct tx_sw_desc *d;
384 unsigned int cidx = q->cidx;
385 struct device *dev = adap->pdev_dev;
389 if (d->skb) { /* an SGL is present */
391 unmap_sgl(dev, d->skb, d->sgl, q);
392 dev_consume_skb_any(d->skb);
396 if (++cidx == q->size) {
405 * Return the number of reclaimable descriptors in a Tx queue.
407 static inline int reclaimable(const struct sge_txq *q)
409 int hw_cidx = ntohs(q->stat->cidx);
411 return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
415 * reclaim_completed_tx - reclaims completed Tx descriptors
417 * @q: the Tx queue to reclaim completed descriptors from
418 * @unmap: whether the buffers should be unmapped for DMA
420 * Reclaims Tx descriptors that the SGE has indicated it has processed,
421 * and frees the associated buffers if possible. Called with the Tx
424 static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
427 int avail = reclaimable(q);
431 * Limit the amount of clean up work we do at a time to keep
432 * the Tx lock hold time O(1).
434 if (avail > MAX_TX_RECLAIM)
435 avail = MAX_TX_RECLAIM;
437 free_tx_desc(adap, q, avail, unmap);
442 static inline int get_buf_size(struct adapter *adapter,
443 const struct rx_sw_desc *d)
445 struct sge *s = &adapter->sge;
446 unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
449 switch (rx_buf_size_idx) {
450 case RX_SMALL_PG_BUF:
451 buf_size = PAGE_SIZE;
454 case RX_LARGE_PG_BUF:
455 buf_size = PAGE_SIZE << s->fl_pg_order;
458 case RX_SMALL_MTU_BUF:
459 buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
462 case RX_LARGE_MTU_BUF:
463 buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
474 * free_rx_bufs - free the Rx buffers on an SGE free list
476 * @q: the SGE free list to free buffers from
477 * @n: how many buffers to free
479 * Release the next @n buffers on an SGE free-buffer Rx queue. The
480 * buffers must be made inaccessible to HW before calling this function.
482 static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
485 struct rx_sw_desc *d = &q->sdesc[q->cidx];
487 if (is_buf_mapped(d))
488 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
489 get_buf_size(adap, d),
493 if (++q->cidx == q->size)
500 * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
502 * @q: the SGE free list
504 * Unmap the current buffer on an SGE free-buffer Rx queue. The
505 * buffer must be made inaccessible to HW before calling this function.
507 * This is similar to @free_rx_bufs above but does not free the buffer.
508 * Do note that the FL still loses any further access to the buffer.
510 static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
512 struct rx_sw_desc *d = &q->sdesc[q->cidx];
514 if (is_buf_mapped(d))
515 dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
516 get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
518 if (++q->cidx == q->size)
523 static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
526 if (q->pend_cred >= 8) {
527 if (is_t4(adap->params.chip))
528 val = PIDX_V(q->pend_cred / 8);
530 val = PIDX_T5_V(q->pend_cred / 8) |
534 /* Make sure all memory writes to the Free List queue are
535 * committed before we tell the hardware about them.
539 /* If we don't have access to the new User Doorbell (T5+), use
540 * the old doorbell mechanism; otherwise use the new BAR2
543 if (unlikely(q->bar2_addr == NULL)) {
544 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
545 val | QID_V(q->cntxt_id));
547 writel(val | QID_V(q->bar2_qid),
548 q->bar2_addr + SGE_UDB_KDOORBELL);
550 /* This Write memory Barrier will force the write to
551 * the User Doorbell area to be flushed.
559 static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
563 sd->dma_addr = mapping; /* includes size low bits */
567 * refill_fl - refill an SGE Rx buffer ring
569 * @q: the ring to refill
570 * @n: the number of new buffers to allocate
571 * @gfp: the gfp flags for the allocations
573 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
574 * allocated with the supplied gfp flags. The caller must assure that
575 * @n does not exceed the queue's capacity. If afterwards the queue is
576 * found critically low mark it as starving in the bitmap of starving FLs.
578 * Returns the number of buffers allocated.
580 static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
583 struct sge *s = &adap->sge;
586 unsigned int cred = q->avail;
587 __be64 *d = &q->desc[q->pidx];
588 struct rx_sw_desc *sd = &q->sdesc[q->pidx];
591 #ifdef CONFIG_DEBUG_FS
592 if (test_bit(q->cntxt_id - adap->sge.egr_start, adap->sge.blocked_fl))
597 node = dev_to_node(adap->pdev_dev);
599 if (s->fl_pg_order == 0)
600 goto alloc_small_pages;
603 * Prefer large buffers
606 pg = alloc_pages_node(node, gfp | __GFP_COMP, s->fl_pg_order);
608 q->large_alloc_failed++;
609 break; /* fall back to single pages */
612 mapping = dma_map_page(adap->pdev_dev, pg, 0,
613 PAGE_SIZE << s->fl_pg_order,
615 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
616 __free_pages(pg, s->fl_pg_order);
617 goto out; /* do not try small pages for this error */
619 mapping |= RX_LARGE_PG_BUF;
620 *d++ = cpu_to_be64(mapping);
622 set_rx_sw_desc(sd, pg, mapping);
626 if (++q->pidx == q->size) {
636 pg = alloc_pages_node(node, gfp, 0);
642 mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
644 if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
648 *d++ = cpu_to_be64(mapping);
650 set_rx_sw_desc(sd, pg, mapping);
654 if (++q->pidx == q->size) {
661 out: cred = q->avail - cred;
662 q->pend_cred += cred;
665 if (unlikely(fl_starving(adap, q))) {
667 set_bit(q->cntxt_id - adap->sge.egr_start,
668 adap->sge.starving_fl);
674 static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
676 refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
681 * alloc_ring - allocate resources for an SGE descriptor ring
682 * @dev: the PCI device's core device
683 * @nelem: the number of descriptors
684 * @elem_size: the size of each descriptor
685 * @sw_size: the size of the SW state associated with each ring element
686 * @phys: the physical address of the allocated ring
687 * @metadata: address of the array holding the SW state for the ring
688 * @stat_size: extra space in HW ring for status information
689 * @node: preferred node for memory allocations
691 * Allocates resources for an SGE descriptor ring, such as Tx queues,
692 * free buffer lists, or response queues. Each SGE ring requires
693 * space for its HW descriptors plus, optionally, space for the SW state
694 * associated with each HW entry (the metadata). The function returns
695 * three values: the virtual address for the HW ring (the return value
696 * of the function), the bus address of the HW ring, and the address
699 static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
700 size_t sw_size, dma_addr_t *phys, void *metadata,
701 size_t stat_size, int node)
703 size_t len = nelem * elem_size + stat_size;
705 void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
710 s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
713 dma_free_coherent(dev, len, p, *phys);
718 *(void **)metadata = s;
724 * sgl_len - calculates the size of an SGL of the given capacity
725 * @n: the number of SGL entries
727 * Calculates the number of flits needed for a scatter/gather list that
728 * can hold the given number of entries.
730 static inline unsigned int sgl_len(unsigned int n)
732 /* A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
733 * addresses. The DSGL Work Request starts off with a 32-bit DSGL
734 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
735 * repeated sequences of { Length[i], Length[i+1], Address[i],
736 * Address[i+1] } (this ensures that all addresses are on 64-bit
737 * boundaries). If N is even, then Length[N+1] should be set to 0 and
738 * Address[N+1] is omitted.
740 * The following calculation incorporates all of the above. It's
741 * somewhat hard to follow but, briefly: the "+2" accounts for the
742 * first two flits which include the DSGL header, Length0 and
743 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
744 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
745 * finally the "+((n-1)&1)" adds the one remaining flit needed if
749 return (3 * n) / 2 + (n & 1) + 2;
753 * flits_to_desc - returns the num of Tx descriptors for the given flits
754 * @n: the number of flits
756 * Returns the number of Tx descriptors needed for the supplied number
759 static inline unsigned int flits_to_desc(unsigned int n)
761 BUG_ON(n > SGE_MAX_WR_LEN / 8);
762 return DIV_ROUND_UP(n, 8);
766 * is_eth_imm - can an Ethernet packet be sent as immediate data?
769 * Returns whether an Ethernet packet is small enough to fit as
770 * immediate data. Return value corresponds to headroom required.
772 static inline int is_eth_imm(const struct sk_buff *skb)
774 int hdrlen = skb_shinfo(skb)->gso_size ?
775 sizeof(struct cpl_tx_pkt_lso_core) : 0;
777 hdrlen += sizeof(struct cpl_tx_pkt);
778 if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
784 * calc_tx_flits - calculate the number of flits for a packet Tx WR
787 * Returns the number of flits needed for a Tx WR for the given Ethernet
788 * packet, including the needed WR and CPL headers.
790 static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
793 int hdrlen = is_eth_imm(skb);
795 /* If the skb is small enough, we can pump it out as a work request
796 * with only immediate data. In that case we just have to have the
797 * TX Packet header plus the skb data in the Work Request.
801 return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
803 /* Otherwise, we're going to have to construct a Scatter gather list
804 * of the skb body and fragments. We also include the flits necessary
805 * for the TX Packet Work Request and CPL. We always have a firmware
806 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
807 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
808 * message or, if we're doing a Large Send Offload, an LSO CPL message
809 * with an embedded TX Packet Write CPL message.
811 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
812 if (skb_shinfo(skb)->gso_size)
813 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
814 sizeof(struct cpl_tx_pkt_lso_core) +
815 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
817 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
818 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
823 * calc_tx_descs - calculate the number of Tx descriptors for a packet
826 * Returns the number of Tx descriptors needed for the given Ethernet
827 * packet, including the needed WR and CPL headers.
829 static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
831 return flits_to_desc(calc_tx_flits(skb));
835 * write_sgl - populate a scatter/gather list for a packet
837 * @q: the Tx queue we are writing into
838 * @sgl: starting location for writing the SGL
839 * @end: points right after the end of the SGL
840 * @start: start offset into skb main-body data to include in the SGL
841 * @addr: the list of bus addresses for the SGL elements
843 * Generates a gather list for the buffers that make up a packet.
844 * The caller must provide adequate space for the SGL that will be written.
845 * The SGL includes all of the packet's page fragments and the data in its
846 * main body except for the first @start bytes. @sgl must be 16-byte
847 * aligned and within a Tx descriptor with available space. @end points
848 * right after the end of the SGL but does not account for any potential
849 * wrap around, i.e., @end > @sgl.
851 static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
852 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
853 const dma_addr_t *addr)
856 struct ulptx_sge_pair *to;
857 const struct skb_shared_info *si = skb_shinfo(skb);
858 unsigned int nfrags = si->nr_frags;
859 struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
861 len = skb_headlen(skb) - start;
863 sgl->len0 = htonl(len);
864 sgl->addr0 = cpu_to_be64(addr[0] + start);
867 sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
868 sgl->addr0 = cpu_to_be64(addr[1]);
871 sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
872 ULPTX_NSGE_V(nfrags));
873 if (likely(--nfrags == 0))
876 * Most of the complexity below deals with the possibility we hit the
877 * end of the queue in the middle of writing the SGL. For this case
878 * only we create the SGL in a temporary buffer and then copy it.
880 to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
882 for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
883 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
884 to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
885 to->addr[0] = cpu_to_be64(addr[i]);
886 to->addr[1] = cpu_to_be64(addr[++i]);
889 to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
890 to->len[1] = cpu_to_be32(0);
891 to->addr[0] = cpu_to_be64(addr[i + 1]);
893 if (unlikely((u8 *)end > (u8 *)q->stat)) {
894 unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
897 memcpy(sgl->sge, buf, part0);
898 part1 = (u8 *)end - (u8 *)q->stat;
899 memcpy(q->desc, (u8 *)buf + part0, part1);
900 end = (void *)q->desc + part1;
902 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
906 /* This function copies 64 byte coalesced work request to
907 * memory mapped BAR2 space. For coalesced WR SGE fetches
908 * data from the FIFO instead of from Host.
910 static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
923 * ring_tx_db - check and potentially ring a Tx queue's doorbell
926 * @n: number of new descriptors to give to HW
928 * Ring the doorbel for a Tx queue.
930 static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
932 /* Make sure that all writes to the TX Descriptors are committed
933 * before we tell the hardware about them.
937 /* If we don't have access to the new User Doorbell (T5+), use the old
938 * doorbell mechanism; otherwise use the new BAR2 mechanism.
940 if (unlikely(q->bar2_addr == NULL)) {
944 /* For T4 we need to participate in the Doorbell Recovery
947 spin_lock_irqsave(&q->db_lock, flags);
949 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
950 QID_V(q->cntxt_id) | val);
953 q->db_pidx = q->pidx;
954 spin_unlock_irqrestore(&q->db_lock, flags);
956 u32 val = PIDX_T5_V(n);
958 /* T4 and later chips share the same PIDX field offset within
959 * the doorbell, but T5 and later shrank the field in order to
960 * gain a bit for Doorbell Priority. The field was absurdly
961 * large in the first place (14 bits) so we just use the T5
962 * and later limits and warn if a Queue ID is too large.
964 WARN_ON(val & DBPRIO_F);
966 /* If we're only writing a single TX Descriptor and we can use
967 * Inferred QID registers, we can use the Write Combining
968 * Gather Buffer; otherwise we use the simple doorbell.
970 if (n == 1 && q->bar2_qid == 0) {
974 u64 *wr = (u64 *)&q->desc[index];
976 cxgb_pio_copy((u64 __iomem *)
977 (q->bar2_addr + SGE_UDB_WCDOORBELL),
980 writel(val | QID_V(q->bar2_qid),
981 q->bar2_addr + SGE_UDB_KDOORBELL);
984 /* This Write Memory Barrier will force the write to the User
985 * Doorbell area to be flushed. This is needed to prevent
986 * writes on different CPUs for the same queue from hitting
987 * the adapter out of order. This is required when some Work
988 * Requests take the Write Combine Gather Buffer path (user
989 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
990 * take the traditional path where we simply increment the
991 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
992 * hardware DMA read the actual Work Request.
999 * inline_tx_skb - inline a packet's data into Tx descriptors
1001 * @q: the Tx queue where the packet will be inlined
1002 * @pos: starting position in the Tx queue where to inline the packet
1004 * Inline a packet's contents directly into Tx descriptors, starting at
1005 * the given position within the Tx DMA ring.
1006 * Most of the complexity of this operation is dealing with wrap arounds
1007 * in the middle of the packet we want to inline.
1009 static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
1013 int left = (void *)q->stat - pos;
1015 if (likely(skb->len <= left)) {
1016 if (likely(!skb->data_len))
1017 skb_copy_from_linear_data(skb, pos, skb->len);
1019 skb_copy_bits(skb, 0, pos, skb->len);
1022 skb_copy_bits(skb, 0, pos, left);
1023 skb_copy_bits(skb, left, q->desc, skb->len - left);
1024 pos = (void *)q->desc + (skb->len - left);
1027 /* 0-pad to multiple of 16 */
1028 p = PTR_ALIGN(pos, 8);
1029 if ((uintptr_t)p & 8)
1034 * Figure out what HW csum a packet wants and return the appropriate control
1037 static u64 hwcsum(const struct sk_buff *skb)
1040 const struct iphdr *iph = ip_hdr(skb);
1042 if (iph->version == 4) {
1043 if (iph->protocol == IPPROTO_TCP)
1044 csum_type = TX_CSUM_TCPIP;
1045 else if (iph->protocol == IPPROTO_UDP)
1046 csum_type = TX_CSUM_UDPIP;
1049 * unknown protocol, disable HW csum
1050 * and hope a bad packet is detected
1052 return TXPKT_L4CSUM_DIS_F;
1056 * this doesn't work with extension headers
1058 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
1060 if (ip6h->nexthdr == IPPROTO_TCP)
1061 csum_type = TX_CSUM_TCPIP6;
1062 else if (ip6h->nexthdr == IPPROTO_UDP)
1063 csum_type = TX_CSUM_UDPIP6;
1068 if (likely(csum_type >= TX_CSUM_TCPIP))
1069 return TXPKT_CSUM_TYPE_V(csum_type) |
1070 TXPKT_IPHDR_LEN_V(skb_network_header_len(skb)) |
1071 TXPKT_ETHHDR_LEN_V(skb_network_offset(skb) - ETH_HLEN);
1073 int start = skb_transport_offset(skb);
1075 return TXPKT_CSUM_TYPE_V(csum_type) |
1076 TXPKT_CSUM_START_V(start) |
1077 TXPKT_CSUM_LOC_V(start + skb->csum_offset);
1081 static void eth_txq_stop(struct sge_eth_txq *q)
1083 netif_tx_stop_queue(q->txq);
1087 static inline void txq_advance(struct sge_txq *q, unsigned int n)
1091 if (q->pidx >= q->size)
1095 #ifdef CONFIG_CHELSIO_T4_FCOE
1097 cxgb_fcoe_offload(struct sk_buff *skb, struct adapter *adap,
1098 const struct port_info *pi, u64 *cntrl)
1100 const struct cxgb_fcoe *fcoe = &pi->fcoe;
1102 if (!(fcoe->flags & CXGB_FCOE_ENABLED))
1105 if (skb->protocol != htons(ETH_P_FCOE))
1108 skb_reset_mac_header(skb);
1109 skb->mac_len = sizeof(struct ethhdr);
1111 skb_set_network_header(skb, skb->mac_len);
1112 skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
1114 if (!cxgb_fcoe_sof_eof_supported(adap, skb))
1117 /* FC CRC offload */
1118 *cntrl = TXPKT_CSUM_TYPE_V(TX_CSUM_FCOE) |
1119 TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F |
1120 TXPKT_CSUM_START_V(CXGB_FCOE_TXPKT_CSUM_START) |
1121 TXPKT_CSUM_END_V(CXGB_FCOE_TXPKT_CSUM_END) |
1122 TXPKT_CSUM_LOC_V(CXGB_FCOE_TXPKT_CSUM_END);
1125 #endif /* CONFIG_CHELSIO_T4_FCOE */
1128 * t4_eth_xmit - add a packet to an Ethernet Tx queue
1130 * @dev: the egress net device
1132 * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
1134 netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1139 unsigned int flits, ndesc;
1140 struct adapter *adap;
1141 struct sge_eth_txq *q;
1142 const struct port_info *pi;
1143 struct fw_eth_tx_pkt_wr *wr;
1144 struct cpl_tx_pkt_core *cpl;
1145 const struct skb_shared_info *ssi;
1146 dma_addr_t addr[MAX_SKB_FRAGS + 1];
1147 bool immediate = false;
1148 int len, max_pkt_len;
1149 #ifdef CONFIG_CHELSIO_T4_FCOE
1151 #endif /* CONFIG_CHELSIO_T4_FCOE */
1154 * The chip min packet length is 10 octets but play safe and reject
1155 * anything shorter than an Ethernet header.
1157 if (unlikely(skb->len < ETH_HLEN)) {
1158 out_free: dev_kfree_skb_any(skb);
1159 return NETDEV_TX_OK;
1162 /* Discard the packet if the length is greater than mtu */
1163 max_pkt_len = ETH_HLEN + dev->mtu;
1164 if (skb_vlan_tag_present(skb))
1165 max_pkt_len += VLAN_HLEN;
1166 if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
1169 pi = netdev_priv(dev);
1171 qidx = skb_get_queue_mapping(skb);
1172 q = &adap->sge.ethtxq[qidx + pi->first_qset];
1174 reclaim_completed_tx(adap, &q->q, true);
1175 cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
1177 #ifdef CONFIG_CHELSIO_T4_FCOE
1178 err = cxgb_fcoe_offload(skb, adap, pi, &cntrl);
1179 if (unlikely(err == -ENOTSUPP))
1181 #endif /* CONFIG_CHELSIO_T4_FCOE */
1183 flits = calc_tx_flits(skb);
1184 ndesc = flits_to_desc(flits);
1185 credits = txq_avail(&q->q) - ndesc;
1187 if (unlikely(credits < 0)) {
1189 dev_err(adap->pdev_dev,
1190 "%s: Tx ring %u full while queue awake!\n",
1192 return NETDEV_TX_BUSY;
1195 if (is_eth_imm(skb))
1199 unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
1204 wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
1205 if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1207 wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
1210 wr = (void *)&q->q.desc[q->q.pidx];
1211 wr->equiq_to_len16 = htonl(wr_mid);
1212 wr->r3 = cpu_to_be64(0);
1213 end = (u64 *)wr + flits;
1215 len = immediate ? skb->len : 0;
1216 ssi = skb_shinfo(skb);
1217 if (ssi->gso_size) {
1218 struct cpl_tx_pkt_lso *lso = (void *)wr;
1219 bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1220 int l3hdr_len = skb_network_header_len(skb);
1221 int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1223 len += sizeof(*lso);
1224 wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
1225 FW_WR_IMMDLEN_V(len));
1226 lso->c.lso_ctrl = htonl(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
1227 LSO_FIRST_SLICE_F | LSO_LAST_SLICE_F |
1229 LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
1230 LSO_IPHDR_LEN_V(l3hdr_len / 4) |
1231 LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
1232 lso->c.ipid_ofst = htons(0);
1233 lso->c.mss = htons(ssi->gso_size);
1234 lso->c.seqno_offset = htonl(0);
1235 if (is_t4(adap->params.chip))
1236 lso->c.len = htonl(skb->len);
1238 lso->c.len = htonl(LSO_T5_XFER_SIZE_V(skb->len));
1239 cpl = (void *)(lso + 1);
1240 cntrl = TXPKT_CSUM_TYPE_V(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
1241 TXPKT_IPHDR_LEN_V(l3hdr_len) |
1242 TXPKT_ETHHDR_LEN_V(eth_xtra_len);
1244 q->tx_cso += ssi->gso_segs;
1246 len += sizeof(*cpl);
1247 wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
1248 FW_WR_IMMDLEN_V(len));
1249 cpl = (void *)(wr + 1);
1250 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1251 cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS_F;
1256 if (skb_vlan_tag_present(skb)) {
1258 cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
1259 #ifdef CONFIG_CHELSIO_T4_FCOE
1260 if (skb->protocol == htons(ETH_P_FCOE))
1261 cntrl |= TXPKT_VLAN_V(
1262 ((skb->priority & 0x7) << VLAN_PRIO_SHIFT));
1263 #endif /* CONFIG_CHELSIO_T4_FCOE */
1266 cpl->ctrl0 = htonl(TXPKT_OPCODE_V(CPL_TX_PKT_XT) |
1267 TXPKT_INTF_V(pi->tx_chan) |
1268 TXPKT_PF_V(adap->fn));
1269 cpl->pack = htons(0);
1270 cpl->len = htons(skb->len);
1271 cpl->ctrl1 = cpu_to_be64(cntrl);
1274 inline_tx_skb(skb, &q->q, cpl + 1);
1275 dev_consume_skb_any(skb);
1279 write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
1283 last_desc = q->q.pidx + ndesc - 1;
1284 if (last_desc >= q->q.size)
1285 last_desc -= q->q.size;
1286 q->q.sdesc[last_desc].skb = skb;
1287 q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
1290 txq_advance(&q->q, ndesc);
1292 ring_tx_db(adap, &q->q, ndesc);
1293 return NETDEV_TX_OK;
1297 * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
1298 * @q: the SGE control Tx queue
1300 * This is a variant of reclaim_completed_tx() that is used for Tx queues
1301 * that send only immediate data (presently just the control queues) and
1302 * thus do not have any sk_buffs to release.
1304 static inline void reclaim_completed_tx_imm(struct sge_txq *q)
1306 int hw_cidx = ntohs(q->stat->cidx);
1307 int reclaim = hw_cidx - q->cidx;
1312 q->in_use -= reclaim;
1317 * is_imm - check whether a packet can be sent as immediate data
1320 * Returns true if a packet can be sent as a WR with immediate data.
1322 static inline int is_imm(const struct sk_buff *skb)
1324 return skb->len <= MAX_CTRL_WR_LEN;
1328 * ctrlq_check_stop - check if a control queue is full and should stop
1330 * @wr: most recent WR written to the queue
1332 * Check if a control queue has become full and should be stopped.
1333 * We clean up control queue descriptors very lazily, only when we are out.
1334 * If the queue is still full after reclaiming any completed descriptors
1335 * we suspend it and have the last WR wake it up.
1337 static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
1339 reclaim_completed_tx_imm(&q->q);
1340 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1341 wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
1348 * ctrl_xmit - send a packet through an SGE control Tx queue
1349 * @q: the control queue
1352 * Send a packet through an SGE control Tx queue. Packets sent through
1353 * a control queue must fit entirely as immediate data.
1355 static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
1358 struct fw_wr_hdr *wr;
1360 if (unlikely(!is_imm(skb))) {
1363 return NET_XMIT_DROP;
1366 ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
1367 spin_lock(&q->sendq.lock);
1369 if (unlikely(q->full)) {
1370 skb->priority = ndesc; /* save for restart */
1371 __skb_queue_tail(&q->sendq, skb);
1372 spin_unlock(&q->sendq.lock);
1376 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1377 inline_tx_skb(skb, &q->q, wr);
1379 txq_advance(&q->q, ndesc);
1380 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
1381 ctrlq_check_stop(q, wr);
1383 ring_tx_db(q->adap, &q->q, ndesc);
1384 spin_unlock(&q->sendq.lock);
1387 return NET_XMIT_SUCCESS;
1391 * restart_ctrlq - restart a suspended control queue
1392 * @data: the control queue to restart
1394 * Resumes transmission on a suspended Tx control queue.
1396 static void restart_ctrlq(unsigned long data)
1398 struct sk_buff *skb;
1399 unsigned int written = 0;
1400 struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
1402 spin_lock(&q->sendq.lock);
1403 reclaim_completed_tx_imm(&q->q);
1404 BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
1406 while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
1407 struct fw_wr_hdr *wr;
1408 unsigned int ndesc = skb->priority; /* previously saved */
1411 * Write descriptors and free skbs outside the lock to limit
1412 * wait times. q->full is still set so new skbs will be queued.
1414 spin_unlock(&q->sendq.lock);
1416 wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1417 inline_tx_skb(skb, &q->q, wr);
1421 txq_advance(&q->q, ndesc);
1422 if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1423 unsigned long old = q->q.stops;
1425 ctrlq_check_stop(q, wr);
1426 if (q->q.stops != old) { /* suspended anew */
1427 spin_lock(&q->sendq.lock);
1432 ring_tx_db(q->adap, &q->q, written);
1435 spin_lock(&q->sendq.lock);
1438 ringdb: if (written)
1439 ring_tx_db(q->adap, &q->q, written);
1440 spin_unlock(&q->sendq.lock);
1444 * t4_mgmt_tx - send a management message
1445 * @adap: the adapter
1446 * @skb: the packet containing the management message
1448 * Send a management message through control queue 0.
1450 int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
1455 ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
1461 * is_ofld_imm - check whether a packet can be sent as immediate data
1464 * Returns true if a packet can be sent as an offload WR with immediate
1465 * data. We currently use the same limit as for Ethernet packets.
1467 static inline int is_ofld_imm(const struct sk_buff *skb)
1469 return skb->len <= MAX_IMM_TX_PKT_LEN;
1473 * calc_tx_flits_ofld - calculate # of flits for an offload packet
1476 * Returns the number of flits needed for the given offload packet.
1477 * These packets are already fully constructed and no additional headers
1480 static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
1482 unsigned int flits, cnt;
1484 if (is_ofld_imm(skb))
1485 return DIV_ROUND_UP(skb->len, 8);
1487 flits = skb_transport_offset(skb) / 8U; /* headers */
1488 cnt = skb_shinfo(skb)->nr_frags;
1489 if (skb_tail_pointer(skb) != skb_transport_header(skb))
1491 return flits + sgl_len(cnt);
1495 * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
1496 * @adap: the adapter
1497 * @q: the queue to stop
1499 * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
1500 * inability to map packets. A periodic timer attempts to restart
1503 static void txq_stop_maperr(struct sge_ofld_txq *q)
1507 set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
1508 q->adap->sge.txq_maperr);
1512 * ofldtxq_stop - stop an offload Tx queue that has become full
1513 * @q: the queue to stop
1514 * @skb: the packet causing the queue to become full
1516 * Stops an offload Tx queue that has become full and modifies the packet
1517 * being written to request a wakeup.
1519 static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
1521 struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
1523 wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
1529 * service_ofldq - restart a suspended offload queue
1530 * @q: the offload queue
1532 * Services an offload Tx queue by moving packets from its packet queue
1533 * to the HW Tx ring. The function starts and ends with the queue locked.
1535 static void service_ofldq(struct sge_ofld_txq *q)
1539 struct sk_buff *skb;
1540 unsigned int written = 0;
1541 unsigned int flits, ndesc;
1543 while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
1545 * We drop the lock but leave skb on sendq, thus retaining
1546 * exclusive access to the state of the queue.
1548 spin_unlock(&q->sendq.lock);
1550 reclaim_completed_tx(q->adap, &q->q, false);
1552 flits = skb->priority; /* previously saved */
1553 ndesc = flits_to_desc(flits);
1554 credits = txq_avail(&q->q) - ndesc;
1555 BUG_ON(credits < 0);
1556 if (unlikely(credits < TXQ_STOP_THRES))
1557 ofldtxq_stop(q, skb);
1559 pos = (u64 *)&q->q.desc[q->q.pidx];
1560 if (is_ofld_imm(skb))
1561 inline_tx_skb(skb, &q->q, pos);
1562 else if (map_skb(q->adap->pdev_dev, skb,
1563 (dma_addr_t *)skb->head)) {
1565 spin_lock(&q->sendq.lock);
1568 int last_desc, hdr_len = skb_transport_offset(skb);
1570 memcpy(pos, skb->data, hdr_len);
1571 write_sgl(skb, &q->q, (void *)pos + hdr_len,
1572 pos + flits, hdr_len,
1573 (dma_addr_t *)skb->head);
1574 #ifdef CONFIG_NEED_DMA_MAP_STATE
1575 skb->dev = q->adap->port[0];
1576 skb->destructor = deferred_unmap_destructor;
1578 last_desc = q->q.pidx + ndesc - 1;
1579 if (last_desc >= q->q.size)
1580 last_desc -= q->q.size;
1581 q->q.sdesc[last_desc].skb = skb;
1584 txq_advance(&q->q, ndesc);
1586 if (unlikely(written > 32)) {
1587 ring_tx_db(q->adap, &q->q, written);
1591 spin_lock(&q->sendq.lock);
1592 __skb_unlink(skb, &q->sendq);
1593 if (is_ofld_imm(skb))
1596 if (likely(written))
1597 ring_tx_db(q->adap, &q->q, written);
1601 * ofld_xmit - send a packet through an offload queue
1602 * @q: the Tx offload queue
1605 * Send an offload packet through an SGE offload queue.
1607 static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
1609 skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
1610 spin_lock(&q->sendq.lock);
1611 __skb_queue_tail(&q->sendq, skb);
1612 if (q->sendq.qlen == 1)
1614 spin_unlock(&q->sendq.lock);
1615 return NET_XMIT_SUCCESS;
1619 * restart_ofldq - restart a suspended offload queue
1620 * @data: the offload queue to restart
1622 * Resumes transmission on a suspended Tx offload queue.
1624 static void restart_ofldq(unsigned long data)
1626 struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
1628 spin_lock(&q->sendq.lock);
1629 q->full = 0; /* the queue actually is completely empty now */
1631 spin_unlock(&q->sendq.lock);
1635 * skb_txq - return the Tx queue an offload packet should use
1638 * Returns the Tx queue an offload packet should use as indicated by bits
1639 * 1-15 in the packet's queue_mapping.
1641 static inline unsigned int skb_txq(const struct sk_buff *skb)
1643 return skb->queue_mapping >> 1;
1647 * is_ctrl_pkt - return whether an offload packet is a control packet
1650 * Returns whether an offload packet should use an OFLD or a CTRL
1651 * Tx queue as indicated by bit 0 in the packet's queue_mapping.
1653 static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
1655 return skb->queue_mapping & 1;
1658 static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
1660 unsigned int idx = skb_txq(skb);
1662 if (unlikely(is_ctrl_pkt(skb))) {
1663 /* Single ctrl queue is a requirement for LE workaround path */
1664 if (adap->tids.nsftids)
1666 return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
1668 return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
1672 * t4_ofld_send - send an offload packet
1673 * @adap: the adapter
1676 * Sends an offload packet. We use the packet queue_mapping to select the
1677 * appropriate Tx queue as follows: bit 0 indicates whether the packet
1678 * should be sent as regular or control, bits 1-15 select the queue.
1680 int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
1685 ret = ofld_send(adap, skb);
1691 * cxgb4_ofld_send - send an offload packet
1692 * @dev: the net device
1695 * Sends an offload packet. This is an exported version of @t4_ofld_send,
1696 * intended for ULDs.
1698 int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
1700 return t4_ofld_send(netdev2adap(dev), skb);
1702 EXPORT_SYMBOL(cxgb4_ofld_send);
1704 static inline void copy_frags(struct sk_buff *skb,
1705 const struct pkt_gl *gl, unsigned int offset)
1709 /* usually there's just one frag */
1710 __skb_fill_page_desc(skb, 0, gl->frags[0].page,
1711 gl->frags[0].offset + offset,
1712 gl->frags[0].size - offset);
1713 skb_shinfo(skb)->nr_frags = gl->nfrags;
1714 for (i = 1; i < gl->nfrags; i++)
1715 __skb_fill_page_desc(skb, i, gl->frags[i].page,
1716 gl->frags[i].offset,
1719 /* get a reference to the last page, we don't own it */
1720 get_page(gl->frags[gl->nfrags - 1].page);
1724 * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
1725 * @gl: the gather list
1726 * @skb_len: size of sk_buff main body if it carries fragments
1727 * @pull_len: amount of data to move to the sk_buff's main body
1729 * Builds an sk_buff from the given packet gather list. Returns the
1730 * sk_buff or %NULL if sk_buff allocation failed.
1732 struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
1733 unsigned int skb_len, unsigned int pull_len)
1735 struct sk_buff *skb;
1738 * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
1739 * size, which is expected since buffers are at least PAGE_SIZEd.
1740 * In this case packets up to RX_COPY_THRES have only one fragment.
1742 if (gl->tot_len <= RX_COPY_THRES) {
1743 skb = dev_alloc_skb(gl->tot_len);
1746 __skb_put(skb, gl->tot_len);
1747 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1749 skb = dev_alloc_skb(skb_len);
1752 __skb_put(skb, pull_len);
1753 skb_copy_to_linear_data(skb, gl->va, pull_len);
1755 copy_frags(skb, gl, pull_len);
1756 skb->len = gl->tot_len;
1757 skb->data_len = skb->len - pull_len;
1758 skb->truesize += skb->data_len;
1762 EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
1765 * t4_pktgl_free - free a packet gather list
1766 * @gl: the gather list
1768 * Releases the pages of a packet gather list. We do not own the last
1769 * page on the list and do not free it.
1771 static void t4_pktgl_free(const struct pkt_gl *gl)
1774 const struct page_frag *p;
1776 for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
1781 * Process an MPS trace packet. Give it an unused protocol number so it won't
1782 * be delivered to anyone and send it to the stack for capture.
1784 static noinline int handle_trace_pkt(struct adapter *adap,
1785 const struct pkt_gl *gl)
1787 struct sk_buff *skb;
1789 skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
1790 if (unlikely(!skb)) {
1795 if (is_t4(adap->params.chip))
1796 __skb_pull(skb, sizeof(struct cpl_trace_pkt));
1798 __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
1800 skb_reset_mac_header(skb);
1801 skb->protocol = htons(0xffff);
1802 skb->dev = adap->port[0];
1803 netif_receive_skb(skb);
1807 static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1808 const struct cpl_rx_pkt *pkt)
1810 struct adapter *adapter = rxq->rspq.adap;
1811 struct sge *s = &adapter->sge;
1813 struct sk_buff *skb;
1815 skb = napi_get_frags(&rxq->rspq.napi);
1816 if (unlikely(!skb)) {
1818 rxq->stats.rx_drops++;
1822 copy_frags(skb, gl, s->pktshift);
1823 skb->len = gl->tot_len - s->pktshift;
1824 skb->data_len = skb->len;
1825 skb->truesize += skb->data_len;
1826 skb->ip_summed = CHECKSUM_UNNECESSARY;
1827 skb_record_rx_queue(skb, rxq->rspq.idx);
1828 skb_mark_napi_id(skb, &rxq->rspq.napi);
1829 if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
1830 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
1833 if (unlikely(pkt->vlan_ex)) {
1834 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
1835 rxq->stats.vlan_ex++;
1837 ret = napi_gro_frags(&rxq->rspq.napi);
1838 if (ret == GRO_HELD)
1839 rxq->stats.lro_pkts++;
1840 else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1841 rxq->stats.lro_merged++;
1843 rxq->stats.rx_cso++;
1847 * t4_ethrx_handler - process an ingress ethernet packet
1848 * @q: the response queue that received the packet
1849 * @rsp: the response queue descriptor holding the RX_PKT message
1850 * @si: the gather list of packet fragments
1852 * Process an ingress ethernet packet and deliver it to the stack.
1854 int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
1855 const struct pkt_gl *si)
1858 struct sk_buff *skb;
1859 const struct cpl_rx_pkt *pkt;
1860 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1861 struct sge *s = &q->adap->sge;
1862 int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
1863 CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
1864 #ifdef CONFIG_CHELSIO_T4_FCOE
1865 struct port_info *pi;
1868 if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
1869 return handle_trace_pkt(q->adap, si);
1871 pkt = (const struct cpl_rx_pkt *)rsp;
1872 csum_ok = pkt->csum_calc && !pkt->err_vec &&
1873 (q->netdev->features & NETIF_F_RXCSUM);
1874 if ((pkt->l2info & htonl(RXF_TCP_F)) &&
1875 !(cxgb_poll_busy_polling(q)) &&
1876 (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
1877 do_gro(rxq, si, pkt);
1881 skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
1882 if (unlikely(!skb)) {
1884 rxq->stats.rx_drops++;
1888 __skb_pull(skb, s->pktshift); /* remove ethernet header padding */
1889 skb->protocol = eth_type_trans(skb, q->netdev);
1890 skb_record_rx_queue(skb, q->idx);
1891 if (skb->dev->features & NETIF_F_RXHASH)
1892 skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
1897 if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
1898 if (!pkt->ip_frag) {
1899 skb->ip_summed = CHECKSUM_UNNECESSARY;
1900 rxq->stats.rx_cso++;
1901 } else if (pkt->l2info & htonl(RXF_IP_F)) {
1902 __sum16 c = (__force __sum16)pkt->csum;
1903 skb->csum = csum_unfold(c);
1904 skb->ip_summed = CHECKSUM_COMPLETE;
1905 rxq->stats.rx_cso++;
1908 skb_checksum_none_assert(skb);
1909 #ifdef CONFIG_CHELSIO_T4_FCOE
1910 #define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
1911 RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
1913 pi = netdev_priv(skb->dev);
1914 if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
1915 if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
1916 (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
1917 if (!(pkt->err_vec & cpu_to_be16(RXERR_CSUM_F)))
1918 skb->ip_summed = CHECKSUM_UNNECESSARY;
1922 #undef CPL_RX_PKT_FLAGS
1923 #endif /* CONFIG_CHELSIO_T4_FCOE */
1926 if (unlikely(pkt->vlan_ex)) {
1927 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
1928 rxq->stats.vlan_ex++;
1930 skb_mark_napi_id(skb, &q->napi);
1931 netif_receive_skb(skb);
1936 * restore_rx_bufs - put back a packet's Rx buffers
1937 * @si: the packet gather list
1938 * @q: the SGE free list
1939 * @frags: number of FL buffers to restore
1941 * Puts back on an FL the Rx buffers associated with @si. The buffers
1942 * have already been unmapped and are left unmapped, we mark them so to
1943 * prevent further unmapping attempts.
1945 * This function undoes a series of @unmap_rx_buf calls when we find out
1946 * that the current packet can't be processed right away afterall and we
1947 * need to come back to it later. This is a very rare event and there's
1948 * no effort to make this particularly efficient.
1950 static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
1953 struct rx_sw_desc *d;
1957 q->cidx = q->size - 1;
1960 d = &q->sdesc[q->cidx];
1961 d->page = si->frags[frags].page;
1962 d->dma_addr |= RX_UNMAPPED_BUF;
1968 * is_new_response - check if a response is newly written
1969 * @r: the response descriptor
1970 * @q: the response queue
1972 * Returns true if a response descriptor contains a yet unprocessed
1975 static inline bool is_new_response(const struct rsp_ctrl *r,
1976 const struct sge_rspq *q)
1978 return (r->type_gen >> RSPD_GEN_S) == q->gen;
1982 * rspq_next - advance to the next entry in a response queue
1985 * Updates the state of a response queue to advance it to the next entry.
1987 static inline void rspq_next(struct sge_rspq *q)
1989 q->cur_desc = (void *)q->cur_desc + q->iqe_len;
1990 if (unlikely(++q->cidx == q->size)) {
1993 q->cur_desc = q->desc;
1998 * process_responses - process responses from an SGE response queue
1999 * @q: the ingress queue to process
2000 * @budget: how many responses can be processed in this round
2002 * Process responses from an SGE response queue up to the supplied budget.
2003 * Responses include received packets as well as control messages from FW
2006 * Additionally choose the interrupt holdoff time for the next interrupt
2007 * on this queue. If the system is under memory shortage use a fairly
2008 * long delay to help recovery.
2010 static int process_responses(struct sge_rspq *q, int budget)
2013 int budget_left = budget;
2014 const struct rsp_ctrl *rc;
2015 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
2016 struct adapter *adapter = q->adap;
2017 struct sge *s = &adapter->sge;
2019 while (likely(budget_left)) {
2020 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
2021 if (!is_new_response(rc, q))
2025 rsp_type = RSPD_TYPE_G(rc->type_gen);
2026 if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
2027 struct page_frag *fp;
2029 const struct rx_sw_desc *rsd;
2030 u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
2032 if (len & RSPD_NEWBUF_F) {
2033 if (likely(q->offset > 0)) {
2034 free_rx_bufs(q->adap, &rxq->fl, 1);
2037 len = RSPD_LEN_G(len);
2041 /* gather packet fragments */
2042 for (frags = 0, fp = si.frags; ; frags++, fp++) {
2043 rsd = &rxq->fl.sdesc[rxq->fl.cidx];
2044 bufsz = get_buf_size(adapter, rsd);
2045 fp->page = rsd->page;
2046 fp->offset = q->offset;
2047 fp->size = min(bufsz, len);
2051 unmap_rx_buf(q->adap, &rxq->fl);
2055 * Last buffer remains mapped so explicitly make it
2056 * coherent for CPU access.
2058 dma_sync_single_for_cpu(q->adap->pdev_dev,
2060 fp->size, DMA_FROM_DEVICE);
2062 si.va = page_address(si.frags[0].page) +
2066 si.nfrags = frags + 1;
2067 ret = q->handler(q, q->cur_desc, &si);
2068 if (likely(ret == 0))
2069 q->offset += ALIGN(fp->size, s->fl_align);
2071 restore_rx_bufs(&si, &rxq->fl, frags);
2072 } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
2073 ret = q->handler(q, q->cur_desc, NULL);
2075 ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
2078 if (unlikely(ret)) {
2079 /* couldn't process descriptor, back off for recovery */
2080 q->next_intr_params = QINTR_TIMER_IDX_V(NOMEM_TMR_IDX);
2088 if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
2089 __refill_fl(q->adap, &rxq->fl);
2090 return budget - budget_left;
2093 #ifdef CONFIG_NET_RX_BUSY_POLL
2094 int cxgb_busy_poll(struct napi_struct *napi)
2096 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
2097 unsigned int params, work_done;
2100 if (!cxgb_poll_lock_poll(q))
2101 return LL_FLUSH_BUSY;
2103 work_done = process_responses(q, 4);
2104 params = QINTR_TIMER_IDX_V(TIMERREG_COUNTER0_X) | QINTR_CNT_EN_V(1);
2105 q->next_intr_params = params;
2106 val = CIDXINC_V(work_done) | SEINTARM_V(params);
2108 /* If we don't have access to the new User GTS (T5+), use the old
2109 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2111 if (unlikely(!q->bar2_addr))
2112 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
2113 val | INGRESSQID_V((u32)q->cntxt_id));
2115 writel(val | INGRESSQID_V(q->bar2_qid),
2116 q->bar2_addr + SGE_UDB_GTS);
2120 cxgb_poll_unlock_poll(q);
2123 #endif /* CONFIG_NET_RX_BUSY_POLL */
2126 * napi_rx_handler - the NAPI handler for Rx processing
2127 * @napi: the napi instance
2128 * @budget: how many packets we can process in this round
2130 * Handler for new data events when using NAPI. This does not need any
2131 * locking or protection from interrupts as data interrupts are off at
2132 * this point and other adapter interrupts do not interfere (the latter
2133 * in not a concern at all with MSI-X as non-data interrupts then have
2134 * a separate handler).
2136 static int napi_rx_handler(struct napi_struct *napi, int budget)
2138 unsigned int params;
2139 struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
2143 if (!cxgb_poll_lock_napi(q))
2146 work_done = process_responses(q, budget);
2147 if (likely(work_done < budget)) {
2150 napi_complete(napi);
2151 timer_index = QINTR_TIMER_IDX_G(q->next_intr_params);
2153 if (q->adaptive_rx) {
2154 if (work_done > max(timer_pkt_quota[timer_index],
2156 timer_index = (timer_index + 1);
2158 timer_index = timer_index - 1;
2160 timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
2161 q->next_intr_params =
2162 QINTR_TIMER_IDX_V(timer_index) |
2164 params = q->next_intr_params;
2166 params = q->next_intr_params;
2167 q->next_intr_params = q->intr_params;
2170 params = QINTR_TIMER_IDX_V(7);
2172 val = CIDXINC_V(work_done) | SEINTARM_V(params);
2174 /* If we don't have access to the new User GTS (T5+), use the old
2175 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2177 if (unlikely(q->bar2_addr == NULL)) {
2178 t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
2179 val | INGRESSQID_V((u32)q->cntxt_id));
2181 writel(val | INGRESSQID_V(q->bar2_qid),
2182 q->bar2_addr + SGE_UDB_GTS);
2185 cxgb_poll_unlock_napi(q);
2190 * The MSI-X interrupt handler for an SGE response queue.
2192 irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
2194 struct sge_rspq *q = cookie;
2196 napi_schedule(&q->napi);
2201 * Process the indirect interrupt entries in the interrupt queue and kick off
2202 * NAPI for each queue that has generated an entry.
2204 static unsigned int process_intrq(struct adapter *adap)
2206 unsigned int credits;
2207 const struct rsp_ctrl *rc;
2208 struct sge_rspq *q = &adap->sge.intrq;
2211 spin_lock(&adap->sge.intrq_lock);
2212 for (credits = 0; ; credits++) {
2213 rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
2214 if (!is_new_response(rc, q))
2218 if (RSPD_TYPE_G(rc->type_gen) == RSPD_TYPE_INTR_X) {
2219 unsigned int qid = ntohl(rc->pldbuflen_qid);
2221 qid -= adap->sge.ingr_start;
2222 napi_schedule(&adap->sge.ingr_map[qid]->napi);
2228 val = CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
2230 /* If we don't have access to the new User GTS (T5+), use the old
2231 * doorbell mechanism; otherwise use the new BAR2 mechanism.
2233 if (unlikely(q->bar2_addr == NULL)) {
2234 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
2235 val | INGRESSQID_V(q->cntxt_id));
2237 writel(val | INGRESSQID_V(q->bar2_qid),
2238 q->bar2_addr + SGE_UDB_GTS);
2241 spin_unlock(&adap->sge.intrq_lock);
2246 * The MSI interrupt handler, which handles data events from SGE response queues
2247 * as well as error and other async events as they all use the same MSI vector.
2249 static irqreturn_t t4_intr_msi(int irq, void *cookie)
2251 struct adapter *adap = cookie;
2253 if (adap->flags & MASTER_PF)
2254 t4_slow_intr_handler(adap);
2255 process_intrq(adap);
2260 * Interrupt handler for legacy INTx interrupts.
2261 * Handles data events from SGE response queues as well as error and other
2262 * async events as they all use the same interrupt line.
2264 static irqreturn_t t4_intr_intx(int irq, void *cookie)
2266 struct adapter *adap = cookie;
2268 t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
2269 if (((adap->flags & MASTER_PF) && t4_slow_intr_handler(adap)) |
2270 process_intrq(adap))
2272 return IRQ_NONE; /* probably shared interrupt */
2276 * t4_intr_handler - select the top-level interrupt handler
2277 * @adap: the adapter
2279 * Selects the top-level interrupt handler based on the type of interrupts
2280 * (MSI-X, MSI, or INTx).
2282 irq_handler_t t4_intr_handler(struct adapter *adap)
2284 if (adap->flags & USING_MSIX)
2285 return t4_sge_intr_msix;
2286 if (adap->flags & USING_MSI)
2288 return t4_intr_intx;
2291 static void sge_rx_timer_cb(unsigned long data)
2295 struct adapter *adap = (struct adapter *)data;
2296 struct sge *s = &adap->sge;
2298 for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
2299 for (m = s->starving_fl[i]; m; m &= m - 1) {
2300 struct sge_eth_rxq *rxq;
2301 unsigned int id = __ffs(m) + i * BITS_PER_LONG;
2302 struct sge_fl *fl = s->egr_map[id];
2304 clear_bit(id, s->starving_fl);
2305 smp_mb__after_atomic();
2307 if (fl_starving(adap, fl)) {
2308 rxq = container_of(fl, struct sge_eth_rxq, fl);
2309 if (napi_reschedule(&rxq->rspq.napi))
2312 set_bit(id, s->starving_fl);
2315 /* The remainder of the SGE RX Timer Callback routine is dedicated to
2316 * global Master PF activities like checking for chip ingress stalls,
2319 if (!(adap->flags & MASTER_PF))
2322 t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
2325 mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
2328 static void sge_tx_timer_cb(unsigned long data)
2331 unsigned int i, budget;
2332 struct adapter *adap = (struct adapter *)data;
2333 struct sge *s = &adap->sge;
2335 for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
2336 for (m = s->txq_maperr[i]; m; m &= m - 1) {
2337 unsigned long id = __ffs(m) + i * BITS_PER_LONG;
2338 struct sge_ofld_txq *txq = s->egr_map[id];
2340 clear_bit(id, s->txq_maperr);
2341 tasklet_schedule(&txq->qresume_tsk);
2344 budget = MAX_TIMER_TX_RECLAIM;
2345 i = s->ethtxq_rover;
2347 struct sge_eth_txq *q = &s->ethtxq[i];
2350 time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
2351 __netif_tx_trylock(q->txq)) {
2352 int avail = reclaimable(&q->q);
2358 free_tx_desc(adap, &q->q, avail, true);
2359 q->q.in_use -= avail;
2362 __netif_tx_unlock(q->txq);
2365 if (++i >= s->ethqsets)
2367 } while (budget && i != s->ethtxq_rover);
2368 s->ethtxq_rover = i;
2369 mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
2373 * bar2_address - return the BAR2 address for an SGE Queue's Registers
2374 * @adapter: the adapter
2375 * @qid: the SGE Queue ID
2376 * @qtype: the SGE Queue Type (Egress or Ingress)
2377 * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
2379 * Returns the BAR2 address for the SGE Queue Registers associated with
2380 * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
2381 * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
2382 * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
2383 * Registers are supported (e.g. the Write Combining Doorbell Buffer).
2385 static void __iomem *bar2_address(struct adapter *adapter,
2387 enum t4_bar2_qtype qtype,
2388 unsigned int *pbar2_qid)
2393 ret = cxgb4_t4_bar2_sge_qregs(adapter, qid, qtype,
2394 &bar2_qoffset, pbar2_qid);
2398 return adapter->bar2 + bar2_qoffset;
2401 /* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
2402 * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
2404 int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
2405 struct net_device *dev, int intr_idx,
2406 struct sge_fl *fl, rspq_handler_t hnd, int cong)
2410 struct sge *s = &adap->sge;
2411 struct port_info *pi = netdev_priv(dev);
2413 /* Size needs to be multiple of 16, including status entry. */
2414 iq->size = roundup(iq->size, 16);
2416 iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
2417 &iq->phys_addr, NULL, 0, NUMA_NO_NODE);
2421 memset(&c, 0, sizeof(c));
2422 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
2423 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
2424 FW_IQ_CMD_PFN_V(adap->fn) | FW_IQ_CMD_VFN_V(0));
2425 c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
2427 c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
2428 FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
2429 FW_IQ_CMD_IQANDST_V(intr_idx < 0) |
2430 FW_IQ_CMD_IQANUD_V(UPDATEDELIVERY_INTERRUPT_X) |
2431 FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
2433 c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
2434 FW_IQ_CMD_IQGTSMODE_F |
2435 FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
2436 FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
2437 c.iqsize = htons(iq->size);
2438 c.iqaddr = cpu_to_be64(iq->phys_addr);
2440 c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F);
2443 /* Allocate the ring for the hardware free list (with space
2444 * for its status page) along with the associated software
2445 * descriptor ring. The free list size needs to be a multiple
2446 * of the Egress Queue Unit and at least 2 Egress Units larger
2447 * than the SGE's Egress Congrestion Threshold
2448 * (fl_starve_thres - 1).
2450 if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
2451 fl->size = s->fl_starve_thres - 1 + 2 * 8;
2452 fl->size = roundup(fl->size, 8);
2453 fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
2454 sizeof(struct rx_sw_desc), &fl->addr,
2455 &fl->sdesc, s->stat_len, NUMA_NO_NODE);
2459 flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
2460 c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
2461 FW_IQ_CMD_FL0FETCHRO_F |
2462 FW_IQ_CMD_FL0DATARO_F |
2463 FW_IQ_CMD_FL0PADEN_F);
2465 c.iqns_to_fl0congen |=
2466 htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
2467 FW_IQ_CMD_FL0CONGCIF_F |
2468 FW_IQ_CMD_FL0CONGEN_F);
2469 c.fl0dcaen_to_fl0cidxfthresh =
2470 htons(FW_IQ_CMD_FL0FBMIN_V(FETCHBURSTMIN_64B_X) |
2471 FW_IQ_CMD_FL0FBMAX_V(FETCHBURSTMAX_512B_X));
2472 c.fl0size = htons(flsz);
2473 c.fl0addr = cpu_to_be64(fl->addr);
2476 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2480 netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
2481 napi_hash_add(&iq->napi);
2482 iq->cur_desc = iq->desc;
2485 iq->next_intr_params = iq->intr_params;
2486 iq->cntxt_id = ntohs(c.iqid);
2487 iq->abs_id = ntohs(c.physiqid);
2488 iq->bar2_addr = bar2_address(adap,
2490 T4_BAR2_QTYPE_INGRESS,
2492 iq->size--; /* subtract status entry */
2496 /* set offset to -1 to distinguish ingress queues without FL */
2497 iq->offset = fl ? 0 : -1;
2499 adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
2502 fl->cntxt_id = ntohs(c.fl0id);
2503 fl->avail = fl->pend_cred = 0;
2504 fl->pidx = fl->cidx = 0;
2505 fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
2506 adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
2508 /* Note, we must initialize the BAR2 Free List User Doorbell
2509 * information before refilling the Free List!
2511 fl->bar2_addr = bar2_address(adap,
2513 T4_BAR2_QTYPE_EGRESS,
2515 refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
2518 /* For T5 and later we attempt to set up the Congestion Manager values
2519 * of the new RX Ethernet Queue. This should really be handled by
2520 * firmware because it's more complex than any host driver wants to
2521 * get involved with and it's different per chip and this is almost
2522 * certainly wrong. Firmware would be wrong as well, but it would be
2523 * a lot easier to fix in one place ... For now we do something very
2524 * simple (and hopefully less wrong).
2526 if (!is_t4(adap->params.chip) && cong >= 0) {
2530 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
2531 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
2532 FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
2534 val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
2537 CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
2538 for (i = 0; i < 4; i++) {
2539 if (cong & (1 << i))
2541 CONMCTXT_CNGCHMAP_V(1 << (i << 2));
2544 ret = t4_set_params(adap, adap->mbox, adap->fn, 0, 1,
2547 dev_warn(adap->pdev_dev, "Failed to set Congestion"
2548 " Manager Context for Ingress Queue %d: %d\n",
2549 iq->cntxt_id, -ret);
2558 dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
2559 iq->desc, iq->phys_addr);
2562 if (fl && fl->desc) {
2565 dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
2566 fl->desc, fl->addr);
2572 static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
2575 q->bar2_addr = bar2_address(adap,
2577 T4_BAR2_QTYPE_EGRESS,
2580 q->cidx = q->pidx = 0;
2581 q->stops = q->restarts = 0;
2582 q->stat = (void *)&q->desc[q->size];
2583 spin_lock_init(&q->db_lock);
2584 adap->sge.egr_map[id - adap->sge.egr_start] = q;
2587 int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
2588 struct net_device *dev, struct netdev_queue *netdevq,
2592 struct fw_eq_eth_cmd c;
2593 struct sge *s = &adap->sge;
2594 struct port_info *pi = netdev_priv(dev);
2596 /* Add status entries */
2597 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
2599 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2600 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
2601 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
2602 netdev_queue_numa_node_read(netdevq));
2606 memset(&c, 0, sizeof(c));
2607 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
2608 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
2609 FW_EQ_ETH_CMD_PFN_V(adap->fn) |
2610 FW_EQ_ETH_CMD_VFN_V(0));
2611 c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
2612 FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
2613 c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
2614 FW_EQ_ETH_CMD_VIID_V(pi->viid));
2615 c.fetchszm_to_iqid =
2616 htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
2617 FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
2618 FW_EQ_ETH_CMD_FETCHRO_F | FW_EQ_ETH_CMD_IQID_V(iqid));
2620 htonl(FW_EQ_ETH_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
2621 FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
2622 FW_EQ_ETH_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
2623 FW_EQ_ETH_CMD_EQSIZE_V(nentries));
2624 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2626 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2628 kfree(txq->q.sdesc);
2629 txq->q.sdesc = NULL;
2630 dma_free_coherent(adap->pdev_dev,
2631 nentries * sizeof(struct tx_desc),
2632 txq->q.desc, txq->q.phys_addr);
2637 init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
2639 txq->tso = txq->tx_cso = txq->vlan_ins = 0;
2640 txq->mapping_err = 0;
2644 int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
2645 struct net_device *dev, unsigned int iqid,
2646 unsigned int cmplqid)
2649 struct fw_eq_ctrl_cmd c;
2650 struct sge *s = &adap->sge;
2651 struct port_info *pi = netdev_priv(dev);
2653 /* Add status entries */
2654 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
2656 txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
2657 sizeof(struct tx_desc), 0, &txq->q.phys_addr,
2658 NULL, 0, dev_to_node(adap->pdev_dev));
2662 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
2663 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
2664 FW_EQ_CTRL_CMD_PFN_V(adap->fn) |
2665 FW_EQ_CTRL_CMD_VFN_V(0));
2666 c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
2667 FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
2668 c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
2669 c.physeqid_pkd = htonl(0);
2670 c.fetchszm_to_iqid =
2671 htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
2672 FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
2673 FW_EQ_CTRL_CMD_FETCHRO_F | FW_EQ_CTRL_CMD_IQID_V(iqid));
2675 htonl(FW_EQ_CTRL_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
2676 FW_EQ_CTRL_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
2677 FW_EQ_CTRL_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
2678 FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
2679 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2681 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2683 dma_free_coherent(adap->pdev_dev,
2684 nentries * sizeof(struct tx_desc),
2685 txq->q.desc, txq->q.phys_addr);
2690 init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
2692 skb_queue_head_init(&txq->sendq);
2693 tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
2698 int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
2699 struct net_device *dev, unsigned int iqid)
2702 struct fw_eq_ofld_cmd c;
2703 struct sge *s = &adap->sge;
2704 struct port_info *pi = netdev_priv(dev);
2706 /* Add status entries */
2707 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
2709 txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2710 sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
2711 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
2716 memset(&c, 0, sizeof(c));
2717 c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST_F |
2718 FW_CMD_WRITE_F | FW_CMD_EXEC_F |
2719 FW_EQ_OFLD_CMD_PFN_V(adap->fn) |
2720 FW_EQ_OFLD_CMD_VFN_V(0));
2721 c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
2722 FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
2723 c.fetchszm_to_iqid =
2724 htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
2725 FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
2726 FW_EQ_OFLD_CMD_FETCHRO_F | FW_EQ_OFLD_CMD_IQID_V(iqid));
2728 htonl(FW_EQ_OFLD_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
2729 FW_EQ_OFLD_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
2730 FW_EQ_OFLD_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
2731 FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
2732 c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2734 ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2736 kfree(txq->q.sdesc);
2737 txq->q.sdesc = NULL;
2738 dma_free_coherent(adap->pdev_dev,
2739 nentries * sizeof(struct tx_desc),
2740 txq->q.desc, txq->q.phys_addr);
2745 init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
2747 skb_queue_head_init(&txq->sendq);
2748 tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
2750 txq->mapping_err = 0;
2754 static void free_txq(struct adapter *adap, struct sge_txq *q)
2756 struct sge *s = &adap->sge;
2758 dma_free_coherent(adap->pdev_dev,
2759 q->size * sizeof(struct tx_desc) + s->stat_len,
2760 q->desc, q->phys_addr);
2766 static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
2769 struct sge *s = &adap->sge;
2770 unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
2772 adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
2773 t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
2774 rq->cntxt_id, fl_id, 0xffff);
2775 dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
2776 rq->desc, rq->phys_addr);
2777 napi_hash_del(&rq->napi);
2778 netif_napi_del(&rq->napi);
2780 rq->cntxt_id = rq->abs_id = 0;
2784 free_rx_bufs(adap, fl, fl->avail);
2785 dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
2786 fl->desc, fl->addr);
2795 * t4_free_ofld_rxqs - free a block of consecutive Rx queues
2796 * @adap: the adapter
2797 * @n: number of queues
2798 * @q: pointer to first queue
2800 * Release the resources of a consecutive block of offload Rx queues.
2802 void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
2804 for ( ; n; n--, q++)
2806 free_rspq_fl(adap, &q->rspq,
2807 q->fl.size ? &q->fl : NULL);
2811 * t4_free_sge_resources - free SGE resources
2812 * @adap: the adapter
2814 * Frees resources used by the SGE queue sets.
2816 void t4_free_sge_resources(struct adapter *adap)
2819 struct sge_eth_rxq *eq = adap->sge.ethrxq;
2820 struct sge_eth_txq *etq = adap->sge.ethtxq;
2822 /* clean up Ethernet Tx/Rx queues */
2823 for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
2825 free_rspq_fl(adap, &eq->rspq,
2826 eq->fl.size ? &eq->fl : NULL);
2828 t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
2830 free_tx_desc(adap, &etq->q, etq->q.in_use, true);
2831 kfree(etq->q.sdesc);
2832 free_txq(adap, &etq->q);
2836 /* clean up RDMA and iSCSI Rx queues */
2837 t4_free_ofld_rxqs(adap, adap->sge.ofldqsets, adap->sge.ofldrxq);
2838 t4_free_ofld_rxqs(adap, adap->sge.rdmaqs, adap->sge.rdmarxq);
2839 t4_free_ofld_rxqs(adap, adap->sge.rdmaciqs, adap->sge.rdmaciq);
2841 /* clean up offload Tx queues */
2842 for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
2843 struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
2846 tasklet_kill(&q->qresume_tsk);
2847 t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
2849 free_tx_desc(adap, &q->q, q->q.in_use, false);
2851 __skb_queue_purge(&q->sendq);
2852 free_txq(adap, &q->q);
2856 /* clean up control Tx queues */
2857 for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
2858 struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
2861 tasklet_kill(&cq->qresume_tsk);
2862 t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
2864 __skb_queue_purge(&cq->sendq);
2865 free_txq(adap, &cq->q);
2869 if (adap->sge.fw_evtq.desc)
2870 free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
2872 if (adap->sge.intrq.desc)
2873 free_rspq_fl(adap, &adap->sge.intrq, NULL);
2875 /* clear the reverse egress queue map */
2876 memset(adap->sge.egr_map, 0,
2877 adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
2880 void t4_sge_start(struct adapter *adap)
2882 adap->sge.ethtxq_rover = 0;
2883 mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2884 mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2888 * t4_sge_stop - disable SGE operation
2889 * @adap: the adapter
2891 * Stop tasklets and timers associated with the DMA engine. Note that
2892 * this is effective only if measures have been taken to disable any HW
2893 * events that may restart them.
2895 void t4_sge_stop(struct adapter *adap)
2898 struct sge *s = &adap->sge;
2900 if (in_interrupt()) /* actions below require waiting */
2903 if (s->rx_timer.function)
2904 del_timer_sync(&s->rx_timer);
2905 if (s->tx_timer.function)
2906 del_timer_sync(&s->tx_timer);
2908 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
2909 struct sge_ofld_txq *q = &s->ofldtxq[i];
2912 tasklet_kill(&q->qresume_tsk);
2914 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
2915 struct sge_ctrl_txq *cq = &s->ctrlq[i];
2918 tasklet_kill(&cq->qresume_tsk);
2923 * t4_sge_init_soft - grab core SGE values needed by SGE code
2924 * @adap: the adapter
2926 * We need to grab the SGE operating parameters that we need to have
2927 * in order to do our job and make sure we can live with them.
2930 static int t4_sge_init_soft(struct adapter *adap)
2932 struct sge *s = &adap->sge;
2933 u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
2934 u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
2935 u32 ingress_rx_threshold;
2938 * Verify that CPL messages are going to the Ingress Queue for
2939 * process_responses() and that only packet data is going to the
2942 if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
2943 RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
2944 dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
2949 * Validate the Host Buffer Register Array indices that we want to
2952 * XXX Note that we should really read through the Host Buffer Size
2953 * XXX register array and find the indices of the Buffer Sizes which
2954 * XXX meet our needs!
2956 #define READ_FL_BUF(x) \
2957 t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
2959 fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
2960 fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
2961 fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
2962 fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
2964 /* We only bother using the Large Page logic if the Large Page Buffer
2965 * is larger than our Page Size Buffer.
2967 if (fl_large_pg <= fl_small_pg)
2972 /* The Page Size Buffer must be exactly equal to our Page Size and the
2973 * Large Page Size Buffer should be 0 (per above) or a power of 2.
2975 if (fl_small_pg != PAGE_SIZE ||
2976 (fl_large_pg & (fl_large_pg-1)) != 0) {
2977 dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
2978 fl_small_pg, fl_large_pg);
2982 s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
2984 if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
2985 fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
2986 dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
2987 fl_small_mtu, fl_large_mtu);
2992 * Retrieve our RX interrupt holdoff timer values and counter
2993 * threshold values from the SGE parameters.
2995 timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
2996 timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
2997 timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
2998 s->timer_val[0] = core_ticks_to_us(adap,
2999 TIMERVALUE0_G(timer_value_0_and_1));
3000 s->timer_val[1] = core_ticks_to_us(adap,
3001 TIMERVALUE1_G(timer_value_0_and_1));
3002 s->timer_val[2] = core_ticks_to_us(adap,
3003 TIMERVALUE2_G(timer_value_2_and_3));
3004 s->timer_val[3] = core_ticks_to_us(adap,
3005 TIMERVALUE3_G(timer_value_2_and_3));
3006 s->timer_val[4] = core_ticks_to_us(adap,
3007 TIMERVALUE4_G(timer_value_4_and_5));
3008 s->timer_val[5] = core_ticks_to_us(adap,
3009 TIMERVALUE5_G(timer_value_4_and_5));
3011 ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
3012 s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
3013 s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
3014 s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
3015 s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
3021 * t4_sge_init - initialize SGE
3022 * @adap: the adapter
3024 * Perform low-level SGE code initialization needed every time after a
3027 int t4_sge_init(struct adapter *adap)
3029 struct sge *s = &adap->sge;
3030 u32 sge_control, sge_control2, sge_conm_ctrl;
3031 unsigned int ingpadboundary, ingpackboundary;
3032 int ret, egress_threshold;
3035 * Ingress Padding Boundary and Egress Status Page Size are set up by
3036 * t4_fixup_host_params().
3038 sge_control = t4_read_reg(adap, SGE_CONTROL_A);
3039 s->pktshift = PKTSHIFT_G(sge_control);
3040 s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
3042 /* T4 uses a single control field to specify both the PCIe Padding and
3043 * Packing Boundary. T5 introduced the ability to specify these
3044 * separately. The actual Ingress Packet Data alignment boundary
3045 * within Packed Buffer Mode is the maximum of these two
3046 * specifications. (Note that it makes no real practical sense to
3047 * have the Pading Boudary be larger than the Packing Boundary but you
3048 * could set the chip up that way and, in fact, legacy T4 code would
3049 * end doing this because it would initialize the Padding Boundary and
3050 * leave the Packing Boundary initialized to 0 (16 bytes).)
3052 ingpadboundary = 1 << (INGPADBOUNDARY_G(sge_control) +
3053 INGPADBOUNDARY_SHIFT_X);
3054 if (is_t4(adap->params.chip)) {
3055 s->fl_align = ingpadboundary;
3057 /* T5 has a different interpretation of one of the PCIe Packing
3060 sge_control2 = t4_read_reg(adap, SGE_CONTROL2_A);
3061 ingpackboundary = INGPACKBOUNDARY_G(sge_control2);
3062 if (ingpackboundary == INGPACKBOUNDARY_16B_X)
3063 ingpackboundary = 16;
3065 ingpackboundary = 1 << (ingpackboundary +
3066 INGPACKBOUNDARY_SHIFT_X);
3068 s->fl_align = max(ingpadboundary, ingpackboundary);
3071 ret = t4_sge_init_soft(adap);
3076 * A FL with <= fl_starve_thres buffers is starving and a periodic
3077 * timer will attempt to refill it. This needs to be larger than the
3078 * SGE's Egress Congestion Threshold. If it isn't, then we can get
3079 * stuck waiting for new packets while the SGE is waiting for us to
3080 * give it more Free List entries. (Note that the SGE's Egress
3081 * Congestion Threshold is in units of 2 Free List pointers.) For T4,
3082 * there was only a single field to control this. For T5 there's the
3083 * original field which now only applies to Unpacked Mode Free List
3084 * buffers and a new field which only applies to Packed Mode Free List
3087 sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
3088 if (is_t4(adap->params.chip))
3089 egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
3091 egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
3092 s->fl_starve_thres = 2*egress_threshold + 1;
3094 t4_idma_monitor_init(adap, &s->idma_monitor);
3096 /* Set up timers used for recuring callbacks to process RX and TX
3097 * administrative tasks.
3099 setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
3100 setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
3102 spin_lock_init(&s->intrq_lock);