ether3.c 23 KB

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