Merge branch 'master' into upstream-fixes
[sfrench/cifs-2.6.git] / drivers / net / arm / ether1.c
1 /*
2  *  linux/drivers/acorn/net/ether1.c
3  *
4  *  Copyright (C) 1996-2000 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Acorn ether1 driver (82586 chip) for Acorn machines
11  *
12  * We basically keep two queues in the cards memory - one for transmit
13  * and one for receive.  Each has a head and a tail.  The head is where
14  * we/the chip adds packets to be transmitted/received, and the tail
15  * is where the transmitter has got to/where the receiver will stop.
16  * Both of these queues are circular, and since the chip is running
17  * all the time, we have to be careful when we modify the pointers etc
18  * so that the buffer memory contents is valid all the time.
19  *
20  * Change log:
21  * 1.00 RMK                     Released
22  * 1.01 RMK     19/03/1996      Transfers the last odd byte onto/off of the card now.
23  * 1.02 RMK     25/05/1997      Added code to restart RU if it goes not ready
24  * 1.03 RMK     14/09/1997      Cleaned up the handling of a reset during the TX interrupt.
25  *                              Should prevent lockup.
26  * 1.04 RMK     17/09/1997      Added more info when initialsation of chip goes wrong.
27  *                              TDR now only reports failure when chip reports non-zero
28  *                              TDR time-distance.
29  * 1.05 RMK     31/12/1997      Removed calls to dev_tint for 2.1
30  * 1.06 RMK     10/02/2000      Updated for 2.3.43
31  * 1.07 RMK     13/05/2000      Updated for 2.3.99-pre8
32  */
33
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/types.h>
38 #include <linux/fcntl.h>
39 #include <linux/interrupt.h>
40 #include <linux/ptrace.h>
41 #include <linux/ioport.h>
42 #include <linux/in.h>
43 #include <linux/slab.h>
44 #include <linux/string.h>
45 #include <linux/errno.h>
46 #include <linux/device.h>
47 #include <linux/init.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/bitops.h>
52
53 #include <asm/system.h>
54 #include <asm/io.h>
55 #include <asm/dma.h>
56 #include <asm/ecard.h>
57
58 #define __ETHER1_C
59 #include "ether1.h"
60
61 static unsigned int net_debug = NET_DEBUG;
62
63 #define BUFFER_SIZE     0x10000
64 #define TX_AREA_START   0x00100
65 #define TX_AREA_END     0x05000
66 #define RX_AREA_START   0x05000
67 #define RX_AREA_END     0x0fc00
68
69 static int ether1_open(struct net_device *dev);
70 static int ether1_sendpacket(struct sk_buff *skb, struct net_device *dev);
71 static irqreturn_t ether1_interrupt(int irq, void *dev_id);
72 static int ether1_close(struct net_device *dev);
73 static struct net_device_stats *ether1_getstats(struct net_device *dev);
74 static void ether1_setmulticastlist(struct net_device *dev);
75 static void ether1_timeout(struct net_device *dev);
76
77 /* ------------------------------------------------------------------------- */
78
79 static char version[] __initdata = "ether1 ethernet driver (c) 2000 Russell King v1.07\n";
80
81 #define BUS_16 16
82 #define BUS_8  8
83
84 /* ------------------------------------------------------------------------- */
85
86 #define DISABLEIRQS 1
87 #define NORMALIRQS  0
88
89 #define ether1_readw(dev, addr, type, offset, svflgs) ether1_inw_p (dev, addr + (int)(&((type *)0)->offset), svflgs)
90 #define ether1_writew(dev, val, addr, type, offset, svflgs) ether1_outw_p (dev, val, addr + (int)(&((type *)0)->offset), svflgs)
91
92 static inline unsigned short
93 ether1_inw_p (struct net_device *dev, int addr, int svflgs)
94 {
95         unsigned long flags;
96         unsigned short ret;
97
98         if (svflgs)
99                 local_irq_save (flags);
100
101         writeb(addr >> 12, REG_PAGE);
102         ret = readw(ETHER1_RAM + ((addr & 4095) << 1));
103         if (svflgs)
104                 local_irq_restore (flags);
105         return ret;
106 }
107
108 static inline void
109 ether1_outw_p (struct net_device *dev, unsigned short val, int addr, int svflgs)
110 {
111         unsigned long flags;
112
113         if (svflgs)
114                 local_irq_save (flags);
115
116         writeb(addr >> 12, REG_PAGE);
117         writew(val, ETHER1_RAM + ((addr & 4095) << 1));
118         if (svflgs)
119                 local_irq_restore (flags);
120 }
121
122 /*
123  * Some inline assembler to allow fast transfers on to/off of the card.
124  * Since this driver depends on some features presented by the ARM
125  * specific architecture, and that you can't configure this driver
126  * without specifiing ARM mode, this is not a problem.
127  *
128  * This routine is essentially an optimised memcpy from the card's
129  * onboard RAM to kernel memory.
130  */
131 static void
132 ether1_writebuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
133 {
134         unsigned int page, thislen, offset;
135         void __iomem *addr;
136
137         offset = start & 4095;
138         page = start >> 12;
139         addr = ETHER1_RAM + (offset << 1);
140
141         if (offset + length > 4096)
142                 thislen = 4096 - offset;
143         else
144                 thislen = length;
145
146         do {
147                 int used;
148
149                 writeb(page, REG_PAGE);
150                 length -= thislen;
151
152                 __asm__ __volatile__(
153         "subs   %3, %3, #2\n\
154         bmi     2f\n\
155 1:      ldr     %0, [%1], #2\n\
156         mov     %0, %0, lsl #16\n\
157         orr     %0, %0, %0, lsr #16\n\
158         str     %0, [%2], #4\n\
159         subs    %3, %3, #2\n\
160         bmi     2f\n\
161         ldr     %0, [%1], #2\n\
162         mov     %0, %0, lsl #16\n\
163         orr     %0, %0, %0, lsr #16\n\
164         str     %0, [%2], #4\n\
165         subs    %3, %3, #2\n\
166         bmi     2f\n\
167         ldr     %0, [%1], #2\n\
168         mov     %0, %0, lsl #16\n\
169         orr     %0, %0, %0, lsr #16\n\
170         str     %0, [%2], #4\n\
171         subs    %3, %3, #2\n\
172         bmi     2f\n\
173         ldr     %0, [%1], #2\n\
174         mov     %0, %0, lsl #16\n\
175         orr     %0, %0, %0, lsr #16\n\
176         str     %0, [%2], #4\n\
177         subs    %3, %3, #2\n\
178         bpl     1b\n\
179 2:      adds    %3, %3, #1\n\
180         ldreqb  %0, [%1]\n\
181         streqb  %0, [%2]"
182                 : "=&r" (used), "=&r" (data)
183                 : "r"  (addr), "r" (thislen), "1" (data));
184
185                 addr = ETHER1_RAM;
186
187                 thislen = length;
188                 if (thislen > 4096)
189                         thislen = 4096;
190                 page++;
191         } while (thislen);
192 }
193
194 static void
195 ether1_readbuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
196 {
197         unsigned int page, thislen, offset;
198         void __iomem *addr;
199
200         offset = start & 4095;
201         page = start >> 12;
202         addr = ETHER1_RAM + (offset << 1);
203
204         if (offset + length > 4096)
205                 thislen = 4096 - offset;
206         else
207                 thislen = length;
208
209         do {
210                 int used;
211
212                 writeb(page, REG_PAGE);
213                 length -= thislen;
214
215                 __asm__ __volatile__(
216         "subs   %3, %3, #2\n\
217         bmi     2f\n\
218 1:      ldr     %0, [%2], #4\n\
219         strb    %0, [%1], #1\n\
220         mov     %0, %0, lsr #8\n\
221         strb    %0, [%1], #1\n\
222         subs    %3, %3, #2\n\
223         bmi     2f\n\
224         ldr     %0, [%2], #4\n\
225         strb    %0, [%1], #1\n\
226         mov     %0, %0, lsr #8\n\
227         strb    %0, [%1], #1\n\
228         subs    %3, %3, #2\n\
229         bmi     2f\n\
230         ldr     %0, [%2], #4\n\
231         strb    %0, [%1], #1\n\
232         mov     %0, %0, lsr #8\n\
233         strb    %0, [%1], #1\n\
234         subs    %3, %3, #2\n\
235         bmi     2f\n\
236         ldr     %0, [%2], #4\n\
237         strb    %0, [%1], #1\n\
238         mov     %0, %0, lsr #8\n\
239         strb    %0, [%1], #1\n\
240         subs    %3, %3, #2\n\
241         bpl     1b\n\
242 2:      adds    %3, %3, #1\n\
243         ldreqb  %0, [%2]\n\
244         streqb  %0, [%1]"
245                 : "=&r" (used), "=&r" (data)
246                 : "r"  (addr), "r" (thislen), "1" (data));
247
248                 addr = ETHER1_RAM;
249
250                 thislen = length;
251                 if (thislen > 4096)
252                         thislen = 4096;
253                 page++;
254         } while (thislen);
255 }
256
257 static int __init
258 ether1_ramtest(struct net_device *dev, unsigned char byte)
259 {
260         unsigned char *buffer = kmalloc (BUFFER_SIZE, GFP_KERNEL);
261         int i, ret = BUFFER_SIZE;
262         int max_errors = 15;
263         int bad = -1;
264         int bad_start = 0;
265
266         if (!buffer)
267                 return 1;
268
269         memset (buffer, byte, BUFFER_SIZE);
270         ether1_writebuffer (dev, buffer, 0, BUFFER_SIZE);
271         memset (buffer, byte ^ 0xff, BUFFER_SIZE);
272         ether1_readbuffer (dev, buffer, 0, BUFFER_SIZE);
273
274         for (i = 0; i < BUFFER_SIZE; i++) {
275                 if (buffer[i] != byte) {
276                         if (max_errors >= 0 && bad != buffer[i]) {
277                                 if (bad != -1)
278                                         printk ("\n");
279                                 printk (KERN_CRIT "%s: RAM failed with (%02X instead of %02X) at 0x%04X",
280                                         dev->name, buffer[i], byte, i);
281                                 ret = -ENODEV;
282                                 max_errors --;
283                                 bad = buffer[i];
284                                 bad_start = i;
285                         }
286                 } else {
287                         if (bad != -1) {
288                                 if (bad_start == i - 1)
289                                         printk ("\n");
290                                 else
291                                         printk (" - 0x%04X\n", i - 1);
292                                 bad = -1;
293                         }
294                 }
295         }
296
297         if (bad != -1)
298                 printk (" - 0x%04X\n", BUFFER_SIZE);
299         kfree (buffer);
300
301         return ret;
302 }
303
304 static int
305 ether1_reset (struct net_device *dev)
306 {
307         writeb(CTRL_RST|CTRL_ACK, REG_CONTROL);
308         return BUS_16;
309 }
310
311 static int __init
312 ether1_init_2(struct net_device *dev)
313 {
314         int i;
315         dev->mem_start = 0;
316
317         i = ether1_ramtest (dev, 0x5a);
318
319         if (i > 0)
320                 i = ether1_ramtest (dev, 0x1e);
321
322         if (i <= 0)
323                 return -ENODEV;
324
325         dev->mem_end = i;
326         return 0;
327 }
328
329 /*
330  * These are the structures that are loaded into the ether RAM card to
331  * initialise the 82586
332  */
333
334 /* at 0x0100 */
335 #define NOP_ADDR        (TX_AREA_START)
336 #define NOP_SIZE        (0x06)
337 static nop_t  init_nop  = {
338         0,
339         CMD_NOP,
340         NOP_ADDR
341 };
342
343 /* at 0x003a */
344 #define TDR_ADDR        (0x003a)
345 #define TDR_SIZE        (0x08)
346 static tdr_t  init_tdr  = {
347         0,
348         CMD_TDR | CMD_INTR,
349         NOP_ADDR,
350         0
351 };
352
353 /* at 0x002e */
354 #define MC_ADDR         (0x002e)
355 #define MC_SIZE         (0x0c)
356 static mc_t   init_mc   = {
357         0,
358         CMD_SETMULTICAST,
359         TDR_ADDR,
360         0,
361         { { 0, } }
362 };
363
364 /* at 0x0022 */
365 #define SA_ADDR         (0x0022)
366 #define SA_SIZE         (0x0c)
367 static sa_t   init_sa   = {
368         0,
369         CMD_SETADDRESS,
370         MC_ADDR,
371         { 0, }
372 };
373
374 /* at 0x0010 */
375 #define CFG_ADDR        (0x0010)
376 #define CFG_SIZE        (0x12)
377 static cfg_t  init_cfg  = {
378         0,
379         CMD_CONFIG,
380         SA_ADDR,
381         8,
382         8,
383         CFG8_SRDY,
384         CFG9_PREAMB8 | CFG9_ADDRLENBUF | CFG9_ADDRLEN(6),
385         0,
386         0x60,
387         0,
388         CFG13_RETRY(15) | CFG13_SLOTH(2),
389         0,
390 };
391
392 /* at 0x0000 */
393 #define SCB_ADDR        (0x0000)
394 #define SCB_SIZE        (0x10)
395 static scb_t  init_scb  = {
396         0,
397         SCB_CMDACKRNR | SCB_CMDACKCNA | SCB_CMDACKFR | SCB_CMDACKCX,
398         CFG_ADDR,
399         RX_AREA_START,
400         0,
401         0,
402         0,
403         0
404 };
405
406 /* at 0xffee */
407 #define ISCP_ADDR       (0xffee)
408 #define ISCP_SIZE       (0x08)
409 static iscp_t init_iscp = {
410         1,
411         SCB_ADDR,
412         0x0000,
413         0x0000
414 };
415
416 /* at 0xfff6 */
417 #define SCP_ADDR        (0xfff6)
418 #define SCP_SIZE        (0x0a)
419 static scp_t  init_scp  = {
420         SCP_SY_16BBUS,
421         { 0, 0 },
422         ISCP_ADDR,
423         0
424 };
425
426 #define RFD_SIZE        (0x16)
427 static rfd_t  init_rfd  = {
428         0,
429         0,
430         0,
431         0,
432         { 0, },
433         { 0, },
434         0
435 };
436
437 #define RBD_SIZE        (0x0a)
438 static rbd_t  init_rbd  = {
439         0,
440         0,
441         0,
442         0,
443         ETH_FRAME_LEN + 8
444 };
445
446 #define TX_SIZE         (0x08)
447 #define TBD_SIZE        (0x08)
448
449 static int
450 ether1_init_for_open (struct net_device *dev)
451 {
452         int i, status, addr, next, next2;
453         int failures = 0;
454         unsigned long timeout;
455
456         writeb(CTRL_RST|CTRL_ACK, REG_CONTROL);
457
458         for (i = 0; i < 6; i++)
459                 init_sa.sa_addr[i] = dev->dev_addr[i];
460
461         /* load data structures into ether1 RAM */
462         ether1_writebuffer (dev, &init_scp,  SCP_ADDR,  SCP_SIZE);
463         ether1_writebuffer (dev, &init_iscp, ISCP_ADDR, ISCP_SIZE);
464         ether1_writebuffer (dev, &init_scb,  SCB_ADDR,  SCB_SIZE);
465         ether1_writebuffer (dev, &init_cfg,  CFG_ADDR,  CFG_SIZE);
466         ether1_writebuffer (dev, &init_sa,   SA_ADDR,   SA_SIZE);
467         ether1_writebuffer (dev, &init_mc,   MC_ADDR,   MC_SIZE);
468         ether1_writebuffer (dev, &init_tdr,  TDR_ADDR,  TDR_SIZE);
469         ether1_writebuffer (dev, &init_nop,  NOP_ADDR,  NOP_SIZE);
470
471         if (ether1_readw(dev, CFG_ADDR, cfg_t, cfg_command, NORMALIRQS) != CMD_CONFIG) {
472                 printk (KERN_ERR "%s: detected either RAM fault or compiler bug\n",
473                         dev->name);
474                 return 1;
475         }
476
477         /*
478          * setup circularly linked list of { rfd, rbd, buffer }, with
479          * all rfds circularly linked, rbds circularly linked.
480          * First rfd is linked to scp, first rbd is linked to first
481          * rfd.  Last rbd has a suspend command.
482          */
483         addr = RX_AREA_START;
484         do {
485                 next = addr + RFD_SIZE + RBD_SIZE + ETH_FRAME_LEN + 10;
486                 next2 = next + RFD_SIZE + RBD_SIZE + ETH_FRAME_LEN + 10;
487
488                 if (next2 >= RX_AREA_END) {
489                         next = RX_AREA_START;
490                         init_rfd.rfd_command = RFD_CMDEL | RFD_CMDSUSPEND;
491                         priv(dev)->rx_tail = addr;
492                 } else
493                         init_rfd.rfd_command = 0;
494                 if (addr == RX_AREA_START)
495                         init_rfd.rfd_rbdoffset = addr + RFD_SIZE;
496                 else
497                         init_rfd.rfd_rbdoffset = 0;
498                 init_rfd.rfd_link = next;
499                 init_rbd.rbd_link = next + RFD_SIZE;
500                 init_rbd.rbd_bufl = addr + RFD_SIZE + RBD_SIZE;
501
502                 ether1_writebuffer (dev, &init_rfd, addr, RFD_SIZE);
503                 ether1_writebuffer (dev, &init_rbd, addr + RFD_SIZE, RBD_SIZE);
504                 addr = next;
505         } while (next2 < RX_AREA_END);
506
507         priv(dev)->tx_link = NOP_ADDR;
508         priv(dev)->tx_head = NOP_ADDR + NOP_SIZE;
509         priv(dev)->tx_tail = TDR_ADDR;
510         priv(dev)->rx_head = RX_AREA_START;
511
512         /* release reset & give 586 a prod */
513         priv(dev)->resetting = 1;
514         priv(dev)->initialising = 1;
515         writeb(CTRL_RST, REG_CONTROL);
516         writeb(0, REG_CONTROL);
517         writeb(CTRL_CA, REG_CONTROL);
518
519         /* 586 should now unset iscp.busy */
520         timeout = jiffies + HZ/2;
521         while (ether1_readw(dev, ISCP_ADDR, iscp_t, iscp_busy, DISABLEIRQS) == 1) {
522                 if (time_after(jiffies, timeout)) {
523                         printk (KERN_WARNING "%s: can't initialise 82586: iscp is busy\n", dev->name);
524                         return 1;
525                 }
526         }
527
528         /* check status of commands that we issued */
529         timeout += HZ/10;
530         while (((status = ether1_readw(dev, CFG_ADDR, cfg_t, cfg_status, DISABLEIRQS))
531                         & STAT_COMPLETE) == 0) {
532                 if (time_after(jiffies, timeout))
533                         break;
534         }
535
536         if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
537                 printk (KERN_WARNING "%s: can't initialise 82586: config status %04X\n", dev->name, status);
538                 printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
539                         ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
540                         ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
541                         ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
542                         ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
543                 failures += 1;
544         }
545
546         timeout += HZ/10;
547         while (((status = ether1_readw(dev, SA_ADDR, sa_t, sa_status, DISABLEIRQS))
548                         & STAT_COMPLETE) == 0) {
549                 if (time_after(jiffies, timeout))
550                         break;
551         }
552
553         if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
554                 printk (KERN_WARNING "%s: can't initialise 82586: set address status %04X\n", dev->name, status);
555                 printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
556                         ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
557                         ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
558                         ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
559                         ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
560                 failures += 1;
561         }
562
563         timeout += HZ/10;
564         while (((status = ether1_readw(dev, MC_ADDR, mc_t, mc_status, DISABLEIRQS))
565                         & STAT_COMPLETE) == 0) {
566                 if (time_after(jiffies, timeout))
567                         break;
568         }
569
570         if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
571                 printk (KERN_WARNING "%s: can't initialise 82586: set multicast status %04X\n", dev->name, status);
572                 printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
573                         ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
574                         ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
575                         ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
576                         ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
577                 failures += 1;
578         }
579
580         timeout += HZ;
581         while (((status = ether1_readw(dev, TDR_ADDR, tdr_t, tdr_status, DISABLEIRQS))
582                         & STAT_COMPLETE) == 0) {
583                 if (time_after(jiffies, timeout))
584                         break;
585         }
586
587         if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
588                 printk (KERN_WARNING "%s: can't tdr (ignored)\n", dev->name);
589                 printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
590                         ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
591                         ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
592                         ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
593                         ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
594         } else {
595                 status = ether1_readw(dev, TDR_ADDR, tdr_t, tdr_result, DISABLEIRQS);
596                 if (status & TDR_XCVRPROB)
597                         printk (KERN_WARNING "%s: i/f failed tdr: transceiver problem\n", dev->name);
598                 else if ((status & (TDR_SHORT|TDR_OPEN)) && (status & TDR_TIME)) {
599 #ifdef FANCY
600                         printk (KERN_WARNING "%s: i/f failed tdr: cable %s %d.%d us away\n", dev->name,
601                                 status & TDR_SHORT ? "short" : "open", (status & TDR_TIME) / 10,
602                                 (status & TDR_TIME) % 10);
603 #else
604                         printk (KERN_WARNING "%s: i/f failed tdr: cable %s %d clks away\n", dev->name,
605                                 status & TDR_SHORT ? "short" : "open", (status & TDR_TIME));
606 #endif
607                 }
608         }
609
610         if (failures)
611                 ether1_reset (dev);
612         return failures ? 1 : 0;
613 }
614
615 /* ------------------------------------------------------------------------- */
616
617 static int
618 ether1_txalloc (struct net_device *dev, int size)
619 {
620         int start, tail;
621
622         size = (size + 1) & ~1;
623         tail = priv(dev)->tx_tail;
624
625         if (priv(dev)->tx_head + size > TX_AREA_END) {
626                 if (tail > priv(dev)->tx_head)
627                         return -1;
628                 start = TX_AREA_START;
629                 if (start + size > tail)
630                         return -1;
631                 priv(dev)->tx_head = start + size;
632         } else {
633                 if (priv(dev)->tx_head < tail && (priv(dev)->tx_head + size) > tail)
634                         return -1;
635                 start = priv(dev)->tx_head;
636                 priv(dev)->tx_head += size;
637         }
638
639         return start;
640 }
641
642 static int
643 ether1_open (struct net_device *dev)
644 {
645         if (!is_valid_ether_addr(dev->dev_addr)) {
646                 printk(KERN_WARNING "%s: invalid ethernet MAC address\n",
647                         dev->name);
648                 return -EINVAL;
649         }
650
651         if (request_irq(dev->irq, ether1_interrupt, 0, "ether1", dev))
652                 return -EAGAIN;
653
654         memset (&priv(dev)->stats, 0, sizeof (struct net_device_stats));
655
656         if (ether1_init_for_open (dev)) {
657                 free_irq (dev->irq, dev);
658                 return -EAGAIN;
659         }
660
661         netif_start_queue(dev);
662
663         return 0;
664 }
665
666 static void
667 ether1_timeout(struct net_device *dev)
668 {
669         printk(KERN_WARNING "%s: transmit timeout, network cable problem?\n",
670                 dev->name);
671         printk(KERN_WARNING "%s: resetting device\n", dev->name);
672
673         ether1_reset (dev);
674
675         if (ether1_init_for_open (dev))
676                 printk (KERN_ERR "%s: unable to restart interface\n", dev->name);
677
678         priv(dev)->stats.tx_errors++;
679         netif_wake_queue(dev);
680 }
681
682 static int
683 ether1_sendpacket (struct sk_buff *skb, struct net_device *dev)
684 {
685         int tmp, tst, nopaddr, txaddr, tbdaddr, dataddr;
686         unsigned long flags;
687         tx_t tx;
688         tbd_t tbd;
689         nop_t nop;
690
691         if (priv(dev)->restart) {
692                 printk(KERN_WARNING "%s: resetting device\n", dev->name);
693
694                 ether1_reset(dev);
695
696                 if (ether1_init_for_open(dev))
697                         printk(KERN_ERR "%s: unable to restart interface\n", dev->name);
698                 else
699                         priv(dev)->restart = 0;
700         }
701
702         if (skb->len < ETH_ZLEN) {
703                 if (skb_padto(skb, ETH_ZLEN))
704                         goto out;
705         }
706
707         /*
708          * insert packet followed by a nop
709          */
710         txaddr = ether1_txalloc (dev, TX_SIZE);
711         tbdaddr = ether1_txalloc (dev, TBD_SIZE);
712         dataddr = ether1_txalloc (dev, skb->len);
713         nopaddr = ether1_txalloc (dev, NOP_SIZE);
714
715         tx.tx_status = 0;
716         tx.tx_command = CMD_TX | CMD_INTR;
717         tx.tx_link = nopaddr;
718         tx.tx_tbdoffset = tbdaddr;
719         tbd.tbd_opts = TBD_EOL | skb->len;
720         tbd.tbd_link = I82586_NULL;
721         tbd.tbd_bufl = dataddr;
722         tbd.tbd_bufh = 0;
723         nop.nop_status = 0;
724         nop.nop_command = CMD_NOP;
725         nop.nop_link = nopaddr;
726
727         local_irq_save(flags);
728         ether1_writebuffer (dev, &tx, txaddr, TX_SIZE);
729         ether1_writebuffer (dev, &tbd, tbdaddr, TBD_SIZE);
730         ether1_writebuffer (dev, skb->data, dataddr, skb->len);
731         ether1_writebuffer (dev, &nop, nopaddr, NOP_SIZE);
732         tmp = priv(dev)->tx_link;
733         priv(dev)->tx_link = nopaddr;
734
735         /* now reset the previous nop pointer */
736         ether1_writew(dev, txaddr, tmp, nop_t, nop_link, NORMALIRQS);
737
738         local_irq_restore(flags);
739
740         /* handle transmit */
741         dev->trans_start = jiffies;
742
743         /* check to see if we have room for a full sized ether frame */
744         tmp = priv(dev)->tx_head;
745         tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
746         priv(dev)->tx_head = tmp;
747         dev_kfree_skb (skb);
748
749         if (tst == -1)
750                 netif_stop_queue(dev);
751
752  out:
753         return 0;
754 }
755
756 static void
757 ether1_xmit_done (struct net_device *dev)
758 {
759         nop_t nop;
760         int caddr, tst;
761
762         caddr = priv(dev)->tx_tail;
763
764 again:
765         ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
766
767         switch (nop.nop_command & CMD_MASK) {
768         case CMD_TDR:
769                 /* special case */
770                 if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
771                                 != (unsigned short)I82586_NULL) {
772                         ether1_writew(dev, SCB_CMDCUCSTART | SCB_CMDRXSTART, SCB_ADDR, scb_t,
773                                     scb_command, NORMALIRQS);
774                         writeb(CTRL_CA, REG_CONTROL);
775                 }
776                 priv(dev)->tx_tail = NOP_ADDR;
777                 return;
778
779         case CMD_NOP:
780                 if (nop.nop_link == caddr) {
781                         if (priv(dev)->initialising == 0)
782                                 printk (KERN_WARNING "%s: strange command complete with no tx command!\n", dev->name);
783                         else
784                                 priv(dev)->initialising = 0;
785                         return;
786                 }
787                 if (caddr == nop.nop_link)
788                         return;
789                 caddr = nop.nop_link;
790                 goto again;
791
792         case CMD_TX:
793                 if (nop.nop_status & STAT_COMPLETE)
794                         break;
795                 printk (KERN_ERR "%s: strange command complete without completed command\n", dev->name);
796                 priv(dev)->restart = 1;
797                 return;
798
799         default:
800                 printk (KERN_WARNING "%s: strange command %d complete! (offset %04X)", dev->name,
801                         nop.nop_command & CMD_MASK, caddr);
802                 priv(dev)->restart = 1;
803                 return;
804         }
805
806         while (nop.nop_status & STAT_COMPLETE) {
807                 if (nop.nop_status & STAT_OK) {
808                         priv(dev)->stats.tx_packets ++;
809                         priv(dev)->stats.collisions += (nop.nop_status & STAT_COLLISIONS);
810                 } else {
811                         priv(dev)->stats.tx_errors ++;
812
813                         if (nop.nop_status & STAT_COLLAFTERTX)
814                                 priv(dev)->stats.collisions ++;
815                         if (nop.nop_status & STAT_NOCARRIER)
816                                 priv(dev)->stats.tx_carrier_errors ++;
817                         if (nop.nop_status & STAT_TXLOSTCTS)
818                                 printk (KERN_WARNING "%s: cts lost\n", dev->name);
819                         if (nop.nop_status & STAT_TXSLOWDMA)
820                                 priv(dev)->stats.tx_fifo_errors ++;
821                         if (nop.nop_status & STAT_COLLEXCESSIVE)
822                                 priv(dev)->stats.collisions += 16;
823                 }
824
825                 if (nop.nop_link == caddr) {
826                         printk (KERN_ERR "%s: tx buffer chaining error: tx command points to itself\n", dev->name);
827                         break;
828                 }
829
830                 caddr = nop.nop_link;
831                 ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
832                 if ((nop.nop_command & CMD_MASK) != CMD_NOP) {
833                         printk (KERN_ERR "%s: tx buffer chaining error: no nop after tx command\n", dev->name);
834                         break;
835                 }
836
837                 if (caddr == nop.nop_link)
838                         break;
839
840                 caddr = nop.nop_link;
841                 ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
842                 if ((nop.nop_command & CMD_MASK) != CMD_TX) {
843                         printk (KERN_ERR "%s: tx buffer chaining error: no tx command after nop\n", dev->name);
844                         break;
845                 }
846         }
847         priv(dev)->tx_tail = caddr;
848
849         caddr = priv(dev)->tx_head;
850         tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
851         priv(dev)->tx_head = caddr;
852         if (tst != -1)
853                 netif_wake_queue(dev);
854 }
855
856 static void
857 ether1_recv_done (struct net_device *dev)
858 {
859         int status;
860         int nexttail, rbdaddr;
861         rbd_t rbd;
862
863         do {
864                 status = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_status, NORMALIRQS);
865                 if ((status & RFD_COMPLETE) == 0)
866                         break;
867
868                 rbdaddr = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_rbdoffset, NORMALIRQS);
869                 ether1_readbuffer (dev, &rbd, rbdaddr, RBD_SIZE);
870
871                 if ((rbd.rbd_status & (RBD_EOF | RBD_ACNTVALID)) == (RBD_EOF | RBD_ACNTVALID)) {
872                         int length = rbd.rbd_status & RBD_ACNT;
873                         struct sk_buff *skb;
874
875                         length = (length + 1) & ~1;
876                         skb = dev_alloc_skb (length + 2);
877
878                         if (skb) {
879                                 skb->dev = dev;
880                                 skb_reserve (skb, 2);
881
882                                 ether1_readbuffer (dev, skb_put (skb, length), rbd.rbd_bufl, length);
883
884                                 skb->protocol = eth_type_trans (skb, dev);
885                                 netif_rx (skb);
886                                 priv(dev)->stats.rx_packets ++;
887                         } else
888                                 priv(dev)->stats.rx_dropped ++;
889                 } else {
890                         printk(KERN_WARNING "%s: %s\n", dev->name,
891                                 (rbd.rbd_status & RBD_EOF) ? "oversized packet" : "acnt not valid");
892                         priv(dev)->stats.rx_dropped ++;
893                 }
894
895                 nexttail = ether1_readw(dev, priv(dev)->rx_tail, rfd_t, rfd_link, NORMALIRQS);
896                 /* nexttail should be rx_head */
897                 if (nexttail != priv(dev)->rx_head)
898                         printk(KERN_ERR "%s: receiver buffer chaining error (%04X != %04X)\n",
899                                 dev->name, nexttail, priv(dev)->rx_head);
900                 ether1_writew(dev, RFD_CMDEL | RFD_CMDSUSPEND, nexttail, rfd_t, rfd_command, NORMALIRQS);
901                 ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_command, NORMALIRQS);
902                 ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_status, NORMALIRQS);
903                 ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_rbdoffset, NORMALIRQS);
904         
905                 priv(dev)->rx_tail = nexttail;
906                 priv(dev)->rx_head = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_link, NORMALIRQS);
907         } while (1);
908 }
909
910 static irqreturn_t
911 ether1_interrupt (int irq, void *dev_id)
912 {
913         struct net_device *dev = (struct net_device *)dev_id;
914         int status;
915
916         status = ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS);
917
918         if (status) {
919                 ether1_writew(dev, status & (SCB_STRNR | SCB_STCNA | SCB_STFR | SCB_STCX),
920                             SCB_ADDR, scb_t, scb_command, NORMALIRQS);
921                 writeb(CTRL_CA | CTRL_ACK, REG_CONTROL);
922                 if (status & SCB_STCX) {
923                         ether1_xmit_done (dev);
924                 }
925                 if (status & SCB_STCNA) {
926                         if (priv(dev)->resetting == 0)
927                                 printk (KERN_WARNING "%s: CU went not ready ???\n", dev->name);
928                         else
929                                 priv(dev)->resetting += 1;
930                         if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
931                                         != (unsigned short)I82586_NULL) {
932                                 ether1_writew(dev, SCB_CMDCUCSTART, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
933                                 writeb(CTRL_CA, REG_CONTROL);
934                         }
935                         if (priv(dev)->resetting == 2)
936                                 priv(dev)->resetting = 0;
937                 }
938                 if (status & SCB_STFR) {
939                         ether1_recv_done (dev);
940                 }
941                 if (status & SCB_STRNR) {
942                         if (ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS) & SCB_STRXSUSP) {
943                                 printk (KERN_WARNING "%s: RU went not ready: RU suspended\n", dev->name);
944                                 ether1_writew(dev, SCB_CMDRXRESUME, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
945                                 writeb(CTRL_CA, REG_CONTROL);
946                                 priv(dev)->stats.rx_dropped ++; /* we suspended due to lack of buffer space */
947                         } else
948                                 printk(KERN_WARNING "%s: RU went not ready: %04X\n", dev->name,
949                                         ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS));
950                         printk (KERN_WARNING "RU ptr = %04X\n", ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset,
951                                                 NORMALIRQS));
952                 }
953         } else
954                 writeb(CTRL_ACK, REG_CONTROL);
955
956         return IRQ_HANDLED;
957 }
958
959 static int
960 ether1_close (struct net_device *dev)
961 {
962         ether1_reset (dev);
963
964         free_irq(dev->irq, dev);
965
966         return 0;
967 }
968
969 static struct net_device_stats *
970 ether1_getstats (struct net_device *dev)
971 {
972         return &priv(dev)->stats;
973 }
974
975 /*
976  * Set or clear the multicast filter for this adaptor.
977  * num_addrs == -1      Promiscuous mode, receive all packets.
978  * num_addrs == 0       Normal mode, clear multicast list.
979  * num_addrs > 0        Multicast mode, receive normal and MC packets, and do
980  *                      best-effort filtering.
981  */
982 static void
983 ether1_setmulticastlist (struct net_device *dev)
984 {
985 }
986
987 /* ------------------------------------------------------------------------- */
988
989 static void __init ether1_banner(void)
990 {
991         static unsigned int version_printed = 0;
992
993         if (net_debug && version_printed++ == 0)
994                 printk(KERN_INFO "%s", version);
995 }
996
997 static int __devinit
998 ether1_probe(struct expansion_card *ec, const struct ecard_id *id)
999 {
1000         struct net_device *dev;
1001         int i, ret = 0;
1002
1003         ether1_banner();
1004
1005         ret = ecard_request_resources(ec);
1006         if (ret)
1007                 goto out;
1008
1009         dev = alloc_etherdev(sizeof(struct ether1_priv));
1010         if (!dev) {
1011                 ret = -ENOMEM;
1012                 goto release;
1013         }
1014
1015         SET_MODULE_OWNER(dev);
1016         SET_NETDEV_DEV(dev, &ec->dev);
1017
1018         dev->irq = ec->irq;
1019         priv(dev)->base = ioremap(ecard_resource_start(ec, ECARD_RES_IOCFAST),
1020                                   ecard_resource_len(ec, ECARD_RES_IOCFAST));
1021         if (!priv(dev)->base) {
1022                 ret = -ENOMEM;
1023                 goto free;
1024         }
1025
1026         if ((priv(dev)->bus_type = ether1_reset(dev)) == 0) {
1027                 ret = -ENODEV;
1028                 goto free;
1029         }
1030
1031         for (i = 0; i < 6; i++)
1032                 dev->dev_addr[i] = readb(IDPROM_ADDRESS + (i << 2));
1033
1034         if (ether1_init_2(dev)) {
1035                 ret = -ENODEV;
1036                 goto free;
1037         }
1038
1039         dev->open               = ether1_open;
1040         dev->stop               = ether1_close;
1041         dev->hard_start_xmit    = ether1_sendpacket;
1042         dev->get_stats          = ether1_getstats;
1043         dev->set_multicast_list = ether1_setmulticastlist;
1044         dev->tx_timeout         = ether1_timeout;
1045         dev->watchdog_timeo     = 5 * HZ / 100;
1046
1047         ret = register_netdev(dev);
1048         if (ret)
1049                 goto free;
1050
1051         printk(KERN_INFO "%s: ether1 in slot %d, ",
1052                 dev->name, ec->slot_no);
1053     
1054         for (i = 0; i < 6; i++)
1055                 printk ("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
1056
1057         ecard_set_drvdata(ec, dev);
1058         return 0;
1059
1060  free:
1061         if (priv(dev)->base)
1062                 iounmap(priv(dev)->base);
1063         free_netdev(dev);
1064  release:
1065         ecard_release_resources(ec);
1066  out:
1067         return ret;
1068 }
1069
1070 static void __devexit ether1_remove(struct expansion_card *ec)
1071 {
1072         struct net_device *dev = ecard_get_drvdata(ec);
1073
1074         ecard_set_drvdata(ec, NULL);    
1075
1076         unregister_netdev(dev);
1077         iounmap(priv(dev)->base);
1078         free_netdev(dev);
1079         ecard_release_resources(ec);
1080 }
1081
1082 static const struct ecard_id ether1_ids[] = {
1083         { MANU_ACORN, PROD_ACORN_ETHER1 },
1084         { 0xffff, 0xffff }
1085 };
1086
1087 static struct ecard_driver ether1_driver = {
1088         .probe          = ether1_probe,
1089         .remove         = __devexit_p(ether1_remove),
1090         .id_table       = ether1_ids,
1091         .drv = {
1092                 .name   = "ether1",
1093         },
1094 };
1095
1096 static int __init ether1_init(void)
1097 {
1098         return ecard_register_driver(&ether1_driver);
1099 }
1100
1101 static void __exit ether1_exit(void)
1102 {
1103         ecard_remove_driver(&ether1_driver);
1104 }
1105
1106 module_init(ether1_init);
1107 module_exit(ether1_exit);
1108
1109 MODULE_LICENSE("GPL");