00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 static const char *version = "pcnet32.c:v1.25kf 26.9.1999 tsbogend@alpha.franken.de\n";
00017
00018 #include <linux/config.h>
00019 #include <linux/module.h>
00020 #ifdef MODVERSIONS
00021 #include <linux/modversions.h>
00022 #endif
00023
00024 #include <linux/kernel.h>
00025 #include <linux/sched.h>
00026 #include <linux/string.h>
00027 #include <linux/ptrace.h>
00028 #include <linux/errno.h>
00029 #include <linux/ioport.h>
00030 #include <linux/malloc.h>
00031 #include <linux/interrupt.h>
00032 #include <linux/pci.h>
00033 #include <linux/delay.h>
00034 #include <linux/init.h>
00035 #include <asm/bitops.h>
00036 #include <asm/io.h>
00037 #include <asm/dma.h>
00038
00039 #include <linux/netdevice.h>
00040 #include <linux/etherdevice.h>
00041 #include <linux/skbuff.h>
00042 #include <asm/spinlock.h>
00043
00044 static unsigned int pcnet32_portlist[] __initdata = {0x300, 0x320, 0x340, 0x360, 0};
00045
00046 static int pcnet32_debug = 1;
00047 static int tx_start = 1;
00048
00049 #ifdef MODULE
00050 static struct device *pcnet32_dev = NULL;
00051 #endif
00052
00053 static const int max_interrupt_work = 80;
00054 static const int rx_copybreak = 200;
00055
00056 #define PORT_AUI 0x00
00057 #define PORT_10BT 0x01
00058 #define PORT_GPSI 0x02
00059 #define PORT_MII 0x03
00060
00061 #define PORT_PORTSEL 0x03
00062 #define PORT_ASEL 0x04
00063 #define PORT_100 0x40
00064 #define PORT_FD 0x80
00065
00066
00067
00068
00069
00070
00071 static unsigned char options_mapping[] = {
00072 PORT_ASEL,
00073 PORT_AUI,
00074 PORT_AUI,
00075 PORT_ASEL,
00076 PORT_10BT | PORT_FD,
00077 PORT_ASEL,
00078 PORT_ASEL,
00079 PORT_ASEL,
00080 PORT_ASEL,
00081 PORT_MII,
00082 PORT_MII | PORT_FD,
00083 PORT_MII,
00084 PORT_10BT,
00085 PORT_MII | PORT_100,
00086 PORT_MII | PORT_100 | PORT_FD,
00087 PORT_ASEL
00088 };
00089
00090 #define MAX_UNITS 8
00091 static int options[MAX_UNITS] = {0, };
00092 static int full_duplex[MAX_UNITS] = {0, };
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
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 #ifndef PCNET32_LOG_TX_BUFFERS
00176 #define PCNET32_LOG_TX_BUFFERS 4
00177 #define PCNET32_LOG_RX_BUFFERS 5
00178 #endif
00179
00180 #define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
00181 #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
00182 #define TX_RING_LEN_BITS ((PCNET32_LOG_TX_BUFFERS) << 12)
00183
00184 #define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
00185 #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
00186 #define RX_RING_LEN_BITS ((PCNET32_LOG_RX_BUFFERS) << 4)
00187
00188 #define PKT_BUF_SZ 1544
00189
00190
00191 #define PCNET32_WIO_RDP 0x10
00192 #define PCNET32_WIO_RAP 0x12
00193 #define PCNET32_WIO_RESET 0x14
00194 #define PCNET32_WIO_BDP 0x16
00195
00196 #define PCNET32_DWIO_RDP 0x10
00197 #define PCNET32_DWIO_RAP 0x14
00198 #define PCNET32_DWIO_RESET 0x18
00199 #define PCNET32_DWIO_BDP 0x1C
00200
00201 #define PCNET32_TOTAL_SIZE 0x20
00202
00203
00204 #ifndef PCI_DEVICE_ID_AMD_LANCE
00205 #define PCI_VENDOR_ID_AMD 0x1022
00206 #define PCI_DEVICE_ID_AMD_LANCE 0x2000
00207 #endif
00208 #ifndef PCI_DEVICE_ID_AMD_PCNETHOME
00209 #define PCI_DEVICE_ID_AMD_PCNETHOME 0x2001
00210 #endif
00211
00212
00213 #define CRC_POLYNOMIAL_LE 0xedb88320UL
00214
00215
00216 struct pcnet32_rx_head {
00217 u32 base;
00218 s16 buf_length;
00219 s16 status;
00220 u32 msg_length;
00221 u32 reserved;
00222 };
00223
00224 struct pcnet32_tx_head {
00225 u32 base;
00226 s16 length;
00227 s16 status;
00228 u32 misc;
00229 u32 reserved;
00230 };
00231
00232
00233 struct pcnet32_init_block {
00234 u16 mode;
00235 u16 tlen_rlen;
00236 u8 phys_addr[6];
00237 u16 reserved;
00238 u32 filter[2];
00239
00240 u32 rx_ring;
00241 u32 tx_ring;
00242 };
00243
00244
00245 struct pcnet32_access {
00246 u16 (*read_csr)(unsigned long, int);
00247 void (*write_csr)(unsigned long, int, u16);
00248 u16 (*read_bcr)(unsigned long, int);
00249 void (*write_bcr)(unsigned long, int, u16);
00250 u16 (*read_rap)(unsigned long);
00251 void (*write_rap)(unsigned long, u16);
00252 void (*reset)(unsigned long);
00253 };
00254
00255 struct pcnet32_private {
00256
00257 struct pcnet32_rx_head rx_ring[RX_RING_SIZE];
00258 struct pcnet32_tx_head tx_ring[TX_RING_SIZE];
00259 struct pcnet32_init_block init_block;
00260 const char *name;
00261
00262 struct sk_buff *tx_skbuff[TX_RING_SIZE];
00263 struct sk_buff *rx_skbuff[RX_RING_SIZE];
00264 struct pcnet32_access a;
00265 void *origmem;
00266 spinlock_t lock;
00267 unsigned int cur_rx, cur_tx;
00268 unsigned int dirty_rx, dirty_tx;
00269 struct net_device_stats stats;
00270 char tx_full;
00271 int options;
00272 int shared_irq:1,
00273 ltint:1,
00274 #ifdef DO_DXSUFLO
00275 dxsuflo:1,
00276 #endif
00277 full_duplex:1,
00278 mii:1;
00279 #ifdef MODULE
00280 struct device *next;
00281 #endif
00282 };
00283
00284 int pcnet32_probe(struct device *);
00285 static int pcnet32_probe1(struct device *, unsigned long, unsigned char, int, int);
00286 static int pcnet32_open(struct device *);
00287 static int pcnet32_init_ring(struct device *);
00288 static int pcnet32_start_xmit(struct sk_buff *, struct device *);
00289 static int pcnet32_rx(struct device *);
00290 static void pcnet32_interrupt(int, void *, struct pt_regs *);
00291 static int pcnet32_close(struct device *);
00292 static struct net_device_stats *pcnet32_get_stats(struct device *);
00293 static void pcnet32_set_multicast_list(struct device *);
00294 #ifdef HAVE_PRIVATE_IOCTL
00295 static int pcnet32_mii_ioctl(struct device *, struct ifreq *, int);
00296 #endif
00297
00298 enum pci_flags_bit {
00299 PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
00300 PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
00301 };
00302
00303 struct pcnet32_pci_id_info {
00304 const char *name;
00305 u16 vendor_id, device_id, svid, sdid, flags;
00306 int io_size;
00307 int (*probe1) (struct device *, unsigned long, unsigned char, int, int);
00308 };
00309
00310 static struct pcnet32_pci_id_info pcnet32_tbl[] = {
00311 { "AMD PCnetPCI series",
00312 PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, 0, 0,
00313 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
00314 pcnet32_probe1},
00315 { "AMD PCnetPCI series (IBM)",
00316 PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, 0x1014, 0x2000,
00317 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
00318 pcnet32_probe1},
00319
00320
00321 { "AMD PCnetPCI series (IBM)",
00322 0x1023, 0x2000, 0, 0,
00323 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
00324 pcnet32_probe1},
00325 { "AMD PCnetHome series",
00326 PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_PCNETHOME, 0, 0,
00327 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
00328 pcnet32_probe1},
00329 {0,}
00330 };
00331
00332 static u16 pcnet32_wio_read_csr (unsigned long addr, int index)
00333 {
00334 outw (index, addr+PCNET32_WIO_RAP);
00335 return inw (addr+PCNET32_WIO_RDP);
00336 }
00337
00338 static void pcnet32_wio_write_csr (unsigned long addr, int index, u16 val)
00339 {
00340 outw (index, addr+PCNET32_WIO_RAP);
00341 outw (val, addr+PCNET32_WIO_RDP);
00342 }
00343
00344 static u16 pcnet32_wio_read_bcr (unsigned long addr, int index)
00345 {
00346 outw (index, addr+PCNET32_WIO_RAP);
00347 return inw (addr+PCNET32_WIO_BDP);
00348 }
00349
00350 static void pcnet32_wio_write_bcr (unsigned long addr, int index, u16 val)
00351 {
00352 outw (index, addr+PCNET32_WIO_RAP);
00353 outw (val, addr+PCNET32_WIO_BDP);
00354 }
00355
00356 static u16 pcnet32_wio_read_rap (unsigned long addr)
00357 {
00358 return inw (addr+PCNET32_WIO_RAP);
00359 }
00360
00361 static void pcnet32_wio_write_rap (unsigned long addr, u16 val)
00362 {
00363 outw (val, addr+PCNET32_WIO_RAP);
00364 }
00365
00366 static void pcnet32_wio_reset (unsigned long addr)
00367 {
00368 inw (addr+PCNET32_WIO_RESET);
00369 }
00370
00371 static int pcnet32_wio_check (unsigned long addr)
00372 {
00373 outw (88, addr+PCNET32_WIO_RAP);
00374 return (inw (addr+PCNET32_WIO_RAP) == 88);
00375 }
00376
00377 static struct pcnet32_access pcnet32_wio = {
00378 pcnet32_wio_read_csr,
00379 pcnet32_wio_write_csr,
00380 pcnet32_wio_read_bcr,
00381 pcnet32_wio_write_bcr,
00382 pcnet32_wio_read_rap,
00383 pcnet32_wio_write_rap,
00384 pcnet32_wio_reset
00385 };
00386
00387 static u16 pcnet32_dwio_read_csr (unsigned long addr, int index)
00388 {
00389 outl (index, addr+PCNET32_DWIO_RAP);
00390 return (inl (addr+PCNET32_DWIO_RDP) & 0xffff);
00391 }
00392
00393 static void pcnet32_dwio_write_csr (unsigned long addr, int index, u16 val)
00394 {
00395 outl (index, addr+PCNET32_DWIO_RAP);
00396 outl (val, addr+PCNET32_DWIO_RDP);
00397 }
00398
00399 static u16 pcnet32_dwio_read_bcr (unsigned long addr, int index)
00400 {
00401 outl (index, addr+PCNET32_DWIO_RAP);
00402 return (inl (addr+PCNET32_DWIO_BDP) & 0xffff);
00403 }
00404
00405 static void pcnet32_dwio_write_bcr (unsigned long addr, int index, u16 val)
00406 {
00407 outl (index, addr+PCNET32_DWIO_RAP);
00408 outl (val, addr+PCNET32_DWIO_BDP);
00409 }
00410
00411 static u16 pcnet32_dwio_read_rap (unsigned long addr)
00412 {
00413 return (inl (addr+PCNET32_DWIO_RAP) & 0xffff);
00414 }
00415
00416 static void pcnet32_dwio_write_rap (unsigned long addr, u16 val)
00417 {
00418 outl (val, addr+PCNET32_DWIO_RAP);
00419 }
00420
00421 static void pcnet32_dwio_reset (unsigned long addr)
00422 {
00423 inl (addr+PCNET32_DWIO_RESET);
00424 }
00425
00426 static int pcnet32_dwio_check (unsigned long addr)
00427 {
00428 outl (88, addr+PCNET32_DWIO_RAP);
00429 return (inl (addr+PCNET32_DWIO_RAP) == 88);
00430 }
00431
00432 static struct pcnet32_access pcnet32_dwio = {
00433 pcnet32_dwio_read_csr,
00434 pcnet32_dwio_write_csr,
00435 pcnet32_dwio_read_bcr,
00436 pcnet32_dwio_write_bcr,
00437 pcnet32_dwio_read_rap,
00438 pcnet32_dwio_write_rap,
00439 pcnet32_dwio_reset
00440
00441 };
00442
00443
00444
00445 int __init pcnet32_probe (struct device *dev)
00446 {
00447 unsigned long ioaddr = dev ? dev->base_addr: 0;
00448 unsigned int irq_line = dev ? dev->irq : 0;
00449 int *port;
00450 int cards_found = 0;
00451
00452
00453 #ifndef __powerpc__
00454 if (ioaddr > 0x1ff) {
00455 if (check_region(ioaddr, PCNET32_TOTAL_SIZE) == 0)
00456 return pcnet32_probe1(dev, ioaddr, irq_line, 0, 0);
00457 else
00458 return ENODEV;
00459 } else
00460 #endif
00461 if(ioaddr != 0)
00462 return ENXIO;
00463
00464 #if defined(CONFIG_PCI)
00465 if (pci_present()) {
00466 struct pci_dev *pdev = NULL;
00467
00468 printk("pcnet32.c: PCI bios is present, checking for devices...\n");
00469 while ((pdev = pci_find_class (PCI_CLASS_NETWORK_ETHERNET<<8, pdev))) {
00470 u16 pci_command;
00471 int chip_idx;
00472 u16 sdid,svid;
00473
00474 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &svid);
00475 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdid);
00476 for (chip_idx = 0; pcnet32_tbl[chip_idx].vendor_id; chip_idx++)
00477 if ((pdev->vendor == pcnet32_tbl[chip_idx].vendor_id) &&
00478 (pdev->device == pcnet32_tbl[chip_idx].device_id) &&
00479 (pcnet32_tbl[chip_idx].svid == 0 ||
00480 (svid == pcnet32_tbl[chip_idx].svid)) &&
00481 (pcnet32_tbl[chip_idx].sdid == 0 ||
00482 (sdid == pcnet32_tbl[chip_idx].sdid)))
00483 break;
00484 if (pcnet32_tbl[chip_idx].vendor_id == 0)
00485 continue;
00486
00487 ioaddr = pdev->base_address[0] & PCI_BASE_ADDRESS_IO_MASK;
00488 #if defined(ADDR_64BITS) && defined(__alpha__)
00489 ioaddr |= ((long)pdev->base_address[1]) << 32;
00490 #endif
00491 irq_line = pdev->irq;
00492
00493
00494 if ((pcnet32_tbl[chip_idx].flags & PCI_USES_IO) &&
00495 check_region(ioaddr, pcnet32_tbl[chip_idx].io_size))
00496 continue;
00497
00498
00499
00500
00501
00502 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
00503 if ( ! (pci_command & PCI_COMMAND_MASTER)) {
00504 printk("PCI Master Bit has not been set. Setting...\n");
00505 pci_command |= PCI_COMMAND_MASTER|PCI_COMMAND_IO;
00506 pci_write_config_word(pdev, PCI_COMMAND, pci_command);
00507 }
00508 printk("Found PCnet/PCI at %#lx, irq %d.\n", ioaddr, irq_line);
00509
00510 if (pcnet32_tbl[chip_idx].probe1(dev, ioaddr, irq_line, 1, cards_found) == 0) {
00511 cards_found++;
00512 dev = NULL;
00513 }
00514 }
00515 } else
00516 #endif
00517
00518
00519 for (port = pcnet32_portlist; *port; port++) {
00520 unsigned long ioaddr = *port;
00521
00522 if ( check_region(ioaddr, PCNET32_TOTAL_SIZE) == 0) {
00523
00524 if ((inb(ioaddr + 14) == 0x57) &&
00525 (inb(ioaddr + 15) == 0x57) &&
00526 (pcnet32_probe1(dev, ioaddr, 0, 0, 0) == 0))
00527 cards_found++;
00528 }
00529 }
00530 return cards_found ? 0: ENODEV;
00531 }
00532
00533
00534
00535 static int __init
00536 pcnet32_probe1(struct device *dev, unsigned long ioaddr, unsigned char irq_line, int shared, int card_idx)
00537 {
00538 struct pcnet32_private *lp;
00539 int i,media,fdx = 0, mii = 0;
00540 #ifdef DO_DXSUFLO
00541 int dxsuflo = 0;
00542 #endif
00543 int ltint = 0;
00544 int chip_version;
00545 char *chipname;
00546 char *priv;
00547 struct pcnet32_access *a;
00548
00549
00550 pcnet32_dwio_reset(ioaddr);
00551 pcnet32_wio_reset(ioaddr);
00552
00553 if (pcnet32_wio_read_csr (ioaddr, 0) == 4 && pcnet32_wio_check (ioaddr)) {
00554 a = &pcnet32_wio;
00555 } else {
00556 if (pcnet32_dwio_read_csr (ioaddr, 0) == 4 && pcnet32_dwio_check(ioaddr)) {
00557 a = &pcnet32_dwio;
00558 } else
00559 return ENODEV;
00560 }
00561
00562
00563 chip_version = a->read_csr (ioaddr, 88) | (a->read_csr (ioaddr,89) << 16);
00564 if (pcnet32_debug > 2)
00565 printk(" PCnet chip version is %#x.\n", chip_version);
00566 if ((chip_version & 0xfff) != 0x003)
00567 return ENODEV;
00568 chip_version = (chip_version >> 12) & 0xffff;
00569
00570 switch (chip_version) {
00571 case 0x2420:
00572 chipname = "PCnet/PCI 79C970";
00573 break;
00574 case 0x2430:
00575 if (shared)
00576 chipname = "PCnet/PCI 79C970";
00577 else
00578 chipname = "PCnet/32 79C965";
00579 break;
00580 case 0x2621:
00581 chipname = "PCnet/PCI II 79C970A";
00582 fdx = 1;
00583 break;
00584 case 0x2623:
00585 chipname = "PCnet/FAST 79C971";
00586
00587
00588
00589 a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0800));
00590
00591 i = a->read_csr(ioaddr, 80) & ~0x0C00;
00592 a->write_csr(ioaddr, 80, i | (tx_start << 10));
00593 fdx = 1; mii = 1;
00594 #ifdef DO_DXSUFLO
00595 dxsuflo = 1;
00596 #endif
00597 ltint = 1;
00598 break;
00599 case 0x2624:
00600 chipname = "PCnet/FAST+ 79C972";
00601
00602
00603
00604 a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0800));
00605
00606 i = a->read_csr(ioaddr, 80) & ~0x0C00;
00607 a->write_csr(ioaddr, 80, i | (tx_start << 10));
00608 fdx = 1; mii = 1;
00609 #ifdef DO_DXSUFLO
00610 dxsuflo = 1;
00611 #endif
00612 ltint = 1;
00613 break;
00614 case 0x2625:
00615 chipname = "PCnet/FAST III 79C973";
00616 fdx = 1; mii = 1;
00617 break;
00618 case 0x2626:
00619 chipname = "PCnet/Home 79C978";
00620 fdx = 1;
00621
00622
00623
00624
00625
00626
00627
00628
00629 media = a->read_bcr (ioaddr, 49);
00630 if (pcnet32_debug > 2)
00631 printk("pcnet32: pcnet32 media value %#x.\n", media);
00632 media &= ~3;
00633 media |= 1;
00634 if (pcnet32_debug > 2)
00635 printk("pcnet32: pcnet32 media reset to %#x.\n", media);
00636 a->write_bcr (ioaddr, 49, media);
00637 break;
00638 case 0x2627:
00639 chipname = "PCnet/FAST III 79C975";
00640 fdx = 1; mii = 1;
00641 break;
00642 default:
00643 printk("pcnet32: PCnet version %#x, no PCnet32 chip.\n",chip_version);
00644 return ENODEV;
00645 }
00646
00647 dev = init_etherdev(dev, 0);
00648
00649 printk(KERN_INFO "%s: %s at %#3lx,", dev->name, chipname, ioaddr);
00650
00651
00652
00653 for (i = 0; i < 6; i++)
00654 printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
00655
00656 if (((chip_version + 1) & 0xfffe) == 0x2624) {
00657 i = a->read_csr(ioaddr, 80) & 0x0C00;
00658 printk("\n tx_start_pt(0x%04x):",i);
00659 switch(i>>10) {
00660 case 0: printk(" 20 bytes,"); break;
00661 case 1: printk(" 64 bytes,"); break;
00662 case 2: printk(" 128 bytes,"); break;
00663 case 3: printk("~220 bytes,"); break;
00664 }
00665 i = a->read_bcr(ioaddr, 18);
00666 printk(" BCR18(%x):",i&0xffff);
00667 if (i & (1<<5)) printk("BurstWrEn ");
00668 if (i & (1<<6)) printk("BurstRdEn ");
00669 if (i & (1<<7)) printk("DWordIO ");
00670 if (i & (1<<11)) printk("NoUFlow ");
00671 i = a->read_bcr(ioaddr, 25);
00672 printk("\n SRAMSIZE=0x%04x,",i<<8);
00673 i = a->read_bcr(ioaddr, 26);
00674 printk(" SRAM_BND=0x%04x,",i<<8);
00675 i = a->read_bcr(ioaddr, 27);
00676 if (i & (1<<14)) printk("LowLatRx,");
00677 }
00678
00679 dev->base_addr = ioaddr;
00680 request_region(ioaddr, PCNET32_TOTAL_SIZE, chipname);
00681
00682 if ((priv = kmalloc(sizeof(*lp)+15,GFP_KERNEL)) == NULL)
00683 return ENOMEM;
00684
00685
00686
00687
00688
00689 lp = (struct pcnet32_private *)(((unsigned long)priv+15) & ~15);
00690
00691 memset(lp, 0, sizeof(*lp));
00692
00693 spin_lock_init(&lp->lock);
00694
00695 dev->priv = lp;
00696 lp->name = chipname;
00697 lp->shared_irq = shared;
00698 lp->full_duplex = fdx;
00699 #ifdef DO_DXSUFLO
00700 lp->dxsuflo = dxsuflo;
00701 #endif
00702 lp->ltint = ltint;
00703 lp->mii = mii;
00704 if (options[card_idx] > sizeof (options_mapping))
00705 lp->options = PORT_ASEL;
00706 else
00707 lp->options = options_mapping[options[card_idx]];
00708
00709 if (fdx && !(lp->options & PORT_ASEL) && full_duplex[card_idx])
00710 lp->options |= PORT_FD;
00711
00712 lp->origmem = priv;
00713 lp->a = *a;
00714
00715
00716 if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0 && dev->dev_addr[2] == 0x75)
00717 lp->options = PORT_FD | PORT_GPSI;
00718
00719 lp->init_block.mode = le16_to_cpu(0x0003);
00720 lp->init_block.tlen_rlen = le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
00721 for (i = 0; i < 6; i++)
00722 lp->init_block.phys_addr[i] = dev->dev_addr[i];
00723 lp->init_block.filter[0] = 0x00000000;
00724 lp->init_block.filter[1] = 0x00000000;
00725 lp->init_block.rx_ring = (u32)le32_to_cpu(virt_to_bus(lp->rx_ring));
00726 lp->init_block.tx_ring = (u32)le32_to_cpu(virt_to_bus(lp->tx_ring));
00727
00728
00729 a->write_bcr (ioaddr, 20, 2);
00730
00731 a->write_csr (ioaddr, 1, virt_to_bus(&lp->init_block) & 0xffff);
00732 a->write_csr (ioaddr, 2, virt_to_bus(&lp->init_block) >> 16);
00733
00734 if (irq_line) {
00735 dev->irq = irq_line;
00736 }
00737
00738 if (dev->irq >= 2)
00739 printk(" assigned IRQ %d.\n", dev->irq);
00740 else {
00741 unsigned long irq_mask = probe_irq_on();
00742
00743
00744
00745
00746
00747
00748
00749 a->write_csr (ioaddr, 0, 0x41);
00750 mdelay (1);
00751
00752 dev->irq = probe_irq_off (irq_mask);
00753 if (dev->irq)
00754 printk(", probed IRQ %d.\n", dev->irq);
00755 else {
00756 printk(", failed to detect IRQ line.\n");
00757 return ENODEV;
00758 }
00759 }
00760
00761 if (pcnet32_debug > 0)
00762 printk(version);
00763
00764
00765 dev->open = &pcnet32_open;
00766 dev->hard_start_xmit = &pcnet32_start_xmit;
00767 dev->stop = &pcnet32_close;
00768 dev->get_stats = &pcnet32_get_stats;
00769 dev->set_multicast_list = &pcnet32_set_multicast_list;
00770 #ifdef HAVE_PRIVATE_IOCTL
00771 dev->do_ioctl = &pcnet32_mii_ioctl;
00772 #endif
00773
00774
00775 #ifdef MODULE
00776 lp->next = pcnet32_dev;
00777 pcnet32_dev = dev;
00778 #endif
00779
00780
00781 ether_setup(dev);
00782 return 0;
00783 }
00784
00785
00786 static int
00787 pcnet32_open(struct device *dev)
00788 {
00789 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
00790 unsigned long ioaddr = dev->base_addr;
00791 u16 val;
00792 int i;
00793
00794 if (dev->irq == 0 ||
00795 request_irq(dev->irq, &pcnet32_interrupt,
00796 lp->shared_irq ? SA_SHIRQ : 0, lp->name, (void *)dev)) {
00797 return -EAGAIN;
00798 }
00799
00800
00801 lp->a.reset (ioaddr);
00802
00803
00804 lp->a.write_bcr (ioaddr, 20, 2);
00805
00806 if (pcnet32_debug > 1)
00807 printk("%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
00808 dev->name, dev->irq,
00809 (u32) virt_to_bus(lp->tx_ring),
00810 (u32) virt_to_bus(lp->rx_ring),
00811 (u32) virt_to_bus(&lp->init_block));
00812
00813
00814 val = lp->a.read_bcr (ioaddr, 2) & ~2;
00815 if (lp->options & PORT_ASEL)
00816 val |= 2;
00817 lp->a.write_bcr (ioaddr, 2, val);
00818
00819
00820 if (lp->full_duplex) {
00821 val = lp->a.read_bcr (ioaddr, 9) & ~3;
00822 if (lp->options & PORT_FD) {
00823 val |= 1;
00824 if (lp->options == (PORT_FD | PORT_AUI))
00825 val |= 2;
00826 }
00827 lp->a.write_bcr (ioaddr, 9, val);
00828 }
00829
00830
00831 val = lp->a.read_csr (ioaddr, 124) & ~0x10;
00832 if ((lp->options & PORT_PORTSEL) == PORT_GPSI)
00833 val |= 0x10;
00834 lp->a.write_csr (ioaddr, 124, val);
00835
00836 if (lp->mii & !(lp->options & PORT_ASEL)) {
00837 val = lp->a.read_bcr (ioaddr, 32) & ~0x38;
00838 if (lp->options & PORT_FD)
00839 val |= 0x10;
00840 if (lp->options & PORT_100)
00841 val |= 0x08;
00842 lp->a.write_bcr (ioaddr, 32, val);
00843 }
00844
00845 #ifdef DO_DXSUFLO
00846 if (lp->dxsuflo) {
00847 val = lp->a.read_csr (ioaddr, 3);
00848 val |= 0x40;
00849 lp->a.write_csr (ioaddr, 3, val);
00850 }
00851 #endif
00852 if (lp->ltint) {
00853 val = lp->a.read_csr (ioaddr, 5);
00854 val |= (1<<14);
00855 lp->a.write_csr (ioaddr, 5, val);
00856 }
00857
00858 lp->init_block.mode = le16_to_cpu((lp->options & PORT_PORTSEL) << 7);
00859 lp->init_block.filter[0] = 0x00000000;
00860 lp->init_block.filter[1] = 0x00000000;
00861 if (pcnet32_init_ring(dev))
00862 return -ENOMEM;
00863
00864
00865 lp->a.write_csr (ioaddr, 1, virt_to_bus(&lp->init_block) &0xffff);
00866 lp->a.write_csr (ioaddr, 2, virt_to_bus(&lp->init_block) >> 16);
00867
00868 lp->a.write_csr (ioaddr, 4, 0x0915);
00869 lp->a.write_csr (ioaddr, 0, 0x0001);
00870
00871 dev->tbusy = 0;
00872 dev->interrupt = 0;
00873 dev->start = 1;
00874 i = 0;
00875 while (i++ < 100)
00876 if (lp->a.read_csr (ioaddr, 0) & 0x0100)
00877 break;
00878
00879
00880
00881
00882 lp->a.write_csr (ioaddr, 0, 0x0042);
00883
00884 if (pcnet32_debug > 2)
00885 printk("%s: PCNET32 open after %d ticks, init block %#x csr0 %4.4x.\n",
00886 dev->name, i, (u32) virt_to_bus(&lp->init_block),
00887 lp->a.read_csr (ioaddr, 0));
00888
00889 MOD_INC_USE_COUNT;
00890
00891 return 0;
00892 }
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907 static void
00908 pcnet32_purge_tx_ring(struct device *dev)
00909 {
00910 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
00911 int i;
00912
00913 for (i = 0; i < TX_RING_SIZE; i++) {
00914 if (lp->tx_skbuff[i]) {
00915 dev_kfree_skb(lp->tx_skbuff[i]);
00916 lp->tx_skbuff[i] = NULL;
00917 }
00918 }
00919 }
00920
00921
00922
00923 static int
00924 pcnet32_init_ring(struct device *dev)
00925 {
00926 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
00927 int i;
00928
00929 lp->tx_full = 0;
00930 lp->cur_rx = lp->cur_tx = 0;
00931 lp->dirty_rx = lp->dirty_tx = 0;
00932
00933 for (i = 0; i < RX_RING_SIZE; i++) {
00934 if (lp->rx_skbuff[i] == NULL) {
00935 if (!(lp->rx_skbuff[i] = dev_alloc_skb (PKT_BUF_SZ))) {
00936
00937 printk ("%s: pcnet32_init_ring dev_alloc_skb failed.\n",dev->name);
00938 return -1;
00939 }
00940 skb_reserve (lp->rx_skbuff[i], 2);
00941 }
00942 lp->rx_ring[i].base = (u32)le32_to_cpu(virt_to_bus(lp->rx_skbuff[i]->tail));
00943 lp->rx_ring[i].buf_length = le16_to_cpu(-PKT_BUF_SZ);
00944 lp->rx_ring[i].status = le16_to_cpu(0x8000);
00945 }
00946
00947
00948 for (i = 0; i < TX_RING_SIZE; i++) {
00949 lp->tx_ring[i].base = 0;
00950 lp->tx_ring[i].status = 0;
00951 }
00952
00953 lp->init_block.tlen_rlen = le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
00954 for (i = 0; i < 6; i++)
00955 lp->init_block.phys_addr[i] = dev->dev_addr[i];
00956 lp->init_block.rx_ring = (u32)le32_to_cpu(virt_to_bus(lp->rx_ring));
00957 lp->init_block.tx_ring = (u32)le32_to_cpu(virt_to_bus(lp->tx_ring));
00958 return 0;
00959 }
00960
00961 static void
00962 pcnet32_restart(struct device *dev, unsigned int csr0_bits)
00963 {
00964 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
00965 unsigned long ioaddr = dev->base_addr;
00966 int i;
00967
00968 pcnet32_purge_tx_ring(dev);
00969 if (pcnet32_init_ring(dev))
00970 return;
00971
00972
00973 lp->a.write_csr (ioaddr, 0, 1);
00974 i = 0;
00975 while (i++ < 100)
00976 if (lp->a.read_csr (ioaddr, 0) & 0x0100)
00977 break;
00978
00979 lp->a.write_csr (ioaddr, 0, csr0_bits);
00980 }
00981
00982 static int
00983 pcnet32_start_xmit(struct sk_buff *skb, struct device *dev)
00984 {
00985 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
00986 unsigned int ioaddr = dev->base_addr;
00987 u16 status;
00988 int entry;
00989 unsigned long flags;
00990
00991
00992 if (dev->tbusy) {
00993 int tickssofar = jiffies - dev->trans_start;
00994 if (tickssofar < HZ/2)
00995 return 1;
00996 printk("%s: transmit timed out, status %4.4x, resetting.\n",
00997 dev->name, lp->a.read_csr (ioaddr, 0));
00998 lp->a.write_csr (ioaddr, 0, 0x0004);
00999 lp->stats.tx_errors++;
01000 if (pcnet32_debug > 2) {
01001 int i;
01002 printk(" Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
01003 lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
01004 lp->cur_rx);
01005 for (i = 0 ; i < RX_RING_SIZE; i++)
01006 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
01007 lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
01008 lp->rx_ring[i].msg_length, (unsigned)lp->rx_ring[i].status);
01009 for (i = 0 ; i < TX_RING_SIZE; i++)
01010 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
01011 lp->tx_ring[i].base, -lp->tx_ring[i].length,
01012 lp->tx_ring[i].misc, (unsigned)lp->tx_ring[i].status);
01013 printk("\n");
01014 }
01015 pcnet32_restart(dev, 0x0042);
01016
01017 dev->tbusy = 0;
01018 dev->trans_start = jiffies;
01019 dev_kfree_skb(skb);
01020 return 0;
01021 }
01022
01023 if (pcnet32_debug > 3) {
01024 printk("%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
01025 dev->name, lp->a.read_csr (ioaddr, 0));
01026 }
01027
01028
01029
01030 if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
01031 printk("%s: Transmitter access conflict.\n", dev->name);
01032 return 1;
01033 }
01034
01035 spin_lock_irqsave(&lp->lock, flags);
01036
01037
01038
01039
01040 status = 0x8300;
01041 if ((lp->ltint) &&
01042 ((lp->cur_tx - lp->dirty_tx == TX_RING_SIZE/2) ||
01043 (lp->cur_tx - lp->dirty_tx >= TX_RING_SIZE-2)))
01044 {
01045
01046
01047
01048
01049
01050 status = 0x9300;
01051 }
01052
01053
01054
01055
01056 entry = lp->cur_tx & TX_RING_MOD_MASK;
01057
01058
01059
01060
01061 lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
01062
01063 lp->tx_ring[entry].misc = 0x00000000;
01064
01065 lp->tx_skbuff[entry] = skb;
01066 lp->tx_ring[entry].base = (u32)le32_to_cpu(virt_to_bus(skb->data));
01067
01068 lp->tx_ring[entry].status = le16_to_cpu(status);
01069
01070 lp->cur_tx++;
01071 lp->stats.tx_bytes += skb->len;
01072
01073
01074 lp->a.write_csr (ioaddr, 0, 0x0048);
01075
01076 dev->trans_start = jiffies;
01077
01078 if (lp->tx_ring[(entry+1) & TX_RING_MOD_MASK].base == 0)
01079 clear_bit (0, (void *)&dev->tbusy);
01080 else
01081 lp->tx_full = 1;
01082 spin_unlock_irqrestore(&lp->lock, flags);
01083 return 0;
01084 }
01085
01086
01087 static void
01088 pcnet32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
01089 {
01090 struct device *dev = (struct device *)dev_id;
01091 struct pcnet32_private *lp;
01092 unsigned long ioaddr;
01093 u16 csr0,rap;
01094 int boguscnt = max_interrupt_work;
01095 int must_restart;
01096
01097 if (dev == NULL) {
01098 printk ("pcnet32_interrupt(): irq %d for unknown device.\n", irq);
01099 return;
01100 }
01101
01102 ioaddr = dev->base_addr;
01103 lp = (struct pcnet32_private *)dev->priv;
01104
01105 spin_lock(&lp->lock);
01106
01107 if (dev->interrupt)
01108 printk("%s: Re-entering the interrupt handler.\n", dev->name);
01109
01110 dev->interrupt = 1;
01111
01112 rap = lp->a.read_rap(ioaddr);
01113 while ((csr0 = lp->a.read_csr (ioaddr, 0)) & 0x8600 && --boguscnt >= 0) {
01114
01115 lp->a.write_csr (ioaddr, 0, csr0 & ~0x004f);
01116
01117 must_restart = 0;
01118
01119 if (pcnet32_debug > 5)
01120 printk("%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
01121 dev->name, csr0, lp->a.read_csr (ioaddr, 0));
01122
01123 if (csr0 & 0x0400)
01124 pcnet32_rx(dev);
01125
01126 if (csr0 & 0x0200) {
01127 unsigned int dirty_tx = lp->dirty_tx;
01128
01129 while (dirty_tx < lp->cur_tx) {
01130 int entry = dirty_tx & TX_RING_MOD_MASK;
01131 int status = (short)le16_to_cpu(lp->tx_ring[entry].status);
01132
01133 if (status < 0)
01134 break;
01135
01136 lp->tx_ring[entry].base = 0;
01137
01138 if (status & 0x4000) {
01139
01140 int err_status = le32_to_cpu(lp->tx_ring[entry].misc);
01141 lp->stats.tx_errors++;
01142 if (err_status & 0x04000000) lp->stats.tx_aborted