1 /*
2 * Sun3 i82586 Ethernet driver
3 *
4 * Cloned from ni52.c for the Sun3 by Sam Creasey (sammy@sammy.net)
5 *
6 * Original copyright follows:
7 * --------------------------
8 *
9 * net-3-driver for the NI5210 card (i82586 Ethernet chip)
10 *
11 * This is an extension to the Linux operating system, and is covered by the
12 * same Gnu Public License that covers that work.
13 *
14 * Alphacode 0.82 (96/09/29) for Linux 2.0.0 (or later)
15 * Copyrights (c) 1994,1995,1996 by M.Hipp (hippm@informatik.uni-tuebingen.de)
16 * --------------------------
17 *
18 * Consult ni52.c for further notes from the original driver.
19 *
20 * This incarnation currently supports the OBIO version of the i82586 chip
21 * used in certain sun3 models. It should be fairly doable to expand this
22 * to support VME if I should every acquire such a board.
23 *
24 */
25
26 static int debuglevel = 0; /* debug-printk 0: off 1: a few 2: more */
27 static int automatic_resume = 0; /* experimental .. better should be zero */
28 static int rfdadd = 0; /* rfdadd=1 may be better for 8K MEM cards */
29 static int fifo=0x8; /* don't change */
30
31 /* #define REALLY_SLOW_IO */
32
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/string.h>
36 #include <linux/errno.h>
37 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/init.h>
42 #include <asm/bitops.h>
43 #include <asm/io.h>
44 #include <asm/idprom.h>
45 #include <asm/machines.h>
46 #include <asm/sun3mmu.h>
47 #include <asm/dvma.h>
48 #include <asm/byteorder.h>
49
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/skbuff.h>
53
54 #include "sun3_82586.h"
55
56 #define DEBUG /* debug on */
57 #define SYSBUSVAL 0 /* 16 Bit */
58
59 #define sun3_attn586() {*(volatile unsigned char *)(dev->base_addr) |= IEOB_ATTEN; *(volatile unsigned char *)(dev->base_addr) &= ~IEOB_ATTEN;}
60 #define sun3_reset586() {*(volatile unsigned char *)(dev->base_addr) = 0; udelay(100); *(volatile unsigned char *)(dev->base_addr) = IEOB_NORSET;}
61 #define sun3_disint() {*(volatile unsigned char *)(dev->base_addr) &= ~IEOB_IENAB;}
62 #define sun3_enaint() {*(volatile unsigned char *)(dev->base_addr) |= IEOB_IENAB;}
63 #define sun3_active() {*(volatile unsigned char *)(dev->base_addr) |= (IEOB_IENAB|IEOB_ONAIR|IEOB_NORSET);}
64
65 #define make32(ptr16) (p->memtop + (swab16((unsigned short) (ptr16))) )
66 #define make24(ptr32) (char *)swab32(( ((unsigned long) (ptr32)) - p->base))
67 #define make16(ptr32) (swab16((unsigned short) ((unsigned long)(ptr32) - (unsigned long) p->memtop )))
68
69 /******************* how to calculate the buffers *****************************
70
71 * IMPORTANT NOTE: if you configure only one NUM_XMIT_BUFFS, the driver works
72 * --------------- in a different (more stable?) mode. Only in this mode it's
73 * possible to configure the driver with 'NO_NOPCOMMANDS'
74
75 sizeof(scp)=12; sizeof(scb)=16; sizeof(iscp)=8;
76 sizeof(scp)+sizeof(iscp)+sizeof(scb) = 36 = INIT
77 sizeof(rfd) = 24; sizeof(rbd) = 12;
78 sizeof(tbd) = 8; sizeof(transmit_cmd) = 16;
79 sizeof(nop_cmd) = 8;
80
81 * if you don't know the driver, better do not change these values: */
82
83 #define RECV_BUFF_SIZE 1536 /* slightly oversized */
84 #define XMIT_BUFF_SIZE 1536 /* slightly oversized */
85 #define NUM_XMIT_BUFFS 1 /* config for 32K shmem */
86 #define NUM_RECV_BUFFS_8 4 /* config for 32K shared mem */
87 #define NUM_RECV_BUFFS_16 9 /* config for 32K shared mem */
88 #define NUM_RECV_BUFFS_32 16 /* config for 32K shared mem */
89 #define NO_NOPCOMMANDS /* only possible with NUM_XMIT_BUFFS=1 */
90
91 /**************************************************************************/
92
93 /* different DELAYs */
94 #define DELAY(x) mdelay(32 * x);
95 #define DELAY_16(); { udelay(16); }
96 #define DELAY_18(); { udelay(4); }
97
98 /* wait for command with timeout: */
99 #define WAIT_4_SCB_CMD() \
100 { int i; \
101 for(i=0;i<16384;i++) { \
102 if(!p->scb->cmd_cuc) break; \
103 DELAY_18(); \
104 if(i == 16383) { \
105 printk("%s: scb_cmd timed out: %04x,%04x .. disabling i82586!!\n",dev->name,p->scb->cmd_cuc,p->scb->cus); \
106 if(!p->reseted) { p->reseted = 1; sun3_reset586(); } } } }
107
108 #define WAIT_4_SCB_CMD_RUC() { int i; \
109 for(i=0;i<16384;i++) { \
110 if(!p->scb->cmd_ruc) break; \
111 DELAY_18(); \
112 if(i == 16383) { \
113 printk("%s: scb_cmd (ruc) timed out: %04x,%04x .. disabling i82586!!\n",dev->name,p->scb->cmd_ruc,p->scb->rus); \
114 if(!p->reseted) { p->reseted = 1; sun3_reset586(); } } } }
115
116 #define WAIT_4_STAT_COMPL(addr) { int i; \
117 for(i=0;i<32767;i++) { \
118 if(swab16((addr)->cmd_status) & STAT_COMPL) break; \
119 DELAY_16(); DELAY_16(); } }
120
121 static int sun3_82586_probe1(struct net_device *dev,int ioaddr);
122 static void sun3_82586_interrupt(int irq,void *dev_id,struct pt_regs *reg_ptr);
123 static int sun3_82586_open(struct net_device *dev);
124 static int sun3_82586_close(struct net_device *dev);
125 static int sun3_82586_send_packet(struct sk_buff *,struct net_device *);
126 static struct net_device_stats *sun3_82586_get_stats(struct net_device *dev);
127 static void set_multicast_list(struct net_device *dev);
128 static void sun3_82586_timeout(struct net_device *dev);
129 #if 0
130 static void sun3_82586_dump(struct net_device *,void *);
131 #endif
132
133 /* helper-functions */
134 static int init586(struct net_device *dev);
135 static int check586(struct net_device *dev,char *where,unsigned size);
136 static void alloc586(struct net_device *dev);
137 static void startrecv586(struct net_device *dev);
138 static void *alloc_rfa(struct net_device *dev,void *ptr);
139 static void sun3_82586_rcv_int(struct net_device *dev);
140 static void sun3_82586_xmt_int(struct net_device *dev);
141 static void sun3_82586_rnr_int(struct net_device *dev);
142
143 struct priv
144 {
145 struct net_device_stats stats;
146 unsigned long base;
147 char *memtop;
148 long int lock;
149 int reseted;
150 volatile struct rfd_struct *rfd_last,*rfd_top,*rfd_first;
151 volatile struct scp_struct *scp; /* volatile is important */
152 volatile struct iscp_struct *iscp; /* volatile is important */
153 volatile struct scb_struct *scb; /* volatile is important */
154 volatile struct tbd_struct *xmit_buffs[NUM_XMIT_BUFFS];
155 volatile struct transmit_cmd_struct *xmit_cmds[NUM_XMIT_BUFFS];
156 #if (NUM_XMIT_BUFFS == 1)
157 volatile struct nop_cmd_struct *nop_cmds[2];
158 #else
159 volatile struct nop_cmd_struct *nop_cmds[NUM_XMIT_BUFFS];
160 #endif
161 volatile int nop_point,num_recv_buffs;
162 volatile char *xmit_cbuffs[NUM_XMIT_BUFFS];
163 volatile int xmit_count,xmit_last;
164 };
165
166 /**********************************************
167 * close device
168 */
sun3_82586_close(struct net_device * dev)169 static int sun3_82586_close(struct net_device *dev)
170 {
171 free_irq(dev->irq, dev);
172
173 sun3_reset586(); /* the hard way to stop the receiver */
174
175 netif_stop_queue(dev);
176
177 return 0;
178 }
179
180 /**********************************************
181 * open device
182 */
sun3_82586_open(struct net_device * dev)183 static int sun3_82586_open(struct net_device *dev)
184 {
185 int ret;
186
187 sun3_disint();
188 alloc586(dev);
189 init586(dev);
190 startrecv586(dev);
191 sun3_enaint();
192
193 ret = request_irq(dev->irq, &sun3_82586_interrupt,0,dev->name,dev);
194 if (ret)
195 {
196 sun3_reset586();
197 return ret;
198 }
199
200 netif_start_queue(dev);
201
202 return 0; /* most done by init */
203 }
204
205 /**********************************************
206 * Check to see if there's an 82586 out there.
207 */
check586(struct net_device * dev,char * where,unsigned size)208 static int check586(struct net_device *dev,char *where,unsigned size)
209 {
210 struct priv pb;
211 struct priv *p = /* (struct priv *) dev->priv*/ &pb;
212 char *iscp_addr;
213 int i;
214
215 p->base = (unsigned long) dvma_btov(0);
216 p->memtop = (char *)dvma_btov((unsigned long)where);
217 p->scp = (struct scp_struct *)(p->base + SCP_DEFAULT_ADDRESS);
218 memset((char *)p->scp,0, sizeof(struct scp_struct));
219 for(i=0;i<sizeof(struct scp_struct);i++) /* memory was writeable? */
220 if(((char *)p->scp)[i])
221 return 0;
222 p->scp->sysbus = SYSBUSVAL; /* 1 = 8Bit-Bus, 0 = 16 Bit */
223 if(p->scp->sysbus != SYSBUSVAL)
224 return 0;
225
226 iscp_addr = (char *)dvma_btov((unsigned long)where);
227
228 p->iscp = (struct iscp_struct *) iscp_addr;
229 memset((char *)p->iscp,0, sizeof(struct iscp_struct));
230
231 p->scp->iscp = make24(p->iscp);
232 p->iscp->busy = 1;
233
234 sun3_reset586();
235 sun3_attn586();
236 DELAY(1); /* wait a while... */
237
238 if(p->iscp->busy) /* i82586 clears 'busy' after successful init */
239 return 0;
240
241 return 1;
242 }
243
244 /******************************************************************
245 * set iscp at the right place, called by sun3_82586_probe1 and open586.
246 */
alloc586(struct net_device * dev)247 static void alloc586(struct net_device *dev)
248 {
249 struct priv *p = (struct priv *) dev->priv;
250
251 sun3_reset586();
252 DELAY(1);
253
254 p->scp = (struct scp_struct *) (p->base + SCP_DEFAULT_ADDRESS);
255 p->iscp = (struct iscp_struct *) dvma_btov(dev->mem_start);
256 p->scb = (struct scb_struct *) ((char *)p->iscp + sizeof(struct iscp_struct));
257
258 memset((char *) p->iscp,0,sizeof(struct iscp_struct));
259 memset((char *) p->scp ,0,sizeof(struct scp_struct));
260
261 p->scp->iscp = make24(p->iscp);
262 p->scp->sysbus = SYSBUSVAL;
263 p->iscp->scb_offset = make16(p->scb);
264 p->iscp->scb_base = make24(dvma_btov(dev->mem_start));
265
266 p->iscp->busy = 1;
267 sun3_reset586();
268 sun3_attn586();
269
270 DELAY(1);
271
272 if(p->iscp->busy)
273 printk("%s: Init-Problems (alloc).\n",dev->name);
274
275 p->reseted = 0;
276
277 memset((char *)p->scb,0,sizeof(struct scb_struct));
278 }
279
sun3_82586_probe(struct net_device * dev)280 int __init sun3_82586_probe(struct net_device *dev)
281 {
282 unsigned long ioaddr;
283 static int found = 0;
284
285 /* check that this machine has an onboard 82586 */
286 switch(idprom->id_machtype) {
287 case SM_SUN3|SM_3_160:
288 case SM_SUN3|SM_3_260:
289 /* these machines have 82586 */
290 break;
291
292 default:
293 return(-ENODEV);
294 }
295
296 if(found)
297 return -ENODEV;
298
299 ioaddr = (unsigned long)ioremap(IE_OBIO, PAGE_SIZE);
300 found = 1;
301
302 SET_MODULE_OWNER(dev);
303
304 dev->irq = IE_IRQ;
305 dev->base_addr = ioaddr;
306 if(sun3_82586_probe1(dev, ioaddr) == 0)
307 return 0;
308
309 return -ENODEV;
310 }
311
sun3_82586_probe1(struct net_device * dev,int ioaddr)312 static int __init sun3_82586_probe1(struct net_device *dev,int ioaddr)
313 {
314 int i, size, retval;
315
316 // if (!request_region(ioaddr, SUN3_82586_TOTAL_SIZE, dev->name))
317 // return -EBUSY;
318
319 /* copy in the ethernet address from the prom */
320 for(i = 0; i < 6 ; i++)
321 dev->dev_addr[i] = idprom->id_ethaddr[i];
322
323 printk("%s: SUN3 Intel 82586 found at %lx, ",dev->name,dev->base_addr);
324
325 /*
326 * check (or search) IO-Memory, 32K
327 */
328 size = 0x8000;
329
330 dev->mem_start = (unsigned long)dvma_malloc_align(0x8000, 0x1000);
331 dev->mem_end = dev->mem_start + size;
332
333 if(size != 0x2000 && size != 0x4000 && size != 0x8000) {
334 printk("\n%s: Illegal memory size %d. Allowed is 0x2000 or 0x4000 or 0x8000 bytes.\n",dev->name,size);
335 retval = -ENODEV;
336 goto out;
337 }
338 if(!check586(dev,(char *) dev->mem_start,size)) {
339 printk("?memcheck, Can't find memory at 0x%lx with size %d!\n",dev->mem_start,size);
340 retval = -ENODEV;
341 goto out;
342 }
343
344 dev->priv = (void *) kmalloc(sizeof(struct priv),GFP_KERNEL);
345 if(dev->priv == NULL) {
346 printk("%s: Ooops .. can't allocate private driver memory.\n",dev->name);
347 retval = -ENOMEM;
348 goto out;
349 }
350
351 /* warning: we don't free it on errors */
352 memset((char *) dev->priv,0,sizeof(struct priv));
353
354 ((struct priv *) (dev->priv))->memtop = (char *)dvma_btov(dev->mem_start);
355 ((struct priv *) (dev->priv))->base = (unsigned long) dvma_btov(0);
356 alloc586(dev);
357
358 /* set number of receive-buffs according to memsize */
359 if(size == 0x2000)
360 ((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_8;
361 else if(size == 0x4000)
362 ((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_16;
363 else
364 ((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_32;
365
366 printk("Memaddr: 0x%lx, Memsize: %d, IRQ %d\n",dev->mem_start,size, dev->irq);
367
368 dev->open = sun3_82586_open;
369 dev->stop = sun3_82586_close;
370 dev->get_stats = sun3_82586_get_stats;
371 dev->tx_timeout = sun3_82586_timeout;
372 dev->watchdog_timeo = HZ/20;
373 dev->hard_start_xmit = sun3_82586_send_packet;
374 dev->set_multicast_list = set_multicast_list;
375
376 dev->if_port = 0;
377
378 ether_setup(dev);
379
380 return 0;
381 out:
382 return retval;
383 }
384
385
init586(struct net_device * dev)386 static int init586(struct net_device *dev)
387 {
388 void *ptr;
389 int i,result=0;
390 struct priv *p = (struct priv *) dev->priv;
391 volatile struct configure_cmd_struct *cfg_cmd;
392 volatile struct iasetup_cmd_struct *ias_cmd;
393 volatile struct tdr_cmd_struct *tdr_cmd;
394 volatile struct mcsetup_cmd_struct *mc_cmd;
395 struct dev_mc_list *dmi=dev->mc_list;
396 int num_addrs=dev->mc_count;
397
398 ptr = (void *) ((char *)p->scb + sizeof(struct scb_struct));
399
400 cfg_cmd = (struct configure_cmd_struct *)ptr; /* configure-command */
401 cfg_cmd->cmd_status = 0;
402 cfg_cmd->cmd_cmd = swab16(CMD_CONFIGURE | CMD_LAST);
403 cfg_cmd->cmd_link = 0xffff;
404
405 cfg_cmd->byte_cnt = 0x0a; /* number of cfg bytes */
406 cfg_cmd->fifo = fifo; /* fifo-limit (8=tx:32/rx:64) */
407 cfg_cmd->sav_bf = 0x40; /* hold or discard bad recv frames (bit 7) */
408 cfg_cmd->adr_len = 0x2e; /* addr_len |!src_insert |pre-len |loopback */
409 cfg_cmd->priority = 0x00;
410 cfg_cmd->ifs = 0x60;
411 cfg_cmd->time_low = 0x00;
412 cfg_cmd->time_high = 0xf2;
413 cfg_cmd->promisc = 0;
414 if(dev->flags & IFF_ALLMULTI) {
415 int len = ((char *) p->iscp - (char *) ptr - 8) / 6;
416 if(num_addrs > len) {
417 printk("%s: switching to promisc. mode\n",dev->name);
418 dev->flags|=IFF_PROMISC;
419 }
420 }
421 if(dev->flags&IFF_PROMISC)
422 {
423 cfg_cmd->promisc=1;
424 dev->flags|=IFF_PROMISC;
425 }
426 cfg_cmd->carr_coll = 0x00;
427
428 p->scb->cbl_offset = make16(cfg_cmd);
429 p->scb->cmd_ruc = 0;
430
431 p->scb->cmd_cuc = CUC_START; /* cmd.-unit start */
432 sun3_attn586();
433
434 WAIT_4_STAT_COMPL(cfg_cmd);
435
436 if((swab16(cfg_cmd->cmd_status) & (STAT_OK|STAT_COMPL)) != (STAT_COMPL|STAT_OK))
437 {
438 printk("%s: configure command failed: %x\n",dev->name,swab16(cfg_cmd->cmd_status));
439 return 1;
440 }
441
442 /*
443 * individual address setup
444 */
445
446 ias_cmd = (struct iasetup_cmd_struct *)ptr;
447
448 ias_cmd->cmd_status = 0;
449 ias_cmd->cmd_cmd = swab16(CMD_IASETUP | CMD_LAST);
450 ias_cmd->cmd_link = 0xffff;
451
452 memcpy((char *)&ias_cmd->iaddr,(char *) dev->dev_addr,ETH_ALEN);
453
454 p->scb->cbl_offset = make16(ias_cmd);
455
456 p->scb->cmd_cuc = CUC_START; /* cmd.-unit start */
457 sun3_attn586();
458
459 WAIT_4_STAT_COMPL(ias_cmd);
460
461 if((swab16(ias_cmd->cmd_status) & (STAT_OK|STAT_COMPL)) != (STAT_OK|STAT_COMPL)) {
462 printk("%s (82586): individual address setup command failed: %04x\n",dev->name,swab16(ias_cmd->cmd_status));
463 return 1;
464 }
465
466 /*
467 * TDR, wire check .. e.g. no resistor e.t.c
468 */
469
470 tdr_cmd = (struct tdr_cmd_struct *)ptr;
471
472 tdr_cmd->cmd_status = 0;
473 tdr_cmd->cmd_cmd = swab16(CMD_TDR | CMD_LAST);
474 tdr_cmd->cmd_link = 0xffff;
475 tdr_cmd->status = 0;
476
477 p->scb->cbl_offset = make16(tdr_cmd);
478 p->scb->cmd_cuc = CUC_START; /* cmd.-unit start */
479 sun3_attn586();
480
481 WAIT_4_STAT_COMPL(tdr_cmd);
482
483 if(!(swab16(tdr_cmd->cmd_status) & STAT_COMPL))
484 {
485 printk("%s: Problems while running the TDR.\n",dev->name);
486 }
487 else
488 {
489 DELAY_16(); /* wait for result */
490 result = swab16(tdr_cmd->status);
491
492 p->scb->cmd_cuc = p->scb->cus & STAT_MASK;
493 sun3_attn586(); /* ack the interrupts */
494
495 if(result & TDR_LNK_OK)
496 ;
497 else if(result & TDR_XCVR_PRB)
498 printk("%s: TDR: Transceiver problem. Check the cable(s)!\n",dev->name);
499 else if(result & TDR_ET_OPN)
500 printk("%s: TDR: No correct termination %d clocks away.\n",dev->name,result & TDR_TIMEMASK);
501 else if(result & TDR_ET_SRT)
502 {
503 if (result & TDR_TIMEMASK) /* time == 0 -> strange :-) */
504 printk("%s: TDR: Detected a short circuit %d clocks away.\n",dev->name,result & TDR_TIMEMASK);
505 }
506 else
507 printk("%s: TDR: Unknown status %04x\n",dev->name,result);
508 }
509
510 /*
511 * Multicast setup
512 */
513 if(num_addrs && !(dev->flags & IFF_PROMISC) )
514 {
515 mc_cmd = (struct mcsetup_cmd_struct *) ptr;
516 mc_cmd->cmd_status = 0;
517 mc_cmd->cmd_cmd = swab16(CMD_MCSETUP | CMD_LAST);
518 mc_cmd->cmd_link = 0xffff;
519 mc_cmd->mc_cnt = swab16(num_addrs * 6);
520
521 for(i=0;i<num_addrs;i++,dmi=dmi->next)
522 memcpy((char *) mc_cmd->mc_list[i], dmi->dmi_addr,6);
523
524 p->scb->cbl_offset = make16(mc_cmd);
525 p->scb->cmd_cuc = CUC_START;
526 sun3_attn586();
527
528 WAIT_4_STAT_COMPL(mc_cmd);
529
530 if( (swab16(mc_cmd->cmd_status) & (STAT_COMPL|STAT_OK)) != (STAT_COMPL|STAT_OK) )
531 printk("%s: Can't apply multicast-address-list.\n",dev->name);
532 }
533
534 /*
535 * alloc nop/xmit-cmds
536 */
537 #if (NUM_XMIT_BUFFS == 1)
538 for(i=0;i<2;i++)
539 {
540 p->nop_cmds[i] = (struct nop_cmd_struct *)ptr;
541 p->nop_cmds[i]->cmd_cmd = swab16(CMD_NOP);
542 p->nop_cmds[i]->cmd_status = 0;
543 p->nop_cmds[i]->cmd_link = make16((p->nop_cmds[i]));
544 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
545 }
546 #else
547 for(i=0;i<NUM_XMIT_BUFFS;i++)
548 {
549 p->nop_cmds[i] = (struct nop_cmd_struct *)ptr;
550 p->nop_cmds[i]->cmd_cmd = swab16(CMD_NOP);
551 p->nop_cmds[i]->cmd_status = 0;
552 p->nop_cmds[i]->cmd_link = make16((p->nop_cmds[i]));
553 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
554 }
555 #endif
556
557 ptr = alloc_rfa(dev,(void *)ptr); /* init receive-frame-area */
558
559 /*
560 * alloc xmit-buffs / init xmit_cmds
561 */
562 for(i=0;i<NUM_XMIT_BUFFS;i++)
563 {
564 p->xmit_cmds[i] = (struct transmit_cmd_struct *)ptr; /*transmit cmd/buff 0*/
565 ptr = (char *) ptr + sizeof(struct transmit_cmd_struct);
566 p->xmit_cbuffs[i] = (char *)ptr; /* char-buffs */
567 ptr = (char *) ptr + XMIT_BUFF_SIZE;
568 p->xmit_buffs[i] = (struct tbd_struct *)ptr; /* TBD */
569 ptr = (char *) ptr + sizeof(struct tbd_struct);
570 if((void *)ptr > (void *)dev->mem_end)
571 {
572 printk("%s: not enough shared-mem for your configuration!\n",dev->name);
573 return 1;
574 }
575 memset((char *)(p->xmit_cmds[i]) ,0, sizeof(struct transmit_cmd_struct));
576 memset((char *)(p->xmit_buffs[i]),0, sizeof(struct tbd_struct));
577 p->xmit_cmds[i]->cmd_link = make16(p->nop_cmds[(i+1)%NUM_XMIT_BUFFS]);
578 p->xmit_cmds[i]->cmd_status = swab16(STAT_COMPL);
579 p->xmit_cmds[i]->cmd_cmd = swab16(CMD_XMIT | CMD_INT);
580 p->xmit_cmds[i]->tbd_offset = make16((p->xmit_buffs[i]));
581 p->xmit_buffs[i]->next = 0xffff;
582 p->xmit_buffs[i]->buffer = make24((p->xmit_cbuffs[i]));
583 }
584
585 p->xmit_count = 0;
586 p->xmit_last = 0;
587 #ifndef NO_NOPCOMMANDS
588 p->nop_point = 0;
589 #endif
590
591 /*
592 * 'start transmitter'
593 */
594 #ifndef NO_NOPCOMMANDS
595 p->scb->cbl_offset = make16(p->nop_cmds[0]);
596 p->scb->cmd_cuc = CUC_START;
597 sun3_attn586();
598 WAIT_4_SCB_CMD();
599 #else
600 p->xmit_cmds[0]->cmd_link = make16(p->xmit_cmds[0]);
601 p->xmit_cmds[0]->cmd_cmd = swab16(CMD_XMIT | CMD_SUSPEND | CMD_INT);
602 #endif
603
604 /*
605 * ack. interrupts
606 */
607 p->scb->cmd_cuc = p->scb->cus & STAT_MASK;
608 sun3_attn586();
609 DELAY_16();
610
611 sun3_enaint();
612 sun3_active();
613
614 return 0;
615 }
616
617 /******************************************************
618 * This is a helper routine for sun3_82586_rnr_int() and init586().
619 * It sets up the Receive Frame Area (RFA).
620 */
621
alloc_rfa(struct net_device * dev,void * ptr)622 static void *alloc_rfa(struct net_device *dev,void *ptr)
623 {
624 volatile struct rfd_struct *rfd = (struct rfd_struct *)ptr;
625 volatile struct rbd_struct *rbd;
626 int i;
627 struct priv *p = (struct priv *) dev->priv;
628
629 memset((char *) rfd,0,sizeof(struct rfd_struct)*(p->num_recv_buffs+rfdadd));
630 p->rfd_first = rfd;
631
632 for(i = 0; i < (p->num_recv_buffs+rfdadd); i++) {
633 rfd[i].next = make16(rfd + (i+1) % (p->num_recv_buffs+rfdadd) );
634 rfd[i].rbd_offset = 0xffff;
635 }
636 rfd[p->num_recv_buffs-1+rfdadd].last = RFD_SUSP; /* RU suspend */
637
638 ptr = (void *) (rfd + (p->num_recv_buffs + rfdadd) );
639
640 rbd = (struct rbd_struct *) ptr;
641 ptr = (void *) (rbd + p->num_recv_buffs);
642
643 /* clr descriptors */
644 memset((char *) rbd,0,sizeof(struct rbd_struct)*(p->num_recv_buffs));
645
646 for(i=0;i<p->num_recv_buffs;i++)
647 {
648 rbd[i].next = make16((rbd + (i+1) % p->num_recv_buffs));
649 rbd[i].size = swab16(RECV_BUFF_SIZE);
650 rbd[i].buffer = make24(ptr);
651 ptr = (char *) ptr + RECV_BUFF_SIZE;
652 }
653
654 p->rfd_top = p->rfd_first;
655 p->rfd_last = p->rfd_first + (p->num_recv_buffs - 1 + rfdadd);
656
657 p->scb->rfa_offset = make16(p->rfd_first);
658 p->rfd_first->rbd_offset = make16(rbd);
659
660 return ptr;
661 }
662
663
664 /**************************************************
665 * Interrupt Handler ...
666 */
667
sun3_82586_interrupt(int irq,void * dev_id,struct pt_regs * reg_ptr)668 static void sun3_82586_interrupt(int irq,void *dev_id,struct pt_regs *reg_ptr)
669 {
670 struct net_device *dev = dev_id;
671 unsigned short stat;
672 int cnt=0;
673 struct priv *p;
674
675 if (!dev) {
676 printk ("sun3_82586-interrupt: irq %d for unknown device.\n",irq);
677 return;
678 }
679 p = (struct priv *) dev->priv;
680
681 if(debuglevel > 1)
682 printk("I");
683
684 WAIT_4_SCB_CMD(); /* wait for last command */
685
686 while((stat=p->scb->cus & STAT_MASK))
687 {
688 p->scb->cmd_cuc = stat;
689 sun3_attn586();
690
691 if(stat & STAT_FR) /* received a frame */
692 sun3_82586_rcv_int(dev);
693
694 if(stat & STAT_RNR) /* RU went 'not ready' */
695 {
696 printk("(R)");
697 if(p->scb->rus & RU_SUSPEND) /* special case: RU_SUSPEND */
698 {
699 WAIT_4_SCB_CMD();
700 p->scb->cmd_ruc = RUC_RESUME;
701 sun3_attn586();
702 WAIT_4_SCB_CMD_RUC();
703 }
704 else
705 {
706 printk("%s: Receiver-Unit went 'NOT READY': %04x/%02x.\n",dev->name,(int) stat,(int) p->scb->rus);
707 sun3_82586_rnr_int(dev);
708 }
709 }
710
711 if(stat & STAT_CX) /* command with I-bit set complete */
712 sun3_82586_xmt_int(dev);
713
714 #ifndef NO_NOPCOMMANDS
715 if(stat & STAT_CNA) /* CU went 'not ready' */
716 {
717 if(netif_running(dev))
718 printk("%s: oops! CU has left active state. stat: %04x/%02x.\n",dev->name,(int) stat,(int) p->scb->cus);
719 }
720 #endif
721
722 if(debuglevel > 1)
723 printk("%d",cnt++);
724
725 WAIT_4_SCB_CMD(); /* wait for ack. (sun3_82586_xmt_int can be faster than ack!!) */
726 if(p->scb->cmd_cuc) /* timed out? */
727 {
728 printk("%s: Acknowledge timed out.\n",dev->name);
729 sun3_disint();
730 break;
731 }
732 }
733
734 if(debuglevel > 1)
735 printk("i");
736 }
737
738 /*******************************************************
739 * receive-interrupt
740 */
741
sun3_82586_rcv_int(struct net_device * dev)742 static void sun3_82586_rcv_int(struct net_device *dev)
743 {
744 int status,cnt=0;
745 unsigned short totlen;
746 struct sk_buff *skb;
747 struct rbd_struct *rbd;
748 struct priv *p = (struct priv *) dev->priv;
749
750 if(debuglevel > 0)
751 printk("R");
752
753 for(;(status = p->rfd_top->stat_high) & RFD_COMPL;)
754 {
755 rbd = (struct rbd_struct *) make32(p->rfd_top->rbd_offset);
756
757 if(status & RFD_OK) /* frame received without error? */
758 {
759 if( (totlen = swab16(rbd->status)) & RBD_LAST) /* the first and the last buffer? */
760 {
761 totlen &= RBD_MASK; /* length of this frame */
762 rbd->status = 0;
763 skb = (struct sk_buff *) dev_alloc_skb(totlen+2);
764 if(skb != NULL)
765 {
766 skb->dev = dev;
767 skb_reserve(skb,2);
768 skb_put(skb,totlen);
769 eth_copy_and_sum(skb,(char *) p->base+swab32((unsigned long) rbd->buffer),totlen,0);
770 skb->protocol=eth_type_trans(skb,dev);
771 netif_rx(skb);
772 p->stats.rx_packets++;
773 }
774 else
775 p->stats.rx_dropped++;
776 }
777 else
778 {
779 int rstat;
780 /* free all RBD's until RBD_LAST is set */
781 totlen = 0;
782 while(!((rstat=swab16(rbd->status)) & RBD_LAST))
783 {
784 totlen += rstat & RBD_MASK;
785 if(!rstat)
786 {
787 printk("%s: Whoops .. no end mark in RBD list\n",dev->name);
788 break;
789 }
790 rbd->status = 0;
791 rbd = (struct rbd_struct *) make32(rbd->next);
792 }
793 totlen += rstat & RBD_MASK;
794 rbd->status = 0;
795 printk("%s: received oversized frame! length: %d\n",dev->name,totlen);
796 p->stats.rx_dropped++;
797 }
798 }
799 else /* frame !(ok), only with 'save-bad-frames' */
800 {
801 printk("%s: oops! rfd-error-status: %04x\n",dev->name,status);
802 p->stats.rx_errors++;
803 }
804 p->rfd_top->stat_high = 0;
805 p->rfd_top->last = RFD_SUSP; /* maybe exchange by RFD_LAST */
806 p->rfd_top->rbd_offset = 0xffff;
807 p->rfd_last->last = 0; /* delete RFD_SUSP */
808 p->rfd_last = p->rfd_top;
809 p->rfd_top = (struct rfd_struct *) make32(p->rfd_top->next); /* step to next RFD */
810 p->scb->rfa_offset = make16(p->rfd_top);
811
812 if(debuglevel > 0)
813 printk("%d",cnt++);
814 }
815
816 if(automatic_resume)
817 {
818 WAIT_4_SCB_CMD();
819 p->scb->cmd_ruc = RUC_RESUME;
820 sun3_attn586();
821 WAIT_4_SCB_CMD_RUC();
822 }
823
824 #ifdef WAIT_4_BUSY
825 {
826 int i;
827 for(i=0;i<1024;i++)
828 {
829 if(p->rfd_top->status)
830 break;
831 DELAY_16();
832 if(i == 1023)
833 printk("%s: RU hasn't fetched next RFD (not busy/complete)\n",dev->name);
834 }
835 }
836 #endif
837
838 #if 0
839 if(!at_least_one)
840 {
841 int i;
842 volatile struct rfd_struct *rfds=p->rfd_top;
843 volatile struct rbd_struct *rbds;
844 printk("%s: received a FC intr. without having a frame: %04x %d\n",dev->name,status,old_at_least);
845 for(i=0;i< (p->num_recv_buffs+4);i++)
846 {
847 rbds = (struct rbd_struct *) make32(rfds->rbd_offset);
848 printk("%04x:%04x ",rfds->status,rbds->status);
849 rfds = (struct rfd_struct *) make32(rfds->next);
850 }
851 printk("\nerrs: %04x %04x stat: %04x\n",(int)p->scb->rsc_errs,(int)p->scb->ovrn_errs,(int)p->scb->status);
852 printk("\nerrs: %04x %04x rus: %02x, cus: %02x\n",(int)p->scb->rsc_errs,(int)p->scb->ovrn_errs,(int)p->scb->rus,(int)p->scb->cus);
853 }
854 old_at_least = at_least_one;
855 #endif
856
857 if(debuglevel > 0)
858 printk("r");
859 }
860
861 /**********************************************************
862 * handle 'Receiver went not ready'.
863 */
864
sun3_82586_rnr_int(struct net_device * dev)865 static void sun3_82586_rnr_int(struct net_device *dev)
866 {
867 struct priv *p = (struct priv *) dev->priv;
868
869 p->stats.rx_errors++;
870
871 WAIT_4_SCB_CMD(); /* wait for the last cmd, WAIT_4_FULLSTAT?? */
872 p->scb->cmd_ruc = RUC_ABORT; /* usually the RU is in the 'no resource'-state .. abort it now. */
873 sun3_attn586();
874 WAIT_4_SCB_CMD_RUC(); /* wait for accept cmd. */
875
876 alloc_rfa(dev,(char *)p->rfd_first);
877 /* maybe add a check here, before restarting the RU */
878 startrecv586(dev); /* restart RU */
879
880 printk("%s: Receive-Unit restarted. Status: %04x\n",dev->name,p->scb->rus);
881
882 }
883
884 /**********************************************************
885 * handle xmit - interrupt
886 */
887
sun3_82586_xmt_int(struct net_device * dev)888 static void sun3_82586_xmt_int(struct net_device *dev)
889 {
890 int status;
891 struct priv *p = (struct priv *) dev->priv;
892
893 if(debuglevel > 0)
894 printk("X");
895
896 status = swab16(p->xmit_cmds[p->xmit_last]->cmd_status);
897 if(!(status & STAT_COMPL))
898 printk("%s: strange .. xmit-int without a 'COMPLETE'\n",dev->name);
899
900 if(status & STAT_OK)
901 {
902 p->stats.tx_packets++;
903 p->stats.collisions += (status & TCMD_MAXCOLLMASK);
904 }
905 else
906 {
907 p->stats.tx_errors++;
908 if(status & TCMD_LATECOLL) {
909 printk("%s: late collision detected.\n",dev->name);
910 p->stats.collisions++;
911 }
912 else if(status & TCMD_NOCARRIER) {
913 p->stats.tx_carrier_errors++;
914 printk("%s: no carrier detected.\n",dev->name);
915 }
916 else if(status & TCMD_LOSTCTS)
917 printk("%s: loss of CTS detected.\n",dev->name);
918 else if(status & TCMD_UNDERRUN) {
919 p->stats.tx_fifo_errors++;
920 printk("%s: DMA underrun detected.\n",dev->name);
921 }
922 else if(status & TCMD_MAXCOLL) {
923 printk("%s: Max. collisions exceeded.\n",dev->name);
924 p->stats.collisions += 16;
925 }
926 }
927
928 #if (NUM_XMIT_BUFFS > 1)
929 if( (++p->xmit_last) == NUM_XMIT_BUFFS)
930 p->xmit_last = 0;
931 #endif
932 netif_wake_queue(dev);
933 }
934
935 /***********************************************************
936 * (re)start the receiver
937 */
938
startrecv586(struct net_device * dev)939 static void startrecv586(struct net_device *dev)
940 {
941 struct priv *p = (struct priv *) dev->priv;
942
943 WAIT_4_SCB_CMD();
944 WAIT_4_SCB_CMD_RUC();
945 p->scb->rfa_offset = make16(p->rfd_first);
946 p->scb->cmd_ruc = RUC_START;
947 sun3_attn586(); /* start cmd. */
948 WAIT_4_SCB_CMD_RUC(); /* wait for accept cmd. (no timeout!!) */
949 }
950
sun3_82586_timeout(struct net_device * dev)951 static void sun3_82586_timeout(struct net_device *dev)
952 {
953 struct priv *p = (struct priv *) dev->priv;
954 #ifndef NO_NOPCOMMANDS
955 if(p->scb->cus & CU_ACTIVE) /* COMMAND-UNIT active? */
956 {
957 netif_wake_queue(dev);
958 #ifdef DEBUG
959 printk("%s: strange ... timeout with CU active?!?\n",dev->name);
960 printk("%s: X0: %04x N0: %04x N1: %04x %d\n",dev->name,(int)swab16(p->xmit_cmds[0]->cmd_status),(int)swab16(p->nop_cmds[0]->cmd_status),(int)swab16(p->nop_cmds[1]->cmd_status),(int)p->nop_point);
961 #endif
962 p->scb->cmd_cuc = CUC_ABORT;
963 sun3_attn586();
964 WAIT_4_SCB_CMD();
965 p->scb->cbl_offset = make16(p->nop_cmds[p->nop_point]);
966 p->scb->cmd_cuc = CUC_START;
967 sun3_attn586();
968 WAIT_4_SCB_CMD();
969 dev->trans_start = jiffies;
970 return 0;
971 }
972 #endif
973 {
974 #ifdef DEBUG
975 printk("%s: xmitter timed out, try to restart! stat: %02x\n",dev->name,p->scb->cus);
976 printk("%s: command-stats: %04x %04x\n",dev->name,swab16(p->xmit_cmds[0]->cmd_status),swab16(p->xmit_cmds[1]->cmd_status));
977 printk("%s: check, whether you set the right interrupt number!\n",dev->name);
978 #endif
979 sun3_82586_close(dev);
980 sun3_82586_open(dev);
981 }
982 dev->trans_start = jiffies;
983 }
984
985 /******************************************************
986 * send frame
987 */
988
sun3_82586_send_packet(struct sk_buff * skb,struct net_device * dev)989 static int sun3_82586_send_packet(struct sk_buff *skb, struct net_device *dev)
990 {
991 int len,i;
992 #ifndef NO_NOPCOMMANDS
993 int next_nop;
994 #endif
995 struct priv *p = (struct priv *) dev->priv;
996
997 if(skb->len > XMIT_BUFF_SIZE)
998 {
999 printk("%s: Sorry, max. framelength is %d bytes. The length of your frame is %d bytes.\n",dev->name,XMIT_BUFF_SIZE,skb->len);
1000 return 0;
1001 }
1002
1003 netif_stop_queue(dev);
1004
1005 #if(NUM_XMIT_BUFFS > 1)
1006 if(test_and_set_bit(0,(void *) &p->lock)) {
1007 printk("%s: Queue was locked\n",dev->name);
1008 return 1;
1009 }
1010 else
1011 #endif
1012 {
1013 len = skb->len;
1014 if(len < ETH_ZLEN)
1015 {
1016 memset((char *)p->xmit_cbuffs[p->xmit_count], 0, ETH_ZLEN);
1017 len = ETH_ZLEN;
1018 }
1019 memcpy((char *)p->xmit_cbuffs[p->xmit_count],(char *)(skb->data),skb->len);
1020
1021 #if (NUM_XMIT_BUFFS == 1)
1022 # ifdef NO_NOPCOMMANDS
1023
1024 #ifdef DEBUG
1025 if(p->scb->cus & CU_ACTIVE)
1026 {
1027 printk("%s: Hmmm .. CU is still running and we wanna send a new packet.\n",dev->name);
1028 printk("%s: stat: %04x %04x\n",dev->name,p->scb->cus,swab16(p->xmit_cmds[0]->cmd_status));
1029 }
1030 #endif
1031
1032 p->xmit_buffs[0]->size = swab16(TBD_LAST | len);
1033 for(i=0;i<16;i++)
1034 {
1035 p->xmit_cmds[0]->cmd_status = 0;
1036 WAIT_4_SCB_CMD();
1037 if( (p->scb->cus & CU_STATUS) == CU_SUSPEND)
1038 p->scb->cmd_cuc = CUC_RESUME;
1039 else
1040 {
1041 p->scb->cbl_offset = make16(p->xmit_cmds[0]);
1042 p->scb->cmd_cuc = CUC_START;
1043 }
1044
1045 sun3_attn586();
1046 dev->trans_start = jiffies;
1047 if(!i)
1048 dev_kfree_skb(skb);
1049 WAIT_4_SCB_CMD();
1050 if( (p->scb->cus & CU_ACTIVE)) /* test it, because CU sometimes doesn't start immediately */
1051 break;
1052 if(p->xmit_cmds[0]->cmd_status)
1053 break;
1054 if(i==15)
1055 printk("%s: Can't start transmit-command.\n",dev->name);
1056 }
1057 # else
1058 next_nop = (p->nop_point + 1) & 0x1;
1059 p->xmit_buffs[0]->size = swab16(TBD_LAST | len);
1060
1061 p->xmit_cmds[0]->cmd_link = p->nop_cmds[next_nop]->cmd_link
1062 = make16((p->nop_cmds[next_nop]));
1063 p->xmit_cmds[0]->cmd_status = p->nop_cmds[next_nop]->cmd_status = 0;
1064
1065 p->nop_cmds[p->nop_point]->cmd_link = make16((p->xmit_cmds[0]));
1066 dev->trans_start = jiffies;
1067 p->nop_point = next_nop;
1068 dev_kfree_skb(skb);
1069 # endif
1070 #else
1071 p->xmit_buffs[p->xmit_count]->size = swab16(TBD_LAST | len);
1072 if( (next_nop = p->xmit_count + 1) == NUM_XMIT_BUFFS )
1073 next_nop = 0;
1074
1075 p->xmit_cmds[p->xmit_count]->cmd_status = 0;
1076 /* linkpointer of xmit-command already points to next nop cmd */
1077 p->nop_cmds[next_nop]->cmd_link = make16((p->nop_cmds[next_nop]));
1078 p->nop_cmds[next_nop]->cmd_status = 0;
1079
1080 p->nop_cmds[p->xmit_count]->cmd_link = make16((p->xmit_cmds[p->xmit_count]));
1081 dev->trans_start = jiffies;
1082 p->xmit_count = next_nop;
1083
1084 {
1085 unsigned long flags;
1086 save_flags(flags);
1087 cli();
1088 if(p->xmit_count != p->xmit_last)
1089 netif_wake_queue(dev);
1090 p->lock = 0;
1091 restore_flags(flags);
1092 }
1093 dev_kfree_skb(skb);
1094 #endif
1095 }
1096 return 0;
1097 }
1098
1099 /*******************************************
1100 * Someone wanna have the statistics
1101 */
1102
sun3_82586_get_stats(struct net_device * dev)1103 static struct net_device_stats *sun3_82586_get_stats(struct net_device *dev)
1104 {
1105 struct priv *p = (struct priv *) dev->priv;
1106 unsigned short crc,aln,rsc,ovrn;
1107
1108 crc = swab16(p->scb->crc_errs); /* get error-statistic from the ni82586 */
1109 p->scb->crc_errs = 0;
1110 aln = swab16(p->scb->aln_errs);
1111 p->scb->aln_errs = 0;
1112 rsc = swab16(p->scb->rsc_errs);
1113 p->scb->rsc_errs = 0;
1114 ovrn = swab16(p->scb->ovrn_errs);
1115 p->scb->ovrn_errs = 0;
1116
1117 p->stats.rx_crc_errors += crc;
1118 p->stats.rx_fifo_errors += ovrn;
1119 p->stats.rx_frame_errors += aln;
1120 p->stats.rx_dropped += rsc;
1121
1122 return &p->stats;
1123 }
1124
1125 /********************************************************
1126 * Set MC list ..
1127 */
1128
set_multicast_list(struct net_device * dev)1129 static void set_multicast_list(struct net_device *dev)
1130 {
1131 netif_stop_queue(dev);
1132 sun3_disint();
1133 alloc586(dev);
1134 init586(dev);
1135 startrecv586(dev);
1136 sun3_enaint();
1137 netif_wake_queue(dev);
1138 }
1139
1140 #ifdef MODULE
1141 #error This code is not currently supported as a module
1142 static struct net_device dev_sun3_82586;
1143
init_module(void)1144 int init_module(void)
1145 {
1146 dev_sun3_82586.init = sun3_82586_probe;
1147 if (register_netdev(&dev_sun3_82586) != 0)
1148 return -EIO;
1149 return 0;
1150 }
1151
cleanup_module(void)1152 void cleanup_module(void)
1153 {
1154 unregister_netdev(&dev_sun3_82586);
1155 kfree(dev_sun3_82586.priv);
1156 dev_sun3_82586.priv = NULL;
1157 }
1158 #endif /* MODULE */
1159
1160 #if 0
1161 /*
1162 * DUMP .. we expect a not running CMD unit and enough space
1163 */
1164 void sun3_82586_dump(struct net_device *dev,void *ptr)
1165 {
1166 struct priv *p = (struct priv *) dev->priv;
1167 struct dump_cmd_struct *dump_cmd = (struct dump_cmd_struct *) ptr;
1168 int i;
1169
1170 p->scb->cmd_cuc = CUC_ABORT;
1171 sun3_attn586();
1172 WAIT_4_SCB_CMD();
1173 WAIT_4_SCB_CMD_RUC();
1174
1175 dump_cmd->cmd_status = 0;
1176 dump_cmd->cmd_cmd = CMD_DUMP | CMD_LAST;
1177 dump_cmd->dump_offset = make16((dump_cmd + 1));
1178 dump_cmd->cmd_link = 0xffff;
1179
1180 p->scb->cbl_offset = make16(dump_cmd);
1181 p->scb->cmd_cuc = CUC_START;
1182 sun3_attn586();
1183 WAIT_4_STAT_COMPL(dump_cmd);
1184
1185 if( (dump_cmd->cmd_status & (STAT_COMPL|STAT_OK)) != (STAT_COMPL|STAT_OK) )
1186 printk("%s: Can't get dump information.\n",dev->name);
1187
1188 for(i=0;i<170;i++) {
1189 printk("%02x ",(int) ((unsigned char *) (dump_cmd + 1))[i]);
1190 if(i % 24 == 23)
1191 printk("\n");
1192 }
1193 printk("\n");
1194 }
1195 #endif
1196
1197 MODULE_LICENSE("GPL");
1198