Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / net / arcnet / arcnet.c
1 /*
2  * Linux ARCnet driver - device-independent routines
3  * 
4  * Written 1997 by David Woodhouse.
5  * Written 1994-1999 by Avery Pennarun.
6  * Written 1999-2000 by Martin Mares <mj@suse.cz>.
7  * Derived from skeleton.c by Donald Becker.
8  *
9  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
10  *  for sponsoring the further development of this driver.
11  *
12  * **********************
13  *
14  * The original copyright was as follows:
15  *
16  * skeleton.c Written 1993 by Donald Becker.
17  * Copyright 1993 United States Government as represented by the
18  * Director, National Security Agency.  This software may only be used
19  * and distributed according to the terms of the GNU Public License as
20  * modified by SRC, incorporated herein by reference.
21  *
22  * **********************
23  * 
24  * The change log is now in a file called ChangeLog in this directory.
25  *
26  * Sources:
27  *  - Crynwr arcnet.com/arcether.com packet drivers.
28  *  - arcnet.c v0.00 dated 1/1/94 and apparently by 
29  *     Donald Becker - it didn't work :)
30  *  - skeleton.c v0.05 dated 11/16/93 by Donald Becker
31  *     (from Linux Kernel 1.1.45)
32  *  - RFC's 1201 and 1051 - re: TCP/IP over ARCnet
33  *  - The official ARCnet COM9026 data sheets (!) thanks to
34  *     Ken Cornetet <kcornete@nyx10.cs.du.edu>
35  *  - The official ARCnet COM20020 data sheets.
36  *  - Information on some more obscure ARCnet controller chips, thanks
37  *     to the nice people at SMSC.
38  *  - net/inet/eth.c (from kernel 1.1.50) for header-building info.
39  *  - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
40  *  - Textual information and more alternate source from Joachim Koenig
41  *     <jojo@repas.de>
42  */
43
44 #define VERSION "arcnet: v3.93 BETA 2000/04/29 - by Avery Pennarun et al.\n"
45
46 #include <linux/module.h>
47 #include <linux/config.h>
48 #include <linux/types.h>
49 #include <linux/delay.h>
50 #include <linux/netdevice.h>
51 #include <linux/if_arp.h>
52 #include <net/arp.h>
53 #include <linux/init.h>
54 #include <linux/arcdevice.h>
55
56
57 /* "do nothing" functions for protocol drivers */
58 static void null_rx(struct net_device *dev, int bufnum,
59                     struct archdr *pkthdr, int length);
60 static int null_build_header(struct sk_buff *skb, unsigned short type,
61                              uint8_t daddr);
62 static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
63                            int length, int bufnum);
64
65
66 /*
67  * one ArcProto per possible proto ID.  None of the elements of
68  * arc_proto_map are allowed to be NULL; they will get set to
69  * arc_proto_default instead.  It also must not be NULL; if you would like
70  * to set it to NULL, set it to &arc_proto_null instead.
71  */
72 struct ArcProto *arc_proto_map[256], *arc_proto_default, *arc_bcast_proto;
73
74 struct ArcProto arc_proto_null =
75 {
76         '?',
77         XMTU,
78         null_rx,
79         null_build_header,
80         null_prepare_tx
81 };
82
83
84 /* Exported function prototypes */
85 int arcnet_debug = ARCNET_DEBUG;
86
87 EXPORT_SYMBOL(arc_proto_map);
88 EXPORT_SYMBOL(arc_proto_default);
89 EXPORT_SYMBOL(arc_bcast_proto);
90 EXPORT_SYMBOL(arc_proto_null);
91 EXPORT_SYMBOL(arcnet_unregister_proto);
92 EXPORT_SYMBOL(arcnet_debug);
93 EXPORT_SYMBOL(arcdev_setup);
94 EXPORT_SYMBOL(arcnet_interrupt);
95
96 /* Internal function prototypes */
97 static int arcnet_open(struct net_device *dev);
98 static int arcnet_close(struct net_device *dev);
99 static int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev);
100 static void arcnet_timeout(struct net_device *dev);
101 static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
102                          unsigned short type, void *daddr, void *saddr,
103                          unsigned len);
104 static int arcnet_rebuild_header(struct sk_buff *skb);
105 static struct net_device_stats *arcnet_get_stats(struct net_device *dev);
106 static int go_tx(struct net_device *dev);
107
108 void __init arcnet_init(void)
109 {
110         static int arcnet_inited = 0;
111         int count;
112
113         if (arcnet_inited++)
114                 return;
115
116         printk(VERSION);
117
118 #ifdef ALPHA_WARNING
119         BUGLVL(D_EXTRA) {
120                 printk("arcnet: ***\n"
121                 "arcnet: * Read arcnet.txt for important release notes!\n"
122                        "arcnet: *\n"
123                        "arcnet: * This is an ALPHA version! (Last stable release: v3.02)  E-mail\n"
124                        "arcnet: * me if you have any questions, comments, or bug reports.\n"
125                        "arcnet: ***\n");
126         }
127 #endif
128
129         /* initialize the protocol map */
130         arc_proto_default = arc_bcast_proto = &arc_proto_null;
131         for (count = 0; count < 256; count++)
132                 arc_proto_map[count] = arc_proto_default;
133
134         BUGLVL(D_DURING)
135             printk("arcnet: struct sizes: %d %d %d %d %d\n",
136                  sizeof(struct arc_hardware), sizeof(struct arc_rfc1201),
137                 sizeof(struct arc_rfc1051), sizeof(struct arc_eth_encap),
138                    sizeof(struct archdr));
139
140 #ifdef CONFIG_ARCNET            /* We're not built as a module */
141         printk("arcnet: Available protocols:");
142 #ifdef CONFIG_ARCNET_1201
143         printk(" RFC1201");
144         arcnet_rfc1201_init();
145 #endif
146 #ifdef CONFIG_ARCNET_1051
147         printk(" RFC1051");
148         arcnet_rfc1051_init();
149 #endif
150 #ifdef CONFIG_ARCNET_RAW
151         printk(" RAW");
152         arcnet_raw_init();
153 #endif
154         printk("\n");
155 #ifdef CONFIG_ARCNET_COM90xx
156         com90xx_probe(NULL);
157 #endif
158 #endif
159 }
160
161
162 #ifdef MODULE
163
164 static int debug = ARCNET_DEBUG;
165 MODULE_PARM(debug, "i");
166
167 int __init init_module(void)
168 {
169         arcnet_debug = debug;
170         arcnet_init();
171         return 0;
172 }
173
174 void cleanup_module(void)
175 {
176 }
177
178 #endif
179
180
181 /*
182  * Dump the contents of an sk_buff
183  */
184 #if ARCNET_DEBUG_MAX & D_SKB
185 void arcnet_dump_skb(struct net_device *dev, struct sk_buff *skb, char *desc)
186 {
187         int i;
188         long flags;
189
190         save_flags(flags);
191         cli();
192         printk(KERN_DEBUG "%6s: skb dump (%s) follows:", dev->name, desc);
193         for (i = 0; i < skb->len; i++) {
194                 if (i % 16 == 0)
195                         printk("\n" KERN_DEBUG "[%04X] ", i);
196                 printk("%02X ", ((u_char *) skb->data)[i]);
197         }
198         printk("\n");
199         restore_flags(flags);
200 }
201
202 EXPORT_SYMBOL(arcnet_dump_skb);
203 #endif
204
205
206 /*
207  * Dump the contents of an ARCnet buffer
208  */
209 #if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
210 void arcnet_dump_packet(struct net_device *dev, int bufnum, char *desc)
211 {
212         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
213         int i, length;
214         long flags;
215         static uint8_t buf[512];
216
217         save_flags(flags);
218         cli();
219
220         lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
221
222         /* if the offset[0] byte is nonzero, this is a 256-byte packet */
223         length = (buf[2] ? 256 : 512);
224
225         printk(KERN_DEBUG "%6s: packet dump (%s) follows:", dev->name, desc);
226         for (i = 0; i < length; i++) {
227                 if (i % 16 == 0)
228                         printk("\n" KERN_DEBUG "[%04X] ", i);
229                 printk("%02X ", buf[i]);
230         }
231         printk("\n");
232
233         restore_flags(flags);
234 }
235
236 EXPORT_SYMBOL(arcnet_dump_packet);
237 #endif
238
239
240 /*
241  * Unregister a protocol driver from the arc_proto_map.  Protocol drivers
242  * are responsible for registering themselves, but the unregister routine
243  * is pretty generic so we'll do it here.
244  */
245 void arcnet_unregister_proto(struct ArcProto *proto)
246 {
247         int count;
248
249         if (arc_proto_default == proto)
250                 arc_proto_default = &arc_proto_null;
251         if (arc_bcast_proto == proto)
252                 arc_bcast_proto = arc_proto_default;
253
254         for (count = 0; count < 256; count++) {
255                 if (arc_proto_map[count] == proto)
256                         arc_proto_map[count] = arc_proto_default;
257         }
258 }
259
260
261 /*
262  * Add a buffer to the queue.  Only the interrupt handler is allowed to do
263  * this, unless interrupts are disabled.
264  * 
265  * Note: we don't check for a full queue, since there aren't enough buffers
266  * to more than fill it.
267  */
268 static void release_arcbuf(struct net_device *dev, int bufnum)
269 {
270         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
271         int i;
272
273         lp->buf_queue[lp->first_free_buf++] = bufnum;
274         lp->first_free_buf %= 5;
275
276         BUGLVL(D_DURING) {
277                 BUGMSG(D_DURING, "release_arcbuf: freed #%d; buffer queue is now: ",
278                        bufnum);
279                 for (i = lp->next_buf; i != lp->first_free_buf; i = ++i % 5)
280                         BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
281                 BUGMSG2(D_DURING, "\n");
282         }
283 }
284
285
286 /*
287  * Get a buffer from the queue.  If this returns -1, there are no buffers
288  * available.
289  */
290 static int get_arcbuf(struct net_device *dev)
291 {
292         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
293         int buf = -1, i;
294
295         if (!atomic_dec_and_test(&lp->buf_lock))        /* already in this function */
296                 BUGMSG(D_NORMAL, "get_arcbuf: overlap (%d)!\n", lp->buf_lock.counter);
297         else {                  /* we can continue */
298                 if (lp->next_buf >= 5)
299                         lp->next_buf -= 5;
300
301                 if (lp->next_buf == lp->first_free_buf)
302                         BUGMSG(D_NORMAL, "get_arcbuf: BUG: no buffers are available??\n");
303                 else {
304                         buf = lp->buf_queue[lp->next_buf++];
305                         lp->next_buf %= 5;
306                 }
307         }
308
309
310         BUGLVL(D_DURING) {
311                 BUGMSG(D_DURING, "get_arcbuf: got #%d; buffer queue is now: ", buf);
312                 for (i = lp->next_buf; i != lp->first_free_buf; i = ++i % 5)
313                         BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
314                 BUGMSG2(D_DURING, "\n");
315         }
316
317         atomic_inc(&lp->buf_lock);
318         return buf;
319 }
320
321
322 static int choose_mtu(void)
323 {
324         int count, mtu = 65535;
325
326         /* choose the smallest MTU of all available encaps */
327         for (count = 0; count < 256; count++) {
328                 if (arc_proto_map[count] != &arc_proto_null
329                     && arc_proto_map[count]->mtu < mtu) {
330                         mtu = arc_proto_map[count]->mtu;
331                 }
332         }
333
334         return mtu == 65535 ? XMTU : mtu;
335 }
336
337
338 /* Setup a struct device for ARCnet. */
339 void arcdev_setup(struct net_device *dev)
340 {
341         dev->type = ARPHRD_ARCNET;
342         dev->hard_header_len = sizeof(struct archdr);
343         dev->mtu = choose_mtu();
344
345         dev->addr_len = 1;
346         dev->tx_queue_len = 30;
347         dev->broadcast[0] = 0x00;       /* for us, broadcasts are address 0 */
348         dev->watchdog_timeo = TX_TIMEOUT;
349
350         /* New-style flags. */
351         dev->flags = IFF_BROADCAST;
352
353         /*
354          * Put in this stuff here, so we don't have to export the symbols to
355          * the chipset drivers.
356          */
357         dev->open = arcnet_open;
358         dev->stop = arcnet_close;
359         dev->hard_start_xmit = arcnet_send_packet;
360         dev->tx_timeout = arcnet_timeout;
361         dev->get_stats = arcnet_get_stats;
362         dev->hard_header = arcnet_header;
363         dev->rebuild_header = arcnet_rebuild_header;
364
365         dev_init_buffers(dev);
366 }
367
368
369 /*
370  * Open/initialize the board.  This is called sometime after booting when
371  * the 'ifconfig' program is run.
372  *
373  * This routine should set everything up anew at each open, even registers
374  * that "should" only need to be set once at boot, so that there is
375  * non-reboot way to recover if something goes wrong.
376  */
377 static int arcnet_open(struct net_device *dev)
378 {
379         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
380         int count, newmtu;
381
382         BUGLVL(D_PROTO) {
383                 int count;
384                 BUGMSG(D_PROTO, "protocol map (default is '%c'): ",
385                        arc_proto_default->suffix);
386                 for (count = 0; count < 256; count++)
387                         BUGMSG2(D_PROTO, "%c", arc_proto_map[count]->suffix);
388                 BUGMSG2(D_PROTO, "\n");
389         }
390
391
392         BUGMSG(D_INIT, "arcnet_open: resetting card.\n");
393
394         /* try to put the card in a defined state - if it fails the first
395          * time, actually reset it.
396          */
397         if (ARCRESET(0) && ARCRESET(1))
398                 return -ENODEV;
399
400         newmtu = choose_mtu();
401         if (newmtu < dev->mtu)
402                 dev->mtu = newmtu;
403
404         /* autodetect the encapsulation for each host. */
405         memset(lp->default_proto, 0, sizeof(lp->default_proto));
406
407         /* the broadcast address is special - use the 'bcast' protocol */
408         for (count = 0; count < 256; count++) {
409                 if (arc_proto_map[count] == arc_bcast_proto) {
410                         lp->default_proto[0] = count;
411                         break;
412                 }
413         }
414
415         /* initialize buffers */
416         atomic_set(&lp->buf_lock, 1);
417         lp->next_buf = lp->first_free_buf = 0;
418         release_arcbuf(dev, 0);
419         release_arcbuf(dev, 1);
420         release_arcbuf(dev, 2);
421         release_arcbuf(dev, 3);
422         lp->cur_tx = lp->next_tx = -1;
423         lp->cur_rx = -1;
424
425         lp->rfc1201.sequence = 1;
426
427         /* bring up the hardware driver */
428         ARCOPEN(1);
429
430         if (dev->dev_addr[0] == 0)
431                 BUGMSG(D_NORMAL, "WARNING!  Station address 00 is reserved "
432                        "for broadcasts!\n");
433         else if (dev->dev_addr[0] == 255)
434                 BUGMSG(D_NORMAL, "WARNING!  Station address FF may confuse "
435                        "DOS networking programs!\n");
436
437         if (ASTATUS() & RESETflag)
438                 ACOMMAND(CFLAGScmd | RESETclear);
439
440         /* make sure we're ready to receive IRQ's. */
441         AINTMASK(0);
442         udelay(1);              /* give it time to set the mask before
443                                  * we reset it again. (may not even be
444                                  * necessary)
445                                  */
446         lp->intmask = NORXflag | RECONflag;
447         AINTMASK(lp->intmask);
448
449         netif_start_queue(dev);
450
451         return 0;
452 }
453
454
455 /* The inverse routine to arcnet_open - shuts down the card. */
456 static int arcnet_close(struct net_device *dev)
457 {
458         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
459
460         netif_stop_queue(dev);
461
462         /* flush TX and disable RX */
463         AINTMASK(0);
464         ACOMMAND(NOTXcmd);      /* stop transmit */
465         ACOMMAND(NORXcmd);      /* disable receive */
466         mdelay(1);
467
468         /* shut down the card */
469         ARCOPEN(0);
470
471         return 0;
472 }
473
474
475 static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
476                          unsigned short type, void *daddr, void *saddr,
477                          unsigned len)
478 {
479         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
480         uint8_t _daddr, proto_num;
481         struct ArcProto *proto;
482
483         BUGMSG(D_DURING,
484             "create header from %d to %d; protocol %d (%Xh); size %u.\n",
485                saddr ? *(uint8_t *) saddr : -1,
486                daddr ? *(uint8_t *) daddr : -1,
487                type, type, len);
488
489         if (len != skb->len)
490                 BUGMSG(D_NORMAL, "arcnet_header: Yikes!  skb->len(%d) != len(%d)!\n",
491                        skb->len, len);
492
493         /*
494          * if the dest addr isn't provided, we can't choose an encapsulation!
495          * Store the packet type (eg. ETH_P_IP) for now, and we'll push on a
496          * real header when we do rebuild_header. 
497          */
498         if (!daddr) {
499                 *(uint16_t *) skb_push(skb, 2) = type;
500                 if (skb->nh.raw - skb->mac.raw != 2)
501                         BUGMSG(D_NORMAL, "arcnet_header: Yikes!  diff (%d) is not 2!\n",
502                                skb->nh.raw - skb->mac.raw);
503                 return -2;      /* return error -- can't transmit yet! */
504         }
505         /* otherwise, we can just add the header as usual. */
506         _daddr = *(uint8_t *) daddr;
507         proto_num = lp->default_proto[_daddr];
508         proto = arc_proto_map[proto_num];
509         BUGMSG(D_DURING, "building header for %02Xh using protocol '%c'\n",
510                proto_num, proto->suffix);
511         if (proto == &arc_proto_null && arc_bcast_proto != proto) {
512                 BUGMSG(D_DURING, "actually, let's use '%c' instead.\n",
513                        arc_bcast_proto->suffix);
514                 proto = arc_bcast_proto;
515         }
516         return proto->build_header(skb, type, _daddr);
517 }
518
519
520 /* 
521  * Rebuild the ARCnet hard header. This is called after an ARP (or in the
522  * future other address resolution) has completed on this sk_buff. We now
523  * let ARP fill in the destination field.
524  */
525 static int arcnet_rebuild_header(struct sk_buff *skb)
526 {
527         struct net_device *dev = skb->dev;
528         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
529         int status = 0;         /* default is failure */
530         unsigned short type;
531         uint8_t daddr;
532
533         if (skb->nh.raw - skb->mac.raw != 2) {
534                 BUGMSG(D_NORMAL,
535                      "rebuild_header: shouldn't be here! (hdrsize=%d)\n",
536                        skb->nh.raw - skb->mac.raw);
537                 return 0;
538         }
539         type = *(uint16_t *) skb_pull(skb, 2);
540
541         if (type == ETH_P_IP) {
542 #ifdef CONFIG_INET
543                 BUGMSG(D_DURING, "rebuild header for ethernet protocol %Xh\n", type);
544                 status = arp_find(&daddr, skb) ? 1 : 0;
545                 BUGMSG(D_DURING, " rebuilt: dest is %d; protocol %Xh\n",
546                        daddr, type);
547 #endif
548         } else {
549                 BUGMSG(D_NORMAL,
550                        "I don't understand ethernet protocol %Xh addresses!\n", type);
551                 lp->stats.tx_errors++;
552                 lp->stats.tx_aborted_errors++;
553         }
554
555         /* if we couldn't resolve the address... give up. */
556         if (!status)
557                 return 0;
558
559         /* add the _real_ header this time! */
560         arc_proto_map[lp->default_proto[daddr]]->build_header(skb, type, daddr);
561
562         return 1;               /* success */
563 }
564
565
566
567 /* Called by the kernel in order to transmit a packet. */
568 static int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev)
569 {
570         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
571         struct archdr *pkt;
572         struct arc_rfc1201 *soft;
573         struct ArcProto *proto;
574         int txbuf;
575
576         BUGMSG(D_DURING,
577                "transmit requested (status=%Xh, txbufs=%d/%d, len=%d)\n",
578                ASTATUS(), lp->cur_tx, lp->next_tx, skb->len);
579
580         pkt = (struct archdr *) skb->data;
581         soft = &pkt->soft.rfc1201;
582         proto = arc_proto_map[soft->proto];
583
584         BUGMSG(D_SKB_SIZE, "skb: transmitting %d bytes to %02X\n",
585                 skb->len, pkt->hard.dest);
586         BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "tx");
587
588         /* fits in one packet? */
589         if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
590                 BUGMSG(D_NORMAL, "fixme: packet too large: compensating badly!\n");
591                 dev_kfree_skb(skb);
592                 return 0;       /* don't try again */
593         }
594
595         /* We're busy transmitting a packet... */
596         netif_stop_queue(dev);
597
598         AINTMASK(0);
599
600         txbuf = get_arcbuf(dev);
601         if (txbuf != -1) {
602                 if (proto->prepare_tx(dev, pkt, skb->len, txbuf)) {
603                         /* done right away */
604                         lp->stats.tx_bytes += skb->len;
605                         dev_kfree_skb(skb);
606                 } else {
607                         /* do it the 'split' way */
608                         lp->outgoing.proto = proto;
609                         lp->outgoing.skb = skb;
610                         lp->outgoing.pkt = pkt;
611
612                         if (!proto->continue_tx)
613                                 BUGMSG(D_NORMAL, "bug! prep_tx==0, but no continue_tx!\n");
614                         else if (proto->continue_tx(dev, txbuf)) {
615                                 BUGMSG(D_NORMAL,
616                                        "bug! continue_tx finished the first time! "
617                                        "(proto='%c')\n", proto->suffix);
618                         }
619                 }
620
621                 lp->next_tx = txbuf;
622         } else
623                 dev_kfree_skb(skb);
624
625         /* make sure we didn't ignore a TX IRQ while we were in here */
626         AINTMASK(0);
627         lp->intmask |= TXFREEflag;
628         AINTMASK(lp->intmask);
629
630         return 0;               /* no need to try again */
631 }
632
633
634 /*
635  * Actually start transmitting a packet that was loaded into a buffer
636  * by prepare_tx.  This should _only_ be called by the interrupt handler.
637  */
638 static int go_tx(struct net_device *dev)
639 {
640         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
641
642         BUGMSG(D_DURING, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
643                ASTATUS(), lp->intmask, lp->next_tx, lp->cur_tx);
644
645         if (lp->cur_tx != -1 || lp->next_tx == -1)
646                 return 0;
647
648         BUGLVL(D_TX) arcnet_dump_packet(dev, lp->next_tx, "go_tx");
649
650         lp->cur_tx = lp->next_tx;
651         lp->next_tx = -1;
652
653         /* start sending */
654         ACOMMAND(TXcmd | (lp->cur_tx << 3));
655
656         dev->trans_start = jiffies;
657         lp->stats.tx_packets++;
658         lp->lasttrans_dest = lp->lastload_dest;
659         lp->lastload_dest = 0;
660         lp->intmask |= TXFREEflag;
661
662         return 1;
663 }
664
665
666 /* Called by the kernel when transmit times out */
667 static void arcnet_timeout(struct net_device *dev)
668 {
669         unsigned long flags;
670         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
671         int status = ASTATUS();
672         char *msg;
673
674         save_flags(flags);
675         cli();
676
677         if (status & TXFREEflag) {      /* transmit _DID_ finish */
678                 msg = " - missed IRQ?";
679         } else {
680                 msg = "";
681                 lp->stats.tx_aborted_errors++;
682                 lp->timed_out = 1;
683                 ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
684         }
685         lp->stats.tx_errors++;
686
687         /* make sure we didn't miss a TX IRQ */
688         AINTMASK(0);
689         lp->intmask |= TXFREEflag;
690         AINTMASK(lp->intmask);
691
692         restore_flags(flags);
693
694         if (jiffies - lp->last_timeout > 10*HZ) {
695                 BUGMSG(D_EXTRA, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
696                        msg, status, lp->intmask, lp->lasttrans_dest);
697                 lp->last_timeout = jiffies;
698         }
699 }
700
701
702 /*
703  * The typical workload of the driver: Handle the network interface
704  * interrupts. Establish which device needs attention, and call the correct
705  * chipset interrupt handler.
706  */
707 void arcnet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
708 {
709         struct net_device *dev = dev_id;
710         struct arcnet_local *lp;
711         int recbuf, status, didsomething, boguscount;
712
713         BUGMSG(D_DURING, "\n");
714
715         if (dev == NULL) {
716                 BUGMSG(D_DURING, "arcnet: irq %d for unknown device.\n", irq);
717                 return;
718         }
719         BUGMSG(D_DURING, "in arcnet_interrupt\n");
720
721         lp = (struct arcnet_local *) dev->priv;
722         if (!lp) {
723                 BUGMSG(D_DURING, "arcnet: irq ignored due to missing lp.\n");
724                 return;
725         }
726         /*
727          * RESET flag was enabled - if device is not running, we must clear it right
728          * away (but nothing else).
729          */
730         if (!netif_running(dev)) {
731                 if (ASTATUS() & RESETflag)
732                         ACOMMAND(CFLAGScmd | RESETclear);
733                 AINTMASK(0);
734                 return;
735         }
736
737         BUGMSG(D_DURING, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
738                ASTATUS(), lp->intmask);
739
740         boguscount = 5;
741         do {
742                 status = ASTATUS();
743                 didsomething = 0;
744
745                 /*
746                  * RESET flag was enabled - card is resetting and if RX is
747                  * disabled, it's NOT because we just got a packet.
748                  * 
749                  * The card is in an undefined state.  Clear it out and start over.
750                  */
751                 if (status & RESETflag) {
752                         BUGMSG(D_NORMAL, "spurious reset (status=%Xh)\n", status);
753                         arcnet_close(dev);
754                         arcnet_open(dev);
755
756                         /* get out of the interrupt handler! */
757                         break;
758                 }
759                 /* 
760                  * RX is inhibited - we must have received something. Prepare to
761                  * receive into the next buffer.
762                  * 
763                  * We don't actually copy the received packet from the card until
764                  * after the transmit handler runs (and possibly launches the next
765                  * tx); this should improve latency slightly if we get both types
766                  * of interrupts at once. 
767                  */
768                 recbuf = -1;
769                 if (status & lp->intmask & NORXflag) {
770                         recbuf = lp->cur_rx;
771                         BUGMSG(D_DURING, "Buffer #%d: receive irq (status=%Xh)\n",
772                                recbuf, status);
773
774                         lp->cur_rx = get_arcbuf(dev);
775                         if (lp->cur_rx != -1) {
776                                 BUGMSG(D_DURING, "enabling receive to buffer #%d\n",
777                                        lp->cur_rx);
778                                 ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
779                         }
780                         didsomething++;
781                 }
782                 /* a transmit finished, and we're interested in it. */
783                 if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
784                         lp->intmask &= ~TXFREEflag;
785
786                         BUGMSG(D_DURING, "TX IRQ (stat=%Xh)\n", status);
787
788                         if (lp->cur_tx != -1 && !(status & TXACKflag) && !lp->timed_out) {
789                                 if (lp->lasttrans_dest != 0) {
790                                         BUGMSG(D_EXTRA, "transmit was not acknowledged! "
791                                             "(status=%Xh, dest=%02Xh)\n",
792                                              status, lp->lasttrans_dest);
793                                         lp->stats.tx_errors++;
794                                         lp->stats.tx_carrier_errors++;
795                                 } else {
796                                         BUGMSG(D_DURING,
797                                                "broadcast was not acknowledged; that's normal "
798                                             "(status=%Xh, dest=%02Xh)\n",
799                                              status, lp->lasttrans_dest);
800                                 }
801                         }
802                         if (lp->cur_tx != -1)
803                                 release_arcbuf(dev, lp->cur_tx);
804
805                         lp->cur_tx = -1;
806                         lp->timed_out = 0;
807                         didsomething++;
808
809                         /* send another packet if there is one */
810                         go_tx(dev);
811
812                         /* continue a split packet, if any */
813                         if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
814                                 int txbuf = get_arcbuf(dev);
815                                 if (txbuf != -1) {
816                                         if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
817                                                 /* that was the last segment */
818                                                 lp->stats.tx_bytes += lp->outgoing.skb->len;
819                                                 dev_kfree_skb_irq(lp->outgoing.skb);
820                                                 lp->outgoing.proto = NULL;
821                                         }
822                                         lp->next_tx = txbuf;
823                                 }
824                         }
825                         /* inform upper layers of idleness, if necessary */
826                         if (lp->cur_tx == -1)
827                                 netif_wake_queue(dev);
828                 }
829                 /* now process the received packet, if any */
830                 if (recbuf != -1) {
831                         BUGLVL(D_RX) arcnet_dump_packet(dev, recbuf, "rx irq");
832
833                         arcnet_rx(dev, recbuf);
834                         release_arcbuf(dev, recbuf);
835
836                         didsomething++;
837                 }
838                 if (status & lp->intmask & RECONflag) {
839                         ACOMMAND(CFLAGScmd | CONFIGclear);
840                         lp->stats.tx_carrier_errors++;
841
842                         BUGMSG(D_RECON, "Network reconfiguration detected (status=%Xh)\n",
843                                status);
844
845                         /* is the RECON info empty or old? */
846                         if (!lp->first_recon || !lp->last_recon ||
847                             jiffies - lp->last_recon > HZ * 10) {
848                                 if (lp->network_down)
849                                         BUGMSG(D_NORMAL, "reconfiguration detected: cabling restored?\n");
850                                 lp->first_recon = lp->last_recon = jiffies;
851                                 lp->num_recons = lp->network_down = 0;
852
853                                 BUGMSG(D_DURING, "recon: clearing counters.\n");
854                         } else {        /* add to current RECON counter */
855                                 lp->last_recon = jiffies;
856                                 lp->num_recons++;
857
858                                 BUGMSG(D_DURING, "recon: counter=%d, time=%lds, net=%d\n",
859                                        lp->num_recons,
860                                  (lp->last_recon - lp->first_recon) / HZ,
861                                        lp->network_down);
862
863                                 /* if network is marked up;
864                                  * and first_recon and last_recon are 60+ apart;
865                                  * and the average no. of recons counted is
866                                  *    > RECON_THRESHOLD/min;
867                                  * then print a warning message.
868                                  */
869                                 if (!lp->network_down
870                                     && (lp->last_recon - lp->first_recon) <= HZ * 60
871                                   && lp->num_recons >= RECON_THRESHOLD) {
872                                         lp->network_down = 1;
873                                         BUGMSG(D_NORMAL, "many reconfigurations detected: cabling problem?\n");
874                                 } else if (!lp->network_down
875                                            && lp->last_recon - lp->first_recon > HZ * 60) {
876                                         /* reset counters if we've gone for over a minute. */
877                                         lp->first_recon = lp->last_recon;
878                                         lp->num_recons = 1;
879                                 }
880                         }
881                 } else if (lp->network_down && jiffies - lp->last_recon > HZ * 10) {
882                         if (lp->network_down)
883                                 BUGMSG(D_NORMAL, "cabling restored?\n");
884                         lp->first_recon = lp->last_recon = 0;
885                         lp->num_recons = lp->network_down = 0;
886
887                         BUGMSG(D_DURING, "not recon: clearing counters anyway.\n");
888                 }
889         }
890         while (--boguscount && didsomething);
891
892         BUGMSG(D_DURING, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
893                ASTATUS(), boguscount);
894         BUGMSG(D_DURING, "\n");
895
896
897         AINTMASK(0);
898         udelay(1);
899         AINTMASK(lp->intmask);
900 }
901
902
903 /*
904  * This is a generic packet receiver that calls arcnet??_rx depending on the
905  * protocol ID found.
906  */
907 void arcnet_rx(struct net_device *dev, int bufnum)
908 {
909         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
910         struct archdr pkt;
911         struct arc_rfc1201 *soft;
912         int length, ofs;
913
914         soft = &pkt.soft.rfc1201;
915
916         lp->hw.copy_from_card(dev, bufnum, 0, &pkt, sizeof(ARC_HDR_SIZE));
917         if (pkt.hard.offset[0]) {
918                 ofs = pkt.hard.offset[0];
919                 length = 256 - ofs;
920         } else {
921                 ofs = pkt.hard.offset[1];
922                 length = 512 - ofs;
923         }
924
925         /* get the full header, if possible */
926         if (sizeof(pkt.soft) < length)
927                 lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
928         else {
929                 memset(&pkt.soft, 0, sizeof(pkt.soft));
930                 lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
931         }
932
933         BUGMSG(D_DURING, "Buffer #%d: received packet from %02Xh to %02Xh "
934                "(%d+4 bytes)\n",
935                bufnum, pkt.hard.source, pkt.hard.dest, length);
936
937         lp->stats.rx_packets++;
938         lp->stats.rx_bytes += length + ARC_HDR_SIZE;
939
940         /* call the right receiver for the protocol */
941         if (arc_proto_map[soft->proto] != &arc_proto_null) {
942                 BUGLVL(D_PROTO) {
943                         struct ArcProto
944                         *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
945                         *newp = arc_proto_map[soft->proto];
946
947                         if (oldp != newp) {
948                                 BUGMSG(D_PROTO,
949                                        "got protocol %02Xh; encap for host %02Xh is now '%c'"
950                                        " (was '%c')\n", soft->proto, pkt.hard.source,
951                                        newp->suffix, oldp->suffix);
952                         }
953                 }
954
955                 /* broadcasts will always be done with the last-used encap. */
956                 lp->default_proto[0] = soft->proto;
957
958                 /* in striking contrast, the following isn't a hack. */
959                 lp->default_proto[pkt.hard.source] = soft->proto;
960         }
961         /* call the protocol-specific receiver. */
962         arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
963 }
964
965
966
967 /* 
968  * Get the current statistics.  This may be called with the card open or
969  * closed.
970  */
971 static struct net_device_stats *arcnet_get_stats(struct net_device *dev)
972 {
973         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
974         return &lp->stats;
975 }
976
977
978 static void null_rx(struct net_device *dev, int bufnum,
979                     struct archdr *pkthdr, int length)
980 {
981         BUGMSG(D_PROTO,
982         "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
983                pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
984 }
985
986
987 static int null_build_header(struct sk_buff *skb, unsigned short type,
988                              uint8_t daddr)
989 {
990         struct net_device *dev = skb->dev;
991         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
992
993         BUGMSG(D_PROTO,
994                "tx: can't build header for encap %02Xh; load a protocol driver.\n",
995                lp->default_proto[daddr]);
996
997         /* always fails */
998         return 0;
999 }
1000
1001
1002 /* the "do nothing" prepare_tx function warns that there's nothing to do. */
1003 static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
1004                            int length, int bufnum)
1005 {
1006         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
1007         struct arc_hardware newpkt;
1008
1009         BUGMSG(D_PROTO, "tx: no encap for this host; load a protocol driver.\n");
1010
1011         /* send a packet to myself -- will never get received, of course */
1012         newpkt.source = newpkt.dest = dev->dev_addr[0];
1013
1014         /* only one byte of actual data (and it's random) */
1015         newpkt.offset[0] = 0xFF;
1016
1017         lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1018
1019         return 1;               /* done */
1020 }