1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102static int debuglevel;
103static int automatic_resume;
104static int rfdadd;
105static int fifo = 0x8;
106
107#include <linux/module.h>
108#include <linux/kernel.h>
109#include <linux/string.h>
110#include <linux/errno.h>
111#include <linux/ioport.h>
112#include <linux/interrupt.h>
113#include <linux/delay.h>
114#include <linux/init.h>
115#include <linux/bitops.h>
116#include <asm/io.h>
117
118#include <linux/netdevice.h>
119#include <linux/etherdevice.h>
120#include <linux/skbuff.h>
121
122#include "ni52.h"
123
124#define DRV_NAME "ni52"
125
126#define DEBUG
127#define SYSBUSVAL 1
128
129#define ni_attn586() { outb(0, dev->base_addr + NI52_ATTENTION); }
130#define ni_reset586() { outb(0, dev->base_addr + NI52_RESET); }
131#define ni_disint() { outb(0, dev->base_addr + NI52_INTDIS); }
132#define ni_enaint() { outb(0, dev->base_addr + NI52_INTENA); }
133
134#define make32(ptr16) ((void __iomem *)(p->memtop + (short) (ptr16)))
135#define make24(ptr32) ((char __iomem *)(ptr32)) - p->base
136#define make16(ptr32) ((unsigned short) ((char __iomem *)(ptr32)\
137 - p->memtop))
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153#define RECV_BUFF_SIZE 1524
154#define XMIT_BUFF_SIZE 1524
155#define NUM_XMIT_BUFFS 1
156#define NUM_RECV_BUFFS_8 4
157#define NUM_RECV_BUFFS_16 9
158#define NO_NOPCOMMANDS
159
160
161
162
163#define NI52_TOTAL_SIZE 16
164#define NI52_ADDR0 0x02
165#define NI52_ADDR1 0x07
166#define NI52_ADDR2 0x01
167
168static int ni52_probe1(struct net_device *dev, int ioaddr);
169static irqreturn_t ni52_interrupt(int irq, void *dev_id);
170static int ni52_open(struct net_device *dev);
171static int ni52_close(struct net_device *dev);
172static netdev_tx_t ni52_send_packet(struct sk_buff *, struct net_device *);
173static struct net_device_stats *ni52_get_stats(struct net_device *dev);
174static void set_multicast_list(struct net_device *dev);
175static void ni52_timeout(struct net_device *dev);
176
177
178static int init586(struct net_device *dev);
179static int check586(struct net_device *dev, unsigned size);
180static void alloc586(struct net_device *dev);
181static void startrecv586(struct net_device *dev);
182static void __iomem *alloc_rfa(struct net_device *dev, void __iomem *ptr);
183static void ni52_rcv_int(struct net_device *dev);
184static void ni52_xmt_int(struct net_device *dev);
185static void ni52_rnr_int(struct net_device *dev);
186
187struct priv {
188 char __iomem *base;
189 char __iomem *mapped;
190 char __iomem *memtop;
191 spinlock_t spinlock;
192 int reset;
193 struct rfd_struct __iomem *rfd_last, *rfd_top, *rfd_first;
194 struct scp_struct __iomem *scp;
195 struct iscp_struct __iomem *iscp;
196 struct scb_struct __iomem *scb;
197 struct tbd_struct __iomem *xmit_buffs[NUM_XMIT_BUFFS];
198#if (NUM_XMIT_BUFFS == 1)
199 struct transmit_cmd_struct __iomem *xmit_cmds[2];
200 struct nop_cmd_struct __iomem *nop_cmds[2];
201#else
202 struct transmit_cmd_struct __iomem *xmit_cmds[NUM_XMIT_BUFFS];
203 struct nop_cmd_struct __iomem *nop_cmds[NUM_XMIT_BUFFS];
204#endif
205 int nop_point, num_recv_buffs;
206 char __iomem *xmit_cbuffs[NUM_XMIT_BUFFS];
207 int xmit_count, xmit_last;
208};
209
210
211static void wait_for_scb_cmd(struct net_device *dev)
212{
213 struct priv *p = netdev_priv(dev);
214 int i;
215 for (i = 0; i < 16384; i++) {
216 if (readb(&p->scb->cmd_cuc) == 0)
217 break;
218 udelay(4);
219 if (i == 16383) {
220 printk(KERN_ERR "%s: scb_cmd timed out: %04x,%04x .. disabling i82586!!\n",
221 dev->name, readb(&p->scb->cmd_cuc), readb(&p->scb->cus));
222 if (!p->reset) {
223 p->reset = 1;
224 ni_reset586();
225 }
226 }
227 }
228}
229
230static void wait_for_scb_cmd_ruc(struct net_device *dev)
231{
232 struct priv *p = netdev_priv(dev);
233 int i;
234 for (i = 0; i < 16384; i++) {
235 if (readb(&p->scb->cmd_ruc) == 0)
236 break;
237 udelay(4);
238 if (i == 16383) {
239 printk(KERN_ERR "%s: scb_cmd (ruc) timed out: %04x,%04x .. disabling i82586!!\n",
240 dev->name, readb(&p->scb->cmd_ruc),
241 readb(&p->scb->rus));
242 if (!p->reset) {
243 p->reset = 1;
244 ni_reset586();
245 }
246 }
247 }
248}
249
250static void wait_for_stat_compl(void __iomem *p)
251{
252 struct nop_cmd_struct __iomem *addr = p;
253 int i;
254 for (i = 0; i < 32767; i++) {
255 if (readw(&((addr)->cmd_status)) & STAT_COMPL)
256 break;
257 udelay(32);
258 }
259}
260
261
262
263
264static int ni52_close(struct net_device *dev)
265{
266 free_irq(dev->irq, dev);
267 ni_reset586();
268 netif_stop_queue(dev);
269 return 0;
270}
271
272
273
274
275static int ni52_open(struct net_device *dev)
276{
277 int ret;
278
279 ni_disint();
280 alloc586(dev);
281 init586(dev);
282 startrecv586(dev);
283 ni_enaint();
284
285 ret = request_irq(dev->irq, ni52_interrupt, 0, dev->name, dev);
286 if (ret) {
287 ni_reset586();
288 return ret;
289 }
290 netif_start_queue(dev);
291 return 0;
292}
293
294static int check_iscp(struct net_device *dev, void __iomem *addr)
295{
296 struct iscp_struct __iomem *iscp = addr;
297 struct priv *p = netdev_priv(dev);
298 memset_io(iscp, 0, sizeof(struct iscp_struct));
299
300 writel(make24(iscp), &p->scp->iscp);
301 writeb(1, &iscp->busy);
302
303 ni_reset586();
304 ni_attn586();
305 mdelay(32);
306
307 if (readb(&iscp->busy))
308 return 0;
309 return 1;
310}
311
312
313
314
315static int check586(struct net_device *dev, unsigned size)
316{
317 struct priv *p = netdev_priv(dev);
318 int i;
319
320 p->mapped = ioremap(dev->mem_start, size);
321 if (!p->mapped)
322 return 0;
323
324 p->base = p->mapped + size - 0x01000000;
325 p->memtop = p->mapped + size;
326 p->scp = (struct scp_struct __iomem *)(p->base + SCP_DEFAULT_ADDRESS);
327 p->scb = (struct scb_struct __iomem *) p->mapped;
328 p->iscp = (struct iscp_struct __iomem *)p->scp - 1;
329 memset_io(p->scp, 0, sizeof(struct scp_struct));
330 for (i = 0; i < sizeof(struct scp_struct); i++)
331
332 if (readb((char __iomem *)p->scp + i))
333 goto Enodev;
334 writeb(SYSBUSVAL, &p->scp->sysbus);
335 if (readb(&p->scp->sysbus) != SYSBUSVAL)
336 goto Enodev;
337
338 if (!check_iscp(dev, p->mapped))
339 goto Enodev;
340 if (!check_iscp(dev, p->iscp))
341 goto Enodev;
342 return 1;
343Enodev:
344 iounmap(p->mapped);
345 return 0;
346}
347
348
349
350
351static void alloc586(struct net_device *dev)
352{
353 struct priv *p = netdev_priv(dev);
354
355 ni_reset586();
356 mdelay(32);
357
358 memset_io(p->iscp, 0, sizeof(struct iscp_struct));
359 memset_io(p->scp , 0, sizeof(struct scp_struct));
360
361 writel(make24(p->iscp), &p->scp->iscp);
362 writeb(SYSBUSVAL, &p->scp->sysbus);
363 writew(make16(p->scb), &p->iscp->scb_offset);
364
365 writeb(1, &p->iscp->busy);
366 ni_reset586();
367 ni_attn586();
368
369 mdelay(32);
370
371 if (readb(&p->iscp->busy))
372 printk(KERN_ERR "%s: Init-Problems (alloc).\n", dev->name);
373
374 p->reset = 0;
375
376 memset_io(p->scb, 0, sizeof(struct scb_struct));
377}
378
379
380static int irq = 9;
381static int io = 0x300;
382static long memstart;
383static long memend;
384
385
386
387
388struct net_device * __init ni52_probe(int unit)
389{
390 struct net_device *dev = alloc_etherdev(sizeof(struct priv));
391 static const int ports[] = {0x300, 0x280, 0x360, 0x320, 0x340, 0};
392 const int *port;
393 struct priv *p;
394 int err = 0;
395
396 if (!dev)
397 return ERR_PTR(-ENOMEM);
398
399 p = netdev_priv(dev);
400
401 if (unit >= 0) {
402 sprintf(dev->name, "eth%d", unit);
403 netdev_boot_setup_check(dev);
404 io = dev->base_addr;
405 irq = dev->irq;
406 memstart = dev->mem_start;
407 memend = dev->mem_end;
408 }
409
410 if (io > 0x1ff) {
411 err = ni52_probe1(dev, io);
412 } else if (io > 0) {
413 err = -ENXIO;
414 } else {
415 for (port = ports; *port && ni52_probe1(dev, *port) ; port++)
416 ;
417 if (*port)
418 goto got_it;
419#ifdef FULL_IO_PROBE
420 for (io = 0x200; io < 0x400 && ni52_probe1(dev, io); io += 8)
421 ;
422 if (io < 0x400)
423 goto got_it;
424#endif
425 err = -ENODEV;
426 }
427 if (err)
428 goto out;
429got_it:
430 err = register_netdev(dev);
431 if (err)
432 goto out1;
433 return dev;
434out1:
435 iounmap(p->mapped);
436 release_region(dev->base_addr, NI52_TOTAL_SIZE);
437out:
438 free_netdev(dev);
439 return ERR_PTR(err);
440}
441
442static const struct net_device_ops ni52_netdev_ops = {
443 .ndo_open = ni52_open,
444 .ndo_stop = ni52_close,
445 .ndo_get_stats = ni52_get_stats,
446 .ndo_tx_timeout = ni52_timeout,
447 .ndo_start_xmit = ni52_send_packet,
448 .ndo_set_rx_mode = set_multicast_list,
449 .ndo_change_mtu = eth_change_mtu,
450 .ndo_set_mac_address = eth_mac_addr,
451 .ndo_validate_addr = eth_validate_addr,
452};
453
454static int __init ni52_probe1(struct net_device *dev, int ioaddr)
455{
456 int i, size, retval;
457 struct priv *priv = netdev_priv(dev);
458
459 dev->base_addr = ioaddr;
460 dev->irq = irq;
461 dev->mem_start = memstart;
462 dev->mem_end = memend;
463
464 spin_lock_init(&priv->spinlock);
465
466 if (!request_region(ioaddr, NI52_TOTAL_SIZE, DRV_NAME))
467 return -EBUSY;
468
469 if (!(inb(ioaddr+NI52_MAGIC1) == NI52_MAGICVAL1) ||
470 !(inb(ioaddr+NI52_MAGIC2) == NI52_MAGICVAL2)) {
471 retval = -ENODEV;
472 goto out;
473 }
474
475 for (i = 0; i < ETH_ALEN; i++)
476 dev->dev_addr[i] = inb(dev->base_addr+i);
477
478 if (dev->dev_addr[0] != NI52_ADDR0 || dev->dev_addr[1] != NI52_ADDR1 ||
479 dev->dev_addr[2] != NI52_ADDR2) {
480 retval = -ENODEV;
481 goto out;
482 }
483
484 printk(KERN_INFO "%s: NI5210 found at %#3lx, ",
485 dev->name, dev->base_addr);
486
487
488
489
490#ifdef MODULE
491 size = dev->mem_end - dev->mem_start;
492 if (size != 0x2000 && size != 0x4000) {
493 printk("\n");
494 printk(KERN_ERR "%s: Invalid memory size %d. Allowed is 0x2000 or 0x4000 bytes.\n", dev->name, size);
495 retval = -ENODEV;
496 goto out;
497 }
498 if (!check586(dev, size)) {
499 printk(KERN_ERR "?memcheck, Can't find memory at 0x%lx with size %d!\n", dev->mem_start, size);
500 retval = -ENODEV;
501 goto out;
502 }
503#else
504 if (dev->mem_start != 0) {
505
506 size = 0x4000;
507 if (!check586(dev, size)) {
508 size = 0x2000;
509 if (!check586(dev, size)) {
510 printk(KERN_ERR "?memprobe, Can't find memory at 0x%lx!\n", dev->mem_start);
511 retval = -ENODEV;
512 goto out;
513 }
514 }
515 } else {
516 static const unsigned long memaddrs[] = {
517 0xc8000, 0xca000, 0xcc000, 0xce000, 0xd0000, 0xd2000,
518 0xd4000, 0xd6000, 0xd8000, 0xda000, 0xdc000, 0
519 };
520 for (i = 0;; i++) {
521 if (!memaddrs[i]) {
522 printk(KERN_ERR "?memprobe, Can't find io-memory!\n");
523 retval = -ENODEV;
524 goto out;
525 }
526 dev->mem_start = memaddrs[i];
527 size = 0x2000;
528 if (check586(dev, size))
529
530 break;
531 size = 0x4000;
532 if (check586(dev, size))
533
534 break;
535 }
536 }
537
538 dev->mem_end = dev->mem_start + size;
539#endif
540
541 alloc586(dev);
542
543
544 if (size == 0x2000)
545 priv->num_recv_buffs = NUM_RECV_BUFFS_8;
546 else
547 priv->num_recv_buffs = NUM_RECV_BUFFS_16;
548
549 printk(KERN_DEBUG "Memaddr: 0x%lx, Memsize: %d, ",
550 dev->mem_start, size);
551
552 if (dev->irq < 2) {
553 unsigned long irq_mask;
554
555 irq_mask = probe_irq_on();
556 ni_reset586();
557 ni_attn586();
558
559 mdelay(20);
560 dev->irq = probe_irq_off(irq_mask);
561 if (!dev->irq) {
562 printk("?autoirq, Failed to detect IRQ line!\n");
563 retval = -EAGAIN;
564 iounmap(priv->mapped);
565 goto out;
566 }
567 printk("IRQ %d (autodetected).\n", dev->irq);
568 } else {
569 if (dev->irq == 2)
570 dev->irq = 9;
571 printk("IRQ %d (assigned and not checked!).\n", dev->irq);
572 }
573
574 dev->netdev_ops = &ni52_netdev_ops;
575 dev->watchdog_timeo = HZ/20;
576
577 return 0;
578out:
579 release_region(ioaddr, NI52_TOTAL_SIZE);
580 return retval;
581}
582
583
584
585
586
587
588static int init586(struct net_device *dev)
589{
590 void __iomem *ptr;
591 int i, result = 0;
592 struct priv *p = netdev_priv(dev);
593 struct configure_cmd_struct __iomem *cfg_cmd;
594 struct iasetup_cmd_struct __iomem *ias_cmd;
595 struct tdr_cmd_struct __iomem *tdr_cmd;
596 struct mcsetup_cmd_struct __iomem *mc_cmd;
597 struct netdev_hw_addr *ha;
598 int num_addrs = netdev_mc_count(dev);
599
600 ptr = p->scb + 1;
601
602 cfg_cmd = ptr;
603 writew(0, &cfg_cmd->cmd_status);
604 writew(CMD_CONFIGURE | CMD_LAST, &cfg_cmd->cmd_cmd);
605 writew(0xFFFF, &cfg_cmd->cmd_link);
606
607
608 writeb(0x0a, &cfg_cmd->byte_cnt);
609
610 writeb(fifo, &cfg_cmd->fifo);
611
612 writeb(0x40, &cfg_cmd->sav_bf);
613
614 writeb(0x2e, &cfg_cmd->adr_len);
615 writeb(0x00, &cfg_cmd->priority);
616 writeb(0x60, &cfg_cmd->ifs);
617 writeb(0x00, &cfg_cmd->time_low);
618 writeb(0xf2, &cfg_cmd->time_high);
619 writeb(0x00, &cfg_cmd->promisc);
620 if (dev->flags & IFF_ALLMULTI) {
621 int len = ((char __iomem *)p->iscp - (char __iomem *)ptr - 8) / 6;
622 if (num_addrs > len) {
623 printk(KERN_ERR "%s: switching to promisc. mode\n",
624 dev->name);
625 writeb(0x01, &cfg_cmd->promisc);
626 }
627 }
628 if (dev->flags & IFF_PROMISC)
629 writeb(0x01, &cfg_cmd->promisc);
630 writeb(0x00, &cfg_cmd->carr_coll);
631 writew(make16(cfg_cmd), &p->scb->cbl_offset);
632 writeb(0, &p->scb->cmd_ruc);
633
634 writeb(CUC_START, &p->scb->cmd_cuc);
635 ni_attn586();
636
637 wait_for_stat_compl(cfg_cmd);
638
639 if ((readw(&cfg_cmd->cmd_status) & (STAT_OK|STAT_COMPL)) !=
640 (STAT_COMPL|STAT_OK)) {
641 printk(KERN_ERR "%s: configure command failed: %x\n",
642 dev->name, readw(&cfg_cmd->cmd_status));
643 return 1;
644 }
645
646
647
648
649
650 ias_cmd = ptr;
651
652 writew(0, &ias_cmd->cmd_status);
653 writew(CMD_IASETUP | CMD_LAST, &ias_cmd->cmd_cmd);
654 writew(0xffff, &ias_cmd->cmd_link);
655
656 memcpy_toio(&ias_cmd->iaddr, (char *)dev->dev_addr, ETH_ALEN);
657
658 writew(make16(ias_cmd), &p->scb->cbl_offset);
659
660 writeb(CUC_START, &p->scb->cmd_cuc);
661 ni_attn586();
662
663 wait_for_stat_compl(ias_cmd);
664
665 if ((readw(&ias_cmd->cmd_status) & (STAT_OK|STAT_COMPL)) !=
666 (STAT_OK|STAT_COMPL)) {
667 printk(KERN_ERR "%s (ni52): individual address setup command failed: %04x\n", dev->name, readw(&ias_cmd->cmd_status));
668 return 1;
669 }
670
671
672
673
674
675 tdr_cmd = ptr;
676
677 writew(0, &tdr_cmd->cmd_status);
678 writew(CMD_TDR | CMD_LAST, &tdr_cmd->cmd_cmd);
679 writew(0xffff, &tdr_cmd->cmd_link);
680 writew(0, &tdr_cmd->status);
681
682 writew(make16(tdr_cmd), &p->scb->cbl_offset);
683 writeb(CUC_START, &p->scb->cmd_cuc);
684 ni_attn586();
685
686 wait_for_stat_compl(tdr_cmd);
687
688 if (!(readw(&tdr_cmd->cmd_status) & STAT_COMPL))
689 printk(KERN_ERR "%s: Problems while running the TDR.\n",
690 dev->name);
691 else {
692 udelay(16);
693 result = readw(&tdr_cmd->status);
694 writeb(readb(&p->scb->cus) & STAT_MASK, &p->scb->cmd_cuc);
695 ni_attn586();
696
697 if (result & TDR_LNK_OK)
698 ;
699 else if (result & TDR_XCVR_PRB)
700 printk(KERN_ERR "%s: TDR: Transceiver problem. Check the cable(s)!\n",
701 dev->name);
702 else if (result & TDR_ET_OPN)
703 printk(KERN_ERR "%s: TDR: No correct termination %d clocks away.\n",
704 dev->name, result & TDR_TIMEMASK);
705 else if (result & TDR_ET_SRT) {
706
707 if (result & TDR_TIMEMASK)
708 printk(KERN_ERR "%s: TDR: Detected a short circuit %d clocks away.\n",
709 dev->name, result & TDR_TIMEMASK);
710 } else
711 printk(KERN_ERR "%s: TDR: Unknown status %04x\n",
712 dev->name, result);
713 }
714
715
716
717
718 if (num_addrs && !(dev->flags & IFF_PROMISC)) {
719 mc_cmd = ptr;
720 writew(0, &mc_cmd->cmd_status);
721 writew(CMD_MCSETUP | CMD_LAST, &mc_cmd->cmd_cmd);
722 writew(0xffff, &mc_cmd->cmd_link);
723 writew(num_addrs * 6, &mc_cmd->mc_cnt);
724
725 i = 0;
726 netdev_for_each_mc_addr(ha, dev)
727 memcpy_toio(mc_cmd->mc_list[i++], ha->addr, 6);
728
729 writew(make16(mc_cmd), &p->scb->cbl_offset);
730 writeb(CUC_START, &p->scb->cmd_cuc);
731 ni_attn586();
732
733 wait_for_stat_compl(mc_cmd);
734
735 if ((readw(&mc_cmd->cmd_status) & (STAT_COMPL|STAT_OK))
736 != (STAT_COMPL|STAT_OK))
737 printk(KERN_ERR "%s: Can't apply multicast-address-list.\n", dev->name);
738 }
739
740
741
742
743#if (NUM_XMIT_BUFFS == 1)
744 for (i = 0; i < 2; i++) {
745 p->nop_cmds[i] = ptr;
746 writew(CMD_NOP, &p->nop_cmds[i]->cmd_cmd);
747 writew(0, &p->nop_cmds[i]->cmd_status);
748 writew(make16(p->nop_cmds[i]), &p->nop_cmds[i]->cmd_link);
749 ptr = ptr + sizeof(struct nop_cmd_struct);
750 }
751#else
752 for (i = 0; i < NUM_XMIT_BUFFS; i++) {
753 p->nop_cmds[i] = ptr;
754 writew(CMD_NOP, &p->nop_cmds[i]->cmd_cmd);
755 writew(0, &p->nop_cmds[i]->cmd_status);
756 writew(make16(p->nop_cmds[i]), &p->nop_cmds[i]->cmd_link);
757 ptr = ptr + sizeof(struct nop_cmd_struct);
758 }
759#endif
760
761 ptr = alloc_rfa(dev, ptr);
762
763
764
765
766 for (i = 0; i < NUM_XMIT_BUFFS; i++) {
767
768 p->xmit_cmds[i] = ptr;
769 ptr = ptr + sizeof(struct transmit_cmd_struct);
770 p->xmit_cbuffs[i] = ptr;
771 ptr = ptr + XMIT_BUFF_SIZE;
772 p->xmit_buffs[i] = ptr;
773 ptr = ptr + sizeof(struct tbd_struct);
774 if ((void __iomem *)ptr > (void __iomem *)p->iscp) {
775 printk(KERN_ERR "%s: not enough shared-mem for your configuration!\n",
776 dev->name);
777 return 1;
778 }
779 memset_io(p->xmit_cmds[i], 0,
780 sizeof(struct transmit_cmd_struct));
781 memset_io(p->xmit_buffs[i], 0,
782 sizeof(struct tbd_struct));
783 writew(make16(p->nop_cmds[(i+1)%NUM_XMIT_BUFFS]),
784 &p->xmit_cmds[i]->cmd_link);
785 writew(STAT_COMPL, &p->xmit_cmds[i]->cmd_status);
786 writew(CMD_XMIT|CMD_INT, &p->xmit_cmds[i]->cmd_cmd);
787 writew(make16(p->xmit_buffs[i]), &p->xmit_cmds[i]->tbd_offset);
788 writew(0xffff, &p->xmit_buffs[i]->next);
789 writel(make24(p->xmit_cbuffs[i]), &p->xmit_buffs[i]->buffer);
790 }
791
792 p->xmit_count = 0;
793 p->xmit_last = 0;
794#ifndef NO_NOPCOMMANDS
795 p->nop_point = 0;
796#endif
797
798
799
800
801#ifndef NO_NOPCOMMANDS
802 writew(make16(p->nop_cmds[0]), &p->scb->cbl_offset);
803 writeb(CUC_START, &p->scb->cmd_cuc);
804 ni_attn586();
805 wait_for_scb_cmd(dev);
806#else
807 writew(make16(p->xmit_cmds[0]), &p->xmit_cmds[0]->cmd_link);
808 writew(CMD_XMIT | CMD_SUSPEND | CMD_INT, &p->xmit_cmds[0]->cmd_cmd);
809#endif
810
811
812
813
814 writeb(readb(&p->scb->cus) & STAT_MASK, &p->scb->cmd_cuc);
815 ni_attn586();
816 udelay(16);
817
818 ni_enaint();
819
820 return 0;
821}
822
823
824
825
826
827
828static void __iomem *alloc_rfa(struct net_device *dev, void __iomem *ptr)
829{
830 struct rfd_struct __iomem *rfd = ptr;
831 struct rbd_struct __iomem *rbd;
832 int i;
833 struct priv *p = netdev_priv(dev);
834
835 memset_io(rfd, 0,
836 sizeof(struct rfd_struct) * (p->num_recv_buffs + rfdadd));
837 p->rfd_first = rfd;
838
839 for (i = 0; i < (p->num_recv_buffs + rfdadd); i++) {
840 writew(make16(rfd + (i+1) % (p->num_recv_buffs+rfdadd)),
841 &rfd[i].next);
842 writew(0xffff, &rfd[i].rbd_offset);
843 }
844
845 writeb(RFD_SUSP, &rfd[p->num_recv_buffs-1+rfdadd].last);
846
847 ptr = rfd + (p->num_recv_buffs + rfdadd);
848
849 rbd = ptr;
850 ptr = rbd + p->num_recv_buffs;
851
852
853 memset_io(rbd, 0, sizeof(struct rbd_struct) * (p->num_recv_buffs));
854
855 for (i = 0; i < p->num_recv_buffs; i++) {
856 writew(make16(rbd + (i+1) % p->num_recv_buffs), &rbd[i].next);
857 writew(RECV_BUFF_SIZE, &rbd[i].size);
858 writel(make24(ptr), &rbd[i].buffer);
859 ptr = ptr + RECV_BUFF_SIZE;
860 }
861 p->rfd_top = p->rfd_first;
862 p->rfd_last = p->rfd_first + (p->num_recv_buffs - 1 + rfdadd);
863
864 writew(make16(p->rfd_first), &p->scb->rfa_offset);
865 writew(make16(rbd), &p->rfd_first->rbd_offset);
866
867 return ptr;
868}
869
870
871
872
873
874
875static irqreturn_t ni52_interrupt(int irq, void *dev_id)
876{
877 struct net_device *dev = dev_id;
878 unsigned int stat;
879 int cnt = 0;
880 struct priv *p;
881
882 p = netdev_priv(dev);
883
884 if (debuglevel > 1)
885 printk("I");
886
887 spin_lock(&p->spinlock);
888
889 wait_for_scb_cmd(dev);
890
891 while ((stat = readb(&p->scb->cus) & STAT_MASK)) {
892 writeb(stat, &p->scb->cmd_cuc);
893 ni_attn586();
894
895 if (stat & STAT_FR)
896 ni52_rcv_int(dev);
897
898 if (stat & STAT_RNR) {
899 printk("(R)");
900 if (readb(&p->scb->rus) & RU_SUSPEND) {
901
902 wait_for_scb_cmd(dev);
903 writeb(RUC_RESUME, &p->scb->cmd_ruc);
904 ni_attn586();
905 wait_for_scb_cmd_ruc(dev);
906 } else {
907 printk(KERN_ERR "%s: Receiver-Unit went 'NOT READY': %04x/%02x.\n",
908 dev->name, stat, readb(&p->scb->rus));
909 ni52_rnr_int(dev);
910 }
911 }
912
913
914 if (stat & STAT_CX)
915 ni52_xmt_int(dev);
916
917#ifndef NO_NOPCOMMANDS
918 if (stat & STAT_CNA) {
919 if (netif_running(dev))
920 printk(KERN_ERR "%s: oops! CU has left active state. stat: %04x/%02x.\n",
921 dev->name, stat, readb(&p->scb->cus));
922 }
923#endif
924
925 if (debuglevel > 1)
926 printk("%d", cnt++);
927
928
929 wait_for_scb_cmd(dev);
930 if (readb(&p->scb->cmd_cuc)) {
931 printk(KERN_ERR "%s: Acknowledge timed out.\n",
932 dev->name);
933 ni_disint();
934 break;
935 }
936 }
937 spin_unlock(&p->spinlock);
938
939 if (debuglevel > 1)
940 printk("i");
941 return IRQ_HANDLED;
942}
943
944
945
946
947
948static void ni52_rcv_int(struct net_device *dev)
949{
950 int status, cnt = 0;
951 unsigned short totlen;
952 struct sk_buff *skb;
953 struct rbd_struct __iomem *rbd;
954 struct priv *p = netdev_priv(dev);
955
956 if (debuglevel > 0)
957 printk("R");
958
959 for (; (status = readb(&p->rfd_top->stat_high)) & RFD_COMPL;) {
960 rbd = make32(readw(&p->rfd_top->rbd_offset));
961 if (status & RFD_OK) {
962 totlen = readw(&rbd->status);
963 if (totlen & RBD_LAST) {
964
965 totlen &= RBD_MASK;
966 writew(0x00, &rbd->status);
967 skb = netdev_alloc_skb(dev, totlen + 2);
968 if (skb != NULL) {
969 skb_reserve(skb, 2);
970 skb_put(skb, totlen);
971 memcpy_fromio(skb->data, p->base + readl(&rbd->buffer), totlen);
972 skb->protocol = eth_type_trans(skb, dev);
973 netif_rx(skb);
974 dev->stats.rx_packets++;
975 dev->stats.rx_bytes += totlen;
976 } else
977 dev->stats.rx_dropped++;
978 } else {
979 int rstat;
980
981 totlen = 0;
982 while (!((rstat = readw(&rbd->status)) & RBD_LAST)) {
983 totlen += rstat & RBD_MASK;
984 if (!rstat) {
985 printk(KERN_ERR "%s: Whoops .. no end mark in RBD list\n", dev->name);
986 break;
987 }
988 writew(0, &rbd->status);
989 rbd = make32(readw(&rbd->next));
990 }
991 totlen += rstat & RBD_MASK;
992 writew(0, &rbd->status);
993 printk(KERN_ERR "%s: received oversized frame! length: %d\n",
994 dev->name, totlen);
995 dev->stats.rx_dropped++;
996 }
997 } else {
998 printk(KERN_ERR "%s: oops! rfd-error-status: %04x\n",
999 dev->name, status);
1000 dev->stats.rx_errors++;
1001 }
1002 writeb(0, &p->rfd_top->stat_high);
1003 writeb(RFD_SUSP, &p->rfd_top->last);
1004 writew(0xffff, &p->rfd_top->rbd_offset);
1005 writeb(0, &p->rfd_last->last);
1006 p->rfd_last = p->rfd_top;
1007 p->rfd_top = make32(readw(&p->rfd_top->next));
1008 writew(make16(p->rfd_top), &p->scb->rfa_offset);
1009
1010 if (debuglevel > 0)
1011 printk("%d", cnt++);
1012 }
1013
1014 if (automatic_resume) {
1015 wait_for_scb_cmd(dev);
1016 writeb(RUC_RESUME, &p->scb->cmd_ruc);
1017 ni_attn586();
1018 wait_for_scb_cmd_ruc(dev);
1019 }
1020
1021#ifdef WAIT_4_BUSY
1022 {
1023 int i;
1024 for (i = 0; i < 1024; i++) {
1025 if (p->rfd_top->status)
1026 break;
1027 udelay(16);
1028 if (i == 1023)
1029 printk(KERN_ERR "%s: RU hasn't fetched next RFD (not busy/complete)\n", dev->name);
1030 }
1031 }
1032#endif
1033 if (debuglevel > 0)
1034 printk("r");
1035}
1036
1037
1038
1039
1040
1041static void ni52_rnr_int(struct net_device *dev)
1042{
1043 struct priv *p = netdev_priv(dev);
1044
1045 dev->stats.rx_errors++;
1046
1047 wait_for_scb_cmd(dev);
1048 writeb(RUC_ABORT, &p->scb->cmd_ruc);
1049 ni_attn586();
1050 wait_for_scb_cmd_ruc(dev);
1051
1052 alloc_rfa(dev, p->rfd_first);
1053
1054 startrecv586(dev);
1055
1056 printk(KERN_ERR "%s: Receive-Unit restarted. Status: %04x\n",
1057 dev->name, readb(&p->scb->rus));
1058
1059}
1060
1061
1062
1063
1064
1065static void ni52_xmt_int(struct net_device *dev)
1066{
1067 int status;
1068 struct priv *p = netdev_priv(dev);
1069
1070 if (debuglevel > 0)
1071 printk("X");
1072
1073 status = readw(&p->xmit_cmds[p->xmit_last]->cmd_status);
1074 if (!(status & STAT_COMPL))
1075 printk(KERN_ERR "%s: strange .. xmit-int without a 'COMPLETE'\n", dev->name);
1076
1077 if (status & STAT_OK) {
1078 dev->stats.tx_packets++;
1079 dev->stats.collisions += (status & TCMD_MAXCOLLMASK);
1080 } else {
1081 dev->stats.tx_errors++;
1082 if (status & TCMD_LATECOLL) {
1083 printk(KERN_ERR "%s: late collision detected.\n",
1084 dev->name);
1085 dev->stats.collisions++;
1086 } else if (status & TCMD_NOCARRIER) {
1087 dev->stats.tx_carrier_errors++;
1088 printk(KERN_ERR "%s: no carrier detected.\n",
1089 dev->name);
1090 } else if (status & TCMD_LOSTCTS)
1091 printk(KERN_ERR "%s: loss of CTS detected.\n",
1092 dev->name);
1093 else if (status & TCMD_UNDERRUN) {
1094 dev->stats.tx_fifo_errors++;
1095 printk(KERN_ERR "%s: DMA underrun detected.\n",
1096 dev->name);
1097 } else if (status & TCMD_MAXCOLL) {
1098 printk(KERN_ERR "%s: Max. collisions exceeded.\n",
1099 dev->name);
1100 dev->stats.collisions += 16;
1101 }
1102 }
1103#if (NUM_XMIT_BUFFS > 1)
1104 if ((++p->xmit_last) == NUM_XMIT_BUFFS)
1105 p->xmit_last = 0;
1106#endif
1107 netif_wake_queue(dev);
1108}
1109
1110
1111
1112
1113
1114static void startrecv586(struct net_device *dev)
1115{
1116 struct priv *p = netdev_priv(dev);
1117
1118 wait_for_scb_cmd(dev);
1119 wait_for_scb_cmd_ruc(dev);
1120 writew(make16(p->rfd_first), &p->scb->rfa_offset);
1121 writeb(RUC_START, &p->scb->cmd_ruc);
1122 ni_attn586();
1123 wait_for_scb_cmd_ruc(dev);
1124
1125}
1126
1127static void ni52_timeout(struct net_device *dev)
1128{
1129 struct priv *p = netdev_priv(dev);
1130#ifndef NO_NOPCOMMANDS
1131 if (readb(&p->scb->cus) & CU_ACTIVE) {
1132 netif_wake_queue(dev);
1133#ifdef DEBUG
1134 printk(KERN_ERR "%s: strange ... timeout with CU active?!?\n",
1135 dev->name);
1136 printk(KERN_ERR "%s: X0: %04x N0: %04x N1: %04x %d\n",
1137 dev->name, (int)p->xmit_cmds[0]->cmd_status,
1138 readw(&p->nop_cmds[0]->cmd_status),
1139 readw(&p->nop_cmds[1]->cmd_status),
1140 p->nop_point);
1141#endif
1142 writeb(CUC_ABORT, &p->scb->cmd_cuc);
1143 ni_attn586();
1144 wait_for_scb_cmd(dev);
1145 writew(make16(p->nop_cmds[p->nop_point]), &p->scb->cbl_offset);
1146 writeb(CUC_START, &p->scb->cmd_cuc);
1147 ni_attn586();
1148 wait_for_scb_cmd(dev);
1149 dev->trans_start = jiffies;
1150 return 0;
1151 }
1152#endif
1153 {
1154#ifdef DEBUG
1155 printk(KERN_ERR "%s: xmitter timed out, try to restart! stat: %02x\n",
1156 dev->name, readb(&p->scb->cus));
1157 printk(KERN_ERR "%s: command-stats: %04x %04x\n",
1158 dev->name,
1159 readw(&p->xmit_cmds[0]->cmd_status),
1160 readw(&p->xmit_cmds[1]->cmd_status));
1161 printk(KERN_ERR "%s: check, whether you set the right interrupt number!\n",
1162 dev->name);
1163#endif
1164 ni52_close(dev);
1165 ni52_open(dev);
1166 }
1167 dev->trans_start = jiffies;
1168}
1169
1170
1171
1172
1173
1174static netdev_tx_t ni52_send_packet(struct sk_buff *skb,
1175 struct net_device *dev)
1176{
1177 int len, i;
1178#ifndef NO_NOPCOMMANDS
1179 int next_nop;
1180#endif
1181 struct priv *p = netdev_priv(dev);
1182
1183 if (skb->len > XMIT_BUFF_SIZE) {
1184 printk(KERN_ERR "%s: Sorry, max. framelength is %d bytes. The length of your frame is %d bytes.\n", dev->name, XMIT_BUFF_SIZE, skb->len);
1185 return NETDEV_TX_OK;
1186 }
1187
1188 netif_stop_queue(dev);
1189
1190 memcpy_toio(p->xmit_cbuffs[p->xmit_count], skb->data, skb->len);
1191 len = skb->len;
1192 if (len < ETH_ZLEN) {
1193 len = ETH_ZLEN;
1194 memset_io(p->xmit_cbuffs[p->xmit_count]+skb->len, 0,
1195 len - skb->len);
1196 }
1197
1198#if (NUM_XMIT_BUFFS == 1)
1199# ifdef NO_NOPCOMMANDS
1200
1201#ifdef DEBUG
1202 if (readb(&p->scb->cus) & CU_ACTIVE) {
1203 printk(KERN_ERR "%s: Hmmm .. CU is still running and we wanna send a new packet.\n", dev->name);
1204 printk(KERN_ERR "%s: stat: %04x %04x\n",
1205 dev->name, readb(&p->scb->cus),
1206 readw(&p->xmit_cmds[0]->cmd_status));
1207 }
1208#endif
1209 writew(TBD_LAST | len, &p->xmit_buffs[0]->size);
1210 for (i = 0; i < 16; i++) {
1211 writew(0, &p->xmit_cmds[0]->cmd_status);
1212 wait_for_scb_cmd(dev);
1213 if ((readb(&p->scb->cus) & CU_STATUS) == CU_SUSPEND)
1214 writeb(CUC_RESUME, &p->scb->cmd_cuc);
1215 else {
1216 writew(make16(p->xmit_cmds[0]), &p->scb->cbl_offset);
1217 writeb(CUC_START, &p->scb->cmd_cuc);
1218 }
1219 ni_attn586();
1220 if (!i)
1221 dev_kfree_skb(skb);
1222 wait_for_scb_cmd(dev);
1223
1224 if (readb(&p->scb->cus) & CU_ACTIVE)
1225 break;
1226 if (readw(&p->xmit_cmds[0]->cmd_status))
1227 break;
1228 if (i == 15)
1229 printk(KERN_WARNING "%s: Can't start transmit-command.\n", dev->name);
1230 }
1231# else
1232 next_nop = (p->nop_point + 1) & 0x1;
1233 writew(TBD_LAST | len, &p->xmit_buffs[0]->size);
1234 writew(make16(p->nop_cmds[next_nop]), &p->xmit_cmds[0]->cmd_link);
1235 writew(make16(p->nop_cmds[next_nop]),
1236 &p->nop_cmds[next_nop]->cmd_link);
1237 writew(0, &p->xmit_cmds[0]->cmd_status);
1238 writew(0, &p->nop_cmds[next_nop]->cmd_status);
1239
1240 writew(make16(p->xmit_cmds[0]), &p->nop_cmds[p->nop_point]->cmd_link);
1241 p->nop_point = next_nop;
1242 dev_kfree_skb(skb);
1243# endif
1244#else
1245 writew(TBD_LAST | len, &p->xmit_buffs[p->xmit_count]->size);
1246 next_nop = p->xmit_count + 1
1247 if (next_nop == NUM_XMIT_BUFFS)
1248 next_nop = 0;
1249 writew(0, &p->xmit_cmds[p->xmit_count]->cmd_status);
1250
1251 writew(make16(p->nop_cmds[next_nop]),
1252 &p->nop_cmds[next_nop]->cmd_link);
1253 writew(0, &p->nop_cmds[next_nop]->cmd_status);
1254 writew(make16(p->xmit_cmds[p->xmit_count]),
1255 &p->nop_cmds[p->xmit_count]->cmd_link);
1256 p->xmit_count = next_nop;
1257 {
1258 unsigned long flags;
1259 spin_lock_irqsave(&p->spinlock);
1260 if (p->xmit_count != p->xmit_last)
1261 netif_wake_queue(dev);
1262 spin_unlock_irqrestore(&p->spinlock);
1263 }
1264 dev_kfree_skb(skb);
1265#endif
1266 return NETDEV_TX_OK;
1267}
1268
1269
1270
1271
1272
1273static struct net_device_stats *ni52_get_stats(struct net_device *dev)
1274{
1275 struct priv *p = netdev_priv(dev);
1276 unsigned short crc, aln, rsc, ovrn;
1277
1278
1279 crc = readw(&p->scb->crc_errs);
1280 writew(0, &p->scb->crc_errs);
1281 aln = readw(&p->scb->aln_errs);
1282 writew(0, &p->scb->aln_errs);
1283 rsc = readw(&p->scb->rsc_errs);
1284 writew(0, &p->scb->rsc_errs);
1285 ovrn = readw(&p->scb->ovrn_errs);
1286 writew(0, &p->scb->ovrn_errs);
1287
1288 dev->stats.rx_crc_errors += crc;
1289 dev->stats.rx_fifo_errors += ovrn;
1290 dev->stats.rx_frame_errors += aln;
1291 dev->stats.rx_dropped += rsc;
1292
1293 return &dev->stats;
1294}
1295
1296
1297
1298
1299
1300static void set_multicast_list(struct net_device *dev)
1301{
1302 netif_stop_queue(dev);
1303 ni_disint();
1304 alloc586(dev);
1305 init586(dev);
1306 startrecv586(dev);
1307 ni_enaint();
1308 netif_wake_queue(dev);
1309}
1310
1311#ifdef MODULE
1312static struct net_device *dev_ni52;
1313
1314module_param(io, int, 0);
1315module_param(irq, int, 0);
1316module_param(memstart, long, 0);
1317module_param(memend, long, 0);
1318MODULE_PARM_DESC(io, "NI5210 I/O base address,required");
1319MODULE_PARM_DESC(irq, "NI5210 IRQ number,required");
1320MODULE_PARM_DESC(memstart, "NI5210 memory base address,required");
1321MODULE_PARM_DESC(memend, "NI5210 memory end address,required");
1322
1323int __init init_module(void)
1324{
1325 if (io <= 0x0 || !memend || !memstart || irq < 2) {
1326 printk(KERN_ERR "ni52: Autoprobing not allowed for modules.\n");
1327 printk(KERN_ERR "ni52: Set symbols 'io' 'irq' 'memstart' and 'memend'\n");
1328 return -ENODEV;
1329 }
1330 dev_ni52 = ni52_probe(-1);
1331 if (IS_ERR(dev_ni52))
1332 return PTR_ERR(dev_ni52);
1333 return 0;
1334}
1335
1336void __exit cleanup_module(void)
1337{
1338 struct priv *p = netdev_priv(dev_ni52);
1339 unregister_netdev(dev_ni52);
1340 iounmap(p->mapped);
1341 release_region(dev_ni52->base_addr, NI52_TOTAL_SIZE);
1342 free_netdev(dev_ni52);
1343}
1344#endif
1345
1346MODULE_LICENSE("GPL");
1347