arcnet.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. /* 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("arcnet loaded.\n");
  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. static void arcnet_dump_packet(struct net_device *dev, int bufnum,
  158. char *desc, 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. #else
  182. #define arcnet_dump_packet(dev, bufnum, desc,take_arcnet_lock) do { } while (0)
  183. #endif
  184. /*
  185. * Unregister a protocol driver from the arc_proto_map. Protocol drivers
  186. * are responsible for registering themselves, but the unregister routine
  187. * is pretty generic so we'll do it here.
  188. */
  189. void arcnet_unregister_proto(struct ArcProto *proto)
  190. {
  191. int count;
  192. if (arc_proto_default == proto)
  193. arc_proto_default = &arc_proto_null;
  194. if (arc_bcast_proto == proto)
  195. arc_bcast_proto = arc_proto_default;
  196. if (arc_raw_proto == proto)
  197. arc_raw_proto = arc_proto_default;
  198. for (count = 0; count < 256; count++) {
  199. if (arc_proto_map[count] == proto)
  200. arc_proto_map[count] = arc_proto_default;
  201. }
  202. }
  203. /*
  204. * Add a buffer to the queue. Only the interrupt handler is allowed to do
  205. * this, unless interrupts are disabled.
  206. *
  207. * Note: we don't check for a full queue, since there aren't enough buffers
  208. * to more than fill it.
  209. */
  210. static void release_arcbuf(struct net_device *dev, int bufnum)
  211. {
  212. struct arcnet_local *lp = dev->priv;
  213. int i;
  214. lp->buf_queue[lp->first_free_buf++] = bufnum;
  215. lp->first_free_buf %= 5;
  216. BUGLVL(D_DURING) {
  217. BUGMSG(D_DURING, "release_arcbuf: freed #%d; buffer queue is now: ",
  218. bufnum);
  219. for (i = lp->next_buf; i != lp->first_free_buf; i = (i+1) % 5)
  220. BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
  221. BUGMSG2(D_DURING, "\n");
  222. }
  223. }
  224. /*
  225. * Get a buffer from the queue. If this returns -1, there are no buffers
  226. * available.
  227. */
  228. static int get_arcbuf(struct net_device *dev)
  229. {
  230. struct arcnet_local *lp = dev->priv;
  231. int buf = -1, i;
  232. if (!atomic_dec_and_test(&lp->buf_lock)) {
  233. /* already in this function */
  234. BUGMSG(D_NORMAL, "get_arcbuf: overlap (%d)!\n",
  235. lp->buf_lock.counter);
  236. }
  237. else { /* we can continue */
  238. if (lp->next_buf >= 5)
  239. lp->next_buf -= 5;
  240. if (lp->next_buf == lp->first_free_buf)
  241. BUGMSG(D_NORMAL, "get_arcbuf: BUG: no buffers are available??\n");
  242. else {
  243. buf = lp->buf_queue[lp->next_buf++];
  244. lp->next_buf %= 5;
  245. }
  246. }
  247. BUGLVL(D_DURING) {
  248. BUGMSG(D_DURING, "get_arcbuf: got #%d; buffer queue is now: ", buf);
  249. for (i = lp->next_buf; i != lp->first_free_buf; i = (i+1) % 5)
  250. BUGMSG2(D_DURING, "#%d ", lp->buf_queue[i]);
  251. BUGMSG2(D_DURING, "\n");
  252. }
  253. atomic_inc(&lp->buf_lock);
  254. return buf;
  255. }
  256. static int choose_mtu(void)
  257. {
  258. int count, mtu = 65535;
  259. /* choose the smallest MTU of all available encaps */
  260. for (count = 0; count < 256; count++) {
  261. if (arc_proto_map[count] != &arc_proto_null
  262. && arc_proto_map[count]->mtu < mtu) {
  263. mtu = arc_proto_map[count]->mtu;
  264. }
  265. }
  266. return mtu == 65535 ? XMTU : mtu;
  267. }
  268. /* Setup a struct device for ARCnet. */
  269. static void arcdev_setup(struct net_device *dev)
  270. {
  271. dev->type = ARPHRD_ARCNET;
  272. dev->hard_header_len = sizeof(struct archdr);
  273. dev->mtu = choose_mtu();
  274. dev->addr_len = ARCNET_ALEN;
  275. dev->tx_queue_len = 100;
  276. dev->broadcast[0] = 0x00; /* for us, broadcasts are address 0 */
  277. dev->watchdog_timeo = TX_TIMEOUT;
  278. /* New-style flags. */
  279. dev->flags = IFF_BROADCAST;
  280. /*
  281. * Put in this stuff here, so we don't have to export the symbols to
  282. * the chipset drivers.
  283. */
  284. dev->open = arcnet_open;
  285. dev->stop = arcnet_close;
  286. dev->hard_start_xmit = arcnet_send_packet;
  287. dev->tx_timeout = arcnet_timeout;
  288. dev->get_stats = arcnet_get_stats;
  289. dev->hard_header = arcnet_header;
  290. dev->rebuild_header = arcnet_rebuild_header;
  291. }
  292. struct net_device *alloc_arcdev(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 = (struct arcnet_local *) dev->priv;
  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. static int arcnet_open(struct net_device *dev)
  312. {
  313. struct arcnet_local *lp = dev->priv;
  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. int count;
  320. BUGMSG(D_PROTO, "protocol map (default is '%c'): ",
  321. arc_proto_default->suffix);
  322. for (count = 0; count < 256; count++)
  323. BUGMSG2(D_PROTO, "%c", arc_proto_map[count]->suffix);
  324. BUGMSG2(D_PROTO, "\n");
  325. }
  326. BUGMSG(D_INIT, "arcnet_open: resetting card.\n");
  327. /* try to put the card in a defined state - if it fails the first
  328. * time, actually reset it.
  329. */
  330. error = -ENODEV;
  331. if (ARCRESET(0) && ARCRESET(1))
  332. goto out_module_put;
  333. newmtu = choose_mtu();
  334. if (newmtu < dev->mtu)
  335. dev->mtu = newmtu;
  336. BUGMSG(D_INIT, "arcnet_open: mtu: %d.\n", dev->mtu);
  337. /* autodetect the encapsulation for each host. */
  338. memset(lp->default_proto, 0, sizeof(lp->default_proto));
  339. /* the broadcast address is special - use the 'bcast' protocol */
  340. for (count = 0; count < 256; count++) {
  341. if (arc_proto_map[count] == arc_bcast_proto) {
  342. lp->default_proto[0] = count;
  343. break;
  344. }
  345. }
  346. /* initialize buffers */
  347. atomic_set(&lp->buf_lock, 1);
  348. lp->next_buf = lp->first_free_buf = 0;
  349. release_arcbuf(dev, 0);
  350. release_arcbuf(dev, 1);
  351. release_arcbuf(dev, 2);
  352. release_arcbuf(dev, 3);
  353. lp->cur_tx = lp->next_tx = -1;
  354. lp->cur_rx = -1;
  355. lp->rfc1201.sequence = 1;
  356. /* bring up the hardware driver */
  357. if (lp->hw.open)
  358. lp->hw.open(dev);
  359. if (dev->dev_addr[0] == 0)
  360. BUGMSG(D_NORMAL, "WARNING! Station address 00 is reserved "
  361. "for broadcasts!\n");
  362. else if (dev->dev_addr[0] == 255)
  363. BUGMSG(D_NORMAL, "WARNING! Station address FF may confuse "
  364. "DOS networking programs!\n");
  365. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  366. if (ASTATUS() & RESETflag) {
  367. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  368. ACOMMAND(CFLAGScmd | RESETclear);
  369. }
  370. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  371. /* make sure we're ready to receive IRQ's. */
  372. AINTMASK(0);
  373. udelay(1); /* give it time to set the mask before
  374. * we reset it again. (may not even be
  375. * necessary)
  376. */
  377. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  378. lp->intmask = NORXflag | RECONflag;
  379. AINTMASK(lp->intmask);
  380. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  381. netif_start_queue(dev);
  382. return 0;
  383. out_module_put:
  384. module_put(lp->hw.owner);
  385. return error;
  386. }
  387. /* The inverse routine to arcnet_open - shuts down the card. */
  388. static int arcnet_close(struct net_device *dev)
  389. {
  390. struct arcnet_local *lp = dev->priv;
  391. netif_stop_queue(dev);
  392. /* flush TX and disable RX */
  393. AINTMASK(0);
  394. ACOMMAND(NOTXcmd); /* stop transmit */
  395. ACOMMAND(NORXcmd); /* disable receive */
  396. mdelay(1);
  397. /* shut down the card */
  398. lp->hw.close(dev);
  399. module_put(lp->hw.owner);
  400. return 0;
  401. }
  402. static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
  403. unsigned short type, void *daddr, void *saddr,
  404. unsigned len)
  405. {
  406. struct arcnet_local *lp = dev->priv;
  407. uint8_t _daddr, proto_num;
  408. struct ArcProto *proto;
  409. BUGMSG(D_DURING,
  410. "create header from %d to %d; protocol %d (%Xh); size %u.\n",
  411. saddr ? *(uint8_t *) saddr : -1,
  412. daddr ? *(uint8_t *) daddr : -1,
  413. type, type, len);
  414. if (skb->len!=0 && len != skb->len)
  415. BUGMSG(D_NORMAL, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
  416. skb->len, len);
  417. /* Type is host order - ? */
  418. if(type == ETH_P_ARCNET) {
  419. proto = arc_raw_proto;
  420. BUGMSG(D_DEBUG, "arc_raw_proto used. proto='%c'\n",proto->suffix);
  421. _daddr = daddr ? *(uint8_t *) daddr : 0;
  422. }
  423. else if (!daddr) {
  424. /*
  425. * if the dest addr isn't provided, we can't choose an encapsulation!
  426. * Store the packet type (eg. ETH_P_IP) for now, and we'll push on a
  427. * real header when we do rebuild_header.
  428. */
  429. *(uint16_t *) skb_push(skb, 2) = type;
  430. /*
  431. * XXX: Why not use skb->mac_len?
  432. */
  433. if (skb->network_header - skb->mac_header != 2)
  434. BUGMSG(D_NORMAL, "arcnet_header: Yikes! diff (%d) is not 2!\n",
  435. (int)(skb->network_header - skb->mac_header));
  436. return -2; /* return error -- can't transmit yet! */
  437. }
  438. else {
  439. /* otherwise, we can just add the header as usual. */
  440. _daddr = *(uint8_t *) daddr;
  441. proto_num = lp->default_proto[_daddr];
  442. proto = arc_proto_map[proto_num];
  443. BUGMSG(D_DURING, "building header for %02Xh using protocol '%c'\n",
  444. proto_num, proto->suffix);
  445. if (proto == &arc_proto_null && arc_bcast_proto != proto) {
  446. BUGMSG(D_DURING, "actually, let's use '%c' instead.\n",
  447. arc_bcast_proto->suffix);
  448. proto = arc_bcast_proto;
  449. }
  450. }
  451. return proto->build_header(skb, dev, type, _daddr);
  452. }
  453. /*
  454. * Rebuild the ARCnet hard header. This is called after an ARP (or in the
  455. * future other address resolution) has completed on this sk_buff. We now
  456. * let ARP fill in the destination field.
  457. */
  458. static int arcnet_rebuild_header(struct sk_buff *skb)
  459. {
  460. struct net_device *dev = skb->dev;
  461. struct arcnet_local *lp = dev->priv;
  462. int status = 0; /* default is failure */
  463. unsigned short type;
  464. uint8_t daddr=0;
  465. struct ArcProto *proto;
  466. /*
  467. * XXX: Why not use skb->mac_len?
  468. */
  469. if (skb->network_header - skb->mac_header != 2) {
  470. BUGMSG(D_NORMAL,
  471. "rebuild_header: shouldn't be here! (hdrsize=%d)\n",
  472. (int)(skb->network_header - skb->mac_header));
  473. return 0;
  474. }
  475. type = *(uint16_t *) skb_pull(skb, 2);
  476. BUGMSG(D_DURING, "rebuild header for protocol %Xh\n", type);
  477. if (type == ETH_P_IP) {
  478. #ifdef CONFIG_INET
  479. BUGMSG(D_DURING, "rebuild header for ethernet protocol %Xh\n", type);
  480. status = arp_find(&daddr, skb) ? 1 : 0;
  481. BUGMSG(D_DURING, " rebuilt: dest is %d; protocol %Xh\n",
  482. daddr, type);
  483. #endif
  484. } else {
  485. BUGMSG(D_NORMAL,
  486. "I don't understand ethernet protocol %Xh addresses!\n", type);
  487. lp->stats.tx_errors++;
  488. lp->stats.tx_aborted_errors++;
  489. }
  490. /* if we couldn't resolve the address... give up. */
  491. if (!status)
  492. return 0;
  493. /* add the _real_ header this time! */
  494. proto = arc_proto_map[lp->default_proto[daddr]];
  495. proto->build_header(skb, dev, type, daddr);
  496. return 1; /* success */
  497. }
  498. /* Called by the kernel in order to transmit a packet. */
  499. static int arcnet_send_packet(struct sk_buff *skb, struct net_device *dev)
  500. {
  501. struct arcnet_local *lp = dev->priv;
  502. struct archdr *pkt;
  503. struct arc_rfc1201 *soft;
  504. struct ArcProto *proto;
  505. int txbuf;
  506. unsigned long flags;
  507. int freeskb, retval;
  508. BUGMSG(D_DURING,
  509. "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
  510. ASTATUS(), lp->cur_tx, lp->next_tx, skb->len,skb->protocol);
  511. pkt = (struct archdr *) skb->data;
  512. soft = &pkt->soft.rfc1201;
  513. proto = arc_proto_map[soft->proto];
  514. BUGMSG(D_SKB_SIZE, "skb: transmitting %d bytes to %02X\n",
  515. skb->len, pkt->hard.dest);
  516. BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "tx");
  517. /* fits in one packet? */
  518. if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
  519. BUGMSG(D_NORMAL, "fixme: packet too large: compensating badly!\n");
  520. dev_kfree_skb(skb);
  521. return NETDEV_TX_OK; /* don't try again */
  522. }
  523. /* We're busy transmitting a packet... */
  524. netif_stop_queue(dev);
  525. spin_lock_irqsave(&lp->lock, flags);
  526. AINTMASK(0);
  527. if(lp->next_tx == -1)
  528. txbuf = get_arcbuf(dev);
  529. else {
  530. txbuf = -1;
  531. }
  532. if (txbuf != -1) {
  533. if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
  534. !proto->ack_tx) {
  535. /* done right away and we don't want to acknowledge
  536. the package later - forget about it now */
  537. lp->stats.tx_bytes += skb->len;
  538. freeskb = 1;
  539. } else {
  540. /* do it the 'split' way */
  541. lp->outgoing.proto = proto;
  542. lp->outgoing.skb = skb;
  543. lp->outgoing.pkt = pkt;
  544. freeskb = 0;
  545. if (proto->continue_tx &&
  546. proto->continue_tx(dev, txbuf)) {
  547. BUGMSG(D_NORMAL,
  548. "bug! continue_tx finished the first time! "
  549. "(proto='%c')\n", proto->suffix);
  550. }
  551. }
  552. retval = NETDEV_TX_OK;
  553. dev->trans_start = jiffies;
  554. lp->next_tx = txbuf;
  555. } else {
  556. retval = NETDEV_TX_BUSY;
  557. freeskb = 0;
  558. }
  559. BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n",__FILE__,__LINE__,__FUNCTION__,ASTATUS());
  560. /* make sure we didn't ignore a TX IRQ while we were in here */
  561. AINTMASK(0);
  562. BUGMSG(D_DEBUG, "%s: %d: %s\n",__FILE__,__LINE__,__FUNCTION__);
  563. lp->intmask |= TXFREEflag|EXCNAKflag;
  564. AINTMASK(lp->intmask);
  565. BUGMSG(D_DEBUG, "%s: %d: %s, status: %x\n",__FILE__,__LINE__,__FUNCTION__,ASTATUS());
  566. spin_unlock_irqrestore(&lp->lock, flags);
  567. if (freeskb) {
  568. dev_kfree_skb(skb);
  569. }
  570. return retval; /* no need to try again */
  571. }
  572. /*
  573. * Actually start transmitting a packet that was loaded into a buffer
  574. * by prepare_tx. This should _only_ be called by the interrupt handler.
  575. */
  576. static int go_tx(struct net_device *dev)
  577. {
  578. struct arcnet_local *lp = dev->priv;
  579. BUGMSG(D_DURING, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
  580. ASTATUS(), lp->intmask, lp->next_tx, lp->cur_tx);
  581. if (lp->cur_tx != -1 || lp->next_tx == -1)
  582. return 0;
  583. BUGLVL(D_TX) arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
  584. lp->cur_tx = lp->next_tx;
  585. lp->next_tx = -1;
  586. /* start sending */
  587. ACOMMAND(TXcmd | (lp->cur_tx << 3));
  588. lp->stats.tx_packets++;
  589. lp->lasttrans_dest = lp->lastload_dest;
  590. lp->lastload_dest = 0;
  591. lp->excnak_pending = 0;
  592. lp->intmask |= TXFREEflag|EXCNAKflag;
  593. return 1;
  594. }
  595. /* Called by the kernel when transmit times out */
  596. static void arcnet_timeout(struct net_device *dev)
  597. {
  598. unsigned long flags;
  599. struct arcnet_local *lp = dev->priv;
  600. int status = ASTATUS();
  601. char *msg;
  602. spin_lock_irqsave(&lp->lock, flags);
  603. if (status & TXFREEflag) { /* transmit _DID_ finish */
  604. msg = " - missed IRQ?";
  605. } else {
  606. msg = "";
  607. lp->stats.tx_aborted_errors++;
  608. lp->timed_out = 1;
  609. ACOMMAND(NOTXcmd | (lp->cur_tx << 3));
  610. }
  611. lp->stats.tx_errors++;
  612. /* make sure we didn't miss a TX or a EXC NAK IRQ */
  613. AINTMASK(0);
  614. lp->intmask |= TXFREEflag|EXCNAKflag;
  615. AINTMASK(lp->intmask);
  616. spin_unlock_irqrestore(&lp->lock, flags);
  617. if (time_after(jiffies, lp->last_timeout + 10*HZ)) {
  618. BUGMSG(D_EXTRA, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
  619. msg, status, lp->intmask, lp->lasttrans_dest);
  620. lp->last_timeout = jiffies;
  621. }
  622. if (lp->cur_tx == -1)
  623. netif_wake_queue(dev);
  624. }
  625. /*
  626. * The typical workload of the driver: Handle the network interface
  627. * interrupts. Establish which device needs attention, and call the correct
  628. * chipset interrupt handler.
  629. */
  630. irqreturn_t arcnet_interrupt(int irq, void *dev_id)
  631. {
  632. struct net_device *dev = dev_id;
  633. struct arcnet_local *lp;
  634. int recbuf, status, diagstatus, didsomething, boguscount;
  635. int retval = IRQ_NONE;
  636. BUGMSG(D_DURING, "\n");
  637. BUGMSG(D_DURING, "in arcnet_interrupt\n");
  638. lp = dev->priv;
  639. BUG_ON(!lp);
  640. spin_lock(&lp->lock);
  641. /*
  642. * RESET flag was enabled - if device is not running, we must clear it right
  643. * away (but nothing else).
  644. */
  645. if (!netif_running(dev)) {
  646. if (ASTATUS() & RESETflag)
  647. ACOMMAND(CFLAGScmd | RESETclear);
  648. AINTMASK(0);
  649. spin_unlock(&lp->lock);
  650. return IRQ_HANDLED;
  651. }
  652. BUGMSG(D_DURING, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
  653. ASTATUS(), lp->intmask);
  654. boguscount = 5;
  655. do {
  656. status = ASTATUS();
  657. diagstatus = (status >> 8) & 0xFF;
  658. BUGMSG(D_DEBUG, "%s: %d: %s: status=%x\n",
  659. __FILE__,__LINE__,__FUNCTION__,status);
  660. didsomething = 0;
  661. /*
  662. * RESET flag was enabled - card is resetting and if RX is
  663. * disabled, it's NOT because we just got a packet.
  664. *
  665. * The card is in an undefined state. Clear it out and start over.
  666. */
  667. if (status & RESETflag) {
  668. BUGMSG(D_NORMAL, "spurious reset (status=%Xh)\n", status);
  669. arcnet_close(dev);
  670. arcnet_open(dev);
  671. /* get out of the interrupt handler! */
  672. break;
  673. }
  674. /*
  675. * RX is inhibited - we must have received something. Prepare to
  676. * receive into the next buffer.
  677. *
  678. * We don't actually copy the received packet from the card until
  679. * after the transmit handler runs (and possibly launches the next
  680. * tx); this should improve latency slightly if we get both types
  681. * of interrupts at once.
  682. */
  683. recbuf = -1;
  684. if (status & lp->intmask & NORXflag) {
  685. recbuf = lp->cur_rx;
  686. BUGMSG(D_DURING, "Buffer #%d: receive irq (status=%Xh)\n",
  687. recbuf, status);
  688. lp->cur_rx = get_arcbuf(dev);
  689. if (lp->cur_rx != -1) {
  690. BUGMSG(D_DURING, "enabling receive to buffer #%d\n",
  691. lp->cur_rx);
  692. ACOMMAND(RXcmd | (lp->cur_rx << 3) | RXbcasts);
  693. }
  694. didsomething++;
  695. }
  696. if((diagstatus & EXCNAKflag)) {
  697. BUGMSG(D_DURING, "EXCNAK IRQ (diagstat=%Xh)\n",
  698. diagstatus);
  699. ACOMMAND(NOTXcmd); /* disable transmit */
  700. lp->excnak_pending = 1;
  701. ACOMMAND(EXCNAKclear);
  702. lp->intmask &= ~(EXCNAKflag);
  703. didsomething++;
  704. }
  705. /* a transmit finished, and we're interested in it. */
  706. if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
  707. lp->intmask &= ~(TXFREEflag|EXCNAKflag);
  708. BUGMSG(D_DURING, "TX IRQ (stat=%Xh)\n", status);
  709. if (lp->cur_tx != -1 && !lp->timed_out) {
  710. if(!(status & TXACKflag)) {
  711. if (lp->lasttrans_dest != 0) {
  712. BUGMSG(D_EXTRA,
  713. "transmit was not acknowledged! "
  714. "(status=%Xh, dest=%02Xh)\n",
  715. status, lp->lasttrans_dest);
  716. lp->stats.tx_errors++;
  717. lp->stats.tx_carrier_errors++;
  718. } else {
  719. BUGMSG(D_DURING,
  720. "broadcast was not acknowledged; that's normal "
  721. "(status=%Xh, dest=%02Xh)\n",
  722. status, lp->lasttrans_dest);
  723. }
  724. }
  725. if (lp->outgoing.proto &&
  726. lp->outgoing.proto->ack_tx) {
  727. int ackstatus;
  728. if(status & TXACKflag)
  729. ackstatus=2;
  730. else if(lp->excnak_pending)
  731. ackstatus=1;
  732. else
  733. ackstatus=0;
  734. lp->outgoing.proto
  735. ->ack_tx(dev, ackstatus);
  736. }
  737. }
  738. if (lp->cur_tx != -1)
  739. release_arcbuf(dev, lp->cur_tx);
  740. lp->cur_tx = -1;
  741. lp->timed_out = 0;
  742. didsomething++;
  743. /* send another packet if there is one */
  744. go_tx(dev);
  745. /* continue a split packet, if any */
  746. if (lp->outgoing.proto && lp->outgoing.proto->continue_tx) {
  747. int txbuf = get_arcbuf(dev);
  748. if (txbuf != -1) {
  749. if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
  750. /* that was the last segment */
  751. lp->stats.tx_bytes += lp->outgoing.skb->len;
  752. if(!lp->outgoing.proto->ack_tx)
  753. {
  754. dev_kfree_skb_irq(lp->outgoing.skb);
  755. lp->outgoing.proto = NULL;
  756. }
  757. }
  758. lp->next_tx = txbuf;
  759. }
  760. }
  761. /* inform upper layers of idleness, if necessary */
  762. if (lp->cur_tx == -1)
  763. netif_wake_queue(dev);
  764. }
  765. /* now process the received packet, if any */
  766. if (recbuf != -1) {
  767. BUGLVL(D_RX) arcnet_dump_packet(dev, recbuf, "rx irq", 0);
  768. arcnet_rx(dev, recbuf);
  769. release_arcbuf(dev, recbuf);
  770. didsomething++;
  771. }
  772. if (status & lp->intmask & RECONflag) {
  773. ACOMMAND(CFLAGScmd | CONFIGclear);
  774. lp->stats.tx_carrier_errors++;
  775. BUGMSG(D_RECON, "Network reconfiguration detected (status=%Xh)\n",
  776. status);
  777. /* MYRECON bit is at bit 7 of diagstatus */
  778. if(diagstatus & 0x80)
  779. BUGMSG(D_RECON,"Put out that recon myself\n");
  780. /* is the RECON info empty or old? */
  781. if (!lp->first_recon || !lp->last_recon ||
  782. jiffies - lp->last_recon > HZ * 10) {
  783. if (lp->network_down)
  784. BUGMSG(D_NORMAL, "reconfiguration detected: cabling restored?\n");
  785. lp->first_recon = lp->last_recon = jiffies;
  786. lp->num_recons = lp->network_down = 0;
  787. BUGMSG(D_DURING, "recon: clearing counters.\n");
  788. } else { /* add to current RECON counter */
  789. lp->last_recon = jiffies;
  790. lp->num_recons++;
  791. BUGMSG(D_DURING, "recon: counter=%d, time=%lds, net=%d\n",
  792. lp->num_recons,
  793. (lp->last_recon - lp->first_recon) / HZ,
  794. lp->network_down);
  795. /* if network is marked up;
  796. * and first_recon and last_recon are 60+ apart;
  797. * and the average no. of recons counted is
  798. * > RECON_THRESHOLD/min;
  799. * then print a warning message.
  800. */
  801. if (!lp->network_down
  802. && (lp->last_recon - lp->first_recon) <= HZ * 60
  803. && lp->num_recons >= RECON_THRESHOLD) {
  804. lp->network_down = 1;
  805. BUGMSG(D_NORMAL, "many reconfigurations detected: cabling problem?\n");
  806. } else if (!lp->network_down
  807. && lp->last_recon - lp->first_recon > HZ * 60) {
  808. /* reset counters if we've gone for over a minute. */
  809. lp->first_recon = lp->last_recon;
  810. lp->num_recons = 1;
  811. }
  812. }
  813. } else if (lp->network_down && 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 = dev->priv;
  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. lp->stats.rx_packets++;
  864. lp->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. /*
  887. * Get the current statistics. This may be called with the card open or
  888. * closed.
  889. */
  890. static struct net_device_stats *arcnet_get_stats(struct net_device *dev)
  891. {
  892. struct arcnet_local *lp = dev->priv;
  893. return &lp->stats;
  894. }
  895. static void null_rx(struct net_device *dev, int bufnum,
  896. struct archdr *pkthdr, int length)
  897. {
  898. BUGMSG(D_PROTO,
  899. "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
  900. pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
  901. }
  902. static int null_build_header(struct sk_buff *skb, struct net_device *dev,
  903. unsigned short type, uint8_t daddr)
  904. {
  905. struct arcnet_local *lp = dev->priv;
  906. BUGMSG(D_PROTO,
  907. "tx: can't build header for encap %02Xh; load a protocol driver.\n",
  908. lp->default_proto[daddr]);
  909. /* always fails */
  910. return 0;
  911. }
  912. /* the "do nothing" prepare_tx function warns that there's nothing to do. */
  913. static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
  914. int length, int bufnum)
  915. {
  916. struct arcnet_local *lp = dev->priv;
  917. struct arc_hardware newpkt;
  918. BUGMSG(D_PROTO, "tx: no encap for this host; load a protocol driver.\n");
  919. /* send a packet to myself -- will never get received, of course */
  920. newpkt.source = newpkt.dest = dev->dev_addr[0];
  921. /* only one byte of actual data (and it's random) */
  922. newpkt.offset[0] = 0xFF;
  923. lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
  924. return 1; /* done */
  925. }