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 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/jiffies.h>
54#include <linux/errqueue.h>
55
56#include <linux/leds.h>
57
58#include "arcdevice.h"
59#include "com9026.h"
60
61/* "do nothing" functions for protocol drivers */
62static void null_rx(struct net_device *dev, int bufnum,
63		    struct archdr *pkthdr, int length);
64static int null_build_header(struct sk_buff *skb, struct net_device *dev,
65			     unsigned short type, uint8_t daddr);
66static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
67			   int length, int bufnum);
68
69static void arcnet_rx(struct net_device *dev, int bufnum);
70
71/* one ArcProto per possible proto ID.  None of the elements of
72 * arc_proto_map are allowed to be NULL; they will get set to
73 * arc_proto_default instead.  It also must not be NULL; if you would like
74 * to set it to NULL, set it to &arc_proto_null instead.
75 */
76struct ArcProto *arc_proto_map[256];
77EXPORT_SYMBOL(arc_proto_map);
78
79struct ArcProto *arc_proto_default;
80EXPORT_SYMBOL(arc_proto_default);
81
82struct ArcProto *arc_bcast_proto;
83EXPORT_SYMBOL(arc_bcast_proto);
84
85struct ArcProto *arc_raw_proto;
86EXPORT_SYMBOL(arc_raw_proto);
87
88static struct ArcProto arc_proto_null = {
89	.suffix		= '?',
90	.mtu		= XMTU,
91	.is_ip          = 0,
92	.rx		= null_rx,
93	.build_header	= null_build_header,
94	.prepare_tx	= null_prepare_tx,
95	.continue_tx    = NULL,
96	.ack_tx         = NULL
97};
98
99/* Exported function prototypes */
100int arcnet_debug = ARCNET_DEBUG;
101EXPORT_SYMBOL(arcnet_debug);
102
103/* Internal function prototypes */
104static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
105			 unsigned short type, const void *daddr,
106			 const void *saddr, unsigned len);
107static int go_tx(struct net_device *dev);
108
109static int debug = ARCNET_DEBUG;
110module_param(debug, int, 0);
111MODULE_LICENSE("GPL");
112
113static int __init arcnet_init(void)
114{
115	int count;
116
117	arcnet_debug = debug;
118
119	pr_info("arcnet loaded\n");
120
121	/* initialize the protocol map */
122	arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
123	for (count = 0; count < 256; count++)
124		arc_proto_map[count] = arc_proto_default;
125
126	if (BUGLVL(D_DURING))
127		pr_info("struct sizes: %zd %zd %zd %zd %zd\n",
128			sizeof(struct arc_hardware),
129			sizeof(struct arc_rfc1201),
130			sizeof(struct arc_rfc1051),
131			sizeof(struct arc_eth_encap),
132			sizeof(struct archdr));
133
134	return 0;
135}
136
137static void __exit arcnet_exit(void)
138{
139}
140
141module_init(arcnet_init);
142module_exit(arcnet_exit);
143
144/* Dump the contents of an sk_buff */
145#if ARCNET_DEBUG_MAX & D_SKB
146void arcnet_dump_skb(struct net_device *dev,
147		     struct sk_buff *skb, char *desc)
148{
149	char hdr[32];
150
151	/* dump the packet */
152	snprintf(hdr, sizeof(hdr), "%6s:%s skb->data:", dev->name, desc);
153	print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
154		       16, 1, skb->data, skb->len, true);
155}
156EXPORT_SYMBOL(arcnet_dump_skb);
157#endif
158
159/* Dump the contents of an ARCnet buffer */
160#if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
161static void arcnet_dump_packet(struct net_device *dev, int bufnum,
162			       char *desc, int take_arcnet_lock)
163{
164	struct arcnet_local *lp = netdev_priv(dev);
165	int i, length;
166	unsigned long flags = 0;
167	static uint8_t buf[512];
168	char hdr[32];
169
170	/* hw.copy_from_card expects IRQ context so take the IRQ lock
171	 * to keep it single threaded
172	 */
173	if (take_arcnet_lock)
174		spin_lock_irqsave(&lp->lock, flags);
175
176	lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
177	if (take_arcnet_lock)
178		spin_unlock_irqrestore(&lp->lock, flags);
179
180	/* if the offset[0] byte is nonzero, this is a 256-byte packet */
181	length = (buf[2] ? 256 : 512);
182
183	/* dump the packet */
184	snprintf(hdr, sizeof(hdr), "%6s:%s packet dump:", dev->name, desc);
185	print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
186		       16, 1, buf, length, true);
187}
188
189#else
190
191#define arcnet_dump_packet(dev, bufnum, desc, take_arcnet_lock) do { } while (0)
192
193#endif
194
195/* Trigger a LED event in response to a ARCNET device event */
196void arcnet_led_event(struct net_device *dev, enum arcnet_led_event event)
197{
198	struct arcnet_local *lp = netdev_priv(dev);
199	unsigned long led_delay = 350;
200	unsigned long tx_delay = 50;
201
202	switch (event) {
203	case ARCNET_LED_EVENT_RECON:
204		led_trigger_blink_oneshot(lp->recon_led_trig,
205					  &led_delay, &led_delay, 0);
206		break;
207	case ARCNET_LED_EVENT_OPEN:
208		led_trigger_event(lp->tx_led_trig, LED_OFF);
209		led_trigger_event(lp->recon_led_trig, LED_OFF);
210		break;
211	case ARCNET_LED_EVENT_STOP:
212		led_trigger_event(lp->tx_led_trig, LED_OFF);
213		led_trigger_event(lp->recon_led_trig, LED_OFF);
214		break;
215	case ARCNET_LED_EVENT_TX:
216		led_trigger_blink_oneshot(lp->tx_led_trig,
217					  &tx_delay, &tx_delay, 0);
218		break;
219	}
220}
221EXPORT_SYMBOL_GPL(arcnet_led_event);
222
223static void arcnet_led_release(struct device *gendev, void *res)
224{
225	struct arcnet_local *lp = netdev_priv(to_net_dev(gendev));
226
227	led_trigger_unregister_simple(lp->tx_led_trig);
228	led_trigger_unregister_simple(lp->recon_led_trig);
229}
230
231/* Register ARCNET LED triggers for a arcnet device
232 *
233 * This is normally called from a driver's probe function
234 */
235void devm_arcnet_led_init(struct net_device *netdev, int index, int subid)
236{
237	struct arcnet_local *lp = netdev_priv(netdev);
238	void *res;
239
240	res = devres_alloc(arcnet_led_release, 0, GFP_KERNEL);
241	if (!res) {
242		netdev_err(netdev, "cannot register LED triggers\n");
243		return;
244	}
245
246	snprintf(lp->tx_led_trig_name, sizeof(lp->tx_led_trig_name),
247		 "arc%d-%d-tx", index, subid);
248	snprintf(lp->recon_led_trig_name, sizeof(lp->recon_led_trig_name),
249		 "arc%d-%d-recon", index, subid);
250
251	led_trigger_register_simple(lp->tx_led_trig_name,
252				    &lp->tx_led_trig);
253	led_trigger_register_simple(lp->recon_led_trig_name,
254				    &lp->recon_led_trig);
255
256	devres_add(&netdev->dev, res);
257}
258EXPORT_SYMBOL_GPL(devm_arcnet_led_init);
259
260/* Unregister a protocol driver from the arc_proto_map.  Protocol drivers
261 * are responsible for registering themselves, but the unregister routine
262 * is pretty generic so we'll do it here.
263 */
264void arcnet_unregister_proto(struct ArcProto *proto)
265{
266	int count;
267
268	if (arc_proto_default == proto)
269		arc_proto_default = &arc_proto_null;
270	if (arc_bcast_proto == proto)
271		arc_bcast_proto = arc_proto_default;
272	if (arc_raw_proto == proto)
273		arc_raw_proto = arc_proto_default;
274
275	for (count = 0; count < 256; count++) {
276		if (arc_proto_map[count] == proto)
277			arc_proto_map[count] = arc_proto_default;
278	}
279}
280EXPORT_SYMBOL(arcnet_unregister_proto);
281
282/* Add a buffer to the queue.  Only the interrupt handler is allowed to do
283 * this, unless interrupts are disabled.
284 *
285 * Note: we don't check for a full queue, since there aren't enough buffers
286 * to more than fill it.
287 */
288static void release_arcbuf(struct net_device *dev, int bufnum)
289{
290	struct arcnet_local *lp = netdev_priv(dev);
291	int i;
292
293	lp->buf_queue[lp->first_free_buf++] = bufnum;
294	lp->first_free_buf %= 5;
295
296	if (BUGLVL(D_DURING)) {
297		arc_printk(D_DURING, dev, "release_arcbuf: freed #%d; buffer queue is now: ",
298			   bufnum);
299		for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
300			arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
301		arc_cont(D_DURING, "\n");
302	}
303}
304
305/* Get a buffer from the queue.
306 * If this returns -1, there are no buffers available.
307 */
308static int get_arcbuf(struct net_device *dev)
309{
310	struct arcnet_local *lp = netdev_priv(dev);
311	int buf = -1, i;
312
313	if (!atomic_dec_and_test(&lp->buf_lock)) {
314		/* already in this function */
315		arc_printk(D_NORMAL, dev, "get_arcbuf: overlap (%d)!\n",
316			   lp->buf_lock.counter);
317	} else {			/* we can continue */
318		if (lp->next_buf >= 5)
319			lp->next_buf -= 5;
320
321		if (lp->next_buf == lp->first_free_buf) {
322			arc_printk(D_NORMAL, dev, "get_arcbuf: BUG: no buffers are available??\n");
323		} else {
324			buf = lp->buf_queue[lp->next_buf++];
325			lp->next_buf %= 5;
326		}
327	}
328
329	if (BUGLVL(D_DURING)) {
330		arc_printk(D_DURING, dev, "get_arcbuf: got #%d; buffer queue is now: ",
331			   buf);
332		for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
333			arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
334		arc_cont(D_DURING, "\n");
335	}
336
337	atomic_inc(&lp->buf_lock);
338	return buf;
339}
340
341static int choose_mtu(void)
342{
343	int count, mtu = 65535;
344
345	/* choose the smallest MTU of all available encaps */
346	for (count = 0; count < 256; count++) {
347		if (arc_proto_map[count] != &arc_proto_null &&
348		    arc_proto_map[count]->mtu < mtu) {
349			mtu = arc_proto_map[count]->mtu;
350		}
351	}
352
353	return mtu == 65535 ? XMTU : mtu;
354}
355
356static const struct header_ops arcnet_header_ops = {
357	.create = arcnet_header,
358};
359
360static const struct net_device_ops arcnet_netdev_ops = {
361	.ndo_open	= arcnet_open,
362	.ndo_stop	= arcnet_close,
363	.ndo_start_xmit = arcnet_send_packet,
364	.ndo_tx_timeout = arcnet_timeout,
365};
366
367/* Setup a struct device for ARCnet. */
368static void arcdev_setup(struct net_device *dev)
369{
370	dev->type = ARPHRD_ARCNET;
371	dev->netdev_ops = &arcnet_netdev_ops;
372	dev->header_ops = &arcnet_header_ops;
373	dev->hard_header_len = sizeof(struct arc_hardware);
374	dev->mtu = choose_mtu();
375
376	dev->addr_len = ARCNET_ALEN;
377	dev->tx_queue_len = 100;
378	dev->broadcast[0] = 0x00;	/* for us, broadcasts are address 0 */
379	dev->watchdog_timeo = TX_TIMEOUT;
380
381	/* New-style flags. */
382	dev->flags = IFF_BROADCAST;
383}
384
385static void arcnet_timer(struct timer_list *t)
386{
387	struct arcnet_local *lp = from_timer(lp, t, timer);
388	struct net_device *dev = lp->dev;
389
390	spin_lock_irq(&lp->lock);
391
392	if (!lp->reset_in_progress && !netif_carrier_ok(dev)) {
393		netif_carrier_on(dev);
394		netdev_info(dev, "link up\n");
395	}
396
397	spin_unlock_irq(&lp->lock);
398}
399
400static void reset_device_work(struct work_struct *work)
401{
402	struct arcnet_local *lp;
403	struct net_device *dev;
404
405	lp = container_of(work, struct arcnet_local, reset_work);
406	dev = lp->dev;
407
408	/* Do not bring the network interface back up if an ifdown
409	 * was already done.
410	 */
411	if (!netif_running(dev) || !lp->reset_in_progress)
412		return;
413
414	rtnl_lock();
415
416	/* Do another check, in case of an ifdown that was triggered in
417	 * the small race window between the exit condition above and
418	 * acquiring RTNL.
419	 */
420	if (!netif_running(dev) || !lp->reset_in_progress)
421		goto out;
422
423	dev_close(dev);
424	dev_open(dev, NULL);
425
426out:
427	rtnl_unlock();
428}
429
430static void arcnet_reply_tasklet(unsigned long data)
431{
432	struct arcnet_local *lp = (struct arcnet_local *)data;
433
434	struct sk_buff *ackskb, *skb;
435	struct sock_exterr_skb *serr;
436	struct sock *sk;
437	int ret;
438
439	local_irq_disable();
440	skb = lp->outgoing.skb;
441	if (!skb || !skb->sk) {
442		local_irq_enable();
443		return;
444	}
445
446	sock_hold(skb->sk);
447	sk = skb->sk;
448	ackskb = skb_clone_sk(skb);
449	sock_put(skb->sk);
450
451	if (!ackskb) {
452		local_irq_enable();
453		return;
454	}
455
456	serr = SKB_EXT_ERR(ackskb);
457	memset(serr, 0, sizeof(*serr));
458	serr->ee.ee_errno = ENOMSG;
459	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
460	serr->ee.ee_data = skb_shinfo(skb)->tskey;
461	serr->ee.ee_info = lp->reply_status;
462
463	/* finally erasing outgoing skb */
464	dev_kfree_skb(lp->outgoing.skb);
465	lp->outgoing.skb = NULL;
466
467	ackskb->dev = lp->dev;
468
469	ret = sock_queue_err_skb(sk, ackskb);
470	if (ret)
471		dev_kfree_skb_irq(ackskb);
472
473	local_irq_enable();
474};
475
476struct net_device *alloc_arcdev(const char *name)
477{
478	struct net_device *dev;
479
480	dev = alloc_netdev(sizeof(struct arcnet_local),
481			   name && *name ? name : "arc%d", NET_NAME_UNKNOWN,
482			   arcdev_setup);
483	if (dev) {
484		struct arcnet_local *lp = netdev_priv(dev);
485
486		lp->dev = dev;
487		spin_lock_init(&lp->lock);
488		timer_setup(&lp->timer, arcnet_timer, 0);
489		INIT_WORK(&lp->reset_work, reset_device_work);
490	}
491
492	return dev;
493}
494EXPORT_SYMBOL(alloc_arcdev);
495
496void free_arcdev(struct net_device *dev)
497{
498	struct arcnet_local *lp = netdev_priv(dev);
499
500	/* Do not cancel this at ->ndo_close(), as the workqueue itself
501	 * indirectly calls the ifdown path through dev_close().
502	 */
503	cancel_work_sync(&lp->reset_work);
504	free_netdev(dev);
505}
506EXPORT_SYMBOL(free_arcdev);
507
508/* Open/initialize the board.  This is called sometime after booting when
509 * the 'ifconfig' program is run.
510 *
511 * This routine should set everything up anew at each open, even registers
512 * that "should" only need to be set once at boot, so that there is
513 * non-reboot way to recover if something goes wrong.
514 */
515int arcnet_open(struct net_device *dev)
516{
517	struct arcnet_local *lp = netdev_priv(dev);
518	int count, newmtu, error;
519
520	arc_printk(D_INIT, dev, "opened.");
521
522	if (!try_module_get(lp->hw.owner))
523		return -ENODEV;
524
525	if (BUGLVL(D_PROTO)) {
526		arc_printk(D_PROTO, dev, "protocol map (default is '%c'): ",
527			   arc_proto_default->suffix);
528		for (count = 0; count < 256; count++)
529			arc_cont(D_PROTO, "%c", arc_proto_map[count]->suffix);
530		arc_cont(D_PROTO, "\n");
531	}
532
533	tasklet_init(&lp->reply_tasklet, arcnet_reply_tasklet,
534		     (unsigned long)lp);
535
536	arc_printk(D_INIT, dev, "arcnet_open: resetting card.\n");
537
538	/* try to put the card in a defined state - if it fails the first
539	 * time, actually reset it.
540	 */
541	error = -ENODEV;
542	if (lp->hw.reset(dev, 0) && lp->hw.reset(dev, 1))
543		goto out_module_put;
544
545	newmtu = choose_mtu();
546	if (newmtu < dev->mtu)
547		dev->mtu = newmtu;
548
549	arc_printk(D_INIT, dev, "arcnet_open: mtu: %d.\n", dev->mtu);
550
551	/* autodetect the encapsulation for each host. */
552	memset(lp->default_proto, 0, sizeof(lp->default_proto));
553
554	/* the broadcast address is special - use the 'bcast' protocol */
555	for (count = 0; count < 256; count++) {
556		if (arc_proto_map[count] == arc_bcast_proto) {
557			lp->default_proto[0] = count;
558			break;
559		}
560	}
561
562	/* initialize buffers */
563	atomic_set(&lp->buf_lock, 1);
564
565	lp->next_buf = lp->first_free_buf = 0;
566	release_arcbuf(dev, 0);
567	release_arcbuf(dev, 1);
568	release_arcbuf(dev, 2);
569	release_arcbuf(dev, 3);
570	lp->cur_tx = lp->next_tx = -1;
571	lp->cur_rx = -1;
572
573	lp->rfc1201.sequence = 1;
574
575	/* bring up the hardware driver */
576	if (lp->hw.open)
577		lp->hw.open(dev);
578
579	if (dev->dev_addr[0] == 0)
580		arc_printk(D_NORMAL, dev, "WARNING!  Station address 00 is reserved for broadcasts!\n");
581	else if (dev->dev_addr[0] == 255)
582		arc_printk(D_NORMAL, dev, "WARNING!  Station address FF may confuse DOS networking programs!\n");
583
584	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
585	if (lp->hw.status(dev) & RESETflag) {
586		arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
587			   __FILE__, __LINE__, __func__);
588		lp->hw.command(dev, CFLAGScmd | RESETclear);
589	}
590
591	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
592	/* make sure we're ready to receive IRQ's. */
593	lp->hw.intmask(dev, 0);
594	udelay(1);		/* give it time to set the mask before
595				 * we reset it again. (may not even be
596				 * necessary)
597				 */
598	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
599	lp->intmask = NORXflag | RECONflag;
600	lp->hw.intmask(dev, lp->intmask);
601	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
602
603	netif_carrier_off(dev);
604	netif_start_queue(dev);
605	mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000));
606
607	arcnet_led_event(dev, ARCNET_LED_EVENT_OPEN);
608	return 0;
609
610 out_module_put:
611	module_put(lp->hw.owner);
612	return error;
613}
614EXPORT_SYMBOL(arcnet_open);
615
616/* The inverse routine to arcnet_open - shuts down the card. */
617int arcnet_close(struct net_device *dev)
618{
619	struct arcnet_local *lp = netdev_priv(dev);
620
621	arcnet_led_event(dev, ARCNET_LED_EVENT_STOP);
622	del_timer_sync(&lp->timer);
623
624	netif_stop_queue(dev);
625	netif_carrier_off(dev);
626
627	tasklet_kill(&lp->reply_tasklet);
628
629	/* flush TX and disable RX */
630	lp->hw.intmask(dev, 0);
631	lp->hw.command(dev, NOTXcmd);	/* stop transmit */
632	lp->hw.command(dev, NORXcmd);	/* disable receive */
633	mdelay(1);
634
635	/* shut down the card */
636	lp->hw.close(dev);
637
638	/* reset counters */
639	lp->reset_in_progress = 0;
640
641	module_put(lp->hw.owner);
642	return 0;
643}
644EXPORT_SYMBOL(arcnet_close);
645
646static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
647			 unsigned short type, const void *daddr,
648			 const void *saddr, unsigned len)
649{
650	const struct arcnet_local *lp = netdev_priv(dev);
651	uint8_t _daddr, proto_num;
652	struct ArcProto *proto;
653
654	arc_printk(D_DURING, dev,
655		   "create header from %d to %d; protocol %d (%Xh); size %u.\n",
656		   saddr ? *(uint8_t *)saddr : -1,
657		   daddr ? *(uint8_t *)daddr : -1,
658		   type, type, len);
659
660	if (skb->len != 0 && len != skb->len)
661		arc_printk(D_NORMAL, dev, "arcnet_header: Yikes!  skb->len(%d) != len(%d)!\n",
662			   skb->len, len);
663
664	/* Type is host order - ? */
665	if (type == ETH_P_ARCNET) {
666		proto = arc_raw_proto;
667		arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n",
668			   proto->suffix);
669		_daddr = daddr ? *(uint8_t *)daddr : 0;
670	} else if (!daddr) {
671		/* if the dest addr isn't provided, we can't choose an
672		 * encapsulation!  Store the packet type (eg. ETH_P_IP)
673		 * for now, and we'll push on a real header when we do
674		 * rebuild_header.
675		 */
676		*(uint16_t *)skb_push(skb, 2) = type;
677		/* XXX: Why not use skb->mac_len? */
678		if (skb->network_header - skb->mac_header != 2)
679			arc_printk(D_NORMAL, dev, "arcnet_header: Yikes!  diff (%u) is not 2!\n",
680				   skb->network_header - skb->mac_header);
681		return -2;	/* return error -- can't transmit yet! */
682	} else {
683		/* otherwise, we can just add the header as usual. */
684		_daddr = *(uint8_t *)daddr;
685		proto_num = lp->default_proto[_daddr];
686		proto = arc_proto_map[proto_num];
687		arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n",
688			   proto_num, proto->suffix);
689		if (proto == &arc_proto_null && arc_bcast_proto != proto) {
690			arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n",
691				   arc_bcast_proto->suffix);
692			proto = arc_bcast_proto;
693		}
694	}
695	return proto->build_header(skb, dev, type, _daddr);
696}
697
698/* Called by the kernel in order to transmit a packet. */
699netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
700			       struct net_device *dev)
701{
702	struct arcnet_local *lp = netdev_priv(dev);
703	struct archdr *pkt;
704	struct arc_rfc1201 *soft;
705	struct ArcProto *proto;
706	int txbuf;
707	unsigned long flags;
708	int retval;
709
710	arc_printk(D_DURING, dev,
711		   "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
712		   lp->hw.status(dev), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
713
714	pkt = (struct archdr *)skb->data;
715	soft = &pkt->soft.rfc1201;
716	proto = arc_proto_map[soft->proto];
717
718	arc_printk(D_SKB_SIZE, dev, "skb: transmitting %d bytes to %02X\n",
719		   skb->len, pkt->hard.dest);
720	if (BUGLVL(D_SKB))
721		arcnet_dump_skb(dev, skb, "tx");
722
723	/* fits in one packet? */
724	if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
725		arc_printk(D_NORMAL, dev, "fixme: packet too large: compensating badly!\n");
726		dev_kfree_skb(skb);
727		return NETDEV_TX_OK;	/* don't try again */
728	}
729
730	/* We're busy transmitting a packet... */
731	netif_stop_queue(dev);
732
733	spin_lock_irqsave(&lp->lock, flags);
734	lp->hw.intmask(dev, 0);
735	if (lp->next_tx == -1)
736		txbuf = get_arcbuf(dev);
737	else
738		txbuf = -1;
739
740	if (txbuf != -1) {
741		lp->outgoing.skb = skb;
742		if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
743		    !proto->ack_tx) {
744			/* done right away and we don't want to acknowledge
745			 *  the package later - forget about it now
746			 */
747			dev->stats.tx_bytes += skb->len;
748		} else {
749			/* do it the 'split' way */
750			lp->outgoing.proto = proto;
751			lp->outgoing.skb = skb;
752			lp->outgoing.pkt = pkt;
753
754			if (proto->continue_tx &&
755			    proto->continue_tx(dev, txbuf)) {
756				arc_printk(D_NORMAL, dev,
757					   "bug! continue_tx finished the first time! (proto='%c')\n",
758					   proto->suffix);
759			}
760		}
761		retval = NETDEV_TX_OK;
762		lp->next_tx = txbuf;
763	} else {
764		retval = NETDEV_TX_BUSY;
765	}
766
767	arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
768		   __FILE__, __LINE__, __func__, lp->hw.status(dev));
769	/* make sure we didn't ignore a TX IRQ while we were in here */
770	lp->hw.intmask(dev, 0);
771
772	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
773	lp->intmask |= TXFREEflag | EXCNAKflag;
774	lp->hw.intmask(dev, lp->intmask);
775	arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
776		   __FILE__, __LINE__, __func__, lp->hw.status(dev));
777
778	arcnet_led_event(dev, ARCNET_LED_EVENT_TX);
779
780	spin_unlock_irqrestore(&lp->lock, flags);
781	return retval;		/* no need to try again */
782}
783EXPORT_SYMBOL(arcnet_send_packet);
784
785/* Actually start transmitting a packet that was loaded into a buffer
786 * by prepare_tx.  This should _only_ be called by the interrupt handler.
787 */
788static int go_tx(struct net_device *dev)
789{
790	struct arcnet_local *lp = netdev_priv(dev);
791
792	arc_printk(D_DURING, dev, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
793		   lp->hw.status(dev), lp->intmask, lp->next_tx, lp->cur_tx);
794
795	if (lp->cur_tx != -1 || lp->next_tx == -1)
796		return 0;
797
798	if (BUGLVL(D_TX))
799		arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
800
801	lp->cur_tx = lp->next_tx;
802	lp->next_tx = -1;
803
804	/* start sending */
805	lp->hw.command(dev, TXcmd | (lp->cur_tx << 3));
806
807	dev->stats.tx_packets++;
808	lp->lasttrans_dest = lp->lastload_dest;
809	lp->lastload_dest = 0;
810	lp->excnak_pending = 0;
811	lp->intmask |= TXFREEflag | EXCNAKflag;
812
813	return 1;
814}
815
816/* Called by the kernel when transmit times out */
817void arcnet_timeout(struct net_device *dev, unsigned int txqueue)
818{
819	unsigned long flags;
820	struct arcnet_local *lp = netdev_priv(dev);
821	int status = lp->hw.status(dev);
822	char *msg;
823
824	spin_lock_irqsave(&lp->lock, flags);
825	if (status & TXFREEflag) {	/* transmit _DID_ finish */
826		msg = " - missed IRQ?";
827	} else {
828		msg = "";
829		dev->stats.tx_aborted_errors++;
830		lp->timed_out = 1;
831		lp->hw.command(dev, NOTXcmd | (lp->cur_tx << 3));
832	}
833	dev->stats.tx_errors++;
834
835	/* make sure we didn't miss a TX or a EXC NAK IRQ */
836	lp->hw.intmask(dev, 0);
837	lp->intmask |= TXFREEflag | EXCNAKflag;
838	lp->hw.intmask(dev, lp->intmask);
839
840	spin_unlock_irqrestore(&lp->lock, flags);
841
842	if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
843		arc_printk(D_EXTRA, dev, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
844			   msg, status, lp->intmask, lp->lasttrans_dest);
845		lp->last_timeout = jiffies;
846	}
847
848	if (lp->cur_tx == -1)
849		netif_wake_queue(dev);
850}
851EXPORT_SYMBOL(arcnet_timeout);
852
853/* The typical workload of the driver: Handle the network interface
854 * interrupts. Establish which device needs attention, and call the correct
855 * chipset interrupt handler.
856 */
857irqreturn_t arcnet_interrupt(int irq, void *dev_id)
858{
859	struct net_device *dev = dev_id;
860	struct arcnet_local *lp;
861	int recbuf, status, diagstatus, didsomething, boguscount;
862	unsigned long flags;
863	int retval = IRQ_NONE;
864
865	arc_printk(D_DURING, dev, "\n");
866
867	arc_printk(D_DURING, dev, "in arcnet_interrupt\n");
868
869	lp = netdev_priv(dev);
870	BUG_ON(!lp);
871
872	spin_lock_irqsave(&lp->lock, flags);
873
874	if (lp->reset_in_progress)
875		goto out;
876
877	/* RESET flag was enabled - if device is not running, we must
878	 * clear it right away (but nothing else).
879	 */
880	if (!netif_running(dev)) {
881		if (lp->hw.status(dev) & RESETflag)
882			lp->hw.command(dev, CFLAGScmd | RESETclear);
883		lp->hw.intmask(dev, 0);
884		spin_unlock_irqrestore(&lp->lock, flags);
885		return retval;
886	}
887
888	arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
889		   lp->hw.status(dev), lp->intmask);
890
891	boguscount = 5;
892	do {
893		status = lp->hw.status(dev);
894		diagstatus = (status >> 8) & 0xFF;
895
896		arc_printk(D_DEBUG, dev, "%s: %d: %s: status=%x\n",
897			   __FILE__, __LINE__, __func__, status);
898		didsomething = 0;
899
900		/* RESET flag was enabled - card is resetting and if RX is
901		 * disabled, it's NOT because we just got a packet.
902		 *
903		 * The card is in an undefined state.
904		 * Clear it out and start over.
905		 */
906		if (status & RESETflag) {
907			arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n",
908				   status);
909
910			lp->reset_in_progress = 1;
911			netif_stop_queue(dev);
912			netif_carrier_off(dev);
913			schedule_work(&lp->reset_work);
914
915			/* get out of the interrupt handler! */
916			goto out;
917		}
918		/* RX is inhibited - we must have received something.
919		 * Prepare to receive into the next buffer.
920		 *
921		 * We don't actually copy the received packet from the card
922		 * until after the transmit handler runs (and possibly
923		 * launches the next tx); this should improve latency slightly
924		 * if we get both types of interrupts at once.
925		 */
926		recbuf = -1;
927		if (status & lp->intmask & NORXflag) {
928			recbuf = lp->cur_rx;
929			arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n",
930				   recbuf, status);
931
932			lp->cur_rx = get_arcbuf(dev);
933			if (lp->cur_rx != -1) {
934				arc_printk(D_DURING, dev, "enabling receive to buffer #%d\n",
935					   lp->cur_rx);
936				lp->hw.command(dev, RXcmd | (lp->cur_rx << 3) | RXbcasts);
937			}
938			didsomething++;
939		}
940
941		if ((diagstatus & EXCNAKflag)) {
942			arc_printk(D_DURING, dev, "EXCNAK IRQ (diagstat=%Xh)\n",
943				   diagstatus);
944
945			lp->hw.command(dev, NOTXcmd);      /* disable transmit */
946			lp->excnak_pending = 1;
947
948			lp->hw.command(dev, EXCNAKclear);
949			lp->intmask &= ~(EXCNAKflag);
950			didsomething++;
951		}
952
953		/* a transmit finished, and we're interested in it. */
954		if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
955			int ackstatus;
956			lp->intmask &= ~(TXFREEflag | EXCNAKflag);
957
958			if (status & TXACKflag)
959				ackstatus = 2;
960			else if (lp->excnak_pending)
961				ackstatus = 1;
962			else
963				ackstatus = 0;
964
965			arc_printk(D_DURING, dev, "TX IRQ (stat=%Xh)\n",
966				   status);
967
968			if (lp->cur_tx != -1 && !lp->timed_out) {
969				if (!(status & TXACKflag)) {
970					if (lp->lasttrans_dest != 0) {
971						arc_printk(D_EXTRA, dev,
972							   "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
973							   status,
974							   lp->lasttrans_dest);
975						dev->stats.tx_errors++;
976						dev->stats.tx_carrier_errors++;
977					} else {
978						arc_printk(D_DURING, dev,
979							   "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
980							   status,
981							   lp->lasttrans_dest);
982					}
983				}
984
985				if (lp->outgoing.proto &&
986				    lp->outgoing.proto->ack_tx) {
987					lp->outgoing.proto
988						->ack_tx(dev, ackstatus);
989				}
990				lp->reply_status = ackstatus;
991				tasklet_hi_schedule(&lp->reply_tasklet);
992			}
993			if (lp->cur_tx != -1)
994				release_arcbuf(dev, lp->cur_tx);
995
996			lp->cur_tx = -1;
997			lp->timed_out = 0;
998			didsomething++;
999
1000			/* send another packet if there is one */
1001			go_tx(dev);
1002
1003			/* continue a split packet, if any */
1004			if (lp->outgoing.proto &&
1005			    lp->outgoing.proto->continue_tx) {
1006				int txbuf = get_arcbuf(dev);
1007
1008				if (txbuf != -1) {
1009					if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
1010						/* that was the last segment */
1011						dev->stats.tx_bytes += lp->outgoing.skb->len;
1012						if (!lp->outgoing.proto->ack_tx) {
1013							dev_kfree_skb_irq(lp->outgoing.skb);
1014							lp->outgoing.proto = NULL;
1015						}
1016					}
1017					lp->next_tx = txbuf;
1018				}
1019			}
1020			/* inform upper layers of idleness, if necessary */
1021			if (lp->cur_tx == -1)
1022				netif_wake_queue(dev);
1023		}
1024		/* now process the received packet, if any */
1025		if (recbuf != -1) {
1026			if (BUGLVL(D_RX))
1027				arcnet_dump_packet(dev, recbuf, "rx irq", 0);
1028
1029			arcnet_rx(dev, recbuf);
1030			release_arcbuf(dev, recbuf);
1031
1032			didsomething++;
1033		}
1034		if (status & lp->intmask & RECONflag) {
1035			lp->hw.command(dev, CFLAGScmd | CONFIGclear);
1036			dev->stats.tx_carrier_errors++;
1037
1038			arc_printk(D_RECON, dev, "Network reconfiguration detected (status=%Xh)\n",
1039				   status);
1040			if (netif_carrier_ok(dev)) {
1041				netif_carrier_off(dev);
1042				netdev_info(dev, "link down\n");
1043			}
1044			mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000));
1045
1046			arcnet_led_event(dev, ARCNET_LED_EVENT_RECON);
1047			/* MYRECON bit is at bit 7 of diagstatus */
1048			if (diagstatus & 0x80)
1049				arc_printk(D_RECON, dev, "Put out that recon myself\n");
1050
1051			/* is the RECON info empty or old? */
1052			if (!lp->first_recon || !lp->last_recon ||
1053			    time_after(jiffies, lp->last_recon + HZ * 10)) {
1054				if (lp->network_down)
1055					arc_printk(D_NORMAL, dev, "reconfiguration detected: cabling restored?\n");
1056				lp->first_recon = lp->last_recon = jiffies;
1057				lp->num_recons = lp->network_down = 0;
1058
1059				arc_printk(D_DURING, dev, "recon: clearing counters.\n");
1060			} else {	/* add to current RECON counter */
1061				lp->last_recon = jiffies;
1062				lp->num_recons++;
1063
1064				arc_printk(D_DURING, dev, "recon: counter=%d, time=%lds, net=%d\n",
1065					   lp->num_recons,
1066					   (lp->last_recon - lp->first_recon) / HZ,
1067					   lp->network_down);
1068
1069				/* if network is marked up;
1070				 * and first_recon and last_recon are 60+ apart;
1071				 * and the average no. of recons counted is
1072				 *    > RECON_THRESHOLD/min;
1073				 * then print a warning message.
1074				 */
1075				if (!lp->network_down &&
1076				    (lp->last_recon - lp->first_recon) <= HZ * 60 &&
1077				    lp->num_recons >= RECON_THRESHOLD) {
1078					lp->network_down = 1;
1079					arc_printk(D_NORMAL, dev, "many reconfigurations detected: cabling problem?\n");
1080				} else if (!lp->network_down &&
1081					   lp->last_recon - lp->first_recon > HZ * 60) {
1082					/* reset counters if we've gone for
1083					 *  over a minute.
1084					 */
1085					lp->first_recon = lp->last_recon;
1086					lp->num_recons = 1;
1087				}
1088			}
1089		} else if (lp->network_down &&
1090			   time_after(jiffies, lp->last_recon + HZ * 10)) {
1091			if (lp->network_down)
1092				arc_printk(D_NORMAL, dev, "cabling restored?\n");
1093			lp->first_recon = lp->last_recon = 0;
1094			lp->num_recons = lp->network_down = 0;
1095
1096			arc_printk(D_DURING, dev, "not recon: clearing counters anyway.\n");
1097			netif_carrier_on(dev);
1098		}
1099
1100		if (didsomething)
1101			retval |= IRQ_HANDLED;
1102	} while (--boguscount && didsomething);
1103
1104	arc_printk(D_DURING, dev, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
1105		   lp->hw.status(dev), boguscount);
1106	arc_printk(D_DURING, dev, "\n");
1107
1108	lp->hw.intmask(dev, 0);
1109	udelay(1);
1110	lp->hw.intmask(dev, lp->intmask);
1111
1112out:
1113	spin_unlock_irqrestore(&lp->lock, flags);
1114	return retval;
1115}
1116EXPORT_SYMBOL(arcnet_interrupt);
1117
1118/* This is a generic packet receiver that calls arcnet??_rx depending on the
1119 * protocol ID found.
1120 */
1121static void arcnet_rx(struct net_device *dev, int bufnum)
1122{
1123	struct arcnet_local *lp = netdev_priv(dev);
1124	union {
1125		struct archdr pkt;
1126		char buf[512];
1127	} rxdata;
1128	struct arc_rfc1201 *soft;
1129	int length, ofs;
1130
1131	soft = &rxdata.pkt.soft.rfc1201;
1132
1133	lp->hw.copy_from_card(dev, bufnum, 0, &rxdata.pkt, ARC_HDR_SIZE);
1134	if (rxdata.pkt.hard.offset[0]) {
1135		ofs = rxdata.pkt.hard.offset[0];
1136		length = 256 - ofs;
1137	} else {
1138		ofs = rxdata.pkt.hard.offset[1];
1139		length = 512 - ofs;
1140	}
1141
1142	/* get the full header, if possible */
1143	if (sizeof(rxdata.pkt.soft) <= length) {
1144		lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(rxdata.pkt.soft));
1145	} else {
1146		memset(&rxdata.pkt.soft, 0, sizeof(rxdata.pkt.soft));
1147		lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
1148	}
1149
1150	arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
1151		   bufnum, rxdata.pkt.hard.source, rxdata.pkt.hard.dest, length);
1152
1153	dev->stats.rx_packets++;
1154	dev->stats.rx_bytes += length + ARC_HDR_SIZE;
1155
1156	/* call the right receiver for the protocol */
1157	if (arc_proto_map[soft->proto]->is_ip) {
1158		if (BUGLVL(D_PROTO)) {
1159			struct ArcProto
1160			*oldp = arc_proto_map[lp->default_proto[rxdata.pkt.hard.source]],
1161			*newp = arc_proto_map[soft->proto];
1162
1163			if (oldp != newp) {
1164				arc_printk(D_PROTO, dev,
1165					   "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
1166					   soft->proto, rxdata.pkt.hard.source,
1167					   newp->suffix, oldp->suffix);
1168			}
1169		}
1170
1171		/* broadcasts will always be done with the last-used encap. */
1172		lp->default_proto[0] = soft->proto;
1173
1174		/* in striking contrast, the following isn't a hack. */
1175		lp->default_proto[rxdata.pkt.hard.source] = soft->proto;
1176	}
1177	/* call the protocol-specific receiver. */
1178	arc_proto_map[soft->proto]->rx(dev, bufnum, &rxdata.pkt, length);
1179}
1180
1181static void null_rx(struct net_device *dev, int bufnum,
1182		    struct archdr *pkthdr, int length)
1183{
1184	arc_printk(D_PROTO, dev,
1185		   "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
1186		   pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
1187}
1188
1189static int null_build_header(struct sk_buff *skb, struct net_device *dev,
1190			     unsigned short type, uint8_t daddr)
1191{
1192	struct arcnet_local *lp = netdev_priv(dev);
1193
1194	arc_printk(D_PROTO, dev,
1195		   "tx: can't build header for encap %02Xh; load a protocol driver.\n",
1196		   lp->default_proto[daddr]);
1197
1198	/* always fails */
1199	return 0;
1200}
1201
1202/* the "do nothing" prepare_tx function warns that there's nothing to do. */
1203static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
1204			   int length, int bufnum)
1205{
1206	struct arcnet_local *lp = netdev_priv(dev);
1207	struct arc_hardware newpkt;
1208
1209	arc_printk(D_PROTO, dev, "tx: no encap for this host; load a protocol driver.\n");
1210
1211	/* send a packet to myself -- will never get received, of course */
1212	newpkt.source = newpkt.dest = dev->dev_addr[0];
1213
1214	/* only one byte of actual data (and it's random) */
1215	newpkt.offset[0] = 0xFF;
1216
1217	lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1218
1219	return 1;		/* done */
1220}
1221