ether3.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * linux/drivers/acorn/net/ether3.c
  3. *
  4. * Copyright (C) 1995-2000 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * SEEQ nq8005 ethernet driver for Acorn/ANT Ether3 card
  11. * for Acorn machines
  12. *
  13. * By Russell King, with some suggestions from borris@ant.co.uk
  14. *
  15. * Changelog:
  16. * 1.04 RMK 29/02/1996 Won't pass packets that are from our ethernet
  17. * address up to the higher levels - they're
  18. * silently ignored. I/F can now be put into
  19. * multicast mode. Receiver routine optimised.
  20. * 1.05 RMK 30/02/1996 Now claims interrupt at open when part of
  21. * the kernel rather than when a module.
  22. * 1.06 RMK 02/03/1996 Various code cleanups
  23. * 1.07 RMK 13/10/1996 Optimised interrupt routine and transmit
  24. * routines.
  25. * 1.08 RMK 14/10/1996 Fixed problem with too many packets,
  26. * prevented the kernel message about dropped
  27. * packets appearing too many times a second.
  28. * Now does not disable all IRQs, only the IRQ
  29. * used by this card.
  30. * 1.09 RMK 10/11/1996 Only enables TX irq when buffer space is low,
  31. * but we still service the TX queue if we get a
  32. * RX interrupt.
  33. * 1.10 RMK 15/07/1997 Fixed autoprobing of NQ8004.
  34. * 1.11 RMK 16/11/1997 Fixed autoprobing of NQ8005A.
  35. * 1.12 RMK 31/12/1997 Removed reference to dev_tint for Linux 2.1.
  36. * RMK 27/06/1998 Changed asm/delay.h to linux/delay.h.
  37. * 1.13 RMK 29/06/1998 Fixed problem with transmission of packets.
  38. * Chip seems to have a bug in, whereby if the
  39. * packet starts two bytes from the end of the
  40. * buffer, it corrupts the receiver chain, and
  41. * never updates the transmit status correctly.
  42. * 1.14 RMK 07/01/1998 Added initial code for ETHERB addressing.
  43. * 1.15 RMK 30/04/1999 More fixes to the transmit routine for buggy
  44. * hardware.
  45. * 1.16 RMK 10/02/2000 Updated for 2.3.43
  46. * 1.17 RMK 13/05/2000 Updated for 2.3.99-pre8
  47. */
  48. #include <linux/module.h>
  49. #include <linux/kernel.h>
  50. #include <linux/sched.h>
  51. #include <linux/types.h>
  52. #include <linux/fcntl.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/ptrace.h>
  55. #include <linux/ioport.h>
  56. #include <linux/in.h>
  57. #include <linux/slab.h>
  58. #include <linux/string.h>
  59. #include <linux/errno.h>
  60. #include <linux/netdevice.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/skbuff.h>
  63. #include <linux/device.h>
  64. #include <linux/init.h>
  65. #include <linux/delay.h>
  66. #include <linux/bitops.h>
  67. #include <asm/system.h>
  68. #include <asm/ecard.h>
  69. #include <asm/io.h>
  70. static char version[] __initdata = "ether3 ethernet driver (c) 1995-2000 R.M.King v1.17\n";
  71. #include "ether3.h"
  72. static unsigned int net_debug = NET_DEBUG;
  73. static void ether3_setmulticastlist(struct net_device *dev);
  74. static int ether3_rx(struct net_device *dev, unsigned int maxcnt);
  75. static void ether3_tx(struct net_device *dev);
  76. static int ether3_open (struct net_device *dev);
  77. static int ether3_sendpacket (struct sk_buff *skb, struct net_device *dev);
  78. static irqreturn_t ether3_interrupt (int irq, void *dev_id);
  79. static int ether3_close (struct net_device *dev);
  80. static struct net_device_stats *ether3_getstats (struct net_device *dev);
  81. static void ether3_setmulticastlist (struct net_device *dev);
  82. static void ether3_timeout(struct net_device *dev);
  83. #define BUS_16 2
  84. #define BUS_8 1
  85. #define BUS_UNKNOWN 0
  86. /* --------------------------------------------------------------------------- */
  87. typedef enum {
  88. buffer_write,
  89. buffer_read
  90. } buffer_rw_t;
  91. /*
  92. * ether3 read/write. Slow things down a bit...
  93. * The SEEQ8005 doesn't like us writing to its registers
  94. * too quickly.
  95. */
  96. static inline void ether3_outb(int v, const void __iomem *r)
  97. {
  98. writeb(v, r);
  99. udelay(1);
  100. }
  101. static inline void ether3_outw(int v, const void __iomem *r)
  102. {
  103. writew(v, r);
  104. udelay(1);
  105. }
  106. #define ether3_inb(r) ({ unsigned int __v = readb((r)); udelay(1); __v; })
  107. #define ether3_inw(r) ({ unsigned int __v = readw((r)); udelay(1); __v; })
  108. static int
  109. ether3_setbuffer(struct net_device *dev, buffer_rw_t read, int start)
  110. {
  111. int timeout = 1000;
  112. ether3_outw(priv(dev)->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
  113. ether3_outw(priv(dev)->regs.command | CMD_FIFOWRITE, REG_COMMAND);
  114. while ((ether3_inw(REG_STATUS) & STAT_FIFOEMPTY) == 0) {
  115. if (!timeout--) {
  116. printk("%s: setbuffer broken\n", dev->name);
  117. priv(dev)->broken = 1;
  118. return 1;
  119. }
  120. udelay(1);
  121. }
  122. if (read == buffer_read) {
  123. ether3_outw(start, REG_DMAADDR);
  124. ether3_outw(priv(dev)->regs.command | CMD_FIFOREAD, REG_COMMAND);
  125. } else {
  126. ether3_outw(priv(dev)->regs.command | CMD_FIFOWRITE, REG_COMMAND);
  127. ether3_outw(start, REG_DMAADDR);
  128. }
  129. return 0;
  130. }
  131. /*
  132. * write data to the buffer memory
  133. */
  134. #define ether3_writebuffer(dev,data,length) \
  135. writesw(REG_BUFWIN, (data), (length) >> 1)
  136. #define ether3_writeword(dev,data) \
  137. writew((data), REG_BUFWIN)
  138. #define ether3_writelong(dev,data) { \
  139. void __iomem *reg_bufwin = REG_BUFWIN; \
  140. writew((data), reg_bufwin); \
  141. writew((data) >> 16, reg_bufwin); \
  142. }
  143. /*
  144. * read data from the buffer memory
  145. */
  146. #define ether3_readbuffer(dev,data,length) \
  147. readsw(REG_BUFWIN, (data), (length) >> 1)
  148. #define ether3_readword(dev) \
  149. readw(REG_BUFWIN)
  150. #define ether3_readlong(dev) \
  151. readw(REG_BUFWIN) | (readw(REG_BUFWIN) << 16)
  152. /*
  153. * Switch LED off...
  154. */
  155. static void ether3_ledoff(unsigned long data)
  156. {
  157. struct net_device *dev = (struct net_device *)data;
  158. ether3_outw(priv(dev)->regs.config2 |= CFG2_CTRLO, REG_CONFIG2);
  159. }
  160. /*
  161. * switch LED on...
  162. */
  163. static inline void ether3_ledon(struct net_device *dev)
  164. {
  165. del_timer(&priv(dev)->timer);
  166. priv(dev)->timer.expires = jiffies + HZ / 50; /* leave on for 1/50th second */
  167. priv(dev)->timer.data = (unsigned long)dev;
  168. priv(dev)->timer.function = ether3_ledoff;
  169. add_timer(&priv(dev)->timer);
  170. if (priv(dev)->regs.config2 & CFG2_CTRLO)
  171. ether3_outw(priv(dev)->regs.config2 &= ~CFG2_CTRLO, REG_CONFIG2);
  172. }
  173. /*
  174. * Read the ethernet address string from the on board rom.
  175. * This is an ascii string!!!
  176. */
  177. static int __init
  178. ether3_addr(char *addr, struct expansion_card *ec)
  179. {
  180. struct in_chunk_dir cd;
  181. char *s;
  182. if (ecard_readchunk(&cd, ec, 0xf5, 0) && (s = strchr(cd.d.string, '('))) {
  183. int i;
  184. for (i = 0; i<6; i++) {
  185. addr[i] = simple_strtoul(s + 1, &s, 0x10);
  186. if (*s != (i==5?')' : ':' ))
  187. break;
  188. }
  189. if (i == 6)
  190. return 0;
  191. }
  192. /* I wonder if we should even let the user continue in this case
  193. * - no, it would be better to disable the device
  194. */
  195. printk(KERN_ERR "ether3: Couldn't read a valid MAC address from card.\n");
  196. return -ENODEV;
  197. }
  198. /* --------------------------------------------------------------------------- */
  199. static int __init
  200. ether3_ramtest(struct net_device *dev, unsigned char byte)
  201. {
  202. unsigned char *buffer = kmalloc(RX_END, GFP_KERNEL);
  203. int i,ret = 0;
  204. int max_errors = 4;
  205. int bad = -1;
  206. if (!buffer)
  207. return 1;
  208. memset(buffer, byte, RX_END);
  209. ether3_setbuffer(dev, buffer_write, 0);
  210. ether3_writebuffer(dev, buffer, TX_END);
  211. ether3_setbuffer(dev, buffer_write, RX_START);
  212. ether3_writebuffer(dev, buffer + RX_START, RX_LEN);
  213. memset(buffer, byte ^ 0xff, RX_END);
  214. ether3_setbuffer(dev, buffer_read, 0);
  215. ether3_readbuffer(dev, buffer, TX_END);
  216. ether3_setbuffer(dev, buffer_read, RX_START);
  217. ether3_readbuffer(dev, buffer + RX_START, RX_LEN);
  218. for (i = 0; i < RX_END; i++) {
  219. if (buffer[i] != byte) {
  220. if (max_errors > 0 && bad != buffer[i]) {
  221. printk("%s: RAM failed with (%02X instead of %02X) at 0x%04X",
  222. dev->name, buffer[i], byte, i);
  223. ret = 2;
  224. max_errors--;
  225. bad = i;
  226. }
  227. } else {
  228. if (bad != -1) {
  229. if (bad != i - 1)
  230. printk(" - 0x%04X\n", i - 1);
  231. printk("\n");
  232. bad = -1;
  233. }
  234. }
  235. }
  236. if (bad != -1)
  237. printk(" - 0xffff\n");
  238. kfree(buffer);
  239. return ret;
  240. }
  241. /* ------------------------------------------------------------------------------- */
  242. static int __init ether3_init_2(struct net_device *dev)
  243. {
  244. int i;
  245. priv(dev)->regs.config1 = CFG1_RECVCOMPSTAT0|CFG1_DMABURST8;
  246. priv(dev)->regs.config2 = CFG2_CTRLO|CFG2_RECVCRC|CFG2_ERRENCRC;
  247. priv(dev)->regs.command = 0;
  248. /*
  249. * Set up our hardware address
  250. */
  251. ether3_outw(priv(dev)->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
  252. for (i = 0; i < 6; i++)
  253. ether3_outb(dev->dev_addr[i], REG_BUFWIN);
  254. if (dev->flags & IFF_PROMISC)
  255. priv(dev)->regs.config1 |= CFG1_RECVPROMISC;
  256. else if (dev->flags & IFF_MULTICAST)
  257. priv(dev)->regs.config1 |= CFG1_RECVSPECBRMULTI;
  258. else
  259. priv(dev)->regs.config1 |= CFG1_RECVSPECBROAD;
  260. /*
  261. * There is a problem with the NQ8005 in that it occasionally loses the
  262. * last two bytes. To get round this problem, we receive the CRC as
  263. * well. That way, if we do lose the last two, then it doesn't matter.
  264. */
  265. ether3_outw(priv(dev)->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
  266. ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
  267. ether3_outw(priv(dev)->rx_head, REG_RECVPTR);
  268. ether3_outw(0, REG_TRANSMITPTR);
  269. ether3_outw(priv(dev)->rx_head >> 8, REG_RECVEND);
  270. ether3_outw(priv(dev)->regs.config2, REG_CONFIG2);
  271. ether3_outw(priv(dev)->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
  272. ether3_outw(priv(dev)->regs.command, REG_COMMAND);
  273. i = ether3_ramtest(dev, 0x5A);
  274. if(i)
  275. return i;
  276. i = ether3_ramtest(dev, 0x1E);
  277. if(i)
  278. return i;
  279. ether3_setbuffer(dev, buffer_write, 0);
  280. ether3_writelong(dev, 0);
  281. return 0;
  282. }
  283. static void
  284. ether3_init_for_open(struct net_device *dev)
  285. {
  286. int i;
  287. memset(&priv(dev)->stats, 0, sizeof(struct net_device_stats));
  288. /* Reset the chip */
  289. ether3_outw(CFG2_RESET, REG_CONFIG2);
  290. udelay(4);
  291. priv(dev)->regs.command = 0;
  292. ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
  293. while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON))
  294. barrier();
  295. ether3_outw(priv(dev)->regs.config1 | CFG1_BUFSELSTAT0, REG_CONFIG1);
  296. for (i = 0; i < 6; i++)
  297. ether3_outb(dev->dev_addr[i], REG_BUFWIN);
  298. priv(dev)->tx_head = 0;
  299. priv(dev)->tx_tail = 0;
  300. priv(dev)->regs.config2 |= CFG2_CTRLO;
  301. priv(dev)->rx_head = RX_START;
  302. ether3_outw(priv(dev)->regs.config1 | CFG1_TRANSEND, REG_CONFIG1);
  303. ether3_outw((TX_END>>8) - 1, REG_BUFWIN);
  304. ether3_outw(priv(dev)->rx_head, REG_RECVPTR);
  305. ether3_outw(priv(dev)->rx_head >> 8, REG_RECVEND);
  306. ether3_outw(0, REG_TRANSMITPTR);
  307. ether3_outw(priv(dev)->regs.config2, REG_CONFIG2);
  308. ether3_outw(priv(dev)->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
  309. ether3_setbuffer(dev, buffer_write, 0);
  310. ether3_writelong(dev, 0);
  311. priv(dev)->regs.command = CMD_ENINTRX | CMD_ENINTTX;
  312. ether3_outw(priv(dev)->regs.command | CMD_RXON, REG_COMMAND);
  313. }
  314. static inline int
  315. ether3_probe_bus_8(struct net_device *dev, int val)
  316. {
  317. int write_low, write_high, read_low, read_high;
  318. write_low = val & 255;
  319. write_high = val >> 8;
  320. printk(KERN_DEBUG "ether3_probe: write8 [%02X:%02X]", write_high, write_low);
  321. ether3_outb(write_low, REG_RECVPTR);
  322. ether3_outb(write_high, REG_RECVPTR + 4);
  323. read_low = ether3_inb(REG_RECVPTR);
  324. read_high = ether3_inb(REG_RECVPTR + 4);
  325. printk(", read8 [%02X:%02X]\n", read_high, read_low);
  326. return read_low == write_low && read_high == write_high;
  327. }
  328. static inline int
  329. ether3_probe_bus_16(struct net_device *dev, int val)
  330. {
  331. int read_val;
  332. ether3_outw(val, REG_RECVPTR);
  333. read_val = ether3_inw(REG_RECVPTR);
  334. printk(KERN_DEBUG "ether3_probe: write16 [%04X], read16 [%04X]\n", val, read_val);
  335. return read_val == val;
  336. }
  337. /*
  338. * Open/initialize the board. This is called (in the current kernel)
  339. * sometime after booting when the 'ifconfig' program is run.
  340. *
  341. * This routine should set everything up anew at each open, even
  342. * registers that "should" only need to be set once at boot, so that
  343. * there is non-reboot way to recover if something goes wrong.
  344. */
  345. static int
  346. ether3_open(struct net_device *dev)
  347. {
  348. if (!is_valid_ether_addr(dev->dev_addr)) {
  349. printk(KERN_WARNING "%s: invalid ethernet MAC address\n",
  350. dev->name);
  351. return -EINVAL;
  352. }
  353. if (request_irq(dev->irq, ether3_interrupt, 0, "ether3", dev))
  354. return -EAGAIN;
  355. ether3_init_for_open(dev);
  356. netif_start_queue(dev);
  357. return 0;
  358. }
  359. /*
  360. * The inverse routine to ether3_open().
  361. */
  362. static int
  363. ether3_close(struct net_device *dev)
  364. {
  365. netif_stop_queue(dev);
  366. disable_irq(dev->irq);
  367. ether3_outw(CMD_RXOFF|CMD_TXOFF, REG_COMMAND);
  368. priv(dev)->regs.command = 0;
  369. while (ether3_inw(REG_STATUS) & (STAT_RXON|STAT_TXON))
  370. barrier();
  371. ether3_outb(0x80, REG_CONFIG2 + 4);
  372. ether3_outw(0, REG_COMMAND);
  373. free_irq(dev->irq, dev);
  374. return 0;
  375. }
  376. /*
  377. * Get the current statistics. This may be called with the card open or
  378. * closed.
  379. */
  380. static struct net_device_stats *ether3_getstats(struct net_device *dev)
  381. {
  382. return &priv(dev)->stats;
  383. }
  384. /*
  385. * Set or clear promiscuous/multicast mode filter for this adaptor.
  386. *
  387. * We don't attempt any packet filtering. The card may have a SEEQ 8004
  388. * in which does not have the other ethernet address registers present...
  389. */
  390. static void ether3_setmulticastlist(struct net_device *dev)
  391. {
  392. priv(dev)->regs.config1 &= ~CFG1_RECVPROMISC;
  393. if (dev->flags & IFF_PROMISC) {
  394. /* promiscuous mode */
  395. priv(dev)->regs.config1 |= CFG1_RECVPROMISC;
  396. } else if (dev->flags & IFF_ALLMULTI) {
  397. priv(dev)->regs.config1 |= CFG1_RECVSPECBRMULTI;
  398. } else
  399. priv(dev)->regs.config1 |= CFG1_RECVSPECBROAD;
  400. ether3_outw(priv(dev)->regs.config1 | CFG1_LOCBUFMEM, REG_CONFIG1);
  401. }
  402. static void ether3_timeout(struct net_device *dev)
  403. {
  404. unsigned long flags;
  405. del_timer(&priv(dev)->timer);
  406. local_irq_save(flags);
  407. printk(KERN_ERR "%s: transmit timed out, network cable problem?\n", dev->name);
  408. printk(KERN_ERR "%s: state: { status=%04X cfg1=%04X cfg2=%04X }\n", dev->name,
  409. ether3_inw(REG_STATUS), ether3_inw(REG_CONFIG1), ether3_inw(REG_CONFIG2));
  410. printk(KERN_ERR "%s: { rpr=%04X rea=%04X tpr=%04X }\n", dev->name,
  411. ether3_inw(REG_RECVPTR), ether3_inw(REG_RECVEND), ether3_inw(REG_TRANSMITPTR));
  412. printk(KERN_ERR "%s: tx head=%X tx tail=%X\n", dev->name,
  413. priv(dev)->tx_head, priv(dev)->tx_tail);
  414. ether3_setbuffer(dev, buffer_read, priv(dev)->tx_tail);
  415. printk(KERN_ERR "%s: packet status = %08X\n", dev->name, ether3_readlong(dev));
  416. local_irq_restore(flags);
  417. priv(dev)->regs.config2 |= CFG2_CTRLO;
  418. priv(dev)->stats.tx_errors += 1;
  419. ether3_outw(priv(dev)->regs.config2, REG_CONFIG2);
  420. priv(dev)->tx_head = priv(dev)->tx_tail = 0;
  421. netif_wake_queue(dev);
  422. }
  423. /*
  424. * Transmit a packet
  425. */
  426. static int
  427. ether3_sendpacket(struct sk_buff *skb, struct net_device *dev)
  428. {
  429. unsigned long flags;
  430. unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
  431. unsigned int ptr, next_ptr;
  432. if (priv(dev)->broken) {
  433. dev_kfree_skb(skb);
  434. priv(dev)->stats.tx_dropped ++;
  435. netif_start_queue(dev);
  436. return 0;
  437. }
  438. length = (length + 1) & ~1;
  439. if (length != skb->len) {
  440. if (skb_padto(skb, length))
  441. goto out;
  442. }
  443. next_ptr = (priv(dev)->tx_head + 1) & 15;
  444. local_irq_save(flags);
  445. if (priv(dev)->tx_tail == next_ptr) {
  446. local_irq_restore(flags);
  447. return 1; /* unable to queue */
  448. }
  449. dev->trans_start = jiffies;
  450. ptr = 0x600 * priv(dev)->tx_head;
  451. priv(dev)->tx_head = next_ptr;
  452. next_ptr *= 0x600;
  453. #define TXHDR_FLAGS (TXHDR_TRANSMIT|TXHDR_CHAINCONTINUE|TXHDR_DATAFOLLOWS|TXHDR_ENSUCCESS)
  454. ether3_setbuffer(dev, buffer_write, next_ptr);
  455. ether3_writelong(dev, 0);
  456. ether3_setbuffer(dev, buffer_write, ptr);
  457. ether3_writelong(dev, 0);
  458. ether3_writebuffer(dev, skb->data, length);
  459. ether3_writeword(dev, htons(next_ptr));
  460. ether3_writeword(dev, TXHDR_CHAINCONTINUE >> 16);
  461. ether3_setbuffer(dev, buffer_write, ptr);
  462. ether3_writeword(dev, htons((ptr + length + 4)));
  463. ether3_writeword(dev, TXHDR_FLAGS >> 16);
  464. ether3_ledon(dev);
  465. if (!(ether3_inw(REG_STATUS) & STAT_TXON)) {
  466. ether3_outw(ptr, REG_TRANSMITPTR);
  467. ether3_outw(priv(dev)->regs.command | CMD_TXON, REG_COMMAND);
  468. }
  469. next_ptr = (priv(dev)->tx_head + 1) & 15;
  470. local_irq_restore(flags);
  471. dev_kfree_skb(skb);
  472. if (priv(dev)->tx_tail == next_ptr)
  473. netif_stop_queue(dev);
  474. out:
  475. return 0;
  476. }
  477. static irqreturn_t
  478. ether3_interrupt(int irq, void *dev_id)
  479. {
  480. struct net_device *dev = (struct net_device *)dev_id;
  481. unsigned int status, handled = IRQ_NONE;
  482. #if NET_DEBUG > 1
  483. if(net_debug & DEBUG_INT)
  484. printk("eth3irq: %d ", irq);
  485. #endif
  486. status = ether3_inw(REG_STATUS);
  487. if (status & STAT_INTRX) {
  488. ether3_outw(CMD_ACKINTRX | priv(dev)->regs.command, REG_COMMAND);
  489. ether3_rx(dev, 12);
  490. handled = IRQ_HANDLED;
  491. }
  492. if (status & STAT_INTTX) {
  493. ether3_outw(CMD_ACKINTTX | priv(dev)->regs.command, REG_COMMAND);
  494. ether3_tx(dev);
  495. handled = IRQ_HANDLED;
  496. }
  497. #if NET_DEBUG > 1
  498. if(net_debug & DEBUG_INT)
  499. printk("done\n");
  500. #endif
  501. return handled;
  502. }
  503. /*
  504. * If we have a good packet(s), get it/them out of the buffers.
  505. */
  506. static int ether3_rx(struct net_device *dev, unsigned int maxcnt)
  507. {
  508. unsigned int next_ptr = priv(dev)->rx_head, received = 0;
  509. ether3_ledon(dev);
  510. do {
  511. unsigned int this_ptr, status;
  512. unsigned char addrs[16];
  513. /*
  514. * read the first 16 bytes from the buffer.
  515. * This contains the status bytes etc and ethernet addresses,
  516. * and we also check the source ethernet address to see if
  517. * it originated from us.
  518. */
  519. {
  520. unsigned int temp_ptr;
  521. ether3_setbuffer(dev, buffer_read, next_ptr);
  522. temp_ptr = ether3_readword(dev);
  523. status = ether3_readword(dev);
  524. if ((status & (RXSTAT_DONE | RXHDR_CHAINCONTINUE | RXHDR_RECEIVE)) !=
  525. (RXSTAT_DONE | RXHDR_CHAINCONTINUE) || !temp_ptr)
  526. break;
  527. this_ptr = next_ptr + 4;
  528. next_ptr = ntohs(temp_ptr);
  529. }
  530. ether3_setbuffer(dev, buffer_read, this_ptr);
  531. ether3_readbuffer(dev, addrs+2, 12);
  532. if (next_ptr < RX_START || next_ptr >= RX_END) {
  533. int i;
  534. printk("%s: bad next pointer @%04X: ", dev->name, priv(dev)->rx_head);
  535. printk("%02X %02X %02X %02X ", next_ptr >> 8, next_ptr & 255, status & 255, status >> 8);
  536. for (i = 2; i < 14; i++)
  537. printk("%02X ", addrs[i]);
  538. printk("\n");
  539. next_ptr = priv(dev)->rx_head;
  540. break;
  541. }
  542. /*
  543. * ignore our own packets...
  544. */
  545. if (!(*(unsigned long *)&dev->dev_addr[0] ^ *(unsigned long *)&addrs[2+6]) &&
  546. !(*(unsigned short *)&dev->dev_addr[4] ^ *(unsigned short *)&addrs[2+10])) {
  547. maxcnt ++; /* compensate for loopedback packet */
  548. ether3_outw(next_ptr >> 8, REG_RECVEND);
  549. } else
  550. if (!(status & (RXSTAT_OVERSIZE|RXSTAT_CRCERROR|RXSTAT_DRIBBLEERROR|RXSTAT_SHORTPACKET))) {
  551. unsigned int length = next_ptr - this_ptr;
  552. struct sk_buff *skb;
  553. if (next_ptr <= this_ptr)
  554. length += RX_END - RX_START;
  555. skb = dev_alloc_skb(length + 2);
  556. if (skb) {
  557. unsigned char *buf;
  558. skb->dev = dev;
  559. skb_reserve(skb, 2);
  560. buf = skb_put(skb, length);
  561. ether3_readbuffer(dev, buf + 12, length - 12);
  562. ether3_outw(next_ptr >> 8, REG_RECVEND);
  563. *(unsigned short *)(buf + 0) = *(unsigned short *)(addrs + 2);
  564. *(unsigned long *)(buf + 2) = *(unsigned long *)(addrs + 4);
  565. *(unsigned long *)(buf + 6) = *(unsigned long *)(addrs + 8);
  566. *(unsigned short *)(buf + 10) = *(unsigned short *)(addrs + 12);
  567. skb->protocol = eth_type_trans(skb, dev);
  568. netif_rx(skb);
  569. received ++;
  570. } else
  571. goto dropping;
  572. } else {
  573. struct net_device_stats *stats = &priv(dev)->stats;
  574. ether3_outw(next_ptr >> 8, REG_RECVEND);
  575. if (status & RXSTAT_OVERSIZE) stats->rx_over_errors ++;
  576. if (status & RXSTAT_CRCERROR) stats->rx_crc_errors ++;
  577. if (status & RXSTAT_DRIBBLEERROR) stats->rx_fifo_errors ++;
  578. if (status & RXSTAT_SHORTPACKET) stats->rx_length_errors ++;
  579. stats->rx_errors++;
  580. }
  581. }
  582. while (-- maxcnt);
  583. done:
  584. priv(dev)->stats.rx_packets += received;
  585. priv(dev)->rx_head = next_ptr;
  586. /*
  587. * If rx went off line, then that means that the buffer may be full. We
  588. * have dropped at least one packet.
  589. */
  590. if (!(ether3_inw(REG_STATUS) & STAT_RXON)) {
  591. priv(dev)->stats.rx_dropped ++;
  592. ether3_outw(next_ptr, REG_RECVPTR);
  593. ether3_outw(priv(dev)->regs.command | CMD_RXON, REG_COMMAND);
  594. }
  595. return maxcnt;
  596. dropping:{
  597. static unsigned long last_warned;
  598. ether3_outw(next_ptr >> 8, REG_RECVEND);
  599. /*
  600. * Don't print this message too many times...
  601. */
  602. if (time_after(jiffies, last_warned + 10 * HZ)) {
  603. last_warned = jiffies;
  604. printk("%s: memory squeeze, dropping packet.\n", dev->name);
  605. }
  606. priv(dev)->stats.rx_dropped ++;
  607. goto done;
  608. }
  609. }
  610. /*
  611. * Update stats for the transmitted packet(s)
  612. */
  613. static void ether3_tx(struct net_device *dev)
  614. {
  615. unsigned int tx_tail = priv(dev)->tx_tail;
  616. int max_work = 14;
  617. do {
  618. unsigned long status;
  619. /*
  620. * Read the packet header
  621. */
  622. ether3_setbuffer(dev, buffer_read, tx_tail * 0x600);
  623. status = ether3_readlong(dev);
  624. /*
  625. * Check to see if this packet has been transmitted
  626. */
  627. if ((status & (TXSTAT_DONE | TXHDR_TRANSMIT)) !=
  628. (TXSTAT_DONE | TXHDR_TRANSMIT))
  629. break;
  630. /*
  631. * Update errors
  632. */
  633. if (!(status & (TXSTAT_BABBLED | TXSTAT_16COLLISIONS)))
  634. priv(dev)->stats.tx_packets++;
  635. else {
  636. priv(dev)->stats.tx_errors ++;
  637. if (status & TXSTAT_16COLLISIONS)
  638. priv(dev)->stats.collisions += 16;
  639. if (status & TXSTAT_BABBLED)
  640. priv(dev)->stats.tx_fifo_errors ++;
  641. }
  642. tx_tail = (tx_tail + 1) & 15;
  643. } while (--max_work);
  644. if (priv(dev)->tx_tail != tx_tail) {
  645. priv(dev)->tx_tail = tx_tail;
  646. netif_wake_queue(dev);
  647. }
  648. }
  649. static void __init ether3_banner(void)
  650. {
  651. static unsigned version_printed = 0;
  652. if (net_debug && version_printed++ == 0)
  653. printk(KERN_INFO "%s", version);
  654. }
  655. static int __devinit
  656. ether3_probe(struct expansion_card *ec, const struct ecard_id *id)
  657. {
  658. const struct ether3_data *data = id->data;
  659. struct net_device *dev;
  660. int i, bus_type, ret;
  661. ether3_banner();
  662. ret = ecard_request_resources(ec);
  663. if (ret)
  664. goto out;
  665. dev = alloc_etherdev(sizeof(struct dev_priv));
  666. if (!dev) {
  667. ret = -ENOMEM;
  668. goto release;
  669. }
  670. SET_MODULE_OWNER(dev);
  671. SET_NETDEV_DEV(dev, &ec->dev);
  672. priv(dev)->base = ioremap(ecard_resource_start(ec, ECARD_RES_MEMC),
  673. ecard_resource_len(ec, ECARD_RES_MEMC));
  674. if (!priv(dev)->base) {
  675. ret = -ENOMEM;
  676. goto free;
  677. }
  678. ec->irqaddr = priv(dev)->base + data->base_offset;
  679. ec->irqmask = 0xf0;
  680. priv(dev)->seeq = priv(dev)->base + data->base_offset;
  681. dev->irq = ec->irq;
  682. ether3_addr(dev->dev_addr, ec);
  683. init_timer(&priv(dev)->timer);
  684. /* Reset card...
  685. */
  686. ether3_outb(0x80, REG_CONFIG2 + 4);
  687. bus_type = BUS_UNKNOWN;
  688. udelay(4);
  689. /* Test using Receive Pointer (16-bit register) to find out
  690. * how the ether3 is connected to the bus...
  691. */
  692. if (ether3_probe_bus_8(dev, 0x100) &&
  693. ether3_probe_bus_8(dev, 0x201))
  694. bus_type = BUS_8;
  695. if (bus_type == BUS_UNKNOWN &&
  696. ether3_probe_bus_16(dev, 0x101) &&
  697. ether3_probe_bus_16(dev, 0x201))
  698. bus_type = BUS_16;
  699. switch (bus_type) {
  700. case BUS_UNKNOWN:
  701. printk(KERN_ERR "%s: unable to identify bus width\n", dev->name);
  702. ret = -ENODEV;
  703. goto free;
  704. case BUS_8:
  705. printk(KERN_ERR "%s: %s found, but is an unsupported "
  706. "8-bit card\n", dev->name, data->name);
  707. ret = -ENODEV;
  708. goto free;
  709. default:
  710. break;
  711. }
  712. if (ether3_init_2(dev)) {
  713. ret = -ENODEV;
  714. goto free;
  715. }
  716. dev->open = ether3_open;
  717. dev->stop = ether3_close;
  718. dev->hard_start_xmit = ether3_sendpacket;
  719. dev->get_stats = ether3_getstats;
  720. dev->set_multicast_list = ether3_setmulticastlist;
  721. dev->tx_timeout = ether3_timeout;
  722. dev->watchdog_timeo = 5 * HZ / 100;
  723. ret = register_netdev(dev);
  724. if (ret)
  725. goto free;
  726. printk("%s: %s in slot %d, ", dev->name, data->name, ec->slot_no);
  727. for (i = 0; i < 6; i++)
  728. printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
  729. ecard_set_drvdata(ec, dev);
  730. return 0;
  731. free:
  732. if (priv(dev)->base)
  733. iounmap(priv(dev)->base);
  734. free_netdev(dev);
  735. release:
  736. ecard_release_resources(ec);
  737. out:
  738. return ret;
  739. }
  740. static void __devexit ether3_remove(struct expansion_card *ec)
  741. {
  742. struct net_device *dev = ecard_get_drvdata(ec);
  743. ecard_set_drvdata(ec, NULL);
  744. unregister_netdev(dev);
  745. iounmap(priv(dev)->base);
  746. free_netdev(dev);
  747. ecard_release_resources(ec);
  748. }
  749. static struct ether3_data ether3 = {
  750. .name = "ether3",
  751. .base_offset = 0,
  752. };
  753. static struct ether3_data etherb = {
  754. .name = "etherb",
  755. .base_offset = 0x800,
  756. };
  757. static const struct ecard_id ether3_ids[] = {
  758. { MANU_ANT2, PROD_ANT_ETHER3, &ether3 },
  759. { MANU_ANT, PROD_ANT_ETHER3, &ether3 },
  760. { MANU_ANT, PROD_ANT_ETHERB, &etherb },
  761. { 0xffff, 0xffff }
  762. };
  763. static struct ecard_driver ether3_driver = {
  764. .probe = ether3_probe,
  765. .remove = __devexit_p(ether3_remove),
  766. .id_table = ether3_ids,
  767. .drv = {
  768. .name = "ether3",
  769. },
  770. };
  771. static int __init ether3_init(void)
  772. {
  773. return ecard_register_driver(&ether3_driver);
  774. }
  775. static void __exit ether3_exit(void)
  776. {
  777. ecard_remove_driver(&ether3_driver);
  778. }
  779. module_init(ether3_init);
  780. module_exit(ether3_exit);
  781. MODULE_LICENSE("GPL");