arcnet.c 30 KB

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