2 * net-3-driver for the NI5210 card (i82586 Ethernet chip)
4 * This is an extension to the Linux operating system, and is covered by the
5 * same GNU General Public License that covers that work.
7 * Alphacode 0.82 (96/09/29) for Linux 2.0.0 (or later)
8 * Copyrights (c) 1994,1995,1996 by M.Hipp (hippm@informatik.uni-tuebingen.de)
9 * [feel free to mail ....]
11 * when using as module: (no autoprobing!)
13 * insmod ni52.o io=0x360 irq=9 memstart=0xd0000 memend=0xd4000
15 * CAN YOU PLEASE REPORT ME YOUR PERFORMANCE EXPERIENCES !!.
17 * If you find a bug, please report me:
18 * The kernel panic output and any kmsg from the ni52 driver
19 * the ni5210-driver-version and the linux-kernel version
20 * how many shared memory (memsize) on the netcard,
21 * bootprom: yes/no, base_addr, mem_start
22 * maybe the ni5210-card revision and the i82586 version
24 * autoprobe for: base_addr: 0x300,0x280,0x360,0x320,0x340
25 * mem_start: 0xd0000,0xd2000,0xc8000,0xca000,0xd4000,0xd6000,
26 * 0xd8000,0xcc000,0xce000,0xda000,0xdc000
29 * skeleton.c from Donald Becker
31 * I have also done a look in the following sources: (mail me if you need them)
32 * crynwr-packet-driver by Russ Nelson
33 * Garret A. Wollman's (fourth) i82586-driver for BSD
34 * (before getting an i82596 (yes 596 not 586) manual, the existing drivers
35 * helped me a lot to understand this tricky chip.)
38 * The internal sysbus seems to be slow. So we often lose packets because of
39 * overruns while receiving from a fast remote host.
40 * This can slow down TCP connections. Maybe the newer ni5210 cards are
41 * better. My experience is, that if a machine sends with more than about
42 * 500-600K/s the fifo/sysbus overflows.
45 * On fast networks, it's a (very) good idea to have 16K shared memory. With
46 * 8K, we can store only 4 receive frames, so it can (easily) happen that a
47 * remote machine 'overruns' our system.
49 * Known i82586/card problems (I'm sure, there are many more!):
50 * Running the NOP-mode, the i82586 sometimes seems to forget to report
51 * every xmit-interrupt until we restart the CU.
52 * Another MAJOR bug is, that the RU sometimes seems to ignore the EL-Bit
53 * in the RBD-Struct which indicates an end of the RBD queue.
54 * Instead, the RU fetches another (randomly selected and
55 * usually used) RBD and begins to fill it. (Maybe, this happens only if
56 * the last buffer from the previous RFD fits exact into the queue and
57 * the next RFD can't fetch an initial RBD. Anyone knows more? )
59 * results from ftp performance tests with Linux 1.2.5
60 * send and receive about 350-400 KByte/s (peak up to 460 kbytes/s)
61 * sending in NOP-mode: peak performance up to 530K/s (but better don't
66 static int debuglevel
; /* debug-printk 0: off 1: a few 2: more */
67 static int automatic_resume
; /* experimental .. better should be zero */
68 static int rfdadd
; /* rfdadd=1 may be better for 8K MEM cards */
69 static int fifo
= 0x8; /* don't change */
71 #include <linux/module.h>
72 #include <linux/kernel.h>
73 #include <linux/string.h>
74 #include <linux/errno.h>
75 #include <linux/ioport.h>
76 #include <linux/interrupt.h>
77 #include <linux/delay.h>
78 #include <linux/init.h>
79 #include <linux/bitops.h>
82 #include <linux/netdevice.h>
83 #include <linux/etherdevice.h>
84 #include <linux/skbuff.h>
88 #define DRV_NAME "ni52"
90 #define DEBUG /* debug on */
91 #define SYSBUSVAL 1 /* 8 Bit */
93 #define ni_attn586() { outb(0, dev->base_addr + NI52_ATTENTION); }
94 #define ni_reset586() { outb(0, dev->base_addr + NI52_RESET); }
95 #define ni_disint() { outb(0, dev->base_addr + NI52_INTDIS); }
96 #define ni_enaint() { outb(0, dev->base_addr + NI52_INTENA); }
98 #define make32(ptr16) ((void __iomem *)(p->memtop + (short) (ptr16)))
99 #define make24(ptr32) ((char __iomem *)(ptr32)) - p->base
100 #define make16(ptr32) ((unsigned short) ((char __iomem *)(ptr32)\
103 /******************* how to calculate the buffers *****************************
105 * IMPORTANT NOTE: if you configure only one NUM_XMIT_BUFFS, the driver works
106 * --------------- in a different (more stable?) mode. Only in this mode it's
107 * possible to configure the driver with 'NO_NOPCOMMANDS'
109 sizeof(scp)=12; sizeof(scb)=16; sizeof(iscp)=8;
110 sizeof(scp)+sizeof(iscp)+sizeof(scb) = 36 = INIT
111 sizeof(rfd) = 24; sizeof(rbd) = 12;
112 sizeof(tbd) = 8; sizeof(transmit_cmd) = 16;
115 * if you don't know the driver, better do not change these values: */
117 #define RECV_BUFF_SIZE 1524 /* slightly oversized */
118 #define XMIT_BUFF_SIZE 1524 /* slightly oversized */
119 #define NUM_XMIT_BUFFS 1 /* config for both, 8K and 16K shmem */
120 #define NUM_RECV_BUFFS_8 4 /* config for 8K shared mem */
121 #define NUM_RECV_BUFFS_16 9 /* config for 16K shared mem */
122 #define NO_NOPCOMMANDS /* only possible with NUM_XMIT_BUFFS=1 */
124 /**************************************************************************/
127 #define NI52_TOTAL_SIZE 16
128 #define NI52_ADDR0 0x02
129 #define NI52_ADDR1 0x07
130 #define NI52_ADDR2 0x01
132 static int ni52_probe1(struct net_device
*dev
, int ioaddr
);
133 static irqreturn_t
ni52_interrupt(int irq
, void *dev_id
);
134 static int ni52_open(struct net_device
*dev
);
135 static int ni52_close(struct net_device
*dev
);
136 static netdev_tx_t
ni52_send_packet(struct sk_buff
*, struct net_device
*);
137 static struct net_device_stats
*ni52_get_stats(struct net_device
*dev
);
138 static void set_multicast_list(struct net_device
*dev
);
139 static void ni52_timeout(struct net_device
*dev
);
141 /* helper-functions */
142 static int init586(struct net_device
*dev
);
143 static int check586(struct net_device
*dev
, unsigned size
);
144 static void alloc586(struct net_device
*dev
);
145 static void startrecv586(struct net_device
*dev
);
146 static void __iomem
*alloc_rfa(struct net_device
*dev
, void __iomem
*ptr
);
147 static void ni52_rcv_int(struct net_device
*dev
);
148 static void ni52_xmt_int(struct net_device
*dev
);
149 static void ni52_rnr_int(struct net_device
*dev
);
153 char __iomem
*mapped
;
154 char __iomem
*memtop
;
157 struct rfd_struct __iomem
*rfd_last
, *rfd_top
, *rfd_first
;
158 struct scp_struct __iomem
*scp
;
159 struct iscp_struct __iomem
*iscp
;
160 struct scb_struct __iomem
*scb
;
161 struct tbd_struct __iomem
*xmit_buffs
[NUM_XMIT_BUFFS
];
162 #if (NUM_XMIT_BUFFS == 1)
163 struct transmit_cmd_struct __iomem
*xmit_cmds
[2];
164 struct nop_cmd_struct __iomem
*nop_cmds
[2];
166 struct transmit_cmd_struct __iomem
*xmit_cmds
[NUM_XMIT_BUFFS
];
167 struct nop_cmd_struct __iomem
*nop_cmds
[NUM_XMIT_BUFFS
];
169 int nop_point
, num_recv_buffs
;
170 char __iomem
*xmit_cbuffs
[NUM_XMIT_BUFFS
];
171 int xmit_count
, xmit_last
;
174 /* wait for command with timeout: */
175 static void wait_for_scb_cmd(struct net_device
*dev
)
177 struct priv
*p
= netdev_priv(dev
);
179 for (i
= 0; i
< 16384; i
++) {
180 if (readb(&p
->scb
->cmd_cuc
) == 0)
184 printk(KERN_ERR
"%s: scb_cmd timed out: %04x,%04x .. disabling i82586!!\n",
185 dev
->name
, readb(&p
->scb
->cmd_cuc
), readb(&p
->scb
->cus
));
194 static void wait_for_scb_cmd_ruc(struct net_device
*dev
)
196 struct priv
*p
= netdev_priv(dev
);
198 for (i
= 0; i
< 16384; i
++) {
199 if (readb(&p
->scb
->cmd_ruc
) == 0)
203 printk(KERN_ERR
"%s: scb_cmd (ruc) timed out: %04x,%04x .. disabling i82586!!\n",
204 dev
->name
, readb(&p
->scb
->cmd_ruc
),
205 readb(&p
->scb
->rus
));
214 static void wait_for_stat_compl(void __iomem
*p
)
216 struct nop_cmd_struct __iomem
*addr
= p
;
218 for (i
= 0; i
< 32767; i
++) {
219 if (readw(&((addr
)->cmd_status
)) & STAT_COMPL
)
225 /**********************************************
228 static int ni52_close(struct net_device
*dev
)
230 free_irq(dev
->irq
, dev
);
231 ni_reset586(); /* the hard way to stop the receiver */
232 netif_stop_queue(dev
);
236 /**********************************************
239 static int ni52_open(struct net_device
*dev
)
249 ret
= request_irq(dev
->irq
, ni52_interrupt
, 0, dev
->name
, dev
);
254 netif_start_queue(dev
);
255 return 0; /* most done by init */
258 static int check_iscp(struct net_device
*dev
, void __iomem
*addr
)
260 struct iscp_struct __iomem
*iscp
= addr
;
261 struct priv
*p
= netdev_priv(dev
);
262 memset_io(iscp
, 0, sizeof(struct iscp_struct
));
264 writel(make24(iscp
), &p
->scp
->iscp
);
265 writeb(1, &iscp
->busy
);
269 mdelay(32); /* wait a while... */
270 /* i82586 clears 'busy' after successful init */
271 if (readb(&iscp
->busy
))
276 /**********************************************
277 * Check to see if there's an 82586 out there.
279 static int check586(struct net_device
*dev
, unsigned size
)
281 struct priv
*p
= netdev_priv(dev
);
284 p
->mapped
= ioremap(dev
->mem_start
, size
);
288 p
->base
= p
->mapped
+ size
- 0x01000000;
289 p
->memtop
= p
->mapped
+ size
;
290 p
->scp
= (struct scp_struct __iomem
*)(p
->base
+ SCP_DEFAULT_ADDRESS
);
291 p
->scb
= (struct scb_struct __iomem
*) p
->mapped
;
292 p
->iscp
= (struct iscp_struct __iomem
*)p
->scp
- 1;
293 memset_io(p
->scp
, 0, sizeof(struct scp_struct
));
294 for (i
= 0; i
< sizeof(struct scp_struct
); i
++)
295 /* memory was writeable? */
296 if (readb((char __iomem
*)p
->scp
+ i
))
298 writeb(SYSBUSVAL
, &p
->scp
->sysbus
); /* 1 = 8Bit-Bus, 0 = 16 Bit */
299 if (readb(&p
->scp
->sysbus
) != SYSBUSVAL
)
302 if (!check_iscp(dev
, p
->mapped
))
304 if (!check_iscp(dev
, p
->iscp
))
312 /******************************************************************
313 * set iscp at the right place, called by ni52_probe1 and open586.
315 static void alloc586(struct net_device
*dev
)
317 struct priv
*p
= netdev_priv(dev
);
322 memset_io(p
->iscp
, 0, sizeof(struct iscp_struct
));
323 memset_io(p
->scp
, 0, sizeof(struct scp_struct
));
325 writel(make24(p
->iscp
), &p
->scp
->iscp
);
326 writeb(SYSBUSVAL
, &p
->scp
->sysbus
);
327 writew(make16(p
->scb
), &p
->iscp
->scb_offset
);
329 writeb(1, &p
->iscp
->busy
);
335 if (readb(&p
->iscp
->busy
))
336 printk(KERN_ERR
"%s: Init-Problems (alloc).\n", dev
->name
);
340 memset_io(p
->scb
, 0, sizeof(struct scb_struct
));
343 /* set: io,irq,memstart,memend or set it when calling insmod */
345 static int io
= 0x300;
346 static long memstart
; /* e.g 0xd0000 */
347 static long memend
; /* e.g 0xd4000 */
349 /**********************************************
350 * probe the ni5210-card
352 struct net_device
* __init
ni52_probe(int unit
)
354 struct net_device
*dev
= alloc_etherdev(sizeof(struct priv
));
355 static int ports
[] = {0x300, 0x280, 0x360 , 0x320 , 0x340, 0};
361 return ERR_PTR(-ENOMEM
);
363 p
= netdev_priv(dev
);
366 sprintf(dev
->name
, "eth%d", unit
);
367 netdev_boot_setup_check(dev
);
370 memstart
= dev
->mem_start
;
371 memend
= dev
->mem_end
;
374 if (io
> 0x1ff) { /* Check a single specified location. */
375 err
= ni52_probe1(dev
, io
);
376 } else if (io
> 0) { /* Don't probe at all. */
379 for (port
= ports
; *port
&& ni52_probe1(dev
, *port
) ; port
++)
384 for (io
= 0x200; io
< 0x400 && ni52_probe1(dev
, io
); io
+= 8)
394 err
= register_netdev(dev
);
400 release_region(dev
->base_addr
, NI52_TOTAL_SIZE
);
406 static const struct net_device_ops ni52_netdev_ops
= {
407 .ndo_open
= ni52_open
,
408 .ndo_stop
= ni52_close
,
409 .ndo_get_stats
= ni52_get_stats
,
410 .ndo_tx_timeout
= ni52_timeout
,
411 .ndo_start_xmit
= ni52_send_packet
,
412 .ndo_set_multicast_list
= set_multicast_list
,
413 .ndo_change_mtu
= eth_change_mtu
,
414 .ndo_set_mac_address
= eth_mac_addr
,
415 .ndo_validate_addr
= eth_validate_addr
,
418 static int __init
ni52_probe1(struct net_device
*dev
, int ioaddr
)
421 struct priv
*priv
= netdev_priv(dev
);
423 dev
->base_addr
= ioaddr
;
425 dev
->mem_start
= memstart
;
426 dev
->mem_end
= memend
;
428 spin_lock_init(&priv
->spinlock
);
430 if (!request_region(ioaddr
, NI52_TOTAL_SIZE
, DRV_NAME
))
433 if (!(inb(ioaddr
+NI52_MAGIC1
) == NI52_MAGICVAL1
) ||
434 !(inb(ioaddr
+NI52_MAGIC2
) == NI52_MAGICVAL2
)) {
439 for (i
= 0; i
< ETH_ALEN
; i
++)
440 dev
->dev_addr
[i
] = inb(dev
->base_addr
+i
);
442 if (dev
->dev_addr
[0] != NI52_ADDR0
|| dev
->dev_addr
[1] != NI52_ADDR1
||
443 dev
->dev_addr
[2] != NI52_ADDR2
) {
448 printk(KERN_INFO
"%s: NI5210 found at %#3lx, ",
449 dev
->name
, dev
->base_addr
);
452 * check (or search) IO-Memory, 8K and 16K
455 size
= dev
->mem_end
- dev
->mem_start
;
456 if (size
!= 0x2000 && size
!= 0x4000) {
458 printk(KERN_ERR
"%s: Invalid memory size %d. Allowed is 0x2000 or 0x4000 bytes.\n", dev
->name
, size
);
462 if (!check586(dev
, size
)) {
463 printk(KERN_ERR
"?memcheck, Can't find memory at 0x%lx with size %d!\n", dev
->mem_start
, size
);
468 if (dev
->mem_start
!= 0) {
469 /* no auto-mem-probe */
470 size
= 0x4000; /* check for 16K mem */
471 if (!check586(dev
, size
)) {
472 size
= 0x2000; /* check for 8K mem */
473 if (!check586(dev
, size
)) {
474 printk(KERN_ERR
"?memprobe, Can't find memory at 0x%lx!\n", dev
->mem_start
);
480 static const unsigned long memaddrs
[] = {
481 0xc8000, 0xca000, 0xcc000, 0xce000, 0xd0000, 0xd2000,
482 0xd4000, 0xd6000, 0xd8000, 0xda000, 0xdc000, 0
486 printk(KERN_ERR
"?memprobe, Can't find io-memory!\n");
490 dev
->mem_start
= memaddrs
[i
];
491 size
= 0x2000; /* check for 8K mem */
492 if (check586(dev
, size
))
495 size
= 0x4000; /* check for 16K mem */
496 if (check586(dev
, size
))
501 /* set mem_end showed by 'ifconfig' */
502 dev
->mem_end
= dev
->mem_start
+ size
;
507 /* set number of receive-buffs according to memsize */
509 priv
->num_recv_buffs
= NUM_RECV_BUFFS_8
;
511 priv
->num_recv_buffs
= NUM_RECV_BUFFS_16
;
513 printk(KERN_DEBUG
"Memaddr: 0x%lx, Memsize: %d, ",
514 dev
->mem_start
, size
);
517 unsigned long irq_mask
;
519 irq_mask
= probe_irq_on();
524 dev
->irq
= probe_irq_off(irq_mask
);
526 printk("?autoirq, Failed to detect IRQ line!\n");
528 iounmap(priv
->mapped
);
531 printk("IRQ %d (autodetected).\n", dev
->irq
);
535 printk("IRQ %d (assigned and not checked!).\n", dev
->irq
);
538 dev
->netdev_ops
= &ni52_netdev_ops
;
539 dev
->watchdog_timeo
= HZ
/20;
543 release_region(ioaddr
, NI52_TOTAL_SIZE
);
547 /**********************************************
548 * init the chip (ni52-interrupt should be disabled?!)
549 * needs a correct 'allocated' memory
552 static int init586(struct net_device
*dev
)
556 struct priv
*p
= netdev_priv(dev
);
557 struct configure_cmd_struct __iomem
*cfg_cmd
;
558 struct iasetup_cmd_struct __iomem
*ias_cmd
;
559 struct tdr_cmd_struct __iomem
*tdr_cmd
;
560 struct mcsetup_cmd_struct __iomem
*mc_cmd
;
561 struct netdev_hw_addr
*ha
;
562 int num_addrs
= netdev_mc_count(dev
);
566 cfg_cmd
= ptr
; /* configure-command */
567 writew(0, &cfg_cmd
->cmd_status
);
568 writew(CMD_CONFIGURE
| CMD_LAST
, &cfg_cmd
->cmd_cmd
);
569 writew(0xFFFF, &cfg_cmd
->cmd_link
);
571 /* number of cfg bytes */
572 writeb(0x0a, &cfg_cmd
->byte_cnt
);
573 /* fifo-limit (8=tx:32/rx:64) */
574 writeb(fifo
, &cfg_cmd
->fifo
);
575 /* hold or discard bad recv frames (bit 7) */
576 writeb(0x40, &cfg_cmd
->sav_bf
);
577 /* addr_len |!src_insert |pre-len |loopback */
578 writeb(0x2e, &cfg_cmd
->adr_len
);
579 writeb(0x00, &cfg_cmd
->priority
);
580 writeb(0x60, &cfg_cmd
->ifs
);
581 writeb(0x00, &cfg_cmd
->time_low
);
582 writeb(0xf2, &cfg_cmd
->time_high
);
583 writeb(0x00, &cfg_cmd
->promisc
);
584 if (dev
->flags
& IFF_ALLMULTI
) {
585 int len
= ((char __iomem
*)p
->iscp
- (char __iomem
*)ptr
- 8) / 6;
586 if (num_addrs
> len
) {
587 printk(KERN_ERR
"%s: switching to promisc. mode\n",
589 writeb(0x01, &cfg_cmd
->promisc
);
592 if (dev
->flags
& IFF_PROMISC
)
593 writeb(0x01, &cfg_cmd
->promisc
);
594 writeb(0x00, &cfg_cmd
->carr_coll
);
595 writew(make16(cfg_cmd
), &p
->scb
->cbl_offset
);
596 writeb(0, &p
->scb
->cmd_ruc
);
598 writeb(CUC_START
, &p
->scb
->cmd_cuc
); /* cmd.-unit start */
601 wait_for_stat_compl(cfg_cmd
);
603 if ((readw(&cfg_cmd
->cmd_status
) & (STAT_OK
|STAT_COMPL
)) !=
604 (STAT_COMPL
|STAT_OK
)) {
605 printk(KERN_ERR
"%s: configure command failed: %x\n",
606 dev
->name
, readw(&cfg_cmd
->cmd_status
));
611 * individual address setup
616 writew(0, &ias_cmd
->cmd_status
);
617 writew(CMD_IASETUP
| CMD_LAST
, &ias_cmd
->cmd_cmd
);
618 writew(0xffff, &ias_cmd
->cmd_link
);
620 memcpy_toio(&ias_cmd
->iaddr
, (char *)dev
->dev_addr
, ETH_ALEN
);
622 writew(make16(ias_cmd
), &p
->scb
->cbl_offset
);
624 writeb(CUC_START
, &p
->scb
->cmd_cuc
); /* cmd.-unit start */
627 wait_for_stat_compl(ias_cmd
);
629 if ((readw(&ias_cmd
->cmd_status
) & (STAT_OK
|STAT_COMPL
)) !=
630 (STAT_OK
|STAT_COMPL
)) {
631 printk(KERN_ERR
"%s (ni52): individual address setup command failed: %04x\n", dev
->name
, readw(&ias_cmd
->cmd_status
));
636 * TDR, wire check .. e.g. no resistor e.t.c
641 writew(0, &tdr_cmd
->cmd_status
);
642 writew(CMD_TDR
| CMD_LAST
, &tdr_cmd
->cmd_cmd
);
643 writew(0xffff, &tdr_cmd
->cmd_link
);
644 writew(0, &tdr_cmd
->status
);
646 writew(make16(tdr_cmd
), &p
->scb
->cbl_offset
);
647 writeb(CUC_START
, &p
->scb
->cmd_cuc
); /* cmd.-unit start */
650 wait_for_stat_compl(tdr_cmd
);
652 if (!(readw(&tdr_cmd
->cmd_status
) & STAT_COMPL
))
653 printk(KERN_ERR
"%s: Problems while running the TDR.\n",
657 result
= readw(&tdr_cmd
->status
);
658 writeb(readb(&p
->scb
->cus
) & STAT_MASK
, &p
->scb
->cmd_cuc
);
659 ni_attn586(); /* ack the interrupts */
661 if (result
& TDR_LNK_OK
)
663 else if (result
& TDR_XCVR_PRB
)
664 printk(KERN_ERR
"%s: TDR: Transceiver problem. Check the cable(s)!\n",
666 else if (result
& TDR_ET_OPN
)
667 printk(KERN_ERR
"%s: TDR: No correct termination %d clocks away.\n",
668 dev
->name
, result
& TDR_TIMEMASK
);
669 else if (result
& TDR_ET_SRT
) {
670 /* time == 0 -> strange :-) */
671 if (result
& TDR_TIMEMASK
)
672 printk(KERN_ERR
"%s: TDR: Detected a short circuit %d clocks away.\n",
673 dev
->name
, result
& TDR_TIMEMASK
);
675 printk(KERN_ERR
"%s: TDR: Unknown status %04x\n",
682 if (num_addrs
&& !(dev
->flags
& IFF_PROMISC
)) {
684 writew(0, &mc_cmd
->cmd_status
);
685 writew(CMD_MCSETUP
| CMD_LAST
, &mc_cmd
->cmd_cmd
);
686 writew(0xffff, &mc_cmd
->cmd_link
);
687 writew(num_addrs
* 6, &mc_cmd
->mc_cnt
);
690 netdev_for_each_mc_addr(ha
, dev
)
691 memcpy_toio(mc_cmd
->mc_list
[i
++], ha
->addr
, 6);
693 writew(make16(mc_cmd
), &p
->scb
->cbl_offset
);
694 writeb(CUC_START
, &p
->scb
->cmd_cuc
);
697 wait_for_stat_compl(mc_cmd
);
699 if ((readw(&mc_cmd
->cmd_status
) & (STAT_COMPL
|STAT_OK
))
700 != (STAT_COMPL
|STAT_OK
))
701 printk(KERN_ERR
"%s: Can't apply multicast-address-list.\n", dev
->name
);
705 * alloc nop/xmit-cmds
707 #if (NUM_XMIT_BUFFS == 1)
708 for (i
= 0; i
< 2; i
++) {
709 p
->nop_cmds
[i
] = ptr
;
710 writew(CMD_NOP
, &p
->nop_cmds
[i
]->cmd_cmd
);
711 writew(0, &p
->nop_cmds
[i
]->cmd_status
);
712 writew(make16(p
->nop_cmds
[i
]), &p
->nop_cmds
[i
]->cmd_link
);
713 ptr
= ptr
+ sizeof(struct nop_cmd_struct
);
716 for (i
= 0; i
< NUM_XMIT_BUFFS
; i
++) {
717 p
->nop_cmds
[i
] = ptr
;
718 writew(CMD_NOP
, &p
->nop_cmds
[i
]->cmd_cmd
);
719 writew(0, &p
->nop_cmds
[i
]->cmd_status
);
720 writew(make16(p
->nop_cmds
[i
]), &p
->nop_cmds
[i
]->cmd_link
);
721 ptr
= ptr
+ sizeof(struct nop_cmd_struct
);
725 ptr
= alloc_rfa(dev
, ptr
); /* init receive-frame-area */
728 * alloc xmit-buffs / init xmit_cmds
730 for (i
= 0; i
< NUM_XMIT_BUFFS
; i
++) {
731 /* Transmit cmd/buff 0 */
732 p
->xmit_cmds
[i
] = ptr
;
733 ptr
= ptr
+ sizeof(struct transmit_cmd_struct
);
734 p
->xmit_cbuffs
[i
] = ptr
; /* char-buffs */
735 ptr
= ptr
+ XMIT_BUFF_SIZE
;
736 p
->xmit_buffs
[i
] = ptr
; /* TBD */
737 ptr
= ptr
+ sizeof(struct tbd_struct
);
738 if ((void __iomem
*)ptr
> (void __iomem
*)p
->iscp
) {
739 printk(KERN_ERR
"%s: not enough shared-mem for your configuration!\n",
743 memset_io(p
->xmit_cmds
[i
], 0,
744 sizeof(struct transmit_cmd_struct
));
745 memset_io(p
->xmit_buffs
[i
], 0,
746 sizeof(struct tbd_struct
));
747 writew(make16(p
->nop_cmds
[(i
+1)%NUM_XMIT_BUFFS
]),
748 &p
->xmit_cmds
[i
]->cmd_link
);
749 writew(STAT_COMPL
, &p
->xmit_cmds
[i
]->cmd_status
);
750 writew(CMD_XMIT
|CMD_INT
, &p
->xmit_cmds
[i
]->cmd_cmd
);
751 writew(make16(p
->xmit_buffs
[i
]), &p
->xmit_cmds
[i
]->tbd_offset
);
752 writew(0xffff, &p
->xmit_buffs
[i
]->next
);
753 writel(make24(p
->xmit_cbuffs
[i
]), &p
->xmit_buffs
[i
]->buffer
);
758 #ifndef NO_NOPCOMMANDS
763 * 'start transmitter'
765 #ifndef NO_NOPCOMMANDS
766 writew(make16(p
->nop_cmds
[0]), &p
->scb
->cbl_offset
);
767 writeb(CUC_START
, &p
->scb
->cmd_cuc
);
769 wait_for_scb_cmd(dev
);
771 writew(make16(p
->xmit_cmds
[0]), &p
->xmit_cmds
[0]->cmd_link
);
772 writew(CMD_XMIT
| CMD_SUSPEND
| CMD_INT
, &p
->xmit_cmds
[0]->cmd_cmd
);
778 writeb(readb(&p
->scb
->cus
) & STAT_MASK
, &p
->scb
->cmd_cuc
);
787 /******************************************************
788 * This is a helper routine for ni52_rnr_int() and init586().
789 * It sets up the Receive Frame Area (RFA).
792 static void __iomem
*alloc_rfa(struct net_device
*dev
, void __iomem
*ptr
)
794 struct rfd_struct __iomem
*rfd
= ptr
;
795 struct rbd_struct __iomem
*rbd
;
797 struct priv
*p
= netdev_priv(dev
);
800 sizeof(struct rfd_struct
) * (p
->num_recv_buffs
+ rfdadd
));
803 for (i
= 0; i
< (p
->num_recv_buffs
+ rfdadd
); i
++) {
804 writew(make16(rfd
+ (i
+1) % (p
->num_recv_buffs
+rfdadd
)),
806 writew(0xffff, &rfd
[i
].rbd_offset
);
809 writeb(RFD_SUSP
, &rfd
[p
->num_recv_buffs
-1+rfdadd
].last
);
811 ptr
= rfd
+ (p
->num_recv_buffs
+ rfdadd
);
814 ptr
= rbd
+ p
->num_recv_buffs
;
816 /* clr descriptors */
817 memset_io(rbd
, 0, sizeof(struct rbd_struct
) * (p
->num_recv_buffs
));
819 for (i
= 0; i
< p
->num_recv_buffs
; i
++) {
820 writew(make16(rbd
+ (i
+1) % p
->num_recv_buffs
), &rbd
[i
].next
);
821 writew(RECV_BUFF_SIZE
, &rbd
[i
].size
);
822 writel(make24(ptr
), &rbd
[i
].buffer
);
823 ptr
= ptr
+ RECV_BUFF_SIZE
;
825 p
->rfd_top
= p
->rfd_first
;
826 p
->rfd_last
= p
->rfd_first
+ (p
->num_recv_buffs
- 1 + rfdadd
);
828 writew(make16(p
->rfd_first
), &p
->scb
->rfa_offset
);
829 writew(make16(rbd
), &p
->rfd_first
->rbd_offset
);
835 /**************************************************
836 * Interrupt Handler ...
839 static irqreturn_t
ni52_interrupt(int irq
, void *dev_id
)
841 struct net_device
*dev
= dev_id
;
846 p
= netdev_priv(dev
);
851 spin_lock(&p
->spinlock
);
853 wait_for_scb_cmd(dev
); /* wait for last command */
855 while ((stat
= readb(&p
->scb
->cus
) & STAT_MASK
)) {
856 writeb(stat
, &p
->scb
->cmd_cuc
);
859 if (stat
& STAT_FR
) /* received a frame */
862 if (stat
& STAT_RNR
) { /* RU went 'not ready' */
864 if (readb(&p
->scb
->rus
) & RU_SUSPEND
) {
865 /* special case: RU_SUSPEND */
866 wait_for_scb_cmd(dev
);
867 writeb(RUC_RESUME
, &p
->scb
->cmd_ruc
);
869 wait_for_scb_cmd_ruc(dev
);
871 printk(KERN_ERR
"%s: Receiver-Unit went 'NOT READY': %04x/%02x.\n",
872 dev
->name
, stat
, readb(&p
->scb
->rus
));
877 /* Command with I-bit set complete */
881 #ifndef NO_NOPCOMMANDS
882 if (stat
& STAT_CNA
) { /* CU went 'not ready' */
883 if (netif_running(dev
))
884 printk(KERN_ERR
"%s: oops! CU has left active state. stat: %04x/%02x.\n",
885 dev
->name
, stat
, readb(&p
->scb
->cus
));
892 /* Wait for ack. (ni52_xmt_int can be faster than ack!!) */
893 wait_for_scb_cmd(dev
);
894 if (readb(&p
->scb
->cmd_cuc
)) { /* timed out? */
895 printk(KERN_ERR
"%s: Acknowledge timed out.\n",
901 spin_unlock(&p
->spinlock
);
908 /*******************************************************
912 static void ni52_rcv_int(struct net_device
*dev
)
915 unsigned short totlen
;
917 struct rbd_struct __iomem
*rbd
;
918 struct priv
*p
= netdev_priv(dev
);
923 for (; (status
= readb(&p
->rfd_top
->stat_high
)) & RFD_COMPL
;) {
924 rbd
= make32(readw(&p
->rfd_top
->rbd_offset
));
925 if (status
& RFD_OK
) { /* frame received without error? */
926 totlen
= readw(&rbd
->status
);
927 if (totlen
& RBD_LAST
) {
928 /* the first and the last buffer? */
929 totlen
&= RBD_MASK
; /* length of this frame */
930 writew(0x00, &rbd
->status
);
931 skb
= (struct sk_buff
*)dev_alloc_skb(totlen
+2);
934 skb_put(skb
, totlen
);
935 memcpy_fromio(skb
->data
, p
->base
+ readl(&rbd
->buffer
), totlen
);
936 skb
->protocol
= eth_type_trans(skb
, dev
);
938 dev
->stats
.rx_packets
++;
939 dev
->stats
.rx_bytes
+= totlen
;
941 dev
->stats
.rx_dropped
++;
944 /* free all RBD's until RBD_LAST is set */
946 while (!((rstat
= readw(&rbd
->status
)) & RBD_LAST
)) {
947 totlen
+= rstat
& RBD_MASK
;
949 printk(KERN_ERR
"%s: Whoops .. no end mark in RBD list\n", dev
->name
);
952 writew(0, &rbd
->status
);
953 rbd
= make32(readw(&rbd
->next
));
955 totlen
+= rstat
& RBD_MASK
;
956 writew(0, &rbd
->status
);
957 printk(KERN_ERR
"%s: received oversized frame! length: %d\n",
959 dev
->stats
.rx_dropped
++;
961 } else {/* frame !(ok), only with 'save-bad-frames' */
962 printk(KERN_ERR
"%s: oops! rfd-error-status: %04x\n",
964 dev
->stats
.rx_errors
++;
966 writeb(0, &p
->rfd_top
->stat_high
);
967 writeb(RFD_SUSP
, &p
->rfd_top
->last
); /* maybe exchange by RFD_LAST */
968 writew(0xffff, &p
->rfd_top
->rbd_offset
);
969 writeb(0, &p
->rfd_last
->last
); /* delete RFD_SUSP */
970 p
->rfd_last
= p
->rfd_top
;
971 p
->rfd_top
= make32(readw(&p
->rfd_top
->next
)); /* step to next RFD */
972 writew(make16(p
->rfd_top
), &p
->scb
->rfa_offset
);
978 if (automatic_resume
) {
979 wait_for_scb_cmd(dev
);
980 writeb(RUC_RESUME
, &p
->scb
->cmd_ruc
);
982 wait_for_scb_cmd_ruc(dev
);
988 for (i
= 0; i
< 1024; i
++) {
989 if (p
->rfd_top
->status
)
993 printk(KERN_ERR
"%s: RU hasn't fetched next RFD (not busy/complete)\n", dev
->name
);
1001 /**********************************************************
1002 * handle 'Receiver went not ready'.
1005 static void ni52_rnr_int(struct net_device
*dev
)
1007 struct priv
*p
= netdev_priv(dev
);
1009 dev
->stats
.rx_errors
++;
1011 wait_for_scb_cmd(dev
); /* wait for the last cmd, WAIT_4_FULLSTAT?? */
1012 writeb(RUC_ABORT
, &p
->scb
->cmd_ruc
); /* usually the RU is in the 'no resource'-state .. abort it now. */
1014 wait_for_scb_cmd_ruc(dev
); /* wait for accept cmd. */
1016 alloc_rfa(dev
, p
->rfd_first
);
1017 /* maybe add a check here, before restarting the RU */
1018 startrecv586(dev
); /* restart RU */
1020 printk(KERN_ERR
"%s: Receive-Unit restarted. Status: %04x\n",
1021 dev
->name
, readb(&p
->scb
->rus
));
1025 /**********************************************************
1026 * handle xmit - interrupt
1029 static void ni52_xmt_int(struct net_device
*dev
)
1032 struct priv
*p
= netdev_priv(dev
);
1037 status
= readw(&p
->xmit_cmds
[p
->xmit_last
]->cmd_status
);
1038 if (!(status
& STAT_COMPL
))
1039 printk(KERN_ERR
"%s: strange .. xmit-int without a 'COMPLETE'\n", dev
->name
);
1041 if (status
& STAT_OK
) {
1042 dev
->stats
.tx_packets
++;
1043 dev
->stats
.collisions
+= (status
& TCMD_MAXCOLLMASK
);
1045 dev
->stats
.tx_errors
++;
1046 if (status
& TCMD_LATECOLL
) {
1047 printk(KERN_ERR
"%s: late collision detected.\n",
1049 dev
->stats
.collisions
++;
1050 } else if (status
& TCMD_NOCARRIER
) {
1051 dev
->stats
.tx_carrier_errors
++;
1052 printk(KERN_ERR
"%s: no carrier detected.\n",
1054 } else if (status
& TCMD_LOSTCTS
)
1055 printk(KERN_ERR
"%s: loss of CTS detected.\n",
1057 else if (status
& TCMD_UNDERRUN
) {
1058 dev
->stats
.tx_fifo_errors
++;
1059 printk(KERN_ERR
"%s: DMA underrun detected.\n",
1061 } else if (status
& TCMD_MAXCOLL
) {
1062 printk(KERN_ERR
"%s: Max. collisions exceeded.\n",
1064 dev
->stats
.collisions
+= 16;
1067 #if (NUM_XMIT_BUFFS > 1)
1068 if ((++p
->xmit_last
) == NUM_XMIT_BUFFS
)
1071 netif_wake_queue(dev
);
1074 /***********************************************************
1075 * (re)start the receiver
1078 static void startrecv586(struct net_device
*dev
)
1080 struct priv
*p
= netdev_priv(dev
);
1082 wait_for_scb_cmd(dev
);
1083 wait_for_scb_cmd_ruc(dev
);
1084 writew(make16(p
->rfd_first
), &p
->scb
->rfa_offset
);
1085 writeb(RUC_START
, &p
->scb
->cmd_ruc
);
1086 ni_attn586(); /* start cmd. */
1087 wait_for_scb_cmd_ruc(dev
);
1088 /* wait for accept cmd. (no timeout!!) */
1091 static void ni52_timeout(struct net_device
*dev
)
1093 struct priv
*p
= netdev_priv(dev
);
1094 #ifndef NO_NOPCOMMANDS
1095 if (readb(&p
->scb
->cus
) & CU_ACTIVE
) { /* COMMAND-UNIT active? */
1096 netif_wake_queue(dev
);
1098 printk(KERN_ERR
"%s: strange ... timeout with CU active?!?\n",
1100 printk(KERN_ERR
"%s: X0: %04x N0: %04x N1: %04x %d\n",
1101 dev
->name
, (int)p
->xmit_cmds
[0]->cmd_status
,
1102 readw(&p
->nop_cmds
[0]->cmd_status
),
1103 readw(&p
->nop_cmds
[1]->cmd_status
),
1106 writeb(CUC_ABORT
, &p
->scb
->cmd_cuc
);
1108 wait_for_scb_cmd(dev
);
1109 writew(make16(p
->nop_cmds
[p
->nop_point
]), &p
->scb
->cbl_offset
);
1110 writeb(CUC_START
, &p
->scb
->cmd_cuc
);
1112 wait_for_scb_cmd(dev
);
1113 dev
->trans_start
= jiffies
; /* prevent tx timeout */
1119 printk(KERN_ERR
"%s: xmitter timed out, try to restart! stat: %02x\n",
1120 dev
->name
, readb(&p
->scb
->cus
));
1121 printk(KERN_ERR
"%s: command-stats: %04x %04x\n",
1123 readw(&p
->xmit_cmds
[0]->cmd_status
),
1124 readw(&p
->xmit_cmds
[1]->cmd_status
));
1125 printk(KERN_ERR
"%s: check, whether you set the right interrupt number!\n",
1131 dev
->trans_start
= jiffies
; /* prevent tx timeout */
1134 /******************************************************
1138 static netdev_tx_t
ni52_send_packet(struct sk_buff
*skb
,
1139 struct net_device
*dev
)
1142 #ifndef NO_NOPCOMMANDS
1145 struct priv
*p
= netdev_priv(dev
);
1147 if (skb
->len
> XMIT_BUFF_SIZE
) {
1148 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
);
1149 return NETDEV_TX_OK
;
1152 netif_stop_queue(dev
);
1154 memcpy_toio(p
->xmit_cbuffs
[p
->xmit_count
], skb
->data
, skb
->len
);
1156 if (len
< ETH_ZLEN
) {
1158 memset_io(p
->xmit_cbuffs
[p
->xmit_count
]+skb
->len
, 0,
1162 #if (NUM_XMIT_BUFFS == 1)
1163 # ifdef NO_NOPCOMMANDS
1166 if (readb(&p
->scb
->cus
) & CU_ACTIVE
) {
1167 printk(KERN_ERR
"%s: Hmmm .. CU is still running and we wanna send a new packet.\n", dev
->name
);
1168 printk(KERN_ERR
"%s: stat: %04x %04x\n",
1169 dev
->name
, readb(&p
->scb
->cus
),
1170 readw(&p
->xmit_cmds
[0]->cmd_status
));
1173 writew(TBD_LAST
| len
, &p
->xmit_buffs
[0]->size
);
1174 for (i
= 0; i
< 16; i
++) {
1175 writew(0, &p
->xmit_cmds
[0]->cmd_status
);
1176 wait_for_scb_cmd(dev
);
1177 if ((readb(&p
->scb
->cus
) & CU_STATUS
) == CU_SUSPEND
)
1178 writeb(CUC_RESUME
, &p
->scb
->cmd_cuc
);
1180 writew(make16(p
->xmit_cmds
[0]), &p
->scb
->cbl_offset
);
1181 writeb(CUC_START
, &p
->scb
->cmd_cuc
);
1186 wait_for_scb_cmd(dev
);
1187 /* test it, because CU sometimes doesn't start immediately */
1188 if (readb(&p
->scb
->cus
) & CU_ACTIVE
)
1190 if (readw(&p
->xmit_cmds
[0]->cmd_status
))
1193 printk(KERN_WARNING
"%s: Can't start transmit-command.\n", dev
->name
);
1196 next_nop
= (p
->nop_point
+ 1) & 0x1;
1197 writew(TBD_LAST
| len
, &p
->xmit_buffs
[0]->size
);
1198 writew(make16(p
->nop_cmds
[next_nop
]), &p
->xmit_cmds
[0]->cmd_link
);
1199 writew(make16(p
->nop_cmds
[next_nop
]),
1200 &p
->nop_cmds
[next_nop
]->cmd_link
);
1201 writew(0, &p
->xmit_cmds
[0]->cmd_status
);
1202 writew(0, &p
->nop_cmds
[next_nop
]->cmd_status
);
1204 writew(make16(p
->xmit_cmds
[0]), &p
->nop_cmds
[p
->nop_point
]->cmd_link
);
1205 p
->nop_point
= next_nop
;
1209 writew(TBD_LAST
| len
, &p
->xmit_buffs
[p
->xmit_count
]->size
);
1210 next_nop
= p
->xmit_count
+ 1
1211 if (next_nop
== NUM_XMIT_BUFFS
)
1213 writew(0, &p
->xmit_cmds
[p
->xmit_count
]->cmd_status
);
1214 /* linkpointer of xmit-command already points to next nop cmd */
1215 writew(make16(p
->nop_cmds
[next_nop
]),
1216 &p
->nop_cmds
[next_nop
]->cmd_link
);
1217 writew(0, &p
->nop_cmds
[next_nop
]->cmd_status
);
1218 writew(make16(p
->xmit_cmds
[p
->xmit_count
]),
1219 &p
->nop_cmds
[p
->xmit_count
]->cmd_link
);
1220 p
->xmit_count
= next_nop
;
1222 unsigned long flags
;
1223 spin_lock_irqsave(&p
->spinlock
);
1224 if (p
->xmit_count
!= p
->xmit_last
)
1225 netif_wake_queue(dev
);
1226 spin_unlock_irqrestore(&p
->spinlock
);
1230 return NETDEV_TX_OK
;
1233 /*******************************************
1234 * Someone wanna have the statistics
1237 static struct net_device_stats
*ni52_get_stats(struct net_device
*dev
)
1239 struct priv
*p
= netdev_priv(dev
);
1240 unsigned short crc
, aln
, rsc
, ovrn
;
1242 /* Get error-statistics from the ni82586 */
1243 crc
= readw(&p
->scb
->crc_errs
);
1244 writew(0, &p
->scb
->crc_errs
);
1245 aln
= readw(&p
->scb
->aln_errs
);
1246 writew(0, &p
->scb
->aln_errs
);
1247 rsc
= readw(&p
->scb
->rsc_errs
);
1248 writew(0, &p
->scb
->rsc_errs
);
1249 ovrn
= readw(&p
->scb
->ovrn_errs
);
1250 writew(0, &p
->scb
->ovrn_errs
);
1252 dev
->stats
.rx_crc_errors
+= crc
;
1253 dev
->stats
.rx_fifo_errors
+= ovrn
;
1254 dev
->stats
.rx_frame_errors
+= aln
;
1255 dev
->stats
.rx_dropped
+= rsc
;
1260 /********************************************************
1264 static void set_multicast_list(struct net_device
*dev
)
1266 netif_stop_queue(dev
);
1272 netif_wake_queue(dev
);
1276 static struct net_device
*dev_ni52
;
1278 module_param(io
, int, 0);
1279 module_param(irq
, int, 0);
1280 module_param(memstart
, long, 0);
1281 module_param(memend
, long, 0);
1282 MODULE_PARM_DESC(io
, "NI5210 I/O base address,required");
1283 MODULE_PARM_DESC(irq
, "NI5210 IRQ number,required");
1284 MODULE_PARM_DESC(memstart
, "NI5210 memory base address,required");
1285 MODULE_PARM_DESC(memend
, "NI5210 memory end address,required");
1287 int __init
init_module(void)
1289 if (io
<= 0x0 || !memend
|| !memstart
|| irq
< 2) {
1290 printk(KERN_ERR
"ni52: Autoprobing not allowed for modules.\n");
1291 printk(KERN_ERR
"ni52: Set symbols 'io' 'irq' 'memstart' and 'memend'\n");
1294 dev_ni52
= ni52_probe(-1);
1295 if (IS_ERR(dev_ni52
))
1296 return PTR_ERR(dev_ni52
);
1300 void __exit
cleanup_module(void)
1302 struct priv
*p
= netdev_priv(dev_ni52
);
1303 unregister_netdev(dev_ni52
);
1305 release_region(dev_ni52
->base_addr
, NI52_TOTAL_SIZE
);
1306 free_netdev(dev_ni52
);
1310 MODULE_LICENSE("GPL");