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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [net/] [8390.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1622 jcastillo
/* 8390.c: A general NS8390 ethernet driver core for linux. */
2
/*
3
        Written 1992-94 by Donald Becker.
4
 
5
        Copyright 1993 United States Government as represented by the
6
        Director, National Security Agency.
7
 
8
        This software may be used and distributed according to the terms
9
        of the GNU Public License, incorporated herein by reference.
10
 
11
        The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
12
        Center of Excellence in Space Data and Information Sciences
13
           Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
14
 
15
  This is the chip-specific code for many 8390-based ethernet adaptors.
16
  This is not a complete driver, it must be combined with board-specific
17
  code such as ne.c, wd.c, 3c503.c, etc.
18
 
19
  Seeing how at least eight drivers use this code, (not counting the
20
  PCMCIA ones either) it is easy to break some card by what seems like
21
  a simple innocent change. Please contact me or Donald if you think
22
  you have found something that needs changing. -- PG
23
 
24
 
25
  Changelog:
26
 
27
  Paul Gortmaker        : remove set_bit lock, other cleanups.
28
  Paul Gortmaker        : add ei_get_8390_hdr() so we can pass skb's to
29
                          ei_block_input() for eth_io_copy_and_sum().
30
  Paul Gortmaker        : exchange static int ei_pingpong for a #define,
31
                          also add better Tx error handling.
32
  Paul Gortmaker        : rewrite Rx overrun handling as per NS specs.
33
 
34
 
35
  Sources:
36
  The National Semiconductor LAN Databook, and the 3Com 3c503 databook.
37
 
38
  */
39
 
40
static const char *version =
41
    "8390.c:v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
42
 
43
#include <linux/module.h>
44
#include <linux/kernel.h>
45
#include <linux/sched.h>
46
#include <linux/fs.h>
47
#include <linux/types.h>
48
#include <linux/ptrace.h>
49
#include <linux/string.h>
50
#include <asm/system.h>
51
#include <asm/segment.h>
52
#include <asm/bitops.h>
53
#include <asm/io.h>
54
#include <linux/errno.h>
55
#include <linux/fcntl.h>
56
#include <linux/in.h>
57
#include <linux/interrupt.h>
58
 
59
#include <linux/netdevice.h>
60
#include <linux/etherdevice.h>
61
 
62
#include "8390.h"
63
 
64
/* These are the operational function interfaces to board-specific
65
   routines.
66
        void reset_8390(struct device *dev)
67
                Resets the board associated with DEV, including a hardware reset of
68
                the 8390.  This is only called when there is a transmit timeout, and
69
                it is always followed by 8390_init().
70
        void block_output(struct device *dev, int count, const unsigned char *buf,
71
                                          int start_page)
72
                Write the COUNT bytes of BUF to the packet buffer at START_PAGE.  The
73
                "page" value uses the 8390's 256-byte pages.
74
        void get_8390_hdr(struct device *dev, struct e8390_hdr *hdr, int ring_page)
75
                Read the 4 byte, page aligned 8390 header. *If* there is a
76
                subsequent read, it will be of the rest of the packet.
77
        void block_input(struct device *dev, int count, struct sk_buff *skb, int ring_offset)
78
                Read COUNT bytes from the packet buffer into the skb data area. Start
79
                reading from RING_OFFSET, the address as the 8390 sees it.  This will always
80
                follow the read of the 8390 header.
81
*/
82
#define ei_reset_8390 (ei_local->reset_8390)
83
#define ei_block_output (ei_local->block_output)
84
#define ei_block_input (ei_local->block_input)
85
#define ei_get_8390_hdr (ei_local->get_8390_hdr)
86
 
87
/* use 0 for production, 1 for verification, >2 for debug */
88
#ifdef EI_DEBUG
89
int ei_debug = EI_DEBUG;
90
#else
91
int ei_debug = 1;
92
#endif
93
 
94
/* Index to functions. */
95
static void ei_tx_intr(struct device *dev);
96
static void ei_tx_err(struct device *dev);
97
static void ei_receive(struct device *dev);
98
static void ei_rx_overrun(struct device *dev);
99
 
100
/* Routines generic to NS8390-based boards. */
101
static void NS8390_trigger_send(struct device *dev, unsigned int length,
102
                                                                int start_page);
103
static void set_multicast_list(struct device *dev);
104
 
105
 
106
/* Open/initialize the board.  This routine goes all-out, setting everything
107
   up anew at each open, even though many of these registers should only
108
   need to be set once at boot.
109
   */
110
int ei_open(struct device *dev)
111
{
112
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
113
 
114
    /* This can't happen unless somebody forgot to call ethdev_init(). */
115
    if (ei_local == NULL) {
116
        printk(KERN_EMERG "%s: ei_open passed a non-existent device!\n", dev->name);
117
        return -ENXIO;
118
    }
119
 
120
    NS8390_init(dev, 1);
121
    dev->start = 1;
122
    ei_local->irqlock = 0;
123
    return 0;
124
}
125
 
126
/* Opposite of above. Only used when "ifconfig <devname> down" is done. */
127
int ei_close(struct device *dev)
128
{
129
    NS8390_init(dev, 0);
130
    dev->start = 0;
131
    return 0;
132
}
133
 
134
static int ei_start_xmit(struct sk_buff *skb, struct device *dev)
135
{
136
    int e8390_base = dev->base_addr;
137
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
138
    int length, send_length, output_page;
139
 
140
/*
141
 *  We normally shouldn't be called if dev->tbusy is set, but the
142
 *  existing code does anyway. If it has been too long since the
143
 *  last Tx, we assume the board has died and kick it.
144
 */
145
 
146
    if (dev->tbusy) {   /* Do timeouts, just like the 8003 driver. */
147
                int txsr = inb(e8390_base+EN0_TSR), isr;
148
                int tickssofar = jiffies - dev->trans_start;
149
                if (tickssofar < TX_TIMEOUT ||  (tickssofar < (TX_TIMEOUT+5) && ! (txsr & ENTSR_PTX))) {
150
                        return 1;
151
                }
152
                isr = inb(e8390_base+EN0_ISR);
153
                if (dev->start == 0) {
154
                        printk("%s: xmit on stopped card\n", dev->name);
155
                        return 1;
156
                }
157
 
158
                /*
159
                 * Note that if the Tx posted a TX_ERR interrupt, then the
160
                 * error will have been handled from the interrupt handler.
161
                 * and not here.
162
                 */
163
 
164
                printk(KERN_DEBUG "%s: Tx timed out, %s TSR=%#2x, ISR=%#2x, t=%d.\n",
165
                   dev->name, (txsr & ENTSR_ABT) ? "excess collisions." :
166
                   (isr) ? "lost interrupt?" : "cable problem?", txsr, isr, tickssofar);
167
 
168
                if (!isr && !ei_local->stat.tx_packets) {
169
                   /* The 8390 probably hasn't gotten on the cable yet. */
170
                   ei_local->interface_num ^= 1;   /* Try a different xcvr.  */
171
                }
172
 
173
                /* Try to restart the card.  Perhaps the user has fixed something. */
174
                ei_reset_8390(dev);
175
                NS8390_init(dev, 1);
176
                dev->trans_start = jiffies;
177
    }
178
 
179
    /* Sending a NULL skb means some higher layer thinks we've missed an
180
       tx-done interrupt. Caution: dev_tint() handles the cli()/sti()
181
       itself. */
182
    if (skb == NULL) {
183
                dev_tint(dev);
184
                return 0;
185
    }
186
 
187
    length = skb->len;
188
    if (skb->len <= 0)
189
                return 0;
190
 
191
    /* Mask interrupts from the ethercard. */
192
    outb_p(0x00, e8390_base + EN0_IMR);
193
    if (dev->interrupt) {
194
        printk("%s: Tx request while isr active.\n",dev->name);
195
        outb_p(ENISR_ALL, e8390_base + EN0_IMR);
196
        return 1;
197
    }
198
    ei_local->irqlock = 1;
199
 
200
    send_length = ETH_ZLEN < length ? length : ETH_ZLEN;
201
 
202
#ifdef EI_PINGPONG
203
 
204
    /*
205
     * We have two Tx slots available for use. Find the first free
206
     * slot, and then perform some sanity checks. With two Tx bufs,
207
     * you get very close to transmitting back-to-back packets. With
208
     * only one Tx buf, the transmitter sits idle while you reload the
209
     * card, leaving a substantial gap between each transmitted packet.
210
     */
211
 
212
    if (ei_local->tx1 == 0) {
213
        output_page = ei_local->tx_start_page;
214
        ei_local->tx1 = send_length;
215
        if (ei_debug  &&  ei_local->tx2 > 0)
216
                printk("%s: idle transmitter tx2=%d, lasttx=%d, txing=%d.\n",
217
                        dev->name, ei_local->tx2, ei_local->lasttx, ei_local->txing);
218
    } else if (ei_local->tx2 == 0) {
219
        output_page = ei_local->tx_start_page + TX_1X_PAGES;
220
        ei_local->tx2 = send_length;
221
        if (ei_debug  &&  ei_local->tx1 > 0)
222
                printk("%s: idle transmitter, tx1=%d, lasttx=%d, txing=%d.\n",
223
                        dev->name, ei_local->tx1, ei_local->lasttx, ei_local->txing);
224
    } else {    /* We should never get here. */
225
        if (ei_debug)
226
                printk("%s: No Tx buffers free! irq=%d tx1=%d tx2=%d last=%d\n",
227
                        dev->name, dev->interrupt, ei_local->tx1, ei_local->tx2, ei_local->lasttx);
228
        ei_local->irqlock = 0;
229
        dev->tbusy = 1;
230
        outb_p(ENISR_ALL, e8390_base + EN0_IMR);
231
        return 1;
232
    }
233
 
234
    /*
235
     * Okay, now upload the packet and trigger a send if the transmitter
236
     * isn't already sending. If it is busy, the interrupt handler will
237
     * trigger the send later, upon receiving a Tx done interrupt.
238
     */
239
 
240
    ei_block_output(dev, length, skb->data, output_page);
241
    if (! ei_local->txing) {
242
        ei_local->txing = 1;
243
        NS8390_trigger_send(dev, send_length, output_page);
244
        dev->trans_start = jiffies;
245
        if (output_page == ei_local->tx_start_page) {
246
                ei_local->tx1 = -1;
247
                ei_local->lasttx = -1;
248
        } else {
249
                ei_local->tx2 = -1;
250
                ei_local->lasttx = -2;
251
        }
252
    } else
253
        ei_local->txqueue++;
254
 
255
    dev->tbusy = (ei_local->tx1  &&  ei_local->tx2);
256
 
257
#else   /* EI_PINGPONG */
258
 
259
    /*
260
     * Only one Tx buffer in use. You need two Tx bufs to come close to
261
     * back-to-back transmits. Expect a 20 -> 25% performance hit on
262
     * reasonable hardware if you only use one Tx buffer.
263
     */
264
 
265
    ei_block_output(dev, length, skb->data, ei_local->tx_start_page);
266
    ei_local->txing = 1;
267
    NS8390_trigger_send(dev, send_length, ei_local->tx_start_page);
268
    dev->trans_start = jiffies;
269
    dev->tbusy = 1;
270
 
271
#endif  /* EI_PINGPONG */
272
 
273
    /* Turn 8390 interrupts back on. */
274
    ei_local->irqlock = 0;
275
    outb_p(ENISR_ALL, e8390_base + EN0_IMR);
276
 
277
    dev_kfree_skb (skb, FREE_WRITE);
278
 
279
    return 0;
280
}
281
 
282
/* The typical workload of the driver:
283
   Handle the ether interface interrupts. */
284
void ei_interrupt(int irq, void *dev_id, struct pt_regs * regs)
285
{
286
    struct device *dev = (struct device *)dev_id;
287
    int e8390_base;
288
    int interrupts, nr_serviced = 0;
289
    struct ei_device *ei_local;
290
 
291
    if (dev == NULL) {
292
                printk ("net_interrupt(): irq %d for unknown device.\n", irq);
293
                return;
294
    }
295
    e8390_base = dev->base_addr;
296
    ei_local = (struct ei_device *) dev->priv;
297
    if (dev->interrupt || ei_local->irqlock) {
298
                /* The "irqlock" check is only for testing. */
299
                printk(ei_local->irqlock
300
                           ? "%s: Interrupted while interrupts are masked! isr=%#2x imr=%#2x.\n"
301
                           : "%s: Reentering the interrupt handler! isr=%#2x imr=%#2x.\n",
302
                           dev->name, inb_p(e8390_base + EN0_ISR),
303
                           inb_p(e8390_base + EN0_IMR));
304
                return;
305
    }
306
 
307
    dev->interrupt = 1;
308
 
309
    /* Change to page 0 and read the intr status reg. */
310
    outb_p(E8390_NODMA+E8390_PAGE0, e8390_base + E8390_CMD);
311
    if (ei_debug > 3)
312
                printk("%s: interrupt(isr=%#2.2x).\n", dev->name,
313
                           inb_p(e8390_base + EN0_ISR));
314
 
315
    /* !!Assumption!! -- we stay in page 0.      Don't break this. */
316
    while ((interrupts = inb_p(e8390_base + EN0_ISR)) != 0
317
                   && ++nr_serviced < MAX_SERVICE) {
318
                if (dev->start == 0) {
319
                        printk("%s: interrupt from stopped card\n", dev->name);
320
                        interrupts = 0;
321
                        break;
322
                }
323
                if (interrupts & ENISR_OVER) {
324
                        ei_rx_overrun(dev);
325
                } else if (interrupts & (ENISR_RX+ENISR_RX_ERR)) {
326
                        /* Got a good (?) packet. */
327
                        ei_receive(dev);
328
                }
329
                /* Push the next to-transmit packet through. */
330
                if (interrupts & ENISR_TX) {
331
                        ei_tx_intr(dev);
332
                } else if (interrupts & ENISR_TX_ERR) {
333
                        ei_tx_err(dev);
334
                }
335
 
336
                if (interrupts & ENISR_COUNTERS) {
337
                        ei_local->stat.rx_frame_errors += inb_p(e8390_base + EN0_COUNTER0);
338
                        ei_local->stat.rx_crc_errors   += inb_p(e8390_base + EN0_COUNTER1);
339
                        ei_local->stat.rx_missed_errors+= inb_p(e8390_base + EN0_COUNTER2);
340
                        outb_p(ENISR_COUNTERS, e8390_base + EN0_ISR); /* Ack intr. */
341
                }
342
 
343
                /* Ignore any RDC interrupts that make it back to here. */
344
                if (interrupts & ENISR_RDC) {
345
                        outb_p(ENISR_RDC, e8390_base + EN0_ISR);
346
                }
347
 
348
                outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base + E8390_CMD);
349
    }
350
 
351
    if (interrupts && ei_debug) {
352
                outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base + E8390_CMD);
353
                if (nr_serviced >= MAX_SERVICE) {
354
                        printk("%s: Too much work at interrupt, status %#2.2x\n",
355
                                   dev->name, interrupts);
356
                        outb_p(ENISR_ALL, e8390_base + EN0_ISR); /* Ack. most intrs. */
357
                } else {
358
                        printk("%s: unknown interrupt %#2x\n", dev->name, interrupts);
359
                        outb_p(0xff, e8390_base + EN0_ISR); /* Ack. all intrs. */
360
                }
361
    }
362
    dev->interrupt = 0;
363
    return;
364
}
365
 
366
/*
367
 * A transmitter error has happened. Most likely excess collisions (which
368
 * is a fairly normal condition). If the error is one where the Tx will
369
 * have been aborted, we try and send another one right away, instead of
370
 * letting the failed packet sit and collect dust in the Tx buffer. This
371
 * is a much better solution as it avoids kernel based Tx timeouts, and
372
 * an unnecessary card reset.
373
 */
374
 
375
static void ei_tx_err(struct device *dev)
376
{
377
    int e8390_base = dev->base_addr;
378
    unsigned char txsr = inb_p(e8390_base+EN0_TSR);
379
    unsigned char tx_was_aborted = txsr & (ENTSR_ABT+ENTSR_FU);
380
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
381
 
382
#ifdef VERBOSE_ERROR_DUMP
383
    printk(KERN_DEBUG "%s: transmitter error (%#2x): ", dev->name, txsr);
384
    if (txsr & ENTSR_ABT)
385
                printk("excess-collisions ");
386
    if (txsr & ENTSR_ND)
387
                printk("non-deferral ");
388
    if (txsr & ENTSR_CRS)
389
                printk("lost-carrier ");
390
    if (txsr & ENTSR_FU)
391
                printk("FIFO-underrun ");
392
    if (txsr & ENTSR_CDH)
393
                printk("lost-heartbeat ");
394
    printk("\n");
395
#endif
396
 
397
    outb_p(ENISR_TX_ERR, e8390_base + EN0_ISR); /* Ack intr. */
398
 
399
    if (tx_was_aborted)
400
                ei_tx_intr(dev);
401
 
402
    /*
403
     * Note: NCR reads zero on 16 collisions so we add them
404
     * in by hand. Somebody might care...
405
     */
406
    if (txsr & ENTSR_ABT)
407
        ei_local->stat.collisions += 16;
408
 
409
}
410
 
411
/* We have finished a transmit: check for errors and then trigger the next
412
   packet to be sent. */
413
static void ei_tx_intr(struct device *dev)
414
{
415
    int e8390_base = dev->base_addr;
416
    int status = inb(e8390_base + EN0_TSR);
417
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
418
 
419
    outb_p(ENISR_TX, e8390_base + EN0_ISR); /* Ack intr. */
420
 
421
#ifdef EI_PINGPONG
422
 
423
    /*
424
     * There are two Tx buffers, see which one finished, and trigger
425
     * the send of another one if it exists.
426
     */
427
    ei_local->txqueue--;
428
    if (ei_local->tx1 < 0) {
429
        if (ei_local->lasttx != 1 && ei_local->lasttx != -1)
430
                printk("%s: bogus last_tx_buffer %d, tx1=%d.\n",
431
                           ei_local->name, ei_local->lasttx, ei_local->tx1);
432
        ei_local->tx1 = 0;
433
        dev->tbusy = 0;
434
        if (ei_local->tx2 > 0) {
435
                ei_local->txing = 1;
436
                NS8390_trigger_send(dev, ei_local->tx2, ei_local->tx_start_page + 6);
437
                dev->trans_start = jiffies;
438
                ei_local->tx2 = -1,
439
                ei_local->lasttx = 2;
440
        } else
441
                ei_local->lasttx = 20, ei_local->txing = 0;
442
    } else if (ei_local->tx2 < 0) {
443
        if (ei_local->lasttx != 2  &&  ei_local->lasttx != -2)
444
                printk("%s: bogus last_tx_buffer %d, tx2=%d.\n",
445
                           ei_local->name, ei_local->lasttx, ei_local->tx2);
446
        ei_local->tx2 = 0;
447
        dev->tbusy = 0;
448
        if (ei_local->tx1 > 0) {
449
                ei_local->txing = 1;
450
                NS8390_trigger_send(dev, ei_local->tx1, ei_local->tx_start_page);
451
                dev->trans_start = jiffies;
452
                ei_local->tx1 = -1;
453
                ei_local->lasttx = 1;
454
        } else
455
                ei_local->lasttx = 10, ei_local->txing = 0;
456
    } else
457
        printk("%s: unexpected TX-done interrupt, lasttx=%d.\n",
458
                   dev->name, ei_local->lasttx);
459
 
460
#else   /* EI_PINGPONG */
461
    /*
462
     *  Single Tx buffer: mark it free so another packet can be loaded.
463
     */
464
    ei_local->txing = 0;
465
    dev->tbusy = 0;
466
#endif
467
 
468
    /* Minimize Tx latency: update the statistics after we restart TXing. */
469
    if (status & ENTSR_COL)
470
        ei_local->stat.collisions++;
471
    if (status & ENTSR_PTX)
472
        ei_local->stat.tx_packets++;
473
    else {
474
        ei_local->stat.tx_errors++;
475
        if (status & ENTSR_ABT) ei_local->stat.tx_aborted_errors++;
476
        if (status & ENTSR_CRS) ei_local->stat.tx_carrier_errors++;
477
        if (status & ENTSR_FU)  ei_local->stat.tx_fifo_errors++;
478
        if (status & ENTSR_CDH) ei_local->stat.tx_heartbeat_errors++;
479
        if (status & ENTSR_OWC) ei_local->stat.tx_window_errors++;
480
    }
481
 
482
    mark_bh (NET_BH);
483
}
484
 
485
/* We have a good packet(s), get it/them out of the buffers. */
486
 
487
static void ei_receive(struct device *dev)
488
{
489
    int e8390_base = dev->base_addr;
490
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
491
    unsigned char rxing_page, this_frame, next_frame;
492
    unsigned short current_offset;
493
    int rx_pkt_count = 0;
494
    struct e8390_pkt_hdr rx_frame;
495
    int num_rx_pages = ei_local->stop_page-ei_local->rx_start_page;
496
 
497
    while (++rx_pkt_count < 10) {
498
                int pkt_len;
499
 
500
                /* Get the rx page (incoming packet pointer). */
501
                outb_p(E8390_NODMA+E8390_PAGE1, e8390_base + E8390_CMD);
502
                rxing_page = inb_p(e8390_base + EN1_CURPAG);
503
                outb_p(E8390_NODMA+E8390_PAGE0, e8390_base + E8390_CMD);
504
 
505
                /* Remove one frame from the ring.  Boundary is always a page behind. */
506
                this_frame = inb_p(e8390_base + EN0_BOUNDARY) + 1;
507
                if (this_frame >= ei_local->stop_page)
508
                        this_frame = ei_local->rx_start_page;
509
 
510
                /* Someday we'll omit the previous, iff we never get this message.
511
                   (There is at least one clone claimed to have a problem.)  */
512
                if (ei_debug > 0  &&  this_frame != ei_local->current_page)
513
                        printk("%s: mismatched read page pointers %2x vs %2x.\n",
514
                                   dev->name, this_frame, ei_local->current_page);
515
 
516
                if (this_frame == rxing_page)   /* Read all the frames? */
517
                        break;                          /* Done for now */
518
 
519
                current_offset = this_frame << 8;
520
                ei_get_8390_hdr(dev, &rx_frame, this_frame);
521
 
522
                pkt_len = rx_frame.count - sizeof(struct e8390_pkt_hdr);
523
 
524
                next_frame = this_frame + 1 + ((pkt_len+4)>>8);
525
 
526
                /* Check for bogosity warned by 3c503 book: the status byte is never
527
                   written.  This happened a lot during testing! This code should be
528
                   cleaned up someday. */
529
                if (rx_frame.next != next_frame
530
                        && rx_frame.next != next_frame + 1
531
                        && rx_frame.next != next_frame - num_rx_pages
532
                        && rx_frame.next != next_frame + 1 - num_rx_pages) {
533
                        ei_local->current_page = rxing_page;
534
                        outb(ei_local->current_page-1, e8390_base+EN0_BOUNDARY);
535
                        ei_local->stat.rx_errors++;
536
                        continue;
537
                }
538
 
539
                if (pkt_len < 60  ||  pkt_len > 1518) {
540
                        if (ei_debug)
541
                                printk("%s: bogus packet size: %d, status=%#2x nxpg=%#2x.\n",
542
                                           dev->name, rx_frame.count, rx_frame.status,
543
                                           rx_frame.next);
544
                        ei_local->stat.rx_errors++;
545
                } else if ((rx_frame.status & 0x0F) == ENRSR_RXOK) {
546
                        struct sk_buff *skb;
547
 
548
                        skb = dev_alloc_skb(pkt_len+2);
549
                        if (skb == NULL) {
550
                                if (ei_debug > 1)
551
                                        printk("%s: Couldn't allocate a sk_buff of size %d.\n",
552
                                                   dev->name, pkt_len);
553
                                ei_local->stat.rx_dropped++;
554
                                break;
555
                        } else {
556
                                skb_reserve(skb,2);     /* IP headers on 16 byte boundaries */
557
                                skb->dev = dev;
558
                                skb_put(skb, pkt_len);  /* Make room */
559
                                ei_block_input(dev, pkt_len, skb, current_offset + sizeof(rx_frame));
560
                                skb->protocol=eth_type_trans(skb,dev);
561
                                netif_rx(skb);
562
                                ei_local->stat.rx_packets++;
563
                        }
564
                } else {
565
                        int errs = rx_frame.status;
566
                        if (ei_debug)
567
                                printk("%s: bogus packet: status=%#2x nxpg=%#2x size=%d\n",
568
                                           dev->name, rx_frame.status, rx_frame.next,
569
                                           rx_frame.count);
570
                        if (errs & ENRSR_FO)
571
                                ei_local->stat.rx_fifo_errors++;
572
                }
573
                next_frame = rx_frame.next;
574
 
575
                /* This _should_ never happen: it's here for avoiding bad clones. */
576
                if (next_frame >= ei_local->stop_page) {
577
                        printk("%s: next frame inconsistency, %#2x\n", dev->name,
578
                                   next_frame);
579
                        next_frame = ei_local->rx_start_page;
580
                }
581
                ei_local->current_page = next_frame;
582
                outb_p(next_frame-1, e8390_base+EN0_BOUNDARY);
583
    }
584
 
585
    /* We used to also ack ENISR_OVER here, but that would sometimes mask
586
    a real overrun, leaving the 8390 in a stopped state with rec'vr off. */
587
    outb_p(ENISR_RX+ENISR_RX_ERR, e8390_base+EN0_ISR);
588
    return;
589
}
590
 
591
/*
592
 * We have a receiver overrun: we have to kick the 8390 to get it started
593
 * again. Problem is that you have to kick it exactly as NS prescribes in
594
 * the updated datasheets, or "the NIC may act in an unpredictable manner."
595
 * This includes causing "the NIC to defer indefinitely when it is stopped
596
 * on a busy network."  Ugh.
597
 */
598
static void ei_rx_overrun(struct device *dev)
599
{
600
    int e8390_base = dev->base_addr;
601
    unsigned long wait_start_time;
602
    unsigned char was_txing, must_resend = 0;
603
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
604
 
605
    /*
606
     * Record whether a Tx was in progress and then issue the
607
     * stop command.
608
     */
609
    was_txing = inb_p(e8390_base+E8390_CMD) & E8390_TRANS;
610
    outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD);
611
 
612
    if (ei_debug > 1)
613
        printk("%s: Receiver overrun.\n", dev->name);
614
    ei_local->stat.rx_over_errors++;
615
 
616
    /*
617
     * Wait a full Tx time (1.2ms) + some guard time, NS says 1.6ms total.
618
     * Early datasheets said to poll the reset bit, but now they say that
619
     * it "is not a reliable indicator and subsequently should be ignored."
620
     * We wait at least 10ms.
621
     */
622
    wait_start_time = jiffies;
623
    while (jiffies - wait_start_time <= 1*HZ/100)
624
        barrier();
625
 
626
    /*
627
     * Reset RBCR[01] back to zero as per magic incantation.
628
     */
629
    outb_p(0x00, e8390_base+EN0_RCNTLO);
630
    outb_p(0x00, e8390_base+EN0_RCNTHI);
631
 
632
    /*
633
     * See if any Tx was interrupted or not. According to NS, this
634
     * step is vital, and skipping it will cause no end of havoc.
635
     */
636
    if (was_txing) {
637
        unsigned char tx_completed = inb_p(e8390_base+EN0_ISR) & (ENISR_TX+ENISR_TX_ERR);
638
        if (!tx_completed) must_resend = 1;
639
    }
640
 
641
    /*
642
     * Have to enter loopback mode and then restart the NIC before
643
     * you are allowed to slurp packets up off the ring.
644
     */
645
    outb_p(E8390_TXOFF, e8390_base + EN0_TXCR);
646
    outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START, e8390_base + E8390_CMD);
647
 
648
    /*
649
     * Clear the Rx ring of all the debris, and ack the interrupt.
650
     */
651
    ei_receive(dev);
652
    outb_p(ENISR_OVER, e8390_base+EN0_ISR);
653
 
654
    /*
655
     * Leave loopback mode, and resend any packet that got stopped.
656
     */
657
    outb_p(E8390_TXCONFIG, e8390_base + EN0_TXCR);
658
    if (must_resend)
659
        outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START + E8390_TRANS, e8390_base + E8390_CMD);
660
 
661
}
662
 
663
static struct enet_statistics *get_stats(struct device *dev)
664
{
665
    int ioaddr = dev->base_addr;
666
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
667
 
668
    /* If the card is stopped, just return the present stats. */
669
    if (dev->start == 0) return &ei_local->stat;
670
 
671
    /* Read the counter registers, assuming we are in page 0. */
672
    ei_local->stat.rx_frame_errors += inb_p(ioaddr + EN0_COUNTER0);
673
    ei_local->stat.rx_crc_errors   += inb_p(ioaddr + EN0_COUNTER1);
674
    ei_local->stat.rx_missed_errors+= inb_p(ioaddr + EN0_COUNTER2);
675
 
676
    return &ei_local->stat;
677
}
678
 
679
/*
680
 *      Set or clear the multicast filter for this adaptor.
681
 */
682
 
683
static void set_multicast_list(struct device *dev)
684
{
685
        int ioaddr = dev->base_addr;
686
 
687
        if(dev->flags&IFF_PROMISC)
688
        {
689
                outb_p(E8390_RXCONFIG | 0x18, ioaddr + EN0_RXCR);
690
        }
691
        else if((dev->flags&IFF_ALLMULTI)||dev->mc_list)
692
        {
693
                /* The multicast-accept list is initialized to accept-all, and we
694
                   rely on higher-level filtering for now. */
695
                outb_p(E8390_RXCONFIG | 0x08, ioaddr + EN0_RXCR);
696
        }
697
        else
698
                outb_p(E8390_RXCONFIG, ioaddr + EN0_RXCR);
699
}
700
 
701
/* Initialize the rest of the 8390 device structure. */
702
int ethdev_init(struct device *dev)
703
{
704
    if (ei_debug > 1)
705
                printk(version);
706
 
707
    if (dev->priv == NULL) {
708
                struct ei_device *ei_local;
709
 
710
                dev->priv = kmalloc(sizeof(struct ei_device), GFP_KERNEL);
711
                if (dev->priv == NULL)
712
                        return -ENOMEM;
713
                memset(dev->priv, 0, sizeof(struct ei_device));
714
                ei_local = (struct ei_device *)dev->priv;
715
    }
716
 
717
    dev->hard_start_xmit = &ei_start_xmit;
718
    dev->get_stats      = get_stats;
719
    dev->set_multicast_list = &set_multicast_list;
720
 
721
    ether_setup(dev);
722
 
723
    return 0;
724
}
725
 
726
 
727
/* This page of functions should be 8390 generic */
728
/* Follow National Semi's recommendations for initializing the "NIC". */
729
void NS8390_init(struct device *dev, int startp)
730
{
731
    int e8390_base = dev->base_addr;
732
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
733
    int i;
734
    int endcfg = ei_local->word16 ? (0x48 | ENDCFG_WTS) : 0x48;
735
    unsigned long flags;
736
 
737
    /* Follow National Semi's recommendations for initing the DP83902. */
738
    outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base); /* 0x21 */
739
    outb_p(endcfg, e8390_base + EN0_DCFG);      /* 0x48 or 0x49 */
740
    /* Clear the remote byte count registers. */
741
    outb_p(0x00,  e8390_base + EN0_RCNTLO);
742
    outb_p(0x00,  e8390_base + EN0_RCNTHI);
743
    /* Set to monitor and loopback mode -- this is vital!. */
744
    outb_p(E8390_RXOFF, e8390_base + EN0_RXCR); /* 0x20 */
745
    outb_p(E8390_TXOFF, e8390_base + EN0_TXCR); /* 0x02 */
746
    /* Set the transmit page and receive ring. */
747
    outb_p(ei_local->tx_start_page,      e8390_base + EN0_TPSR);
748
    ei_local->tx1 = ei_local->tx2 = 0;
749
    outb_p(ei_local->rx_start_page,      e8390_base + EN0_STARTPG);
750
    outb_p(ei_local->stop_page-1, e8390_base + EN0_BOUNDARY); /* 3c503 says 0x3f,NS0x26*/
751
    ei_local->current_page = ei_local->rx_start_page;           /* assert boundary+1 */
752
    outb_p(ei_local->stop_page,   e8390_base + EN0_STOPPG);
753
    /* Clear the pending interrupts and mask. */
754
    outb_p(0xFF, e8390_base + EN0_ISR);
755
    outb_p(0x00,  e8390_base + EN0_IMR);
756
 
757
    /* Copy the station address into the DS8390 registers,
758
       and set the multicast hash bitmap to receive all multicasts. */
759
    save_flags(flags);
760
    cli();
761
    outb_p(E8390_NODMA + E8390_PAGE1 + E8390_STOP, e8390_base); /* 0x61 */
762
    for(i = 0; i < 6; i++) {
763
                outb_p(dev->dev_addr[i], e8390_base + EN1_PHYS + i);
764
    }
765
    /* Initialize the multicast list to accept-all.  If we enable multicast
766
       the higher levels can do the filtering. */
767
    for(i = 0; i < 8; i++)
768
                outb_p(0xff, e8390_base + EN1_MULT + i);
769
 
770
    outb_p(ei_local->rx_start_page,      e8390_base + EN1_CURPAG);
771
    outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base);
772
    restore_flags(flags);
773
    dev->tbusy = 0;
774
    dev->interrupt = 0;
775
    ei_local->tx1 = ei_local->tx2 = 0;
776
    ei_local->txing = 0;
777
    if (startp) {
778
                outb_p(0xff,  e8390_base + EN0_ISR);
779
                outb_p(ENISR_ALL,  e8390_base + EN0_IMR);
780
                outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base);
781
                outb_p(E8390_TXCONFIG, e8390_base + EN0_TXCR); /* xmit on. */
782
                /* 3c503 TechMan says rxconfig only after the NIC is started. */
783
                outb_p(E8390_RXCONFIG,  e8390_base + EN0_RXCR); /* rx on,  */
784
                dev->set_multicast_list(dev);           /* Get the multicast status right if this
785
                                                           was a reset. */
786
    }
787
    return;
788
}
789
 
790
/* Trigger a transmit start, assuming the length is valid. */
791
static void NS8390_trigger_send(struct device *dev, unsigned int length,
792
                                                                int start_page)
793
{
794
    int e8390_base = dev->base_addr;
795
 
796
    outb_p(E8390_NODMA+E8390_PAGE0, e8390_base);
797
 
798
    if (inb_p(e8390_base) & E8390_TRANS) {
799
                printk("%s: trigger_send() called with the transmitter busy.\n",
800
                           dev->name);
801
                return;
802
    }
803
    outb_p(length & 0xff, e8390_base + EN0_TCNTLO);
804
    outb_p(length >> 8, e8390_base + EN0_TCNTHI);
805
    outb_p(start_page, e8390_base + EN0_TPSR);
806
    outb_p(E8390_NODMA+E8390_TRANS+E8390_START, e8390_base);
807
    return;
808
}
809
 
810
#ifdef MODULE
811
 
812
int init_module(void)
813
{
814
     return 0;
815
}
816
 
817
void
818
cleanup_module(void)
819
{
820
}
821
#endif /* MODULE */
822
 
823
/*
824
 * Local variables:
825
 *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c 8390.c"
826
 *  version-control: t
827
 *  kept-new-versions: 5
828
 *  c-indent-level: 4
829
 *  tab-width: 4
830
 * End:
831
 */

powered by: WebSVN 2.1.0

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