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