arcnet.c 32 KB

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