xref: /linux/drivers/net/ethernet/chelsio/cxgb4vf/sge.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1c6e0d914SCasey Leedom /*
2c6e0d914SCasey Leedom  * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
3c6e0d914SCasey Leedom  * driver for Linux.
4c6e0d914SCasey Leedom  *
5c6e0d914SCasey Leedom  * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
6c6e0d914SCasey Leedom  *
7c6e0d914SCasey Leedom  * This software is available to you under a choice of one of two
8c6e0d914SCasey Leedom  * licenses.  You may choose to be licensed under the terms of the GNU
9c6e0d914SCasey Leedom  * General Public License (GPL) Version 2, available from the file
10c6e0d914SCasey Leedom  * COPYING in the main directory of this source tree, or the
11c6e0d914SCasey Leedom  * OpenIB.org BSD license below:
12c6e0d914SCasey Leedom  *
13c6e0d914SCasey Leedom  *     Redistribution and use in source and binary forms, with or
14c6e0d914SCasey Leedom  *     without modification, are permitted provided that the following
15c6e0d914SCasey Leedom  *     conditions are met:
16c6e0d914SCasey Leedom  *
17c6e0d914SCasey Leedom  *      - Redistributions of source code must retain the above
18c6e0d914SCasey Leedom  *        copyright notice, this list of conditions and the following
19c6e0d914SCasey Leedom  *        disclaimer.
20c6e0d914SCasey Leedom  *
21c6e0d914SCasey Leedom  *      - Redistributions in binary form must reproduce the above
22c6e0d914SCasey Leedom  *        copyright notice, this list of conditions and the following
23c6e0d914SCasey Leedom  *        disclaimer in the documentation and/or other materials
24c6e0d914SCasey Leedom  *        provided with the distribution.
25c6e0d914SCasey Leedom  *
26c6e0d914SCasey Leedom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27c6e0d914SCasey Leedom  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28c6e0d914SCasey Leedom  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29c6e0d914SCasey Leedom  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30c6e0d914SCasey Leedom  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31c6e0d914SCasey Leedom  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32c6e0d914SCasey Leedom  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33c6e0d914SCasey Leedom  * SOFTWARE.
34c6e0d914SCasey Leedom  */
35c6e0d914SCasey Leedom 
36c6e0d914SCasey Leedom #include <linux/skbuff.h>
37c6e0d914SCasey Leedom #include <linux/netdevice.h>
38c6e0d914SCasey Leedom #include <linux/etherdevice.h>
39c6e0d914SCasey Leedom #include <linux/if_vlan.h>
40c6e0d914SCasey Leedom #include <linux/ip.h>
41c6e0d914SCasey Leedom #include <net/ipv6.h>
42c6e0d914SCasey Leedom #include <net/tcp.h>
43c6e0d914SCasey Leedom #include <linux/dma-mapping.h>
4470c71606SPaul Gortmaker #include <linux/prefetch.h>
45c6e0d914SCasey Leedom 
46c6e0d914SCasey Leedom #include "t4vf_common.h"
47c6e0d914SCasey Leedom #include "t4vf_defs.h"
48c6e0d914SCasey Leedom 
49c6e0d914SCasey Leedom #include "../cxgb4/t4_regs.h"
50f612b815SHariprasad Shenai #include "../cxgb4/t4_values.h"
51c6e0d914SCasey Leedom #include "../cxgb4/t4fw_api.h"
52c6e0d914SCasey Leedom #include "../cxgb4/t4_msg.h"
53c6e0d914SCasey Leedom 
54c6e0d914SCasey Leedom /*
55c6e0d914SCasey Leedom  * Constants ...
56c6e0d914SCasey Leedom  */
57c6e0d914SCasey Leedom enum {
58c6e0d914SCasey Leedom 	/*
59c6e0d914SCasey Leedom 	 * Egress Queue sizes, producer and consumer indices are all in units
60c6e0d914SCasey Leedom 	 * of Egress Context Units bytes.  Note that as far as the hardware is
61c6e0d914SCasey Leedom 	 * concerned, the free list is an Egress Queue (the host produces free
62c6e0d914SCasey Leedom 	 * buffers which the hardware consumes) and free list entries are
63c6e0d914SCasey Leedom 	 * 64-bit PCI DMA addresses.
64c6e0d914SCasey Leedom 	 */
65c6e0d914SCasey Leedom 	EQ_UNIT = SGE_EQ_IDXSIZE,
66c6e0d914SCasey Leedom 	FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
67c6e0d914SCasey Leedom 	TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
68c6e0d914SCasey Leedom 
69c6e0d914SCasey Leedom 	/*
70c6e0d914SCasey Leedom 	 * Max number of TX descriptors we clean up at a time.  Should be
71c6e0d914SCasey Leedom 	 * modest as freeing skbs isn't cheap and it happens while holding
72c6e0d914SCasey Leedom 	 * locks.  We just need to free packets faster than they arrive, we
73c6e0d914SCasey Leedom 	 * eventually catch up and keep the amortized cost reasonable.
74c6e0d914SCasey Leedom 	 */
75c6e0d914SCasey Leedom 	MAX_TX_RECLAIM = 16,
76c6e0d914SCasey Leedom 
77c6e0d914SCasey Leedom 	/*
78c6e0d914SCasey Leedom 	 * Max number of Rx buffers we replenish at a time.  Again keep this
79c6e0d914SCasey Leedom 	 * modest, allocating buffers isn't cheap either.
80c6e0d914SCasey Leedom 	 */
81c6e0d914SCasey Leedom 	MAX_RX_REFILL = 16,
82c6e0d914SCasey Leedom 
83c6e0d914SCasey Leedom 	/*
84c6e0d914SCasey Leedom 	 * Period of the Rx queue check timer.  This timer is infrequent as it
85c6e0d914SCasey Leedom 	 * has something to do only when the system experiences severe memory
86c6e0d914SCasey Leedom 	 * shortage.
87c6e0d914SCasey Leedom 	 */
88c6e0d914SCasey Leedom 	RX_QCHECK_PERIOD = (HZ / 2),
89c6e0d914SCasey Leedom 
90c6e0d914SCasey Leedom 	/*
91c6e0d914SCasey Leedom 	 * Period of the TX queue check timer and the maximum number of TX
92c6e0d914SCasey Leedom 	 * descriptors to be reclaimed by the TX timer.
93c6e0d914SCasey Leedom 	 */
94c6e0d914SCasey Leedom 	TX_QCHECK_PERIOD = (HZ / 2),
95c6e0d914SCasey Leedom 	MAX_TIMER_TX_RECLAIM = 100,
96c6e0d914SCasey Leedom 
97c6e0d914SCasey Leedom 	/*
98c6e0d914SCasey Leedom 	 * Suspend an Ethernet TX queue with fewer available descriptors than
99c6e0d914SCasey Leedom 	 * this.  We always want to have room for a maximum sized packet:
100c6e0d914SCasey Leedom 	 * inline immediate data + MAX_SKB_FRAGS. This is the same as
101c6e0d914SCasey Leedom 	 * calc_tx_flits() for a TSO packet with nr_frags == MAX_SKB_FRAGS
102c6e0d914SCasey Leedom 	 * (see that function and its helpers for a description of the
103c6e0d914SCasey Leedom 	 * calculation).
104c6e0d914SCasey Leedom 	 */
105c6e0d914SCasey Leedom 	ETHTXQ_MAX_FRAGS = MAX_SKB_FRAGS + 1,
106c6e0d914SCasey Leedom 	ETHTXQ_MAX_SGL_LEN = ((3 * (ETHTXQ_MAX_FRAGS-1))/2 +
107c6e0d914SCasey Leedom 				   ((ETHTXQ_MAX_FRAGS-1) & 1) +
108c6e0d914SCasey Leedom 				   2),
109c6e0d914SCasey Leedom 	ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) +
110c6e0d914SCasey Leedom 			  sizeof(struct cpl_tx_pkt_lso_core) +
111c6e0d914SCasey Leedom 			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64),
112c6e0d914SCasey Leedom 	ETHTXQ_MAX_FLITS = ETHTXQ_MAX_SGL_LEN + ETHTXQ_MAX_HDR,
113c6e0d914SCasey Leedom 
114c6e0d914SCasey Leedom 	ETHTXQ_STOP_THRES = 1 + DIV_ROUND_UP(ETHTXQ_MAX_FLITS, TXD_PER_EQ_UNIT),
115c6e0d914SCasey Leedom 
116c6e0d914SCasey Leedom 	/*
117c6e0d914SCasey Leedom 	 * Max TX descriptor space we allow for an Ethernet packet to be
118c6e0d914SCasey Leedom 	 * inlined into a WR.  This is limited by the maximum value which
119c6e0d914SCasey Leedom 	 * we can specify for immediate data in the firmware Ethernet TX
120c6e0d914SCasey Leedom 	 * Work Request.
121c6e0d914SCasey Leedom 	 */
122e2ac9628SHariprasad Shenai 	MAX_IMM_TX_PKT_LEN = FW_WR_IMMDLEN_M,
123c6e0d914SCasey Leedom 
124c6e0d914SCasey Leedom 	/*
125c6e0d914SCasey Leedom 	 * Max size of a WR sent through a control TX queue.
126c6e0d914SCasey Leedom 	 */
127c6e0d914SCasey Leedom 	MAX_CTRL_WR_LEN = 256,
128c6e0d914SCasey Leedom 
129c6e0d914SCasey Leedom 	/*
130c6e0d914SCasey Leedom 	 * Maximum amount of data which we'll ever need to inline into a
131c6e0d914SCasey Leedom 	 * TX ring: max(MAX_IMM_TX_PKT_LEN, MAX_CTRL_WR_LEN).
132c6e0d914SCasey Leedom 	 */
133c6e0d914SCasey Leedom 	MAX_IMM_TX_LEN = (MAX_IMM_TX_PKT_LEN > MAX_CTRL_WR_LEN
134c6e0d914SCasey Leedom 			  ? MAX_IMM_TX_PKT_LEN
135c6e0d914SCasey Leedom 			  : MAX_CTRL_WR_LEN),
136c6e0d914SCasey Leedom 
137c6e0d914SCasey Leedom 	/*
138c6e0d914SCasey Leedom 	 * For incoming packets less than RX_COPY_THRES, we copy the data into
139c6e0d914SCasey Leedom 	 * an skb rather than referencing the data.  We allocate enough
140c6e0d914SCasey Leedom 	 * in-line room in skb's to accommodate pulling in RX_PULL_LEN bytes
141c6e0d914SCasey Leedom 	 * of the data (header).
142c6e0d914SCasey Leedom 	 */
143c6e0d914SCasey Leedom 	RX_COPY_THRES = 256,
144c6e0d914SCasey Leedom 	RX_PULL_LEN = 128,
145c6e0d914SCasey Leedom 
146c6e0d914SCasey Leedom 	/*
147eb6c503dSCasey Leedom 	 * Main body length for sk_buffs used for RX Ethernet packets with
148eb6c503dSCasey Leedom 	 * fragments.  Should be >= RX_PULL_LEN but possibly bigger to give
149eb6c503dSCasey Leedom 	 * pskb_may_pull() some room.
150c6e0d914SCasey Leedom 	 */
151eb6c503dSCasey Leedom 	RX_SKB_LEN = 512,
152eb6c503dSCasey Leedom };
153c6e0d914SCasey Leedom 
154c6e0d914SCasey Leedom /*
155c6e0d914SCasey Leedom  * Software state per TX descriptor.
156c6e0d914SCasey Leedom  */
157c6e0d914SCasey Leedom struct tx_sw_desc {
158c6e0d914SCasey Leedom 	struct sk_buff *skb;		/* socket buffer of TX data source */
159c6e0d914SCasey Leedom 	struct ulptx_sgl *sgl;		/* scatter/gather list in TX Queue */
160c6e0d914SCasey Leedom };
161c6e0d914SCasey Leedom 
162c6e0d914SCasey Leedom /*
163c6e0d914SCasey Leedom  * Software state per RX Free List descriptor.  We keep track of the allocated
164c6e0d914SCasey Leedom  * FL page, its size, and its PCI DMA address (if the page is mapped).  The FL
165c6e0d914SCasey Leedom  * page size and its PCI DMA mapped state are stored in the low bits of the
166c6e0d914SCasey Leedom  * PCI DMA address as per below.
167c6e0d914SCasey Leedom  */
168c6e0d914SCasey Leedom struct rx_sw_desc {
169c6e0d914SCasey Leedom 	struct page *page;		/* Free List page buffer */
170c6e0d914SCasey Leedom 	dma_addr_t dma_addr;		/* PCI DMA address (if mapped) */
171c6e0d914SCasey Leedom 					/*   and flags (see below) */
172c6e0d914SCasey Leedom };
173c6e0d914SCasey Leedom 
174c6e0d914SCasey Leedom /*
175c6e0d914SCasey Leedom  * The low bits of rx_sw_desc.dma_addr have special meaning.  Note that the
176c6e0d914SCasey Leedom  * SGE also uses the low 4 bits to determine the size of the buffer.  It uses
177c6e0d914SCasey Leedom  * those bits to index into the SGE_FL_BUFFER_SIZE[index] register array.
178c6e0d914SCasey Leedom  * Since we only use SGE_FL_BUFFER_SIZE0 and SGE_FL_BUFFER_SIZE1, these low 4
179c6e0d914SCasey Leedom  * bits can only contain a 0 or a 1 to indicate which size buffer we're giving
180c6e0d914SCasey Leedom  * to the SGE.  Thus, our software state of "is the buffer mapped for DMA" is
181c6e0d914SCasey Leedom  * maintained in an inverse sense so the hardware never sees that bit high.
182c6e0d914SCasey Leedom  */
183c6e0d914SCasey Leedom enum {
184c6e0d914SCasey Leedom 	RX_LARGE_BUF    = 1 << 0,	/* buffer is SGE_FL_BUFFER_SIZE[1] */
185c6e0d914SCasey Leedom 	RX_UNMAPPED_BUF = 1 << 1,	/* buffer is not mapped */
186c6e0d914SCasey Leedom };
187c6e0d914SCasey Leedom 
188c6e0d914SCasey Leedom /**
189c6e0d914SCasey Leedom  *	get_buf_addr - return DMA buffer address of software descriptor
190c6e0d914SCasey Leedom  *	@sdesc: pointer to the software buffer descriptor
191c6e0d914SCasey Leedom  *
192c6e0d914SCasey Leedom  *	Return the DMA buffer address of a software descriptor (stripping out
193c6e0d914SCasey Leedom  *	our low-order flag bits).
194c6e0d914SCasey Leedom  */
get_buf_addr(const struct rx_sw_desc * sdesc)195c6e0d914SCasey Leedom static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *sdesc)
196c6e0d914SCasey Leedom {
197c6e0d914SCasey Leedom 	return sdesc->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF);
198c6e0d914SCasey Leedom }
199c6e0d914SCasey Leedom 
200c6e0d914SCasey Leedom /**
201c6e0d914SCasey Leedom  *	is_buf_mapped - is buffer mapped for DMA?
202c6e0d914SCasey Leedom  *	@sdesc: pointer to the software buffer descriptor
203c6e0d914SCasey Leedom  *
204c6e0d914SCasey Leedom  *	Determine whether the buffer associated with a software descriptor in
205c6e0d914SCasey Leedom  *	mapped for DMA or not.
206c6e0d914SCasey Leedom  */
is_buf_mapped(const struct rx_sw_desc * sdesc)207c6e0d914SCasey Leedom static inline bool is_buf_mapped(const struct rx_sw_desc *sdesc)
208c6e0d914SCasey Leedom {
209c6e0d914SCasey Leedom 	return !(sdesc->dma_addr & RX_UNMAPPED_BUF);
210c6e0d914SCasey Leedom }
211c6e0d914SCasey Leedom 
212c6e0d914SCasey Leedom /**
213c6e0d914SCasey Leedom  *	need_skb_unmap - does the platform need unmapping of sk_buffs?
214c6e0d914SCasey Leedom  *
21525985edcSLucas De Marchi  *	Returns true if the platform needs sk_buff unmapping.  The compiler
21625985edcSLucas De Marchi  *	optimizes away unnecessary code if this returns true.
217c6e0d914SCasey Leedom  */
need_skb_unmap(void)218c6e0d914SCasey Leedom static inline int need_skb_unmap(void)
219c6e0d914SCasey Leedom {
22057b2eaf7SFUJITA Tomonori #ifdef CONFIG_NEED_DMA_MAP_STATE
22157b2eaf7SFUJITA Tomonori 	return 1;
22257b2eaf7SFUJITA Tomonori #else
22357b2eaf7SFUJITA Tomonori 	return 0;
22457b2eaf7SFUJITA Tomonori #endif
225c6e0d914SCasey Leedom }
226c6e0d914SCasey Leedom 
227c6e0d914SCasey Leedom /**
228c6e0d914SCasey Leedom  *	txq_avail - return the number of available slots in a TX queue
229c6e0d914SCasey Leedom  *	@tq: the TX queue
230c6e0d914SCasey Leedom  *
231c6e0d914SCasey Leedom  *	Returns the number of available descriptors in a TX queue.
232c6e0d914SCasey Leedom  */
txq_avail(const struct sge_txq * tq)233c6e0d914SCasey Leedom static inline unsigned int txq_avail(const struct sge_txq *tq)
234c6e0d914SCasey Leedom {
235c6e0d914SCasey Leedom 	return tq->size - 1 - tq->in_use;
236c6e0d914SCasey Leedom }
237c6e0d914SCasey Leedom 
238c6e0d914SCasey Leedom /**
239c6e0d914SCasey Leedom  *	fl_cap - return the capacity of a Free List
240c6e0d914SCasey Leedom  *	@fl: the Free List
241c6e0d914SCasey Leedom  *
242c6e0d914SCasey Leedom  *	Returns the capacity of a Free List.  The capacity is less than the
243c6e0d914SCasey Leedom  *	size because an Egress Queue Index Unit worth of descriptors needs to
244c6e0d914SCasey Leedom  *	be left unpopulated, otherwise the Producer and Consumer indices PIDX
245c6e0d914SCasey Leedom  *	and CIDX will match and the hardware will think the FL is empty.
246c6e0d914SCasey Leedom  */
fl_cap(const struct sge_fl * fl)247c6e0d914SCasey Leedom static inline unsigned int fl_cap(const struct sge_fl *fl)
248c6e0d914SCasey Leedom {
249c6e0d914SCasey Leedom 	return fl->size - FL_PER_EQ_UNIT;
250c6e0d914SCasey Leedom }
251c6e0d914SCasey Leedom 
252c6e0d914SCasey Leedom /**
253c6e0d914SCasey Leedom  *	fl_starving - return whether a Free List is starving.
25465f6ecc9SHariprasad Shenai  *	@adapter: pointer to the adapter
255c6e0d914SCasey Leedom  *	@fl: the Free List
256c6e0d914SCasey Leedom  *
257c6e0d914SCasey Leedom  *	Tests specified Free List to see whether the number of buffers
258c6e0d914SCasey Leedom  *	available to the hardware has falled below our "starvation"
25925985edcSLucas De Marchi  *	threshold.
260c6e0d914SCasey Leedom  */
fl_starving(const struct adapter * adapter,const struct sge_fl * fl)26165f6ecc9SHariprasad Shenai static inline bool fl_starving(const struct adapter *adapter,
26265f6ecc9SHariprasad Shenai 			       const struct sge_fl *fl)
263c6e0d914SCasey Leedom {
26465f6ecc9SHariprasad Shenai 	const struct sge *s = &adapter->sge;
26565f6ecc9SHariprasad Shenai 
26665f6ecc9SHariprasad Shenai 	return fl->avail - fl->pend_cred <= s->fl_starve_thres;
267c6e0d914SCasey Leedom }
268c6e0d914SCasey Leedom 
269c6e0d914SCasey Leedom /**
270c6e0d914SCasey Leedom  *	map_skb -  map an skb for DMA to the device
271c6e0d914SCasey Leedom  *	@dev: the egress net device
272c6e0d914SCasey Leedom  *	@skb: the packet to map
273c6e0d914SCasey Leedom  *	@addr: a pointer to the base of the DMA mapping array
274c6e0d914SCasey Leedom  *
275c6e0d914SCasey Leedom  *	Map an skb for DMA to the device and return an array of DMA addresses.
276c6e0d914SCasey Leedom  */
map_skb(struct device * dev,const struct sk_buff * skb,dma_addr_t * addr)277c6e0d914SCasey Leedom static int map_skb(struct device *dev, const struct sk_buff *skb,
278c6e0d914SCasey Leedom 		   dma_addr_t *addr)
279c6e0d914SCasey Leedom {
280c6e0d914SCasey Leedom 	const skb_frag_t *fp, *end;
281c6e0d914SCasey Leedom 	const struct skb_shared_info *si;
282c6e0d914SCasey Leedom 
283c6e0d914SCasey Leedom 	*addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
284c6e0d914SCasey Leedom 	if (dma_mapping_error(dev, *addr))
285c6e0d914SCasey Leedom 		goto out_err;
286c6e0d914SCasey Leedom 
287c6e0d914SCasey Leedom 	si = skb_shinfo(skb);
288c6e0d914SCasey Leedom 	end = &si->frags[si->nr_frags];
289c6e0d914SCasey Leedom 	for (fp = si->frags; fp < end; fp++) {
290a0006a86SIan Campbell 		*++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
291a0006a86SIan Campbell 					   DMA_TO_DEVICE);
292c6e0d914SCasey Leedom 		if (dma_mapping_error(dev, *addr))
293c6e0d914SCasey Leedom 			goto unwind;
294c6e0d914SCasey Leedom 	}
295c6e0d914SCasey Leedom 	return 0;
296c6e0d914SCasey Leedom 
297c6e0d914SCasey Leedom unwind:
298c6e0d914SCasey Leedom 	while (fp-- > si->frags)
2999e903e08SEric Dumazet 		dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
300c6e0d914SCasey Leedom 	dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
301c6e0d914SCasey Leedom 
302c6e0d914SCasey Leedom out_err:
303c6e0d914SCasey Leedom 	return -ENOMEM;
304c6e0d914SCasey Leedom }
305c6e0d914SCasey Leedom 
unmap_sgl(struct device * dev,const struct sk_buff * skb,const struct ulptx_sgl * sgl,const struct sge_txq * tq)306c6e0d914SCasey Leedom static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
307c6e0d914SCasey Leedom 		      const struct ulptx_sgl *sgl, const struct sge_txq *tq)
308c6e0d914SCasey Leedom {
309c6e0d914SCasey Leedom 	const struct ulptx_sge_pair *p;
310c6e0d914SCasey Leedom 	unsigned int nfrags = skb_shinfo(skb)->nr_frags;
311c6e0d914SCasey Leedom 
312c6e0d914SCasey Leedom 	if (likely(skb_headlen(skb)))
313c6e0d914SCasey Leedom 		dma_unmap_single(dev, be64_to_cpu(sgl->addr0),
314c6e0d914SCasey Leedom 				 be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
315c6e0d914SCasey Leedom 	else {
316c6e0d914SCasey Leedom 		dma_unmap_page(dev, be64_to_cpu(sgl->addr0),
317c6e0d914SCasey Leedom 			       be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
318c6e0d914SCasey Leedom 		nfrags--;
319c6e0d914SCasey Leedom 	}
320c6e0d914SCasey Leedom 
321c6e0d914SCasey Leedom 	/*
322c6e0d914SCasey Leedom 	 * the complexity below is because of the possibility of a wrap-around
323c6e0d914SCasey Leedom 	 * in the middle of an SGL
324c6e0d914SCasey Leedom 	 */
325c6e0d914SCasey Leedom 	for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
326c6e0d914SCasey Leedom 		if (likely((u8 *)(p + 1) <= (u8 *)tq->stat)) {
327c6e0d914SCasey Leedom unmap:
328c6e0d914SCasey Leedom 			dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
329c6e0d914SCasey Leedom 				       be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
330c6e0d914SCasey Leedom 			dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
331c6e0d914SCasey Leedom 				       be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
332c6e0d914SCasey Leedom 			p++;
333c6e0d914SCasey Leedom 		} else if ((u8 *)p == (u8 *)tq->stat) {
334c6e0d914SCasey Leedom 			p = (const struct ulptx_sge_pair *)tq->desc;
335c6e0d914SCasey Leedom 			goto unmap;
336c6e0d914SCasey Leedom 		} else if ((u8 *)p + 8 == (u8 *)tq->stat) {
337c6e0d914SCasey Leedom 			const __be64 *addr = (const __be64 *)tq->desc;
338c6e0d914SCasey Leedom 
339c6e0d914SCasey Leedom 			dma_unmap_page(dev, be64_to_cpu(addr[0]),
340c6e0d914SCasey Leedom 				       be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
341c6e0d914SCasey Leedom 			dma_unmap_page(dev, be64_to_cpu(addr[1]),
342c6e0d914SCasey Leedom 				       be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
343c6e0d914SCasey Leedom 			p = (const struct ulptx_sge_pair *)&addr[2];
344c6e0d914SCasey Leedom 		} else {
345c6e0d914SCasey Leedom 			const __be64 *addr = (const __be64 *)tq->desc;
346c6e0d914SCasey Leedom 
347c6e0d914SCasey Leedom 			dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
348c6e0d914SCasey Leedom 				       be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
349c6e0d914SCasey Leedom 			dma_unmap_page(dev, be64_to_cpu(addr[0]),
350c6e0d914SCasey Leedom 				       be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
351c6e0d914SCasey Leedom 			p = (const struct ulptx_sge_pair *)&addr[1];
352c6e0d914SCasey Leedom 		}
353c6e0d914SCasey Leedom 	}
354c6e0d914SCasey Leedom 	if (nfrags) {
355c6e0d914SCasey Leedom 		__be64 addr;
356c6e0d914SCasey Leedom 
357c6e0d914SCasey Leedom 		if ((u8 *)p == (u8 *)tq->stat)
358c6e0d914SCasey Leedom 			p = (const struct ulptx_sge_pair *)tq->desc;
359c6e0d914SCasey Leedom 		addr = ((u8 *)p + 16 <= (u8 *)tq->stat
360c6e0d914SCasey Leedom 			? p->addr[0]
361c6e0d914SCasey Leedom 			: *(const __be64 *)tq->desc);
362c6e0d914SCasey Leedom 		dma_unmap_page(dev, be64_to_cpu(addr), be32_to_cpu(p->len[0]),
363c6e0d914SCasey Leedom 			       DMA_TO_DEVICE);
364c6e0d914SCasey Leedom 	}
365c6e0d914SCasey Leedom }
366c6e0d914SCasey Leedom 
367c6e0d914SCasey Leedom /**
368c6e0d914SCasey Leedom  *	free_tx_desc - reclaims TX descriptors and their buffers
369c6e0d914SCasey Leedom  *	@adapter: the adapter
370c6e0d914SCasey Leedom  *	@tq: the TX queue to reclaim descriptors from
371c6e0d914SCasey Leedom  *	@n: the number of descriptors to reclaim
372c6e0d914SCasey Leedom  *	@unmap: whether the buffers should be unmapped for DMA
373c6e0d914SCasey Leedom  *
374c6e0d914SCasey Leedom  *	Reclaims TX descriptors from an SGE TX queue and frees the associated
375c6e0d914SCasey Leedom  *	TX buffers.  Called with the TX queue lock held.
376c6e0d914SCasey Leedom  */
free_tx_desc(struct adapter * adapter,struct sge_txq * tq,unsigned int n,bool unmap)377c6e0d914SCasey Leedom static void free_tx_desc(struct adapter *adapter, struct sge_txq *tq,
378c6e0d914SCasey Leedom 			 unsigned int n, bool unmap)
379c6e0d914SCasey Leedom {
380c6e0d914SCasey Leedom 	struct tx_sw_desc *sdesc;
381c6e0d914SCasey Leedom 	unsigned int cidx = tq->cidx;
382c6e0d914SCasey Leedom 	struct device *dev = adapter->pdev_dev;
383c6e0d914SCasey Leedom 
384c6e0d914SCasey Leedom 	const int need_unmap = need_skb_unmap() && unmap;
385c6e0d914SCasey Leedom 
386c6e0d914SCasey Leedom 	sdesc = &tq->sdesc[cidx];
387c6e0d914SCasey Leedom 	while (n--) {
388c6e0d914SCasey Leedom 		/*
389c6e0d914SCasey Leedom 		 * If we kept a reference to the original TX skb, we need to
390c6e0d914SCasey Leedom 		 * unmap it from PCI DMA space (if required) and free it.
391c6e0d914SCasey Leedom 		 */
392c6e0d914SCasey Leedom 		if (sdesc->skb) {
393c6e0d914SCasey Leedom 			if (need_unmap)
394c6e0d914SCasey Leedom 				unmap_sgl(dev, sdesc->skb, sdesc->sgl, tq);
39542ffda5fSEric W. Biederman 			dev_consume_skb_any(sdesc->skb);
396c6e0d914SCasey Leedom 			sdesc->skb = NULL;
397c6e0d914SCasey Leedom 		}
398c6e0d914SCasey Leedom 
399c6e0d914SCasey Leedom 		sdesc++;
400c6e0d914SCasey Leedom 		if (++cidx == tq->size) {
401c6e0d914SCasey Leedom 			cidx = 0;
402c6e0d914SCasey Leedom 			sdesc = tq->sdesc;
403c6e0d914SCasey Leedom 		}
404c6e0d914SCasey Leedom 	}
405c6e0d914SCasey Leedom 	tq->cidx = cidx;
406c6e0d914SCasey Leedom }
407c6e0d914SCasey Leedom 
408c6e0d914SCasey Leedom /*
409c6e0d914SCasey Leedom  * Return the number of reclaimable descriptors in a TX queue.
410c6e0d914SCasey Leedom  */
reclaimable(const struct sge_txq * tq)411c6e0d914SCasey Leedom static inline int reclaimable(const struct sge_txq *tq)
412c6e0d914SCasey Leedom {
413c6e0d914SCasey Leedom 	int hw_cidx = be16_to_cpu(tq->stat->cidx);
414c6e0d914SCasey Leedom 	int reclaimable = hw_cidx - tq->cidx;
415c6e0d914SCasey Leedom 	if (reclaimable < 0)
416c6e0d914SCasey Leedom 		reclaimable += tq->size;
417c6e0d914SCasey Leedom 	return reclaimable;
418c6e0d914SCasey Leedom }
419c6e0d914SCasey Leedom 
420c6e0d914SCasey Leedom /**
421c6e0d914SCasey Leedom  *	reclaim_completed_tx - reclaims completed TX descriptors
422c6e0d914SCasey Leedom  *	@adapter: the adapter
423c6e0d914SCasey Leedom  *	@tq: the TX queue to reclaim completed descriptors from
424c6e0d914SCasey Leedom  *	@unmap: whether the buffers should be unmapped for DMA
425c6e0d914SCasey Leedom  *
426c6e0d914SCasey Leedom  *	Reclaims TX descriptors that the SGE has indicated it has processed,
427c6e0d914SCasey Leedom  *	and frees the associated buffers if possible.  Called with the TX
428c6e0d914SCasey Leedom  *	queue locked.
429c6e0d914SCasey Leedom  */
reclaim_completed_tx(struct adapter * adapter,struct sge_txq * tq,bool unmap)430c6e0d914SCasey Leedom static inline void reclaim_completed_tx(struct adapter *adapter,
431c6e0d914SCasey Leedom 					struct sge_txq *tq,
432c6e0d914SCasey Leedom 					bool unmap)
433c6e0d914SCasey Leedom {
434c6e0d914SCasey Leedom 	int avail = reclaimable(tq);
435c6e0d914SCasey Leedom 
436c6e0d914SCasey Leedom 	if (avail) {
437c6e0d914SCasey Leedom 		/*
438c6e0d914SCasey Leedom 		 * Limit the amount of clean up work we do at a time to keep
439c6e0d914SCasey Leedom 		 * the TX lock hold time O(1).
440c6e0d914SCasey Leedom 		 */
441c6e0d914SCasey Leedom 		if (avail > MAX_TX_RECLAIM)
442c6e0d914SCasey Leedom 			avail = MAX_TX_RECLAIM;
443c6e0d914SCasey Leedom 
444c6e0d914SCasey Leedom 		free_tx_desc(adapter, tq, avail, unmap);
445c6e0d914SCasey Leedom 		tq->in_use -= avail;
446c6e0d914SCasey Leedom 	}
447c6e0d914SCasey Leedom }
448c6e0d914SCasey Leedom 
449c6e0d914SCasey Leedom /**
450c6e0d914SCasey Leedom  *	get_buf_size - return the size of an RX Free List buffer.
45165f6ecc9SHariprasad Shenai  *	@adapter: pointer to the associated adapter
452c6e0d914SCasey Leedom  *	@sdesc: pointer to the software buffer descriptor
453c6e0d914SCasey Leedom  */
get_buf_size(const struct adapter * adapter,const struct rx_sw_desc * sdesc)45465f6ecc9SHariprasad Shenai static inline int get_buf_size(const struct adapter *adapter,
45565f6ecc9SHariprasad Shenai 			       const struct rx_sw_desc *sdesc)
456c6e0d914SCasey Leedom {
45765f6ecc9SHariprasad Shenai 	const struct sge *s = &adapter->sge;
45865f6ecc9SHariprasad Shenai 
45965f6ecc9SHariprasad Shenai 	return (s->fl_pg_order > 0 && (sdesc->dma_addr & RX_LARGE_BUF)
46065f6ecc9SHariprasad Shenai 		? (PAGE_SIZE << s->fl_pg_order) : PAGE_SIZE);
461c6e0d914SCasey Leedom }
462c6e0d914SCasey Leedom 
463c6e0d914SCasey Leedom /**
464c6e0d914SCasey Leedom  *	free_rx_bufs - free RX buffers on an SGE Free List
465c6e0d914SCasey Leedom  *	@adapter: the adapter
466c6e0d914SCasey Leedom  *	@fl: the SGE Free List to free buffers from
467c6e0d914SCasey Leedom  *	@n: how many buffers to free
468c6e0d914SCasey Leedom  *
469c6e0d914SCasey Leedom  *	Release the next @n buffers on an SGE Free List RX queue.   The
470c6e0d914SCasey Leedom  *	buffers must be made inaccessible to hardware before calling this
471c6e0d914SCasey Leedom  *	function.
472c6e0d914SCasey Leedom  */
free_rx_bufs(struct adapter * adapter,struct sge_fl * fl,int n)473c6e0d914SCasey Leedom static void free_rx_bufs(struct adapter *adapter, struct sge_fl *fl, int n)
474c6e0d914SCasey Leedom {
475c6e0d914SCasey Leedom 	while (n--) {
476c6e0d914SCasey Leedom 		struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
477c6e0d914SCasey Leedom 
478c6e0d914SCasey Leedom 		if (is_buf_mapped(sdesc))
479c6e0d914SCasey Leedom 			dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
48065f6ecc9SHariprasad Shenai 				       get_buf_size(adapter, sdesc),
4814489d8f5SChristophe JAILLET 				       DMA_FROM_DEVICE);
482c6e0d914SCasey Leedom 		put_page(sdesc->page);
483c6e0d914SCasey Leedom 		sdesc->page = NULL;
484c6e0d914SCasey Leedom 		if (++fl->cidx == fl->size)
485c6e0d914SCasey Leedom 			fl->cidx = 0;
486c6e0d914SCasey Leedom 		fl->avail--;
487c6e0d914SCasey Leedom 	}
488c6e0d914SCasey Leedom }
489c6e0d914SCasey Leedom 
490c6e0d914SCasey Leedom /**
491c6e0d914SCasey Leedom  *	unmap_rx_buf - unmap the current RX buffer on an SGE Free List
492c6e0d914SCasey Leedom  *	@adapter: the adapter
493c6e0d914SCasey Leedom  *	@fl: the SGE Free List
494c6e0d914SCasey Leedom  *
495c6e0d914SCasey Leedom  *	Unmap the current buffer on an SGE Free List RX queue.   The
496c6e0d914SCasey Leedom  *	buffer must be made inaccessible to HW before calling this function.
497c6e0d914SCasey Leedom  *
498c6e0d914SCasey Leedom  *	This is similar to @free_rx_bufs above but does not free the buffer.
499c6e0d914SCasey Leedom  *	Do note that the FL still loses any further access to the buffer.
500c6e0d914SCasey Leedom  *	This is used predominantly to "transfer ownership" of an FL buffer
501c6e0d914SCasey Leedom  *	to another entity (typically an skb's fragment list).
502c6e0d914SCasey Leedom  */
unmap_rx_buf(struct adapter * adapter,struct sge_fl * fl)503c6e0d914SCasey Leedom static void unmap_rx_buf(struct adapter *adapter, struct sge_fl *fl)
504c6e0d914SCasey Leedom {
505c6e0d914SCasey Leedom 	struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
506c6e0d914SCasey Leedom 
507c6e0d914SCasey Leedom 	if (is_buf_mapped(sdesc))
508c6e0d914SCasey Leedom 		dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
50965f6ecc9SHariprasad Shenai 			       get_buf_size(adapter, sdesc),
5104489d8f5SChristophe JAILLET 			       DMA_FROM_DEVICE);
511c6e0d914SCasey Leedom 	sdesc->page = NULL;
512c6e0d914SCasey Leedom 	if (++fl->cidx == fl->size)
513c6e0d914SCasey Leedom 		fl->cidx = 0;
514c6e0d914SCasey Leedom 	fl->avail--;
515c6e0d914SCasey Leedom }
516c6e0d914SCasey Leedom 
517c6e0d914SCasey Leedom /**
518c6e0d914SCasey Leedom  *	ring_fl_db - righ doorbell on free list
519c6e0d914SCasey Leedom  *	@adapter: the adapter
520c6e0d914SCasey Leedom  *	@fl: the Free List whose doorbell should be rung ...
521c6e0d914SCasey Leedom  *
522c6e0d914SCasey Leedom  *	Tell the Scatter Gather Engine that there are new free list entries
523c6e0d914SCasey Leedom  *	available.
524c6e0d914SCasey Leedom  */
ring_fl_db(struct adapter * adapter,struct sge_fl * fl)525c6e0d914SCasey Leedom static inline void ring_fl_db(struct adapter *adapter, struct sge_fl *fl)
526c6e0d914SCasey Leedom {
52741fc2e41SHariprasad Shenai 	u32 val = adapter->params.arch.sge_fl_db;
528622c62b5SSantosh Rastapur 
529df64e4d3SHariprasad Shenai 	/* The SGE keeps track of its Producer and Consumer Indices in terms
530c6e0d914SCasey Leedom 	 * of Egress Queue Units so we can only tell it about integral numbers
531c6e0d914SCasey Leedom 	 * of multiples of Free List Entries per Egress Queue Units ...
532c6e0d914SCasey Leedom 	 */
533c6e0d914SCasey Leedom 	if (fl->pend_cred >= FL_PER_EQ_UNIT) {
534df64e4d3SHariprasad Shenai 		if (is_t4(adapter->params.chip))
53541fc2e41SHariprasad Shenai 			val |= PIDX_V(fl->pend_cred / FL_PER_EQ_UNIT);
536df64e4d3SHariprasad Shenai 		else
53741fc2e41SHariprasad Shenai 			val |= PIDX_T5_V(fl->pend_cred / FL_PER_EQ_UNIT);
538df64e4d3SHariprasad Shenai 
539df64e4d3SHariprasad Shenai 		/* Make sure all memory writes to the Free List queue are
540df64e4d3SHariprasad Shenai 		 * committed before we tell the hardware about them.
541df64e4d3SHariprasad Shenai 		 */
542c6e0d914SCasey Leedom 		wmb();
543df64e4d3SHariprasad Shenai 
544df64e4d3SHariprasad Shenai 		/* If we don't have access to the new User Doorbell (T5+), use
545df64e4d3SHariprasad Shenai 		 * the old doorbell mechanism; otherwise use the new BAR2
546df64e4d3SHariprasad Shenai 		 * mechanism.
547df64e4d3SHariprasad Shenai 		 */
548df64e4d3SHariprasad Shenai 		if (unlikely(fl->bar2_addr == NULL)) {
549df64e4d3SHariprasad Shenai 			t4_write_reg(adapter,
550df64e4d3SHariprasad Shenai 				     T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
551f612b815SHariprasad Shenai 				     QID_V(fl->cntxt_id) | val);
552df64e4d3SHariprasad Shenai 		} else {
553f612b815SHariprasad Shenai 			writel(val | QID_V(fl->bar2_qid),
554df64e4d3SHariprasad Shenai 			       fl->bar2_addr + SGE_UDB_KDOORBELL);
555df64e4d3SHariprasad Shenai 
556df64e4d3SHariprasad Shenai 			/* This Write memory Barrier will force the write to
557df64e4d3SHariprasad Shenai 			 * the User Doorbell area to be flushed.
558df64e4d3SHariprasad Shenai 			 */
559df64e4d3SHariprasad Shenai 			wmb();
560df64e4d3SHariprasad Shenai 		}
561c6e0d914SCasey Leedom 		fl->pend_cred %= FL_PER_EQ_UNIT;
562c6e0d914SCasey Leedom 	}
563c6e0d914SCasey Leedom }
564c6e0d914SCasey Leedom 
565c6e0d914SCasey Leedom /**
566c6e0d914SCasey Leedom  *	set_rx_sw_desc - initialize software RX buffer descriptor
567c6e0d914SCasey Leedom  *	@sdesc: pointer to the softwore RX buffer descriptor
568c6e0d914SCasey Leedom  *	@page: pointer to the page data structure backing the RX buffer
569c6e0d914SCasey Leedom  *	@dma_addr: PCI DMA address (possibly with low-bit flags)
570c6e0d914SCasey Leedom  */
set_rx_sw_desc(struct rx_sw_desc * sdesc,struct page * page,dma_addr_t dma_addr)571c6e0d914SCasey Leedom static inline void set_rx_sw_desc(struct rx_sw_desc *sdesc, struct page *page,
572c6e0d914SCasey Leedom 				  dma_addr_t dma_addr)
573c6e0d914SCasey Leedom {
574c6e0d914SCasey Leedom 	sdesc->page = page;
575c6e0d914SCasey Leedom 	sdesc->dma_addr = dma_addr;
576c6e0d914SCasey Leedom }
577c6e0d914SCasey Leedom 
578c6e0d914SCasey Leedom /*
579c6e0d914SCasey Leedom  * Support for poisoning RX buffers ...
580c6e0d914SCasey Leedom  */
581c6e0d914SCasey Leedom #define POISON_BUF_VAL -1
582c6e0d914SCasey Leedom 
poison_buf(struct page * page,size_t sz)583c6e0d914SCasey Leedom static inline void poison_buf(struct page *page, size_t sz)
584c6e0d914SCasey Leedom {
585c6e0d914SCasey Leedom #if POISON_BUF_VAL >= 0
586c6e0d914SCasey Leedom 	memset(page_address(page), POISON_BUF_VAL, sz);
587c6e0d914SCasey Leedom #endif
588c6e0d914SCasey Leedom }
589c6e0d914SCasey Leedom 
590c6e0d914SCasey Leedom /**
591c6e0d914SCasey Leedom  *	refill_fl - refill an SGE RX buffer ring
592c6e0d914SCasey Leedom  *	@adapter: the adapter
593c6e0d914SCasey Leedom  *	@fl: the Free List ring to refill
594c6e0d914SCasey Leedom  *	@n: the number of new buffers to allocate
595c6e0d914SCasey Leedom  *	@gfp: the gfp flags for the allocations
596c6e0d914SCasey Leedom  *
597c6e0d914SCasey Leedom  *	(Re)populate an SGE free-buffer queue with up to @n new packet buffers,
598c6e0d914SCasey Leedom  *	allocated with the supplied gfp flags.  The caller must assure that
599c6e0d914SCasey Leedom  *	@n does not exceed the queue's capacity -- i.e. (cidx == pidx) _IN
600c6e0d914SCasey Leedom  *	EGRESS QUEUE UNITS_ indicates an empty Free List!  Returns the number
601c6e0d914SCasey Leedom  *	of buffers allocated.  If afterwards the queue is found critically low,
602c6e0d914SCasey Leedom  *	mark it as starving in the bitmap of starving FLs.
603c6e0d914SCasey Leedom  */
refill_fl(struct adapter * adapter,struct sge_fl * fl,int n,gfp_t gfp)604c6e0d914SCasey Leedom static unsigned int refill_fl(struct adapter *adapter, struct sge_fl *fl,
605c6e0d914SCasey Leedom 			      int n, gfp_t gfp)
606c6e0d914SCasey Leedom {
60765f6ecc9SHariprasad Shenai 	struct sge *s = &adapter->sge;
608c6e0d914SCasey Leedom 	struct page *page;
609c6e0d914SCasey Leedom 	dma_addr_t dma_addr;
610c6e0d914SCasey Leedom 	unsigned int cred = fl->avail;
611c6e0d914SCasey Leedom 	__be64 *d = &fl->desc[fl->pidx];
612c6e0d914SCasey Leedom 	struct rx_sw_desc *sdesc = &fl->sdesc[fl->pidx];
613c6e0d914SCasey Leedom 
614c6e0d914SCasey Leedom 	/*
615c6e0d914SCasey Leedom 	 * Sanity: ensure that the result of adding n Free List buffers
616c6e0d914SCasey Leedom 	 * won't result in wrapping the SGE's Producer Index around to
617c6e0d914SCasey Leedom 	 * it's Consumer Index thereby indicating an empty Free List ...
618c6e0d914SCasey Leedom 	 */
619c6e0d914SCasey Leedom 	BUG_ON(fl->avail + n > fl->size - FL_PER_EQ_UNIT);
620c6e0d914SCasey Leedom 
621aa9cd31cSAlexander Duyck 	gfp |= __GFP_NOWARN;
622aa9cd31cSAlexander Duyck 
623c6e0d914SCasey Leedom 	/*
624c6e0d914SCasey Leedom 	 * If we support large pages, prefer large buffers and fail over to
625c6e0d914SCasey Leedom 	 * small pages if we can't allocate large pages to satisfy the refill.
626c6e0d914SCasey Leedom 	 * If we don't support large pages, drop directly into the small page
627c6e0d914SCasey Leedom 	 * allocation code.
628c6e0d914SCasey Leedom 	 */
62965f6ecc9SHariprasad Shenai 	if (s->fl_pg_order == 0)
630c6e0d914SCasey Leedom 		goto alloc_small_pages;
631c6e0d914SCasey Leedom 
632c6e0d914SCasey Leedom 	while (n) {
633076ce448SDavid S. Miller 		page = __dev_alloc_pages(gfp, s->fl_pg_order);
634c6e0d914SCasey Leedom 		if (unlikely(!page)) {
635c6e0d914SCasey Leedom 			/*
636c6e0d914SCasey Leedom 			 * We've failed inour attempt to allocate a "large
637c6e0d914SCasey Leedom 			 * page".  Fail over to the "small page" allocation
638c6e0d914SCasey Leedom 			 * below.
639c6e0d914SCasey Leedom 			 */
640c6e0d914SCasey Leedom 			fl->large_alloc_failed++;
641c6e0d914SCasey Leedom 			break;
642c6e0d914SCasey Leedom 		}
64365f6ecc9SHariprasad Shenai 		poison_buf(page, PAGE_SIZE << s->fl_pg_order);
644c6e0d914SCasey Leedom 
645c6e0d914SCasey Leedom 		dma_addr = dma_map_page(adapter->pdev_dev, page, 0,
64665f6ecc9SHariprasad Shenai 					PAGE_SIZE << s->fl_pg_order,
6474489d8f5SChristophe JAILLET 					DMA_FROM_DEVICE);
648c6e0d914SCasey Leedom 		if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
649c6e0d914SCasey Leedom 			/*
650c6e0d914SCasey Leedom 			 * We've run out of DMA mapping space.  Free up the
651c6e0d914SCasey Leedom 			 * buffer and return with what we've managed to put
652c6e0d914SCasey Leedom 			 * into the free list.  We don't want to fail over to
653c6e0d914SCasey Leedom 			 * the small page allocation below in this case
654c6e0d914SCasey Leedom 			 * because DMA mapping resources are typically
655c6e0d914SCasey Leedom 			 * critical resources once they become scarse.
656c6e0d914SCasey Leedom 			 */
65765f6ecc9SHariprasad Shenai 			__free_pages(page, s->fl_pg_order);
658c6e0d914SCasey Leedom 			goto out;
659c6e0d914SCasey Leedom 		}
660c6e0d914SCasey Leedom 		dma_addr |= RX_LARGE_BUF;
661c6e0d914SCasey Leedom 		*d++ = cpu_to_be64(dma_addr);
662c6e0d914SCasey Leedom 
663c6e0d914SCasey Leedom 		set_rx_sw_desc(sdesc, page, dma_addr);
664c6e0d914SCasey Leedom 		sdesc++;
665c6e0d914SCasey Leedom 
666c6e0d914SCasey Leedom 		fl->avail++;
667c6e0d914SCasey Leedom 		if (++fl->pidx == fl->size) {
668c6e0d914SCasey Leedom 			fl->pidx = 0;
669c6e0d914SCasey Leedom 			sdesc = fl->sdesc;
670c6e0d914SCasey Leedom 			d = fl->desc;
671c6e0d914SCasey Leedom 		}
672c6e0d914SCasey Leedom 		n--;
673c6e0d914SCasey Leedom 	}
674c6e0d914SCasey Leedom 
675c6e0d914SCasey Leedom alloc_small_pages:
676c6e0d914SCasey Leedom 	while (n--) {
677aa9cd31cSAlexander Duyck 		page = __dev_alloc_page(gfp);
678c6e0d914SCasey Leedom 		if (unlikely(!page)) {
679c6e0d914SCasey Leedom 			fl->alloc_failed++;
680c6e0d914SCasey Leedom 			break;
681c6e0d914SCasey Leedom 		}
682c6e0d914SCasey Leedom 		poison_buf(page, PAGE_SIZE);
683c6e0d914SCasey Leedom 
684c6e0d914SCasey Leedom 		dma_addr = dma_map_page(adapter->pdev_dev, page, 0, PAGE_SIZE,
6854489d8f5SChristophe JAILLET 				       DMA_FROM_DEVICE);
686c6e0d914SCasey Leedom 		if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
6871f2149c1SEric Dumazet 			put_page(page);
688c6e0d914SCasey Leedom 			break;
689c6e0d914SCasey Leedom 		}
690c6e0d914SCasey Leedom 		*d++ = cpu_to_be64(dma_addr);
691c6e0d914SCasey Leedom 
692c6e0d914SCasey Leedom 		set_rx_sw_desc(sdesc, page, dma_addr);
693c6e0d914SCasey Leedom 		sdesc++;
694c6e0d914SCasey Leedom 
695c6e0d914SCasey Leedom 		fl->avail++;
696c6e0d914SCasey Leedom 		if (++fl->pidx == fl->size) {
697c6e0d914SCasey Leedom 			fl->pidx = 0;
698c6e0d914SCasey Leedom 			sdesc = fl->sdesc;
699c6e0d914SCasey Leedom 			d = fl->desc;
700c6e0d914SCasey Leedom 		}
701c6e0d914SCasey Leedom 	}
702c6e0d914SCasey Leedom 
703c6e0d914SCasey Leedom out:
704c6e0d914SCasey Leedom 	/*
705c6e0d914SCasey Leedom 	 * Update our accounting state to incorporate the new Free List
706c6e0d914SCasey Leedom 	 * buffers, tell the hardware about them and return the number of
70790802ed9SPaul Bolle 	 * buffers which we were able to allocate.
708c6e0d914SCasey Leedom 	 */
709c6e0d914SCasey Leedom 	cred = fl->avail - cred;
710c6e0d914SCasey Leedom 	fl->pend_cred += cred;
711c6e0d914SCasey Leedom 	ring_fl_db(adapter, fl);
712c6e0d914SCasey Leedom 
71365f6ecc9SHariprasad Shenai 	if (unlikely(fl_starving(adapter, fl))) {
714c6e0d914SCasey Leedom 		smp_wmb();
715c6e0d914SCasey Leedom 		set_bit(fl->cntxt_id, adapter->sge.starving_fl);
716c6e0d914SCasey Leedom 	}
717c6e0d914SCasey Leedom 
718c6e0d914SCasey Leedom 	return cred;
719c6e0d914SCasey Leedom }
720c6e0d914SCasey Leedom 
721c6e0d914SCasey Leedom /*
722c6e0d914SCasey Leedom  * Refill a Free List to its capacity or the Maximum Refill Increment,
723c6e0d914SCasey Leedom  * whichever is smaller ...
724c6e0d914SCasey Leedom  */
__refill_fl(struct adapter * adapter,struct sge_fl * fl)725c6e0d914SCasey Leedom static inline void __refill_fl(struct adapter *adapter, struct sge_fl *fl)
726c6e0d914SCasey Leedom {
727c6e0d914SCasey Leedom 	refill_fl(adapter, fl,
728c6e0d914SCasey Leedom 		  min((unsigned int)MAX_RX_REFILL, fl_cap(fl) - fl->avail),
729c6e0d914SCasey Leedom 		  GFP_ATOMIC);
730c6e0d914SCasey Leedom }
731c6e0d914SCasey Leedom 
732c6e0d914SCasey Leedom /**
733c6e0d914SCasey Leedom  *	alloc_ring - allocate resources for an SGE descriptor ring
734c6e0d914SCasey Leedom  *	@dev: the PCI device's core device
735c6e0d914SCasey Leedom  *	@nelem: the number of descriptors
736c6e0d914SCasey Leedom  *	@hwsize: the size of each hardware descriptor
737c6e0d914SCasey Leedom  *	@swsize: the size of each software descriptor
738c6e0d914SCasey Leedom  *	@busaddrp: the physical PCI bus address of the allocated ring
739c6e0d914SCasey Leedom  *	@swringp: return address pointer for software ring
740c6e0d914SCasey Leedom  *	@stat_size: extra space in hardware ring for status information
741c6e0d914SCasey Leedom  *
742c6e0d914SCasey Leedom  *	Allocates resources for an SGE descriptor ring, such as TX queues,
743c6e0d914SCasey Leedom  *	free buffer lists, response queues, etc.  Each SGE ring requires
744c6e0d914SCasey Leedom  *	space for its hardware descriptors plus, optionally, space for software
745c6e0d914SCasey Leedom  *	state associated with each hardware entry (the metadata).  The function
746c6e0d914SCasey Leedom  *	returns three values: the virtual address for the hardware ring (the
747c6e0d914SCasey Leedom  *	return value of the function), the PCI bus address of the hardware
748c6e0d914SCasey Leedom  *	ring (in *busaddrp), and the address of the software ring (in swringp).
749c6e0d914SCasey Leedom  *	Both the hardware and software rings are returned zeroed out.
750c6e0d914SCasey Leedom  */
alloc_ring(struct device * dev,size_t nelem,size_t hwsize,size_t swsize,dma_addr_t * busaddrp,void * swringp,size_t stat_size)751c6e0d914SCasey Leedom static void *alloc_ring(struct device *dev, size_t nelem, size_t hwsize,
752c6e0d914SCasey Leedom 			size_t swsize, dma_addr_t *busaddrp, void *swringp,
753c6e0d914SCasey Leedom 			size_t stat_size)
754c6e0d914SCasey Leedom {
755c6e0d914SCasey Leedom 	/*
756c6e0d914SCasey Leedom 	 * Allocate the hardware ring and PCI DMA bus address space for said.
757c6e0d914SCasey Leedom 	 */
758c6e0d914SCasey Leedom 	size_t hwlen = nelem * hwsize + stat_size;
759750afb08SLuis Chamberlain 	void *hwring = dma_alloc_coherent(dev, hwlen, busaddrp, GFP_KERNEL);
760c6e0d914SCasey Leedom 
761c6e0d914SCasey Leedom 	if (!hwring)
762c6e0d914SCasey Leedom 		return NULL;
763c6e0d914SCasey Leedom 
764c6e0d914SCasey Leedom 	/*
765c6e0d914SCasey Leedom 	 * If the caller wants a software ring, allocate it and return a
766c6e0d914SCasey Leedom 	 * pointer to it in *swringp.
767c6e0d914SCasey Leedom 	 */
768c6e0d914SCasey Leedom 	BUG_ON((swsize != 0) != (swringp != NULL));
769c6e0d914SCasey Leedom 	if (swsize) {
770c6e0d914SCasey Leedom 		void *swring = kcalloc(nelem, swsize, GFP_KERNEL);
771c6e0d914SCasey Leedom 
772c6e0d914SCasey Leedom 		if (!swring) {
773c6e0d914SCasey Leedom 			dma_free_coherent(dev, hwlen, hwring, *busaddrp);
774c6e0d914SCasey Leedom 			return NULL;
775c6e0d914SCasey Leedom 		}
776c6e0d914SCasey Leedom 		*(void **)swringp = swring;
777c6e0d914SCasey Leedom 	}
778c6e0d914SCasey Leedom 
779c6e0d914SCasey Leedom 	return hwring;
780c6e0d914SCasey Leedom }
781c6e0d914SCasey Leedom 
782c6e0d914SCasey Leedom /**
783c6e0d914SCasey Leedom  *	sgl_len - calculates the size of an SGL of the given capacity
784c6e0d914SCasey Leedom  *	@n: the number of SGL entries
785c6e0d914SCasey Leedom  *
786c6e0d914SCasey Leedom  *	Calculates the number of flits (8-byte units) needed for a Direct
787c6e0d914SCasey Leedom  *	Scatter/Gather List that can hold the given number of entries.
788c6e0d914SCasey Leedom  */
sgl_len(unsigned int n)789c6e0d914SCasey Leedom static inline unsigned int sgl_len(unsigned int n)
790c6e0d914SCasey Leedom {
791c6e0d914SCasey Leedom 	/*
792c6e0d914SCasey Leedom 	 * A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
793c6e0d914SCasey Leedom 	 * addresses.  The DSGL Work Request starts off with a 32-bit DSGL
794c6e0d914SCasey Leedom 	 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
795c6e0d914SCasey Leedom 	 * repeated sequences of { Length[i], Length[i+1], Address[i],
796c6e0d914SCasey Leedom 	 * Address[i+1] } (this ensures that all addresses are on 64-bit
797c6e0d914SCasey Leedom 	 * boundaries).  If N is even, then Length[N+1] should be set to 0 and
798c6e0d914SCasey Leedom 	 * Address[N+1] is omitted.
799c6e0d914SCasey Leedom 	 *
800c6e0d914SCasey Leedom 	 * The following calculation incorporates all of the above.  It's
801c6e0d914SCasey Leedom 	 * somewhat hard to follow but, briefly: the "+2" accounts for the
802c6e0d914SCasey Leedom 	 * first two flits which include the DSGL header, Length0 and
803c6e0d914SCasey Leedom 	 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
804c6e0d914SCasey Leedom 	 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
805c6e0d914SCasey Leedom 	 * finally the "+((n-1)&1)" adds the one remaining flit needed if
806c6e0d914SCasey Leedom 	 * (n-1) is odd ...
807c6e0d914SCasey Leedom 	 */
808c6e0d914SCasey Leedom 	n--;
809c6e0d914SCasey Leedom 	return (3 * n) / 2 + (n & 1) + 2;
810c6e0d914SCasey Leedom }
811c6e0d914SCasey Leedom 
812c6e0d914SCasey Leedom /**
813c6e0d914SCasey Leedom  *	flits_to_desc - returns the num of TX descriptors for the given flits
814c6e0d914SCasey Leedom  *	@flits: the number of flits
815c6e0d914SCasey Leedom  *
816c6e0d914SCasey Leedom  *	Returns the number of TX descriptors needed for the supplied number
817c6e0d914SCasey Leedom  *	of flits.
818c6e0d914SCasey Leedom  */
flits_to_desc(unsigned int flits)819c6e0d914SCasey Leedom static inline unsigned int flits_to_desc(unsigned int flits)
820c6e0d914SCasey Leedom {
821c6e0d914SCasey Leedom 	BUG_ON(flits > SGE_MAX_WR_LEN / sizeof(__be64));
822c6e0d914SCasey Leedom 	return DIV_ROUND_UP(flits, TXD_PER_EQ_UNIT);
823c6e0d914SCasey Leedom }
824c6e0d914SCasey Leedom 
825c6e0d914SCasey Leedom /**
826c6e0d914SCasey Leedom  *	is_eth_imm - can an Ethernet packet be sent as immediate data?
827c6e0d914SCasey Leedom  *	@skb: the packet
828c6e0d914SCasey Leedom  *
829c6e0d914SCasey Leedom  *	Returns whether an Ethernet packet is small enough to fit completely as
830c6e0d914SCasey Leedom  *	immediate data.
831c6e0d914SCasey Leedom  */
is_eth_imm(const struct sk_buff * skb)832c6e0d914SCasey Leedom static inline int is_eth_imm(const struct sk_buff *skb)
833c6e0d914SCasey Leedom {
834c6e0d914SCasey Leedom 	/*
835c6e0d914SCasey Leedom 	 * The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request
836c6e0d914SCasey Leedom 	 * which does not accommodate immediate data.  We could dike out all
837c6e0d914SCasey Leedom 	 * of the support code for immediate data but that would tie our hands
838c6e0d914SCasey Leedom 	 * too much if we ever want to enhace the firmware.  It would also
839c6e0d914SCasey Leedom 	 * create more differences between the PF and VF Drivers.
840c6e0d914SCasey Leedom 	 */
841c6e0d914SCasey Leedom 	return false;
842c6e0d914SCasey Leedom }
843c6e0d914SCasey Leedom 
844c6e0d914SCasey Leedom /**
845c6e0d914SCasey Leedom  *	calc_tx_flits - calculate the number of flits for a packet TX WR
846c6e0d914SCasey Leedom  *	@skb: the packet
847c6e0d914SCasey Leedom  *
848c6e0d914SCasey Leedom  *	Returns the number of flits needed for a TX Work Request for the
849c6e0d914SCasey Leedom  *	given Ethernet packet, including the needed WR and CPL headers.
850c6e0d914SCasey Leedom  */
calc_tx_flits(const struct sk_buff * skb)851c6e0d914SCasey Leedom static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
852c6e0d914SCasey Leedom {
853c6e0d914SCasey Leedom 	unsigned int flits;
854c6e0d914SCasey Leedom 
855c6e0d914SCasey Leedom 	/*
856c6e0d914SCasey Leedom 	 * If the skb is small enough, we can pump it out as a work request
857c6e0d914SCasey Leedom 	 * with only immediate data.  In that case we just have to have the
858c6e0d914SCasey Leedom 	 * TX Packet header plus the skb data in the Work Request.
859c6e0d914SCasey Leedom 	 */
860c6e0d914SCasey Leedom 	if (is_eth_imm(skb))
861c6e0d914SCasey Leedom 		return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt),
862c6e0d914SCasey Leedom 				    sizeof(__be64));
863c6e0d914SCasey Leedom 
864c6e0d914SCasey Leedom 	/*
865c6e0d914SCasey Leedom 	 * Otherwise, we're going to have to construct a Scatter gather list
866c6e0d914SCasey Leedom 	 * of the skb body and fragments.  We also include the flits necessary
867c6e0d914SCasey Leedom 	 * for the TX Packet Work Request and CPL.  We always have a firmware
868c6e0d914SCasey Leedom 	 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
869c6e0d914SCasey Leedom 	 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
870c6e0d914SCasey Leedom 	 * message or, if we're doing a Large Send Offload, an LSO CPL message
871dbedd44eSJoe Perches 	 * with an embedded TX Packet Write CPL message.
872c6e0d914SCasey Leedom 	 */
873c6e0d914SCasey Leedom 	flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
874c6e0d914SCasey Leedom 	if (skb_shinfo(skb)->gso_size)
875c6e0d914SCasey Leedom 		flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
876c6e0d914SCasey Leedom 			  sizeof(struct cpl_tx_pkt_lso_core) +
877c6e0d914SCasey Leedom 			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
878c6e0d914SCasey Leedom 	else
879c6e0d914SCasey Leedom 		flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
880c6e0d914SCasey Leedom 			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
881c6e0d914SCasey Leedom 	return flits;
882c6e0d914SCasey Leedom }
883c6e0d914SCasey Leedom 
884c6e0d914SCasey Leedom /**
885c6e0d914SCasey Leedom  *	write_sgl - populate a Scatter/Gather List for a packet
886c6e0d914SCasey Leedom  *	@skb: the packet
887c6e0d914SCasey Leedom  *	@tq: the TX queue we are writing into
888c6e0d914SCasey Leedom  *	@sgl: starting location for writing the SGL
889c6e0d914SCasey Leedom  *	@end: points right after the end of the SGL
890c6e0d914SCasey Leedom  *	@start: start offset into skb main-body data to include in the SGL
891c6e0d914SCasey Leedom  *	@addr: the list of DMA bus addresses for the SGL elements
892c6e0d914SCasey Leedom  *
893c6e0d914SCasey Leedom  *	Generates a Scatter/Gather List for the buffers that make up a packet.
894c6e0d914SCasey Leedom  *	The caller must provide adequate space for the SGL that will be written.
895c6e0d914SCasey Leedom  *	The SGL includes all of the packet's page fragments and the data in its
896c6e0d914SCasey Leedom  *	main body except for the first @start bytes.  @pos must be 16-byte
897c6e0d914SCasey Leedom  *	aligned and within a TX descriptor with available space.  @end points
898c6e0d914SCasey Leedom  *	write after the end of the SGL but does not account for any potential
899c6e0d914SCasey Leedom  *	wrap around, i.e., @end > @tq->stat.
900c6e0d914SCasey Leedom  */
write_sgl(const struct sk_buff * skb,struct sge_txq * tq,struct ulptx_sgl * sgl,u64 * end,unsigned int start,const dma_addr_t * addr)901c6e0d914SCasey Leedom static void write_sgl(const struct sk_buff *skb, struct sge_txq *tq,
902c6e0d914SCasey Leedom 		      struct ulptx_sgl *sgl, u64 *end, unsigned int start,
903c6e0d914SCasey Leedom 		      const dma_addr_t *addr)
904c6e0d914SCasey Leedom {
905c6e0d914SCasey Leedom 	unsigned int i, len;
906c6e0d914SCasey Leedom 	struct ulptx_sge_pair *to;
907c6e0d914SCasey Leedom 	const struct skb_shared_info *si = skb_shinfo(skb);
908c6e0d914SCasey Leedom 	unsigned int nfrags = si->nr_frags;
909c6e0d914SCasey Leedom 	struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
910c6e0d914SCasey Leedom 
911c6e0d914SCasey Leedom 	len = skb_headlen(skb) - start;
912c6e0d914SCasey Leedom 	if (likely(len)) {
913c6e0d914SCasey Leedom 		sgl->len0 = htonl(len);
914c6e0d914SCasey Leedom 		sgl->addr0 = cpu_to_be64(addr[0] + start);
915c6e0d914SCasey Leedom 		nfrags++;
916c6e0d914SCasey Leedom 	} else {
9179e903e08SEric Dumazet 		sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
918c6e0d914SCasey Leedom 		sgl->addr0 = cpu_to_be64(addr[1]);
919c6e0d914SCasey Leedom 	}
920c6e0d914SCasey Leedom 
921d7990b0cSAnish Bhatt 	sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
922bdc590b9SHariprasad Shenai 			      ULPTX_NSGE_V(nfrags));
923c6e0d914SCasey Leedom 	if (likely(--nfrags == 0))
924c6e0d914SCasey Leedom 		return;
925c6e0d914SCasey Leedom 	/*
926c6e0d914SCasey Leedom 	 * Most of the complexity below deals with the possibility we hit the
927c6e0d914SCasey Leedom 	 * end of the queue in the middle of writing the SGL.  For this case
928c6e0d914SCasey Leedom 	 * only we create the SGL in a temporary buffer and then copy it.
929c6e0d914SCasey Leedom 	 */
930c6e0d914SCasey Leedom 	to = (u8 *)end > (u8 *)tq->stat ? buf : sgl->sge;
931c6e0d914SCasey Leedom 
932c6e0d914SCasey Leedom 	for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
9339e903e08SEric Dumazet 		to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
9349e903e08SEric Dumazet 		to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
935c6e0d914SCasey Leedom 		to->addr[0] = cpu_to_be64(addr[i]);
936c6e0d914SCasey Leedom 		to->addr[1] = cpu_to_be64(addr[++i]);
937c6e0d914SCasey Leedom 	}
938c6e0d914SCasey Leedom 	if (nfrags) {
9399e903e08SEric Dumazet 		to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
940c6e0d914SCasey Leedom 		to->len[1] = cpu_to_be32(0);
941c6e0d914SCasey Leedom 		to->addr[0] = cpu_to_be64(addr[i + 1]);
942c6e0d914SCasey Leedom 	}
943c6e0d914SCasey Leedom 	if (unlikely((u8 *)end > (u8 *)tq->stat)) {
944c6e0d914SCasey Leedom 		unsigned int part0 = (u8 *)tq->stat - (u8 *)sgl->sge, part1;
945c6e0d914SCasey Leedom 
946c6e0d914SCasey Leedom 		if (likely(part0))
947c6e0d914SCasey Leedom 			memcpy(sgl->sge, buf, part0);
948c6e0d914SCasey Leedom 		part1 = (u8 *)end - (u8 *)tq->stat;
949c6e0d914SCasey Leedom 		memcpy(tq->desc, (u8 *)buf + part0, part1);
950c6e0d914SCasey Leedom 		end = (void *)tq->desc + part1;
951c6e0d914SCasey Leedom 	}
952c6e0d914SCasey Leedom 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
95364699336SJoe Perches 		*end = 0;
954c6e0d914SCasey Leedom }
955c6e0d914SCasey Leedom 
956c6e0d914SCasey Leedom /**
9571eb00ff5SYang Shen  *	ring_tx_db - check and potentially ring a TX queue's doorbell
958c6e0d914SCasey Leedom  *	@adapter: the adapter
959c6e0d914SCasey Leedom  *	@tq: the TX queue
960c6e0d914SCasey Leedom  *	@n: number of new descriptors to give to HW
961c6e0d914SCasey Leedom  *
962c6e0d914SCasey Leedom  *	Ring the doorbel for a TX queue.
963c6e0d914SCasey Leedom  */
ring_tx_db(struct adapter * adapter,struct sge_txq * tq,int n)964c6e0d914SCasey Leedom static inline void ring_tx_db(struct adapter *adapter, struct sge_txq *tq,
965c6e0d914SCasey Leedom 			      int n)
966c6e0d914SCasey Leedom {
967df64e4d3SHariprasad Shenai 	/* Make sure that all writes to the TX Descriptors are committed
968df64e4d3SHariprasad Shenai 	 * before we tell the hardware about them.
969c6e0d914SCasey Leedom 	 */
970c6e0d914SCasey Leedom 	wmb();
971df64e4d3SHariprasad Shenai 
972df64e4d3SHariprasad Shenai 	/* If we don't have access to the new User Doorbell (T5+), use the old
973df64e4d3SHariprasad Shenai 	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
974df64e4d3SHariprasad Shenai 	 */
975df64e4d3SHariprasad Shenai 	if (unlikely(tq->bar2_addr == NULL)) {
976f612b815SHariprasad Shenai 		u32 val = PIDX_V(n);
977df64e4d3SHariprasad Shenai 
978c6e0d914SCasey Leedom 		t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
979f612b815SHariprasad Shenai 			     QID_V(tq->cntxt_id) | val);
980df64e4d3SHariprasad Shenai 	} else {
981f612b815SHariprasad Shenai 		u32 val = PIDX_T5_V(n);
982df64e4d3SHariprasad Shenai 
983df64e4d3SHariprasad Shenai 		/* T4 and later chips share the same PIDX field offset within
984df64e4d3SHariprasad Shenai 		 * the doorbell, but T5 and later shrank the field in order to
985df64e4d3SHariprasad Shenai 		 * gain a bit for Doorbell Priority.  The field was absurdly
986df64e4d3SHariprasad Shenai 		 * large in the first place (14 bits) so we just use the T5
987df64e4d3SHariprasad Shenai 		 * and later limits and warn if a Queue ID is too large.
988df64e4d3SHariprasad Shenai 		 */
989f612b815SHariprasad Shenai 		WARN_ON(val & DBPRIO_F);
990df64e4d3SHariprasad Shenai 
991df64e4d3SHariprasad Shenai 		/* If we're only writing a single Egress Unit and the BAR2
992df64e4d3SHariprasad Shenai 		 * Queue ID is 0, we can use the Write Combining Doorbell
993df64e4d3SHariprasad Shenai 		 * Gather Buffer; otherwise we use the simple doorbell.
994df64e4d3SHariprasad Shenai 		 */
995df64e4d3SHariprasad Shenai 		if (n == 1 && tq->bar2_qid == 0) {
996df64e4d3SHariprasad Shenai 			unsigned int index = (tq->pidx
997df64e4d3SHariprasad Shenai 					      ? (tq->pidx - 1)
998df64e4d3SHariprasad Shenai 					      : (tq->size - 1));
999df64e4d3SHariprasad Shenai 			__be64 *src = (__be64 *)&tq->desc[index];
10002ff2acf1SHariprasad Shenai 			__be64 __iomem *dst = (__be64 __iomem *)(tq->bar2_addr +
1001df64e4d3SHariprasad Shenai 							 SGE_UDB_WCDOORBELL);
1002df64e4d3SHariprasad Shenai 			unsigned int count = EQ_UNIT / sizeof(__be64);
1003df64e4d3SHariprasad Shenai 
1004df64e4d3SHariprasad Shenai 			/* Copy the TX Descriptor in a tight loop in order to
1005df64e4d3SHariprasad Shenai 			 * try to get it to the adapter in a single Write
1006df64e4d3SHariprasad Shenai 			 * Combined transfer on the PCI-E Bus.  If the Write
1007df64e4d3SHariprasad Shenai 			 * Combine fails (say because of an interrupt, etc.)
1008df64e4d3SHariprasad Shenai 			 * the hardware will simply take the last write as a
1009df64e4d3SHariprasad Shenai 			 * simple doorbell write with a PIDX Increment of 1
1010df64e4d3SHariprasad Shenai 			 * and will fetch the TX Descriptor from memory via
1011df64e4d3SHariprasad Shenai 			 * DMA.
1012df64e4d3SHariprasad Shenai 			 */
1013df64e4d3SHariprasad Shenai 			while (count) {
10142ff2acf1SHariprasad Shenai 				/* the (__force u64) is because the compiler
10152ff2acf1SHariprasad Shenai 				 * doesn't understand the endian swizzling
10162ff2acf1SHariprasad Shenai 				 * going on
10172ff2acf1SHariprasad Shenai 				 */
10182ff2acf1SHariprasad Shenai 				writeq((__force u64)*src, dst);
1019df64e4d3SHariprasad Shenai 				src++;
1020df64e4d3SHariprasad Shenai 				dst++;
1021df64e4d3SHariprasad Shenai 				count--;
1022df64e4d3SHariprasad Shenai 			}
1023df64e4d3SHariprasad Shenai 		} else
1024f612b815SHariprasad Shenai 			writel(val | QID_V(tq->bar2_qid),
1025df64e4d3SHariprasad Shenai 			       tq->bar2_addr + SGE_UDB_KDOORBELL);
1026df64e4d3SHariprasad Shenai 
1027df64e4d3SHariprasad Shenai 		/* This Write Memory Barrier will force the write to the User
1028df64e4d3SHariprasad Shenai 		 * Doorbell area to be flushed.  This is needed to prevent
1029df64e4d3SHariprasad Shenai 		 * writes on different CPUs for the same queue from hitting
1030df64e4d3SHariprasad Shenai 		 * the adapter out of order.  This is required when some Work
1031df64e4d3SHariprasad Shenai 		 * Requests take the Write Combine Gather Buffer path (user
1032df64e4d3SHariprasad Shenai 		 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
1033df64e4d3SHariprasad Shenai 		 * take the traditional path where we simply increment the
1034df64e4d3SHariprasad Shenai 		 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
1035df64e4d3SHariprasad Shenai 		 * hardware DMA read the actual Work Request.
1036df64e4d3SHariprasad Shenai 		 */
1037df64e4d3SHariprasad Shenai 		wmb();
1038df64e4d3SHariprasad Shenai 	}
1039c6e0d914SCasey Leedom }
1040c6e0d914SCasey Leedom 
1041c6e0d914SCasey Leedom /**
1042c6e0d914SCasey Leedom  *	inline_tx_skb - inline a packet's data into TX descriptors
1043c6e0d914SCasey Leedom  *	@skb: the packet
1044c6e0d914SCasey Leedom  *	@tq: the TX queue where the packet will be inlined
1045c6e0d914SCasey Leedom  *	@pos: starting position in the TX queue to inline the packet
1046c6e0d914SCasey Leedom  *
1047c6e0d914SCasey Leedom  *	Inline a packet's contents directly into TX descriptors, starting at
1048c6e0d914SCasey Leedom  *	the given position within the TX DMA ring.
1049c6e0d914SCasey Leedom  *	Most of the complexity of this operation is dealing with wrap arounds
1050c6e0d914SCasey Leedom  *	in the middle of the packet we want to inline.
1051c6e0d914SCasey Leedom  */
inline_tx_skb(const struct sk_buff * skb,const struct sge_txq * tq,void * pos)1052c6e0d914SCasey Leedom static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *tq,
1053c6e0d914SCasey Leedom 			  void *pos)
1054c6e0d914SCasey Leedom {
1055c6e0d914SCasey Leedom 	u64 *p;
1056c6e0d914SCasey Leedom 	int left = (void *)tq->stat - pos;
1057c6e0d914SCasey Leedom 
1058c6e0d914SCasey Leedom 	if (likely(skb->len <= left)) {
1059c6e0d914SCasey Leedom 		if (likely(!skb->data_len))
1060c6e0d914SCasey Leedom 			skb_copy_from_linear_data(skb, pos, skb->len);
1061c6e0d914SCasey Leedom 		else
1062c6e0d914SCasey Leedom 			skb_copy_bits(skb, 0, pos, skb->len);
1063c6e0d914SCasey Leedom 		pos += skb->len;
1064c6e0d914SCasey Leedom 	} else {
1065c6e0d914SCasey Leedom 		skb_copy_bits(skb, 0, pos, left);
1066c6e0d914SCasey Leedom 		skb_copy_bits(skb, left, tq->desc, skb->len - left);
1067c6e0d914SCasey Leedom 		pos = (void *)tq->desc + (skb->len - left);
1068c6e0d914SCasey Leedom 	}
1069c6e0d914SCasey Leedom 
1070c6e0d914SCasey Leedom 	/* 0-pad to multiple of 16 */
1071c6e0d914SCasey Leedom 	p = PTR_ALIGN(pos, 8);
1072c6e0d914SCasey Leedom 	if ((uintptr_t)p & 8)
1073c6e0d914SCasey Leedom 		*p = 0;
1074c6e0d914SCasey Leedom }
1075c6e0d914SCasey Leedom 
1076c6e0d914SCasey Leedom /*
1077c6e0d914SCasey Leedom  * Figure out what HW csum a packet wants and return the appropriate control
1078c6e0d914SCasey Leedom  * bits.
1079c6e0d914SCasey Leedom  */
hwcsum(enum chip_type chip,const struct sk_buff * skb)108041fc2e41SHariprasad Shenai static u64 hwcsum(enum chip_type chip, const struct sk_buff *skb)
1081c6e0d914SCasey Leedom {
1082c6e0d914SCasey Leedom 	int csum_type;
1083c6e0d914SCasey Leedom 	const struct iphdr *iph = ip_hdr(skb);
1084c6e0d914SCasey Leedom 
1085c6e0d914SCasey Leedom 	if (iph->version == 4) {
1086c6e0d914SCasey Leedom 		if (iph->protocol == IPPROTO_TCP)
1087c6e0d914SCasey Leedom 			csum_type = TX_CSUM_TCPIP;
1088c6e0d914SCasey Leedom 		else if (iph->protocol == IPPROTO_UDP)
1089c6e0d914SCasey Leedom 			csum_type = TX_CSUM_UDPIP;
1090c6e0d914SCasey Leedom 		else {
1091c6e0d914SCasey Leedom nocsum:
1092c6e0d914SCasey Leedom 			/*
1093c6e0d914SCasey Leedom 			 * unknown protocol, disable HW csum
1094c6e0d914SCasey Leedom 			 * and hope a bad packet is detected
1095c6e0d914SCasey Leedom 			 */
10961ecc7b7aSHariprasad Shenai 			return TXPKT_L4CSUM_DIS_F;
1097c6e0d914SCasey Leedom 		}
1098c6e0d914SCasey Leedom 	} else {
1099c6e0d914SCasey Leedom 		/*
1100c6e0d914SCasey Leedom 		 * this doesn't work with extension headers
1101c6e0d914SCasey Leedom 		 */
1102c6e0d914SCasey Leedom 		const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
1103c6e0d914SCasey Leedom 
1104c6e0d914SCasey Leedom 		if (ip6h->nexthdr == IPPROTO_TCP)
1105c6e0d914SCasey Leedom 			csum_type = TX_CSUM_TCPIP6;
1106c6e0d914SCasey Leedom 		else if (ip6h->nexthdr == IPPROTO_UDP)
1107c6e0d914SCasey Leedom 			csum_type = TX_CSUM_UDPIP6;
1108c6e0d914SCasey Leedom 		else
1109c6e0d914SCasey Leedom 			goto nocsum;
1110c6e0d914SCasey Leedom 	}
1111c6e0d914SCasey Leedom 
111241fc2e41SHariprasad Shenai 	if (likely(csum_type >= TX_CSUM_TCPIP)) {
111341fc2e41SHariprasad Shenai 		u64 hdr_len = TXPKT_IPHDR_LEN_V(skb_network_header_len(skb));
111441fc2e41SHariprasad Shenai 		int eth_hdr_len = skb_network_offset(skb) - ETH_HLEN;
111541fc2e41SHariprasad Shenai 
111641fc2e41SHariprasad Shenai 		if (chip <= CHELSIO_T5)
111741fc2e41SHariprasad Shenai 			hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len);
111841fc2e41SHariprasad Shenai 		else
111941fc2e41SHariprasad Shenai 			hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len);
112041fc2e41SHariprasad Shenai 		return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len;
112141fc2e41SHariprasad Shenai 	} else {
1122c6e0d914SCasey Leedom 		int start = skb_transport_offset(skb);
1123c6e0d914SCasey Leedom 
11241ecc7b7aSHariprasad Shenai 		return TXPKT_CSUM_TYPE_V(csum_type) |
11251ecc7b7aSHariprasad Shenai 			TXPKT_CSUM_START_V(start) |
11261ecc7b7aSHariprasad Shenai 			TXPKT_CSUM_LOC_V(start + skb->csum_offset);
1127c6e0d914SCasey Leedom 	}
1128c6e0d914SCasey Leedom }
1129c6e0d914SCasey Leedom 
1130c6e0d914SCasey Leedom /*
1131c6e0d914SCasey Leedom  * Stop an Ethernet TX queue and record that state change.
1132c6e0d914SCasey Leedom  */
txq_stop(struct sge_eth_txq * txq)1133c6e0d914SCasey Leedom static void txq_stop(struct sge_eth_txq *txq)
1134c6e0d914SCasey Leedom {
1135c6e0d914SCasey Leedom 	netif_tx_stop_queue(txq->txq);
1136c6e0d914SCasey Leedom 	txq->q.stops++;
1137c6e0d914SCasey Leedom }
1138c6e0d914SCasey Leedom 
1139c6e0d914SCasey Leedom /*
1140c6e0d914SCasey Leedom  * Advance our software state for a TX queue by adding n in use descriptors.
1141c6e0d914SCasey Leedom  */
txq_advance(struct sge_txq * tq,unsigned int n)1142c6e0d914SCasey Leedom static inline void txq_advance(struct sge_txq *tq, unsigned int n)
1143c6e0d914SCasey Leedom {
1144c6e0d914SCasey Leedom 	tq->in_use += n;
1145c6e0d914SCasey Leedom 	tq->pidx += n;
1146c6e0d914SCasey Leedom 	if (tq->pidx >= tq->size)
1147c6e0d914SCasey Leedom 		tq->pidx -= tq->size;
1148c6e0d914SCasey Leedom }
1149c6e0d914SCasey Leedom 
1150c6e0d914SCasey Leedom /**
1151c6e0d914SCasey Leedom  *	t4vf_eth_xmit - add a packet to an Ethernet TX queue
1152c6e0d914SCasey Leedom  *	@skb: the packet
1153c6e0d914SCasey Leedom  *	@dev: the egress net device
1154c6e0d914SCasey Leedom  *
1155c6e0d914SCasey Leedom  *	Add a packet to an SGE Ethernet TX queue.  Runs with softirqs disabled.
1156c6e0d914SCasey Leedom  */
t4vf_eth_xmit(struct sk_buff * skb,struct net_device * dev)11572a784784SLuc Van Oostenryck netdev_tx_t t4vf_eth_xmit(struct sk_buff *skb, struct net_device *dev)
1158c6e0d914SCasey Leedom {
11597f9dd2faSCasey Leedom 	u32 wr_mid;
1160c6e0d914SCasey Leedom 	u64 cntrl, *end;
1161637d3e99SHariprasad Shenai 	int qidx, credits, max_pkt_len;
1162c6e0d914SCasey Leedom 	unsigned int flits, ndesc;
1163c6e0d914SCasey Leedom 	struct adapter *adapter;
1164c6e0d914SCasey Leedom 	struct sge_eth_txq *txq;
1165c6e0d914SCasey Leedom 	const struct port_info *pi;
1166c6e0d914SCasey Leedom 	struct fw_eth_tx_pkt_vm_wr *wr;
1167c6e0d914SCasey Leedom 	struct cpl_tx_pkt_core *cpl;
1168c6e0d914SCasey Leedom 	const struct skb_shared_info *ssi;
1169c6e0d914SCasey Leedom 	dma_addr_t addr[MAX_SKB_FRAGS + 1];
1170641d3ef0SKees Cook 	const size_t fw_hdr_copy_len = sizeof(wr->firmware);
1171c6e0d914SCasey Leedom 
1172c6e0d914SCasey Leedom 	/*
1173c6e0d914SCasey Leedom 	 * The chip minimum packet length is 10 octets but the firmware
1174c6e0d914SCasey Leedom 	 * command that we are using requires that we copy the Ethernet header
1175c6e0d914SCasey Leedom 	 * (including the VLAN tag) into the header so we reject anything
1176c6e0d914SCasey Leedom 	 * smaller than that ...
1177c6e0d914SCasey Leedom 	 */
1178c6e0d914SCasey Leedom 	if (unlikely(skb->len < fw_hdr_copy_len))
1179c6e0d914SCasey Leedom 		goto out_free;
1180c6e0d914SCasey Leedom 
1181637d3e99SHariprasad Shenai 	/* Discard the packet if the length is greater than mtu */
1182637d3e99SHariprasad Shenai 	max_pkt_len = ETH_HLEN + dev->mtu;
11838d09e6b8SHariprasad Shenai 	if (skb_vlan_tagged(skb))
1184637d3e99SHariprasad Shenai 		max_pkt_len += VLAN_HLEN;
1185637d3e99SHariprasad Shenai 	if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
1186637d3e99SHariprasad Shenai 		goto out_free;
1187637d3e99SHariprasad Shenai 
1188c6e0d914SCasey Leedom 	/*
1189c6e0d914SCasey Leedom 	 * Figure out which TX Queue we're going to use.
1190c6e0d914SCasey Leedom 	 */
1191c6e0d914SCasey Leedom 	pi = netdev_priv(dev);
1192c6e0d914SCasey Leedom 	adapter = pi->adapter;
1193c6e0d914SCasey Leedom 	qidx = skb_get_queue_mapping(skb);
1194c6e0d914SCasey Leedom 	BUG_ON(qidx >= pi->nqsets);
1195c6e0d914SCasey Leedom 	txq = &adapter->sge.ethtxq[pi->first_qset + qidx];
1196c6e0d914SCasey Leedom 
11979d5fd927SGanesh Goudar 	if (pi->vlan_id && !skb_vlan_tag_present(skb))
11989d5fd927SGanesh Goudar 		__vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q),
11999d5fd927SGanesh Goudar 				       pi->vlan_id);
12009d5fd927SGanesh Goudar 
1201c6e0d914SCasey Leedom 	/*
1202c6e0d914SCasey Leedom 	 * Take this opportunity to reclaim any TX Descriptors whose DMA
1203c6e0d914SCasey Leedom 	 * transfers have completed.
1204c6e0d914SCasey Leedom 	 */
1205c6e0d914SCasey Leedom 	reclaim_completed_tx(adapter, &txq->q, true);
1206c6e0d914SCasey Leedom 
1207c6e0d914SCasey Leedom 	/*
1208c6e0d914SCasey Leedom 	 * Calculate the number of flits and TX Descriptors we're going to
1209c6e0d914SCasey Leedom 	 * need along with how many TX Descriptors will be left over after
1210c6e0d914SCasey Leedom 	 * we inject our Work Request.
1211c6e0d914SCasey Leedom 	 */
1212c6e0d914SCasey Leedom 	flits = calc_tx_flits(skb);
1213c6e0d914SCasey Leedom 	ndesc = flits_to_desc(flits);
1214c6e0d914SCasey Leedom 	credits = txq_avail(&txq->q) - ndesc;
1215c6e0d914SCasey Leedom 
1216c6e0d914SCasey Leedom 	if (unlikely(credits < 0)) {
1217c6e0d914SCasey Leedom 		/*
1218c6e0d914SCasey Leedom 		 * Not enough room for this packet's Work Request.  Stop the
1219c6e0d914SCasey Leedom 		 * TX Queue and return a "busy" condition.  The queue will get
1220c6e0d914SCasey Leedom 		 * started later on when the firmware informs us that space
1221c6e0d914SCasey Leedom 		 * has opened up.
1222c6e0d914SCasey Leedom 		 */
1223c6e0d914SCasey Leedom 		txq_stop(txq);
1224c6e0d914SCasey Leedom 		dev_err(adapter->pdev_dev,
1225c6e0d914SCasey Leedom 			"%s: TX ring %u full while queue awake!\n",
1226c6e0d914SCasey Leedom 			dev->name, qidx);
1227c6e0d914SCasey Leedom 		return NETDEV_TX_BUSY;
1228c6e0d914SCasey Leedom 	}
1229c6e0d914SCasey Leedom 
1230c6e0d914SCasey Leedom 	if (!is_eth_imm(skb) &&
1231c6e0d914SCasey Leedom 	    unlikely(map_skb(adapter->pdev_dev, skb, addr) < 0)) {
1232c6e0d914SCasey Leedom 		/*
1233c6e0d914SCasey Leedom 		 * We need to map the skb into PCI DMA space (because it can't
1234c6e0d914SCasey Leedom 		 * be in-lined directly into the Work Request) and the mapping
1235c6e0d914SCasey Leedom 		 * operation failed.  Record the error and drop the packet.
1236c6e0d914SCasey Leedom 		 */
1237c6e0d914SCasey Leedom 		txq->mapping_err++;
1238c6e0d914SCasey Leedom 		goto out_free;
1239c6e0d914SCasey Leedom 	}
1240c6e0d914SCasey Leedom 
1241e2ac9628SHariprasad Shenai 	wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
1242c6e0d914SCasey Leedom 	if (unlikely(credits < ETHTXQ_STOP_THRES)) {
1243c6e0d914SCasey Leedom 		/*
1244c6e0d914SCasey Leedom 		 * After we're done injecting the Work Request for this
124525985edcSLucas De Marchi 		 * packet, we'll be below our "stop threshold" so stop the TX
12467f9dd2faSCasey Leedom 		 * Queue now and schedule a request for an SGE Egress Queue
12477f9dd2faSCasey Leedom 		 * Update message.  The queue will get started later on when
12487f9dd2faSCasey Leedom 		 * the firmware processes this Work Request and sends us an
12497f9dd2faSCasey Leedom 		 * Egress Queue Status Update message indicating that space
12507f9dd2faSCasey Leedom 		 * has opened up.
1251c6e0d914SCasey Leedom 		 */
1252c6e0d914SCasey Leedom 		txq_stop(txq);
1253e2ac9628SHariprasad Shenai 		wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
1254c6e0d914SCasey Leedom 	}
1255c6e0d914SCasey Leedom 
1256c6e0d914SCasey Leedom 	/*
1257c6e0d914SCasey Leedom 	 * Start filling in our Work Request.  Note that we do _not_ handle
1258c6e0d914SCasey Leedom 	 * the WR Header wrapping around the TX Descriptor Ring.  If our
1259c6e0d914SCasey Leedom 	 * maximum header size ever exceeds one TX Descriptor, we'll need to
1260c6e0d914SCasey Leedom 	 * do something else here.
1261c6e0d914SCasey Leedom 	 */
1262c6e0d914SCasey Leedom 	BUG_ON(DIV_ROUND_UP(ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1);
1263c6e0d914SCasey Leedom 	wr = (void *)&txq->q.desc[txq->q.pidx];
12647f9dd2faSCasey Leedom 	wr->equiq_to_len16 = cpu_to_be32(wr_mid);
12652ff2acf1SHariprasad Shenai 	wr->r3[0] = cpu_to_be32(0);
12662ff2acf1SHariprasad Shenai 	wr->r3[1] = cpu_to_be32(0);
1267641d3ef0SKees Cook 	skb_copy_from_linear_data(skb, &wr->firmware, fw_hdr_copy_len);
1268c6e0d914SCasey Leedom 	end = (u64 *)wr + flits;
1269c6e0d914SCasey Leedom 
1270c6e0d914SCasey Leedom 	/*
1271c6e0d914SCasey Leedom 	 * If this is a Large Send Offload packet we'll put in an LSO CPL
1272c6e0d914SCasey Leedom 	 * message with an encapsulated TX Packet CPL message.  Otherwise we
1273c6e0d914SCasey Leedom 	 * just use a TX Packet CPL message.
1274c6e0d914SCasey Leedom 	 */
1275c6e0d914SCasey Leedom 	ssi = skb_shinfo(skb);
1276c6e0d914SCasey Leedom 	if (ssi->gso_size) {
1277c6e0d914SCasey Leedom 		struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
1278c6e0d914SCasey Leedom 		bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
1279c6e0d914SCasey Leedom 		int l3hdr_len = skb_network_header_len(skb);
1280c6e0d914SCasey Leedom 		int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
1281c6e0d914SCasey Leedom 
1282c6e0d914SCasey Leedom 		wr->op_immdlen =
1283e2ac9628SHariprasad Shenai 			cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
1284e2ac9628SHariprasad Shenai 				    FW_WR_IMMDLEN_V(sizeof(*lso) +
1285c6e0d914SCasey Leedom 						    sizeof(*cpl)));
1286c6e0d914SCasey Leedom 		/*
1287c6e0d914SCasey Leedom 		 * Fill in the LSO CPL message.
1288c6e0d914SCasey Leedom 		 */
1289c6e0d914SCasey Leedom 		lso->lso_ctrl =
12901ecc7b7aSHariprasad Shenai 			cpu_to_be32(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
12911ecc7b7aSHariprasad Shenai 				    LSO_FIRST_SLICE_F |
12921ecc7b7aSHariprasad Shenai 				    LSO_LAST_SLICE_F |
12931ecc7b7aSHariprasad Shenai 				    LSO_IPV6_V(v6) |
12941ecc7b7aSHariprasad Shenai 				    LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
12951ecc7b7aSHariprasad Shenai 				    LSO_IPHDR_LEN_V(l3hdr_len / 4) |
12961ecc7b7aSHariprasad Shenai 				    LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
1297c6e0d914SCasey Leedom 		lso->ipid_ofst = cpu_to_be16(0);
1298c6e0d914SCasey Leedom 		lso->mss = cpu_to_be16(ssi->gso_size);
1299c6e0d914SCasey Leedom 		lso->seqno_offset = cpu_to_be32(0);
13007207c0d1SHariprasad Shenai 		if (is_t4(adapter->params.chip))
1301c6e0d914SCasey Leedom 			lso->len = cpu_to_be32(skb->len);
13027207c0d1SHariprasad Shenai 		else
13031ecc7b7aSHariprasad Shenai 			lso->len = cpu_to_be32(LSO_T5_XFER_SIZE_V(skb->len));
1304c6e0d914SCasey Leedom 
1305c6e0d914SCasey Leedom 		/*
1306c6e0d914SCasey Leedom 		 * Set up TX Packet CPL pointer, control word and perform
1307c6e0d914SCasey Leedom 		 * accounting.
1308c6e0d914SCasey Leedom 		 */
1309c6e0d914SCasey Leedom 		cpl = (void *)(lso + 1);
131041fc2e41SHariprasad Shenai 
131141fc2e41SHariprasad Shenai 		if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
131241fc2e41SHariprasad Shenai 			cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
131341fc2e41SHariprasad Shenai 		else
131441fc2e41SHariprasad Shenai 			cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
131541fc2e41SHariprasad Shenai 
131641fc2e41SHariprasad Shenai 		cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
13171ecc7b7aSHariprasad Shenai 					   TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
131841fc2e41SHariprasad Shenai 			 TXPKT_IPHDR_LEN_V(l3hdr_len);
1319c6e0d914SCasey Leedom 		txq->tso++;
1320c6e0d914SCasey Leedom 		txq->tx_cso += ssi->gso_segs;
1321c6e0d914SCasey Leedom 	} else {
1322c6e0d914SCasey Leedom 		int len;
1323c6e0d914SCasey Leedom 
1324c6e0d914SCasey Leedom 		len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
1325c6e0d914SCasey Leedom 		wr->op_immdlen =
1326e2ac9628SHariprasad Shenai 			cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
1327e2ac9628SHariprasad Shenai 				    FW_WR_IMMDLEN_V(len));
1328c6e0d914SCasey Leedom 
1329c6e0d914SCasey Leedom 		/*
1330c6e0d914SCasey Leedom 		 * Set up TX Packet CPL pointer, control word and perform
1331c6e0d914SCasey Leedom 		 * accounting.
1332c6e0d914SCasey Leedom 		 */
1333c6e0d914SCasey Leedom 		cpl = (void *)(wr + 1);
1334c6e0d914SCasey Leedom 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
133541fc2e41SHariprasad Shenai 			cntrl = hwcsum(adapter->params.chip, skb) |
133641fc2e41SHariprasad Shenai 				TXPKT_IPCSUM_DIS_F;
1337c6e0d914SCasey Leedom 			txq->tx_cso++;
1338c6e0d914SCasey Leedom 		} else
13391ecc7b7aSHariprasad Shenai 			cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
1340c6e0d914SCasey Leedom 	}
1341c6e0d914SCasey Leedom 
1342c6e0d914SCasey Leedom 	/*
1343c6e0d914SCasey Leedom 	 * If there's a VLAN tag present, add that to the list of things to
1344c6e0d914SCasey Leedom 	 * do in this Work Request.
1345c6e0d914SCasey Leedom 	 */
1346df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
1347c6e0d914SCasey Leedom 		txq->vlan_ins++;
13481ecc7b7aSHariprasad Shenai 		cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
1349c6e0d914SCasey Leedom 	}
1350c6e0d914SCasey Leedom 
1351c6e0d914SCasey Leedom 	/*
1352c6e0d914SCasey Leedom 	 * Fill in the TX Packet CPL message header.
1353c6e0d914SCasey Leedom 	 */
13541ecc7b7aSHariprasad Shenai 	cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE_V(CPL_TX_PKT_XT) |
13551ecc7b7aSHariprasad Shenai 				 TXPKT_INTF_V(pi->port_id) |
13561ecc7b7aSHariprasad Shenai 				 TXPKT_PF_V(0));
1357c6e0d914SCasey Leedom 	cpl->pack = cpu_to_be16(0);
1358c6e0d914SCasey Leedom 	cpl->len = cpu_to_be16(skb->len);
1359c6e0d914SCasey Leedom 	cpl->ctrl1 = cpu_to_be64(cntrl);
1360c6e0d914SCasey Leedom 
1361c6e0d914SCasey Leedom #ifdef T4_TRACE
1362c6e0d914SCasey Leedom 	T4_TRACE5(adapter->tb[txq->q.cntxt_id & 7],
1363c6e0d914SCasey Leedom 		  "eth_xmit: ndesc %u, credits %u, pidx %u, len %u, frags %u",
1364c6e0d914SCasey Leedom 		  ndesc, credits, txq->q.pidx, skb->len, ssi->nr_frags);
1365c6e0d914SCasey Leedom #endif
1366c6e0d914SCasey Leedom 
1367c6e0d914SCasey Leedom 	/*
1368c6e0d914SCasey Leedom 	 * Fill in the body of the TX Packet CPL message with either in-lined
1369c6e0d914SCasey Leedom 	 * data or a Scatter/Gather List.
1370c6e0d914SCasey Leedom 	 */
1371c6e0d914SCasey Leedom 	if (is_eth_imm(skb)) {
1372c6e0d914SCasey Leedom 		/*
1373c6e0d914SCasey Leedom 		 * In-line the packet's data and free the skb since we don't
1374c6e0d914SCasey Leedom 		 * need it any longer.
1375c6e0d914SCasey Leedom 		 */
1376c6e0d914SCasey Leedom 		inline_tx_skb(skb, &txq->q, cpl + 1);
137742ffda5fSEric W. Biederman 		dev_consume_skb_any(skb);
1378c6e0d914SCasey Leedom 	} else {
1379c6e0d914SCasey Leedom 		/*
1380c6e0d914SCasey Leedom 		 * Write the skb's Scatter/Gather list into the TX Packet CPL
1381c6e0d914SCasey Leedom 		 * message and retain a pointer to the skb so we can free it
1382c6e0d914SCasey Leedom 		 * later when its DMA completes.  (We store the skb pointer
1383c6e0d914SCasey Leedom 		 * in the Software Descriptor corresponding to the last TX
1384c6e0d914SCasey Leedom 		 * Descriptor used by the Work Request.)
1385c6e0d914SCasey Leedom 		 *
1386c6e0d914SCasey Leedom 		 * The retained skb will be freed when the corresponding TX
1387c6e0d914SCasey Leedom 		 * Descriptors are reclaimed after their DMAs complete.
1388c6e0d914SCasey Leedom 		 * However, this could take quite a while since, in general,
1389c6e0d914SCasey Leedom 		 * the hardware is set up to be lazy about sending DMA
1390c6e0d914SCasey Leedom 		 * completion notifications to us and we mostly perform TX
1391c6e0d914SCasey Leedom 		 * reclaims in the transmit routine.
1392c6e0d914SCasey Leedom 		 *
1393c6e0d914SCasey Leedom 		 * This is good for performamce but means that we rely on new
1394c6e0d914SCasey Leedom 		 * TX packets arriving to run the destructors of completed
1395c6e0d914SCasey Leedom 		 * packets, which open up space in their sockets' send queues.
1396c6e0d914SCasey Leedom 		 * Sometimes we do not get such new packets causing TX to
1397c6e0d914SCasey Leedom 		 * stall.  A single UDP transmitter is a good example of this
1398c6e0d914SCasey Leedom 		 * situation.  We have a clean up timer that periodically
1399c6e0d914SCasey Leedom 		 * reclaims completed packets but it doesn't run often enough
1400c6e0d914SCasey Leedom 		 * (nor do we want it to) to prevent lengthy stalls.  A
1401c6e0d914SCasey Leedom 		 * solution to this problem is to run the destructor early,
1402c6e0d914SCasey Leedom 		 * after the packet is queued but before it's DMAd.  A con is
1403c6e0d914SCasey Leedom 		 * that we lie to socket memory accounting, but the amount of
1404c6e0d914SCasey Leedom 		 * extra memory is reasonable (limited by the number of TX
1405c6e0d914SCasey Leedom 		 * descriptors), the packets do actually get freed quickly by
1406c6e0d914SCasey Leedom 		 * new packets almost always, and for protocols like TCP that
1407c6e0d914SCasey Leedom 		 * wait for acks to really free up the data the extra memory
1408c6e0d914SCasey Leedom 		 * is even less.  On the positive side we run the destructors
1409c6e0d914SCasey Leedom 		 * on the sending CPU rather than on a potentially different
141064bb336cSCasey Leedom 		 * completing CPU, usually a good thing.
1411c6e0d914SCasey Leedom 		 *
1412c6e0d914SCasey Leedom 		 * Run the destructor before telling the DMA engine about the
1413c6e0d914SCasey Leedom 		 * packet to make sure it doesn't complete and get freed
1414c6e0d914SCasey Leedom 		 * prematurely.
1415c6e0d914SCasey Leedom 		 */
1416c6e0d914SCasey Leedom 		struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1);
1417c6e0d914SCasey Leedom 		struct sge_txq *tq = &txq->q;
1418c6e0d914SCasey Leedom 		int last_desc;
1419c6e0d914SCasey Leedom 
1420c6e0d914SCasey Leedom 		/*
1421c6e0d914SCasey Leedom 		 * If the Work Request header was an exact multiple of our TX
1422c6e0d914SCasey Leedom 		 * Descriptor length, then it's possible that the starting SGL
1423c6e0d914SCasey Leedom 		 * pointer lines up exactly with the end of our TX Descriptor
1424c6e0d914SCasey Leedom 		 * ring.  If that's the case, wrap around to the beginning
1425c6e0d914SCasey Leedom 		 * here ...
1426c6e0d914SCasey Leedom 		 */
1427c6e0d914SCasey Leedom 		if (unlikely((void *)sgl == (void *)tq->stat)) {
1428c6e0d914SCasey Leedom 			sgl = (void *)tq->desc;
142964699336SJoe Perches 			end = ((void *)tq->desc + ((void *)end - (void *)tq->stat));
1430c6e0d914SCasey Leedom 		}
1431c6e0d914SCasey Leedom 
1432c6e0d914SCasey Leedom 		write_sgl(skb, tq, sgl, end, 0, addr);
1433c6e0d914SCasey Leedom 		skb_orphan(skb);
1434c6e0d914SCasey Leedom 
1435c6e0d914SCasey Leedom 		last_desc = tq->pidx + ndesc - 1;
1436c6e0d914SCasey Leedom 		if (last_desc >= tq->size)
1437c6e0d914SCasey Leedom 			last_desc -= tq->size;
1438c6e0d914SCasey Leedom 		tq->sdesc[last_desc].skb = skb;
1439c6e0d914SCasey Leedom 		tq->sdesc[last_desc].sgl = sgl;
1440c6e0d914SCasey Leedom 	}
1441c6e0d914SCasey Leedom 
1442c6e0d914SCasey Leedom 	/*
1443c6e0d914SCasey Leedom 	 * Advance our internal TX Queue state, tell the hardware about
1444c6e0d914SCasey Leedom 	 * the new TX descriptors and return success.
1445c6e0d914SCasey Leedom 	 */
1446c6e0d914SCasey Leedom 	txq_advance(&txq->q, ndesc);
1447860e9538SFlorian Westphal 	netif_trans_update(dev);
1448c6e0d914SCasey Leedom 	ring_tx_db(adapter, &txq->q, ndesc);
1449c6e0d914SCasey Leedom 	return NETDEV_TX_OK;
1450c6e0d914SCasey Leedom 
1451c6e0d914SCasey Leedom out_free:
1452c6e0d914SCasey Leedom 	/*
1453c6e0d914SCasey Leedom 	 * An error of some sort happened.  Free the TX skb and tell the
1454c6e0d914SCasey Leedom 	 * OS that we've "dealt" with the packet ...
1455c6e0d914SCasey Leedom 	 */
145642ffda5fSEric W. Biederman 	dev_kfree_skb_any(skb);
1457c6e0d914SCasey Leedom 	return NETDEV_TX_OK;
1458c6e0d914SCasey Leedom }
1459c6e0d914SCasey Leedom 
1460c6e0d914SCasey Leedom /**
1461a0006a86SIan Campbell  *	copy_frags - copy fragments from gather list into skb_shared_info
1462a0006a86SIan Campbell  *	@skb: destination skb
1463a0006a86SIan Campbell  *	@gl: source internal packet gather list
1464a0006a86SIan Campbell  *	@offset: packet start offset in first page
1465a0006a86SIan Campbell  *
1466a0006a86SIan Campbell  *	Copy an internal packet gather list into a Linux skb_shared_info
1467a0006a86SIan Campbell  *	structure.
1468a0006a86SIan Campbell  */
copy_frags(struct sk_buff * skb,const struct pkt_gl * gl,unsigned int offset)1469a0006a86SIan Campbell static inline void copy_frags(struct sk_buff *skb,
1470a0006a86SIan Campbell 			      const struct pkt_gl *gl,
1471a0006a86SIan Campbell 			      unsigned int offset)
1472a0006a86SIan Campbell {
1473a0006a86SIan Campbell 	int i;
1474a0006a86SIan Campbell 
1475a0006a86SIan Campbell 	/* usually there's just one frag */
1476a0006a86SIan Campbell 	__skb_fill_page_desc(skb, 0, gl->frags[0].page,
1477a0006a86SIan Campbell 			     gl->frags[0].offset + offset,
1478a0006a86SIan Campbell 			     gl->frags[0].size - offset);
1479a0006a86SIan Campbell 	skb_shinfo(skb)->nr_frags = gl->nfrags;
1480a0006a86SIan Campbell 	for (i = 1; i < gl->nfrags; i++)
1481a0006a86SIan Campbell 		__skb_fill_page_desc(skb, i, gl->frags[i].page,
1482a0006a86SIan Campbell 				     gl->frags[i].offset,
1483a0006a86SIan Campbell 				     gl->frags[i].size);
1484a0006a86SIan Campbell 
1485a0006a86SIan Campbell 	/* get a reference to the last page, we don't own it */
1486a0006a86SIan Campbell 	get_page(gl->frags[gl->nfrags - 1].page);
1487a0006a86SIan Campbell }
1488a0006a86SIan Campbell 
1489a0006a86SIan Campbell /**
1490eb6c503dSCasey Leedom  *	t4vf_pktgl_to_skb - build an sk_buff from a packet gather list
1491eb6c503dSCasey Leedom  *	@gl: the gather list
1492eb6c503dSCasey Leedom  *	@skb_len: size of sk_buff main body if it carries fragments
1493eb6c503dSCasey Leedom  *	@pull_len: amount of data to move to the sk_buff's main body
1494eb6c503dSCasey Leedom  *
1495eb6c503dSCasey Leedom  *	Builds an sk_buff from the given packet gather list.  Returns the
1496eb6c503dSCasey Leedom  *	sk_buff or %NULL if sk_buff allocation failed.
1497eb6c503dSCasey Leedom  */
t4vf_pktgl_to_skb(const struct pkt_gl * gl,unsigned int skb_len,unsigned int pull_len)14988a67d1c6SSachin Kamat static struct sk_buff *t4vf_pktgl_to_skb(const struct pkt_gl *gl,
14998a67d1c6SSachin Kamat 					 unsigned int skb_len,
15008a67d1c6SSachin Kamat 					 unsigned int pull_len)
1501eb6c503dSCasey Leedom {
1502eb6c503dSCasey Leedom 	struct sk_buff *skb;
1503eb6c503dSCasey Leedom 
1504eb6c503dSCasey Leedom 	/*
1505eb6c503dSCasey Leedom 	 * If the ingress packet is small enough, allocate an skb large enough
1506eb6c503dSCasey Leedom 	 * for all of the data and copy it inline.  Otherwise, allocate an skb
1507eb6c503dSCasey Leedom 	 * with enough room to pull in the header and reference the rest of
1508eb6c503dSCasey Leedom 	 * the data via the skb fragment list.
1509eb6c503dSCasey Leedom 	 *
1510eb6c503dSCasey Leedom 	 * Below we rely on RX_COPY_THRES being less than the smallest Rx
1511eb6c503dSCasey Leedom 	 * buff!  size, which is expected since buffers are at least
1512eb6c503dSCasey Leedom 	 * PAGE_SIZEd.  In this case packets up to RX_COPY_THRES have only one
1513eb6c503dSCasey Leedom 	 * fragment.
1514eb6c503dSCasey Leedom 	 */
1515eb6c503dSCasey Leedom 	if (gl->tot_len <= RX_COPY_THRES) {
1516eb6c503dSCasey Leedom 		/* small packets have only one fragment */
1517eb6c503dSCasey Leedom 		skb = alloc_skb(gl->tot_len, GFP_ATOMIC);
1518eb6c503dSCasey Leedom 		if (unlikely(!skb))
1519eb6c503dSCasey Leedom 			goto out;
1520eb6c503dSCasey Leedom 		__skb_put(skb, gl->tot_len);
1521eb6c503dSCasey Leedom 		skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
1522eb6c503dSCasey Leedom 	} else {
1523eb6c503dSCasey Leedom 		skb = alloc_skb(skb_len, GFP_ATOMIC);
1524eb6c503dSCasey Leedom 		if (unlikely(!skb))
1525eb6c503dSCasey Leedom 			goto out;
1526eb6c503dSCasey Leedom 		__skb_put(skb, pull_len);
1527eb6c503dSCasey Leedom 		skb_copy_to_linear_data(skb, gl->va, pull_len);
1528eb6c503dSCasey Leedom 
1529a0006a86SIan Campbell 		copy_frags(skb, gl, pull_len);
1530eb6c503dSCasey Leedom 		skb->len = gl->tot_len;
1531eb6c503dSCasey Leedom 		skb->data_len = skb->len - pull_len;
1532eb6c503dSCasey Leedom 		skb->truesize += skb->data_len;
1533eb6c503dSCasey Leedom 	}
1534eb6c503dSCasey Leedom 
1535eb6c503dSCasey Leedom out:
1536eb6c503dSCasey Leedom 	return skb;
1537eb6c503dSCasey Leedom }
1538eb6c503dSCasey Leedom 
1539eb6c503dSCasey Leedom /**
1540c6e0d914SCasey Leedom  *	t4vf_pktgl_free - free a packet gather list
1541c6e0d914SCasey Leedom  *	@gl: the gather list
1542c6e0d914SCasey Leedom  *
1543c6e0d914SCasey Leedom  *	Releases the pages of a packet gather list.  We do not own the last
1544c6e0d914SCasey Leedom  *	page on the list and do not free it.
1545c6e0d914SCasey Leedom  */
t4vf_pktgl_free(const struct pkt_gl * gl)15468a67d1c6SSachin Kamat static void t4vf_pktgl_free(const struct pkt_gl *gl)
1547c6e0d914SCasey Leedom {
1548c6e0d914SCasey Leedom 	int frag;
1549c6e0d914SCasey Leedom 
1550c6e0d914SCasey Leedom 	frag = gl->nfrags - 1;
1551c6e0d914SCasey Leedom 	while (frag--)
1552c6e0d914SCasey Leedom 		put_page(gl->frags[frag].page);
1553c6e0d914SCasey Leedom }
1554c6e0d914SCasey Leedom 
1555c6e0d914SCasey Leedom /**
1556c6e0d914SCasey Leedom  *	do_gro - perform Generic Receive Offload ingress packet processing
1557c6e0d914SCasey Leedom  *	@rxq: ingress RX Ethernet Queue
1558c6e0d914SCasey Leedom  *	@gl: gather list for ingress packet
1559c6e0d914SCasey Leedom  *	@pkt: CPL header for last packet fragment
1560c6e0d914SCasey Leedom  *
1561c6e0d914SCasey Leedom  *	Perform Generic Receive Offload (GRO) ingress packet processing.
1562c6e0d914SCasey Leedom  *	We use the standard Linux GRO interfaces for this.
1563c6e0d914SCasey Leedom  */
do_gro(struct sge_eth_rxq * rxq,const struct pkt_gl * gl,const struct cpl_rx_pkt * pkt)1564c6e0d914SCasey Leedom static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
1565c6e0d914SCasey Leedom 		   const struct cpl_rx_pkt *pkt)
1566c6e0d914SCasey Leedom {
156765f6ecc9SHariprasad Shenai 	struct adapter *adapter = rxq->rspq.adapter;
156865f6ecc9SHariprasad Shenai 	struct sge *s = &adapter->sge;
15699d5fd927SGanesh Goudar 	struct port_info *pi;
1570c6e0d914SCasey Leedom 	int ret;
1571c6e0d914SCasey Leedom 	struct sk_buff *skb;
1572c6e0d914SCasey Leedom 
1573c6e0d914SCasey Leedom 	skb = napi_get_frags(&rxq->rspq.napi);
1574c6e0d914SCasey Leedom 	if (unlikely(!skb)) {
1575c6e0d914SCasey Leedom 		t4vf_pktgl_free(gl);
1576c6e0d914SCasey Leedom 		rxq->stats.rx_drops++;
1577c6e0d914SCasey Leedom 		return;
1578c6e0d914SCasey Leedom 	}
1579c6e0d914SCasey Leedom 
158065f6ecc9SHariprasad Shenai 	copy_frags(skb, gl, s->pktshift);
158165f6ecc9SHariprasad Shenai 	skb->len = gl->tot_len - s->pktshift;
1582c6e0d914SCasey Leedom 	skb->data_len = skb->len;
1583c6e0d914SCasey Leedom 	skb->truesize += skb->data_len;
1584c6e0d914SCasey Leedom 	skb->ip_summed = CHECKSUM_UNNECESSARY;
1585c6e0d914SCasey Leedom 	skb_record_rx_queue(skb, rxq->rspq.idx);
15869d5fd927SGanesh Goudar 	pi = netdev_priv(skb->dev);
1587c6e0d914SCasey Leedom 
15889d5fd927SGanesh Goudar 	if (pkt->vlan_ex && !pi->vlan_id) {
158986a9bad3SPatrick McHardy 		__vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q),
159086a9bad3SPatrick McHardy 					be16_to_cpu(pkt->vlan));
1591af32de0eSVipul Pandya 		rxq->stats.vlan_ex++;
1592af32de0eSVipul Pandya 	}
1593c6e0d914SCasey Leedom 	ret = napi_gro_frags(&rxq->rspq.napi);
1594c6e0d914SCasey Leedom 
1595c6e0d914SCasey Leedom 	if (ret == GRO_HELD)
1596c6e0d914SCasey Leedom 		rxq->stats.lro_pkts++;
1597c6e0d914SCasey Leedom 	else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
1598c6e0d914SCasey Leedom 		rxq->stats.lro_merged++;
1599c6e0d914SCasey Leedom 	rxq->stats.pkts++;
1600c6e0d914SCasey Leedom 	rxq->stats.rx_cso++;
1601c6e0d914SCasey Leedom }
1602c6e0d914SCasey Leedom 
1603c6e0d914SCasey Leedom /**
1604c6e0d914SCasey Leedom  *	t4vf_ethrx_handler - process an ingress ethernet packet
1605c6e0d914SCasey Leedom  *	@rspq: the response queue that received the packet
1606c6e0d914SCasey Leedom  *	@rsp: the response queue descriptor holding the RX_PKT message
1607c6e0d914SCasey Leedom  *	@gl: the gather list of packet fragments
1608c6e0d914SCasey Leedom  *
1609c6e0d914SCasey Leedom  *	Process an ingress ethernet packet and deliver it to the stack.
1610c6e0d914SCasey Leedom  */
t4vf_ethrx_handler(struct sge_rspq * rspq,const __be64 * rsp,const struct pkt_gl * gl)1611c6e0d914SCasey Leedom int t4vf_ethrx_handler(struct sge_rspq *rspq, const __be64 *rsp,
1612c6e0d914SCasey Leedom 		       const struct pkt_gl *gl)
1613c6e0d914SCasey Leedom {
1614c6e0d914SCasey Leedom 	struct sk_buff *skb;
16158b9a4d56SVipul Pandya 	const struct cpl_rx_pkt *pkt = (void *)rsp;
1616c3136f55SHariprasad Shenai 	bool csum_ok = pkt->csum_calc && !pkt->err_vec &&
1617c3136f55SHariprasad Shenai 		       (rspq->netdev->features & NETIF_F_RXCSUM);
1618c6e0d914SCasey Leedom 	struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
161965f6ecc9SHariprasad Shenai 	struct adapter *adapter = rspq->adapter;
162065f6ecc9SHariprasad Shenai 	struct sge *s = &adapter->sge;
16219d5fd927SGanesh Goudar 	struct port_info *pi;
1622c6e0d914SCasey Leedom 
1623c6e0d914SCasey Leedom 	/*
1624c6e0d914SCasey Leedom 	 * If this is a good TCP packet and we have Generic Receive Offload
1625c6e0d914SCasey Leedom 	 * enabled, handle the packet in the GRO path.
1626c6e0d914SCasey Leedom 	 */
1627bdc590b9SHariprasad Shenai 	if ((pkt->l2info & cpu_to_be32(RXF_TCP_F)) &&
1628c6e0d914SCasey Leedom 	    (rspq->netdev->features & NETIF_F_GRO) && csum_ok &&
1629c6e0d914SCasey Leedom 	    !pkt->ip_frag) {
1630c6e0d914SCasey Leedom 		do_gro(rxq, gl, pkt);
1631c6e0d914SCasey Leedom 		return 0;
1632c6e0d914SCasey Leedom 	}
1633c6e0d914SCasey Leedom 
1634c6e0d914SCasey Leedom 	/*
1635eb6c503dSCasey Leedom 	 * Convert the Packet Gather List into an skb.
1636c6e0d914SCasey Leedom 	 */
1637eb6c503dSCasey Leedom 	skb = t4vf_pktgl_to_skb(gl, RX_SKB_LEN, RX_PULL_LEN);
1638eb6c503dSCasey Leedom 	if (unlikely(!skb)) {
1639eb6c503dSCasey Leedom 		t4vf_pktgl_free(gl);
1640eb6c503dSCasey Leedom 		rxq->stats.rx_drops++;
1641eb6c503dSCasey Leedom 		return 0;
1642c6e0d914SCasey Leedom 	}
164365f6ecc9SHariprasad Shenai 	__skb_pull(skb, s->pktshift);
1644c6e0d914SCasey Leedom 	skb->protocol = eth_type_trans(skb, rspq->netdev);
1645c6e0d914SCasey Leedom 	skb_record_rx_queue(skb, rspq->idx);
16469d5fd927SGanesh Goudar 	pi = netdev_priv(skb->dev);
1647c6e0d914SCasey Leedom 	rxq->stats.pkts++;
1648c6e0d914SCasey Leedom 
1649c3136f55SHariprasad Shenai 	if (csum_ok && !pkt->err_vec &&
1650bdc590b9SHariprasad Shenai 	    (be32_to_cpu(pkt->l2info) & (RXF_UDP_F | RXF_TCP_F))) {
16515400e54aSHariprasad Shenai 		if (!pkt->ip_frag) {
1652c6e0d914SCasey Leedom 			skb->ip_summed = CHECKSUM_UNNECESSARY;
16535400e54aSHariprasad Shenai 			rxq->stats.rx_cso++;
16545400e54aSHariprasad Shenai 		} else if (pkt->l2info & htonl(RXF_IP_F)) {
1655c6e0d914SCasey Leedom 			__sum16 c = (__force __sum16)pkt->csum;
1656c6e0d914SCasey Leedom 			skb->csum = csum_unfold(c);
1657c6e0d914SCasey Leedom 			skb->ip_summed = CHECKSUM_COMPLETE;
1658c6e0d914SCasey Leedom 			rxq->stats.rx_cso++;
16595400e54aSHariprasad Shenai 		}
1660c6e0d914SCasey Leedom 	} else
1661bc8acf2cSEric Dumazet 		skb_checksum_none_assert(skb);
1662c6e0d914SCasey Leedom 
16639d5fd927SGanesh Goudar 	if (pkt->vlan_ex && !pi->vlan_id) {
1664c6e0d914SCasey Leedom 		rxq->stats.vlan_ex++;
16659d5fd927SGanesh Goudar 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
16669d5fd927SGanesh Goudar 				       be16_to_cpu(pkt->vlan));
166787737663SJiri Pirko 	}
166887737663SJiri Pirko 
1669c6e0d914SCasey Leedom 	netif_receive_skb(skb);
1670c6e0d914SCasey Leedom 
1671c6e0d914SCasey Leedom 	return 0;
1672c6e0d914SCasey Leedom }
1673c6e0d914SCasey Leedom 
1674c6e0d914SCasey Leedom /**
1675c6e0d914SCasey Leedom  *	is_new_response - check if a response is newly written
1676c6e0d914SCasey Leedom  *	@rc: the response control descriptor
1677c6e0d914SCasey Leedom  *	@rspq: the response queue
1678c6e0d914SCasey Leedom  *
1679c6e0d914SCasey Leedom  *	Returns true if a response descriptor contains a yet unprocessed
1680c6e0d914SCasey Leedom  *	response.
1681c6e0d914SCasey Leedom  */
is_new_response(const struct rsp_ctrl * rc,const struct sge_rspq * rspq)1682c6e0d914SCasey Leedom static inline bool is_new_response(const struct rsp_ctrl *rc,
1683c6e0d914SCasey Leedom 				   const struct sge_rspq *rspq)
1684c6e0d914SCasey Leedom {
16851ecc7b7aSHariprasad Shenai 	return ((rc->type_gen >> RSPD_GEN_S) & 0x1) == rspq->gen;
1686c6e0d914SCasey Leedom }
1687c6e0d914SCasey Leedom 
1688c6e0d914SCasey Leedom /**
1689c6e0d914SCasey Leedom  *	restore_rx_bufs - put back a packet's RX buffers
1690c6e0d914SCasey Leedom  *	@gl: the packet gather list
1691c6e0d914SCasey Leedom  *	@fl: the SGE Free List
169220bb0c8fSRahul Lakkireddy  *	@frags: how many fragments in @si
1693c6e0d914SCasey Leedom  *
1694c6e0d914SCasey Leedom  *	Called when we find out that the current packet, @si, can't be
1695c6e0d914SCasey Leedom  *	processed right away for some reason.  This is a very rare event and
1696c6e0d914SCasey Leedom  *	there's no effort to make this suspension/resumption process
1697c6e0d914SCasey Leedom  *	particularly efficient.
1698c6e0d914SCasey Leedom  *
1699c6e0d914SCasey Leedom  *	We implement the suspension by putting all of the RX buffers associated
1700c6e0d914SCasey Leedom  *	with the current packet back on the original Free List.  The buffers
1701c6e0d914SCasey Leedom  *	have already been unmapped and are left unmapped, we mark them as
1702c6e0d914SCasey Leedom  *	unmapped in order to prevent further unmapping attempts.  (Effectively
1703c6e0d914SCasey Leedom  *	this function undoes the series of @unmap_rx_buf calls which were done
1704c6e0d914SCasey Leedom  *	to create the current packet's gather list.)  This leaves us ready to
1705c6e0d914SCasey Leedom  *	restart processing of the packet the next time we start processing the
1706c6e0d914SCasey Leedom  *	RX Queue ...
1707c6e0d914SCasey Leedom  */
restore_rx_bufs(const struct pkt_gl * gl,struct sge_fl * fl,int frags)1708c6e0d914SCasey Leedom static void restore_rx_bufs(const struct pkt_gl *gl, struct sge_fl *fl,
1709c6e0d914SCasey Leedom 			    int frags)
1710c6e0d914SCasey Leedom {
1711c6e0d914SCasey Leedom 	struct rx_sw_desc *sdesc;
1712c6e0d914SCasey Leedom 
1713c6e0d914SCasey Leedom 	while (frags--) {
1714c6e0d914SCasey Leedom 		if (fl->cidx == 0)
1715c6e0d914SCasey Leedom 			fl->cidx = fl->size - 1;
1716c6e0d914SCasey Leedom 		else
1717c6e0d914SCasey Leedom 			fl->cidx--;
1718c6e0d914SCasey Leedom 		sdesc = &fl->sdesc[fl->cidx];
1719c6e0d914SCasey Leedom 		sdesc->page = gl->frags[frags].page;
1720c6e0d914SCasey Leedom 		sdesc->dma_addr |= RX_UNMAPPED_BUF;
1721c6e0d914SCasey Leedom 		fl->avail++;
1722c6e0d914SCasey Leedom 	}
1723c6e0d914SCasey Leedom }
1724c6e0d914SCasey Leedom 
1725c6e0d914SCasey Leedom /**
1726c6e0d914SCasey Leedom  *	rspq_next - advance to the next entry in a response queue
1727c6e0d914SCasey Leedom  *	@rspq: the queue
1728c6e0d914SCasey Leedom  *
1729c6e0d914SCasey Leedom  *	Updates the state of a response queue to advance it to the next entry.
1730c6e0d914SCasey Leedom  */
rspq_next(struct sge_rspq * rspq)1731c6e0d914SCasey Leedom static inline void rspq_next(struct sge_rspq *rspq)
1732c6e0d914SCasey Leedom {
1733c6e0d914SCasey Leedom 	rspq->cur_desc = (void *)rspq->cur_desc + rspq->iqe_len;
1734c6e0d914SCasey Leedom 	if (unlikely(++rspq->cidx == rspq->size)) {
1735c6e0d914SCasey Leedom 		rspq->cidx = 0;
1736c6e0d914SCasey Leedom 		rspq->gen ^= 1;
1737c6e0d914SCasey Leedom 		rspq->cur_desc = rspq->desc;
1738c6e0d914SCasey Leedom 	}
1739c6e0d914SCasey Leedom }
1740c6e0d914SCasey Leedom 
1741c6e0d914SCasey Leedom /**
1742c6e0d914SCasey Leedom  *	process_responses - process responses from an SGE response queue
1743c6e0d914SCasey Leedom  *	@rspq: the ingress response queue to process
1744c6e0d914SCasey Leedom  *	@budget: how many responses can be processed in this round
1745c6e0d914SCasey Leedom  *
1746c6e0d914SCasey Leedom  *	Process responses from a Scatter Gather Engine response queue up to
1747c6e0d914SCasey Leedom  *	the supplied budget.  Responses include received packets as well as
1748c6e0d914SCasey Leedom  *	control messages from firmware or hardware.
1749c6e0d914SCasey Leedom  *
1750c6e0d914SCasey Leedom  *	Additionally choose the interrupt holdoff time for the next interrupt
1751c6e0d914SCasey Leedom  *	on this queue.  If the system is under memory shortage use a fairly
1752c6e0d914SCasey Leedom  *	long delay to help recovery.
1753c6e0d914SCasey Leedom  */
process_responses(struct sge_rspq * rspq,int budget)17548a67d1c6SSachin Kamat static int process_responses(struct sge_rspq *rspq, int budget)
1755c6e0d914SCasey Leedom {
1756c6e0d914SCasey Leedom 	struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
175765f6ecc9SHariprasad Shenai 	struct adapter *adapter = rspq->adapter;
175865f6ecc9SHariprasad Shenai 	struct sge *s = &adapter->sge;
1759c6e0d914SCasey Leedom 	int budget_left = budget;
1760c6e0d914SCasey Leedom 
1761c6e0d914SCasey Leedom 	while (likely(budget_left)) {
1762c6e0d914SCasey Leedom 		int ret, rsp_type;
1763c6e0d914SCasey Leedom 		const struct rsp_ctrl *rc;
1764c6e0d914SCasey Leedom 
1765c6e0d914SCasey Leedom 		rc = (void *)rspq->cur_desc + (rspq->iqe_len - sizeof(*rc));
1766c6e0d914SCasey Leedom 		if (!is_new_response(rc, rspq))
1767c6e0d914SCasey Leedom 			break;
1768c6e0d914SCasey Leedom 
1769c6e0d914SCasey Leedom 		/*
1770c6e0d914SCasey Leedom 		 * Figure out what kind of response we've received from the
1771c6e0d914SCasey Leedom 		 * SGE.
1772c6e0d914SCasey Leedom 		 */
1773019be1cfSAlexander Duyck 		dma_rmb();
17741ecc7b7aSHariprasad Shenai 		rsp_type = RSPD_TYPE_G(rc->type_gen);
17751ecc7b7aSHariprasad Shenai 		if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
1776a0006a86SIan Campbell 			struct page_frag *fp;
1777c6e0d914SCasey Leedom 			struct pkt_gl gl;
1778c6e0d914SCasey Leedom 			const struct rx_sw_desc *sdesc;
1779c6e0d914SCasey Leedom 			u32 bufsz, frag;
1780c6e0d914SCasey Leedom 			u32 len = be32_to_cpu(rc->pldbuflen_qid);
1781c6e0d914SCasey Leedom 
1782c6e0d914SCasey Leedom 			/*
1783c6e0d914SCasey Leedom 			 * If we get a "new buffer" message from the SGE we
1784c6e0d914SCasey Leedom 			 * need to move on to the next Free List buffer.
1785c6e0d914SCasey Leedom 			 */
17861ecc7b7aSHariprasad Shenai 			if (len & RSPD_NEWBUF_F) {
1787c6e0d914SCasey Leedom 				/*
1788c6e0d914SCasey Leedom 				 * We get one "new buffer" message when we
1789c6e0d914SCasey Leedom 				 * first start up a queue so we need to ignore
1790c6e0d914SCasey Leedom 				 * it when our offset into the buffer is 0.
1791c6e0d914SCasey Leedom 				 */
1792c6e0d914SCasey Leedom 				if (likely(rspq->offset > 0)) {
1793c6e0d914SCasey Leedom 					free_rx_bufs(rspq->adapter, &rxq->fl,
1794c6e0d914SCasey Leedom 						     1);
1795c6e0d914SCasey Leedom 					rspq->offset = 0;
1796c6e0d914SCasey Leedom 				}
17971ecc7b7aSHariprasad Shenai 				len = RSPD_LEN_G(len);
1798c6e0d914SCasey Leedom 			}
1799b94e72e2SCasey Leedom 			gl.tot_len = len;
1800c6e0d914SCasey Leedom 
1801c6e0d914SCasey Leedom 			/*
1802c6e0d914SCasey Leedom 			 * Gather packet fragments.
1803c6e0d914SCasey Leedom 			 */
1804c6e0d914SCasey Leedom 			for (frag = 0, fp = gl.frags; /**/; frag++, fp++) {
1805c6e0d914SCasey Leedom 				BUG_ON(frag >= MAX_SKB_FRAGS);
1806c6e0d914SCasey Leedom 				BUG_ON(rxq->fl.avail == 0);
1807c6e0d914SCasey Leedom 				sdesc = &rxq->fl.sdesc[rxq->fl.cidx];
180865f6ecc9SHariprasad Shenai 				bufsz = get_buf_size(adapter, sdesc);
1809c6e0d914SCasey Leedom 				fp->page = sdesc->page;
1810a0006a86SIan Campbell 				fp->offset = rspq->offset;
1811a0006a86SIan Campbell 				fp->size = min(bufsz, len);
1812a0006a86SIan Campbell 				len -= fp->size;
1813c6e0d914SCasey Leedom 				if (!len)
1814c6e0d914SCasey Leedom 					break;
1815c6e0d914SCasey Leedom 				unmap_rx_buf(rspq->adapter, &rxq->fl);
1816c6e0d914SCasey Leedom 			}
1817c6e0d914SCasey Leedom 			gl.nfrags = frag+1;
1818c6e0d914SCasey Leedom 
1819c6e0d914SCasey Leedom 			/*
1820c6e0d914SCasey Leedom 			 * Last buffer remains mapped so explicitly make it
1821c6e0d914SCasey Leedom 			 * coherent for CPU access and start preloading first
1822c6e0d914SCasey Leedom 			 * cache line ...
1823c6e0d914SCasey Leedom 			 */
1824c6e0d914SCasey Leedom 			dma_sync_single_for_cpu(rspq->adapter->pdev_dev,
1825c6e0d914SCasey Leedom 						get_buf_addr(sdesc),
1826a0006a86SIan Campbell 						fp->size, DMA_FROM_DEVICE);
1827c6e0d914SCasey Leedom 			gl.va = (page_address(gl.frags[0].page) +
1828a0006a86SIan Campbell 				 gl.frags[0].offset);
1829c6e0d914SCasey Leedom 			prefetch(gl.va);
1830c6e0d914SCasey Leedom 
1831c6e0d914SCasey Leedom 			/*
1832c6e0d914SCasey Leedom 			 * Hand the new ingress packet to the handler for
1833c6e0d914SCasey Leedom 			 * this Response Queue.
1834c6e0d914SCasey Leedom 			 */
1835c6e0d914SCasey Leedom 			ret = rspq->handler(rspq, rspq->cur_desc, &gl);
1836c6e0d914SCasey Leedom 			if (likely(ret == 0))
183765f6ecc9SHariprasad Shenai 				rspq->offset += ALIGN(fp->size, s->fl_align);
1838c6e0d914SCasey Leedom 			else
1839c6e0d914SCasey Leedom 				restore_rx_bufs(&gl, &rxq->fl, frag);
18401ecc7b7aSHariprasad Shenai 		} else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
1841c6e0d914SCasey Leedom 			ret = rspq->handler(rspq, rspq->cur_desc, NULL);
1842c6e0d914SCasey Leedom 		} else {
18431ecc7b7aSHariprasad Shenai 			WARN_ON(rsp_type > RSPD_TYPE_CPL_X);
1844c6e0d914SCasey Leedom 			ret = 0;
1845c6e0d914SCasey Leedom 		}
1846c6e0d914SCasey Leedom 
1847c6e0d914SCasey Leedom 		if (unlikely(ret)) {
1848c6e0d914SCasey Leedom 			/*
1849c6e0d914SCasey Leedom 			 * Couldn't process descriptor, back off for recovery.
1850c6e0d914SCasey Leedom 			 * We use the SGE's last timer which has the longest
1851c6e0d914SCasey Leedom 			 * interrupt coalescing value ...
1852c6e0d914SCasey Leedom 			 */
1853c6e0d914SCasey Leedom 			const int NOMEM_TIMER_IDX = SGE_NTIMERS-1;
1854c6e0d914SCasey Leedom 			rspq->next_intr_params =
18551ecc7b7aSHariprasad Shenai 				QINTR_TIMER_IDX_V(NOMEM_TIMER_IDX);
1856c6e0d914SCasey Leedom 			break;
1857c6e0d914SCasey Leedom 		}
1858c6e0d914SCasey Leedom 
1859c6e0d914SCasey Leedom 		rspq_next(rspq);
1860c6e0d914SCasey Leedom 		budget_left--;
1861c6e0d914SCasey Leedom 	}
1862c6e0d914SCasey Leedom 
1863c6e0d914SCasey Leedom 	/*
1864c6e0d914SCasey Leedom 	 * If this is a Response Queue with an associated Free List and
1865c6e0d914SCasey Leedom 	 * at least two Egress Queue units available in the Free List
1866c6e0d914SCasey Leedom 	 * for new buffer pointers, refill the Free List.
1867c6e0d914SCasey Leedom 	 */
1868c6e0d914SCasey Leedom 	if (rspq->offset >= 0 &&
1869da08e425SHariprasad Shenai 	    fl_cap(&rxq->fl) - rxq->fl.avail >= 2*FL_PER_EQ_UNIT)
1870c6e0d914SCasey Leedom 		__refill_fl(rspq->adapter, &rxq->fl);
1871c6e0d914SCasey Leedom 	return budget - budget_left;
1872c6e0d914SCasey Leedom }
1873c6e0d914SCasey Leedom 
1874c6e0d914SCasey Leedom /**
1875c6e0d914SCasey Leedom  *	napi_rx_handler - the NAPI handler for RX processing
1876c6e0d914SCasey Leedom  *	@napi: the napi instance
1877c6e0d914SCasey Leedom  *	@budget: how many packets we can process in this round
1878c6e0d914SCasey Leedom  *
1879c6e0d914SCasey Leedom  *	Handler for new data events when using NAPI.  This does not need any
1880c6e0d914SCasey Leedom  *	locking or protection from interrupts as data interrupts are off at
1881c6e0d914SCasey Leedom  *	this point and other adapter interrupts do not interfere (the latter
1882c6e0d914SCasey Leedom  *	in not a concern at all with MSI-X as non-data interrupts then have
1883c6e0d914SCasey Leedom  *	a separate handler).
1884c6e0d914SCasey Leedom  */
napi_rx_handler(struct napi_struct * napi,int budget)1885c6e0d914SCasey Leedom static int napi_rx_handler(struct napi_struct *napi, int budget)
1886c6e0d914SCasey Leedom {
1887c6e0d914SCasey Leedom 	unsigned int intr_params;
1888c6e0d914SCasey Leedom 	struct sge_rspq *rspq = container_of(napi, struct sge_rspq, napi);
1889c6e0d914SCasey Leedom 	int work_done = process_responses(rspq, budget);
1890df64e4d3SHariprasad Shenai 	u32 val;
1891c6e0d914SCasey Leedom 
1892c6e0d914SCasey Leedom 	if (likely(work_done < budget)) {
18936ad20165SEric Dumazet 		napi_complete_done(napi, work_done);
1894c6e0d914SCasey Leedom 		intr_params = rspq->next_intr_params;
1895c6e0d914SCasey Leedom 		rspq->next_intr_params = rspq->intr_params;
1896c6e0d914SCasey Leedom 	} else
18971ecc7b7aSHariprasad Shenai 		intr_params = QINTR_TIMER_IDX_V(SGE_TIMER_UPD_CIDX);
1898c6e0d914SCasey Leedom 
189968dc9d36SCasey Leedom 	if (unlikely(work_done == 0))
190068dc9d36SCasey Leedom 		rspq->unhandled_irqs++;
190168dc9d36SCasey Leedom 
1902f612b815SHariprasad Shenai 	val = CIDXINC_V(work_done) | SEINTARM_V(intr_params);
190371d3c0b4SHariprasad Shenai 	/* If we don't have access to the new User GTS (T5+), use the old
190471d3c0b4SHariprasad Shenai 	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
190571d3c0b4SHariprasad Shenai 	 */
190671d3c0b4SHariprasad Shenai 	if (unlikely(!rspq->bar2_addr)) {
1907c6e0d914SCasey Leedom 		t4_write_reg(rspq->adapter,
1908c6e0d914SCasey Leedom 			     T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
1909f612b815SHariprasad Shenai 			     val | INGRESSQID_V((u32)rspq->cntxt_id));
1910df64e4d3SHariprasad Shenai 	} else {
1911f612b815SHariprasad Shenai 		writel(val | INGRESSQID_V(rspq->bar2_qid),
1912df64e4d3SHariprasad Shenai 		       rspq->bar2_addr + SGE_UDB_GTS);
1913df64e4d3SHariprasad Shenai 		wmb();
1914df64e4d3SHariprasad Shenai 	}
1915c6e0d914SCasey Leedom 	return work_done;
1916c6e0d914SCasey Leedom }
1917c6e0d914SCasey Leedom 
1918c6e0d914SCasey Leedom /*
1919c6e0d914SCasey Leedom  * The MSI-X interrupt handler for an SGE response queue for the NAPI case
1920c6e0d914SCasey Leedom  * (i.e., response queue serviced by NAPI polling).
1921c6e0d914SCasey Leedom  */
t4vf_sge_intr_msix(int irq,void * cookie)1922c6e0d914SCasey Leedom irqreturn_t t4vf_sge_intr_msix(int irq, void *cookie)
1923c6e0d914SCasey Leedom {
1924c6e0d914SCasey Leedom 	struct sge_rspq *rspq = cookie;
1925c6e0d914SCasey Leedom 
1926c6e0d914SCasey Leedom 	napi_schedule(&rspq->napi);
1927c6e0d914SCasey Leedom 	return IRQ_HANDLED;
1928c6e0d914SCasey Leedom }
1929c6e0d914SCasey Leedom 
1930c6e0d914SCasey Leedom /*
1931c6e0d914SCasey Leedom  * Process the indirect interrupt entries in the interrupt queue and kick off
1932c6e0d914SCasey Leedom  * NAPI for each queue that has generated an entry.
1933c6e0d914SCasey Leedom  */
process_intrq(struct adapter * adapter)1934c6e0d914SCasey Leedom static unsigned int process_intrq(struct adapter *adapter)
1935c6e0d914SCasey Leedom {
1936c6e0d914SCasey Leedom 	struct sge *s = &adapter->sge;
1937c6e0d914SCasey Leedom 	struct sge_rspq *intrq = &s->intrq;
1938c6e0d914SCasey Leedom 	unsigned int work_done;
1939df64e4d3SHariprasad Shenai 	u32 val;
1940c6e0d914SCasey Leedom 
1941c6e0d914SCasey Leedom 	spin_lock(&adapter->sge.intrq_lock);
1942c6e0d914SCasey Leedom 	for (work_done = 0; ; work_done++) {
1943c6e0d914SCasey Leedom 		const struct rsp_ctrl *rc;
1944c6e0d914SCasey Leedom 		unsigned int qid, iq_idx;
1945c6e0d914SCasey Leedom 		struct sge_rspq *rspq;
1946c6e0d914SCasey Leedom 
1947c6e0d914SCasey Leedom 		/*
1948c6e0d914SCasey Leedom 		 * Grab the next response from the interrupt queue and bail
1949c6e0d914SCasey Leedom 		 * out if it's not a new response.
1950c6e0d914SCasey Leedom 		 */
1951c6e0d914SCasey Leedom 		rc = (void *)intrq->cur_desc + (intrq->iqe_len - sizeof(*rc));
1952c6e0d914SCasey Leedom 		if (!is_new_response(rc, intrq))
1953c6e0d914SCasey Leedom 			break;
1954c6e0d914SCasey Leedom 
1955c6e0d914SCasey Leedom 		/*
1956c6e0d914SCasey Leedom 		 * If the response isn't a forwarded interrupt message issue a
1957c6e0d914SCasey Leedom 		 * error and go on to the next response message.  This should
1958c6e0d914SCasey Leedom 		 * never happen ...
1959c6e0d914SCasey Leedom 		 */
1960019be1cfSAlexander Duyck 		dma_rmb();
19611ecc7b7aSHariprasad Shenai 		if (unlikely(RSPD_TYPE_G(rc->type_gen) != RSPD_TYPE_INTR_X)) {
1962c6e0d914SCasey Leedom 			dev_err(adapter->pdev_dev,
1963c6e0d914SCasey Leedom 				"Unexpected INTRQ response type %d\n",
19641ecc7b7aSHariprasad Shenai 				RSPD_TYPE_G(rc->type_gen));
1965c6e0d914SCasey Leedom 			continue;
1966c6e0d914SCasey Leedom 		}
1967c6e0d914SCasey Leedom 
1968c6e0d914SCasey Leedom 		/*
1969c6e0d914SCasey Leedom 		 * Extract the Queue ID from the interrupt message and perform
1970c6e0d914SCasey Leedom 		 * sanity checking to make sure it really refers to one of our
1971c6e0d914SCasey Leedom 		 * Ingress Queues which is active and matches the queue's ID.
1972c6e0d914SCasey Leedom 		 * None of these error conditions should ever happen so we may
1973c6e0d914SCasey Leedom 		 * want to either make them fatal and/or conditionalized under
1974c6e0d914SCasey Leedom 		 * DEBUG.
1975c6e0d914SCasey Leedom 		 */
19761ecc7b7aSHariprasad Shenai 		qid = RSPD_QID_G(be32_to_cpu(rc->pldbuflen_qid));
1977c6e0d914SCasey Leedom 		iq_idx = IQ_IDX(s, qid);
1978c6e0d914SCasey Leedom 		if (unlikely(iq_idx >= MAX_INGQ)) {
1979c6e0d914SCasey Leedom 			dev_err(adapter->pdev_dev,
1980c6e0d914SCasey Leedom 				"Ingress QID %d out of range\n", qid);
1981c6e0d914SCasey Leedom 			continue;
1982c6e0d914SCasey Leedom 		}
1983c6e0d914SCasey Leedom 		rspq = s->ingr_map[iq_idx];
1984c6e0d914SCasey Leedom 		if (unlikely(rspq == NULL)) {
1985c6e0d914SCasey Leedom 			dev_err(adapter->pdev_dev,
1986c6e0d914SCasey Leedom 				"Ingress QID %d RSPQ=NULL\n", qid);
1987c6e0d914SCasey Leedom 			continue;
1988c6e0d914SCasey Leedom 		}
1989c6e0d914SCasey Leedom 		if (unlikely(rspq->abs_id != qid)) {
1990c6e0d914SCasey Leedom 			dev_err(adapter->pdev_dev,
1991c6e0d914SCasey Leedom 				"Ingress QID %d refers to RSPQ %d\n",
1992c6e0d914SCasey Leedom 				qid, rspq->abs_id);
1993c6e0d914SCasey Leedom 			continue;
1994c6e0d914SCasey Leedom 		}
1995c6e0d914SCasey Leedom 
1996c6e0d914SCasey Leedom 		/*
1997c6e0d914SCasey Leedom 		 * Schedule NAPI processing on the indicated Response Queue
1998c6e0d914SCasey Leedom 		 * and move on to the next entry in the Forwarded Interrupt
1999c6e0d914SCasey Leedom 		 * Queue.
2000c6e0d914SCasey Leedom 		 */
2001c6e0d914SCasey Leedom 		napi_schedule(&rspq->napi);
2002c6e0d914SCasey Leedom 		rspq_next(intrq);
2003c6e0d914SCasey Leedom 	}
2004c6e0d914SCasey Leedom 
2005f612b815SHariprasad Shenai 	val = CIDXINC_V(work_done) | SEINTARM_V(intrq->intr_params);
200671d3c0b4SHariprasad Shenai 	/* If we don't have access to the new User GTS (T5+), use the old
200771d3c0b4SHariprasad Shenai 	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
200871d3c0b4SHariprasad Shenai 	 */
200971d3c0b4SHariprasad Shenai 	if (unlikely(!intrq->bar2_addr)) {
2010c6e0d914SCasey Leedom 		t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
2011f612b815SHariprasad Shenai 			     val | INGRESSQID_V(intrq->cntxt_id));
201271d3c0b4SHariprasad Shenai 	} else {
2013f612b815SHariprasad Shenai 		writel(val | INGRESSQID_V(intrq->bar2_qid),
2014df64e4d3SHariprasad Shenai 		       intrq->bar2_addr + SGE_UDB_GTS);
2015df64e4d3SHariprasad Shenai 		wmb();
2016df64e4d3SHariprasad Shenai 	}
2017c6e0d914SCasey Leedom 
2018c6e0d914SCasey Leedom 	spin_unlock(&adapter->sge.intrq_lock);
2019c6e0d914SCasey Leedom 
2020c6e0d914SCasey Leedom 	return work_done;
2021c6e0d914SCasey Leedom }
2022c6e0d914SCasey Leedom 
2023c6e0d914SCasey Leedom /*
2024c6e0d914SCasey Leedom  * The MSI interrupt handler handles data events from SGE response queues as
2025c6e0d914SCasey Leedom  * well as error and other async events as they all use the same MSI vector.
2026c6e0d914SCasey Leedom  */
t4vf_intr_msi(int irq,void * cookie)20278a67d1c6SSachin Kamat static irqreturn_t t4vf_intr_msi(int irq, void *cookie)
2028c6e0d914SCasey Leedom {
2029c6e0d914SCasey Leedom 	struct adapter *adapter = cookie;
2030c6e0d914SCasey Leedom 
2031c6e0d914SCasey Leedom 	process_intrq(adapter);
2032c6e0d914SCasey Leedom 	return IRQ_HANDLED;
2033c6e0d914SCasey Leedom }
2034c6e0d914SCasey Leedom 
2035c6e0d914SCasey Leedom /**
2036c6e0d914SCasey Leedom  *	t4vf_intr_handler - select the top-level interrupt handler
2037c6e0d914SCasey Leedom  *	@adapter: the adapter
2038c6e0d914SCasey Leedom  *
2039c6e0d914SCasey Leedom  *	Selects the top-level interrupt handler based on the type of interrupts
2040c6e0d914SCasey Leedom  *	(MSI-X or MSI).
2041c6e0d914SCasey Leedom  */
t4vf_intr_handler(struct adapter * adapter)2042c6e0d914SCasey Leedom irq_handler_t t4vf_intr_handler(struct adapter *adapter)
2043c6e0d914SCasey Leedom {
20443d78bfaaSArjun Vynipadath 	BUG_ON((adapter->flags &
20453d78bfaaSArjun Vynipadath 	       (CXGB4VF_USING_MSIX | CXGB4VF_USING_MSI)) == 0);
20463d78bfaaSArjun Vynipadath 	if (adapter->flags & CXGB4VF_USING_MSIX)
2047c6e0d914SCasey Leedom 		return t4vf_sge_intr_msix;
2048c6e0d914SCasey Leedom 	else
2049c6e0d914SCasey Leedom 		return t4vf_intr_msi;
2050c6e0d914SCasey Leedom }
2051c6e0d914SCasey Leedom 
2052c6e0d914SCasey Leedom /**
2053c6e0d914SCasey Leedom  *	sge_rx_timer_cb - perform periodic maintenance of SGE RX queues
205420bb0c8fSRahul Lakkireddy  *	@t: Rx timer
2055c6e0d914SCasey Leedom  *
2056c6e0d914SCasey Leedom  *	Runs periodically from a timer to perform maintenance of SGE RX queues.
2057c6e0d914SCasey Leedom  *
2058c6e0d914SCasey Leedom  *	a) Replenishes RX queues that have run out due to memory shortage.
2059c6e0d914SCasey Leedom  *	Normally new RX buffers are added when existing ones are consumed but
2060c6e0d914SCasey Leedom  *	when out of memory a queue can become empty.  We schedule NAPI to do
2061c6e0d914SCasey Leedom  *	the actual refill.
2062c6e0d914SCasey Leedom  */
sge_rx_timer_cb(struct timer_list * t)20630e23daebSKees Cook static void sge_rx_timer_cb(struct timer_list *t)
2064c6e0d914SCasey Leedom {
2065*41cb0855SIngo Molnar 	struct adapter *adapter = timer_container_of(adapter, t, sge.rx_timer);
2066c6e0d914SCasey Leedom 	struct sge *s = &adapter->sge;
2067c6e0d914SCasey Leedom 	unsigned int i;
2068c6e0d914SCasey Leedom 
2069c6e0d914SCasey Leedom 	/*
2070c6e0d914SCasey Leedom 	 * Scan the "Starving Free Lists" flag array looking for any Free
2071c6e0d914SCasey Leedom 	 * Lists in need of more free buffers.  If we find one and it's not
2072c6e0d914SCasey Leedom 	 * being actively polled, then bump its "starving" counter and attempt
2073c6e0d914SCasey Leedom 	 * to refill it.  If we're successful in adding enough buffers to push
2074c6e0d914SCasey Leedom 	 * the Free List over the starving threshold, then we can clear its
2075c6e0d914SCasey Leedom 	 * "starving" status.
2076c6e0d914SCasey Leedom 	 */
2077c6e0d914SCasey Leedom 	for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++) {
2078c6e0d914SCasey Leedom 		unsigned long m;
2079c6e0d914SCasey Leedom 
2080c6e0d914SCasey Leedom 		for (m = s->starving_fl[i]; m; m &= m - 1) {
2081c6e0d914SCasey Leedom 			unsigned int id = __ffs(m) + i * BITS_PER_LONG;
2082c6e0d914SCasey Leedom 			struct sge_fl *fl = s->egr_map[id];
2083c6e0d914SCasey Leedom 
2084c6e0d914SCasey Leedom 			clear_bit(id, s->starving_fl);
20854e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2086c6e0d914SCasey Leedom 
2087c6e0d914SCasey Leedom 			/*
2088c6e0d914SCasey Leedom 			 * Since we are accessing fl without a lock there's a
2089c6e0d914SCasey Leedom 			 * small probability of a false positive where we
2090c6e0d914SCasey Leedom 			 * schedule napi but the FL is no longer starving.
2091c6e0d914SCasey Leedom 			 * No biggie.
2092c6e0d914SCasey Leedom 			 */
209365f6ecc9SHariprasad Shenai 			if (fl_starving(adapter, fl)) {
2094c6e0d914SCasey Leedom 				struct sge_eth_rxq *rxq;
2095c6e0d914SCasey Leedom 
2096c6e0d914SCasey Leedom 				rxq = container_of(fl, struct sge_eth_rxq, fl);
209773382e91SChristian Marangi 				if (napi_schedule(&rxq->rspq.napi))
2098c6e0d914SCasey Leedom 					fl->starving++;
2099c6e0d914SCasey Leedom 				else
2100c6e0d914SCasey Leedom 					set_bit(id, s->starving_fl);
2101c6e0d914SCasey Leedom 			}
2102c6e0d914SCasey Leedom 		}
2103c6e0d914SCasey Leedom 	}
2104c6e0d914SCasey Leedom 
2105c6e0d914SCasey Leedom 	/*
2106c6e0d914SCasey Leedom 	 * Reschedule the next scan for starving Free Lists ...
2107c6e0d914SCasey Leedom 	 */
2108c6e0d914SCasey Leedom 	mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
2109c6e0d914SCasey Leedom }
2110c6e0d914SCasey Leedom 
2111c6e0d914SCasey Leedom /**
2112c6e0d914SCasey Leedom  *	sge_tx_timer_cb - perform periodic maintenance of SGE Tx queues
211320bb0c8fSRahul Lakkireddy  *	@t: Tx timer
2114c6e0d914SCasey Leedom  *
2115c6e0d914SCasey Leedom  *	Runs periodically from a timer to perform maintenance of SGE TX queues.
2116c6e0d914SCasey Leedom  *
2117c6e0d914SCasey Leedom  *	b) Reclaims completed Tx packets for the Ethernet queues.  Normally
2118c6e0d914SCasey Leedom  *	packets are cleaned up by new Tx packets, this timer cleans up packets
2119c6e0d914SCasey Leedom  *	when no new packets are being submitted.  This is essential for pktgen,
2120c6e0d914SCasey Leedom  *	at least.
2121c6e0d914SCasey Leedom  */
sge_tx_timer_cb(struct timer_list * t)21220e23daebSKees Cook static void sge_tx_timer_cb(struct timer_list *t)
2123c6e0d914SCasey Leedom {
2124*41cb0855SIngo Molnar 	struct adapter *adapter = timer_container_of(adapter, t, sge.tx_timer);
2125c6e0d914SCasey Leedom 	struct sge *s = &adapter->sge;
2126c6e0d914SCasey Leedom 	unsigned int i, budget;
2127c6e0d914SCasey Leedom 
2128c6e0d914SCasey Leedom 	budget = MAX_TIMER_TX_RECLAIM;
2129c6e0d914SCasey Leedom 	i = s->ethtxq_rover;
2130c6e0d914SCasey Leedom 	do {
2131c6e0d914SCasey Leedom 		struct sge_eth_txq *txq = &s->ethtxq[i];
2132c6e0d914SCasey Leedom 
2133c6e0d914SCasey Leedom 		if (reclaimable(&txq->q) && __netif_tx_trylock(txq->txq)) {
2134c6e0d914SCasey Leedom 			int avail = reclaimable(&txq->q);
2135c6e0d914SCasey Leedom 
2136c6e0d914SCasey Leedom 			if (avail > budget)
2137c6e0d914SCasey Leedom 				avail = budget;
2138c6e0d914SCasey Leedom 
2139c6e0d914SCasey Leedom 			free_tx_desc(adapter, &txq->q, avail, true);
2140c6e0d914SCasey Leedom 			txq->q.in_use -= avail;
2141c6e0d914SCasey Leedom 			__netif_tx_unlock(txq->txq);
2142c6e0d914SCasey Leedom 
2143c6e0d914SCasey Leedom 			budget -= avail;
2144c6e0d914SCasey Leedom 			if (!budget)
2145c6e0d914SCasey Leedom 				break;
2146c6e0d914SCasey Leedom 		}
2147c6e0d914SCasey Leedom 
2148c6e0d914SCasey Leedom 		i++;
2149c6e0d914SCasey Leedom 		if (i >= s->ethqsets)
2150c6e0d914SCasey Leedom 			i = 0;
2151c6e0d914SCasey Leedom 	} while (i != s->ethtxq_rover);
2152c6e0d914SCasey Leedom 	s->ethtxq_rover = i;
2153c6e0d914SCasey Leedom 
2154c6e0d914SCasey Leedom 	/*
2155c6e0d914SCasey Leedom 	 * If we found too many reclaimable packets schedule a timer in the
2156c6e0d914SCasey Leedom 	 * near future to continue where we left off.  Otherwise the next timer
2157c6e0d914SCasey Leedom 	 * will be at its normal interval.
2158c6e0d914SCasey Leedom 	 */
2159c6e0d914SCasey Leedom 	mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
2160c6e0d914SCasey Leedom }
2161c6e0d914SCasey Leedom 
2162c6e0d914SCasey Leedom /**
2163df64e4d3SHariprasad Shenai  *	bar2_address - return the BAR2 address for an SGE Queue's Registers
2164df64e4d3SHariprasad Shenai  *	@adapter: the adapter
2165df64e4d3SHariprasad Shenai  *	@qid: the SGE Queue ID
2166df64e4d3SHariprasad Shenai  *	@qtype: the SGE Queue Type (Egress or Ingress)
2167df64e4d3SHariprasad Shenai  *	@pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
2168df64e4d3SHariprasad Shenai  *
2169df64e4d3SHariprasad Shenai  *	Returns the BAR2 address for the SGE Queue Registers associated with
2170df64e4d3SHariprasad Shenai  *	@qid.  If BAR2 SGE Registers aren't available, returns NULL.  Also
2171df64e4d3SHariprasad Shenai  *	returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
2172df64e4d3SHariprasad Shenai  *	Queue Registers.  If the BAR2 Queue ID is 0, then "Inferred Queue ID"
2173df64e4d3SHariprasad Shenai  *	Registers are supported (e.g. the Write Combining Doorbell Buffer).
2174df64e4d3SHariprasad Shenai  */
bar2_address(struct adapter * adapter,unsigned int qid,enum t4_bar2_qtype qtype,unsigned int * pbar2_qid)2175df64e4d3SHariprasad Shenai static void __iomem *bar2_address(struct adapter *adapter,
2176df64e4d3SHariprasad Shenai 				  unsigned int qid,
2177df64e4d3SHariprasad Shenai 				  enum t4_bar2_qtype qtype,
2178df64e4d3SHariprasad Shenai 				  unsigned int *pbar2_qid)
2179df64e4d3SHariprasad Shenai {
2180df64e4d3SHariprasad Shenai 	u64 bar2_qoffset;
2181df64e4d3SHariprasad Shenai 	int ret;
2182df64e4d3SHariprasad Shenai 
2183b2612722SHariprasad Shenai 	ret = t4vf_bar2_sge_qregs(adapter, qid, qtype,
2184df64e4d3SHariprasad Shenai 				  &bar2_qoffset, pbar2_qid);
2185df64e4d3SHariprasad Shenai 	if (ret)
2186df64e4d3SHariprasad Shenai 		return NULL;
2187df64e4d3SHariprasad Shenai 
2188df64e4d3SHariprasad Shenai 	return adapter->bar2 + bar2_qoffset;
2189df64e4d3SHariprasad Shenai }
2190df64e4d3SHariprasad Shenai 
2191df64e4d3SHariprasad Shenai /**
2192c6e0d914SCasey Leedom  *	t4vf_sge_alloc_rxq - allocate an SGE RX Queue
2193c6e0d914SCasey Leedom  *	@adapter: the adapter
2194c6e0d914SCasey Leedom  *	@rspq: pointer to the new rxq's Response Queue to be filled in
2195c6e0d914SCasey Leedom  *	@iqasynch: if 0, a normal rspq; if 1, an asynchronous event queue
2196c6e0d914SCasey Leedom  *	@dev: the network device associated with the new rspq
2197c6e0d914SCasey Leedom  *	@intr_dest: MSI-X vector index (overriden in MSI mode)
2198c6e0d914SCasey Leedom  *	@fl: pointer to the new rxq's Free List to be filled in
2199c6e0d914SCasey Leedom  *	@hnd: the interrupt handler to invoke for the rspq
2200c6e0d914SCasey Leedom  */
t4vf_sge_alloc_rxq(struct adapter * adapter,struct sge_rspq * rspq,bool iqasynch,struct net_device * dev,int intr_dest,struct sge_fl * fl,rspq_handler_t hnd)2201c6e0d914SCasey Leedom int t4vf_sge_alloc_rxq(struct adapter *adapter, struct sge_rspq *rspq,
2202c6e0d914SCasey Leedom 		       bool iqasynch, struct net_device *dev,
2203c6e0d914SCasey Leedom 		       int intr_dest,
2204c6e0d914SCasey Leedom 		       struct sge_fl *fl, rspq_handler_t hnd)
2205c6e0d914SCasey Leedom {
220665f6ecc9SHariprasad Shenai 	struct sge *s = &adapter->sge;
2207c6e0d914SCasey Leedom 	struct port_info *pi = netdev_priv(dev);
2208c6e0d914SCasey Leedom 	struct fw_iq_cmd cmd, rpl;
2209c6e0d914SCasey Leedom 	int ret, iqandst, flsz = 0;
22103d78bfaaSArjun Vynipadath 	int relaxed = !(adapter->flags & CXGB4VF_ROOT_NO_RELAXED_ORDERING);
2211c6e0d914SCasey Leedom 
2212c6e0d914SCasey Leedom 	/*
2213c6e0d914SCasey Leedom 	 * If we're using MSI interrupts and we're not initializing the
2214c6e0d914SCasey Leedom 	 * Forwarded Interrupt Queue itself, then set up this queue for
2215c6e0d914SCasey Leedom 	 * indirect interrupts to the Forwarded Interrupt Queue.  Obviously
2216c6e0d914SCasey Leedom 	 * the Forwarded Interrupt Queue must be set up before any other
2217c6e0d914SCasey Leedom 	 * ingress queue ...
2218c6e0d914SCasey Leedom 	 */
22193d78bfaaSArjun Vynipadath 	if ((adapter->flags & CXGB4VF_USING_MSI) &&
22203d78bfaaSArjun Vynipadath 	    rspq != &adapter->sge.intrq) {
2221c6e0d914SCasey Leedom 		iqandst = SGE_INTRDST_IQ;
2222c6e0d914SCasey Leedom 		intr_dest = adapter->sge.intrq.abs_id;
2223c6e0d914SCasey Leedom 	} else
2224c6e0d914SCasey Leedom 		iqandst = SGE_INTRDST_PCI;
2225c6e0d914SCasey Leedom 
2226c6e0d914SCasey Leedom 	/*
2227c6e0d914SCasey Leedom 	 * Allocate the hardware ring for the Response Queue.  The size needs
2228c6e0d914SCasey Leedom 	 * to be a multiple of 16 which includes the mandatory status entry
2229c6e0d914SCasey Leedom 	 * (regardless of whether the Status Page capabilities are enabled or
2230c6e0d914SCasey Leedom 	 * not).
2231c6e0d914SCasey Leedom 	 */
2232c6e0d914SCasey Leedom 	rspq->size = roundup(rspq->size, 16);
2233c6e0d914SCasey Leedom 	rspq->desc = alloc_ring(adapter->pdev_dev, rspq->size, rspq->iqe_len,
2234c6e0d914SCasey Leedom 				0, &rspq->phys_addr, NULL, 0);
2235c6e0d914SCasey Leedom 	if (!rspq->desc)
2236c6e0d914SCasey Leedom 		return -ENOMEM;
2237c6e0d914SCasey Leedom 
2238c6e0d914SCasey Leedom 	/*
2239c6e0d914SCasey Leedom 	 * Fill in the Ingress Queue Command.  Note: Ideally this code would
2240c6e0d914SCasey Leedom 	 * be in t4vf_hw.c but there are so many parameters and dependencies
2241c6e0d914SCasey Leedom 	 * on our Linux SGE state that we would end up having to pass tons of
2242c6e0d914SCasey Leedom 	 * parameters.  We'll have to think about how this might be migrated
2243c6e0d914SCasey Leedom 	 * into OS-independent common code ...
2244c6e0d914SCasey Leedom 	 */
2245c6e0d914SCasey Leedom 	memset(&cmd, 0, sizeof(cmd));
2246e2ac9628SHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_IQ_CMD) |
2247e2ac9628SHariprasad Shenai 				    FW_CMD_REQUEST_F |
2248e2ac9628SHariprasad Shenai 				    FW_CMD_WRITE_F |
2249e2ac9628SHariprasad Shenai 				    FW_CMD_EXEC_F);
22506e4b51a6SHariprasad Shenai 	cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_ALLOC_F |
22516e4b51a6SHariprasad Shenai 					 FW_IQ_CMD_IQSTART_F |
2252c6e0d914SCasey Leedom 					 FW_LEN16(cmd));
2253c6e0d914SCasey Leedom 	cmd.type_to_iqandstindex =
22546e4b51a6SHariprasad Shenai 		cpu_to_be32(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
22556e4b51a6SHariprasad Shenai 			    FW_IQ_CMD_IQASYNCH_V(iqasynch) |
22566e4b51a6SHariprasad Shenai 			    FW_IQ_CMD_VIID_V(pi->viid) |
22576e4b51a6SHariprasad Shenai 			    FW_IQ_CMD_IQANDST_V(iqandst) |
22586e4b51a6SHariprasad Shenai 			    FW_IQ_CMD_IQANUS_V(1) |
22596e4b51a6SHariprasad Shenai 			    FW_IQ_CMD_IQANUD_V(SGE_UPDATEDEL_INTR) |
22606e4b51a6SHariprasad Shenai 			    FW_IQ_CMD_IQANDSTINDEX_V(intr_dest));
2261c6e0d914SCasey Leedom 	cmd.iqdroprss_to_iqesize =
22626e4b51a6SHariprasad Shenai 		cpu_to_be16(FW_IQ_CMD_IQPCIECH_V(pi->port_id) |
22636e4b51a6SHariprasad Shenai 			    FW_IQ_CMD_IQGTSMODE_F |
22646e4b51a6SHariprasad Shenai 			    FW_IQ_CMD_IQINTCNTTHRESH_V(rspq->pktcnt_idx) |
22656e4b51a6SHariprasad Shenai 			    FW_IQ_CMD_IQESIZE_V(ilog2(rspq->iqe_len) - 4));
2266c6e0d914SCasey Leedom 	cmd.iqsize = cpu_to_be16(rspq->size);
2267c6e0d914SCasey Leedom 	cmd.iqaddr = cpu_to_be64(rspq->phys_addr);
2268c6e0d914SCasey Leedom 
2269c6e0d914SCasey Leedom 	if (fl) {
2270d429005fSVishal Kulkarni 		unsigned int chip_ver =
227141fc2e41SHariprasad Shenai 			CHELSIO_CHIP_VERSION(adapter->params.chip);
2272c6e0d914SCasey Leedom 		/*
2273c6e0d914SCasey Leedom 		 * Allocate the ring for the hardware free list (with space
2274c6e0d914SCasey Leedom 		 * for its status page) along with the associated software
2275c6e0d914SCasey Leedom 		 * descriptor ring.  The free list size needs to be a multiple
227613432997SHariprasad Shenai 		 * of the Egress Queue Unit and at least 2 Egress Units larger
227713432997SHariprasad Shenai 		 * than the SGE's Egress Congrestion Threshold
227813432997SHariprasad Shenai 		 * (fl_starve_thres - 1).
2279c6e0d914SCasey Leedom 		 */
228013432997SHariprasad Shenai 		if (fl->size < s->fl_starve_thres - 1 + 2 * FL_PER_EQ_UNIT)
228113432997SHariprasad Shenai 			fl->size = s->fl_starve_thres - 1 + 2 * FL_PER_EQ_UNIT;
2282c6e0d914SCasey Leedom 		fl->size = roundup(fl->size, FL_PER_EQ_UNIT);
2283c6e0d914SCasey Leedom 		fl->desc = alloc_ring(adapter->pdev_dev, fl->size,
2284c6e0d914SCasey Leedom 				      sizeof(__be64), sizeof(struct rx_sw_desc),
228565f6ecc9SHariprasad Shenai 				      &fl->addr, &fl->sdesc, s->stat_len);
2286c6e0d914SCasey Leedom 		if (!fl->desc) {
2287c6e0d914SCasey Leedom 			ret = -ENOMEM;
2288c6e0d914SCasey Leedom 			goto err;
2289c6e0d914SCasey Leedom 		}
2290c6e0d914SCasey Leedom 
2291c6e0d914SCasey Leedom 		/*
2292c6e0d914SCasey Leedom 		 * Calculate the size of the hardware free list ring plus
2293caedda35SCasey Leedom 		 * Status Page (which the SGE will place after the end of the
2294c6e0d914SCasey Leedom 		 * free list ring) in Egress Queue Units.
2295c6e0d914SCasey Leedom 		 */
2296c6e0d914SCasey Leedom 		flsz = (fl->size / FL_PER_EQ_UNIT +
229765f6ecc9SHariprasad Shenai 			s->stat_len / EQ_UNIT);
2298c6e0d914SCasey Leedom 
2299c6e0d914SCasey Leedom 		/*
2300c6e0d914SCasey Leedom 		 * Fill in all the relevant firmware Ingress Queue Command
2301c6e0d914SCasey Leedom 		 * fields for the free list.
2302c6e0d914SCasey Leedom 		 */
2303c6e0d914SCasey Leedom 		cmd.iqns_to_fl0congen =
2304c6e0d914SCasey Leedom 			cpu_to_be32(
23056e4b51a6SHariprasad Shenai 				FW_IQ_CMD_FL0HOSTFCMODE_V(SGE_HOSTFCMODE_NONE) |
23066e4b51a6SHariprasad Shenai 				FW_IQ_CMD_FL0PACKEN_F |
2307b629276dSCasey Leedom 				FW_IQ_CMD_FL0FETCHRO_V(relaxed) |
2308b629276dSCasey Leedom 				FW_IQ_CMD_FL0DATARO_V(relaxed) |
23096e4b51a6SHariprasad Shenai 				FW_IQ_CMD_FL0PADEN_F);
2310edadad80SHariprasad Shenai 
2311edadad80SHariprasad Shenai 		/* In T6, for egress queue type FL there is internal overhead
2312edadad80SHariprasad Shenai 		 * of 16B for header going into FLM module.  Hence the maximum
2313edadad80SHariprasad Shenai 		 * allowed burst size is 448 bytes.  For T4/T5, the hardware
2314edadad80SHariprasad Shenai 		 * doesn't coalesce fetch requests if more than 64 bytes of
2315edadad80SHariprasad Shenai 		 * Free List pointers are provided, so we use a 128-byte Fetch
2316edadad80SHariprasad Shenai 		 * Burst Minimum there (T6 implements coalescing so we can use
2317edadad80SHariprasad Shenai 		 * the smaller 64-byte value there).
2318edadad80SHariprasad Shenai 		 */
2319c6e0d914SCasey Leedom 		cmd.fl0dcaen_to_fl0cidxfthresh =
2320c6e0d914SCasey Leedom 			cpu_to_be16(
2321d429005fSVishal Kulkarni 				FW_IQ_CMD_FL0FBMIN_V(chip_ver <= CHELSIO_T5
2322d429005fSVishal Kulkarni 						     ? FETCHBURSTMIN_128B_X
2323d429005fSVishal Kulkarni 						     : FETCHBURSTMIN_64B_T6_X) |
2324d429005fSVishal Kulkarni 				FW_IQ_CMD_FL0FBMAX_V((chip_ver <= CHELSIO_T5) ?
232541fc2e41SHariprasad Shenai 						     FETCHBURSTMAX_512B_X :
232641fc2e41SHariprasad Shenai 						     FETCHBURSTMAX_256B_X));
2327c6e0d914SCasey Leedom 		cmd.fl0size = cpu_to_be16(flsz);
2328c6e0d914SCasey Leedom 		cmd.fl0addr = cpu_to_be64(fl->addr);
2329c6e0d914SCasey Leedom 	}
2330c6e0d914SCasey Leedom 
2331c6e0d914SCasey Leedom 	/*
2332c6e0d914SCasey Leedom 	 * Issue the firmware Ingress Queue Command and extract the results if
2333c6e0d914SCasey Leedom 	 * it completes successfully.
2334c6e0d914SCasey Leedom 	 */
2335c6e0d914SCasey Leedom 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
2336c6e0d914SCasey Leedom 	if (ret)
2337c6e0d914SCasey Leedom 		goto err;
2338c6e0d914SCasey Leedom 
2339b48b89f9SJakub Kicinski 	netif_napi_add(dev, &rspq->napi, napi_rx_handler);
2340c6e0d914SCasey Leedom 	rspq->cur_desc = rspq->desc;
2341c6e0d914SCasey Leedom 	rspq->cidx = 0;
2342c6e0d914SCasey Leedom 	rspq->gen = 1;
2343c6e0d914SCasey Leedom 	rspq->next_intr_params = rspq->intr_params;
2344c6e0d914SCasey Leedom 	rspq->cntxt_id = be16_to_cpu(rpl.iqid);
2345df64e4d3SHariprasad Shenai 	rspq->bar2_addr = bar2_address(adapter,
2346df64e4d3SHariprasad Shenai 				       rspq->cntxt_id,
2347df64e4d3SHariprasad Shenai 				       T4_BAR2_QTYPE_INGRESS,
2348df64e4d3SHariprasad Shenai 				       &rspq->bar2_qid);
2349c6e0d914SCasey Leedom 	rspq->abs_id = be16_to_cpu(rpl.physiqid);
2350c6e0d914SCasey Leedom 	rspq->size--;			/* subtract status entry */
2351c6e0d914SCasey Leedom 	rspq->adapter = adapter;
2352c6e0d914SCasey Leedom 	rspq->netdev = dev;
2353c6e0d914SCasey Leedom 	rspq->handler = hnd;
2354c6e0d914SCasey Leedom 
2355c6e0d914SCasey Leedom 	/* set offset to -1 to distinguish ingress queues without FL */
2356c6e0d914SCasey Leedom 	rspq->offset = fl ? 0 : -1;
2357c6e0d914SCasey Leedom 
2358c6e0d914SCasey Leedom 	if (fl) {
2359c6e0d914SCasey Leedom 		fl->cntxt_id = be16_to_cpu(rpl.fl0id);
2360c6e0d914SCasey Leedom 		fl->avail = 0;
2361c6e0d914SCasey Leedom 		fl->pend_cred = 0;
2362c6e0d914SCasey Leedom 		fl->pidx = 0;
2363c6e0d914SCasey Leedom 		fl->cidx = 0;
2364c6e0d914SCasey Leedom 		fl->alloc_failed = 0;
2365c6e0d914SCasey Leedom 		fl->large_alloc_failed = 0;
2366c6e0d914SCasey Leedom 		fl->starving = 0;
2367df64e4d3SHariprasad Shenai 
2368df64e4d3SHariprasad Shenai 		/* Note, we must initialize the BAR2 Free List User Doorbell
2369df64e4d3SHariprasad Shenai 		 * information before refilling the Free List!
2370df64e4d3SHariprasad Shenai 		 */
2371df64e4d3SHariprasad Shenai 		fl->bar2_addr = bar2_address(adapter,
2372df64e4d3SHariprasad Shenai 					     fl->cntxt_id,
2373df64e4d3SHariprasad Shenai 					     T4_BAR2_QTYPE_EGRESS,
2374df64e4d3SHariprasad Shenai 					     &fl->bar2_qid);
2375df64e4d3SHariprasad Shenai 
2376c6e0d914SCasey Leedom 		refill_fl(adapter, fl, fl_cap(fl), GFP_KERNEL);
2377c6e0d914SCasey Leedom 	}
2378c6e0d914SCasey Leedom 
2379c6e0d914SCasey Leedom 	return 0;
2380c6e0d914SCasey Leedom 
2381c6e0d914SCasey Leedom err:
2382c6e0d914SCasey Leedom 	/*
2383c6e0d914SCasey Leedom 	 * An error occurred.  Clean up our partial allocation state and
2384c6e0d914SCasey Leedom 	 * return the error.
2385c6e0d914SCasey Leedom 	 */
2386c6e0d914SCasey Leedom 	if (rspq->desc) {
2387c6e0d914SCasey Leedom 		dma_free_coherent(adapter->pdev_dev, rspq->size * rspq->iqe_len,
2388c6e0d914SCasey Leedom 				  rspq->desc, rspq->phys_addr);
2389c6e0d914SCasey Leedom 		rspq->desc = NULL;
2390c6e0d914SCasey Leedom 	}
2391c6e0d914SCasey Leedom 	if (fl && fl->desc) {
2392c6e0d914SCasey Leedom 		kfree(fl->sdesc);
2393c6e0d914SCasey Leedom 		fl->sdesc = NULL;
2394c6e0d914SCasey Leedom 		dma_free_coherent(adapter->pdev_dev, flsz * EQ_UNIT,
2395c6e0d914SCasey Leedom 				  fl->desc, fl->addr);
2396c6e0d914SCasey Leedom 		fl->desc = NULL;
2397c6e0d914SCasey Leedom 	}
2398c6e0d914SCasey Leedom 	return ret;
2399c6e0d914SCasey Leedom }
2400c6e0d914SCasey Leedom 
2401c6e0d914SCasey Leedom /**
2402c6e0d914SCasey Leedom  *	t4vf_sge_alloc_eth_txq - allocate an SGE Ethernet TX Queue
2403c6e0d914SCasey Leedom  *	@adapter: the adapter
2404c6e0d914SCasey Leedom  *	@txq: pointer to the new txq to be filled in
240520bb0c8fSRahul Lakkireddy  *	@dev: the network device
2406c6e0d914SCasey Leedom  *	@devq: the network TX queue associated with the new txq
2407c6e0d914SCasey Leedom  *	@iqid: the relative ingress queue ID to which events relating to
2408c6e0d914SCasey Leedom  *		the new txq should be directed
2409c6e0d914SCasey Leedom  */
t4vf_sge_alloc_eth_txq(struct adapter * adapter,struct sge_eth_txq * txq,struct net_device * dev,struct netdev_queue * devq,unsigned int iqid)2410c6e0d914SCasey Leedom int t4vf_sge_alloc_eth_txq(struct adapter *adapter, struct sge_eth_txq *txq,
2411c6e0d914SCasey Leedom 			   struct net_device *dev, struct netdev_queue *devq,
2412c6e0d914SCasey Leedom 			   unsigned int iqid)
2413c6e0d914SCasey Leedom {
2414d429005fSVishal Kulkarni 	unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
2415d429005fSVishal Kulkarni 	struct port_info *pi = netdev_priv(dev);
2416d429005fSVishal Kulkarni 	struct fw_eq_eth_cmd cmd, rpl;
241765f6ecc9SHariprasad Shenai 	struct sge *s = &adapter->sge;
2418c6e0d914SCasey Leedom 	int ret, nentries;
2419c6e0d914SCasey Leedom 
2420c6e0d914SCasey Leedom 	/*
2421caedda35SCasey Leedom 	 * Calculate the size of the hardware TX Queue (including the Status
2422caedda35SCasey Leedom 	 * Page on the end of the TX Queue) in units of TX Descriptors.
2423c6e0d914SCasey Leedom 	 */
242465f6ecc9SHariprasad Shenai 	nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
2425c6e0d914SCasey Leedom 
2426c6e0d914SCasey Leedom 	/*
2427c6e0d914SCasey Leedom 	 * Allocate the hardware ring for the TX ring (with space for its
2428c6e0d914SCasey Leedom 	 * status page) along with the associated software descriptor ring.
2429c6e0d914SCasey Leedom 	 */
2430c6e0d914SCasey Leedom 	txq->q.desc = alloc_ring(adapter->pdev_dev, txq->q.size,
2431c6e0d914SCasey Leedom 				 sizeof(struct tx_desc),
2432c6e0d914SCasey Leedom 				 sizeof(struct tx_sw_desc),
243365f6ecc9SHariprasad Shenai 				 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len);
2434c6e0d914SCasey Leedom 	if (!txq->q.desc)
2435c6e0d914SCasey Leedom 		return -ENOMEM;
2436c6e0d914SCasey Leedom 
2437c6e0d914SCasey Leedom 	/*
2438c6e0d914SCasey Leedom 	 * Fill in the Egress Queue Command.  Note: As with the direct use of
2439c6e0d914SCasey Leedom 	 * the firmware Ingress Queue COmmand above in our RXQ allocation
2440c6e0d914SCasey Leedom 	 * routine, ideally, this code would be in t4vf_hw.c.  Again, we'll
2441c6e0d914SCasey Leedom 	 * have to see if there's some reasonable way to parameterize it
2442c6e0d914SCasey Leedom 	 * into the common code ...
2443c6e0d914SCasey Leedom 	 */
2444c6e0d914SCasey Leedom 	memset(&cmd, 0, sizeof(cmd));
2445e2ac9628SHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_EQ_ETH_CMD) |
2446e2ac9628SHariprasad Shenai 				    FW_CMD_REQUEST_F |
2447e2ac9628SHariprasad Shenai 				    FW_CMD_WRITE_F |
2448e2ac9628SHariprasad Shenai 				    FW_CMD_EXEC_F);
24496e4b51a6SHariprasad Shenai 	cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_ALLOC_F |
24506e4b51a6SHariprasad Shenai 					 FW_EQ_ETH_CMD_EQSTART_F |
2451c6e0d914SCasey Leedom 					 FW_LEN16(cmd));
2452d429005fSVishal Kulkarni 	cmd.autoequiqe_to_viid = cpu_to_be32(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
24536e4b51a6SHariprasad Shenai 					     FW_EQ_ETH_CMD_VIID_V(pi->viid));
2454c6e0d914SCasey Leedom 	cmd.fetchszm_to_iqid =
24556e4b51a6SHariprasad Shenai 		cpu_to_be32(FW_EQ_ETH_CMD_HOSTFCMODE_V(SGE_HOSTFCMODE_STPG) |
24566e4b51a6SHariprasad Shenai 			    FW_EQ_ETH_CMD_PCIECHN_V(pi->port_id) |
24576e4b51a6SHariprasad Shenai 			    FW_EQ_ETH_CMD_IQID_V(iqid));
2458c6e0d914SCasey Leedom 	cmd.dcaen_to_eqsize =
2459d429005fSVishal Kulkarni 		cpu_to_be32(FW_EQ_ETH_CMD_FBMIN_V(chip_ver <= CHELSIO_T5
2460d429005fSVishal Kulkarni 						  ? FETCHBURSTMIN_64B_X
2461d429005fSVishal Kulkarni 						  : FETCHBURSTMIN_64B_T6_X) |
2462d429005fSVishal Kulkarni 			    FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
24636e4b51a6SHariprasad Shenai 			    FW_EQ_ETH_CMD_CIDXFTHRESH_V(
2464d429005fSVishal Kulkarni 						CIDXFLUSHTHRESH_32_X) |
24656e4b51a6SHariprasad Shenai 			    FW_EQ_ETH_CMD_EQSIZE_V(nentries));
2466c6e0d914SCasey Leedom 	cmd.eqaddr = cpu_to_be64(txq->q.phys_addr);
2467c6e0d914SCasey Leedom 
2468c6e0d914SCasey Leedom 	/*
2469c6e0d914SCasey Leedom 	 * Issue the firmware Egress Queue Command and extract the results if
2470c6e0d914SCasey Leedom 	 * it completes successfully.
2471c6e0d914SCasey Leedom 	 */
2472c6e0d914SCasey Leedom 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
2473c6e0d914SCasey Leedom 	if (ret) {
2474c6e0d914SCasey Leedom 		/*
2475c6e0d914SCasey Leedom 		 * The girmware Ingress Queue Command failed for some reason.
2476c6e0d914SCasey Leedom 		 * Free up our partial allocation state and return the error.
2477c6e0d914SCasey Leedom 		 */
2478c6e0d914SCasey Leedom 		kfree(txq->q.sdesc);
2479c6e0d914SCasey Leedom 		txq->q.sdesc = NULL;
2480c6e0d914SCasey Leedom 		dma_free_coherent(adapter->pdev_dev,
2481c6e0d914SCasey Leedom 				  nentries * sizeof(struct tx_desc),
2482c6e0d914SCasey Leedom 				  txq->q.desc, txq->q.phys_addr);
2483c6e0d914SCasey Leedom 		txq->q.desc = NULL;
2484c6e0d914SCasey Leedom 		return ret;
2485c6e0d914SCasey Leedom 	}
2486c6e0d914SCasey Leedom 
2487c6e0d914SCasey Leedom 	txq->q.in_use = 0;
2488c6e0d914SCasey Leedom 	txq->q.cidx = 0;
2489c6e0d914SCasey Leedom 	txq->q.pidx = 0;
2490c6e0d914SCasey Leedom 	txq->q.stat = (void *)&txq->q.desc[txq->q.size];
24916e4b51a6SHariprasad Shenai 	txq->q.cntxt_id = FW_EQ_ETH_CMD_EQID_G(be32_to_cpu(rpl.eqid_pkd));
2492df64e4d3SHariprasad Shenai 	txq->q.bar2_addr = bar2_address(adapter,
2493df64e4d3SHariprasad Shenai 					txq->q.cntxt_id,
2494df64e4d3SHariprasad Shenai 					T4_BAR2_QTYPE_EGRESS,
2495df64e4d3SHariprasad Shenai 					&txq->q.bar2_qid);
2496c6e0d914SCasey Leedom 	txq->q.abs_id =
24976e4b51a6SHariprasad Shenai 		FW_EQ_ETH_CMD_PHYSEQID_G(be32_to_cpu(rpl.physeqid_pkd));
2498c6e0d914SCasey Leedom 	txq->txq = devq;
2499c6e0d914SCasey Leedom 	txq->tso = 0;
2500c6e0d914SCasey Leedom 	txq->tx_cso = 0;
2501c6e0d914SCasey Leedom 	txq->vlan_ins = 0;
2502c6e0d914SCasey Leedom 	txq->q.stops = 0;
2503c6e0d914SCasey Leedom 	txq->q.restarts = 0;
2504c6e0d914SCasey Leedom 	txq->mapping_err = 0;
2505c6e0d914SCasey Leedom 	return 0;
2506c6e0d914SCasey Leedom }
2507c6e0d914SCasey Leedom 
2508c6e0d914SCasey Leedom /*
2509c6e0d914SCasey Leedom  * Free the DMA map resources associated with a TX queue.
2510c6e0d914SCasey Leedom  */
free_txq(struct adapter * adapter,struct sge_txq * tq)2511c6e0d914SCasey Leedom static void free_txq(struct adapter *adapter, struct sge_txq *tq)
2512c6e0d914SCasey Leedom {
251365f6ecc9SHariprasad Shenai 	struct sge *s = &adapter->sge;
251465f6ecc9SHariprasad Shenai 
2515c6e0d914SCasey Leedom 	dma_free_coherent(adapter->pdev_dev,
251665f6ecc9SHariprasad Shenai 			  tq->size * sizeof(*tq->desc) + s->stat_len,
2517c6e0d914SCasey Leedom 			  tq->desc, tq->phys_addr);
2518c6e0d914SCasey Leedom 	tq->cntxt_id = 0;
2519c6e0d914SCasey Leedom 	tq->sdesc = NULL;
2520c6e0d914SCasey Leedom 	tq->desc = NULL;
2521c6e0d914SCasey Leedom }
2522c6e0d914SCasey Leedom 
2523c6e0d914SCasey Leedom /*
2524c6e0d914SCasey Leedom  * Free the resources associated with a response queue (possibly including a
2525c6e0d914SCasey Leedom  * free list).
2526c6e0d914SCasey Leedom  */
free_rspq_fl(struct adapter * adapter,struct sge_rspq * rspq,struct sge_fl * fl)2527c6e0d914SCasey Leedom static void free_rspq_fl(struct adapter *adapter, struct sge_rspq *rspq,
2528c6e0d914SCasey Leedom 			 struct sge_fl *fl)
2529c6e0d914SCasey Leedom {
253065f6ecc9SHariprasad Shenai 	struct sge *s = &adapter->sge;
2531c6e0d914SCasey Leedom 	unsigned int flid = fl ? fl->cntxt_id : 0xffff;
2532c6e0d914SCasey Leedom 
2533c6e0d914SCasey Leedom 	t4vf_iq_free(adapter, FW_IQ_TYPE_FL_INT_CAP,
2534c6e0d914SCasey Leedom 		     rspq->cntxt_id, flid, 0xffff);
2535c6e0d914SCasey Leedom 	dma_free_coherent(adapter->pdev_dev, (rspq->size + 1) * rspq->iqe_len,
2536c6e0d914SCasey Leedom 			  rspq->desc, rspq->phys_addr);
2537c6e0d914SCasey Leedom 	netif_napi_del(&rspq->napi);
2538c6e0d914SCasey Leedom 	rspq->netdev = NULL;
2539c6e0d914SCasey Leedom 	rspq->cntxt_id = 0;
2540c6e0d914SCasey Leedom 	rspq->abs_id = 0;
2541c6e0d914SCasey Leedom 	rspq->desc = NULL;
2542c6e0d914SCasey Leedom 
2543c6e0d914SCasey Leedom 	if (fl) {
2544c6e0d914SCasey Leedom 		free_rx_bufs(adapter, fl, fl->avail);
2545c6e0d914SCasey Leedom 		dma_free_coherent(adapter->pdev_dev,
254665f6ecc9SHariprasad Shenai 				  fl->size * sizeof(*fl->desc) + s->stat_len,
2547c6e0d914SCasey Leedom 				  fl->desc, fl->addr);
2548c6e0d914SCasey Leedom 		kfree(fl->sdesc);
2549c6e0d914SCasey Leedom 		fl->sdesc = NULL;
2550c6e0d914SCasey Leedom 		fl->cntxt_id = 0;
2551c6e0d914SCasey Leedom 		fl->desc = NULL;
2552c6e0d914SCasey Leedom 	}
2553c6e0d914SCasey Leedom }
2554c6e0d914SCasey Leedom 
2555c6e0d914SCasey Leedom /**
2556c6e0d914SCasey Leedom  *	t4vf_free_sge_resources - free SGE resources
2557c6e0d914SCasey Leedom  *	@adapter: the adapter
2558c6e0d914SCasey Leedom  *
2559c6e0d914SCasey Leedom  *	Frees resources used by the SGE queue sets.
2560c6e0d914SCasey Leedom  */
t4vf_free_sge_resources(struct adapter * adapter)2561c6e0d914SCasey Leedom void t4vf_free_sge_resources(struct adapter *adapter)
2562c6e0d914SCasey Leedom {
2563c6e0d914SCasey Leedom 	struct sge *s = &adapter->sge;
2564c6e0d914SCasey Leedom 	struct sge_eth_rxq *rxq = s->ethrxq;
2565c6e0d914SCasey Leedom 	struct sge_eth_txq *txq = s->ethtxq;
2566c6e0d914SCasey Leedom 	struct sge_rspq *evtq = &s->fw_evtq;
2567c6e0d914SCasey Leedom 	struct sge_rspq *intrq = &s->intrq;
2568c6e0d914SCasey Leedom 	int qs;
2569c6e0d914SCasey Leedom 
2570b97d13a5SCasey Leedom 	for (qs = 0; qs < adapter->sge.ethqsets; qs++, rxq++, txq++) {
2571c6e0d914SCasey Leedom 		if (rxq->rspq.desc)
2572c6e0d914SCasey Leedom 			free_rspq_fl(adapter, &rxq->rspq, &rxq->fl);
2573c6e0d914SCasey Leedom 		if (txq->q.desc) {
2574c6e0d914SCasey Leedom 			t4vf_eth_eq_free(adapter, txq->q.cntxt_id);
2575c6e0d914SCasey Leedom 			free_tx_desc(adapter, &txq->q, txq->q.in_use, true);
2576c6e0d914SCasey Leedom 			kfree(txq->q.sdesc);
2577c6e0d914SCasey Leedom 			free_txq(adapter, &txq->q);
2578c6e0d914SCasey Leedom 		}
2579c6e0d914SCasey Leedom 	}
2580c6e0d914SCasey Leedom 	if (evtq->desc)
2581c6e0d914SCasey Leedom 		free_rspq_fl(adapter, evtq, NULL);
2582c6e0d914SCasey Leedom 	if (intrq->desc)
2583c6e0d914SCasey Leedom 		free_rspq_fl(adapter, intrq, NULL);
2584c6e0d914SCasey Leedom }
2585c6e0d914SCasey Leedom 
2586c6e0d914SCasey Leedom /**
2587c6e0d914SCasey Leedom  *	t4vf_sge_start - enable SGE operation
2588c6e0d914SCasey Leedom  *	@adapter: the adapter
2589c6e0d914SCasey Leedom  *
2590c6e0d914SCasey Leedom  *	Start tasklets and timers associated with the DMA engine.
2591c6e0d914SCasey Leedom  */
t4vf_sge_start(struct adapter * adapter)2592c6e0d914SCasey Leedom void t4vf_sge_start(struct adapter *adapter)
2593c6e0d914SCasey Leedom {
2594c6e0d914SCasey Leedom 	adapter->sge.ethtxq_rover = 0;
2595c6e0d914SCasey Leedom 	mod_timer(&adapter->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
2596c6e0d914SCasey Leedom 	mod_timer(&adapter->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
2597c6e0d914SCasey Leedom }
2598c6e0d914SCasey Leedom 
2599c6e0d914SCasey Leedom /**
2600c6e0d914SCasey Leedom  *	t4vf_sge_stop - disable SGE operation
2601c6e0d914SCasey Leedom  *	@adapter: the adapter
2602c6e0d914SCasey Leedom  *
2603c6e0d914SCasey Leedom  *	Stop tasklets and timers associated with the DMA engine.  Note that
2604c6e0d914SCasey Leedom  *	this is effective only if measures have been taken to disable any HW
2605c6e0d914SCasey Leedom  *	events that may restart them.
2606c6e0d914SCasey Leedom  */
t4vf_sge_stop(struct adapter * adapter)2607c6e0d914SCasey Leedom void t4vf_sge_stop(struct adapter *adapter)
2608c6e0d914SCasey Leedom {
2609c6e0d914SCasey Leedom 	struct sge *s = &adapter->sge;
2610c6e0d914SCasey Leedom 
2611c6e0d914SCasey Leedom 	if (s->rx_timer.function)
26128fa7292fSThomas Gleixner 		timer_delete_sync(&s->rx_timer);
2613c6e0d914SCasey Leedom 	if (s->tx_timer.function)
26148fa7292fSThomas Gleixner 		timer_delete_sync(&s->tx_timer);
2615c6e0d914SCasey Leedom }
2616c6e0d914SCasey Leedom 
2617c6e0d914SCasey Leedom /**
2618c6e0d914SCasey Leedom  *	t4vf_sge_init - initialize SGE
2619c6e0d914SCasey Leedom  *	@adapter: the adapter
2620c6e0d914SCasey Leedom  *
2621c6e0d914SCasey Leedom  *	Performs SGE initialization needed every time after a chip reset.
2622c6e0d914SCasey Leedom  *	We do not initialize any of the queue sets here, instead the driver
2623c6e0d914SCasey Leedom  *	top-level must request those individually.  We also do not enable DMA
2624c6e0d914SCasey Leedom  *	here, that should be done after the queues have been set up.
2625c6e0d914SCasey Leedom  */
t4vf_sge_init(struct adapter * adapter)2626c6e0d914SCasey Leedom int t4vf_sge_init(struct adapter *adapter)
2627c6e0d914SCasey Leedom {
2628c6e0d914SCasey Leedom 	struct sge_params *sge_params = &adapter->params.sge;
2629ea0a4210SArjun Vynipadath 	u32 fl_small_pg = sge_params->sge_fl_buffer_size[0];
2630ea0a4210SArjun Vynipadath 	u32 fl_large_pg = sge_params->sge_fl_buffer_size[1];
2631c6e0d914SCasey Leedom 	struct sge *s = &adapter->sge;
2632c6e0d914SCasey Leedom 
2633c6e0d914SCasey Leedom 	/*
2634c6e0d914SCasey Leedom 	 * Start by vetting the basic SGE parameters which have been set up by
2635c6e0d914SCasey Leedom 	 * the Physical Function Driver.  Ideally we should be able to deal
2636c6e0d914SCasey Leedom 	 * with _any_ configuration.  Practice is different ...
2637c6e0d914SCasey Leedom 	 */
2638ea0a4210SArjun Vynipadath 
2639ea0a4210SArjun Vynipadath 	/* We only bother using the Large Page logic if the Large Page Buffer
2640ea0a4210SArjun Vynipadath 	 * is larger than our Page Size Buffer.
2641ea0a4210SArjun Vynipadath 	 */
2642ea0a4210SArjun Vynipadath 	if (fl_large_pg <= fl_small_pg)
2643ea0a4210SArjun Vynipadath 		fl_large_pg = 0;
2644ea0a4210SArjun Vynipadath 
2645ea0a4210SArjun Vynipadath 	/* The Page Size Buffer must be exactly equal to our Page Size and the
2646ea0a4210SArjun Vynipadath 	 * Large Page Size Buffer should be 0 (per above) or a power of 2.
2647ea0a4210SArjun Vynipadath 	 */
2648ea0a4210SArjun Vynipadath 	if (fl_small_pg != PAGE_SIZE ||
2649ea0a4210SArjun Vynipadath 	    (fl_large_pg & (fl_large_pg - 1)) != 0) {
2650c6e0d914SCasey Leedom 		dev_err(adapter->pdev_dev, "bad SGE FL buffer sizes [%d, %d]\n",
2651ea0a4210SArjun Vynipadath 			fl_small_pg, fl_large_pg);
2652c6e0d914SCasey Leedom 		return -EINVAL;
2653c6e0d914SCasey Leedom 	}
2654cb440364SHariprasad Shenai 	if ((sge_params->sge_control & RXPKTCPLMODE_F) !=
2655cb440364SHariprasad Shenai 	    RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
2656c6e0d914SCasey Leedom 		dev_err(adapter->pdev_dev, "bad SGE CPL MODE\n");
2657c6e0d914SCasey Leedom 		return -EINVAL;
2658c6e0d914SCasey Leedom 	}
2659c6e0d914SCasey Leedom 
2660c6e0d914SCasey Leedom 	/*
2661c6e0d914SCasey Leedom 	 * Now translate the adapter parameters into our internal forms.
2662c6e0d914SCasey Leedom 	 */
2663ea0a4210SArjun Vynipadath 	if (fl_large_pg)
2664ea0a4210SArjun Vynipadath 		s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
2665f612b815SHariprasad Shenai 	s->stat_len = ((sge_params->sge_control & EGRSTATUSPAGESIZE_F)
266652367a76SVipul Pandya 			? 128 : 64);
2667f612b815SHariprasad Shenai 	s->pktshift = PKTSHIFT_G(sge_params->sge_control);
2668cb440364SHariprasad Shenai 	s->fl_align = t4vf_fl_pkt_align(adapter);
2669c6e0d914SCasey Leedom 
267050d21a66SHariprasad Shenai 	/* A FL with <= fl_starve_thres buffers is starving and a periodic
267150d21a66SHariprasad Shenai 	 * timer will attempt to refill it.  This needs to be larger than the
267250d21a66SHariprasad Shenai 	 * SGE's Egress Congestion Threshold.  If it isn't, then we can get
267350d21a66SHariprasad Shenai 	 * stuck waiting for new packets while the SGE is waiting for us to
267450d21a66SHariprasad Shenai 	 * give it more Free List entries.  (Note that the SGE's Egress
267550d21a66SHariprasad Shenai 	 * Congestion Threshold is in units of 2 Free List pointers.)
267650d21a66SHariprasad Shenai 	 */
2677ea6f82feSHariprasad Shenai 	switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
2678ea6f82feSHariprasad Shenai 	case CHELSIO_T4:
2679ea6f82feSHariprasad Shenai 		s->fl_starve_thres =
2680ea6f82feSHariprasad Shenai 		   EGRTHRESHOLD_G(sge_params->sge_congestion_control);
2681ea6f82feSHariprasad Shenai 		break;
2682ea6f82feSHariprasad Shenai 	case CHELSIO_T5:
2683ea6f82feSHariprasad Shenai 		s->fl_starve_thres =
2684ea6f82feSHariprasad Shenai 		   EGRTHRESHOLDPACKING_G(sge_params->sge_congestion_control);
2685ea6f82feSHariprasad Shenai 		break;
2686ea6f82feSHariprasad Shenai 	case CHELSIO_T6:
2687ea6f82feSHariprasad Shenai 	default:
2688ea6f82feSHariprasad Shenai 		s->fl_starve_thres =
2689ea6f82feSHariprasad Shenai 		   T6_EGRTHRESHOLDPACKING_G(sge_params->sge_congestion_control);
2690ea6f82feSHariprasad Shenai 		break;
2691ea6f82feSHariprasad Shenai 	}
2692ea6f82feSHariprasad Shenai 	s->fl_starve_thres = s->fl_starve_thres * 2 + 1;
2693c6e0d914SCasey Leedom 
2694c6e0d914SCasey Leedom 	/*
2695c6e0d914SCasey Leedom 	 * Set up tasklet timers.
2696c6e0d914SCasey Leedom 	 */
26970e23daebSKees Cook 	timer_setup(&s->rx_timer, sge_rx_timer_cb, 0);
26980e23daebSKees Cook 	timer_setup(&s->tx_timer, sge_tx_timer_cb, 0);
2699c6e0d914SCasey Leedom 
2700c6e0d914SCasey Leedom 	/*
2701c6e0d914SCasey Leedom 	 * Initialize Forwarded Interrupt Queue lock.
2702c6e0d914SCasey Leedom 	 */
2703c6e0d914SCasey Leedom 	spin_lock_init(&s->intrq_lock);
2704c6e0d914SCasey Leedom 
2705c6e0d914SCasey Leedom 	return 0;
2706c6e0d914SCasey Leedom }
2707