arcnet.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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(unsigned long data)
  325. {
  326. struct net_device *dev = (struct net_device *)data;
  327. if (!netif_carrier_ok(dev)) {
  328. netif_carrier_on(dev);
  329. netdev_info(dev, "link up\n");
  330. }
  331. }
  332. static void arcnet_reply_tasklet(unsigned long data)
  333. {
  334. struct arcnet_local *lp = (struct arcnet_local *)data;
  335. struct sk_buff *ackskb, *skb;
  336. struct sock_exterr_skb *serr;
  337. struct sock *sk;
  338. int ret;
  339. local_irq_disable();
  340. skb = lp->outgoing.skb;
  341. if (!skb || !skb->sk) {
  342. local_irq_enable();
  343. return;
  344. }
  345. sock_hold(skb->sk);
  346. sk = skb->sk;
  347. ackskb = skb_clone_sk(skb);
  348. sock_put(skb->sk);
  349. if (!ackskb) {
  350. local_irq_enable();
  351. return;
  352. }
  353. serr = SKB_EXT_ERR(ackskb);
  354. memset(serr, 0, sizeof(*serr));
  355. serr->ee.ee_errno = ENOMSG;
  356. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  357. serr->ee.ee_data = skb_shinfo(skb)->tskey;
  358. serr->ee.ee_info = lp->reply_status;
  359. /* finally erasing outgoing skb */
  360. dev_kfree_skb(lp->outgoing.skb);
  361. lp->outgoing.skb = NULL;
  362. ackskb->dev = lp->dev;
  363. ret = sock_queue_err_skb(sk, ackskb);
  364. if (ret)
  365. kfree_skb(ackskb);
  366. local_irq_enable();
  367. };
  368. struct net_device *alloc_arcdev(const char *name)
  369. {
  370. struct net_device *dev;
  371. dev = alloc_netdev(sizeof(struct arcnet_local),
  372. name && *name ? name : "arc%d", NET_NAME_UNKNOWN,
  373. arcdev_setup);
  374. if (dev) {
  375. struct arcnet_local *lp = netdev_priv(dev);
  376. lp->dev = dev;
  377. spin_lock_init(&lp->lock);
  378. init_timer(&lp->timer);
  379. lp->timer.data = (unsigned long) dev;
  380. lp->timer.function = arcnet_timer;
  381. }
  382. return dev;
  383. }
  384. EXPORT_SYMBOL(alloc_arcdev);
  385. /* Open/initialize the board. This is called sometime after booting when
  386. * the 'ifconfig' program is run.
  387. *
  388. * This routine should set everything up anew at each open, even registers
  389. * that "should" only need to be set once at boot, so that there is
  390. * non-reboot way to recover if something goes wrong.
  391. */
  392. int arcnet_open(struct net_device *dev)
  393. {
  394. struct arcnet_local *lp = netdev_priv(dev);
  395. int count, newmtu, error;
  396. arc_printk(D_INIT, dev, "opened.");
  397. if (!try_module_get(lp->hw.owner))
  398. return -ENODEV;
  399. if (BUGLVL(D_PROTO)) {
  400. arc_printk(D_PROTO, dev, "protocol map (default is '%c'): ",
  401. arc_proto_default->suffix);
  402. for (count = 0; count < 256; count++)
  403. arc_cont(D_PROTO, "%c", arc_proto_map[count]->suffix);
  404. arc_cont(D_PROTO, "\n");
  405. }
  406. tasklet_init(&lp->reply_tasklet, arcnet_reply_tasklet,
  407. (unsigned long)lp);
  408. arc_printk(D_INIT, dev, "arcnet_open: resetting card.\n");
  409. /* try to put the card in a defined state - if it fails the first
  410. * time, actually reset it.
  411. */
  412. error = -ENODEV;
  413. if (lp->hw.reset(dev, 0) && lp->hw.reset(dev, 1))
  414. goto out_module_put;
  415. newmtu = choose_mtu();
  416. if (newmtu < dev->mtu)
  417. dev->mtu = newmtu;
  418. arc_printk(D_INIT, dev, "arcnet_open: mtu: %d.\n", dev->mtu);
  419. /* autodetect the encapsulation for each host. */
  420. memset(lp->default_proto, 0, sizeof(lp->default_proto));
  421. /* the broadcast address is special - use the 'bcast' protocol */
  422. for (count = 0; count < 256; count++) {
  423. if (arc_proto_map[count] == arc_bcast_proto) {
  424. lp->default_proto[0] = count;
  425. break;
  426. }
  427. }
  428. /* initialize buffers */
  429. atomic_set(&lp->buf_lock, 1);
  430. lp->next_buf = lp->first_free_buf = 0;
  431. release_arcbuf(dev, 0);
  432. release_arcbuf(dev, 1);
  433. release_arcbuf(dev, 2);
  434. release_arcbuf(dev, 3);
  435. lp->cur_tx = lp->next_tx = -1;
  436. lp->cur_rx = -1;
  437. lp->rfc1201.sequence = 1;
  438. /* bring up the hardware driver */
  439. if (lp->hw.open)
  440. lp->hw.open(dev);
  441. if (dev->dev_addr[0] == 0)
  442. arc_printk(D_NORMAL, dev, "WARNING! Station address 00 is reserved for broadcasts!\n");
  443. else if (dev->dev_addr[0] == 255)
  444. arc_printk(D_NORMAL, dev, "WARNING! Station address FF may confuse DOS networking programs!\n");
  445. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  446. if (lp->hw.status(dev) & RESETflag) {
  447. arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
  448. __FILE__, __LINE__, __func__);
  449. lp->hw.command(dev, CFLAGScmd | RESETclear);
  450. }
  451. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  452. /* make sure we're ready to receive IRQ's. */
  453. lp->hw.intmask(dev, 0);
  454. udelay(1); /* give it time to set the mask before
  455. * we reset it again. (may not even be
  456. * necessary)
  457. */
  458. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  459. lp->intmask = NORXflag | RECONflag;
  460. lp->hw.intmask(dev, lp->intmask);
  461. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  462. netif_carrier_off(dev);
  463. netif_start_queue(dev);
  464. mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000));
  465. arcnet_led_event(dev, ARCNET_LED_EVENT_OPEN);
  466. return 0;
  467. out_module_put:
  468. module_put(lp->hw.owner);
  469. return error;
  470. }
  471. EXPORT_SYMBOL(arcnet_open);
  472. /* The inverse routine to arcnet_open - shuts down the card. */
  473. int arcnet_close(struct net_device *dev)
  474. {
  475. struct arcnet_local *lp = netdev_priv(dev);
  476. arcnet_led_event(dev, ARCNET_LED_EVENT_STOP);
  477. del_timer_sync(&lp->timer);
  478. netif_stop_queue(dev);
  479. netif_carrier_off(dev);
  480. tasklet_kill(&lp->reply_tasklet);
  481. /* flush TX and disable RX */
  482. lp->hw.intmask(dev, 0);
  483. lp->hw.command(dev, NOTXcmd); /* stop transmit */
  484. lp->hw.command(dev, NORXcmd); /* disable receive */
  485. mdelay(1);
  486. /* shut down the card */
  487. lp->hw.close(dev);
  488. module_put(lp->hw.owner);
  489. return 0;
  490. }
  491. EXPORT_SYMBOL(arcnet_close);
  492. static int arcnet_header(struct sk_buff *skb, struct net_device *dev,
  493. unsigned short type, const void *daddr,
  494. const void *saddr, unsigned len)
  495. {
  496. const struct arcnet_local *lp = netdev_priv(dev);
  497. uint8_t _daddr, proto_num;
  498. struct ArcProto *proto;
  499. arc_printk(D_DURING, dev,
  500. "create header from %d to %d; protocol %d (%Xh); size %u.\n",
  501. saddr ? *(uint8_t *)saddr : -1,
  502. daddr ? *(uint8_t *)daddr : -1,
  503. type, type, len);
  504. if (skb->len != 0 && len != skb->len)
  505. arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
  506. skb->len, len);
  507. /* Type is host order - ? */
  508. if (type == ETH_P_ARCNET) {
  509. proto = arc_raw_proto;
  510. arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n",
  511. proto->suffix);
  512. _daddr = daddr ? *(uint8_t *)daddr : 0;
  513. } else if (!daddr) {
  514. /* if the dest addr isn't provided, we can't choose an
  515. * encapsulation! Store the packet type (eg. ETH_P_IP)
  516. * for now, and we'll push on a real header when we do
  517. * rebuild_header.
  518. */
  519. *(uint16_t *)skb_push(skb, 2) = type;
  520. /* XXX: Why not use skb->mac_len? */
  521. if (skb->network_header - skb->mac_header != 2)
  522. arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! diff (%u) is not 2!\n",
  523. skb->network_header - skb->mac_header);
  524. return -2; /* return error -- can't transmit yet! */
  525. } else {
  526. /* otherwise, we can just add the header as usual. */
  527. _daddr = *(uint8_t *)daddr;
  528. proto_num = lp->default_proto[_daddr];
  529. proto = arc_proto_map[proto_num];
  530. arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n",
  531. proto_num, proto->suffix);
  532. if (proto == &arc_proto_null && arc_bcast_proto != proto) {
  533. arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n",
  534. arc_bcast_proto->suffix);
  535. proto = arc_bcast_proto;
  536. }
  537. }
  538. return proto->build_header(skb, dev, type, _daddr);
  539. }
  540. /* Called by the kernel in order to transmit a packet. */
  541. netdev_tx_t arcnet_send_packet(struct sk_buff *skb,
  542. struct net_device *dev)
  543. {
  544. struct arcnet_local *lp = netdev_priv(dev);
  545. struct archdr *pkt;
  546. struct arc_rfc1201 *soft;
  547. struct ArcProto *proto;
  548. int txbuf;
  549. unsigned long flags;
  550. int retval;
  551. arc_printk(D_DURING, dev,
  552. "transmit requested (status=%Xh, txbufs=%d/%d, len=%d, protocol %x)\n",
  553. lp->hw.status(dev), lp->cur_tx, lp->next_tx, skb->len, skb->protocol);
  554. pkt = (struct archdr *)skb->data;
  555. soft = &pkt->soft.rfc1201;
  556. proto = arc_proto_map[soft->proto];
  557. arc_printk(D_SKB_SIZE, dev, "skb: transmitting %d bytes to %02X\n",
  558. skb->len, pkt->hard.dest);
  559. if (BUGLVL(D_SKB))
  560. arcnet_dump_skb(dev, skb, "tx");
  561. /* fits in one packet? */
  562. if (skb->len - ARC_HDR_SIZE > XMTU && !proto->continue_tx) {
  563. arc_printk(D_NORMAL, dev, "fixme: packet too large: compensating badly!\n");
  564. dev_kfree_skb(skb);
  565. return NETDEV_TX_OK; /* don't try again */
  566. }
  567. /* We're busy transmitting a packet... */
  568. netif_stop_queue(dev);
  569. spin_lock_irqsave(&lp->lock, flags);
  570. lp->hw.intmask(dev, 0);
  571. if (lp->next_tx == -1)
  572. txbuf = get_arcbuf(dev);
  573. else
  574. txbuf = -1;
  575. if (txbuf != -1) {
  576. lp->outgoing.skb = skb;
  577. if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
  578. !proto->ack_tx) {
  579. /* done right away and we don't want to acknowledge
  580. * the package later - forget about it now
  581. */
  582. dev->stats.tx_bytes += skb->len;
  583. } else {
  584. /* do it the 'split' way */
  585. lp->outgoing.proto = proto;
  586. lp->outgoing.skb = skb;
  587. lp->outgoing.pkt = pkt;
  588. if (proto->continue_tx &&
  589. proto->continue_tx(dev, txbuf)) {
  590. arc_printk(D_NORMAL, dev,
  591. "bug! continue_tx finished the first time! (proto='%c')\n",
  592. proto->suffix);
  593. }
  594. }
  595. retval = NETDEV_TX_OK;
  596. lp->next_tx = txbuf;
  597. } else {
  598. retval = NETDEV_TX_BUSY;
  599. }
  600. arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
  601. __FILE__, __LINE__, __func__, lp->hw.status(dev));
  602. /* make sure we didn't ignore a TX IRQ while we were in here */
  603. lp->hw.intmask(dev, 0);
  604. arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
  605. lp->intmask |= TXFREEflag | EXCNAKflag;
  606. lp->hw.intmask(dev, lp->intmask);
  607. arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
  608. __FILE__, __LINE__, __func__, lp->hw.status(dev));
  609. arcnet_led_event(dev, ARCNET_LED_EVENT_TX);
  610. spin_unlock_irqrestore(&lp->lock, flags);
  611. return retval; /* no need to try again */
  612. }
  613. EXPORT_SYMBOL(arcnet_send_packet);
  614. /* Actually start transmitting a packet that was loaded into a buffer
  615. * by prepare_tx. This should _only_ be called by the interrupt handler.
  616. */
  617. static int go_tx(struct net_device *dev)
  618. {
  619. struct arcnet_local *lp = netdev_priv(dev);
  620. arc_printk(D_DURING, dev, "go_tx: status=%Xh, intmask=%Xh, next_tx=%d, cur_tx=%d\n",
  621. lp->hw.status(dev), lp->intmask, lp->next_tx, lp->cur_tx);
  622. if (lp->cur_tx != -1 || lp->next_tx == -1)
  623. return 0;
  624. if (BUGLVL(D_TX))
  625. arcnet_dump_packet(dev, lp->next_tx, "go_tx", 0);
  626. lp->cur_tx = lp->next_tx;
  627. lp->next_tx = -1;
  628. /* start sending */
  629. lp->hw.command(dev, TXcmd | (lp->cur_tx << 3));
  630. dev->stats.tx_packets++;
  631. lp->lasttrans_dest = lp->lastload_dest;
  632. lp->lastload_dest = 0;
  633. lp->excnak_pending = 0;
  634. lp->intmask |= TXFREEflag | EXCNAKflag;
  635. return 1;
  636. }
  637. /* Called by the kernel when transmit times out */
  638. void arcnet_timeout(struct net_device *dev)
  639. {
  640. unsigned long flags;
  641. struct arcnet_local *lp = netdev_priv(dev);
  642. int status = lp->hw.status(dev);
  643. char *msg;
  644. spin_lock_irqsave(&lp->lock, flags);
  645. if (status & TXFREEflag) { /* transmit _DID_ finish */
  646. msg = " - missed IRQ?";
  647. } else {
  648. msg = "";
  649. dev->stats.tx_aborted_errors++;
  650. lp->timed_out = 1;
  651. lp->hw.command(dev, NOTXcmd | (lp->cur_tx << 3));
  652. }
  653. dev->stats.tx_errors++;
  654. /* make sure we didn't miss a TX or a EXC NAK IRQ */
  655. lp->hw.intmask(dev, 0);
  656. lp->intmask |= TXFREEflag | EXCNAKflag;
  657. lp->hw.intmask(dev, lp->intmask);
  658. spin_unlock_irqrestore(&lp->lock, flags);
  659. if (time_after(jiffies, lp->last_timeout + 10 * HZ)) {
  660. arc_printk(D_EXTRA, dev, "tx timed out%s (status=%Xh, intmask=%Xh, dest=%02Xh)\n",
  661. msg, status, lp->intmask, lp->lasttrans_dest);
  662. lp->last_timeout = jiffies;
  663. }
  664. if (lp->cur_tx == -1)
  665. netif_wake_queue(dev);
  666. }
  667. EXPORT_SYMBOL(arcnet_timeout);
  668. /* The typical workload of the driver: Handle the network interface
  669. * interrupts. Establish which device needs attention, and call the correct
  670. * chipset interrupt handler.
  671. */
  672. irqreturn_t arcnet_interrupt(int irq, void *dev_id)
  673. {
  674. struct net_device *dev = dev_id;
  675. struct arcnet_local *lp;
  676. int recbuf, status, diagstatus, didsomething, boguscount;
  677. unsigned long flags;
  678. int retval = IRQ_NONE;
  679. arc_printk(D_DURING, dev, "\n");
  680. arc_printk(D_DURING, dev, "in arcnet_interrupt\n");
  681. lp = netdev_priv(dev);
  682. BUG_ON(!lp);
  683. spin_lock_irqsave(&lp->lock, flags);
  684. /* RESET flag was enabled - if device is not running, we must
  685. * clear it right away (but nothing else).
  686. */
  687. if (!netif_running(dev)) {
  688. if (lp->hw.status(dev) & RESETflag)
  689. lp->hw.command(dev, CFLAGScmd | RESETclear);
  690. lp->hw.intmask(dev, 0);
  691. spin_unlock_irqrestore(&lp->lock, flags);
  692. return retval;
  693. }
  694. arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
  695. lp->hw.status(dev), lp->intmask);
  696. boguscount = 5;
  697. do {
  698. status = lp->hw.status(dev);
  699. diagstatus = (status >> 8) & 0xFF;
  700. arc_printk(D_DEBUG, dev, "%s: %d: %s: status=%x\n",
  701. __FILE__, __LINE__, __func__, status);
  702. didsomething = 0;
  703. /* RESET flag was enabled - card is resetting and if RX is
  704. * disabled, it's NOT because we just got a packet.
  705. *
  706. * The card is in an undefined state.
  707. * Clear it out and start over.
  708. */
  709. if (status & RESETflag) {
  710. arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n",
  711. status);
  712. arcnet_close(dev);
  713. arcnet_open(dev);
  714. /* get out of the interrupt handler! */
  715. break;
  716. }
  717. /* RX is inhibited - we must have received something.
  718. * Prepare to receive into the next buffer.
  719. *
  720. * We don't actually copy the received packet from the card
  721. * until after the transmit handler runs (and possibly
  722. * launches the next tx); this should improve latency slightly
  723. * if we get both types of interrupts at once.
  724. */
  725. recbuf = -1;
  726. if (status & lp->intmask & NORXflag) {
  727. recbuf = lp->cur_rx;
  728. arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n",
  729. recbuf, status);
  730. lp->cur_rx = get_arcbuf(dev);
  731. if (lp->cur_rx != -1) {
  732. arc_printk(D_DURING, dev, "enabling receive to buffer #%d\n",
  733. lp->cur_rx);
  734. lp->hw.command(dev, RXcmd | (lp->cur_rx << 3) | RXbcasts);
  735. }
  736. didsomething++;
  737. }
  738. if ((diagstatus & EXCNAKflag)) {
  739. arc_printk(D_DURING, dev, "EXCNAK IRQ (diagstat=%Xh)\n",
  740. diagstatus);
  741. lp->hw.command(dev, NOTXcmd); /* disable transmit */
  742. lp->excnak_pending = 1;
  743. lp->hw.command(dev, EXCNAKclear);
  744. lp->intmask &= ~(EXCNAKflag);
  745. didsomething++;
  746. }
  747. /* a transmit finished, and we're interested in it. */
  748. if ((status & lp->intmask & TXFREEflag) || lp->timed_out) {
  749. int ackstatus;
  750. lp->intmask &= ~(TXFREEflag | EXCNAKflag);
  751. if (status & TXACKflag)
  752. ackstatus = 2;
  753. else if (lp->excnak_pending)
  754. ackstatus = 1;
  755. else
  756. ackstatus = 0;
  757. arc_printk(D_DURING, dev, "TX IRQ (stat=%Xh)\n",
  758. status);
  759. if (lp->cur_tx != -1 && !lp->timed_out) {
  760. if (!(status & TXACKflag)) {
  761. if (lp->lasttrans_dest != 0) {
  762. arc_printk(D_EXTRA, dev,
  763. "transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
  764. status,
  765. lp->lasttrans_dest);
  766. dev->stats.tx_errors++;
  767. dev->stats.tx_carrier_errors++;
  768. } else {
  769. arc_printk(D_DURING, dev,
  770. "broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
  771. status,
  772. lp->lasttrans_dest);
  773. }
  774. }
  775. if (lp->outgoing.proto &&
  776. lp->outgoing.proto->ack_tx) {
  777. lp->outgoing.proto
  778. ->ack_tx(dev, ackstatus);
  779. }
  780. lp->reply_status = ackstatus;
  781. tasklet_hi_schedule(&lp->reply_tasklet);
  782. }
  783. if (lp->cur_tx != -1)
  784. release_arcbuf(dev, lp->cur_tx);
  785. lp->cur_tx = -1;
  786. lp->timed_out = 0;
  787. didsomething++;
  788. /* send another packet if there is one */
  789. go_tx(dev);
  790. /* continue a split packet, if any */
  791. if (lp->outgoing.proto &&
  792. lp->outgoing.proto->continue_tx) {
  793. int txbuf = get_arcbuf(dev);
  794. if (txbuf != -1) {
  795. if (lp->outgoing.proto->continue_tx(dev, txbuf)) {
  796. /* that was the last segment */
  797. dev->stats.tx_bytes += lp->outgoing.skb->len;
  798. if (!lp->outgoing.proto->ack_tx) {
  799. dev_kfree_skb_irq(lp->outgoing.skb);
  800. lp->outgoing.proto = NULL;
  801. }
  802. }
  803. lp->next_tx = txbuf;
  804. }
  805. }
  806. /* inform upper layers of idleness, if necessary */
  807. if (lp->cur_tx == -1)
  808. netif_wake_queue(dev);
  809. }
  810. /* now process the received packet, if any */
  811. if (recbuf != -1) {
  812. if (BUGLVL(D_RX))
  813. arcnet_dump_packet(dev, recbuf, "rx irq", 0);
  814. arcnet_rx(dev, recbuf);
  815. release_arcbuf(dev, recbuf);
  816. didsomething++;
  817. }
  818. if (status & lp->intmask & RECONflag) {
  819. lp->hw.command(dev, CFLAGScmd | CONFIGclear);
  820. dev->stats.tx_carrier_errors++;
  821. arc_printk(D_RECON, dev, "Network reconfiguration detected (status=%Xh)\n",
  822. status);
  823. if (netif_carrier_ok(dev)) {
  824. netif_carrier_off(dev);
  825. netdev_info(dev, "link down\n");
  826. }
  827. mod_timer(&lp->timer, jiffies + msecs_to_jiffies(1000));
  828. arcnet_led_event(dev, ARCNET_LED_EVENT_RECON);
  829. /* MYRECON bit is at bit 7 of diagstatus */
  830. if (diagstatus & 0x80)
  831. arc_printk(D_RECON, dev, "Put out that recon myself\n");
  832. /* is the RECON info empty or old? */
  833. if (!lp->first_recon || !lp->last_recon ||
  834. time_after(jiffies, lp->last_recon + HZ * 10)) {
  835. if (lp->network_down)
  836. arc_printk(D_NORMAL, dev, "reconfiguration detected: cabling restored?\n");
  837. lp->first_recon = lp->last_recon = jiffies;
  838. lp->num_recons = lp->network_down = 0;
  839. arc_printk(D_DURING, dev, "recon: clearing counters.\n");
  840. } else { /* add to current RECON counter */
  841. lp->last_recon = jiffies;
  842. lp->num_recons++;
  843. arc_printk(D_DURING, dev, "recon: counter=%d, time=%lds, net=%d\n",
  844. lp->num_recons,
  845. (lp->last_recon - lp->first_recon) / HZ,
  846. lp->network_down);
  847. /* if network is marked up;
  848. * and first_recon and last_recon are 60+ apart;
  849. * and the average no. of recons counted is
  850. * > RECON_THRESHOLD/min;
  851. * then print a warning message.
  852. */
  853. if (!lp->network_down &&
  854. (lp->last_recon - lp->first_recon) <= HZ * 60 &&
  855. lp->num_recons >= RECON_THRESHOLD) {
  856. lp->network_down = 1;
  857. arc_printk(D_NORMAL, dev, "many reconfigurations detected: cabling problem?\n");
  858. } else if (!lp->network_down &&
  859. lp->last_recon - lp->first_recon > HZ * 60) {
  860. /* reset counters if we've gone for
  861. * over a minute.
  862. */
  863. lp->first_recon = lp->last_recon;
  864. lp->num_recons = 1;
  865. }
  866. }
  867. } else if (lp->network_down &&
  868. time_after(jiffies, lp->last_recon + HZ * 10)) {
  869. if (lp->network_down)
  870. arc_printk(D_NORMAL, dev, "cabling restored?\n");
  871. lp->first_recon = lp->last_recon = 0;
  872. lp->num_recons = lp->network_down = 0;
  873. arc_printk(D_DURING, dev, "not recon: clearing counters anyway.\n");
  874. netif_carrier_on(dev);
  875. }
  876. if (didsomething)
  877. retval |= IRQ_HANDLED;
  878. } while (--boguscount && didsomething);
  879. arc_printk(D_DURING, dev, "arcnet_interrupt complete (status=%Xh, count=%d)\n",
  880. lp->hw.status(dev), boguscount);
  881. arc_printk(D_DURING, dev, "\n");
  882. lp->hw.intmask(dev, 0);
  883. udelay(1);
  884. lp->hw.intmask(dev, lp->intmask);
  885. spin_unlock_irqrestore(&lp->lock, flags);
  886. return retval;
  887. }
  888. EXPORT_SYMBOL(arcnet_interrupt);
  889. /* This is a generic packet receiver that calls arcnet??_rx depending on the
  890. * protocol ID found.
  891. */
  892. static void arcnet_rx(struct net_device *dev, int bufnum)
  893. {
  894. struct arcnet_local *lp = netdev_priv(dev);
  895. struct archdr pkt;
  896. struct arc_rfc1201 *soft;
  897. int length, ofs;
  898. soft = &pkt.soft.rfc1201;
  899. lp->hw.copy_from_card(dev, bufnum, 0, &pkt, ARC_HDR_SIZE);
  900. if (pkt.hard.offset[0]) {
  901. ofs = pkt.hard.offset[0];
  902. length = 256 - ofs;
  903. } else {
  904. ofs = pkt.hard.offset[1];
  905. length = 512 - ofs;
  906. }
  907. /* get the full header, if possible */
  908. if (sizeof(pkt.soft) <= length) {
  909. lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(pkt.soft));
  910. } else {
  911. memset(&pkt.soft, 0, sizeof(pkt.soft));
  912. lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
  913. }
  914. arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
  915. bufnum, pkt.hard.source, pkt.hard.dest, length);
  916. dev->stats.rx_packets++;
  917. dev->stats.rx_bytes += length + ARC_HDR_SIZE;
  918. /* call the right receiver for the protocol */
  919. if (arc_proto_map[soft->proto]->is_ip) {
  920. if (BUGLVL(D_PROTO)) {
  921. struct ArcProto
  922. *oldp = arc_proto_map[lp->default_proto[pkt.hard.source]],
  923. *newp = arc_proto_map[soft->proto];
  924. if (oldp != newp) {
  925. arc_printk(D_PROTO, dev,
  926. "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
  927. soft->proto, pkt.hard.source,
  928. newp->suffix, oldp->suffix);
  929. }
  930. }
  931. /* broadcasts will always be done with the last-used encap. */
  932. lp->default_proto[0] = soft->proto;
  933. /* in striking contrast, the following isn't a hack. */
  934. lp->default_proto[pkt.hard.source] = soft->proto;
  935. }
  936. /* call the protocol-specific receiver. */
  937. arc_proto_map[soft->proto]->rx(dev, bufnum, &pkt, length);
  938. }
  939. static void null_rx(struct net_device *dev, int bufnum,
  940. struct archdr *pkthdr, int length)
  941. {
  942. arc_printk(D_PROTO, dev,
  943. "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
  944. pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
  945. }
  946. static int null_build_header(struct sk_buff *skb, struct net_device *dev,
  947. unsigned short type, uint8_t daddr)
  948. {
  949. struct arcnet_local *lp = netdev_priv(dev);
  950. arc_printk(D_PROTO, dev,
  951. "tx: can't build header for encap %02Xh; load a protocol driver.\n",
  952. lp->default_proto[daddr]);
  953. /* always fails */
  954. return 0;
  955. }
  956. /* the "do nothing" prepare_tx function warns that there's nothing to do. */
  957. static int null_prepare_tx(struct net_device *dev, struct archdr *pkt,
  958. int length, int bufnum)
  959. {
  960. struct arcnet_local *lp = netdev_priv(dev);
  961. struct arc_hardware newpkt;
  962. arc_printk(D_PROTO, dev, "tx: no encap for this host; load a protocol driver.\n");
  963. /* send a packet to myself -- will never get received, of course */
  964. newpkt.source = newpkt.dest = dev->dev_addr[0];
  965. /* only one byte of actual data (and it's random) */
  966. newpkt.offset[0] = 0xFF;
  967. lp->hw.copy_to_card(dev, bufnum, 0, &newpkt, ARC_HDR_SIZE);
  968. return 1; /* done */
  969. }