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
131
132
133
134
135
136
137
138
139
140
141
142#if !defined(__OPTIMIZE__)
143#warning You must compile this file with the correct options!
144#warning See the last lines of the source file.
145#error You must compile this driver with "-O".
146#endif
147
148#include <linux/config.h>
149#include <linux/module.h>
150#include <linux/kernel.h>
151#include <linux/string.h>
152#include <linux/timer.h>
153#include <linux/errno.h>
154#include <linux/ioport.h>
155#include <linux/slab.h>
156#include <linux/interrupt.h>
157#include <linux/pci.h>
158#include <linux/netdevice.h>
159#include <linux/etherdevice.h>
160#include <linux/skbuff.h>
161#include <linux/init.h>
162#include <linux/spinlock.h>
163#include <linux/ethtool.h>
164#include <linux/delay.h>
165#include <linux/rtnetlink.h>
166#include <linux/mii.h>
167#include <linux/crc32.h>
168#include <asm/processor.h>
169#include <asm/bitops.h>
170#include <asm/io.h>
171#include <asm/irq.h>
172#include <asm/uaccess.h>
173
174#define DRV_NAME "natsemi"
175#define DRV_VERSION "1.07+LK1.0.17"
176#define DRV_RELDATE "Sep 27, 2002"
177
178
179
180
181
182
183#define NATSEMI_DEF_MSG (NETIF_MSG_DRV | \
184 NETIF_MSG_LINK | \
185 NETIF_MSG_WOL | \
186 NETIF_MSG_RX_ERR | \
187 NETIF_MSG_TX_ERR)
188static int debug = -1;
189
190
191static int max_interrupt_work = 20;
192static int mtu;
193
194
195
196static int multicast_filter_limit = 100;
197
198
199
200static int rx_copybreak;
201
202
203
204
205
206
207#define MAX_UNITS 8
208static int options[MAX_UNITS];
209static int full_duplex[MAX_UNITS];
210
211
212
213
214
215
216
217
218#define TX_RING_SIZE 16
219#define TX_QUEUE_LEN 10
220#define RX_RING_SIZE 32
221
222
223
224#define TX_TIMEOUT (2*HZ)
225
226#define NATSEMI_HW_TIMEOUT 400
227#define NATSEMI_TIMER_FREQ 3*HZ
228#define NATSEMI_PG0_NREGS 64
229#define NATSEMI_RFDR_NREGS 8
230#define NATSEMI_PG1_NREGS 4
231#define NATSEMI_NREGS (NATSEMI_PG0_NREGS + NATSEMI_RFDR_NREGS + \
232 NATSEMI_PG1_NREGS)
233#define NATSEMI_REGS_VER 1
234#define NATSEMI_REGS_SIZE (NATSEMI_NREGS * sizeof(u32))
235#define NATSEMI_EEPROM_SIZE 24
236
237#define PKT_BUF_SZ 1536
238
239
240static char version[] __devinitdata =
241 KERN_INFO DRV_NAME " dp8381x driver, version "
242 DRV_VERSION ", " DRV_RELDATE "\n"
243 KERN_INFO " originally by Donald Becker <becker@scyld.com>\n"
244 KERN_INFO " http://www.scyld.com/network/natsemi.html\n"
245 KERN_INFO " 2.4.x kernel port by Jeff Garzik, Tjeerd Mulder\n";
246
247MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
248MODULE_DESCRIPTION("National Semiconductor DP8381x series PCI Ethernet driver");
249MODULE_LICENSE("GPL");
250
251MODULE_PARM(max_interrupt_work, "i");
252MODULE_PARM(mtu, "i");
253MODULE_PARM(debug, "i");
254MODULE_PARM(rx_copybreak, "i");
255MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
256MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
257MODULE_PARM_DESC(max_interrupt_work,
258 "DP8381x maximum events handled per interrupt");
259MODULE_PARM_DESC(mtu, "DP8381x MTU (all boards)");
260MODULE_PARM_DESC(debug, "DP8381x default debug level");
261MODULE_PARM_DESC(rx_copybreak,
262 "DP8381x copy breakpoint for copy-only-tiny-frames");
263MODULE_PARM_DESC(options,
264 "DP8381x: Bits 0-3: media type, bit 17: full duplex");
265MODULE_PARM_DESC(full_duplex, "DP8381x full duplex setting(s) (1)");
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
349enum pcistuff {
350 PCI_USES_IO = 0x01,
351 PCI_USES_MEM = 0x02,
352 PCI_USES_MASTER = 0x04,
353 PCI_ADDR0 = 0x08,
354 PCI_ADDR1 = 0x10,
355};
356
357
358#define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_MEM | PCI_ADDR1)
359
360
361
362static struct {
363 const char *name;
364 unsigned long flags;
365} natsemi_pci_info[] __devinitdata = {
366 { "NatSemi DP8381[56]", PCI_IOTYPE },
367};
368
369static struct pci_device_id natsemi_pci_tbl[] = {
370 { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_83815, PCI_ANY_ID, PCI_ANY_ID, },
371 { 0, },
372};
373MODULE_DEVICE_TABLE(pci, natsemi_pci_tbl);
374
375
376
377
378
379
380enum register_offsets {
381 ChipCmd = 0x00,
382 ChipConfig = 0x04,
383 EECtrl = 0x08,
384 PCIBusCfg = 0x0C,
385 IntrStatus = 0x10,
386 IntrMask = 0x14,
387 IntrEnable = 0x18,
388 IntrHoldoff = 0x16,
389 TxRingPtr = 0x20,
390 TxConfig = 0x24,
391 RxRingPtr = 0x30,
392 RxConfig = 0x34,
393 ClkRun = 0x3C,
394 WOLCmd = 0x40,
395 PauseCmd = 0x44,
396 RxFilterAddr = 0x48,
397 RxFilterData = 0x4C,
398 BootRomAddr = 0x50,
399 BootRomData = 0x54,
400 SiliconRev = 0x58,
401 StatsCtrl = 0x5C,
402 StatsData = 0x60,
403 RxPktErrs = 0x60,
404 RxMissed = 0x68,
405 RxCRCErrs = 0x64,
406 BasicControl = 0x80,
407 BasicStatus = 0x84,
408 AnegAdv = 0x90,
409 AnegPeer = 0x94,
410 PhyStatus = 0xC0,
411 MIntrCtrl = 0xC4,
412 MIntrStatus = 0xC8,
413 PhyCtrl = 0xE4,
414
415
416
417 PGSEL = 0xCC,
418 PMDCSR = 0xE4,
419 TSTDAT = 0xFC,
420 DSPCFG = 0xF4,
421 SDCFG = 0xF8
422};
423
424#define PMDCSR_VAL 0x189c
425#define TSTDAT_VAL 0x0
426#define DSPCFG_VAL 0x5040
427#define SDCFG_VAL 0x008c
428#define DSPCFG_LOCK 0x20
429#define TSTDAT_FIXED 0xe8
430
431
432enum pci_register_offsets {
433 PCIPM = 0x44,
434};
435
436enum ChipCmd_bits {
437 ChipReset = 0x100,
438 RxReset = 0x20,
439 TxReset = 0x10,
440 RxOff = 0x08,
441 RxOn = 0x04,
442 TxOff = 0x02,
443 TxOn = 0x01,
444};
445
446enum ChipConfig_bits {
447 CfgPhyDis = 0x200,
448 CfgPhyRst = 0x400,
449 CfgExtPhy = 0x1000,
450 CfgAnegEnable = 0x2000,
451 CfgAneg100 = 0x4000,
452 CfgAnegFull = 0x8000,
453 CfgAnegDone = 0x8000000,
454 CfgFullDuplex = 0x20000000,
455 CfgSpeed100 = 0x40000000,
456 CfgLink = 0x80000000,
457};
458
459enum EECtrl_bits {
460 EE_ShiftClk = 0x04,
461 EE_DataIn = 0x01,
462 EE_ChipSelect = 0x08,
463 EE_DataOut = 0x02,
464};
465
466enum PCIBusCfg_bits {
467 EepromReload = 0x4,
468};
469
470
471enum IntrStatus_bits {
472 IntrRxDone = 0x0001,
473 IntrRxIntr = 0x0002,
474 IntrRxErr = 0x0004,
475 IntrRxEarly = 0x0008,
476 IntrRxIdle = 0x0010,
477 IntrRxOverrun = 0x0020,
478 IntrTxDone = 0x0040,
479 IntrTxIntr = 0x0080,
480 IntrTxErr = 0x0100,
481 IntrTxIdle = 0x0200,
482 IntrTxUnderrun = 0x0400,
483 StatsMax = 0x0800,
484 SWInt = 0x1000,
485 WOLPkt = 0x2000,
486 LinkChange = 0x4000,
487 IntrHighBits = 0x8000,
488 RxStatusFIFOOver = 0x10000,
489 IntrPCIErr = 0xf00000,
490 RxResetDone = 0x1000000,
491 TxResetDone = 0x2000000,
492 IntrAbnormalSummary = 0xCD20,
493};
494
495
496
497
498
499
500
501
502
503
504#define DEFAULT_INTR 0x00f1cd65
505
506enum TxConfig_bits {
507 TxDrthMask = 0x3f,
508 TxFlthMask = 0x3f00,
509 TxMxdmaMask = 0x700000,
510 TxMxdma_512 = 0x0,
511 TxMxdma_4 = 0x100000,
512 TxMxdma_8 = 0x200000,
513 TxMxdma_16 = 0x300000,
514 TxMxdma_32 = 0x400000,
515 TxMxdma_64 = 0x500000,
516 TxMxdma_128 = 0x600000,
517 TxMxdma_256 = 0x700000,
518 TxCollRetry = 0x800000,
519 TxAutoPad = 0x10000000,
520 TxMacLoop = 0x20000000,
521 TxHeartIgn = 0x40000000,
522 TxCarrierIgn = 0x80000000
523};
524
525enum RxConfig_bits {
526 RxDrthMask = 0x3e,
527 RxMxdmaMask = 0x700000,
528 RxMxdma_512 = 0x0,
529 RxMxdma_4 = 0x100000,
530 RxMxdma_8 = 0x200000,
531 RxMxdma_16 = 0x300000,
532 RxMxdma_32 = 0x400000,
533 RxMxdma_64 = 0x500000,
534 RxMxdma_128 = 0x600000,
535 RxMxdma_256 = 0x700000,
536 RxAcceptLong = 0x8000000,
537 RxAcceptTx = 0x10000000,
538 RxAcceptRunt = 0x40000000,
539 RxAcceptErr = 0x80000000
540};
541
542enum ClkRun_bits {
543 PMEEnable = 0x100,
544 PMEStatus = 0x8000,
545};
546
547enum WolCmd_bits {
548 WakePhy = 0x1,
549 WakeUnicast = 0x2,
550 WakeMulticast = 0x4,
551 WakeBroadcast = 0x8,
552 WakeArp = 0x10,
553 WakePMatch0 = 0x20,
554 WakePMatch1 = 0x40,
555 WakePMatch2 = 0x80,
556 WakePMatch3 = 0x100,
557 WakeMagic = 0x200,
558 WakeMagicSecure = 0x400,
559 SecureHack = 0x100000,
560 WokePhy = 0x400000,
561 WokeUnicast = 0x800000,
562 WokeMulticast = 0x1000000,
563 WokeBroadcast = 0x2000000,
564 WokeArp = 0x4000000,
565 WokePMatch0 = 0x8000000,
566 WokePMatch1 = 0x10000000,
567 WokePMatch2 = 0x20000000,
568 WokePMatch3 = 0x40000000,
569 WokeMagic = 0x80000000,
570 WakeOptsSummary = 0x7ff
571};
572
573enum RxFilterAddr_bits {
574 RFCRAddressMask = 0x3ff,
575 AcceptMulticast = 0x00200000,
576 AcceptMyPhys = 0x08000000,
577 AcceptAllPhys = 0x10000000,
578 AcceptAllMulticast = 0x20000000,
579 AcceptBroadcast = 0x40000000,
580 RxFilterEnable = 0x80000000
581};
582
583enum StatsCtrl_bits {
584 StatsWarn = 0x1,
585 StatsFreeze = 0x2,
586 StatsClear = 0x4,
587 StatsStrobe = 0x8,
588};
589
590enum MIntrCtrl_bits {
591 MICRIntEn = 0x2,
592};
593
594enum PhyCtrl_bits {
595 PhyAddrMask = 0xf,
596};
597
598
599#define SRR_DP83815_C 0x0302
600#define SRR_DP83815_D 0x0403
601#define SRR_DP83816_A4 0x0504
602#define SRR_DP83816_A5 0x0505
603
604
605
606
607struct netdev_desc {
608 u32 next_desc;
609 s32 cmd_status;
610 u32 addr;
611 u32 software_use;
612};
613
614
615enum desc_status_bits {
616 DescOwn=0x80000000, DescMore=0x40000000, DescIntr=0x20000000,
617 DescNoCRC=0x10000000, DescPktOK=0x08000000,
618 DescSizeMask=0xfff,
619
620 DescTxAbort=0x04000000, DescTxFIFO=0x02000000,
621 DescTxCarrier=0x01000000, DescTxDefer=0x00800000,
622 DescTxExcDefer=0x00400000, DescTxOOWCol=0x00200000,
623 DescTxExcColl=0x00100000, DescTxCollCount=0x000f0000,
624
625 DescRxAbort=0x04000000, DescRxOver=0x02000000,
626 DescRxDest=0x01800000, DescRxLong=0x00400000,
627 DescRxRunt=0x00200000, DescRxInvalid=0x00100000,
628 DescRxCRC=0x00080000, DescRxAlign=0x00040000,
629 DescRxLoop=0x00020000, DesRxColl=0x00010000,
630};
631
632struct netdev_private {
633
634 dma_addr_t ring_dma;
635 struct netdev_desc *rx_ring;
636 struct netdev_desc *tx_ring;
637
638 struct sk_buff *rx_skbuff[RX_RING_SIZE];
639 dma_addr_t rx_dma[RX_RING_SIZE];
640
641 struct sk_buff *tx_skbuff[TX_RING_SIZE];
642 dma_addr_t tx_dma[TX_RING_SIZE];
643 struct net_device_stats stats;
644
645 struct timer_list timer;
646
647 struct pci_dev *pci_dev;
648 struct netdev_desc *rx_head_desc;
649
650 unsigned int cur_rx, dirty_rx;
651 unsigned int cur_tx, dirty_tx;
652
653 unsigned int rx_buf_sz;
654 int oom;
655
656 int hands_off;
657
658 unsigned int full_duplex;
659
660 u32 cur_rx_mode;
661 u32 rx_filter[16];
662
663 u32 tx_config, rx_config;
664
665 u32 SavedClkRun;
666
667 u32 srr;
668
669 u16 dspcfg;
670
671 u16 advertising;
672 unsigned int iosize;
673 spinlock_t lock;
674 u32 msg_enable;
675};
676
677static int eeprom_read(long ioaddr, int location);
678static int mdio_read(struct net_device *dev, int phy_id, int reg);
679static void mdio_write(struct net_device *dev, int phy_id, int reg, u16 data);
680static void natsemi_reset(struct net_device *dev);
681static void natsemi_reload_eeprom(struct net_device *dev);
682static void natsemi_stop_rxtx(struct net_device *dev);
683static int netdev_open(struct net_device *dev);
684static void do_cable_magic(struct net_device *dev);
685static void undo_cable_magic(struct net_device *dev);
686static void check_link(struct net_device *dev);
687static void netdev_timer(unsigned long data);
688static void dump_ring(struct net_device *dev);
689static void tx_timeout(struct net_device *dev);
690static int alloc_ring(struct net_device *dev);
691static void refill_rx(struct net_device *dev);
692static void init_ring(struct net_device *dev);
693static void drain_tx(struct net_device *dev);
694static void drain_ring(struct net_device *dev);
695static void free_ring(struct net_device *dev);
696static void reinit_ring(struct net_device *dev);
697static void init_registers(struct net_device *dev);
698static int start_tx(struct sk_buff *skb, struct net_device *dev);
699static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
700static void netdev_error(struct net_device *dev, int intr_status);
701static void netdev_rx(struct net_device *dev);
702static void netdev_tx_done(struct net_device *dev);
703static void __set_rx_mode(struct net_device *dev);
704static void set_rx_mode(struct net_device *dev);
705static void __get_stats(struct net_device *dev);
706static struct net_device_stats *get_stats(struct net_device *dev);
707static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
708static int netdev_set_wol(struct net_device *dev, u32 newval);
709static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur);
710static int netdev_set_sopass(struct net_device *dev, u8 *newval);
711static int netdev_get_sopass(struct net_device *dev, u8 *data);
712static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
713static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
714static void enable_wol_mode(struct net_device *dev, int enable_intr);
715static int netdev_close(struct net_device *dev);
716static int netdev_get_regs(struct net_device *dev, u8 *buf);
717static int netdev_get_eeprom(struct net_device *dev, u8 *buf);
718
719
720static int __devinit natsemi_probe1 (struct pci_dev *pdev,
721 const struct pci_device_id *ent)
722{
723 struct net_device *dev;
724 struct netdev_private *np;
725 int i, option, irq, chip_idx = ent->driver_data;
726 static int find_cnt = -1;
727 unsigned long ioaddr, iosize;
728 const int pcibar = 1;
729 int prev_eedata;
730 u32 tmp;
731
732
733#ifndef MODULE
734 static int printed_version;
735 if (!printed_version++)
736 printk(version);
737#endif
738
739 i = pci_enable_device(pdev);
740 if (i) return i;
741
742
743
744
745
746 pci_read_config_dword(pdev, PCIPM, &tmp);
747 if (tmp & PCI_PM_CTRL_STATE_MASK) {
748
749 u32 newtmp = tmp & ~PCI_PM_CTRL_STATE_MASK;
750 pci_write_config_dword(pdev, PCIPM, newtmp);
751 }
752
753 find_cnt++;
754 ioaddr = pci_resource_start(pdev, pcibar);
755 iosize = pci_resource_len(pdev, pcibar);
756 irq = pdev->irq;
757
758 if (natsemi_pci_info[chip_idx].flags & PCI_USES_MASTER)
759 pci_set_master(pdev);
760
761 dev = alloc_etherdev(sizeof (struct netdev_private));
762 if (!dev)
763 return -ENOMEM;
764 SET_MODULE_OWNER(dev);
765 SET_NETDEV_DEV(dev, &pdev->dev);
766
767 i = pci_request_regions(pdev, dev->name);
768 if (i) {
769 free_netdev(dev);
770 return i;
771 }
772
773 {
774 void *mmio = ioremap (ioaddr, iosize);
775 if (!mmio) {
776 pci_release_regions(pdev);
777 free_netdev(dev);
778 return -ENOMEM;
779 }
780 ioaddr = (unsigned long) mmio;
781 }
782
783
784 prev_eedata = eeprom_read(ioaddr, 6);
785 for (i = 0; i < 3; i++) {
786 int eedata = eeprom_read(ioaddr, i + 7);
787 dev->dev_addr[i*2] = (eedata << 1) + (prev_eedata >> 15);
788 dev->dev_addr[i*2+1] = eedata >> 7;
789 prev_eedata = eedata;
790 }
791
792 dev->base_addr = ioaddr;
793 dev->irq = irq;
794
795 np = dev->priv;
796
797 np->pci_dev = pdev;
798 pci_set_drvdata(pdev, dev);
799 np->iosize = iosize;
800 spin_lock_init(&np->lock);
801 np->msg_enable = (debug >= 0) ? (1<<debug)-1 : NATSEMI_DEF_MSG;
802 np->hands_off = 0;
803
804
805 natsemi_reload_eeprom(dev);
806 natsemi_reset(dev);
807
808 option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
809 if (dev->mem_start)
810 option = dev->mem_start;
811
812
813 if (option) {
814 if (option & 0x200)
815 np->full_duplex = 1;
816 if (option & 15)
817 printk(KERN_INFO
818 "%s: ignoring user supplied media type %d",
819 dev->name, option & 15);
820 }
821 if (find_cnt < MAX_UNITS && full_duplex[find_cnt])
822 np->full_duplex = 1;
823
824
825 dev->open = &netdev_open;
826 dev->hard_start_xmit = &start_tx;
827 dev->stop = &netdev_close;
828 dev->get_stats = &get_stats;
829 dev->set_multicast_list = &set_rx_mode;
830 dev->do_ioctl = &netdev_ioctl;
831 dev->tx_timeout = &tx_timeout;
832 dev->watchdog_timeo = TX_TIMEOUT;
833
834 if (mtu)
835 dev->mtu = mtu;
836
837 i = register_netdev(dev);
838 if (i) {
839 pci_release_regions(pdev);
840 unregister_netdev(dev);
841 free_netdev(dev);
842 pci_set_drvdata(pdev, NULL);
843 return i;
844 }
845 netif_carrier_off(dev);
846
847 if (netif_msg_drv(np)) {
848 printk(KERN_INFO "%s: %s at %#08lx, ",
849 dev->name, natsemi_pci_info[chip_idx].name, ioaddr);
850 for (i = 0; i < ETH_ALEN-1; i++)
851 printk("%02x:", dev->dev_addr[i]);
852 printk("%02x, IRQ %d.\n", dev->dev_addr[i], irq);
853 }
854
855 np->advertising = mdio_read(dev, 1, MII_ADVERTISE);
856 if ((readl(ioaddr + ChipConfig) & 0xe000) != 0xe000
857 && netif_msg_probe(np)) {
858 u32 chip_config = readl(ioaddr + ChipConfig);
859 printk(KERN_INFO "%s: Transceiver default autonegotiation %s "
860 "10%s %s duplex.\n",
861 dev->name,
862 chip_config & CfgAnegEnable ?
863 "enabled, advertise" : "disabled, force",
864 chip_config & CfgAneg100 ? "0" : "",
865 chip_config & CfgAnegFull ? "full" : "half");
866 }
867 if (netif_msg_probe(np))
868 printk(KERN_INFO
869 "%s: Transceiver status %#04x advertising %#04x.\n",
870 dev->name, mdio_read(dev, 1, MII_BMSR),
871 np->advertising);
872
873
874 np->srr = readl(ioaddr + SiliconRev);
875 if (netif_msg_hw(np))
876 printk(KERN_INFO "%s: silicon revision %#04x.\n",
877 dev->name, np->srr);
878
879
880 return 0;
881}
882
883
884
885
886
887
888
889
890
891
892
893
894#define eeprom_delay(ee_addr) readl(ee_addr)
895
896#define EE_Write0 (EE_ChipSelect)
897#define EE_Write1 (EE_ChipSelect | EE_DataIn)
898
899
900enum EEPROM_Cmds {
901 EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
902};
903
904static int eeprom_read(long addr, int location)
905{
906 int i;
907 int retval = 0;
908 long ee_addr = addr + EECtrl;
909 int read_cmd = location | EE_ReadCmd;
910 writel(EE_Write0, ee_addr);
911
912
913 for (i = 10; i >= 0; i--) {
914 short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
915 writel(dataval, ee_addr);
916 eeprom_delay(ee_addr);
917 writel(dataval | EE_ShiftClk, ee_addr);
918 eeprom_delay(ee_addr);
919 }
920 writel(EE_ChipSelect, ee_addr);
921 eeprom_delay(ee_addr);
922
923 for (i = 0; i < 16; i++) {
924 writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
925 eeprom_delay(ee_addr);
926 retval |= (readl(ee_addr) & EE_DataOut) ? 1 << i : 0;
927 writel(EE_ChipSelect, ee_addr);
928 eeprom_delay(ee_addr);
929 }
930
931
932 writel(EE_Write0, ee_addr);
933 writel(0, ee_addr);
934 return retval;
935}
936
937
938
939
940
941static int mdio_read(struct net_device *dev, int phy_id, int reg)
942{
943 if (phy_id == 1 && reg < 32)
944 return readl(dev->base_addr+BasicControl+(reg<<2))&0xffff;
945 else
946 return 0xffff;
947}
948
949static void mdio_write(struct net_device *dev, int phy_id, int reg, u16 data)
950{
951 struct netdev_private *np = dev->priv;
952 if (phy_id == 1 && reg < 32) {
953 writew(data, dev->base_addr+BasicControl+(reg<<2));
954 switch (reg) {
955 case MII_ADVERTISE: np->advertising = data; break;
956 }
957 }
958}
959
960
961#define CFG_RESET_SAVE 0xfde000
962
963#define WCSR_RESET_SAVE 0x61f
964
965#define RFCR_RESET_SAVE 0xf8500000;
966
967static void natsemi_reset(struct net_device *dev)
968{
969 int i;
970 u32 cfg;
971 u32 wcsr;
972 u32 rfcr;
973 u16 pmatch[3];
974 u16 sopass[3];
975 struct netdev_private *np = dev->priv;
976
977
978
979
980
981
982
983
984
985
986 cfg = readl(dev->base_addr + ChipConfig) & CFG_RESET_SAVE;
987
988 wcsr = readl(dev->base_addr + WOLCmd) & WCSR_RESET_SAVE;
989
990 rfcr = readl(dev->base_addr + RxFilterAddr) & RFCR_RESET_SAVE;
991
992 for (i = 0; i < 3; i++) {
993 writel(i*2, dev->base_addr + RxFilterAddr);
994 pmatch[i] = readw(dev->base_addr + RxFilterData);
995 }
996
997 for (i = 0; i < 3; i++) {
998 writel(0xa+(i*2), dev->base_addr + RxFilterAddr);
999 sopass[i] = readw(dev->base_addr + RxFilterData);
1000 }
1001
1002
1003 writel(ChipReset, dev->base_addr + ChipCmd);
1004 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1005 if (!(readl(dev->base_addr + ChipCmd) & ChipReset))
1006 break;
1007 udelay(5);
1008 }
1009 if (i==NATSEMI_HW_TIMEOUT) {
1010 printk(KERN_WARNING "%s: reset did not complete in %d usec.\n",
1011 dev->name, i*5);
1012 } else if (netif_msg_hw(np)) {
1013 printk(KERN_DEBUG "%s: reset completed in %d usec.\n",
1014 dev->name, i*5);
1015 }
1016
1017
1018 cfg |= readl(dev->base_addr + ChipConfig) & ~CFG_RESET_SAVE;
1019 writel(cfg, dev->base_addr + ChipConfig);
1020
1021 wcsr |= readl(dev->base_addr + WOLCmd) & ~WCSR_RESET_SAVE;
1022 writel(wcsr, dev->base_addr + WOLCmd);
1023
1024 rfcr |= readl(dev->base_addr + RxFilterAddr) & ~RFCR_RESET_SAVE;
1025
1026 for (i = 0; i < 3; i++) {
1027 writel(i*2, dev->base_addr + RxFilterAddr);
1028 writew(pmatch[i], dev->base_addr + RxFilterData);
1029 }
1030 for (i = 0; i < 3; i++) {
1031 writel(0xa+(i*2), dev->base_addr + RxFilterAddr);
1032 writew(sopass[i], dev->base_addr + RxFilterData);
1033 }
1034
1035 writel(rfcr, dev->base_addr + RxFilterAddr);
1036}
1037
1038static void natsemi_reload_eeprom(struct net_device *dev)
1039{
1040 struct netdev_private *np = dev->priv;
1041 int i;
1042
1043 writel(EepromReload, dev->base_addr + PCIBusCfg);
1044 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1045 udelay(50);
1046 if (!(readl(dev->base_addr + PCIBusCfg) & EepromReload))
1047 break;
1048 }
1049 if (i==NATSEMI_HW_TIMEOUT) {
1050 printk(KERN_WARNING "%s: EEPROM did not reload in %d usec.\n",
1051 dev->name, i*50);
1052 } else if (netif_msg_hw(np)) {
1053 printk(KERN_DEBUG "%s: EEPROM reloaded in %d usec.\n",
1054 dev->name, i*50);
1055 }
1056}
1057
1058static void natsemi_stop_rxtx(struct net_device *dev)
1059{
1060 long ioaddr = dev->base_addr;
1061 struct netdev_private *np = dev->priv;
1062 int i;
1063
1064 writel(RxOff | TxOff, ioaddr + ChipCmd);
1065 for(i=0;i< NATSEMI_HW_TIMEOUT;i++) {
1066 if ((readl(ioaddr + ChipCmd) & (TxOn|RxOn)) == 0)
1067 break;
1068 udelay(5);
1069 }
1070 if (i==NATSEMI_HW_TIMEOUT) {
1071 printk(KERN_WARNING "%s: Tx/Rx process did not stop in %d usec.\n",
1072 dev->name, i*5);
1073 } else if (netif_msg_hw(np)) {
1074 printk(KERN_DEBUG "%s: Tx/Rx process stopped in %d usec.\n",
1075 dev->name, i*5);
1076 }
1077}
1078
1079static int netdev_open(struct net_device *dev)
1080{
1081 struct netdev_private *np = dev->priv;
1082 long ioaddr = dev->base_addr;
1083 int i;
1084
1085
1086 natsemi_reset(dev);
1087
1088 i = request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev);
1089 if (i) return i;
1090
1091 if (netif_msg_ifup(np))
1092 printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
1093 dev->name, dev->irq);
1094 i = alloc_ring(dev);
1095 if (i < 0) {
1096 free_irq(dev->irq, dev);
1097 return i;
1098 }
1099 init_ring(dev);
1100 spin_lock_irq(&np->lock);
1101 init_registers(dev);
1102
1103 for (i = 0; i < 3; i++) {
1104 u16 mac = (dev->dev_addr[2*i+1]<<8) + dev->dev_addr[2*i];
1105
1106 writel(i*2, ioaddr + RxFilterAddr);
1107 writew(mac, ioaddr + RxFilterData);
1108 }
1109 writel(np->cur_rx_mode, ioaddr + RxFilterAddr);
1110 spin_unlock_irq(&np->lock);
1111
1112 netif_start_queue(dev);
1113
1114 if (netif_msg_ifup(np))
1115 printk(KERN_DEBUG "%s: Done netdev_open(), status: %#08x.\n",
1116 dev->name, (int)readl(ioaddr + ChipCmd));
1117
1118
1119 init_timer(&np->timer);
1120 np->timer.expires = jiffies + NATSEMI_TIMER_FREQ;
1121 np->timer.data = (unsigned long)dev;
1122 np->timer.function = &netdev_timer;
1123 add_timer(&np->timer);
1124
1125 return 0;
1126}
1127
1128static void do_cable_magic(struct net_device *dev)
1129{
1130 struct netdev_private *np = dev->priv;
1131
1132 if (np->srr >= SRR_DP83816_A5)
1133 return;
1134
1135
1136
1137
1138
1139
1140
1141 if (readl(dev->base_addr + ChipConfig) & CfgSpeed100) {
1142 u16 data;
1143
1144 writew(1, dev->base_addr + PGSEL);
1145
1146
1147
1148
1149 data = readw(dev->base_addr + TSTDAT) & 0xff;
1150
1151
1152
1153
1154 if (!(data & 0x80) || ((data >= 0xd8) && (data <= 0xff))) {
1155 struct netdev_private *np = dev->priv;
1156
1157
1158 writew(TSTDAT_FIXED, dev->base_addr + TSTDAT);
1159
1160 data = readw(dev->base_addr + DSPCFG);
1161 np->dspcfg = data | DSPCFG_LOCK;
1162 writew(np->dspcfg, dev->base_addr + DSPCFG);
1163 }
1164 writew(0, dev->base_addr + PGSEL);
1165 }
1166}
1167
1168static void undo_cable_magic(struct net_device *dev)
1169{
1170 u16 data;
1171 struct netdev_private *np = dev->priv;
1172
1173 if (np->srr >= SRR_DP83816_A5)
1174 return;
1175
1176 writew(1, dev->base_addr + PGSEL);
1177
1178 data = readw(dev->base_addr + DSPCFG);
1179 np->dspcfg = data & ~DSPCFG_LOCK;
1180 writew(np->dspcfg, dev->base_addr + DSPCFG);
1181 writew(0, dev->base_addr + PGSEL);
1182}
1183
1184static void check_link(struct net_device *dev)
1185{
1186 struct netdev_private *np = dev->priv;
1187 long ioaddr = dev->base_addr;
1188 int duplex;
1189 int chipcfg = readl(ioaddr + ChipConfig);
1190
1191 if (!(chipcfg & CfgLink)) {
1192 if (netif_carrier_ok(dev)) {
1193 if (netif_msg_link(np))
1194 printk(KERN_NOTICE "%s: link down.\n",
1195 dev->name);
1196 netif_carrier_off(dev);
1197 undo_cable_magic(dev);
1198 }
1199 return;
1200 }
1201 if (!netif_carrier_ok(dev)) {
1202 if (netif_msg_link(np))
1203 printk(KERN_NOTICE "%s: link up.\n", dev->name);
1204 netif_carrier_on(dev);
1205 do_cable_magic(dev);
1206 }
1207
1208 duplex = np->full_duplex || (chipcfg & CfgFullDuplex ? 1 : 0);
1209
1210
1211 if (duplex ^ !!(np->rx_config & RxAcceptTx)) {
1212 if (netif_msg_link(np))
1213 printk(KERN_INFO
1214 "%s: Setting %s-duplex based on negotiated "
1215 "link capability.\n", dev->name,
1216 duplex ? "full" : "half");
1217 if (duplex) {
1218 np->rx_config |= RxAcceptTx;
1219 np->tx_config |= TxCarrierIgn | TxHeartIgn;
1220 } else {
1221 np->rx_config &= ~RxAcceptTx;
1222 np->tx_config &= ~(TxCarrierIgn | TxHeartIgn);
1223 }
1224 writel(np->tx_config, ioaddr + TxConfig);
1225 writel(np->rx_config, ioaddr + RxConfig);
1226 }
1227}
1228
1229static void init_registers(struct net_device *dev)
1230{
1231 struct netdev_private *np = dev->priv;
1232 long ioaddr = dev->base_addr;
1233 int i;
1234
1235 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1236 if (readl(dev->base_addr + ChipConfig) & CfgAnegDone)
1237 break;
1238 udelay(10);
1239 }
1240 if (i==NATSEMI_HW_TIMEOUT && netif_msg_link(np)) {
1241 printk(KERN_INFO
1242 "%s: autonegotiation did not complete in %d usec.\n",
1243 dev->name, i*10);
1244 }
1245
1246
1247
1248
1249
1250
1251
1252
1253 writew(1, ioaddr + PGSEL);
1254 writew(PMDCSR_VAL, ioaddr + PMDCSR);
1255 writew(TSTDAT_VAL, ioaddr + TSTDAT);
1256 writew(DSPCFG_VAL, ioaddr + DSPCFG);
1257 writew(SDCFG_VAL, ioaddr + SDCFG);
1258 writew(0, ioaddr + PGSEL);
1259 np->dspcfg = DSPCFG_VAL;
1260
1261
1262
1263
1264
1265 readw(ioaddr + MIntrStatus);
1266 writew(MICRIntEn, ioaddr + MIntrCtrl);
1267
1268
1269 readl(ioaddr + IntrStatus);
1270
1271 writel(np->ring_dma, ioaddr + RxRingPtr);
1272 writel(np->ring_dma + RX_RING_SIZE * sizeof(struct netdev_desc),
1273 ioaddr + TxRingPtr);
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289 np->tx_config = TxAutoPad | TxCollRetry | TxMxdma_256 | (0x1002);
1290 writel(np->tx_config, ioaddr + TxConfig);
1291
1292
1293
1294
1295 np->rx_config = RxMxdma_256 | 0x20;
1296 writel(np->rx_config, ioaddr + RxConfig);
1297
1298
1299
1300
1301
1302
1303
1304 np->SavedClkRun = readl(ioaddr + ClkRun);
1305 writel(np->SavedClkRun & ~PMEEnable, ioaddr + ClkRun);
1306 if (np->SavedClkRun & PMEStatus && netif_msg_wol(np)) {
1307 printk(KERN_NOTICE "%s: Wake-up event %#08x\n",
1308 dev->name, readl(ioaddr + WOLCmd));
1309 }
1310
1311 check_link(dev);
1312 __set_rx_mode(dev);
1313
1314
1315 writel(DEFAULT_INTR, ioaddr + IntrMask);
1316 writel(1, ioaddr + IntrEnable);
1317
1318 writel(RxOn | TxOn, ioaddr + ChipCmd);
1319 writel(StatsClear, ioaddr + StatsCtrl);
1320}
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334static void netdev_timer(unsigned long data)
1335{
1336 struct net_device *dev = (struct net_device *)data;
1337 struct netdev_private *np = dev->priv;
1338 int next_tick = 5*HZ;
1339 long ioaddr = dev->base_addr;
1340 u16 dspcfg;
1341
1342 if (netif_msg_timer(np)) {
1343
1344
1345
1346 printk(KERN_DEBUG "%s: Media selection timer tick.\n",
1347 dev->name);
1348 }
1349
1350 spin_lock_irq(&np->lock);
1351
1352
1353 writew(1, ioaddr+PGSEL);
1354 dspcfg = readw(ioaddr+DSPCFG);
1355 writew(0, ioaddr+PGSEL);
1356 if (dspcfg != np->dspcfg) {
1357 if (!netif_queue_stopped(dev)) {
1358 spin_unlock_irq(&np->lock);
1359 if (netif_msg_hw(np))
1360 printk(KERN_NOTICE "%s: possible phy reset: "
1361 "re-initializing\n", dev->name);
1362 disable_irq(dev->irq);
1363 spin_lock_irq(&np->lock);
1364 natsemi_stop_rxtx(dev);
1365 dump_ring(dev);
1366 reinit_ring(dev);
1367 init_registers(dev);
1368 spin_unlock_irq(&np->lock);
1369 enable_irq(dev->irq);
1370 } else {
1371
1372 next_tick = HZ;
1373 spin_unlock_irq(&np->lock);
1374 }
1375 } else {
1376
1377 check_link(dev);
1378 spin_unlock_irq(&np->lock);
1379 }
1380 if (np->oom) {
1381 disable_irq(dev->irq);
1382 np->oom = 0;
1383 refill_rx(dev);
1384 enable_irq(dev->irq);
1385 if (!np->oom) {
1386 writel(RxOn, dev->base_addr + ChipCmd);
1387 } else {
1388 next_tick = 1;
1389 }
1390 }
1391 mod_timer(&np->timer, jiffies + next_tick);
1392}
1393
1394static void dump_ring(struct net_device *dev)
1395{
1396 struct netdev_private *np = dev->priv;
1397
1398 if (netif_msg_pktdata(np)) {
1399 int i;
1400 printk(KERN_DEBUG " Tx ring at %p:\n", np->tx_ring);
1401 for (i = 0; i < TX_RING_SIZE; i++) {
1402 printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1403 i, np->tx_ring[i].next_desc,
1404 np->tx_ring[i].cmd_status,
1405 np->tx_ring[i].addr);
1406 }
1407 printk(KERN_DEBUG " Rx ring %p:\n", np->rx_ring);
1408 for (i = 0; i < RX_RING_SIZE; i++) {
1409 printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1410 i, np->rx_ring[i].next_desc,
1411 np->rx_ring[i].cmd_status,
1412 np->rx_ring[i].addr);
1413 }
1414 }
1415}
1416
1417static void tx_timeout(struct net_device *dev)
1418{
1419 struct netdev_private *np = dev->priv;
1420 long ioaddr = dev->base_addr;
1421
1422 disable_irq(dev->irq);
1423 spin_lock_irq(&np->lock);
1424 if (!np->hands_off) {
1425 if (netif_msg_tx_err(np))
1426 printk(KERN_WARNING
1427 "%s: Transmit timed out, status %#08x,"
1428 " resetting...\n",
1429 dev->name, readl(ioaddr + IntrStatus));
1430 dump_ring(dev);
1431
1432 natsemi_reset(dev);
1433 reinit_ring(dev);
1434 init_registers(dev);
1435 } else {
1436 printk(KERN_WARNING
1437 "%s: tx_timeout while in hands_off state?\n",
1438 dev->name);
1439 }
1440 spin_unlock_irq(&np->lock);
1441 enable_irq(dev->irq);
1442
1443 dev->trans_start = jiffies;
1444 np->stats.tx_errors++;
1445 netif_wake_queue(dev);
1446}
1447
1448static int alloc_ring(struct net_device *dev)
1449{
1450 struct netdev_private *np = dev->priv;
1451 np->rx_ring = pci_alloc_consistent(np->pci_dev,
1452 sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
1453 &np->ring_dma);
1454 if (!np->rx_ring)
1455 return -ENOMEM;
1456 np->tx_ring = &np->rx_ring[RX_RING_SIZE];
1457 return 0;
1458}
1459
1460static void refill_rx(struct net_device *dev)
1461{
1462 struct netdev_private *np = dev->priv;
1463
1464
1465 for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1466 struct sk_buff *skb;
1467 int entry = np->dirty_rx % RX_RING_SIZE;
1468 if (np->rx_skbuff[entry] == NULL) {
1469 skb = dev_alloc_skb(np->rx_buf_sz);
1470 np->rx_skbuff[entry] = skb;
1471 if (skb == NULL)
1472 break;
1473 skb->dev = dev;
1474 np->rx_dma[entry] = pci_map_single(np->pci_dev,
1475 skb->data, skb->len, PCI_DMA_FROMDEVICE);
1476 np->rx_ring[entry].addr = cpu_to_le32(np->rx_dma[entry]);
1477 }
1478 np->rx_ring[entry].cmd_status = cpu_to_le32(np->rx_buf_sz);
1479 }
1480 if (np->cur_rx - np->dirty_rx == RX_RING_SIZE) {
1481 if (netif_msg_rx_err(np))
1482 printk(KERN_WARNING "%s: going OOM.\n", dev->name);
1483 np->oom = 1;
1484 }
1485}
1486
1487
1488static void init_ring(struct net_device *dev)
1489{
1490 struct netdev_private *np = dev->priv;
1491 int i;
1492
1493
1494 np->dirty_tx = np->cur_tx = 0;
1495 for (i = 0; i < TX_RING_SIZE; i++) {
1496 np->tx_skbuff[i] = NULL;
1497 np->tx_ring[i].next_desc = cpu_to_le32(np->ring_dma
1498 +sizeof(struct netdev_desc)
1499 *((i+1)%TX_RING_SIZE+RX_RING_SIZE));
1500 np->tx_ring[i].cmd_status = 0;
1501 }
1502
1503
1504 np->dirty_rx = 0;
1505 np->cur_rx = RX_RING_SIZE;
1506 np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
1507 np->oom = 0;
1508 np->rx_head_desc = &np->rx_ring[0];
1509
1510
1511
1512
1513
1514 for (i = 0; i < RX_RING_SIZE; i++) {
1515 np->rx_ring[i].next_desc = cpu_to_le32(np->ring_dma
1516 +sizeof(struct netdev_desc)
1517 *((i+1)%RX_RING_SIZE));
1518 np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
1519 np->rx_skbuff[i] = NULL;
1520 }
1521 refill_rx(dev);
1522 dump_ring(dev);
1523}
1524
1525static void drain_tx(struct net_device *dev)
1526{
1527 struct netdev_private *np = dev->priv;
1528 int i;
1529
1530 for (i = 0; i < TX_RING_SIZE; i++) {
1531 if (np->tx_skbuff[i]) {
1532 pci_unmap_single(np->pci_dev,
1533 np->tx_dma[i], np->tx_skbuff[i]->len,
1534 PCI_DMA_TODEVICE);
1535 dev_kfree_skb(np->tx_skbuff[i]);
1536 np->stats.tx_dropped++;
1537 }
1538 np->tx_skbuff[i] = NULL;
1539 }
1540}
1541
1542static void drain_ring(struct net_device *dev)
1543{
1544 struct netdev_private *np = dev->priv;
1545 int i;
1546
1547
1548 for (i = 0; i < RX_RING_SIZE; i++) {
1549 np->rx_ring[i].cmd_status = 0;
1550 np->rx_ring[i].addr = 0xBADF00D0;
1551 if (np->rx_skbuff[i]) {
1552 pci_unmap_single(np->pci_dev,
1553 np->rx_dma[i], np->rx_skbuff[i]->len,
1554 PCI_DMA_FROMDEVICE);
1555 dev_kfree_skb(np->rx_skbuff[i]);
1556 }
1557 np->rx_skbuff[i] = NULL;
1558 }
1559 drain_tx(dev);
1560}
1561
1562static void free_ring(struct net_device *dev)
1563{
1564 struct netdev_private *np = dev->priv;
1565 pci_free_consistent(np->pci_dev,
1566 sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
1567 np->rx_ring, np->ring_dma);
1568}
1569
1570static void reinit_ring(struct net_device *dev)
1571{
1572 struct netdev_private *np = dev->priv;
1573 int i;
1574
1575
1576 drain_tx(dev);
1577 np->dirty_tx = np->cur_tx = 0;
1578 for (i=0;i<TX_RING_SIZE;i++)
1579 np->tx_ring[i].cmd_status = 0;
1580
1581
1582 np->dirty_rx = 0;
1583 np->cur_rx = RX_RING_SIZE;
1584 np->rx_head_desc = &np->rx_ring[0];
1585
1586 for (i = 0; i < RX_RING_SIZE; i++)
1587 np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
1588
1589 refill_rx(dev);
1590}
1591
1592static int start_tx(struct sk_buff *skb, struct net_device *dev)
1593{
1594 struct netdev_private *np = dev->priv;
1595 unsigned entry;
1596
1597
1598
1599
1600
1601 entry = np->cur_tx % TX_RING_SIZE;
1602
1603 np->tx_skbuff[entry] = skb;
1604 np->tx_dma[entry] = pci_map_single(np->pci_dev,
1605 skb->data,skb->len, PCI_DMA_TODEVICE);
1606
1607 np->tx_ring[entry].addr = cpu_to_le32(np->tx_dma[entry]);
1608
1609 spin_lock_irq(&np->lock);
1610
1611 if (!np->hands_off) {
1612 np->tx_ring[entry].cmd_status = cpu_to_le32(DescOwn | skb->len);
1613
1614
1615 wmb();
1616 np->cur_tx++;
1617 if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) {
1618 netdev_tx_done(dev);
1619 if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1)
1620 netif_stop_queue(dev);
1621 }
1622
1623 writel(TxOn, dev->base_addr + ChipCmd);
1624 } else {
1625 dev_kfree_skb_irq(skb);
1626 np->stats.tx_dropped++;
1627 }
1628 spin_unlock_irq(&np->lock);
1629
1630 dev->trans_start = jiffies;
1631
1632 if (netif_msg_tx_queued(np)) {
1633 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
1634 dev->name, np->cur_tx, entry);
1635 }
1636 return 0;
1637}
1638
1639static void netdev_tx_done(struct net_device *dev)
1640{
1641 struct netdev_private *np = dev->priv;
1642
1643 for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
1644 int entry = np->dirty_tx % TX_RING_SIZE;
1645 if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescOwn))
1646 break;
1647 if (netif_msg_tx_done(np))
1648 printk(KERN_DEBUG
1649 "%s: tx frame #%d finished, status %#08x.\n",
1650 dev->name, np->dirty_tx,
1651 le32_to_cpu(np->tx_ring[entry].cmd_status));
1652 if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescPktOK)) {
1653 np->stats.tx_packets++;
1654 np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1655 } else {
1656 int tx_status =
1657 le32_to_cpu(np->tx_ring[entry].cmd_status);
1658 if (tx_status & (DescTxAbort|DescTxExcColl))
1659 np->stats.tx_aborted_errors++;
1660 if (tx_status & DescTxFIFO)
1661 np->stats.tx_fifo_errors++;
1662 if (tx_status & DescTxCarrier)
1663 np->stats.tx_carrier_errors++;
1664 if (tx_status & DescTxOOWCol)
1665 np->stats.tx_window_errors++;
1666 np->stats.tx_errors++;
1667 }
1668 pci_unmap_single(np->pci_dev,np->tx_dma[entry],
1669 np->tx_skbuff[entry]->len,
1670 PCI_DMA_TODEVICE);
1671
1672 dev_kfree_skb_irq(np->tx_skbuff[entry]);
1673 np->tx_skbuff[entry] = NULL;
1674 }
1675 if (netif_queue_stopped(dev)
1676 && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
1677
1678 netif_wake_queue(dev);
1679 }
1680}
1681
1682
1683
1684static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
1685{
1686 struct net_device *dev = dev_instance;
1687 struct netdev_private *np = dev->priv;
1688 long ioaddr = dev->base_addr;
1689 int boguscnt = max_interrupt_work;
1690 unsigned int handled = 0;
1691
1692 if (np->hands_off)
1693 return IRQ_NONE;
1694 do {
1695
1696 u32 intr_status = readl(ioaddr + IntrStatus);
1697
1698 if (netif_msg_intr(np))
1699 printk(KERN_DEBUG
1700 "%s: Interrupt, status %#08x, mask %#08x.\n",
1701 dev->name, intr_status,
1702 readl(ioaddr + IntrMask));
1703
1704 if (intr_status == 0)
1705 break;
1706 handled = 1;
1707
1708 if (intr_status &
1709 (IntrRxDone | IntrRxIntr | RxStatusFIFOOver |
1710 IntrRxErr | IntrRxOverrun)) {
1711 netdev_rx(dev);
1712 }
1713
1714 if (intr_status &
1715 (IntrTxDone | IntrTxIntr | IntrTxIdle | IntrTxErr)) {
1716 spin_lock(&np->lock);
1717 netdev_tx_done(dev);
1718 spin_unlock(&np->lock);
1719 }
1720
1721
1722 if (intr_status & IntrAbnormalSummary)
1723 netdev_error(dev, intr_status);
1724
1725 if (--boguscnt < 0) {
1726 if (netif_msg_intr(np))
1727 printk(KERN_WARNING
1728 "%s: Too much work at interrupt, "
1729 "status=%#08x.\n",
1730 dev->name, intr_status);
1731 break;
1732 }
1733 } while (1);
1734
1735 if (netif_msg_intr(np))
1736 printk(KERN_DEBUG "%s: exiting interrupt.\n", dev->name);
1737
1738 return IRQ_RETVAL(handled);
1739}
1740
1741
1742
1743static void netdev_rx(struct net_device *dev)
1744{
1745 struct netdev_private *np = dev->priv;
1746 int entry = np->cur_rx % RX_RING_SIZE;
1747 int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
1748 s32 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
1749
1750
1751 while (desc_status < 0) {
1752 if (netif_msg_rx_status(np))
1753 printk(KERN_DEBUG
1754 " netdev_rx() entry %d status was %#08x.\n",
1755 entry, desc_status);
1756 if (--boguscnt < 0)
1757 break;
1758 if ((desc_status&(DescMore|DescPktOK|DescRxLong)) != DescPktOK){
1759 if (desc_status & DescMore) {
1760 if (netif_msg_rx_err(np))
1761 printk(KERN_WARNING
1762 "%s: Oversized(?) Ethernet "
1763 "frame spanned multiple "
1764 "buffers, entry %#08x "
1765 "status %#08x.\n", dev->name,
1766 np->cur_rx, desc_status);
1767 np->stats.rx_length_errors++;
1768 } else {
1769
1770 np->stats.rx_errors++;
1771 if (desc_status & (DescRxAbort|DescRxOver))
1772 np->stats.rx_over_errors++;
1773 if (desc_status & (DescRxLong|DescRxRunt))
1774 np->stats.rx_length_errors++;
1775 if (desc_status & (DescRxInvalid|DescRxAlign))
1776 np->stats.rx_frame_errors++;
1777 if (desc_status & DescRxCRC)
1778 np->stats.rx_crc_errors++;
1779 }
1780 } else {
1781 struct sk_buff *skb;
1782
1783 int pkt_len = (desc_status & DescSizeMask) - 4;
1784
1785
1786 if (pkt_len < rx_copybreak
1787 && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1788 skb->dev = dev;
1789
1790 skb_reserve(skb, 2);
1791 pci_dma_sync_single(np->pci_dev,
1792 np->rx_dma[entry],
1793 np->rx_skbuff[entry]->len,
1794 PCI_DMA_FROMDEVICE);
1795#if HAS_IP_COPYSUM
1796 eth_copy_and_sum(skb,
1797 np->rx_skbuff[entry]->tail, pkt_len, 0);
1798 skb_put(skb, pkt_len);
1799#else
1800 memcpy(skb_put(skb, pkt_len),
1801 np->rx_skbuff[entry]->tail, pkt_len);
1802#endif
1803 } else {
1804 pci_unmap_single(np->pci_dev, np->rx_dma[entry],
1805 np->rx_skbuff[entry]->len,
1806 PCI_DMA_FROMDEVICE);
1807 skb_put(skb = np->rx_skbuff[entry], pkt_len);
1808 np->rx_skbuff[entry] = NULL;
1809 }
1810 skb->protocol = eth_type_trans(skb, dev);
1811 netif_rx(skb);
1812 dev->last_rx = jiffies;
1813 np->stats.rx_packets++;
1814 np->stats.rx_bytes += pkt_len;
1815 }
1816 entry = (++np->cur_rx) % RX_RING_SIZE;
1817 np->rx_head_desc = &np->rx_ring[entry];
1818 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
1819 }
1820 refill_rx(dev);
1821
1822
1823 if (np->oom)
1824 mod_timer(&np->timer, jiffies + 1);
1825 else
1826 writel(RxOn, dev->base_addr + ChipCmd);
1827}
1828
1829static void netdev_error(struct net_device *dev, int intr_status)
1830{
1831 struct netdev_private *np = dev->priv;
1832 long ioaddr = dev->base_addr;
1833
1834 spin_lock(&np->lock);
1835 if (intr_status & LinkChange) {
1836 u16 adv = mdio_read(dev, 1, MII_ADVERTISE);
1837 u16 lpa = mdio_read(dev, 1, MII_LPA);
1838 if (mdio_read(dev, 1, MII_BMCR) & BMCR_ANENABLE
1839 && netif_msg_link(np)) {
1840 printk(KERN_INFO
1841 "%s: Autonegotiation advertising"
1842 " %#04x partner %#04x.\n", dev->name,
1843 adv, lpa);
1844 }
1845
1846
1847 readw(ioaddr + MIntrStatus);
1848 check_link(dev);
1849 }
1850 if (intr_status & StatsMax) {
1851 __get_stats(dev);
1852 }
1853 if (intr_status & IntrTxUnderrun) {
1854 if ((np->tx_config & TxDrthMask) < 62)
1855 np->tx_config += 2;
1856 if (netif_msg_tx_err(np))
1857 printk(KERN_NOTICE
1858 "%s: increased Tx threshold, txcfg %#08x.\n",
1859 dev->name, np->tx_config);
1860 writel(np->tx_config, ioaddr + TxConfig);
1861 }
1862 if (intr_status & WOLPkt && netif_msg_wol(np)) {
1863 int wol_status = readl(ioaddr + WOLCmd);
1864 printk(KERN_NOTICE "%s: Link wake-up event %#08x\n",
1865 dev->name, wol_status);
1866 }
1867 if (intr_status & RxStatusFIFOOver) {
1868 if (netif_msg_rx_err(np) && netif_msg_intr(np)) {
1869 printk(KERN_NOTICE "%s: Rx status FIFO overrun\n",
1870 dev->name);
1871 }
1872 np->stats.rx_fifo_errors++;
1873 }
1874
1875 if (intr_status & IntrPCIErr) {
1876 printk(KERN_NOTICE "%s: PCI error %#08x\n", dev->name,
1877 intr_status & IntrPCIErr);
1878 np->stats.tx_fifo_errors++;
1879 np->stats.rx_fifo_errors++;
1880 }
1881 spin_unlock(&np->lock);
1882}
1883
1884static void __get_stats(struct net_device *dev)
1885{
1886 long ioaddr = dev->base_addr;
1887 struct netdev_private *np = dev->priv;
1888
1889
1890 np->stats.rx_crc_errors += readl(ioaddr + RxCRCErrs);
1891 np->stats.rx_missed_errors += readl(ioaddr + RxMissed);
1892}
1893
1894static struct net_device_stats *get_stats(struct net_device *dev)
1895{
1896 struct netdev_private *np = dev->priv;
1897
1898
1899 spin_lock_irq(&np->lock);
1900 if (netif_running(dev) && !np->hands_off)
1901 __get_stats(dev);
1902 spin_unlock_irq(&np->lock);
1903
1904 return &np->stats;
1905}
1906
1907#define HASH_TABLE 0x200
1908static void __set_rx_mode(struct net_device *dev)
1909{
1910 long ioaddr = dev->base_addr;
1911 struct netdev_private *np = dev->priv;
1912 u8 mc_filter[64];
1913 u32 rx_mode;
1914
1915 if (dev->flags & IFF_PROMISC) {
1916
1917 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
1918 dev->name);
1919 rx_mode = RxFilterEnable | AcceptBroadcast
1920 | AcceptAllMulticast | AcceptAllPhys | AcceptMyPhys;
1921 } else if ((dev->mc_count > multicast_filter_limit)
1922 || (dev->flags & IFF_ALLMULTI)) {
1923 rx_mode = RxFilterEnable | AcceptBroadcast
1924 | AcceptAllMulticast | AcceptMyPhys;
1925 } else {
1926 struct dev_mc_list *mclist;
1927 int i;
1928 memset(mc_filter, 0, sizeof(mc_filter));
1929 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1930 i++, mclist = mclist->next) {
1931 int i = (ether_crc(ETH_ALEN, mclist->dmi_addr) >> 23) & 0x1ff;
1932 mc_filter[i/8] |= (1 << (i & 0x07));
1933 }
1934 rx_mode = RxFilterEnable | AcceptBroadcast
1935 | AcceptMulticast | AcceptMyPhys;
1936 for (i = 0; i < 64; i += 2) {
1937 writew(HASH_TABLE + i, ioaddr + RxFilterAddr);
1938 writew((mc_filter[i+1]<<8) + mc_filter[i],
1939 ioaddr + RxFilterData);
1940 }
1941 }
1942 writel(rx_mode, ioaddr + RxFilterAddr);
1943 np->cur_rx_mode = rx_mode;
1944}
1945
1946static void set_rx_mode(struct net_device *dev)
1947{
1948 struct netdev_private *np = dev->priv;
1949 spin_lock_irq(&np->lock);
1950 if (!np->hands_off)
1951 __set_rx_mode(dev);
1952 spin_unlock_irq(&np->lock);
1953}
1954
1955static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
1956{
1957 struct netdev_private *np = dev->priv;
1958 u32 cmd;
1959
1960 if (get_user(cmd, (u32 *)useraddr))
1961 return -EFAULT;
1962
1963 switch (cmd) {
1964
1965 case ETHTOOL_GDRVINFO: {
1966 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
1967 strncpy(info.driver, DRV_NAME, ETHTOOL_BUSINFO_LEN);
1968 strncpy(info.version, DRV_VERSION, ETHTOOL_BUSINFO_LEN);
1969 info.fw_version[0] = '\0';
1970 strncpy(info.bus_info, pci_name(np->pci_dev),
1971 ETHTOOL_BUSINFO_LEN);
1972 info.eedump_len = NATSEMI_EEPROM_SIZE;
1973 info.regdump_len = NATSEMI_REGS_SIZE;
1974 if (copy_to_user(useraddr, &info, sizeof(info)))
1975 return -EFAULT;
1976 return 0;
1977 }
1978
1979 case ETHTOOL_GSET: {
1980 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1981 spin_lock_irq(&np->lock);
1982 netdev_get_ecmd(dev, &ecmd);
1983 spin_unlock_irq(&np->lock);
1984 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1985 return -EFAULT;
1986 return 0;
1987 }
1988
1989 case ETHTOOL_SSET: {
1990 struct ethtool_cmd ecmd;
1991 int r;
1992 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1993 return -EFAULT;
1994 spin_lock_irq(&np->lock);
1995 r = netdev_set_ecmd(dev, &ecmd);
1996 spin_unlock_irq(&np->lock);
1997 return r;
1998 }
1999
2000 case ETHTOOL_GWOL: {
2001 struct ethtool_wolinfo wol = {ETHTOOL_GWOL};
2002 spin_lock_irq(&np->lock);
2003 netdev_get_wol(dev, &wol.supported, &wol.wolopts);
2004 netdev_get_sopass(dev, wol.sopass);
2005 spin_unlock_irq(&np->lock);
2006 if (copy_to_user(useraddr, &wol, sizeof(wol)))
2007 return -EFAULT;
2008 return 0;
2009 }
2010
2011 case ETHTOOL_SWOL: {
2012 struct ethtool_wolinfo wol;
2013 int r;
2014 if (copy_from_user(&wol, useraddr, sizeof(wol)))
2015 return -EFAULT;
2016 spin_lock_irq(&np->lock);
2017 netdev_set_wol(dev, wol.wolopts);
2018 r = netdev_set_sopass(dev, wol.sopass);
2019 spin_unlock_irq(&np->lock);
2020 return r;
2021 }
2022
2023 case ETHTOOL_GREGS: {
2024 struct ethtool_regs regs;
2025 u8 regbuf[NATSEMI_REGS_SIZE];
2026 int r;
2027
2028 if (copy_from_user(®s, useraddr, sizeof(regs)))
2029 return -EFAULT;
2030
2031 if (regs.len > NATSEMI_REGS_SIZE) {
2032 regs.len = NATSEMI_REGS_SIZE;
2033 }
2034 regs.version = NATSEMI_REGS_VER;
2035 if (copy_to_user(useraddr, ®s, sizeof(regs)))
2036 return -EFAULT;
2037
2038 useraddr += offsetof(struct ethtool_regs, data);
2039
2040 spin_lock_irq(&np->lock);
2041 r = netdev_get_regs(dev, regbuf);
2042 spin_unlock_irq(&np->lock);
2043
2044 if (r)
2045 return r;
2046 if (copy_to_user(useraddr, regbuf, regs.len))
2047 return -EFAULT;
2048 return 0;
2049 }
2050
2051 case ETHTOOL_GMSGLVL: {
2052 struct ethtool_value edata = {ETHTOOL_GMSGLVL};
2053 edata.data = np->msg_enable;
2054 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2055 return -EFAULT;
2056 return 0;
2057 }
2058
2059 case ETHTOOL_SMSGLVL: {
2060 struct ethtool_value edata;
2061 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2062 return -EFAULT;
2063 np->msg_enable = edata.data;
2064 return 0;
2065 }
2066
2067 case ETHTOOL_NWAY_RST: {
2068 int tmp;
2069 int r = -EINVAL;
2070
2071 tmp = mdio_read(dev, 1, MII_BMCR);
2072 if (tmp & BMCR_ANENABLE) {
2073 tmp |= (BMCR_ANRESTART);
2074 mdio_write(dev, 1, MII_BMCR, tmp);
2075 r = 0;
2076 }
2077 return r;
2078 }
2079
2080 case ETHTOOL_GLINK: {
2081 struct ethtool_value edata = {ETHTOOL_GLINK};
2082
2083 mdio_read(dev, 1, MII_BMSR);
2084 edata.data = (mdio_read(dev, 1, MII_BMSR)&BMSR_LSTATUS) ? 1:0;
2085 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2086 return -EFAULT;
2087 return 0;
2088 }
2089
2090 case ETHTOOL_GEEPROM: {
2091 struct ethtool_eeprom eeprom;
2092 u8 eebuf[NATSEMI_EEPROM_SIZE];
2093 int r;
2094
2095 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
2096 return -EFAULT;
2097
2098 if (eeprom.offset > eeprom.offset+eeprom.len)
2099 return -EINVAL;
2100
2101 if ((eeprom.offset+eeprom.len) > NATSEMI_EEPROM_SIZE) {
2102 eeprom.len = NATSEMI_EEPROM_SIZE-eeprom.offset;
2103 }
2104 eeprom.magic = PCI_VENDOR_ID_NS | (PCI_DEVICE_ID_NS_83815<<16);
2105 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
2106 return -EFAULT;
2107
2108 useraddr += offsetof(struct ethtool_eeprom, data);
2109
2110 spin_lock_irq(&np->lock);
2111 r = netdev_get_eeprom(dev, eebuf);
2112 spin_unlock_irq(&np->lock);
2113
2114 if (r)
2115 return r;
2116 if (copy_to_user(useraddr, eebuf+eeprom.offset, eeprom.len))
2117 return -EFAULT;
2118 return 0;
2119 }
2120
2121 }
2122
2123 return -EOPNOTSUPP;
2124}
2125
2126static int netdev_set_wol(struct net_device *dev, u32 newval)
2127{
2128 struct netdev_private *np = dev->priv;
2129 u32 data = readl(dev->base_addr + WOLCmd) & ~WakeOptsSummary;
2130
2131
2132 if (newval & WAKE_PHY)
2133 data |= WakePhy;
2134 if (newval & WAKE_UCAST)
2135 data |= WakeUnicast;
2136 if (newval & WAKE_MCAST)
2137 data |= WakeMulticast;
2138 if (newval & WAKE_BCAST)
2139 data |= WakeBroadcast;
2140 if (newval & WAKE_ARP)
2141 data |= WakeArp;
2142 if (newval & WAKE_MAGIC)
2143 data |= WakeMagic;
2144 if (np->srr >= SRR_DP83815_D) {
2145 if (newval & WAKE_MAGICSECURE) {
2146 data |= WakeMagicSecure;
2147 }
2148 }
2149
2150 writel(data, dev->base_addr + WOLCmd);
2151
2152 return 0;
2153}
2154
2155static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur)
2156{
2157 struct netdev_private *np = dev->priv;
2158 u32 regval = readl(dev->base_addr + WOLCmd);
2159
2160 *supported = (WAKE_PHY | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST
2161 | WAKE_ARP | WAKE_MAGIC);
2162
2163 if (np->srr >= SRR_DP83815_D) {
2164
2165 *supported |= WAKE_MAGICSECURE;
2166 }
2167 *cur = 0;
2168
2169
2170 if (regval & WakePhy)
2171 *cur |= WAKE_PHY;
2172 if (regval & WakeUnicast)
2173 *cur |= WAKE_UCAST;
2174 if (regval & WakeMulticast)
2175 *cur |= WAKE_MCAST;
2176 if (regval & WakeBroadcast)
2177 *cur |= WAKE_BCAST;
2178 if (regval & WakeArp)
2179 *cur |= WAKE_ARP;
2180 if (regval & WakeMagic)
2181 *cur |= WAKE_MAGIC;
2182 if (regval & WakeMagicSecure) {
2183
2184 *cur |= WAKE_MAGICSECURE;
2185 }
2186
2187 return 0;
2188}
2189
2190static int netdev_set_sopass(struct net_device *dev, u8 *newval)
2191{
2192 struct netdev_private *np = dev->priv;
2193 u16 *sval = (u16 *)newval;
2194 u32 addr;
2195
2196 if (np->srr < SRR_DP83815_D) {
2197 return 0;
2198 }
2199
2200
2201 addr = readl(dev->base_addr + RxFilterAddr) & ~RFCRAddressMask;
2202 addr &= ~RxFilterEnable;
2203 writel(addr, dev->base_addr + RxFilterAddr);
2204
2205
2206 writel(addr | 0xa, dev->base_addr + RxFilterAddr);
2207 writew(sval[0], dev->base_addr + RxFilterData);
2208
2209 writel(addr | 0xc, dev->base_addr + RxFilterAddr);
2210 writew(sval[1], dev->base_addr + RxFilterData);
2211
2212 writel(addr | 0xe, dev->base_addr + RxFilterAddr);
2213 writew(sval[2], dev->base_addr + RxFilterData);
2214
2215
2216 writel(addr | RxFilterEnable, dev->base_addr + RxFilterAddr);
2217
2218 return 0;
2219}
2220
2221static int netdev_get_sopass(struct net_device *dev, u8 *data)
2222{
2223 struct netdev_private *np = dev->priv;
2224 u16 *sval = (u16 *)data;
2225 u32 addr;
2226
2227 if (np->srr < SRR_DP83815_D) {
2228 sval[0] = sval[1] = sval[2] = 0;
2229 return 0;
2230 }
2231
2232
2233 addr = readl(dev->base_addr + RxFilterAddr) & ~RFCRAddressMask;
2234
2235 writel(addr | 0xa, dev->base_addr + RxFilterAddr);
2236 sval[0] = readw(dev->base_addr + RxFilterData);
2237
2238 writel(addr | 0xc, dev->base_addr + RxFilterAddr);
2239 sval[1] = readw(dev->base_addr + RxFilterData);
2240
2241 writel(addr | 0xe, dev->base_addr + RxFilterAddr);
2242 sval[2] = readw(dev->base_addr + RxFilterData);
2243
2244 writel(addr, dev->base_addr + RxFilterAddr);
2245
2246 return 0;
2247}
2248
2249static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2250{
2251 u32 tmp;
2252
2253 ecmd->supported =
2254 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2255 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2256 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
2257
2258
2259 tmp = readl(dev->base_addr + ChipConfig);
2260 if (tmp & CfgExtPhy)
2261 ecmd->port = PORT_MII;
2262 else
2263 ecmd->port = PORT_TP;
2264
2265
2266 ecmd->transceiver = XCVR_INTERNAL;
2267
2268
2269 ecmd->phy_address = readw(dev->base_addr + PhyCtrl) & PhyAddrMask;
2270
2271 ecmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
2272 tmp = mdio_read(dev, 1, MII_ADVERTISE);
2273 if (tmp & ADVERTISE_10HALF)
2274 ecmd->advertising |= ADVERTISED_10baseT_Half;
2275 if (tmp & ADVERTISE_10FULL)
2276 ecmd->advertising |= ADVERTISED_10baseT_Full;
2277 if (tmp & ADVERTISE_100HALF)
2278 ecmd->advertising |= ADVERTISED_100baseT_Half;
2279 if (tmp & ADVERTISE_100FULL)
2280 ecmd->advertising |= ADVERTISED_100baseT_Full;
2281
2282 tmp = mdio_read(dev, 1, MII_BMCR);
2283 if (tmp & BMCR_ANENABLE) {
2284 ecmd->advertising |= ADVERTISED_Autoneg;
2285 ecmd->autoneg = AUTONEG_ENABLE;
2286 } else {
2287 ecmd->autoneg = AUTONEG_DISABLE;
2288 }
2289
2290 tmp = readl(dev->base_addr + ChipConfig);
2291 if (tmp & CfgSpeed100) {
2292 ecmd->speed = SPEED_100;
2293 } else {
2294 ecmd->speed = SPEED_10;
2295 }
2296
2297 if (tmp & CfgFullDuplex) {
2298 ecmd->duplex = DUPLEX_FULL;
2299 } else {
2300 ecmd->duplex = DUPLEX_HALF;
2301 }
2302
2303
2304
2305 return 0;
2306}
2307
2308static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2309{
2310 struct netdev_private *np = dev->priv;
2311 u32 tmp;
2312
2313 if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
2314 return -EINVAL;
2315 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
2316 return -EINVAL;
2317 if (ecmd->port != PORT_TP && ecmd->port != PORT_MII)
2318 return -EINVAL;
2319 if (ecmd->transceiver != XCVR_INTERNAL)
2320 return -EINVAL;
2321 if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
2322 return -EINVAL;
2323
2324
2325
2326
2327 tmp = mdio_read(dev, 1, MII_BMCR);
2328 if (ecmd->autoneg == AUTONEG_ENABLE) {
2329
2330 tmp |= BMCR_ANENABLE;
2331 np->advertising = mdio_read(dev, 1, MII_ADVERTISE);
2332 } else {
2333
2334 tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
2335 if (ecmd->speed == SPEED_100)
2336 tmp |= BMCR_SPEED100;
2337 if (ecmd->duplex == DUPLEX_FULL)
2338 tmp |= BMCR_FULLDPLX;
2339 else
2340 np->full_duplex = 0;
2341 }
2342 mdio_write(dev, 1, MII_BMCR, tmp);
2343 return 0;
2344}
2345
2346static int netdev_get_regs(struct net_device *dev, u8 *buf)
2347{
2348 int i;
2349 int j;
2350 u32 rfcr;
2351 u32 *rbuf = (u32 *)buf;
2352
2353
2354 for (i = 0; i < NATSEMI_PG0_NREGS; i++) {
2355 rbuf[i] = readl(dev->base_addr + i*4);
2356 }
2357
2358
2359 writew(1, dev->base_addr + PGSEL);
2360 rbuf[i++] = readw(dev->base_addr + PMDCSR);
2361 rbuf[i++] = readw(dev->base_addr + TSTDAT);
2362 rbuf[i++] = readw(dev->base_addr + DSPCFG);
2363 rbuf[i++] = readw(dev->base_addr + SDCFG);
2364 writew(0, dev->base_addr + PGSEL);
2365
2366
2367 rfcr = readl(dev->base_addr + RxFilterAddr);
2368 for (j = 0; j < NATSEMI_RFDR_NREGS; j++) {
2369 writel(j*2, dev->base_addr + RxFilterAddr);
2370 rbuf[i++] = readw(dev->base_addr + RxFilterData);
2371 }
2372 writel(rfcr, dev->base_addr + RxFilterAddr);
2373
2374
2375 if (rbuf[4] & rbuf[5]) {
2376 printk(KERN_WARNING
2377 "%s: shoot, we dropped an interrupt (%#08x)\n",
2378 dev->name, rbuf[4] & rbuf[5]);
2379 }
2380
2381 return 0;
2382}
2383
2384#define SWAP_BITS(x) ( (((x) & 0x0001) << 15) | (((x) & 0x0002) << 13) \
2385 | (((x) & 0x0004) << 11) | (((x) & 0x0008) << 9) \
2386 | (((x) & 0x0010) << 7) | (((x) & 0x0020) << 5) \
2387 | (((x) & 0x0040) << 3) | (((x) & 0x0080) << 1) \
2388 | (((x) & 0x0100) >> 1) | (((x) & 0x0200) >> 3) \
2389 | (((x) & 0x0400) >> 5) | (((x) & 0x0800) >> 7) \
2390 | (((x) & 0x1000) >> 9) | (((x) & 0x2000) >> 11) \
2391 | (((x) & 0x4000) >> 13) | (((x) & 0x8000) >> 15) )
2392
2393static int netdev_get_eeprom(struct net_device *dev, u8 *buf)
2394{
2395 int i;
2396 u16 *ebuf = (u16 *)buf;
2397
2398
2399 for (i = 0; i < NATSEMI_EEPROM_SIZE/2; i++) {
2400 ebuf[i] = eeprom_read(dev->base_addr, i);
2401
2402
2403
2404 ebuf[i] = SWAP_BITS(ebuf[i]);
2405 }
2406 return 0;
2407}
2408
2409static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2410{
2411 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
2412
2413 switch(cmd) {
2414 case SIOCETHTOOL:
2415 return netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
2416 case SIOCGMIIPHY:
2417 case SIOCDEVPRIVATE:
2418 data->phy_id = 1;
2419
2420
2421 case SIOCGMIIREG:
2422 case SIOCDEVPRIVATE+1:
2423 data->val_out = mdio_read(dev, data->phy_id & 0x1f,
2424 data->reg_num & 0x1f);
2425 return 0;
2426
2427 case SIOCSMIIREG:
2428 case SIOCDEVPRIVATE+2:
2429 if (!capable(CAP_NET_ADMIN))
2430 return -EPERM;
2431 mdio_write(dev, data->phy_id & 0x1f, data->reg_num & 0x1f,
2432 data->val_in);
2433 return 0;
2434 default:
2435 return -EOPNOTSUPP;
2436 }
2437}
2438
2439static void enable_wol_mode(struct net_device *dev, int enable_intr)
2440{
2441 long ioaddr = dev->base_addr;
2442 struct netdev_private *np = dev->priv;
2443
2444 if (netif_msg_wol(np))
2445 printk(KERN_INFO "%s: remaining active for wake-on-lan\n",
2446 dev->name);
2447
2448
2449
2450
2451
2452 writel(0, ioaddr + RxRingPtr);
2453
2454
2455 readl(ioaddr + WOLCmd);
2456
2457
2458 writel(np->SavedClkRun | PMEEnable | PMEStatus, ioaddr + ClkRun);
2459
2460
2461 writel(RxOn, ioaddr + ChipCmd);
2462
2463 if (enable_intr) {
2464
2465
2466
2467 writel(WOLPkt | LinkChange, ioaddr + IntrMask);
2468 writel(1, ioaddr + IntrEnable);
2469 }
2470}
2471
2472static int netdev_close(struct net_device *dev)
2473{
2474 long ioaddr = dev->base_addr;
2475 struct netdev_private *np = dev->priv;
2476
2477 if (netif_msg_ifdown(np))
2478 printk(KERN_DEBUG
2479 "%s: Shutting down ethercard, status was %#04x.\n",
2480 dev->name, (int)readl(ioaddr + ChipCmd));
2481 if (netif_msg_pktdata(np))
2482 printk(KERN_DEBUG
2483 "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
2484 dev->name, np->cur_tx, np->dirty_tx,
2485 np->cur_rx, np->dirty_rx);
2486
2487
2488
2489
2490
2491
2492
2493
2494 del_timer_sync(&np->timer);
2495 disable_irq(dev->irq);
2496 spin_lock_irq(&np->lock);
2497
2498 writel(0, ioaddr + IntrEnable);
2499 readl(ioaddr + IntrEnable);
2500 np->hands_off = 1;
2501 spin_unlock_irq(&np->lock);
2502 enable_irq(dev->irq);
2503
2504 free_irq(dev->irq, dev);
2505
2506
2507
2508
2509
2510 spin_lock_irq(&np->lock);
2511 np->hands_off = 0;
2512 readl(ioaddr + IntrMask);
2513 readw(ioaddr + MIntrStatus);
2514
2515
2516 writel(StatsFreeze, ioaddr + StatsCtrl);
2517
2518
2519 natsemi_stop_rxtx(dev);
2520
2521 __get_stats(dev);
2522 spin_unlock_irq(&np->lock);
2523
2524
2525 netif_carrier_off(dev);
2526 netif_stop_queue(dev);
2527
2528 dump_ring(dev);
2529 drain_ring(dev);
2530 free_ring(dev);
2531
2532 {
2533 u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
2534 if (wol) {
2535
2536
2537
2538 enable_wol_mode(dev, 0);
2539 } else {
2540
2541 writel(np->SavedClkRun, ioaddr + ClkRun);
2542 }
2543 }
2544 return 0;
2545}
2546
2547
2548static void __devexit natsemi_remove1 (struct pci_dev *pdev)
2549{
2550 struct net_device *dev = pci_get_drvdata(pdev);
2551
2552 unregister_netdev (dev);
2553 pci_release_regions (pdev);
2554 iounmap ((char *) dev->base_addr);
2555 free_netdev (dev);
2556 pci_set_drvdata(pdev, NULL);
2557}
2558
2559#ifdef CONFIG_PM
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584static int natsemi_suspend (struct pci_dev *pdev, u32 state)
2585{
2586 struct net_device *dev = pci_get_drvdata (pdev);
2587 struct netdev_private *np = dev->priv;
2588 long ioaddr = dev->base_addr;
2589
2590 rtnl_lock();
2591 if (netif_running (dev)) {
2592 del_timer_sync(&np->timer);
2593
2594 disable_irq(dev->irq);
2595 spin_lock_irq(&np->lock);
2596
2597 writel(0, ioaddr + IntrEnable);
2598 np->hands_off = 1;
2599 natsemi_stop_rxtx(dev);
2600 netif_stop_queue(dev);
2601
2602 spin_unlock_irq(&np->lock);
2603 enable_irq(dev->irq);
2604
2605
2606 __get_stats(dev);
2607
2608
2609 drain_ring(dev);
2610 {
2611 u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
2612
2613 if (wol) {
2614
2615
2616
2617
2618 enable_wol_mode(dev, 0);
2619 } else {
2620
2621 writel(np->SavedClkRun, ioaddr + ClkRun);
2622 }
2623 }
2624 }
2625 netif_device_detach(dev);
2626 rtnl_unlock();
2627 return 0;
2628}
2629
2630
2631static int natsemi_resume (struct pci_dev *pdev)
2632{
2633 struct net_device *dev = pci_get_drvdata (pdev);
2634 struct netdev_private *np = dev->priv;
2635
2636 rtnl_lock();
2637 if (netif_device_present(dev))
2638 goto out;
2639 if (netif_running(dev)) {
2640 BUG_ON(!np->hands_off);
2641 pci_enable_device(pdev);
2642
2643
2644 natsemi_reset(dev);
2645 init_ring(dev);
2646 disable_irq(dev->irq);
2647 spin_lock_irq(&np->lock);
2648 np->hands_off = 0;
2649 init_registers(dev);
2650 netif_device_attach(dev);
2651 spin_unlock_irq(&np->lock);
2652 enable_irq(dev->irq);
2653
2654 mod_timer(&np->timer, jiffies + 1*HZ);
2655 }
2656 netif_device_attach(dev);
2657out:
2658 rtnl_unlock();
2659 return 0;
2660}
2661
2662#endif
2663
2664static struct pci_driver natsemi_driver = {
2665 .name = DRV_NAME,
2666 .id_table = natsemi_pci_tbl,
2667 .probe = natsemi_probe1,
2668 .remove = __devexit_p(natsemi_remove1),
2669#ifdef CONFIG_PM
2670 .suspend = natsemi_suspend,
2671 .resume = natsemi_resume,
2672#endif
2673};
2674
2675static int __init natsemi_init_mod (void)
2676{
2677
2678#ifdef MODULE
2679 printk(version);
2680#endif
2681
2682 return pci_module_init (&natsemi_driver);
2683}
2684
2685static void __exit natsemi_exit_mod (void)
2686{
2687 pci_unregister_driver (&natsemi_driver);
2688}
2689
2690module_init(natsemi_init_mod);
2691module_exit(natsemi_exit_mod);
2692
2693