1 /* 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 General Public License, incorporated herein by reference.
10
11 The author may be reached as becker@scyld.com, or C/O
12 Scyld Computing Corporation
13 410 Severn Ave., Suite 210
14 Annapolis MD 21403
15
16
17 This is the chip-specific code for many 8390-based ethernet adaptors.
18 This is not a complete driver, it must be combined with board-specific
19 code such as ne.c, wd.c, 3c503.c, etc.
20
21 Seeing how at least eight drivers use this code, (not counting the
22 PCMCIA ones either) it is easy to break some card by what seems like
23 a simple innocent change. Please contact me or Donald if you think
24 you have found something that needs changing. -- PG
25
26
27 Changelog:
28
29 Paul Gortmaker : remove set_bit lock, other cleanups.
30 Paul Gortmaker : add ei_get_8390_hdr() so we can pass skb's to
31 ei_block_input() for eth_io_copy_and_sum().
32 Paul Gortmaker : exchange static int ei_pingpong for a #define,
33 also add better Tx error handling.
34 Paul Gortmaker : rewrite Rx overrun handling as per NS specs.
35 Alexey Kuznetsov : use the 8390's six bit hash multicast filter.
36 Paul Gortmaker : tweak ANK's above multicast changes a bit.
37 Paul Gortmaker : update packet statistics for v2.1.x
38 Alan Cox : support arbitary stupid port mappings on the
39 68K Macintosh. Support >16bit I/O spaces
40 Paul Gortmaker : add kmod support for auto-loading of the 8390
41 module by all drivers that require it.
42 Alan Cox : Spinlocking work, added 'BUG_83C690'
43 Paul Gortmaker : Separate out Tx timeout code from Tx path.
44
45 Sources:
46 The National Semiconductor LAN Databook, and the 3Com 3c503 databook.
47
48 */
49
50 static const char version[] =
51 "8390.c:v1.10cvs 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
52
53 #include <linux/module.h>
54 #include <linux/kernel.h>
55 #include <linux/sched.h>
56 #include <linux/fs.h>
57 #include <linux/types.h>
58 #include <linux/ptrace.h>
59 #include <linux/string.h>
60 #include <asm/system.h>
61 #include <asm/uaccess.h>
62 #include <asm/bitops.h>
63 #include <asm/io.h>
64 #include <asm/irq.h>
65 #include <linux/delay.h>
66 #include <linux/errno.h>
67 #include <linux/fcntl.h>
68 #include <linux/in.h>
69 #include <linux/interrupt.h>
70 #include <linux/init.h>
71 #include <linux/crc32.h>
72
73 #include <linux/netdevice.h>
74 #include <linux/etherdevice.h>
75
76 #define NS8390_CORE
77 #include "8390.h"
78
79 #define BUG_83C690
80
81 /* These are the operational function interfaces to board-specific
82 routines.
83 void reset_8390(struct net_device *dev)
84 Resets the board associated with DEV, including a hardware reset of
85 the 8390. This is only called when there is a transmit timeout, and
86 it is always followed by 8390_init().
87 void block_output(struct net_device *dev, int count, const unsigned char *buf,
88 int start_page)
89 Write the COUNT bytes of BUF to the packet buffer at START_PAGE. The
90 "page" value uses the 8390's 256-byte pages.
91 void get_8390_hdr(struct net_device *dev, struct e8390_hdr *hdr, int ring_page)
92 Read the 4 byte, page aligned 8390 header. *If* there is a
93 subsequent read, it will be of the rest of the packet.
94 void block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
95 Read COUNT bytes from the packet buffer into the skb data area. Start
96 reading from RING_OFFSET, the address as the 8390 sees it. This will always
97 follow the read of the 8390 header.
98 */
99 #define ei_reset_8390 (ei_local->reset_8390)
100 #define ei_block_output (ei_local->block_output)
101 #define ei_block_input (ei_local->block_input)
102 #define ei_get_8390_hdr (ei_local->get_8390_hdr)
103
104 /* use 0 for production, 1 for verification, >2 for debug */
105 #ifndef ei_debug
106 int ei_debug = 1;
107 #endif
108
109 /* Index to functions. */
110 static void ei_tx_intr(struct net_device *dev);
111 static void ei_tx_err(struct net_device *dev);
112 static void ei_tx_timeout(struct net_device *dev);
113 static void ei_receive(struct net_device *dev);
114 static void ei_rx_overrun(struct net_device *dev);
115
116 /* Routines generic to NS8390-based boards. */
117 static void NS8390_trigger_send(struct net_device *dev, unsigned int length,
118 int start_page);
119 static void set_multicast_list(struct net_device *dev);
120 static void do_set_multicast_list(struct net_device *dev);
121
122 /*
123 * SMP and the 8390 setup.
124 *
125 * The 8390 isnt exactly designed to be multithreaded on RX/TX. There is
126 * a page register that controls bank and packet buffer access. We guard
127 * this with ei_local->page_lock. Nobody should assume or set the page other
128 * than zero when the lock is not held. Lock holders must restore page 0
129 * before unlocking. Even pure readers must take the lock to protect in
130 * page 0.
131 *
132 * To make life difficult the chip can also be very slow. We therefore can't
133 * just use spinlocks. For the longer lockups we disable the irq the device
134 * sits on and hold the lock. We must hold the lock because there is a dual
135 * processor case other than interrupts (get stats/set multicast list in
136 * parallel with each other and transmit).
137 *
138 * Note: in theory we can just disable the irq on the card _but_ there is
139 * a latency on SMP irq delivery. So we can easily go "disable irq" "sync irqs"
140 * enter lock, take the queued irq. So we waddle instead of flying.
141 *
142 * Finally by special arrangement for the purpose of being generally
143 * annoying the transmit function is called bh atomic. That places
144 * restrictions on the user context callers as disable_irq won't save
145 * them.
146 */
147
148
149
150 /**
151 * ei_open - Open/initialize the board.
152 * @dev: network device to initialize
153 *
154 * This routine goes all-out, setting everything
155 * up anew at each open, even though many of these registers should only
156 * need to be set once at boot.
157 */
ei_open(struct net_device * dev)158 int ei_open(struct net_device *dev)
159 {
160 unsigned long flags;
161 struct ei_device *ei_local = (struct ei_device *) dev->priv;
162
163 /* This can't happen unless somebody forgot to call ethdev_init(). */
164 if (ei_local == NULL)
165 {
166 printk(KERN_EMERG "%s: ei_open passed a non-existent device!\n", dev->name);
167 return -ENXIO;
168 }
169
170 /* The card I/O part of the driver (e.g. 3c503) can hook a Tx timeout
171 wrapper that does e.g. media check & then calls ei_tx_timeout. */
172 if (dev->tx_timeout == NULL)
173 dev->tx_timeout = ei_tx_timeout;
174 if (dev->watchdog_timeo <= 0)
175 dev->watchdog_timeo = TX_TIMEOUT;
176
177 /*
178 * Grab the page lock so we own the register set, then call
179 * the init function.
180 */
181
182 spin_lock_irqsave(&ei_local->page_lock, flags);
183 NS8390_init(dev, 1);
184 /* Set the flag before we drop the lock, That way the IRQ arrives
185 after its set and we get no silly warnings */
186 netif_start_queue(dev);
187 spin_unlock_irqrestore(&ei_local->page_lock, flags);
188 ei_local->irqlock = 0;
189 return 0;
190 }
191
192 /**
193 * ei_close - shut down network device
194 * @dev: network device to close
195 *
196 * Opposite of ei_open(). Only used when "ifconfig <devname> down" is done.
197 */
ei_close(struct net_device * dev)198 int ei_close(struct net_device *dev)
199 {
200 struct ei_device *ei_local = (struct ei_device *) dev->priv;
201 unsigned long flags;
202
203 /*
204 * Hold the page lock during close
205 */
206
207 spin_lock_irqsave(&ei_local->page_lock, flags);
208 NS8390_init(dev, 0);
209 spin_unlock_irqrestore(&ei_local->page_lock, flags);
210 netif_stop_queue(dev);
211 return 0;
212 }
213
214 /**
215 * ei_tx_timeout - handle transmit time out condition
216 * @dev: network device which has apparently fallen asleep
217 *
218 * Called by kernel when device never acknowledges a transmit has
219 * completed (or failed) - i.e. never posted a Tx related interrupt.
220 */
221
ei_tx_timeout(struct net_device * dev)222 void ei_tx_timeout(struct net_device *dev)
223 {
224 long e8390_base = dev->base_addr;
225 struct ei_device *ei_local = (struct ei_device *) dev->priv;
226 int txsr, isr, tickssofar = jiffies - dev->trans_start;
227 unsigned long flags;
228
229 ei_local->stat.tx_errors++;
230
231 spin_lock_irqsave(&ei_local->page_lock, flags);
232 txsr = inb(e8390_base+EN0_TSR);
233 isr = inb(e8390_base+EN0_ISR);
234 spin_unlock_irqrestore(&ei_local->page_lock, flags);
235
236 printk(KERN_DEBUG "%s: Tx timed out, %s TSR=%#2x, ISR=%#2x, t=%d.\n",
237 dev->name, (txsr & ENTSR_ABT) ? "excess collisions." :
238 (isr) ? "lost interrupt?" : "cable problem?", txsr, isr, tickssofar);
239
240 if (!isr && !ei_local->stat.tx_packets)
241 {
242 /* The 8390 probably hasn't gotten on the cable yet. */
243 ei_local->interface_num ^= 1; /* Try a different xcvr. */
244 }
245
246 /* Ugly but a reset can be slow, yet must be protected */
247
248 disable_irq_nosync(dev->irq);
249 spin_lock(&ei_local->page_lock);
250
251 /* Try to restart the card. Perhaps the user has fixed something. */
252 ei_reset_8390(dev);
253 NS8390_init(dev, 1);
254
255 spin_unlock(&ei_local->page_lock);
256 enable_irq(dev->irq);
257 netif_wake_queue(dev);
258 }
259
260 /**
261 * ei_start_xmit - begin packet transmission
262 * @skb: packet to be sent
263 * @dev: network device to which packet is sent
264 *
265 * Sends a packet to an 8390 network device.
266 */
267
ei_start_xmit(struct sk_buff * skb,struct net_device * dev)268 static int ei_start_xmit(struct sk_buff *skb, struct net_device *dev)
269 {
270 long e8390_base = dev->base_addr;
271 struct ei_device *ei_local = (struct ei_device *) dev->priv;
272 int length, send_length, output_page;
273 unsigned long flags;
274 char scratch[ETH_ZLEN];
275
276 length = skb->len;
277
278 /* Mask interrupts from the ethercard.
279 SMP: We have to grab the lock here otherwise the IRQ handler
280 on another CPU can flip window and race the IRQ mask set. We end
281 up trashing the mcast filter not disabling irqs if we dont lock */
282
283 spin_lock_irqsave(&ei_local->page_lock, flags);
284 outb_p(0x00, e8390_base + EN0_IMR);
285 spin_unlock_irqrestore(&ei_local->page_lock, flags);
286
287
288 /*
289 * Slow phase with lock held.
290 */
291
292 disable_irq_nosync(dev->irq);
293
294 spin_lock(&ei_local->page_lock);
295
296 ei_local->irqlock = 1;
297
298 send_length = ETH_ZLEN < length ? length : ETH_ZLEN;
299
300 #ifdef EI_PINGPONG
301
302 /*
303 * We have two Tx slots available for use. Find the first free
304 * slot, and then perform some sanity checks. With two Tx bufs,
305 * you get very close to transmitting back-to-back packets. With
306 * only one Tx buf, the transmitter sits idle while you reload the
307 * card, leaving a substantial gap between each transmitted packet.
308 */
309
310 if (ei_local->tx1 == 0)
311 {
312 output_page = ei_local->tx_start_page;
313 ei_local->tx1 = send_length;
314 if (ei_debug && ei_local->tx2 > 0)
315 printk(KERN_DEBUG "%s: idle transmitter tx2=%d, lasttx=%d, txing=%d.\n",
316 dev->name, ei_local->tx2, ei_local->lasttx, ei_local->txing);
317 }
318 else if (ei_local->tx2 == 0)
319 {
320 output_page = ei_local->tx_start_page + TX_1X_PAGES;
321 ei_local->tx2 = send_length;
322 if (ei_debug && ei_local->tx1 > 0)
323 printk(KERN_DEBUG "%s: idle transmitter, tx1=%d, lasttx=%d, txing=%d.\n",
324 dev->name, ei_local->tx1, ei_local->lasttx, ei_local->txing);
325 }
326 else
327 { /* We should never get here. */
328 if (ei_debug)
329 printk(KERN_DEBUG "%s: No Tx buffers free! tx1=%d tx2=%d last=%d\n",
330 dev->name, ei_local->tx1, ei_local->tx2, ei_local->lasttx);
331 ei_local->irqlock = 0;
332 netif_stop_queue(dev);
333 outb_p(ENISR_ALL, e8390_base + EN0_IMR);
334 spin_unlock(&ei_local->page_lock);
335 enable_irq(dev->irq);
336 ei_local->stat.tx_errors++;
337 return 1;
338 }
339
340 /*
341 * Okay, now upload the packet and trigger a send if the transmitter
342 * isn't already sending. If it is busy, the interrupt handler will
343 * trigger the send later, upon receiving a Tx done interrupt.
344 */
345
346 if(length == send_length)
347 ei_block_output(dev, length, skb->data, output_page);
348 else
349 {
350 memset(scratch, 0, ETH_ZLEN);
351 memcpy(scratch, skb->data, skb->len);
352 ei_block_output(dev, ETH_ZLEN, scratch, output_page);
353 }
354
355 if (! ei_local->txing)
356 {
357 ei_local->txing = 1;
358 NS8390_trigger_send(dev, send_length, output_page);
359 dev->trans_start = jiffies;
360 if (output_page == ei_local->tx_start_page)
361 {
362 ei_local->tx1 = -1;
363 ei_local->lasttx = -1;
364 }
365 else
366 {
367 ei_local->tx2 = -1;
368 ei_local->lasttx = -2;
369 }
370 }
371 else ei_local->txqueue++;
372
373 if (ei_local->tx1 && ei_local->tx2)
374 netif_stop_queue(dev);
375 else
376 netif_start_queue(dev);
377
378 #else /* EI_PINGPONG */
379
380 /*
381 * Only one Tx buffer in use. You need two Tx bufs to come close to
382 * back-to-back transmits. Expect a 20 -> 25% performance hit on
383 * reasonable hardware if you only use one Tx buffer.
384 */
385
386 if(length == send_length)
387 ei_block_output(dev, length, skb->data, ei_local->tx_start_page);
388 else
389 {
390 memset(scratch, 0, ETH_ZLEN);
391 memcpy(scratch, skb->data, skb->len);
392 ei_block_output(dev, ETH_ZLEN, scratch, ei_local->tx_start_page);
393 }
394 ei_local->txing = 1;
395 NS8390_trigger_send(dev, send_length, ei_local->tx_start_page);
396 dev->trans_start = jiffies;
397 netif_stop_queue(dev);
398
399 #endif /* EI_PINGPONG */
400
401 /* Turn 8390 interrupts back on. */
402 ei_local->irqlock = 0;
403 outb_p(ENISR_ALL, e8390_base + EN0_IMR);
404
405 spin_unlock(&ei_local->page_lock);
406 enable_irq(dev->irq);
407
408 dev_kfree_skb (skb);
409 ei_local->stat.tx_bytes += send_length;
410
411 return 0;
412 }
413
414 /**
415 * ei_interrupt - handle the interrupts from an 8390
416 * @irq: interrupt number
417 * @dev_id: a pointer to the net_device
418 * @regs: unused
419 *
420 * Handle the ether interface interrupts. We pull packets from
421 * the 8390 via the card specific functions and fire them at the networking
422 * stack. We also handle transmit completions and wake the transmit path if
423 * neccessary. We also update the counters and do other housekeeping as
424 * needed.
425 */
426
ei_interrupt(int irq,void * dev_id,struct pt_regs * regs)427 void ei_interrupt(int irq, void *dev_id, struct pt_regs * regs)
428 {
429 struct net_device *dev = dev_id;
430 long e8390_base;
431 int interrupts, nr_serviced = 0;
432 struct ei_device *ei_local;
433
434 if (dev == NULL)
435 {
436 printk ("net_interrupt(): irq %d for unknown device.\n", irq);
437 return;
438 }
439
440 e8390_base = dev->base_addr;
441 ei_local = (struct ei_device *) dev->priv;
442
443 /*
444 * Protect the irq test too.
445 */
446
447 spin_lock(&ei_local->page_lock);
448
449 if (ei_local->irqlock)
450 {
451 #if 1 /* This might just be an interrupt for a PCI device sharing this line */
452 /* The "irqlock" check is only for testing. */
453 printk(ei_local->irqlock
454 ? "%s: Interrupted while interrupts are masked! isr=%#2x imr=%#2x.\n"
455 : "%s: Reentering the interrupt handler! isr=%#2x imr=%#2x.\n",
456 dev->name, inb_p(e8390_base + EN0_ISR),
457 inb_p(e8390_base + EN0_IMR));
458 #endif
459 spin_unlock(&ei_local->page_lock);
460 return;
461 }
462
463 /* Change to page 0 and read the intr status reg. */
464 outb_p(E8390_NODMA+E8390_PAGE0, e8390_base + E8390_CMD);
465 if (ei_debug > 3)
466 printk(KERN_DEBUG "%s: interrupt(isr=%#2.2x).\n", dev->name,
467 inb_p(e8390_base + EN0_ISR));
468
469 /* !!Assumption!! -- we stay in page 0. Don't break this. */
470 while ((interrupts = inb_p(e8390_base + EN0_ISR)) != 0
471 && ++nr_serviced < MAX_SERVICE)
472 {
473 if (!netif_running(dev)) {
474 printk(KERN_WARNING "%s: interrupt from stopped card\n", dev->name);
475 /* rmk - acknowledge the interrupts */
476 outb_p(interrupts, e8390_base + EN0_ISR);
477 interrupts = 0;
478 break;
479 }
480 if (interrupts & ENISR_OVER)
481 ei_rx_overrun(dev);
482 else if (interrupts & (ENISR_RX+ENISR_RX_ERR))
483 {
484 /* Got a good (?) packet. */
485 ei_receive(dev);
486 }
487 /* Push the next to-transmit packet through. */
488 if (interrupts & ENISR_TX)
489 ei_tx_intr(dev);
490 else if (interrupts & ENISR_TX_ERR)
491 ei_tx_err(dev);
492
493 if (interrupts & ENISR_COUNTERS)
494 {
495 ei_local->stat.rx_frame_errors += inb_p(e8390_base + EN0_COUNTER0);
496 ei_local->stat.rx_crc_errors += inb_p(e8390_base + EN0_COUNTER1);
497 ei_local->stat.rx_missed_errors+= inb_p(e8390_base + EN0_COUNTER2);
498 outb_p(ENISR_COUNTERS, e8390_base + EN0_ISR); /* Ack intr. */
499 }
500
501 /* Ignore any RDC interrupts that make it back to here. */
502 if (interrupts & ENISR_RDC)
503 {
504 outb_p(ENISR_RDC, e8390_base + EN0_ISR);
505 }
506
507 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base + E8390_CMD);
508 }
509
510 if (interrupts && ei_debug)
511 {
512 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base + E8390_CMD);
513 if (nr_serviced >= MAX_SERVICE)
514 {
515 /* 0xFF is valid for a card removal */
516 if(interrupts!=0xFF)
517 printk(KERN_WARNING "%s: Too much work at interrupt, status %#2.2x\n",
518 dev->name, interrupts);
519 outb_p(ENISR_ALL, e8390_base + EN0_ISR); /* Ack. most intrs. */
520 } else {
521 printk(KERN_WARNING "%s: unknown interrupt %#2x\n", dev->name, interrupts);
522 outb_p(0xff, e8390_base + EN0_ISR); /* Ack. all intrs. */
523 }
524 }
525 spin_unlock(&ei_local->page_lock);
526 return;
527 }
528
529 /**
530 * ei_tx_err - handle transmitter error
531 * @dev: network device which threw the exception
532 *
533 * A transmitter error has happened. Most likely excess collisions (which
534 * is a fairly normal condition). If the error is one where the Tx will
535 * have been aborted, we try and send another one right away, instead of
536 * letting the failed packet sit and collect dust in the Tx buffer. This
537 * is a much better solution as it avoids kernel based Tx timeouts, and
538 * an unnecessary card reset.
539 *
540 * Called with lock held.
541 */
542
ei_tx_err(struct net_device * dev)543 static void ei_tx_err(struct net_device *dev)
544 {
545 long e8390_base = dev->base_addr;
546 struct ei_device *ei_local = (struct ei_device *) dev->priv;
547 unsigned char txsr = inb_p(e8390_base+EN0_TSR);
548 unsigned char tx_was_aborted = txsr & (ENTSR_ABT+ENTSR_FU);
549
550 #ifdef VERBOSE_ERROR_DUMP
551 printk(KERN_DEBUG "%s: transmitter error (%#2x): ", dev->name, txsr);
552 if (txsr & ENTSR_ABT)
553 printk("excess-collisions ");
554 if (txsr & ENTSR_ND)
555 printk("non-deferral ");
556 if (txsr & ENTSR_CRS)
557 printk("lost-carrier ");
558 if (txsr & ENTSR_FU)
559 printk("FIFO-underrun ");
560 if (txsr & ENTSR_CDH)
561 printk("lost-heartbeat ");
562 printk("\n");
563 #endif
564
565 outb_p(ENISR_TX_ERR, e8390_base + EN0_ISR); /* Ack intr. */
566
567 if (tx_was_aborted)
568 ei_tx_intr(dev);
569 else
570 {
571 ei_local->stat.tx_errors++;
572 if (txsr & ENTSR_CRS) ei_local->stat.tx_carrier_errors++;
573 if (txsr & ENTSR_CDH) ei_local->stat.tx_heartbeat_errors++;
574 if (txsr & ENTSR_OWC) ei_local->stat.tx_window_errors++;
575 }
576 }
577
578 /**
579 * ei_tx_intr - transmit interrupt handler
580 * @dev: network device for which tx intr is handled
581 *
582 * We have finished a transmit: check for errors and then trigger the next
583 * packet to be sent. Called with lock held.
584 */
585
ei_tx_intr(struct net_device * dev)586 static void ei_tx_intr(struct net_device *dev)
587 {
588 long e8390_base = dev->base_addr;
589 struct ei_device *ei_local = (struct ei_device *) dev->priv;
590 int status = inb(e8390_base + EN0_TSR);
591
592 outb_p(ENISR_TX, e8390_base + EN0_ISR); /* Ack intr. */
593
594 #ifdef EI_PINGPONG
595
596 /*
597 * There are two Tx buffers, see which one finished, and trigger
598 * the send of another one if it exists.
599 */
600 ei_local->txqueue--;
601
602 if (ei_local->tx1 < 0)
603 {
604 if (ei_local->lasttx != 1 && ei_local->lasttx != -1)
605 printk(KERN_ERR "%s: bogus last_tx_buffer %d, tx1=%d.\n",
606 ei_local->name, ei_local->lasttx, ei_local->tx1);
607 ei_local->tx1 = 0;
608 if (ei_local->tx2 > 0)
609 {
610 ei_local->txing = 1;
611 NS8390_trigger_send(dev, ei_local->tx2, ei_local->tx_start_page + 6);
612 dev->trans_start = jiffies;
613 ei_local->tx2 = -1,
614 ei_local->lasttx = 2;
615 }
616 else ei_local->lasttx = 20, ei_local->txing = 0;
617 }
618 else if (ei_local->tx2 < 0)
619 {
620 if (ei_local->lasttx != 2 && ei_local->lasttx != -2)
621 printk("%s: bogus last_tx_buffer %d, tx2=%d.\n",
622 ei_local->name, ei_local->lasttx, ei_local->tx2);
623 ei_local->tx2 = 0;
624 if (ei_local->tx1 > 0)
625 {
626 ei_local->txing = 1;
627 NS8390_trigger_send(dev, ei_local->tx1, ei_local->tx_start_page);
628 dev->trans_start = jiffies;
629 ei_local->tx1 = -1;
630 ei_local->lasttx = 1;
631 }
632 else
633 ei_local->lasttx = 10, ei_local->txing = 0;
634 }
635 // else printk(KERN_WARNING "%s: unexpected TX-done interrupt, lasttx=%d.\n",
636 // dev->name, ei_local->lasttx);
637
638 #else /* EI_PINGPONG */
639 /*
640 * Single Tx buffer: mark it free so another packet can be loaded.
641 */
642 ei_local->txing = 0;
643 #endif
644
645 /* Minimize Tx latency: update the statistics after we restart TXing. */
646 if (status & ENTSR_COL)
647 ei_local->stat.collisions++;
648 if (status & ENTSR_PTX)
649 ei_local->stat.tx_packets++;
650 else
651 {
652 ei_local->stat.tx_errors++;
653 if (status & ENTSR_ABT)
654 {
655 ei_local->stat.tx_aborted_errors++;
656 ei_local->stat.collisions += 16;
657 }
658 if (status & ENTSR_CRS)
659 ei_local->stat.tx_carrier_errors++;
660 if (status & ENTSR_FU)
661 ei_local->stat.tx_fifo_errors++;
662 if (status & ENTSR_CDH)
663 ei_local->stat.tx_heartbeat_errors++;
664 if (status & ENTSR_OWC)
665 ei_local->stat.tx_window_errors++;
666 }
667 netif_wake_queue(dev);
668 }
669
670 /**
671 * ei_receive - receive some packets
672 * @dev: network device with which receive will be run
673 *
674 * We have a good packet(s), get it/them out of the buffers.
675 * Called with lock held.
676 */
677
ei_receive(struct net_device * dev)678 static void ei_receive(struct net_device *dev)
679 {
680 long e8390_base = dev->base_addr;
681 struct ei_device *ei_local = (struct ei_device *) dev->priv;
682 unsigned char rxing_page, this_frame, next_frame;
683 unsigned short current_offset;
684 int rx_pkt_count = 0;
685 struct e8390_pkt_hdr rx_frame;
686 int num_rx_pages = ei_local->stop_page-ei_local->rx_start_page;
687
688 while (++rx_pkt_count < 10)
689 {
690 int pkt_len, pkt_stat;
691
692 /* Get the rx page (incoming packet pointer). */
693 outb_p(E8390_NODMA+E8390_PAGE1, e8390_base + E8390_CMD);
694 rxing_page = inb_p(e8390_base + EN1_CURPAG);
695 outb_p(E8390_NODMA+E8390_PAGE0, e8390_base + E8390_CMD);
696
697 /* Remove one frame from the ring. Boundary is always a page behind. */
698 this_frame = inb_p(e8390_base + EN0_BOUNDARY) + 1;
699 if (this_frame >= ei_local->stop_page)
700 this_frame = ei_local->rx_start_page;
701
702 /* Someday we'll omit the previous, iff we never get this message.
703 (There is at least one clone claimed to have a problem.)
704
705 Keep quiet if it looks like a card removal. One problem here
706 is that some clones crash in roughly the same way.
707 */
708 if (ei_debug > 0 && this_frame != ei_local->current_page && (this_frame!=0x0 || rxing_page!=0xFF))
709 printk(KERN_ERR "%s: mismatched read page pointers %2x vs %2x.\n",
710 dev->name, this_frame, ei_local->current_page);
711
712 if (this_frame == rxing_page) /* Read all the frames? */
713 break; /* Done for now */
714
715 current_offset = this_frame << 8;
716 ei_get_8390_hdr(dev, &rx_frame, this_frame);
717
718 pkt_len = rx_frame.count - sizeof(struct e8390_pkt_hdr);
719 pkt_stat = rx_frame.status;
720
721 next_frame = this_frame + 1 + ((pkt_len+4)>>8);
722
723 /* Check for bogosity warned by 3c503 book: the status byte is never
724 written. This happened a lot during testing! This code should be
725 cleaned up someday. */
726 if (rx_frame.next != next_frame
727 && rx_frame.next != next_frame + 1
728 && rx_frame.next != next_frame - num_rx_pages
729 && rx_frame.next != next_frame + 1 - num_rx_pages) {
730 ei_local->current_page = rxing_page;
731 outb(ei_local->current_page-1, e8390_base+EN0_BOUNDARY);
732 ei_local->stat.rx_errors++;
733 continue;
734 }
735
736 if (pkt_len < 60 || pkt_len > 1518)
737 {
738 if (ei_debug)
739 printk(KERN_DEBUG "%s: bogus packet size: %d, status=%#2x nxpg=%#2x.\n",
740 dev->name, rx_frame.count, rx_frame.status,
741 rx_frame.next);
742 ei_local->stat.rx_errors++;
743 ei_local->stat.rx_length_errors++;
744 }
745 else if ((pkt_stat & 0x0F) == ENRSR_RXOK)
746 {
747 struct sk_buff *skb;
748
749 skb = dev_alloc_skb(pkt_len+2);
750 if (skb == NULL)
751 {
752 if (ei_debug > 1)
753 printk(KERN_DEBUG "%s: Couldn't allocate a sk_buff of size %d.\n",
754 dev->name, pkt_len);
755 ei_local->stat.rx_dropped++;
756 break;
757 }
758 else
759 {
760 skb_reserve(skb,2); /* IP headers on 16 byte boundaries */
761 skb->dev = dev;
762 skb_put(skb, pkt_len); /* Make room */
763 ei_block_input(dev, pkt_len, skb, current_offset + sizeof(rx_frame));
764 skb->protocol=eth_type_trans(skb,dev);
765 netif_rx(skb);
766 dev->last_rx = jiffies;
767 ei_local->stat.rx_packets++;
768 ei_local->stat.rx_bytes += pkt_len;
769 if (pkt_stat & ENRSR_PHY)
770 ei_local->stat.multicast++;
771 }
772 }
773 else
774 {
775 if (ei_debug)
776 printk(KERN_DEBUG "%s: bogus packet: status=%#2x nxpg=%#2x size=%d\n",
777 dev->name, rx_frame.status, rx_frame.next,
778 rx_frame.count);
779 ei_local->stat.rx_errors++;
780 /* NB: The NIC counts CRC, frame and missed errors. */
781 if (pkt_stat & ENRSR_FO)
782 ei_local->stat.rx_fifo_errors++;
783 }
784 next_frame = rx_frame.next;
785
786 /* This _should_ never happen: it's here for avoiding bad clones. */
787 if (next_frame >= ei_local->stop_page) {
788 printk("%s: next frame inconsistency, %#2x\n", dev->name,
789 next_frame);
790 next_frame = ei_local->rx_start_page;
791 }
792 ei_local->current_page = next_frame;
793 outb_p(next_frame-1, e8390_base+EN0_BOUNDARY);
794 }
795
796 /* We used to also ack ENISR_OVER here, but that would sometimes mask
797 a real overrun, leaving the 8390 in a stopped state with rec'vr off. */
798 outb_p(ENISR_RX+ENISR_RX_ERR, e8390_base+EN0_ISR);
799 return;
800 }
801
802 /**
803 * ei_rx_overrun - handle receiver overrun
804 * @dev: network device which threw exception
805 *
806 * We have a receiver overrun: we have to kick the 8390 to get it started
807 * again. Problem is that you have to kick it exactly as NS prescribes in
808 * the updated datasheets, or "the NIC may act in an unpredictable manner."
809 * This includes causing "the NIC to defer indefinitely when it is stopped
810 * on a busy network." Ugh.
811 * Called with lock held. Don't call this with the interrupts off or your
812 * computer will hate you - it takes 10ms or so.
813 */
814
ei_rx_overrun(struct net_device * dev)815 static void ei_rx_overrun(struct net_device *dev)
816 {
817 long e8390_base = dev->base_addr;
818 unsigned char was_txing, must_resend = 0;
819 struct ei_device *ei_local = (struct ei_device *) dev->priv;
820
821 /*
822 * Record whether a Tx was in progress and then issue the
823 * stop command.
824 */
825 was_txing = inb_p(e8390_base+E8390_CMD) & E8390_TRANS;
826 outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD);
827
828 if (ei_debug > 1)
829 printk(KERN_DEBUG "%s: Receiver overrun.\n", dev->name);
830 ei_local->stat.rx_over_errors++;
831
832 /*
833 * Wait a full Tx time (1.2ms) + some guard time, NS says 1.6ms total.
834 * Early datasheets said to poll the reset bit, but now they say that
835 * it "is not a reliable indicator and subsequently should be ignored."
836 * We wait at least 10ms.
837 */
838
839 udelay(10*1000);
840
841 /*
842 * Reset RBCR[01] back to zero as per magic incantation.
843 */
844 outb_p(0x00, e8390_base+EN0_RCNTLO);
845 outb_p(0x00, e8390_base+EN0_RCNTHI);
846
847 /*
848 * See if any Tx was interrupted or not. According to NS, this
849 * step is vital, and skipping it will cause no end of havoc.
850 */
851
852 if (was_txing)
853 {
854 unsigned char tx_completed = inb_p(e8390_base+EN0_ISR) & (ENISR_TX+ENISR_TX_ERR);
855 if (!tx_completed)
856 must_resend = 1;
857 }
858
859 /*
860 * Have to enter loopback mode and then restart the NIC before
861 * you are allowed to slurp packets up off the ring.
862 */
863 outb_p(E8390_TXOFF, e8390_base + EN0_TXCR);
864 outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START, e8390_base + E8390_CMD);
865
866 /*
867 * Clear the Rx ring of all the debris, and ack the interrupt.
868 */
869 ei_receive(dev);
870 outb_p(ENISR_OVER, e8390_base+EN0_ISR);
871
872 /*
873 * Leave loopback mode, and resend any packet that got stopped.
874 */
875 outb_p(E8390_TXCONFIG, e8390_base + EN0_TXCR);
876 if (must_resend)
877 outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START + E8390_TRANS, e8390_base + E8390_CMD);
878 }
879
880 /*
881 * Collect the stats. This is called unlocked and from several contexts.
882 */
883
get_stats(struct net_device * dev)884 static struct net_device_stats *get_stats(struct net_device *dev)
885 {
886 long ioaddr = dev->base_addr;
887 struct ei_device *ei_local = (struct ei_device *) dev->priv;
888 unsigned long flags;
889
890 /* If the card is stopped, just return the present stats. */
891 if (!netif_running(dev))
892 return &ei_local->stat;
893
894 spin_lock_irqsave(&ei_local->page_lock,flags);
895 /* Read the counter registers, assuming we are in page 0. */
896 ei_local->stat.rx_frame_errors += inb_p(ioaddr + EN0_COUNTER0);
897 ei_local->stat.rx_crc_errors += inb_p(ioaddr + EN0_COUNTER1);
898 ei_local->stat.rx_missed_errors+= inb_p(ioaddr + EN0_COUNTER2);
899 spin_unlock_irqrestore(&ei_local->page_lock, flags);
900
901 return &ei_local->stat;
902 }
903
904 /*
905 * Form the 64 bit 8390 multicast table from the linked list of addresses
906 * associated with this dev structure.
907 */
908
make_mc_bits(u8 * bits,struct net_device * dev)909 static inline void make_mc_bits(u8 *bits, struct net_device *dev)
910 {
911 struct dev_mc_list *dmi;
912
913 for (dmi=dev->mc_list; dmi; dmi=dmi->next)
914 {
915 u32 crc;
916 if (dmi->dmi_addrlen != ETH_ALEN)
917 {
918 printk(KERN_INFO "%s: invalid multicast address length given.\n", dev->name);
919 continue;
920 }
921 crc = ether_crc(ETH_ALEN, dmi->dmi_addr);
922 /*
923 * The 8390 uses the 6 most significant bits of the
924 * CRC to index the multicast table.
925 */
926 bits[crc>>29] |= (1<<((crc>>26)&7));
927 }
928 }
929
930 /**
931 * do_set_multicast_list - set/clear multicast filter
932 * @dev: net device for which multicast filter is adjusted
933 *
934 * Set or clear the multicast filter for this adaptor. May be called
935 * from a BH in 2.1.x. Must be called with lock held.
936 */
937
do_set_multicast_list(struct net_device * dev)938 static void do_set_multicast_list(struct net_device *dev)
939 {
940 long e8390_base = dev->base_addr;
941 int i;
942 struct ei_device *ei_local = (struct ei_device*)dev->priv;
943
944 if (!(dev->flags&(IFF_PROMISC|IFF_ALLMULTI)))
945 {
946 memset(ei_local->mcfilter, 0, 8);
947 if (dev->mc_list)
948 make_mc_bits(ei_local->mcfilter, dev);
949 }
950 else
951 memset(ei_local->mcfilter, 0xFF, 8); /* mcast set to accept-all */
952
953 /*
954 * DP8390 manuals don't specify any magic sequence for altering
955 * the multicast regs on an already running card. To be safe, we
956 * ensure multicast mode is off prior to loading up the new hash
957 * table. If this proves to be not enough, we can always resort
958 * to stopping the NIC, loading the table and then restarting.
959 *
960 * Bug Alert! The MC regs on the SMC 83C690 (SMC Elite and SMC
961 * Elite16) appear to be write-only. The NS 8390 data sheet lists
962 * them as r/w so this is a bug. The SMC 83C790 (SMC Ultra and
963 * Ultra32 EISA) appears to have this bug fixed.
964 */
965
966 if (netif_running(dev))
967 outb_p(E8390_RXCONFIG, e8390_base + EN0_RXCR);
968 outb_p(E8390_NODMA + E8390_PAGE1, e8390_base + E8390_CMD);
969 for(i = 0; i < 8; i++)
970 {
971 outb_p(ei_local->mcfilter[i], e8390_base + EN1_MULT_SHIFT(i));
972 #ifndef BUG_83C690
973 if(inb_p(e8390_base + EN1_MULT_SHIFT(i))!=ei_local->mcfilter[i])
974 printk(KERN_ERR "Multicast filter read/write mismap %d\n",i);
975 #endif
976 }
977 outb_p(E8390_NODMA + E8390_PAGE0, e8390_base + E8390_CMD);
978
979 if(dev->flags&IFF_PROMISC)
980 outb_p(E8390_RXCONFIG | 0x18, e8390_base + EN0_RXCR);
981 else if(dev->flags&IFF_ALLMULTI || dev->mc_list)
982 outb_p(E8390_RXCONFIG | 0x08, e8390_base + EN0_RXCR);
983 else
984 outb_p(E8390_RXCONFIG, e8390_base + EN0_RXCR);
985 }
986
987 /*
988 * Called without lock held. This is invoked from user context and may
989 * be parallel to just about everything else. Its also fairly quick and
990 * not called too often. Must protect against both bh and irq users
991 */
992
set_multicast_list(struct net_device * dev)993 static void set_multicast_list(struct net_device *dev)
994 {
995 unsigned long flags;
996 struct ei_device *ei_local = (struct ei_device*)dev->priv;
997
998 spin_lock_irqsave(&ei_local->page_lock, flags);
999 do_set_multicast_list(dev);
1000 spin_unlock_irqrestore(&ei_local->page_lock, flags);
1001 }
1002
ei_device_init(struct ei_device * ei_local)1003 static inline void ei_device_init(struct ei_device *ei_local)
1004 {
1005 spin_lock_init(&ei_local->page_lock);
1006 }
1007
1008 /**
1009 * ethdev_init - init rest of 8390 device struct
1010 * @dev: network device structure to init
1011 *
1012 * Initialize the rest of the 8390 device structure. Do NOT __init
1013 * this, as it is used by 8390 based modular drivers too.
1014 */
1015
ethdev_init(struct net_device * dev)1016 int ethdev_init(struct net_device *dev)
1017 {
1018 if (ei_debug > 1)
1019 printk(version);
1020
1021 if (dev->priv == NULL)
1022 {
1023 dev->priv = kmalloc(sizeof(struct ei_device), GFP_KERNEL);
1024 if (dev->priv == NULL)
1025 return -ENOMEM;
1026 memset(dev->priv, 0, sizeof(struct ei_device));
1027 ei_device_init(dev->priv);
1028 }
1029
1030 dev->hard_start_xmit = &ei_start_xmit;
1031 dev->get_stats = get_stats;
1032 dev->set_multicast_list = &set_multicast_list;
1033
1034 ether_setup(dev);
1035
1036 return 0;
1037 }
1038
1039 /* wrapper to make alloc_netdev happy; probably should just cast... */
__ethdev_init(struct net_device * dev)1040 static void __ethdev_init(struct net_device *dev)
1041 {
1042 ethdev_init(dev);
1043 }
1044
1045 /**
1046 * alloc_ei_netdev - alloc_etherdev counterpart for 8390
1047 *
1048 * Allocate 8390-specific net_device.
1049 */
alloc_ei_netdev(void)1050 struct net_device *alloc_ei_netdev(void)
1051 {
1052 struct net_device *dev;
1053
1054 dev = alloc_netdev(sizeof(struct ei_device), "eth%d", __ethdev_init);
1055 if (dev)
1056 ei_device_init(dev->priv);
1057
1058 return dev;
1059 }
1060
1061
1062
1063
1064 /* This page of functions should be 8390 generic */
1065 /* Follow National Semi's recommendations for initializing the "NIC". */
1066
1067 /**
1068 * NS8390_init - initialize 8390 hardware
1069 * @dev: network device to initialize
1070 * @startp: boolean. non-zero value to initiate chip processing
1071 *
1072 * Must be called with lock held.
1073 */
1074
NS8390_init(struct net_device * dev,int startp)1075 void NS8390_init(struct net_device *dev, int startp)
1076 {
1077 long e8390_base = dev->base_addr;
1078 struct ei_device *ei_local = (struct ei_device *) dev->priv;
1079 int i;
1080 int endcfg = ei_local->word16
1081 ? (0x48 | ENDCFG_WTS | (ei_local->bigendian ? ENDCFG_BOS : 0))
1082 : 0x48;
1083
1084 if(sizeof(struct e8390_pkt_hdr)!=4)
1085 panic("8390.c: header struct mispacked\n");
1086 /* Follow National Semi's recommendations for initing the DP83902. */
1087 outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD); /* 0x21 */
1088 outb_p(endcfg, e8390_base + EN0_DCFG); /* 0x48 or 0x49 */
1089 /* Clear the remote byte count registers. */
1090 outb_p(0x00, e8390_base + EN0_RCNTLO);
1091 outb_p(0x00, e8390_base + EN0_RCNTHI);
1092 /* Set to monitor and loopback mode -- this is vital!. */
1093 outb_p(E8390_RXOFF, e8390_base + EN0_RXCR); /* 0x20 */
1094 outb_p(E8390_TXOFF, e8390_base + EN0_TXCR); /* 0x02 */
1095 /* Set the transmit page and receive ring. */
1096 outb_p(ei_local->tx_start_page, e8390_base + EN0_TPSR);
1097 ei_local->tx1 = ei_local->tx2 = 0;
1098 outb_p(ei_local->rx_start_page, e8390_base + EN0_STARTPG);
1099 outb_p(ei_local->stop_page-1, e8390_base + EN0_BOUNDARY); /* 3c503 says 0x3f,NS0x26*/
1100 ei_local->current_page = ei_local->rx_start_page; /* assert boundary+1 */
1101 outb_p(ei_local->stop_page, e8390_base + EN0_STOPPG);
1102 /* Clear the pending interrupts and mask. */
1103 outb_p(0xFF, e8390_base + EN0_ISR);
1104 outb_p(0x00, e8390_base + EN0_IMR);
1105
1106 /* Copy the station address into the DS8390 registers. */
1107
1108 outb_p(E8390_NODMA + E8390_PAGE1 + E8390_STOP, e8390_base+E8390_CMD); /* 0x61 */
1109 for(i = 0; i < 6; i++)
1110 {
1111 outb_p(dev->dev_addr[i], e8390_base + EN1_PHYS_SHIFT(i));
1112 if (ei_debug > 1 && inb_p(e8390_base + EN1_PHYS_SHIFT(i))!=dev->dev_addr[i])
1113 printk(KERN_ERR "Hw. address read/write mismap %d\n",i);
1114 }
1115
1116 outb_p(ei_local->rx_start_page, e8390_base + EN1_CURPAG);
1117 outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD);
1118
1119 netif_start_queue(dev);
1120 ei_local->tx1 = ei_local->tx2 = 0;
1121 ei_local->txing = 0;
1122
1123 if (startp)
1124 {
1125 outb_p(0xff, e8390_base + EN0_ISR);
1126 outb_p(ENISR_ALL, e8390_base + EN0_IMR);
1127 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base+E8390_CMD);
1128 outb_p(E8390_TXCONFIG, e8390_base + EN0_TXCR); /* xmit on. */
1129 /* 3c503 TechMan says rxconfig only after the NIC is started. */
1130 outb_p(E8390_RXCONFIG, e8390_base + EN0_RXCR); /* rx on, */
1131 do_set_multicast_list(dev); /* (re)load the mcast table */
1132 }
1133 }
1134
1135 /* Trigger a transmit start, assuming the length is valid.
1136 Always called with the page lock held */
1137
NS8390_trigger_send(struct net_device * dev,unsigned int length,int start_page)1138 static void NS8390_trigger_send(struct net_device *dev, unsigned int length,
1139 int start_page)
1140 {
1141 long e8390_base = dev->base_addr;
1142 struct ei_device *ei_local __attribute((unused)) = (struct ei_device *) dev->priv;
1143
1144 outb_p(E8390_NODMA+E8390_PAGE0, e8390_base+E8390_CMD);
1145
1146 if (inb_p(e8390_base+E8390_CMD) & E8390_TRANS)
1147 {
1148 printk(KERN_WARNING "%s: trigger_send() called with the transmitter busy.\n",
1149 dev->name);
1150 return;
1151 }
1152 outb_p(length & 0xff, e8390_base + EN0_TCNTLO);
1153 outb_p(length >> 8, e8390_base + EN0_TCNTHI);
1154 outb_p(start_page, e8390_base + EN0_TPSR);
1155 outb_p(E8390_NODMA+E8390_TRANS+E8390_START, e8390_base+E8390_CMD);
1156 }
1157
1158 EXPORT_SYMBOL(ei_open);
1159 EXPORT_SYMBOL(ei_close);
1160 EXPORT_SYMBOL(ei_interrupt);
1161 EXPORT_SYMBOL(ei_tx_timeout);
1162 EXPORT_SYMBOL(ethdev_init);
1163 EXPORT_SYMBOL(NS8390_init);
1164 EXPORT_SYMBOL(alloc_ei_netdev);
1165
1166 #if defined(MODULE)
1167
init_module(void)1168 int init_module(void)
1169 {
1170 return 0;
1171 }
1172
cleanup_module(void)1173 void cleanup_module(void)
1174 {
1175 }
1176
1177 #endif /* MODULE */
1178 MODULE_LICENSE("GPL");
1179
1180
1181 /*
1182 * Local variables:
1183 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c 8390.c"
1184 * version-control: t
1185 * kept-new-versions: 5
1186 * c-indent-level: 4
1187 * tab-width: 4
1188 * End:
1189 */
1190