arcnet.c 31 KB

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