xref: /linux/drivers/net/arcnet/arcnet.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Linux ARCnet driver - device-independent routines
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Written 1997 by David Woodhouse.
51da177e4SLinus Torvalds  * Written 1994-1999 by Avery Pennarun.
61da177e4SLinus Torvalds  * Written 1999-2000 by Martin Mares <mj@ucw.cz>.
71da177e4SLinus Torvalds  * Derived from skeleton.c by Donald Becker.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
101da177e4SLinus Torvalds  *  for sponsoring the further development of this driver.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * **********************
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * The original copyright was as follows:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * skeleton.c Written 1993 by Donald Becker.
171da177e4SLinus Torvalds  * Copyright 1993 United States Government as represented by the
181da177e4SLinus Torvalds  * Director, National Security Agency.  This software may only be used
191da177e4SLinus Torvalds  * and distributed according to the terms of the GNU General Public License as
201da177e4SLinus Torvalds  * modified by SRC, incorporated herein by reference.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * **********************
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * The change log is now in a file called ChangeLog in this directory.
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * Sources:
271da177e4SLinus Torvalds  *  - Crynwr arcnet.com/arcether.com packet drivers.
281da177e4SLinus Torvalds  *  - arcnet.c v0.00 dated 1/1/94 and apparently by
291da177e4SLinus Torvalds  *     Donald Becker - it didn't work :)
301da177e4SLinus Torvalds  *  - skeleton.c v0.05 dated 11/16/93 by Donald Becker
311da177e4SLinus Torvalds  *     (from Linux Kernel 1.1.45)
321da177e4SLinus Torvalds  *  - RFC's 1201 and 1051 - re: TCP/IP over ARCnet
331da177e4SLinus Torvalds  *  - The official ARCnet COM9026 data sheets (!) thanks to
341da177e4SLinus Torvalds  *     Ken Cornetet <kcornete@nyx10.cs.du.edu>
351da177e4SLinus Torvalds  *  - The official ARCnet COM20020 data sheets.
361da177e4SLinus Torvalds  *  - Information on some more obscure ARCnet controller chips, thanks
371da177e4SLinus Torvalds  *     to the nice people at SMSC.
381da177e4SLinus Torvalds  *  - net/inet/eth.c (from kernel 1.1.50) for header-building info.
391da177e4SLinus Torvalds  *  - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
401da177e4SLinus Torvalds  *  - Textual information and more alternate source from Joachim Koenig
411da177e4SLinus Torvalds  *     <jojo@repas.de>
421da177e4SLinus Torvalds  */
431da177e4SLinus Torvalds 
4405a24b23SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <linux/module.h>
471da177e4SLinus Torvalds #include <linux/types.h>
481da177e4SLinus Torvalds #include <linux/delay.h>
491da177e4SLinus Torvalds #include <linux/netdevice.h>
501da177e4SLinus Torvalds #include <linux/if_arp.h>
511da177e4SLinus Torvalds #include <net/arp.h>
521da177e4SLinus Torvalds #include <linux/init.h>
53ff5688aeSMarcelo Feitoza Parisi #include <linux/jiffies.h>
5405fcd31cSMichael Grzeschik #include <linux/errqueue.h>
551da177e4SLinus Torvalds 
568890624aSMichael Grzeschik #include <linux/leds.h>
57*775d2e2bSAllen Pais #include <linux/workqueue.h>
588890624aSMichael Grzeschik 
5926c6d281SJoe Perches #include "arcdevice.h"
608e0f295eSJoe Perches #include "com9026.h"
6126c6d281SJoe Perches 
621da177e4SLinus Torvalds /* "do nothing" functions for protocol drivers */
631da177e4SLinus Torvalds static void null_rx(struct net_device *dev, int bufnum,
641da177e4SLinus Torvalds 		    struct archdr *pkthdr, int length);
651da177e4SLinus Torvalds static int null_build_header(struct sk_buff *skb, struct net_device *dev,
661da177e4SLinus Torvalds 			     unsigned short type, uint8_t daddr);
671da177e4SLinus Torvalds static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
681da177e4SLinus Torvalds 			   int length, int bufnum);
691da177e4SLinus Torvalds 
70f03aa2d8SAdrian Bunk static void arcnet_rx(struct net_device *dev, int bufnum);
711da177e4SLinus Torvalds 
72f2f0a16bSJoe Perches /* one ArcProto per possible proto ID.  None of the elements of
731da177e4SLinus Torvalds  * arc_proto_map are allowed to be NULL; they will get set to
741da177e4SLinus Torvalds  * arc_proto_default instead.  It also must not be NULL; if you would like
751da177e4SLinus Torvalds  * to set it to NULL, set it to &arc_proto_null instead.
761da177e4SLinus Torvalds  */
77811eafc0SJoe Perches struct ArcProto *arc_proto_map[256];
78811eafc0SJoe Perches EXPORT_SYMBOL(arc_proto_map);
79811eafc0SJoe Perches 
80811eafc0SJoe Perches struct ArcProto *arc_proto_default;
81811eafc0SJoe Perches EXPORT_SYMBOL(arc_proto_default);
82811eafc0SJoe Perches 
83811eafc0SJoe Perches struct ArcProto *arc_bcast_proto;
84811eafc0SJoe Perches EXPORT_SYMBOL(arc_bcast_proto);
85811eafc0SJoe Perches 
86811eafc0SJoe Perches struct ArcProto *arc_raw_proto;
87811eafc0SJoe Perches EXPORT_SYMBOL(arc_raw_proto);
881da177e4SLinus Torvalds 
897f5e760cSJoe Perches static struct ArcProto arc_proto_null = {
901da177e4SLinus Torvalds 	.suffix		= '?',
911da177e4SLinus Torvalds 	.mtu		= XMTU,
921da177e4SLinus Torvalds 	.is_ip          = 0,
931da177e4SLinus Torvalds 	.rx		= null_rx,
941da177e4SLinus Torvalds 	.build_header	= null_build_header,
951da177e4SLinus Torvalds 	.prepare_tx	= null_prepare_tx,
961da177e4SLinus Torvalds 	.continue_tx    = NULL,
971da177e4SLinus Torvalds 	.ack_tx         = NULL
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /* Exported function prototypes */
1011da177e4SLinus Torvalds int arcnet_debug = ARCNET_DEBUG;
1021da177e4SLinus Torvalds EXPORT_SYMBOL(arcnet_debug);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /* Internal function prototypes */
1051da177e4SLinus Torvalds static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
1063b04dddeSStephen Hemminger 			 unsigned short type, const void *daddr,
1073b04dddeSStephen Hemminger 			 const void *saddr, unsigned len);
1081da177e4SLinus Torvalds static int go_tx(struct net_device *dev);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds static int debug = ARCNET_DEBUG;
1111da177e4SLinus Torvalds module_param(debug, int, 0);
112d06aa0ebSBreno Leitao MODULE_DESCRIPTION("ARCnet core driver");
1131da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1141da177e4SLinus Torvalds 
arcnet_init(void)1151da177e4SLinus Torvalds static int __init arcnet_init(void)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	int count;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	arcnet_debug = debug;
1201da177e4SLinus Torvalds 
12105a24b23SJoe Perches 	pr_info("arcnet loaded\n");
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	/* initialize the protocol map */
1241da177e4SLinus Torvalds 	arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
1251da177e4SLinus Torvalds 	for (count = 0; count < 256; count++)
1261da177e4SLinus Torvalds 		arc_proto_map[count] = arc_proto_default;
1271da177e4SLinus Torvalds 
12872aeea48SJoe Perches 	if (BUGLVL(D_DURING))
1295b5e0928SAlexey Dobriyan 		pr_info("struct sizes: %zd %zd %zd %zd %zd\n",
13005a24b23SJoe Perches 			sizeof(struct arc_hardware),
13105a24b23SJoe Perches 			sizeof(struct arc_rfc1201),
13205a24b23SJoe Perches 			sizeof(struct arc_rfc1051),
13305a24b23SJoe Perches 			sizeof(struct arc_eth_encap),
1341da177e4SLinus Torvalds 			sizeof(struct archdr));
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	return 0;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
arcnet_exit(void)1391da177e4SLinus Torvalds static void __exit arcnet_exit(void)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds module_init(arcnet_init);
1441da177e4SLinus Torvalds module_exit(arcnet_exit);
1451da177e4SLinus Torvalds 
146f2f0a16bSJoe Perches /* Dump the contents of an sk_buff */
1471da177e4SLinus Torvalds #if ARCNET_DEBUG_MAX & D_SKB
arcnet_dump_skb(struct net_device * dev,struct sk_buff * skb,char * desc)1481da177e4SLinus Torvalds void arcnet_dump_skb(struct net_device *dev,
1491da177e4SLinus Torvalds 		     struct sk_buff *skb, char *desc)
1501da177e4SLinus Torvalds {
151ad361c98SJoe Perches 	char hdr[32];
1521da177e4SLinus Torvalds 
153ad361c98SJoe Perches 	/* dump the packet */
154ad361c98SJoe Perches 	snprintf(hdr, sizeof(hdr), "%6s:%s skb->data:", dev->name, desc);
155ad361c98SJoe Perches 	print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
156ad361c98SJoe Perches 		       16, 1, skb->data, skb->len, true);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds EXPORT_SYMBOL(arcnet_dump_skb);
1591da177e4SLinus Torvalds #endif
1601da177e4SLinus Torvalds 
161f2f0a16bSJoe Perches /* Dump the contents of an ARCnet buffer */
1621da177e4SLinus Torvalds #if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
arcnet_dump_packet(struct net_device * dev,int bufnum,char * desc,int take_arcnet_lock)163f03aa2d8SAdrian Bunk static void arcnet_dump_packet(struct net_device *dev, int bufnum,
164f03aa2d8SAdrian Bunk 			       char *desc, int take_arcnet_lock)
1651da177e4SLinus Torvalds {
166454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
1671da177e4SLinus Torvalds 	int i, length;
1681da177e4SLinus Torvalds 	unsigned long flags = 0;
1691da177e4SLinus Torvalds 	static uint8_t buf[512];
170ad361c98SJoe Perches 	char hdr[32];
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	/* hw.copy_from_card expects IRQ context so take the IRQ lock
173f2f0a16bSJoe Perches 	 * to keep it single threaded
174f2f0a16bSJoe Perches 	 */
1751da177e4SLinus Torvalds 	if (take_arcnet_lock)
1761da177e4SLinus Torvalds 		spin_lock_irqsave(&lp->lock, flags);
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
1791da177e4SLinus Torvalds 	if (take_arcnet_lock)
1801da177e4SLinus Torvalds 		spin_unlock_irqrestore(&lp->lock, flags);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	/* if the offset[0] byte is nonzero, this is a 256-byte packet */
1831da177e4SLinus Torvalds 	length = (buf[2] ? 256 : 512);
1841da177e4SLinus Torvalds 
185ad361c98SJoe Perches 	/* dump the packet */
186ad361c98SJoe Perches 	snprintf(hdr, sizeof(hdr), "%6s:%s packet dump:", dev->name, desc);
187ad361c98SJoe Perches 	print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
188ad361c98SJoe Perches 		       16, 1, buf, length, true);
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
191f03aa2d8SAdrian Bunk #else
192f03aa2d8SAdrian Bunk 
193f03aa2d8SAdrian Bunk #define arcnet_dump_packet(dev, bufnum, desc, take_arcnet_lock) do { } while (0)
194f03aa2d8SAdrian Bunk 
1951da177e4SLinus Torvalds #endif
1961da177e4SLinus Torvalds 
1978890624aSMichael Grzeschik /* Trigger a LED event in response to a ARCNET device event */
arcnet_led_event(struct net_device * dev,enum arcnet_led_event event)1988890624aSMichael Grzeschik void arcnet_led_event(struct net_device *dev, enum arcnet_led_event event)
1998890624aSMichael Grzeschik {
2008890624aSMichael Grzeschik 	struct arcnet_local *lp = netdev_priv(dev);
2018890624aSMichael Grzeschik 
2028890624aSMichael Grzeschik 	switch (event) {
2038890624aSMichael Grzeschik 	case ARCNET_LED_EVENT_RECON:
204e298d8a3SHans de Goede 		led_trigger_blink_oneshot(lp->recon_led_trig, 350, 350, 0);
2058890624aSMichael Grzeschik 		break;
2068890624aSMichael Grzeschik 	case ARCNET_LED_EVENT_OPEN:
2078890624aSMichael Grzeschik 		led_trigger_event(lp->tx_led_trig, LED_OFF);
2088890624aSMichael Grzeschik 		led_trigger_event(lp->recon_led_trig, LED_OFF);
2098890624aSMichael Grzeschik 		break;
2108890624aSMichael Grzeschik 	case ARCNET_LED_EVENT_STOP:
2118890624aSMichael Grzeschik 		led_trigger_event(lp->tx_led_trig, LED_OFF);
2128890624aSMichael Grzeschik 		led_trigger_event(lp->recon_led_trig, LED_OFF);
2138890624aSMichael Grzeschik 		break;
2148890624aSMichael Grzeschik 	case ARCNET_LED_EVENT_TX:
215e298d8a3SHans de Goede 		led_trigger_blink_oneshot(lp->tx_led_trig, 50, 50, 0);
2168890624aSMichael Grzeschik 		break;
2178890624aSMichael Grzeschik 	}
2188890624aSMichael Grzeschik }
2198890624aSMichael Grzeschik EXPORT_SYMBOL_GPL(arcnet_led_event);
2208890624aSMichael Grzeschik 
arcnet_led_release(struct device * gendev,void * res)2218890624aSMichael Grzeschik static void arcnet_led_release(struct device *gendev, void *res)
2228890624aSMichael Grzeschik {
2238890624aSMichael Grzeschik 	struct arcnet_local *lp = netdev_priv(to_net_dev(gendev));
2248890624aSMichael Grzeschik 
2258890624aSMichael Grzeschik 	led_trigger_unregister_simple(lp->tx_led_trig);
2268890624aSMichael Grzeschik 	led_trigger_unregister_simple(lp->recon_led_trig);
2278890624aSMichael Grzeschik }
2288890624aSMichael Grzeschik 
2298890624aSMichael Grzeschik /* Register ARCNET LED triggers for a arcnet device
2308890624aSMichael Grzeschik  *
2318890624aSMichael Grzeschik  * This is normally called from a driver's probe function
2328890624aSMichael Grzeschik  */
devm_arcnet_led_init(struct net_device * netdev,int index,int subid)2338890624aSMichael Grzeschik void devm_arcnet_led_init(struct net_device *netdev, int index, int subid)
2348890624aSMichael Grzeschik {
2358890624aSMichael Grzeschik 	struct arcnet_local *lp = netdev_priv(netdev);
2368890624aSMichael Grzeschik 	void *res;
2378890624aSMichael Grzeschik 
2388890624aSMichael Grzeschik 	res = devres_alloc(arcnet_led_release, 0, GFP_KERNEL);
2398890624aSMichael Grzeschik 	if (!res) {
2408890624aSMichael Grzeschik 		netdev_err(netdev, "cannot register LED triggers\n");
2418890624aSMichael Grzeschik 		return;
2428890624aSMichael Grzeschik 	}
2438890624aSMichael Grzeschik 
2448890624aSMichael Grzeschik 	snprintf(lp->tx_led_trig_name, sizeof(lp->tx_led_trig_name),
2458890624aSMichael Grzeschik 		 "arc%d-%d-tx", index, subid);
2468890624aSMichael Grzeschik 	snprintf(lp->recon_led_trig_name, sizeof(lp->recon_led_trig_name),
2478890624aSMichael Grzeschik 		 "arc%d-%d-recon", index, subid);
2488890624aSMichael Grzeschik 
2498890624aSMichael Grzeschik 	led_trigger_register_simple(lp->tx_led_trig_name,
2508890624aSMichael Grzeschik 				    &lp->tx_led_trig);
2518890624aSMichael Grzeschik 	led_trigger_register_simple(lp->recon_led_trig_name,
2528890624aSMichael Grzeschik 				    &lp->recon_led_trig);
2538890624aSMichael Grzeschik 
2548890624aSMichael Grzeschik 	devres_add(&netdev->dev, res);
2558890624aSMichael Grzeschik }
2568890624aSMichael Grzeschik EXPORT_SYMBOL_GPL(devm_arcnet_led_init);
2578890624aSMichael Grzeschik 
258f2f0a16bSJoe Perches /* Unregister a protocol driver from the arc_proto_map.  Protocol drivers
2591da177e4SLinus Torvalds  * are responsible for registering themselves, but the unregister routine
2601da177e4SLinus Torvalds  * is pretty generic so we'll do it here.
2611da177e4SLinus Torvalds  */
arcnet_unregister_proto(struct ArcProto * proto)2621da177e4SLinus Torvalds void arcnet_unregister_proto(struct ArcProto *proto)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	int count;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	if (arc_proto_default == proto)
2671da177e4SLinus Torvalds 		arc_proto_default = &arc_proto_null;
2681da177e4SLinus Torvalds 	if (arc_bcast_proto == proto)
2691da177e4SLinus Torvalds 		arc_bcast_proto = arc_proto_default;
2701da177e4SLinus Torvalds 	if (arc_raw_proto == proto)
2711da177e4SLinus Torvalds 		arc_raw_proto = arc_proto_default;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	for (count = 0; count < 256; count++) {
2741da177e4SLinus Torvalds 		if (arc_proto_map[count] == proto)
2751da177e4SLinus Torvalds 			arc_proto_map[count] = arc_proto_default;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds }
278811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_unregister_proto);
2791da177e4SLinus Torvalds 
280f2f0a16bSJoe Perches /* Add a buffer to the queue.  Only the interrupt handler is allowed to do
2811da177e4SLinus Torvalds  * this, unless interrupts are disabled.
2821da177e4SLinus Torvalds  *
2831da177e4SLinus Torvalds  * Note: we don't check for a full queue, since there aren't enough buffers
2841da177e4SLinus Torvalds  * to more than fill it.
2851da177e4SLinus Torvalds  */
release_arcbuf(struct net_device * dev,int bufnum)2861da177e4SLinus Torvalds static void release_arcbuf(struct net_device *dev, int bufnum)
2871da177e4SLinus Torvalds {
288454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
2891da177e4SLinus Torvalds 	int i;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	lp->buf_queue[lp->first_free_buf++] = bufnum;
2921da177e4SLinus Torvalds 	lp->first_free_buf %= 5;
2931da177e4SLinus Torvalds 
29472aeea48SJoe Perches 	if (BUGLVL(D_DURING)) {
295a34c0932SJoe Perches 		arc_printk(D_DURING, dev, "release_arcbuf: freed #%d; buffer queue is now: ",
2961da177e4SLinus Torvalds 			   bufnum);
2971da177e4SLinus Torvalds 		for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
298a34c0932SJoe Perches 			arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
299a34c0932SJoe Perches 		arc_cont(D_DURING, "\n");
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
303f2f0a16bSJoe Perches /* Get a buffer from the queue.
304f2f0a16bSJoe Perches  * If this returns -1, there are no buffers available.
3051da177e4SLinus Torvalds  */
get_arcbuf(struct net_device * dev)3061da177e4SLinus Torvalds static int get_arcbuf(struct net_device *dev)
3071da177e4SLinus Torvalds {
308454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
3091da177e4SLinus Torvalds 	int buf = -1, i;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&lp->buf_lock)) {
3121da177e4SLinus Torvalds 		/* already in this function */
313a34c0932SJoe Perches 		arc_printk(D_NORMAL, dev, "get_arcbuf: overlap (%d)!\n",
3141da177e4SLinus Torvalds 			   lp->buf_lock.counter);
3157f5e760cSJoe Perches 	} else {			/* we can continue */
3161da177e4SLinus Torvalds 		if (lp->next_buf >= 5)
3171da177e4SLinus Torvalds 			lp->next_buf -= 5;
3181da177e4SLinus Torvalds 
3197f5e760cSJoe Perches 		if (lp->next_buf == lp->first_free_buf) {
320a34c0932SJoe Perches 			arc_printk(D_NORMAL, dev, "get_arcbuf: BUG: no buffers are available??\n");
3217f5e760cSJoe Perches 		} else {
3221da177e4SLinus Torvalds 			buf = lp->buf_queue[lp->next_buf++];
3231da177e4SLinus Torvalds 			lp->next_buf %= 5;
3241da177e4SLinus Torvalds 		}
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
32772aeea48SJoe Perches 	if (BUGLVL(D_DURING)) {
328a34c0932SJoe Perches 		arc_printk(D_DURING, dev, "get_arcbuf: got #%d; buffer queue is now: ",
329a34c0932SJoe Perches 			   buf);
3301da177e4SLinus Torvalds 		for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
331a34c0932SJoe Perches 			arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
332a34c0932SJoe Perches 		arc_cont(D_DURING, "\n");
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	atomic_inc(&lp->buf_lock);
3361da177e4SLinus Torvalds 	return buf;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
choose_mtu(void)3391da177e4SLinus Torvalds static int choose_mtu(void)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	int count, mtu = 65535;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	/* choose the smallest MTU of all available encaps */
3441da177e4SLinus Torvalds 	for (count = 0; count < 256; count++) {
3458e95a202SJoe Perches 		if (arc_proto_map[count] != &arc_proto_null &&
3468e95a202SJoe Perches 		    arc_proto_map[count]->mtu < mtu) {
3471da177e4SLinus Torvalds 			mtu = arc_proto_map[count]->mtu;
3481da177e4SLinus Torvalds 		}
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	return mtu == 65535 ? XMTU : mtu;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3543b04dddeSStephen Hemminger static const struct header_ops arcnet_header_ops = {
3553b04dddeSStephen Hemminger 	.create = arcnet_header,
3563b04dddeSStephen Hemminger };
3573b04dddeSStephen Hemminger 
358bca5b893SStephen Hemminger static const struct net_device_ops arcnet_netdev_ops = {
359bca5b893SStephen Hemminger 	.ndo_open	= arcnet_open,
360bca5b893SStephen Hemminger 	.ndo_stop	= arcnet_close,
361bca5b893SStephen Hemminger 	.ndo_start_xmit = arcnet_send_packet,
362bca5b893SStephen Hemminger 	.ndo_tx_timeout = arcnet_timeout,
363bca5b893SStephen Hemminger };
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds /* Setup a struct device for ARCnet. */
arcdev_setup(struct net_device * dev)3661da177e4SLinus Torvalds static void arcdev_setup(struct net_device *dev)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	dev->type = ARPHRD_ARCNET;
369bca5b893SStephen Hemminger 	dev->netdev_ops = &arcnet_netdev_ops;
3703b04dddeSStephen Hemminger 	dev->header_ops = &arcnet_header_ops;
371980137a2SMichael Grzeschik 	dev->hard_header_len = sizeof(struct arc_hardware);
3721da177e4SLinus Torvalds 	dev->mtu = choose_mtu();
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	dev->addr_len = ARCNET_ALEN;
3751da177e4SLinus Torvalds 	dev->tx_queue_len = 100;
3761da177e4SLinus Torvalds 	dev->broadcast[0] = 0x00;	/* for us, broadcasts are address 0 */
3771da177e4SLinus Torvalds 	dev->watchdog_timeo = TX_TIMEOUT;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	/* New-style flags. */
3801da177e4SLinus Torvalds 	dev->flags = IFF_BROADCAST;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
arcnet_timer(struct timer_list * t)383c58320deSKees Cook static void arcnet_timer(struct timer_list *t)
38459fbcbc6SMichael Grzeschik {
385c58320deSKees Cook 	struct arcnet_local *lp = from_timer(lp, t, timer);
386c58320deSKees Cook 	struct net_device *dev = lp->dev;
38759fbcbc6SMichael Grzeschik 
38801365633SAhmed S. Darwish 	spin_lock_irq(&lp->lock);
38901365633SAhmed S. Darwish 
39001365633SAhmed S. Darwish 	if (!lp->reset_in_progress && !netif_carrier_ok(dev)) {
39159fbcbc6SMichael Grzeschik 		netif_carrier_on(dev);
39259fbcbc6SMichael Grzeschik 		netdev_info(dev, "link up\n");
39359fbcbc6SMichael Grzeschik 	}
39401365633SAhmed S. Darwish 
39501365633SAhmed S. Darwish 	spin_unlock_irq(&lp->lock);
39601365633SAhmed S. Darwish }
39701365633SAhmed S. Darwish 
reset_device_work(struct work_struct * work)39801365633SAhmed S. Darwish static void reset_device_work(struct work_struct *work)
39901365633SAhmed S. Darwish {
40001365633SAhmed S. Darwish 	struct arcnet_local *lp;
40101365633SAhmed S. Darwish 	struct net_device *dev;
40201365633SAhmed S. Darwish 
40301365633SAhmed S. Darwish 	lp = container_of(work, struct arcnet_local, reset_work);
40401365633SAhmed S. Darwish 	dev = lp->dev;
40501365633SAhmed S. Darwish 
40601365633SAhmed S. Darwish 	/* Do not bring the network interface back up if an ifdown
40701365633SAhmed S. Darwish 	 * was already done.
40801365633SAhmed S. Darwish 	 */
40901365633SAhmed S. Darwish 	if (!netif_running(dev) || !lp->reset_in_progress)
41001365633SAhmed S. Darwish 		return;
41101365633SAhmed S. Darwish 
41201365633SAhmed S. Darwish 	rtnl_lock();
41301365633SAhmed S. Darwish 
41401365633SAhmed S. Darwish 	/* Do another check, in case of an ifdown that was triggered in
41501365633SAhmed S. Darwish 	 * the small race window between the exit condition above and
41601365633SAhmed S. Darwish 	 * acquiring RTNL.
41701365633SAhmed S. Darwish 	 */
41801365633SAhmed S. Darwish 	if (!netif_running(dev) || !lp->reset_in_progress)
41901365633SAhmed S. Darwish 		goto out;
42001365633SAhmed S. Darwish 
42101365633SAhmed S. Darwish 	dev_close(dev);
42201365633SAhmed S. Darwish 	dev_open(dev, NULL);
42301365633SAhmed S. Darwish 
42401365633SAhmed S. Darwish out:
42501365633SAhmed S. Darwish 	rtnl_unlock();
42659fbcbc6SMichael Grzeschik }
42759fbcbc6SMichael Grzeschik 
arcnet_reply_work(struct work_struct * t)428*775d2e2bSAllen Pais static void arcnet_reply_work(struct work_struct *t)
42905fcd31cSMichael Grzeschik {
430*775d2e2bSAllen Pais 	struct arcnet_local *lp = from_work(lp, t, reply_work);
43105fcd31cSMichael Grzeschik 
43205fcd31cSMichael Grzeschik 	struct sk_buff *ackskb, *skb;
43305fcd31cSMichael Grzeschik 	struct sock_exterr_skb *serr;
43405fcd31cSMichael Grzeschik 	struct sock *sk;
43505fcd31cSMichael Grzeschik 	int ret;
43605fcd31cSMichael Grzeschik 
43705fcd31cSMichael Grzeschik 	local_irq_disable();
43805fcd31cSMichael Grzeschik 	skb = lp->outgoing.skb;
43905fcd31cSMichael Grzeschik 	if (!skb || !skb->sk) {
44005fcd31cSMichael Grzeschik 		local_irq_enable();
44105fcd31cSMichael Grzeschik 		return;
44205fcd31cSMichael Grzeschik 	}
44305fcd31cSMichael Grzeschik 
44405fcd31cSMichael Grzeschik 	sock_hold(skb->sk);
44505fcd31cSMichael Grzeschik 	sk = skb->sk;
44605fcd31cSMichael Grzeschik 	ackskb = skb_clone_sk(skb);
44705fcd31cSMichael Grzeschik 	sock_put(skb->sk);
44805fcd31cSMichael Grzeschik 
44905fcd31cSMichael Grzeschik 	if (!ackskb) {
45005fcd31cSMichael Grzeschik 		local_irq_enable();
45105fcd31cSMichael Grzeschik 		return;
45205fcd31cSMichael Grzeschik 	}
45305fcd31cSMichael Grzeschik 
45405fcd31cSMichael Grzeschik 	serr = SKB_EXT_ERR(ackskb);
45505fcd31cSMichael Grzeschik 	memset(serr, 0, sizeof(*serr));
45605fcd31cSMichael Grzeschik 	serr->ee.ee_errno = ENOMSG;
45705fcd31cSMichael Grzeschik 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
45805fcd31cSMichael Grzeschik 	serr->ee.ee_data = skb_shinfo(skb)->tskey;
45905fcd31cSMichael Grzeschik 	serr->ee.ee_info = lp->reply_status;
46005fcd31cSMichael Grzeschik 
46105fcd31cSMichael Grzeschik 	/* finally erasing outgoing skb */
46205fcd31cSMichael Grzeschik 	dev_kfree_skb(lp->outgoing.skb);
46305fcd31cSMichael Grzeschik 	lp->outgoing.skb = NULL;
46405fcd31cSMichael Grzeschik 
46505fcd31cSMichael Grzeschik 	ackskb->dev = lp->dev;
46605fcd31cSMichael Grzeschik 
46705fcd31cSMichael Grzeschik 	ret = sock_queue_err_skb(sk, ackskb);
46805fcd31cSMichael Grzeschik 	if (ret)
469786c96e9SJinjie Ruan 		dev_kfree_skb_irq(ackskb);
47005fcd31cSMichael Grzeschik 
47105fcd31cSMichael Grzeschik 	local_irq_enable();
47205fcd31cSMichael Grzeschik };
47305fcd31cSMichael Grzeschik 
alloc_arcdev(const char * name)474bca5b893SStephen Hemminger struct net_device *alloc_arcdev(const char *name)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	struct net_device *dev;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	dev = alloc_netdev(sizeof(struct arcnet_local),
479c835a677STom Gundersen 			   name && *name ? name : "arc%d", NET_NAME_UNKNOWN,
480c835a677STom Gundersen 			   arcdev_setup);
4811da177e4SLinus Torvalds 	if (dev) {
482454d7c9bSWang Chen 		struct arcnet_local *lp = netdev_priv(dev);
48301a1d5acSJoe Perches 
48405fcd31cSMichael Grzeschik 		lp->dev = dev;
4851da177e4SLinus Torvalds 		spin_lock_init(&lp->lock);
486c58320deSKees Cook 		timer_setup(&lp->timer, arcnet_timer, 0);
48701365633SAhmed S. Darwish 		INIT_WORK(&lp->reset_work, reset_device_work);
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	return dev;
4911da177e4SLinus Torvalds }
492811eafc0SJoe Perches EXPORT_SYMBOL(alloc_arcdev);
4931da177e4SLinus Torvalds 
free_arcdev(struct net_device * dev)49401365633SAhmed S. Darwish void free_arcdev(struct net_device *dev)
49501365633SAhmed S. Darwish {
49601365633SAhmed S. Darwish 	struct arcnet_local *lp = netdev_priv(dev);
49701365633SAhmed S. Darwish 
49801365633SAhmed S. Darwish 	/* Do not cancel this at ->ndo_close(), as the workqueue itself
49901365633SAhmed S. Darwish 	 * indirectly calls the ifdown path through dev_close().
50001365633SAhmed S. Darwish 	 */
50101365633SAhmed S. Darwish 	cancel_work_sync(&lp->reset_work);
50201365633SAhmed S. Darwish 	free_netdev(dev);
50301365633SAhmed S. Darwish }
50401365633SAhmed S. Darwish EXPORT_SYMBOL(free_arcdev);
50501365633SAhmed S. Darwish 
506f2f0a16bSJoe Perches /* Open/initialize the board.  This is called sometime after booting when
5071da177e4SLinus Torvalds  * the 'ifconfig' program is run.
5081da177e4SLinus Torvalds  *
5091da177e4SLinus Torvalds  * This routine should set everything up anew at each open, even registers
5101da177e4SLinus Torvalds  * that "should" only need to be set once at boot, so that there is
5111da177e4SLinus Torvalds  * non-reboot way to recover if something goes wrong.
5121da177e4SLinus Torvalds  */
arcnet_open(struct net_device * dev)513bca5b893SStephen Hemminger int arcnet_open(struct net_device *dev)
5141da177e4SLinus Torvalds {
515454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
5161da177e4SLinus Torvalds 	int count, newmtu, error;
5171da177e4SLinus Torvalds 
518a34c0932SJoe Perches 	arc_printk(D_INIT, dev, "opened.");
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	if (!try_module_get(lp->hw.owner))
5211da177e4SLinus Torvalds 		return -ENODEV;
5221da177e4SLinus Torvalds 
52372aeea48SJoe Perches 	if (BUGLVL(D_PROTO)) {
524a34c0932SJoe Perches 		arc_printk(D_PROTO, dev, "protocol map (default is '%c'): ",
5251da177e4SLinus Torvalds 			   arc_proto_default->suffix);
5261da177e4SLinus Torvalds 		for (count = 0; count < 256; count++)
527a34c0932SJoe Perches 			arc_cont(D_PROTO, "%c", arc_proto_map[count]->suffix);
528a34c0932SJoe Perches 		arc_cont(D_PROTO, "\n");
5291da177e4SLinus Torvalds 	}
5301da177e4SLinus Torvalds 
531*775d2e2bSAllen Pais 	INIT_WORK(&lp->reply_work, arcnet_reply_work);
53205fcd31cSMichael Grzeschik 
533a34c0932SJoe Perches 	arc_printk(D_INIT, dev, "arcnet_open: resetting card.\n");
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	/* try to put the card in a defined state - if it fails the first
5361da177e4SLinus Torvalds 	 * time, actually reset it.
5371da177e4SLinus Torvalds 	 */
5381da177e4SLinus Torvalds 	error = -ENODEV;
539e15b0362SJoe Perches 	if (lp->hw.reset(dev, 0) && lp->hw.reset(dev, 1))
5401da177e4SLinus Torvalds 		goto out_module_put;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	newmtu = choose_mtu();
5431da177e4SLinus Torvalds 	if (newmtu < dev->mtu)
5441da177e4SLinus Torvalds 		dev->mtu = newmtu;
5451da177e4SLinus Torvalds 
546a34c0932SJoe Perches 	arc_printk(D_INIT, dev, "arcnet_open: mtu: %d.\n", dev->mtu);
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	/* autodetect the encapsulation for each host. */
5491da177e4SLinus Torvalds 	memset(lp->default_proto, 0, sizeof(lp->default_proto));
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	/* the broadcast address is special - use the 'bcast' protocol */
5521da177e4SLinus Torvalds 	for (count = 0; count < 256; count++) {
5531da177e4SLinus Torvalds 		if (arc_proto_map[count] == arc_bcast_proto) {
5541da177e4SLinus Torvalds 			lp->default_proto[0] = count;
5551da177e4SLinus Torvalds 			break;
5561da177e4SLinus Torvalds 		}
5571da177e4SLinus Torvalds 	}
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	/* initialize buffers */
5601da177e4SLinus Torvalds 	atomic_set(&lp->buf_lock, 1);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	lp->next_buf = lp->first_free_buf = 0;
5631da177e4SLinus Torvalds 	release_arcbuf(dev, 0);
5641da177e4SLinus Torvalds 	release_arcbuf(dev, 1);
5651da177e4SLinus Torvalds 	release_arcbuf(dev, 2);
5661da177e4SLinus Torvalds 	release_arcbuf(dev, 3);
5671da177e4SLinus Torvalds 	lp->cur_tx = lp->next_tx = -1;
5681da177e4SLinus Torvalds 	lp->cur_rx = -1;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	lp->rfc1201.sequence = 1;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	/* bring up the hardware driver */
5731da177e4SLinus Torvalds 	if (lp->hw.open)
5741da177e4SLinus Torvalds 		lp->hw.open(dev);
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	if (dev->dev_addr[0] == 0)
577a34c0932SJoe Perches 		arc_printk(D_NORMAL, dev, "WARNING!  Station address 00 is reserved for broadcasts!\n");
5781da177e4SLinus Torvalds 	else if (dev->dev_addr[0] == 255)
579a34c0932SJoe Perches 		arc_printk(D_NORMAL, dev, "WARNING!  Station address FF may confuse DOS networking programs!\n");
5801da177e4SLinus Torvalds 
581a34c0932SJoe Perches 	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
582e15b0362SJoe Perches 	if (lp->hw.status(dev) & RESETflag) {
583a34c0932SJoe Perches 		arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
584a34c0932SJoe Perches 			   __FILE__, __LINE__, __func__);
585e15b0362SJoe Perches 		lp->hw.command(dev, CFLAGScmd | RESETclear);
5861da177e4SLinus Torvalds 	}
5871da177e4SLinus Torvalds 
588a34c0932SJoe Perches 	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
5891da177e4SLinus Torvalds 	/* make sure we're ready to receive IRQ's. */
590e15b0362SJoe Perches 	lp->hw.intmask(dev, 0);
5911da177e4SLinus Torvalds 	udelay(1);		/* give it time to set the mask before
5921da177e4SLinus Torvalds 				 * we reset it again. (may not even be
5931da177e4SLinus Torvalds 				 * necessary)
5941da177e4SLinus Torvalds 				 */
595a34c0932SJoe Perches 	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
5961da177e4SLinus Torvalds 	lp->intmask = NORXflag | RECONflag;
597e15b0362SJoe Perches 	lp->hw.intmask(dev, lp->intmask);
598a34c0932SJoe Perches 	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
5991da177e4SLinus Torvalds 
60059fbcbc6SMichael Grzeschik 	netif_carrier_off(dev);
6011da177e4SLinus Torvalds 	netif_start_queue(dev);
60259fbcbc6SMichael Grzeschik 	mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000));
6031da177e4SLinus Torvalds 
6048890624aSMichael Grzeschik 	arcnet_led_event(dev, ARCNET_LED_EVENT_OPEN);
6051da177e4SLinus Torvalds 	return 0;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds  out_module_put:
6081da177e4SLinus Torvalds 	module_put(lp->hw.owner);
6091da177e4SLinus Torvalds 	return error;
6101da177e4SLinus Torvalds }
611811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_open);
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds /* The inverse routine to arcnet_open - shuts down the card. */
arcnet_close(struct net_device * dev)614bca5b893SStephen Hemminger int arcnet_close(struct net_device *dev)
6151da177e4SLinus Torvalds {
616454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
6171da177e4SLinus Torvalds 
6188890624aSMichael Grzeschik 	arcnet_led_event(dev, ARCNET_LED_EVENT_STOP);
61959fbcbc6SMichael Grzeschik 	del_timer_sync(&lp->timer);
62059fbcbc6SMichael Grzeschik 
6211da177e4SLinus Torvalds 	netif_stop_queue(dev);
62259fbcbc6SMichael Grzeschik 	netif_carrier_off(dev);
6231da177e4SLinus Torvalds 
624*775d2e2bSAllen Pais 	cancel_work_sync(&lp->reply_work);
62505fcd31cSMichael Grzeschik 
6261da177e4SLinus Torvalds 	/* flush TX and disable RX */
627e15b0362SJoe Perches 	lp->hw.intmask(dev, 0);
628e15b0362SJoe Perches 	lp->hw.command(dev, NOTXcmd);	/* stop transmit */
629e15b0362SJoe Perches 	lp->hw.command(dev, NORXcmd);	/* disable receive */
6301da177e4SLinus Torvalds 	mdelay(1);
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	/* shut down the card */
6331da177e4SLinus Torvalds 	lp->hw.close(dev);
63401365633SAhmed S. Darwish 
63501365633SAhmed S. Darwish 	/* reset counters */
63601365633SAhmed S. Darwish 	lp->reset_in_progress = 0;
63701365633SAhmed S. Darwish 
6381da177e4SLinus Torvalds 	module_put(lp->hw.owner);
6391da177e4SLinus Torvalds 	return 0;
6401da177e4SLinus Torvalds }
641811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_close);
6421da177e4SLinus Torvalds 
arcnet_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned len)6431da177e4SLinus Torvalds static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
6443b04dddeSStephen Hemminger 			 unsigned short type, const void *daddr,
6453b04dddeSStephen Hemminger 			 const void *saddr, unsigned len)
6461da177e4SLinus Torvalds {
6473b04dddeSStephen Hemminger 	const struct arcnet_local *lp = netdev_priv(dev);
6481da177e4SLinus Torvalds 	uint8_t _daddr, proto_num;
6491da177e4SLinus Torvalds 	struct ArcProto *proto;
6501da177e4SLinus Torvalds 
651a34c0932SJoe Perches 	arc_printk(D_DURING, dev,
6521da177e4SLinus Torvalds 		   "create header from %d to %d; protocol %d (%Xh); size %u.\n",
6531da177e4SLinus Torvalds 		   saddr ? *(uint8_t *)saddr : -1,
6541da177e4SLinus Torvalds 		   daddr ? *(uint8_t *)daddr : -1,
6551da177e4SLinus Torvalds 		   type, type, len);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	if (skb->len != 0 && len != skb->len)
658a34c0932SJoe Perches 		arc_printk(D_NORMAL, dev, "arcnet_header: Yikes!  skb->len(%d) != len(%d)!\n",
6591da177e4SLinus Torvalds 			   skb->len, len);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	/* Type is host order - ? */
6621da177e4SLinus Torvalds 	if (type == ETH_P_ARCNET) {
6631da177e4SLinus Torvalds 		proto = arc_raw_proto;
664a34c0932SJoe Perches 		arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n",
665a34c0932SJoe Perches 			   proto->suffix);
6661da177e4SLinus Torvalds 		_daddr = daddr ? *(uint8_t *)daddr : 0;
6677f5e760cSJoe Perches 	} else if (!daddr) {
668f2f0a16bSJoe Perches 		/* if the dest addr isn't provided, we can't choose an
669f2f0a16bSJoe Perches 		 * encapsulation!  Store the packet type (eg. ETH_P_IP)
670f2f0a16bSJoe Perches 		 * for now, and we'll push on a real header when we do
671f2f0a16bSJoe Perches 		 * rebuild_header.
6721da177e4SLinus Torvalds 		 */
6731da177e4SLinus Torvalds 		*(uint16_t *)skb_push(skb, 2) = type;
674f2f0a16bSJoe Perches 		/* XXX: Why not use skb->mac_len? */
675b0e380b1SArnaldo Carvalho de Melo 		if (skb->network_header - skb->mac_header != 2)
676a34c0932SJoe Perches 			arc_printk(D_NORMAL, dev, "arcnet_header: Yikes!  diff (%u) is not 2!\n",
677a34c0932SJoe Perches 				   skb->network_header - skb->mac_header);
6781da177e4SLinus Torvalds 		return -2;	/* return error -- can't transmit yet! */
6797f5e760cSJoe Perches 	} else {
6801da177e4SLinus Torvalds 		/* otherwise, we can just add the header as usual. */
6811da177e4SLinus Torvalds 		_daddr = *(uint8_t *)daddr;
6821da177e4SLinus Torvalds 		proto_num = lp->default_proto[_daddr];
6831da177e4SLinus Torvalds 		proto = arc_proto_map[proto_num];
684a34c0932SJoe Perches 		arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n",
6851da177e4SLinus Torvalds 			   proto_num, proto->suffix);
6861da177e4SLinus Torvalds 		if (proto == &arc_proto_null && arc_bcast_proto != proto) {
687a34c0932SJoe Perches 			arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n",
6881da177e4SLinus Torvalds 				   arc_bcast_proto->suffix);
6891da177e4SLinus Torvalds 			proto = arc_bcast_proto;
6901da177e4SLinus Torvalds 		}
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 	return proto->build_header(skb, dev, type, _daddr);
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds /* Called by the kernel in order to transmit a packet. */
arcnet_send_packet(struct sk_buff * skb,struct net_device * dev)69661357325SStephen Hemminger netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
69761357325SStephen Hemminger 			       struct net_device *dev)
6981da177e4SLinus Torvalds {
699454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
7001da177e4SLinus Torvalds 	struct archdr *pkt;
7011da177e4SLinus Torvalds 	struct arc_rfc1201 *soft;
7021da177e4SLinus Torvalds 	struct ArcProto *proto;
7031da177e4SLinus Torvalds 	int txbuf;
7041da177e4SLinus Torvalds 	unsigned long flags;
705b82de0e2SMichael Grzeschik 	int retval;
7061da177e4SLinus Torvalds 
707a34c0932SJoe Perches 	arc_printk(D_DURING, dev,
7081da177e4SLinus Torvalds 		   "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
709e15b0362SJoe Perches 		   lp->hw.status(dev), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	pkt = (struct archdr *)skb->data;
7121da177e4SLinus Torvalds 	soft = &pkt->soft.rfc1201;
7131da177e4SLinus Torvalds 	proto = arc_proto_map[soft->proto];
7141da177e4SLinus Torvalds 
715a34c0932SJoe Perches 	arc_printk(D_SKB_SIZE, dev, "skb: transmitting %d bytes to %02X\n",
7161da177e4SLinus Torvalds 		   skb->len, pkt->hard.dest);
71772aeea48SJoe Perches 	if (BUGLVL(D_SKB))
71872aeea48SJoe Perches 		arcnet_dump_skb(dev, skb, "tx");
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	/* fits in one packet? */
7211da177e4SLinus Torvalds 	if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
722a34c0932SJoe Perches 		arc_printk(D_NORMAL, dev, "fixme: packet too large: compensating badly!\n");
7231da177e4SLinus Torvalds 		dev_kfree_skb(skb);
724c6bb15a0SPieter Dejaeghere 		return NETDEV_TX_OK;	/* don't try again */
7251da177e4SLinus Torvalds 	}
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	/* We're busy transmitting a packet... */
7281da177e4SLinus Torvalds 	netif_stop_queue(dev);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	spin_lock_irqsave(&lp->lock, flags);
731e15b0362SJoe Perches 	lp->hw.intmask(dev, 0);
732c6bb15a0SPieter Dejaeghere 	if (lp->next_tx == -1)
7331da177e4SLinus Torvalds 		txbuf = get_arcbuf(dev);
7347f5e760cSJoe Perches 	else
735c6bb15a0SPieter Dejaeghere 		txbuf = -1;
7367f5e760cSJoe Perches 
7371da177e4SLinus Torvalds 	if (txbuf != -1) {
73805fcd31cSMichael Grzeschik 		lp->outgoing.skb = skb;
7391da177e4SLinus Torvalds 		if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
7401da177e4SLinus Torvalds 		    !proto->ack_tx) {
7411da177e4SLinus Torvalds 			/* done right away and we don't want to acknowledge
742f2f0a16bSJoe Perches 			 *  the package later - forget about it now
743f2f0a16bSJoe Perches 			 */
7445803c512SStephen Hemminger 			dev->stats.tx_bytes += skb->len;
7451da177e4SLinus Torvalds 		} else {
7461da177e4SLinus Torvalds 			/* do it the 'split' way */
7471da177e4SLinus Torvalds 			lp->outgoing.proto = proto;
7481da177e4SLinus Torvalds 			lp->outgoing.skb = skb;
7491da177e4SLinus Torvalds 			lp->outgoing.pkt = pkt;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 			if (proto->continue_tx &&
7521da177e4SLinus Torvalds 			    proto->continue_tx(dev, txbuf)) {
753a34c0932SJoe Perches 				arc_printk(D_NORMAL, dev,
7543b4e5551SJoe Perches 					   "bug! continue_tx finished the first time! (proto='%c')\n",
7553b4e5551SJoe Perches 					   proto->suffix);
7561da177e4SLinus Torvalds 			}
7571da177e4SLinus Torvalds 		}
758c6bb15a0SPieter Dejaeghere 		retval = NETDEV_TX_OK;
7591da177e4SLinus Torvalds 		lp->next_tx = txbuf;
7601da177e4SLinus Torvalds 	} else {
761c6bb15a0SPieter Dejaeghere 		retval = NETDEV_TX_BUSY;
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 
764a34c0932SJoe Perches 	arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
765e15b0362SJoe Perches 		   __FILE__, __LINE__, __func__, lp->hw.status(dev));
7661da177e4SLinus Torvalds 	/* make sure we didn't ignore a TX IRQ while we were in here */
767e15b0362SJoe Perches 	lp->hw.intmask(dev, 0);
7681da177e4SLinus Torvalds 
769a34c0932SJoe Perches 	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
7701da177e4SLinus Torvalds 	lp->intmask |= TXFREEflag | EXCNAKflag;
771e15b0362SJoe Perches 	lp->hw.intmask(dev, lp->intmask);
772a34c0932SJoe Perches 	arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
773e15b0362SJoe Perches 		   __FILE__, __LINE__, __func__, lp->hw.status(dev));
7741da177e4SLinus Torvalds 
7758890624aSMichael Grzeschik 	arcnet_led_event(dev, ARCNET_LED_EVENT_TX);
7768890624aSMichael Grzeschik 
7771da177e4SLinus Torvalds 	spin_unlock_irqrestore(&lp->lock, flags);
778c6bb15a0SPieter Dejaeghere 	return retval;		/* no need to try again */
7791da177e4SLinus Torvalds }
780811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_send_packet);
7811da177e4SLinus Torvalds 
782f2f0a16bSJoe Perches /* Actually start transmitting a packet that was loaded into a buffer
7831da177e4SLinus Torvalds  * by prepare_tx.  This should _only_ be called by the interrupt handler.
7841da177e4SLinus Torvalds  */
go_tx(struct net_device * dev)7851da177e4SLinus Torvalds static int go_tx(struct net_device *dev)
7861da177e4SLinus Torvalds {
787454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
7881da177e4SLinus Torvalds 
789a34c0932SJoe Perches 	arc_printk(D_DURING, dev, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
790e15b0362SJoe Perches 		   lp->hw.status(dev), lp->intmask, lp->next_tx, lp->cur_tx);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	if (lp->cur_tx != -1 || lp->next_tx == -1)
7931da177e4SLinus Torvalds 		return 0;
7941da177e4SLinus Torvalds 
79572aeea48SJoe Perches 	if (BUGLVL(D_TX))
79672aeea48SJoe Perches 		arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	lp->cur_tx = lp->next_tx;
7991da177e4SLinus Torvalds 	lp->next_tx = -1;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	/* start sending */
802e15b0362SJoe Perches 	lp->hw.command(dev, TXcmd | (lp->cur_tx << 3));
8031da177e4SLinus Torvalds 
8045803c512SStephen Hemminger 	dev->stats.tx_packets++;
8051da177e4SLinus Torvalds 	lp->lasttrans_dest = lp->lastload_dest;
8061da177e4SLinus Torvalds 	lp->lastload_dest = 0;
8071da177e4SLinus Torvalds 	lp->excnak_pending = 0;
8081da177e4SLinus Torvalds 	lp->intmask |= TXFREEflag | EXCNAKflag;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	return 1;
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds /* Called by the kernel when transmit times out */
arcnet_timeout(struct net_device * dev,unsigned int txqueue)8140290bd29SMichael S. Tsirkin void arcnet_timeout(struct net_device *dev, unsigned int txqueue)
8151da177e4SLinus Torvalds {
8161da177e4SLinus Torvalds 	unsigned long flags;
817454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
818e15b0362SJoe Perches 	int status = lp->hw.status(dev);
8191da177e4SLinus Torvalds 	char *msg;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	spin_lock_irqsave(&lp->lock, flags);
8221da177e4SLinus Torvalds 	if (status & TXFREEflag) {	/* transmit _DID_ finish */
8231da177e4SLinus Torvalds 		msg = " - missed IRQ?";
8241da177e4SLinus Torvalds 	} else {
8251da177e4SLinus Torvalds 		msg = "";
8265803c512SStephen Hemminger 		dev->stats.tx_aborted_errors++;
8271da177e4SLinus Torvalds 		lp->timed_out = 1;
828e15b0362SJoe Perches 		lp->hw.command(dev, NOTXcmd | (lp->cur_tx << 3));
8291da177e4SLinus Torvalds 	}
8305803c512SStephen Hemminger 	dev->stats.tx_errors++;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	/* make sure we didn't miss a TX or a EXC NAK IRQ */
833e15b0362SJoe Perches 	lp->hw.intmask(dev, 0);
8341da177e4SLinus Torvalds 	lp->intmask |= TXFREEflag | EXCNAKflag;
835e15b0362SJoe Perches 	lp->hw.intmask(dev, lp->intmask);
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	spin_unlock_irqrestore(&lp->lock, flags);
8381da177e4SLinus Torvalds 
839ff5688aeSMarcelo Feitoza Parisi 	if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
840a34c0932SJoe Perches 		arc_printk(D_EXTRA, dev, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
8411da177e4SLinus Torvalds 			   msg, status, lp->intmask, lp->lasttrans_dest);
8421da177e4SLinus Torvalds 		lp->last_timeout = jiffies;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	if (lp->cur_tx == -1)
8461da177e4SLinus Torvalds 		netif_wake_queue(dev);
8471da177e4SLinus Torvalds }
848811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_timeout);
8491da177e4SLinus Torvalds 
850f2f0a16bSJoe Perches /* The typical workload of the driver: Handle the network interface
8511da177e4SLinus Torvalds  * interrupts. Establish which device needs attention, and call the correct
8521da177e4SLinus Torvalds  * chipset interrupt handler.
8531da177e4SLinus Torvalds  */
arcnet_interrupt(int irq,void * dev_id)8547d12e780SDavid Howells irqreturn_t arcnet_interrupt(int irq, void *dev_id)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	struct net_device *dev = dev_id;
8571da177e4SLinus Torvalds 	struct arcnet_local *lp;
8581da177e4SLinus Torvalds 	int recbuf, status, diagstatus, didsomething, boguscount;
8595b858403SMichael Grzeschik 	unsigned long flags;
8601da177e4SLinus Torvalds 	int retval = IRQ_NONE;
8611da177e4SLinus Torvalds 
862a34c0932SJoe Perches 	arc_printk(D_DURING, dev, "\n");
8631da177e4SLinus Torvalds 
864a34c0932SJoe Perches 	arc_printk(D_DURING, dev, "in arcnet_interrupt\n");
8651da177e4SLinus Torvalds 
866454d7c9bSWang Chen 	lp = netdev_priv(dev);
8675d9428deSEric Sesterhenn 	BUG_ON(!lp);
8681da177e4SLinus Torvalds 
8695b858403SMichael Grzeschik 	spin_lock_irqsave(&lp->lock, flags);
8701da177e4SLinus Torvalds 
87101365633SAhmed S. Darwish 	if (lp->reset_in_progress)
87201365633SAhmed S. Darwish 		goto out;
87301365633SAhmed S. Darwish 
874f2f0a16bSJoe Perches 	/* RESET flag was enabled - if device is not running, we must
875f2f0a16bSJoe Perches 	 * clear it right away (but nothing else).
8761da177e4SLinus Torvalds 	 */
8771da177e4SLinus Torvalds 	if (!netif_running(dev)) {
878e15b0362SJoe Perches 		if (lp->hw.status(dev) & RESETflag)
879e15b0362SJoe Perches 			lp->hw.command(dev, CFLAGScmd | RESETclear);
880e15b0362SJoe Perches 		lp->hw.intmask(dev, 0);
8815b858403SMichael Grzeschik 		spin_unlock_irqrestore(&lp->lock, flags);
882226ee675SMichael Grzeschik 		return retval;
8831da177e4SLinus Torvalds 	}
8841da177e4SLinus Torvalds 
885a34c0932SJoe Perches 	arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
886e15b0362SJoe Perches 		   lp->hw.status(dev), lp->intmask);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	boguscount = 5;
8891da177e4SLinus Torvalds 	do {
890e15b0362SJoe Perches 		status = lp->hw.status(dev);
8911da177e4SLinus Torvalds 		diagstatus = (status >> 8) & 0xFF;
8921da177e4SLinus Torvalds 
893a34c0932SJoe Perches 		arc_printk(D_DEBUG, dev, "%s: %d: %s: status=%x\n",
894b39d66a8SHarvey Harrison 			   __FILE__, __LINE__, __func__, status);
8951da177e4SLinus Torvalds 		didsomething = 0;
8961da177e4SLinus Torvalds 
897f2f0a16bSJoe Perches 		/* RESET flag was enabled - card is resetting and if RX is
8981da177e4SLinus Torvalds 		 * disabled, it's NOT because we just got a packet.
8991da177e4SLinus Torvalds 		 *
900f2f0a16bSJoe Perches 		 * The card is in an undefined state.
901f2f0a16bSJoe Perches 		 * Clear it out and start over.
9021da177e4SLinus Torvalds 		 */
9031da177e4SLinus Torvalds 		if (status & RESETflag) {
904a34c0932SJoe Perches 			arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n",
905a34c0932SJoe Perches 				   status);
90601365633SAhmed S. Darwish 
90701365633SAhmed S. Darwish 			lp->reset_in_progress = 1;
90801365633SAhmed S. Darwish 			netif_stop_queue(dev);
90901365633SAhmed S. Darwish 			netif_carrier_off(dev);
91001365633SAhmed S. Darwish 			schedule_work(&lp->reset_work);
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 			/* get out of the interrupt handler! */
91301365633SAhmed S. Darwish 			goto out;
9141da177e4SLinus Torvalds 		}
915f2f0a16bSJoe Perches 		/* RX is inhibited - we must have received something.
916f2f0a16bSJoe Perches 		 * Prepare to receive into the next buffer.
9171da177e4SLinus Torvalds 		 *
918f2f0a16bSJoe Perches 		 * We don't actually copy the received packet from the card
919f2f0a16bSJoe Perches 		 * until after the transmit handler runs (and possibly
920f2f0a16bSJoe Perches 		 * launches the next tx); this should improve latency slightly
921f2f0a16bSJoe Perches 		 * if we get both types of interrupts at once.
9221da177e4SLinus Torvalds 		 */
9231da177e4SLinus Torvalds 		recbuf = -1;
9241da177e4SLinus Torvalds 		if (status & lp->intmask & NORXflag) {
9251da177e4SLinus Torvalds 			recbuf = lp->cur_rx;
926a34c0932SJoe Perches 			arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n",
9271da177e4SLinus Torvalds 				   recbuf, status);
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 			lp->cur_rx = get_arcbuf(dev);
9301da177e4SLinus Torvalds 			if (lp->cur_rx != -1) {
931a34c0932SJoe Perches 				arc_printk(D_DURING, dev, "enabling receive to buffer #%d\n",
9321da177e4SLinus Torvalds 					   lp->cur_rx);
933e15b0362SJoe Perches 				lp->hw.command(dev, RXcmd | (lp->cur_rx << 3) | RXbcasts);
9341da177e4SLinus Torvalds 			}
9351da177e4SLinus Torvalds 			didsomething++;
9361da177e4SLinus Torvalds 		}
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 		if ((diagstatus & EXCNAKflag)) {
939a34c0932SJoe Perches 			arc_printk(D_DURING, dev, "EXCNAK IRQ (diagstat=%Xh)\n",
9401da177e4SLinus Torvalds 				   diagstatus);
9411da177e4SLinus Torvalds 
942e15b0362SJoe Perches 			lp->hw.command(dev, NOTXcmd);      /* disable transmit */
9431da177e4SLinus Torvalds 			lp->excnak_pending = 1;
9441da177e4SLinus Torvalds 
945e15b0362SJoe Perches 			lp->hw.command(dev, EXCNAKclear);
9461da177e4SLinus Torvalds 			lp->intmask &= ~(EXCNAKflag);
9471da177e4SLinus Torvalds 			didsomething++;
9481da177e4SLinus Torvalds 		}
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 		/* a transmit finished, and we're interested in it. */
9511da177e4SLinus Torvalds 		if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
95205fcd31cSMichael Grzeschik 			int ackstatus;
9531da177e4SLinus Torvalds 			lp->intmask &= ~(TXFREEflag | EXCNAKflag);
9541da177e4SLinus Torvalds 
95505fcd31cSMichael Grzeschik 			if (status & TXACKflag)
95605fcd31cSMichael Grzeschik 				ackstatus = 2;
95705fcd31cSMichael Grzeschik 			else if (lp->excnak_pending)
95805fcd31cSMichael Grzeschik 				ackstatus = 1;
95905fcd31cSMichael Grzeschik 			else
96005fcd31cSMichael Grzeschik 				ackstatus = 0;
96105fcd31cSMichael Grzeschik 
962d6d7d3edSJoe Perches 			arc_printk(D_DURING, dev, "TX IRQ (stat=%Xh)\n",
963d6d7d3edSJoe Perches 				   status);
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 			if (lp->cur_tx != -1 && !lp->timed_out) {
9661da177e4SLinus Torvalds 				if (!(status & TXACKflag)) {
9671da177e4SLinus Torvalds 					if (lp->lasttrans_dest != 0) {
968a34c0932SJoe Perches 						arc_printk(D_EXTRA, dev,
9693b4e5551SJoe Perches 							   "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
970a34c0932SJoe Perches 							   status,
971a34c0932SJoe Perches 							   lp->lasttrans_dest);
9725803c512SStephen Hemminger 						dev->stats.tx_errors++;
9735803c512SStephen Hemminger 						dev->stats.tx_carrier_errors++;
9741da177e4SLinus Torvalds 					} else {
975a34c0932SJoe Perches 						arc_printk(D_DURING, dev,
9763b4e5551SJoe Perches 							   "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
977a34c0932SJoe Perches 							   status,
978a34c0932SJoe Perches 							   lp->lasttrans_dest);
9791da177e4SLinus Torvalds 					}
9801da177e4SLinus Torvalds 				}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 				if (lp->outgoing.proto &&
9831da177e4SLinus Torvalds 				    lp->outgoing.proto->ack_tx) {
9841da177e4SLinus Torvalds 					lp->outgoing.proto
9851da177e4SLinus Torvalds 						->ack_tx(dev, ackstatus);
9861da177e4SLinus Torvalds 				}
98705fcd31cSMichael Grzeschik 				lp->reply_status = ackstatus;
988*775d2e2bSAllen Pais 				queue_work(system_bh_highpri_wq, &lp->reply_work);
9891da177e4SLinus Torvalds 			}
9901da177e4SLinus Torvalds 			if (lp->cur_tx != -1)
9911da177e4SLinus Torvalds 				release_arcbuf(dev, lp->cur_tx);
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 			lp->cur_tx = -1;
9941da177e4SLinus Torvalds 			lp->timed_out = 0;
9951da177e4SLinus Torvalds 			didsomething++;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 			/* send another packet if there is one */
9981da177e4SLinus Torvalds 			go_tx(dev);
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 			/* continue a split packet, if any */
1001d6d7d3edSJoe Perches 			if (lp->outgoing.proto &&
1002d6d7d3edSJoe Perches 			    lp->outgoing.proto->continue_tx) {
10031da177e4SLinus Torvalds 				int txbuf = get_arcbuf(dev);
100401a1d5acSJoe Perches 
10051da177e4SLinus Torvalds 				if (txbuf != -1) {
10061da177e4SLinus Torvalds 					if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
10071da177e4SLinus Torvalds 						/* that was the last segment */
10085803c512SStephen Hemminger 						dev->stats.tx_bytes += lp->outgoing.skb->len;
10097f5e760cSJoe Perches 						if (!lp->outgoing.proto->ack_tx) {
10101da177e4SLinus Torvalds 							dev_kfree_skb_irq(lp->outgoing.skb);
10111da177e4SLinus Torvalds 							lp->outgoing.proto = NULL;
10121da177e4SLinus Torvalds 						}
10131da177e4SLinus Torvalds 					}
10141da177e4SLinus Torvalds 					lp->next_tx = txbuf;
10151da177e4SLinus Torvalds 				}
10161da177e4SLinus Torvalds 			}
10171da177e4SLinus Torvalds 			/* inform upper layers of idleness, if necessary */
10181da177e4SLinus Torvalds 			if (lp->cur_tx == -1)
10191da177e4SLinus Torvalds 				netif_wake_queue(dev);
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 		/* now process the received packet, if any */
10221da177e4SLinus Torvalds 		if (recbuf != -1) {
102372aeea48SJoe Perches 			if (BUGLVL(D_RX))
102472aeea48SJoe Perches 				arcnet_dump_packet(dev, recbuf, "rx irq", 0);
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 			arcnet_rx(dev, recbuf);
10271da177e4SLinus Torvalds 			release_arcbuf(dev, recbuf);
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 			didsomething++;
10301da177e4SLinus Torvalds 		}
10311da177e4SLinus Torvalds 		if (status & lp->intmask & RECONflag) {
1032e15b0362SJoe Perches 			lp->hw.command(dev, CFLAGScmd | CONFIGclear);
10335803c512SStephen Hemminger 			dev->stats.tx_carrier_errors++;
10341da177e4SLinus Torvalds 
1035a34c0932SJoe Perches 			arc_printk(D_RECON, dev, "Network reconfiguration detected (status=%Xh)\n",
10361da177e4SLinus Torvalds 				   status);
103759fbcbc6SMichael Grzeschik 			if (netif_carrier_ok(dev)) {
103859fbcbc6SMichael Grzeschik 				netif_carrier_off(dev);
103959fbcbc6SMichael Grzeschik 				netdev_info(dev, "link down\n");
104059fbcbc6SMichael Grzeschik 			}
104159fbcbc6SMichael Grzeschik 			mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000));
104259fbcbc6SMichael Grzeschik 
10438890624aSMichael Grzeschik 			arcnet_led_event(dev, ARCNET_LED_EVENT_RECON);
1044c6bb15a0SPieter Dejaeghere 			/* MYRECON bit is at bit 7 of diagstatus */
1045c6bb15a0SPieter Dejaeghere 			if (diagstatus & 0x80)
1046a34c0932SJoe Perches 				arc_printk(D_RECON, dev, "Put out that recon myself\n");
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 			/* is the RECON info empty or old? */
10491da177e4SLinus Torvalds 			if (!lp->first_recon || !lp->last_recon ||
10509307b570SS.Caglar Onur 			    time_after(jiffies, lp->last_recon + HZ * 10)) {
10511da177e4SLinus Torvalds 				if (lp->network_down)
1052a34c0932SJoe Perches 					arc_printk(D_NORMAL, dev, "reconfiguration detected: cabling restored?\n");
10531da177e4SLinus Torvalds 				lp->first_recon = lp->last_recon = jiffies;
10541da177e4SLinus Torvalds 				lp->num_recons = lp->network_down = 0;
10551da177e4SLinus Torvalds 
1056a34c0932SJoe Perches 				arc_printk(D_DURING, dev, "recon: clearing counters.\n");
10571da177e4SLinus Torvalds 			} else {	/* add to current RECON counter */
10581da177e4SLinus Torvalds 				lp->last_recon = jiffies;
10591da177e4SLinus Torvalds 				lp->num_recons++;
10601da177e4SLinus Torvalds 
1061a34c0932SJoe Perches 				arc_printk(D_DURING, dev, "recon: counter=%d, time=%lds, net=%d\n",
10621da177e4SLinus Torvalds 					   lp->num_recons,
10631da177e4SLinus Torvalds 					   (lp->last_recon - lp->first_recon) / HZ,
10641da177e4SLinus Torvalds 					   lp->network_down);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 				/* if network is marked up;
10671da177e4SLinus Torvalds 				 * and first_recon and last_recon are 60+ apart;
10681da177e4SLinus Torvalds 				 * and the average no. of recons counted is
10691da177e4SLinus Torvalds 				 *    > RECON_THRESHOLD/min;
10701da177e4SLinus Torvalds 				 * then print a warning message.
10711da177e4SLinus Torvalds 				 */
10728e95a202SJoe Perches 				if (!lp->network_down &&
10738e95a202SJoe Perches 				    (lp->last_recon - lp->first_recon) <= HZ * 60 &&
10748e95a202SJoe Perches 				    lp->num_recons >= RECON_THRESHOLD) {
10751da177e4SLinus Torvalds 					lp->network_down = 1;
1076a34c0932SJoe Perches 					arc_printk(D_NORMAL, dev, "many reconfigurations detected: cabling problem?\n");
10778e95a202SJoe Perches 				} else if (!lp->network_down &&
10788e95a202SJoe Perches 					   lp->last_recon - lp->first_recon > HZ * 60) {
1079d6d7d3edSJoe Perches 					/* reset counters if we've gone for
1080d6d7d3edSJoe Perches 					 *  over a minute.
1081d6d7d3edSJoe Perches 					 */
10821da177e4SLinus Torvalds 					lp->first_recon = lp->last_recon;
10831da177e4SLinus Torvalds 					lp->num_recons = 1;
10841da177e4SLinus Torvalds 				}
10851da177e4SLinus Torvalds 			}
10869307b570SS.Caglar Onur 		} else if (lp->network_down &&
10879307b570SS.Caglar Onur 			   time_after(jiffies, lp->last_recon + HZ * 10)) {
10881da177e4SLinus Torvalds 			if (lp->network_down)
1089a34c0932SJoe Perches 				arc_printk(D_NORMAL, dev, "cabling restored?\n");
10901da177e4SLinus Torvalds 			lp->first_recon = lp->last_recon = 0;
10911da177e4SLinus Torvalds 			lp->num_recons = lp->network_down = 0;
10921da177e4SLinus Torvalds 
1093a34c0932SJoe Perches 			arc_printk(D_DURING, dev, "not recon: clearing counters anyway.\n");
109459fbcbc6SMichael Grzeschik 			netif_carrier_on(dev);
10951da177e4SLinus Torvalds 		}
10961da177e4SLinus Torvalds 
10977f5e760cSJoe Perches 		if (didsomething)
10981da177e4SLinus Torvalds 			retval |= IRQ_HANDLED;
10997f5e760cSJoe Perches 	} while (--boguscount && didsomething);
11001da177e4SLinus Torvalds 
1101a34c0932SJoe Perches 	arc_printk(D_DURING, dev, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
1102e15b0362SJoe Perches 		   lp->hw.status(dev), boguscount);
1103a34c0932SJoe Perches 	arc_printk(D_DURING, dev, "\n");
11041da177e4SLinus Torvalds 
1105e15b0362SJoe Perches 	lp->hw.intmask(dev, 0);
11061da177e4SLinus Torvalds 	udelay(1);
1107e15b0362SJoe Perches 	lp->hw.intmask(dev, lp->intmask);
11081da177e4SLinus Torvalds 
110901365633SAhmed S. Darwish out:
11105b858403SMichael Grzeschik 	spin_unlock_irqrestore(&lp->lock, flags);
11111da177e4SLinus Torvalds 	return retval;
11121da177e4SLinus Torvalds }
1113811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_interrupt);
11141da177e4SLinus Torvalds 
1115f2f0a16bSJoe Perches /* This is a generic packet receiver that calls arcnet??_rx depending on the
11161da177e4SLinus Torvalds  * protocol ID found.
11171da177e4SLinus Torvalds  */
arcnet_rx(struct net_device * dev,int bufnum)1118f03aa2d8SAdrian Bunk static void arcnet_rx(struct net_device *dev, int bufnum)
11191da177e4SLinus Torvalds {
1120454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
112102a07046SUwe Kleine-König 	union {
11221da177e4SLinus Torvalds 		struct archdr pkt;
112302a07046SUwe Kleine-König 		char buf[512];
112402a07046SUwe Kleine-König 	} rxdata;
11251da177e4SLinus Torvalds 	struct arc_rfc1201 *soft;
11261da177e4SLinus Torvalds 	int length, ofs;
11271da177e4SLinus Torvalds 
112802a07046SUwe Kleine-König 	soft = &rxdata.pkt.soft.rfc1201;
11291da177e4SLinus Torvalds 
113002a07046SUwe Kleine-König 	lp->hw.copy_from_card(dev, bufnum, 0, &rxdata.pkt, ARC_HDR_SIZE);
113102a07046SUwe Kleine-König 	if (rxdata.pkt.hard.offset[0]) {
113202a07046SUwe Kleine-König 		ofs = rxdata.pkt.hard.offset[0];
11331da177e4SLinus Torvalds 		length = 256 - ofs;
11341da177e4SLinus Torvalds 	} else {
113502a07046SUwe Kleine-König 		ofs = rxdata.pkt.hard.offset[1];
11361da177e4SLinus Torvalds 		length = 512 - ofs;
11371da177e4SLinus Torvalds 	}
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	/* get the full header, if possible */
114002a07046SUwe Kleine-König 	if (sizeof(rxdata.pkt.soft) <= length) {
114102a07046SUwe Kleine-König 		lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(rxdata.pkt.soft));
11427f5e760cSJoe Perches 	} else {
114302a07046SUwe Kleine-König 		memset(&rxdata.pkt.soft, 0, sizeof(rxdata.pkt.soft));
11441da177e4SLinus Torvalds 		lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 
1147a34c0932SJoe Perches 	arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
114802a07046SUwe Kleine-König 		   bufnum, rxdata.pkt.hard.source, rxdata.pkt.hard.dest, length);
11491da177e4SLinus Torvalds 
11505803c512SStephen Hemminger 	dev->stats.rx_packets++;
11515803c512SStephen Hemminger 	dev->stats.rx_bytes += length + ARC_HDR_SIZE;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	/* call the right receiver for the protocol */
11541da177e4SLinus Torvalds 	if (arc_proto_map[soft->proto]->is_ip) {
115572aeea48SJoe Perches 		if (BUGLVL(D_PROTO)) {
11561da177e4SLinus Torvalds 			struct ArcProto
115702a07046SUwe Kleine-König 			*oldp = arc_proto_map[lp->default_proto[rxdata.pkt.hard.source]],
11581da177e4SLinus Torvalds 			*newp = arc_proto_map[soft->proto];
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 			if (oldp != newp) {
1161a34c0932SJoe Perches 				arc_printk(D_PROTO, dev,
11623b4e5551SJoe Perches 					   "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
116302a07046SUwe Kleine-König 					   soft->proto, rxdata.pkt.hard.source,
11641da177e4SLinus Torvalds 					   newp->suffix, oldp->suffix);
11651da177e4SLinus Torvalds 			}
11661da177e4SLinus Torvalds 		}
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 		/* broadcasts will always be done with the last-used encap. */
11691da177e4SLinus Torvalds 		lp->default_proto[0] = soft->proto;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 		/* in striking contrast, the following isn't a hack. */
117202a07046SUwe Kleine-König 		lp->default_proto[rxdata.pkt.hard.source] = soft->proto;
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds 	/* call the protocol-specific receiver. */
117502a07046SUwe Kleine-König 	arc_proto_map[soft->proto]->rx(dev, bufnum, &rxdata.pkt, length);
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
null_rx(struct net_device * dev,int bufnum,struct archdr * pkthdr,int length)11781da177e4SLinus Torvalds static void null_rx(struct net_device *dev, int bufnum,
11791da177e4SLinus Torvalds 		    struct archdr *pkthdr, int length)
11801da177e4SLinus Torvalds {
1181a34c0932SJoe Perches 	arc_printk(D_PROTO, dev,
11821da177e4SLinus Torvalds 		   "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
11831da177e4SLinus Torvalds 		   pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
null_build_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,uint8_t daddr)11861da177e4SLinus Torvalds static int null_build_header(struct sk_buff *skb, struct net_device *dev,
11871da177e4SLinus Torvalds 			     unsigned short type, uint8_t daddr)
11881da177e4SLinus Torvalds {
1189454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
11901da177e4SLinus Torvalds 
1191a34c0932SJoe Perches 	arc_printk(D_PROTO, dev,
11921da177e4SLinus Torvalds 		   "tx: can't build header for encap %02Xh; load a protocol driver.\n",
11931da177e4SLinus Torvalds 		   lp->default_proto[daddr]);
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	/* always fails */
11961da177e4SLinus Torvalds 	return 0;
11971da177e4SLinus Torvalds }
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds /* the "do nothing" prepare_tx function warns that there's nothing to do. */
null_prepare_tx(struct net_device * dev,struct archdr * pkt,int length,int bufnum)12001da177e4SLinus Torvalds static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
12011da177e4SLinus Torvalds 			   int length, int bufnum)
12021da177e4SLinus Torvalds {
1203454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
12041da177e4SLinus Torvalds 	struct arc_hardware newpkt;
12051da177e4SLinus Torvalds 
1206a34c0932SJoe Perches 	arc_printk(D_PROTO, dev, "tx: no encap for this host; load a protocol driver.\n");
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	/* send a packet to myself -- will never get received, of course */
12091da177e4SLinus Torvalds 	newpkt.source = newpkt.dest = dev->dev_addr[0];
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	/* only one byte of actual data (and it's random) */
12121da177e4SLinus Torvalds 	newpkt.offset[0] = 0xFF;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	return 1;		/* done */
12171da177e4SLinus Torvalds }
1218