blob: 542e2b46b9ebe7d56f3ef7a5909a5269c3158753 [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"
Joe Perches8e0f2952015-05-05 10:06:13 -070056#include "com9026.h"
Joe Perches26c6d282015-05-05 10:06:03 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* "do nothing" functions for protocol drivers */
59static void null_rx(struct net_device *dev, int bufnum,
60 struct archdr *pkthdr, int length);
61static int null_build_header(struct sk_buff *skb, struct net_device *dev,
62 unsigned short type, uint8_t daddr);
63static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
64 int length, int bufnum);
65
Adrian Bunkf03aa2d2006-01-14 03:10:22 +010066static void arcnet_rx(struct net_device *dev, int bufnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Joe Perchesf2f0a162015-05-05 10:05:52 -070068/* one ArcProto per possible proto ID. None of the elements of
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * arc_proto_map are allowed to be NULL; they will get set to
70 * arc_proto_default instead. It also must not be NULL; if you would like
71 * to set it to NULL, set it to &arc_proto_null instead.
72 */
Joe Perches811eafc2015-05-05 10:05:57 -070073struct ArcProto *arc_proto_map[256];
74EXPORT_SYMBOL(arc_proto_map);
75
76struct ArcProto *arc_proto_default;
77EXPORT_SYMBOL(arc_proto_default);
78
79struct ArcProto *arc_bcast_proto;
80EXPORT_SYMBOL(arc_bcast_proto);
81
82struct ArcProto *arc_raw_proto;
83EXPORT_SYMBOL(arc_raw_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Joe Perches7f5e7602015-05-05 10:05:49 -070085static struct ArcProto arc_proto_null = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .suffix = '?',
87 .mtu = XMTU,
88 .is_ip = 0,
89 .rx = null_rx,
90 .build_header = null_build_header,
91 .prepare_tx = null_prepare_tx,
92 .continue_tx = NULL,
93 .ack_tx = NULL
94};
95
96/* Exported function prototypes */
97int arcnet_debug = ARCNET_DEBUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098EXPORT_SYMBOL(arcnet_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/* Internal function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700102 unsigned short type, const void *daddr,
103 const void *saddr, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int go_tx(struct net_device *dev);
105
106static int debug = ARCNET_DEBUG;
107module_param(debug, int, 0);
108MODULE_LICENSE("GPL");
109
110static int __init arcnet_init(void)
111{
112 int count;
113
114 arcnet_debug = debug;
115
Joe Perches05a24b22015-05-05 10:05:56 -0700116 pr_info("arcnet loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 /* initialize the protocol map */
119 arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
120 for (count = 0; count < 256; count++)
121 arc_proto_map[count] = arc_proto_default;
122
Joe Perches72aeea42015-05-05 10:05:54 -0700123 if (BUGLVL(D_DURING))
Joe Perches05a24b22015-05-05 10:05:56 -0700124 pr_info("struct sizes: %Zd %Zd %Zd %Zd %Zd\n",
125 sizeof(struct arc_hardware),
126 sizeof(struct arc_rfc1201),
127 sizeof(struct arc_rfc1051),
128 sizeof(struct arc_eth_encap),
129 sizeof(struct archdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return 0;
132}
133
134static void __exit arcnet_exit(void)
135{
136}
137
138module_init(arcnet_init);
139module_exit(arcnet_exit);
140
Joe Perchesf2f0a162015-05-05 10:05:52 -0700141/* Dump the contents of an sk_buff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#if ARCNET_DEBUG_MAX & D_SKB
143void arcnet_dump_skb(struct net_device *dev,
144 struct sk_buff *skb, char *desc)
145{
Joe Perchesad361c92009-07-06 13:05:40 -0700146 char hdr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Joe Perchesad361c92009-07-06 13:05:40 -0700148 /* dump the packet */
149 snprintf(hdr, sizeof(hdr), "%6s:%s skb->data:", dev->name, desc);
150 print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
151 16, 1, skb->data, skb->len, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153EXPORT_SYMBOL(arcnet_dump_skb);
154#endif
155
Joe Perchesf2f0a162015-05-05 10:05:52 -0700156/* Dump the contents of an ARCnet buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100158static void arcnet_dump_packet(struct net_device *dev, int bufnum,
159 char *desc, int take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Wang Chen454d7c92008-11-12 23:37:49 -0800161 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int i, length;
163 unsigned long flags = 0;
164 static uint8_t buf[512];
Joe Perchesad361c92009-07-06 13:05:40 -0700165 char hdr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* hw.copy_from_card expects IRQ context so take the IRQ lock
Joe Perchesf2f0a162015-05-05 10:05:52 -0700168 * to keep it single threaded
169 */
Joe Perchescb334642015-05-05 10:05:47 -0700170 if (take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 spin_lock_irqsave(&lp->lock, flags);
172
173 lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
Joe Perchescb334642015-05-05 10:05:47 -0700174 if (take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 spin_unlock_irqrestore(&lp->lock, flags);
176
177 /* if the offset[0] byte is nonzero, this is a 256-byte packet */
178 length = (buf[2] ? 256 : 512);
179
Joe Perchesad361c92009-07-06 13:05:40 -0700180 /* dump the packet */
181 snprintf(hdr, sizeof(hdr), "%6s:%s packet dump:", dev->name, desc);
182 print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
183 16, 1, buf, length, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100186#else
187
Joe Perchescb334642015-05-05 10:05:47 -0700188#define arcnet_dump_packet(dev, bufnum, desc, take_arcnet_lock) do { } while (0)
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
191
Joe Perchesf2f0a162015-05-05 10:05:52 -0700192/* Unregister a protocol driver from the arc_proto_map. Protocol drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * are responsible for registering themselves, but the unregister routine
194 * is pretty generic so we'll do it here.
195 */
196void arcnet_unregister_proto(struct ArcProto *proto)
197{
198 int count;
199
200 if (arc_proto_default == proto)
201 arc_proto_default = &arc_proto_null;
202 if (arc_bcast_proto == proto)
203 arc_bcast_proto = arc_proto_default;
204 if (arc_raw_proto == proto)
205 arc_raw_proto = arc_proto_default;
206
207 for (count = 0; count < 256; count++) {
208 if (arc_proto_map[count] == proto)
209 arc_proto_map[count] = arc_proto_default;
210 }
211}
Joe Perches811eafc2015-05-05 10:05:57 -0700212EXPORT_SYMBOL(arcnet_unregister_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Joe Perchesf2f0a162015-05-05 10:05:52 -0700214/* Add a buffer to the queue. Only the interrupt handler is allowed to do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * this, unless interrupts are disabled.
Joe Perchescb334642015-05-05 10:05:47 -0700216 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * Note: we don't check for a full queue, since there aren't enough buffers
218 * to more than fill it.
219 */
220static void release_arcbuf(struct net_device *dev, int bufnum)
221{
Wang Chen454d7c92008-11-12 23:37:49 -0800222 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int i;
224
225 lp->buf_queue[lp->first_free_buf++] = bufnum;
226 lp->first_free_buf %= 5;
227
Joe Perches72aeea42015-05-05 10:05:54 -0700228 if (BUGLVL(D_DURING)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700229 arc_printk(D_DURING, dev, "release_arcbuf: freed #%d; buffer queue is now: ",
230 bufnum);
Joe Perchescb334642015-05-05 10:05:47 -0700231 for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
Joe Perchesa34c0932015-05-05 10:05:55 -0700232 arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
233 arc_cont(D_DURING, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235}
236
Joe Perchesf2f0a162015-05-05 10:05:52 -0700237/* Get a buffer from the queue.
238 * If this returns -1, there are no buffers available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
240static int get_arcbuf(struct net_device *dev)
241{
Wang Chen454d7c92008-11-12 23:37:49 -0800242 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int buf = -1, i;
244
245 if (!atomic_dec_and_test(&lp->buf_lock)) {
246 /* already in this function */
Joe Perchesa34c0932015-05-05 10:05:55 -0700247 arc_printk(D_NORMAL, dev, "get_arcbuf: overlap (%d)!\n",
248 lp->buf_lock.counter);
Joe Perches7f5e7602015-05-05 10:05:49 -0700249 } else { /* we can continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (lp->next_buf >= 5)
251 lp->next_buf -= 5;
252
Joe Perches7f5e7602015-05-05 10:05:49 -0700253 if (lp->next_buf == lp->first_free_buf) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700254 arc_printk(D_NORMAL, dev, "get_arcbuf: BUG: no buffers are available??\n");
Joe Perches7f5e7602015-05-05 10:05:49 -0700255 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 buf = lp->buf_queue[lp->next_buf++];
257 lp->next_buf %= 5;
258 }
259 }
260
Joe Perches72aeea42015-05-05 10:05:54 -0700261 if (BUGLVL(D_DURING)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700262 arc_printk(D_DURING, dev, "get_arcbuf: got #%d; buffer queue is now: ",
263 buf);
Joe Perchescb334642015-05-05 10:05:47 -0700264 for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
Joe Perchesa34c0932015-05-05 10:05:55 -0700265 arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
266 arc_cont(D_DURING, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
269 atomic_inc(&lp->buf_lock);
270 return buf;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static int choose_mtu(void)
274{
275 int count, mtu = 65535;
276
277 /* choose the smallest MTU of all available encaps */
278 for (count = 0; count < 256; count++) {
Joe Perches8e95a202009-12-03 07:58:21 +0000279 if (arc_proto_map[count] != &arc_proto_null &&
280 arc_proto_map[count]->mtu < mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 mtu = arc_proto_map[count]->mtu;
282 }
283 }
284
285 return mtu == 65535 ? XMTU : mtu;
286}
287
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700288static const struct header_ops arcnet_header_ops = {
289 .create = arcnet_header,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700290};
291
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000292static const struct net_device_ops arcnet_netdev_ops = {
293 .ndo_open = arcnet_open,
294 .ndo_stop = arcnet_close,
295 .ndo_start_xmit = arcnet_send_packet,
296 .ndo_tx_timeout = arcnet_timeout,
297};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299/* Setup a struct device for ARCnet. */
300static void arcdev_setup(struct net_device *dev)
301{
302 dev->type = ARPHRD_ARCNET;
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000303 dev->netdev_ops = &arcnet_netdev_ops;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700304 dev->header_ops = &arcnet_header_ops;
Michael Grzeschik980137a2015-09-17 15:18:34 +0200305 dev->hard_header_len = sizeof(struct arc_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 dev->mtu = choose_mtu();
307
308 dev->addr_len = ARCNET_ALEN;
309 dev->tx_queue_len = 100;
310 dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
311 dev->watchdog_timeo = TX_TIMEOUT;
312
313 /* New-style flags. */
314 dev->flags = IFF_BROADCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000317struct net_device *alloc_arcdev(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct net_device *dev;
320
321 dev = alloc_netdev(sizeof(struct arcnet_local),
Tom Gundersenc835a672014-07-14 16:37:24 +0200322 name && *name ? name : "arc%d", NET_NAME_UNKNOWN,
323 arcdev_setup);
Joe Perchescb334642015-05-05 10:05:47 -0700324 if (dev) {
Wang Chen454d7c92008-11-12 23:37:49 -0800325 struct arcnet_local *lp = netdev_priv(dev);
Joe Perches01a1d5a2015-05-05 10:05:48 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_lock_init(&lp->lock);
328 }
329
330 return dev;
331}
Joe Perches811eafc2015-05-05 10:05:57 -0700332EXPORT_SYMBOL(alloc_arcdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Joe Perchesf2f0a162015-05-05 10:05:52 -0700334/* Open/initialize the board. This is called sometime after booting when
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * the 'ifconfig' program is run.
336 *
337 * This routine should set everything up anew at each open, even registers
338 * that "should" only need to be set once at boot, so that there is
339 * non-reboot way to recover if something goes wrong.
340 */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000341int arcnet_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Wang Chen454d7c92008-11-12 23:37:49 -0800343 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int count, newmtu, error;
345
Joe Perchesa34c0932015-05-05 10:05:55 -0700346 arc_printk(D_INIT, dev, "opened.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (!try_module_get(lp->hw.owner))
349 return -ENODEV;
350
Joe Perches72aeea42015-05-05 10:05:54 -0700351 if (BUGLVL(D_PROTO)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700352 arc_printk(D_PROTO, dev, "protocol map (default is '%c'): ",
353 arc_proto_default->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 for (count = 0; count < 256; count++)
Joe Perchesa34c0932015-05-05 10:05:55 -0700355 arc_cont(D_PROTO, "%c", arc_proto_map[count]->suffix);
356 arc_cont(D_PROTO, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
Joe Perchesa34c0932015-05-05 10:05:55 -0700359 arc_printk(D_INIT, dev, "arcnet_open: resetting card.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* try to put the card in a defined state - if it fails the first
362 * time, actually reset it.
363 */
364 error = -ENODEV;
Joe Perchese15b0362015-05-05 10:06:12 -0700365 if (lp->hw.reset(dev, 0) && lp->hw.reset(dev, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto out_module_put;
367
368 newmtu = choose_mtu();
369 if (newmtu < dev->mtu)
370 dev->mtu = newmtu;
371
Joe Perchesa34c0932015-05-05 10:05:55 -0700372 arc_printk(D_INIT, dev, "arcnet_open: mtu: %d.\n", dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* autodetect the encapsulation for each host. */
375 memset(lp->default_proto, 0, sizeof(lp->default_proto));
376
377 /* the broadcast address is special - use the 'bcast' protocol */
378 for (count = 0; count < 256; count++) {
379 if (arc_proto_map[count] == arc_bcast_proto) {
380 lp->default_proto[0] = count;
381 break;
382 }
383 }
384
385 /* initialize buffers */
386 atomic_set(&lp->buf_lock, 1);
387
388 lp->next_buf = lp->first_free_buf = 0;
389 release_arcbuf(dev, 0);
390 release_arcbuf(dev, 1);
391 release_arcbuf(dev, 2);
392 release_arcbuf(dev, 3);
393 lp->cur_tx = lp->next_tx = -1;
394 lp->cur_rx = -1;
395
396 lp->rfc1201.sequence = 1;
397
398 /* bring up the hardware driver */
399 if (lp->hw.open)
400 lp->hw.open(dev);
401
402 if (dev->dev_addr[0] == 0)
Joe Perchesa34c0932015-05-05 10:05:55 -0700403 arc_printk(D_NORMAL, dev, "WARNING! Station address 00 is reserved for broadcasts!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 else if (dev->dev_addr[0] == 255)
Joe Perchesa34c0932015-05-05 10:05:55 -0700405 arc_printk(D_NORMAL, dev, "WARNING! Station address FF may confuse DOS networking programs!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Joe Perchesa34c0932015-05-05 10:05:55 -0700407 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Joe Perchese15b0362015-05-05 10:06:12 -0700408 if (lp->hw.status(dev) & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700409 arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
410 __FILE__, __LINE__, __func__);
Joe Perchese15b0362015-05-05 10:06:12 -0700411 lp->hw.command(dev, CFLAGScmd | RESETclear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Joe Perchesa34c0932015-05-05 10:05:55 -0700414 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* make sure we're ready to receive IRQ's. */
Joe Perchese15b0362015-05-05 10:06:12 -0700416 lp->hw.intmask(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 udelay(1); /* give it time to set the mask before
418 * we reset it again. (may not even be
419 * necessary)
420 */
Joe Perchesa34c0932015-05-05 10:05:55 -0700421 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 lp->intmask = NORXflag | RECONflag;
Joe Perchese15b0362015-05-05 10:06:12 -0700423 lp->hw.intmask(dev, lp->intmask);
Joe Perchesa34c0932015-05-05 10:05:55 -0700424 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 netif_start_queue(dev);
427
428 return 0;
429
430 out_module_put:
431 module_put(lp->hw.owner);
432 return error;
433}
Joe Perches811eafc2015-05-05 10:05:57 -0700434EXPORT_SYMBOL(arcnet_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/* The inverse routine to arcnet_open - shuts down the card. */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000437int arcnet_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Wang Chen454d7c92008-11-12 23:37:49 -0800439 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 netif_stop_queue(dev);
442
443 /* flush TX and disable RX */
Joe Perchese15b0362015-05-05 10:06:12 -0700444 lp->hw.intmask(dev, 0);
445 lp->hw.command(dev, NOTXcmd); /* stop transmit */
446 lp->hw.command(dev, NORXcmd); /* disable receive */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 mdelay(1);
448
449 /* shut down the card */
450 lp->hw.close(dev);
451 module_put(lp->hw.owner);
452 return 0;
453}
Joe Perches811eafc2015-05-05 10:05:57 -0700454EXPORT_SYMBOL(arcnet_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700457 unsigned short type, const void *daddr,
458 const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700460 const struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 uint8_t _daddr, proto_num;
462 struct ArcProto *proto;
463
Joe Perchesa34c0932015-05-05 10:05:55 -0700464 arc_printk(D_DURING, dev,
465 "create header from %d to %d; protocol %d (%Xh); size %u.\n",
466 saddr ? *(uint8_t *)saddr : -1,
467 daddr ? *(uint8_t *)daddr : -1,
468 type, type, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Joe Perchescb334642015-05-05 10:05:47 -0700470 if (skb->len != 0 && len != skb->len)
Joe Perchesa34c0932015-05-05 10:05:55 -0700471 arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
472 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Joe Perchescb334642015-05-05 10:05:47 -0700474 /* Type is host order - ? */
475 if (type == ETH_P_ARCNET) {
476 proto = arc_raw_proto;
Joe Perchesa34c0932015-05-05 10:05:55 -0700477 arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n",
478 proto->suffix);
Joe Perchescb334642015-05-05 10:05:47 -0700479 _daddr = daddr ? *(uint8_t *)daddr : 0;
Joe Perches7f5e7602015-05-05 10:05:49 -0700480 } else if (!daddr) {
Joe Perchesf2f0a162015-05-05 10:05:52 -0700481 /* if the dest addr isn't provided, we can't choose an
482 * encapsulation! Store the packet type (eg. ETH_P_IP)
483 * for now, and we'll push on a real header when we do
484 * rebuild_header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
Joe Perchescb334642015-05-05 10:05:47 -0700486 *(uint16_t *)skb_push(skb, 2) = type;
Joe Perchesf2f0a162015-05-05 10:05:52 -0700487 /* XXX: Why not use skb->mac_len? */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700488 if (skb->network_header - skb->mac_header != 2)
Joe Perchesa34c0932015-05-05 10:05:55 -0700489 arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! diff (%u) is not 2!\n",
490 skb->network_header - skb->mac_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -2; /* return error -- can't transmit yet! */
Joe Perches7f5e7602015-05-05 10:05:49 -0700492 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* otherwise, we can just add the header as usual. */
Joe Perchescb334642015-05-05 10:05:47 -0700494 _daddr = *(uint8_t *)daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 proto_num = lp->default_proto[_daddr];
496 proto = arc_proto_map[proto_num];
Joe Perchesa34c0932015-05-05 10:05:55 -0700497 arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n",
498 proto_num, proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (proto == &arc_proto_null && arc_bcast_proto != proto) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700500 arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n",
501 arc_bcast_proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 proto = arc_bcast_proto;
503 }
504 }
505 return proto->build_header(skb, dev, type, _daddr);
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/* Called by the kernel in order to transmit a packet. */
Stephen Hemminger613573252009-08-31 19:50:58 +0000509netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
Joe Perchescb334642015-05-05 10:05:47 -0700510 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Wang Chen454d7c92008-11-12 23:37:49 -0800512 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct archdr *pkt;
514 struct arc_rfc1201 *soft;
515 struct ArcProto *proto;
516 int txbuf;
517 unsigned long flags;
Michael Grzeschikb82de0e2015-04-22 16:41:46 +0200518 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Joe Perchesa34c0932015-05-05 10:05:55 -0700520 arc_printk(D_DURING, dev,
521 "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700522 lp->hw.status(dev), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Joe Perchescb334642015-05-05 10:05:47 -0700524 pkt = (struct archdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 soft = &pkt->soft.rfc1201;
526 proto = arc_proto_map[soft->proto];
527
Joe Perchesa34c0932015-05-05 10:05:55 -0700528 arc_printk(D_SKB_SIZE, dev, "skb: transmitting %d bytes to %02X\n",
529 skb->len, pkt->hard.dest);
Joe Perches72aeea42015-05-05 10:05:54 -0700530 if (BUGLVL(D_SKB))
531 arcnet_dump_skb(dev, skb, "tx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* fits in one packet? */
534 if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700535 arc_printk(D_NORMAL, dev, "fixme: packet too large: compensating badly!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 dev_kfree_skb(skb);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700537 return NETDEV_TX_OK; /* don't try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 /* We're busy transmitting a packet... */
541 netif_stop_queue(dev);
542
543 spin_lock_irqsave(&lp->lock, flags);
Joe Perchese15b0362015-05-05 10:06:12 -0700544 lp->hw.intmask(dev, 0);
Joe Perchescb334642015-05-05 10:05:47 -0700545 if (lp->next_tx == -1)
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700546 txbuf = get_arcbuf(dev);
Joe Perches7f5e7602015-05-05 10:05:49 -0700547 else
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700548 txbuf = -1;
Joe Perches7f5e7602015-05-05 10:05:49 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (txbuf != -1) {
551 if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
552 !proto->ack_tx) {
553 /* done right away and we don't want to acknowledge
Joe Perchesf2f0a162015-05-05 10:05:52 -0700554 * the package later - forget about it now
555 */
Stephen Hemminger5803c512009-01-09 13:01:08 +0000556 dev->stats.tx_bytes += skb->len;
Michael Grzeschikb82de0e2015-04-22 16:41:46 +0200557 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 } else {
559 /* do it the 'split' way */
560 lp->outgoing.proto = proto;
561 lp->outgoing.skb = skb;
562 lp->outgoing.pkt = pkt;
563
564 if (proto->continue_tx &&
565 proto->continue_tx(dev, txbuf)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700566 arc_printk(D_NORMAL, dev,
567 "bug! continue_tx finished the first time! (proto='%c')\n",
568 proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 }
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700571 retval = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 lp->next_tx = txbuf;
573 } else {
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700574 retval = NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Joe Perchesa34c0932015-05-05 10:05:55 -0700577 arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700578 __FILE__, __LINE__, __func__, lp->hw.status(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* make sure we didn't ignore a TX IRQ while we were in here */
Joe Perchese15b0362015-05-05 10:06:12 -0700580 lp->hw.intmask(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Joe Perchesa34c0932015-05-05 10:05:55 -0700582 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Joe Perchescb334642015-05-05 10:05:47 -0700583 lp->intmask |= TXFREEflag | EXCNAKflag;
Joe Perchese15b0362015-05-05 10:06:12 -0700584 lp->hw.intmask(dev, lp->intmask);
Joe Perchesa34c0932015-05-05 10:05:55 -0700585 arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700586 __FILE__, __LINE__, __func__, lp->hw.status(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 spin_unlock_irqrestore(&lp->lock, flags);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700589 return retval; /* no need to try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
Joe Perches811eafc2015-05-05 10:05:57 -0700591EXPORT_SYMBOL(arcnet_send_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Joe Perchesf2f0a162015-05-05 10:05:52 -0700593/* Actually start transmitting a packet that was loaded into a buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * by prepare_tx. This should _only_ be called by the interrupt handler.
595 */
596static int go_tx(struct net_device *dev)
597{
Wang Chen454d7c92008-11-12 23:37:49 -0800598 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Joe Perchesa34c0932015-05-05 10:05:55 -0700600 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 -0700601 lp->hw.status(dev), lp->intmask, lp->next_tx, lp->cur_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (lp->cur_tx != -1 || lp->next_tx == -1)
604 return 0;
605
Joe Perches72aeea42015-05-05 10:05:54 -0700606 if (BUGLVL(D_TX))
607 arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 lp->cur_tx = lp->next_tx;
610 lp->next_tx = -1;
611
612 /* start sending */
Joe Perchese15b0362015-05-05 10:06:12 -0700613 lp->hw.command(dev, TXcmd | (lp->cur_tx << 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Stephen Hemminger5803c512009-01-09 13:01:08 +0000615 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 lp->lasttrans_dest = lp->lastload_dest;
617 lp->lastload_dest = 0;
618 lp->excnak_pending = 0;
Joe Perchescb334642015-05-05 10:05:47 -0700619 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 return 1;
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/* Called by the kernel when transmit times out */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000625void arcnet_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 unsigned long flags;
Wang Chen454d7c92008-11-12 23:37:49 -0800628 struct arcnet_local *lp = netdev_priv(dev);
Joe Perchese15b0362015-05-05 10:06:12 -0700629 int status = lp->hw.status(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 char *msg;
631
632 spin_lock_irqsave(&lp->lock, flags);
633 if (status & TXFREEflag) { /* transmit _DID_ finish */
634 msg = " - missed IRQ?";
635 } else {
636 msg = "";
Stephen Hemminger5803c512009-01-09 13:01:08 +0000637 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 lp->timed_out = 1;
Joe Perchese15b0362015-05-05 10:06:12 -0700639 lp->hw.command(dev, NOTXcmd | (lp->cur_tx << 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Stephen Hemminger5803c512009-01-09 13:01:08 +0000641 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* make sure we didn't miss a TX or a EXC NAK IRQ */
Joe Perchese15b0362015-05-05 10:06:12 -0700644 lp->hw.intmask(dev, 0);
Joe Perchescb334642015-05-05 10:05:47 -0700645 lp->intmask |= TXFREEflag | EXCNAKflag;
Joe Perchese15b0362015-05-05 10:06:12 -0700646 lp->hw.intmask(dev, lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 spin_unlock_irqrestore(&lp->lock, flags);
649
Joe Perchescb334642015-05-05 10:05:47 -0700650 if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700651 arc_printk(D_EXTRA, dev, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
652 msg, status, lp->intmask, lp->lasttrans_dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 lp->last_timeout = jiffies;
654 }
655
656 if (lp->cur_tx == -1)
657 netif_wake_queue(dev);
658}
Joe Perches811eafc2015-05-05 10:05:57 -0700659EXPORT_SYMBOL(arcnet_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Joe Perchesf2f0a162015-05-05 10:05:52 -0700661/* The typical workload of the driver: Handle the network interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * interrupts. Establish which device needs attention, and call the correct
663 * chipset interrupt handler.
664 */
David Howells7d12e782006-10-05 14:55:46 +0100665irqreturn_t arcnet_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct net_device *dev = dev_id;
668 struct arcnet_local *lp;
669 int recbuf, status, diagstatus, didsomething, boguscount;
670 int retval = IRQ_NONE;
671
Joe Perchesa34c0932015-05-05 10:05:55 -0700672 arc_printk(D_DURING, dev, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Joe Perchesa34c0932015-05-05 10:05:55 -0700674 arc_printk(D_DURING, dev, "in arcnet_interrupt\n");
Wang Chen454d7c92008-11-12 23:37:49 -0800675
676 lp = netdev_priv(dev);
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200677 BUG_ON(!lp);
Joe Perchescb334642015-05-05 10:05:47 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 spin_lock(&lp->lock);
680
Joe Perchesf2f0a162015-05-05 10:05:52 -0700681 /* RESET flag was enabled - if device is not running, we must
682 * clear it right away (but nothing else).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
684 if (!netif_running(dev)) {
Joe Perchese15b0362015-05-05 10:06:12 -0700685 if (lp->hw.status(dev) & RESETflag)
686 lp->hw.command(dev, CFLAGScmd | RESETclear);
687 lp->hw.intmask(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 spin_unlock(&lp->lock);
Michael Grzeschik226ee672014-09-29 11:55:34 +0200689 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
Joe Perchesa34c0932015-05-05 10:05:55 -0700692 arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700693 lp->hw.status(dev), lp->intmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 boguscount = 5;
696 do {
Joe Perchese15b0362015-05-05 10:06:12 -0700697 status = lp->hw.status(dev);
Joe Perchescb334642015-05-05 10:05:47 -0700698 diagstatus = (status >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Joe Perchesa34c0932015-05-05 10:05:55 -0700700 arc_printk(D_DEBUG, dev, "%s: %d: %s: status=%x\n",
701 __FILE__, __LINE__, __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 didsomething = 0;
703
Joe Perchesf2f0a162015-05-05 10:05:52 -0700704 /* RESET flag was enabled - card is resetting and if RX is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * disabled, it's NOT because we just got a packet.
Joe Perchescb334642015-05-05 10:05:47 -0700706 *
Joe Perchesf2f0a162015-05-05 10:05:52 -0700707 * The card is in an undefined state.
708 * Clear it out and start over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
710 if (status & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700711 arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n",
712 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 arcnet_close(dev);
714 arcnet_open(dev);
715
716 /* get out of the interrupt handler! */
717 break;
718 }
Joe Perchesf2f0a162015-05-05 10:05:52 -0700719 /* RX is inhibited - we must have received something.
720 * Prepare to receive into the next buffer.
Joe Perchescb334642015-05-05 10:05:47 -0700721 *
Joe Perchesf2f0a162015-05-05 10:05:52 -0700722 * We don't actually copy the received packet from the card
723 * until after the transmit handler runs (and possibly
724 * launches the next tx); this should improve latency slightly
725 * if we get both types of interrupts at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
727 recbuf = -1;
728 if (status & lp->intmask & NORXflag) {
729 recbuf = lp->cur_rx;
Joe Perchesa34c0932015-05-05 10:05:55 -0700730 arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n",
731 recbuf, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 lp->cur_rx = get_arcbuf(dev);
734 if (lp->cur_rx != -1) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700735 arc_printk(D_DURING, dev, "enabling receive to buffer #%d\n",
736 lp->cur_rx);
Joe Perchese15b0362015-05-05 10:06:12 -0700737 lp->hw.command(dev, RXcmd | (lp->cur_rx << 3) | RXbcasts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 didsomething++;
740 }
741
Joe Perchescb334642015-05-05 10:05:47 -0700742 if ((diagstatus & EXCNAKflag)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700743 arc_printk(D_DURING, dev, "EXCNAK IRQ (diagstat=%Xh)\n",
744 diagstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Joe Perchese15b0362015-05-05 10:06:12 -0700746 lp->hw.command(dev, NOTXcmd); /* disable transmit */
Joe Perchescb334642015-05-05 10:05:47 -0700747 lp->excnak_pending = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Joe Perchese15b0362015-05-05 10:06:12 -0700749 lp->hw.command(dev, EXCNAKclear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 lp->intmask &= ~(EXCNAKflag);
Joe Perchescb334642015-05-05 10:05:47 -0700751 didsomething++;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* a transmit finished, and we're interested in it. */
755 if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700756 lp->intmask &= ~(TXFREEflag | EXCNAKflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700758 arc_printk(D_DURING, dev, "TX IRQ (stat=%Xh)\n",
759 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (lp->cur_tx != -1 && !lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700762 if (!(status & TXACKflag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (lp->lasttrans_dest != 0) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700764 arc_printk(D_EXTRA, dev,
765 "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
766 status,
767 lp->lasttrans_dest);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000768 dev->stats.tx_errors++;
769 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 } else {
Joe Perchesa34c0932015-05-05 10:05:55 -0700771 arc_printk(D_DURING, dev,
772 "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
773 status,
774 lp->lasttrans_dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 }
777
778 if (lp->outgoing.proto &&
779 lp->outgoing.proto->ack_tx) {
Joe Perchescb334642015-05-05 10:05:47 -0700780 int ackstatus;
Joe Perches01a1d5a2015-05-05 10:05:48 -0700781
Joe Perchescb334642015-05-05 10:05:47 -0700782 if (status & TXACKflag)
783 ackstatus = 2;
784 else if (lp->excnak_pending)
785 ackstatus = 1;
786 else
787 ackstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Joe Perchescb334642015-05-05 10:05:47 -0700789 lp->outgoing.proto
790 ->ack_tx(dev, ackstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 }
793 if (lp->cur_tx != -1)
794 release_arcbuf(dev, lp->cur_tx);
795
796 lp->cur_tx = -1;
797 lp->timed_out = 0;
798 didsomething++;
799
800 /* send another packet if there is one */
801 go_tx(dev);
802
803 /* continue a split packet, if any */
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700804 if (lp->outgoing.proto &&
805 lp->outgoing.proto->continue_tx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int txbuf = get_arcbuf(dev);
Joe Perches01a1d5a2015-05-05 10:05:48 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (txbuf != -1) {
809 if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
810 /* that was the last segment */
Stephen Hemminger5803c512009-01-09 13:01:08 +0000811 dev->stats.tx_bytes += lp->outgoing.skb->len;
Joe Perches7f5e7602015-05-05 10:05:49 -0700812 if (!lp->outgoing.proto->ack_tx) {
Joe Perchescb334642015-05-05 10:05:47 -0700813 dev_kfree_skb_irq(lp->outgoing.skb);
814 lp->outgoing.proto = NULL;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 lp->next_tx = txbuf;
818 }
819 }
820 /* inform upper layers of idleness, if necessary */
821 if (lp->cur_tx == -1)
822 netif_wake_queue(dev);
823 }
824 /* now process the received packet, if any */
825 if (recbuf != -1) {
Joe Perches72aeea42015-05-05 10:05:54 -0700826 if (BUGLVL(D_RX))
827 arcnet_dump_packet(dev, recbuf, "rx irq", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 arcnet_rx(dev, recbuf);
830 release_arcbuf(dev, recbuf);
831
832 didsomething++;
833 }
834 if (status & lp->intmask & RECONflag) {
Joe Perchese15b0362015-05-05 10:06:12 -0700835 lp->hw.command(dev, CFLAGScmd | CONFIGclear);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000836 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Joe Perchesa34c0932015-05-05 10:05:55 -0700838 arc_printk(D_RECON, dev, "Network reconfiguration detected (status=%Xh)\n",
839 status);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700840 /* MYRECON bit is at bit 7 of diagstatus */
Joe Perchescb334642015-05-05 10:05:47 -0700841 if (diagstatus & 0x80)
Joe Perchesa34c0932015-05-05 10:05:55 -0700842 arc_printk(D_RECON, dev, "Put out that recon myself\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* is the RECON info empty or old? */
845 if (!lp->first_recon || !lp->last_recon ||
S.Caglar Onur9307b572008-03-28 14:41:24 -0700846 time_after(jiffies, lp->last_recon + HZ * 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (lp->network_down)
Joe Perchesa34c0932015-05-05 10:05:55 -0700848 arc_printk(D_NORMAL, dev, "reconfiguration detected: cabling restored?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 lp->first_recon = lp->last_recon = jiffies;
850 lp->num_recons = lp->network_down = 0;
851
Joe Perchesa34c0932015-05-05 10:05:55 -0700852 arc_printk(D_DURING, dev, "recon: clearing counters.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } else { /* add to current RECON counter */
854 lp->last_recon = jiffies;
855 lp->num_recons++;
856
Joe Perchesa34c0932015-05-05 10:05:55 -0700857 arc_printk(D_DURING, dev, "recon: counter=%d, time=%lds, net=%d\n",
858 lp->num_recons,
859 (lp->last_recon - lp->first_recon) / HZ,
860 lp->network_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* if network is marked up;
863 * and first_recon and last_recon are 60+ apart;
864 * and the average no. of recons counted is
865 * > RECON_THRESHOLD/min;
866 * then print a warning message.
867 */
Joe Perches8e95a202009-12-03 07:58:21 +0000868 if (!lp->network_down &&
869 (lp->last_recon - lp->first_recon) <= HZ * 60 &&
870 lp->num_recons >= RECON_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 lp->network_down = 1;
Joe Perchesa34c0932015-05-05 10:05:55 -0700872 arc_printk(D_NORMAL, dev, "many reconfigurations detected: cabling problem?\n");
Joe Perches8e95a202009-12-03 07:58:21 +0000873 } else if (!lp->network_down &&
874 lp->last_recon - lp->first_recon > HZ * 60) {
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700875 /* reset counters if we've gone for
876 * over a minute.
877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 lp->first_recon = lp->last_recon;
879 lp->num_recons = 1;
880 }
881 }
S.Caglar Onur9307b572008-03-28 14:41:24 -0700882 } else if (lp->network_down &&
Joe Perchescb334642015-05-05 10:05:47 -0700883 time_after(jiffies, lp->last_recon + HZ * 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (lp->network_down)
Joe Perchesa34c0932015-05-05 10:05:55 -0700885 arc_printk(D_NORMAL, dev, "cabling restored?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 lp->first_recon = lp->last_recon = 0;
887 lp->num_recons = lp->network_down = 0;
888
Joe Perchesa34c0932015-05-05 10:05:55 -0700889 arc_printk(D_DURING, dev, "not recon: clearing counters anyway.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
Joe Perches7f5e7602015-05-05 10:05:49 -0700892 if (didsomething)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 retval |= IRQ_HANDLED;
Joe Perches7f5e7602015-05-05 10:05:49 -0700894 } while (--boguscount && didsomething);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Joe Perchesa34c0932015-05-05 10:05:55 -0700896 arc_printk(D_DURING, dev, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
Joe Perchese15b0362015-05-05 10:06:12 -0700897 lp->hw.status(dev), boguscount);
Joe Perchesa34c0932015-05-05 10:05:55 -0700898 arc_printk(D_DURING, dev, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Joe Perchese15b0362015-05-05 10:06:12 -0700900 lp->hw.intmask(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 udelay(1);
Joe Perchese15b0362015-05-05 10:06:12 -0700902 lp->hw.intmask(dev, lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 spin_unlock(&lp->lock);
905 return retval;
906}
Joe Perches811eafc2015-05-05 10:05:57 -0700907EXPORT_SYMBOL(arcnet_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Joe Perchesf2f0a162015-05-05 10:05:52 -0700909/* This is a generic packet receiver that calls arcnet??_rx depending on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * protocol ID found.
911 */
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100912static void arcnet_rx(struct net_device *dev, int bufnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Wang Chen454d7c92008-11-12 23:37:49 -0800914 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct archdr pkt;
916 struct arc_rfc1201 *soft;
917 int length, ofs;
918
919 soft = &pkt.soft.rfc1201;
920
Dan Carpenter087d2732013-07-19 08:48:05 +0300921 lp->hw.copy_from_card(dev, bufnum, 0, &pkt, ARC_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (pkt.hard.offset[0]) {
923 ofs = pkt.hard.offset[0];
924 length = 256 - ofs;
925 } else {
926 ofs = pkt.hard.offset[1];
927 length = 512 - ofs;
928 }
929
930 /* get the full header, if possible */
Joe Perches7f5e7602015-05-05 10:05:49 -0700931 if (sizeof(pkt.soft) <= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
Joe Perches7f5e7602015-05-05 10:05:49 -0700933 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 memset(&pkt.soft, 0, sizeof(pkt.soft));
935 lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
936 }
937
Joe Perchesa34c0932015-05-05 10:05:55 -0700938 arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
939 bufnum, pkt.hard.source, pkt.hard.dest, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Stephen Hemminger5803c512009-01-09 13:01:08 +0000941 dev->stats.rx_packets++;
942 dev->stats.rx_bytes += length + ARC_HDR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* call the right receiver for the protocol */
945 if (arc_proto_map[soft->proto]->is_ip) {
Joe Perches72aeea42015-05-05 10:05:54 -0700946 if (BUGLVL(D_PROTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct ArcProto
948 *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
949 *newp = arc_proto_map[soft->proto];
950
951 if (oldp != newp) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700952 arc_printk(D_PROTO, dev,
953 "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
954 soft->proto, pkt.hard.source,
955 newp->suffix, oldp->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 }
958
959 /* broadcasts will always be done with the last-used encap. */
960 lp->default_proto[0] = soft->proto;
961
962 /* in striking contrast, the following isn't a hack. */
963 lp->default_proto[pkt.hard.source] = soft->proto;
964 }
965 /* call the protocol-specific receiver. */
966 arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969static void null_rx(struct net_device *dev, int bufnum,
970 struct archdr *pkthdr, int length)
971{
Joe Perchesa34c0932015-05-05 10:05:55 -0700972 arc_printk(D_PROTO, dev,
973 "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
974 pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static int null_build_header(struct sk_buff *skb, struct net_device *dev,
978 unsigned short type, uint8_t daddr)
979{
Wang Chen454d7c92008-11-12 23:37:49 -0800980 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Joe Perchesa34c0932015-05-05 10:05:55 -0700982 arc_printk(D_PROTO, dev,
983 "tx: can't build header for encap %02Xh; load a protocol driver.\n",
984 lp->default_proto[daddr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /* always fails */
987 return 0;
988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990/* the "do nothing" prepare_tx function warns that there's nothing to do. */
991static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
992 int length, int bufnum)
993{
Wang Chen454d7c92008-11-12 23:37:49 -0800994 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct arc_hardware newpkt;
996
Joe Perchesa34c0932015-05-05 10:05:55 -0700997 arc_printk(D_PROTO, dev, "tx: no encap for this host; load a protocol driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /* send a packet to myself -- will never get received, of course */
1000 newpkt.source = newpkt.dest = dev->dev_addr[0];
1001
1002 /* only one byte of actual data (and it's random) */
1003 newpkt.offset[0] = 0xFF;
1004
1005 lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1006
1007 return 1; /* done */
1008}