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