ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / fealnx.c
1 /*
2         Written 1998-2000 by Donald Becker.
3
4         This software may be used and distributed according to the terms of
5         the GNU General Public License (GPL), incorporated herein by reference.
6         Drivers based on or derived from this code fall under the GPL and must
7         retain the authorship, copyright and license notice.  This file is not
8         a complete program and may only be used when the entire operating
9         system is licensed under the GPL.
10
11         The author may be reached as becker@scyld.com, or C/O
12         Scyld Computing Corporation
13         410 Severn Ave., Suite 210
14         Annapolis MD 21403
15
16         Support information and updates available at
17         http://www.scyld.com/network/pci-skeleton.html
18
19         Linux kernel updates:
20
21         Version 2.51, Nov 17, 2001 (jgarzik):
22         - Add ethtool support
23         - Replace some MII-related magic numbers with constants
24
25 */
26
27 #define DRV_NAME        "fealnx"
28 #define DRV_VERSION     "2.51"
29 #define DRV_RELDATE     "Nov-17-2001"
30
31 static int debug;               /* 1-> print debug message */
32 static int max_interrupt_work = 20;
33
34 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). */
35 static int multicast_filter_limit = 32;
36
37 /* Set the copy breakpoint for the copy-only-tiny-frames scheme. */
38 /* Setting to > 1518 effectively disables this feature.          */
39 static int rx_copybreak;
40
41 /* Used to pass the media type, etc.                            */
42 /* Both 'options[]' and 'full_duplex[]' should exist for driver */
43 /* interoperability.                                            */
44 /* The media type is usually passed in 'options[]'.             */
45 #define MAX_UNITS 8             /* More are supported, limit only on options */
46 static int options[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
47 static int full_duplex[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
48
49 /* Operational parameters that are set at compile time.                 */
50 /* Keep the ring sizes a power of two for compile efficiency.           */
51 /* The compiler will convert <unsigned>'%'<2^N> into a bit mask.        */
52 /* Making the Tx ring too large decreases the effectiveness of channel  */
53 /* bonding and packet priority.                                         */
54 /* There are no ill effects from too-large receive rings.               */
55 // 88-12-9 modify,
56 // #define TX_RING_SIZE    16
57 // #define RX_RING_SIZE    32
58 #define TX_RING_SIZE    6
59 #define RX_RING_SIZE    12
60 #define TX_TOTAL_SIZE   TX_RING_SIZE*sizeof(struct fealnx_desc)
61 #define RX_TOTAL_SIZE   RX_RING_SIZE*sizeof(struct fealnx_desc)
62
63 /* Operational parameters that usually are not changed. */
64 /* Time in jiffies before concluding the transmitter is hung. */
65 #define TX_TIMEOUT      (2*HZ)
66
67 #define PKT_BUF_SZ      1536    /* Size of each temporary Rx buffer. */
68
69
70 /* Include files, designed to support most kernel versions 2.0.0 and later. */
71 #include <linux/module.h>
72 #include <linux/kernel.h>
73 #include <linux/string.h>
74 #include <linux/timer.h>
75 #include <linux/errno.h>
76 #include <linux/ioport.h>
77 #include <linux/slab.h>
78 #include <linux/interrupt.h>
79 #include <linux/pci.h>
80 #include <linux/netdevice.h>
81 #include <linux/etherdevice.h>
82 #include <linux/skbuff.h>
83 #include <linux/init.h>
84 #include <linux/mii.h>
85 #include <linux/ethtool.h>
86 #include <linux/crc32.h>
87
88 #include <asm/processor.h>      /* Processor type for cache alignment. */
89 #include <asm/bitops.h>
90 #include <asm/io.h>
91 #include <asm/uaccess.h>
92
93 /* These identify the driver base version and may not be removed. */
94 static char version[] __devinitdata =
95 KERN_INFO DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE "\n";
96
97
98 /* This driver was written to use PCI memory space, however some x86 systems
99    work only with I/O space accesses. */
100 #ifndef __alpha__
101 #define USE_IO_OPS
102 #endif
103
104 #ifdef USE_IO_OPS
105 #undef readb
106 #undef readw
107 #undef readl
108 #undef writeb
109 #undef writew
110 #undef writel
111 #define readb inb
112 #define readw inw
113 #define readl inl
114 #define writeb outb
115 #define writew outw
116 #define writel outl
117 #endif
118
119 /* Kernel compatibility defines, some common to David Hinds' PCMCIA package. */
120 /* This is only in the support-all-kernels source code. */
121
122 #define RUN_AT(x) (jiffies + (x))
123
124 MODULE_AUTHOR("Myson or whoever");
125 MODULE_DESCRIPTION("Myson MTD-8xx 100/10M Ethernet PCI Adapter Driver");
126 MODULE_LICENSE("GPL");
127 MODULE_PARM(max_interrupt_work, "i");
128 //MODULE_PARM(min_pci_latency, "i");
129 MODULE_PARM(debug, "i");
130 MODULE_PARM(rx_copybreak, "i");
131 MODULE_PARM(multicast_filter_limit, "i");
132 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
133 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
134 MODULE_PARM_DESC(max_interrupt_work, "fealnx maximum events handled per interrupt");
135 MODULE_PARM_DESC(debug, "fealnx enable debugging (0-1)");
136 MODULE_PARM_DESC(rx_copybreak, "fealnx copy breakpoint for copy-only-tiny-frames");
137 MODULE_PARM_DESC(multicast_filter_limit, "fealnx maximum number of filtered multicast addresses");
138 MODULE_PARM_DESC(options, "fealnx: Bits 0-3: media type, bit 17: full duplex");
139 MODULE_PARM_DESC(full_duplex, "fealnx full duplex setting(s) (1)");
140
141 #define MIN_REGION_SIZE 136
142
143 enum pci_flags_bit {
144         PCI_USES_IO = 1,
145         PCI_USES_MEM = 2,
146         PCI_USES_MASTER = 4,
147         PCI_ADDR0 = 0x10 << 0,
148         PCI_ADDR1 = 0x10 << 1,
149         PCI_ADDR2 = 0x10 << 2,
150         PCI_ADDR3 = 0x10 << 3,
151 };
152
153 /* A chip capabilities table, matching the entries in pci_tbl[] above. */
154 enum chip_capability_flags {
155         HAS_MII_XCVR,
156         HAS_CHIP_XCVR,
157 };
158
159 /* 89/6/13 add, */
160 /* for different PHY */
161 enum phy_type_flags {
162         MysonPHY = 1,
163         AhdocPHY = 2,
164         SeeqPHY = 3,
165         MarvellPHY = 4,
166         Myson981 = 5,
167         LevelOnePHY = 6,
168         OtherPHY = 10,
169 };
170
171 struct chip_info {
172         char *chip_name;
173         int io_size;
174         int flags;
175 };
176
177 static struct chip_info skel_netdrv_tbl[] = {
178         {"100/10M Ethernet PCI Adapter", 136, HAS_MII_XCVR},
179         {"100/10M Ethernet PCI Adapter", 136, HAS_CHIP_XCVR},
180         {"1000/100/10M Ethernet PCI Adapter", 136, HAS_MII_XCVR},
181 };
182
183 /* Offsets to the Command and Status Registers. */
184 enum fealnx_offsets {
185         PAR0 = 0x0,             /* physical address 0-3 */
186         PAR1 = 0x04,            /* physical address 4-5 */
187         MAR0 = 0x08,            /* multicast address 0-3 */
188         MAR1 = 0x0C,            /* multicast address 4-7 */
189         FAR0 = 0x10,            /* flow-control address 0-3 */
190         FAR1 = 0x14,            /* flow-control address 4-5 */
191         TCRRCR = 0x18,          /* receive & transmit configuration */
192         BCR = 0x1C,             /* bus command */
193         TXPDR = 0x20,           /* transmit polling demand */
194         RXPDR = 0x24,           /* receive polling demand */
195         RXCWP = 0x28,           /* receive current word pointer */
196         TXLBA = 0x2C,           /* transmit list base address */
197         RXLBA = 0x30,           /* receive list base address */
198         ISR = 0x34,             /* interrupt status */
199         IMR = 0x38,             /* interrupt mask */
200         FTH = 0x3C,             /* flow control high/low threshold */
201         MANAGEMENT = 0x40,      /* bootrom/eeprom and mii management */
202         TALLY = 0x44,           /* tally counters for crc and mpa */
203         TSR = 0x48,             /* tally counter for transmit status */
204         BMCRSR = 0x4c,          /* basic mode control and status */
205         PHYIDENTIFIER = 0x50,   /* phy identifier */
206         ANARANLPAR = 0x54,      /* auto-negotiation advertisement and link
207                                    partner ability */
208         ANEROCR = 0x58,         /* auto-negotiation expansion and pci conf. */
209         BPREMRPSR = 0x5c,       /* bypass & receive error mask and phy status */
210 };
211
212 /* Bits in the interrupt status/enable registers. */
213 /* The bits in the Intr Status/Enable registers, mostly interrupt sources. */
214 enum intr_status_bits {
215         RFCON = 0x00020000,     /* receive flow control xon packet */
216         RFCOFF = 0x00010000,    /* receive flow control xoff packet */
217         LSCStatus = 0x00008000, /* link status change */
218         ANCStatus = 0x00004000, /* autonegotiation completed */
219         FBE = 0x00002000,       /* fatal bus error */
220         FBEMask = 0x00001800,   /* mask bit12-11 */
221         ParityErr = 0x00000000, /* parity error */
222         TargetErr = 0x00001000, /* target abort */
223         MasterErr = 0x00000800, /* master error */
224         TUNF = 0x00000400,      /* transmit underflow */
225         ROVF = 0x00000200,      /* receive overflow */
226         ETI = 0x00000100,       /* transmit early int */
227         ERI = 0x00000080,       /* receive early int */
228         CNTOVF = 0x00000040,    /* counter overflow */
229         RBU = 0x00000020,       /* receive buffer unavailable */
230         TBU = 0x00000010,       /* transmit buffer unavilable */
231         TI = 0x00000008,        /* transmit interrupt */
232         RI = 0x00000004,        /* receive interrupt */
233         RxErr = 0x00000002,     /* receive error */
234 };
235
236 /* Bits in the NetworkConfig register. */
237 enum rx_mode_bits {
238         RxModeMask = 0xe0,
239         PROM = 0x80,            /* promiscuous mode */
240         AB = 0x40,              /* accept broadcast */
241         AM = 0x20,              /* accept mutlicast */
242         ARP = 0x08,             /* receive runt pkt */
243         ALP = 0x04,             /* receive long pkt */
244         SEP = 0x02,             /* receive error pkt */
245 };
246
247 /* The Tulip Rx and Tx buffer descriptors. */
248 struct fealnx_desc {
249         s32 status;
250         s32 control;
251         u32 buffer;
252         u32 next_desc;
253         struct fealnx_desc *next_desc_logical;
254         struct sk_buff *skbuff;
255         u32 reserved1;
256         u32 reserved2;
257 };
258
259 /* Bits in network_desc.status */
260 enum rx_desc_status_bits {
261         RXOWN = 0x80000000,     /* own bit */
262         FLNGMASK = 0x0fff0000,  /* frame length */
263         FLNGShift = 16,
264         MARSTATUS = 0x00004000, /* multicast address received */
265         BARSTATUS = 0x00002000, /* broadcast address received */
266         PHYSTATUS = 0x00001000, /* physical address received */
267         RXFSD = 0x00000800,     /* first descriptor */
268         RXLSD = 0x00000400,     /* last descriptor */
269         ErrorSummary = 0x80,    /* error summary */
270         RUNT = 0x40,            /* runt packet received */
271         LONG = 0x20,            /* long packet received */
272         FAE = 0x10,             /* frame align error */
273         CRC = 0x08,             /* crc error */
274         RXER = 0x04,            /* receive error */
275 };
276
277 enum rx_desc_control_bits {
278         RXIC = 0x00800000,      /* interrupt control */
279         RBSShift = 0,
280 };
281
282 enum tx_desc_status_bits {
283         TXOWN = 0x80000000,     /* own bit */
284         JABTO = 0x00004000,     /* jabber timeout */
285         CSL = 0x00002000,       /* carrier sense lost */
286         LC = 0x00001000,        /* late collision */
287         EC = 0x00000800,        /* excessive collision */
288         UDF = 0x00000400,       /* fifo underflow */
289         DFR = 0x00000200,       /* deferred */
290         HF = 0x00000100,        /* heartbeat fail */
291         NCRMask = 0x000000ff,   /* collision retry count */
292         NCRShift = 0,
293 };
294
295 enum tx_desc_control_bits {
296         TXIC = 0x80000000,      /* interrupt control */
297         ETIControl = 0x40000000,        /* early transmit interrupt */
298         TXLD = 0x20000000,      /* last descriptor */
299         TXFD = 0x10000000,      /* first descriptor */
300         CRCEnable = 0x08000000, /* crc control */
301         PADEnable = 0x04000000, /* padding control */
302         RetryTxLC = 0x02000000, /* retry late collision */
303         PKTSMask = 0x3ff800,    /* packet size bit21-11 */
304         PKTSShift = 11,
305         TBSMask = 0x000007ff,   /* transmit buffer bit 10-0 */
306         TBSShift = 0,
307 };
308
309 /* BootROM/EEPROM/MII Management Register */
310 #define MASK_MIIR_MII_READ       0x00000000
311 #define MASK_MIIR_MII_WRITE      0x00000008
312 #define MASK_MIIR_MII_MDO        0x00000004
313 #define MASK_MIIR_MII_MDI        0x00000002
314 #define MASK_MIIR_MII_MDC        0x00000001
315
316 /* ST+OP+PHYAD+REGAD+TA */
317 #define OP_READ             0x6000      /* ST:01+OP:10+PHYAD+REGAD+TA:Z0 */
318 #define OP_WRITE            0x5002      /* ST:01+OP:01+PHYAD+REGAD+TA:10 */
319
320 /* ------------------------------------------------------------------------- */
321 /*      Constants for Myson PHY                                              */
322 /* ------------------------------------------------------------------------- */
323 #define MysonPHYID      0xd0000302
324 /* 89-7-27 add, (begin) */
325 #define MysonPHYID0     0x0302
326 #define StatusRegister  18
327 #define SPEED100        0x0400  // bit10
328 #define FULLMODE        0x0800  // bit11
329 /* 89-7-27 add, (end) */
330
331 /* ------------------------------------------------------------------------- */
332 /*      Constants for Seeq 80225 PHY                                         */
333 /* ------------------------------------------------------------------------- */
334 #define SeeqPHYID0      0x0016
335
336 #define MIIRegister18   18
337 #define SPD_DET_100     0x80
338 #define DPLX_DET_FULL   0x40
339
340 /* ------------------------------------------------------------------------- */
341 /*      Constants for Ahdoc 101 PHY                                          */
342 /* ------------------------------------------------------------------------- */
343 #define AhdocPHYID0     0x0022
344
345 #define DiagnosticReg   18
346 #define DPLX_FULL       0x0800
347 #define Speed_100       0x0400
348
349 /* 89/6/13 add, */
350 /* -------------------------------------------------------------------------- */
351 /*      Constants                                                             */
352 /* -------------------------------------------------------------------------- */
353 #define MarvellPHYID0           0x0141
354 #define LevelOnePHYID0          0x0013
355
356 #define MII1000BaseTControlReg  9
357 #define MII1000BaseTStatusReg   10
358 #define SpecificReg             17
359
360 /* for 1000BaseT Control Register */
361 #define PHYAbletoPerform1000FullDuplex  0x0200
362 #define PHYAbletoPerform1000HalfDuplex  0x0100
363 #define PHY1000AbilityMask              0x300
364
365 // for phy specific status register, marvell phy.
366 #define SpeedMask       0x0c000
367 #define Speed_1000M     0x08000
368 #define Speed_100M      0x4000
369 #define Speed_10M       0
370 #define Full_Duplex     0x2000
371
372 // 89/12/29 add, for phy specific status register, levelone phy, (begin)
373 #define LXT1000_100M    0x08000
374 #define LXT1000_1000M   0x0c000
375 #define LXT1000_Full    0x200
376 // 89/12/29 add, for phy specific status register, levelone phy, (end)
377
378 /* for 3-in-1 case */
379 #define PS10            0x00080000
380 #define FD              0x00100000
381 #define PS1000          0x00010000
382 #define LinkIsUp2       0x00040000
383
384 /* for PHY */
385 #define LinkIsUp        0x0004
386
387
388 struct netdev_private {
389         /* Descriptor rings first for alignment. */
390         struct fealnx_desc *rx_ring;
391         struct fealnx_desc *tx_ring;
392
393         dma_addr_t rx_ring_dma;
394         dma_addr_t tx_ring_dma;
395
396         spinlock_t lock;
397
398         struct net_device_stats stats;
399
400         /* Media monitoring timer. */
401         struct timer_list timer;
402
403         /* Frequently used values: keep some adjacent for cache effect. */
404         int flags;
405         struct pci_dev *pci_dev;
406         unsigned long crvalue;
407         unsigned long bcrvalue;
408         unsigned long imrvalue;
409         struct fealnx_desc *cur_rx;
410         struct fealnx_desc *lack_rxbuf;
411         int really_rx_count;
412         struct fealnx_desc *cur_tx;
413         struct fealnx_desc *cur_tx_copy;
414         int really_tx_count;
415         int free_tx_count;
416         unsigned int rx_buf_sz; /* Based on MTU+slack. */
417
418         /* These values are keep track of the transceiver/media in use. */
419         unsigned int linkok;
420         unsigned int line_speed;
421         unsigned int duplexmode;
422         unsigned int default_port:4;    /* Last dev->if_port value. */
423         unsigned int PHYType;
424
425         /* MII transceiver section. */
426         int mii_cnt;            /* MII device addresses. */
427         unsigned char phys[2];  /* MII device addresses. */
428         struct mii_if_info mii;
429 };
430
431
432 static int mdio_read(struct net_device *dev, int phy_id, int location);
433 static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
434 static int netdev_open(struct net_device *dev);
435 static void getlinktype(struct net_device *dev);
436 static void getlinkstatus(struct net_device *dev);
437 static void netdev_timer(unsigned long data);
438 static void tx_timeout(struct net_device *dev);
439 static void init_ring(struct net_device *dev);
440 static int start_tx(struct sk_buff *skb, struct net_device *dev);
441 static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
442 static int netdev_rx(struct net_device *dev);
443 static void set_rx_mode(struct net_device *dev);
444 static struct net_device_stats *get_stats(struct net_device *dev);
445 static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
446 static struct ethtool_ops netdev_ethtool_ops;
447 static int netdev_close(struct net_device *dev);
448 static void reset_rx_descriptors(struct net_device *dev);
449
450 void stop_nic_tx(long ioaddr, long crvalue)
451 {
452         writel(crvalue & (~0x40000), ioaddr + TCRRCR);
453
454         /* wait for tx stop */
455         {
456                 int i = 0, delay = 0x1000;
457
458                 while ((!(readl(ioaddr + TCRRCR) & 0x04000000)) && (i < delay)) {
459                         ++i;
460                 }
461         }
462 }
463
464
465 void stop_nic_rx(long ioaddr, long crvalue)
466 {
467         writel(crvalue & (~0x1), ioaddr + TCRRCR);
468
469         /* wait for rx stop */
470         {
471                 int i = 0, delay = 0x1000;
472
473                 while ((!(readl(ioaddr + TCRRCR) & 0x00008000)) && (i < delay)) {
474                         ++i;
475                 }
476         }
477 }
478
479
480
481 static int __devinit fealnx_init_one(struct pci_dev *pdev,
482                                      const struct pci_device_id *ent)
483 {
484         struct netdev_private *np;
485         int i, option, err, irq;
486         static int card_idx = -1;
487         char boardname[12];
488         long ioaddr;
489         unsigned int chip_id = ent->driver_data;
490         struct net_device *dev;
491         void *ring_space;
492         dma_addr_t ring_dma;
493         
494 /* when built into the kernel, we only print version if device is found */
495 #ifndef MODULE
496         static int printed_version;
497         if (!printed_version++)
498                 printk (version);
499 #endif
500         
501         card_idx++;
502         sprintf(boardname, "fealnx%d", card_idx);
503         
504         option = card_idx < MAX_UNITS ? options[card_idx] : 0;
505
506         i = pci_enable_device(pdev);
507         if (i) return i;
508         pci_set_master(pdev);
509         
510 #ifdef USE_IO_OPS
511         ioaddr = pci_resource_len(pdev, 0);
512 #else
513         ioaddr = pci_resource_len(pdev, 1);
514 #endif
515         if (ioaddr < MIN_REGION_SIZE) {
516                 printk(KERN_ERR "%s: region size %ld too small, aborting\n",
517                        boardname, ioaddr);
518                 return -ENODEV;
519         }
520
521         i = pci_request_regions(pdev, boardname);
522         if (i) return i;
523         
524         irq = pdev->irq;
525
526 #ifdef USE_IO_OPS
527         ioaddr = pci_resource_start(pdev, 0);
528 #else
529         ioaddr = (long) ioremap(pci_resource_start(pdev, 1),
530                                 pci_resource_len(pdev, 1));
531         if (!ioaddr) {
532                 err = -ENOMEM;
533                 goto err_out_res;
534         }
535 #endif
536         
537         dev = alloc_etherdev(sizeof(struct netdev_private));
538         if (!dev) {
539                 err = -ENOMEM;
540                 goto err_out_unmap;
541         }
542         SET_MODULE_OWNER(dev);
543         SET_NETDEV_DEV(dev, &pdev->dev);
544
545         /* read ethernet id */
546         for (i = 0; i < 6; ++i)
547                 dev->dev_addr[i] = readb(ioaddr + PAR0 + i);
548
549         /* Reset the chip to erase previous misconfiguration. */
550         writel(0x00000001, ioaddr + BCR);
551
552         dev->base_addr = ioaddr;
553         dev->irq = irq;
554
555         /* Make certain the descriptor lists are aligned. */
556         np = dev->priv;
557         spin_lock_init(&np->lock);
558         np->pci_dev = pdev;
559         np->flags = skel_netdrv_tbl[chip_id].flags;
560         pci_set_drvdata(pdev, dev);
561         np->mii.dev = dev;
562         np->mii.mdio_read = mdio_read;
563         np->mii.mdio_write = mdio_write;
564         np->mii.phy_id_mask = 0x1f;
565         np->mii.reg_num_mask = 0x1f;
566
567         ring_space = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
568         if (!ring_space) {
569                 err = -ENOMEM;
570                 goto err_out_free_dev;
571         }
572         np->rx_ring = (struct fealnx_desc *)ring_space;
573         np->rx_ring_dma = ring_dma;
574
575         ring_space = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
576         if (!ring_space) {
577                 err = -ENOMEM;
578                 goto err_out_free_rx;
579         }
580         np->tx_ring = (struct fealnx_desc *)ring_space;
581         np->tx_ring_dma = ring_dma;
582
583         /* find the connected MII xcvrs */
584         if (np->flags == HAS_MII_XCVR) {
585                 int phy, phy_idx = 0;
586
587                 for (phy = 1; phy < 32 && phy_idx < 4; phy++) {
588                         int mii_status = mdio_read(dev, phy, 1);
589
590                         if (mii_status != 0xffff && mii_status != 0x0000) {
591                                 np->phys[phy_idx++] = phy;
592                                 printk(KERN_INFO
593                                        "%s: MII PHY found at address %d, status "
594                                        "0x%4.4x.\n", dev->name, phy, mii_status);
595                                 /* get phy type */
596                                 {
597                                         unsigned int data;
598
599                                         data = mdio_read(dev, np->phys[0], 2);
600                                         if (data == SeeqPHYID0)
601                                                 np->PHYType = SeeqPHY;
602                                         else if (data == AhdocPHYID0)
603                                                 np->PHYType = AhdocPHY;
604                                         else if (data == MarvellPHYID0)
605                                                 np->PHYType = MarvellPHY;
606                                         else if (data == MysonPHYID0)
607                                                 np->PHYType = Myson981;
608                                         else if (data == LevelOnePHYID0)
609                                                 np->PHYType = LevelOnePHY;
610                                         else
611                                                 np->PHYType = OtherPHY;
612                                 }
613                         }
614                 }
615
616                 np->mii_cnt = phy_idx;
617                 if (phy_idx == 0) {
618                         printk(KERN_WARNING "%s: MII PHY not found -- this device may "
619                                "not operate correctly.\n", dev->name);
620                 }
621         } else {
622                 np->phys[0] = 32;
623 /* 89/6/23 add, (begin) */
624                 /* get phy type */
625                 if (readl(dev->base_addr + PHYIDENTIFIER) == MysonPHYID)
626                         np->PHYType = MysonPHY;
627                 else
628                         np->PHYType = OtherPHY;
629         }
630         np->mii.phy_id = np->phys[0];
631
632         if (dev->mem_start)
633                 option = dev->mem_start;
634
635         /* The lower four bits are the media type. */
636         if (option > 0) {
637                 if (option & 0x200)
638                         np->mii.full_duplex = 1;
639                 np->default_port = option & 15;
640         }
641
642         if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
643                 np->mii.full_duplex = full_duplex[card_idx];
644
645         if (np->mii.full_duplex) {
646                 printk(KERN_INFO "%s: Media type forced to Full Duplex.\n", dev->name);
647 /* 89/6/13 add, (begin) */
648 //      if (np->PHYType==MarvellPHY)
649                 if ((np->PHYType == MarvellPHY) || (np->PHYType == LevelOnePHY)) {
650                         unsigned int data;
651
652                         data = mdio_read(dev, np->phys[0], 9);
653                         data = (data & 0xfcff) | 0x0200;
654                         mdio_write(dev, np->phys[0], 9, data);
655                 }
656 /* 89/6/13 add, (end) */
657                 if (np->flags == HAS_MII_XCVR)
658                         mdio_write(dev, np->phys[0], MII_ADVERTISE, ADVERTISE_FULL);
659                 else
660                         writel(ADVERTISE_FULL, dev->base_addr + ANARANLPAR);
661                 np->mii.force_media = 1;
662         }
663
664         /* The chip-specific entries in the device structure. */
665         dev->open = &netdev_open;
666         dev->hard_start_xmit = &start_tx;
667         dev->stop = &netdev_close;
668         dev->get_stats = &get_stats;
669         dev->set_multicast_list = &set_rx_mode;
670         dev->do_ioctl = &mii_ioctl;
671         dev->ethtool_ops = &netdev_ethtool_ops;
672         dev->tx_timeout = tx_timeout;
673         dev->watchdog_timeo = TX_TIMEOUT;
674         
675         err = register_netdev(dev);
676         if (err)
677                 goto err_out_free_tx;
678
679         printk(KERN_INFO "%s: %s at 0x%lx, ",
680                dev->name, skel_netdrv_tbl[chip_id].chip_name, ioaddr);
681         for (i = 0; i < 5; i++)
682                 printk("%2.2x:", dev->dev_addr[i]);
683         printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
684
685         return 0;
686
687 err_out_free_tx:
688         pci_free_consistent(pdev, TX_TOTAL_SIZE, np->tx_ring, np->tx_ring_dma);
689 err_out_free_rx:
690         pci_free_consistent(pdev, RX_TOTAL_SIZE, np->rx_ring, np->rx_ring_dma);
691 err_out_free_dev:
692         free_netdev(dev);
693 err_out_unmap:
694 #ifndef USE_IO_OPS
695         iounmap((void *)ioaddr);
696 err_out_res:
697 #endif
698         pci_release_regions(pdev);
699         return err;
700 }
701
702 static void __devexit fealnx_remove_one(struct pci_dev *pdev)
703 {
704         struct net_device *dev = pci_get_drvdata(pdev);
705
706         if (dev) {
707                 struct netdev_private *np = dev->priv;
708
709                 pci_free_consistent(pdev, TX_TOTAL_SIZE, np->tx_ring,
710                         np->tx_ring_dma);
711                 pci_free_consistent(pdev, RX_TOTAL_SIZE, np->rx_ring,
712                         np->rx_ring_dma);
713                 unregister_netdev(dev);
714 #ifndef USE_IO_OPS
715                 iounmap((void *)dev->base_addr);
716 #endif
717                 free_netdev(dev);
718                 pci_release_regions(pdev);
719                 pci_set_drvdata(pdev, NULL);
720         } else
721                 printk(KERN_ERR "fealnx: remove for unknown device\n");
722 }
723
724 unsigned int m80x_read_tick(void)
725 /* function: Reads the Timer tick count register which decrements by 2 from  */
726 /*           65536 to 0 every 1/36.414 of a second. Each 2 decrements of the *//*           count represents 838 nsec's.                                    */
727 /* input   : none.                                                           */
728 /* output  : none.                                                           */
729 {
730         unsigned char tmp;
731         int value;
732
733         writeb((char) 0x06, 0x43);      // Command 8254 to latch T0's count
734
735         // now read the count.
736         tmp = (unsigned char) readb(0x40);
737         value = ((int) tmp) << 8;
738         tmp = (unsigned char) readb(0x40);
739         value |= (((int) tmp) & 0xff);
740         return (value);
741 }
742
743
744 void m80x_delay(unsigned int interval)
745 /* function: to wait for a specified time.                                   */
746 /* input   : interval ... the specified time.                                */
747 /* output  : none.                                                           */
748 {
749         unsigned int interval1, interval2, i = 0;
750
751         interval1 = m80x_read_tick();   // get initial value
752         do {
753                 interval2 = m80x_read_tick();
754                 if (interval1 < interval2)
755                         interval1 = interval2;
756                 ++i;
757         } while (((interval1 - interval2) < (ushort) interval) && (i < 65535));
758 }
759
760
761 static ulong m80x_send_cmd_to_phy(long miiport, int opcode, int phyad, int regad)
762 {
763         ulong miir;
764         int i;
765         unsigned int mask, data;
766
767         /* enable MII output */
768         miir = (ulong) readl(miiport);
769         miir &= 0xfffffff0;
770
771         miir |= MASK_MIIR_MII_WRITE + MASK_MIIR_MII_MDO;
772
773         /* send 32 1's preamble */
774         for (i = 0; i < 32; i++) {
775                 /* low MDC; MDO is already high (miir) */
776                 miir &= ~MASK_MIIR_MII_MDC;
777                 writel(miir, miiport);
778
779                 /* high MDC */
780                 miir |= MASK_MIIR_MII_MDC;
781                 writel(miir, miiport);
782         }
783
784         /* calculate ST+OP+PHYAD+REGAD+TA */
785         data = opcode | (phyad << 7) | (regad << 2);
786
787         /* sent out */
788         mask = 0x8000;
789         while (mask) {
790                 /* low MDC, prepare MDO */
791                 miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
792                 if (mask & data)
793                         miir |= MASK_MIIR_MII_MDO;
794
795                 writel(miir, miiport);
796                 /* high MDC */
797                 miir |= MASK_MIIR_MII_MDC;
798                 writel(miir, miiport);
799                 m80x_delay(30);
800
801                 /* next */
802                 mask >>= 1;
803                 if (mask == 0x2 && opcode == OP_READ)
804                         miir &= ~MASK_MIIR_MII_WRITE;
805         }
806         return miir;
807 }
808
809
810 static int mdio_read(struct net_device *dev, int phyad, int regad)
811 {
812         long miiport = dev->base_addr + MANAGEMENT;
813         ulong miir;
814         unsigned int mask, data;
815
816         miir = m80x_send_cmd_to_phy(miiport, OP_READ, phyad, regad);
817
818         /* read data */
819         mask = 0x8000;
820         data = 0;
821         while (mask) {
822                 /* low MDC */
823                 miir &= ~MASK_MIIR_MII_MDC;
824                 writel(miir, miiport);
825
826                 /* read MDI */
827                 miir = readl(miiport);
828                 if (miir & MASK_MIIR_MII_MDI)
829                         data |= mask;
830
831                 /* high MDC, and wait */
832                 miir |= MASK_MIIR_MII_MDC;
833                 writel(miir, miiport);
834                 m80x_delay((int) 30);
835
836                 /* next */
837                 mask >>= 1;
838         }
839
840         /* low MDC */
841         miir &= ~MASK_MIIR_MII_MDC;
842         writel(miir, miiport);
843
844         return data & 0xffff;
845 }
846
847
848 static void mdio_write(struct net_device *dev, int phyad, int regad, int data)
849 {
850         long miiport = dev->base_addr + MANAGEMENT;
851         ulong miir;
852         unsigned int mask;
853
854         miir = m80x_send_cmd_to_phy(miiport, OP_WRITE, phyad, regad);
855
856         /* write data */
857         mask = 0x8000;
858         while (mask) {
859                 /* low MDC, prepare MDO */
860                 miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
861                 if (mask & data)
862                         miir |= MASK_MIIR_MII_MDO;
863                 writel(miir, miiport);
864
865                 /* high MDC */
866                 miir |= MASK_MIIR_MII_MDC;
867                 writel(miir, miiport);
868
869                 /* next */
870                 mask >>= 1;
871         }
872
873         /* low MDC */
874         miir &= ~MASK_MIIR_MII_MDC;
875         writel(miir, miiport);
876
877         return;
878 }
879
880
881 static int netdev_open(struct net_device *dev)
882 {
883         struct netdev_private *np = dev->priv;
884         long ioaddr = dev->base_addr;
885
886         writel(0x00000001, ioaddr + BCR);       /* Reset */
887
888         if (request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev))
889                 return -EAGAIN;
890
891         init_ring(dev);
892
893         writel(np->rx_ring_dma, ioaddr + RXLBA);
894         writel(np->tx_ring_dma, ioaddr + TXLBA);
895
896         /* Initialize other registers. */
897         /* Configure the PCI bus bursts and FIFO thresholds.
898            486: Set 8 longword burst.
899            586: no burst limit.
900            Burst length 5:3
901            0 0 0   1
902            0 0 1   4
903            0 1 0   8
904            0 1 1   16
905            1 0 0   32
906            1 0 1   64
907            1 1 0   128
908            1 1 1   256
909            Wait the specified 50 PCI cycles after a reset by initializing
910            Tx and Rx queues and the address filter list.
911            FIXME (Ueimor): optimistic for alpha + posted writes ? */
912 #if defined(__powerpc__) || defined(__sparc__)
913 // 89/9/1 modify, 
914 //   np->bcrvalue=0x04 | 0x0x38;  /* big-endian, 256 burst length */
915         np->bcrvalue = 0x04 | 0x10;     /* big-endian, tx 8 burst length */
916         np->crvalue = 0xe00;    /* rx 128 burst length */
917 #elif defined(__alpha__) || defined(__x86_64__)
918 // 89/9/1 modify, 
919 //   np->bcrvalue=0x38;           /* little-endian, 256 burst length */
920         np->bcrvalue = 0x10;    /* little-endian, 8 burst length */
921         np->crvalue = 0xe00;    /* rx 128 burst length */
922 #elif defined(__i386__)
923 #if defined(MODULE)
924 // 89/9/1 modify, 
925 //   np->bcrvalue=0x38;           /* little-endian, 256 burst length */
926         np->bcrvalue = 0x10;    /* little-endian, 8 burst length */
927         np->crvalue = 0xe00;    /* rx 128 burst length */
928 #else
929         /* When not a module we can work around broken '486 PCI boards. */
930 #define x86 boot_cpu_data.x86
931 // 89/9/1 modify, 
932 //   np->bcrvalue=(x86 <= 4 ? 0x10 : 0x38);
933         np->bcrvalue = 0x10;
934         np->crvalue = (x86 <= 4 ? 0xa00 : 0xe00);
935         if (x86 <= 4)
936                 printk(KERN_INFO "%s: This is a 386/486 PCI system, setting burst "
937                        "length to %x.\n", dev->name, (x86 <= 4 ? 0x10 : 0x38));
938 #endif
939 #else
940 // 89/9/1 modify,
941 //   np->bcrvalue=0x38;
942         np->bcrvalue = 0x10;
943         np->crvalue = 0xe00;    /* rx 128 burst length */
944 #warning Processor architecture undefined!
945 #endif
946 // 89/12/29 add,
947 // 90/1/16 modify,
948 //   np->imrvalue=FBE|TUNF|CNTOVF|RBU|TI|RI;
949         np->imrvalue = TUNF | CNTOVF | RBU | TI | RI;
950         if (np->pci_dev->device == 0x891) {
951                 np->bcrvalue |= 0x200;  /* set PROG bit */
952                 np->crvalue |= 0x02000000;      /* set enhanced bit */
953                 np->imrvalue |= ETI;
954         }
955         writel(np->bcrvalue, ioaddr + BCR);
956
957         if (dev->if_port == 0)
958                 dev->if_port = np->default_port;
959
960         writel(0, dev->base_addr + RXPDR);
961 // 89/9/1 modify,
962 //   np->crvalue = 0x00e40001;    /* tx store and forward, tx/rx enable */
963         np->crvalue |= 0x00e40001;      /* tx store and forward, tx/rx enable */
964         np->mii.full_duplex = np->mii.force_media;
965         getlinkstatus(dev);
966         if (np->linkok)
967                 getlinktype(dev);
968         set_rx_mode(dev);
969
970         netif_start_queue(dev);
971
972         /* Clear and Enable interrupts by setting the interrupt mask. */
973         writel(FBE | TUNF | CNTOVF | RBU | TI | RI, ioaddr + ISR);
974         writel(np->imrvalue, ioaddr + IMR);
975
976         if (debug)
977                 printk(KERN_DEBUG "%s: Done netdev_open().\n", dev->name);
978
979         /* Set the timer to check for link beat. */
980         init_timer(&np->timer);
981         np->timer.expires = RUN_AT(3 * HZ);
982         np->timer.data = (unsigned long) dev;
983         np->timer.function = &netdev_timer;
984
985         /* timer handler */
986         add_timer(&np->timer);
987
988         return 0;
989 }
990
991
992 static void getlinkstatus(struct net_device *dev)
993 /* function: Routine will read MII Status Register to get link status.       */
994 /* input   : dev... pointer to the adapter block.                            */
995 /* output  : none.                                                           */
996 {
997         struct netdev_private *np = dev->priv;
998         unsigned int i, DelayTime = 0x1000;
999
1000         np->linkok = 0;
1001
1002         if (np->PHYType == MysonPHY) {
1003                 for (i = 0; i < DelayTime; ++i) {
1004                         if (readl(dev->base_addr + BMCRSR) & LinkIsUp2) {
1005                                 np->linkok = 1;
1006                                 return;
1007                         }
1008                         // delay
1009                         m80x_delay(100);
1010                 }
1011         } else {
1012                 for (i = 0; i < DelayTime; ++i) {
1013                         if (mdio_read(dev, np->phys[0], MII_BMSR) & BMSR_LSTATUS) {
1014                                 np->linkok = 1;
1015                                 return;
1016                         }
1017                         // delay
1018                         m80x_delay(100);
1019                 }
1020         }
1021 }
1022
1023
1024 static void getlinktype(struct net_device *dev)
1025 {
1026         struct netdev_private *np = dev->priv;
1027
1028         if (np->PHYType == MysonPHY) {  /* 3-in-1 case */
1029                 if (readl(dev->base_addr + TCRRCR) & FD)
1030                         np->duplexmode = 2;     /* full duplex */
1031                 else
1032                         np->duplexmode = 1;     /* half duplex */
1033                 if (readl(dev->base_addr + TCRRCR) & PS10)
1034                         np->line_speed = 1;     /* 10M */
1035                 else
1036                         np->line_speed = 2;     /* 100M */
1037         } else {
1038                 if (np->PHYType == SeeqPHY) {   /* this PHY is SEEQ 80225 */
1039                         unsigned int data;
1040
1041                         data = mdio_read(dev, np->phys[0], MIIRegister18);
1042                         if (data & SPD_DET_100)
1043                                 np->line_speed = 2;     /* 100M */
1044                         else
1045                                 np->line_speed = 1;     /* 10M */
1046                         if (data & DPLX_DET_FULL)
1047                                 np->duplexmode = 2;     /* full duplex mode */
1048                         else
1049                                 np->duplexmode = 1;     /* half duplex mode */
1050                 } else if (np->PHYType == AhdocPHY) {
1051                         unsigned int data;
1052
1053                         data = mdio_read(dev, np->phys[0], DiagnosticReg);
1054                         if (data & Speed_100)
1055                                 np->line_speed = 2;     /* 100M */
1056                         else
1057                                 np->line_speed = 1;     /* 10M */
1058                         if (data & DPLX_FULL)
1059                                 np->duplexmode = 2;     /* full duplex mode */
1060                         else
1061                                 np->duplexmode = 1;     /* half duplex mode */
1062                 }
1063 /* 89/6/13 add, (begin) */
1064                 else if (np->PHYType == MarvellPHY) {
1065                         unsigned int data;
1066
1067                         data = mdio_read(dev, np->phys[0], SpecificReg);
1068                         if (data & Full_Duplex)
1069                                 np->duplexmode = 2;     /* full duplex mode */
1070                         else
1071                                 np->duplexmode = 1;     /* half duplex mode */
1072                         data &= SpeedMask;
1073                         if (data == Speed_1000M)
1074                                 np->line_speed = 3;     /* 1000M */
1075                         else if (data == Speed_100M)
1076                                 np->line_speed = 2;     /* 100M */
1077                         else
1078                                 np->line_speed = 1;     /* 10M */
1079                 }
1080 /* 89/6/13 add, (end) */
1081 /* 89/7/27 add, (begin) */
1082                 else if (np->PHYType == Myson981) {
1083                         unsigned int data;
1084
1085                         data = mdio_read(dev, np->phys[0], StatusRegister);
1086
1087                         if (data & SPEED100)
1088                                 np->line_speed = 2;
1089                         else
1090                                 np->line_speed = 1;
1091
1092                         if (data & FULLMODE)
1093                                 np->duplexmode = 2;
1094                         else
1095                                 np->duplexmode = 1;
1096                 }
1097 /* 89/7/27 add, (end) */
1098 /* 89/12/29 add */
1099                 else if (np->PHYType == LevelOnePHY) {
1100                         unsigned int data;
1101
1102                         data = mdio_read(dev, np->phys[0], SpecificReg);
1103                         if (data & LXT1000_Full)
1104                                 np->duplexmode = 2;     /* full duplex mode */
1105                         else
1106                                 np->duplexmode = 1;     /* half duplex mode */
1107                         data &= SpeedMask;
1108                         if (data == LXT1000_1000M)
1109                                 np->line_speed = 3;     /* 1000M */
1110                         else if (data == LXT1000_100M)
1111                                 np->line_speed = 2;     /* 100M */
1112                         else
1113                                 np->line_speed = 1;     /* 10M */
1114                 }
1115                 // chage crvalue
1116                 // np->crvalue&=(~PS10)&(~FD);
1117                 np->crvalue &= (~PS10) & (~FD) & (~PS1000);
1118                 if (np->line_speed == 1)
1119                         np->crvalue |= PS10;
1120                 else if (np->line_speed == 3)
1121                         np->crvalue |= PS1000;
1122                 if (np->duplexmode == 2)
1123                         np->crvalue |= FD;
1124         }
1125 }
1126
1127
1128 static void allocate_rx_buffers(struct net_device *dev)
1129 {
1130         struct netdev_private *np = dev->priv;
1131
1132         /*  allocate skb for rx buffers */
1133         while (np->really_rx_count != RX_RING_SIZE) {
1134                 struct sk_buff *skb;
1135
1136                 skb = dev_alloc_skb(np->rx_buf_sz);
1137                 np->lack_rxbuf->skbuff = skb;
1138
1139                 if (skb == NULL)
1140                         break;  /* Better luck next round. */
1141
1142                 skb->dev = dev; /* Mark as being used by this device. */
1143                 np->lack_rxbuf->buffer = pci_map_single(np->pci_dev, skb->tail,
1144                         np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1145                 np->lack_rxbuf = np->lack_rxbuf->next_desc_logical;
1146                 ++np->really_rx_count;
1147         }
1148 }
1149
1150
1151 static void netdev_timer(unsigned long data)
1152 {
1153         struct net_device *dev = (struct net_device *) data;
1154         struct netdev_private *np = dev->priv;
1155         long ioaddr = dev->base_addr;
1156         int next_tick = 10 * HZ;
1157         int old_crvalue = np->crvalue;
1158         unsigned int old_linkok = np->linkok;
1159
1160         if (debug)
1161                 printk(KERN_DEBUG "%s: Media selection timer tick, status %8.8x "
1162                        "config %8.8x.\n", dev->name, readl(ioaddr + ISR),
1163                        readl(ioaddr + TCRRCR));
1164
1165         if (np->flags == HAS_MII_XCVR) {
1166                 getlinkstatus(dev);
1167                 if ((old_linkok == 0) && (np->linkok == 1)) {   /* we need to detect the media type again */
1168                         getlinktype(dev);
1169                         if (np->crvalue != old_crvalue) {
1170                                 stop_nic_tx(ioaddr, np->crvalue);
1171                                 stop_nic_rx(ioaddr, np->crvalue & (~0x40000));
1172                                 writel(np->crvalue, ioaddr + TCRRCR);
1173                         }
1174                 }
1175         }
1176
1177         allocate_rx_buffers(dev);
1178
1179         np->timer.expires = RUN_AT(next_tick);
1180         add_timer(&np->timer);
1181 }
1182
1183
1184 static void tx_timeout(struct net_device *dev)
1185 {
1186         struct netdev_private *np = dev->priv;
1187         long ioaddr = dev->base_addr;
1188         int i;
1189
1190         printk(KERN_WARNING "%s: Transmit timed out, status %8.8x,"
1191                " resetting...\n", dev->name, readl(ioaddr + ISR));
1192
1193         {
1194
1195                 printk(KERN_DEBUG "  Rx ring %p: ", np->rx_ring);
1196                 for (i = 0; i < RX_RING_SIZE; i++)
1197                         printk(" %8.8x", (unsigned int) np->rx_ring[i].status);
1198                 printk("\n" KERN_DEBUG "  Tx ring %p: ", np->tx_ring);
1199                 for (i = 0; i < TX_RING_SIZE; i++)
1200                         printk(" %4.4x", np->tx_ring[i].status);
1201                 printk("\n");
1202         }
1203
1204         /* Reinit. Gross */
1205
1206         /* Reset the chip's Tx and Rx processes. */
1207         stop_nic_tx(ioaddr, 0);
1208         reset_rx_descriptors(dev);
1209
1210         /* Disable interrupts by clearing the interrupt mask. */
1211         writel(0x0000, ioaddr + IMR);
1212
1213         /* Reset the chip to erase previous misconfiguration. */
1214         writel(0x00000001, ioaddr + BCR);
1215
1216         /* Ueimor: wait for 50 PCI cycles (and flush posted writes btw). 
1217            We surely wait too long (address+data phase). Who cares ? */
1218         for (i = 0; i < 50; i++) {
1219                 readl(ioaddr + BCR);
1220                 rmb();
1221         }
1222
1223         writel((np->cur_tx - np->tx_ring)*sizeof(struct fealnx_desc) +
1224                 np->tx_ring_dma, ioaddr + TXLBA);
1225         writel((np->cur_rx - np->rx_ring)*sizeof(struct fealnx_desc) +
1226                 np->rx_ring_dma, ioaddr + RXLBA);
1227
1228         writel(np->bcrvalue, ioaddr + BCR);
1229
1230         writel(0, dev->base_addr + RXPDR);
1231         set_rx_mode(dev);
1232         /* Clear and Enable interrupts by setting the interrupt mask. */
1233         writel(FBE | TUNF | CNTOVF | RBU | TI | RI, ioaddr + ISR);
1234         writel(np->imrvalue, ioaddr + IMR);
1235
1236         writel(0, dev->base_addr + TXPDR);
1237
1238         dev->trans_start = jiffies;
1239         np->stats.tx_errors++;
1240
1241         return;
1242 }
1243
1244
1245 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
1246 static void init_ring(struct net_device *dev)
1247 {
1248         struct netdev_private *np = dev->priv;
1249         int i;
1250
1251         /* initialize rx variables */
1252         np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
1253         np->cur_rx = &np->rx_ring[0];
1254         np->lack_rxbuf = NULL;
1255         np->really_rx_count = 0;
1256
1257         /* initial rx descriptors. */
1258         for (i = 0; i < RX_RING_SIZE; i++) {
1259                 np->rx_ring[i].status = 0;
1260                 np->rx_ring[i].control = np->rx_buf_sz << RBSShift;
1261                 np->rx_ring[i].next_desc = np->rx_ring_dma +
1262                         (i + 1)*sizeof(struct fealnx_desc);
1263                 np->rx_ring[i].next_desc_logical = &np->rx_ring[i + 1];
1264                 np->rx_ring[i].skbuff = NULL;
1265         }
1266
1267         /* for the last rx descriptor */
1268         np->rx_ring[i - 1].next_desc = np->rx_ring_dma;
1269         np->rx_ring[i - 1].next_desc_logical = np->rx_ring;
1270
1271         /* allocate skb for rx buffers */
1272         for (i = 0; i < RX_RING_SIZE; i++) {
1273                 struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
1274
1275                 if (skb == NULL) {
1276                         np->lack_rxbuf = &np->rx_ring[i];
1277                         break;
1278                 }
1279
1280                 ++np->really_rx_count;
1281                 np->rx_ring[i].skbuff = skb;
1282                 skb->dev = dev; /* Mark as being used by this device. */
1283                 np->rx_ring[i].buffer = pci_map_single(np->pci_dev, skb->tail,
1284                         np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1285                 np->rx_ring[i].status = RXOWN;
1286                 np->rx_ring[i].control |= RXIC;
1287         }
1288
1289         /* initialize tx variables */
1290         np->cur_tx = &np->tx_ring[0];
1291         np->cur_tx_copy = &np->tx_ring[0];
1292         np->really_tx_count = 0;
1293         np->free_tx_count = TX_RING_SIZE;
1294
1295         for (i = 0; i < TX_RING_SIZE; i++) {
1296                 np->tx_ring[i].status = 0;
1297                 np->tx_ring[i].next_desc = np->tx_ring_dma +
1298                         (i + 1)*sizeof(struct fealnx_desc);
1299                 np->tx_ring[i].next_desc_logical = &np->tx_ring[i + 1];
1300                 np->tx_ring[i].skbuff = NULL;
1301         }
1302
1303         /* for the last tx descriptor */
1304         np->tx_ring[i - 1].next_desc = np->tx_ring_dma;
1305         np->tx_ring[i - 1].next_desc_logical = &np->tx_ring[0];
1306 }
1307
1308
1309 static int start_tx(struct sk_buff *skb, struct net_device *dev)
1310 {
1311         struct netdev_private *np = dev->priv;
1312         unsigned long flags;
1313
1314         spin_lock_irqsave(&np->lock, flags);
1315
1316         np->cur_tx_copy->skbuff = skb;
1317
1318 #define one_buffer
1319 #define BPT 1022
1320 #if defined(one_buffer)
1321         np->cur_tx_copy->buffer = pci_map_single(np->pci_dev, skb->data,
1322                 skb->len, PCI_DMA_TODEVICE);
1323         np->cur_tx_copy->control = TXIC | TXLD | TXFD | CRCEnable | PADEnable;
1324         np->cur_tx_copy->control |= (skb->len << PKTSShift);    /* pkt size */
1325         np->cur_tx_copy->control |= (skb->len << TBSShift);     /* buffer size */
1326 // 89/12/29 add,
1327         if (np->pci_dev->device == 0x891)
1328                 np->cur_tx_copy->control |= ETIControl | RetryTxLC;
1329         np->cur_tx_copy->status = TXOWN;
1330         np->cur_tx_copy = np->cur_tx_copy->next_desc_logical;
1331         --np->free_tx_count;
1332 #elif defined(two_buffer)
1333         if (skb->len > BPT) {
1334                 struct fealnx_desc *next;
1335
1336                 /* for the first descriptor */
1337                 np->cur_tx_copy->buffer = pci_map_single(np->pci_dev, skb->data,
1338                         BPT, PCI_DMA_TODEVICE);
1339                 np->cur_tx_copy->control = TXIC | TXFD | CRCEnable | PADEnable;
1340                 np->cur_tx_copy->control |= (skb->len << PKTSShift);    /* pkt size */
1341                 np->cur_tx_copy->control |= (BPT << TBSShift);  /* buffer size */
1342
1343                 /* for the last descriptor */
1344                 next = (struct fealnx *) np->cur_tx_copy.next_desc_logical;
1345                 next->skbuff = skb;
1346                 next->control = TXIC | TXLD | CRCEnable | PADEnable;
1347                 next->control |= (skb->len << PKTSShift);       /* pkt size */
1348                 next->control |= ((skb->len - BPT) << TBSShift);        /* buf size */
1349 // 89/12/29 add,
1350                 if (np->pci_dev->device == 0x891)
1351                         np->cur_tx_copy->control |= ETIControl | RetryTxLC;
1352                 next->buffer = pci_map_single(ep->pci_dev, skb->data + BPT,
1353                                 skb->len - BPT, PCI_DMA_TODEVICE);
1354
1355                 next->status = TXOWN;
1356                 np->cur_tx_copy->status = TXOWN;
1357
1358                 np->cur_tx_copy = next->next_desc_logical;
1359                 np->free_tx_count -= 2;
1360         } else {
1361                 np->cur_tx_copy->buffer = pci_map_single(np->pci_dev, skb->data,
1362                         skb->len, PCI_DMA_TODEVICE);
1363                 np->cur_tx_copy->control = TXIC | TXLD | TXFD | CRCEnable | PADEnable;
1364                 np->cur_tx_copy->control |= (skb->len << PKTSShift);    /* pkt size */
1365                 np->cur_tx_copy->control |= (skb->len << TBSShift);     /* buffer size */
1366 // 89/12/29 add,
1367                 if (np->pci_dev->device == 0x891)
1368                         np->cur_tx_copy->control |= ETIControl | RetryTxLC;
1369                 np->cur_tx_copy->status = TXOWN;
1370                 np->cur_tx_copy = np->cur_tx_copy->next_desc_logical;
1371                 --np->free_tx_count;
1372         }
1373 #endif
1374
1375         if (np->free_tx_count < 2)
1376                 netif_stop_queue(dev);
1377         ++np->really_tx_count;
1378         writel(0, dev->base_addr + TXPDR);
1379         dev->trans_start = jiffies;
1380
1381         spin_unlock_irqrestore(&np->lock, flags);
1382         return 0;
1383 }
1384
1385
1386 void free_one_rx_descriptor(struct netdev_private *np)
1387 {
1388         if (np->really_rx_count == RX_RING_SIZE)
1389                 np->cur_rx->status = RXOWN;
1390         else {
1391                 np->lack_rxbuf->skbuff = np->cur_rx->skbuff;
1392                 np->lack_rxbuf->buffer = np->cur_rx->buffer;
1393                 np->lack_rxbuf->status = RXOWN;
1394                 ++np->really_rx_count;
1395                 np->lack_rxbuf = np->lack_rxbuf->next_desc_logical;
1396         }
1397         np->cur_rx = np->cur_rx->next_desc_logical;
1398 }
1399
1400
1401 void reset_rx_descriptors(struct net_device *dev)
1402 {
1403         struct netdev_private *np = dev->priv;
1404
1405         stop_nic_rx(dev->base_addr, np->crvalue);
1406
1407         while (!(np->cur_rx->status & RXOWN))
1408                 free_one_rx_descriptor(np);
1409
1410         allocate_rx_buffers(dev);
1411
1412         writel(np->rx_ring_dma + (np->cur_rx - np->rx_ring),
1413                 dev->base_addr + RXLBA);
1414         writel(np->crvalue, dev->base_addr + TCRRCR);
1415 }
1416
1417
1418 /* The interrupt handler does all of the Rx thread work and cleans up
1419    after the Tx thread. */
1420 static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
1421 {
1422         struct net_device *dev = (struct net_device *) dev_instance;
1423         struct netdev_private *np = dev->priv;
1424         long ioaddr, boguscnt = max_interrupt_work;
1425         unsigned int num_tx = 0;
1426         int handled = 0;
1427
1428         spin_lock(&np->lock);
1429
1430         writel(0, dev->base_addr + IMR);
1431
1432         ioaddr = dev->base_addr;
1433         np = dev->priv;
1434
1435         do {
1436                 u32 intr_status = readl(ioaddr + ISR);
1437
1438                 /* Acknowledge all of the current interrupt sources ASAP. */
1439                 writel(intr_status, ioaddr + ISR);
1440
1441                 if (debug)
1442                         printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n", dev->name,
1443                                intr_status);
1444
1445                 if (!(intr_status & np->imrvalue))
1446                         break;
1447
1448                 handled = 1;
1449
1450 // 90/1/16 delete,
1451 //
1452 //      if (intr_status & FBE)
1453 //      {   /* fatal error */
1454 //          stop_nic_tx(ioaddr, 0);
1455 //          stop_nic_rx(ioaddr, 0);
1456 //          break;
1457 //      };
1458
1459                 if (intr_status & TUNF)
1460                         writel(0, ioaddr + TXPDR);
1461
1462                 if (intr_status & CNTOVF) {
1463                         /* missed pkts */
1464                         np->stats.rx_missed_errors += readl(ioaddr + TALLY) & 0x7fff;
1465
1466                         /* crc error */
1467                         np->stats.rx_crc_errors +=
1468                             (readl(ioaddr + TALLY) & 0x7fff0000) >> 16;
1469                 }
1470
1471                 if (intr_status & (RI | RBU)) {
1472                         if (intr_status & RI)
1473                                 netdev_rx(dev);
1474                         else
1475                                 reset_rx_descriptors(dev);
1476                 }
1477
1478                 while (np->really_tx_count) {
1479                         long tx_status = np->cur_tx->status;
1480                         long tx_control = np->cur_tx->control;
1481
1482                         if (!(tx_control & TXLD)) {     /* this pkt is combined by two tx descriptors */
1483                                 struct fealnx_desc *next;
1484
1485                                 next = np->cur_tx->next_desc_logical;
1486                                 tx_status = next->status;
1487                                 tx_control = next->control;
1488                         }
1489
1490                         if (tx_status & TXOWN)
1491                                 break;
1492
1493                         if (!(np->crvalue & 0x02000000)) {
1494                                 if (tx_status & (CSL | LC | EC | UDF | HF)) {
1495                                         np->stats.tx_errors++;
1496                                         if (tx_status & EC)
1497                                                 np->stats.tx_aborted_errors++;
1498                                         if (tx_status & CSL)
1499                                                 np->stats.tx_carrier_errors++;
1500                                         if (tx_status & LC)
1501                                                 np->stats.tx_window_errors++;
1502                                         if (tx_status & UDF)
1503                                                 np->stats.tx_fifo_errors++;
1504                                         if ((tx_status & HF) && np->mii.full_duplex == 0)
1505                                                 np->stats.tx_heartbeat_errors++;
1506
1507                                 } else {
1508                                         np->stats.tx_bytes +=
1509                                             ((tx_control & PKTSMask) >> PKTSShift);
1510
1511                                         np->stats.collisions +=
1512                                             ((tx_status & NCRMask) >> NCRShift);
1513                                         np->stats.tx_packets++;
1514                                 }
1515                         } else {
1516                                 np->stats.tx_bytes +=
1517                                     ((tx_control & PKTSMask) >> PKTSShift);
1518                                 np->stats.tx_packets++;
1519                         }
1520
1521                         /* Free the original skb. */
1522                         pci_unmap_single(np->pci_dev, np->cur_tx->buffer,
1523                                 np->cur_tx->skbuff->len, PCI_DMA_TODEVICE);
1524                         dev_kfree_skb_irq(np->cur_tx->skbuff);
1525                         np->cur_tx->skbuff = NULL;
1526                         --np->really_tx_count;
1527                         if (np->cur_tx->control & TXLD) {
1528                                 np->cur_tx = np->cur_tx->next_desc_logical;
1529                                 ++np->free_tx_count;
1530                         } else {
1531                                 np->cur_tx = np->cur_tx->next_desc_logical;
1532                                 np->cur_tx = np->cur_tx->next_desc_logical;
1533                                 np->free_tx_count += 2;
1534                         }
1535                         num_tx++;
1536                 }               /* end of for loop */
1537                 
1538                 if (num_tx && np->free_tx_count >= 2)
1539                         netif_wake_queue(dev);
1540
1541                 /* read transmit status for enhanced mode only */
1542                 if (np->crvalue & 0x02000000) {
1543                         long data;
1544
1545                         data = readl(ioaddr + TSR);
1546                         np->stats.tx_errors += (data & 0xff000000) >> 24;
1547                         np->stats.tx_aborted_errors += (data & 0xff000000) >> 24;
1548                         np->stats.tx_window_errors += (data & 0x00ff0000) >> 16;
1549                         np->stats.collisions += (data & 0x0000ffff);
1550                 }
1551
1552                 if (--boguscnt < 0) {
1553                         printk(KERN_WARNING "%s: Too much work at interrupt, "
1554                                "status=0x%4.4x.\n", dev->name, intr_status);
1555                         break;
1556                 }
1557         } while (1);
1558
1559         /* read the tally counters */
1560         /* missed pkts */
1561         np->stats.rx_missed_errors += readl(ioaddr + TALLY) & 0x7fff;
1562
1563         /* crc error */
1564         np->stats.rx_crc_errors += (readl(ioaddr + TALLY) & 0x7fff0000) >> 16;
1565
1566         if (debug)
1567                 printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
1568                        dev->name, readl(ioaddr + ISR));
1569
1570         writel(np->imrvalue, ioaddr + IMR);
1571
1572         spin_unlock(&np->lock);
1573
1574         return IRQ_RETVAL(handled);
1575 }
1576
1577
1578 /* This routine is logically part of the interrupt handler, but separated
1579    for clarity and better register allocation. */
1580 static int netdev_rx(struct net_device *dev)
1581 {
1582         struct netdev_private *np = dev->priv;
1583
1584         /* If EOP is set on the next entry, it's a new packet. Send it up. */
1585         while (!(np->cur_rx->status & RXOWN)) {
1586                 s32 rx_status = np->cur_rx->status;
1587
1588                 if (np->really_rx_count == 0)
1589                         break;
1590
1591                 if (debug)
1592                         printk(KERN_DEBUG "  netdev_rx() status was %8.8x.\n", rx_status);
1593
1594                 if ((!((rx_status & RXFSD) && (rx_status & RXLSD)))
1595                     || (rx_status & ErrorSummary)) {
1596                         if (rx_status & ErrorSummary) { /* there was a fatal error */
1597                                 if (debug)
1598                                         printk(KERN_DEBUG
1599                                                "%s: Receive error, Rx status %8.8x.\n",
1600                                                dev->name, rx_status);
1601
1602                                 np->stats.rx_errors++;  /* end of a packet. */
1603                                 if (rx_status & (LONG | RUNT))
1604                                         np->stats.rx_length_errors++;
1605                                 if (rx_status & RXER)
1606                                         np->stats.rx_frame_errors++;
1607                                 if (rx_status & CRC)
1608                                         np->stats.rx_crc_errors++;
1609                         } else {
1610                                 int need_to_reset = 0;
1611                                 int desno = 0;
1612
1613                                 if (rx_status & RXFSD) {        /* this pkt is too long, over one rx buffer */
1614                                         struct fealnx_desc *cur;
1615
1616                                         /* check this packet is received completely? */
1617                                         cur = np->cur_rx;
1618                                         while (desno <= np->really_rx_count) {
1619                                                 ++desno;
1620                                                 if ((!(cur->status & RXOWN))
1621                                                     && (cur->status & RXLSD))
1622                                                         break;
1623                                                 /* goto next rx descriptor */
1624                                                 cur = cur->next_desc_logical;
1625                                         }
1626                                         if (desno > np->really_rx_count)
1627                                                 need_to_reset = 1;
1628                                 } else  /* RXLSD did not find, something error */
1629                                         need_to_reset = 1;
1630
1631                                 if (need_to_reset == 0) {
1632                                         int i;
1633
1634                                         np->stats.rx_length_errors++;
1635
1636                                         /* free all rx descriptors related this long pkt */
1637                                         for (i = 0; i < desno; ++i)
1638                                                 free_one_rx_descriptor(np);
1639                                         continue;
1640                                 } else {        /* something error, need to reset this chip */
1641                                         reset_rx_descriptors(dev);
1642                                 }
1643                                 break;  /* exit the while loop */
1644                         }
1645                 } else {        /* this received pkt is ok */
1646
1647                         struct sk_buff *skb;
1648                         /* Omit the four octet CRC from the length. */
1649                         short pkt_len = ((rx_status & FLNGMASK) >> FLNGShift) - 4;
1650
1651 #ifndef final_version
1652                         if (debug)
1653                                 printk(KERN_DEBUG "  netdev_rx() normal Rx pkt length %d"
1654                                        " status %x.\n", pkt_len, rx_status);
1655 #endif
1656
1657                         /* Check if the packet is long enough to accept without copying
1658                            to a minimally-sized skbuff. */
1659                         if (pkt_len < rx_copybreak &&
1660                             (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1661                                 skb->dev = dev;
1662                                 skb_reserve(skb, 2);    /* 16 byte align the IP header */
1663                                 pci_dma_sync_single_for_cpu(np->pci_dev,
1664                                                             np->cur_rx->buffer,
1665                                                             np->rx_buf_sz,
1666                                                             PCI_DMA_FROMDEVICE);
1667                                 /* Call copy + cksum if available. */
1668
1669 #if ! defined(__alpha__)
1670                                 eth_copy_and_sum(skb, 
1671                                         np->cur_rx->skbuff->tail, pkt_len, 0);
1672                                 skb_put(skb, pkt_len);
1673 #else
1674                                 memcpy(skb_put(skb, pkt_len),
1675                                         np->cur_rx->skbuff->tail, pkt_len);
1676 #endif
1677                                 pci_dma_sync_single_for_device(np->pci_dev,
1678                                                                np->cur_rx->buffer,
1679                                                                np->rx_buf_sz,
1680                                                                PCI_DMA_FROMDEVICE);
1681                         } else {
1682                                 pci_unmap_single(np->pci_dev,
1683                                                  np->cur_rx->buffer,
1684                                                  np->rx_buf_sz,
1685                                                  PCI_DMA_FROMDEVICE);
1686                                 skb_put(skb = np->cur_rx->skbuff, pkt_len);
1687                                 np->cur_rx->skbuff = NULL;
1688                                 if (np->really_rx_count == RX_RING_SIZE)
1689                                         np->lack_rxbuf = np->cur_rx;
1690                                 --np->really_rx_count;
1691                         }
1692                         skb->protocol = eth_type_trans(skb, dev);
1693                         netif_rx(skb);
1694                         dev->last_rx = jiffies;
1695                         np->stats.rx_packets++;
1696                         np->stats.rx_bytes += pkt_len;
1697                 }
1698
1699                 if (np->cur_rx->skbuff == NULL) {
1700                         struct sk_buff *skb;
1701
1702                         skb = dev_alloc_skb(np->rx_buf_sz);
1703
1704                         if (skb != NULL) {
1705                                 skb->dev = dev; /* Mark as being used by this device. */
1706                                 np->cur_rx->buffer = pci_map_single(np->pci_dev,
1707                                                                     skb->tail,
1708                                                                     np->rx_buf_sz,
1709                                                                     PCI_DMA_FROMDEVICE);
1710                                 np->cur_rx->skbuff = skb;
1711                                 ++np->really_rx_count;
1712                         }
1713                 }
1714
1715                 if (np->cur_rx->skbuff != NULL)
1716                         free_one_rx_descriptor(np);
1717         }                       /* end of while loop */
1718
1719         /*  allocate skb for rx buffers */
1720         allocate_rx_buffers(dev);
1721
1722         return 0;
1723 }
1724
1725
1726 static struct net_device_stats *get_stats(struct net_device *dev)
1727 {
1728         long ioaddr = dev->base_addr;
1729         struct netdev_private *np = dev->priv;
1730
1731         /* The chip only need report frame silently dropped. */
1732         if (netif_running(dev)) {
1733                 np->stats.rx_missed_errors += readl(ioaddr + TALLY) & 0x7fff;
1734                 np->stats.rx_crc_errors += (readl(ioaddr + TALLY) & 0x7fff0000) >> 16;
1735         }
1736
1737         return &np->stats;
1738 }
1739
1740 static void set_rx_mode(struct net_device *dev)
1741 {
1742         struct netdev_private *np = dev->priv;
1743         long ioaddr = dev->base_addr;
1744         u32 mc_filter[2];       /* Multicast hash filter */
1745         u32 rx_mode;
1746
1747         if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1748                 /* Unconditionally log net taps. */
1749                 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
1750                 memset(mc_filter, 0xff, sizeof(mc_filter));
1751                 rx_mode = PROM | AB | AM;
1752         } else if ((dev->mc_count > multicast_filter_limit)
1753                    || (dev->flags & IFF_ALLMULTI)) {
1754                 /* Too many to match, or accept all multicasts. */
1755                 memset(mc_filter, 0xff, sizeof(mc_filter));
1756                 rx_mode = AB | AM;
1757         } else {
1758                 struct dev_mc_list *mclist;
1759                 int i;
1760
1761                 memset(mc_filter, 0, sizeof(mc_filter));
1762                 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1763                      i++, mclist = mclist->next) {
1764                         unsigned int bit;
1765                         bit = (ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26) ^ 0x3F;
1766                         mc_filter[bit >> 5] |= (1 << bit);
1767                 }
1768                 rx_mode = AB | AM;
1769         }
1770
1771         stop_nic_tx(ioaddr, np->crvalue);
1772         stop_nic_rx(ioaddr, np->crvalue & (~0x40000));
1773
1774         writel(mc_filter[0], ioaddr + MAR0);
1775         writel(mc_filter[1], ioaddr + MAR1);
1776         np->crvalue &= ~RxModeMask;
1777         np->crvalue |= rx_mode;
1778         writel(np->crvalue, ioaddr + TCRRCR);
1779 }
1780
1781 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1782 {
1783         struct netdev_private *np = dev->priv;
1784
1785         strcpy (info->driver, DRV_NAME);
1786         strcpy (info->version, DRV_VERSION);
1787         strcpy (info->bus_info, pci_name(np->pci_dev));
1788 }
1789
1790 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1791 {
1792         struct netdev_private *np = dev->priv;
1793         int rc;
1794
1795         spin_lock_irq(&np->lock);
1796         rc = mii_ethtool_gset(&np->mii, cmd);
1797         spin_unlock_irq(&np->lock);
1798
1799         return rc;
1800 }
1801
1802 static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1803 {
1804         struct netdev_private *np = dev->priv;
1805         int rc;
1806
1807         spin_lock_irq(&np->lock);
1808         rc = mii_ethtool_sset(&np->mii, cmd);
1809         spin_unlock_irq(&np->lock);
1810
1811         return rc;
1812 }
1813
1814 static int netdev_nway_reset(struct net_device *dev)
1815 {
1816         struct netdev_private *np = dev->priv;
1817         return mii_nway_restart(&np->mii);
1818 }
1819
1820 static u32 netdev_get_link(struct net_device *dev)
1821 {
1822         struct netdev_private *np = dev->priv;
1823         return mii_link_ok(&np->mii);
1824 }
1825
1826 static u32 netdev_get_msglevel(struct net_device *dev)
1827 {
1828         return debug;
1829 }
1830
1831 static void netdev_set_msglevel(struct net_device *dev, u32 value)
1832 {
1833         debug = value;
1834 }
1835
1836 static struct ethtool_ops netdev_ethtool_ops = {
1837         .get_drvinfo            = netdev_get_drvinfo,
1838         .get_settings           = netdev_get_settings,
1839         .set_settings           = netdev_set_settings,
1840         .nway_reset             = netdev_nway_reset,
1841         .get_link               = netdev_get_link,
1842         .get_msglevel           = netdev_get_msglevel,
1843         .set_msglevel           = netdev_set_msglevel,
1844         .get_sg                 = ethtool_op_get_sg,
1845         .get_tx_csum            = ethtool_op_get_tx_csum,
1846 };
1847
1848 static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1849 {
1850         struct netdev_private *np = dev->priv;
1851         struct mii_ioctl_data *data = (struct mii_ioctl_data *) & rq->ifr_data;
1852         int rc;
1853
1854         if (!netif_running(dev))
1855                 return -EINVAL;
1856
1857         spin_lock_irq(&np->lock);
1858         rc = generic_mii_ioctl(&np->mii, data, cmd, NULL);
1859         spin_unlock_irq(&np->lock);
1860
1861         return rc;
1862 }
1863
1864
1865 static int netdev_close(struct net_device *dev)
1866 {
1867         long ioaddr = dev->base_addr;
1868         struct netdev_private *np = dev->priv;
1869         int i;
1870
1871         netif_stop_queue(dev);
1872
1873         /* Disable interrupts by clearing the interrupt mask. */
1874         writel(0x0000, ioaddr + IMR);
1875
1876         /* Stop the chip's Tx and Rx processes. */
1877         stop_nic_tx(ioaddr, 0);
1878         stop_nic_rx(ioaddr, 0);
1879
1880         del_timer_sync(&np->timer);
1881
1882         free_irq(dev->irq, dev);
1883
1884         /* Free all the skbuffs in the Rx queue. */
1885         for (i = 0; i < RX_RING_SIZE; i++) {
1886                 struct sk_buff *skb = np->rx_ring[i].skbuff;
1887
1888                 np->rx_ring[i].status = 0;
1889                 if (skb) {
1890                         pci_unmap_single(np->pci_dev, np->rx_ring[i].buffer,
1891                                 np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1892                         dev_kfree_skb(skb);
1893                         np->rx_ring[i].skbuff = NULL;
1894                 }
1895         }
1896
1897         for (i = 0; i < TX_RING_SIZE; i++) {
1898                 struct sk_buff *skb = np->tx_ring[i].skbuff;
1899
1900                 if (skb) {
1901                         pci_unmap_single(np->pci_dev, np->tx_ring[i].buffer,
1902                                 skb->len, PCI_DMA_TODEVICE);
1903                         dev_kfree_skb(skb);
1904                         np->tx_ring[i].skbuff = NULL;
1905                 }
1906         }
1907
1908         return 0;
1909 }
1910
1911 static struct pci_device_id fealnx_pci_tbl[] = {
1912         {0x1516, 0x0800, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1913         {0x1516, 0x0803, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
1914         {0x1516, 0x0891, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
1915         {} /* terminate list */
1916 };
1917 MODULE_DEVICE_TABLE(pci, fealnx_pci_tbl);
1918
1919
1920 static struct pci_driver fealnx_driver = {
1921         .name           = "fealnx",
1922         .id_table       = fealnx_pci_tbl,
1923         .probe          = fealnx_init_one,
1924         .remove         = __devexit_p(fealnx_remove_one),
1925 };
1926
1927 static int __init fealnx_init(void)
1928 {
1929 /* when a module, this is printed whether or not devices are found in probe */
1930 #ifdef MODULE
1931         printk (version);
1932 #endif
1933
1934         return pci_module_init(&fealnx_driver);
1935 }
1936
1937 static void __exit fealnx_exit(void)
1938 {
1939         pci_unregister_driver(&fealnx_driver);
1940 }
1941
1942 module_init(fealnx_init);
1943 module_exit(fealnx_exit);