ni52.c 37.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 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
/*
 * net-3-driver for the NI5210 card (i82586 Ethernet chip)
 *
 * This is an extension to the Linux operating system, and is covered by the
 * same GNU General Public License that covers that work.
 *
 * Alphacode 0.82 (96/09/29) for Linux 2.0.0 (or later)
 * Copyrights (c) 1994,1995,1996 by M.Hipp (hippm@informatik.uni-tuebingen.de)
 *    [feel free to mail ....]
 *
 * when using as module: (no autoprobing!)
 *   compile with:
 *       gcc -O2 -fomit-frame-pointer -m486 -D__KERNEL__ -DMODULE -c ni52.c
 *   run with e.g:
 *       insmod ni52.o io=0x360 irq=9 memstart=0xd0000 memend=0xd4000
 *
 * CAN YOU PLEASE REPORT ME YOUR PERFORMANCE EXPERIENCES !!.
 *
 * If you find a bug, please report me:
 *   The kernel panic output and any kmsg from the ni52 driver
 *   the ni5210-driver-version and the linux-kernel version
 *   how many shared memory (memsize) on the netcard,
 *   bootprom: yes/no, base_addr, mem_start
 *   maybe the ni5210-card revision and the i82586 version
 *
 * autoprobe for: base_addr: 0x300,0x280,0x360,0x320,0x340
 *                mem_start: 0xd0000,0xd2000,0xc8000,0xca000,0xd4000,0xd6000,
 *                           0xd8000,0xcc000,0xce000,0xda000,0xdc000
 *
 * sources:
 *   skeleton.c from Donald Becker
 *
 * I have also done a look in the following sources: (mail me if you need them)
 *   crynwr-packet-driver by Russ Nelson
 *   Garret A. Wollman's (fourth) i82586-driver for BSD
36 37
 *   (before getting an i82596 (yes 596 not 586) manual, the existing drivers
 *    helped me a lot to understand this tricky chip.)
L
Linus Torvalds 已提交
38 39 40 41
 *
 * Known Problems:
 *   The internal sysbus seems to be slow. So we often lose packets because of
 *   overruns while receiving from a fast remote host.
42 43 44
 *   This can slow down TCP connections. Maybe the newer ni5210 cards are
 *   better. My experience is, that if a machine sends with more than about
 *   500-600K/s the fifo/sysbus overflows.
L
Linus Torvalds 已提交
45 46 47
 *
 * IMPORTANT NOTE:
 *   On fast networks, it's a (very) good idea to have 16K shared memory. With
48 49
 *   8K, we can store only 4 receive frames, so it can (easily) happen that a
 *   remote machine 'overruns' our system.
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
 *
 * Known i82586/card problems (I'm sure, there are many more!):
 *   Running the NOP-mode, the i82586 sometimes seems to forget to report
 *   every xmit-interrupt until we restart the CU.
 *   Another MAJOR bug is, that the RU sometimes seems to ignore the EL-Bit
 *   in the RBD-Struct which indicates an end of the RBD queue.
 *   Instead, the RU fetches another (randomly selected and
 *   usually used) RBD and begins to fill it. (Maybe, this happens only if
 *   the last buffer from the previous RFD fits exact into the queue and
 *   the next RFD can't fetch an initial RBD. Anyone knows more? )
 *
 * results from ftp performance tests with Linux 1.2.5
 *   send and receive about 350-400 KByte/s (peak up to 460 kbytes/s)
63 64
 *   sending in NOP-mode: peak performance up to 530K/s (but better don't
 *   run this mode)
L
Linus Torvalds 已提交
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
 */

/*
 * 29.Sept.96: virt_to_bus changes for new memory scheme
 * 19.Feb.96: more Mcast changes, module support (MH)
 *
 * 18.Nov.95: Mcast changes (AC).
 *
 * 23.April.95: fixed(?) receiving problems by configuring a RFD more
 *              than the number of RBD's. Can maybe cause other problems.
 * 18.April.95: Added MODULE support (MH)
 * 17.April.95: MC related changes in init586() and set_multicast_list().
 *              removed use of 'jiffies' in init586() (MH)
 *
 * 19.Sep.94: Added Multicast support (not tested yet) (MH)
 *
 * 18.Sep.94: Workaround for 'EL-Bug'. Removed flexible RBD-handling.
 *            Now, every RFD has exact one RBD. (MH)
 *
 * 14.Sep.94: added promiscuous mode, a few cleanups (MH)
 *
 * 19.Aug.94: changed request_irq() parameter (MH)
 *
 * 20.July.94: removed cleanup bugs, removed a 16K-mem-probe-bug (MH)
 *
 * 19.July.94: lotsa cleanups .. (MH)
 *
 * 17.July.94: some patches ... verified to run with 1.1.29 (MH)
 *
 * 4.July.94: patches for Linux 1.1.24  (MH)
 *
 * 26.March.94: patches for Linux 1.0 and iomem-auto-probe (MH)
 *
98 99
 * 30.Sep.93: Added nop-chain .. driver now runs with only one Xmit-Buff,
 *				too (MH)
L
Linus Torvalds 已提交
100 101 102 103 104 105 106
 *
 * < 30.Sep.93: first versions
 */

static int debuglevel;	/* debug-printk 0: off 1: a few 2: more */
static int automatic_resume; /* experimental .. better should be zero */
static int rfdadd;	/* rfdadd=1 may be better for 8K MEM cards */
107
static int fifo = 0x8;	/* don't change */
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <asm/io.h>

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>

#include "ni52.h"

#define DRV_NAME "ni52"

#define DEBUG       /* debug on */
#define SYSBUSVAL 1 /* 8 Bit */

132 133 134 135
#define ni_attn586()  { outb(0, dev->base_addr + NI52_ATTENTION); }
#define ni_reset586() { outb(0, dev->base_addr + NI52_RESET); }
#define ni_disint()   { outb(0, dev->base_addr + NI52_INTDIS); }
#define ni_enaint()   { outb(0, dev->base_addr + NI52_INTENA); }
L
Linus Torvalds 已提交
136

137 138 139 140
#define make32(ptr16) (p->memtop + (short) (ptr16))
#define make24(ptr32) ((unsigned long)(ptr32)) - p->base
#define make16(ptr32) ((unsigned short) ((unsigned long)(ptr32)\
					- (unsigned long) p->memtop))
L
Linus Torvalds 已提交
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

/******************* how to calculate the buffers *****************************

  * IMPORTANT NOTE: if you configure only one NUM_XMIT_BUFFS, the driver works
  * --------------- in a different (more stable?) mode. Only in this mode it's
  *                 possible to configure the driver with 'NO_NOPCOMMANDS'

sizeof(scp)=12; sizeof(scb)=16; sizeof(iscp)=8;
sizeof(scp)+sizeof(iscp)+sizeof(scb) = 36 = INIT
sizeof(rfd) = 24; sizeof(rbd) = 12;
sizeof(tbd) = 8; sizeof(transmit_cmd) = 16;
sizeof(nop_cmd) = 8;

  * if you don't know the driver, better do not change these values: */

#define RECV_BUFF_SIZE 1524 /* slightly oversized */
#define XMIT_BUFF_SIZE 1524 /* slightly oversized */
#define NUM_XMIT_BUFFS 1    /* config for both, 8K and 16K shmem */
#define NUM_RECV_BUFFS_8  4 /* config for 8K shared mem */
#define NUM_RECV_BUFFS_16 9 /* config for 16K shared mem */
#define NO_NOPCOMMANDS      /* only possible with NUM_XMIT_BUFFS=1 */

/**************************************************************************/


#define NI52_TOTAL_SIZE 16
#define NI52_ADDR0 0x02
#define NI52_ADDR1 0x07
#define NI52_ADDR2 0x01

171 172
static int     ni52_probe1(struct net_device *dev, int ioaddr);
static irqreturn_t ni52_interrupt(int irq, void *dev_id);
L
Linus Torvalds 已提交
173 174
static int     ni52_open(struct net_device *dev);
static int     ni52_close(struct net_device *dev);
175
static int     ni52_send_packet(struct sk_buff *, struct net_device *);
L
Linus Torvalds 已提交
176 177 178 179 180 181
static struct  net_device_stats *ni52_get_stats(struct net_device *dev);
static void    set_multicast_list(struct net_device *dev);
static void    ni52_timeout(struct net_device *dev);

/* helper-functions */
static int     init586(struct net_device *dev);
182
static int     check586(struct net_device *dev, unsigned size);
L
Linus Torvalds 已提交
183 184
static void    alloc586(struct net_device *dev);
static void    startrecv586(struct net_device *dev);
A
Al Viro 已提交
185
static void   __iomem *alloc_rfa(struct net_device *dev, void __iomem *ptr);
L
Linus Torvalds 已提交
186 187 188 189
static void    ni52_rcv_int(struct net_device *dev);
static void    ni52_xmt_int(struct net_device *dev);
static void    ni52_rnr_int(struct net_device *dev);

190
struct priv {
L
Linus Torvalds 已提交
191 192
	struct net_device_stats stats;
	unsigned long base;
A
Al Viro 已提交
193
	char __iomem *memtop;
194 195
	spinlock_t spinlock;
	int reset;
A
Al Viro 已提交
196 197 198 199 200
	struct rfd_struct __iomem *rfd_last, *rfd_top, *rfd_first;
	struct scp_struct __iomem *scp;
	struct iscp_struct __iomem *iscp;
	struct scb_struct __iomem *scb;
	struct tbd_struct __iomem *xmit_buffs[NUM_XMIT_BUFFS];
L
Linus Torvalds 已提交
201
#if (NUM_XMIT_BUFFS == 1)
A
Al Viro 已提交
202 203
	struct transmit_cmd_struct __iomem *xmit_cmds[2];
	struct nop_cmd_struct __iomem *nop_cmds[2];
L
Linus Torvalds 已提交
204
#else
A
Al Viro 已提交
205 206
	struct transmit_cmd_struct __iomem *xmit_cmds[NUM_XMIT_BUFFS];
	struct nop_cmd_struct __iomem *nop_cmds[NUM_XMIT_BUFFS];
L
Linus Torvalds 已提交
207
#endif
208
	int nop_point, num_recv_buffs;
A
Al Viro 已提交
209
	char __iomem *xmit_cbuffs[NUM_XMIT_BUFFS];
210
	int xmit_count, xmit_last;
L
Linus Torvalds 已提交
211 212
};

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
/* wait for command with timeout: */
static void wait_for_scb_cmd(struct net_device *dev)
{
	struct priv *p = dev->priv;
	int i;
	for (i = 0; i < 16384; i++) {
		if (readb(&p->scb->cmd_cuc) == 0)
		      break;
		udelay(4);
		if (i == 16383) {
			printk(KERN_ERR "%s: scb_cmd timed out: %04x,%04x .. disabling i82586!!\n",
				dev->name, readb(&p->scb->cmd_cuc), readb(&p->scb->cus));
			if (!p->reset) {
				p->reset = 1;
				ni_reset586();
			}
		}
	}
}

static void wait_for_scb_cmd_ruc(struct net_device *dev)
{
	struct priv *p = dev->priv;
	int i;
	for (i = 0; i < 16384; i++) {
		if (readb(&p->scb->cmd_ruc) == 0)
			break;
		udelay(4);
		if (i == 16383) {
			printk(KERN_ERR "%s: scb_cmd (ruc) timed out: %04x,%04x .. disabling i82586!!\n",
				dev->name, p->scb->cmd_ruc, p->scb->rus);
			if (!p->reset) {
				p->reset = 1;
				ni_reset586();
			}
		}
	}
}

A
Al Viro 已提交
252
static void wait_for_stat_compl(void __iomem *p)
253
{
A
Al Viro 已提交
254
	struct nop_cmd_struct __iomem *addr = p;
255 256 257 258 259 260 261 262
	int i;
	for (i = 0; i < 32767; i++) {
		if (readw(&((addr)->cmd_status)) & STAT_COMPL)
			break;
		udelay(32);
	}
}

L
Linus Torvalds 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
/**********************************************
 * close device
 */
static int ni52_close(struct net_device *dev)
{
	free_irq(dev->irq, dev);
	ni_reset586(); /* the hard way to stop the receiver */
	netif_stop_queue(dev);
	return 0;
}

/**********************************************
 * open device
 */
static int ni52_open(struct net_device *dev)
{
	int ret;

	ni_disint();
	alloc586(dev);
	init586(dev);
	startrecv586(dev);
	ni_enaint();

287 288
	ret = request_irq(dev->irq, &ni52_interrupt, 0, dev->name, dev);
	if (ret) {
L
Linus Torvalds 已提交
289 290 291 292 293 294 295 296 297 298
		ni_reset586();
		return ret;
	}
	netif_start_queue(dev);
	return 0; /* most done by init */
}

/**********************************************
 * Check to see if there's an 82586 out there.
 */
299
static int check586(struct net_device *dev, unsigned size)
L
Linus Torvalds 已提交
300
{
301
	unsigned long where = dev->mem_start;
L
Linus Torvalds 已提交
302 303
	struct priv pb;
	struct priv *p = /* (struct priv *) dev->priv*/ &pb;
A
Al Viro 已提交
304
	char __iomem *iscp_addrs[2];
L
Linus Torvalds 已提交
305 306
	int i;

307 308
	p->base = (unsigned long) isa_bus_to_virt(where) + size - 0x01000000;
	p->memtop = (char __iomem *)isa_bus_to_virt(where) + size;
A
Al Viro 已提交
309 310
	p->scp = (struct scp_struct __iomem *)(p->base + SCP_DEFAULT_ADDRESS);
	memset_io(p->scp, 0, sizeof(struct scp_struct));
311 312
	for (i = 0; i < sizeof(struct scp_struct); i++)
		/* memory was writeable? */
A
Al Viro 已提交
313
		if (readb((char __iomem *)p->scp + i))
L
Linus Torvalds 已提交
314
			return 0;
315 316
	writeb(SYSBUSVAL, &p->scp->sysbus);	/* 1 = 8Bit-Bus, 0 = 16 Bit */
	if (readb(&p->scp->sysbus) != SYSBUSVAL)
L
Linus Torvalds 已提交
317 318
		return 0;

319
	iscp_addrs[0] = (char __iomem *)isa_bus_to_virt(where);
A
Al Viro 已提交
320
	iscp_addrs[1] = (char __iomem *)p->scp - sizeof(struct iscp_struct);
L
Linus Torvalds 已提交
321

322
	for (i = 0; i < 2; i++) {
A
Al Viro 已提交
323 324
		p->iscp = (struct iscp_struct __iomem *) iscp_addrs[i];
		memset_io(p->iscp, 0, sizeof(struct iscp_struct));
L
Linus Torvalds 已提交
325

326 327
		writel(make24(p->iscp), &p->scp->iscp);
		writeb(1, &p->iscp->busy);
L
Linus Torvalds 已提交
328 329 330

		ni_reset586();
		ni_attn586();
331 332 333
		mdelay(32);	/* wait a while... */
		/* i82586 clears 'busy' after successful init */
		if (readb(&p->iscp->busy))
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346
			return 0;
	}
	return 1;
}

/******************************************************************
 * set iscp at the right place, called by ni52_probe1 and open586.
 */
static void alloc586(struct net_device *dev)
{
	struct priv *p =	(struct priv *) dev->priv;

	ni_reset586();
347 348 349
	mdelay(32);

	spin_lock_init(&p->spinlock);
L
Linus Torvalds 已提交
350

A
Al Viro 已提交
351 352 353 354
	p->scp	= (struct scp_struct __iomem *)	(p->base + SCP_DEFAULT_ADDRESS);
	p->scb	= (struct scb_struct __iomem *)	isa_bus_to_virt(dev->mem_start);
	p->iscp = (struct iscp_struct __iomem *)
			((char __iomem *)p->scp - sizeof(struct iscp_struct));
L
Linus Torvalds 已提交
355

356 357
	memset_io(p->iscp, 0, sizeof(struct iscp_struct));
	memset_io(p->scp , 0, sizeof(struct scp_struct));
L
Linus Torvalds 已提交
358

359 360 361
	writel(make24(p->iscp), &p->scp->iscp);
	writeb(SYSBUSVAL, &p->scp->sysbus);
	writew(make16(p->scb), &p->iscp->scb_offset);
L
Linus Torvalds 已提交
362

363
	writeb(1, &p->iscp->busy);
L
Linus Torvalds 已提交
364 365 366
	ni_reset586();
	ni_attn586();

367
	mdelay(32);
L
Linus Torvalds 已提交
368

369 370
	if (readb(&p->iscp->busy))
		printk(KERN_ERR "%s: Init-Problems (alloc).\n", dev->name);
L
Linus Torvalds 已提交
371

372
	p->reset = 0;
L
Linus Torvalds 已提交
373

A
Al Viro 已提交
374
	memset_io(p->scb, 0, sizeof(struct scb_struct));
L
Linus Torvalds 已提交
375 376 377
}

/* set: io,irq,memstart,memend or set it when calling insmod */
378 379
static int irq = 9;
static int io = 0x300;
L
Linus Torvalds 已提交
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
static long memstart;	/* e.g 0xd0000 */
static long memend;	/* e.g 0xd4000 */

/**********************************************
 * probe the ni5210-card
 */
struct net_device * __init ni52_probe(int unit)
{
	struct net_device *dev = alloc_etherdev(sizeof(struct priv));
	static int ports[] = {0x300, 0x280, 0x360 , 0x320 , 0x340, 0};
	int *port;
	int err = 0;

	if (!dev)
		return ERR_PTR(-ENOMEM);

	if (unit >= 0) {
		sprintf(dev->name, "eth%d", unit);
		netdev_boot_setup_check(dev);
		io = dev->base_addr;
		irq = dev->irq;
		memstart = dev->mem_start;
		memend = dev->mem_end;
	}

	if (io > 0x1ff)	{	/* Check a single specified location. */
		err = ni52_probe1(dev, io);
	} else if (io > 0) {		/* Don't probe at all. */
		err = -ENXIO;
	} else {
		for (port = ports; *port && ni52_probe1(dev, *port) ; port++)
			;
		if (*port)
			goto got_it;
#ifdef FULL_IO_PROBE
		for (io = 0x200; io < 0x400 && ni52_probe1(dev, io); io += 8)
			;
		if (io < 0x400)
			goto got_it;
#endif
		err = -ENODEV;
	}
	if (err)
		goto out;
got_it:
	err = register_netdev(dev);
	if (err)
		goto out1;
	return dev;
out1:
	release_region(dev->base_addr, NI52_TOTAL_SIZE);
out:
	free_netdev(dev);
	return ERR_PTR(err);
}

436
static int __init ni52_probe1(struct net_device *dev, int ioaddr)
L
Linus Torvalds 已提交
437 438 439 440 441 442 443 444 445 446 447
{
	int i, size, retval;

	dev->base_addr = ioaddr;
	dev->irq = irq;
	dev->mem_start = memstart;
	dev->mem_end = memend;

	if (!request_region(ioaddr, NI52_TOTAL_SIZE, DRV_NAME))
		return -EBUSY;

448
	if (!(inb(ioaddr+NI52_MAGIC1) == NI52_MAGICVAL1) ||
L
Linus Torvalds 已提交
449 450 451 452 453
	    !(inb(ioaddr+NI52_MAGIC2) == NI52_MAGICVAL2)) {
		retval = -ENODEV;
		goto out;
	}

454
	for (i = 0; i < ETH_ALEN; i++)
L
Linus Torvalds 已提交
455 456
		dev->dev_addr[i] = inb(dev->base_addr+i);

457
	if (dev->dev_addr[0] != NI52_ADDR0 || dev->dev_addr[1] != NI52_ADDR1
L
Linus Torvalds 已提交
458 459 460 461 462
		 || dev->dev_addr[2] != NI52_ADDR2) {
		retval = -ENODEV;
		goto out;
	}

463 464
	printk(KERN_INFO "%s: NI5210 found at %#3lx, ",
				dev->name, dev->base_addr);
L
Linus Torvalds 已提交
465 466 467 468 469 470

	/*
	 * check (or search) IO-Memory, 8K and 16K
	 */
#ifdef MODULE
	size = dev->mem_end - dev->mem_start;
471 472 473
	if (size != 0x2000 && size != 0x4000) {
		printk("\n");
		printk(KERN_ERR "%s: Invalid memory size %d. Allowed is 0x2000 or 0x4000 bytes.\n", dev->name, size);
L
Linus Torvalds 已提交
474 475 476
		retval = -ENODEV;
		goto out;
	}
477
	if (!check586(dev, size)) {
478
		printk(KERN_ERR "?memcheck, Can't find memory at 0x%lx with size %d!\n", dev->mem_start, size);
L
Linus Torvalds 已提交
479 480 481 482
		retval = -ENODEV;
		goto out;
	}
#else
483 484
	if (dev->mem_start != 0) {
		/* no auto-mem-probe */
L
Linus Torvalds 已提交
485
		size = 0x4000; /* check for 16K mem */
486
		if (!check586(dev, size)) {
L
Linus Torvalds 已提交
487
			size = 0x2000; /* check for 8K mem */
488
			if (!check586(dev, size)) {
489
				printk(KERN_ERR "?memprobe, Can't find memory at 0x%lx!\n", dev->mem_start);
L
Linus Torvalds 已提交
490 491 492 493
				retval = -ENODEV;
				goto out;
			}
		}
494 495 496 497 498 499 500 501
	} else {
		static const unsigned long memaddrs[] = {
			0xc8000, 0xca000, 0xcc000, 0xce000, 0xd0000, 0xd2000,
			0xd4000, 0xd6000, 0xd8000, 0xda000, 0xdc000, 0
		};
		for (i = 0;; i++) {
			if (!memaddrs[i]) {
				printk(KERN_ERR "?memprobe, Can't find io-memory!\n");
L
Linus Torvalds 已提交
502 503 504 505 506
				retval = -ENODEV;
				goto out;
			}
			dev->mem_start = memaddrs[i];
			size = 0x2000; /* check for 8K mem */
507
			if (check586(dev, size))
508
				/* 8K-check */
L
Linus Torvalds 已提交
509 510
				break;
			size = 0x4000; /* check for 16K mem */
511
			if (check586(dev, size))
512
				/* 16K-check */
L
Linus Torvalds 已提交
513 514 515
				break;
		}
	}
516 517
	/* set mem_end showed by 'ifconfig' */
	dev->mem_end = dev->mem_start + size;
L
Linus Torvalds 已提交
518 519
#endif

520
	memset((char *)dev->priv, 0, sizeof(struct priv));
L
Linus Torvalds 已提交
521

522
	((struct priv *)(dev->priv))->memtop =
A
Al Viro 已提交
523
				(char __iomem *)isa_bus_to_virt(dev->mem_start) + size;
524 525
	((struct priv *)(dev->priv))->base =  (unsigned long)
			isa_bus_to_virt(dev->mem_start) + size - 0x01000000;
L
Linus Torvalds 已提交
526 527 528
	alloc586(dev);

	/* set number of receive-buffs according to memsize */
529
	if (size == 0x2000)
L
Linus Torvalds 已提交
530 531 532 533
		((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_8;
	else
		((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_16;

534 535
	printk(KERN_DEBUG "Memaddr: 0x%lx, Memsize: %d, ",
				dev->mem_start, size);
L
Linus Torvalds 已提交
536

537
	if (dev->irq < 2) {
L
Linus Torvalds 已提交
538 539 540 541 542 543 544 545
		unsigned long irq_mask;

		irq_mask = probe_irq_on();
		ni_reset586();
		ni_attn586();

		mdelay(20);
		dev->irq = probe_irq_off(irq_mask);
546
		if (!dev->irq) {
L
Linus Torvalds 已提交
547 548 549 550
			printk("?autoirq, Failed to detect IRQ line!\n");
			retval = -EAGAIN;
			goto out;
		}
551 552 553
		printk("IRQ %d (autodetected).\n", dev->irq);
	} else {
		if (dev->irq == 2)
L
Linus Torvalds 已提交
554
			dev->irq = 9;
555
		printk("IRQ %d (assigned and not checked!).\n", dev->irq);
L
Linus Torvalds 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	}

	dev->open		= ni52_open;
	dev->stop		= ni52_close;
	dev->get_stats		= ni52_get_stats;
	dev->tx_timeout 	= ni52_timeout;
	dev->watchdog_timeo	= HZ/20;
	dev->hard_start_xmit 	= ni52_send_packet;
	dev->set_multicast_list = set_multicast_list;

	dev->if_port 		= 0;

	return 0;
out:
	release_region(ioaddr, NI52_TOTAL_SIZE);
	return retval;
}

/**********************************************
 * init the chip (ni52-interrupt should be disabled?!)
 * needs a correct 'allocated' memory
 */

static int init586(struct net_device *dev)
{
A
Al Viro 已提交
581
	void __iomem *ptr;
582 583
	int i, result = 0;
	struct priv *p = (struct priv *)dev->priv;
A
Al Viro 已提交
584 585 586 587
	struct configure_cmd_struct __iomem *cfg_cmd;
	struct iasetup_cmd_struct __iomem *ias_cmd;
	struct tdr_cmd_struct __iomem *tdr_cmd;
	struct mcsetup_cmd_struct __iomem *mc_cmd;
588 589
	struct dev_mc_list *dmi = dev->mc_list;
	int num_addrs = dev->mc_count;
L
Linus Torvalds 已提交
590

A
Al Viro 已提交
591
	ptr = p->scb + 1;
L
Linus Torvalds 已提交
592

A
Al Viro 已提交
593
	cfg_cmd = ptr; /* configure-command */
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	writew(0, &cfg_cmd->cmd_status);
	writew(CMD_CONFIGURE | CMD_LAST, &cfg_cmd->cmd_cmd);
	writew(0xFFFF, &cfg_cmd->cmd_link);

	/* number of cfg bytes */
	writeb(0x0a, &cfg_cmd->byte_cnt);
	/* fifo-limit (8=tx:32/rx:64) */
	writeb(fifo, &cfg_cmd->fifo);
	/* hold or discard bad recv frames (bit 7) */
	writeb(0x40, &cfg_cmd->sav_bf);
	/* addr_len |!src_insert |pre-len |loopback */
	writeb(0x2e, &cfg_cmd->adr_len);
	writeb(0x00, &cfg_cmd->priority);
	writeb(0x60, &cfg_cmd->ifs);;
	writeb(0x00, &cfg_cmd->time_low);
	writeb(0xf2, &cfg_cmd->time_high);
	writeb(0x00, &cfg_cmd->promisc);;
	if (dev->flags & IFF_ALLMULTI) {
A
Al Viro 已提交
612
		int len = ((char __iomem *)p->iscp - (char __iomem *)ptr - 8) / 6;
613 614 615 616
		if (num_addrs > len) {
			printk(KERN_ERR "%s: switching to promisc. mode\n",
				dev->name);
			dev->flags |= IFF_PROMISC;
L
Linus Torvalds 已提交
617 618
		}
	}
619 620 621 622 623
	if (dev->flags & IFF_PROMISC)
		writeb(0x01, &cfg_cmd->promisc);
	writeb(0x00, &cfg_cmd->carr_coll);
	writew(make16(cfg_cmd), &p->scb->cbl_offset);
	writew(0, &p->scb->cmd_ruc);
L
Linus Torvalds 已提交
624

625
	writeb(CUC_START, &p->scb->cmd_cuc); /* cmd.-unit start */
L
Linus Torvalds 已提交
626 627
	ni_attn586();

628
	wait_for_stat_compl(cfg_cmd);
L
Linus Torvalds 已提交
629

630 631 632 633
	if ((readw(&cfg_cmd->cmd_status) & (STAT_OK|STAT_COMPL)) !=
							(STAT_COMPL|STAT_OK)) {
		printk(KERN_ERR "%s: configure command failed: %x\n",
				dev->name, readw(&cfg_cmd->cmd_status));
L
Linus Torvalds 已提交
634 635 636 637 638 639 640
		return 1;
	}

	/*
	 * individual address setup
	 */

A
Al Viro 已提交
641
	ias_cmd = ptr;
L
Linus Torvalds 已提交
642

643 644 645
	writew(0, &ias_cmd->cmd_status);
	writew(CMD_IASETUP | CMD_LAST, &ias_cmd->cmd_cmd);
	writew(0xffff, &ias_cmd->cmd_link);
L
Linus Torvalds 已提交
646

A
Al Viro 已提交
647
	memcpy_toio(&ias_cmd->iaddr, (char *)dev->dev_addr, ETH_ALEN);
L
Linus Torvalds 已提交
648

649
	writew(make16(ias_cmd), &p->scb->cbl_offset);
L
Linus Torvalds 已提交
650

651
	writeb(CUC_START, &p->scb->cmd_cuc); /* cmd.-unit start */
L
Linus Torvalds 已提交
652 653
	ni_attn586();

654
	wait_for_stat_compl(ias_cmd);
L
Linus Torvalds 已提交
655

656 657 658
	if ((readw(&ias_cmd->cmd_status) & (STAT_OK|STAT_COMPL)) !=
							(STAT_OK|STAT_COMPL)) {
		printk(KERN_ERR "%s (ni52): individual address setup command failed: %04x\n", dev->name, readw(&ias_cmd->cmd_status));
L
Linus Torvalds 已提交
659 660 661 662 663 664
		return 1;
	}

	/*
	 * TDR, wire check .. e.g. no resistor e.t.c
	 */
665

A
Al Viro 已提交
666
	tdr_cmd = ptr;
L
Linus Torvalds 已提交
667

668 669 670 671
	writew(0, &tdr_cmd->cmd_status);
	writew(CMD_TDR | CMD_LAST, &tdr_cmd->cmd_cmd);
	writew(0xffff, &tdr_cmd->cmd_link);
	writew(0, &tdr_cmd->status);
L
Linus Torvalds 已提交
672

673 674
	writew(make16(tdr_cmd), &p->scb->cbl_offset);
	writeb(CUC_START, &p->scb->cmd_cuc); /* cmd.-unit start */
L
Linus Torvalds 已提交
675 676
	ni_attn586();

677
	wait_for_stat_compl(tdr_cmd);
L
Linus Torvalds 已提交
678

679 680 681 682 683 684 685
	if (!(readw(&tdr_cmd->cmd_status) & STAT_COMPL))
		printk(KERN_ERR "%s: Problems while running the TDR.\n",
				dev->name);
	else {
		udelay(16);
		result = readw(&tdr_cmd->status);
		writeb(readb(&p->scb->cus) & STAT_MASK, &p->scb->cmd_cuc);
L
Linus Torvalds 已提交
686 687
		ni_attn586(); /* ack the interrupts */

688
		if (result & TDR_LNK_OK)
L
Linus Torvalds 已提交
689
			;
690 691 692 693 694 695 696 697 698 699 700 701 702 703
		else if (result & TDR_XCVR_PRB)
			printk(KERN_ERR "%s: TDR: Transceiver problem. Check the cable(s)!\n",
				dev->name);
		else if (result & TDR_ET_OPN)
			printk(KERN_ERR "%s: TDR: No correct termination %d clocks away.\n",
				dev->name, result & TDR_TIMEMASK);
		else if (result & TDR_ET_SRT) {
			/* time == 0 -> strange :-) */
			if (result & TDR_TIMEMASK)
				printk(KERN_ERR "%s: TDR: Detected a short circuit %d clocks away.\n",
					dev->name, result & TDR_TIMEMASK);
		} else
			printk(KERN_ERR "%s: TDR: Unknown status %04x\n",
						dev->name, result);
L
Linus Torvalds 已提交
704 705 706 707 708
	}

	/*
	 * Multicast setup
	 */
709
	if (num_addrs && !(dev->flags & IFF_PROMISC)) {
A
Al Viro 已提交
710
		mc_cmd = ptr;
711 712 713 714
		writew(0, &mc_cmd->cmd_status);
		writew(CMD_MCSETUP | CMD_LAST, &mc_cmd->cmd_cmd);
		writew(0xffff, &mc_cmd->cmd_link);
		writew(num_addrs * 6, &mc_cmd->mc_cnt);
L
Linus Torvalds 已提交
715

716
		for (i = 0; i < num_addrs; i++, dmi = dmi->next)
A
Al Viro 已提交
717
			memcpy_toio(mc_cmd->mc_list[i],
718
							dmi->dmi_addr, 6);
L
Linus Torvalds 已提交
719

720 721
		writew(make16(mc_cmd), &p->scb->cbl_offset);
		writeb(CUC_START, &p->scb->cmd_cuc);
L
Linus Torvalds 已提交
722 723
		ni_attn586();

724
		wait_for_stat_compl(mc_cmd);
L
Linus Torvalds 已提交
725

726 727 728
		if ((readw(&mc_cmd->cmd_status) & (STAT_COMPL|STAT_OK))
						 != (STAT_COMPL|STAT_OK))
			printk(KERN_ERR "%s: Can't apply multicast-address-list.\n", dev->name);
L
Linus Torvalds 已提交
729 730 731 732 733 734
	}

	/*
	 * alloc nop/xmit-cmds
	 */
#if (NUM_XMIT_BUFFS == 1)
735
	for (i = 0; i < 2; i++) {
A
Al Viro 已提交
736
		p->nop_cmds[i] = ptr;
737 738 739
		writew(CMD_NOP, &p->nop_cmds[i]->cmd_cmd);
		writew(0, &p->nop_cmds[i]->cmd_status);
		writew(make16(p->nop_cmds[i]), &p->nop_cmds[i]->cmd_link);
A
Al Viro 已提交
740
		ptr = ptr + sizeof(struct nop_cmd_struct);
L
Linus Torvalds 已提交
741 742
	}
#else
743
	for (i = 0; i < NUM_XMIT_BUFFS; i++) {
A
Al Viro 已提交
744
		p->nop_cmds[i] = ptr;
745 746 747
		writew(CMD_NOP, &p->nop_cmds[i]->cmd_cmd);
		writew(0, &p->nop_cmds[i]->cmd_status);
		writew(make16(p->nop_cmds[i]), &p->nop_cmds[i]->cmd_link);
A
Al Viro 已提交
748
		ptr = ptr + sizeof(struct nop_cmd_struct);
L
Linus Torvalds 已提交
749 750 751
	}
#endif

A
Al Viro 已提交
752
	ptr = alloc_rfa(dev, ptr); /* init receive-frame-area */
L
Linus Torvalds 已提交
753 754 755 756

	/*
	 * alloc xmit-buffs / init xmit_cmds
	 */
757 758
	for (i = 0; i < NUM_XMIT_BUFFS; i++) {
		/* Transmit cmd/buff 0 */
A
Al Viro 已提交
759 760 761 762 763 764 765
		p->xmit_cmds[i] = ptr;
		ptr = ptr + sizeof(struct transmit_cmd_struct);
		p->xmit_cbuffs[i] = ptr; /* char-buffs */
		ptr = ptr + XMIT_BUFF_SIZE;
		p->xmit_buffs[i] = ptr; /* TBD */
		ptr = ptr + sizeof(struct tbd_struct);
		if ((void __iomem *)ptr > (void __iomem *)p->iscp) {
766 767
			printk(KERN_ERR "%s: not enough shared-mem for your configuration!\n",
				dev->name);
L
Linus Torvalds 已提交
768 769
			return 1;
		}
A
Al Viro 已提交
770
		memset_io(p->xmit_cmds[i], 0,
771
					sizeof(struct transmit_cmd_struct));
A
Al Viro 已提交
772
		memset_io(p->xmit_buffs[i], 0,
773 774 775 776 777 778 779 780
					sizeof(struct tbd_struct));
		writew(make16(p->nop_cmds[(i+1)%NUM_XMIT_BUFFS]),
					&p->xmit_cmds[i]->cmd_link);
		writew(STAT_COMPL, &p->xmit_cmds[i]->cmd_status);
		writew(CMD_XMIT|CMD_INT, &p->xmit_cmds[i]->cmd_cmd);
		writew(make16(p->xmit_buffs[i]), &p->xmit_cmds[i]->tbd_offset);
		writew(0xffff, &p->xmit_buffs[i]->next);
		writel(make24(p->xmit_cbuffs[i]), &p->xmit_buffs[i]->buffer);
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788 789 790 791 792
	}

	p->xmit_count = 0;
	p->xmit_last	= 0;
#ifndef NO_NOPCOMMANDS
	p->nop_point	= 0;
#endif

	 /*
		* 'start transmitter'
		*/
#ifndef NO_NOPCOMMANDS
793 794
	writew(make16(p->nop_cmds[0]), &p->scb->cbl_offset);
	writeb(CUC_START, &p->scb->cmd_cuc);
L
Linus Torvalds 已提交
795
	ni_attn586();
796
	wait_for_scb_cmd(dev);
L
Linus Torvalds 已提交
797
#else
798 799
	writew(make16(p->xmit_cmds[0]), &p->xmit_cmds[0]->cmd_link);
	writew(CMD_XMIT | CMD_SUSPEND | CMD_INT, &p->xmit_cmds[0]->cmd_cmd);
L
Linus Torvalds 已提交
800 801 802 803 804
#endif

	/*
	 * ack. interrupts
	 */
805
	writeb(readb(&p->scb->cus) & STAT_MASK, &p->scb->cmd_cuc);
L
Linus Torvalds 已提交
806
	ni_attn586();
807
	udelay(16);
L
Linus Torvalds 已提交
808 809 810 811 812 813 814 815 816 817 818

	ni_enaint();

	return 0;
}

/******************************************************
 * This is a helper routine for ni52_rnr_int() and init586().
 * It sets up the Receive Frame Area (RFA).
 */

A
Al Viro 已提交
819
static void __iomem *alloc_rfa(struct net_device *dev, void __iomem *ptr)
L
Linus Torvalds 已提交
820
{
A
Al Viro 已提交
821 822
	struct rfd_struct __iomem *rfd = ptr;
	struct rbd_struct __iomem *rbd;
L
Linus Torvalds 已提交
823 824 825
	int i;
	struct priv *p = (struct priv *) dev->priv;

A
Al Viro 已提交
826
	memset_io(rfd, 0,
827
		sizeof(struct rfd_struct) * (p->num_recv_buffs + rfdadd));
L
Linus Torvalds 已提交
828 829
	p->rfd_first = rfd;

830 831 832 833
	for (i = 0; i < (p->num_recv_buffs + rfdadd); i++) {
		writew(make16(rfd + (i+1) % (p->num_recv_buffs+rfdadd)),
			&rfd[i].next);
		writew(0xffff, &rfd[i].rbd_offset);
L
Linus Torvalds 已提交
834
	}
835 836
	/* RU suspend */
	writeb(RFD_SUSP, &rfd[p->num_recv_buffs-1+rfdadd].last);
L
Linus Torvalds 已提交
837

A
Al Viro 已提交
838
	ptr = rfd + (p->num_recv_buffs + rfdadd);
L
Linus Torvalds 已提交
839

A
Al Viro 已提交
840 841
	rbd = ptr;
	ptr = rbd + p->num_recv_buffs;
L
Linus Torvalds 已提交
842 843

	 /* clr descriptors */
A
Al Viro 已提交
844
	memset_io(rbd, 0, sizeof(struct rbd_struct) * (p->num_recv_buffs));
L
Linus Torvalds 已提交
845

846 847 848 849
	for (i = 0; i < p->num_recv_buffs; i++) {
		writew(make16(rbd + (i+1) % p->num_recv_buffs), &rbd[i].next);
		writew(RECV_BUFF_SIZE, &rbd[i].size);
		writel(make24(ptr), &rbd[i].buffer);
A
Al Viro 已提交
850
		ptr = ptr + RECV_BUFF_SIZE;
L
Linus Torvalds 已提交
851 852 853 854
	}
	p->rfd_top	= p->rfd_first;
	p->rfd_last = p->rfd_first + (p->num_recv_buffs - 1 + rfdadd);

855 856
	writew(make16(p->rfd_first), &p->scb->rfa_offset);
	writew(make16(rbd), &p->rfd_first->rbd_offset);
L
Linus Torvalds 已提交
857 858 859 860 861 862 863 864 865

	return ptr;
}


/**************************************************
 * Interrupt Handler ...
 */

866
static irqreturn_t ni52_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
867 868
{
	struct net_device *dev = dev_id;
869 870
	unsigned int stat;
	int cnt = 0;
L
Linus Torvalds 已提交
871 872 873 874
	struct priv *p;

	p = (struct priv *) dev->priv;

875
	if (debuglevel > 1)
L
Linus Torvalds 已提交
876 877
		printk("I");

878
	spin_lock(&p->spinlock);
L
Linus Torvalds 已提交
879

880 881 882 883
	wait_for_scb_cmd(dev); /* wait for last command	*/

	while ((stat = readb(&p->scb->cus) & STAT_MASK)) {
		writeb(stat, &p->scb->cmd_cuc);
L
Linus Torvalds 已提交
884 885
		ni_attn586();

886
		if (stat & STAT_FR)	 /* received a frame */
L
Linus Torvalds 已提交
887 888
			ni52_rcv_int(dev);

889
		if (stat & STAT_RNR) { /* RU went 'not ready' */
L
Linus Torvalds 已提交
890
			printk("(R)");
891 892 893
			if (readb(&p->scb->rus) & RU_SUSPEND) {
				/* special case: RU_SUSPEND */
				wait_for_scb_cmd(dev);
L
Linus Torvalds 已提交
894 895
				p->scb->cmd_ruc = RUC_RESUME;
				ni_attn586();
896 897 898 899
				wait_for_scb_cmd_ruc(dev);
			} else {
				printk(KERN_ERR "%s: Receiver-Unit went 'NOT READY': %04x/%02x.\n",
					dev->name, stat, readb(&p->scb->rus));
L
Linus Torvalds 已提交
900 901 902 903
				ni52_rnr_int(dev);
			}
		}

904 905
		/* Command with I-bit set complete */
		if (stat & STAT_CX)
L
Linus Torvalds 已提交
906 907 908
			 ni52_xmt_int(dev);

#ifndef NO_NOPCOMMANDS
909 910 911 912
		if (stat & STAT_CNA) {	/* CU went 'not ready' */
			if (netif_running(dev))
				printk(KERN_ERR "%s: oops! CU has left active state. stat: %04x/%02x.\n",
					dev->name, stat, readb(&p->scb->cus));
L
Linus Torvalds 已提交
913 914 915
		}
#endif

916 917
		if (debuglevel > 1)
			printk("%d", cnt++);
L
Linus Torvalds 已提交
918

919 920 921 922 923
		/* Wait for ack. (ni52_xmt_int can be faster than ack!!) */
		wait_for_scb_cmd(dev);
		if (p->scb->cmd_cuc) {	 /* timed out? */
			printk(KERN_ERR "%s: Acknowledge timed out.\n",
				dev->name);
L
Linus Torvalds 已提交
924 925 926 927
			ni_disint();
			break;
		}
	}
928
	spin_unlock(&p->spinlock);
L
Linus Torvalds 已提交
929

930
	if (debuglevel > 1)
L
Linus Torvalds 已提交
931 932 933 934 935 936 937 938 939 940
		printk("i");
	return IRQ_HANDLED;
}

/*******************************************************
 * receive-interrupt
 */

static void ni52_rcv_int(struct net_device *dev)
{
941
	int status, cnt = 0;
L
Linus Torvalds 已提交
942 943
	unsigned short totlen;
	struct sk_buff *skb;
A
Al Viro 已提交
944
	struct rbd_struct __iomem *rbd;
945
	struct priv *p = (struct priv *)dev->priv;
L
Linus Torvalds 已提交
946

947
	if (debuglevel > 0)
L
Linus Torvalds 已提交
948 949
		printk("R");

950
	for (; (status = readb(&p->rfd_top->stat_high)) & RFD_COMPL;) {
A
Al Viro 已提交
951
		rbd = (struct rbd_struct __iomem *) make32(p->rfd_top->rbd_offset);
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
		if (status & RFD_OK) { /* frame received without error? */
			totlen = readw(&rbd->status);
			if (totlen & RBD_LAST) {
				/* the first and the last buffer? */
				totlen &= RBD_MASK; /* length of this frame */
				writew(0x00, &rbd->status);
				skb = (struct sk_buff *)dev_alloc_skb(totlen+2);
				if (skb != NULL) {
					skb_reserve(skb, 2);
					skb_put(skb, totlen);
					skb_copy_to_linear_data(skb, (char *)p->base + (unsigned long) rbd->buffer, totlen);
					skb->protocol = eth_type_trans(skb, dev);
					netif_rx(skb);
					dev->last_rx = jiffies;
					p->stats.rx_packets++;
					p->stats.rx_bytes += totlen;
				} else
					p->stats.rx_dropped++;
			} else {
				int rstat;
				 /* free all RBD's until RBD_LAST is set */
				totlen = 0;
				while (!((rstat = readw(&rbd->status)) & RBD_LAST)) {
					totlen += rstat & RBD_MASK;
					if (!rstat) {
						printk(KERN_ERR "%s: Whoops .. no end mark in RBD list\n", dev->name);
						break;
L
Linus Torvalds 已提交
979
					}
980
					writew(0, &rbd->status);
A
Al Viro 已提交
981
					rbd = (struct rbd_struct __iomem *) make32(readl(&rbd->next));
L
Linus Torvalds 已提交
982
				}
983 984 985 986 987
				totlen += rstat & RBD_MASK;
				writew(0, &rbd->status);
				printk(KERN_ERR "%s: received oversized frame! length: %d\n",
					dev->name, totlen);
				p->stats.rx_dropped++;
L
Linus Torvalds 已提交
988
			 }
989 990 991
		} else {/* frame !(ok), only with 'save-bad-frames' */
			printk(KERN_ERR "%s: oops! rfd-error-status: %04x\n",
				dev->name, status);
L
Linus Torvalds 已提交
992 993
			p->stats.rx_errors++;
		}
994 995 996 997
		writeb(0, &p->rfd_top->stat_high);
		writeb(RFD_SUSP, &p->rfd_top->last); /* maybe exchange by RFD_LAST */
		writew(0xffff, &p->rfd_top->rbd_offset);
		writeb(0, &p->rfd_last->last);	/* delete RFD_SUSP	*/
L
Linus Torvalds 已提交
998
		p->rfd_last = p->rfd_top;
A
Al Viro 已提交
999
		p->rfd_top = (struct rfd_struct __iomem *) make32(p->rfd_top->next); /* step to next RFD */
1000
		writew(make16(p->rfd_top), &p->scb->rfa_offset);
L
Linus Torvalds 已提交
1001

1002 1003
		if (debuglevel > 0)
			printk("%d", cnt++);
L
Linus Torvalds 已提交
1004 1005
	}

1006 1007 1008
	if (automatic_resume) {
		wait_for_scb_cmd(dev);
		writeb(RUC_RESUME, &p->scb->cmd_ruc);
L
Linus Torvalds 已提交
1009
		ni_attn586();
1010
		wait_for_scb_cmd_ruc(dev);
L
Linus Torvalds 已提交
1011 1012 1013 1014 1015
	}

#ifdef WAIT_4_BUSY
	{
		int i;
1016 1017
		for (i = 0; i < 1024; i++) {
			if (p->rfd_top->status)
L
Linus Torvalds 已提交
1018
				break;
1019 1020 1021
			udelay(16);
			if (i == 1023)
				printk(KERN_ERR "%s: RU hasn't fetched next RFD (not busy/complete)\n", dev->name);
L
Linus Torvalds 已提交
1022 1023 1024
		}
	}
#endif
1025
	if (debuglevel > 0)
L
Linus Torvalds 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
		printk("r");
}

/**********************************************************
 * handle 'Receiver went not ready'.
 */

static void ni52_rnr_int(struct net_device *dev)
{
	struct priv *p = (struct priv *) dev->priv;

	p->stats.rx_errors++;

1039 1040
	wait_for_scb_cmd(dev);		/* wait for the last cmd, WAIT_4_FULLSTAT?? */
	writeb(RUC_ABORT, &p->scb->cmd_ruc); /* usually the RU is in the 'no resource'-state .. abort it now. */
L
Linus Torvalds 已提交
1041
	ni_attn586();
1042
	wait_for_scb_cmd_ruc(dev);		/* wait for accept cmd. */
L
Linus Torvalds 已提交
1043

A
Al Viro 已提交
1044
	alloc_rfa(dev, p->rfd_first);
1045
	/* maybe add a check here, before restarting the RU */
L
Linus Torvalds 已提交
1046 1047
	startrecv586(dev); /* restart RU */

1048
	printk(KERN_ERR "%s: Receive-Unit restarted. Status: %04x\n", dev->name, p->scb->rus);
L
Linus Torvalds 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

}

/**********************************************************
 * handle xmit - interrupt
 */

static void ni52_xmt_int(struct net_device *dev)
{
	int status;
	struct priv *p = (struct priv *) dev->priv;

1061
	if (debuglevel > 0)
L
Linus Torvalds 已提交
1062 1063
		printk("X");

1064 1065 1066
	status = readw(&p->xmit_cmds[p->xmit_last]->cmd_status);
	if (!(status & STAT_COMPL))
		printk(KERN_ERR "%s: strange .. xmit-int without a 'COMPLETE'\n", dev->name);
L
Linus Torvalds 已提交
1067

1068
	if (status & STAT_OK) {
L
Linus Torvalds 已提交
1069 1070
		p->stats.tx_packets++;
		p->stats.collisions += (status & TCMD_MAXCOLLMASK);
1071
	} else {
L
Linus Torvalds 已提交
1072
		p->stats.tx_errors++;
1073 1074 1075
		if (status & TCMD_LATECOLL) {
			printk(KERN_ERR "%s: late collision detected.\n",
				dev->name);
L
Linus Torvalds 已提交
1076
			p->stats.collisions++;
1077
		} else if (status & TCMD_NOCARRIER) {
L
Linus Torvalds 已提交
1078
			p->stats.tx_carrier_errors++;
1079 1080 1081 1082 1083 1084
			printk(KERN_ERR "%s: no carrier detected.\n",
				dev->name);
		} else if (status & TCMD_LOSTCTS)
			printk(KERN_ERR "%s: loss of CTS detected.\n",
				dev->name);
		else if (status & TCMD_UNDERRUN) {
L
Linus Torvalds 已提交
1085
			p->stats.tx_fifo_errors++;
1086 1087 1088 1089 1090
			printk(KERN_ERR "%s: DMA underrun detected.\n",
				dev->name);
		} else if (status & TCMD_MAXCOLL) {
			printk(KERN_ERR "%s: Max. collisions exceeded.\n",
				dev->name);
L
Linus Torvalds 已提交
1091 1092 1093 1094
			p->stats.collisions += 16;
		}
	}
#if (NUM_XMIT_BUFFS > 1)
1095
	if ((++p->xmit_last) == NUM_XMIT_BUFFS)
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
		p->xmit_last = 0;
#endif
	netif_wake_queue(dev);
}

/***********************************************************
 * (re)start the receiver
 */

static void startrecv586(struct net_device *dev)
{
	struct priv *p = (struct priv *) dev->priv;

1109 1110 1111 1112
	wait_for_scb_cmd(dev);
	wait_for_scb_cmd_ruc(dev);
	writew(make16(p->rfd_first), &p->scb->rfa_offset);
	writeb(RUC_START, &p->scb->cmd_ruc);
L
Linus Torvalds 已提交
1113
	ni_attn586();		/* start cmd. */
1114 1115
	wait_for_scb_cmd_ruc(dev);
	/* wait for accept cmd. (no timeout!!) */
L
Linus Torvalds 已提交
1116 1117 1118 1119 1120 1121
}

static void ni52_timeout(struct net_device *dev)
{
	struct priv *p = (struct priv *) dev->priv;
#ifndef NO_NOPCOMMANDS
1122
	if (readb(&p->scb->cus) & CU_ACTIVE) { /* COMMAND-UNIT active? */
L
Linus Torvalds 已提交
1123 1124
		netif_wake_queue(dev);
#ifdef DEBUG
1125 1126 1127 1128 1129 1130 1131
		printk(KERN_ERR "%s: strange ... timeout with CU active?!?\n",
			dev->name);
		printk(KERN_ERR "%s: X0: %04x N0: %04x N1: %04x %d\n",
			dev->name, (int)p->xmit_cmds[0]->cmd_status,
			readw(&p->nop_cmds[0]->cmd_status),
			readw(&p->nop_cmds[1]->cmd_status),
			p->nop_point);
L
Linus Torvalds 已提交
1132
#endif
1133
		writeb(CUC_ABORT, &p->scb->cmd_cuc);
L
Linus Torvalds 已提交
1134
		ni_attn586();
1135 1136 1137
		wait_for_scb_cmd(dev);
		writew(make16(p->nop_cmds[p->nop_point]), &p->scb->cbl_offset);
		writeb(CUC_START, &p->scb->cmd_cuc);
L
Linus Torvalds 已提交
1138
		ni_attn586();
1139
		wait_for_scb_cmd(dev);
L
Linus Torvalds 已提交
1140 1141 1142 1143 1144 1145
		dev->trans_start = jiffies;
		return 0;
	}
#endif
	{
#ifdef DEBUG
1146 1147 1148 1149 1150 1151 1152 1153
		printk(KERN_ERR "%s: xmitter timed out, try to restart! stat: %02x\n",
				dev->name, readb(&p->scb->cus));
		printk(KERN_ERR "%s: command-stats: %04x %04x\n",
				dev->name,
				readw(&p->xmit_cmds[0]->cmd_status),
				readw(&p->xmit_cmds[1]->cmd_status));
		printk(KERN_ERR "%s: check, whether you set the right interrupt number!\n",
				dev->name);
L
Linus Torvalds 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
#endif
		ni52_close(dev);
		ni52_open(dev);
	}
	dev->trans_start = jiffies;
}

/******************************************************
 * send frame
 */

static int ni52_send_packet(struct sk_buff *skb, struct net_device *dev)
{
1167
	int len, i;
L
Linus Torvalds 已提交
1168 1169 1170 1171 1172
#ifndef NO_NOPCOMMANDS
	int next_nop;
#endif
	struct priv *p = (struct priv *) dev->priv;

1173 1174
	if (skb->len > XMIT_BUFF_SIZE) {
		printk(KERN_ERR "%s: Sorry, max. framelength is %d bytes. The length of your frame is %d bytes.\n", dev->name, XMIT_BUFF_SIZE, skb->len);
L
Linus Torvalds 已提交
1175 1176 1177 1178 1179
		return 0;
	}

	netif_stop_queue(dev);

1180 1181 1182 1183 1184 1185 1186
	skb_copy_from_linear_data(skb, (char *)p->xmit_cbuffs[p->xmit_count],
							skb->len);
	len = skb->len;
	if (len < ETH_ZLEN) {
		len = ETH_ZLEN;
		memset((char *)p->xmit_cbuffs[p->xmit_count]+skb->len, 0,
							len - skb->len);
L
Linus Torvalds 已提交
1187 1188 1189 1190 1191 1192
	}

#if (NUM_XMIT_BUFFS == 1)
#	ifdef NO_NOPCOMMANDS

#ifdef DEBUG
1193 1194 1195 1196 1197 1198
	if (p->scb->cus & CU_ACTIVE) {
		printk(KERN_ERR "%s: Hmmm .. CU is still running and we wanna send a new packet.\n", dev->name);
		printk(KERN_ERR "%s: stat: %04x %04x\n",
				dev->name, readb(&p->scb->cus),
				readw(&p->xmit_cmds[0]->cmd_status));
	}
L
Linus Torvalds 已提交
1199
#endif
1200 1201 1202 1203 1204 1205 1206 1207 1208
	writew(TBD_LAST | len, &p->xmit_buffs[0]->size);;
	for (i = 0; i < 16; i++) {
		writew(0, &p->xmit_cmds[0]->cmd_status);
		wait_for_scb_cmd(dev);
		if ((readb(&p->scb->cus) & CU_STATUS) == CU_SUSPEND)
			writeb(CUC_RESUME, &p->scb->cmd_cuc);
		else {
			writew(make16(p->xmit_cmds[0]), &p->scb->cbl_offset);
			writeb(CUC_START, &p->scb->cmd_cuc);
L
Linus Torvalds 已提交
1209
		}
1210
		ni_attn586();
L
Linus Torvalds 已提交
1211
		dev->trans_start = jiffies;
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
		if (!i)
			dev_kfree_skb(skb);
		wait_for_scb_cmd(dev);
		/* test it, because CU sometimes doesn't start immediately */
		if (readb(&p->scb->cus) & CU_ACTIVE)
			break;
		if (readw(&p->xmit_cmds[0]->cmd_status))
			break;
		if (i == 15)
			printk(KERN_WARNING "%s: Can't start transmit-command.\n", dev->name);
	}
#	else
	next_nop = (p->nop_point + 1) & 0x1;
	writew(TBD_LAST | len, &p->xmit_buffs[0]->size);
	writew(make16(p->nop_cmds[next_nop]), &p->xmit_cmds[0]->cmd_link);
	writew(make16(p->nop_cmds[next_nop]),
				&p->nop_cmds[next_nop]->cmd_link);
	writew(0, &p->xmit_cmds[0]->cmd_status);
	writew(0, &p->nop_cmds[next_nop]->cmd_status);

	writew(make16(p->xmit_cmds[0]), &p->nop_cmds[p->nop_point]->cmd_link);
	dev->trans_start = jiffies;
	p->nop_point = next_nop;
	dev_kfree_skb(skb);
L
Linus Torvalds 已提交
1236 1237
#	endif
#else
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
	writew(TBD_LAST | len, &p->xmit_buffs[p->xmit_count]->size);
	next_nop = p->xmit_count + 1
	if (next_nop == NUM_XMIT_BUFFS)
		next_nop = 0;
	writew(0, &p->xmit_cmds[p->xmit_count]->cmd_status);
	/* linkpointer of xmit-command already points to next nop cmd */
	writew(make16(p->nop_cmds[next_nop]),
				&p->nop_cmds[next_nop]->cmd_link);
	writew(0, &p->nop_cmds[next_nop]->cmd_status);
	writew(make16(p->xmit_cmds[p->xmit_count]),
				&p->nop_cmds[p->xmit_count]->cmd_link);
	dev->trans_start = jiffies;
	p->xmit_count = next_nop;
	{
		unsigned long flags;
		spin_lock_irqsave(&p->spinlock);
		if (p->xmit_count != p->xmit_last)
			netif_wake_queue(dev);
		spin_unlock_irqrestore(&p->spinlock);
L
Linus Torvalds 已提交
1257
	}
1258 1259
	dev_kfree_skb(skb);
#endif
L
Linus Torvalds 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	return 0;
}

/*******************************************
 * Someone wanna have the statistics
 */

static struct net_device_stats *ni52_get_stats(struct net_device *dev)
{
	struct priv *p = (struct priv *) dev->priv;
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	unsigned short crc, aln, rsc, ovrn;

	/* Get error-statistics from the ni82586 */
	crc = readw(&p->scb->crc_errs);
	writew(0, &p->scb->crc_errs);
	aln = readw(&p->scb->aln_errs);
	writew(0, &p->scb->aln_errs);
	rsc = readw(&p->scb->rsc_errs);
	writew(0, &p->scb->rsc_errs);
	ovrn = readw(&p->scb->ovrn_errs);
	writew(0, &p->scb->ovrn_errs);
L
Linus Torvalds 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

	p->stats.rx_crc_errors += crc;
	p->stats.rx_fifo_errors += ovrn;
	p->stats.rx_frame_errors += aln;
	p->stats.rx_dropped += rsc;

	return &p->stats;
}

/********************************************************
 * Set MC list ..
 */

static void set_multicast_list(struct net_device *dev)
{
	netif_stop_queue(dev);
	ni_disint();
	alloc586(dev);
	init586(dev);
	startrecv586(dev);
	ni_enaint();
	netif_wake_queue(dev);
}

#ifdef MODULE
static struct net_device *dev_ni52;

module_param(io, int, 0);
module_param(irq, int, 0);
module_param(memstart, long, 0);
module_param(memend, long, 0);
MODULE_PARM_DESC(io, "NI5210 I/O base address,required");
MODULE_PARM_DESC(irq, "NI5210 IRQ number,required");
MODULE_PARM_DESC(memstart, "NI5210 memory base address,required");
MODULE_PARM_DESC(memend, "NI5210 memory end address,required");

A
Andrew Morton 已提交
1317
int __init init_module(void)
L
Linus Torvalds 已提交
1318
{
1319 1320 1321
	if (io <= 0x0 || !memend || !memstart || irq < 2) {
		printk(KERN_ERR "ni52: Autoprobing not allowed for modules.\n");
		printk(KERN_ERR "ni52: Set symbols 'io' 'irq' 'memstart' and 'memend'\n");
L
Linus Torvalds 已提交
1322 1323 1324 1325 1326 1327 1328 1329
		return -ENODEV;
	}
	dev_ni52 = ni52_probe(-1);
	if (IS_ERR(dev_ni52))
		return PTR_ERR(dev_ni52);
	return 0;
}

1330
void __exit cleanup_module(void)
L
Linus Torvalds 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
{
	unregister_netdev(dev_ni52);
	release_region(dev_ni52->base_addr, NI52_TOTAL_SIZE);
	free_netdev(dev_ni52);
}
#endif /* MODULE */

MODULE_LICENSE("GPL");

/*
 * END: linux/drivers/net/ni52.c
 */