arcnet.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. int i;
  142. printk(KERN_DEBUG "%6s: skb dump (%s) follows:", dev->name, desc);
  143. for (i = 0; i < skb->len; i++) {
  144. if (i % 16 == 0)
  145. printk("\n" KERN_DEBUG "[%04X] ", i);
  146. printk("%02X ", ((u_char *) skb->data)[i]);
  147. }
  148. printk("\n");
  149. }
  150. EXPORT_SYMBOL(arcnet_dump_skb);
  151. #endif
  152. /*
  153. * Dump the contents of an ARCnet buffer
  154. */
  155. #if (ARCNET_DEBUG_MAX & (D_RX | D_TX))
  156. static void arcnet_dump_packet(struct net_device *dev, int bufnum,
  157. char *desc, int take_arcnet_lock)
  158. {
  159. struct arcnet_local *lp = netdev_priv(dev);
  160. int i, length;
  161. unsigned long flags = 0;
  162. static uint8_t buf[512];
  163. /* hw.copy_from_card expects IRQ context so take the IRQ lock
  164. to keep it single threaded */
  165. if(take_arcnet_lock)
  166. spin_lock_irqsave(&lp->lock, flags);
  167. lp->hw.copy_from_card(dev, bufnum, 0, buf, 512);
  168. if(take_arcnet_lock)
  169. spin_unlock_irqrestore(&lp->lock, flags);
  170. /* if the offset[0] byte is nonzero, this is a 256-byte packet */
  171. length = (buf[2] ? 256 : 512);
  172. printk(KERN_DEBUG "%6s: packet dump (%s) follows:", dev->name, desc);
  173. for (i = 0; i < length; i++) {
  174. if (i % 16 == 0)
  175. printk("\n" KERN_DEBUG "[%04X] ", i);
  176. printk("%02X ", buf[i]);
  177. }
  178. printk("\n");
  179. }
  180. #else
  181. #define arcnet_dump_packet(dev, bufnum, desc,take_arcnet_lock) do { } while (0)
  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 = netdev_priv(dev);
  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 = netdev_priv(dev);
  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. static const struct header_ops arcnet_header_ops = {
  268. .create = arcnet_header,
  269. .rebuild = arcnet_rebuild_header,
  270. };
  271. static const struct net_device_ops arcnet_netdev_ops = {
  272. .ndo_open = arcnet_open,
  273. .ndo_stop = arcnet_close,
  274. .ndo_start_xmit = arcnet_send_packet,
  275. .ndo_tx_timeout = arcnet_timeout,
  276. };
  277. /* Setup a struct device for ARCnet. */
  278. static void arcdev_setup(struct net_device *dev)
  279. {
  280. dev->type = ARPHRD_ARCNET;
  281. dev->netdev_ops = &arcnet_netdev_ops;
  282. dev->header_ops = &arcnet_header_ops;
  283. dev->hard_header_len = sizeof(struct archdr);
  284. dev->mtu = choose_mtu();
  285. dev->addr_len = ARCNET_ALEN;
  286. dev->tx_queue_len = 100;
  287. dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
  288. dev->watchdog_timeo = TX_TIMEOUT;
  289. /* New-style flags. */
  290. dev->flags = IFF_BROADCAST;
  291. }
  292. struct net_device *alloc_arcdev(const char *name)
  293. {
  294. struct net_device *dev;
  295. dev = alloc_netdev(sizeof(struct arcnet_local),
  296. name && *name ? name : "arc%d", arcdev_setup);
  297. if(dev) {
  298. struct arcnet_local *lp = netdev_priv(dev);
  299. spin_lock_init(&lp->lock);
  300. }
  301. return dev;
  302. }
  303. /*
  304. * Open/initialize the board. This is called sometime after booting when
  305. * the 'ifconfig' program is run.
  306. *
  307. * This routine should set everything up anew at each open, even registers
  308. * that "should" only need to be set once at boot, so that there is
  309. * non-reboot way to recover if something goes wrong.
  310. */
  311. int arcnet_open(struct net_device *dev)
  312. {
  313. struct arcnet_local *lp = netdev_priv(dev);
  314. int count, newmtu, error;
  315. BUGMSG(D_INIT,"opened.");
  316. if (!try_module_get(lp->hw.owner))
  317. return -ENODEV;
  318. BUGLVL(D_PROTO) {
  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__,__func__);
  365. if (ASTATUS() & RESETflag) {
  366. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  367. ACOMMAND(CFLAGScmd | RESETclear);
  368. }
  369. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  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__,__func__);
  377. lp->intmask = NORXflag | RECONflag;
  378. AINTMASK(lp->intmask);
  379. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  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. int arcnet_close(struct net_device *dev)
  388. {
  389. struct arcnet_local *lp = netdev_priv(dev);
  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, const void *daddr,
  403. const void *saddr, unsigned len)
  404. {
  405. const struct arcnet_local *lp = netdev_priv(dev);
  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. /*
  430. * XXX: Why not use skb->mac_len?
  431. */
  432. if (skb->network_header - skb->mac_header != 2)
  433. BUGMSG(D_NORMAL, "arcnet_header: Yikes! diff (%d) is not 2!\n",
  434. (int)(skb->network_header - skb->mac_header));
  435. return -2; /* return error -- can't transmit yet! */
  436. }
  437. else {
  438. /* otherwise, we can just add the header as usual. */
  439. _daddr = *(uint8_t *) daddr;
  440. proto_num = lp->default_proto[_daddr];
  441. proto = arc_proto_map[proto_num];
  442. BUGMSG(D_DURING, "building header for %02Xh using protocol '%c'\n",
  443. proto_num, proto->suffix);
  444. if (proto == &arc_proto_null && arc_bcast_proto != proto) {
  445. BUGMSG(D_DURING, "actually, let's use '%c' instead.\n",
  446. arc_bcast_proto->suffix);
  447. proto = arc_bcast_proto;
  448. }
  449. }
  450. return proto->build_header(skb, dev, type, _daddr);
  451. }
  452. /*
  453. * Rebuild the ARCnet hard header. This is called after an ARP (or in the
  454. * future other address resolution) has completed on this sk_buff. We now
  455. * let ARP fill in the destination field.
  456. */
  457. static int arcnet_rebuild_header(struct sk_buff *skb)
  458. {
  459. struct net_device *dev = skb->dev;
  460. struct arcnet_local *lp = netdev_priv(dev);
  461. int status = 0; /* default is failure */
  462. unsigned short type;
  463. uint8_t daddr=0;
  464. struct ArcProto *proto;
  465. /*
  466. * XXX: Why not use skb->mac_len?
  467. */
  468. if (skb->network_header - skb->mac_header != 2) {
  469. BUGMSG(D_NORMAL,
  470. "rebuild_header: shouldn't be here! (hdrsize=%d)\n",
  471. (int)(skb->network_header - skb->mac_header));
  472. return 0;
  473. }
  474. type = *(uint16_t *) skb_pull(skb, 2);
  475. BUGMSG(D_DURING, "rebuild header for protocol %Xh\n", type);
  476. if (type == ETH_P_IP) {
  477. #ifdef CONFIG_INET
  478. BUGMSG(D_DURING, "rebuild header for ethernet protocol %Xh\n", type);
  479. status = arp_find(&daddr, skb) ? 1 : 0;
  480. BUGMSG(D_DURING, " rebuilt: dest is %d; protocol %Xh\n",
  481. daddr, type);
  482. #endif
  483. } else {
  484. BUGMSG(D_NORMAL,
  485. "I don't understand ethernet protocol %Xh addresses!\n", type);
  486. dev->stats.tx_errors++;
  487. dev->stats.tx_aborted_errors++;
  488. }
  489. /* if we couldn't resolve the address... give up. */
  490. if (!status)
  491. return 0;
  492. /* add the _real_ header this time! */
  493. proto = arc_proto_map[lp->default_proto[daddr]];
  494. proto->build_header(skb, dev, type, daddr);
  495. return 1; /* success */
  496. }
  497. /* Called by the kernel in order to transmit a packet. */
  498. int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev)
  499. {
  500. struct arcnet_local *lp = netdev_priv(dev);
  501. struct archdr *pkt;
  502. struct arc_rfc1201 *soft;
  503. struct ArcProto *proto;
  504. int txbuf;
  505. unsigned long flags;
  506. int freeskb, retval;
  507. BUGMSG(D_DURING,
  508. "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
  509. ASTATUS(), lp->cur_tx, lp->next_tx, skb->len,skb->protocol);
  510. pkt = (struct archdr *) skb->data;
  511. soft = &pkt->soft.rfc1201;
  512. proto = arc_proto_map[soft->proto];
  513. BUGMSG(D_SKB_SIZE, "skb: transmitting %d bytes to %02X\n",
  514. skb->len, pkt->hard.dest);
  515. BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "tx");
  516. /* fits in one packet? */
  517. if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
  518. BUGMSG(D_NORMAL, "fixme: packet too large: compensating badly!\n");
  519. dev_kfree_skb(skb);
  520. return NETDEV_TX_OK; /* don't try again */
  521. }
  522. /* We're busy transmitting a packet... */
  523. netif_stop_queue(dev);
  524. spin_lock_irqsave(&lp->lock, flags);
  525. AINTMASK(0);
  526. if(lp->next_tx == -1)
  527. txbuf = get_arcbuf(dev);
  528. else {
  529. txbuf = -1;
  530. }
  531. if (txbuf != -1) {
  532. if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
  533. !proto->ack_tx) {
  534. /* done right away and we don't want to acknowledge
  535. the package later - forget about it now */
  536. dev->stats.tx_bytes += skb->len;
  537. freeskb = 1;
  538. } else {
  539. /* do it the 'split' way */
  540. lp->outgoing.proto = proto;
  541. lp->outgoing.skb = skb;
  542. lp->outgoing.pkt = pkt;
  543. freeskb = 0;
  544. if (proto->continue_tx &&
  545. proto->continue_tx(dev, txbuf)) {
  546. BUGMSG(D_NORMAL,
  547. "bug! continue_tx finished the first time! "
  548. "(proto='%c')\n", proto->suffix);
  549. }
  550. }
  551. retval = NETDEV_TX_OK;
  552. dev->trans_start = jiffies;
  553. lp->next_tx = txbuf;
  554. } else {
  555. retval = NETDEV_TX_BUSY;
  556. freeskb = 0;
  557. }
  558. BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n",__FILE__,__LINE__,__func__,ASTATUS());
  559. /* make sure we didn't ignore a TX IRQ while we were in here */
  560. AINTMASK(0);
  561. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__func__);
  562. lp->intmask |= TXFREEflag|EXCNAKflag;
  563. AINTMASK(lp->intmask);
  564. BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n",__FILE__,__LINE__,__func__,ASTATUS());
  565. spin_unlock_irqrestore(&lp->lock, flags);
  566. if (freeskb) {
  567. dev_kfree_skb(skb);
  568. }
  569. return retval; /* no need to try again */
  570. }
  571. /*
  572. * Actually start transmitting a packet that was loaded into a buffer
  573. * by prepare_tx. This should _only_ be called by the interrupt handler.
  574. */
  575. static int go_tx(struct net_device *dev)
  576. {
  577. struct arcnet_local *lp = netdev_priv(dev);
  578. BUGMSG(D_DURING, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
  579. ASTATUS(), lp->intmask, lp->next_tx, lp->cur_tx);
  580. if (lp->cur_tx != -1 || lp->next_tx == -1)
  581. return 0;
  582. BUGLVL(D_TX) arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
  583. lp->cur_tx = lp->next_tx;
  584. lp->next_tx = -1;
  585. /* start sending */
  586. ACOMMAND(TXcmd | (lp->cur_tx << 3));
  587. dev->stats.tx_packets++;
  588. lp->lasttrans_dest = lp->lastload_dest;
  589. lp->lastload_dest = 0;
  590. lp->excnak_pending = 0;
  591. lp->intmask |= TXFREEflag|EXCNAKflag;
  592. return 1;
  593. }
  594. /* Called by the kernel when transmit times out */
  595. void arcnet_timeout(struct net_device *dev)
  596. {
  597. unsigned long flags;
  598. struct arcnet_local *lp = netdev_priv(dev);
  599. int status = ASTATUS();
  600. char *msg;
  601. spin_lock_irqsave(&lp->lock, flags);
  602. if (status & TXFREEflag) { /* transmit _DID_ finish */
  603. msg = " - missed IRQ?";
  604. } else {
  605. msg = "";
  606. dev->stats.tx_aborted_errors++;
  607. lp->timed_out = 1;
  608. ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
  609. }
  610. dev->stats.tx_errors++;
  611. /* make sure we didn't miss a TX or a EXC NAK IRQ */
  612. AINTMASK(0);
  613. lp->intmask |= TXFREEflag|EXCNAKflag;
  614. AINTMASK(lp->intmask);
  615. spin_unlock_irqrestore(&lp->lock, flags);
  616. if (time_after(jiffies, lp->last_timeout + 10*HZ)) {
  617. BUGMSG(D_EXTRA, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
  618. msg, status, lp->intmask, lp->lasttrans_dest);
  619. lp->last_timeout = jiffies;
  620. }
  621. if (lp->cur_tx == -1)
  622. netif_wake_queue(dev);
  623. }
  624. /*
  625. * The typical workload of the driver: Handle the network interface
  626. * interrupts. Establish which device needs attention, and call the correct
  627. * chipset interrupt handler.
  628. */
  629. irqreturn_t arcnet_interrupt(int irq, void *dev_id)
  630. {
  631. struct net_device *dev = dev_id;
  632. struct arcnet_local *lp;
  633. int recbuf, status, diagstatus, didsomething, boguscount;
  634. int retval = IRQ_NONE;
  635. BUGMSG(D_DURING, "\n");
  636. BUGMSG(D_DURING, "in arcnet_interrupt\n");
  637. lp = netdev_priv(dev);
  638. BUG_ON(!lp);
  639. spin_lock(&lp->lock);
  640. /*
  641. * RESET flag was enabled - if device is not running, we must clear it right
  642. * away (but nothing else).
  643. */
  644. if (!netif_running(dev)) {
  645. if (ASTATUS() & RESETflag)
  646. ACOMMAND(CFLAGScmd | RESETclear);
  647. AINTMASK(0);
  648. spin_unlock(&lp->lock);
  649. return IRQ_HANDLED;
  650. }
  651. BUGMSG(D_DURING, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
  652. ASTATUS(), lp->intmask);
  653. boguscount = 5;
  654. do {
  655. status = ASTATUS();
  656. diagstatus = (status >> 8) & 0xFF;
  657. BUGMSG(D_DEBUG, "%s: %d: %s: status=%x\n",
  658. __FILE__,__LINE__,__func__,status);
  659. didsomething = 0;
  660. /*
  661. * RESET flag was enabled - card is resetting and if RX is
  662. * disabled, it's NOT because we just got a packet.
  663. *
  664. * The card is in an undefined state. Clear it out and start over.
  665. */
  666. if (status & RESETflag) {
  667. BUGMSG(D_NORMAL, "spurious reset (status=%Xh)\n", status);
  668. arcnet_close(dev);
  669. arcnet_open(dev);
  670. /* get out of the interrupt handler! */
  671. break;
  672. }
  673. /*
  674. * RX is inhibited - we must have received something. Prepare to
  675. * receive into the next buffer.
  676. *
  677. * We don't actually copy the received packet from the card until
  678. * after the transmit handler runs (and possibly launches the next
  679. * tx); this should improve latency slightly if we get both types
  680. * of interrupts at once.
  681. */
  682. recbuf = -1;
  683. if (status & lp->intmask & NORXflag) {
  684. recbuf = lp->cur_rx;
  685. BUGMSG(D_DURING, "Buffer #%d: receive irq (status=%Xh)\n",
  686. recbuf, status);
  687. lp->cur_rx = get_arcbuf(dev);
  688. if (lp->cur_rx != -1) {
  689. BUGMSG(D_DURING, "enabling receive to buffer #%d\n",
  690. lp->cur_rx);
  691. ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
  692. }
  693. didsomething++;
  694. }
  695. if((diagstatus & EXCNAKflag)) {
  696. BUGMSG(D_DURING, "EXCNAK IRQ (diagstat=%Xh)\n",
  697. diagstatus);
  698. ACOMMAND(NOTXcmd); /* disable transmit */
  699. lp->excnak_pending = 1;
  700. ACOMMAND(EXCNAKclear);
  701. lp->intmask &= ~(EXCNAKflag);
  702. didsomething++;
  703. }
  704. /* a transmit finished, and we're interested in it. */
  705. if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
  706. lp->intmask &= ~(TXFREEflag|EXCNAKflag);
  707. BUGMSG(D_DURING, "TX IRQ (stat=%Xh)\n", status);
  708. if (lp->cur_tx != -1 && !lp->timed_out) {
  709. if(!(status & TXACKflag)) {
  710. if (lp->lasttrans_dest != 0) {
  711. BUGMSG(D_EXTRA,
  712. "transmit was not acknowledged! "
  713. "(status=%Xh, dest=%02Xh)\n",
  714. status, lp->lasttrans_dest);
  715. dev->stats.tx_errors++;
  716. dev->stats.tx_carrier_errors++;
  717. } else {
  718. BUGMSG(D_DURING,
  719. "broadcast was not acknowledged; that's normal "
  720. "(status=%Xh, dest=%02Xh)\n",
  721. status, lp->lasttrans_dest);
  722. }
  723. }
  724. if (lp->outgoing.proto &&
  725. lp->outgoing.proto->ack_tx) {
  726. int ackstatus;
  727. if(status & TXACKflag)
  728. ackstatus=2;
  729. else if(lp->excnak_pending)
  730. ackstatus=1;
  731. else
  732. ackstatus=0;
  733. lp->outgoing.proto
  734. ->ack_tx(dev, ackstatus);
  735. }
  736. }
  737. if (lp->cur_tx != -1)
  738. release_arcbuf(dev, lp->cur_tx);
  739. lp->cur_tx = -1;
  740. lp->timed_out = 0;
  741. didsomething++;
  742. /* send another packet if there is one */
  743. go_tx(dev);
  744. /* continue a split packet, if any */
  745. if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
  746. int txbuf = get_arcbuf(dev);
  747. if (txbuf != -1) {
  748. if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
  749. /* that was the last segment */
  750. dev->stats.tx_bytes += lp->outgoing.skb->len;
  751. if(!lp->outgoing.proto->ack_tx)
  752. {
  753. dev_kfree_skb_irq(lp->outgoing.skb);
  754. lp->outgoing.proto = NULL;
  755. }
  756. }
  757. lp->next_tx = txbuf;
  758. }
  759. }
  760. /* inform upper layers of idleness, if necessary */
  761. if (lp->cur_tx == -1)
  762. netif_wake_queue(dev);
  763. }
  764. /* now process the received packet, if any */
  765. if (recbuf != -1) {
  766. BUGLVL(D_RX) arcnet_dump_packet(dev, recbuf, "rx irq", 0);
  767. arcnet_rx(dev, recbuf);
  768. release_arcbuf(dev, recbuf);
  769. didsomething++;
  770. }
  771. if (status & lp->intmask & RECONflag) {
  772. ACOMMAND(CFLAGScmd | CONFIGclear);
  773. dev->stats.tx_carrier_errors++;
  774. BUGMSG(D_RECON, "Network reconfiguration detected (status=%Xh)\n",
  775. status);
  776. /* MYRECON bit is at bit 7 of diagstatus */
  777. if(diagstatus & 0x80)
  778. BUGMSG(D_RECON,"Put out that recon myself\n");
  779. /* is the RECON info empty or old? */
  780. if (!lp->first_recon || !lp->last_recon ||
  781. time_after(jiffies, lp->last_recon + HZ * 10)) {
  782. if (lp->network_down)
  783. BUGMSG(D_NORMAL, "reconfiguration detected: cabling restored?\n");
  784. lp->first_recon = lp->last_recon = jiffies;
  785. lp->num_recons = lp->network_down = 0;
  786. BUGMSG(D_DURING, "recon: clearing counters.\n");
  787. } else { /* add to current RECON counter */
  788. lp->last_recon = jiffies;
  789. lp->num_recons++;
  790. BUGMSG(D_DURING, "recon: counter=%d, time=%lds, net=%d\n",
  791. lp->num_recons,
  792. (lp->last_recon - lp->first_recon) / HZ,
  793. lp->network_down);
  794. /* if network is marked up;
  795. * and first_recon and last_recon are 60+ apart;
  796. * and the average no. of recons counted is
  797. * > RECON_THRESHOLD/min;
  798. * then print a warning message.
  799. */
  800. if (!lp->network_down
  801. && (lp->last_recon - lp->first_recon) <= HZ * 60
  802. && lp->num_recons >= RECON_THRESHOLD) {
  803. lp->network_down = 1;
  804. BUGMSG(D_NORMAL, "many reconfigurations detected: cabling problem?\n");
  805. } else if (!lp->network_down
  806. && lp->last_recon - lp->first_recon > HZ * 60) {
  807. /* reset counters if we've gone for over a minute. */
  808. lp->first_recon = lp->last_recon;
  809. lp->num_recons = 1;
  810. }
  811. }
  812. } else if (lp->network_down &&
  813. time_after(jiffies, lp->last_recon + HZ * 10)) {
  814. if (lp->network_down)
  815. BUGMSG(D_NORMAL, "cabling restored?\n");
  816. lp->first_recon = lp->last_recon = 0;
  817. lp->num_recons = lp->network_down = 0;
  818. BUGMSG(D_DURING, "not recon: clearing counters anyway.\n");
  819. }
  820. if(didsomething) {
  821. retval |= IRQ_HANDLED;
  822. }
  823. }
  824. while (--boguscount && didsomething);
  825. BUGMSG(D_DURING, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
  826. ASTATUS(), boguscount);
  827. BUGMSG(D_DURING, "\n");
  828. AINTMASK(0);
  829. udelay(1);
  830. AINTMASK(lp->intmask);
  831. spin_unlock(&lp->lock);
  832. return retval;
  833. }
  834. /*
  835. * This is a generic packet receiver that calls arcnet??_rx depending on the
  836. * protocol ID found.
  837. */
  838. static void arcnet_rx(struct net_device *dev, int bufnum)
  839. {
  840. struct arcnet_local *lp = netdev_priv(dev);
  841. struct archdr pkt;
  842. struct arc_rfc1201 *soft;
  843. int length, ofs;
  844. soft = &pkt.soft.rfc1201;
  845. lp->hw.copy_from_card(dev, bufnum, 0, &pkt, sizeof(ARC_HDR_SIZE));
  846. if (pkt.hard.offset[0]) {
  847. ofs = pkt.hard.offset[0];
  848. length = 256 - ofs;
  849. } else {
  850. ofs = pkt.hard.offset[1];
  851. length = 512 - ofs;
  852. }
  853. /* get the full header, if possible */
  854. if (sizeof(pkt.soft) <= length)
  855. lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
  856. else {
  857. memset(&pkt.soft, 0, sizeof(pkt.soft));
  858. lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
  859. }
  860. BUGMSG(D_DURING, "Buffer #%d: received packet from %02Xh to %02Xh "
  861. "(%d+4 bytes)\n",
  862. bufnum, pkt.hard.source, pkt.hard.dest, length);
  863. dev->stats.rx_packets++;
  864. dev->stats.rx_bytes += length + ARC_HDR_SIZE;
  865. /* call the right receiver for the protocol */
  866. if (arc_proto_map[soft->proto]->is_ip) {
  867. BUGLVL(D_PROTO) {
  868. struct ArcProto
  869. *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
  870. *newp = arc_proto_map[soft->proto];
  871. if (oldp != newp) {
  872. BUGMSG(D_PROTO,
  873. "got protocol %02Xh; encap for host %02Xh is now '%c'"
  874. " (was '%c')\n", soft->proto, pkt.hard.source,
  875. newp->suffix, oldp->suffix);
  876. }
  877. }
  878. /* broadcasts will always be done with the last-used encap. */
  879. lp->default_proto[0] = soft->proto;
  880. /* in striking contrast, the following isn't a hack. */
  881. lp->default_proto[pkt.hard.source] = soft->proto;
  882. }
  883. /* call the protocol-specific receiver. */
  884. arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
  885. }
  886. static void null_rx(struct net_device *dev, int bufnum,
  887. struct archdr *pkthdr, int length)
  888. {
  889. BUGMSG(D_PROTO,
  890. "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
  891. pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
  892. }
  893. static int null_build_header(struct sk_buff *skb, struct net_device *dev,
  894. unsigned short type, uint8_t daddr)
  895. {
  896. struct arcnet_local *lp = netdev_priv(dev);
  897. BUGMSG(D_PROTO,
  898. "tx: can't build header for encap %02Xh; load a protocol driver.\n",
  899. lp->default_proto[daddr]);
  900. /* always fails */
  901. return 0;
  902. }
  903. /* the "do nothing" prepare_tx function warns that there's nothing to do. */
  904. static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
  905. int length, int bufnum)
  906. {
  907. struct arcnet_local *lp = netdev_priv(dev);
  908. struct arc_hardware newpkt;
  909. BUGMSG(D_PROTO, "tx: no encap for this host; load a protocol driver.\n");
  910. /* send a packet to myself -- will never get received, of course */
  911. newpkt.source = newpkt.dest = dev->dev_addr[0];
  912. /* only one byte of actual data (and it's random) */
  913. newpkt.offset[0] = 0xFF;
  914. lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
  915. return 1; /* done */
  916. }