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