OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [before_ORP/] [uclinux/] [uClinux-2.0.x/] [drivers/] [net/] [rtl8139.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/* rtl8139.c: A RealTek RTL8129/8139 Fast Ethernet driver for Linux. */
2
/*
3
        Written 1997-1998 by Donald Becker.
4
 
5
        This software may be used and distributed according to the terms
6
        of the GNU Public License, incorporated herein by reference.
7
    All other rights reserved.
8
 
9
        This driver is for boards based on the RTL8129 and RTL8139 PCI ethernet
10
        chips.
11
 
12
        The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
13
        Center of Excellence in Space Data and Information Sciences
14
           Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
15
 
16
        Support and updates available at
17
        http://cesdis.gsfc.nasa.gov/linux/drivers/rtl8139.html
18
 
19
        Twister-tuning code contributed by Kinston <shangh@realtek.com.tw>.
20
*/
21
 
22
static const char *version =
23
"rtl8139.c:v1.04 9/22/98 Donald Becker http://cesdis.gsfc.nasa.gov/linux/drivers/rtl8139.html\n";
24
 
25
/* A few user-configurable values. */
26
/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
27
static int max_interrupt_work = 20;
28
#define rtl8129_debug debug
29
static int rtl8129_debug = 1;
30
 
31
/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
32
   The RTL chips use a 64 element hash table based on the Ethernet CRC.  */
33
static int multicast_filter_limit = 32;
34
 
35
/* Used to pass the full-duplex flag, etc. */
36
#define MAX_UNITS 8             /* More are supported, limit only on options */
37
static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
38
static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
39
 
40
/* Size of the in-memory receive ring. */
41
#define RX_BUF_LEN_IDX  3                       /* 0==8K, 1==16K, 2==32K, 3==64K */
42
#define RX_BUF_LEN (8192 << RX_BUF_LEN_IDX)
43
/* Size of the Tx bounce buffers -- must be at least (dev->mtu+14+4). */
44
#define TX_BUF_SIZE     1536
45
 
46
/* PCI Tuning Parameters
47
   Threshold is bytes transferred to chip before transmission starts. */
48
#define TX_FIFO_THRESH 256      /* In bytes, rounded down to 32 byte units. */
49
 
50
/* The following settings are log_2(bytes)-4:  0 == 16 bytes .. 6==1024. */
51
#define RX_FIFO_THRESH  4               /* Rx buffer level before first PCI xfer.  */
52
#define RX_DMA_BURST    4               /* Maximum PCI burst, '4' is 256 bytes */
53
#define TX_DMA_BURST    4               /* Calculate as 16<<val. */
54
 
55
/* Operational parameters that usually are not changed. */
56
/* Time in jiffies before concluding the transmitter is hung. */
57
#define TX_TIMEOUT  (4*HZ)
58
 
59
#include <linux/config.h>
60
#ifdef MODULE
61
#ifdef MODVERSIONS
62
#include <linux/modversions.h>
63
#endif
64
#include <linux/module.h>
65
#include <linux/version.h>
66
#else
67
#define MOD_INC_USE_COUNT
68
#define MOD_DEC_USE_COUNT
69
#endif
70
 
71
#include <linux/kernel.h>
72
#include <linux/sched.h>
73
#include <linux/string.h>
74
#include <linux/timer.h>
75
#include <linux/errno.h>
76
#include <linux/ioport.h>
77
#include <linux/malloc.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 <asm/processor.h>              /* Processor type for cache alignment. */
84
#include <asm/bitops.h>
85
#include <asm/io.h>
86
 
87
/* Kernel compatibility defines, some common to David Hind's PCMCIA package.
88
   This is only in the support-all-kernels source code. */
89
 
90
#define RUN_AT(x) (jiffies + (x))
91
 
92
#include <linux/delay.h>
93
 
94
#if LINUX_VERSION_CODE < 0x20123
95
#define test_and_set_bit(val, addr) set_bit(val, addr)
96
#endif
97
#if LINUX_VERSION_CODE <= 0x20139
98
#define net_device_stats enet_statistics
99
#else
100
#define NETSTATS_VER2
101
#endif
102
#if LINUX_VERSION_CODE < 0x20155  ||  defined(CARDBUS)
103
/* Grrrr, the PCI code changed, but did not consider CardBus... */
104
#include <linux/bios32.h>
105
#define PCI_SUPPORT_VER1
106
#else
107
#define PCI_SUPPORT_VER2
108
#endif
109
#if LINUX_VERSION_CODE < 0x20159
110
#define dev_free_skb(skb) dev_kfree_skb(skb, FREE_WRITE);
111
#else
112
#define dev_free_skb(skb) dev_kfree_skb(skb);
113
#endif
114
 
115
/* The I/O extent. */
116
#define RTL8129_TOTAL_SIZE 0x80
117
 
118
/*
119
                                Theory of Operation
120
 
121
I. Board Compatibility
122
 
123
This device driver is designed for the RealTek RTL8129, the RealTek Fast
124
Ethernet controllers for PCI.  This chip is used on a few clone boards.
125
 
126
 
127
II. Board-specific settings
128
 
129
PCI bus devices are configured by the system at boot time, so no jumpers
130
need to be set on the board.  The system BIOS will assign the
131
PCI INTA signal to a (preferably otherwise unused) system IRQ line.
132
Note: Kernel versions earlier than 1.3.73 do not support shared PCI
133
interrupt lines.
134
 
135
III. Driver operation
136
 
137
IIIa. Rx Ring buffers
138
 
139
The receive unit uses a single linear ring buffer rather than the more
140
common (and more efficient) descriptor-based architecture.  Incoming frames
141
are sequentially stored into the Rx region, and the host copies them into
142
skbuffs.
143
 
144
Comment: While it is theoretically possible to process many frames in place,
145
any delay in Rx processing would cause us to drop frames.  More importantly,
146
the Linux protocol stack is not designed to operate in this manner.
147
 
148
IIIb. Tx operation
149
 
150
The RTL8129 uses a fixed set of four Tx descriptors in register space.
151
In a stunningly bad design choice, Tx frames must be 32 bit aligned.  Linux
152
aligns the IP header on word boundaries, and 14 byte ethernet header means
153
that almost all frames will need to be copied to an alignment buffer.
154
 
155
IVb. References
156
 
157
http://www.realtek.com.tw/cn/cn.html
158
http://cesdis.gsfc.nasa.gov/linux/misc/NWay.html
159
 
160
IVc. Errata
161
 
162
*/
163
 
164
 
165
/* This table drives the PCI probe routines.  It's mostly boilerplate in all
166
   of the drivers, and will likely be provided by some future kernel.
167
   Note the matching code -- the first table entry matchs all 56** cards but
168
   second only the 1234 card.
169
*/
170
enum pci_flags_bit {
171
        PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
172
        PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
173
};
174
struct pci_id_info {
175
        const char *name;
176
        u16     vendor_id, device_id, device_id_mask, flags;
177
        int io_size;
178
        struct device *(*probe1)(int pci_bus, int pci_devfn, struct device *dev,
179
                                                         long ioaddr, int irq, int chip_idx, int fnd_cnt);
180
};
181
 
182
static struct device * rtl8129_probe1(int pci_bus, int pci_devfn,
183
                                                                          struct device *dev, long ioaddr,
184
                                                                          int irq, int chp_idx, int fnd_cnt);
185
 
186
static struct pci_id_info pci_tbl[] =
187
{{ "RealTek RTL8129 Fast Ethernet",
188
   0x10ec, 0x8129, 0xffff, PCI_USES_IO|PCI_USES_MASTER, 0x80, rtl8129_probe1},
189
 { "RealTek RTL8139 Fast Ethernet",
190
   0x10ec, 0x8139, 0xffff, PCI_USES_IO|PCI_USES_MASTER, 0x80, rtl8129_probe1},
191
 { "RealTek RTL8139 Fast Ethernet (mislabeled)",
192
   0x1113, 0x1211, 0xffff, PCI_USES_IO|PCI_USES_MASTER, 0x80, rtl8129_probe1},
193
 {0,},                                           /* 0 terminated list. */
194
};
195
 
196
/* The capability table matches the chip table above. */
197
enum {HAS_MII_XCVR=0x01, HAS_CHIP_XCVR=0x02, HAS_LNK_CHNG=0x04};
198
static int rtl_cap_tbl[] = {
199
        HAS_MII_XCVR, HAS_CHIP_XCVR|HAS_LNK_CHNG, HAS_CHIP_XCVR|HAS_LNK_CHNG,
200
};
201
 
202
 
203
/* The rest of these values should never change. */
204
#define NUM_TX_DESC     4                       /* Number of Tx descriptor registers. */
205
 
206
/* Symbolic offsets to registers. */
207
enum RTL8129_registers {
208
        MAC0=0,                                          /* Ethernet hardware address. */
209
        MAR0=8,                                         /* Multicast filter. */
210
        TxStatus0=0x10,                         /* Transmit status (Four 32bit registers). */
211
        TxAddr0=0x20,                           /* Tx descriptors (also four 32bit). */
212
        RxBuf=0x30, RxEarlyCnt=0x34, RxEarlyStatus=0x36,
213
        ChipCmd=0x37, RxBufPtr=0x38, RxBufAddr=0x3A,
214
        IntrMask=0x3C, IntrStatus=0x3E,
215
        TxConfig=0x40, RxConfig=0x44,
216
        Timer=0x48,                                     /* A general-purpose counter. */
217
        RxMissed=0x4C,                          /* 24 bits valid, write clears. */
218
        Cfg9346=0x50, Config0=0x51, Config1=0x52,
219
        FlashReg=0x54, GPPinData=0x58, GPPinDir=0x59, MII_SMI=0x5A, HltClk=0x5B,
220
        MultiIntr=0x5C, TxSummary=0x60,
221
        MII_BMCR=0x62, MII_BMSR=0x64, NWayAdvert=0x66, NWayLPAR=0x68,
222
        NWayExpansion=0x6A,
223
        /* Undocumented registers, but required for proper operation. */
224
        FIFOTMS=0x70,   /* FIFO Test Mode Select */
225
        CSCR=0x74,      /* Chip Status and Configuration Register. */
226
        PARA78=0x78, PARA7c=0x7c,       /* Magic transceiver parameter register. */
227
};
228
 
229
enum ChipCmdBits {
230
        CmdReset=0x10, CmdRxEnb=0x08, CmdTxEnb=0x04, RxBufEmpty=0x01, };
231
 
232
/* Interrupt register bits, using my own meaningful names. */
233
enum IntrStatusBits {
234
        PCIErr=0x8000, PCSTimeout=0x4000,
235
        RxFIFOOver=0x40, RxUnderrun=0x20, RxOverflow=0x10,
236
        TxErr=0x08, TxOK=0x04, RxErr=0x02, RxOK=0x01,
237
};
238
enum TxStatusBits {
239
        TxHostOwns=0x2000, TxUnderrun=0x4000, TxStatOK=0x8000,
240
        TxOutOfWindow=0x20000000, TxAborted=0x40000000, TxCarrierLost=0x80000000,
241
};
242
enum RxStatusBits {
243
        RxMulticast=0x8000, RxPhysical=0x4000, RxBroadcast=0x2000,
244
        RxBadSymbol=0x0020, RxRunt=0x0010, RxTooLong=0x0008, RxCRCErr=0x0004,
245
        RxBadAlign=0x0002, RxStatusOK=0x0001,
246
};
247
 
248
enum CSCRBits {
249
        CSCR_LinkOKBit=0x0400, CSCR_LinkChangeBit=0x0800,
250
        CSCR_LinkStatusBits=0x0f000, CSCR_LinkDownOffCmd=0x003c0,
251
        CSCR_LinkDownCmd=0x0f3c0,
252
};
253
 
254
/* Twister tuning parameters from RealTek.  Completely undocumented. */
255
unsigned long param[4][4]={
256
        {0x0cb39de43,0x0cb39ce43,0x0fb38de03,0x0cb38de43},
257
        {0x0cb39de43,0x0cb39ce43,0x0cb39ce83,0x0cb39ce83},
258
        {0x0cb39de43,0x0cb39ce43,0x0cb39ce83,0x0cb39ce83},
259
        {0x0bb39de43,0x0bb39ce43,0x0bb39ce83,0x0bb39ce83}
260
};
261
 
262
struct rtl8129_private {
263
        char devname[8];                        /* Used only for kernel debugging. */
264
        const char *product_name;
265
        struct device *next_module;
266
        int chip_id;
267
        int chip_revision;
268
        unsigned char pci_bus, pci_devfn;
269
#if LINUX_VERSION_CODE > 0x20139
270
        struct net_device_stats stats;
271
#else
272
        struct enet_statistics stats;
273
#endif
274
        struct timer_list timer;        /* Media selection timer. */
275
        unsigned int cur_rx;            /* Index into the Rx buffer of next Rx pkt. */
276
        unsigned int cur_tx, dirty_tx, tx_flag;
277
        /* The saved address of a sent-in-place packet/buffer, for skfree(). */
278
        struct sk_buff* tx_skbuff[NUM_TX_DESC];
279
        unsigned char *tx_buf[NUM_TX_DESC];     /* Tx bounce buffers */
280
        unsigned char *rx_ring;
281
        unsigned char *tx_bufs;                         /* Tx bounce buffer region. */
282
        char phys[4];                                           /* MII device addresses. */
283
        unsigned int tx_full:1;                         /* The Tx queue is full. */
284
        unsigned int full_duplex:1;                     /* Full-duplex operation requested. */
285
        unsigned int duplex_lock:1;                     /* Full-duplex operation requested. */
286
        unsigned int default_port:4;            /* Last dev->if_port value. */
287
        unsigned int media2:4;                          /* Secondary monitored media port. */
288
        unsigned int medialock:1;                       /* Don't sense media type. */
289
        unsigned int mediasense:1;                      /* Media sensing in progress. */
290
};
291
 
292
#ifdef MODULE
293
#if LINUX_VERSION_CODE > 0x20115
294
MODULE_AUTHOR("Donald Becker <becker@cesdis.gsfc.nasa.gov>");
295
MODULE_DESCRIPTION("RealTek RTL8129/8139 Fast Ethernet driver");
296
MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
297
MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
298
MODULE_PARM(multicast_filter_limit, "i");
299
MODULE_PARM(max_interrupt_work, "i");
300
MODULE_PARM(debug, "i");
301
#endif
302
#endif
303
 
304
static int rtl8129_open(struct device *dev);
305
static int read_eeprom(long ioaddr, int location);
306
static int mdio_read(struct device *dev, int phy_id, int location);
307
static void mdio_write(struct device *dev, int phy_id, int location, int val);
308
static void rtl8129_timer(unsigned long data);
309
static void rtl8129_tx_timeout(struct device *dev);
310
static void rtl8129_init_ring(struct device *dev);
311
static int rtl8129_start_xmit(struct sk_buff *skb, struct device *dev);
312
static int rtl8129_rx(struct device *dev);
313
static void rtl8129_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
314
static int rtl8129_close(struct device *dev);
315
static int mii_ioctl(struct device *dev, struct ifreq *rq, int cmd);
316
static struct enet_statistics *rtl8129_get_stats(struct device *dev);
317
static inline u32 ether_crc(int length, unsigned char *data);
318
static void set_rx_mode(struct device *dev);
319
 
320
 
321
/* A list of all installed RTL8129 devices, for removing the driver module. */
322
static struct device *root_rtl8129_dev = NULL;
323
 
324
/* Ideally we would detect all network cards in slot order.  That would
325
   be best done a central PCI probe dispatch, which wouldn't work
326
   well when dynamically adding drivers.  So instead we detect just the
327
   Rtl81*9 cards in slot order. */
328
 
329
int rtl8139_probe(struct device *dev)
330
{
331
        int cards_found = 0;
332
        int pci_index = 0;
333
        unsigned char pci_bus, pci_device_fn;
334
 
335
        if ( ! pcibios_present())
336
                return -ENODEV;
337
 
338
        for (;pci_index < 0xff; pci_index++) {
339
                u16 vendor, device, pci_command, new_command;
340
                int chip_idx, irq;
341
                long ioaddr;
342
 
343
                if (pcibios_find_class (PCI_CLASS_NETWORK_ETHERNET << 8, pci_index,
344
                                                                &pci_bus, &pci_device_fn)
345
                        != PCIBIOS_SUCCESSFUL)
346
                        break;
347
                pcibios_read_config_word(pci_bus, pci_device_fn,
348
                                                                 PCI_VENDOR_ID, &vendor);
349
                pcibios_read_config_word(pci_bus, pci_device_fn,
350
                                                                 PCI_DEVICE_ID, &device);
351
 
352
                for (chip_idx = 0; pci_tbl[chip_idx].vendor_id; chip_idx++)
353
                        if (vendor == pci_tbl[chip_idx].vendor_id
354
                                && (device & pci_tbl[chip_idx].device_id_mask) ==
355
                                pci_tbl[chip_idx].device_id)
356
                                break;
357
                if (pci_tbl[chip_idx].vendor_id == 0)            /* Compiled out! */
358
                        continue;
359
 
360
                {
361
#if defined(PCI_SUPPORT_VER2)
362
                        struct pci_dev *pdev = pci_find_slot(pci_bus, pci_device_fn);
363
                        ioaddr = pdev->base_address[0] & ~3;
364
                        irq = pdev->irq;
365
#else
366
                        u32 pci_ioaddr;
367
                        u8 pci_irq_line;
368
                        pcibios_read_config_byte(pci_bus, pci_device_fn,
369
                                                                         PCI_INTERRUPT_LINE, &pci_irq_line);
370
                        pcibios_read_config_dword(pci_bus, pci_device_fn,
371
                                                                          PCI_BASE_ADDRESS_0, &pci_ioaddr);
372
                        ioaddr = pci_ioaddr & ~3;
373
                        irq = pci_irq_line;
374
#endif
375
                }
376
 
377
                if ((pci_tbl[chip_idx].flags & PCI_USES_IO) &&
378
                        check_region(ioaddr, pci_tbl[chip_idx].io_size))
379
                        continue;
380
 
381
                /* Activate the card: fix for brain-damaged Win98 BIOSes. */
382
                pcibios_read_config_word(pci_bus, pci_device_fn,
383
                                                                 PCI_COMMAND, &pci_command);
384
                new_command = pci_command | (pci_tbl[chip_idx].flags & 7);
385
                if (pci_command != new_command) {
386
                        printk(KERN_INFO "  The PCI BIOS has not enabled the"
387
                                   " device at %d/%d!  Updating PCI command %4.4x->%4.4x.\n",
388
                                   pci_bus, pci_device_fn, pci_command, new_command);
389
                        pcibios_write_config_word(pci_bus, pci_device_fn,
390
                                                                          PCI_COMMAND, new_command);
391
                }
392
 
393
                dev = pci_tbl[chip_idx].probe1(pci_bus, pci_device_fn, dev, ioaddr,
394
                                                                           irq, chip_idx, cards_found);
395
 
396
                if (dev  && (pci_tbl[chip_idx].flags & PCI_COMMAND_MASTER)) {
397
                        u8 pci_latency;
398
                        pcibios_read_config_byte(pci_bus, pci_device_fn,
399
                                                                         PCI_LATENCY_TIMER, &pci_latency);
400
                        if (pci_latency < 32) {
401
                                printk(KERN_NOTICE "  PCI latency timer (CFLT) is "
402
                                           "unreasonably low at %d.  Setting to 64 clocks.\n",
403
                                           pci_latency);
404
                                pcibios_write_config_byte(pci_bus, pci_device_fn,
405
                                                                                  PCI_LATENCY_TIMER, 64);
406
                        }
407
                }
408
                dev = 0;
409
                cards_found++;
410
        }
411
 
412
        return cards_found ? 0 : -ENODEV;
413
}
414
 
415
static struct device * rtl8129_probe1(int pci_bus, int pci_devfn,
416
                                                                          struct device *dev, long ioaddr,
417
                                                                          int irq, int chip_idx, int found_cnt)
418
{
419
        static int did_version = 0;                      /* Already printed version info. */
420
        struct rtl8129_private *tp;
421
        int i, option = found_cnt < MAX_UNITS ? options[found_cnt] : 0;
422
 
423
        if (rtl8129_debug > 0  &&  did_version++ == 0)
424
                printk(KERN_INFO "%s", version);
425
 
426
        dev = init_etherdev(dev, 0);
427
 
428
        printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ",
429
                   dev->name, pci_tbl[chip_idx].name, ioaddr, irq);
430
 
431
        /* Bring the chip out of low-power mode. */
432
        outb(0x00, ioaddr + Config1);
433
 
434
        if (read_eeprom(ioaddr, 0) != 0xffff)
435
                for (i = 0; i < 3; i++)
436
                        ((u16 *)(dev->dev_addr))[i] = read_eeprom(ioaddr, i + 7);
437
        else
438
                for (i = 0; i < 6; i++)
439
                        dev->dev_addr[i] = inb(ioaddr + MAC0 + i);
440
 
441
        for (i = 0; i < 5; i++)
442
                printk("%2.2x:", dev->dev_addr[i]);
443
        printk("%2.2x.\n", dev->dev_addr[i]);
444
 
445
        /* We do a request_region() to register /proc/ioports info. */
446
        request_region(ioaddr, pci_tbl[chip_idx].io_size, dev->name);
447
 
448
        dev->base_addr = ioaddr;
449
        dev->irq = irq;
450
 
451
        /* Some data structures must be quadword aligned. */
452
        tp = kmalloc(sizeof(*tp), GFP_KERNEL | GFP_DMA);
453
        memset(tp, 0, sizeof(*tp));
454
        dev->priv = tp;
455
 
456
        tp->next_module = root_rtl8129_dev;
457
        root_rtl8129_dev = dev;
458
 
459
        tp->chip_id = chip_idx;
460
        tp->pci_bus = pci_bus;
461
        tp->pci_devfn = pci_devfn;
462
 
463
        /* Find the connected MII xcvrs.
464
           Doing this in open() would allow detecting external xcvrs later, but
465
           takes too much time. */
466
        if (rtl_cap_tbl[chip_idx] & HAS_MII_XCVR) {
467
                int phy, phy_idx;
468
                for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(tp->phys);
469
                         phy++) {
470
                        int mii_status = mdio_read(dev, phy, 1);
471
                        if (mii_status != 0xffff  && mii_status != 0x0000) {
472
                                tp->phys[phy_idx++] = phy;
473
                                printk(KERN_INFO "%s: MII transceiver found at address %d.\n",
474
                                           dev->name, phy);
475
                        }
476
                }
477
                if (phy_idx == 0) {
478
                        printk(KERN_INFO "%s: No MII transceivers found!  Assuming SYM "
479
                                   "transceiver.\n",
480
                                   dev->name);
481
                        tp->phys[0] = -1;
482
                }
483
        } else {
484
                        tp->phys[0] = 32;
485
        }
486
 
487
        /* Put the chip into low-power mode. */
488
        outb(0xC0, ioaddr + Cfg9346);
489
        outb(0x03, ioaddr + Config1);
490
        outb('H', ioaddr + HltClk);             /* 'R' would leave the clock running. */
491
 
492
        /* The lower four bits are the media type. */
493
        if (option > 0) {
494
                tp->full_duplex = (option & 0x200) ? 1 : 0;
495
                tp->default_port = option & 15;
496
                if (tp->default_port)
497
                        tp->medialock = 1;
498
        }
499
 
500
        if (found_cnt < MAX_UNITS  &&  full_duplex[found_cnt] > 0)
501
                tp->full_duplex = full_duplex[found_cnt];
502
 
503
        if (tp->full_duplex) {
504
                printk(KERN_INFO "%s: Media type forced to Full Duplex.\n", dev->name);
505
                mdio_write(dev, tp->phys[0], 4, 0x141);
506
                tp->duplex_lock = 1;
507
        }
508
 
509
        /* The Rtl8129-specific entries in the device structure. */
510
        dev->open = &rtl8129_open;
511
        dev->hard_start_xmit = &rtl8129_start_xmit;
512
        dev->stop = &rtl8129_close;
513
        dev->get_stats = &rtl8129_get_stats;
514
        dev->set_multicast_list = &set_rx_mode;
515
        dev->do_ioctl = &mii_ioctl;
516
 
517
        return dev;
518
}
519
 
520
/* Serial EEPROM section. */
521
 
522
/*  EEPROM_Ctrl bits. */
523
#define EE_SHIFT_CLK    0x04    /* EEPROM shift clock. */
524
#define EE_CS                   0x08    /* EEPROM chip select. */
525
#define EE_DATA_WRITE   0x02    /* EEPROM chip data in. */
526
#define EE_WRITE_0              0x00
527
#define EE_WRITE_1              0x02
528
#define EE_DATA_READ    0x01    /* EEPROM chip data out. */
529
#define EE_ENB                  (0x80 | EE_CS)
530
 
531
/* Delay between EEPROM clock transitions.
532
   No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
533
 */
534
 
535
#define eeprom_delay()  inl(ee_addr)
536
 
537
/* The EEPROM commands include the alway-set leading bit. */
538
#define EE_WRITE_CMD    (5 << 6)
539
#define EE_READ_CMD             (6 << 6)
540
#define EE_ERASE_CMD    (7 << 6)
541
 
542
static int read_eeprom(long ioaddr, int location)
543
{
544
        int i;
545
        unsigned retval = 0;
546
        long ee_addr = ioaddr + Cfg9346;
547
        int read_cmd = location | EE_READ_CMD;
548
 
549
        outb(EE_ENB & ~EE_CS, ee_addr);
550
        outb(EE_ENB, ee_addr);
551
 
552
        /* Shift the read command bits out. */
553
        for (i = 10; i >= 0; i--) {
554
                int dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
555
                outb(EE_ENB | dataval, ee_addr);
556
                eeprom_delay();
557
                outb(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
558
                eeprom_delay();
559
        }
560
        outb(EE_ENB, ee_addr);
561
        eeprom_delay();
562
 
563
        for (i = 16; i > 0; i--) {
564
                outb(EE_ENB | EE_SHIFT_CLK, ee_addr);
565
                eeprom_delay();
566
                retval = (retval << 1) | ((inb(ee_addr) & EE_DATA_READ) ? 1 : 0);
567
                outb(EE_ENB, ee_addr);
568
                eeprom_delay();
569
        }
570
 
571
        /* Terminate the EEPROM access. */
572
        outb(~EE_CS, ee_addr);
573
        return retval;
574
}
575
 
576
/* MII serial management: mostly bogus for now. */
577
/* Read and write the MII management registers using software-generated
578
   serial MDIO protocol.
579
   The maximum data clock rate is 2.5 Mhz.  The minimum timing is usually
580
   met by back-to-back PCI I/O cycles, but we insert a delay to avoid
581
   "overclocking" issues. */
582
#define MDIO_DIR                0x80
583
#define MDIO_DATA_OUT   0x04
584
#define MDIO_DATA_IN    0x02
585
#define MDIO_CLK                0x01
586
#define MDIO_WRITE0 (MDIO_DIR)
587
#define MDIO_WRITE1 (MDIO_DIR | MDIO_DATA_OUT)
588
 
589
#define mdio_delay()    inb(mdio_addr)
590
 
591
static char mii_2_8139_map[8] = {MII_BMCR, MII_BMSR, 0, 0, NWayAdvert,
592
                                                                 NWayLPAR, NWayExpansion, 0 };
593
 
594
/* Syncronize the MII management interface by shifting 32 one bits out. */
595
static void mdio_sync(long mdio_addr)
596
{
597
        int i;
598
 
599
        for (i = 32; i >= 0; i--) {
600
                outb(MDIO_WRITE1, mdio_addr);
601
                mdio_delay();
602
                outb(MDIO_WRITE1 | MDIO_CLK, mdio_addr);
603
                mdio_delay();
604
        }
605
        return;
606
}
607
static int mdio_read(struct device *dev, int phy_id, int location)
608
{
609
        long mdio_addr = dev->base_addr + MII_SMI;
610
        int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
611
        int retval = 0;
612
        int i;
613
 
614
        if ((phy_id & 0x1f) == 0) {      /* Really a 8139.  Use internal registers. */
615
                return location < 8 && mii_2_8139_map[location] ?
616
                        inw(dev->base_addr + mii_2_8139_map[location]) : 0;
617
        }
618
        mdio_sync(mdio_addr);
619
        /* Shift the read command bits out. */
620
        for (i = 15; i >= 0; i--) {
621
                int dataval = (mii_cmd & (1 << i)) ? MDIO_DATA_OUT : 0;
622
 
623
                outb(MDIO_DIR | dataval, mdio_addr);
624
                mdio_delay();
625
                outb(MDIO_DIR | dataval | MDIO_CLK, mdio_addr);
626
                mdio_delay();
627
        }
628
 
629
        /* Read the two transition, 16 data, and wire-idle bits. */
630
        for (i = 19; i > 0; i--) {
631
                outb(0, mdio_addr);
632
                mdio_delay();
633
                retval = (retval << 1) | ((inb(mdio_addr) & MDIO_DATA_IN) ? 1 : 0);
634
                outb(MDIO_CLK, mdio_addr);
635
                mdio_delay();
636
        }
637
        return (retval>>1) & 0xffff;
638
}
639
 
640
static void mdio_write(struct device *dev, int phy_id, int location, int value)
641
{
642
        long mdio_addr = dev->base_addr + MII_SMI;
643
        int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
644
        int i;
645
 
646
        if (phy_id == 32) {                     /* Really a 8139.  Use internal registers. */
647
                if (location < 8  &&  mii_2_8139_map[location])
648
                        outw(value, dev->base_addr + mii_2_8139_map[location]);
649
                return;
650
        }
651
        mdio_sync(mdio_addr);
652
 
653
        /* Shift the command bits out. */
654
        for (i = 31; i >= 0; i--) {
655
                int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
656
                outb(dataval, mdio_addr);
657
                mdio_delay();
658
                outb(dataval | MDIO_CLK, mdio_addr);
659
                mdio_delay();
660
        }
661
        /* Clear out extra bits. */
662
        for (i = 2; i > 0; i--) {
663
                outb(0, mdio_addr);
664
                mdio_delay();
665
                outb(MDIO_CLK, mdio_addr);
666
                mdio_delay();
667
        }
668
        return;
669
}
670
 
671
 
672
static int
673
rtl8129_open(struct device *dev)
674
{
675
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
676
        long ioaddr = dev->base_addr;
677
        int i;
678
 
679
        /* Soft reset the chip. */
680
        outb(CmdReset, ioaddr + ChipCmd);
681
 
682
        if (request_irq(dev->irq, &rtl8129_interrupt, SA_SHIRQ, dev->name, dev)) {
683
                return -EAGAIN;
684
        }
685
 
686
        MOD_INC_USE_COUNT;
687
 
688
        tp->tx_bufs = kmalloc(TX_BUF_SIZE * NUM_TX_DESC, GFP_KERNEL);
689
        tp->rx_ring = kmalloc(RX_BUF_LEN + 16, GFP_KERNEL);
690
        if (tp->tx_bufs == NULL ||  tp->rx_ring == NULL) {
691
                if (tp->tx_bufs)
692
                        kfree(tp->tx_bufs);
693
                if (rtl8129_debug > 0)
694
                        printk(KERN_ERR "%s: Couldn't allocate a %d byte receive ring.\n",
695
                                   dev->name, RX_BUF_LEN);
696
                return -ENOMEM;
697
        }
698
        rtl8129_init_ring(dev);
699
 
700
        /* Check that the chip has finished the reset. */
701
        for (i = 1000; i > 0; i--)
702
                if ((inb(ioaddr + ChipCmd) & CmdReset) == 0)
703
                        break;
704
 
705
        for (i = 0; i < 6; i++)
706
                outb(dev->dev_addr[i], ioaddr + MAC0 + i);
707
 
708
        /* Must enable Tx/Rx before setting transfer thresholds! */
709
        outb(CmdRxEnb | CmdTxEnb, ioaddr + ChipCmd);
710
        outl((RX_FIFO_THRESH << 13) | (RX_BUF_LEN_IDX << 11) | (RX_DMA_BURST<<8),
711
                 ioaddr + RxConfig);
712
        outl((TX_DMA_BURST<<8)|0x03000000, ioaddr + TxConfig);
713
        tp->tx_flag = (TX_FIFO_THRESH<<11) & 0x003f0000;
714
 
715
        tp->full_duplex = tp->duplex_lock;
716
        if (tp->phys[0] >= 0  ||  (rtl_cap_tbl[tp->chip_id] & HAS_MII_XCVR)) {
717
                u16 mii_reg5 = mdio_read(dev, tp->phys[0], 5);
718
                if (mii_reg5 == 0xffff)
719
                        ;                                       /* Not there */
720
                else if ((mii_reg5 & 0x0100) == 0x0100
721
                                 || (mii_reg5 & 0x00C0) == 0x0040)
722
                        tp->full_duplex = 1;
723
                if (rtl8129_debug > 1)
724
                        printk(KERN_INFO"%s: Setting %s%s-duplex based on"
725
                                   " auto-negotiated partner ability %4.4x.\n", dev->name,
726
                                   mii_reg5 == 0 ? "" :
727
                                   (mii_reg5 & 0x0180) ? "100mbps " : "10mbps ",
728
                                   tp->full_duplex ? "full" : "half", mii_reg5);
729
        }
730
 
731
        outb(0xC0, ioaddr + Cfg9346);
732
        outb(tp->full_duplex ? 0x60 : 0x20, ioaddr + Config1);
733
        outb(0x00, ioaddr + Cfg9346);
734
 
735
        outl(virt_to_bus(tp->rx_ring), ioaddr + RxBuf);
736
 
737
        /* Start the chip's Tx and Rx process. */
738
        outl(0, ioaddr + RxMissed);
739
        set_rx_mode(dev);
740
 
741
        outb(CmdRxEnb | CmdTxEnb, ioaddr + ChipCmd);
742
 
743
        dev->tbusy = 0;
744
        dev->interrupt = 0;
745
        dev->start = 1;
746
 
747
        /* Enable all known interrupts by setting the interrupt mask. */
748
        outw(PCIErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver
749
                | TxErr | TxOK | RxErr | RxOK, ioaddr + IntrMask);
750
 
751
        if (rtl8129_debug > 1)
752
                printk(KERN_DEBUG"%s: rtl8129_open() ioaddr %#lx IRQ %d"
753
                           " GP Pins %2.2x %s-duplex.\n",
754
                           dev->name, ioaddr, dev->irq, inb(ioaddr + GPPinData),
755
                           tp->full_duplex ? "full" : "half");
756
 
757
        /* Set the timer to switch to check for link beat and perhaps switch
758
           to an alternate media type. */
759
        init_timer(&tp->timer);
760
        tp->timer.expires = RUN_AT((24*HZ)/10);                 /* 2.4 sec. */
761
        tp->timer.data = (unsigned long)dev;
762
        tp->timer.function = &rtl8129_timer;                            /* timer handler */
763
        add_timer(&tp->timer);
764
 
765
        return 0;
766
}
767
 
768
static void rtl8129_timer(unsigned long data)
769
{
770
        struct device *dev = (struct device *)data;
771
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
772
        long ioaddr = dev->base_addr;
773
        int next_tick = 0;
774
        int mii_reg5 = mdio_read(dev, tp->phys[0], 5);
775
 
776
        if (! tp->duplex_lock  &&  mii_reg5 != 0xffff) {
777
                int duplex = (mii_reg5&0x0100) || (mii_reg5 & 0x01C0) == 0x0040;
778
                if (tp->full_duplex != duplex) {
779
                        tp->full_duplex = duplex;
780
                        printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d link"
781
                                   " partner ability of %4.4x.\n", dev->name,
782
                                   tp->full_duplex ? "full" : "half", tp->phys[0], mii_reg5);
783
                        outb(0xC0, ioaddr + Cfg9346);
784
                        outb(tp->full_duplex ? 0x60 : 0x20, ioaddr + Config1);
785
                        outb(0x00, ioaddr + Cfg9346);
786
                }
787
                next_tick = 60*HZ;
788
        }
789
 
790
        if (rtl8129_debug > 2) {
791
                if (rtl_cap_tbl[tp->chip_id] & HAS_MII_XCVR)
792
                        printk(KERN_DEBUG"%s: Media selection tick, GP pins %2.2x.\n",
793
                                   dev->name, inb(ioaddr + GPPinData));
794
                else
795
                        printk(KERN_DEBUG"%s: Media selection tick, Link partner %4.4x.\n",
796
                                   dev->name, inw(ioaddr + NWayLPAR));
797
                printk(KERN_DEBUG"%s:  Other registers are IntMask %4.4x IntStatus %4.4x"
798
                           " RxStatus %4.4x.\n",
799
                           dev->name, inw(ioaddr + IntrMask), inw(ioaddr + IntrStatus),
800
                           inl(ioaddr + RxEarlyStatus));
801
                printk(KERN_DEBUG"%s:  Chip config %2.2x %2.2x.\n",
802
                           dev->name, inb(ioaddr + Config0), inb(ioaddr + Config1));
803
        }
804
 
805
        if (next_tick) {
806
                tp->timer.expires = RUN_AT(next_tick);
807
                add_timer(&tp->timer);
808
        }
809
}
810
 
811
static void rtl8129_tx_timeout(struct device *dev)
812
{
813
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
814
        long ioaddr = dev->base_addr;
815
        int mii_reg, i;
816
 
817
        if (rtl8129_debug > 0)
818
                printk(KERN_WARNING "%s: Transmit timeout, status %2.2x %4.4x "
819
                           "media %2.2x.\n",
820
                           dev->name, inb(ioaddr + ChipCmd), inw(ioaddr + IntrStatus),
821
                           inb(ioaddr + GPPinData));
822
 
823
        /* Disable interrupts by clearing the interrupt mask. */
824
        outw(0x0000, ioaddr + IntrMask);
825
        /* Emit info to figure out what went wrong. */
826
        printk("%s: Tx queue start entry %d  dirty entry %d.\n",
827
                   dev->name, tp->cur_tx, tp->dirty_tx);
828
        for (i = 0; i < NUM_TX_DESC; i++)
829
                printk(KERN_DEBUG"%s:  Tx descriptor %d is %8.8x.%s\n",
830
                           dev->name, i, inl(ioaddr + TxStatus0 + i*4),
831
                           i == tp->dirty_tx % NUM_TX_DESC ? " (queue head)" : "");
832
        printk(KERN_DEBUG"%s: MII #%d registers are:", dev->name, tp->phys[0]);
833
        for (mii_reg = 0; mii_reg < 8; mii_reg++)
834
                printk(" %4.4x", mdio_read(dev, tp->phys[0], mii_reg));
835
        printk(".\n");
836
 
837
        /* Soft reset the chip. */
838
        outb(CmdReset, ioaddr + ChipCmd);
839
        /* Check that the chip has finished the reset. */
840
        for (i = 1000; i > 0; i--)
841
                if ((inb(ioaddr + ChipCmd) & CmdReset) == 0)
842
                        break;
843
        for (i = 0; i < 6; i++)
844
                outb(dev->dev_addr[i], ioaddr + MAC0 + i);
845
 
846
        outb(0x00, ioaddr + Cfg9346);
847
        tp->cur_rx = 0;
848
        /* Must enable Tx/Rx before setting transfer thresholds! */
849
        outb(CmdRxEnb | CmdTxEnb, ioaddr + ChipCmd);
850
        outl((RX_FIFO_THRESH << 13) | (RX_BUF_LEN_IDX << 11) | (RX_DMA_BURST<<8),
851
                 ioaddr + RxConfig);
852
        outl((TX_DMA_BURST<<8), ioaddr + TxConfig);
853
        set_rx_mode(dev);
854
        {                                                       /* Save the unsent Tx packets. */
855
                struct sk_buff *saved_skb[NUM_TX_DESC], *skb;
856
                int j;
857
                for (j = 0; tp->cur_tx - tp->dirty_tx > 0 ; j++, tp->dirty_tx++)
858
                        saved_skb[j] = tp->tx_skbuff[tp->dirty_tx % NUM_TX_DESC];
859
                tp->dirty_tx = tp->cur_tx = 0;
860
 
861
                for (i = 0; i < j; i++) {
862
                        skb = tp->tx_skbuff[i] = saved_skb[i];
863
                        if ((long)skb->data & 3) {              /* Must use alignment buffer. */
864
                                memcpy(tp->tx_buf[i], skb->data, skb->len);
865
                                outl(virt_to_bus(tp->tx_buf[i]), ioaddr + TxAddr0 + i*4);
866
                        } else
867
                                outl(virt_to_bus(skb->data), ioaddr + TxAddr0 + i*4);
868
                        /* Note: the chip doesn't have auto-pad! */
869
                        outl(tp->tx_flag | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN),
870
                                 ioaddr + TxStatus0 + i*4);
871
                }
872
                tp->cur_tx = i;
873
                while (i < NUM_TX_DESC)
874
                        tp->tx_skbuff[i] = 0;
875
                if (tp->cur_tx - tp->dirty_tx < NUM_TX_DESC) {/* Typical path */
876
                        dev->tbusy = 0;
877
                        tp->tx_full = 0;
878
                } else {
879
                        tp->tx_full = 1;
880
                }
881
        }
882
 
883
        dev->trans_start = jiffies;
884
        tp->stats.tx_errors++;
885
        /* Enable all known interrupts by setting the interrupt mask. */
886
        outw(PCIErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver
887
                 | TxErr | TxOK | RxErr | RxOK, ioaddr + IntrMask);
888
        return;
889
}
890
 
891
 
892
/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
893
static void
894
rtl8129_init_ring(struct device *dev)
895
{
896
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
897
        int i;
898
 
899
        tp->tx_full = 0;
900
        tp->cur_rx = 0;
901
        tp->dirty_tx = tp->cur_tx = 0;
902
 
903
        for (i = 0; i < NUM_TX_DESC; i++) {
904
                tp->tx_skbuff[i] = 0;
905
                tp->tx_buf[i] = &tp->tx_bufs[i*TX_BUF_SIZE];
906
        }
907
}
908
 
909
static int
910
rtl8129_start_xmit(struct sk_buff *skb, struct device *dev)
911
{
912
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
913
        long ioaddr = dev->base_addr;
914
        int entry;
915
 
916
        /* Block a timer-based transmit from overlapping.  This could better be
917
           done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
918
        if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
919
                if (jiffies - dev->trans_start < TX_TIMEOUT)
920
                        return 1;
921
                rtl8129_tx_timeout(dev);
922
                return 1;
923
        }
924
 
925
        /* Calculate the next Tx descriptor entry. */
926
        entry = tp->cur_tx % NUM_TX_DESC;
927
 
928
        tp->tx_skbuff[entry] = skb;
929
        if ((long)skb->data & 3) {                      /* Must use alignment buffer. */
930
                memcpy(tp->tx_buf[entry], skb->data, skb->len);
931
                outl(virt_to_bus(tp->tx_buf[entry]), ioaddr + TxAddr0 + entry*4);
932
        } else
933
                outl(virt_to_bus(skb->data), ioaddr + TxAddr0 + entry*4);
934
        /* Note: the chip doesn't have auto-pad! */
935
        outl(tp->tx_flag | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN),
936
                 ioaddr + TxStatus0 + entry*4);
937
 
938
        if (++tp->cur_tx - tp->dirty_tx < NUM_TX_DESC) {/* Typical path */
939
                clear_bit(0, (void*)&dev->tbusy);
940
        } else {
941
                tp->tx_full = 1;
942
        }
943
 
944
        dev->trans_start = jiffies;
945
        if (rtl8129_debug > 4)
946
                printk(KERN_DEBUG"%s: Queued Tx packet at %p size %d to slot %d.\n",
947
                           dev->name, skb->data, (int)skb->len, entry);
948
 
949
        return 0;
950
}
951
 
952
/* The interrupt handler does all of the Rx thread work and cleans up
953
   after the Tx thread. */
954
static void rtl8129_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
955
{
956
        struct device *dev = (struct device *)dev_instance;
957
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
958
        int boguscnt = max_interrupt_work;
959
        int status;
960
        long ioaddr = dev->base_addr;
961
 
962
#if defined(__i386__)
963
        /* A lock to prevent simultaneous entry bug on Intel SMP machines. */
964
        if (test_and_set_bit(0, (void*)&dev->interrupt)) {
965
                printk(KERN_ERR"%s: SMP simultaneous entry of an interrupt handler.\n",
966
                           dev->name);
967
                dev->interrupt = 0;      /* Avoid halting machine. */
968
                return;
969
        }
970
#else
971
        if (dev->interrupt) {
972
                printk(KERN_ERR "%s: Re-entering the interrupt handler.\n", dev->name);
973
                return;
974
        }
975
        dev->interrupt = 1;
976
#endif
977
 
978
        do {
979
                status = inw(ioaddr + IntrStatus);
980
                /* Acknowledge all of the current interrupt sources ASAP. */
981
                outw(status, ioaddr + IntrStatus);
982
 
983
                if (rtl8129_debug > 4)
984
                        printk(KERN_DEBUG"%s: interrupt  status=%#4.4x new intstat=%#4.4x.\n",
985
                                   dev->name, status, inw(ioaddr + IntrStatus));
986
 
987
                if ((status & (PCIErr|PCSTimeout|RxUnderrun|RxOverflow|RxFIFOOver
988
                                           |TxErr|TxOK|RxErr|RxOK)) == 0)
989
                        break;
990
 
991
                if (status & (RxOK|RxUnderrun|RxOverflow|RxFIFOOver))/* Rx interrupt */
992
                        rtl8129_rx(dev);
993
 
994
                if (status & (TxOK | TxErr)) {
995
                        unsigned int dirty_tx;
996
 
997
                        for (dirty_tx = tp->dirty_tx; dirty_tx < tp->cur_tx; dirty_tx++) {
998
                                int entry = dirty_tx % NUM_TX_DESC;
999
                                int txstatus = inl(ioaddr + TxStatus0 + entry*4);
1000
 
1001
                                if ( ! (txstatus & (TxStatOK | TxUnderrun | TxAborted)))
1002
                                        break;                  /* It still hasn't been Txed */
1003
 
1004
                                /* Note: TxCarrierLost is always asserted at 100mbps. */
1005
                                if (txstatus & (TxOutOfWindow | TxAborted)) {
1006
                                        /* There was an major error, log it. */
1007
#ifndef final_version
1008
                                        if (rtl8129_debug > 1)
1009
                                                printk(KERN_NOTICE"%s: Transmit error, Tx status %8.8x.\n",
1010
                                                           dev->name, txstatus);
1011
#endif
1012
                                        tp->stats.tx_errors++;
1013
                                        if (txstatus&TxAborted) {
1014
                                                tp->stats.tx_aborted_errors++;
1015
                                                outl((TX_DMA_BURST<<8)|0x03000001, ioaddr + TxConfig);
1016
                                        }
1017
                                        if (txstatus&TxCarrierLost) tp->stats.tx_carrier_errors++;
1018
                                        if (txstatus&TxOutOfWindow) tp->stats.tx_window_errors++;
1019
#ifdef ETHER_STATS
1020
                                        if ((txstatus & 0x0f000000) == 0x0f000000)
1021
                                                tp->stats.collisions16++;
1022
#endif
1023
                                } else {
1024
#ifdef ETHER_STATS
1025
                                        /* No count for tp->stats.tx_deferred */
1026
#endif
1027
                                        if (txstatus & TxUnderrun) {
1028
                                                /* Add 64 to the Tx FIFO threshold. */
1029
                                                if (tp->tx_flag <  0x00300000)
1030
                                                        tp->tx_flag += 0x00020000;
1031
                                                tp->stats.tx_fifo_errors++;
1032
                                        }
1033
                                        tp->stats.collisions += (txstatus >> 24) & 15;
1034
#if LINUX_VERSION_CODE > 0x20119
1035
                                        tp->stats.tx_bytes += txstatus & 0x7ff;
1036
#endif
1037
                                        tp->stats.tx_packets++;
1038
                                }
1039
 
1040
                                /* Free the original skb. */
1041
                                dev_free_skb(tp->tx_skbuff[entry]);
1042
                                tp->tx_skbuff[entry] = 0;
1043
                        }
1044
 
1045
#ifndef final_version
1046
                        if (tp->cur_tx - dirty_tx > NUM_TX_DESC) {
1047
                                printk(KERN_ERR"%s: Out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
1048
                                           dev->name, dirty_tx, tp->cur_tx, tp->tx_full);
1049
                                dirty_tx += NUM_TX_DESC;
1050
                        }
1051
#endif
1052
 
1053
                        if (tp->tx_full  &&  dirty_tx > tp->cur_tx - NUM_TX_DESC) {
1054
                                /* The ring is no longer full, clear tbusy. */
1055
                                tp->tx_full = 0;
1056
                                dev->tbusy = 0;
1057
                                mark_bh(NET_BH);
1058
                        }
1059
 
1060
                        tp->dirty_tx = dirty_tx;
1061
                }
1062
 
1063
                /* Check uncommon events with one test. */
1064
                if (status & (PCIErr|PCSTimeout |RxUnderrun|RxOverflow|RxFIFOOver
1065
                                          |TxErr|RxErr)) {
1066
                        if (rtl8129_debug > 2)
1067
                                printk(KERN_NOTICE"%s: Abnormal interrupt, status %8.8x.\n",
1068
                                           dev->name, status);
1069
 
1070
                        if (status == 0xffffffff)
1071
                                break;
1072
                        /* Update the error count. */
1073
                        tp->stats.rx_missed_errors += inl(ioaddr + RxMissed);
1074
                        outl(0, ioaddr + RxMissed);
1075
 
1076
                        if ((status & RxUnderrun)  &&
1077
                                (rtl_cap_tbl[tp->chip_id] & HAS_LNK_CHNG)) {
1078
                                /* Really link-change on new chips. */
1079
                                int lpar = inw(ioaddr + NWayLPAR);
1080
                                int duplex = (lpar&0x0100)||(lpar & 0x01C0) == 0x0040;
1081
                                if (tp->full_duplex != duplex) {
1082
                                        tp->full_duplex = duplex;
1083
                                        outb(0xC0, ioaddr + Cfg9346);
1084
                                        outb(tp->full_duplex ? 0x60 : 0x20, ioaddr + Config1);
1085
                                        outb(0x00, ioaddr + Cfg9346);
1086
                                }
1087
                                status &= ~RxUnderrun;
1088
                        }
1089
                        if (status & (RxUnderrun | RxOverflow | RxErr | RxFIFOOver))
1090
                                tp->stats.rx_errors++;
1091
 
1092
                        if (status & (PCSTimeout)) tp->stats.rx_length_errors++;
1093
                        if (status & (RxUnderrun|RxFIFOOver)) tp->stats.rx_fifo_errors++;
1094
                        if (status & RxOverflow) {
1095
                                tp->stats.rx_over_errors++;
1096
                                tp->cur_rx = inw(ioaddr + RxBufAddr) % RX_BUF_LEN;
1097
                                outw(tp->cur_rx - 16, ioaddr + RxBufPtr);
1098
                        }
1099
                        if (status & PCIErr) {
1100
                                u32 pci_cmd_status;
1101
                                pcibios_read_config_dword(tp->pci_bus, tp->pci_devfn,
1102
                                                                                  PCI_COMMAND, &pci_cmd_status);
1103
 
1104
                                printk(KERN_ERR "%s: PCI Bus error %4.4x.\n",
1105
                                           dev->name, pci_cmd_status);
1106
                        }
1107
                }
1108
                if (--boguscnt < 0) {
1109
                        printk(KERN_WARNING"%s: Too much work at interrupt, "
1110
                                   "IntrStatus=0x%4.4x.\n",
1111
                                   dev->name, status);
1112
                        /* Clear all interrupt sources. */
1113
                        outw(0xffff, ioaddr + IntrStatus);
1114
                        break;
1115
                }
1116
        } while (1);
1117
 
1118
        if (rtl8129_debug > 3)
1119
                printk(KERN_DEBUG"%s: exiting interrupt, intr_status=%#4.4x.\n",
1120
                           dev->name, inl(ioaddr + IntrStatus));
1121
 
1122
#if defined(__i386__)
1123
        clear_bit(0, (void*)&dev->interrupt);
1124
#else
1125
        dev->interrupt = 0;
1126
#endif
1127
        return;
1128
}
1129
 
1130
/* The data sheet doesn't describe the Rx ring at all, so I'm guessing at the
1131
   field alignments and semantics. */
1132
static int rtl8129_rx(struct device *dev)
1133
{
1134
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
1135
        long ioaddr = dev->base_addr;
1136
        unsigned char *rx_ring = tp->rx_ring;
1137
        u16 cur_rx = tp->cur_rx;
1138
 
1139
        if (rtl8129_debug > 4)
1140
                printk(KERN_DEBUG"%s: In rtl8129_rx(), current %4.4x BufAddr %4.4x,"
1141
                           " free to %4.4x, Cmd %2.2x.\n",
1142
                           dev->name, cur_rx, inw(ioaddr + RxBufAddr),
1143
                           inw(ioaddr + RxBufPtr), inb(ioaddr + ChipCmd));
1144
 
1145
        while ((inb(ioaddr + ChipCmd) & 1) == 0) {
1146
                int ring_offset = cur_rx % RX_BUF_LEN;
1147
                u32 rx_status = *(u32*)(rx_ring + ring_offset);
1148
                int rx_size = rx_status >> 16;
1149
 
1150
                if (rtl8129_debug > 4) {
1151
                        int i;
1152
                        printk(KERN_DEBUG"%s:  rtl8129_rx() status %4.4x, size %4.4x, cur %4.4x.\n",
1153
                                   dev->name, rx_status, rx_size, cur_rx);
1154
                        printk(KERN_DEBUG"%s: Frame contents ", dev->name);
1155
                        for (i = 0; i < 70; i++)
1156
                                printk(" %2.2x", rx_ring[ring_offset + i]);
1157
                        printk(".\n");
1158
                }
1159
                if (rx_status & RxTooLong) {
1160
                        if (rtl8129_debug > 0)
1161
                                printk(KERN_NOTICE"%s: Oversized Ethernet frame, status %4.4x!\n",
1162
                                           dev->name, rx_status);
1163
                        tp->stats.rx_length_errors++;
1164
                } else if (rx_status &
1165
                                   (RxBadSymbol|RxRunt|RxTooLong|RxCRCErr|RxBadAlign)) {
1166
                        if (rtl8129_debug > 1)
1167
                                printk(KERN_DEBUG"%s: Ethernet frame had errors,"
1168
                                           " status %4.4x.\n", dev->name, rx_status);
1169
                        tp->stats.rx_errors++;
1170
                        if (rx_status & (RxBadSymbol|RxBadAlign))
1171
                                tp->stats.rx_frame_errors++;
1172
                        if (rx_status & (RxRunt|RxTooLong)) tp->stats.rx_length_errors++;
1173
                        if (rx_status & RxCRCErr) tp->stats.rx_crc_errors++;
1174
                        /* Reset the receiver, based on RealTek recommendation. (Bug?) */
1175
                        tp->cur_rx = 0;
1176
                        outb(CmdTxEnb, ioaddr + ChipCmd);
1177
                        outb(CmdRxEnb | CmdTxEnb, ioaddr + ChipCmd);
1178
                        outl((RX_FIFO_THRESH << 13) | (RX_BUF_LEN_IDX << 11) |
1179
                                 (RX_DMA_BURST<<8), ioaddr + RxConfig);
1180
                } else {
1181
                        /* Malloc up new buffer, compatible with net-2e. */
1182
                        /* Omit the four octet CRC from the length. */
1183
                        struct sk_buff *skb;
1184
 
1185
                        skb = dev_alloc_skb(rx_size + 2);
1186
                        if (skb == NULL) {
1187
                                printk(KERN_WARNING"%s: Memory squeeze, deferring packet.\n",
1188
                                           dev->name);
1189
                                /* We should check that some rx space is free.
1190
                                   If not, free one and mark stats->rx_dropped++. */
1191
                                tp->stats.rx_dropped++;
1192
                                break;
1193
                        }
1194
                        skb->dev = dev;
1195
                        skb_reserve(skb, 2);    /* 16 byte align the IP fields. */
1196
                        if (ring_offset+rx_size+4 > RX_BUF_LEN) {
1197
                                int semi_count = RX_BUF_LEN - ring_offset - 4;
1198
                                memcpy(skb_put(skb, semi_count), &rx_ring[ring_offset + 4],
1199
                                           semi_count);
1200
                                memcpy(skb_put(skb, rx_size-semi_count), rx_ring,
1201
                                           rx_size-semi_count);
1202
                                if (rtl8129_debug > 4) {
1203
                                        int i;
1204
                                        printk(KERN_DEBUG"%s:  Frame wrap @%d",
1205
                                                   dev->name, semi_count);
1206
                                        for (i = 0; i < 16; i++)
1207
                                                printk(" %2.2x", rx_ring[i]);
1208
                                        printk(".\n");
1209
                                        memset(rx_ring, 0xcc, 16);
1210
                                }
1211
                        } else {
1212
#if 1  /* USE_IP_COPYSUM */
1213
                                eth_copy_and_sum(skb, &rx_ring[ring_offset + 4],
1214
                                                                 rx_size, 0);
1215
                                skb_put(skb, rx_size);
1216
#else
1217
                                memcpy(skb_put(skb, rx_size), &rx_ring[ring_offset + 4],
1218
                                           rx_size);
1219
#endif
1220
                        }
1221
                        skb->protocol = eth_type_trans(skb, dev);
1222
                        netif_rx(skb);
1223
#if LINUX_VERSION_CODE > 0x20119
1224
                        tp->stats.rx_bytes += rx_size;
1225
#endif
1226
                        tp->stats.rx_packets++;
1227
                }
1228
 
1229
                cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
1230
                outw(cur_rx - 16, ioaddr + RxBufPtr);
1231
        }
1232
        if (rtl8129_debug > 4)
1233
                printk(KERN_DEBUG"%s: Done rtl8129_rx(), current %4.4x BufAddr %4.4x,"
1234
                           " free to %4.4x, Cmd %2.2x.\n",
1235
                           dev->name, cur_rx, inw(ioaddr + RxBufAddr),
1236
                           inw(ioaddr + RxBufPtr), inb(ioaddr + ChipCmd));
1237
        tp->cur_rx = cur_rx;
1238
        return 0;
1239
}
1240
 
1241
static int
1242
rtl8129_close(struct device *dev)
1243
{
1244
        long ioaddr = dev->base_addr;
1245
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
1246
        int i;
1247
 
1248
        dev->start = 0;
1249
        dev->tbusy = 1;
1250
 
1251
        if (rtl8129_debug > 1)
1252
                printk(KERN_DEBUG"%s: Shutting down ethercard, status was 0x%4.4x.\n",
1253
                           dev->name, inw(ioaddr + IntrStatus));
1254
 
1255
        /* Disable interrupts by clearing the interrupt mask. */
1256
        outw(0x0000, ioaddr + IntrMask);
1257
 
1258
        /* Stop the chip's Tx and Rx DMA processes. */
1259
        outb(0x00, ioaddr + ChipCmd);
1260
 
1261
        /* Update the error counts. */
1262
        tp->stats.rx_missed_errors += inl(ioaddr + RxMissed);
1263
        outl(0, ioaddr + RxMissed);
1264
 
1265
        del_timer(&tp->timer);
1266
 
1267
        free_irq(dev->irq, dev);
1268
 
1269
        for (i = 0; i < NUM_TX_DESC; i++) {
1270
                if (tp->tx_skbuff[i])
1271
                        dev_free_skb(tp->tx_skbuff[i]);
1272
                tp->tx_skbuff[i] = 0;
1273
        }
1274
        kfree(tp->rx_ring);
1275
        kfree(tp->tx_bufs);
1276
 
1277
        /* Green! Put the chip in low-power mode. */
1278
        outb(0xC0, ioaddr + Cfg9346);
1279
        outb(0x03, ioaddr + Config1);
1280
        outb('H', ioaddr + HltClk);             /* 'R' would leave the clock running. */
1281
 
1282
        MOD_DEC_USE_COUNT;
1283
 
1284
        return 0;
1285
}
1286
 
1287
static int mii_ioctl(struct device *dev, struct ifreq *rq, int cmd)
1288
{
1289
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
1290
        u16 *data = (u16 *)&rq->ifr_data;
1291
 
1292
        switch(cmd) {
1293
        case SIOCDEVPRIVATE:            /* Get the address of the PHY in use. */
1294
                data[0] = tp->phys[0] & 0x3f;
1295
                /* Fall Through */
1296
        case SIOCDEVPRIVATE+1:          /* Read the specified MII register. */
1297
                data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f);
1298
                return 0;
1299
        case SIOCDEVPRIVATE+2:          /* Write the specified MII register */
1300
                if (!suser())
1301
                        return -EPERM;
1302
                mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
1303
                return 0;
1304
        default:
1305
                return -EOPNOTSUPP;
1306
        }
1307
}
1308
 
1309
static struct enet_statistics *
1310
rtl8129_get_stats(struct device *dev)
1311
{
1312
        struct rtl8129_private *tp = (struct rtl8129_private *)dev->priv;
1313
        long ioaddr = dev->base_addr;
1314
 
1315
        if (dev->start) {
1316
                tp->stats.rx_missed_errors += inl(ioaddr + RxMissed);
1317
                outl(0, ioaddr + RxMissed);
1318
        }
1319
 
1320
        return &tp->stats;
1321
}
1322
 
1323
/* Set or clear the multicast filter for this adaptor.
1324
   This routine is not state sensitive and need not be SMP locked. */
1325
 
1326
static unsigned const ethernet_polynomial = 0x04c11db7U;
1327
static inline u32 ether_crc(int length, unsigned char *data)
1328
{
1329
    int crc = -1;
1330
 
1331
    while(--length >= 0) {
1332
                unsigned char current_octet = *data++;
1333
                int bit;
1334
                for (bit = 0; bit < 8; bit++, current_octet >>= 1)
1335
                        crc = (crc << 1) ^
1336
                                ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
1337
    }
1338
    return crc;
1339
}
1340
 
1341
/* Bits in RxConfig. */
1342
enum rx_mode_bits {
1343
        AcceptErr=0x20, AcceptRunt=0x10, AcceptBroadcast=0x08,
1344
        AcceptMulticast=0x04, AcceptMyPhys=0x02, AcceptAllPhys=0x01,
1345
};
1346
 
1347
static void set_rx_mode(struct device *dev)
1348
{
1349
        long ioaddr = dev->base_addr;
1350
        u32 mc_filter[2];                /* Multicast hash filter */
1351
        int i, rx_mode;
1352
 
1353
        if (rtl8129_debug > 3)
1354
                printk(KERN_DEBUG"%s:   set_rx_mode(%4.4x) done -- Rx config %8.8x.\n",
1355
                           dev->name, dev->flags, inl(ioaddr + RxConfig));
1356
 
1357
        /* Note: do not reorder, GCC is clever about common statements. */
1358
        if (dev->flags & IFF_PROMISC) {
1359
                /* Unconditionally log net taps. */
1360
                printk(KERN_NOTICE"%s: Promiscuous mode enabled.\n", dev->name);
1361
                rx_mode = AcceptBroadcast|AcceptMulticast|AcceptMyPhys|AcceptAllPhys;
1362
                mc_filter[1] = mc_filter[0] = 0xffffffff;
1363
        } else if ((dev->mc_count > multicast_filter_limit)
1364
                           ||  (dev->flags & IFF_ALLMULTI)) {
1365
                /* Too many to filter perfectly -- accept all multicasts. */
1366
                rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1367
                mc_filter[1] = mc_filter[0] = 0xffffffff;
1368
        } else {
1369
                struct dev_mc_list *mclist;
1370
                rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1371
                mc_filter[1] = mc_filter[0] = 0;
1372
                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1373
                         i++, mclist = mclist->next)
1374
                        set_bit(ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26, mc_filter);
1375
        }
1376
        /* We can safely update without stopping the chip. */
1377
        outb(rx_mode, ioaddr + RxConfig);
1378
        outl(mc_filter[0], ioaddr + MAR0 + 0);
1379
        outl(mc_filter[1], ioaddr + MAR0 + 4);
1380
        return;
1381
}
1382
 
1383
#ifdef MODULE
1384
int init_module(void)
1385
{
1386
        return rtl8139_probe(0);
1387
}
1388
 
1389
void
1390
cleanup_module(void)
1391
{
1392
        struct device *next_dev;
1393
 
1394
        /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1395
        while (root_rtl8129_dev) {
1396
                struct rtl8129_private *tp =
1397
                        (struct rtl8129_private *)root_rtl8129_dev->priv;
1398
                next_dev = tp->next_module;
1399
                unregister_netdev(root_rtl8129_dev);
1400
                release_region(root_rtl8129_dev->base_addr,
1401
                                           pci_tbl[tp->chip_id].io_size);
1402
                kfree(tp);
1403
                kfree(root_rtl8129_dev);
1404
                root_rtl8129_dev = next_dev;
1405
        }
1406
}
1407
 
1408
#endif  /* MODULE */
1409
 
1410
/*
1411
 * Local variables:
1412
 *  compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c rtl8139.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
1413
 *  SMP-compile-command: "gcc -D__SMP__ -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c rtl8139.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
1414
 *  c-indent-level: 4
1415
 *  c-basic-offset: 4
1416
 *  tab-width: 4
1417
 * End:
1418
 */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.