1 /*
2 * 7990.c -- LANCE ethernet IC generic routines.
3 * This is an attempt to separate out the bits of various ethernet
4 * drivers that are common because they all use the AMD 7990 LANCE
5 * (Local Area Network Controller for Ethernet) chip.
6 *
7 * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
8 *
9 * Most of this stuff was obtained by looking at other LANCE drivers,
10 * in particular a2065.[ch]. The AMD C-LANCE datasheet was also helpful.
11 * NB: this was made easy by the fact that Jes Sorensen had cleaned up
12 * most of a2025 and sunlance with the aim of merging them, so the
13 * common code was pretty obvious.
14 */
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/types.h>
19 #include <linux/fcntl.h>
20 #include <linux/interrupt.h>
21 #include <linux/ptrace.h>
22 #include <linux/ioport.h>
23 #include <linux/in.h>
24 #include <linux/malloc.h>
25 #include <linux/string.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <asm/system.h>
29 #include <asm/bitops.h>
30 #include <asm/io.h>
31 #include <asm/dma.h>
32 #include <asm/pgtable.h>
33 #include <linux/errno.h>
34
35 /* Used for the temporal inet entries and routing */
36 #include <linux/socket.h>
37 #include <linux/route.h>
38
39 #include <linux/dio.h>
40
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44
45 #include "7990.h"
46
47 /* Lossage Factor Nine, Mr Sulu. */
48 #define WRITERAP(x) (lp->writerap(lp,x))
49 #define WRITERDP(x) (lp->writerdp(lp,x))
50 #define READRDP() (lp->readrdp(lp))
51 /* These used to be ll->rap = x, ll->rdp = x, and (ll->rdp). Sigh.
52 * If you want to switch them back then
53 * #define DECLARE_LL volatile struct lance_regs *ll = lp->ll
54 */
55 #define DECLARE_LL /* nothing to declare */
56
57 /* debugging output macros, various flavours */
58 /* #define TEST_HITS */
59 #ifdef UNDEF
60 #define PRINT_RINGS() \
61 do { \
62 int t; \
63 for (t=0; t < RX_RING_SIZE; t++) { \
64 printk("R%d: @(%02X %04X) len %04X, mblen %04X, bits %02X\n",\
65 t, ib->brx_ring[t].rmd1_hadr, ib->brx_ring[t].rmd0,\
66 ib->brx_ring[t].length,\
67 ib->brx_ring[t].mblength, ib->brx_ring[t].rmd1_bits);\
68 }\
69 for (t=0; t < TX_RING_SIZE; t++) { \
70 printk("T%d: @(%02X %04X) len %04X, misc %04X, bits %02X\n",\
71 t, ib->btx_ring[t].tmd1_hadr, ib->btx_ring[t].tmd0,\
72 ib->btx_ring[t].length,\
73 ib->btx_ring[t].misc, ib->btx_ring[t].tmd1_bits);\
74 }\
75 } while (0)
76 #else
77 #define PRINT_RINGS()
78 #endif
79
80 /* Load the CSR registers. The LANCE has to be STOPped when we do this! */
81 static void load_csrs (struct lance_private *lp)
82 {
83 volatile struct lance_init_block *aib = lp->lance_init_block;
84 int leptr;
85 DECLARE_LL;
86
87 leptr = LANCE_ADDR (aib);
88
89 WRITERAP(LE_CSR1); /* load address of init block */
90 WRITERDP(leptr & 0xFFFF);
91 WRITERAP(LE_CSR2);
92 WRITERDP(leptr >> 16);
93 WRITERAP(LE_CSR3);
94 WRITERDP(lp->busmaster_regval); /* set byteswap/ALEctrl/byte ctrl */
95
96 /* Point back to csr0 */
97 WRITERAP(LE_CSR0);
98 }
99
100 /* #define to 0 or 1 appropriately */
101 #define DEBUG_IRING 0
102 /* Set up the Lance Rx and Tx rings and the init block */
103 static void lance_init_ring (struct net_device *dev)
104 {
105 struct lance_private *lp = (struct lance_private *) dev->priv;
106 volatile struct lance_init_block *ib = lp->init_block;
107 volatile struct lance_init_block *aib; /* for LANCE_ADDR computations */
108 int leptr;
109 int i;
110
111 aib = lp->lance_init_block;
112
113 lp->rx_new = lp->tx_new = 0;
114 lp->rx_old = lp->tx_old = 0;
115
116 ib->mode = LE_MO_PROM; /* normal, enable Tx & Rx */
117
118 /* Copy the ethernet address to the lance init block
119 * Notice that we do a byteswap if we're big endian.
120 * [I think this is the right criterion; at least, sunlance,
121 * a2065 and atarilance do the byteswap and lance.c (PC) doesn't.
122 * However, the datasheet says that the BSWAP bit doesn't affect
123 * the init block, so surely it should be low byte first for
124 * everybody? Um.]
125 * We could define the ib->physaddr as three 16bit values and
126 * use (addr[1] << 8) | addr[0] & co, but this is more efficient.
127 */
128 #ifdef __BIG_ENDIAN
129 ib->phys_addr [0] = dev->dev_addr [1];
130 ib->phys_addr [1] = dev->dev_addr [0];
131 ib->phys_addr [2] = dev->dev_addr [3];
132 ib->phys_addr [3] = dev->dev_addr [2];
133 ib->phys_addr [4] = dev->dev_addr [5];
134 ib->phys_addr [5] = dev->dev_addr [4];
135 #else
136 for (i=0; i<6; i++)
137 ib->phys_addr[i] = dev->dev_addr[i];
138 #endif
139
140 if (DEBUG_IRING)
141 printk ("TX rings:\n");
142
143 lp->tx_full = 0;
144 /* Setup the Tx ring entries */
145 for (i = 0; i < (1<<lp->lance_log_tx_bufs); i++) {
146 leptr = LANCE_ADDR(&aib->tx_buf[i][0]);
147 ib->btx_ring [i].tmd0 = leptr;
148 ib->btx_ring [i].tmd1_hadr = leptr >> 16;
149 ib->btx_ring [i].tmd1_bits = 0;
150 ib->btx_ring [i].length = 0xf000; /* The ones required by tmd2 */
151 ib->btx_ring [i].misc = 0;
152 if (DEBUG_IRING)
153 printk ("%d: 0x%8.8x\n", i, leptr);
154 }
155
156 /* Setup the Rx ring entries */
157 if (DEBUG_IRING)
158 printk ("RX rings:\n");
159 for (i = 0; i < (1<<lp->lance_log_rx_bufs); i++) {
160 leptr = LANCE_ADDR(&aib->rx_buf[i][0]);
161
162 ib->brx_ring [i].rmd0 = leptr;
163 ib->brx_ring [i].rmd1_hadr = leptr >> 16;
164 ib->brx_ring [i].rmd1_bits = LE_R1_OWN;
165 /* 0xf000 == bits that must be one (reserved, presumably) */
166 ib->brx_ring [i].length = -RX_BUFF_SIZE | 0xf000;
167 ib->brx_ring [i].mblength = 0;
168 if (DEBUG_IRING)
169 printk ("%d: 0x%8.8x\n", i, leptr);
170 }
171
172 /* Setup the initialization block */
173
174 /* Setup rx descriptor pointer */
175 leptr = LANCE_ADDR(&aib->brx_ring);
176 ib->rx_len = (lp->lance_log_rx_bufs << 13) | (leptr >> 16);
177 ib->rx_ptr = leptr;
178 if (DEBUG_IRING)
179 printk ("RX ptr: %8.8x\n", leptr);
180
181 /* Setup tx descriptor pointer */
182 leptr = LANCE_ADDR(&aib->btx_ring);
183 ib->tx_len = (lp->lance_log_tx_bufs << 13) | (leptr >> 16);
184 ib->tx_ptr = leptr;
185 if (DEBUG_IRING)
186 printk ("TX ptr: %8.8x\n", leptr);
187
188 /* Clear the multicast filter */
189 ib->filter [0] = 0;
190 ib->filter [1] = 0;
191 PRINT_RINGS();
192 }
193
194 /* LANCE must be STOPped before we do this, too... */
195 static int init_restart_lance (struct lance_private *lp)
196 {
197 int i;
198 DECLARE_LL;
199
200 WRITERAP(LE_CSR0);
201 WRITERDP(LE_C0_INIT);
202
203 /* Need a hook here for sunlance ledma stuff */
204
205 /* Wait for the lance to complete initialization */
206 for (i = 0; (i < 100) && !(READRDP() & (LE_C0_ERR | LE_C0_IDON)); i++)
207 barrier();
208 if ((i == 100) || (READRDP() & LE_C0_ERR)) {
209 printk ("LANCE unopened after %d ticks, csr0=%4.4x.\n", i, READRDP());
210 return -1;
211 }
212
213 /* Clear IDON by writing a "1", enable interrupts and start lance */
214 WRITERDP(LE_C0_IDON);
215 WRITERDP(LE_C0_INEA | LE_C0_STRT);
216
217 return 0;
218 }
219
220 static int lance_reset (struct net_device *dev)
221 {
222 struct lance_private *lp = (struct lance_private *)dev->priv;
223 int status;
224 DECLARE_LL;
225
226 /* Stop the lance */
227 WRITERAP(LE_CSR0);
228 WRITERDP(LE_C0_STOP);
229
230 load_csrs (lp);
231 lance_init_ring (dev);
232 dev->trans_start = jiffies;
233 status = init_restart_lance (lp);
234 #ifdef DEBUG_DRIVER
235 printk ("Lance restart=%d\n", status);
236 #endif
237 return status;
238 }
239
240 static int lance_rx (struct net_device *dev)
241 {
242 struct lance_private *lp = (struct lance_private *) dev->priv;
243 volatile struct lance_init_block *ib = lp->init_block;
244 volatile struct lance_rx_desc *rd;
245 unsigned char bits;
246 int len = 0; /* XXX shut up gcc warnings */
247 struct sk_buff *skb = 0; /* XXX shut up gcc warnings */
248 #ifdef TEST_HITS
249 int i;
250 #endif
251 DECLARE_LL;
252
253 #ifdef TEST_HITS
254 printk ("[");
255 for (i = 0; i < RX_RING_SIZE; i++) {
256 if (i == lp->rx_new)
257 printk ("%s",
258 ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "_" : "X");
259 else
260 printk ("%s",
261 ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "." : "1");
262 }
263 printk ("]");
264 #endif
265
266 WRITERDP(LE_C0_RINT | LE_C0_INEA); /* ack Rx int, reenable ints */
267 for (rd = &ib->brx_ring [lp->rx_new]; /* For each Rx ring we own... */
268 !((bits = rd->rmd1_bits) & LE_R1_OWN);
269 rd = &ib->brx_ring [lp->rx_new]) {
270
271 /* We got an incomplete frame? */
272 if ((bits & LE_R1_POK) != LE_R1_POK) {
273 lp->stats.rx_over_errors++;
274 lp->stats.rx_errors++;
275 continue;
276 } else if (bits & LE_R1_ERR) {
277 /* Count only the end frame as a rx error,
278 * not the beginning
279 */
280 if (bits & LE_R1_BUF) lp->stats.rx_fifo_errors++;
281 if (bits & LE_R1_CRC) lp->stats.rx_crc_errors++;
282 if (bits & LE_R1_OFL) lp->stats.rx_over_errors++;
283 if (bits & LE_R1_FRA) lp->stats.rx_frame_errors++;
284 if (bits & LE_R1_EOP) lp->stats.rx_errors++;
285 } else {
286 len = (rd->mblength & 0xfff) - 4;
287 skb = dev_alloc_skb (len+2);
288
289 if (skb == 0) {
290 printk ("%s: Memory squeeze, deferring packet.\n",
291 dev->name);
292 lp->stats.rx_dropped++;
293 rd->mblength = 0;
294 rd->rmd1_bits = LE_R1_OWN;
295 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
296 return 0;
297 }
298
299 skb->dev = dev;
300 skb_reserve (skb, 2); /* 16 byte align */
301 skb_put (skb, len); /* make room */
302 eth_copy_and_sum(skb,
303 (unsigned char *)&(ib->rx_buf [lp->rx_new][0]),
304 len, 0);
305 skb->protocol = eth_type_trans (skb, dev);
306 netif_rx (skb);
307 lp->stats.rx_packets++;
308 }
309
310 /* Return the packet to the pool */
311 rd->mblength = 0;
312 rd->rmd1_bits = LE_R1_OWN;
313 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
314 }
315 return 0;
316 }
317
318 static int lance_tx (struct net_device *dev)
319 {
320 struct lance_private *lp = (struct lance_private *) dev->priv;
321 volatile struct lance_init_block *ib = lp->init_block;
322 volatile struct lance_tx_desc *td;
323 int i, j;
324 int status;
325 DECLARE_LL;
326
327 /* csr0 is 2f3 */
328 WRITERDP(LE_C0_TINT | LE_C0_INEA);
329 /* csr0 is 73 */
330
331 j = lp->tx_old;
332 for (i = j; i != lp->tx_new; i = j) {
333 td = &ib->btx_ring [i];
334
335 /* If we hit a packet not owned by us, stop */
336 if (td->tmd1_bits & LE_T1_OWN)
337 break;
338
339 if (td->tmd1_bits & LE_T1_ERR) {
340 status = td->misc;
341
342 lp->stats.tx_errors++;
343 if (status & LE_T3_RTY) lp->stats.tx_aborted_errors++;
344 if (status & LE_T3_LCOL) lp->stats.tx_window_errors++;
345
346 if (status & LE_T3_CLOS) {
347 lp->stats.tx_carrier_errors++;
348 if (lp->auto_select) {
349 lp->tpe = 1 - lp->tpe;
350 printk("%s: Carrier Lost, trying %s\n",
351 dev->name, lp->tpe?"TPE":"AUI");
352 /* Stop the lance */
353 WRITERAP(LE_CSR0);
354 WRITERDP(LE_C0_STOP);
355 lance_init_ring (dev);
356 load_csrs (lp);
357 init_restart_lance (lp);
358 return 0;
359 }
360 }
361
362 /* buffer errors and underflows turn off the transmitter */
363 /* Restart the adapter */
364 if (status & (LE_T3_BUF|LE_T3_UFL)) {
365 lp->stats.tx_fifo_errors++;
366
367 printk ("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
368 dev->name);
369 /* Stop the lance */
370 WRITERAP(LE_CSR0);
371 WRITERDP(LE_C0_STOP);
372 lance_init_ring (dev);
373 load_csrs (lp);
374 init_restart_lance (lp);
375 return 0;
376 }
377 } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
378 /*
379 * So we don't count the packet more than once.
380 */
381 td->tmd1_bits &= ~(LE_T1_POK);
382
383 /* One collision before packet was sent. */
384 if (td->tmd1_bits & LE_T1_EONE)
385 lp->stats.collisions++;
386
387 /* More than one collision, be optimistic. */
388 if (td->tmd1_bits & LE_T1_EMORE)
389 lp->stats.collisions += 2;
390
391 lp->stats.tx_packets++;
392 }
393
394 j = (j + 1) & lp->tx_ring_mod_mask;
395 }
396 lp->tx_old = j;
397 WRITERDP(LE_C0_TINT | LE_C0_INEA);
398 return 0;
399 }
400
401 static void lance_interrupt (int irq, void *dev_id, struct pt_regs *regs)
402 {
403 struct net_device *dev = (struct net_device *)dev_id;
404 struct lance_private *lp = (struct lance_private *)dev->priv;
405 int csr0;
406 DECLARE_LL;
407
408 spin_lock (&lp->devlock);
409
410 WRITERAP(LE_CSR0); /* LANCE Controller Status */
411 csr0 = READRDP();
412
413 PRINT_RINGS();
414
415 if (!(csr0 & LE_C0_INTR)) { /* Check if any interrupt has */
416 spin_lock (&lp->devlock);
417 return; /* been generated by the Lance. */
418 }
419
420 /* Acknowledge all the interrupt sources ASAP */
421 WRITERDP(csr0 & ~(LE_C0_INEA|LE_C0_TDMD|LE_C0_STOP|LE_C0_STRT|LE_C0_INIT));
422
423 if ((csr0 & LE_C0_ERR)) {
424 /* Clear the error condition */
425 WRITERDP(LE_C0_BABL|LE_C0_ERR|LE_C0_MISS|LE_C0_INEA);
426 }
427
428 if (csr0 & LE_C0_RINT)
429 lance_rx (dev);
430
431 if (csr0 & LE_C0_TINT)
432 lance_tx (dev);
433
434 /* Log misc errors. */
435 if (csr0 & LE_C0_BABL)
436 lp->stats.tx_errors++; /* Tx babble. */
437 if (csr0 & LE_C0_MISS)
438 lp->stats.rx_errors++; /* Missed a Rx frame. */
439 if (csr0 & LE_C0_MERR) {
440 printk("%s: Bus master arbitration failure, status %4.4x.\n",
441 dev->name, csr0);
442 /* Restart the chip. */
443 WRITERDP(LE_C0_STRT);
444 }
445
446 if (lp->tx_full && netif_queue_stopped(dev) && (TX_BUFFS_AVAIL >= 0)) {
447 lp->tx_full = 0;
448 netif_wake_queue (dev);
449 }
450
451 WRITERAP(LE_CSR0);
452 WRITERDP(LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_MERR|LE_C0_IDON|LE_C0_INEA);
453
454 spin_unlock (&lp->devlock);
455 }
456
457 int lance_open (struct net_device *dev)
458 {
459 struct lance_private *lp = (struct lance_private *)dev->priv;
460 int res;
461 DECLARE_LL;
462
463 /* Install the Interrupt handler. Or we could shunt this out to specific drivers? */
464 if (request_irq(lp->irq, lance_interrupt, 0, lp->name, dev))
465 return -EAGAIN;
466
467 res = lance_reset(dev);
468 lp->devlock = SPIN_LOCK_UNLOCKED;
469 netif_start_queue (dev);
470
471 return res;
472 }
473
474 int lance_close (struct net_device *dev)
475 {
476 struct lance_private *lp = (struct lance_private *) dev->priv;
477 DECLARE_LL;
478
479 netif_stop_queue (dev);
480
481 /* Stop the LANCE */
482 WRITERAP(LE_CSR0);
483 WRITERDP(LE_C0_STOP);
484
485 free_irq(lp->irq, dev);
486
487 return 0;
488 }
489
490 void lance_tx_timeout(struct net_device *dev)
491 {
492 printk("lance_tx_timeout\n");
493 lance_reset(dev);
494 dev->trans_start = jiffies;
495 netif_start_queue (dev);
496 }
497
498
499 int lance_start_xmit (struct sk_buff *skb, struct net_device *dev)
500 {
501 struct lance_private *lp = (struct lance_private *)dev->priv;
502 volatile struct lance_init_block *ib = lp->init_block;
503 int entry, skblen, len;
504 static int outs;
505 unsigned long flags;
506 DECLARE_LL;
507
508 if (!TX_BUFFS_AVAIL)
509 return -1;
510
511 netif_stop_queue (dev);
512
513 skblen = skb->len;
514
515 #ifdef DEBUG_DRIVER
516 /* dump the packet */
517 {
518 int i;
519
520 for (i = 0; i < 64; i++) {
521 if ((i % 16) == 0)
522 printk ("\n");
523 printk ("%2.2x ", skb->data [i]);
524 }
525 }
526 #endif
527 len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
528 entry = lp->tx_new & lp->tx_ring_mod_mask;
529 ib->btx_ring [entry].length = (-len) | 0xf000;
530 ib->btx_ring [entry].misc = 0;
531
532 memcpy ((char *)&ib->tx_buf [entry][0], skb->data, skblen);
533
534 /* Now, give the packet to the lance */
535 ib->btx_ring [entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN);
536 lp->tx_new = (lp->tx_new+1) & lp->tx_ring_mod_mask;
537
538 outs++;
539 /* Kick the lance: transmit now */
540 WRITERDP(LE_C0_INEA | LE_C0_TDMD);
541 dev->trans_start = jiffies;
542 dev_kfree_skb (skb);
543
544 spin_lock_irqsave (&lp->devlock, flags);
545 if (TX_BUFFS_AVAIL)
546 netif_start_queue (dev);
547 else
548 lp->tx_full = 1;
549 spin_unlock_irqrestore (&lp->devlock, flags);
550
551 return 0;
552 }
553
554 struct net_device_stats *lance_get_stats (struct net_device *dev)
555 {
556 struct lance_private *lp = (struct lance_private *) dev->priv;
557
558 return &lp->stats;
559 }
560
561 /* taken from the depca driver via a2065.c */
562 static void lance_load_multicast (struct net_device *dev)
563 {
564 struct lance_private *lp = (struct lance_private *) dev->priv;
565 volatile struct lance_init_block *ib = lp->init_block;
566 volatile u16 *mcast_table = (u16 *)&ib->filter;
567 struct dev_mc_list *dmi=dev->mc_list;
568 char *addrs;
569 int i, j, bit, byte;
570 u32 crc, poly = CRC_POLYNOMIAL_LE;
571
572 /* set all multicast bits */
573 if (dev->flags & IFF_ALLMULTI){
574 ib->filter [0] = 0xffffffff;
575 ib->filter [1] = 0xffffffff;
576 return;
577 }
578 /* clear the multicast filter */
579 ib->filter [0] = 0;
580 ib->filter [1] = 0;
581
582 /* Add addresses */
583 for (i = 0; i < dev->mc_count; i++){
584 addrs = dmi->dmi_addr;
585 dmi = dmi->next;
586
587 /* multicast address? */
588 if (!(*addrs & 1))
589 continue;
590
591 crc = 0xffffffff;
592 for (byte = 0; byte < 6; byte++)
593 for (bit = *addrs++, j = 0; j < 8; j++, bit>>=1)
594 {
595 int test;
596
597 test = ((bit ^ crc) & 0x01);
598 crc >>= 1;
599
600 if (test)
601 {
602 crc = crc ^ poly;
603 }
604 }
605
606 crc = crc >> 26;
607 mcast_table [crc >> 4] |= 1 << (crc & 0xf);
608 }
609 return;
610 }
611
612
613 void lance_set_multicast (struct net_device *dev)
614 {
615 struct lance_private *lp = (struct lance_private *) dev->priv;
616 volatile struct lance_init_block *ib = lp->init_block;
617 int stopped;
618 DECLARE_LL;
619
620 stopped = netif_queue_stopped(dev);
621 if (!stopped)
622 netif_stop_queue (dev);
623
624 while (lp->tx_old != lp->tx_new)
625 schedule();
626
627 WRITERAP(LE_CSR0);
628 WRITERDP(LE_C0_STOP);
629 lance_init_ring (dev);
630
631 if (dev->flags & IFF_PROMISC) {
632 ib->mode |= LE_MO_PROM;
633 } else {
634 ib->mode &= ~LE_MO_PROM;
635 lance_load_multicast (dev);
636 }
637 load_csrs (lp);
638 init_restart_lance (lp);
639
640 if (!stopped)
641 netif_start_queue (dev);
642 }
643
644
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.