11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux ARCnet driver - device-independent routines 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written 1997 by David Woodhouse. 51da177e4SLinus Torvalds * Written 1994-1999 by Avery Pennarun. 61da177e4SLinus Torvalds * Written 1999-2000 by Martin Mares <mj@ucw.cz>. 71da177e4SLinus Torvalds * Derived from skeleton.c by Donald Becker. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com) 101da177e4SLinus Torvalds * for sponsoring the further development of this driver. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * ********************** 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * The original copyright was as follows: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * skeleton.c Written 1993 by Donald Becker. 171da177e4SLinus Torvalds * Copyright 1993 United States Government as represented by the 181da177e4SLinus Torvalds * Director, National Security Agency. This software may only be used 191da177e4SLinus Torvalds * and distributed according to the terms of the GNU General Public License as 201da177e4SLinus Torvalds * modified by SRC, incorporated herein by reference. 211da177e4SLinus Torvalds * 221da177e4SLinus Torvalds * ********************** 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * The change log is now in a file called ChangeLog in this directory. 251da177e4SLinus Torvalds * 261da177e4SLinus Torvalds * Sources: 271da177e4SLinus Torvalds * - Crynwr arcnet.com/arcether.com packet drivers. 281da177e4SLinus Torvalds * - arcnet.c v0.00 dated 1/1/94 and apparently by 291da177e4SLinus Torvalds * Donald Becker - it didn't work :) 301da177e4SLinus Torvalds * - skeleton.c v0.05 dated 11/16/93 by Donald Becker 311da177e4SLinus Torvalds * (from Linux Kernel 1.1.45) 321da177e4SLinus Torvalds * - RFC's 1201 and 1051 - re: TCP/IP over ARCnet 331da177e4SLinus Torvalds * - The official ARCnet COM9026 data sheets (!) thanks to 341da177e4SLinus Torvalds * Ken Cornetet <kcornete@nyx10.cs.du.edu> 351da177e4SLinus Torvalds * - The official ARCnet COM20020 data sheets. 361da177e4SLinus Torvalds * - Information on some more obscure ARCnet controller chips, thanks 371da177e4SLinus Torvalds * to the nice people at SMSC. 381da177e4SLinus Torvalds * - net/inet/eth.c (from kernel 1.1.50) for header-building info. 391da177e4SLinus Torvalds * - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su> 401da177e4SLinus Torvalds * - Textual information and more alternate source from Joachim Koenig 411da177e4SLinus Torvalds * <jojo@repas.de> 421da177e4SLinus Torvalds */ 431da177e4SLinus Torvalds 4405a24b23SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #include <linux/module.h> 471da177e4SLinus Torvalds #include <linux/types.h> 481da177e4SLinus Torvalds #include <linux/delay.h> 491da177e4SLinus Torvalds #include <linux/netdevice.h> 501da177e4SLinus Torvalds #include <linux/if_arp.h> 511da177e4SLinus Torvalds #include <net/arp.h> 521da177e4SLinus Torvalds #include <linux/init.h> 53ff5688aeSMarcelo Feitoza Parisi #include <linux/jiffies.h> 5405fcd31cSMichael Grzeschik #include <linux/errqueue.h> 551da177e4SLinus Torvalds 568890624aSMichael Grzeschik #include <linux/leds.h> 578890624aSMichael Grzeschik 5826c6d281SJoe Perches #include "arcdevice.h" 598e0f295eSJoe Perches #include "com9026.h" 6026c6d281SJoe Perches 611da177e4SLinus Torvalds /* "do nothing" functions for protocol drivers */ 621da177e4SLinus Torvalds static void null_rx(struct net_device *dev, int bufnum, 631da177e4SLinus Torvalds struct archdr *pkthdr, int length); 641da177e4SLinus Torvalds static int null_build_header(struct sk_buff *skb, struct net_device *dev, 651da177e4SLinus Torvalds unsigned short type, uint8_t daddr); 661da177e4SLinus Torvalds static int null_prepare_tx(struct net_device *dev, struct archdr *pkt, 671da177e4SLinus Torvalds int length, int bufnum); 681da177e4SLinus Torvalds 69f03aa2d8SAdrian Bunk static void arcnet_rx(struct net_device *dev, int bufnum); 701da177e4SLinus Torvalds 71f2f0a16bSJoe Perches /* one ArcProto per possible proto ID. None of the elements of 721da177e4SLinus Torvalds * arc_proto_map are allowed to be NULL; they will get set to 731da177e4SLinus Torvalds * arc_proto_default instead. It also must not be NULL; if you would like 741da177e4SLinus Torvalds * to set it to NULL, set it to &arc_proto_null instead. 751da177e4SLinus Torvalds */ 76811eafc0SJoe Perches struct ArcProto *arc_proto_map[256]; 77811eafc0SJoe Perches EXPORT_SYMBOL(arc_proto_map); 78811eafc0SJoe Perches 79811eafc0SJoe Perches struct ArcProto *arc_proto_default; 80811eafc0SJoe Perches EXPORT_SYMBOL(arc_proto_default); 81811eafc0SJoe Perches 82811eafc0SJoe Perches struct ArcProto *arc_bcast_proto; 83811eafc0SJoe Perches EXPORT_SYMBOL(arc_bcast_proto); 84811eafc0SJoe Perches 85811eafc0SJoe Perches struct ArcProto *arc_raw_proto; 86811eafc0SJoe Perches EXPORT_SYMBOL(arc_raw_proto); 871da177e4SLinus Torvalds 887f5e760cSJoe Perches static struct ArcProto arc_proto_null = { 891da177e4SLinus Torvalds .suffix = '?', 901da177e4SLinus Torvalds .mtu = XMTU, 911da177e4SLinus Torvalds .is_ip = 0, 921da177e4SLinus Torvalds .rx = null_rx, 931da177e4SLinus Torvalds .build_header = null_build_header, 941da177e4SLinus Torvalds .prepare_tx = null_prepare_tx, 951da177e4SLinus Torvalds .continue_tx = NULL, 961da177e4SLinus Torvalds .ack_tx = NULL 971da177e4SLinus Torvalds }; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* Exported function prototypes */ 1001da177e4SLinus Torvalds int arcnet_debug = ARCNET_DEBUG; 1011da177e4SLinus Torvalds EXPORT_SYMBOL(arcnet_debug); 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* Internal function prototypes */ 1041da177e4SLinus Torvalds static int arcnet_header(struct sk_buff *skb, struct net_device *dev, 1053b04dddeSStephen Hemminger unsigned short type, const void *daddr, 1063b04dddeSStephen Hemminger const void *saddr, unsigned len); 1071da177e4SLinus Torvalds static int go_tx(struct net_device *dev); 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds static int debug = ARCNET_DEBUG; 1101da177e4SLinus Torvalds module_param(debug, int, 0); 1111da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds static int __init arcnet_init(void) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds int count; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds arcnet_debug = debug; 1181da177e4SLinus Torvalds 11905a24b23SJoe Perches pr_info("arcnet loaded\n"); 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* initialize the protocol map */ 1221da177e4SLinus Torvalds arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null; 1231da177e4SLinus Torvalds for (count = 0; count < 256; count++) 1241da177e4SLinus Torvalds arc_proto_map[count] = arc_proto_default; 1251da177e4SLinus Torvalds 12672aeea48SJoe Perches if (BUGLVL(D_DURING)) 1275b5e0928SAlexey Dobriyan pr_info("struct sizes: %zd %zd %zd %zd %zd\n", 12805a24b23SJoe Perches sizeof(struct arc_hardware), 12905a24b23SJoe Perches sizeof(struct arc_rfc1201), 13005a24b23SJoe Perches sizeof(struct arc_rfc1051), 13105a24b23SJoe Perches sizeof(struct arc_eth_encap), 1321da177e4SLinus Torvalds sizeof(struct archdr)); 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds return 0; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds static void __exit arcnet_exit(void) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds module_init(arcnet_init); 1421da177e4SLinus Torvalds module_exit(arcnet_exit); 1431da177e4SLinus Torvalds 144f2f0a16bSJoe Perches /* Dump the contents of an sk_buff */ 1451da177e4SLinus Torvalds #if ARCNET_DEBUG_MAX & D_SKB 1461da177e4SLinus Torvalds void arcnet_dump_skb(struct net_device *dev, 1471da177e4SLinus Torvalds struct sk_buff *skb, char *desc) 1481da177e4SLinus Torvalds { 149ad361c98SJoe Perches char hdr[32]; 1501da177e4SLinus Torvalds 151ad361c98SJoe Perches /* dump the packet */ 152ad361c98SJoe Perches snprintf(hdr, sizeof(hdr), "%6s:%s skb->data:", dev->name, desc); 153ad361c98SJoe Perches print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET, 154ad361c98SJoe Perches 16, 1, skb->data, skb->len, true); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds EXPORT_SYMBOL(arcnet_dump_skb); 1571da177e4SLinus Torvalds #endif 1581da177e4SLinus Torvalds 159f2f0a16bSJoe Perches /* Dump the contents of an ARCnet buffer */ 1601da177e4SLinus Torvalds #if (ARCNET_DEBUG_MAX & (D_RX | D_TX)) 161f03aa2d8SAdrian Bunk static void arcnet_dump_packet(struct net_device *dev, int bufnum, 162f03aa2d8SAdrian Bunk char *desc, int take_arcnet_lock) 1631da177e4SLinus Torvalds { 164454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 1651da177e4SLinus Torvalds int i, length; 1661da177e4SLinus Torvalds unsigned long flags = 0; 1671da177e4SLinus Torvalds static uint8_t buf[512]; 168ad361c98SJoe Perches char hdr[32]; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* hw.copy_from_card expects IRQ context so take the IRQ lock 171f2f0a16bSJoe Perches * to keep it single threaded 172f2f0a16bSJoe Perches */ 1731da177e4SLinus Torvalds if (take_arcnet_lock) 1741da177e4SLinus Torvalds spin_lock_irqsave(&lp->lock, flags); 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds lp->hw.copy_from_card(dev, bufnum, 0, buf, 512); 1771da177e4SLinus Torvalds if (take_arcnet_lock) 1781da177e4SLinus Torvalds spin_unlock_irqrestore(&lp->lock, flags); 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* if the offset[0] byte is nonzero, this is a 256-byte packet */ 1811da177e4SLinus Torvalds length = (buf[2] ? 256 : 512); 1821da177e4SLinus Torvalds 183ad361c98SJoe Perches /* dump the packet */ 184ad361c98SJoe Perches snprintf(hdr, sizeof(hdr), "%6s:%s packet dump:", dev->name, desc); 185ad361c98SJoe Perches print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET, 186ad361c98SJoe Perches 16, 1, buf, length, true); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 189f03aa2d8SAdrian Bunk #else 190f03aa2d8SAdrian Bunk 191f03aa2d8SAdrian Bunk #define arcnet_dump_packet(dev, bufnum, desc, take_arcnet_lock) do { } while (0) 192f03aa2d8SAdrian Bunk 1931da177e4SLinus Torvalds #endif 1941da177e4SLinus Torvalds 1958890624aSMichael Grzeschik /* Trigger a LED event in response to a ARCNET device event */ 1968890624aSMichael Grzeschik void arcnet_led_event(struct net_device *dev, enum arcnet_led_event event) 1978890624aSMichael Grzeschik { 1988890624aSMichael Grzeschik struct arcnet_local *lp = netdev_priv(dev); 1998890624aSMichael Grzeschik unsigned long led_delay = 350; 2008890624aSMichael Grzeschik unsigned long tx_delay = 50; 2018890624aSMichael Grzeschik 2028890624aSMichael Grzeschik switch (event) { 2038890624aSMichael Grzeschik case ARCNET_LED_EVENT_RECON: 2048890624aSMichael Grzeschik led_trigger_blink_oneshot(lp->recon_led_trig, 2058890624aSMichael Grzeschik &led_delay, &led_delay, 0); 2068890624aSMichael Grzeschik break; 2078890624aSMichael Grzeschik case ARCNET_LED_EVENT_OPEN: 2088890624aSMichael Grzeschik led_trigger_event(lp->tx_led_trig, LED_OFF); 2098890624aSMichael Grzeschik led_trigger_event(lp->recon_led_trig, LED_OFF); 2108890624aSMichael Grzeschik break; 2118890624aSMichael Grzeschik case ARCNET_LED_EVENT_STOP: 2128890624aSMichael Grzeschik led_trigger_event(lp->tx_led_trig, LED_OFF); 2138890624aSMichael Grzeschik led_trigger_event(lp->recon_led_trig, LED_OFF); 2148890624aSMichael Grzeschik break; 2158890624aSMichael Grzeschik case ARCNET_LED_EVENT_TX: 2168890624aSMichael Grzeschik led_trigger_blink_oneshot(lp->tx_led_trig, 2178890624aSMichael Grzeschik &tx_delay, &tx_delay, 0); 2188890624aSMichael Grzeschik break; 2198890624aSMichael Grzeschik } 2208890624aSMichael Grzeschik } 2218890624aSMichael Grzeschik EXPORT_SYMBOL_GPL(arcnet_led_event); 2228890624aSMichael Grzeschik 2238890624aSMichael Grzeschik static void arcnet_led_release(struct device *gendev, void *res) 2248890624aSMichael Grzeschik { 2258890624aSMichael Grzeschik struct arcnet_local *lp = netdev_priv(to_net_dev(gendev)); 2268890624aSMichael Grzeschik 2278890624aSMichael Grzeschik led_trigger_unregister_simple(lp->tx_led_trig); 2288890624aSMichael Grzeschik led_trigger_unregister_simple(lp->recon_led_trig); 2298890624aSMichael Grzeschik } 2308890624aSMichael Grzeschik 2318890624aSMichael Grzeschik /* Register ARCNET LED triggers for a arcnet device 2328890624aSMichael Grzeschik * 2338890624aSMichael Grzeschik * This is normally called from a driver's probe function 2348890624aSMichael Grzeschik */ 2358890624aSMichael Grzeschik void devm_arcnet_led_init(struct net_device *netdev, int index, int subid) 2368890624aSMichael Grzeschik { 2378890624aSMichael Grzeschik struct arcnet_local *lp = netdev_priv(netdev); 2388890624aSMichael Grzeschik void *res; 2398890624aSMichael Grzeschik 2408890624aSMichael Grzeschik res = devres_alloc(arcnet_led_release, 0, GFP_KERNEL); 2418890624aSMichael Grzeschik if (!res) { 2428890624aSMichael Grzeschik netdev_err(netdev, "cannot register LED triggers\n"); 2438890624aSMichael Grzeschik return; 2448890624aSMichael Grzeschik } 2458890624aSMichael Grzeschik 2468890624aSMichael Grzeschik snprintf(lp->tx_led_trig_name, sizeof(lp->tx_led_trig_name), 2478890624aSMichael Grzeschik "arc%d-%d-tx", index, subid); 2488890624aSMichael Grzeschik snprintf(lp->recon_led_trig_name, sizeof(lp->recon_led_trig_name), 2498890624aSMichael Grzeschik "arc%d-%d-recon", index, subid); 2508890624aSMichael Grzeschik 2518890624aSMichael Grzeschik led_trigger_register_simple(lp->tx_led_trig_name, 2528890624aSMichael Grzeschik &lp->tx_led_trig); 2538890624aSMichael Grzeschik led_trigger_register_simple(lp->recon_led_trig_name, 2548890624aSMichael Grzeschik &lp->recon_led_trig); 2558890624aSMichael Grzeschik 2568890624aSMichael Grzeschik devres_add(&netdev->dev, res); 2578890624aSMichael Grzeschik } 2588890624aSMichael Grzeschik EXPORT_SYMBOL_GPL(devm_arcnet_led_init); 2598890624aSMichael Grzeschik 260f2f0a16bSJoe Perches /* Unregister a protocol driver from the arc_proto_map. Protocol drivers 2611da177e4SLinus Torvalds * are responsible for registering themselves, but the unregister routine 2621da177e4SLinus Torvalds * is pretty generic so we'll do it here. 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds void arcnet_unregister_proto(struct ArcProto *proto) 2651da177e4SLinus Torvalds { 2661da177e4SLinus Torvalds int count; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds if (arc_proto_default == proto) 2691da177e4SLinus Torvalds arc_proto_default = &arc_proto_null; 2701da177e4SLinus Torvalds if (arc_bcast_proto == proto) 2711da177e4SLinus Torvalds arc_bcast_proto = arc_proto_default; 2721da177e4SLinus Torvalds if (arc_raw_proto == proto) 2731da177e4SLinus Torvalds arc_raw_proto = arc_proto_default; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds for (count = 0; count < 256; count++) { 2761da177e4SLinus Torvalds if (arc_proto_map[count] == proto) 2771da177e4SLinus Torvalds arc_proto_map[count] = arc_proto_default; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds } 280811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_unregister_proto); 2811da177e4SLinus Torvalds 282f2f0a16bSJoe Perches /* Add a buffer to the queue. Only the interrupt handler is allowed to do 2831da177e4SLinus Torvalds * this, unless interrupts are disabled. 2841da177e4SLinus Torvalds * 2851da177e4SLinus Torvalds * Note: we don't check for a full queue, since there aren't enough buffers 2861da177e4SLinus Torvalds * to more than fill it. 2871da177e4SLinus Torvalds */ 2881da177e4SLinus Torvalds static void release_arcbuf(struct net_device *dev, int bufnum) 2891da177e4SLinus Torvalds { 290454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 2911da177e4SLinus Torvalds int i; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds lp->buf_queue[lp->first_free_buf++] = bufnum; 2941da177e4SLinus Torvalds lp->first_free_buf %= 5; 2951da177e4SLinus Torvalds 29672aeea48SJoe Perches if (BUGLVL(D_DURING)) { 297a34c0932SJoe Perches arc_printk(D_DURING, dev, "release_arcbuf: freed #%d; buffer queue is now: ", 2981da177e4SLinus Torvalds bufnum); 2991da177e4SLinus Torvalds for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5) 300a34c0932SJoe Perches arc_cont(D_DURING, "#%d ", lp->buf_queue[i]); 301a34c0932SJoe Perches arc_cont(D_DURING, "\n"); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 305f2f0a16bSJoe Perches /* Get a buffer from the queue. 306f2f0a16bSJoe Perches * If this returns -1, there are no buffers available. 3071da177e4SLinus Torvalds */ 3081da177e4SLinus Torvalds static int get_arcbuf(struct net_device *dev) 3091da177e4SLinus Torvalds { 310454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 3111da177e4SLinus Torvalds int buf = -1, i; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds if (!atomic_dec_and_test(&lp->buf_lock)) { 3141da177e4SLinus Torvalds /* already in this function */ 315a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "get_arcbuf: overlap (%d)!\n", 3161da177e4SLinus Torvalds lp->buf_lock.counter); 3177f5e760cSJoe Perches } else { /* we can continue */ 3181da177e4SLinus Torvalds if (lp->next_buf >= 5) 3191da177e4SLinus Torvalds lp->next_buf -= 5; 3201da177e4SLinus Torvalds 3217f5e760cSJoe Perches if (lp->next_buf == lp->first_free_buf) { 322a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "get_arcbuf: BUG: no buffers are available??\n"); 3237f5e760cSJoe Perches } else { 3241da177e4SLinus Torvalds buf = lp->buf_queue[lp->next_buf++]; 3251da177e4SLinus Torvalds lp->next_buf %= 5; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 32972aeea48SJoe Perches if (BUGLVL(D_DURING)) { 330a34c0932SJoe Perches arc_printk(D_DURING, dev, "get_arcbuf: got #%d; buffer queue is now: ", 331a34c0932SJoe Perches buf); 3321da177e4SLinus Torvalds for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5) 333a34c0932SJoe Perches arc_cont(D_DURING, "#%d ", lp->buf_queue[i]); 334a34c0932SJoe Perches arc_cont(D_DURING, "\n"); 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds atomic_inc(&lp->buf_lock); 3381da177e4SLinus Torvalds return buf; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds static int choose_mtu(void) 3421da177e4SLinus Torvalds { 3431da177e4SLinus Torvalds int count, mtu = 65535; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* choose the smallest MTU of all available encaps */ 3461da177e4SLinus Torvalds for (count = 0; count < 256; count++) { 3478e95a202SJoe Perches if (arc_proto_map[count] != &arc_proto_null && 3488e95a202SJoe Perches arc_proto_map[count]->mtu < mtu) { 3491da177e4SLinus Torvalds mtu = arc_proto_map[count]->mtu; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds return mtu == 65535 ? XMTU : mtu; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 3563b04dddeSStephen Hemminger static const struct header_ops arcnet_header_ops = { 3573b04dddeSStephen Hemminger .create = arcnet_header, 3583b04dddeSStephen Hemminger }; 3593b04dddeSStephen Hemminger 360bca5b893SStephen Hemminger static const struct net_device_ops arcnet_netdev_ops = { 361bca5b893SStephen Hemminger .ndo_open = arcnet_open, 362bca5b893SStephen Hemminger .ndo_stop = arcnet_close, 363bca5b893SStephen Hemminger .ndo_start_xmit = arcnet_send_packet, 364bca5b893SStephen Hemminger .ndo_tx_timeout = arcnet_timeout, 365bca5b893SStephen Hemminger }; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds /* Setup a struct device for ARCnet. */ 3681da177e4SLinus Torvalds static void arcdev_setup(struct net_device *dev) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds dev->type = ARPHRD_ARCNET; 371bca5b893SStephen Hemminger dev->netdev_ops = &arcnet_netdev_ops; 3723b04dddeSStephen Hemminger dev->header_ops = &arcnet_header_ops; 373980137a2SMichael Grzeschik dev->hard_header_len = sizeof(struct arc_hardware); 3741da177e4SLinus Torvalds dev->mtu = choose_mtu(); 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds dev->addr_len = ARCNET_ALEN; 3771da177e4SLinus Torvalds dev->tx_queue_len = 100; 3781da177e4SLinus Torvalds dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */ 3791da177e4SLinus Torvalds dev->watchdog_timeo = TX_TIMEOUT; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds /* New-style flags. */ 3821da177e4SLinus Torvalds dev->flags = IFF_BROADCAST; 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 385*c58320deSKees Cook static void arcnet_timer(struct timer_list *t) 38659fbcbc6SMichael Grzeschik { 387*c58320deSKees Cook struct arcnet_local *lp = from_timer(lp, t, timer); 388*c58320deSKees Cook struct net_device *dev = lp->dev; 38959fbcbc6SMichael Grzeschik 39059fbcbc6SMichael Grzeschik if (!netif_carrier_ok(dev)) { 39159fbcbc6SMichael Grzeschik netif_carrier_on(dev); 39259fbcbc6SMichael Grzeschik netdev_info(dev, "link up\n"); 39359fbcbc6SMichael Grzeschik } 39459fbcbc6SMichael Grzeschik } 39559fbcbc6SMichael Grzeschik 39605fcd31cSMichael Grzeschik static void arcnet_reply_tasklet(unsigned long data) 39705fcd31cSMichael Grzeschik { 39805fcd31cSMichael Grzeschik struct arcnet_local *lp = (struct arcnet_local *)data; 39905fcd31cSMichael Grzeschik 40005fcd31cSMichael Grzeschik struct sk_buff *ackskb, *skb; 40105fcd31cSMichael Grzeschik struct sock_exterr_skb *serr; 40205fcd31cSMichael Grzeschik struct sock *sk; 40305fcd31cSMichael Grzeschik int ret; 40405fcd31cSMichael Grzeschik 40505fcd31cSMichael Grzeschik local_irq_disable(); 40605fcd31cSMichael Grzeschik skb = lp->outgoing.skb; 40705fcd31cSMichael Grzeschik if (!skb || !skb->sk) { 40805fcd31cSMichael Grzeschik local_irq_enable(); 40905fcd31cSMichael Grzeschik return; 41005fcd31cSMichael Grzeschik } 41105fcd31cSMichael Grzeschik 41205fcd31cSMichael Grzeschik sock_hold(skb->sk); 41305fcd31cSMichael Grzeschik sk = skb->sk; 41405fcd31cSMichael Grzeschik ackskb = skb_clone_sk(skb); 41505fcd31cSMichael Grzeschik sock_put(skb->sk); 41605fcd31cSMichael Grzeschik 41705fcd31cSMichael Grzeschik if (!ackskb) { 41805fcd31cSMichael Grzeschik local_irq_enable(); 41905fcd31cSMichael Grzeschik return; 42005fcd31cSMichael Grzeschik } 42105fcd31cSMichael Grzeschik 42205fcd31cSMichael Grzeschik serr = SKB_EXT_ERR(ackskb); 42305fcd31cSMichael Grzeschik memset(serr, 0, sizeof(*serr)); 42405fcd31cSMichael Grzeschik serr->ee.ee_errno = ENOMSG; 42505fcd31cSMichael Grzeschik serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 42605fcd31cSMichael Grzeschik serr->ee.ee_data = skb_shinfo(skb)->tskey; 42705fcd31cSMichael Grzeschik serr->ee.ee_info = lp->reply_status; 42805fcd31cSMichael Grzeschik 42905fcd31cSMichael Grzeschik /* finally erasing outgoing skb */ 43005fcd31cSMichael Grzeschik dev_kfree_skb(lp->outgoing.skb); 43105fcd31cSMichael Grzeschik lp->outgoing.skb = NULL; 43205fcd31cSMichael Grzeschik 43305fcd31cSMichael Grzeschik ackskb->dev = lp->dev; 43405fcd31cSMichael Grzeschik 43505fcd31cSMichael Grzeschik ret = sock_queue_err_skb(sk, ackskb); 43605fcd31cSMichael Grzeschik if (ret) 43705fcd31cSMichael Grzeschik kfree_skb(ackskb); 43805fcd31cSMichael Grzeschik 43905fcd31cSMichael Grzeschik local_irq_enable(); 44005fcd31cSMichael Grzeschik }; 44105fcd31cSMichael Grzeschik 442bca5b893SStephen Hemminger struct net_device *alloc_arcdev(const char *name) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds struct net_device *dev; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds dev = alloc_netdev(sizeof(struct arcnet_local), 447c835a677STom Gundersen name && *name ? name : "arc%d", NET_NAME_UNKNOWN, 448c835a677STom Gundersen arcdev_setup); 4491da177e4SLinus Torvalds if (dev) { 450454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 45101a1d5acSJoe Perches 45205fcd31cSMichael Grzeschik lp->dev = dev; 4531da177e4SLinus Torvalds spin_lock_init(&lp->lock); 454*c58320deSKees Cook timer_setup(&lp->timer, arcnet_timer, 0); 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds return dev; 4581da177e4SLinus Torvalds } 459811eafc0SJoe Perches EXPORT_SYMBOL(alloc_arcdev); 4601da177e4SLinus Torvalds 461f2f0a16bSJoe Perches /* Open/initialize the board. This is called sometime after booting when 4621da177e4SLinus Torvalds * the 'ifconfig' program is run. 4631da177e4SLinus Torvalds * 4641da177e4SLinus Torvalds * This routine should set everything up anew at each open, even registers 4651da177e4SLinus Torvalds * that "should" only need to be set once at boot, so that there is 4661da177e4SLinus Torvalds * non-reboot way to recover if something goes wrong. 4671da177e4SLinus Torvalds */ 468bca5b893SStephen Hemminger int arcnet_open(struct net_device *dev) 4691da177e4SLinus Torvalds { 470454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 4711da177e4SLinus Torvalds int count, newmtu, error; 4721da177e4SLinus Torvalds 473a34c0932SJoe Perches arc_printk(D_INIT, dev, "opened."); 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds if (!try_module_get(lp->hw.owner)) 4761da177e4SLinus Torvalds return -ENODEV; 4771da177e4SLinus Torvalds 47872aeea48SJoe Perches if (BUGLVL(D_PROTO)) { 479a34c0932SJoe Perches arc_printk(D_PROTO, dev, "protocol map (default is '%c'): ", 4801da177e4SLinus Torvalds arc_proto_default->suffix); 4811da177e4SLinus Torvalds for (count = 0; count < 256; count++) 482a34c0932SJoe Perches arc_cont(D_PROTO, "%c", arc_proto_map[count]->suffix); 483a34c0932SJoe Perches arc_cont(D_PROTO, "\n"); 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 48605fcd31cSMichael Grzeschik tasklet_init(&lp->reply_tasklet, arcnet_reply_tasklet, 48705fcd31cSMichael Grzeschik (unsigned long)lp); 48805fcd31cSMichael Grzeschik 489a34c0932SJoe Perches arc_printk(D_INIT, dev, "arcnet_open: resetting card.\n"); 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* try to put the card in a defined state - if it fails the first 4921da177e4SLinus Torvalds * time, actually reset it. 4931da177e4SLinus Torvalds */ 4941da177e4SLinus Torvalds error = -ENODEV; 495e15b0362SJoe Perches if (lp->hw.reset(dev, 0) && lp->hw.reset(dev, 1)) 4961da177e4SLinus Torvalds goto out_module_put; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds newmtu = choose_mtu(); 4991da177e4SLinus Torvalds if (newmtu < dev->mtu) 5001da177e4SLinus Torvalds dev->mtu = newmtu; 5011da177e4SLinus Torvalds 502a34c0932SJoe Perches arc_printk(D_INIT, dev, "arcnet_open: mtu: %d.\n", dev->mtu); 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds /* autodetect the encapsulation for each host. */ 5051da177e4SLinus Torvalds memset(lp->default_proto, 0, sizeof(lp->default_proto)); 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds /* the broadcast address is special - use the 'bcast' protocol */ 5081da177e4SLinus Torvalds for (count = 0; count < 256; count++) { 5091da177e4SLinus Torvalds if (arc_proto_map[count] == arc_bcast_proto) { 5101da177e4SLinus Torvalds lp->default_proto[0] = count; 5111da177e4SLinus Torvalds break; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* initialize buffers */ 5161da177e4SLinus Torvalds atomic_set(&lp->buf_lock, 1); 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds lp->next_buf = lp->first_free_buf = 0; 5191da177e4SLinus Torvalds release_arcbuf(dev, 0); 5201da177e4SLinus Torvalds release_arcbuf(dev, 1); 5211da177e4SLinus Torvalds release_arcbuf(dev, 2); 5221da177e4SLinus Torvalds release_arcbuf(dev, 3); 5231da177e4SLinus Torvalds lp->cur_tx = lp->next_tx = -1; 5241da177e4SLinus Torvalds lp->cur_rx = -1; 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds lp->rfc1201.sequence = 1; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds /* bring up the hardware driver */ 5291da177e4SLinus Torvalds if (lp->hw.open) 5301da177e4SLinus Torvalds lp->hw.open(dev); 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds if (dev->dev_addr[0] == 0) 533a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "WARNING! Station address 00 is reserved for broadcasts!\n"); 5341da177e4SLinus Torvalds else if (dev->dev_addr[0] == 255) 535a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "WARNING! Station address FF may confuse DOS networking programs!\n"); 5361da177e4SLinus Torvalds 537a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); 538e15b0362SJoe Perches if (lp->hw.status(dev) & RESETflag) { 539a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "%s: %d: %s\n", 540a34c0932SJoe Perches __FILE__, __LINE__, __func__); 541e15b0362SJoe Perches lp->hw.command(dev, CFLAGScmd | RESETclear); 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 544a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); 5451da177e4SLinus Torvalds /* make sure we're ready to receive IRQ's. */ 546e15b0362SJoe Perches lp->hw.intmask(dev, 0); 5471da177e4SLinus Torvalds udelay(1); /* give it time to set the mask before 5481da177e4SLinus Torvalds * we reset it again. (may not even be 5491da177e4SLinus Torvalds * necessary) 5501da177e4SLinus Torvalds */ 551a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); 5521da177e4SLinus Torvalds lp->intmask = NORXflag | RECONflag; 553e15b0362SJoe Perches lp->hw.intmask(dev, lp->intmask); 554a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); 5551da177e4SLinus Torvalds 55659fbcbc6SMichael Grzeschik netif_carrier_off(dev); 5571da177e4SLinus Torvalds netif_start_queue(dev); 55859fbcbc6SMichael Grzeschik mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000)); 5591da177e4SLinus Torvalds 5608890624aSMichael Grzeschik arcnet_led_event(dev, ARCNET_LED_EVENT_OPEN); 5611da177e4SLinus Torvalds return 0; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds out_module_put: 5641da177e4SLinus Torvalds module_put(lp->hw.owner); 5651da177e4SLinus Torvalds return error; 5661da177e4SLinus Torvalds } 567811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_open); 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds /* The inverse routine to arcnet_open - shuts down the card. */ 570bca5b893SStephen Hemminger int arcnet_close(struct net_device *dev) 5711da177e4SLinus Torvalds { 572454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 5731da177e4SLinus Torvalds 5748890624aSMichael Grzeschik arcnet_led_event(dev, ARCNET_LED_EVENT_STOP); 57559fbcbc6SMichael Grzeschik del_timer_sync(&lp->timer); 57659fbcbc6SMichael Grzeschik 5771da177e4SLinus Torvalds netif_stop_queue(dev); 57859fbcbc6SMichael Grzeschik netif_carrier_off(dev); 5791da177e4SLinus Torvalds 58005fcd31cSMichael Grzeschik tasklet_kill(&lp->reply_tasklet); 58105fcd31cSMichael Grzeschik 5821da177e4SLinus Torvalds /* flush TX and disable RX */ 583e15b0362SJoe Perches lp->hw.intmask(dev, 0); 584e15b0362SJoe Perches lp->hw.command(dev, NOTXcmd); /* stop transmit */ 585e15b0362SJoe Perches lp->hw.command(dev, NORXcmd); /* disable receive */ 5861da177e4SLinus Torvalds mdelay(1); 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds /* shut down the card */ 5891da177e4SLinus Torvalds lp->hw.close(dev); 5901da177e4SLinus Torvalds module_put(lp->hw.owner); 5911da177e4SLinus Torvalds return 0; 5921da177e4SLinus Torvalds } 593811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_close); 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds static int arcnet_header(struct sk_buff *skb, struct net_device *dev, 5963b04dddeSStephen Hemminger unsigned short type, const void *daddr, 5973b04dddeSStephen Hemminger const void *saddr, unsigned len) 5981da177e4SLinus Torvalds { 5993b04dddeSStephen Hemminger const struct arcnet_local *lp = netdev_priv(dev); 6001da177e4SLinus Torvalds uint8_t _daddr, proto_num; 6011da177e4SLinus Torvalds struct ArcProto *proto; 6021da177e4SLinus Torvalds 603a34c0932SJoe Perches arc_printk(D_DURING, dev, 6041da177e4SLinus Torvalds "create header from %d to %d; protocol %d (%Xh); size %u.\n", 6051da177e4SLinus Torvalds saddr ? *(uint8_t *)saddr : -1, 6061da177e4SLinus Torvalds daddr ? *(uint8_t *)daddr : -1, 6071da177e4SLinus Torvalds type, type, len); 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds if (skb->len != 0 && len != skb->len) 610a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n", 6111da177e4SLinus Torvalds skb->len, len); 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds /* Type is host order - ? */ 6141da177e4SLinus Torvalds if (type == ETH_P_ARCNET) { 6151da177e4SLinus Torvalds proto = arc_raw_proto; 616a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n", 617a34c0932SJoe Perches proto->suffix); 6181da177e4SLinus Torvalds _daddr = daddr ? *(uint8_t *)daddr : 0; 6197f5e760cSJoe Perches } else if (!daddr) { 620f2f0a16bSJoe Perches /* if the dest addr isn't provided, we can't choose an 621f2f0a16bSJoe Perches * encapsulation! Store the packet type (eg. ETH_P_IP) 622f2f0a16bSJoe Perches * for now, and we'll push on a real header when we do 623f2f0a16bSJoe Perches * rebuild_header. 6241da177e4SLinus Torvalds */ 6251da177e4SLinus Torvalds *(uint16_t *)skb_push(skb, 2) = type; 626f2f0a16bSJoe Perches /* XXX: Why not use skb->mac_len? */ 627b0e380b1SArnaldo Carvalho de Melo if (skb->network_header - skb->mac_header != 2) 628a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! diff (%u) is not 2!\n", 629a34c0932SJoe Perches skb->network_header - skb->mac_header); 6301da177e4SLinus Torvalds return -2; /* return error -- can't transmit yet! */ 6317f5e760cSJoe Perches } else { 6321da177e4SLinus Torvalds /* otherwise, we can just add the header as usual. */ 6331da177e4SLinus Torvalds _daddr = *(uint8_t *)daddr; 6341da177e4SLinus Torvalds proto_num = lp->default_proto[_daddr]; 6351da177e4SLinus Torvalds proto = arc_proto_map[proto_num]; 636a34c0932SJoe Perches arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n", 6371da177e4SLinus Torvalds proto_num, proto->suffix); 6381da177e4SLinus Torvalds if (proto == &arc_proto_null && arc_bcast_proto != proto) { 639a34c0932SJoe Perches arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n", 6401da177e4SLinus Torvalds arc_bcast_proto->suffix); 6411da177e4SLinus Torvalds proto = arc_bcast_proto; 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds return proto->build_header(skb, dev, type, _daddr); 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* Called by the kernel in order to transmit a packet. */ 64861357325SStephen Hemminger netdev_tx_t arcnet_send_packet(struct sk_buff *skb, 64961357325SStephen Hemminger struct net_device *dev) 6501da177e4SLinus Torvalds { 651454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 6521da177e4SLinus Torvalds struct archdr *pkt; 6531da177e4SLinus Torvalds struct arc_rfc1201 *soft; 6541da177e4SLinus Torvalds struct ArcProto *proto; 6551da177e4SLinus Torvalds int txbuf; 6561da177e4SLinus Torvalds unsigned long flags; 657b82de0e2SMichael Grzeschik int retval; 6581da177e4SLinus Torvalds 659a34c0932SJoe Perches arc_printk(D_DURING, dev, 6601da177e4SLinus Torvalds "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n", 661e15b0362SJoe Perches lp->hw.status(dev), lp->cur_tx, lp->next_tx, skb->len, skb->protocol); 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds pkt = (struct archdr *)skb->data; 6641da177e4SLinus Torvalds soft = &pkt->soft.rfc1201; 6651da177e4SLinus Torvalds proto = arc_proto_map[soft->proto]; 6661da177e4SLinus Torvalds 667a34c0932SJoe Perches arc_printk(D_SKB_SIZE, dev, "skb: transmitting %d bytes to %02X\n", 6681da177e4SLinus Torvalds skb->len, pkt->hard.dest); 66972aeea48SJoe Perches if (BUGLVL(D_SKB)) 67072aeea48SJoe Perches arcnet_dump_skb(dev, skb, "tx"); 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds /* fits in one packet? */ 6731da177e4SLinus Torvalds if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) { 674a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "fixme: packet too large: compensating badly!\n"); 6751da177e4SLinus Torvalds dev_kfree_skb(skb); 676c6bb15a0SPieter Dejaeghere return NETDEV_TX_OK; /* don't try again */ 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds /* We're busy transmitting a packet... */ 6801da177e4SLinus Torvalds netif_stop_queue(dev); 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds spin_lock_irqsave(&lp->lock, flags); 683e15b0362SJoe Perches lp->hw.intmask(dev, 0); 684c6bb15a0SPieter Dejaeghere if (lp->next_tx == -1) 6851da177e4SLinus Torvalds txbuf = get_arcbuf(dev); 6867f5e760cSJoe Perches else 687c6bb15a0SPieter Dejaeghere txbuf = -1; 6887f5e760cSJoe Perches 6891da177e4SLinus Torvalds if (txbuf != -1) { 69005fcd31cSMichael Grzeschik lp->outgoing.skb = skb; 6911da177e4SLinus Torvalds if (proto->prepare_tx(dev, pkt, skb->len, txbuf) && 6921da177e4SLinus Torvalds !proto->ack_tx) { 6931da177e4SLinus Torvalds /* done right away and we don't want to acknowledge 694f2f0a16bSJoe Perches * the package later - forget about it now 695f2f0a16bSJoe Perches */ 6965803c512SStephen Hemminger dev->stats.tx_bytes += skb->len; 6971da177e4SLinus Torvalds } else { 6981da177e4SLinus Torvalds /* do it the 'split' way */ 6991da177e4SLinus Torvalds lp->outgoing.proto = proto; 7001da177e4SLinus Torvalds lp->outgoing.skb = skb; 7011da177e4SLinus Torvalds lp->outgoing.pkt = pkt; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds if (proto->continue_tx && 7041da177e4SLinus Torvalds proto->continue_tx(dev, txbuf)) { 705a34c0932SJoe Perches arc_printk(D_NORMAL, dev, 7063b4e5551SJoe Perches "bug! continue_tx finished the first time! (proto='%c')\n", 7073b4e5551SJoe Perches proto->suffix); 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds } 710c6bb15a0SPieter Dejaeghere retval = NETDEV_TX_OK; 7111da177e4SLinus Torvalds lp->next_tx = txbuf; 7121da177e4SLinus Torvalds } else { 713c6bb15a0SPieter Dejaeghere retval = NETDEV_TX_BUSY; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 716a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n", 717e15b0362SJoe Perches __FILE__, __LINE__, __func__, lp->hw.status(dev)); 7181da177e4SLinus Torvalds /* make sure we didn't ignore a TX IRQ while we were in here */ 719e15b0362SJoe Perches lp->hw.intmask(dev, 0); 7201da177e4SLinus Torvalds 721a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); 7221da177e4SLinus Torvalds lp->intmask |= TXFREEflag | EXCNAKflag; 723e15b0362SJoe Perches lp->hw.intmask(dev, lp->intmask); 724a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n", 725e15b0362SJoe Perches __FILE__, __LINE__, __func__, lp->hw.status(dev)); 7261da177e4SLinus Torvalds 7278890624aSMichael Grzeschik arcnet_led_event(dev, ARCNET_LED_EVENT_TX); 7288890624aSMichael Grzeschik 7291da177e4SLinus Torvalds spin_unlock_irqrestore(&lp->lock, flags); 730c6bb15a0SPieter Dejaeghere return retval; /* no need to try again */ 7311da177e4SLinus Torvalds } 732811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_send_packet); 7331da177e4SLinus Torvalds 734f2f0a16bSJoe Perches /* Actually start transmitting a packet that was loaded into a buffer 7351da177e4SLinus Torvalds * by prepare_tx. This should _only_ be called by the interrupt handler. 7361da177e4SLinus Torvalds */ 7371da177e4SLinus Torvalds static int go_tx(struct net_device *dev) 7381da177e4SLinus Torvalds { 739454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 7401da177e4SLinus Torvalds 741a34c0932SJoe Perches arc_printk(D_DURING, dev, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n", 742e15b0362SJoe Perches lp->hw.status(dev), lp->intmask, lp->next_tx, lp->cur_tx); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds if (lp->cur_tx != -1 || lp->next_tx == -1) 7451da177e4SLinus Torvalds return 0; 7461da177e4SLinus Torvalds 74772aeea48SJoe Perches if (BUGLVL(D_TX)) 74872aeea48SJoe Perches arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0); 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds lp->cur_tx = lp->next_tx; 7511da177e4SLinus Torvalds lp->next_tx = -1; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* start sending */ 754e15b0362SJoe Perches lp->hw.command(dev, TXcmd | (lp->cur_tx << 3)); 7551da177e4SLinus Torvalds 7565803c512SStephen Hemminger dev->stats.tx_packets++; 7571da177e4SLinus Torvalds lp->lasttrans_dest = lp->lastload_dest; 7581da177e4SLinus Torvalds lp->lastload_dest = 0; 7591da177e4SLinus Torvalds lp->excnak_pending = 0; 7601da177e4SLinus Torvalds lp->intmask |= TXFREEflag | EXCNAKflag; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds return 1; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds /* Called by the kernel when transmit times out */ 766bca5b893SStephen Hemminger void arcnet_timeout(struct net_device *dev) 7671da177e4SLinus Torvalds { 7681da177e4SLinus Torvalds unsigned long flags; 769454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 770e15b0362SJoe Perches int status = lp->hw.status(dev); 7711da177e4SLinus Torvalds char *msg; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds spin_lock_irqsave(&lp->lock, flags); 7741da177e4SLinus Torvalds if (status & TXFREEflag) { /* transmit _DID_ finish */ 7751da177e4SLinus Torvalds msg = " - missed IRQ?"; 7761da177e4SLinus Torvalds } else { 7771da177e4SLinus Torvalds msg = ""; 7785803c512SStephen Hemminger dev->stats.tx_aborted_errors++; 7791da177e4SLinus Torvalds lp->timed_out = 1; 780e15b0362SJoe Perches lp->hw.command(dev, NOTXcmd | (lp->cur_tx << 3)); 7811da177e4SLinus Torvalds } 7825803c512SStephen Hemminger dev->stats.tx_errors++; 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds /* make sure we didn't miss a TX or a EXC NAK IRQ */ 785e15b0362SJoe Perches lp->hw.intmask(dev, 0); 7861da177e4SLinus Torvalds lp->intmask |= TXFREEflag | EXCNAKflag; 787e15b0362SJoe Perches lp->hw.intmask(dev, lp->intmask); 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds spin_unlock_irqrestore(&lp->lock, flags); 7901da177e4SLinus Torvalds 791ff5688aeSMarcelo Feitoza Parisi if (time_after(jiffies, lp->last_timeout + 10 * HZ)) { 792a34c0932SJoe Perches arc_printk(D_EXTRA, dev, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n", 7931da177e4SLinus Torvalds msg, status, lp->intmask, lp->lasttrans_dest); 7941da177e4SLinus Torvalds lp->last_timeout = jiffies; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds if (lp->cur_tx == -1) 7981da177e4SLinus Torvalds netif_wake_queue(dev); 7991da177e4SLinus Torvalds } 800811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_timeout); 8011da177e4SLinus Torvalds 802f2f0a16bSJoe Perches /* The typical workload of the driver: Handle the network interface 8031da177e4SLinus Torvalds * interrupts. Establish which device needs attention, and call the correct 8041da177e4SLinus Torvalds * chipset interrupt handler. 8051da177e4SLinus Torvalds */ 8067d12e780SDavid Howells irqreturn_t arcnet_interrupt(int irq, void *dev_id) 8071da177e4SLinus Torvalds { 8081da177e4SLinus Torvalds struct net_device *dev = dev_id; 8091da177e4SLinus Torvalds struct arcnet_local *lp; 8101da177e4SLinus Torvalds int recbuf, status, diagstatus, didsomething, boguscount; 8115b858403SMichael Grzeschik unsigned long flags; 8121da177e4SLinus Torvalds int retval = IRQ_NONE; 8131da177e4SLinus Torvalds 814a34c0932SJoe Perches arc_printk(D_DURING, dev, "\n"); 8151da177e4SLinus Torvalds 816a34c0932SJoe Perches arc_printk(D_DURING, dev, "in arcnet_interrupt\n"); 8171da177e4SLinus Torvalds 818454d7c9bSWang Chen lp = netdev_priv(dev); 8195d9428deSEric Sesterhenn BUG_ON(!lp); 8201da177e4SLinus Torvalds 8215b858403SMichael Grzeschik spin_lock_irqsave(&lp->lock, flags); 8221da177e4SLinus Torvalds 823f2f0a16bSJoe Perches /* RESET flag was enabled - if device is not running, we must 824f2f0a16bSJoe Perches * clear it right away (but nothing else). 8251da177e4SLinus Torvalds */ 8261da177e4SLinus Torvalds if (!netif_running(dev)) { 827e15b0362SJoe Perches if (lp->hw.status(dev) & RESETflag) 828e15b0362SJoe Perches lp->hw.command(dev, CFLAGScmd | RESETclear); 829e15b0362SJoe Perches lp->hw.intmask(dev, 0); 8305b858403SMichael Grzeschik spin_unlock_irqrestore(&lp->lock, flags); 831226ee675SMichael Grzeschik return retval; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 834a34c0932SJoe Perches arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n", 835e15b0362SJoe Perches lp->hw.status(dev), lp->intmask); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds boguscount = 5; 8381da177e4SLinus Torvalds do { 839e15b0362SJoe Perches status = lp->hw.status(dev); 8401da177e4SLinus Torvalds diagstatus = (status >> 8) & 0xFF; 8411da177e4SLinus Torvalds 842a34c0932SJoe Perches arc_printk(D_DEBUG, dev, "%s: %d: %s: status=%x\n", 843b39d66a8SHarvey Harrison __FILE__, __LINE__, __func__, status); 8441da177e4SLinus Torvalds didsomething = 0; 8451da177e4SLinus Torvalds 846f2f0a16bSJoe Perches /* RESET flag was enabled - card is resetting and if RX is 8471da177e4SLinus Torvalds * disabled, it's NOT because we just got a packet. 8481da177e4SLinus Torvalds * 849f2f0a16bSJoe Perches * The card is in an undefined state. 850f2f0a16bSJoe Perches * Clear it out and start over. 8511da177e4SLinus Torvalds */ 8521da177e4SLinus Torvalds if (status & RESETflag) { 853a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n", 854a34c0932SJoe Perches status); 8551da177e4SLinus Torvalds arcnet_close(dev); 8561da177e4SLinus Torvalds arcnet_open(dev); 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds /* get out of the interrupt handler! */ 8591da177e4SLinus Torvalds break; 8601da177e4SLinus Torvalds } 861f2f0a16bSJoe Perches /* RX is inhibited - we must have received something. 862f2f0a16bSJoe Perches * Prepare to receive into the next buffer. 8631da177e4SLinus Torvalds * 864f2f0a16bSJoe Perches * We don't actually copy the received packet from the card 865f2f0a16bSJoe Perches * until after the transmit handler runs (and possibly 866f2f0a16bSJoe Perches * launches the next tx); this should improve latency slightly 867f2f0a16bSJoe Perches * if we get both types of interrupts at once. 8681da177e4SLinus Torvalds */ 8691da177e4SLinus Torvalds recbuf = -1; 8701da177e4SLinus Torvalds if (status & lp->intmask & NORXflag) { 8711da177e4SLinus Torvalds recbuf = lp->cur_rx; 872a34c0932SJoe Perches arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n", 8731da177e4SLinus Torvalds recbuf, status); 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds lp->cur_rx = get_arcbuf(dev); 8761da177e4SLinus Torvalds if (lp->cur_rx != -1) { 877a34c0932SJoe Perches arc_printk(D_DURING, dev, "enabling receive to buffer #%d\n", 8781da177e4SLinus Torvalds lp->cur_rx); 879e15b0362SJoe Perches lp->hw.command(dev, RXcmd | (lp->cur_rx << 3) | RXbcasts); 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds didsomething++; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds if ((diagstatus & EXCNAKflag)) { 885a34c0932SJoe Perches arc_printk(D_DURING, dev, "EXCNAK IRQ (diagstat=%Xh)\n", 8861da177e4SLinus Torvalds diagstatus); 8871da177e4SLinus Torvalds 888e15b0362SJoe Perches lp->hw.command(dev, NOTXcmd); /* disable transmit */ 8891da177e4SLinus Torvalds lp->excnak_pending = 1; 8901da177e4SLinus Torvalds 891e15b0362SJoe Perches lp->hw.command(dev, EXCNAKclear); 8921da177e4SLinus Torvalds lp->intmask &= ~(EXCNAKflag); 8931da177e4SLinus Torvalds didsomething++; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds /* a transmit finished, and we're interested in it. */ 8971da177e4SLinus Torvalds if ((status & lp->intmask & TXFREEflag) || lp->timed_out) { 89805fcd31cSMichael Grzeschik int ackstatus; 8991da177e4SLinus Torvalds lp->intmask &= ~(TXFREEflag | EXCNAKflag); 9001da177e4SLinus Torvalds 90105fcd31cSMichael Grzeschik if (status & TXACKflag) 90205fcd31cSMichael Grzeschik ackstatus = 2; 90305fcd31cSMichael Grzeschik else if (lp->excnak_pending) 90405fcd31cSMichael Grzeschik ackstatus = 1; 90505fcd31cSMichael Grzeschik else 90605fcd31cSMichael Grzeschik ackstatus = 0; 90705fcd31cSMichael Grzeschik 908d6d7d3edSJoe Perches arc_printk(D_DURING, dev, "TX IRQ (stat=%Xh)\n", 909d6d7d3edSJoe Perches status); 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds if (lp->cur_tx != -1 && !lp->timed_out) { 9121da177e4SLinus Torvalds if (!(status & TXACKflag)) { 9131da177e4SLinus Torvalds if (lp->lasttrans_dest != 0) { 914a34c0932SJoe Perches arc_printk(D_EXTRA, dev, 9153b4e5551SJoe Perches "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n", 916a34c0932SJoe Perches status, 917a34c0932SJoe Perches lp->lasttrans_dest); 9185803c512SStephen Hemminger dev->stats.tx_errors++; 9195803c512SStephen Hemminger dev->stats.tx_carrier_errors++; 9201da177e4SLinus Torvalds } else { 921a34c0932SJoe Perches arc_printk(D_DURING, dev, 9223b4e5551SJoe Perches "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n", 923a34c0932SJoe Perches status, 924a34c0932SJoe Perches lp->lasttrans_dest); 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds if (lp->outgoing.proto && 9291da177e4SLinus Torvalds lp->outgoing.proto->ack_tx) { 9301da177e4SLinus Torvalds lp->outgoing.proto 9311da177e4SLinus Torvalds ->ack_tx(dev, ackstatus); 9321da177e4SLinus Torvalds } 93305fcd31cSMichael Grzeschik lp->reply_status = ackstatus; 93405fcd31cSMichael Grzeschik tasklet_hi_schedule(&lp->reply_tasklet); 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds if (lp->cur_tx != -1) 9371da177e4SLinus Torvalds release_arcbuf(dev, lp->cur_tx); 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds lp->cur_tx = -1; 9401da177e4SLinus Torvalds lp->timed_out = 0; 9411da177e4SLinus Torvalds didsomething++; 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds /* send another packet if there is one */ 9441da177e4SLinus Torvalds go_tx(dev); 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds /* continue a split packet, if any */ 947d6d7d3edSJoe Perches if (lp->outgoing.proto && 948d6d7d3edSJoe Perches lp->outgoing.proto->continue_tx) { 9491da177e4SLinus Torvalds int txbuf = get_arcbuf(dev); 95001a1d5acSJoe Perches 9511da177e4SLinus Torvalds if (txbuf != -1) { 9521da177e4SLinus Torvalds if (lp->outgoing.proto->continue_tx(dev, txbuf)) { 9531da177e4SLinus Torvalds /* that was the last segment */ 9545803c512SStephen Hemminger dev->stats.tx_bytes += lp->outgoing.skb->len; 9557f5e760cSJoe Perches if (!lp->outgoing.proto->ack_tx) { 9561da177e4SLinus Torvalds dev_kfree_skb_irq(lp->outgoing.skb); 9571da177e4SLinus Torvalds lp->outgoing.proto = NULL; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds lp->next_tx = txbuf; 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds /* inform upper layers of idleness, if necessary */ 9641da177e4SLinus Torvalds if (lp->cur_tx == -1) 9651da177e4SLinus Torvalds netif_wake_queue(dev); 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds /* now process the received packet, if any */ 9681da177e4SLinus Torvalds if (recbuf != -1) { 96972aeea48SJoe Perches if (BUGLVL(D_RX)) 97072aeea48SJoe Perches arcnet_dump_packet(dev, recbuf, "rx irq", 0); 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds arcnet_rx(dev, recbuf); 9731da177e4SLinus Torvalds release_arcbuf(dev, recbuf); 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds didsomething++; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds if (status & lp->intmask & RECONflag) { 978e15b0362SJoe Perches lp->hw.command(dev, CFLAGScmd | CONFIGclear); 9795803c512SStephen Hemminger dev->stats.tx_carrier_errors++; 9801da177e4SLinus Torvalds 981a34c0932SJoe Perches arc_printk(D_RECON, dev, "Network reconfiguration detected (status=%Xh)\n", 9821da177e4SLinus Torvalds status); 98359fbcbc6SMichael Grzeschik if (netif_carrier_ok(dev)) { 98459fbcbc6SMichael Grzeschik netif_carrier_off(dev); 98559fbcbc6SMichael Grzeschik netdev_info(dev, "link down\n"); 98659fbcbc6SMichael Grzeschik } 98759fbcbc6SMichael Grzeschik mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000)); 98859fbcbc6SMichael Grzeschik 9898890624aSMichael Grzeschik arcnet_led_event(dev, ARCNET_LED_EVENT_RECON); 990c6bb15a0SPieter Dejaeghere /* MYRECON bit is at bit 7 of diagstatus */ 991c6bb15a0SPieter Dejaeghere if (diagstatus & 0x80) 992a34c0932SJoe Perches arc_printk(D_RECON, dev, "Put out that recon myself\n"); 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds /* is the RECON info empty or old? */ 9951da177e4SLinus Torvalds if (!lp->first_recon || !lp->last_recon || 9969307b570SS.Caglar Onur time_after(jiffies, lp->last_recon + HZ * 10)) { 9971da177e4SLinus Torvalds if (lp->network_down) 998a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "reconfiguration detected: cabling restored?\n"); 9991da177e4SLinus Torvalds lp->first_recon = lp->last_recon = jiffies; 10001da177e4SLinus Torvalds lp->num_recons = lp->network_down = 0; 10011da177e4SLinus Torvalds 1002a34c0932SJoe Perches arc_printk(D_DURING, dev, "recon: clearing counters.\n"); 10031da177e4SLinus Torvalds } else { /* add to current RECON counter */ 10041da177e4SLinus Torvalds lp->last_recon = jiffies; 10051da177e4SLinus Torvalds lp->num_recons++; 10061da177e4SLinus Torvalds 1007a34c0932SJoe Perches arc_printk(D_DURING, dev, "recon: counter=%d, time=%lds, net=%d\n", 10081da177e4SLinus Torvalds lp->num_recons, 10091da177e4SLinus Torvalds (lp->last_recon - lp->first_recon) / HZ, 10101da177e4SLinus Torvalds lp->network_down); 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds /* if network is marked up; 10131da177e4SLinus Torvalds * and first_recon and last_recon are 60+ apart; 10141da177e4SLinus Torvalds * and the average no. of recons counted is 10151da177e4SLinus Torvalds * > RECON_THRESHOLD/min; 10161da177e4SLinus Torvalds * then print a warning message. 10171da177e4SLinus Torvalds */ 10188e95a202SJoe Perches if (!lp->network_down && 10198e95a202SJoe Perches (lp->last_recon - lp->first_recon) <= HZ * 60 && 10208e95a202SJoe Perches lp->num_recons >= RECON_THRESHOLD) { 10211da177e4SLinus Torvalds lp->network_down = 1; 1022a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "many reconfigurations detected: cabling problem?\n"); 10238e95a202SJoe Perches } else if (!lp->network_down && 10248e95a202SJoe Perches lp->last_recon - lp->first_recon > HZ * 60) { 1025d6d7d3edSJoe Perches /* reset counters if we've gone for 1026d6d7d3edSJoe Perches * over a minute. 1027d6d7d3edSJoe Perches */ 10281da177e4SLinus Torvalds lp->first_recon = lp->last_recon; 10291da177e4SLinus Torvalds lp->num_recons = 1; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds } 10329307b570SS.Caglar Onur } else if (lp->network_down && 10339307b570SS.Caglar Onur time_after(jiffies, lp->last_recon + HZ * 10)) { 10341da177e4SLinus Torvalds if (lp->network_down) 1035a34c0932SJoe Perches arc_printk(D_NORMAL, dev, "cabling restored?\n"); 10361da177e4SLinus Torvalds lp->first_recon = lp->last_recon = 0; 10371da177e4SLinus Torvalds lp->num_recons = lp->network_down = 0; 10381da177e4SLinus Torvalds 1039a34c0932SJoe Perches arc_printk(D_DURING, dev, "not recon: clearing counters anyway.\n"); 104059fbcbc6SMichael Grzeschik netif_carrier_on(dev); 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds 10437f5e760cSJoe Perches if (didsomething) 10441da177e4SLinus Torvalds retval |= IRQ_HANDLED; 10457f5e760cSJoe Perches } while (--boguscount && didsomething); 10461da177e4SLinus Torvalds 1047a34c0932SJoe Perches arc_printk(D_DURING, dev, "arcnet_interrupt complete (status=%Xh, count=%d)\n", 1048e15b0362SJoe Perches lp->hw.status(dev), boguscount); 1049a34c0932SJoe Perches arc_printk(D_DURING, dev, "\n"); 10501da177e4SLinus Torvalds 1051e15b0362SJoe Perches lp->hw.intmask(dev, 0); 10521da177e4SLinus Torvalds udelay(1); 1053e15b0362SJoe Perches lp->hw.intmask(dev, lp->intmask); 10541da177e4SLinus Torvalds 10555b858403SMichael Grzeschik spin_unlock_irqrestore(&lp->lock, flags); 10561da177e4SLinus Torvalds return retval; 10571da177e4SLinus Torvalds } 1058811eafc0SJoe Perches EXPORT_SYMBOL(arcnet_interrupt); 10591da177e4SLinus Torvalds 1060f2f0a16bSJoe Perches /* This is a generic packet receiver that calls arcnet??_rx depending on the 10611da177e4SLinus Torvalds * protocol ID found. 10621da177e4SLinus Torvalds */ 1063f03aa2d8SAdrian Bunk static void arcnet_rx(struct net_device *dev, int bufnum) 10641da177e4SLinus Torvalds { 1065454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 10661da177e4SLinus Torvalds struct archdr pkt; 10671da177e4SLinus Torvalds struct arc_rfc1201 *soft; 10681da177e4SLinus Torvalds int length, ofs; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds soft = &pkt.soft.rfc1201; 10711da177e4SLinus Torvalds 1072087d273cSDan Carpenter lp->hw.copy_from_card(dev, bufnum, 0, &pkt, ARC_HDR_SIZE); 10731da177e4SLinus Torvalds if (pkt.hard.offset[0]) { 10741da177e4SLinus Torvalds ofs = pkt.hard.offset[0]; 10751da177e4SLinus Torvalds length = 256 - ofs; 10761da177e4SLinus Torvalds } else { 10771da177e4SLinus Torvalds ofs = pkt.hard.offset[1]; 10781da177e4SLinus Torvalds length = 512 - ofs; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds /* get the full header, if possible */ 10827f5e760cSJoe Perches if (sizeof(pkt.soft) <= length) { 10831da177e4SLinus Torvalds lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft)); 10847f5e760cSJoe Perches } else { 10851da177e4SLinus Torvalds memset(&pkt.soft, 0, sizeof(pkt.soft)); 10861da177e4SLinus Torvalds lp->hw.copy_from_card(dev, bufnum, ofs, soft, length); 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 1089a34c0932SJoe Perches arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n", 10901da177e4SLinus Torvalds bufnum, pkt.hard.source, pkt.hard.dest, length); 10911da177e4SLinus Torvalds 10925803c512SStephen Hemminger dev->stats.rx_packets++; 10935803c512SStephen Hemminger dev->stats.rx_bytes += length + ARC_HDR_SIZE; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds /* call the right receiver for the protocol */ 10961da177e4SLinus Torvalds if (arc_proto_map[soft->proto]->is_ip) { 109772aeea48SJoe Perches if (BUGLVL(D_PROTO)) { 10981da177e4SLinus Torvalds struct ArcProto 10991da177e4SLinus Torvalds *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]], 11001da177e4SLinus Torvalds *newp = arc_proto_map[soft->proto]; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds if (oldp != newp) { 1103a34c0932SJoe Perches arc_printk(D_PROTO, dev, 11043b4e5551SJoe Perches "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n", 11053b4e5551SJoe Perches soft->proto, pkt.hard.source, 11061da177e4SLinus Torvalds newp->suffix, oldp->suffix); 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds /* broadcasts will always be done with the last-used encap. */ 11111da177e4SLinus Torvalds lp->default_proto[0] = soft->proto; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds /* in striking contrast, the following isn't a hack. */ 11141da177e4SLinus Torvalds lp->default_proto[pkt.hard.source] = soft->proto; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds /* call the protocol-specific receiver. */ 11171da177e4SLinus Torvalds arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length); 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds static void null_rx(struct net_device *dev, int bufnum, 11211da177e4SLinus Torvalds struct archdr *pkthdr, int length) 11221da177e4SLinus Torvalds { 1123a34c0932SJoe Perches arc_printk(D_PROTO, dev, 11241da177e4SLinus Torvalds "rx: don't know how to deal with proto %02Xh from host %02Xh.\n", 11251da177e4SLinus Torvalds pkthdr->soft.rfc1201.proto, pkthdr->hard.source); 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds static int null_build_header(struct sk_buff *skb, struct net_device *dev, 11291da177e4SLinus Torvalds unsigned short type, uint8_t daddr) 11301da177e4SLinus Torvalds { 1131454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 11321da177e4SLinus Torvalds 1133a34c0932SJoe Perches arc_printk(D_PROTO, dev, 11341da177e4SLinus Torvalds "tx: can't build header for encap %02Xh; load a protocol driver.\n", 11351da177e4SLinus Torvalds lp->default_proto[daddr]); 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds /* always fails */ 11381da177e4SLinus Torvalds return 0; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds /* the "do nothing" prepare_tx function warns that there's nothing to do. */ 11421da177e4SLinus Torvalds static int null_prepare_tx(struct net_device *dev, struct archdr *pkt, 11431da177e4SLinus Torvalds int length, int bufnum) 11441da177e4SLinus Torvalds { 1145454d7c9bSWang Chen struct arcnet_local *lp = netdev_priv(dev); 11461da177e4SLinus Torvalds struct arc_hardware newpkt; 11471da177e4SLinus Torvalds 1148a34c0932SJoe Perches arc_printk(D_PROTO, dev, "tx: no encap for this host; load a protocol driver.\n"); 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds /* send a packet to myself -- will never get received, of course */ 11511da177e4SLinus Torvalds newpkt.source = newpkt.dest = dev->dev_addr[0]; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds /* only one byte of actual data (and it's random) */ 11541da177e4SLinus Torvalds newpkt.offset[0] = 0xFF; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE); 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds return 1; /* done */ 11591da177e4SLinus Torvalds } 1160