blob: 2be8ab66d06718410a768395fa7ff3c3dd1dbd5b [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>
53#include <linux/arcdevice.h>
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -080054#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* "do nothing" functions for protocol drivers */
57static void null_rx(struct net_device *dev, int bufnum,
58 struct archdr *pkthdr, int length);
59static int null_build_header(struct sk_buff *skb, struct net_device *dev,
60 unsigned short type, uint8_t daddr);
61static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
62 int length, int bufnum);
63
Adrian Bunkf03aa2d2006-01-14 03:10:22 +010064static void arcnet_rx(struct net_device *dev, int bufnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Joe Perchesf2f0a162015-05-05 10:05:52 -070066/* one ArcProto per possible proto ID. None of the elements of
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * arc_proto_map are allowed to be NULL; they will get set to
68 * arc_proto_default instead. It also must not be NULL; if you would like
69 * to set it to NULL, set it to &arc_proto_null instead.
70 */
Joe Perchescb334642015-05-05 10:05:47 -070071struct ArcProto *arc_proto_map[256], *arc_proto_default,
72 *arc_bcast_proto, *arc_raw_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Joe Perches7f5e7602015-05-05 10:05:49 -070074static struct ArcProto arc_proto_null = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .suffix = '?',
76 .mtu = XMTU,
77 .is_ip = 0,
78 .rx = null_rx,
79 .build_header = null_build_header,
80 .prepare_tx = null_prepare_tx,
81 .continue_tx = NULL,
82 .ack_tx = NULL
83};
84
85/* Exported function prototypes */
86int arcnet_debug = ARCNET_DEBUG;
87
88EXPORT_SYMBOL(arc_proto_map);
89EXPORT_SYMBOL(arc_proto_default);
90EXPORT_SYMBOL(arc_bcast_proto);
91EXPORT_SYMBOL(arc_raw_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092EXPORT_SYMBOL(arcnet_unregister_proto);
93EXPORT_SYMBOL(arcnet_debug);
94EXPORT_SYMBOL(alloc_arcdev);
95EXPORT_SYMBOL(arcnet_interrupt);
Stephen Hemmingerbca5b892009-01-09 13:01:09 +000096EXPORT_SYMBOL(arcnet_open);
97EXPORT_SYMBOL(arcnet_close);
98EXPORT_SYMBOL(arcnet_send_packet);
99EXPORT_SYMBOL(arcnet_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/* Internal function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700103 unsigned short type, const void *daddr,
104 const void *saddr, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int go_tx(struct net_device *dev);
106
107static int debug = ARCNET_DEBUG;
108module_param(debug, int, 0);
109MODULE_LICENSE("GPL");
110
111static int __init arcnet_init(void)
112{
113 int count;
114
115 arcnet_debug = debug;
116
Joe Perches05a24b22015-05-05 10:05:56 -0700117 pr_info("arcnet loaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /* initialize the protocol map */
120 arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
121 for (count = 0; count < 256; count++)
122 arc_proto_map[count] = arc_proto_default;
123
Joe Perches72aeea42015-05-05 10:05:54 -0700124 if (BUGLVL(D_DURING))
Joe Perches05a24b22015-05-05 10:05:56 -0700125 pr_info("struct sizes: %Zd %Zd %Zd %Zd %Zd\n",
126 sizeof(struct arc_hardware),
127 sizeof(struct arc_rfc1201),
128 sizeof(struct arc_rfc1051),
129 sizeof(struct arc_eth_encap),
130 sizeof(struct archdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 return 0;
133}
134
135static void __exit arcnet_exit(void)
136{
137}
138
139module_init(arcnet_init);
140module_exit(arcnet_exit);
141
Joe Perchesf2f0a162015-05-05 10:05:52 -0700142/* Dump the contents of an sk_buff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#if ARCNET_DEBUG_MAX & D_SKB
144void arcnet_dump_skb(struct net_device *dev,
145 struct sk_buff *skb, char *desc)
146{
Joe Perchesad361c92009-07-06 13:05:40 -0700147 char hdr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Joe Perchesad361c92009-07-06 13:05:40 -0700149 /* dump the packet */
150 snprintf(hdr, sizeof(hdr), "%6s:%s skb->data:", dev->name, desc);
151 print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
152 16, 1, skb->data, skb->len, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155EXPORT_SYMBOL(arcnet_dump_skb);
156#endif
157
Joe Perchesf2f0a162015-05-05 10:05:52 -0700158/* Dump the contents of an ARCnet buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100160static void arcnet_dump_packet(struct net_device *dev, int bufnum,
161 char *desc, int take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Wang Chen454d7c92008-11-12 23:37:49 -0800163 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int i, length;
165 unsigned long flags = 0;
166 static uint8_t buf[512];
Joe Perchesad361c92009-07-06 13:05:40 -0700167 char hdr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /* hw.copy_from_card expects IRQ context so take the IRQ lock
Joe Perchesf2f0a162015-05-05 10:05:52 -0700170 * to keep it single threaded
171 */
Joe Perchescb334642015-05-05 10:05:47 -0700172 if (take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 spin_lock_irqsave(&lp->lock, flags);
174
175 lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
Joe Perchescb334642015-05-05 10:05:47 -0700176 if (take_arcnet_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 spin_unlock_irqrestore(&lp->lock, flags);
178
179 /* if the offset[0] byte is nonzero, this is a 256-byte packet */
180 length = (buf[2] ? 256 : 512);
181
Joe Perchesad361c92009-07-06 13:05:40 -0700182 /* dump the packet */
183 snprintf(hdr, sizeof(hdr), "%6s:%s packet dump:", dev->name, desc);
184 print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
185 16, 1, buf, length, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100188#else
189
Joe Perchescb334642015-05-05 10:05:47 -0700190#define arcnet_dump_packet(dev, bufnum, desc, take_arcnet_lock) do { } while (0)
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#endif
193
Joe Perchesf2f0a162015-05-05 10:05:52 -0700194/* Unregister a protocol driver from the arc_proto_map. Protocol drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * are responsible for registering themselves, but the unregister routine
196 * is pretty generic so we'll do it here.
197 */
198void arcnet_unregister_proto(struct ArcProto *proto)
199{
200 int count;
201
202 if (arc_proto_default == proto)
203 arc_proto_default = &arc_proto_null;
204 if (arc_bcast_proto == proto)
205 arc_bcast_proto = arc_proto_default;
206 if (arc_raw_proto == proto)
207 arc_raw_proto = arc_proto_default;
208
209 for (count = 0; count < 256; count++) {
210 if (arc_proto_map[count] == proto)
211 arc_proto_map[count] = arc_proto_default;
212 }
213}
214
Joe Perchesf2f0a162015-05-05 10:05:52 -0700215/* Add a buffer to the queue. Only the interrupt handler is allowed to do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * this, unless interrupts are disabled.
Joe Perchescb334642015-05-05 10:05:47 -0700217 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Note: we don't check for a full queue, since there aren't enough buffers
219 * to more than fill it.
220 */
221static void release_arcbuf(struct net_device *dev, int bufnum)
222{
Wang Chen454d7c92008-11-12 23:37:49 -0800223 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int i;
225
226 lp->buf_queue[lp->first_free_buf++] = bufnum;
227 lp->first_free_buf %= 5;
228
Joe Perches72aeea42015-05-05 10:05:54 -0700229 if (BUGLVL(D_DURING)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700230 arc_printk(D_DURING, dev, "release_arcbuf: freed #%d; buffer queue is now: ",
231 bufnum);
Joe Perchescb334642015-05-05 10:05:47 -0700232 for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
Joe Perchesa34c0932015-05-05 10:05:55 -0700233 arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
234 arc_cont(D_DURING, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236}
237
Joe Perchesf2f0a162015-05-05 10:05:52 -0700238/* Get a buffer from the queue.
239 * If this returns -1, there are no buffers available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241static int get_arcbuf(struct net_device *dev)
242{
Wang Chen454d7c92008-11-12 23:37:49 -0800243 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 int buf = -1, i;
245
246 if (!atomic_dec_and_test(&lp->buf_lock)) {
247 /* already in this function */
Joe Perchesa34c0932015-05-05 10:05:55 -0700248 arc_printk(D_NORMAL, dev, "get_arcbuf: overlap (%d)!\n",
249 lp->buf_lock.counter);
Joe Perches7f5e7602015-05-05 10:05:49 -0700250 } else { /* we can continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (lp->next_buf >= 5)
252 lp->next_buf -= 5;
253
Joe Perches7f5e7602015-05-05 10:05:49 -0700254 if (lp->next_buf == lp->first_free_buf) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700255 arc_printk(D_NORMAL, dev, "get_arcbuf: BUG: no buffers are available??\n");
Joe Perches7f5e7602015-05-05 10:05:49 -0700256 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 buf = lp->buf_queue[lp->next_buf++];
258 lp->next_buf %= 5;
259 }
260 }
261
Joe Perches72aeea42015-05-05 10:05:54 -0700262 if (BUGLVL(D_DURING)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700263 arc_printk(D_DURING, dev, "get_arcbuf: got #%d; buffer queue is now: ",
264 buf);
Joe Perchescb334642015-05-05 10:05:47 -0700265 for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
Joe Perchesa34c0932015-05-05 10:05:55 -0700266 arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
267 arc_cont(D_DURING, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
270 atomic_inc(&lp->buf_lock);
271 return buf;
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static int choose_mtu(void)
275{
276 int count, mtu = 65535;
277
278 /* choose the smallest MTU of all available encaps */
279 for (count = 0; count < 256; count++) {
Joe Perches8e95a202009-12-03 07:58:21 +0000280 if (arc_proto_map[count] != &arc_proto_null &&
281 arc_proto_map[count]->mtu < mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 mtu = arc_proto_map[count]->mtu;
283 }
284 }
285
286 return mtu == 65535 ? XMTU : mtu;
287}
288
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700289static const struct header_ops arcnet_header_ops = {
290 .create = arcnet_header,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700291};
292
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000293static const struct net_device_ops arcnet_netdev_ops = {
294 .ndo_open = arcnet_open,
295 .ndo_stop = arcnet_close,
296 .ndo_start_xmit = arcnet_send_packet,
297 .ndo_tx_timeout = arcnet_timeout,
298};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300/* Setup a struct device for ARCnet. */
301static void arcdev_setup(struct net_device *dev)
302{
303 dev->type = ARPHRD_ARCNET;
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000304 dev->netdev_ops = &arcnet_netdev_ops;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700305 dev->header_ops = &arcnet_header_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 dev->hard_header_len = sizeof(struct archdr);
307 dev->mtu = choose_mtu();
308
309 dev->addr_len = ARCNET_ALEN;
310 dev->tx_queue_len = 100;
311 dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
312 dev->watchdog_timeo = TX_TIMEOUT;
313
314 /* New-style flags. */
315 dev->flags = IFF_BROADCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000318struct net_device *alloc_arcdev(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct net_device *dev;
321
322 dev = alloc_netdev(sizeof(struct arcnet_local),
Tom Gundersenc835a672014-07-14 16:37:24 +0200323 name && *name ? name : "arc%d", NET_NAME_UNKNOWN,
324 arcdev_setup);
Joe Perchescb334642015-05-05 10:05:47 -0700325 if (dev) {
Wang Chen454d7c92008-11-12 23:37:49 -0800326 struct arcnet_local *lp = netdev_priv(dev);
Joe Perches01a1d5a2015-05-05 10:05:48 -0700327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 spin_lock_init(&lp->lock);
329 }
330
331 return dev;
332}
333
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;
365 if (ARCRESET(0) && ARCRESET(1))
366 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__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (ASTATUS() & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700409 arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
410 __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 ACOMMAND(CFLAGScmd | RESETclear);
412 }
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. */
416 AINTMASK(0);
417 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;
423 AINTMASK(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}
434
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 */
443 AINTMASK(0);
444 ACOMMAND(NOTXcmd); /* stop transmit */
445 ACOMMAND(NORXcmd); /* disable receive */
446 mdelay(1);
447
448 /* shut down the card */
449 lp->hw.close(dev);
450 module_put(lp->hw.owner);
451 return 0;
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700455 unsigned short type, const void *daddr,
456 const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700458 const struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 uint8_t _daddr, proto_num;
460 struct ArcProto *proto;
461
Joe Perchesa34c0932015-05-05 10:05:55 -0700462 arc_printk(D_DURING, dev,
463 "create header from %d to %d; protocol %d (%Xh); size %u.\n",
464 saddr ? *(uint8_t *)saddr : -1,
465 daddr ? *(uint8_t *)daddr : -1,
466 type, type, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Joe Perchescb334642015-05-05 10:05:47 -0700468 if (skb->len != 0 && len != skb->len)
Joe Perchesa34c0932015-05-05 10:05:55 -0700469 arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
470 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Joe Perchescb334642015-05-05 10:05:47 -0700472 /* Type is host order - ? */
473 if (type == ETH_P_ARCNET) {
474 proto = arc_raw_proto;
Joe Perchesa34c0932015-05-05 10:05:55 -0700475 arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n",
476 proto->suffix);
Joe Perchescb334642015-05-05 10:05:47 -0700477 _daddr = daddr ? *(uint8_t *)daddr : 0;
Joe Perches7f5e7602015-05-05 10:05:49 -0700478 } else if (!daddr) {
Joe Perchesf2f0a162015-05-05 10:05:52 -0700479 /* if the dest addr isn't provided, we can't choose an
480 * encapsulation! Store the packet type (eg. ETH_P_IP)
481 * for now, and we'll push on a real header when we do
482 * rebuild_header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Joe Perchescb334642015-05-05 10:05:47 -0700484 *(uint16_t *)skb_push(skb, 2) = type;
Joe Perchesf2f0a162015-05-05 10:05:52 -0700485 /* XXX: Why not use skb->mac_len? */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700486 if (skb->network_header - skb->mac_header != 2)
Joe Perchesa34c0932015-05-05 10:05:55 -0700487 arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! diff (%u) is not 2!\n",
488 skb->network_header - skb->mac_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -2; /* return error -- can't transmit yet! */
Joe Perches7f5e7602015-05-05 10:05:49 -0700490 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* otherwise, we can just add the header as usual. */
Joe Perchescb334642015-05-05 10:05:47 -0700492 _daddr = *(uint8_t *)daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 proto_num = lp->default_proto[_daddr];
494 proto = arc_proto_map[proto_num];
Joe Perchesa34c0932015-05-05 10:05:55 -0700495 arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n",
496 proto_num, proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (proto == &arc_proto_null && arc_bcast_proto != proto) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700498 arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n",
499 arc_bcast_proto->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 proto = arc_bcast_proto;
501 }
502 }
503 return proto->build_header(skb, dev, type, _daddr);
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/* Called by the kernel in order to transmit a packet. */
Stephen Hemminger613573252009-08-31 19:50:58 +0000507netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
Joe Perchescb334642015-05-05 10:05:47 -0700508 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Wang Chen454d7c92008-11-12 23:37:49 -0800510 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct archdr *pkt;
512 struct arc_rfc1201 *soft;
513 struct ArcProto *proto;
514 int txbuf;
515 unsigned long flags;
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700516 int freeskb, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Joe Perchesa34c0932015-05-05 10:05:55 -0700518 arc_printk(D_DURING, dev,
519 "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
520 ASTATUS(), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Joe Perchescb334642015-05-05 10:05:47 -0700522 pkt = (struct archdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 soft = &pkt->soft.rfc1201;
524 proto = arc_proto_map[soft->proto];
525
Joe Perchesa34c0932015-05-05 10:05:55 -0700526 arc_printk(D_SKB_SIZE, dev, "skb: transmitting %d bytes to %02X\n",
527 skb->len, pkt->hard.dest);
Joe Perches72aeea42015-05-05 10:05:54 -0700528 if (BUGLVL(D_SKB))
529 arcnet_dump_skb(dev, skb, "tx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* fits in one packet? */
532 if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700533 arc_printk(D_NORMAL, dev, "fixme: packet too large: compensating badly!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 dev_kfree_skb(skb);
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700535 return NETDEV_TX_OK; /* don't try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
538 /* We're busy transmitting a packet... */
539 netif_stop_queue(dev);
540
541 spin_lock_irqsave(&lp->lock, flags);
542 AINTMASK(0);
Joe Perchescb334642015-05-05 10:05:47 -0700543 if (lp->next_tx == -1)
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700544 txbuf = get_arcbuf(dev);
Joe Perches7f5e7602015-05-05 10:05:49 -0700545 else
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700546 txbuf = -1;
Joe Perches7f5e7602015-05-05 10:05:49 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (txbuf != -1) {
549 if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
550 !proto->ack_tx) {
551 /* done right away and we don't want to acknowledge
Joe Perchesf2f0a162015-05-05 10:05:52 -0700552 * the package later - forget about it now
553 */
Stephen Hemminger5803c512009-01-09 13:01:08 +0000554 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 freeskb = 1;
556 } else {
557 /* do it the 'split' way */
558 lp->outgoing.proto = proto;
559 lp->outgoing.skb = skb;
560 lp->outgoing.pkt = pkt;
561
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700562 freeskb = 0;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 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;
575 freeskb = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
Joe Perchesa34c0932015-05-05 10:05:55 -0700578 arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
579 __FILE__, __LINE__, __func__, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* make sure we didn't ignore a TX IRQ while we were in here */
581 AINTMASK(0);
582
Joe Perchesa34c0932015-05-05 10:05:55 -0700583 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Joe Perchescb334642015-05-05 10:05:47 -0700584 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 AINTMASK(lp->intmask);
Joe Perchesa34c0932015-05-05 10:05:55 -0700586 arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
587 __FILE__, __LINE__, __func__, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 spin_unlock_irqrestore(&lp->lock, flags);
Joe Perches7f5e7602015-05-05 10:05:49 -0700590 if (freeskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 dev_kfree_skb(skb);
Joe Perches7f5e7602015-05-05 10:05:49 -0700592
Pieter Dejaegherec6bb15a2005-09-06 19:54:48 -0700593 return retval; /* no need to try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Joe Perchesf2f0a162015-05-05 10:05:52 -0700596/* Actually start transmitting a packet that was loaded into a buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * by prepare_tx. This should _only_ be called by the interrupt handler.
598 */
599static int go_tx(struct net_device *dev)
600{
Wang Chen454d7c92008-11-12 23:37:49 -0800601 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Joe Perchesa34c0932015-05-05 10:05:55 -0700603 arc_printk(D_DURING, dev, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
604 ASTATUS(), lp->intmask, lp->next_tx, lp->cur_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (lp->cur_tx != -1 || lp->next_tx == -1)
607 return 0;
608
Joe Perches72aeea42015-05-05 10:05:54 -0700609 if (BUGLVL(D_TX))
610 arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 lp->cur_tx = lp->next_tx;
613 lp->next_tx = -1;
614
615 /* start sending */
616 ACOMMAND(TXcmd | (lp->cur_tx << 3));
617
Stephen Hemminger5803c512009-01-09 13:01:08 +0000618 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 lp->lasttrans_dest = lp->lastload_dest;
620 lp->lastload_dest = 0;
621 lp->excnak_pending = 0;
Joe Perchescb334642015-05-05 10:05:47 -0700622 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 return 1;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/* Called by the kernel when transmit times out */
Stephen Hemmingerbca5b892009-01-09 13:01:09 +0000628void arcnet_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 unsigned long flags;
Wang Chen454d7c92008-11-12 23:37:49 -0800631 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int status = ASTATUS();
633 char *msg;
634
635 spin_lock_irqsave(&lp->lock, flags);
636 if (status & TXFREEflag) { /* transmit _DID_ finish */
637 msg = " - missed IRQ?";
638 } else {
639 msg = "";
Stephen Hemminger5803c512009-01-09 13:01:08 +0000640 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 lp->timed_out = 1;
642 ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
643 }
Stephen Hemminger5803c512009-01-09 13:01:08 +0000644 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* make sure we didn't miss a TX or a EXC NAK IRQ */
647 AINTMASK(0);
Joe Perchescb334642015-05-05 10:05:47 -0700648 lp->intmask |= TXFREEflag | EXCNAKflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 spin_unlock_irqrestore(&lp->lock, flags);
652
Joe Perchescb334642015-05-05 10:05:47 -0700653 if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700654 arc_printk(D_EXTRA, dev, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
655 msg, status, lp->intmask, lp->lasttrans_dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 lp->last_timeout = jiffies;
657 }
658
659 if (lp->cur_tx == -1)
660 netif_wake_queue(dev);
661}
662
Joe Perchesf2f0a162015-05-05 10:05:52 -0700663/* The typical workload of the driver: Handle the network interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 * interrupts. Establish which device needs attention, and call the correct
665 * chipset interrupt handler.
666 */
David Howells7d12e782006-10-05 14:55:46 +0100667irqreturn_t arcnet_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct net_device *dev = dev_id;
670 struct arcnet_local *lp;
671 int recbuf, status, diagstatus, didsomething, boguscount;
672 int retval = IRQ_NONE;
673
Joe Perchesa34c0932015-05-05 10:05:55 -0700674 arc_printk(D_DURING, dev, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Joe Perchesa34c0932015-05-05 10:05:55 -0700676 arc_printk(D_DURING, dev, "in arcnet_interrupt\n");
Wang Chen454d7c92008-11-12 23:37:49 -0800677
678 lp = netdev_priv(dev);
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200679 BUG_ON(!lp);
Joe Perchescb334642015-05-05 10:05:47 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_lock(&lp->lock);
682
Joe Perchesf2f0a162015-05-05 10:05:52 -0700683 /* RESET flag was enabled - if device is not running, we must
684 * clear it right away (but nothing else).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
686 if (!netif_running(dev)) {
687 if (ASTATUS() & RESETflag)
688 ACOMMAND(CFLAGScmd | RESETclear);
689 AINTMASK(0);
690 spin_unlock(&lp->lock);
Michael Grzeschik226ee672014-09-29 11:55:34 +0200691 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
Joe Perchesa34c0932015-05-05 10:05:55 -0700694 arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
695 ASTATUS(), lp->intmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 boguscount = 5;
698 do {
699 status = ASTATUS();
Joe Perchescb334642015-05-05 10:05:47 -0700700 diagstatus = (status >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Joe Perchesa34c0932015-05-05 10:05:55 -0700702 arc_printk(D_DEBUG, dev, "%s: %d: %s: status=%x\n",
703 __FILE__, __LINE__, __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 didsomething = 0;
705
Joe Perchesf2f0a162015-05-05 10:05:52 -0700706 /* RESET flag was enabled - card is resetting and if RX is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * disabled, it's NOT because we just got a packet.
Joe Perchescb334642015-05-05 10:05:47 -0700708 *
Joe Perchesf2f0a162015-05-05 10:05:52 -0700709 * The card is in an undefined state.
710 * Clear it out and start over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
712 if (status & RESETflag) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700713 arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n",
714 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 arcnet_close(dev);
716 arcnet_open(dev);
717
718 /* get out of the interrupt handler! */
719 break;
720 }
Joe Perchesf2f0a162015-05-05 10:05:52 -0700721 /* RX is inhibited - we must have received something.
722 * Prepare to receive into the next buffer.
Joe Perchescb334642015-05-05 10:05:47 -0700723 *
Joe Perchesf2f0a162015-05-05 10:05:52 -0700724 * We don't actually copy the received packet from the card
725 * until after the transmit handler runs (and possibly
726 * launches the next tx); this should improve latency slightly
727 * if we get both types of interrupts at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
729 recbuf = -1;
730 if (status & lp->intmask & NORXflag) {
731 recbuf = lp->cur_rx;
Joe Perchesa34c0932015-05-05 10:05:55 -0700732 arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n",
733 recbuf, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 lp->cur_rx = get_arcbuf(dev);
736 if (lp->cur_rx != -1) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700737 arc_printk(D_DURING, dev, "enabling receive to buffer #%d\n",
738 lp->cur_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
740 }
741 didsomething++;
742 }
743
Joe Perchescb334642015-05-05 10:05:47 -0700744 if ((diagstatus & EXCNAKflag)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700745 arc_printk(D_DURING, dev, "EXCNAK IRQ (diagstat=%Xh)\n",
746 diagstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Joe Perchescb334642015-05-05 10:05:47 -0700748 ACOMMAND(NOTXcmd); /* disable transmit */
749 lp->excnak_pending = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Joe Perchescb334642015-05-05 10:05:47 -0700751 ACOMMAND(EXCNAKclear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 lp->intmask &= ~(EXCNAKflag);
Joe Perchescb334642015-05-05 10:05:47 -0700753 didsomething++;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* a transmit finished, and we're interested in it. */
757 if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700758 lp->intmask &= ~(TXFREEflag | EXCNAKflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Joe Perchesa34c0932015-05-05 10:05:55 -0700760 arc_printk(D_DURING, dev, "TX IRQ (stat=%Xh)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (lp->cur_tx != -1 && !lp->timed_out) {
Joe Perchescb334642015-05-05 10:05:47 -0700763 if (!(status & TXACKflag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (lp->lasttrans_dest != 0) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700765 arc_printk(D_EXTRA, dev,
766 "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
767 status,
768 lp->lasttrans_dest);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000769 dev->stats.tx_errors++;
770 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 } else {
Joe Perchesa34c0932015-05-05 10:05:55 -0700772 arc_printk(D_DURING, dev,
773 "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
774 status,
775 lp->lasttrans_dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 }
778
779 if (lp->outgoing.proto &&
780 lp->outgoing.proto->ack_tx) {
Joe Perchescb334642015-05-05 10:05:47 -0700781 int ackstatus;
Joe Perches01a1d5a2015-05-05 10:05:48 -0700782
Joe Perchescb334642015-05-05 10:05:47 -0700783 if (status & TXACKflag)
784 ackstatus = 2;
785 else if (lp->excnak_pending)
786 ackstatus = 1;
787 else
788 ackstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Joe Perchescb334642015-05-05 10:05:47 -0700790 lp->outgoing.proto
791 ->ack_tx(dev, ackstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 }
794 if (lp->cur_tx != -1)
795 release_arcbuf(dev, lp->cur_tx);
796
797 lp->cur_tx = -1;
798 lp->timed_out = 0;
799 didsomething++;
800
801 /* send another packet if there is one */
802 go_tx(dev);
803
804 /* continue a split packet, if any */
805 if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
806 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) {
835 ACOMMAND(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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* reset counters if we've gone for over a minute. */
876 lp->first_recon = lp->last_recon;
877 lp->num_recons = 1;
878 }
879 }
S.Caglar Onur9307b572008-03-28 14:41:24 -0700880 } else if (lp->network_down &&
Joe Perchescb334642015-05-05 10:05:47 -0700881 time_after(jiffies, lp->last_recon + HZ * 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (lp->network_down)
Joe Perchesa34c0932015-05-05 10:05:55 -0700883 arc_printk(D_NORMAL, dev, "cabling restored?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 lp->first_recon = lp->last_recon = 0;
885 lp->num_recons = lp->network_down = 0;
886
Joe Perchesa34c0932015-05-05 10:05:55 -0700887 arc_printk(D_DURING, dev, "not recon: clearing counters anyway.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889
Joe Perches7f5e7602015-05-05 10:05:49 -0700890 if (didsomething)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 retval |= IRQ_HANDLED;
Joe Perches7f5e7602015-05-05 10:05:49 -0700892 } while (--boguscount && didsomething);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Joe Perchesa34c0932015-05-05 10:05:55 -0700894 arc_printk(D_DURING, dev, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
895 ASTATUS(), boguscount);
896 arc_printk(D_DURING, dev, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 AINTMASK(0);
899 udelay(1);
900 AINTMASK(lp->intmask);
Joe Perchescb334642015-05-05 10:05:47 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_unlock(&lp->lock);
903 return retval;
904}
905
Joe Perchesf2f0a162015-05-05 10:05:52 -0700906/* This is a generic packet receiver that calls arcnet??_rx depending on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * protocol ID found.
908 */
Adrian Bunkf03aa2d2006-01-14 03:10:22 +0100909static void arcnet_rx(struct net_device *dev, int bufnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Wang Chen454d7c92008-11-12 23:37:49 -0800911 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct archdr pkt;
913 struct arc_rfc1201 *soft;
914 int length, ofs;
915
916 soft = &pkt.soft.rfc1201;
917
Dan Carpenter087d2732013-07-19 08:48:05 +0300918 lp->hw.copy_from_card(dev, bufnum, 0, &pkt, ARC_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (pkt.hard.offset[0]) {
920 ofs = pkt.hard.offset[0];
921 length = 256 - ofs;
922 } else {
923 ofs = pkt.hard.offset[1];
924 length = 512 - ofs;
925 }
926
927 /* get the full header, if possible */
Joe Perches7f5e7602015-05-05 10:05:49 -0700928 if (sizeof(pkt.soft) <= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
Joe Perches7f5e7602015-05-05 10:05:49 -0700930 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 memset(&pkt.soft, 0, sizeof(pkt.soft));
932 lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
933 }
934
Joe Perchesa34c0932015-05-05 10:05:55 -0700935 arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
936 bufnum, pkt.hard.source, pkt.hard.dest, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Stephen Hemminger5803c512009-01-09 13:01:08 +0000938 dev->stats.rx_packets++;
939 dev->stats.rx_bytes += length + ARC_HDR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 /* call the right receiver for the protocol */
942 if (arc_proto_map[soft->proto]->is_ip) {
Joe Perches72aeea42015-05-05 10:05:54 -0700943 if (BUGLVL(D_PROTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct ArcProto
945 *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
946 *newp = arc_proto_map[soft->proto];
947
948 if (oldp != newp) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700949 arc_printk(D_PROTO, dev,
950 "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
951 soft->proto, pkt.hard.source,
952 newp->suffix, oldp->suffix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 }
955
956 /* broadcasts will always be done with the last-used encap. */
957 lp->default_proto[0] = soft->proto;
958
959 /* in striking contrast, the following isn't a hack. */
960 lp->default_proto[pkt.hard.source] = soft->proto;
961 }
962 /* call the protocol-specific receiver. */
963 arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966static void null_rx(struct net_device *dev, int bufnum,
967 struct archdr *pkthdr, int length)
968{
Joe Perchesa34c0932015-05-05 10:05:55 -0700969 arc_printk(D_PROTO, dev,
970 "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
971 pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static int null_build_header(struct sk_buff *skb, struct net_device *dev,
975 unsigned short type, uint8_t daddr)
976{
Wang Chen454d7c92008-11-12 23:37:49 -0800977 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Joe Perchesa34c0932015-05-05 10:05:55 -0700979 arc_printk(D_PROTO, dev,
980 "tx: can't build header for encap %02Xh; load a protocol driver.\n",
981 lp->default_proto[daddr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /* always fails */
984 return 0;
985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/* the "do nothing" prepare_tx function warns that there's nothing to do. */
988static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
989 int length, int bufnum)
990{
Wang Chen454d7c92008-11-12 23:37:49 -0800991 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 struct arc_hardware newpkt;
993
Joe Perchesa34c0932015-05-05 10:05:55 -0700994 arc_printk(D_PROTO, dev, "tx: no encap for this host; load a protocol driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /* send a packet to myself -- will never get received, of course */
997 newpkt.source = newpkt.dest = dev->dev_addr[0];
998
999 /* only one byte of actual data (and it's random) */
1000 newpkt.offset[0] = 0xFF;
1001
1002 lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
1003
1004 return 1; /* done */
1005}