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