blob: bcfdd9123e4f0b35abc4b9139b3f84084bfe9596 [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
Joe Perches05a24b22015-05-05 10:05:56 -070044#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -080053#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Joe Perches26c6d282015-05-05 10:06:03 -070055#include "arcdevice.h"
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* "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
Joe Perchesf2f0a162015-05-05 10:05:52 -070067/* one ArcProto per possible proto ID. None of the elements of
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * 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 Perches811eafc2015-05-05 10:05:57 -070072struct ArcProto *arc_proto_map[256];
73EXPORT_SYMBOL(arc_proto_map);
74
75struct ArcProto *arc_proto_default;
76EXPORT_SYMBOL(arc_proto_default);
77
78struct ArcProto *arc_bcast_proto;
79EXPORT_SYMBOL(arc_bcast_proto);
80
81struct ArcProto *arc_raw_proto;
82EXPORT_SYMBOL(arc_raw_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Joe Perches7f5e7602015-05-05 10:05:49 -070084static struct ArcProto arc_proto_null = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .suffix = '?',
86 .mtu = XMTU,
87 .is_ip = 0,
88 .rx = null_rx,
89 .build_header = null_build_header,
90 .prepare_tx = null_prepare_tx,
91 .continue_tx = NULL,
92 .ack_tx = NULL
93};
94
95/* Exported function prototypes */
96int arcnet_debug = ARCNET_DEBUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097EXPORT_SYMBOL(arcnet_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/* Internal function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700101 unsigned short type, const void *daddr,
102 const void *saddr, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int go_tx(struct net_device *dev);
104
105static int debug = ARCNET_DEBUG;
106module_param(debug, int, 0);
107MODULE_LICENSE("GPL");
108
109static int __init arcnet_init(void)
110{
111 int count;
112
113 arcnet_debug = debug;
114
Joe Perches05a24b22015-05-05 10:05:56 -0700115 pr_info("arcnet loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 /* initialize the protocol map */
118 arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
119 for (count = 0; count < 256; count++)
120 arc_proto_map[count] = arc_proto_default;
121
Joe Perches72aeea42015-05-05 10:05:54 -0700122 if (BUGLVL(D_DURING))
Joe Perches05a24b22015-05-05 10:05:56 -0700123 pr_info("struct sizes: %Zd %Zd %Zd %Zd %Zd\n",
124 sizeof(struct arc_hardware),
125 sizeof(struct arc_rfc1201),
126 sizeof(struct arc_rfc1051),
127 sizeof(struct arc_eth_encap),
128 sizeof(struct archdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 return 0;
131}
132
133static void __exit arcnet_exit(void)
134{
135}
136
137module_init(arcnet_init);
138module_exit(arcnet_exit);
139
Joe Perchesf2f0a162015-05-05 10:05:52 -0700140/* Dump the contents of an sk_buff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#if ARCNET_DEBUG_MAX & D_SKB
142void arcnet_dump_skb(struct net_device *dev,
143 struct sk_buff *skb, char *desc)
144{
Joe Perchesad361c92009-07-06 13:05:40 -0700145 char hdr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Joe Perchesad361c92009-07-06 13:05:40 -0700147 /* dump the packet */
148 snprintf(hdr, sizeof(hdr), "%6s:%s skb->data:", dev->name, desc);
149 print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
150 16, 1, skb->data, skb->len, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152EXPORT_SYMBOL(arcnet_dump_skb);
153#endif
154
Joe Perchesf2f0a162015-05-05 10:05:52 -0700155/* Dump the contents of an ARCnet buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100157static void arcnet_dump_packet(struct net_device *dev, int bufnum,
158 char *desc, int take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Wang Chen454d7c92008-11-12 23:37:49 -0800160 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int i, length;
162 unsigned long flags = 0;
163 static uint8_t buf[512];
Joe Perchesad361c92009-07-06 13:05:40 -0700164 char hdr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /* hw.copy_from_card expects IRQ context so take the IRQ lock
Joe Perchesf2f0a162015-05-05 10:05:52 -0700167 * to keep it single threaded
168 */
Joe Perchescb334642015-05-05 10:05:47 -0700169 if (take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 spin_lock_irqsave(&lp->lock, flags);
171
172 lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
Joe Perchescb334642015-05-05 10:05:47 -0700173 if (take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 spin_unlock_irqrestore(&lp->lock, flags);
175
176 /* if the offset[0] byte is nonzero, this is a 256-byte packet */
177 length = (buf[2] ? 256 : 512);
178
Joe Perchesad361c92009-07-06 13:05:40 -0700179 /* dump the packet */
180 snprintf(hdr, sizeof(hdr), "%6s:%s packet dump:", dev->name, desc);
181 print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
182 16, 1, buf, length, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100185#else
186
Joe Perchescb334642015-05-05 10:05:47 -0700187#define arcnet_dump_packet(dev, bufnum, desc, take_arcnet_lock) do { } while (0)
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#endif
190
Joe Perchesf2f0a162015-05-05 10:05:52 -0700191/* Unregister a protocol driver from the arc_proto_map. Protocol drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * are responsible for registering themselves, but the unregister routine
193 * is pretty generic so we'll do it here.
194 */
195void arcnet_unregister_proto(struct ArcProto *proto)
196{
197 int count;
198
199 if (arc_proto_default == proto)
200 arc_proto_default = &arc_proto_null;
201 if (arc_bcast_proto == proto)
202 arc_bcast_proto = arc_proto_default;
203 if (arc_raw_proto == proto)
204 arc_raw_proto = arc_proto_default;
205
206 for (count = 0; count < 256; count++) {
207 if (arc_proto_map[count] == proto)
208 arc_proto_map[count] = arc_proto_default;
209 }
210}
Joe Perches811eafc2015-05-05 10:05:57 -0700211EXPORT_SYMBOL(arcnet_unregister_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Joe Perchesf2f0a162015-05-05 10:05:52 -0700213/* Add a buffer to the queue. Only the interrupt handler is allowed to do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * this, unless interrupts are disabled.
Joe Perchescb334642015-05-05 10:05:47 -0700215 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Note: we don't check for a full queue, since there aren't enough buffers
217 * to more than fill it.
218 */
219static void release_arcbuf(struct net_device *dev, int bufnum)
220{
Wang Chen454d7c92008-11-12 23:37:49 -0800221 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int i;
223
224 lp->buf_queue[lp->first_free_buf++] = bufnum;
225 lp->first_free_buf %= 5;
226
Joe Perches72aeea42015-05-05 10:05:54 -0700227 if (BUGLVL(D_DURING)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700228 arc_printk(D_DURING, dev, "release_arcbuf: freed #%d; buffer queue is now: ",
229 bufnum);
Joe Perchescb334642015-05-05 10:05:47 -0700230 for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
Joe Perchesa34c0932015-05-05 10:05:55 -0700231 arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
232 arc_cont(D_DURING, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234}
235
Joe Perchesf2f0a162015-05-05 10:05:52 -0700236/* Get a buffer from the queue.
237 * If this returns -1, there are no buffers available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239static int get_arcbuf(struct net_device *dev)
240{
Wang Chen454d7c92008-11-12 23:37:49 -0800241 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 int buf = -1, i;
243
244 if (!atomic_dec_and_test(&lp->buf_lock)) {
245 /* already in this function */
Joe Perchesa34c0932015-05-05 10:05:55 -0700246 arc_printk(D_NORMAL, dev, "get_arcbuf: overlap (%d)!\n",
247 lp->buf_lock.counter);
Joe Perches7f5e7602015-05-05 10:05:49 -0700248 } else { /* we can continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (lp->next_buf >= 5)
250 lp->next_buf -= 5;
251
Joe Perches7f5e7602015-05-05 10:05:49 -0700252 if (lp->next_buf == lp->first_free_buf) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700253 arc_printk(D_NORMAL, dev, "get_arcbuf: BUG: no buffers are available??\n");
Joe Perches7f5e7602015-05-05 10:05:49 -0700254 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 buf = lp->buf_queue[lp->next_buf++];
256 lp->next_buf %= 5;
257 }
258 }
259
Joe Perches72aeea42015-05-05 10:05:54 -0700260 if (BUGLVL(D_DURING)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700261 arc_printk(D_DURING, dev, "get_arcbuf: got #%d; buffer queue is now: ",
262 buf);
Joe Perchescb334642015-05-05 10:05:47 -0700263 for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
Joe Perchesa34c0932015-05-05 10:05:55 -0700264 arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
265 arc_cont(D_DURING, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
268 atomic_inc(&lp->buf_lock);
269 return buf;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static int choose_mtu(void)
273{
274 int count, mtu = 65535;
275
276 /* choose the smallest MTU of all available encaps */
277 for (count = 0; count < 256; count++) {
Joe Perches8e95a202009-12-03 07:58:21 +0000278 if (arc_proto_map[count] != &arc_proto_null &&
279 arc_proto_map[count]->mtu < mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 mtu = arc_proto_map[count]->mtu;
281 }
282 }
283
284 return mtu == 65535 ? XMTU : mtu;
285}
286
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700287static const struct header_ops arcnet_header_ops = {
288 .create = arcnet_header,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700289};
290
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000291static const struct net_device_ops arcnet_netdev_ops = {
292 .ndo_open = arcnet_open,
293 .ndo_stop = arcnet_close,
294 .ndo_start_xmit = arcnet_send_packet,
295 .ndo_tx_timeout = arcnet_timeout,
296};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/* Setup a struct device for ARCnet. */
299static void arcdev_setup(struct net_device *dev)
300{
301 dev->type = ARPHRD_ARCNET;
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000302 dev->netdev_ops = &arcnet_netdev_ops;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700303 dev->header_ops = &arcnet_header_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dev->hard_header_len = sizeof(struct archdr);
305 dev->mtu = choose_mtu();
306
307 dev->addr_len = ARCNET_ALEN;
308 dev->tx_queue_len = 100;
309 dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
310 dev->watchdog_timeo = TX_TIMEOUT;
311
312 /* New-style flags. */
313 dev->flags = IFF_BROADCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000316struct net_device *alloc_arcdev(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 struct net_device *dev;
319
320 dev = alloc_netdev(sizeof(struct arcnet_local),
Tom Gundersenc835a672014-07-14 16:37:24 +0200321 name && *name ? name : "arc%d", NET_NAME_UNKNOWN,
322 arcdev_setup);
Joe Perchescb334642015-05-05 10:05:47 -0700323 if (dev) {
Wang Chen454d7c92008-11-12 23:37:49 -0800324 struct arcnet_local *lp = netdev_priv(dev);
Joe Perches01a1d5a2015-05-05 10:05:48 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 spin_lock_init(&lp->lock);
327 }
328
329 return dev;
330}
Joe Perches811eafc2015-05-05 10:05:57 -0700331EXPORT_SYMBOL(alloc_arcdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Joe Perchesf2f0a162015-05-05 10:05:52 -0700333/* Open/initialize the board. This is called sometime after booting when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * the 'ifconfig' program is run.
335 *
336 * This routine should set everything up anew at each open, even registers
337 * that "should" only need to be set once at boot, so that there is
338 * non-reboot way to recover if something goes wrong.
339 */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000340int arcnet_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Wang Chen454d7c92008-11-12 23:37:49 -0800342 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int count, newmtu, error;
344
Joe Perchesa34c0932015-05-05 10:05:55 -0700345 arc_printk(D_INIT, dev, "opened.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (!try_module_get(lp->hw.owner))
348 return -ENODEV;
349
Joe Perches72aeea42015-05-05 10:05:54 -0700350 if (BUGLVL(D_PROTO)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700351 arc_printk(D_PROTO, dev, "protocol map (default is '%c'): ",
352 arc_proto_default->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 for (count = 0; count < 256; count++)
Joe Perchesa34c0932015-05-05 10:05:55 -0700354 arc_cont(D_PROTO, "%c", arc_proto_map[count]->suffix);
355 arc_cont(D_PROTO, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
Joe Perchesa34c0932015-05-05 10:05:55 -0700358 arc_printk(D_INIT, dev, "arcnet_open: resetting card.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* try to put the card in a defined state - if it fails the first
361 * time, actually reset it.
362 */
363 error = -ENODEV;
Joe Perchese15b0362015-05-05 10:06:12 -0700364 if (lp->hw.reset(dev, 0) && lp->hw.reset(dev, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto out_module_put;
366
367 newmtu = choose_mtu();
368 if (newmtu < dev->mtu)
369 dev->mtu = newmtu;
370
Joe Perchesa34c0932015-05-05 10:05:55 -0700371 arc_printk(D_INIT, dev, "arcnet_open: mtu: %d.\n", dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /* autodetect the encapsulation for each host. */
374 memset(lp->default_proto, 0, sizeof(lp->default_proto));
375
376 /* the broadcast address is special - use the 'bcast' protocol */
377 for (count = 0; count < 256; count++) {
378 if (arc_proto_map[count] == arc_bcast_proto) {
379 lp->default_proto[0] = count;
380 break;
381 }
382 }
383
384 /* initialize buffers */
385 atomic_set(&lp->buf_lock, 1);
386
387 lp->next_buf = lp->first_free_buf = 0;
388 release_arcbuf(dev, 0);
389 release_arcbuf(dev, 1);
390 release_arcbuf(dev, 2);
391 release_arcbuf(dev, 3);
392 lp->cur_tx = lp->next_tx = -1;
393 lp->cur_rx = -1;
394
395 lp->rfc1201.sequence = 1;
396
397 /* bring up the hardware driver */
398 if (lp->hw.open)
399 lp->hw.open(dev);
400
401 if (dev->dev_addr[0] == 0)
Joe Perchesa34c0932015-05-05 10:05:55 -0700402 arc_printk(D_NORMAL, dev, "WARNING! Station address 00 is reserved for broadcasts!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 else if (dev->dev_addr[0] == 255)
Joe Perchesa34c0932015-05-05 10:05:55 -0700404 arc_printk(D_NORMAL, dev, "WARNING! Station address FF may confuse DOS networking programs!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Joe Perchesa34c0932015-05-05 10:05:55 -0700406 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Joe Perchese15b0362015-05-05 10:06:12 -0700407 if (lp->hw.status(dev) & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700408 arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
409 __FILE__, __LINE__, __func__);
Joe Perchese15b0362015-05-05 10:06:12 -0700410 lp->hw.command(dev, CFLAGScmd | RESETclear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
Joe Perchesa34c0932015-05-05 10:05:55 -0700413 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* make sure we're ready to receive IRQ's. */
Joe Perchese15b0362015-05-05 10:06:12 -0700415 lp->hw.intmask(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 udelay(1); /* give it time to set the mask before
417 * we reset it again. (may not even be
418 * necessary)
419 */
Joe Perchesa34c0932015-05-05 10:05:55 -0700420 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 lp->intmask = NORXflag | RECONflag;
Joe Perchese15b0362015-05-05 10:06:12 -0700422 lp->hw.intmask(dev, lp->intmask);
Joe Perchesa34c0932015-05-05 10:05:55 -0700423 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 netif_start_queue(dev);
426
427 return 0;
428
429 out_module_put:
430 module_put(lp->hw.owner);
431 return error;
432}
Joe Perches811eafc2015-05-05 10:05:57 -0700433EXPORT_SYMBOL(arcnet_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/* The inverse routine to arcnet_open - shuts down the card. */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000436int arcnet_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Wang Chen454d7c92008-11-12 23:37:49 -0800438 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 netif_stop_queue(dev);
441
442 /* flush TX and disable RX */
Joe Perchese15b0362015-05-05 10:06:12 -0700443 lp->hw.intmask(dev, 0);
444 lp->hw.command(dev, NOTXcmd); /* stop transmit */
445 lp->hw.command(dev, NORXcmd); /* disable receive */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 mdelay(1);
447
448 /* shut down the card */
449 lp->hw.close(dev);
450 module_put(lp->hw.owner);
451 return 0;
452}
Joe Perches811eafc2015-05-05 10:05:57 -0700453EXPORT_SYMBOL(arcnet_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700456 unsigned short type, const void *daddr,
457 const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700459 const struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 uint8_t _daddr, proto_num;
461 struct ArcProto *proto;
462
Joe Perchesa34c0932015-05-05 10:05:55 -0700463 arc_printk(D_DURING, dev,
464 "create header from %d to %d; protocol %d (%Xh); size %u.\n",
465 saddr ? *(uint8_t *)saddr : -1,
466 daddr ? *(uint8_t *)daddr : -1,
467 type, type, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Joe Perchescb334642015-05-05 10:05:47 -0700469 if (skb->len != 0 && len != skb->len)
Joe Perchesa34c0932015-05-05 10:05:55 -0700470 arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
471 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Joe Perchescb334642015-05-05 10:05:47 -0700473 /* Type is host order - ? */
474 if (type == ETH_P_ARCNET) {
475 proto = arc_raw_proto;
Joe Perchesa34c0932015-05-05 10:05:55 -0700476 arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n",
477 proto->suffix);
Joe Perchescb334642015-05-05 10:05:47 -0700478 _daddr = daddr ? *(uint8_t *)daddr : 0;
Joe Perches7f5e7602015-05-05 10:05:49 -0700479 } else if (!daddr) {
Joe Perchesf2f0a162015-05-05 10:05:52 -0700480 /* if the dest addr isn't provided, we can't choose an
481 * encapsulation! Store the packet type (eg. ETH_P_IP)
482 * for now, and we'll push on a real header when we do
483 * rebuild_header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 */
Joe Perchescb334642015-05-05 10:05:47 -0700485 *(uint16_t *)skb_push(skb, 2) = type;
Joe Perchesf2f0a162015-05-05 10:05:52 -0700486 /* XXX: Why not use skb->mac_len? */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700487 if (skb->network_header - skb->mac_header != 2)
Joe Perchesa34c0932015-05-05 10:05:55 -0700488 arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! diff (%u) is not 2!\n",
489 skb->network_header - skb->mac_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -2; /* return error -- can't transmit yet! */
Joe Perches7f5e7602015-05-05 10:05:49 -0700491 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* otherwise, we can just add the header as usual. */
Joe Perchescb334642015-05-05 10:05:47 -0700493 _daddr = *(uint8_t *)daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 proto_num = lp->default_proto[_daddr];
495 proto = arc_proto_map[proto_num];
Joe Perchesa34c0932015-05-05 10:05:55 -0700496 arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n",
497 proto_num, proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (proto == &arc_proto_null && arc_bcast_proto != proto) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700499 arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n",
500 arc_bcast_proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 proto = arc_bcast_proto;
502 }
503 }
504 return proto->build_header(skb, dev, type, _daddr);
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/* Called by the kernel in order to transmit a packet. */
Stephen Hemminger613573252009-08-31 19:50:58 +0000508netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
Joe Perchescb334642015-05-05 10:05:47 -0700509 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Wang Chen454d7c92008-11-12 23:37:49 -0800511 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct archdr *pkt;
513 struct arc_rfc1201 *soft;
514 struct ArcProto *proto;
515 int txbuf;
516 unsigned long flags;
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700517 int freeskb, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Joe Perchesa34c0932015-05-05 10:05:55 -0700519 arc_printk(D_DURING, dev,
520 "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700521 lp->hw.status(dev), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Joe Perchescb334642015-05-05 10:05:47 -0700523 pkt = (struct archdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 soft = &pkt->soft.rfc1201;
525 proto = arc_proto_map[soft->proto];
526
Joe Perchesa34c0932015-05-05 10:05:55 -0700527 arc_printk(D_SKB_SIZE, dev, "skb: transmitting %d bytes to %02X\n",
528 skb->len, pkt->hard.dest);
Joe Perches72aeea42015-05-05 10:05:54 -0700529 if (BUGLVL(D_SKB))
530 arcnet_dump_skb(dev, skb, "tx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* fits in one packet? */
533 if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700534 arc_printk(D_NORMAL, dev, "fixme: packet too large: compensating badly!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 dev_kfree_skb(skb);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700536 return NETDEV_TX_OK; /* don't try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
539 /* We're busy transmitting a packet... */
540 netif_stop_queue(dev);
541
542 spin_lock_irqsave(&lp->lock, flags);
Joe Perchese15b0362015-05-05 10:06:12 -0700543 lp->hw.intmask(dev, 0);
Joe Perchescb334642015-05-05 10:05:47 -0700544 if (lp->next_tx == -1)
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700545 txbuf = get_arcbuf(dev);
Joe Perches7f5e7602015-05-05 10:05:49 -0700546 else
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700547 txbuf = -1;
Joe Perches7f5e7602015-05-05 10:05:49 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (txbuf != -1) {
550 if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
551 !proto->ack_tx) {
552 /* done right away and we don't want to acknowledge
Joe Perchesf2f0a162015-05-05 10:05:52 -0700553 * the package later - forget about it now
554 */
Stephen Hemminger5803c512009-01-09 13:01:08 +0000555 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 freeskb = 1;
557 } else {
558 /* do it the 'split' way */
559 lp->outgoing.proto = proto;
560 lp->outgoing.skb = skb;
561 lp->outgoing.pkt = pkt;
562
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700563 freeskb = 0;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (proto->continue_tx &&
566 proto->continue_tx(dev, txbuf)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700567 arc_printk(D_NORMAL, dev,
568 "bug! continue_tx finished the first time! (proto='%c')\n",
569 proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 }
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700572 retval = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 lp->next_tx = txbuf;
574 } else {
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700575 retval = NETDEV_TX_BUSY;
576 freeskb = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
Joe Perchesa34c0932015-05-05 10:05:55 -0700579 arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700580 __FILE__, __LINE__, __func__, lp->hw.status(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* make sure we didn't ignore a TX IRQ while we were in here */
Joe Perchese15b0362015-05-05 10:06:12 -0700582 lp->hw.intmask(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Joe Perchesa34c0932015-05-05 10:05:55 -0700584 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Joe Perchescb334642015-05-05 10:05:47 -0700585 lp->intmask |= TXFREEflag | EXCNAKflag;
Joe Perchese15b0362015-05-05 10:06:12 -0700586 lp->hw.intmask(dev, lp->intmask);
Joe Perchesa34c0932015-05-05 10:05:55 -0700587 arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700588 __FILE__, __LINE__, __func__, lp->hw.status(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 spin_unlock_irqrestore(&lp->lock, flags);
Joe Perches7f5e7602015-05-05 10:05:49 -0700591 if (freeskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 dev_kfree_skb(skb);
Joe Perches7f5e7602015-05-05 10:05:49 -0700593
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700594 return retval; /* no need to try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
Joe Perches811eafc2015-05-05 10:05:57 -0700596EXPORT_SYMBOL(arcnet_send_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Joe Perchesf2f0a162015-05-05 10:05:52 -0700598/* Actually start transmitting a packet that was loaded into a buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * by prepare_tx. This should _only_ be called by the interrupt handler.
600 */
601static int go_tx(struct net_device *dev)
602{
Wang Chen454d7c92008-11-12 23:37:49 -0800603 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Joe Perchesa34c0932015-05-05 10:05:55 -0700605 arc_printk(D_DURING, dev, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700606 lp->hw.status(dev), lp->intmask, lp->next_tx, lp->cur_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (lp->cur_tx != -1 || lp->next_tx == -1)
609 return 0;
610
Joe Perches72aeea42015-05-05 10:05:54 -0700611 if (BUGLVL(D_TX))
612 arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 lp->cur_tx = lp->next_tx;
615 lp->next_tx = -1;
616
617 /* start sending */
Joe Perchese15b0362015-05-05 10:06:12 -0700618 lp->hw.command(dev, TXcmd | (lp->cur_tx << 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Stephen Hemminger5803c512009-01-09 13:01:08 +0000620 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 lp->lasttrans_dest = lp->lastload_dest;
622 lp->lastload_dest = 0;
623 lp->excnak_pending = 0;
Joe Perchescb334642015-05-05 10:05:47 -0700624 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 return 1;
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/* Called by the kernel when transmit times out */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000630void arcnet_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 unsigned long flags;
Wang Chen454d7c92008-11-12 23:37:49 -0800633 struct arcnet_local *lp = netdev_priv(dev);
Joe Perchese15b0362015-05-05 10:06:12 -0700634 int status = lp->hw.status(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 char *msg;
636
637 spin_lock_irqsave(&lp->lock, flags);
638 if (status & TXFREEflag) { /* transmit _DID_ finish */
639 msg = " - missed IRQ?";
640 } else {
641 msg = "";
Stephen Hemminger5803c512009-01-09 13:01:08 +0000642 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 lp->timed_out = 1;
Joe Perchese15b0362015-05-05 10:06:12 -0700644 lp->hw.command(dev, NOTXcmd | (lp->cur_tx << 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Stephen Hemminger5803c512009-01-09 13:01:08 +0000646 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* make sure we didn't miss a TX or a EXC NAK IRQ */
Joe Perchese15b0362015-05-05 10:06:12 -0700649 lp->hw.intmask(dev, 0);
Joe Perchescb334642015-05-05 10:05:47 -0700650 lp->intmask |= TXFREEflag | EXCNAKflag;
Joe Perchese15b0362015-05-05 10:06:12 -0700651 lp->hw.intmask(dev, lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 spin_unlock_irqrestore(&lp->lock, flags);
654
Joe Perchescb334642015-05-05 10:05:47 -0700655 if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700656 arc_printk(D_EXTRA, dev, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
657 msg, status, lp->intmask, lp->lasttrans_dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 lp->last_timeout = jiffies;
659 }
660
661 if (lp->cur_tx == -1)
662 netif_wake_queue(dev);
663}
Joe Perches811eafc2015-05-05 10:05:57 -0700664EXPORT_SYMBOL(arcnet_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Joe Perchesf2f0a162015-05-05 10:05:52 -0700666/* The typical workload of the driver: Handle the network interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * interrupts. Establish which device needs attention, and call the correct
668 * chipset interrupt handler.
669 */
David Howells7d12e782006-10-05 14:55:46 +0100670irqreturn_t arcnet_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct net_device *dev = dev_id;
673 struct arcnet_local *lp;
674 int recbuf, status, diagstatus, didsomething, boguscount;
675 int retval = IRQ_NONE;
676
Joe Perchesa34c0932015-05-05 10:05:55 -0700677 arc_printk(D_DURING, dev, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Joe Perchesa34c0932015-05-05 10:05:55 -0700679 arc_printk(D_DURING, dev, "in arcnet_interrupt\n");
Wang Chen454d7c92008-11-12 23:37:49 -0800680
681 lp = netdev_priv(dev);
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200682 BUG_ON(!lp);
Joe Perchescb334642015-05-05 10:05:47 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 spin_lock(&lp->lock);
685
Joe Perchesf2f0a162015-05-05 10:05:52 -0700686 /* RESET flag was enabled - if device is not running, we must
687 * clear it right away (but nothing else).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
689 if (!netif_running(dev)) {
Joe Perchese15b0362015-05-05 10:06:12 -0700690 if (lp->hw.status(dev) & RESETflag)
691 lp->hw.command(dev, CFLAGScmd | RESETclear);
692 lp->hw.intmask(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 spin_unlock(&lp->lock);
Michael Grzeschik226ee672014-09-29 11:55:34 +0200694 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
Joe Perchesa34c0932015-05-05 10:05:55 -0700697 arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700698 lp->hw.status(dev), lp->intmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 boguscount = 5;
701 do {
Joe Perchese15b0362015-05-05 10:06:12 -0700702 status = lp->hw.status(dev);
Joe Perchescb334642015-05-05 10:05:47 -0700703 diagstatus = (status >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Joe Perchesa34c0932015-05-05 10:05:55 -0700705 arc_printk(D_DEBUG, dev, "%s: %d: %s: status=%x\n",
706 __FILE__, __LINE__, __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 didsomething = 0;
708
Joe Perchesf2f0a162015-05-05 10:05:52 -0700709 /* RESET flag was enabled - card is resetting and if RX is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 * disabled, it's NOT because we just got a packet.
Joe Perchescb334642015-05-05 10:05:47 -0700711 *
Joe Perchesf2f0a162015-05-05 10:05:52 -0700712 * The card is in an undefined state.
713 * Clear it out and start over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
715 if (status & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700716 arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n",
717 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 arcnet_close(dev);
719 arcnet_open(dev);
720
721 /* get out of the interrupt handler! */
722 break;
723 }
Joe Perchesf2f0a162015-05-05 10:05:52 -0700724 /* RX is inhibited - we must have received something.
725 * Prepare to receive into the next buffer.
Joe Perchescb334642015-05-05 10:05:47 -0700726 *
Joe Perchesf2f0a162015-05-05 10:05:52 -0700727 * We don't actually copy the received packet from the card
728 * until after the transmit handler runs (and possibly
729 * launches the next tx); this should improve latency slightly
730 * if we get both types of interrupts at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
732 recbuf = -1;
733 if (status & lp->intmask & NORXflag) {
734 recbuf = lp->cur_rx;
Joe Perchesa34c0932015-05-05 10:05:55 -0700735 arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n",
736 recbuf, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 lp->cur_rx = get_arcbuf(dev);
739 if (lp->cur_rx != -1) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700740 arc_printk(D_DURING, dev, "enabling receive to buffer #%d\n",
741 lp->cur_rx);
Joe Perchese15b0362015-05-05 10:06:12 -0700742 lp->hw.command(dev, RXcmd | (lp->cur_rx << 3) | RXbcasts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 didsomething++;
745 }
746
Joe Perchescb334642015-05-05 10:05:47 -0700747 if ((diagstatus & EXCNAKflag)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700748 arc_printk(D_DURING, dev, "EXCNAK IRQ (diagstat=%Xh)\n",
749 diagstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Joe Perchese15b0362015-05-05 10:06:12 -0700751 lp->hw.command(dev, NOTXcmd); /* disable transmit */
Joe Perchescb334642015-05-05 10:05:47 -0700752 lp->excnak_pending = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Joe Perchese15b0362015-05-05 10:06:12 -0700754 lp->hw.command(dev, EXCNAKclear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 lp->intmask &= ~(EXCNAKflag);
Joe Perchescb334642015-05-05 10:05:47 -0700756 didsomething++;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* a transmit finished, and we're interested in it. */
760 if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700761 lp->intmask &= ~(TXFREEflag | EXCNAKflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700763 arc_printk(D_DURING, dev, "TX IRQ (stat=%Xh)\n",
764 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (lp->cur_tx != -1 && !lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700767 if (!(status & TXACKflag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (lp->lasttrans_dest != 0) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700769 arc_printk(D_EXTRA, dev,
770 "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
771 status,
772 lp->lasttrans_dest);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000773 dev->stats.tx_errors++;
774 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 } else {
Joe Perchesa34c0932015-05-05 10:05:55 -0700776 arc_printk(D_DURING, dev,
777 "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
778 status,
779 lp->lasttrans_dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781 }
782
783 if (lp->outgoing.proto &&
784 lp->outgoing.proto->ack_tx) {
Joe Perchescb334642015-05-05 10:05:47 -0700785 int ackstatus;
Joe Perches01a1d5a2015-05-05 10:05:48 -0700786
Joe Perchescb334642015-05-05 10:05:47 -0700787 if (status & TXACKflag)
788 ackstatus = 2;
789 else if (lp->excnak_pending)
790 ackstatus = 1;
791 else
792 ackstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Joe Perchescb334642015-05-05 10:05:47 -0700794 lp->outgoing.proto
795 ->ack_tx(dev, ackstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 }
798 if (lp->cur_tx != -1)
799 release_arcbuf(dev, lp->cur_tx);
800
801 lp->cur_tx = -1;
802 lp->timed_out = 0;
803 didsomething++;
804
805 /* send another packet if there is one */
806 go_tx(dev);
807
808 /* continue a split packet, if any */
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700809 if (lp->outgoing.proto &&
810 lp->outgoing.proto->continue_tx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int txbuf = get_arcbuf(dev);
Joe Perches01a1d5a2015-05-05 10:05:48 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (txbuf != -1) {
814 if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
815 /* that was the last segment */
Stephen Hemminger5803c512009-01-09 13:01:08 +0000816 dev->stats.tx_bytes += lp->outgoing.skb->len;
Joe Perches7f5e7602015-05-05 10:05:49 -0700817 if (!lp->outgoing.proto->ack_tx) {
Joe Perchescb334642015-05-05 10:05:47 -0700818 dev_kfree_skb_irq(lp->outgoing.skb);
819 lp->outgoing.proto = NULL;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822 lp->next_tx = txbuf;
823 }
824 }
825 /* inform upper layers of idleness, if necessary */
826 if (lp->cur_tx == -1)
827 netif_wake_queue(dev);
828 }
829 /* now process the received packet, if any */
830 if (recbuf != -1) {
Joe Perches72aeea42015-05-05 10:05:54 -0700831 if (BUGLVL(D_RX))
832 arcnet_dump_packet(dev, recbuf, "rx irq", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 arcnet_rx(dev, recbuf);
835 release_arcbuf(dev, recbuf);
836
837 didsomething++;
838 }
839 if (status & lp->intmask & RECONflag) {
Joe Perchese15b0362015-05-05 10:06:12 -0700840 lp->hw.command(dev, CFLAGScmd | CONFIGclear);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000841 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Joe Perchesa34c0932015-05-05 10:05:55 -0700843 arc_printk(D_RECON, dev, "Network reconfiguration detected (status=%Xh)\n",
844 status);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700845 /* MYRECON bit is at bit 7 of diagstatus */
Joe Perchescb334642015-05-05 10:05:47 -0700846 if (diagstatus & 0x80)
Joe Perchesa34c0932015-05-05 10:05:55 -0700847 arc_printk(D_RECON, dev, "Put out that recon myself\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* is the RECON info empty or old? */
850 if (!lp->first_recon || !lp->last_recon ||
S.Caglar Onur9307b572008-03-28 14:41:24 -0700851 time_after(jiffies, lp->last_recon + HZ * 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (lp->network_down)
Joe Perchesa34c0932015-05-05 10:05:55 -0700853 arc_printk(D_NORMAL, dev, "reconfiguration detected: cabling restored?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 lp->first_recon = lp->last_recon = jiffies;
855 lp->num_recons = lp->network_down = 0;
856
Joe Perchesa34c0932015-05-05 10:05:55 -0700857 arc_printk(D_DURING, dev, "recon: clearing counters.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 } else { /* add to current RECON counter */
859 lp->last_recon = jiffies;
860 lp->num_recons++;
861
Joe Perchesa34c0932015-05-05 10:05:55 -0700862 arc_printk(D_DURING, dev, "recon: counter=%d, time=%lds, net=%d\n",
863 lp->num_recons,
864 (lp->last_recon - lp->first_recon) / HZ,
865 lp->network_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /* if network is marked up;
868 * and first_recon and last_recon are 60+ apart;
869 * and the average no. of recons counted is
870 * > RECON_THRESHOLD/min;
871 * then print a warning message.
872 */
Joe Perches8e95a202009-12-03 07:58:21 +0000873 if (!lp->network_down &&
874 (lp->last_recon - lp->first_recon) <= HZ * 60 &&
875 lp->num_recons >= RECON_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 lp->network_down = 1;
Joe Perchesa34c0932015-05-05 10:05:55 -0700877 arc_printk(D_NORMAL, dev, "many reconfigurations detected: cabling problem?\n");
Joe Perches8e95a202009-12-03 07:58:21 +0000878 } else if (!lp->network_down &&
879 lp->last_recon - lp->first_recon > HZ * 60) {
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700880 /* reset counters if we've gone for
881 * over a minute.
882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 lp->first_recon = lp->last_recon;
884 lp->num_recons = 1;
885 }
886 }
S.Caglar Onur9307b572008-03-28 14:41:24 -0700887 } else if (lp->network_down &&
Joe Perchescb334642015-05-05 10:05:47 -0700888 time_after(jiffies, lp->last_recon + HZ * 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (lp->network_down)
Joe Perchesa34c0932015-05-05 10:05:55 -0700890 arc_printk(D_NORMAL, dev, "cabling restored?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 lp->first_recon = lp->last_recon = 0;
892 lp->num_recons = lp->network_down = 0;
893
Joe Perchesa34c0932015-05-05 10:05:55 -0700894 arc_printk(D_DURING, dev, "not recon: clearing counters anyway.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
Joe Perches7f5e7602015-05-05 10:05:49 -0700897 if (didsomething)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 retval |= IRQ_HANDLED;
Joe Perches7f5e7602015-05-05 10:05:49 -0700899 } while (--boguscount && didsomething);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Joe Perchesa34c0932015-05-05 10:05:55 -0700901 arc_printk(D_DURING, dev, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700902 lp->hw.status(dev), boguscount);
Joe Perchesa34c0932015-05-05 10:05:55 -0700903 arc_printk(D_DURING, dev, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Joe Perchese15b0362015-05-05 10:06:12 -0700905 lp->hw.intmask(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 udelay(1);
Joe Perchese15b0362015-05-05 10:06:12 -0700907 lp->hw.intmask(dev, lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 spin_unlock(&lp->lock);
910 return retval;
911}
Joe Perches811eafc2015-05-05 10:05:57 -0700912EXPORT_SYMBOL(arcnet_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Joe Perchesf2f0a162015-05-05 10:05:52 -0700914/* This is a generic packet receiver that calls arcnet??_rx depending on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * protocol ID found.
916 */
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100917static void arcnet_rx(struct net_device *dev, int bufnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Wang Chen454d7c92008-11-12 23:37:49 -0800919 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct archdr pkt;
921 struct arc_rfc1201 *soft;
922 int length, ofs;
923
924 soft = &pkt.soft.rfc1201;
925
Dan Carpenter087d2732013-07-19 08:48:05 +0300926 lp->hw.copy_from_card(dev, bufnum, 0, &pkt, ARC_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (pkt.hard.offset[0]) {
928 ofs = pkt.hard.offset[0];
929 length = 256 - ofs;
930 } else {
931 ofs = pkt.hard.offset[1];
932 length = 512 - ofs;
933 }
934
935 /* get the full header, if possible */
Joe Perches7f5e7602015-05-05 10:05:49 -0700936 if (sizeof(pkt.soft) <= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
Joe Perches7f5e7602015-05-05 10:05:49 -0700938 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 memset(&pkt.soft, 0, sizeof(pkt.soft));
940 lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
941 }
942
Joe Perchesa34c0932015-05-05 10:05:55 -0700943 arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
944 bufnum, pkt.hard.source, pkt.hard.dest, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Stephen Hemminger5803c512009-01-09 13:01:08 +0000946 dev->stats.rx_packets++;
947 dev->stats.rx_bytes += length + ARC_HDR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* call the right receiver for the protocol */
950 if (arc_proto_map[soft->proto]->is_ip) {
Joe Perches72aeea42015-05-05 10:05:54 -0700951 if (BUGLVL(D_PROTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct ArcProto
953 *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
954 *newp = arc_proto_map[soft->proto];
955
956 if (oldp != newp) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700957 arc_printk(D_PROTO, dev,
958 "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
959 soft->proto, pkt.hard.source,
960 newp->suffix, oldp->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 }
963
964 /* broadcasts will always be done with the last-used encap. */
965 lp->default_proto[0] = soft->proto;
966
967 /* in striking contrast, the following isn't a hack. */
968 lp->default_proto[pkt.hard.source] = soft->proto;
969 }
970 /* call the protocol-specific receiver. */
971 arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static void null_rx(struct net_device *dev, int bufnum,
975 struct archdr *pkthdr, int length)
976{
Joe Perchesa34c0932015-05-05 10:05:55 -0700977 arc_printk(D_PROTO, dev,
978 "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
979 pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982static int null_build_header(struct sk_buff *skb, struct net_device *dev,
983 unsigned short type, uint8_t daddr)
984{
Wang Chen454d7c92008-11-12 23:37:49 -0800985 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Joe Perchesa34c0932015-05-05 10:05:55 -0700987 arc_printk(D_PROTO, dev,
988 "tx: can't build header for encap %02Xh; load a protocol driver.\n",
989 lp->default_proto[daddr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* always fails */
992 return 0;
993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995/* the "do nothing" prepare_tx function warns that there's nothing to do. */
996static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
997 int length, int bufnum)
998{
Wang Chen454d7c92008-11-12 23:37:49 -0800999 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 struct arc_hardware newpkt;
1001
Joe Perchesa34c0932015-05-05 10:05:55 -07001002 arc_printk(D_PROTO, dev, "tx: no encap for this host; load a protocol driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /* send a packet to myself -- will never get received, of course */
1005 newpkt.source = newpkt.dest = dev->dev_addr[0];
1006
1007 /* only one byte of actual data (and it's random) */
1008 newpkt.offset[0] = 0xFF;
1009
1010 lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1011
1012 return 1; /* done */
1013}