blob: 24c0a64150237a378a62b49ce71510180d9b5f40 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux ARCnet driver - device-independent routines
Joe Perchescb334642015-05-05 10:05:47 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 * **********************
Joe Perchescb334642015-05-05 10:05:47 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * The change log is now in a file called ChangeLog in this directory.
25 *
26 * Sources:
27 * - Crynwr arcnet.com/arcether.com packet drivers.
Joe Perchescb334642015-05-05 10:05:47 -070028 * - arcnet.c v0.00 dated 1/1/94 and apparently by
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * 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
Jeff Morrow52edc172007-02-16 01:42:27 -080044#define VERSION "arcnet: v3.94 BETA 2007/02/08 - by Avery Pennarun et al.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/types.h>
48#include <linux/delay.h>
49#include <linux/netdevice.h>
50#include <linux/if_arp.h>
51#include <net/arp.h>
52#include <linux/init.h>
53#include <linux/arcdevice.h>
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -080054#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* "do nothing" functions for protocol drivers */
57static void null_rx(struct net_device *dev, int bufnum,
58 struct archdr *pkthdr, int length);
59static int null_build_header(struct sk_buff *skb, struct net_device *dev,
60 unsigned short type, uint8_t daddr);
61static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
62 int length, int bufnum);
63
Adrian Bunkf03aa2d2006-01-14 03:10:22 +010064static void arcnet_rx(struct net_device *dev, int bufnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * one ArcProto per possible proto ID. None of the elements of
68 * arc_proto_map are allowed to be NULL; they will get set to
69 * arc_proto_default instead. It also must not be NULL; if you would like
70 * to set it to NULL, set it to &arc_proto_null instead.
71 */
Joe Perchescb334642015-05-05 10:05:47 -070072struct ArcProto *arc_proto_map[256], *arc_proto_default,
73 *arc_bcast_proto, *arc_raw_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Joe Perches7f5e7602015-05-05 10:05:49 -070075static struct ArcProto arc_proto_null = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 .suffix = '?',
77 .mtu = XMTU,
78 .is_ip = 0,
79 .rx = null_rx,
80 .build_header = null_build_header,
81 .prepare_tx = null_prepare_tx,
82 .continue_tx = NULL,
83 .ack_tx = NULL
84};
85
86/* Exported function prototypes */
87int arcnet_debug = ARCNET_DEBUG;
88
89EXPORT_SYMBOL(arc_proto_map);
90EXPORT_SYMBOL(arc_proto_default);
91EXPORT_SYMBOL(arc_bcast_proto);
92EXPORT_SYMBOL(arc_raw_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093EXPORT_SYMBOL(arcnet_unregister_proto);
94EXPORT_SYMBOL(arcnet_debug);
95EXPORT_SYMBOL(alloc_arcdev);
96EXPORT_SYMBOL(arcnet_interrupt);
Stephen Hemmingerbca5b892009-01-09 13:01:09 +000097EXPORT_SYMBOL(arcnet_open);
98EXPORT_SYMBOL(arcnet_close);
99EXPORT_SYMBOL(arcnet_send_packet);
100EXPORT_SYMBOL(arcnet_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* Internal function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700104 unsigned short type, const void *daddr,
105 const void *saddr, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int go_tx(struct net_device *dev);
107
108static int debug = ARCNET_DEBUG;
109module_param(debug, int, 0);
110MODULE_LICENSE("GPL");
111
112static int __init arcnet_init(void)
113{
114 int count;
115
116 arcnet_debug = debug;
117
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100118 printk("arcnet loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#ifdef ALPHA_WARNING
121 BUGLVL(D_EXTRA) {
122 printk("arcnet: ***\n"
123 "arcnet: * Read arcnet.txt for important release notes!\n"
124 "arcnet: *\n"
125 "arcnet: * This is an ALPHA version! (Last stable release: v3.02) E-mail\n"
126 "arcnet: * me if you have any questions, comments, or bug reports.\n"
127 "arcnet: ***\n");
128 }
129#endif
130
131 /* initialize the protocol map */
132 arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
133 for (count = 0; count < 256; count++)
134 arc_proto_map[count] = arc_proto_default;
135
136 BUGLVL(D_DURING)
137 printk("arcnet: struct sizes: %Zd %Zd %Zd %Zd %Zd\n",
Joe Perchescb334642015-05-05 10:05:47 -0700138 sizeof(struct arc_hardware), sizeof(struct arc_rfc1201),
139 sizeof(struct arc_rfc1051), sizeof(struct arc_eth_encap),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 sizeof(struct archdr));
141
142 return 0;
143}
144
145static void __exit arcnet_exit(void)
146{
147}
148
149module_init(arcnet_init);
150module_exit(arcnet_exit);
151
152/*
153 * Dump the contents of an sk_buff
154 */
155#if ARCNET_DEBUG_MAX & D_SKB
156void arcnet_dump_skb(struct net_device *dev,
157 struct sk_buff *skb, char *desc)
158{
Joe Perchesad361c92009-07-06 13:05:40 -0700159 char hdr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Joe Perchesad361c92009-07-06 13:05:40 -0700161 /* dump the packet */
162 snprintf(hdr, sizeof(hdr), "%6s:%s skb->data:", dev->name, desc);
163 print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
164 16, 1, skb->data, skb->len, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167EXPORT_SYMBOL(arcnet_dump_skb);
168#endif
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
171 * Dump the contents of an ARCnet buffer
172 */
173#if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100174static void arcnet_dump_packet(struct net_device *dev, int bufnum,
175 char *desc, int take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Wang Chen454d7c92008-11-12 23:37:49 -0800177 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int i, length;
179 unsigned long flags = 0;
180 static uint8_t buf[512];
Joe Perchesad361c92009-07-06 13:05:40 -0700181 char hdr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* hw.copy_from_card expects IRQ context so take the IRQ lock
184 to keep it single threaded */
Joe Perchescb334642015-05-05 10:05:47 -0700185 if (take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 spin_lock_irqsave(&lp->lock, flags);
187
188 lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
Joe Perchescb334642015-05-05 10:05:47 -0700189 if (take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 spin_unlock_irqrestore(&lp->lock, flags);
191
192 /* if the offset[0] byte is nonzero, this is a 256-byte packet */
193 length = (buf[2] ? 256 : 512);
194
Joe Perchesad361c92009-07-06 13:05:40 -0700195 /* dump the packet */
196 snprintf(hdr, sizeof(hdr), "%6s:%s packet dump:", dev->name, desc);
197 print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
198 16, 1, buf, length, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100201#else
202
Joe Perchescb334642015-05-05 10:05:47 -0700203#define arcnet_dump_packet(dev, bufnum, desc, take_arcnet_lock) do { } while (0)
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#endif
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * Unregister a protocol driver from the arc_proto_map. Protocol drivers
209 * are responsible for registering themselves, but the unregister routine
210 * is pretty generic so we'll do it here.
211 */
212void arcnet_unregister_proto(struct ArcProto *proto)
213{
214 int count;
215
216 if (arc_proto_default == proto)
217 arc_proto_default = &arc_proto_null;
218 if (arc_bcast_proto == proto)
219 arc_bcast_proto = arc_proto_default;
220 if (arc_raw_proto == proto)
221 arc_raw_proto = arc_proto_default;
222
223 for (count = 0; count < 256; count++) {
224 if (arc_proto_map[count] == proto)
225 arc_proto_map[count] = arc_proto_default;
226 }
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
230 * Add a buffer to the queue. Only the interrupt handler is allowed to do
231 * this, unless interrupts are disabled.
Joe Perchescb334642015-05-05 10:05:47 -0700232 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * Note: we don't check for a full queue, since there aren't enough buffers
234 * to more than fill it.
235 */
236static void release_arcbuf(struct net_device *dev, int bufnum)
237{
Wang Chen454d7c92008-11-12 23:37:49 -0800238 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int i;
240
241 lp->buf_queue[lp->first_free_buf++] = bufnum;
242 lp->first_free_buf %= 5;
243
244 BUGLVL(D_DURING) {
245 BUGMSG(D_DURING, "release_arcbuf: freed #%d; buffer queue is now: ",
246 bufnum);
Joe Perchescb334642015-05-05 10:05:47 -0700247 for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
249 BUGMSG2(D_DURING, "\n");
250 }
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/*
254 * Get a buffer from the queue. If this returns -1, there are no buffers
255 * available.
256 */
257static int get_arcbuf(struct net_device *dev)
258{
Wang Chen454d7c92008-11-12 23:37:49 -0800259 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int buf = -1, i;
261
262 if (!atomic_dec_and_test(&lp->buf_lock)) {
263 /* already in this function */
264 BUGMSG(D_NORMAL, "get_arcbuf: overlap (%d)!\n",
265 lp->buf_lock.counter);
Joe Perches7f5e7602015-05-05 10:05:49 -0700266 } else { /* we can continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (lp->next_buf >= 5)
268 lp->next_buf -= 5;
269
Joe Perches7f5e7602015-05-05 10:05:49 -0700270 if (lp->next_buf == lp->first_free_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 BUGMSG(D_NORMAL, "get_arcbuf: BUG: no buffers are available??\n");
Joe Perches7f5e7602015-05-05 10:05:49 -0700272 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 buf = lp->buf_queue[lp->next_buf++];
274 lp->next_buf %= 5;
275 }
276 }
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 BUGLVL(D_DURING) {
279 BUGMSG(D_DURING, "get_arcbuf: got #%d; buffer queue is now: ", buf);
Joe Perchescb334642015-05-05 10:05:47 -0700280 for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
282 BUGMSG2(D_DURING, "\n");
283 }
284
285 atomic_inc(&lp->buf_lock);
286 return buf;
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static int choose_mtu(void)
290{
291 int count, mtu = 65535;
292
293 /* choose the smallest MTU of all available encaps */
294 for (count = 0; count < 256; count++) {
Joe Perches8e95a202009-12-03 07:58:21 +0000295 if (arc_proto_map[count] != &arc_proto_null &&
296 arc_proto_map[count]->mtu < mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 mtu = arc_proto_map[count]->mtu;
298 }
299 }
300
301 return mtu == 65535 ? XMTU : mtu;
302}
303
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700304static const struct header_ops arcnet_header_ops = {
305 .create = arcnet_header,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700306};
307
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000308static const struct net_device_ops arcnet_netdev_ops = {
309 .ndo_open = arcnet_open,
310 .ndo_stop = arcnet_close,
311 .ndo_start_xmit = arcnet_send_packet,
312 .ndo_tx_timeout = arcnet_timeout,
313};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/* Setup a struct device for ARCnet. */
316static void arcdev_setup(struct net_device *dev)
317{
318 dev->type = ARPHRD_ARCNET;
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000319 dev->netdev_ops = &arcnet_netdev_ops;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700320 dev->header_ops = &arcnet_header_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 dev->hard_header_len = sizeof(struct archdr);
322 dev->mtu = choose_mtu();
323
324 dev->addr_len = ARCNET_ALEN;
325 dev->tx_queue_len = 100;
326 dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
327 dev->watchdog_timeo = TX_TIMEOUT;
328
329 /* New-style flags. */
330 dev->flags = IFF_BROADCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000333struct net_device *alloc_arcdev(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct net_device *dev;
336
337 dev = alloc_netdev(sizeof(struct arcnet_local),
Tom Gundersenc835a672014-07-14 16:37:24 +0200338 name && *name ? name : "arc%d", NET_NAME_UNKNOWN,
339 arcdev_setup);
Joe Perchescb334642015-05-05 10:05:47 -0700340 if (dev) {
Wang Chen454d7c92008-11-12 23:37:49 -0800341 struct arcnet_local *lp = netdev_priv(dev);
Joe Perches01a1d5a2015-05-05 10:05:48 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 spin_lock_init(&lp->lock);
344 }
345
346 return dev;
347}
348
349/*
350 * Open/initialize the board. This is called sometime after booting when
351 * the 'ifconfig' program is run.
352 *
353 * This routine should set everything up anew at each open, even registers
354 * that "should" only need to be set once at boot, so that there is
355 * non-reboot way to recover if something goes wrong.
356 */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000357int arcnet_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Wang Chen454d7c92008-11-12 23:37:49 -0800359 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int count, newmtu, error;
361
Joe Perchescb334642015-05-05 10:05:47 -0700362 BUGMSG(D_INIT, "opened.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (!try_module_get(lp->hw.owner))
365 return -ENODEV;
366
367 BUGLVL(D_PROTO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 BUGMSG(D_PROTO, "protocol map (default is '%c'): ",
369 arc_proto_default->suffix);
370 for (count = 0; count < 256; count++)
371 BUGMSG2(D_PROTO, "%c", arc_proto_map[count]->suffix);
372 BUGMSG2(D_PROTO, "\n");
373 }
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 BUGMSG(D_INIT, "arcnet_open: resetting card.\n");
376
377 /* try to put the card in a defined state - if it fails the first
378 * time, actually reset it.
379 */
380 error = -ENODEV;
381 if (ARCRESET(0) && ARCRESET(1))
382 goto out_module_put;
383
384 newmtu = choose_mtu();
385 if (newmtu < dev->mtu)
386 dev->mtu = newmtu;
387
388 BUGMSG(D_INIT, "arcnet_open: mtu: %d.\n", dev->mtu);
389
390 /* autodetect the encapsulation for each host. */
391 memset(lp->default_proto, 0, sizeof(lp->default_proto));
392
393 /* the broadcast address is special - use the 'bcast' protocol */
394 for (count = 0; count < 256; count++) {
395 if (arc_proto_map[count] == arc_bcast_proto) {
396 lp->default_proto[0] = count;
397 break;
398 }
399 }
400
401 /* initialize buffers */
402 atomic_set(&lp->buf_lock, 1);
403
404 lp->next_buf = lp->first_free_buf = 0;
405 release_arcbuf(dev, 0);
406 release_arcbuf(dev, 1);
407 release_arcbuf(dev, 2);
408 release_arcbuf(dev, 3);
409 lp->cur_tx = lp->next_tx = -1;
410 lp->cur_rx = -1;
411
412 lp->rfc1201.sequence = 1;
413
414 /* bring up the hardware driver */
415 if (lp->hw.open)
416 lp->hw.open(dev);
417
418 if (dev->dev_addr[0] == 0)
419 BUGMSG(D_NORMAL, "WARNING! Station address 00 is reserved "
420 "for broadcasts!\n");
421 else if (dev->dev_addr[0] == 255)
422 BUGMSG(D_NORMAL, "WARNING! Station address FF may confuse "
423 "DOS networking programs!\n");
424
Joe Perchescb334642015-05-05 10:05:47 -0700425 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (ASTATUS() & RESETflag) {
Joe Perchescb334642015-05-05 10:05:47 -0700427 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 ACOMMAND(CFLAGScmd | RESETclear);
429 }
430
Joe Perchescb334642015-05-05 10:05:47 -0700431 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* make sure we're ready to receive IRQ's. */
433 AINTMASK(0);
434 udelay(1); /* give it time to set the mask before
435 * we reset it again. (may not even be
436 * necessary)
437 */
Joe Perchescb334642015-05-05 10:05:47 -0700438 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 lp->intmask = NORXflag | RECONflag;
440 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700441 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 netif_start_queue(dev);
444
445 return 0;
446
447 out_module_put:
448 module_put(lp->hw.owner);
449 return error;
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/* The inverse routine to arcnet_open - shuts down the card. */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000453int arcnet_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Wang Chen454d7c92008-11-12 23:37:49 -0800455 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 netif_stop_queue(dev);
458
459 /* flush TX and disable RX */
460 AINTMASK(0);
461 ACOMMAND(NOTXcmd); /* stop transmit */
462 ACOMMAND(NORXcmd); /* disable receive */
463 mdelay(1);
464
465 /* shut down the card */
466 lp->hw.close(dev);
467 module_put(lp->hw.owner);
468 return 0;
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700472 unsigned short type, const void *daddr,
473 const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700475 const struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 uint8_t _daddr, proto_num;
477 struct ArcProto *proto;
478
479 BUGMSG(D_DURING,
Joe Perchescb334642015-05-05 10:05:47 -0700480 "create header from %d to %d; protocol %d (%Xh); size %u.\n",
481 saddr ? *(uint8_t *)saddr : -1,
482 daddr ? *(uint8_t *)daddr : -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 type, type, len);
484
Joe Perchescb334642015-05-05 10:05:47 -0700485 if (skb->len != 0 && len != skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 BUGMSG(D_NORMAL, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
487 skb->len, len);
488
Joe Perchescb334642015-05-05 10:05:47 -0700489 /* Type is host order - ? */
490 if (type == ETH_P_ARCNET) {
491 proto = arc_raw_proto;
492 BUGMSG(D_DEBUG, "arc_raw_proto used. proto='%c'\n", proto->suffix);
493 _daddr = daddr ? *(uint8_t *)daddr : 0;
Joe Perches7f5e7602015-05-05 10:05:49 -0700494 } else if (!daddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /*
496 * if the dest addr isn't provided, we can't choose an encapsulation!
497 * Store the packet type (eg. ETH_P_IP) for now, and we'll push on a
498 * real header when we do rebuild_header.
499 */
Joe Perchescb334642015-05-05 10:05:47 -0700500 *(uint16_t *)skb_push(skb, 2) = type;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700501 /*
502 * XXX: Why not use skb->mac_len?
503 */
504 if (skb->network_header - skb->mac_header != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 BUGMSG(D_NORMAL, "arcnet_header: Yikes! diff (%d) is not 2!\n",
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700506 (int)(skb->network_header - skb->mac_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return -2; /* return error -- can't transmit yet! */
Joe Perches7f5e7602015-05-05 10:05:49 -0700508 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* otherwise, we can just add the header as usual. */
Joe Perchescb334642015-05-05 10:05:47 -0700510 _daddr = *(uint8_t *)daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 proto_num = lp->default_proto[_daddr];
512 proto = arc_proto_map[proto_num];
513 BUGMSG(D_DURING, "building header for %02Xh using protocol '%c'\n",
514 proto_num, proto->suffix);
515 if (proto == &arc_proto_null && arc_bcast_proto != proto) {
516 BUGMSG(D_DURING, "actually, let's use '%c' instead.\n",
517 arc_bcast_proto->suffix);
518 proto = arc_bcast_proto;
519 }
520 }
521 return proto->build_header(skb, dev, type, _daddr);
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/* Called by the kernel in order to transmit a packet. */
Stephen Hemminger613573252009-08-31 19:50:58 +0000525netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
Joe Perchescb334642015-05-05 10:05:47 -0700526 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Wang Chen454d7c92008-11-12 23:37:49 -0800528 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct archdr *pkt;
530 struct arc_rfc1201 *soft;
531 struct ArcProto *proto;
532 int txbuf;
533 unsigned long flags;
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700534 int freeskb, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 BUGMSG(D_DURING,
537 "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
Joe Perchescb334642015-05-05 10:05:47 -0700538 ASTATUS(), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Joe Perchescb334642015-05-05 10:05:47 -0700540 pkt = (struct archdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 soft = &pkt->soft.rfc1201;
542 proto = arc_proto_map[soft->proto];
543
544 BUGMSG(D_SKB_SIZE, "skb: transmitting %d bytes to %02X\n",
Joe Perchescb334642015-05-05 10:05:47 -0700545 skb->len, pkt->hard.dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "tx");
547
548 /* fits in one packet? */
549 if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
550 BUGMSG(D_NORMAL, "fixme: packet too large: compensating badly!\n");
551 dev_kfree_skb(skb);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700552 return NETDEV_TX_OK; /* don't try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
555 /* We're busy transmitting a packet... */
556 netif_stop_queue(dev);
557
558 spin_lock_irqsave(&lp->lock, flags);
559 AINTMASK(0);
Joe Perchescb334642015-05-05 10:05:47 -0700560 if (lp->next_tx == -1)
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700561 txbuf = get_arcbuf(dev);
Joe Perches7f5e7602015-05-05 10:05:49 -0700562 else
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700563 txbuf = -1;
Joe Perches7f5e7602015-05-05 10:05:49 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (txbuf != -1) {
566 if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
567 !proto->ack_tx) {
568 /* done right away and we don't want to acknowledge
569 the package later - forget about it now */
Stephen Hemminger5803c512009-01-09 13:01:08 +0000570 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 freeskb = 1;
572 } else {
573 /* do it the 'split' way */
574 lp->outgoing.proto = proto;
575 lp->outgoing.skb = skb;
576 lp->outgoing.pkt = pkt;
577
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700578 freeskb = 0;
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (proto->continue_tx &&
581 proto->continue_tx(dev, txbuf)) {
Joe Perchescb334642015-05-05 10:05:47 -0700582 BUGMSG(D_NORMAL,
583 "bug! continue_tx finished the first time! "
584 "(proto='%c')\n", proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 }
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700587 retval = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 lp->next_tx = txbuf;
589 } else {
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700590 retval = NETDEV_TX_BUSY;
591 freeskb = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Joe Perchescb334642015-05-05 10:05:47 -0700594 BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n", __FILE__, __LINE__, __func__, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* make sure we didn't ignore a TX IRQ while we were in here */
596 AINTMASK(0);
597
Joe Perchescb334642015-05-05 10:05:47 -0700598 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
599 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700601 BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n", __FILE__, __LINE__, __func__, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 spin_unlock_irqrestore(&lp->lock, flags);
Joe Perches7f5e7602015-05-05 10:05:49 -0700604 if (freeskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 dev_kfree_skb(skb);
Joe Perches7f5e7602015-05-05 10:05:49 -0700606
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700607 return retval; /* no need to try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*
611 * Actually start transmitting a packet that was loaded into a buffer
612 * by prepare_tx. This should _only_ be called by the interrupt handler.
613 */
614static int go_tx(struct net_device *dev)
615{
Wang Chen454d7c92008-11-12 23:37:49 -0800616 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 BUGMSG(D_DURING, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
619 ASTATUS(), lp->intmask, lp->next_tx, lp->cur_tx);
620
621 if (lp->cur_tx != -1 || lp->next_tx == -1)
622 return 0;
623
624 BUGLVL(D_TX) arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
625
626 lp->cur_tx = lp->next_tx;
627 lp->next_tx = -1;
628
629 /* start sending */
630 ACOMMAND(TXcmd | (lp->cur_tx << 3));
631
Stephen Hemminger5803c512009-01-09 13:01:08 +0000632 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 lp->lasttrans_dest = lp->lastload_dest;
634 lp->lastload_dest = 0;
635 lp->excnak_pending = 0;
Joe Perchescb334642015-05-05 10:05:47 -0700636 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 return 1;
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/* Called by the kernel when transmit times out */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000642void arcnet_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 unsigned long flags;
Wang Chen454d7c92008-11-12 23:37:49 -0800645 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int status = ASTATUS();
647 char *msg;
648
649 spin_lock_irqsave(&lp->lock, flags);
650 if (status & TXFREEflag) { /* transmit _DID_ finish */
651 msg = " - missed IRQ?";
652 } else {
653 msg = "";
Stephen Hemminger5803c512009-01-09 13:01:08 +0000654 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 lp->timed_out = 1;
656 ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
657 }
Stephen Hemminger5803c512009-01-09 13:01:08 +0000658 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* make sure we didn't miss a TX or a EXC NAK IRQ */
661 AINTMASK(0);
Joe Perchescb334642015-05-05 10:05:47 -0700662 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_unlock_irqrestore(&lp->lock, flags);
666
Joe Perchescb334642015-05-05 10:05:47 -0700667 if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 BUGMSG(D_EXTRA, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
669 msg, status, lp->intmask, lp->lasttrans_dest);
670 lp->last_timeout = jiffies;
671 }
672
673 if (lp->cur_tx == -1)
674 netif_wake_queue(dev);
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/*
678 * The typical workload of the driver: Handle the network interface
679 * interrupts. Establish which device needs attention, and call the correct
680 * chipset interrupt handler.
681 */
David Howells7d12e782006-10-05 14:55:46 +0100682irqreturn_t arcnet_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct net_device *dev = dev_id;
685 struct arcnet_local *lp;
686 int recbuf, status, diagstatus, didsomething, boguscount;
687 int retval = IRQ_NONE;
688
689 BUGMSG(D_DURING, "\n");
690
691 BUGMSG(D_DURING, "in arcnet_interrupt\n");
Wang Chen454d7c92008-11-12 23:37:49 -0800692
693 lp = netdev_priv(dev);
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200694 BUG_ON(!lp);
Joe Perchescb334642015-05-05 10:05:47 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 spin_lock(&lp->lock);
697
698 /*
699 * RESET flag was enabled - if device is not running, we must clear it right
700 * away (but nothing else).
701 */
702 if (!netif_running(dev)) {
703 if (ASTATUS() & RESETflag)
704 ACOMMAND(CFLAGScmd | RESETclear);
705 AINTMASK(0);
706 spin_unlock(&lp->lock);
Michael Grzeschik226ee672014-09-29 11:55:34 +0200707 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 BUGMSG(D_DURING, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
711 ASTATUS(), lp->intmask);
712
713 boguscount = 5;
714 do {
715 status = ASTATUS();
Joe Perchescb334642015-05-05 10:05:47 -0700716 diagstatus = (status >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 BUGMSG(D_DEBUG, "%s: %d: %s: status=%x\n",
Joe Perchescb334642015-05-05 10:05:47 -0700719 __FILE__, __LINE__, __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 didsomething = 0;
721
722 /*
723 * RESET flag was enabled - card is resetting and if RX is
724 * disabled, it's NOT because we just got a packet.
Joe Perchescb334642015-05-05 10:05:47 -0700725 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * The card is in an undefined state. Clear it out and start over.
727 */
728 if (status & RESETflag) {
729 BUGMSG(D_NORMAL, "spurious reset (status=%Xh)\n", status);
730 arcnet_close(dev);
731 arcnet_open(dev);
732
733 /* get out of the interrupt handler! */
734 break;
735 }
Joe Perchescb334642015-05-05 10:05:47 -0700736 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * RX is inhibited - we must have received something. Prepare to
738 * receive into the next buffer.
Joe Perchescb334642015-05-05 10:05:47 -0700739 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * We don't actually copy the received packet from the card until
741 * after the transmit handler runs (and possibly launches the next
742 * tx); this should improve latency slightly if we get both types
Joe Perchescb334642015-05-05 10:05:47 -0700743 * of interrupts at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
745 recbuf = -1;
746 if (status & lp->intmask & NORXflag) {
747 recbuf = lp->cur_rx;
748 BUGMSG(D_DURING, "Buffer #%d: receive irq (status=%Xh)\n",
749 recbuf, status);
750
751 lp->cur_rx = get_arcbuf(dev);
752 if (lp->cur_rx != -1) {
753 BUGMSG(D_DURING, "enabling receive to buffer #%d\n",
754 lp->cur_rx);
755 ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
756 }
757 didsomething++;
758 }
759
Joe Perchescb334642015-05-05 10:05:47 -0700760 if ((diagstatus & EXCNAKflag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 BUGMSG(D_DURING, "EXCNAK IRQ (diagstat=%Xh)\n",
762 diagstatus);
763
Joe Perchescb334642015-05-05 10:05:47 -0700764 ACOMMAND(NOTXcmd); /* disable transmit */
765 lp->excnak_pending = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Joe Perchescb334642015-05-05 10:05:47 -0700767 ACOMMAND(EXCNAKclear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 lp->intmask &= ~(EXCNAKflag);
Joe Perchescb334642015-05-05 10:05:47 -0700769 didsomething++;
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* a transmit finished, and we're interested in it. */
773 if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700774 lp->intmask &= ~(TXFREEflag | EXCNAKflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 BUGMSG(D_DURING, "TX IRQ (stat=%Xh)\n", status);
777
778 if (lp->cur_tx != -1 && !lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700779 if (!(status & TXACKflag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (lp->lasttrans_dest != 0) {
781 BUGMSG(D_EXTRA,
782 "transmit was not acknowledged! "
783 "(status=%Xh, dest=%02Xh)\n",
784 status, lp->lasttrans_dest);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000785 dev->stats.tx_errors++;
786 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 } else {
788 BUGMSG(D_DURING,
789 "broadcast was not acknowledged; that's normal "
790 "(status=%Xh, dest=%02Xh)\n",
791 status, lp->lasttrans_dest);
792 }
793 }
794
795 if (lp->outgoing.proto &&
796 lp->outgoing.proto->ack_tx) {
Joe Perchescb334642015-05-05 10:05:47 -0700797 int ackstatus;
Joe Perches01a1d5a2015-05-05 10:05:48 -0700798
Joe Perchescb334642015-05-05 10:05:47 -0700799 if (status & TXACKflag)
800 ackstatus = 2;
801 else if (lp->excnak_pending)
802 ackstatus = 1;
803 else
804 ackstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Joe Perchescb334642015-05-05 10:05:47 -0700806 lp->outgoing.proto
807 ->ack_tx(dev, ackstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 }
810 if (lp->cur_tx != -1)
811 release_arcbuf(dev, lp->cur_tx);
812
813 lp->cur_tx = -1;
814 lp->timed_out = 0;
815 didsomething++;
816
817 /* send another packet if there is one */
818 go_tx(dev);
819
820 /* continue a split packet, if any */
821 if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
822 int txbuf = get_arcbuf(dev);
Joe Perches01a1d5a2015-05-05 10:05:48 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (txbuf != -1) {
825 if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
826 /* that was the last segment */
Stephen Hemminger5803c512009-01-09 13:01:08 +0000827 dev->stats.tx_bytes += lp->outgoing.skb->len;
Joe Perches7f5e7602015-05-05 10:05:49 -0700828 if (!lp->outgoing.proto->ack_tx) {
Joe Perchescb334642015-05-05 10:05:47 -0700829 dev_kfree_skb_irq(lp->outgoing.skb);
830 lp->outgoing.proto = NULL;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833 lp->next_tx = txbuf;
834 }
835 }
836 /* inform upper layers of idleness, if necessary */
837 if (lp->cur_tx == -1)
838 netif_wake_queue(dev);
839 }
840 /* now process the received packet, if any */
841 if (recbuf != -1) {
842 BUGLVL(D_RX) arcnet_dump_packet(dev, recbuf, "rx irq", 0);
843
844 arcnet_rx(dev, recbuf);
845 release_arcbuf(dev, recbuf);
846
847 didsomething++;
848 }
849 if (status & lp->intmask & RECONflag) {
850 ACOMMAND(CFLAGScmd | CONFIGclear);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000851 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 BUGMSG(D_RECON, "Network reconfiguration detected (status=%Xh)\n",
854 status);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700855 /* MYRECON bit is at bit 7 of diagstatus */
Joe Perchescb334642015-05-05 10:05:47 -0700856 if (diagstatus & 0x80)
857 BUGMSG(D_RECON, "Put out that recon myself\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /* is the RECON info empty or old? */
860 if (!lp->first_recon || !lp->last_recon ||
S.Caglar Onur9307b572008-03-28 14:41:24 -0700861 time_after(jiffies, lp->last_recon + HZ * 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (lp->network_down)
863 BUGMSG(D_NORMAL, "reconfiguration detected: cabling restored?\n");
864 lp->first_recon = lp->last_recon = jiffies;
865 lp->num_recons = lp->network_down = 0;
866
867 BUGMSG(D_DURING, "recon: clearing counters.\n");
868 } else { /* add to current RECON counter */
869 lp->last_recon = jiffies;
870 lp->num_recons++;
871
872 BUGMSG(D_DURING, "recon: counter=%d, time=%lds, net=%d\n",
873 lp->num_recons,
Joe Perchescb334642015-05-05 10:05:47 -0700874 (lp->last_recon - lp->first_recon) / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 lp->network_down);
876
877 /* if network is marked up;
878 * and first_recon and last_recon are 60+ apart;
879 * and the average no. of recons counted is
880 * > RECON_THRESHOLD/min;
881 * then print a warning message.
882 */
Joe Perches8e95a202009-12-03 07:58:21 +0000883 if (!lp->network_down &&
884 (lp->last_recon - lp->first_recon) <= HZ * 60 &&
885 lp->num_recons >= RECON_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 lp->network_down = 1;
887 BUGMSG(D_NORMAL, "many reconfigurations detected: cabling problem?\n");
Joe Perches8e95a202009-12-03 07:58:21 +0000888 } else if (!lp->network_down &&
889 lp->last_recon - lp->first_recon > HZ * 60) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* reset counters if we've gone for over a minute. */
891 lp->first_recon = lp->last_recon;
892 lp->num_recons = 1;
893 }
894 }
S.Caglar Onur9307b572008-03-28 14:41:24 -0700895 } else if (lp->network_down &&
Joe Perchescb334642015-05-05 10:05:47 -0700896 time_after(jiffies, lp->last_recon + HZ * 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (lp->network_down)
898 BUGMSG(D_NORMAL, "cabling restored?\n");
899 lp->first_recon = lp->last_recon = 0;
900 lp->num_recons = lp->network_down = 0;
901
902 BUGMSG(D_DURING, "not recon: clearing counters anyway.\n");
903 }
904
Joe Perches7f5e7602015-05-05 10:05:49 -0700905 if (didsomething)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 retval |= IRQ_HANDLED;
Joe Perches7f5e7602015-05-05 10:05:49 -0700907 } while (--boguscount && didsomething);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 BUGMSG(D_DURING, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
910 ASTATUS(), boguscount);
911 BUGMSG(D_DURING, "\n");
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 AINTMASK(0);
914 udelay(1);
915 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 spin_unlock(&lp->lock);
918 return retval;
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/*
922 * This is a generic packet receiver that calls arcnet??_rx depending on the
923 * protocol ID found.
924 */
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100925static void arcnet_rx(struct net_device *dev, int bufnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Wang Chen454d7c92008-11-12 23:37:49 -0800927 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct archdr pkt;
929 struct arc_rfc1201 *soft;
930 int length, ofs;
931
932 soft = &pkt.soft.rfc1201;
933
Dan Carpenter087d2732013-07-19 08:48:05 +0300934 lp->hw.copy_from_card(dev, bufnum, 0, &pkt, ARC_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (pkt.hard.offset[0]) {
936 ofs = pkt.hard.offset[0];
937 length = 256 - ofs;
938 } else {
939 ofs = pkt.hard.offset[1];
940 length = 512 - ofs;
941 }
942
943 /* get the full header, if possible */
Joe Perches7f5e7602015-05-05 10:05:49 -0700944 if (sizeof(pkt.soft) <= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
Joe Perches7f5e7602015-05-05 10:05:49 -0700946 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 memset(&pkt.soft, 0, sizeof(pkt.soft));
948 lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
949 }
950
951 BUGMSG(D_DURING, "Buffer #%d: received packet from %02Xh to %02Xh "
952 "(%d+4 bytes)\n",
953 bufnum, pkt.hard.source, pkt.hard.dest, length);
954
Stephen Hemminger5803c512009-01-09 13:01:08 +0000955 dev->stats.rx_packets++;
956 dev->stats.rx_bytes += length + ARC_HDR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* call the right receiver for the protocol */
959 if (arc_proto_map[soft->proto]->is_ip) {
960 BUGLVL(D_PROTO) {
961 struct ArcProto
962 *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
963 *newp = arc_proto_map[soft->proto];
964
965 if (oldp != newp) {
966 BUGMSG(D_PROTO,
967 "got protocol %02Xh; encap for host %02Xh is now '%c'"
968 " (was '%c')\n", soft->proto, pkt.hard.source,
969 newp->suffix, oldp->suffix);
970 }
971 }
972
973 /* broadcasts will always be done with the last-used encap. */
974 lp->default_proto[0] = soft->proto;
975
976 /* in striking contrast, the following isn't a hack. */
977 lp->default_proto[pkt.hard.source] = soft->proto;
978 }
979 /* call the protocol-specific receiver. */
980 arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983static void null_rx(struct net_device *dev, int bufnum,
984 struct archdr *pkthdr, int length)
985{
986 BUGMSG(D_PROTO,
Joe Perchescb334642015-05-05 10:05:47 -0700987 "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991static int null_build_header(struct sk_buff *skb, struct net_device *dev,
992 unsigned short type, uint8_t daddr)
993{
Wang Chen454d7c92008-11-12 23:37:49 -0800994 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 BUGMSG(D_PROTO,
997 "tx: can't build header for encap %02Xh; load a protocol driver.\n",
998 lp->default_proto[daddr]);
999
1000 /* always fails */
1001 return 0;
1002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004/* the "do nothing" prepare_tx function warns that there's nothing to do. */
1005static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
1006 int length, int bufnum)
1007{
Wang Chen454d7c92008-11-12 23:37:49 -08001008 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct arc_hardware newpkt;
1010
1011 BUGMSG(D_PROTO, "tx: no encap for this host; load a protocol driver.\n");
1012
1013 /* send a packet to myself -- will never get received, of course */
1014 newpkt.source = newpkt.dest = dev->dev_addr[0];
1015
1016 /* only one byte of actual data (and it's random) */
1017 newpkt.offset[0] = 0xFF;
1018
1019 lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1020
1021 return 1; /* done */
1022}