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