blob: fabc0607b0f1d15a500b1abd6989507ac2777561 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux ARCnet driver - device-independent routines
3 *
4 * Written 1997 by David Woodhouse.
5 * Written 1994-1999 by Avery Pennarun.
6 * Written 1999-2000 by Martin Mares <mj@ucw.cz>.
7 * Derived from skeleton.c by Donald Becker.
8 *
9 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
10 * for sponsoring the further development of this driver.
11 *
12 * **********************
13 *
14 * The original copyright was as follows:
15 *
16 * skeleton.c Written 1993 by Donald Becker.
17 * Copyright 1993 United States Government as represented by the
18 * Director, National Security Agency. This software may only be used
19 * and distributed according to the terms of the GNU General Public License as
20 * modified by SRC, incorporated herein by reference.
21 *
22 * **********************
23 *
24 * The change log is now in a file called ChangeLog in this directory.
25 *
26 * Sources:
27 * - Crynwr arcnet.com/arcether.com packet drivers.
28 * - arcnet.c v0.00 dated 1/1/94 and apparently by
29 * Donald Becker - it didn't work :)
30 * - skeleton.c v0.05 dated 11/16/93 by Donald Becker
31 * (from Linux Kernel 1.1.45)
32 * - RFC's 1201 and 1051 - re: TCP/IP over ARCnet
33 * - The official ARCnet COM9026 data sheets (!) thanks to
34 * Ken Cornetet <kcornete@nyx10.cs.du.edu>
35 * - The official ARCnet COM20020 data sheets.
36 * - Information on some more obscure ARCnet controller chips, thanks
37 * to the nice people at SMSC.
38 * - net/inet/eth.c (from kernel 1.1.50) for header-building info.
39 * - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
40 * - Textual information and more alternate source from Joachim Koenig
41 * <jojo@repas.de>
42 */
43
44#define VERSION "arcnet: v3.93 BETA 2000/04/29 - by Avery Pennarun et al.\n"
45
46#include <linux/module.h>
47#include <linux/config.h>
48#include <linux/types.h>
49#include <linux/delay.h>
50#include <linux/netdevice.h>
51#include <linux/if_arp.h>
52#include <net/arp.h>
53#include <linux/init.h>
54#include <linux/arcdevice.h>
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -080055#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* "do nothing" functions for protocol drivers */
58static void null_rx(struct net_device *dev, int bufnum,
59 struct archdr *pkthdr, int length);
60static int null_build_header(struct sk_buff *skb, struct net_device *dev,
61 unsigned short type, uint8_t daddr);
62static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
63 int length, int bufnum);
64
Adrian Bunkf03aa2d2006-01-14 03:10:22 +010065static void arcnet_rx(struct net_device *dev, int bufnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * one ArcProto per possible proto ID. None of the elements of
69 * arc_proto_map are allowed to be NULL; they will get set to
70 * arc_proto_default instead. It also must not be NULL; if you would like
71 * to set it to NULL, set it to &arc_proto_null instead.
72 */
73 struct ArcProto *arc_proto_map[256], *arc_proto_default,
74 *arc_bcast_proto, *arc_raw_proto;
75
Adrian Bunkf03aa2d2006-01-14 03:10:22 +010076static struct ArcProto arc_proto_null =
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 .suffix = '?',
79 .mtu = XMTU,
80 .is_ip = 0,
81 .rx = null_rx,
82 .build_header = null_build_header,
83 .prepare_tx = null_prepare_tx,
84 .continue_tx = NULL,
85 .ack_tx = NULL
86};
87
88/* Exported function prototypes */
89int arcnet_debug = ARCNET_DEBUG;
90
91EXPORT_SYMBOL(arc_proto_map);
92EXPORT_SYMBOL(arc_proto_default);
93EXPORT_SYMBOL(arc_bcast_proto);
94EXPORT_SYMBOL(arc_raw_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095EXPORT_SYMBOL(arcnet_unregister_proto);
96EXPORT_SYMBOL(arcnet_debug);
97EXPORT_SYMBOL(alloc_arcdev);
98EXPORT_SYMBOL(arcnet_interrupt);
99
100/* Internal function prototypes */
101static int arcnet_open(struct net_device *dev);
102static int arcnet_close(struct net_device *dev);
103static int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev);
104static void arcnet_timeout(struct net_device *dev);
105static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
106 unsigned short type, void *daddr, void *saddr,
107 unsigned len);
108static int arcnet_rebuild_header(struct sk_buff *skb);
109static struct net_device_stats *arcnet_get_stats(struct net_device *dev);
110static int go_tx(struct net_device *dev);
111
112static int debug = ARCNET_DEBUG;
113module_param(debug, int, 0);
114MODULE_LICENSE("GPL");
115
116static int __init arcnet_init(void)
117{
118 int count;
119
120 arcnet_debug = debug;
121
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100122 printk("arcnet loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124#ifdef ALPHA_WARNING
125 BUGLVL(D_EXTRA) {
126 printk("arcnet: ***\n"
127 "arcnet: * Read arcnet.txt for important release notes!\n"
128 "arcnet: *\n"
129 "arcnet: * This is an ALPHA version! (Last stable release: v3.02) E-mail\n"
130 "arcnet: * me if you have any questions, comments, or bug reports.\n"
131 "arcnet: ***\n");
132 }
133#endif
134
135 /* initialize the protocol map */
136 arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
137 for (count = 0; count < 256; count++)
138 arc_proto_map[count] = arc_proto_default;
139
140 BUGLVL(D_DURING)
141 printk("arcnet: struct sizes: %Zd %Zd %Zd %Zd %Zd\n",
142 sizeof(struct arc_hardware), sizeof(struct arc_rfc1201),
143 sizeof(struct arc_rfc1051), sizeof(struct arc_eth_encap),
144 sizeof(struct archdr));
145
146 return 0;
147}
148
149static void __exit arcnet_exit(void)
150{
151}
152
153module_init(arcnet_init);
154module_exit(arcnet_exit);
155
156/*
157 * Dump the contents of an sk_buff
158 */
159#if ARCNET_DEBUG_MAX & D_SKB
160void arcnet_dump_skb(struct net_device *dev,
161 struct sk_buff *skb, char *desc)
162{
163 int i;
164
165 printk(KERN_DEBUG "%6s: skb dump (%s) follows:", dev->name, desc);
166 for (i = 0; i < skb->len; i++) {
167 if (i % 16 == 0)
168 printk("\n" KERN_DEBUG "[%04X] ", i);
169 printk("%02X ", ((u_char *) skb->data)[i]);
170 }
171 printk("\n");
172}
173
174EXPORT_SYMBOL(arcnet_dump_skb);
175#endif
176
177
178/*
179 * Dump the contents of an ARCnet buffer
180 */
181#if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100182static void arcnet_dump_packet(struct net_device *dev, int bufnum,
183 char *desc, int take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct arcnet_local *lp = dev->priv;
186 int i, length;
187 unsigned long flags = 0;
188 static uint8_t buf[512];
189
190 /* hw.copy_from_card expects IRQ context so take the IRQ lock
191 to keep it single threaded */
192 if(take_arcnet_lock)
193 spin_lock_irqsave(&lp->lock, flags);
194
195 lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
196 if(take_arcnet_lock)
197 spin_unlock_irqrestore(&lp->lock, flags);
198
199 /* if the offset[0] byte is nonzero, this is a 256-byte packet */
200 length = (buf[2] ? 256 : 512);
201
202 printk(KERN_DEBUG "%6s: packet dump (%s) follows:", dev->name, desc);
203 for (i = 0; i < length; i++) {
204 if (i % 16 == 0)
205 printk("\n" KERN_DEBUG "[%04X] ", i);
206 printk("%02X ", buf[i]);
207 }
208 printk("\n");
209
210}
211
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100212#else
213
214#define arcnet_dump_packet(dev, bufnum, desc,take_arcnet_lock) do { } while (0)
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#endif
217
218
219/*
220 * Unregister a protocol driver from the arc_proto_map. Protocol drivers
221 * are responsible for registering themselves, but the unregister routine
222 * is pretty generic so we'll do it here.
223 */
224void arcnet_unregister_proto(struct ArcProto *proto)
225{
226 int count;
227
228 if (arc_proto_default == proto)
229 arc_proto_default = &arc_proto_null;
230 if (arc_bcast_proto == proto)
231 arc_bcast_proto = arc_proto_default;
232 if (arc_raw_proto == proto)
233 arc_raw_proto = arc_proto_default;
234
235 for (count = 0; count < 256; count++) {
236 if (arc_proto_map[count] == proto)
237 arc_proto_map[count] = arc_proto_default;
238 }
239}
240
241
242/*
243 * Add a buffer to the queue. Only the interrupt handler is allowed to do
244 * this, unless interrupts are disabled.
245 *
246 * Note: we don't check for a full queue, since there aren't enough buffers
247 * to more than fill it.
248 */
249static void release_arcbuf(struct net_device *dev, int bufnum)
250{
251 struct arcnet_local *lp = dev->priv;
252 int i;
253
254 lp->buf_queue[lp->first_free_buf++] = bufnum;
255 lp->first_free_buf %= 5;
256
257 BUGLVL(D_DURING) {
258 BUGMSG(D_DURING, "release_arcbuf: freed #%d; buffer queue is now: ",
259 bufnum);
260 for (i = lp->next_buf; i != lp->first_free_buf; i = (i+1) % 5)
261 BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
262 BUGMSG2(D_DURING, "\n");
263 }
264}
265
266
267/*
268 * Get a buffer from the queue. If this returns -1, there are no buffers
269 * available.
270 */
271static int get_arcbuf(struct net_device *dev)
272{
273 struct arcnet_local *lp = dev->priv;
274 int buf = -1, i;
275
276 if (!atomic_dec_and_test(&lp->buf_lock)) {
277 /* already in this function */
278 BUGMSG(D_NORMAL, "get_arcbuf: overlap (%d)!\n",
279 lp->buf_lock.counter);
280 }
281 else { /* we can continue */
282 if (lp->next_buf >= 5)
283 lp->next_buf -= 5;
284
285 if (lp->next_buf == lp->first_free_buf)
286 BUGMSG(D_NORMAL, "get_arcbuf: BUG: no buffers are available??\n");
287 else {
288 buf = lp->buf_queue[lp->next_buf++];
289 lp->next_buf %= 5;
290 }
291 }
292
293
294 BUGLVL(D_DURING) {
295 BUGMSG(D_DURING, "get_arcbuf: got #%d; buffer queue is now: ", buf);
296 for (i = lp->next_buf; i != lp->first_free_buf; i = (i+1) % 5)
297 BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
298 BUGMSG2(D_DURING, "\n");
299 }
300
301 atomic_inc(&lp->buf_lock);
302 return buf;
303}
304
305
306static int choose_mtu(void)
307{
308 int count, mtu = 65535;
309
310 /* choose the smallest MTU of all available encaps */
311 for (count = 0; count < 256; count++) {
312 if (arc_proto_map[count] != &arc_proto_null
313 && arc_proto_map[count]->mtu < mtu) {
314 mtu = arc_proto_map[count]->mtu;
315 }
316 }
317
318 return mtu == 65535 ? XMTU : mtu;
319}
320
321
322/* Setup a struct device for ARCnet. */
323static void arcdev_setup(struct net_device *dev)
324{
325 dev->type = ARPHRD_ARCNET;
326 dev->hard_header_len = sizeof(struct archdr);
327 dev->mtu = choose_mtu();
328
329 dev->addr_len = ARCNET_ALEN;
330 dev->tx_queue_len = 100;
331 dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
332 dev->watchdog_timeo = TX_TIMEOUT;
333
334 /* New-style flags. */
335 dev->flags = IFF_BROADCAST;
336
337 /*
338 * Put in this stuff here, so we don't have to export the symbols to
339 * the chipset drivers.
340 */
341 dev->open = arcnet_open;
342 dev->stop = arcnet_close;
343 dev->hard_start_xmit = arcnet_send_packet;
344 dev->tx_timeout = arcnet_timeout;
345 dev->get_stats = arcnet_get_stats;
346 dev->hard_header = arcnet_header;
347 dev->rebuild_header = arcnet_rebuild_header;
348}
349
350struct net_device *alloc_arcdev(char *name)
351{
352 struct net_device *dev;
353
354 dev = alloc_netdev(sizeof(struct arcnet_local),
355 name && *name ? name : "arc%d", arcdev_setup);
356 if(dev) {
357 struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
358 spin_lock_init(&lp->lock);
359 }
360
361 return dev;
362}
363
364/*
365 * Open/initialize the board. This is called sometime after booting when
366 * the 'ifconfig' program is run.
367 *
368 * This routine should set everything up anew at each open, even registers
369 * that "should" only need to be set once at boot, so that there is
370 * non-reboot way to recover if something goes wrong.
371 */
372static int arcnet_open(struct net_device *dev)
373{
374 struct arcnet_local *lp = dev->priv;
375 int count, newmtu, error;
376
377 BUGMSG(D_INIT,"opened.");
378
379 if (!try_module_get(lp->hw.owner))
380 return -ENODEV;
381
382 BUGLVL(D_PROTO) {
383 int count;
384 BUGMSG(D_PROTO, "protocol map (default is '%c'): ",
385 arc_proto_default->suffix);
386 for (count = 0; count < 256; count++)
387 BUGMSG2(D_PROTO, "%c", arc_proto_map[count]->suffix);
388 BUGMSG2(D_PROTO, "\n");
389 }
390
391
392 BUGMSG(D_INIT, "arcnet_open: resetting card.\n");
393
394 /* try to put the card in a defined state - if it fails the first
395 * time, actually reset it.
396 */
397 error = -ENODEV;
398 if (ARCRESET(0) && ARCRESET(1))
399 goto out_module_put;
400
401 newmtu = choose_mtu();
402 if (newmtu < dev->mtu)
403 dev->mtu = newmtu;
404
405 BUGMSG(D_INIT, "arcnet_open: mtu: %d.\n", dev->mtu);
406
407 /* autodetect the encapsulation for each host. */
408 memset(lp->default_proto, 0, sizeof(lp->default_proto));
409
410 /* the broadcast address is special - use the 'bcast' protocol */
411 for (count = 0; count < 256; count++) {
412 if (arc_proto_map[count] == arc_bcast_proto) {
413 lp->default_proto[0] = count;
414 break;
415 }
416 }
417
418 /* initialize buffers */
419 atomic_set(&lp->buf_lock, 1);
420
421 lp->next_buf = lp->first_free_buf = 0;
422 release_arcbuf(dev, 0);
423 release_arcbuf(dev, 1);
424 release_arcbuf(dev, 2);
425 release_arcbuf(dev, 3);
426 lp->cur_tx = lp->next_tx = -1;
427 lp->cur_rx = -1;
428
429 lp->rfc1201.sequence = 1;
430
431 /* bring up the hardware driver */
432 if (lp->hw.open)
433 lp->hw.open(dev);
434
435 if (dev->dev_addr[0] == 0)
436 BUGMSG(D_NORMAL, "WARNING! Station address 00 is reserved "
437 "for broadcasts!\n");
438 else if (dev->dev_addr[0] == 255)
439 BUGMSG(D_NORMAL, "WARNING! Station address FF may confuse "
440 "DOS networking programs!\n");
441
442 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
443 if (ASTATUS() & RESETflag) {
444 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
445 ACOMMAND(CFLAGScmd | RESETclear);
446 }
447
448
449 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
450 /* make sure we're ready to receive IRQ's. */
451 AINTMASK(0);
452 udelay(1); /* give it time to set the mask before
453 * we reset it again. (may not even be
454 * necessary)
455 */
456 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
457 lp->intmask = NORXflag | RECONflag;
458 AINTMASK(lp->intmask);
459 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
460
461 netif_start_queue(dev);
462
463 return 0;
464
465 out_module_put:
466 module_put(lp->hw.owner);
467 return error;
468}
469
470
471/* The inverse routine to arcnet_open - shuts down the card. */
472static int arcnet_close(struct net_device *dev)
473{
474 struct arcnet_local *lp = dev->priv;
475
476 netif_stop_queue(dev);
477
478 /* flush TX and disable RX */
479 AINTMASK(0);
480 ACOMMAND(NOTXcmd); /* stop transmit */
481 ACOMMAND(NORXcmd); /* disable receive */
482 mdelay(1);
483
484 /* shut down the card */
485 lp->hw.close(dev);
486 module_put(lp->hw.owner);
487 return 0;
488}
489
490
491static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
492 unsigned short type, void *daddr, void *saddr,
493 unsigned len)
494{
495 struct arcnet_local *lp = dev->priv;
496 uint8_t _daddr, proto_num;
497 struct ArcProto *proto;
498
499 BUGMSG(D_DURING,
500 "create header from %d to %d; protocol %d (%Xh); size %u.\n",
501 saddr ? *(uint8_t *) saddr : -1,
502 daddr ? *(uint8_t *) daddr : -1,
503 type, type, len);
504
505 if (skb->len!=0 && len != skb->len)
506 BUGMSG(D_NORMAL, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
507 skb->len, len);
508
509
510 /* Type is host order - ? */
511 if(type == ETH_P_ARCNET) {
512 proto = arc_raw_proto;
513 BUGMSG(D_DEBUG, "arc_raw_proto used. proto='%c'\n",proto->suffix);
514 _daddr = daddr ? *(uint8_t *) daddr : 0;
515 }
516 else if (!daddr) {
517 /*
518 * if the dest addr isn't provided, we can't choose an encapsulation!
519 * Store the packet type (eg. ETH_P_IP) for now, and we'll push on a
520 * real header when we do rebuild_header.
521 */
522 *(uint16_t *) skb_push(skb, 2) = type;
523 if (skb->nh.raw - skb->mac.raw != 2)
524 BUGMSG(D_NORMAL, "arcnet_header: Yikes! diff (%d) is not 2!\n",
525 (int)(skb->nh.raw - skb->mac.raw));
526 return -2; /* return error -- can't transmit yet! */
527 }
528 else {
529 /* otherwise, we can just add the header as usual. */
530 _daddr = *(uint8_t *) daddr;
531 proto_num = lp->default_proto[_daddr];
532 proto = arc_proto_map[proto_num];
533 BUGMSG(D_DURING, "building header for %02Xh using protocol '%c'\n",
534 proto_num, proto->suffix);
535 if (proto == &arc_proto_null && arc_bcast_proto != proto) {
536 BUGMSG(D_DURING, "actually, let's use '%c' instead.\n",
537 arc_bcast_proto->suffix);
538 proto = arc_bcast_proto;
539 }
540 }
541 return proto->build_header(skb, dev, type, _daddr);
542}
543
544
545/*
546 * Rebuild the ARCnet hard header. This is called after an ARP (or in the
547 * future other address resolution) has completed on this sk_buff. We now
548 * let ARP fill in the destination field.
549 */
550static int arcnet_rebuild_header(struct sk_buff *skb)
551{
552 struct net_device *dev = skb->dev;
553 struct arcnet_local *lp = dev->priv;
554 int status = 0; /* default is failure */
555 unsigned short type;
556 uint8_t daddr=0;
557 struct ArcProto *proto;
558
559 if (skb->nh.raw - skb->mac.raw != 2) {
560 BUGMSG(D_NORMAL,
561 "rebuild_header: shouldn't be here! (hdrsize=%d)\n",
562 (int)(skb->nh.raw - skb->mac.raw));
563 return 0;
564 }
565 type = *(uint16_t *) skb_pull(skb, 2);
566 BUGMSG(D_DURING, "rebuild header for protocol %Xh\n", type);
567
568 if (type == ETH_P_IP) {
569#ifdef CONFIG_INET
570 BUGMSG(D_DURING, "rebuild header for ethernet protocol %Xh\n", type);
571 status = arp_find(&daddr, skb) ? 1 : 0;
572 BUGMSG(D_DURING, " rebuilt: dest is %d; protocol %Xh\n",
573 daddr, type);
574#endif
575 } else {
576 BUGMSG(D_NORMAL,
577 "I don't understand ethernet protocol %Xh addresses!\n", type);
578 lp->stats.tx_errors++;
579 lp->stats.tx_aborted_errors++;
580 }
581
582 /* if we couldn't resolve the address... give up. */
583 if (!status)
584 return 0;
585
586 /* add the _real_ header this time! */
587 proto = arc_proto_map[lp->default_proto[daddr]];
588 proto->build_header(skb, dev, type, daddr);
589
590 return 1; /* success */
591}
592
593
594
595/* Called by the kernel in order to transmit a packet. */
596static int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev)
597{
598 struct arcnet_local *lp = dev->priv;
599 struct archdr *pkt;
600 struct arc_rfc1201 *soft;
601 struct ArcProto *proto;
602 int txbuf;
603 unsigned long flags;
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700604 int freeskb, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 BUGMSG(D_DURING,
607 "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
608 ASTATUS(), lp->cur_tx, lp->next_tx, skb->len,skb->protocol);
609
610 pkt = (struct archdr *) skb->data;
611 soft = &pkt->soft.rfc1201;
612 proto = arc_proto_map[soft->proto];
613
614 BUGMSG(D_SKB_SIZE, "skb: transmitting %d bytes to %02X\n",
615 skb->len, pkt->hard.dest);
616 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "tx");
617
618 /* fits in one packet? */
619 if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
620 BUGMSG(D_NORMAL, "fixme: packet too large: compensating badly!\n");
621 dev_kfree_skb(skb);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700622 return NETDEV_TX_OK; /* don't try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 /* We're busy transmitting a packet... */
626 netif_stop_queue(dev);
627
628 spin_lock_irqsave(&lp->lock, flags);
629 AINTMASK(0);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700630 if(lp->next_tx == -1)
631 txbuf = get_arcbuf(dev);
632 else {
633 txbuf = -1;
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (txbuf != -1) {
636 if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
637 !proto->ack_tx) {
638 /* done right away and we don't want to acknowledge
639 the package later - forget about it now */
640 lp->stats.tx_bytes += skb->len;
641 freeskb = 1;
642 } else {
643 /* do it the 'split' way */
644 lp->outgoing.proto = proto;
645 lp->outgoing.skb = skb;
646 lp->outgoing.pkt = pkt;
647
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700648 freeskb = 0;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (proto->continue_tx &&
651 proto->continue_tx(dev, txbuf)) {
652 BUGMSG(D_NORMAL,
653 "bug! continue_tx finished the first time! "
654 "(proto='%c')\n", proto->suffix);
655 }
656 }
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700657 retval = NETDEV_TX_OK;
658 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 lp->next_tx = txbuf;
660 } else {
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700661 retval = NETDEV_TX_BUSY;
662 freeskb = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n",__FILE__,__LINE__,__FUNCTION__,ASTATUS());
666 /* make sure we didn't ignore a TX IRQ while we were in here */
667 AINTMASK(0);
668
669 BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
670 lp->intmask |= TXFREEflag|EXCNAKflag;
671 AINTMASK(lp->intmask);
672 BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n",__FILE__,__LINE__,__FUNCTION__,ASTATUS());
673
674 spin_unlock_irqrestore(&lp->lock, flags);
675 if (freeskb) {
676 dev_kfree_skb(skb);
677 }
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700678 return retval; /* no need to try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681
682/*
683 * Actually start transmitting a packet that was loaded into a buffer
684 * by prepare_tx. This should _only_ be called by the interrupt handler.
685 */
686static int go_tx(struct net_device *dev)
687{
688 struct arcnet_local *lp = dev->priv;
689
690 BUGMSG(D_DURING, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
691 ASTATUS(), lp->intmask, lp->next_tx, lp->cur_tx);
692
693 if (lp->cur_tx != -1 || lp->next_tx == -1)
694 return 0;
695
696 BUGLVL(D_TX) arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
697
698 lp->cur_tx = lp->next_tx;
699 lp->next_tx = -1;
700
701 /* start sending */
702 ACOMMAND(TXcmd | (lp->cur_tx << 3));
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 lp->stats.tx_packets++;
705 lp->lasttrans_dest = lp->lastload_dest;
706 lp->lastload_dest = 0;
707 lp->excnak_pending = 0;
708 lp->intmask |= TXFREEflag|EXCNAKflag;
709
710 return 1;
711}
712
713
714/* Called by the kernel when transmit times out */
715static void arcnet_timeout(struct net_device *dev)
716{
717 unsigned long flags;
718 struct arcnet_local *lp = dev->priv;
719 int status = ASTATUS();
720 char *msg;
721
722 spin_lock_irqsave(&lp->lock, flags);
723 if (status & TXFREEflag) { /* transmit _DID_ finish */
724 msg = " - missed IRQ?";
725 } else {
726 msg = "";
727 lp->stats.tx_aborted_errors++;
728 lp->timed_out = 1;
729 ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
730 }
731 lp->stats.tx_errors++;
732
733 /* make sure we didn't miss a TX or a EXC NAK IRQ */
734 AINTMASK(0);
735 lp->intmask |= TXFREEflag|EXCNAKflag;
736 AINTMASK(lp->intmask);
737
738 spin_unlock_irqrestore(&lp->lock, flags);
739
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -0800740 if (time_after(jiffies, lp->last_timeout + 10*HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 BUGMSG(D_EXTRA, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
742 msg, status, lp->intmask, lp->lasttrans_dest);
743 lp->last_timeout = jiffies;
744 }
745
746 if (lp->cur_tx == -1)
747 netif_wake_queue(dev);
748}
749
750
751/*
752 * The typical workload of the driver: Handle the network interface
753 * interrupts. Establish which device needs attention, and call the correct
754 * chipset interrupt handler.
755 */
756irqreturn_t arcnet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
757{
758 struct net_device *dev = dev_id;
759 struct arcnet_local *lp;
760 int recbuf, status, diagstatus, didsomething, boguscount;
761 int retval = IRQ_NONE;
762
763 BUGMSG(D_DURING, "\n");
764
765 BUGMSG(D_DURING, "in arcnet_interrupt\n");
766
767 lp = dev->priv;
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200768 BUG_ON(!lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 spin_lock(&lp->lock);
771
772 /*
773 * RESET flag was enabled - if device is not running, we must clear it right
774 * away (but nothing else).
775 */
776 if (!netif_running(dev)) {
777 if (ASTATUS() & RESETflag)
778 ACOMMAND(CFLAGScmd | RESETclear);
779 AINTMASK(0);
780 spin_unlock(&lp->lock);
781 return IRQ_HANDLED;
782 }
783
784 BUGMSG(D_DURING, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
785 ASTATUS(), lp->intmask);
786
787 boguscount = 5;
788 do {
789 status = ASTATUS();
790 diagstatus = (status >> 8) & 0xFF;
791
792 BUGMSG(D_DEBUG, "%s: %d: %s: status=%x\n",
793 __FILE__,__LINE__,__FUNCTION__,status);
794 didsomething = 0;
795
796 /*
797 * RESET flag was enabled - card is resetting and if RX is
798 * disabled, it's NOT because we just got a packet.
799 *
800 * The card is in an undefined state. Clear it out and start over.
801 */
802 if (status & RESETflag) {
803 BUGMSG(D_NORMAL, "spurious reset (status=%Xh)\n", status);
804 arcnet_close(dev);
805 arcnet_open(dev);
806
807 /* get out of the interrupt handler! */
808 break;
809 }
810 /*
811 * RX is inhibited - we must have received something. Prepare to
812 * receive into the next buffer.
813 *
814 * We don't actually copy the received packet from the card until
815 * after the transmit handler runs (and possibly launches the next
816 * tx); this should improve latency slightly if we get both types
817 * of interrupts at once.
818 */
819 recbuf = -1;
820 if (status & lp->intmask & NORXflag) {
821 recbuf = lp->cur_rx;
822 BUGMSG(D_DURING, "Buffer #%d: receive irq (status=%Xh)\n",
823 recbuf, status);
824
825 lp->cur_rx = get_arcbuf(dev);
826 if (lp->cur_rx != -1) {
827 BUGMSG(D_DURING, "enabling receive to buffer #%d\n",
828 lp->cur_rx);
829 ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
830 }
831 didsomething++;
832 }
833
834 if((diagstatus & EXCNAKflag)) {
835 BUGMSG(D_DURING, "EXCNAK IRQ (diagstat=%Xh)\n",
836 diagstatus);
837
838 ACOMMAND(NOTXcmd); /* disable transmit */
839 lp->excnak_pending = 1;
840
841 ACOMMAND(EXCNAKclear);
842 lp->intmask &= ~(EXCNAKflag);
843 didsomething++;
844 }
845
846
847 /* a transmit finished, and we're interested in it. */
848 if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
849 lp->intmask &= ~(TXFREEflag|EXCNAKflag);
850
851 BUGMSG(D_DURING, "TX IRQ (stat=%Xh)\n", status);
852
853 if (lp->cur_tx != -1 && !lp->timed_out) {
854 if(!(status & TXACKflag)) {
855 if (lp->lasttrans_dest != 0) {
856 BUGMSG(D_EXTRA,
857 "transmit was not acknowledged! "
858 "(status=%Xh, dest=%02Xh)\n",
859 status, lp->lasttrans_dest);
860 lp->stats.tx_errors++;
861 lp->stats.tx_carrier_errors++;
862 } else {
863 BUGMSG(D_DURING,
864 "broadcast was not acknowledged; that's normal "
865 "(status=%Xh, dest=%02Xh)\n",
866 status, lp->lasttrans_dest);
867 }
868 }
869
870 if (lp->outgoing.proto &&
871 lp->outgoing.proto->ack_tx) {
872 int ackstatus;
873 if(status & TXACKflag)
874 ackstatus=2;
875 else if(lp->excnak_pending)
876 ackstatus=1;
877 else
878 ackstatus=0;
879
880 lp->outgoing.proto
881 ->ack_tx(dev, ackstatus);
882 }
883 }
884 if (lp->cur_tx != -1)
885 release_arcbuf(dev, lp->cur_tx);
886
887 lp->cur_tx = -1;
888 lp->timed_out = 0;
889 didsomething++;
890
891 /* send another packet if there is one */
892 go_tx(dev);
893
894 /* continue a split packet, if any */
895 if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
896 int txbuf = get_arcbuf(dev);
897 if (txbuf != -1) {
898 if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
899 /* that was the last segment */
900 lp->stats.tx_bytes += lp->outgoing.skb->len;
901 if(!lp->outgoing.proto->ack_tx)
902 {
903 dev_kfree_skb_irq(lp->outgoing.skb);
904 lp->outgoing.proto = NULL;
905 }
906 }
907 lp->next_tx = txbuf;
908 }
909 }
910 /* inform upper layers of idleness, if necessary */
911 if (lp->cur_tx == -1)
912 netif_wake_queue(dev);
913 }
914 /* now process the received packet, if any */
915 if (recbuf != -1) {
916 BUGLVL(D_RX) arcnet_dump_packet(dev, recbuf, "rx irq", 0);
917
918 arcnet_rx(dev, recbuf);
919 release_arcbuf(dev, recbuf);
920
921 didsomething++;
922 }
923 if (status & lp->intmask & RECONflag) {
924 ACOMMAND(CFLAGScmd | CONFIGclear);
925 lp->stats.tx_carrier_errors++;
926
927 BUGMSG(D_RECON, "Network reconfiguration detected (status=%Xh)\n",
928 status);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700929 /* MYRECON bit is at bit 7 of diagstatus */
930 if(diagstatus & 0x80)
931 BUGMSG(D_RECON,"Put out that recon myself\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* is the RECON info empty or old? */
934 if (!lp->first_recon || !lp->last_recon ||
935 jiffies - lp->last_recon > HZ * 10) {
936 if (lp->network_down)
937 BUGMSG(D_NORMAL, "reconfiguration detected: cabling restored?\n");
938 lp->first_recon = lp->last_recon = jiffies;
939 lp->num_recons = lp->network_down = 0;
940
941 BUGMSG(D_DURING, "recon: clearing counters.\n");
942 } else { /* add to current RECON counter */
943 lp->last_recon = jiffies;
944 lp->num_recons++;
945
946 BUGMSG(D_DURING, "recon: counter=%d, time=%lds, net=%d\n",
947 lp->num_recons,
948 (lp->last_recon - lp->first_recon) / HZ,
949 lp->network_down);
950
951 /* if network is marked up;
952 * and first_recon and last_recon are 60+ apart;
953 * and the average no. of recons counted is
954 * > RECON_THRESHOLD/min;
955 * then print a warning message.
956 */
957 if (!lp->network_down
958 && (lp->last_recon - lp->first_recon) <= HZ * 60
959 && lp->num_recons >= RECON_THRESHOLD) {
960 lp->network_down = 1;
961 BUGMSG(D_NORMAL, "many reconfigurations detected: cabling problem?\n");
962 } else if (!lp->network_down
963 && lp->last_recon - lp->first_recon > HZ * 60) {
964 /* reset counters if we've gone for over a minute. */
965 lp->first_recon = lp->last_recon;
966 lp->num_recons = 1;
967 }
968 }
969 } else if (lp->network_down && jiffies - lp->last_recon > HZ * 10) {
970 if (lp->network_down)
971 BUGMSG(D_NORMAL, "cabling restored?\n");
972 lp->first_recon = lp->last_recon = 0;
973 lp->num_recons = lp->network_down = 0;
974
975 BUGMSG(D_DURING, "not recon: clearing counters anyway.\n");
976 }
977
978 if(didsomething) {
979 retval |= IRQ_HANDLED;
980 }
981 }
982 while (--boguscount && didsomething);
983
984 BUGMSG(D_DURING, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
985 ASTATUS(), boguscount);
986 BUGMSG(D_DURING, "\n");
987
988
989 AINTMASK(0);
990 udelay(1);
991 AINTMASK(lp->intmask);
992
993 spin_unlock(&lp->lock);
994 return retval;
995}
996
997
998/*
999 * This is a generic packet receiver that calls arcnet??_rx depending on the
1000 * protocol ID found.
1001 */
Adrian Bunkf03aa2d2006-01-14 03:10:22 +01001002static void arcnet_rx(struct net_device *dev, int bufnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 struct arcnet_local *lp = dev->priv;
1005 struct archdr pkt;
1006 struct arc_rfc1201 *soft;
1007 int length, ofs;
1008
1009 soft = &pkt.soft.rfc1201;
1010
1011 lp->hw.copy_from_card(dev, bufnum, 0, &pkt, sizeof(ARC_HDR_SIZE));
1012 if (pkt.hard.offset[0]) {
1013 ofs = pkt.hard.offset[0];
1014 length = 256 - ofs;
1015 } else {
1016 ofs = pkt.hard.offset[1];
1017 length = 512 - ofs;
1018 }
1019
1020 /* get the full header, if possible */
1021 if (sizeof(pkt.soft) <= length)
1022 lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
1023 else {
1024 memset(&pkt.soft, 0, sizeof(pkt.soft));
1025 lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
1026 }
1027
1028 BUGMSG(D_DURING, "Buffer #%d: received packet from %02Xh to %02Xh "
1029 "(%d+4 bytes)\n",
1030 bufnum, pkt.hard.source, pkt.hard.dest, length);
1031
1032 lp->stats.rx_packets++;
1033 lp->stats.rx_bytes += length + ARC_HDR_SIZE;
1034
1035 /* call the right receiver for the protocol */
1036 if (arc_proto_map[soft->proto]->is_ip) {
1037 BUGLVL(D_PROTO) {
1038 struct ArcProto
1039 *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
1040 *newp = arc_proto_map[soft->proto];
1041
1042 if (oldp != newp) {
1043 BUGMSG(D_PROTO,
1044 "got protocol %02Xh; encap for host %02Xh is now '%c'"
1045 " (was '%c')\n", soft->proto, pkt.hard.source,
1046 newp->suffix, oldp->suffix);
1047 }
1048 }
1049
1050 /* broadcasts will always be done with the last-used encap. */
1051 lp->default_proto[0] = soft->proto;
1052
1053 /* in striking contrast, the following isn't a hack. */
1054 lp->default_proto[pkt.hard.source] = soft->proto;
1055 }
1056 /* call the protocol-specific receiver. */
1057 arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
1058}
1059
1060
1061
1062/*
1063 * Get the current statistics. This may be called with the card open or
1064 * closed.
1065 */
1066static struct net_device_stats *arcnet_get_stats(struct net_device *dev)
1067{
1068 struct arcnet_local *lp = dev->priv;
1069 return &lp->stats;
1070}
1071
1072
1073static void null_rx(struct net_device *dev, int bufnum,
1074 struct archdr *pkthdr, int length)
1075{
1076 BUGMSG(D_PROTO,
1077 "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
1078 pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
1079}
1080
1081
1082static int null_build_header(struct sk_buff *skb, struct net_device *dev,
1083 unsigned short type, uint8_t daddr)
1084{
1085 struct arcnet_local *lp = dev->priv;
1086
1087 BUGMSG(D_PROTO,
1088 "tx: can't build header for encap %02Xh; load a protocol driver.\n",
1089 lp->default_proto[daddr]);
1090
1091 /* always fails */
1092 return 0;
1093}
1094
1095
1096/* the "do nothing" prepare_tx function warns that there's nothing to do. */
1097static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
1098 int length, int bufnum)
1099{
1100 struct arcnet_local *lp = dev->priv;
1101 struct arc_hardware newpkt;
1102
1103 BUGMSG(D_PROTO, "tx: no encap for this host; load a protocol driver.\n");
1104
1105 /* send a packet to myself -- will never get received, of course */
1106 newpkt.source = newpkt.dest = dev->dev_addr[0];
1107
1108 /* only one byte of actual data (and it's random) */
1109 newpkt.offset[0] = 0xFF;
1110
1111 lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1112
1113 return 1; /* done */
1114}