xref: /linux/drivers/net/ethernet/chelsio/cxgb/sge.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1*a6013785SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f7917c00SJeff Kirsher /*****************************************************************************
3f7917c00SJeff Kirsher  *                                                                           *
4f7917c00SJeff Kirsher  * File: sge.c                                                               *
5f7917c00SJeff Kirsher  * $Revision: 1.26 $                                                         *
6f7917c00SJeff Kirsher  * $Date: 2005/06/21 18:29:48 $                                              *
7f7917c00SJeff Kirsher  * Description:                                                              *
8f7917c00SJeff Kirsher  *  DMA engine.                                                              *
9f7917c00SJeff Kirsher  *  part of the Chelsio 10Gb Ethernet Driver.                                *
10f7917c00SJeff Kirsher  *                                                                           *
11f7917c00SJeff Kirsher  *                                                                           *
12f7917c00SJeff Kirsher  * http://www.chelsio.com                                                    *
13f7917c00SJeff Kirsher  *                                                                           *
14f7917c00SJeff Kirsher  * Copyright (c) 2003 - 2005 Chelsio Communications, Inc.                    *
15f7917c00SJeff Kirsher  * All rights reserved.                                                      *
16f7917c00SJeff Kirsher  *                                                                           *
17f7917c00SJeff Kirsher  * Maintainers: maintainers@chelsio.com                                      *
18f7917c00SJeff Kirsher  *                                                                           *
19f7917c00SJeff Kirsher  * Authors: Dimitrios Michailidis   <dm@chelsio.com>                         *
20f7917c00SJeff Kirsher  *          Tina Yang               <tainay@chelsio.com>                     *
21f7917c00SJeff Kirsher  *          Felix Marti             <felix@chelsio.com>                      *
22f7917c00SJeff Kirsher  *          Scott Bardone           <sbardone@chelsio.com>                   *
23f7917c00SJeff Kirsher  *          Kurt Ottaway            <kottaway@chelsio.com>                   *
24f7917c00SJeff Kirsher  *          Frank DiMambro          <frank@chelsio.com>                      *
25f7917c00SJeff Kirsher  *                                                                           *
26f7917c00SJeff Kirsher  * History:                                                                  *
27f7917c00SJeff Kirsher  *                                                                           *
28f7917c00SJeff Kirsher  ****************************************************************************/
29f7917c00SJeff Kirsher 
30f7917c00SJeff Kirsher #include "common.h"
31f7917c00SJeff Kirsher 
32f7917c00SJeff Kirsher #include <linux/types.h>
33f7917c00SJeff Kirsher #include <linux/errno.h>
34f7917c00SJeff Kirsher #include <linux/pci.h>
35f7917c00SJeff Kirsher #include <linux/ktime.h>
36f7917c00SJeff Kirsher #include <linux/netdevice.h>
37f7917c00SJeff Kirsher #include <linux/etherdevice.h>
38f7917c00SJeff Kirsher #include <linux/if_vlan.h>
39f7917c00SJeff Kirsher #include <linux/skbuff.h>
40f7917c00SJeff Kirsher #include <linux/mm.h>
41f7917c00SJeff Kirsher #include <linux/tcp.h>
42f7917c00SJeff Kirsher #include <linux/ip.h>
43f7917c00SJeff Kirsher #include <linux/in.h>
44f7917c00SJeff Kirsher #include <linux/if_arp.h>
45f7917c00SJeff Kirsher #include <linux/slab.h>
46f7917c00SJeff Kirsher #include <linux/prefetch.h>
47f7917c00SJeff Kirsher 
48f7917c00SJeff Kirsher #include "cpl5_cmd.h"
49f7917c00SJeff Kirsher #include "sge.h"
50f7917c00SJeff Kirsher #include "regs.h"
51f7917c00SJeff Kirsher #include "espi.h"
52f7917c00SJeff Kirsher 
53f7917c00SJeff Kirsher /* This belongs in if_ether.h */
54f7917c00SJeff Kirsher #define ETH_P_CPL5 0xf
55f7917c00SJeff Kirsher 
56f7917c00SJeff Kirsher #define SGE_CMDQ_N		2
57f7917c00SJeff Kirsher #define SGE_FREELQ_N		2
58f7917c00SJeff Kirsher #define SGE_CMDQ0_E_N		1024
59f7917c00SJeff Kirsher #define SGE_CMDQ1_E_N		128
60f7917c00SJeff Kirsher #define SGE_FREEL_SIZE		4096
61f7917c00SJeff Kirsher #define SGE_JUMBO_FREEL_SIZE	512
62f7917c00SJeff Kirsher #define SGE_FREEL_REFILL_THRESH	16
63f7917c00SJeff Kirsher #define SGE_RESPQ_E_N		1024
64f7917c00SJeff Kirsher #define SGE_INTRTIMER_NRES	1000
65f7917c00SJeff Kirsher #define SGE_RX_SM_BUF_SIZE	1536
66f7917c00SJeff Kirsher #define SGE_TX_DESC_MAX_PLEN	16384
67f7917c00SJeff Kirsher 
68f7917c00SJeff Kirsher #define SGE_RESPQ_REPLENISH_THRES (SGE_RESPQ_E_N / 4)
69f7917c00SJeff Kirsher 
70f7917c00SJeff Kirsher /*
71f7917c00SJeff Kirsher  * Period of the TX buffer reclaim timer.  This timer does not need to run
72f7917c00SJeff Kirsher  * frequently as TX buffers are usually reclaimed by new TX packets.
73f7917c00SJeff Kirsher  */
74f7917c00SJeff Kirsher #define TX_RECLAIM_PERIOD (HZ / 4)
75f7917c00SJeff Kirsher 
76f7917c00SJeff Kirsher #define M_CMD_LEN       0x7fffffff
77f7917c00SJeff Kirsher #define V_CMD_LEN(v)    (v)
78f7917c00SJeff Kirsher #define G_CMD_LEN(v)    ((v) & M_CMD_LEN)
79f7917c00SJeff Kirsher #define V_CMD_GEN1(v)   ((v) << 31)
80f7917c00SJeff Kirsher #define V_CMD_GEN2(v)   (v)
81f7917c00SJeff Kirsher #define F_CMD_DATAVALID (1 << 1)
82f7917c00SJeff Kirsher #define F_CMD_SOP       (1 << 2)
83f7917c00SJeff Kirsher #define V_CMD_EOP(v)    ((v) << 3)
84f7917c00SJeff Kirsher 
85f7917c00SJeff Kirsher /*
86f7917c00SJeff Kirsher  * Command queue, receive buffer list, and response queue descriptors.
87f7917c00SJeff Kirsher  */
88f7917c00SJeff Kirsher #if defined(__BIG_ENDIAN_BITFIELD)
89f7917c00SJeff Kirsher struct cmdQ_e {
90f7917c00SJeff Kirsher 	u32 addr_lo;
91f7917c00SJeff Kirsher 	u32 len_gen;
92f7917c00SJeff Kirsher 	u32 flags;
93f7917c00SJeff Kirsher 	u32 addr_hi;
94f7917c00SJeff Kirsher };
95f7917c00SJeff Kirsher 
96f7917c00SJeff Kirsher struct freelQ_e {
97f7917c00SJeff Kirsher 	u32 addr_lo;
98f7917c00SJeff Kirsher 	u32 len_gen;
99f7917c00SJeff Kirsher 	u32 gen2;
100f7917c00SJeff Kirsher 	u32 addr_hi;
101f7917c00SJeff Kirsher };
102f7917c00SJeff Kirsher 
103f7917c00SJeff Kirsher struct respQ_e {
104f7917c00SJeff Kirsher 	u32 Qsleeping		: 4;
105f7917c00SJeff Kirsher 	u32 Cmdq1CreditReturn	: 5;
106f7917c00SJeff Kirsher 	u32 Cmdq1DmaComplete	: 5;
107f7917c00SJeff Kirsher 	u32 Cmdq0CreditReturn	: 5;
108f7917c00SJeff Kirsher 	u32 Cmdq0DmaComplete	: 5;
109f7917c00SJeff Kirsher 	u32 FreelistQid		: 2;
110f7917c00SJeff Kirsher 	u32 CreditValid		: 1;
111f7917c00SJeff Kirsher 	u32 DataValid		: 1;
112f7917c00SJeff Kirsher 	u32 Offload		: 1;
113f7917c00SJeff Kirsher 	u32 Eop			: 1;
114f7917c00SJeff Kirsher 	u32 Sop			: 1;
115f7917c00SJeff Kirsher 	u32 GenerationBit	: 1;
116f7917c00SJeff Kirsher 	u32 BufferLength;
117f7917c00SJeff Kirsher };
118f7917c00SJeff Kirsher #elif defined(__LITTLE_ENDIAN_BITFIELD)
119f7917c00SJeff Kirsher struct cmdQ_e {
120f7917c00SJeff Kirsher 	u32 len_gen;
121f7917c00SJeff Kirsher 	u32 addr_lo;
122f7917c00SJeff Kirsher 	u32 addr_hi;
123f7917c00SJeff Kirsher 	u32 flags;
124f7917c00SJeff Kirsher };
125f7917c00SJeff Kirsher 
126f7917c00SJeff Kirsher struct freelQ_e {
127f7917c00SJeff Kirsher 	u32 len_gen;
128f7917c00SJeff Kirsher 	u32 addr_lo;
129f7917c00SJeff Kirsher 	u32 addr_hi;
130f7917c00SJeff Kirsher 	u32 gen2;
131f7917c00SJeff Kirsher };
132f7917c00SJeff Kirsher 
133f7917c00SJeff Kirsher struct respQ_e {
134f7917c00SJeff Kirsher 	u32 BufferLength;
135f7917c00SJeff Kirsher 	u32 GenerationBit	: 1;
136f7917c00SJeff Kirsher 	u32 Sop			: 1;
137f7917c00SJeff Kirsher 	u32 Eop			: 1;
138f7917c00SJeff Kirsher 	u32 Offload		: 1;
139f7917c00SJeff Kirsher 	u32 DataValid		: 1;
140f7917c00SJeff Kirsher 	u32 CreditValid		: 1;
141f7917c00SJeff Kirsher 	u32 FreelistQid		: 2;
142f7917c00SJeff Kirsher 	u32 Cmdq0DmaComplete	: 5;
143f7917c00SJeff Kirsher 	u32 Cmdq0CreditReturn	: 5;
144f7917c00SJeff Kirsher 	u32 Cmdq1DmaComplete	: 5;
145f7917c00SJeff Kirsher 	u32 Cmdq1CreditReturn	: 5;
146f7917c00SJeff Kirsher 	u32 Qsleeping		: 4;
147f7917c00SJeff Kirsher } ;
148f7917c00SJeff Kirsher #endif
149f7917c00SJeff Kirsher 
150f7917c00SJeff Kirsher /*
151f7917c00SJeff Kirsher  * SW Context Command and Freelist Queue Descriptors
152f7917c00SJeff Kirsher  */
153f7917c00SJeff Kirsher struct cmdQ_ce {
154f7917c00SJeff Kirsher 	struct sk_buff *skb;
155f7917c00SJeff Kirsher 	DEFINE_DMA_UNMAP_ADDR(dma_addr);
156f7917c00SJeff Kirsher 	DEFINE_DMA_UNMAP_LEN(dma_len);
157f7917c00SJeff Kirsher };
158f7917c00SJeff Kirsher 
159f7917c00SJeff Kirsher struct freelQ_ce {
160f7917c00SJeff Kirsher 	struct sk_buff *skb;
161f7917c00SJeff Kirsher 	DEFINE_DMA_UNMAP_ADDR(dma_addr);
162f7917c00SJeff Kirsher 	DEFINE_DMA_UNMAP_LEN(dma_len);
163f7917c00SJeff Kirsher };
164f7917c00SJeff Kirsher 
165f7917c00SJeff Kirsher /*
166f7917c00SJeff Kirsher  * SW command, freelist and response rings
167f7917c00SJeff Kirsher  */
168f7917c00SJeff Kirsher struct cmdQ {
169f7917c00SJeff Kirsher 	unsigned long   status;         /* HW DMA fetch status */
170f7917c00SJeff Kirsher 	unsigned int    in_use;         /* # of in-use command descriptors */
171f7917c00SJeff Kirsher 	unsigned int	size;	        /* # of descriptors */
172f7917c00SJeff Kirsher 	unsigned int    processed;      /* total # of descs HW has processed */
173f7917c00SJeff Kirsher 	unsigned int    cleaned;        /* total # of descs SW has reclaimed */
174f7917c00SJeff Kirsher 	unsigned int    stop_thres;     /* SW TX queue suspend threshold */
175f7917c00SJeff Kirsher 	u16		pidx;           /* producer index (SW) */
176f7917c00SJeff Kirsher 	u16		cidx;           /* consumer index (HW) */
177f7917c00SJeff Kirsher 	u8		genbit;         /* current generation (=valid) bit */
178f7917c00SJeff Kirsher 	u8              sop;            /* is next entry start of packet? */
179f7917c00SJeff Kirsher 	struct cmdQ_e  *entries;        /* HW command descriptor Q */
180f7917c00SJeff Kirsher 	struct cmdQ_ce *centries;       /* SW command context descriptor Q */
181f7917c00SJeff Kirsher 	dma_addr_t	dma_addr;       /* DMA addr HW command descriptor Q */
182f7917c00SJeff Kirsher 	spinlock_t	lock;           /* Lock to protect cmdQ enqueuing */
183f7917c00SJeff Kirsher };
184f7917c00SJeff Kirsher 
185f7917c00SJeff Kirsher struct freelQ {
186f7917c00SJeff Kirsher 	unsigned int	credits;        /* # of available RX buffers */
187f7917c00SJeff Kirsher 	unsigned int	size;	        /* free list capacity */
188f7917c00SJeff Kirsher 	u16		pidx;           /* producer index (SW) */
189f7917c00SJeff Kirsher 	u16		cidx;           /* consumer index (HW) */
190f7917c00SJeff Kirsher 	u16		rx_buffer_size; /* Buffer size on this free list */
191f7917c00SJeff Kirsher 	u16             dma_offset;     /* DMA offset to align IP headers */
192f7917c00SJeff Kirsher 	u16             recycleq_idx;   /* skb recycle q to use */
193f7917c00SJeff Kirsher 	u8		genbit;	        /* current generation (=valid) bit */
194f7917c00SJeff Kirsher 	struct freelQ_e	*entries;       /* HW freelist descriptor Q */
195f7917c00SJeff Kirsher 	struct freelQ_ce *centries;     /* SW freelist context descriptor Q */
196f7917c00SJeff Kirsher 	dma_addr_t	dma_addr;       /* DMA addr HW freelist descriptor Q */
197f7917c00SJeff Kirsher };
198f7917c00SJeff Kirsher 
199f7917c00SJeff Kirsher struct respQ {
200f7917c00SJeff Kirsher 	unsigned int	credits;        /* credits to be returned to SGE */
201f7917c00SJeff Kirsher 	unsigned int	size;	        /* # of response Q descriptors */
202f7917c00SJeff Kirsher 	u16		cidx;	        /* consumer index (SW) */
203f7917c00SJeff Kirsher 	u8		genbit;	        /* current generation(=valid) bit */
204f7917c00SJeff Kirsher 	struct respQ_e *entries;        /* HW response descriptor Q */
205f7917c00SJeff Kirsher 	dma_addr_t	dma_addr;       /* DMA addr HW response descriptor Q */
206f7917c00SJeff Kirsher };
207f7917c00SJeff Kirsher 
208f7917c00SJeff Kirsher /* Bit flags for cmdQ.status */
209f7917c00SJeff Kirsher enum {
210f7917c00SJeff Kirsher 	CMDQ_STAT_RUNNING = 1,          /* fetch engine is running */
211f7917c00SJeff Kirsher 	CMDQ_STAT_LAST_PKT_DB = 2       /* last packet rung the doorbell */
212f7917c00SJeff Kirsher };
213f7917c00SJeff Kirsher 
214f7917c00SJeff Kirsher /* T204 TX SW scheduler */
215f7917c00SJeff Kirsher 
216f7917c00SJeff Kirsher /* Per T204 TX port */
217f7917c00SJeff Kirsher struct sched_port {
218f7917c00SJeff Kirsher 	unsigned int	avail;		/* available bits - quota */
219f7917c00SJeff Kirsher 	unsigned int	drain_bits_per_1024ns; /* drain rate */
220f7917c00SJeff Kirsher 	unsigned int	speed;		/* drain rate, mbps */
221f7917c00SJeff Kirsher 	unsigned int	mtu;		/* mtu size */
222f7917c00SJeff Kirsher 	struct sk_buff_head skbq;	/* pending skbs */
223f7917c00SJeff Kirsher };
224f7917c00SJeff Kirsher 
225f7917c00SJeff Kirsher /* Per T204 device */
226f7917c00SJeff Kirsher struct sched {
227f7917c00SJeff Kirsher 	ktime_t         last_updated;   /* last time quotas were computed */
228f7917c00SJeff Kirsher 	unsigned int	max_avail;	/* max bits to be sent to any port */
229f7917c00SJeff Kirsher 	unsigned int	port;		/* port index (round robin ports) */
230f7917c00SJeff Kirsher 	unsigned int	num;		/* num skbs in per port queues */
231f7917c00SJeff Kirsher 	struct sched_port p[MAX_NPORTS];
232f7917c00SJeff Kirsher 	struct tasklet_struct sched_tsk;/* tasklet used to run scheduler */
2336660de07SAllen Pais 	struct sge *sge;
234f7917c00SJeff Kirsher };
2356660de07SAllen Pais 
2366660de07SAllen Pais static void restart_sched(struct tasklet_struct *t);
237f7917c00SJeff Kirsher 
238f7917c00SJeff Kirsher 
239f7917c00SJeff Kirsher /*
240f7917c00SJeff Kirsher  * Main SGE data structure
241f7917c00SJeff Kirsher  *
242f7917c00SJeff Kirsher  * Interrupts are handled by a single CPU and it is likely that on a MP system
243f7917c00SJeff Kirsher  * the application is migrated to another CPU. In that scenario, we try to
244f7917c00SJeff Kirsher  * separate the RX(in irq context) and TX state in order to decrease memory
245f7917c00SJeff Kirsher  * contention.
246f7917c00SJeff Kirsher  */
247f7917c00SJeff Kirsher struct sge {
248f7917c00SJeff Kirsher 	struct adapter *adapter;	/* adapter backpointer */
249f7917c00SJeff Kirsher 	struct net_device *netdev;      /* netdevice backpointer */
250f7917c00SJeff Kirsher 	struct freelQ	freelQ[SGE_FREELQ_N]; /* buffer free lists */
251f7917c00SJeff Kirsher 	struct respQ	respQ;		/* response Q */
252f7917c00SJeff Kirsher 	unsigned long   stopped_tx_queues; /* bitmap of suspended Tx queues */
253f7917c00SJeff Kirsher 	unsigned int	rx_pkt_pad;     /* RX padding for L2 packets */
254f7917c00SJeff Kirsher 	unsigned int	jumbo_fl;       /* jumbo freelist Q index */
255f7917c00SJeff Kirsher 	unsigned int	intrtimer_nres;	/* no-resource interrupt timer */
256f7917c00SJeff Kirsher 	unsigned int    fixed_intrtimer;/* non-adaptive interrupt timer */
257f7917c00SJeff Kirsher 	struct timer_list tx_reclaim_timer; /* reclaims TX buffers */
258f7917c00SJeff Kirsher 	struct timer_list espibug_timer;
259f7917c00SJeff Kirsher 	unsigned long	espibug_timeout;
260f7917c00SJeff Kirsher 	struct sk_buff	*espibug_skb[MAX_NPORTS];
261f7917c00SJeff Kirsher 	u32		sge_control;	/* shadow value of sge control reg */
262f7917c00SJeff Kirsher 	struct sge_intr_counts stats;
263f7917c00SJeff Kirsher 	struct sge_port_stats __percpu *port_stats[MAX_NPORTS];
264f7917c00SJeff Kirsher 	struct sched	*tx_sched;
265f7917c00SJeff Kirsher 	struct cmdQ cmdQ[SGE_CMDQ_N] ____cacheline_aligned_in_smp;
266f7917c00SJeff Kirsher };
267f7917c00SJeff Kirsher 
268f7917c00SJeff Kirsher static const u8 ch_mac_addr[ETH_ALEN] = {
269f7917c00SJeff Kirsher 	0x0, 0x7, 0x43, 0x0, 0x0, 0x0
270f7917c00SJeff Kirsher };
271f7917c00SJeff Kirsher 
272f7917c00SJeff Kirsher /*
273f7917c00SJeff Kirsher  * stop tasklet and free all pending skb's
274f7917c00SJeff Kirsher  */
tx_sched_stop(struct sge * sge)275f7917c00SJeff Kirsher static void tx_sched_stop(struct sge *sge)
276f7917c00SJeff Kirsher {
277f7917c00SJeff Kirsher 	struct sched *s = sge->tx_sched;
278f7917c00SJeff Kirsher 	int i;
279f7917c00SJeff Kirsher 
280f7917c00SJeff Kirsher 	tasklet_kill(&s->sched_tsk);
281f7917c00SJeff Kirsher 
282f7917c00SJeff Kirsher 	for (i = 0; i < MAX_NPORTS; i++)
283f7917c00SJeff Kirsher 		__skb_queue_purge(&s->p[s->port].skbq);
284f7917c00SJeff Kirsher }
285f7917c00SJeff Kirsher 
286f7917c00SJeff Kirsher /*
287f7917c00SJeff Kirsher  * t1_sched_update_parms() is called when the MTU or link speed changes. It
288f7917c00SJeff Kirsher  * re-computes scheduler parameters to scope with the change.
289f7917c00SJeff Kirsher  */
t1_sched_update_parms(struct sge * sge,unsigned int port,unsigned int mtu,unsigned int speed)290f7917c00SJeff Kirsher unsigned int t1_sched_update_parms(struct sge *sge, unsigned int port,
291f7917c00SJeff Kirsher 				   unsigned int mtu, unsigned int speed)
292f7917c00SJeff Kirsher {
293f7917c00SJeff Kirsher 	struct sched *s = sge->tx_sched;
294f7917c00SJeff Kirsher 	struct sched_port *p = &s->p[port];
295f7917c00SJeff Kirsher 	unsigned int max_avail_segs;
296f7917c00SJeff Kirsher 
2975e84e189SJulia Lawall 	pr_debug("%s mtu=%d speed=%d\n", __func__, mtu, speed);
298f7917c00SJeff Kirsher 	if (speed)
299f7917c00SJeff Kirsher 		p->speed = speed;
300f7917c00SJeff Kirsher 	if (mtu)
301f7917c00SJeff Kirsher 		p->mtu = mtu;
302f7917c00SJeff Kirsher 
303f7917c00SJeff Kirsher 	if (speed || mtu) {
304f7917c00SJeff Kirsher 		unsigned long long drain = 1024ULL * p->speed * (p->mtu - 40);
305f7917c00SJeff Kirsher 		do_div(drain, (p->mtu + 50) * 1000);
306f7917c00SJeff Kirsher 		p->drain_bits_per_1024ns = (unsigned int) drain;
307f7917c00SJeff Kirsher 
308f7917c00SJeff Kirsher 		if (p->speed < 1000)
309f7917c00SJeff Kirsher 			p->drain_bits_per_1024ns =
310f7917c00SJeff Kirsher 				90 * p->drain_bits_per_1024ns / 100;
311f7917c00SJeff Kirsher 	}
312f7917c00SJeff Kirsher 
313f7917c00SJeff Kirsher 	if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204) {
314f7917c00SJeff Kirsher 		p->drain_bits_per_1024ns -= 16;
315f7917c00SJeff Kirsher 		s->max_avail = max(4096U, p->mtu + 16 + 14 + 4);
316f7917c00SJeff Kirsher 		max_avail_segs = max(1U, 4096 / (p->mtu - 40));
317f7917c00SJeff Kirsher 	} else {
318f7917c00SJeff Kirsher 		s->max_avail = 16384;
319f7917c00SJeff Kirsher 		max_avail_segs = max(1U, 9000 / (p->mtu - 40));
320f7917c00SJeff Kirsher 	}
321f7917c00SJeff Kirsher 
322f7917c00SJeff Kirsher 	pr_debug("t1_sched_update_parms: mtu %u speed %u max_avail %u "
323f7917c00SJeff Kirsher 		 "max_avail_segs %u drain_bits_per_1024ns %u\n", p->mtu,
324f7917c00SJeff Kirsher 		 p->speed, s->max_avail, max_avail_segs,
325f7917c00SJeff Kirsher 		 p->drain_bits_per_1024ns);
326f7917c00SJeff Kirsher 
327f7917c00SJeff Kirsher 	return max_avail_segs * (p->mtu - 40);
328f7917c00SJeff Kirsher }
329f7917c00SJeff Kirsher 
330f7917c00SJeff Kirsher #if 0
331f7917c00SJeff Kirsher 
332f7917c00SJeff Kirsher /*
333f7917c00SJeff Kirsher  * t1_sched_max_avail_bytes() tells the scheduler the maximum amount of
334f7917c00SJeff Kirsher  * data that can be pushed per port.
335f7917c00SJeff Kirsher  */
336f7917c00SJeff Kirsher void t1_sched_set_max_avail_bytes(struct sge *sge, unsigned int val)
337f7917c00SJeff Kirsher {
338f7917c00SJeff Kirsher 	struct sched *s = sge->tx_sched;
339f7917c00SJeff Kirsher 	unsigned int i;
340f7917c00SJeff Kirsher 
341f7917c00SJeff Kirsher 	s->max_avail = val;
342f7917c00SJeff Kirsher 	for (i = 0; i < MAX_NPORTS; i++)
343f7917c00SJeff Kirsher 		t1_sched_update_parms(sge, i, 0, 0);
344f7917c00SJeff Kirsher }
345f7917c00SJeff Kirsher 
346f7917c00SJeff Kirsher /*
347f7917c00SJeff Kirsher  * t1_sched_set_drain_bits_per_us() tells the scheduler at which rate a port
348f7917c00SJeff Kirsher  * is draining.
349f7917c00SJeff Kirsher  */
350f7917c00SJeff Kirsher void t1_sched_set_drain_bits_per_us(struct sge *sge, unsigned int port,
351f7917c00SJeff Kirsher 					 unsigned int val)
352f7917c00SJeff Kirsher {
353f7917c00SJeff Kirsher 	struct sched *s = sge->tx_sched;
354f7917c00SJeff Kirsher 	struct sched_port *p = &s->p[port];
355f7917c00SJeff Kirsher 	p->drain_bits_per_1024ns = val * 1024 / 1000;
356f7917c00SJeff Kirsher 	t1_sched_update_parms(sge, port, 0, 0);
357f7917c00SJeff Kirsher }
358f7917c00SJeff Kirsher 
359f7917c00SJeff Kirsher #endif  /*  0  */
360f7917c00SJeff Kirsher 
361f7917c00SJeff Kirsher /*
362f7917c00SJeff Kirsher  * tx_sched_init() allocates resources and does basic initialization.
363f7917c00SJeff Kirsher  */
tx_sched_init(struct sge * sge)364f7917c00SJeff Kirsher static int tx_sched_init(struct sge *sge)
365f7917c00SJeff Kirsher {
366f7917c00SJeff Kirsher 	struct sched *s;
367f7917c00SJeff Kirsher 	int i;
368f7917c00SJeff Kirsher 
369f7917c00SJeff Kirsher 	s = kzalloc(sizeof (struct sched), GFP_KERNEL);
370f7917c00SJeff Kirsher 	if (!s)
371f7917c00SJeff Kirsher 		return -ENOMEM;
372f7917c00SJeff Kirsher 
373f7917c00SJeff Kirsher 	pr_debug("tx_sched_init\n");
3746660de07SAllen Pais 	tasklet_setup(&s->sched_tsk, restart_sched);
3756660de07SAllen Pais 	s->sge = sge;
376f7917c00SJeff Kirsher 	sge->tx_sched = s;
377f7917c00SJeff Kirsher 
378f7917c00SJeff Kirsher 	for (i = 0; i < MAX_NPORTS; i++) {
379f7917c00SJeff Kirsher 		skb_queue_head_init(&s->p[i].skbq);
380f7917c00SJeff Kirsher 		t1_sched_update_parms(sge, i, 1500, 1000);
381f7917c00SJeff Kirsher 	}
382f7917c00SJeff Kirsher 
383f7917c00SJeff Kirsher 	return 0;
384f7917c00SJeff Kirsher }
385f7917c00SJeff Kirsher 
386f7917c00SJeff Kirsher /*
387f7917c00SJeff Kirsher  * sched_update_avail() computes the delta since the last time it was called
388f7917c00SJeff Kirsher  * and updates the per port quota (number of bits that can be sent to the any
389f7917c00SJeff Kirsher  * port).
390f7917c00SJeff Kirsher  */
sched_update_avail(struct sge * sge)391f7917c00SJeff Kirsher static inline int sched_update_avail(struct sge *sge)
392f7917c00SJeff Kirsher {
393f7917c00SJeff Kirsher 	struct sched *s = sge->tx_sched;
394fd3065b2SJan Glauber 	ktime_t now = ktime_get();
395f7917c00SJeff Kirsher 	unsigned int i;
396f7917c00SJeff Kirsher 	long long delta_time_ns;
397f7917c00SJeff Kirsher 
398f7917c00SJeff Kirsher 	delta_time_ns = ktime_to_ns(ktime_sub(now, s->last_updated));
399f7917c00SJeff Kirsher 
400f7917c00SJeff Kirsher 	pr_debug("sched_update_avail delta=%lld\n", delta_time_ns);
401f7917c00SJeff Kirsher 	if (delta_time_ns < 15000)
402f7917c00SJeff Kirsher 		return 0;
403f7917c00SJeff Kirsher 
404f7917c00SJeff Kirsher 	for (i = 0; i < MAX_NPORTS; i++) {
405f7917c00SJeff Kirsher 		struct sched_port *p = &s->p[i];
406f7917c00SJeff Kirsher 		unsigned int delta_avail;
407f7917c00SJeff Kirsher 
408f7917c00SJeff Kirsher 		delta_avail = (p->drain_bits_per_1024ns * delta_time_ns) >> 13;
409f7917c00SJeff Kirsher 		p->avail = min(p->avail + delta_avail, s->max_avail);
410f7917c00SJeff Kirsher 	}
411f7917c00SJeff Kirsher 
412f7917c00SJeff Kirsher 	s->last_updated = now;
413f7917c00SJeff Kirsher 
414f7917c00SJeff Kirsher 	return 1;
415f7917c00SJeff Kirsher }
416f7917c00SJeff Kirsher 
417f7917c00SJeff Kirsher /*
418f7917c00SJeff Kirsher  * sched_skb() is called from two different places. In the tx path, any
419f7917c00SJeff Kirsher  * packet generating load on an output port will call sched_skb()
420f7917c00SJeff Kirsher  * (skb != NULL). In addition, sched_skb() is called from the irq/soft irq
421f7917c00SJeff Kirsher  * context (skb == NULL).
422f7917c00SJeff Kirsher  * The scheduler only returns a skb (which will then be sent) if the
423f7917c00SJeff Kirsher  * length of the skb is <= the current quota of the output port.
424f7917c00SJeff Kirsher  */
sched_skb(struct sge * sge,struct sk_buff * skb,unsigned int credits)425f7917c00SJeff Kirsher static struct sk_buff *sched_skb(struct sge *sge, struct sk_buff *skb,
426f7917c00SJeff Kirsher 				unsigned int credits)
427f7917c00SJeff Kirsher {
428f7917c00SJeff Kirsher 	struct sched *s = sge->tx_sched;
429f7917c00SJeff Kirsher 	struct sk_buff_head *skbq;
430f7917c00SJeff Kirsher 	unsigned int i, len, update = 1;
431f7917c00SJeff Kirsher 
432f7917c00SJeff Kirsher 	pr_debug("sched_skb %p\n", skb);
433f7917c00SJeff Kirsher 	if (!skb) {
434f7917c00SJeff Kirsher 		if (!s->num)
435f7917c00SJeff Kirsher 			return NULL;
436f7917c00SJeff Kirsher 	} else {
437f7917c00SJeff Kirsher 		skbq = &s->p[skb->dev->if_port].skbq;
438f7917c00SJeff Kirsher 		__skb_queue_tail(skbq, skb);
439f7917c00SJeff Kirsher 		s->num++;
440f7917c00SJeff Kirsher 		skb = NULL;
441f7917c00SJeff Kirsher 	}
442f7917c00SJeff Kirsher 
443f7917c00SJeff Kirsher 	if (credits < MAX_SKB_FRAGS + 1)
444f7917c00SJeff Kirsher 		goto out;
445f7917c00SJeff Kirsher 
446f7917c00SJeff Kirsher again:
447f7917c00SJeff Kirsher 	for (i = 0; i < MAX_NPORTS; i++) {
448f7917c00SJeff Kirsher 		s->port = (s->port + 1) & (MAX_NPORTS - 1);
449f7917c00SJeff Kirsher 		skbq = &s->p[s->port].skbq;
450f7917c00SJeff Kirsher 
451f7917c00SJeff Kirsher 		skb = skb_peek(skbq);
452f7917c00SJeff Kirsher 
453f7917c00SJeff Kirsher 		if (!skb)
454f7917c00SJeff Kirsher 			continue;
455f7917c00SJeff Kirsher 
456f7917c00SJeff Kirsher 		len = skb->len;
457f7917c00SJeff Kirsher 		if (len <= s->p[s->port].avail) {
458f7917c00SJeff Kirsher 			s->p[s->port].avail -= len;
459f7917c00SJeff Kirsher 			s->num--;
460f7917c00SJeff Kirsher 			__skb_unlink(skb, skbq);
461f7917c00SJeff Kirsher 			goto out;
462f7917c00SJeff Kirsher 		}
463f7917c00SJeff Kirsher 		skb = NULL;
464f7917c00SJeff Kirsher 	}
465f7917c00SJeff Kirsher 
466f7917c00SJeff Kirsher 	if (update-- && sched_update_avail(sge))
467f7917c00SJeff Kirsher 		goto again;
468f7917c00SJeff Kirsher 
469f7917c00SJeff Kirsher out:
470f7917c00SJeff Kirsher 	/* If there are more pending skbs, we use the hardware to schedule us
471f7917c00SJeff Kirsher 	 * again.
472f7917c00SJeff Kirsher 	 */
473f7917c00SJeff Kirsher 	if (s->num && !skb) {
474f7917c00SJeff Kirsher 		struct cmdQ *q = &sge->cmdQ[0];
475f7917c00SJeff Kirsher 		clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
476f7917c00SJeff Kirsher 		if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
477f7917c00SJeff Kirsher 			set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
478f7917c00SJeff Kirsher 			writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
479f7917c00SJeff Kirsher 		}
480f7917c00SJeff Kirsher 	}
481f7917c00SJeff Kirsher 	pr_debug("sched_skb ret %p\n", skb);
482f7917c00SJeff Kirsher 
483f7917c00SJeff Kirsher 	return skb;
484f7917c00SJeff Kirsher }
485f7917c00SJeff Kirsher 
486f7917c00SJeff Kirsher /*
487f7917c00SJeff Kirsher  * PIO to indicate that memory mapped Q contains valid descriptor(s).
488f7917c00SJeff Kirsher  */
doorbell_pio(struct adapter * adapter,u32 val)489f7917c00SJeff Kirsher static inline void doorbell_pio(struct adapter *adapter, u32 val)
490f7917c00SJeff Kirsher {
491f7917c00SJeff Kirsher 	wmb();
492f7917c00SJeff Kirsher 	writel(val, adapter->regs + A_SG_DOORBELL);
493f7917c00SJeff Kirsher }
494f7917c00SJeff Kirsher 
495f7917c00SJeff Kirsher /*
496f7917c00SJeff Kirsher  * Frees all RX buffers on the freelist Q. The caller must make sure that
497f7917c00SJeff Kirsher  * the SGE is turned off before calling this function.
498f7917c00SJeff Kirsher  */
free_freelQ_buffers(struct pci_dev * pdev,struct freelQ * q)499f7917c00SJeff Kirsher static void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *q)
500f7917c00SJeff Kirsher {
501f7917c00SJeff Kirsher 	unsigned int cidx = q->cidx;
502f7917c00SJeff Kirsher 
503f7917c00SJeff Kirsher 	while (q->credits--) {
504f7917c00SJeff Kirsher 		struct freelQ_ce *ce = &q->centries[cidx];
505f7917c00SJeff Kirsher 
5069ab90179SChristophe JAILLET 		dma_unmap_single(&pdev->dev, dma_unmap_addr(ce, dma_addr),
5079ab90179SChristophe JAILLET 				 dma_unmap_len(ce, dma_len), DMA_FROM_DEVICE);
508f7917c00SJeff Kirsher 		dev_kfree_skb(ce->skb);
509f7917c00SJeff Kirsher 		ce->skb = NULL;
510f7917c00SJeff Kirsher 		if (++cidx == q->size)
511f7917c00SJeff Kirsher 			cidx = 0;
512f7917c00SJeff Kirsher 	}
513f7917c00SJeff Kirsher }
514f7917c00SJeff Kirsher 
515f7917c00SJeff Kirsher /*
516f7917c00SJeff Kirsher  * Free RX free list and response queue resources.
517f7917c00SJeff Kirsher  */
free_rx_resources(struct sge * sge)518f7917c00SJeff Kirsher static void free_rx_resources(struct sge *sge)
519f7917c00SJeff Kirsher {
520f7917c00SJeff Kirsher 	struct pci_dev *pdev = sge->adapter->pdev;
521f7917c00SJeff Kirsher 	unsigned int size, i;
522f7917c00SJeff Kirsher 
523f7917c00SJeff Kirsher 	if (sge->respQ.entries) {
524f7917c00SJeff Kirsher 		size = sizeof(struct respQ_e) * sge->respQ.size;
5259ab90179SChristophe JAILLET 		dma_free_coherent(&pdev->dev, size, sge->respQ.entries,
526f7917c00SJeff Kirsher 				  sge->respQ.dma_addr);
527f7917c00SJeff Kirsher 	}
528f7917c00SJeff Kirsher 
529f7917c00SJeff Kirsher 	for (i = 0; i < SGE_FREELQ_N; i++) {
530f7917c00SJeff Kirsher 		struct freelQ *q = &sge->freelQ[i];
531f7917c00SJeff Kirsher 
532f7917c00SJeff Kirsher 		if (q->centries) {
533f7917c00SJeff Kirsher 			free_freelQ_buffers(pdev, q);
534f7917c00SJeff Kirsher 			kfree(q->centries);
535f7917c00SJeff Kirsher 		}
536f7917c00SJeff Kirsher 		if (q->entries) {
537f7917c00SJeff Kirsher 			size = sizeof(struct freelQ_e) * q->size;
5389ab90179SChristophe JAILLET 			dma_free_coherent(&pdev->dev, size, q->entries,
539f7917c00SJeff Kirsher 					  q->dma_addr);
540f7917c00SJeff Kirsher 		}
541f7917c00SJeff Kirsher 	}
542f7917c00SJeff Kirsher }
543f7917c00SJeff Kirsher 
544f7917c00SJeff Kirsher /*
545f7917c00SJeff Kirsher  * Allocates basic RX resources, consisting of memory mapped freelist Qs and a
546f7917c00SJeff Kirsher  * response queue.
547f7917c00SJeff Kirsher  */
alloc_rx_resources(struct sge * sge,struct sge_params * p)548f7917c00SJeff Kirsher static int alloc_rx_resources(struct sge *sge, struct sge_params *p)
549f7917c00SJeff Kirsher {
550f7917c00SJeff Kirsher 	struct pci_dev *pdev = sge->adapter->pdev;
551f7917c00SJeff Kirsher 	unsigned int size, i;
552f7917c00SJeff Kirsher 
553f7917c00SJeff Kirsher 	for (i = 0; i < SGE_FREELQ_N; i++) {
554f7917c00SJeff Kirsher 		struct freelQ *q = &sge->freelQ[i];
555f7917c00SJeff Kirsher 
556f7917c00SJeff Kirsher 		q->genbit = 1;
557f7917c00SJeff Kirsher 		q->size = p->freelQ_size[i];
558f7917c00SJeff Kirsher 		q->dma_offset = sge->rx_pkt_pad ? 0 : NET_IP_ALIGN;
559f7917c00SJeff Kirsher 		size = sizeof(struct freelQ_e) * q->size;
5609ab90179SChristophe JAILLET 		q->entries = dma_alloc_coherent(&pdev->dev, size,
5619ab90179SChristophe JAILLET 						&q->dma_addr, GFP_KERNEL);
562f7917c00SJeff Kirsher 		if (!q->entries)
563f7917c00SJeff Kirsher 			goto err_no_mem;
564f7917c00SJeff Kirsher 
565f7917c00SJeff Kirsher 		size = sizeof(struct freelQ_ce) * q->size;
566f7917c00SJeff Kirsher 		q->centries = kzalloc(size, GFP_KERNEL);
567f7917c00SJeff Kirsher 		if (!q->centries)
568f7917c00SJeff Kirsher 			goto err_no_mem;
569f7917c00SJeff Kirsher 	}
570f7917c00SJeff Kirsher 
571f7917c00SJeff Kirsher 	/*
572f7917c00SJeff Kirsher 	 * Calculate the buffer sizes for the two free lists.  FL0 accommodates
573f7917c00SJeff Kirsher 	 * regular sized Ethernet frames, FL1 is sized not to exceed 16K,
574f7917c00SJeff Kirsher 	 * including all the sk_buff overhead.
575f7917c00SJeff Kirsher 	 *
576f7917c00SJeff Kirsher 	 * Note: For T2 FL0 and FL1 are reversed.
577f7917c00SJeff Kirsher 	 */
578f7917c00SJeff Kirsher 	sge->freelQ[!sge->jumbo_fl].rx_buffer_size = SGE_RX_SM_BUF_SIZE +
579f7917c00SJeff Kirsher 		sizeof(struct cpl_rx_data) +
580f7917c00SJeff Kirsher 		sge->freelQ[!sge->jumbo_fl].dma_offset;
581f7917c00SJeff Kirsher 
5827831216bSColin Ian King 	size = (16 * 1024) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
583f7917c00SJeff Kirsher 
584f7917c00SJeff Kirsher 	sge->freelQ[sge->jumbo_fl].rx_buffer_size = size;
585f7917c00SJeff Kirsher 
586f7917c00SJeff Kirsher 	/*
587f7917c00SJeff Kirsher 	 * Setup which skb recycle Q should be used when recycling buffers from
588f7917c00SJeff Kirsher 	 * each free list.
589f7917c00SJeff Kirsher 	 */
590f7917c00SJeff Kirsher 	sge->freelQ[!sge->jumbo_fl].recycleq_idx = 0;
591f7917c00SJeff Kirsher 	sge->freelQ[sge->jumbo_fl].recycleq_idx = 1;
592f7917c00SJeff Kirsher 
593f7917c00SJeff Kirsher 	sge->respQ.genbit = 1;
594f7917c00SJeff Kirsher 	sge->respQ.size = SGE_RESPQ_E_N;
595f7917c00SJeff Kirsher 	sge->respQ.credits = 0;
596f7917c00SJeff Kirsher 	size = sizeof(struct respQ_e) * sge->respQ.size;
597f7917c00SJeff Kirsher 	sge->respQ.entries =
5989ab90179SChristophe JAILLET 		dma_alloc_coherent(&pdev->dev, size, &sge->respQ.dma_addr,
5999ab90179SChristophe JAILLET 				   GFP_KERNEL);
600f7917c00SJeff Kirsher 	if (!sge->respQ.entries)
601f7917c00SJeff Kirsher 		goto err_no_mem;
602f7917c00SJeff Kirsher 	return 0;
603f7917c00SJeff Kirsher 
604f7917c00SJeff Kirsher err_no_mem:
605f7917c00SJeff Kirsher 	free_rx_resources(sge);
606f7917c00SJeff Kirsher 	return -ENOMEM;
607f7917c00SJeff Kirsher }
608f7917c00SJeff Kirsher 
609f7917c00SJeff Kirsher /*
610f7917c00SJeff Kirsher  * Reclaims n TX descriptors and frees the buffers associated with them.
611f7917c00SJeff Kirsher  */
free_cmdQ_buffers(struct sge * sge,struct cmdQ * q,unsigned int n)612f7917c00SJeff Kirsher static void free_cmdQ_buffers(struct sge *sge, struct cmdQ *q, unsigned int n)
613f7917c00SJeff Kirsher {
614f7917c00SJeff Kirsher 	struct cmdQ_ce *ce;
615f7917c00SJeff Kirsher 	struct pci_dev *pdev = sge->adapter->pdev;
616f7917c00SJeff Kirsher 	unsigned int cidx = q->cidx;
617f7917c00SJeff Kirsher 
618f7917c00SJeff Kirsher 	q->in_use -= n;
619f7917c00SJeff Kirsher 	ce = &q->centries[cidx];
620f7917c00SJeff Kirsher 	while (n--) {
621f7917c00SJeff Kirsher 		if (likely(dma_unmap_len(ce, dma_len))) {
6229ab90179SChristophe JAILLET 			dma_unmap_single(&pdev->dev,
6239ab90179SChristophe JAILLET 					 dma_unmap_addr(ce, dma_addr),
624f7917c00SJeff Kirsher 					 dma_unmap_len(ce, dma_len),
6259ab90179SChristophe JAILLET 					 DMA_TO_DEVICE);
626f7917c00SJeff Kirsher 			if (q->sop)
627f7917c00SJeff Kirsher 				q->sop = 0;
628f7917c00SJeff Kirsher 		}
629f7917c00SJeff Kirsher 		if (ce->skb) {
630f7917c00SJeff Kirsher 			dev_kfree_skb_any(ce->skb);
631f7917c00SJeff Kirsher 			q->sop = 1;
632f7917c00SJeff Kirsher 		}
633f7917c00SJeff Kirsher 		ce++;
634f7917c00SJeff Kirsher 		if (++cidx == q->size) {
635f7917c00SJeff Kirsher 			cidx = 0;
636f7917c00SJeff Kirsher 			ce = q->centries;
637f7917c00SJeff Kirsher 		}
638f7917c00SJeff Kirsher 	}
639f7917c00SJeff Kirsher 	q->cidx = cidx;
640f7917c00SJeff Kirsher }
641f7917c00SJeff Kirsher 
642f7917c00SJeff Kirsher /*
643f7917c00SJeff Kirsher  * Free TX resources.
644f7917c00SJeff Kirsher  *
645f7917c00SJeff Kirsher  * Assumes that SGE is stopped and all interrupts are disabled.
646f7917c00SJeff Kirsher  */
free_tx_resources(struct sge * sge)647f7917c00SJeff Kirsher static void free_tx_resources(struct sge *sge)
648f7917c00SJeff Kirsher {
649f7917c00SJeff Kirsher 	struct pci_dev *pdev = sge->adapter->pdev;
650f7917c00SJeff Kirsher 	unsigned int size, i;
651f7917c00SJeff Kirsher 
652f7917c00SJeff Kirsher 	for (i = 0; i < SGE_CMDQ_N; i++) {
653f7917c00SJeff Kirsher 		struct cmdQ *q = &sge->cmdQ[i];
654f7917c00SJeff Kirsher 
655f7917c00SJeff Kirsher 		if (q->centries) {
656f7917c00SJeff Kirsher 			if (q->in_use)
657f7917c00SJeff Kirsher 				free_cmdQ_buffers(sge, q, q->in_use);
658f7917c00SJeff Kirsher 			kfree(q->centries);
659f7917c00SJeff Kirsher 		}
660f7917c00SJeff Kirsher 		if (q->entries) {
661f7917c00SJeff Kirsher 			size = sizeof(struct cmdQ_e) * q->size;
6629ab90179SChristophe JAILLET 			dma_free_coherent(&pdev->dev, size, q->entries,
663f7917c00SJeff Kirsher 					  q->dma_addr);
664f7917c00SJeff Kirsher 		}
665f7917c00SJeff Kirsher 	}
666f7917c00SJeff Kirsher }
667f7917c00SJeff Kirsher 
668f7917c00SJeff Kirsher /*
669f7917c00SJeff Kirsher  * Allocates basic TX resources, consisting of memory mapped command Qs.
670f7917c00SJeff Kirsher  */
alloc_tx_resources(struct sge * sge,struct sge_params * p)671f7917c00SJeff Kirsher static int alloc_tx_resources(struct sge *sge, struct sge_params *p)
672f7917c00SJeff Kirsher {
673f7917c00SJeff Kirsher 	struct pci_dev *pdev = sge->adapter->pdev;
674f7917c00SJeff Kirsher 	unsigned int size, i;
675f7917c00SJeff Kirsher 
676f7917c00SJeff Kirsher 	for (i = 0; i < SGE_CMDQ_N; i++) {
677f7917c00SJeff Kirsher 		struct cmdQ *q = &sge->cmdQ[i];
678f7917c00SJeff Kirsher 
679f7917c00SJeff Kirsher 		q->genbit = 1;
680f7917c00SJeff Kirsher 		q->sop = 1;
681f7917c00SJeff Kirsher 		q->size = p->cmdQ_size[i];
682f7917c00SJeff Kirsher 		q->in_use = 0;
683f7917c00SJeff Kirsher 		q->status = 0;
684f7917c00SJeff Kirsher 		q->processed = q->cleaned = 0;
685f7917c00SJeff Kirsher 		q->stop_thres = 0;
686f7917c00SJeff Kirsher 		spin_lock_init(&q->lock);
687f7917c00SJeff Kirsher 		size = sizeof(struct cmdQ_e) * q->size;
6889ab90179SChristophe JAILLET 		q->entries = dma_alloc_coherent(&pdev->dev, size,
6899ab90179SChristophe JAILLET 						&q->dma_addr, GFP_KERNEL);
690f7917c00SJeff Kirsher 		if (!q->entries)
691f7917c00SJeff Kirsher 			goto err_no_mem;
692f7917c00SJeff Kirsher 
693f7917c00SJeff Kirsher 		size = sizeof(struct cmdQ_ce) * q->size;
694f7917c00SJeff Kirsher 		q->centries = kzalloc(size, GFP_KERNEL);
695f7917c00SJeff Kirsher 		if (!q->centries)
696f7917c00SJeff Kirsher 			goto err_no_mem;
697f7917c00SJeff Kirsher 	}
698f7917c00SJeff Kirsher 
699f7917c00SJeff Kirsher 	/*
700f7917c00SJeff Kirsher 	 * CommandQ 0 handles Ethernet and TOE packets, while queue 1 is TOE
701f7917c00SJeff Kirsher 	 * only.  For queue 0 set the stop threshold so we can handle one more
702f7917c00SJeff Kirsher 	 * packet from each port, plus reserve an additional 24 entries for
703f7917c00SJeff Kirsher 	 * Ethernet packets only.  Queue 1 never suspends nor do we reserve
704f7917c00SJeff Kirsher 	 * space for Ethernet packets.
705f7917c00SJeff Kirsher 	 */
706f7917c00SJeff Kirsher 	sge->cmdQ[0].stop_thres = sge->adapter->params.nports *
707f7917c00SJeff Kirsher 		(MAX_SKB_FRAGS + 1);
708f7917c00SJeff Kirsher 	return 0;
709f7917c00SJeff Kirsher 
710f7917c00SJeff Kirsher err_no_mem:
711f7917c00SJeff Kirsher 	free_tx_resources(sge);
712f7917c00SJeff Kirsher 	return -ENOMEM;
713f7917c00SJeff Kirsher }
714f7917c00SJeff Kirsher 
setup_ring_params(struct adapter * adapter,u64 addr,u32 size,int base_reg_lo,int base_reg_hi,int size_reg)715f7917c00SJeff Kirsher static inline void setup_ring_params(struct adapter *adapter, u64 addr,
716f7917c00SJeff Kirsher 				     u32 size, int base_reg_lo,
717f7917c00SJeff Kirsher 				     int base_reg_hi, int size_reg)
718f7917c00SJeff Kirsher {
719f7917c00SJeff Kirsher 	writel((u32)addr, adapter->regs + base_reg_lo);
720f7917c00SJeff Kirsher 	writel(addr >> 32, adapter->regs + base_reg_hi);
721f7917c00SJeff Kirsher 	writel(size, adapter->regs + size_reg);
722f7917c00SJeff Kirsher }
723f7917c00SJeff Kirsher 
724f7917c00SJeff Kirsher /*
725f7917c00SJeff Kirsher  * Enable/disable VLAN acceleration.
726f7917c00SJeff Kirsher  */
t1_vlan_mode(struct adapter * adapter,netdev_features_t features)727c8f44affSMichał Mirosław void t1_vlan_mode(struct adapter *adapter, netdev_features_t features)
728f7917c00SJeff Kirsher {
729f7917c00SJeff Kirsher 	struct sge *sge = adapter->sge;
730f7917c00SJeff Kirsher 
731f646968fSPatrick McHardy 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
732f7917c00SJeff Kirsher 		sge->sge_control |= F_VLAN_XTRACT;
733f7917c00SJeff Kirsher 	else
734f7917c00SJeff Kirsher 		sge->sge_control &= ~F_VLAN_XTRACT;
735f7917c00SJeff Kirsher 	if (adapter->open_device_map) {
736f7917c00SJeff Kirsher 		writel(sge->sge_control, adapter->regs + A_SG_CONTROL);
737f7917c00SJeff Kirsher 		readl(adapter->regs + A_SG_CONTROL);   /* flush */
738f7917c00SJeff Kirsher 	}
739f7917c00SJeff Kirsher }
740f7917c00SJeff Kirsher 
741f7917c00SJeff Kirsher /*
742f7917c00SJeff Kirsher  * Programs the various SGE registers. However, the engine is not yet enabled,
743f7917c00SJeff Kirsher  * but sge->sge_control is setup and ready to go.
744f7917c00SJeff Kirsher  */
configure_sge(struct sge * sge,struct sge_params * p)745f7917c00SJeff Kirsher static void configure_sge(struct sge *sge, struct sge_params *p)
746f7917c00SJeff Kirsher {
747f7917c00SJeff Kirsher 	struct adapter *ap = sge->adapter;
748f7917c00SJeff Kirsher 
749f7917c00SJeff Kirsher 	writel(0, ap->regs + A_SG_CONTROL);
750f7917c00SJeff Kirsher 	setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
751f7917c00SJeff Kirsher 			  A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
752f7917c00SJeff Kirsher 	setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
753f7917c00SJeff Kirsher 			  A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
754f7917c00SJeff Kirsher 	setup_ring_params(ap, sge->freelQ[0].dma_addr,
755f7917c00SJeff Kirsher 			  sge->freelQ[0].size, A_SG_FL0BASELWR,
756f7917c00SJeff Kirsher 			  A_SG_FL0BASEUPR, A_SG_FL0SIZE);
757f7917c00SJeff Kirsher 	setup_ring_params(ap, sge->freelQ[1].dma_addr,
758f7917c00SJeff Kirsher 			  sge->freelQ[1].size, A_SG_FL1BASELWR,
759f7917c00SJeff Kirsher 			  A_SG_FL1BASEUPR, A_SG_FL1SIZE);
760f7917c00SJeff Kirsher 
761f7917c00SJeff Kirsher 	/* The threshold comparison uses <. */
762f7917c00SJeff Kirsher 	writel(SGE_RX_SM_BUF_SIZE + 1, ap->regs + A_SG_FLTHRESHOLD);
763f7917c00SJeff Kirsher 
764f7917c00SJeff Kirsher 	setup_ring_params(ap, sge->respQ.dma_addr, sge->respQ.size,
765f7917c00SJeff Kirsher 			  A_SG_RSPBASELWR, A_SG_RSPBASEUPR, A_SG_RSPSIZE);
766f7917c00SJeff Kirsher 	writel((u32)sge->respQ.size - 1, ap->regs + A_SG_RSPQUEUECREDIT);
767f7917c00SJeff Kirsher 
768f7917c00SJeff Kirsher 	sge->sge_control = F_CMDQ0_ENABLE | F_CMDQ1_ENABLE | F_FL0_ENABLE |
769f7917c00SJeff Kirsher 		F_FL1_ENABLE | F_CPL_ENABLE | F_RESPONSE_QUEUE_ENABLE |
770f7917c00SJeff Kirsher 		V_CMDQ_PRIORITY(2) | F_DISABLE_CMDQ1_GTS | F_ISCSI_COALESCE |
771f7917c00SJeff Kirsher 		V_RX_PKT_OFFSET(sge->rx_pkt_pad);
772f7917c00SJeff Kirsher 
773f7917c00SJeff Kirsher #if defined(__BIG_ENDIAN_BITFIELD)
774f7917c00SJeff Kirsher 	sge->sge_control |= F_ENABLE_BIG_ENDIAN;
775f7917c00SJeff Kirsher #endif
776f7917c00SJeff Kirsher 
777f7917c00SJeff Kirsher 	/* Initialize no-resource timer */
778f7917c00SJeff Kirsher 	sge->intrtimer_nres = SGE_INTRTIMER_NRES * core_ticks_per_usec(ap);
779f7917c00SJeff Kirsher 
780f7917c00SJeff Kirsher 	t1_sge_set_coalesce_params(sge, p);
781f7917c00SJeff Kirsher }
782f7917c00SJeff Kirsher 
783f7917c00SJeff Kirsher /*
784f7917c00SJeff Kirsher  * Return the payload capacity of the jumbo free-list buffers.
785f7917c00SJeff Kirsher  */
jumbo_payload_capacity(const struct sge * sge)786f7917c00SJeff Kirsher static inline unsigned int jumbo_payload_capacity(const struct sge *sge)
787f7917c00SJeff Kirsher {
788f7917c00SJeff Kirsher 	return sge->freelQ[sge->jumbo_fl].rx_buffer_size -
789f7917c00SJeff Kirsher 		sge->freelQ[sge->jumbo_fl].dma_offset -
790f7917c00SJeff Kirsher 		sizeof(struct cpl_rx_data);
791f7917c00SJeff Kirsher }
792f7917c00SJeff Kirsher 
793f7917c00SJeff Kirsher /*
794f7917c00SJeff Kirsher  * Frees all SGE related resources and the sge structure itself
795f7917c00SJeff Kirsher  */
t1_sge_destroy(struct sge * sge)796f7917c00SJeff Kirsher void t1_sge_destroy(struct sge *sge)
797f7917c00SJeff Kirsher {
798f7917c00SJeff Kirsher 	int i;
799f7917c00SJeff Kirsher 
800f7917c00SJeff Kirsher 	for_each_port(sge->adapter, i)
801f7917c00SJeff Kirsher 		free_percpu(sge->port_stats[i]);
802f7917c00SJeff Kirsher 
803f7917c00SJeff Kirsher 	kfree(sge->tx_sched);
804f7917c00SJeff Kirsher 	free_tx_resources(sge);
805f7917c00SJeff Kirsher 	free_rx_resources(sge);
806f7917c00SJeff Kirsher 	kfree(sge);
807f7917c00SJeff Kirsher }
808f7917c00SJeff Kirsher 
809f7917c00SJeff Kirsher /*
810f7917c00SJeff Kirsher  * Allocates new RX buffers on the freelist Q (and tracks them on the freelist
811f7917c00SJeff Kirsher  * context Q) until the Q is full or alloc_skb fails.
812f7917c00SJeff Kirsher  *
813f7917c00SJeff Kirsher  * It is possible that the generation bits already match, indicating that the
814f7917c00SJeff Kirsher  * buffer is already valid and nothing needs to be done. This happens when we
815f7917c00SJeff Kirsher  * copied a received buffer into a new sk_buff during the interrupt processing.
816f7917c00SJeff Kirsher  *
817f7917c00SJeff Kirsher  * If the SGE doesn't automatically align packets properly (!sge->rx_pkt_pad),
818f7917c00SJeff Kirsher  * we specify a RX_OFFSET in order to make sure that the IP header is 4B
819f7917c00SJeff Kirsher  * aligned.
820f7917c00SJeff Kirsher  */
refill_free_list(struct sge * sge,struct freelQ * q)821f7917c00SJeff Kirsher static void refill_free_list(struct sge *sge, struct freelQ *q)
822f7917c00SJeff Kirsher {
823f7917c00SJeff Kirsher 	struct pci_dev *pdev = sge->adapter->pdev;
824f7917c00SJeff Kirsher 	struct freelQ_ce *ce = &q->centries[q->pidx];
825f7917c00SJeff Kirsher 	struct freelQ_e *e = &q->entries[q->pidx];
826f7917c00SJeff Kirsher 	unsigned int dma_len = q->rx_buffer_size - q->dma_offset;
827f7917c00SJeff Kirsher 
828f7917c00SJeff Kirsher 	while (q->credits < q->size) {
829f7917c00SJeff Kirsher 		struct sk_buff *skb;
830f7917c00SJeff Kirsher 		dma_addr_t mapping;
831f7917c00SJeff Kirsher 
83270386d40SEric Dumazet 		skb = dev_alloc_skb(q->rx_buffer_size);
833f7917c00SJeff Kirsher 		if (!skb)
834f7917c00SJeff Kirsher 			break;
835f7917c00SJeff Kirsher 
836f7917c00SJeff Kirsher 		skb_reserve(skb, q->dma_offset);
8379ab90179SChristophe JAILLET 		mapping = dma_map_single(&pdev->dev, skb->data, dma_len,
8389ab90179SChristophe JAILLET 					 DMA_FROM_DEVICE);
839f7917c00SJeff Kirsher 		skb_reserve(skb, sge->rx_pkt_pad);
840f7917c00SJeff Kirsher 
841f7917c00SJeff Kirsher 		ce->skb = skb;
842f7917c00SJeff Kirsher 		dma_unmap_addr_set(ce, dma_addr, mapping);
843f7917c00SJeff Kirsher 		dma_unmap_len_set(ce, dma_len, dma_len);
844f7917c00SJeff Kirsher 		e->addr_lo = (u32)mapping;
845f7917c00SJeff Kirsher 		e->addr_hi = (u64)mapping >> 32;
846f7917c00SJeff Kirsher 		e->len_gen = V_CMD_LEN(dma_len) | V_CMD_GEN1(q->genbit);
847f7917c00SJeff Kirsher 		wmb();
848f7917c00SJeff Kirsher 		e->gen2 = V_CMD_GEN2(q->genbit);
849f7917c00SJeff Kirsher 
850f7917c00SJeff Kirsher 		e++;
851f7917c00SJeff Kirsher 		ce++;
852f7917c00SJeff Kirsher 		if (++q->pidx == q->size) {
853f7917c00SJeff Kirsher 			q->pidx = 0;
854f7917c00SJeff Kirsher 			q->genbit ^= 1;
855f7917c00SJeff Kirsher 			ce = q->centries;
856f7917c00SJeff Kirsher 			e = q->entries;
857f7917c00SJeff Kirsher 		}
858f7917c00SJeff Kirsher 		q->credits++;
859f7917c00SJeff Kirsher 	}
860f7917c00SJeff Kirsher }
861f7917c00SJeff Kirsher 
862f7917c00SJeff Kirsher /*
863f7917c00SJeff Kirsher  * Calls refill_free_list for both free lists. If we cannot fill at least 1/4
864f7917c00SJeff Kirsher  * of both rings, we go into 'few interrupt mode' in order to give the system
865f7917c00SJeff Kirsher  * time to free up resources.
866f7917c00SJeff Kirsher  */
freelQs_empty(struct sge * sge)867f7917c00SJeff Kirsher static void freelQs_empty(struct sge *sge)
868f7917c00SJeff Kirsher {
869f7917c00SJeff Kirsher 	struct adapter *adapter = sge->adapter;
870f7917c00SJeff Kirsher 	u32 irq_reg = readl(adapter->regs + A_SG_INT_ENABLE);
871f7917c00SJeff Kirsher 	u32 irqholdoff_reg;
872f7917c00SJeff Kirsher 
873f7917c00SJeff Kirsher 	refill_free_list(sge, &sge->freelQ[0]);
874f7917c00SJeff Kirsher 	refill_free_list(sge, &sge->freelQ[1]);
875f7917c00SJeff Kirsher 
876f7917c00SJeff Kirsher 	if (sge->freelQ[0].credits > (sge->freelQ[0].size >> 2) &&
877f7917c00SJeff Kirsher 	    sge->freelQ[1].credits > (sge->freelQ[1].size >> 2)) {
878f7917c00SJeff Kirsher 		irq_reg |= F_FL_EXHAUSTED;
879f7917c00SJeff Kirsher 		irqholdoff_reg = sge->fixed_intrtimer;
880f7917c00SJeff Kirsher 	} else {
881f7917c00SJeff Kirsher 		/* Clear the F_FL_EXHAUSTED interrupts for now */
882f7917c00SJeff Kirsher 		irq_reg &= ~F_FL_EXHAUSTED;
883f7917c00SJeff Kirsher 		irqholdoff_reg = sge->intrtimer_nres;
884f7917c00SJeff Kirsher 	}
885f7917c00SJeff Kirsher 	writel(irqholdoff_reg, adapter->regs + A_SG_INTRTIMER);
886f7917c00SJeff Kirsher 	writel(irq_reg, adapter->regs + A_SG_INT_ENABLE);
887f7917c00SJeff Kirsher 
888f7917c00SJeff Kirsher 	/* We reenable the Qs to force a freelist GTS interrupt later */
889f7917c00SJeff Kirsher 	doorbell_pio(adapter, F_FL0_ENABLE | F_FL1_ENABLE);
890f7917c00SJeff Kirsher }
891f7917c00SJeff Kirsher 
892f7917c00SJeff Kirsher #define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
893f7917c00SJeff Kirsher #define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
894f7917c00SJeff Kirsher #define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
895f7917c00SJeff Kirsher 			F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
896f7917c00SJeff Kirsher 
897f7917c00SJeff Kirsher /*
898f7917c00SJeff Kirsher  * Disable SGE Interrupts
899f7917c00SJeff Kirsher  */
t1_sge_intr_disable(struct sge * sge)900f7917c00SJeff Kirsher void t1_sge_intr_disable(struct sge *sge)
901f7917c00SJeff Kirsher {
902f7917c00SJeff Kirsher 	u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
903f7917c00SJeff Kirsher 
904f7917c00SJeff Kirsher 	writel(val & ~SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
905f7917c00SJeff Kirsher 	writel(0, sge->adapter->regs + A_SG_INT_ENABLE);
906f7917c00SJeff Kirsher }
907f7917c00SJeff Kirsher 
908f7917c00SJeff Kirsher /*
909f7917c00SJeff Kirsher  * Enable SGE interrupts.
910f7917c00SJeff Kirsher  */
t1_sge_intr_enable(struct sge * sge)911f7917c00SJeff Kirsher void t1_sge_intr_enable(struct sge *sge)
912f7917c00SJeff Kirsher {
913f7917c00SJeff Kirsher 	u32 en = SGE_INT_ENABLE;
914f7917c00SJeff Kirsher 	u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
915f7917c00SJeff Kirsher 
916f7917c00SJeff Kirsher 	if (sge->adapter->port[0].dev->hw_features & NETIF_F_TSO)
917f7917c00SJeff Kirsher 		en &= ~F_PACKET_TOO_BIG;
918f7917c00SJeff Kirsher 	writel(en, sge->adapter->regs + A_SG_INT_ENABLE);
919f7917c00SJeff Kirsher 	writel(val | SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
920f7917c00SJeff Kirsher }
921f7917c00SJeff Kirsher 
922f7917c00SJeff Kirsher /*
923f7917c00SJeff Kirsher  * Clear SGE interrupts.
924f7917c00SJeff Kirsher  */
t1_sge_intr_clear(struct sge * sge)925f7917c00SJeff Kirsher void t1_sge_intr_clear(struct sge *sge)
926f7917c00SJeff Kirsher {
927f7917c00SJeff Kirsher 	writel(SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_CAUSE);
928f7917c00SJeff Kirsher 	writel(0xffffffff, sge->adapter->regs + A_SG_INT_CAUSE);
929f7917c00SJeff Kirsher }
930f7917c00SJeff Kirsher 
931f7917c00SJeff Kirsher /*
932f7917c00SJeff Kirsher  * SGE 'Error' interrupt handler
933f7917c00SJeff Kirsher  */
t1_sge_intr_error_handler(struct sge * sge)93482154580SSebastian Andrzej Siewior bool t1_sge_intr_error_handler(struct sge *sge)
935f7917c00SJeff Kirsher {
936f7917c00SJeff Kirsher 	struct adapter *adapter = sge->adapter;
937f7917c00SJeff Kirsher 	u32 cause = readl(adapter->regs + A_SG_INT_CAUSE);
93882154580SSebastian Andrzej Siewior 	bool wake = false;
939f7917c00SJeff Kirsher 
940f7917c00SJeff Kirsher 	if (adapter->port[0].dev->hw_features & NETIF_F_TSO)
941f7917c00SJeff Kirsher 		cause &= ~F_PACKET_TOO_BIG;
942f7917c00SJeff Kirsher 	if (cause & F_RESPQ_EXHAUSTED)
943f7917c00SJeff Kirsher 		sge->stats.respQ_empty++;
944f7917c00SJeff Kirsher 	if (cause & F_RESPQ_OVERFLOW) {
945f7917c00SJeff Kirsher 		sge->stats.respQ_overflow++;
946f7917c00SJeff Kirsher 		pr_alert("%s: SGE response queue overflow\n",
947f7917c00SJeff Kirsher 			 adapter->name);
948f7917c00SJeff Kirsher 	}
949f7917c00SJeff Kirsher 	if (cause & F_FL_EXHAUSTED) {
950f7917c00SJeff Kirsher 		sge->stats.freelistQ_empty++;
951f7917c00SJeff Kirsher 		freelQs_empty(sge);
952f7917c00SJeff Kirsher 	}
953f7917c00SJeff Kirsher 	if (cause & F_PACKET_TOO_BIG) {
954f7917c00SJeff Kirsher 		sge->stats.pkt_too_big++;
955f7917c00SJeff Kirsher 		pr_alert("%s: SGE max packet size exceeded\n",
956f7917c00SJeff Kirsher 			 adapter->name);
957f7917c00SJeff Kirsher 	}
958f7917c00SJeff Kirsher 	if (cause & F_PACKET_MISMATCH) {
959f7917c00SJeff Kirsher 		sge->stats.pkt_mismatch++;
960f7917c00SJeff Kirsher 		pr_alert("%s: SGE packet mismatch\n", adapter->name);
961f7917c00SJeff Kirsher 	}
96282154580SSebastian Andrzej Siewior 	if (cause & SGE_INT_FATAL) {
96382154580SSebastian Andrzej Siewior 		t1_interrupts_disable(adapter);
96482154580SSebastian Andrzej Siewior 		adapter->pending_thread_intr |= F_PL_INTR_SGE_ERR;
96582154580SSebastian Andrzej Siewior 		wake = true;
96682154580SSebastian Andrzej Siewior 	}
967f7917c00SJeff Kirsher 
968f7917c00SJeff Kirsher 	writel(cause, adapter->regs + A_SG_INT_CAUSE);
96982154580SSebastian Andrzej Siewior 	return wake;
970f7917c00SJeff Kirsher }
971f7917c00SJeff Kirsher 
t1_sge_get_intr_counts(const struct sge * sge)972f7917c00SJeff Kirsher const struct sge_intr_counts *t1_sge_get_intr_counts(const struct sge *sge)
973f7917c00SJeff Kirsher {
974f7917c00SJeff Kirsher 	return &sge->stats;
975f7917c00SJeff Kirsher }
976f7917c00SJeff Kirsher 
t1_sge_get_port_stats(const struct sge * sge,int port,struct sge_port_stats * ss)977f7917c00SJeff Kirsher void t1_sge_get_port_stats(const struct sge *sge, int port,
978f7917c00SJeff Kirsher 			   struct sge_port_stats *ss)
979f7917c00SJeff Kirsher {
980f7917c00SJeff Kirsher 	int cpu;
981f7917c00SJeff Kirsher 
982f7917c00SJeff Kirsher 	memset(ss, 0, sizeof(*ss));
983f7917c00SJeff Kirsher 	for_each_possible_cpu(cpu) {
984f7917c00SJeff Kirsher 		struct sge_port_stats *st = per_cpu_ptr(sge->port_stats[port], cpu);
985f7917c00SJeff Kirsher 
986f7917c00SJeff Kirsher 		ss->rx_cso_good += st->rx_cso_good;
987f7917c00SJeff Kirsher 		ss->tx_cso += st->tx_cso;
988f7917c00SJeff Kirsher 		ss->tx_tso += st->tx_tso;
989f7917c00SJeff Kirsher 		ss->tx_need_hdrroom += st->tx_need_hdrroom;
990f7917c00SJeff Kirsher 		ss->vlan_xtract += st->vlan_xtract;
991f7917c00SJeff Kirsher 		ss->vlan_insert += st->vlan_insert;
992f7917c00SJeff Kirsher 	}
993f7917c00SJeff Kirsher }
994f7917c00SJeff Kirsher 
995f7917c00SJeff Kirsher /**
996f7917c00SJeff Kirsher  *	recycle_fl_buf - recycle a free list buffer
997f7917c00SJeff Kirsher  *	@fl: the free list
998f7917c00SJeff Kirsher  *	@idx: index of buffer to recycle
999f7917c00SJeff Kirsher  *
1000f7917c00SJeff Kirsher  *	Recycles the specified buffer on the given free list by adding it at
1001f7917c00SJeff Kirsher  *	the next available slot on the list.
1002f7917c00SJeff Kirsher  */
recycle_fl_buf(struct freelQ * fl,int idx)1003f7917c00SJeff Kirsher static void recycle_fl_buf(struct freelQ *fl, int idx)
1004f7917c00SJeff Kirsher {
1005f7917c00SJeff Kirsher 	struct freelQ_e *from = &fl->entries[idx];
1006f7917c00SJeff Kirsher 	struct freelQ_e *to = &fl->entries[fl->pidx];
1007f7917c00SJeff Kirsher 
1008f7917c00SJeff Kirsher 	fl->centries[fl->pidx] = fl->centries[idx];
1009f7917c00SJeff Kirsher 	to->addr_lo = from->addr_lo;
1010f7917c00SJeff Kirsher 	to->addr_hi = from->addr_hi;
1011f7917c00SJeff Kirsher 	to->len_gen = G_CMD_LEN(from->len_gen) | V_CMD_GEN1(fl->genbit);
1012f7917c00SJeff Kirsher 	wmb();
1013f7917c00SJeff Kirsher 	to->gen2 = V_CMD_GEN2(fl->genbit);
1014f7917c00SJeff Kirsher 	fl->credits++;
1015f7917c00SJeff Kirsher 
1016f7917c00SJeff Kirsher 	if (++fl->pidx == fl->size) {
1017f7917c00SJeff Kirsher 		fl->pidx = 0;
1018f7917c00SJeff Kirsher 		fl->genbit ^= 1;
1019f7917c00SJeff Kirsher 	}
1020f7917c00SJeff Kirsher }
1021f7917c00SJeff Kirsher 
1022f7917c00SJeff Kirsher static int copybreak __read_mostly = 256;
1023f7917c00SJeff Kirsher module_param(copybreak, int, 0);
1024f7917c00SJeff Kirsher MODULE_PARM_DESC(copybreak, "Receive copy threshold");
1025f7917c00SJeff Kirsher 
1026f7917c00SJeff Kirsher /**
1027f7917c00SJeff Kirsher  *	get_packet - return the next ingress packet buffer
1028e0e31216SAlexander Duyck  *	@adapter: the adapter that received the packet
1029f7917c00SJeff Kirsher  *	@fl: the SGE free list holding the packet
1030f7917c00SJeff Kirsher  *	@len: the actual packet length, excluding any SGE padding
1031f7917c00SJeff Kirsher  *
1032f7917c00SJeff Kirsher  *	Get the next packet from a free list and complete setup of the
1033f7917c00SJeff Kirsher  *	sk_buff.  If the packet is small we make a copy and recycle the
1034f7917c00SJeff Kirsher  *	original buffer, otherwise we use the original buffer itself.  If a
1035f7917c00SJeff Kirsher  *	positive drop threshold is supplied packets are dropped and their
1036f7917c00SJeff Kirsher  *	buffers recycled if (a) the number of remaining buffers is under the
1037f7917c00SJeff Kirsher  *	threshold and the packet is too big to copy, or (b) the packet should
1038f7917c00SJeff Kirsher  *	be copied but there is no memory for the copy.
1039f7917c00SJeff Kirsher  */
get_packet(struct adapter * adapter,struct freelQ * fl,unsigned int len)1040e0e31216SAlexander Duyck static inline struct sk_buff *get_packet(struct adapter *adapter,
1041f7917c00SJeff Kirsher 					 struct freelQ *fl, unsigned int len)
1042f7917c00SJeff Kirsher {
1043f7917c00SJeff Kirsher 	const struct freelQ_ce *ce = &fl->centries[fl->cidx];
1044e0e31216SAlexander Duyck 	struct pci_dev *pdev = adapter->pdev;
1045e0e31216SAlexander Duyck 	struct sk_buff *skb;
1046f7917c00SJeff Kirsher 
1047f7917c00SJeff Kirsher 	if (len < copybreak) {
1048e0e31216SAlexander Duyck 		skb = napi_alloc_skb(&adapter->napi, len);
1049f7917c00SJeff Kirsher 		if (!skb)
1050f7917c00SJeff Kirsher 			goto use_orig_buf;
1051f7917c00SJeff Kirsher 
1052f7917c00SJeff Kirsher 		skb_put(skb, len);
10539ab90179SChristophe JAILLET 		dma_sync_single_for_cpu(&pdev->dev,
1054f7917c00SJeff Kirsher 					dma_unmap_addr(ce, dma_addr),
1055f7917c00SJeff Kirsher 					dma_unmap_len(ce, dma_len),
10569ab90179SChristophe JAILLET 					DMA_FROM_DEVICE);
1057f7917c00SJeff Kirsher 		skb_copy_from_linear_data(ce->skb, skb->data, len);
10589ab90179SChristophe JAILLET 		dma_sync_single_for_device(&pdev->dev,
1059f7917c00SJeff Kirsher 					   dma_unmap_addr(ce, dma_addr),
1060f7917c00SJeff Kirsher 					   dma_unmap_len(ce, dma_len),
10619ab90179SChristophe JAILLET 					   DMA_FROM_DEVICE);
1062f7917c00SJeff Kirsher 		recycle_fl_buf(fl, fl->cidx);
1063f7917c00SJeff Kirsher 		return skb;
1064f7917c00SJeff Kirsher 	}
1065f7917c00SJeff Kirsher 
1066f7917c00SJeff Kirsher use_orig_buf:
1067f7917c00SJeff Kirsher 	if (fl->credits < 2) {
1068f7917c00SJeff Kirsher 		recycle_fl_buf(fl, fl->cidx);
1069f7917c00SJeff Kirsher 		return NULL;
1070f7917c00SJeff Kirsher 	}
1071f7917c00SJeff Kirsher 
10729ab90179SChristophe JAILLET 	dma_unmap_single(&pdev->dev, dma_unmap_addr(ce, dma_addr),
10739ab90179SChristophe JAILLET 			 dma_unmap_len(ce, dma_len), DMA_FROM_DEVICE);
1074f7917c00SJeff Kirsher 	skb = ce->skb;
1075f7917c00SJeff Kirsher 	prefetch(skb->data);
1076f7917c00SJeff Kirsher 
1077f7917c00SJeff Kirsher 	skb_put(skb, len);
1078f7917c00SJeff Kirsher 	return skb;
1079f7917c00SJeff Kirsher }
1080f7917c00SJeff Kirsher 
1081f7917c00SJeff Kirsher /**
1082f7917c00SJeff Kirsher  *	unexpected_offload - handle an unexpected offload packet
1083f7917c00SJeff Kirsher  *	@adapter: the adapter
1084f7917c00SJeff Kirsher  *	@fl: the free list that received the packet
1085f7917c00SJeff Kirsher  *
1086f7917c00SJeff Kirsher  *	Called when we receive an unexpected offload packet (e.g., the TOE
1087f7917c00SJeff Kirsher  *	function is disabled or the card is a NIC).  Prints a message and
1088f7917c00SJeff Kirsher  *	recycles the buffer.
1089f7917c00SJeff Kirsher  */
unexpected_offload(struct adapter * adapter,struct freelQ * fl)1090f7917c00SJeff Kirsher static void unexpected_offload(struct adapter *adapter, struct freelQ *fl)
1091f7917c00SJeff Kirsher {
1092f7917c00SJeff Kirsher 	struct freelQ_ce *ce = &fl->centries[fl->cidx];
1093f7917c00SJeff Kirsher 	struct sk_buff *skb = ce->skb;
1094f7917c00SJeff Kirsher 
10959ab90179SChristophe JAILLET 	dma_sync_single_for_cpu(&adapter->pdev->dev,
10969ab90179SChristophe JAILLET 				dma_unmap_addr(ce, dma_addr),
10979ab90179SChristophe JAILLET 				dma_unmap_len(ce, dma_len), DMA_FROM_DEVICE);
1098f7917c00SJeff Kirsher 	pr_err("%s: unexpected offload packet, cmd %u\n",
1099f7917c00SJeff Kirsher 	       adapter->name, *skb->data);
1100f7917c00SJeff Kirsher 	recycle_fl_buf(fl, fl->cidx);
1101f7917c00SJeff Kirsher }
1102f7917c00SJeff Kirsher 
1103f7917c00SJeff Kirsher /*
1104f7917c00SJeff Kirsher  * T1/T2 SGE limits the maximum DMA size per TX descriptor to
1105f7917c00SJeff Kirsher  * SGE_TX_DESC_MAX_PLEN (16KB). If the PAGE_SIZE is larger than 16KB, the
1106f7917c00SJeff Kirsher  * stack might send more than SGE_TX_DESC_MAX_PLEN in a contiguous manner.
1107f7917c00SJeff Kirsher  * Note that the *_large_page_tx_descs stuff will be optimized out when
1108f7917c00SJeff Kirsher  * PAGE_SIZE <= SGE_TX_DESC_MAX_PLEN.
1109f7917c00SJeff Kirsher  *
1110f7917c00SJeff Kirsher  * compute_large_page_descs() computes how many additional descriptors are
1111f7917c00SJeff Kirsher  * required to break down the stack's request.
1112f7917c00SJeff Kirsher  */
compute_large_page_tx_descs(struct sk_buff * skb)1113f7917c00SJeff Kirsher static inline unsigned int compute_large_page_tx_descs(struct sk_buff *skb)
1114f7917c00SJeff Kirsher {
1115f7917c00SJeff Kirsher 	unsigned int count = 0;
1116f7917c00SJeff Kirsher 
1117f7917c00SJeff Kirsher 	if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
1118f7917c00SJeff Kirsher 		unsigned int nfrags = skb_shinfo(skb)->nr_frags;
1119f7917c00SJeff Kirsher 		unsigned int i, len = skb_headlen(skb);
1120f7917c00SJeff Kirsher 		while (len > SGE_TX_DESC_MAX_PLEN) {
1121f7917c00SJeff Kirsher 			count++;
1122f7917c00SJeff Kirsher 			len -= SGE_TX_DESC_MAX_PLEN;
1123f7917c00SJeff Kirsher 		}
1124f7917c00SJeff Kirsher 		for (i = 0; nfrags--; i++) {
11259e903e08SEric Dumazet 			const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
11269e903e08SEric Dumazet 			len = skb_frag_size(frag);
1127f7917c00SJeff Kirsher 			while (len > SGE_TX_DESC_MAX_PLEN) {
1128f7917c00SJeff Kirsher 				count++;
1129f7917c00SJeff Kirsher 				len -= SGE_TX_DESC_MAX_PLEN;
1130f7917c00SJeff Kirsher 			}
1131f7917c00SJeff Kirsher 		}
1132f7917c00SJeff Kirsher 	}
1133f7917c00SJeff Kirsher 	return count;
1134f7917c00SJeff Kirsher }
1135f7917c00SJeff Kirsher 
1136f7917c00SJeff Kirsher /*
1137f7917c00SJeff Kirsher  * Write a cmdQ entry.
1138f7917c00SJeff Kirsher  *
1139f7917c00SJeff Kirsher  * Since this function writes the 'flags' field, it must not be used to
1140f7917c00SJeff Kirsher  * write the first cmdQ entry.
1141f7917c00SJeff Kirsher  */
write_tx_desc(struct cmdQ_e * e,dma_addr_t mapping,unsigned int len,unsigned int gen,unsigned int eop)1142f7917c00SJeff Kirsher static inline void write_tx_desc(struct cmdQ_e *e, dma_addr_t mapping,
1143f7917c00SJeff Kirsher 				 unsigned int len, unsigned int gen,
1144f7917c00SJeff Kirsher 				 unsigned int eop)
1145f7917c00SJeff Kirsher {
1146f7917c00SJeff Kirsher 	BUG_ON(len > SGE_TX_DESC_MAX_PLEN);
1147f7917c00SJeff Kirsher 
1148f7917c00SJeff Kirsher 	e->addr_lo = (u32)mapping;
1149f7917c00SJeff Kirsher 	e->addr_hi = (u64)mapping >> 32;
1150f7917c00SJeff Kirsher 	e->len_gen = V_CMD_LEN(len) | V_CMD_GEN1(gen);
1151f7917c00SJeff Kirsher 	e->flags = F_CMD_DATAVALID | V_CMD_EOP(eop) | V_CMD_GEN2(gen);
1152f7917c00SJeff Kirsher }
1153f7917c00SJeff Kirsher 
1154f7917c00SJeff Kirsher /*
1155f7917c00SJeff Kirsher  * See comment for previous function.
1156f7917c00SJeff Kirsher  *
1157f7917c00SJeff Kirsher  * write_tx_descs_large_page() writes additional SGE tx descriptors if
1158f7917c00SJeff Kirsher  * *desc_len exceeds HW's capability.
1159f7917c00SJeff Kirsher  */
write_large_page_tx_descs(unsigned int pidx,struct cmdQ_e ** e,struct cmdQ_ce ** ce,unsigned int * gen,dma_addr_t * desc_mapping,unsigned int * desc_len,unsigned int nfrags,struct cmdQ * q)1160f7917c00SJeff Kirsher static inline unsigned int write_large_page_tx_descs(unsigned int pidx,
1161f7917c00SJeff Kirsher 						     struct cmdQ_e **e,
1162f7917c00SJeff Kirsher 						     struct cmdQ_ce **ce,
1163f7917c00SJeff Kirsher 						     unsigned int *gen,
1164f7917c00SJeff Kirsher 						     dma_addr_t *desc_mapping,
1165f7917c00SJeff Kirsher 						     unsigned int *desc_len,
1166f7917c00SJeff Kirsher 						     unsigned int nfrags,
1167f7917c00SJeff Kirsher 						     struct cmdQ *q)
1168f7917c00SJeff Kirsher {
1169f7917c00SJeff Kirsher 	if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
1170f7917c00SJeff Kirsher 		struct cmdQ_e *e1 = *e;
1171f7917c00SJeff Kirsher 		struct cmdQ_ce *ce1 = *ce;
1172f7917c00SJeff Kirsher 
1173f7917c00SJeff Kirsher 		while (*desc_len > SGE_TX_DESC_MAX_PLEN) {
1174f7917c00SJeff Kirsher 			*desc_len -= SGE_TX_DESC_MAX_PLEN;
1175f7917c00SJeff Kirsher 			write_tx_desc(e1, *desc_mapping, SGE_TX_DESC_MAX_PLEN,
1176f7917c00SJeff Kirsher 				      *gen, nfrags == 0 && *desc_len == 0);
1177f7917c00SJeff Kirsher 			ce1->skb = NULL;
1178f7917c00SJeff Kirsher 			dma_unmap_len_set(ce1, dma_len, 0);
1179f7917c00SJeff Kirsher 			*desc_mapping += SGE_TX_DESC_MAX_PLEN;
1180f7917c00SJeff Kirsher 			if (*desc_len) {
1181f7917c00SJeff Kirsher 				ce1++;
1182f7917c00SJeff Kirsher 				e1++;
1183f7917c00SJeff Kirsher 				if (++pidx == q->size) {
1184f7917c00SJeff Kirsher 					pidx = 0;
1185f7917c00SJeff Kirsher 					*gen ^= 1;
1186f7917c00SJeff Kirsher 					ce1 = q->centries;
1187f7917c00SJeff Kirsher 					e1 = q->entries;
1188f7917c00SJeff Kirsher 				}
1189f7917c00SJeff Kirsher 			}
1190f7917c00SJeff Kirsher 		}
1191f7917c00SJeff Kirsher 		*e = e1;
1192f7917c00SJeff Kirsher 		*ce = ce1;
1193f7917c00SJeff Kirsher 	}
1194f7917c00SJeff Kirsher 	return pidx;
1195f7917c00SJeff Kirsher }
1196f7917c00SJeff Kirsher 
1197f7917c00SJeff Kirsher /*
1198f7917c00SJeff Kirsher  * Write the command descriptors to transmit the given skb starting at
1199f7917c00SJeff Kirsher  * descriptor pidx with the given generation.
1200f7917c00SJeff Kirsher  */
write_tx_descs(struct adapter * adapter,struct sk_buff * skb,unsigned int pidx,unsigned int gen,struct cmdQ * q)1201f7917c00SJeff Kirsher static inline void write_tx_descs(struct adapter *adapter, struct sk_buff *skb,
1202f7917c00SJeff Kirsher 				  unsigned int pidx, unsigned int gen,
1203f7917c00SJeff Kirsher 				  struct cmdQ *q)
1204f7917c00SJeff Kirsher {
1205f7917c00SJeff Kirsher 	dma_addr_t mapping, desc_mapping;
1206f7917c00SJeff Kirsher 	struct cmdQ_e *e, *e1;
1207f7917c00SJeff Kirsher 	struct cmdQ_ce *ce;
1208f7917c00SJeff Kirsher 	unsigned int i, flags, first_desc_len, desc_len,
1209f7917c00SJeff Kirsher 	    nfrags = skb_shinfo(skb)->nr_frags;
1210f7917c00SJeff Kirsher 
1211f7917c00SJeff Kirsher 	e = e1 = &q->entries[pidx];
1212f7917c00SJeff Kirsher 	ce = &q->centries[pidx];
1213f7917c00SJeff Kirsher 
12149ab90179SChristophe JAILLET 	mapping = dma_map_single(&adapter->pdev->dev, skb->data,
12159ab90179SChristophe JAILLET 				 skb_headlen(skb), DMA_TO_DEVICE);
1216f7917c00SJeff Kirsher 
1217f7917c00SJeff Kirsher 	desc_mapping = mapping;
1218f7917c00SJeff Kirsher 	desc_len = skb_headlen(skb);
1219f7917c00SJeff Kirsher 
1220f7917c00SJeff Kirsher 	flags = F_CMD_DATAVALID | F_CMD_SOP |
1221f7917c00SJeff Kirsher 	    V_CMD_EOP(nfrags == 0 && desc_len <= SGE_TX_DESC_MAX_PLEN) |
1222f7917c00SJeff Kirsher 	    V_CMD_GEN2(gen);
1223f7917c00SJeff Kirsher 	first_desc_len = (desc_len <= SGE_TX_DESC_MAX_PLEN) ?
1224f7917c00SJeff Kirsher 	    desc_len : SGE_TX_DESC_MAX_PLEN;
1225f7917c00SJeff Kirsher 	e->addr_lo = (u32)desc_mapping;
1226f7917c00SJeff Kirsher 	e->addr_hi = (u64)desc_mapping >> 32;
1227f7917c00SJeff Kirsher 	e->len_gen = V_CMD_LEN(first_desc_len) | V_CMD_GEN1(gen);
1228f7917c00SJeff Kirsher 	ce->skb = NULL;
1229f7917c00SJeff Kirsher 	dma_unmap_len_set(ce, dma_len, 0);
1230f7917c00SJeff Kirsher 
1231f7917c00SJeff Kirsher 	if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN &&
1232f7917c00SJeff Kirsher 	    desc_len > SGE_TX_DESC_MAX_PLEN) {
1233f7917c00SJeff Kirsher 		desc_mapping += first_desc_len;
1234f7917c00SJeff Kirsher 		desc_len -= first_desc_len;
1235f7917c00SJeff Kirsher 		e1++;
1236f7917c00SJeff Kirsher 		ce++;
1237f7917c00SJeff Kirsher 		if (++pidx == q->size) {
1238f7917c00SJeff Kirsher 			pidx = 0;
1239f7917c00SJeff Kirsher 			gen ^= 1;
1240f7917c00SJeff Kirsher 			e1 = q->entries;
1241f7917c00SJeff Kirsher 			ce = q->centries;
1242f7917c00SJeff Kirsher 		}
1243f7917c00SJeff Kirsher 		pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
1244f7917c00SJeff Kirsher 						 &desc_mapping, &desc_len,
1245f7917c00SJeff Kirsher 						 nfrags, q);
1246f7917c00SJeff Kirsher 
1247f7917c00SJeff Kirsher 		if (likely(desc_len))
1248f7917c00SJeff Kirsher 			write_tx_desc(e1, desc_mapping, desc_len, gen,
1249f7917c00SJeff Kirsher 				      nfrags == 0);
1250f7917c00SJeff Kirsher 	}
1251f7917c00SJeff Kirsher 
1252f7917c00SJeff Kirsher 	ce->skb = NULL;
1253f7917c00SJeff Kirsher 	dma_unmap_addr_set(ce, dma_addr, mapping);
1254f7917c00SJeff Kirsher 	dma_unmap_len_set(ce, dma_len, skb_headlen(skb));
1255f7917c00SJeff Kirsher 
1256f7917c00SJeff Kirsher 	for (i = 0; nfrags--; i++) {
1257f7917c00SJeff Kirsher 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1258f7917c00SJeff Kirsher 		e1++;
1259f7917c00SJeff Kirsher 		ce++;
1260f7917c00SJeff Kirsher 		if (++pidx == q->size) {
1261f7917c00SJeff Kirsher 			pidx = 0;
1262f7917c00SJeff Kirsher 			gen ^= 1;
1263f7917c00SJeff Kirsher 			e1 = q->entries;
1264f7917c00SJeff Kirsher 			ce = q->centries;
1265f7917c00SJeff Kirsher 		}
1266f7917c00SJeff Kirsher 
126701139222SIan Campbell 		mapping = skb_frag_dma_map(&adapter->pdev->dev, frag, 0,
12689e903e08SEric Dumazet 					   skb_frag_size(frag), DMA_TO_DEVICE);
1269f7917c00SJeff Kirsher 		desc_mapping = mapping;
12709e903e08SEric Dumazet 		desc_len = skb_frag_size(frag);
1271f7917c00SJeff Kirsher 
1272f7917c00SJeff Kirsher 		pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
1273f7917c00SJeff Kirsher 						 &desc_mapping, &desc_len,
1274f7917c00SJeff Kirsher 						 nfrags, q);
1275f7917c00SJeff Kirsher 		if (likely(desc_len))
1276f7917c00SJeff Kirsher 			write_tx_desc(e1, desc_mapping, desc_len, gen,
1277f7917c00SJeff Kirsher 				      nfrags == 0);
1278f7917c00SJeff Kirsher 		ce->skb = NULL;
1279f7917c00SJeff Kirsher 		dma_unmap_addr_set(ce, dma_addr, mapping);
12809e903e08SEric Dumazet 		dma_unmap_len_set(ce, dma_len, skb_frag_size(frag));
1281f7917c00SJeff Kirsher 	}
1282f7917c00SJeff Kirsher 	ce->skb = skb;
1283f7917c00SJeff Kirsher 	wmb();
1284f7917c00SJeff Kirsher 	e->flags = flags;
1285f7917c00SJeff Kirsher }
1286f7917c00SJeff Kirsher 
1287f7917c00SJeff Kirsher /*
1288f7917c00SJeff Kirsher  * Clean up completed Tx buffers.
1289f7917c00SJeff Kirsher  */
reclaim_completed_tx(struct sge * sge,struct cmdQ * q)1290f7917c00SJeff Kirsher static inline void reclaim_completed_tx(struct sge *sge, struct cmdQ *q)
1291f7917c00SJeff Kirsher {
1292f7917c00SJeff Kirsher 	unsigned int reclaim = q->processed - q->cleaned;
1293f7917c00SJeff Kirsher 
1294f7917c00SJeff Kirsher 	if (reclaim) {
1295f7917c00SJeff Kirsher 		pr_debug("reclaim_completed_tx processed:%d cleaned:%d\n",
1296f7917c00SJeff Kirsher 			 q->processed, q->cleaned);
1297f7917c00SJeff Kirsher 		free_cmdQ_buffers(sge, q, reclaim);
1298f7917c00SJeff Kirsher 		q->cleaned += reclaim;
1299f7917c00SJeff Kirsher 	}
1300f7917c00SJeff Kirsher }
1301f7917c00SJeff Kirsher 
1302f7917c00SJeff Kirsher /*
1303f7917c00SJeff Kirsher  * Called from tasklet. Checks the scheduler for any
1304f7917c00SJeff Kirsher  * pending skbs that can be sent.
1305f7917c00SJeff Kirsher  */
restart_sched(struct tasklet_struct * t)13066660de07SAllen Pais static void restart_sched(struct tasklet_struct *t)
1307f7917c00SJeff Kirsher {
13086660de07SAllen Pais 	struct sched *s = from_tasklet(s, t, sched_tsk);
13096660de07SAllen Pais 	struct sge *sge = s->sge;
1310f7917c00SJeff Kirsher 	struct adapter *adapter = sge->adapter;
1311f7917c00SJeff Kirsher 	struct cmdQ *q = &sge->cmdQ[0];
1312f7917c00SJeff Kirsher 	struct sk_buff *skb;
1313f7917c00SJeff Kirsher 	unsigned int credits, queued_skb = 0;
1314f7917c00SJeff Kirsher 
1315f7917c00SJeff Kirsher 	spin_lock(&q->lock);
1316f7917c00SJeff Kirsher 	reclaim_completed_tx(sge, q);
1317f7917c00SJeff Kirsher 
1318f7917c00SJeff Kirsher 	credits = q->size - q->in_use;
1319f7917c00SJeff Kirsher 	pr_debug("restart_sched credits=%d\n", credits);
1320f7917c00SJeff Kirsher 	while ((skb = sched_skb(sge, NULL, credits)) != NULL) {
1321f7917c00SJeff Kirsher 		unsigned int genbit, pidx, count;
1322f7917c00SJeff Kirsher 	        count = 1 + skb_shinfo(skb)->nr_frags;
1323f7917c00SJeff Kirsher 		count += compute_large_page_tx_descs(skb);
1324f7917c00SJeff Kirsher 		q->in_use += count;
1325f7917c00SJeff Kirsher 		genbit = q->genbit;
1326f7917c00SJeff Kirsher 		pidx = q->pidx;
1327f7917c00SJeff Kirsher 		q->pidx += count;
1328f7917c00SJeff Kirsher 		if (q->pidx >= q->size) {
1329f7917c00SJeff Kirsher 			q->pidx -= q->size;
1330f7917c00SJeff Kirsher 			q->genbit ^= 1;
1331f7917c00SJeff Kirsher 		}
1332f7917c00SJeff Kirsher 		write_tx_descs(adapter, skb, pidx, genbit, q);
1333f7917c00SJeff Kirsher 	        credits = q->size - q->in_use;
1334f7917c00SJeff Kirsher 		queued_skb = 1;
1335f7917c00SJeff Kirsher 	}
1336f7917c00SJeff Kirsher 
1337f7917c00SJeff Kirsher 	if (queued_skb) {
1338f7917c00SJeff Kirsher 		clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1339f7917c00SJeff Kirsher 		if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
1340f7917c00SJeff Kirsher 			set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1341f7917c00SJeff Kirsher 			writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1342f7917c00SJeff Kirsher 		}
1343f7917c00SJeff Kirsher 	}
1344f7917c00SJeff Kirsher 	spin_unlock(&q->lock);
1345f7917c00SJeff Kirsher }
1346f7917c00SJeff Kirsher 
1347f7917c00SJeff Kirsher /**
1348f7917c00SJeff Kirsher  *	sge_rx - process an ingress ethernet packet
1349f7917c00SJeff Kirsher  *	@sge: the sge structure
1350f7917c00SJeff Kirsher  *	@fl: the free list that contains the packet buffer
1351f7917c00SJeff Kirsher  *	@len: the packet length
1352f7917c00SJeff Kirsher  *
13536167597dSJean Sacren  *	Process an ingress ethernet packet and deliver it to the stack.
1354f7917c00SJeff Kirsher  */
sge_rx(struct sge * sge,struct freelQ * fl,unsigned int len)1355f7917c00SJeff Kirsher static void sge_rx(struct sge *sge, struct freelQ *fl, unsigned int len)
1356f7917c00SJeff Kirsher {
1357f7917c00SJeff Kirsher 	struct sk_buff *skb;
1358f7917c00SJeff Kirsher 	const struct cpl_rx_pkt *p;
1359f7917c00SJeff Kirsher 	struct adapter *adapter = sge->adapter;
1360f7917c00SJeff Kirsher 	struct sge_port_stats *st;
1361f7917c00SJeff Kirsher 	struct net_device *dev;
1362f7917c00SJeff Kirsher 
1363e0e31216SAlexander Duyck 	skb = get_packet(adapter, fl, len - sge->rx_pkt_pad);
1364f7917c00SJeff Kirsher 	if (unlikely(!skb)) {
1365f7917c00SJeff Kirsher 		sge->stats.rx_drops++;
1366f7917c00SJeff Kirsher 		return;
1367f7917c00SJeff Kirsher 	}
1368f7917c00SJeff Kirsher 
1369f7917c00SJeff Kirsher 	p = (const struct cpl_rx_pkt *) skb->data;
1370f7917c00SJeff Kirsher 	if (p->iff >= adapter->params.nports) {
1371f7917c00SJeff Kirsher 		kfree_skb(skb);
1372f7917c00SJeff Kirsher 		return;
1373f7917c00SJeff Kirsher 	}
1374f7917c00SJeff Kirsher 	__skb_pull(skb, sizeof(*p));
1375f7917c00SJeff Kirsher 
1376f7917c00SJeff Kirsher 	st = this_cpu_ptr(sge->port_stats[p->iff]);
1377f7917c00SJeff Kirsher 	dev = adapter->port[p->iff].dev;
1378f7917c00SJeff Kirsher 
1379f7917c00SJeff Kirsher 	skb->protocol = eth_type_trans(skb, dev);
1380f7917c00SJeff Kirsher 	if ((dev->features & NETIF_F_RXCSUM) && p->csum == 0xffff &&
1381f7917c00SJeff Kirsher 	    skb->protocol == htons(ETH_P_IP) &&
1382f7917c00SJeff Kirsher 	    (skb->data[9] == IPPROTO_TCP || skb->data[9] == IPPROTO_UDP)) {
1383f7917c00SJeff Kirsher 		++st->rx_cso_good;
1384f7917c00SJeff Kirsher 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1385f7917c00SJeff Kirsher 	} else
1386f7917c00SJeff Kirsher 		skb_checksum_none_assert(skb);
1387f7917c00SJeff Kirsher 
1388f7917c00SJeff Kirsher 	if (p->vlan_valid) {
1389f7917c00SJeff Kirsher 		st->vlan_xtract++;
139086a9bad3SPatrick McHardy 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(p->vlan));
1391f7917c00SJeff Kirsher 	}
1392f7917c00SJeff Kirsher 	netif_receive_skb(skb);
1393f7917c00SJeff Kirsher }
1394f7917c00SJeff Kirsher 
1395f7917c00SJeff Kirsher /*
1396f7917c00SJeff Kirsher  * Returns true if a command queue has enough available descriptors that
1397f7917c00SJeff Kirsher  * we can resume Tx operation after temporarily disabling its packet queue.
1398f7917c00SJeff Kirsher  */
enough_free_Tx_descs(const struct cmdQ * q)1399f7917c00SJeff Kirsher static inline int enough_free_Tx_descs(const struct cmdQ *q)
1400f7917c00SJeff Kirsher {
1401f7917c00SJeff Kirsher 	unsigned int r = q->processed - q->cleaned;
1402f7917c00SJeff Kirsher 
1403f7917c00SJeff Kirsher 	return q->in_use - r < (q->size >> 1);
1404f7917c00SJeff Kirsher }
1405f7917c00SJeff Kirsher 
1406f7917c00SJeff Kirsher /*
1407f7917c00SJeff Kirsher  * Called when sufficient space has become available in the SGE command queues
1408f7917c00SJeff Kirsher  * after the Tx packet schedulers have been suspended to restart the Tx path.
1409f7917c00SJeff Kirsher  */
restart_tx_queues(struct sge * sge)1410f7917c00SJeff Kirsher static void restart_tx_queues(struct sge *sge)
1411f7917c00SJeff Kirsher {
1412f7917c00SJeff Kirsher 	struct adapter *adap = sge->adapter;
1413f7917c00SJeff Kirsher 	int i;
1414f7917c00SJeff Kirsher 
1415f7917c00SJeff Kirsher 	if (!enough_free_Tx_descs(&sge->cmdQ[0]))
1416f7917c00SJeff Kirsher 		return;
1417f7917c00SJeff Kirsher 
1418f7917c00SJeff Kirsher 	for_each_port(adap, i) {
1419f7917c00SJeff Kirsher 		struct net_device *nd = adap->port[i].dev;
1420f7917c00SJeff Kirsher 
1421f7917c00SJeff Kirsher 		if (test_and_clear_bit(nd->if_port, &sge->stopped_tx_queues) &&
1422f7917c00SJeff Kirsher 		    netif_running(nd)) {
1423f7917c00SJeff Kirsher 			sge->stats.cmdQ_restarted[2]++;
1424f7917c00SJeff Kirsher 			netif_wake_queue(nd);
1425f7917c00SJeff Kirsher 		}
1426f7917c00SJeff Kirsher 	}
1427f7917c00SJeff Kirsher }
1428f7917c00SJeff Kirsher 
1429f7917c00SJeff Kirsher /*
1430f7917c00SJeff Kirsher  * update_tx_info is called from the interrupt handler/NAPI to return cmdQ0
1431f7917c00SJeff Kirsher  * information.
1432f7917c00SJeff Kirsher  */
update_tx_info(struct adapter * adapter,unsigned int flags,unsigned int pr0)1433f7917c00SJeff Kirsher static unsigned int update_tx_info(struct adapter *adapter,
1434f7917c00SJeff Kirsher 					  unsigned int flags,
1435f7917c00SJeff Kirsher 					  unsigned int pr0)
1436f7917c00SJeff Kirsher {
1437f7917c00SJeff Kirsher 	struct sge *sge = adapter->sge;
1438f7917c00SJeff Kirsher 	struct cmdQ *cmdq = &sge->cmdQ[0];
1439f7917c00SJeff Kirsher 
1440f7917c00SJeff Kirsher 	cmdq->processed += pr0;
1441f7917c00SJeff Kirsher 	if (flags & (F_FL0_ENABLE | F_FL1_ENABLE)) {
1442f7917c00SJeff Kirsher 		freelQs_empty(sge);
1443f7917c00SJeff Kirsher 		flags &= ~(F_FL0_ENABLE | F_FL1_ENABLE);
1444f7917c00SJeff Kirsher 	}
1445f7917c00SJeff Kirsher 	if (flags & F_CMDQ0_ENABLE) {
1446f7917c00SJeff Kirsher 		clear_bit(CMDQ_STAT_RUNNING, &cmdq->status);
1447f7917c00SJeff Kirsher 
1448f7917c00SJeff Kirsher 		if (cmdq->cleaned + cmdq->in_use != cmdq->processed &&
1449f7917c00SJeff Kirsher 		    !test_and_set_bit(CMDQ_STAT_LAST_PKT_DB, &cmdq->status)) {
1450f7917c00SJeff Kirsher 			set_bit(CMDQ_STAT_RUNNING, &cmdq->status);
1451f7917c00SJeff Kirsher 			writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1452f7917c00SJeff Kirsher 		}
1453f7917c00SJeff Kirsher 		if (sge->tx_sched)
1454f7917c00SJeff Kirsher 			tasklet_hi_schedule(&sge->tx_sched->sched_tsk);
1455f7917c00SJeff Kirsher 
1456f7917c00SJeff Kirsher 		flags &= ~F_CMDQ0_ENABLE;
1457f7917c00SJeff Kirsher 	}
1458f7917c00SJeff Kirsher 
1459f7917c00SJeff Kirsher 	if (unlikely(sge->stopped_tx_queues != 0))
1460f7917c00SJeff Kirsher 		restart_tx_queues(sge);
1461f7917c00SJeff Kirsher 
1462f7917c00SJeff Kirsher 	return flags;
1463f7917c00SJeff Kirsher }
1464f7917c00SJeff Kirsher 
1465f7917c00SJeff Kirsher /*
1466f7917c00SJeff Kirsher  * Process SGE responses, up to the supplied budget.  Returns the number of
1467f7917c00SJeff Kirsher  * responses processed.  A negative budget is effectively unlimited.
1468f7917c00SJeff Kirsher  */
process_responses(struct adapter * adapter,int budget)1469f7917c00SJeff Kirsher static int process_responses(struct adapter *adapter, int budget)
1470f7917c00SJeff Kirsher {
1471f7917c00SJeff Kirsher 	struct sge *sge = adapter->sge;
1472f7917c00SJeff Kirsher 	struct respQ *q = &sge->respQ;
1473f7917c00SJeff Kirsher 	struct respQ_e *e = &q->entries[q->cidx];
1474f7917c00SJeff Kirsher 	int done = 0;
1475f7917c00SJeff Kirsher 	unsigned int flags = 0;
1476f7917c00SJeff Kirsher 	unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1477f7917c00SJeff Kirsher 
1478f7917c00SJeff Kirsher 	while (done < budget && e->GenerationBit == q->genbit) {
1479f7917c00SJeff Kirsher 		flags |= e->Qsleeping;
1480f7917c00SJeff Kirsher 
1481f7917c00SJeff Kirsher 		cmdq_processed[0] += e->Cmdq0CreditReturn;
1482f7917c00SJeff Kirsher 		cmdq_processed[1] += e->Cmdq1CreditReturn;
1483f7917c00SJeff Kirsher 
1484f7917c00SJeff Kirsher 		/* We batch updates to the TX side to avoid cacheline
1485f7917c00SJeff Kirsher 		 * ping-pong of TX state information on MP where the sender
1486f7917c00SJeff Kirsher 		 * might run on a different CPU than this function...
1487f7917c00SJeff Kirsher 		 */
1488f7917c00SJeff Kirsher 		if (unlikely((flags & F_CMDQ0_ENABLE) || cmdq_processed[0] > 64)) {
1489f7917c00SJeff Kirsher 			flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1490f7917c00SJeff Kirsher 			cmdq_processed[0] = 0;
1491f7917c00SJeff Kirsher 		}
1492f7917c00SJeff Kirsher 
1493f7917c00SJeff Kirsher 		if (unlikely(cmdq_processed[1] > 16)) {
1494f7917c00SJeff Kirsher 			sge->cmdQ[1].processed += cmdq_processed[1];
1495f7917c00SJeff Kirsher 			cmdq_processed[1] = 0;
1496f7917c00SJeff Kirsher 		}
1497f7917c00SJeff Kirsher 
1498f7917c00SJeff Kirsher 		if (likely(e->DataValid)) {
1499f7917c00SJeff Kirsher 			struct freelQ *fl = &sge->freelQ[e->FreelistQid];
1500f7917c00SJeff Kirsher 
1501f7917c00SJeff Kirsher 			BUG_ON(!e->Sop || !e->Eop);
1502f7917c00SJeff Kirsher 			if (unlikely(e->Offload))
1503f7917c00SJeff Kirsher 				unexpected_offload(adapter, fl);
1504f7917c00SJeff Kirsher 			else
1505f7917c00SJeff Kirsher 				sge_rx(sge, fl, e->BufferLength);
1506f7917c00SJeff Kirsher 
1507f7917c00SJeff Kirsher 			++done;
1508f7917c00SJeff Kirsher 
1509f7917c00SJeff Kirsher 			/*
1510f7917c00SJeff Kirsher 			 * Note: this depends on each packet consuming a
1511f7917c00SJeff Kirsher 			 * single free-list buffer; cf. the BUG above.
1512f7917c00SJeff Kirsher 			 */
1513f7917c00SJeff Kirsher 			if (++fl->cidx == fl->size)
1514f7917c00SJeff Kirsher 				fl->cidx = 0;
1515f7917c00SJeff Kirsher 			prefetch(fl->centries[fl->cidx].skb);
1516f7917c00SJeff Kirsher 
1517f7917c00SJeff Kirsher 			if (unlikely(--fl->credits <
1518f7917c00SJeff Kirsher 				     fl->size - SGE_FREEL_REFILL_THRESH))
1519f7917c00SJeff Kirsher 				refill_free_list(sge, fl);
1520f7917c00SJeff Kirsher 		} else
1521f7917c00SJeff Kirsher 			sge->stats.pure_rsps++;
1522f7917c00SJeff Kirsher 
1523f7917c00SJeff Kirsher 		e++;
1524f7917c00SJeff Kirsher 		if (unlikely(++q->cidx == q->size)) {
1525f7917c00SJeff Kirsher 			q->cidx = 0;
1526f7917c00SJeff Kirsher 			q->genbit ^= 1;
1527f7917c00SJeff Kirsher 			e = q->entries;
1528f7917c00SJeff Kirsher 		}
1529f7917c00SJeff Kirsher 		prefetch(e);
1530f7917c00SJeff Kirsher 
1531f7917c00SJeff Kirsher 		if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
1532f7917c00SJeff Kirsher 			writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
1533f7917c00SJeff Kirsher 			q->credits = 0;
1534f7917c00SJeff Kirsher 		}
1535f7917c00SJeff Kirsher 	}
1536f7917c00SJeff Kirsher 
1537f7917c00SJeff Kirsher 	flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1538f7917c00SJeff Kirsher 	sge->cmdQ[1].processed += cmdq_processed[1];
1539f7917c00SJeff Kirsher 
1540f7917c00SJeff Kirsher 	return done;
1541f7917c00SJeff Kirsher }
1542f7917c00SJeff Kirsher 
responses_pending(const struct adapter * adapter)1543f7917c00SJeff Kirsher static inline int responses_pending(const struct adapter *adapter)
1544f7917c00SJeff Kirsher {
1545f7917c00SJeff Kirsher 	const struct respQ *Q = &adapter->sge->respQ;
1546f7917c00SJeff Kirsher 	const struct respQ_e *e = &Q->entries[Q->cidx];
1547f7917c00SJeff Kirsher 
1548f7917c00SJeff Kirsher 	return e->GenerationBit == Q->genbit;
1549f7917c00SJeff Kirsher }
1550f7917c00SJeff Kirsher 
1551f7917c00SJeff Kirsher /*
1552f7917c00SJeff Kirsher  * A simpler version of process_responses() that handles only pure (i.e.,
1553f7917c00SJeff Kirsher  * non data-carrying) responses.  Such respones are too light-weight to justify
1554f7917c00SJeff Kirsher  * calling a softirq when using NAPI, so we handle them specially in hard
1555f7917c00SJeff Kirsher  * interrupt context.  The function is called with a pointer to a response,
1556f7917c00SJeff Kirsher  * which the caller must ensure is a valid pure response.  Returns 1 if it
1557f7917c00SJeff Kirsher  * encounters a valid data-carrying response, 0 otherwise.
1558f7917c00SJeff Kirsher  */
process_pure_responses(struct adapter * adapter)1559f7917c00SJeff Kirsher static int process_pure_responses(struct adapter *adapter)
1560f7917c00SJeff Kirsher {
1561f7917c00SJeff Kirsher 	struct sge *sge = adapter->sge;
1562f7917c00SJeff Kirsher 	struct respQ *q = &sge->respQ;
1563f7917c00SJeff Kirsher 	struct respQ_e *e = &q->entries[q->cidx];
1564f7917c00SJeff Kirsher 	const struct freelQ *fl = &sge->freelQ[e->FreelistQid];
1565f7917c00SJeff Kirsher 	unsigned int flags = 0;
1566f7917c00SJeff Kirsher 	unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1567f7917c00SJeff Kirsher 
1568f7917c00SJeff Kirsher 	prefetch(fl->centries[fl->cidx].skb);
1569f7917c00SJeff Kirsher 	if (e->DataValid)
1570f7917c00SJeff Kirsher 		return 1;
1571f7917c00SJeff Kirsher 
1572f7917c00SJeff Kirsher 	do {
1573f7917c00SJeff Kirsher 		flags |= e->Qsleeping;
1574f7917c00SJeff Kirsher 
1575f7917c00SJeff Kirsher 		cmdq_processed[0] += e->Cmdq0CreditReturn;
1576f7917c00SJeff Kirsher 		cmdq_processed[1] += e->Cmdq1CreditReturn;
1577f7917c00SJeff Kirsher 
1578f7917c00SJeff Kirsher 		e++;
1579f7917c00SJeff Kirsher 		if (unlikely(++q->cidx == q->size)) {
1580f7917c00SJeff Kirsher 			q->cidx = 0;
1581f7917c00SJeff Kirsher 			q->genbit ^= 1;
1582f7917c00SJeff Kirsher 			e = q->entries;
1583f7917c00SJeff Kirsher 		}
1584f7917c00SJeff Kirsher 		prefetch(e);
1585f7917c00SJeff Kirsher 
1586f7917c00SJeff Kirsher 		if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
1587f7917c00SJeff Kirsher 			writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
1588f7917c00SJeff Kirsher 			q->credits = 0;
1589f7917c00SJeff Kirsher 		}
1590f7917c00SJeff Kirsher 		sge->stats.pure_rsps++;
1591f7917c00SJeff Kirsher 	} while (e->GenerationBit == q->genbit && !e->DataValid);
1592f7917c00SJeff Kirsher 
1593f7917c00SJeff Kirsher 	flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1594f7917c00SJeff Kirsher 	sge->cmdQ[1].processed += cmdq_processed[1];
1595f7917c00SJeff Kirsher 
1596f7917c00SJeff Kirsher 	return e->GenerationBit == q->genbit;
1597f7917c00SJeff Kirsher }
1598f7917c00SJeff Kirsher 
1599f7917c00SJeff Kirsher /*
1600f7917c00SJeff Kirsher  * Handler for new data events when using NAPI.  This does not need any locking
1601f7917c00SJeff Kirsher  * or protection from interrupts as data interrupts are off at this point and
1602f7917c00SJeff Kirsher  * other adapter interrupts do not interfere.
1603f7917c00SJeff Kirsher  */
t1_poll(struct napi_struct * napi,int budget)1604f7917c00SJeff Kirsher int t1_poll(struct napi_struct *napi, int budget)
1605f7917c00SJeff Kirsher {
1606f7917c00SJeff Kirsher 	struct adapter *adapter = container_of(napi, struct adapter, napi);
1607f7917c00SJeff Kirsher 	int work_done = process_responses(adapter, budget);
1608f7917c00SJeff Kirsher 
1609f7917c00SJeff Kirsher 	if (likely(work_done < budget)) {
16106ad20165SEric Dumazet 		napi_complete_done(napi, work_done);
1611f7917c00SJeff Kirsher 		writel(adapter->sge->respQ.cidx,
1612f7917c00SJeff Kirsher 		       adapter->regs + A_SG_SLEEPING);
1613f7917c00SJeff Kirsher 	}
1614f7917c00SJeff Kirsher 	return work_done;
1615f7917c00SJeff Kirsher }
1616f7917c00SJeff Kirsher 
t1_interrupt_thread(int irq,void * data)1617fec7fa0aSSebastian Andrzej Siewior irqreturn_t t1_interrupt_thread(int irq, void *data)
1618fec7fa0aSSebastian Andrzej Siewior {
1619fec7fa0aSSebastian Andrzej Siewior 	struct adapter *adapter = data;
1620fec7fa0aSSebastian Andrzej Siewior 	u32 pending_thread_intr;
1621fec7fa0aSSebastian Andrzej Siewior 
1622fec7fa0aSSebastian Andrzej Siewior 	spin_lock_irq(&adapter->async_lock);
1623fec7fa0aSSebastian Andrzej Siewior 	pending_thread_intr = adapter->pending_thread_intr;
1624fec7fa0aSSebastian Andrzej Siewior 	adapter->pending_thread_intr = 0;
1625fec7fa0aSSebastian Andrzej Siewior 	spin_unlock_irq(&adapter->async_lock);
1626fec7fa0aSSebastian Andrzej Siewior 
1627fec7fa0aSSebastian Andrzej Siewior 	if (!pending_thread_intr)
1628fec7fa0aSSebastian Andrzej Siewior 		return IRQ_NONE;
1629fec7fa0aSSebastian Andrzej Siewior 
1630fec7fa0aSSebastian Andrzej Siewior 	if (pending_thread_intr & F_PL_INTR_EXT)
1631fec7fa0aSSebastian Andrzej Siewior 		t1_elmer0_ext_intr_handler(adapter);
1632fec7fa0aSSebastian Andrzej Siewior 
163382154580SSebastian Andrzej Siewior 	/* This error is fatal, interrupts remain off */
163482154580SSebastian Andrzej Siewior 	if (pending_thread_intr & F_PL_INTR_SGE_ERR) {
163582154580SSebastian Andrzej Siewior 		pr_alert("%s: encountered fatal error, operation suspended\n",
163682154580SSebastian Andrzej Siewior 			 adapter->name);
163782154580SSebastian Andrzej Siewior 		t1_sge_stop(adapter->sge);
163882154580SSebastian Andrzej Siewior 		return IRQ_HANDLED;
163982154580SSebastian Andrzej Siewior 	}
164082154580SSebastian Andrzej Siewior 
1641fec7fa0aSSebastian Andrzej Siewior 	spin_lock_irq(&adapter->async_lock);
1642fec7fa0aSSebastian Andrzej Siewior 	adapter->slow_intr_mask |= F_PL_INTR_EXT;
1643fec7fa0aSSebastian Andrzej Siewior 
1644fec7fa0aSSebastian Andrzej Siewior 	writel(F_PL_INTR_EXT, adapter->regs + A_PL_CAUSE);
1645fec7fa0aSSebastian Andrzej Siewior 	writel(adapter->slow_intr_mask | F_PL_INTR_SGE_DATA,
1646fec7fa0aSSebastian Andrzej Siewior 	       adapter->regs + A_PL_ENABLE);
1647fec7fa0aSSebastian Andrzej Siewior 	spin_unlock_irq(&adapter->async_lock);
1648fec7fa0aSSebastian Andrzej Siewior 
1649fec7fa0aSSebastian Andrzej Siewior 	return IRQ_HANDLED;
1650fec7fa0aSSebastian Andrzej Siewior }
1651fec7fa0aSSebastian Andrzej Siewior 
t1_interrupt(int irq,void * data)1652f7917c00SJeff Kirsher irqreturn_t t1_interrupt(int irq, void *data)
1653f7917c00SJeff Kirsher {
1654f7917c00SJeff Kirsher 	struct adapter *adapter = data;
1655f7917c00SJeff Kirsher 	struct sge *sge = adapter->sge;
1656fec7fa0aSSebastian Andrzej Siewior 	irqreturn_t handled;
1657f7917c00SJeff Kirsher 
1658f7917c00SJeff Kirsher 	if (likely(responses_pending(adapter))) {
1659f7917c00SJeff Kirsher 		writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
1660f7917c00SJeff Kirsher 
1661f7917c00SJeff Kirsher 		if (napi_schedule_prep(&adapter->napi)) {
1662f7917c00SJeff Kirsher 			if (process_pure_responses(adapter))
1663f7917c00SJeff Kirsher 				__napi_schedule(&adapter->napi);
1664f7917c00SJeff Kirsher 			else {
1665f7917c00SJeff Kirsher 				/* no data, no NAPI needed */
1666f7917c00SJeff Kirsher 				writel(sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
1667f7917c00SJeff Kirsher 				/* undo schedule_prep */
1668f7917c00SJeff Kirsher 				napi_enable(&adapter->napi);
1669f7917c00SJeff Kirsher 			}
1670f7917c00SJeff Kirsher 		}
1671f7917c00SJeff Kirsher 		return IRQ_HANDLED;
1672f7917c00SJeff Kirsher 	}
1673f7917c00SJeff Kirsher 
1674f7917c00SJeff Kirsher 	spin_lock(&adapter->async_lock);
1675f7917c00SJeff Kirsher 	handled = t1_slow_intr_handler(adapter);
1676f7917c00SJeff Kirsher 	spin_unlock(&adapter->async_lock);
1677f7917c00SJeff Kirsher 
1678fec7fa0aSSebastian Andrzej Siewior 	if (handled == IRQ_NONE)
1679f7917c00SJeff Kirsher 		sge->stats.unhandled_irqs++;
1680f7917c00SJeff Kirsher 
1681fec7fa0aSSebastian Andrzej Siewior 	return handled;
1682f7917c00SJeff Kirsher }
1683f7917c00SJeff Kirsher 
1684f7917c00SJeff Kirsher /*
1685f7917c00SJeff Kirsher  * Enqueues the sk_buff onto the cmdQ[qid] and has hardware fetch it.
1686f7917c00SJeff Kirsher  *
1687f7917c00SJeff Kirsher  * The code figures out how many entries the sk_buff will require in the
1688f7917c00SJeff Kirsher  * cmdQ and updates the cmdQ data structure with the state once the enqueue
1689f7917c00SJeff Kirsher  * has complete. Then, it doesn't access the global structure anymore, but
1690f7917c00SJeff Kirsher  * uses the corresponding fields on the stack. In conjunction with a spinlock
1691f7917c00SJeff Kirsher  * around that code, we can make the function reentrant without holding the
1692f7917c00SJeff Kirsher  * lock when we actually enqueue (which might be expensive, especially on
1693f7917c00SJeff Kirsher  * architectures with IO MMUs).
1694f7917c00SJeff Kirsher  *
1695f7917c00SJeff Kirsher  * This runs with softirqs disabled.
1696f7917c00SJeff Kirsher  */
t1_sge_tx(struct sk_buff * skb,struct adapter * adapter,unsigned int qid,struct net_device * dev)1697f7917c00SJeff Kirsher static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
1698f7917c00SJeff Kirsher 		     unsigned int qid, struct net_device *dev)
1699f7917c00SJeff Kirsher {
1700f7917c00SJeff Kirsher 	struct sge *sge = adapter->sge;
1701f7917c00SJeff Kirsher 	struct cmdQ *q = &sge->cmdQ[qid];
1702f7917c00SJeff Kirsher 	unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
1703f7917c00SJeff Kirsher 
1704a6086a89SFlorian Westphal 	spin_lock(&q->lock);
1705f7917c00SJeff Kirsher 
1706f7917c00SJeff Kirsher 	reclaim_completed_tx(sge, q);
1707f7917c00SJeff Kirsher 
1708f7917c00SJeff Kirsher 	pidx = q->pidx;
1709f7917c00SJeff Kirsher 	credits = q->size - q->in_use;
1710f7917c00SJeff Kirsher 	count = 1 + skb_shinfo(skb)->nr_frags;
1711f7917c00SJeff Kirsher 	count += compute_large_page_tx_descs(skb);
1712f7917c00SJeff Kirsher 
1713f7917c00SJeff Kirsher 	/* Ethernet packet */
1714f7917c00SJeff Kirsher 	if (unlikely(credits < count)) {
1715f7917c00SJeff Kirsher 		if (!netif_queue_stopped(dev)) {
1716f7917c00SJeff Kirsher 			netif_stop_queue(dev);
1717f7917c00SJeff Kirsher 			set_bit(dev->if_port, &sge->stopped_tx_queues);
1718f7917c00SJeff Kirsher 			sge->stats.cmdQ_full[2]++;
1719f7917c00SJeff Kirsher 			pr_err("%s: Tx ring full while queue awake!\n",
1720f7917c00SJeff Kirsher 			       adapter->name);
1721f7917c00SJeff Kirsher 		}
1722f7917c00SJeff Kirsher 		spin_unlock(&q->lock);
1723f7917c00SJeff Kirsher 		return NETDEV_TX_BUSY;
1724f7917c00SJeff Kirsher 	}
1725f7917c00SJeff Kirsher 
1726f7917c00SJeff Kirsher 	if (unlikely(credits - count < q->stop_thres)) {
1727f7917c00SJeff Kirsher 		netif_stop_queue(dev);
1728f7917c00SJeff Kirsher 		set_bit(dev->if_port, &sge->stopped_tx_queues);
1729f7917c00SJeff Kirsher 		sge->stats.cmdQ_full[2]++;
1730f7917c00SJeff Kirsher 	}
1731f7917c00SJeff Kirsher 
1732f7917c00SJeff Kirsher 	/* T204 cmdQ0 skbs that are destined for a certain port have to go
1733f7917c00SJeff Kirsher 	 * through the scheduler.
1734f7917c00SJeff Kirsher 	 */
1735f7917c00SJeff Kirsher 	if (sge->tx_sched && !qid && skb->dev) {
1736f7917c00SJeff Kirsher use_sched:
1737f7917c00SJeff Kirsher 		use_sched_skb = 1;
1738f7917c00SJeff Kirsher 		/* Note that the scheduler might return a different skb than
1739f7917c00SJeff Kirsher 		 * the one passed in.
1740f7917c00SJeff Kirsher 		 */
1741f7917c00SJeff Kirsher 		skb = sched_skb(sge, skb, credits);
1742f7917c00SJeff Kirsher 		if (!skb) {
1743f7917c00SJeff Kirsher 			spin_unlock(&q->lock);
1744f7917c00SJeff Kirsher 			return NETDEV_TX_OK;
1745f7917c00SJeff Kirsher 		}
1746f7917c00SJeff Kirsher 		pidx = q->pidx;
1747f7917c00SJeff Kirsher 		count = 1 + skb_shinfo(skb)->nr_frags;
1748f7917c00SJeff Kirsher 		count += compute_large_page_tx_descs(skb);
1749f7917c00SJeff Kirsher 	}
1750f7917c00SJeff Kirsher 
1751f7917c00SJeff Kirsher 	q->in_use += count;
1752f7917c00SJeff Kirsher 	genbit = q->genbit;
1753f7917c00SJeff Kirsher 	pidx = q->pidx;
1754f7917c00SJeff Kirsher 	q->pidx += count;
1755f7917c00SJeff Kirsher 	if (q->pidx >= q->size) {
1756f7917c00SJeff Kirsher 		q->pidx -= q->size;
1757f7917c00SJeff Kirsher 		q->genbit ^= 1;
1758f7917c00SJeff Kirsher 	}
1759f7917c00SJeff Kirsher 	spin_unlock(&q->lock);
1760f7917c00SJeff Kirsher 
1761f7917c00SJeff Kirsher 	write_tx_descs(adapter, skb, pidx, genbit, q);
1762f7917c00SJeff Kirsher 
1763f7917c00SJeff Kirsher 	/*
1764f7917c00SJeff Kirsher 	 * We always ring the doorbell for cmdQ1.  For cmdQ0, we only ring
1765f7917c00SJeff Kirsher 	 * the doorbell if the Q is asleep. There is a natural race, where
1766f7917c00SJeff Kirsher 	 * the hardware is going to sleep just after we checked, however,
1767f7917c00SJeff Kirsher 	 * then the interrupt handler will detect the outstanding TX packet
1768f7917c00SJeff Kirsher 	 * and ring the doorbell for us.
1769f7917c00SJeff Kirsher 	 */
1770f7917c00SJeff Kirsher 	if (qid)
1771f7917c00SJeff Kirsher 		doorbell_pio(adapter, F_CMDQ1_ENABLE);
1772f7917c00SJeff Kirsher 	else {
1773f7917c00SJeff Kirsher 		clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1774f7917c00SJeff Kirsher 		if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
1775f7917c00SJeff Kirsher 			set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1776f7917c00SJeff Kirsher 			writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1777f7917c00SJeff Kirsher 		}
1778f7917c00SJeff Kirsher 	}
1779f7917c00SJeff Kirsher 
1780f7917c00SJeff Kirsher 	if (use_sched_skb) {
1781f7917c00SJeff Kirsher 		if (spin_trylock(&q->lock)) {
1782f7917c00SJeff Kirsher 			credits = q->size - q->in_use;
1783f7917c00SJeff Kirsher 			skb = NULL;
1784f7917c00SJeff Kirsher 			goto use_sched;
1785f7917c00SJeff Kirsher 		}
1786f7917c00SJeff Kirsher 	}
1787f7917c00SJeff Kirsher 	return NETDEV_TX_OK;
1788f7917c00SJeff Kirsher }
1789f7917c00SJeff Kirsher 
1790f7917c00SJeff Kirsher #define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))
1791f7917c00SJeff Kirsher 
1792f7917c00SJeff Kirsher /*
1793f7917c00SJeff Kirsher  *	eth_hdr_len - return the length of an Ethernet header
1794f7917c00SJeff Kirsher  *	@data: pointer to the start of the Ethernet header
1795f7917c00SJeff Kirsher  *
1796f7917c00SJeff Kirsher  *	Returns the length of an Ethernet header, including optional VLAN tag.
1797f7917c00SJeff Kirsher  */
eth_hdr_len(const void * data)1798f7917c00SJeff Kirsher static inline int eth_hdr_len(const void *data)
1799f7917c00SJeff Kirsher {
1800f7917c00SJeff Kirsher 	const struct ethhdr *e = data;
1801f7917c00SJeff Kirsher 
1802f7917c00SJeff Kirsher 	return e->h_proto == htons(ETH_P_8021Q) ? VLAN_ETH_HLEN : ETH_HLEN;
1803f7917c00SJeff Kirsher }
1804f7917c00SJeff Kirsher 
1805f7917c00SJeff Kirsher /*
1806f7917c00SJeff Kirsher  * Adds the CPL header to the sk_buff and passes it to t1_sge_tx.
1807f7917c00SJeff Kirsher  */
t1_start_xmit(struct sk_buff * skb,struct net_device * dev)1808f7917c00SJeff Kirsher netdev_tx_t t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
1809f7917c00SJeff Kirsher {
1810f7917c00SJeff Kirsher 	struct adapter *adapter = dev->ml_priv;
1811f7917c00SJeff Kirsher 	struct sge *sge = adapter->sge;
1812f7917c00SJeff Kirsher 	struct sge_port_stats *st = this_cpu_ptr(sge->port_stats[dev->if_port]);
1813f7917c00SJeff Kirsher 	struct cpl_tx_pkt *cpl;
1814f7917c00SJeff Kirsher 	struct sk_buff *orig_skb = skb;
1815f7917c00SJeff Kirsher 	int ret;
1816f7917c00SJeff Kirsher 
1817f7917c00SJeff Kirsher 	if (skb->protocol == htons(ETH_P_CPL5))
1818f7917c00SJeff Kirsher 		goto send;
1819f7917c00SJeff Kirsher 
1820f7917c00SJeff Kirsher 	/*
1821f7917c00SJeff Kirsher 	 * We are using a non-standard hard_header_len.
1822f7917c00SJeff Kirsher 	 * Allocate more header room in the rare cases it is not big enough.
1823f7917c00SJeff Kirsher 	 */
1824f7917c00SJeff Kirsher 	if (unlikely(skb_headroom(skb) < dev->hard_header_len - ETH_HLEN)) {
1825f7917c00SJeff Kirsher 		skb = skb_realloc_headroom(skb, sizeof(struct cpl_tx_pkt_lso));
1826f7917c00SJeff Kirsher 		++st->tx_need_hdrroom;
1827f7917c00SJeff Kirsher 		dev_kfree_skb_any(orig_skb);
1828f7917c00SJeff Kirsher 		if (!skb)
1829f7917c00SJeff Kirsher 			return NETDEV_TX_OK;
1830f7917c00SJeff Kirsher 	}
1831f7917c00SJeff Kirsher 
1832f7917c00SJeff Kirsher 	if (skb_shinfo(skb)->gso_size) {
1833f7917c00SJeff Kirsher 		int eth_type;
1834f7917c00SJeff Kirsher 		struct cpl_tx_pkt_lso *hdr;
1835f7917c00SJeff Kirsher 
1836f7917c00SJeff Kirsher 		++st->tx_tso;
1837f7917c00SJeff Kirsher 
1838f7917c00SJeff Kirsher 		eth_type = skb_network_offset(skb) == ETH_HLEN ?
1839f7917c00SJeff Kirsher 			CPL_ETH_II : CPL_ETH_II_VLAN;
1840f7917c00SJeff Kirsher 
1841d58ff351SJohannes Berg 		hdr = skb_push(skb, sizeof(*hdr));
1842f7917c00SJeff Kirsher 		hdr->opcode = CPL_TX_PKT_LSO;
1843f7917c00SJeff Kirsher 		hdr->ip_csum_dis = hdr->l4_csum_dis = 0;
1844f7917c00SJeff Kirsher 		hdr->ip_hdr_words = ip_hdr(skb)->ihl;
1845f7917c00SJeff Kirsher 		hdr->tcp_hdr_words = tcp_hdr(skb)->doff;
1846f7917c00SJeff Kirsher 		hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
1847f7917c00SJeff Kirsher 							  skb_shinfo(skb)->gso_size));
1848f7917c00SJeff Kirsher 		hdr->len = htonl(skb->len - sizeof(*hdr));
1849f7917c00SJeff Kirsher 		cpl = (struct cpl_tx_pkt *)hdr;
1850f7917c00SJeff Kirsher 	} else {
1851f7917c00SJeff Kirsher 		/*
1852f7917c00SJeff Kirsher 		 * Packets shorter than ETH_HLEN can break the MAC, drop them
1853f7917c00SJeff Kirsher 		 * early.  Also, we may get oversized packets because some
1854f7917c00SJeff Kirsher 		 * parts of the kernel don't handle our unusual hard_header_len
1855f7917c00SJeff Kirsher 		 * right, drop those too.
1856f7917c00SJeff Kirsher 		 */
1857f7917c00SJeff Kirsher 		if (unlikely(skb->len < ETH_HLEN ||
1858f7917c00SJeff Kirsher 			     skb->len > dev->mtu + eth_hdr_len(skb->data))) {
1859428ac43fSJoe Perches 			netdev_dbg(dev, "packet size %d hdr %d mtu%d\n",
1860f7917c00SJeff Kirsher 				   skb->len, eth_hdr_len(skb->data), dev->mtu);
1861f7917c00SJeff Kirsher 			dev_kfree_skb_any(skb);
1862f7917c00SJeff Kirsher 			return NETDEV_TX_OK;
1863f7917c00SJeff Kirsher 		}
1864f7917c00SJeff Kirsher 
1865f7917c00SJeff Kirsher 		if (skb->ip_summed == CHECKSUM_PARTIAL &&
1866f7917c00SJeff Kirsher 		    ip_hdr(skb)->protocol == IPPROTO_UDP) {
1867f7917c00SJeff Kirsher 			if (unlikely(skb_checksum_help(skb))) {
1868428ac43fSJoe Perches 				netdev_dbg(dev, "unable to do udp checksum\n");
1869f7917c00SJeff Kirsher 				dev_kfree_skb_any(skb);
1870f7917c00SJeff Kirsher 				return NETDEV_TX_OK;
1871f7917c00SJeff Kirsher 			}
1872f7917c00SJeff Kirsher 		}
1873f7917c00SJeff Kirsher 
1874f7917c00SJeff Kirsher 		/* Hmmm, assuming to catch the gratious arp... and we'll use
1875f7917c00SJeff Kirsher 		 * it to flush out stuck espi packets...
1876f7917c00SJeff Kirsher 		 */
1877f7917c00SJeff Kirsher 		if ((unlikely(!adapter->sge->espibug_skb[dev->if_port]))) {
1878f7917c00SJeff Kirsher 			if (skb->protocol == htons(ETH_P_ARP) &&
1879f7917c00SJeff Kirsher 			    arp_hdr(skb)->ar_op == htons(ARPOP_REQUEST)) {
1880f7917c00SJeff Kirsher 				adapter->sge->espibug_skb[dev->if_port] = skb;
1881f7917c00SJeff Kirsher 				/* We want to re-use this skb later. We
1882f7917c00SJeff Kirsher 				 * simply bump the reference count and it
1883f7917c00SJeff Kirsher 				 * will not be freed...
1884f7917c00SJeff Kirsher 				 */
1885f7917c00SJeff Kirsher 				skb = skb_get(skb);
1886f7917c00SJeff Kirsher 			}
1887f7917c00SJeff Kirsher 		}
1888f7917c00SJeff Kirsher 
1889d58ff351SJohannes Berg 		cpl = __skb_push(skb, sizeof(*cpl));
1890f7917c00SJeff Kirsher 		cpl->opcode = CPL_TX_PKT;
1891f7917c00SJeff Kirsher 		cpl->ip_csum_dis = 1;    /* SW calculates IP csum */
1892f7917c00SJeff Kirsher 		cpl->l4_csum_dis = skb->ip_summed == CHECKSUM_PARTIAL ? 0 : 1;
1893f7917c00SJeff Kirsher 		/* the length field isn't used so don't bother setting it */
1894f7917c00SJeff Kirsher 
1895f7917c00SJeff Kirsher 		st->tx_cso += (skb->ip_summed == CHECKSUM_PARTIAL);
1896f7917c00SJeff Kirsher 	}
1897f7917c00SJeff Kirsher 	cpl->iff = dev->if_port;
1898f7917c00SJeff Kirsher 
1899df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
1900f7917c00SJeff Kirsher 		cpl->vlan_valid = 1;
1901df8a39deSJiri Pirko 		cpl->vlan = htons(skb_vlan_tag_get(skb));
1902f7917c00SJeff Kirsher 		st->vlan_insert++;
1903f7917c00SJeff Kirsher 	} else
1904f7917c00SJeff Kirsher 		cpl->vlan_valid = 0;
1905f7917c00SJeff Kirsher 
1906f7917c00SJeff Kirsher send:
1907f7917c00SJeff Kirsher 	ret = t1_sge_tx(skb, adapter, 0, dev);
1908f7917c00SJeff Kirsher 
1909f7917c00SJeff Kirsher 	/* If transmit busy, and we reallocated skb's due to headroom limit,
1910f7917c00SJeff Kirsher 	 * then silently discard to avoid leak.
1911f7917c00SJeff Kirsher 	 */
1912f7917c00SJeff Kirsher 	if (unlikely(ret != NETDEV_TX_OK && skb != orig_skb)) {
1913f7917c00SJeff Kirsher 		dev_kfree_skb_any(skb);
1914f7917c00SJeff Kirsher 		ret = NETDEV_TX_OK;
1915f7917c00SJeff Kirsher 	}
1916f7917c00SJeff Kirsher 	return ret;
1917f7917c00SJeff Kirsher }
1918f7917c00SJeff Kirsher 
1919f7917c00SJeff Kirsher /*
1920f7917c00SJeff Kirsher  * Callback for the Tx buffer reclaim timer.  Runs with softirqs disabled.
1921f7917c00SJeff Kirsher  */
sge_tx_reclaim_cb(struct timer_list * t)1922cacd2b3fSKees Cook static void sge_tx_reclaim_cb(struct timer_list *t)
1923f7917c00SJeff Kirsher {
1924f7917c00SJeff Kirsher 	int i;
1925cacd2b3fSKees Cook 	struct sge *sge = from_timer(sge, t, tx_reclaim_timer);
1926f7917c00SJeff Kirsher 
1927f7917c00SJeff Kirsher 	for (i = 0; i < SGE_CMDQ_N; ++i) {
1928f7917c00SJeff Kirsher 		struct cmdQ *q = &sge->cmdQ[i];
1929f7917c00SJeff Kirsher 
1930f7917c00SJeff Kirsher 		if (!spin_trylock(&q->lock))
1931f7917c00SJeff Kirsher 			continue;
1932f7917c00SJeff Kirsher 
1933f7917c00SJeff Kirsher 		reclaim_completed_tx(sge, q);
1934f7917c00SJeff Kirsher 		if (i == 0 && q->in_use) {    /* flush pending credits */
1935f7917c00SJeff Kirsher 			writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
1936f7917c00SJeff Kirsher 		}
1937f7917c00SJeff Kirsher 		spin_unlock(&q->lock);
1938f7917c00SJeff Kirsher 	}
1939f7917c00SJeff Kirsher 	mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
1940f7917c00SJeff Kirsher }
1941f7917c00SJeff Kirsher 
1942f7917c00SJeff Kirsher /*
1943f7917c00SJeff Kirsher  * Propagate changes of the SGE coalescing parameters to the HW.
1944f7917c00SJeff Kirsher  */
t1_sge_set_coalesce_params(struct sge * sge,struct sge_params * p)1945f7917c00SJeff Kirsher int t1_sge_set_coalesce_params(struct sge *sge, struct sge_params *p)
1946f7917c00SJeff Kirsher {
1947f7917c00SJeff Kirsher 	sge->fixed_intrtimer = p->rx_coalesce_usecs *
1948f7917c00SJeff Kirsher 		core_ticks_per_usec(sge->adapter);
1949f7917c00SJeff Kirsher 	writel(sge->fixed_intrtimer, sge->adapter->regs + A_SG_INTRTIMER);
1950f7917c00SJeff Kirsher 	return 0;
1951f7917c00SJeff Kirsher }
1952f7917c00SJeff Kirsher 
1953f7917c00SJeff Kirsher /*
1954f7917c00SJeff Kirsher  * Allocates both RX and TX resources and configures the SGE. However,
1955f7917c00SJeff Kirsher  * the hardware is not enabled yet.
1956f7917c00SJeff Kirsher  */
t1_sge_configure(struct sge * sge,struct sge_params * p)1957f7917c00SJeff Kirsher int t1_sge_configure(struct sge *sge, struct sge_params *p)
1958f7917c00SJeff Kirsher {
1959f7917c00SJeff Kirsher 	if (alloc_rx_resources(sge, p))
1960f7917c00SJeff Kirsher 		return -ENOMEM;
1961f7917c00SJeff Kirsher 	if (alloc_tx_resources(sge, p)) {
1962f7917c00SJeff Kirsher 		free_rx_resources(sge);
1963f7917c00SJeff Kirsher 		return -ENOMEM;
1964f7917c00SJeff Kirsher 	}
1965f7917c00SJeff Kirsher 	configure_sge(sge, p);
1966f7917c00SJeff Kirsher 
1967f7917c00SJeff Kirsher 	/*
1968f7917c00SJeff Kirsher 	 * Now that we have sized the free lists calculate the payload
1969f7917c00SJeff Kirsher 	 * capacity of the large buffers.  Other parts of the driver use
1970f7917c00SJeff Kirsher 	 * this to set the max offload coalescing size so that RX packets
1971f7917c00SJeff Kirsher 	 * do not overflow our large buffers.
1972f7917c00SJeff Kirsher 	 */
1973f7917c00SJeff Kirsher 	p->large_buf_capacity = jumbo_payload_capacity(sge);
1974f7917c00SJeff Kirsher 	return 0;
1975f7917c00SJeff Kirsher }
1976f7917c00SJeff Kirsher 
1977f7917c00SJeff Kirsher /*
1978f7917c00SJeff Kirsher  * Disables the DMA engine.
1979f7917c00SJeff Kirsher  */
t1_sge_stop(struct sge * sge)1980f7917c00SJeff Kirsher void t1_sge_stop(struct sge *sge)
1981f7917c00SJeff Kirsher {
1982f7917c00SJeff Kirsher 	int i;
1983f7917c00SJeff Kirsher 	writel(0, sge->adapter->regs + A_SG_CONTROL);
1984f7917c00SJeff Kirsher 	readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
1985f7917c00SJeff Kirsher 
1986f7917c00SJeff Kirsher 	if (is_T2(sge->adapter))
1987f7917c00SJeff Kirsher 		del_timer_sync(&sge->espibug_timer);
1988f7917c00SJeff Kirsher 
1989f7917c00SJeff Kirsher 	del_timer_sync(&sge->tx_reclaim_timer);
1990f7917c00SJeff Kirsher 	if (sge->tx_sched)
1991f7917c00SJeff Kirsher 		tx_sched_stop(sge);
1992f7917c00SJeff Kirsher 
1993f7917c00SJeff Kirsher 	for (i = 0; i < MAX_NPORTS; i++)
1994f7917c00SJeff Kirsher 		kfree_skb(sge->espibug_skb[i]);
1995f7917c00SJeff Kirsher }
1996f7917c00SJeff Kirsher 
1997f7917c00SJeff Kirsher /*
1998f7917c00SJeff Kirsher  * Enables the DMA engine.
1999f7917c00SJeff Kirsher  */
t1_sge_start(struct sge * sge)2000f7917c00SJeff Kirsher void t1_sge_start(struct sge *sge)
2001f7917c00SJeff Kirsher {
2002f7917c00SJeff Kirsher 	refill_free_list(sge, &sge->freelQ[0]);
2003f7917c00SJeff Kirsher 	refill_free_list(sge, &sge->freelQ[1]);
2004f7917c00SJeff Kirsher 
2005f7917c00SJeff Kirsher 	writel(sge->sge_control, sge->adapter->regs + A_SG_CONTROL);
2006f7917c00SJeff Kirsher 	doorbell_pio(sge->adapter, F_FL0_ENABLE | F_FL1_ENABLE);
2007f7917c00SJeff Kirsher 	readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
2008f7917c00SJeff Kirsher 
2009f7917c00SJeff Kirsher 	mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
2010f7917c00SJeff Kirsher 
2011f7917c00SJeff Kirsher 	if (is_T2(sge->adapter))
2012f7917c00SJeff Kirsher 		mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
2013f7917c00SJeff Kirsher }
2014f7917c00SJeff Kirsher 
2015f7917c00SJeff Kirsher /*
2016f7917c00SJeff Kirsher  * Callback for the T2 ESPI 'stuck packet feature' workaorund
2017f7917c00SJeff Kirsher  */
espibug_workaround_t204(struct timer_list * t)2018cacd2b3fSKees Cook static void espibug_workaround_t204(struct timer_list *t)
2019f7917c00SJeff Kirsher {
2020cacd2b3fSKees Cook 	struct sge *sge = from_timer(sge, t, espibug_timer);
2021cacd2b3fSKees Cook 	struct adapter *adapter = sge->adapter;
2022f7917c00SJeff Kirsher 	unsigned int nports = adapter->params.nports;
2023f7917c00SJeff Kirsher 	u32 seop[MAX_NPORTS];
2024f7917c00SJeff Kirsher 
2025f7917c00SJeff Kirsher 	if (adapter->open_device_map & PORT_MASK) {
2026f7917c00SJeff Kirsher 		int i;
2027f7917c00SJeff Kirsher 
2028f7917c00SJeff Kirsher 		if (t1_espi_get_mon_t204(adapter, &(seop[0]), 0) < 0)
2029f7917c00SJeff Kirsher 			return;
2030f7917c00SJeff Kirsher 
2031f7917c00SJeff Kirsher 		for (i = 0; i < nports; i++) {
2032f7917c00SJeff Kirsher 			struct sk_buff *skb = sge->espibug_skb[i];
2033f7917c00SJeff Kirsher 
2034f7917c00SJeff Kirsher 			if (!netif_running(adapter->port[i].dev) ||
2035f7917c00SJeff Kirsher 			    netif_queue_stopped(adapter->port[i].dev) ||
2036f7917c00SJeff Kirsher 			    !seop[i] || ((seop[i] & 0xfff) != 0) || !skb)
2037f7917c00SJeff Kirsher 				continue;
2038f7917c00SJeff Kirsher 
2039f7917c00SJeff Kirsher 			if (!skb->cb[0]) {
2040f7917c00SJeff Kirsher 				skb_copy_to_linear_data_offset(skb,
2041f7917c00SJeff Kirsher 						    sizeof(struct cpl_tx_pkt),
2042f7917c00SJeff Kirsher 							       ch_mac_addr,
2043f7917c00SJeff Kirsher 							       ETH_ALEN);
2044f7917c00SJeff Kirsher 				skb_copy_to_linear_data_offset(skb,
2045f7917c00SJeff Kirsher 							       skb->len - 10,
2046f7917c00SJeff Kirsher 							       ch_mac_addr,
2047f7917c00SJeff Kirsher 							       ETH_ALEN);
2048f7917c00SJeff Kirsher 				skb->cb[0] = 0xff;
2049f7917c00SJeff Kirsher 			}
2050f7917c00SJeff Kirsher 
2051f7917c00SJeff Kirsher 			/* bump the reference count to avoid freeing of
2052f7917c00SJeff Kirsher 			 * the skb once the DMA has completed.
2053f7917c00SJeff Kirsher 			 */
2054f7917c00SJeff Kirsher 			skb = skb_get(skb);
2055f7917c00SJeff Kirsher 			t1_sge_tx(skb, adapter, 0, adapter->port[i].dev);
2056f7917c00SJeff Kirsher 		}
2057f7917c00SJeff Kirsher 	}
2058f7917c00SJeff Kirsher 	mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
2059f7917c00SJeff Kirsher }
2060f7917c00SJeff Kirsher 
espibug_workaround(struct timer_list * t)2061cacd2b3fSKees Cook static void espibug_workaround(struct timer_list *t)
2062f7917c00SJeff Kirsher {
2063cacd2b3fSKees Cook 	struct sge *sge = from_timer(sge, t, espibug_timer);
2064cacd2b3fSKees Cook 	struct adapter *adapter = sge->adapter;
2065f7917c00SJeff Kirsher 
2066f7917c00SJeff Kirsher 	if (netif_running(adapter->port[0].dev)) {
2067f7917c00SJeff Kirsher 	        struct sk_buff *skb = sge->espibug_skb[0];
2068f7917c00SJeff Kirsher 	        u32 seop = t1_espi_get_mon(adapter, 0x930, 0);
2069f7917c00SJeff Kirsher 
2070f7917c00SJeff Kirsher 	        if ((seop & 0xfff0fff) == 0xfff && skb) {
2071f7917c00SJeff Kirsher 	                if (!skb->cb[0]) {
2072f7917c00SJeff Kirsher 	                        skb_copy_to_linear_data_offset(skb,
2073f7917c00SJeff Kirsher 						     sizeof(struct cpl_tx_pkt),
2074f7917c00SJeff Kirsher 							       ch_mac_addr,
2075f7917c00SJeff Kirsher 							       ETH_ALEN);
2076f7917c00SJeff Kirsher 	                        skb_copy_to_linear_data_offset(skb,
2077f7917c00SJeff Kirsher 							       skb->len - 10,
2078f7917c00SJeff Kirsher 							       ch_mac_addr,
2079f7917c00SJeff Kirsher 							       ETH_ALEN);
2080f7917c00SJeff Kirsher 	                        skb->cb[0] = 0xff;
2081f7917c00SJeff Kirsher 	                }
2082f7917c00SJeff Kirsher 
2083f7917c00SJeff Kirsher 	                /* bump the reference count to avoid freeing of the
2084f7917c00SJeff Kirsher 	                 * skb once the DMA has completed.
2085f7917c00SJeff Kirsher 	                 */
2086f7917c00SJeff Kirsher 	                skb = skb_get(skb);
2087f7917c00SJeff Kirsher 	                t1_sge_tx(skb, adapter, 0, adapter->port[0].dev);
2088f7917c00SJeff Kirsher 	        }
2089f7917c00SJeff Kirsher 	}
2090f7917c00SJeff Kirsher 	mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
2091f7917c00SJeff Kirsher }
2092f7917c00SJeff Kirsher 
2093f7917c00SJeff Kirsher /*
2094f7917c00SJeff Kirsher  * Creates a t1_sge structure and returns suggested resource parameters.
2095f7917c00SJeff Kirsher  */
t1_sge_create(struct adapter * adapter,struct sge_params * p)20961dd06ae8SGreg Kroah-Hartman struct sge *t1_sge_create(struct adapter *adapter, struct sge_params *p)
2097f7917c00SJeff Kirsher {
2098f7917c00SJeff Kirsher 	struct sge *sge = kzalloc(sizeof(*sge), GFP_KERNEL);
2099f7917c00SJeff Kirsher 	int i;
2100f7917c00SJeff Kirsher 
2101f7917c00SJeff Kirsher 	if (!sge)
2102f7917c00SJeff Kirsher 		return NULL;
2103f7917c00SJeff Kirsher 
2104f7917c00SJeff Kirsher 	sge->adapter = adapter;
2105f7917c00SJeff Kirsher 	sge->netdev = adapter->port[0].dev;
2106f7917c00SJeff Kirsher 	sge->rx_pkt_pad = t1_is_T1B(adapter) ? 0 : 2;
2107f7917c00SJeff Kirsher 	sge->jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
2108f7917c00SJeff Kirsher 
2109f7917c00SJeff Kirsher 	for_each_port(adapter, i) {
2110f7917c00SJeff Kirsher 		sge->port_stats[i] = alloc_percpu(struct sge_port_stats);
2111f7917c00SJeff Kirsher 		if (!sge->port_stats[i])
2112f7917c00SJeff Kirsher 			goto nomem_port;
2113f7917c00SJeff Kirsher 	}
2114f7917c00SJeff Kirsher 
2115cacd2b3fSKees Cook 	timer_setup(&sge->tx_reclaim_timer, sge_tx_reclaim_cb, 0);
2116f7917c00SJeff Kirsher 
2117f7917c00SJeff Kirsher 	if (is_T2(sge->adapter)) {
2118cacd2b3fSKees Cook 		timer_setup(&sge->espibug_timer,
2119cacd2b3fSKees Cook 			    adapter->params.nports > 1 ? espibug_workaround_t204 : espibug_workaround,
2120cacd2b3fSKees Cook 			    0);
2121f7917c00SJeff Kirsher 
2122cacd2b3fSKees Cook 		if (adapter->params.nports > 1)
2123f7917c00SJeff Kirsher 			tx_sched_init(sge);
2124f7917c00SJeff Kirsher 
2125f7917c00SJeff Kirsher 		sge->espibug_timeout = 1;
2126f7917c00SJeff Kirsher 		/* for T204, every 10ms */
2127f7917c00SJeff Kirsher 		if (adapter->params.nports > 1)
2128f7917c00SJeff Kirsher 			sge->espibug_timeout = HZ/100;
2129f7917c00SJeff Kirsher 	}
2130f7917c00SJeff Kirsher 
2131f7917c00SJeff Kirsher 
2132f7917c00SJeff Kirsher 	p->cmdQ_size[0] = SGE_CMDQ0_E_N;
2133f7917c00SJeff Kirsher 	p->cmdQ_size[1] = SGE_CMDQ1_E_N;
2134f7917c00SJeff Kirsher 	p->freelQ_size[!sge->jumbo_fl] = SGE_FREEL_SIZE;
2135f7917c00SJeff Kirsher 	p->freelQ_size[sge->jumbo_fl] = SGE_JUMBO_FREEL_SIZE;
2136f7917c00SJeff Kirsher 	if (sge->tx_sched) {
2137f7917c00SJeff Kirsher 		if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204)
2138f7917c00SJeff Kirsher 			p->rx_coalesce_usecs = 15;
2139f7917c00SJeff Kirsher 		else
2140f7917c00SJeff Kirsher 			p->rx_coalesce_usecs = 50;
2141f7917c00SJeff Kirsher 	} else
2142f7917c00SJeff Kirsher 		p->rx_coalesce_usecs = 50;
2143f7917c00SJeff Kirsher 
2144f7917c00SJeff Kirsher 	p->coalesce_enable = 0;
2145f7917c00SJeff Kirsher 	p->sample_interval_usecs = 0;
2146f7917c00SJeff Kirsher 
2147f7917c00SJeff Kirsher 	return sge;
2148f7917c00SJeff Kirsher nomem_port:
2149f7917c00SJeff Kirsher 	while (i >= 0) {
2150f7917c00SJeff Kirsher 		free_percpu(sge->port_stats[i]);
2151f7917c00SJeff Kirsher 		--i;
2152f7917c00SJeff Kirsher 	}
2153f7917c00SJeff Kirsher 	kfree(sge);
2154f7917c00SJeff Kirsher 	return NULL;
2155f7917c00SJeff Kirsher 
2156f7917c00SJeff Kirsher }
2157