sge.c 58.1 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
 * 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>
45
#include <linux/ktime.h>
46 47 48 49 50 51
#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>
52
#include <linux/tcp.h>
53 54 55
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/if_arp.h>
56
#include <linux/slab.h>
57
#include <linux/prefetch.h>
58 59 60 61 62 63

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

64 65
/* This belongs in if_ether.h */
#define ETH_P_CPL5 0xf
66 67 68

#define SGE_CMDQ_N		2
#define SGE_FREELQ_N		2
S
Scott Bardone 已提交
69
#define SGE_CMDQ0_E_N		1024
70 71 72 73 74
#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 已提交
75
#define SGE_INTRTIMER_NRES	1000
76
#define SGE_RX_SM_BUF_SIZE	1536
77
#define SGE_TX_DESC_MAX_PLEN	16384
78

S
Scott Bardone 已提交
79 80 81 82 83 84 85
#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

S
Scott Bardone 已提交
87 88 89 90 91 92 93 94 95
#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)

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

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

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 已提交
131 132 133 134
	u32 len_gen;
	u32 addr_lo;
	u32 addr_hi;
	u32 flags;
135 136 137
};

struct freelQ_e {
S
Scott Bardone 已提交
138 139 140 141
	u32 len_gen;
	u32 addr_lo;
	u32 addr_hi;
	u32 gen2;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
};

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;
166 167
	DEFINE_DMA_UNMAP_ADDR(dma_addr);
	DEFINE_DMA_UNMAP_LEN(dma_len);
168 169 170 171
};

struct freelQ_ce {
	struct sk_buff *skb;
172 173
	DEFINE_DMA_UNMAP_ADDR(dma_addr);
	DEFINE_DMA_UNMAP_LEN(dma_len);
174 175 176
};

/*
S
Scott Bardone 已提交
177
 * SW command, freelist and response rings
178 179
 */
struct cmdQ {
S
Scott Bardone 已提交
180 181 182
	unsigned long   status;         /* HW DMA fetch status */
	unsigned int    in_use;         /* # of in-use command descriptors */
	unsigned int	size;	        /* # of descriptors */
183 184 185
	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 */
S
Scott Bardone 已提交
186 187 188
	u16		pidx;           /* producer index (SW) */
	u16		cidx;           /* consumer index (HW) */
	u8		genbit;         /* current generation (=valid) bit */
189
	u8              sop;            /* is next entry start of packet? */
S
Scott Bardone 已提交
190 191 192
	struct cmdQ_e  *entries;        /* HW command descriptor Q */
	struct cmdQ_ce *centries;       /* SW command context descriptor Q */
	dma_addr_t	dma_addr;       /* DMA addr HW command descriptor Q */
193
	spinlock_t	lock;           /* Lock to protect cmdQ enqueuing */
194 195 196
};

struct freelQ {
S
Scott Bardone 已提交
197 198 199 200
	unsigned int	credits;        /* # of available RX buffers */
	unsigned int	size;	        /* free list capacity */
	u16		pidx;           /* producer index (SW) */
	u16		cidx;           /* consumer index (HW) */
201
	u16		rx_buffer_size; /* Buffer size on this free list */
202 203
	u16             dma_offset;     /* DMA offset to align IP headers */
	u16             recycleq_idx;   /* skb recycle q to use */
S
Scott Bardone 已提交
204 205 206 207
	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 */
208 209 210
};

struct respQ {
S
Scott Bardone 已提交
211 212 213 214
	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 */
215
	struct respQ_e *entries;        /* HW response descriptor Q */
S
Scott Bardone 已提交
216 217 218 219 220 221 222
	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 */
223 224
};

225 226 227 228 229 230 231 232 233 234 235 236 237 238
/* T204 TX SW scheduler */

/* Per T204 TX port */
struct sched_port {
	unsigned int	avail;		/* available bits - quota */
	unsigned int	drain_bits_per_1024ns; /* drain rate */
	unsigned int	speed;		/* drain rate, mbps */
	unsigned int	mtu;		/* mtu size */
	struct sk_buff_head skbq;	/* pending skbs */
};

/* Per T204 device */
struct sched {
	ktime_t         last_updated;   /* last time quotas were computed */
239 240 241
	unsigned int	max_avail;	/* max bits to be sent to any port */
	unsigned int	port;		/* port index (round robin ports) */
	unsigned int	num;		/* num skbs in per port queues */
242 243 244 245 246 247
	struct sched_port p[MAX_NPORTS];
	struct tasklet_struct sched_tsk;/* tasklet used to run scheduler */
};
static void restart_sched(unsigned long);


248 249 250 251 252
/*
 * 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
D
Daniel Mack 已提交
253
 * separate the RX(in irq context) and TX state in order to decrease memory
254 255 256
 * contention.
 */
struct sge {
257
	struct adapter *adapter;	/* adapter backpointer */
S
Scott Bardone 已提交
258
	struct net_device *netdev;      /* netdevice backpointer */
259 260
	struct freelQ	freelQ[SGE_FREELQ_N]; /* buffer free lists */
	struct respQ	respQ;		/* response Q */
S
Scott Bardone 已提交
261
	unsigned long   stopped_tx_queues; /* bitmap of suspended Tx queues */
262 263
	unsigned int	rx_pkt_pad;     /* RX padding for L2 packets */
	unsigned int	jumbo_fl;       /* jumbo freelist Q index */
S
Scott Bardone 已提交
264
	unsigned int	intrtimer_nres;	/* no-resource interrupt timer */
265
	unsigned int    fixed_intrtimer;/* non-adaptive interrupt timer */
S
Scott Bardone 已提交
266 267
	struct timer_list tx_reclaim_timer; /* reclaims TX buffers */
	struct timer_list espibug_timer;
268 269
	unsigned long	espibug_timeout;
	struct sk_buff	*espibug_skb[MAX_NPORTS];
S
Scott Bardone 已提交
270 271
	u32		sge_control;	/* shadow value of sge control reg */
	struct sge_intr_counts stats;
272
	struct sge_port_stats __percpu *port_stats[MAX_NPORTS];
273
	struct sched	*tx_sched;
S
Scott Bardone 已提交
274
	struct cmdQ cmdQ[SGE_CMDQ_N] ____cacheline_aligned_in_smp;
275 276
};

J
Joe Perches 已提交
277 278 279 280
static const u8 ch_mac_addr[ETH_ALEN] = {
	0x0, 0x7, 0x43, 0x0, 0x0, 0x0
};

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/*
 * stop tasklet and free all pending skb's
 */
static void tx_sched_stop(struct sge *sge)
{
	struct sched *s = sge->tx_sched;
	int i;

	tasklet_kill(&s->sched_tsk);

	for (i = 0; i < MAX_NPORTS; i++)
		__skb_queue_purge(&s->p[s->port].skbq);
}

/*
 * t1_sched_update_parms() is called when the MTU or link speed changes. It
 * re-computes scheduler parameters to scope with the change.
 */
unsigned int t1_sched_update_parms(struct sge *sge, unsigned int port,
				   unsigned int mtu, unsigned int speed)
{
	struct sched *s = sge->tx_sched;
	struct sched_port *p = &s->p[port];
	unsigned int max_avail_segs;

	pr_debug("t1_sched_update_params mtu=%d speed=%d\n", mtu, speed);
	if (speed)
		p->speed = speed;
	if (mtu)
		p->mtu = mtu;

	if (speed || mtu) {
		unsigned long long drain = 1024ULL * p->speed * (p->mtu - 40);
		do_div(drain, (p->mtu + 50) * 1000);
		p->drain_bits_per_1024ns = (unsigned int) drain;

		if (p->speed < 1000)
			p->drain_bits_per_1024ns =
				90 * p->drain_bits_per_1024ns / 100;
	}

	if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204) {
		p->drain_bits_per_1024ns -= 16;
		s->max_avail = max(4096U, p->mtu + 16 + 14 + 4);
		max_avail_segs = max(1U, 4096 / (p->mtu - 40));
	} else {
		s->max_avail = 16384;
		max_avail_segs = max(1U, 9000 / (p->mtu - 40));
	}

	pr_debug("t1_sched_update_parms: mtu %u speed %u max_avail %u "
		 "max_avail_segs %u drain_bits_per_1024ns %u\n", p->mtu,
		 p->speed, s->max_avail, max_avail_segs,
		 p->drain_bits_per_1024ns);

	return max_avail_segs * (p->mtu - 40);
}

339 340
#if 0

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
/*
 * t1_sched_max_avail_bytes() tells the scheduler the maximum amount of
 * data that can be pushed per port.
 */
void t1_sched_set_max_avail_bytes(struct sge *sge, unsigned int val)
{
	struct sched *s = sge->tx_sched;
	unsigned int i;

	s->max_avail = val;
	for (i = 0; i < MAX_NPORTS; i++)
		t1_sched_update_parms(sge, i, 0, 0);
}

/*
 * t1_sched_set_drain_bits_per_us() tells the scheduler at which rate a port
 * is draining.
 */
void t1_sched_set_drain_bits_per_us(struct sge *sge, unsigned int port,
					 unsigned int val)
{
	struct sched *s = sge->tx_sched;
	struct sched_port *p = &s->p[port];
	p->drain_bits_per_1024ns = val * 1024 / 1000;
	t1_sched_update_parms(sge, port, 0, 0);
}

368 369
#endif  /*  0  */

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

/*
 * get_clock() implements a ns clock (see ktime_get)
 */
static inline ktime_t get_clock(void)
{
	struct timespec ts;

	ktime_get_ts(&ts);
	return timespec_to_ktime(ts);
}

/*
 * tx_sched_init() allocates resources and does basic initialization.
 */
static int tx_sched_init(struct sge *sge)
{
	struct sched *s;
	int i;

	s = kzalloc(sizeof (struct sched), GFP_KERNEL);
	if (!s)
		return -ENOMEM;

	pr_debug("tx_sched_init\n");
	tasklet_init(&s->sched_tsk, restart_sched, (unsigned long) sge);
	sge->tx_sched = s;

	for (i = 0; i < MAX_NPORTS; i++) {
		skb_queue_head_init(&s->p[i].skbq);
		t1_sched_update_parms(sge, i, 1500, 1000);
	}

	return 0;
}

/*
 * sched_update_avail() computes the delta since the last time it was called
 * and updates the per port quota (number of bits that can be sent to the any
 * port).
 */
static inline int sched_update_avail(struct sge *sge)
{
	struct sched *s = sge->tx_sched;
	ktime_t now = get_clock();
	unsigned int i;
	long long delta_time_ns;

	delta_time_ns = ktime_to_ns(ktime_sub(now, s->last_updated));

	pr_debug("sched_update_avail delta=%lld\n", delta_time_ns);
	if (delta_time_ns < 15000)
		return 0;

	for (i = 0; i < MAX_NPORTS; i++) {
		struct sched_port *p = &s->p[i];
		unsigned int delta_avail;

		delta_avail = (p->drain_bits_per_1024ns * delta_time_ns) >> 13;
		p->avail = min(p->avail + delta_avail, s->max_avail);
	}

	s->last_updated = now;

	return 1;
}

/*
 * sched_skb() is called from two different places. In the tx path, any
 * packet generating load on an output port will call sched_skb()
 * (skb != NULL). In addition, sched_skb() is called from the irq/soft irq
 * context (skb == NULL).
 * The scheduler only returns a skb (which will then be sent) if the
 * length of the skb is <= the current quota of the output port.
 */
static struct sk_buff *sched_skb(struct sge *sge, struct sk_buff *skb,
				unsigned int credits)
{
	struct sched *s = sge->tx_sched;
	struct sk_buff_head *skbq;
	unsigned int i, len, update = 1;

	pr_debug("sched_skb %p\n", skb);
	if (!skb) {
		if (!s->num)
			return NULL;
	} else {
		skbq = &s->p[skb->dev->if_port].skbq;
		__skb_queue_tail(skbq, skb);
		s->num++;
		skb = NULL;
	}

	if (credits < MAX_SKB_FRAGS + 1)
		goto out;

466
again:
467
	for (i = 0; i < MAX_NPORTS; i++) {
D
David S. Miller 已提交
468
		s->port = (s->port + 1) & (MAX_NPORTS - 1);
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
		skbq = &s->p[s->port].skbq;

		skb = skb_peek(skbq);

		if (!skb)
			continue;

		len = skb->len;
		if (len <= s->p[s->port].avail) {
			s->p[s->port].avail -= len;
			s->num--;
			__skb_unlink(skb, skbq);
			goto out;
		}
		skb = NULL;
	}

	if (update-- && sched_update_avail(sge))
		goto again;

489 490
out:
	/* If there are more pending skbs, we use the hardware to schedule us
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	 * again.
	 */
	if (s->num && !skb) {
		struct cmdQ *q = &sge->cmdQ[0];
		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, sge->adapter->regs + A_SG_DOORBELL);
		}
	}
	pr_debug("sched_skb ret %p\n", skb);

	return skb;
}

506 507 508
/*
 * PIO to indicate that memory mapped Q contains valid descriptor(s).
 */
S
Scott Bardone 已提交
509
static inline void doorbell_pio(struct adapter *adapter, u32 val)
510 511
{
	wmb();
S
Scott Bardone 已提交
512
	writel(val, adapter->regs + A_SG_DOORBELL);
513 514 515 516 517 518
}

/*
 * 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 已提交
519
static void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *q)
520
{
S
Scott Bardone 已提交
521
	unsigned int cidx = q->cidx;
522

S
Scott Bardone 已提交
523 524
	while (q->credits--) {
		struct freelQ_ce *ce = &q->centries[cidx];
525

526 527
		pci_unmap_single(pdev, dma_unmap_addr(ce, dma_addr),
				 dma_unmap_len(ce, dma_len),
528 529 530
				 PCI_DMA_FROMDEVICE);
		dev_kfree_skb(ce->skb);
		ce->skb = NULL;
S
Scott Bardone 已提交
531
		if (++cidx == q->size)
532 533 534 535 536 537 538 539 540 541 542 543 544
			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 已提交
545
		size = sizeof(struct respQ_e) * sge->respQ.size;
546 547 548 549 550
		pci_free_consistent(pdev, size, sge->respQ.entries,
				    sge->respQ.dma_addr);
	}

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

S
Scott Bardone 已提交
553 554 555
		if (q->centries) {
			free_freelQ_buffers(pdev, q);
			kfree(q->centries);
556
		}
S
Scott Bardone 已提交
557 558 559 560
		if (q->entries) {
			size = sizeof(struct freelQ_e) * q->size;
			pci_free_consistent(pdev, size, q->entries,
					    q->dma_addr);
561 562 563 564 565 566
		}
	}
}

/*
 * Allocates basic RX resources, consisting of memory mapped freelist Qs and a
S
Scott Bardone 已提交
567
 * response queue.
568 569 570 571 572 573 574
 */
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 已提交
575 576 577 578 579 580
		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;
F
Francois Romieu 已提交
581
		q->entries = pci_alloc_consistent(pdev, size, &q->dma_addr);
S
Scott Bardone 已提交
582
		if (!q->entries)
583
			goto err_no_mem;
F
Francois Romieu 已提交
584

S
Scott Bardone 已提交
585
		size = sizeof(struct freelQ_ce) * q->size;
S
Stephen Hemminger 已提交
586
		q->centries = kzalloc(size, GFP_KERNEL);
S
Scott Bardone 已提交
587
		if (!q->centries)
588 589 590 591 592 593 594 595 596 597 598 599 600
			goto err_no_mem;
	}

	/*
	 * 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;
601 602 603 604 605

		size = (16 * 1024) -
		    SKB_DATA_ALIGN(sizeof(struct skb_shared_info));

	sge->freelQ[sge->jumbo_fl].rx_buffer_size = size;
606

S
Scott Bardone 已提交
607 608 609 610 611 612 613
	/*
	 * 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;

614
	sge->respQ.genbit = 1;
S
Scott Bardone 已提交
615 616 617
	sge->respQ.size = SGE_RESPQ_E_N;
	sge->respQ.credits = 0;
	size = sizeof(struct respQ_e) * sge->respQ.size;
F
Francois Romieu 已提交
618
	sge->respQ.entries =
619 620 621 622 623 624 625 626 627 628 629
		pci_alloc_consistent(pdev, size, &sge->respQ.dma_addr);
	if (!sge->respQ.entries)
		goto err_no_mem;
	return 0;

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

/*
S
Scott Bardone 已提交
630
 * Reclaims n TX descriptors and frees the buffers associated with them.
631
 */
S
Scott Bardone 已提交
632
static void free_cmdQ_buffers(struct sge *sge, struct cmdQ *q, unsigned int n)
633
{
S
Scott Bardone 已提交
634
	struct cmdQ_ce *ce;
635
	struct pci_dev *pdev = sge->adapter->pdev;
S
Scott Bardone 已提交
636
	unsigned int cidx = q->cidx;
637

S
Scott Bardone 已提交
638 639 640
	q->in_use -= n;
	ce = &q->centries[cidx];
	while (n--) {
641 642 643
		if (likely(dma_unmap_len(ce, dma_len))) {
			pci_unmap_single(pdev, dma_unmap_addr(ce, dma_addr),
					 dma_unmap_len(ce, dma_len),
F
Francois Romieu 已提交
644 645
					 PCI_DMA_TODEVICE);
			if (q->sop)
646 647
				q->sop = 0;
		}
S
Scott Bardone 已提交
648
		if (ce->skb) {
649
			dev_kfree_skb_any(ce->skb);
S
Scott Bardone 已提交
650 651
			q->sop = 1;
		}
652
		ce++;
S
Scott Bardone 已提交
653
		if (++cidx == q->size) {
654
			cidx = 0;
S
Scott Bardone 已提交
655
			ce = q->centries;
656 657
		}
	}
S
Scott Bardone 已提交
658
	q->cidx = cidx;
659 660 661 662 663 664 665 666 667 668 669 670 671
}

/*
 * 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 已提交
672
		struct cmdQ *q = &sge->cmdQ[i];
673

S
Scott Bardone 已提交
674 675 676 677
		if (q->centries) {
			if (q->in_use)
				free_cmdQ_buffers(sge, q, q->in_use);
			kfree(q->centries);
678
		}
S
Scott Bardone 已提交
679 680 681 682
		if (q->entries) {
			size = sizeof(struct cmdQ_e) * q->size;
			pci_free_consistent(pdev, size, q->entries,
					    q->dma_addr);
683 684 685 686 687 688 689 690 691 692 693 694 695
		}
	}
}

/*
 * 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 已提交
696 697 698 699 700 701 702 703 704 705 706
		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;
F
Francois Romieu 已提交
707
		q->entries = pci_alloc_consistent(pdev, size, &q->dma_addr);
S
Scott Bardone 已提交
708
		if (!q->entries)
709
			goto err_no_mem;
F
Francois Romieu 已提交
710

S
Scott Bardone 已提交
711
		size = sizeof(struct cmdQ_ce) * q->size;
S
Stephen Hemminger 已提交
712
		q->centries = kzalloc(size, GFP_KERNEL);
S
Scott Bardone 已提交
713
		if (!q->centries)
714 715 716
			goto err_no_mem;
	}

S
Scott Bardone 已提交
717 718 719 720 721 722 723 724 725
	/*
	 * 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);
726 727 728 729 730 731 732 733 734 735 736
	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 已提交
737 738 739
	writel((u32)addr, adapter->regs + base_reg_lo);
	writel(addr >> 32, adapter->regs + base_reg_hi);
	writel(size, adapter->regs + size_reg);
740 741 742 743 744
}

/*
 * Enable/disable VLAN acceleration.
 */
J
Jiri Pirko 已提交
745
void t1_vlan_mode(struct adapter *adapter, u32 features)
746 747 748
{
	struct sge *sge = adapter->sge;

J
Jiri Pirko 已提交
749
	if (features & NETIF_F_HW_VLAN_RX)
750
		sge->sge_control |= F_VLAN_XTRACT;
J
Jiri Pirko 已提交
751 752
	else
		sge->sge_control &= ~F_VLAN_XTRACT;
753
	if (adapter->open_device_map) {
S
Scott Bardone 已提交
754
		writel(sge->sge_control, adapter->regs + A_SG_CONTROL);
755
		readl(adapter->regs + A_SG_CONTROL);   /* flush */
756 757 758 759 760 761 762 763 764 765
	}
}

/*
 * 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;
766

S
Scott Bardone 已提交
767 768
	writel(0, ap->regs + A_SG_CONTROL);
	setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
769
			  A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
S
Scott Bardone 已提交
770
	setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
771 772
			  A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
	setup_ring_params(ap, sge->freelQ[0].dma_addr,
S
Scott Bardone 已提交
773
			  sge->freelQ[0].size, A_SG_FL0BASELWR,
774 775
			  A_SG_FL0BASEUPR, A_SG_FL0SIZE);
	setup_ring_params(ap, sge->freelQ[1].dma_addr,
S
Scott Bardone 已提交
776
			  sge->freelQ[1].size, A_SG_FL1BASELWR,
777 778 779
			  A_SG_FL1BASEUPR, A_SG_FL1SIZE);

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

S
Scott Bardone 已提交
782 783 784
	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);
785 786 787 788 789 790 791 792 793 794

	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 |
		V_RX_PKT_OFFSET(sge->rx_pkt_pad);

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

S
Scott Bardone 已提交
795 796 797 798
	/* Initialize no-resource timer */
	sge->intrtimer_nres = SGE_INTRTIMER_NRES * core_ticks_per_usec(ap);

	t1_sge_set_coalesce_params(sge, p);
799 800 801 802 803 804 805 806
}

/*
 * 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 已提交
807 808
		sge->freelQ[sge->jumbo_fl].dma_offset -
		sizeof(struct cpl_rx_data);
809 810 811 812 813 814 815
}

/*
 * Frees all SGE related resources and the sge structure itself
 */
void t1_sge_destroy(struct sge *sge)
{
816 817 818 819 820
	int i;

	for_each_port(sge->adapter, i)
		free_percpu(sge->port_stats[i]);

821
	kfree(sge->tx_sched);
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
	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 已提交
839
static void refill_free_list(struct sge *sge, struct freelQ *q)
840 841
{
	struct pci_dev *pdev = sge->adapter->pdev;
S
Scott Bardone 已提交
842 843 844
	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;
845

S
Scott Bardone 已提交
846 847 848
	while (q->credits < q->size) {
		struct sk_buff *skb;
		dma_addr_t mapping;
849

S
Scott Bardone 已提交
850 851 852 853 854 855 856
		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);
S
Stephen Hemminger 已提交
857 858
		skb_reserve(skb, sge->rx_pkt_pad);

S
Scott Bardone 已提交
859
		ce->skb = skb;
860 861
		dma_unmap_addr_set(ce, dma_addr, mapping);
		dma_unmap_len_set(ce, dma_len, dma_len);
S
Scott Bardone 已提交
862 863 864 865 866
		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);
867 868 869

		e++;
		ce++;
S
Scott Bardone 已提交
870 871 872 873 874
		if (++q->pidx == q->size) {
			q->pidx = 0;
			q->genbit ^= 1;
			ce = q->centries;
			e = q->entries;
875
		}
S
Scott Bardone 已提交
876
		q->credits++;
877 878 879 880
	}
}

/*
S
Scott Bardone 已提交
881 882 883
 * 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.
884 885 886
 */
static void freelQs_empty(struct sge *sge)
{
S
Scott Bardone 已提交
887 888
	struct adapter *adapter = sge->adapter;
	u32 irq_reg = readl(adapter->regs + A_SG_INT_ENABLE);
889 890 891 892 893
	u32 irqholdoff_reg;

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

S
Scott Bardone 已提交
894 895
	if (sge->freelQ[0].credits > (sge->freelQ[0].size >> 2) &&
	    sge->freelQ[1].credits > (sge->freelQ[1].size >> 2)) {
896
		irq_reg |= F_FL_EXHAUSTED;
S
Scott Bardone 已提交
897
		irqholdoff_reg = sge->fixed_intrtimer;
898 899 900 901 902
	} else {
		/* Clear the F_FL_EXHAUSTED interrupts for now */
		irq_reg &= ~F_FL_EXHAUSTED;
		irqholdoff_reg = sge->intrtimer_nres;
	}
S
Scott Bardone 已提交
903 904
	writel(irqholdoff_reg, adapter->regs + A_SG_INTRTIMER);
	writel(irq_reg, adapter->regs + A_SG_INT_ENABLE);
905 906

	/* We reenable the Qs to force a freelist GTS interrupt later */
S
Scott Bardone 已提交
907
	doorbell_pio(adapter, F_FL0_ENABLE | F_FL1_ENABLE);
908 909 910 911 912 913 914 915 916 917 918 919
}

#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 已提交
920
	u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
921

S
Scott Bardone 已提交
922 923
	writel(val & ~SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
	writel(0, sge->adapter->regs + A_SG_INT_ENABLE);
924 925 926 927 928 929 930 931
}

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

934
	if (sge->adapter->port[0].dev->hw_features & NETIF_F_TSO)
935
		en &= ~F_PACKET_TOO_BIG;
S
Scott Bardone 已提交
936 937
	writel(en, sge->adapter->regs + A_SG_INT_ENABLE);
	writel(val | SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
938 939 940 941 942 943 944
}

/*
 * Clear SGE interrupts.
 */
void t1_sge_intr_clear(struct sge *sge)
{
S
Scott Bardone 已提交
945 946
	writel(SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_CAUSE);
	writel(0xffffffff, sge->adapter->regs + A_SG_INT_CAUSE);
947 948 949 950 951 952 953 954
}

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

957
	if (adapter->port[0].dev->hw_features & NETIF_F_TSO)
958 959
		cause &= ~F_PACKET_TOO_BIG;
	if (cause & F_RESPQ_EXHAUSTED)
S
Scott Bardone 已提交
960
		sge->stats.respQ_empty++;
961
	if (cause & F_RESPQ_OVERFLOW) {
S
Scott Bardone 已提交
962
		sge->stats.respQ_overflow++;
963
		pr_alert("%s: SGE response queue overflow\n",
964 965 966
			 adapter->name);
	}
	if (cause & F_FL_EXHAUSTED) {
S
Scott Bardone 已提交
967
		sge->stats.freelistQ_empty++;
968 969 970
		freelQs_empty(sge);
	}
	if (cause & F_PACKET_TOO_BIG) {
S
Scott Bardone 已提交
971
		sge->stats.pkt_too_big++;
972
		pr_alert("%s: SGE max packet size exceeded\n",
973 974 975
			 adapter->name);
	}
	if (cause & F_PACKET_MISMATCH) {
S
Scott Bardone 已提交
976
		sge->stats.pkt_mismatch++;
977
		pr_alert("%s: SGE packet mismatch\n", adapter->name);
978 979 980 981
	}
	if (cause & SGE_INT_FATAL)
		t1_fatal_err(adapter);

S
Scott Bardone 已提交
982
	writel(cause, adapter->regs + A_SG_INT_CAUSE);
983 984 985
	return 0;
}

986
const struct sge_intr_counts *t1_sge_get_intr_counts(const struct sge *sge)
S
Scott Bardone 已提交
987 988 989 990
{
	return &sge->stats;
}

991 992
void t1_sge_get_port_stats(const struct sge *sge, int port,
			   struct sge_port_stats *ss)
S
Scott Bardone 已提交
993
{
994 995 996 997 998 999 1000 1001 1002
	int cpu;

	memset(ss, 0, sizeof(*ss));
	for_each_possible_cpu(cpu) {
		struct sge_port_stats *st = per_cpu_ptr(sge->port_stats[port], cpu);

		ss->rx_cso_good += st->rx_cso_good;
		ss->tx_cso += st->tx_cso;
		ss->tx_tso += st->tx_tso;
D
Divy Le Ray 已提交
1003
		ss->tx_need_hdrroom += st->tx_need_hdrroom;
1004 1005 1006
		ss->vlan_xtract += st->vlan_xtract;
		ss->vlan_insert += st->vlan_insert;
	}
S
Scott Bardone 已提交
1007 1008 1009 1010 1011 1012
}

/**
 *	recycle_fl_buf - recycle a free list buffer
 *	@fl: the free list
 *	@idx: index of buffer to recycle
1013
 *
S
Scott Bardone 已提交
1014 1015
 *	Recycles the specified buffer on the given free list by adding it at
 *	the next available slot on the list.
1016
 */
S
Scott Bardone 已提交
1017
static void recycle_fl_buf(struct freelQ *fl, int idx)
1018
{
S
Scott Bardone 已提交
1019 1020
	struct freelQ_e *from = &fl->entries[idx];
	struct freelQ_e *to = &fl->entries[fl->pidx];
1021

S
Scott Bardone 已提交
1022 1023 1024 1025 1026 1027 1028
	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++;
1029

S
Scott Bardone 已提交
1030 1031 1032
	if (++fl->pidx == fl->size) {
		fl->pidx = 0;
		fl->genbit ^= 1;
1033
	}
S
Scott Bardone 已提交
1034
}
1035

S
Stephen Hemminger 已提交
1036 1037 1038 1039
static int copybreak __read_mostly = 256;
module_param(copybreak, int, 0);
MODULE_PARM_DESC(copybreak, "Receive copy threshold");

S
Scott Bardone 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
/**
 *	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
 *
 *	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,
S
Stephen Hemminger 已提交
1055
					 struct freelQ *fl, unsigned int len)
S
Scott Bardone 已提交
1056 1057
{
	struct sk_buff *skb;
S
Stephen Hemminger 已提交
1058
	const struct freelQ_ce *ce = &fl->centries[fl->cidx];
S
Scott Bardone 已提交
1059

S
Stephen Hemminger 已提交
1060 1061 1062 1063 1064 1065 1066 1067
	if (len < copybreak) {
		skb = alloc_skb(len + 2, GFP_ATOMIC);
		if (!skb)
			goto use_orig_buf;

		skb_reserve(skb, 2);	/* align IP header */
		skb_put(skb, len);
		pci_dma_sync_single_for_cpu(pdev,
1068 1069
					    dma_unmap_addr(ce, dma_addr),
					    dma_unmap_len(ce, dma_len),
S
Scott Bardone 已提交
1070
					    PCI_DMA_FROMDEVICE);
1071
		skb_copy_from_linear_data(ce->skb, skb->data, len);
S
Stephen Hemminger 已提交
1072
		pci_dma_sync_single_for_device(pdev,
1073 1074
					       dma_unmap_addr(ce, dma_addr),
					       dma_unmap_len(ce, dma_len),
S
Stephen Hemminger 已提交
1075
					       PCI_DMA_FROMDEVICE);
S
Scott Bardone 已提交
1076 1077
		recycle_fl_buf(fl, fl->cidx);
		return skb;
1078 1079
	}

S
Stephen Hemminger 已提交
1080 1081
use_orig_buf:
	if (fl->credits < 2) {
S
Scott Bardone 已提交
1082 1083 1084
		recycle_fl_buf(fl, fl->cidx);
		return NULL;
	}
1085

1086 1087
	pci_unmap_single(pdev, dma_unmap_addr(ce, dma_addr),
			 dma_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
S
Scott Bardone 已提交
1088
	skb = ce->skb;
S
Stephen Hemminger 已提交
1089 1090
	prefetch(skb->data);

S
Scott Bardone 已提交
1091 1092 1093
	skb_put(skb, len);
	return skb;
}
1094

S
Scott Bardone 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
/**
 *	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;

1109 1110
	pci_dma_sync_single_for_cpu(adapter->pdev, dma_unmap_addr(ce, dma_addr),
			    dma_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
1111
	pr_err("%s: unexpected offload packet, cmd %u\n",
S
Scott Bardone 已提交
1112 1113
	       adapter->name, *skb->data);
	recycle_fl_buf(fl, fl->cidx);
1114 1115
}

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
/*
 * T1/T2 SGE limits the maximum DMA size per TX descriptor to
 * SGE_TX_DESC_MAX_PLEN (16KB). If the PAGE_SIZE is larger than 16KB, the
 * stack might send more than SGE_TX_DESC_MAX_PLEN in a contiguous manner.
 * Note that the *_large_page_tx_descs stuff will be optimized out when
 * PAGE_SIZE <= SGE_TX_DESC_MAX_PLEN.
 *
 * compute_large_page_descs() computes how many additional descriptors are
 * required to break down the stack's request.
 */
static inline unsigned int compute_large_page_tx_descs(struct sk_buff *skb)
{
	unsigned int count = 0;
1129

1130 1131
	if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
		unsigned int nfrags = skb_shinfo(skb)->nr_frags;
E
Eric Dumazet 已提交
1132
		unsigned int i, len = skb_headlen(skb);
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
		while (len > SGE_TX_DESC_MAX_PLEN) {
			count++;
			len -= SGE_TX_DESC_MAX_PLEN;
		}
		for (i = 0; nfrags--; i++) {
			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
			len = frag->size;
			while (len > SGE_TX_DESC_MAX_PLEN) {
				count++;
				len -= SGE_TX_DESC_MAX_PLEN;
			}
		}
	}
	return count;
}

/*
 * Write a cmdQ entry.
 *
 * Since this function writes the 'flags' field, it must not be used to
 * write the first cmdQ entry.
 */
static inline void write_tx_desc(struct cmdQ_e *e, dma_addr_t mapping,
				 unsigned int len, unsigned int gen,
				 unsigned int eop)
{
1159 1160
	BUG_ON(len > SGE_TX_DESC_MAX_PLEN);

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	e->addr_lo = (u32)mapping;
	e->addr_hi = (u64)mapping >> 32;
	e->len_gen = V_CMD_LEN(len) | V_CMD_GEN1(gen);
	e->flags = F_CMD_DATAVALID | V_CMD_EOP(eop) | V_CMD_GEN2(gen);
}

/*
 * See comment for previous function.
 *
 * write_tx_descs_large_page() writes additional SGE tx descriptors if
 * *desc_len exceeds HW's capability.
 */
static inline unsigned int write_large_page_tx_descs(unsigned int pidx,
						     struct cmdQ_e **e,
						     struct cmdQ_ce **ce,
						     unsigned int *gen,
						     dma_addr_t *desc_mapping,
						     unsigned int *desc_len,
						     unsigned int nfrags,
						     struct cmdQ *q)
{
	if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
		struct cmdQ_e *e1 = *e;
		struct cmdQ_ce *ce1 = *ce;

		while (*desc_len > SGE_TX_DESC_MAX_PLEN) {
			*desc_len -= SGE_TX_DESC_MAX_PLEN;
			write_tx_desc(e1, *desc_mapping, SGE_TX_DESC_MAX_PLEN,
				      *gen, nfrags == 0 && *desc_len == 0);
			ce1->skb = NULL;
1191
			dma_unmap_len_set(ce1, dma_len, 0);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
			*desc_mapping += SGE_TX_DESC_MAX_PLEN;
			if (*desc_len) {
				ce1++;
				e1++;
				if (++pidx == q->size) {
					pidx = 0;
					*gen ^= 1;
					ce1 = q->centries;
					e1 = q->entries;
				}
			}
		}
		*e = e1;
		*ce = ce1;
	}
	return pidx;
}

1210
/*
S
Scott Bardone 已提交
1211 1212
 * Write the command descriptors to transmit the given skb starting at
 * descriptor pidx with the given generation.
1213
 */
S
Scott Bardone 已提交
1214 1215 1216
static inline void write_tx_descs(struct adapter *adapter, struct sk_buff *skb,
				  unsigned int pidx, unsigned int gen,
				  struct cmdQ *q)
1217
{
1218
	dma_addr_t mapping, desc_mapping;
S
Scott Bardone 已提交
1219 1220
	struct cmdQ_e *e, *e1;
	struct cmdQ_ce *ce;
1221 1222
	unsigned int i, flags, first_desc_len, desc_len,
	    nfrags = skb_shinfo(skb)->nr_frags;
S
Scott Bardone 已提交
1223

1224
	e = e1 = &q->entries[pidx];
S
Scott Bardone 已提交
1225
	ce = &q->centries[pidx];
1226 1227

	mapping = pci_map_single(adapter->pdev, skb->data,
E
Eric Dumazet 已提交
1228
				 skb_headlen(skb), PCI_DMA_TODEVICE);
1229 1230

	desc_mapping = mapping;
E
Eric Dumazet 已提交
1231
	desc_len = skb_headlen(skb);
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241

	flags = F_CMD_DATAVALID | F_CMD_SOP |
	    V_CMD_EOP(nfrags == 0 && desc_len <= SGE_TX_DESC_MAX_PLEN) |
	    V_CMD_GEN2(gen);
	first_desc_len = (desc_len <= SGE_TX_DESC_MAX_PLEN) ?
	    desc_len : SGE_TX_DESC_MAX_PLEN;
	e->addr_lo = (u32)desc_mapping;
	e->addr_hi = (u64)desc_mapping >> 32;
	e->len_gen = V_CMD_LEN(first_desc_len) | V_CMD_GEN1(gen);
	ce->skb = NULL;
1242
	dma_unmap_len_set(ce, dma_len, 0);
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

	if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN &&
	    desc_len > SGE_TX_DESC_MAX_PLEN) {
		desc_mapping += first_desc_len;
		desc_len -= first_desc_len;
		e1++;
		ce++;
		if (++pidx == q->size) {
			pidx = 0;
			gen ^= 1;
			e1 = q->entries;
			ce = q->centries;
		}
		pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
						 &desc_mapping, &desc_len,
						 nfrags, q);

		if (likely(desc_len))
			write_tx_desc(e1, desc_mapping, desc_len, gen,
				      nfrags == 0);
	}

S
Scott Bardone 已提交
1265
	ce->skb = NULL;
1266
	dma_unmap_addr_set(ce, dma_addr, mapping);
E
Eric Dumazet 已提交
1267
	dma_unmap_len_set(ce, dma_len, skb_headlen(skb));
1268

1269
	for (i = 0; nfrags--; i++) {
S
Scott Bardone 已提交
1270 1271
		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
		e1++;
1272
		ce++;
S
Scott Bardone 已提交
1273 1274 1275 1276
		if (++pidx == q->size) {
			pidx = 0;
			gen ^= 1;
			e1 = q->entries;
1277
			ce = q->centries;
1278 1279
		}

S
Scott Bardone 已提交
1280 1281 1282
		mapping = pci_map_page(adapter->pdev, frag->page,
				       frag->page_offset, frag->size,
				       PCI_DMA_TODEVICE);
1283 1284 1285 1286 1287 1288 1289 1290 1291
		desc_mapping = mapping;
		desc_len = frag->size;

		pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
						 &desc_mapping, &desc_len,
						 nfrags, q);
		if (likely(desc_len))
			write_tx_desc(e1, desc_mapping, desc_len, gen,
				      nfrags == 0);
S
Scott Bardone 已提交
1292
		ce->skb = NULL;
1293 1294
		dma_unmap_addr_set(ce, dma_addr, mapping);
		dma_unmap_len_set(ce, dma_len, frag->size);
1295
	}
S
Scott Bardone 已提交
1296 1297 1298 1299
	ce->skb = skb;
	wmb();
	e->flags = flags;
}
1300

S
Scott Bardone 已提交
1301 1302 1303 1304 1305 1306
/*
 * Clean up completed Tx buffers.
 */
static inline void reclaim_completed_tx(struct sge *sge, struct cmdQ *q)
{
	unsigned int reclaim = q->processed - q->cleaned;
1307

S
Scott Bardone 已提交
1308
	if (reclaim) {
1309 1310
		pr_debug("reclaim_completed_tx processed:%d cleaned:%d\n",
			 q->processed, q->cleaned);
S
Scott Bardone 已提交
1311 1312
		free_cmdQ_buffers(sge, q, reclaim);
		q->cleaned += reclaim;
1313
	}
S
Scott Bardone 已提交
1314
}
1315

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
/*
 * Called from tasklet. Checks the scheduler for any
 * pending skbs that can be sent.
 */
static void restart_sched(unsigned long arg)
{
	struct sge *sge = (struct sge *) arg;
	struct adapter *adapter = sge->adapter;
	struct cmdQ *q = &sge->cmdQ[0];
	struct sk_buff *skb;
	unsigned int credits, queued_skb = 0;

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

	credits = q->size - q->in_use;
	pr_debug("restart_sched credits=%d\n", credits);
	while ((skb = sched_skb(sge, NULL, credits)) != NULL) {
		unsigned int genbit, pidx, count;
	        count = 1 + skb_shinfo(skb)->nr_frags;
1336
		count += compute_large_page_tx_descs(skb);
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
		q->in_use += count;
		genbit = q->genbit;
		pidx = q->pidx;
		q->pidx += count;
		if (q->pidx >= q->size) {
			q->pidx -= q->size;
			q->genbit ^= 1;
		}
		write_tx_descs(adapter, skb, pidx, genbit, q);
	        credits = q->size - q->in_use;
		queued_skb = 1;
	}

	if (queued_skb) {
		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);
		}
	}
	spin_unlock(&q->lock);
}
1359

S
Scott Bardone 已提交
1360 1361 1362 1363 1364
/**
 *	sge_rx - process an ingress ethernet packet
 *	@sge: the sge structure
 *	@fl: the free list that contains the packet buffer
 *	@len: the packet length
1365
 *
S
Scott Bardone 已提交
1366
 *	Process an ingress ethernet pakcet and deliver it to the stack.
1367
 */
S
Stephen Hemminger 已提交
1368
static void sge_rx(struct sge *sge, struct freelQ *fl, unsigned int len)
1369
{
S
Scott Bardone 已提交
1370
	struct sk_buff *skb;
S
Stephen Hemminger 已提交
1371
	const struct cpl_rx_pkt *p;
S
Scott Bardone 已提交
1372
	struct adapter *adapter = sge->adapter;
1373
	struct sge_port_stats *st;
1374
	struct net_device *dev;
1375

S
Stephen Hemminger 已提交
1376
	skb = get_packet(adapter->pdev, fl, len - sge->rx_pkt_pad);
1377 1378
	if (unlikely(!skb)) {
		sge->stats.rx_drops++;
S
Stephen Hemminger 已提交
1379
		return;
1380
	}
S
Scott Bardone 已提交
1381

S
Stephen Hemminger 已提交
1382
	p = (const struct cpl_rx_pkt *) skb->data;
1383 1384
	if (p->iff >= adapter->params.nports) {
		kfree_skb(skb);
S
Stephen Hemminger 已提交
1385
		return;
1386
	}
S
Stephen Hemminger 已提交
1387
	__skb_pull(skb, sizeof(*p));
1388

1389
	st = this_cpu_ptr(sge->port_stats[p->iff]);
1390
	dev = adapter->port[p->iff].dev;
1391

1392 1393
	skb->protocol = eth_type_trans(skb, dev);
	if ((dev->features & NETIF_F_RXCSUM) && p->csum == 0xffff &&
S
Scott Bardone 已提交
1394 1395
	    skb->protocol == htons(ETH_P_IP) &&
	    (skb->data[9] == IPPROTO_TCP || skb->data[9] == IPPROTO_UDP)) {
1396
		++st->rx_cso_good;
S
Scott Bardone 已提交
1397 1398
		skb->ip_summed = CHECKSUM_UNNECESSARY;
	} else
1399
		skb_checksum_none_assert(skb);
S
Scott Bardone 已提交
1400

J
Jiri Pirko 已提交
1401
	if (p->vlan_valid) {
1402
		st->vlan_xtract++;
J
Jiri Pirko 已提交
1403 1404 1405
		__vlan_hwaccel_put_tag(skb, ntohs(p->vlan));
	}
	netif_receive_skb(skb);
1406 1407 1408
}

/*
S
Scott Bardone 已提交
1409
 * Returns true if a command queue has enough available descriptors that
1410 1411
 * we can resume Tx operation after temporarily disabling its packet queue.
 */
S
Scott Bardone 已提交
1412
static inline int enough_free_Tx_descs(const struct cmdQ *q)
1413
{
S
Scott Bardone 已提交
1414 1415 1416
	unsigned int r = q->processed - q->cleaned;

	return q->in_use - r < (q->size >> 1);
1417 1418 1419
}

/*
S
Scott Bardone 已提交
1420 1421
 * 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.
1422
 */
S
Scott Bardone 已提交
1423
static void restart_tx_queues(struct sge *sge)
1424
{
S
Scott Bardone 已提交
1425
	struct adapter *adap = sge->adapter;
F
Francois Romieu 已提交
1426
	int i;
1427

F
Francois Romieu 已提交
1428 1429
	if (!enough_free_Tx_descs(&sge->cmdQ[0]))
		return;
S
Scott Bardone 已提交
1430

F
Francois Romieu 已提交
1431 1432
	for_each_port(adap, i) {
		struct net_device *nd = adap->port[i].dev;
S
Scott Bardone 已提交
1433

F
Francois Romieu 已提交
1434 1435 1436 1437
		if (test_and_clear_bit(nd->if_port, &sge->stopped_tx_queues) &&
		    netif_running(nd)) {
			sge->stats.cmdQ_restarted[2]++;
			netif_wake_queue(nd);
S
Scott Bardone 已提交
1438 1439 1440 1441 1442
		}
	}
}

/*
1443
 * update_tx_info is called from the interrupt handler/NAPI to return cmdQ0
S
Scott Bardone 已提交
1444 1445
 * information.
 */
1446 1447
static unsigned int update_tx_info(struct adapter *adapter,
					  unsigned int flags,
S
Scott Bardone 已提交
1448 1449 1450 1451
					  unsigned int pr0)
{
	struct sge *sge = adapter->sge;
	struct cmdQ *cmdq = &sge->cmdQ[0];
1452

S
Scott Bardone 已提交
1453
	cmdq->processed += pr0;
1454 1455 1456 1457
	if (flags & (F_FL0_ENABLE | F_FL1_ENABLE)) {
		freelQs_empty(sge);
		flags &= ~(F_FL0_ENABLE | F_FL1_ENABLE);
	}
S
Scott Bardone 已提交
1458 1459
	if (flags & F_CMDQ0_ENABLE) {
		clear_bit(CMDQ_STAT_RUNNING, &cmdq->status);
1460

S
Scott Bardone 已提交
1461 1462 1463 1464 1465
		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);
		}
1466 1467 1468 1469
		if (sge->tx_sched)
			tasklet_hi_schedule(&sge->tx_sched->sched_tsk);

		flags &= ~F_CMDQ0_ENABLE;
S
Scott Bardone 已提交
1470
	}
1471

S
Scott Bardone 已提交
1472 1473
	if (unlikely(sge->stopped_tx_queues != 0))
		restart_tx_queues(sge);
1474

S
Scott Bardone 已提交
1475 1476
	return flags;
}
1477

S
Scott Bardone 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486
/*
 * 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];
S
Stephen Hemminger 已提交
1487
	int done = 0;
S
Scott Bardone 已提交
1488 1489
	unsigned int flags = 0;
	unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1490

S
Stephen Hemminger 已提交
1491
	while (done < budget && e->GenerationBit == q->genbit) {
S
Scott Bardone 已提交
1492
		flags |= e->Qsleeping;
1493

S
Scott Bardone 已提交
1494 1495
		cmdq_processed[0] += e->Cmdq0CreditReturn;
		cmdq_processed[1] += e->Cmdq1CreditReturn;
1496

S
Scott Bardone 已提交
1497 1498 1499 1500
		/* 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...
		 */
S
Stephen Hemminger 已提交
1501
		if (unlikely((flags & F_CMDQ0_ENABLE) || cmdq_processed[0] > 64)) {
S
Scott Bardone 已提交
1502 1503 1504
			flags = update_tx_info(adapter, flags, cmdq_processed[0]);
			cmdq_processed[0] = 0;
		}
S
Stephen Hemminger 已提交
1505

S
Scott Bardone 已提交
1506 1507 1508
		if (unlikely(cmdq_processed[1] > 16)) {
			sge->cmdQ[1].processed += cmdq_processed[1];
			cmdq_processed[1] = 0;
1509
		}
S
Stephen Hemminger 已提交
1510

1511
		if (likely(e->DataValid)) {
S
Scott Bardone 已提交
1512 1513
			struct freelQ *fl = &sge->freelQ[e->FreelistQid];

1514
			BUG_ON(!e->Sop || !e->Eop);
S
Scott Bardone 已提交
1515 1516 1517 1518 1519
			if (unlikely(e->Offload))
				unexpected_offload(adapter, fl);
			else
				sge_rx(sge, fl, e->BufferLength);

S
Stephen Hemminger 已提交
1520 1521
			++done;

S
Scott Bardone 已提交
1522 1523 1524 1525 1526 1527
			/*
			 * Note: this depends on each packet consuming a
			 * single free-list buffer; cf. the BUG above.
			 */
			if (++fl->cidx == fl->size)
				fl->cidx = 0;
S
Stephen Hemminger 已提交
1528 1529
			prefetch(fl->centries[fl->cidx].skb);

S
Scott Bardone 已提交
1530 1531 1532 1533 1534
			if (unlikely(--fl->credits <
				     fl->size - SGE_FREEL_REFILL_THRESH))
				refill_free_list(sge, fl);
		} else
			sge->stats.pure_rsps++;
1535 1536

		e++;
S
Scott Bardone 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
		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;
1547 1548 1549
		}
	}

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

S
Stephen Hemminger 已提交
1553
	return done;
S
Scott Bardone 已提交
1554
}
1555

1556 1557 1558 1559 1560
static inline int responses_pending(const struct adapter *adapter)
{
	const struct respQ *Q = &adapter->sge->respQ;
	const struct respQ_e *e = &Q->entries[Q->cidx];

1561
	return e->GenerationBit == Q->genbit;
1562 1563
}

S
Scott Bardone 已提交
1564 1565 1566 1567 1568 1569 1570 1571
/*
 * 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.
 */
1572
static int process_pure_responses(struct adapter *adapter)
S
Scott Bardone 已提交
1573 1574 1575
{
	struct sge *sge = adapter->sge;
	struct respQ *q = &sge->respQ;
1576
	struct respQ_e *e = &q->entries[q->cidx];
S
Stephen Hemminger 已提交
1577
	const struct freelQ *fl = &sge->freelQ[e->FreelistQid];
S
Scott Bardone 已提交
1578 1579
	unsigned int flags = 0;
	unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
1580

S
Stephen Hemminger 已提交
1581
	prefetch(fl->centries[fl->cidx].skb);
1582 1583
	if (e->DataValid)
		return 1;
S
Stephen Hemminger 已提交
1584

S
Scott Bardone 已提交
1585 1586
	do {
		flags |= e->Qsleeping;
1587

S
Scott Bardone 已提交
1588 1589
		cmdq_processed[0] += e->Cmdq0CreditReturn;
		cmdq_processed[1] += e->Cmdq1CreditReturn;
1590

S
Scott Bardone 已提交
1591 1592 1593 1594 1595
		e++;
		if (unlikely(++q->cidx == q->size)) {
			q->cidx = 0;
			q->genbit ^= 1;
			e = q->entries;
1596
		}
S
Scott Bardone 已提交
1597
		prefetch(e);
1598

S
Scott Bardone 已提交
1599 1600 1601
		if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
			writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
			q->credits = 0;
1602
		}
S
Scott Bardone 已提交
1603 1604
		sge->stats.pure_rsps++;
	} while (e->GenerationBit == q->genbit && !e->DataValid);
1605

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

S
Scott Bardone 已提交
1609
	return e->GenerationBit == q->genbit;
1610 1611 1612
}

/*
S
Scott Bardone 已提交
1613 1614 1615
 * 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.
1616
 */
1617
int t1_poll(struct napi_struct *napi, int budget)
1618
{
1619
	struct adapter *adapter = container_of(napi, struct adapter, napi);
D
Divy Le Ray 已提交
1620
	int work_done = process_responses(adapter, budget);
S
Stephen Hemminger 已提交
1621

D
Divy Le Ray 已提交
1622
	if (likely(work_done < budget)) {
1623
		napi_complete(napi);
1624 1625 1626 1627
		writel(adapter->sge->respQ.cidx,
		       adapter->regs + A_SG_SLEEPING);
	}
	return work_done;
S
Scott Bardone 已提交
1628
}
1629

S
Stephen Hemminger 已提交
1630
irqreturn_t t1_interrupt(int irq, void *data)
S
Scott Bardone 已提交
1631 1632 1633
{
	struct adapter *adapter = data;
	struct sge *sge = adapter->sge;
1634
	int handled;
S
Scott Bardone 已提交
1635

1636
	if (likely(responses_pending(adapter))) {
1637
		writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
S
Stephen Hemminger 已提交
1638

1639
		if (napi_schedule_prep(&adapter->napi)) {
1640
			if (process_pure_responses(adapter))
1641
				__napi_schedule(&adapter->napi);
1642 1643 1644
			else {
				/* no data, no NAPI needed */
				writel(sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
1645 1646
				/* undo schedule_prep */
				napi_enable(&adapter->napi);
S
Stephen Hemminger 已提交
1647 1648
			}
		}
1649 1650 1651 1652 1653 1654
		return IRQ_HANDLED;
	}

	spin_lock(&adapter->async_lock);
	handled = t1_slow_intr_handler(adapter);
	spin_unlock(&adapter->async_lock);
S
Stephen Hemminger 已提交
1655

S
Scott Bardone 已提交
1656 1657
	if (!handled)
		sge->stats.unhandled_irqs++;
1658

S
Scott Bardone 已提交
1659 1660
	return IRQ_RETVAL(handled != 0);
}
1661

S
Scott Bardone 已提交
1662 1663 1664 1665 1666 1667
/*
 * 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
L
Lucas De Marchi 已提交
1668
 * uses the corresponding fields on the stack. In conjunction with a spinlock
S
Scott Bardone 已提交
1669 1670 1671 1672 1673 1674
 * 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.
 */
1675 1676
static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
		     unsigned int qid, struct net_device *dev)
S
Scott Bardone 已提交
1677 1678 1679
{
	struct sge *sge = adapter->sge;
	struct cmdQ *q = &sge->cmdQ[qid];
1680
	unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
S
Scott Bardone 已提交
1681

1682 1683 1684
	if (!spin_trylock(&q->lock))
		return NETDEV_TX_LOCKED;

S
Scott Bardone 已提交
1685 1686 1687 1688 1689
	reclaim_completed_tx(sge, q);

	pidx = q->pidx;
	credits = q->size - q->in_use;
	count = 1 + skb_shinfo(skb)->nr_frags;
1690
	count += compute_large_page_tx_descs(skb);
S
Scott Bardone 已提交
1691

1692 1693 1694
	/* Ethernet packet */
	if (unlikely(credits < count)) {
		if (!netif_queue_stopped(dev)) {
S
Scott Bardone 已提交
1695 1696
			netif_stop_queue(dev);
			set_bit(dev->if_port, &sge->stopped_tx_queues);
1697
			sge->stats.cmdQ_full[2]++;
1698
			pr_err("%s: Tx ring full while queue awake!\n",
1699
			       adapter->name);
1700
		}
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
		spin_unlock(&q->lock);
		return NETDEV_TX_BUSY;
	}

	if (unlikely(credits - count < q->stop_thres)) {
		netif_stop_queue(dev);
		set_bit(dev->if_port, &sge->stopped_tx_queues);
		sge->stats.cmdQ_full[2]++;
	}

	/* T204 cmdQ0 skbs that are destined for a certain port have to go
	 * through the scheduler.
	 */
	if (sge->tx_sched && !qid && skb->dev) {
1715
use_sched:
1716 1717 1718 1719 1720 1721 1722 1723
		use_sched_skb = 1;
		/* Note that the scheduler might return a different skb than
		 * the one passed in.
		 */
		skb = sched_skb(sge, skb, credits);
		if (!skb) {
			spin_unlock(&q->lock);
			return NETDEV_TX_OK;
S
Scott Bardone 已提交
1724
		}
1725 1726 1727
		pidx = q->pidx;
		count = 1 + skb_shinfo(skb)->nr_frags;
		count += compute_large_page_tx_descs(skb);
S
Scott Bardone 已提交
1728
	}
1729

S
Scott Bardone 已提交
1730 1731
	q->in_use += count;
	genbit = q->genbit;
1732
	pidx = q->pidx;
S
Scott Bardone 已提交
1733 1734 1735 1736
	q->pidx += count;
	if (q->pidx >= q->size) {
		q->pidx -= q->size;
		q->genbit ^= 1;
1737
	}
S
Scott Bardone 已提交
1738
	spin_unlock(&q->lock);
1739

S
Scott Bardone 已提交
1740
	write_tx_descs(adapter, skb, pidx, genbit, q);
1741 1742 1743 1744 1745 1746 1747 1748

	/*
	 * 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 已提交
1749 1750 1751 1752 1753 1754 1755 1756
	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);
		}
1757
	}
1758 1759 1760 1761 1762 1763 1764 1765

	if (use_sched_skb) {
		if (spin_trylock(&q->lock)) {
			credits = q->size - q->in_use;
			skb = NULL;
			goto use_sched;
		}
	}
1766
	return NETDEV_TX_OK;
1767 1768 1769 1770
}

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

S
Scott Bardone 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
/*
 *	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;
}

1784 1785 1786
/*
 * Adds the CPL header to the sk_buff and passes it to t1_sge_tx.
 */
1787
netdev_tx_t t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
1788
{
1789
	struct adapter *adapter = dev->ml_priv;
S
Scott Bardone 已提交
1790
	struct sge *sge = adapter->sge;
1791
	struct sge_port_stats *st = this_cpu_ptr(sge->port_stats[dev->if_port]);
1792
	struct cpl_tx_pkt *cpl;
1793 1794
	struct sk_buff *orig_skb = skb;
	int ret;
1795

1796 1797 1798
	if (skb->protocol == htons(ETH_P_CPL5))
		goto send;

D
Divy Le Ray 已提交
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
	/*
	 * We are using a non-standard hard_header_len.
	 * Allocate more header room in the rare cases it is not big enough.
	 */
	if (unlikely(skb_headroom(skb) < dev->hard_header_len - ETH_HLEN)) {
		skb = skb_realloc_headroom(skb, sizeof(struct cpl_tx_pkt_lso));
		++st->tx_need_hdrroom;
		dev_kfree_skb_any(orig_skb);
		if (!skb)
			return NETDEV_TX_OK;
	}

1811
	if (skb_shinfo(skb)->gso_size) {
1812 1813 1814
		int eth_type;
		struct cpl_tx_pkt_lso *hdr;

1815
		++st->tx_tso;
S
Scott Bardone 已提交
1816

1817
		eth_type = skb_network_offset(skb) == ETH_HLEN ?
1818 1819 1820 1821 1822
			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;
1823
		hdr->ip_hdr_words = ip_hdr(skb)->ihl;
1824
		hdr->tcp_hdr_words = tcp_hdr(skb)->doff;
1825
		hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
1826
							  skb_shinfo(skb)->gso_size));
1827 1828
		hdr->len = htonl(skb->len - sizeof(*hdr));
		cpl = (struct cpl_tx_pkt *)hdr;
1829
	} else {
1830
		/*
1831
		 * Packets shorter than ETH_HLEN can break the MAC, drop them
S
Scott Bardone 已提交
1832 1833 1834
		 * 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.
1835
		 */
S
Scott Bardone 已提交
1836 1837
		if (unlikely(skb->len < ETH_HLEN ||
			     skb->len > dev->mtu + eth_hdr_len(skb->data))) {
1838 1839
			pr_debug("%s: packet size %d hdr %d mtu%d\n", dev->name,
				 skb->len, eth_hdr_len(skb->data), dev->mtu);
S
Scott Bardone 已提交
1840
			dev_kfree_skb_any(skb);
1841
			return NETDEV_TX_OK;
S
Scott Bardone 已提交
1842 1843
		}

1844
		if (skb->ip_summed == CHECKSUM_PARTIAL &&
1845
		    ip_hdr(skb)->protocol == IPPROTO_UDP) {
1846
			if (unlikely(skb_checksum_help(skb))) {
1847
				pr_debug("%s: unable to do udp checksum\n", dev->name);
S
Scott Bardone 已提交
1848
				dev_kfree_skb_any(skb);
1849
				return NETDEV_TX_OK;
S
Scott Bardone 已提交
1850
			}
1851
		}
1852

S
Scott Bardone 已提交
1853 1854
		/* Hmmm, assuming to catch the gratious arp... and we'll use
		 * it to flush out stuck espi packets...
1855 1856
		 */
		if ((unlikely(!adapter->sge->espibug_skb[dev->if_port]))) {
1857
			if (skb->protocol == htons(ETH_P_ARP) &&
1858
			    arp_hdr(skb)->ar_op == htons(ARPOP_REQUEST)) {
1859
				adapter->sge->espibug_skb[dev->if_port] = skb;
S
Scott Bardone 已提交
1860 1861 1862 1863 1864 1865
				/* We want to re-use this skb later. We
				 * simply bump the reference count and it
				 * will not be freed...
				 */
				skb = skb_get(skb);
			}
1866
		}
S
Scott Bardone 已提交
1867 1868

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

1874
		st->tx_cso += (skb->ip_summed == CHECKSUM_PARTIAL);
1875 1876 1877
	}
	cpl->iff = dev->if_port;

1878
	if (vlan_tx_tag_present(skb)) {
1879 1880
		cpl->vlan_valid = 1;
		cpl->vlan = htons(vlan_tx_tag_get(skb));
S
Scott Bardone 已提交
1881
		st->vlan_insert++;
1882 1883 1884
	} else
		cpl->vlan_valid = 0;

1885
send:
1886 1887 1888 1889 1890 1891
	ret = t1_sge_tx(skb, adapter, 0, dev);

	/* If transmit busy, and we reallocated skb's due to headroom limit,
	 * then silently discard to avoid leak.
	 */
	if (unlikely(ret != NETDEV_TX_OK && skb != orig_skb)) {
1892
		dev_kfree_skb_any(skb);
1893
		ret = NETDEV_TX_OK;
1894
	}
1895
	return ret;
S
Scott Bardone 已提交
1896
}
1897

S
Scott Bardone 已提交
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
/*
 * 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;
1911

S
Scott Bardone 已提交
1912
		reclaim_completed_tx(sge, q);
1913 1914 1915
		if (i == 0 && q->in_use) {    /* flush pending credits */
			writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
		}
S
Scott Bardone 已提交
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
		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->fixed_intrtimer = p->rx_coalesce_usecs *
		core_ticks_per_usec(sge->adapter);
	writel(sge->fixed_intrtimer, sge->adapter->regs + A_SG_INTRTIMER);
1929 1930 1931
	return 0;
}

S
Scott Bardone 已提交
1932 1933 1934 1935 1936
/*
 * 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)
1937
{
S
Scott Bardone 已提交
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
	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;
}
1955

S
Scott Bardone 已提交
1956 1957 1958 1959 1960
/*
 * Disables the DMA engine.
 */
void t1_sge_stop(struct sge *sge)
{
1961
	int i;
S
Scott Bardone 已提交
1962
	writel(0, sge->adapter->regs + A_SG_CONTROL);
1963 1964
	readl(sge->adapter->regs + A_SG_CONTROL); /* flush */

S
Scott Bardone 已提交
1965 1966
	if (is_T2(sge->adapter))
		del_timer_sync(&sge->espibug_timer);
1967

S
Scott Bardone 已提交
1968
	del_timer_sync(&sge->tx_reclaim_timer);
1969 1970 1971 1972
	if (sge->tx_sched)
		tx_sched_stop(sge);

	for (i = 0; i < MAX_NPORTS; i++)
1973
		kfree_skb(sge->espibug_skb[i]);
1974 1975
}

S
Scott Bardone 已提交
1976 1977 1978 1979
/*
 * Enables the DMA engine.
 */
void t1_sge_start(struct sge *sge)
1980
{
S
Scott Bardone 已提交
1981 1982 1983 1984 1985
	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);
1986
	readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
S
Scott Bardone 已提交
1987 1988 1989

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

1990
	if (is_T2(sge->adapter))
S
Scott Bardone 已提交
1991 1992 1993 1994 1995 1996
		mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
}

/*
 * Callback for the T2 ESPI 'stuck packet feature' workaorund
 */
1997
static void espibug_workaround_t204(unsigned long data)
S
Scott Bardone 已提交
1998 1999
{
	struct adapter *adapter = (struct adapter *)data;
2000
	struct sge *sge = adapter->sge;
2001 2002
	unsigned int nports = adapter->params.nports;
	u32 seop[MAX_NPORTS];
2003

2004 2005
	if (adapter->open_device_map & PORT_MASK) {
		int i;
2006 2007

		if (t1_espi_get_mon_t204(adapter, &(seop[0]), 0) < 0)
2008
			return;
2009

2010
		for (i = 0; i < nports; i++) {
2011 2012 2013 2014 2015 2016 2017 2018
			struct sk_buff *skb = sge->espibug_skb[i];

			if (!netif_running(adapter->port[i].dev) ||
			    netif_queue_stopped(adapter->port[i].dev) ||
			    !seop[i] || ((seop[i] & 0xfff) != 0) || !skb)
				continue;

			if (!skb->cb[0]) {
2019 2020 2021 2022 2023 2024 2025 2026
				skb_copy_to_linear_data_offset(skb,
						    sizeof(struct cpl_tx_pkt),
							       ch_mac_addr,
							       ETH_ALEN);
				skb_copy_to_linear_data_offset(skb,
							       skb->len - 10,
							       ch_mac_addr,
							       ETH_ALEN);
2027
				skb->cb[0] = 0xff;
S
Scott Bardone 已提交
2028
			}
2029 2030 2031 2032 2033 2034

			/* 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[i].dev);
S
Scott Bardone 已提交
2035 2036 2037
		}
	}
	mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
2038 2039
}

2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
static void espibug_workaround(unsigned long data)
{
	struct adapter *adapter = (struct adapter *)data;
	struct sge *sge = adapter->sge;

	if (netif_running(adapter->port[0].dev)) {
	        struct sk_buff *skb = sge->espibug_skb[0];
	        u32 seop = t1_espi_get_mon(adapter, 0x930, 0);

	        if ((seop & 0xfff0fff) == 0xfff && skb) {
	                if (!skb->cb[0]) {
2051 2052 2053 2054 2055 2056 2057 2058
	                        skb_copy_to_linear_data_offset(skb,
						     sizeof(struct cpl_tx_pkt),
							       ch_mac_addr,
							       ETH_ALEN);
	                        skb_copy_to_linear_data_offset(skb,
							       skb->len - 10,
							       ch_mac_addr,
							       ETH_ALEN);
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
	                        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);
}

S
Scott Bardone 已提交
2072 2073 2074 2075 2076 2077
/*
 * Creates a t1_sge structure and returns suggested resource parameters.
 */
struct sge * __devinit t1_sge_create(struct adapter *adapter,
				     struct sge_params *p)
{
S
Stephen Hemminger 已提交
2078
	struct sge *sge = kzalloc(sizeof(*sge), GFP_KERNEL);
2079
	int i;
S
Scott Bardone 已提交
2080 2081 2082 2083 2084 2085 2086 2087 2088

	if (!sge)
		return NULL;

	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;

2089 2090 2091 2092 2093 2094
	for_each_port(adapter, i) {
		sge->port_stats[i] = alloc_percpu(struct sge_port_stats);
		if (!sge->port_stats[i])
			goto nomem_port;
	}

S
Scott Bardone 已提交
2095 2096 2097 2098 2099 2100
	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);
2101 2102 2103 2104

		if (adapter->params.nports > 1) {
			tx_sched_init(sge);
			sge->espibug_timer.function = espibug_workaround_t204;
F
Francois Romieu 已提交
2105
		} else
2106
			sge->espibug_timer.function = espibug_workaround;
S
Scott Bardone 已提交
2107
		sge->espibug_timer.data = (unsigned long)sge->adapter;
2108

S
Scott Bardone 已提交
2109
		sge->espibug_timeout = 1;
2110 2111 2112
		/* for T204, every 10ms */
		if (adapter->params.nports > 1)
			sge->espibug_timeout = HZ/100;
S
Scott Bardone 已提交
2113
	}
2114

S
Scott Bardone 已提交
2115 2116 2117 2118 2119

	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;
2120 2121 2122 2123 2124 2125 2126 2127
	if (sge->tx_sched) {
		if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204)
			p->rx_coalesce_usecs = 15;
		else
			p->rx_coalesce_usecs = 50;
	} else
		p->rx_coalesce_usecs = 50;

S
Scott Bardone 已提交
2128 2129 2130 2131
	p->coalesce_enable = 0;
	p->sample_interval_usecs = 0;

	return sge;
2132 2133 2134 2135 2136 2137 2138 2139
nomem_port:
	while (i >= 0) {
		free_percpu(sge->port_stats[i]);
		--i;
	}
	kfree(sge);
	return NULL;

S
Scott Bardone 已提交
2140
}