162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
362306a36Sopenharmony_ci * driver for Linux.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * This software is available to you under a choice of one of two
862306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
962306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
1062306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
1162306a36Sopenharmony_ci * OpenIB.org BSD license below:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1462306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1562306a36Sopenharmony_ci *     conditions are met:
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1862306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1962306a36Sopenharmony_ci *        disclaimer.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2262306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2362306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2462306a36Sopenharmony_ci *        provided with the distribution.
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2762306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2862306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2962306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
3062306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3162306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3262306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3362306a36Sopenharmony_ci * SOFTWARE.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#include <linux/skbuff.h>
3762306a36Sopenharmony_ci#include <linux/netdevice.h>
3862306a36Sopenharmony_ci#include <linux/etherdevice.h>
3962306a36Sopenharmony_ci#include <linux/if_vlan.h>
4062306a36Sopenharmony_ci#include <linux/ip.h>
4162306a36Sopenharmony_ci#include <net/ipv6.h>
4262306a36Sopenharmony_ci#include <net/tcp.h>
4362306a36Sopenharmony_ci#include <linux/dma-mapping.h>
4462306a36Sopenharmony_ci#include <linux/prefetch.h>
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#include "t4vf_common.h"
4762306a36Sopenharmony_ci#include "t4vf_defs.h"
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#include "../cxgb4/t4_regs.h"
5062306a36Sopenharmony_ci#include "../cxgb4/t4_values.h"
5162306a36Sopenharmony_ci#include "../cxgb4/t4fw_api.h"
5262306a36Sopenharmony_ci#include "../cxgb4/t4_msg.h"
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/*
5562306a36Sopenharmony_ci * Constants ...
5662306a36Sopenharmony_ci */
5762306a36Sopenharmony_cienum {
5862306a36Sopenharmony_ci	/*
5962306a36Sopenharmony_ci	 * Egress Queue sizes, producer and consumer indices are all in units
6062306a36Sopenharmony_ci	 * of Egress Context Units bytes.  Note that as far as the hardware is
6162306a36Sopenharmony_ci	 * concerned, the free list is an Egress Queue (the host produces free
6262306a36Sopenharmony_ci	 * buffers which the hardware consumes) and free list entries are
6362306a36Sopenharmony_ci	 * 64-bit PCI DMA addresses.
6462306a36Sopenharmony_ci	 */
6562306a36Sopenharmony_ci	EQ_UNIT = SGE_EQ_IDXSIZE,
6662306a36Sopenharmony_ci	FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
6762306a36Sopenharmony_ci	TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	/*
7062306a36Sopenharmony_ci	 * Max number of TX descriptors we clean up at a time.  Should be
7162306a36Sopenharmony_ci	 * modest as freeing skbs isn't cheap and it happens while holding
7262306a36Sopenharmony_ci	 * locks.  We just need to free packets faster than they arrive, we
7362306a36Sopenharmony_ci	 * eventually catch up and keep the amortized cost reasonable.
7462306a36Sopenharmony_ci	 */
7562306a36Sopenharmony_ci	MAX_TX_RECLAIM = 16,
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	/*
7862306a36Sopenharmony_ci	 * Max number of Rx buffers we replenish at a time.  Again keep this
7962306a36Sopenharmony_ci	 * modest, allocating buffers isn't cheap either.
8062306a36Sopenharmony_ci	 */
8162306a36Sopenharmony_ci	MAX_RX_REFILL = 16,
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	/*
8462306a36Sopenharmony_ci	 * Period of the Rx queue check timer.  This timer is infrequent as it
8562306a36Sopenharmony_ci	 * has something to do only when the system experiences severe memory
8662306a36Sopenharmony_ci	 * shortage.
8762306a36Sopenharmony_ci	 */
8862306a36Sopenharmony_ci	RX_QCHECK_PERIOD = (HZ / 2),
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	/*
9162306a36Sopenharmony_ci	 * Period of the TX queue check timer and the maximum number of TX
9262306a36Sopenharmony_ci	 * descriptors to be reclaimed by the TX timer.
9362306a36Sopenharmony_ci	 */
9462306a36Sopenharmony_ci	TX_QCHECK_PERIOD = (HZ / 2),
9562306a36Sopenharmony_ci	MAX_TIMER_TX_RECLAIM = 100,
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/*
9862306a36Sopenharmony_ci	 * Suspend an Ethernet TX queue with fewer available descriptors than
9962306a36Sopenharmony_ci	 * this.  We always want to have room for a maximum sized packet:
10062306a36Sopenharmony_ci	 * inline immediate data + MAX_SKB_FRAGS. This is the same as
10162306a36Sopenharmony_ci	 * calc_tx_flits() for a TSO packet with nr_frags == MAX_SKB_FRAGS
10262306a36Sopenharmony_ci	 * (see that function and its helpers for a description of the
10362306a36Sopenharmony_ci	 * calculation).
10462306a36Sopenharmony_ci	 */
10562306a36Sopenharmony_ci	ETHTXQ_MAX_FRAGS = MAX_SKB_FRAGS + 1,
10662306a36Sopenharmony_ci	ETHTXQ_MAX_SGL_LEN = ((3 * (ETHTXQ_MAX_FRAGS-1))/2 +
10762306a36Sopenharmony_ci				   ((ETHTXQ_MAX_FRAGS-1) & 1) +
10862306a36Sopenharmony_ci				   2),
10962306a36Sopenharmony_ci	ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) +
11062306a36Sopenharmony_ci			  sizeof(struct cpl_tx_pkt_lso_core) +
11162306a36Sopenharmony_ci			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64),
11262306a36Sopenharmony_ci	ETHTXQ_MAX_FLITS = ETHTXQ_MAX_SGL_LEN + ETHTXQ_MAX_HDR,
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	ETHTXQ_STOP_THRES = 1 + DIV_ROUND_UP(ETHTXQ_MAX_FLITS, TXD_PER_EQ_UNIT),
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	/*
11762306a36Sopenharmony_ci	 * Max TX descriptor space we allow for an Ethernet packet to be
11862306a36Sopenharmony_ci	 * inlined into a WR.  This is limited by the maximum value which
11962306a36Sopenharmony_ci	 * we can specify for immediate data in the firmware Ethernet TX
12062306a36Sopenharmony_ci	 * Work Request.
12162306a36Sopenharmony_ci	 */
12262306a36Sopenharmony_ci	MAX_IMM_TX_PKT_LEN = FW_WR_IMMDLEN_M,
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	/*
12562306a36Sopenharmony_ci	 * Max size of a WR sent through a control TX queue.
12662306a36Sopenharmony_ci	 */
12762306a36Sopenharmony_ci	MAX_CTRL_WR_LEN = 256,
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	/*
13062306a36Sopenharmony_ci	 * Maximum amount of data which we'll ever need to inline into a
13162306a36Sopenharmony_ci	 * TX ring: max(MAX_IMM_TX_PKT_LEN, MAX_CTRL_WR_LEN).
13262306a36Sopenharmony_ci	 */
13362306a36Sopenharmony_ci	MAX_IMM_TX_LEN = (MAX_IMM_TX_PKT_LEN > MAX_CTRL_WR_LEN
13462306a36Sopenharmony_ci			  ? MAX_IMM_TX_PKT_LEN
13562306a36Sopenharmony_ci			  : MAX_CTRL_WR_LEN),
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	/*
13862306a36Sopenharmony_ci	 * For incoming packets less than RX_COPY_THRES, we copy the data into
13962306a36Sopenharmony_ci	 * an skb rather than referencing the data.  We allocate enough
14062306a36Sopenharmony_ci	 * in-line room in skb's to accommodate pulling in RX_PULL_LEN bytes
14162306a36Sopenharmony_ci	 * of the data (header).
14262306a36Sopenharmony_ci	 */
14362306a36Sopenharmony_ci	RX_COPY_THRES = 256,
14462306a36Sopenharmony_ci	RX_PULL_LEN = 128,
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	/*
14762306a36Sopenharmony_ci	 * Main body length for sk_buffs used for RX Ethernet packets with
14862306a36Sopenharmony_ci	 * fragments.  Should be >= RX_PULL_LEN but possibly bigger to give
14962306a36Sopenharmony_ci	 * pskb_may_pull() some room.
15062306a36Sopenharmony_ci	 */
15162306a36Sopenharmony_ci	RX_SKB_LEN = 512,
15262306a36Sopenharmony_ci};
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci/*
15562306a36Sopenharmony_ci * Software state per TX descriptor.
15662306a36Sopenharmony_ci */
15762306a36Sopenharmony_cistruct tx_sw_desc {
15862306a36Sopenharmony_ci	struct sk_buff *skb;		/* socket buffer of TX data source */
15962306a36Sopenharmony_ci	struct ulptx_sgl *sgl;		/* scatter/gather list in TX Queue */
16062306a36Sopenharmony_ci};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci/*
16362306a36Sopenharmony_ci * Software state per RX Free List descriptor.  We keep track of the allocated
16462306a36Sopenharmony_ci * FL page, its size, and its PCI DMA address (if the page is mapped).  The FL
16562306a36Sopenharmony_ci * page size and its PCI DMA mapped state are stored in the low bits of the
16662306a36Sopenharmony_ci * PCI DMA address as per below.
16762306a36Sopenharmony_ci */
16862306a36Sopenharmony_cistruct rx_sw_desc {
16962306a36Sopenharmony_ci	struct page *page;		/* Free List page buffer */
17062306a36Sopenharmony_ci	dma_addr_t dma_addr;		/* PCI DMA address (if mapped) */
17162306a36Sopenharmony_ci					/*   and flags (see below) */
17262306a36Sopenharmony_ci};
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci/*
17562306a36Sopenharmony_ci * The low bits of rx_sw_desc.dma_addr have special meaning.  Note that the
17662306a36Sopenharmony_ci * SGE also uses the low 4 bits to determine the size of the buffer.  It uses
17762306a36Sopenharmony_ci * those bits to index into the SGE_FL_BUFFER_SIZE[index] register array.
17862306a36Sopenharmony_ci * Since we only use SGE_FL_BUFFER_SIZE0 and SGE_FL_BUFFER_SIZE1, these low 4
17962306a36Sopenharmony_ci * bits can only contain a 0 or a 1 to indicate which size buffer we're giving
18062306a36Sopenharmony_ci * to the SGE.  Thus, our software state of "is the buffer mapped for DMA" is
18162306a36Sopenharmony_ci * maintained in an inverse sense so the hardware never sees that bit high.
18262306a36Sopenharmony_ci */
18362306a36Sopenharmony_cienum {
18462306a36Sopenharmony_ci	RX_LARGE_BUF    = 1 << 0,	/* buffer is SGE_FL_BUFFER_SIZE[1] */
18562306a36Sopenharmony_ci	RX_UNMAPPED_BUF = 1 << 1,	/* buffer is not mapped */
18662306a36Sopenharmony_ci};
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci/**
18962306a36Sopenharmony_ci *	get_buf_addr - return DMA buffer address of software descriptor
19062306a36Sopenharmony_ci *	@sdesc: pointer to the software buffer descriptor
19162306a36Sopenharmony_ci *
19262306a36Sopenharmony_ci *	Return the DMA buffer address of a software descriptor (stripping out
19362306a36Sopenharmony_ci *	our low-order flag bits).
19462306a36Sopenharmony_ci */
19562306a36Sopenharmony_cistatic inline dma_addr_t get_buf_addr(const struct rx_sw_desc *sdesc)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	return sdesc->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF);
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci/**
20162306a36Sopenharmony_ci *	is_buf_mapped - is buffer mapped for DMA?
20262306a36Sopenharmony_ci *	@sdesc: pointer to the software buffer descriptor
20362306a36Sopenharmony_ci *
20462306a36Sopenharmony_ci *	Determine whether the buffer associated with a software descriptor in
20562306a36Sopenharmony_ci *	mapped for DMA or not.
20662306a36Sopenharmony_ci */
20762306a36Sopenharmony_cistatic inline bool is_buf_mapped(const struct rx_sw_desc *sdesc)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	return !(sdesc->dma_addr & RX_UNMAPPED_BUF);
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci/**
21362306a36Sopenharmony_ci *	need_skb_unmap - does the platform need unmapping of sk_buffs?
21462306a36Sopenharmony_ci *
21562306a36Sopenharmony_ci *	Returns true if the platform needs sk_buff unmapping.  The compiler
21662306a36Sopenharmony_ci *	optimizes away unnecessary code if this returns true.
21762306a36Sopenharmony_ci */
21862306a36Sopenharmony_cistatic inline int need_skb_unmap(void)
21962306a36Sopenharmony_ci{
22062306a36Sopenharmony_ci#ifdef CONFIG_NEED_DMA_MAP_STATE
22162306a36Sopenharmony_ci	return 1;
22262306a36Sopenharmony_ci#else
22362306a36Sopenharmony_ci	return 0;
22462306a36Sopenharmony_ci#endif
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci/**
22862306a36Sopenharmony_ci *	txq_avail - return the number of available slots in a TX queue
22962306a36Sopenharmony_ci *	@tq: the TX queue
23062306a36Sopenharmony_ci *
23162306a36Sopenharmony_ci *	Returns the number of available descriptors in a TX queue.
23262306a36Sopenharmony_ci */
23362306a36Sopenharmony_cistatic inline unsigned int txq_avail(const struct sge_txq *tq)
23462306a36Sopenharmony_ci{
23562306a36Sopenharmony_ci	return tq->size - 1 - tq->in_use;
23662306a36Sopenharmony_ci}
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci/**
23962306a36Sopenharmony_ci *	fl_cap - return the capacity of a Free List
24062306a36Sopenharmony_ci *	@fl: the Free List
24162306a36Sopenharmony_ci *
24262306a36Sopenharmony_ci *	Returns the capacity of a Free List.  The capacity is less than the
24362306a36Sopenharmony_ci *	size because an Egress Queue Index Unit worth of descriptors needs to
24462306a36Sopenharmony_ci *	be left unpopulated, otherwise the Producer and Consumer indices PIDX
24562306a36Sopenharmony_ci *	and CIDX will match and the hardware will think the FL is empty.
24662306a36Sopenharmony_ci */
24762306a36Sopenharmony_cistatic inline unsigned int fl_cap(const struct sge_fl *fl)
24862306a36Sopenharmony_ci{
24962306a36Sopenharmony_ci	return fl->size - FL_PER_EQ_UNIT;
25062306a36Sopenharmony_ci}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci/**
25362306a36Sopenharmony_ci *	fl_starving - return whether a Free List is starving.
25462306a36Sopenharmony_ci *	@adapter: pointer to the adapter
25562306a36Sopenharmony_ci *	@fl: the Free List
25662306a36Sopenharmony_ci *
25762306a36Sopenharmony_ci *	Tests specified Free List to see whether the number of buffers
25862306a36Sopenharmony_ci *	available to the hardware has falled below our "starvation"
25962306a36Sopenharmony_ci *	threshold.
26062306a36Sopenharmony_ci */
26162306a36Sopenharmony_cistatic inline bool fl_starving(const struct adapter *adapter,
26262306a36Sopenharmony_ci			       const struct sge_fl *fl)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	const struct sge *s = &adapter->sge;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	return fl->avail - fl->pend_cred <= s->fl_starve_thres;
26762306a36Sopenharmony_ci}
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci/**
27062306a36Sopenharmony_ci *	map_skb -  map an skb for DMA to the device
27162306a36Sopenharmony_ci *	@dev: the egress net device
27262306a36Sopenharmony_ci *	@skb: the packet to map
27362306a36Sopenharmony_ci *	@addr: a pointer to the base of the DMA mapping array
27462306a36Sopenharmony_ci *
27562306a36Sopenharmony_ci *	Map an skb for DMA to the device and return an array of DMA addresses.
27662306a36Sopenharmony_ci */
27762306a36Sopenharmony_cistatic int map_skb(struct device *dev, const struct sk_buff *skb,
27862306a36Sopenharmony_ci		   dma_addr_t *addr)
27962306a36Sopenharmony_ci{
28062306a36Sopenharmony_ci	const skb_frag_t *fp, *end;
28162306a36Sopenharmony_ci	const struct skb_shared_info *si;
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	*addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
28462306a36Sopenharmony_ci	if (dma_mapping_error(dev, *addr))
28562306a36Sopenharmony_ci		goto out_err;
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	si = skb_shinfo(skb);
28862306a36Sopenharmony_ci	end = &si->frags[si->nr_frags];
28962306a36Sopenharmony_ci	for (fp = si->frags; fp < end; fp++) {
29062306a36Sopenharmony_ci		*++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
29162306a36Sopenharmony_ci					   DMA_TO_DEVICE);
29262306a36Sopenharmony_ci		if (dma_mapping_error(dev, *addr))
29362306a36Sopenharmony_ci			goto unwind;
29462306a36Sopenharmony_ci	}
29562306a36Sopenharmony_ci	return 0;
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ciunwind:
29862306a36Sopenharmony_ci	while (fp-- > si->frags)
29962306a36Sopenharmony_ci		dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
30062306a36Sopenharmony_ci	dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ciout_err:
30362306a36Sopenharmony_ci	return -ENOMEM;
30462306a36Sopenharmony_ci}
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_cistatic void unmap_sgl(struct device *dev, const struct sk_buff *skb,
30762306a36Sopenharmony_ci		      const struct ulptx_sgl *sgl, const struct sge_txq *tq)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	const struct ulptx_sge_pair *p;
31062306a36Sopenharmony_ci	unsigned int nfrags = skb_shinfo(skb)->nr_frags;
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	if (likely(skb_headlen(skb)))
31362306a36Sopenharmony_ci		dma_unmap_single(dev, be64_to_cpu(sgl->addr0),
31462306a36Sopenharmony_ci				 be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
31562306a36Sopenharmony_ci	else {
31662306a36Sopenharmony_ci		dma_unmap_page(dev, be64_to_cpu(sgl->addr0),
31762306a36Sopenharmony_ci			       be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
31862306a36Sopenharmony_ci		nfrags--;
31962306a36Sopenharmony_ci	}
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci	/*
32262306a36Sopenharmony_ci	 * the complexity below is because of the possibility of a wrap-around
32362306a36Sopenharmony_ci	 * in the middle of an SGL
32462306a36Sopenharmony_ci	 */
32562306a36Sopenharmony_ci	for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
32662306a36Sopenharmony_ci		if (likely((u8 *)(p + 1) <= (u8 *)tq->stat)) {
32762306a36Sopenharmony_ciunmap:
32862306a36Sopenharmony_ci			dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
32962306a36Sopenharmony_ci				       be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
33062306a36Sopenharmony_ci			dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
33162306a36Sopenharmony_ci				       be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
33262306a36Sopenharmony_ci			p++;
33362306a36Sopenharmony_ci		} else if ((u8 *)p == (u8 *)tq->stat) {
33462306a36Sopenharmony_ci			p = (const struct ulptx_sge_pair *)tq->desc;
33562306a36Sopenharmony_ci			goto unmap;
33662306a36Sopenharmony_ci		} else if ((u8 *)p + 8 == (u8 *)tq->stat) {
33762306a36Sopenharmony_ci			const __be64 *addr = (const __be64 *)tq->desc;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci			dma_unmap_page(dev, be64_to_cpu(addr[0]),
34062306a36Sopenharmony_ci				       be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
34162306a36Sopenharmony_ci			dma_unmap_page(dev, be64_to_cpu(addr[1]),
34262306a36Sopenharmony_ci				       be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
34362306a36Sopenharmony_ci			p = (const struct ulptx_sge_pair *)&addr[2];
34462306a36Sopenharmony_ci		} else {
34562306a36Sopenharmony_ci			const __be64 *addr = (const __be64 *)tq->desc;
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci			dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
34862306a36Sopenharmony_ci				       be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
34962306a36Sopenharmony_ci			dma_unmap_page(dev, be64_to_cpu(addr[0]),
35062306a36Sopenharmony_ci				       be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
35162306a36Sopenharmony_ci			p = (const struct ulptx_sge_pair *)&addr[1];
35262306a36Sopenharmony_ci		}
35362306a36Sopenharmony_ci	}
35462306a36Sopenharmony_ci	if (nfrags) {
35562306a36Sopenharmony_ci		__be64 addr;
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci		if ((u8 *)p == (u8 *)tq->stat)
35862306a36Sopenharmony_ci			p = (const struct ulptx_sge_pair *)tq->desc;
35962306a36Sopenharmony_ci		addr = ((u8 *)p + 16 <= (u8 *)tq->stat
36062306a36Sopenharmony_ci			? p->addr[0]
36162306a36Sopenharmony_ci			: *(const __be64 *)tq->desc);
36262306a36Sopenharmony_ci		dma_unmap_page(dev, be64_to_cpu(addr), be32_to_cpu(p->len[0]),
36362306a36Sopenharmony_ci			       DMA_TO_DEVICE);
36462306a36Sopenharmony_ci	}
36562306a36Sopenharmony_ci}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci/**
36862306a36Sopenharmony_ci *	free_tx_desc - reclaims TX descriptors and their buffers
36962306a36Sopenharmony_ci *	@adapter: the adapter
37062306a36Sopenharmony_ci *	@tq: the TX queue to reclaim descriptors from
37162306a36Sopenharmony_ci *	@n: the number of descriptors to reclaim
37262306a36Sopenharmony_ci *	@unmap: whether the buffers should be unmapped for DMA
37362306a36Sopenharmony_ci *
37462306a36Sopenharmony_ci *	Reclaims TX descriptors from an SGE TX queue and frees the associated
37562306a36Sopenharmony_ci *	TX buffers.  Called with the TX queue lock held.
37662306a36Sopenharmony_ci */
37762306a36Sopenharmony_cistatic void free_tx_desc(struct adapter *adapter, struct sge_txq *tq,
37862306a36Sopenharmony_ci			 unsigned int n, bool unmap)
37962306a36Sopenharmony_ci{
38062306a36Sopenharmony_ci	struct tx_sw_desc *sdesc;
38162306a36Sopenharmony_ci	unsigned int cidx = tq->cidx;
38262306a36Sopenharmony_ci	struct device *dev = adapter->pdev_dev;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	const int need_unmap = need_skb_unmap() && unmap;
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	sdesc = &tq->sdesc[cidx];
38762306a36Sopenharmony_ci	while (n--) {
38862306a36Sopenharmony_ci		/*
38962306a36Sopenharmony_ci		 * If we kept a reference to the original TX skb, we need to
39062306a36Sopenharmony_ci		 * unmap it from PCI DMA space (if required) and free it.
39162306a36Sopenharmony_ci		 */
39262306a36Sopenharmony_ci		if (sdesc->skb) {
39362306a36Sopenharmony_ci			if (need_unmap)
39462306a36Sopenharmony_ci				unmap_sgl(dev, sdesc->skb, sdesc->sgl, tq);
39562306a36Sopenharmony_ci			dev_consume_skb_any(sdesc->skb);
39662306a36Sopenharmony_ci			sdesc->skb = NULL;
39762306a36Sopenharmony_ci		}
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci		sdesc++;
40062306a36Sopenharmony_ci		if (++cidx == tq->size) {
40162306a36Sopenharmony_ci			cidx = 0;
40262306a36Sopenharmony_ci			sdesc = tq->sdesc;
40362306a36Sopenharmony_ci		}
40462306a36Sopenharmony_ci	}
40562306a36Sopenharmony_ci	tq->cidx = cidx;
40662306a36Sopenharmony_ci}
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci/*
40962306a36Sopenharmony_ci * Return the number of reclaimable descriptors in a TX queue.
41062306a36Sopenharmony_ci */
41162306a36Sopenharmony_cistatic inline int reclaimable(const struct sge_txq *tq)
41262306a36Sopenharmony_ci{
41362306a36Sopenharmony_ci	int hw_cidx = be16_to_cpu(tq->stat->cidx);
41462306a36Sopenharmony_ci	int reclaimable = hw_cidx - tq->cidx;
41562306a36Sopenharmony_ci	if (reclaimable < 0)
41662306a36Sopenharmony_ci		reclaimable += tq->size;
41762306a36Sopenharmony_ci	return reclaimable;
41862306a36Sopenharmony_ci}
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci/**
42162306a36Sopenharmony_ci *	reclaim_completed_tx - reclaims completed TX descriptors
42262306a36Sopenharmony_ci *	@adapter: the adapter
42362306a36Sopenharmony_ci *	@tq: the TX queue to reclaim completed descriptors from
42462306a36Sopenharmony_ci *	@unmap: whether the buffers should be unmapped for DMA
42562306a36Sopenharmony_ci *
42662306a36Sopenharmony_ci *	Reclaims TX descriptors that the SGE has indicated it has processed,
42762306a36Sopenharmony_ci *	and frees the associated buffers if possible.  Called with the TX
42862306a36Sopenharmony_ci *	queue locked.
42962306a36Sopenharmony_ci */
43062306a36Sopenharmony_cistatic inline void reclaim_completed_tx(struct adapter *adapter,
43162306a36Sopenharmony_ci					struct sge_txq *tq,
43262306a36Sopenharmony_ci					bool unmap)
43362306a36Sopenharmony_ci{
43462306a36Sopenharmony_ci	int avail = reclaimable(tq);
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci	if (avail) {
43762306a36Sopenharmony_ci		/*
43862306a36Sopenharmony_ci		 * Limit the amount of clean up work we do at a time to keep
43962306a36Sopenharmony_ci		 * the TX lock hold time O(1).
44062306a36Sopenharmony_ci		 */
44162306a36Sopenharmony_ci		if (avail > MAX_TX_RECLAIM)
44262306a36Sopenharmony_ci			avail = MAX_TX_RECLAIM;
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci		free_tx_desc(adapter, tq, avail, unmap);
44562306a36Sopenharmony_ci		tq->in_use -= avail;
44662306a36Sopenharmony_ci	}
44762306a36Sopenharmony_ci}
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci/**
45062306a36Sopenharmony_ci *	get_buf_size - return the size of an RX Free List buffer.
45162306a36Sopenharmony_ci *	@adapter: pointer to the associated adapter
45262306a36Sopenharmony_ci *	@sdesc: pointer to the software buffer descriptor
45362306a36Sopenharmony_ci */
45462306a36Sopenharmony_cistatic inline int get_buf_size(const struct adapter *adapter,
45562306a36Sopenharmony_ci			       const struct rx_sw_desc *sdesc)
45662306a36Sopenharmony_ci{
45762306a36Sopenharmony_ci	const struct sge *s = &adapter->sge;
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	return (s->fl_pg_order > 0 && (sdesc->dma_addr & RX_LARGE_BUF)
46062306a36Sopenharmony_ci		? (PAGE_SIZE << s->fl_pg_order) : PAGE_SIZE);
46162306a36Sopenharmony_ci}
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci/**
46462306a36Sopenharmony_ci *	free_rx_bufs - free RX buffers on an SGE Free List
46562306a36Sopenharmony_ci *	@adapter: the adapter
46662306a36Sopenharmony_ci *	@fl: the SGE Free List to free buffers from
46762306a36Sopenharmony_ci *	@n: how many buffers to free
46862306a36Sopenharmony_ci *
46962306a36Sopenharmony_ci *	Release the next @n buffers on an SGE Free List RX queue.   The
47062306a36Sopenharmony_ci *	buffers must be made inaccessible to hardware before calling this
47162306a36Sopenharmony_ci *	function.
47262306a36Sopenharmony_ci */
47362306a36Sopenharmony_cistatic void free_rx_bufs(struct adapter *adapter, struct sge_fl *fl, int n)
47462306a36Sopenharmony_ci{
47562306a36Sopenharmony_ci	while (n--) {
47662306a36Sopenharmony_ci		struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci		if (is_buf_mapped(sdesc))
47962306a36Sopenharmony_ci			dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
48062306a36Sopenharmony_ci				       get_buf_size(adapter, sdesc),
48162306a36Sopenharmony_ci				       DMA_FROM_DEVICE);
48262306a36Sopenharmony_ci		put_page(sdesc->page);
48362306a36Sopenharmony_ci		sdesc->page = NULL;
48462306a36Sopenharmony_ci		if (++fl->cidx == fl->size)
48562306a36Sopenharmony_ci			fl->cidx = 0;
48662306a36Sopenharmony_ci		fl->avail--;
48762306a36Sopenharmony_ci	}
48862306a36Sopenharmony_ci}
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci/**
49162306a36Sopenharmony_ci *	unmap_rx_buf - unmap the current RX buffer on an SGE Free List
49262306a36Sopenharmony_ci *	@adapter: the adapter
49362306a36Sopenharmony_ci *	@fl: the SGE Free List
49462306a36Sopenharmony_ci *
49562306a36Sopenharmony_ci *	Unmap the current buffer on an SGE Free List RX queue.   The
49662306a36Sopenharmony_ci *	buffer must be made inaccessible to HW before calling this function.
49762306a36Sopenharmony_ci *
49862306a36Sopenharmony_ci *	This is similar to @free_rx_bufs above but does not free the buffer.
49962306a36Sopenharmony_ci *	Do note that the FL still loses any further access to the buffer.
50062306a36Sopenharmony_ci *	This is used predominantly to "transfer ownership" of an FL buffer
50162306a36Sopenharmony_ci *	to another entity (typically an skb's fragment list).
50262306a36Sopenharmony_ci */
50362306a36Sopenharmony_cistatic void unmap_rx_buf(struct adapter *adapter, struct sge_fl *fl)
50462306a36Sopenharmony_ci{
50562306a36Sopenharmony_ci	struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci	if (is_buf_mapped(sdesc))
50862306a36Sopenharmony_ci		dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
50962306a36Sopenharmony_ci			       get_buf_size(adapter, sdesc),
51062306a36Sopenharmony_ci			       DMA_FROM_DEVICE);
51162306a36Sopenharmony_ci	sdesc->page = NULL;
51262306a36Sopenharmony_ci	if (++fl->cidx == fl->size)
51362306a36Sopenharmony_ci		fl->cidx = 0;
51462306a36Sopenharmony_ci	fl->avail--;
51562306a36Sopenharmony_ci}
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci/**
51862306a36Sopenharmony_ci *	ring_fl_db - righ doorbell on free list
51962306a36Sopenharmony_ci *	@adapter: the adapter
52062306a36Sopenharmony_ci *	@fl: the Free List whose doorbell should be rung ...
52162306a36Sopenharmony_ci *
52262306a36Sopenharmony_ci *	Tell the Scatter Gather Engine that there are new free list entries
52362306a36Sopenharmony_ci *	available.
52462306a36Sopenharmony_ci */
52562306a36Sopenharmony_cistatic inline void ring_fl_db(struct adapter *adapter, struct sge_fl *fl)
52662306a36Sopenharmony_ci{
52762306a36Sopenharmony_ci	u32 val = adapter->params.arch.sge_fl_db;
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci	/* The SGE keeps track of its Producer and Consumer Indices in terms
53062306a36Sopenharmony_ci	 * of Egress Queue Units so we can only tell it about integral numbers
53162306a36Sopenharmony_ci	 * of multiples of Free List Entries per Egress Queue Units ...
53262306a36Sopenharmony_ci	 */
53362306a36Sopenharmony_ci	if (fl->pend_cred >= FL_PER_EQ_UNIT) {
53462306a36Sopenharmony_ci		if (is_t4(adapter->params.chip))
53562306a36Sopenharmony_ci			val |= PIDX_V(fl->pend_cred / FL_PER_EQ_UNIT);
53662306a36Sopenharmony_ci		else
53762306a36Sopenharmony_ci			val |= PIDX_T5_V(fl->pend_cred / FL_PER_EQ_UNIT);
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci		/* Make sure all memory writes to the Free List queue are
54062306a36Sopenharmony_ci		 * committed before we tell the hardware about them.
54162306a36Sopenharmony_ci		 */
54262306a36Sopenharmony_ci		wmb();
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci		/* If we don't have access to the new User Doorbell (T5+), use
54562306a36Sopenharmony_ci		 * the old doorbell mechanism; otherwise use the new BAR2
54662306a36Sopenharmony_ci		 * mechanism.
54762306a36Sopenharmony_ci		 */
54862306a36Sopenharmony_ci		if (unlikely(fl->bar2_addr == NULL)) {
54962306a36Sopenharmony_ci			t4_write_reg(adapter,
55062306a36Sopenharmony_ci				     T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
55162306a36Sopenharmony_ci				     QID_V(fl->cntxt_id) | val);
55262306a36Sopenharmony_ci		} else {
55362306a36Sopenharmony_ci			writel(val | QID_V(fl->bar2_qid),
55462306a36Sopenharmony_ci			       fl->bar2_addr + SGE_UDB_KDOORBELL);
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci			/* This Write memory Barrier will force the write to
55762306a36Sopenharmony_ci			 * the User Doorbell area to be flushed.
55862306a36Sopenharmony_ci			 */
55962306a36Sopenharmony_ci			wmb();
56062306a36Sopenharmony_ci		}
56162306a36Sopenharmony_ci		fl->pend_cred %= FL_PER_EQ_UNIT;
56262306a36Sopenharmony_ci	}
56362306a36Sopenharmony_ci}
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ci/**
56662306a36Sopenharmony_ci *	set_rx_sw_desc - initialize software RX buffer descriptor
56762306a36Sopenharmony_ci *	@sdesc: pointer to the softwore RX buffer descriptor
56862306a36Sopenharmony_ci *	@page: pointer to the page data structure backing the RX buffer
56962306a36Sopenharmony_ci *	@dma_addr: PCI DMA address (possibly with low-bit flags)
57062306a36Sopenharmony_ci */
57162306a36Sopenharmony_cistatic inline void set_rx_sw_desc(struct rx_sw_desc *sdesc, struct page *page,
57262306a36Sopenharmony_ci				  dma_addr_t dma_addr)
57362306a36Sopenharmony_ci{
57462306a36Sopenharmony_ci	sdesc->page = page;
57562306a36Sopenharmony_ci	sdesc->dma_addr = dma_addr;
57662306a36Sopenharmony_ci}
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ci/*
57962306a36Sopenharmony_ci * Support for poisoning RX buffers ...
58062306a36Sopenharmony_ci */
58162306a36Sopenharmony_ci#define POISON_BUF_VAL -1
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_cistatic inline void poison_buf(struct page *page, size_t sz)
58462306a36Sopenharmony_ci{
58562306a36Sopenharmony_ci#if POISON_BUF_VAL >= 0
58662306a36Sopenharmony_ci	memset(page_address(page), POISON_BUF_VAL, sz);
58762306a36Sopenharmony_ci#endif
58862306a36Sopenharmony_ci}
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ci/**
59162306a36Sopenharmony_ci *	refill_fl - refill an SGE RX buffer ring
59262306a36Sopenharmony_ci *	@adapter: the adapter
59362306a36Sopenharmony_ci *	@fl: the Free List ring to refill
59462306a36Sopenharmony_ci *	@n: the number of new buffers to allocate
59562306a36Sopenharmony_ci *	@gfp: the gfp flags for the allocations
59662306a36Sopenharmony_ci *
59762306a36Sopenharmony_ci *	(Re)populate an SGE free-buffer queue with up to @n new packet buffers,
59862306a36Sopenharmony_ci *	allocated with the supplied gfp flags.  The caller must assure that
59962306a36Sopenharmony_ci *	@n does not exceed the queue's capacity -- i.e. (cidx == pidx) _IN
60062306a36Sopenharmony_ci *	EGRESS QUEUE UNITS_ indicates an empty Free List!  Returns the number
60162306a36Sopenharmony_ci *	of buffers allocated.  If afterwards the queue is found critically low,
60262306a36Sopenharmony_ci *	mark it as starving in the bitmap of starving FLs.
60362306a36Sopenharmony_ci */
60462306a36Sopenharmony_cistatic unsigned int refill_fl(struct adapter *adapter, struct sge_fl *fl,
60562306a36Sopenharmony_ci			      int n, gfp_t gfp)
60662306a36Sopenharmony_ci{
60762306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
60862306a36Sopenharmony_ci	struct page *page;
60962306a36Sopenharmony_ci	dma_addr_t dma_addr;
61062306a36Sopenharmony_ci	unsigned int cred = fl->avail;
61162306a36Sopenharmony_ci	__be64 *d = &fl->desc[fl->pidx];
61262306a36Sopenharmony_ci	struct rx_sw_desc *sdesc = &fl->sdesc[fl->pidx];
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ci	/*
61562306a36Sopenharmony_ci	 * Sanity: ensure that the result of adding n Free List buffers
61662306a36Sopenharmony_ci	 * won't result in wrapping the SGE's Producer Index around to
61762306a36Sopenharmony_ci	 * it's Consumer Index thereby indicating an empty Free List ...
61862306a36Sopenharmony_ci	 */
61962306a36Sopenharmony_ci	BUG_ON(fl->avail + n > fl->size - FL_PER_EQ_UNIT);
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_ci	gfp |= __GFP_NOWARN;
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci	/*
62462306a36Sopenharmony_ci	 * If we support large pages, prefer large buffers and fail over to
62562306a36Sopenharmony_ci	 * small pages if we can't allocate large pages to satisfy the refill.
62662306a36Sopenharmony_ci	 * If we don't support large pages, drop directly into the small page
62762306a36Sopenharmony_ci	 * allocation code.
62862306a36Sopenharmony_ci	 */
62962306a36Sopenharmony_ci	if (s->fl_pg_order == 0)
63062306a36Sopenharmony_ci		goto alloc_small_pages;
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_ci	while (n) {
63362306a36Sopenharmony_ci		page = __dev_alloc_pages(gfp, s->fl_pg_order);
63462306a36Sopenharmony_ci		if (unlikely(!page)) {
63562306a36Sopenharmony_ci			/*
63662306a36Sopenharmony_ci			 * We've failed inour attempt to allocate a "large
63762306a36Sopenharmony_ci			 * page".  Fail over to the "small page" allocation
63862306a36Sopenharmony_ci			 * below.
63962306a36Sopenharmony_ci			 */
64062306a36Sopenharmony_ci			fl->large_alloc_failed++;
64162306a36Sopenharmony_ci			break;
64262306a36Sopenharmony_ci		}
64362306a36Sopenharmony_ci		poison_buf(page, PAGE_SIZE << s->fl_pg_order);
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci		dma_addr = dma_map_page(adapter->pdev_dev, page, 0,
64662306a36Sopenharmony_ci					PAGE_SIZE << s->fl_pg_order,
64762306a36Sopenharmony_ci					DMA_FROM_DEVICE);
64862306a36Sopenharmony_ci		if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
64962306a36Sopenharmony_ci			/*
65062306a36Sopenharmony_ci			 * We've run out of DMA mapping space.  Free up the
65162306a36Sopenharmony_ci			 * buffer and return with what we've managed to put
65262306a36Sopenharmony_ci			 * into the free list.  We don't want to fail over to
65362306a36Sopenharmony_ci			 * the small page allocation below in this case
65462306a36Sopenharmony_ci			 * because DMA mapping resources are typically
65562306a36Sopenharmony_ci			 * critical resources once they become scarse.
65662306a36Sopenharmony_ci			 */
65762306a36Sopenharmony_ci			__free_pages(page, s->fl_pg_order);
65862306a36Sopenharmony_ci			goto out;
65962306a36Sopenharmony_ci		}
66062306a36Sopenharmony_ci		dma_addr |= RX_LARGE_BUF;
66162306a36Sopenharmony_ci		*d++ = cpu_to_be64(dma_addr);
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci		set_rx_sw_desc(sdesc, page, dma_addr);
66462306a36Sopenharmony_ci		sdesc++;
66562306a36Sopenharmony_ci
66662306a36Sopenharmony_ci		fl->avail++;
66762306a36Sopenharmony_ci		if (++fl->pidx == fl->size) {
66862306a36Sopenharmony_ci			fl->pidx = 0;
66962306a36Sopenharmony_ci			sdesc = fl->sdesc;
67062306a36Sopenharmony_ci			d = fl->desc;
67162306a36Sopenharmony_ci		}
67262306a36Sopenharmony_ci		n--;
67362306a36Sopenharmony_ci	}
67462306a36Sopenharmony_ci
67562306a36Sopenharmony_cialloc_small_pages:
67662306a36Sopenharmony_ci	while (n--) {
67762306a36Sopenharmony_ci		page = __dev_alloc_page(gfp);
67862306a36Sopenharmony_ci		if (unlikely(!page)) {
67962306a36Sopenharmony_ci			fl->alloc_failed++;
68062306a36Sopenharmony_ci			break;
68162306a36Sopenharmony_ci		}
68262306a36Sopenharmony_ci		poison_buf(page, PAGE_SIZE);
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci		dma_addr = dma_map_page(adapter->pdev_dev, page, 0, PAGE_SIZE,
68562306a36Sopenharmony_ci				       DMA_FROM_DEVICE);
68662306a36Sopenharmony_ci		if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
68762306a36Sopenharmony_ci			put_page(page);
68862306a36Sopenharmony_ci			break;
68962306a36Sopenharmony_ci		}
69062306a36Sopenharmony_ci		*d++ = cpu_to_be64(dma_addr);
69162306a36Sopenharmony_ci
69262306a36Sopenharmony_ci		set_rx_sw_desc(sdesc, page, dma_addr);
69362306a36Sopenharmony_ci		sdesc++;
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci		fl->avail++;
69662306a36Sopenharmony_ci		if (++fl->pidx == fl->size) {
69762306a36Sopenharmony_ci			fl->pidx = 0;
69862306a36Sopenharmony_ci			sdesc = fl->sdesc;
69962306a36Sopenharmony_ci			d = fl->desc;
70062306a36Sopenharmony_ci		}
70162306a36Sopenharmony_ci	}
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ciout:
70462306a36Sopenharmony_ci	/*
70562306a36Sopenharmony_ci	 * Update our accounting state to incorporate the new Free List
70662306a36Sopenharmony_ci	 * buffers, tell the hardware about them and return the number of
70762306a36Sopenharmony_ci	 * buffers which we were able to allocate.
70862306a36Sopenharmony_ci	 */
70962306a36Sopenharmony_ci	cred = fl->avail - cred;
71062306a36Sopenharmony_ci	fl->pend_cred += cred;
71162306a36Sopenharmony_ci	ring_fl_db(adapter, fl);
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ci	if (unlikely(fl_starving(adapter, fl))) {
71462306a36Sopenharmony_ci		smp_wmb();
71562306a36Sopenharmony_ci		set_bit(fl->cntxt_id, adapter->sge.starving_fl);
71662306a36Sopenharmony_ci	}
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	return cred;
71962306a36Sopenharmony_ci}
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci/*
72262306a36Sopenharmony_ci * Refill a Free List to its capacity or the Maximum Refill Increment,
72362306a36Sopenharmony_ci * whichever is smaller ...
72462306a36Sopenharmony_ci */
72562306a36Sopenharmony_cistatic inline void __refill_fl(struct adapter *adapter, struct sge_fl *fl)
72662306a36Sopenharmony_ci{
72762306a36Sopenharmony_ci	refill_fl(adapter, fl,
72862306a36Sopenharmony_ci		  min((unsigned int)MAX_RX_REFILL, fl_cap(fl) - fl->avail),
72962306a36Sopenharmony_ci		  GFP_ATOMIC);
73062306a36Sopenharmony_ci}
73162306a36Sopenharmony_ci
73262306a36Sopenharmony_ci/**
73362306a36Sopenharmony_ci *	alloc_ring - allocate resources for an SGE descriptor ring
73462306a36Sopenharmony_ci *	@dev: the PCI device's core device
73562306a36Sopenharmony_ci *	@nelem: the number of descriptors
73662306a36Sopenharmony_ci *	@hwsize: the size of each hardware descriptor
73762306a36Sopenharmony_ci *	@swsize: the size of each software descriptor
73862306a36Sopenharmony_ci *	@busaddrp: the physical PCI bus address of the allocated ring
73962306a36Sopenharmony_ci *	@swringp: return address pointer for software ring
74062306a36Sopenharmony_ci *	@stat_size: extra space in hardware ring for status information
74162306a36Sopenharmony_ci *
74262306a36Sopenharmony_ci *	Allocates resources for an SGE descriptor ring, such as TX queues,
74362306a36Sopenharmony_ci *	free buffer lists, response queues, etc.  Each SGE ring requires
74462306a36Sopenharmony_ci *	space for its hardware descriptors plus, optionally, space for software
74562306a36Sopenharmony_ci *	state associated with each hardware entry (the metadata).  The function
74662306a36Sopenharmony_ci *	returns three values: the virtual address for the hardware ring (the
74762306a36Sopenharmony_ci *	return value of the function), the PCI bus address of the hardware
74862306a36Sopenharmony_ci *	ring (in *busaddrp), and the address of the software ring (in swringp).
74962306a36Sopenharmony_ci *	Both the hardware and software rings are returned zeroed out.
75062306a36Sopenharmony_ci */
75162306a36Sopenharmony_cistatic void *alloc_ring(struct device *dev, size_t nelem, size_t hwsize,
75262306a36Sopenharmony_ci			size_t swsize, dma_addr_t *busaddrp, void *swringp,
75362306a36Sopenharmony_ci			size_t stat_size)
75462306a36Sopenharmony_ci{
75562306a36Sopenharmony_ci	/*
75662306a36Sopenharmony_ci	 * Allocate the hardware ring and PCI DMA bus address space for said.
75762306a36Sopenharmony_ci	 */
75862306a36Sopenharmony_ci	size_t hwlen = nelem * hwsize + stat_size;
75962306a36Sopenharmony_ci	void *hwring = dma_alloc_coherent(dev, hwlen, busaddrp, GFP_KERNEL);
76062306a36Sopenharmony_ci
76162306a36Sopenharmony_ci	if (!hwring)
76262306a36Sopenharmony_ci		return NULL;
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci	/*
76562306a36Sopenharmony_ci	 * If the caller wants a software ring, allocate it and return a
76662306a36Sopenharmony_ci	 * pointer to it in *swringp.
76762306a36Sopenharmony_ci	 */
76862306a36Sopenharmony_ci	BUG_ON((swsize != 0) != (swringp != NULL));
76962306a36Sopenharmony_ci	if (swsize) {
77062306a36Sopenharmony_ci		void *swring = kcalloc(nelem, swsize, GFP_KERNEL);
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ci		if (!swring) {
77362306a36Sopenharmony_ci			dma_free_coherent(dev, hwlen, hwring, *busaddrp);
77462306a36Sopenharmony_ci			return NULL;
77562306a36Sopenharmony_ci		}
77662306a36Sopenharmony_ci		*(void **)swringp = swring;
77762306a36Sopenharmony_ci	}
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_ci	return hwring;
78062306a36Sopenharmony_ci}
78162306a36Sopenharmony_ci
78262306a36Sopenharmony_ci/**
78362306a36Sopenharmony_ci *	sgl_len - calculates the size of an SGL of the given capacity
78462306a36Sopenharmony_ci *	@n: the number of SGL entries
78562306a36Sopenharmony_ci *
78662306a36Sopenharmony_ci *	Calculates the number of flits (8-byte units) needed for a Direct
78762306a36Sopenharmony_ci *	Scatter/Gather List that can hold the given number of entries.
78862306a36Sopenharmony_ci */
78962306a36Sopenharmony_cistatic inline unsigned int sgl_len(unsigned int n)
79062306a36Sopenharmony_ci{
79162306a36Sopenharmony_ci	/*
79262306a36Sopenharmony_ci	 * A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
79362306a36Sopenharmony_ci	 * addresses.  The DSGL Work Request starts off with a 32-bit DSGL
79462306a36Sopenharmony_ci	 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
79562306a36Sopenharmony_ci	 * repeated sequences of { Length[i], Length[i+1], Address[i],
79662306a36Sopenharmony_ci	 * Address[i+1] } (this ensures that all addresses are on 64-bit
79762306a36Sopenharmony_ci	 * boundaries).  If N is even, then Length[N+1] should be set to 0 and
79862306a36Sopenharmony_ci	 * Address[N+1] is omitted.
79962306a36Sopenharmony_ci	 *
80062306a36Sopenharmony_ci	 * The following calculation incorporates all of the above.  It's
80162306a36Sopenharmony_ci	 * somewhat hard to follow but, briefly: the "+2" accounts for the
80262306a36Sopenharmony_ci	 * first two flits which include the DSGL header, Length0 and
80362306a36Sopenharmony_ci	 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
80462306a36Sopenharmony_ci	 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
80562306a36Sopenharmony_ci	 * finally the "+((n-1)&1)" adds the one remaining flit needed if
80662306a36Sopenharmony_ci	 * (n-1) is odd ...
80762306a36Sopenharmony_ci	 */
80862306a36Sopenharmony_ci	n--;
80962306a36Sopenharmony_ci	return (3 * n) / 2 + (n & 1) + 2;
81062306a36Sopenharmony_ci}
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci/**
81362306a36Sopenharmony_ci *	flits_to_desc - returns the num of TX descriptors for the given flits
81462306a36Sopenharmony_ci *	@flits: the number of flits
81562306a36Sopenharmony_ci *
81662306a36Sopenharmony_ci *	Returns the number of TX descriptors needed for the supplied number
81762306a36Sopenharmony_ci *	of flits.
81862306a36Sopenharmony_ci */
81962306a36Sopenharmony_cistatic inline unsigned int flits_to_desc(unsigned int flits)
82062306a36Sopenharmony_ci{
82162306a36Sopenharmony_ci	BUG_ON(flits > SGE_MAX_WR_LEN / sizeof(__be64));
82262306a36Sopenharmony_ci	return DIV_ROUND_UP(flits, TXD_PER_EQ_UNIT);
82362306a36Sopenharmony_ci}
82462306a36Sopenharmony_ci
82562306a36Sopenharmony_ci/**
82662306a36Sopenharmony_ci *	is_eth_imm - can an Ethernet packet be sent as immediate data?
82762306a36Sopenharmony_ci *	@skb: the packet
82862306a36Sopenharmony_ci *
82962306a36Sopenharmony_ci *	Returns whether an Ethernet packet is small enough to fit completely as
83062306a36Sopenharmony_ci *	immediate data.
83162306a36Sopenharmony_ci */
83262306a36Sopenharmony_cistatic inline int is_eth_imm(const struct sk_buff *skb)
83362306a36Sopenharmony_ci{
83462306a36Sopenharmony_ci	/*
83562306a36Sopenharmony_ci	 * The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request
83662306a36Sopenharmony_ci	 * which does not accommodate immediate data.  We could dike out all
83762306a36Sopenharmony_ci	 * of the support code for immediate data but that would tie our hands
83862306a36Sopenharmony_ci	 * too much if we ever want to enhace the firmware.  It would also
83962306a36Sopenharmony_ci	 * create more differences between the PF and VF Drivers.
84062306a36Sopenharmony_ci	 */
84162306a36Sopenharmony_ci	return false;
84262306a36Sopenharmony_ci}
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_ci/**
84562306a36Sopenharmony_ci *	calc_tx_flits - calculate the number of flits for a packet TX WR
84662306a36Sopenharmony_ci *	@skb: the packet
84762306a36Sopenharmony_ci *
84862306a36Sopenharmony_ci *	Returns the number of flits needed for a TX Work Request for the
84962306a36Sopenharmony_ci *	given Ethernet packet, including the needed WR and CPL headers.
85062306a36Sopenharmony_ci */
85162306a36Sopenharmony_cistatic inline unsigned int calc_tx_flits(const struct sk_buff *skb)
85262306a36Sopenharmony_ci{
85362306a36Sopenharmony_ci	unsigned int flits;
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ci	/*
85662306a36Sopenharmony_ci	 * If the skb is small enough, we can pump it out as a work request
85762306a36Sopenharmony_ci	 * with only immediate data.  In that case we just have to have the
85862306a36Sopenharmony_ci	 * TX Packet header plus the skb data in the Work Request.
85962306a36Sopenharmony_ci	 */
86062306a36Sopenharmony_ci	if (is_eth_imm(skb))
86162306a36Sopenharmony_ci		return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt),
86262306a36Sopenharmony_ci				    sizeof(__be64));
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci	/*
86562306a36Sopenharmony_ci	 * Otherwise, we're going to have to construct a Scatter gather list
86662306a36Sopenharmony_ci	 * of the skb body and fragments.  We also include the flits necessary
86762306a36Sopenharmony_ci	 * for the TX Packet Work Request and CPL.  We always have a firmware
86862306a36Sopenharmony_ci	 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
86962306a36Sopenharmony_ci	 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
87062306a36Sopenharmony_ci	 * message or, if we're doing a Large Send Offload, an LSO CPL message
87162306a36Sopenharmony_ci	 * with an embedded TX Packet Write CPL message.
87262306a36Sopenharmony_ci	 */
87362306a36Sopenharmony_ci	flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
87462306a36Sopenharmony_ci	if (skb_shinfo(skb)->gso_size)
87562306a36Sopenharmony_ci		flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
87662306a36Sopenharmony_ci			  sizeof(struct cpl_tx_pkt_lso_core) +
87762306a36Sopenharmony_ci			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
87862306a36Sopenharmony_ci	else
87962306a36Sopenharmony_ci		flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
88062306a36Sopenharmony_ci			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
88162306a36Sopenharmony_ci	return flits;
88262306a36Sopenharmony_ci}
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci/**
88562306a36Sopenharmony_ci *	write_sgl - populate a Scatter/Gather List for a packet
88662306a36Sopenharmony_ci *	@skb: the packet
88762306a36Sopenharmony_ci *	@tq: the TX queue we are writing into
88862306a36Sopenharmony_ci *	@sgl: starting location for writing the SGL
88962306a36Sopenharmony_ci *	@end: points right after the end of the SGL
89062306a36Sopenharmony_ci *	@start: start offset into skb main-body data to include in the SGL
89162306a36Sopenharmony_ci *	@addr: the list of DMA bus addresses for the SGL elements
89262306a36Sopenharmony_ci *
89362306a36Sopenharmony_ci *	Generates a Scatter/Gather List for the buffers that make up a packet.
89462306a36Sopenharmony_ci *	The caller must provide adequate space for the SGL that will be written.
89562306a36Sopenharmony_ci *	The SGL includes all of the packet's page fragments and the data in its
89662306a36Sopenharmony_ci *	main body except for the first @start bytes.  @pos must be 16-byte
89762306a36Sopenharmony_ci *	aligned and within a TX descriptor with available space.  @end points
89862306a36Sopenharmony_ci *	write after the end of the SGL but does not account for any potential
89962306a36Sopenharmony_ci *	wrap around, i.e., @end > @tq->stat.
90062306a36Sopenharmony_ci */
90162306a36Sopenharmony_cistatic void write_sgl(const struct sk_buff *skb, struct sge_txq *tq,
90262306a36Sopenharmony_ci		      struct ulptx_sgl *sgl, u64 *end, unsigned int start,
90362306a36Sopenharmony_ci		      const dma_addr_t *addr)
90462306a36Sopenharmony_ci{
90562306a36Sopenharmony_ci	unsigned int i, len;
90662306a36Sopenharmony_ci	struct ulptx_sge_pair *to;
90762306a36Sopenharmony_ci	const struct skb_shared_info *si = skb_shinfo(skb);
90862306a36Sopenharmony_ci	unsigned int nfrags = si->nr_frags;
90962306a36Sopenharmony_ci	struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
91062306a36Sopenharmony_ci
91162306a36Sopenharmony_ci	len = skb_headlen(skb) - start;
91262306a36Sopenharmony_ci	if (likely(len)) {
91362306a36Sopenharmony_ci		sgl->len0 = htonl(len);
91462306a36Sopenharmony_ci		sgl->addr0 = cpu_to_be64(addr[0] + start);
91562306a36Sopenharmony_ci		nfrags++;
91662306a36Sopenharmony_ci	} else {
91762306a36Sopenharmony_ci		sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
91862306a36Sopenharmony_ci		sgl->addr0 = cpu_to_be64(addr[1]);
91962306a36Sopenharmony_ci	}
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ci	sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
92262306a36Sopenharmony_ci			      ULPTX_NSGE_V(nfrags));
92362306a36Sopenharmony_ci	if (likely(--nfrags == 0))
92462306a36Sopenharmony_ci		return;
92562306a36Sopenharmony_ci	/*
92662306a36Sopenharmony_ci	 * Most of the complexity below deals with the possibility we hit the
92762306a36Sopenharmony_ci	 * end of the queue in the middle of writing the SGL.  For this case
92862306a36Sopenharmony_ci	 * only we create the SGL in a temporary buffer and then copy it.
92962306a36Sopenharmony_ci	 */
93062306a36Sopenharmony_ci	to = (u8 *)end > (u8 *)tq->stat ? buf : sgl->sge;
93162306a36Sopenharmony_ci
93262306a36Sopenharmony_ci	for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
93362306a36Sopenharmony_ci		to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
93462306a36Sopenharmony_ci		to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
93562306a36Sopenharmony_ci		to->addr[0] = cpu_to_be64(addr[i]);
93662306a36Sopenharmony_ci		to->addr[1] = cpu_to_be64(addr[++i]);
93762306a36Sopenharmony_ci	}
93862306a36Sopenharmony_ci	if (nfrags) {
93962306a36Sopenharmony_ci		to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
94062306a36Sopenharmony_ci		to->len[1] = cpu_to_be32(0);
94162306a36Sopenharmony_ci		to->addr[0] = cpu_to_be64(addr[i + 1]);
94262306a36Sopenharmony_ci	}
94362306a36Sopenharmony_ci	if (unlikely((u8 *)end > (u8 *)tq->stat)) {
94462306a36Sopenharmony_ci		unsigned int part0 = (u8 *)tq->stat - (u8 *)sgl->sge, part1;
94562306a36Sopenharmony_ci
94662306a36Sopenharmony_ci		if (likely(part0))
94762306a36Sopenharmony_ci			memcpy(sgl->sge, buf, part0);
94862306a36Sopenharmony_ci		part1 = (u8 *)end - (u8 *)tq->stat;
94962306a36Sopenharmony_ci		memcpy(tq->desc, (u8 *)buf + part0, part1);
95062306a36Sopenharmony_ci		end = (void *)tq->desc + part1;
95162306a36Sopenharmony_ci	}
95262306a36Sopenharmony_ci	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
95362306a36Sopenharmony_ci		*end = 0;
95462306a36Sopenharmony_ci}
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ci/**
95762306a36Sopenharmony_ci *	ring_tx_db - check and potentially ring a TX queue's doorbell
95862306a36Sopenharmony_ci *	@adapter: the adapter
95962306a36Sopenharmony_ci *	@tq: the TX queue
96062306a36Sopenharmony_ci *	@n: number of new descriptors to give to HW
96162306a36Sopenharmony_ci *
96262306a36Sopenharmony_ci *	Ring the doorbel for a TX queue.
96362306a36Sopenharmony_ci */
96462306a36Sopenharmony_cistatic inline void ring_tx_db(struct adapter *adapter, struct sge_txq *tq,
96562306a36Sopenharmony_ci			      int n)
96662306a36Sopenharmony_ci{
96762306a36Sopenharmony_ci	/* Make sure that all writes to the TX Descriptors are committed
96862306a36Sopenharmony_ci	 * before we tell the hardware about them.
96962306a36Sopenharmony_ci	 */
97062306a36Sopenharmony_ci	wmb();
97162306a36Sopenharmony_ci
97262306a36Sopenharmony_ci	/* If we don't have access to the new User Doorbell (T5+), use the old
97362306a36Sopenharmony_ci	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
97462306a36Sopenharmony_ci	 */
97562306a36Sopenharmony_ci	if (unlikely(tq->bar2_addr == NULL)) {
97662306a36Sopenharmony_ci		u32 val = PIDX_V(n);
97762306a36Sopenharmony_ci
97862306a36Sopenharmony_ci		t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
97962306a36Sopenharmony_ci			     QID_V(tq->cntxt_id) | val);
98062306a36Sopenharmony_ci	} else {
98162306a36Sopenharmony_ci		u32 val = PIDX_T5_V(n);
98262306a36Sopenharmony_ci
98362306a36Sopenharmony_ci		/* T4 and later chips share the same PIDX field offset within
98462306a36Sopenharmony_ci		 * the doorbell, but T5 and later shrank the field in order to
98562306a36Sopenharmony_ci		 * gain a bit for Doorbell Priority.  The field was absurdly
98662306a36Sopenharmony_ci		 * large in the first place (14 bits) so we just use the T5
98762306a36Sopenharmony_ci		 * and later limits and warn if a Queue ID is too large.
98862306a36Sopenharmony_ci		 */
98962306a36Sopenharmony_ci		WARN_ON(val & DBPRIO_F);
99062306a36Sopenharmony_ci
99162306a36Sopenharmony_ci		/* If we're only writing a single Egress Unit and the BAR2
99262306a36Sopenharmony_ci		 * Queue ID is 0, we can use the Write Combining Doorbell
99362306a36Sopenharmony_ci		 * Gather Buffer; otherwise we use the simple doorbell.
99462306a36Sopenharmony_ci		 */
99562306a36Sopenharmony_ci		if (n == 1 && tq->bar2_qid == 0) {
99662306a36Sopenharmony_ci			unsigned int index = (tq->pidx
99762306a36Sopenharmony_ci					      ? (tq->pidx - 1)
99862306a36Sopenharmony_ci					      : (tq->size - 1));
99962306a36Sopenharmony_ci			__be64 *src = (__be64 *)&tq->desc[index];
100062306a36Sopenharmony_ci			__be64 __iomem *dst = (__be64 __iomem *)(tq->bar2_addr +
100162306a36Sopenharmony_ci							 SGE_UDB_WCDOORBELL);
100262306a36Sopenharmony_ci			unsigned int count = EQ_UNIT / sizeof(__be64);
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci			/* Copy the TX Descriptor in a tight loop in order to
100562306a36Sopenharmony_ci			 * try to get it to the adapter in a single Write
100662306a36Sopenharmony_ci			 * Combined transfer on the PCI-E Bus.  If the Write
100762306a36Sopenharmony_ci			 * Combine fails (say because of an interrupt, etc.)
100862306a36Sopenharmony_ci			 * the hardware will simply take the last write as a
100962306a36Sopenharmony_ci			 * simple doorbell write with a PIDX Increment of 1
101062306a36Sopenharmony_ci			 * and will fetch the TX Descriptor from memory via
101162306a36Sopenharmony_ci			 * DMA.
101262306a36Sopenharmony_ci			 */
101362306a36Sopenharmony_ci			while (count) {
101462306a36Sopenharmony_ci				/* the (__force u64) is because the compiler
101562306a36Sopenharmony_ci				 * doesn't understand the endian swizzling
101662306a36Sopenharmony_ci				 * going on
101762306a36Sopenharmony_ci				 */
101862306a36Sopenharmony_ci				writeq((__force u64)*src, dst);
101962306a36Sopenharmony_ci				src++;
102062306a36Sopenharmony_ci				dst++;
102162306a36Sopenharmony_ci				count--;
102262306a36Sopenharmony_ci			}
102362306a36Sopenharmony_ci		} else
102462306a36Sopenharmony_ci			writel(val | QID_V(tq->bar2_qid),
102562306a36Sopenharmony_ci			       tq->bar2_addr + SGE_UDB_KDOORBELL);
102662306a36Sopenharmony_ci
102762306a36Sopenharmony_ci		/* This Write Memory Barrier will force the write to the User
102862306a36Sopenharmony_ci		 * Doorbell area to be flushed.  This is needed to prevent
102962306a36Sopenharmony_ci		 * writes on different CPUs for the same queue from hitting
103062306a36Sopenharmony_ci		 * the adapter out of order.  This is required when some Work
103162306a36Sopenharmony_ci		 * Requests take the Write Combine Gather Buffer path (user
103262306a36Sopenharmony_ci		 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
103362306a36Sopenharmony_ci		 * take the traditional path where we simply increment the
103462306a36Sopenharmony_ci		 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
103562306a36Sopenharmony_ci		 * hardware DMA read the actual Work Request.
103662306a36Sopenharmony_ci		 */
103762306a36Sopenharmony_ci		wmb();
103862306a36Sopenharmony_ci	}
103962306a36Sopenharmony_ci}
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci/**
104262306a36Sopenharmony_ci *	inline_tx_skb - inline a packet's data into TX descriptors
104362306a36Sopenharmony_ci *	@skb: the packet
104462306a36Sopenharmony_ci *	@tq: the TX queue where the packet will be inlined
104562306a36Sopenharmony_ci *	@pos: starting position in the TX queue to inline the packet
104662306a36Sopenharmony_ci *
104762306a36Sopenharmony_ci *	Inline a packet's contents directly into TX descriptors, starting at
104862306a36Sopenharmony_ci *	the given position within the TX DMA ring.
104962306a36Sopenharmony_ci *	Most of the complexity of this operation is dealing with wrap arounds
105062306a36Sopenharmony_ci *	in the middle of the packet we want to inline.
105162306a36Sopenharmony_ci */
105262306a36Sopenharmony_cistatic void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *tq,
105362306a36Sopenharmony_ci			  void *pos)
105462306a36Sopenharmony_ci{
105562306a36Sopenharmony_ci	u64 *p;
105662306a36Sopenharmony_ci	int left = (void *)tq->stat - pos;
105762306a36Sopenharmony_ci
105862306a36Sopenharmony_ci	if (likely(skb->len <= left)) {
105962306a36Sopenharmony_ci		if (likely(!skb->data_len))
106062306a36Sopenharmony_ci			skb_copy_from_linear_data(skb, pos, skb->len);
106162306a36Sopenharmony_ci		else
106262306a36Sopenharmony_ci			skb_copy_bits(skb, 0, pos, skb->len);
106362306a36Sopenharmony_ci		pos += skb->len;
106462306a36Sopenharmony_ci	} else {
106562306a36Sopenharmony_ci		skb_copy_bits(skb, 0, pos, left);
106662306a36Sopenharmony_ci		skb_copy_bits(skb, left, tq->desc, skb->len - left);
106762306a36Sopenharmony_ci		pos = (void *)tq->desc + (skb->len - left);
106862306a36Sopenharmony_ci	}
106962306a36Sopenharmony_ci
107062306a36Sopenharmony_ci	/* 0-pad to multiple of 16 */
107162306a36Sopenharmony_ci	p = PTR_ALIGN(pos, 8);
107262306a36Sopenharmony_ci	if ((uintptr_t)p & 8)
107362306a36Sopenharmony_ci		*p = 0;
107462306a36Sopenharmony_ci}
107562306a36Sopenharmony_ci
107662306a36Sopenharmony_ci/*
107762306a36Sopenharmony_ci * Figure out what HW csum a packet wants and return the appropriate control
107862306a36Sopenharmony_ci * bits.
107962306a36Sopenharmony_ci */
108062306a36Sopenharmony_cistatic u64 hwcsum(enum chip_type chip, const struct sk_buff *skb)
108162306a36Sopenharmony_ci{
108262306a36Sopenharmony_ci	int csum_type;
108362306a36Sopenharmony_ci	const struct iphdr *iph = ip_hdr(skb);
108462306a36Sopenharmony_ci
108562306a36Sopenharmony_ci	if (iph->version == 4) {
108662306a36Sopenharmony_ci		if (iph->protocol == IPPROTO_TCP)
108762306a36Sopenharmony_ci			csum_type = TX_CSUM_TCPIP;
108862306a36Sopenharmony_ci		else if (iph->protocol == IPPROTO_UDP)
108962306a36Sopenharmony_ci			csum_type = TX_CSUM_UDPIP;
109062306a36Sopenharmony_ci		else {
109162306a36Sopenharmony_cinocsum:
109262306a36Sopenharmony_ci			/*
109362306a36Sopenharmony_ci			 * unknown protocol, disable HW csum
109462306a36Sopenharmony_ci			 * and hope a bad packet is detected
109562306a36Sopenharmony_ci			 */
109662306a36Sopenharmony_ci			return TXPKT_L4CSUM_DIS_F;
109762306a36Sopenharmony_ci		}
109862306a36Sopenharmony_ci	} else {
109962306a36Sopenharmony_ci		/*
110062306a36Sopenharmony_ci		 * this doesn't work with extension headers
110162306a36Sopenharmony_ci		 */
110262306a36Sopenharmony_ci		const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
110362306a36Sopenharmony_ci
110462306a36Sopenharmony_ci		if (ip6h->nexthdr == IPPROTO_TCP)
110562306a36Sopenharmony_ci			csum_type = TX_CSUM_TCPIP6;
110662306a36Sopenharmony_ci		else if (ip6h->nexthdr == IPPROTO_UDP)
110762306a36Sopenharmony_ci			csum_type = TX_CSUM_UDPIP6;
110862306a36Sopenharmony_ci		else
110962306a36Sopenharmony_ci			goto nocsum;
111062306a36Sopenharmony_ci	}
111162306a36Sopenharmony_ci
111262306a36Sopenharmony_ci	if (likely(csum_type >= TX_CSUM_TCPIP)) {
111362306a36Sopenharmony_ci		u64 hdr_len = TXPKT_IPHDR_LEN_V(skb_network_header_len(skb));
111462306a36Sopenharmony_ci		int eth_hdr_len = skb_network_offset(skb) - ETH_HLEN;
111562306a36Sopenharmony_ci
111662306a36Sopenharmony_ci		if (chip <= CHELSIO_T5)
111762306a36Sopenharmony_ci			hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len);
111862306a36Sopenharmony_ci		else
111962306a36Sopenharmony_ci			hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len);
112062306a36Sopenharmony_ci		return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len;
112162306a36Sopenharmony_ci	} else {
112262306a36Sopenharmony_ci		int start = skb_transport_offset(skb);
112362306a36Sopenharmony_ci
112462306a36Sopenharmony_ci		return TXPKT_CSUM_TYPE_V(csum_type) |
112562306a36Sopenharmony_ci			TXPKT_CSUM_START_V(start) |
112662306a36Sopenharmony_ci			TXPKT_CSUM_LOC_V(start + skb->csum_offset);
112762306a36Sopenharmony_ci	}
112862306a36Sopenharmony_ci}
112962306a36Sopenharmony_ci
113062306a36Sopenharmony_ci/*
113162306a36Sopenharmony_ci * Stop an Ethernet TX queue and record that state change.
113262306a36Sopenharmony_ci */
113362306a36Sopenharmony_cistatic void txq_stop(struct sge_eth_txq *txq)
113462306a36Sopenharmony_ci{
113562306a36Sopenharmony_ci	netif_tx_stop_queue(txq->txq);
113662306a36Sopenharmony_ci	txq->q.stops++;
113762306a36Sopenharmony_ci}
113862306a36Sopenharmony_ci
113962306a36Sopenharmony_ci/*
114062306a36Sopenharmony_ci * Advance our software state for a TX queue by adding n in use descriptors.
114162306a36Sopenharmony_ci */
114262306a36Sopenharmony_cistatic inline void txq_advance(struct sge_txq *tq, unsigned int n)
114362306a36Sopenharmony_ci{
114462306a36Sopenharmony_ci	tq->in_use += n;
114562306a36Sopenharmony_ci	tq->pidx += n;
114662306a36Sopenharmony_ci	if (tq->pidx >= tq->size)
114762306a36Sopenharmony_ci		tq->pidx -= tq->size;
114862306a36Sopenharmony_ci}
114962306a36Sopenharmony_ci
115062306a36Sopenharmony_ci/**
115162306a36Sopenharmony_ci *	t4vf_eth_xmit - add a packet to an Ethernet TX queue
115262306a36Sopenharmony_ci *	@skb: the packet
115362306a36Sopenharmony_ci *	@dev: the egress net device
115462306a36Sopenharmony_ci *
115562306a36Sopenharmony_ci *	Add a packet to an SGE Ethernet TX queue.  Runs with softirqs disabled.
115662306a36Sopenharmony_ci */
115762306a36Sopenharmony_cinetdev_tx_t t4vf_eth_xmit(struct sk_buff *skb, struct net_device *dev)
115862306a36Sopenharmony_ci{
115962306a36Sopenharmony_ci	u32 wr_mid;
116062306a36Sopenharmony_ci	u64 cntrl, *end;
116162306a36Sopenharmony_ci	int qidx, credits, max_pkt_len;
116262306a36Sopenharmony_ci	unsigned int flits, ndesc;
116362306a36Sopenharmony_ci	struct adapter *adapter;
116462306a36Sopenharmony_ci	struct sge_eth_txq *txq;
116562306a36Sopenharmony_ci	const struct port_info *pi;
116662306a36Sopenharmony_ci	struct fw_eth_tx_pkt_vm_wr *wr;
116762306a36Sopenharmony_ci	struct cpl_tx_pkt_core *cpl;
116862306a36Sopenharmony_ci	const struct skb_shared_info *ssi;
116962306a36Sopenharmony_ci	dma_addr_t addr[MAX_SKB_FRAGS + 1];
117062306a36Sopenharmony_ci	const size_t fw_hdr_copy_len = sizeof(wr->firmware);
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci	/*
117362306a36Sopenharmony_ci	 * The chip minimum packet length is 10 octets but the firmware
117462306a36Sopenharmony_ci	 * command that we are using requires that we copy the Ethernet header
117562306a36Sopenharmony_ci	 * (including the VLAN tag) into the header so we reject anything
117662306a36Sopenharmony_ci	 * smaller than that ...
117762306a36Sopenharmony_ci	 */
117862306a36Sopenharmony_ci	if (unlikely(skb->len < fw_hdr_copy_len))
117962306a36Sopenharmony_ci		goto out_free;
118062306a36Sopenharmony_ci
118162306a36Sopenharmony_ci	/* Discard the packet if the length is greater than mtu */
118262306a36Sopenharmony_ci	max_pkt_len = ETH_HLEN + dev->mtu;
118362306a36Sopenharmony_ci	if (skb_vlan_tagged(skb))
118462306a36Sopenharmony_ci		max_pkt_len += VLAN_HLEN;
118562306a36Sopenharmony_ci	if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
118662306a36Sopenharmony_ci		goto out_free;
118762306a36Sopenharmony_ci
118862306a36Sopenharmony_ci	/*
118962306a36Sopenharmony_ci	 * Figure out which TX Queue we're going to use.
119062306a36Sopenharmony_ci	 */
119162306a36Sopenharmony_ci	pi = netdev_priv(dev);
119262306a36Sopenharmony_ci	adapter = pi->adapter;
119362306a36Sopenharmony_ci	qidx = skb_get_queue_mapping(skb);
119462306a36Sopenharmony_ci	BUG_ON(qidx >= pi->nqsets);
119562306a36Sopenharmony_ci	txq = &adapter->sge.ethtxq[pi->first_qset + qidx];
119662306a36Sopenharmony_ci
119762306a36Sopenharmony_ci	if (pi->vlan_id && !skb_vlan_tag_present(skb))
119862306a36Sopenharmony_ci		__vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q),
119962306a36Sopenharmony_ci				       pi->vlan_id);
120062306a36Sopenharmony_ci
120162306a36Sopenharmony_ci	/*
120262306a36Sopenharmony_ci	 * Take this opportunity to reclaim any TX Descriptors whose DMA
120362306a36Sopenharmony_ci	 * transfers have completed.
120462306a36Sopenharmony_ci	 */
120562306a36Sopenharmony_ci	reclaim_completed_tx(adapter, &txq->q, true);
120662306a36Sopenharmony_ci
120762306a36Sopenharmony_ci	/*
120862306a36Sopenharmony_ci	 * Calculate the number of flits and TX Descriptors we're going to
120962306a36Sopenharmony_ci	 * need along with how many TX Descriptors will be left over after
121062306a36Sopenharmony_ci	 * we inject our Work Request.
121162306a36Sopenharmony_ci	 */
121262306a36Sopenharmony_ci	flits = calc_tx_flits(skb);
121362306a36Sopenharmony_ci	ndesc = flits_to_desc(flits);
121462306a36Sopenharmony_ci	credits = txq_avail(&txq->q) - ndesc;
121562306a36Sopenharmony_ci
121662306a36Sopenharmony_ci	if (unlikely(credits < 0)) {
121762306a36Sopenharmony_ci		/*
121862306a36Sopenharmony_ci		 * Not enough room for this packet's Work Request.  Stop the
121962306a36Sopenharmony_ci		 * TX Queue and return a "busy" condition.  The queue will get
122062306a36Sopenharmony_ci		 * started later on when the firmware informs us that space
122162306a36Sopenharmony_ci		 * has opened up.
122262306a36Sopenharmony_ci		 */
122362306a36Sopenharmony_ci		txq_stop(txq);
122462306a36Sopenharmony_ci		dev_err(adapter->pdev_dev,
122562306a36Sopenharmony_ci			"%s: TX ring %u full while queue awake!\n",
122662306a36Sopenharmony_ci			dev->name, qidx);
122762306a36Sopenharmony_ci		return NETDEV_TX_BUSY;
122862306a36Sopenharmony_ci	}
122962306a36Sopenharmony_ci
123062306a36Sopenharmony_ci	if (!is_eth_imm(skb) &&
123162306a36Sopenharmony_ci	    unlikely(map_skb(adapter->pdev_dev, skb, addr) < 0)) {
123262306a36Sopenharmony_ci		/*
123362306a36Sopenharmony_ci		 * We need to map the skb into PCI DMA space (because it can't
123462306a36Sopenharmony_ci		 * be in-lined directly into the Work Request) and the mapping
123562306a36Sopenharmony_ci		 * operation failed.  Record the error and drop the packet.
123662306a36Sopenharmony_ci		 */
123762306a36Sopenharmony_ci		txq->mapping_err++;
123862306a36Sopenharmony_ci		goto out_free;
123962306a36Sopenharmony_ci	}
124062306a36Sopenharmony_ci
124162306a36Sopenharmony_ci	wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
124262306a36Sopenharmony_ci	if (unlikely(credits < ETHTXQ_STOP_THRES)) {
124362306a36Sopenharmony_ci		/*
124462306a36Sopenharmony_ci		 * After we're done injecting the Work Request for this
124562306a36Sopenharmony_ci		 * packet, we'll be below our "stop threshold" so stop the TX
124662306a36Sopenharmony_ci		 * Queue now and schedule a request for an SGE Egress Queue
124762306a36Sopenharmony_ci		 * Update message.  The queue will get started later on when
124862306a36Sopenharmony_ci		 * the firmware processes this Work Request and sends us an
124962306a36Sopenharmony_ci		 * Egress Queue Status Update message indicating that space
125062306a36Sopenharmony_ci		 * has opened up.
125162306a36Sopenharmony_ci		 */
125262306a36Sopenharmony_ci		txq_stop(txq);
125362306a36Sopenharmony_ci		wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
125462306a36Sopenharmony_ci	}
125562306a36Sopenharmony_ci
125662306a36Sopenharmony_ci	/*
125762306a36Sopenharmony_ci	 * Start filling in our Work Request.  Note that we do _not_ handle
125862306a36Sopenharmony_ci	 * the WR Header wrapping around the TX Descriptor Ring.  If our
125962306a36Sopenharmony_ci	 * maximum header size ever exceeds one TX Descriptor, we'll need to
126062306a36Sopenharmony_ci	 * do something else here.
126162306a36Sopenharmony_ci	 */
126262306a36Sopenharmony_ci	BUG_ON(DIV_ROUND_UP(ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1);
126362306a36Sopenharmony_ci	wr = (void *)&txq->q.desc[txq->q.pidx];
126462306a36Sopenharmony_ci	wr->equiq_to_len16 = cpu_to_be32(wr_mid);
126562306a36Sopenharmony_ci	wr->r3[0] = cpu_to_be32(0);
126662306a36Sopenharmony_ci	wr->r3[1] = cpu_to_be32(0);
126762306a36Sopenharmony_ci	skb_copy_from_linear_data(skb, &wr->firmware, fw_hdr_copy_len);
126862306a36Sopenharmony_ci	end = (u64 *)wr + flits;
126962306a36Sopenharmony_ci
127062306a36Sopenharmony_ci	/*
127162306a36Sopenharmony_ci	 * If this is a Large Send Offload packet we'll put in an LSO CPL
127262306a36Sopenharmony_ci	 * message with an encapsulated TX Packet CPL message.  Otherwise we
127362306a36Sopenharmony_ci	 * just use a TX Packet CPL message.
127462306a36Sopenharmony_ci	 */
127562306a36Sopenharmony_ci	ssi = skb_shinfo(skb);
127662306a36Sopenharmony_ci	if (ssi->gso_size) {
127762306a36Sopenharmony_ci		struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
127862306a36Sopenharmony_ci		bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
127962306a36Sopenharmony_ci		int l3hdr_len = skb_network_header_len(skb);
128062306a36Sopenharmony_ci		int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
128162306a36Sopenharmony_ci
128262306a36Sopenharmony_ci		wr->op_immdlen =
128362306a36Sopenharmony_ci			cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
128462306a36Sopenharmony_ci				    FW_WR_IMMDLEN_V(sizeof(*lso) +
128562306a36Sopenharmony_ci						    sizeof(*cpl)));
128662306a36Sopenharmony_ci		/*
128762306a36Sopenharmony_ci		 * Fill in the LSO CPL message.
128862306a36Sopenharmony_ci		 */
128962306a36Sopenharmony_ci		lso->lso_ctrl =
129062306a36Sopenharmony_ci			cpu_to_be32(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
129162306a36Sopenharmony_ci				    LSO_FIRST_SLICE_F |
129262306a36Sopenharmony_ci				    LSO_LAST_SLICE_F |
129362306a36Sopenharmony_ci				    LSO_IPV6_V(v6) |
129462306a36Sopenharmony_ci				    LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
129562306a36Sopenharmony_ci				    LSO_IPHDR_LEN_V(l3hdr_len / 4) |
129662306a36Sopenharmony_ci				    LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
129762306a36Sopenharmony_ci		lso->ipid_ofst = cpu_to_be16(0);
129862306a36Sopenharmony_ci		lso->mss = cpu_to_be16(ssi->gso_size);
129962306a36Sopenharmony_ci		lso->seqno_offset = cpu_to_be32(0);
130062306a36Sopenharmony_ci		if (is_t4(adapter->params.chip))
130162306a36Sopenharmony_ci			lso->len = cpu_to_be32(skb->len);
130262306a36Sopenharmony_ci		else
130362306a36Sopenharmony_ci			lso->len = cpu_to_be32(LSO_T5_XFER_SIZE_V(skb->len));
130462306a36Sopenharmony_ci
130562306a36Sopenharmony_ci		/*
130662306a36Sopenharmony_ci		 * Set up TX Packet CPL pointer, control word and perform
130762306a36Sopenharmony_ci		 * accounting.
130862306a36Sopenharmony_ci		 */
130962306a36Sopenharmony_ci		cpl = (void *)(lso + 1);
131062306a36Sopenharmony_ci
131162306a36Sopenharmony_ci		if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
131262306a36Sopenharmony_ci			cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
131362306a36Sopenharmony_ci		else
131462306a36Sopenharmony_ci			cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
131562306a36Sopenharmony_ci
131662306a36Sopenharmony_ci		cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
131762306a36Sopenharmony_ci					   TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
131862306a36Sopenharmony_ci			 TXPKT_IPHDR_LEN_V(l3hdr_len);
131962306a36Sopenharmony_ci		txq->tso++;
132062306a36Sopenharmony_ci		txq->tx_cso += ssi->gso_segs;
132162306a36Sopenharmony_ci	} else {
132262306a36Sopenharmony_ci		int len;
132362306a36Sopenharmony_ci
132462306a36Sopenharmony_ci		len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
132562306a36Sopenharmony_ci		wr->op_immdlen =
132662306a36Sopenharmony_ci			cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
132762306a36Sopenharmony_ci				    FW_WR_IMMDLEN_V(len));
132862306a36Sopenharmony_ci
132962306a36Sopenharmony_ci		/*
133062306a36Sopenharmony_ci		 * Set up TX Packet CPL pointer, control word and perform
133162306a36Sopenharmony_ci		 * accounting.
133262306a36Sopenharmony_ci		 */
133362306a36Sopenharmony_ci		cpl = (void *)(wr + 1);
133462306a36Sopenharmony_ci		if (skb->ip_summed == CHECKSUM_PARTIAL) {
133562306a36Sopenharmony_ci			cntrl = hwcsum(adapter->params.chip, skb) |
133662306a36Sopenharmony_ci				TXPKT_IPCSUM_DIS_F;
133762306a36Sopenharmony_ci			txq->tx_cso++;
133862306a36Sopenharmony_ci		} else
133962306a36Sopenharmony_ci			cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
134062306a36Sopenharmony_ci	}
134162306a36Sopenharmony_ci
134262306a36Sopenharmony_ci	/*
134362306a36Sopenharmony_ci	 * If there's a VLAN tag present, add that to the list of things to
134462306a36Sopenharmony_ci	 * do in this Work Request.
134562306a36Sopenharmony_ci	 */
134662306a36Sopenharmony_ci	if (skb_vlan_tag_present(skb)) {
134762306a36Sopenharmony_ci		txq->vlan_ins++;
134862306a36Sopenharmony_ci		cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
134962306a36Sopenharmony_ci	}
135062306a36Sopenharmony_ci
135162306a36Sopenharmony_ci	/*
135262306a36Sopenharmony_ci	 * Fill in the TX Packet CPL message header.
135362306a36Sopenharmony_ci	 */
135462306a36Sopenharmony_ci	cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE_V(CPL_TX_PKT_XT) |
135562306a36Sopenharmony_ci				 TXPKT_INTF_V(pi->port_id) |
135662306a36Sopenharmony_ci				 TXPKT_PF_V(0));
135762306a36Sopenharmony_ci	cpl->pack = cpu_to_be16(0);
135862306a36Sopenharmony_ci	cpl->len = cpu_to_be16(skb->len);
135962306a36Sopenharmony_ci	cpl->ctrl1 = cpu_to_be64(cntrl);
136062306a36Sopenharmony_ci
136162306a36Sopenharmony_ci#ifdef T4_TRACE
136262306a36Sopenharmony_ci	T4_TRACE5(adapter->tb[txq->q.cntxt_id & 7],
136362306a36Sopenharmony_ci		  "eth_xmit: ndesc %u, credits %u, pidx %u, len %u, frags %u",
136462306a36Sopenharmony_ci		  ndesc, credits, txq->q.pidx, skb->len, ssi->nr_frags);
136562306a36Sopenharmony_ci#endif
136662306a36Sopenharmony_ci
136762306a36Sopenharmony_ci	/*
136862306a36Sopenharmony_ci	 * Fill in the body of the TX Packet CPL message with either in-lined
136962306a36Sopenharmony_ci	 * data or a Scatter/Gather List.
137062306a36Sopenharmony_ci	 */
137162306a36Sopenharmony_ci	if (is_eth_imm(skb)) {
137262306a36Sopenharmony_ci		/*
137362306a36Sopenharmony_ci		 * In-line the packet's data and free the skb since we don't
137462306a36Sopenharmony_ci		 * need it any longer.
137562306a36Sopenharmony_ci		 */
137662306a36Sopenharmony_ci		inline_tx_skb(skb, &txq->q, cpl + 1);
137762306a36Sopenharmony_ci		dev_consume_skb_any(skb);
137862306a36Sopenharmony_ci	} else {
137962306a36Sopenharmony_ci		/*
138062306a36Sopenharmony_ci		 * Write the skb's Scatter/Gather list into the TX Packet CPL
138162306a36Sopenharmony_ci		 * message and retain a pointer to the skb so we can free it
138262306a36Sopenharmony_ci		 * later when its DMA completes.  (We store the skb pointer
138362306a36Sopenharmony_ci		 * in the Software Descriptor corresponding to the last TX
138462306a36Sopenharmony_ci		 * Descriptor used by the Work Request.)
138562306a36Sopenharmony_ci		 *
138662306a36Sopenharmony_ci		 * The retained skb will be freed when the corresponding TX
138762306a36Sopenharmony_ci		 * Descriptors are reclaimed after their DMAs complete.
138862306a36Sopenharmony_ci		 * However, this could take quite a while since, in general,
138962306a36Sopenharmony_ci		 * the hardware is set up to be lazy about sending DMA
139062306a36Sopenharmony_ci		 * completion notifications to us and we mostly perform TX
139162306a36Sopenharmony_ci		 * reclaims in the transmit routine.
139262306a36Sopenharmony_ci		 *
139362306a36Sopenharmony_ci		 * This is good for performamce but means that we rely on new
139462306a36Sopenharmony_ci		 * TX packets arriving to run the destructors of completed
139562306a36Sopenharmony_ci		 * packets, which open up space in their sockets' send queues.
139662306a36Sopenharmony_ci		 * Sometimes we do not get such new packets causing TX to
139762306a36Sopenharmony_ci		 * stall.  A single UDP transmitter is a good example of this
139862306a36Sopenharmony_ci		 * situation.  We have a clean up timer that periodically
139962306a36Sopenharmony_ci		 * reclaims completed packets but it doesn't run often enough
140062306a36Sopenharmony_ci		 * (nor do we want it to) to prevent lengthy stalls.  A
140162306a36Sopenharmony_ci		 * solution to this problem is to run the destructor early,
140262306a36Sopenharmony_ci		 * after the packet is queued but before it's DMAd.  A con is
140362306a36Sopenharmony_ci		 * that we lie to socket memory accounting, but the amount of
140462306a36Sopenharmony_ci		 * extra memory is reasonable (limited by the number of TX
140562306a36Sopenharmony_ci		 * descriptors), the packets do actually get freed quickly by
140662306a36Sopenharmony_ci		 * new packets almost always, and for protocols like TCP that
140762306a36Sopenharmony_ci		 * wait for acks to really free up the data the extra memory
140862306a36Sopenharmony_ci		 * is even less.  On the positive side we run the destructors
140962306a36Sopenharmony_ci		 * on the sending CPU rather than on a potentially different
141062306a36Sopenharmony_ci		 * completing CPU, usually a good thing.
141162306a36Sopenharmony_ci		 *
141262306a36Sopenharmony_ci		 * Run the destructor before telling the DMA engine about the
141362306a36Sopenharmony_ci		 * packet to make sure it doesn't complete and get freed
141462306a36Sopenharmony_ci		 * prematurely.
141562306a36Sopenharmony_ci		 */
141662306a36Sopenharmony_ci		struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1);
141762306a36Sopenharmony_ci		struct sge_txq *tq = &txq->q;
141862306a36Sopenharmony_ci		int last_desc;
141962306a36Sopenharmony_ci
142062306a36Sopenharmony_ci		/*
142162306a36Sopenharmony_ci		 * If the Work Request header was an exact multiple of our TX
142262306a36Sopenharmony_ci		 * Descriptor length, then it's possible that the starting SGL
142362306a36Sopenharmony_ci		 * pointer lines up exactly with the end of our TX Descriptor
142462306a36Sopenharmony_ci		 * ring.  If that's the case, wrap around to the beginning
142562306a36Sopenharmony_ci		 * here ...
142662306a36Sopenharmony_ci		 */
142762306a36Sopenharmony_ci		if (unlikely((void *)sgl == (void *)tq->stat)) {
142862306a36Sopenharmony_ci			sgl = (void *)tq->desc;
142962306a36Sopenharmony_ci			end = ((void *)tq->desc + ((void *)end - (void *)tq->stat));
143062306a36Sopenharmony_ci		}
143162306a36Sopenharmony_ci
143262306a36Sopenharmony_ci		write_sgl(skb, tq, sgl, end, 0, addr);
143362306a36Sopenharmony_ci		skb_orphan(skb);
143462306a36Sopenharmony_ci
143562306a36Sopenharmony_ci		last_desc = tq->pidx + ndesc - 1;
143662306a36Sopenharmony_ci		if (last_desc >= tq->size)
143762306a36Sopenharmony_ci			last_desc -= tq->size;
143862306a36Sopenharmony_ci		tq->sdesc[last_desc].skb = skb;
143962306a36Sopenharmony_ci		tq->sdesc[last_desc].sgl = sgl;
144062306a36Sopenharmony_ci	}
144162306a36Sopenharmony_ci
144262306a36Sopenharmony_ci	/*
144362306a36Sopenharmony_ci	 * Advance our internal TX Queue state, tell the hardware about
144462306a36Sopenharmony_ci	 * the new TX descriptors and return success.
144562306a36Sopenharmony_ci	 */
144662306a36Sopenharmony_ci	txq_advance(&txq->q, ndesc);
144762306a36Sopenharmony_ci	netif_trans_update(dev);
144862306a36Sopenharmony_ci	ring_tx_db(adapter, &txq->q, ndesc);
144962306a36Sopenharmony_ci	return NETDEV_TX_OK;
145062306a36Sopenharmony_ci
145162306a36Sopenharmony_ciout_free:
145262306a36Sopenharmony_ci	/*
145362306a36Sopenharmony_ci	 * An error of some sort happened.  Free the TX skb and tell the
145462306a36Sopenharmony_ci	 * OS that we've "dealt" with the packet ...
145562306a36Sopenharmony_ci	 */
145662306a36Sopenharmony_ci	dev_kfree_skb_any(skb);
145762306a36Sopenharmony_ci	return NETDEV_TX_OK;
145862306a36Sopenharmony_ci}
145962306a36Sopenharmony_ci
146062306a36Sopenharmony_ci/**
146162306a36Sopenharmony_ci *	copy_frags - copy fragments from gather list into skb_shared_info
146262306a36Sopenharmony_ci *	@skb: destination skb
146362306a36Sopenharmony_ci *	@gl: source internal packet gather list
146462306a36Sopenharmony_ci *	@offset: packet start offset in first page
146562306a36Sopenharmony_ci *
146662306a36Sopenharmony_ci *	Copy an internal packet gather list into a Linux skb_shared_info
146762306a36Sopenharmony_ci *	structure.
146862306a36Sopenharmony_ci */
146962306a36Sopenharmony_cistatic inline void copy_frags(struct sk_buff *skb,
147062306a36Sopenharmony_ci			      const struct pkt_gl *gl,
147162306a36Sopenharmony_ci			      unsigned int offset)
147262306a36Sopenharmony_ci{
147362306a36Sopenharmony_ci	int i;
147462306a36Sopenharmony_ci
147562306a36Sopenharmony_ci	/* usually there's just one frag */
147662306a36Sopenharmony_ci	__skb_fill_page_desc(skb, 0, gl->frags[0].page,
147762306a36Sopenharmony_ci			     gl->frags[0].offset + offset,
147862306a36Sopenharmony_ci			     gl->frags[0].size - offset);
147962306a36Sopenharmony_ci	skb_shinfo(skb)->nr_frags = gl->nfrags;
148062306a36Sopenharmony_ci	for (i = 1; i < gl->nfrags; i++)
148162306a36Sopenharmony_ci		__skb_fill_page_desc(skb, i, gl->frags[i].page,
148262306a36Sopenharmony_ci				     gl->frags[i].offset,
148362306a36Sopenharmony_ci				     gl->frags[i].size);
148462306a36Sopenharmony_ci
148562306a36Sopenharmony_ci	/* get a reference to the last page, we don't own it */
148662306a36Sopenharmony_ci	get_page(gl->frags[gl->nfrags - 1].page);
148762306a36Sopenharmony_ci}
148862306a36Sopenharmony_ci
148962306a36Sopenharmony_ci/**
149062306a36Sopenharmony_ci *	t4vf_pktgl_to_skb - build an sk_buff from a packet gather list
149162306a36Sopenharmony_ci *	@gl: the gather list
149262306a36Sopenharmony_ci *	@skb_len: size of sk_buff main body if it carries fragments
149362306a36Sopenharmony_ci *	@pull_len: amount of data to move to the sk_buff's main body
149462306a36Sopenharmony_ci *
149562306a36Sopenharmony_ci *	Builds an sk_buff from the given packet gather list.  Returns the
149662306a36Sopenharmony_ci *	sk_buff or %NULL if sk_buff allocation failed.
149762306a36Sopenharmony_ci */
149862306a36Sopenharmony_cistatic struct sk_buff *t4vf_pktgl_to_skb(const struct pkt_gl *gl,
149962306a36Sopenharmony_ci					 unsigned int skb_len,
150062306a36Sopenharmony_ci					 unsigned int pull_len)
150162306a36Sopenharmony_ci{
150262306a36Sopenharmony_ci	struct sk_buff *skb;
150362306a36Sopenharmony_ci
150462306a36Sopenharmony_ci	/*
150562306a36Sopenharmony_ci	 * If the ingress packet is small enough, allocate an skb large enough
150662306a36Sopenharmony_ci	 * for all of the data and copy it inline.  Otherwise, allocate an skb
150762306a36Sopenharmony_ci	 * with enough room to pull in the header and reference the rest of
150862306a36Sopenharmony_ci	 * the data via the skb fragment list.
150962306a36Sopenharmony_ci	 *
151062306a36Sopenharmony_ci	 * Below we rely on RX_COPY_THRES being less than the smallest Rx
151162306a36Sopenharmony_ci	 * buff!  size, which is expected since buffers are at least
151262306a36Sopenharmony_ci	 * PAGE_SIZEd.  In this case packets up to RX_COPY_THRES have only one
151362306a36Sopenharmony_ci	 * fragment.
151462306a36Sopenharmony_ci	 */
151562306a36Sopenharmony_ci	if (gl->tot_len <= RX_COPY_THRES) {
151662306a36Sopenharmony_ci		/* small packets have only one fragment */
151762306a36Sopenharmony_ci		skb = alloc_skb(gl->tot_len, GFP_ATOMIC);
151862306a36Sopenharmony_ci		if (unlikely(!skb))
151962306a36Sopenharmony_ci			goto out;
152062306a36Sopenharmony_ci		__skb_put(skb, gl->tot_len);
152162306a36Sopenharmony_ci		skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
152262306a36Sopenharmony_ci	} else {
152362306a36Sopenharmony_ci		skb = alloc_skb(skb_len, GFP_ATOMIC);
152462306a36Sopenharmony_ci		if (unlikely(!skb))
152562306a36Sopenharmony_ci			goto out;
152662306a36Sopenharmony_ci		__skb_put(skb, pull_len);
152762306a36Sopenharmony_ci		skb_copy_to_linear_data(skb, gl->va, pull_len);
152862306a36Sopenharmony_ci
152962306a36Sopenharmony_ci		copy_frags(skb, gl, pull_len);
153062306a36Sopenharmony_ci		skb->len = gl->tot_len;
153162306a36Sopenharmony_ci		skb->data_len = skb->len - pull_len;
153262306a36Sopenharmony_ci		skb->truesize += skb->data_len;
153362306a36Sopenharmony_ci	}
153462306a36Sopenharmony_ci
153562306a36Sopenharmony_ciout:
153662306a36Sopenharmony_ci	return skb;
153762306a36Sopenharmony_ci}
153862306a36Sopenharmony_ci
153962306a36Sopenharmony_ci/**
154062306a36Sopenharmony_ci *	t4vf_pktgl_free - free a packet gather list
154162306a36Sopenharmony_ci *	@gl: the gather list
154262306a36Sopenharmony_ci *
154362306a36Sopenharmony_ci *	Releases the pages of a packet gather list.  We do not own the last
154462306a36Sopenharmony_ci *	page on the list and do not free it.
154562306a36Sopenharmony_ci */
154662306a36Sopenharmony_cistatic void t4vf_pktgl_free(const struct pkt_gl *gl)
154762306a36Sopenharmony_ci{
154862306a36Sopenharmony_ci	int frag;
154962306a36Sopenharmony_ci
155062306a36Sopenharmony_ci	frag = gl->nfrags - 1;
155162306a36Sopenharmony_ci	while (frag--)
155262306a36Sopenharmony_ci		put_page(gl->frags[frag].page);
155362306a36Sopenharmony_ci}
155462306a36Sopenharmony_ci
155562306a36Sopenharmony_ci/**
155662306a36Sopenharmony_ci *	do_gro - perform Generic Receive Offload ingress packet processing
155762306a36Sopenharmony_ci *	@rxq: ingress RX Ethernet Queue
155862306a36Sopenharmony_ci *	@gl: gather list for ingress packet
155962306a36Sopenharmony_ci *	@pkt: CPL header for last packet fragment
156062306a36Sopenharmony_ci *
156162306a36Sopenharmony_ci *	Perform Generic Receive Offload (GRO) ingress packet processing.
156262306a36Sopenharmony_ci *	We use the standard Linux GRO interfaces for this.
156362306a36Sopenharmony_ci */
156462306a36Sopenharmony_cistatic void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
156562306a36Sopenharmony_ci		   const struct cpl_rx_pkt *pkt)
156662306a36Sopenharmony_ci{
156762306a36Sopenharmony_ci	struct adapter *adapter = rxq->rspq.adapter;
156862306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
156962306a36Sopenharmony_ci	struct port_info *pi;
157062306a36Sopenharmony_ci	int ret;
157162306a36Sopenharmony_ci	struct sk_buff *skb;
157262306a36Sopenharmony_ci
157362306a36Sopenharmony_ci	skb = napi_get_frags(&rxq->rspq.napi);
157462306a36Sopenharmony_ci	if (unlikely(!skb)) {
157562306a36Sopenharmony_ci		t4vf_pktgl_free(gl);
157662306a36Sopenharmony_ci		rxq->stats.rx_drops++;
157762306a36Sopenharmony_ci		return;
157862306a36Sopenharmony_ci	}
157962306a36Sopenharmony_ci
158062306a36Sopenharmony_ci	copy_frags(skb, gl, s->pktshift);
158162306a36Sopenharmony_ci	skb->len = gl->tot_len - s->pktshift;
158262306a36Sopenharmony_ci	skb->data_len = skb->len;
158362306a36Sopenharmony_ci	skb->truesize += skb->data_len;
158462306a36Sopenharmony_ci	skb->ip_summed = CHECKSUM_UNNECESSARY;
158562306a36Sopenharmony_ci	skb_record_rx_queue(skb, rxq->rspq.idx);
158662306a36Sopenharmony_ci	pi = netdev_priv(skb->dev);
158762306a36Sopenharmony_ci
158862306a36Sopenharmony_ci	if (pkt->vlan_ex && !pi->vlan_id) {
158962306a36Sopenharmony_ci		__vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q),
159062306a36Sopenharmony_ci					be16_to_cpu(pkt->vlan));
159162306a36Sopenharmony_ci		rxq->stats.vlan_ex++;
159262306a36Sopenharmony_ci	}
159362306a36Sopenharmony_ci	ret = napi_gro_frags(&rxq->rspq.napi);
159462306a36Sopenharmony_ci
159562306a36Sopenharmony_ci	if (ret == GRO_HELD)
159662306a36Sopenharmony_ci		rxq->stats.lro_pkts++;
159762306a36Sopenharmony_ci	else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
159862306a36Sopenharmony_ci		rxq->stats.lro_merged++;
159962306a36Sopenharmony_ci	rxq->stats.pkts++;
160062306a36Sopenharmony_ci	rxq->stats.rx_cso++;
160162306a36Sopenharmony_ci}
160262306a36Sopenharmony_ci
160362306a36Sopenharmony_ci/**
160462306a36Sopenharmony_ci *	t4vf_ethrx_handler - process an ingress ethernet packet
160562306a36Sopenharmony_ci *	@rspq: the response queue that received the packet
160662306a36Sopenharmony_ci *	@rsp: the response queue descriptor holding the RX_PKT message
160762306a36Sopenharmony_ci *	@gl: the gather list of packet fragments
160862306a36Sopenharmony_ci *
160962306a36Sopenharmony_ci *	Process an ingress ethernet packet and deliver it to the stack.
161062306a36Sopenharmony_ci */
161162306a36Sopenharmony_ciint t4vf_ethrx_handler(struct sge_rspq *rspq, const __be64 *rsp,
161262306a36Sopenharmony_ci		       const struct pkt_gl *gl)
161362306a36Sopenharmony_ci{
161462306a36Sopenharmony_ci	struct sk_buff *skb;
161562306a36Sopenharmony_ci	const struct cpl_rx_pkt *pkt = (void *)rsp;
161662306a36Sopenharmony_ci	bool csum_ok = pkt->csum_calc && !pkt->err_vec &&
161762306a36Sopenharmony_ci		       (rspq->netdev->features & NETIF_F_RXCSUM);
161862306a36Sopenharmony_ci	struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
161962306a36Sopenharmony_ci	struct adapter *adapter = rspq->adapter;
162062306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
162162306a36Sopenharmony_ci	struct port_info *pi;
162262306a36Sopenharmony_ci
162362306a36Sopenharmony_ci	/*
162462306a36Sopenharmony_ci	 * If this is a good TCP packet and we have Generic Receive Offload
162562306a36Sopenharmony_ci	 * enabled, handle the packet in the GRO path.
162662306a36Sopenharmony_ci	 */
162762306a36Sopenharmony_ci	if ((pkt->l2info & cpu_to_be32(RXF_TCP_F)) &&
162862306a36Sopenharmony_ci	    (rspq->netdev->features & NETIF_F_GRO) && csum_ok &&
162962306a36Sopenharmony_ci	    !pkt->ip_frag) {
163062306a36Sopenharmony_ci		do_gro(rxq, gl, pkt);
163162306a36Sopenharmony_ci		return 0;
163262306a36Sopenharmony_ci	}
163362306a36Sopenharmony_ci
163462306a36Sopenharmony_ci	/*
163562306a36Sopenharmony_ci	 * Convert the Packet Gather List into an skb.
163662306a36Sopenharmony_ci	 */
163762306a36Sopenharmony_ci	skb = t4vf_pktgl_to_skb(gl, RX_SKB_LEN, RX_PULL_LEN);
163862306a36Sopenharmony_ci	if (unlikely(!skb)) {
163962306a36Sopenharmony_ci		t4vf_pktgl_free(gl);
164062306a36Sopenharmony_ci		rxq->stats.rx_drops++;
164162306a36Sopenharmony_ci		return 0;
164262306a36Sopenharmony_ci	}
164362306a36Sopenharmony_ci	__skb_pull(skb, s->pktshift);
164462306a36Sopenharmony_ci	skb->protocol = eth_type_trans(skb, rspq->netdev);
164562306a36Sopenharmony_ci	skb_record_rx_queue(skb, rspq->idx);
164662306a36Sopenharmony_ci	pi = netdev_priv(skb->dev);
164762306a36Sopenharmony_ci	rxq->stats.pkts++;
164862306a36Sopenharmony_ci
164962306a36Sopenharmony_ci	if (csum_ok && !pkt->err_vec &&
165062306a36Sopenharmony_ci	    (be32_to_cpu(pkt->l2info) & (RXF_UDP_F | RXF_TCP_F))) {
165162306a36Sopenharmony_ci		if (!pkt->ip_frag) {
165262306a36Sopenharmony_ci			skb->ip_summed = CHECKSUM_UNNECESSARY;
165362306a36Sopenharmony_ci			rxq->stats.rx_cso++;
165462306a36Sopenharmony_ci		} else if (pkt->l2info & htonl(RXF_IP_F)) {
165562306a36Sopenharmony_ci			__sum16 c = (__force __sum16)pkt->csum;
165662306a36Sopenharmony_ci			skb->csum = csum_unfold(c);
165762306a36Sopenharmony_ci			skb->ip_summed = CHECKSUM_COMPLETE;
165862306a36Sopenharmony_ci			rxq->stats.rx_cso++;
165962306a36Sopenharmony_ci		}
166062306a36Sopenharmony_ci	} else
166162306a36Sopenharmony_ci		skb_checksum_none_assert(skb);
166262306a36Sopenharmony_ci
166362306a36Sopenharmony_ci	if (pkt->vlan_ex && !pi->vlan_id) {
166462306a36Sopenharmony_ci		rxq->stats.vlan_ex++;
166562306a36Sopenharmony_ci		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
166662306a36Sopenharmony_ci				       be16_to_cpu(pkt->vlan));
166762306a36Sopenharmony_ci	}
166862306a36Sopenharmony_ci
166962306a36Sopenharmony_ci	netif_receive_skb(skb);
167062306a36Sopenharmony_ci
167162306a36Sopenharmony_ci	return 0;
167262306a36Sopenharmony_ci}
167362306a36Sopenharmony_ci
167462306a36Sopenharmony_ci/**
167562306a36Sopenharmony_ci *	is_new_response - check if a response is newly written
167662306a36Sopenharmony_ci *	@rc: the response control descriptor
167762306a36Sopenharmony_ci *	@rspq: the response queue
167862306a36Sopenharmony_ci *
167962306a36Sopenharmony_ci *	Returns true if a response descriptor contains a yet unprocessed
168062306a36Sopenharmony_ci *	response.
168162306a36Sopenharmony_ci */
168262306a36Sopenharmony_cistatic inline bool is_new_response(const struct rsp_ctrl *rc,
168362306a36Sopenharmony_ci				   const struct sge_rspq *rspq)
168462306a36Sopenharmony_ci{
168562306a36Sopenharmony_ci	return ((rc->type_gen >> RSPD_GEN_S) & 0x1) == rspq->gen;
168662306a36Sopenharmony_ci}
168762306a36Sopenharmony_ci
168862306a36Sopenharmony_ci/**
168962306a36Sopenharmony_ci *	restore_rx_bufs - put back a packet's RX buffers
169062306a36Sopenharmony_ci *	@gl: the packet gather list
169162306a36Sopenharmony_ci *	@fl: the SGE Free List
169262306a36Sopenharmony_ci *	@frags: how many fragments in @si
169362306a36Sopenharmony_ci *
169462306a36Sopenharmony_ci *	Called when we find out that the current packet, @si, can't be
169562306a36Sopenharmony_ci *	processed right away for some reason.  This is a very rare event and
169662306a36Sopenharmony_ci *	there's no effort to make this suspension/resumption process
169762306a36Sopenharmony_ci *	particularly efficient.
169862306a36Sopenharmony_ci *
169962306a36Sopenharmony_ci *	We implement the suspension by putting all of the RX buffers associated
170062306a36Sopenharmony_ci *	with the current packet back on the original Free List.  The buffers
170162306a36Sopenharmony_ci *	have already been unmapped and are left unmapped, we mark them as
170262306a36Sopenharmony_ci *	unmapped in order to prevent further unmapping attempts.  (Effectively
170362306a36Sopenharmony_ci *	this function undoes the series of @unmap_rx_buf calls which were done
170462306a36Sopenharmony_ci *	to create the current packet's gather list.)  This leaves us ready to
170562306a36Sopenharmony_ci *	restart processing of the packet the next time we start processing the
170662306a36Sopenharmony_ci *	RX Queue ...
170762306a36Sopenharmony_ci */
170862306a36Sopenharmony_cistatic void restore_rx_bufs(const struct pkt_gl *gl, struct sge_fl *fl,
170962306a36Sopenharmony_ci			    int frags)
171062306a36Sopenharmony_ci{
171162306a36Sopenharmony_ci	struct rx_sw_desc *sdesc;
171262306a36Sopenharmony_ci
171362306a36Sopenharmony_ci	while (frags--) {
171462306a36Sopenharmony_ci		if (fl->cidx == 0)
171562306a36Sopenharmony_ci			fl->cidx = fl->size - 1;
171662306a36Sopenharmony_ci		else
171762306a36Sopenharmony_ci			fl->cidx--;
171862306a36Sopenharmony_ci		sdesc = &fl->sdesc[fl->cidx];
171962306a36Sopenharmony_ci		sdesc->page = gl->frags[frags].page;
172062306a36Sopenharmony_ci		sdesc->dma_addr |= RX_UNMAPPED_BUF;
172162306a36Sopenharmony_ci		fl->avail++;
172262306a36Sopenharmony_ci	}
172362306a36Sopenharmony_ci}
172462306a36Sopenharmony_ci
172562306a36Sopenharmony_ci/**
172662306a36Sopenharmony_ci *	rspq_next - advance to the next entry in a response queue
172762306a36Sopenharmony_ci *	@rspq: the queue
172862306a36Sopenharmony_ci *
172962306a36Sopenharmony_ci *	Updates the state of a response queue to advance it to the next entry.
173062306a36Sopenharmony_ci */
173162306a36Sopenharmony_cistatic inline void rspq_next(struct sge_rspq *rspq)
173262306a36Sopenharmony_ci{
173362306a36Sopenharmony_ci	rspq->cur_desc = (void *)rspq->cur_desc + rspq->iqe_len;
173462306a36Sopenharmony_ci	if (unlikely(++rspq->cidx == rspq->size)) {
173562306a36Sopenharmony_ci		rspq->cidx = 0;
173662306a36Sopenharmony_ci		rspq->gen ^= 1;
173762306a36Sopenharmony_ci		rspq->cur_desc = rspq->desc;
173862306a36Sopenharmony_ci	}
173962306a36Sopenharmony_ci}
174062306a36Sopenharmony_ci
174162306a36Sopenharmony_ci/**
174262306a36Sopenharmony_ci *	process_responses - process responses from an SGE response queue
174362306a36Sopenharmony_ci *	@rspq: the ingress response queue to process
174462306a36Sopenharmony_ci *	@budget: how many responses can be processed in this round
174562306a36Sopenharmony_ci *
174662306a36Sopenharmony_ci *	Process responses from a Scatter Gather Engine response queue up to
174762306a36Sopenharmony_ci *	the supplied budget.  Responses include received packets as well as
174862306a36Sopenharmony_ci *	control messages from firmware or hardware.
174962306a36Sopenharmony_ci *
175062306a36Sopenharmony_ci *	Additionally choose the interrupt holdoff time for the next interrupt
175162306a36Sopenharmony_ci *	on this queue.  If the system is under memory shortage use a fairly
175262306a36Sopenharmony_ci *	long delay to help recovery.
175362306a36Sopenharmony_ci */
175462306a36Sopenharmony_cistatic int process_responses(struct sge_rspq *rspq, int budget)
175562306a36Sopenharmony_ci{
175662306a36Sopenharmony_ci	struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
175762306a36Sopenharmony_ci	struct adapter *adapter = rspq->adapter;
175862306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
175962306a36Sopenharmony_ci	int budget_left = budget;
176062306a36Sopenharmony_ci
176162306a36Sopenharmony_ci	while (likely(budget_left)) {
176262306a36Sopenharmony_ci		int ret, rsp_type;
176362306a36Sopenharmony_ci		const struct rsp_ctrl *rc;
176462306a36Sopenharmony_ci
176562306a36Sopenharmony_ci		rc = (void *)rspq->cur_desc + (rspq->iqe_len - sizeof(*rc));
176662306a36Sopenharmony_ci		if (!is_new_response(rc, rspq))
176762306a36Sopenharmony_ci			break;
176862306a36Sopenharmony_ci
176962306a36Sopenharmony_ci		/*
177062306a36Sopenharmony_ci		 * Figure out what kind of response we've received from the
177162306a36Sopenharmony_ci		 * SGE.
177262306a36Sopenharmony_ci		 */
177362306a36Sopenharmony_ci		dma_rmb();
177462306a36Sopenharmony_ci		rsp_type = RSPD_TYPE_G(rc->type_gen);
177562306a36Sopenharmony_ci		if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
177662306a36Sopenharmony_ci			struct page_frag *fp;
177762306a36Sopenharmony_ci			struct pkt_gl gl;
177862306a36Sopenharmony_ci			const struct rx_sw_desc *sdesc;
177962306a36Sopenharmony_ci			u32 bufsz, frag;
178062306a36Sopenharmony_ci			u32 len = be32_to_cpu(rc->pldbuflen_qid);
178162306a36Sopenharmony_ci
178262306a36Sopenharmony_ci			/*
178362306a36Sopenharmony_ci			 * If we get a "new buffer" message from the SGE we
178462306a36Sopenharmony_ci			 * need to move on to the next Free List buffer.
178562306a36Sopenharmony_ci			 */
178662306a36Sopenharmony_ci			if (len & RSPD_NEWBUF_F) {
178762306a36Sopenharmony_ci				/*
178862306a36Sopenharmony_ci				 * We get one "new buffer" message when we
178962306a36Sopenharmony_ci				 * first start up a queue so we need to ignore
179062306a36Sopenharmony_ci				 * it when our offset into the buffer is 0.
179162306a36Sopenharmony_ci				 */
179262306a36Sopenharmony_ci				if (likely(rspq->offset > 0)) {
179362306a36Sopenharmony_ci					free_rx_bufs(rspq->adapter, &rxq->fl,
179462306a36Sopenharmony_ci						     1);
179562306a36Sopenharmony_ci					rspq->offset = 0;
179662306a36Sopenharmony_ci				}
179762306a36Sopenharmony_ci				len = RSPD_LEN_G(len);
179862306a36Sopenharmony_ci			}
179962306a36Sopenharmony_ci			gl.tot_len = len;
180062306a36Sopenharmony_ci
180162306a36Sopenharmony_ci			/*
180262306a36Sopenharmony_ci			 * Gather packet fragments.
180362306a36Sopenharmony_ci			 */
180462306a36Sopenharmony_ci			for (frag = 0, fp = gl.frags; /**/; frag++, fp++) {
180562306a36Sopenharmony_ci				BUG_ON(frag >= MAX_SKB_FRAGS);
180662306a36Sopenharmony_ci				BUG_ON(rxq->fl.avail == 0);
180762306a36Sopenharmony_ci				sdesc = &rxq->fl.sdesc[rxq->fl.cidx];
180862306a36Sopenharmony_ci				bufsz = get_buf_size(adapter, sdesc);
180962306a36Sopenharmony_ci				fp->page = sdesc->page;
181062306a36Sopenharmony_ci				fp->offset = rspq->offset;
181162306a36Sopenharmony_ci				fp->size = min(bufsz, len);
181262306a36Sopenharmony_ci				len -= fp->size;
181362306a36Sopenharmony_ci				if (!len)
181462306a36Sopenharmony_ci					break;
181562306a36Sopenharmony_ci				unmap_rx_buf(rspq->adapter, &rxq->fl);
181662306a36Sopenharmony_ci			}
181762306a36Sopenharmony_ci			gl.nfrags = frag+1;
181862306a36Sopenharmony_ci
181962306a36Sopenharmony_ci			/*
182062306a36Sopenharmony_ci			 * Last buffer remains mapped so explicitly make it
182162306a36Sopenharmony_ci			 * coherent for CPU access and start preloading first
182262306a36Sopenharmony_ci			 * cache line ...
182362306a36Sopenharmony_ci			 */
182462306a36Sopenharmony_ci			dma_sync_single_for_cpu(rspq->adapter->pdev_dev,
182562306a36Sopenharmony_ci						get_buf_addr(sdesc),
182662306a36Sopenharmony_ci						fp->size, DMA_FROM_DEVICE);
182762306a36Sopenharmony_ci			gl.va = (page_address(gl.frags[0].page) +
182862306a36Sopenharmony_ci				 gl.frags[0].offset);
182962306a36Sopenharmony_ci			prefetch(gl.va);
183062306a36Sopenharmony_ci
183162306a36Sopenharmony_ci			/*
183262306a36Sopenharmony_ci			 * Hand the new ingress packet to the handler for
183362306a36Sopenharmony_ci			 * this Response Queue.
183462306a36Sopenharmony_ci			 */
183562306a36Sopenharmony_ci			ret = rspq->handler(rspq, rspq->cur_desc, &gl);
183662306a36Sopenharmony_ci			if (likely(ret == 0))
183762306a36Sopenharmony_ci				rspq->offset += ALIGN(fp->size, s->fl_align);
183862306a36Sopenharmony_ci			else
183962306a36Sopenharmony_ci				restore_rx_bufs(&gl, &rxq->fl, frag);
184062306a36Sopenharmony_ci		} else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
184162306a36Sopenharmony_ci			ret = rspq->handler(rspq, rspq->cur_desc, NULL);
184262306a36Sopenharmony_ci		} else {
184362306a36Sopenharmony_ci			WARN_ON(rsp_type > RSPD_TYPE_CPL_X);
184462306a36Sopenharmony_ci			ret = 0;
184562306a36Sopenharmony_ci		}
184662306a36Sopenharmony_ci
184762306a36Sopenharmony_ci		if (unlikely(ret)) {
184862306a36Sopenharmony_ci			/*
184962306a36Sopenharmony_ci			 * Couldn't process descriptor, back off for recovery.
185062306a36Sopenharmony_ci			 * We use the SGE's last timer which has the longest
185162306a36Sopenharmony_ci			 * interrupt coalescing value ...
185262306a36Sopenharmony_ci			 */
185362306a36Sopenharmony_ci			const int NOMEM_TIMER_IDX = SGE_NTIMERS-1;
185462306a36Sopenharmony_ci			rspq->next_intr_params =
185562306a36Sopenharmony_ci				QINTR_TIMER_IDX_V(NOMEM_TIMER_IDX);
185662306a36Sopenharmony_ci			break;
185762306a36Sopenharmony_ci		}
185862306a36Sopenharmony_ci
185962306a36Sopenharmony_ci		rspq_next(rspq);
186062306a36Sopenharmony_ci		budget_left--;
186162306a36Sopenharmony_ci	}
186262306a36Sopenharmony_ci
186362306a36Sopenharmony_ci	/*
186462306a36Sopenharmony_ci	 * If this is a Response Queue with an associated Free List and
186562306a36Sopenharmony_ci	 * at least two Egress Queue units available in the Free List
186662306a36Sopenharmony_ci	 * for new buffer pointers, refill the Free List.
186762306a36Sopenharmony_ci	 */
186862306a36Sopenharmony_ci	if (rspq->offset >= 0 &&
186962306a36Sopenharmony_ci	    fl_cap(&rxq->fl) - rxq->fl.avail >= 2*FL_PER_EQ_UNIT)
187062306a36Sopenharmony_ci		__refill_fl(rspq->adapter, &rxq->fl);
187162306a36Sopenharmony_ci	return budget - budget_left;
187262306a36Sopenharmony_ci}
187362306a36Sopenharmony_ci
187462306a36Sopenharmony_ci/**
187562306a36Sopenharmony_ci *	napi_rx_handler - the NAPI handler for RX processing
187662306a36Sopenharmony_ci *	@napi: the napi instance
187762306a36Sopenharmony_ci *	@budget: how many packets we can process in this round
187862306a36Sopenharmony_ci *
187962306a36Sopenharmony_ci *	Handler for new data events when using NAPI.  This does not need any
188062306a36Sopenharmony_ci *	locking or protection from interrupts as data interrupts are off at
188162306a36Sopenharmony_ci *	this point and other adapter interrupts do not interfere (the latter
188262306a36Sopenharmony_ci *	in not a concern at all with MSI-X as non-data interrupts then have
188362306a36Sopenharmony_ci *	a separate handler).
188462306a36Sopenharmony_ci */
188562306a36Sopenharmony_cistatic int napi_rx_handler(struct napi_struct *napi, int budget)
188662306a36Sopenharmony_ci{
188762306a36Sopenharmony_ci	unsigned int intr_params;
188862306a36Sopenharmony_ci	struct sge_rspq *rspq = container_of(napi, struct sge_rspq, napi);
188962306a36Sopenharmony_ci	int work_done = process_responses(rspq, budget);
189062306a36Sopenharmony_ci	u32 val;
189162306a36Sopenharmony_ci
189262306a36Sopenharmony_ci	if (likely(work_done < budget)) {
189362306a36Sopenharmony_ci		napi_complete_done(napi, work_done);
189462306a36Sopenharmony_ci		intr_params = rspq->next_intr_params;
189562306a36Sopenharmony_ci		rspq->next_intr_params = rspq->intr_params;
189662306a36Sopenharmony_ci	} else
189762306a36Sopenharmony_ci		intr_params = QINTR_TIMER_IDX_V(SGE_TIMER_UPD_CIDX);
189862306a36Sopenharmony_ci
189962306a36Sopenharmony_ci	if (unlikely(work_done == 0))
190062306a36Sopenharmony_ci		rspq->unhandled_irqs++;
190162306a36Sopenharmony_ci
190262306a36Sopenharmony_ci	val = CIDXINC_V(work_done) | SEINTARM_V(intr_params);
190362306a36Sopenharmony_ci	/* If we don't have access to the new User GTS (T5+), use the old
190462306a36Sopenharmony_ci	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
190562306a36Sopenharmony_ci	 */
190662306a36Sopenharmony_ci	if (unlikely(!rspq->bar2_addr)) {
190762306a36Sopenharmony_ci		t4_write_reg(rspq->adapter,
190862306a36Sopenharmony_ci			     T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
190962306a36Sopenharmony_ci			     val | INGRESSQID_V((u32)rspq->cntxt_id));
191062306a36Sopenharmony_ci	} else {
191162306a36Sopenharmony_ci		writel(val | INGRESSQID_V(rspq->bar2_qid),
191262306a36Sopenharmony_ci		       rspq->bar2_addr + SGE_UDB_GTS);
191362306a36Sopenharmony_ci		wmb();
191462306a36Sopenharmony_ci	}
191562306a36Sopenharmony_ci	return work_done;
191662306a36Sopenharmony_ci}
191762306a36Sopenharmony_ci
191862306a36Sopenharmony_ci/*
191962306a36Sopenharmony_ci * The MSI-X interrupt handler for an SGE response queue for the NAPI case
192062306a36Sopenharmony_ci * (i.e., response queue serviced by NAPI polling).
192162306a36Sopenharmony_ci */
192262306a36Sopenharmony_ciirqreturn_t t4vf_sge_intr_msix(int irq, void *cookie)
192362306a36Sopenharmony_ci{
192462306a36Sopenharmony_ci	struct sge_rspq *rspq = cookie;
192562306a36Sopenharmony_ci
192662306a36Sopenharmony_ci	napi_schedule(&rspq->napi);
192762306a36Sopenharmony_ci	return IRQ_HANDLED;
192862306a36Sopenharmony_ci}
192962306a36Sopenharmony_ci
193062306a36Sopenharmony_ci/*
193162306a36Sopenharmony_ci * Process the indirect interrupt entries in the interrupt queue and kick off
193262306a36Sopenharmony_ci * NAPI for each queue that has generated an entry.
193362306a36Sopenharmony_ci */
193462306a36Sopenharmony_cistatic unsigned int process_intrq(struct adapter *adapter)
193562306a36Sopenharmony_ci{
193662306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
193762306a36Sopenharmony_ci	struct sge_rspq *intrq = &s->intrq;
193862306a36Sopenharmony_ci	unsigned int work_done;
193962306a36Sopenharmony_ci	u32 val;
194062306a36Sopenharmony_ci
194162306a36Sopenharmony_ci	spin_lock(&adapter->sge.intrq_lock);
194262306a36Sopenharmony_ci	for (work_done = 0; ; work_done++) {
194362306a36Sopenharmony_ci		const struct rsp_ctrl *rc;
194462306a36Sopenharmony_ci		unsigned int qid, iq_idx;
194562306a36Sopenharmony_ci		struct sge_rspq *rspq;
194662306a36Sopenharmony_ci
194762306a36Sopenharmony_ci		/*
194862306a36Sopenharmony_ci		 * Grab the next response from the interrupt queue and bail
194962306a36Sopenharmony_ci		 * out if it's not a new response.
195062306a36Sopenharmony_ci		 */
195162306a36Sopenharmony_ci		rc = (void *)intrq->cur_desc + (intrq->iqe_len - sizeof(*rc));
195262306a36Sopenharmony_ci		if (!is_new_response(rc, intrq))
195362306a36Sopenharmony_ci			break;
195462306a36Sopenharmony_ci
195562306a36Sopenharmony_ci		/*
195662306a36Sopenharmony_ci		 * If the response isn't a forwarded interrupt message issue a
195762306a36Sopenharmony_ci		 * error and go on to the next response message.  This should
195862306a36Sopenharmony_ci		 * never happen ...
195962306a36Sopenharmony_ci		 */
196062306a36Sopenharmony_ci		dma_rmb();
196162306a36Sopenharmony_ci		if (unlikely(RSPD_TYPE_G(rc->type_gen) != RSPD_TYPE_INTR_X)) {
196262306a36Sopenharmony_ci			dev_err(adapter->pdev_dev,
196362306a36Sopenharmony_ci				"Unexpected INTRQ response type %d\n",
196462306a36Sopenharmony_ci				RSPD_TYPE_G(rc->type_gen));
196562306a36Sopenharmony_ci			continue;
196662306a36Sopenharmony_ci		}
196762306a36Sopenharmony_ci
196862306a36Sopenharmony_ci		/*
196962306a36Sopenharmony_ci		 * Extract the Queue ID from the interrupt message and perform
197062306a36Sopenharmony_ci		 * sanity checking to make sure it really refers to one of our
197162306a36Sopenharmony_ci		 * Ingress Queues which is active and matches the queue's ID.
197262306a36Sopenharmony_ci		 * None of these error conditions should ever happen so we may
197362306a36Sopenharmony_ci		 * want to either make them fatal and/or conditionalized under
197462306a36Sopenharmony_ci		 * DEBUG.
197562306a36Sopenharmony_ci		 */
197662306a36Sopenharmony_ci		qid = RSPD_QID_G(be32_to_cpu(rc->pldbuflen_qid));
197762306a36Sopenharmony_ci		iq_idx = IQ_IDX(s, qid);
197862306a36Sopenharmony_ci		if (unlikely(iq_idx >= MAX_INGQ)) {
197962306a36Sopenharmony_ci			dev_err(adapter->pdev_dev,
198062306a36Sopenharmony_ci				"Ingress QID %d out of range\n", qid);
198162306a36Sopenharmony_ci			continue;
198262306a36Sopenharmony_ci		}
198362306a36Sopenharmony_ci		rspq = s->ingr_map[iq_idx];
198462306a36Sopenharmony_ci		if (unlikely(rspq == NULL)) {
198562306a36Sopenharmony_ci			dev_err(adapter->pdev_dev,
198662306a36Sopenharmony_ci				"Ingress QID %d RSPQ=NULL\n", qid);
198762306a36Sopenharmony_ci			continue;
198862306a36Sopenharmony_ci		}
198962306a36Sopenharmony_ci		if (unlikely(rspq->abs_id != qid)) {
199062306a36Sopenharmony_ci			dev_err(adapter->pdev_dev,
199162306a36Sopenharmony_ci				"Ingress QID %d refers to RSPQ %d\n",
199262306a36Sopenharmony_ci				qid, rspq->abs_id);
199362306a36Sopenharmony_ci			continue;
199462306a36Sopenharmony_ci		}
199562306a36Sopenharmony_ci
199662306a36Sopenharmony_ci		/*
199762306a36Sopenharmony_ci		 * Schedule NAPI processing on the indicated Response Queue
199862306a36Sopenharmony_ci		 * and move on to the next entry in the Forwarded Interrupt
199962306a36Sopenharmony_ci		 * Queue.
200062306a36Sopenharmony_ci		 */
200162306a36Sopenharmony_ci		napi_schedule(&rspq->napi);
200262306a36Sopenharmony_ci		rspq_next(intrq);
200362306a36Sopenharmony_ci	}
200462306a36Sopenharmony_ci
200562306a36Sopenharmony_ci	val = CIDXINC_V(work_done) | SEINTARM_V(intrq->intr_params);
200662306a36Sopenharmony_ci	/* If we don't have access to the new User GTS (T5+), use the old
200762306a36Sopenharmony_ci	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
200862306a36Sopenharmony_ci	 */
200962306a36Sopenharmony_ci	if (unlikely(!intrq->bar2_addr)) {
201062306a36Sopenharmony_ci		t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
201162306a36Sopenharmony_ci			     val | INGRESSQID_V(intrq->cntxt_id));
201262306a36Sopenharmony_ci	} else {
201362306a36Sopenharmony_ci		writel(val | INGRESSQID_V(intrq->bar2_qid),
201462306a36Sopenharmony_ci		       intrq->bar2_addr + SGE_UDB_GTS);
201562306a36Sopenharmony_ci		wmb();
201662306a36Sopenharmony_ci	}
201762306a36Sopenharmony_ci
201862306a36Sopenharmony_ci	spin_unlock(&adapter->sge.intrq_lock);
201962306a36Sopenharmony_ci
202062306a36Sopenharmony_ci	return work_done;
202162306a36Sopenharmony_ci}
202262306a36Sopenharmony_ci
202362306a36Sopenharmony_ci/*
202462306a36Sopenharmony_ci * The MSI interrupt handler handles data events from SGE response queues as
202562306a36Sopenharmony_ci * well as error and other async events as they all use the same MSI vector.
202662306a36Sopenharmony_ci */
202762306a36Sopenharmony_cistatic irqreturn_t t4vf_intr_msi(int irq, void *cookie)
202862306a36Sopenharmony_ci{
202962306a36Sopenharmony_ci	struct adapter *adapter = cookie;
203062306a36Sopenharmony_ci
203162306a36Sopenharmony_ci	process_intrq(adapter);
203262306a36Sopenharmony_ci	return IRQ_HANDLED;
203362306a36Sopenharmony_ci}
203462306a36Sopenharmony_ci
203562306a36Sopenharmony_ci/**
203662306a36Sopenharmony_ci *	t4vf_intr_handler - select the top-level interrupt handler
203762306a36Sopenharmony_ci *	@adapter: the adapter
203862306a36Sopenharmony_ci *
203962306a36Sopenharmony_ci *	Selects the top-level interrupt handler based on the type of interrupts
204062306a36Sopenharmony_ci *	(MSI-X or MSI).
204162306a36Sopenharmony_ci */
204262306a36Sopenharmony_ciirq_handler_t t4vf_intr_handler(struct adapter *adapter)
204362306a36Sopenharmony_ci{
204462306a36Sopenharmony_ci	BUG_ON((adapter->flags &
204562306a36Sopenharmony_ci	       (CXGB4VF_USING_MSIX | CXGB4VF_USING_MSI)) == 0);
204662306a36Sopenharmony_ci	if (adapter->flags & CXGB4VF_USING_MSIX)
204762306a36Sopenharmony_ci		return t4vf_sge_intr_msix;
204862306a36Sopenharmony_ci	else
204962306a36Sopenharmony_ci		return t4vf_intr_msi;
205062306a36Sopenharmony_ci}
205162306a36Sopenharmony_ci
205262306a36Sopenharmony_ci/**
205362306a36Sopenharmony_ci *	sge_rx_timer_cb - perform periodic maintenance of SGE RX queues
205462306a36Sopenharmony_ci *	@t: Rx timer
205562306a36Sopenharmony_ci *
205662306a36Sopenharmony_ci *	Runs periodically from a timer to perform maintenance of SGE RX queues.
205762306a36Sopenharmony_ci *
205862306a36Sopenharmony_ci *	a) Replenishes RX queues that have run out due to memory shortage.
205962306a36Sopenharmony_ci *	Normally new RX buffers are added when existing ones are consumed but
206062306a36Sopenharmony_ci *	when out of memory a queue can become empty.  We schedule NAPI to do
206162306a36Sopenharmony_ci *	the actual refill.
206262306a36Sopenharmony_ci */
206362306a36Sopenharmony_cistatic void sge_rx_timer_cb(struct timer_list *t)
206462306a36Sopenharmony_ci{
206562306a36Sopenharmony_ci	struct adapter *adapter = from_timer(adapter, t, sge.rx_timer);
206662306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
206762306a36Sopenharmony_ci	unsigned int i;
206862306a36Sopenharmony_ci
206962306a36Sopenharmony_ci	/*
207062306a36Sopenharmony_ci	 * Scan the "Starving Free Lists" flag array looking for any Free
207162306a36Sopenharmony_ci	 * Lists in need of more free buffers.  If we find one and it's not
207262306a36Sopenharmony_ci	 * being actively polled, then bump its "starving" counter and attempt
207362306a36Sopenharmony_ci	 * to refill it.  If we're successful in adding enough buffers to push
207462306a36Sopenharmony_ci	 * the Free List over the starving threshold, then we can clear its
207562306a36Sopenharmony_ci	 * "starving" status.
207662306a36Sopenharmony_ci	 */
207762306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++) {
207862306a36Sopenharmony_ci		unsigned long m;
207962306a36Sopenharmony_ci
208062306a36Sopenharmony_ci		for (m = s->starving_fl[i]; m; m &= m - 1) {
208162306a36Sopenharmony_ci			unsigned int id = __ffs(m) + i * BITS_PER_LONG;
208262306a36Sopenharmony_ci			struct sge_fl *fl = s->egr_map[id];
208362306a36Sopenharmony_ci
208462306a36Sopenharmony_ci			clear_bit(id, s->starving_fl);
208562306a36Sopenharmony_ci			smp_mb__after_atomic();
208662306a36Sopenharmony_ci
208762306a36Sopenharmony_ci			/*
208862306a36Sopenharmony_ci			 * Since we are accessing fl without a lock there's a
208962306a36Sopenharmony_ci			 * small probability of a false positive where we
209062306a36Sopenharmony_ci			 * schedule napi but the FL is no longer starving.
209162306a36Sopenharmony_ci			 * No biggie.
209262306a36Sopenharmony_ci			 */
209362306a36Sopenharmony_ci			if (fl_starving(adapter, fl)) {
209462306a36Sopenharmony_ci				struct sge_eth_rxq *rxq;
209562306a36Sopenharmony_ci
209662306a36Sopenharmony_ci				rxq = container_of(fl, struct sge_eth_rxq, fl);
209762306a36Sopenharmony_ci				if (napi_reschedule(&rxq->rspq.napi))
209862306a36Sopenharmony_ci					fl->starving++;
209962306a36Sopenharmony_ci				else
210062306a36Sopenharmony_ci					set_bit(id, s->starving_fl);
210162306a36Sopenharmony_ci			}
210262306a36Sopenharmony_ci		}
210362306a36Sopenharmony_ci	}
210462306a36Sopenharmony_ci
210562306a36Sopenharmony_ci	/*
210662306a36Sopenharmony_ci	 * Reschedule the next scan for starving Free Lists ...
210762306a36Sopenharmony_ci	 */
210862306a36Sopenharmony_ci	mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
210962306a36Sopenharmony_ci}
211062306a36Sopenharmony_ci
211162306a36Sopenharmony_ci/**
211262306a36Sopenharmony_ci *	sge_tx_timer_cb - perform periodic maintenance of SGE Tx queues
211362306a36Sopenharmony_ci *	@t: Tx timer
211462306a36Sopenharmony_ci *
211562306a36Sopenharmony_ci *	Runs periodically from a timer to perform maintenance of SGE TX queues.
211662306a36Sopenharmony_ci *
211762306a36Sopenharmony_ci *	b) Reclaims completed Tx packets for the Ethernet queues.  Normally
211862306a36Sopenharmony_ci *	packets are cleaned up by new Tx packets, this timer cleans up packets
211962306a36Sopenharmony_ci *	when no new packets are being submitted.  This is essential for pktgen,
212062306a36Sopenharmony_ci *	at least.
212162306a36Sopenharmony_ci */
212262306a36Sopenharmony_cistatic void sge_tx_timer_cb(struct timer_list *t)
212362306a36Sopenharmony_ci{
212462306a36Sopenharmony_ci	struct adapter *adapter = from_timer(adapter, t, sge.tx_timer);
212562306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
212662306a36Sopenharmony_ci	unsigned int i, budget;
212762306a36Sopenharmony_ci
212862306a36Sopenharmony_ci	budget = MAX_TIMER_TX_RECLAIM;
212962306a36Sopenharmony_ci	i = s->ethtxq_rover;
213062306a36Sopenharmony_ci	do {
213162306a36Sopenharmony_ci		struct sge_eth_txq *txq = &s->ethtxq[i];
213262306a36Sopenharmony_ci
213362306a36Sopenharmony_ci		if (reclaimable(&txq->q) && __netif_tx_trylock(txq->txq)) {
213462306a36Sopenharmony_ci			int avail = reclaimable(&txq->q);
213562306a36Sopenharmony_ci
213662306a36Sopenharmony_ci			if (avail > budget)
213762306a36Sopenharmony_ci				avail = budget;
213862306a36Sopenharmony_ci
213962306a36Sopenharmony_ci			free_tx_desc(adapter, &txq->q, avail, true);
214062306a36Sopenharmony_ci			txq->q.in_use -= avail;
214162306a36Sopenharmony_ci			__netif_tx_unlock(txq->txq);
214262306a36Sopenharmony_ci
214362306a36Sopenharmony_ci			budget -= avail;
214462306a36Sopenharmony_ci			if (!budget)
214562306a36Sopenharmony_ci				break;
214662306a36Sopenharmony_ci		}
214762306a36Sopenharmony_ci
214862306a36Sopenharmony_ci		i++;
214962306a36Sopenharmony_ci		if (i >= s->ethqsets)
215062306a36Sopenharmony_ci			i = 0;
215162306a36Sopenharmony_ci	} while (i != s->ethtxq_rover);
215262306a36Sopenharmony_ci	s->ethtxq_rover = i;
215362306a36Sopenharmony_ci
215462306a36Sopenharmony_ci	/*
215562306a36Sopenharmony_ci	 * If we found too many reclaimable packets schedule a timer in the
215662306a36Sopenharmony_ci	 * near future to continue where we left off.  Otherwise the next timer
215762306a36Sopenharmony_ci	 * will be at its normal interval.
215862306a36Sopenharmony_ci	 */
215962306a36Sopenharmony_ci	mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
216062306a36Sopenharmony_ci}
216162306a36Sopenharmony_ci
216262306a36Sopenharmony_ci/**
216362306a36Sopenharmony_ci *	bar2_address - return the BAR2 address for an SGE Queue's Registers
216462306a36Sopenharmony_ci *	@adapter: the adapter
216562306a36Sopenharmony_ci *	@qid: the SGE Queue ID
216662306a36Sopenharmony_ci *	@qtype: the SGE Queue Type (Egress or Ingress)
216762306a36Sopenharmony_ci *	@pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
216862306a36Sopenharmony_ci *
216962306a36Sopenharmony_ci *	Returns the BAR2 address for the SGE Queue Registers associated with
217062306a36Sopenharmony_ci *	@qid.  If BAR2 SGE Registers aren't available, returns NULL.  Also
217162306a36Sopenharmony_ci *	returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
217262306a36Sopenharmony_ci *	Queue Registers.  If the BAR2 Queue ID is 0, then "Inferred Queue ID"
217362306a36Sopenharmony_ci *	Registers are supported (e.g. the Write Combining Doorbell Buffer).
217462306a36Sopenharmony_ci */
217562306a36Sopenharmony_cistatic void __iomem *bar2_address(struct adapter *adapter,
217662306a36Sopenharmony_ci				  unsigned int qid,
217762306a36Sopenharmony_ci				  enum t4_bar2_qtype qtype,
217862306a36Sopenharmony_ci				  unsigned int *pbar2_qid)
217962306a36Sopenharmony_ci{
218062306a36Sopenharmony_ci	u64 bar2_qoffset;
218162306a36Sopenharmony_ci	int ret;
218262306a36Sopenharmony_ci
218362306a36Sopenharmony_ci	ret = t4vf_bar2_sge_qregs(adapter, qid, qtype,
218462306a36Sopenharmony_ci				  &bar2_qoffset, pbar2_qid);
218562306a36Sopenharmony_ci	if (ret)
218662306a36Sopenharmony_ci		return NULL;
218762306a36Sopenharmony_ci
218862306a36Sopenharmony_ci	return adapter->bar2 + bar2_qoffset;
218962306a36Sopenharmony_ci}
219062306a36Sopenharmony_ci
219162306a36Sopenharmony_ci/**
219262306a36Sopenharmony_ci *	t4vf_sge_alloc_rxq - allocate an SGE RX Queue
219362306a36Sopenharmony_ci *	@adapter: the adapter
219462306a36Sopenharmony_ci *	@rspq: pointer to to the new rxq's Response Queue to be filled in
219562306a36Sopenharmony_ci *	@iqasynch: if 0, a normal rspq; if 1, an asynchronous event queue
219662306a36Sopenharmony_ci *	@dev: the network device associated with the new rspq
219762306a36Sopenharmony_ci *	@intr_dest: MSI-X vector index (overriden in MSI mode)
219862306a36Sopenharmony_ci *	@fl: pointer to the new rxq's Free List to be filled in
219962306a36Sopenharmony_ci *	@hnd: the interrupt handler to invoke for the rspq
220062306a36Sopenharmony_ci */
220162306a36Sopenharmony_ciint t4vf_sge_alloc_rxq(struct adapter *adapter, struct sge_rspq *rspq,
220262306a36Sopenharmony_ci		       bool iqasynch, struct net_device *dev,
220362306a36Sopenharmony_ci		       int intr_dest,
220462306a36Sopenharmony_ci		       struct sge_fl *fl, rspq_handler_t hnd)
220562306a36Sopenharmony_ci{
220662306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
220762306a36Sopenharmony_ci	struct port_info *pi = netdev_priv(dev);
220862306a36Sopenharmony_ci	struct fw_iq_cmd cmd, rpl;
220962306a36Sopenharmony_ci	int ret, iqandst, flsz = 0;
221062306a36Sopenharmony_ci	int relaxed = !(adapter->flags & CXGB4VF_ROOT_NO_RELAXED_ORDERING);
221162306a36Sopenharmony_ci
221262306a36Sopenharmony_ci	/*
221362306a36Sopenharmony_ci	 * If we're using MSI interrupts and we're not initializing the
221462306a36Sopenharmony_ci	 * Forwarded Interrupt Queue itself, then set up this queue for
221562306a36Sopenharmony_ci	 * indirect interrupts to the Forwarded Interrupt Queue.  Obviously
221662306a36Sopenharmony_ci	 * the Forwarded Interrupt Queue must be set up before any other
221762306a36Sopenharmony_ci	 * ingress queue ...
221862306a36Sopenharmony_ci	 */
221962306a36Sopenharmony_ci	if ((adapter->flags & CXGB4VF_USING_MSI) &&
222062306a36Sopenharmony_ci	    rspq != &adapter->sge.intrq) {
222162306a36Sopenharmony_ci		iqandst = SGE_INTRDST_IQ;
222262306a36Sopenharmony_ci		intr_dest = adapter->sge.intrq.abs_id;
222362306a36Sopenharmony_ci	} else
222462306a36Sopenharmony_ci		iqandst = SGE_INTRDST_PCI;
222562306a36Sopenharmony_ci
222662306a36Sopenharmony_ci	/*
222762306a36Sopenharmony_ci	 * Allocate the hardware ring for the Response Queue.  The size needs
222862306a36Sopenharmony_ci	 * to be a multiple of 16 which includes the mandatory status entry
222962306a36Sopenharmony_ci	 * (regardless of whether the Status Page capabilities are enabled or
223062306a36Sopenharmony_ci	 * not).
223162306a36Sopenharmony_ci	 */
223262306a36Sopenharmony_ci	rspq->size = roundup(rspq->size, 16);
223362306a36Sopenharmony_ci	rspq->desc = alloc_ring(adapter->pdev_dev, rspq->size, rspq->iqe_len,
223462306a36Sopenharmony_ci				0, &rspq->phys_addr, NULL, 0);
223562306a36Sopenharmony_ci	if (!rspq->desc)
223662306a36Sopenharmony_ci		return -ENOMEM;
223762306a36Sopenharmony_ci
223862306a36Sopenharmony_ci	/*
223962306a36Sopenharmony_ci	 * Fill in the Ingress Queue Command.  Note: Ideally this code would
224062306a36Sopenharmony_ci	 * be in t4vf_hw.c but there are so many parameters and dependencies
224162306a36Sopenharmony_ci	 * on our Linux SGE state that we would end up having to pass tons of
224262306a36Sopenharmony_ci	 * parameters.  We'll have to think about how this might be migrated
224362306a36Sopenharmony_ci	 * into OS-independent common code ...
224462306a36Sopenharmony_ci	 */
224562306a36Sopenharmony_ci	memset(&cmd, 0, sizeof(cmd));
224662306a36Sopenharmony_ci	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_IQ_CMD) |
224762306a36Sopenharmony_ci				    FW_CMD_REQUEST_F |
224862306a36Sopenharmony_ci				    FW_CMD_WRITE_F |
224962306a36Sopenharmony_ci				    FW_CMD_EXEC_F);
225062306a36Sopenharmony_ci	cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_ALLOC_F |
225162306a36Sopenharmony_ci					 FW_IQ_CMD_IQSTART_F |
225262306a36Sopenharmony_ci					 FW_LEN16(cmd));
225362306a36Sopenharmony_ci	cmd.type_to_iqandstindex =
225462306a36Sopenharmony_ci		cpu_to_be32(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
225562306a36Sopenharmony_ci			    FW_IQ_CMD_IQASYNCH_V(iqasynch) |
225662306a36Sopenharmony_ci			    FW_IQ_CMD_VIID_V(pi->viid) |
225762306a36Sopenharmony_ci			    FW_IQ_CMD_IQANDST_V(iqandst) |
225862306a36Sopenharmony_ci			    FW_IQ_CMD_IQANUS_V(1) |
225962306a36Sopenharmony_ci			    FW_IQ_CMD_IQANUD_V(SGE_UPDATEDEL_INTR) |
226062306a36Sopenharmony_ci			    FW_IQ_CMD_IQANDSTINDEX_V(intr_dest));
226162306a36Sopenharmony_ci	cmd.iqdroprss_to_iqesize =
226262306a36Sopenharmony_ci		cpu_to_be16(FW_IQ_CMD_IQPCIECH_V(pi->port_id) |
226362306a36Sopenharmony_ci			    FW_IQ_CMD_IQGTSMODE_F |
226462306a36Sopenharmony_ci			    FW_IQ_CMD_IQINTCNTTHRESH_V(rspq->pktcnt_idx) |
226562306a36Sopenharmony_ci			    FW_IQ_CMD_IQESIZE_V(ilog2(rspq->iqe_len) - 4));
226662306a36Sopenharmony_ci	cmd.iqsize = cpu_to_be16(rspq->size);
226762306a36Sopenharmony_ci	cmd.iqaddr = cpu_to_be64(rspq->phys_addr);
226862306a36Sopenharmony_ci
226962306a36Sopenharmony_ci	if (fl) {
227062306a36Sopenharmony_ci		unsigned int chip_ver =
227162306a36Sopenharmony_ci			CHELSIO_CHIP_VERSION(adapter->params.chip);
227262306a36Sopenharmony_ci		/*
227362306a36Sopenharmony_ci		 * Allocate the ring for the hardware free list (with space
227462306a36Sopenharmony_ci		 * for its status page) along with the associated software
227562306a36Sopenharmony_ci		 * descriptor ring.  The free list size needs to be a multiple
227662306a36Sopenharmony_ci		 * of the Egress Queue Unit and at least 2 Egress Units larger
227762306a36Sopenharmony_ci		 * than the SGE's Egress Congrestion Threshold
227862306a36Sopenharmony_ci		 * (fl_starve_thres - 1).
227962306a36Sopenharmony_ci		 */
228062306a36Sopenharmony_ci		if (fl->size < s->fl_starve_thres - 1 + 2 * FL_PER_EQ_UNIT)
228162306a36Sopenharmony_ci			fl->size = s->fl_starve_thres - 1 + 2 * FL_PER_EQ_UNIT;
228262306a36Sopenharmony_ci		fl->size = roundup(fl->size, FL_PER_EQ_UNIT);
228362306a36Sopenharmony_ci		fl->desc = alloc_ring(adapter->pdev_dev, fl->size,
228462306a36Sopenharmony_ci				      sizeof(__be64), sizeof(struct rx_sw_desc),
228562306a36Sopenharmony_ci				      &fl->addr, &fl->sdesc, s->stat_len);
228662306a36Sopenharmony_ci		if (!fl->desc) {
228762306a36Sopenharmony_ci			ret = -ENOMEM;
228862306a36Sopenharmony_ci			goto err;
228962306a36Sopenharmony_ci		}
229062306a36Sopenharmony_ci
229162306a36Sopenharmony_ci		/*
229262306a36Sopenharmony_ci		 * Calculate the size of the hardware free list ring plus
229362306a36Sopenharmony_ci		 * Status Page (which the SGE will place after the end of the
229462306a36Sopenharmony_ci		 * free list ring) in Egress Queue Units.
229562306a36Sopenharmony_ci		 */
229662306a36Sopenharmony_ci		flsz = (fl->size / FL_PER_EQ_UNIT +
229762306a36Sopenharmony_ci			s->stat_len / EQ_UNIT);
229862306a36Sopenharmony_ci
229962306a36Sopenharmony_ci		/*
230062306a36Sopenharmony_ci		 * Fill in all the relevant firmware Ingress Queue Command
230162306a36Sopenharmony_ci		 * fields for the free list.
230262306a36Sopenharmony_ci		 */
230362306a36Sopenharmony_ci		cmd.iqns_to_fl0congen =
230462306a36Sopenharmony_ci			cpu_to_be32(
230562306a36Sopenharmony_ci				FW_IQ_CMD_FL0HOSTFCMODE_V(SGE_HOSTFCMODE_NONE) |
230662306a36Sopenharmony_ci				FW_IQ_CMD_FL0PACKEN_F |
230762306a36Sopenharmony_ci				FW_IQ_CMD_FL0FETCHRO_V(relaxed) |
230862306a36Sopenharmony_ci				FW_IQ_CMD_FL0DATARO_V(relaxed) |
230962306a36Sopenharmony_ci				FW_IQ_CMD_FL0PADEN_F);
231062306a36Sopenharmony_ci
231162306a36Sopenharmony_ci		/* In T6, for egress queue type FL there is internal overhead
231262306a36Sopenharmony_ci		 * of 16B for header going into FLM module.  Hence the maximum
231362306a36Sopenharmony_ci		 * allowed burst size is 448 bytes.  For T4/T5, the hardware
231462306a36Sopenharmony_ci		 * doesn't coalesce fetch requests if more than 64 bytes of
231562306a36Sopenharmony_ci		 * Free List pointers are provided, so we use a 128-byte Fetch
231662306a36Sopenharmony_ci		 * Burst Minimum there (T6 implements coalescing so we can use
231762306a36Sopenharmony_ci		 * the smaller 64-byte value there).
231862306a36Sopenharmony_ci		 */
231962306a36Sopenharmony_ci		cmd.fl0dcaen_to_fl0cidxfthresh =
232062306a36Sopenharmony_ci			cpu_to_be16(
232162306a36Sopenharmony_ci				FW_IQ_CMD_FL0FBMIN_V(chip_ver <= CHELSIO_T5
232262306a36Sopenharmony_ci						     ? FETCHBURSTMIN_128B_X
232362306a36Sopenharmony_ci						     : FETCHBURSTMIN_64B_T6_X) |
232462306a36Sopenharmony_ci				FW_IQ_CMD_FL0FBMAX_V((chip_ver <= CHELSIO_T5) ?
232562306a36Sopenharmony_ci						     FETCHBURSTMAX_512B_X :
232662306a36Sopenharmony_ci						     FETCHBURSTMAX_256B_X));
232762306a36Sopenharmony_ci		cmd.fl0size = cpu_to_be16(flsz);
232862306a36Sopenharmony_ci		cmd.fl0addr = cpu_to_be64(fl->addr);
232962306a36Sopenharmony_ci	}
233062306a36Sopenharmony_ci
233162306a36Sopenharmony_ci	/*
233262306a36Sopenharmony_ci	 * Issue the firmware Ingress Queue Command and extract the results if
233362306a36Sopenharmony_ci	 * it completes successfully.
233462306a36Sopenharmony_ci	 */
233562306a36Sopenharmony_ci	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
233662306a36Sopenharmony_ci	if (ret)
233762306a36Sopenharmony_ci		goto err;
233862306a36Sopenharmony_ci
233962306a36Sopenharmony_ci	netif_napi_add(dev, &rspq->napi, napi_rx_handler);
234062306a36Sopenharmony_ci	rspq->cur_desc = rspq->desc;
234162306a36Sopenharmony_ci	rspq->cidx = 0;
234262306a36Sopenharmony_ci	rspq->gen = 1;
234362306a36Sopenharmony_ci	rspq->next_intr_params = rspq->intr_params;
234462306a36Sopenharmony_ci	rspq->cntxt_id = be16_to_cpu(rpl.iqid);
234562306a36Sopenharmony_ci	rspq->bar2_addr = bar2_address(adapter,
234662306a36Sopenharmony_ci				       rspq->cntxt_id,
234762306a36Sopenharmony_ci				       T4_BAR2_QTYPE_INGRESS,
234862306a36Sopenharmony_ci				       &rspq->bar2_qid);
234962306a36Sopenharmony_ci	rspq->abs_id = be16_to_cpu(rpl.physiqid);
235062306a36Sopenharmony_ci	rspq->size--;			/* subtract status entry */
235162306a36Sopenharmony_ci	rspq->adapter = adapter;
235262306a36Sopenharmony_ci	rspq->netdev = dev;
235362306a36Sopenharmony_ci	rspq->handler = hnd;
235462306a36Sopenharmony_ci
235562306a36Sopenharmony_ci	/* set offset to -1 to distinguish ingress queues without FL */
235662306a36Sopenharmony_ci	rspq->offset = fl ? 0 : -1;
235762306a36Sopenharmony_ci
235862306a36Sopenharmony_ci	if (fl) {
235962306a36Sopenharmony_ci		fl->cntxt_id = be16_to_cpu(rpl.fl0id);
236062306a36Sopenharmony_ci		fl->avail = 0;
236162306a36Sopenharmony_ci		fl->pend_cred = 0;
236262306a36Sopenharmony_ci		fl->pidx = 0;
236362306a36Sopenharmony_ci		fl->cidx = 0;
236462306a36Sopenharmony_ci		fl->alloc_failed = 0;
236562306a36Sopenharmony_ci		fl->large_alloc_failed = 0;
236662306a36Sopenharmony_ci		fl->starving = 0;
236762306a36Sopenharmony_ci
236862306a36Sopenharmony_ci		/* Note, we must initialize the BAR2 Free List User Doorbell
236962306a36Sopenharmony_ci		 * information before refilling the Free List!
237062306a36Sopenharmony_ci		 */
237162306a36Sopenharmony_ci		fl->bar2_addr = bar2_address(adapter,
237262306a36Sopenharmony_ci					     fl->cntxt_id,
237362306a36Sopenharmony_ci					     T4_BAR2_QTYPE_EGRESS,
237462306a36Sopenharmony_ci					     &fl->bar2_qid);
237562306a36Sopenharmony_ci
237662306a36Sopenharmony_ci		refill_fl(adapter, fl, fl_cap(fl), GFP_KERNEL);
237762306a36Sopenharmony_ci	}
237862306a36Sopenharmony_ci
237962306a36Sopenharmony_ci	return 0;
238062306a36Sopenharmony_ci
238162306a36Sopenharmony_cierr:
238262306a36Sopenharmony_ci	/*
238362306a36Sopenharmony_ci	 * An error occurred.  Clean up our partial allocation state and
238462306a36Sopenharmony_ci	 * return the error.
238562306a36Sopenharmony_ci	 */
238662306a36Sopenharmony_ci	if (rspq->desc) {
238762306a36Sopenharmony_ci		dma_free_coherent(adapter->pdev_dev, rspq->size * rspq->iqe_len,
238862306a36Sopenharmony_ci				  rspq->desc, rspq->phys_addr);
238962306a36Sopenharmony_ci		rspq->desc = NULL;
239062306a36Sopenharmony_ci	}
239162306a36Sopenharmony_ci	if (fl && fl->desc) {
239262306a36Sopenharmony_ci		kfree(fl->sdesc);
239362306a36Sopenharmony_ci		fl->sdesc = NULL;
239462306a36Sopenharmony_ci		dma_free_coherent(adapter->pdev_dev, flsz * EQ_UNIT,
239562306a36Sopenharmony_ci				  fl->desc, fl->addr);
239662306a36Sopenharmony_ci		fl->desc = NULL;
239762306a36Sopenharmony_ci	}
239862306a36Sopenharmony_ci	return ret;
239962306a36Sopenharmony_ci}
240062306a36Sopenharmony_ci
240162306a36Sopenharmony_ci/**
240262306a36Sopenharmony_ci *	t4vf_sge_alloc_eth_txq - allocate an SGE Ethernet TX Queue
240362306a36Sopenharmony_ci *	@adapter: the adapter
240462306a36Sopenharmony_ci *	@txq: pointer to the new txq to be filled in
240562306a36Sopenharmony_ci *	@dev: the network device
240662306a36Sopenharmony_ci *	@devq: the network TX queue associated with the new txq
240762306a36Sopenharmony_ci *	@iqid: the relative ingress queue ID to which events relating to
240862306a36Sopenharmony_ci *		the new txq should be directed
240962306a36Sopenharmony_ci */
241062306a36Sopenharmony_ciint t4vf_sge_alloc_eth_txq(struct adapter *adapter, struct sge_eth_txq *txq,
241162306a36Sopenharmony_ci			   struct net_device *dev, struct netdev_queue *devq,
241262306a36Sopenharmony_ci			   unsigned int iqid)
241362306a36Sopenharmony_ci{
241462306a36Sopenharmony_ci	unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
241562306a36Sopenharmony_ci	struct port_info *pi = netdev_priv(dev);
241662306a36Sopenharmony_ci	struct fw_eq_eth_cmd cmd, rpl;
241762306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
241862306a36Sopenharmony_ci	int ret, nentries;
241962306a36Sopenharmony_ci
242062306a36Sopenharmony_ci	/*
242162306a36Sopenharmony_ci	 * Calculate the size of the hardware TX Queue (including the Status
242262306a36Sopenharmony_ci	 * Page on the end of the TX Queue) in units of TX Descriptors.
242362306a36Sopenharmony_ci	 */
242462306a36Sopenharmony_ci	nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
242562306a36Sopenharmony_ci
242662306a36Sopenharmony_ci	/*
242762306a36Sopenharmony_ci	 * Allocate the hardware ring for the TX ring (with space for its
242862306a36Sopenharmony_ci	 * status page) along with the associated software descriptor ring.
242962306a36Sopenharmony_ci	 */
243062306a36Sopenharmony_ci	txq->q.desc = alloc_ring(adapter->pdev_dev, txq->q.size,
243162306a36Sopenharmony_ci				 sizeof(struct tx_desc),
243262306a36Sopenharmony_ci				 sizeof(struct tx_sw_desc),
243362306a36Sopenharmony_ci				 &txq->q.phys_addr, &txq->q.sdesc, s->stat_len);
243462306a36Sopenharmony_ci	if (!txq->q.desc)
243562306a36Sopenharmony_ci		return -ENOMEM;
243662306a36Sopenharmony_ci
243762306a36Sopenharmony_ci	/*
243862306a36Sopenharmony_ci	 * Fill in the Egress Queue Command.  Note: As with the direct use of
243962306a36Sopenharmony_ci	 * the firmware Ingress Queue COmmand above in our RXQ allocation
244062306a36Sopenharmony_ci	 * routine, ideally, this code would be in t4vf_hw.c.  Again, we'll
244162306a36Sopenharmony_ci	 * have to see if there's some reasonable way to parameterize it
244262306a36Sopenharmony_ci	 * into the common code ...
244362306a36Sopenharmony_ci	 */
244462306a36Sopenharmony_ci	memset(&cmd, 0, sizeof(cmd));
244562306a36Sopenharmony_ci	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_EQ_ETH_CMD) |
244662306a36Sopenharmony_ci				    FW_CMD_REQUEST_F |
244762306a36Sopenharmony_ci				    FW_CMD_WRITE_F |
244862306a36Sopenharmony_ci				    FW_CMD_EXEC_F);
244962306a36Sopenharmony_ci	cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_ALLOC_F |
245062306a36Sopenharmony_ci					 FW_EQ_ETH_CMD_EQSTART_F |
245162306a36Sopenharmony_ci					 FW_LEN16(cmd));
245262306a36Sopenharmony_ci	cmd.autoequiqe_to_viid = cpu_to_be32(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
245362306a36Sopenharmony_ci					     FW_EQ_ETH_CMD_VIID_V(pi->viid));
245462306a36Sopenharmony_ci	cmd.fetchszm_to_iqid =
245562306a36Sopenharmony_ci		cpu_to_be32(FW_EQ_ETH_CMD_HOSTFCMODE_V(SGE_HOSTFCMODE_STPG) |
245662306a36Sopenharmony_ci			    FW_EQ_ETH_CMD_PCIECHN_V(pi->port_id) |
245762306a36Sopenharmony_ci			    FW_EQ_ETH_CMD_IQID_V(iqid));
245862306a36Sopenharmony_ci	cmd.dcaen_to_eqsize =
245962306a36Sopenharmony_ci		cpu_to_be32(FW_EQ_ETH_CMD_FBMIN_V(chip_ver <= CHELSIO_T5
246062306a36Sopenharmony_ci						  ? FETCHBURSTMIN_64B_X
246162306a36Sopenharmony_ci						  : FETCHBURSTMIN_64B_T6_X) |
246262306a36Sopenharmony_ci			    FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
246362306a36Sopenharmony_ci			    FW_EQ_ETH_CMD_CIDXFTHRESH_V(
246462306a36Sopenharmony_ci						CIDXFLUSHTHRESH_32_X) |
246562306a36Sopenharmony_ci			    FW_EQ_ETH_CMD_EQSIZE_V(nentries));
246662306a36Sopenharmony_ci	cmd.eqaddr = cpu_to_be64(txq->q.phys_addr);
246762306a36Sopenharmony_ci
246862306a36Sopenharmony_ci	/*
246962306a36Sopenharmony_ci	 * Issue the firmware Egress Queue Command and extract the results if
247062306a36Sopenharmony_ci	 * it completes successfully.
247162306a36Sopenharmony_ci	 */
247262306a36Sopenharmony_ci	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
247362306a36Sopenharmony_ci	if (ret) {
247462306a36Sopenharmony_ci		/*
247562306a36Sopenharmony_ci		 * The girmware Ingress Queue Command failed for some reason.
247662306a36Sopenharmony_ci		 * Free up our partial allocation state and return the error.
247762306a36Sopenharmony_ci		 */
247862306a36Sopenharmony_ci		kfree(txq->q.sdesc);
247962306a36Sopenharmony_ci		txq->q.sdesc = NULL;
248062306a36Sopenharmony_ci		dma_free_coherent(adapter->pdev_dev,
248162306a36Sopenharmony_ci				  nentries * sizeof(struct tx_desc),
248262306a36Sopenharmony_ci				  txq->q.desc, txq->q.phys_addr);
248362306a36Sopenharmony_ci		txq->q.desc = NULL;
248462306a36Sopenharmony_ci		return ret;
248562306a36Sopenharmony_ci	}
248662306a36Sopenharmony_ci
248762306a36Sopenharmony_ci	txq->q.in_use = 0;
248862306a36Sopenharmony_ci	txq->q.cidx = 0;
248962306a36Sopenharmony_ci	txq->q.pidx = 0;
249062306a36Sopenharmony_ci	txq->q.stat = (void *)&txq->q.desc[txq->q.size];
249162306a36Sopenharmony_ci	txq->q.cntxt_id = FW_EQ_ETH_CMD_EQID_G(be32_to_cpu(rpl.eqid_pkd));
249262306a36Sopenharmony_ci	txq->q.bar2_addr = bar2_address(adapter,
249362306a36Sopenharmony_ci					txq->q.cntxt_id,
249462306a36Sopenharmony_ci					T4_BAR2_QTYPE_EGRESS,
249562306a36Sopenharmony_ci					&txq->q.bar2_qid);
249662306a36Sopenharmony_ci	txq->q.abs_id =
249762306a36Sopenharmony_ci		FW_EQ_ETH_CMD_PHYSEQID_G(be32_to_cpu(rpl.physeqid_pkd));
249862306a36Sopenharmony_ci	txq->txq = devq;
249962306a36Sopenharmony_ci	txq->tso = 0;
250062306a36Sopenharmony_ci	txq->tx_cso = 0;
250162306a36Sopenharmony_ci	txq->vlan_ins = 0;
250262306a36Sopenharmony_ci	txq->q.stops = 0;
250362306a36Sopenharmony_ci	txq->q.restarts = 0;
250462306a36Sopenharmony_ci	txq->mapping_err = 0;
250562306a36Sopenharmony_ci	return 0;
250662306a36Sopenharmony_ci}
250762306a36Sopenharmony_ci
250862306a36Sopenharmony_ci/*
250962306a36Sopenharmony_ci * Free the DMA map resources associated with a TX queue.
251062306a36Sopenharmony_ci */
251162306a36Sopenharmony_cistatic void free_txq(struct adapter *adapter, struct sge_txq *tq)
251262306a36Sopenharmony_ci{
251362306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
251462306a36Sopenharmony_ci
251562306a36Sopenharmony_ci	dma_free_coherent(adapter->pdev_dev,
251662306a36Sopenharmony_ci			  tq->size * sizeof(*tq->desc) + s->stat_len,
251762306a36Sopenharmony_ci			  tq->desc, tq->phys_addr);
251862306a36Sopenharmony_ci	tq->cntxt_id = 0;
251962306a36Sopenharmony_ci	tq->sdesc = NULL;
252062306a36Sopenharmony_ci	tq->desc = NULL;
252162306a36Sopenharmony_ci}
252262306a36Sopenharmony_ci
252362306a36Sopenharmony_ci/*
252462306a36Sopenharmony_ci * Free the resources associated with a response queue (possibly including a
252562306a36Sopenharmony_ci * free list).
252662306a36Sopenharmony_ci */
252762306a36Sopenharmony_cistatic void free_rspq_fl(struct adapter *adapter, struct sge_rspq *rspq,
252862306a36Sopenharmony_ci			 struct sge_fl *fl)
252962306a36Sopenharmony_ci{
253062306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
253162306a36Sopenharmony_ci	unsigned int flid = fl ? fl->cntxt_id : 0xffff;
253262306a36Sopenharmony_ci
253362306a36Sopenharmony_ci	t4vf_iq_free(adapter, FW_IQ_TYPE_FL_INT_CAP,
253462306a36Sopenharmony_ci		     rspq->cntxt_id, flid, 0xffff);
253562306a36Sopenharmony_ci	dma_free_coherent(adapter->pdev_dev, (rspq->size + 1) * rspq->iqe_len,
253662306a36Sopenharmony_ci			  rspq->desc, rspq->phys_addr);
253762306a36Sopenharmony_ci	netif_napi_del(&rspq->napi);
253862306a36Sopenharmony_ci	rspq->netdev = NULL;
253962306a36Sopenharmony_ci	rspq->cntxt_id = 0;
254062306a36Sopenharmony_ci	rspq->abs_id = 0;
254162306a36Sopenharmony_ci	rspq->desc = NULL;
254262306a36Sopenharmony_ci
254362306a36Sopenharmony_ci	if (fl) {
254462306a36Sopenharmony_ci		free_rx_bufs(adapter, fl, fl->avail);
254562306a36Sopenharmony_ci		dma_free_coherent(adapter->pdev_dev,
254662306a36Sopenharmony_ci				  fl->size * sizeof(*fl->desc) + s->stat_len,
254762306a36Sopenharmony_ci				  fl->desc, fl->addr);
254862306a36Sopenharmony_ci		kfree(fl->sdesc);
254962306a36Sopenharmony_ci		fl->sdesc = NULL;
255062306a36Sopenharmony_ci		fl->cntxt_id = 0;
255162306a36Sopenharmony_ci		fl->desc = NULL;
255262306a36Sopenharmony_ci	}
255362306a36Sopenharmony_ci}
255462306a36Sopenharmony_ci
255562306a36Sopenharmony_ci/**
255662306a36Sopenharmony_ci *	t4vf_free_sge_resources - free SGE resources
255762306a36Sopenharmony_ci *	@adapter: the adapter
255862306a36Sopenharmony_ci *
255962306a36Sopenharmony_ci *	Frees resources used by the SGE queue sets.
256062306a36Sopenharmony_ci */
256162306a36Sopenharmony_civoid t4vf_free_sge_resources(struct adapter *adapter)
256262306a36Sopenharmony_ci{
256362306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
256462306a36Sopenharmony_ci	struct sge_eth_rxq *rxq = s->ethrxq;
256562306a36Sopenharmony_ci	struct sge_eth_txq *txq = s->ethtxq;
256662306a36Sopenharmony_ci	struct sge_rspq *evtq = &s->fw_evtq;
256762306a36Sopenharmony_ci	struct sge_rspq *intrq = &s->intrq;
256862306a36Sopenharmony_ci	int qs;
256962306a36Sopenharmony_ci
257062306a36Sopenharmony_ci	for (qs = 0; qs < adapter->sge.ethqsets; qs++, rxq++, txq++) {
257162306a36Sopenharmony_ci		if (rxq->rspq.desc)
257262306a36Sopenharmony_ci			free_rspq_fl(adapter, &rxq->rspq, &rxq->fl);
257362306a36Sopenharmony_ci		if (txq->q.desc) {
257462306a36Sopenharmony_ci			t4vf_eth_eq_free(adapter, txq->q.cntxt_id);
257562306a36Sopenharmony_ci			free_tx_desc(adapter, &txq->q, txq->q.in_use, true);
257662306a36Sopenharmony_ci			kfree(txq->q.sdesc);
257762306a36Sopenharmony_ci			free_txq(adapter, &txq->q);
257862306a36Sopenharmony_ci		}
257962306a36Sopenharmony_ci	}
258062306a36Sopenharmony_ci	if (evtq->desc)
258162306a36Sopenharmony_ci		free_rspq_fl(adapter, evtq, NULL);
258262306a36Sopenharmony_ci	if (intrq->desc)
258362306a36Sopenharmony_ci		free_rspq_fl(adapter, intrq, NULL);
258462306a36Sopenharmony_ci}
258562306a36Sopenharmony_ci
258662306a36Sopenharmony_ci/**
258762306a36Sopenharmony_ci *	t4vf_sge_start - enable SGE operation
258862306a36Sopenharmony_ci *	@adapter: the adapter
258962306a36Sopenharmony_ci *
259062306a36Sopenharmony_ci *	Start tasklets and timers associated with the DMA engine.
259162306a36Sopenharmony_ci */
259262306a36Sopenharmony_civoid t4vf_sge_start(struct adapter *adapter)
259362306a36Sopenharmony_ci{
259462306a36Sopenharmony_ci	adapter->sge.ethtxq_rover = 0;
259562306a36Sopenharmony_ci	mod_timer(&adapter->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
259662306a36Sopenharmony_ci	mod_timer(&adapter->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
259762306a36Sopenharmony_ci}
259862306a36Sopenharmony_ci
259962306a36Sopenharmony_ci/**
260062306a36Sopenharmony_ci *	t4vf_sge_stop - disable SGE operation
260162306a36Sopenharmony_ci *	@adapter: the adapter
260262306a36Sopenharmony_ci *
260362306a36Sopenharmony_ci *	Stop tasklets and timers associated with the DMA engine.  Note that
260462306a36Sopenharmony_ci *	this is effective only if measures have been taken to disable any HW
260562306a36Sopenharmony_ci *	events that may restart them.
260662306a36Sopenharmony_ci */
260762306a36Sopenharmony_civoid t4vf_sge_stop(struct adapter *adapter)
260862306a36Sopenharmony_ci{
260962306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
261062306a36Sopenharmony_ci
261162306a36Sopenharmony_ci	if (s->rx_timer.function)
261262306a36Sopenharmony_ci		del_timer_sync(&s->rx_timer);
261362306a36Sopenharmony_ci	if (s->tx_timer.function)
261462306a36Sopenharmony_ci		del_timer_sync(&s->tx_timer);
261562306a36Sopenharmony_ci}
261662306a36Sopenharmony_ci
261762306a36Sopenharmony_ci/**
261862306a36Sopenharmony_ci *	t4vf_sge_init - initialize SGE
261962306a36Sopenharmony_ci *	@adapter: the adapter
262062306a36Sopenharmony_ci *
262162306a36Sopenharmony_ci *	Performs SGE initialization needed every time after a chip reset.
262262306a36Sopenharmony_ci *	We do not initialize any of the queue sets here, instead the driver
262362306a36Sopenharmony_ci *	top-level must request those individually.  We also do not enable DMA
262462306a36Sopenharmony_ci *	here, that should be done after the queues have been set up.
262562306a36Sopenharmony_ci */
262662306a36Sopenharmony_ciint t4vf_sge_init(struct adapter *adapter)
262762306a36Sopenharmony_ci{
262862306a36Sopenharmony_ci	struct sge_params *sge_params = &adapter->params.sge;
262962306a36Sopenharmony_ci	u32 fl_small_pg = sge_params->sge_fl_buffer_size[0];
263062306a36Sopenharmony_ci	u32 fl_large_pg = sge_params->sge_fl_buffer_size[1];
263162306a36Sopenharmony_ci	struct sge *s = &adapter->sge;
263262306a36Sopenharmony_ci
263362306a36Sopenharmony_ci	/*
263462306a36Sopenharmony_ci	 * Start by vetting the basic SGE parameters which have been set up by
263562306a36Sopenharmony_ci	 * the Physical Function Driver.  Ideally we should be able to deal
263662306a36Sopenharmony_ci	 * with _any_ configuration.  Practice is different ...
263762306a36Sopenharmony_ci	 */
263862306a36Sopenharmony_ci
263962306a36Sopenharmony_ci	/* We only bother using the Large Page logic if the Large Page Buffer
264062306a36Sopenharmony_ci	 * is larger than our Page Size Buffer.
264162306a36Sopenharmony_ci	 */
264262306a36Sopenharmony_ci	if (fl_large_pg <= fl_small_pg)
264362306a36Sopenharmony_ci		fl_large_pg = 0;
264462306a36Sopenharmony_ci
264562306a36Sopenharmony_ci	/* The Page Size Buffer must be exactly equal to our Page Size and the
264662306a36Sopenharmony_ci	 * Large Page Size Buffer should be 0 (per above) or a power of 2.
264762306a36Sopenharmony_ci	 */
264862306a36Sopenharmony_ci	if (fl_small_pg != PAGE_SIZE ||
264962306a36Sopenharmony_ci	    (fl_large_pg & (fl_large_pg - 1)) != 0) {
265062306a36Sopenharmony_ci		dev_err(adapter->pdev_dev, "bad SGE FL buffer sizes [%d, %d]\n",
265162306a36Sopenharmony_ci			fl_small_pg, fl_large_pg);
265262306a36Sopenharmony_ci		return -EINVAL;
265362306a36Sopenharmony_ci	}
265462306a36Sopenharmony_ci	if ((sge_params->sge_control & RXPKTCPLMODE_F) !=
265562306a36Sopenharmony_ci	    RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
265662306a36Sopenharmony_ci		dev_err(adapter->pdev_dev, "bad SGE CPL MODE\n");
265762306a36Sopenharmony_ci		return -EINVAL;
265862306a36Sopenharmony_ci	}
265962306a36Sopenharmony_ci
266062306a36Sopenharmony_ci	/*
266162306a36Sopenharmony_ci	 * Now translate the adapter parameters into our internal forms.
266262306a36Sopenharmony_ci	 */
266362306a36Sopenharmony_ci	if (fl_large_pg)
266462306a36Sopenharmony_ci		s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
266562306a36Sopenharmony_ci	s->stat_len = ((sge_params->sge_control & EGRSTATUSPAGESIZE_F)
266662306a36Sopenharmony_ci			? 128 : 64);
266762306a36Sopenharmony_ci	s->pktshift = PKTSHIFT_G(sge_params->sge_control);
266862306a36Sopenharmony_ci	s->fl_align = t4vf_fl_pkt_align(adapter);
266962306a36Sopenharmony_ci
267062306a36Sopenharmony_ci	/* A FL with <= fl_starve_thres buffers is starving and a periodic
267162306a36Sopenharmony_ci	 * timer will attempt to refill it.  This needs to be larger than the
267262306a36Sopenharmony_ci	 * SGE's Egress Congestion Threshold.  If it isn't, then we can get
267362306a36Sopenharmony_ci	 * stuck waiting for new packets while the SGE is waiting for us to
267462306a36Sopenharmony_ci	 * give it more Free List entries.  (Note that the SGE's Egress
267562306a36Sopenharmony_ci	 * Congestion Threshold is in units of 2 Free List pointers.)
267662306a36Sopenharmony_ci	 */
267762306a36Sopenharmony_ci	switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
267862306a36Sopenharmony_ci	case CHELSIO_T4:
267962306a36Sopenharmony_ci		s->fl_starve_thres =
268062306a36Sopenharmony_ci		   EGRTHRESHOLD_G(sge_params->sge_congestion_control);
268162306a36Sopenharmony_ci		break;
268262306a36Sopenharmony_ci	case CHELSIO_T5:
268362306a36Sopenharmony_ci		s->fl_starve_thres =
268462306a36Sopenharmony_ci		   EGRTHRESHOLDPACKING_G(sge_params->sge_congestion_control);
268562306a36Sopenharmony_ci		break;
268662306a36Sopenharmony_ci	case CHELSIO_T6:
268762306a36Sopenharmony_ci	default:
268862306a36Sopenharmony_ci		s->fl_starve_thres =
268962306a36Sopenharmony_ci		   T6_EGRTHRESHOLDPACKING_G(sge_params->sge_congestion_control);
269062306a36Sopenharmony_ci		break;
269162306a36Sopenharmony_ci	}
269262306a36Sopenharmony_ci	s->fl_starve_thres = s->fl_starve_thres * 2 + 1;
269362306a36Sopenharmony_ci
269462306a36Sopenharmony_ci	/*
269562306a36Sopenharmony_ci	 * Set up tasklet timers.
269662306a36Sopenharmony_ci	 */
269762306a36Sopenharmony_ci	timer_setup(&s->rx_timer, sge_rx_timer_cb, 0);
269862306a36Sopenharmony_ci	timer_setup(&s->tx_timer, sge_tx_timer_cb, 0);
269962306a36Sopenharmony_ci
270062306a36Sopenharmony_ci	/*
270162306a36Sopenharmony_ci	 * Initialize Forwarded Interrupt Queue lock.
270262306a36Sopenharmony_ci	 */
270362306a36Sopenharmony_ci	spin_lock_init(&s->intrq_lock);
270462306a36Sopenharmony_ci
270562306a36Sopenharmony_ci	return 0;
270662306a36Sopenharmony_ci}
2707