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