arcnet.c 30 KB

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