00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 static const char version1[] =
00036 "via-rhine.c:v1.08b-LK1.0.1 12/14/2000 Written by Donald Becker\n";
00037 static const char version2[] =
00038 " http://www.scyld.com/network/via-rhine.html\n";
00039
00040
00041
00042
00043 static int debug = 1;
00044 static int max_interrupt_work = 20;
00045 static int min_pci_latency = 32;
00046
00047
00048
00049 static int rx_copybreak = 0;
00050
00051
00052
00053
00054
00055
00056 #define MAX_UNITS 8
00057 static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
00058 static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
00059
00060
00061
00062 static const int multicast_filter_limit = 32;
00063
00064
00065
00066
00067
00068
00069
00070
00071 #define TX_RING_SIZE 16
00072 #define TX_QUEUE_LEN 10
00073 #define RX_RING_SIZE 16
00074
00075
00076
00077 #define TX_TIMEOUT (2*HZ)
00078
00079 #define PKT_BUF_SZ 1536
00080
00081
00082 #if !defined(__OPTIMIZE__)
00083 #warning You must compile this file with the correct options!
00084 #warning See the last lines of the source file.
00085 #error You must compile this driver with "-O".
00086 #endif
00087
00088 #include <linux/version.h>
00089 #include <linux/module.h>
00090 #include <linux/kernel.h>
00091 #include <linux/string.h>
00092 #include <linux/timer.h>
00093 #include <linux/errno.h>
00094 #include <linux/ioport.h>
00095 #include <linux/malloc.h>
00096 #include <linux/interrupt.h>
00097 #include <linux/pci.h>
00098 #include <linux/netdevice.h>
00099 #include <linux/etherdevice.h>
00100 #include <linux/skbuff.h>
00101 #include <linux/init.h>
00102 #include <linux/delay.h>
00103 #include <asm/processor.h>
00104 #include <asm/bitops.h>
00105 #include <asm/io.h>
00106 #include <asm/irq.h>
00107
00108
00109 #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
00110 #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
00111
00112
00113
00114 #if defined(VIA_USE_MEMORY)
00115 #warning Many adapters using the VIA Rhine chip are not configured to work
00116 #warning with PCI memory space accesses.
00117 #else
00118 #define USE_IO_OPS
00119 #undef readb
00120 #undef readw
00121 #undef readl
00122 #undef writeb
00123 #undef writew
00124 #undef writel
00125 #define readb inb
00126 #define readw inw
00127 #define readl inl
00128 #define writeb outb
00129 #define writew outw
00130 #define writel outl
00131 #endif
00132
00133 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
00134 MODULE_DESCRIPTION("VIA Rhine PCI Fast Ethernet driver");
00135 MODULE_PARM(max_interrupt_work, "i");
00136 MODULE_PARM(min_pci_latency, "i");
00137 MODULE_PARM(debug, "i");
00138 MODULE_PARM(rx_copybreak, "i");
00139 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
00140 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
00141
00142 #define NETSTATS_VER2
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 enum pci_flags_bit {
00241 PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
00242 PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
00243 };
00244
00245 #if defined(VIA_USE_MEMORY)
00246 #define RHINE_IOTYPE (PCI_USES_MEM | PCI_USES_MASTER | PCI_ADDR0)
00247 #define RHINEII_IOSIZE 4096
00248 #else
00249 #define RHINE_IOTYPE (PCI_USES_IO | PCI_USES_MASTER | PCI_ADDR1)
00250 #define RHINEII_IOSIZE 256
00251 #endif
00252
00253 struct pci_id_info {
00254 const char *name;
00255 u16 vendor_id, device_id, device_id_mask, flags;
00256 int io_size;
00257 struct device *(*probe1)(int pci_bus, int pci_devfn, struct device *dev,
00258 long ioaddr, int irq, int chip_idx, int fnd_cnt);
00259 };
00260
00261 static struct device *via_probe1(int pci_bus, int pci_devfn,
00262 struct device *dev, long ioaddr, int irq,
00263 int chp_idx, int fnd_cnt);
00264
00265 enum via_rhine_chips {
00266 VT86C100A = 0,
00267 VT6102,
00268 VT3043,
00269 };
00270
00271
00272 static struct pci_id_info pci_tbl[] __initdata = {
00273 { "VIA VT86C100A Rhine-II", 0x1106, 0x6100, 0xffff,
00274 RHINE_IOTYPE, 128, via_probe1},
00275 { "VIA VT6102 Rhine-II", 0x1106, 0x3065, 0xffff,
00276 RHINE_IOTYPE, RHINEII_IOSIZE, via_probe1},
00277 { "VIA VT3043 Rhine", 0x1106, 0x3043, 0xffff,
00278 RHINE_IOTYPE, 128, via_probe1},
00279 {0,},
00280 };
00281
00282
00283
00284 enum chip_capability_flags {
00285 CanHaveMII=1, HasESIPhy=2, HasDavicomPhy=4,
00286 ReqTxAlign=0x10, HasWOL=0x20,
00287 };
00288
00289 struct chip_info {
00290 int io_size;
00291 int flags;
00292 } static cap_tbl[] __initdata = {
00293 {128, CanHaveMII | ReqTxAlign, },
00294 {128, CanHaveMII | HasWOL, },
00295 {128, CanHaveMII | ReqTxAlign, },
00296 };
00297
00298
00299
00300 enum register_offsets {
00301 StationAddr=0x00, RxConfig=0x06, TxConfig=0x07, ChipCmd=0x08,
00302 IntrStatus=0x0C, IntrEnable=0x0E,
00303 MulticastFilter0=0x10, MulticastFilter1=0x14,
00304 RxRingPtr=0x18, TxRingPtr=0x1C,
00305 MIIPhyAddr=0x6C, MIIStatus=0x6D, PCIBusConfig=0x6E,
00306 MIICmd=0x70, MIIRegAddr=0x71, MIIData=0x72,
00307 Config=0x78, ConfigA=0x7A, RxMissed=0x7C, RxCRCErrs=0x7E,
00308 StickyHW=0x83, WOLcrClr=0xA4, WOLcgClr=0xA7, PwrcsrClr=0xAC,
00309 };
00310
00311
00312 enum intr_status_bits {
00313 IntrRxDone=0x0001, IntrRxErr=0x0004, IntrRxEmpty=0x0020,
00314 IntrTxDone=0x0002, IntrTxAbort=0x0008, IntrTxUnderrun=0x0010,
00315 IntrPCIErr=0x0040,
00316 IntrStatsMax=0x0080, IntrRxEarly=0x0100, IntrMIIChange=0x0200,
00317 IntrRxOverflow=0x0400, IntrRxDropped=0x0800, IntrRxNoBuf=0x1000,
00318 IntrTxAborted=0x2000, IntrLinkChange=0x4000,
00319 IntrRxWakeUp=0x8000,
00320 IntrNormalSummary=0x0003, IntrAbnormalSummary=0xC260,
00321 };
00322
00323
00324 struct rx_desc {
00325 s32 rx_status;
00326 u32 desc_length;
00327 u32 addr;
00328 u32 next_desc;
00329 };
00330 struct tx_desc {
00331 s32 tx_status;
00332 u32 desc_length;
00333 u32 addr;
00334 u32 next_desc;
00335 };
00336
00337
00338 enum rx_status_bits {
00339 RxOK=0x8000, RxWholePkt=0x0300, RxErr=0x008F
00340 };
00341 enum desc_status_bits {
00342 DescOwn=0x80000000, DescEndPacket=0x4000, DescIntr=0x1000,
00343 };
00344
00345
00346 enum chip_cmd_bits {
00347 CmdInit=0x0001, CmdStart=0x0002, CmdStop=0x0004, CmdRxOn=0x0008,
00348 CmdTxOn=0x0010, CmdTxDemand=0x0020, CmdRxDemand=0x0040,
00349 CmdEarlyRx=0x0100, CmdEarlyTx=0x0200, CmdFDuplex=0x0400,
00350 CmdNoTxPoll=0x0800, CmdReset=0x8000,
00351 };
00352
00353 #define PRIV_ALIGN 15
00354 struct netdev_private {
00355
00356 struct rx_desc rx_ring[RX_RING_SIZE];
00357 struct tx_desc tx_ring[TX_RING_SIZE];
00358
00359 struct sk_buff* rx_skbuff[RX_RING_SIZE];
00360
00361 struct sk_buff* tx_skbuff[TX_RING_SIZE];
00362 unsigned char *tx_buf[TX_RING_SIZE];
00363 unsigned char *tx_bufs;
00364 struct device *next_module;
00365 void *priv_addr;
00366 struct net_device_stats stats;
00367 struct timer_list timer;
00368 unsigned char pci_bus, pci_devfn;
00369
00370 int chip_id, drv_flags;
00371 struct rx_desc *rx_head_desc;
00372 unsigned int cur_rx, dirty_rx;
00373 unsigned int cur_tx, dirty_tx;
00374 unsigned int rx_buf_sz;
00375 u16 chip_cmd;
00376 unsigned int tx_full:1;
00377
00378 unsigned int full_duplex:1;
00379 unsigned int duplex_lock:1;
00380 unsigned int medialock:1;
00381 unsigned int default_port:4;
00382 u8 tx_thresh, rx_thresh;
00383
00384 int mii_cnt;
00385 u16 advertising;
00386 unsigned char phys[2];
00387 };
00388
00389 static int mdio_read(struct device *dev, int phy_id, int location);
00390 static void mdio_write(struct device *dev, int phy_id, int location, int value);
00391 static int netdev_open(struct device *dev);
00392 static void check_duplex(struct device *dev);
00393 static void netdev_timer(unsigned long data);
00394 static void tx_timeout(struct device *dev);
00395 static int start_tx(struct sk_buff *skb, struct device *dev);
00396 static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
00397 static int netdev_rx(struct device *dev);
00398 static void netdev_error(struct device *dev, int intr_status);
00399 static void set_rx_mode(struct device *dev);
00400 static struct net_device_stats *get_stats(struct device *dev);
00401 static int mii_ioctl(struct device *dev, struct ifreq *rq, int cmd);
00402 static int netdev_close(struct device *dev);
00403 static inline void clear_tally_counters(long ioaddr);
00404
00405
00406
00407
00408 static struct device *root_net_dev = NULL;
00409
00410 static void wait_for_reset(struct device *dev, char *name)
00411 {
00412 struct netdev_private *np = dev->priv;
00413 long ioaddr = dev->base_addr;
00414 int chip_id = np->chip_id;
00415 int i;
00416
00417
00418 if (chip_id == VT3043 || chip_id == VT86C100A)
00419 udelay(100);
00420
00421 i = 0;
00422 do {
00423 udelay(5);
00424 i++;
00425 if(i > 2000) {
00426 printk(KERN_ERR "%s: reset did not complete in 10 ms.\n", name);
00427 break;
00428 }
00429 } while(readw(ioaddr + ChipCmd) & CmdReset);
00430 if (debug > 1)
00431 printk(KERN_INFO "%s: reset finished after %d microseconds.\n",
00432 name, 5*i);
00433 }
00434
00435
00436
00437
00438
00439
00440 static int __init pci_etherdev_probe(struct device *dev, struct pci_id_info pci_tbl[])
00441 {
00442 int cards_found = 0;
00443 int pci_index = 0;
00444 unsigned char pci_bus, pci_device_fn;
00445
00446 if ( ! pcibios_present())
00447 return -ENODEV;
00448
00449 for (;pci_index < 0xff; pci_index++) {
00450 u16 vendor, device, pci_command, new_command;
00451 int chip_idx, irq;
00452 long pciaddr;
00453 long ioaddr;
00454
00455 if (pcibios_find_class (PCI_CLASS_NETWORK_ETHERNET << 8, pci_index,
00456 &pci_bus, &pci_device_fn)
00457 != PCIBIOS_SUCCESSFUL)
00458 break;
00459 pcibios_read_config_word(pci_bus, pci_device_fn,
00460 PCI_VENDOR_ID, &vendor);
00461 pcibios_read_config_word(pci_bus, pci_device_fn,
00462 PCI_DEVICE_ID, &device);
00463
00464 for (chip_idx = 0; pci_tbl[chip_idx].vendor_id; chip_idx++)
00465 if (vendor == pci_tbl[chip_idx].vendor_id
00466 && (device & pci_tbl[chip_idx].device_id_mask) ==
00467 pci_tbl[chip_idx].device_id)
00468 break;
00469 if (pci_tbl[chip_idx].vendor_id == 0)
00470 continue;
00471
00472 {
00473 struct pci_dev *pdev = pci_find_slot(pci_bus, pci_device_fn);
00474 #ifdef USE_IO_OPS
00475 pciaddr = pdev->base_address[0];
00476 #else
00477 pciaddr = pdev->base_address[1];
00478 #endif
00479 irq = pdev->irq;
00480 }
00481
00482 if (debug > 2)
00483 printk(KERN_INFO "Found %s at PCI address %#lx, IRQ %d.\n",
00484 pci_tbl[chip_idx].name, pciaddr, irq);
00485
00486 if (pci_tbl[chip_idx].flags & PCI_USES_IO) {
00487 ioaddr = pciaddr & ~3;
00488 if (check_region(ioaddr, pci_tbl[chip_idx].io_size))
00489 continue;
00490 } else if ((ioaddr = (long)ioremap(pciaddr & ~0xf,
00491 pci_tbl[chip_idx].io_size)) == 0) {
00492 printk(KERN_INFO "Failed to map PCI address %#lx.\n",
00493 pciaddr);
00494 continue;
00495 }
00496
00497 pcibios_read_config_word(pci_bus, pci_device_fn,
00498 PCI_COMMAND, &pci_command);
00499 new_command = pci_command | (pci_tbl[chip_idx].flags & 7);
00500 if (pci_command != new_command) {
00501 printk(KERN_INFO " The PCI BIOS has not enabled the"
00502 " device at %d/%d! Updating PCI command %4.4x->%4.4x.\n",
00503 pci_bus, pci_device_fn, pci_command, new_command);
00504 pcibios_write_config_word(pci_bus, pci_device_fn,
00505 PCI_COMMAND, new_command);
00506 }
00507
00508 dev = pci_tbl[chip_idx].probe1(pci_bus, pci_device_fn, dev, ioaddr,
00509 irq, chip_idx, cards_found);
00510
00511 if (dev && (pci_tbl[chip_idx].flags & PCI_COMMAND_MASTER)) {
00512 u8 pci_latency;
00513 pcibios_read_config_byte(pci_bus, pci_device_fn,
00514 PCI_LATENCY_TIMER, &pci_latency);
00515 if (pci_latency < min_pci_latency) {
00516 printk(KERN_INFO " PCI latency timer (CFLT) is "
00517 "unreasonably low at %d. Setting to %d clocks.\n",
00518 pci_latency, min_pci_latency);
00519 pcibios_write_config_byte(pci_bus, pci_device_fn,
00520 PCI_LATENCY_TIMER, min_pci_latency);
00521 }
00522 }
00523 dev = 0;
00524 cards_found++;
00525 }
00526
00527 return cards_found ? 0 : -ENODEV;
00528 }
00529
00530 #ifndef MODULE
00531 int __init via_rhine_probe(struct device *dev)
00532 {
00533 static int did_version = 0;
00534 if (!did_version++)
00535 printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
00536 return pci_etherdev_probe(dev, pci_tbl);
00537 }
00538 #endif
00539
00540 static struct device * __init via_probe1(int pci_bus, int pci_devfn,
00541 struct device *dev, long ioaddr, int irq,
00542 int chip_id, int card_idx)
00543 {
00544 struct netdev_private *np;
00545 void *priv_mem;
00546 int i, option = card_idx < MAX_UNITS ? options[card_idx] : 0;
00547
00548 dev = init_etherdev(dev, 0);
00549
00550 printk(KERN_INFO "%s: %s at 0x%lx, ",
00551 dev->name, pci_tbl[chip_id].name, ioaddr);
00552
00553
00554 for (i = 0; i < 6; i++)
00555 dev->dev_addr[i] = readb(ioaddr + StationAddr + i);
00556 for (i = 0; i < 5; i++)
00557 printk("%2.2x:", dev->dev_addr[i]);
00558 printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
00559
00560
00561
00562 priv_mem = kmalloc(sizeof(*np) + PRIV_ALIGN, GFP_KERNEL);
00563 if (priv_mem == NULL)
00564 return NULL;
00565
00566 #ifdef USE_IO_OPS
00567 request_region(ioaddr, pci_tbl[chip_id].io_size, dev->name);
00568 #endif
00569
00570
00571 writew(CmdReset, ioaddr + ChipCmd);
00572
00573 dev->base_addr = ioaddr;
00574 dev->irq = irq;
00575
00576
00577 dev->priv = np = (void *)(((long)priv_mem + PRIV_ALIGN) & ~PRIV_ALIGN);
00578 memset(np, 0, sizeof(*np));
00579 np->priv_addr = priv_mem;
00580
00581 np->next_module = root_net_dev;
00582 root_net_dev = dev;
00583
00584 np->pci_bus = pci_bus;
00585 np->pci_devfn = pci_devfn;
00586 np->chip_id = chip_id;
00587 np->drv_flags = cap_tbl[chip_id].flags;
00588
00589 if (dev->mem_start)
00590 option = dev->mem_start;
00591
00592
00593 if (option > 0) {
00594 if (option & 0x200)
00595 np->full_duplex = 1;
00596 np->default_port = option & 15;
00597 if (np->default_port)
00598 np->medialock = 1;
00599 }
00600 if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
00601 np->full_duplex = 1;
00602
00603 if (np->full_duplex)
00604 np->duplex_lock = 1;
00605
00606
00607 dev->open = &netdev_open;
00608 dev->hard_start_xmit = &start_tx;
00609 dev->stop = &netdev_close;
00610 dev->get_stats = &get_stats;
00611 dev->set_multicast_list = &set_rx_mode;
00612 dev->do_ioctl = &mii_ioctl;
00613
00614 if (np->drv_flags & CanHaveMII) {
00615 int phy, phy_idx = 0;
00616 np->phys[0] = 1;
00617 for (phy = 1; phy < 32 && phy_idx < 4; phy++) {
00618 int mii_status = mdio_read(dev, phy, 1);
00619 if (mii_status != 0xffff && mii_status != 0x0000) {
00620 np->phys[phy_idx++] = phy;
00621 np->advertising = mdio_read(dev, phy, 4);
00622 printk(KERN_INFO "%s: MII PHY found at address %d, status "
00623 "0x%4.4x advertising %4.4x Link %4.4x.\n",
00624 dev->name, phy, mii_status, np->advertising,
00625 mdio_read(dev, phy, 5));
00626 }
00627 }
00628 np->mii_cnt = phy_idx;
00629 }
00630
00631 return dev;
00632 }
00633
00634 static void alloc_rbufs(struct device *dev)
00635 {
00636 struct netdev_private *np = (struct netdev_private *)dev->priv;
00637 int i;
00638
00639 np->cur_rx = 0;
00640 np->dirty_rx = 0;
00641
00642 np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
00643 np->rx_head_desc = &np->rx_ring[0];
00644
00645 for (i = 0; i < RX_RING_SIZE; i++) {
00646 np->rx_ring[i].rx_status = 0;
00647 np->rx_ring[i].desc_length = cpu_to_le32(np->rx_buf_sz);
00648 np->rx_ring[i].next_desc = virt_to_le32desc(&np->rx_ring[i+1]);
00649 np->rx_skbuff[i] = 0;
00650 }
00651
00652 np->rx_ring[i-1].next_desc = virt_to_le32desc(&np->rx_ring[0]);
00653
00654
00655 for (i = 0; i < RX_RING_SIZE; i++) {
00656 struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
00657 np->rx_skbuff[i] = skb;
00658 if (skb == NULL)
00659 break;
00660 skb->dev = dev;
00661 np->rx_ring[i].addr = virt_to_le32desc(skb->tail);
00662 np->rx_ring[i].rx_status = cpu_to_le32(DescOwn);
00663 }
00664 np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
00665 }
00666
00667 static void free_rbufs(struct device* dev)
00668 {
00669 struct netdev_private *np = (struct netdev_private *)dev->priv;
00670 int i;
00671
00672
00673 for (i = 0; i < RX_RING_SIZE; i++) {
00674 np->rx_ring[i].rx_status = 0;
00675 np->rx_ring[i].addr = 0xBADF00D0;
00676 if (np->rx_skbuff[i]) {
00677 #if LINUX_VERSION_CODE < 0x20100
00678 np->rx_skbuff[i]->free = 1;
00679 #endif
00680 dev_kfree_skb(np->rx_skbuff[i]);
00681 }
00682 np->rx_skbuff[i] = 0;
00683 }
00684 }
00685
00686 static void alloc_tbufs(struct device* dev)
00687 {
00688 struct netdev_private *np = (struct netdev_private *)dev->priv;
00689 int i;
00690
00691 np->tx_full = 0;
00692 np->cur_tx = 0;
00693 np->dirty_tx = 0;
00694
00695 for (i = 0; i < TX_RING_SIZE; i++) {
00696 np->tx_skbuff[i] = 0;
00697 np->tx_ring[i].tx_status = 0;
00698 np->tx_ring[i].desc_length = cpu_to_le32(0x00e08000);
00699 np->tx_ring[i].next_desc = virt_to_le32desc(&np->tx_ring[i+1]);
00700 np->tx_buf[i] = kmalloc(PKT_BUF_SZ, GFP_KERNEL);
00701 }
00702 np->tx_ring[i-1].next_desc = virt_to_le32desc(&np->tx_ring[0]);
00703 }
00704
00705 static void free_tbufs(struct device *dev)
00706 {
00707 struct netdev_private *np = (struct netdev_private *)dev->priv;
00708 int i;
00709
00710 for (i = 0; i < TX_RING_SIZE; i++) {
00711 if (np->tx_skbuff[i])
00712 dev_kfree_skb(np->tx_skbuff[i]);
00713 np->tx_skbuff[i] = 0;
00714 if (np->tx_buf[i]) {
00715 kfree(np->tx_buf[i]);
00716 np->tx_buf[i] = 0;
00717 }
00718 }
00719 }
00720
00721 static void init_registers(struct device *dev)
00722 {
00723 struct netdev_private *np = (struct netdev_private *)dev->priv;
00724 long ioaddr = dev->base_addr;
00725 int i;
00726
00727 for (i = 0; i < 6; i++)
00728 writeb(dev->dev_addr[i], ioaddr + StationAddr + i);
00729
00730
00731 writew(0x0006, ioaddr + PCIBusConfig);
00732
00733 writeb(0x20, ioaddr + TxConfig);
00734 np->tx_thresh = 0x20;
00735 np->rx_thresh = 0x60;
00736
00737 if (dev->if_port == 0)
00738 dev->if_port = np->default_port;
00739
00740 dev->tbusy = 0;
00741 dev->interrupt = 0;
00742
00743 writel(virt_to_bus(np->rx_ring), ioaddr + RxRingPtr);
00744 writel(virt_to_bus(np->tx_ring), ioaddr + TxRingPtr);
00745
00746 set_rx_mode(dev);
00747
00748 dev->start = 1;
00749
00750
00751 writew(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow| IntrRxDropped|
00752 IntrTxDone | IntrTxAbort | IntrTxUnderrun |
00753 IntrPCIErr | IntrStatsMax | IntrLinkChange | IntrMIIChange,
00754 ioaddr + IntrEnable);
00755
00756 np->chip_cmd = CmdStart|CmdTxOn|CmdRxOn|CmdNoTxPoll;
00757 if (np->duplex_lock)
00758 np->chip_cmd |= CmdFDuplex;
00759 writew(np->chip_cmd, ioaddr + ChipCmd);
00760
00761 check_duplex(dev);
00762
00763
00764
00765 mdio_write(dev, np->phys[0], 0x17, mdio_read(dev, np->phys[0], 0x17) |
00766 (np->drv_flags & HasESIPhy) ? 0x0080 : 0x0001);
00767 }
00768
00769
00770
00771
00772 static int mdio_read(struct device *dev, int phy_id, int regnum)
00773 {
00774 long ioaddr = dev->base_addr;
00775 int boguscnt = 1024;
00776
00777
00778 while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
00779 ;
00780 writeb(0x00, ioaddr + MIICmd);
00781 writeb(phy_id, ioaddr + MIIPhyAddr);
00782 writeb(regnum, ioaddr + MIIRegAddr);
00783 writeb(0x40, ioaddr + MIICmd);
00784 boguscnt = 1024;
00785 while ((readb(ioaddr + MIICmd) & 0x40) && --boguscnt > 0)
00786 ;
00787 return readw(ioaddr + MIIData);
00788 }
00789
00790 static void mdio_write(struct device *dev, int phy_id, int regnum, int value)
00791 {
00792 struct netdev_private *np = (struct netdev_private *)dev->priv;
00793 long ioaddr = dev->base_addr;
00794 int boguscnt = 1024;
00795
00796 if (phy_id == np->phys[0]) {
00797 switch (regnum) {
00798 case 0:
00799 if (value & 0x9000)
00800 np->duplex_lock = 0;
00801 else
00802 np->full_duplex = (value & 0x0100) ? 1 : 0;
00803 break;
00804 case 4:
00805 np->advertising = value;
00806 break;
00807 }
00808 }
00809
00810 while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
00811 ;
00812 writeb(0x00, ioaddr + MIICmd);
00813 writeb(phy_id, ioaddr + MIIPhyAddr);
00814 writeb(regnum, ioaddr + MIIRegAddr);
00815 writew(value, ioaddr + MIIData);
00816 writeb(0x20, ioaddr + MIICmd);
00817 return;
00818 }
00819
00820
00821 static int netdev_open(struct device *dev)
00822 {
00823 struct netdev_private *np = (struct netdev_private *)dev->priv;
00824 long ioaddr = dev->base_addr;
00825
00826
00827 writew(CmdReset, ioaddr + ChipCmd);
00828
00829 MOD_INC_USE_COUNT;
00830
00831 if (request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev)) {
00832 MOD_DEC_USE_COUNT;
00833 return -EAGAIN;
00834 }
00835
00836 if (debug > 1)
00837 printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
00838 dev->name, dev->irq);
00839
00840 alloc_rbufs(dev);
00841 alloc_tbufs(dev);
00842
00843 init_registers(dev);
00844
00845 if (debug > 2)
00846 printk(KERN_DEBUG "%s: Done netdev_open(), status %4.4x "
00847 "MII status: %4.4x.\n",
00848 dev->name, readw(ioaddr + ChipCmd),
00849 mdio_read(dev, np->phys[0], 1));
00850
00851
00852 init_timer(&np->timer);
00853 np->timer.expires = jiffies + 2;
00854 np->timer.data = (unsigned long)dev;
00855 np->timer.function = &netdev_timer;
00856 add_timer(&np->timer);
00857
00858 return 0;
00859 }
00860
00861 static void check_duplex(struct device *dev)
00862 {
00863 struct netdev_private *np = (struct netdev_private *)dev->priv;
00864 long ioaddr = dev->base_addr;
00865 int mii_reg5 = mdio_read(dev, np->phys[0], 5);
00866 int negotiated = mii_reg5 & np->advertising;
00867 int duplex;
00868
00869 if (np->duplex_lock || mii_reg5 == 0xffff)
00870 return;
00871 duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
00872 if (np->full_duplex != duplex) {
00873 np->full_duplex = duplex;
00874 if (debug)
00875 printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d link"
00876 " partner capability of %4.4x.\n", dev->name,
00877 duplex ? "full" : "half", np->phys[0], mii_reg5);
00878 if (duplex)
00879 np->chip_cmd |= CmdFDuplex;
00880 else
00881 np->chip_cmd &= ~CmdFDuplex;
00882 writew(np->chip_cmd, ioaddr + ChipCmd);
00883 }
00884 }
00885
00886 static void netdev_timer(unsigned long data)
00887 {
00888 struct device *dev = (struct device *)data;
00889 struct netdev_private *np = (struct netdev_private *)dev->priv;
00890 long ioaddr = dev->base_addr;
00891 int next_tick = 10*HZ;
00892
00893 if (debug > 3) {
00894 printk(KERN_DEBUG "%s: VIA Rhine monitor tick, status %4.4x.\n",
00895 dev->name, readw(ioaddr + IntrStatus));
00896 }
00897 if (test_bit(0, (void*)&dev->tbusy) != 0
00898 && np->cur_tx - np->dirty_tx > 1
00899 && jiffies - dev->trans_start > TX_TIMEOUT)
00900 tx_timeout(dev);
00901
00902 check_duplex(dev);
00903
00904 np->timer.expires = jiffies + next_tick;
00905 add_timer(&np->timer);
00906 }
00907
00908 static void tx_timeout(struct device *dev)
00909 {
00910 struct netdev_private *np = (struct netdev_private *)dev->priv;
00911 struct pci_dev *pdev = pci_find_slot(np->pci_bus, np->pci_devfn);
00912 long ioaddr = dev->base_addr;
00913
00914 printk(KERN_WARNING "%s: Transmit timed out, status %4.4x, PHY status "
00915 "%4.4x, resetting...\n",
00916 dev->name, readw(ioaddr + IntrStatus),
00917 mdio_read(dev, np->phys[0], 1));
00918
00919 dev->if_port = 0;
00920
00921
00922 disable_irq(pdev->irq);
00923
00924
00925 writew(CmdReset, ioaddr + ChipCmd);
00926
00927
00928 free_tbufs(dev);
00929 free_rbufs(dev);
00930 alloc_tbufs(dev);
00931 alloc_rbufs(dev);
00932
00933
00934 wait_for_reset(dev, dev->name);
00935 init_registers(dev);
00936
00937 enable_irq(pdev->irq);
00938
00939 dev->trans_start = jiffies;
00940 np->stats.tx_errors++;
00941
00942
00943 clear_bit(0, (void*)&dev->tbusy);
00944 mark_bh(NET_BH);
00945
00946 return;
00947 }
00948
00949 static int start_tx(struct sk_buff *skb, struct device *dev)
00950 {
00951 struct netdev_private *np = (struct netdev_private *)dev->priv;
00952 unsigned entry;
00953
00954
00955
00956 if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
00957
00958 if (jiffies - dev->trans_start > TX_TIMEOUT)
00959 tx_timeout(dev);
00960 return 1;
00961 }
00962
00963
00964
00965
00966
00967
00968
00969
00970 entry = np->cur_tx % TX_RING_SIZE;
00971
00972 if (skb->len < ETH_ZLEN) {
00973 skb = skb_padto(skb, ETH_ZLEN);
00974 if(skb == NULL)
00975 {
00976 dev->tbusy = 0;
00977 return 0;
00978 }
00979 }
00980
00981 np->tx_skbuff[entry] = skb;
00982
00983 if ((np->drv_flags & ReqTxAlign) && ((long)skb->data & 3)) {
00984
00985 if (np->tx_buf[entry] == NULL &&
00986 (np->tx_buf[entry] = kmalloc(PKT_BUF_SZ, GFP_KERNEL)) == NULL)
00987 return 1;
00988 memcpy(np->tx_buf[entry], skb->data, skb->len);
00989 np->tx_ring[entry].addr = virt_to_le32desc(np->tx_buf[entry]);
00990 } else
00991 np->tx_ring[entry].addr = virt_to_le32desc(skb->data);
00992
00993 np->tx_ring[entry].desc_length =
00994 cpu_to_le32(0x00E08000 | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));
00995 np->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
00996
00997 np->cur_tx++;
00998
00999
01000
01001
01002 writew(CmdTxDemand | np->chip_cmd, dev->base_addr + ChipCmd);
01003
01004 if (np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 1)
01005 clear_bit(0, (void*)&dev->tbusy);
01006 else
01007 np->tx_full = 1;
01008 dev->trans_start = jiffies;
01009
01010 if (debug > 4) {
01011 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
01012 dev->name, np->cur_tx, entry);
01013 }
01014 return 0;
01015 }
01016
01017
01018
01019 static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
01020 {
01021 struct device *dev = (struct device *)dev_instance;
01022 struct netdev_private *np = (void *)dev->priv;
01023 long ioaddr = dev->base_addr;
01024 int boguscnt = max_interrupt_work;
01025
01026 do {
01027 u32 intr_status = readw(ioaddr + IntrStatus);
01028
01029
01030 writew(intr_status & 0xffff, ioaddr + IntrStatus);
01031
01032 if (debug > 4)
01033 printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",
01034 dev->name, intr_status);
01035
01036 if (intr_status == 0)
01037 break;
01038
01039 if (intr_status & (IntrRxDone | IntrRxErr | IntrRxDropped |
01040 IntrRxWakeUp | IntrRxEmpty | IntrRxNoBuf))
01041 netdev_rx(dev);
01042
01043 for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
01044 int entry = np->dirty_tx % TX_RING_SIZE;
01045 int txstatus = le32_to_cpu(np->tx_ring[entry].tx_status);
01046 if (txstatus & DescOwn)
01047 break;
01048 if (debug > 6)
01049 printk(KERN_DEBUG " Tx scavenge %d status %4.4x.\n",
01050 entry, txstatus);
01051 if (txstatus & 0x8000) {
01052 if (debug > 1)
01053 printk(KERN_DEBUG "%s: Transmit error, Tx status %4.4x.\n",
01054 dev->name, txstatus);
01055 np->stats.tx_errors++;
01056 if (txstatus & 0x0400) np->stats.tx_carrier_errors++;
01057 if (txstatus & 0x0200) np->stats.tx_window_errors++;
01058 if (txstatus & 0x0100) np->stats.tx_aborted_errors++;
01059 if (txstatus & 0x0080) np->stats.tx_heartbeat_errors++;
01060 if (txstatus & 0x0002) np->stats.tx_fifo_errors++;
01061 #ifdef ETHER_STATS
01062 if (txstatus & 0x0100) np->stats.collisions16++;
01063 #endif
01064
01065 } else {
01066 #ifdef ETHER_STATS
01067 if (txstatus & 0x0001) np->stats.tx_deferred++;
01068 #endif
01069 np->stats.collisions += (txstatus >> 3) & 15;
01070 #if defined(NETSTATS_VER2)
01071 np->stats.tx_bytes += np->tx_skbuff[entry]->len;
01072 #endif
01073 np->stats.tx_packets++;
01074 }
01075
01076 dev_kfree_skb(np->tx_skbuff[entry]);
01077 np->tx_skbuff[entry] = 0;
01078 }
01079 if (np->tx_full && dev->tbusy
01080 && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
01081
01082 np->tx_full = 0;
01083 clear_bit(0, (void*)&dev->tbusy);
01084 mark_bh(