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