blob: 539e18ab485cd5da15f1efe17061a5600b48baad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ni5010.c: A network driver for the MiCom-Interlan NI5010 ethercard.
2 *
Andreas Mohr5b552b12006-06-30 02:25:07 -07003 * Copyright 1996,1997,2006 Jan-Pascal van Best and Andreas Mohr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
7 *
8 * The authors may be reached as:
Andreas Mohr5b552b12006-06-30 02:25:07 -07009 * janpascal@vanbest.org andi@lisas.de
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Sources:
12 * Donald Becker's "skeleton.c"
13 * Crynwr ni5010 packet driver
14 *
15 * Changes:
16 * v0.0: First test version
17 * v0.1: First working version
18 * v0.2:
19 * v0.3->v0.90: Now demand setting io and irq when loading as module
20 * 970430 v0.91: modified for Linux 2.1.14
21 * v0.92: Implemented Andreas' (better) NI5010 probe
22 * 970503 v0.93: Fixed auto-irq failure on warm reboot (JB)
23 * 970623 v1.00: First kernel version (AM)
24 * 970814 v1.01: Added detection of onboard receive buffer size (AM)
Andreas Mohr5b552b12006-06-30 02:25:07 -070025 * 060611 v1.02: slight cleanup: email addresses, driver modernization.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Bugs:
Andreas Mohr5b552b12006-06-30 02:25:07 -070027 * - not SMP-safe (no locking of I/O accesses)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * - Note that you have to patch ifconfig for the new /proc/net/dev
29 * format. It gives incorrect stats otherwise.
30 *
31 * To do:
32 * Fix all bugs :-)
33 * Move some stuff to chipset_init()
34 * Handle xmt errors other than collisions
35 * Complete merge with Andreas' driver
36 * Implement ring buffers (Is this useful? You can't squeeze
37 * too many packet in a 2k buffer!)
Andreas Mohr5b552b12006-06-30 02:25:07 -070038 * Implement DMA (Again, is this useful? Some docs say DMA is
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * slower than programmed I/O)
40 *
41 * Compile with:
42 * gcc -O2 -fomit-frame-pointer -m486 -D__KERNEL__ \
Jeff Garzik6aa20a22006-09-13 13:24:59 -040043 * -DMODULE -c ni5010.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 * Insert with e.g.:
Andreas Mohr5b552b12006-06-30 02:25:07 -070046 * insmod ni5010.ko io=0x300 irq=5
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
48
49#include <linux/module.h>
50#include <linux/kernel.h>
51#include <linux/string.h>
52#include <linux/errno.h>
53#include <linux/ioport.h>
54#include <linux/slab.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57#include <linux/init.h>
58#include <linux/bitops.h>
59#include <asm/io.h>
60#include <asm/dma.h>
61
62#include <linux/netdevice.h>
63#include <linux/etherdevice.h>
64#include <linux/skbuff.h>
65
66#include "ni5010.h"
67
Andreas Mohr5b552b12006-06-30 02:25:07 -070068static const char boardname[] = "NI5010";
69static char version[] __initdata =
70 "ni5010.c: v1.02 20060611 Jan-Pascal van Best and Andreas Mohr\n";
Jeff Garzik6aa20a22006-09-13 13:24:59 -040071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* bufsize_rcv == 0 means autoprobing */
73static unsigned int bufsize_rcv;
74
Andreas Mohr5b552b12006-06-30 02:25:07 -070075#define JUMPERED_INTERRUPTS /* IRQ line jumpered on board */
76#undef JUMPERED_DMA /* No DMA used */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#undef FULL_IODETECT /* Only detect in portlist */
78
79#ifndef FULL_IODETECT
80/* A zero-terminated list of I/O addresses to be probed. */
81static unsigned int ports[] __initdata =
82 { 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0 };
83#endif
84
85/* Use 0 for production, 1 for verification, >2 for debug */
86#ifndef NI5010_DEBUG
87#define NI5010_DEBUG 0
88#endif
89
90/* Information that needs to be kept for each board. */
91struct ni5010_local {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int o_pkt_size;
93 spinlock_t lock;
94};
95
96/* Index to functions, as function prototypes. */
97
98static int ni5010_probe1(struct net_device *dev, int ioaddr);
99static int ni5010_open(struct net_device *dev);
100static int ni5010_send_packet(struct sk_buff *skb, struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100101static irqreturn_t ni5010_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static void ni5010_rx(struct net_device *dev);
103static void ni5010_timeout(struct net_device *dev);
104static int ni5010_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static void ni5010_set_multicast_list(struct net_device *dev);
106static void reset_receiver(struct net_device *dev);
107
108static int process_xmt_interrupt(struct net_device *dev);
109#define tx_done(dev) 1
110static void hardware_send_packet(struct net_device *dev, char *buf, int length, int pad);
111static void chipset_init(struct net_device *dev, int startp);
112static void dump_packet(void *buf, int len);
113static void ni5010_show_registers(struct net_device *dev);
114
115static int io;
116static int irq;
117
118struct net_device * __init ni5010_probe(int unit)
119{
120 struct net_device *dev = alloc_etherdev(sizeof(struct ni5010_local));
121 int *port;
122 int err = 0;
123
124 if (!dev)
125 return ERR_PTR(-ENOMEM);
126
127 if (unit >= 0) {
128 sprintf(dev->name, "eth%d", unit);
129 netdev_boot_setup_check(dev);
130 io = dev->base_addr;
131 irq = dev->irq;
132 }
133
134 PRINTK2((KERN_DEBUG "%s: Entering ni5010_probe\n", dev->name));
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (io > 0x1ff) { /* Check a single specified location. */
137 err = ni5010_probe1(dev, io);
138 } else if (io != 0) { /* Don't probe at all. */
139 err = -ENXIO;
140 } else {
141#ifdef FULL_IODETECT
142 for (io=0x200; io<0x400 && ni5010_probe1(dev, io) ; io+=0x20)
143 ;
144 if (io == 0x400)
145 err = -ENODEV;
146
147#else
148 for (port = ports; *port && ni5010_probe1(dev, *port); port++)
149 ;
150 if (!*port)
151 err = -ENODEV;
152#endif /* FULL_IODETECT */
153 }
154 if (err)
155 goto out;
156 err = register_netdev(dev);
157 if (err)
158 goto out1;
159 return dev;
160out1:
161 release_region(dev->base_addr, NI5010_IO_EXTENT);
162out:
163 free_netdev(dev);
164 return ERR_PTR(err);
165}
166
167static inline int rd_port(int ioaddr)
168{
169 inb(IE_RBUF);
170 return inb(IE_SAPROM);
171}
172
173static void __init trigger_irq(int ioaddr)
174{
175 outb(0x00, EDLC_RESET); /* Clear EDLC hold RESET state */
176 outb(0x00, IE_RESET); /* Board reset */
177 outb(0x00, EDLC_XMASK); /* Disable all Xmt interrupts */
178 outb(0x00, EDLC_RMASK); /* Disable all Rcv interrupt */
179 outb(0xff, EDLC_XCLR); /* Clear all pending Xmt interrupts */
180 outb(0xff, EDLC_RCLR); /* Clear all pending Rcv interrupts */
181 /*
182 * Transmit packet mode: Ignore parity, Power xcvr,
183 * Enable loopback
184 */
185 outb(XMD_IG_PAR | XMD_T_MODE | XMD_LBC, EDLC_XMODE);
186 outb(RMD_BROADCAST, EDLC_RMODE); /* Receive normal&broadcast */
187 outb(XM_ALL, EDLC_XMASK); /* Enable all Xmt interrupts */
188 udelay(50); /* FIXME: Necessary? */
189 outb(MM_EN_XMT|MM_MUX, IE_MMODE); /* Start transmission */
190}
191
192/*
193 * This is the real probe routine. Linux has a history of friendly device
194 * probes on the ISA bus. A good device probes avoids doing writes, and
195 * verifies that the correct device exists and functions.
196 */
197
198static int __init ni5010_probe1(struct net_device *dev, int ioaddr)
199{
200 static unsigned version_printed;
201 struct ni5010_local *lp;
202 int i;
203 unsigned int data = 0;
204 int boguscount = 40;
205 int err = -ENODEV;
206
207 dev->base_addr = ioaddr;
208 dev->irq = irq;
209
210 if (!request_region(ioaddr, NI5010_IO_EXTENT, boardname))
211 return -EBUSY;
212
213 /*
214 * This is no "official" probe method, I've rather tested which
215 * probe works best with my seven NI5010 cards
216 * (they have very different serial numbers)
217 * Suggestions or failure reports are very, very welcome !
218 * But I think it is a relatively good probe method
219 * since it doesn't use any "outb"
220 * It should be nearly 100% reliable !
221 * well-known WARNING: this probe method (like many others)
222 * will hang the system if a NE2000 card region is probed !
223 *
224 * - Andreas
225 */
226
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400227 PRINTK2((KERN_DEBUG "%s: entering ni5010_probe1(%#3x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 dev->name, ioaddr));
229
230 if (inb(ioaddr+0) == 0xff)
231 goto out;
232
233 while ( (rd_port(ioaddr) & rd_port(ioaddr) & rd_port(ioaddr) &
234 rd_port(ioaddr) & rd_port(ioaddr) & rd_port(ioaddr)) != 0xff)
235 {
236 if (boguscount-- == 0)
237 goto out;
238 }
239
240 PRINTK2((KERN_DEBUG "%s: I/O #1 passed!\n", dev->name));
241
242 for (i=0; i<32; i++)
243 if ( (data = rd_port(ioaddr)) != 0xff) break;
244 if (data==0xff)
245 goto out;
246
247 PRINTK2((KERN_DEBUG "%s: I/O #2 passed!\n", dev->name));
248
249 if ((data != SA_ADDR0) || (rd_port(ioaddr) != SA_ADDR1) ||
250 (rd_port(ioaddr) != SA_ADDR2))
251 goto out;
252
253 for (i=0; i<4; i++)
254 rd_port(ioaddr);
255
256 if ( (rd_port(ioaddr) != NI5010_MAGICVAL1) ||
257 (rd_port(ioaddr) != NI5010_MAGICVAL2) )
258 goto out;
259
260 PRINTK2((KERN_DEBUG "%s: I/O #3 passed!\n", dev->name));
261
262 if (NI5010_DEBUG && version_printed++ == 0)
263 printk(KERN_INFO "%s", version);
264
265 printk("NI5010 ethercard probe at 0x%x: ", ioaddr);
266
267 dev->base_addr = ioaddr;
268
269 for (i=0; i<6; i++) {
270 outw(i, IE_GP);
Joe Perches0795af52007-10-03 17:59:30 -0700271 dev->dev_addr[i] = inb(IE_SAPROM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Johannes Berge1749612008-10-27 15:59:26 -0700273 printk("%pM ", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 PRINTK2((KERN_DEBUG "%s: I/O #4 passed!\n", dev->name));
276
Andreas Mohr5b552b12006-06-30 02:25:07 -0700277#ifdef JUMPERED_INTERRUPTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (dev->irq == 0xff)
279 ;
280 else if (dev->irq < 2) {
281 unsigned long irq_mask;
282
283 PRINTK2((KERN_DEBUG "%s: I/O #5 passed!\n", dev->name));
284
285 irq_mask = probe_irq_on();
286 trigger_irq(ioaddr);
287 mdelay(20);
288 dev->irq = probe_irq_off(irq_mask);
289
290 PRINTK2((KERN_DEBUG "%s: I/O #6 passed!\n", dev->name));
291
292 if (dev->irq == 0) {
293 err = -EAGAIN;
294 printk(KERN_WARNING "%s: no IRQ found!\n", dev->name);
295 goto out;
296 }
297 PRINTK2((KERN_DEBUG "%s: I/O #7 passed!\n", dev->name));
298 } else if (dev->irq == 2) {
299 dev->irq = 9;
300 }
Andreas Mohr5b552b12006-06-30 02:25:07 -0700301#endif /* JUMPERED_INTERRUPTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 PRINTK2((KERN_DEBUG "%s: I/O #9 passed!\n", dev->name));
303
304 /* DMA is not supported (yet?), so no use detecting it */
305 lp = netdev_priv(dev);
306
307 spin_lock_init(&lp->lock);
308
309 PRINTK2((KERN_DEBUG "%s: I/O #10 passed!\n", dev->name));
310
311/* get the size of the onboard receive buffer
312 * higher addresses than bufsize are wrapped into real buffer
313 * i.e. data for offs. 0x801 is written to 0x1 with a 2K onboard buffer
314 */
315 if (!bufsize_rcv) {
316 outb(1, IE_MMODE); /* Put Rcv buffer on system bus */
317 outw(0, IE_GP); /* Point GP at start of packet */
318 outb(0, IE_RBUF); /* set buffer byte 0 to 0 */
319 for (i = 1; i < 0xff; i++) {
320 outw(i << 8, IE_GP); /* Point GP at packet size to be tested */
321 outb(i, IE_RBUF);
322 outw(0x0, IE_GP); /* Point GP at start of packet */
323 data = inb(IE_RBUF);
324 if (data == i) break;
325 }
326 bufsize_rcv = i << 8;
327 outw(0, IE_GP); /* Point GP at start of packet */
328 outb(0, IE_RBUF); /* set buffer byte 0 to 0 again */
329 }
Andreas Mohr5b552b12006-06-30 02:25:07 -0700330 printk("-> bufsize rcv/xmt=%d/%d\n", bufsize_rcv, NI5010_BUFSIZE);
Wang Chen4cf16532008-11-12 23:38:14 -0800331 memset(netdev_priv(dev), 0, sizeof(struct ni5010_local));
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 dev->open = ni5010_open;
334 dev->stop = ni5010_close;
335 dev->hard_start_xmit = ni5010_send_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dev->set_multicast_list = ni5010_set_multicast_list;
337 dev->tx_timeout = ni5010_timeout;
338 dev->watchdog_timeo = HZ/20;
339
340 dev->flags &= ~IFF_MULTICAST; /* Multicast doesn't work */
341
342 /* Shut up the ni5010 */
343 outb(0, EDLC_RMASK); /* Mask all receive interrupts */
344 outb(0, EDLC_XMASK); /* Mask all xmit interrupts */
345 outb(0xff, EDLC_RCLR); /* Kill all pending rcv interrupts */
346 outb(0xff, EDLC_XCLR); /* Kill all pending xmt interrupts */
347
348 printk(KERN_INFO "%s: NI5010 found at 0x%x, using IRQ %d", dev->name, ioaddr, dev->irq);
Andreas Mohr5b552b12006-06-30 02:25:07 -0700349 if (dev->dma)
350 printk(" & DMA %d", dev->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 printk(".\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353out:
354 release_region(dev->base_addr, NI5010_IO_EXTENT);
355 return err;
356}
357
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400358/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * Open/initialize the board. This is called (in the current kernel)
360 * sometime after booting when the 'ifconfig' program is run.
361 *
362 * This routine should set everything up anew at each open, even
363 * registers that "should" only need to be set once at boot, so that
Andreas Mohr5b552b12006-06-30 02:25:07 -0700364 * there is a non-reboot way to recover if something goes wrong.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static int ni5010_open(struct net_device *dev)
368{
369 int ioaddr = dev->base_addr;
370 int i;
371
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400372 PRINTK2((KERN_DEBUG "%s: entering ni5010_open()\n", dev->name));
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (request_irq(dev->irq, &ni5010_interrupt, 0, boardname, dev)) {
375 printk(KERN_WARNING "%s: Cannot get irq %#2x\n", dev->name, dev->irq);
376 return -EAGAIN;
377 }
378 PRINTK3((KERN_DEBUG "%s: passed open() #1\n", dev->name));
379 /*
380 * Always allocate the DMA channel after the IRQ,
381 * and clean up on failure.
382 */
Andreas Mohr5b552b12006-06-30 02:25:07 -0700383#ifdef JUMPERED_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (request_dma(dev->dma, cardname)) {
385 printk(KERN_WARNING "%s: Cannot get dma %#2x\n", dev->name, dev->dma);
386 free_irq(dev->irq, NULL);
387 return -EAGAIN;
388 }
Andreas Mohr5b552b12006-06-30 02:25:07 -0700389#endif /* JUMPERED_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 PRINTK3((KERN_DEBUG "%s: passed open() #2\n", dev->name));
392 /* Reset the hardware here. Don't forget to set the station address. */
393
394 outb(RS_RESET, EDLC_RESET); /* Hold up EDLC_RESET while configing board */
395 outb(0, IE_RESET); /* Hardware reset of ni5010 board */
396 outb(XMD_LBC, EDLC_XMODE); /* Only loopback xmits */
397
398 PRINTK3((KERN_DEBUG "%s: passed open() #3\n", dev->name));
399 /* Set the station address */
400 for(i = 0;i < 6; i++) {
401 outb(dev->dev_addr[i], EDLC_ADDR + i);
402 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400403
404 PRINTK3((KERN_DEBUG "%s: Initialising ni5010\n", dev->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 outb(0, EDLC_XMASK); /* No xmit interrupts for now */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400406 outb(XMD_IG_PAR | XMD_T_MODE | XMD_LBC, EDLC_XMODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* Normal packet xmit mode */
408 outb(0xff, EDLC_XCLR); /* Clear all pending xmit interrupts */
409 outb(RMD_BROADCAST, EDLC_RMODE);
410 /* Receive broadcast and normal packets */
411 reset_receiver(dev); /* Ready ni5010 for receiving packets */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 outb(0, EDLC_RESET); /* Un-reset the ni5010 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 netif_start_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400416
417 if (NI5010_DEBUG) ni5010_show_registers(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 PRINTK((KERN_DEBUG "%s: open successful\n", dev->name));
420 return 0;
421}
422
423static void reset_receiver(struct net_device *dev)
424{
425 int ioaddr = dev->base_addr;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 PRINTK3((KERN_DEBUG "%s: resetting receiver\n", dev->name));
428 outw(0, IE_GP); /* Receive packet at start of buffer */
429 outb(0xff, EDLC_RCLR); /* Clear all pending rcv interrupts */
430 outb(0, IE_MMODE); /* Put EDLC to rcv buffer */
431 outb(MM_EN_RCV, IE_MMODE); /* Enable rcv */
432 outb(0xff, EDLC_RMASK); /* Enable all rcv interrupts */
433}
434
435static void ni5010_timeout(struct net_device *dev)
436{
437 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
438 tx_done(dev) ? "IRQ conflict" : "network cable problem");
439 /* Try to restart the adaptor. */
440 /* FIXME: Give it a real kick here */
441 chipset_init(dev, 1);
442 dev->trans_start = jiffies;
443 netif_wake_queue(dev);
444}
445
446static int ni5010_send_packet(struct sk_buff *skb, struct net_device *dev)
447{
448 int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
449
450 PRINTK2((KERN_DEBUG "%s: entering ni5010_send_packet\n", dev->name));
451
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400452 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * Block sending
454 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 netif_stop_queue(dev);
457 hardware_send_packet(dev, (unsigned char *)skb->data, skb->len, length-skb->len);
458 dev->trans_start = jiffies;
459 dev_kfree_skb (skb);
460 return 0;
461}
462
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400463/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * The typical workload of the driver:
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400465 * Handle the network interface interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
David Howells7d12e782006-10-05 14:55:46 +0100467static irqreturn_t ni5010_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct net_device *dev = dev_id;
470 struct ni5010_local *lp;
471 int ioaddr, status;
472 int xmit_was_error = 0;
473
474 PRINTK2((KERN_DEBUG "%s: entering ni5010_interrupt\n", dev->name));
475
476 ioaddr = dev->base_addr;
477 lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 spin_lock(&lp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400480 status = inb(IE_ISTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 PRINTK3((KERN_DEBUG "%s: IE_ISTAT = %#02x\n", dev->name, status));
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if ((status & IS_R_INT) == 0) ni5010_rx(dev);
484
485 if ((status & IS_X_INT) == 0) {
486 xmit_was_error = process_xmt_interrupt(dev);
487 }
488
489 if ((status & IS_DMA_INT) == 0) {
490 PRINTK((KERN_DEBUG "%s: DMA complete (?)\n", dev->name));
491 outb(0, IE_DMA_RST); /* Reset DMA int */
492 }
493
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400494 if (!xmit_was_error)
495 reset_receiver(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 spin_unlock(&lp->lock);
497 return IRQ_HANDLED;
498}
499
500
501static void dump_packet(void *buf, int len)
502{
503 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 printk(KERN_DEBUG "Packet length = %#4x\n", len);
506 for (i = 0; i < len; i++){
507 if (i % 16 == 0) printk(KERN_DEBUG "%#4.4x", i);
508 if (i % 2 == 0) printk(" ");
509 printk("%2.2x", ((unsigned char *)buf)[i]);
510 if (i % 16 == 15) printk("\n");
511 }
512 printk("\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return;
515}
516
517/* We have a good packet, get it out of the buffer. */
518static void ni5010_rx(struct net_device *dev)
519{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int ioaddr = dev->base_addr;
521 unsigned char rcv_stat;
522 struct sk_buff *skb;
523 int i_pkt_size;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400524
525 PRINTK2((KERN_DEBUG "%s: entering ni5010_rx()\n", dev->name));
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 rcv_stat = inb(EDLC_RSTAT);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400528 PRINTK3((KERN_DEBUG "%s: EDLC_RSTAT = %#2x\n", dev->name, rcv_stat));
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if ( (rcv_stat & RS_VALID_BITS) != RS_PKT_OK) {
531 PRINTK((KERN_INFO "%s: receive error.\n", dev->name));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700532 dev->stats.rx_errors++;
533 if (rcv_stat & RS_RUNT) dev->stats.rx_length_errors++;
534 if (rcv_stat & RS_ALIGN) dev->stats.rx_frame_errors++;
535 if (rcv_stat & RS_CRC_ERR) dev->stats.rx_crc_errors++;
536 if (rcv_stat & RS_OFLW) dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 outb(0xff, EDLC_RCLR); /* Clear the interrupt */
538 return;
539 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 outb(0xff, EDLC_RCLR); /* Clear the interrupt */
542
543 i_pkt_size = inw(IE_RCNT);
544 if (i_pkt_size > ETH_FRAME_LEN || i_pkt_size < 10 ) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400545 PRINTK((KERN_DEBUG "%s: Packet size error, packet size = %#4.4x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 dev->name, i_pkt_size));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700547 dev->stats.rx_errors++;
548 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return;
550 }
551
552 /* Malloc up new buffer. */
553 skb = dev_alloc_skb(i_pkt_size + 3);
554 if (skb == NULL) {
555 printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700556 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return;
558 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 skb_reserve(skb, 2);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Read packet into buffer */
563 outb(MM_MUX, IE_MMODE); /* Rcv buffer to system bus */
564 outw(0, IE_GP); /* Seek to beginning of packet */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400565 insb(IE_RBUF, skb_put(skb, i_pkt_size), i_pkt_size);
566
567 if (NI5010_DEBUG >= 4)
568 dump_packet(skb->data, skb->len);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 skb->protocol = eth_type_trans(skb,dev);
571 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700572 dev->stats.rx_packets++;
573 dev->stats.rx_bytes += i_pkt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400575 PRINTK2((KERN_DEBUG "%s: Received packet, size=%#4.4x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 dev->name, i_pkt_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579static int process_xmt_interrupt(struct net_device *dev)
580{
581 struct ni5010_local *lp = netdev_priv(dev);
582 int ioaddr = dev->base_addr;
583 int xmit_stat;
584
585 PRINTK2((KERN_DEBUG "%s: entering process_xmt_interrupt\n", dev->name));
586
587 xmit_stat = inb(EDLC_XSTAT);
588 PRINTK3((KERN_DEBUG "%s: EDLC_XSTAT = %2.2x\n", dev->name, xmit_stat));
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 outb(0, EDLC_XMASK); /* Disable xmit IRQ's */
591 outb(0xff, EDLC_XCLR); /* Clear all pending xmit IRQ's */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (xmit_stat & XS_COLL){
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400594 PRINTK((KERN_DEBUG "%s: collision detected, retransmitting\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 dev->name));
596 outw(NI5010_BUFSIZE - lp->o_pkt_size, IE_GP);
597 /* outb(0, IE_MMODE); */ /* xmt buf on sysbus FIXME: needed ? */
598 outb(MM_EN_XMT | MM_MUX, IE_MMODE);
599 outb(XM_ALL, EDLC_XMASK); /* Enable xmt IRQ's */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700600 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 1;
602 }
603
604 /* FIXME: handle other xmt error conditions */
605
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700606 dev->stats.tx_packets++;
607 dev->stats.tx_bytes += lp->o_pkt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 netif_wake_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400609
610 PRINTK2((KERN_DEBUG "%s: sent packet, size=%#4.4x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 dev->name, lp->o_pkt_size));
612
613 return 0;
614}
615
616/* The inverse routine to ni5010_open(). */
617static int ni5010_close(struct net_device *dev)
618{
619 int ioaddr = dev->base_addr;
620
621 PRINTK2((KERN_DEBUG "%s: entering ni5010_close\n", dev->name));
Andreas Mohr5b552b12006-06-30 02:25:07 -0700622#ifdef JUMPERED_INTERRUPTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 free_irq(dev->irq, NULL);
624#endif
625 /* Put card in held-RESET state */
626 outb(0, IE_MMODE);
627 outb(RS_RESET, EDLC_RESET);
628
629 netif_stop_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 PRINTK((KERN_DEBUG "%s: %s closed down\n", dev->name, boardname));
632 return 0;
633
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/* Set or clear the multicast filter for this adaptor.
637 num_addrs == -1 Promiscuous mode, receive all packets
638 num_addrs == 0 Normal mode, clear multicast list
639 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
640 best-effort filtering.
641*/
642static void ni5010_set_multicast_list(struct net_device *dev)
643{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400644 short ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 PRINTK2((KERN_DEBUG "%s: entering set_multicast_list\n", dev->name));
647
YOSHIFUJI Hideaki / 吉藤英明b947dd42007-07-17 13:45:50 +0900648 if (dev->flags&IFF_PROMISC || dev->flags&IFF_ALLMULTI || dev->mc_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 outb(RMD_PROMISC, EDLC_RMODE); /* Enable promiscuous mode */
650 PRINTK((KERN_DEBUG "%s: Entering promiscuous mode\n", dev->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 } else {
652 PRINTK((KERN_DEBUG "%s: Entering broadcast mode\n", dev->name));
653 outb(RMD_BROADCAST, EDLC_RMODE); /* Disable promiscuous mode, use normal mode */
654 }
655}
656
657static void hardware_send_packet(struct net_device *dev, char *buf, int length, int pad)
658{
659 struct ni5010_local *lp = netdev_priv(dev);
660 int ioaddr = dev->base_addr;
661 unsigned long flags;
662 unsigned int buf_offs;
663
664 PRINTK2((KERN_DEBUG "%s: entering hardware_send_packet\n", dev->name));
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (length > ETH_FRAME_LEN) {
667 PRINTK((KERN_WARNING "%s: packet too large, not possible\n",
668 dev->name));
669 return;
670 }
671
672 if (NI5010_DEBUG) ni5010_show_registers(dev);
673
674 if (inb(IE_ISTAT) & IS_EN_XMT) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400675 PRINTK((KERN_WARNING "%s: sending packet while already transmitting, not possible\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 dev->name));
677 return;
678 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (NI5010_DEBUG > 3) dump_packet(buf, length);
681
682 buf_offs = NI5010_BUFSIZE - length - pad;
683
684 spin_lock_irqsave(&lp->lock, flags);
685 lp->o_pkt_size = length + pad;
686
687 outb(0, EDLC_RMASK); /* Mask all receive interrupts */
688 outb(0, IE_MMODE); /* Put Xmit buffer on system bus */
689 outb(0xff, EDLC_RCLR); /* Clear out pending rcv interrupts */
690
691 outw(buf_offs, IE_GP); /* Point GP at start of packet */
692 outsb(IE_XBUF, buf, length); /* Put data in buffer */
693 while(pad--)
694 outb(0, IE_XBUF);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 outw(buf_offs, IE_GP); /* Rewrite where packet starts */
697
698 /* should work without that outb() (Crynwr used it) */
699 /*outb(MM_MUX, IE_MMODE);*/ /* Xmt buffer to EDLC bus */
700 outb(MM_EN_XMT | MM_MUX, IE_MMODE); /* Begin transmission */
701 outb(XM_ALL, EDLC_XMASK); /* Cause interrupt after completion or fail */
702
703 spin_unlock_irqrestore(&lp->lock, flags);
704
705 netif_wake_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400706
707 if (NI5010_DEBUG) ni5010_show_registers(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static void chipset_init(struct net_device *dev, int startp)
711{
712 /* FIXME: Move some stuff here */
713 PRINTK3((KERN_DEBUG "%s: doing NOTHING in chipset_init\n", dev->name));
714}
715
716static void ni5010_show_registers(struct net_device *dev)
717{
718 int ioaddr = dev->base_addr;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 PRINTK3((KERN_DEBUG "%s: XSTAT %#2.2x\n", dev->name, inb(EDLC_XSTAT)));
721 PRINTK3((KERN_DEBUG "%s: XMASK %#2.2x\n", dev->name, inb(EDLC_XMASK)));
722 PRINTK3((KERN_DEBUG "%s: RSTAT %#2.2x\n", dev->name, inb(EDLC_RSTAT)));
723 PRINTK3((KERN_DEBUG "%s: RMASK %#2.2x\n", dev->name, inb(EDLC_RMASK)));
724 PRINTK3((KERN_DEBUG "%s: RMODE %#2.2x\n", dev->name, inb(EDLC_RMODE)));
725 PRINTK3((KERN_DEBUG "%s: XMODE %#2.2x\n", dev->name, inb(EDLC_XMODE)));
726 PRINTK3((KERN_DEBUG "%s: ISTAT %#2.2x\n", dev->name, inb(IE_ISTAT)));
727}
728
729#ifdef MODULE
730static struct net_device *dev_ni5010;
731
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800732module_param(io, int, 0);
733module_param(irq, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734MODULE_PARM_DESC(io, "ni5010 I/O base address");
735MODULE_PARM_DESC(irq, "ni5010 IRQ number");
736
Andreas Mohr5b552b12006-06-30 02:25:07 -0700737static int __init ni5010_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 PRINTK2((KERN_DEBUG "%s: entering init_module\n", boardname));
740 /*
741 if(io <= 0 || irq == 0){
742 printk(KERN_WARNING "%s: Autoprobing not allowed for modules.\n", boardname);
743 printk(KERN_WARNING "%s: Set symbols 'io' and 'irq'\n", boardname);
744 return -EINVAL;
745 }
746 */
747 if (io <= 0){
748 printk(KERN_WARNING "%s: Autoprobing for modules is hazardous, trying anyway..\n", boardname);
749 }
750
751 PRINTK2((KERN_DEBUG "%s: init_module irq=%#2x, io=%#3x\n", boardname, irq, io));
752 dev_ni5010 = ni5010_probe(-1);
753 if (IS_ERR(dev_ni5010))
754 return PTR_ERR(dev_ni5010);
755 return 0;
756}
757
Andreas Mohr5b552b12006-06-30 02:25:07 -0700758static void __exit ni5010_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 PRINTK2((KERN_DEBUG "%s: entering cleanup_module\n", boardname));
761 unregister_netdev(dev_ni5010);
762 release_region(dev_ni5010->base_addr, NI5010_IO_EXTENT);
763 free_netdev(dev_ni5010);
764}
Andreas Mohr5b552b12006-06-30 02:25:07 -0700765module_init(ni5010_init_module);
766module_exit(ni5010_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#endif /* MODULE */
768MODULE_LICENSE("GPL");