2 * Linux ARCnet driver - device-independent routines
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.
9 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
10 * for sponsoring the further development of this driver.
12 * **********************
14 * The original copyright was as follows:
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.
22 * **********************
24 * The change log is now in a file called ChangeLog in this directory.
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
44 #define VERSION "arcnet: v3.93 BETA 2000/04/29 - by Avery Pennarun et al.\n"
46 #include <linux/module.h>
47 #include <linux/config.h>
48 #include <linux/types.h>
49 #include <linux/delay.h>
50 #include <linux/netdevice.h>
51 #include <linux/if_arp.h>
53 #include <linux/init.h>
54 #include <linux/arcdevice.h>
55 #include <linux/jiffies.h>
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);
65 static void arcnet_rx(struct net_device *dev, int bufnum);
68 * one ArcProto per possible proto ID. None of the elements of
69 * arc_proto_map are allowed to be NULL; they will get set to
70 * arc_proto_default instead. It also must not be NULL; if you would like
71 * to set it to NULL, set it to &arc_proto_null instead.
73 struct ArcProto *arc_proto_map[256], *arc_proto_default,
74 *arc_bcast_proto, *arc_raw_proto;
76 static struct ArcProto arc_proto_null =
82 .build_header = null_build_header,
83 .prepare_tx = null_prepare_tx,
88 /* Exported function prototypes */
89 int arcnet_debug = ARCNET_DEBUG;
91 EXPORT_SYMBOL(arc_proto_map);
92 EXPORT_SYMBOL(arc_proto_default);
93 EXPORT_SYMBOL(arc_bcast_proto);
94 EXPORT_SYMBOL(arc_raw_proto);
95 EXPORT_SYMBOL(arcnet_unregister_proto);
96 EXPORT_SYMBOL(arcnet_debug);
97 EXPORT_SYMBOL(alloc_arcdev);
98 EXPORT_SYMBOL(arcnet_interrupt);
100 /* Internal function prototypes */
101 static int arcnet_open(struct net_device *dev);
102 static int arcnet_close(struct net_device *dev);
103 static int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev);
104 static void arcnet_timeout(struct net_device *dev);
105 static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
106 unsigned short type, void *daddr, void *saddr,
108 static int arcnet_rebuild_header(struct sk_buff *skb);
109 static struct net_device_stats *arcnet_get_stats(struct net_device *dev);
110 static int go_tx(struct net_device *dev);
112 static int debug = ARCNET_DEBUG;
113 module_param(debug, int, 0);
114 MODULE_LICENSE("GPL");
116 static int __init arcnet_init(void)
120 arcnet_debug = debug;
122 printk("arcnet loaded.\n");
126 printk("arcnet: ***\n"
127 "arcnet: * Read arcnet.txt for important release notes!\n"
129 "arcnet: * This is an ALPHA version! (Last stable release: v3.02) E-mail\n"
130 "arcnet: * me if you have any questions, comments, or bug reports.\n"
135 /* initialize the protocol map */
136 arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
137 for (count = 0; count < 256; count++)
138 arc_proto_map[count] = arc_proto_default;
141 printk("arcnet: struct sizes: %Zd %Zd %Zd %Zd %Zd\n",
142 sizeof(struct arc_hardware), sizeof(struct arc_rfc1201),
143 sizeof(struct arc_rfc1051), sizeof(struct arc_eth_encap),
144 sizeof(struct archdr));
149 static void __exit arcnet_exit(void)
153 module_init(arcnet_init);
154 module_exit(arcnet_exit);
157 * Dump the contents of an sk_buff
159 #if ARCNET_DEBUG_MAX & D_SKB
160 void arcnet_dump_skb(struct net_device *dev,
161 struct sk_buff *skb, char *desc)
165 printk(KERN_DEBUG "%6s: skb dump (%s) follows:", dev->name, desc);
166 for (i = 0; i < skb->len; i++) {
168 printk("\n" KERN_DEBUG "[%04X] ", i);
169 printk("%02X ", ((u_char *) skb->data)[i]);
174 EXPORT_SYMBOL(arcnet_dump_skb);
179 * Dump the contents of an ARCnet buffer
181 #if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
182 static void arcnet_dump_packet(struct net_device *dev, int bufnum,
183 char *desc, int take_arcnet_lock)
185 struct arcnet_local *lp = dev->priv;
187 unsigned long flags = 0;
188 static uint8_t buf[512];
190 /* hw.copy_from_card expects IRQ context so take the IRQ lock
191 to keep it single threaded */
193 spin_lock_irqsave(&lp->lock, flags);
195 lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
197 spin_unlock_irqrestore(&lp->lock, flags);
199 /* if the offset[0] byte is nonzero, this is a 256-byte packet */
200 length = (buf[2] ? 256 : 512);
202 printk(KERN_DEBUG "%6s: packet dump (%s) follows:", dev->name, desc);
203 for (i = 0; i < length; i++) {
205 printk("\n" KERN_DEBUG "[%04X] ", i);
206 printk("%02X ", buf[i]);
214 #define arcnet_dump_packet(dev, bufnum, desc,take_arcnet_lock) do { } while (0)
220 * Unregister a protocol driver from the arc_proto_map. Protocol drivers
221 * are responsible for registering themselves, but the unregister routine
222 * is pretty generic so we'll do it here.
224 void arcnet_unregister_proto(struct ArcProto *proto)
228 if (arc_proto_default == proto)
229 arc_proto_default = &arc_proto_null;
230 if (arc_bcast_proto == proto)
231 arc_bcast_proto = arc_proto_default;
232 if (arc_raw_proto == proto)
233 arc_raw_proto = arc_proto_default;
235 for (count = 0; count < 256; count++) {
236 if (arc_proto_map[count] == proto)
237 arc_proto_map[count] = arc_proto_default;
243 * Add a buffer to the queue. Only the interrupt handler is allowed to do
244 * this, unless interrupts are disabled.
246 * Note: we don't check for a full queue, since there aren't enough buffers
247 * to more than fill it.
249 static void release_arcbuf(struct net_device *dev, int bufnum)
251 struct arcnet_local *lp = dev->priv;
254 lp->buf_queue[lp->first_free_buf++] = bufnum;
255 lp->first_free_buf %= 5;
258 BUGMSG(D_DURING, "release_arcbuf: freed #%d; buffer queue is now: ",
260 for (i = lp->next_buf; i != lp->first_free_buf; i = (i+1) % 5)
261 BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
262 BUGMSG2(D_DURING, "\n");
268 * Get a buffer from the queue. If this returns -1, there are no buffers
271 static int get_arcbuf(struct net_device *dev)
273 struct arcnet_local *lp = dev->priv;
276 if (!atomic_dec_and_test(&lp->buf_lock)) {
277 /* already in this function */
278 BUGMSG(D_NORMAL, "get_arcbuf: overlap (%d)!\n",
279 lp->buf_lock.counter);
281 else { /* we can continue */
282 if (lp->next_buf >= 5)
285 if (lp->next_buf == lp->first_free_buf)
286 BUGMSG(D_NORMAL, "get_arcbuf: BUG: no buffers are available??\n");
288 buf = lp->buf_queue[lp->next_buf++];
295 BUGMSG(D_DURING, "get_arcbuf: got #%d; buffer queue is now: ", buf);
296 for (i = lp->next_buf; i != lp->first_free_buf; i = (i+1) % 5)
297 BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
298 BUGMSG2(D_DURING, "\n");
301 atomic_inc(&lp->buf_lock);
306 static int choose_mtu(void)
308 int count, mtu = 65535;
310 /* choose the smallest MTU of all available encaps */
311 for (count = 0; count < 256; count++) {
312 if (arc_proto_map[count] != &arc_proto_null
313 && arc_proto_map[count]->mtu < mtu) {
314 mtu = arc_proto_map[count]->mtu;
318 return mtu == 65535 ? XMTU : mtu;
322 /* Setup a struct device for ARCnet. */
323 static void arcdev_setup(struct net_device *dev)
325 dev->type = ARPHRD_ARCNET;
326 dev->hard_header_len = sizeof(struct archdr);
327 dev->mtu = choose_mtu();
329 dev->addr_len = ARCNET_ALEN;
330 dev->tx_queue_len = 100;
331 dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
332 dev->watchdog_timeo = TX_TIMEOUT;
334 /* New-style flags. */
335 dev->flags = IFF_BROADCAST;
338 * Put in this stuff here, so we don't have to export the symbols to
339 * the chipset drivers.
341 dev->open = arcnet_open;
342 dev->stop = arcnet_close;
343 dev->hard_start_xmit = arcnet_send_packet;
344 dev->tx_timeout = arcnet_timeout;
345 dev->get_stats = arcnet_get_stats;
346 dev->hard_header = arcnet_header;
347 dev->rebuild_header = arcnet_rebuild_header;
350 struct net_device *alloc_arcdev(char *name)
352 struct net_device *dev;
354 dev = alloc_netdev(sizeof(struct arcnet_local),
355 name && *name ? name : "arc%d", arcdev_setup);
357 struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
358 spin_lock_init(&lp->lock);
365 * Open/initialize the board. This is called sometime after booting when
366 * the 'ifconfig' program is run.
368 * This routine should set everything up anew at each open, even registers
369 * that "should" only need to be set once at boot, so that there is
370 * non-reboot way to recover if something goes wrong.
372 static int arcnet_open(struct net_device *dev)
374 struct arcnet_local *lp = dev->priv;
375 int count, newmtu, error;
377 BUGMSG(D_INIT,"opened.");
379 if (!try_module_get(lp->hw.owner))
384 BUGMSG(D_PROTO, "protocol map (default is '%c'): ",
385 arc_proto_default->suffix);
386 for (count = 0; count < 256; count++)
387 BUGMSG2(D_PROTO, "%c", arc_proto_map[count]->suffix);
388 BUGMSG2(D_PROTO, "\n");
392 BUGMSG(D_INIT, "arcnet_open: resetting card.\n");
394 /* try to put the card in a defined state - if it fails the first
395 * time, actually reset it.
398 if (ARCRESET(0) && ARCRESET(1))
401 newmtu = choose_mtu();
402 if (newmtu < dev->mtu)
405 BUGMSG(D_INIT, "arcnet_open: mtu: %d.\n", dev->mtu);
407 /* autodetect the encapsulation for each host. */
408 memset(lp->default_proto, 0, sizeof(lp->default_proto));
410 /* the broadcast address is special - use the 'bcast' protocol */
411 for (count = 0; count < 256; count++) {
412 if (arc_proto_map[count] == arc_bcast_proto) {
413 lp->default_proto[0] = count;
418 /* initialize buffers */
419 atomic_set(&lp->buf_lock, 1);
421 lp->next_buf = lp->first_free_buf = 0;
422 release_arcbuf(dev, 0);
423 release_arcbuf(dev, 1);
424 release_arcbuf(dev, 2);
425 release_arcbuf(dev, 3);
426 lp->cur_tx = lp->next_tx = -1;
429 lp->rfc1201.sequence = 1;
431 /* bring up the hardware driver */
435 if (dev->dev_addr[0] == 0)
436 BUGMSG(D_NORMAL, "WARNING! Station address 00 is reserved "
437 "for broadcasts!\n");
438 else if (dev->dev_addr[0] == 255)
439 BUGMSG(D_NORMAL, "WARNING! Station address FF may confuse "
440 "DOS networking programs!\n");
442 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
443 if (ASTATUS() & RESETflag) {
444 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
445 ACOMMAND(CFLAGScmd | RESETclear);
449 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
450 /* make sure we're ready to receive IRQ's. */
452 udelay(1); /* give it time to set the mask before
453 * we reset it again. (may not even be
456 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
457 lp->intmask = NORXflag | RECONflag;
458 AINTMASK(lp->intmask);
459 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
461 netif_start_queue(dev);
466 module_put(lp->hw.owner);
471 /* The inverse routine to arcnet_open - shuts down the card. */
472 static int arcnet_close(struct net_device *dev)
474 struct arcnet_local *lp = dev->priv;
476 netif_stop_queue(dev);
478 /* flush TX and disable RX */
480 ACOMMAND(NOTXcmd); /* stop transmit */
481 ACOMMAND(NORXcmd); /* disable receive */
484 /* shut down the card */
486 module_put(lp->hw.owner);
491 static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
492 unsigned short type, void *daddr, void *saddr,
495 struct arcnet_local *lp = dev->priv;
496 uint8_t _daddr, proto_num;
497 struct ArcProto *proto;
500 "create header from %d to %d; protocol %d (%Xh); size %u.\n",
501 saddr ? *(uint8_t *) saddr : -1,
502 daddr ? *(uint8_t *) daddr : -1,
505 if (skb->len!=0 && len != skb->len)
506 BUGMSG(D_NORMAL, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
510 /* Type is host order - ? */
511 if(type == ETH_P_ARCNET) {
512 proto = arc_raw_proto;
513 BUGMSG(D_DEBUG, "arc_raw_proto used. proto='%c'\n",proto->suffix);
514 _daddr = daddr ? *(uint8_t *) daddr : 0;
518 * if the dest addr isn't provided, we can't choose an encapsulation!
519 * Store the packet type (eg. ETH_P_IP) for now, and we'll push on a
520 * real header when we do rebuild_header.
522 *(uint16_t *) skb_push(skb, 2) = type;
523 if (skb->nh.raw - skb->mac.raw != 2)
524 BUGMSG(D_NORMAL, "arcnet_header: Yikes! diff (%d) is not 2!\n",
525 (int)(skb->nh.raw - skb->mac.raw));
526 return -2; /* return error -- can't transmit yet! */
529 /* otherwise, we can just add the header as usual. */
530 _daddr = *(uint8_t *) daddr;
531 proto_num = lp->default_proto[_daddr];
532 proto = arc_proto_map[proto_num];
533 BUGMSG(D_DURING, "building header for %02Xh using protocol '%c'\n",
534 proto_num, proto->suffix);
535 if (proto == &arc_proto_null && arc_bcast_proto != proto) {
536 BUGMSG(D_DURING, "actually, let's use '%c' instead.\n",
537 arc_bcast_proto->suffix);
538 proto = arc_bcast_proto;
541 return proto->build_header(skb, dev, type, _daddr);
546 * Rebuild the ARCnet hard header. This is called after an ARP (or in the
547 * future other address resolution) has completed on this sk_buff. We now
548 * let ARP fill in the destination field.
550 static int arcnet_rebuild_header(struct sk_buff *skb)
552 struct net_device *dev = skb->dev;
553 struct arcnet_local *lp = dev->priv;
554 int status = 0; /* default is failure */
557 struct ArcProto *proto;
559 if (skb->nh.raw - skb->mac.raw != 2) {
561 "rebuild_header: shouldn't be here! (hdrsize=%d)\n",
562 (int)(skb->nh.raw - skb->mac.raw));
565 type = *(uint16_t *) skb_pull(skb, 2);
566 BUGMSG(D_DURING, "rebuild header for protocol %Xh\n", type);
568 if (type == ETH_P_IP) {
570 BUGMSG(D_DURING, "rebuild header for ethernet protocol %Xh\n", type);
571 status = arp_find(&daddr, skb) ? 1 : 0;
572 BUGMSG(D_DURING, " rebuilt: dest is %d; protocol %Xh\n",
577 "I don't understand ethernet protocol %Xh addresses!\n", type);
578 lp->stats.tx_errors++;
579 lp->stats.tx_aborted_errors++;
582 /* if we couldn't resolve the address... give up. */
586 /* add the _real_ header this time! */
587 proto = arc_proto_map[lp->default_proto[daddr]];
588 proto->build_header(skb, dev, type, daddr);
590 return 1; /* success */
595 /* Called by the kernel in order to transmit a packet. */
596 static int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev)
598 struct arcnet_local *lp = dev->priv;
600 struct arc_rfc1201 *soft;
601 struct ArcProto *proto;
607 "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
608 ASTATUS(), lp->cur_tx, lp->next_tx, skb->len,skb->protocol);
610 pkt = (struct archdr *) skb->data;
611 soft = &pkt->soft.rfc1201;
612 proto = arc_proto_map[soft->proto];
614 BUGMSG(D_SKB_SIZE, "skb: transmitting %d bytes to %02X\n",
615 skb->len, pkt->hard.dest);
616 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "tx");
618 /* fits in one packet? */
619 if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
620 BUGMSG(D_NORMAL, "fixme: packet too large: compensating badly!\n");
622 return NETDEV_TX_OK; /* don't try again */
625 /* We're busy transmitting a packet... */
626 netif_stop_queue(dev);
628 spin_lock_irqsave(&lp->lock, flags);
630 if(lp->next_tx == -1)
631 txbuf = get_arcbuf(dev);
636 if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
638 /* done right away and we don't want to acknowledge
639 the package later - forget about it now */
640 lp->stats.tx_bytes += skb->len;
643 /* do it the 'split' way */
644 lp->outgoing.proto = proto;
645 lp->outgoing.skb = skb;
646 lp->outgoing.pkt = pkt;
650 if (proto->continue_tx &&
651 proto->continue_tx(dev, txbuf)) {
653 "bug! continue_tx finished the first time! "
654 "(proto='%c')\n", proto->suffix);
657 retval = NETDEV_TX_OK;
658 dev->trans_start = jiffies;
661 retval = NETDEV_TX_BUSY;
665 BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n",__FILE__,__LINE__,__FUNCTION__,ASTATUS());
666 /* make sure we didn't ignore a TX IRQ while we were in here */
669 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
670 lp->intmask |= TXFREEflag|EXCNAKflag;
671 AINTMASK(lp->intmask);
672 BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n",__FILE__,__LINE__,__FUNCTION__,ASTATUS());
674 spin_unlock_irqrestore(&lp->lock, flags);
678 return retval; /* no need to try again */
683 * Actually start transmitting a packet that was loaded into a buffer
684 * by prepare_tx. This should _only_ be called by the interrupt handler.
686 static int go_tx(struct net_device *dev)
688 struct arcnet_local *lp = dev->priv;
690 BUGMSG(D_DURING, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
691 ASTATUS(), lp->intmask, lp->next_tx, lp->cur_tx);
693 if (lp->cur_tx != -1 || lp->next_tx == -1)
696 BUGLVL(D_TX) arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
698 lp->cur_tx = lp->next_tx;
702 ACOMMAND(TXcmd | (lp->cur_tx << 3));
704 lp->stats.tx_packets++;
705 lp->lasttrans_dest = lp->lastload_dest;
706 lp->lastload_dest = 0;
707 lp->excnak_pending = 0;
708 lp->intmask |= TXFREEflag|EXCNAKflag;
714 /* Called by the kernel when transmit times out */
715 static void arcnet_timeout(struct net_device *dev)
718 struct arcnet_local *lp = dev->priv;
719 int status = ASTATUS();
722 spin_lock_irqsave(&lp->lock, flags);
723 if (status & TXFREEflag) { /* transmit _DID_ finish */
724 msg = " - missed IRQ?";
727 lp->stats.tx_aborted_errors++;
729 ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
731 lp->stats.tx_errors++;
733 /* make sure we didn't miss a TX or a EXC NAK IRQ */
735 lp->intmask |= TXFREEflag|EXCNAKflag;
736 AINTMASK(lp->intmask);
738 spin_unlock_irqrestore(&lp->lock, flags);
740 if (time_after(jiffies, lp->last_timeout + 10*HZ)) {
741 BUGMSG(D_EXTRA, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
742 msg, status, lp->intmask, lp->lasttrans_dest);
743 lp->last_timeout = jiffies;
746 if (lp->cur_tx == -1)
747 netif_wake_queue(dev);
752 * The typical workload of the driver: Handle the network interface
753 * interrupts. Establish which device needs attention, and call the correct
754 * chipset interrupt handler.
756 irqreturn_t arcnet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
758 struct net_device *dev = dev_id;
759 struct arcnet_local *lp;
760 int recbuf, status, diagstatus, didsomething, boguscount;
761 int retval = IRQ_NONE;
763 BUGMSG(D_DURING, "\n");
765 BUGMSG(D_DURING, "in arcnet_interrupt\n");
770 spin_lock(&lp->lock);
773 * RESET flag was enabled - if device is not running, we must clear it right
774 * away (but nothing else).
776 if (!netif_running(dev)) {
777 if (ASTATUS() & RESETflag)
778 ACOMMAND(CFLAGScmd | RESETclear);
780 spin_unlock(&lp->lock);
784 BUGMSG(D_DURING, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
785 ASTATUS(), lp->intmask);
790 diagstatus = (status >> 8) & 0xFF;
792 BUGMSG(D_DEBUG, "%s: %d: %s: status=%x\n",
793 __FILE__,__LINE__,__FUNCTION__,status);
797 * RESET flag was enabled - card is resetting and if RX is
798 * disabled, it's NOT because we just got a packet.
800 * The card is in an undefined state. Clear it out and start over.
802 if (status & RESETflag) {
803 BUGMSG(D_NORMAL, "spurious reset (status=%Xh)\n", status);
807 /* get out of the interrupt handler! */
811 * RX is inhibited - we must have received something. Prepare to
812 * receive into the next buffer.
814 * We don't actually copy the received packet from the card until
815 * after the transmit handler runs (and possibly launches the next
816 * tx); this should improve latency slightly if we get both types
817 * of interrupts at once.
820 if (status & lp->intmask & NORXflag) {
822 BUGMSG(D_DURING, "Buffer #%d: receive irq (status=%Xh)\n",
825 lp->cur_rx = get_arcbuf(dev);
826 if (lp->cur_rx != -1) {
827 BUGMSG(D_DURING, "enabling receive to buffer #%d\n",
829 ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
834 if((diagstatus & EXCNAKflag)) {
835 BUGMSG(D_DURING, "EXCNAK IRQ (diagstat=%Xh)\n",
838 ACOMMAND(NOTXcmd); /* disable transmit */
839 lp->excnak_pending = 1;
841 ACOMMAND(EXCNAKclear);
842 lp->intmask &= ~(EXCNAKflag);
847 /* a transmit finished, and we're interested in it. */
848 if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
849 lp->intmask &= ~(TXFREEflag|EXCNAKflag);
851 BUGMSG(D_DURING, "TX IRQ (stat=%Xh)\n", status);
853 if (lp->cur_tx != -1 && !lp->timed_out) {
854 if(!(status & TXACKflag)) {
855 if (lp->lasttrans_dest != 0) {
857 "transmit was not acknowledged! "
858 "(status=%Xh, dest=%02Xh)\n",
859 status, lp->lasttrans_dest);
860 lp->stats.tx_errors++;
861 lp->stats.tx_carrier_errors++;
864 "broadcast was not acknowledged; that's normal "
865 "(status=%Xh, dest=%02Xh)\n",
866 status, lp->lasttrans_dest);
870 if (lp->outgoing.proto &&
871 lp->outgoing.proto->ack_tx) {
873 if(status & TXACKflag)
875 else if(lp->excnak_pending)
881 ->ack_tx(dev, ackstatus);
884 if (lp->cur_tx != -1)
885 release_arcbuf(dev, lp->cur_tx);
891 /* send another packet if there is one */
894 /* continue a split packet, if any */
895 if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
896 int txbuf = get_arcbuf(dev);
898 if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
899 /* that was the last segment */
900 lp->stats.tx_bytes += lp->outgoing.skb->len;
901 if(!lp->outgoing.proto->ack_tx)
903 dev_kfree_skb_irq(lp->outgoing.skb);
904 lp->outgoing.proto = NULL;
910 /* inform upper layers of idleness, if necessary */
911 if (lp->cur_tx == -1)
912 netif_wake_queue(dev);
914 /* now process the received packet, if any */
916 BUGLVL(D_RX) arcnet_dump_packet(dev, recbuf, "rx irq", 0);
918 arcnet_rx(dev, recbuf);
919 release_arcbuf(dev, recbuf);
923 if (status & lp->intmask & RECONflag) {
924 ACOMMAND(CFLAGScmd | CONFIGclear);
925 lp->stats.tx_carrier_errors++;
927 BUGMSG(D_RECON, "Network reconfiguration detected (status=%Xh)\n",
929 /* MYRECON bit is at bit 7 of diagstatus */
930 if(diagstatus & 0x80)
931 BUGMSG(D_RECON,"Put out that recon myself\n");
933 /* is the RECON info empty or old? */
934 if (!lp->first_recon || !lp->last_recon ||
935 jiffies - lp->last_recon > HZ * 10) {
936 if (lp->network_down)
937 BUGMSG(D_NORMAL, "reconfiguration detected: cabling restored?\n");
938 lp->first_recon = lp->last_recon = jiffies;
939 lp->num_recons = lp->network_down = 0;
941 BUGMSG(D_DURING, "recon: clearing counters.\n");
942 } else { /* add to current RECON counter */
943 lp->last_recon = jiffies;
946 BUGMSG(D_DURING, "recon: counter=%d, time=%lds, net=%d\n",
948 (lp->last_recon - lp->first_recon) / HZ,
951 /* if network is marked up;
952 * and first_recon and last_recon are 60+ apart;
953 * and the average no. of recons counted is
954 * > RECON_THRESHOLD/min;
955 * then print a warning message.
957 if (!lp->network_down
958 && (lp->last_recon - lp->first_recon) <= HZ * 60
959 && lp->num_recons >= RECON_THRESHOLD) {
960 lp->network_down = 1;
961 BUGMSG(D_NORMAL, "many reconfigurations detected: cabling problem?\n");
962 } else if (!lp->network_down
963 && lp->last_recon - lp->first_recon > HZ * 60) {
964 /* reset counters if we've gone for over a minute. */
965 lp->first_recon = lp->last_recon;
969 } else if (lp->network_down && jiffies - lp->last_recon > HZ * 10) {
970 if (lp->network_down)
971 BUGMSG(D_NORMAL, "cabling restored?\n");
972 lp->first_recon = lp->last_recon = 0;
973 lp->num_recons = lp->network_down = 0;
975 BUGMSG(D_DURING, "not recon: clearing counters anyway.\n");
979 retval |= IRQ_HANDLED;
982 while (--boguscount && didsomething);
984 BUGMSG(D_DURING, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
985 ASTATUS(), boguscount);
986 BUGMSG(D_DURING, "\n");
991 AINTMASK(lp->intmask);
993 spin_unlock(&lp->lock);
999 * This is a generic packet receiver that calls arcnet??_rx depending on the
1000 * protocol ID found.
1002 static void arcnet_rx(struct net_device *dev, int bufnum)
1004 struct arcnet_local *lp = dev->priv;
1006 struct arc_rfc1201 *soft;
1009 soft = &pkt.soft.rfc1201;
1011 lp->hw.copy_from_card(dev, bufnum, 0, &pkt, sizeof(ARC_HDR_SIZE));
1012 if (pkt.hard.offset[0]) {
1013 ofs = pkt.hard.offset[0];
1016 ofs = pkt.hard.offset[1];
1020 /* get the full header, if possible */
1021 if (sizeof(pkt.soft) <= length)
1022 lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
1024 memset(&pkt.soft, 0, sizeof(pkt.soft));
1025 lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
1028 BUGMSG(D_DURING, "Buffer #%d: received packet from %02Xh to %02Xh "
1030 bufnum, pkt.hard.source, pkt.hard.dest, length);
1032 lp->stats.rx_packets++;
1033 lp->stats.rx_bytes += length + ARC_HDR_SIZE;
1035 /* call the right receiver for the protocol */
1036 if (arc_proto_map[soft->proto]->is_ip) {
1039 *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
1040 *newp = arc_proto_map[soft->proto];
1044 "got protocol %02Xh; encap for host %02Xh is now '%c'"
1045 " (was '%c')\n", soft->proto, pkt.hard.source,
1046 newp->suffix, oldp->suffix);
1050 /* broadcasts will always be done with the last-used encap. */
1051 lp->default_proto[0] = soft->proto;
1053 /* in striking contrast, the following isn't a hack. */
1054 lp->default_proto[pkt.hard.source] = soft->proto;
1056 /* call the protocol-specific receiver. */
1057 arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
1063 * Get the current statistics. This may be called with the card open or
1066 static struct net_device_stats *arcnet_get_stats(struct net_device *dev)
1068 struct arcnet_local *lp = dev->priv;
1073 static void null_rx(struct net_device *dev, int bufnum,
1074 struct archdr *pkthdr, int length)
1077 "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
1078 pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
1082 static int null_build_header(struct sk_buff *skb, struct net_device *dev,
1083 unsigned short type, uint8_t daddr)
1085 struct arcnet_local *lp = dev->priv;
1088 "tx: can't build header for encap %02Xh; load a protocol driver.\n",
1089 lp->default_proto[daddr]);
1096 /* the "do nothing" prepare_tx function warns that there's nothing to do. */
1097 static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
1098 int length, int bufnum)
1100 struct arcnet_local *lp = dev->priv;
1101 struct arc_hardware newpkt;
1103 BUGMSG(D_PROTO, "tx: no encap for this host; load a protocol driver.\n");
1105 /* send a packet to myself -- will never get received, of course */
1106 newpkt.source = newpkt.dest = dev->dev_addr[0];
1108 /* only one byte of actual data (and it's random) */
1109 newpkt.offset[0] = 0xFF;
1111 lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1113 return 1; /* done */