arcnet.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Linux ARCnet driver - device-independent routines
  3. *
  4. * 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. * **********************
  23. *
  24. * The change log is now in a file called ChangeLog in this directory.
  25. *
  26. * Sources:
  27. * - Crynwr arcnet.com/arcether.com packet drivers.
  28. * - arcnet.c v0.00 dated 1/1/94 and apparently by
  29. * 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  44. #include <linux/module.h>
  45. #include <linux/types.h>
  46. #include <linux/delay.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/if_arp.h>
  49. #include <net/arp.h>
  50. #include <linux/init.h>
  51. #include <linux/jiffies.h>
  52. #include <linux/leds.h>
  53. #include "arcdevice.h"
  54. #include "com9026.h"
  55. /* "do nothing" functions for protocol drivers */
  56. static void null_rx(struct net_device *dev, int bufnum,
  57. struct archdr *pkthdr, int length);
  58. static int null_build_header(struct sk_buff *skb, struct net_device *dev,
  59. unsigned short type, uint8_t daddr);
  60. static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
  61. int length, int bufnum);
  62. static void arcnet_rx(struct net_device *dev, int bufnum);
  63. /* one ArcProto per possible proto ID. None of the elements of
  64. * arc_proto_map are allowed to be NULL; they will get set to
  65. * arc_proto_default instead. It also must not be NULL; if you would like
  66. * to set it to NULL, set it to &arc_proto_null instead.
  67. */
  68. struct ArcProto *arc_proto_map[256];
  69. EXPORT_SYMBOL(arc_proto_map);
  70. struct ArcProto *arc_proto_default;
  71. EXPORT_SYMBOL(arc_proto_default);
  72. struct ArcProto *arc_bcast_proto;
  73. EXPORT_SYMBOL(arc_bcast_proto);
  74. struct ArcProto *arc_raw_proto;
  75. EXPORT_SYMBOL(arc_raw_proto);
  76. static struct ArcProto arc_proto_null = {
  77. .suffix = '?',
  78. .mtu = XMTU,
  79. .is_ip = 0,
  80. .rx = null_rx,
  81. .build_header = null_build_header,
  82. .prepare_tx = null_prepare_tx,
  83. .continue_tx = NULL,
  84. .ack_tx = NULL
  85. };
  86. /* Exported function prototypes */
  87. int arcnet_debug = ARCNET_DEBUG;
  88. EXPORT_SYMBOL(arcnet_debug);
  89. /* Internal function prototypes */
  90. static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
  91. unsigned short type, const void *daddr,
  92. const void *saddr, unsigned len);
  93. static int go_tx(struct net_device *dev);
  94. static int debug = ARCNET_DEBUG;
  95. module_param(debug, int, 0);
  96. MODULE_LICENSE("GPL");
  97. static int __init arcnet_init(void)
  98. {
  99. int count;
  100. arcnet_debug = debug;
  101. pr_info("arcnet loaded\n");
  102. /* initialize the protocol map */
  103. arc_raw_proto = arc_proto_default = arc_bcast_proto = &arc_proto_null;
  104. for (count = 0; count < 256; count++)
  105. arc_proto_map[count] = arc_proto_default;
  106. if (BUGLVL(D_DURING))
  107. pr_info("struct sizes: %Zd %Zd %Zd %Zd %Zd\n",
  108. sizeof(struct arc_hardware),
  109. sizeof(struct arc_rfc1201),
  110. sizeof(struct arc_rfc1051),
  111. sizeof(struct arc_eth_encap),
  112. sizeof(struct archdr));
  113. return 0;
  114. }
  115. static void __exit arcnet_exit(void)
  116. {
  117. }
  118. module_init(arcnet_init);
  119. module_exit(arcnet_exit);
  120. /* Dump the contents of an sk_buff */
  121. #if ARCNET_DEBUG_MAX & D_SKB
  122. void arcnet_dump_skb(struct net_device *dev,
  123. struct sk_buff *skb, char *desc)
  124. {
  125. char hdr[32];
  126. /* dump the packet */
  127. snprintf(hdr, sizeof(hdr), "%6s:%s skb->data:", dev->name, desc);
  128. print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
  129. 16, 1, skb->data, skb->len, true);
  130. }
  131. EXPORT_SYMBOL(arcnet_dump_skb);
  132. #endif
  133. /* Dump the contents of an ARCnet buffer */
  134. #if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
  135. static void arcnet_dump_packet(struct net_device *dev, int bufnum,
  136. char *desc, int take_arcnet_lock)
  137. {
  138. struct arcnet_local *lp = netdev_priv(dev);
  139. int i, length;
  140. unsigned long flags = 0;
  141. static uint8_t buf[512];
  142. char hdr[32];
  143. /* hw.copy_from_card expects IRQ context so take the IRQ lock
  144. * to keep it single threaded
  145. */
  146. if (take_arcnet_lock)
  147. spin_lock_irqsave(&lp->lock, flags);
  148. lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
  149. if (take_arcnet_lock)
  150. spin_unlock_irqrestore(&lp->lock, flags);
  151. /* if the offset[0] byte is nonzero, this is a 256-byte packet */
  152. length = (buf[2] ? 256 : 512);
  153. /* dump the packet */
  154. snprintf(hdr, sizeof(hdr), "%6s:%s packet dump:", dev->name, desc);
  155. print_hex_dump(KERN_DEBUG, hdr, DUMP_PREFIX_OFFSET,
  156. 16, 1, buf, length, true);
  157. }
  158. #else
  159. #define arcnet_dump_packet(dev, bufnum, desc, take_arcnet_lock) do { } while (0)
  160. #endif
  161. /* Trigger a LED event in response to a ARCNET device event */
  162. void arcnet_led_event(struct net_device *dev, enum arcnet_led_event event)
  163. {
  164. struct arcnet_local *lp = netdev_priv(dev);
  165. unsigned long led_delay = 350;
  166. unsigned long tx_delay = 50;
  167. switch (event) {
  168. case ARCNET_LED_EVENT_RECON:
  169. led_trigger_blink_oneshot(lp->recon_led_trig,
  170. &led_delay, &led_delay, 0);
  171. break;
  172. case ARCNET_LED_EVENT_OPEN:
  173. led_trigger_event(lp->tx_led_trig, LED_OFF);
  174. led_trigger_event(lp->recon_led_trig, LED_OFF);
  175. break;
  176. case ARCNET_LED_EVENT_STOP:
  177. led_trigger_event(lp->tx_led_trig, LED_OFF);
  178. led_trigger_event(lp->recon_led_trig, LED_OFF);
  179. break;
  180. case ARCNET_LED_EVENT_TX:
  181. led_trigger_blink_oneshot(lp->tx_led_trig,
  182. &tx_delay, &tx_delay, 0);
  183. break;
  184. }
  185. }
  186. EXPORT_SYMBOL_GPL(arcnet_led_event);
  187. static void arcnet_led_release(struct device *gendev, void *res)
  188. {
  189. struct arcnet_local *lp = netdev_priv(to_net_dev(gendev));
  190. led_trigger_unregister_simple(lp->tx_led_trig);
  191. led_trigger_unregister_simple(lp->recon_led_trig);
  192. }
  193. /* Register ARCNET LED triggers for a arcnet device
  194. *
  195. * This is normally called from a driver's probe function
  196. */
  197. void devm_arcnet_led_init(struct net_device *netdev, int index, int subid)
  198. {
  199. struct arcnet_local *lp = netdev_priv(netdev);
  200. void *res;
  201. res = devres_alloc(arcnet_led_release, 0, GFP_KERNEL);
  202. if (!res) {
  203. netdev_err(netdev, "cannot register LED triggers\n");
  204. return;
  205. }
  206. snprintf(lp->tx_led_trig_name, sizeof(lp->tx_led_trig_name),
  207. "arc%d-%d-tx", index, subid);
  208. snprintf(lp->recon_led_trig_name, sizeof(lp->recon_led_trig_name),
  209. "arc%d-%d-recon", index, subid);
  210. led_trigger_register_simple(lp->tx_led_trig_name,
  211. &lp->tx_led_trig);
  212. led_trigger_register_simple(lp->recon_led_trig_name,
  213. &lp->recon_led_trig);
  214. devres_add(&netdev->dev, res);
  215. }
  216. EXPORT_SYMBOL_GPL(devm_arcnet_led_init);
  217. /* Unregister a protocol driver from the arc_proto_map. Protocol drivers
  218. * are responsible for registering themselves, but the unregister routine
  219. * is pretty generic so we'll do it here.
  220. */
  221. void arcnet_unregister_proto(struct ArcProto *proto)
  222. {
  223. int count;
  224. if (arc_proto_default == proto)
  225. arc_proto_default = &arc_proto_null;
  226. if (arc_bcast_proto == proto)
  227. arc_bcast_proto = arc_proto_default;
  228. if (arc_raw_proto == proto)
  229. arc_raw_proto = arc_proto_default;
  230. for (count = 0; count < 256; count++) {
  231. if (arc_proto_map[count] == proto)
  232. arc_proto_map[count] = arc_proto_default;
  233. }
  234. }
  235. EXPORT_SYMBOL(arcnet_unregister_proto);
  236. /* Add a buffer to the queue. Only the interrupt handler is allowed to do
  237. * this, unless interrupts are disabled.
  238. *
  239. * Note: we don't check for a full queue, since there aren't enough buffers
  240. * to more than fill it.
  241. */
  242. static void release_arcbuf(struct net_device *dev, int bufnum)
  243. {
  244. struct arcnet_local *lp = netdev_priv(dev);
  245. int i;
  246. lp->buf_queue[lp->first_free_buf++] = bufnum;
  247. lp->first_free_buf %= 5;
  248. if (BUGLVL(D_DURING)) {
  249. arc_printk(D_DURING, dev, "release_arcbuf: freed #%d; buffer queue is now: ",
  250. bufnum);
  251. for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
  252. arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
  253. arc_cont(D_DURING, "\n");
  254. }
  255. }
  256. /* Get a buffer from the queue.
  257. * If this returns -1, there are no buffers available.
  258. */
  259. static int get_arcbuf(struct net_device *dev)
  260. {
  261. struct arcnet_local *lp = netdev_priv(dev);
  262. int buf = -1, i;
  263. if (!atomic_dec_and_test(&lp->buf_lock)) {
  264. /* already in this function */
  265. arc_printk(D_NORMAL, dev, "get_arcbuf: overlap (%d)!\n",
  266. lp->buf_lock.counter);
  267. } else { /* we can continue */
  268. if (lp->next_buf >= 5)
  269. lp->next_buf -= 5;
  270. if (lp->next_buf == lp->first_free_buf) {
  271. arc_printk(D_NORMAL, dev, "get_arcbuf: BUG: no buffers are available??\n");
  272. } else {
  273. buf = lp->buf_queue[lp->next_buf++];
  274. lp->next_buf %= 5;
  275. }
  276. }
  277. if (BUGLVL(D_DURING)) {
  278. arc_printk(D_DURING, dev, "get_arcbuf: got #%d; buffer queue is now: ",
  279. buf);
  280. for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
  281. arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
  282. arc_cont(D_DURING, "\n");
  283. }
  284. atomic_inc(&lp->buf_lock);
  285. return buf;
  286. }
  287. static int choose_mtu(void)
  288. {
  289. int count, mtu = 65535;
  290. /* choose the smallest MTU of all available encaps */
  291. for (count = 0; count < 256; count++) {
  292. if (arc_proto_map[count] != &arc_proto_null &&
  293. arc_proto_map[count]->mtu < mtu) {
  294. mtu = arc_proto_map[count]->mtu;
  295. }
  296. }
  297. return mtu == 65535 ? XMTU : mtu;
  298. }
  299. static const struct header_ops arcnet_header_ops = {
  300. .create = arcnet_header,
  301. };
  302. static const struct net_device_ops arcnet_netdev_ops = {
  303. .ndo_open = arcnet_open,
  304. .ndo_stop = arcnet_close,
  305. .ndo_start_xmit = arcnet_send_packet,
  306. .ndo_tx_timeout = arcnet_timeout,
  307. };
  308. /* Setup a struct device for ARCnet. */
  309. static void arcdev_setup(struct net_device *dev)
  310. {
  311. dev->type = ARPHRD_ARCNET;
  312. dev->netdev_ops = &arcnet_netdev_ops;
  313. dev->header_ops = &arcnet_header_ops;
  314. dev->hard_header_len = sizeof(struct arc_hardware);
  315. dev->mtu = choose_mtu();
  316. dev->addr_len = ARCNET_ALEN;
  317. dev->tx_queue_len = 100;
  318. dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
  319. dev->watchdog_timeo = TX_TIMEOUT;
  320. /* New-style flags. */
  321. dev->flags = IFF_BROADCAST;
  322. }
  323. static void arcnet_timer(unsigned long data)
  324. {
  325. struct net_device *dev = (struct net_device *)data;
  326. if (!netif_carrier_ok(dev)) {
  327. netif_carrier_on(dev);
  328. netdev_info(dev, "link up\n");
  329. }
  330. }
  331. struct net_device *alloc_arcdev(const char *name)
  332. {
  333. struct net_device *dev;
  334. dev = alloc_netdev(sizeof(struct arcnet_local),
  335. name && *name ? name : "arc%d", NET_NAME_UNKNOWN,
  336. arcdev_setup);
  337. if (dev) {
  338. struct arcnet_local *lp = netdev_priv(dev);
  339. spin_lock_init(&lp->lock);
  340. init_timer(&lp->timer);
  341. lp->timer.data = (unsigned long) dev;
  342. lp->timer.function = arcnet_timer;
  343. }
  344. return dev;
  345. }
  346. EXPORT_SYMBOL(alloc_arcdev);
  347. /* Open/initialize the board. This is called sometime after booting when
  348. * the 'ifconfig' program is run.
  349. *
  350. * This routine should set everything up anew at each open, even registers
  351. * that "should" only need to be set once at boot, so that there is
  352. * non-reboot way to recover if something goes wrong.
  353. */
  354. int arcnet_open(struct net_device *dev)
  355. {
  356. struct arcnet_local *lp = netdev_priv(dev);
  357. int count, newmtu, error;
  358. arc_printk(D_INIT, dev, "opened.");
  359. if (!try_module_get(lp->hw.owner))
  360. return -ENODEV;
  361. if (BUGLVL(D_PROTO)) {
  362. arc_printk(D_PROTO, dev, "protocol map (default is '%c'): ",
  363. arc_proto_default->suffix);
  364. for (count = 0; count < 256; count++)
  365. arc_cont(D_PROTO, "%c", arc_proto_map[count]->suffix);
  366. arc_cont(D_PROTO, "\n");
  367. }
  368. arc_printk(D_INIT, dev, "arcnet_open: resetting card.\n");
  369. /* try to put the card in a defined state - if it fails the first
  370. * time, actually reset it.
  371. */
  372. error = -ENODEV;
  373. if (lp->hw.reset(dev, 0) && lp->hw.reset(dev, 1))
  374. goto out_module_put;
  375. newmtu = choose_mtu();
  376. if (newmtu < dev->mtu)
  377. dev->mtu = newmtu;
  378. arc_printk(D_INIT, dev, "arcnet_open: mtu: %d.\n", dev->mtu);
  379. /* autodetect the encapsulation for each host. */
  380. memset(lp->default_proto, 0, sizeof(lp->default_proto));
  381. /* the broadcast address is special - use the 'bcast' protocol */
  382. for (count = 0; count < 256; count++) {
  383. if (arc_proto_map[count] == arc_bcast_proto) {
  384. lp->default_proto[0] = count;
  385. break;
  386. }
  387. }
  388. /* initialize buffers */
  389. atomic_set(&lp->buf_lock, 1);
  390. lp->next_buf = lp->first_free_buf = 0;
  391. release_arcbuf(dev, 0);
  392. release_arcbuf(dev, 1);
  393. release_arcbuf(dev, 2);
  394. release_arcbuf(dev, 3);
  395. lp->cur_tx = lp->next_tx = -1;
  396. lp->cur_rx = -1;
  397. lp->rfc1201.sequence = 1;
  398. /* bring up the hardware driver */
  399. if (lp->hw.open)
  400. lp->hw.open(dev);
  401. if (dev->dev_addr[0] == 0)
  402. arc_printk(D_NORMAL, dev, "WARNING! Station address 00 is reserved for broadcasts!\n");
  403. else if (dev->dev_addr[0] == 255)
  404. arc_printk(D_NORMAL, dev, "WARNING! Station address FF may confuse DOS networking programs!\n");
  405. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  406. if (lp->hw.status(dev) & RESETflag) {
  407. arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
  408. __FILE__, __LINE__, __func__);
  409. lp->hw.command(dev, CFLAGScmd | RESETclear);
  410. }
  411. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  412. /* make sure we're ready to receive IRQ's. */
  413. lp->hw.intmask(dev, 0);
  414. udelay(1); /* give it time to set the mask before
  415. * we reset it again. (may not even be
  416. * necessary)
  417. */
  418. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  419. lp->intmask = NORXflag | RECONflag;
  420. lp->hw.intmask(dev, lp->intmask);
  421. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  422. netif_carrier_off(dev);
  423. netif_start_queue(dev);
  424. mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000));
  425. arcnet_led_event(dev, ARCNET_LED_EVENT_OPEN);
  426. return 0;
  427. out_module_put:
  428. module_put(lp->hw.owner);
  429. return error;
  430. }
  431. EXPORT_SYMBOL(arcnet_open);
  432. /* The inverse routine to arcnet_open - shuts down the card. */
  433. int arcnet_close(struct net_device *dev)
  434. {
  435. struct arcnet_local *lp = netdev_priv(dev);
  436. arcnet_led_event(dev, ARCNET_LED_EVENT_STOP);
  437. del_timer_sync(&lp->timer);
  438. netif_stop_queue(dev);
  439. netif_carrier_off(dev);
  440. /* flush TX and disable RX */
  441. lp->hw.intmask(dev, 0);
  442. lp->hw.command(dev, NOTXcmd); /* stop transmit */
  443. lp->hw.command(dev, NORXcmd); /* disable receive */
  444. mdelay(1);
  445. /* shut down the card */
  446. lp->hw.close(dev);
  447. module_put(lp->hw.owner);
  448. return 0;
  449. }
  450. EXPORT_SYMBOL(arcnet_close);
  451. static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
  452. unsigned short type, const void *daddr,
  453. const void *saddr, unsigned len)
  454. {
  455. const struct arcnet_local *lp = netdev_priv(dev);
  456. uint8_t _daddr, proto_num;
  457. struct ArcProto *proto;
  458. arc_printk(D_DURING, dev,
  459. "create header from %d to %d; protocol %d (%Xh); size %u.\n",
  460. saddr ? *(uint8_t *)saddr : -1,
  461. daddr ? *(uint8_t *)daddr : -1,
  462. type, type, len);
  463. if (skb->len != 0 && len != skb->len)
  464. arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
  465. skb->len, len);
  466. /* Type is host order - ? */
  467. if (type == ETH_P_ARCNET) {
  468. proto = arc_raw_proto;
  469. arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n",
  470. proto->suffix);
  471. _daddr = daddr ? *(uint8_t *)daddr : 0;
  472. } else if (!daddr) {
  473. /* if the dest addr isn't provided, we can't choose an
  474. * encapsulation! Store the packet type (eg. ETH_P_IP)
  475. * for now, and we'll push on a real header when we do
  476. * rebuild_header.
  477. */
  478. *(uint16_t *)skb_push(skb, 2) = type;
  479. /* XXX: Why not use skb->mac_len? */
  480. if (skb->network_header - skb->mac_header != 2)
  481. arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! diff (%u) is not 2!\n",
  482. skb->network_header - skb->mac_header);
  483. return -2; /* return error -- can't transmit yet! */
  484. } else {
  485. /* otherwise, we can just add the header as usual. */
  486. _daddr = *(uint8_t *)daddr;
  487. proto_num = lp->default_proto[_daddr];
  488. proto = arc_proto_map[proto_num];
  489. arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n",
  490. proto_num, proto->suffix);
  491. if (proto == &arc_proto_null && arc_bcast_proto != proto) {
  492. arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n",
  493. arc_bcast_proto->suffix);
  494. proto = arc_bcast_proto;
  495. }
  496. }
  497. return proto->build_header(skb, dev, type, _daddr);
  498. }
  499. /* Called by the kernel in order to transmit a packet. */
  500. netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
  501. struct net_device *dev)
  502. {
  503. struct arcnet_local *lp = netdev_priv(dev);
  504. struct archdr *pkt;
  505. struct arc_rfc1201 *soft;
  506. struct ArcProto *proto;
  507. int txbuf;
  508. unsigned long flags;
  509. int retval;
  510. arc_printk(D_DURING, dev,
  511. "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
  512. lp->hw.status(dev), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
  513. pkt = (struct archdr *)skb->data;
  514. soft = &pkt->soft.rfc1201;
  515. proto = arc_proto_map[soft->proto];
  516. arc_printk(D_SKB_SIZE, dev, "skb: transmitting %d bytes to %02X\n",
  517. skb->len, pkt->hard.dest);
  518. if (BUGLVL(D_SKB))
  519. arcnet_dump_skb(dev, skb, "tx");
  520. /* fits in one packet? */
  521. if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
  522. arc_printk(D_NORMAL, dev, "fixme: packet too large: compensating badly!\n");
  523. dev_kfree_skb(skb);
  524. return NETDEV_TX_OK; /* don't try again */
  525. }
  526. /* We're busy transmitting a packet... */
  527. netif_stop_queue(dev);
  528. spin_lock_irqsave(&lp->lock, flags);
  529. lp->hw.intmask(dev, 0);
  530. if (lp->next_tx == -1)
  531. txbuf = get_arcbuf(dev);
  532. else
  533. txbuf = -1;
  534. if (txbuf != -1) {
  535. if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
  536. !proto->ack_tx) {
  537. /* done right away and we don't want to acknowledge
  538. * the package later - forget about it now
  539. */
  540. dev->stats.tx_bytes += skb->len;
  541. dev_kfree_skb(skb);
  542. } else {
  543. /* do it the 'split' way */
  544. lp->outgoing.proto = proto;
  545. lp->outgoing.skb = skb;
  546. lp->outgoing.pkt = pkt;
  547. if (proto->continue_tx &&
  548. proto->continue_tx(dev, txbuf)) {
  549. arc_printk(D_NORMAL, dev,
  550. "bug! continue_tx finished the first time! (proto='%c')\n",
  551. proto->suffix);
  552. }
  553. }
  554. retval = NETDEV_TX_OK;
  555. lp->next_tx = txbuf;
  556. } else {
  557. retval = NETDEV_TX_BUSY;
  558. }
  559. arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
  560. __FILE__, __LINE__, __func__, lp->hw.status(dev));
  561. /* make sure we didn't ignore a TX IRQ while we were in here */
  562. lp->hw.intmask(dev, 0);
  563. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  564. lp->intmask |= TXFREEflag | EXCNAKflag;
  565. lp->hw.intmask(dev, lp->intmask);
  566. arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
  567. __FILE__, __LINE__, __func__, lp->hw.status(dev));
  568. arcnet_led_event(dev, ARCNET_LED_EVENT_TX);
  569. spin_unlock_irqrestore(&lp->lock, flags);
  570. return retval; /* no need to try again */
  571. }
  572. EXPORT_SYMBOL(arcnet_send_packet);
  573. /* Actually start transmitting a packet that was loaded into a buffer
  574. * by prepare_tx. This should _only_ be called by the interrupt handler.
  575. */
  576. static int go_tx(struct net_device *dev)
  577. {
  578. struct arcnet_local *lp = netdev_priv(dev);
  579. arc_printk(D_DURING, dev, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
  580. lp->hw.status(dev), lp->intmask, lp->next_tx, lp->cur_tx);
  581. if (lp->cur_tx != -1 || lp->next_tx == -1)
  582. return 0;
  583. if (BUGLVL(D_TX))
  584. arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
  585. lp->cur_tx = lp->next_tx;
  586. lp->next_tx = -1;
  587. /* start sending */
  588. lp->hw.command(dev, TXcmd | (lp->cur_tx << 3));
  589. dev->stats.tx_packets++;
  590. lp->lasttrans_dest = lp->lastload_dest;
  591. lp->lastload_dest = 0;
  592. lp->excnak_pending = 0;
  593. lp->intmask |= TXFREEflag | EXCNAKflag;
  594. return 1;
  595. }
  596. /* Called by the kernel when transmit times out */
  597. void arcnet_timeout(struct net_device *dev)
  598. {
  599. unsigned long flags;
  600. struct arcnet_local *lp = netdev_priv(dev);
  601. int status = lp->hw.status(dev);
  602. char *msg;
  603. spin_lock_irqsave(&lp->lock, flags);
  604. if (status & TXFREEflag) { /* transmit _DID_ finish */
  605. msg = " - missed IRQ?";
  606. } else {
  607. msg = "";
  608. dev->stats.tx_aborted_errors++;
  609. lp->timed_out = 1;
  610. lp->hw.command(dev, NOTXcmd | (lp->cur_tx << 3));
  611. }
  612. dev->stats.tx_errors++;
  613. /* make sure we didn't miss a TX or a EXC NAK IRQ */
  614. lp->hw.intmask(dev, 0);
  615. lp->intmask |= TXFREEflag | EXCNAKflag;
  616. lp->hw.intmask(dev, lp->intmask);
  617. spin_unlock_irqrestore(&lp->lock, flags);
  618. if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
  619. arc_printk(D_EXTRA, dev, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
  620. msg, status, lp->intmask, lp->lasttrans_dest);
  621. lp->last_timeout = jiffies;
  622. }
  623. if (lp->cur_tx == -1)
  624. netif_wake_queue(dev);
  625. }
  626. EXPORT_SYMBOL(arcnet_timeout);
  627. /* The typical workload of the driver: Handle the network interface
  628. * interrupts. Establish which device needs attention, and call the correct
  629. * chipset interrupt handler.
  630. */
  631. irqreturn_t arcnet_interrupt(int irq, void *dev_id)
  632. {
  633. struct net_device *dev = dev_id;
  634. struct arcnet_local *lp;
  635. int recbuf, status, diagstatus, didsomething, boguscount;
  636. int retval = IRQ_NONE;
  637. arc_printk(D_DURING, dev, "\n");
  638. arc_printk(D_DURING, dev, "in arcnet_interrupt\n");
  639. lp = netdev_priv(dev);
  640. BUG_ON(!lp);
  641. spin_lock(&lp->lock);
  642. /* RESET flag was enabled - if device is not running, we must
  643. * clear it right away (but nothing else).
  644. */
  645. if (!netif_running(dev)) {
  646. if (lp->hw.status(dev) & RESETflag)
  647. lp->hw.command(dev, CFLAGScmd | RESETclear);
  648. lp->hw.intmask(dev, 0);
  649. spin_unlock(&lp->lock);
  650. return retval;
  651. }
  652. arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
  653. lp->hw.status(dev), lp->intmask);
  654. boguscount = 5;
  655. do {
  656. status = lp->hw.status(dev);
  657. diagstatus = (status >> 8) & 0xFF;
  658. arc_printk(D_DEBUG, dev, "%s: %d: %s: status=%x\n",
  659. __FILE__, __LINE__, __func__, status);
  660. didsomething = 0;
  661. /* RESET flag was enabled - card is resetting and if RX is
  662. * disabled, it's NOT because we just got a packet.
  663. *
  664. * The card is in an undefined state.
  665. * Clear it out and start over.
  666. */
  667. if (status & RESETflag) {
  668. arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n",
  669. status);
  670. arcnet_close(dev);
  671. arcnet_open(dev);
  672. /* get out of the interrupt handler! */
  673. break;
  674. }
  675. /* RX is inhibited - we must have received something.
  676. * Prepare to receive into the next buffer.
  677. *
  678. * We don't actually copy the received packet from the card
  679. * until after the transmit handler runs (and possibly
  680. * launches the next tx); this should improve latency slightly
  681. * if we get both types of interrupts at once.
  682. */
  683. recbuf = -1;
  684. if (status & lp->intmask & NORXflag) {
  685. recbuf = lp->cur_rx;
  686. arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n",
  687. recbuf, status);
  688. lp->cur_rx = get_arcbuf(dev);
  689. if (lp->cur_rx != -1) {
  690. arc_printk(D_DURING, dev, "enabling receive to buffer #%d\n",
  691. lp->cur_rx);
  692. lp->hw.command(dev, RXcmd | (lp->cur_rx << 3) | RXbcasts);
  693. }
  694. didsomething++;
  695. }
  696. if ((diagstatus & EXCNAKflag)) {
  697. arc_printk(D_DURING, dev, "EXCNAK IRQ (diagstat=%Xh)\n",
  698. diagstatus);
  699. lp->hw.command(dev, NOTXcmd); /* disable transmit */
  700. lp->excnak_pending = 1;
  701. lp->hw.command(dev, EXCNAKclear);
  702. lp->intmask &= ~(EXCNAKflag);
  703. didsomething++;
  704. }
  705. /* a transmit finished, and we're interested in it. */
  706. if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
  707. lp->intmask &= ~(TXFREEflag | EXCNAKflag);
  708. arc_printk(D_DURING, dev, "TX IRQ (stat=%Xh)\n",
  709. status);
  710. if (lp->cur_tx != -1 && !lp->timed_out) {
  711. if (!(status & TXACKflag)) {
  712. if (lp->lasttrans_dest != 0) {
  713. arc_printk(D_EXTRA, dev,
  714. "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
  715. status,
  716. lp->lasttrans_dest);
  717. dev->stats.tx_errors++;
  718. dev->stats.tx_carrier_errors++;
  719. } else {
  720. arc_printk(D_DURING, dev,
  721. "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
  722. status,
  723. lp->lasttrans_dest);
  724. }
  725. }
  726. if (lp->outgoing.proto &&
  727. lp->outgoing.proto->ack_tx) {
  728. int ackstatus;
  729. if (status & TXACKflag)
  730. ackstatus = 2;
  731. else if (lp->excnak_pending)
  732. ackstatus = 1;
  733. else
  734. ackstatus = 0;
  735. lp->outgoing.proto
  736. ->ack_tx(dev, ackstatus);
  737. }
  738. }
  739. if (lp->cur_tx != -1)
  740. release_arcbuf(dev, lp->cur_tx);
  741. lp->cur_tx = -1;
  742. lp->timed_out = 0;
  743. didsomething++;
  744. /* send another packet if there is one */
  745. go_tx(dev);
  746. /* continue a split packet, if any */
  747. if (lp->outgoing.proto &&
  748. lp->outgoing.proto->continue_tx) {
  749. int txbuf = get_arcbuf(dev);
  750. if (txbuf != -1) {
  751. if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
  752. /* that was the last segment */
  753. dev->stats.tx_bytes += lp->outgoing.skb->len;
  754. if (!lp->outgoing.proto->ack_tx) {
  755. dev_kfree_skb_irq(lp->outgoing.skb);
  756. lp->outgoing.proto = NULL;
  757. }
  758. }
  759. lp->next_tx = txbuf;
  760. }
  761. }
  762. /* inform upper layers of idleness, if necessary */
  763. if (lp->cur_tx == -1)
  764. netif_wake_queue(dev);
  765. }
  766. /* now process the received packet, if any */
  767. if (recbuf != -1) {
  768. if (BUGLVL(D_RX))
  769. arcnet_dump_packet(dev, recbuf, "rx irq", 0);
  770. arcnet_rx(dev, recbuf);
  771. release_arcbuf(dev, recbuf);
  772. didsomething++;
  773. }
  774. if (status & lp->intmask & RECONflag) {
  775. lp->hw.command(dev, CFLAGScmd | CONFIGclear);
  776. dev->stats.tx_carrier_errors++;
  777. arc_printk(D_RECON, dev, "Network reconfiguration detected (status=%Xh)\n",
  778. status);
  779. if (netif_carrier_ok(dev)) {
  780. netif_carrier_off(dev);
  781. netdev_info(dev, "link down\n");
  782. }
  783. mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000));
  784. arcnet_led_event(dev, ARCNET_LED_EVENT_RECON);
  785. /* MYRECON bit is at bit 7 of diagstatus */
  786. if (diagstatus & 0x80)
  787. arc_printk(D_RECON, dev, "Put out that recon myself\n");
  788. /* is the RECON info empty or old? */
  789. if (!lp->first_recon || !lp->last_recon ||
  790. time_after(jiffies, lp->last_recon + HZ * 10)) {
  791. if (lp->network_down)
  792. arc_printk(D_NORMAL, dev, "reconfiguration detected: cabling restored?\n");
  793. lp->first_recon = lp->last_recon = jiffies;
  794. lp->num_recons = lp->network_down = 0;
  795. arc_printk(D_DURING, dev, "recon: clearing counters.\n");
  796. } else { /* add to current RECON counter */
  797. lp->last_recon = jiffies;
  798. lp->num_recons++;
  799. arc_printk(D_DURING, dev, "recon: counter=%d, time=%lds, net=%d\n",
  800. lp->num_recons,
  801. (lp->last_recon - lp->first_recon) / HZ,
  802. lp->network_down);
  803. /* if network is marked up;
  804. * and first_recon and last_recon are 60+ apart;
  805. * and the average no. of recons counted is
  806. * > RECON_THRESHOLD/min;
  807. * then print a warning message.
  808. */
  809. if (!lp->network_down &&
  810. (lp->last_recon - lp->first_recon) <= HZ * 60 &&
  811. lp->num_recons >= RECON_THRESHOLD) {
  812. lp->network_down = 1;
  813. arc_printk(D_NORMAL, dev, "many reconfigurations detected: cabling problem?\n");
  814. } else if (!lp->network_down &&
  815. lp->last_recon - lp->first_recon > HZ * 60) {
  816. /* reset counters if we've gone for
  817. * over a minute.
  818. */
  819. lp->first_recon = lp->last_recon;
  820. lp->num_recons = 1;
  821. }
  822. }
  823. } else if (lp->network_down &&
  824. time_after(jiffies, lp->last_recon + HZ * 10)) {
  825. if (lp->network_down)
  826. arc_printk(D_NORMAL, dev, "cabling restored?\n");
  827. lp->first_recon = lp->last_recon = 0;
  828. lp->num_recons = lp->network_down = 0;
  829. arc_printk(D_DURING, dev, "not recon: clearing counters anyway.\n");
  830. netif_carrier_on(dev);
  831. }
  832. if (didsomething)
  833. retval |= IRQ_HANDLED;
  834. } while (--boguscount && didsomething);
  835. arc_printk(D_DURING, dev, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
  836. lp->hw.status(dev), boguscount);
  837. arc_printk(D_DURING, dev, "\n");
  838. lp->hw.intmask(dev, 0);
  839. udelay(1);
  840. lp->hw.intmask(dev, lp->intmask);
  841. spin_unlock(&lp->lock);
  842. return retval;
  843. }
  844. EXPORT_SYMBOL(arcnet_interrupt);
  845. /* This is a generic packet receiver that calls arcnet??_rx depending on the
  846. * protocol ID found.
  847. */
  848. static void arcnet_rx(struct net_device *dev, int bufnum)
  849. {
  850. struct arcnet_local *lp = netdev_priv(dev);
  851. struct archdr pkt;
  852. struct arc_rfc1201 *soft;
  853. int length, ofs;
  854. soft = &pkt.soft.rfc1201;
  855. lp->hw.copy_from_card(dev, bufnum, 0, &pkt, ARC_HDR_SIZE);
  856. if (pkt.hard.offset[0]) {
  857. ofs = pkt.hard.offset[0];
  858. length = 256 - ofs;
  859. } else {
  860. ofs = pkt.hard.offset[1];
  861. length = 512 - ofs;
  862. }
  863. /* get the full header, if possible */
  864. if (sizeof(pkt.soft) <= length) {
  865. lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
  866. } else {
  867. memset(&pkt.soft, 0, sizeof(pkt.soft));
  868. lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
  869. }
  870. arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
  871. bufnum, pkt.hard.source, pkt.hard.dest, length);
  872. dev->stats.rx_packets++;
  873. dev->stats.rx_bytes += length + ARC_HDR_SIZE;
  874. /* call the right receiver for the protocol */
  875. if (arc_proto_map[soft->proto]->is_ip) {
  876. if (BUGLVL(D_PROTO)) {
  877. struct ArcProto
  878. *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
  879. *newp = arc_proto_map[soft->proto];
  880. if (oldp != newp) {
  881. arc_printk(D_PROTO, dev,
  882. "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
  883. soft->proto, pkt.hard.source,
  884. newp->suffix, oldp->suffix);
  885. }
  886. }
  887. /* broadcasts will always be done with the last-used encap. */
  888. lp->default_proto[0] = soft->proto;
  889. /* in striking contrast, the following isn't a hack. */
  890. lp->default_proto[pkt.hard.source] = soft->proto;
  891. }
  892. /* call the protocol-specific receiver. */
  893. arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
  894. }
  895. static void null_rx(struct net_device *dev, int bufnum,
  896. struct archdr *pkthdr, int length)
  897. {
  898. arc_printk(D_PROTO, dev,
  899. "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
  900. pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
  901. }
  902. static int null_build_header(struct sk_buff *skb, struct net_device *dev,
  903. unsigned short type, uint8_t daddr)
  904. {
  905. struct arcnet_local *lp = netdev_priv(dev);
  906. arc_printk(D_PROTO, dev,
  907. "tx: can't build header for encap %02Xh; load a protocol driver.\n",
  908. lp->default_proto[daddr]);
  909. /* always fails */
  910. return 0;
  911. }
  912. /* the "do nothing" prepare_tx function warns that there's nothing to do. */
  913. static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
  914. int length, int bufnum)
  915. {
  916. struct arcnet_local *lp = netdev_priv(dev);
  917. struct arc_hardware newpkt;
  918. arc_printk(D_PROTO, dev, "tx: no encap for this host; load a protocol driver.\n");
  919. /* send a packet to myself -- will never get received, of course */
  920. newpkt.source = newpkt.dest = dev->dev_addr[0];
  921. /* only one byte of actual data (and it's random) */
  922. newpkt.offset[0] = 0xFF;
  923. lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
  924. return 1; /* done */
  925. }