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