Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* ni5010.c: A network driver for the MiCom-Interlan NI5010 ethercard. |
2 | * | |
5b552b16 | 3 | * Copyright 1996,1997,2006 Jan-Pascal van Best and Andreas Mohr. |
1da177e4 LT |
4 | * |
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: | |
5b552b16 | 9 | * janpascal@vanbest.org andi@lisas.de |
1da177e4 LT |
10 | * |
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) | |
5b552b16 | 25 | * 060611 v1.02: slight cleanup: email addresses, driver modernization. |
1da177e4 | 26 | * Bugs: |
5b552b16 | 27 | * - not SMP-safe (no locking of I/O accesses) |
1da177e4 LT |
28 | * - 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!) | |
5b552b16 | 38 | * Implement DMA (Again, is this useful? Some docs say DMA is |
1da177e4 LT |
39 | * slower than programmed I/O) |
40 | * | |
41 | * Compile with: | |
42 | * gcc -O2 -fomit-frame-pointer -m486 -D__KERNEL__ \ | |
6aa20a22 | 43 | * -DMODULE -c ni5010.c |
1da177e4 LT |
44 | * |
45 | * Insert with e.g.: | |
5b552b16 | 46 | * insmod ni5010.ko io=0x300 irq=5 |
1da177e4 LT |
47 | */ |
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 | ||
5b552b16 AM |
68 | static const char boardname[] = "NI5010"; |
69 | static char version[] __initdata = | |
70 | "ni5010.c: v1.02 20060611 Jan-Pascal van Best and Andreas Mohr\n"; | |
6aa20a22 | 71 | |
1da177e4 LT |
72 | /* bufsize_rcv == 0 means autoprobing */ |
73 | static unsigned int bufsize_rcv; | |
74 | ||
5b552b16 AM |
75 | #define JUMPERED_INTERRUPTS /* IRQ line jumpered on board */ |
76 | #undef JUMPERED_DMA /* No DMA used */ | |
1da177e4 LT |
77 | #undef FULL_IODETECT /* Only detect in portlist */ |
78 | ||
79 | #ifndef FULL_IODETECT | |
80 | /* A zero-terminated list of I/O addresses to be probed. */ | |
81 | static 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. */ | |
91 | struct ni5010_local { | |
1da177e4 LT |
92 | int o_pkt_size; |
93 | spinlock_t lock; | |
94 | }; | |
95 | ||
96 | /* Index to functions, as function prototypes. */ | |
97 | ||
98 | static int ni5010_probe1(struct net_device *dev, int ioaddr); | |
99 | static int ni5010_open(struct net_device *dev); | |
100 | static int ni5010_send_packet(struct sk_buff *skb, struct net_device *dev); | |
7d12e780 | 101 | static irqreturn_t ni5010_interrupt(int irq, void *dev_id); |
1da177e4 LT |
102 | static void ni5010_rx(struct net_device *dev); |
103 | static void ni5010_timeout(struct net_device *dev); | |
104 | static int ni5010_close(struct net_device *dev); | |
1da177e4 LT |
105 | static void ni5010_set_multicast_list(struct net_device *dev); |
106 | static void reset_receiver(struct net_device *dev); | |
107 | ||
108 | static int process_xmt_interrupt(struct net_device *dev); | |
109 | #define tx_done(dev) 1 | |
110 | static void hardware_send_packet(struct net_device *dev, char *buf, int length, int pad); | |
111 | static void chipset_init(struct net_device *dev, int startp); | |
112 | static void dump_packet(void *buf, int len); | |
113 | static void ni5010_show_registers(struct net_device *dev); | |
114 | ||
115 | static int io; | |
116 | static int irq; | |
117 | ||
118 | struct 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 | ||
1da177e4 LT |
136 | 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; | |
160 | out1: | |
161 | release_region(dev->base_addr, NI5010_IO_EXTENT); | |
162 | out: | |
163 | free_netdev(dev); | |
164 | return ERR_PTR(err); | |
165 | } | |
166 | ||
167 | static inline int rd_port(int ioaddr) | |
168 | { | |
169 | inb(IE_RBUF); | |
170 | return inb(IE_SAPROM); | |
171 | } | |
172 | ||
173 | static 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 | ||
198 | static 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; | |
0795af57 | 206 | DECLARE_MAC_BUF(mac); |
1da177e4 LT |
207 | |
208 | dev->base_addr = ioaddr; | |
209 | dev->irq = irq; | |
210 | ||
211 | if (!request_region(ioaddr, NI5010_IO_EXTENT, boardname)) | |
212 | return -EBUSY; | |
213 | ||
214 | /* | |
215 | * This is no "official" probe method, I've rather tested which | |
216 | * probe works best with my seven NI5010 cards | |
217 | * (they have very different serial numbers) | |
218 | * Suggestions or failure reports are very, very welcome ! | |
219 | * But I think it is a relatively good probe method | |
220 | * since it doesn't use any "outb" | |
221 | * It should be nearly 100% reliable ! | |
222 | * well-known WARNING: this probe method (like many others) | |
223 | * will hang the system if a NE2000 card region is probed ! | |
224 | * | |
225 | * - Andreas | |
226 | */ | |
227 | ||
6aa20a22 | 228 | PRINTK2((KERN_DEBUG "%s: entering ni5010_probe1(%#3x)\n", |
1da177e4 LT |
229 | dev->name, ioaddr)); |
230 | ||
231 | if (inb(ioaddr+0) == 0xff) | |
232 | goto out; | |
233 | ||
234 | while ( (rd_port(ioaddr) & rd_port(ioaddr) & rd_port(ioaddr) & | |
235 | rd_port(ioaddr) & rd_port(ioaddr) & rd_port(ioaddr)) != 0xff) | |
236 | { | |
237 | if (boguscount-- == 0) | |
238 | goto out; | |
239 | } | |
240 | ||
241 | PRINTK2((KERN_DEBUG "%s: I/O #1 passed!\n", dev->name)); | |
242 | ||
243 | for (i=0; i<32; i++) | |
244 | if ( (data = rd_port(ioaddr)) != 0xff) break; | |
245 | if (data==0xff) | |
246 | goto out; | |
247 | ||
248 | PRINTK2((KERN_DEBUG "%s: I/O #2 passed!\n", dev->name)); | |
249 | ||
250 | if ((data != SA_ADDR0) || (rd_port(ioaddr) != SA_ADDR1) || | |
251 | (rd_port(ioaddr) != SA_ADDR2)) | |
252 | goto out; | |
253 | ||
254 | for (i=0; i<4; i++) | |
255 | rd_port(ioaddr); | |
256 | ||
257 | if ( (rd_port(ioaddr) != NI5010_MAGICVAL1) || | |
258 | (rd_port(ioaddr) != NI5010_MAGICVAL2) ) | |
259 | goto out; | |
260 | ||
261 | PRINTK2((KERN_DEBUG "%s: I/O #3 passed!\n", dev->name)); | |
262 | ||
263 | if (NI5010_DEBUG && version_printed++ == 0) | |
264 | printk(KERN_INFO "%s", version); | |
265 | ||
266 | printk("NI5010 ethercard probe at 0x%x: ", ioaddr); | |
267 | ||
268 | dev->base_addr = ioaddr; | |
269 | ||
270 | for (i=0; i<6; i++) { | |
271 | outw(i, IE_GP); | |
0795af57 | 272 | dev->dev_addr[i] = inb(IE_SAPROM); |
1da177e4 | 273 | } |
0795af57 | 274 | printk("%s ", print_mac(mac, dev->dev_addr)); |
1da177e4 LT |
275 | |
276 | PRINTK2((KERN_DEBUG "%s: I/O #4 passed!\n", dev->name)); | |
277 | ||
5b552b16 | 278 | #ifdef JUMPERED_INTERRUPTS |
1da177e4 LT |
279 | if (dev->irq == 0xff) |
280 | ; | |
281 | else if (dev->irq < 2) { | |
282 | unsigned long irq_mask; | |
283 | ||
284 | PRINTK2((KERN_DEBUG "%s: I/O #5 passed!\n", dev->name)); | |
285 | ||
286 | irq_mask = probe_irq_on(); | |
287 | trigger_irq(ioaddr); | |
288 | mdelay(20); | |
289 | dev->irq = probe_irq_off(irq_mask); | |
290 | ||
291 | PRINTK2((KERN_DEBUG "%s: I/O #6 passed!\n", dev->name)); | |
292 | ||
293 | if (dev->irq == 0) { | |
294 | err = -EAGAIN; | |
295 | printk(KERN_WARNING "%s: no IRQ found!\n", dev->name); | |
296 | goto out; | |
297 | } | |
298 | PRINTK2((KERN_DEBUG "%s: I/O #7 passed!\n", dev->name)); | |
299 | } else if (dev->irq == 2) { | |
300 | dev->irq = 9; | |
301 | } | |
5b552b16 | 302 | #endif /* JUMPERED_INTERRUPTS */ |
1da177e4 LT |
303 | PRINTK2((KERN_DEBUG "%s: I/O #9 passed!\n", dev->name)); |
304 | ||
305 | /* DMA is not supported (yet?), so no use detecting it */ | |
306 | lp = netdev_priv(dev); | |
307 | ||
308 | spin_lock_init(&lp->lock); | |
309 | ||
310 | PRINTK2((KERN_DEBUG "%s: I/O #10 passed!\n", dev->name)); | |
311 | ||
312 | /* get the size of the onboard receive buffer | |
313 | * higher addresses than bufsize are wrapped into real buffer | |
314 | * i.e. data for offs. 0x801 is written to 0x1 with a 2K onboard buffer | |
315 | */ | |
316 | if (!bufsize_rcv) { | |
317 | outb(1, IE_MMODE); /* Put Rcv buffer on system bus */ | |
318 | outw(0, IE_GP); /* Point GP at start of packet */ | |
319 | outb(0, IE_RBUF); /* set buffer byte 0 to 0 */ | |
320 | for (i = 1; i < 0xff; i++) { | |
321 | outw(i << 8, IE_GP); /* Point GP at packet size to be tested */ | |
322 | outb(i, IE_RBUF); | |
323 | outw(0x0, IE_GP); /* Point GP at start of packet */ | |
324 | data = inb(IE_RBUF); | |
325 | if (data == i) break; | |
326 | } | |
327 | bufsize_rcv = i << 8; | |
328 | outw(0, IE_GP); /* Point GP at start of packet */ | |
329 | outb(0, IE_RBUF); /* set buffer byte 0 to 0 again */ | |
330 | } | |
5b552b16 | 331 | printk("-> bufsize rcv/xmt=%d/%d\n", bufsize_rcv, NI5010_BUFSIZE); |
1da177e4 | 332 | memset(dev->priv, 0, sizeof(struct ni5010_local)); |
6aa20a22 | 333 | |
1da177e4 LT |
334 | dev->open = ni5010_open; |
335 | dev->stop = ni5010_close; | |
336 | dev->hard_start_xmit = ni5010_send_packet; | |
1da177e4 LT |
337 | dev->set_multicast_list = ni5010_set_multicast_list; |
338 | dev->tx_timeout = ni5010_timeout; | |
339 | dev->watchdog_timeo = HZ/20; | |
340 | ||
341 | dev->flags &= ~IFF_MULTICAST; /* Multicast doesn't work */ | |
342 | ||
343 | /* Shut up the ni5010 */ | |
344 | outb(0, EDLC_RMASK); /* Mask all receive interrupts */ | |
345 | outb(0, EDLC_XMASK); /* Mask all xmit interrupts */ | |
346 | outb(0xff, EDLC_RCLR); /* Kill all pending rcv interrupts */ | |
347 | outb(0xff, EDLC_XCLR); /* Kill all pending xmt interrupts */ | |
348 | ||
349 | printk(KERN_INFO "%s: NI5010 found at 0x%x, using IRQ %d", dev->name, ioaddr, dev->irq); | |
5b552b16 AM |
350 | if (dev->dma) |
351 | printk(" & DMA %d", dev->dma); | |
1da177e4 | 352 | printk(".\n"); |
1da177e4 LT |
353 | return 0; |
354 | out: | |
355 | release_region(dev->base_addr, NI5010_IO_EXTENT); | |
356 | return err; | |
357 | } | |
358 | ||
6aa20a22 | 359 | /* |
1da177e4 LT |
360 | * Open/initialize the board. This is called (in the current kernel) |
361 | * sometime after booting when the 'ifconfig' program is run. | |
362 | * | |
363 | * This routine should set everything up anew at each open, even | |
364 | * registers that "should" only need to be set once at boot, so that | |
5b552b16 | 365 | * there is a non-reboot way to recover if something goes wrong. |
1da177e4 | 366 | */ |
6aa20a22 | 367 | |
1da177e4 LT |
368 | static int ni5010_open(struct net_device *dev) |
369 | { | |
370 | int ioaddr = dev->base_addr; | |
371 | int i; | |
372 | ||
6aa20a22 JG |
373 | PRINTK2((KERN_DEBUG "%s: entering ni5010_open()\n", dev->name)); |
374 | ||
1da177e4 LT |
375 | if (request_irq(dev->irq, &ni5010_interrupt, 0, boardname, dev)) { |
376 | printk(KERN_WARNING "%s: Cannot get irq %#2x\n", dev->name, dev->irq); | |
377 | return -EAGAIN; | |
378 | } | |
379 | PRINTK3((KERN_DEBUG "%s: passed open() #1\n", dev->name)); | |
380 | /* | |
381 | * Always allocate the DMA channel after the IRQ, | |
382 | * and clean up on failure. | |
383 | */ | |
5b552b16 | 384 | #ifdef JUMPERED_DMA |
1da177e4 LT |
385 | if (request_dma(dev->dma, cardname)) { |
386 | printk(KERN_WARNING "%s: Cannot get dma %#2x\n", dev->name, dev->dma); | |
387 | free_irq(dev->irq, NULL); | |
388 | return -EAGAIN; | |
389 | } | |
5b552b16 | 390 | #endif /* JUMPERED_DMA */ |
1da177e4 LT |
391 | |
392 | PRINTK3((KERN_DEBUG "%s: passed open() #2\n", dev->name)); | |
393 | /* Reset the hardware here. Don't forget to set the station address. */ | |
394 | ||
395 | outb(RS_RESET, EDLC_RESET); /* Hold up EDLC_RESET while configing board */ | |
396 | outb(0, IE_RESET); /* Hardware reset of ni5010 board */ | |
397 | outb(XMD_LBC, EDLC_XMODE); /* Only loopback xmits */ | |
398 | ||
399 | PRINTK3((KERN_DEBUG "%s: passed open() #3\n", dev->name)); | |
400 | /* Set the station address */ | |
401 | for(i = 0;i < 6; i++) { | |
402 | outb(dev->dev_addr[i], EDLC_ADDR + i); | |
403 | } | |
6aa20a22 JG |
404 | |
405 | PRINTK3((KERN_DEBUG "%s: Initialising ni5010\n", dev->name)); | |
1da177e4 | 406 | outb(0, EDLC_XMASK); /* No xmit interrupts for now */ |
6aa20a22 | 407 | outb(XMD_IG_PAR | XMD_T_MODE | XMD_LBC, EDLC_XMODE); |
1da177e4 LT |
408 | /* Normal packet xmit mode */ |
409 | outb(0xff, EDLC_XCLR); /* Clear all pending xmit interrupts */ | |
410 | outb(RMD_BROADCAST, EDLC_RMODE); | |
411 | /* Receive broadcast and normal packets */ | |
412 | reset_receiver(dev); /* Ready ni5010 for receiving packets */ | |
6aa20a22 | 413 | |
1da177e4 | 414 | outb(0, EDLC_RESET); /* Un-reset the ni5010 */ |
6aa20a22 | 415 | |
1da177e4 | 416 | netif_start_queue(dev); |
6aa20a22 JG |
417 | |
418 | if (NI5010_DEBUG) ni5010_show_registers(dev); | |
1da177e4 LT |
419 | |
420 | PRINTK((KERN_DEBUG "%s: open successful\n", dev->name)); | |
421 | return 0; | |
422 | } | |
423 | ||
424 | static void reset_receiver(struct net_device *dev) | |
425 | { | |
426 | int ioaddr = dev->base_addr; | |
6aa20a22 | 427 | |
1da177e4 LT |
428 | PRINTK3((KERN_DEBUG "%s: resetting receiver\n", dev->name)); |
429 | outw(0, IE_GP); /* Receive packet at start of buffer */ | |
430 | outb(0xff, EDLC_RCLR); /* Clear all pending rcv interrupts */ | |
431 | outb(0, IE_MMODE); /* Put EDLC to rcv buffer */ | |
432 | outb(MM_EN_RCV, IE_MMODE); /* Enable rcv */ | |
433 | outb(0xff, EDLC_RMASK); /* Enable all rcv interrupts */ | |
434 | } | |
435 | ||
436 | static void ni5010_timeout(struct net_device *dev) | |
437 | { | |
438 | printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name, | |
439 | tx_done(dev) ? "IRQ conflict" : "network cable problem"); | |
440 | /* Try to restart the adaptor. */ | |
441 | /* FIXME: Give it a real kick here */ | |
442 | chipset_init(dev, 1); | |
443 | dev->trans_start = jiffies; | |
444 | netif_wake_queue(dev); | |
445 | } | |
446 | ||
447 | static int ni5010_send_packet(struct sk_buff *skb, struct net_device *dev) | |
448 | { | |
449 | int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN; | |
450 | ||
451 | PRINTK2((KERN_DEBUG "%s: entering ni5010_send_packet\n", dev->name)); | |
452 | ||
6aa20a22 | 453 | /* |
1da177e4 LT |
454 | * Block sending |
455 | */ | |
6aa20a22 | 456 | |
1da177e4 LT |
457 | netif_stop_queue(dev); |
458 | hardware_send_packet(dev, (unsigned char *)skb->data, skb->len, length-skb->len); | |
459 | dev->trans_start = jiffies; | |
460 | dev_kfree_skb (skb); | |
461 | return 0; | |
462 | } | |
463 | ||
6aa20a22 | 464 | /* |
1da177e4 | 465 | * The typical workload of the driver: |
6aa20a22 | 466 | * Handle the network interface interrupts. |
1da177e4 | 467 | */ |
7d12e780 | 468 | static irqreturn_t ni5010_interrupt(int irq, void *dev_id) |
1da177e4 LT |
469 | { |
470 | struct net_device *dev = dev_id; | |
471 | struct ni5010_local *lp; | |
472 | int ioaddr, status; | |
473 | int xmit_was_error = 0; | |
474 | ||
475 | PRINTK2((KERN_DEBUG "%s: entering ni5010_interrupt\n", dev->name)); | |
476 | ||
477 | ioaddr = dev->base_addr; | |
478 | lp = netdev_priv(dev); | |
6aa20a22 | 479 | |
1da177e4 | 480 | spin_lock(&lp->lock); |
6aa20a22 | 481 | status = inb(IE_ISTAT); |
1da177e4 | 482 | PRINTK3((KERN_DEBUG "%s: IE_ISTAT = %#02x\n", dev->name, status)); |
6aa20a22 | 483 | |
1da177e4 LT |
484 | if ((status & IS_R_INT) == 0) ni5010_rx(dev); |
485 | ||
486 | if ((status & IS_X_INT) == 0) { | |
487 | xmit_was_error = process_xmt_interrupt(dev); | |
488 | } | |
489 | ||
490 | if ((status & IS_DMA_INT) == 0) { | |
491 | PRINTK((KERN_DEBUG "%s: DMA complete (?)\n", dev->name)); | |
492 | outb(0, IE_DMA_RST); /* Reset DMA int */ | |
493 | } | |
494 | ||
6aa20a22 JG |
495 | if (!xmit_was_error) |
496 | reset_receiver(dev); | |
1da177e4 LT |
497 | spin_unlock(&lp->lock); |
498 | return IRQ_HANDLED; | |
499 | } | |
500 | ||
501 | ||
502 | static void dump_packet(void *buf, int len) | |
503 | { | |
504 | int i; | |
6aa20a22 | 505 | |
1da177e4 LT |
506 | printk(KERN_DEBUG "Packet length = %#4x\n", len); |
507 | for (i = 0; i < len; i++){ | |
508 | if (i % 16 == 0) printk(KERN_DEBUG "%#4.4x", i); | |
509 | if (i % 2 == 0) printk(" "); | |
510 | printk("%2.2x", ((unsigned char *)buf)[i]); | |
511 | if (i % 16 == 15) printk("\n"); | |
512 | } | |
513 | printk("\n"); | |
6aa20a22 | 514 | |
1da177e4 LT |
515 | return; |
516 | } | |
517 | ||
518 | /* We have a good packet, get it out of the buffer. */ | |
519 | static void ni5010_rx(struct net_device *dev) | |
520 | { | |
1da177e4 LT |
521 | int ioaddr = dev->base_addr; |
522 | unsigned char rcv_stat; | |
523 | struct sk_buff *skb; | |
524 | int i_pkt_size; | |
6aa20a22 JG |
525 | |
526 | PRINTK2((KERN_DEBUG "%s: entering ni5010_rx()\n", dev->name)); | |
527 | ||
1da177e4 | 528 | rcv_stat = inb(EDLC_RSTAT); |
6aa20a22 JG |
529 | PRINTK3((KERN_DEBUG "%s: EDLC_RSTAT = %#2x\n", dev->name, rcv_stat)); |
530 | ||
1da177e4 LT |
531 | if ( (rcv_stat & RS_VALID_BITS) != RS_PKT_OK) { |
532 | PRINTK((KERN_INFO "%s: receive error.\n", dev->name)); | |
09f75cd7 JG |
533 | dev->stats.rx_errors++; |
534 | if (rcv_stat & RS_RUNT) dev->stats.rx_length_errors++; | |
535 | if (rcv_stat & RS_ALIGN) dev->stats.rx_frame_errors++; | |
536 | if (rcv_stat & RS_CRC_ERR) dev->stats.rx_crc_errors++; | |
537 | if (rcv_stat & RS_OFLW) dev->stats.rx_fifo_errors++; | |
1da177e4 LT |
538 | outb(0xff, EDLC_RCLR); /* Clear the interrupt */ |
539 | return; | |
540 | } | |
6aa20a22 | 541 | |
1da177e4 LT |
542 | outb(0xff, EDLC_RCLR); /* Clear the interrupt */ |
543 | ||
544 | i_pkt_size = inw(IE_RCNT); | |
545 | if (i_pkt_size > ETH_FRAME_LEN || i_pkt_size < 10 ) { | |
6aa20a22 | 546 | PRINTK((KERN_DEBUG "%s: Packet size error, packet size = %#4.4x\n", |
1da177e4 | 547 | dev->name, i_pkt_size)); |
09f75cd7 JG |
548 | dev->stats.rx_errors++; |
549 | dev->stats.rx_length_errors++; | |
1da177e4 LT |
550 | return; |
551 | } | |
552 | ||
553 | /* Malloc up new buffer. */ | |
554 | skb = dev_alloc_skb(i_pkt_size + 3); | |
555 | if (skb == NULL) { | |
556 | printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name); | |
09f75cd7 | 557 | dev->stats.rx_dropped++; |
1da177e4 LT |
558 | return; |
559 | } | |
6aa20a22 | 560 | |
1da177e4 | 561 | skb_reserve(skb, 2); |
6aa20a22 | 562 | |
1da177e4 LT |
563 | /* Read packet into buffer */ |
564 | outb(MM_MUX, IE_MMODE); /* Rcv buffer to system bus */ | |
565 | outw(0, IE_GP); /* Seek to beginning of packet */ | |
6aa20a22 JG |
566 | insb(IE_RBUF, skb_put(skb, i_pkt_size), i_pkt_size); |
567 | ||
568 | if (NI5010_DEBUG >= 4) | |
569 | dump_packet(skb->data, skb->len); | |
570 | ||
1da177e4 LT |
571 | skb->protocol = eth_type_trans(skb,dev); |
572 | netif_rx(skb); | |
573 | dev->last_rx = jiffies; | |
09f75cd7 JG |
574 | dev->stats.rx_packets++; |
575 | dev->stats.rx_bytes += i_pkt_size; | |
1da177e4 | 576 | |
6aa20a22 | 577 | PRINTK2((KERN_DEBUG "%s: Received packet, size=%#4.4x\n", |
1da177e4 | 578 | dev->name, i_pkt_size)); |
1da177e4 LT |
579 | } |
580 | ||
581 | static int process_xmt_interrupt(struct net_device *dev) | |
582 | { | |
583 | struct ni5010_local *lp = netdev_priv(dev); | |
584 | int ioaddr = dev->base_addr; | |
585 | int xmit_stat; | |
586 | ||
587 | PRINTK2((KERN_DEBUG "%s: entering process_xmt_interrupt\n", dev->name)); | |
588 | ||
589 | xmit_stat = inb(EDLC_XSTAT); | |
590 | PRINTK3((KERN_DEBUG "%s: EDLC_XSTAT = %2.2x\n", dev->name, xmit_stat)); | |
6aa20a22 | 591 | |
1da177e4 LT |
592 | outb(0, EDLC_XMASK); /* Disable xmit IRQ's */ |
593 | outb(0xff, EDLC_XCLR); /* Clear all pending xmit IRQ's */ | |
6aa20a22 | 594 | |
1da177e4 | 595 | if (xmit_stat & XS_COLL){ |
6aa20a22 | 596 | PRINTK((KERN_DEBUG "%s: collision detected, retransmitting\n", |
1da177e4 LT |
597 | dev->name)); |
598 | outw(NI5010_BUFSIZE - lp->o_pkt_size, IE_GP); | |
599 | /* outb(0, IE_MMODE); */ /* xmt buf on sysbus FIXME: needed ? */ | |
600 | outb(MM_EN_XMT | MM_MUX, IE_MMODE); | |
601 | outb(XM_ALL, EDLC_XMASK); /* Enable xmt IRQ's */ | |
09f75cd7 | 602 | dev->stats.collisions++; |
1da177e4 LT |
603 | return 1; |
604 | } | |
605 | ||
606 | /* FIXME: handle other xmt error conditions */ | |
607 | ||
09f75cd7 JG |
608 | dev->stats.tx_packets++; |
609 | dev->stats.tx_bytes += lp->o_pkt_size; | |
1da177e4 | 610 | netif_wake_queue(dev); |
6aa20a22 JG |
611 | |
612 | PRINTK2((KERN_DEBUG "%s: sent packet, size=%#4.4x\n", | |
1da177e4 LT |
613 | dev->name, lp->o_pkt_size)); |
614 | ||
615 | return 0; | |
616 | } | |
617 | ||
618 | /* The inverse routine to ni5010_open(). */ | |
619 | static int ni5010_close(struct net_device *dev) | |
620 | { | |
621 | int ioaddr = dev->base_addr; | |
622 | ||
623 | PRINTK2((KERN_DEBUG "%s: entering ni5010_close\n", dev->name)); | |
5b552b16 | 624 | #ifdef JUMPERED_INTERRUPTS |
1da177e4 LT |
625 | free_irq(dev->irq, NULL); |
626 | #endif | |
627 | /* Put card in held-RESET state */ | |
628 | outb(0, IE_MMODE); | |
629 | outb(RS_RESET, EDLC_RESET); | |
630 | ||
631 | netif_stop_queue(dev); | |
6aa20a22 | 632 | |
1da177e4 LT |
633 | PRINTK((KERN_DEBUG "%s: %s closed down\n", dev->name, boardname)); |
634 | return 0; | |
635 | ||
636 | } | |
637 | ||
1da177e4 LT |
638 | /* Set or clear the multicast filter for this adaptor. |
639 | num_addrs == -1 Promiscuous mode, receive all packets | |
640 | num_addrs == 0 Normal mode, clear multicast list | |
641 | num_addrs > 0 Multicast mode, receive normal and MC packets, and do | |
642 | best-effort filtering. | |
643 | */ | |
644 | static void ni5010_set_multicast_list(struct net_device *dev) | |
645 | { | |
6aa20a22 | 646 | short ioaddr = dev->base_addr; |
1da177e4 LT |
647 | |
648 | PRINTK2((KERN_DEBUG "%s: entering set_multicast_list\n", dev->name)); | |
649 | ||
b947dd4b | 650 | if (dev->flags&IFF_PROMISC || dev->flags&IFF_ALLMULTI || dev->mc_list) { |
1da177e4 LT |
651 | outb(RMD_PROMISC, EDLC_RMODE); /* Enable promiscuous mode */ |
652 | PRINTK((KERN_DEBUG "%s: Entering promiscuous mode\n", dev->name)); | |
1da177e4 LT |
653 | } else { |
654 | PRINTK((KERN_DEBUG "%s: Entering broadcast mode\n", dev->name)); | |
655 | outb(RMD_BROADCAST, EDLC_RMODE); /* Disable promiscuous mode, use normal mode */ | |
656 | } | |
657 | } | |
658 | ||
659 | static void hardware_send_packet(struct net_device *dev, char *buf, int length, int pad) | |
660 | { | |
661 | struct ni5010_local *lp = netdev_priv(dev); | |
662 | int ioaddr = dev->base_addr; | |
663 | unsigned long flags; | |
664 | unsigned int buf_offs; | |
665 | ||
666 | PRINTK2((KERN_DEBUG "%s: entering hardware_send_packet\n", dev->name)); | |
6aa20a22 | 667 | |
1da177e4 LT |
668 | if (length > ETH_FRAME_LEN) { |
669 | PRINTK((KERN_WARNING "%s: packet too large, not possible\n", | |
670 | dev->name)); | |
671 | return; | |
672 | } | |
673 | ||
674 | if (NI5010_DEBUG) ni5010_show_registers(dev); | |
675 | ||
676 | if (inb(IE_ISTAT) & IS_EN_XMT) { | |
6aa20a22 | 677 | PRINTK((KERN_WARNING "%s: sending packet while already transmitting, not possible\n", |
1da177e4 LT |
678 | dev->name)); |
679 | return; | |
680 | } | |
6aa20a22 | 681 | |
1da177e4 LT |
682 | if (NI5010_DEBUG > 3) dump_packet(buf, length); |
683 | ||
684 | buf_offs = NI5010_BUFSIZE - length - pad; | |
685 | ||
686 | spin_lock_irqsave(&lp->lock, flags); | |
687 | lp->o_pkt_size = length + pad; | |
688 | ||
689 | outb(0, EDLC_RMASK); /* Mask all receive interrupts */ | |
690 | outb(0, IE_MMODE); /* Put Xmit buffer on system bus */ | |
691 | outb(0xff, EDLC_RCLR); /* Clear out pending rcv interrupts */ | |
692 | ||
693 | outw(buf_offs, IE_GP); /* Point GP at start of packet */ | |
694 | outsb(IE_XBUF, buf, length); /* Put data in buffer */ | |
695 | while(pad--) | |
696 | outb(0, IE_XBUF); | |
6aa20a22 | 697 | |
1da177e4 LT |
698 | outw(buf_offs, IE_GP); /* Rewrite where packet starts */ |
699 | ||
700 | /* should work without that outb() (Crynwr used it) */ | |
701 | /*outb(MM_MUX, IE_MMODE);*/ /* Xmt buffer to EDLC bus */ | |
702 | outb(MM_EN_XMT | MM_MUX, IE_MMODE); /* Begin transmission */ | |
703 | outb(XM_ALL, EDLC_XMASK); /* Cause interrupt after completion or fail */ | |
704 | ||
705 | spin_unlock_irqrestore(&lp->lock, flags); | |
706 | ||
707 | netif_wake_queue(dev); | |
6aa20a22 JG |
708 | |
709 | if (NI5010_DEBUG) ni5010_show_registers(dev); | |
1da177e4 LT |
710 | } |
711 | ||
712 | static void chipset_init(struct net_device *dev, int startp) | |
713 | { | |
714 | /* FIXME: Move some stuff here */ | |
715 | PRINTK3((KERN_DEBUG "%s: doing NOTHING in chipset_init\n", dev->name)); | |
716 | } | |
717 | ||
718 | static void ni5010_show_registers(struct net_device *dev) | |
719 | { | |
720 | int ioaddr = dev->base_addr; | |
6aa20a22 | 721 | |
1da177e4 LT |
722 | PRINTK3((KERN_DEBUG "%s: XSTAT %#2.2x\n", dev->name, inb(EDLC_XSTAT))); |
723 | PRINTK3((KERN_DEBUG "%s: XMASK %#2.2x\n", dev->name, inb(EDLC_XMASK))); | |
724 | PRINTK3((KERN_DEBUG "%s: RSTAT %#2.2x\n", dev->name, inb(EDLC_RSTAT))); | |
725 | PRINTK3((KERN_DEBUG "%s: RMASK %#2.2x\n", dev->name, inb(EDLC_RMASK))); | |
726 | PRINTK3((KERN_DEBUG "%s: RMODE %#2.2x\n", dev->name, inb(EDLC_RMODE))); | |
727 | PRINTK3((KERN_DEBUG "%s: XMODE %#2.2x\n", dev->name, inb(EDLC_XMODE))); | |
728 | PRINTK3((KERN_DEBUG "%s: ISTAT %#2.2x\n", dev->name, inb(IE_ISTAT))); | |
729 | } | |
730 | ||
731 | #ifdef MODULE | |
732 | static struct net_device *dev_ni5010; | |
733 | ||
8d3b33f6 RR |
734 | module_param(io, int, 0); |
735 | module_param(irq, int, 0); | |
1da177e4 LT |
736 | MODULE_PARM_DESC(io, "ni5010 I/O base address"); |
737 | MODULE_PARM_DESC(irq, "ni5010 IRQ number"); | |
738 | ||
5b552b16 | 739 | static int __init ni5010_init_module(void) |
1da177e4 LT |
740 | { |
741 | PRINTK2((KERN_DEBUG "%s: entering init_module\n", boardname)); | |
742 | /* | |
743 | if(io <= 0 || irq == 0){ | |
744 | printk(KERN_WARNING "%s: Autoprobing not allowed for modules.\n", boardname); | |
745 | printk(KERN_WARNING "%s: Set symbols 'io' and 'irq'\n", boardname); | |
746 | return -EINVAL; | |
747 | } | |
748 | */ | |
749 | if (io <= 0){ | |
750 | printk(KERN_WARNING "%s: Autoprobing for modules is hazardous, trying anyway..\n", boardname); | |
751 | } | |
752 | ||
753 | PRINTK2((KERN_DEBUG "%s: init_module irq=%#2x, io=%#3x\n", boardname, irq, io)); | |
754 | dev_ni5010 = ni5010_probe(-1); | |
755 | if (IS_ERR(dev_ni5010)) | |
756 | return PTR_ERR(dev_ni5010); | |
757 | return 0; | |
758 | } | |
759 | ||
5b552b16 | 760 | static void __exit ni5010_cleanup_module(void) |
1da177e4 LT |
761 | { |
762 | PRINTK2((KERN_DEBUG "%s: entering cleanup_module\n", boardname)); | |
763 | unregister_netdev(dev_ni5010); | |
764 | release_region(dev_ni5010->base_addr, NI5010_IO_EXTENT); | |
765 | free_netdev(dev_ni5010); | |
766 | } | |
5b552b16 AM |
767 | module_init(ni5010_init_module); |
768 | module_exit(ni5010_cleanup_module); | |
1da177e4 LT |
769 | #endif /* MODULE */ |
770 | MODULE_LICENSE("GPL"); | |
771 | ||
772 | /* | |
773 | * Local variables: | |
774 | * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c ni5010.c" | |
775 | * version-control: t | |
776 | * kept-new-versions: 5 | |
777 | * tab-width: 4 | |
778 | * End: | |
779 | */ |