Linux Audio

Check our new training course

Loading...
v6.13.7
  1/* sun3lance.c: Ethernet driver for SUN3 Lance chip */
  2/*
  3
  4  Sun3 Lance ethernet driver, by Sam Creasey (sammy@users.qual.net).
  5  This driver is a part of the linux kernel, and is thus distributed
  6  under the GNU General Public License.
  7
  8  The values used in LANCE_OBIO and LANCE_IRQ seem to be empirically
  9  true for the correct IRQ and address of the lance registers.  They
 10  have not been widely tested, however.  What we probably need is a
 11  "proper" way to search for a device in the sun3's prom, but, alas,
 12  linux has no such thing.
 13
 14  This driver is largely based on atarilance.c, by Roman Hodek.  Other
 15  sources of inspiration were the NetBSD sun3 am7990 driver, and the
 16  linux sparc lance driver (sunlance.c).
 17
 18  There are more assumptions made throughout this driver, it almost
 19  certainly still needs work, but it does work at least for RARP/BOOTP and
 20  mounting the root NFS filesystem.
 21
 22*/
 23
 24static const char version[] =
 25"sun3lance.c: v1.2 1/12/2001  Sam Creasey (sammy@sammy.net)\n";
 26
 27#include <linux/module.h>
 28#include <linux/stddef.h>
 29#include <linux/kernel.h>
 30#include <linux/string.h>
 31#include <linux/errno.h>
 32#include <linux/interrupt.h>
 33#include <linux/init.h>
 34#include <linux/ioport.h>
 35#include <linux/delay.h>
 36#include <linux/netdevice.h>
 37#include <linux/etherdevice.h>
 38#include <linux/skbuff.h>
 39#include <linux/bitops.h>
 40#include <linux/pgtable.h>
 41
 42#include <asm/cacheflush.h>
 43#include <asm/setup.h>
 44#include <asm/irq.h>
 45#include <asm/io.h>
 46#include <asm/dvma.h>
 47#include <asm/idprom.h>
 48#include <asm/machines.h>
 49
 50#ifdef CONFIG_SUN3
 51#include <asm/sun3mmu.h>
 52#else
 53#include <asm/sun3xprom.h>
 54#endif
 55
 56/* sun3/60 addr/irq for the lance chip.  If your sun is different,
 57   change this. */
 58#define LANCE_OBIO 0x120000
 59#define LANCE_IRQ IRQ_AUTO_3
 60
 61/* Debug level:
 62 *  0 = silent, print only serious errors
 63 *  1 = normal, print error messages
 64 *  2 = debug, print debug infos
 65 *  3 = debug, print even more debug infos (packet data)
 66 */
 67
 68#define	LANCE_DEBUG	0
 69
 70#ifdef LANCE_DEBUG
 71static int lance_debug = LANCE_DEBUG;
 72#else
 73static int lance_debug = 1;
 74#endif
 75module_param(lance_debug, int, 0);
 76MODULE_PARM_DESC(lance_debug, "SUN3 Lance debug level (0-3)");
 77MODULE_DESCRIPTION("Sun3/Sun3x on-board LANCE Ethernet driver");
 78MODULE_LICENSE("GPL");
 79
 80#define	DPRINTK(n,a) \
 81	do {  \
 82		if (lance_debug >= n)  \
 83			printk a; \
 84	} while( 0 )
 85
 86
 87/* we're only using 32k of memory, so we use 4 TX
 88   buffers and 16 RX buffers.  These values are expressed as log2. */
 89
 90#define TX_LOG_RING_SIZE			3
 91#define RX_LOG_RING_SIZE			5
 92
 93/* These are the derived values */
 94
 95#define TX_RING_SIZE			(1 << TX_LOG_RING_SIZE)
 96#define TX_RING_LEN_BITS		(TX_LOG_RING_SIZE << 5)
 97#define	TX_RING_MOD_MASK		(TX_RING_SIZE - 1)
 98
 99#define RX_RING_SIZE			(1 << RX_LOG_RING_SIZE)
100#define RX_RING_LEN_BITS		(RX_LOG_RING_SIZE << 5)
101#define	RX_RING_MOD_MASK		(RX_RING_SIZE - 1)
102
103/* Definitions for packet buffer access: */
104#define PKT_BUF_SZ		1544
105
106/* Get the address of a packet buffer corresponding to a given buffer head */
107#define	PKTBUF_ADDR(head)	(void *)((unsigned long)(MEM) | (head)->base)
108
109
110/* The LANCE Rx and Tx ring descriptors. */
111struct lance_rx_head {
112	unsigned short	base;		/* Low word of base addr */
113	volatile unsigned char	flag;
114	unsigned char  base_hi;	/* High word of base addr (unused) */
115	short buf_length;	/* This length is 2s complement! */
116	volatile short msg_length;	/* This length is "normal". */
117};
118
119struct lance_tx_head {
120	unsigned short base;		/* Low word of base addr */
121	volatile unsigned char	flag;
122	unsigned char base_hi;	/* High word of base addr (unused) */
123	short length;		/* Length is 2s complement! */
124	volatile short misc;
125};
126
127/* The LANCE initialization block, described in databook. */
128struct lance_init_block {
129	unsigned short	mode;		/* Pre-set mode */
130	unsigned char	hwaddr[6];	/* Physical ethernet address */
131	unsigned int    filter[2];	/* Multicast filter (unused). */
132	/* Receive and transmit ring base, along with length bits. */
133	unsigned short rdra;
134	unsigned short rlen;
135	unsigned short tdra;
136	unsigned short tlen;
137	unsigned short pad[4]; /* is thie needed? */
138};
139
140/* The whole layout of the Lance shared memory */
141struct lance_memory {
142	struct lance_init_block	init;
143	struct lance_tx_head	tx_head[TX_RING_SIZE];
144	struct lance_rx_head	rx_head[RX_RING_SIZE];
145	char   rx_data[RX_RING_SIZE][PKT_BUF_SZ];
146	char   tx_data[TX_RING_SIZE][PKT_BUF_SZ];
147};
148
149/* The driver's private device structure */
150
151struct lance_private {
152	volatile unsigned short	*iobase;
153	struct lance_memory	*mem;
154	int new_rx, new_tx;	/* The next free ring entry */
155	int old_tx, old_rx;     /* ring entry to be processed */
156/* These two must be longs for set_bit() */
157	long	    tx_full;
158	long	    lock;
159};
160
161/* I/O register access macros */
162
163#define	MEM	lp->mem
164#define	DREG	lp->iobase[0]
165#define	AREG	lp->iobase[1]
166#define	REGA(a)	(*( AREG = (a), &DREG ))
167
168/* Definitions for the Lance */
169
170/* tx_head flags */
171#define TMD1_ENP		0x01	/* end of packet */
172#define TMD1_STP		0x02	/* start of packet */
173#define TMD1_DEF		0x04	/* deferred */
174#define TMD1_ONE		0x08	/* one retry needed */
175#define TMD1_MORE		0x10	/* more than one retry needed */
176#define TMD1_ERR		0x40	/* error summary */
177#define TMD1_OWN 		0x80	/* ownership (set: chip owns) */
178
179#define TMD1_OWN_CHIP	TMD1_OWN
180#define TMD1_OWN_HOST	0
181
182/* tx_head misc field */
183#define TMD3_TDR		0x03FF	/* Time Domain Reflectometry counter */
184#define TMD3_RTRY		0x0400	/* failed after 16 retries */
185#define TMD3_LCAR		0x0800	/* carrier lost */
186#define TMD3_LCOL		0x1000	/* late collision */
187#define TMD3_UFLO		0x4000	/* underflow (late memory) */
188#define TMD3_BUFF		0x8000	/* buffering error (no ENP) */
189
190/* rx_head flags */
191#define RMD1_ENP		0x01	/* end of packet */
192#define RMD1_STP		0x02	/* start of packet */
193#define RMD1_BUFF		0x04	/* buffer error */
194#define RMD1_CRC		0x08	/* CRC error */
195#define RMD1_OFLO		0x10	/* overflow */
196#define RMD1_FRAM		0x20	/* framing error */
197#define RMD1_ERR		0x40	/* error summary */
198#define RMD1_OWN 		0x80	/* ownership (set: ship owns) */
199
200#define RMD1_OWN_CHIP	RMD1_OWN
201#define RMD1_OWN_HOST	0
202
203/* register names */
204#define CSR0	0		/* mode/status */
205#define CSR1	1		/* init block addr (low) */
206#define CSR2	2		/* init block addr (high) */
207#define CSR3	3		/* misc */
208#define CSR8	8	  	/* address filter */
209#define CSR15	15		/* promiscuous mode */
210
211/* CSR0 */
212/* (R=readable, W=writeable, S=set on write, C=clear on write) */
213#define CSR0_INIT	0x0001		/* initialize (RS) */
214#define CSR0_STRT	0x0002		/* start (RS) */
215#define CSR0_STOP	0x0004		/* stop (RS) */
216#define CSR0_TDMD	0x0008		/* transmit demand (RS) */
217#define CSR0_TXON	0x0010		/* transmitter on (R) */
218#define CSR0_RXON	0x0020		/* receiver on (R) */
219#define CSR0_INEA	0x0040		/* interrupt enable (RW) */
220#define CSR0_INTR	0x0080		/* interrupt active (R) */
221#define CSR0_IDON	0x0100		/* initialization done (RC) */
222#define CSR0_TINT	0x0200		/* transmitter interrupt (RC) */
223#define CSR0_RINT	0x0400		/* receiver interrupt (RC) */
224#define CSR0_MERR	0x0800		/* memory error (RC) */
225#define CSR0_MISS	0x1000		/* missed frame (RC) */
226#define CSR0_CERR	0x2000		/* carrier error (no heartbeat :-) (RC) */
227#define CSR0_BABL	0x4000		/* babble: tx-ed too many bits (RC) */
228#define CSR0_ERR	0x8000		/* error (RC) */
229
230/* CSR3 */
231#define CSR3_BCON	0x0001		/* byte control */
232#define CSR3_ACON	0x0002		/* ALE control */
233#define CSR3_BSWP	0x0004		/* byte swap (1=big endian) */
234
235/***************************** Prototypes *****************************/
236
237static int lance_probe( struct net_device *dev);
238static int lance_open( struct net_device *dev );
239static void lance_init_ring( struct net_device *dev );
240static netdev_tx_t lance_start_xmit(struct sk_buff *skb,
241				    struct net_device *dev);
242static irqreturn_t lance_interrupt( int irq, void *dev_id);
243static int lance_rx( struct net_device *dev );
244static int lance_close( struct net_device *dev );
245static void set_multicast_list( struct net_device *dev );
246
247/************************* End of Prototypes **************************/
248
249static struct net_device * __init sun3lance_probe(void)
250{
251	struct net_device *dev;
252	static int found;
253	int err = -ENODEV;
254
255	if (!MACH_IS_SUN3 && !MACH_IS_SUN3X)
256		return ERR_PTR(-ENODEV);
257
258	/* check that this machine has an onboard lance */
259	switch(idprom->id_machtype) {
260	case SM_SUN3|SM_3_50:
261	case SM_SUN3|SM_3_60:
262	case SM_SUN3X|SM_3_80:
263		/* these machines have lance */
264		break;
265
266	default:
267		return ERR_PTR(-ENODEV);
268	}
269
270	if (found)
271		return ERR_PTR(-ENODEV);
272
273	dev = alloc_etherdev(sizeof(struct lance_private));
274	if (!dev)
275		return ERR_PTR(-ENOMEM);
276
277	if (!lance_probe(dev))
278		goto out;
279
280	err = register_netdev(dev);
281	if (err)
282		goto out1;
283	found = 1;
284	return dev;
285
286out1:
287#ifdef CONFIG_SUN3
288	iounmap((void __iomem *)dev->base_addr);
289#endif
290out:
291	free_netdev(dev);
292	return ERR_PTR(err);
293}
294
295static const struct net_device_ops lance_netdev_ops = {
296	.ndo_open		= lance_open,
297	.ndo_stop		= lance_close,
298	.ndo_start_xmit		= lance_start_xmit,
299	.ndo_set_rx_mode	= set_multicast_list,
300	.ndo_set_mac_address	= NULL,
301	.ndo_validate_addr	= eth_validate_addr,
302};
303
304static int __init lance_probe( struct net_device *dev)
305{
306	unsigned long ioaddr;
307
308	struct lance_private	*lp;
309	static int 		did_version;
310	volatile unsigned short *ioaddr_probe;
311	unsigned short tmp1, tmp2;
312
313#ifdef CONFIG_SUN3
314	ioaddr = (unsigned long)ioremap(LANCE_OBIO, PAGE_SIZE);
315	if (!ioaddr)
316		return 0;
317#else
318	ioaddr = SUN3X_LANCE;
319#endif
320
321	/* test to see if there's really a lance here */
322	/* (CSRO_INIT shouldn't be readable) */
323
324	ioaddr_probe = (volatile unsigned short *)ioaddr;
325	tmp1 = ioaddr_probe[0];
326	tmp2 = ioaddr_probe[1];
327
328	ioaddr_probe[1] = CSR0;
329	ioaddr_probe[0] = CSR0_INIT | CSR0_STOP;
330
331	if(ioaddr_probe[0] != CSR0_STOP) {
332		ioaddr_probe[0] = tmp1;
333		ioaddr_probe[1] = tmp2;
334
335#ifdef CONFIG_SUN3
336		iounmap((void __iomem *)ioaddr);
337#endif
338		return 0;
339	}
340
341	lp = netdev_priv(dev);
342
343	/* XXX - leak? */
344	MEM = dvma_malloc_align(sizeof(struct lance_memory), 0x10000);
345	if (!MEM) {
346#ifdef CONFIG_SUN3
347		iounmap((void __iomem *)ioaddr);
348#endif
349		printk(KERN_WARNING "SUN3 Lance couldn't allocate DVMA memory\n");
350		return 0;
351	}
352
353	lp->iobase = (volatile unsigned short *)ioaddr;
354	dev->base_addr = (unsigned long)ioaddr; /* informational only */
355
356	REGA(CSR0) = CSR0_STOP;
357
358	if (request_irq(LANCE_IRQ, lance_interrupt, 0, "SUN3 Lance", dev) < 0) {
359#ifdef CONFIG_SUN3
360		iounmap((void __iomem *)ioaddr);
361#endif
362		dvma_free((void *)MEM);
363		printk(KERN_WARNING "SUN3 Lance unable to allocate IRQ\n");
364		return 0;
365	}
366	dev->irq = (unsigned short)LANCE_IRQ;
367
368
369	printk("%s: SUN3 Lance at io %#lx, mem %#lx, irq %d, hwaddr ",
370		   dev->name,
371		   (unsigned long)ioaddr,
372		   (unsigned long)MEM,
373		   dev->irq);
374
375	/* copy in the ethernet address from the prom */
376	eth_hw_addr_set(dev, idprom->id_ethaddr);
377
378	/* tell the card it's ether address, bytes swapped */
379	MEM->init.hwaddr[0] = dev->dev_addr[1];
380	MEM->init.hwaddr[1] = dev->dev_addr[0];
381	MEM->init.hwaddr[2] = dev->dev_addr[3];
382	MEM->init.hwaddr[3] = dev->dev_addr[2];
383	MEM->init.hwaddr[4] = dev->dev_addr[5];
384	MEM->init.hwaddr[5] = dev->dev_addr[4];
385
386	printk("%pM\n", dev->dev_addr);
387
388	MEM->init.mode = 0x0000;
389	MEM->init.filter[0] = 0x00000000;
390	MEM->init.filter[1] = 0x00000000;
391	MEM->init.rdra = dvma_vtob(MEM->rx_head);
392	MEM->init.rlen    = (RX_LOG_RING_SIZE << 13) |
393		(dvma_vtob(MEM->rx_head) >> 16);
394	MEM->init.tdra = dvma_vtob(MEM->tx_head);
395	MEM->init.tlen    = (TX_LOG_RING_SIZE << 13) |
396		(dvma_vtob(MEM->tx_head) >> 16);
397
398	DPRINTK(2, ("initaddr: %08lx rx_ring: %08lx tx_ring: %08lx\n",
399	       dvma_vtob(&(MEM->init)), dvma_vtob(MEM->rx_head),
400	       (dvma_vtob(MEM->tx_head))));
401
402	if (did_version++ == 0)
403		printk( version );
404
405	dev->netdev_ops = &lance_netdev_ops;
406//	KLUDGE -- REMOVE ME
407	set_bit(__LINK_STATE_PRESENT, &dev->state);
408
409
410	return 1;
411}
412
413static int lance_open( struct net_device *dev )
414{
415	struct lance_private *lp = netdev_priv(dev);
416	int i;
417
418	DPRINTK( 2, ( "%s: lance_open()\n", dev->name ));
419
420	REGA(CSR0) = CSR0_STOP;
421
422	lance_init_ring(dev);
423
424	/* From now on, AREG is kept to point to CSR0 */
425	REGA(CSR0) = CSR0_INIT;
426
427	i = 1000000;
428	while (--i > 0)
429		if (DREG & CSR0_IDON)
430			break;
431	if (i <= 0 || (DREG & CSR0_ERR)) {
432		DPRINTK( 2, ( "lance_open(): opening %s failed, i=%d, csr0=%04x\n",
433					  dev->name, i, DREG ));
434		DREG = CSR0_STOP;
435		return -EIO;
436	}
437
438	DREG = CSR0_IDON | CSR0_STRT | CSR0_INEA;
439
440	netif_start_queue(dev);
441
442	DPRINTK( 2, ( "%s: LANCE is open, csr0 %04x\n", dev->name, DREG ));
443
444	return 0;
445}
446
447
448/* Initialize the LANCE Rx and Tx rings. */
449
450static void lance_init_ring( struct net_device *dev )
451{
452	struct lance_private *lp = netdev_priv(dev);
453	int i;
454
455	lp->lock = 0;
456	lp->tx_full = 0;
457	lp->new_rx = lp->new_tx = 0;
458	lp->old_rx = lp->old_tx = 0;
459
460	for( i = 0; i < TX_RING_SIZE; i++ ) {
461		MEM->tx_head[i].base = dvma_vtob(MEM->tx_data[i]);
462		MEM->tx_head[i].flag = 0;
463		MEM->tx_head[i].base_hi =
464			(dvma_vtob(MEM->tx_data[i])) >>16;
465		MEM->tx_head[i].length = 0;
466		MEM->tx_head[i].misc = 0;
467	}
468
469	for( i = 0; i < RX_RING_SIZE; i++ ) {
470		MEM->rx_head[i].base = dvma_vtob(MEM->rx_data[i]);
471		MEM->rx_head[i].flag = RMD1_OWN_CHIP;
472		MEM->rx_head[i].base_hi =
473			(dvma_vtob(MEM->rx_data[i])) >> 16;
474		MEM->rx_head[i].buf_length = -PKT_BUF_SZ | 0xf000;
475		MEM->rx_head[i].msg_length = 0;
476	}
477
478	/* tell the card it's ether address, bytes swapped */
479	MEM->init.hwaddr[0] = dev->dev_addr[1];
480	MEM->init.hwaddr[1] = dev->dev_addr[0];
481	MEM->init.hwaddr[2] = dev->dev_addr[3];
482	MEM->init.hwaddr[3] = dev->dev_addr[2];
483	MEM->init.hwaddr[4] = dev->dev_addr[5];
484	MEM->init.hwaddr[5] = dev->dev_addr[4];
485
486	MEM->init.mode = 0x0000;
487	MEM->init.filter[0] = 0x00000000;
488	MEM->init.filter[1] = 0x00000000;
489	MEM->init.rdra = dvma_vtob(MEM->rx_head);
490	MEM->init.rlen    = (RX_LOG_RING_SIZE << 13) |
491		(dvma_vtob(MEM->rx_head) >> 16);
492	MEM->init.tdra = dvma_vtob(MEM->tx_head);
493	MEM->init.tlen    = (TX_LOG_RING_SIZE << 13) |
494		(dvma_vtob(MEM->tx_head) >> 16);
495
496
497	/* tell the lance the address of its init block */
498	REGA(CSR1) = dvma_vtob(&(MEM->init));
499	REGA(CSR2) = dvma_vtob(&(MEM->init)) >> 16;
500
501#ifdef CONFIG_SUN3X
502	REGA(CSR3) = CSR3_BSWP | CSR3_ACON | CSR3_BCON;
503#else
504	REGA(CSR3) = CSR3_BSWP;
505#endif
506
507}
508
509
510static netdev_tx_t
511lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
512{
513	struct lance_private *lp = netdev_priv(dev);
514	int entry, len;
515	struct lance_tx_head *head;
516	unsigned long flags;
517
518	DPRINTK( 1, ( "%s: transmit start.\n",
519		      dev->name));
520
521	/* Transmitter timeout, serious problems. */
522	if (netif_queue_stopped(dev)) {
523		int tickssofar = jiffies - dev_trans_start(dev);
524		if (tickssofar < HZ/5)
525			return NETDEV_TX_BUSY;
526
527		DPRINTK( 1, ( "%s: transmit timed out, status %04x, resetting.\n",
528					  dev->name, DREG ));
529		DREG = CSR0_STOP;
530		/*
531		 * Always set BSWP after a STOP as STOP puts it back into
532		 * little endian mode.
533		 */
534		REGA(CSR3) = CSR3_BSWP;
535		dev->stats.tx_errors++;
536
537		if(lance_debug >= 2) {
538			int i;
539			printk("Ring data: old_tx %d new_tx %d%s new_rx %d\n",
540			       lp->old_tx, lp->new_tx,
541			       lp->tx_full ? " (full)" : "",
542			       lp->new_rx );
543			for( i = 0 ; i < RX_RING_SIZE; i++ )
544				printk( "rx #%d: base=%04x blen=%04x mlen=%04x\n",
545					i, MEM->rx_head[i].base,
546					-MEM->rx_head[i].buf_length,
547					MEM->rx_head[i].msg_length);
548			for( i = 0 ; i < TX_RING_SIZE; i++ )
549				printk("tx #%d: base=%04x len=%04x misc=%04x\n",
550				       i, MEM->tx_head[i].base,
551				       -MEM->tx_head[i].length,
552				       MEM->tx_head[i].misc );
553		}
554
555		lance_init_ring(dev);
556		REGA( CSR0 ) = CSR0_INEA | CSR0_INIT | CSR0_STRT;
557
558		netif_start_queue(dev);
559
560		return NETDEV_TX_OK;
561	}
562
563
564	/* Block a timer-based transmit from overlapping.  This could better be
565	   done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
566
567	/* Block a timer-based transmit from overlapping with us by
568	   stopping the queue for a bit... */
569
570	netif_stop_queue(dev);
571
572	if (test_and_set_bit( 0, (void*)&lp->lock ) != 0) {
573		printk( "%s: tx queue lock!.\n", dev->name);
574		/* don't clear dev->tbusy flag. */
575		return NETDEV_TX_BUSY;
576	}
577
578	AREG = CSR0;
579	DPRINTK( 2, ( "%s: lance_start_xmit() called, csr0 %4.4x.\n",
580				  dev->name, DREG ));
581
582#ifdef CONFIG_SUN3X
583	/* this weirdness doesn't appear on sun3... */
584	if(!(DREG & CSR0_INIT)) {
585		DPRINTK( 1, ("INIT not set, reinitializing...\n"));
586		REGA( CSR0 ) = CSR0_STOP;
587		lance_init_ring(dev);
588		REGA( CSR0 ) = CSR0_INIT | CSR0_STRT;
589	}
590#endif
591
592	/* Fill in a Tx ring entry */
593#if 0
594	if (lance_debug >= 2) {
595		printk( "%s: TX pkt %d type 0x%04x"
596			" from %s to %s"
597			" data at 0x%08x len %d\n",
598			dev->name, lp->new_tx, ((u_short *)skb->data)[6],
599			DEV_ADDR(&skb->data[6]), DEV_ADDR(skb->data),
600			(int)skb->data, (int)skb->len );
601	}
602#endif
603	/* We're not prepared for the int until the last flags are set/reset.
604	 * And the int may happen already after setting the OWN_CHIP... */
605	local_irq_save(flags);
606
607	/* Mask to ring buffer boundary. */
608	entry = lp->new_tx;
609	head  = &(MEM->tx_head[entry]);
610
611	/* Caution: the write order is important here, set the "ownership" bits
612	 * last.
613	 */
614
615	/* the sun3's lance needs it's buffer padded to the minimum
616	   size */
617	len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
618
619//	head->length = -len;
620	head->length = (-len) | 0xf000;
621	head->misc = 0;
622
623	skb_copy_from_linear_data(skb, PKTBUF_ADDR(head), skb->len);
624	if (len != skb->len)
625		memset(PKTBUF_ADDR(head) + skb->len, 0, len-skb->len);
626
627	head->flag = TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP;
628	lp->new_tx = (lp->new_tx + 1) & TX_RING_MOD_MASK;
629	dev->stats.tx_bytes += skb->len;
630
631	/* Trigger an immediate send poll. */
632	REGA(CSR0) = CSR0_INEA | CSR0_TDMD | CSR0_STRT;
633	AREG = CSR0;
634	DPRINTK( 2, ( "%s: lance_start_xmit() exiting, csr0 %4.4x.\n",
635				  dev->name, DREG ));
636	dev_kfree_skb(skb);
637
638	lp->lock = 0;
639	if ((MEM->tx_head[(entry+1) & TX_RING_MOD_MASK].flag & TMD1_OWN) ==
640	    TMD1_OWN_HOST)
641		netif_start_queue(dev);
642
643	local_irq_restore(flags);
644
645	return NETDEV_TX_OK;
646}
647
648/* The LANCE interrupt handler. */
649
650static irqreturn_t lance_interrupt( int irq, void *dev_id)
651{
652	struct net_device *dev = dev_id;
653	struct lance_private *lp = netdev_priv(dev);
654	int csr0;
655
656 still_more:
657	flush_cache_all();
658
659	AREG = CSR0;
660	csr0 = DREG;
661
662	/* ack interrupts */
663	DREG = csr0 & (CSR0_TINT | CSR0_RINT | CSR0_IDON);
664
665	/* clear errors */
666	if(csr0 & CSR0_ERR)
667		DREG = CSR0_BABL | CSR0_MERR | CSR0_CERR | CSR0_MISS;
668
669
670	DPRINTK( 2, ( "%s: interrupt  csr0=%04x new csr=%04x.\n",
671		      dev->name, csr0, DREG ));
672
673	if (csr0 & CSR0_TINT) {			/* Tx-done interrupt */
674		int old_tx = lp->old_tx;
675
676//		if(lance_debug >= 3) {
677//			int i;
678//
679//			printk("%s: tx int\n", dev->name);
680//
681//			for(i = 0; i < TX_RING_SIZE; i++)
682//				printk("ring %d flag=%04x\n", i,
683//				       MEM->tx_head[i].flag);
684//		}
685
686		while( old_tx != lp->new_tx) {
687			struct lance_tx_head *head = &(MEM->tx_head[old_tx]);
688
689			DPRINTK(3, ("on tx_ring %d\n", old_tx));
690
691			if (head->flag & TMD1_OWN_CHIP)
692				break; /* It still hasn't been Txed */
693
694			if (head->flag & TMD1_ERR) {
695				int status = head->misc;
696				dev->stats.tx_errors++;
697				if (status & TMD3_RTRY) dev->stats.tx_aborted_errors++;
698				if (status & TMD3_LCAR) dev->stats.tx_carrier_errors++;
699				if (status & TMD3_LCOL) dev->stats.tx_window_errors++;
700				if (status & (TMD3_UFLO | TMD3_BUFF)) {
701					dev->stats.tx_fifo_errors++;
702					printk("%s: Tx FIFO error\n",
703					       dev->name);
704					REGA(CSR0) = CSR0_STOP;
705					REGA(CSR3) = CSR3_BSWP;
706					lance_init_ring(dev);
707					REGA(CSR0) = CSR0_STRT | CSR0_INEA;
708					return IRQ_HANDLED;
709				}
710			} else if(head->flag & (TMD1_ENP | TMD1_STP)) {
711
712				head->flag &= ~(TMD1_ENP | TMD1_STP);
713				if(head->flag & (TMD1_ONE | TMD1_MORE))
714					dev->stats.collisions++;
715
716				dev->stats.tx_packets++;
717				DPRINTK(3, ("cleared tx ring %d\n", old_tx));
718			}
719			old_tx = (old_tx +1) & TX_RING_MOD_MASK;
720		}
721
722		lp->old_tx = old_tx;
723	}
724
725
726	if (netif_queue_stopped(dev)) {
727		/* The ring is no longer full, clear tbusy. */
728		netif_start_queue(dev);
729		netif_wake_queue(dev);
730	}
731
732	if (csr0 & CSR0_RINT)			/* Rx interrupt */
733		lance_rx( dev );
734
735	/* Log misc errors. */
736	if (csr0 & CSR0_BABL) dev->stats.tx_errors++; /* Tx babble. */
737	if (csr0 & CSR0_MISS) dev->stats.rx_errors++; /* Missed a Rx frame. */
738	if (csr0 & CSR0_MERR) {
739		DPRINTK( 1, ( "%s: Bus master arbitration failure (?!?), "
740			      "status %04x.\n", dev->name, csr0 ));
741		/* Restart the chip. */
742		REGA(CSR0) = CSR0_STOP;
743		REGA(CSR3) = CSR3_BSWP;
744		lance_init_ring(dev);
745		REGA(CSR0) = CSR0_STRT | CSR0_INEA;
746	}
747
748
749    /* Clear any other interrupt, and set interrupt enable. */
750//	DREG = CSR0_BABL | CSR0_CERR | CSR0_MISS | CSR0_MERR |
751//		   CSR0_IDON | CSR0_INEA;
752
753	REGA(CSR0) = CSR0_INEA;
754
755	if(DREG & (CSR0_RINT | CSR0_TINT)) {
756	     DPRINTK(2, ("restarting interrupt, csr0=%#04x\n", DREG));
757	     goto still_more;
758	}
759
760	DPRINTK( 2, ( "%s: exiting interrupt, csr0=%#04x.\n",
761				  dev->name, DREG ));
762	return IRQ_HANDLED;
763}
764
765/* get packet, toss into skbuff */
766static int lance_rx( struct net_device *dev )
767{
768	struct lance_private *lp = netdev_priv(dev);
769	int entry = lp->new_rx;
770
771	/* If we own the next entry, it's a new packet. Send it up. */
772	while( (MEM->rx_head[entry].flag & RMD1_OWN) == RMD1_OWN_HOST ) {
773		struct lance_rx_head *head = &(MEM->rx_head[entry]);
774		int status = head->flag;
775
776		if (status != (RMD1_ENP|RMD1_STP)) {  /* There was an error. */
777			/* There is a tricky error noted by John Murphy,
778			   <murf@perftech.com> to Russ Nelson: Even with
779			   full-sized buffers it's possible for a jabber packet to use two
780			   buffers, with only the last correctly noting the error. */
781			if (status & RMD1_ENP)	/* Only count a general error at the */
782				dev->stats.rx_errors++; /* end of a packet.*/
783			if (status & RMD1_FRAM) dev->stats.rx_frame_errors++;
784			if (status & RMD1_OFLO) dev->stats.rx_over_errors++;
785			if (status & RMD1_CRC) dev->stats.rx_crc_errors++;
786			if (status & RMD1_BUFF) dev->stats.rx_fifo_errors++;
787			head->flag &= (RMD1_ENP|RMD1_STP);
788		} else {
789			/* Malloc up new buffer, compatible with net-3. */
790//			short pkt_len = head->msg_length;// & 0xfff;
791			short pkt_len = (head->msg_length & 0xfff) - 4;
792			struct sk_buff *skb;
793
794			if (pkt_len < 60) {
795				printk( "%s: Runt packet!\n", dev->name );
796				dev->stats.rx_errors++;
797			}
798			else {
799				skb = netdev_alloc_skb(dev, pkt_len + 2);
800				if (!skb) {
801					dev->stats.rx_dropped++;
802					head->msg_length = 0;
803					head->flag |= RMD1_OWN_CHIP;
804					lp->new_rx = (lp->new_rx+1) &
805					     RX_RING_MOD_MASK;
806				}
807
808#if 0
809				if (lance_debug >= 3) {
810					u_char *data = PKTBUF_ADDR(head);
811					printk("%s: RX pkt %d type 0x%04x"
812					       " from %pM to %pM",
813					       dev->name, lp->new_tx, ((u_short *)data)[6],
814					       &data[6], data);
815
816					printk(" data %02x %02x %02x %02x %02x %02x %02x %02x "
817					       "len %d at %08x\n",
818					       data[15], data[16], data[17], data[18],
819					       data[19], data[20], data[21], data[22],
820					       pkt_len, data);
821				}
822#endif
823				if (lance_debug >= 3) {
824					u_char *data = PKTBUF_ADDR(head);
825					printk( "%s: RX pkt %d type 0x%04x len %d\n ", dev->name, entry, ((u_short *)data)[6], pkt_len);
826				}
827
828
829				skb_reserve( skb, 2 );	/* 16 byte align */
830				skb_put( skb, pkt_len );	/* Make room */
831				skb_copy_to_linear_data(skb,
832						 PKTBUF_ADDR(head),
833						 pkt_len);
834
835				skb->protocol = eth_type_trans( skb, dev );
836				netif_rx( skb );
837				dev->stats.rx_packets++;
838				dev->stats.rx_bytes += pkt_len;
839			}
840		}
841
842//		head->buf_length = -PKT_BUF_SZ | 0xf000;
843		head->msg_length = 0;
844		head->flag = RMD1_OWN_CHIP;
845
846		entry = lp->new_rx = (lp->new_rx +1) & RX_RING_MOD_MASK;
847	}
848
849	/* From lance.c (Donald Becker): */
850	/* We should check that at least two ring entries are free.
851	   If not, we should free one and mark stats->rx_dropped++. */
852
853	return 0;
854}
855
856
857static int lance_close( struct net_device *dev )
858{
859	struct lance_private *lp = netdev_priv(dev);
860
861	netif_stop_queue(dev);
862
863	AREG = CSR0;
864
865	DPRINTK( 2, ( "%s: Shutting down ethercard, status was %2.2x.\n",
866				  dev->name, DREG ));
867
868	/* We stop the LANCE here -- it occasionally polls
869	   memory if we don't. */
870	DREG = CSR0_STOP;
871	return 0;
872}
873
874
875/* Set or clear the multicast filter for this adaptor.
876   num_addrs == -1		Promiscuous mode, receive all packets
877   num_addrs == 0		Normal mode, clear multicast list
878   num_addrs > 0		Multicast mode, receive normal and MC packets, and do
879						best-effort filtering.
880 */
881
882/* completely untested on a sun3 */
883static void set_multicast_list( struct net_device *dev )
884{
885	struct lance_private *lp = netdev_priv(dev);
886
887	if(netif_queue_stopped(dev))
888		/* Only possible if board is already started */
889		return;
890
891	/* We take the simple way out and always enable promiscuous mode. */
892	DREG = CSR0_STOP; /* Temporarily stop the lance. */
893
894	if (dev->flags & IFF_PROMISC) {
895		/* Log any net taps. */
896		DPRINTK( 3, ( "%s: Promiscuous mode enabled.\n", dev->name ));
897		REGA( CSR15 ) = 0x8000; /* Set promiscuous mode */
898	} else {
899		short multicast_table[4];
900		int num_addrs = netdev_mc_count(dev);
901		int i;
902		/* We don't use the multicast table, but rely on upper-layer
903		 * filtering. */
904		memset( multicast_table, (num_addrs == 0) ? 0 : -1,
905				sizeof(multicast_table) );
906		for( i = 0; i < 4; i++ )
907			REGA( CSR8+i ) = multicast_table[i];
908		REGA( CSR15 ) = 0; /* Unset promiscuous mode */
909	}
910
911	/*
912	 * Always set BSWP after a STOP as STOP puts it back into
913	 * little endian mode.
914	 */
915	REGA( CSR3 ) = CSR3_BSWP;
916
917	/* Resume normal operation and reset AREG to CSR0 */
918	REGA( CSR0 ) = CSR0_IDON | CSR0_INEA | CSR0_STRT;
919}
920
921
922static struct net_device *sun3lance_dev;
923
924static int __init sun3lance_init(void)
925{
926	sun3lance_dev = sun3lance_probe();
927	return PTR_ERR_OR_ZERO(sun3lance_dev);
928}
929module_init(sun3lance_init);
930
931static void __exit sun3lance_cleanup(void)
932{
933	unregister_netdev(sun3lance_dev);
934#ifdef CONFIG_SUN3
935	iounmap((void __iomem *)sun3lance_dev->base_addr);
936#endif
937	free_netdev(sun3lance_dev);
938}
939module_exit(sun3lance_cleanup);
v6.8
  1/* sun3lance.c: Ethernet driver for SUN3 Lance chip */
  2/*
  3
  4  Sun3 Lance ethernet driver, by Sam Creasey (sammy@users.qual.net).
  5  This driver is a part of the linux kernel, and is thus distributed
  6  under the GNU General Public License.
  7
  8  The values used in LANCE_OBIO and LANCE_IRQ seem to be empirically
  9  true for the correct IRQ and address of the lance registers.  They
 10  have not been widely tested, however.  What we probably need is a
 11  "proper" way to search for a device in the sun3's prom, but, alas,
 12  linux has no such thing.
 13
 14  This driver is largely based on atarilance.c, by Roman Hodek.  Other
 15  sources of inspiration were the NetBSD sun3 am7990 driver, and the
 16  linux sparc lance driver (sunlance.c).
 17
 18  There are more assumptions made throughout this driver, it almost
 19  certainly still needs work, but it does work at least for RARP/BOOTP and
 20  mounting the root NFS filesystem.
 21
 22*/
 23
 24static const char version[] =
 25"sun3lance.c: v1.2 1/12/2001  Sam Creasey (sammy@sammy.net)\n";
 26
 27#include <linux/module.h>
 28#include <linux/stddef.h>
 29#include <linux/kernel.h>
 30#include <linux/string.h>
 31#include <linux/errno.h>
 32#include <linux/interrupt.h>
 33#include <linux/init.h>
 34#include <linux/ioport.h>
 35#include <linux/delay.h>
 36#include <linux/netdevice.h>
 37#include <linux/etherdevice.h>
 38#include <linux/skbuff.h>
 39#include <linux/bitops.h>
 40#include <linux/pgtable.h>
 41
 42#include <asm/cacheflush.h>
 43#include <asm/setup.h>
 44#include <asm/irq.h>
 45#include <asm/io.h>
 46#include <asm/dvma.h>
 47#include <asm/idprom.h>
 48#include <asm/machines.h>
 49
 50#ifdef CONFIG_SUN3
 51#include <asm/sun3mmu.h>
 52#else
 53#include <asm/sun3xprom.h>
 54#endif
 55
 56/* sun3/60 addr/irq for the lance chip.  If your sun is different,
 57   change this. */
 58#define LANCE_OBIO 0x120000
 59#define LANCE_IRQ IRQ_AUTO_3
 60
 61/* Debug level:
 62 *  0 = silent, print only serious errors
 63 *  1 = normal, print error messages
 64 *  2 = debug, print debug infos
 65 *  3 = debug, print even more debug infos (packet data)
 66 */
 67
 68#define	LANCE_DEBUG	0
 69
 70#ifdef LANCE_DEBUG
 71static int lance_debug = LANCE_DEBUG;
 72#else
 73static int lance_debug = 1;
 74#endif
 75module_param(lance_debug, int, 0);
 76MODULE_PARM_DESC(lance_debug, "SUN3 Lance debug level (0-3)");
 
 77MODULE_LICENSE("GPL");
 78
 79#define	DPRINTK(n,a) \
 80	do {  \
 81		if (lance_debug >= n)  \
 82			printk a; \
 83	} while( 0 )
 84
 85
 86/* we're only using 32k of memory, so we use 4 TX
 87   buffers and 16 RX buffers.  These values are expressed as log2. */
 88
 89#define TX_LOG_RING_SIZE			3
 90#define RX_LOG_RING_SIZE			5
 91
 92/* These are the derived values */
 93
 94#define TX_RING_SIZE			(1 << TX_LOG_RING_SIZE)
 95#define TX_RING_LEN_BITS		(TX_LOG_RING_SIZE << 5)
 96#define	TX_RING_MOD_MASK		(TX_RING_SIZE - 1)
 97
 98#define RX_RING_SIZE			(1 << RX_LOG_RING_SIZE)
 99#define RX_RING_LEN_BITS		(RX_LOG_RING_SIZE << 5)
100#define	RX_RING_MOD_MASK		(RX_RING_SIZE - 1)
101
102/* Definitions for packet buffer access: */
103#define PKT_BUF_SZ		1544
104
105/* Get the address of a packet buffer corresponding to a given buffer head */
106#define	PKTBUF_ADDR(head)	(void *)((unsigned long)(MEM) | (head)->base)
107
108
109/* The LANCE Rx and Tx ring descriptors. */
110struct lance_rx_head {
111	unsigned short	base;		/* Low word of base addr */
112	volatile unsigned char	flag;
113	unsigned char  base_hi;	/* High word of base addr (unused) */
114	short buf_length;	/* This length is 2s complement! */
115	volatile short msg_length;	/* This length is "normal". */
116};
117
118struct lance_tx_head {
119	unsigned short base;		/* Low word of base addr */
120	volatile unsigned char	flag;
121	unsigned char base_hi;	/* High word of base addr (unused) */
122	short length;		/* Length is 2s complement! */
123	volatile short misc;
124};
125
126/* The LANCE initialization block, described in databook. */
127struct lance_init_block {
128	unsigned short	mode;		/* Pre-set mode */
129	unsigned char	hwaddr[6];	/* Physical ethernet address */
130	unsigned int    filter[2];	/* Multicast filter (unused). */
131	/* Receive and transmit ring base, along with length bits. */
132	unsigned short rdra;
133	unsigned short rlen;
134	unsigned short tdra;
135	unsigned short tlen;
136	unsigned short pad[4]; /* is thie needed? */
137};
138
139/* The whole layout of the Lance shared memory */
140struct lance_memory {
141	struct lance_init_block	init;
142	struct lance_tx_head	tx_head[TX_RING_SIZE];
143	struct lance_rx_head	rx_head[RX_RING_SIZE];
144	char   rx_data[RX_RING_SIZE][PKT_BUF_SZ];
145	char   tx_data[TX_RING_SIZE][PKT_BUF_SZ];
146};
147
148/* The driver's private device structure */
149
150struct lance_private {
151	volatile unsigned short	*iobase;
152	struct lance_memory	*mem;
153	int new_rx, new_tx;	/* The next free ring entry */
154	int old_tx, old_rx;     /* ring entry to be processed */
155/* These two must be longs for set_bit() */
156	long	    tx_full;
157	long	    lock;
158};
159
160/* I/O register access macros */
161
162#define	MEM	lp->mem
163#define	DREG	lp->iobase[0]
164#define	AREG	lp->iobase[1]
165#define	REGA(a)	(*( AREG = (a), &DREG ))
166
167/* Definitions for the Lance */
168
169/* tx_head flags */
170#define TMD1_ENP		0x01	/* end of packet */
171#define TMD1_STP		0x02	/* start of packet */
172#define TMD1_DEF		0x04	/* deferred */
173#define TMD1_ONE		0x08	/* one retry needed */
174#define TMD1_MORE		0x10	/* more than one retry needed */
175#define TMD1_ERR		0x40	/* error summary */
176#define TMD1_OWN 		0x80	/* ownership (set: chip owns) */
177
178#define TMD1_OWN_CHIP	TMD1_OWN
179#define TMD1_OWN_HOST	0
180
181/* tx_head misc field */
182#define TMD3_TDR		0x03FF	/* Time Domain Reflectometry counter */
183#define TMD3_RTRY		0x0400	/* failed after 16 retries */
184#define TMD3_LCAR		0x0800	/* carrier lost */
185#define TMD3_LCOL		0x1000	/* late collision */
186#define TMD3_UFLO		0x4000	/* underflow (late memory) */
187#define TMD3_BUFF		0x8000	/* buffering error (no ENP) */
188
189/* rx_head flags */
190#define RMD1_ENP		0x01	/* end of packet */
191#define RMD1_STP		0x02	/* start of packet */
192#define RMD1_BUFF		0x04	/* buffer error */
193#define RMD1_CRC		0x08	/* CRC error */
194#define RMD1_OFLO		0x10	/* overflow */
195#define RMD1_FRAM		0x20	/* framing error */
196#define RMD1_ERR		0x40	/* error summary */
197#define RMD1_OWN 		0x80	/* ownership (set: ship owns) */
198
199#define RMD1_OWN_CHIP	RMD1_OWN
200#define RMD1_OWN_HOST	0
201
202/* register names */
203#define CSR0	0		/* mode/status */
204#define CSR1	1		/* init block addr (low) */
205#define CSR2	2		/* init block addr (high) */
206#define CSR3	3		/* misc */
207#define CSR8	8	  	/* address filter */
208#define CSR15	15		/* promiscuous mode */
209
210/* CSR0 */
211/* (R=readable, W=writeable, S=set on write, C=clear on write) */
212#define CSR0_INIT	0x0001		/* initialize (RS) */
213#define CSR0_STRT	0x0002		/* start (RS) */
214#define CSR0_STOP	0x0004		/* stop (RS) */
215#define CSR0_TDMD	0x0008		/* transmit demand (RS) */
216#define CSR0_TXON	0x0010		/* transmitter on (R) */
217#define CSR0_RXON	0x0020		/* receiver on (R) */
218#define CSR0_INEA	0x0040		/* interrupt enable (RW) */
219#define CSR0_INTR	0x0080		/* interrupt active (R) */
220#define CSR0_IDON	0x0100		/* initialization done (RC) */
221#define CSR0_TINT	0x0200		/* transmitter interrupt (RC) */
222#define CSR0_RINT	0x0400		/* receiver interrupt (RC) */
223#define CSR0_MERR	0x0800		/* memory error (RC) */
224#define CSR0_MISS	0x1000		/* missed frame (RC) */
225#define CSR0_CERR	0x2000		/* carrier error (no heartbeat :-) (RC) */
226#define CSR0_BABL	0x4000		/* babble: tx-ed too many bits (RC) */
227#define CSR0_ERR	0x8000		/* error (RC) */
228
229/* CSR3 */
230#define CSR3_BCON	0x0001		/* byte control */
231#define CSR3_ACON	0x0002		/* ALE control */
232#define CSR3_BSWP	0x0004		/* byte swap (1=big endian) */
233
234/***************************** Prototypes *****************************/
235
236static int lance_probe( struct net_device *dev);
237static int lance_open( struct net_device *dev );
238static void lance_init_ring( struct net_device *dev );
239static netdev_tx_t lance_start_xmit(struct sk_buff *skb,
240				    struct net_device *dev);
241static irqreturn_t lance_interrupt( int irq, void *dev_id);
242static int lance_rx( struct net_device *dev );
243static int lance_close( struct net_device *dev );
244static void set_multicast_list( struct net_device *dev );
245
246/************************* End of Prototypes **************************/
247
248static struct net_device * __init sun3lance_probe(void)
249{
250	struct net_device *dev;
251	static int found;
252	int err = -ENODEV;
253
254	if (!MACH_IS_SUN3 && !MACH_IS_SUN3X)
255		return ERR_PTR(-ENODEV);
256
257	/* check that this machine has an onboard lance */
258	switch(idprom->id_machtype) {
259	case SM_SUN3|SM_3_50:
260	case SM_SUN3|SM_3_60:
261	case SM_SUN3X|SM_3_80:
262		/* these machines have lance */
263		break;
264
265	default:
266		return ERR_PTR(-ENODEV);
267	}
268
269	if (found)
270		return ERR_PTR(-ENODEV);
271
272	dev = alloc_etherdev(sizeof(struct lance_private));
273	if (!dev)
274		return ERR_PTR(-ENOMEM);
275
276	if (!lance_probe(dev))
277		goto out;
278
279	err = register_netdev(dev);
280	if (err)
281		goto out1;
282	found = 1;
283	return dev;
284
285out1:
286#ifdef CONFIG_SUN3
287	iounmap((void __iomem *)dev->base_addr);
288#endif
289out:
290	free_netdev(dev);
291	return ERR_PTR(err);
292}
293
294static const struct net_device_ops lance_netdev_ops = {
295	.ndo_open		= lance_open,
296	.ndo_stop		= lance_close,
297	.ndo_start_xmit		= lance_start_xmit,
298	.ndo_set_rx_mode	= set_multicast_list,
299	.ndo_set_mac_address	= NULL,
300	.ndo_validate_addr	= eth_validate_addr,
301};
302
303static int __init lance_probe( struct net_device *dev)
304{
305	unsigned long ioaddr;
306
307	struct lance_private	*lp;
308	static int 		did_version;
309	volatile unsigned short *ioaddr_probe;
310	unsigned short tmp1, tmp2;
311
312#ifdef CONFIG_SUN3
313	ioaddr = (unsigned long)ioremap(LANCE_OBIO, PAGE_SIZE);
314	if (!ioaddr)
315		return 0;
316#else
317	ioaddr = SUN3X_LANCE;
318#endif
319
320	/* test to see if there's really a lance here */
321	/* (CSRO_INIT shouldn't be readable) */
322
323	ioaddr_probe = (volatile unsigned short *)ioaddr;
324	tmp1 = ioaddr_probe[0];
325	tmp2 = ioaddr_probe[1];
326
327	ioaddr_probe[1] = CSR0;
328	ioaddr_probe[0] = CSR0_INIT | CSR0_STOP;
329
330	if(ioaddr_probe[0] != CSR0_STOP) {
331		ioaddr_probe[0] = tmp1;
332		ioaddr_probe[1] = tmp2;
333
334#ifdef CONFIG_SUN3
335		iounmap((void __iomem *)ioaddr);
336#endif
337		return 0;
338	}
339
340	lp = netdev_priv(dev);
341
342	/* XXX - leak? */
343	MEM = dvma_malloc_align(sizeof(struct lance_memory), 0x10000);
344	if (!MEM) {
345#ifdef CONFIG_SUN3
346		iounmap((void __iomem *)ioaddr);
347#endif
348		printk(KERN_WARNING "SUN3 Lance couldn't allocate DVMA memory\n");
349		return 0;
350	}
351
352	lp->iobase = (volatile unsigned short *)ioaddr;
353	dev->base_addr = (unsigned long)ioaddr; /* informational only */
354
355	REGA(CSR0) = CSR0_STOP;
356
357	if (request_irq(LANCE_IRQ, lance_interrupt, 0, "SUN3 Lance", dev) < 0) {
358#ifdef CONFIG_SUN3
359		iounmap((void __iomem *)ioaddr);
360#endif
361		dvma_free((void *)MEM);
362		printk(KERN_WARNING "SUN3 Lance unable to allocate IRQ\n");
363		return 0;
364	}
365	dev->irq = (unsigned short)LANCE_IRQ;
366
367
368	printk("%s: SUN3 Lance at io %#lx, mem %#lx, irq %d, hwaddr ",
369		   dev->name,
370		   (unsigned long)ioaddr,
371		   (unsigned long)MEM,
372		   dev->irq);
373
374	/* copy in the ethernet address from the prom */
375	eth_hw_addr_set(dev, idprom->id_ethaddr);
376
377	/* tell the card it's ether address, bytes swapped */
378	MEM->init.hwaddr[0] = dev->dev_addr[1];
379	MEM->init.hwaddr[1] = dev->dev_addr[0];
380	MEM->init.hwaddr[2] = dev->dev_addr[3];
381	MEM->init.hwaddr[3] = dev->dev_addr[2];
382	MEM->init.hwaddr[4] = dev->dev_addr[5];
383	MEM->init.hwaddr[5] = dev->dev_addr[4];
384
385	printk("%pM\n", dev->dev_addr);
386
387	MEM->init.mode = 0x0000;
388	MEM->init.filter[0] = 0x00000000;
389	MEM->init.filter[1] = 0x00000000;
390	MEM->init.rdra = dvma_vtob(MEM->rx_head);
391	MEM->init.rlen    = (RX_LOG_RING_SIZE << 13) |
392		(dvma_vtob(MEM->rx_head) >> 16);
393	MEM->init.tdra = dvma_vtob(MEM->tx_head);
394	MEM->init.tlen    = (TX_LOG_RING_SIZE << 13) |
395		(dvma_vtob(MEM->tx_head) >> 16);
396
397	DPRINTK(2, ("initaddr: %08lx rx_ring: %08lx tx_ring: %08lx\n",
398	       dvma_vtob(&(MEM->init)), dvma_vtob(MEM->rx_head),
399	       (dvma_vtob(MEM->tx_head))));
400
401	if (did_version++ == 0)
402		printk( version );
403
404	dev->netdev_ops = &lance_netdev_ops;
405//	KLUDGE -- REMOVE ME
406	set_bit(__LINK_STATE_PRESENT, &dev->state);
407
408
409	return 1;
410}
411
412static int lance_open( struct net_device *dev )
413{
414	struct lance_private *lp = netdev_priv(dev);
415	int i;
416
417	DPRINTK( 2, ( "%s: lance_open()\n", dev->name ));
418
419	REGA(CSR0) = CSR0_STOP;
420
421	lance_init_ring(dev);
422
423	/* From now on, AREG is kept to point to CSR0 */
424	REGA(CSR0) = CSR0_INIT;
425
426	i = 1000000;
427	while (--i > 0)
428		if (DREG & CSR0_IDON)
429			break;
430	if (i <= 0 || (DREG & CSR0_ERR)) {
431		DPRINTK( 2, ( "lance_open(): opening %s failed, i=%d, csr0=%04x\n",
432					  dev->name, i, DREG ));
433		DREG = CSR0_STOP;
434		return -EIO;
435	}
436
437	DREG = CSR0_IDON | CSR0_STRT | CSR0_INEA;
438
439	netif_start_queue(dev);
440
441	DPRINTK( 2, ( "%s: LANCE is open, csr0 %04x\n", dev->name, DREG ));
442
443	return 0;
444}
445
446
447/* Initialize the LANCE Rx and Tx rings. */
448
449static void lance_init_ring( struct net_device *dev )
450{
451	struct lance_private *lp = netdev_priv(dev);
452	int i;
453
454	lp->lock = 0;
455	lp->tx_full = 0;
456	lp->new_rx = lp->new_tx = 0;
457	lp->old_rx = lp->old_tx = 0;
458
459	for( i = 0; i < TX_RING_SIZE; i++ ) {
460		MEM->tx_head[i].base = dvma_vtob(MEM->tx_data[i]);
461		MEM->tx_head[i].flag = 0;
462		MEM->tx_head[i].base_hi =
463			(dvma_vtob(MEM->tx_data[i])) >>16;
464		MEM->tx_head[i].length = 0;
465		MEM->tx_head[i].misc = 0;
466	}
467
468	for( i = 0; i < RX_RING_SIZE; i++ ) {
469		MEM->rx_head[i].base = dvma_vtob(MEM->rx_data[i]);
470		MEM->rx_head[i].flag = RMD1_OWN_CHIP;
471		MEM->rx_head[i].base_hi =
472			(dvma_vtob(MEM->rx_data[i])) >> 16;
473		MEM->rx_head[i].buf_length = -PKT_BUF_SZ | 0xf000;
474		MEM->rx_head[i].msg_length = 0;
475	}
476
477	/* tell the card it's ether address, bytes swapped */
478	MEM->init.hwaddr[0] = dev->dev_addr[1];
479	MEM->init.hwaddr[1] = dev->dev_addr[0];
480	MEM->init.hwaddr[2] = dev->dev_addr[3];
481	MEM->init.hwaddr[3] = dev->dev_addr[2];
482	MEM->init.hwaddr[4] = dev->dev_addr[5];
483	MEM->init.hwaddr[5] = dev->dev_addr[4];
484
485	MEM->init.mode = 0x0000;
486	MEM->init.filter[0] = 0x00000000;
487	MEM->init.filter[1] = 0x00000000;
488	MEM->init.rdra = dvma_vtob(MEM->rx_head);
489	MEM->init.rlen    = (RX_LOG_RING_SIZE << 13) |
490		(dvma_vtob(MEM->rx_head) >> 16);
491	MEM->init.tdra = dvma_vtob(MEM->tx_head);
492	MEM->init.tlen    = (TX_LOG_RING_SIZE << 13) |
493		(dvma_vtob(MEM->tx_head) >> 16);
494
495
496	/* tell the lance the address of its init block */
497	REGA(CSR1) = dvma_vtob(&(MEM->init));
498	REGA(CSR2) = dvma_vtob(&(MEM->init)) >> 16;
499
500#ifdef CONFIG_SUN3X
501	REGA(CSR3) = CSR3_BSWP | CSR3_ACON | CSR3_BCON;
502#else
503	REGA(CSR3) = CSR3_BSWP;
504#endif
505
506}
507
508
509static netdev_tx_t
510lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
511{
512	struct lance_private *lp = netdev_priv(dev);
513	int entry, len;
514	struct lance_tx_head *head;
515	unsigned long flags;
516
517	DPRINTK( 1, ( "%s: transmit start.\n",
518		      dev->name));
519
520	/* Transmitter timeout, serious problems. */
521	if (netif_queue_stopped(dev)) {
522		int tickssofar = jiffies - dev_trans_start(dev);
523		if (tickssofar < HZ/5)
524			return NETDEV_TX_BUSY;
525
526		DPRINTK( 1, ( "%s: transmit timed out, status %04x, resetting.\n",
527					  dev->name, DREG ));
528		DREG = CSR0_STOP;
529		/*
530		 * Always set BSWP after a STOP as STOP puts it back into
531		 * little endian mode.
532		 */
533		REGA(CSR3) = CSR3_BSWP;
534		dev->stats.tx_errors++;
535
536		if(lance_debug >= 2) {
537			int i;
538			printk("Ring data: old_tx %d new_tx %d%s new_rx %d\n",
539			       lp->old_tx, lp->new_tx,
540			       lp->tx_full ? " (full)" : "",
541			       lp->new_rx );
542			for( i = 0 ; i < RX_RING_SIZE; i++ )
543				printk( "rx #%d: base=%04x blen=%04x mlen=%04x\n",
544					i, MEM->rx_head[i].base,
545					-MEM->rx_head[i].buf_length,
546					MEM->rx_head[i].msg_length);
547			for( i = 0 ; i < TX_RING_SIZE; i++ )
548				printk("tx #%d: base=%04x len=%04x misc=%04x\n",
549				       i, MEM->tx_head[i].base,
550				       -MEM->tx_head[i].length,
551				       MEM->tx_head[i].misc );
552		}
553
554		lance_init_ring(dev);
555		REGA( CSR0 ) = CSR0_INEA | CSR0_INIT | CSR0_STRT;
556
557		netif_start_queue(dev);
558
559		return NETDEV_TX_OK;
560	}
561
562
563	/* Block a timer-based transmit from overlapping.  This could better be
564	   done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
565
566	/* Block a timer-based transmit from overlapping with us by
567	   stopping the queue for a bit... */
568
569	netif_stop_queue(dev);
570
571	if (test_and_set_bit( 0, (void*)&lp->lock ) != 0) {
572		printk( "%s: tx queue lock!.\n", dev->name);
573		/* don't clear dev->tbusy flag. */
574		return NETDEV_TX_BUSY;
575	}
576
577	AREG = CSR0;
578	DPRINTK( 2, ( "%s: lance_start_xmit() called, csr0 %4.4x.\n",
579				  dev->name, DREG ));
580
581#ifdef CONFIG_SUN3X
582	/* this weirdness doesn't appear on sun3... */
583	if(!(DREG & CSR0_INIT)) {
584		DPRINTK( 1, ("INIT not set, reinitializing...\n"));
585		REGA( CSR0 ) = CSR0_STOP;
586		lance_init_ring(dev);
587		REGA( CSR0 ) = CSR0_INIT | CSR0_STRT;
588	}
589#endif
590
591	/* Fill in a Tx ring entry */
592#if 0
593	if (lance_debug >= 2) {
594		printk( "%s: TX pkt %d type 0x%04x"
595			" from %s to %s"
596			" data at 0x%08x len %d\n",
597			dev->name, lp->new_tx, ((u_short *)skb->data)[6],
598			DEV_ADDR(&skb->data[6]), DEV_ADDR(skb->data),
599			(int)skb->data, (int)skb->len );
600	}
601#endif
602	/* We're not prepared for the int until the last flags are set/reset.
603	 * And the int may happen already after setting the OWN_CHIP... */
604	local_irq_save(flags);
605
606	/* Mask to ring buffer boundary. */
607	entry = lp->new_tx;
608	head  = &(MEM->tx_head[entry]);
609
610	/* Caution: the write order is important here, set the "ownership" bits
611	 * last.
612	 */
613
614	/* the sun3's lance needs it's buffer padded to the minimum
615	   size */
616	len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
617
618//	head->length = -len;
619	head->length = (-len) | 0xf000;
620	head->misc = 0;
621
622	skb_copy_from_linear_data(skb, PKTBUF_ADDR(head), skb->len);
623	if (len != skb->len)
624		memset(PKTBUF_ADDR(head) + skb->len, 0, len-skb->len);
625
626	head->flag = TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP;
627	lp->new_tx = (lp->new_tx + 1) & TX_RING_MOD_MASK;
628	dev->stats.tx_bytes += skb->len;
629
630	/* Trigger an immediate send poll. */
631	REGA(CSR0) = CSR0_INEA | CSR0_TDMD | CSR0_STRT;
632	AREG = CSR0;
633	DPRINTK( 2, ( "%s: lance_start_xmit() exiting, csr0 %4.4x.\n",
634				  dev->name, DREG ));
635	dev_kfree_skb(skb);
636
637	lp->lock = 0;
638	if ((MEM->tx_head[(entry+1) & TX_RING_MOD_MASK].flag & TMD1_OWN) ==
639	    TMD1_OWN_HOST)
640		netif_start_queue(dev);
641
642	local_irq_restore(flags);
643
644	return NETDEV_TX_OK;
645}
646
647/* The LANCE interrupt handler. */
648
649static irqreturn_t lance_interrupt( int irq, void *dev_id)
650{
651	struct net_device *dev = dev_id;
652	struct lance_private *lp = netdev_priv(dev);
653	int csr0;
654
655 still_more:
656	flush_cache_all();
657
658	AREG = CSR0;
659	csr0 = DREG;
660
661	/* ack interrupts */
662	DREG = csr0 & (CSR0_TINT | CSR0_RINT | CSR0_IDON);
663
664	/* clear errors */
665	if(csr0 & CSR0_ERR)
666		DREG = CSR0_BABL | CSR0_MERR | CSR0_CERR | CSR0_MISS;
667
668
669	DPRINTK( 2, ( "%s: interrupt  csr0=%04x new csr=%04x.\n",
670		      dev->name, csr0, DREG ));
671
672	if (csr0 & CSR0_TINT) {			/* Tx-done interrupt */
673		int old_tx = lp->old_tx;
674
675//		if(lance_debug >= 3) {
676//			int i;
677//
678//			printk("%s: tx int\n", dev->name);
679//
680//			for(i = 0; i < TX_RING_SIZE; i++)
681//				printk("ring %d flag=%04x\n", i,
682//				       MEM->tx_head[i].flag);
683//		}
684
685		while( old_tx != lp->new_tx) {
686			struct lance_tx_head *head = &(MEM->tx_head[old_tx]);
687
688			DPRINTK(3, ("on tx_ring %d\n", old_tx));
689
690			if (head->flag & TMD1_OWN_CHIP)
691				break; /* It still hasn't been Txed */
692
693			if (head->flag & TMD1_ERR) {
694				int status = head->misc;
695				dev->stats.tx_errors++;
696				if (status & TMD3_RTRY) dev->stats.tx_aborted_errors++;
697				if (status & TMD3_LCAR) dev->stats.tx_carrier_errors++;
698				if (status & TMD3_LCOL) dev->stats.tx_window_errors++;
699				if (status & (TMD3_UFLO | TMD3_BUFF)) {
700					dev->stats.tx_fifo_errors++;
701					printk("%s: Tx FIFO error\n",
702					       dev->name);
703					REGA(CSR0) = CSR0_STOP;
704					REGA(CSR3) = CSR3_BSWP;
705					lance_init_ring(dev);
706					REGA(CSR0) = CSR0_STRT | CSR0_INEA;
707					return IRQ_HANDLED;
708				}
709			} else if(head->flag & (TMD1_ENP | TMD1_STP)) {
710
711				head->flag &= ~(TMD1_ENP | TMD1_STP);
712				if(head->flag & (TMD1_ONE | TMD1_MORE))
713					dev->stats.collisions++;
714
715				dev->stats.tx_packets++;
716				DPRINTK(3, ("cleared tx ring %d\n", old_tx));
717			}
718			old_tx = (old_tx +1) & TX_RING_MOD_MASK;
719		}
720
721		lp->old_tx = old_tx;
722	}
723
724
725	if (netif_queue_stopped(dev)) {
726		/* The ring is no longer full, clear tbusy. */
727		netif_start_queue(dev);
728		netif_wake_queue(dev);
729	}
730
731	if (csr0 & CSR0_RINT)			/* Rx interrupt */
732		lance_rx( dev );
733
734	/* Log misc errors. */
735	if (csr0 & CSR0_BABL) dev->stats.tx_errors++; /* Tx babble. */
736	if (csr0 & CSR0_MISS) dev->stats.rx_errors++; /* Missed a Rx frame. */
737	if (csr0 & CSR0_MERR) {
738		DPRINTK( 1, ( "%s: Bus master arbitration failure (?!?), "
739			      "status %04x.\n", dev->name, csr0 ));
740		/* Restart the chip. */
741		REGA(CSR0) = CSR0_STOP;
742		REGA(CSR3) = CSR3_BSWP;
743		lance_init_ring(dev);
744		REGA(CSR0) = CSR0_STRT | CSR0_INEA;
745	}
746
747
748    /* Clear any other interrupt, and set interrupt enable. */
749//	DREG = CSR0_BABL | CSR0_CERR | CSR0_MISS | CSR0_MERR |
750//		   CSR0_IDON | CSR0_INEA;
751
752	REGA(CSR0) = CSR0_INEA;
753
754	if(DREG & (CSR0_RINT | CSR0_TINT)) {
755	     DPRINTK(2, ("restarting interrupt, csr0=%#04x\n", DREG));
756	     goto still_more;
757	}
758
759	DPRINTK( 2, ( "%s: exiting interrupt, csr0=%#04x.\n",
760				  dev->name, DREG ));
761	return IRQ_HANDLED;
762}
763
764/* get packet, toss into skbuff */
765static int lance_rx( struct net_device *dev )
766{
767	struct lance_private *lp = netdev_priv(dev);
768	int entry = lp->new_rx;
769
770	/* If we own the next entry, it's a new packet. Send it up. */
771	while( (MEM->rx_head[entry].flag & RMD1_OWN) == RMD1_OWN_HOST ) {
772		struct lance_rx_head *head = &(MEM->rx_head[entry]);
773		int status = head->flag;
774
775		if (status != (RMD1_ENP|RMD1_STP)) {  /* There was an error. */
776			/* There is a tricky error noted by John Murphy,
777			   <murf@perftech.com> to Russ Nelson: Even with
778			   full-sized buffers it's possible for a jabber packet to use two
779			   buffers, with only the last correctly noting the error. */
780			if (status & RMD1_ENP)	/* Only count a general error at the */
781				dev->stats.rx_errors++; /* end of a packet.*/
782			if (status & RMD1_FRAM) dev->stats.rx_frame_errors++;
783			if (status & RMD1_OFLO) dev->stats.rx_over_errors++;
784			if (status & RMD1_CRC) dev->stats.rx_crc_errors++;
785			if (status & RMD1_BUFF) dev->stats.rx_fifo_errors++;
786			head->flag &= (RMD1_ENP|RMD1_STP);
787		} else {
788			/* Malloc up new buffer, compatible with net-3. */
789//			short pkt_len = head->msg_length;// & 0xfff;
790			short pkt_len = (head->msg_length & 0xfff) - 4;
791			struct sk_buff *skb;
792
793			if (pkt_len < 60) {
794				printk( "%s: Runt packet!\n", dev->name );
795				dev->stats.rx_errors++;
796			}
797			else {
798				skb = netdev_alloc_skb(dev, pkt_len + 2);
799				if (!skb) {
800					dev->stats.rx_dropped++;
801					head->msg_length = 0;
802					head->flag |= RMD1_OWN_CHIP;
803					lp->new_rx = (lp->new_rx+1) &
804					     RX_RING_MOD_MASK;
805				}
806
807#if 0
808				if (lance_debug >= 3) {
809					u_char *data = PKTBUF_ADDR(head);
810					printk("%s: RX pkt %d type 0x%04x"
811					       " from %pM to %pM",
812					       dev->name, lp->new_tx, ((u_short *)data)[6],
813					       &data[6], data);
814
815					printk(" data %02x %02x %02x %02x %02x %02x %02x %02x "
816					       "len %d at %08x\n",
817					       data[15], data[16], data[17], data[18],
818					       data[19], data[20], data[21], data[22],
819					       pkt_len, data);
820				}
821#endif
822				if (lance_debug >= 3) {
823					u_char *data = PKTBUF_ADDR(head);
824					printk( "%s: RX pkt %d type 0x%04x len %d\n ", dev->name, entry, ((u_short *)data)[6], pkt_len);
825				}
826
827
828				skb_reserve( skb, 2 );	/* 16 byte align */
829				skb_put( skb, pkt_len );	/* Make room */
830				skb_copy_to_linear_data(skb,
831						 PKTBUF_ADDR(head),
832						 pkt_len);
833
834				skb->protocol = eth_type_trans( skb, dev );
835				netif_rx( skb );
836				dev->stats.rx_packets++;
837				dev->stats.rx_bytes += pkt_len;
838			}
839		}
840
841//		head->buf_length = -PKT_BUF_SZ | 0xf000;
842		head->msg_length = 0;
843		head->flag = RMD1_OWN_CHIP;
844
845		entry = lp->new_rx = (lp->new_rx +1) & RX_RING_MOD_MASK;
846	}
847
848	/* From lance.c (Donald Becker): */
849	/* We should check that at least two ring entries are free.
850	   If not, we should free one and mark stats->rx_dropped++. */
851
852	return 0;
853}
854
855
856static int lance_close( struct net_device *dev )
857{
858	struct lance_private *lp = netdev_priv(dev);
859
860	netif_stop_queue(dev);
861
862	AREG = CSR0;
863
864	DPRINTK( 2, ( "%s: Shutting down ethercard, status was %2.2x.\n",
865				  dev->name, DREG ));
866
867	/* We stop the LANCE here -- it occasionally polls
868	   memory if we don't. */
869	DREG = CSR0_STOP;
870	return 0;
871}
872
873
874/* Set or clear the multicast filter for this adaptor.
875   num_addrs == -1		Promiscuous mode, receive all packets
876   num_addrs == 0		Normal mode, clear multicast list
877   num_addrs > 0		Multicast mode, receive normal and MC packets, and do
878						best-effort filtering.
879 */
880
881/* completely untested on a sun3 */
882static void set_multicast_list( struct net_device *dev )
883{
884	struct lance_private *lp = netdev_priv(dev);
885
886	if(netif_queue_stopped(dev))
887		/* Only possible if board is already started */
888		return;
889
890	/* We take the simple way out and always enable promiscuous mode. */
891	DREG = CSR0_STOP; /* Temporarily stop the lance. */
892
893	if (dev->flags & IFF_PROMISC) {
894		/* Log any net taps. */
895		DPRINTK( 3, ( "%s: Promiscuous mode enabled.\n", dev->name ));
896		REGA( CSR15 ) = 0x8000; /* Set promiscuous mode */
897	} else {
898		short multicast_table[4];
899		int num_addrs = netdev_mc_count(dev);
900		int i;
901		/* We don't use the multicast table, but rely on upper-layer
902		 * filtering. */
903		memset( multicast_table, (num_addrs == 0) ? 0 : -1,
904				sizeof(multicast_table) );
905		for( i = 0; i < 4; i++ )
906			REGA( CSR8+i ) = multicast_table[i];
907		REGA( CSR15 ) = 0; /* Unset promiscuous mode */
908	}
909
910	/*
911	 * Always set BSWP after a STOP as STOP puts it back into
912	 * little endian mode.
913	 */
914	REGA( CSR3 ) = CSR3_BSWP;
915
916	/* Resume normal operation and reset AREG to CSR0 */
917	REGA( CSR0 ) = CSR0_IDON | CSR0_INEA | CSR0_STRT;
918}
919
920
921static struct net_device *sun3lance_dev;
922
923static int __init sun3lance_init(void)
924{
925	sun3lance_dev = sun3lance_probe();
926	return PTR_ERR_OR_ZERO(sun3lance_dev);
927}
928module_init(sun3lance_init);
929
930static void __exit sun3lance_cleanup(void)
931{
932	unregister_netdev(sun3lance_dev);
933#ifdef CONFIG_SUN3
934	iounmap((void __iomem *)sun3lance_dev->base_addr);
935#endif
936	free_netdev(sun3lance_dev);
937}
938module_exit(sun3lance_cleanup);