1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#define DRV_NAME "pcnet32"
25#define DRV_VERSION "1.27b"
26#define DRV_RELDATE "01.10.2002"
27#define PFX DRV_NAME ": "
28
29static const char *version =
30DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " tsbogend@alpha.franken.de\n";
31
32#include <linux/module.h>
33#include <linux/kernel.h>
34#include <linux/string.h>
35#include <linux/errno.h>
36#include <linux/ioport.h>
37#include <linux/slab.h>
38#include <linux/interrupt.h>
39#include <linux/pci.h>
40#include <linux/delay.h>
41#include <linux/init.h>
42#include <linux/ethtool.h>
43#include <linux/mii.h>
44#include <linux/crc32.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/skbuff.h>
48#include <linux/spinlock.h>
49
50#include <asm/bitops.h>
51#include <asm/dma.h>
52#include <asm/io.h>
53#include <asm/uaccess.h>
54
55
56
57
58static struct pci_device_id pcnet32_pci_tbl[] = {
59 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE_HOME, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
60 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
61 { 0, }
62};
63
64MODULE_DEVICE_TABLE (pci, pcnet32_pci_tbl);
65
66int cards_found __initdata;
67
68
69
70
71static unsigned int pcnet32_portlist[] __initdata =
72 { 0x300, 0x320, 0x340, 0x360, 0 };
73
74
75
76static int pcnet32_debug = 1;
77static int tx_start = 1;
78static int pcnet32vlb;
79
80static struct net_device *pcnet32_dev;
81
82static int max_interrupt_work = 80;
83static int rx_copybreak = 200;
84
85#define PCNET32_PORT_AUI 0x00
86#define PCNET32_PORT_10BT 0x01
87#define PCNET32_PORT_GPSI 0x02
88#define PCNET32_PORT_MII 0x03
89
90#define PCNET32_PORT_PORTSEL 0x03
91#define PCNET32_PORT_ASEL 0x04
92#define PCNET32_PORT_100 0x40
93#define PCNET32_PORT_FD 0x80
94
95#define PCNET32_DMA_MASK 0xffffffff
96
97#define PCNET32_WATCHDOG_TIMEOUT (jiffies + (2 * HZ))
98
99
100
101
102
103static unsigned char options_mapping[] = {
104 PCNET32_PORT_ASEL,
105 PCNET32_PORT_AUI,
106 PCNET32_PORT_AUI,
107 PCNET32_PORT_ASEL,
108 PCNET32_PORT_10BT | PCNET32_PORT_FD,
109 PCNET32_PORT_ASEL,
110 PCNET32_PORT_ASEL,
111 PCNET32_PORT_ASEL,
112 PCNET32_PORT_ASEL,
113 PCNET32_PORT_MII,
114 PCNET32_PORT_MII | PCNET32_PORT_FD,
115 PCNET32_PORT_MII,
116 PCNET32_PORT_10BT,
117 PCNET32_PORT_MII | PCNET32_PORT_100,
118 PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD,
119 PCNET32_PORT_ASEL
120};
121
122#define MAX_UNITS 8
123static int options[MAX_UNITS];
124static int full_duplex[MAX_UNITS];
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224#ifndef PCNET32_LOG_TX_BUFFERS
225#define PCNET32_LOG_TX_BUFFERS 4
226#define PCNET32_LOG_RX_BUFFERS 5
227#endif
228
229#define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
230#define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
231#define TX_RING_LEN_BITS ((PCNET32_LOG_TX_BUFFERS) << 12)
232
233#define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
234#define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
235#define RX_RING_LEN_BITS ((PCNET32_LOG_RX_BUFFERS) << 4)
236
237#define PKT_BUF_SZ 1544
238
239
240#define PCNET32_WIO_RDP 0x10
241#define PCNET32_WIO_RAP 0x12
242#define PCNET32_WIO_RESET 0x14
243#define PCNET32_WIO_BDP 0x16
244
245#define PCNET32_DWIO_RDP 0x10
246#define PCNET32_DWIO_RAP 0x14
247#define PCNET32_DWIO_RESET 0x18
248#define PCNET32_DWIO_BDP 0x1C
249
250#define PCNET32_TOTAL_SIZE 0x20
251
252
253struct pcnet32_rx_head {
254 u32 base;
255 s16 buf_length;
256 s16 status;
257 u32 msg_length;
258 u32 reserved;
259};
260
261struct pcnet32_tx_head {
262 u32 base;
263 s16 length;
264 s16 status;
265 u32 misc;
266 u32 reserved;
267};
268
269
270struct pcnet32_init_block {
271 u16 mode;
272 u16 tlen_rlen;
273 u8 phys_addr[6];
274 u16 reserved;
275 u32 filter[2];
276
277 u32 rx_ring;
278 u32 tx_ring;
279};
280
281
282struct pcnet32_access {
283 u16 (*read_csr)(unsigned long, int);
284 void (*write_csr)(unsigned long, int, u16);
285 u16 (*read_bcr)(unsigned long, int);
286 void (*write_bcr)(unsigned long, int, u16);
287 u16 (*read_rap)(unsigned long);
288 void (*write_rap)(unsigned long, u16);
289 void (*reset)(unsigned long);
290};
291
292
293
294
295
296struct pcnet32_private {
297
298 struct pcnet32_rx_head rx_ring[RX_RING_SIZE];
299 struct pcnet32_tx_head tx_ring[TX_RING_SIZE];
300 struct pcnet32_init_block init_block;
301 dma_addr_t dma_addr;
302
303 struct pci_dev *pci_dev;
304 const char *name;
305
306 struct sk_buff *tx_skbuff[TX_RING_SIZE];
307 struct sk_buff *rx_skbuff[RX_RING_SIZE];
308 dma_addr_t tx_dma_addr[TX_RING_SIZE];
309 dma_addr_t rx_dma_addr[RX_RING_SIZE];
310 struct pcnet32_access a;
311 spinlock_t lock;
312 unsigned int cur_rx, cur_tx;
313 unsigned int dirty_rx, dirty_tx;
314 struct net_device_stats stats;
315 char tx_full;
316 int options;
317 int shared_irq:1,
318 ltint:1,
319 dxsuflo:1,
320 mii:1;
321 struct net_device *next;
322 struct mii_if_info mii_if;
323 struct timer_list watchdog_timer;
324};
325
326static void pcnet32_probe_vlbus(void);
327static int pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
328static int pcnet32_probe1(unsigned long, unsigned int, int, struct pci_dev *);
329static int pcnet32_open(struct net_device *);
330static int pcnet32_init_ring(struct net_device *);
331static int pcnet32_start_xmit(struct sk_buff *, struct net_device *);
332static int pcnet32_rx(struct net_device *);
333static void pcnet32_tx_timeout (struct net_device *dev);
334static irqreturn_t pcnet32_interrupt(int, void *, struct pt_regs *);
335static int pcnet32_close(struct net_device *);
336static struct net_device_stats *pcnet32_get_stats(struct net_device *);
337static void pcnet32_set_multicast_list(struct net_device *);
338static int pcnet32_ioctl(struct net_device *, struct ifreq *, int);
339static void pcnet32_watchdog(struct net_device *);
340static int mdio_read(struct net_device *dev, int phy_id, int reg_num);
341static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val);
342
343enum pci_flags_bit {
344 PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
345 PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
346};
347
348
349static u16 pcnet32_wio_read_csr (unsigned long addr, int index)
350{
351 outw (index, addr+PCNET32_WIO_RAP);
352 return inw (addr+PCNET32_WIO_RDP);
353}
354
355static void pcnet32_wio_write_csr (unsigned long addr, int index, u16 val)
356{
357 outw (index, addr+PCNET32_WIO_RAP);
358 outw (val, addr+PCNET32_WIO_RDP);
359}
360
361static u16 pcnet32_wio_read_bcr (unsigned long addr, int index)
362{
363 outw (index, addr+PCNET32_WIO_RAP);
364 return inw (addr+PCNET32_WIO_BDP);
365}
366
367static void pcnet32_wio_write_bcr (unsigned long addr, int index, u16 val)
368{
369 outw (index, addr+PCNET32_WIO_RAP);
370 outw (val, addr+PCNET32_WIO_BDP);
371}
372
373static u16 pcnet32_wio_read_rap (unsigned long addr)
374{
375 return inw (addr+PCNET32_WIO_RAP);
376}
377
378static void pcnet32_wio_write_rap (unsigned long addr, u16 val)
379{
380 outw (val, addr+PCNET32_WIO_RAP);
381}
382
383static void pcnet32_wio_reset (unsigned long addr)
384{
385 inw (addr+PCNET32_WIO_RESET);
386}
387
388static int pcnet32_wio_check (unsigned long addr)
389{
390 outw (88, addr+PCNET32_WIO_RAP);
391 return (inw (addr+PCNET32_WIO_RAP) == 88);
392}
393
394static struct pcnet32_access pcnet32_wio = {
395 .read_csr = pcnet32_wio_read_csr,
396 .write_csr = pcnet32_wio_write_csr,
397 .read_bcr = pcnet32_wio_read_bcr,
398 .write_bcr = pcnet32_wio_write_bcr,
399 .read_rap = pcnet32_wio_read_rap,
400 .write_rap = pcnet32_wio_write_rap,
401 .reset = pcnet32_wio_reset
402};
403
404static u16 pcnet32_dwio_read_csr (unsigned long addr, int index)
405{
406 outl (index, addr+PCNET32_DWIO_RAP);
407 return (inl (addr+PCNET32_DWIO_RDP) & 0xffff);
408}
409
410static void pcnet32_dwio_write_csr (unsigned long addr, int index, u16 val)
411{
412 outl (index, addr+PCNET32_DWIO_RAP);
413 outl (val, addr+PCNET32_DWIO_RDP);
414}
415
416static u16 pcnet32_dwio_read_bcr (unsigned long addr, int index)
417{
418 outl (index, addr+PCNET32_DWIO_RAP);
419 return (inl (addr+PCNET32_DWIO_BDP) & 0xffff);
420}
421
422static void pcnet32_dwio_write_bcr (unsigned long addr, int index, u16 val)
423{
424 outl (index, addr+PCNET32_DWIO_RAP);
425 outl (val, addr+PCNET32_DWIO_BDP);
426}
427
428static u16 pcnet32_dwio_read_rap (unsigned long addr)
429{
430 return (inl (addr+PCNET32_DWIO_RAP) & 0xffff);
431}
432
433static void pcnet32_dwio_write_rap (unsigned long addr, u16 val)
434{
435 outl (val, addr+PCNET32_DWIO_RAP);
436}
437
438static void pcnet32_dwio_reset (unsigned long addr)
439{
440 inl (addr+PCNET32_DWIO_RESET);
441}
442
443static int pcnet32_dwio_check (unsigned long addr)
444{
445 outl (88, addr+PCNET32_DWIO_RAP);
446 return ((inl (addr+PCNET32_DWIO_RAP) & 0xffff) == 88);
447}
448
449static struct pcnet32_access pcnet32_dwio = {
450 .read_csr = pcnet32_dwio_read_csr,
451 .write_csr = pcnet32_dwio_write_csr,
452 .read_bcr = pcnet32_dwio_read_bcr,
453 .write_bcr = pcnet32_dwio_write_bcr,
454 .read_rap = pcnet32_dwio_read_rap,
455 .write_rap = pcnet32_dwio_write_rap,
456 .reset = pcnet32_dwio_reset
457};
458
459
460
461
462
463
464static void __devinit
465pcnet32_probe_vlbus(void)
466{
467 unsigned int *port, ioaddr;
468
469
470 for (port = pcnet32_portlist; (ioaddr = *port); port++) {
471 if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_vlbus")) {
472
473 if ((inb(ioaddr + 14) == 0x57) && (inb(ioaddr + 15) == 0x57)) {
474 pcnet32_probe1(ioaddr, 0, 0, NULL);
475 } else {
476 release_region(ioaddr, PCNET32_TOTAL_SIZE);
477 }
478 }
479 }
480}
481
482
483static int __devinit
484pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
485{
486 unsigned long ioaddr;
487 int err;
488
489 err = pci_enable_device(pdev);
490 if (err < 0) {
491 printk(KERN_ERR PFX "failed to enable device -- err=%d\n", err);
492 return err;
493 }
494 pci_set_master(pdev);
495
496 ioaddr = pci_resource_start (pdev, 0);
497 if (!ioaddr) {
498 printk (KERN_ERR PFX "card has no PCI IO resources, aborting\n");
499 return -ENODEV;
500 }
501
502 if (!pci_dma_supported(pdev, PCNET32_DMA_MASK)) {
503 printk(KERN_ERR PFX "architecture does not support 32bit PCI busmaster DMA\n");
504 return -ENODEV;
505 }
506 if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_pci") == NULL) {
507 printk(KERN_ERR PFX "io address range already allocated\n");
508 return -EBUSY;
509 }
510
511 return pcnet32_probe1(ioaddr, pdev->irq, 1, pdev);
512}
513
514
515
516
517
518
519static int __devinit
520pcnet32_probe1(unsigned long ioaddr, unsigned int irq_line, int shared,
521 struct pci_dev *pdev)
522{
523 struct pcnet32_private *lp;
524 dma_addr_t lp_dma_addr;
525 int i, media;
526 int fdx, mii, fset, dxsuflo, ltint;
527 int chip_version;
528 char *chipname;
529 struct net_device *dev;
530 struct pcnet32_access *a = NULL;
531 u8 promaddr[6];
532 int ret = -ENODEV;
533
534
535 pcnet32_wio_reset(ioaddr);
536
537
538 if (pcnet32_wio_read_csr(ioaddr, 0) == 4 && pcnet32_wio_check(ioaddr)) {
539 a = &pcnet32_wio;
540 } else {
541 pcnet32_dwio_reset(ioaddr);
542 if (pcnet32_dwio_read_csr(ioaddr, 0) == 4 && pcnet32_dwio_check(ioaddr)) {
543 a = &pcnet32_dwio;
544 } else
545 goto err_release_region;
546 }
547
548 chip_version = a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr,89) << 16);
549 if (pcnet32_debug > 2)
550 printk(KERN_INFO " PCnet chip version is %#x.\n", chip_version);
551 if ((chip_version & 0xfff) != 0x003)
552 goto err_release_region;
553
554
555 fdx = mii = fset = dxsuflo = ltint = 0;
556 chip_version = (chip_version >> 12) & 0xffff;
557
558 switch (chip_version) {
559 case 0x2420:
560 chipname = "PCnet/PCI 79C970";
561 break;
562 case 0x2430:
563 if (shared)
564 chipname = "PCnet/PCI 79C970";
565 else
566 chipname = "PCnet/32 79C965";
567 break;
568 case 0x2621:
569 chipname = "PCnet/PCI II 79C970A";
570 fdx = 1;
571 break;
572 case 0x2623:
573 chipname = "PCnet/FAST 79C971";
574 fdx = 1; mii = 1; fset = 1;
575 ltint = 1;
576 break;
577 case 0x2624:
578 chipname = "PCnet/FAST+ 79C972";
579 fdx = 1; mii = 1; fset = 1;
580 break;
581 case 0x2625:
582 chipname = "PCnet/FAST III 79C973";
583 fdx = 1; mii = 1;
584 break;
585 case 0x2626:
586 chipname = "PCnet/Home 79C978";
587 fdx = 1;
588
589
590
591
592
593
594
595
596 media = a->read_bcr(ioaddr, 49);
597#if 0
598 if (pcnet32_debug > 2)
599 printk(KERN_DEBUG PFX "media value %#x.\n", media);
600 media &= ~3;
601 media |= 1;
602#endif
603 if (pcnet32_debug > 2)
604 printk(KERN_DEBUG PFX "media reset to %#x.\n", media);
605 a->write_bcr(ioaddr, 49, media);
606 break;
607 case 0x2627:
608 chipname = "PCnet/FAST III 79C975";
609 fdx = 1; mii = 1;
610 break;
611 default:
612 printk(KERN_INFO PFX "PCnet version %#x, no PCnet32 chip.\n",
613 chip_version);
614 goto err_release_region;
615 }
616
617
618
619
620
621
622
623
624 if(fset)
625 {
626 a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0800));
627 a->write_csr(ioaddr, 80, (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
628 dxsuflo = 1;
629 ltint = 1;
630 }
631
632 dev = alloc_etherdev(0);
633 if(!dev) {
634 ret = -ENOMEM;
635 goto err_release_region;
636 }
637 SET_NETDEV_DEV(dev, &pdev->dev);
638
639 printk(KERN_INFO PFX "%s at %#3lx,", chipname, ioaddr);
640
641
642
643
644
645
646
647
648 for (i = 0; i < 3; i++) {
649 unsigned int val;
650 val = a->read_csr(ioaddr, i+12) & 0x0ffff;
651
652 dev->dev_addr[2*i] = val & 0x0ff;
653 dev->dev_addr[2*i+1] = (val >> 8) & 0x0ff;
654 }
655
656
657 for (i = 0; i < 6; i++)
658 promaddr[i] = inb(ioaddr + i);
659
660 if( memcmp( promaddr, dev->dev_addr, 6)
661 || !is_valid_ether_addr(dev->dev_addr) ) {
662#ifndef __powerpc__
663 if( is_valid_ether_addr(promaddr) ){
664#else
665 if( !is_valid_ether_addr(dev->dev_addr)
666 && is_valid_ether_addr(promaddr)) {
667#endif
668 printk(" warning: CSR address invalid,\n");
669 printk(KERN_INFO " using instead PROM address of");
670 memcpy(dev->dev_addr, promaddr, 6);
671 }
672 }
673
674
675 if( !is_valid_ether_addr(dev->dev_addr) )
676 memset(dev->dev_addr, 0, sizeof(dev->dev_addr));
677
678 for (i = 0; i < 6; i++)
679 printk(" %2.2x", dev->dev_addr[i] );
680
681 if (((chip_version + 1) & 0xfffe) == 0x2624) {
682 i = a->read_csr(ioaddr, 80) & 0x0C00;
683 printk("\n" KERN_INFO " tx_start_pt(0x%04x):",i);
684 switch(i>>10) {
685 case 0: printk(" 20 bytes,"); break;
686 case 1: printk(" 64 bytes,"); break;
687 case 2: printk(" 128 bytes,"); break;
688 case 3: printk("~220 bytes,"); break;
689 }
690 i = a->read_bcr(ioaddr, 18);
691 printk(" BCR18(%x):",i&0xffff);
692 if (i & (1<<5)) printk("BurstWrEn ");
693 if (i & (1<<6)) printk("BurstRdEn ");
694 if (i & (1<<7)) printk("DWordIO ");
695 if (i & (1<<11)) printk("NoUFlow ");
696 i = a->read_bcr(ioaddr, 25);
697 printk("\n" KERN_INFO " SRAMSIZE=0x%04x,",i<<8);
698 i = a->read_bcr(ioaddr, 26);
699 printk(" SRAM_BND=0x%04x,",i<<8);
700 i = a->read_bcr(ioaddr, 27);
701 if (i & (1<<14)) printk("LowLatRx");
702 }
703
704 dev->base_addr = ioaddr;
705
706 if ((lp = pci_alloc_consistent(pdev, sizeof(*lp), &lp_dma_addr)) == NULL) {
707 ret = -ENOMEM;
708 goto err_free_netdev;
709 }
710
711 memset(lp, 0, sizeof(*lp));
712 lp->dma_addr = lp_dma_addr;
713 lp->pci_dev = pdev;
714
715 spin_lock_init(&lp->lock);
716
717 SET_MODULE_OWNER(dev);
718 SET_NETDEV_DEV(dev, &pdev->dev);
719 dev->priv = lp;
720 lp->name = chipname;
721 lp->shared_irq = shared;
722 lp->mii_if.full_duplex = fdx;
723 lp->dxsuflo = dxsuflo;
724 lp->ltint = ltint;
725 lp->mii = mii;
726 if ((cards_found >= MAX_UNITS) || (options[cards_found] > sizeof(options_mapping)))
727 lp->options = PCNET32_PORT_ASEL;
728 else
729 lp->options = options_mapping[options[cards_found]];
730 lp->mii_if.dev = dev;
731 lp->mii_if.mdio_read = mdio_read;
732 lp->mii_if.mdio_write = mdio_write;
733
734 if (fdx && !(lp->options & PCNET32_PORT_ASEL) &&
735 ((cards_found>=MAX_UNITS) || full_duplex[cards_found]))
736 lp->options |= PCNET32_PORT_FD;
737
738 if (!a) {
739 printk(KERN_ERR PFX "No access methods\n");
740 ret = -ENODEV;
741 goto err_free_consistent;
742 }
743 lp->a = *a;
744
745
746 if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0 && dev->dev_addr[2] == 0x75)
747 lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
748
749 lp->init_block.mode = le16_to_cpu(0x0003);
750 lp->init_block.tlen_rlen = le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
751 for (i = 0; i < 6; i++)
752 lp->init_block.phys_addr[i] = dev->dev_addr[i];
753 lp->init_block.filter[0] = 0x00000000;
754 lp->init_block.filter[1] = 0x00000000;
755 lp->init_block.rx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, rx_ring));
756 lp->init_block.tx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, tx_ring));
757
758
759 a->write_bcr (ioaddr, 20, 2);
760
761 a->write_csr (ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) & 0xffff);
762 a->write_csr (ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) >> 16);
763
764 if (irq_line) {
765 dev->irq = irq_line;
766 }
767
768 if (dev->irq >= 2)
769 printk(" assigned IRQ %d.\n", dev->irq);
770 else {
771 unsigned long irq_mask = probe_irq_on();
772
773
774
775
776
777
778
779 a->write_csr (ioaddr, 0, 0x41);
780 mdelay (1);
781
782 dev->irq = probe_irq_off (irq_mask);
783 if (!dev->irq) {
784 printk(", failed to detect IRQ line.\n");
785 ret = -ENODEV;
786 goto err_free_consistent;
787 }
788 printk(", probed IRQ %d.\n", dev->irq);
789 }
790
791
792 if (lp->mii)
793 lp->mii_if.phy_id = ((lp->a.read_bcr (ioaddr, 33)) >> 5) & 0x1f;
794
795 init_timer (&lp->watchdog_timer);
796 lp->watchdog_timer.data = (unsigned long) dev;
797 lp->watchdog_timer.function = (void *) &pcnet32_watchdog;
798
799
800 dev->open = &pcnet32_open;
801 dev->hard_start_xmit = &pcnet32_start_xmit;
802 dev->stop = &pcnet32_close;
803 dev->get_stats = &pcnet32_get_stats;
804 dev->set_multicast_list = &pcnet32_set_multicast_list;
805 dev->do_ioctl = &pcnet32_ioctl;
806 dev->tx_timeout = pcnet32_tx_timeout;
807 dev->watchdog_timeo = (5*HZ);
808
809 lp->next = pcnet32_dev;
810 pcnet32_dev = dev;
811
812
813 register_netdev(dev);
814 printk(KERN_INFO "%s: registered as %s\n",dev->name, lp->name);
815 cards_found++;
816 return 0;
817
818err_free_consistent:
819 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
820err_free_netdev:
821 free_netdev(dev);
822err_release_region:
823 release_region(ioaddr, PCNET32_TOTAL_SIZE);
824 return ret;
825}
826
827
828static int
829pcnet32_open(struct net_device *dev)
830{
831 struct pcnet32_private *lp = dev->priv;
832 unsigned long ioaddr = dev->base_addr;
833 u16 val;
834 int i;
835
836 if (dev->irq == 0 ||
837 request_irq(dev->irq, &pcnet32_interrupt,
838 lp->shared_irq ? SA_SHIRQ : 0, lp->name, (void *)dev)) {
839 return -EAGAIN;
840 }
841
842
843 if( !is_valid_ether_addr(dev->dev_addr) )
844 return -EINVAL;
845
846
847 lp->a.reset (ioaddr);
848
849
850 lp->a.write_bcr (ioaddr, 20, 2);
851
852 if (pcnet32_debug > 1)
853 printk(KERN_DEBUG "%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
854 dev->name, dev->irq,
855 (u32) (lp->dma_addr + offsetof(struct pcnet32_private, tx_ring)),
856 (u32) (lp->dma_addr + offsetof(struct pcnet32_private, rx_ring)),
857 (u32) (lp->dma_addr + offsetof(struct pcnet32_private, init_block)));
858
859
860 val = lp->a.read_bcr (ioaddr, 2) & ~2;
861 if (lp->options & PCNET32_PORT_ASEL)
862 val |= 2;
863 lp->a.write_bcr (ioaddr, 2, val);
864
865
866 if (lp->mii_if.full_duplex) {
867 val = lp->a.read_bcr (ioaddr, 9) & ~3;
868 if (lp->options & PCNET32_PORT_FD) {
869 val |= 1;
870 if (lp->options == (PCNET32_PORT_FD | PCNET32_PORT_AUI))
871 val |= 2;
872 } else if (lp->options & PCNET32_PORT_ASEL) {
873
874 i = ((lp->a.read_csr(ioaddr, 88) | (lp->a.read_csr(ioaddr,89) << 16)) >> 12) & 0xffff;
875 if (i == 0x2627) val |= 3;
876 }
877 lp->a.write_bcr (ioaddr, 9, val);
878 }
879
880
881 val = lp->a.read_csr (ioaddr, 124) & ~0x10;
882 if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
883 val |= 0x10;
884 lp->a.write_csr (ioaddr, 124, val);
885
886 if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
887 val = lp->a.read_bcr (ioaddr, 32) & ~0x38;
888 if (lp->options & PCNET32_PORT_FD)
889 val |= 0x10;
890 if (lp->options & PCNET32_PORT_100)
891 val |= 0x08;
892 lp->a.write_bcr (ioaddr, 32, val);
893 } else {
894 if (lp->options & PCNET32_PORT_ASEL) {
895 val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
896 val |= 0x20;
897 lp->a.write_bcr(ioaddr, 32, val);
898 }
899 }
900
901#ifdef DO_DXSUFLO
902 if (lp->dxsuflo) {
903 val = lp->a.read_csr (ioaddr, 3);
904 val |= 0x40;
905 lp->a.write_csr (ioaddr, 3, val);
906 }
907#endif
908
909 if (lp->ltint) {
910 val = lp->a.read_csr (ioaddr, 5);
911 val |= (1<<14);
912 lp->a.write_csr (ioaddr, 5, val);
913 }
914
915 lp->init_block.mode = le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
916 lp->init_block.filter[0] = 0x00000000;
917 lp->init_block.filter[1] = 0x00000000;
918 if (pcnet32_init_ring(dev))
919 return -ENOMEM;
920
921
922 lp->a.write_csr (ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) &0xffff);
923 lp->a.write_csr (ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) >> 16);
924
925 lp->a.write_csr (ioaddr, 4, 0x0915);
926 lp->a.write_csr (ioaddr, 0, 0x0001);
927
928 netif_start_queue(dev);
929
930
931 if (lp->mii) {
932 mii_check_media (&lp->mii_if, 1, 1);
933 mod_timer (&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
934 }
935
936 i = 0;
937 while (i++ < 100)
938 if (lp->a.read_csr (ioaddr, 0) & 0x0100)
939 break;
940
941
942
943
944 lp->a.write_csr (ioaddr, 0, 0x0042);
945
946 if (pcnet32_debug > 2)
947 printk(KERN_DEBUG "%s: pcnet32 open after %d ticks, init block %#x csr0 %4.4x.\n",
948 dev->name, i, (u32) (lp->dma_addr + offsetof(struct pcnet32_private, init_block)),
949 lp->a.read_csr(ioaddr, 0));
950
951
952 return 0;
953}
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968static void
969pcnet32_purge_tx_ring(struct net_device *dev)
970{
971 struct pcnet32_private *lp = dev->priv;
972 int i;
973
974 for (i = 0; i < TX_RING_SIZE; i++) {
975 if (lp->tx_skbuff[i]) {
976 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i], lp->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
977 dev_kfree_skb_any(lp->tx_skbuff[i]);
978 lp->tx_skbuff[i] = NULL;
979 lp->tx_dma_addr[i] = 0;
980 }
981 }
982}
983
984
985
986static int
987pcnet32_init_ring(struct net_device *dev)
988{
989 struct pcnet32_private *lp = dev->priv;
990 int i;
991
992 lp->tx_full = 0;
993 lp->cur_rx = lp->cur_tx = 0;
994 lp->dirty_rx = lp->dirty_tx = 0;
995
996 for (i = 0; i < RX_RING_SIZE; i++) {
997 struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
998 if (rx_skbuff == NULL) {
999 if (!(rx_skbuff = lp->rx_skbuff[i] = dev_alloc_skb (PKT_BUF_SZ))) {
1000
1001 printk(KERN_ERR "%s: pcnet32_init_ring dev_alloc_skb failed.\n",dev->name);
1002 return -1;
1003 }
1004 skb_reserve (rx_skbuff, 2);
1005 }
1006
1007 if (lp->rx_dma_addr[i] == 0)
1008 lp->rx_dma_addr[i] = pci_map_single(lp->pci_dev, rx_skbuff->tail, rx_skbuff->len, PCI_DMA_FROMDEVICE);
1009 lp->rx_ring[i].base = (u32)le32_to_cpu(lp->rx_dma_addr[i]);
1010 lp->rx_ring[i].buf_length = le16_to_cpu(-PKT_BUF_SZ);
1011 lp->rx_ring[i].status = le16_to_cpu(0x8000);
1012 }
1013
1014
1015 for (i = 0; i < TX_RING_SIZE; i++) {
1016 lp->tx_ring[i].base = 0;
1017 lp->tx_ring[i].status = 0;
1018 lp->tx_dma_addr[i] = 0;
1019 }
1020
1021 lp->init_block.tlen_rlen = le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
1022 for (i = 0; i < 6; i++)
1023 lp->init_block.phys_addr[i] = dev->dev_addr[i];
1024 lp->init_block.rx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, rx_ring));
1025 lp->init_block.tx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, tx_ring));
1026 return 0;
1027}
1028
1029static void
1030pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
1031{
1032 struct pcnet32_private *lp = dev->priv;
1033 unsigned long ioaddr = dev->base_addr;
1034 int i;
1035
1036 pcnet32_purge_tx_ring(dev);
1037 if (pcnet32_init_ring(dev))
1038 return;
1039
1040
1041 lp->a.write_csr (ioaddr, 0, 1);
1042 i = 0;
1043 while (i++ < 1000)
1044 if (lp->a.read_csr (ioaddr, 0) & 0x0100)
1045 break;
1046
1047 lp->a.write_csr (ioaddr, 0, csr0_bits);
1048}
1049
1050
1051static void
1052pcnet32_tx_timeout (struct net_device *dev)
1053{
1054 struct pcnet32_private *lp = dev->priv;
1055 unsigned long ioaddr = dev->base_addr, flags;
1056
1057 spin_lock_irqsave(&lp->lock, flags);
1058
1059 printk(KERN_ERR "%s: transmit timed out, status %4.4x, resetting.\n",
1060 dev->name, lp->a.read_csr(ioaddr, 0));
1061 lp->a.write_csr (ioaddr, 0, 0x0004);
1062 lp->stats.tx_errors++;
1063 if (pcnet32_debug > 2) {
1064 int i;
1065 printk(KERN_DEBUG " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
1066 lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
1067 lp->cur_rx);
1068 for (i = 0 ; i < RX_RING_SIZE; i++)
1069 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
1070 lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
1071 lp->rx_ring[i].msg_length, (unsigned)lp->rx_ring[i].status);
1072 for (i = 0 ; i < TX_RING_SIZE; i++)
1073 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
1074 lp->tx_ring[i].base, -lp->tx_ring[i].length,
1075 lp->tx_ring[i].misc, (unsigned)lp->tx_ring[i].status);
1076 printk("\n");
1077 }
1078 pcnet32_restart(dev, 0x0042);
1079
1080 dev->trans_start = jiffies;
1081 netif_start_queue(dev);
1082
1083 spin_unlock_irqrestore(&lp->lock, flags);
1084}
1085
1086
1087static int
1088pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
1089{
1090 struct pcnet32_private *lp = dev->priv;
1091 unsigned long ioaddr = dev->base_addr;
1092 u16 status;
1093 int entry;
1094 unsigned long flags;
1095
1096 if (pcnet32_debug > 3) {
1097 printk(KERN_DEBUG "%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
1098 dev->name, lp->a.read_csr(ioaddr, 0));
1099 }
1100
1101 spin_lock_irqsave(&lp->lock, flags);
1102
1103
1104
1105
1106 status = 0x8300;
1107 if ((lp->ltint) &&
1108 ((lp->cur_tx - lp->dirty_tx == TX_RING_SIZE/2) ||
1109 (lp->cur_tx - lp->dirty_tx >= TX_RING_SIZE-2)))
1110 {
1111
1112
1113
1114
1115
1116 status = 0x9300;
1117 }
1118
1119
1120
1121
1122 entry = lp->cur_tx & TX_RING_MOD_MASK;
1123
1124
1125
1126
1127 lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
1128
1129 lp->tx_ring[entry].misc = 0x00000000;
1130
1131 lp->tx_skbuff[entry] = skb;
1132 lp->tx_dma_addr[entry] = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
1133 lp->tx_ring[entry].base = (u32)le32_to_cpu(lp->tx_dma_addr[entry]);
1134 wmb();
1135 lp->tx_ring[entry].status = le16_to_cpu(status);
1136
1137 lp->cur_tx++;
1138 lp->stats.tx_bytes += skb->len;
1139
1140
1141 lp->a.write_csr (ioaddr, 0, 0x0048);
1142
1143 dev->trans_start = jiffies;
1144
1145 if (lp->tx_ring[(entry+1) & TX_RING_MOD_MASK].base == 0)
1146 netif_start_queue(dev);
1147 else {
1148 lp->tx_full = 1;
1149 netif_stop_queue(dev);
1150 }
1151 spin_unlock_irqrestore(&lp->lock, flags);
1152 return 0;
1153}
1154
1155
1156static irqreturn_t
1157pcnet32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1158{
1159 struct net_device *dev = dev_id;
1160 struct pcnet32_private *lp;
1161 unsigned long ioaddr;
1162 u16 csr0,rap;
1163 int boguscnt = max_interrupt_work;
1164 int must_restart;
1165
1166 if (!dev) {
1167 printk (KERN_DEBUG "%s(): irq %d for unknown device\n",
1168 __FUNCTION__, irq);
1169 return IRQ_NONE;
1170 }
1171
1172 ioaddr = dev->base_addr;
1173 lp = dev->priv;
1174
1175 spin_lock(&lp->lock);
1176
1177 rap = lp->a.read_rap(ioaddr);
1178 while ((csr0 = lp->a.read_csr (ioaddr, 0)) & 0x8600 && --boguscnt >= 0) {
1179
1180 lp->a.write_csr (ioaddr, 0, csr0 & ~0x004f);
1181
1182 must_restart = 0;
1183
1184 if (pcnet32_debug > 5)
1185 printk(KERN_DEBUG "%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
1186 dev->name, csr0, lp->a.read_csr (ioaddr, 0));
1187
1188 if (csr0 & 0x0400)
1189 pcnet32_rx(dev);
1190
1191 if (csr0 & 0x0200) {
1192 unsigned int dirty_tx = lp->dirty_tx;
1193
1194 while (dirty_tx < lp->cur_tx) {
1195 int entry = dirty_tx & TX_RING_MOD_MASK;
1196 int status = (short)le16_to_cpu(lp->tx_ring[entry].status);
1197
1198 if (status < 0)
1199 break;
1200
1201 lp->tx_ring[entry].base = 0;
1202
1203 if (status & 0x4000) {
1204
1205 int err_status = le32_to_cpu(lp->tx_ring[entry].misc);
1206 lp->stats.tx_errors++;
1207 if (err_status & 0x04000000) lp->stats.tx_aborted_errors++;
1208 if (err_status & 0x08000000) lp->stats.tx_carrier_errors++;
1209 if (err_status & 0x10000000) lp->stats.tx_window_errors++;
1210#ifndef DO_DXSUFLO
1211 if (err_status & 0x40000000) {
1212 lp->stats.tx_fifo_errors++;
1213
1214
1215 printk(KERN_ERR "%s: Tx FIFO error! CSR0=%4.4x\n",
1216 dev->name, csr0);
1217 must_restart = 1;
1218 }
1219#else
1220 if (err_status & 0x40000000) {
1221 lp->stats.tx_fifo_errors++;
1222 if (! lp->dxsuflo) {
1223
1224
1225 printk(KERN_ERR "%s: Tx FIFO error! CSR0=%4.4x\n",
1226 dev->name, csr0);
1227 must_restart = 1;
1228 }
1229 }
1230#endif
1231 } else {
1232 if (status & 0x1800)
1233 lp->stats.collisions++;
1234 lp->stats.tx_packets++;
1235 }
1236
1237
1238 if (lp->tx_skbuff[entry]) {
1239 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[entry],
1240 lp->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
1241 dev_kfree_skb_irq(lp->tx_skbuff[entry]);
1242 lp->tx_skbuff[entry] = 0;
1243 lp->tx_dma_addr[entry] = 0;
1244 }
1245 dirty_tx++;
1246 }
1247
1248 if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
1249 printk(KERN_ERR "%s: out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
1250 dev->name, dirty_tx, lp->cur_tx, lp->tx_full);
1251 dirty_tx += TX_RING_SIZE;
1252 }
1253
1254 if (lp->tx_full &&
1255 netif_queue_stopped(dev) &&
1256 dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
1257
1258 lp->tx_full = 0;
1259 netif_wake_queue (dev);
1260 }
1261 lp->dirty_tx = dirty_tx;
1262 }
1263
1264
1265 if (csr0 & 0x4000) lp->stats.tx_errors++;
1266 if (csr0 & 0x1000) {
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276 pcnet32_rx(dev);
1277 lp->stats.rx_errors++;
1278 }
1279 if (csr0 & 0x0800) {
1280 printk(KERN_ERR "%s: Bus master arbitration failure, status %4.4x.\n",
1281 dev->name, csr0);
1282
1283 }
1284
1285 if (must_restart) {
1286
1287 lp->a.write_csr (ioaddr, 0, 0x0004);
1288 pcnet32_restart(dev, 0x0002);
1289 }
1290 }
1291
1292
1293 lp->a.write_csr (ioaddr, 0, 0x7940);
1294 lp->a.write_rap (ioaddr,rap);
1295
1296 if (pcnet32_debug > 4)
1297 printk(KERN_DEBUG "%s: exiting interrupt, csr0=%#4.4x.\n",
1298 dev->name, lp->a.read_csr (ioaddr, 0));
1299
1300 spin_unlock(&lp->lock);
1301
1302 return IRQ_HANDLED;
1303}
1304
1305static int
1306pcnet32_rx(struct net_device *dev)
1307{
1308 struct pcnet32_private *lp = dev->priv;
1309 int entry = lp->cur_rx & RX_RING_MOD_MASK;
1310
1311
1312 while ((short)le16_to_cpu(lp->rx_ring[entry].status) >= 0) {
1313 int status = (short)le16_to_cpu(lp->rx_ring[entry].status) >> 8;
1314
1315 if (status != 0x03) {
1316
1317
1318
1319
1320
1321
1322 if (status & 0x01)
1323 lp->stats.rx_errors++;
1324 if (status & 0x20) lp->stats.rx_frame_errors++;
1325 if (status & 0x10) lp->stats.rx_over_errors++;
1326 if (status & 0x08) lp->stats.rx_crc_errors++;
1327 if (status & 0x04) lp->stats.rx_fifo_errors++;
1328 lp->rx_ring[entry].status &= le16_to_cpu(0x03ff);
1329 } else {
1330
1331 short pkt_len = (le32_to_cpu(lp->rx_ring[entry].msg_length) & 0xfff)-4;
1332 struct sk_buff *skb;
1333
1334 if(pkt_len < 60) {
1335 printk(KERN_ERR "%s: Runt packet!\n",dev->name);
1336 lp->stats.rx_errors++;
1337 } else {
1338 int rx_in_place = 0;
1339
1340 if (pkt_len > rx_copybreak) {
1341 struct sk_buff *newskb;
1342
1343 if ((newskb = dev_alloc_skb (PKT_BUF_SZ))) {
1344 skb_reserve (newskb, 2);
1345 skb = lp->rx_skbuff[entry];
1346 pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[entry], skb->len, PCI_DMA_FROMDEVICE);
1347 skb_put (skb, pkt_len);
1348 lp->rx_skbuff[entry] = newskb;
1349 newskb->dev = dev;
1350 lp->rx_dma_addr[entry] =
1351 pci_map_single(lp->pci_dev, newskb->tail,
1352 newskb->len, PCI_DMA_FROMDEVICE);
1353 lp->rx_ring[entry].base = le32_to_cpu(lp->rx_dma_addr[entry]);
1354 rx_in_place = 1;
1355 } else
1356 skb = NULL;
1357 } else {
1358 skb = dev_alloc_skb(pkt_len+2);
1359 }
1360
1361 if (skb == NULL) {
1362 int i;
1363 printk(KERN_ERR "%s: Memory squeeze, deferring packet.\n", dev->name);
1364 for (i = 0; i < RX_RING_SIZE; i++)
1365 if ((short)le16_to_cpu(lp->rx_ring[(entry+i) & RX_RING_MOD_MASK].status) < 0)
1366 break;
1367
1368 if (i > RX_RING_SIZE -2) {
1369 lp->stats.rx_dropped++;
1370 lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
1371 lp->cur_rx++;
1372 }
1373 break;
1374 }
1375 skb->dev = dev;
1376 if (!rx_in_place) {
1377 skb_reserve(skb,2);
1378 skb_put(skb,pkt_len);
1379 pci_dma_sync_single(lp->pci_dev,
1380 lp->rx_dma_addr[entry],
1381 PKT_BUF_SZ,
1382 PCI_DMA_FROMDEVICE);
1383 eth_copy_and_sum(skb,
1384 (unsigned char *)(lp->rx_skbuff[entry]->tail),
1385 pkt_len,0);
1386 }
1387 lp->stats.rx_bytes += skb->len;
1388 skb->protocol=eth_type_trans(skb,dev);
1389 netif_rx(skb);
1390 dev->last_rx = jiffies;
1391 lp->stats.rx_packets++;
1392 }
1393 }
1394
1395
1396
1397
1398 lp->rx_ring[entry].buf_length = le16_to_cpu(-PKT_BUF_SZ);
1399 lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
1400 entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1401 }
1402
1403 return 0;
1404}
1405
1406static int
1407pcnet32_close(struct net_device *dev)
1408{
1409 unsigned long ioaddr = dev->base_addr;
1410 struct pcnet32_private *lp = dev->priv;
1411 int i;
1412
1413 del_timer_sync(&lp->watchdog_timer);
1414
1415 netif_stop_queue(dev);
1416
1417 lp->stats.rx_missed_errors = lp->a.read_csr (ioaddr, 112);
1418
1419 if (pcnet32_debug > 1)
1420 printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
1421 dev->name, lp->a.read_csr (ioaddr, 0));
1422
1423
1424 lp->a.write_csr (ioaddr, 0, 0x0004);
1425
1426
1427
1428
1429
1430 lp->a.write_bcr (ioaddr, 20, 4);
1431
1432 free_irq(dev->irq, dev);
1433
1434
1435 for (i = 0; i < RX_RING_SIZE; i++) {
1436 lp->rx_ring[i].status = 0;
1437 if (lp->rx_skbuff[i]) {
1438 pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i], lp->rx_skbuff[i]->len, PCI_DMA_FROMDEVICE);
1439 dev_kfree_skb(lp->rx_skbuff[i]);
1440 }
1441 lp->rx_skbuff[i] = NULL;
1442 lp->rx_dma_addr[i] = 0;
1443 }
1444
1445 for (i = 0; i < TX_RING_SIZE; i++) {
1446 if (lp->tx_skbuff[i]) {
1447 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i], lp->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
1448 dev_kfree_skb(lp->tx_skbuff[i]);
1449 }
1450 lp->tx_skbuff[i] = NULL;
1451 lp->tx_dma_addr[i] = 0;
1452 }
1453
1454 return 0;
1455}
1456
1457static struct net_device_stats *
1458pcnet32_get_stats(struct net_device *dev)
1459{
1460 struct pcnet32_private *lp = dev->priv;
1461 unsigned long ioaddr = dev->base_addr;
1462 u16 saved_addr;
1463 unsigned long flags;
1464
1465 spin_lock_irqsave(&lp->lock, flags);
1466 saved_addr = lp->a.read_rap(ioaddr);
1467 lp->stats.rx_missed_errors = lp->a.read_csr (ioaddr, 112);
1468 lp->a.write_rap(ioaddr, saved_addr);
1469 spin_unlock_irqrestore(&lp->lock, flags);
1470
1471 return &lp->stats;
1472}
1473
1474
1475static void pcnet32_load_multicast (struct net_device *dev)
1476{
1477 struct pcnet32_private *lp = dev->priv;
1478 volatile struct pcnet32_init_block *ib = &lp->init_block;
1479 volatile u16 *mcast_table = (u16 *)&ib->filter;
1480 struct dev_mc_list *dmi=dev->mc_list;
1481 char *addrs;
1482 int i;
1483 u32 crc;
1484
1485
1486 if (dev->flags & IFF_ALLMULTI){
1487 ib->filter[0] = 0xffffffff;
1488 ib->filter[1] = 0xffffffff;
1489 return;
1490 }
1491
1492 ib->filter[0] = 0;
1493 ib->filter[1] = 0;
1494
1495
1496 for (i = 0; i < dev->mc_count; i++){
1497 addrs = dmi->dmi_addr;
1498 dmi = dmi->next;
1499
1500
1501 if (!(*addrs & 1))
1502 continue;
1503
1504 crc = ether_crc_le(6, addrs);
1505 crc = crc >> 26;
1506 mcast_table [crc >> 4] = le16_to_cpu(
1507 le16_to_cpu(mcast_table [crc >> 4]) | (1 << (crc & 0xf))
1508 );
1509 }
1510 return;
1511}
1512
1513
1514
1515
1516
1517static void pcnet32_set_multicast_list(struct net_device *dev)
1518{
1519 unsigned long ioaddr = dev->base_addr, flags;
1520 struct pcnet32_private *lp = dev->priv;
1521
1522 spin_lock_irqsave(&lp->lock, flags);
1523 if (dev->flags&IFF_PROMISC) {
1524
1525 printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
1526 lp->init_block.mode = le16_to_cpu(0x8000 | (lp->options & PCNET32_PORT_PORTSEL) << 7);
1527 } else {
1528 lp->init_block.mode = le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
1529 pcnet32_load_multicast (dev);
1530 }
1531
1532 lp->a.write_csr (ioaddr, 0, 0x0004);
1533
1534 pcnet32_restart(dev, 0x0042);
1535 spin_unlock_irqrestore(&lp->lock, flags);
1536}
1537
1538static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
1539{
1540 struct pcnet32_private *lp = dev->priv;
1541 unsigned long ioaddr = dev->base_addr;
1542 u16 val_out;
1543 int phyaddr;
1544
1545 if (!lp->mii)
1546 return 0;
1547
1548 phyaddr = lp->a.read_bcr(ioaddr, 33);
1549
1550 lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
1551 val_out = lp->a.read_bcr(ioaddr, 34);
1552 lp->a.write_bcr(ioaddr, 33, phyaddr);
1553
1554 return val_out;
1555}
1556
1557static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
1558{
1559 struct pcnet32_private *lp = dev->priv;
1560 unsigned long ioaddr = dev->base_addr;
1561 int phyaddr;
1562
1563 if (!lp->mii)
1564 return;
1565
1566 phyaddr = lp->a.read_bcr(ioaddr, 33);
1567
1568 lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
1569 lp->a.write_bcr(ioaddr, 34, val);
1570 lp->a.write_bcr(ioaddr, 33, phyaddr);
1571}
1572
1573static int pcnet32_ethtool_ioctl (struct net_device *dev, void *useraddr)
1574{
1575 struct pcnet32_private *lp = dev->priv;
1576 u32 ethcmd;
1577 int phyaddr = 0;
1578 int phy_id = 0;
1579 unsigned long ioaddr = dev->base_addr;
1580
1581 if (lp->mii) {
1582 phyaddr = lp->a.read_bcr (ioaddr, 33);
1583 phy_id = (phyaddr >> 5) & 0x1f;
1584 lp->mii_if.phy_id = phy_id;
1585 }
1586
1587 if (copy_from_user (ðcmd, useraddr, sizeof (ethcmd)))
1588 return -EFAULT;
1589
1590 switch (ethcmd) {
1591 case ETHTOOL_GDRVINFO: {
1592 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1593 strcpy (info.driver, DRV_NAME);
1594 strcpy (info.version, DRV_VERSION);
1595 if (lp->pci_dev)
1596 strcpy (info.bus_info, pci_name(lp->pci_dev));
1597 else
1598 sprintf(info.bus_info, "VLB 0x%lx", dev->base_addr);
1599 if (copy_to_user (useraddr, &info, sizeof (info)))
1600 return -EFAULT;
1601 return 0;
1602 }
1603
1604
1605 case ETHTOOL_GSET: {
1606 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1607 spin_lock_irq(&lp->lock);
1608 mii_ethtool_gset(&lp->mii_if, &ecmd);
1609 spin_unlock_irq(&lp->lock);
1610 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1611 return -EFAULT;
1612 return 0;
1613 }
1614
1615 case ETHTOOL_SSET: {
1616 int r;
1617 struct ethtool_cmd ecmd;
1618 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1619 return -EFAULT;
1620 spin_lock_irq(&lp->lock);
1621 r = mii_ethtool_sset(&lp->mii_if, &ecmd);
1622 spin_unlock_irq(&lp->lock);
1623 return r;
1624 }
1625
1626 case ETHTOOL_NWAY_RST: {
1627 return mii_nway_restart(&lp->mii_if);
1628 }
1629
1630 case ETHTOOL_GLINK: {
1631 struct ethtool_value edata = {ETHTOOL_GLINK};
1632 edata.data = mii_link_ok(&lp->mii_if);
1633 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1634 return -EFAULT;
1635 return 0;
1636 }
1637
1638
1639 case ETHTOOL_GMSGLVL: {
1640 struct ethtool_value edata = {ETHTOOL_GMSGLVL};
1641 edata.data = pcnet32_debug;
1642 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1643 return -EFAULT;
1644 return 0;
1645 }
1646
1647 case ETHTOOL_SMSGLVL: {
1648 struct ethtool_value edata;
1649 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1650 return -EFAULT;
1651 pcnet32_debug = edata.data;
1652 return 0;
1653 }
1654 default:
1655 break;
1656 }
1657
1658 return -EOPNOTSUPP;
1659}
1660
1661static int pcnet32_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1662{
1663 unsigned long ioaddr = dev->base_addr;
1664 struct pcnet32_private *lp = dev->priv;
1665 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
1666 int phyaddr = lp->a.read_bcr (ioaddr, 33);
1667
1668 if (cmd == SIOCETHTOOL)
1669 return pcnet32_ethtool_ioctl(dev, (void *) rq->ifr_data);
1670
1671 if (lp->mii) {
1672 switch(cmd) {
1673 case SIOCGMIIPHY:
1674 data->phy_id = (phyaddr >> 5) & 0x1f;
1675
1676 case SIOCGMIIREG:
1677 lp->a.write_bcr (ioaddr, 33, ((data->phy_id & 0x1f) << 5) | (data->reg_num & 0x1f));
1678 data->val_out = lp->a.read_bcr (ioaddr, 34);
1679 lp->a.write_bcr (ioaddr, 33, phyaddr);
1680 return 0;
1681 case SIOCSMIIREG:
1682 if (!capable(CAP_NET_ADMIN))
1683 return -EPERM;
1684 lp->a.write_bcr (ioaddr, 33, ((data->phy_id & 0x1f) << 5) | (data->reg_num & 0x1f));
1685 lp->a.write_bcr (ioaddr, 34, data->val_in);
1686 lp->a.write_bcr (ioaddr, 33, phyaddr);
1687 return 0;
1688 default:
1689 return -EOPNOTSUPP;
1690 }
1691 }
1692 return -EOPNOTSUPP;
1693}
1694
1695static void pcnet32_watchdog(struct net_device *dev)
1696{
1697 struct pcnet32_private *lp = dev->priv;
1698
1699
1700 if (lp->mii)
1701 mii_check_media (&lp->mii_if, 1, 0);
1702
1703 mod_timer (&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
1704}
1705
1706static struct pci_driver pcnet32_driver = {
1707 .name = DRV_NAME,
1708 .probe = pcnet32_probe_pci,
1709 .id_table = pcnet32_pci_tbl,
1710};
1711
1712MODULE_PARM(debug, "i");
1713MODULE_PARM_DESC(debug, DRV_NAME " debug level (0-6)");
1714MODULE_PARM(max_interrupt_work, "i");
1715MODULE_PARM_DESC(max_interrupt_work, DRV_NAME " maximum events handled per interrupt");
1716MODULE_PARM(rx_copybreak, "i");
1717MODULE_PARM_DESC(rx_copybreak, DRV_NAME " copy breakpoint for copy-only-tiny-frames");
1718MODULE_PARM(tx_start_pt, "i");
1719MODULE_PARM_DESC(tx_start_pt, DRV_NAME " transmit start point (0-3)");
1720MODULE_PARM(pcnet32vlb, "i");
1721MODULE_PARM_DESC(pcnet32vlb, DRV_NAME " Vesa local bus (VLB) support (0/1)");
1722MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
1723MODULE_PARM_DESC(options, DRV_NAME " initial option setting(s) (0-15)");
1724MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
1725MODULE_PARM_DESC(full_duplex, DRV_NAME " full duplex setting(s) (1)");
1726
1727MODULE_AUTHOR("Thomas Bogendoerfer");
1728MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
1729MODULE_LICENSE("GPL");
1730
1731
1732static int debug = -1;
1733static int tx_start_pt = -1;
1734static int pcnet32_have_pci;
1735
1736static int __init pcnet32_init_module(void)
1737{
1738 printk(KERN_INFO "%s", version);
1739
1740 if (debug > 0)
1741 pcnet32_debug = debug;
1742
1743 if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
1744 tx_start = tx_start_pt;
1745
1746
1747 if (!pci_module_init(&pcnet32_driver))
1748 pcnet32_have_pci = 1;
1749
1750
1751 if (pcnet32vlb)
1752 pcnet32_probe_vlbus();
1753
1754 if (cards_found)
1755 printk(KERN_INFO PFX "%d cards_found.\n", cards_found);
1756
1757 return (pcnet32_have_pci + cards_found) ? 0 : -ENODEV;
1758}
1759
1760static void __exit pcnet32_cleanup_module(void)
1761{
1762 struct net_device *next_dev;
1763
1764 while (pcnet32_dev) {
1765 struct pcnet32_private *lp = pcnet32_dev->priv;
1766 next_dev = lp->next;
1767 unregister_netdev(pcnet32_dev);
1768 release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
1769 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
1770 free_netdev(pcnet32_dev);
1771 pcnet32_dev = next_dev;
1772 }
1773
1774 if (pcnet32_have_pci)
1775 pci_unregister_driver(&pcnet32_driver);
1776}
1777
1778module_init(pcnet32_init_module);
1779module_exit(pcnet32_cleanup_module);
1780
1781
1782
1783
1784
1785
1786
1787
1788