blob: 3892330f244a0e65b6a96bf3be8e2b72236f6e79 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/interrupt.h>
55#include <linux/delay.h>
56#include <linux/init.h>
57#include <linux/bitops.h>
58#include <asm/io.h>
59#include <asm/dma.h>
60
61#include <linux/netdevice.h>
62#include <linux/etherdevice.h>
63#include <linux/skbuff.h>
64
65#include "ni5010.h"
66
Andreas Mohr5b552b12006-06-30 02:25:07 -070067static const char boardname[] = "NI5010";
68static char version[] __initdata =
69 "ni5010.c: v1.02 20060611 Jan-Pascal van Best and Andreas Mohr\n";
Jeff Garzik6aa20a22006-09-13 13:24:59 -040070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* bufsize_rcv == 0 means autoprobing */
72static unsigned int bufsize_rcv;
73
Andreas Mohr5b552b12006-06-30 02:25:07 -070074#define JUMPERED_INTERRUPTS /* IRQ line jumpered on board */
75#undef JUMPERED_DMA /* No DMA used */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#undef FULL_IODETECT /* Only detect in portlist */
77
78#ifndef FULL_IODETECT
79/* A zero-terminated list of I/O addresses to be probed. */
80static unsigned int ports[] __initdata =
81 { 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0 };
82#endif
83
84/* Use 0 for production, 1 for verification, >2 for debug */
85#ifndef NI5010_DEBUG
86#define NI5010_DEBUG 0
87#endif
88
89/* Information that needs to be kept for each board. */
90struct ni5010_local {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int o_pkt_size;
92 spinlock_t lock;
93};
94
95/* Index to functions, as function prototypes. */
96
97static int ni5010_probe1(struct net_device *dev, int ioaddr);
98static int ni5010_open(struct net_device *dev);
99static int ni5010_send_packet(struct sk_buff *skb, struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100100static irqreturn_t ni5010_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static void ni5010_rx(struct net_device *dev);
102static void ni5010_timeout(struct net_device *dev);
103static int ni5010_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static void ni5010_set_multicast_list(struct net_device *dev);
105static void reset_receiver(struct net_device *dev);
106
107static int process_xmt_interrupt(struct net_device *dev);
108#define tx_done(dev) 1
109static void hardware_send_packet(struct net_device *dev, char *buf, int length, int pad);
110static void chipset_init(struct net_device *dev, int startp);
111static void dump_packet(void *buf, int len);
112static void ni5010_show_registers(struct net_device *dev);
113
114static int io;
115static int irq;
116
117struct net_device * __init ni5010_probe(int unit)
118{
119 struct net_device *dev = alloc_etherdev(sizeof(struct ni5010_local));
120 int *port;
121 int err = 0;
122
123 if (!dev)
124 return ERR_PTR(-ENOMEM);
125
126 if (unit >= 0) {
127 sprintf(dev->name, "eth%d", unit);
128 netdev_boot_setup_check(dev);
129 io = dev->base_addr;
130 irq = dev->irq;
131 }
132
133 PRINTK2((KERN_DEBUG "%s: Entering ni5010_probe\n", dev->name));
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (io > 0x1ff) { /* Check a single specified location. */
136 err = ni5010_probe1(dev, io);
137 } else if (io != 0) { /* Don't probe at all. */
138 err = -ENXIO;
139 } else {
140#ifdef FULL_IODETECT
141 for (io=0x200; io<0x400 && ni5010_probe1(dev, io) ; io+=0x20)
142 ;
143 if (io == 0x400)
144 err = -ENODEV;
145
146#else
147 for (port = ports; *port && ni5010_probe1(dev, *port); port++)
148 ;
149 if (!*port)
150 err = -ENODEV;
151#endif /* FULL_IODETECT */
152 }
153 if (err)
154 goto out;
155 err = register_netdev(dev);
156 if (err)
157 goto out1;
158 return dev;
159out1:
160 release_region(dev->base_addr, NI5010_IO_EXTENT);
161out:
162 free_netdev(dev);
163 return ERR_PTR(err);
164}
165
166static inline int rd_port(int ioaddr)
167{
168 inb(IE_RBUF);
169 return inb(IE_SAPROM);
170}
171
172static void __init trigger_irq(int ioaddr)
173{
174 outb(0x00, EDLC_RESET); /* Clear EDLC hold RESET state */
175 outb(0x00, IE_RESET); /* Board reset */
176 outb(0x00, EDLC_XMASK); /* Disable all Xmt interrupts */
177 outb(0x00, EDLC_RMASK); /* Disable all Rcv interrupt */
178 outb(0xff, EDLC_XCLR); /* Clear all pending Xmt interrupts */
179 outb(0xff, EDLC_RCLR); /* Clear all pending Rcv interrupts */
180 /*
181 * Transmit packet mode: Ignore parity, Power xcvr,
182 * Enable loopback
183 */
184 outb(XMD_IG_PAR | XMD_T_MODE | XMD_LBC, EDLC_XMODE);
185 outb(RMD_BROADCAST, EDLC_RMODE); /* Receive normal&broadcast */
186 outb(XM_ALL, EDLC_XMASK); /* Enable all Xmt interrupts */
187 udelay(50); /* FIXME: Necessary? */
188 outb(MM_EN_XMT|MM_MUX, IE_MMODE); /* Start transmission */
189}
190
Alexander Beregalov79f1bc02009-03-28 23:37:27 -0700191static const struct net_device_ops ni5010_netdev_ops = {
192 .ndo_open = ni5010_open,
193 .ndo_stop = ni5010_close,
194 .ndo_start_xmit = ni5010_send_packet,
195 .ndo_set_multicast_list = ni5010_set_multicast_list,
196 .ndo_tx_timeout = ni5010_timeout,
197 .ndo_validate_addr = eth_validate_addr,
198 .ndo_set_mac_address = eth_mac_addr,
199 .ndo_change_mtu = eth_change_mtu,
200};
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
203 * This is the real probe routine. Linux has a history of friendly device
204 * probes on the ISA bus. A good device probes avoids doing writes, and
205 * verifies that the correct device exists and functions.
206 */
207
208static int __init ni5010_probe1(struct net_device *dev, int ioaddr)
209{
210 static unsigned version_printed;
211 struct ni5010_local *lp;
212 int i;
213 unsigned int data = 0;
214 int boguscount = 40;
215 int err = -ENODEV;
216
217 dev->base_addr = ioaddr;
218 dev->irq = irq;
219
220 if (!request_region(ioaddr, NI5010_IO_EXTENT, boardname))
221 return -EBUSY;
222
223 /*
224 * This is no "official" probe method, I've rather tested which
225 * probe works best with my seven NI5010 cards
226 * (they have very different serial numbers)
227 * Suggestions or failure reports are very, very welcome !
228 * But I think it is a relatively good probe method
229 * since it doesn't use any "outb"
230 * It should be nearly 100% reliable !
231 * well-known WARNING: this probe method (like many others)
232 * will hang the system if a NE2000 card region is probed !
233 *
234 * - Andreas
235 */
236
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400237 PRINTK2((KERN_DEBUG "%s: entering ni5010_probe1(%#3x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 dev->name, ioaddr));
239
240 if (inb(ioaddr+0) == 0xff)
241 goto out;
242
243 while ( (rd_port(ioaddr) & rd_port(ioaddr) & rd_port(ioaddr) &
244 rd_port(ioaddr) & rd_port(ioaddr) & rd_port(ioaddr)) != 0xff)
245 {
246 if (boguscount-- == 0)
247 goto out;
248 }
249
250 PRINTK2((KERN_DEBUG "%s: I/O #1 passed!\n", dev->name));
251
252 for (i=0; i<32; i++)
253 if ( (data = rd_port(ioaddr)) != 0xff) break;
254 if (data==0xff)
255 goto out;
256
257 PRINTK2((KERN_DEBUG "%s: I/O #2 passed!\n", dev->name));
258
259 if ((data != SA_ADDR0) || (rd_port(ioaddr) != SA_ADDR1) ||
260 (rd_port(ioaddr) != SA_ADDR2))
261 goto out;
262
263 for (i=0; i<4; i++)
264 rd_port(ioaddr);
265
266 if ( (rd_port(ioaddr) != NI5010_MAGICVAL1) ||
267 (rd_port(ioaddr) != NI5010_MAGICVAL2) )
268 goto out;
269
270 PRINTK2((KERN_DEBUG "%s: I/O #3 passed!\n", dev->name));
271
272 if (NI5010_DEBUG && version_printed++ == 0)
273 printk(KERN_INFO "%s", version);
274
275 printk("NI5010 ethercard probe at 0x%x: ", ioaddr);
276
277 dev->base_addr = ioaddr;
278
279 for (i=0; i<6; i++) {
280 outw(i, IE_GP);
Joe Perches0795af52007-10-03 17:59:30 -0700281 dev->dev_addr[i] = inb(IE_SAPROM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Johannes Berge1749612008-10-27 15:59:26 -0700283 printk("%pM ", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 PRINTK2((KERN_DEBUG "%s: I/O #4 passed!\n", dev->name));
286
Andreas Mohr5b552b12006-06-30 02:25:07 -0700287#ifdef JUMPERED_INTERRUPTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (dev->irq == 0xff)
289 ;
290 else if (dev->irq < 2) {
291 unsigned long irq_mask;
292
293 PRINTK2((KERN_DEBUG "%s: I/O #5 passed!\n", dev->name));
294
295 irq_mask = probe_irq_on();
296 trigger_irq(ioaddr);
297 mdelay(20);
298 dev->irq = probe_irq_off(irq_mask);
299
300 PRINTK2((KERN_DEBUG "%s: I/O #6 passed!\n", dev->name));
301
302 if (dev->irq == 0) {
303 err = -EAGAIN;
304 printk(KERN_WARNING "%s: no IRQ found!\n", dev->name);
305 goto out;
306 }
307 PRINTK2((KERN_DEBUG "%s: I/O #7 passed!\n", dev->name));
308 } else if (dev->irq == 2) {
309 dev->irq = 9;
310 }
Andreas Mohr5b552b12006-06-30 02:25:07 -0700311#endif /* JUMPERED_INTERRUPTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 PRINTK2((KERN_DEBUG "%s: I/O #9 passed!\n", dev->name));
313
314 /* DMA is not supported (yet?), so no use detecting it */
315 lp = netdev_priv(dev);
316
317 spin_lock_init(&lp->lock);
318
319 PRINTK2((KERN_DEBUG "%s: I/O #10 passed!\n", dev->name));
320
321/* get the size of the onboard receive buffer
322 * higher addresses than bufsize are wrapped into real buffer
323 * i.e. data for offs. 0x801 is written to 0x1 with a 2K onboard buffer
324 */
325 if (!bufsize_rcv) {
326 outb(1, IE_MMODE); /* Put Rcv buffer on system bus */
327 outw(0, IE_GP); /* Point GP at start of packet */
328 outb(0, IE_RBUF); /* set buffer byte 0 to 0 */
329 for (i = 1; i < 0xff; i++) {
330 outw(i << 8, IE_GP); /* Point GP at packet size to be tested */
331 outb(i, IE_RBUF);
332 outw(0x0, IE_GP); /* Point GP at start of packet */
333 data = inb(IE_RBUF);
334 if (data == i) break;
335 }
336 bufsize_rcv = i << 8;
337 outw(0, IE_GP); /* Point GP at start of packet */
338 outb(0, IE_RBUF); /* set buffer byte 0 to 0 again */
339 }
Andreas Mohr5b552b12006-06-30 02:25:07 -0700340 printk("-> bufsize rcv/xmt=%d/%d\n", bufsize_rcv, NI5010_BUFSIZE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400341
Alexander Beregalov79f1bc02009-03-28 23:37:27 -0700342 dev->netdev_ops = &ni5010_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 dev->watchdog_timeo = HZ/20;
344
345 dev->flags &= ~IFF_MULTICAST; /* Multicast doesn't work */
346
347 /* Shut up the ni5010 */
348 outb(0, EDLC_RMASK); /* Mask all receive interrupts */
349 outb(0, EDLC_XMASK); /* Mask all xmit interrupts */
350 outb(0xff, EDLC_RCLR); /* Kill all pending rcv interrupts */
351 outb(0xff, EDLC_XCLR); /* Kill all pending xmt interrupts */
352
353 printk(KERN_INFO "%s: NI5010 found at 0x%x, using IRQ %d", dev->name, ioaddr, dev->irq);
Andreas Mohr5b552b12006-06-30 02:25:07 -0700354 if (dev->dma)
355 printk(" & DMA %d", dev->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 printk(".\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358out:
359 release_region(dev->base_addr, NI5010_IO_EXTENT);
360 return err;
361}
362
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * Open/initialize the board. This is called (in the current kernel)
365 * sometime after booting when the 'ifconfig' program is run.
366 *
367 * This routine should set everything up anew at each open, even
368 * registers that "should" only need to be set once at boot, so that
Andreas Mohr5b552b12006-06-30 02:25:07 -0700369 * there is a non-reboot way to recover if something goes wrong.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static int ni5010_open(struct net_device *dev)
373{
374 int ioaddr = dev->base_addr;
375 int i;
376
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400377 PRINTK2((KERN_DEBUG "%s: entering ni5010_open()\n", dev->name));
378
Joe Perchesa0607fd2009-11-18 23:29:17 -0800379 if (request_irq(dev->irq, ni5010_interrupt, 0, boardname, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 printk(KERN_WARNING "%s: Cannot get irq %#2x\n", dev->name, dev->irq);
381 return -EAGAIN;
382 }
383 PRINTK3((KERN_DEBUG "%s: passed open() #1\n", dev->name));
384 /*
385 * Always allocate the DMA channel after the IRQ,
386 * and clean up on failure.
387 */
Andreas Mohr5b552b12006-06-30 02:25:07 -0700388#ifdef JUMPERED_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (request_dma(dev->dma, cardname)) {
390 printk(KERN_WARNING "%s: Cannot get dma %#2x\n", dev->name, dev->dma);
391 free_irq(dev->irq, NULL);
392 return -EAGAIN;
393 }
Andreas Mohr5b552b12006-06-30 02:25:07 -0700394#endif /* JUMPERED_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 PRINTK3((KERN_DEBUG "%s: passed open() #2\n", dev->name));
397 /* Reset the hardware here. Don't forget to set the station address. */
398
399 outb(RS_RESET, EDLC_RESET); /* Hold up EDLC_RESET while configing board */
400 outb(0, IE_RESET); /* Hardware reset of ni5010 board */
401 outb(XMD_LBC, EDLC_XMODE); /* Only loopback xmits */
402
403 PRINTK3((KERN_DEBUG "%s: passed open() #3\n", dev->name));
404 /* Set the station address */
405 for(i = 0;i < 6; i++) {
406 outb(dev->dev_addr[i], EDLC_ADDR + i);
407 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400408
409 PRINTK3((KERN_DEBUG "%s: Initialising ni5010\n", dev->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 outb(0, EDLC_XMASK); /* No xmit interrupts for now */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400411 outb(XMD_IG_PAR | XMD_T_MODE | XMD_LBC, EDLC_XMODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Normal packet xmit mode */
413 outb(0xff, EDLC_XCLR); /* Clear all pending xmit interrupts */
414 outb(RMD_BROADCAST, EDLC_RMODE);
415 /* Receive broadcast and normal packets */
416 reset_receiver(dev); /* Ready ni5010 for receiving packets */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 outb(0, EDLC_RESET); /* Un-reset the ni5010 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 netif_start_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400421
422 if (NI5010_DEBUG) ni5010_show_registers(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 PRINTK((KERN_DEBUG "%s: open successful\n", dev->name));
425 return 0;
426}
427
428static void reset_receiver(struct net_device *dev)
429{
430 int ioaddr = dev->base_addr;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 PRINTK3((KERN_DEBUG "%s: resetting receiver\n", dev->name));
433 outw(0, IE_GP); /* Receive packet at start of buffer */
434 outb(0xff, EDLC_RCLR); /* Clear all pending rcv interrupts */
435 outb(0, IE_MMODE); /* Put EDLC to rcv buffer */
436 outb(MM_EN_RCV, IE_MMODE); /* Enable rcv */
437 outb(0xff, EDLC_RMASK); /* Enable all rcv interrupts */
438}
439
440static void ni5010_timeout(struct net_device *dev)
441{
442 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
443 tx_done(dev) ? "IRQ conflict" : "network cable problem");
444 /* Try to restart the adaptor. */
445 /* FIXME: Give it a real kick here */
446 chipset_init(dev, 1);
447 dev->trans_start = jiffies;
448 netif_wake_queue(dev);
449}
450
451static int ni5010_send_packet(struct sk_buff *skb, struct net_device *dev)
452{
453 int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
454
455 PRINTK2((KERN_DEBUG "%s: entering ni5010_send_packet\n", dev->name));
456
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400457 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * Block sending
459 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 netif_stop_queue(dev);
462 hardware_send_packet(dev, (unsigned char *)skb->data, skb->len, length-skb->len);
463 dev->trans_start = jiffies;
464 dev_kfree_skb (skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000465 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400468/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * The typical workload of the driver:
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400470 * Handle the network interface interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
David Howells7d12e782006-10-05 14:55:46 +0100472static irqreturn_t ni5010_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct net_device *dev = dev_id;
475 struct ni5010_local *lp;
476 int ioaddr, status;
477 int xmit_was_error = 0;
478
479 PRINTK2((KERN_DEBUG "%s: entering ni5010_interrupt\n", dev->name));
480
481 ioaddr = dev->base_addr;
482 lp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 spin_lock(&lp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400485 status = inb(IE_ISTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 PRINTK3((KERN_DEBUG "%s: IE_ISTAT = %#02x\n", dev->name, status));
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if ((status & IS_R_INT) == 0) ni5010_rx(dev);
489
490 if ((status & IS_X_INT) == 0) {
491 xmit_was_error = process_xmt_interrupt(dev);
492 }
493
494 if ((status & IS_DMA_INT) == 0) {
495 PRINTK((KERN_DEBUG "%s: DMA complete (?)\n", dev->name));
496 outb(0, IE_DMA_RST); /* Reset DMA int */
497 }
498
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400499 if (!xmit_was_error)
500 reset_receiver(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 spin_unlock(&lp->lock);
502 return IRQ_HANDLED;
503}
504
505
506static void dump_packet(void *buf, int len)
507{
508 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 printk(KERN_DEBUG "Packet length = %#4x\n", len);
511 for (i = 0; i < len; i++){
512 if (i % 16 == 0) printk(KERN_DEBUG "%#4.4x", i);
513 if (i % 2 == 0) printk(" ");
514 printk("%2.2x", ((unsigned char *)buf)[i]);
515 if (i % 16 == 15) printk("\n");
516 }
517 printk("\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return;
520}
521
522/* We have a good packet, get it out of the buffer. */
523static void ni5010_rx(struct net_device *dev)
524{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int ioaddr = dev->base_addr;
526 unsigned char rcv_stat;
527 struct sk_buff *skb;
528 int i_pkt_size;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400529
530 PRINTK2((KERN_DEBUG "%s: entering ni5010_rx()\n", dev->name));
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 rcv_stat = inb(EDLC_RSTAT);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400533 PRINTK3((KERN_DEBUG "%s: EDLC_RSTAT = %#2x\n", dev->name, rcv_stat));
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if ( (rcv_stat & RS_VALID_BITS) != RS_PKT_OK) {
536 PRINTK((KERN_INFO "%s: receive error.\n", dev->name));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700537 dev->stats.rx_errors++;
538 if (rcv_stat & RS_RUNT) dev->stats.rx_length_errors++;
539 if (rcv_stat & RS_ALIGN) dev->stats.rx_frame_errors++;
540 if (rcv_stat & RS_CRC_ERR) dev->stats.rx_crc_errors++;
541 if (rcv_stat & RS_OFLW) dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 outb(0xff, EDLC_RCLR); /* Clear the interrupt */
543 return;
544 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 outb(0xff, EDLC_RCLR); /* Clear the interrupt */
547
548 i_pkt_size = inw(IE_RCNT);
549 if (i_pkt_size > ETH_FRAME_LEN || i_pkt_size < 10 ) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400550 PRINTK((KERN_DEBUG "%s: Packet size error, packet size = %#4.4x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dev->name, i_pkt_size));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700552 dev->stats.rx_errors++;
553 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return;
555 }
556
557 /* Malloc up new buffer. */
558 skb = dev_alloc_skb(i_pkt_size + 3);
559 if (skb == NULL) {
560 printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700561 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return;
563 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 skb_reserve(skb, 2);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Read packet into buffer */
568 outb(MM_MUX, IE_MMODE); /* Rcv buffer to system bus */
569 outw(0, IE_GP); /* Seek to beginning of packet */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400570 insb(IE_RBUF, skb_put(skb, i_pkt_size), i_pkt_size);
571
572 if (NI5010_DEBUG >= 4)
573 dump_packet(skb->data, skb->len);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 skb->protocol = eth_type_trans(skb,dev);
576 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700577 dev->stats.rx_packets++;
578 dev->stats.rx_bytes += i_pkt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400580 PRINTK2((KERN_DEBUG "%s: Received packet, size=%#4.4x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 dev->name, i_pkt_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584static int process_xmt_interrupt(struct net_device *dev)
585{
586 struct ni5010_local *lp = netdev_priv(dev);
587 int ioaddr = dev->base_addr;
588 int xmit_stat;
589
590 PRINTK2((KERN_DEBUG "%s: entering process_xmt_interrupt\n", dev->name));
591
592 xmit_stat = inb(EDLC_XSTAT);
593 PRINTK3((KERN_DEBUG "%s: EDLC_XSTAT = %2.2x\n", dev->name, xmit_stat));
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 outb(0, EDLC_XMASK); /* Disable xmit IRQ's */
596 outb(0xff, EDLC_XCLR); /* Clear all pending xmit IRQ's */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (xmit_stat & XS_COLL){
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400599 PRINTK((KERN_DEBUG "%s: collision detected, retransmitting\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 dev->name));
601 outw(NI5010_BUFSIZE - lp->o_pkt_size, IE_GP);
602 /* outb(0, IE_MMODE); */ /* xmt buf on sysbus FIXME: needed ? */
603 outb(MM_EN_XMT | MM_MUX, IE_MMODE);
604 outb(XM_ALL, EDLC_XMASK); /* Enable xmt IRQ's */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700605 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return 1;
607 }
608
609 /* FIXME: handle other xmt error conditions */
610
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700611 dev->stats.tx_packets++;
612 dev->stats.tx_bytes += lp->o_pkt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 netif_wake_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400614
615 PRINTK2((KERN_DEBUG "%s: sent packet, size=%#4.4x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 dev->name, lp->o_pkt_size));
617
618 return 0;
619}
620
621/* The inverse routine to ni5010_open(). */
622static int ni5010_close(struct net_device *dev)
623{
624 int ioaddr = dev->base_addr;
625
626 PRINTK2((KERN_DEBUG "%s: entering ni5010_close\n", dev->name));
Andreas Mohr5b552b12006-06-30 02:25:07 -0700627#ifdef JUMPERED_INTERRUPTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 free_irq(dev->irq, NULL);
629#endif
630 /* Put card in held-RESET state */
631 outb(0, IE_MMODE);
632 outb(RS_RESET, EDLC_RESET);
633
634 netif_stop_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 PRINTK((KERN_DEBUG "%s: %s closed down\n", dev->name, boardname));
637 return 0;
638
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/* Set or clear the multicast filter for this adaptor.
642 num_addrs == -1 Promiscuous mode, receive all packets
643 num_addrs == 0 Normal mode, clear multicast list
644 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
645 best-effort filtering.
646*/
647static void ni5010_set_multicast_list(struct net_device *dev)
648{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400649 short ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 PRINTK2((KERN_DEBUG "%s: entering set_multicast_list\n", dev->name));
652
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000653 if (dev->flags & IFF_PROMISC || dev->flags & IFF_ALLMULTI ||
654 !netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 outb(RMD_PROMISC, EDLC_RMODE); /* Enable promiscuous mode */
656 PRINTK((KERN_DEBUG "%s: Entering promiscuous mode\n", dev->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else {
658 PRINTK((KERN_DEBUG "%s: Entering broadcast mode\n", dev->name));
659 outb(RMD_BROADCAST, EDLC_RMODE); /* Disable promiscuous mode, use normal mode */
660 }
661}
662
663static void hardware_send_packet(struct net_device *dev, char *buf, int length, int pad)
664{
665 struct ni5010_local *lp = netdev_priv(dev);
666 int ioaddr = dev->base_addr;
667 unsigned long flags;
668 unsigned int buf_offs;
669
670 PRINTK2((KERN_DEBUG "%s: entering hardware_send_packet\n", dev->name));
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (length > ETH_FRAME_LEN) {
673 PRINTK((KERN_WARNING "%s: packet too large, not possible\n",
674 dev->name));
675 return;
676 }
677
678 if (NI5010_DEBUG) ni5010_show_registers(dev);
679
680 if (inb(IE_ISTAT) & IS_EN_XMT) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400681 PRINTK((KERN_WARNING "%s: sending packet while already transmitting, not possible\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 dev->name));
683 return;
684 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (NI5010_DEBUG > 3) dump_packet(buf, length);
687
688 buf_offs = NI5010_BUFSIZE - length - pad;
689
690 spin_lock_irqsave(&lp->lock, flags);
691 lp->o_pkt_size = length + pad;
692
693 outb(0, EDLC_RMASK); /* Mask all receive interrupts */
694 outb(0, IE_MMODE); /* Put Xmit buffer on system bus */
695 outb(0xff, EDLC_RCLR); /* Clear out pending rcv interrupts */
696
697 outw(buf_offs, IE_GP); /* Point GP at start of packet */
698 outsb(IE_XBUF, buf, length); /* Put data in buffer */
699 while(pad--)
700 outb(0, IE_XBUF);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 outw(buf_offs, IE_GP); /* Rewrite where packet starts */
703
704 /* should work without that outb() (Crynwr used it) */
705 /*outb(MM_MUX, IE_MMODE);*/ /* Xmt buffer to EDLC bus */
706 outb(MM_EN_XMT | MM_MUX, IE_MMODE); /* Begin transmission */
707 outb(XM_ALL, EDLC_XMASK); /* Cause interrupt after completion or fail */
708
709 spin_unlock_irqrestore(&lp->lock, flags);
710
711 netif_wake_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400712
713 if (NI5010_DEBUG) ni5010_show_registers(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716static void chipset_init(struct net_device *dev, int startp)
717{
718 /* FIXME: Move some stuff here */
719 PRINTK3((KERN_DEBUG "%s: doing NOTHING in chipset_init\n", dev->name));
720}
721
722static void ni5010_show_registers(struct net_device *dev)
723{
724 int ioaddr = dev->base_addr;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 PRINTK3((KERN_DEBUG "%s: XSTAT %#2.2x\n", dev->name, inb(EDLC_XSTAT)));
727 PRINTK3((KERN_DEBUG "%s: XMASK %#2.2x\n", dev->name, inb(EDLC_XMASK)));
728 PRINTK3((KERN_DEBUG "%s: RSTAT %#2.2x\n", dev->name, inb(EDLC_RSTAT)));
729 PRINTK3((KERN_DEBUG "%s: RMASK %#2.2x\n", dev->name, inb(EDLC_RMASK)));
730 PRINTK3((KERN_DEBUG "%s: RMODE %#2.2x\n", dev->name, inb(EDLC_RMODE)));
731 PRINTK3((KERN_DEBUG "%s: XMODE %#2.2x\n", dev->name, inb(EDLC_XMODE)));
732 PRINTK3((KERN_DEBUG "%s: ISTAT %#2.2x\n", dev->name, inb(IE_ISTAT)));
733}
734
735#ifdef MODULE
736static struct net_device *dev_ni5010;
737
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800738module_param(io, int, 0);
739module_param(irq, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740MODULE_PARM_DESC(io, "ni5010 I/O base address");
741MODULE_PARM_DESC(irq, "ni5010 IRQ number");
742
Andreas Mohr5b552b12006-06-30 02:25:07 -0700743static int __init ni5010_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 PRINTK2((KERN_DEBUG "%s: entering init_module\n", boardname));
746 /*
747 if(io <= 0 || irq == 0){
748 printk(KERN_WARNING "%s: Autoprobing not allowed for modules.\n", boardname);
749 printk(KERN_WARNING "%s: Set symbols 'io' and 'irq'\n", boardname);
750 return -EINVAL;
751 }
752 */
753 if (io <= 0){
754 printk(KERN_WARNING "%s: Autoprobing for modules is hazardous, trying anyway..\n", boardname);
755 }
756
757 PRINTK2((KERN_DEBUG "%s: init_module irq=%#2x, io=%#3x\n", boardname, irq, io));
758 dev_ni5010 = ni5010_probe(-1);
759 if (IS_ERR(dev_ni5010))
760 return PTR_ERR(dev_ni5010);
761 return 0;
762}
763
Andreas Mohr5b552b12006-06-30 02:25:07 -0700764static void __exit ni5010_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 PRINTK2((KERN_DEBUG "%s: entering cleanup_module\n", boardname));
767 unregister_netdev(dev_ni5010);
768 release_region(dev_ni5010->base_addr, NI5010_IO_EXTENT);
769 free_netdev(dev_ni5010);
770}
Andreas Mohr5b552b12006-06-30 02:25:07 -0700771module_init(ni5010_init_module);
772module_exit(ni5010_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#endif /* MODULE */
774MODULE_LICENSE("GPL");