ether3.c 24 KB

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