1f7917c00SJeff Kirsher /*
2f7917c00SJeff Kirsher  * This file is part of the Chelsio T4 Ethernet driver for Linux.
3f7917c00SJeff Kirsher  *
4f7917c00SJeff Kirsher  * Copyright (c) 2003-2010 Chelsio Communications, Inc. All rights reserved.
5f7917c00SJeff Kirsher  *
6f7917c00SJeff Kirsher  * This software is available to you under a choice of one of two
7f7917c00SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
8f7917c00SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
9f7917c00SJeff Kirsher  * COPYING in the main directory of this source tree, or the
10f7917c00SJeff Kirsher  * OpenIB.org BSD license below:
11f7917c00SJeff Kirsher  *
12f7917c00SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
13f7917c00SJeff Kirsher  *     without modification, are permitted provided that the following
14f7917c00SJeff Kirsher  *     conditions are met:
15f7917c00SJeff Kirsher  *
16f7917c00SJeff Kirsher  *      - Redistributions of source code must retain the above
17f7917c00SJeff Kirsher  *        copyright notice, this list of conditions and the following
18f7917c00SJeff Kirsher  *        disclaimer.
19f7917c00SJeff Kirsher  *
20f7917c00SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
21f7917c00SJeff Kirsher  *        copyright notice, this list of conditions and the following
22f7917c00SJeff Kirsher  *        disclaimer in the documentation and/or other materials
23f7917c00SJeff Kirsher  *        provided with the distribution.
24f7917c00SJeff Kirsher  *
25f7917c00SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26f7917c00SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27f7917c00SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28f7917c00SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29f7917c00SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30f7917c00SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31f7917c00SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32f7917c00SJeff Kirsher  * SOFTWARE.
33f7917c00SJeff Kirsher  */
34f7917c00SJeff Kirsher 
35f7917c00SJeff Kirsher #include <linux/skbuff.h>
36f7917c00SJeff Kirsher #include <linux/netdevice.h>
37f7917c00SJeff Kirsher #include <linux/etherdevice.h>
38f7917c00SJeff Kirsher #include <linux/if_vlan.h>
39f7917c00SJeff Kirsher #include <linux/ip.h>
40f7917c00SJeff Kirsher #include <linux/dma-mapping.h>
41f7917c00SJeff Kirsher #include <linux/jiffies.h>
42f7917c00SJeff Kirsher #include <linux/prefetch.h>
43ee40fa06SPaul Gortmaker #include <linux/export.h>
44f7917c00SJeff Kirsher #include <net/ipv6.h>
45f7917c00SJeff Kirsher #include <net/tcp.h>
46f7917c00SJeff Kirsher #include "cxgb4.h"
47f7917c00SJeff Kirsher #include "t4_regs.h"
48f7917c00SJeff Kirsher #include "t4_msg.h"
49f7917c00SJeff Kirsher #include "t4fw_api.h"
50f7917c00SJeff Kirsher 
51f7917c00SJeff Kirsher /*
52f7917c00SJeff Kirsher  * Rx buffer size.  We use largish buffers if possible but settle for single
53f7917c00SJeff Kirsher  * pages under memory shortage.
54f7917c00SJeff Kirsher  */
55f7917c00SJeff Kirsher #if PAGE_SHIFT >= 16
56f7917c00SJeff Kirsher # define FL_PG_ORDER 0
57f7917c00SJeff Kirsher #else
58f7917c00SJeff Kirsher # define FL_PG_ORDER (16 - PAGE_SHIFT)
59f7917c00SJeff Kirsher #endif
60f7917c00SJeff Kirsher 
61f7917c00SJeff Kirsher /* RX_PULL_LEN should be <= RX_COPY_THRES */
62f7917c00SJeff Kirsher #define RX_COPY_THRES    256
63f7917c00SJeff Kirsher #define RX_PULL_LEN      128
64f7917c00SJeff Kirsher 
65f7917c00SJeff Kirsher /*
66f7917c00SJeff Kirsher  * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
67f7917c00SJeff Kirsher  * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
68f7917c00SJeff Kirsher  */
69f7917c00SJeff Kirsher #define RX_PKT_SKB_LEN   512
70f7917c00SJeff Kirsher 
71f7917c00SJeff Kirsher /* Ethernet header padding prepended to RX_PKTs */
72f7917c00SJeff Kirsher #define RX_PKT_PAD 2
73f7917c00SJeff Kirsher 
74f7917c00SJeff Kirsher /*
75f7917c00SJeff Kirsher  * Max number of Tx descriptors we clean up at a time.  Should be modest as
76f7917c00SJeff Kirsher  * freeing skbs isn't cheap and it happens while holding locks.  We just need
77f7917c00SJeff Kirsher  * to free packets faster than they arrive, we eventually catch up and keep
78f7917c00SJeff Kirsher  * the amortized cost reasonable.  Must be >= 2 * TXQ_STOP_THRES.
79f7917c00SJeff Kirsher  */
80f7917c00SJeff Kirsher #define MAX_TX_RECLAIM 16
81f7917c00SJeff Kirsher 
82f7917c00SJeff Kirsher /*
83f7917c00SJeff Kirsher  * Max number of Rx buffers we replenish at a time.  Again keep this modest,
84f7917c00SJeff Kirsher  * allocating buffers isn't cheap either.
85f7917c00SJeff Kirsher  */
86f7917c00SJeff Kirsher #define MAX_RX_REFILL 16U
87f7917c00SJeff Kirsher 
88f7917c00SJeff Kirsher /*
89f7917c00SJeff Kirsher  * Period of the Rx queue check timer.  This timer is infrequent as it has
90f7917c00SJeff Kirsher  * something to do only when the system experiences severe memory shortage.
91f7917c00SJeff Kirsher  */
92f7917c00SJeff Kirsher #define RX_QCHECK_PERIOD (HZ / 2)
93f7917c00SJeff Kirsher 
94f7917c00SJeff Kirsher /*
95f7917c00SJeff Kirsher  * Period of the Tx queue check timer.
96f7917c00SJeff Kirsher  */
97f7917c00SJeff Kirsher #define TX_QCHECK_PERIOD (HZ / 2)
98f7917c00SJeff Kirsher 
99f7917c00SJeff Kirsher /*
100f7917c00SJeff Kirsher  * Max number of Tx descriptors to be reclaimed by the Tx timer.
101f7917c00SJeff Kirsher  */
102f7917c00SJeff Kirsher #define MAX_TIMER_TX_RECLAIM 100
103f7917c00SJeff Kirsher 
104f7917c00SJeff Kirsher /*
105f7917c00SJeff Kirsher  * Timer index used when backing off due to memory shortage.
106f7917c00SJeff Kirsher  */
107f7917c00SJeff Kirsher #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
108f7917c00SJeff Kirsher 
109f7917c00SJeff Kirsher /*
110f7917c00SJeff Kirsher  * An FL with <= FL_STARVE_THRES buffers is starving and a periodic timer will
111f7917c00SJeff Kirsher  * attempt to refill it.
112f7917c00SJeff Kirsher  */
113f7917c00SJeff Kirsher #define FL_STARVE_THRES 4
114f7917c00SJeff Kirsher 
115f7917c00SJeff Kirsher /*
116f7917c00SJeff Kirsher  * Suspend an Ethernet Tx queue with fewer available descriptors than this.
117f7917c00SJeff Kirsher  * This is the same as calc_tx_descs() for a TSO packet with
118f7917c00SJeff Kirsher  * nr_frags == MAX_SKB_FRAGS.
119f7917c00SJeff Kirsher  */
120f7917c00SJeff Kirsher #define ETHTXQ_STOP_THRES \
121f7917c00SJeff Kirsher 	(1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
122f7917c00SJeff Kirsher 
123f7917c00SJeff Kirsher /*
124f7917c00SJeff Kirsher  * Suspension threshold for non-Ethernet Tx queues.  We require enough room
125f7917c00SJeff Kirsher  * for a full sized WR.
126f7917c00SJeff Kirsher  */
127f7917c00SJeff Kirsher #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
128f7917c00SJeff Kirsher 
129f7917c00SJeff Kirsher /*
130f7917c00SJeff Kirsher  * Max Tx descriptor space we allow for an Ethernet packet to be inlined
131f7917c00SJeff Kirsher  * into a WR.
132f7917c00SJeff Kirsher  */
133f7917c00SJeff Kirsher #define MAX_IMM_TX_PKT_LEN 128
134f7917c00SJeff Kirsher 
135f7917c00SJeff Kirsher /*
136f7917c00SJeff Kirsher  * Max size of a WR sent through a control Tx queue.
137f7917c00SJeff Kirsher  */
138f7917c00SJeff Kirsher #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
139f7917c00SJeff Kirsher 
140f7917c00SJeff Kirsher enum {
141f7917c00SJeff Kirsher 	/* packet alignment in FL buffers */
142f7917c00SJeff Kirsher 	FL_ALIGN = L1_CACHE_BYTES < 32 ? 32 : L1_CACHE_BYTES,
143f7917c00SJeff Kirsher 	/* egress status entry size */
144f7917c00SJeff Kirsher 	STAT_LEN = L1_CACHE_BYTES > 64 ? 128 : 64
145f7917c00SJeff Kirsher };
146f7917c00SJeff Kirsher 
147f7917c00SJeff Kirsher struct tx_sw_desc {                /* SW state per Tx descriptor */
148f7917c00SJeff Kirsher 	struct sk_buff *skb;
149f7917c00SJeff Kirsher 	struct ulptx_sgl *sgl;
150f7917c00SJeff Kirsher };
151f7917c00SJeff Kirsher 
152f7917c00SJeff Kirsher struct rx_sw_desc {                /* SW state per Rx descriptor */
153f7917c00SJeff Kirsher 	struct page *page;
154f7917c00SJeff Kirsher 	dma_addr_t dma_addr;
155f7917c00SJeff Kirsher };
156f7917c00SJeff Kirsher 
157f7917c00SJeff Kirsher /*
158f7917c00SJeff Kirsher  * The low bits of rx_sw_desc.dma_addr have special meaning.
159f7917c00SJeff Kirsher  */
160f7917c00SJeff Kirsher enum {
161f7917c00SJeff Kirsher 	RX_LARGE_BUF    = 1 << 0, /* buffer is larger than PAGE_SIZE */
162f7917c00SJeff Kirsher 	RX_UNMAPPED_BUF = 1 << 1, /* buffer is not mapped */
163f7917c00SJeff Kirsher };
164f7917c00SJeff Kirsher 
165f7917c00SJeff Kirsher static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
166f7917c00SJeff Kirsher {
167f7917c00SJeff Kirsher 	return d->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF);
168f7917c00SJeff Kirsher }
169f7917c00SJeff Kirsher 
170f7917c00SJeff Kirsher static inline bool is_buf_mapped(const struct rx_sw_desc *d)
171f7917c00SJeff Kirsher {
172f7917c00SJeff Kirsher 	return !(d->dma_addr & RX_UNMAPPED_BUF);
173f7917c00SJeff Kirsher }
174f7917c00SJeff Kirsher 
175f7917c00SJeff Kirsher /**
176f7917c00SJeff Kirsher  *	txq_avail - return the number of available slots in a Tx queue
177f7917c00SJeff Kirsher  *	@q: the Tx queue
178f7917c00SJeff Kirsher  *
179f7917c00SJeff Kirsher  *	Returns the number of descriptors in a Tx queue available to write new
180f7917c00SJeff Kirsher  *	packets.
181f7917c00SJeff Kirsher  */
182f7917c00SJeff Kirsher static inline unsigned int txq_avail(const struct sge_txq *q)
183f7917c00SJeff Kirsher {
184f7917c00SJeff Kirsher 	return q->size - 1 - q->in_use;
185f7917c00SJeff Kirsher }
186f7917c00SJeff Kirsher 
187f7917c00SJeff Kirsher /**
188f7917c00SJeff Kirsher  *	fl_cap - return the capacity of a free-buffer list
189f7917c00SJeff Kirsher  *	@fl: the FL
190f7917c00SJeff Kirsher  *
191f7917c00SJeff Kirsher  *	Returns the capacity of a free-buffer list.  The capacity is less than
192f7917c00SJeff Kirsher  *	the size because one descriptor needs to be left unpopulated, otherwise
193f7917c00SJeff Kirsher  *	HW will think the FL is empty.
194f7917c00SJeff Kirsher  */
195f7917c00SJeff Kirsher static inline unsigned int fl_cap(const struct sge_fl *fl)
196f7917c00SJeff Kirsher {
197f7917c00SJeff Kirsher 	return fl->size - 8;   /* 1 descriptor = 8 buffers */
198f7917c00SJeff Kirsher }
199f7917c00SJeff Kirsher 
200f7917c00SJeff Kirsher static inline bool fl_starving(const struct sge_fl *fl)
201f7917c00SJeff Kirsher {
202f7917c00SJeff Kirsher 	return fl->avail - fl->pend_cred <= FL_STARVE_THRES;
203f7917c00SJeff Kirsher }
204f7917c00SJeff Kirsher 
205f7917c00SJeff Kirsher static int map_skb(struct device *dev, const struct sk_buff *skb,
206f7917c00SJeff Kirsher 		   dma_addr_t *addr)
207f7917c00SJeff Kirsher {
208f7917c00SJeff Kirsher 	const skb_frag_t *fp, *end;
209f7917c00SJeff Kirsher 	const struct skb_shared_info *si;
210f7917c00SJeff Kirsher 
211f7917c00SJeff Kirsher 	*addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
212f7917c00SJeff Kirsher 	if (dma_mapping_error(dev, *addr))
213f7917c00SJeff Kirsher 		goto out_err;
214f7917c00SJeff Kirsher 
215f7917c00SJeff Kirsher 	si = skb_shinfo(skb);
216f7917c00SJeff Kirsher 	end = &si->frags[si->nr_frags];
217f7917c00SJeff Kirsher 
218f7917c00SJeff Kirsher 	for (fp = si->frags; fp < end; fp++) {
219e91b0f24SIan Campbell 		*++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
220e91b0f24SIan Campbell 					   DMA_TO_DEVICE);
221f7917c00SJeff Kirsher 		if (dma_mapping_error(dev, *addr))
222f7917c00SJeff Kirsher 			goto unwind;
223f7917c00SJeff Kirsher 	}
224f7917c00SJeff Kirsher 	return 0;
225f7917c00SJeff Kirsher 
226f7917c00SJeff Kirsher unwind:
227f7917c00SJeff Kirsher 	while (fp-- > si->frags)
2289e903e08SEric Dumazet 		dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
229f7917c00SJeff Kirsher 
230f7917c00SJeff Kirsher 	dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
231f7917c00SJeff Kirsher out_err:
232f7917c00SJeff Kirsher 	return -ENOMEM;
233f7917c00SJeff Kirsher }
234f7917c00SJeff Kirsher 
235f7917c00SJeff Kirsher #ifdef CONFIG_NEED_DMA_MAP_STATE
236f7917c00SJeff Kirsher static void unmap_skb(struct device *dev, const struct sk_buff *skb,
237f7917c00SJeff Kirsher 		      const dma_addr_t *addr)
238f7917c00SJeff Kirsher {
239f7917c00SJeff Kirsher 	const skb_frag_t *fp, *end;
240f7917c00SJeff Kirsher 	const struct skb_shared_info *si;
241f7917c00SJeff Kirsher 
242f7917c00SJeff Kirsher 	dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
243f7917c00SJeff Kirsher 
244f7917c00SJeff Kirsher 	si = skb_shinfo(skb);
245f7917c00SJeff Kirsher 	end = &si->frags[si->nr_frags];
246f7917c00SJeff Kirsher 	for (fp = si->frags; fp < end; fp++)
2479e903e08SEric Dumazet 		dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
248f7917c00SJeff Kirsher }
249f7917c00SJeff Kirsher 
250f7917c00SJeff Kirsher /**
251f7917c00SJeff Kirsher  *	deferred_unmap_destructor - unmap a packet when it is freed
252f7917c00SJeff Kirsher  *	@skb: the packet
253f7917c00SJeff Kirsher  *
254f7917c00SJeff Kirsher  *	This is the packet destructor used for Tx packets that need to remain
255f7917c00SJeff Kirsher  *	mapped until they are freed rather than until their Tx descriptors are
256f7917c00SJeff Kirsher  *	freed.
257f7917c00SJeff Kirsher  */
258f7917c00SJeff Kirsher static void deferred_unmap_destructor(struct sk_buff *skb)
259f7917c00SJeff Kirsher {
260f7917c00SJeff Kirsher 	unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
261f7917c00SJeff Kirsher }
262f7917c00SJeff Kirsher #endif
263f7917c00SJeff Kirsher 
264f7917c00SJeff Kirsher static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
265f7917c00SJeff Kirsher 		      const struct ulptx_sgl *sgl, const struct sge_txq *q)
266f7917c00SJeff Kirsher {
267f7917c00SJeff Kirsher 	const struct ulptx_sge_pair *p;
268f7917c00SJeff Kirsher 	unsigned int nfrags = skb_shinfo(skb)->nr_frags;
269f7917c00SJeff Kirsher 
270f7917c00SJeff Kirsher 	if (likely(skb_headlen(skb)))
271f7917c00SJeff Kirsher 		dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
272f7917c00SJeff Kirsher 				 DMA_TO_DEVICE);
273f7917c00SJeff Kirsher 	else {
274f7917c00SJeff Kirsher 		dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
275f7917c00SJeff Kirsher 			       DMA_TO_DEVICE);
276f7917c00SJeff Kirsher 		nfrags--;
277f7917c00SJeff Kirsher 	}
278f7917c00SJeff Kirsher 
279f7917c00SJeff Kirsher 	/*
280f7917c00SJeff Kirsher 	 * the complexity below is because of the possibility of a wrap-around
281f7917c00SJeff Kirsher 	 * in the middle of an SGL
282f7917c00SJeff Kirsher 	 */
283f7917c00SJeff Kirsher 	for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
284f7917c00SJeff Kirsher 		if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
285f7917c00SJeff Kirsher unmap:			dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
286f7917c00SJeff Kirsher 				       ntohl(p->len[0]), DMA_TO_DEVICE);
287f7917c00SJeff Kirsher 			dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
288f7917c00SJeff Kirsher 				       ntohl(p->len[1]), DMA_TO_DEVICE);
289f7917c00SJeff Kirsher 			p++;
290f7917c00SJeff Kirsher 		} else if ((u8 *)p == (u8 *)q->stat) {
291f7917c00SJeff Kirsher 			p = (const struct ulptx_sge_pair *)q->desc;
292f7917c00SJeff Kirsher 			goto unmap;
293f7917c00SJeff Kirsher 		} else if ((u8 *)p + 8 == (u8 *)q->stat) {
294f7917c00SJeff Kirsher 			const __be64 *addr = (const __be64 *)q->desc;
295f7917c00SJeff Kirsher 
296f7917c00SJeff Kirsher 			dma_unmap_page(dev, be64_to_cpu(addr[0]),
297f7917c00SJeff Kirsher 				       ntohl(p->len[0]), DMA_TO_DEVICE);
298f7917c00SJeff Kirsher 			dma_unmap_page(dev, be64_to_cpu(addr[1]),
299f7917c00SJeff Kirsher 				       ntohl(p->len[1]), DMA_TO_DEVICE);
300f7917c00SJeff Kirsher 			p = (const struct ulptx_sge_pair *)&addr[2];
301f7917c00SJeff Kirsher 		} else {
302f7917c00SJeff Kirsher 			const __be64 *addr = (const __be64 *)q->desc;
303f7917c00SJeff Kirsher 
304f7917c00SJeff Kirsher 			dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
305f7917c00SJeff Kirsher 				       ntohl(p->len[0]), DMA_TO_DEVICE);
306f7917c00SJeff Kirsher 			dma_unmap_page(dev, be64_to_cpu(addr[0]),
307f7917c00SJeff Kirsher 				       ntohl(p->len[1]), DMA_TO_DEVICE);
308f7917c00SJeff Kirsher 			p = (const struct ulptx_sge_pair *)&addr[1];
309f7917c00SJeff Kirsher 		}
310f7917c00SJeff Kirsher 	}
311f7917c00SJeff Kirsher 	if (nfrags) {
312f7917c00SJeff Kirsher 		__be64 addr;
313f7917c00SJeff Kirsher 
314f7917c00SJeff Kirsher 		if ((u8 *)p == (u8 *)q->stat)
315f7917c00SJeff Kirsher 			p = (const struct ulptx_sge_pair *)q->desc;
316f7917c00SJeff Kirsher 		addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
317f7917c00SJeff Kirsher 						       *(const __be64 *)q->desc;
318f7917c00SJeff Kirsher 		dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
319f7917c00SJeff Kirsher 			       DMA_TO_DEVICE);
320f7917c00SJeff Kirsher 	}
321f7917c00SJeff Kirsher }
322f7917c00SJeff Kirsher 
323f7917c00SJeff Kirsher /**
324f7917c00SJeff Kirsher  *	free_tx_desc - reclaims Tx descriptors and their buffers
325f7917c00SJeff Kirsher  *	@adapter: the adapter
326f7917c00SJeff Kirsher  *	@q: the Tx queue to reclaim descriptors from
327f7917c00SJeff Kirsher  *	@n: the number of descriptors to reclaim
328f7917c00SJeff Kirsher  *	@unmap: whether the buffers should be unmapped for DMA
329f7917c00SJeff Kirsher  *
330f7917c00SJeff Kirsher  *	Reclaims Tx descriptors from an SGE Tx queue and frees the associated
331f7917c00SJeff Kirsher  *	Tx buffers.  Called with the Tx queue lock held.
332f7917c00SJeff Kirsher  */
333f7917c00SJeff Kirsher static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
334f7917c00SJeff Kirsher 			 unsigned int n, bool unmap)
335f7917c00SJeff Kirsher {
336f7917c00SJeff Kirsher 	struct tx_sw_desc *d;
337f7917c00SJeff Kirsher 	unsigned int cidx = q->cidx;
338f7917c00SJeff Kirsher 	struct device *dev = adap->pdev_dev;
339f7917c00SJeff Kirsher 
340f7917c00SJeff Kirsher 	d = &q->sdesc[cidx];
341f7917c00SJeff Kirsher 	while (n--) {
342f7917c00SJeff Kirsher 		if (d->skb) {                       /* an SGL is present */
343f7917c00SJeff Kirsher 			if (unmap)
344f7917c00SJeff Kirsher 				unmap_sgl(dev, d->skb, d->sgl, q);
345f7917c00SJeff Kirsher 			kfree_skb(d->skb);
346f7917c00SJeff Kirsher 			d->skb = NULL;
347f7917c00SJeff Kirsher 		}
348f7917c00SJeff Kirsher 		++d;
349f7917c00SJeff Kirsher 		if (++cidx == q->size) {
350f7917c00SJeff Kirsher 			cidx = 0;
351f7917c00SJeff Kirsher 			d = q->sdesc;
352f7917c00SJeff Kirsher 		}
353f7917c00SJeff Kirsher 	}
354f7917c00SJeff Kirsher 	q->cidx = cidx;
355f7917c00SJeff Kirsher }
356f7917c00SJeff Kirsher 
357f7917c00SJeff Kirsher /*
358f7917c00SJeff Kirsher  * Return the number of reclaimable descriptors in a Tx queue.
359f7917c00SJeff Kirsher  */
360f7917c00SJeff Kirsher static inline int reclaimable(const struct sge_txq *q)
361f7917c00SJeff Kirsher {
362f7917c00SJeff Kirsher 	int hw_cidx = ntohs(q->stat->cidx);
363f7917c00SJeff Kirsher 	hw_cidx -= q->cidx;
364f7917c00SJeff Kirsher 	return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
365f7917c00SJeff Kirsher }
366f7917c00SJeff Kirsher 
367f7917c00SJeff Kirsher /**
368f7917c00SJeff Kirsher  *	reclaim_completed_tx - reclaims completed Tx descriptors
369f7917c00SJeff Kirsher  *	@adap: the adapter
370f7917c00SJeff Kirsher  *	@q: the Tx queue to reclaim completed descriptors from
371f7917c00SJeff Kirsher  *	@unmap: whether the buffers should be unmapped for DMA
372f7917c00SJeff Kirsher  *
373f7917c00SJeff Kirsher  *	Reclaims Tx descriptors that the SGE has indicated it has processed,
374f7917c00SJeff Kirsher  *	and frees the associated buffers if possible.  Called with the Tx
375f7917c00SJeff Kirsher  *	queue locked.
376f7917c00SJeff Kirsher  */
377f7917c00SJeff Kirsher static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
378f7917c00SJeff Kirsher 					bool unmap)
379f7917c00SJeff Kirsher {
380f7917c00SJeff Kirsher 	int avail = reclaimable(q);
381f7917c00SJeff Kirsher 
382f7917c00SJeff Kirsher 	if (avail) {
383f7917c00SJeff Kirsher 		/*
384f7917c00SJeff Kirsher 		 * Limit the amount of clean up work we do at a time to keep
385f7917c00SJeff Kirsher 		 * the Tx lock hold time O(1).
386f7917c00SJeff Kirsher 		 */
387f7917c00SJeff Kirsher 		if (avail > MAX_TX_RECLAIM)
388f7917c00SJeff Kirsher 			avail = MAX_TX_RECLAIM;
389f7917c00SJeff Kirsher 
390f7917c00SJeff Kirsher 		free_tx_desc(adap, q, avail, unmap);
391f7917c00SJeff Kirsher 		q->in_use -= avail;
392f7917c00SJeff Kirsher 	}
393f7917c00SJeff Kirsher }
394f7917c00SJeff Kirsher 
395f7917c00SJeff Kirsher static inline int get_buf_size(const struct rx_sw_desc *d)
396f7917c00SJeff Kirsher {
397f7917c00SJeff Kirsher #if FL_PG_ORDER > 0
398f7917c00SJeff Kirsher 	return (d->dma_addr & RX_LARGE_BUF) ? (PAGE_SIZE << FL_PG_ORDER) :
399f7917c00SJeff Kirsher 					      PAGE_SIZE;
400f7917c00SJeff Kirsher #else
401f7917c00SJeff Kirsher 	return PAGE_SIZE;
402f7917c00SJeff Kirsher #endif
403f7917c00SJeff Kirsher }
404f7917c00SJeff Kirsher 
405f7917c00SJeff Kirsher /**
406f7917c00SJeff Kirsher  *	free_rx_bufs - free the Rx buffers on an SGE free list
407f7917c00SJeff Kirsher  *	@adap: the adapter
408f7917c00SJeff Kirsher  *	@q: the SGE free list to free buffers from
409f7917c00SJeff Kirsher  *	@n: how many buffers to free
410f7917c00SJeff Kirsher  *
411f7917c00SJeff Kirsher  *	Release the next @n buffers on an SGE free-buffer Rx queue.   The
412f7917c00SJeff Kirsher  *	buffers must be made inaccessible to HW before calling this function.
413f7917c00SJeff Kirsher  */
414f7917c00SJeff Kirsher static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
415f7917c00SJeff Kirsher {
416f7917c00SJeff Kirsher 	while (n--) {
417f7917c00SJeff Kirsher 		struct rx_sw_desc *d = &q->sdesc[q->cidx];
418f7917c00SJeff Kirsher 
419f7917c00SJeff Kirsher 		if (is_buf_mapped(d))
420f7917c00SJeff Kirsher 			dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
421f7917c00SJeff Kirsher 				       get_buf_size(d), PCI_DMA_FROMDEVICE);
422f7917c00SJeff Kirsher 		put_page(d->page);
423f7917c00SJeff Kirsher 		d->page = NULL;
424f7917c00SJeff Kirsher 		if (++q->cidx == q->size)
425f7917c00SJeff Kirsher 			q->cidx = 0;
426f7917c00SJeff Kirsher 		q->avail--;
427f7917c00SJeff Kirsher 	}
428f7917c00SJeff Kirsher }
429f7917c00SJeff Kirsher 
430f7917c00SJeff Kirsher /**
431f7917c00SJeff Kirsher  *	unmap_rx_buf - unmap the current Rx buffer on an SGE free list
432f7917c00SJeff Kirsher  *	@adap: the adapter
433f7917c00SJeff Kirsher  *	@q: the SGE free list
434f7917c00SJeff Kirsher  *
435f7917c00SJeff Kirsher  *	Unmap the current buffer on an SGE free-buffer Rx queue.   The
436f7917c00SJeff Kirsher  *	buffer must be made inaccessible to HW before calling this function.
437f7917c00SJeff Kirsher  *
438f7917c00SJeff Kirsher  *	This is similar to @free_rx_bufs above but does not free the buffer.
439f7917c00SJeff Kirsher  *	Do note that the FL still loses any further access to the buffer.
440f7917c00SJeff Kirsher  */
441f7917c00SJeff Kirsher static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
442f7917c00SJeff Kirsher {
443f7917c00SJeff Kirsher 	struct rx_sw_desc *d = &q->sdesc[q->cidx];
444f7917c00SJeff Kirsher 
445f7917c00SJeff Kirsher 	if (is_buf_mapped(d))
446f7917c00SJeff Kirsher 		dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
447f7917c00SJeff Kirsher 			       get_buf_size(d), PCI_DMA_FROMDEVICE);
448f7917c00SJeff Kirsher 	d->page = NULL;
449f7917c00SJeff Kirsher 	if (++q->cidx == q->size)
450f7917c00SJeff Kirsher 		q->cidx = 0;
451f7917c00SJeff Kirsher 	q->avail--;
452f7917c00SJeff Kirsher }
453f7917c00SJeff Kirsher 
454f7917c00SJeff Kirsher static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
455f7917c00SJeff Kirsher {
456f7917c00SJeff Kirsher 	if (q->pend_cred >= 8) {
457f7917c00SJeff Kirsher 		wmb();
458f7917c00SJeff Kirsher 		t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL), DBPRIO |
459f7917c00SJeff Kirsher 			     QID(q->cntxt_id) | PIDX(q->pend_cred / 8));
460f7917c00SJeff Kirsher 		q->pend_cred &= 7;
461f7917c00SJeff Kirsher 	}
462f7917c00SJeff Kirsher }
463f7917c00SJeff Kirsher 
464f7917c00SJeff Kirsher static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
465f7917c00SJeff Kirsher 				  dma_addr_t mapping)
466f7917c00SJeff Kirsher {
467f7917c00SJeff Kirsher 	sd->page = pg;
468f7917c00SJeff Kirsher 	sd->dma_addr = mapping;      /* includes size low bits */
469f7917c00SJeff Kirsher }
470f7917c00SJeff Kirsher 
471f7917c00SJeff Kirsher /**
472f7917c00SJeff Kirsher  *	refill_fl - refill an SGE Rx buffer ring
473f7917c00SJeff Kirsher  *	@adap: the adapter
474f7917c00SJeff Kirsher  *	@q: the ring to refill
475f7917c00SJeff Kirsher  *	@n: the number of new buffers to allocate
476f7917c00SJeff Kirsher  *	@gfp: the gfp flags for the allocations
477f7917c00SJeff Kirsher  *
478f7917c00SJeff Kirsher  *	(Re)populate an SGE free-buffer queue with up to @n new packet buffers,
479f7917c00SJeff Kirsher  *	allocated with the supplied gfp flags.  The caller must assure that
480f7917c00SJeff Kirsher  *	@n does not exceed the queue's capacity.  If afterwards the queue is
481f7917c00SJeff Kirsher  *	found critically low mark it as starving in the bitmap of starving FLs.
482f7917c00SJeff Kirsher  *
483f7917c00SJeff Kirsher  *	Returns the number of buffers allocated.
484f7917c00SJeff Kirsher  */
485f7917c00SJeff Kirsher static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
486f7917c00SJeff Kirsher 			      gfp_t gfp)
487f7917c00SJeff Kirsher {
488f7917c00SJeff Kirsher 	struct page *pg;
489f7917c00SJeff Kirsher 	dma_addr_t mapping;
490f7917c00SJeff Kirsher 	unsigned int cred = q->avail;
491f7917c00SJeff Kirsher 	__be64 *d = &q->desc[q->pidx];
492f7917c00SJeff Kirsher 	struct rx_sw_desc *sd = &q->sdesc[q->pidx];
493f7917c00SJeff Kirsher 
4941f2149c1SEric Dumazet 	gfp |= __GFP_NOWARN | __GFP_COLD;
495f7917c00SJeff Kirsher 
496f7917c00SJeff Kirsher #if FL_PG_ORDER > 0
497f7917c00SJeff Kirsher 	/*
498f7917c00SJeff Kirsher 	 * Prefer large buffers
499f7917c00SJeff Kirsher 	 */
500f7917c00SJeff Kirsher 	while (n) {
501f7917c00SJeff Kirsher 		pg = alloc_pages(gfp | __GFP_COMP, FL_PG_ORDER);
502f7917c00SJeff Kirsher 		if (unlikely(!pg)) {
503f7917c00SJeff Kirsher 			q->large_alloc_failed++;
504f7917c00SJeff Kirsher 			break;       /* fall back to single pages */
505f7917c00SJeff Kirsher 		}
506f7917c00SJeff Kirsher 
507f7917c00SJeff Kirsher 		mapping = dma_map_page(adap->pdev_dev, pg, 0,
508f7917c00SJeff Kirsher 				       PAGE_SIZE << FL_PG_ORDER,
509f7917c00SJeff Kirsher 				       PCI_DMA_FROMDEVICE);
510f7917c00SJeff Kirsher 		if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
511f7917c00SJeff Kirsher 			__free_pages(pg, FL_PG_ORDER);
512f7917c00SJeff Kirsher 			goto out;   /* do not try small pages for this error */
513f7917c00SJeff Kirsher 		}
514f7917c00SJeff Kirsher 		mapping |= RX_LARGE_BUF;
515f7917c00SJeff Kirsher 		*d++ = cpu_to_be64(mapping);
516f7917c00SJeff Kirsher 
517f7917c00SJeff Kirsher 		set_rx_sw_desc(sd, pg, mapping);
518f7917c00SJeff Kirsher 		sd++;
519f7917c00SJeff Kirsher 
520f7917c00SJeff Kirsher 		q->avail++;
521f7917c00SJeff Kirsher 		if (++q->pidx == q->size) {
522f7917c00SJeff Kirsher 			q->pidx = 0;
523f7917c00SJeff Kirsher 			sd = q->sdesc;
524f7917c00SJeff Kirsher 			d = q->desc;
525f7917c00SJeff Kirsher 		}
526f7917c00SJeff Kirsher 		n--;
527f7917c00SJeff Kirsher 	}
528f7917c00SJeff Kirsher #endif
529f7917c00SJeff Kirsher 
530f7917c00SJeff Kirsher 	while (n--) {
5311f2149c1SEric Dumazet 		pg = alloc_page(gfp);
532f7917c00SJeff Kirsher 		if (unlikely(!pg)) {
533f7917c00SJeff Kirsher 			q->alloc_failed++;
534f7917c00SJeff Kirsher 			break;
535f7917c00SJeff Kirsher 		}
536f7917c00SJeff Kirsher 
537f7917c00SJeff Kirsher 		mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
538f7917c00SJeff Kirsher 				       PCI_DMA_FROMDEVICE);
539f7917c00SJeff Kirsher 		if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
5401f2149c1SEric Dumazet 			put_page(pg);
541f7917c00SJeff Kirsher 			goto out;
542f7917c00SJeff Kirsher 		}
543f7917c00SJeff Kirsher 		*d++ = cpu_to_be64(mapping);
544f7917c00SJeff Kirsher 
545f7917c00SJeff Kirsher 		set_rx_sw_desc(sd, pg, mapping);
546f7917c00SJeff Kirsher 		sd++;
547f7917c00SJeff Kirsher 
548f7917c00SJeff Kirsher 		q->avail++;
549f7917c00SJeff Kirsher 		if (++q->pidx == q->size) {
550f7917c00SJeff Kirsher 			q->pidx = 0;
551f7917c00SJeff Kirsher 			sd = q->sdesc;
552f7917c00SJeff Kirsher 			d = q->desc;
553f7917c00SJeff Kirsher 		}
554f7917c00SJeff Kirsher 	}
555f7917c00SJeff Kirsher 
556f7917c00SJeff Kirsher out:	cred = q->avail - cred;
557f7917c00SJeff Kirsher 	q->pend_cred += cred;
558f7917c00SJeff Kirsher 	ring_fl_db(adap, q);
559f7917c00SJeff Kirsher 
560f7917c00SJeff Kirsher 	if (unlikely(fl_starving(q))) {
561f7917c00SJeff Kirsher 		smp_wmb();
562f7917c00SJeff Kirsher 		set_bit(q->cntxt_id - adap->sge.egr_start,
563f7917c00SJeff Kirsher 			adap->sge.starving_fl);
564f7917c00SJeff Kirsher 	}
565f7917c00SJeff Kirsher 
566f7917c00SJeff Kirsher 	return cred;
567f7917c00SJeff Kirsher }
568f7917c00SJeff Kirsher 
569f7917c00SJeff Kirsher static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
570f7917c00SJeff Kirsher {
571f7917c00SJeff Kirsher 	refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
572f7917c00SJeff Kirsher 		  GFP_ATOMIC);
573f7917c00SJeff Kirsher }
574f7917c00SJeff Kirsher 
575f7917c00SJeff Kirsher /**
576f7917c00SJeff Kirsher  *	alloc_ring - allocate resources for an SGE descriptor ring
577f7917c00SJeff Kirsher  *	@dev: the PCI device's core device
578f7917c00SJeff Kirsher  *	@nelem: the number of descriptors
579f7917c00SJeff Kirsher  *	@elem_size: the size of each descriptor
580f7917c00SJeff Kirsher  *	@sw_size: the size of the SW state associated with each ring element
581f7917c00SJeff Kirsher  *	@phys: the physical address of the allocated ring
582f7917c00SJeff Kirsher  *	@metadata: address of the array holding the SW state for the ring
583f7917c00SJeff Kirsher  *	@stat_size: extra space in HW ring for status information
584f7917c00SJeff Kirsher  *	@node: preferred node for memory allocations
585f7917c00SJeff Kirsher  *
586f7917c00SJeff Kirsher  *	Allocates resources for an SGE descriptor ring, such as Tx queues,
587f7917c00SJeff Kirsher  *	free buffer lists, or response queues.  Each SGE ring requires
588f7917c00SJeff Kirsher  *	space for its HW descriptors plus, optionally, space for the SW state
589f7917c00SJeff Kirsher  *	associated with each HW entry (the metadata).  The function returns
590f7917c00SJeff Kirsher  *	three values: the virtual address for the HW ring (the return value
591f7917c00SJeff Kirsher  *	of the function), the bus address of the HW ring, and the address
592f7917c00SJeff Kirsher  *	of the SW ring.
593f7917c00SJeff Kirsher  */
594f7917c00SJeff Kirsher static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
595f7917c00SJeff Kirsher 			size_t sw_size, dma_addr_t *phys, void *metadata,
596f7917c00SJeff Kirsher 			size_t stat_size, int node)
597f7917c00SJeff Kirsher {
598f7917c00SJeff Kirsher 	size_t len = nelem * elem_size + stat_size;
599f7917c00SJeff Kirsher 	void *s = NULL;
600f7917c00SJeff Kirsher 	void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
601f7917c00SJeff Kirsher 
602f7917c00SJeff Kirsher 	if (!p)
603f7917c00SJeff Kirsher 		return NULL;
604f7917c00SJeff Kirsher 	if (sw_size) {
605f7917c00SJeff Kirsher 		s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
606f7917c00SJeff Kirsher 
607f7917c00SJeff Kirsher 		if (!s) {
608f7917c00SJeff Kirsher 			dma_free_coherent(dev, len, p, *phys);
609f7917c00SJeff Kirsher 			return NULL;
610f7917c00SJeff Kirsher 		}
611f7917c00SJeff Kirsher 	}
612f7917c00SJeff Kirsher 	if (metadata)
613f7917c00SJeff Kirsher 		*(void **)metadata = s;
614f7917c00SJeff Kirsher 	memset(p, 0, len);
615f7917c00SJeff Kirsher 	return p;
616f7917c00SJeff Kirsher }
617f7917c00SJeff Kirsher 
618f7917c00SJeff Kirsher /**
619f7917c00SJeff Kirsher  *	sgl_len - calculates the size of an SGL of the given capacity
620f7917c00SJeff Kirsher  *	@n: the number of SGL entries
621f7917c00SJeff Kirsher  *
622f7917c00SJeff Kirsher  *	Calculates the number of flits needed for a scatter/gather list that
623f7917c00SJeff Kirsher  *	can hold the given number of entries.
624f7917c00SJeff Kirsher  */
625f7917c00SJeff Kirsher static inline unsigned int sgl_len(unsigned int n)
626f7917c00SJeff Kirsher {
627f7917c00SJeff Kirsher 	n--;
628f7917c00SJeff Kirsher 	return (3 * n) / 2 + (n & 1) + 2;
629f7917c00SJeff Kirsher }
630f7917c00SJeff Kirsher 
631f7917c00SJeff Kirsher /**
632f7917c00SJeff Kirsher  *	flits_to_desc - returns the num of Tx descriptors for the given flits
633f7917c00SJeff Kirsher  *	@n: the number of flits
634f7917c00SJeff Kirsher  *
635f7917c00SJeff Kirsher  *	Returns the number of Tx descriptors needed for the supplied number
636f7917c00SJeff Kirsher  *	of flits.
637f7917c00SJeff Kirsher  */
638f7917c00SJeff Kirsher static inline unsigned int flits_to_desc(unsigned int n)
639f7917c00SJeff Kirsher {
640f7917c00SJeff Kirsher 	BUG_ON(n > SGE_MAX_WR_LEN / 8);
641f7917c00SJeff Kirsher 	return DIV_ROUND_UP(n, 8);
642f7917c00SJeff Kirsher }
643f7917c00SJeff Kirsher 
644f7917c00SJeff Kirsher /**
645f7917c00SJeff Kirsher  *	is_eth_imm - can an Ethernet packet be sent as immediate data?
646f7917c00SJeff Kirsher  *	@skb: the packet
647f7917c00SJeff Kirsher  *
648f7917c00SJeff Kirsher  *	Returns whether an Ethernet packet is small enough to fit as
649f7917c00SJeff Kirsher  *	immediate data.
650f7917c00SJeff Kirsher  */
651f7917c00SJeff Kirsher static inline int is_eth_imm(const struct sk_buff *skb)
652f7917c00SJeff Kirsher {
653f7917c00SJeff Kirsher 	return skb->len <= MAX_IMM_TX_PKT_LEN - sizeof(struct cpl_tx_pkt);
654f7917c00SJeff Kirsher }
655f7917c00SJeff Kirsher 
656f7917c00SJeff Kirsher /**
657f7917c00SJeff Kirsher  *	calc_tx_flits - calculate the number of flits for a packet Tx WR
658f7917c00SJeff Kirsher  *	@skb: the packet
659f7917c00SJeff Kirsher  *
660f7917c00SJeff Kirsher  *	Returns the number of flits needed for a Tx WR for the given Ethernet
661f7917c00SJeff Kirsher  *	packet, including the needed WR and CPL headers.
662f7917c00SJeff Kirsher  */
663f7917c00SJeff Kirsher static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
664f7917c00SJeff Kirsher {
665f7917c00SJeff Kirsher 	unsigned int flits;
666f7917c00SJeff Kirsher 
667f7917c00SJeff Kirsher 	if (is_eth_imm(skb))
668f7917c00SJeff Kirsher 		return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt), 8);
669f7917c00SJeff Kirsher 
670f7917c00SJeff Kirsher 	flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
671f7917c00SJeff Kirsher 	if (skb_shinfo(skb)->gso_size)
672f7917c00SJeff Kirsher 		flits += 2;
673f7917c00SJeff Kirsher 	return flits;
674f7917c00SJeff Kirsher }
675f7917c00SJeff Kirsher 
676f7917c00SJeff Kirsher /**
677f7917c00SJeff Kirsher  *	calc_tx_descs - calculate the number of Tx descriptors for a packet
678f7917c00SJeff Kirsher  *	@skb: the packet
679f7917c00SJeff Kirsher  *
680f7917c00SJeff Kirsher  *	Returns the number of Tx descriptors needed for the given Ethernet
681f7917c00SJeff Kirsher  *	packet, including the needed WR and CPL headers.
682f7917c00SJeff Kirsher  */
683f7917c00SJeff Kirsher static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
684f7917c00SJeff Kirsher {
685f7917c00SJeff Kirsher 	return flits_to_desc(calc_tx_flits(skb));
686f7917c00SJeff Kirsher }
687f7917c00SJeff Kirsher 
688f7917c00SJeff Kirsher /**
689f7917c00SJeff Kirsher  *	write_sgl - populate a scatter/gather list for a packet
690f7917c00SJeff Kirsher  *	@skb: the packet
691f7917c00SJeff Kirsher  *	@q: the Tx queue we are writing into
692f7917c00SJeff Kirsher  *	@sgl: starting location for writing the SGL
693f7917c00SJeff Kirsher  *	@end: points right after the end of the SGL
694f7917c00SJeff Kirsher  *	@start: start offset into skb main-body data to include in the SGL
695f7917c00SJeff Kirsher  *	@addr: the list of bus addresses for the SGL elements
696f7917c00SJeff Kirsher  *
697f7917c00SJeff Kirsher  *	Generates a gather list for the buffers that make up a packet.
698f7917c00SJeff Kirsher  *	The caller must provide adequate space for the SGL that will be written.
699f7917c00SJeff Kirsher  *	The SGL includes all of the packet's page fragments and the data in its
700f7917c00SJeff Kirsher  *	main body except for the first @start bytes.  @sgl must be 16-byte
701f7917c00SJeff Kirsher  *	aligned and within a Tx descriptor with available space.  @end points
702f7917c00SJeff Kirsher  *	right after the end of the SGL but does not account for any potential
703f7917c00SJeff Kirsher  *	wrap around, i.e., @end > @sgl.
704f7917c00SJeff Kirsher  */
705f7917c00SJeff Kirsher static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
706f7917c00SJeff Kirsher 		      struct ulptx_sgl *sgl, u64 *end, unsigned int start,
707f7917c00SJeff Kirsher 		      const dma_addr_t *addr)
708f7917c00SJeff Kirsher {
709f7917c00SJeff Kirsher 	unsigned int i, len;
710f7917c00SJeff Kirsher 	struct ulptx_sge_pair *to;
711f7917c00SJeff Kirsher 	const struct skb_shared_info *si = skb_shinfo(skb);
712f7917c00SJeff Kirsher 	unsigned int nfrags = si->nr_frags;
713f7917c00SJeff Kirsher 	struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
714f7917c00SJeff Kirsher 
715f7917c00SJeff Kirsher 	len = skb_headlen(skb) - start;
716f7917c00SJeff Kirsher 	if (likely(len)) {
717f7917c00SJeff Kirsher 		sgl->len0 = htonl(len);
718f7917c00SJeff Kirsher 		sgl->addr0 = cpu_to_be64(addr[0] + start);
719f7917c00SJeff Kirsher 		nfrags++;
720f7917c00SJeff Kirsher 	} else {
7219e903e08SEric Dumazet 		sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
722f7917c00SJeff Kirsher 		sgl->addr0 = cpu_to_be64(addr[1]);
723f7917c00SJeff Kirsher 	}
724f7917c00SJeff Kirsher 
725f7917c00SJeff Kirsher 	sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_NSGE(nfrags));
726f7917c00SJeff Kirsher 	if (likely(--nfrags == 0))
727f7917c00SJeff Kirsher 		return;
728f7917c00SJeff Kirsher 	/*
729f7917c00SJeff Kirsher 	 * Most of the complexity below deals with the possibility we hit the
730f7917c00SJeff Kirsher 	 * end of the queue in the middle of writing the SGL.  For this case
731f7917c00SJeff Kirsher 	 * only we create the SGL in a temporary buffer and then copy it.
732f7917c00SJeff Kirsher 	 */
733f7917c00SJeff Kirsher 	to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
734f7917c00SJeff Kirsher 
735f7917c00SJeff Kirsher 	for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
7369e903e08SEric Dumazet 		to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
7379e903e08SEric Dumazet 		to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
738f7917c00SJeff Kirsher 		to->addr[0] = cpu_to_be64(addr[i]);
739f7917c00SJeff Kirsher 		to->addr[1] = cpu_to_be64(addr[++i]);
740f7917c00SJeff Kirsher 	}
741f7917c00SJeff Kirsher 	if (nfrags) {
7429e903e08SEric Dumazet 		to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
743f7917c00SJeff Kirsher 		to->len[1] = cpu_to_be32(0);
744f7917c00SJeff Kirsher 		to->addr[0] = cpu_to_be64(addr[i + 1]);
745f7917c00SJeff Kirsher 	}
746f7917c00SJeff Kirsher 	if (unlikely((u8 *)end > (u8 *)q->stat)) {
747f7917c00SJeff Kirsher 		unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
748f7917c00SJeff Kirsher 
749f7917c00SJeff Kirsher 		if (likely(part0))
750f7917c00SJeff Kirsher 			memcpy(sgl->sge, buf, part0);
751f7917c00SJeff Kirsher 		part1 = (u8 *)end - (u8 *)q->stat;
752f7917c00SJeff Kirsher 		memcpy(q->desc, (u8 *)buf + part0, part1);
753f7917c00SJeff Kirsher 		end = (void *)q->desc + part1;
754f7917c00SJeff Kirsher 	}
755f7917c00SJeff Kirsher 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
75664699336SJoe Perches 		*end = 0;
757f7917c00SJeff Kirsher }
758f7917c00SJeff Kirsher 
759f7917c00SJeff Kirsher /**
760f7917c00SJeff Kirsher  *	ring_tx_db - check and potentially ring a Tx queue's doorbell
761f7917c00SJeff Kirsher  *	@adap: the adapter
762f7917c00SJeff Kirsher  *	@q: the Tx queue
763f7917c00SJeff Kirsher  *	@n: number of new descriptors to give to HW
764f7917c00SJeff Kirsher  *
765f7917c00SJeff Kirsher  *	Ring the doorbel for a Tx queue.
766f7917c00SJeff Kirsher  */
767f7917c00SJeff Kirsher static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
768f7917c00SJeff Kirsher {
769f7917c00SJeff Kirsher 	wmb();            /* write descriptors before telling HW */
7703069ee9bSVipul Pandya 	spin_lock(&q->db_lock);
7713069ee9bSVipul Pandya 	if (!q->db_disabled) {
7723069ee9bSVipul Pandya 		t4_write_reg(adap, MYPF_REG(A_SGE_PF_KDOORBELL),
7733069ee9bSVipul Pandya 			     V_QID(q->cntxt_id) | V_PIDX(n));
7743069ee9bSVipul Pandya 	}
7753069ee9bSVipul Pandya 	q->db_pidx = q->pidx;
7763069ee9bSVipul Pandya 	spin_unlock(&q->db_lock);
777f7917c00SJeff Kirsher }
778f7917c00SJeff Kirsher 
779f7917c00SJeff Kirsher /**
780f7917c00SJeff Kirsher  *	inline_tx_skb - inline a packet's data into Tx descriptors
781f7917c00SJeff Kirsher  *	@skb: the packet
782f7917c00SJeff Kirsher  *	@q: the Tx queue where the packet will be inlined
783f7917c00SJeff Kirsher  *	@pos: starting position in the Tx queue where to inline the packet
784f7917c00SJeff Kirsher  *
785f7917c00SJeff Kirsher  *	Inline a packet's contents directly into Tx descriptors, starting at
786f7917c00SJeff Kirsher  *	the given position within the Tx DMA ring.
787f7917c00SJeff Kirsher  *	Most of the complexity of this operation is dealing with wrap arounds
788f7917c00SJeff Kirsher  *	in the middle of the packet we want to inline.
789f7917c00SJeff Kirsher  */
790f7917c00SJeff Kirsher static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
791f7917c00SJeff Kirsher 			  void *pos)
792f7917c00SJeff Kirsher {
793f7917c00SJeff Kirsher 	u64 *p;
794f7917c00SJeff Kirsher 	int left = (void *)q->stat - pos;
795f7917c00SJeff Kirsher 
796f7917c00SJeff Kirsher 	if (likely(skb->len <= left)) {
797f7917c00SJeff Kirsher 		if (likely(!skb->data_len))
798f7917c00SJeff Kirsher 			skb_copy_from_linear_data(skb, pos, skb->len);
799f7917c00SJeff Kirsher 		else
800f7917c00SJeff Kirsher 			skb_copy_bits(skb, 0, pos, skb->len);
801f7917c00SJeff Kirsher 		pos += skb->len;
802f7917c00SJeff Kirsher 	} else {
803f7917c00SJeff Kirsher 		skb_copy_bits(skb, 0, pos, left);
804f7917c00SJeff Kirsher 		skb_copy_bits(skb, left, q->desc, skb->len - left);
805f7917c00SJeff Kirsher 		pos = (void *)q->desc + (skb->len - left);
806f7917c00SJeff Kirsher 	}
807f7917c00SJeff Kirsher 
808f7917c00SJeff Kirsher 	/* 0-pad to multiple of 16 */
809f7917c00SJeff Kirsher 	p = PTR_ALIGN(pos, 8);
810f7917c00SJeff Kirsher 	if ((uintptr_t)p & 8)
811f7917c00SJeff Kirsher 		*p = 0;
812f7917c00SJeff Kirsher }
813f7917c00SJeff Kirsher 
814f7917c00SJeff Kirsher /*
815f7917c00SJeff Kirsher  * Figure out what HW csum a packet wants and return the appropriate control
816f7917c00SJeff Kirsher  * bits.
817f7917c00SJeff Kirsher  */
818f7917c00SJeff Kirsher static u64 hwcsum(const struct sk_buff *skb)
819f7917c00SJeff Kirsher {
820f7917c00SJeff Kirsher 	int csum_type;
821f7917c00SJeff Kirsher 	const struct iphdr *iph = ip_hdr(skb);
822f7917c00SJeff Kirsher 
823f7917c00SJeff Kirsher 	if (iph->version == 4) {
824f7917c00SJeff Kirsher 		if (iph->protocol == IPPROTO_TCP)
825f7917c00SJeff Kirsher 			csum_type = TX_CSUM_TCPIP;
826f7917c00SJeff Kirsher 		else if (iph->protocol == IPPROTO_UDP)
827f7917c00SJeff Kirsher 			csum_type = TX_CSUM_UDPIP;
828f7917c00SJeff Kirsher 		else {
829f7917c00SJeff Kirsher nocsum:			/*
830f7917c00SJeff Kirsher 			 * unknown protocol, disable HW csum
831f7917c00SJeff Kirsher 			 * and hope a bad packet is detected
832f7917c00SJeff Kirsher 			 */
833f7917c00SJeff Kirsher 			return TXPKT_L4CSUM_DIS;
834f7917c00SJeff Kirsher 		}
835f7917c00SJeff Kirsher 	} else {
836f7917c00SJeff Kirsher 		/*
837f7917c00SJeff Kirsher 		 * this doesn't work with extension headers
838f7917c00SJeff Kirsher 		 */
839f7917c00SJeff Kirsher 		const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
840f7917c00SJeff Kirsher 
841f7917c00SJeff Kirsher 		if (ip6h->nexthdr == IPPROTO_TCP)
842f7917c00SJeff Kirsher 			csum_type = TX_CSUM_TCPIP6;
843f7917c00SJeff Kirsher 		else if (ip6h->nexthdr == IPPROTO_UDP)
844f7917c00SJeff Kirsher 			csum_type = TX_CSUM_UDPIP6;
845f7917c00SJeff Kirsher 		else
846f7917c00SJeff Kirsher 			goto nocsum;
847f7917c00SJeff Kirsher 	}
848f7917c00SJeff Kirsher 
849f7917c00SJeff Kirsher 	if (likely(csum_type >= TX_CSUM_TCPIP))
850f7917c00SJeff Kirsher 		return TXPKT_CSUM_TYPE(csum_type) |
851f7917c00SJeff Kirsher 			TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
852f7917c00SJeff Kirsher 			TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
853f7917c00SJeff Kirsher 	else {
854f7917c00SJeff Kirsher 		int start = skb_transport_offset(skb);
855f7917c00SJeff Kirsher 
856f7917c00SJeff Kirsher 		return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
857f7917c00SJeff Kirsher 			TXPKT_CSUM_LOC(start + skb->csum_offset);
858f7917c00SJeff Kirsher 	}
859f7917c00SJeff Kirsher }
860f7917c00SJeff Kirsher 
861f7917c00SJeff Kirsher static void eth_txq_stop(struct sge_eth_txq *q)
862f7917c00SJeff Kirsher {
863f7917c00SJeff Kirsher 	netif_tx_stop_queue(q->txq);
864f7917c00SJeff Kirsher 	q->q.stops++;
865f7917c00SJeff Kirsher }
866f7917c00SJeff Kirsher 
867f7917c00SJeff Kirsher static inline void txq_advance(struct sge_txq *q, unsigned int n)
868f7917c00SJeff Kirsher {
869f7917c00SJeff Kirsher 	q->in_use += n;
870f7917c00SJeff Kirsher 	q->pidx += n;
871f7917c00SJeff Kirsher 	if (q->pidx >= q->size)
872f7917c00SJeff Kirsher 		q->pidx -= q->size;
873f7917c00SJeff Kirsher }
874f7917c00SJeff Kirsher 
875f7917c00SJeff Kirsher /**
876f7917c00SJeff Kirsher  *	t4_eth_xmit - add a packet to an Ethernet Tx queue
877f7917c00SJeff Kirsher  *	@skb: the packet
878f7917c00SJeff Kirsher  *	@dev: the egress net device
879f7917c00SJeff Kirsher  *
880f7917c00SJeff Kirsher  *	Add a packet to an SGE Ethernet Tx queue.  Runs with softirqs disabled.
881f7917c00SJeff Kirsher  */
882f7917c00SJeff Kirsher netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
883f7917c00SJeff Kirsher {
884f7917c00SJeff Kirsher 	u32 wr_mid;
885f7917c00SJeff Kirsher 	u64 cntrl, *end;
886f7917c00SJeff Kirsher 	int qidx, credits;
887f7917c00SJeff Kirsher 	unsigned int flits, ndesc;
888f7917c00SJeff Kirsher 	struct adapter *adap;
889f7917c00SJeff Kirsher 	struct sge_eth_txq *q;
890f7917c00SJeff Kirsher 	const struct port_info *pi;
891f7917c00SJeff Kirsher 	struct fw_eth_tx_pkt_wr *wr;
892f7917c00SJeff Kirsher 	struct cpl_tx_pkt_core *cpl;
893f7917c00SJeff Kirsher 	const struct skb_shared_info *ssi;
894f7917c00SJeff Kirsher 	dma_addr_t addr[MAX_SKB_FRAGS + 1];
895f7917c00SJeff Kirsher 
896f7917c00SJeff Kirsher 	/*
897f7917c00SJeff Kirsher 	 * The chip min packet length is 10 octets but play safe and reject
898f7917c00SJeff Kirsher 	 * anything shorter than an Ethernet header.
899f7917c00SJeff Kirsher 	 */
900f7917c00SJeff Kirsher 	if (unlikely(skb->len < ETH_HLEN)) {
901f7917c00SJeff Kirsher out_free:	dev_kfree_skb(skb);
902f7917c00SJeff Kirsher 		return NETDEV_TX_OK;
903f7917c00SJeff Kirsher 	}
904f7917c00SJeff Kirsher 
905f7917c00SJeff Kirsher 	pi = netdev_priv(dev);
906f7917c00SJeff Kirsher 	adap = pi->adapter;
907f7917c00SJeff Kirsher 	qidx = skb_get_queue_mapping(skb);
908f7917c00SJeff Kirsher 	q = &adap->sge.ethtxq[qidx + pi->first_qset];
909f7917c00SJeff Kirsher 
910f7917c00SJeff Kirsher 	reclaim_completed_tx(adap, &q->q, true);
911f7917c00SJeff Kirsher 
912f7917c00SJeff Kirsher 	flits = calc_tx_flits(skb);
913f7917c00SJeff Kirsher 	ndesc = flits_to_desc(flits);
914f7917c00SJeff Kirsher 	credits = txq_avail(&q->q) - ndesc;
915f7917c00SJeff Kirsher 
916f7917c00SJeff Kirsher 	if (unlikely(credits < 0)) {
917f7917c00SJeff Kirsher 		eth_txq_stop(q);
918f7917c00SJeff Kirsher 		dev_err(adap->pdev_dev,
919f7917c00SJeff Kirsher 			"%s: Tx ring %u full while queue awake!\n",
920f7917c00SJeff Kirsher 			dev->name, qidx);
921f7917c00SJeff Kirsher 		return NETDEV_TX_BUSY;
922f7917c00SJeff Kirsher 	}
923f7917c00SJeff Kirsher 
924f7917c00SJeff Kirsher 	if (!is_eth_imm(skb) &&
925f7917c00SJeff Kirsher 	    unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
926f7917c00SJeff Kirsher 		q->mapping_err++;
927f7917c00SJeff Kirsher 		goto out_free;
928f7917c00SJeff Kirsher 	}
929f7917c00SJeff Kirsher 
930f7917c00SJeff Kirsher 	wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
931f7917c00SJeff Kirsher 	if (unlikely(credits < ETHTXQ_STOP_THRES)) {
932f7917c00SJeff Kirsher 		eth_txq_stop(q);
933f7917c00SJeff Kirsher 		wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
934f7917c00SJeff Kirsher 	}
935f7917c00SJeff Kirsher 
936f7917c00SJeff Kirsher 	wr = (void *)&q->q.desc[q->q.pidx];
937f7917c00SJeff Kirsher 	wr->equiq_to_len16 = htonl(wr_mid);
938f7917c00SJeff Kirsher 	wr->r3 = cpu_to_be64(0);
939f7917c00SJeff Kirsher 	end = (u64 *)wr + flits;
940f7917c00SJeff Kirsher 
941f7917c00SJeff Kirsher 	ssi = skb_shinfo(skb);
942f7917c00SJeff Kirsher 	if (ssi->gso_size) {
943f7917c00SJeff Kirsher 		struct cpl_tx_pkt_lso *lso = (void *)wr;
944f7917c00SJeff Kirsher 		bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
945f7917c00SJeff Kirsher 		int l3hdr_len = skb_network_header_len(skb);
946f7917c00SJeff Kirsher 		int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
947f7917c00SJeff Kirsher 
948f7917c00SJeff Kirsher 		wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
949f7917c00SJeff Kirsher 				       FW_WR_IMMDLEN(sizeof(*lso)));
950f7917c00SJeff Kirsher 		lso->c.lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
951f7917c00SJeff Kirsher 					LSO_FIRST_SLICE | LSO_LAST_SLICE |
952f7917c00SJeff Kirsher 					LSO_IPV6(v6) |
953f7917c00SJeff Kirsher 					LSO_ETHHDR_LEN(eth_xtra_len / 4) |
954f7917c00SJeff Kirsher 					LSO_IPHDR_LEN(l3hdr_len / 4) |
955f7917c00SJeff Kirsher 					LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
956f7917c00SJeff Kirsher 		lso->c.ipid_ofst = htons(0);
957f7917c00SJeff Kirsher 		lso->c.mss = htons(ssi->gso_size);
958f7917c00SJeff Kirsher 		lso->c.seqno_offset = htonl(0);
959f7917c00SJeff Kirsher 		lso->c.len = htonl(skb->len);
960f7917c00SJeff Kirsher 		cpl = (void *)(lso + 1);
961f7917c00SJeff Kirsher 		cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
962f7917c00SJeff Kirsher 			TXPKT_IPHDR_LEN(l3hdr_len) |
963f7917c00SJeff Kirsher 			TXPKT_ETHHDR_LEN(eth_xtra_len);
964f7917c00SJeff Kirsher 		q->tso++;
965f7917c00SJeff Kirsher 		q->tx_cso += ssi->gso_segs;
966f7917c00SJeff Kirsher 	} else {
967f7917c00SJeff Kirsher 		int len;
968f7917c00SJeff Kirsher 
969f7917c00SJeff Kirsher 		len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
970f7917c00SJeff Kirsher 		wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
971f7917c00SJeff Kirsher 				       FW_WR_IMMDLEN(len));
972f7917c00SJeff Kirsher 		cpl = (void *)(wr + 1);
973f7917c00SJeff Kirsher 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
974f7917c00SJeff Kirsher 			cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
975f7917c00SJeff Kirsher 			q->tx_cso++;
976f7917c00SJeff Kirsher 		} else
977f7917c00SJeff Kirsher 			cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
978f7917c00SJeff Kirsher 	}
979f7917c00SJeff Kirsher 
980f7917c00SJeff Kirsher 	if (vlan_tx_tag_present(skb)) {
981f7917c00SJeff Kirsher 		q->vlan_ins++;
982f7917c00SJeff Kirsher 		cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
983f7917c00SJeff Kirsher 	}
984f7917c00SJeff Kirsher 
985f7917c00SJeff Kirsher 	cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
986f7917c00SJeff Kirsher 			   TXPKT_INTF(pi->tx_chan) | TXPKT_PF(adap->fn));
987f7917c00SJeff Kirsher 	cpl->pack = htons(0);
988f7917c00SJeff Kirsher 	cpl->len = htons(skb->len);
989f7917c00SJeff Kirsher 	cpl->ctrl1 = cpu_to_be64(cntrl);
990f7917c00SJeff Kirsher 
991f7917c00SJeff Kirsher 	if (is_eth_imm(skb)) {
992f7917c00SJeff Kirsher 		inline_tx_skb(skb, &q->q, cpl + 1);
993f7917c00SJeff Kirsher 		dev_kfree_skb(skb);
994f7917c00SJeff Kirsher 	} else {
995f7917c00SJeff Kirsher 		int last_desc;
996f7917c00SJeff Kirsher 
997f7917c00SJeff Kirsher 		write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
998f7917c00SJeff Kirsher 			  addr);
999f7917c00SJeff Kirsher 		skb_orphan(skb);
1000f7917c00SJeff Kirsher 
1001f7917c00SJeff Kirsher 		last_desc = q->q.pidx + ndesc - 1;
1002f7917c00SJeff Kirsher 		if (last_desc >= q->q.size)
1003f7917c00SJeff Kirsher 			last_desc -= q->q.size;
1004f7917c00SJeff Kirsher 		q->q.sdesc[last_desc].skb = skb;
1005f7917c00SJeff Kirsher 		q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
1006f7917c00SJeff Kirsher 	}
1007f7917c00SJeff Kirsher 
1008f7917c00SJeff Kirsher 	txq_advance(&q->q, ndesc);
1009f7917c00SJeff Kirsher 
1010f7917c00SJeff Kirsher 	ring_tx_db(adap, &q->q, ndesc);
1011f7917c00SJeff Kirsher 	return NETDEV_TX_OK;
1012f7917c00SJeff Kirsher }
1013f7917c00SJeff Kirsher 
1014f7917c00SJeff Kirsher /**
1015f7917c00SJeff Kirsher  *	reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
1016f7917c00SJeff Kirsher  *	@q: the SGE control Tx queue
1017f7917c00SJeff Kirsher  *
1018f7917c00SJeff Kirsher  *	This is a variant of reclaim_completed_tx() that is used for Tx queues
1019f7917c00SJeff Kirsher  *	that send only immediate data (presently just the control queues) and
1020f7917c00SJeff Kirsher  *	thus do not have any sk_buffs to release.
1021f7917c00SJeff Kirsher  */
1022f7917c00SJeff Kirsher static inline void reclaim_completed_tx_imm(struct sge_txq *q)
1023f7917c00SJeff Kirsher {
1024f7917c00SJeff Kirsher 	int hw_cidx = ntohs(q->stat->cidx);
1025f7917c00SJeff Kirsher 	int reclaim = hw_cidx - q->cidx;
1026f7917c00SJeff Kirsher 
1027f7917c00SJeff Kirsher 	if (reclaim < 0)
1028f7917c00SJeff Kirsher 		reclaim += q->size;
1029f7917c00SJeff Kirsher 
1030f7917c00SJeff Kirsher 	q->in_use -= reclaim;
1031f7917c00SJeff Kirsher 	q->cidx = hw_cidx;
1032f7917c00SJeff Kirsher }
1033f7917c00SJeff Kirsher 
1034f7917c00SJeff Kirsher /**
1035f7917c00SJeff Kirsher  *	is_imm - check whether a packet can be sent as immediate data
1036f7917c00SJeff Kirsher  *	@skb: the packet
1037f7917c00SJeff Kirsher  *
1038f7917c00SJeff Kirsher  *	Returns true if a packet can be sent as a WR with immediate data.
1039f7917c00SJeff Kirsher  */
1040f7917c00SJeff Kirsher static inline int is_imm(const struct sk_buff *skb)
1041f7917c00SJeff Kirsher {
1042f7917c00SJeff Kirsher 	return skb->len <= MAX_CTRL_WR_LEN;
1043f7917c00SJeff Kirsher }
1044f7917c00SJeff Kirsher 
1045f7917c00SJeff Kirsher /**
1046f7917c00SJeff Kirsher  *	ctrlq_check_stop - check if a control queue is full and should stop
1047f7917c00SJeff Kirsher  *	@q: the queue
1048f7917c00SJeff Kirsher  *	@wr: most recent WR written to the queue
1049f7917c00SJeff Kirsher  *
1050f7917c00SJeff Kirsher  *	Check if a control queue has become full and should be stopped.
1051f7917c00SJeff Kirsher  *	We clean up control queue descriptors very lazily, only when we are out.
1052f7917c00SJeff Kirsher  *	If the queue is still full after reclaiming any completed descriptors
1053f7917c00SJeff Kirsher  *	we suspend it and have the last WR wake it up.
1054f7917c00SJeff Kirsher  */
1055f7917c00SJeff Kirsher static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
1056f7917c00SJeff Kirsher {
1057f7917c00SJeff Kirsher 	reclaim_completed_tx_imm(&q->q);
1058f7917c00SJeff Kirsher 	if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1059f7917c00SJeff Kirsher 		wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
1060f7917c00SJeff Kirsher 		q->q.stops++;
1061f7917c00SJeff Kirsher 		q->full = 1;
1062f7917c00SJeff Kirsher 	}
1063f7917c00SJeff Kirsher }
1064f7917c00SJeff Kirsher 
1065f7917c00SJeff Kirsher /**
1066f7917c00SJeff Kirsher  *	ctrl_xmit - send a packet through an SGE control Tx queue
1067f7917c00SJeff Kirsher  *	@q: the control queue
1068f7917c00SJeff Kirsher  *	@skb: the packet
1069f7917c00SJeff Kirsher  *
1070f7917c00SJeff Kirsher  *	Send a packet through an SGE control Tx queue.  Packets sent through
1071f7917c00SJeff Kirsher  *	a control queue must fit entirely as immediate data.
1072f7917c00SJeff Kirsher  */
1073f7917c00SJeff Kirsher static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
1074f7917c00SJeff Kirsher {
1075f7917c00SJeff Kirsher 	unsigned int ndesc;
1076f7917c00SJeff Kirsher 	struct fw_wr_hdr *wr;
1077f7917c00SJeff Kirsher 
1078f7917c00SJeff Kirsher 	if (unlikely(!is_imm(skb))) {
1079f7917c00SJeff Kirsher 		WARN_ON(1);
1080f7917c00SJeff Kirsher 		dev_kfree_skb(skb);
1081f7917c00SJeff Kirsher 		return NET_XMIT_DROP;
1082f7917c00SJeff Kirsher 	}
1083f7917c00SJeff Kirsher 
1084f7917c00SJeff Kirsher 	ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
1085f7917c00SJeff Kirsher 	spin_lock(&q->sendq.lock);
1086f7917c00SJeff Kirsher 
1087f7917c00SJeff Kirsher 	if (unlikely(q->full)) {
1088f7917c00SJeff Kirsher 		skb->priority = ndesc;                  /* save for restart */
1089f7917c00SJeff Kirsher 		__skb_queue_tail(&q->sendq, skb);
1090f7917c00SJeff Kirsher 		spin_unlock(&q->sendq.lock);
1091f7917c00SJeff Kirsher 		return NET_XMIT_CN;
1092f7917c00SJeff Kirsher 	}
1093f7917c00SJeff Kirsher 
1094f7917c00SJeff Kirsher 	wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1095f7917c00SJeff Kirsher 	inline_tx_skb(skb, &q->q, wr);
1096f7917c00SJeff Kirsher 
1097f7917c00SJeff Kirsher 	txq_advance(&q->q, ndesc);
1098f7917c00SJeff Kirsher 	if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
1099f7917c00SJeff Kirsher 		ctrlq_check_stop(q, wr);
1100f7917c00SJeff Kirsher 
1101f7917c00SJeff Kirsher 	ring_tx_db(q->adap, &q->q, ndesc);
1102f7917c00SJeff Kirsher 	spin_unlock(&q->sendq.lock);
1103f7917c00SJeff Kirsher 
1104f7917c00SJeff Kirsher 	kfree_skb(skb);
1105f7917c00SJeff Kirsher 	return NET_XMIT_SUCCESS;
1106f7917c00SJeff Kirsher }
1107f7917c00SJeff Kirsher 
1108f7917c00SJeff Kirsher /**
1109f7917c00SJeff Kirsher  *	restart_ctrlq - restart a suspended control queue
1110f7917c00SJeff Kirsher  *	@data: the control queue to restart
1111f7917c00SJeff Kirsher  *
1112f7917c00SJeff Kirsher  *	Resumes transmission on a suspended Tx control queue.
1113f7917c00SJeff Kirsher  */
1114f7917c00SJeff Kirsher static void restart_ctrlq(unsigned long data)
1115f7917c00SJeff Kirsher {
1116f7917c00SJeff Kirsher 	struct sk_buff *skb;
1117f7917c00SJeff Kirsher 	unsigned int written = 0;
1118f7917c00SJeff Kirsher 	struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
1119f7917c00SJeff Kirsher 
1120f7917c00SJeff Kirsher 	spin_lock(&q->sendq.lock);
1121f7917c00SJeff Kirsher 	reclaim_completed_tx_imm(&q->q);
1122f7917c00SJeff Kirsher 	BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES);  /* q should be empty */
1123f7917c00SJeff Kirsher 
1124f7917c00SJeff Kirsher 	while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
1125f7917c00SJeff Kirsher 		struct fw_wr_hdr *wr;
1126f7917c00SJeff Kirsher 		unsigned int ndesc = skb->priority;     /* previously saved */
1127f7917c00SJeff Kirsher 
1128f7917c00SJeff Kirsher 		/*
1129f7917c00SJeff Kirsher 		 * Write descriptors and free skbs outside the lock to limit
1130f7917c00SJeff Kirsher 		 * wait times.  q->full is still set so new skbs will be queued.
1131f7917c00SJeff Kirsher 		 */
1132f7917c00SJeff Kirsher 		spin_unlock(&q->sendq.lock);
1133f7917c00SJeff Kirsher 
1134f7917c00SJeff Kirsher 		wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1135f7917c00SJeff Kirsher 		inline_tx_skb(skb, &q->q, wr);
1136f7917c00SJeff Kirsher 		kfree_skb(skb);
1137f7917c00SJeff Kirsher 
1138f7917c00SJeff Kirsher 		written += ndesc;
1139f7917c00SJeff Kirsher 		txq_advance(&q->q, ndesc);
1140f7917c00SJeff Kirsher 		if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
1141f7917c00SJeff Kirsher 			unsigned long old = q->q.stops;
1142f7917c00SJeff Kirsher 
1143f7917c00SJeff Kirsher 			ctrlq_check_stop(q, wr);
1144f7917c00SJeff Kirsher 			if (q->q.stops != old) {          /* suspended anew */
1145f7917c00SJeff Kirsher 				spin_lock(&q->sendq.lock);
1146f7917c00SJeff Kirsher 				goto ringdb;
1147f7917c00SJeff Kirsher 			}
1148f7917c00SJeff Kirsher 		}
1149f7917c00SJeff Kirsher 		if (written > 16) {
1150f7917c00SJeff Kirsher 			ring_tx_db(q->adap, &q->q, written);
1151f7917c00SJeff Kirsher 			written = 0;
1152f7917c00SJeff Kirsher 		}
1153f7917c00SJeff Kirsher 		spin_lock(&q->sendq.lock);
1154f7917c00SJeff Kirsher 	}
1155f7917c00SJeff Kirsher 	q->full = 0;
1156f7917c00SJeff Kirsher ringdb: if (written)
1157f7917c00SJeff Kirsher 		ring_tx_db(q->adap, &q->q, written);
1158f7917c00SJeff Kirsher 	spin_unlock(&q->sendq.lock);
1159f7917c00SJeff Kirsher }
1160f7917c00SJeff Kirsher 
1161f7917c00SJeff Kirsher /**
1162f7917c00SJeff Kirsher  *	t4_mgmt_tx - send a management message
1163f7917c00SJeff Kirsher  *	@adap: the adapter
1164f7917c00SJeff Kirsher  *	@skb: the packet containing the management message
1165f7917c00SJeff Kirsher  *
1166f7917c00SJeff Kirsher  *	Send a management message through control queue 0.
1167f7917c00SJeff Kirsher  */
1168f7917c00SJeff Kirsher int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
1169f7917c00SJeff Kirsher {
1170f7917c00SJeff Kirsher 	int ret;
1171f7917c00SJeff Kirsher 
1172f7917c00SJeff Kirsher 	local_bh_disable();
1173f7917c00SJeff Kirsher 	ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
1174f7917c00SJeff Kirsher 	local_bh_enable();
1175f7917c00SJeff Kirsher 	return ret;
1176f7917c00SJeff Kirsher }
1177f7917c00SJeff Kirsher 
1178f7917c00SJeff Kirsher /**
1179f7917c00SJeff Kirsher  *	is_ofld_imm - check whether a packet can be sent as immediate data
1180f7917c00SJeff Kirsher  *	@skb: the packet
1181f7917c00SJeff Kirsher  *
1182f7917c00SJeff Kirsher  *	Returns true if a packet can be sent as an offload WR with immediate
1183f7917c00SJeff Kirsher  *	data.  We currently use the same limit as for Ethernet packets.
1184f7917c00SJeff Kirsher  */
1185f7917c00SJeff Kirsher static inline int is_ofld_imm(const struct sk_buff *skb)
1186f7917c00SJeff Kirsher {
1187f7917c00SJeff Kirsher 	return skb->len <= MAX_IMM_TX_PKT_LEN;
1188f7917c00SJeff Kirsher }
1189f7917c00SJeff Kirsher 
1190f7917c00SJeff Kirsher /**
1191f7917c00SJeff Kirsher  *	calc_tx_flits_ofld - calculate # of flits for an offload packet
1192f7917c00SJeff Kirsher  *	@skb: the packet
1193f7917c00SJeff Kirsher  *
1194f7917c00SJeff Kirsher  *	Returns the number of flits needed for the given offload packet.
1195f7917c00SJeff Kirsher  *	These packets are already fully constructed and no additional headers
1196f7917c00SJeff Kirsher  *	will be added.
1197f7917c00SJeff Kirsher  */
1198f7917c00SJeff Kirsher static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
1199f7917c00SJeff Kirsher {
1200f7917c00SJeff Kirsher 	unsigned int flits, cnt;
1201f7917c00SJeff Kirsher 
1202f7917c00SJeff Kirsher 	if (is_ofld_imm(skb))
1203f7917c00SJeff Kirsher 		return DIV_ROUND_UP(skb->len, 8);
1204f7917c00SJeff Kirsher 
1205f7917c00SJeff Kirsher 	flits = skb_transport_offset(skb) / 8U;   /* headers */
1206f7917c00SJeff Kirsher 	cnt = skb_shinfo(skb)->nr_frags;
1207f7917c00SJeff Kirsher 	if (skb->tail != skb->transport_header)
1208f7917c00SJeff Kirsher 		cnt++;
1209f7917c00SJeff Kirsher 	return flits + sgl_len(cnt);
1210f7917c00SJeff Kirsher }
1211f7917c00SJeff Kirsher 
1212f7917c00SJeff Kirsher /**
1213f7917c00SJeff Kirsher  *	txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
1214f7917c00SJeff Kirsher  *	@adap: the adapter
1215f7917c00SJeff Kirsher  *	@q: the queue to stop
1216f7917c00SJeff Kirsher  *
1217f7917c00SJeff Kirsher  *	Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
1218f7917c00SJeff Kirsher  *	inability to map packets.  A periodic timer attempts to restart
1219f7917c00SJeff Kirsher  *	queues so marked.
1220f7917c00SJeff Kirsher  */
1221f7917c00SJeff Kirsher static void txq_stop_maperr(struct sge_ofld_txq *q)
1222f7917c00SJeff Kirsher {
1223f7917c00SJeff Kirsher 	q->mapping_err++;
1224f7917c00SJeff Kirsher 	q->q.stops++;
1225f7917c00SJeff Kirsher 	set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
1226f7917c00SJeff Kirsher 		q->adap->sge.txq_maperr);
1227f7917c00SJeff Kirsher }
1228f7917c00SJeff Kirsher 
1229f7917c00SJeff Kirsher /**
1230f7917c00SJeff Kirsher  *	ofldtxq_stop - stop an offload Tx queue that has become full
1231f7917c00SJeff Kirsher  *	@q: the queue to stop
1232f7917c00SJeff Kirsher  *	@skb: the packet causing the queue to become full
1233f7917c00SJeff Kirsher  *
1234f7917c00SJeff Kirsher  *	Stops an offload Tx queue that has become full and modifies the packet
1235f7917c00SJeff Kirsher  *	being written to request a wakeup.
1236f7917c00SJeff Kirsher  */
1237f7917c00SJeff Kirsher static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
1238f7917c00SJeff Kirsher {
1239f7917c00SJeff Kirsher 	struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
1240f7917c00SJeff Kirsher 
1241f7917c00SJeff Kirsher 	wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
1242f7917c00SJeff Kirsher 	q->q.stops++;
1243f7917c00SJeff Kirsher 	q->full = 1;
1244f7917c00SJeff Kirsher }
1245f7917c00SJeff Kirsher 
1246f7917c00SJeff Kirsher /**
1247f7917c00SJeff Kirsher  *	service_ofldq - restart a suspended offload queue
1248f7917c00SJeff Kirsher  *	@q: the offload queue
1249f7917c00SJeff Kirsher  *
1250f7917c00SJeff Kirsher  *	Services an offload Tx queue by moving packets from its packet queue
1251f7917c00SJeff Kirsher  *	to the HW Tx ring.  The function starts and ends with the queue locked.
1252f7917c00SJeff Kirsher  */
1253f7917c00SJeff Kirsher static void service_ofldq(struct sge_ofld_txq *q)
1254f7917c00SJeff Kirsher {
1255f7917c00SJeff Kirsher 	u64 *pos;
1256f7917c00SJeff Kirsher 	int credits;
1257f7917c00SJeff Kirsher 	struct sk_buff *skb;
1258f7917c00SJeff Kirsher 	unsigned int written = 0;
1259f7917c00SJeff Kirsher 	unsigned int flits, ndesc;
1260f7917c00SJeff Kirsher 
1261f7917c00SJeff Kirsher 	while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
1262f7917c00SJeff Kirsher 		/*
1263f7917c00SJeff Kirsher 		 * We drop the lock but leave skb on sendq, thus retaining
1264f7917c00SJeff Kirsher 		 * exclusive access to the state of the queue.
1265f7917c00SJeff Kirsher 		 */
1266f7917c00SJeff Kirsher 		spin_unlock(&q->sendq.lock);
1267f7917c00SJeff Kirsher 
1268f7917c00SJeff Kirsher 		reclaim_completed_tx(q->adap, &q->q, false);
1269f7917c00SJeff Kirsher 
1270f7917c00SJeff Kirsher 		flits = skb->priority;                /* previously saved */
1271f7917c00SJeff Kirsher 		ndesc = flits_to_desc(flits);
1272f7917c00SJeff Kirsher 		credits = txq_avail(&q->q) - ndesc;
1273f7917c00SJeff Kirsher 		BUG_ON(credits < 0);
1274f7917c00SJeff Kirsher 		if (unlikely(credits < TXQ_STOP_THRES))
1275f7917c00SJeff Kirsher 			ofldtxq_stop(q, skb);
1276f7917c00SJeff Kirsher 
1277f7917c00SJeff Kirsher 		pos = (u64 *)&q->q.desc[q->q.pidx];
1278f7917c00SJeff Kirsher 		if (is_ofld_imm(skb))
1279f7917c00SJeff Kirsher 			inline_tx_skb(skb, &q->q, pos);
1280f7917c00SJeff Kirsher 		else if (map_skb(q->adap->pdev_dev, skb,
1281f7917c00SJeff Kirsher 				 (dma_addr_t *)skb->head)) {
1282f7917c00SJeff Kirsher 			txq_stop_maperr(q);
1283f7917c00SJeff Kirsher 			spin_lock(&q->sendq.lock);
1284f7917c00SJeff Kirsher 			break;
1285f7917c00SJeff Kirsher 		} else {
1286f7917c00SJeff Kirsher 			int last_desc, hdr_len = skb_transport_offset(skb);
1287f7917c00SJeff Kirsher 
1288f7917c00SJeff Kirsher 			memcpy(pos, skb->data, hdr_len);
1289f7917c00SJeff Kirsher 			write_sgl(skb, &q->q, (void *)pos + hdr_len,
1290f7917c00SJeff Kirsher 				  pos + flits, hdr_len,
1291f7917c00SJeff Kirsher 				  (dma_addr_t *)skb->head);
1292f7917c00SJeff Kirsher #ifdef CONFIG_NEED_DMA_MAP_STATE
1293f7917c00SJeff Kirsher 			skb->dev = q->adap->port[0];
1294f7917c00SJeff Kirsher 			skb->destructor = deferred_unmap_destructor;
1295f7917c00SJeff Kirsher #endif
1296f7917c00SJeff Kirsher 			last_desc = q->q.pidx + ndesc - 1;
1297f7917c00SJeff Kirsher 			if (last_desc >= q->q.size)
1298f7917c00SJeff Kirsher 				last_desc -= q->q.size;
1299f7917c00SJeff Kirsher 			q->q.sdesc[last_desc].skb = skb;
1300f7917c00SJeff Kirsher 		}
1301f7917c00SJeff Kirsher 
1302f7917c00SJeff Kirsher 		txq_advance(&q->q, ndesc);
1303f7917c00SJeff Kirsher 		written += ndesc;
1304f7917c00SJeff Kirsher 		if (unlikely(written > 32)) {
1305f7917c00SJeff Kirsher 			ring_tx_db(q->adap, &q->q, written);
1306f7917c00SJeff Kirsher 			written = 0;
1307f7917c00SJeff Kirsher 		}
1308f7917c00SJeff Kirsher 
1309f7917c00SJeff Kirsher 		spin_lock(&q->sendq.lock);
1310f7917c00SJeff Kirsher 		__skb_unlink(skb, &q->sendq);
1311f7917c00SJeff Kirsher 		if (is_ofld_imm(skb))
1312f7917c00SJeff Kirsher 			kfree_skb(skb);
1313f7917c00SJeff Kirsher 	}
1314f7917c00SJeff Kirsher 	if (likely(written))
1315f7917c00SJeff Kirsher 		ring_tx_db(q->adap, &q->q, written);
1316f7917c00SJeff Kirsher }
1317f7917c00SJeff Kirsher 
1318f7917c00SJeff Kirsher /**
1319f7917c00SJeff Kirsher  *	ofld_xmit - send a packet through an offload queue
1320f7917c00SJeff Kirsher  *	@q: the Tx offload queue
1321f7917c00SJeff Kirsher  *	@skb: the packet
1322f7917c00SJeff Kirsher  *
1323f7917c00SJeff Kirsher  *	Send an offload packet through an SGE offload queue.
1324f7917c00SJeff Kirsher  */
1325f7917c00SJeff Kirsher static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
1326f7917c00SJeff Kirsher {
1327f7917c00SJeff Kirsher 	skb->priority = calc_tx_flits_ofld(skb);       /* save for restart */
1328f7917c00SJeff Kirsher 	spin_lock(&q->sendq.lock);
1329f7917c00SJeff Kirsher 	__skb_queue_tail(&q->sendq, skb);
1330f7917c00SJeff Kirsher 	if (q->sendq.qlen == 1)
1331f7917c00SJeff Kirsher 		service_ofldq(q);
1332f7917c00SJeff Kirsher 	spin_unlock(&q->sendq.lock);
1333f7917c00SJeff Kirsher 	return NET_XMIT_SUCCESS;
1334f7917c00SJeff Kirsher }
1335f7917c00SJeff Kirsher 
1336f7917c00SJeff Kirsher /**
1337f7917c00SJeff Kirsher  *	restart_ofldq - restart a suspended offload queue
1338f7917c00SJeff Kirsher  *	@data: the offload queue to restart
1339f7917c00SJeff Kirsher  *
1340f7917c00SJeff Kirsher  *	Resumes transmission on a suspended Tx offload queue.
1341f7917c00SJeff Kirsher  */
1342f7917c00SJeff Kirsher static void restart_ofldq(unsigned long data)
1343f7917c00SJeff Kirsher {
1344f7917c00SJeff Kirsher 	struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
1345f7917c00SJeff Kirsher 
1346f7917c00SJeff Kirsher 	spin_lock(&q->sendq.lock);
1347f7917c00SJeff Kirsher 	q->full = 0;            /* the queue actually is completely empty now */
1348f7917c00SJeff Kirsher 	service_ofldq(q);
1349f7917c00SJeff Kirsher 	spin_unlock(&q->sendq.lock);
1350f7917c00SJeff Kirsher }
1351f7917c00SJeff Kirsher 
1352f7917c00SJeff Kirsher /**
1353f7917c00SJeff Kirsher  *	skb_txq - return the Tx queue an offload packet should use
1354f7917c00SJeff Kirsher  *	@skb: the packet
1355f7917c00SJeff Kirsher  *
1356f7917c00SJeff Kirsher  *	Returns the Tx queue an offload packet should use as indicated by bits
1357f7917c00SJeff Kirsher  *	1-15 in the packet's queue_mapping.
1358f7917c00SJeff Kirsher  */
1359f7917c00SJeff Kirsher static inline unsigned int skb_txq(const struct sk_buff *skb)
1360f7917c00SJeff Kirsher {
1361f7917c00SJeff Kirsher 	return skb->queue_mapping >> 1;
1362f7917c00SJeff Kirsher }
1363f7917c00SJeff Kirsher 
1364f7917c00SJeff Kirsher /**
1365f7917c00SJeff Kirsher  *	is_ctrl_pkt - return whether an offload packet is a control packet
1366f7917c00SJeff Kirsher  *	@skb: the packet
1367f7917c00SJeff Kirsher  *
1368f7917c00SJeff Kirsher  *	Returns whether an offload packet should use an OFLD or a CTRL
1369f7917c00SJeff Kirsher  *	Tx queue as indicated by bit 0 in the packet's queue_mapping.
1370f7917c00SJeff Kirsher  */
1371f7917c00SJeff Kirsher static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
1372f7917c00SJeff Kirsher {
1373f7917c00SJeff Kirsher 	return skb->queue_mapping & 1;
1374f7917c00SJeff Kirsher }
1375f7917c00SJeff Kirsher 
1376f7917c00SJeff Kirsher static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
1377f7917c00SJeff Kirsher {
1378f7917c00SJeff Kirsher 	unsigned int idx = skb_txq(skb);
1379f7917c00SJeff Kirsher 
1380f7917c00SJeff Kirsher 	if (unlikely(is_ctrl_pkt(skb)))
1381f7917c00SJeff Kirsher 		return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
1382f7917c00SJeff Kirsher 	return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
1383f7917c00SJeff Kirsher }
1384f7917c00SJeff Kirsher 
1385f7917c00SJeff Kirsher /**
1386f7917c00SJeff Kirsher  *	t4_ofld_send - send an offload packet
1387f7917c00SJeff Kirsher  *	@adap: the adapter
1388f7917c00SJeff Kirsher  *	@skb: the packet
1389f7917c00SJeff Kirsher  *
1390f7917c00SJeff Kirsher  *	Sends an offload packet.  We use the packet queue_mapping to select the
1391f7917c00SJeff Kirsher  *	appropriate Tx queue as follows: bit 0 indicates whether the packet
1392f7917c00SJeff Kirsher  *	should be sent as regular or control, bits 1-15 select the queue.
1393f7917c00SJeff Kirsher  */
1394f7917c00SJeff Kirsher int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
1395f7917c00SJeff Kirsher {
1396f7917c00SJeff Kirsher 	int ret;
1397f7917c00SJeff Kirsher 
1398f7917c00SJeff Kirsher 	local_bh_disable();
1399f7917c00SJeff Kirsher 	ret = ofld_send(adap, skb);
1400f7917c00SJeff Kirsher 	local_bh_enable();
1401f7917c00SJeff Kirsher 	return ret;
1402f7917c00SJeff Kirsher }
1403f7917c00SJeff Kirsher 
1404f7917c00SJeff Kirsher /**
1405f7917c00SJeff Kirsher  *	cxgb4_ofld_send - send an offload packet
1406f7917c00SJeff Kirsher  *	@dev: the net device
1407f7917c00SJeff Kirsher  *	@skb: the packet
1408f7917c00SJeff Kirsher  *
1409f7917c00SJeff Kirsher  *	Sends an offload packet.  This is an exported version of @t4_ofld_send,
1410f7917c00SJeff Kirsher  *	intended for ULDs.
1411f7917c00SJeff Kirsher  */
1412f7917c00SJeff Kirsher int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
1413f7917c00SJeff Kirsher {
1414f7917c00SJeff Kirsher 	return t4_ofld_send(netdev2adap(dev), skb);
1415f7917c00SJeff Kirsher }
1416f7917c00SJeff Kirsher EXPORT_SYMBOL(cxgb4_ofld_send);
1417f7917c00SJeff Kirsher 
1418e91b0f24SIan Campbell static inline void copy_frags(struct sk_buff *skb,
1419f7917c00SJeff Kirsher 			      const struct pkt_gl *gl, unsigned int offset)
1420f7917c00SJeff Kirsher {
1421e91b0f24SIan Campbell 	int i;
1422f7917c00SJeff Kirsher 
1423f7917c00SJeff Kirsher 	/* usually there's just one frag */
1424e91b0f24SIan Campbell 	__skb_fill_page_desc(skb, 0, gl->frags[0].page,
1425e91b0f24SIan Campbell 			     gl->frags[0].offset + offset,
1426e91b0f24SIan Campbell 			     gl->frags[0].size - offset);
1427e91b0f24SIan Campbell 	skb_shinfo(skb)->nr_frags = gl->nfrags;
1428e91b0f24SIan Campbell 	for (i = 1; i < gl->nfrags; i++)
1429e91b0f24SIan Campbell 		__skb_fill_page_desc(skb, i, gl->frags[i].page,
1430e91b0f24SIan Campbell 				     gl->frags[i].offset,
1431e91b0f24SIan Campbell 				     gl->frags[i].size);
1432f7917c00SJeff Kirsher 
1433f7917c00SJeff Kirsher 	/* get a reference to the last page, we don't own it */
1434e91b0f24SIan Campbell 	get_page(gl->frags[gl->nfrags - 1].page);
1435f7917c00SJeff Kirsher }
1436f7917c00SJeff Kirsher 
1437f7917c00SJeff Kirsher /**
1438f7917c00SJeff Kirsher  *	cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
1439f7917c00SJeff Kirsher  *	@gl: the gather list
1440f7917c00SJeff Kirsher  *	@skb_len: size of sk_buff main body if it carries fragments
1441f7917c00SJeff Kirsher  *	@pull_len: amount of data to move to the sk_buff's main body
1442f7917c00SJeff Kirsher  *
1443f7917c00SJeff Kirsher  *	Builds an sk_buff from the given packet gather list.  Returns the
1444f7917c00SJeff Kirsher  *	sk_buff or %NULL if sk_buff allocation failed.
1445f7917c00SJeff Kirsher  */
1446f7917c00SJeff Kirsher struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
1447f7917c00SJeff Kirsher 				   unsigned int skb_len, unsigned int pull_len)
1448f7917c00SJeff Kirsher {
1449f7917c00SJeff Kirsher 	struct sk_buff *skb;
1450f7917c00SJeff Kirsher 
1451f7917c00SJeff Kirsher 	/*
1452f7917c00SJeff Kirsher 	 * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
1453f7917c00SJeff Kirsher 	 * size, which is expected since buffers are at least PAGE_SIZEd.
1454f7917c00SJeff Kirsher 	 * In this case packets up to RX_COPY_THRES have only one fragment.
1455f7917c00SJeff Kirsher 	 */
1456f7917c00SJeff Kirsher 	if (gl->tot_len <= RX_COPY_THRES) {
1457f7917c00SJeff Kirsher 		skb = dev_alloc_skb(gl->tot_len);
1458f7917c00SJeff Kirsher 		if (unlikely(!skb))
1459f7917c00SJeff Kirsher 			goto out;
1460f7917c00SJeff Kirsher 		__skb_put(skb, gl->tot_len);
1461f7917c00SJeff Kirsher 		skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1462f7917c00SJeff Kirsher 	} else {
1463f7917c00SJeff Kirsher 		skb = dev_alloc_skb(skb_len);
1464f7917c00SJeff Kirsher 		if (unlikely(!skb))
1465f7917c00SJeff Kirsher 			goto out;
1466f7917c00SJeff Kirsher 		__skb_put(skb, pull_len);
1467f7917c00SJeff Kirsher 		skb_copy_to_linear_data(skb, gl->va, pull_len);
1468f7917c00SJeff Kirsher 
1469e91b0f24SIan Campbell 		copy_frags(skb, gl, pull_len);
1470f7917c00SJeff Kirsher 		skb->len = gl->tot_len;
1471f7917c00SJeff Kirsher 		skb->data_len = skb->len - pull_len;
1472f7917c00SJeff Kirsher 		skb->truesize += skb->data_len;
1473f7917c00SJeff Kirsher 	}
1474f7917c00SJeff Kirsher out:	return skb;
1475f7917c00SJeff Kirsher }
1476f7917c00SJeff Kirsher EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
1477f7917c00SJeff Kirsher 
1478f7917c00SJeff Kirsher /**
1479f7917c00SJeff Kirsher  *	t4_pktgl_free - free a packet gather list
1480f7917c00SJeff Kirsher  *	@gl: the gather list
1481f7917c00SJeff Kirsher  *
1482f7917c00SJeff Kirsher  *	Releases the pages of a packet gather list.  We do not own the last
1483f7917c00SJeff Kirsher  *	page on the list and do not free it.
1484f7917c00SJeff Kirsher  */
1485f7917c00SJeff Kirsher static void t4_pktgl_free(const struct pkt_gl *gl)
1486f7917c00SJeff Kirsher {
1487f7917c00SJeff Kirsher 	int n;
1488e91b0f24SIan Campbell 	const struct page_frag *p;
1489f7917c00SJeff Kirsher 
1490f7917c00SJeff Kirsher 	for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
1491f7917c00SJeff Kirsher 		put_page(p->page);
1492f7917c00SJeff Kirsher }
1493f7917c00SJeff Kirsher 
1494f7917c00SJeff Kirsher /*
1495f7917c00SJeff Kirsher  * Process an MPS trace packet.  Give it an unused protocol number so it won't
1496f7917c00SJeff Kirsher  * be delivered to anyone and send it to the stack for capture.
1497f7917c00SJeff Kirsher  */
1498f7917c00SJeff Kirsher static noinline int handle_trace_pkt(struct adapter *adap,
1499f7917c00SJeff Kirsher 				     const struct pkt_gl *gl)
1500f7917c00SJeff Kirsher {
1501f7917c00SJeff Kirsher 	struct sk_buff *skb;
1502f7917c00SJeff Kirsher 	struct cpl_trace_pkt *p;
1503f7917c00SJeff Kirsher 
1504f7917c00SJeff Kirsher 	skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
1505f7917c00SJeff Kirsher 	if (unlikely(!skb)) {
1506f7917c00SJeff Kirsher 		t4_pktgl_free(gl);
1507f7917c00SJeff Kirsher 		return 0;
1508f7917c00SJeff Kirsher 	}
1509f7917c00SJeff Kirsher 
1510f7917c00SJeff Kirsher 	p = (struct cpl_trace_pkt *)skb->data;
1511f7917c00SJeff Kirsher 	__skb_pull(skb, sizeof(*p));
1512f7917c00SJeff Kirsher 	skb_reset_mac_header(skb);
1513f7917c00SJeff Kirsher 	skb->protocol = htons(0xffff);
1514f7917c00SJeff Kirsher 	skb->dev = adap->port[0];
1515f7917c00SJeff Kirsher 	netif_receive_skb(skb);
1516f7917c00SJeff Kirsher 	return 0;
1517f7917c00SJeff Kirsher }
1518f7917c00SJeff Kirsher 
1519f7917c00SJeff Kirsher static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1520f7917c00SJeff Kirsher 		   const struct cpl_rx_pkt *pkt)
1521f7917c00SJeff Kirsher {
1522f7917c00SJeff Kirsher 	int ret;
1523f7917c00SJeff Kirsher 	struct sk_buff *skb;
1524f7917c00SJeff Kirsher 
1525f7917c00SJeff Kirsher 	skb = napi_get_frags(&rxq->rspq.napi);
1526f7917c00SJeff Kirsher 	if (unlikely(!skb)) {
1527f7917c00SJeff Kirsher 		t4_pktgl_free(gl);
1528f7917c00SJeff Kirsher 		rxq->stats.rx_drops++;
1529f7917c00SJeff Kirsher 		return;
1530f7917c00SJeff Kirsher 	}
1531f7917c00SJeff Kirsher 
1532e91b0f24SIan Campbell 	copy_frags(skb, gl, RX_PKT_PAD);
1533f7917c00SJeff Kirsher 	skb->len = gl->tot_len - RX_PKT_PAD;
1534f7917c00SJeff Kirsher 	skb->data_len = skb->len;
1535f7917c00SJeff Kirsher 	skb->truesize += skb->data_len;
1536f7917c00SJeff Kirsher 	skb->ip_summed = CHECKSUM_UNNECESSARY;
1537f7917c00SJeff Kirsher 	skb_record_rx_queue(skb, rxq->rspq.idx);
1538f7917c00SJeff Kirsher 	if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
1539f7917c00SJeff Kirsher 		skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
1540f7917c00SJeff Kirsher 
1541f7917c00SJeff Kirsher 	if (unlikely(pkt->vlan_ex)) {
1542f7917c00SJeff Kirsher 		__vlan_hwaccel_put_tag(skb, ntohs(pkt->vlan));
1543f7917c00SJeff Kirsher 		rxq->stats.vlan_ex++;
1544f7917c00SJeff Kirsher 	}
1545f7917c00SJeff Kirsher 	ret = napi_gro_frags(&rxq->rspq.napi);
1546f7917c00SJeff Kirsher 	if (ret == GRO_HELD)
1547f7917c00SJeff Kirsher 		rxq->stats.lro_pkts++;
1548f7917c00SJeff Kirsher 	else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1549f7917c00SJeff Kirsher 		rxq->stats.lro_merged++;
1550f7917c00SJeff Kirsher 	rxq->stats.pkts++;
1551f7917c00SJeff Kirsher 	rxq->stats.rx_cso++;
1552f7917c00SJeff Kirsher }
1553f7917c00SJeff Kirsher 
1554f7917c00SJeff Kirsher /**
1555f7917c00SJeff Kirsher  *	t4_ethrx_handler - process an ingress ethernet packet
1556f7917c00SJeff Kirsher  *	@q: the response queue that received the packet
1557f7917c00SJeff Kirsher  *	@rsp: the response queue descriptor holding the RX_PKT message
1558f7917c00SJeff Kirsher  *	@si: the gather list of packet fragments
1559f7917c00SJeff Kirsher  *
1560f7917c00SJeff Kirsher  *	Process an ingress ethernet packet and deliver it to the stack.
1561f7917c00SJeff Kirsher  */
1562f7917c00SJeff Kirsher int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
1563f7917c00SJeff Kirsher 		     const struct pkt_gl *si)
1564f7917c00SJeff Kirsher {
1565f7917c00SJeff Kirsher 	bool csum_ok;
1566f7917c00SJeff Kirsher 	struct sk_buff *skb;
1567f7917c00SJeff Kirsher 	const struct cpl_rx_pkt *pkt;
1568f7917c00SJeff Kirsher 	struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1569f7917c00SJeff Kirsher 
1570f7917c00SJeff Kirsher 	if (unlikely(*(u8 *)rsp == CPL_TRACE_PKT))
1571f7917c00SJeff Kirsher 		return handle_trace_pkt(q->adap, si);
1572f7917c00SJeff Kirsher 
1573f7917c00SJeff Kirsher 	pkt = (const struct cpl_rx_pkt *)rsp;
1574f7917c00SJeff Kirsher 	csum_ok = pkt->csum_calc && !pkt->err_vec;
1575f7917c00SJeff Kirsher 	if ((pkt->l2info & htonl(RXF_TCP)) &&
1576f7917c00SJeff Kirsher 	    (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
1577f7917c00SJeff Kirsher 		do_gro(rxq, si, pkt);
1578f7917c00SJeff Kirsher 		return 0;
1579f7917c00SJeff Kirsher 	}
1580f7917c00SJeff Kirsher 
1581f7917c00SJeff Kirsher 	skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
1582f7917c00SJeff Kirsher 	if (unlikely(!skb)) {
1583f7917c00SJeff Kirsher 		t4_pktgl_free(si);
1584f7917c00SJeff Kirsher 		rxq->stats.rx_drops++;
1585f7917c00SJeff Kirsher 		return 0;
1586f7917c00SJeff Kirsher 	}
1587f7917c00SJeff Kirsher 
1588f7917c00SJeff Kirsher 	__skb_pull(skb, RX_PKT_PAD);      /* remove ethernet header padding */
1589f7917c00SJeff Kirsher 	skb->protocol = eth_type_trans(skb, q->netdev);
1590f7917c00SJeff Kirsher 	skb_record_rx_queue(skb, q->idx);
1591f7917c00SJeff Kirsher 	if (skb->dev->features & NETIF_F_RXHASH)
1592f7917c00SJeff Kirsher 		skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
1593f7917c00SJeff Kirsher 
1594f7917c00SJeff Kirsher 	rxq->stats.pkts++;
1595f7917c00SJeff Kirsher 
1596f7917c00SJeff Kirsher 	if (csum_ok && (q->netdev->features & NETIF_F_RXCSUM) &&
1597f7917c00SJeff Kirsher 	    (pkt->l2info & htonl(RXF_UDP | RXF_TCP))) {
1598f7917c00SJeff Kirsher 		if (!pkt->ip_frag) {
1599f7917c00SJeff Kirsher 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1600f7917c00SJeff Kirsher 			rxq->stats.rx_cso++;
1601f7917c00SJeff Kirsher 		} else if (pkt->l2info & htonl(RXF_IP)) {
1602f7917c00SJeff Kirsher 			__sum16 c = (__force __sum16)pkt->csum;
1603f7917c00SJeff Kirsher 			skb->csum = csum_unfold(c);
1604f7917c00SJeff Kirsher 			skb->ip_summed = CHECKSUM_COMPLETE;
1605f7917c00SJeff Kirsher 			rxq->stats.rx_cso++;
1606f7917c00SJeff Kirsher 		}
1607f7917c00SJeff Kirsher 	} else
1608f7917c00SJeff Kirsher 		skb_checksum_none_assert(skb);
1609f7917c00SJeff Kirsher 
1610f7917c00SJeff Kirsher 	if (unlikely(pkt->vlan_ex)) {
1611f7917c00SJeff Kirsher 		__vlan_hwaccel_put_tag(skb, ntohs(pkt->vlan));
1612f7917c00SJeff Kirsher 		rxq->stats.vlan_ex++;
1613f7917c00SJeff Kirsher 	}
1614f7917c00SJeff Kirsher 	netif_receive_skb(skb);
1615f7917c00SJeff Kirsher 	return 0;
1616f7917c00SJeff Kirsher }
1617f7917c00SJeff Kirsher 
1618f7917c00SJeff Kirsher /**
1619f7917c00SJeff Kirsher  *	restore_rx_bufs - put back a packet's Rx buffers
1620f7917c00SJeff Kirsher  *	@si: the packet gather list
1621f7917c00SJeff Kirsher  *	@q: the SGE free list
1622f7917c00SJeff Kirsher  *	@frags: number of FL buffers to restore
1623f7917c00SJeff Kirsher  *
1624f7917c00SJeff Kirsher  *	Puts back on an FL the Rx buffers associated with @si.  The buffers
1625f7917c00SJeff Kirsher  *	have already been unmapped and are left unmapped, we mark them so to
1626f7917c00SJeff Kirsher  *	prevent further unmapping attempts.
1627f7917c00SJeff Kirsher  *
1628f7917c00SJeff Kirsher  *	This function undoes a series of @unmap_rx_buf calls when we find out
1629f7917c00SJeff Kirsher  *	that the current packet can't be processed right away afterall and we
1630f7917c00SJeff Kirsher  *	need to come back to it later.  This is a very rare event and there's
1631f7917c00SJeff Kirsher  *	no effort to make this particularly efficient.
1632f7917c00SJeff Kirsher  */
1633f7917c00SJeff Kirsher static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
1634f7917c00SJeff Kirsher 			    int frags)
1635f7917c00SJeff Kirsher {
1636f7917c00SJeff Kirsher 	struct rx_sw_desc *d;
1637f7917c00SJeff Kirsher 
1638f7917c00SJeff Kirsher 	while (frags--) {
1639f7917c00SJeff Kirsher 		if (q->cidx == 0)
1640f7917c00SJeff Kirsher 			q->cidx = q->size - 1;
1641f7917c00SJeff Kirsher 		else
1642f7917c00SJeff Kirsher 			q->cidx--;
1643f7917c00SJeff Kirsher 		d = &q->sdesc[q->cidx];
1644f7917c00SJeff Kirsher 		d->page = si->frags[frags].page;
1645f7917c00SJeff Kirsher 		d->dma_addr |= RX_UNMAPPED_BUF;
1646f7917c00SJeff Kirsher 		q->avail++;
1647f7917c00SJeff Kirsher 	}
1648f7917c00SJeff Kirsher }
1649f7917c00SJeff Kirsher 
1650f7917c00SJeff Kirsher /**
1651f7917c00SJeff Kirsher  *	is_new_response - check if a response is newly written
1652f7917c00SJeff Kirsher  *	@r: the response descriptor
1653f7917c00SJeff Kirsher  *	@q: the response queue
1654f7917c00SJeff Kirsher  *
1655f7917c00SJeff Kirsher  *	Returns true if a response descriptor contains a yet unprocessed
1656f7917c00SJeff Kirsher  *	response.
1657f7917c00SJeff Kirsher  */
1658f7917c00SJeff Kirsher static inline bool is_new_response(const struct rsp_ctrl *r,
1659f7917c00SJeff Kirsher 				   const struct sge_rspq *q)
1660f7917c00SJeff Kirsher {
1661f7917c00SJeff Kirsher 	return RSPD_GEN(r->type_gen) == q->gen;
1662f7917c00SJeff Kirsher }
1663f7917c00SJeff Kirsher 
1664f7917c00SJeff Kirsher /**
1665f7917c00SJeff Kirsher  *	rspq_next - advance to the next entry in a response queue
1666f7917c00SJeff Kirsher  *	@q: the queue
1667f7917c00SJeff Kirsher  *
1668f7917c00SJeff Kirsher  *	Updates the state of a response queue to advance it to the next entry.
1669f7917c00SJeff Kirsher  */
1670f7917c00SJeff Kirsher static inline void rspq_next(struct sge_rspq *q)
1671f7917c00SJeff Kirsher {
1672f7917c00SJeff Kirsher 	q->cur_desc = (void *)q->cur_desc + q->iqe_len;
1673f7917c00SJeff Kirsher 	if (unlikely(++q->cidx == q->size)) {
1674f7917c00SJeff Kirsher 		q->cidx = 0;
1675f7917c00SJeff Kirsher 		q->gen ^= 1;
1676f7917c00SJeff Kirsher 		q->cur_desc = q->desc;
1677f7917c00SJeff Kirsher 	}
1678f7917c00SJeff Kirsher }
1679f7917c00SJeff Kirsher 
1680f7917c00SJeff Kirsher /**
1681f7917c00SJeff Kirsher  *	process_responses - process responses from an SGE response queue
1682f7917c00SJeff Kirsher  *	@q: the ingress queue to process
1683f7917c00SJeff Kirsher  *	@budget: how many responses can be processed in this round
1684f7917c00SJeff Kirsher  *
1685f7917c00SJeff Kirsher  *	Process responses from an SGE response queue up to the supplied budget.
1686f7917c00SJeff Kirsher  *	Responses include received packets as well as control messages from FW
1687f7917c00SJeff Kirsher  *	or HW.
1688f7917c00SJeff Kirsher  *
1689f7917c00SJeff Kirsher  *	Additionally choose the interrupt holdoff time for the next interrupt
1690f7917c00SJeff Kirsher  *	on this queue.  If the system is under memory shortage use a fairly
1691f7917c00SJeff Kirsher  *	long delay to help recovery.
1692f7917c00SJeff Kirsher  */
1693f7917c00SJeff Kirsher static int process_responses(struct sge_rspq *q, int budget)
1694f7917c00SJeff Kirsher {
1695f7917c00SJeff Kirsher 	int ret, rsp_type;
1696f7917c00SJeff Kirsher 	int budget_left = budget;
1697f7917c00SJeff Kirsher 	const struct rsp_ctrl *rc;
1698f7917c00SJeff Kirsher 	struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1699f7917c00SJeff Kirsher 
1700f7917c00SJeff Kirsher 	while (likely(budget_left)) {
1701f7917c00SJeff Kirsher 		rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
1702f7917c00SJeff Kirsher 		if (!is_new_response(rc, q))
1703f7917c00SJeff Kirsher 			break;
1704f7917c00SJeff Kirsher 
1705f7917c00SJeff Kirsher 		rmb();
1706f7917c00SJeff Kirsher 		rsp_type = RSPD_TYPE(rc->type_gen);
1707f7917c00SJeff Kirsher 		if (likely(rsp_type == RSP_TYPE_FLBUF)) {
1708e91b0f24SIan Campbell 			struct page_frag *fp;
1709f7917c00SJeff Kirsher 			struct pkt_gl si;
1710f7917c00SJeff Kirsher 			const struct rx_sw_desc *rsd;
1711f7917c00SJeff Kirsher 			u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
1712f7917c00SJeff Kirsher 
1713f7917c00SJeff Kirsher 			if (len & RSPD_NEWBUF) {
1714f7917c00SJeff Kirsher 				if (likely(q->offset > 0)) {
1715f7917c00SJeff Kirsher 					free_rx_bufs(q->adap, &rxq->fl, 1);
1716f7917c00SJeff Kirsher 					q->offset = 0;
1717f7917c00SJeff Kirsher 				}
1718f7917c00SJeff Kirsher 				len = RSPD_LEN(len);
1719f7917c00SJeff Kirsher 			}
1720f7917c00SJeff Kirsher 			si.tot_len = len;
1721f7917c00SJeff Kirsher 
1722f7917c00SJeff Kirsher 			/* gather packet fragments */
1723f7917c00SJeff Kirsher 			for (frags = 0, fp = si.frags; ; frags++, fp++) {
1724f7917c00SJeff Kirsher 				rsd = &rxq->fl.sdesc[rxq->fl.cidx];
1725f7917c00SJeff Kirsher 				bufsz = get_buf_size(rsd);
1726f7917c00SJeff Kirsher 				fp->page = rsd->page;
1727e91b0f24SIan Campbell 				fp->offset = q->offset;
1728e91b0f24SIan Campbell 				fp->size = min(bufsz, len);
1729e91b0f24SIan Campbell 				len -= fp->size;
1730f7917c00SJeff Kirsher 				if (!len)
1731f7917c00SJeff Kirsher 					break;
1732f7917c00SJeff Kirsher 				unmap_rx_buf(q->adap, &rxq->fl);
1733f7917c00SJeff Kirsher 			}
1734f7917c00SJeff Kirsher 
1735f7917c00SJeff Kirsher 			/*
1736f7917c00SJeff Kirsher 			 * Last buffer remains mapped so explicitly make it
1737f7917c00SJeff Kirsher 			 * coherent for CPU access.
1738f7917c00SJeff Kirsher 			 */
1739f7917c00SJeff Kirsher 			dma_sync_single_for_cpu(q->adap->pdev_dev,
1740f7917c00SJeff Kirsher 						get_buf_addr(rsd),
1741e91b0f24SIan Campbell 						fp->size, DMA_FROM_DEVICE);
1742f7917c00SJeff Kirsher 
1743f7917c00SJeff Kirsher 			si.va = page_address(si.frags[0].page) +
1744e91b0f24SIan Campbell 				si.frags[0].offset;
1745f7917c00SJeff Kirsher 			prefetch(si.va);
1746f7917c00SJeff Kirsher 
1747f7917c00SJeff Kirsher 			si.nfrags = frags + 1;
1748f7917c00SJeff Kirsher 			ret = q->handler(q, q->cur_desc, &si);
1749f7917c00SJeff Kirsher 			if (likely(ret == 0))
1750e91b0f24SIan Campbell 				q->offset += ALIGN(fp->size, FL_ALIGN);
1751f7917c00SJeff Kirsher 			else
1752f7917c00SJeff Kirsher 				restore_rx_bufs(&si, &rxq->fl, frags);
1753f7917c00SJeff Kirsher 		} else if (likely(rsp_type == RSP_TYPE_CPL)) {
1754f7917c00SJeff Kirsher 			ret = q->handler(q, q->cur_desc, NULL);
1755f7917c00SJeff Kirsher 		} else {
1756f7917c00SJeff Kirsher 			ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
1757f7917c00SJeff Kirsher 		}
1758f7917c00SJeff Kirsher 
1759f7917c00SJeff Kirsher 		if (unlikely(ret)) {
1760f7917c00SJeff Kirsher 			/* couldn't process descriptor, back off for recovery */
1761f7917c00SJeff Kirsher 			q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
1762f7917c00SJeff Kirsher 			break;
1763f7917c00SJeff Kirsher 		}
1764f7917c00SJeff Kirsher 
1765f7917c00SJeff Kirsher 		rspq_next(q);
1766f7917c00SJeff Kirsher 		budget_left--;
1767f7917c00SJeff Kirsher 	}
1768f7917c00SJeff Kirsher 
1769f7917c00SJeff Kirsher 	if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
1770f7917c00SJeff Kirsher 		__refill_fl(q->adap, &rxq->fl);
1771f7917c00SJeff Kirsher 	return budget - budget_left;
1772f7917c00SJeff Kirsher }
1773f7917c00SJeff Kirsher 
1774f7917c00SJeff Kirsher /**
1775f7917c00SJeff Kirsher  *	napi_rx_handler - the NAPI handler for Rx processing
1776f7917c00SJeff Kirsher  *	@napi: the napi instance
1777f7917c00SJeff Kirsher  *	@budget: how many packets we can process in this round
1778f7917c00SJeff Kirsher  *
1779f7917c00SJeff Kirsher  *	Handler for new data events when using NAPI.  This does not need any
1780f7917c00SJeff Kirsher  *	locking or protection from interrupts as data interrupts are off at
1781f7917c00SJeff Kirsher  *	this point and other adapter interrupts do not interfere (the latter
1782f7917c00SJeff Kirsher  *	in not a concern at all with MSI-X as non-data interrupts then have
1783f7917c00SJeff Kirsher  *	a separate handler).
1784f7917c00SJeff Kirsher  */
1785f7917c00SJeff Kirsher static int napi_rx_handler(struct napi_struct *napi, int budget)
1786f7917c00SJeff Kirsher {
1787f7917c00SJeff Kirsher 	unsigned int params;
1788f7917c00SJeff Kirsher 	struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
1789f7917c00SJeff Kirsher 	int work_done = process_responses(q, budget);
1790f7917c00SJeff Kirsher 
1791f7917c00SJeff Kirsher 	if (likely(work_done < budget)) {
1792f7917c00SJeff Kirsher 		napi_complete(napi);
1793f7917c00SJeff Kirsher 		params = q->next_intr_params;
1794f7917c00SJeff Kirsher 		q->next_intr_params = q->intr_params;
1795f7917c00SJeff Kirsher 	} else
1796f7917c00SJeff Kirsher 		params = QINTR_TIMER_IDX(7);
1797f7917c00SJeff Kirsher 
1798f7917c00SJeff Kirsher 	t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS), CIDXINC(work_done) |
1799f7917c00SJeff Kirsher 		     INGRESSQID((u32)q->cntxt_id) | SEINTARM(params));
1800f7917c00SJeff Kirsher 	return work_done;
1801f7917c00SJeff Kirsher }
1802f7917c00SJeff Kirsher 
1803f7917c00SJeff Kirsher /*
1804f7917c00SJeff Kirsher  * The MSI-X interrupt handler for an SGE response queue.
1805f7917c00SJeff Kirsher  */
1806f7917c00SJeff Kirsher irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
1807f7917c00SJeff Kirsher {
1808f7917c00SJeff Kirsher 	struct sge_rspq *q = cookie;
1809f7917c00SJeff Kirsher 
1810f7917c00SJeff Kirsher 	napi_schedule(&q->napi);
1811f7917c00SJeff Kirsher 	return IRQ_HANDLED;
1812f7917c00SJeff Kirsher }
1813f7917c00SJeff Kirsher 
1814f7917c00SJeff Kirsher /*
1815f7917c00SJeff Kirsher  * Process the indirect interrupt entries in the interrupt queue and kick off
1816f7917c00SJeff Kirsher  * NAPI for each queue that has generated an entry.
1817f7917c00SJeff Kirsher  */
1818f7917c00SJeff Kirsher static unsigned int process_intrq(struct adapter *adap)
1819f7917c00SJeff Kirsher {
1820f7917c00SJeff Kirsher 	unsigned int credits;
1821f7917c00SJeff Kirsher 	const struct rsp_ctrl *rc;
1822f7917c00SJeff Kirsher 	struct sge_rspq *q = &adap->sge.intrq;
1823f7917c00SJeff Kirsher 
1824f7917c00SJeff Kirsher 	spin_lock(&adap->sge.intrq_lock);
1825f7917c00SJeff Kirsher 	for (credits = 0; ; credits++) {
1826f7917c00SJeff Kirsher 		rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
1827f7917c00SJeff Kirsher 		if (!is_new_response(rc, q))
1828f7917c00SJeff Kirsher 			break;
1829f7917c00SJeff Kirsher 
1830f7917c00SJeff Kirsher 		rmb();
1831f7917c00SJeff Kirsher 		if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
1832f7917c00SJeff Kirsher 			unsigned int qid = ntohl(rc->pldbuflen_qid);
1833f7917c00SJeff Kirsher 
1834f7917c00SJeff Kirsher 			qid -= adap->sge.ingr_start;
1835f7917c00SJeff Kirsher 			napi_schedule(&adap->sge.ingr_map[qid]->napi);
1836f7917c00SJeff Kirsher 		}
1837f7917c00SJeff Kirsher 
1838f7917c00SJeff Kirsher 		rspq_next(q);
1839f7917c00SJeff Kirsher 	}
1840f7917c00SJeff Kirsher 
1841f7917c00SJeff Kirsher 	t4_write_reg(adap, MYPF_REG(SGE_PF_GTS), CIDXINC(credits) |
1842f7917c00SJeff Kirsher 		     INGRESSQID(q->cntxt_id) | SEINTARM(q->intr_params));
1843f7917c00SJeff Kirsher 	spin_unlock(&adap->sge.intrq_lock);
1844f7917c00SJeff Kirsher 	return credits;
1845f7917c00SJeff Kirsher }
1846f7917c00SJeff Kirsher 
1847f7917c00SJeff Kirsher /*
1848f7917c00SJeff Kirsher  * The MSI interrupt handler, which handles data events from SGE response queues
1849f7917c00SJeff Kirsher  * as well as error and other async events as they all use the same MSI vector.
1850f7917c00SJeff Kirsher  */
1851f7917c00SJeff Kirsher static irqreturn_t t4_intr_msi(int irq, void *cookie)
1852f7917c00SJeff Kirsher {
1853f7917c00SJeff Kirsher 	struct adapter *adap = cookie;
1854f7917c00SJeff Kirsher 
1855f7917c00SJeff Kirsher 	t4_slow_intr_handler(adap);
1856f7917c00SJeff Kirsher 	process_intrq(adap);
1857f7917c00SJeff Kirsher 	return IRQ_HANDLED;
1858f7917c00SJeff Kirsher }
1859f7917c00SJeff Kirsher 
1860f7917c00SJeff Kirsher /*
1861f7917c00SJeff Kirsher  * Interrupt handler for legacy INTx interrupts.
1862f7917c00SJeff Kirsher  * Handles data events from SGE response queues as well as error and other
1863f7917c00SJeff Kirsher  * async events as they all use the same interrupt line.
1864f7917c00SJeff Kirsher  */
1865f7917c00SJeff Kirsher static irqreturn_t t4_intr_intx(int irq, void *cookie)
1866f7917c00SJeff Kirsher {
1867f7917c00SJeff Kirsher 	struct adapter *adap = cookie;
1868f7917c00SJeff Kirsher 
1869f7917c00SJeff Kirsher 	t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI), 0);
1870f7917c00SJeff Kirsher 	if (t4_slow_intr_handler(adap) | process_intrq(adap))
1871f7917c00SJeff Kirsher 		return IRQ_HANDLED;
1872f7917c00SJeff Kirsher 	return IRQ_NONE;             /* probably shared interrupt */
1873f7917c00SJeff Kirsher }
1874f7917c00SJeff Kirsher 
1875f7917c00SJeff Kirsher /**
1876f7917c00SJeff Kirsher  *	t4_intr_handler - select the top-level interrupt handler
1877f7917c00SJeff Kirsher  *	@adap: the adapter
1878f7917c00SJeff Kirsher  *
1879f7917c00SJeff Kirsher  *	Selects the top-level interrupt handler based on the type of interrupts
1880f7917c00SJeff Kirsher  *	(MSI-X, MSI, or INTx).
1881f7917c00SJeff Kirsher  */
1882f7917c00SJeff Kirsher irq_handler_t t4_intr_handler(struct adapter *adap)
1883f7917c00SJeff Kirsher {
1884f7917c00SJeff Kirsher 	if (adap->flags & USING_MSIX)
1885f7917c00SJeff Kirsher 		return t4_sge_intr_msix;
1886f7917c00SJeff Kirsher 	if (adap->flags & USING_MSI)
1887f7917c00SJeff Kirsher 		return t4_intr_msi;
1888f7917c00SJeff Kirsher 	return t4_intr_intx;
1889f7917c00SJeff Kirsher }
1890f7917c00SJeff Kirsher 
1891f7917c00SJeff Kirsher static void sge_rx_timer_cb(unsigned long data)
1892f7917c00SJeff Kirsher {
1893f7917c00SJeff Kirsher 	unsigned long m;
1894f7917c00SJeff Kirsher 	unsigned int i, cnt[2];
1895f7917c00SJeff Kirsher 	struct adapter *adap = (struct adapter *)data;
1896f7917c00SJeff Kirsher 	struct sge *s = &adap->sge;
1897f7917c00SJeff Kirsher 
1898f7917c00SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++)
1899f7917c00SJeff Kirsher 		for (m = s->starving_fl[i]; m; m &= m - 1) {
1900f7917c00SJeff Kirsher 			struct sge_eth_rxq *rxq;
1901f7917c00SJeff Kirsher 			unsigned int id = __ffs(m) + i * BITS_PER_LONG;
1902f7917c00SJeff Kirsher 			struct sge_fl *fl = s->egr_map[id];
1903f7917c00SJeff Kirsher 
1904f7917c00SJeff Kirsher 			clear_bit(id, s->starving_fl);
1905f7917c00SJeff Kirsher 			smp_mb__after_clear_bit();
1906f7917c00SJeff Kirsher 
1907f7917c00SJeff Kirsher 			if (fl_starving(fl)) {
1908f7917c00SJeff Kirsher 				rxq = container_of(fl, struct sge_eth_rxq, fl);
1909f7917c00SJeff Kirsher 				if (napi_reschedule(&rxq->rspq.napi))
1910f7917c00SJeff Kirsher 					fl->starving++;
1911f7917c00SJeff Kirsher 				else
1912f7917c00SJeff Kirsher 					set_bit(id, s->starving_fl);
1913f7917c00SJeff Kirsher 			}
1914f7917c00SJeff Kirsher 		}
1915f7917c00SJeff Kirsher 
1916f7917c00SJeff Kirsher 	t4_write_reg(adap, SGE_DEBUG_INDEX, 13);
1917f7917c00SJeff Kirsher 	cnt[0] = t4_read_reg(adap, SGE_DEBUG_DATA_HIGH);
1918f7917c00SJeff Kirsher 	cnt[1] = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
1919f7917c00SJeff Kirsher 
1920f7917c00SJeff Kirsher 	for (i = 0; i < 2; i++)
1921f7917c00SJeff Kirsher 		if (cnt[i] >= s->starve_thres) {
1922f7917c00SJeff Kirsher 			if (s->idma_state[i] || cnt[i] == 0xffffffff)
1923f7917c00SJeff Kirsher 				continue;
1924f7917c00SJeff Kirsher 			s->idma_state[i] = 1;
1925f7917c00SJeff Kirsher 			t4_write_reg(adap, SGE_DEBUG_INDEX, 11);
1926f7917c00SJeff Kirsher 			m = t4_read_reg(adap, SGE_DEBUG_DATA_LOW) >> (i * 16);
1927f7917c00SJeff Kirsher 			dev_warn(adap->pdev_dev,
1928f7917c00SJeff Kirsher 				 "SGE idma%u starvation detected for "
1929f7917c00SJeff Kirsher 				 "queue %lu\n", i, m & 0xffff);
1930f7917c00SJeff Kirsher 		} else if (s->idma_state[i])
1931f7917c00SJeff Kirsher 			s->idma_state[i] = 0;
1932f7917c00SJeff Kirsher 
1933f7917c00SJeff Kirsher 	mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
1934f7917c00SJeff Kirsher }
1935f7917c00SJeff Kirsher 
1936f7917c00SJeff Kirsher static void sge_tx_timer_cb(unsigned long data)
1937f7917c00SJeff Kirsher {
1938f7917c00SJeff Kirsher 	unsigned long m;
1939f7917c00SJeff Kirsher 	unsigned int i, budget;
1940f7917c00SJeff Kirsher 	struct adapter *adap = (struct adapter *)data;
1941f7917c00SJeff Kirsher 	struct sge *s = &adap->sge;
1942f7917c00SJeff Kirsher 
1943f7917c00SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(s->txq_maperr); i++)
1944f7917c00SJeff Kirsher 		for (m = s->txq_maperr[i]; m; m &= m - 1) {
1945f7917c00SJeff Kirsher 			unsigned long id = __ffs(m) + i * BITS_PER_LONG;
1946f7917c00SJeff Kirsher 			struct sge_ofld_txq *txq = s->egr_map[id];
1947f7917c00SJeff Kirsher 
1948f7917c00SJeff Kirsher 			clear_bit(id, s->txq_maperr);
1949f7917c00SJeff Kirsher 			tasklet_schedule(&txq->qresume_tsk);
1950f7917c00SJeff Kirsher 		}
1951f7917c00SJeff Kirsher 
1952f7917c00SJeff Kirsher 	budget = MAX_TIMER_TX_RECLAIM;
1953f7917c00SJeff Kirsher 	i = s->ethtxq_rover;
1954f7917c00SJeff Kirsher 	do {
1955f7917c00SJeff Kirsher 		struct sge_eth_txq *q = &s->ethtxq[i];
1956f7917c00SJeff Kirsher 
1957f7917c00SJeff Kirsher 		if (q->q.in_use &&
1958f7917c00SJeff Kirsher 		    time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
1959f7917c00SJeff Kirsher 		    __netif_tx_trylock(q->txq)) {
1960f7917c00SJeff Kirsher 			int avail = reclaimable(&q->q);
1961f7917c00SJeff Kirsher 
1962f7917c00SJeff Kirsher 			if (avail) {
1963f7917c00SJeff Kirsher 				if (avail > budget)
1964f7917c00SJeff Kirsher 					avail = budget;
1965f7917c00SJeff Kirsher 
1966f7917c00SJeff Kirsher 				free_tx_desc(adap, &q->q, avail, true);
1967f7917c00SJeff Kirsher 				q->q.in_use -= avail;
1968f7917c00SJeff Kirsher 				budget -= avail;
1969f7917c00SJeff Kirsher 			}
1970f7917c00SJeff Kirsher 			__netif_tx_unlock(q->txq);
1971f7917c00SJeff Kirsher 		}
1972f7917c00SJeff Kirsher 
1973f7917c00SJeff Kirsher 		if (++i >= s->ethqsets)
1974f7917c00SJeff Kirsher 			i = 0;
1975f7917c00SJeff Kirsher 	} while (budget && i != s->ethtxq_rover);
1976f7917c00SJeff Kirsher 	s->ethtxq_rover = i;
1977f7917c00SJeff Kirsher 	mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
1978f7917c00SJeff Kirsher }
1979f7917c00SJeff Kirsher 
1980f7917c00SJeff Kirsher int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
1981f7917c00SJeff Kirsher 		     struct net_device *dev, int intr_idx,
1982f7917c00SJeff Kirsher 		     struct sge_fl *fl, rspq_handler_t hnd)
1983f7917c00SJeff Kirsher {
1984f7917c00SJeff Kirsher 	int ret, flsz = 0;
1985f7917c00SJeff Kirsher 	struct fw_iq_cmd c;
1986f7917c00SJeff Kirsher 	struct port_info *pi = netdev_priv(dev);
1987f7917c00SJeff Kirsher 
1988f7917c00SJeff Kirsher 	/* Size needs to be multiple of 16, including status entry. */
1989f7917c00SJeff Kirsher 	iq->size = roundup(iq->size, 16);
1990f7917c00SJeff Kirsher 
1991f7917c00SJeff Kirsher 	iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
1992f7917c00SJeff Kirsher 			      &iq->phys_addr, NULL, 0, NUMA_NO_NODE);
1993f7917c00SJeff Kirsher 	if (!iq->desc)
1994f7917c00SJeff Kirsher 		return -ENOMEM;
1995f7917c00SJeff Kirsher 
1996f7917c00SJeff Kirsher 	memset(&c, 0, sizeof(c));
1997f7917c00SJeff Kirsher 	c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
1998f7917c00SJeff Kirsher 			    FW_CMD_WRITE | FW_CMD_EXEC |
1999f7917c00SJeff Kirsher 			    FW_IQ_CMD_PFN(adap->fn) | FW_IQ_CMD_VFN(0));
2000f7917c00SJeff Kirsher 	c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC | FW_IQ_CMD_IQSTART(1) |
2001f7917c00SJeff Kirsher 				 FW_LEN16(c));
2002f7917c00SJeff Kirsher 	c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
2003f7917c00SJeff Kirsher 		FW_IQ_CMD_IQASYNCH(fwevtq) | FW_IQ_CMD_VIID(pi->viid) |
2004f7917c00SJeff Kirsher 		FW_IQ_CMD_IQANDST(intr_idx < 0) | FW_IQ_CMD_IQANUD(1) |
2005f7917c00SJeff Kirsher 		FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
2006f7917c00SJeff Kirsher 							-intr_idx - 1));
2007f7917c00SJeff Kirsher 	c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
2008f7917c00SJeff Kirsher 		FW_IQ_CMD_IQGTSMODE |
2009f7917c00SJeff Kirsher 		FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
2010f7917c00SJeff Kirsher 		FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
2011f7917c00SJeff Kirsher 	c.iqsize = htons(iq->size);
2012f7917c00SJeff Kirsher 	c.iqaddr = cpu_to_be64(iq->phys_addr);
2013f7917c00SJeff Kirsher 
2014f7917c00SJeff Kirsher 	if (fl) {
2015f7917c00SJeff Kirsher 		fl->size = roundup(fl->size, 8);
2016f7917c00SJeff Kirsher 		fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
2017f7917c00SJeff Kirsher 				      sizeof(struct rx_sw_desc), &fl->addr,
2018f7917c00SJeff Kirsher 				      &fl->sdesc, STAT_LEN, NUMA_NO_NODE);
2019f7917c00SJeff Kirsher 		if (!fl->desc)
2020f7917c00SJeff Kirsher 			goto fl_nomem;
2021f7917c00SJeff Kirsher 
2022f7917c00SJeff Kirsher 		flsz = fl->size / 8 + STAT_LEN / sizeof(struct tx_desc);
2023f7917c00SJeff Kirsher 		c.iqns_to_fl0congen = htonl(FW_IQ_CMD_FL0PACKEN |
2024f7917c00SJeff Kirsher 					    FW_IQ_CMD_FL0FETCHRO(1) |
2025f7917c00SJeff Kirsher 					    FW_IQ_CMD_FL0DATARO(1) |
2026f7917c00SJeff Kirsher 					    FW_IQ_CMD_FL0PADEN);
2027f7917c00SJeff Kirsher 		c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN(2) |
2028f7917c00SJeff Kirsher 				FW_IQ_CMD_FL0FBMAX(3));
2029f7917c00SJeff Kirsher 		c.fl0size = htons(flsz);
2030f7917c00SJeff Kirsher 		c.fl0addr = cpu_to_be64(fl->addr);
2031f7917c00SJeff Kirsher 	}
2032f7917c00SJeff Kirsher 
2033f7917c00SJeff Kirsher 	ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2034f7917c00SJeff Kirsher 	if (ret)
2035f7917c00SJeff Kirsher 		goto err;
2036f7917c00SJeff Kirsher 
2037f7917c00SJeff Kirsher 	netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
2038f7917c00SJeff Kirsher 	iq->cur_desc = iq->desc;
2039f7917c00SJeff Kirsher 	iq->cidx = 0;
2040f7917c00SJeff Kirsher 	iq->gen = 1;
2041f7917c00SJeff Kirsher 	iq->next_intr_params = iq->intr_params;
2042f7917c00SJeff Kirsher 	iq->cntxt_id = ntohs(c.iqid);
2043f7917c00SJeff Kirsher 	iq->abs_id = ntohs(c.physiqid);
2044f7917c00SJeff Kirsher 	iq->size--;                           /* subtract status entry */
2045f7917c00SJeff Kirsher 	iq->adap = adap;
2046f7917c00SJeff Kirsher 	iq->netdev = dev;
2047f7917c00SJeff Kirsher 	iq->handler = hnd;
2048f7917c00SJeff Kirsher 
2049f7917c00SJeff Kirsher 	/* set offset to -1 to distinguish ingress queues without FL */
2050f7917c00SJeff Kirsher 	iq->offset = fl ? 0 : -1;
2051f7917c00SJeff Kirsher 
2052f7917c00SJeff Kirsher 	adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
2053f7917c00SJeff Kirsher 
2054f7917c00SJeff Kirsher 	if (fl) {
2055f7917c00SJeff Kirsher 		fl->cntxt_id = ntohs(c.fl0id);
2056f7917c00SJeff Kirsher 		fl->avail = fl->pend_cred = 0;
2057f7917c00SJeff Kirsher 		fl->pidx = fl->cidx = 0;
2058f7917c00SJeff Kirsher 		fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
2059f7917c00SJeff Kirsher 		adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
2060f7917c00SJeff Kirsher 		refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
2061f7917c00SJeff Kirsher 	}
2062f7917c00SJeff Kirsher 	return 0;
2063f7917c00SJeff Kirsher 
2064f7917c00SJeff Kirsher fl_nomem:
2065f7917c00SJeff Kirsher 	ret = -ENOMEM;
2066f7917c00SJeff Kirsher err:
2067f7917c00SJeff Kirsher 	if (iq->desc) {
2068f7917c00SJeff Kirsher 		dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
2069f7917c00SJeff Kirsher 				  iq->desc, iq->phys_addr);
2070f7917c00SJeff Kirsher 		iq->desc = NULL;
2071f7917c00SJeff Kirsher 	}
2072f7917c00SJeff Kirsher 	if (fl && fl->desc) {
2073f7917c00SJeff Kirsher 		kfree(fl->sdesc);
2074f7917c00SJeff Kirsher 		fl->sdesc = NULL;
2075f7917c00SJeff Kirsher 		dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
2076f7917c00SJeff Kirsher 				  fl->desc, fl->addr);
2077f7917c00SJeff Kirsher 		fl->desc = NULL;
2078f7917c00SJeff Kirsher 	}
2079f7917c00SJeff Kirsher 	return ret;
2080f7917c00SJeff Kirsher }
2081f7917c00SJeff Kirsher 
2082f7917c00SJeff Kirsher static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
2083f7917c00SJeff Kirsher {
2084f7917c00SJeff Kirsher 	q->in_use = 0;
2085f7917c00SJeff Kirsher 	q->cidx = q->pidx = 0;
2086f7917c00SJeff Kirsher 	q->stops = q->restarts = 0;
2087f7917c00SJeff Kirsher 	q->stat = (void *)&q->desc[q->size];
2088f7917c00SJeff Kirsher 	q->cntxt_id = id;
20893069ee9bSVipul Pandya 	spin_lock_init(&q->db_lock);
2090f7917c00SJeff Kirsher 	adap->sge.egr_map[id - adap->sge.egr_start] = q;
2091f7917c00SJeff Kirsher }
2092f7917c00SJeff Kirsher 
2093f7917c00SJeff Kirsher int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
2094f7917c00SJeff Kirsher 			 struct net_device *dev, struct netdev_queue *netdevq,
2095f7917c00SJeff Kirsher 			 unsigned int iqid)
2096f7917c00SJeff Kirsher {
2097f7917c00SJeff Kirsher 	int ret, nentries;
2098f7917c00SJeff Kirsher 	struct fw_eq_eth_cmd c;
2099f7917c00SJeff Kirsher 	struct port_info *pi = netdev_priv(dev);
2100f7917c00SJeff Kirsher 
2101f7917c00SJeff Kirsher 	/* Add status entries */
2102f7917c00SJeff Kirsher 	nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
2103f7917c00SJeff Kirsher 
2104f7917c00SJeff Kirsher 	txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2105f7917c00SJeff Kirsher 			sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
2106f7917c00SJeff Kirsher 			&txq->q.phys_addr, &txq->q.sdesc, STAT_LEN,
2107f7917c00SJeff Kirsher 			netdev_queue_numa_node_read(netdevq));
2108f7917c00SJeff Kirsher 	if (!txq->q.desc)
2109f7917c00SJeff Kirsher 		return -ENOMEM;
2110f7917c00SJeff Kirsher 
2111f7917c00SJeff Kirsher 	memset(&c, 0, sizeof(c));
2112f7917c00SJeff Kirsher 	c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
2113f7917c00SJeff Kirsher 			    FW_CMD_WRITE | FW_CMD_EXEC |
2114f7917c00SJeff Kirsher 			    FW_EQ_ETH_CMD_PFN(adap->fn) | FW_EQ_ETH_CMD_VFN(0));
2115f7917c00SJeff Kirsher 	c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC |
2116f7917c00SJeff Kirsher 				 FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
2117f7917c00SJeff Kirsher 	c.viid_pkd = htonl(FW_EQ_ETH_CMD_VIID(pi->viid));
2118f7917c00SJeff Kirsher 	c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE(2) |
2119f7917c00SJeff Kirsher 				   FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
2120f7917c00SJeff Kirsher 				   FW_EQ_ETH_CMD_FETCHRO(1) |
2121f7917c00SJeff Kirsher 				   FW_EQ_ETH_CMD_IQID(iqid));
2122f7917c00SJeff Kirsher 	c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN(2) |
2123f7917c00SJeff Kirsher 				  FW_EQ_ETH_CMD_FBMAX(3) |
2124f7917c00SJeff Kirsher 				  FW_EQ_ETH_CMD_CIDXFTHRESH(5) |
2125f7917c00SJeff Kirsher 				  FW_EQ_ETH_CMD_EQSIZE(nentries));
2126f7917c00SJeff Kirsher 	c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2127f7917c00SJeff Kirsher 
2128f7917c00SJeff Kirsher 	ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2129f7917c00SJeff Kirsher 	if (ret) {
2130f7917c00SJeff Kirsher 		kfree(txq->q.sdesc);
2131f7917c00SJeff Kirsher 		txq->q.sdesc = NULL;
2132f7917c00SJeff Kirsher 		dma_free_coherent(adap->pdev_dev,
2133f7917c00SJeff Kirsher 				  nentries * sizeof(struct tx_desc),
2134f7917c00SJeff Kirsher 				  txq->q.desc, txq->q.phys_addr);
2135f7917c00SJeff Kirsher 		txq->q.desc = NULL;
2136f7917c00SJeff Kirsher 		return ret;
2137f7917c00SJeff Kirsher 	}
2138f7917c00SJeff Kirsher 
2139f7917c00SJeff Kirsher 	init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_GET(ntohl(c.eqid_pkd)));
2140f7917c00SJeff Kirsher 	txq->txq = netdevq;
2141f7917c00SJeff Kirsher 	txq->tso = txq->tx_cso = txq->vlan_ins = 0;
2142f7917c00SJeff Kirsher 	txq->mapping_err = 0;
2143f7917c00SJeff Kirsher 	return 0;
2144f7917c00SJeff Kirsher }
2145f7917c00SJeff Kirsher 
2146f7917c00SJeff Kirsher int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
2147f7917c00SJeff Kirsher 			  struct net_device *dev, unsigned int iqid,
2148f7917c00SJeff Kirsher 			  unsigned int cmplqid)
2149f7917c00SJeff Kirsher {
2150f7917c00SJeff Kirsher 	int ret, nentries;
2151f7917c00SJeff Kirsher 	struct fw_eq_ctrl_cmd c;
2152f7917c00SJeff Kirsher 	struct port_info *pi = netdev_priv(dev);
2153f7917c00SJeff Kirsher 
2154f7917c00SJeff Kirsher 	/* Add status entries */
2155f7917c00SJeff Kirsher 	nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
2156f7917c00SJeff Kirsher 
2157f7917c00SJeff Kirsher 	txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
2158f7917c00SJeff Kirsher 				 sizeof(struct tx_desc), 0, &txq->q.phys_addr,
2159f7917c00SJeff Kirsher 				 NULL, 0, NUMA_NO_NODE);
2160f7917c00SJeff Kirsher 	if (!txq->q.desc)
2161f7917c00SJeff Kirsher 		return -ENOMEM;
2162f7917c00SJeff Kirsher 
2163f7917c00SJeff Kirsher 	c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
2164f7917c00SJeff Kirsher 			    FW_CMD_WRITE | FW_CMD_EXEC |
2165f7917c00SJeff Kirsher 			    FW_EQ_CTRL_CMD_PFN(adap->fn) |
2166f7917c00SJeff Kirsher 			    FW_EQ_CTRL_CMD_VFN(0));
2167f7917c00SJeff Kirsher 	c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC |
2168f7917c00SJeff Kirsher 				 FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
2169f7917c00SJeff Kirsher 	c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID(cmplqid));
2170f7917c00SJeff Kirsher 	c.physeqid_pkd = htonl(0);
2171f7917c00SJeff Kirsher 	c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE(2) |
2172f7917c00SJeff Kirsher 				   FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
2173f7917c00SJeff Kirsher 				   FW_EQ_CTRL_CMD_FETCHRO |
2174f7917c00SJeff Kirsher 				   FW_EQ_CTRL_CMD_IQID(iqid));
2175f7917c00SJeff Kirsher 	c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN(2) |
2176f7917c00SJeff Kirsher 				  FW_EQ_CTRL_CMD_FBMAX(3) |
2177f7917c00SJeff Kirsher 				  FW_EQ_CTRL_CMD_CIDXFTHRESH(5) |
2178f7917c00SJeff Kirsher 				  FW_EQ_CTRL_CMD_EQSIZE(nentries));
2179f7917c00SJeff Kirsher 	c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2180f7917c00SJeff Kirsher 
2181f7917c00SJeff Kirsher 	ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2182f7917c00SJeff Kirsher 	if (ret) {
2183f7917c00SJeff Kirsher 		dma_free_coherent(adap->pdev_dev,
2184f7917c00SJeff Kirsher 				  nentries * sizeof(struct tx_desc),
2185f7917c00SJeff Kirsher 				  txq->q.desc, txq->q.phys_addr);
2186f7917c00SJeff Kirsher 		txq->q.desc = NULL;
2187f7917c00SJeff Kirsher 		return ret;
2188f7917c00SJeff Kirsher 	}
2189f7917c00SJeff Kirsher 
2190f7917c00SJeff Kirsher 	init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_GET(ntohl(c.cmpliqid_eqid)));
2191f7917c00SJeff Kirsher 	txq->adap = adap;
2192f7917c00SJeff Kirsher 	skb_queue_head_init(&txq->sendq);
2193f7917c00SJeff Kirsher 	tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
2194f7917c00SJeff Kirsher 	txq->full = 0;
2195f7917c00SJeff Kirsher 	return 0;
2196f7917c00SJeff Kirsher }
2197f7917c00SJeff Kirsher 
2198f7917c00SJeff Kirsher int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
2199f7917c00SJeff Kirsher 			  struct net_device *dev, unsigned int iqid)
2200f7917c00SJeff Kirsher {
2201f7917c00SJeff Kirsher 	int ret, nentries;
2202f7917c00SJeff Kirsher 	struct fw_eq_ofld_cmd c;
2203f7917c00SJeff Kirsher 	struct port_info *pi = netdev_priv(dev);
2204f7917c00SJeff Kirsher 
2205f7917c00SJeff Kirsher 	/* Add status entries */
2206f7917c00SJeff Kirsher 	nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
2207f7917c00SJeff Kirsher 
2208f7917c00SJeff Kirsher 	txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
2209f7917c00SJeff Kirsher 			sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
2210f7917c00SJeff Kirsher 			&txq->q.phys_addr, &txq->q.sdesc, STAT_LEN,
2211f7917c00SJeff Kirsher 			NUMA_NO_NODE);
2212f7917c00SJeff Kirsher 	if (!txq->q.desc)
2213f7917c00SJeff Kirsher 		return -ENOMEM;
2214f7917c00SJeff Kirsher 
2215f7917c00SJeff Kirsher 	memset(&c, 0, sizeof(c));
2216f7917c00SJeff Kirsher 	c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
2217f7917c00SJeff Kirsher 			    FW_CMD_WRITE | FW_CMD_EXEC |
2218f7917c00SJeff Kirsher 			    FW_EQ_OFLD_CMD_PFN(adap->fn) |
2219f7917c00SJeff Kirsher 			    FW_EQ_OFLD_CMD_VFN(0));
2220f7917c00SJeff Kirsher 	c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC |
2221f7917c00SJeff Kirsher 				 FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
2222f7917c00SJeff Kirsher 	c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE(2) |
2223f7917c00SJeff Kirsher 				   FW_EQ_OFLD_CMD_PCIECHN(pi->tx_chan) |
2224f7917c00SJeff Kirsher 				   FW_EQ_OFLD_CMD_FETCHRO(1) |
2225f7917c00SJeff Kirsher 				   FW_EQ_OFLD_CMD_IQID(iqid));
2226f7917c00SJeff Kirsher 	c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN(2) |
2227f7917c00SJeff Kirsher 				  FW_EQ_OFLD_CMD_FBMAX(3) |
2228f7917c00SJeff Kirsher 				  FW_EQ_OFLD_CMD_CIDXFTHRESH(5) |
2229f7917c00SJeff Kirsher 				  FW_EQ_OFLD_CMD_EQSIZE(nentries));
2230f7917c00SJeff Kirsher 	c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2231f7917c00SJeff Kirsher 
2232f7917c00SJeff Kirsher 	ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
2233f7917c00SJeff Kirsher 	if (ret) {
2234f7917c00SJeff Kirsher 		kfree(txq->q.sdesc);
2235f7917c00SJeff Kirsher 		txq->q.sdesc = NULL;
2236f7917c00SJeff Kirsher 		dma_free_coherent(adap->pdev_dev,
2237f7917c00SJeff Kirsher 				  nentries * sizeof(struct tx_desc),
2238f7917c00SJeff Kirsher 				  txq->q.desc, txq->q.phys_addr);
2239f7917c00SJeff Kirsher 		txq->q.desc = NULL;
2240f7917c00SJeff Kirsher 		return ret;
2241f7917c00SJeff Kirsher 	}
2242f7917c00SJeff Kirsher 
2243f7917c00SJeff Kirsher 	init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_GET(ntohl(c.eqid_pkd)));
2244f7917c00SJeff Kirsher 	txq->adap = adap;
2245f7917c00SJeff Kirsher 	skb_queue_head_init(&txq->sendq);
2246f7917c00SJeff Kirsher 	tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
2247f7917c00SJeff Kirsher 	txq->full = 0;
2248f7917c00SJeff Kirsher 	txq->mapping_err = 0;
2249f7917c00SJeff Kirsher 	return 0;
2250f7917c00SJeff Kirsher }
2251f7917c00SJeff Kirsher 
2252f7917c00SJeff Kirsher static void free_txq(struct adapter *adap, struct sge_txq *q)
2253f7917c00SJeff Kirsher {
2254f7917c00SJeff Kirsher 	dma_free_coherent(adap->pdev_dev,
2255f7917c00SJeff Kirsher 			  q->size * sizeof(struct tx_desc) + STAT_LEN,
2256f7917c00SJeff Kirsher 			  q->desc, q->phys_addr);
2257f7917c00SJeff Kirsher 	q->cntxt_id = 0;
2258f7917c00SJeff Kirsher 	q->sdesc = NULL;
2259f7917c00SJeff Kirsher 	q->desc = NULL;
2260f7917c00SJeff Kirsher }
2261f7917c00SJeff Kirsher 
2262f7917c00SJeff Kirsher static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
2263f7917c00SJeff Kirsher 			 struct sge_fl *fl)
2264f7917c00SJeff Kirsher {
2265f7917c00SJeff Kirsher 	unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
2266f7917c00SJeff Kirsher 
2267f7917c00SJeff Kirsher 	adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
2268f7917c00SJeff Kirsher 	t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
2269f7917c00SJeff Kirsher 		   rq->cntxt_id, fl_id, 0xffff);
2270f7917c00SJeff Kirsher 	dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
2271f7917c00SJeff Kirsher 			  rq->desc, rq->phys_addr);
2272f7917c00SJeff Kirsher 	netif_napi_del(&rq->napi);
2273f7917c00SJeff Kirsher 	rq->netdev = NULL;
2274f7917c00SJeff Kirsher 	rq->cntxt_id = rq->abs_id = 0;
2275f7917c00SJeff Kirsher 	rq->desc = NULL;
2276f7917c00SJeff Kirsher 
2277f7917c00SJeff Kirsher 	if (fl) {
2278f7917c00SJeff Kirsher 		free_rx_bufs(adap, fl, fl->avail);
2279f7917c00SJeff Kirsher 		dma_free_coherent(adap->pdev_dev, fl->size * 8 + STAT_LEN,
2280f7917c00SJeff Kirsher 				  fl->desc, fl->addr);
2281f7917c00SJeff Kirsher 		kfree(fl->sdesc);
2282f7917c00SJeff Kirsher 		fl->sdesc = NULL;
2283f7917c00SJeff Kirsher 		fl->cntxt_id = 0;
2284f7917c00SJeff Kirsher 		fl->desc = NULL;
2285f7917c00SJeff Kirsher 	}
2286f7917c00SJeff Kirsher }
2287f7917c00SJeff Kirsher 
2288f7917c00SJeff Kirsher /**
2289f7917c00SJeff Kirsher  *	t4_free_sge_resources - free SGE resources
2290f7917c00SJeff Kirsher  *	@adap: the adapter
2291f7917c00SJeff Kirsher  *
2292f7917c00SJeff Kirsher  *	Frees resources used by the SGE queue sets.
2293f7917c00SJeff Kirsher  */
2294f7917c00SJeff Kirsher void t4_free_sge_resources(struct adapter *adap)
2295f7917c00SJeff Kirsher {
2296f7917c00SJeff Kirsher 	int i;
2297f7917c00SJeff Kirsher 	struct sge_eth_rxq *eq = adap->sge.ethrxq;
2298f7917c00SJeff Kirsher 	struct sge_eth_txq *etq = adap->sge.ethtxq;
2299f7917c00SJeff Kirsher 	struct sge_ofld_rxq *oq = adap->sge.ofldrxq;
2300f7917c00SJeff Kirsher 
2301f7917c00SJeff Kirsher 	/* clean up Ethernet Tx/Rx queues */
2302f7917c00SJeff Kirsher 	for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
2303f7917c00SJeff Kirsher 		if (eq->rspq.desc)
2304f7917c00SJeff Kirsher 			free_rspq_fl(adap, &eq->rspq, &eq->fl);
2305f7917c00SJeff Kirsher 		if (etq->q.desc) {
2306f7917c00SJeff Kirsher 			t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
2307f7917c00SJeff Kirsher 				       etq->q.cntxt_id);
2308f7917c00SJeff Kirsher 			free_tx_desc(adap, &etq->q, etq->q.in_use, true);
2309f7917c00SJeff Kirsher 			kfree(etq->q.sdesc);
2310f7917c00SJeff Kirsher 			free_txq(adap, &etq->q);
2311f7917c00SJeff Kirsher 		}
2312f7917c00SJeff Kirsher 	}
2313f7917c00SJeff Kirsher 
2314f7917c00SJeff Kirsher 	/* clean up RDMA and iSCSI Rx queues */
2315f7917c00SJeff Kirsher 	for (i = 0; i < adap->sge.ofldqsets; i++, oq++) {
2316f7917c00SJeff Kirsher 		if (oq->rspq.desc)
2317f7917c00SJeff Kirsher 			free_rspq_fl(adap, &oq->rspq, &oq->fl);
2318f7917c00SJeff Kirsher 	}
2319f7917c00SJeff Kirsher 	for (i = 0, oq = adap->sge.rdmarxq; i < adap->sge.rdmaqs; i++, oq++) {
2320f7917c00SJeff Kirsher 		if (oq->rspq.desc)
2321f7917c00SJeff Kirsher 			free_rspq_fl(adap, &oq->rspq, &oq->fl);
2322f7917c00SJeff Kirsher 	}
2323f7917c00SJeff Kirsher 
2324f7917c00SJeff Kirsher 	/* clean up offload Tx queues */
2325f7917c00SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
2326f7917c00SJeff Kirsher 		struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
2327f7917c00SJeff Kirsher 
2328f7917c00SJeff Kirsher 		if (q->q.desc) {
2329f7917c00SJeff Kirsher 			tasklet_kill(&q->qresume_tsk);
2330f7917c00SJeff Kirsher 			t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
2331f7917c00SJeff Kirsher 					q->q.cntxt_id);
2332f7917c00SJeff Kirsher 			free_tx_desc(adap, &q->q, q->q.in_use, false);
2333f7917c00SJeff Kirsher 			kfree(q->q.sdesc);
2334f7917c00SJeff Kirsher 			__skb_queue_purge(&q->sendq);
2335f7917c00SJeff Kirsher 			free_txq(adap, &q->q);
2336f7917c00SJeff Kirsher 		}
2337f7917c00SJeff Kirsher 	}
2338f7917c00SJeff Kirsher 
2339f7917c00SJeff Kirsher 	/* clean up control Tx queues */
2340f7917c00SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
2341f7917c00SJeff Kirsher 		struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
2342f7917c00SJeff Kirsher 
2343f7917c00SJeff Kirsher 		if (cq->q.desc) {
2344f7917c00SJeff Kirsher 			tasklet_kill(&cq->qresume_tsk);
2345f7917c00SJeff Kirsher 			t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
2346f7917c00SJeff Kirsher 					cq->q.cntxt_id);
2347f7917c00SJeff Kirsher 			__skb_queue_purge(&cq->sendq);
2348f7917c00SJeff Kirsher 			free_txq(adap, &cq->q);
2349f7917c00SJeff Kirsher 		}
2350f7917c00SJeff Kirsher 	}
2351f7917c00SJeff Kirsher 
2352f7917c00SJeff Kirsher 	if (adap->sge.fw_evtq.desc)
2353f7917c00SJeff Kirsher 		free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
2354f7917c00SJeff Kirsher 
2355f7917c00SJeff Kirsher 	if (adap->sge.intrq.desc)
2356f7917c00SJeff Kirsher 		free_rspq_fl(adap, &adap->sge.intrq, NULL);
2357f7917c00SJeff Kirsher 
2358f7917c00SJeff Kirsher 	/* clear the reverse egress queue map */
2359f7917c00SJeff Kirsher 	memset(adap->sge.egr_map, 0, sizeof(adap->sge.egr_map));
2360f7917c00SJeff Kirsher }
2361f7917c00SJeff Kirsher 
2362f7917c00SJeff Kirsher void t4_sge_start(struct adapter *adap)
2363f7917c00SJeff Kirsher {
2364f7917c00SJeff Kirsher 	adap->sge.ethtxq_rover = 0;
2365f7917c00SJeff Kirsher 	mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2366f7917c00SJeff Kirsher 	mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2367f7917c00SJeff Kirsher }
2368f7917c00SJeff Kirsher 
2369f7917c00SJeff Kirsher /**
2370f7917c00SJeff Kirsher  *	t4_sge_stop - disable SGE operation
2371f7917c00SJeff Kirsher  *	@adap: the adapter
2372f7917c00SJeff Kirsher  *
2373f7917c00SJeff Kirsher  *	Stop tasklets and timers associated with the DMA engine.  Note that
2374f7917c00SJeff Kirsher  *	this is effective only if measures have been taken to disable any HW
2375f7917c00SJeff Kirsher  *	events that may restart them.
2376f7917c00SJeff Kirsher  */
2377f7917c00SJeff Kirsher void t4_sge_stop(struct adapter *adap)
2378f7917c00SJeff Kirsher {
2379f7917c00SJeff Kirsher 	int i;
2380f7917c00SJeff Kirsher 	struct sge *s = &adap->sge;
2381f7917c00SJeff Kirsher 
2382f7917c00SJeff Kirsher 	if (in_interrupt())  /* actions below require waiting */
2383f7917c00SJeff Kirsher 		return;
2384f7917c00SJeff Kirsher 
2385f7917c00SJeff Kirsher 	if (s->rx_timer.function)
2386f7917c00SJeff Kirsher 		del_timer_sync(&s->rx_timer);
2387f7917c00SJeff Kirsher 	if (s->tx_timer.function)
2388f7917c00SJeff Kirsher 		del_timer_sync(&s->tx_timer);
2389f7917c00SJeff Kirsher 
2390f7917c00SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
2391f7917c00SJeff Kirsher 		struct sge_ofld_txq *q = &s->ofldtxq[i];
2392f7917c00SJeff Kirsher 
2393f7917c00SJeff Kirsher 		if (q->q.desc)
2394f7917c00SJeff Kirsher 			tasklet_kill(&q->qresume_tsk);
2395f7917c00SJeff Kirsher 	}
2396f7917c00SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
2397f7917c00SJeff Kirsher 		struct sge_ctrl_txq *cq = &s->ctrlq[i];
2398f7917c00SJeff Kirsher 
2399f7917c00SJeff Kirsher 		if (cq->q.desc)
2400f7917c00SJeff Kirsher 			tasklet_kill(&cq->qresume_tsk);
2401f7917c00SJeff Kirsher 	}
2402f7917c00SJeff Kirsher }
2403f7917c00SJeff Kirsher 
2404f7917c00SJeff Kirsher /**
2405f7917c00SJeff Kirsher  *	t4_sge_init - initialize SGE
2406f7917c00SJeff Kirsher  *	@adap: the adapter
2407f7917c00SJeff Kirsher  *
2408f7917c00SJeff Kirsher  *	Performs SGE initialization needed every time after a chip reset.
2409f7917c00SJeff Kirsher  *	We do not initialize any of the queues here, instead the driver
2410f7917c00SJeff Kirsher  *	top-level must request them individually.
2411f7917c00SJeff Kirsher  */
2412f7917c00SJeff Kirsher void t4_sge_init(struct adapter *adap)
2413f7917c00SJeff Kirsher {
2414f7917c00SJeff Kirsher 	unsigned int i, v;
2415f7917c00SJeff Kirsher 	struct sge *s = &adap->sge;
2416f7917c00SJeff Kirsher 	unsigned int fl_align_log = ilog2(FL_ALIGN);
2417f7917c00SJeff Kirsher 
2418f7917c00SJeff Kirsher 	t4_set_reg_field(adap, SGE_CONTROL, PKTSHIFT_MASK |
2419f7917c00SJeff Kirsher 			 INGPADBOUNDARY_MASK | EGRSTATUSPAGESIZE,
2420f7917c00SJeff Kirsher 			 INGPADBOUNDARY(fl_align_log - 5) | PKTSHIFT(2) |
2421f7917c00SJeff Kirsher 			 RXPKTCPLMODE |
2422f7917c00SJeff Kirsher 			 (STAT_LEN == 128 ? EGRSTATUSPAGESIZE : 0));
2423f7917c00SJeff Kirsher 
24243069ee9bSVipul Pandya 	/*
24253069ee9bSVipul Pandya 	 * Set up to drop DOORBELL writes when the DOORBELL FIFO overflows
24263069ee9bSVipul Pandya 	 * and generate an interrupt when this occurs so we can recover.
24273069ee9bSVipul Pandya 	 */
2428881806bcSVipul Pandya 	t4_set_reg_field(adap, A_SGE_DBFIFO_STATUS,
24293069ee9bSVipul Pandya 			V_HP_INT_THRESH(M_HP_INT_THRESH) |
24303069ee9bSVipul Pandya 			V_LP_INT_THRESH(M_LP_INT_THRESH),
24313069ee9bSVipul Pandya 			V_HP_INT_THRESH(dbfifo_int_thresh) |
24323069ee9bSVipul Pandya 			V_LP_INT_THRESH(dbfifo_int_thresh));
2433881806bcSVipul Pandya 	t4_set_reg_field(adap, A_SGE_DOORBELL_CONTROL, F_ENABLE_DROP,
2434881806bcSVipul Pandya 			F_ENABLE_DROP);
2435881806bcSVipul Pandya 
2436f7917c00SJeff Kirsher 	for (i = v = 0; i < 32; i += 4)
2437f7917c00SJeff Kirsher 		v |= (PAGE_SHIFT - 10) << i;
2438f7917c00SJeff Kirsher 	t4_write_reg(adap, SGE_HOST_PAGE_SIZE, v);
2439f7917c00SJeff Kirsher 	t4_write_reg(adap, SGE_FL_BUFFER_SIZE0, PAGE_SIZE);
2440f7917c00SJeff Kirsher #if FL_PG_ORDER > 0
2441f7917c00SJeff Kirsher 	t4_write_reg(adap, SGE_FL_BUFFER_SIZE1, PAGE_SIZE << FL_PG_ORDER);
2442f7917c00SJeff Kirsher #endif
2443f7917c00SJeff Kirsher 	t4_write_reg(adap, SGE_INGRESS_RX_THRESHOLD,
2444f7917c00SJeff Kirsher 		     THRESHOLD_0(s->counter_val[0]) |
2445f7917c00SJeff Kirsher 		     THRESHOLD_1(s->counter_val[1]) |
2446f7917c00SJeff Kirsher 		     THRESHOLD_2(s->counter_val[2]) |
2447f7917c00SJeff Kirsher 		     THRESHOLD_3(s->counter_val[3]));
2448f7917c00SJeff Kirsher 	t4_write_reg(adap, SGE_TIMER_VALUE_0_AND_1,
2449f7917c00SJeff Kirsher 		     TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[0])) |
2450f7917c00SJeff Kirsher 		     TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[1])));
2451f7917c00SJeff Kirsher 	t4_write_reg(adap, SGE_TIMER_VALUE_2_AND_3,
2452f7917c00SJeff Kirsher 		     TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[2])) |
2453f7917c00SJeff Kirsher 		     TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[3])));
2454f7917c00SJeff Kirsher 	t4_write_reg(adap, SGE_TIMER_VALUE_4_AND_5,
2455f7917c00SJeff Kirsher 		     TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[4])) |
2456f7917c00SJeff Kirsher 		     TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[5])));
2457f7917c00SJeff Kirsher 	setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
2458f7917c00SJeff Kirsher 	setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
2459f7917c00SJeff Kirsher 	s->starve_thres = core_ticks_per_usec(adap) * 1000000;  /* 1 s */
2460f7917c00SJeff Kirsher 	s->idma_state[0] = s->idma_state[1] = 0;
2461f7917c00SJeff Kirsher 	spin_lock_init(&s->intrq_lock);
2462f7917c00SJeff Kirsher }
2463