blob: 26df2049d84964638740ff3613abddc4efd6a5cc [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>
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -080045#include <linux/ktime.h>
Christoph Lameter8199d3a2005-03-30 13:34:31 -080046#include <linux/netdevice.h>
47#include <linux/etherdevice.h>
48#include <linux/if_vlan.h>
49#include <linux/skbuff.h>
50#include <linux/init.h>
51#include <linux/mm.h>
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -080052#include <linux/tcp.h>
Christoph Lameter8199d3a2005-03-30 13:34:31 -080053#include <linux/ip.h>
54#include <linux/in.h>
55#include <linux/if_arp.h>
56
57#include "cpl5_cmd.h"
58#include "sge.h"
59#include "regs.h"
60#include "espi.h"
61
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -080062/* This belongs in if_ether.h */
63#define ETH_P_CPL5 0xf
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
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -080076#define SGE_TX_DESC_MAX_PLEN 16384
Christoph Lameter8199d3a2005-03-30 13:34:31 -080077
Scott Bardone559fb512005-06-23 01:40:19 -040078# define SGE_RX_DROP_THRES 2
Christoph Lameter8199d3a2005-03-30 13:34:31 -080079
Scott Bardone559fb512005-06-23 01:40:19 -040080#define SGE_RESPQ_REPLENISH_THRES (SGE_RESPQ_E_N / 4)
Christoph Lameter8199d3a2005-03-30 13:34:31 -080081
82/*
Scott Bardone559fb512005-06-23 01:40:19 -040083 * Period of the TX buffer reclaim timer. This timer does not need to run
84 * frequently as TX buffers are usually reclaimed by new TX packets.
85 */
86#define TX_RECLAIM_PERIOD (HZ / 4)
87
88#ifndef NET_IP_ALIGN
89# define NET_IP_ALIGN 2
90#endif
91
92#define M_CMD_LEN 0x7fffffff
93#define V_CMD_LEN(v) (v)
94#define G_CMD_LEN(v) ((v) & M_CMD_LEN)
95#define V_CMD_GEN1(v) ((v) << 31)
96#define V_CMD_GEN2(v) (v)
97#define F_CMD_DATAVALID (1 << 1)
98#define F_CMD_SOP (1 << 2)
99#define V_CMD_EOP(v) ((v) << 3)
100
101/*
102 * Command queue, receive buffer list, and response queue descriptors.
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800103 */
104#if defined(__BIG_ENDIAN_BITFIELD)
105struct cmdQ_e {
Scott Bardone559fb512005-06-23 01:40:19 -0400106 u32 addr_lo;
107 u32 len_gen;
108 u32 flags;
109 u32 addr_hi;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800110};
111
112struct freelQ_e {
Scott Bardone559fb512005-06-23 01:40:19 -0400113 u32 addr_lo;
114 u32 len_gen;
115 u32 gen2;
116 u32 addr_hi;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800117};
118
119struct respQ_e {
120 u32 Qsleeping : 4;
121 u32 Cmdq1CreditReturn : 5;
122 u32 Cmdq1DmaComplete : 5;
123 u32 Cmdq0CreditReturn : 5;
124 u32 Cmdq0DmaComplete : 5;
125 u32 FreelistQid : 2;
126 u32 CreditValid : 1;
127 u32 DataValid : 1;
128 u32 Offload : 1;
129 u32 Eop : 1;
130 u32 Sop : 1;
131 u32 GenerationBit : 1;
132 u32 BufferLength;
133};
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800134#elif defined(__LITTLE_ENDIAN_BITFIELD)
135struct cmdQ_e {
Scott Bardone559fb512005-06-23 01:40:19 -0400136 u32 len_gen;
137 u32 addr_lo;
138 u32 addr_hi;
139 u32 flags;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800140};
141
142struct freelQ_e {
Scott Bardone559fb512005-06-23 01:40:19 -0400143 u32 len_gen;
144 u32 addr_lo;
145 u32 addr_hi;
146 u32 gen2;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800147};
148
149struct respQ_e {
150 u32 BufferLength;
151 u32 GenerationBit : 1;
152 u32 Sop : 1;
153 u32 Eop : 1;
154 u32 Offload : 1;
155 u32 DataValid : 1;
156 u32 CreditValid : 1;
157 u32 FreelistQid : 2;
158 u32 Cmdq0DmaComplete : 5;
159 u32 Cmdq0CreditReturn : 5;
160 u32 Cmdq1DmaComplete : 5;
161 u32 Cmdq1CreditReturn : 5;
162 u32 Qsleeping : 4;
163} ;
164#endif
165
166/*
167 * SW Context Command and Freelist Queue Descriptors
168 */
169struct cmdQ_ce {
170 struct sk_buff *skb;
171 DECLARE_PCI_UNMAP_ADDR(dma_addr);
172 DECLARE_PCI_UNMAP_LEN(dma_len);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800173};
174
175struct freelQ_ce {
176 struct sk_buff *skb;
177 DECLARE_PCI_UNMAP_ADDR(dma_addr);
178 DECLARE_PCI_UNMAP_LEN(dma_len);
179};
180
181/*
Scott Bardone559fb512005-06-23 01:40:19 -0400182 * SW command, freelist and response rings
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800183 */
184struct cmdQ {
Scott Bardone559fb512005-06-23 01:40:19 -0400185 unsigned long status; /* HW DMA fetch status */
186 unsigned int in_use; /* # of in-use command descriptors */
187 unsigned int size; /* # of descriptors */
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800188 unsigned int processed; /* total # of descs HW has processed */
189 unsigned int cleaned; /* total # of descs SW has reclaimed */
190 unsigned int stop_thres; /* SW TX queue suspend threshold */
Scott Bardone559fb512005-06-23 01:40:19 -0400191 u16 pidx; /* producer index (SW) */
192 u16 cidx; /* consumer index (HW) */
193 u8 genbit; /* current generation (=valid) bit */
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800194 u8 sop; /* is next entry start of packet? */
Scott Bardone559fb512005-06-23 01:40:19 -0400195 struct cmdQ_e *entries; /* HW command descriptor Q */
196 struct cmdQ_ce *centries; /* SW command context descriptor Q */
Scott Bardone559fb512005-06-23 01:40:19 -0400197 dma_addr_t dma_addr; /* DMA addr HW command descriptor Q */
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800198 spinlock_t lock; /* Lock to protect cmdQ enqueuing */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800199};
200
201struct freelQ {
Scott Bardone559fb512005-06-23 01:40:19 -0400202 unsigned int credits; /* # of available RX buffers */
203 unsigned int size; /* free list capacity */
204 u16 pidx; /* producer index (SW) */
205 u16 cidx; /* consumer index (HW) */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800206 u16 rx_buffer_size; /* Buffer size on this free list */
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800207 u16 dma_offset; /* DMA offset to align IP headers */
208 u16 recycleq_idx; /* skb recycle q to use */
Scott Bardone559fb512005-06-23 01:40:19 -0400209 u8 genbit; /* current generation (=valid) bit */
210 struct freelQ_e *entries; /* HW freelist descriptor Q */
211 struct freelQ_ce *centries; /* SW freelist context descriptor Q */
212 dma_addr_t dma_addr; /* DMA addr HW freelist descriptor Q */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800213};
214
215struct respQ {
Scott Bardone559fb512005-06-23 01:40:19 -0400216 unsigned int credits; /* credits to be returned to SGE */
217 unsigned int size; /* # of response Q descriptors */
218 u16 cidx; /* consumer index (SW) */
219 u8 genbit; /* current generation(=valid) bit */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800220 struct respQ_e *entries; /* HW response descriptor Q */
Scott Bardone559fb512005-06-23 01:40:19 -0400221 dma_addr_t dma_addr; /* DMA addr HW response descriptor Q */
222};
223
224/* Bit flags for cmdQ.status */
225enum {
226 CMDQ_STAT_RUNNING = 1, /* fetch engine is running */
227 CMDQ_STAT_LAST_PKT_DB = 2 /* last packet rung the doorbell */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800228};
229
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800230/* T204 TX SW scheduler */
231
232/* Per T204 TX port */
233struct sched_port {
234 unsigned int avail; /* available bits - quota */
235 unsigned int drain_bits_per_1024ns; /* drain rate */
236 unsigned int speed; /* drain rate, mbps */
237 unsigned int mtu; /* mtu size */
238 struct sk_buff_head skbq; /* pending skbs */
239};
240
241/* Per T204 device */
242struct sched {
243 ktime_t last_updated; /* last time quotas were computed */
244 unsigned int max_avail; /* max bits to be sent to any port */
245 unsigned int port; /* port index (round robin ports) */
246 unsigned int num; /* num skbs in per port queues */
247 struct sched_port p[MAX_NPORTS];
248 struct tasklet_struct sched_tsk;/* tasklet used to run scheduler */
249};
250static void restart_sched(unsigned long);
251
252
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800253/*
254 * Main SGE data structure
255 *
256 * Interrupts are handled by a single CPU and it is likely that on a MP system
257 * the application is migrated to another CPU. In that scenario, we try to
258 * seperate the RX(in irq context) and TX state in order to decrease memory
259 * contention.
260 */
261struct sge {
262 struct adapter *adapter; /* adapter backpointer */
Scott Bardone559fb512005-06-23 01:40:19 -0400263 struct net_device *netdev; /* netdevice backpointer */
264 struct freelQ freelQ[SGE_FREELQ_N]; /* buffer free lists */
265 struct respQ respQ; /* response Q */
266 unsigned long stopped_tx_queues; /* bitmap of suspended Tx queues */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800267 unsigned int rx_pkt_pad; /* RX padding for L2 packets */
268 unsigned int jumbo_fl; /* jumbo freelist Q index */
Scott Bardone559fb512005-06-23 01:40:19 -0400269 unsigned int intrtimer_nres; /* no-resource interrupt timer */
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800270 unsigned int fixed_intrtimer;/* non-adaptive interrupt timer */
Scott Bardone559fb512005-06-23 01:40:19 -0400271 struct timer_list tx_reclaim_timer; /* reclaims TX buffers */
272 struct timer_list espibug_timer;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800273 unsigned long espibug_timeout;
274 struct sk_buff *espibug_skb[MAX_NPORTS];
Scott Bardone559fb512005-06-23 01:40:19 -0400275 u32 sge_control; /* shadow value of sge control reg */
276 struct sge_intr_counts stats;
277 struct sge_port_stats port_stats[MAX_NPORTS];
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800278 struct sched *tx_sched;
Scott Bardone559fb512005-06-23 01:40:19 -0400279 struct cmdQ cmdQ[SGE_CMDQ_N] ____cacheline_aligned_in_smp;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800280};
281
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800282/*
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800283 * stop tasklet and free all pending skb's
284 */
285static void tx_sched_stop(struct sge *sge)
286{
287 struct sched *s = sge->tx_sched;
288 int i;
289
290 tasklet_kill(&s->sched_tsk);
291
292 for (i = 0; i < MAX_NPORTS; i++)
293 __skb_queue_purge(&s->p[s->port].skbq);
294}
295
296/*
297 * t1_sched_update_parms() is called when the MTU or link speed changes. It
298 * re-computes scheduler parameters to scope with the change.
299 */
300unsigned int t1_sched_update_parms(struct sge *sge, unsigned int port,
301 unsigned int mtu, unsigned int speed)
302{
303 struct sched *s = sge->tx_sched;
304 struct sched_port *p = &s->p[port];
305 unsigned int max_avail_segs;
306
307 pr_debug("t1_sched_update_params mtu=%d speed=%d\n", mtu, speed);
308 if (speed)
309 p->speed = speed;
310 if (mtu)
311 p->mtu = mtu;
312
313 if (speed || mtu) {
314 unsigned long long drain = 1024ULL * p->speed * (p->mtu - 40);
315 do_div(drain, (p->mtu + 50) * 1000);
316 p->drain_bits_per_1024ns = (unsigned int) drain;
317
318 if (p->speed < 1000)
319 p->drain_bits_per_1024ns =
320 90 * p->drain_bits_per_1024ns / 100;
321 }
322
323 if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204) {
324 p->drain_bits_per_1024ns -= 16;
325 s->max_avail = max(4096U, p->mtu + 16 + 14 + 4);
326 max_avail_segs = max(1U, 4096 / (p->mtu - 40));
327 } else {
328 s->max_avail = 16384;
329 max_avail_segs = max(1U, 9000 / (p->mtu - 40));
330 }
331
332 pr_debug("t1_sched_update_parms: mtu %u speed %u max_avail %u "
333 "max_avail_segs %u drain_bits_per_1024ns %u\n", p->mtu,
334 p->speed, s->max_avail, max_avail_segs,
335 p->drain_bits_per_1024ns);
336
337 return max_avail_segs * (p->mtu - 40);
338}
339
340/*
341 * t1_sched_max_avail_bytes() tells the scheduler the maximum amount of
342 * data that can be pushed per port.
343 */
344void t1_sched_set_max_avail_bytes(struct sge *sge, unsigned int val)
345{
346 struct sched *s = sge->tx_sched;
347 unsigned int i;
348
349 s->max_avail = val;
350 for (i = 0; i < MAX_NPORTS; i++)
351 t1_sched_update_parms(sge, i, 0, 0);
352}
353
354/*
355 * t1_sched_set_drain_bits_per_us() tells the scheduler at which rate a port
356 * is draining.
357 */
358void t1_sched_set_drain_bits_per_us(struct sge *sge, unsigned int port,
359 unsigned int val)
360{
361 struct sched *s = sge->tx_sched;
362 struct sched_port *p = &s->p[port];
363 p->drain_bits_per_1024ns = val * 1024 / 1000;
364 t1_sched_update_parms(sge, port, 0, 0);
365}
366
367
368/*
369 * get_clock() implements a ns clock (see ktime_get)
370 */
371static inline ktime_t get_clock(void)
372{
373 struct timespec ts;
374
375 ktime_get_ts(&ts);
376 return timespec_to_ktime(ts);
377}
378
379/*
380 * tx_sched_init() allocates resources and does basic initialization.
381 */
382static int tx_sched_init(struct sge *sge)
383{
384 struct sched *s;
385 int i;
386
387 s = kzalloc(sizeof (struct sched), GFP_KERNEL);
388 if (!s)
389 return -ENOMEM;
390
391 pr_debug("tx_sched_init\n");
392 tasklet_init(&s->sched_tsk, restart_sched, (unsigned long) sge);
393 sge->tx_sched = s;
394
395 for (i = 0; i < MAX_NPORTS; i++) {
396 skb_queue_head_init(&s->p[i].skbq);
397 t1_sched_update_parms(sge, i, 1500, 1000);
398 }
399
400 return 0;
401}
402
403/*
404 * sched_update_avail() computes the delta since the last time it was called
405 * and updates the per port quota (number of bits that can be sent to the any
406 * port).
407 */
408static inline int sched_update_avail(struct sge *sge)
409{
410 struct sched *s = sge->tx_sched;
411 ktime_t now = get_clock();
412 unsigned int i;
413 long long delta_time_ns;
414
415 delta_time_ns = ktime_to_ns(ktime_sub(now, s->last_updated));
416
417 pr_debug("sched_update_avail delta=%lld\n", delta_time_ns);
418 if (delta_time_ns < 15000)
419 return 0;
420
421 for (i = 0; i < MAX_NPORTS; i++) {
422 struct sched_port *p = &s->p[i];
423 unsigned int delta_avail;
424
425 delta_avail = (p->drain_bits_per_1024ns * delta_time_ns) >> 13;
426 p->avail = min(p->avail + delta_avail, s->max_avail);
427 }
428
429 s->last_updated = now;
430
431 return 1;
432}
433
434/*
435 * sched_skb() is called from two different places. In the tx path, any
436 * packet generating load on an output port will call sched_skb()
437 * (skb != NULL). In addition, sched_skb() is called from the irq/soft irq
438 * context (skb == NULL).
439 * The scheduler only returns a skb (which will then be sent) if the
440 * length of the skb is <= the current quota of the output port.
441 */
442static struct sk_buff *sched_skb(struct sge *sge, struct sk_buff *skb,
443 unsigned int credits)
444{
445 struct sched *s = sge->tx_sched;
446 struct sk_buff_head *skbq;
447 unsigned int i, len, update = 1;
448
449 pr_debug("sched_skb %p\n", skb);
450 if (!skb) {
451 if (!s->num)
452 return NULL;
453 } else {
454 skbq = &s->p[skb->dev->if_port].skbq;
455 __skb_queue_tail(skbq, skb);
456 s->num++;
457 skb = NULL;
458 }
459
460 if (credits < MAX_SKB_FRAGS + 1)
461 goto out;
462
463 again:
464 for (i = 0; i < MAX_NPORTS; i++) {
465 s->port = ++s->port & (MAX_NPORTS - 1);
466 skbq = &s->p[s->port].skbq;
467
468 skb = skb_peek(skbq);
469
470 if (!skb)
471 continue;
472
473 len = skb->len;
474 if (len <= s->p[s->port].avail) {
475 s->p[s->port].avail -= len;
476 s->num--;
477 __skb_unlink(skb, skbq);
478 goto out;
479 }
480 skb = NULL;
481 }
482
483 if (update-- && sched_update_avail(sge))
484 goto again;
485
486 out:
487 /* If there are more pending skbs, we use the hardware to schedule us
488 * again.
489 */
490 if (s->num && !skb) {
491 struct cmdQ *q = &sge->cmdQ[0];
492 clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
493 if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
494 set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
495 writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
496 }
497 }
498 pr_debug("sched_skb ret %p\n", skb);
499
500 return skb;
501}
502
503/*
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800504 * PIO to indicate that memory mapped Q contains valid descriptor(s).
505 */
Scott Bardone559fb512005-06-23 01:40:19 -0400506static inline void doorbell_pio(struct adapter *adapter, u32 val)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800507{
508 wmb();
Scott Bardone559fb512005-06-23 01:40:19 -0400509 writel(val, adapter->regs + A_SG_DOORBELL);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800510}
511
512/*
513 * Frees all RX buffers on the freelist Q. The caller must make sure that
514 * the SGE is turned off before calling this function.
515 */
Scott Bardone559fb512005-06-23 01:40:19 -0400516static void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *q)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800517{
Scott Bardone559fb512005-06-23 01:40:19 -0400518 unsigned int cidx = q->cidx;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800519
Scott Bardone559fb512005-06-23 01:40:19 -0400520 while (q->credits--) {
521 struct freelQ_ce *ce = &q->centries[cidx];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800522
523 pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
524 pci_unmap_len(ce, dma_len),
525 PCI_DMA_FROMDEVICE);
526 dev_kfree_skb(ce->skb);
527 ce->skb = NULL;
Scott Bardone559fb512005-06-23 01:40:19 -0400528 if (++cidx == q->size)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800529 cidx = 0;
530 }
531}
532
533/*
534 * Free RX free list and response queue resources.
535 */
536static void free_rx_resources(struct sge *sge)
537{
538 struct pci_dev *pdev = sge->adapter->pdev;
539 unsigned int size, i;
540
541 if (sge->respQ.entries) {
Scott Bardone559fb512005-06-23 01:40:19 -0400542 size = sizeof(struct respQ_e) * sge->respQ.size;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800543 pci_free_consistent(pdev, size, sge->respQ.entries,
544 sge->respQ.dma_addr);
545 }
546
547 for (i = 0; i < SGE_FREELQ_N; i++) {
Scott Bardone559fb512005-06-23 01:40:19 -0400548 struct freelQ *q = &sge->freelQ[i];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800549
Scott Bardone559fb512005-06-23 01:40:19 -0400550 if (q->centries) {
551 free_freelQ_buffers(pdev, q);
552 kfree(q->centries);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800553 }
Scott Bardone559fb512005-06-23 01:40:19 -0400554 if (q->entries) {
555 size = sizeof(struct freelQ_e) * q->size;
556 pci_free_consistent(pdev, size, q->entries,
557 q->dma_addr);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800558 }
559 }
560}
561
562/*
563 * Allocates basic RX resources, consisting of memory mapped freelist Qs and a
Scott Bardone559fb512005-06-23 01:40:19 -0400564 * response queue.
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800565 */
566static int alloc_rx_resources(struct sge *sge, struct sge_params *p)
567{
568 struct pci_dev *pdev = sge->adapter->pdev;
569 unsigned int size, i;
570
571 for (i = 0; i < SGE_FREELQ_N; i++) {
Scott Bardone559fb512005-06-23 01:40:19 -0400572 struct freelQ *q = &sge->freelQ[i];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800573
Scott Bardone559fb512005-06-23 01:40:19 -0400574 q->genbit = 1;
575 q->size = p->freelQ_size[i];
576 q->dma_offset = sge->rx_pkt_pad ? 0 : NET_IP_ALIGN;
577 size = sizeof(struct freelQ_e) * q->size;
578 q->entries = (struct freelQ_e *)
579 pci_alloc_consistent(pdev, size, &q->dma_addr);
580 if (!q->entries)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800581 goto err_no_mem;
Scott Bardone559fb512005-06-23 01:40:19 -0400582 memset(q->entries, 0, size);
583 size = sizeof(struct freelQ_ce) * q->size;
Stephen Hemmingercbee9f92006-11-17 17:01:52 -0800584 q->centries = kzalloc(size, GFP_KERNEL);
Scott Bardone559fb512005-06-23 01:40:19 -0400585 if (!q->centries)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800586 goto err_no_mem;
587 }
588
589 /*
590 * Calculate the buffer sizes for the two free lists. FL0 accommodates
591 * regular sized Ethernet frames, FL1 is sized not to exceed 16K,
592 * including all the sk_buff overhead.
593 *
594 * Note: For T2 FL0 and FL1 are reversed.
595 */
596 sge->freelQ[!sge->jumbo_fl].rx_buffer_size = SGE_RX_SM_BUF_SIZE +
597 sizeof(struct cpl_rx_data) +
598 sge->freelQ[!sge->jumbo_fl].dma_offset;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800599
600 size = (16 * 1024) -
601 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
602
603 sge->freelQ[sge->jumbo_fl].rx_buffer_size = size;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800604
Scott Bardone559fb512005-06-23 01:40:19 -0400605 /*
606 * Setup which skb recycle Q should be used when recycling buffers from
607 * each free list.
608 */
609 sge->freelQ[!sge->jumbo_fl].recycleq_idx = 0;
610 sge->freelQ[sge->jumbo_fl].recycleq_idx = 1;
611
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800612 sge->respQ.genbit = 1;
Scott Bardone559fb512005-06-23 01:40:19 -0400613 sge->respQ.size = SGE_RESPQ_E_N;
614 sge->respQ.credits = 0;
615 size = sizeof(struct respQ_e) * sge->respQ.size;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800616 sge->respQ.entries = (struct respQ_e *)
617 pci_alloc_consistent(pdev, size, &sge->respQ.dma_addr);
618 if (!sge->respQ.entries)
619 goto err_no_mem;
620 memset(sge->respQ.entries, 0, size);
621 return 0;
622
623err_no_mem:
624 free_rx_resources(sge);
625 return -ENOMEM;
626}
627
628/*
Scott Bardone559fb512005-06-23 01:40:19 -0400629 * Reclaims n TX descriptors and frees the buffers associated with them.
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800630 */
Scott Bardone559fb512005-06-23 01:40:19 -0400631static void free_cmdQ_buffers(struct sge *sge, struct cmdQ *q, unsigned int n)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800632{
Scott Bardone559fb512005-06-23 01:40:19 -0400633 struct cmdQ_ce *ce;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800634 struct pci_dev *pdev = sge->adapter->pdev;
Scott Bardone559fb512005-06-23 01:40:19 -0400635 unsigned int cidx = q->cidx;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800636
Scott Bardone559fb512005-06-23 01:40:19 -0400637 q->in_use -= n;
638 ce = &q->centries[cidx];
639 while (n--) {
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800640 if (q->sop) {
641 if (likely(pci_unmap_len(ce, dma_len))) {
642 pci_unmap_single(pdev,
643 pci_unmap_addr(ce, dma_addr),
644 pci_unmap_len(ce, dma_len),
645 PCI_DMA_TODEVICE);
646 q->sop = 0;
647 }
648 } else {
649 if (likely(pci_unmap_len(ce, dma_len))) {
650 pci_unmap_page(pdev, pci_unmap_addr(ce, dma_addr),
651 pci_unmap_len(ce, dma_len),
652 PCI_DMA_TODEVICE);
653 }
654 }
Scott Bardone559fb512005-06-23 01:40:19 -0400655 if (ce->skb) {
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800656 dev_kfree_skb_any(ce->skb);
Scott Bardone559fb512005-06-23 01:40:19 -0400657 q->sop = 1;
658 }
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800659 ce++;
Scott Bardone559fb512005-06-23 01:40:19 -0400660 if (++cidx == q->size) {
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800661 cidx = 0;
Scott Bardone559fb512005-06-23 01:40:19 -0400662 ce = q->centries;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800663 }
664 }
Scott Bardone559fb512005-06-23 01:40:19 -0400665 q->cidx = cidx;
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800666}
667
668/*
669 * Free TX resources.
670 *
671 * Assumes that SGE is stopped and all interrupts are disabled.
672 */
673static void free_tx_resources(struct sge *sge)
674{
675 struct pci_dev *pdev = sge->adapter->pdev;
676 unsigned int size, i;
677
678 for (i = 0; i < SGE_CMDQ_N; i++) {
Scott Bardone559fb512005-06-23 01:40:19 -0400679 struct cmdQ *q = &sge->cmdQ[i];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800680
Scott Bardone559fb512005-06-23 01:40:19 -0400681 if (q->centries) {
682 if (q->in_use)
683 free_cmdQ_buffers(sge, q, q->in_use);
684 kfree(q->centries);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800685 }
Scott Bardone559fb512005-06-23 01:40:19 -0400686 if (q->entries) {
687 size = sizeof(struct cmdQ_e) * q->size;
688 pci_free_consistent(pdev, size, q->entries,
689 q->dma_addr);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800690 }
691 }
692}
693
694/*
695 * Allocates basic TX resources, consisting of memory mapped command Qs.
696 */
697static int alloc_tx_resources(struct sge *sge, struct sge_params *p)
698{
699 struct pci_dev *pdev = sge->adapter->pdev;
700 unsigned int size, i;
701
702 for (i = 0; i < SGE_CMDQ_N; i++) {
Scott Bardone559fb512005-06-23 01:40:19 -0400703 struct cmdQ *q = &sge->cmdQ[i];
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800704
Scott Bardone559fb512005-06-23 01:40:19 -0400705 q->genbit = 1;
706 q->sop = 1;
707 q->size = p->cmdQ_size[i];
708 q->in_use = 0;
709 q->status = 0;
710 q->processed = q->cleaned = 0;
711 q->stop_thres = 0;
712 spin_lock_init(&q->lock);
713 size = sizeof(struct cmdQ_e) * q->size;
714 q->entries = (struct cmdQ_e *)
715 pci_alloc_consistent(pdev, size, &q->dma_addr);
716 if (!q->entries)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800717 goto err_no_mem;
Scott Bardone559fb512005-06-23 01:40:19 -0400718 memset(q->entries, 0, size);
719 size = sizeof(struct cmdQ_ce) * q->size;
Stephen Hemmingercbee9f92006-11-17 17:01:52 -0800720 q->centries = kzalloc(size, GFP_KERNEL);
Scott Bardone559fb512005-06-23 01:40:19 -0400721 if (!q->centries)
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800722 goto err_no_mem;
723 }
724
Scott Bardone559fb512005-06-23 01:40:19 -0400725 /*
726 * CommandQ 0 handles Ethernet and TOE packets, while queue 1 is TOE
727 * only. For queue 0 set the stop threshold so we can handle one more
728 * packet from each port, plus reserve an additional 24 entries for
729 * Ethernet packets only. Queue 1 never suspends nor do we reserve
730 * space for Ethernet packets.
731 */
732 sge->cmdQ[0].stop_thres = sge->adapter->params.nports *
733 (MAX_SKB_FRAGS + 1);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800734 return 0;
735
736err_no_mem:
737 free_tx_resources(sge);
738 return -ENOMEM;
739}
740
741static inline void setup_ring_params(struct adapter *adapter, u64 addr,
742 u32 size, int base_reg_lo,
743 int base_reg_hi, int size_reg)
744{
Scott Bardone559fb512005-06-23 01:40:19 -0400745 writel((u32)addr, adapter->regs + base_reg_lo);
746 writel(addr >> 32, adapter->regs + base_reg_hi);
747 writel(size, adapter->regs + size_reg);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800748}
749
750/*
751 * Enable/disable VLAN acceleration.
752 */
753void t1_set_vlan_accel(struct adapter *adapter, int on_off)
754{
755 struct sge *sge = adapter->sge;
756
757 sge->sge_control &= ~F_VLAN_XTRACT;
758 if (on_off)
759 sge->sge_control |= F_VLAN_XTRACT;
760 if (adapter->open_device_map) {
Scott Bardone559fb512005-06-23 01:40:19 -0400761 writel(sge->sge_control, adapter->regs + A_SG_CONTROL);
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800762 readl(adapter->regs + A_SG_CONTROL); /* flush */
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800763 }
764}
765
766/*
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800767 * Programs the various SGE registers. However, the engine is not yet enabled,
768 * but sge->sge_control is setup and ready to go.
769 */
770static void configure_sge(struct sge *sge, struct sge_params *p)
771{
772 struct adapter *ap = sge->adapter;
Scott Bardone559fb512005-06-23 01:40:19 -0400773
774 writel(0, ap->regs + A_SG_CONTROL);
775 setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800776 A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
Scott Bardone559fb512005-06-23 01:40:19 -0400777 setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800778 A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
779 setup_ring_params(ap, sge->freelQ[0].dma_addr,
Scott Bardone559fb512005-06-23 01:40:19 -0400780 sge->freelQ[0].size, A_SG_FL0BASELWR,
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800781 A_SG_FL0BASEUPR, A_SG_FL0SIZE);
782 setup_ring_params(ap, sge->freelQ[1].dma_addr,
Scott Bardone559fb512005-06-23 01:40:19 -0400783 sge->freelQ[1].size, A_SG_FL1BASELWR,
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800784 A_SG_FL1BASEUPR, A_SG_FL1SIZE);
785
786 /* The threshold comparison uses <. */
Scott Bardone559fb512005-06-23 01:40:19 -0400787 writel(SGE_RX_SM_BUF_SIZE + 1, ap->regs + A_SG_FLTHRESHOLD);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800788
Scott Bardone559fb512005-06-23 01:40:19 -0400789 setup_ring_params(ap, sge->respQ.dma_addr, sge->respQ.size,
790 A_SG_RSPBASELWR, A_SG_RSPBASEUPR, A_SG_RSPSIZE);
791 writel((u32)sge->respQ.size - 1, ap->regs + A_SG_RSPQUEUECREDIT);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800792
793 sge->sge_control = F_CMDQ0_ENABLE | F_CMDQ1_ENABLE | F_FL0_ENABLE |
794 F_FL1_ENABLE | F_CPL_ENABLE | F_RESPONSE_QUEUE_ENABLE |
795 V_CMDQ_PRIORITY(2) | F_DISABLE_CMDQ1_GTS | F_ISCSI_COALESCE |
796 V_RX_PKT_OFFSET(sge->rx_pkt_pad);
797
798#if defined(__BIG_ENDIAN_BITFIELD)
799 sge->sge_control |= F_ENABLE_BIG_ENDIAN;
800#endif
801
Scott Bardone559fb512005-06-23 01:40:19 -0400802 /* Initialize no-resource timer */
803 sge->intrtimer_nres = SGE_INTRTIMER_NRES * core_ticks_per_usec(ap);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800804
Scott Bardone559fb512005-06-23 01:40:19 -0400805 t1_sge_set_coalesce_params(sge, p);
Christoph Lameter8199d3a2005-03-30 13:34:31 -0800806}
807
808/*
809 * Return the payload capacity of the jumbo free-list buffers.
810 */
811static inline unsigned int jumbo_payload_capacity(const struct sge *sge)
812{
813 return sge->freelQ[sge->jumbo_fl].rx_buffer_size -
Scott Bardone559fb512005-06-23 01:40:19 -0400814 sge->freelQ[sge->jumbo_fl].dma_offset -
815 sizeof(struct cpl_rx_data);
816}
817
818/*
819 * Frees all SGE related resources and the sge structure itself
820 */
821void t1_sge_destroy(struct sge *sge)
822{
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -0800823 kfree(sge->tx_sched);
Scott Bardone559fb512005-06-23 01:40:19 -0400824 free_tx_resources(sge);
825 free_rx_resources(sge);
826 kfree(sge);
827}
828
829/*
830 * Allocates new RX buffers on the freelist Q (and tracks them on the freelist
831 * context Q) until the Q is full or alloc_skb fails.
832 *
833 * It is possible that the generation bits already match, indicating that the
834 * buffer is already valid and nothing needs to be done. This happens when we
835 * copied a received buffer into a new sk_buff during the interrupt processing.
836 *
837 * If the SGE doesn't automatically align packets properly (!sge->rx_pkt_pad),
838 * we specify a RX_OFFSET in order to make sure that the IP header is 4B
839 * aligned.
840 */
841static void refill_free_list(struct sge *sge, struct freelQ *q)
842{
843 struct pci_dev *pdev = sge->adapter->pdev;
844 struct freelQ_ce *ce = &q->centries[q->pidx];
845 struct freelQ_e *e = &q->entries[q->pidx];
846 unsigned int dma_len = q->rx_buffer_size - q->dma_offset;
847
848
849 while (q->credits < q->size) {
850 struct sk_buff *skb;
851 dma_addr_t mapping;
852
853 skb = alloc_skb(q->rx_buffer_size, GFP_ATOMIC);
854 if (!skb)
855 break;
856
857 skb_reserve(skb, q->dma_offset);
858 mapping = pci_map_single(pdev, skb->data, dma_len,
859 PCI_DMA_FROMDEVICE);
860 ce->skb = skb;
861 pci_unmap_addr_set(ce, dma_addr, mapping);
862 pci_unmap_len_set(ce, dma_len, dma_len);
863 e->addr_lo = (u32)mapping;
864 e->addr_hi = (u64)mapping >> 32;
865 e->len_gen = V_CMD_LEN(dma_len) | V_CMD_GEN1(q->genbit);
866 wmb();
867 e->gen2 = V_CMD_GEN2(q->genbit);
868
869 e++;
870 ce++;
871 if (++q->pidx == q->size) {
872 q->pidx = 0;
873 q->genbit ^= 1;
874 ce = q->centries;
875 e = q->entries;
876 }
877 q->credits++;
878 }
879
880}
881
882/*
883 * Calls refill_free_list for both free lists. If we cannot fill at least 1/4
884 * of both rings, we go into 'few interrupt mode' in order to give the system
885 * time to free up resources.
886 */
887static void freelQs_empty(struct sge *sge)
888{
889 struct adapter *adapter = sge->adapter;
890 u32 irq_reg = readl(adapter->regs + A_SG_INT_ENABLE);
891 u32 irqholdoff_reg;
892
893 refill_free_list(sge, &sge->freelQ[0]);
894 refill_free_list(sge, &sge->freelQ[1]);
895
896 if (sge->freelQ[0].credits > (sge->freelQ[0].size >> 2) &&
897 sge->freelQ[1].credits > (sge->freelQ[1].size >> 2)) {
898 irq_reg |= F_FL_EXHAUSTED;
899 irqholdoff_reg = sge->fixed_intrtimer;
900 } else {
901 /* Clear the F_FL_EXHAUSTED interrupts for now */
902 irq_reg &= ~F_FL_EXHAUSTED;
903 irqholdoff_reg = sge->intrtimer_nres;
904 }
905 writel(irqholdoff_reg, adapter->regs + A_SG_INTRTIMER);
906 writel(irq_reg, adapter->regs + A_SG_INT_ENABLE);
907
908 /* We reenable the Qs to force a freelist GTS interrupt later */
909 doorbell_pio(adapter, F_FL0_ENABLE | F_FL1_ENABLE);
910}
911
912#define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
913#define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
914#define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
915 F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
916
917/*
918 * Disable SGE Interrupts
919 */
920void t1_sge_intr_disable(struct sge *sge)
921{
922 u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
923
924 writel(val & ~SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
925 writel(0, sge->adapter->regs + A_SG_INT_ENABLE);
926}
927
928/*
929 * Enable SGE interrupts.
930 */
931void t1_sge_intr_enable(struct sge *sge)
932{
933 u32 en = SGE_INT_ENABLE;
934 u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
935
936 if (sge->adapter->flags & TSO_CAPABLE)
937 en &= ~F_PACKET_TOO_BIG;
938 writel(en, sge->adapter->regs + A_SG_INT_ENABLE);
939 writel(val | SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
940}
941
942/*
943 * Clear SGE interrupts.
944 */
945void t1_sge_intr_clear(struct sge *sge)
946{
947 writel(SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_CAUSE);
948 writel(0xffffffff, sge->adapter->regs + A_SG_INT_CAUSE);
949}
950
951/*
952 * SGE 'Error' interrupt handler
953 */
954int t1_sge_intr_error_handler(struct sge *sge)
955{
956 struct adapter *adapter = sge->adapter;
957 u32 cause = readl(adapter->regs + A_SG_INT_CAUSE);
958
959 if (adapter->flags & TSO_CAPABLE)
960 cause &= ~F_PACKET_TOO_BIG;
961 if (cause & F_RESPQ_EXHAUSTED)
962 sge->stats.respQ_empty++;
963 if (cause & F_RESPQ_OVERFLOW) {
964 sge->stats.respQ_overflow++;
965 CH_ALERT("%s: SGE response queue overflow\n",
966 adapter->name);
967 }
968 if (cause & F_FL_EXHAUSTED) {
969 sge->stats.freelistQ_empty++;
970 freelQs_empty(sge);
971 }
972 if (cause & F_PACKET_TOO_BIG) {
973 sge->stats.pkt_too_big++;
974 CH_ALERT("%s: SGE max packet size exceeded\n",
975 adapter->name);
976 }
977 if (cause & F_PACKET_MISMATCH) {
978 sge->stats.pkt_mismatch++;
979 CH_ALERT("%s: SGE packet mismatch\n", adapter->name);
980 }
981 if (cause & SGE_INT_FATAL)
982 t1_fatal_err(adapter);
983
984 writel(cause, adapter->regs + A_SG_INT_CAUSE);
985 return 0;
986}
987
988const struct sge_intr_counts *t1_sge_get_intr_counts(struct sge *sge)
989{
990 return &sge->stats;
991}
992
993const struct sge_port_stats *t1_sge_get_port_stats(struct sge *sge, int port)
994{
995 return &sge->port_stats[port];
996}
997
998/**
999 * recycle_fl_buf - recycle a free list buffer
1000 * @fl: the free list
1001 * @idx: index of buffer to recycle
1002 *
1003 * Recycles the specified buffer on the given free list by adding it at
1004 * the next available slot on the list.
1005 */
1006static void recycle_fl_buf(struct freelQ *fl, int idx)
1007{
1008 struct freelQ_e *from = &fl->entries[idx];
1009 struct freelQ_e *to = &fl->entries[fl->pidx];
1010
1011 fl->centries[fl->pidx] = fl->centries[idx];
1012 to->addr_lo = from->addr_lo;
1013 to->addr_hi = from->addr_hi;
1014 to->len_gen = G_CMD_LEN(from->len_gen) | V_CMD_GEN1(fl->genbit);
1015 wmb();
1016 to->gen2 = V_CMD_GEN2(fl->genbit);
1017 fl->credits++;
1018
1019 if (++fl->pidx == fl->size) {
1020 fl->pidx = 0;
1021 fl->genbit ^= 1;
1022 }
1023}
1024
1025/**
1026 * get_packet - return the next ingress packet buffer
1027 * @pdev: the PCI device that received the packet
1028 * @fl: the SGE free list holding the packet
1029 * @len: the actual packet length, excluding any SGE padding
1030 * @dma_pad: padding at beginning of buffer left by SGE DMA
1031 * @skb_pad: padding to be used if the packet is copied
1032 * @copy_thres: length threshold under which a packet should be copied
1033 * @drop_thres: # of remaining buffers before we start dropping packets
1034 *
1035 * Get the next packet from a free list and complete setup of the
1036 * sk_buff. If the packet is small we make a copy and recycle the
1037 * original buffer, otherwise we use the original buffer itself. If a
1038 * positive drop threshold is supplied packets are dropped and their
1039 * buffers recycled if (a) the number of remaining buffers is under the
1040 * threshold and the packet is too big to copy, or (b) the packet should
1041 * be copied but there is no memory for the copy.
1042 */
1043static inline struct sk_buff *get_packet(struct pci_dev *pdev,
1044 struct freelQ *fl, unsigned int len,
1045 int dma_pad, int skb_pad,
1046 unsigned int copy_thres,
1047 unsigned int drop_thres)
1048{
1049 struct sk_buff *skb;
1050 struct freelQ_ce *ce = &fl->centries[fl->cidx];
1051
1052 if (len < copy_thres) {
1053 skb = alloc_skb(len + skb_pad, GFP_ATOMIC);
1054 if (likely(skb != NULL)) {
1055 skb_reserve(skb, skb_pad);
1056 skb_put(skb, len);
1057 pci_dma_sync_single_for_cpu(pdev,
1058 pci_unmap_addr(ce, dma_addr),
1059 pci_unmap_len(ce, dma_len),
1060 PCI_DMA_FROMDEVICE);
1061 memcpy(skb->data, ce->skb->data + dma_pad, len);
1062 pci_dma_sync_single_for_device(pdev,
1063 pci_unmap_addr(ce, dma_addr),
1064 pci_unmap_len(ce, dma_len),
1065 PCI_DMA_FROMDEVICE);
1066 } else if (!drop_thres)
1067 goto use_orig_buf;
1068
1069 recycle_fl_buf(fl, fl->cidx);
1070 return skb;
1071 }
1072
1073 if (fl->credits < drop_thres) {
1074 recycle_fl_buf(fl, fl->cidx);
1075 return NULL;
1076 }
1077
1078use_orig_buf:
1079 pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
1080 pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
1081 skb = ce->skb;
1082 skb_reserve(skb, dma_pad);
1083 skb_put(skb, len);
1084 return skb;
1085}
1086
1087/**
1088 * unexpected_offload - handle an unexpected offload packet
1089 * @adapter: the adapter
1090 * @fl: the free list that received the packet
1091 *
1092 * Called when we receive an unexpected offload packet (e.g., the TOE
1093 * function is disabled or the card is a NIC). Prints a message and
1094 * recycles the buffer.
1095 */
1096static void unexpected_offload(struct adapter *adapter, struct freelQ *fl)
1097{
1098 struct freelQ_ce *ce = &fl->centries[fl->cidx];
1099 struct sk_buff *skb = ce->skb;
1100
1101 pci_dma_sync_single_for_cpu(adapter->pdev, pci_unmap_addr(ce, dma_addr),
1102 pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
1103 CH_ERR("%s: unexpected offload packet, cmd %u\n",
1104 adapter->name, *skb->data);
1105 recycle_fl_buf(fl, fl->cidx);
1106}
1107
1108/*
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001109 * T1/T2 SGE limits the maximum DMA size per TX descriptor to
1110 * SGE_TX_DESC_MAX_PLEN (16KB). If the PAGE_SIZE is larger than 16KB, the
1111 * stack might send more than SGE_TX_DESC_MAX_PLEN in a contiguous manner.
1112 * Note that the *_large_page_tx_descs stuff will be optimized out when
1113 * PAGE_SIZE <= SGE_TX_DESC_MAX_PLEN.
1114 *
1115 * compute_large_page_descs() computes how many additional descriptors are
1116 * required to break down the stack's request.
1117 */
1118static inline unsigned int compute_large_page_tx_descs(struct sk_buff *skb)
1119{
1120 unsigned int count = 0;
1121 if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
1122 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
1123 unsigned int i, len = skb->len - skb->data_len;
1124 while (len > SGE_TX_DESC_MAX_PLEN) {
1125 count++;
1126 len -= SGE_TX_DESC_MAX_PLEN;
1127 }
1128 for (i = 0; nfrags--; i++) {
1129 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1130 len = frag->size;
1131 while (len > SGE_TX_DESC_MAX_PLEN) {
1132 count++;
1133 len -= SGE_TX_DESC_MAX_PLEN;
1134 }
1135 }
1136 }
1137 return count;
1138}
1139
1140/*
1141 * Write a cmdQ entry.
1142 *
1143 * Since this function writes the 'flags' field, it must not be used to
1144 * write the first cmdQ entry.
1145 */
1146static inline void write_tx_desc(struct cmdQ_e *e, dma_addr_t mapping,
1147 unsigned int len, unsigned int gen,
1148 unsigned int eop)
1149{
1150 if (unlikely(len > SGE_TX_DESC_MAX_PLEN))
1151 BUG();
1152 e->addr_lo = (u32)mapping;
1153 e->addr_hi = (u64)mapping >> 32;
1154 e->len_gen = V_CMD_LEN(len) | V_CMD_GEN1(gen);
1155 e->flags = F_CMD_DATAVALID | V_CMD_EOP(eop) | V_CMD_GEN2(gen);
1156}
1157
1158/*
1159 * See comment for previous function.
1160 *
1161 * write_tx_descs_large_page() writes additional SGE tx descriptors if
1162 * *desc_len exceeds HW's capability.
1163 */
1164static inline unsigned int write_large_page_tx_descs(unsigned int pidx,
1165 struct cmdQ_e **e,
1166 struct cmdQ_ce **ce,
1167 unsigned int *gen,
1168 dma_addr_t *desc_mapping,
1169 unsigned int *desc_len,
1170 unsigned int nfrags,
1171 struct cmdQ *q)
1172{
1173 if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
1174 struct cmdQ_e *e1 = *e;
1175 struct cmdQ_ce *ce1 = *ce;
1176
1177 while (*desc_len > SGE_TX_DESC_MAX_PLEN) {
1178 *desc_len -= SGE_TX_DESC_MAX_PLEN;
1179 write_tx_desc(e1, *desc_mapping, SGE_TX_DESC_MAX_PLEN,
1180 *gen, nfrags == 0 && *desc_len == 0);
1181 ce1->skb = NULL;
1182 pci_unmap_len_set(ce1, dma_len, 0);
1183 *desc_mapping += SGE_TX_DESC_MAX_PLEN;
1184 if (*desc_len) {
1185 ce1++;
1186 e1++;
1187 if (++pidx == q->size) {
1188 pidx = 0;
1189 *gen ^= 1;
1190 ce1 = q->centries;
1191 e1 = q->entries;
1192 }
1193 }
1194 }
1195 *e = e1;
1196 *ce = ce1;
1197 }
1198 return pidx;
1199}
1200
1201/*
Scott Bardone559fb512005-06-23 01:40:19 -04001202 * Write the command descriptors to transmit the given skb starting at
1203 * descriptor pidx with the given generation.
1204 */
1205static inline void write_tx_descs(struct adapter *adapter, struct sk_buff *skb,
1206 unsigned int pidx, unsigned int gen,
1207 struct cmdQ *q)
1208{
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001209 dma_addr_t mapping, desc_mapping;
Scott Bardone559fb512005-06-23 01:40:19 -04001210 struct cmdQ_e *e, *e1;
1211 struct cmdQ_ce *ce;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001212 unsigned int i, flags, first_desc_len, desc_len,
1213 nfrags = skb_shinfo(skb)->nr_frags;
1214
1215 e = e1 = &q->entries[pidx];
1216 ce = &q->centries[pidx];
Scott Bardone559fb512005-06-23 01:40:19 -04001217
1218 mapping = pci_map_single(adapter->pdev, skb->data,
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001219 skb->len - skb->data_len, PCI_DMA_TODEVICE);
1220
1221 desc_mapping = mapping;
1222 desc_len = skb->len - skb->data_len;
1223
1224 flags = F_CMD_DATAVALID | F_CMD_SOP |
1225 V_CMD_EOP(nfrags == 0 && desc_len <= SGE_TX_DESC_MAX_PLEN) |
1226 V_CMD_GEN2(gen);
1227 first_desc_len = (desc_len <= SGE_TX_DESC_MAX_PLEN) ?
1228 desc_len : SGE_TX_DESC_MAX_PLEN;
1229 e->addr_lo = (u32)desc_mapping;
1230 e->addr_hi = (u64)desc_mapping >> 32;
1231 e->len_gen = V_CMD_LEN(first_desc_len) | V_CMD_GEN1(gen);
1232 ce->skb = NULL;
1233 pci_unmap_len_set(ce, dma_len, 0);
1234
1235 if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN &&
1236 desc_len > SGE_TX_DESC_MAX_PLEN) {
1237 desc_mapping += first_desc_len;
1238 desc_len -= first_desc_len;
1239 e1++;
1240 ce++;
1241 if (++pidx == q->size) {
1242 pidx = 0;
1243 gen ^= 1;
1244 e1 = q->entries;
1245 ce = q->centries;
1246 }
1247 pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
1248 &desc_mapping, &desc_len,
1249 nfrags, q);
1250
1251 if (likely(desc_len))
1252 write_tx_desc(e1, desc_mapping, desc_len, gen,
1253 nfrags == 0);
1254 }
1255
Scott Bardone559fb512005-06-23 01:40:19 -04001256 ce->skb = NULL;
1257 pci_unmap_addr_set(ce, dma_addr, mapping);
1258 pci_unmap_len_set(ce, dma_len, skb->len - skb->data_len);
1259
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001260 for (i = 0; nfrags--; i++) {
Scott Bardone559fb512005-06-23 01:40:19 -04001261 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Scott Bardone559fb512005-06-23 01:40:19 -04001262 e1++;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001263 ce++;
Scott Bardone559fb512005-06-23 01:40:19 -04001264 if (++pidx == q->size) {
1265 pidx = 0;
1266 gen ^= 1;
Scott Bardone559fb512005-06-23 01:40:19 -04001267 e1 = q->entries;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001268 ce = q->centries;
Scott Bardone559fb512005-06-23 01:40:19 -04001269 }
1270
1271 mapping = pci_map_page(adapter->pdev, frag->page,
1272 frag->page_offset, frag->size,
1273 PCI_DMA_TODEVICE);
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001274 desc_mapping = mapping;
1275 desc_len = frag->size;
1276
1277 pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
1278 &desc_mapping, &desc_len,
1279 nfrags, q);
1280 if (likely(desc_len))
1281 write_tx_desc(e1, desc_mapping, desc_len, gen,
1282 nfrags == 0);
Scott Bardone559fb512005-06-23 01:40:19 -04001283 ce->skb = NULL;
1284 pci_unmap_addr_set(ce, dma_addr, mapping);
1285 pci_unmap_len_set(ce, dma_len, frag->size);
Scott Bardone559fb512005-06-23 01:40:19 -04001286 }
Scott Bardone559fb512005-06-23 01:40:19 -04001287 ce->skb = skb;
1288 wmb();
1289 e->flags = flags;
1290}
1291
1292/*
1293 * Clean up completed Tx buffers.
1294 */
1295static inline void reclaim_completed_tx(struct sge *sge, struct cmdQ *q)
1296{
1297 unsigned int reclaim = q->processed - q->cleaned;
1298
1299 if (reclaim) {
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001300 pr_debug("reclaim_completed_tx processed:%d cleaned:%d\n",
1301 q->processed, q->cleaned);
Scott Bardone559fb512005-06-23 01:40:19 -04001302 free_cmdQ_buffers(sge, q, reclaim);
1303 q->cleaned += reclaim;
1304 }
1305}
1306
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001307/*
1308 * Called from tasklet. Checks the scheduler for any
1309 * pending skbs that can be sent.
1310 */
1311static void restart_sched(unsigned long arg)
1312{
1313 struct sge *sge = (struct sge *) arg;
1314 struct adapter *adapter = sge->adapter;
1315 struct cmdQ *q = &sge->cmdQ[0];
1316 struct sk_buff *skb;
1317 unsigned int credits, queued_skb = 0;
1318
1319 spin_lock(&q->lock);
1320 reclaim_completed_tx(sge, q);
1321
1322 credits = q->size - q->in_use;
1323 pr_debug("restart_sched credits=%d\n", credits);
1324 while ((skb = sched_skb(sge, NULL, credits)) != NULL) {
1325 unsigned int genbit, pidx, count;
1326 count = 1 + skb_shinfo(skb)->nr_frags;
1327 count += compute_large_page_tx_descs(skb);
1328 q->in_use += count;
1329 genbit = q->genbit;
1330 pidx = q->pidx;
1331 q->pidx += count;
1332 if (q->pidx >= q->size) {
1333 q->pidx -= q->size;
1334 q->genbit ^= 1;
1335 }
1336 write_tx_descs(adapter, skb, pidx, genbit, q);
1337 credits = q->size - q->in_use;
1338 queued_skb = 1;
1339 }
1340
1341 if (queued_skb) {
1342 clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1343 if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
1344 set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1345 writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1346 }
1347 }
1348 spin_unlock(&q->lock);
1349}
Scott Bardone559fb512005-06-23 01:40:19 -04001350
Scott Bardone559fb512005-06-23 01:40:19 -04001351/**
1352 * sge_rx - process an ingress ethernet packet
1353 * @sge: the sge structure
1354 * @fl: the free list that contains the packet buffer
1355 * @len: the packet length
1356 *
1357 * Process an ingress ethernet pakcet and deliver it to the stack.
1358 */
1359static int sge_rx(struct sge *sge, struct freelQ *fl, unsigned int len)
1360{
1361 struct sk_buff *skb;
1362 struct cpl_rx_pkt *p;
1363 struct adapter *adapter = sge->adapter;
1364
1365 sge->stats.ethernet_pkts++;
1366 skb = get_packet(adapter->pdev, fl, len - sge->rx_pkt_pad,
1367 sge->rx_pkt_pad, 2, SGE_RX_COPY_THRES,
1368 SGE_RX_DROP_THRES);
1369 if (!skb) {
1370 sge->port_stats[0].rx_drops++; /* charge only port 0 for now */
1371 return 0;
1372 }
1373
1374 p = (struct cpl_rx_pkt *)skb->data;
1375 skb_pull(skb, sizeof(*p));
1376 skb->dev = adapter->port[p->iff].dev;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001377 if (p->iff >= adapter->params.nports) {
1378 kfree_skb(skb);
1379 return 0;
1380 }
1381
Scott Bardone559fb512005-06-23 01:40:19 -04001382 skb->dev->last_rx = jiffies;
1383 skb->protocol = eth_type_trans(skb, skb->dev);
1384 if ((adapter->flags & RX_CSUM_ENABLED) && p->csum == 0xffff &&
1385 skb->protocol == htons(ETH_P_IP) &&
1386 (skb->data[9] == IPPROTO_TCP || skb->data[9] == IPPROTO_UDP)) {
1387 sge->port_stats[p->iff].rx_cso_good++;
1388 skb->ip_summed = CHECKSUM_UNNECESSARY;
1389 } else
1390 skb->ip_summed = CHECKSUM_NONE;
1391
1392 if (unlikely(adapter->vlan_grp && p->vlan_valid)) {
1393 sge->port_stats[p->iff].vlan_xtract++;
1394 if (adapter->params.sge.polling)
1395 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp,
1396 ntohs(p->vlan));
1397 else
1398 vlan_hwaccel_rx(skb, adapter->vlan_grp,
1399 ntohs(p->vlan));
1400 } else if (adapter->params.sge.polling)
1401 netif_receive_skb(skb);
1402 else
1403 netif_rx(skb);
1404 return 0;
1405}
1406
1407/*
1408 * Returns true if a command queue has enough available descriptors that
1409 * we can resume Tx operation after temporarily disabling its packet queue.
1410 */
1411static inline int enough_free_Tx_descs(const struct cmdQ *q)
1412{
1413 unsigned int r = q->processed - q->cleaned;
1414
1415 return q->in_use - r < (q->size >> 1);
1416}
1417
1418/*
1419 * Called when sufficient space has become available in the SGE command queues
1420 * after the Tx packet schedulers have been suspended to restart the Tx path.
1421 */
1422static void restart_tx_queues(struct sge *sge)
1423{
1424 struct adapter *adap = sge->adapter;
1425
1426 if (enough_free_Tx_descs(&sge->cmdQ[0])) {
1427 int i;
1428
1429 for_each_port(adap, i) {
1430 struct net_device *nd = adap->port[i].dev;
1431
1432 if (test_and_clear_bit(nd->if_port,
1433 &sge->stopped_tx_queues) &&
1434 netif_running(nd)) {
Scott Bardone232a3472006-03-16 19:20:40 -05001435 sge->stats.cmdQ_restarted[2]++;
Scott Bardone559fb512005-06-23 01:40:19 -04001436 netif_wake_queue(nd);
1437 }
1438 }
1439 }
1440}
1441
1442/*
1443 * update_tx_info is called from the interrupt handler/NAPI to return cmdQ0
1444 * information.
1445 */
1446static unsigned int update_tx_info(struct adapter *adapter,
1447 unsigned int flags,
1448 unsigned int pr0)
1449{
1450 struct sge *sge = adapter->sge;
1451 struct cmdQ *cmdq = &sge->cmdQ[0];
1452
1453 cmdq->processed += pr0;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001454 if (flags & (F_FL0_ENABLE | F_FL1_ENABLE)) {
1455 freelQs_empty(sge);
1456 flags &= ~(F_FL0_ENABLE | F_FL1_ENABLE);
1457 }
Scott Bardone559fb512005-06-23 01:40:19 -04001458 if (flags & F_CMDQ0_ENABLE) {
1459 clear_bit(CMDQ_STAT_RUNNING, &cmdq->status);
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001460
Scott Bardone559fb512005-06-23 01:40:19 -04001461 if (cmdq->cleaned + cmdq->in_use != cmdq->processed &&
1462 !test_and_set_bit(CMDQ_STAT_LAST_PKT_DB, &cmdq->status)) {
1463 set_bit(CMDQ_STAT_RUNNING, &cmdq->status);
1464 writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1465 }
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001466 if (sge->tx_sched)
1467 tasklet_hi_schedule(&sge->tx_sched->sched_tsk);
1468
1469 flags &= ~F_CMDQ0_ENABLE;
Scott Bardone559fb512005-06-23 01:40:19 -04001470 }
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001471
Scott Bardone559fb512005-06-23 01:40:19 -04001472 if (unlikely(sge->stopped_tx_queues != 0))
1473 restart_tx_queues(sge);
1474
1475 return flags;
1476}
1477
1478/*
1479 * Process SGE responses, up to the supplied budget. Returns the number of
1480 * responses processed. A negative budget is effectively unlimited.
1481 */
1482static int process_responses(struct adapter *adapter, int budget)
1483{
1484 struct sge *sge = adapter->sge;
1485 struct respQ *q = &sge->respQ;
1486 struct respQ_e *e = &q->entries[q->cidx];
1487 int budget_left = budget;
1488 unsigned int flags = 0;
1489 unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1490
1491
1492 while (likely(budget_left && e->GenerationBit == q->genbit)) {
1493 flags |= e->Qsleeping;
1494
1495 cmdq_processed[0] += e->Cmdq0CreditReturn;
1496 cmdq_processed[1] += e->Cmdq1CreditReturn;
1497
1498 /* We batch updates to the TX side to avoid cacheline
1499 * ping-pong of TX state information on MP where the sender
1500 * might run on a different CPU than this function...
1501 */
1502 if (unlikely(flags & F_CMDQ0_ENABLE || cmdq_processed[0] > 64)) {
1503 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1504 cmdq_processed[0] = 0;
1505 }
1506 if (unlikely(cmdq_processed[1] > 16)) {
1507 sge->cmdQ[1].processed += cmdq_processed[1];
1508 cmdq_processed[1] = 0;
1509 }
1510 if (likely(e->DataValid)) {
1511 struct freelQ *fl = &sge->freelQ[e->FreelistQid];
1512
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02001513 BUG_ON(!e->Sop || !e->Eop);
Scott Bardone559fb512005-06-23 01:40:19 -04001514 if (unlikely(e->Offload))
1515 unexpected_offload(adapter, fl);
1516 else
1517 sge_rx(sge, fl, e->BufferLength);
1518
1519 /*
1520 * Note: this depends on each packet consuming a
1521 * single free-list buffer; cf. the BUG above.
1522 */
1523 if (++fl->cidx == fl->size)
1524 fl->cidx = 0;
1525 if (unlikely(--fl->credits <
1526 fl->size - SGE_FREEL_REFILL_THRESH))
1527 refill_free_list(sge, fl);
1528 } else
1529 sge->stats.pure_rsps++;
1530
1531 e++;
1532 if (unlikely(++q->cidx == q->size)) {
1533 q->cidx = 0;
1534 q->genbit ^= 1;
1535 e = q->entries;
1536 }
1537 prefetch(e);
1538
1539 if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
1540 writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
1541 q->credits = 0;
1542 }
1543 --budget_left;
1544 }
1545
1546 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1547 sge->cmdQ[1].processed += cmdq_processed[1];
1548
1549 budget -= budget_left;
1550 return budget;
1551}
1552
1553/*
1554 * A simpler version of process_responses() that handles only pure (i.e.,
1555 * non data-carrying) responses. Such respones are too light-weight to justify
1556 * calling a softirq when using NAPI, so we handle them specially in hard
1557 * interrupt context. The function is called with a pointer to a response,
1558 * which the caller must ensure is a valid pure response. Returns 1 if it
1559 * encounters a valid data-carrying response, 0 otherwise.
1560 */
1561static int process_pure_responses(struct adapter *adapter, struct respQ_e *e)
1562{
1563 struct sge *sge = adapter->sge;
1564 struct respQ *q = &sge->respQ;
1565 unsigned int flags = 0;
1566 unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1567
1568 do {
1569 flags |= e->Qsleeping;
1570
1571 cmdq_processed[0] += e->Cmdq0CreditReturn;
1572 cmdq_processed[1] += e->Cmdq1CreditReturn;
1573
1574 e++;
1575 if (unlikely(++q->cidx == q->size)) {
1576 q->cidx = 0;
1577 q->genbit ^= 1;
1578 e = q->entries;
1579 }
1580 prefetch(e);
1581
1582 if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
1583 writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
1584 q->credits = 0;
1585 }
1586 sge->stats.pure_rsps++;
1587 } while (e->GenerationBit == q->genbit && !e->DataValid);
1588
1589 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1590 sge->cmdQ[1].processed += cmdq_processed[1];
1591
1592 return e->GenerationBit == q->genbit;
1593}
1594
1595/*
1596 * Handler for new data events when using NAPI. This does not need any locking
1597 * or protection from interrupts as data interrupts are off at this point and
1598 * other adapter interrupts do not interfere.
1599 */
1600static int t1_poll(struct net_device *dev, int *budget)
1601{
1602 struct adapter *adapter = dev->priv;
1603 int effective_budget = min(*budget, dev->quota);
1604
1605 int work_done = process_responses(adapter, effective_budget);
1606 *budget -= work_done;
1607 dev->quota -= work_done;
1608
1609 if (work_done >= effective_budget)
1610 return 1;
1611
1612 __netif_rx_complete(dev);
1613
1614 /*
1615 * Because we don't atomically flush the following write it is
1616 * possible that in very rare cases it can reach the device in a way
1617 * that races with a new response being written plus an error interrupt
1618 * causing the NAPI interrupt handler below to return unhandled status
1619 * to the OS. To protect against this would require flushing the write
1620 * and doing both the write and the flush with interrupts off. Way too
1621 * expensive and unjustifiable given the rarity of the race.
1622 */
1623 writel(adapter->sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
1624 return 0;
1625}
1626
1627/*
1628 * Returns true if the device is already scheduled for polling.
1629 */
1630static inline int napi_is_scheduled(struct net_device *dev)
1631{
1632 return test_bit(__LINK_STATE_RX_SCHED, &dev->state);
1633}
1634
1635/*
1636 * NAPI version of the main interrupt handler.
1637 */
David Howells7d12e782006-10-05 14:55:46 +01001638static irqreturn_t t1_interrupt_napi(int irq, void *data)
Scott Bardone559fb512005-06-23 01:40:19 -04001639{
1640 int handled;
1641 struct adapter *adapter = data;
1642 struct sge *sge = adapter->sge;
1643 struct respQ *q = &adapter->sge->respQ;
1644
1645 /*
1646 * Clear the SGE_DATA interrupt first thing. Normally the NAPI
1647 * handler has control of the response queue and the interrupt handler
1648 * can look at the queue reliably only once it knows NAPI is off.
1649 * We can't wait that long to clear the SGE_DATA interrupt because we
1650 * could race with t1_poll rearming the SGE interrupt, so we need to
1651 * clear the interrupt speculatively and really early on.
1652 */
1653 writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
1654
1655 spin_lock(&adapter->async_lock);
1656 if (!napi_is_scheduled(sge->netdev)) {
1657 struct respQ_e *e = &q->entries[q->cidx];
1658
1659 if (e->GenerationBit == q->genbit) {
1660 if (e->DataValid ||
1661 process_pure_responses(adapter, e)) {
Stephen Hemminger86c27d22006-11-17 17:00:34 -08001662 if (likely(__netif_rx_schedule_prep(sge->netdev)))
Scott Bardone559fb512005-06-23 01:40:19 -04001663 __netif_rx_schedule(sge->netdev);
Stephen Hemminger86c27d22006-11-17 17:00:34 -08001664 else if (net_ratelimit())
1665 printk(KERN_INFO
Scott Bardone559fb512005-06-23 01:40:19 -04001666 "NAPI schedule failure!\n");
1667 } else
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001668 writel(q->cidx, adapter->regs + A_SG_SLEEPING);
1669
Scott Bardone559fb512005-06-23 01:40:19 -04001670 handled = 1;
1671 goto unlock;
1672 } else
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001673 writel(q->cidx, adapter->regs + A_SG_SLEEPING);
1674 } else if (readl(adapter->regs + A_PL_CAUSE) & F_PL_INTR_SGE_DATA) {
1675 printk(KERN_ERR "data interrupt while NAPI running\n");
1676 }
Scott Bardone559fb512005-06-23 01:40:19 -04001677
1678 handled = t1_slow_intr_handler(adapter);
1679 if (!handled)
1680 sge->stats.unhandled_irqs++;
1681 unlock:
1682 spin_unlock(&adapter->async_lock);
1683 return IRQ_RETVAL(handled != 0);
1684}
1685
1686/*
1687 * Main interrupt handler, optimized assuming that we took a 'DATA'
1688 * interrupt.
1689 *
1690 * 1. Clear the interrupt
1691 * 2. Loop while we find valid descriptors and process them; accumulate
1692 * information that can be processed after the loop
1693 * 3. Tell the SGE at which index we stopped processing descriptors
1694 * 4. Bookkeeping; free TX buffers, ring doorbell if there are any
1695 * outstanding TX buffers waiting, replenish RX buffers, potentially
1696 * reenable upper layers if they were turned off due to lack of TX
1697 * resources which are available again.
1698 * 5. If we took an interrupt, but no valid respQ descriptors was found we
1699 * let the slow_intr_handler run and do error handling.
1700 */
David Howells7d12e782006-10-05 14:55:46 +01001701static irqreturn_t t1_interrupt(int irq, void *cookie)
Scott Bardone559fb512005-06-23 01:40:19 -04001702{
1703 int work_done;
1704 struct respQ_e *e;
1705 struct adapter *adapter = cookie;
1706 struct respQ *Q = &adapter->sge->respQ;
1707
1708 spin_lock(&adapter->async_lock);
1709 e = &Q->entries[Q->cidx];
1710 prefetch(e);
1711
1712 writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
1713
1714 if (likely(e->GenerationBit == Q->genbit))
1715 work_done = process_responses(adapter, -1);
1716 else
1717 work_done = t1_slow_intr_handler(adapter);
1718
1719 /*
1720 * The unconditional clearing of the PL_CAUSE above may have raced
1721 * with DMA completion and the corresponding generation of a response
1722 * to cause us to miss the resulting data interrupt. The next write
1723 * is also unconditional to recover the missed interrupt and render
1724 * this race harmless.
1725 */
1726 writel(Q->cidx, adapter->regs + A_SG_SLEEPING);
1727
1728 if (!work_done)
1729 adapter->sge->stats.unhandled_irqs++;
1730 spin_unlock(&adapter->async_lock);
1731 return IRQ_RETVAL(work_done != 0);
1732}
1733
David Howells7d12e782006-10-05 14:55:46 +01001734irq_handler_t t1_select_intr_handler(adapter_t *adapter)
Scott Bardone559fb512005-06-23 01:40:19 -04001735{
1736 return adapter->params.sge.polling ? t1_interrupt_napi : t1_interrupt;
1737}
1738
1739/*
1740 * Enqueues the sk_buff onto the cmdQ[qid] and has hardware fetch it.
1741 *
1742 * The code figures out how many entries the sk_buff will require in the
1743 * cmdQ and updates the cmdQ data structure with the state once the enqueue
1744 * has complete. Then, it doesn't access the global structure anymore, but
1745 * uses the corresponding fields on the stack. In conjuction with a spinlock
1746 * around that code, we can make the function reentrant without holding the
1747 * lock when we actually enqueue (which might be expensive, especially on
1748 * architectures with IO MMUs).
1749 *
1750 * This runs with softirqs disabled.
1751 */
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001752static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
1753 unsigned int qid, struct net_device *dev)
Scott Bardone559fb512005-06-23 01:40:19 -04001754{
1755 struct sge *sge = adapter->sge;
1756 struct cmdQ *q = &sge->cmdQ[qid];
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001757 unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
Scott Bardone559fb512005-06-23 01:40:19 -04001758
1759 spin_lock(&q->lock);
1760 reclaim_completed_tx(sge, q);
1761
1762 pidx = q->pidx;
1763 credits = q->size - q->in_use;
1764 count = 1 + skb_shinfo(skb)->nr_frags;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001765 count += compute_large_page_tx_descs(skb);
Scott Bardone559fb512005-06-23 01:40:19 -04001766
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001767 /* Ethernet packet */
1768 if (unlikely(credits < count)) {
1769 if (!netif_queue_stopped(dev)) {
Scott Bardone559fb512005-06-23 01:40:19 -04001770 netif_stop_queue(dev);
1771 set_bit(dev->if_port, &sge->stopped_tx_queues);
Scott Bardone232a3472006-03-16 19:20:40 -05001772 sge->stats.cmdQ_full[2]++;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001773 CH_ERR("%s: Tx ring full while queue awake!\n",
1774 adapter->name);
Scott Bardone559fb512005-06-23 01:40:19 -04001775 }
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001776 spin_unlock(&q->lock);
1777 return NETDEV_TX_BUSY;
Scott Bardone559fb512005-06-23 01:40:19 -04001778 }
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001779
1780 if (unlikely(credits - count < q->stop_thres)) {
1781 netif_stop_queue(dev);
1782 set_bit(dev->if_port, &sge->stopped_tx_queues);
1783 sge->stats.cmdQ_full[2]++;
1784 }
1785
1786 /* T204 cmdQ0 skbs that are destined for a certain port have to go
1787 * through the scheduler.
1788 */
1789 if (sge->tx_sched && !qid && skb->dev) {
1790 use_sched:
1791 use_sched_skb = 1;
1792 /* Note that the scheduler might return a different skb than
1793 * the one passed in.
1794 */
1795 skb = sched_skb(sge, skb, credits);
1796 if (!skb) {
1797 spin_unlock(&q->lock);
1798 return NETDEV_TX_OK;
1799 }
1800 pidx = q->pidx;
1801 count = 1 + skb_shinfo(skb)->nr_frags;
1802 count += compute_large_page_tx_descs(skb);
1803 }
1804
Scott Bardone559fb512005-06-23 01:40:19 -04001805 q->in_use += count;
1806 genbit = q->genbit;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001807 pidx = q->pidx;
Scott Bardone559fb512005-06-23 01:40:19 -04001808 q->pidx += count;
1809 if (q->pidx >= q->size) {
1810 q->pidx -= q->size;
1811 q->genbit ^= 1;
1812 }
1813 spin_unlock(&q->lock);
1814
1815 write_tx_descs(adapter, skb, pidx, genbit, q);
1816
1817 /*
1818 * We always ring the doorbell for cmdQ1. For cmdQ0, we only ring
1819 * the doorbell if the Q is asleep. There is a natural race, where
1820 * the hardware is going to sleep just after we checked, however,
1821 * then the interrupt handler will detect the outstanding TX packet
1822 * and ring the doorbell for us.
1823 */
1824 if (qid)
1825 doorbell_pio(adapter, F_CMDQ1_ENABLE);
1826 else {
1827 clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1828 if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
1829 set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
1830 writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
1831 }
1832 }
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001833
1834 if (use_sched_skb) {
1835 if (spin_trylock(&q->lock)) {
1836 credits = q->size - q->in_use;
1837 skb = NULL;
1838 goto use_sched;
1839 }
1840 }
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001841 return NETDEV_TX_OK;
Scott Bardone559fb512005-06-23 01:40:19 -04001842}
1843
1844#define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))
1845
1846/*
1847 * eth_hdr_len - return the length of an Ethernet header
1848 * @data: pointer to the start of the Ethernet header
1849 *
1850 * Returns the length of an Ethernet header, including optional VLAN tag.
1851 */
1852static inline int eth_hdr_len(const void *data)
1853{
1854 const struct ethhdr *e = data;
1855
1856 return e->h_proto == htons(ETH_P_8021Q) ? VLAN_ETH_HLEN : ETH_HLEN;
1857}
1858
1859/*
1860 * Adds the CPL header to the sk_buff and passes it to t1_sge_tx.
1861 */
1862int t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
1863{
1864 struct adapter *adapter = dev->priv;
1865 struct sge_port_stats *st = &adapter->sge->port_stats[dev->if_port];
1866 struct sge *sge = adapter->sge;
1867 struct cpl_tx_pkt *cpl;
1868
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001869 if (skb->protocol == htons(ETH_P_CPL5))
1870 goto send;
1871
1872 if (skb_shinfo(skb)->gso_size) {
Scott Bardone559fb512005-06-23 01:40:19 -04001873 int eth_type;
1874 struct cpl_tx_pkt_lso *hdr;
1875
1876 st->tso++;
1877
1878 eth_type = skb->nh.raw - skb->data == ETH_HLEN ?
1879 CPL_ETH_II : CPL_ETH_II_VLAN;
1880
1881 hdr = (struct cpl_tx_pkt_lso *)skb_push(skb, sizeof(*hdr));
1882 hdr->opcode = CPL_TX_PKT_LSO;
1883 hdr->ip_csum_dis = hdr->l4_csum_dis = 0;
1884 hdr->ip_hdr_words = skb->nh.iph->ihl;
1885 hdr->tcp_hdr_words = skb->h.th->doff;
1886 hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001887 skb_shinfo(skb)->gso_size));
Scott Bardone559fb512005-06-23 01:40:19 -04001888 hdr->len = htonl(skb->len - sizeof(*hdr));
1889 cpl = (struct cpl_tx_pkt *)hdr;
1890 sge->stats.tx_lso_pkts++;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001891 } else {
Scott Bardone559fb512005-06-23 01:40:19 -04001892 /*
1893 * Packets shorter than ETH_HLEN can break the MAC, drop them
1894 * early. Also, we may get oversized packets because some
1895 * parts of the kernel don't handle our unusual hard_header_len
1896 * right, drop those too.
1897 */
1898 if (unlikely(skb->len < ETH_HLEN ||
1899 skb->len > dev->mtu + eth_hdr_len(skb->data))) {
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001900 pr_debug("%s: packet size %d hdr %d mtu%d\n", dev->name,
1901 skb->len, eth_hdr_len(skb->data), dev->mtu);
Scott Bardone559fb512005-06-23 01:40:19 -04001902 dev_kfree_skb_any(skb);
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001903 return NETDEV_TX_OK;
Scott Bardone559fb512005-06-23 01:40:19 -04001904 }
1905
1906 /*
1907 * We are using a non-standard hard_header_len and some kernel
1908 * components, such as pktgen, do not handle it right.
1909 * Complain when this happens but try to fix things up.
1910 */
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001911 if (unlikely(skb_headroom(skb) < dev->hard_header_len - ETH_HLEN)) {
Scott Bardone559fb512005-06-23 01:40:19 -04001912 struct sk_buff *orig_skb = skb;
1913
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001914 pr_debug("%s: headroom %d header_len %d\n", dev->name,
1915 skb_headroom(skb), dev->hard_header_len);
1916
Scott Bardone559fb512005-06-23 01:40:19 -04001917 if (net_ratelimit())
1918 printk(KERN_ERR "%s: inadequate headroom in "
1919 "Tx packet\n", dev->name);
1920 skb = skb_realloc_headroom(skb, sizeof(*cpl));
1921 dev_kfree_skb_any(orig_skb);
1922 if (!skb)
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001923 return NETDEV_TX_OK;
Scott Bardone559fb512005-06-23 01:40:19 -04001924 }
1925
1926 if (!(adapter->flags & UDP_CSUM_CAPABLE) &&
Patrick McHardy84fa7932006-08-29 16:44:56 -07001927 skb->ip_summed == CHECKSUM_PARTIAL &&
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001928 skb->nh.iph->protocol == IPPROTO_UDP) {
Patrick McHardy84fa7932006-08-29 16:44:56 -07001929 if (unlikely(skb_checksum_help(skb))) {
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001930 pr_debug("%s: unable to do udp checksum\n", dev->name);
Scott Bardone559fb512005-06-23 01:40:19 -04001931 dev_kfree_skb_any(skb);
Stephen Hemmingeraa845052005-12-14 14:38:44 -08001932 return NETDEV_TX_OK;
Scott Bardone559fb512005-06-23 01:40:19 -04001933 }
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001934 }
Scott Bardone559fb512005-06-23 01:40:19 -04001935
1936 /* Hmmm, assuming to catch the gratious arp... and we'll use
1937 * it to flush out stuck espi packets...
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001938 */
1939 if ((unlikely(!adapter->sge->espibug_skb[dev->if_port]))) {
Scott Bardone559fb512005-06-23 01:40:19 -04001940 if (skb->protocol == htons(ETH_P_ARP) &&
1941 skb->nh.arph->ar_op == htons(ARPOP_REQUEST)) {
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001942 adapter->sge->espibug_skb[dev->if_port] = skb;
Scott Bardone559fb512005-06-23 01:40:19 -04001943 /* We want to re-use this skb later. We
1944 * simply bump the reference count and it
1945 * will not be freed...
1946 */
1947 skb = skb_get(skb);
1948 }
1949 }
1950
1951 cpl = (struct cpl_tx_pkt *)__skb_push(skb, sizeof(*cpl));
1952 cpl->opcode = CPL_TX_PKT;
1953 cpl->ip_csum_dis = 1; /* SW calculates IP csum */
Patrick McHardy84fa7932006-08-29 16:44:56 -07001954 cpl->l4_csum_dis = skb->ip_summed == CHECKSUM_PARTIAL ? 0 : 1;
Scott Bardone559fb512005-06-23 01:40:19 -04001955 /* the length field isn't used so don't bother setting it */
1956
Patrick McHardy84fa7932006-08-29 16:44:56 -07001957 st->tx_cso += (skb->ip_summed == CHECKSUM_PARTIAL);
1958 sge->stats.tx_do_cksum += (skb->ip_summed == CHECKSUM_PARTIAL);
Scott Bardone559fb512005-06-23 01:40:19 -04001959 sge->stats.tx_reg_pkts++;
1960 }
1961 cpl->iff = dev->if_port;
1962
1963#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
1964 if (adapter->vlan_grp && vlan_tx_tag_present(skb)) {
1965 cpl->vlan_valid = 1;
1966 cpl->vlan = htons(vlan_tx_tag_get(skb));
1967 st->vlan_insert++;
1968 } else
1969#endif
1970 cpl->vlan_valid = 0;
1971
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001972send:
Scott Bardone559fb512005-06-23 01:40:19 -04001973 dev->trans_start = jiffies;
1974 return t1_sge_tx(skb, adapter, 0, dev);
1975}
1976
1977/*
1978 * Callback for the Tx buffer reclaim timer. Runs with softirqs disabled.
1979 */
1980static void sge_tx_reclaim_cb(unsigned long data)
1981{
1982 int i;
1983 struct sge *sge = (struct sge *)data;
1984
1985 for (i = 0; i < SGE_CMDQ_N; ++i) {
1986 struct cmdQ *q = &sge->cmdQ[i];
1987
1988 if (!spin_trylock(&q->lock))
1989 continue;
1990
1991 reclaim_completed_tx(sge, q);
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08001992 if (i == 0 && q->in_use) { /* flush pending credits */
1993 writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
1994 }
Scott Bardone559fb512005-06-23 01:40:19 -04001995 spin_unlock(&q->lock);
1996 }
1997 mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
1998}
1999
2000/*
2001 * Propagate changes of the SGE coalescing parameters to the HW.
2002 */
2003int t1_sge_set_coalesce_params(struct sge *sge, struct sge_params *p)
2004{
2005 sge->netdev->poll = t1_poll;
2006 sge->fixed_intrtimer = p->rx_coalesce_usecs *
2007 core_ticks_per_usec(sge->adapter);
2008 writel(sge->fixed_intrtimer, sge->adapter->regs + A_SG_INTRTIMER);
2009 return 0;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002010}
2011
2012/*
2013 * Allocates both RX and TX resources and configures the SGE. However,
2014 * the hardware is not enabled yet.
2015 */
2016int t1_sge_configure(struct sge *sge, struct sge_params *p)
2017{
2018 if (alloc_rx_resources(sge, p))
2019 return -ENOMEM;
2020 if (alloc_tx_resources(sge, p)) {
2021 free_rx_resources(sge);
2022 return -ENOMEM;
2023 }
2024 configure_sge(sge, p);
2025
2026 /*
2027 * Now that we have sized the free lists calculate the payload
2028 * capacity of the large buffers. Other parts of the driver use
2029 * this to set the max offload coalescing size so that RX packets
2030 * do not overflow our large buffers.
2031 */
2032 p->large_buf_capacity = jumbo_payload_capacity(sge);
2033 return 0;
2034}
2035
2036/*
Scott Bardone559fb512005-06-23 01:40:19 -04002037 * Disables the DMA engine.
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002038 */
Scott Bardone559fb512005-06-23 01:40:19 -04002039void t1_sge_stop(struct sge *sge)
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002040{
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002041 int i;
Scott Bardone559fb512005-06-23 01:40:19 -04002042 writel(0, sge->adapter->regs + A_SG_CONTROL);
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002043 readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
2044
Scott Bardone559fb512005-06-23 01:40:19 -04002045 if (is_T2(sge->adapter))
2046 del_timer_sync(&sge->espibug_timer);
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002047
Scott Bardone559fb512005-06-23 01:40:19 -04002048 del_timer_sync(&sge->tx_reclaim_timer);
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002049 if (sge->tx_sched)
2050 tx_sched_stop(sge);
2051
2052 for (i = 0; i < MAX_NPORTS; i++)
2053 if (sge->espibug_skb[i])
2054 kfree_skb(sge->espibug_skb[i]);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002055}
2056
2057/*
Scott Bardone559fb512005-06-23 01:40:19 -04002058 * Enables the DMA engine.
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002059 */
Scott Bardone559fb512005-06-23 01:40:19 -04002060void t1_sge_start(struct sge *sge)
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002061{
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002062 refill_free_list(sge, &sge->freelQ[0]);
2063 refill_free_list(sge, &sge->freelQ[1]);
2064
Scott Bardone559fb512005-06-23 01:40:19 -04002065 writel(sge->sge_control, sge->adapter->regs + A_SG_CONTROL);
2066 doorbell_pio(sge->adapter, F_FL0_ENABLE | F_FL1_ENABLE);
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002067 readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002068
Scott Bardone559fb512005-06-23 01:40:19 -04002069 mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002070
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002071 if (is_T2(sge->adapter))
Scott Bardone559fb512005-06-23 01:40:19 -04002072 mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002073}
2074
2075/*
Scott Bardone559fb512005-06-23 01:40:19 -04002076 * Callback for the T2 ESPI 'stuck packet feature' workaorund
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002077 */
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002078static void espibug_workaround_t204(unsigned long data)
2079{
2080 struct adapter *adapter = (struct adapter *)data;
2081 struct sge *sge = adapter->sge;
2082 unsigned int nports = adapter->params.nports;
2083 u32 seop[MAX_NPORTS];
2084
2085 if (adapter->open_device_map & PORT_MASK) {
2086 int i;
2087 if (t1_espi_get_mon_t204(adapter, &(seop[0]), 0) < 0) {
2088 return;
2089 }
2090 for (i = 0; i < nports; i++) {
2091 struct sk_buff *skb = sge->espibug_skb[i];
2092 if ( (netif_running(adapter->port[i].dev)) &&
2093 !(netif_queue_stopped(adapter->port[i].dev)) &&
2094 (seop[i] && ((seop[i] & 0xfff) == 0)) &&
2095 skb ) {
2096 if (!skb->cb[0]) {
2097 u8 ch_mac_addr[ETH_ALEN] =
2098 {0x0, 0x7, 0x43, 0x0, 0x0, 0x0};
2099 memcpy(skb->data + sizeof(struct cpl_tx_pkt),
2100 ch_mac_addr, ETH_ALEN);
2101 memcpy(skb->data + skb->len - 10,
2102 ch_mac_addr, ETH_ALEN);
2103 skb->cb[0] = 0xff;
2104 }
2105
2106 /* bump the reference count to avoid freeing of
2107 * the skb once the DMA has completed.
2108 */
2109 skb = skb_get(skb);
2110 t1_sge_tx(skb, adapter, 0, adapter->port[i].dev);
2111 }
2112 }
2113 }
2114 mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
2115}
2116
2117static void espibug_workaround(unsigned long data)
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002118{
Scott Bardone559fb512005-06-23 01:40:19 -04002119 struct adapter *adapter = (struct adapter *)data;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002120 struct sge *sge = adapter->sge;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002121
Scott Bardone559fb512005-06-23 01:40:19 -04002122 if (netif_running(adapter->port[0].dev)) {
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002123 struct sk_buff *skb = sge->espibug_skb[0];
2124 u32 seop = t1_espi_get_mon(adapter, 0x930, 0);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002125
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002126 if ((seop & 0xfff0fff) == 0xfff && skb) {
2127 if (!skb->cb[0]) {
2128 u8 ch_mac_addr[ETH_ALEN] =
2129 {0x0, 0x7, 0x43, 0x0, 0x0, 0x0};
2130 memcpy(skb->data + sizeof(struct cpl_tx_pkt),
2131 ch_mac_addr, ETH_ALEN);
2132 memcpy(skb->data + skb->len - 10, ch_mac_addr,
2133 ETH_ALEN);
2134 skb->cb[0] = 0xff;
2135 }
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002136
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002137 /* bump the reference count to avoid freeing of the
2138 * skb once the DMA has completed.
2139 */
2140 skb = skb_get(skb);
2141 t1_sge_tx(skb, adapter, 0, adapter->port[0].dev);
2142 }
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002143 }
Scott Bardone559fb512005-06-23 01:40:19 -04002144 mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002145}
2146
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002147/*
Scott Bardone559fb512005-06-23 01:40:19 -04002148 * Creates a t1_sge structure and returns suggested resource parameters.
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002149 */
Scott Bardone559fb512005-06-23 01:40:19 -04002150struct sge * __devinit t1_sge_create(struct adapter *adapter,
2151 struct sge_params *p)
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002152{
Stephen Hemmingercbee9f92006-11-17 17:01:52 -08002153 struct sge *sge = kzalloc(sizeof(*sge), GFP_KERNEL);
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002154
Scott Bardone559fb512005-06-23 01:40:19 -04002155 if (!sge)
2156 return NULL;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002157
Scott Bardone559fb512005-06-23 01:40:19 -04002158 sge->adapter = adapter;
2159 sge->netdev = adapter->port[0].dev;
2160 sge->rx_pkt_pad = t1_is_T1B(adapter) ? 0 : 2;
2161 sge->jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
2162
2163 init_timer(&sge->tx_reclaim_timer);
2164 sge->tx_reclaim_timer.data = (unsigned long)sge;
2165 sge->tx_reclaim_timer.function = sge_tx_reclaim_cb;
2166
2167 if (is_T2(sge->adapter)) {
2168 init_timer(&sge->espibug_timer);
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002169
2170 if (adapter->params.nports > 1) {
2171 tx_sched_init(sge);
2172 sge->espibug_timer.function = espibug_workaround_t204;
2173 } else {
2174 sge->espibug_timer.function = espibug_workaround;
2175 }
Scott Bardone559fb512005-06-23 01:40:19 -04002176 sge->espibug_timer.data = (unsigned long)sge->adapter;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002177
Scott Bardone559fb512005-06-23 01:40:19 -04002178 sge->espibug_timeout = 1;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002179 /* for T204, every 10ms */
2180 if (adapter->params.nports > 1)
2181 sge->espibug_timeout = HZ/100;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002182 }
Scott Bardone559fb512005-06-23 01:40:19 -04002183
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002184
Scott Bardone559fb512005-06-23 01:40:19 -04002185 p->cmdQ_size[0] = SGE_CMDQ0_E_N;
2186 p->cmdQ_size[1] = SGE_CMDQ1_E_N;
2187 p->freelQ_size[!sge->jumbo_fl] = SGE_FREEL_SIZE;
2188 p->freelQ_size[sge->jumbo_fl] = SGE_JUMBO_FREEL_SIZE;
Stephen Hemmingerf1d3d382006-12-01 16:36:16 -08002189 if (sge->tx_sched) {
2190 if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204)
2191 p->rx_coalesce_usecs = 15;
2192 else
2193 p->rx_coalesce_usecs = 50;
2194 } else
2195 p->rx_coalesce_usecs = 50;
2196
Scott Bardone559fb512005-06-23 01:40:19 -04002197 p->coalesce_enable = 0;
2198 p->sample_interval_usecs = 0;
2199 p->polling = 0;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002200
Scott Bardone559fb512005-06-23 01:40:19 -04002201 return sge;
Christoph Lameter8199d3a2005-03-30 13:34:31 -08002202}