blob: 9fb77c6d6c6bb5252ab005aca813c74958de2776 [file] [log] [blame]
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001/*****************************************************************************
2 * *
3 * File: sge.c *
Scott Bardone559fb512005-06-23 01:40:19 -04004 * $Revision: 1.26 $ *
5 * $Date: 2005/06/21 18:29:48 $ *
Christoph Lameter8199d3a2005-03-30 13:34:31 -08006 * Description: *
7 * DMA engine. *
8 * part of the Chelsio 10Gb Ethernet Driver. *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License, version 2, as *
12 * published by the Free Software Foundation. *
13 * *
14 * You should have received a copy of the GNU General Public License along *
15 * with this program; if not, write to the Free Software Foundation, Inc., *
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17 * *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED *
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF *
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *
21 * *
22 * http://www.chelsio.com *
23 * *
24 * Copyright (c) 2003 - 2005 Chelsio Communications, Inc. *
25 * All rights reserved. *
26 * *
27 * Maintainers: maintainers@chelsio.com *
28 * *
29 * Authors: Dimitrios Michailidis <dm@chelsio.com> *
30 * Tina Yang <tainay@chelsio.com> *
31 * Felix Marti <felix@chelsio.com> *
32 * Scott Bardone <sbardone@chelsio.com> *
33 * Kurt Ottaway <kottaway@chelsio.com> *
34 * Frank DiMambro <frank@chelsio.com> *
35 * *
36 * History: *
37 * *
38 ****************************************************************************/
39
40#include "common.h"
41
Christoph Lameter8199d3a2005-03-30 13:34:31 -080042#include <linux/types.h>
43#include <linux/errno.h>
44#include <linux/pci.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/if_vlan.h>
48#include <linux/skbuff.h>
49#include <linux/init.h>
50#include <linux/mm.h>
51#include <linux/ip.h>
52#include <linux/in.h>
53#include <linux/if_arp.h>
54
55#include "cpl5_cmd.h"
56#include "sge.h"
57#include "regs.h"
58#include "espi.h"
59
Scott Bardone559fb512005-06-23 01:40:19 -040060
61#ifdef NETIF_F_TSO
Christoph Lameter8199d3a2005-03-30 13:34:31 -080062#include <linux/tcp.h>
Scott Bardone559fb512005-06-23 01:40:19 -040063#endif
Christoph Lameter8199d3a2005-03-30 13:34:31 -080064
65#define SGE_CMDQ_N 2
66#define SGE_FREELQ_N 2
Scott Bardone559fb512005-06-23 01:40:19 -040067#define SGE_CMDQ0_E_N 1024
Christoph Lameter8199d3a2005-03-30 13:34:31 -080068#define SGE_CMDQ1_E_N 128
69#define SGE_FREEL_SIZE 4096
70#define SGE_JUMBO_FREEL_SIZE 512
71#define SGE_FREEL_REFILL_THRESH 16
72#define SGE_RESPQ_E_N 1024
Scott Bardone559fb512005-06-23 01:40:19 -040073#define SGE_INTRTIMER_NRES 1000
74#define SGE_RX_COPY_THRES 256
Christoph Lameter8199d3a2005-03-30 13:34:31 -080075#define SGE_RX_SM_BUF_SIZE 1536
76
Scott Bardone559fb512005-06-23 01:40:19 -040077# define SGE_RX_DROP_THRES 2
Christoph Lameter8199d3a2005-03-30 13:34:31 -080078
Scott Bardone559fb512005-06-23 01:40:19 -040079#define SGE_RESPQ_REPLENISH_THRES (SGE_RESPQ_E_N / 4)
Christoph Lameter8199d3a2005-03-30 13:34:31 -080080
81/*
Scott Bardone559fb512005-06-23 01:40:19 -040082 * Period of the TX buffer reclaim timer. This timer does not need to run
83 * frequently as TX buffers are usually reclaimed by new TX packets.
84 */
85#define TX_RECLAIM_PERIOD (HZ / 4)
86
87#ifndef NET_IP_ALIGN
88# define NET_IP_ALIGN 2
89#endif
90
91#define M_CMD_LEN 0x7fffffff
92#define V_CMD_LEN(v) (v)
93#define G_CMD_LEN(v) ((v) & M_CMD_LEN)
94#define V_CMD_GEN1(v) ((v) << 31)
95#define V_CMD_GEN2(v) (v)
96#define F_CMD_DATAVALID (1 << 1)
97#define F_CMD_SOP (1 << 2)
98#define V_CMD_EOP(v) ((v) << 3)
99
100/*
101 * Command queue, receive buffer list, and response queue descriptors.
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800102 */
103#if defined(__BIG_ENDIAN_BITFIELD)
104struct cmdQ_e {
Scott Bardone559fb512005-06-23 01:40:19 -0400105 u32 addr_lo;
106 u32 len_gen;
107 u32 flags;
108 u32 addr_hi;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800109};
110
111struct freelQ_e {
Scott Bardone559fb512005-06-23 01:40:19 -0400112 u32 addr_lo;
113 u32 len_gen;
114 u32 gen2;
115 u32 addr_hi;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800116};
117
118struct respQ_e {
119 u32 Qsleeping : 4;
120 u32 Cmdq1CreditReturn : 5;
121 u32 Cmdq1DmaComplete : 5;
122 u32 Cmdq0CreditReturn : 5;
123 u32 Cmdq0DmaComplete : 5;
124 u32 FreelistQid : 2;
125 u32 CreditValid : 1;
126 u32 DataValid : 1;
127 u32 Offload : 1;
128 u32 Eop : 1;
129 u32 Sop : 1;
130 u32 GenerationBit : 1;
131 u32 BufferLength;
132};
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800133#elif defined(__LITTLE_ENDIAN_BITFIELD)
134struct cmdQ_e {
Scott Bardone559fb512005-06-23 01:40:19 -0400135 u32 len_gen;
136 u32 addr_lo;
137 u32 addr_hi;
138 u32 flags;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800139};
140
141struct freelQ_e {
Scott Bardone559fb512005-06-23 01:40:19 -0400142 u32 len_gen;
143 u32 addr_lo;
144 u32 addr_hi;
145 u32 gen2;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800146};
147
148struct respQ_e {
149 u32 BufferLength;
150 u32 GenerationBit : 1;
151 u32 Sop : 1;
152 u32 Eop : 1;
153 u32 Offload : 1;
154 u32 DataValid : 1;
155 u32 CreditValid : 1;
156 u32 FreelistQid : 2;
157 u32 Cmdq0DmaComplete : 5;
158 u32 Cmdq0CreditReturn : 5;
159 u32 Cmdq1DmaComplete : 5;
160 u32 Cmdq1CreditReturn : 5;
161 u32 Qsleeping : 4;
162} ;
163#endif
164
165/*
166 * SW Context Command and Freelist Queue Descriptors
167 */
168struct cmdQ_ce {
169 struct sk_buff *skb;
170 DECLARE_PCI_UNMAP_ADDR(dma_addr);
171 DECLARE_PCI_UNMAP_LEN(dma_len);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800172};
173
174struct freelQ_ce {
175 struct sk_buff *skb;
176 DECLARE_PCI_UNMAP_ADDR(dma_addr);
177 DECLARE_PCI_UNMAP_LEN(dma_len);
178};
179
180/*
Scott Bardone559fb512005-06-23 01:40:19 -0400181 * SW command, freelist and response rings
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800182 */
183struct cmdQ {
Scott Bardone559fb512005-06-23 01:40:19 -0400184 unsigned long status; /* HW DMA fetch status */
185 unsigned int in_use; /* # of in-use command descriptors */
186 unsigned int size; /* # of descriptors */
187 unsigned int processed; /* total # of descs HW has processed */
188 unsigned int cleaned; /* total # of descs SW has reclaimed */
189 unsigned int stop_thres; /* SW TX queue suspend threshold */
190 u16 pidx; /* producer index (SW) */
191 u16 cidx; /* consumer index (HW) */
192 u8 genbit; /* current generation (=valid) bit */
193 u8 sop; /* is next entry start of packet? */
194 struct cmdQ_e *entries; /* HW command descriptor Q */
195 struct cmdQ_ce *centries; /* SW command context descriptor Q */
196 spinlock_t lock; /* Lock to protect cmdQ enqueuing */
197 dma_addr_t dma_addr; /* DMA addr HW command descriptor Q */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800198};
199
200struct freelQ {
Scott Bardone559fb512005-06-23 01:40:19 -0400201 unsigned int credits; /* # of available RX buffers */
202 unsigned int size; /* free list capacity */
203 u16 pidx; /* producer index (SW) */
204 u16 cidx; /* consumer index (HW) */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800205 u16 rx_buffer_size; /* Buffer size on this free list */
206 u16 dma_offset; /* DMA offset to align IP headers */
Scott Bardone559fb512005-06-23 01:40:19 -0400207 u16 recycleq_idx; /* skb recycle q to use */
208 u8 genbit; /* current generation (=valid) bit */
209 struct freelQ_e *entries; /* HW freelist descriptor Q */
210 struct freelQ_ce *centries; /* SW freelist context descriptor Q */
211 dma_addr_t dma_addr; /* DMA addr HW freelist descriptor Q */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800212};
213
214struct respQ {
Scott Bardone559fb512005-06-23 01:40:19 -0400215 unsigned int credits; /* credits to be returned to SGE */
216 unsigned int size; /* # of response Q descriptors */
217 u16 cidx; /* consumer index (SW) */
218 u8 genbit; /* current generation(=valid) bit */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800219 struct respQ_e *entries; /* HW response descriptor Q */
Scott Bardone559fb512005-06-23 01:40:19 -0400220 dma_addr_t dma_addr; /* DMA addr HW response descriptor Q */
221};
222
223/* Bit flags for cmdQ.status */
224enum {
225 CMDQ_STAT_RUNNING = 1, /* fetch engine is running */
226 CMDQ_STAT_LAST_PKT_DB = 2 /* last packet rung the doorbell */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800227};
228
229/*
230 * Main SGE data structure
231 *
232 * Interrupts are handled by a single CPU and it is likely that on a MP system
233 * the application is migrated to another CPU. In that scenario, we try to
234 * seperate the RX(in irq context) and TX state in order to decrease memory
235 * contention.
236 */
237struct sge {
238 struct adapter *adapter; /* adapter backpointer */
Scott Bardone559fb512005-06-23 01:40:19 -0400239 struct net_device *netdev; /* netdevice backpointer */
240 struct freelQ freelQ[SGE_FREELQ_N]; /* buffer free lists */
241 struct respQ respQ; /* response Q */
242 unsigned long stopped_tx_queues; /* bitmap of suspended Tx queues */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800243 unsigned int rx_pkt_pad; /* RX padding for L2 packets */
244 unsigned int jumbo_fl; /* jumbo freelist Q index */
Scott Bardone559fb512005-06-23 01:40:19 -0400245 unsigned int intrtimer_nres; /* no-resource interrupt timer */
246 unsigned int fixed_intrtimer;/* non-adaptive interrupt timer */
247 struct timer_list tx_reclaim_timer; /* reclaims TX buffers */
248 struct timer_list espibug_timer;
249 unsigned int espibug_timeout;
250 struct sk_buff *espibug_skb;
251 u32 sge_control; /* shadow value of sge control reg */
252 struct sge_intr_counts stats;
253 struct sge_port_stats port_stats[MAX_NPORTS];
254 struct cmdQ cmdQ[SGE_CMDQ_N] ____cacheline_aligned_in_smp;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800255};
256
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800257/*
258 * PIO to indicate that memory mapped Q contains valid descriptor(s).
259 */
Scott Bardone559fb512005-06-23 01:40:19 -0400260static inline void doorbell_pio(struct adapter *adapter, u32 val)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800261{
262 wmb();
Scott Bardone559fb512005-06-23 01:40:19 -0400263 writel(val, adapter->regs + A_SG_DOORBELL);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800264}
265
266/*
267 * Frees all RX buffers on the freelist Q. The caller must make sure that
268 * the SGE is turned off before calling this function.
269 */
Scott Bardone559fb512005-06-23 01:40:19 -0400270static void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *q)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800271{
Scott Bardone559fb512005-06-23 01:40:19 -0400272 unsigned int cidx = q->cidx;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800273
Scott Bardone559fb512005-06-23 01:40:19 -0400274 while (q->credits--) {
275 struct freelQ_ce *ce = &q->centries[cidx];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800276
277 pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
278 pci_unmap_len(ce, dma_len),
279 PCI_DMA_FROMDEVICE);
280 dev_kfree_skb(ce->skb);
281 ce->skb = NULL;
Scott Bardone559fb512005-06-23 01:40:19 -0400282 if (++cidx == q->size)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800283 cidx = 0;
284 }
285}
286
287/*
288 * Free RX free list and response queue resources.
289 */
290static void free_rx_resources(struct sge *sge)
291{
292 struct pci_dev *pdev = sge->adapter->pdev;
293 unsigned int size, i;
294
295 if (sge->respQ.entries) {
Scott Bardone559fb512005-06-23 01:40:19 -0400296 size = sizeof(struct respQ_e) * sge->respQ.size;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800297 pci_free_consistent(pdev, size, sge->respQ.entries,
298 sge->respQ.dma_addr);
299 }
300
301 for (i = 0; i < SGE_FREELQ_N; i++) {
Scott Bardone559fb512005-06-23 01:40:19 -0400302 struct freelQ *q = &sge->freelQ[i];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800303
Scott Bardone559fb512005-06-23 01:40:19 -0400304 if (q->centries) {
305 free_freelQ_buffers(pdev, q);
306 kfree(q->centries);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800307 }
Scott Bardone559fb512005-06-23 01:40:19 -0400308 if (q->entries) {
309 size = sizeof(struct freelQ_e) * q->size;
310 pci_free_consistent(pdev, size, q->entries,
311 q->dma_addr);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800312 }
313 }
314}
315
316/*
317 * Allocates basic RX resources, consisting of memory mapped freelist Qs and a
Scott Bardone559fb512005-06-23 01:40:19 -0400318 * response queue.
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800319 */
320static int alloc_rx_resources(struct sge *sge, struct sge_params *p)
321{
322 struct pci_dev *pdev = sge->adapter->pdev;
323 unsigned int size, i;
324
325 for (i = 0; i < SGE_FREELQ_N; i++) {
Scott Bardone559fb512005-06-23 01:40:19 -0400326 struct freelQ *q = &sge->freelQ[i];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800327
Scott Bardone559fb512005-06-23 01:40:19 -0400328 q->genbit = 1;
329 q->size = p->freelQ_size[i];
330 q->dma_offset = sge->rx_pkt_pad ? 0 : NET_IP_ALIGN;
331 size = sizeof(struct freelQ_e) * q->size;
332 q->entries = (struct freelQ_e *)
333 pci_alloc_consistent(pdev, size, &q->dma_addr);
334 if (!q->entries)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800335 goto err_no_mem;
Scott Bardone559fb512005-06-23 01:40:19 -0400336 memset(q->entries, 0, size);
337 size = sizeof(struct freelQ_ce) * q->size;
Stephen Hemmingercbee9f92006-11-17 17:01:52 -0800338 q->centries = kzalloc(size, GFP_KERNEL);
Scott Bardone559fb512005-06-23 01:40:19 -0400339 if (!q->centries)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800340 goto err_no_mem;
341 }
342
343 /*
344 * Calculate the buffer sizes for the two free lists. FL0 accommodates
345 * regular sized Ethernet frames, FL1 is sized not to exceed 16K,
346 * including all the sk_buff overhead.
347 *
348 * Note: For T2 FL0 and FL1 are reversed.
349 */
350 sge->freelQ[!sge->jumbo_fl].rx_buffer_size = SGE_RX_SM_BUF_SIZE +
351 sizeof(struct cpl_rx_data) +
352 sge->freelQ[!sge->jumbo_fl].dma_offset;
353 sge->freelQ[sge->jumbo_fl].rx_buffer_size = (16 * 1024) -
354 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
355
Scott Bardone559fb512005-06-23 01:40:19 -0400356 /*
357 * Setup which skb recycle Q should be used when recycling buffers from
358 * each free list.
359 */
360 sge->freelQ[!sge->jumbo_fl].recycleq_idx = 0;
361 sge->freelQ[sge->jumbo_fl].recycleq_idx = 1;
362
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800363 sge->respQ.genbit = 1;
Scott Bardone559fb512005-06-23 01:40:19 -0400364 sge->respQ.size = SGE_RESPQ_E_N;
365 sge->respQ.credits = 0;
366 size = sizeof(struct respQ_e) * sge->respQ.size;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800367 sge->respQ.entries = (struct respQ_e *)
368 pci_alloc_consistent(pdev, size, &sge->respQ.dma_addr);
369 if (!sge->respQ.entries)
370 goto err_no_mem;
371 memset(sge->respQ.entries, 0, size);
372 return 0;
373
374err_no_mem:
375 free_rx_resources(sge);
376 return -ENOMEM;
377}
378
379/*
Scott Bardone559fb512005-06-23 01:40:19 -0400380 * Reclaims n TX descriptors and frees the buffers associated with them.
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800381 */
Scott Bardone559fb512005-06-23 01:40:19 -0400382static void free_cmdQ_buffers(struct sge *sge, struct cmdQ *q, unsigned int n)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800383{
Scott Bardone559fb512005-06-23 01:40:19 -0400384 struct cmdQ_ce *ce;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800385 struct pci_dev *pdev = sge->adapter->pdev;
Scott Bardone559fb512005-06-23 01:40:19 -0400386 unsigned int cidx = q->cidx;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800387
Scott Bardone559fb512005-06-23 01:40:19 -0400388 q->in_use -= n;
389 ce = &q->centries[cidx];
390 while (n--) {
391 if (q->sop)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800392 pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
Scott Bardone559fb512005-06-23 01:40:19 -0400393 pci_unmap_len(ce, dma_len),
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800394 PCI_DMA_TODEVICE);
395 else
396 pci_unmap_page(pdev, pci_unmap_addr(ce, dma_addr),
Scott Bardone559fb512005-06-23 01:40:19 -0400397 pci_unmap_len(ce, dma_len),
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800398 PCI_DMA_TODEVICE);
Scott Bardone559fb512005-06-23 01:40:19 -0400399 q->sop = 0;
400 if (ce->skb) {
401 dev_kfree_skb(ce->skb);
402 q->sop = 1;
403 }
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800404 ce++;
Scott Bardone559fb512005-06-23 01:40:19 -0400405 if (++cidx == q->size) {
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800406 cidx = 0;
Scott Bardone559fb512005-06-23 01:40:19 -0400407 ce = q->centries;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800408 }
409 }
Scott Bardone559fb512005-06-23 01:40:19 -0400410 q->cidx = cidx;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800411}
412
413/*
414 * Free TX resources.
415 *
416 * Assumes that SGE is stopped and all interrupts are disabled.
417 */
418static void free_tx_resources(struct sge *sge)
419{
420 struct pci_dev *pdev = sge->adapter->pdev;
421 unsigned int size, i;
422
423 for (i = 0; i < SGE_CMDQ_N; i++) {
Scott Bardone559fb512005-06-23 01:40:19 -0400424 struct cmdQ *q = &sge->cmdQ[i];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800425
Scott Bardone559fb512005-06-23 01:40:19 -0400426 if (q->centries) {
427 if (q->in_use)
428 free_cmdQ_buffers(sge, q, q->in_use);
429 kfree(q->centries);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800430 }
Scott Bardone559fb512005-06-23 01:40:19 -0400431 if (q->entries) {
432 size = sizeof(struct cmdQ_e) * q->size;
433 pci_free_consistent(pdev, size, q->entries,
434 q->dma_addr);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800435 }
436 }
437}
438
439/*
440 * Allocates basic TX resources, consisting of memory mapped command Qs.
441 */
442static int alloc_tx_resources(struct sge *sge, struct sge_params *p)
443{
444 struct pci_dev *pdev = sge->adapter->pdev;
445 unsigned int size, i;
446
447 for (i = 0; i < SGE_CMDQ_N; i++) {
Scott Bardone559fb512005-06-23 01:40:19 -0400448 struct cmdQ *q = &sge->cmdQ[i];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800449
Scott Bardone559fb512005-06-23 01:40:19 -0400450 q->genbit = 1;
451 q->sop = 1;
452 q->size = p->cmdQ_size[i];
453 q->in_use = 0;
454 q->status = 0;
455 q->processed = q->cleaned = 0;
456 q->stop_thres = 0;
457 spin_lock_init(&q->lock);
458 size = sizeof(struct cmdQ_e) * q->size;
459 q->entries = (struct cmdQ_e *)
460 pci_alloc_consistent(pdev, size, &q->dma_addr);
461 if (!q->entries)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800462 goto err_no_mem;
Scott Bardone559fb512005-06-23 01:40:19 -0400463 memset(q->entries, 0, size);
464 size = sizeof(struct cmdQ_ce) * q->size;
Stephen Hemmingercbee9f92006-11-17 17:01:52 -0800465 q->centries = kzalloc(size, GFP_KERNEL);
Scott Bardone559fb512005-06-23 01:40:19 -0400466 if (!q->centries)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800467 goto err_no_mem;
468 }
469
Scott Bardone559fb512005-06-23 01:40:19 -0400470 /*
471 * CommandQ 0 handles Ethernet and TOE packets, while queue 1 is TOE
472 * only. For queue 0 set the stop threshold so we can handle one more
473 * packet from each port, plus reserve an additional 24 entries for
474 * Ethernet packets only. Queue 1 never suspends nor do we reserve
475 * space for Ethernet packets.
476 */
477 sge->cmdQ[0].stop_thres = sge->adapter->params.nports *
478 (MAX_SKB_FRAGS + 1);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800479 return 0;
480
481err_no_mem:
482 free_tx_resources(sge);
483 return -ENOMEM;
484}
485
486static inline void setup_ring_params(struct adapter *adapter, u64 addr,
487 u32 size, int base_reg_lo,
488 int base_reg_hi, int size_reg)
489{
Scott Bardone559fb512005-06-23 01:40:19 -0400490 writel((u32)addr, adapter->regs + base_reg_lo);
491 writel(addr >> 32, adapter->regs + base_reg_hi);
492 writel(size, adapter->regs + size_reg);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800493}
494
495/*
496 * Enable/disable VLAN acceleration.
497 */
498void t1_set_vlan_accel(struct adapter *adapter, int on_off)
499{
500 struct sge *sge = adapter->sge;
501
502 sge->sge_control &= ~F_VLAN_XTRACT;
503 if (on_off)
504 sge->sge_control |= F_VLAN_XTRACT;
505 if (adapter->open_device_map) {
Scott Bardone559fb512005-06-23 01:40:19 -0400506 writel(sge->sge_control, adapter->regs + A_SG_CONTROL);
507 readl(adapter->regs + A_SG_CONTROL); /* flush */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800508 }
509}
510
511/*
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800512 * Programs the various SGE registers. However, the engine is not yet enabled,
513 * but sge->sge_control is setup and ready to go.
514 */
515static void configure_sge(struct sge *sge, struct sge_params *p)
516{
517 struct adapter *ap = sge->adapter;
Scott Bardone559fb512005-06-23 01:40:19 -0400518
519 writel(0, ap->regs + A_SG_CONTROL);
520 setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800521 A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
Scott Bardone559fb512005-06-23 01:40:19 -0400522 setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800523 A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
524 setup_ring_params(ap, sge->freelQ[0].dma_addr,
Scott Bardone559fb512005-06-23 01:40:19 -0400525 sge->freelQ[0].size, A_SG_FL0BASELWR,
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800526 A_SG_FL0BASEUPR, A_SG_FL0SIZE);
527 setup_ring_params(ap, sge->freelQ[1].dma_addr,
Scott Bardone559fb512005-06-23 01:40:19 -0400528 sge->freelQ[1].size, A_SG_FL1BASELWR,
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800529 A_SG_FL1BASEUPR, A_SG_FL1SIZE);
530
531 /* The threshold comparison uses <. */
Scott Bardone559fb512005-06-23 01:40:19 -0400532 writel(SGE_RX_SM_BUF_SIZE + 1, ap->regs + A_SG_FLTHRESHOLD);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800533
Scott Bardone559fb512005-06-23 01:40:19 -0400534 setup_ring_params(ap, sge->respQ.dma_addr, sge->respQ.size,
535 A_SG_RSPBASELWR, A_SG_RSPBASEUPR, A_SG_RSPSIZE);
536 writel((u32)sge->respQ.size - 1, ap->regs + A_SG_RSPQUEUECREDIT);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800537
538 sge->sge_control = F_CMDQ0_ENABLE | F_CMDQ1_ENABLE | F_FL0_ENABLE |
539 F_FL1_ENABLE | F_CPL_ENABLE | F_RESPONSE_QUEUE_ENABLE |
540 V_CMDQ_PRIORITY(2) | F_DISABLE_CMDQ1_GTS | F_ISCSI_COALESCE |
Scott Bardone559fb512005-06-23 01:40:19 -0400541 F_DISABLE_FL0_GTS | F_DISABLE_FL1_GTS |
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800542 V_RX_PKT_OFFSET(sge->rx_pkt_pad);
543
544#if defined(__BIG_ENDIAN_BITFIELD)
545 sge->sge_control |= F_ENABLE_BIG_ENDIAN;
546#endif
547
Scott Bardone559fb512005-06-23 01:40:19 -0400548 /* Initialize no-resource timer */
549 sge->intrtimer_nres = SGE_INTRTIMER_NRES * core_ticks_per_usec(ap);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800550
Scott Bardone559fb512005-06-23 01:40:19 -0400551 t1_sge_set_coalesce_params(sge, p);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800552}
553
554/*
555 * Return the payload capacity of the jumbo free-list buffers.
556 */
557static inline unsigned int jumbo_payload_capacity(const struct sge *sge)
558{
559 return sge->freelQ[sge->jumbo_fl].rx_buffer_size -
Scott Bardone559fb512005-06-23 01:40:19 -0400560 sge->freelQ[sge->jumbo_fl].dma_offset -
561 sizeof(struct cpl_rx_data);
562}
563
564/*
565 * Frees all SGE related resources and the sge structure itself
566 */
567void t1_sge_destroy(struct sge *sge)
568{
569 if (sge->espibug_skb)
570 kfree_skb(sge->espibug_skb);
571
572 free_tx_resources(sge);
573 free_rx_resources(sge);
574 kfree(sge);
575}
576
577/*
578 * Allocates new RX buffers on the freelist Q (and tracks them on the freelist
579 * context Q) until the Q is full or alloc_skb fails.
580 *
581 * It is possible that the generation bits already match, indicating that the
582 * buffer is already valid and nothing needs to be done. This happens when we
583 * copied a received buffer into a new sk_buff during the interrupt processing.
584 *
585 * If the SGE doesn't automatically align packets properly (!sge->rx_pkt_pad),
586 * we specify a RX_OFFSET in order to make sure that the IP header is 4B
587 * aligned.
588 */
589static void refill_free_list(struct sge *sge, struct freelQ *q)
590{
591 struct pci_dev *pdev = sge->adapter->pdev;
592 struct freelQ_ce *ce = &q->centries[q->pidx];
593 struct freelQ_e *e = &q->entries[q->pidx];
594 unsigned int dma_len = q->rx_buffer_size - q->dma_offset;
595
596
597 while (q->credits < q->size) {
598 struct sk_buff *skb;
599 dma_addr_t mapping;
600
601 skb = alloc_skb(q->rx_buffer_size, GFP_ATOMIC);
602 if (!skb)
603 break;
604
605 skb_reserve(skb, q->dma_offset);
606 mapping = pci_map_single(pdev, skb->data, dma_len,
607 PCI_DMA_FROMDEVICE);
608 ce->skb = skb;
609 pci_unmap_addr_set(ce, dma_addr, mapping);
610 pci_unmap_len_set(ce, dma_len, dma_len);
611 e->addr_lo = (u32)mapping;
612 e->addr_hi = (u64)mapping >> 32;
613 e->len_gen = V_CMD_LEN(dma_len) | V_CMD_GEN1(q->genbit);
614 wmb();
615 e->gen2 = V_CMD_GEN2(q->genbit);
616
617 e++;
618 ce++;
619 if (++q->pidx == q->size) {
620 q->pidx = 0;
621 q->genbit ^= 1;
622 ce = q->centries;
623 e = q->entries;
624 }
625 q->credits++;
626 }
627
628}
629
630/*
631 * Calls refill_free_list for both free lists. If we cannot fill at least 1/4
632 * of both rings, we go into 'few interrupt mode' in order to give the system
633 * time to free up resources.
634 */
635static void freelQs_empty(struct sge *sge)
636{
637 struct adapter *adapter = sge->adapter;
638 u32 irq_reg = readl(adapter->regs + A_SG_INT_ENABLE);
639 u32 irqholdoff_reg;
640
641 refill_free_list(sge, &sge->freelQ[0]);
642 refill_free_list(sge, &sge->freelQ[1]);
643
644 if (sge->freelQ[0].credits > (sge->freelQ[0].size >> 2) &&
645 sge->freelQ[1].credits > (sge->freelQ[1].size >> 2)) {
646 irq_reg |= F_FL_EXHAUSTED;
647 irqholdoff_reg = sge->fixed_intrtimer;
648 } else {
649 /* Clear the F_FL_EXHAUSTED interrupts for now */
650 irq_reg &= ~F_FL_EXHAUSTED;
651 irqholdoff_reg = sge->intrtimer_nres;
652 }
653 writel(irqholdoff_reg, adapter->regs + A_SG_INTRTIMER);
654 writel(irq_reg, adapter->regs + A_SG_INT_ENABLE);
655
656 /* We reenable the Qs to force a freelist GTS interrupt later */
657 doorbell_pio(adapter, F_FL0_ENABLE | F_FL1_ENABLE);
658}
659
660#define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
661#define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
662#define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
663 F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
664
665/*
666 * Disable SGE Interrupts
667 */
668void t1_sge_intr_disable(struct sge *sge)
669{
670 u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
671
672 writel(val & ~SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
673 writel(0, sge->adapter->regs + A_SG_INT_ENABLE);
674}
675
676/*
677 * Enable SGE interrupts.
678 */
679void t1_sge_intr_enable(struct sge *sge)
680{
681 u32 en = SGE_INT_ENABLE;
682 u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
683
684 if (sge->adapter->flags & TSO_CAPABLE)
685 en &= ~F_PACKET_TOO_BIG;
686 writel(en, sge->adapter->regs + A_SG_INT_ENABLE);
687 writel(val | SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
688}
689
690/*
691 * Clear SGE interrupts.
692 */
693void t1_sge_intr_clear(struct sge *sge)
694{
695 writel(SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_CAUSE);
696 writel(0xffffffff, sge->adapter->regs + A_SG_INT_CAUSE);
697}
698
699/*
700 * SGE 'Error' interrupt handler
701 */
702int t1_sge_intr_error_handler(struct sge *sge)
703{
704 struct adapter *adapter = sge->adapter;
705 u32 cause = readl(adapter->regs + A_SG_INT_CAUSE);
706
707 if (adapter->flags & TSO_CAPABLE)
708 cause &= ~F_PACKET_TOO_BIG;
709 if (cause & F_RESPQ_EXHAUSTED)
710 sge->stats.respQ_empty++;
711 if (cause & F_RESPQ_OVERFLOW) {
712 sge->stats.respQ_overflow++;
713 CH_ALERT("%s: SGE response queue overflow\n",
714 adapter->name);
715 }
716 if (cause & F_FL_EXHAUSTED) {
717 sge->stats.freelistQ_empty++;
718 freelQs_empty(sge);
719 }
720 if (cause & F_PACKET_TOO_BIG) {
721 sge->stats.pkt_too_big++;
722 CH_ALERT("%s: SGE max packet size exceeded\n",
723 adapter->name);
724 }
725 if (cause & F_PACKET_MISMATCH) {
726 sge->stats.pkt_mismatch++;
727 CH_ALERT("%s: SGE packet mismatch\n", adapter->name);
728 }
729 if (cause & SGE_INT_FATAL)
730 t1_fatal_err(adapter);
731
732 writel(cause, adapter->regs + A_SG_INT_CAUSE);
733 return 0;
734}
735
736const struct sge_intr_counts *t1_sge_get_intr_counts(struct sge *sge)
737{
738 return &sge->stats;
739}
740
741const struct sge_port_stats *t1_sge_get_port_stats(struct sge *sge, int port)
742{
743 return &sge->port_stats[port];
744}
745
746/**
747 * recycle_fl_buf - recycle a free list buffer
748 * @fl: the free list
749 * @idx: index of buffer to recycle
750 *
751 * Recycles the specified buffer on the given free list by adding it at
752 * the next available slot on the list.
753 */
754static void recycle_fl_buf(struct freelQ *fl, int idx)
755{
756 struct freelQ_e *from = &fl->entries[idx];
757 struct freelQ_e *to = &fl->entries[fl->pidx];
758
759 fl->centries[fl->pidx] = fl->centries[idx];
760 to->addr_lo = from->addr_lo;
761 to->addr_hi = from->addr_hi;
762 to->len_gen = G_CMD_LEN(from->len_gen) | V_CMD_GEN1(fl->genbit);
763 wmb();
764 to->gen2 = V_CMD_GEN2(fl->genbit);
765 fl->credits++;
766
767 if (++fl->pidx == fl->size) {
768 fl->pidx = 0;
769 fl->genbit ^= 1;
770 }
771}
772
773/**
774 * get_packet - return the next ingress packet buffer
775 * @pdev: the PCI device that received the packet
776 * @fl: the SGE free list holding the packet
777 * @len: the actual packet length, excluding any SGE padding
778 * @dma_pad: padding at beginning of buffer left by SGE DMA
779 * @skb_pad: padding to be used if the packet is copied
780 * @copy_thres: length threshold under which a packet should be copied
781 * @drop_thres: # of remaining buffers before we start dropping packets
782 *
783 * Get the next packet from a free list and complete setup of the
784 * sk_buff. If the packet is small we make a copy and recycle the
785 * original buffer, otherwise we use the original buffer itself. If a
786 * positive drop threshold is supplied packets are dropped and their
787 * buffers recycled if (a) the number of remaining buffers is under the
788 * threshold and the packet is too big to copy, or (b) the packet should
789 * be copied but there is no memory for the copy.
790 */
791static inline struct sk_buff *get_packet(struct pci_dev *pdev,
792 struct freelQ *fl, unsigned int len,
793 int dma_pad, int skb_pad,
794 unsigned int copy_thres,
795 unsigned int drop_thres)
796{
797 struct sk_buff *skb;
798 struct freelQ_ce *ce = &fl->centries[fl->cidx];
799
800 if (len < copy_thres) {
801 skb = alloc_skb(len + skb_pad, GFP_ATOMIC);
802 if (likely(skb != NULL)) {
803 skb_reserve(skb, skb_pad);
804 skb_put(skb, len);
805 pci_dma_sync_single_for_cpu(pdev,
806 pci_unmap_addr(ce, dma_addr),
807 pci_unmap_len(ce, dma_len),
808 PCI_DMA_FROMDEVICE);
809 memcpy(skb->data, ce->skb->data + dma_pad, len);
810 pci_dma_sync_single_for_device(pdev,
811 pci_unmap_addr(ce, dma_addr),
812 pci_unmap_len(ce, dma_len),
813 PCI_DMA_FROMDEVICE);
814 } else if (!drop_thres)
815 goto use_orig_buf;
816
817 recycle_fl_buf(fl, fl->cidx);
818 return skb;
819 }
820
821 if (fl->credits < drop_thres) {
822 recycle_fl_buf(fl, fl->cidx);
823 return NULL;
824 }
825
826use_orig_buf:
827 pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
828 pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
829 skb = ce->skb;
830 skb_reserve(skb, dma_pad);
831 skb_put(skb, len);
832 return skb;
833}
834
835/**
836 * unexpected_offload - handle an unexpected offload packet
837 * @adapter: the adapter
838 * @fl: the free list that received the packet
839 *
840 * Called when we receive an unexpected offload packet (e.g., the TOE
841 * function is disabled or the card is a NIC). Prints a message and
842 * recycles the buffer.
843 */
844static void unexpected_offload(struct adapter *adapter, struct freelQ *fl)
845{
846 struct freelQ_ce *ce = &fl->centries[fl->cidx];
847 struct sk_buff *skb = ce->skb;
848
849 pci_dma_sync_single_for_cpu(adapter->pdev, pci_unmap_addr(ce, dma_addr),
850 pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
851 CH_ERR("%s: unexpected offload packet, cmd %u\n",
852 adapter->name, *skb->data);
853 recycle_fl_buf(fl, fl->cidx);
854}
855
856/*
857 * Write the command descriptors to transmit the given skb starting at
858 * descriptor pidx with the given generation.
859 */
860static inline void write_tx_descs(struct adapter *adapter, struct sk_buff *skb,
861 unsigned int pidx, unsigned int gen,
862 struct cmdQ *q)
863{
864 dma_addr_t mapping;
865 struct cmdQ_e *e, *e1;
866 struct cmdQ_ce *ce;
867 unsigned int i, flags, nfrags = skb_shinfo(skb)->nr_frags;
868
869 mapping = pci_map_single(adapter->pdev, skb->data,
870 skb->len - skb->data_len, PCI_DMA_TODEVICE);
871 ce = &q->centries[pidx];
872 ce->skb = NULL;
873 pci_unmap_addr_set(ce, dma_addr, mapping);
874 pci_unmap_len_set(ce, dma_len, skb->len - skb->data_len);
875
876 flags = F_CMD_DATAVALID | F_CMD_SOP | V_CMD_EOP(nfrags == 0) |
877 V_CMD_GEN2(gen);
878 e = &q->entries[pidx];
879 e->addr_lo = (u32)mapping;
880 e->addr_hi = (u64)mapping >> 32;
881 e->len_gen = V_CMD_LEN(skb->len - skb->data_len) | V_CMD_GEN1(gen);
882 for (e1 = e, i = 0; nfrags--; i++) {
883 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
884
885 ce++;
886 e1++;
887 if (++pidx == q->size) {
888 pidx = 0;
889 gen ^= 1;
890 ce = q->centries;
891 e1 = q->entries;
892 }
893
894 mapping = pci_map_page(adapter->pdev, frag->page,
895 frag->page_offset, frag->size,
896 PCI_DMA_TODEVICE);
897 ce->skb = NULL;
898 pci_unmap_addr_set(ce, dma_addr, mapping);
899 pci_unmap_len_set(ce, dma_len, frag->size);
900
901 e1->addr_lo = (u32)mapping;
902 e1->addr_hi = (u64)mapping >> 32;
903 e1->len_gen = V_CMD_LEN(frag->size) | V_CMD_GEN1(gen);
904 e1->flags = F_CMD_DATAVALID | V_CMD_EOP(nfrags == 0) |
905 V_CMD_GEN2(gen);
906 }
907
908 ce->skb = skb;
909 wmb();
910 e->flags = flags;
911}
912
913/*
914 * Clean up completed Tx buffers.
915 */
916static inline void reclaim_completed_tx(struct sge *sge, struct cmdQ *q)
917{
918 unsigned int reclaim = q->processed - q->cleaned;
919
920 if (reclaim) {
921 free_cmdQ_buffers(sge, q, reclaim);
922 q->cleaned += reclaim;
923 }
924}
925
926#ifndef SET_ETHTOOL_OPS
927# define __netif_rx_complete(dev) netif_rx_complete(dev)
928#endif
929
Scott Bardone559fb512005-06-23 01:40:19 -0400930/**
931 * sge_rx - process an ingress ethernet packet
932 * @sge: the sge structure
933 * @fl: the free list that contains the packet buffer
934 * @len: the packet length
935 *
936 * Process an ingress ethernet pakcet and deliver it to the stack.
937 */
938static int sge_rx(struct sge *sge, struct freelQ *fl, unsigned int len)
939{
940 struct sk_buff *skb;
941 struct cpl_rx_pkt *p;
942 struct adapter *adapter = sge->adapter;
943
944 sge->stats.ethernet_pkts++;
945 skb = get_packet(adapter->pdev, fl, len - sge->rx_pkt_pad,
946 sge->rx_pkt_pad, 2, SGE_RX_COPY_THRES,
947 SGE_RX_DROP_THRES);
948 if (!skb) {
949 sge->port_stats[0].rx_drops++; /* charge only port 0 for now */
950 return 0;
951 }
952
953 p = (struct cpl_rx_pkt *)skb->data;
954 skb_pull(skb, sizeof(*p));
955 skb->dev = adapter->port[p->iff].dev;
956 skb->dev->last_rx = jiffies;
957 skb->protocol = eth_type_trans(skb, skb->dev);
958 if ((adapter->flags & RX_CSUM_ENABLED) && p->csum == 0xffff &&
959 skb->protocol == htons(ETH_P_IP) &&
960 (skb->data[9] == IPPROTO_TCP || skb->data[9] == IPPROTO_UDP)) {
961 sge->port_stats[p->iff].rx_cso_good++;
962 skb->ip_summed = CHECKSUM_UNNECESSARY;
963 } else
964 skb->ip_summed = CHECKSUM_NONE;
965
966 if (unlikely(adapter->vlan_grp && p->vlan_valid)) {
967 sge->port_stats[p->iff].vlan_xtract++;
968 if (adapter->params.sge.polling)
969 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp,
970 ntohs(p->vlan));
971 else
972 vlan_hwaccel_rx(skb, adapter->vlan_grp,
973 ntohs(p->vlan));
974 } else if (adapter->params.sge.polling)
975 netif_receive_skb(skb);
976 else
977 netif_rx(skb);
978 return 0;
979}
980
981/*
982 * Returns true if a command queue has enough available descriptors that
983 * we can resume Tx operation after temporarily disabling its packet queue.
984 */
985static inline int enough_free_Tx_descs(const struct cmdQ *q)
986{
987 unsigned int r = q->processed - q->cleaned;
988
989 return q->in_use - r < (q->size >> 1);
990}
991
992/*
993 * Called when sufficient space has become available in the SGE command queues
994 * after the Tx packet schedulers have been suspended to restart the Tx path.
995 */
996static void restart_tx_queues(struct sge *sge)
997{
998 struct adapter *adap = sge->adapter;
999
1000 if (enough_free_Tx_descs(&sge->cmdQ[0])) {
1001 int i;
1002
1003 for_each_port(adap, i) {
1004 struct net_device *nd = adap->port[i].dev;
1005
1006 if (test_and_clear_bit(nd->if_port,
1007 &sge->stopped_tx_queues) &&
1008 netif_running(nd)) {
Scott Bardone232a3472006-03-16 19:20:40 -05001009 sge->stats.cmdQ_restarted[2]++;
Scott Bardone559fb512005-06-23 01:40:19 -04001010 netif_wake_queue(nd);
1011 }
1012 }
1013 }
1014}
1015
1016/*
1017 * update_tx_info is called from the interrupt handler/NAPI to return cmdQ0
1018 * information.
1019 */
1020static unsigned int update_tx_info(struct adapter *adapter,
1021 unsigned int flags,
1022 unsigned int pr0)
1023{
1024 struct sge *sge = adapter->sge;
1025 struct cmdQ *cmdq = &sge->cmdQ[0];
1026
1027 cmdq->processed += pr0;
1028
1029 if (flags & F_CMDQ0_ENABLE) {
1030 clear_bit(CMDQ_STAT_RUNNING, &cmdq->status);
1031
1032 if (cmdq->cleaned + cmdq->in_use != cmdq->processed &&
1033 !test_and_set_bit(CMDQ_STAT_LAST_PKT_DB, &cmdq->status)) {
1034 set_bit(CMDQ_STAT_RUNNING, &cmdq->status);
1035 writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1036 }
1037 flags &= ~F_CMDQ0_ENABLE;
1038 }
1039
1040 if (unlikely(sge->stopped_tx_queues != 0))
1041 restart_tx_queues(sge);
1042
1043 return flags;
1044}
1045
1046/*
1047 * Process SGE responses, up to the supplied budget. Returns the number of
1048 * responses processed. A negative budget is effectively unlimited.
1049 */
1050static int process_responses(struct adapter *adapter, int budget)
1051{
1052 struct sge *sge = adapter->sge;
1053 struct respQ *q = &sge->respQ;
1054 struct respQ_e *e = &q->entries[q->cidx];
1055 int budget_left = budget;
1056 unsigned int flags = 0;
1057 unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1058
1059
1060 while (likely(budget_left && e->GenerationBit == q->genbit)) {
1061 flags |= e->Qsleeping;
1062
1063 cmdq_processed[0] += e->Cmdq0CreditReturn;
1064 cmdq_processed[1] += e->Cmdq1CreditReturn;
1065
1066 /* We batch updates to the TX side to avoid cacheline
1067 * ping-pong of TX state information on MP where the sender
1068 * might run on a different CPU than this function...
1069 */
1070 if (unlikely(flags & F_CMDQ0_ENABLE || cmdq_processed[0] > 64)) {
1071 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1072 cmdq_processed[0] = 0;
1073 }
1074 if (unlikely(cmdq_processed[1] > 16)) {
1075 sge->cmdQ[1].processed += cmdq_processed[1];
1076 cmdq_processed[1] = 0;
1077 }
1078 if (likely(e->DataValid)) {
1079 struct freelQ *fl = &sge->freelQ[e->FreelistQid];
1080
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02001081 BUG_ON(!e->Sop || !e->Eop);
Scott Bardone559fb512005-06-23 01:40:19 -04001082 if (unlikely(e->Offload))
1083 unexpected_offload(adapter, fl);
1084 else
1085 sge_rx(sge, fl, e->BufferLength);
1086
1087 /*
1088 * Note: this depends on each packet consuming a
1089 * single free-list buffer; cf. the BUG above.
1090 */
1091 if (++fl->cidx == fl->size)
1092 fl->cidx = 0;
1093 if (unlikely(--fl->credits <
1094 fl->size - SGE_FREEL_REFILL_THRESH))
1095 refill_free_list(sge, fl);
1096 } else
1097 sge->stats.pure_rsps++;
1098
1099 e++;
1100 if (unlikely(++q->cidx == q->size)) {
1101 q->cidx = 0;
1102 q->genbit ^= 1;
1103 e = q->entries;
1104 }
1105 prefetch(e);
1106
1107 if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
1108 writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
1109 q->credits = 0;
1110 }
1111 --budget_left;
1112 }
1113
1114 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1115 sge->cmdQ[1].processed += cmdq_processed[1];
1116
1117 budget -= budget_left;
1118 return budget;
1119}
1120
1121/*
1122 * A simpler version of process_responses() that handles only pure (i.e.,
1123 * non data-carrying) responses. Such respones are too light-weight to justify
1124 * calling a softirq when using NAPI, so we handle them specially in hard
1125 * interrupt context. The function is called with a pointer to a response,
1126 * which the caller must ensure is a valid pure response. Returns 1 if it
1127 * encounters a valid data-carrying response, 0 otherwise.
1128 */
1129static int process_pure_responses(struct adapter *adapter, struct respQ_e *e)
1130{
1131 struct sge *sge = adapter->sge;
1132 struct respQ *q = &sge->respQ;
1133 unsigned int flags = 0;
1134 unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1135
1136 do {
1137 flags |= e->Qsleeping;
1138
1139 cmdq_processed[0] += e->Cmdq0CreditReturn;
1140 cmdq_processed[1] += e->Cmdq1CreditReturn;
1141
1142 e++;
1143 if (unlikely(++q->cidx == q->size)) {
1144 q->cidx = 0;
1145 q->genbit ^= 1;
1146 e = q->entries;
1147 }
1148 prefetch(e);
1149
1150 if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
1151 writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
1152 q->credits = 0;
1153 }
1154 sge->stats.pure_rsps++;
1155 } while (e->GenerationBit == q->genbit && !e->DataValid);
1156
1157 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1158 sge->cmdQ[1].processed += cmdq_processed[1];
1159
1160 return e->GenerationBit == q->genbit;
1161}
1162
1163/*
1164 * Handler for new data events when using NAPI. This does not need any locking
1165 * or protection from interrupts as data interrupts are off at this point and
1166 * other adapter interrupts do not interfere.
1167 */
1168static int t1_poll(struct net_device *dev, int *budget)
1169{
1170 struct adapter *adapter = dev->priv;
1171 int effective_budget = min(*budget, dev->quota);
1172
1173 int work_done = process_responses(adapter, effective_budget);
1174 *budget -= work_done;
1175 dev->quota -= work_done;
1176
1177 if (work_done >= effective_budget)
1178 return 1;
1179
1180 __netif_rx_complete(dev);
1181
1182 /*
1183 * Because we don't atomically flush the following write it is
1184 * possible that in very rare cases it can reach the device in a way
1185 * that races with a new response being written plus an error interrupt
1186 * causing the NAPI interrupt handler below to return unhandled status
1187 * to the OS. To protect against this would require flushing the write
1188 * and doing both the write and the flush with interrupts off. Way too
1189 * expensive and unjustifiable given the rarity of the race.
1190 */
1191 writel(adapter->sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
1192 return 0;
1193}
1194
1195/*
1196 * Returns true if the device is already scheduled for polling.
1197 */
1198static inline int napi_is_scheduled(struct net_device *dev)
1199{
1200 return test_bit(__LINK_STATE_RX_SCHED, &dev->state);
1201}
1202
1203/*
1204 * NAPI version of the main interrupt handler.
1205 */
David Howells7d12e782006-10-05 14:55:46 +01001206static irqreturn_t t1_interrupt_napi(int irq, void *data)
Scott Bardone559fb512005-06-23 01:40:19 -04001207{
1208 int handled;
1209 struct adapter *adapter = data;
1210 struct sge *sge = adapter->sge;
1211 struct respQ *q = &adapter->sge->respQ;
1212
1213 /*
1214 * Clear the SGE_DATA interrupt first thing. Normally the NAPI
1215 * handler has control of the response queue and the interrupt handler
1216 * can look at the queue reliably only once it knows NAPI is off.
1217 * We can't wait that long to clear the SGE_DATA interrupt because we
1218 * could race with t1_poll rearming the SGE interrupt, so we need to
1219 * clear the interrupt speculatively and really early on.
1220 */
1221 writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
1222
1223 spin_lock(&adapter->async_lock);
1224 if (!napi_is_scheduled(sge->netdev)) {
1225 struct respQ_e *e = &q->entries[q->cidx];
1226
1227 if (e->GenerationBit == q->genbit) {
1228 if (e->DataValid ||
1229 process_pure_responses(adapter, e)) {
Stephen Hemminger86c27d22006-11-17 17:00:34 -08001230 if (likely(__netif_rx_schedule_prep(sge->netdev)))
Scott Bardone559fb512005-06-23 01:40:19 -04001231 __netif_rx_schedule(sge->netdev);
Stephen Hemminger86c27d22006-11-17 17:00:34 -08001232 else if (net_ratelimit())
1233 printk(KERN_INFO
Scott Bardone559fb512005-06-23 01:40:19 -04001234 "NAPI schedule failure!\n");
1235 } else
1236 writel(q->cidx, adapter->regs + A_SG_SLEEPING);
1237 handled = 1;
1238 goto unlock;
1239 } else
1240 writel(q->cidx, adapter->regs + A_SG_SLEEPING);
1241 } else
1242 if (readl(adapter->regs + A_PL_CAUSE) & F_PL_INTR_SGE_DATA)
1243 printk(KERN_ERR "data interrupt while NAPI running\n");
1244
1245 handled = t1_slow_intr_handler(adapter);
1246 if (!handled)
1247 sge->stats.unhandled_irqs++;
1248 unlock:
1249 spin_unlock(&adapter->async_lock);
1250 return IRQ_RETVAL(handled != 0);
1251}
1252
1253/*
1254 * Main interrupt handler, optimized assuming that we took a 'DATA'
1255 * interrupt.
1256 *
1257 * 1. Clear the interrupt
1258 * 2. Loop while we find valid descriptors and process them; accumulate
1259 * information that can be processed after the loop
1260 * 3. Tell the SGE at which index we stopped processing descriptors
1261 * 4. Bookkeeping; free TX buffers, ring doorbell if there are any
1262 * outstanding TX buffers waiting, replenish RX buffers, potentially
1263 * reenable upper layers if they were turned off due to lack of TX
1264 * resources which are available again.
1265 * 5. If we took an interrupt, but no valid respQ descriptors was found we
1266 * let the slow_intr_handler run and do error handling.
1267 */
David Howells7d12e782006-10-05 14:55:46 +01001268static irqreturn_t t1_interrupt(int irq, void *cookie)
Scott Bardone559fb512005-06-23 01:40:19 -04001269{
1270 int work_done;
1271 struct respQ_e *e;
1272 struct adapter *adapter = cookie;
1273 struct respQ *Q = &adapter->sge->respQ;
1274
1275 spin_lock(&adapter->async_lock);
1276 e = &Q->entries[Q->cidx];
1277 prefetch(e);
1278
1279 writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
1280
1281 if (likely(e->GenerationBit == Q->genbit))
1282 work_done = process_responses(adapter, -1);
1283 else
1284 work_done = t1_slow_intr_handler(adapter);
1285
1286 /*
1287 * The unconditional clearing of the PL_CAUSE above may have raced
1288 * with DMA completion and the corresponding generation of a response
1289 * to cause us to miss the resulting data interrupt. The next write
1290 * is also unconditional to recover the missed interrupt and render
1291 * this race harmless.
1292 */
1293 writel(Q->cidx, adapter->regs + A_SG_SLEEPING);
1294
1295 if (!work_done)
1296 adapter->sge->stats.unhandled_irqs++;
1297 spin_unlock(&adapter->async_lock);
1298 return IRQ_RETVAL(work_done != 0);
1299}
1300
David Howells7d12e782006-10-05 14:55:46 +01001301irq_handler_t t1_select_intr_handler(adapter_t *adapter)
Scott Bardone559fb512005-06-23 01:40:19 -04001302{
1303 return adapter->params.sge.polling ? t1_interrupt_napi : t1_interrupt;
1304}
1305
1306/*
1307 * Enqueues the sk_buff onto the cmdQ[qid] and has hardware fetch it.
1308 *
1309 * The code figures out how many entries the sk_buff will require in the
1310 * cmdQ and updates the cmdQ data structure with the state once the enqueue
1311 * has complete. Then, it doesn't access the global structure anymore, but
1312 * uses the corresponding fields on the stack. In conjuction with a spinlock
1313 * around that code, we can make the function reentrant without holding the
1314 * lock when we actually enqueue (which might be expensive, especially on
1315 * architectures with IO MMUs).
1316 *
1317 * This runs with softirqs disabled.
1318 */
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001319static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
1320 unsigned int qid, struct net_device *dev)
Scott Bardone559fb512005-06-23 01:40:19 -04001321{
1322 struct sge *sge = adapter->sge;
1323 struct cmdQ *q = &sge->cmdQ[qid];
1324 unsigned int credits, pidx, genbit, count;
1325
1326 spin_lock(&q->lock);
1327 reclaim_completed_tx(sge, q);
1328
1329 pidx = q->pidx;
1330 credits = q->size - q->in_use;
1331 count = 1 + skb_shinfo(skb)->nr_frags;
1332
1333 { /* Ethernet packet */
1334 if (unlikely(credits < count)) {
1335 netif_stop_queue(dev);
1336 set_bit(dev->if_port, &sge->stopped_tx_queues);
Scott Bardone232a3472006-03-16 19:20:40 -05001337 sge->stats.cmdQ_full[2]++;
Scott Bardone559fb512005-06-23 01:40:19 -04001338 spin_unlock(&q->lock);
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001339 if (!netif_queue_stopped(dev))
1340 CH_ERR("%s: Tx ring full while queue awake!\n",
1341 adapter->name);
1342 return NETDEV_TX_BUSY;
Scott Bardone559fb512005-06-23 01:40:19 -04001343 }
1344 if (unlikely(credits - count < q->stop_thres)) {
Scott Bardone232a3472006-03-16 19:20:40 -05001345 sge->stats.cmdQ_full[2]++;
Scott Bardone559fb512005-06-23 01:40:19 -04001346 netif_stop_queue(dev);
1347 set_bit(dev->if_port, &sge->stopped_tx_queues);
1348 }
1349 }
1350 q->in_use += count;
1351 genbit = q->genbit;
1352 q->pidx += count;
1353 if (q->pidx >= q->size) {
1354 q->pidx -= q->size;
1355 q->genbit ^= 1;
1356 }
1357 spin_unlock(&q->lock);
1358
1359 write_tx_descs(adapter, skb, pidx, genbit, q);
1360
1361 /*
1362 * We always ring the doorbell for cmdQ1. For cmdQ0, we only ring
1363 * the doorbell if the Q is asleep. There is a natural race, where
1364 * the hardware is going to sleep just after we checked, however,
1365 * then the interrupt handler will detect the outstanding TX packet
1366 * and ring the doorbell for us.
1367 */
1368 if (qid)
1369 doorbell_pio(adapter, F_CMDQ1_ENABLE);
1370 else {
1371 clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1372 if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
1373 set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1374 writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1375 }
1376 }
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001377 return NETDEV_TX_OK;
Scott Bardone559fb512005-06-23 01:40:19 -04001378}
1379
1380#define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))
1381
1382/*
1383 * eth_hdr_len - return the length of an Ethernet header
1384 * @data: pointer to the start of the Ethernet header
1385 *
1386 * Returns the length of an Ethernet header, including optional VLAN tag.
1387 */
1388static inline int eth_hdr_len(const void *data)
1389{
1390 const struct ethhdr *e = data;
1391
1392 return e->h_proto == htons(ETH_P_8021Q) ? VLAN_ETH_HLEN : ETH_HLEN;
1393}
1394
1395/*
1396 * Adds the CPL header to the sk_buff and passes it to t1_sge_tx.
1397 */
1398int t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
1399{
1400 struct adapter *adapter = dev->priv;
1401 struct sge_port_stats *st = &adapter->sge->port_stats[dev->if_port];
1402 struct sge *sge = adapter->sge;
1403 struct cpl_tx_pkt *cpl;
1404
1405#ifdef NETIF_F_TSO
Herbert Xu89114af2006-07-08 13:34:32 -07001406 if (skb_is_gso(skb)) {
Scott Bardone559fb512005-06-23 01:40:19 -04001407 int eth_type;
1408 struct cpl_tx_pkt_lso *hdr;
1409
1410 st->tso++;
1411
1412 eth_type = skb->nh.raw - skb->data == ETH_HLEN ?
1413 CPL_ETH_II : CPL_ETH_II_VLAN;
1414
1415 hdr = (struct cpl_tx_pkt_lso *)skb_push(skb, sizeof(*hdr));
1416 hdr->opcode = CPL_TX_PKT_LSO;
1417 hdr->ip_csum_dis = hdr->l4_csum_dis = 0;
1418 hdr->ip_hdr_words = skb->nh.iph->ihl;
1419 hdr->tcp_hdr_words = skb->h.th->doff;
1420 hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
Herbert Xu79671682006-06-22 02:40:14 -07001421 skb_shinfo(skb)->gso_size));
Scott Bardone559fb512005-06-23 01:40:19 -04001422 hdr->len = htonl(skb->len - sizeof(*hdr));
1423 cpl = (struct cpl_tx_pkt *)hdr;
1424 sge->stats.tx_lso_pkts++;
1425 } else
1426#endif
1427 {
1428 /*
1429 * Packets shorter than ETH_HLEN can break the MAC, drop them
1430 * early. Also, we may get oversized packets because some
1431 * parts of the kernel don't handle our unusual hard_header_len
1432 * right, drop those too.
1433 */
1434 if (unlikely(skb->len < ETH_HLEN ||
1435 skb->len > dev->mtu + eth_hdr_len(skb->data))) {
1436 dev_kfree_skb_any(skb);
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001437 return NETDEV_TX_OK;
Scott Bardone559fb512005-06-23 01:40:19 -04001438 }
1439
1440 /*
1441 * We are using a non-standard hard_header_len and some kernel
1442 * components, such as pktgen, do not handle it right.
1443 * Complain when this happens but try to fix things up.
1444 */
1445 if (unlikely(skb_headroom(skb) <
1446 dev->hard_header_len - ETH_HLEN)) {
1447 struct sk_buff *orig_skb = skb;
1448
1449 if (net_ratelimit())
1450 printk(KERN_ERR "%s: inadequate headroom in "
1451 "Tx packet\n", dev->name);
1452 skb = skb_realloc_headroom(skb, sizeof(*cpl));
1453 dev_kfree_skb_any(orig_skb);
1454 if (!skb)
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001455 return NETDEV_TX_OK;
Scott Bardone559fb512005-06-23 01:40:19 -04001456 }
1457
1458 if (!(adapter->flags & UDP_CSUM_CAPABLE) &&
Patrick McHardy84fa7932006-08-29 16:44:56 -07001459 skb->ip_summed == CHECKSUM_PARTIAL &&
Scott Bardone559fb512005-06-23 01:40:19 -04001460 skb->nh.iph->protocol == IPPROTO_UDP)
Patrick McHardy84fa7932006-08-29 16:44:56 -07001461 if (unlikely(skb_checksum_help(skb))) {
Scott Bardone559fb512005-06-23 01:40:19 -04001462 dev_kfree_skb_any(skb);
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001463 return NETDEV_TX_OK;
Scott Bardone559fb512005-06-23 01:40:19 -04001464 }
1465
1466 /* Hmmm, assuming to catch the gratious arp... and we'll use
1467 * it to flush out stuck espi packets...
1468 */
1469 if (unlikely(!adapter->sge->espibug_skb)) {
1470 if (skb->protocol == htons(ETH_P_ARP) &&
1471 skb->nh.arph->ar_op == htons(ARPOP_REQUEST)) {
1472 adapter->sge->espibug_skb = skb;
1473 /* We want to re-use this skb later. We
1474 * simply bump the reference count and it
1475 * will not be freed...
1476 */
1477 skb = skb_get(skb);
1478 }
1479 }
1480
1481 cpl = (struct cpl_tx_pkt *)__skb_push(skb, sizeof(*cpl));
1482 cpl->opcode = CPL_TX_PKT;
1483 cpl->ip_csum_dis = 1; /* SW calculates IP csum */
Patrick McHardy84fa7932006-08-29 16:44:56 -07001484 cpl->l4_csum_dis = skb->ip_summed == CHECKSUM_PARTIAL ? 0 : 1;
Scott Bardone559fb512005-06-23 01:40:19 -04001485 /* the length field isn't used so don't bother setting it */
1486
Patrick McHardy84fa7932006-08-29 16:44:56 -07001487 st->tx_cso += (skb->ip_summed == CHECKSUM_PARTIAL);
1488 sge->stats.tx_do_cksum += (skb->ip_summed == CHECKSUM_PARTIAL);
Scott Bardone559fb512005-06-23 01:40:19 -04001489 sge->stats.tx_reg_pkts++;
1490 }
1491 cpl->iff = dev->if_port;
1492
1493#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
1494 if (adapter->vlan_grp && vlan_tx_tag_present(skb)) {
1495 cpl->vlan_valid = 1;
1496 cpl->vlan = htons(vlan_tx_tag_get(skb));
1497 st->vlan_insert++;
1498 } else
1499#endif
1500 cpl->vlan_valid = 0;
1501
1502 dev->trans_start = jiffies;
1503 return t1_sge_tx(skb, adapter, 0, dev);
1504}
1505
1506/*
1507 * Callback for the Tx buffer reclaim timer. Runs with softirqs disabled.
1508 */
1509static void sge_tx_reclaim_cb(unsigned long data)
1510{
1511 int i;
1512 struct sge *sge = (struct sge *)data;
1513
1514 for (i = 0; i < SGE_CMDQ_N; ++i) {
1515 struct cmdQ *q = &sge->cmdQ[i];
1516
1517 if (!spin_trylock(&q->lock))
1518 continue;
1519
1520 reclaim_completed_tx(sge, q);
1521 if (i == 0 && q->in_use) /* flush pending credits */
1522 writel(F_CMDQ0_ENABLE,
1523 sge->adapter->regs + A_SG_DOORBELL);
1524
1525 spin_unlock(&q->lock);
1526 }
1527 mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
1528}
1529
1530/*
1531 * Propagate changes of the SGE coalescing parameters to the HW.
1532 */
1533int t1_sge_set_coalesce_params(struct sge *sge, struct sge_params *p)
1534{
1535 sge->netdev->poll = t1_poll;
1536 sge->fixed_intrtimer = p->rx_coalesce_usecs *
1537 core_ticks_per_usec(sge->adapter);
1538 writel(sge->fixed_intrtimer, sge->adapter->regs + A_SG_INTRTIMER);
1539 return 0;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001540}
1541
1542/*
1543 * Allocates both RX and TX resources and configures the SGE. However,
1544 * the hardware is not enabled yet.
1545 */
1546int t1_sge_configure(struct sge *sge, struct sge_params *p)
1547{
1548 if (alloc_rx_resources(sge, p))
1549 return -ENOMEM;
1550 if (alloc_tx_resources(sge, p)) {
1551 free_rx_resources(sge);
1552 return -ENOMEM;
1553 }
1554 configure_sge(sge, p);
1555
1556 /*
1557 * Now that we have sized the free lists calculate the payload
1558 * capacity of the large buffers. Other parts of the driver use
1559 * this to set the max offload coalescing size so that RX packets
1560 * do not overflow our large buffers.
1561 */
1562 p->large_buf_capacity = jumbo_payload_capacity(sge);
1563 return 0;
1564}
1565
1566/*
Scott Bardone559fb512005-06-23 01:40:19 -04001567 * Disables the DMA engine.
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001568 */
Scott Bardone559fb512005-06-23 01:40:19 -04001569void t1_sge_stop(struct sge *sge)
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001570{
Scott Bardone559fb512005-06-23 01:40:19 -04001571 writel(0, sge->adapter->regs + A_SG_CONTROL);
1572 (void) readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
1573 if (is_T2(sge->adapter))
1574 del_timer_sync(&sge->espibug_timer);
1575 del_timer_sync(&sge->tx_reclaim_timer);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001576}
1577
1578/*
Scott Bardone559fb512005-06-23 01:40:19 -04001579 * Enables the DMA engine.
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001580 */
Scott Bardone559fb512005-06-23 01:40:19 -04001581void t1_sge_start(struct sge *sge)
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001582{
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001583 refill_free_list(sge, &sge->freelQ[0]);
1584 refill_free_list(sge, &sge->freelQ[1]);
1585
Scott Bardone559fb512005-06-23 01:40:19 -04001586 writel(sge->sge_control, sge->adapter->regs + A_SG_CONTROL);
1587 doorbell_pio(sge->adapter, F_FL0_ENABLE | F_FL1_ENABLE);
1588 (void) readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001589
Scott Bardone559fb512005-06-23 01:40:19 -04001590 mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001591
Scott Bardone559fb512005-06-23 01:40:19 -04001592 if (is_T2(sge->adapter))
1593 mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001594}
1595
1596/*
Scott Bardone559fb512005-06-23 01:40:19 -04001597 * Callback for the T2 ESPI 'stuck packet feature' workaorund
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001598 */
Scott Bardone559fb512005-06-23 01:40:19 -04001599static void espibug_workaround(void *data)
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001600{
Scott Bardone559fb512005-06-23 01:40:19 -04001601 struct adapter *adapter = (struct adapter *)data;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001602 struct sge *sge = adapter->sge;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001603
Scott Bardone559fb512005-06-23 01:40:19 -04001604 if (netif_running(adapter->port[0].dev)) {
1605 struct sk_buff *skb = sge->espibug_skb;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001606
Scott Bardone559fb512005-06-23 01:40:19 -04001607 u32 seop = t1_espi_get_mon(adapter, 0x930, 0);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001608
Scott Bardone559fb512005-06-23 01:40:19 -04001609 if ((seop & 0xfff0fff) == 0xfff && skb) {
1610 if (!skb->cb[0]) {
1611 u8 ch_mac_addr[ETH_ALEN] =
1612 {0x0, 0x7, 0x43, 0x0, 0x0, 0x0};
1613 memcpy(skb->data + sizeof(struct cpl_tx_pkt),
1614 ch_mac_addr, ETH_ALEN);
1615 memcpy(skb->data + skb->len - 10, ch_mac_addr,
1616 ETH_ALEN);
1617 skb->cb[0] = 0xff;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001618 }
1619
Scott Bardone559fb512005-06-23 01:40:19 -04001620 /* bump the reference count to avoid freeing of the
1621 * skb once the DMA has completed.
1622 */
1623 skb = skb_get(skb);
1624 t1_sge_tx(skb, adapter, 0, adapter->port[0].dev);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001625 }
1626 }
Scott Bardone559fb512005-06-23 01:40:19 -04001627 mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001628}
1629
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001630/*
Scott Bardone559fb512005-06-23 01:40:19 -04001631 * Creates a t1_sge structure and returns suggested resource parameters.
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001632 */
Scott Bardone559fb512005-06-23 01:40:19 -04001633struct sge * __devinit t1_sge_create(struct adapter *adapter,
1634 struct sge_params *p)
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001635{
Stephen Hemmingercbee9f92006-11-17 17:01:52 -08001636 struct sge *sge = kzalloc(sizeof(*sge), GFP_KERNEL);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001637
Scott Bardone559fb512005-06-23 01:40:19 -04001638 if (!sge)
1639 return NULL;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001640
Scott Bardone559fb512005-06-23 01:40:19 -04001641 sge->adapter = adapter;
1642 sge->netdev = adapter->port[0].dev;
1643 sge->rx_pkt_pad = t1_is_T1B(adapter) ? 0 : 2;
1644 sge->jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
1645
1646 init_timer(&sge->tx_reclaim_timer);
1647 sge->tx_reclaim_timer.data = (unsigned long)sge;
1648 sge->tx_reclaim_timer.function = sge_tx_reclaim_cb;
1649
1650 if (is_T2(sge->adapter)) {
1651 init_timer(&sge->espibug_timer);
1652 sge->espibug_timer.function = (void *)&espibug_workaround;
1653 sge->espibug_timer.data = (unsigned long)sge->adapter;
1654 sge->espibug_timeout = 1;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001655 }
Scott Bardone559fb512005-06-23 01:40:19 -04001656
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001657
Scott Bardone559fb512005-06-23 01:40:19 -04001658 p->cmdQ_size[0] = SGE_CMDQ0_E_N;
1659 p->cmdQ_size[1] = SGE_CMDQ1_E_N;
1660 p->freelQ_size[!sge->jumbo_fl] = SGE_FREEL_SIZE;
1661 p->freelQ_size[sge->jumbo_fl] = SGE_JUMBO_FREEL_SIZE;
1662 p->rx_coalesce_usecs = 50;
1663 p->coalesce_enable = 0;
1664 p->sample_interval_usecs = 0;
1665 p->polling = 0;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001666
Scott Bardone559fb512005-06-23 01:40:19 -04001667 return sge;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08001668}