arcnet.c 28 KB

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