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