File: /usr/src/linux/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 (request_irq(dev->irq, ether1_interrupt, 0, "ether1", dev))
651     		return -EAGAIN;
652     
653     	memset (&priv->stats, 0, sizeof (struct net_device_stats));
654     
655     	if (ether1_init_for_open (dev)) {
656     		free_irq (dev->irq, dev);
657     		return -EAGAIN;
658     	}
659     
660     	netif_start_queue(dev);
661     
662     	return 0;
663     }
664     
665     static void
666     ether1_timeout(struct net_device *dev)
667     {
668     	struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
669     
670     	printk(KERN_WARNING "%s: transmit timeout, network cable problem?\n",
671     		dev->name);
672     	printk(KERN_WARNING "%s: resetting device\n", dev->name);
673     
674     	ether1_reset (dev);
675     
676     	if (ether1_init_for_open (dev))
677     		printk (KERN_ERR "%s: unable to restart interface\n", dev->name);
678     
679     	priv->stats.tx_errors++;
680     	netif_wake_queue(dev);
681     }
682     
683     static int
684     ether1_sendpacket (struct sk_buff *skb, struct net_device *dev)
685     {
686     	struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
687     	int len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
688     	int tmp, tst, nopaddr, txaddr, tbdaddr, dataddr;
689     	unsigned long flags;
690     	tx_t tx;
691     	tbd_t tbd;
692     	nop_t nop;
693     
694     	if (priv->restart) {
695     		printk(KERN_WARNING "%s: resetting device\n", dev->name);
696     
697     		ether1_reset(dev);
698     
699     		if (ether1_init_for_open(dev))
700     			printk(KERN_ERR "%s: unable to restart interface\n", dev->name);
701     		else
702     			priv->restart = 0;
703     	}
704     
705     	/*
706     	 * insert packet followed by a nop
707     	 */
708     	txaddr = ether1_txalloc (dev, TX_SIZE);
709     	tbdaddr = ether1_txalloc (dev, TBD_SIZE);
710     	dataddr = ether1_txalloc (dev, len);
711     	nopaddr = ether1_txalloc (dev, NOP_SIZE);
712     
713     	tx.tx_status = 0;
714     	tx.tx_command = CMD_TX | CMD_INTR;
715     	tx.tx_link = nopaddr;
716     	tx.tx_tbdoffset = tbdaddr;
717     	tbd.tbd_opts = TBD_EOL | len;
718     	tbd.tbd_link = I82586_NULL;
719     	tbd.tbd_bufl = dataddr;
720     	tbd.tbd_bufh = 0;
721     	nop.nop_status = 0;
722     	nop.nop_command = CMD_NOP;
723     	nop.nop_link = nopaddr;
724     
725     	save_flags_cli(flags);
726     	ether1_writebuffer (dev, &tx, txaddr, TX_SIZE);
727     	ether1_writebuffer (dev, &tbd, tbdaddr, TBD_SIZE);
728     	ether1_writebuffer (dev, skb->data, dataddr, len);
729     	ether1_writebuffer (dev, &nop, nopaddr, NOP_SIZE);
730     	tmp = priv->tx_link;
731     	priv->tx_link = nopaddr;
732     
733     	/* now reset the previous nop pointer */
734     	ether1_outw (dev, txaddr, tmp, nop_t, nop_link, NORMALIRQS);
735     
736     	restore_flags(flags);
737     
738     	/* handle transmit */
739     	dev->trans_start = jiffies;
740     
741     	/* check to see if we have room for a full sized ether frame */
742     	tmp = priv->tx_head;
743     	tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
744     	priv->tx_head = tmp;
745     	dev_kfree_skb (skb);
746     
747     	if (tst == -1)
748     		netif_stop_queue(dev);
749     
750     	return 0;
751     }
752     
753     static void
754     ether1_xmit_done (struct net_device *dev)
755     {
756     	struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
757     	nop_t nop;
758     	int caddr, tst;
759     
760     	caddr = priv->tx_tail;
761     
762     again:
763     	ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
764     
765     	switch (nop.nop_command & CMD_MASK) {
766     	case CMD_TDR:
767     		/* special case */
768     		if (ether1_inw (dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
769     				!= (unsigned short)I82586_NULL) {
770     			ether1_outw(dev, SCB_CMDCUCSTART | SCB_CMDRXSTART, SCB_ADDR, scb_t,
771     				    scb_command, NORMALIRQS);
772     			outb (CTRL_CA, REG_CONTROL);
773     		}
774     		priv->tx_tail = NOP_ADDR;
775     		return;
776     
777     	case CMD_NOP:
778     		if (nop.nop_link == caddr) {
779     			if (priv->initialising == 0)
780     				printk (KERN_WARNING "%s: strange command complete with no tx command!\n", dev->name);
781     			else
782     			        priv->initialising = 0;
783     			return;
784     		}
785     		if (caddr == nop.nop_link)
786     			return;
787     		caddr = nop.nop_link;
788     		goto again;
789     
790     	case CMD_TX:
791     		if (nop.nop_status & STAT_COMPLETE)
792     			break;
793     		printk (KERN_ERR "%s: strange command complete without completed command\n", dev->name);
794     		priv->restart = 1;
795     		return;
796     
797     	default:
798     		printk (KERN_WARNING "%s: strange command %d complete! (offset %04X)", dev->name,
799     			nop.nop_command & CMD_MASK, caddr);
800     		priv->restart = 1;
801     		return;
802     	}
803     
804     	while (nop.nop_status & STAT_COMPLETE) {
805     		if (nop.nop_status & STAT_OK) {
806     			priv->stats.tx_packets ++;
807     			priv->stats.collisions += (nop.nop_status & STAT_COLLISIONS);
808     		} else {
809     			priv->stats.tx_errors ++;
810     
811     			if (nop.nop_status & STAT_COLLAFTERTX)
812     				priv->stats.collisions ++;
813     			if (nop.nop_status & STAT_NOCARRIER)
814     				priv->stats.tx_carrier_errors ++;
815     			if (nop.nop_status & STAT_TXLOSTCTS)
816     				printk (KERN_WARNING "%s: cts lost\n", dev->name);
817     			if (nop.nop_status & STAT_TXSLOWDMA)
818     				priv->stats.tx_fifo_errors ++;
819     			if (nop.nop_status & STAT_COLLEXCESSIVE)
820     				priv->stats.collisions += 16;
821     		}
822     
823     		if (nop.nop_link == caddr) {
824     			printk (KERN_ERR "%s: tx buffer chaining error: tx command points to itself\n", dev->name);
825     			break;
826     		}
827     
828     		caddr = nop.nop_link;
829     		ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
830     		if ((nop.nop_command & CMD_MASK) != CMD_NOP) {
831     			printk (KERN_ERR "%s: tx buffer chaining error: no nop after tx command\n", dev->name);
832     			break;
833     		}
834     
835     		if (caddr == nop.nop_link)
836     			break;
837     
838     		caddr = nop.nop_link;
839     		ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
840     		if ((nop.nop_command & CMD_MASK) != CMD_TX) {
841     			printk (KERN_ERR "%s: tx buffer chaining error: no tx command after nop\n", dev->name);
842     			break;
843     		}
844     	}
845     	priv->tx_tail = caddr;
846     
847     	caddr = priv->tx_head;
848     	tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
849     	priv->tx_head = caddr;
850     	if (tst != -1)
851     		netif_wake_queue(dev);
852     }
853     
854     static void
855     ether1_recv_done (struct net_device *dev)
856     {
857     	struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
858     	int status;
859     	int nexttail, rbdaddr;
860     	rbd_t rbd;
861     
862     	do {
863     		status = ether1_inw (dev, priv->rx_head, rfd_t, rfd_status, NORMALIRQS);
864     		if ((status & RFD_COMPLETE) == 0)
865     			break;
866     
867     		rbdaddr = ether1_inw (dev, priv->rx_head, rfd_t, rfd_rbdoffset, NORMALIRQS);
868     		ether1_readbuffer (dev, &rbd, rbdaddr, RBD_SIZE);
869     
870     		if ((rbd.rbd_status & (RBD_EOF | RBD_ACNTVALID)) == (RBD_EOF | RBD_ACNTVALID)) {
871     			int length = rbd.rbd_status & RBD_ACNT;
872     			struct sk_buff *skb;
873     
874     			length = (length + 1) & ~1;
875     			skb = dev_alloc_skb (length + 2);
876     
877     			if (skb) {
878     				skb->dev = dev;
879     				skb_reserve (skb, 2);
880     
881     				ether1_readbuffer (dev, skb_put (skb, length), rbd.rbd_bufl, length);
882     
883     				skb->protocol = eth_type_trans (skb, dev);
884     				netif_rx (skb);
885     				priv->stats.rx_packets ++;
886     			} else
887     				priv->stats.rx_dropped ++;
888     		} else {
889     			printk(KERN_WARNING "%s: %s\n", dev->name,
890     				(rbd.rbd_status & RBD_EOF) ? "oversized packet" : "acnt not valid");
891     			priv->stats.rx_dropped ++;
892     		}
893     
894     		nexttail = ether1_inw (dev, priv->rx_tail, rfd_t, rfd_link, NORMALIRQS);
895     		/* nexttail should be rx_head */
896     		if (nexttail != priv->rx_head)
897     			printk(KERN_ERR "%s: receiver buffer chaining error (%04X != %04X)\n",
898     				dev->name, nexttail, priv->rx_head);
899     		ether1_outw (dev, RFD_CMDEL | RFD_CMDSUSPEND, nexttail, rfd_t, rfd_command, NORMALIRQS);
900     		ether1_outw (dev, 0, priv->rx_tail, rfd_t, rfd_command, NORMALIRQS);
901     		ether1_outw (dev, 0, priv->rx_tail, rfd_t, rfd_status, NORMALIRQS);
902     		ether1_outw (dev, 0, priv->rx_tail, rfd_t, rfd_rbdoffset, NORMALIRQS);
903     	
904     		priv->rx_tail = nexttail;
905     		priv->rx_head = ether1_inw (dev, priv->rx_head, rfd_t, rfd_link, NORMALIRQS);
906     	} while (1);
907     }
908     
909     static void
910     ether1_interrupt (int irq, void *dev_id, struct pt_regs *regs)
911     {
912     	struct net_device *dev = (struct net_device *)dev_id;
913     	struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
914     	int status;
915     
916     	status = ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS);
917     
918     	if (status) {
919     		ether1_outw(dev, status & (SCB_STRNR | SCB_STCNA | SCB_STFR | SCB_STCX),
920     			    SCB_ADDR, scb_t, scb_command, NORMALIRQS);
921     		outb (CTRL_CA | CTRL_ACK, REG_CONTROL);
922     		if (status & SCB_STCX) {
923     			ether1_xmit_done (dev);
924     		}
925     		if (status & SCB_STCNA) {
926     			if (priv->resetting == 0)
927     				printk (KERN_WARNING "%s: CU went not ready ???\n", dev->name);
928     			else
929     				priv->resetting += 1;
930     			if (ether1_inw (dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
931     					!= (unsigned short)I82586_NULL) {
932     				ether1_outw (dev, SCB_CMDCUCSTART, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
933     				outb (CTRL_CA, REG_CONTROL);
934     			}
935     			if (priv->resetting == 2)
936     				priv->resetting = 0;
937     		}
938     		if (status & SCB_STFR) {
939     			ether1_recv_done (dev);
940     		}
941     		if (status & SCB_STRNR) {
942     			if (ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS) & SCB_STRXSUSP) {
943     				printk (KERN_WARNING "%s: RU went not ready: RU suspended\n", dev->name);
944     				ether1_outw (dev, SCB_CMDRXRESUME, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
945     				outb (CTRL_CA, REG_CONTROL);
946     				priv->stats.rx_dropped ++;	/* we suspended due to lack of buffer space */
947     			} else
948     				printk(KERN_WARNING "%s: RU went not ready: %04X\n", dev->name,
949     					ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS));
950     			printk (KERN_WARNING "RU ptr = %04X\n", ether1_inw (dev, SCB_ADDR, scb_t, scb_rfa_offset,
951     						NORMALIRQS));
952     		}
953     	} else
954     	        outb (CTRL_ACK, REG_CONTROL);
955     }
956     
957     static int
958     ether1_close (struct net_device *dev)
959     {
960     	ether1_reset (dev);
961     
962     	free_irq(dev->irq, dev);
963     
964     	return 0;
965     }
966     
967     static struct net_device_stats *
968     ether1_getstats (struct net_device *dev)
969     {
970     	struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
971     	return &priv->stats;
972     }
973     
974     /*
975      * Set or clear the multicast filter for this adaptor.
976      * num_addrs == -1	Promiscuous mode, receive all packets.
977      * num_addrs == 0	Normal mode, clear multicast list.
978      * num_addrs > 0	Multicast mode, receive normal and MC packets, and do
979      *			best-effort filtering.
980      */
981     static void
982     ether1_setmulticastlist (struct net_device *dev)
983     {
984     }
985     
986     /* ------------------------------------------------------------------------- */
987     
988     static void __init ether1_banner(void)
989     {
990     	static unsigned int version_printed = 0;
991     
992     	if (net_debug && version_printed++ == 0)
993     		printk(KERN_INFO "%s", version);
994     }
995     
996     static struct net_device * __init ether1_init_one(struct expansion_card *ec)
997     {
998     	struct net_device *dev;
999     	struct ether1_priv *priv;
1000     	int i;
1001     
1002     	ether1_banner();
1003     
1004     	ecard_claim(ec);
1005     
1006     	dev = init_etherdev(NULL, sizeof(struct ether1_priv));
1007     	if (!dev)
1008     		goto out;
1009     
1010     	SET_MODULE_OWNER(dev);
1011     
1012     	dev->base_addr	= ecard_address(ec, ECARD_IOC, ECARD_FAST);
1013     	dev->irq	= ec->irq;
1014     
1015     	/*
1016     	 * these will not fail - the nature of the bus ensures this
1017     	 */
1018     	request_region(dev->base_addr, 16, dev->name);
1019     	request_region(dev->base_addr + 0x800, 4096, dev->name);
1020     
1021     	priv = (struct ether1_priv *)dev->priv;
1022     	if ((priv->bus_type = ether1_reset(dev)) == 0)
1023     		goto release;
1024     
1025     	printk(KERN_INFO "%s: ether1 in slot %d, ",
1026     		dev->name, ec->slot_no);
1027         
1028     	for (i = 0; i < 6; i++) {
1029     		dev->dev_addr[i] = inb(IDPROM_ADDRESS + i);
1030     		printk ("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
1031     	}
1032     
1033     	if (ether1_init_2(dev))
1034     		goto release;
1035     
1036     	dev->open		= ether1_open;
1037     	dev->stop		= ether1_close;
1038     	dev->hard_start_xmit    = ether1_sendpacket;
1039     	dev->get_stats		= ether1_getstats;
1040     	dev->set_multicast_list = ether1_setmulticastlist;
1041     	dev->tx_timeout		= ether1_timeout;
1042     	dev->watchdog_timeo	= 5 * HZ / 100;
1043     	return 0;
1044     
1045     release:
1046     	release_region(dev->base_addr, 16);
1047     	release_region(dev->base_addr + 0x800, 4096);
1048     	unregister_netdev(dev);
1049     	kfree(dev);
1050     out:
1051     	ecard_release(ec);
1052     	return dev;
1053     }
1054     
1055     static struct expansion_card	*e_card[MAX_ECARDS];
1056     static struct net_device	*e_dev[MAX_ECARDS];
1057     
1058     static int __init ether1_init(void)
1059     {
1060     	int i, ret = -ENODEV;
1061     
1062     	ecard_startfind();
1063     
1064     	for (i = 0; i < MAX_ECARDS; i++) {
1065     		struct expansion_card *ec;
1066     		struct net_device *dev;
1067     
1068     		ec = ecard_find(0, ether1_cids);
1069     		if (!ec)
1070     			break;
1071     
1072     		dev = ether1_init_one(ec);
1073     		if (!dev)
1074     			break;
1075     
1076     		e_card[i] = ec;
1077     		e_dev[i]  = dev;
1078     		ret = 0;
1079     	}
1080     
1081     	return ret;
1082     }
1083     
1084     static void __exit ether1_exit(void)
1085     {
1086     	int i;
1087     
1088     	for (i = 0; i < MAX_ECARDS; i++) {
1089     		if (e_dev[i]) {
1090     			unregister_netdev(e_dev[i]);
1091     			release_region(e_dev[i]->base_addr, 16);
1092     			release_region(e_dev[i]->base_addr + 0x800, 4096);
1093     			kfree(e_dev[i]);
1094     			e_dev[i] = NULL;
1095     		}
1096     		if (e_card[i]) {
1097     			ecard_release(e_card[i]);
1098     			e_card[i] = NULL;
1099     		}
1100     	}
1101     }
1102     
1103     module_init(ether1_init);
1104     module_exit(ether1_exit);
1105     
1106     MODULE_LICENSE("GPL");
1107