1
2
3
4
5
6
7
8
9
10
11
12
13
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/ioport.h>
22#include <linux/in.h>
23#include <linux/slab.h>
24#include <linux/string.h>
25#include <linux/delay.h>
26#include <linux/init.h>
27#include <linux/crc32.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
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
48#define WRITERAP(x) (lp->writerap(lp,x))
49#define WRITERDP(x) (lp->writerdp(lp,x))
50#define READRDP() (lp->readrdp(lp))
51
52
53
54
55#define DECLARE_LL
56
57
58
59#ifdef UNDEF
60#define PRINT_RINGS() \
61do { \
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
81static 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);
90 WRITERDP(leptr & 0xFFFF);
91 WRITERAP(LE_CSR2);
92 WRITERDP(leptr >> 16);
93 WRITERAP(LE_CSR3);
94 WRITERDP(lp->busmaster_regval);
95
96
97 WRITERAP(LE_CSR0);
98}
99
100
101#define DEBUG_IRING 0
102
103static 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;
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;
117
118
119
120
121
122
123
124
125
126
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
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;
151 ib->btx_ring [i].misc = 0;
152 if (DEBUG_IRING)
153 printk ("%d: 0x%8.8x\n", i, leptr);
154 }
155
156
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
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
173
174
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
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
189 ib->filter [0] = 0;
190 ib->filter [1] = 0;
191 PRINT_RINGS();
192}
193
194
195static 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
204
205
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
214 WRITERDP(LE_C0_IDON);
215 WRITERDP(LE_C0_INEA | LE_C0_STRT);
216
217 return 0;
218}
219
220static 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
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
240static 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;
247 struct sk_buff *skb = 0;
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);
267 for (rd = &ib->brx_ring [lp->rx_new];
268 !((bits = rd->rmd1_bits) & LE_R1_OWN);
269 rd = &ib->brx_ring [lp->rx_new]) {
270
271
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
278
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);
301 skb_put (skb, len);
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 dev->last_rx = jiffies;
308 lp->stats.rx_packets++;
309 lp->stats.rx_bytes += len;
310 }
311
312
313 rd->mblength = 0;
314 rd->rmd1_bits = LE_R1_OWN;
315 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
316 }
317 return 0;
318}
319
320static int lance_tx (struct net_device *dev)
321{
322 struct lance_private *lp = (struct lance_private *) dev->priv;
323 volatile struct lance_init_block *ib = lp->init_block;
324 volatile struct lance_tx_desc *td;
325 int i, j;
326 int status;
327 DECLARE_LL;
328
329
330 WRITERDP(LE_C0_TINT | LE_C0_INEA);
331
332
333 j = lp->tx_old;
334 for (i = j; i != lp->tx_new; i = j) {
335 td = &ib->btx_ring [i];
336
337
338 if (td->tmd1_bits & LE_T1_OWN)
339 break;
340
341 if (td->tmd1_bits & LE_T1_ERR) {
342 status = td->misc;
343
344 lp->stats.tx_errors++;
345 if (status & LE_T3_RTY) lp->stats.tx_aborted_errors++;
346 if (status & LE_T3_LCOL) lp->stats.tx_window_errors++;
347
348 if (status & LE_T3_CLOS) {
349 lp->stats.tx_carrier_errors++;
350 if (lp->auto_select) {
351 lp->tpe = 1 - lp->tpe;
352 printk("%s: Carrier Lost, trying %s\n",
353 dev->name, lp->tpe?"TPE":"AUI");
354
355 WRITERAP(LE_CSR0);
356 WRITERDP(LE_C0_STOP);
357 lance_init_ring (dev);
358 load_csrs (lp);
359 init_restart_lance (lp);
360 return 0;
361 }
362 }
363
364
365
366 if (status & (LE_T3_BUF|LE_T3_UFL)) {
367 lp->stats.tx_fifo_errors++;
368
369 printk ("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
370 dev->name);
371
372 WRITERAP(LE_CSR0);
373 WRITERDP(LE_C0_STOP);
374 lance_init_ring (dev);
375 load_csrs (lp);
376 init_restart_lance (lp);
377 return 0;
378 }
379 } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
380
381
382
383 td->tmd1_bits &= ~(LE_T1_POK);
384
385
386 if (td->tmd1_bits & LE_T1_EONE)
387 lp->stats.collisions++;
388
389
390 if (td->tmd1_bits & LE_T1_EMORE)
391 lp->stats.collisions += 2;
392
393 lp->stats.tx_packets++;
394 }
395
396 j = (j + 1) & lp->tx_ring_mod_mask;
397 }
398 lp->tx_old = j;
399 WRITERDP(LE_C0_TINT | LE_C0_INEA);
400 return 0;
401}
402
403static void lance_interrupt (int irq, void *dev_id, struct pt_regs *regs)
404{
405 struct net_device *dev = (struct net_device *)dev_id;
406 struct lance_private *lp = (struct lance_private *)dev->priv;
407 int csr0;
408 DECLARE_LL;
409
410 spin_lock (&lp->devlock);
411
412 WRITERAP(LE_CSR0);
413 csr0 = READRDP();
414
415 PRINT_RINGS();
416
417 if (!(csr0 & LE_C0_INTR)) {
418 spin_lock (&lp->devlock);
419 return;
420 }
421
422
423 WRITERDP(csr0 & ~(LE_C0_INEA|LE_C0_TDMD|LE_C0_STOP|LE_C0_STRT|LE_C0_INIT));
424
425 if ((csr0 & LE_C0_ERR)) {
426
427 WRITERDP(LE_C0_BABL|LE_C0_ERR|LE_C0_MISS|LE_C0_INEA);
428 }
429
430 if (csr0 & LE_C0_RINT)
431 lance_rx (dev);
432
433 if (csr0 & LE_C0_TINT)
434 lance_tx (dev);
435
436
437 if (csr0 & LE_C0_BABL)
438 lp->stats.tx_errors++;
439 if (csr0 & LE_C0_MISS)
440 lp->stats.rx_errors++;
441 if (csr0 & LE_C0_MERR) {
442 printk("%s: Bus master arbitration failure, status %4.4x.\n",
443 dev->name, csr0);
444
445 WRITERDP(LE_C0_STRT);
446 }
447
448 if (lp->tx_full && netif_queue_stopped(dev) && (TX_BUFFS_AVAIL >= 0)) {
449 lp->tx_full = 0;
450 netif_wake_queue (dev);
451 }
452
453 WRITERAP(LE_CSR0);
454 WRITERDP(LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_MERR|LE_C0_IDON|LE_C0_INEA);
455
456 spin_unlock (&lp->devlock);
457}
458
459int lance_open (struct net_device *dev)
460{
461 struct lance_private *lp = (struct lance_private *)dev->priv;
462 int res;
463 DECLARE_LL;
464
465
466 if (request_irq(lp->irq, lance_interrupt, 0, lp->name, dev))
467 return -EAGAIN;
468
469 res = lance_reset(dev);
470 lp->devlock = SPIN_LOCK_UNLOCKED;
471 netif_start_queue (dev);
472
473 return res;
474}
475
476int lance_close (struct net_device *dev)
477{
478 struct lance_private *lp = (struct lance_private *) dev->priv;
479 DECLARE_LL;
480
481 netif_stop_queue (dev);
482
483
484 WRITERAP(LE_CSR0);
485 WRITERDP(LE_C0_STOP);
486
487 free_irq(lp->irq, dev);
488
489 return 0;
490}
491
492void lance_tx_timeout(struct net_device *dev)
493{
494 printk("lance_tx_timeout\n");
495 lance_reset(dev);
496 dev->trans_start = jiffies;
497 netif_wake_queue (dev);
498}
499
500
501int lance_start_xmit (struct sk_buff *skb, struct net_device *dev)
502{
503 struct lance_private *lp = (struct lance_private *)dev->priv;
504 volatile struct lance_init_block *ib = lp->init_block;
505 int entry, skblen, len;
506 static int outs;
507 unsigned long flags;
508 DECLARE_LL;
509
510 if (!TX_BUFFS_AVAIL)
511 return -1;
512
513 netif_stop_queue (dev);
514
515 skblen = skb->len;
516
517#ifdef DEBUG_DRIVER
518
519 {
520 int i;
521
522 for (i = 0; i < 64; i++) {
523 if ((i % 16) == 0)
524 printk ("\n");
525 printk ("%2.2x ", skb->data [i]);
526 }
527 }
528#endif
529 len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
530 entry = lp->tx_new & lp->tx_ring_mod_mask;
531 ib->btx_ring [entry].length = (-len) | 0xf000;
532 ib->btx_ring [entry].misc = 0;
533
534 memcpy ((char *)&ib->tx_buf [entry][0], skb->data, skblen);
535
536
537 ib->btx_ring [entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN);
538 lp->tx_new = (lp->tx_new+1) & lp->tx_ring_mod_mask;
539
540 outs++;
541
542 WRITERDP(LE_C0_INEA | LE_C0_TDMD);
543 dev->trans_start = jiffies;
544 dev_kfree_skb (skb);
545
546 spin_lock_irqsave (&lp->devlock, flags);
547 if (TX_BUFFS_AVAIL)
548 netif_start_queue (dev);
549 else
550 lp->tx_full = 1;
551 spin_unlock_irqrestore (&lp->devlock, flags);
552
553 return 0;
554}
555
556struct net_device_stats *lance_get_stats (struct net_device *dev)
557{
558 struct lance_private *lp = (struct lance_private *) dev->priv;
559
560 return &lp->stats;
561}
562
563
564static void lance_load_multicast (struct net_device *dev)
565{
566 struct lance_private *lp = (struct lance_private *) dev->priv;
567 volatile struct lance_init_block *ib = lp->init_block;
568 volatile u16 *mcast_table = (u16 *)&ib->filter;
569 struct dev_mc_list *dmi=dev->mc_list;
570 char *addrs;
571 int i;
572 u32 crc;
573
574
575 if (dev->flags & IFF_ALLMULTI){
576 ib->filter [0] = 0xffffffff;
577 ib->filter [1] = 0xffffffff;
578 return;
579 }
580
581 ib->filter [0] = 0;
582 ib->filter [1] = 0;
583
584
585 for (i = 0; i < dev->mc_count; i++){
586 addrs = dmi->dmi_addr;
587 dmi = dmi->next;
588
589
590 if (!(*addrs & 1))
591 continue;
592
593 crc = ether_crc_le(6, addrs);
594 crc = crc >> 26;
595 mcast_table [crc >> 4] |= 1 << (crc & 0xf);
596 }
597 return;
598}
599
600
601void lance_set_multicast (struct net_device *dev)
602{
603 struct lance_private *lp = (struct lance_private *) dev->priv;
604 volatile struct lance_init_block *ib = lp->init_block;
605 int stopped;
606 DECLARE_LL;
607
608 stopped = netif_queue_stopped(dev);
609 if (!stopped)
610 netif_stop_queue (dev);
611
612 while (lp->tx_old != lp->tx_new)
613 schedule();
614
615 WRITERAP(LE_CSR0);
616 WRITERDP(LE_C0_STOP);
617 lance_init_ring (dev);
618
619 if (dev->flags & IFF_PROMISC) {
620 ib->mode |= LE_MO_PROM;
621 } else {
622 ib->mode &= ~LE_MO_PROM;
623 lance_load_multicast (dev);
624 }
625 load_csrs (lp);
626 init_restart_lance (lp);
627
628 if (!stopped)
629 netif_start_queue (dev);
630}
631
632MODULE_LICENSE("GPL");
633