blob: d70efda777c52253e5bbda9ca01fb42b288d0d57 [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)
Joe Perches3b4e5552015-05-05 10:05:50 -0700419 BUGMSG(D_NORMAL, "WARNING! Station address 00 is reserved for broadcasts!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 else if (dev->dev_addr[0] == 255)
Joe Perches3b4e5552015-05-05 10:05:50 -0700421 BUGMSG(D_NORMAL, "WARNING! Station address FF may confuse DOS networking programs!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Joe Perchescb334642015-05-05 10:05:47 -0700423 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (ASTATUS() & RESETflag) {
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 ACOMMAND(CFLAGScmd | RESETclear);
427 }
428
Joe Perchescb334642015-05-05 10:05:47 -0700429 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* make sure we're ready to receive IRQ's. */
431 AINTMASK(0);
432 udelay(1); /* give it time to set the mask before
433 * we reset it again. (may not even be
434 * necessary)
435 */
Joe Perchescb334642015-05-05 10:05:47 -0700436 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 lp->intmask = NORXflag | RECONflag;
438 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700439 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 netif_start_queue(dev);
442
443 return 0;
444
445 out_module_put:
446 module_put(lp->hw.owner);
447 return error;
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/* The inverse routine to arcnet_open - shuts down the card. */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000451int arcnet_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Wang Chen454d7c92008-11-12 23:37:49 -0800453 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 netif_stop_queue(dev);
456
457 /* flush TX and disable RX */
458 AINTMASK(0);
459 ACOMMAND(NOTXcmd); /* stop transmit */
460 ACOMMAND(NORXcmd); /* disable receive */
461 mdelay(1);
462
463 /* shut down the card */
464 lp->hw.close(dev);
465 module_put(lp->hw.owner);
466 return 0;
467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700470 unsigned short type, const void *daddr,
471 const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700473 const struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 uint8_t _daddr, proto_num;
475 struct ArcProto *proto;
476
477 BUGMSG(D_DURING,
Joe Perchescb334642015-05-05 10:05:47 -0700478 "create header from %d to %d; protocol %d (%Xh); size %u.\n",
479 saddr ? *(uint8_t *)saddr : -1,
480 daddr ? *(uint8_t *)daddr : -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 type, type, len);
482
Joe Perchescb334642015-05-05 10:05:47 -0700483 if (skb->len != 0 && len != skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 BUGMSG(D_NORMAL, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
485 skb->len, len);
486
Joe Perchescb334642015-05-05 10:05:47 -0700487 /* Type is host order - ? */
488 if (type == ETH_P_ARCNET) {
489 proto = arc_raw_proto;
490 BUGMSG(D_DEBUG, "arc_raw_proto used. proto='%c'\n", proto->suffix);
491 _daddr = daddr ? *(uint8_t *)daddr : 0;
Joe Perches7f5e7602015-05-05 10:05:49 -0700492 } else if (!daddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * if the dest addr isn't provided, we can't choose an encapsulation!
495 * Store the packet type (eg. ETH_P_IP) for now, and we'll push on a
496 * real header when we do rebuild_header.
497 */
Joe Perchescb334642015-05-05 10:05:47 -0700498 *(uint16_t *)skb_push(skb, 2) = type;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700499 /*
500 * XXX: Why not use skb->mac_len?
501 */
502 if (skb->network_header - skb->mac_header != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 BUGMSG(D_NORMAL, "arcnet_header: Yikes! diff (%d) is not 2!\n",
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700504 (int)(skb->network_header - skb->mac_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -2; /* return error -- can't transmit yet! */
Joe Perches7f5e7602015-05-05 10:05:49 -0700506 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* otherwise, we can just add the header as usual. */
Joe Perchescb334642015-05-05 10:05:47 -0700508 _daddr = *(uint8_t *)daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 proto_num = lp->default_proto[_daddr];
510 proto = arc_proto_map[proto_num];
511 BUGMSG(D_DURING, "building header for %02Xh using protocol '%c'\n",
512 proto_num, proto->suffix);
513 if (proto == &arc_proto_null && arc_bcast_proto != proto) {
514 BUGMSG(D_DURING, "actually, let's use '%c' instead.\n",
515 arc_bcast_proto->suffix);
516 proto = arc_bcast_proto;
517 }
518 }
519 return proto->build_header(skb, dev, type, _daddr);
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/* Called by the kernel in order to transmit a packet. */
Stephen Hemminger613573252009-08-31 19:50:58 +0000523netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
Joe Perchescb334642015-05-05 10:05:47 -0700524 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Wang Chen454d7c92008-11-12 23:37:49 -0800526 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct archdr *pkt;
528 struct arc_rfc1201 *soft;
529 struct ArcProto *proto;
530 int txbuf;
531 unsigned long flags;
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700532 int freeskb, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 BUGMSG(D_DURING,
535 "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
Joe Perchescb334642015-05-05 10:05:47 -0700536 ASTATUS(), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Joe Perchescb334642015-05-05 10:05:47 -0700538 pkt = (struct archdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 soft = &pkt->soft.rfc1201;
540 proto = arc_proto_map[soft->proto];
541
542 BUGMSG(D_SKB_SIZE, "skb: transmitting %d bytes to %02X\n",
Joe Perchescb334642015-05-05 10:05:47 -0700543 skb->len, pkt->hard.dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "tx");
545
546 /* fits in one packet? */
547 if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
548 BUGMSG(D_NORMAL, "fixme: packet too large: compensating badly!\n");
549 dev_kfree_skb(skb);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700550 return NETDEV_TX_OK; /* don't try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
553 /* We're busy transmitting a packet... */
554 netif_stop_queue(dev);
555
556 spin_lock_irqsave(&lp->lock, flags);
557 AINTMASK(0);
Joe Perchescb334642015-05-05 10:05:47 -0700558 if (lp->next_tx == -1)
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700559 txbuf = get_arcbuf(dev);
Joe Perches7f5e7602015-05-05 10:05:49 -0700560 else
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700561 txbuf = -1;
Joe Perches7f5e7602015-05-05 10:05:49 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (txbuf != -1) {
564 if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
565 !proto->ack_tx) {
566 /* done right away and we don't want to acknowledge
567 the package later - forget about it now */
Stephen Hemminger5803c512009-01-09 13:01:08 +0000568 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 freeskb = 1;
570 } else {
571 /* do it the 'split' way */
572 lp->outgoing.proto = proto;
573 lp->outgoing.skb = skb;
574 lp->outgoing.pkt = pkt;
575
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700576 freeskb = 0;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (proto->continue_tx &&
579 proto->continue_tx(dev, txbuf)) {
Joe Perchescb334642015-05-05 10:05:47 -0700580 BUGMSG(D_NORMAL,
Joe Perches3b4e5552015-05-05 10:05:50 -0700581 "bug! continue_tx finished the first time! (proto='%c')\n",
582 proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 }
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700585 retval = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 lp->next_tx = txbuf;
587 } else {
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700588 retval = NETDEV_TX_BUSY;
589 freeskb = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
Joe Perchescb334642015-05-05 10:05:47 -0700592 BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n", __FILE__, __LINE__, __func__, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* make sure we didn't ignore a TX IRQ while we were in here */
594 AINTMASK(0);
595
Joe Perchescb334642015-05-05 10:05:47 -0700596 BUGMSG(D_DEBUG, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
597 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700599 BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n", __FILE__, __LINE__, __func__, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 spin_unlock_irqrestore(&lp->lock, flags);
Joe Perches7f5e7602015-05-05 10:05:49 -0700602 if (freeskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 dev_kfree_skb(skb);
Joe Perches7f5e7602015-05-05 10:05:49 -0700604
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700605 return retval; /* no need to try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
609 * Actually start transmitting a packet that was loaded into a buffer
610 * by prepare_tx. This should _only_ be called by the interrupt handler.
611 */
612static int go_tx(struct net_device *dev)
613{
Wang Chen454d7c92008-11-12 23:37:49 -0800614 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 BUGMSG(D_DURING, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
617 ASTATUS(), lp->intmask, lp->next_tx, lp->cur_tx);
618
619 if (lp->cur_tx != -1 || lp->next_tx == -1)
620 return 0;
621
622 BUGLVL(D_TX) arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
623
624 lp->cur_tx = lp->next_tx;
625 lp->next_tx = -1;
626
627 /* start sending */
628 ACOMMAND(TXcmd | (lp->cur_tx << 3));
629
Stephen Hemminger5803c512009-01-09 13:01:08 +0000630 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 lp->lasttrans_dest = lp->lastload_dest;
632 lp->lastload_dest = 0;
633 lp->excnak_pending = 0;
Joe Perchescb334642015-05-05 10:05:47 -0700634 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 return 1;
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/* Called by the kernel when transmit times out */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000640void arcnet_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 unsigned long flags;
Wang Chen454d7c92008-11-12 23:37:49 -0800643 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int status = ASTATUS();
645 char *msg;
646
647 spin_lock_irqsave(&lp->lock, flags);
648 if (status & TXFREEflag) { /* transmit _DID_ finish */
649 msg = " - missed IRQ?";
650 } else {
651 msg = "";
Stephen Hemminger5803c512009-01-09 13:01:08 +0000652 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 lp->timed_out = 1;
654 ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
655 }
Stephen Hemminger5803c512009-01-09 13:01:08 +0000656 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* make sure we didn't miss a TX or a EXC NAK IRQ */
659 AINTMASK(0);
Joe Perchescb334642015-05-05 10:05:47 -0700660 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 spin_unlock_irqrestore(&lp->lock, flags);
664
Joe Perchescb334642015-05-05 10:05:47 -0700665 if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 BUGMSG(D_EXTRA, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
667 msg, status, lp->intmask, lp->lasttrans_dest);
668 lp->last_timeout = jiffies;
669 }
670
671 if (lp->cur_tx == -1)
672 netif_wake_queue(dev);
673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/*
676 * The typical workload of the driver: Handle the network interface
677 * interrupts. Establish which device needs attention, and call the correct
678 * chipset interrupt handler.
679 */
David Howells7d12e782006-10-05 14:55:46 +0100680irqreturn_t arcnet_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct net_device *dev = dev_id;
683 struct arcnet_local *lp;
684 int recbuf, status, diagstatus, didsomething, boguscount;
685 int retval = IRQ_NONE;
686
687 BUGMSG(D_DURING, "\n");
688
689 BUGMSG(D_DURING, "in arcnet_interrupt\n");
Wang Chen454d7c92008-11-12 23:37:49 -0800690
691 lp = netdev_priv(dev);
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200692 BUG_ON(!lp);
Joe Perchescb334642015-05-05 10:05:47 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 spin_lock(&lp->lock);
695
696 /*
697 * RESET flag was enabled - if device is not running, we must clear it right
698 * away (but nothing else).
699 */
700 if (!netif_running(dev)) {
701 if (ASTATUS() & RESETflag)
702 ACOMMAND(CFLAGScmd | RESETclear);
703 AINTMASK(0);
704 spin_unlock(&lp->lock);
Michael Grzeschik226ee672014-09-29 11:55:34 +0200705 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
708 BUGMSG(D_DURING, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
709 ASTATUS(), lp->intmask);
710
711 boguscount = 5;
712 do {
713 status = ASTATUS();
Joe Perchescb334642015-05-05 10:05:47 -0700714 diagstatus = (status >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 BUGMSG(D_DEBUG, "%s: %d: %s: status=%x\n",
Joe Perchescb334642015-05-05 10:05:47 -0700717 __FILE__, __LINE__, __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 didsomething = 0;
719
720 /*
721 * RESET flag was enabled - card is resetting and if RX is
722 * disabled, it's NOT because we just got a packet.
Joe Perchescb334642015-05-05 10:05:47 -0700723 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 * The card is in an undefined state. Clear it out and start over.
725 */
726 if (status & RESETflag) {
727 BUGMSG(D_NORMAL, "spurious reset (status=%Xh)\n", status);
728 arcnet_close(dev);
729 arcnet_open(dev);
730
731 /* get out of the interrupt handler! */
732 break;
733 }
Joe Perchescb334642015-05-05 10:05:47 -0700734 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * RX is inhibited - we must have received something. Prepare to
736 * receive into the next buffer.
Joe Perchescb334642015-05-05 10:05:47 -0700737 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * We don't actually copy the received packet from the card until
739 * after the transmit handler runs (and possibly launches the next
740 * tx); this should improve latency slightly if we get both types
Joe Perchescb334642015-05-05 10:05:47 -0700741 * of interrupts at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
743 recbuf = -1;
744 if (status & lp->intmask & NORXflag) {
745 recbuf = lp->cur_rx;
746 BUGMSG(D_DURING, "Buffer #%d: receive irq (status=%Xh)\n",
747 recbuf, status);
748
749 lp->cur_rx = get_arcbuf(dev);
750 if (lp->cur_rx != -1) {
751 BUGMSG(D_DURING, "enabling receive to buffer #%d\n",
752 lp->cur_rx);
753 ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
754 }
755 didsomething++;
756 }
757
Joe Perchescb334642015-05-05 10:05:47 -0700758 if ((diagstatus & EXCNAKflag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 BUGMSG(D_DURING, "EXCNAK IRQ (diagstat=%Xh)\n",
760 diagstatus);
761
Joe Perchescb334642015-05-05 10:05:47 -0700762 ACOMMAND(NOTXcmd); /* disable transmit */
763 lp->excnak_pending = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Joe Perchescb334642015-05-05 10:05:47 -0700765 ACOMMAND(EXCNAKclear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 lp->intmask &= ~(EXCNAKflag);
Joe Perchescb334642015-05-05 10:05:47 -0700767 didsomething++;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* a transmit finished, and we're interested in it. */
771 if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700772 lp->intmask &= ~(TXFREEflag | EXCNAKflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 BUGMSG(D_DURING, "TX IRQ (stat=%Xh)\n", status);
775
776 if (lp->cur_tx != -1 && !lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700777 if (!(status & TXACKflag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (lp->lasttrans_dest != 0) {
779 BUGMSG(D_EXTRA,
Joe Perches3b4e5552015-05-05 10:05:50 -0700780 "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 status, lp->lasttrans_dest);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000782 dev->stats.tx_errors++;
783 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 } else {
785 BUGMSG(D_DURING,
Joe Perches3b4e5552015-05-05 10:05:50 -0700786 "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 status, lp->lasttrans_dest);
788 }
789 }
790
791 if (lp->outgoing.proto &&
792 lp->outgoing.proto->ack_tx) {
Joe Perchescb334642015-05-05 10:05:47 -0700793 int ackstatus;
Joe Perches01a1d5a2015-05-05 10:05:48 -0700794
Joe Perchescb334642015-05-05 10:05:47 -0700795 if (status & TXACKflag)
796 ackstatus = 2;
797 else if (lp->excnak_pending)
798 ackstatus = 1;
799 else
800 ackstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Joe Perchescb334642015-05-05 10:05:47 -0700802 lp->outgoing.proto
803 ->ack_tx(dev, ackstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 }
806 if (lp->cur_tx != -1)
807 release_arcbuf(dev, lp->cur_tx);
808
809 lp->cur_tx = -1;
810 lp->timed_out = 0;
811 didsomething++;
812
813 /* send another packet if there is one */
814 go_tx(dev);
815
816 /* continue a split packet, if any */
817 if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
818 int txbuf = get_arcbuf(dev);
Joe Perches01a1d5a2015-05-05 10:05:48 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (txbuf != -1) {
821 if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
822 /* that was the last segment */
Stephen Hemminger5803c512009-01-09 13:01:08 +0000823 dev->stats.tx_bytes += lp->outgoing.skb->len;
Joe Perches7f5e7602015-05-05 10:05:49 -0700824 if (!lp->outgoing.proto->ack_tx) {
Joe Perchescb334642015-05-05 10:05:47 -0700825 dev_kfree_skb_irq(lp->outgoing.skb);
826 lp->outgoing.proto = NULL;
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 lp->next_tx = txbuf;
830 }
831 }
832 /* inform upper layers of idleness, if necessary */
833 if (lp->cur_tx == -1)
834 netif_wake_queue(dev);
835 }
836 /* now process the received packet, if any */
837 if (recbuf != -1) {
838 BUGLVL(D_RX) arcnet_dump_packet(dev, recbuf, "rx irq", 0);
839
840 arcnet_rx(dev, recbuf);
841 release_arcbuf(dev, recbuf);
842
843 didsomething++;
844 }
845 if (status & lp->intmask & RECONflag) {
846 ACOMMAND(CFLAGScmd | CONFIGclear);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000847 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 BUGMSG(D_RECON, "Network reconfiguration detected (status=%Xh)\n",
850 status);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700851 /* MYRECON bit is at bit 7 of diagstatus */
Joe Perchescb334642015-05-05 10:05:47 -0700852 if (diagstatus & 0x80)
853 BUGMSG(D_RECON, "Put out that recon myself\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* is the RECON info empty or old? */
856 if (!lp->first_recon || !lp->last_recon ||
S.Caglar Onur9307b572008-03-28 14:41:24 -0700857 time_after(jiffies, lp->last_recon + HZ * 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (lp->network_down)
859 BUGMSG(D_NORMAL, "reconfiguration detected: cabling restored?\n");
860 lp->first_recon = lp->last_recon = jiffies;
861 lp->num_recons = lp->network_down = 0;
862
863 BUGMSG(D_DURING, "recon: clearing counters.\n");
864 } else { /* add to current RECON counter */
865 lp->last_recon = jiffies;
866 lp->num_recons++;
867
868 BUGMSG(D_DURING, "recon: counter=%d, time=%lds, net=%d\n",
869 lp->num_recons,
Joe Perchescb334642015-05-05 10:05:47 -0700870 (lp->last_recon - lp->first_recon) / HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 lp->network_down);
872
873 /* if network is marked up;
874 * and first_recon and last_recon are 60+ apart;
875 * and the average no. of recons counted is
876 * > RECON_THRESHOLD/min;
877 * then print a warning message.
878 */
Joe Perches8e95a202009-12-03 07:58:21 +0000879 if (!lp->network_down &&
880 (lp->last_recon - lp->first_recon) <= HZ * 60 &&
881 lp->num_recons >= RECON_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 lp->network_down = 1;
883 BUGMSG(D_NORMAL, "many reconfigurations detected: cabling problem?\n");
Joe Perches8e95a202009-12-03 07:58:21 +0000884 } else if (!lp->network_down &&
885 lp->last_recon - lp->first_recon > HZ * 60) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* reset counters if we've gone for over a minute. */
887 lp->first_recon = lp->last_recon;
888 lp->num_recons = 1;
889 }
890 }
S.Caglar Onur9307b572008-03-28 14:41:24 -0700891 } else if (lp->network_down &&
Joe Perchescb334642015-05-05 10:05:47 -0700892 time_after(jiffies, lp->last_recon + HZ * 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (lp->network_down)
894 BUGMSG(D_NORMAL, "cabling restored?\n");
895 lp->first_recon = lp->last_recon = 0;
896 lp->num_recons = lp->network_down = 0;
897
898 BUGMSG(D_DURING, "not recon: clearing counters anyway.\n");
899 }
900
Joe Perches7f5e7602015-05-05 10:05:49 -0700901 if (didsomething)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 retval |= IRQ_HANDLED;
Joe Perches7f5e7602015-05-05 10:05:49 -0700903 } while (--boguscount && didsomething);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 BUGMSG(D_DURING, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
906 ASTATUS(), boguscount);
907 BUGMSG(D_DURING, "\n");
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 AINTMASK(0);
910 udelay(1);
911 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 spin_unlock(&lp->lock);
914 return retval;
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/*
918 * This is a generic packet receiver that calls arcnet??_rx depending on the
919 * protocol ID found.
920 */
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100921static void arcnet_rx(struct net_device *dev, int bufnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Wang Chen454d7c92008-11-12 23:37:49 -0800923 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct archdr pkt;
925 struct arc_rfc1201 *soft;
926 int length, ofs;
927
928 soft = &pkt.soft.rfc1201;
929
Dan Carpenter087d2732013-07-19 08:48:05 +0300930 lp->hw.copy_from_card(dev, bufnum, 0, &pkt, ARC_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (pkt.hard.offset[0]) {
932 ofs = pkt.hard.offset[0];
933 length = 256 - ofs;
934 } else {
935 ofs = pkt.hard.offset[1];
936 length = 512 - ofs;
937 }
938
939 /* get the full header, if possible */
Joe Perches7f5e7602015-05-05 10:05:49 -0700940 if (sizeof(pkt.soft) <= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
Joe Perches7f5e7602015-05-05 10:05:49 -0700942 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 memset(&pkt.soft, 0, sizeof(pkt.soft));
944 lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
945 }
946
Joe Perches3b4e5552015-05-05 10:05:50 -0700947 BUGMSG(D_DURING, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 bufnum, pkt.hard.source, pkt.hard.dest, length);
949
Stephen Hemminger5803c512009-01-09 13:01:08 +0000950 dev->stats.rx_packets++;
951 dev->stats.rx_bytes += length + ARC_HDR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* call the right receiver for the protocol */
954 if (arc_proto_map[soft->proto]->is_ip) {
955 BUGLVL(D_PROTO) {
956 struct ArcProto
957 *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
958 *newp = arc_proto_map[soft->proto];
959
960 if (oldp != newp) {
961 BUGMSG(D_PROTO,
Joe Perches3b4e5552015-05-05 10:05:50 -0700962 "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
963 soft->proto, pkt.hard.source,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 newp->suffix, oldp->suffix);
965 }
966 }
967
968 /* broadcasts will always be done with the last-used encap. */
969 lp->default_proto[0] = soft->proto;
970
971 /* in striking contrast, the following isn't a hack. */
972 lp->default_proto[pkt.hard.source] = soft->proto;
973 }
974 /* call the protocol-specific receiver. */
975 arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
976}
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978static void null_rx(struct net_device *dev, int bufnum,
979 struct archdr *pkthdr, int length)
980{
981 BUGMSG(D_PROTO,
Joe Perchescb334642015-05-05 10:05:47 -0700982 "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986static int null_build_header(struct sk_buff *skb, struct net_device *dev,
987 unsigned short type, uint8_t daddr)
988{
Wang Chen454d7c92008-11-12 23:37:49 -0800989 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 BUGMSG(D_PROTO,
992 "tx: can't build header for encap %02Xh; load a protocol driver.\n",
993 lp->default_proto[daddr]);
994
995 /* always fails */
996 return 0;
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/* the "do nothing" prepare_tx function warns that there's nothing to do. */
1000static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
1001 int length, int bufnum)
1002{
Wang Chen454d7c92008-11-12 23:37:49 -08001003 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct arc_hardware newpkt;
1005
1006 BUGMSG(D_PROTO, "tx: no encap for this host; load a protocol driver.\n");
1007
1008 /* send a packet to myself -- will never get received, of course */
1009 newpkt.source = newpkt.dest = dev->dev_addr[0];
1010
1011 /* only one byte of actual data (and it's random) */
1012 newpkt.offset[0] = 0xFF;
1013
1014 lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1015
1016 return 1; /* done */
1017}