arcnet.c 28.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Linux ARCnet driver - device-independent routines
3
 *
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Written 1997 by David Woodhouse.
 * Written 1994-1999 by Avery Pennarun.
 * Written 1999-2000 by Martin Mares <mj@ucw.cz>.
 * Derived from skeleton.c by Donald Becker.
 *
 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
 *  for sponsoring the further development of this driver.
 *
 * **********************
 *
 * The original copyright was as follows:
 *
 * skeleton.c Written 1993 by Donald Becker.
 * Copyright 1993 United States Government as represented by the
 * Director, National Security Agency.  This software may only be used
 * and distributed according to the terms of the GNU General Public License as
 * modified by SRC, incorporated herein by reference.
 *
 * **********************
23
 *
L
Linus Torvalds 已提交
24 25 26 27
 * The change log is now in a file called ChangeLog in this directory.
 *
 * Sources:
 *  - Crynwr arcnet.com/arcether.com packet drivers.
28
 *  - arcnet.c v0.00 dated 1/1/94 and apparently by
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *     Donald Becker - it didn't work :)
 *  - skeleton.c v0.05 dated 11/16/93 by Donald Becker
 *     (from Linux Kernel 1.1.45)
 *  - RFC's 1201 and 1051 - re: TCP/IP over ARCnet
 *  - The official ARCnet COM9026 data sheets (!) thanks to
 *     Ken Cornetet <kcornete@nyx10.cs.du.edu>
 *  - The official ARCnet COM20020 data sheets.
 *  - Information on some more obscure ARCnet controller chips, thanks
 *     to the nice people at SMSC.
 *  - net/inet/eth.c (from kernel 1.1.50) for header-building info.
 *  - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
 *  - Textual information and more alternate source from Joachim Koenig
 *     <jojo@repas.de>
 */

44
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52

#include <linux/module.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <net/arp.h>
#include <linux/init.h>
53
#include <linux/jiffies.h>
L
Linus Torvalds 已提交
54

55
#include "arcdevice.h"
56
#include "com9026.h"
57

L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65
/* "do nothing" functions for protocol drivers */
static void null_rx(struct net_device *dev, int bufnum,
		    struct archdr *pkthdr, int length);
static int null_build_header(struct sk_buff *skb, struct net_device *dev,
			     unsigned short type, uint8_t daddr);
static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
			   int length, int bufnum);

66
static void arcnet_rx(struct net_device *dev, int bufnum);
L
Linus Torvalds 已提交
67

68
/* one ArcProto per possible proto ID.  None of the elements of
L
Linus Torvalds 已提交
69 70 71 72
 * arc_proto_map are allowed to be NULL; they will get set to
 * arc_proto_default instead.  It also must not be NULL; if you would like
 * to set it to NULL, set it to &arc_proto_null instead.
 */
73 74 75 76 77 78 79 80 81 82 83
struct ArcProto *arc_proto_map[256];
EXPORT_SYMBOL(arc_proto_map);

struct ArcProto *arc_proto_default;
EXPORT_SYMBOL(arc_proto_default);

struct ArcProto *arc_bcast_proto;
EXPORT_SYMBOL(arc_bcast_proto);

struct ArcProto *arc_raw_proto;
EXPORT_SYMBOL(arc_raw_proto);
L
Linus Torvalds 已提交
84

85
static struct ArcProto arc_proto_null = {
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	.suffix		= '?',
	.mtu		= XMTU,
	.is_ip          = 0,
	.rx		= null_rx,
	.build_header	= null_build_header,
	.prepare_tx	= null_prepare_tx,
	.continue_tx    = NULL,
	.ack_tx         = NULL
};

/* Exported function prototypes */
int arcnet_debug = ARCNET_DEBUG;
EXPORT_SYMBOL(arcnet_debug);

/* Internal function prototypes */
static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
102 103
			 unsigned short type, const void *daddr,
			 const void *saddr, unsigned len);
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112 113 114 115
static int go_tx(struct net_device *dev);

static int debug = ARCNET_DEBUG;
module_param(debug, int, 0);
MODULE_LICENSE("GPL");

static int __init arcnet_init(void)
{
	int count;

	arcnet_debug = debug;

116
	pr_info("arcnet loaded\n");
L
Linus Torvalds 已提交
117 118 119 120 121 122

	/* initialize the protocol map */
	arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
	for (count = 0; count < 256; count++)
		arc_proto_map[count] = arc_proto_default;

123
	if (BUGLVL(D_DURING))
124 125 126 127 128 129
		pr_info("struct sizes: %Zd %Zd %Zd %Zd %Zd\n",
			sizeof(struct arc_hardware),
			sizeof(struct arc_rfc1201),
			sizeof(struct arc_rfc1051),
			sizeof(struct arc_eth_encap),
			sizeof(struct archdr));
L
Linus Torvalds 已提交
130 131 132 133 134 135 136 137 138 139 140

	return 0;
}

static void __exit arcnet_exit(void)
{
}

module_init(arcnet_init);
module_exit(arcnet_exit);

141
/* Dump the contents of an sk_buff */
L
Linus Torvalds 已提交
142 143 144 145
#if ARCNET_DEBUG_MAX & D_SKB
void arcnet_dump_skb(struct net_device *dev,
		     struct sk_buff *skb, char *desc)
{
146
	char hdr[32];
L
Linus Torvalds 已提交
147

148 149 150 151
	/* dump the packet */
	snprintf(hdr, sizeof(hdr), "%6s:%s skb->data:", dev->name, desc);
	print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
		       16, 1, skb->data, skb->len, true);
L
Linus Torvalds 已提交
152 153 154 155
}
EXPORT_SYMBOL(arcnet_dump_skb);
#endif

156
/* Dump the contents of an ARCnet buffer */
L
Linus Torvalds 已提交
157
#if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
158 159
static void arcnet_dump_packet(struct net_device *dev, int bufnum,
			       char *desc, int take_arcnet_lock)
L
Linus Torvalds 已提交
160
{
161
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
162 163 164
	int i, length;
	unsigned long flags = 0;
	static uint8_t buf[512];
165
	char hdr[32];
L
Linus Torvalds 已提交
166 167

	/* hw.copy_from_card expects IRQ context so take the IRQ lock
168 169
	 * to keep it single threaded
	 */
170
	if (take_arcnet_lock)
L
Linus Torvalds 已提交
171 172 173
		spin_lock_irqsave(&lp->lock, flags);

	lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
174
	if (take_arcnet_lock)
L
Linus Torvalds 已提交
175 176 177 178 179
		spin_unlock_irqrestore(&lp->lock, flags);

	/* if the offset[0] byte is nonzero, this is a 256-byte packet */
	length = (buf[2] ? 256 : 512);

180 181 182 183
	/* dump the packet */
	snprintf(hdr, sizeof(hdr), "%6s:%s packet dump:", dev->name, desc);
	print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
		       16, 1, buf, length, true);
L
Linus Torvalds 已提交
184 185
}

186 187
#else

188
#define arcnet_dump_packet(dev, bufnum, desc, take_arcnet_lock) do { } while (0)
189

L
Linus Torvalds 已提交
190 191
#endif

192
/* Unregister a protocol driver from the arc_proto_map.  Protocol drivers
L
Linus Torvalds 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
 * are responsible for registering themselves, but the unregister routine
 * is pretty generic so we'll do it here.
 */
void arcnet_unregister_proto(struct ArcProto *proto)
{
	int count;

	if (arc_proto_default == proto)
		arc_proto_default = &arc_proto_null;
	if (arc_bcast_proto == proto)
		arc_bcast_proto = arc_proto_default;
	if (arc_raw_proto == proto)
		arc_raw_proto = arc_proto_default;

	for (count = 0; count < 256; count++) {
		if (arc_proto_map[count] == proto)
			arc_proto_map[count] = arc_proto_default;
	}
}
212
EXPORT_SYMBOL(arcnet_unregister_proto);
L
Linus Torvalds 已提交
213

214
/* Add a buffer to the queue.  Only the interrupt handler is allowed to do
L
Linus Torvalds 已提交
215
 * this, unless interrupts are disabled.
216
 *
L
Linus Torvalds 已提交
217 218 219 220 221
 * Note: we don't check for a full queue, since there aren't enough buffers
 * to more than fill it.
 */
static void release_arcbuf(struct net_device *dev, int bufnum)
{
222
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
223 224 225 226 227
	int i;

	lp->buf_queue[lp->first_free_buf++] = bufnum;
	lp->first_free_buf %= 5;

228
	if (BUGLVL(D_DURING)) {
229 230
		arc_printk(D_DURING, dev, "release_arcbuf: freed #%d; buffer queue is now: ",
			   bufnum);
231
		for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
232 233
			arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
		arc_cont(D_DURING, "\n");
L
Linus Torvalds 已提交
234 235 236
	}
}

237 238
/* Get a buffer from the queue.
 * If this returns -1, there are no buffers available.
L
Linus Torvalds 已提交
239 240 241
 */
static int get_arcbuf(struct net_device *dev)
{
242
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
243 244 245 246
	int buf = -1, i;

	if (!atomic_dec_and_test(&lp->buf_lock)) {
		/* already in this function */
247 248
		arc_printk(D_NORMAL, dev, "get_arcbuf: overlap (%d)!\n",
			   lp->buf_lock.counter);
249
	} else {			/* we can continue */
L
Linus Torvalds 已提交
250 251 252
		if (lp->next_buf >= 5)
			lp->next_buf -= 5;

253
		if (lp->next_buf == lp->first_free_buf) {
254
			arc_printk(D_NORMAL, dev, "get_arcbuf: BUG: no buffers are available??\n");
255
		} else {
L
Linus Torvalds 已提交
256 257 258 259 260
			buf = lp->buf_queue[lp->next_buf++];
			lp->next_buf %= 5;
		}
	}

261
	if (BUGLVL(D_DURING)) {
262 263
		arc_printk(D_DURING, dev, "get_arcbuf: got #%d; buffer queue is now: ",
			   buf);
264
		for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
265 266
			arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
		arc_cont(D_DURING, "\n");
L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277 278
	}

	atomic_inc(&lp->buf_lock);
	return buf;
}

static int choose_mtu(void)
{
	int count, mtu = 65535;

	/* choose the smallest MTU of all available encaps */
	for (count = 0; count < 256; count++) {
279 280
		if (arc_proto_map[count] != &arc_proto_null &&
		    arc_proto_map[count]->mtu < mtu) {
L
Linus Torvalds 已提交
281 282 283 284 285 286 287
			mtu = arc_proto_map[count]->mtu;
		}
	}

	return mtu == 65535 ? XMTU : mtu;
}

288 289 290 291
static const struct header_ops arcnet_header_ops = {
	.create = arcnet_header,
};

292 293 294 295 296 297
static const struct net_device_ops arcnet_netdev_ops = {
	.ndo_open	= arcnet_open,
	.ndo_stop	= arcnet_close,
	.ndo_start_xmit = arcnet_send_packet,
	.ndo_tx_timeout = arcnet_timeout,
};
L
Linus Torvalds 已提交
298 299 300 301 302

/* Setup a struct device for ARCnet. */
static void arcdev_setup(struct net_device *dev)
{
	dev->type = ARPHRD_ARCNET;
303
	dev->netdev_ops = &arcnet_netdev_ops;
304
	dev->header_ops = &arcnet_header_ops;
305
	dev->hard_header_len = sizeof(struct arc_hardware);
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314 315 316
	dev->mtu = choose_mtu();

	dev->addr_len = ARCNET_ALEN;
	dev->tx_queue_len = 100;
	dev->broadcast[0] = 0x00;	/* for us, broadcasts are address 0 */
	dev->watchdog_timeo = TX_TIMEOUT;

	/* New-style flags. */
	dev->flags = IFF_BROADCAST;
}

317
struct net_device *alloc_arcdev(const char *name)
L
Linus Torvalds 已提交
318 319 320 321
{
	struct net_device *dev;

	dev = alloc_netdev(sizeof(struct arcnet_local),
322 323
			   name && *name ? name : "arc%d", NET_NAME_UNKNOWN,
			   arcdev_setup);
324
	if (dev) {
325
		struct arcnet_local *lp = netdev_priv(dev);
J
Joe Perches 已提交
326

L
Linus Torvalds 已提交
327 328 329 330 331
		spin_lock_init(&lp->lock);
	}

	return dev;
}
332
EXPORT_SYMBOL(alloc_arcdev);
L
Linus Torvalds 已提交
333

334
/* Open/initialize the board.  This is called sometime after booting when
L
Linus Torvalds 已提交
335 336 337 338 339 340
 * the 'ifconfig' program is run.
 *
 * This routine should set everything up anew at each open, even registers
 * that "should" only need to be set once at boot, so that there is
 * non-reboot way to recover if something goes wrong.
 */
341
int arcnet_open(struct net_device *dev)
L
Linus Torvalds 已提交
342
{
343
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
344 345
	int count, newmtu, error;

346
	arc_printk(D_INIT, dev, "opened.");
L
Linus Torvalds 已提交
347 348 349 350

	if (!try_module_get(lp->hw.owner))
		return -ENODEV;

351
	if (BUGLVL(D_PROTO)) {
352 353
		arc_printk(D_PROTO, dev, "protocol map (default is '%c'): ",
			   arc_proto_default->suffix);
L
Linus Torvalds 已提交
354
		for (count = 0; count < 256; count++)
355 356
			arc_cont(D_PROTO, "%c", arc_proto_map[count]->suffix);
		arc_cont(D_PROTO, "\n");
L
Linus Torvalds 已提交
357 358
	}

359
	arc_printk(D_INIT, dev, "arcnet_open: resetting card.\n");
L
Linus Torvalds 已提交
360 361 362 363 364

	/* try to put the card in a defined state - if it fails the first
	 * time, actually reset it.
	 */
	error = -ENODEV;
365
	if (lp->hw.reset(dev, 0) && lp->hw.reset(dev, 1))
L
Linus Torvalds 已提交
366 367 368 369 370 371
		goto out_module_put;

	newmtu = choose_mtu();
	if (newmtu < dev->mtu)
		dev->mtu = newmtu;

372
	arc_printk(D_INIT, dev, "arcnet_open: mtu: %d.\n", dev->mtu);
L
Linus Torvalds 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

	/* autodetect the encapsulation for each host. */
	memset(lp->default_proto, 0, sizeof(lp->default_proto));

	/* the broadcast address is special - use the 'bcast' protocol */
	for (count = 0; count < 256; count++) {
		if (arc_proto_map[count] == arc_bcast_proto) {
			lp->default_proto[0] = count;
			break;
		}
	}

	/* initialize buffers */
	atomic_set(&lp->buf_lock, 1);

	lp->next_buf = lp->first_free_buf = 0;
	release_arcbuf(dev, 0);
	release_arcbuf(dev, 1);
	release_arcbuf(dev, 2);
	release_arcbuf(dev, 3);
	lp->cur_tx = lp->next_tx = -1;
	lp->cur_rx = -1;

	lp->rfc1201.sequence = 1;

	/* bring up the hardware driver */
	if (lp->hw.open)
		lp->hw.open(dev);

	if (dev->dev_addr[0] == 0)
403
		arc_printk(D_NORMAL, dev, "WARNING!  Station address 00 is reserved for broadcasts!\n");
L
Linus Torvalds 已提交
404
	else if (dev->dev_addr[0] == 255)
405
		arc_printk(D_NORMAL, dev, "WARNING!  Station address FF may confuse DOS networking programs!\n");
L
Linus Torvalds 已提交
406

407
	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
408
	if (lp->hw.status(dev) & RESETflag) {
409 410
		arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
			   __FILE__, __LINE__, __func__);
411
		lp->hw.command(dev, CFLAGScmd | RESETclear);
L
Linus Torvalds 已提交
412 413
	}

414
	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
L
Linus Torvalds 已提交
415
	/* make sure we're ready to receive IRQ's. */
416
	lp->hw.intmask(dev, 0);
L
Linus Torvalds 已提交
417 418 419 420
	udelay(1);		/* give it time to set the mask before
				 * we reset it again. (may not even be
				 * necessary)
				 */
421
	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
L
Linus Torvalds 已提交
422
	lp->intmask = NORXflag | RECONflag;
423
	lp->hw.intmask(dev, lp->intmask);
424
	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
L
Linus Torvalds 已提交
425 426 427 428 429 430 431 432 433

	netif_start_queue(dev);

	return 0;

 out_module_put:
	module_put(lp->hw.owner);
	return error;
}
434
EXPORT_SYMBOL(arcnet_open);
L
Linus Torvalds 已提交
435 436

/* The inverse routine to arcnet_open - shuts down the card. */
437
int arcnet_close(struct net_device *dev)
L
Linus Torvalds 已提交
438
{
439
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
440 441 442 443

	netif_stop_queue(dev);

	/* flush TX and disable RX */
444 445 446
	lp->hw.intmask(dev, 0);
	lp->hw.command(dev, NOTXcmd);	/* stop transmit */
	lp->hw.command(dev, NORXcmd);	/* disable receive */
L
Linus Torvalds 已提交
447 448 449 450 451 452 453
	mdelay(1);

	/* shut down the card */
	lp->hw.close(dev);
	module_put(lp->hw.owner);
	return 0;
}
454
EXPORT_SYMBOL(arcnet_close);
L
Linus Torvalds 已提交
455 456

static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
457 458
			 unsigned short type, const void *daddr,
			 const void *saddr, unsigned len)
L
Linus Torvalds 已提交
459
{
460
	const struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
461 462 463
	uint8_t _daddr, proto_num;
	struct ArcProto *proto;

464 465 466 467 468
	arc_printk(D_DURING, dev,
		   "create header from %d to %d; protocol %d (%Xh); size %u.\n",
		   saddr ? *(uint8_t *)saddr : -1,
		   daddr ? *(uint8_t *)daddr : -1,
		   type, type, len);
L
Linus Torvalds 已提交
469

470
	if (skb->len != 0 && len != skb->len)
471 472
		arc_printk(D_NORMAL, dev, "arcnet_header: Yikes!  skb->len(%d) != len(%d)!\n",
			   skb->len, len);
L
Linus Torvalds 已提交
473

474 475 476
	/* Type is host order - ? */
	if (type == ETH_P_ARCNET) {
		proto = arc_raw_proto;
477 478
		arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n",
			   proto->suffix);
479
		_daddr = daddr ? *(uint8_t *)daddr : 0;
480
	} else if (!daddr) {
481 482 483 484
		/* if the dest addr isn't provided, we can't choose an
		 * encapsulation!  Store the packet type (eg. ETH_P_IP)
		 * for now, and we'll push on a real header when we do
		 * rebuild_header.
L
Linus Torvalds 已提交
485
		 */
486
		*(uint16_t *)skb_push(skb, 2) = type;
487
		/* XXX: Why not use skb->mac_len? */
488
		if (skb->network_header - skb->mac_header != 2)
489 490
			arc_printk(D_NORMAL, dev, "arcnet_header: Yikes!  diff (%u) is not 2!\n",
				   skb->network_header - skb->mac_header);
L
Linus Torvalds 已提交
491
		return -2;	/* return error -- can't transmit yet! */
492
	} else {
L
Linus Torvalds 已提交
493
		/* otherwise, we can just add the header as usual. */
494
		_daddr = *(uint8_t *)daddr;
L
Linus Torvalds 已提交
495 496
		proto_num = lp->default_proto[_daddr];
		proto = arc_proto_map[proto_num];
497 498
		arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n",
			   proto_num, proto->suffix);
L
Linus Torvalds 已提交
499
		if (proto == &arc_proto_null && arc_bcast_proto != proto) {
500 501
			arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n",
				   arc_bcast_proto->suffix);
L
Linus Torvalds 已提交
502 503 504 505 506 507 508
			proto = arc_bcast_proto;
		}
	}
	return proto->build_header(skb, dev, type, _daddr);
}

/* Called by the kernel in order to transmit a packet. */
509
netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
510
			       struct net_device *dev)
L
Linus Torvalds 已提交
511
{
512
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
513 514 515 516 517
	struct archdr *pkt;
	struct arc_rfc1201 *soft;
	struct ArcProto *proto;
	int txbuf;
	unsigned long flags;
518
	int retval;
L
Linus Torvalds 已提交
519

520 521
	arc_printk(D_DURING, dev,
		   "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
522
		   lp->hw.status(dev), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
L
Linus Torvalds 已提交
523

524
	pkt = (struct archdr *)skb->data;
L
Linus Torvalds 已提交
525 526 527
	soft = &pkt->soft.rfc1201;
	proto = arc_proto_map[soft->proto];

528 529
	arc_printk(D_SKB_SIZE, dev, "skb: transmitting %d bytes to %02X\n",
		   skb->len, pkt->hard.dest);
530 531
	if (BUGLVL(D_SKB))
		arcnet_dump_skb(dev, skb, "tx");
L
Linus Torvalds 已提交
532 533 534

	/* fits in one packet? */
	if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
535
		arc_printk(D_NORMAL, dev, "fixme: packet too large: compensating badly!\n");
L
Linus Torvalds 已提交
536
		dev_kfree_skb(skb);
537
		return NETDEV_TX_OK;	/* don't try again */
L
Linus Torvalds 已提交
538 539 540 541 542 543
	}

	/* We're busy transmitting a packet... */
	netif_stop_queue(dev);

	spin_lock_irqsave(&lp->lock, flags);
544
	lp->hw.intmask(dev, 0);
545
	if (lp->next_tx == -1)
546
		txbuf = get_arcbuf(dev);
547
	else
548
		txbuf = -1;
549

L
Linus Torvalds 已提交
550 551 552 553
	if (txbuf != -1) {
		if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
		    !proto->ack_tx) {
			/* done right away and we don't want to acknowledge
554 555
			 *  the package later - forget about it now
			 */
556
			dev->stats.tx_bytes += skb->len;
557
			dev_kfree_skb(skb);
L
Linus Torvalds 已提交
558 559 560 561 562 563 564 565
		} else {
			/* do it the 'split' way */
			lp->outgoing.proto = proto;
			lp->outgoing.skb = skb;
			lp->outgoing.pkt = pkt;

			if (proto->continue_tx &&
			    proto->continue_tx(dev, txbuf)) {
566 567 568
				arc_printk(D_NORMAL, dev,
					   "bug! continue_tx finished the first time! (proto='%c')\n",
					   proto->suffix);
L
Linus Torvalds 已提交
569 570
			}
		}
571
		retval = NETDEV_TX_OK;
L
Linus Torvalds 已提交
572 573
		lp->next_tx = txbuf;
	} else {
574
		retval = NETDEV_TX_BUSY;
L
Linus Torvalds 已提交
575 576
	}

577
	arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
578
		   __FILE__, __LINE__, __func__, lp->hw.status(dev));
L
Linus Torvalds 已提交
579
	/* make sure we didn't ignore a TX IRQ while we were in here */
580
	lp->hw.intmask(dev, 0);
L
Linus Torvalds 已提交
581

582
	arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
583
	lp->intmask |= TXFREEflag | EXCNAKflag;
584
	lp->hw.intmask(dev, lp->intmask);
585
	arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
586
		   __FILE__, __LINE__, __func__, lp->hw.status(dev));
L
Linus Torvalds 已提交
587 588

	spin_unlock_irqrestore(&lp->lock, flags);
589
	return retval;		/* no need to try again */
L
Linus Torvalds 已提交
590
}
591
EXPORT_SYMBOL(arcnet_send_packet);
L
Linus Torvalds 已提交
592

593
/* Actually start transmitting a packet that was loaded into a buffer
L
Linus Torvalds 已提交
594 595 596 597
 * by prepare_tx.  This should _only_ be called by the interrupt handler.
 */
static int go_tx(struct net_device *dev)
{
598
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
599

600
	arc_printk(D_DURING, dev, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
601
		   lp->hw.status(dev), lp->intmask, lp->next_tx, lp->cur_tx);
L
Linus Torvalds 已提交
602 603 604 605

	if (lp->cur_tx != -1 || lp->next_tx == -1)
		return 0;

606 607
	if (BUGLVL(D_TX))
		arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
L
Linus Torvalds 已提交
608 609 610 611 612

	lp->cur_tx = lp->next_tx;
	lp->next_tx = -1;

	/* start sending */
613
	lp->hw.command(dev, TXcmd | (lp->cur_tx << 3));
L
Linus Torvalds 已提交
614

615
	dev->stats.tx_packets++;
L
Linus Torvalds 已提交
616 617 618
	lp->lasttrans_dest = lp->lastload_dest;
	lp->lastload_dest = 0;
	lp->excnak_pending = 0;
619
	lp->intmask |= TXFREEflag | EXCNAKflag;
L
Linus Torvalds 已提交
620 621 622 623 624

	return 1;
}

/* Called by the kernel when transmit times out */
625
void arcnet_timeout(struct net_device *dev)
L
Linus Torvalds 已提交
626 627
{
	unsigned long flags;
628
	struct arcnet_local *lp = netdev_priv(dev);
629
	int status = lp->hw.status(dev);
L
Linus Torvalds 已提交
630 631 632 633 634 635 636
	char *msg;

	spin_lock_irqsave(&lp->lock, flags);
	if (status & TXFREEflag) {	/* transmit _DID_ finish */
		msg = " - missed IRQ?";
	} else {
		msg = "";
637
		dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
638
		lp->timed_out = 1;
639
		lp->hw.command(dev, NOTXcmd | (lp->cur_tx << 3));
L
Linus Torvalds 已提交
640
	}
641
	dev->stats.tx_errors++;
L
Linus Torvalds 已提交
642 643

	/* make sure we didn't miss a TX or a EXC NAK IRQ */
644
	lp->hw.intmask(dev, 0);
645
	lp->intmask |= TXFREEflag | EXCNAKflag;
646
	lp->hw.intmask(dev, lp->intmask);
647

L
Linus Torvalds 已提交
648 649
	spin_unlock_irqrestore(&lp->lock, flags);

650
	if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
651 652
		arc_printk(D_EXTRA, dev, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
			   msg, status, lp->intmask, lp->lasttrans_dest);
L
Linus Torvalds 已提交
653 654 655 656 657 658
		lp->last_timeout = jiffies;
	}

	if (lp->cur_tx == -1)
		netif_wake_queue(dev);
}
659
EXPORT_SYMBOL(arcnet_timeout);
L
Linus Torvalds 已提交
660

661
/* The typical workload of the driver: Handle the network interface
L
Linus Torvalds 已提交
662 663 664
 * interrupts. Establish which device needs attention, and call the correct
 * chipset interrupt handler.
 */
665
irqreturn_t arcnet_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
666 667 668 669 670 671
{
	struct net_device *dev = dev_id;
	struct arcnet_local *lp;
	int recbuf, status, diagstatus, didsomething, boguscount;
	int retval = IRQ_NONE;

672
	arc_printk(D_DURING, dev, "\n");
L
Linus Torvalds 已提交
673

674
	arc_printk(D_DURING, dev, "in arcnet_interrupt\n");
675 676

	lp = netdev_priv(dev);
677
	BUG_ON(!lp);
678

L
Linus Torvalds 已提交
679 680
	spin_lock(&lp->lock);

681 682
	/* RESET flag was enabled - if device is not running, we must
	 * clear it right away (but nothing else).
L
Linus Torvalds 已提交
683 684
	 */
	if (!netif_running(dev)) {
685 686 687
		if (lp->hw.status(dev) & RESETflag)
			lp->hw.command(dev, CFLAGScmd | RESETclear);
		lp->hw.intmask(dev, 0);
L
Linus Torvalds 已提交
688
		spin_unlock(&lp->lock);
689
		return retval;
L
Linus Torvalds 已提交
690 691
	}

692
	arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
693
		   lp->hw.status(dev), lp->intmask);
L
Linus Torvalds 已提交
694 695 696

	boguscount = 5;
	do {
697
		status = lp->hw.status(dev);
698
		diagstatus = (status >> 8) & 0xFF;
L
Linus Torvalds 已提交
699

700 701
		arc_printk(D_DEBUG, dev, "%s: %d: %s: status=%x\n",
			   __FILE__, __LINE__, __func__, status);
L
Linus Torvalds 已提交
702 703
		didsomething = 0;

704
		/* RESET flag was enabled - card is resetting and if RX is
L
Linus Torvalds 已提交
705
		 * disabled, it's NOT because we just got a packet.
706
		 *
707 708
		 * The card is in an undefined state.
		 * Clear it out and start over.
L
Linus Torvalds 已提交
709 710
		 */
		if (status & RESETflag) {
711 712
			arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n",
				   status);
L
Linus Torvalds 已提交
713 714 715 716 717 718
			arcnet_close(dev);
			arcnet_open(dev);

			/* get out of the interrupt handler! */
			break;
		}
719 720
		/* RX is inhibited - we must have received something.
		 * Prepare to receive into the next buffer.
721
		 *
722 723 724 725
		 * We don't actually copy the received packet from the card
		 * until after the transmit handler runs (and possibly
		 * launches the next tx); this should improve latency slightly
		 * if we get both types of interrupts at once.
L
Linus Torvalds 已提交
726 727 728 729
		 */
		recbuf = -1;
		if (status & lp->intmask & NORXflag) {
			recbuf = lp->cur_rx;
730 731
			arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n",
				   recbuf, status);
L
Linus Torvalds 已提交
732 733 734

			lp->cur_rx = get_arcbuf(dev);
			if (lp->cur_rx != -1) {
735 736
				arc_printk(D_DURING, dev, "enabling receive to buffer #%d\n",
					   lp->cur_rx);
737
				lp->hw.command(dev, RXcmd | (lp->cur_rx << 3) | RXbcasts);
L
Linus Torvalds 已提交
738 739 740 741
			}
			didsomething++;
		}

742
		if ((diagstatus & EXCNAKflag)) {
743 744
			arc_printk(D_DURING, dev, "EXCNAK IRQ (diagstat=%Xh)\n",
				   diagstatus);
L
Linus Torvalds 已提交
745

746
			lp->hw.command(dev, NOTXcmd);      /* disable transmit */
747
			lp->excnak_pending = 1;
L
Linus Torvalds 已提交
748

749
			lp->hw.command(dev, EXCNAKclear);
L
Linus Torvalds 已提交
750
			lp->intmask &= ~(EXCNAKflag);
751 752
			didsomething++;
		}
L
Linus Torvalds 已提交
753 754 755

		/* a transmit finished, and we're interested in it. */
		if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
756
			lp->intmask &= ~(TXFREEflag | EXCNAKflag);
L
Linus Torvalds 已提交
757

J
Joe Perches 已提交
758 759
			arc_printk(D_DURING, dev, "TX IRQ (stat=%Xh)\n",
				   status);
L
Linus Torvalds 已提交
760 761

			if (lp->cur_tx != -1 && !lp->timed_out) {
762
				if (!(status & TXACKflag)) {
L
Linus Torvalds 已提交
763
					if (lp->lasttrans_dest != 0) {
764 765 766 767
						arc_printk(D_EXTRA, dev,
							   "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
							   status,
							   lp->lasttrans_dest);
768 769
						dev->stats.tx_errors++;
						dev->stats.tx_carrier_errors++;
L
Linus Torvalds 已提交
770
					} else {
771 772 773 774
						arc_printk(D_DURING, dev,
							   "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
							   status,
							   lp->lasttrans_dest);
L
Linus Torvalds 已提交
775 776 777 778 779
					}
				}

				if (lp->outgoing.proto &&
				    lp->outgoing.proto->ack_tx) {
780
					int ackstatus;
J
Joe Perches 已提交
781

782 783 784 785 786 787 788 789 790
					if (status & TXACKflag)
						ackstatus = 2;
					else if (lp->excnak_pending)
						ackstatus = 1;
					else
						ackstatus = 0;

					lp->outgoing.proto
						->ack_tx(dev, ackstatus);
L
Linus Torvalds 已提交
791 792 793 794 795 796 797 798 799 800 801 802 803
				}
			}
			if (lp->cur_tx != -1)
				release_arcbuf(dev, lp->cur_tx);

			lp->cur_tx = -1;
			lp->timed_out = 0;
			didsomething++;

			/* send another packet if there is one */
			go_tx(dev);

			/* continue a split packet, if any */
J
Joe Perches 已提交
804 805
			if (lp->outgoing.proto &&
			    lp->outgoing.proto->continue_tx) {
L
Linus Torvalds 已提交
806
				int txbuf = get_arcbuf(dev);
J
Joe Perches 已提交
807

L
Linus Torvalds 已提交
808 809 810
				if (txbuf != -1) {
					if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
						/* that was the last segment */
811
						dev->stats.tx_bytes += lp->outgoing.skb->len;
812
						if (!lp->outgoing.proto->ack_tx) {
813 814 815
							dev_kfree_skb_irq(lp->outgoing.skb);
							lp->outgoing.proto = NULL;
						}
L
Linus Torvalds 已提交
816 817 818 819 820 821 822 823 824 825
					}
					lp->next_tx = txbuf;
				}
			}
			/* inform upper layers of idleness, if necessary */
			if (lp->cur_tx == -1)
				netif_wake_queue(dev);
		}
		/* now process the received packet, if any */
		if (recbuf != -1) {
826 827
			if (BUGLVL(D_RX))
				arcnet_dump_packet(dev, recbuf, "rx irq", 0);
L
Linus Torvalds 已提交
828 829 830 831 832 833 834

			arcnet_rx(dev, recbuf);
			release_arcbuf(dev, recbuf);

			didsomething++;
		}
		if (status & lp->intmask & RECONflag) {
835
			lp->hw.command(dev, CFLAGScmd | CONFIGclear);
836
			dev->stats.tx_carrier_errors++;
L
Linus Torvalds 已提交
837

838 839
			arc_printk(D_RECON, dev, "Network reconfiguration detected (status=%Xh)\n",
				   status);
840
			/* MYRECON bit is at bit 7 of diagstatus */
841
			if (diagstatus & 0x80)
842
				arc_printk(D_RECON, dev, "Put out that recon myself\n");
L
Linus Torvalds 已提交
843 844 845

			/* is the RECON info empty or old? */
			if (!lp->first_recon || !lp->last_recon ||
846
			    time_after(jiffies, lp->last_recon + HZ * 10)) {
L
Linus Torvalds 已提交
847
				if (lp->network_down)
848
					arc_printk(D_NORMAL, dev, "reconfiguration detected: cabling restored?\n");
L
Linus Torvalds 已提交
849 850 851
				lp->first_recon = lp->last_recon = jiffies;
				lp->num_recons = lp->network_down = 0;

852
				arc_printk(D_DURING, dev, "recon: clearing counters.\n");
L
Linus Torvalds 已提交
853 854 855 856
			} else {	/* add to current RECON counter */
				lp->last_recon = jiffies;
				lp->num_recons++;

857 858 859 860
				arc_printk(D_DURING, dev, "recon: counter=%d, time=%lds, net=%d\n",
					   lp->num_recons,
					   (lp->last_recon - lp->first_recon) / HZ,
					   lp->network_down);
L
Linus Torvalds 已提交
861 862 863 864 865 866 867

				/* if network is marked up;
				 * and first_recon and last_recon are 60+ apart;
				 * and the average no. of recons counted is
				 *    > RECON_THRESHOLD/min;
				 * then print a warning message.
				 */
868 869 870
				if (!lp->network_down &&
				    (lp->last_recon - lp->first_recon) <= HZ * 60 &&
				    lp->num_recons >= RECON_THRESHOLD) {
L
Linus Torvalds 已提交
871
					lp->network_down = 1;
872
					arc_printk(D_NORMAL, dev, "many reconfigurations detected: cabling problem?\n");
873 874
				} else if (!lp->network_down &&
					   lp->last_recon - lp->first_recon > HZ * 60) {
J
Joe Perches 已提交
875 876 877
					/* reset counters if we've gone for
					 *  over a minute.
					 */
L
Linus Torvalds 已提交
878 879 880 881
					lp->first_recon = lp->last_recon;
					lp->num_recons = 1;
				}
			}
882
		} else if (lp->network_down &&
883
			   time_after(jiffies, lp->last_recon + HZ * 10)) {
L
Linus Torvalds 已提交
884
			if (lp->network_down)
885
				arc_printk(D_NORMAL, dev, "cabling restored?\n");
L
Linus Torvalds 已提交
886 887 888
			lp->first_recon = lp->last_recon = 0;
			lp->num_recons = lp->network_down = 0;

889
			arc_printk(D_DURING, dev, "not recon: clearing counters anyway.\n");
L
Linus Torvalds 已提交
890 891
		}

892
		if (didsomething)
L
Linus Torvalds 已提交
893
			retval |= IRQ_HANDLED;
894
	} while (--boguscount && didsomething);
L
Linus Torvalds 已提交
895

896
	arc_printk(D_DURING, dev, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
897
		   lp->hw.status(dev), boguscount);
898
	arc_printk(D_DURING, dev, "\n");
L
Linus Torvalds 已提交
899

900
	lp->hw.intmask(dev, 0);
L
Linus Torvalds 已提交
901
	udelay(1);
902
	lp->hw.intmask(dev, lp->intmask);
903

L
Linus Torvalds 已提交
904 905 906
	spin_unlock(&lp->lock);
	return retval;
}
907
EXPORT_SYMBOL(arcnet_interrupt);
L
Linus Torvalds 已提交
908

909
/* This is a generic packet receiver that calls arcnet??_rx depending on the
L
Linus Torvalds 已提交
910 911
 * protocol ID found.
 */
912
static void arcnet_rx(struct net_device *dev, int bufnum)
L
Linus Torvalds 已提交
913
{
914
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
915 916 917 918 919 920
	struct archdr pkt;
	struct arc_rfc1201 *soft;
	int length, ofs;

	soft = &pkt.soft.rfc1201;

D
Dan Carpenter 已提交
921
	lp->hw.copy_from_card(dev, bufnum, 0, &pkt, ARC_HDR_SIZE);
L
Linus Torvalds 已提交
922 923 924 925 926 927 928 929 930
	if (pkt.hard.offset[0]) {
		ofs = pkt.hard.offset[0];
		length = 256 - ofs;
	} else {
		ofs = pkt.hard.offset[1];
		length = 512 - ofs;
	}

	/* get the full header, if possible */
931
	if (sizeof(pkt.soft) <= length) {
L
Linus Torvalds 已提交
932
		lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
933
	} else {
L
Linus Torvalds 已提交
934 935 936 937
		memset(&pkt.soft, 0, sizeof(pkt.soft));
		lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
	}

938 939
	arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
		   bufnum, pkt.hard.source, pkt.hard.dest, length);
L
Linus Torvalds 已提交
940

941 942
	dev->stats.rx_packets++;
	dev->stats.rx_bytes += length + ARC_HDR_SIZE;
L
Linus Torvalds 已提交
943 944 945

	/* call the right receiver for the protocol */
	if (arc_proto_map[soft->proto]->is_ip) {
946
		if (BUGLVL(D_PROTO)) {
L
Linus Torvalds 已提交
947 948 949 950 951
			struct ArcProto
			*oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
			*newp = arc_proto_map[soft->proto];

			if (oldp != newp) {
952 953 954 955
				arc_printk(D_PROTO, dev,
					   "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
					   soft->proto, pkt.hard.source,
					   newp->suffix, oldp->suffix);
L
Linus Torvalds 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
			}
		}

		/* broadcasts will always be done with the last-used encap. */
		lp->default_proto[0] = soft->proto;

		/* in striking contrast, the following isn't a hack. */
		lp->default_proto[pkt.hard.source] = soft->proto;
	}
	/* call the protocol-specific receiver. */
	arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
}

static void null_rx(struct net_device *dev, int bufnum,
		    struct archdr *pkthdr, int length)
{
972 973 974
	arc_printk(D_PROTO, dev,
		   "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
		   pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
L
Linus Torvalds 已提交
975 976 977 978 979
}

static int null_build_header(struct sk_buff *skb, struct net_device *dev,
			     unsigned short type, uint8_t daddr)
{
980
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
981

982 983 984
	arc_printk(D_PROTO, dev,
		   "tx: can't build header for encap %02Xh; load a protocol driver.\n",
		   lp->default_proto[daddr]);
L
Linus Torvalds 已提交
985 986 987 988 989 990 991 992 993

	/* always fails */
	return 0;
}

/* the "do nothing" prepare_tx function warns that there's nothing to do. */
static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
			   int length, int bufnum)
{
994
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
995 996
	struct arc_hardware newpkt;

997
	arc_printk(D_PROTO, dev, "tx: no encap for this host; load a protocol driver.\n");
L
Linus Torvalds 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

	/* send a packet to myself -- will never get received, of course */
	newpkt.source = newpkt.dest = dev->dev_addr[0];

	/* only one byte of actual data (and it's random) */
	newpkt.offset[0] = 0xFF;

	lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);

	return 1;		/* done */
}