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