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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [arch/] [armnommu/] [drivers/] [net/] [ether3.c] - Blame information for rev 1775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * linux/drivers/net/ether3.c
3
 *
4
 * SEEQ nq8005 ethernet driver for Acorn/ANT Ether3 card
5
 *  for Acorn machines
6
 *
7
 * By Russell King, with some suggestions from borris@ant.co.uk
8
 *
9
 * Changelog:
10
 * 1.04 RMK     29/02/1996      Won't pass packets that are from our ethernet
11
 *                              address up to the higher levels - they're
12
 *                              silently ignored.  I/F can now be put into
13
 *                              multicast mode.  Receiver routine optimised.
14
 * 1.05 RMK     30/02/1996      Now claims interrupt at open when part of
15
 *                              the kernel rather than when a module.
16
 * 1.06 RMK     02/03/1996      Various code cleanups
17
 * 1.07 RMK     13/10/1996      Optimised interrupt routine and transmit
18
 *                              routines.
19
 * 1.08 RMK     14/10/1996      Fixed problem with too many packets,
20
 *                              prevented the kernel message about dropped
21
 *                              packets appearing too many times a second.
22
 *                              Now does not disable all IRQs, only the IRQ
23
 *                              used by this card.
24
 * 1.09 RMK     10/11/1996      Only enables TX irq when buffer space is low,
25
 *                              but we still service the TX queue if we get a
26
 *                              RX interrupt.
27
 * 1.10 RMK     15/07/1997      Fixed autoprobing of NQ8004.
28
 * 1.11 RMK     16/11/1997      Fixed autoprobing of NQ8005A.
29
 * 1.13 RMK     29/06/1998      Fixed problem with transmission of packets.
30
 *                              Chip seems to have a bug in, whereby if the
31
 *                              packet starts two bytes from the end of the
32
 *                              buffer, it corrupts the receiver chain, and
33
 *                              never updates the transmit status correctly.
34
 * 1.14 RMK     07/01/1998      Added initial code for ETHERB addressing.
35
 *
36
 * TODO:
37
 *  When we detect a fatal error on the interface, we should restart it.
38
 */
39
 
40
static char *version = "ether3 ethernet driver (c) 1995-1999 R.M.King v1.14\n";
41
 
42
#include <linux/module.h>
43
#include <linux/kernel.h>
44
#include <linux/sched.h>
45
#include <linux/types.h>
46
#include <linux/fcntl.h>
47
#include <linux/interrupt.h>
48
#include <linux/ptrace.h>
49
#include <linux/ioport.h>
50
#include <linux/in.h>
51
#include <linux/malloc.h>
52
#include <linux/string.h>
53
#include <linux/errno.h>
54
#include <linux/netdevice.h>
55
#include <linux/etherdevice.h>
56
#include <linux/skbuff.h>
57
 
58
#include <asm/system.h>
59
#include <asm/bitops.h>
60
#include <asm/ecard.h>
61
#include <asm/delay.h>
62
#include <asm/io.h>
63
#include <asm/irq.h>
64
 
65
#include "ether3.h"
66
 
67
static unsigned int net_debug = NET_DEBUG;
68
static const card_ids ether3_cids[] = {
69
        { MANU_ANT2, PROD_ANT_ETHER3 },
70
        { MANU_ANT,  PROD_ANT_ETHER3 },
71
        { MANU_ANT,  PROD_ANT_ETHERB }, /* trial - will etherb work? */
72
        { 0xffff, 0xffff }
73
};
74
 
75
static void ether3_setmulticastlist(struct device *dev);
76
static int  ether3_rx(struct device *dev, struct dev_priv *priv, unsigned int maxcnt);
77
static void ether3_tx(struct device *dev, struct dev_priv *priv);
78
 
79
extern int inswb(int reg, void *buffer, int len);
80
extern int outswb(int reg, void *buffer, int len);
81
 
82
#define BUS_16          2
83
#define BUS_8           1
84
#define BUS_UNKNOWN     0
85
 
86
/*
87
 * I'm not sure what address we should default to if the internal one
88
 * is corrupted...
89
 */
90
unsigned char def_eth_addr[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
91
 
92
/* --------------------------------------------------------------------------- */
93
 
94
typedef enum {
95
        buffer_write,
96
        buffer_read
97
} buffer_rw_t;
98
 
99
/*
100
 * ether3 read/write.  Slow things down a bit...
101
 */
102
#define ether3_outb(v,r)        { outb((v),(r)); udelay(1); }
103
#define ether3_outw(v,r)        { outw((v),(r)); udelay(1); }
104
 
105
#define ether3_inb(r)           ({ unsigned int __v = inb((r)); udelay(1); __v; })
106
#define ether3_inw(r)           ({ unsigned int __v = inw((r)); udelay(1); __v; })
107
 
108
static int
109
ether3_setbuffer(struct device *dev, buffer_rw_t read, int start)
110
{
111
        struct dev_priv *priv = (struct dev_priv *)dev->priv;
112
        int timeout = 1000;
113
 
114
        ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
115
        ether3_outw(priv->regs.command | CMD_FIFOWRITE, REG_COMMAND);
116
 
117
        while ((ether3_inw(REG_STATUS) & STAT_FIFOEMPTY) == 0) {
118
                if (!timeout--) {
119
                        printk("%s: setbuffer broken\n", dev->name);
120
                        priv->broken = 1;
121
                        return 1;
122
                }
123
                udelay(1);
124
        }
125
 
126
        if (read == buffer_read) {
127
                ether3_outw(start, REG_DMAADDR);
128
                ether3_outw(priv->regs.command | CMD_FIFOREAD, REG_COMMAND);
129
        } else {
130
                ether3_outw(priv->regs.command | CMD_FIFOWRITE, REG_COMMAND);
131
                ether3_outw(start, REG_DMAADDR);
132
        }
133
        return 0;
134
}
135
 
136
/*
137
 * write data to the buffer memory
138
 */
139
#define ether3_writebuffer(dev,data,length)                     \
140
        outswb(REG_BUFWIN, (data), (length))
141
 
142
#define ether3_writeword(dev,data)                              \
143
        outw((data), REG_BUFWIN)
144
 
145
#define ether3_writelong(dev,data)      {                       \
146
        unsigned long reg_bufwin = REG_BUFWIN;                  \
147
        outw((data), reg_bufwin);                               \
148
        outw((data) >> 16, reg_bufwin);                         \
149
}
150
 
151
/*
152
 * read data from the buffer memory
153
 */
154
#define ether3_readbuffer(dev,data,length)                      \
155
        inswb(REG_BUFWIN, (data), (length))
156
 
157
#define ether3_readword(dev)                                    \
158
        inw(REG_BUFWIN)
159
 
160
#define ether3_readlong(dev)                                    \
161
        inw(REG_BUFWIN) | (inw(REG_BUFWIN) << 16)
162
 
163
/*
164
 * Switch LED off...
165
 */
166
static void
167
ether3_ledoff(unsigned long data)
168
{
169
        struct device *dev = (struct device *)data;
170
        struct dev_priv *priv = (struct dev_priv *)dev->priv;
171
        ether3_outw(priv->regs.config2 |= CFG2_CTRLO, REG_CONFIG2);
172
}
173
 
174
/*
175
 * switch LED on...
176
 */
177
static inline void
178
ether3_ledon(struct device *dev, struct dev_priv *priv)
179
{
180
        del_timer(&priv->timer);
181
        priv->timer.expires = jiffies + HZ / 50; /* leave on for 1/50th second */
182
        priv->timer.data = (unsigned long)dev;
183
        priv->timer.function = ether3_ledoff;
184
        add_timer(&priv->timer);
185
        if (priv->regs.config2 & CFG2_CTRLO)
186
                ether3_outw(priv->regs.config2 &= ~CFG2_CTRLO, REG_CONFIG2);
187
}
188
 
189
/*
190
 * Read the ethernet address string from the on board rom.
191
 * This is an ascii string!!!
192
 */
193
static void
194
ether3_addr(char *addr, struct expansion_card *ec)
195
{
196
        struct in_chunk_dir cd;
197
        char *s;
198
 
199
        if (ecard_readchunk(&cd, ec, 0xf5, 0) && (s = strchr(cd.d.string, '('))) {
200
                int i;
201
                for (i = 0; i<6; i++) {
202
                        addr[i] = simple_strtoul(s + 1, &s, 0x10);
203
                        if (*s != (i==5?')' : ':' ))
204
                                break;
205
                }
206
                if (i == 6)
207
                        return;
208
        }
209
        /* I wonder if we should even let the user continue in this case
210
         *   - no, it would be better to disable the device
211
         */
212
        printk(KERN_ERR "ether3: Couldn't read a valid MAC address from card.\n");
213
        memcpy(addr, def_eth_addr, 6);
214
}
215
 
216
/* --------------------------------------------------------------------------- */
217
 
218
static int
219
ether3_ramtest(struct device *dev, unsigned char byte)
220
{
221
        unsigned char *buffer = kmalloc(RX_END, GFP_KERNEL);
222
        int i,ret = 0;
223
        int max_errors = 4;
224
        int bad = -1;
225
 
226
        if (!buffer)
227
                return 1;
228
 
229
        memset(buffer, byte, RX_END);
230
        ether3_setbuffer(dev, buffer_write, 0);
231
        ether3_writebuffer(dev, buffer, TX_END);
232
        ether3_setbuffer(dev, buffer_write, RX_START);
233
        ether3_writebuffer(dev, buffer + RX_START, RX_LEN);
234
        memset(buffer, byte ^ 0xff, RX_END);
235
        ether3_setbuffer(dev, buffer_read, 0);
236
        ether3_readbuffer(dev, buffer, TX_END);
237
        ether3_setbuffer(dev, buffer_read, RX_START);
238
        ether3_readbuffer(dev, buffer + RX_START, RX_LEN);
239
 
240
        for (i = 0; i < RX_END; i++) {
241
                if (buffer[i] != byte) {
242
                        if (max_errors > 0 && bad != buffer[i]) {
243
                                printk("%s: RAM failed with (%02X instead of %02X) at 0x%04X",
244
                                       dev->name, buffer[i], byte, i);
245
                                ret = 2;
246
                                max_errors--;
247
                                bad = i;
248
                        }
249
                } else {
250
                        if (bad != -1) {
251
                                if (bad != i - 1)
252
                                        printk(" - 0x%04X\n", i - 1);
253
                                printk("\n");
254
                                bad = -1;
255
                        }
256
                }
257
        }
258
        if (bad != -1)
259
                printk(" - 0xffff\n");
260
        kfree(buffer);
261
 
262
        return ret;
263
}
264
 
265
/* ------------------------------------------------------------------------------- */
266
 
267
static int
268
ether3_init_2(struct device *dev)
269
{
270
        struct dev_priv *priv = (struct dev_priv *)dev->priv;
271
        int i;
272
 
273
        priv->regs.config1 = CFG1_RECVCOMPSTAT0|CFG1_DMABURST8;
274
        priv->regs.config2 = CFG2_CTRLO|CFG2_RECVCRC|CFG2_ERRENCRC;
275
        priv->regs.command = 0;
276
 
277
        /*
278
         * Set up our hardware address
279
         */
280
        ether3_outw(priv->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
281
        for (i = 0; i < 6; i++)
282
                ether3_outb(dev->dev_addr[i], REG_BUFWIN);
283
 
284
        if (dev->flags & IFF_PROMISC)
285
                priv->regs.config1 |= CFG1_RECVPROMISC;
286
        else if (dev->flags & IFF_MULTICAST)
287
                priv->regs.config1 |= CFG1_RECVSPECBRMULTI;
288
        else
289
                priv->regs.config1 |= CFG1_RECVSPECBROAD;
290
 
291
        /*
292
         * There is a problem with the NQ8005 in that it occasionally loses the
293
         * last two bytes.  To get round this problem, we receive the CRC as
294
         * well.  That way, if we do loose the last two, then it doesn't matter.
295
         */
296
        ether3_outw(priv->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
297
        ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
298
        ether3_outw(priv->rx_head, REG_RECVPTR);
299
        ether3_outw(0, REG_TRANSMITPTR);
300
        ether3_outw(priv->rx_head >> 8, REG_RECVEND);
301
        ether3_outw(priv->regs.config2, REG_CONFIG2);
302
        ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
303
        ether3_outw(priv->regs.command, REG_COMMAND);
304
 
305
        i = ether3_ramtest(dev, 0x5A);
306
        if(i)
307
                return i;
308
        i = ether3_ramtest(dev, 0x1E);
309
        if(i)
310
                return i;
311
 
312
        ether3_setbuffer(dev, buffer_write, 0);
313
        ether3_writelong(dev, 0);
314
        return 0;
315
}
316
 
317
static void
318
ether3_init_for_open(struct device *dev)
319
{
320
        struct dev_priv *priv = (struct dev_priv *)dev->priv;
321
        int i;
322
 
323
        memset(&priv->stats, 0, sizeof(struct enet_statistics));
324
 
325
        /* Reset the chip */
326
        ether3_outw(CFG2_RESET, REG_CONFIG2);
327
        udelay(4);
328
 
329
        priv->regs.command = 0;
330
        ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
331
        while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON));
332
 
333
        ether3_outw(priv->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
334
        for (i = 0; i < 6; i++)
335
                ether3_outb(dev->dev_addr[i], REG_BUFWIN);
336
 
337
        priv->tx_head   = 0;
338
        priv->tx_tail   = 0;
339
        priv->regs.config2 |= CFG2_CTRLO;
340
        priv->rx_head   = RX_START;
341
 
342
        ether3_outw(priv->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
343
        ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
344
        ether3_outw(priv->rx_head, REG_RECVPTR);
345
        ether3_outw(priv->rx_head >> 8, REG_RECVEND);
346
        ether3_outw(0, REG_TRANSMITPTR);
347
        ether3_outw(priv->regs.config2, REG_CONFIG2);
348
        ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
349
 
350
        ether3_setbuffer(dev, buffer_write, 0);
351
        ether3_writelong(dev, 0);
352
 
353
        priv->regs.command = CMD_ENINTRX | CMD_ENINTTX;
354
        ether3_outw(priv->regs.command | CMD_RXON, REG_COMMAND);
355
}
356
 
357
static inline int
358
ether3_probe_bus_8(struct device *dev, int val)
359
{
360
        int write_low, write_high, read_low, read_high;
361
 
362
        write_low = val & 255;
363
        write_high = val >> 8;
364
 
365
        printk(KERN_DEBUG "ether3_probe: write8 [%02X:%02X]", write_high, write_low);
366
 
367
        ether3_outb(write_low, REG_RECVPTR);
368
        ether3_outb(write_high, REG_RECVPTR + 1);
369
 
370
        read_low = ether3_inb(REG_RECVPTR);
371
        read_high = ether3_inb(REG_RECVPTR + 1);
372
 
373
        printk(", read8 [%02X:%02X]\n", read_high, read_low);
374
 
375
        return read_low == write_low && read_high == write_high;
376
}
377
 
378
static inline int
379
ether3_probe_bus_16(struct device *dev, int val)
380
{
381
        int read_val;
382
 
383
        ether3_outw(val, REG_RECVPTR);
384
        read_val = ether3_inw(REG_RECVPTR);
385
 
386
        printk(KERN_DEBUG "ether3_probe: write16 [%04X], read16 [%04X]\n", val, read_val);
387
 
388
        return read_val == val;
389
}
390
 
391
/*
392
 * This is the real probe routine.
393
 */
394
static int
395
ether3_probe1(struct device *dev)
396
{
397
        static unsigned version_printed = 0;
398
        struct dev_priv *priv;
399
        unsigned int i, bus_type, error = ENODEV;
400
 
401
        if (net_debug && version_printed++ == 0)
402
                printk(version);
403
 
404
        if (!dev->priv) {
405
                dev->priv = kmalloc(sizeof (struct dev_priv), GFP_KERNEL);
406
                if (!dev->priv) {
407
                        printk(KERN_ERR "ether3_probe1: no memory\n");
408
                        return -ENOMEM;
409
                }
410
        }
411
 
412
        priv = (struct dev_priv *) dev->priv;
413
        memset(priv, 0, sizeof(struct dev_priv));
414
 
415
        request_region(dev->base_addr, 128, "ether3");
416
 
417
        /* Reset card...
418
         */
419
        ether3_outb(0x80, REG_CONFIG2 + 1);
420
        bus_type = BUS_UNKNOWN;
421
        udelay(4);
422
 
423
        /* Test using Receive Pointer (16-bit register) to find out
424
         * how the ether3 is connected to the bus...
425
         */
426
        if (ether3_probe_bus_8(dev, 0x100) &&
427
            ether3_probe_bus_8(dev, 0x201))
428
                bus_type = BUS_8;
429
 
430
        if (bus_type == BUS_UNKNOWN &&
431
            ether3_probe_bus_16(dev, 0x101) &&
432
            ether3_probe_bus_16(dev, 0x201))
433
                bus_type = BUS_16;
434
 
435
        switch (bus_type) {
436
        case BUS_UNKNOWN:
437
                printk(KERN_ERR "%s: unable to identify podule bus width\n", dev->name);
438
                goto failed;
439
 
440
        case BUS_8:
441
                printk(KERN_ERR "%s: ether3 found, but is an unsupported 8-bit card\n", dev->name);
442
                goto failed;
443
 
444
        default:
445
                break;
446
        }
447
 
448
        printk("%s: ether3 found at %lx, IRQ%d, ether address ", dev->name, dev->base_addr, dev->irq);
449
        for (i = 0; i < 6; i++)
450
                printk(i == 5 ? "%2.2x\n" : "%2.2x:", dev->dev_addr[i]);
451
 
452
        if (!ether3_init_2(dev)) {
453
                dev->open = ether3_open;
454
                dev->stop = ether3_close;
455
                dev->hard_start_xmit = ether3_sendpacket;
456
                dev->get_stats = ether3_getstats;
457
                dev->set_multicast_list = ether3_setmulticastlist;
458
 
459
                /* Fill in the fields of the device structure with ethernet values. */
460
                ether_setup(dev);
461
 
462
                return 0;
463
        }
464
 
465
failed:
466
        kfree(dev->priv);
467
        dev->priv = NULL;
468
        release_region(dev->base_addr, 128);
469
        return error;
470
}
471
 
472
static void
473
ether3_get_dev(struct device *dev, struct expansion_card *ec)
474
{
475
        ecard_claim(ec);
476
 
477
        dev->base_addr = ecard_address(ec, ECARD_MEMC, 0);
478
        dev->irq = ec->irq;
479
 
480
        if (ec->cid.manufacturer == MANU_ANT &&
481
            ec->cid.product == PROD_ANT_ETHERB) {
482
                dev->base_addr += 0x200;
483
        }
484
 
485
        ec->irqaddr = ioaddr(dev->base_addr);
486
        ec->irqmask = 0xf0;
487
 
488
        ether3_addr(dev->dev_addr, ec);
489
}
490
 
491
#ifndef MODULE
492
int
493
ether3_probe(struct device *dev)
494
{
495
        struct expansion_card *ec;
496
 
497
        if (!dev)
498
                return ENODEV;
499
 
500
        ecard_startfind();
501
 
502
        if ((ec = ecard_find(0, ether3_cids)) == NULL)
503
                return ENODEV;
504
 
505
        ether3_get_dev(dev, ec);
506
 
507
        return ether3_probe1(dev);
508
}
509
#endif
510
 
511
/*
512
 * Open/initialize the board.  This is called (in the current kernel)
513
 * sometime after booting when the 'ifconfig' program is run.
514
 *
515
 * This routine should set everything up anew at each open, even
516
 * registers that "should" only need to be set once at boot, so that
517
 * there is non-reboot way to recover if something goes wrong.
518
 */
519
static int
520
ether3_open(struct device *dev)
521
{
522
        MOD_INC_USE_COUNT;
523
 
524
        if (request_irq(dev->irq, ether3_interrupt, 0, "ether3", dev)) {
525
                MOD_DEC_USE_COUNT;
526
                return -EAGAIN;
527
        }
528
 
529
        dev->tbusy = 0;
530
        dev->interrupt = 0;
531
        dev->start = 1;
532
 
533
        ether3_init_for_open(dev);
534
 
535
        return 0;
536
}
537
 
538
/*
539
 * The inverse routine to ether3_open().
540
 */
541
static int
542
ether3_close(struct device *dev)
543
{
544
        struct dev_priv *priv = (struct dev_priv *)dev->priv;
545
 
546
        dev->tbusy = 1;
547
        dev->start = 0;
548
 
549
        disable_irq(dev->irq);
550
 
551
        ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
552
        priv->regs.command = 0;
553
        while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON));
554
        ether3_outb(0x80, REG_CONFIG2 + 1);
555
        ether3_outw(0, REG_COMMAND);
556
 
557
        free_irq(dev->irq, dev);
558
 
559
        MOD_DEC_USE_COUNT;
560
        return 0;
561
}
562
 
563
/*
564
 * Get the current statistics.  This may be called with the card open or
565
 * closed.
566
 */
567
static struct enet_statistics *ether3_getstats(struct device *dev)
568
{
569
        struct dev_priv *priv = (struct dev_priv *)dev->priv;
570
        return &priv->stats;
571
}
572
 
573
/*
574
 * Set or clear promiscuous/multicast mode filter for this adaptor.
575
 *
576
 * We don't attempt any packet filtering.  The card may have a SEEQ 8004
577
 * in which does not have the other ethernet address registers present...
578
 */
579
static void ether3_setmulticastlist(struct device *dev)
580
{
581
        struct dev_priv *priv = (struct dev_priv *)dev->priv;
582
 
583
        priv->regs.config1 &= ~CFG1_RECVPROMISC;
584
 
585
        if (dev->flags & IFF_PROMISC) {
586
                /* promiscuous mode */
587
                priv->regs.config1 |= CFG1_RECVPROMISC;
588
        } else if (dev->flags & IFF_ALLMULTI) {
589
                priv->regs.config1 |= CFG1_RECVSPECBRMULTI;
590
        } else
591
                priv->regs.config1 |= CFG1_RECVSPECBROAD;
592
 
593
        ether3_outw(priv->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
594
}
595
 
596
/*
597
 * Transmit a packet
598
 */
599
static int
600
ether3_sendpacket(struct sk_buff *skb, struct device *dev)
601
{
602
        struct dev_priv *priv = (struct dev_priv *)dev->priv;
603
retry:
604
        if (!dev->tbusy) {
605
                if (skb) {
606
                    /* Block a timer-based transmit from overlapping. */
607
                    if (!set_bit(0, (void *)&dev->tbusy)) {
608
                        unsigned long flags;
609
                        unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
610
                        unsigned int ptr, next_ptr;
611
 
612
                        length = (length + 1) & ~1;
613
 
614
                        if (priv->broken) {
615
                                dev_kfree_skb(skb, FREE_WRITE);
616
                                priv->stats.tx_dropped ++;
617
                                dev->tbusy = 0;
618
                                return 0;
619
                        }
620
 
621
                        next_ptr = (priv->tx_head + 1) & 15;
622
 
623
                        save_flags_cli(flags);
624
 
625
                        if (priv->tx_tail == next_ptr) {
626
                                restore_flags(flags);
627
                                return 1;       /* unable to queue */
628
                        }
629
 
630
                        dev->trans_start = jiffies;
631
                        ptr              = 0x600 * priv->tx_head;
632
                        priv->tx_head    = next_ptr;
633
                        next_ptr        *= 0x600;
634
 
635
#define TXHDR_FLAGS (TXHDR_TRANSMIT|TXHDR_CHAINCONTINUE|TXHDR_DATAFOLLOWS|TXHDR_ENSUCCESS)
636
 
637
 
638
                        ether3_setbuffer(dev, buffer_write, next_ptr);
639
                        ether3_writelong(dev, 0);
640
                        ether3_setbuffer(dev, buffer_write, ptr);
641
                        ether3_writelong(dev, 0);
642
                        ether3_writebuffer(dev, skb->data, length);
643
                        ether3_writeword(dev, htons(next_ptr));
644
                        ether3_writeword(dev, TXHDR_CHAINCONTINUE >> 16);
645
                        ether3_setbuffer(dev, buffer_write, ptr);
646
                        ether3_writeword(dev, htons((ptr + length + 4)));
647
                        ether3_writeword(dev, TXHDR_FLAGS >> 16);
648
                        ether3_ledon(dev, priv);
649
 
650
                        if (!(ether3_inw(REG_STATUS) & STAT_TXON)) {
651
                                ether3_outw(ptr, REG_TRANSMITPTR);
652
                                ether3_outw(priv->regs.command | CMD_TXON, REG_COMMAND);
653
                        }
654
 
655
                        next_ptr = (priv->tx_head + 1) & 15;
656
                        if (priv->tx_tail != next_ptr)
657
                                dev->tbusy = 0;
658
 
659
                        restore_flags(flags);
660
 
661
                        dev_kfree_skb(skb, FREE_WRITE);
662
 
663
                        return 0;
664
                    } else {
665
                        printk("%s: transmitter access conflict.\n", dev->name);
666
                        return 1;
667
                    }
668
                } else {
669
                    /* If some higher layer thinks we've missed an tx-done interrupt
670
                     * we are passed NULL. Caution: dev_tint() handles the cli()/sti()
671
                     * itself.
672
                     */
673
                    dev_tint(dev);
674
                    return 0;
675
                }
676
        } else {
677
                /* If we get here, some higher level has decided we are broken.
678
                 * There should really be a "kick me" function call instead.
679
                 */
680
                int tickssofar = jiffies - dev->trans_start;
681
                unsigned long flags;
682
 
683
                if (tickssofar < 5)
684
                        return 1;
685
                del_timer(&priv->timer);
686
 
687
                save_flags_cli(flags);
688
                printk(KERN_ERR "%s: transmit timed out, network cable problem?\n", dev->name);
689
                printk(KERN_ERR "%s: state: { status=%04X cfg1=%04X cfg2=%04X }\n", dev->name,
690
                        ether3_inw(REG_STATUS), ether3_inw(REG_CONFIG1), ether3_inw(REG_CONFIG2));
691
                printk(KERN_ERR "%s: { rpr=%04X rea=%04X tpr=%04X }\n", dev->name,
692
                        ether3_inw(REG_RECVPTR), ether3_inw(REG_RECVEND), ether3_inw(REG_TRANSMITPTR));
693
                printk(KERN_ERR "%s: tx head=%X tx tail=%X\n", dev->name,
694
                        priv->tx_head, priv->tx_tail);
695
                ether3_setbuffer(dev, buffer_read, priv->tx_tail);
696
                printk(KERN_ERR "%s: packet status = %08X\n", dev->name, ether3_readlong(dev));
697
                restore_flags(flags);
698
 
699
                dev->tbusy = 0;
700
                priv->regs.config2 |= CFG2_CTRLO;
701
                priv->stats.tx_errors += 1;
702
                ether3_outw(priv->regs.config2, REG_CONFIG2);
703
                dev->trans_start = jiffies;
704
                priv->tx_head = priv->tx_tail = 0;
705
                goto retry;
706
        }
707
}
708
 
709
static void
710
ether3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
711
{
712
        struct device *dev = (struct device *)dev_id;
713
        struct dev_priv *priv;
714
        unsigned int status;
715
 
716
#if NET_DEBUG > 1
717
        if(net_debug & DEBUG_INT)
718
                printk("eth3irq: %d ", irq);
719
#endif
720
 
721
        priv = (struct dev_priv *)dev->priv;
722
 
723
        dev->interrupt = 1;
724
 
725
        status = ether3_inw(REG_STATUS);
726
 
727
        if (status & STAT_INTRX) {
728
                ether3_outw(CMD_ACKINTRX | priv->regs.command, REG_COMMAND);
729
                ether3_rx(dev, priv, 12);
730
        }
731
 
732
        if (status & STAT_INTTX) {
733
                ether3_outw(CMD_ACKINTTX | priv->regs.command, REG_COMMAND);
734
                ether3_tx(dev, priv);
735
        }
736
 
737
        dev->interrupt = 0;
738
 
739
#if NET_DEBUG > 1
740
        if(net_debug & DEBUG_INT)
741
                printk("done\n");
742
#endif
743
}
744
 
745
/*
746
 * If we have a good packet(s), get it/them out of the buffers.
747
 */
748
static int
749
ether3_rx(struct device *dev, struct dev_priv *priv, unsigned int maxcnt)
750
{
751
        unsigned int next_ptr = priv->rx_head, received = 0;
752
        ether3_ledon(dev, priv);
753
 
754
        do {
755
                unsigned int this_ptr, status;
756
                unsigned char addrs[16];
757
 
758
                /*
759
                 * read the first 16 bytes from the buffer.
760
                 * This contains the status bytes etc and ethernet addresses,
761
                 * and we also check the source ethernet address to see if
762
                 * it originated from us.
763
                 */
764
                {
765
                        unsigned int temp_ptr;
766
                        ether3_setbuffer(dev, buffer_read, next_ptr);
767
                        temp_ptr = ether3_readword(dev);
768
                        status = ether3_readword(dev);
769
                        if ((status & (RXSTAT_DONE | RXHDR_CHAINCONTINUE | RXHDR_RECEIVE)) !=
770
                                (RXSTAT_DONE | RXHDR_CHAINCONTINUE) || !temp_ptr)
771
                                break;
772
 
773
                        this_ptr = next_ptr + 4;
774
                        next_ptr = ntohs(temp_ptr);
775
                }
776
                ether3_setbuffer(dev, buffer_read, this_ptr);
777
                ether3_readbuffer(dev, addrs+2, 12);
778
 
779
if (next_ptr < RX_START || next_ptr >= RX_END) {
780
 int i;
781
 printk("%s: bad next pointer @%04X: ", dev->name, priv->rx_head);
782
 printk("%02X %02X %02X %02X ", next_ptr >> 8, next_ptr & 255, status & 255, status >> 8);
783
 for (i = 2; i < 14; i++)
784
   printk("%02X ", addrs[i]);
785
 printk("\n");
786
 next_ptr = priv->rx_head;
787
 break;
788
}
789
                /*
790
                 * ignore our own packets...
791
                 */
792
                if (!(*(unsigned long *)&dev->dev_addr[0] ^ *(unsigned long *)&addrs[2+6]) &&
793
                    !(*(unsigned short *)&dev->dev_addr[4] ^ *(unsigned short *)&addrs[2+10])) {
794
                        maxcnt ++; /* compensate for loopedback packet */
795
                        ether3_outw(next_ptr >> 8, REG_RECVEND);
796
                } else
797
                if (!(status & (RXSTAT_OVERSIZE|RXSTAT_CRCERROR|RXSTAT_DRIBBLEERROR|RXSTAT_SHORTPACKET))) {
798
                        unsigned int length = next_ptr - this_ptr;
799
                        struct sk_buff *skb;
800
 
801
                        if (next_ptr <= this_ptr)
802
                                length += RX_END - RX_START;
803
 
804
                        skb = dev_alloc_skb(length + 2);
805
                        if (skb) {
806
                                unsigned char *buf;
807
 
808
                                skb->dev = dev;
809
                                skb_reserve(skb, 2);
810
                                buf = skb_put(skb, length);
811
                                ether3_readbuffer(dev, buf + 12, length - 12);
812
                                ether3_outw(next_ptr >> 8, REG_RECVEND);
813
                                *(unsigned short *)(buf + 0)     = *(unsigned short *)(addrs + 2);
814
                                *(unsigned long *)(buf + 2)     = *(unsigned long *)(addrs + 4);
815
                                *(unsigned long *)(buf + 6)     = *(unsigned long *)(addrs + 8);
816
                                *(unsigned short *)(buf + 10)   = *(unsigned short *)(addrs + 12);
817
                                skb->protocol = eth_type_trans(skb, dev);
818
                                netif_rx(skb);
819
                                received ++;
820
                        } else
821
                                goto dropping;
822
                } else {
823
                        struct enet_statistics *stats = &priv->stats;
824
                        ether3_outw(next_ptr >> 8, REG_RECVEND);
825
                        if (status & RXSTAT_OVERSIZE)     stats->rx_over_errors ++;
826
                        if (status & RXSTAT_CRCERROR)     stats->rx_crc_errors ++;
827
                        if (status & RXSTAT_DRIBBLEERROR) stats->rx_fifo_errors ++;
828
                        if (status & RXSTAT_SHORTPACKET)  stats->rx_length_errors ++;
829
                        stats->rx_errors++;
830
                }
831
        }
832
        while (-- maxcnt);
833
 
834
done:
835
        priv->stats.rx_packets += received;
836
        priv->rx_head = next_ptr;
837
        /*
838
         * If rx went off line, then that means that the buffer may be full.  We
839
         * have dropped at least one packet.
840
         */
841
        if (!(ether3_inw(REG_STATUS) & STAT_RXON)) {
842
                priv->stats.rx_dropped ++;
843
                ether3_outw(next_ptr, REG_RECVPTR);
844
                ether3_outw(priv->regs.command | CMD_RXON, REG_COMMAND);
845
        }
846
 
847
        return maxcnt;
848
 
849
dropping:{
850
        static unsigned long last_warned;
851
 
852
        ether3_outw(next_ptr >> 8, REG_RECVEND);
853
        /*
854
         * Don't print this message too many times...
855
         */
856
        if (jiffies - last_warned > 30 * HZ) {
857
                last_warned = jiffies;
858
                printk("%s: memory squeeze, dropping packet.\n", dev->name);
859
        }
860
        priv->stats.rx_dropped ++;
861
        goto done;
862
        }
863
}
864
 
865
/*
866
 * Update stats for the transmitted packet(s)
867
 */
868
static void
869
ether3_tx(struct device *dev, struct dev_priv *priv)
870
{
871
        unsigned int tx_tail = priv->tx_tail;
872
        int max_work = 14;
873
 
874
        do {
875
                unsigned long status;
876
 
877
                /*
878
                 * Read the packet header
879
                 */
880
                ether3_setbuffer(dev, buffer_read, tx_tail * 0x600);
881
                status = ether3_readlong(dev);
882
//printk("%s: processing tx packet @ %04X (status=%08lX)\n", dev->name, tx_tail, status);
883
 
884
                /*
885
                 * Check to see if this packet has been transmitted
886
                 */
887
                if ((status & (TXSTAT_DONE | TXHDR_TRANSMIT)) !=
888
                    (TXSTAT_DONE | TXHDR_TRANSMIT))
889
                        break;
890
 
891
                /*
892
                 * Update errors
893
                 */
894
                if (!(status & (TXSTAT_BABBLED | TXSTAT_16COLLISIONS)))
895
                        priv->stats.tx_packets++;
896
                else {
897
                        priv->stats.tx_errors ++;
898
                        if (status & TXSTAT_16COLLISIONS) priv->stats.collisions += 16;
899
                        if (status & TXSTAT_BABBLED) priv->stats.tx_fifo_errors ++;
900
                }
901
 
902
                tx_tail = (tx_tail + 1) & 15;
903
        } while (--max_work);
904
 
905
        if (priv->tx_tail != tx_tail) {
906
                priv->tx_tail = tx_tail;
907
                dev->tbusy = 0;
908
                mark_bh(NET_BH);        /* Inform upper layers. */
909
        }
910
}
911
 
912
#ifdef MODULE
913
 
914
char ethernames[MAX_ECARDS][9];
915
 
916
static struct device *my_ethers[MAX_ECARDS];
917
static struct expansion_card *ec[MAX_ECARDS];
918
 
919
int
920
init_module(void)
921
{
922
        int i;
923
 
924
        for(i = 0; i < MAX_ECARDS; i++) {
925
                my_ethers[i] = NULL;
926
                ec[i] = NULL;
927
                strcpy(ethernames[i], "        ");
928
        }
929
 
930
        i = 0;
931
 
932
        ecard_startfind();
933
 
934
        do {
935
                if ((ec[i] = ecard_find(0, ether3_cids)) == NULL)
936
                        break;
937
 
938
                my_ethers[i] = (struct device *)kmalloc(sizeof(struct device), GFP_KERNEL);
939
                memset(my_ethers[i], 0, sizeof(struct device));
940
 
941
                my_ethers[i]->init = ether3_probe1;
942
                my_ethers[i]->name = ethernames[i];
943
 
944
                ether3_get_dev(my_ethers[i], ec[i]);
945
 
946
                if(register_netdev(my_ethers[i]) != 0) {
947
                        for (i = 0; i < MAX_ECARDS; i++) {
948
                                if(my_ethers[i]) {
949
                                        kfree(my_ethers[i]);
950
                                        my_ethers[i] = NULL;
951
                                }
952
                                if(ec[i]) {
953
                                        ecard_release(ec[i]);
954
                                        ec[i] = NULL;
955
                                }
956
                        }
957
                        return -EIO;
958
                }
959
                i++;
960
        }
961
        while(i < MAX_ECARDS);
962
 
963
        return i != 0 ? 0 : -ENODEV;
964
}
965
 
966
void
967
cleanup_module(void)
968
{
969
        int i;
970
        for (i = 0; i < MAX_ECARDS; i++) {
971
                if (my_ethers[i]) {
972
                        release_region(my_ethers[i]->base_addr, 128);
973
                        unregister_netdev(my_ethers[i]);
974
                        my_ethers[i] = NULL;
975
                }
976
                if (ec[i]) {
977
                        ecard_release(ec[i]);
978
                        ec[i] = NULL;
979
                }
980
        }
981
}
982
#endif /* MODULE */

powered by: WebSVN 2.1.0

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