vserver 1.9.3
[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         if (lp->hw.open)
405                 lp->hw.open(dev);
406
407         if (dev->dev_addr[0] == 0)
408                 BUGMSG(D_NORMAL, "WARNING!  Station address 00 is reserved "
409                        "for broadcasts!\n");
410         else if (dev->dev_addr[0] == 255)
411                 BUGMSG(D_NORMAL, "WARNING!  Station address FF may confuse "
412                        "DOS networking programs!\n");
413
414         if (ASTATUS() & RESETflag)
415                 ACOMMAND(CFLAGScmd | RESETclear);
416
417         /* make sure we're ready to receive IRQ's. */
418         AINTMASK(0);
419         udelay(1);              /* give it time to set the mask before
420                                  * we reset it again. (may not even be
421                                  * necessary)
422                                  */
423         lp->intmask = NORXflag | RECONflag;
424         AINTMASK(lp->intmask);
425
426         netif_start_queue(dev);
427
428         return 0;
429
430  out_module_put:
431         module_put(lp->hw.owner);
432         return error;
433 }
434
435
436 /* The inverse routine to arcnet_open - shuts down the card. */
437 static int arcnet_close(struct net_device *dev)
438 {
439         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
440
441         netif_stop_queue(dev);
442
443         /* flush TX and disable RX */
444         AINTMASK(0);
445         ACOMMAND(NOTXcmd);      /* stop transmit */
446         ACOMMAND(NORXcmd);      /* disable receive */
447         mdelay(1);
448
449         /* shut down the card */
450         lp->hw.close(dev);
451         module_put(lp->hw.owner);
452         return 0;
453 }
454
455
456 static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
457                          unsigned short type, void *daddr, void *saddr,
458                          unsigned len)
459 {
460         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
461         uint8_t _daddr, proto_num;
462         struct ArcProto *proto;
463
464         BUGMSG(D_DURING,
465             "create header from %d to %d; protocol %d (%Xh); size %u.\n",
466                saddr ? *(uint8_t *) saddr : -1,
467                daddr ? *(uint8_t *) daddr : -1,
468                type, type, len);
469
470         if (len != skb->len)
471                 BUGMSG(D_NORMAL, "arcnet_header: Yikes!  skb->len(%d) != len(%d)!\n",
472                        skb->len, len);
473
474         /*
475          * if the dest addr isn't provided, we can't choose an encapsulation!
476          * Store the packet type (eg. ETH_P_IP) for now, and we'll push on a
477          * real header when we do rebuild_header. 
478          */
479         if (!daddr) {
480                 *(uint16_t *) skb_push(skb, 2) = type;
481                 if (skb->nh.raw - skb->mac.raw != 2)
482                         BUGMSG(D_NORMAL, "arcnet_header: Yikes!  diff (%d) is not 2!\n",
483                                (int)(skb->nh.raw - skb->mac.raw));
484                 return -2;      /* return error -- can't transmit yet! */
485         }
486         /* otherwise, we can just add the header as usual. */
487         _daddr = *(uint8_t *) daddr;
488         proto_num = lp->default_proto[_daddr];
489         proto = arc_proto_map[proto_num];
490         BUGMSG(D_DURING, "building header for %02Xh using protocol '%c'\n",
491                proto_num, proto->suffix);
492         if (proto == &arc_proto_null && arc_bcast_proto != proto) {
493                 BUGMSG(D_DURING, "actually, let's use '%c' instead.\n",
494                        arc_bcast_proto->suffix);
495                 proto = arc_bcast_proto;
496         }
497         return proto->build_header(skb, dev, type, _daddr);
498 }
499
500
501 /* 
502  * Rebuild the ARCnet hard header. This is called after an ARP (or in the
503  * future other address resolution) has completed on this sk_buff. We now
504  * let ARP fill in the destination field.
505  */
506 static int arcnet_rebuild_header(struct sk_buff *skb)
507 {
508         struct net_device *dev = skb->dev;
509         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
510         int status = 0;         /* default is failure */
511         unsigned short type;
512         uint8_t daddr=0;
513         struct ArcProto *proto;
514
515         if (skb->nh.raw - skb->mac.raw != 2) {
516                 BUGMSG(D_NORMAL,
517                      "rebuild_header: shouldn't be here! (hdrsize=%d)\n",
518                      (int)(skb->nh.raw - skb->mac.raw));
519                 return 0;
520         }
521         type = *(uint16_t *) skb_pull(skb, 2);
522
523         if (type == ETH_P_IP) {
524 #ifdef CONFIG_INET
525                 BUGMSG(D_DURING, "rebuild header for ethernet protocol %Xh\n", type);
526                 status = arp_find(&daddr, skb) ? 1 : 0;
527                 BUGMSG(D_DURING, " rebuilt: dest is %d; protocol %Xh\n",
528                        daddr, type);
529 #endif
530         } else {
531                 BUGMSG(D_NORMAL,
532                        "I don't understand ethernet protocol %Xh addresses!\n", type);
533                 lp->stats.tx_errors++;
534                 lp->stats.tx_aborted_errors++;
535         }
536
537         /* if we couldn't resolve the address... give up. */
538         if (!status)
539                 return 0;
540
541         /* add the _real_ header this time! */
542         proto = arc_proto_map[lp->default_proto[daddr]];
543         proto->build_header(skb, dev, type, daddr);
544
545         return 1;               /* success */
546 }
547
548
549
550 /* Called by the kernel in order to transmit a packet. */
551 static int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev)
552 {
553         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
554         struct archdr *pkt;
555         struct arc_rfc1201 *soft;
556         struct ArcProto *proto;
557         int txbuf;
558
559         BUGMSG(D_DURING,
560                "transmit requested (status=%Xh, txbufs=%d/%d, len=%d)\n",
561                ASTATUS(), lp->cur_tx, lp->next_tx, skb->len);
562
563         pkt = (struct archdr *) skb->data;
564         soft = &pkt->soft.rfc1201;
565         proto = arc_proto_map[soft->proto];
566
567         BUGMSG(D_SKB_SIZE, "skb: transmitting %d bytes to %02X\n",
568                 skb->len, pkt->hard.dest);
569         BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "tx");
570
571         /* fits in one packet? */
572         if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
573                 BUGMSG(D_NORMAL, "fixme: packet too large: compensating badly!\n");
574                 dev_kfree_skb(skb);
575                 return 0;       /* don't try again */
576         }
577
578         /* We're busy transmitting a packet... */
579         netif_stop_queue(dev);
580
581         AINTMASK(0);
582
583         txbuf = get_arcbuf(dev);
584         if (txbuf != -1) {
585                 if (proto->prepare_tx(dev, pkt, skb->len, txbuf)) {
586                         /* done right away */
587                         lp->stats.tx_bytes += skb->len;
588                         dev_kfree_skb(skb);
589                 } else {
590                         /* do it the 'split' way */
591                         lp->outgoing.proto = proto;
592                         lp->outgoing.skb = skb;
593                         lp->outgoing.pkt = pkt;
594
595                         if (!proto->continue_tx)
596                                 BUGMSG(D_NORMAL, "bug! prep_tx==0, but no continue_tx!\n");
597                         else if (proto->continue_tx(dev, txbuf)) {
598                                 BUGMSG(D_NORMAL,
599                                        "bug! continue_tx finished the first time! "
600                                        "(proto='%c')\n", proto->suffix);
601                         }
602                 }
603
604                 lp->next_tx = txbuf;
605         } else
606                 dev_kfree_skb(skb);
607
608         /* make sure we didn't ignore a TX IRQ while we were in here */
609         AINTMASK(0);
610         lp->intmask |= TXFREEflag;
611         AINTMASK(lp->intmask);
612
613         return 0;               /* no need to try again */
614 }
615
616
617 /*
618  * Actually start transmitting a packet that was loaded into a buffer
619  * by prepare_tx.  This should _only_ be called by the interrupt handler.
620  */
621 static int go_tx(struct net_device *dev)
622 {
623         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
624
625         BUGMSG(D_DURING, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
626                ASTATUS(), lp->intmask, lp->next_tx, lp->cur_tx);
627
628         if (lp->cur_tx != -1 || lp->next_tx == -1)
629                 return 0;
630
631         BUGLVL(D_TX) arcnet_dump_packet(dev, lp->next_tx, "go_tx");
632
633         lp->cur_tx = lp->next_tx;
634         lp->next_tx = -1;
635
636         /* start sending */
637         ACOMMAND(TXcmd | (lp->cur_tx << 3));
638
639         dev->trans_start = jiffies;
640         lp->stats.tx_packets++;
641         lp->lasttrans_dest = lp->lastload_dest;
642         lp->lastload_dest = 0;
643         lp->intmask |= TXFREEflag;
644
645         return 1;
646 }
647
648
649 /* Called by the kernel when transmit times out */
650 static void arcnet_timeout(struct net_device *dev)
651 {
652         unsigned long flags;
653         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
654         int status = ASTATUS();
655         char *msg;
656
657         spin_lock_irqsave(&arcnet_lock, flags);
658         if (status & TXFREEflag) {      /* transmit _DID_ finish */
659                 msg = " - missed IRQ?";
660         } else {
661                 msg = "";
662                 lp->stats.tx_aborted_errors++;
663                 lp->timed_out = 1;
664                 ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
665         }
666         lp->stats.tx_errors++;
667
668         /* make sure we didn't miss a TX IRQ */
669         AINTMASK(0);
670         lp->intmask |= TXFREEflag;
671         AINTMASK(lp->intmask);
672         
673         spin_unlock_irqrestore(&arcnet_lock, flags);
674
675         if (jiffies - lp->last_timeout > 10*HZ) {
676                 BUGMSG(D_EXTRA, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
677                        msg, status, lp->intmask, lp->lasttrans_dest);
678                 lp->last_timeout = jiffies;
679         }
680
681         if (lp->cur_tx == -1)
682                 netif_wake_queue(dev);
683 }
684
685
686 /*
687  * The typical workload of the driver: Handle the network interface
688  * interrupts. Establish which device needs attention, and call the correct
689  * chipset interrupt handler.
690  */
691 irqreturn_t arcnet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
692 {
693         struct net_device *dev = dev_id;
694         struct arcnet_local *lp;
695         int recbuf, status, didsomething, boguscount;
696
697         BUGMSG(D_DURING, "\n");
698
699         BUGMSG(D_DURING, "in arcnet_interrupt\n");
700
701         spin_lock(&arcnet_lock);
702         
703         lp = (struct arcnet_local *) dev->priv;
704         if (!lp)
705                 BUG();
706                 
707         /*
708          * RESET flag was enabled - if device is not running, we must clear it right
709          * away (but nothing else).
710          */
711         if (!netif_running(dev)) {
712                 if (ASTATUS() & RESETflag)
713                         ACOMMAND(CFLAGScmd | RESETclear);
714                 AINTMASK(0);
715                 spin_unlock(&arcnet_lock);
716                 return IRQ_HANDLED;
717         }
718
719         BUGMSG(D_DURING, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
720                ASTATUS(), lp->intmask);
721
722         boguscount = 5;
723         do {
724                 status = ASTATUS();
725                 didsomething = 0;
726
727                 /*
728                  * RESET flag was enabled - card is resetting and if RX is
729                  * disabled, it's NOT because we just got a packet.
730                  * 
731                  * The card is in an undefined state.  Clear it out and start over.
732                  */
733                 if (status & RESETflag) {
734                         BUGMSG(D_NORMAL, "spurious reset (status=%Xh)\n", status);
735                         arcnet_close(dev);
736                         arcnet_open(dev);
737
738                         /* get out of the interrupt handler! */
739                         break;
740                 }
741                 /* 
742                  * RX is inhibited - we must have received something. Prepare to
743                  * receive into the next buffer.
744                  * 
745                  * We don't actually copy the received packet from the card until
746                  * after the transmit handler runs (and possibly launches the next
747                  * tx); this should improve latency slightly if we get both types
748                  * of interrupts at once. 
749                  */
750                 recbuf = -1;
751                 if (status & lp->intmask & NORXflag) {
752                         recbuf = lp->cur_rx;
753                         BUGMSG(D_DURING, "Buffer #%d: receive irq (status=%Xh)\n",
754                                recbuf, status);
755
756                         lp->cur_rx = get_arcbuf(dev);
757                         if (lp->cur_rx != -1) {
758                                 BUGMSG(D_DURING, "enabling receive to buffer #%d\n",
759                                        lp->cur_rx);
760                                 ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
761                         }
762                         didsomething++;
763                 }
764                 /* a transmit finished, and we're interested in it. */
765                 if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
766                         lp->intmask &= ~TXFREEflag;
767
768                         BUGMSG(D_DURING, "TX IRQ (stat=%Xh)\n", status);
769
770                         if (lp->cur_tx != -1 && !(status & TXACKflag) && !lp->timed_out) {
771                                 if (lp->lasttrans_dest != 0) {
772                                         BUGMSG(D_EXTRA, "transmit was not acknowledged! "
773                                             "(status=%Xh, dest=%02Xh)\n",
774                                              status, lp->lasttrans_dest);
775                                         lp->stats.tx_errors++;
776                                         lp->stats.tx_carrier_errors++;
777                                 } else {
778                                         BUGMSG(D_DURING,
779                                                "broadcast was not acknowledged; that's normal "
780                                             "(status=%Xh, dest=%02Xh)\n",
781                                              status, lp->lasttrans_dest);
782                                 }
783                         }
784                         if (lp->cur_tx != -1)
785                                 release_arcbuf(dev, lp->cur_tx);
786
787                         lp->cur_tx = -1;
788                         lp->timed_out = 0;
789                         didsomething++;
790
791                         /* send another packet if there is one */
792                         go_tx(dev);
793
794                         /* continue a split packet, if any */
795                         if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
796                                 int txbuf = get_arcbuf(dev);
797                                 if (txbuf != -1) {
798                                         if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
799                                                 /* that was the last segment */
800                                                 lp->stats.tx_bytes += lp->outgoing.skb->len;
801                                                 dev_kfree_skb_irq(lp->outgoing.skb);
802                                                 lp->outgoing.proto = NULL;
803                                         }
804                                         lp->next_tx = txbuf;
805                                 }
806                         }
807                         /* inform upper layers of idleness, if necessary */
808                         if (lp->cur_tx == -1)
809                                 netif_wake_queue(dev);
810                 }
811                 /* now process the received packet, if any */
812                 if (recbuf != -1) {
813                         BUGLVL(D_RX) arcnet_dump_packet(dev, recbuf, "rx irq");
814
815                         arcnet_rx(dev, recbuf);
816                         release_arcbuf(dev, recbuf);
817
818                         didsomething++;
819                 }
820                 if (status & lp->intmask & RECONflag) {
821                         ACOMMAND(CFLAGScmd | CONFIGclear);
822                         lp->stats.tx_carrier_errors++;
823
824                         BUGMSG(D_RECON, "Network reconfiguration detected (status=%Xh)\n",
825                                status);
826
827                         /* is the RECON info empty or old? */
828                         if (!lp->first_recon || !lp->last_recon ||
829                             jiffies - lp->last_recon > HZ * 10) {
830                                 if (lp->network_down)
831                                         BUGMSG(D_NORMAL, "reconfiguration detected: cabling restored?\n");
832                                 lp->first_recon = lp->last_recon = jiffies;
833                                 lp->num_recons = lp->network_down = 0;
834
835                                 BUGMSG(D_DURING, "recon: clearing counters.\n");
836                         } else {        /* add to current RECON counter */
837                                 lp->last_recon = jiffies;
838                                 lp->num_recons++;
839
840                                 BUGMSG(D_DURING, "recon: counter=%d, time=%lds, net=%d\n",
841                                        lp->num_recons,
842                                  (lp->last_recon - lp->first_recon) / HZ,
843                                        lp->network_down);
844
845                                 /* if network is marked up;
846                                  * and first_recon and last_recon are 60+ apart;
847                                  * and the average no. of recons counted is
848                                  *    > RECON_THRESHOLD/min;
849                                  * then print a warning message.
850                                  */
851                                 if (!lp->network_down
852                                     && (lp->last_recon - lp->first_recon) <= HZ * 60
853                                   && lp->num_recons >= RECON_THRESHOLD) {
854                                         lp->network_down = 1;
855                                         BUGMSG(D_NORMAL, "many reconfigurations detected: cabling problem?\n");
856                                 } else if (!lp->network_down
857                                            && lp->last_recon - lp->first_recon > HZ * 60) {
858                                         /* reset counters if we've gone for over a minute. */
859                                         lp->first_recon = lp->last_recon;
860                                         lp->num_recons = 1;
861                                 }
862                         }
863                 } else if (lp->network_down && jiffies - lp->last_recon > HZ * 10) {
864                         if (lp->network_down)
865                                 BUGMSG(D_NORMAL, "cabling restored?\n");
866                         lp->first_recon = lp->last_recon = 0;
867                         lp->num_recons = lp->network_down = 0;
868
869                         BUGMSG(D_DURING, "not recon: clearing counters anyway.\n");
870                 }
871         }
872         while (--boguscount && didsomething);
873
874         BUGMSG(D_DURING, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
875                ASTATUS(), boguscount);
876         BUGMSG(D_DURING, "\n");
877
878
879         AINTMASK(0);
880         udelay(1);
881         AINTMASK(lp->intmask);
882         
883         spin_unlock(&arcnet_lock);
884         return IRQ_RETVAL(didsomething);
885 }
886
887
888 /*
889  * This is a generic packet receiver that calls arcnet??_rx depending on the
890  * protocol ID found.
891  */
892 void arcnet_rx(struct net_device *dev, int bufnum)
893 {
894         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
895         struct archdr pkt;
896         struct arc_rfc1201 *soft;
897         int length, ofs;
898
899         soft = &pkt.soft.rfc1201;
900
901         lp->hw.copy_from_card(dev, bufnum, 0, &pkt, sizeof(ARC_HDR_SIZE));
902         if (pkt.hard.offset[0]) {
903                 ofs = pkt.hard.offset[0];
904                 length = 256 - ofs;
905         } else {
906                 ofs = pkt.hard.offset[1];
907                 length = 512 - ofs;
908         }
909
910         /* get the full header, if possible */
911         if (sizeof(pkt.soft) < length)
912                 lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
913         else {
914                 memset(&pkt.soft, 0, sizeof(pkt.soft));
915                 lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
916         }
917
918         BUGMSG(D_DURING, "Buffer #%d: received packet from %02Xh to %02Xh "
919                "(%d+4 bytes)\n",
920                bufnum, pkt.hard.source, pkt.hard.dest, length);
921
922         lp->stats.rx_packets++;
923         lp->stats.rx_bytes += length + ARC_HDR_SIZE;
924
925         /* call the right receiver for the protocol */
926         if (arc_proto_map[soft->proto] != &arc_proto_null) {
927                 BUGLVL(D_PROTO) {
928                         struct ArcProto
929                         *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
930                         *newp = arc_proto_map[soft->proto];
931
932                         if (oldp != newp) {
933                                 BUGMSG(D_PROTO,
934                                        "got protocol %02Xh; encap for host %02Xh is now '%c'"
935                                        " (was '%c')\n", soft->proto, pkt.hard.source,
936                                        newp->suffix, oldp->suffix);
937                         }
938                 }
939
940                 /* broadcasts will always be done with the last-used encap. */
941                 lp->default_proto[0] = soft->proto;
942
943                 /* in striking contrast, the following isn't a hack. */
944                 lp->default_proto[pkt.hard.source] = soft->proto;
945         }
946         /* call the protocol-specific receiver. */
947         arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
948 }
949
950
951
952 /* 
953  * Get the current statistics.  This may be called with the card open or
954  * closed.
955  */
956 static struct net_device_stats *arcnet_get_stats(struct net_device *dev)
957 {
958         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
959         return &lp->stats;
960 }
961
962
963 static void null_rx(struct net_device *dev, int bufnum,
964                     struct archdr *pkthdr, int length)
965 {
966         BUGMSG(D_PROTO,
967         "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
968                pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
969 }
970
971
972 static int null_build_header(struct sk_buff *skb, struct net_device *dev,
973                              unsigned short type, uint8_t daddr)
974 {
975         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
976
977         BUGMSG(D_PROTO,
978                "tx: can't build header for encap %02Xh; load a protocol driver.\n",
979                lp->default_proto[daddr]);
980
981         /* always fails */
982         return 0;
983 }
984
985
986 /* the "do nothing" prepare_tx function warns that there's nothing to do. */
987 static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
988                            int length, int bufnum)
989 {
990         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
991         struct arc_hardware newpkt;
992
993         BUGMSG(D_PROTO, "tx: no encap for this host; load a protocol driver.\n");
994
995         /* send a packet to myself -- will never get received, of course */
996         newpkt.source = newpkt.dest = dev->dev_addr[0];
997
998         /* only one byte of actual data (and it's random) */
999         newpkt.offset[0] = 0xFF;
1000
1001         lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1002
1003         return 1;               /* done */
1004 }