sge.c 48.4 KB
Newer Older
1 2 3
/*****************************************************************************
 *                                                                           *
 * File: sge.c                                                               *
S
Scott Bardone 已提交
4 5
 * $Revision: 1.26 $                                                         *
 * $Date: 2005/06/21 18:29:48 $                                              *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
 * Description:                                                              *
 *  DMA engine.                                                              *
 *  part of the Chelsio 10Gb Ethernet Driver.                                *
 *                                                                           *
 * This program is free software; you can redistribute it and/or modify      *
 * it under the terms of the GNU General Public License, version 2, as       *
 * published by the Free Software Foundation.                                *
 *                                                                           *
 * You should have received a copy of the GNU General Public License along   *
 * with this program; if not, write to the Free Software Foundation, Inc.,   *
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                 *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED    *
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF      *
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.                     *
 *                                                                           *
 * http://www.chelsio.com                                                    *
 *                                                                           *
 * Copyright (c) 2003 - 2005 Chelsio Communications, Inc.                    *
 * All rights reserved.                                                      *
 *                                                                           *
 * Maintainers: maintainers@chelsio.com                                      *
 *                                                                           *
 * Authors: Dimitrios Michailidis   <dm@chelsio.com>                         *
 *          Tina Yang               <tainay@chelsio.com>                     *
 *          Felix Marti             <felix@chelsio.com>                      *
 *          Scott Bardone           <sbardone@chelsio.com>                   *
 *          Kurt Ottaway            <kottaway@chelsio.com>                   *
 *          Frank DiMambro          <frank@chelsio.com>                      *
 *                                                                           *
 * History:                                                                  *
 *                                                                           *
 ****************************************************************************/

#include "common.h"

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/if_vlan.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/if_arp.h>

#include "cpl5_cmd.h"
#include "sge.h"
#include "regs.h"
#include "espi.h"

S
Scott Bardone 已提交
60 61

#ifdef NETIF_F_TSO
62
#include <linux/tcp.h>
S
Scott Bardone 已提交
63
#endif
64 65 66

#define SGE_CMDQ_N		2
#define SGE_FREELQ_N		2
S
Scott Bardone 已提交
67
#define SGE_CMDQ0_E_N		1024
68 69 70 71 72
#define SGE_CMDQ1_E_N		128
#define SGE_FREEL_SIZE		4096
#define SGE_JUMBO_FREEL_SIZE	512
#define SGE_FREEL_REFILL_THRESH	16
#define SGE_RESPQ_E_N		1024
S
Scott Bardone 已提交
73 74
#define SGE_INTRTIMER_NRES	1000
#define SGE_RX_COPY_THRES	256
75 76
#define SGE_RX_SM_BUF_SIZE	1536

S
Scott Bardone 已提交
77 78 79 80 81 82 83 84 85
# define SGE_RX_DROP_THRES 2

#define SGE_RESPQ_REPLENISH_THRES (SGE_RESPQ_E_N / 4)

/*
 * Period of the TX buffer reclaim timer.  This timer does not need to run
 * frequently as TX buffers are usually reclaimed by new TX packets.
 */
#define TX_RECLAIM_PERIOD (HZ / 4)
86 87

#ifndef NET_IP_ALIGN
S
Scott Bardone 已提交
88
# define NET_IP_ALIGN 2
89 90
#endif

S
Scott Bardone 已提交
91 92 93 94 95 96 97 98 99
#define M_CMD_LEN       0x7fffffff
#define V_CMD_LEN(v)    (v)
#define G_CMD_LEN(v)    ((v) & M_CMD_LEN)
#define V_CMD_GEN1(v)   ((v) << 31)
#define V_CMD_GEN2(v)   (v)
#define F_CMD_DATAVALID (1 << 1)
#define F_CMD_SOP       (1 << 2)
#define V_CMD_EOP(v)    ((v) << 3)

100
/*
S
Scott Bardone 已提交
101
 * Command queue, receive buffer list, and response queue descriptors.
102 103 104
 */
#if defined(__BIG_ENDIAN_BITFIELD)
struct cmdQ_e {
S
Scott Bardone 已提交
105 106 107 108
	u32 addr_lo;
	u32 len_gen;
	u32 flags;
	u32 addr_hi;
109 110 111
};

struct freelQ_e {
S
Scott Bardone 已提交
112 113 114 115
	u32 addr_lo;
	u32 len_gen;
	u32 gen2;
	u32 addr_hi;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
};

struct respQ_e {
	u32 Qsleeping		: 4;
	u32 Cmdq1CreditReturn	: 5;
	u32 Cmdq1DmaComplete	: 5;
	u32 Cmdq0CreditReturn	: 5;
	u32 Cmdq0DmaComplete	: 5;
	u32 FreelistQid		: 2;
	u32 CreditValid		: 1;
	u32 DataValid		: 1;
	u32 Offload		: 1;
	u32 Eop			: 1;
	u32 Sop			: 1;
	u32 GenerationBit	: 1;
	u32 BufferLength;
};
#elif defined(__LITTLE_ENDIAN_BITFIELD)
struct cmdQ_e {
S
Scott Bardone 已提交
135 136 137 138
	u32 len_gen;
	u32 addr_lo;
	u32 addr_hi;
	u32 flags;
139 140 141
};

struct freelQ_e {
S
Scott Bardone 已提交
142 143 144 145
	u32 len_gen;
	u32 addr_lo;
	u32 addr_hi;
	u32 gen2;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
};

struct respQ_e {
	u32 BufferLength;
	u32 GenerationBit	: 1;
	u32 Sop			: 1;
	u32 Eop			: 1;
	u32 Offload		: 1;
	u32 DataValid		: 1;
	u32 CreditValid		: 1;
	u32 FreelistQid		: 2;
	u32 Cmdq0DmaComplete	: 5;
	u32 Cmdq0CreditReturn	: 5;
	u32 Cmdq1DmaComplete	: 5;
	u32 Cmdq1CreditReturn	: 5;
	u32 Qsleeping		: 4;
} ;
#endif

/*
 * SW Context Command and Freelist Queue Descriptors
 */
struct cmdQ_ce {
	struct sk_buff *skb;
	DECLARE_PCI_UNMAP_ADDR(dma_addr);
	DECLARE_PCI_UNMAP_LEN(dma_len);
};

struct freelQ_ce {
	struct sk_buff *skb;
	DECLARE_PCI_UNMAP_ADDR(dma_addr);
	DECLARE_PCI_UNMAP_LEN(dma_len);
};

/*
S
Scott Bardone 已提交
181
 * SW command, freelist and response rings
182 183
 */
struct cmdQ {
S
Scott Bardone 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197
	unsigned long   status;         /* HW DMA fetch status */
	unsigned int    in_use;         /* # of in-use command descriptors */
	unsigned int	size;	        /* # of descriptors */
	unsigned int	processed;      /* total # of descs HW has processed */
	unsigned int	cleaned;        /* total # of descs SW has reclaimed */
	unsigned int	stop_thres;     /* SW TX queue suspend threshold */
	u16		pidx;           /* producer index (SW) */
	u16		cidx;           /* consumer index (HW) */
	u8		genbit;         /* current generation (=valid) bit */
	u8		sop;            /* is next entry start of packet? */
	struct cmdQ_e  *entries;        /* HW command descriptor Q */
	struct cmdQ_ce *centries;       /* SW command context descriptor Q */
	spinlock_t	lock;           /* Lock to protect cmdQ enqueuing */
	dma_addr_t	dma_addr;       /* DMA addr HW command descriptor Q */
198 199 200
};

struct freelQ {
S
Scott Bardone 已提交
201 202 203 204
	unsigned int	credits;        /* # of available RX buffers */
	unsigned int	size;	        /* free list capacity */
	u16		pidx;           /* producer index (SW) */
	u16		cidx;           /* consumer index (HW) */
205 206
	u16		rx_buffer_size; /* Buffer size on this free list */
	u16		dma_offset;     /* DMA offset to align IP headers */
S
Scott Bardone 已提交
207 208 209 210 211
	u16		recycleq_idx;   /* skb recycle q to use */
	u8		genbit;	        /* current generation (=valid) bit */
	struct freelQ_e	*entries;       /* HW freelist descriptor Q */
	struct freelQ_ce *centries;     /* SW freelist context descriptor Q */
	dma_addr_t	dma_addr;       /* DMA addr HW freelist descriptor Q */
212 213 214
};

struct respQ {
S
Scott Bardone 已提交
215 216 217 218
	unsigned int	credits;        /* credits to be returned to SGE */
	unsigned int	size;	        /* # of response Q descriptors */
	u16		cidx;	        /* consumer index (SW) */
	u8		genbit;	        /* current generation(=valid) bit */
219
	struct respQ_e *entries;        /* HW response descriptor Q */
S
Scott Bardone 已提交
220 221 222 223 224 225 226
	dma_addr_t	dma_addr;       /* DMA addr HW response descriptor Q */
};

/* Bit flags for cmdQ.status */
enum {
	CMDQ_STAT_RUNNING = 1,          /* fetch engine is running */
	CMDQ_STAT_LAST_PKT_DB = 2       /* last packet rung the doorbell */
227 228 229 230 231 232 233 234 235 236 237 238
};

/*
 * Main SGE data structure
 *
 * Interrupts are handled by a single CPU and it is likely that on a MP system
 * the application is migrated to another CPU. In that scenario, we try to
 * seperate the RX(in irq context) and TX state in order to decrease memory
 * contention.
 */
struct sge {
	struct adapter *adapter; 	/* adapter backpointer */
S
Scott Bardone 已提交
239 240 241 242
	struct net_device *netdev;      /* netdevice backpointer */
	struct freelQ 	freelQ[SGE_FREELQ_N]; /* buffer free lists */
	struct respQ 	respQ;		/* response Q */
	unsigned long   stopped_tx_queues; /* bitmap of suspended Tx queues */
243 244
	unsigned int	rx_pkt_pad;     /* RX padding for L2 packets */
	unsigned int	jumbo_fl;       /* jumbo freelist Q index */
S
Scott Bardone 已提交
245 246 247 248 249 250 251 252 253 254
	unsigned int	intrtimer_nres;	/* no-resource interrupt timer */
	unsigned int	fixed_intrtimer;/* non-adaptive interrupt timer */
	struct timer_list tx_reclaim_timer; /* reclaims TX buffers */
	struct timer_list espibug_timer;
	unsigned int	espibug_timeout;
	struct sk_buff	*espibug_skb;
	u32		sge_control;	/* shadow value of sge control reg */
	struct sge_intr_counts stats;
	struct sge_port_stats port_stats[MAX_NPORTS];
	struct cmdQ cmdQ[SGE_CMDQ_N] ____cacheline_aligned_in_smp;
255 256 257 258 259
};

/*
 * PIO to indicate that memory mapped Q contains valid descriptor(s).
 */
S
Scott Bardone 已提交
260
static inline void doorbell_pio(struct adapter *adapter, u32 val)
261 262
{
	wmb();
S
Scott Bardone 已提交
263
	writel(val, adapter->regs + A_SG_DOORBELL);
264 265 266 267 268 269
}

/*
 * Frees all RX buffers on the freelist Q. The caller must make sure that
 * the SGE is turned off before calling this function.
 */
S
Scott Bardone 已提交
270
static void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *q)
271
{
S
Scott Bardone 已提交
272
	unsigned int cidx = q->cidx;
273

S
Scott Bardone 已提交
274 275
	while (q->credits--) {
		struct freelQ_ce *ce = &q->centries[cidx];
276 277 278 279 280 281

		pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
				 pci_unmap_len(ce, dma_len),
				 PCI_DMA_FROMDEVICE);
		dev_kfree_skb(ce->skb);
		ce->skb = NULL;
S
Scott Bardone 已提交
282
		if (++cidx == q->size)
283 284 285 286 287 288 289 290 291 292 293 294 295
			cidx = 0;
	}
}

/*
 * Free RX free list and response queue resources.
 */
static void free_rx_resources(struct sge *sge)
{
	struct pci_dev *pdev = sge->adapter->pdev;
	unsigned int size, i;

	if (sge->respQ.entries) {
S
Scott Bardone 已提交
296
		size = sizeof(struct respQ_e) * sge->respQ.size;
297 298 299 300 301
		pci_free_consistent(pdev, size, sge->respQ.entries,
				    sge->respQ.dma_addr);
	}

	for (i = 0; i < SGE_FREELQ_N; i++) {
S
Scott Bardone 已提交
302
		struct freelQ *q = &sge->freelQ[i];
303

S
Scott Bardone 已提交
304 305 306
		if (q->centries) {
			free_freelQ_buffers(pdev, q);
			kfree(q->centries);
307
		}
S
Scott Bardone 已提交
308 309 310 311
		if (q->entries) {
			size = sizeof(struct freelQ_e) * q->size;
			pci_free_consistent(pdev, size, q->entries,
					    q->dma_addr);
312 313 314 315 316 317
		}
	}
}

/*
 * Allocates basic RX resources, consisting of memory mapped freelist Qs and a
S
Scott Bardone 已提交
318
 * response queue.
319 320 321 322 323 324 325
 */
static int alloc_rx_resources(struct sge *sge, struct sge_params *p)
{
	struct pci_dev *pdev = sge->adapter->pdev;
	unsigned int size, i;

	for (i = 0; i < SGE_FREELQ_N; i++) {
S
Scott Bardone 已提交
326 327 328 329 330 331 332 333 334
		struct freelQ *q = &sge->freelQ[i];

		q->genbit = 1;
		q->size = p->freelQ_size[i];
		q->dma_offset = sge->rx_pkt_pad ? 0 : NET_IP_ALIGN;
		size = sizeof(struct freelQ_e) * q->size;
		q->entries = (struct freelQ_e *)
			      pci_alloc_consistent(pdev, size, &q->dma_addr);
		if (!q->entries)
335
			goto err_no_mem;
S
Scott Bardone 已提交
336 337 338 339
		memset(q->entries, 0, size);
		size = sizeof(struct freelQ_ce) * q->size;
		q->centries = kmalloc(size, GFP_KERNEL);
		if (!q->centries)
340
			goto err_no_mem;
S
Scott Bardone 已提交
341
		memset(q->centries, 0, size);
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	}

	/*
	 * Calculate the buffer sizes for the two free lists.  FL0 accommodates
	 * regular sized Ethernet frames, FL1 is sized not to exceed 16K,
	 * including all the sk_buff overhead.
	 *
	 * Note: For T2 FL0 and FL1 are reversed.
	 */
	sge->freelQ[!sge->jumbo_fl].rx_buffer_size = SGE_RX_SM_BUF_SIZE +
		sizeof(struct cpl_rx_data) +
		sge->freelQ[!sge->jumbo_fl].dma_offset;
	sge->freelQ[sge->jumbo_fl].rx_buffer_size = (16 * 1024) -
		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));

S
Scott Bardone 已提交
357 358 359 360 361 362 363
	/*
	 * Setup which skb recycle Q should be used when recycling buffers from
	 * each free list.
	 */
	sge->freelQ[!sge->jumbo_fl].recycleq_idx = 0;
	sge->freelQ[sge->jumbo_fl].recycleq_idx = 1;

364
	sge->respQ.genbit = 1;
S
Scott Bardone 已提交
365 366 367
	sge->respQ.size = SGE_RESPQ_E_N;
	sge->respQ.credits = 0;
	size = sizeof(struct respQ_e) * sge->respQ.size;
368 369 370 371 372 373 374 375 376 377 378 379 380
	sge->respQ.entries = (struct respQ_e *)
		pci_alloc_consistent(pdev, size, &sge->respQ.dma_addr);
	if (!sge->respQ.entries)
		goto err_no_mem;
	memset(sge->respQ.entries, 0, size);
	return 0;

err_no_mem:
	free_rx_resources(sge);
	return -ENOMEM;
}

/*
S
Scott Bardone 已提交
381
 * Reclaims n TX descriptors and frees the buffers associated with them.
382
 */
S
Scott Bardone 已提交
383
static void free_cmdQ_buffers(struct sge *sge, struct cmdQ *q, unsigned int n)
384
{
S
Scott Bardone 已提交
385
	struct cmdQ_ce *ce;
386
	struct pci_dev *pdev = sge->adapter->pdev;
S
Scott Bardone 已提交
387
	unsigned int cidx = q->cidx;
388

S
Scott Bardone 已提交
389 390 391 392
	q->in_use -= n;
	ce = &q->centries[cidx];
	while (n--) {
		if (q->sop)
393
			pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
S
Scott Bardone 已提交
394
			 		 pci_unmap_len(ce, dma_len),
395 396 397
					 PCI_DMA_TODEVICE);
		else
			pci_unmap_page(pdev, pci_unmap_addr(ce, dma_addr),
S
Scott Bardone 已提交
398
			 	       pci_unmap_len(ce, dma_len),
399
				       PCI_DMA_TODEVICE);
S
Scott Bardone 已提交
400 401 402 403 404
		q->sop = 0;
		if (ce->skb) {
			dev_kfree_skb(ce->skb);
			q->sop = 1;
		}
405
		ce++;
S
Scott Bardone 已提交
406
		if (++cidx == q->size) {
407
			cidx = 0;
S
Scott Bardone 已提交
408
			ce = q->centries;
409 410
		}
	}
S
Scott Bardone 已提交
411
	q->cidx = cidx;
412 413 414 415 416 417 418 419 420 421 422 423 424
}

/*
 * Free TX resources.
 *
 * Assumes that SGE is stopped and all interrupts are disabled.
 */
static void free_tx_resources(struct sge *sge)
{
	struct pci_dev *pdev = sge->adapter->pdev;
	unsigned int size, i;

	for (i = 0; i < SGE_CMDQ_N; i++) {
S
Scott Bardone 已提交
425
		struct cmdQ *q = &sge->cmdQ[i];
426

S
Scott Bardone 已提交
427 428 429 430
		if (q->centries) {
			if (q->in_use)
				free_cmdQ_buffers(sge, q, q->in_use);
			kfree(q->centries);
431
		}
S
Scott Bardone 已提交
432 433 434 435
		if (q->entries) {
			size = sizeof(struct cmdQ_e) * q->size;
			pci_free_consistent(pdev, size, q->entries,
					    q->dma_addr);
436 437 438 439 440 441 442 443 444 445 446 447 448
		}
	}
}

/*
 * Allocates basic TX resources, consisting of memory mapped command Qs.
 */
static int alloc_tx_resources(struct sge *sge, struct sge_params *p)
{
	struct pci_dev *pdev = sge->adapter->pdev;
	unsigned int size, i;

	for (i = 0; i < SGE_CMDQ_N; i++) {
S
Scott Bardone 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462
		struct cmdQ *q = &sge->cmdQ[i];

		q->genbit = 1;
		q->sop = 1;
		q->size = p->cmdQ_size[i];
		q->in_use = 0;
		q->status = 0;
		q->processed = q->cleaned = 0;
		q->stop_thres = 0;
		spin_lock_init(&q->lock);
		size = sizeof(struct cmdQ_e) * q->size;
		q->entries = (struct cmdQ_e *)
			      pci_alloc_consistent(pdev, size, &q->dma_addr);
		if (!q->entries)
463
			goto err_no_mem;
S
Scott Bardone 已提交
464 465 466 467
		memset(q->entries, 0, size);
		size = sizeof(struct cmdQ_ce) * q->size;
		q->centries = kmalloc(size, GFP_KERNEL);
		if (!q->centries)
468
			goto err_no_mem;
S
Scott Bardone 已提交
469
		memset(q->centries, 0, size);
470 471
	}

S
Scott Bardone 已提交
472 473 474 475 476 477 478 479 480
	/*
	 * CommandQ 0 handles Ethernet and TOE packets, while queue 1 is TOE
	 * only.  For queue 0 set the stop threshold so we can handle one more
	 * packet from each port, plus reserve an additional 24 entries for
	 * Ethernet packets only.  Queue 1 never suspends nor do we reserve
	 * space for Ethernet packets.
	 */
	sge->cmdQ[0].stop_thres = sge->adapter->params.nports *
		(MAX_SKB_FRAGS + 1);
481 482 483 484 485 486 487 488 489 490 491
	return 0;

err_no_mem:
	free_tx_resources(sge);
	return -ENOMEM;
}

static inline void setup_ring_params(struct adapter *adapter, u64 addr,
				     u32 size, int base_reg_lo,
				     int base_reg_hi, int size_reg)
{
S
Scott Bardone 已提交
492 493 494
	writel((u32)addr, adapter->regs + base_reg_lo);
	writel(addr >> 32, adapter->regs + base_reg_hi);
	writel(size, adapter->regs + size_reg);
495 496 497 498 499 500 501 502 503 504 505 506 507
}

/*
 * Enable/disable VLAN acceleration.
 */
void t1_set_vlan_accel(struct adapter *adapter, int on_off)
{
	struct sge *sge = adapter->sge;

	sge->sge_control &= ~F_VLAN_XTRACT;
	if (on_off)
		sge->sge_control |= F_VLAN_XTRACT;
	if (adapter->open_device_map) {
S
Scott Bardone 已提交
508 509
		writel(sge->sge_control, adapter->regs + A_SG_CONTROL);
		readl(adapter->regs + A_SG_CONTROL); /* flush */
510 511 512 513 514 515 516 517 518 519
	}
}

/*
 * Programs the various SGE registers. However, the engine is not yet enabled,
 * but sge->sge_control is setup and ready to go.
 */
static void configure_sge(struct sge *sge, struct sge_params *p)
{
	struct adapter *ap = sge->adapter;
S
Scott Bardone 已提交
520 521 522
	
	writel(0, ap->regs + A_SG_CONTROL);
	setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
523
			  A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
S
Scott Bardone 已提交
524
	setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
525 526
			  A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
	setup_ring_params(ap, sge->freelQ[0].dma_addr,
S
Scott Bardone 已提交
527
			  sge->freelQ[0].size, A_SG_FL0BASELWR,
528 529
			  A_SG_FL0BASEUPR, A_SG_FL0SIZE);
	setup_ring_params(ap, sge->freelQ[1].dma_addr,
S
Scott Bardone 已提交
530
			  sge->freelQ[1].size, A_SG_FL1BASELWR,
531 532 533
			  A_SG_FL1BASEUPR, A_SG_FL1SIZE);

	/* The threshold comparison uses <. */
S
Scott Bardone 已提交
534
	writel(SGE_RX_SM_BUF_SIZE + 1, ap->regs + A_SG_FLTHRESHOLD);
535

S
Scott Bardone 已提交
536 537 538
	setup_ring_params(ap, sge->respQ.dma_addr, sge->respQ.size,
			  A_SG_RSPBASELWR, A_SG_RSPBASEUPR, A_SG_RSPSIZE);
	writel((u32)sge->respQ.size - 1, ap->regs + A_SG_RSPQUEUECREDIT);
539 540 541 542

	sge->sge_control = F_CMDQ0_ENABLE | F_CMDQ1_ENABLE | F_FL0_ENABLE |
		F_FL1_ENABLE | F_CPL_ENABLE | F_RESPONSE_QUEUE_ENABLE |
		V_CMDQ_PRIORITY(2) | F_DISABLE_CMDQ1_GTS | F_ISCSI_COALESCE |
S
Scott Bardone 已提交
543
		F_DISABLE_FL0_GTS | F_DISABLE_FL1_GTS |
544 545 546 547 548 549
		V_RX_PKT_OFFSET(sge->rx_pkt_pad);

#if defined(__BIG_ENDIAN_BITFIELD)
	sge->sge_control |= F_ENABLE_BIG_ENDIAN;
#endif

S
Scott Bardone 已提交
550 551 552 553
	/* Initialize no-resource timer */
	sge->intrtimer_nres = SGE_INTRTIMER_NRES * core_ticks_per_usec(ap);

	t1_sge_set_coalesce_params(sge, p);
554 555 556 557 558 559 560 561
}

/*
 * Return the payload capacity of the jumbo free-list buffers.
 */
static inline unsigned int jumbo_payload_capacity(const struct sge *sge)
{
	return sge->freelQ[sge->jumbo_fl].rx_buffer_size -
S
Scott Bardone 已提交
562 563
		sge->freelQ[sge->jumbo_fl].dma_offset -
		sizeof(struct cpl_rx_data);
564 565 566 567 568 569 570
}

/*
 * Frees all SGE related resources and the sge structure itself
 */
void t1_sge_destroy(struct sge *sge)
{
S
Scott Bardone 已提交
571 572 573
	if (sge->espibug_skb)
		kfree_skb(sge->espibug_skb);

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	free_tx_resources(sge);
	free_rx_resources(sge);
	kfree(sge);
}

/*
 * Allocates new RX buffers on the freelist Q (and tracks them on the freelist
 * context Q) until the Q is full or alloc_skb fails.
 *
 * It is possible that the generation bits already match, indicating that the
 * buffer is already valid and nothing needs to be done. This happens when we
 * copied a received buffer into a new sk_buff during the interrupt processing.
 *
 * If the SGE doesn't automatically align packets properly (!sge->rx_pkt_pad),
 * we specify a RX_OFFSET in order to make sure that the IP header is 4B
 * aligned.
 */
S
Scott Bardone 已提交
591
static void refill_free_list(struct sge *sge, struct freelQ *q)
592 593
{
	struct pci_dev *pdev = sge->adapter->pdev;
S
Scott Bardone 已提交
594 595 596
	struct freelQ_ce *ce = &q->centries[q->pidx];
	struct freelQ_e *e = &q->entries[q->pidx];
	unsigned int dma_len = q->rx_buffer_size - q->dma_offset;
597 598


S
Scott Bardone 已提交
599 600 601
	while (q->credits < q->size) {
		struct sk_buff *skb;
		dma_addr_t mapping;
602

S
Scott Bardone 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
		skb = alloc_skb(q->rx_buffer_size, GFP_ATOMIC);
		if (!skb)
			break;

		skb_reserve(skb, q->dma_offset);
		mapping = pci_map_single(pdev, skb->data, dma_len,
					 PCI_DMA_FROMDEVICE);
		ce->skb = skb;
		pci_unmap_addr_set(ce, dma_addr, mapping);
		pci_unmap_len_set(ce, dma_len, dma_len);
		e->addr_lo = (u32)mapping;
		e->addr_hi = (u64)mapping >> 32;
		e->len_gen = V_CMD_LEN(dma_len) | V_CMD_GEN1(q->genbit);
		wmb();
		e->gen2 = V_CMD_GEN2(q->genbit);
618 619 620

		e++;
		ce++;
S
Scott Bardone 已提交
621 622 623 624 625
		if (++q->pidx == q->size) {
			q->pidx = 0;
			q->genbit ^= 1;
			ce = q->centries;
			e = q->entries;
626
		}
S
Scott Bardone 已提交
627
		q->credits++;
628 629 630 631 632
	}

}

/*
S
Scott Bardone 已提交
633 634 635
 * Calls refill_free_list for both free lists. If we cannot fill at least 1/4
 * of both rings, we go into 'few interrupt mode' in order to give the system
 * time to free up resources.
636 637 638
 */
static void freelQs_empty(struct sge *sge)
{
S
Scott Bardone 已提交
639 640
	struct adapter *adapter = sge->adapter;
	u32 irq_reg = readl(adapter->regs + A_SG_INT_ENABLE);
641 642 643 644 645
	u32 irqholdoff_reg;

	refill_free_list(sge, &sge->freelQ[0]);
	refill_free_list(sge, &sge->freelQ[1]);

S
Scott Bardone 已提交
646 647
	if (sge->freelQ[0].credits > (sge->freelQ[0].size >> 2) &&
	    sge->freelQ[1].credits > (sge->freelQ[1].size >> 2)) {
648
		irq_reg |= F_FL_EXHAUSTED;
S
Scott Bardone 已提交
649
		irqholdoff_reg = sge->fixed_intrtimer;
650 651 652 653 654
	} else {
		/* Clear the F_FL_EXHAUSTED interrupts for now */
		irq_reg &= ~F_FL_EXHAUSTED;
		irqholdoff_reg = sge->intrtimer_nres;
	}
S
Scott Bardone 已提交
655 656
	writel(irqholdoff_reg, adapter->regs + A_SG_INTRTIMER);
	writel(irq_reg, adapter->regs + A_SG_INT_ENABLE);
657 658

	/* We reenable the Qs to force a freelist GTS interrupt later */
S
Scott Bardone 已提交
659
	doorbell_pio(adapter, F_FL0_ENABLE | F_FL1_ENABLE);
660 661 662 663 664 665 666 667 668 669 670 671
}

#define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
#define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
#define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
			F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)

/*
 * Disable SGE Interrupts
 */
void t1_sge_intr_disable(struct sge *sge)
{
S
Scott Bardone 已提交
672
	u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
673

S
Scott Bardone 已提交
674 675
	writel(val & ~SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
	writel(0, sge->adapter->regs + A_SG_INT_ENABLE);
676 677 678 679 680 681 682 683
}

/*
 * Enable SGE interrupts.
 */
void t1_sge_intr_enable(struct sge *sge)
{
	u32 en = SGE_INT_ENABLE;
S
Scott Bardone 已提交
684
	u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
685 686 687

	if (sge->adapter->flags & TSO_CAPABLE)
		en &= ~F_PACKET_TOO_BIG;
S
Scott Bardone 已提交
688 689
	writel(en, sge->adapter->regs + A_SG_INT_ENABLE);
	writel(val | SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
690 691 692 693 694 695 696
}

/*
 * Clear SGE interrupts.
 */
void t1_sge_intr_clear(struct sge *sge)
{
S
Scott Bardone 已提交
697 698
	writel(SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_CAUSE);
	writel(0xffffffff, sge->adapter->regs + A_SG_INT_CAUSE);
699 700 701 702 703 704 705 706
}

/*
 * SGE 'Error' interrupt handler
 */
int t1_sge_intr_error_handler(struct sge *sge)
{
	struct adapter *adapter = sge->adapter;
S
Scott Bardone 已提交
707
	u32 cause = readl(adapter->regs + A_SG_INT_CAUSE);
708 709 710 711

	if (adapter->flags & TSO_CAPABLE)
		cause &= ~F_PACKET_TOO_BIG;
	if (cause & F_RESPQ_EXHAUSTED)
S
Scott Bardone 已提交
712
		sge->stats.respQ_empty++;
713
	if (cause & F_RESPQ_OVERFLOW) {
S
Scott Bardone 已提交
714
		sge->stats.respQ_overflow++;
715 716 717 718
		CH_ALERT("%s: SGE response queue overflow\n",
			 adapter->name);
	}
	if (cause & F_FL_EXHAUSTED) {
S
Scott Bardone 已提交
719
		sge->stats.freelistQ_empty++;
720 721 722
		freelQs_empty(sge);
	}
	if (cause & F_PACKET_TOO_BIG) {
S
Scott Bardone 已提交
723
		sge->stats.pkt_too_big++;
724 725 726 727
		CH_ALERT("%s: SGE max packet size exceeded\n",
			 adapter->name);
	}
	if (cause & F_PACKET_MISMATCH) {
S
Scott Bardone 已提交
728
		sge->stats.pkt_mismatch++;
729 730 731 732 733
		CH_ALERT("%s: SGE packet mismatch\n", adapter->name);
	}
	if (cause & SGE_INT_FATAL)
		t1_fatal_err(adapter);

S
Scott Bardone 已提交
734
	writel(cause, adapter->regs + A_SG_INT_CAUSE);
735 736 737
	return 0;
}

S
Scott Bardone 已提交
738 739 740 741 742 743 744 745 746 747 748 749 750 751
const struct sge_intr_counts *t1_sge_get_intr_counts(struct sge *sge)
{
	return &sge->stats;
}

const struct sge_port_stats *t1_sge_get_port_stats(struct sge *sge, int port)
{
	return &sge->port_stats[port];
}

/**
 *	recycle_fl_buf - recycle a free list buffer
 *	@fl: the free list
 *	@idx: index of buffer to recycle
752
 *
S
Scott Bardone 已提交
753 754
 *	Recycles the specified buffer on the given free list by adding it at
 *	the next available slot on the list.
755
 */
S
Scott Bardone 已提交
756
static void recycle_fl_buf(struct freelQ *fl, int idx)
757
{
S
Scott Bardone 已提交
758 759
	struct freelQ_e *from = &fl->entries[idx];
	struct freelQ_e *to = &fl->entries[fl->pidx];
760

S
Scott Bardone 已提交
761 762 763 764 765 766 767
	fl->centries[fl->pidx] = fl->centries[idx];
	to->addr_lo = from->addr_lo;
	to->addr_hi = from->addr_hi;
	to->len_gen = G_CMD_LEN(from->len_gen) | V_CMD_GEN1(fl->genbit);
	wmb();
	to->gen2 = V_CMD_GEN2(fl->genbit);
	fl->credits++;
768

S
Scott Bardone 已提交
769 770 771
	if (++fl->pidx == fl->size) {
		fl->pidx = 0;
		fl->genbit ^= 1;
772
	}
S
Scott Bardone 已提交
773
}
774

S
Scott Bardone 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
/**
 *	get_packet - return the next ingress packet buffer
 *	@pdev: the PCI device that received the packet
 *	@fl: the SGE free list holding the packet
 *	@len: the actual packet length, excluding any SGE padding
 *	@dma_pad: padding at beginning of buffer left by SGE DMA
 *	@skb_pad: padding to be used if the packet is copied
 *	@copy_thres: length threshold under which a packet should be copied
 *	@drop_thres: # of remaining buffers before we start dropping packets
 *
 *	Get the next packet from a free list and complete setup of the
 *	sk_buff.  If the packet is small we make a copy and recycle the
 *	original buffer, otherwise we use the original buffer itself.  If a
 *	positive drop threshold is supplied packets are dropped and their
 *	buffers recycled if (a) the number of remaining buffers is under the
 *	threshold and the packet is too big to copy, or (b) the packet should
 *	be copied but there is no memory for the copy.
 */
static inline struct sk_buff *get_packet(struct pci_dev *pdev,
					 struct freelQ *fl, unsigned int len,
					 int dma_pad, int skb_pad,
					 unsigned int copy_thres,
					 unsigned int drop_thres)
{
	struct sk_buff *skb;
	struct freelQ_ce *ce = &fl->centries[fl->cidx];

	if (len < copy_thres) {
		skb = alloc_skb(len + skb_pad, GFP_ATOMIC);
		if (likely(skb != NULL)) {
			skb_reserve(skb, skb_pad);
			skb_put(skb, len);
			pci_dma_sync_single_for_cpu(pdev,
					    pci_unmap_addr(ce, dma_addr),
 					    pci_unmap_len(ce, dma_len),
					    PCI_DMA_FROMDEVICE);
			memcpy(skb->data, ce->skb->data + dma_pad, len);
			pci_dma_sync_single_for_device(pdev,
					    pci_unmap_addr(ce, dma_addr),
 					    pci_unmap_len(ce, dma_len),
					    PCI_DMA_FROMDEVICE);
		} else if (!drop_thres)
			goto use_orig_buf;
818

S
Scott Bardone 已提交
819 820
		recycle_fl_buf(fl, fl->cidx);
		return skb;
821 822
	}

S
Scott Bardone 已提交
823 824 825 826
	if (fl->credits < drop_thres) {
		recycle_fl_buf(fl, fl->cidx);
		return NULL;
	}
827

S
Scott Bardone 已提交
828 829 830 831 832 833 834 835
use_orig_buf:
	pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
			 pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
	skb = ce->skb;
	skb_reserve(skb, dma_pad);
	skb_put(skb, len);
	return skb;
}
836

S
Scott Bardone 已提交
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
/**
 *	unexpected_offload - handle an unexpected offload packet
 *	@adapter: the adapter
 *	@fl: the free list that received the packet
 *
 *	Called when we receive an unexpected offload packet (e.g., the TOE
 *	function is disabled or the card is a NIC).  Prints a message and
 *	recycles the buffer.
 */
static void unexpected_offload(struct adapter *adapter, struct freelQ *fl)
{
	struct freelQ_ce *ce = &fl->centries[fl->cidx];
	struct sk_buff *skb = ce->skb;

	pci_dma_sync_single_for_cpu(adapter->pdev, pci_unmap_addr(ce, dma_addr),
			    pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
	CH_ERR("%s: unexpected offload packet, cmd %u\n",
	       adapter->name, *skb->data);
	recycle_fl_buf(fl, fl->cidx);
856 857 858
}

/*
S
Scott Bardone 已提交
859 860
 * Write the command descriptors to transmit the given skb starting at
 * descriptor pidx with the given generation.
861
 */
S
Scott Bardone 已提交
862 863 864
static inline void write_tx_descs(struct adapter *adapter, struct sk_buff *skb,
				  unsigned int pidx, unsigned int gen,
				  struct cmdQ *q)
865
{
S
Scott Bardone 已提交
866 867 868 869 870 871 872 873 874 875 876
	dma_addr_t mapping;
	struct cmdQ_e *e, *e1;
	struct cmdQ_ce *ce;
	unsigned int i, flags, nfrags = skb_shinfo(skb)->nr_frags;

	mapping = pci_map_single(adapter->pdev, skb->data,
				 skb->len - skb->data_len, PCI_DMA_TODEVICE);
	ce = &q->centries[pidx];
	ce->skb = NULL;
	pci_unmap_addr_set(ce, dma_addr, mapping);
	pci_unmap_len_set(ce, dma_len, skb->len - skb->data_len);
877

S
Scott Bardone 已提交
878 879 880 881 882 883 884 885
	flags = F_CMD_DATAVALID | F_CMD_SOP | V_CMD_EOP(nfrags == 0) |
		V_CMD_GEN2(gen);
	e = &q->entries[pidx];
	e->addr_lo = (u32)mapping;
	e->addr_hi = (u64)mapping >> 32;
	e->len_gen = V_CMD_LEN(skb->len - skb->data_len) | V_CMD_GEN1(gen);
	for (e1 = e, i = 0; nfrags--; i++) {
		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
886

S
Scott Bardone 已提交
887 888 889 890 891 892 893
		ce++;
		e1++;
		if (++pidx == q->size) {
			pidx = 0;
			gen ^= 1;
			ce = q->centries;
			e1 = q->entries;
894 895
		}

S
Scott Bardone 已提交
896 897 898 899 900 901 902 903 904 905 906 907
		mapping = pci_map_page(adapter->pdev, frag->page,
				       frag->page_offset, frag->size,
				       PCI_DMA_TODEVICE);
		ce->skb = NULL;
		pci_unmap_addr_set(ce, dma_addr, mapping);
		pci_unmap_len_set(ce, dma_len, frag->size);

		e1->addr_lo = (u32)mapping;
		e1->addr_hi = (u64)mapping >> 32;
		e1->len_gen = V_CMD_LEN(frag->size) | V_CMD_GEN1(gen);
		e1->flags = F_CMD_DATAVALID | V_CMD_EOP(nfrags == 0) |
			    V_CMD_GEN2(gen);
908 909
	}

S
Scott Bardone 已提交
910 911 912 913
	ce->skb = skb;
	wmb();
	e->flags = flags;
}
914

S
Scott Bardone 已提交
915 916 917 918 919 920
/*
 * Clean up completed Tx buffers.
 */
static inline void reclaim_completed_tx(struct sge *sge, struct cmdQ *q)
{
	unsigned int reclaim = q->processed - q->cleaned;
921

S
Scott Bardone 已提交
922 923 924
	if (reclaim) {
		free_cmdQ_buffers(sge, q, reclaim);
		q->cleaned += reclaim;
925
	}
S
Scott Bardone 已提交
926
}
927

S
Scott Bardone 已提交
928 929 930
#ifndef SET_ETHTOOL_OPS
# define __netif_rx_complete(dev) netif_rx_complete(dev)
#endif
931

S
Scott Bardone 已提交
932 933 934 935 936 937 938 939 940
/*
 * We cannot use the standard netif_rx_schedule_prep() because we have multiple
 * ports plus the TOE all multiplexing onto a single response queue, therefore
 * accepting new responses cannot depend on the state of any particular port.
 * So define our own equivalent that omits the netif_running() test.
 */
static inline int napi_schedule_prep(struct net_device *dev)
{
	return !test_and_set_bit(__LINK_STATE_RX_SCHED, &dev->state);
941 942 943
}


S
Scott Bardone 已提交
944 945 946 947 948
/**
 *	sge_rx - process an ingress ethernet packet
 *	@sge: the sge structure
 *	@fl: the free list that contains the packet buffer
 *	@len: the packet length
949
 *
S
Scott Bardone 已提交
950
 *	Process an ingress ethernet pakcet and deliver it to the stack.
951
 */
S
Scott Bardone 已提交
952
static int sge_rx(struct sge *sge, struct freelQ *fl, unsigned int len)
953
{
S
Scott Bardone 已提交
954 955 956
	struct sk_buff *skb;
	struct cpl_rx_pkt *p;
	struct adapter *adapter = sge->adapter;
957

S
Scott Bardone 已提交
958 959 960 961 962 963 964
	sge->stats.ethernet_pkts++;
	skb = get_packet(adapter->pdev, fl, len - sge->rx_pkt_pad,
			 sge->rx_pkt_pad, 2, SGE_RX_COPY_THRES,
			 SGE_RX_DROP_THRES);
	if (!skb) {
		sge->port_stats[0].rx_drops++; /* charge only port 0 for now */
		return 0;
965
	}
S
Scott Bardone 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992

	p = (struct cpl_rx_pkt *)skb->data;
	skb_pull(skb, sizeof(*p));
	skb->dev = adapter->port[p->iff].dev;
	skb->dev->last_rx = jiffies;
	skb->protocol = eth_type_trans(skb, skb->dev);
	if ((adapter->flags & RX_CSUM_ENABLED) && p->csum == 0xffff &&
	    skb->protocol == htons(ETH_P_IP) &&
	    (skb->data[9] == IPPROTO_TCP || skb->data[9] == IPPROTO_UDP)) {
		sge->port_stats[p->iff].rx_cso_good++;
		skb->ip_summed = CHECKSUM_UNNECESSARY;
	} else
		skb->ip_summed = CHECKSUM_NONE;

	if (unlikely(adapter->vlan_grp && p->vlan_valid)) {
		sge->port_stats[p->iff].vlan_xtract++;
		if (adapter->params.sge.polling)
			vlan_hwaccel_receive_skb(skb, adapter->vlan_grp,
						 ntohs(p->vlan));
		else
			vlan_hwaccel_rx(skb, adapter->vlan_grp,
					ntohs(p->vlan));
	} else if (adapter->params.sge.polling)
		netif_receive_skb(skb);
	else
		netif_rx(skb);
	return 0;
993 994 995
}

/*
S
Scott Bardone 已提交
996
 * Returns true if a command queue has enough available descriptors that
997 998
 * we can resume Tx operation after temporarily disabling its packet queue.
 */
S
Scott Bardone 已提交
999
static inline int enough_free_Tx_descs(const struct cmdQ *q)
1000
{
S
Scott Bardone 已提交
1001 1002 1003
	unsigned int r = q->processed - q->cleaned;

	return q->in_use - r < (q->size >> 1);
1004 1005 1006
}

/*
S
Scott Bardone 已提交
1007 1008
 * Called when sufficient space has become available in the SGE command queues
 * after the Tx packet schedulers have been suspended to restart the Tx path.
1009
 */
S
Scott Bardone 已提交
1010
static void restart_tx_queues(struct sge *sge)
1011
{
S
Scott Bardone 已提交
1012
	struct adapter *adap = sge->adapter;
1013

S
Scott Bardone 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022
	if (enough_free_Tx_descs(&sge->cmdQ[0])) {
		int i;

		for_each_port(adap, i) {
			struct net_device *nd = adap->port[i].dev;

			if (test_and_clear_bit(nd->if_port,
					       &sge->stopped_tx_queues) &&
			    netif_running(nd)) {
1023
				sge->stats.cmdQ_restarted[2]++;
S
Scott Bardone 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
				netif_wake_queue(nd);
			}
		}
	}
}

/*
 * update_tx_info is called from the interrupt handler/NAPI to return cmdQ0 
 * information.
 */
static unsigned int update_tx_info(struct adapter *adapter, 
					  unsigned int flags, 
					  unsigned int pr0)
{
	struct sge *sge = adapter->sge;
	struct cmdQ *cmdq = &sge->cmdQ[0];
1040

S
Scott Bardone 已提交
1041
	cmdq->processed += pr0;
1042

S
Scott Bardone 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	if (flags & F_CMDQ0_ENABLE) {
		clear_bit(CMDQ_STAT_RUNNING, &cmdq->status);
	
		if (cmdq->cleaned + cmdq->in_use != cmdq->processed &&
		    !test_and_set_bit(CMDQ_STAT_LAST_PKT_DB, &cmdq->status)) {
			set_bit(CMDQ_STAT_RUNNING, &cmdq->status);
			writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
		}
	 	flags &= ~F_CMDQ0_ENABLE;
	}
	
	if (unlikely(sge->stopped_tx_queues != 0))
		restart_tx_queues(sge);
1056

S
Scott Bardone 已提交
1057 1058
	return flags;
}
1059

S
Scott Bardone 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
/*
 * Process SGE responses, up to the supplied budget.  Returns the number of
 * responses processed.  A negative budget is effectively unlimited.
 */
static int process_responses(struct adapter *adapter, int budget)
{
	struct sge *sge = adapter->sge;
	struct respQ *q = &sge->respQ;
	struct respQ_e *e = &q->entries[q->cidx];
	int budget_left = budget;
	unsigned int flags = 0;
	unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
	

	while (likely(budget_left && e->GenerationBit == q->genbit)) {
		flags |= e->Qsleeping;
		
		cmdq_processed[0] += e->Cmdq0CreditReturn;
		cmdq_processed[1] += e->Cmdq1CreditReturn;
		
		/* We batch updates to the TX side to avoid cacheline
		 * ping-pong of TX state information on MP where the sender
		 * might run on a different CPU than this function...
		 */
		if (unlikely(flags & F_CMDQ0_ENABLE || cmdq_processed[0] > 64)) {
			flags = update_tx_info(adapter, flags, cmdq_processed[0]);
			cmdq_processed[0] = 0;
		}
		if (unlikely(cmdq_processed[1] > 16)) {
			sge->cmdQ[1].processed += cmdq_processed[1];
			cmdq_processed[1] = 0;
1091 1092
		}
		if (likely(e->DataValid)) {
S
Scott Bardone 已提交
1093 1094
			struct freelQ *fl = &sge->freelQ[e->FreelistQid];

1095
			BUG_ON(!e->Sop || !e->Eop);
S
Scott Bardone 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
			if (unlikely(e->Offload))
				unexpected_offload(adapter, fl);
			else
				sge_rx(sge, fl, e->BufferLength);

			/*
			 * Note: this depends on each packet consuming a
			 * single free-list buffer; cf. the BUG above.
			 */
			if (++fl->cidx == fl->size)
				fl->cidx = 0;
			if (unlikely(--fl->credits <
				     fl->size - SGE_FREEL_REFILL_THRESH))
				refill_free_list(sge, fl);
		} else
			sge->stats.pure_rsps++;
1112 1113

		e++;
S
Scott Bardone 已提交
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
		if (unlikely(++q->cidx == q->size)) {
			q->cidx = 0;
			q->genbit ^= 1;
			e = q->entries;
		}
		prefetch(e);

		if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
			writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
			q->credits = 0;
1124
		}
S
Scott Bardone 已提交
1125
		--budget_left;
1126 1127
	}

S
Scott Bardone 已提交
1128 1129
	flags = update_tx_info(adapter, flags, cmdq_processed[0]); 
	sge->cmdQ[1].processed += cmdq_processed[1];
1130

S
Scott Bardone 已提交
1131 1132 1133
	budget -= budget_left;
	return budget;
}
1134

S
Scott Bardone 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
/*
 * A simpler version of process_responses() that handles only pure (i.e.,
 * non data-carrying) responses.  Such respones are too light-weight to justify
 * calling a softirq when using NAPI, so we handle them specially in hard
 * interrupt context.  The function is called with a pointer to a response,
 * which the caller must ensure is a valid pure response.  Returns 1 if it
 * encounters a valid data-carrying response, 0 otherwise.
 */
static int process_pure_responses(struct adapter *adapter, struct respQ_e *e)
{
	struct sge *sge = adapter->sge;
	struct respQ *q = &sge->respQ;
	unsigned int flags = 0;
	unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1149

S
Scott Bardone 已提交
1150 1151
	do {
		flags |= e->Qsleeping;
1152

S
Scott Bardone 已提交
1153 1154 1155 1156 1157 1158 1159 1160
		cmdq_processed[0] += e->Cmdq0CreditReturn;
		cmdq_processed[1] += e->Cmdq1CreditReturn;
		
		e++;
		if (unlikely(++q->cidx == q->size)) {
			q->cidx = 0;
			q->genbit ^= 1;
			e = q->entries;
1161
		}
S
Scott Bardone 已提交
1162
		prefetch(e);
1163

S
Scott Bardone 已提交
1164 1165 1166
		if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
			writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
			q->credits = 0;
1167
		}
S
Scott Bardone 已提交
1168 1169
		sge->stats.pure_rsps++;
	} while (e->GenerationBit == q->genbit && !e->DataValid);
1170

S
Scott Bardone 已提交
1171 1172
	flags = update_tx_info(adapter, flags, cmdq_processed[0]); 
	sge->cmdQ[1].processed += cmdq_processed[1];
1173

S
Scott Bardone 已提交
1174
	return e->GenerationBit == q->genbit;
1175 1176 1177
}

/*
S
Scott Bardone 已提交
1178 1179 1180
 * Handler for new data events when using NAPI.  This does not need any locking
 * or protection from interrupts as data interrupts are off at this point and
 * other adapter interrupts do not interfere.
1181
 */
S
Scott Bardone 已提交
1182
static int t1_poll(struct net_device *dev, int *budget)
1183
{
S
Scott Bardone 已提交
1184 1185 1186 1187 1188 1189
	struct adapter *adapter = dev->priv;
	int effective_budget = min(*budget, dev->quota);

	int work_done = process_responses(adapter, effective_budget);
	*budget -= work_done;
	dev->quota -= work_done;
1190

S
Scott Bardone 已提交
1191 1192 1193 1194
	if (work_done >= effective_budget)
		return 1;

	__netif_rx_complete(dev);
1195 1196

	/*
S
Scott Bardone 已提交
1197 1198 1199 1200 1201 1202 1203
	 * Because we don't atomically flush the following write it is
	 * possible that in very rare cases it can reach the device in a way
	 * that races with a new response being written plus an error interrupt
	 * causing the NAPI interrupt handler below to return unhandled status
	 * to the OS.  To protect against this would require flushing the write
	 * and doing both the write and the flush with interrupts off.  Way too
	 * expensive and unjustifiable given the rarity of the race.
1204
	 */
S
Scott Bardone 已提交
1205 1206 1207
	writel(adapter->sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
	return 0;
}
1208

S
Scott Bardone 已提交
1209 1210 1211 1212 1213 1214 1215
/*
 * Returns true if the device is already scheduled for polling.
 */
static inline int napi_is_scheduled(struct net_device *dev)
{
	return test_bit(__LINK_STATE_RX_SCHED, &dev->state);
}
1216

S
Scott Bardone 已提交
1217 1218 1219
/*
 * NAPI version of the main interrupt handler.
 */
1220
static irqreturn_t t1_interrupt_napi(int irq, void *data)
S
Scott Bardone 已提交
1221 1222 1223 1224 1225
{
	int handled;
	struct adapter *adapter = data;
	struct sge *sge = adapter->sge;
	struct respQ *q = &adapter->sge->respQ;
1226

S
Scott Bardone 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	/*
	 * Clear the SGE_DATA interrupt first thing.  Normally the NAPI
	 * handler has control of the response queue and the interrupt handler
	 * can look at the queue reliably only once it knows NAPI is off.
	 * We can't wait that long to clear the SGE_DATA interrupt because we
	 * could race with t1_poll rearming the SGE interrupt, so we need to
	 * clear the interrupt speculatively and really early on.
	 */
	writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);

	spin_lock(&adapter->async_lock);
	if (!napi_is_scheduled(sge->netdev)) {
		struct respQ_e *e = &q->entries[q->cidx];

		if (e->GenerationBit == q->genbit) {
			if (e->DataValid ||
			    process_pure_responses(adapter, e)) {
				if (likely(napi_schedule_prep(sge->netdev)))
					__netif_rx_schedule(sge->netdev);
				else
					printk(KERN_CRIT
					       "NAPI schedule failure!\n");
			} else
			writel(q->cidx, adapter->regs + A_SG_SLEEPING);
			handled = 1;
			goto unlock;
		} else
		writel(q->cidx, adapter->regs + A_SG_SLEEPING);
	}  else
	if (readl(adapter->regs + A_PL_CAUSE) & F_PL_INTR_SGE_DATA)
		printk(KERN_ERR "data interrupt while NAPI running\n");
	
	handled = t1_slow_intr_handler(adapter);
	if (!handled)
		sge->stats.unhandled_irqs++;
 unlock:
	spin_unlock(&adapter->async_lock);
	return IRQ_RETVAL(handled != 0);
}
1266

S
Scott Bardone 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
/*
 * Main interrupt handler, optimized assuming that we took a 'DATA'
 * interrupt.
 *
 * 1. Clear the interrupt
 * 2. Loop while we find valid descriptors and process them; accumulate
 *      information that can be processed after the loop
 * 3. Tell the SGE at which index we stopped processing descriptors
 * 4. Bookkeeping; free TX buffers, ring doorbell if there are any
 *      outstanding TX buffers waiting, replenish RX buffers, potentially
 *      reenable upper layers if they were turned off due to lack of TX
 *      resources which are available again.
 * 5. If we took an interrupt, but no valid respQ descriptors was found we
 *      let the slow_intr_handler run and do error handling.
 */
1282
static irqreturn_t t1_interrupt(int irq, void *cookie)
S
Scott Bardone 已提交
1283 1284 1285 1286 1287
{
	int work_done;
	struct respQ_e *e;
	struct adapter *adapter = cookie;
	struct respQ *Q = &adapter->sge->respQ;
1288

S
Scott Bardone 已提交
1289 1290 1291
	spin_lock(&adapter->async_lock);
	e = &Q->entries[Q->cidx];
	prefetch(e);
1292

S
Scott Bardone 已提交
1293
	writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
1294

S
Scott Bardone 已提交
1295 1296 1297 1298
	if (likely(e->GenerationBit == Q->genbit))
		work_done = process_responses(adapter, -1);
	else
		work_done = t1_slow_intr_handler(adapter);
1299

S
Scott Bardone 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	/*
	 * The unconditional clearing of the PL_CAUSE above may have raced
	 * with DMA completion and the corresponding generation of a response
	 * to cause us to miss the resulting data interrupt.  The next write
	 * is also unconditional to recover the missed interrupt and render
	 * this race harmless.
	 */
	writel(Q->cidx, adapter->regs + A_SG_SLEEPING);

	if (!work_done)
		adapter->sge->stats.unhandled_irqs++;
	spin_unlock(&adapter->async_lock);
	return IRQ_RETVAL(work_done != 0);
}

1315
irq_handler_t t1_select_intr_handler(adapter_t *adapter)
S
Scott Bardone 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
{
	return adapter->params.sge.polling ? t1_interrupt_napi : t1_interrupt;
}

/*
 * Enqueues the sk_buff onto the cmdQ[qid] and has hardware fetch it.
 *
 * The code figures out how many entries the sk_buff will require in the
 * cmdQ and updates the cmdQ data structure with the state once the enqueue
 * has complete. Then, it doesn't access the global structure anymore, but
 * uses the corresponding fields on the stack. In conjuction with a spinlock
 * around that code, we can make the function reentrant without holding the
 * lock when we actually enqueue (which might be expensive, especially on
 * architectures with IO MMUs).
 *
 * This runs with softirqs disabled.
 */
1333 1334
static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
		     unsigned int qid, struct net_device *dev)
S
Scott Bardone 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
{
	struct sge *sge = adapter->sge;
	struct cmdQ *q = &sge->cmdQ[qid];
	unsigned int credits, pidx, genbit, count;

	spin_lock(&q->lock);
	reclaim_completed_tx(sge, q);

	pidx = q->pidx;
	credits = q->size - q->in_use;
	count = 1 + skb_shinfo(skb)->nr_frags;

	{	/* Ethernet packet */
	 	if (unlikely(credits < count)) {
			netif_stop_queue(dev);
			set_bit(dev->if_port, &sge->stopped_tx_queues);
1351
			sge->stats.cmdQ_full[2]++;
S
Scott Bardone 已提交
1352
			spin_unlock(&q->lock);
1353 1354 1355 1356
			if (!netif_queue_stopped(dev))
				CH_ERR("%s: Tx ring full while queue awake!\n",
				       adapter->name);
			return NETDEV_TX_BUSY;
1357
		}
S
Scott Bardone 已提交
1358
		if (unlikely(credits - count < q->stop_thres)) {
1359
			sge->stats.cmdQ_full[2]++;
S
Scott Bardone 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
			netif_stop_queue(dev);
			set_bit(dev->if_port, &sge->stopped_tx_queues);
		}
	}
	q->in_use += count;
	genbit = q->genbit;
	q->pidx += count;
	if (q->pidx >= q->size) {
		q->pidx -= q->size;
		q->genbit ^= 1;
1370
	}
S
Scott Bardone 已提交
1371
	spin_unlock(&q->lock);
1372

S
Scott Bardone 已提交
1373
	write_tx_descs(adapter, skb, pidx, genbit, q);
1374 1375 1376 1377 1378 1379 1380 1381

	/*
	 * We always ring the doorbell for cmdQ1.  For cmdQ0, we only ring
	 * the doorbell if the Q is asleep. There is a natural race, where
	 * the hardware is going to sleep just after we checked, however,
	 * then the interrupt handler will detect the outstanding TX packet
	 * and ring the doorbell for us.
	 */
S
Scott Bardone 已提交
1382 1383 1384 1385 1386 1387 1388 1389
	if (qid)
		doorbell_pio(adapter, F_CMDQ1_ENABLE);
	else {
		clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
		if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
			set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
			writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
		}
1390
	}
1391
	return NETDEV_TX_OK;
1392 1393 1394 1395
}

#define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))

S
Scott Bardone 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
/*
 *	eth_hdr_len - return the length of an Ethernet header
 *	@data: pointer to the start of the Ethernet header
 *
 *	Returns the length of an Ethernet header, including optional VLAN tag.
 */
static inline int eth_hdr_len(const void *data)
{
	const struct ethhdr *e = data;

	return e->h_proto == htons(ETH_P_8021Q) ? VLAN_ETH_HLEN : ETH_HLEN;
}

1409 1410 1411 1412 1413 1414
/*
 * Adds the CPL header to the sk_buff and passes it to t1_sge_tx.
 */
int t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct adapter *adapter = dev->priv;
S
Scott Bardone 已提交
1415 1416
	struct sge_port_stats *st = &adapter->sge->port_stats[dev->if_port];
	struct sge *sge = adapter->sge;
1417 1418
	struct cpl_tx_pkt *cpl;

S
Scott Bardone 已提交
1419
#ifdef NETIF_F_TSO
H
Herbert Xu 已提交
1420
	if (skb_is_gso(skb)) {
1421 1422 1423
		int eth_type;
		struct cpl_tx_pkt_lso *hdr;

S
Scott Bardone 已提交
1424 1425
		st->tso++;

1426 1427 1428 1429 1430 1431 1432 1433 1434
		eth_type = skb->nh.raw - skb->data == ETH_HLEN ?
			CPL_ETH_II : CPL_ETH_II_VLAN;

		hdr = (struct cpl_tx_pkt_lso *)skb_push(skb, sizeof(*hdr));
		hdr->opcode = CPL_TX_PKT_LSO;
		hdr->ip_csum_dis = hdr->l4_csum_dis = 0;
		hdr->ip_hdr_words = skb->nh.iph->ihl;
		hdr->tcp_hdr_words = skb->h.th->doff;
		hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
1435
						skb_shinfo(skb)->gso_size));
1436 1437
		hdr->len = htonl(skb->len - sizeof(*hdr));
		cpl = (struct cpl_tx_pkt *)hdr;
S
Scott Bardone 已提交
1438
		sge->stats.tx_lso_pkts++;
1439
	} else
S
Scott Bardone 已提交
1440
#endif
1441 1442
	{
		/*
S
Scott Bardone 已提交
1443 1444 1445 1446
	 	 * Packets shorter than ETH_HLEN can break the MAC, drop them
		 * early.  Also, we may get oversized packets because some
		 * parts of the kernel don't handle our unusual hard_header_len
		 * right, drop those too.
1447
		 */
S
Scott Bardone 已提交
1448 1449 1450
		if (unlikely(skb->len < ETH_HLEN ||
			     skb->len > dev->mtu + eth_hdr_len(skb->data))) {
			dev_kfree_skb_any(skb);
1451
			return NETDEV_TX_OK;
S
Scott Bardone 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
		}

		/*
		 * We are using a non-standard hard_header_len and some kernel
		 * components, such as pktgen, do not handle it right.
		 * Complain when this happens but try to fix things up.
		 */
		if (unlikely(skb_headroom(skb) <
			     dev->hard_header_len - ETH_HLEN)) {
			struct sk_buff *orig_skb = skb;

			if (net_ratelimit())
				printk(KERN_ERR "%s: inadequate headroom in "
				       "Tx packet\n", dev->name);
			skb = skb_realloc_headroom(skb, sizeof(*cpl));
			dev_kfree_skb_any(orig_skb);
			if (!skb)
1469
				return NETDEV_TX_OK;
S
Scott Bardone 已提交
1470
		}
1471 1472

		if (!(adapter->flags & UDP_CSUM_CAPABLE) &&
1473
		    skb->ip_summed == CHECKSUM_PARTIAL &&
S
Scott Bardone 已提交
1474
		    skb->nh.iph->protocol == IPPROTO_UDP)
1475
			if (unlikely(skb_checksum_help(skb))) {
S
Scott Bardone 已提交
1476
				dev_kfree_skb_any(skb);
1477
				return NETDEV_TX_OK;
S
Scott Bardone 已提交
1478
			}
1479

S
Scott Bardone 已提交
1480 1481 1482 1483
		/* Hmmm, assuming to catch the gratious arp... and we'll use
		 * it to flush out stuck espi packets...
		  */
		if (unlikely(!adapter->sge->espibug_skb)) {
1484
			if (skb->protocol == htons(ETH_P_ARP) &&
S
Scott Bardone 已提交
1485 1486 1487 1488 1489 1490 1491 1492
			    skb->nh.arph->ar_op == htons(ARPOP_REQUEST)) {
				adapter->sge->espibug_skb = skb;
				/* We want to re-use this skb later. We
				 * simply bump the reference count and it
				 * will not be freed...
				 */
				skb = skb_get(skb);
			}
1493
		}
S
Scott Bardone 已提交
1494 1495

		cpl = (struct cpl_tx_pkt *)__skb_push(skb, sizeof(*cpl));
1496 1497
		cpl->opcode = CPL_TX_PKT;
		cpl->ip_csum_dis = 1;    /* SW calculates IP csum */
1498
		cpl->l4_csum_dis = skb->ip_summed == CHECKSUM_PARTIAL ? 0 : 1;
1499
		/* the length field isn't used so don't bother setting it */
S
Scott Bardone 已提交
1500

1501 1502
		st->tx_cso += (skb->ip_summed == CHECKSUM_PARTIAL);
		sge->stats.tx_do_cksum += (skb->ip_summed == CHECKSUM_PARTIAL);
S
Scott Bardone 已提交
1503
		sge->stats.tx_reg_pkts++;
1504 1505 1506 1507 1508 1509 1510
	}
	cpl->iff = dev->if_port;

#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
	if (adapter->vlan_grp && vlan_tx_tag_present(skb)) {
		cpl->vlan_valid = 1;
		cpl->vlan = htons(vlan_tx_tag_get(skb));
S
Scott Bardone 已提交
1511
		st->vlan_insert++;
1512 1513 1514 1515 1516
	} else
#endif
		cpl->vlan_valid = 0;

	dev->trans_start = jiffies;
S
Scott Bardone 已提交
1517 1518
	return t1_sge_tx(skb, adapter, 0, dev);
}
1519

S
Scott Bardone 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
/*
 * Callback for the Tx buffer reclaim timer.  Runs with softirqs disabled.
 */
static void sge_tx_reclaim_cb(unsigned long data)
{
	int i;
	struct sge *sge = (struct sge *)data;

	for (i = 0; i < SGE_CMDQ_N; ++i) {
		struct cmdQ *q = &sge->cmdQ[i];

		if (!spin_trylock(&q->lock))
			continue;
1533

S
Scott Bardone 已提交
1534 1535 1536 1537
		reclaim_completed_tx(sge, q);
		if (i == 0 && q->in_use)   /* flush pending credits */
			writel(F_CMDQ0_ENABLE,
				sge->adapter->regs + A_SG_DOORBELL);
1538

S
Scott Bardone 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
		spin_unlock(&q->lock);
	}
	mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
}

/*
 * Propagate changes of the SGE coalescing parameters to the HW.
 */
int t1_sge_set_coalesce_params(struct sge *sge, struct sge_params *p)
{
	sge->netdev->poll = t1_poll;
	sge->fixed_intrtimer = p->rx_coalesce_usecs *
		core_ticks_per_usec(sge->adapter);
	writel(sge->fixed_intrtimer, sge->adapter->regs + A_SG_INTRTIMER);
1553 1554 1555
	return 0;
}

S
Scott Bardone 已提交
1556 1557 1558 1559 1560
/*
 * Allocates both RX and TX resources and configures the SGE. However,
 * the hardware is not enabled yet.
 */
int t1_sge_configure(struct sge *sge, struct sge_params *p)
1561
{
S
Scott Bardone 已提交
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
	if (alloc_rx_resources(sge, p))
		return -ENOMEM;
	if (alloc_tx_resources(sge, p)) {
		free_rx_resources(sge);
		return -ENOMEM;
	}
	configure_sge(sge, p);

	/*
	 * Now that we have sized the free lists calculate the payload
	 * capacity of the large buffers.  Other parts of the driver use
	 * this to set the max offload coalescing size so that RX packets
	 * do not overflow our large buffers.
	 */
	p->large_buf_capacity = jumbo_payload_capacity(sge);
	return 0;
}
1579

S
Scott Bardone 已提交
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
/*
 * Disables the DMA engine.
 */
void t1_sge_stop(struct sge *sge)
{
	writel(0, sge->adapter->regs + A_SG_CONTROL);
	(void) readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
	if (is_T2(sge->adapter))
		del_timer_sync(&sge->espibug_timer);
	del_timer_sync(&sge->tx_reclaim_timer);
1590 1591
}

S
Scott Bardone 已提交
1592 1593 1594 1595
/*
 * Enables the DMA engine.
 */
void t1_sge_start(struct sge *sge)
1596
{
S
Scott Bardone 已提交
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
	refill_free_list(sge, &sge->freelQ[0]);
	refill_free_list(sge, &sge->freelQ[1]);

	writel(sge->sge_control, sge->adapter->regs + A_SG_CONTROL);
	doorbell_pio(sge->adapter, F_FL0_ENABLE | F_FL1_ENABLE);
	(void) readl(sge->adapter->regs + A_SG_CONTROL); /* flush */

	mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);

	if (is_T2(sge->adapter)) 
		mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
}

/*
 * Callback for the T2 ESPI 'stuck packet feature' workaorund
 */
static void espibug_workaround(void *data)
{
	struct adapter *adapter = (struct adapter *)data;
1616 1617
	struct sge *sge = adapter->sge;

S
Scott Bardone 已提交
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
	if (netif_running(adapter->port[0].dev)) {
		struct sk_buff *skb = sge->espibug_skb;

		u32 seop = t1_espi_get_mon(adapter, 0x930, 0);

		if ((seop & 0xfff0fff) == 0xfff && skb) {
			if (!skb->cb[0]) {
				u8 ch_mac_addr[ETH_ALEN] =
				    {0x0, 0x7, 0x43, 0x0, 0x0, 0x0};
				memcpy(skb->data + sizeof(struct cpl_tx_pkt),
				    ch_mac_addr, ETH_ALEN);
				memcpy(skb->data + skb->len - 10, ch_mac_addr,
				    ETH_ALEN);
				skb->cb[0] = 0xff;
			}

			/* bump the reference count to avoid freeing of the
			 * skb once the DMA has completed.
			 */
			skb = skb_get(skb);
			t1_sge_tx(skb, adapter, 0, adapter->port[0].dev);
		}
	}
	mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
1642 1643
}

S
Scott Bardone 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
/*
 * Creates a t1_sge structure and returns suggested resource parameters.
 */
struct sge * __devinit t1_sge_create(struct adapter *adapter,
				     struct sge_params *p)
{
	struct sge *sge = kmalloc(sizeof(*sge), GFP_KERNEL);

	if (!sge)
		return NULL;
	memset(sge, 0, sizeof(*sge));

	sge->adapter = adapter;
	sge->netdev = adapter->port[0].dev;
	sge->rx_pkt_pad = t1_is_T1B(adapter) ? 0 : 2;
	sge->jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;

	init_timer(&sge->tx_reclaim_timer);
	sge->tx_reclaim_timer.data = (unsigned long)sge;
	sge->tx_reclaim_timer.function = sge_tx_reclaim_cb;

	if (is_T2(sge->adapter)) {
		init_timer(&sge->espibug_timer);
		sge->espibug_timer.function = (void *)&espibug_workaround;
		sge->espibug_timer.data = (unsigned long)sge->adapter;
		sge->espibug_timeout = 1;
	}
	 

	p->cmdQ_size[0] = SGE_CMDQ0_E_N;
	p->cmdQ_size[1] = SGE_CMDQ1_E_N;
	p->freelQ_size[!sge->jumbo_fl] = SGE_FREEL_SIZE;
	p->freelQ_size[sge->jumbo_fl] = SGE_JUMBO_FREEL_SIZE;
	p->rx_coalesce_usecs =  50;
	p->coalesce_enable = 0;
	p->sample_interval_usecs = 0;
	p->polling = 0;

	return sge;
}