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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130#define DRV_NAME "via-rhine"
131#define DRV_VERSION "1.1.19"
132#define DRV_RELDATE "July-12-2003"
133
134
135
136
137
138static int debug = 1;
139static int max_interrupt_work = 20;
140
141
142
143static int rx_copybreak;
144
145
146static int backoff;
147
148
149
150
151
152
153
154
155
156
157
158#define MAX_UNITS 8
159static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
160static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
161
162
163
164static const int multicast_filter_limit = 32;
165
166
167
168
169
170
171
172
173
174#define TX_RING_SIZE 16
175#define TX_QUEUE_LEN 10
176#define RX_RING_SIZE 16
177
178
179
180
181
182#define TX_TIMEOUT (2*HZ)
183
184#define PKT_BUF_SZ 1536
185
186#if !defined(__OPTIMIZE__) || !defined(__KERNEL__)
187#warning You must compile this file with the correct options!
188#warning See the last lines of the source file.
189#error You must compile this driver with "-O".
190#endif
191
192#include <linux/module.h>
193#include <linux/kernel.h>
194#include <linux/string.h>
195#include <linux/timer.h>
196#include <linux/errno.h>
197#include <linux/ioport.h>
198#include <linux/slab.h>
199#include <linux/interrupt.h>
200#include <linux/pci.h>
201#include <linux/netdevice.h>
202#include <linux/etherdevice.h>
203#include <linux/skbuff.h>
204#include <linux/init.h>
205#include <linux/delay.h>
206#include <linux/mii.h>
207#include <linux/ethtool.h>
208#include <linux/crc32.h>
209#include <asm/processor.h>
210#include <asm/bitops.h>
211#include <asm/io.h>
212#include <asm/irq.h>
213#include <asm/uaccess.h>
214
215
216static char version[] __devinitdata =
217KERN_INFO DRV_NAME ".c:v1.10-LK" DRV_VERSION " " DRV_RELDATE " Written by Donald Becker\n"
218KERN_INFO " http://www.scyld.com/network/via-rhine.html\n";
219
220static char shortname[] = DRV_NAME;
221
222
223
224
225#ifdef CONFIG_VIA_RHINE_MMIO
226#define USE_MEM
227#else
228#define USE_IO
229#undef readb
230#undef readw
231#undef readl
232#undef writeb
233#undef writew
234#undef writel
235#define readb inb
236#define readw inw
237#define readl inl
238#define writeb outb
239#define writew outw
240#define writel outl
241#endif
242
243MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
244MODULE_DESCRIPTION("VIA Rhine PCI Fast Ethernet driver");
245MODULE_LICENSE("GPL");
246
247MODULE_PARM(max_interrupt_work, "i");
248MODULE_PARM(debug, "i");
249MODULE_PARM(rx_copybreak, "i");
250MODULE_PARM(backoff, "i");
251MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
252MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
253MODULE_PARM_DESC(max_interrupt_work, "VIA Rhine maximum events handled per interrupt");
254MODULE_PARM_DESC(debug, "VIA Rhine debug level (0-7)");
255MODULE_PARM_DESC(rx_copybreak, "VIA Rhine copy breakpoint for copy-only-tiny-frames");
256MODULE_PARM_DESC(backoff, "VIA Rhine: Bits 0-3: backoff algorithm");
257MODULE_PARM_DESC(options, "VIA Rhine: Bits 0-3: media type, bit 17: full duplex");
258MODULE_PARM_DESC(full_duplex, "VIA Rhine full duplex setting(s) (1)");
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359enum pci_flags_bit {
360 PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
361 PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
362};
363
364enum via_rhine_chips {
365 VT86C100A = 0,
366 VT6102,
367 VT6105,
368 VT6105M
369};
370
371struct via_rhine_chip_info {
372 const char *name;
373 u16 pci_flags;
374 int io_size;
375 int drv_flags;
376};
377
378
379enum chip_capability_flags {
380 CanHaveMII=1, HasESIPhy=2, HasDavicomPhy=4,
381 ReqTxAlign=0x10, HasWOL=0x20, };
382
383#ifdef USE_MEM
384#define RHINE_IOTYPE (PCI_USES_MEM | PCI_USES_MASTER | PCI_ADDR1)
385#else
386#define RHINE_IOTYPE (PCI_USES_IO | PCI_USES_MASTER | PCI_ADDR0)
387#endif
388
389#define IOSYNC do { readb(dev->base_addr + StationAddr); } while (0)
390
391
392static struct via_rhine_chip_info via_rhine_chip_info[] __devinitdata =
393{
394 { "VIA VT86C100A Rhine", RHINE_IOTYPE, 128,
395 CanHaveMII | ReqTxAlign | HasDavicomPhy },
396 { "VIA VT6102 Rhine-II", RHINE_IOTYPE, 256,
397 CanHaveMII | HasWOL },
398 { "VIA VT6105 Rhine-III", RHINE_IOTYPE, 256,
399 CanHaveMII | HasWOL },
400 { "VIA VT6105M Rhine-III", RHINE_IOTYPE, 256,
401 CanHaveMII | HasWOL },
402};
403
404static struct pci_device_id via_rhine_pci_tbl[] =
405{
406 {0x1106, 0x3043, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VT86C100A},
407 {0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VT6102},
408 {0x1106, 0x3106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VT6105},
409 {0x1106, 0x3053, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VT6105M},
410 {0,}
411};
412MODULE_DEVICE_TABLE(pci, via_rhine_pci_tbl);
413
414
415
416enum register_offsets {
417 StationAddr=0x00, RxConfig=0x06, TxConfig=0x07, ChipCmd=0x08,
418 IntrStatus=0x0C, IntrEnable=0x0E,
419 MulticastFilter0=0x10, MulticastFilter1=0x14,
420 RxRingPtr=0x18, TxRingPtr=0x1C, GFIFOTest=0x54,
421 MIIPhyAddr=0x6C, MIIStatus=0x6D, PCIBusConfig=0x6E,
422 MIICmd=0x70, MIIRegAddr=0x71, MIIData=0x72, MACRegEEcsr=0x74,
423 ConfigA=0x78, ConfigB=0x79, ConfigC=0x7A, ConfigD=0x7B,
424 RxMissed=0x7C, RxCRCErrs=0x7E, MiscCmd=0x81,
425 StickyHW=0x83, IntrStatus2=0x84, WOLcrClr=0xA4, WOLcgClr=0xA7,
426 PwrcsrClr=0xAC,
427};
428
429
430enum backoff_bits {
431 BackOptional=0x01, BackModify=0x02,
432 BackCaptureEffect=0x04, BackRandom=0x08
433};
434
435#ifdef USE_MEM
436
437int mmio_verify_registers[] = {
438 RxConfig, TxConfig, IntrEnable, ConfigA, ConfigB, ConfigC, ConfigD,
439 0
440};
441#endif
442
443
444enum intr_status_bits {
445 IntrRxDone=0x0001, IntrRxErr=0x0004, IntrRxEmpty=0x0020,
446 IntrTxDone=0x0002, IntrTxError=0x0008, IntrTxUnderrun=0x0210,
447 IntrPCIErr=0x0040,
448 IntrStatsMax=0x0080, IntrRxEarly=0x0100,
449 IntrRxOverflow=0x0400, IntrRxDropped=0x0800, IntrRxNoBuf=0x1000,
450 IntrTxAborted=0x2000, IntrLinkChange=0x4000,
451 IntrRxWakeUp=0x8000,
452 IntrNormalSummary=0x0003, IntrAbnormalSummary=0xC260,
453 IntrTxDescRace=0x080000,
454 IntrTxErrSummary=0x082218,
455};
456
457
458struct rx_desc {
459 s32 rx_status;
460 u32 desc_length;
461 u32 addr;
462 u32 next_desc;
463};
464struct tx_desc {
465 s32 tx_status;
466 u32 desc_length;
467 u32 addr;
468 u32 next_desc;
469};
470
471
472#define TXDESC 0x00e08000
473
474enum rx_status_bits {
475 RxOK=0x8000, RxWholePkt=0x0300, RxErr=0x008F
476};
477
478
479enum desc_status_bits {
480 DescOwn=0x80000000
481};
482
483
484enum chip_cmd_bits {
485 CmdInit=0x0001, CmdStart=0x0002, CmdStop=0x0004, CmdRxOn=0x0008,
486 CmdTxOn=0x0010, CmdTxDemand=0x0020, CmdRxDemand=0x0040,
487 CmdEarlyRx=0x0100, CmdEarlyTx=0x0200, CmdFDuplex=0x0400,
488 CmdNoTxPoll=0x0800, CmdReset=0x8000,
489};
490
491#define MAX_MII_CNT 4
492struct netdev_private {
493
494 struct rx_desc *rx_ring;
495 struct tx_desc *tx_ring;
496 dma_addr_t rx_ring_dma;
497 dma_addr_t tx_ring_dma;
498
499
500 struct sk_buff *rx_skbuff[RX_RING_SIZE];
501 dma_addr_t rx_skbuff_dma[RX_RING_SIZE];
502
503
504 struct sk_buff *tx_skbuff[TX_RING_SIZE];
505 dma_addr_t tx_skbuff_dma[TX_RING_SIZE];
506
507
508 unsigned char *tx_buf[TX_RING_SIZE];
509 unsigned char *tx_bufs;
510 dma_addr_t tx_bufs_dma;
511
512 struct pci_dev *pdev;
513 struct net_device_stats stats;
514 struct timer_list timer;
515 spinlock_t lock;
516
517
518 int chip_id, drv_flags;
519 struct rx_desc *rx_head_desc;
520 unsigned int cur_rx, dirty_rx;
521 unsigned int cur_tx, dirty_tx;
522 unsigned int rx_buf_sz;
523 u16 chip_cmd;
524
525
526 unsigned int default_port:4;
527 u8 tx_thresh, rx_thresh;
528
529
530 unsigned char phys[MAX_MII_CNT];
531 unsigned int mii_cnt;
532 u16 mii_status;
533 struct mii_if_info mii_if;
534};
535
536static int mdio_read(struct net_device *dev, int phy_id, int location);
537static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
538static int via_rhine_open(struct net_device *dev);
539static void via_rhine_check_duplex(struct net_device *dev);
540static void via_rhine_timer(unsigned long data);
541static void via_rhine_tx_timeout(struct net_device *dev);
542static int via_rhine_start_tx(struct sk_buff *skb, struct net_device *dev);
543static irqreturn_t via_rhine_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
544static void via_rhine_tx(struct net_device *dev);
545static void via_rhine_rx(struct net_device *dev);
546static void via_rhine_error(struct net_device *dev, int intr_status);
547static void via_rhine_set_rx_mode(struct net_device *dev);
548static struct net_device_stats *via_rhine_get_stats(struct net_device *dev);
549static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
550static struct ethtool_ops netdev_ethtool_ops;
551static int via_rhine_close(struct net_device *dev);
552
553static inline u32 get_intr_status(struct net_device *dev)
554{
555 long ioaddr = dev->base_addr;
556 struct netdev_private *np = dev->priv;
557 u32 intr_status;
558
559 intr_status = readw(ioaddr + IntrStatus);
560
561 if (np->chip_id == VT6102)
562 intr_status |= readb(ioaddr + IntrStatus2) << 16;
563 return intr_status;
564}
565
566static void wait_for_reset(struct net_device *dev, int chip_id, char *name)
567{
568 long ioaddr = dev->base_addr;
569 int boguscnt = 20;
570
571 IOSYNC;
572
573 if (readw(ioaddr + ChipCmd) & CmdReset) {
574 printk(KERN_INFO "%s: Reset not complete yet. "
575 "Trying harder.\n", name);
576
577
578 if (chip_id == VT6102)
579 writeb(0x40, ioaddr + MiscCmd);
580
581
582
583 while ((readw(ioaddr + ChipCmd) & CmdReset) && --boguscnt)
584 udelay(5);
585
586 }
587
588 if (debug > 1)
589 printk(KERN_INFO "%s: Reset %s.\n", name,
590 boguscnt ? "succeeded" : "failed");
591}
592
593#ifdef USE_MEM
594static void __devinit enable_mmio(long ioaddr, int chip_id)
595{
596 int n;
597 if (chip_id == VT86C100A) {
598
599 n = inb(ioaddr + ConfigA) | 0x20;
600 outb(n, ioaddr + ConfigA);
601 } else {
602 n = inb(ioaddr + ConfigD) | 0x80;
603 outb(n, ioaddr + ConfigD);
604 }
605}
606#endif
607
608static void __devinit reload_eeprom(long ioaddr)
609{
610 int i;
611 outb(0x20, ioaddr + MACRegEEcsr);
612
613 for (i = 0; i < 150; i++)
614 if (! (inb(ioaddr + MACRegEEcsr) & 0x20))
615 break;
616}
617
618#ifdef CONFIG_NET_POLL_CONTROLLER
619static void via_rhine_poll(struct net_device *dev)
620{
621 disable_irq(dev->irq);
622 via_rhine_interrupt(dev->irq, (void *)dev, NULL);
623 enable_irq(dev->irq);
624}
625#endif
626
627static int __devinit via_rhine_init_one (struct pci_dev *pdev,
628 const struct pci_device_id *ent)
629{
630 struct net_device *dev;
631 struct netdev_private *np;
632 int i, option;
633 int chip_id = (int) ent->driver_data;
634 static int card_idx = -1;
635 long ioaddr;
636 long memaddr;
637 int io_size;
638 int pci_flags;
639#ifdef USE_MEM
640 long ioaddr0;
641#endif
642
643
644#ifndef MODULE
645 static int printed_version;
646 if (!printed_version++)
647 printk(version);
648#endif
649
650 card_idx++;
651 option = card_idx < MAX_UNITS ? options[card_idx] : 0;
652 io_size = via_rhine_chip_info[chip_id].io_size;
653 pci_flags = via_rhine_chip_info[chip_id].pci_flags;
654
655 if (pci_enable_device (pdev))
656 goto err_out;
657
658
659 if (pci_set_dma_mask(pdev, 0xffffffff)) {
660 printk(KERN_ERR "32-bit PCI DMA addresses not supported by the card!?\n");
661 goto err_out;
662 }
663
664
665 if ((pci_resource_len (pdev, 0) < io_size) ||
666 (pci_resource_len (pdev, 1) < io_size)) {
667 printk (KERN_ERR "Insufficient PCI resources, aborting\n");
668 goto err_out;
669 }
670
671 ioaddr = pci_resource_start (pdev, 0);
672 memaddr = pci_resource_start (pdev, 1);
673
674 if (pci_flags & PCI_USES_MASTER)
675 pci_set_master (pdev);
676
677 dev = alloc_etherdev(sizeof(*np));
678 if (dev == NULL) {
679 printk (KERN_ERR "init_ethernet failed for card #%d\n", card_idx);
680 goto err_out;
681 }
682 SET_MODULE_OWNER(dev);
683 SET_NETDEV_DEV(dev, &pdev->dev);
684
685 if (pci_request_regions(pdev, shortname))
686 goto err_out_free_netdev;
687
688#ifdef USE_MEM
689 ioaddr0 = ioaddr;
690 enable_mmio(ioaddr0, chip_id);
691
692 ioaddr = (long) ioremap (memaddr, io_size);
693 if (!ioaddr) {
694 printk (KERN_ERR "ioremap failed for device %s, region 0x%X @ 0x%lX\n",
695 pci_name(pdev), io_size, memaddr);
696 goto err_out_free_res;
697 }
698
699
700 i = 0;
701 while (mmio_verify_registers[i]) {
702 int reg = mmio_verify_registers[i++];
703 unsigned char a = inb(ioaddr0+reg);
704 unsigned char b = readb(ioaddr+reg);
705 if (a != b) {
706 printk (KERN_ERR "MMIO do not match PIO [%02x] (%02x != %02x)\n",
707 reg, a, b);
708 goto err_out_unmap;
709 }
710 }
711#endif
712
713
714 if (via_rhine_chip_info[chip_id].drv_flags & HasWOL) {
715 unsigned char byOrgValue;
716
717
718 byOrgValue = readb(ioaddr + StickyHW);
719 byOrgValue = byOrgValue & 0xFC;
720 writeb(byOrgValue, ioaddr + StickyHW);
721
722
723
724 writeb(0x80, ioaddr + WOLcgClr);
725
726 writeb(0xFF, ioaddr + WOLcrClr);
727
728 writeb(0xFF, ioaddr + PwrcsrClr);
729 }
730
731
732 writew(CmdReset, ioaddr + ChipCmd);
733
734 dev->base_addr = ioaddr;
735 wait_for_reset(dev, chip_id, shortname);
736
737
738#ifdef USE_IO
739 reload_eeprom(ioaddr);
740#else
741 reload_eeprom(ioaddr0);
742
743
744
745 enable_mmio(ioaddr0, chip_id);
746#endif
747
748 for (i = 0; i < 6; i++)
749 dev->dev_addr[i] = readb(ioaddr + StationAddr + i);
750
751 if (!is_valid_ether_addr(dev->dev_addr)) {
752 printk(KERN_ERR "Invalid MAC address for card #%d\n", card_idx);
753 goto err_out_unmap;
754 }
755
756 if (chip_id == VT6102) {
757
758
759
760
761
762 writeb(readb(ioaddr + ConfigA) & 0xFE, ioaddr + ConfigA);
763 }
764
765
766 if (backoff)
767 writeb(readb(ioaddr + ConfigD) & (0xF0 | backoff),
768 ioaddr + ConfigD);
769
770 dev->irq = pdev->irq;
771
772 np = dev->priv;
773 spin_lock_init (&np->lock);
774 np->chip_id = chip_id;
775 np->drv_flags = via_rhine_chip_info[chip_id].drv_flags;
776 np->pdev = pdev;
777 np->mii_if.dev = dev;
778 np->mii_if.mdio_read = mdio_read;
779 np->mii_if.mdio_write = mdio_write;
780 np->mii_if.phy_id_mask = 0x1f;
781 np->mii_if.reg_num_mask = 0x1f;
782
783 if (dev->mem_start)
784 option = dev->mem_start;
785
786
787 dev->open = via_rhine_open;
788 dev->hard_start_xmit = via_rhine_start_tx;
789 dev->stop = via_rhine_close;
790 dev->get_stats = via_rhine_get_stats;
791 dev->set_multicast_list = via_rhine_set_rx_mode;
792 dev->do_ioctl = netdev_ioctl;
793 dev->ethtool_ops = &netdev_ethtool_ops;
794 dev->tx_timeout = via_rhine_tx_timeout;
795 dev->watchdog_timeo = TX_TIMEOUT;
796#ifdef CONFIG_NET_POLL_CONTROLLER
797 dev->poll_controller = via_rhine_poll;
798#endif
799 if (np->drv_flags & ReqTxAlign)
800 dev->features |= NETIF_F_SG|NETIF_F_HW_CSUM;
801
802
803 i = register_netdev(dev);
804 if (i)
805 goto err_out_unmap;
806
807
808 if (option > 0) {
809 if (option & 0x220)
810 np->mii_if.full_duplex = 1;
811 np->default_port = option & 15;
812 }
813 if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
814 np->mii_if.full_duplex = 1;
815
816 if (np->mii_if.full_duplex) {
817 printk(KERN_INFO "%s: Set to forced full duplex, autonegotiation"
818 " disabled.\n", dev->name);
819 np->mii_if.force_media = 1;
820 }
821
822 printk(KERN_INFO "%s: %s at 0x%lx, ",
823 dev->name, via_rhine_chip_info[chip_id].name,
824 (pci_flags & PCI_USES_IO) ? ioaddr : memaddr);
825
826 for (i = 0; i < 5; i++)
827 printk("%2.2x:", dev->dev_addr[i]);
828 printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], pdev->irq);
829
830 pci_set_drvdata(pdev, dev);
831
832 if (np->drv_flags & CanHaveMII) {
833 int phy, phy_idx = 0;
834 np->phys[0] = 1;
835 for (phy = 1; phy < 32 && phy_idx < MAX_MII_CNT; phy++) {
836 int mii_status = mdio_read(dev, phy, 1);
837 if (mii_status != 0xffff && mii_status != 0x0000) {
838 np->phys[phy_idx++] = phy;
839 np->mii_if.advertising = mdio_read(dev, phy, 4);
840 printk(KERN_INFO "%s: MII PHY found at address %d, status "
841 "0x%4.4x advertising %4.4x Link %4.4x.\n",
842 dev->name, phy, mii_status, np->mii_if.advertising,
843 mdio_read(dev, phy, 5));
844
845
846 if (mii_status & BMSR_LSTATUS)
847 netif_carrier_on(dev);
848 else
849 netif_carrier_off(dev);
850
851 break;
852 }
853 }
854 np->mii_cnt = phy_idx;
855 np->mii_if.phy_id = np->phys[0];
856 }
857
858
859 if (option > 0) {
860 if (option & 0x220)
861 np->mii_if.full_duplex = 1;
862 np->default_port = option & 0x3ff;
863 if (np->default_port & 0x330) {
864
865
866 printk(KERN_INFO " Forcing %dMbs %s-duplex operation.\n",
867 (option & 0x300 ? 100 : 10),
868 (option & 0x220 ? "full" : "half"));
869 if (np->mii_cnt)
870 mdio_write(dev, np->phys[0], MII_BMCR,
871 ((option & 0x300) ? 0x2000 : 0) |
872 ((option & 0x220) ? 0x0100 : 0));
873 }
874 }
875
876 return 0;
877
878err_out_unmap:
879#ifdef USE_MEM
880 iounmap((void *)ioaddr);
881err_out_free_res:
882#endif
883 pci_release_regions(pdev);
884err_out_free_netdev:
885 free_netdev (dev);
886err_out:
887 return -ENODEV;
888}
889
890static int alloc_ring(struct net_device* dev)
891{
892 struct netdev_private *np = dev->priv;
893 void *ring;
894 dma_addr_t ring_dma;
895
896 ring = pci_alloc_consistent(np->pdev,
897 RX_RING_SIZE * sizeof(struct rx_desc) +
898 TX_RING_SIZE * sizeof(struct tx_desc),
899 &ring_dma);
900 if (!ring) {
901 printk(KERN_ERR "Could not allocate DMA memory.\n");
902 return -ENOMEM;
903 }
904 if (np->drv_flags & ReqTxAlign) {
905 np->tx_bufs = pci_alloc_consistent(np->pdev, PKT_BUF_SZ * TX_RING_SIZE,
906 &np->tx_bufs_dma);
907 if (np->tx_bufs == NULL) {
908 pci_free_consistent(np->pdev,
909 RX_RING_SIZE * sizeof(struct rx_desc) +
910 TX_RING_SIZE * sizeof(struct tx_desc),
911 ring, ring_dma);
912 return -ENOMEM;
913 }
914 }
915
916 np->rx_ring = ring;
917 np->tx_ring = ring + RX_RING_SIZE * sizeof(struct rx_desc);
918 np->rx_ring_dma = ring_dma;
919 np->tx_ring_dma = ring_dma + RX_RING_SIZE * sizeof(struct rx_desc);
920
921 return 0;
922}
923
924void free_ring(struct net_device* dev)
925{
926 struct netdev_private *np = dev->priv;
927
928 pci_free_consistent(np->pdev,
929 RX_RING_SIZE * sizeof(struct rx_desc) +
930 TX_RING_SIZE * sizeof(struct tx_desc),
931 np->rx_ring, np->rx_ring_dma);
932 np->tx_ring = NULL;
933
934 if (np->tx_bufs)
935 pci_free_consistent(np->pdev, PKT_BUF_SZ * TX_RING_SIZE,
936 np->tx_bufs, np->tx_bufs_dma);
937
938 np->tx_bufs = NULL;
939
940}
941
942static void alloc_rbufs(struct net_device *dev)
943{
944 struct netdev_private *np = dev->priv;
945 dma_addr_t next;
946 int i;
947
948 np->dirty_rx = np->cur_rx = 0;
949
950 np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
951 np->rx_head_desc = &np->rx_ring[0];
952 next = np->rx_ring_dma;
953
954
955 for (i = 0; i < RX_RING_SIZE; i++) {
956 np->rx_ring[i].rx_status = 0;
957 np->rx_ring[i].desc_length = cpu_to_le32(np->rx_buf_sz);
958 next += sizeof(struct rx_desc);
959 np->rx_ring[i].next_desc = cpu_to_le32(next);
960 np->rx_skbuff[i] = 0;
961 }
962
963 np->rx_ring[i-1].next_desc = cpu_to_le32(np->rx_ring_dma);
964
965
966 for (i = 0; i < RX_RING_SIZE; i++) {
967 struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
968 np->rx_skbuff[i] = skb;
969 if (skb == NULL)
970 break;
971 skb->dev = dev;
972
973 np->rx_skbuff_dma[i] =
974 pci_map_single(np->pdev, skb->tail, np->rx_buf_sz,
975 PCI_DMA_FROMDEVICE);
976
977 np->rx_ring[i].addr = cpu_to_le32(np->rx_skbuff_dma[i]);
978 np->rx_ring[i].rx_status = cpu_to_le32(DescOwn);
979 }
980 np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
981}
982
983static void free_rbufs(struct net_device* dev)
984{
985 struct netdev_private *np = dev->priv;
986 int i;
987
988
989 for (i = 0; i < RX_RING_SIZE; i++) {
990 np->rx_ring[i].rx_status = 0;
991 np->rx_ring[i].addr = cpu_to_le32(0xBADF00D0);
992 if (np->rx_skbuff[i]) {
993 pci_unmap_single(np->pdev,
994 np->rx_skbuff_dma[i],
995 np->rx_buf_sz, PCI_DMA_FROMDEVICE);
996 dev_kfree_skb(np->rx_skbuff[i]);
997 }
998 np->rx_skbuff[i] = 0;
999 }
1000}
1001
1002static void alloc_tbufs(struct net_device* dev)
1003{
1004 struct netdev_private *np = dev->priv;
1005 dma_addr_t next;
1006 int i;
1007
1008 np->dirty_tx = np->cur_tx = 0;
1009 next = np->tx_ring_dma;
1010 for (i = 0; i < TX_RING_SIZE; i++) {
1011 np->tx_skbuff[i] = 0;
1012 np->tx_ring[i].tx_status = 0;
1013 np->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
1014 next += sizeof(struct tx_desc);
1015 np->tx_ring[i].next_desc = cpu_to_le32(next);
1016 np->tx_buf[i] = &np->tx_bufs[i * PKT_BUF_SZ];
1017 }
1018 np->tx_ring[i-1].next_desc = cpu_to_le32(np->tx_ring_dma);
1019
1020}
1021
1022static void free_tbufs(struct net_device* dev)
1023{
1024 struct netdev_private *np = dev->priv;
1025 int i;
1026
1027 for (i = 0; i < TX_RING_SIZE; i++) {
1028 np->tx_ring[i].tx_status = 0;
1029 np->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
1030 np->tx_ring[i].addr = cpu_to_le32(0xBADF00D0);
1031 if (np->tx_skbuff[i]) {
1032 if (np->tx_skbuff_dma[i]) {
1033 pci_unmap_single(np->pdev,
1034 np->tx_skbuff_dma[i],
1035 np->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
1036 }
1037 dev_kfree_skb(np->tx_skbuff[i]);
1038 }
1039 np->tx_skbuff[i] = 0;
1040 np->tx_buf[i] = 0;
1041 }
1042}
1043
1044static void init_registers(struct net_device *dev)
1045{
1046 struct netdev_private *np = dev->priv;
1047 long ioaddr = dev->base_addr;
1048 int i;
1049
1050 for (i = 0; i < 6; i++)
1051 writeb(dev->dev_addr[i], ioaddr + StationAddr + i);
1052
1053
1054 writew(0x0006, ioaddr + PCIBusConfig);
1055
1056 writeb(0x20, ioaddr + TxConfig);
1057 np->tx_thresh = 0x20;
1058 np->rx_thresh = 0x60;
1059 np->mii_if.full_duplex = 0;
1060
1061 if (dev->if_port == 0)
1062 dev->if_port = np->default_port;
1063
1064 writel(np->rx_ring_dma, ioaddr + RxRingPtr);
1065 writel(np->tx_ring_dma, ioaddr + TxRingPtr);
1066
1067 via_rhine_set_rx_mode(dev);
1068
1069
1070 writew(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow |
1071 IntrRxDropped | IntrRxNoBuf | IntrTxAborted |
1072 IntrTxDone | IntrTxError | IntrTxUnderrun |
1073 IntrPCIErr | IntrStatsMax | IntrLinkChange,
1074 ioaddr + IntrEnable);
1075
1076 np->chip_cmd = CmdStart|CmdTxOn|CmdRxOn|CmdNoTxPoll;
1077 if (np->mii_if.force_media)
1078 np->chip_cmd |= CmdFDuplex;
1079 writew(np->chip_cmd, ioaddr + ChipCmd);
1080
1081 via_rhine_check_duplex(dev);
1082
1083
1084
1085
1086 mdio_write(dev, np->phys[0], 0x17, mdio_read(dev, np->phys[0], 0x17) |
1087 (np->drv_flags & HasESIPhy) ? 0x0080 : 0x0001);
1088}
1089
1090
1091static int mdio_read(struct net_device *dev, int phy_id, int regnum)
1092{
1093 long ioaddr = dev->base_addr;
1094 int boguscnt = 1024;
1095
1096
1097 while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
1098 ;
1099 writeb(0x00, ioaddr + MIICmd);
1100 writeb(phy_id, ioaddr + MIIPhyAddr);
1101 writeb(regnum, ioaddr + MIIRegAddr);
1102 writeb(0x40, ioaddr + MIICmd);
1103 boguscnt = 1024;
1104 while ((readb(ioaddr + MIICmd) & 0x40) && --boguscnt > 0)
1105 ;
1106 return readw(ioaddr + MIIData);
1107}
1108
1109static void mdio_write(struct net_device *dev, int phy_id, int regnum, int value)
1110{
1111 struct netdev_private *np = dev->priv;
1112 long ioaddr = dev->base_addr;
1113 int boguscnt = 1024;
1114
1115 if (phy_id == np->phys[0]) {
1116 switch (regnum) {
1117 case MII_BMCR:
1118 if (value & 0x9000)
1119 np->mii_if.force_media = 0;
1120 else
1121 np->mii_if.full_duplex = (value & 0x0100) ? 1 : 0;
1122 break;
1123 case MII_ADVERTISE:
1124 np->mii_if.advertising = value;
1125 break;
1126 }
1127 }
1128
1129
1130 while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
1131 ;
1132 writeb(0x00, ioaddr + MIICmd);
1133 writeb(phy_id, ioaddr + MIIPhyAddr);
1134 writeb(regnum, ioaddr + MIIRegAddr);
1135 writew(value, ioaddr + MIIData);
1136 writeb(0x20, ioaddr + MIICmd);
1137}
1138
1139
1140static int via_rhine_open(struct net_device *dev)
1141{
1142 struct netdev_private *np = dev->priv;
1143 long ioaddr = dev->base_addr;
1144 int i;
1145
1146
1147 writew(CmdReset, ioaddr + ChipCmd);
1148
1149 i = request_irq(np->pdev->irq, &via_rhine_interrupt, SA_SHIRQ, dev->name, dev);
1150 if (i)
1151 return i;
1152
1153 if (debug > 1)
1154 printk(KERN_DEBUG "%s: via_rhine_open() irq %d.\n",
1155 dev->name, np->pdev->irq);
1156
1157 i = alloc_ring(dev);
1158 if (i)
1159 return i;
1160 alloc_rbufs(dev);
1161 alloc_tbufs(dev);
1162 wait_for_reset(dev, np->chip_id, dev->name);
1163 init_registers(dev);
1164 if (debug > 2)
1165 printk(KERN_DEBUG "%s: Done via_rhine_open(), status %4.4x "
1166 "MII status: %4.4x.\n",
1167 dev->name, readw(ioaddr + ChipCmd),
1168 mdio_read(dev, np->phys[0], MII_BMSR));
1169
1170 netif_start_queue(dev);
1171
1172
1173 init_timer(&np->timer);
1174 np->timer.expires = jiffies + 2 * HZ/100;
1175 np->timer.data = (unsigned long)dev;
1176 np->timer.function = &via_rhine_timer;
1177 add_timer(&np->timer);
1178
1179 return 0;
1180}
1181
1182static void via_rhine_check_duplex(struct net_device *dev)
1183{
1184 struct netdev_private *np = dev->priv;
1185 long ioaddr = dev->base_addr;
1186 int mii_lpa = mdio_read(dev, np->phys[0], MII_LPA);
1187 int negotiated = mii_lpa & np->mii_if.advertising;
1188 int duplex;
1189
1190 if (np->mii_if.force_media || mii_lpa == 0xffff)
1191 return;
1192 duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
1193 if (np->mii_if.full_duplex != duplex) {
1194 np->mii_if.full_duplex = duplex;
1195 if (debug)
1196 printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d link"
1197 " partner capability of %4.4x.\n", dev->name,
1198 duplex ? "full" : "half", np->phys[0], mii_lpa);
1199 if (duplex)
1200 np->chip_cmd |= CmdFDuplex;
1201 else
1202 np->chip_cmd &= ~CmdFDuplex;
1203 writew(np->chip_cmd, ioaddr + ChipCmd);
1204 }
1205}
1206
1207
1208static void via_rhine_timer(unsigned long data)
1209{
1210 struct net_device *dev = (struct net_device *)data;
1211 struct netdev_private *np = dev->priv;
1212 long ioaddr = dev->base_addr;
1213 int next_tick = 10*HZ;
1214 int mii_status;
1215
1216 if (debug > 3) {
1217 printk(KERN_DEBUG "%s: VIA Rhine monitor tick, status %4.4x.\n",
1218 dev->name, readw(ioaddr + IntrStatus));
1219 }
1220
1221 spin_lock_irq (&np->lock);
1222
1223 via_rhine_check_duplex(dev);
1224
1225
1226 mii_status = mdio_read(dev, np->phys[0], MII_BMSR);
1227 if ( (mii_status & BMSR_LSTATUS) != (np->mii_status & BMSR_LSTATUS) ) {
1228 if (mii_status & BMSR_LSTATUS)
1229 netif_carrier_on(dev);
1230 else
1231 netif_carrier_off(dev);
1232 }
1233 np->mii_status = mii_status;
1234
1235 spin_unlock_irq (&np->lock);
1236
1237 np->timer.expires = jiffies + next_tick;
1238 add_timer(&np->timer);
1239}
1240
1241
1242static void via_rhine_tx_timeout (struct net_device *dev)
1243{
1244 struct netdev_private *np = dev->priv;
1245 long ioaddr = dev->base_addr;
1246
1247 printk (KERN_WARNING "%s: Transmit timed out, status %4.4x, PHY status "
1248 "%4.4x, resetting...\n",
1249 dev->name, readw (ioaddr + IntrStatus),
1250 mdio_read (dev, np->phys[0], MII_BMSR));
1251
1252 dev->if_port = 0;
1253
1254
1255 disable_irq(np->pdev->irq);
1256
1257 spin_lock(&np->lock);
1258
1259
1260 writew(CmdReset, ioaddr + ChipCmd);
1261
1262
1263 free_tbufs(dev);
1264 free_rbufs(dev);
1265 alloc_tbufs(dev);
1266 alloc_rbufs(dev);
1267
1268
1269 wait_for_reset(dev, np->chip_id, dev->name);
1270 init_registers(dev);
1271
1272 spin_unlock(&np->lock);
1273 enable_irq(np->pdev->irq);
1274
1275 dev->trans_start = jiffies;
1276 np->stats.tx_errors++;
1277 netif_wake_queue(dev);
1278}
1279
1280static int via_rhine_start_tx(struct sk_buff *skb, struct net_device *dev)
1281{
1282 struct netdev_private *np = dev->priv;
1283 unsigned entry;
1284 u32 intr_status;
1285
1286
1287
1288
1289
1290 entry = np->cur_tx % TX_RING_SIZE;
1291
1292 if (skb->len < ETH_ZLEN) {
1293 skb = skb_padto(skb, ETH_ZLEN);
1294 if (skb == NULL)
1295 return 0;
1296 }
1297
1298 np->tx_skbuff[entry] = skb;
1299
1300 if ((np->drv_flags & ReqTxAlign) &&
1301 (((long)skb->data & 3) || skb_shinfo(skb)->nr_frags != 0 || skb->ip_summed == CHECKSUM_HW)
1302 ) {
1303
1304 if (skb->len > PKT_BUF_SZ) {
1305
1306 dev_kfree_skb(skb);
1307 np->tx_skbuff[entry] = NULL;
1308 np->stats.tx_dropped++;
1309 return 0;
1310 }
1311 skb_copy_and_csum_dev(skb, np->tx_buf[entry]);
1312 np->tx_skbuff_dma[entry] = 0;
1313 np->tx_ring[entry].addr = cpu_to_le32(np->tx_bufs_dma +
1314 (np->tx_buf[entry] - np->tx_bufs));
1315 } else {
1316 np->tx_skbuff_dma[entry] =
1317 pci_map_single(np->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1318 np->tx_ring[entry].addr = cpu_to_le32(np->tx_skbuff_dma[entry]);
1319 }
1320
1321 np->tx_ring[entry].desc_length =
1322 cpu_to_le32(TXDESC | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));
1323
1324
1325 spin_lock_irq (&np->lock);
1326 wmb();
1327 np->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
1328 wmb();
1329
1330 np->cur_tx++;
1331
1332
1333
1334
1335
1336
1337
1338 intr_status = get_intr_status(dev);
1339 if ((intr_status & IntrTxErrSummary) == 0) {
1340 writew(CmdTxDemand | np->chip_cmd, dev->base_addr + ChipCmd);
1341 }
1342 IOSYNC;
1343
1344 if (np->cur_tx == np->dirty_tx + TX_QUEUE_LEN)
1345 netif_stop_queue(dev);
1346
1347 dev->trans_start = jiffies;
1348
1349 spin_unlock_irq (&np->lock);
1350
1351 if (debug > 4) {
1352 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
1353 dev->name, np->cur_tx-1, entry);
1354 }
1355 return 0;
1356}
1357
1358
1359
1360static irqreturn_t via_rhine_interrupt(int irq, void *dev_instance, struct pt_regs *rgs)
1361{
1362 struct net_device *dev = dev_instance;
1363 long ioaddr;
1364 u32 intr_status;
1365 int boguscnt = max_interrupt_work;
1366 int handled = 0;
1367
1368 ioaddr = dev->base_addr;
1369
1370 while ((intr_status = get_intr_status(dev))) {
1371 handled = 1;
1372
1373
1374 if (intr_status & IntrTxDescRace)
1375 writeb(0x08, ioaddr + IntrStatus2);
1376 writew(intr_status & 0xffff, ioaddr + IntrStatus);
1377 IOSYNC;
1378
1379 if (debug > 4)
1380 printk(KERN_DEBUG "%s: Interrupt, status %8.8x.\n",
1381 dev->name, intr_status);
1382
1383 if (intr_status & (IntrRxDone | IntrRxErr | IntrRxDropped |
1384 IntrRxWakeUp | IntrRxEmpty | IntrRxNoBuf))
1385 via_rhine_rx(dev);
1386
1387 if (intr_status & (IntrTxErrSummary | IntrTxDone)) {
1388 if (intr_status & IntrTxErrSummary) {
1389 int cnt = 20;
1390
1391 while ((readw(ioaddr+ChipCmd) & CmdTxOn) && --cnt)
1392 udelay(5);
1393 if (debug > 2 && !cnt)
1394 printk(KERN_WARNING "%s: via_rhine_interrupt() "
1395 "Tx engine still on.\n",
1396 dev->name);
1397 }
1398 via_rhine_tx(dev);
1399 }
1400
1401
1402 if (intr_status & (IntrPCIErr | IntrLinkChange |
1403 IntrStatsMax | IntrTxError | IntrTxAborted |
1404 IntrTxUnderrun | IntrTxDescRace))
1405 via_rhine_error(dev, intr_status);
1406
1407 if (--boguscnt < 0) {
1408 printk(KERN_WARNING "%s: Too much work at interrupt, "
1409 "status=%#8.8x.\n",
1410 dev->name, intr_status);
1411 break;
1412 }
1413 }
1414
1415 if (debug > 3)
1416 printk(KERN_DEBUG "%s: exiting interrupt, status=%8.8x.\n",
1417 dev->name, readw(ioaddr + IntrStatus));
1418 return IRQ_RETVAL(handled);
1419}
1420
1421
1422
1423static void via_rhine_tx(struct net_device *dev)
1424{
1425 struct netdev_private *np = dev->priv;
1426 int txstatus = 0, entry = np->dirty_tx % TX_RING_SIZE;
1427
1428 spin_lock (&np->lock);
1429
1430
1431 while (np->dirty_tx != np->cur_tx) {
1432 txstatus = le32_to_cpu(np->tx_ring[entry].tx_status);
1433 if (debug > 6)
1434 printk(KERN_DEBUG " Tx scavenge %d status %8.8x.\n",
1435 entry, txstatus);
1436 if (txstatus & DescOwn)
1437 break;
1438 if (txstatus & 0x8000) {
1439 if (debug > 1)
1440 printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
1441 dev->name, txstatus);
1442 np->stats.tx_errors++;
1443 if (txstatus & 0x0400) np->stats.tx_carrier_errors++;
1444 if (txstatus & 0x0200) np->stats.tx_window_errors++;
1445 if (txstatus & 0x0100) np->stats.tx_aborted_errors++;
1446 if (txstatus & 0x0080) np->stats.tx_heartbeat_errors++;
1447 if (((np->chip_id == VT86C100A) && txstatus & 0x0002) ||
1448 (txstatus & 0x0800) || (txstatus & 0x1000)) {
1449 np->stats.tx_fifo_errors++;
1450 np->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
1451 break;
1452 }
1453
1454 } else {
1455 if (np->chip_id == VT86C100A)
1456 np->stats.collisions += (txstatus >> 3) & 0x0F;
1457 else
1458 np->stats.collisions += txstatus & 0x0F;
1459 if (debug > 6)
1460 printk(KERN_DEBUG "collisions: %1.1x:%1.1x\n",
1461 (txstatus >> 3) & 0xF,
1462 txstatus & 0xF);
1463 np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1464 np->stats.tx_packets++;
1465 }
1466
1467 if (np->tx_skbuff_dma[entry]) {
1468 pci_unmap_single(np->pdev,
1469 np->tx_skbuff_dma[entry],
1470 np->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
1471 }
1472 dev_kfree_skb_irq(np->tx_skbuff[entry]);
1473 np->tx_skbuff[entry] = NULL;
1474 entry = (++np->dirty_tx) % TX_RING_SIZE;
1475 }
1476 if ((np->cur_tx - np->dirty_tx) < TX_QUEUE_LEN - 4)
1477 netif_wake_queue (dev);
1478
1479 spin_unlock (&np->lock);
1480}
1481
1482
1483
1484static void via_rhine_rx(struct net_device *dev)
1485{
1486 struct netdev_private *np = dev->priv;
1487 int entry = np->cur_rx % RX_RING_SIZE;
1488 int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
1489
1490 if (debug > 4) {
1491 printk(KERN_DEBUG "%s: via_rhine_rx(), entry %d status %8.8x.\n",
1492 dev->name, entry, le32_to_cpu(np->rx_head_desc->rx_status));
1493 }
1494
1495
1496 while ( ! (np->rx_head_desc->rx_status & cpu_to_le32(DescOwn))) {
1497 struct rx_desc *desc = np->rx_head_desc;
1498 u32 desc_status = le32_to_cpu(desc->rx_status);
1499 int data_size = desc_status >> 16;
1500
1501 if (debug > 4)
1502 printk(KERN_DEBUG " via_rhine_rx() status is %8.8x.\n",
1503 desc_status);
1504 if (--boguscnt < 0)
1505 break;
1506 if ( (desc_status & (RxWholePkt | RxErr)) != RxWholePkt) {
1507 if ((desc_status & RxWholePkt) != RxWholePkt) {
1508 printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
1509 "multiple buffers, entry %#x length %d status %8.8x!\n",
1510 dev->name, entry, data_size, desc_status);
1511 printk(KERN_WARNING "%s: Oversized Ethernet frame %p vs %p.\n",
1512 dev->name, np->rx_head_desc, &np->rx_ring[entry]);
1513 np->stats.rx_length_errors++;
1514 } else if (desc_status & RxErr) {
1515
1516 if (debug > 2)
1517 printk(KERN_DEBUG " via_rhine_rx() Rx error was %8.8x.\n",
1518 desc_status);
1519 np->stats.rx_errors++;
1520 if (desc_status & 0x0030) np->stats.rx_length_errors++;
1521 if (desc_status & 0x0048) np->stats.rx_fifo_errors++;
1522 if (desc_status & 0x0004) np->stats.rx_frame_errors++;
1523 if (desc_status & 0x0002) {
1524
1525 spin_lock (&np->lock);
1526 np->stats.rx_crc_errors++;
1527 spin_unlock (&np->lock);
1528 }
1529 }
1530 } else {
1531 struct sk_buff *skb;
1532
1533 int pkt_len = data_size - 4;
1534
1535
1536
1537 if (pkt_len < rx_copybreak &&
1538 (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1539 skb->dev = dev;
1540 skb_reserve(skb, 2);
1541 pci_dma_sync_single(np->pdev, np->rx_skbuff_dma[entry],
1542 np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1543
1544
1545
1546
1547#if HAS_IP_COPYSUM
1548 eth_copy_and_sum(skb, np->rx_skbuff[entry]->tail, pkt_len, 0);
1549 skb_put(skb, pkt_len);
1550#else
1551 memcpy(skb_put(skb, pkt_len), np->rx_skbuff[entry]->tail,
1552 pkt_len);
1553#endif
1554 } else {
1555 skb = np->rx_skbuff[entry];
1556 if (skb == NULL) {
1557 printk(KERN_ERR "%s: Inconsistent Rx descriptor chain.\n",
1558 dev->name);
1559 break;
1560 }
1561 np->rx_skbuff[entry] = NULL;
1562 skb_put(skb, pkt_len);
1563 pci_unmap_single(np->pdev, np->rx_skbuff_dma[entry],
1564 np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1565 }
1566 skb->protocol = eth_type_trans(skb, dev);
1567 netif_rx(skb);
1568 dev->last_rx = jiffies;
1569 np->stats.rx_bytes += pkt_len;
1570 np->stats.rx_packets++;
1571 }
1572 entry = (++np->cur_rx) % RX_RING_SIZE;
1573 np->rx_head_desc = &np->rx_ring[entry];
1574 }
1575
1576
1577 for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1578 struct sk_buff *skb;
1579 entry = np->dirty_rx % RX_RING_SIZE;
1580 if (np->rx_skbuff[entry] == NULL) {
1581 skb = dev_alloc_skb(np->rx_buf_sz);
1582 np->rx_skbuff[entry] = skb;
1583 if (skb == NULL)
1584 break;
1585 skb->dev = dev;
1586 np->rx_skbuff_dma[entry] =
1587 pci_map_single(np->pdev, skb->tail, np->rx_buf_sz,
1588 PCI_DMA_FROMDEVICE);
1589 np->rx_ring[entry].addr = cpu_to_le32(np->rx_skbuff_dma[entry]);
1590 }
1591 np->rx_ring[entry].rx_status = cpu_to_le32(DescOwn);
1592 }
1593
1594
1595 writew(readw(dev->base_addr + ChipCmd) | CmdRxOn | CmdRxDemand,
1596 dev->base_addr + ChipCmd);
1597}
1598
1599
1600
1601
1602
1603static inline void clear_tally_counters(const long ioaddr)
1604{
1605 writel(0, ioaddr + RxMissed);
1606 readw(ioaddr + RxCRCErrs);
1607 readw(ioaddr + RxMissed);
1608}
1609
1610static void via_rhine_restart_tx(struct net_device *dev) {
1611 struct netdev_private *np = dev->priv;
1612 long ioaddr = dev->base_addr;
1613 int entry = np->dirty_tx % TX_RING_SIZE;
1614 u32 intr_status;
1615
1616
1617
1618
1619
1620 intr_status = get_intr_status(dev);
1621
1622 if ((intr_status & IntrTxErrSummary) == 0) {
1623
1624
1625 writel(np->tx_ring_dma + entry * sizeof(struct tx_desc),
1626 ioaddr + TxRingPtr);
1627
1628 writew(CmdTxDemand | np->chip_cmd, ioaddr + ChipCmd);
1629 IOSYNC;
1630 }
1631 else {
1632
1633 if (debug > 1)
1634 printk(KERN_WARNING "%s: via_rhine_restart_tx() "
1635 "Another error occured %8.8x.\n",
1636 dev->name, intr_status);
1637 }
1638
1639}
1640
1641static void via_rhine_error(struct net_device *dev, int intr_status)
1642{
1643 struct netdev_private *np = dev->priv;
1644 long ioaddr = dev->base_addr;
1645
1646 spin_lock (&np->lock);
1647
1648 if (intr_status & (IntrLinkChange)) {
1649 if (readb(ioaddr + MIIStatus) & 0x02) {
1650
1651 if (np->drv_flags & HasDavicomPhy)
1652 mdio_write(dev, np->phys[0], MII_BMCR, 0x3300);
1653 } else
1654 via_rhine_check_duplex(dev);
1655 if (debug)
1656 printk(KERN_ERR "%s: MII status changed: Autonegotiation "
1657 "advertising %4.4x partner %4.4x.\n", dev->name,
1658 mdio_read(dev, np->phys[0], MII_ADVERTISE),
1659 mdio_read(dev, np->phys[0], MII_LPA));
1660 }
1661 if (intr_status & IntrStatsMax) {
1662 np->stats.rx_crc_errors += readw(ioaddr + RxCRCErrs);
1663 np->stats.rx_missed_errors += readw(ioaddr + RxMissed);
1664 clear_tally_counters(ioaddr);
1665 }
1666 if (intr_status & IntrTxAborted) {
1667 if (debug > 1)
1668 printk(KERN_INFO "%s: Abort %8.8x, frame dropped.\n",
1669 dev->name, intr_status);
1670 }
1671 if (intr_status & IntrTxUnderrun) {
1672 if (np->tx_thresh < 0xE0)
1673 writeb(np->tx_thresh += 0x20, ioaddr + TxConfig);
1674 if (debug > 1)
1675 printk(KERN_INFO "%s: Transmitter underrun, Tx "
1676 "threshold now %2.2x.\n",
1677 dev->name, np->tx_thresh);
1678 }
1679 if (intr_status & IntrTxDescRace) {
1680 if (debug > 2)
1681 printk(KERN_INFO "%s: Tx descriptor write-back race.\n",
1682 dev->name);
1683 }
1684 if ((intr_status & IntrTxError) && ~( IntrTxAborted | IntrTxUnderrun |
1685 IntrTxDescRace )) {
1686 if (np->tx_thresh < 0xE0) {
1687 writeb(np->tx_thresh += 0x20, ioaddr + TxConfig);
1688 }
1689 if (debug > 1)
1690 printk(KERN_INFO "%s: Unspecified error. Tx "
1691 "threshold now %2.2x.\n",
1692 dev->name, np->tx_thresh);
1693 }
1694 if (intr_status & ( IntrTxAborted | IntrTxUnderrun | IntrTxDescRace |
1695 IntrTxError ))
1696 via_rhine_restart_tx(dev);
1697
1698 if (intr_status & ~( IntrLinkChange | IntrStatsMax | IntrTxUnderrun |
1699 IntrTxError | IntrTxAborted | IntrNormalSummary |
1700 IntrTxDescRace )) {
1701 if (debug > 1)
1702 printk(KERN_ERR "%s: Something Wicked happened! %8.8x.\n",
1703 dev->name, intr_status);
1704 }
1705
1706 spin_unlock (&np->lock);
1707}
1708
1709static struct net_device_stats *via_rhine_get_stats(struct net_device *dev)
1710{
1711 struct netdev_private *np = dev->priv;
1712 long ioaddr = dev->base_addr;
1713 unsigned long flags;
1714
1715 spin_lock_irqsave(&np->lock, flags);
1716 np->stats.rx_crc_errors += readw(ioaddr + RxCRCErrs);
1717 np->stats.rx_missed_errors += readw(ioaddr + RxMissed);
1718 clear_tally_counters(ioaddr);
1719 spin_unlock_irqrestore(&np->lock, flags);
1720
1721 return &np->stats;
1722}
1723
1724static void via_rhine_set_rx_mode(struct net_device *dev)
1725{
1726 struct netdev_private *np = dev->priv;
1727 long ioaddr = dev->base_addr;
1728 u32 mc_filter[2];
1729 u8 rx_mode;
1730
1731 if (dev->flags & IFF_PROMISC) {
1732
1733 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
1734 rx_mode = 0x1C;
1735 writel(0xffffffff, ioaddr + MulticastFilter0);
1736 writel(0xffffffff, ioaddr + MulticastFilter1);
1737 } else if ((dev->mc_count > multicast_filter_limit)
1738 || (dev->flags & IFF_ALLMULTI)) {
1739
1740 writel(0xffffffff, ioaddr + MulticastFilter0);
1741 writel(0xffffffff, ioaddr + MulticastFilter1);
1742 rx_mode = 0x0C;
1743 } else {
1744 struct dev_mc_list *mclist;
1745 int i;
1746 memset(mc_filter, 0, sizeof(mc_filter));
1747 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1748 i++, mclist = mclist->next) {
1749 int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
1750
1751 mc_filter[bit_nr >> 5] |= cpu_to_le32(1 << (bit_nr & 31));
1752 }
1753 writel(mc_filter[0], ioaddr + MulticastFilter0);
1754 writel(mc_filter[1], ioaddr + MulticastFilter1);
1755 rx_mode = 0x0C;
1756 }
1757 writeb(np->rx_thresh | rx_mode, ioaddr + RxConfig);
1758}
1759
1760static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1761{
1762 struct netdev_private *np = dev->priv;
1763
1764 strcpy (info->driver, DRV_NAME);
1765 strcpy (info->version, DRV_VERSION);
1766 strcpy (info->bus_info, pci_name(np->pdev));
1767}
1768
1769static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1770{
1771 struct netdev_private *np = dev->priv;
1772 int rc;
1773
1774 if (!(np->drv_flags & CanHaveMII))
1775 return -EINVAL;
1776
1777 spin_lock_irq(&np->lock);
1778 rc = mii_ethtool_gset(&np->mii_if, cmd);
1779 spin_unlock_irq(&np->lock);
1780
1781 return rc;
1782}
1783
1784static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1785{
1786 struct netdev_private *np = dev->priv;
1787 int rc;
1788
1789 if (!(np->drv_flags & CanHaveMII))
1790 return -EINVAL;
1791
1792 spin_lock_irq(&np->lock);
1793 rc = mii_ethtool_sset(&np->mii_if, cmd);
1794 spin_unlock_irq(&np->lock);
1795
1796 return rc;
1797}
1798
1799static int netdev_nway_reset(struct net_device *dev)
1800{
1801 struct netdev_private *np = dev->priv;
1802
1803 if (!(np->drv_flags & CanHaveMII))
1804 return -EINVAL;
1805
1806 return mii_nway_restart(&np->mii_if);
1807}
1808
1809static u32 netdev_get_link(struct net_device *dev)
1810{
1811 struct netdev_private *np = dev->priv;
1812
1813 if (!(np->drv_flags & CanHaveMII))
1814 return 0;
1815
1816 return mii_link_ok(&np->mii_if);
1817}
1818
1819static u32 netdev_get_msglevel(struct net_device *dev)
1820{
1821 return debug;
1822}
1823
1824static void netdev_set_msglevel(struct net_device *dev, u32 value)
1825{
1826 debug = value;
1827}
1828
1829static struct ethtool_ops netdev_ethtool_ops = {
1830 .get_drvinfo = netdev_get_drvinfo,
1831 .get_settings = netdev_get_settings,
1832 .set_settings = netdev_set_settings,
1833 .nway_reset = netdev_nway_reset,
1834 .get_link = netdev_get_link,
1835 .get_msglevel = netdev_get_msglevel,
1836 .set_msglevel = netdev_set_msglevel,
1837 .get_sg = ethtool_op_get_sg,
1838 .get_tx_csum = ethtool_op_get_tx_csum,
1839};
1840
1841static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1842{
1843 struct netdev_private *np = dev->priv;
1844 struct mii_ioctl_data *data = (struct mii_ioctl_data *) & rq->ifr_data;
1845 int rc;
1846
1847 if (!netif_running(dev))
1848 return -EINVAL;
1849
1850 spin_lock_irq(&np->lock);
1851 rc = generic_mii_ioctl(&np->mii_if, data, cmd, NULL);
1852 spin_unlock_irq(&np->lock);
1853
1854 return rc;
1855}
1856
1857static int via_rhine_close(struct net_device *dev)
1858{
1859 long ioaddr = dev->base_addr;
1860 struct netdev_private *np = dev->priv;
1861
1862 del_timer_sync(&np->timer);
1863
1864 spin_lock_irq(&np->lock);
1865
1866 netif_stop_queue(dev);
1867
1868 if (debug > 1)
1869 printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x.\n",
1870 dev->name, readw(ioaddr + ChipCmd));
1871
1872
1873 writeb(np->tx_thresh | 0x02, ioaddr + TxConfig);
1874
1875
1876 writew(0x0000, ioaddr + IntrEnable);
1877
1878
1879 writew(CmdStop, ioaddr + ChipCmd);
1880
1881 spin_unlock_irq(&np->lock);
1882
1883 free_irq(np->pdev->irq, dev);
1884 free_rbufs(dev);
1885 free_tbufs(dev);
1886 free_ring(dev);
1887
1888 return 0;
1889}
1890
1891
1892static void __devexit via_rhine_remove_one (struct pci_dev *pdev)
1893{
1894 struct net_device *dev = pci_get_drvdata(pdev);
1895
1896 unregister_netdev(dev);
1897
1898 pci_release_regions(pdev);
1899
1900#ifdef USE_MEM
1901 iounmap((char *)(dev->base_addr));
1902#endif
1903
1904 free_netdev(dev);
1905 pci_disable_device(pdev);
1906 pci_set_drvdata(pdev, NULL);
1907}
1908
1909
1910static struct pci_driver via_rhine_driver = {
1911 .name = "via-rhine",
1912 .id_table = via_rhine_pci_tbl,
1913 .probe = via_rhine_init_one,
1914 .remove = __devexit_p(via_rhine_remove_one),
1915};
1916
1917
1918static int __init via_rhine_init (void)
1919{
1920
1921#ifdef MODULE
1922 printk(version);
1923#endif
1924 return pci_module_init (&via_rhine_driver);
1925}
1926
1927
1928static void __exit via_rhine_cleanup (void)
1929{
1930 pci_unregister_driver (&via_rhine_driver);
1931}
1932
1933
1934module_init(via_rhine_init);
1935module_exit(via_rhine_cleanup);
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946