xref: /linux/drivers/net/arcnet/arcnet.c (revision de2fe5e07d58424bc286fff3fd3c1b0bf933cd58)
1 /*
2  * Linux ARCnet driver - device-independent routines
3  *
4  * Written 1997 by David Woodhouse.
5  * Written 1994-1999 by Avery Pennarun.
6  * Written 1999-2000 by Martin Mares <mj@ucw.cz>.
7  * Derived from skeleton.c by Donald Becker.
8  *
9  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
10  *  for sponsoring the further development of this driver.
11  *
12  * **********************
13  *
14  * The original copyright was as follows:
15  *
16  * skeleton.c Written 1993 by Donald Becker.
17  * Copyright 1993 United States Government as represented by the
18  * Director, National Security Agency.  This software may only be used
19  * and distributed according to the terms of the GNU General Public License as
20  * modified by SRC, incorporated herein by reference.
21  *
22  * **********************
23  *
24  * The change log is now in a file called ChangeLog in this directory.
25  *
26  * Sources:
27  *  - Crynwr arcnet.com/arcether.com packet drivers.
28  *  - arcnet.c v0.00 dated 1/1/94 and apparently by
29  *     Donald Becker - it didn't work :)
30  *  - skeleton.c v0.05 dated 11/16/93 by Donald Becker
31  *     (from Linux Kernel 1.1.45)
32  *  - RFC's 1201 and 1051 - re: TCP/IP over ARCnet
33  *  - The official ARCnet COM9026 data sheets (!) thanks to
34  *     Ken Cornetet <kcornete@nyx10.cs.du.edu>
35  *  - The official ARCnet COM20020 data sheets.
36  *  - Information on some more obscure ARCnet controller chips, thanks
37  *     to the nice people at SMSC.
38  *  - net/inet/eth.c (from kernel 1.1.50) for header-building info.
39  *  - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
40  *  - Textual information and more alternate source from Joachim Koenig
41  *     <jojo@repas.de>
42  */
43 
44 #define VERSION "arcnet: v3.93 BETA 2000/04/29 - by Avery Pennarun et al.\n"
45 
46 #include <linux/module.h>
47 #include <linux/config.h>
48 #include <linux/types.h>
49 #include <linux/delay.h>
50 #include <linux/netdevice.h>
51 #include <linux/if_arp.h>
52 #include <net/arp.h>
53 #include <linux/init.h>
54 #include <linux/arcdevice.h>
55 #include <linux/jiffies.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 /*
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.
72  */
73  struct ArcProto *arc_proto_map[256], *arc_proto_default,
74    *arc_bcast_proto, *arc_raw_proto;
75 
76 static struct ArcProto arc_proto_null =
77 {
78 	.suffix		= '?',
79 	.mtu		= XMTU,
80 	.is_ip          = 0,
81 	.rx		= null_rx,
82 	.build_header	= null_build_header,
83 	.prepare_tx	= null_prepare_tx,
84 	.continue_tx    = NULL,
85 	.ack_tx         = NULL
86 };
87 
88 /* Exported function prototypes */
89 int arcnet_debug = ARCNET_DEBUG;
90 
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);
99 
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,
107 			 unsigned len);
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);
111 
112 static int debug = ARCNET_DEBUG;
113 module_param(debug, int, 0);
114 MODULE_LICENSE("GPL");
115 
116 static int __init arcnet_init(void)
117 {
118 	int count;
119 
120 	arcnet_debug = debug;
121 
122 	printk("arcnet loaded.\n");
123 
124 #ifdef ALPHA_WARNING
125 	BUGLVL(D_EXTRA) {
126 		printk("arcnet: ***\n"
127 		"arcnet: * Read arcnet.txt for important release notes!\n"
128 		       "arcnet: *\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"
131 		       "arcnet: ***\n");
132 	}
133 #endif
134 
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;
139 
140 	BUGLVL(D_DURING)
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));
145 
146 	return 0;
147 }
148 
149 static void __exit arcnet_exit(void)
150 {
151 }
152 
153 module_init(arcnet_init);
154 module_exit(arcnet_exit);
155 
156 /*
157  * Dump the contents of an sk_buff
158  */
159 #if ARCNET_DEBUG_MAX & D_SKB
160 void arcnet_dump_skb(struct net_device *dev,
161 		     struct sk_buff *skb, char *desc)
162 {
163 	int i;
164 
165 	printk(KERN_DEBUG "%6s: skb dump (%s) follows:", dev->name, desc);
166 	for (i = 0; i < skb->len; i++) {
167 		if (i % 16 == 0)
168 			printk("\n" KERN_DEBUG "[%04X] ", i);
169 		printk("%02X ", ((u_char *) skb->data)[i]);
170 	}
171 	printk("\n");
172 }
173 
174 EXPORT_SYMBOL(arcnet_dump_skb);
175 #endif
176 
177 
178 /*
179  * Dump the contents of an ARCnet buffer
180  */
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)
184 {
185 	struct arcnet_local *lp = dev->priv;
186 	int i, length;
187 	unsigned long flags = 0;
188 	static uint8_t buf[512];
189 
190 	/* hw.copy_from_card expects IRQ context so take the IRQ lock
191 	   to keep it single threaded */
192 	if(take_arcnet_lock)
193 		spin_lock_irqsave(&lp->lock, flags);
194 
195 	lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
196 	if(take_arcnet_lock)
197 		spin_unlock_irqrestore(&lp->lock, flags);
198 
199 	/* if the offset[0] byte is nonzero, this is a 256-byte packet */
200 	length = (buf[2] ? 256 : 512);
201 
202 	printk(KERN_DEBUG "%6s: packet dump (%s) follows:", dev->name, desc);
203 	for (i = 0; i < length; i++) {
204 		if (i % 16 == 0)
205 			printk("\n" KERN_DEBUG "[%04X] ", i);
206 		printk("%02X ", buf[i]);
207 	}
208 	printk("\n");
209 
210 }
211 
212 #else
213 
214 #define arcnet_dump_packet(dev, bufnum, desc,take_arcnet_lock) do { } while (0)
215 
216 #endif
217 
218 
219 /*
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.
223  */
224 void arcnet_unregister_proto(struct ArcProto *proto)
225 {
226 	int count;
227 
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;
234 
235 	for (count = 0; count < 256; count++) {
236 		if (arc_proto_map[count] == proto)
237 			arc_proto_map[count] = arc_proto_default;
238 	}
239 }
240 
241 
242 /*
243  * Add a buffer to the queue.  Only the interrupt handler is allowed to do
244  * this, unless interrupts are disabled.
245  *
246  * Note: we don't check for a full queue, since there aren't enough buffers
247  * to more than fill it.
248  */
249 static void release_arcbuf(struct net_device *dev, int bufnum)
250 {
251 	struct arcnet_local *lp = dev->priv;
252 	int i;
253 
254 	lp->buf_queue[lp->first_free_buf++] = bufnum;
255 	lp->first_free_buf %= 5;
256 
257 	BUGLVL(D_DURING) {
258 		BUGMSG(D_DURING, "release_arcbuf: freed #%d; buffer queue is now: ",
259 		       bufnum);
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");
263 	}
264 }
265 
266 
267 /*
268  * Get a buffer from the queue.  If this returns -1, there are no buffers
269  * available.
270  */
271 static int get_arcbuf(struct net_device *dev)
272 {
273 	struct arcnet_local *lp = dev->priv;
274 	int buf = -1, i;
275 
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);
280 	}
281 	else {			/* we can continue */
282 		if (lp->next_buf >= 5)
283 			lp->next_buf -= 5;
284 
285 		if (lp->next_buf == lp->first_free_buf)
286 			BUGMSG(D_NORMAL, "get_arcbuf: BUG: no buffers are available??\n");
287 		else {
288 			buf = lp->buf_queue[lp->next_buf++];
289 			lp->next_buf %= 5;
290 		}
291 	}
292 
293 
294 	BUGLVL(D_DURING) {
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");
299 	}
300 
301 	atomic_inc(&lp->buf_lock);
302 	return buf;
303 }
304 
305 
306 static int choose_mtu(void)
307 {
308 	int count, mtu = 65535;
309 
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;
315 		}
316 	}
317 
318 	return mtu == 65535 ? XMTU : mtu;
319 }
320 
321 
322 /* Setup a struct device for ARCnet. */
323 static void arcdev_setup(struct net_device *dev)
324 {
325 	dev->type = ARPHRD_ARCNET;
326 	dev->hard_header_len = sizeof(struct archdr);
327 	dev->mtu = choose_mtu();
328 
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;
333 
334 	/* New-style flags. */
335 	dev->flags = IFF_BROADCAST;
336 
337 	/*
338 	 * Put in this stuff here, so we don't have to export the symbols to
339 	 * the chipset drivers.
340 	 */
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;
348 }
349 
350 struct net_device *alloc_arcdev(char *name)
351 {
352 	struct net_device *dev;
353 
354 	dev = alloc_netdev(sizeof(struct arcnet_local),
355 			   name && *name ? name : "arc%d", arcdev_setup);
356 	if(dev) {
357 		struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
358 		spin_lock_init(&lp->lock);
359 	}
360 
361 	return dev;
362 }
363 
364 /*
365  * Open/initialize the board.  This is called sometime after booting when
366  * the 'ifconfig' program is run.
367  *
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.
371  */
372 static int arcnet_open(struct net_device *dev)
373 {
374 	struct arcnet_local *lp = dev->priv;
375 	int count, newmtu, error;
376 
377 	BUGMSG(D_INIT,"opened.");
378 
379 	if (!try_module_get(lp->hw.owner))
380 		return -ENODEV;
381 
382 	BUGLVL(D_PROTO) {
383 		int count;
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");
389 	}
390 
391 
392 	BUGMSG(D_INIT, "arcnet_open: resetting card.\n");
393 
394 	/* try to put the card in a defined state - if it fails the first
395 	 * time, actually reset it.
396 	 */
397 	error = -ENODEV;
398 	if (ARCRESET(0) && ARCRESET(1))
399 		goto out_module_put;
400 
401 	newmtu = choose_mtu();
402 	if (newmtu < dev->mtu)
403 		dev->mtu = newmtu;
404 
405 	BUGMSG(D_INIT, "arcnet_open: mtu: %d.\n", dev->mtu);
406 
407 	/* autodetect the encapsulation for each host. */
408 	memset(lp->default_proto, 0, sizeof(lp->default_proto));
409 
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;
414 			break;
415 		}
416 	}
417 
418 	/* initialize buffers */
419 	atomic_set(&lp->buf_lock, 1);
420 
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;
427 	lp->cur_rx = -1;
428 
429 	lp->rfc1201.sequence = 1;
430 
431 	/* bring up the hardware driver */
432 	if (lp->hw.open)
433 		lp->hw.open(dev);
434 
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");
441 
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);
446 	}
447 
448 
449 	BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
450 	/* make sure we're ready to receive IRQ's. */
451 	AINTMASK(0);
452 	udelay(1);		/* give it time to set the mask before
453 				 * we reset it again. (may not even be
454 				 * necessary)
455 				 */
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__);
460 
461 	netif_start_queue(dev);
462 
463 	return 0;
464 
465  out_module_put:
466 	module_put(lp->hw.owner);
467 	return error;
468 }
469 
470 
471 /* The inverse routine to arcnet_open - shuts down the card. */
472 static int arcnet_close(struct net_device *dev)
473 {
474 	struct arcnet_local *lp = dev->priv;
475 
476 	netif_stop_queue(dev);
477 
478 	/* flush TX and disable RX */
479 	AINTMASK(0);
480 	ACOMMAND(NOTXcmd);	/* stop transmit */
481 	ACOMMAND(NORXcmd);	/* disable receive */
482 	mdelay(1);
483 
484 	/* shut down the card */
485 	lp->hw.close(dev);
486 	module_put(lp->hw.owner);
487 	return 0;
488 }
489 
490 
491 static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
492 			 unsigned short type, void *daddr, void *saddr,
493 			 unsigned len)
494 {
495 	struct arcnet_local *lp = dev->priv;
496 	uint8_t _daddr, proto_num;
497 	struct ArcProto *proto;
498 
499 	BUGMSG(D_DURING,
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,
503 	       type, type, len);
504 
505 	if (skb->len!=0 && len != skb->len)
506 		BUGMSG(D_NORMAL, "arcnet_header: Yikes!  skb->len(%d) != len(%d)!\n",
507 		       skb->len, len);
508 
509 
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;
515   	}
516 	else if (!daddr) {
517 		/*
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.
521 		 */
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! */
527 	}
528 	else {
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;
539 		}
540 	}
541 	return proto->build_header(skb, dev, type, _daddr);
542 }
543 
544 
545 /*
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.
549  */
550 static int arcnet_rebuild_header(struct sk_buff *skb)
551 {
552 	struct net_device *dev = skb->dev;
553 	struct arcnet_local *lp = dev->priv;
554 	int status = 0;		/* default is failure */
555 	unsigned short type;
556 	uint8_t daddr=0;
557 	struct ArcProto *proto;
558 
559 	if (skb->nh.raw - skb->mac.raw != 2) {
560 		BUGMSG(D_NORMAL,
561 		     "rebuild_header: shouldn't be here! (hdrsize=%d)\n",
562 		     (int)(skb->nh.raw - skb->mac.raw));
563 		return 0;
564 	}
565 	type = *(uint16_t *) skb_pull(skb, 2);
566 	BUGMSG(D_DURING, "rebuild header for protocol %Xh\n", type);
567 
568 	if (type == ETH_P_IP) {
569 #ifdef CONFIG_INET
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",
573 		       daddr, type);
574 #endif
575 	} else {
576 		BUGMSG(D_NORMAL,
577 		       "I don't understand ethernet protocol %Xh addresses!\n", type);
578 		lp->stats.tx_errors++;
579 		lp->stats.tx_aborted_errors++;
580 	}
581 
582 	/* if we couldn't resolve the address... give up. */
583 	if (!status)
584 		return 0;
585 
586 	/* add the _real_ header this time! */
587 	proto = arc_proto_map[lp->default_proto[daddr]];
588 	proto->build_header(skb, dev, type, daddr);
589 
590 	return 1;		/* success */
591 }
592 
593 
594 
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)
597 {
598 	struct arcnet_local *lp = dev->priv;
599 	struct archdr *pkt;
600 	struct arc_rfc1201 *soft;
601 	struct ArcProto *proto;
602 	int txbuf;
603 	unsigned long flags;
604 	int freeskb, retval;
605 
606 	BUGMSG(D_DURING,
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);
609 
610 	pkt = (struct archdr *) skb->data;
611 	soft = &pkt->soft.rfc1201;
612 	proto = arc_proto_map[soft->proto];
613 
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");
617 
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");
621 		dev_kfree_skb(skb);
622 		return NETDEV_TX_OK;	/* don't try again */
623 	}
624 
625 	/* We're busy transmitting a packet... */
626 	netif_stop_queue(dev);
627 
628 	spin_lock_irqsave(&lp->lock, flags);
629 	AINTMASK(0);
630 	if(lp->next_tx == -1)
631 		txbuf = get_arcbuf(dev);
632 	else {
633 		txbuf = -1;
634 	}
635 	if (txbuf != -1) {
636 		if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
637 		    !proto->ack_tx) {
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;
641 			freeskb = 1;
642 		} else {
643 			/* do it the 'split' way */
644 			lp->outgoing.proto = proto;
645 			lp->outgoing.skb = skb;
646 			lp->outgoing.pkt = pkt;
647 
648 			freeskb = 0;
649 
650 			if (proto->continue_tx &&
651 			    proto->continue_tx(dev, txbuf)) {
652 			  BUGMSG(D_NORMAL,
653 				 "bug! continue_tx finished the first time! "
654 				 "(proto='%c')\n", proto->suffix);
655 			}
656 		}
657 		retval = NETDEV_TX_OK;
658 		dev->trans_start = jiffies;
659 		lp->next_tx = txbuf;
660 	} else {
661 		retval = NETDEV_TX_BUSY;
662 		freeskb = 0;
663 	}
664 
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 */
667 	AINTMASK(0);
668 
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());
673 
674 	spin_unlock_irqrestore(&lp->lock, flags);
675 	if (freeskb) {
676 		dev_kfree_skb(skb);
677 	}
678 	return retval;		/* no need to try again */
679 }
680 
681 
682 /*
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.
685  */
686 static int go_tx(struct net_device *dev)
687 {
688 	struct arcnet_local *lp = dev->priv;
689 
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);
692 
693 	if (lp->cur_tx != -1 || lp->next_tx == -1)
694 		return 0;
695 
696 	BUGLVL(D_TX) arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
697 
698 	lp->cur_tx = lp->next_tx;
699 	lp->next_tx = -1;
700 
701 	/* start sending */
702 	ACOMMAND(TXcmd | (lp->cur_tx << 3));
703 
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;
709 
710 	return 1;
711 }
712 
713 
714 /* Called by the kernel when transmit times out */
715 static void arcnet_timeout(struct net_device *dev)
716 {
717 	unsigned long flags;
718 	struct arcnet_local *lp = dev->priv;
719 	int status = ASTATUS();
720 	char *msg;
721 
722 	spin_lock_irqsave(&lp->lock, flags);
723 	if (status & TXFREEflag) {	/* transmit _DID_ finish */
724 		msg = " - missed IRQ?";
725 	} else {
726 		msg = "";
727 		lp->stats.tx_aborted_errors++;
728 		lp->timed_out = 1;
729 		ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
730 	}
731 	lp->stats.tx_errors++;
732 
733 	/* make sure we didn't miss a TX or a EXC NAK IRQ */
734 	AINTMASK(0);
735 	lp->intmask |= TXFREEflag|EXCNAKflag;
736 	AINTMASK(lp->intmask);
737 
738 	spin_unlock_irqrestore(&lp->lock, flags);
739 
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;
744 	}
745 
746 	if (lp->cur_tx == -1)
747 		netif_wake_queue(dev);
748 }
749 
750 
751 /*
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.
755  */
756 irqreturn_t arcnet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
757 {
758 	struct net_device *dev = dev_id;
759 	struct arcnet_local *lp;
760 	int recbuf, status, diagstatus, didsomething, boguscount;
761 	int retval = IRQ_NONE;
762 
763 	BUGMSG(D_DURING, "\n");
764 
765 	BUGMSG(D_DURING, "in arcnet_interrupt\n");
766 
767 	lp = dev->priv;
768 	if (!lp)
769 		BUG();
770 
771 	spin_lock(&lp->lock);
772 
773 	/*
774 	 * RESET flag was enabled - if device is not running, we must clear it right
775 	 * away (but nothing else).
776 	 */
777 	if (!netif_running(dev)) {
778 		if (ASTATUS() & RESETflag)
779 			ACOMMAND(CFLAGScmd | RESETclear);
780 		AINTMASK(0);
781 		spin_unlock(&lp->lock);
782 		return IRQ_HANDLED;
783 	}
784 
785 	BUGMSG(D_DURING, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
786 	       ASTATUS(), lp->intmask);
787 
788 	boguscount = 5;
789 	do {
790 		status = ASTATUS();
791                 diagstatus = (status >> 8) & 0xFF;
792 
793 		BUGMSG(D_DEBUG, "%s: %d: %s: status=%x\n",
794 			__FILE__,__LINE__,__FUNCTION__,status);
795 		didsomething = 0;
796 
797 		/*
798 		 * RESET flag was enabled - card is resetting and if RX is
799 		 * disabled, it's NOT because we just got a packet.
800 		 *
801 		 * The card is in an undefined state.  Clear it out and start over.
802 		 */
803 		if (status & RESETflag) {
804 			BUGMSG(D_NORMAL, "spurious reset (status=%Xh)\n", status);
805 			arcnet_close(dev);
806 			arcnet_open(dev);
807 
808 			/* get out of the interrupt handler! */
809 			break;
810 		}
811 		/*
812 		 * RX is inhibited - we must have received something. Prepare to
813 		 * receive into the next buffer.
814 		 *
815 		 * We don't actually copy the received packet from the card until
816 		 * after the transmit handler runs (and possibly launches the next
817 		 * tx); this should improve latency slightly if we get both types
818 		 * of interrupts at once.
819 		 */
820 		recbuf = -1;
821 		if (status & lp->intmask & NORXflag) {
822 			recbuf = lp->cur_rx;
823 			BUGMSG(D_DURING, "Buffer #%d: receive irq (status=%Xh)\n",
824 			       recbuf, status);
825 
826 			lp->cur_rx = get_arcbuf(dev);
827 			if (lp->cur_rx != -1) {
828 				BUGMSG(D_DURING, "enabling receive to buffer #%d\n",
829 				       lp->cur_rx);
830 				ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
831 			}
832 			didsomething++;
833 		}
834 
835 		if((diagstatus & EXCNAKflag)) {
836 			BUGMSG(D_DURING, "EXCNAK IRQ (diagstat=%Xh)\n",
837 			       diagstatus);
838 
839                         ACOMMAND(NOTXcmd);      /* disable transmit */
840                         lp->excnak_pending = 1;
841 
842                         ACOMMAND(EXCNAKclear);
843 			lp->intmask &= ~(EXCNAKflag);
844                         didsomething++;
845                 }
846 
847 
848 		/* a transmit finished, and we're interested in it. */
849 		if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
850 			lp->intmask &= ~(TXFREEflag|EXCNAKflag);
851 
852 			BUGMSG(D_DURING, "TX IRQ (stat=%Xh)\n", status);
853 
854 			if (lp->cur_tx != -1 && !lp->timed_out) {
855 				if(!(status & TXACKflag)) {
856 					if (lp->lasttrans_dest != 0) {
857 						BUGMSG(D_EXTRA,
858 						       "transmit was not acknowledged! "
859 						       "(status=%Xh, dest=%02Xh)\n",
860 						       status, lp->lasttrans_dest);
861 						lp->stats.tx_errors++;
862 						lp->stats.tx_carrier_errors++;
863 					} else {
864 						BUGMSG(D_DURING,
865 						       "broadcast was not acknowledged; that's normal "
866 						       "(status=%Xh, dest=%02Xh)\n",
867 						       status, lp->lasttrans_dest);
868 					}
869 				}
870 
871 				if (lp->outgoing.proto &&
872 				    lp->outgoing.proto->ack_tx) {
873 				  int ackstatus;
874 				  if(status & TXACKflag)
875                                     ackstatus=2;
876                                   else if(lp->excnak_pending)
877                                     ackstatus=1;
878                                   else
879                                     ackstatus=0;
880 
881                                   lp->outgoing.proto
882                                     ->ack_tx(dev, ackstatus);
883 				}
884 			}
885 			if (lp->cur_tx != -1)
886 				release_arcbuf(dev, lp->cur_tx);
887 
888 			lp->cur_tx = -1;
889 			lp->timed_out = 0;
890 			didsomething++;
891 
892 			/* send another packet if there is one */
893 			go_tx(dev);
894 
895 			/* continue a split packet, if any */
896 			if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
897 				int txbuf = get_arcbuf(dev);
898 				if (txbuf != -1) {
899 					if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
900 						/* that was the last segment */
901 						lp->stats.tx_bytes += lp->outgoing.skb->len;
902 						if(!lp->outgoing.proto->ack_tx)
903 						  {
904 						    dev_kfree_skb_irq(lp->outgoing.skb);
905 						    lp->outgoing.proto = NULL;
906 						  }
907 					}
908 					lp->next_tx = txbuf;
909 				}
910 			}
911 			/* inform upper layers of idleness, if necessary */
912 			if (lp->cur_tx == -1)
913 				netif_wake_queue(dev);
914 		}
915 		/* now process the received packet, if any */
916 		if (recbuf != -1) {
917 			BUGLVL(D_RX) arcnet_dump_packet(dev, recbuf, "rx irq", 0);
918 
919 			arcnet_rx(dev, recbuf);
920 			release_arcbuf(dev, recbuf);
921 
922 			didsomething++;
923 		}
924 		if (status & lp->intmask & RECONflag) {
925 			ACOMMAND(CFLAGScmd | CONFIGclear);
926 			lp->stats.tx_carrier_errors++;
927 
928 			BUGMSG(D_RECON, "Network reconfiguration detected (status=%Xh)\n",
929 			       status);
930 			/* MYRECON bit is at bit 7 of diagstatus */
931 			if(diagstatus & 0x80)
932 				BUGMSG(D_RECON,"Put out that recon myself\n");
933 
934 			/* is the RECON info empty or old? */
935 			if (!lp->first_recon || !lp->last_recon ||
936 			    jiffies - lp->last_recon > HZ * 10) {
937 				if (lp->network_down)
938 					BUGMSG(D_NORMAL, "reconfiguration detected: cabling restored?\n");
939 				lp->first_recon = lp->last_recon = jiffies;
940 				lp->num_recons = lp->network_down = 0;
941 
942 				BUGMSG(D_DURING, "recon: clearing counters.\n");
943 			} else {	/* add to current RECON counter */
944 				lp->last_recon = jiffies;
945 				lp->num_recons++;
946 
947 				BUGMSG(D_DURING, "recon: counter=%d, time=%lds, net=%d\n",
948 				       lp->num_recons,
949 				 (lp->last_recon - lp->first_recon) / HZ,
950 				       lp->network_down);
951 
952 				/* if network is marked up;
953 				 * and first_recon and last_recon are 60+ apart;
954 				 * and the average no. of recons counted is
955 				 *    > RECON_THRESHOLD/min;
956 				 * then print a warning message.
957 				 */
958 				if (!lp->network_down
959 				    && (lp->last_recon - lp->first_recon) <= HZ * 60
960 				  && lp->num_recons >= RECON_THRESHOLD) {
961 					lp->network_down = 1;
962 					BUGMSG(D_NORMAL, "many reconfigurations detected: cabling problem?\n");
963 				} else if (!lp->network_down
964 					   && lp->last_recon - lp->first_recon > HZ * 60) {
965 					/* reset counters if we've gone for over a minute. */
966 					lp->first_recon = lp->last_recon;
967 					lp->num_recons = 1;
968 				}
969 			}
970 		} else if (lp->network_down && jiffies - lp->last_recon > HZ * 10) {
971 			if (lp->network_down)
972 				BUGMSG(D_NORMAL, "cabling restored?\n");
973 			lp->first_recon = lp->last_recon = 0;
974 			lp->num_recons = lp->network_down = 0;
975 
976 			BUGMSG(D_DURING, "not recon: clearing counters anyway.\n");
977 		}
978 
979 		if(didsomething) {
980 			retval |= IRQ_HANDLED;
981 		}
982 	}
983 	while (--boguscount && didsomething);
984 
985 	BUGMSG(D_DURING, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
986 	       ASTATUS(), boguscount);
987 	BUGMSG(D_DURING, "\n");
988 
989 
990 	AINTMASK(0);
991 	udelay(1);
992 	AINTMASK(lp->intmask);
993 
994 	spin_unlock(&lp->lock);
995 	return retval;
996 }
997 
998 
999 /*
1000  * This is a generic packet receiver that calls arcnet??_rx depending on the
1001  * protocol ID found.
1002  */
1003 static void arcnet_rx(struct net_device *dev, int bufnum)
1004 {
1005 	struct arcnet_local *lp = dev->priv;
1006 	struct archdr pkt;
1007 	struct arc_rfc1201 *soft;
1008 	int length, ofs;
1009 
1010 	soft = &pkt.soft.rfc1201;
1011 
1012 	lp->hw.copy_from_card(dev, bufnum, 0, &pkt, sizeof(ARC_HDR_SIZE));
1013 	if (pkt.hard.offset[0]) {
1014 		ofs = pkt.hard.offset[0];
1015 		length = 256 - ofs;
1016 	} else {
1017 		ofs = pkt.hard.offset[1];
1018 		length = 512 - ofs;
1019 	}
1020 
1021 	/* get the full header, if possible */
1022 	if (sizeof(pkt.soft) <= length)
1023 		lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
1024 	else {
1025 		memset(&pkt.soft, 0, sizeof(pkt.soft));
1026 		lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
1027 	}
1028 
1029 	BUGMSG(D_DURING, "Buffer #%d: received packet from %02Xh to %02Xh "
1030 	       "(%d+4 bytes)\n",
1031 	       bufnum, pkt.hard.source, pkt.hard.dest, length);
1032 
1033 	lp->stats.rx_packets++;
1034 	lp->stats.rx_bytes += length + ARC_HDR_SIZE;
1035 
1036 	/* call the right receiver for the protocol */
1037 	if (arc_proto_map[soft->proto]->is_ip) {
1038 		BUGLVL(D_PROTO) {
1039 			struct ArcProto
1040 			*oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
1041 			*newp = arc_proto_map[soft->proto];
1042 
1043 			if (oldp != newp) {
1044 				BUGMSG(D_PROTO,
1045 				       "got protocol %02Xh; encap for host %02Xh is now '%c'"
1046 				       " (was '%c')\n", soft->proto, pkt.hard.source,
1047 				       newp->suffix, oldp->suffix);
1048 			}
1049 		}
1050 
1051 		/* broadcasts will always be done with the last-used encap. */
1052 		lp->default_proto[0] = soft->proto;
1053 
1054 		/* in striking contrast, the following isn't a hack. */
1055 		lp->default_proto[pkt.hard.source] = soft->proto;
1056 	}
1057 	/* call the protocol-specific receiver. */
1058 	arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
1059 }
1060 
1061 
1062 
1063 /*
1064  * Get the current statistics.  This may be called with the card open or
1065  * closed.
1066  */
1067 static struct net_device_stats *arcnet_get_stats(struct net_device *dev)
1068 {
1069 	struct arcnet_local *lp = dev->priv;
1070 	return &lp->stats;
1071 }
1072 
1073 
1074 static void null_rx(struct net_device *dev, int bufnum,
1075 		    struct archdr *pkthdr, int length)
1076 {
1077 	BUGMSG(D_PROTO,
1078 	"rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
1079 	       pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
1080 }
1081 
1082 
1083 static int null_build_header(struct sk_buff *skb, struct net_device *dev,
1084 			     unsigned short type, uint8_t daddr)
1085 {
1086 	struct arcnet_local *lp = dev->priv;
1087 
1088 	BUGMSG(D_PROTO,
1089 	       "tx: can't build header for encap %02Xh; load a protocol driver.\n",
1090 	       lp->default_proto[daddr]);
1091 
1092 	/* always fails */
1093 	return 0;
1094 }
1095 
1096 
1097 /* the "do nothing" prepare_tx function warns that there's nothing to do. */
1098 static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
1099 			   int length, int bufnum)
1100 {
1101 	struct arcnet_local *lp = dev->priv;
1102 	struct arc_hardware newpkt;
1103 
1104 	BUGMSG(D_PROTO, "tx: no encap for this host; load a protocol driver.\n");
1105 
1106 	/* send a packet to myself -- will never get received, of course */
1107 	newpkt.source = newpkt.dest = dev->dev_addr[0];
1108 
1109 	/* only one byte of actual data (and it's random) */
1110 	newpkt.offset[0] = 0xFF;
1111 
1112 	lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1113 
1114 	return 1;		/* done */
1115 }
1116