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