sunqe.c 25.4 KB
Newer Older
1
/* sunqe.c: Sparc QuadEthernet 10baseT SBUS card driver.
L
Linus Torvalds 已提交
2 3 4 5
 *          Once again I am out to prove that every ethernet
 *          controller out there can be most efficiently programmed
 *          if you make it look like a LANCE.
 *
6
 * Copyright (C) 1996, 1999, 2003, 2006, 2008 David S. Miller (davem@davemloft.net)
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/crc32.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/ethtool.h>
#include <linux/bitops.h>
27
#include <linux/dma-mapping.h>
28 29
#include <linux/of.h>
#include <linux/of_device.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43

#include <asm/system.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <asm/byteorder.h>
#include <asm/idprom.h>
#include <asm/openprom.h>
#include <asm/oplib.h>
#include <asm/auxio.h>
#include <asm/pgtable.h>
#include <asm/irq.h>

#include "sunqe.h"

44
#define DRV_NAME	"sunqe"
45 46
#define DRV_VERSION	"4.1"
#define DRV_RELDATE	"August 27, 2008"
47
#define DRV_AUTHOR	"David S. Miller (davem@davemloft.net)"
48 49 50 51 52 53 54 55 56

static char version[] =
	DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";

MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION("Sun QuadEthernet 10baseT SBUS card driver");
MODULE_LICENSE("GPL");

L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
static struct sunqec *root_qec_dev;

static void qe_set_multicast(struct net_device *dev);

#define QEC_RESET_TRIES 200

static inline int qec_global_reset(void __iomem *gregs)
{
	int tries = QEC_RESET_TRIES;

	sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
	while (--tries) {
		u32 tmp = sbus_readl(gregs + GLOB_CTRL);
		if (tmp & GLOB_CTRL_RESET) {
			udelay(20);
			continue;
		}
		break;
	}
	if (tries)
		return 0;
	printk(KERN_ERR "QuadEther: AIEEE cannot reset the QEC!\n");
	return -1;
}

#define MACE_RESET_RETRIES 200
#define QE_RESET_RETRIES   200

static inline int qe_stop(struct sunqe *qep)
{
	void __iomem *cregs = qep->qcregs;
	void __iomem *mregs = qep->mregs;
	int tries;

	/* Reset the MACE, then the QEC channel. */
	sbus_writeb(MREGS_BCONFIG_RESET, mregs + MREGS_BCONFIG);
	tries = MACE_RESET_RETRIES;
	while (--tries) {
		u8 tmp = sbus_readb(mregs + MREGS_BCONFIG);
		if (tmp & MREGS_BCONFIG_RESET) {
			udelay(20);
			continue;
		}
		break;
	}
	if (!tries) {
		printk(KERN_ERR "QuadEther: AIEEE cannot reset the MACE!\n");
		return -1;
	}

	sbus_writel(CREG_CTRL_RESET, cregs + CREG_CTRL);
	tries = QE_RESET_RETRIES;
	while (--tries) {
		u32 tmp = sbus_readl(cregs + CREG_CTRL);
		if (tmp & CREG_CTRL_RESET) {
			udelay(20);
			continue;
		}
		break;
	}
	if (!tries) {
		printk(KERN_ERR "QuadEther: Cannot reset QE channel!\n");
		return -1;
	}
	return 0;
}

static void qe_init_rings(struct sunqe *qep)
{
	struct qe_init_block *qb = qep->qe_block;
	struct sunqe_buffers *qbufs = qep->buffers;
	__u32 qbufs_dvma = qep->buffers_dvma;
	int i;

	qep->rx_new = qep->rx_old = qep->tx_new = qep->tx_old = 0;
	memset(qb, 0, sizeof(struct qe_init_block));
	memset(qbufs, 0, sizeof(struct sunqe_buffers));
	for (i = 0; i < RX_RING_SIZE; i++) {
		qb->qe_rxd[i].rx_addr = qbufs_dvma + qebuf_offset(rx_buf, i);
		qb->qe_rxd[i].rx_flags =
			(RXD_OWN | ((RXD_PKT_SZ) & RXD_LENGTH));
	}
}

static int qe_init(struct sunqe *qep, int from_irq)
{
	struct sunqec *qecp = qep->parent;
	void __iomem *cregs = qep->qcregs;
	void __iomem *mregs = qep->mregs;
	void __iomem *gregs = qecp->gregs;
	unsigned char *e = &qep->dev->dev_addr[0];
	u32 tmp;
	int i;

	/* Shut it up. */
	if (qe_stop(qep))
		return -EAGAIN;

	/* Setup initial rx/tx init block pointers. */
	sbus_writel(qep->qblock_dvma + qib_offset(qe_rxd, 0), cregs + CREG_RXDS);
	sbus_writel(qep->qblock_dvma + qib_offset(qe_txd, 0), cregs + CREG_TXDS);

	/* Enable/mask the various irq's. */
	sbus_writel(0, cregs + CREG_RIMASK);
	sbus_writel(1, cregs + CREG_TIMASK);

	sbus_writel(0, cregs + CREG_QMASK);
	sbus_writel(CREG_MMASK_RXCOLL, cregs + CREG_MMASK);

	/* Setup the FIFO pointers into QEC local memory. */
	tmp = qep->channel * sbus_readl(gregs + GLOB_MSIZE);
	sbus_writel(tmp, cregs + CREG_RXRBUFPTR);
	sbus_writel(tmp, cregs + CREG_RXWBUFPTR);

	tmp = sbus_readl(cregs + CREG_RXRBUFPTR) +
		sbus_readl(gregs + GLOB_RSIZE);
	sbus_writel(tmp, cregs + CREG_TXRBUFPTR);
	sbus_writel(tmp, cregs + CREG_TXWBUFPTR);

	/* Clear the channel collision counter. */
	sbus_writel(0, cregs + CREG_CCNT);

	/* For 10baseT, inter frame space nor throttle seems to be necessary. */
	sbus_writel(0, cregs + CREG_PIPG);

	/* Now dork with the AMD MACE. */
	sbus_writeb(MREGS_PHYCONFIG_AUTO, mregs + MREGS_PHYCONFIG);
	sbus_writeb(MREGS_TXFCNTL_AUTOPAD, mregs + MREGS_TXFCNTL);
	sbus_writeb(0, mregs + MREGS_RXFCNTL);

	/* The QEC dma's the rx'd packets from local memory out to main memory,
	 * and therefore it interrupts when the packet reception is "complete".
	 * So don't listen for the MACE talking about it.
	 */
	sbus_writeb(MREGS_IMASK_COLL | MREGS_IMASK_RXIRQ, mregs + MREGS_IMASK);
	sbus_writeb(MREGS_BCONFIG_BSWAP | MREGS_BCONFIG_64TS, mregs + MREGS_BCONFIG);
	sbus_writeb((MREGS_FCONFIG_TXF16 | MREGS_FCONFIG_RXF32 |
		     MREGS_FCONFIG_RFWU | MREGS_FCONFIG_TFWU),
		    mregs + MREGS_FCONFIG);

	/* Only usable interface on QuadEther is twisted pair. */
	sbus_writeb(MREGS_PLSCONFIG_TP, mregs + MREGS_PLSCONFIG);

	/* Tell MACE we are changing the ether address. */
	sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_PARESET,
		    mregs + MREGS_IACONFIG);
	while ((sbus_readb(mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
		barrier();
	sbus_writeb(e[0], mregs + MREGS_ETHADDR);
	sbus_writeb(e[1], mregs + MREGS_ETHADDR);
	sbus_writeb(e[2], mregs + MREGS_ETHADDR);
	sbus_writeb(e[3], mregs + MREGS_ETHADDR);
	sbus_writeb(e[4], mregs + MREGS_ETHADDR);
	sbus_writeb(e[5], mregs + MREGS_ETHADDR);

	/* Clear out the address filter. */
	sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
		    mregs + MREGS_IACONFIG);
	while ((sbus_readb(mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
		barrier();
	for (i = 0; i < 8; i++)
		sbus_writeb(0, mregs + MREGS_FILTER);

	/* Address changes are now complete. */
	sbus_writeb(0, mregs + MREGS_IACONFIG);

	qe_init_rings(qep);

	/* Wait a little bit for the link to come up... */
	mdelay(5);
	if (!(sbus_readb(mregs + MREGS_PHYCONFIG) & MREGS_PHYCONFIG_LTESTDIS)) {
		int tries = 50;

230
		while (--tries) {
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
			u8 tmp;

			mdelay(5);
			barrier();
			tmp = sbus_readb(mregs + MREGS_PHYCONFIG);
			if ((tmp & MREGS_PHYCONFIG_LSTAT) != 0)
				break;
		}
		if (tries == 0)
			printk(KERN_NOTICE "%s: Warning, link state is down.\n", qep->dev->name);
	}

	/* Missed packet counter is cleared on a read. */
	sbus_readb(mregs + MREGS_MPCNT);

	/* Reload multicast information, this will enable the receiver
	 * and transmitter.
	 */
	qe_set_multicast(qep->dev);

	/* QEC should now start to show interrupts. */
	return 0;
}

/* Grrr, certain error conditions completely lock up the AMD MACE,
 * so when we get these we _must_ reset the chip.
 */
static int qe_is_bolixed(struct sunqe *qep, u32 qe_status)
{
	struct net_device *dev = qep->dev;
	int mace_hwbug_workaround = 0;

	if (qe_status & CREG_STAT_EDEFER) {
		printk(KERN_ERR "%s: Excessive transmit defers.\n", dev->name);
265
		dev->stats.tx_errors++;
L
Linus Torvalds 已提交
266 267 268 269
	}

	if (qe_status & CREG_STAT_CLOSS) {
		printk(KERN_ERR "%s: Carrier lost, link down?\n", dev->name);
270 271
		dev->stats.tx_errors++;
		dev->stats.tx_carrier_errors++;
L
Linus Torvalds 已提交
272 273 274 275
	}

	if (qe_status & CREG_STAT_ERETRIES) {
		printk(KERN_ERR "%s: Excessive transmit retries (more than 16).\n", dev->name);
276
		dev->stats.tx_errors++;
L
Linus Torvalds 已提交
277 278 279 280 281
		mace_hwbug_workaround = 1;
	}

	if (qe_status & CREG_STAT_LCOLL) {
		printk(KERN_ERR "%s: Late transmit collision.\n", dev->name);
282 283
		dev->stats.tx_errors++;
		dev->stats.collisions++;
L
Linus Torvalds 已提交
284 285 286 287 288
		mace_hwbug_workaround = 1;
	}

	if (qe_status & CREG_STAT_FUFLOW) {
		printk(KERN_ERR "%s: Transmit fifo underflow, driver bug.\n", dev->name);
289
		dev->stats.tx_errors++;
L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298 299 300 301
		mace_hwbug_workaround = 1;
	}

	if (qe_status & CREG_STAT_JERROR) {
		printk(KERN_ERR "%s: Jabber error.\n", dev->name);
	}

	if (qe_status & CREG_STAT_BERROR) {
		printk(KERN_ERR "%s: Babble error.\n", dev->name);
	}

	if (qe_status & CREG_STAT_CCOFLOW) {
302 303
		dev->stats.tx_errors += 256;
		dev->stats.collisions += 256;
L
Linus Torvalds 已提交
304 305 306 307
	}

	if (qe_status & CREG_STAT_TXDERROR) {
		printk(KERN_ERR "%s: Transmit descriptor is bogus, driver bug.\n", dev->name);
308 309
		dev->stats.tx_errors++;
		dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
310 311 312 313 314
		mace_hwbug_workaround = 1;
	}

	if (qe_status & CREG_STAT_TXLERR) {
		printk(KERN_ERR "%s: Transmit late error.\n", dev->name);
315
		dev->stats.tx_errors++;
L
Linus Torvalds 已提交
316 317 318 319 320
		mace_hwbug_workaround = 1;
	}

	if (qe_status & CREG_STAT_TXPERR) {
		printk(KERN_ERR "%s: Transmit DMA parity error.\n", dev->name);
321 322
		dev->stats.tx_errors++;
		dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
323 324 325 326 327
		mace_hwbug_workaround = 1;
	}

	if (qe_status & CREG_STAT_TXSERR) {
		printk(KERN_ERR "%s: Transmit DMA sbus error ack.\n", dev->name);
328 329
		dev->stats.tx_errors++;
		dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
330 331 332 333
		mace_hwbug_workaround = 1;
	}

	if (qe_status & CREG_STAT_RCCOFLOW) {
334 335
		dev->stats.rx_errors += 256;
		dev->stats.collisions += 256;
L
Linus Torvalds 已提交
336 337 338
	}

	if (qe_status & CREG_STAT_RUOFLOW) {
339 340
		dev->stats.rx_errors += 256;
		dev->stats.rx_over_errors += 256;
L
Linus Torvalds 已提交
341 342 343
	}

	if (qe_status & CREG_STAT_MCOFLOW) {
344 345
		dev->stats.rx_errors += 256;
		dev->stats.rx_missed_errors += 256;
L
Linus Torvalds 已提交
346 347 348 349
	}

	if (qe_status & CREG_STAT_RXFOFLOW) {
		printk(KERN_ERR "%s: Receive fifo overflow.\n", dev->name);
350 351
		dev->stats.rx_errors++;
		dev->stats.rx_over_errors++;
L
Linus Torvalds 已提交
352 353 354 355
	}

	if (qe_status & CREG_STAT_RLCOLL) {
		printk(KERN_ERR "%s: Late receive collision.\n", dev->name);
356 357
		dev->stats.rx_errors++;
		dev->stats.collisions++;
L
Linus Torvalds 已提交
358 359 360
	}

	if (qe_status & CREG_STAT_FCOFLOW) {
361 362
		dev->stats.rx_errors += 256;
		dev->stats.rx_frame_errors += 256;
L
Linus Torvalds 已提交
363 364 365
	}

	if (qe_status & CREG_STAT_CECOFLOW) {
366 367
		dev->stats.rx_errors += 256;
		dev->stats.rx_crc_errors += 256;
L
Linus Torvalds 已提交
368 369 370 371
	}

	if (qe_status & CREG_STAT_RXDROP) {
		printk(KERN_ERR "%s: Receive packet dropped.\n", dev->name);
372 373 374
		dev->stats.rx_errors++;
		dev->stats.rx_dropped++;
		dev->stats.rx_missed_errors++;
L
Linus Torvalds 已提交
375 376 377 378
	}

	if (qe_status & CREG_STAT_RXSMALL) {
		printk(KERN_ERR "%s: Receive buffer too small, driver bug.\n", dev->name);
379 380
		dev->stats.rx_errors++;
		dev->stats.rx_length_errors++;
L
Linus Torvalds 已提交
381 382 383 384
	}

	if (qe_status & CREG_STAT_RXLERR) {
		printk(KERN_ERR "%s: Receive late error.\n", dev->name);
385
		dev->stats.rx_errors++;
L
Linus Torvalds 已提交
386 387 388 389 390
		mace_hwbug_workaround = 1;
	}

	if (qe_status & CREG_STAT_RXPERR) {
		printk(KERN_ERR "%s: Receive DMA parity error.\n", dev->name);
391 392
		dev->stats.rx_errors++;
		dev->stats.rx_missed_errors++;
L
Linus Torvalds 已提交
393 394 395 396 397
		mace_hwbug_workaround = 1;
	}

	if (qe_status & CREG_STAT_RXSERR) {
		printk(KERN_ERR "%s: Receive DMA sbus error ack.\n", dev->name);
398 399
		dev->stats.rx_errors++;
		dev->stats.rx_missed_errors++;
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413
		mace_hwbug_workaround = 1;
	}

	if (mace_hwbug_workaround)
		qe_init(qep, 1);
	return mace_hwbug_workaround;
}

/* Per-QE receive interrupt service routine.  Just like on the happy meal
 * we receive directly into skb's with a small packet copy water mark.
 */
static void qe_rx(struct sunqe *qep)
{
	struct qe_rxd *rxbase = &qep->qe_block->qe_rxd[0];
414
	struct net_device *dev = qep->dev;
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	struct qe_rxd *this;
	struct sunqe_buffers *qbufs = qep->buffers;
	__u32 qbufs_dvma = qep->buffers_dvma;
	int elem = qep->rx_new, drops = 0;
	u32 flags;

	this = &rxbase[elem];
	while (!((flags = this->rx_flags) & RXD_OWN)) {
		struct sk_buff *skb;
		unsigned char *this_qbuf =
			&qbufs->rx_buf[elem & (RX_RING_SIZE - 1)][0];
		__u32 this_qbuf_dvma = qbufs_dvma +
			qebuf_offset(rx_buf, (elem & (RX_RING_SIZE - 1)));
		struct qe_rxd *end_rxd =
			&rxbase[(elem+RX_RING_SIZE)&(RX_RING_MAXSIZE-1)];
		int len = (flags & RXD_LENGTH) - 4;  /* QE adds ether FCS size to len */

		/* Check for errors. */
		if (len < ETH_ZLEN) {
434 435 436
			dev->stats.rx_errors++;
			dev->stats.rx_length_errors++;
			dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
437 438 439 440
		} else {
			skb = dev_alloc_skb(len + 2);
			if (skb == NULL) {
				drops++;
441
				dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
442 443 444
			} else {
				skb_reserve(skb, 2);
				skb_put(skb, len);
445 446
				skb_copy_to_linear_data(skb, (unsigned char *) this_qbuf,
						 len);
L
Linus Torvalds 已提交
447 448
				skb->protocol = eth_type_trans(skb, qep->dev);
				netif_rx(skb);
449 450
				dev->stats.rx_packets++;
				dev->stats.rx_bytes += len;
L
Linus Torvalds 已提交
451 452 453 454
			}
		}
		end_rxd->rx_addr = this_qbuf_dvma;
		end_rxd->rx_flags = (RXD_OWN | ((RXD_PKT_SZ) & RXD_LENGTH));
455

L
Linus Torvalds 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469
		elem = NEXT_RX(elem);
		this = &rxbase[elem];
	}
	qep->rx_new = elem;
	if (drops)
		printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", qep->dev->name);
}

static void qe_tx_reclaim(struct sunqe *qep);

/* Interrupts for all QE's get filtered out via the QEC master controller,
 * so we just run through each qe and check to see who is signaling
 * and thus needs to be serviced.
 */
470
static irqreturn_t qec_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
471
{
472
	struct sunqec *qecp = dev_id;
L
Linus Torvalds 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
	u32 qec_status;
	int channel = 0;

	/* Latch the status now. */
	qec_status = sbus_readl(qecp->gregs + GLOB_STAT);
	while (channel < 4) {
		if (qec_status & 0xf) {
			struct sunqe *qep = qecp->qes[channel];
			u32 qe_status;

			qe_status = sbus_readl(qep->qcregs + CREG_STAT);
			if (qe_status & CREG_STAT_ERRORS) {
				if (qe_is_bolixed(qep, qe_status))
					goto next;
			}
			if (qe_status & CREG_STAT_RXIRQ)
				qe_rx(qep);
			if (netif_queue_stopped(qep->dev) &&
			    (qe_status & CREG_STAT_TXIRQ)) {
				spin_lock(&qep->lock);
				qe_tx_reclaim(qep);
				if (TX_BUFFS_AVAIL(qep) > 0) {
					/* Wake net queue and return to
					 * lazy tx reclaim.
					 */
					netif_wake_queue(qep->dev);
					sbus_writel(1, qep->qcregs + CREG_TIMASK);
				}
				spin_unlock(&qep->lock);
			}
	next:
			;
		}
		qec_status >>= 4;
		channel++;
	}

	return IRQ_HANDLED;
}

static int qe_open(struct net_device *dev)
{
515
	struct sunqe *qep = netdev_priv(dev);
L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523 524

	qep->mconfig = (MREGS_MCONFIG_TXENAB |
			MREGS_MCONFIG_RXENAB |
			MREGS_MCONFIG_MBAENAB);
	return qe_init(qep, 0);
}

static int qe_close(struct net_device *dev)
{
525
	struct sunqe *qep = netdev_priv(dev);
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550

	qe_stop(qep);
	return 0;
}

/* Reclaim TX'd frames from the ring.  This must always run under
 * the IRQ protected qep->lock.
 */
static void qe_tx_reclaim(struct sunqe *qep)
{
	struct qe_txd *txbase = &qep->qe_block->qe_txd[0];
	int elem = qep->tx_old;

	while (elem != qep->tx_new) {
		u32 flags = txbase[elem].tx_flags;

		if (flags & TXD_OWN)
			break;
		elem = NEXT_TX(elem);
	}
	qep->tx_old = elem;
}

static void qe_tx_timeout(struct net_device *dev)
{
551
	struct sunqe *qep = netdev_priv(dev);
L
Linus Torvalds 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	int tx_full;

	spin_lock_irq(&qep->lock);

	/* Try to reclaim, if that frees up some tx
	 * entries, we're fine.
	 */
	qe_tx_reclaim(qep);
	tx_full = TX_BUFFS_AVAIL(qep) <= 0;

	spin_unlock_irq(&qep->lock);

	if (! tx_full)
		goto out;

	printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
	qe_init(qep, 1);

out:
	netif_wake_queue(dev);
}

/* Get a packet queued to go onto the wire. */
static int qe_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
577
	struct sunqe *qep = netdev_priv(dev);
L
Linus Torvalds 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	struct sunqe_buffers *qbufs = qep->buffers;
	__u32 txbuf_dvma, qbufs_dvma = qep->buffers_dvma;
	unsigned char *txbuf;
	int len, entry;

	spin_lock_irq(&qep->lock);

	qe_tx_reclaim(qep);

	len = skb->len;
	entry = qep->tx_new;

	txbuf = &qbufs->tx_buf[entry & (TX_RING_SIZE - 1)][0];
	txbuf_dvma = qbufs_dvma +
		qebuf_offset(tx_buf, (entry & (TX_RING_SIZE - 1)));

	/* Avoid a race... */
	qep->qe_block->qe_txd[entry].tx_flags = TXD_UPDATE;

597
	skb_copy_from_linear_data(skb, txbuf, len);
L
Linus Torvalds 已提交
598 599 600 601 602 603 604 605 606

	qep->qe_block->qe_txd[entry].tx_addr = txbuf_dvma;
	qep->qe_block->qe_txd[entry].tx_flags =
		(TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
	qep->tx_new = NEXT_TX(entry);

	/* Get it going. */
	sbus_writel(CREG_CTRL_TWAKEUP, qep->qcregs + CREG_CTRL);

607 608
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += len;
L
Linus Torvalds 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621 622

	if (TX_BUFFS_AVAIL(qep) <= 0) {
		/* Halt the net queue and enable tx interrupts.
		 * When the tx queue empties the tx irq handler
		 * will wake up the queue and return us back to
		 * the lazy tx reclaim scheme.
		 */
		netif_stop_queue(dev);
		sbus_writel(0, qep->qcregs + CREG_TIMASK);
	}
	spin_unlock_irq(&qep->lock);

	dev_kfree_skb(skb);

623
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
624 625 626 627
}

static void qe_set_multicast(struct net_device *dev)
{
628
	struct sunqe *qep = netdev_priv(dev);
629
	struct netdev_hw_addr *ha;
L
Linus Torvalds 已提交
630 631 632 633 634 635 636
	u8 new_mconfig = qep->mconfig;
	int i;
	u32 crc;

	/* Lock out others. */
	netif_stop_queue(dev);

637
	if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650
		sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
			    qep->mregs + MREGS_IACONFIG);
		while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
			barrier();
		for (i = 0; i < 8; i++)
			sbus_writeb(0xff, qep->mregs + MREGS_FILTER);
		sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
	} else if (dev->flags & IFF_PROMISC) {
		new_mconfig |= MREGS_MCONFIG_PROMISC;
	} else {
		u16 hash_table[4];
		u8 *hbytes = (unsigned char *) &hash_table[0];

651
		memset(hash_table, 0, sizeof(hash_table));
652
		netdev_for_each_mc_addr(ha, dev) {
653
			crc = ether_crc_le(6, ha->addr);
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
			crc >>= 26;
			hash_table[crc >> 4] |= 1 << (crc & 0xf);
		}
		/* Program the qe with the new filter value. */
		sbus_writeb(MREGS_IACONFIG_ACHNGE | MREGS_IACONFIG_LARESET,
			    qep->mregs + MREGS_IACONFIG);
		while ((sbus_readb(qep->mregs + MREGS_IACONFIG) & MREGS_IACONFIG_ACHNGE) != 0)
			barrier();
		for (i = 0; i < 8; i++) {
			u8 tmp = *hbytes++;
			sbus_writeb(tmp, qep->mregs + MREGS_FILTER);
		}
		sbus_writeb(0, qep->mregs + MREGS_IACONFIG);
	}

	/* Any change of the logical address filter, the physical address,
	 * or enabling/disabling promiscuous mode causes the MACE to disable
	 * the receiver.  So we must re-enable them here or else the MACE
	 * refuses to listen to anything on the network.  Sheesh, took
	 * me a day or two to find this bug.
	 */
	qep->mconfig = new_mconfig;
	sbus_writeb(qep->mconfig, qep->mregs + MREGS_MCONFIG);

	/* Let us get going again. */
	netif_wake_queue(dev);
}

/* Ethtool support... */
static void qe_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
{
685
	const struct linux_prom_registers *regs;
686
	struct sunqe *qep = netdev_priv(dev);
687
	struct platform_device *op;
L
Linus Torvalds 已提交
688 689 690

	strcpy(info->driver, "sunqe");
	strcpy(info->version, "3.0");
691 692

	op = qep->op;
693
	regs = of_get_property(op->dev.of_node, "reg", NULL);
694 695 696
	if (regs)
		sprintf(info->bus_info, "SBUS:%d", regs->which_io);

L
Linus Torvalds 已提交
697 698 699 700
}

static u32 qe_get_link(struct net_device *dev)
{
701
	struct sunqe *qep = netdev_priv(dev);
L
Linus Torvalds 已提交
702 703 704 705 706 707 708
	void __iomem *mregs = qep->mregs;
	u8 phyconfig;

	spin_lock_irq(&qep->lock);
	phyconfig = sbus_readb(mregs + MREGS_PHYCONFIG);
	spin_unlock_irq(&qep->lock);

709
	return phyconfig & MREGS_PHYCONFIG_LSTAT;
L
Linus Torvalds 已提交
710 711
}

712
static const struct ethtool_ops qe_ethtool_ops = {
L
Linus Torvalds 已提交
713 714 715 716 717
	.get_drvinfo		= qe_get_drvinfo,
	.get_link		= qe_get_link,
};

/* This is only called once at boot time for each card probed. */
718
static void qec_init_once(struct sunqec *qecp, struct platform_device *op)
L
Linus Torvalds 已提交
719 720 721
{
	u8 bsizes = qecp->qec_bursts;

722
	if (sbus_can_burst64() && (bsizes & DMA_BURST64)) {
L
Linus Torvalds 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735
		sbus_writel(GLOB_CTRL_B64, qecp->gregs + GLOB_CTRL);
	} else if (bsizes & DMA_BURST32) {
		sbus_writel(GLOB_CTRL_B32, qecp->gregs + GLOB_CTRL);
	} else {
		sbus_writel(GLOB_CTRL_B16, qecp->gregs + GLOB_CTRL);
	}

	/* Packetsize only used in 100baseT BigMAC configurations,
	 * set it to zero just to be on the safe side.
	 */
	sbus_writel(GLOB_PSIZE_2048, qecp->gregs + GLOB_PSIZE);

	/* Set the local memsize register, divided up to one piece per QE channel. */
736
	sbus_writel((resource_size(&op->resource[1]) >> 2),
L
Linus Torvalds 已提交
737 738 739 740 741
		    qecp->gregs + GLOB_MSIZE);

	/* Divide up the local QEC memory amongst the 4 QE receiver and
	 * transmitter FIFOs.  Basically it is (total / 2 / num_channels).
	 */
742
	sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
L
Linus Torvalds 已提交
743
		    qecp->gregs + GLOB_TSIZE);
744
	sbus_writel((resource_size(&op->resource[1]) >> 2) >> 1,
L
Linus Torvalds 已提交
745 746 747
		    qecp->gregs + GLOB_RSIZE);
}

A
Adrian Bunk 已提交
748
static u8 __devinit qec_get_burst(struct device_node *dp)
L
Linus Torvalds 已提交
749 750 751
{
	u8 bsizes, bsizes_more;

752 753 754 755 756 757
	/* Find and set the burst sizes for the QEC, since it
	 * does the actual dma for all 4 channels.
	 */
	bsizes = of_getintprop_default(dp, "burst-sizes", 0xff);
	bsizes &= 0xff;
	bsizes_more = of_getintprop_default(dp->parent, "burst-sizes", 0xff);
L
Linus Torvalds 已提交
758

759 760 761 762 763
	if (bsizes_more != 0xff)
		bsizes &= bsizes_more;
	if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
	    (bsizes & DMA_BURST32)==0)
		bsizes = (DMA_BURST32 - 1);
L
Linus Torvalds 已提交
764

765 766
	return bsizes;
}
L
Linus Torvalds 已提交
767

768
static struct sunqec * __devinit get_qec(struct platform_device *child)
769
{
770
	struct platform_device *op = to_platform_device(child->dev.parent);
771
	struct sunqec *qecp;
L
Linus Torvalds 已提交
772

773
	qecp = dev_get_drvdata(&op->dev);
774 775 776 777 778
	if (!qecp) {
		qecp = kzalloc(sizeof(struct sunqec), GFP_KERNEL);
		if (qecp) {
			u32 ctrl;

779 780 781 782
			qecp->op = op;
			qecp->gregs = of_ioremap(&op->resource[0], 0,
						 GLOB_REG_SIZE,
						 "QEC Global Registers");
783 784 785 786 787 788 789 790 791 792
			if (!qecp->gregs)
				goto fail;

			/* Make sure the QEC is in MACE mode. */
			ctrl = sbus_readl(qecp->gregs + GLOB_CTRL);
			ctrl &= 0xf0000000;
			if (ctrl != GLOB_CTRL_MMODE) {
				printk(KERN_ERR "qec: Not in MACE mode!\n");
				goto fail;
			}
L
Linus Torvalds 已提交
793

794 795
			if (qec_global_reset(qecp->gregs))
				goto fail;
L
Linus Torvalds 已提交
796

797
			qecp->qec_bursts = qec_get_burst(op->dev.of_node);
L
Linus Torvalds 已提交
798

799
			qec_init_once(qecp, op);
L
Linus Torvalds 已提交
800

801
			if (request_irq(op->archdata.irqs[0], qec_interrupt,
802
					IRQF_SHARED, "qec", (void *) qecp)) {
803 804 805
				printk(KERN_ERR "qec: Can't register irq.\n");
				goto fail;
			}
L
Linus Torvalds 已提交
806

807 808
			dev_set_drvdata(&op->dev, qecp);

809 810 811
			qecp->next_module = root_qec_dev;
			root_qec_dev = qecp;
		}
L
Linus Torvalds 已提交
812 813
	}

814
	return qecp;
L
Linus Torvalds 已提交
815

816 817
fail:
	if (qecp->gregs)
818
		of_iounmap(&op->resource[0], qecp->gregs, GLOB_REG_SIZE);
819 820 821
	kfree(qecp);
	return NULL;
}
L
Linus Torvalds 已提交
822

823 824 825 826 827 828
static const struct net_device_ops qec_ops = {
	.ndo_open		= qe_open,
	.ndo_stop		= qe_close,
	.ndo_start_xmit		= qe_start_xmit,
	.ndo_set_multicast_list	= qe_set_multicast,
	.ndo_tx_timeout		= qe_tx_timeout,
829 830 831
	.ndo_change_mtu		= eth_change_mtu,
	.ndo_set_mac_address	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
832 833
};

834
static int __devinit qec_ether_init(struct platform_device *op)
835 836 837 838
{
	static unsigned version_printed;
	struct net_device *dev;
	struct sunqec *qecp;
839
	struct sunqe *qe;
840
	int i, res;
L
Linus Torvalds 已提交
841

842 843
	if (version_printed++ == 0)
		printk(KERN_INFO "%s", version);
L
Linus Torvalds 已提交
844

845 846 847
	dev = alloc_etherdev(sizeof(struct sunqe));
	if (!dev)
		return -ENOMEM;
L
Linus Torvalds 已提交
848

849 850
	memcpy(dev->dev_addr, idprom->id_ethaddr, 6);

851
	qe = netdev_priv(dev);
L
Linus Torvalds 已提交
852

853 854
	res = -ENODEV;

855
	i = of_getintprop_default(op->dev.of_node, "channel#", -1);
856 857
	if (i == -1)
		goto fail;
858 859
	qe->channel = i;
	spin_lock_init(&qe->lock);
860

861
	qecp = get_qec(op);
862 863
	if (!qecp)
		goto fail;
L
Linus Torvalds 已提交
864

865 866 867
	qecp->qes[qe->channel] = qe;
	qe->dev = dev;
	qe->parent = qecp;
868
	qe->op = op;
L
Linus Torvalds 已提交
869

870
	res = -ENOMEM;
871 872
	qe->qcregs = of_ioremap(&op->resource[0], 0,
				CREG_REG_SIZE, "QEC Channel Registers");
873 874 875
	if (!qe->qcregs) {
		printk(KERN_ERR "qe: Cannot map channel registers.\n");
		goto fail;
L
Linus Torvalds 已提交
876 877
	}

878 879
	qe->mregs = of_ioremap(&op->resource[1], 0,
			       MREGS_REG_SIZE, "QE MACE Registers");
880 881 882
	if (!qe->mregs) {
		printk(KERN_ERR "qe: Cannot map MACE registers.\n");
		goto fail;
L
Linus Torvalds 已提交
883 884
	}

885
	qe->qe_block = dma_alloc_coherent(&op->dev, PAGE_SIZE,
886
					  &qe->qblock_dvma, GFP_ATOMIC);
887
	qe->buffers = dma_alloc_coherent(&op->dev, sizeof(struct sunqe_buffers),
888
					 &qe->buffers_dvma, GFP_ATOMIC);
889 890 891 892 893 894 895
	if (qe->qe_block == NULL || qe->qblock_dvma == 0 ||
	    qe->buffers == NULL || qe->buffers_dvma == 0)
		goto fail;

	/* Stop this QE. */
	qe_stop(qe);

896
	SET_NETDEV_DEV(dev, &op->dev);
897 898

	dev->watchdog_timeo = 5*HZ;
899
	dev->irq = op->archdata.irqs[0];
900 901
	dev->dma = 0;
	dev->ethtool_ops = &qe_ethtool_ops;
902
	dev->netdev_ops = &qec_ops;
903 904 905 906 907

	res = register_netdev(dev);
	if (res)
		goto fail;

908
	dev_set_drvdata(&op->dev, qe);
909 910 911 912 913 914 915

	printk(KERN_INFO "%s: qe channel[%d] ", dev->name, qe->channel);
	for (i = 0; i < 6; i++)
		printk ("%2.2x%c",
			dev->dev_addr[i],
			i == 5 ? ' ': ':');
	printk("\n");
L
Linus Torvalds 已提交
916 917 918 919


	return 0;

920 921
fail:
	if (qe->qcregs)
922
		of_iounmap(&op->resource[0], qe->qcregs, CREG_REG_SIZE);
923
	if (qe->mregs)
924
		of_iounmap(&op->resource[1], qe->mregs, MREGS_REG_SIZE);
925
	if (qe->qe_block)
926 927
		dma_free_coherent(&op->dev, PAGE_SIZE,
				  qe->qe_block, qe->qblock_dvma);
928
	if (qe->buffers)
929
		dma_free_coherent(&op->dev,
930 931 932
				  sizeof(struct sunqe_buffers),
				  qe->buffers,
				  qe->buffers_dvma);
933 934 935

	free_netdev(dev);

L
Linus Torvalds 已提交
936 937 938
	return res;
}

939
static int __devinit qec_sbus_probe(struct platform_device *op)
L
Linus Torvalds 已提交
940
{
941
	return qec_ether_init(op);
L
Linus Torvalds 已提交
942 943
}

944
static int __devexit qec_sbus_remove(struct platform_device *op)
L
Linus Torvalds 已提交
945
{
946
	struct sunqe *qp = dev_get_drvdata(&op->dev);
947 948
	struct net_device *net_dev = qp->dev;

949
	unregister_netdev(net_dev);
950

951 952 953 954 955 956
	of_iounmap(&op->resource[0], qp->qcregs, CREG_REG_SIZE);
	of_iounmap(&op->resource[1], qp->mregs, MREGS_REG_SIZE);
	dma_free_coherent(&op->dev, PAGE_SIZE,
			  qp->qe_block, qp->qblock_dvma);
	dma_free_coherent(&op->dev, sizeof(struct sunqe_buffers),
			  qp->buffers, qp->buffers_dvma);
957 958 959

	free_netdev(net_dev);

960
	dev_set_drvdata(&op->dev, NULL);
961

L
Linus Torvalds 已提交
962 963 964
	return 0;
}

965
static const struct of_device_id qec_sbus_match[] = {
966 967 968 969 970 971 972 973
	{
		.name = "qe",
	},
	{},
};

MODULE_DEVICE_TABLE(of, qec_sbus_match);

974
static struct platform_driver qec_sbus_driver = {
975 976 977 978 979
	.driver = {
		.name = "qec",
		.owner = THIS_MODULE,
		.of_match_table = qec_sbus_match,
	},
980 981 982 983 984 985
	.probe		= qec_sbus_probe,
	.remove		= __devexit_p(qec_sbus_remove),
};

static int __init qec_init(void)
{
986
	return platform_driver_register(&qec_sbus_driver);
987 988 989
}

static void __exit qec_exit(void)
L
Linus Torvalds 已提交
990
{
991
	platform_driver_unregister(&qec_sbus_driver);
L
Linus Torvalds 已提交
992 993

	while (root_qec_dev) {
994
		struct sunqec *next = root_qec_dev->next_module;
995
		struct platform_device *op = root_qec_dev->op;
996

997
		free_irq(op->archdata.irqs[0], (void *) root_qec_dev);
998 999
		of_iounmap(&op->resource[0], root_qec_dev->gregs,
			   GLOB_REG_SIZE);
L
Linus Torvalds 已提交
1000
		kfree(root_qec_dev);
1001 1002

		root_qec_dev = next;
L
Linus Torvalds 已提交
1003 1004 1005
	}
}

1006 1007
module_init(qec_init);
module_exit(qec_exit);