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