ariadne.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * Amiga Linux/m68k Ariadne Ethernet Driver
  3. *
  4. * © Copyright 1995-2003 by Geert Uytterhoeven (geert@linux-m68k.org)
  5. * Peter De Schrijver (p2@mind.be)
  6. *
  7. * ---------------------------------------------------------------------------
  8. *
  9. * This program is based on
  10. *
  11. * lance.c: An AMD LANCE ethernet driver for linux.
  12. * Written 1993-94 by Donald Becker.
  13. *
  14. * Am79C960: PCnet(tm)-ISA Single-Chip Ethernet Controller
  15. * Advanced Micro Devices
  16. * Publication #16907, Rev. B, Amendment/0, May 1994
  17. *
  18. * MC68230: Parallel Interface/Timer (PI/T)
  19. * Motorola Semiconductors, December, 1983
  20. *
  21. * ---------------------------------------------------------------------------
  22. *
  23. * This file is subject to the terms and conditions of the GNU General Public
  24. * License. See the file COPYING in the main directory of the Linux
  25. * distribution for more details.
  26. *
  27. * ---------------------------------------------------------------------------
  28. *
  29. * The Ariadne is a Zorro-II board made by Village Tronic. It contains:
  30. *
  31. * - an Am79C960 PCnet-ISA Single-Chip Ethernet Controller with both
  32. * 10BASE-2 (thin coax) and 10BASE-T (UTP) connectors
  33. *
  34. * - an MC68230 Parallel Interface/Timer configured as 2 parallel ports
  35. */
  36. #include <linux/module.h>
  37. #include <linux/stddef.h>
  38. #include <linux/kernel.h>
  39. #include <linux/string.h>
  40. #include <linux/errno.h>
  41. #include <linux/ioport.h>
  42. #include <linux/slab.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/etherdevice.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/init.h>
  48. #include <linux/zorro.h>
  49. #include <linux/bitops.h>
  50. #include <asm/amigaints.h>
  51. #include <asm/amigahw.h>
  52. #include <asm/irq.h>
  53. #include "ariadne.h"
  54. #ifdef ARIADNE_DEBUG
  55. int ariadne_debug = ARIADNE_DEBUG;
  56. #else
  57. int ariadne_debug = 1;
  58. #endif
  59. /*
  60. * Macros to Fix Endianness problems
  61. */
  62. /* Swap the Bytes in a WORD */
  63. #define swapw(x) (((x>>8)&0x00ff)|((x<<8)&0xff00))
  64. /* Get the Low BYTE in a WORD */
  65. #define lowb(x) (x&0xff)
  66. /* Get the Swapped High WORD in a LONG */
  67. #define swhighw(x) ((((x)>>8)&0xff00)|(((x)>>24)&0x00ff))
  68. /* Get the Swapped Low WORD in a LONG */
  69. #define swloww(x) ((((x)<<8)&0xff00)|(((x)>>8)&0x00ff))
  70. /*
  71. * Transmit/Receive Ring Definitions
  72. */
  73. #define TX_RING_SIZE 5
  74. #define RX_RING_SIZE 16
  75. #define PKT_BUF_SIZE 1520
  76. /*
  77. * Private Device Data
  78. */
  79. struct ariadne_private {
  80. volatile struct TDRE *tx_ring[TX_RING_SIZE];
  81. volatile struct RDRE *rx_ring[RX_RING_SIZE];
  82. volatile u_short *tx_buff[TX_RING_SIZE];
  83. volatile u_short *rx_buff[RX_RING_SIZE];
  84. int cur_tx, cur_rx; /* The next free ring entry */
  85. int dirty_tx; /* The ring entries to be free()ed. */
  86. char tx_full;
  87. };
  88. /*
  89. * Structure Created in the Ariadne's RAM Buffer
  90. */
  91. struct lancedata {
  92. struct TDRE tx_ring[TX_RING_SIZE];
  93. struct RDRE rx_ring[RX_RING_SIZE];
  94. u_short tx_buff[TX_RING_SIZE][PKT_BUF_SIZE/sizeof(u_short)];
  95. u_short rx_buff[RX_RING_SIZE][PKT_BUF_SIZE/sizeof(u_short)];
  96. };
  97. static int ariadne_open(struct net_device *dev);
  98. static void ariadne_init_ring(struct net_device *dev);
  99. static int ariadne_start_xmit(struct sk_buff *skb, struct net_device *dev);
  100. static void ariadne_tx_timeout(struct net_device *dev);
  101. static int ariadne_rx(struct net_device *dev);
  102. static void ariadne_reset(struct net_device *dev);
  103. static irqreturn_t ariadne_interrupt(int irq, void *data);
  104. static int ariadne_close(struct net_device *dev);
  105. static struct net_device_stats *ariadne_get_stats(struct net_device *dev);
  106. #ifdef HAVE_MULTICAST
  107. static void set_multicast_list(struct net_device *dev);
  108. #endif
  109. static void memcpyw(volatile u_short *dest, u_short *src, int len)
  110. {
  111. while (len >= 2) {
  112. *(dest++) = *(src++);
  113. len -= 2;
  114. }
  115. if (len == 1)
  116. *dest = (*(u_char *)src)<<8;
  117. }
  118. static int __devinit ariadne_init_one(struct zorro_dev *z,
  119. const struct zorro_device_id *ent);
  120. static void __devexit ariadne_remove_one(struct zorro_dev *z);
  121. static struct zorro_device_id ariadne_zorro_tbl[] __devinitdata = {
  122. { ZORRO_PROD_VILLAGE_TRONIC_ARIADNE },
  123. { 0 }
  124. };
  125. static struct zorro_driver ariadne_driver = {
  126. .name = "ariadne",
  127. .id_table = ariadne_zorro_tbl,
  128. .probe = ariadne_init_one,
  129. .remove = __devexit_p(ariadne_remove_one),
  130. };
  131. static int __devinit ariadne_init_one(struct zorro_dev *z,
  132. const struct zorro_device_id *ent)
  133. {
  134. unsigned long board = z->resource.start;
  135. unsigned long base_addr = board+ARIADNE_LANCE;
  136. unsigned long mem_start = board+ARIADNE_RAM;
  137. struct resource *r1, *r2;
  138. struct net_device *dev;
  139. struct ariadne_private *priv;
  140. int err;
  141. DECLARE_MAC_BUF(mac);
  142. r1 = request_mem_region(base_addr, sizeof(struct Am79C960), "Am79C960");
  143. if (!r1)
  144. return -EBUSY;
  145. r2 = request_mem_region(mem_start, ARIADNE_RAM_SIZE, "RAM");
  146. if (!r2) {
  147. release_resource(r1);
  148. return -EBUSY;
  149. }
  150. dev = alloc_etherdev(sizeof(struct ariadne_private));
  151. if (dev == NULL) {
  152. release_resource(r1);
  153. release_resource(r2);
  154. return -ENOMEM;
  155. }
  156. priv = netdev_priv(dev);
  157. r1->name = dev->name;
  158. r2->name = dev->name;
  159. dev->dev_addr[0] = 0x00;
  160. dev->dev_addr[1] = 0x60;
  161. dev->dev_addr[2] = 0x30;
  162. dev->dev_addr[3] = (z->rom.er_SerialNumber>>16) & 0xff;
  163. dev->dev_addr[4] = (z->rom.er_SerialNumber>>8) & 0xff;
  164. dev->dev_addr[5] = z->rom.er_SerialNumber & 0xff;
  165. dev->base_addr = ZTWO_VADDR(base_addr);
  166. dev->mem_start = ZTWO_VADDR(mem_start);
  167. dev->mem_end = dev->mem_start+ARIADNE_RAM_SIZE;
  168. dev->open = &ariadne_open;
  169. dev->stop = &ariadne_close;
  170. dev->hard_start_xmit = &ariadne_start_xmit;
  171. dev->tx_timeout = &ariadne_tx_timeout;
  172. dev->watchdog_timeo = 5*HZ;
  173. dev->get_stats = &ariadne_get_stats;
  174. dev->set_multicast_list = &set_multicast_list;
  175. err = register_netdev(dev);
  176. if (err) {
  177. release_resource(r1);
  178. release_resource(r2);
  179. free_netdev(dev);
  180. return err;
  181. }
  182. zorro_set_drvdata(z, dev);
  183. printk(KERN_INFO "%s: Ariadne at 0x%08lx, Ethernet Address "
  184. "%s\n", dev->name, board,
  185. print_mac(mac, dev->dev_addr));
  186. return 0;
  187. }
  188. static int ariadne_open(struct net_device *dev)
  189. {
  190. volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
  191. u_short in;
  192. u_long version;
  193. int i;
  194. /* Reset the LANCE */
  195. in = lance->Reset;
  196. /* Stop the LANCE */
  197. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  198. lance->RDP = STOP;
  199. /* Check the LANCE version */
  200. lance->RAP = CSR88; /* Chip ID */
  201. version = swapw(lance->RDP);
  202. lance->RAP = CSR89; /* Chip ID */
  203. version |= swapw(lance->RDP)<<16;
  204. if ((version & 0x00000fff) != 0x00000003) {
  205. printk(KERN_WARNING "ariadne_open: Couldn't find AMD Ethernet Chip\n");
  206. return -EAGAIN;
  207. }
  208. if ((version & 0x0ffff000) != 0x00003000) {
  209. printk(KERN_WARNING "ariadne_open: Couldn't find Am79C960 (Wrong part "
  210. "number = %ld)\n", (version & 0x0ffff000)>>12);
  211. return -EAGAIN;
  212. }
  213. #if 0
  214. printk(KERN_DEBUG "ariadne_open: Am79C960 (PCnet-ISA) Revision %ld\n",
  215. (version & 0xf0000000)>>28);
  216. #endif
  217. ariadne_init_ring(dev);
  218. /* Miscellaneous Stuff */
  219. lance->RAP = CSR3; /* Interrupt Masks and Deferral Control */
  220. lance->RDP = 0x0000;
  221. lance->RAP = CSR4; /* Test and Features Control */
  222. lance->RDP = DPOLL|APAD_XMT|MFCOM|RCVCCOM|TXSTRTM|JABM;
  223. /* Set the Multicast Table */
  224. lance->RAP = CSR8; /* Logical Address Filter, LADRF[15:0] */
  225. lance->RDP = 0x0000;
  226. lance->RAP = CSR9; /* Logical Address Filter, LADRF[31:16] */
  227. lance->RDP = 0x0000;
  228. lance->RAP = CSR10; /* Logical Address Filter, LADRF[47:32] */
  229. lance->RDP = 0x0000;
  230. lance->RAP = CSR11; /* Logical Address Filter, LADRF[63:48] */
  231. lance->RDP = 0x0000;
  232. /* Set the Ethernet Hardware Address */
  233. lance->RAP = CSR12; /* Physical Address Register, PADR[15:0] */
  234. lance->RDP = ((u_short *)&dev->dev_addr[0])[0];
  235. lance->RAP = CSR13; /* Physical Address Register, PADR[31:16] */
  236. lance->RDP = ((u_short *)&dev->dev_addr[0])[1];
  237. lance->RAP = CSR14; /* Physical Address Register, PADR[47:32] */
  238. lance->RDP = ((u_short *)&dev->dev_addr[0])[2];
  239. /* Set the Init Block Mode */
  240. lance->RAP = CSR15; /* Mode Register */
  241. lance->RDP = 0x0000;
  242. /* Set the Transmit Descriptor Ring Pointer */
  243. lance->RAP = CSR30; /* Base Address of Transmit Ring */
  244. lance->RDP = swloww(ARIADNE_RAM+offsetof(struct lancedata, tx_ring));
  245. lance->RAP = CSR31; /* Base Address of transmit Ring */
  246. lance->RDP = swhighw(ARIADNE_RAM+offsetof(struct lancedata, tx_ring));
  247. /* Set the Receive Descriptor Ring Pointer */
  248. lance->RAP = CSR24; /* Base Address of Receive Ring */
  249. lance->RDP = swloww(ARIADNE_RAM+offsetof(struct lancedata, rx_ring));
  250. lance->RAP = CSR25; /* Base Address of Receive Ring */
  251. lance->RDP = swhighw(ARIADNE_RAM+offsetof(struct lancedata, rx_ring));
  252. /* Set the Number of RX and TX Ring Entries */
  253. lance->RAP = CSR76; /* Receive Ring Length */
  254. lance->RDP = swapw(((u_short)-RX_RING_SIZE));
  255. lance->RAP = CSR78; /* Transmit Ring Length */
  256. lance->RDP = swapw(((u_short)-TX_RING_SIZE));
  257. /* Enable Media Interface Port Auto Select (10BASE-2/10BASE-T) */
  258. lance->RAP = ISACSR2; /* Miscellaneous Configuration */
  259. lance->IDP = ASEL;
  260. /* LED Control */
  261. lance->RAP = ISACSR5; /* LED1 Status */
  262. lance->IDP = PSE|XMTE;
  263. lance->RAP = ISACSR6; /* LED2 Status */
  264. lance->IDP = PSE|COLE;
  265. lance->RAP = ISACSR7; /* LED3 Status */
  266. lance->IDP = PSE|RCVE;
  267. netif_start_queue(dev);
  268. i = request_irq(IRQ_AMIGA_PORTS, ariadne_interrupt, IRQF_SHARED,
  269. dev->name, dev);
  270. if (i) return i;
  271. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  272. lance->RDP = INEA|STRT;
  273. return 0;
  274. }
  275. static void ariadne_init_ring(struct net_device *dev)
  276. {
  277. struct ariadne_private *priv = netdev_priv(dev);
  278. volatile struct lancedata *lancedata = (struct lancedata *)dev->mem_start;
  279. int i;
  280. netif_stop_queue(dev);
  281. priv->tx_full = 0;
  282. priv->cur_rx = priv->cur_tx = 0;
  283. priv->dirty_tx = 0;
  284. /* Set up TX Ring */
  285. for (i = 0; i < TX_RING_SIZE; i++) {
  286. volatile struct TDRE *t = &lancedata->tx_ring[i];
  287. t->TMD0 = swloww(ARIADNE_RAM+offsetof(struct lancedata, tx_buff[i]));
  288. t->TMD1 = swhighw(ARIADNE_RAM+offsetof(struct lancedata, tx_buff[i])) |
  289. TF_STP | TF_ENP;
  290. t->TMD2 = swapw((u_short)-PKT_BUF_SIZE);
  291. t->TMD3 = 0;
  292. priv->tx_ring[i] = &lancedata->tx_ring[i];
  293. priv->tx_buff[i] = lancedata->tx_buff[i];
  294. #if 0
  295. printk(KERN_DEBUG "TX Entry %2d at %p, Buf at %p\n", i,
  296. &lancedata->tx_ring[i], lancedata->tx_buff[i]);
  297. #endif
  298. }
  299. /* Set up RX Ring */
  300. for (i = 0; i < RX_RING_SIZE; i++) {
  301. volatile struct RDRE *r = &lancedata->rx_ring[i];
  302. r->RMD0 = swloww(ARIADNE_RAM+offsetof(struct lancedata, rx_buff[i]));
  303. r->RMD1 = swhighw(ARIADNE_RAM+offsetof(struct lancedata, rx_buff[i])) |
  304. RF_OWN;
  305. r->RMD2 = swapw((u_short)-PKT_BUF_SIZE);
  306. r->RMD3 = 0x0000;
  307. priv->rx_ring[i] = &lancedata->rx_ring[i];
  308. priv->rx_buff[i] = lancedata->rx_buff[i];
  309. #if 0
  310. printk(KERN_DEBUG "RX Entry %2d at %p, Buf at %p\n", i,
  311. &lancedata->rx_ring[i], lancedata->rx_buff[i]);
  312. #endif
  313. }
  314. }
  315. static int ariadne_close(struct net_device *dev)
  316. {
  317. volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
  318. netif_stop_queue(dev);
  319. lance->RAP = CSR112; /* Missed Frame Count */
  320. dev->stats.rx_missed_errors = swapw(lance->RDP);
  321. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  322. if (ariadne_debug > 1) {
  323. printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
  324. dev->name, lance->RDP);
  325. printk(KERN_DEBUG "%s: %lu packets missed\n", dev->name,
  326. dev->stats.rx_missed_errors);
  327. }
  328. /* We stop the LANCE here -- it occasionally polls memory if we don't. */
  329. lance->RDP = STOP;
  330. free_irq(IRQ_AMIGA_PORTS, dev);
  331. return 0;
  332. }
  333. static inline void ariadne_reset(struct net_device *dev)
  334. {
  335. volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
  336. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  337. lance->RDP = STOP;
  338. ariadne_init_ring(dev);
  339. lance->RDP = INEA|STRT;
  340. netif_start_queue(dev);
  341. }
  342. static irqreturn_t ariadne_interrupt(int irq, void *data)
  343. {
  344. struct net_device *dev = (struct net_device *)data;
  345. volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
  346. struct ariadne_private *priv;
  347. int csr0, boguscnt;
  348. int handled = 0;
  349. if (dev == NULL) {
  350. printk(KERN_WARNING "ariadne_interrupt(): irq for unknown device.\n");
  351. return IRQ_NONE;
  352. }
  353. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  354. if (!(lance->RDP & INTR)) /* Check if any interrupt has been */
  355. return IRQ_NONE; /* generated by the board. */
  356. priv = netdev_priv(dev);
  357. boguscnt = 10;
  358. while ((csr0 = lance->RDP) & (ERR|RINT|TINT) && --boguscnt >= 0) {
  359. /* Acknowledge all of the current interrupt sources ASAP. */
  360. lance->RDP = csr0 & ~(INEA|TDMD|STOP|STRT|INIT);
  361. #if 0
  362. if (ariadne_debug > 5) {
  363. printk(KERN_DEBUG "%s: interrupt csr0=%#2.2x new csr=%#2.2x.",
  364. dev->name, csr0, lance->RDP);
  365. printk("[");
  366. if (csr0 & INTR)
  367. printk(" INTR");
  368. if (csr0 & INEA)
  369. printk(" INEA");
  370. if (csr0 & RXON)
  371. printk(" RXON");
  372. if (csr0 & TXON)
  373. printk(" TXON");
  374. if (csr0 & TDMD)
  375. printk(" TDMD");
  376. if (csr0 & STOP)
  377. printk(" STOP");
  378. if (csr0 & STRT)
  379. printk(" STRT");
  380. if (csr0 & INIT)
  381. printk(" INIT");
  382. if (csr0 & ERR)
  383. printk(" ERR");
  384. if (csr0 & BABL)
  385. printk(" BABL");
  386. if (csr0 & CERR)
  387. printk(" CERR");
  388. if (csr0 & MISS)
  389. printk(" MISS");
  390. if (csr0 & MERR)
  391. printk(" MERR");
  392. if (csr0 & RINT)
  393. printk(" RINT");
  394. if (csr0 & TINT)
  395. printk(" TINT");
  396. if (csr0 & IDON)
  397. printk(" IDON");
  398. printk(" ]\n");
  399. }
  400. #endif
  401. if (csr0 & RINT) { /* Rx interrupt */
  402. handled = 1;
  403. ariadne_rx(dev);
  404. }
  405. if (csr0 & TINT) { /* Tx-done interrupt */
  406. int dirty_tx = priv->dirty_tx;
  407. handled = 1;
  408. while (dirty_tx < priv->cur_tx) {
  409. int entry = dirty_tx % TX_RING_SIZE;
  410. int status = lowb(priv->tx_ring[entry]->TMD1);
  411. if (status & TF_OWN)
  412. break; /* It still hasn't been Txed */
  413. priv->tx_ring[entry]->TMD1 &= 0xff00;
  414. if (status & TF_ERR) {
  415. /* There was an major error, log it. */
  416. int err_status = priv->tx_ring[entry]->TMD3;
  417. dev->stats.tx_errors++;
  418. if (err_status & EF_RTRY)
  419. dev->stats.tx_aborted_errors++;
  420. if (err_status & EF_LCAR)
  421. dev->stats.tx_carrier_errors++;
  422. if (err_status & EF_LCOL)
  423. dev->stats.tx_window_errors++;
  424. if (err_status & EF_UFLO) {
  425. /* Ackk! On FIFO errors the Tx unit is turned off! */
  426. dev->stats.tx_fifo_errors++;
  427. /* Remove this verbosity later! */
  428. printk(KERN_ERR "%s: Tx FIFO error! Status %4.4x.\n",
  429. dev->name, csr0);
  430. /* Restart the chip. */
  431. lance->RDP = STRT;
  432. }
  433. } else {
  434. if (status & (TF_MORE|TF_ONE))
  435. dev->stats.collisions++;
  436. dev->stats.tx_packets++;
  437. }
  438. dirty_tx++;
  439. }
  440. #ifndef final_version
  441. if (priv->cur_tx - dirty_tx >= TX_RING_SIZE) {
  442. printk(KERN_ERR "out-of-sync dirty pointer, %d vs. %d, "
  443. "full=%d.\n", dirty_tx, priv->cur_tx, priv->tx_full);
  444. dirty_tx += TX_RING_SIZE;
  445. }
  446. #endif
  447. if (priv->tx_full && netif_queue_stopped(dev) &&
  448. dirty_tx > priv->cur_tx - TX_RING_SIZE + 2) {
  449. /* The ring is no longer full. */
  450. priv->tx_full = 0;
  451. netif_wake_queue(dev);
  452. }
  453. priv->dirty_tx = dirty_tx;
  454. }
  455. /* Log misc errors. */
  456. if (csr0 & BABL) {
  457. handled = 1;
  458. dev->stats.tx_errors++; /* Tx babble. */
  459. }
  460. if (csr0 & MISS) {
  461. handled = 1;
  462. dev->stats.rx_errors++; /* Missed a Rx frame. */
  463. }
  464. if (csr0 & MERR) {
  465. handled = 1;
  466. printk(KERN_ERR "%s: Bus master arbitration failure, status "
  467. "%4.4x.\n", dev->name, csr0);
  468. /* Restart the chip. */
  469. lance->RDP = STRT;
  470. }
  471. }
  472. /* Clear any other interrupt, and set interrupt enable. */
  473. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  474. lance->RDP = INEA|BABL|CERR|MISS|MERR|IDON;
  475. #if 0
  476. if (ariadne_debug > 4)
  477. printk(KERN_DEBUG "%s: exiting interrupt, csr%d=%#4.4x.\n", dev->name,
  478. lance->RAP, lance->RDP);
  479. #endif
  480. return IRQ_RETVAL(handled);
  481. }
  482. static void ariadne_tx_timeout(struct net_device *dev)
  483. {
  484. volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
  485. printk(KERN_ERR "%s: transmit timed out, status %4.4x, resetting.\n",
  486. dev->name, lance->RDP);
  487. ariadne_reset(dev);
  488. netif_wake_queue(dev);
  489. }
  490. static int ariadne_start_xmit(struct sk_buff *skb, struct net_device *dev)
  491. {
  492. struct ariadne_private *priv = netdev_priv(dev);
  493. volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
  494. int entry;
  495. unsigned long flags;
  496. int len = skb->len;
  497. #if 0
  498. if (ariadne_debug > 3) {
  499. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  500. printk(KERN_DEBUG "%s: ariadne_start_xmit() called, csr0 %4.4x.\n",
  501. dev->name, lance->RDP);
  502. lance->RDP = 0x0000;
  503. }
  504. #endif
  505. /* FIXME: is the 79C960 new enough to do its own padding right ? */
  506. if (skb->len < ETH_ZLEN)
  507. {
  508. if (skb_padto(skb, ETH_ZLEN))
  509. return 0;
  510. len = ETH_ZLEN;
  511. }
  512. /* Fill in a Tx ring entry */
  513. #if 0
  514. {
  515. DECLARE_MAC_BUF(mac);
  516. DECLARE_MAC_BUF(mac2);
  517. printk(KERN_DEBUG "TX pkt type 0x%04x from %s to %s "
  518. " data 0x%08x len %d\n",
  519. ((u_short *)skb->data)[6],
  520. print_mac(mac, ((const u8 *)skb->data)+6),
  521. print_mac(mac, (const u8 *)skb->data),
  522. (int)skb->data, (int)skb->len);
  523. }
  524. #endif
  525. local_irq_save(flags);
  526. entry = priv->cur_tx % TX_RING_SIZE;
  527. /* Caution: the write order is important here, set the base address with
  528. the "ownership" bits last. */
  529. priv->tx_ring[entry]->TMD2 = swapw((u_short)-skb->len);
  530. priv->tx_ring[entry]->TMD3 = 0x0000;
  531. memcpyw(priv->tx_buff[entry], (u_short *)skb->data, len);
  532. #if 0
  533. {
  534. int i, len;
  535. len = skb->len > 64 ? 64 : skb->len;
  536. len >>= 1;
  537. for (i = 0; i < len; i += 8) {
  538. int j;
  539. printk(KERN_DEBUG "%04x:", i);
  540. for (j = 0; (j < 8) && ((i+j) < len); j++) {
  541. if (!(j & 1))
  542. printk(" ");
  543. printk("%04x", priv->tx_buff[entry][i+j]);
  544. }
  545. printk("\n");
  546. }
  547. }
  548. #endif
  549. priv->tx_ring[entry]->TMD1 = (priv->tx_ring[entry]->TMD1&0xff00)|TF_OWN|TF_STP|TF_ENP;
  550. dev_kfree_skb(skb);
  551. priv->cur_tx++;
  552. if ((priv->cur_tx >= TX_RING_SIZE) && (priv->dirty_tx >= TX_RING_SIZE)) {
  553. #if 0
  554. printk(KERN_DEBUG "*** Subtracting TX_RING_SIZE from cur_tx (%d) and "
  555. "dirty_tx (%d)\n", priv->cur_tx, priv->dirty_tx);
  556. #endif
  557. priv->cur_tx -= TX_RING_SIZE;
  558. priv->dirty_tx -= TX_RING_SIZE;
  559. }
  560. dev->stats.tx_bytes += len;
  561. /* Trigger an immediate send poll. */
  562. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  563. lance->RDP = INEA|TDMD;
  564. dev->trans_start = jiffies;
  565. if (lowb(priv->tx_ring[(entry+1) % TX_RING_SIZE]->TMD1) != 0) {
  566. netif_stop_queue(dev);
  567. priv->tx_full = 1;
  568. }
  569. local_irq_restore(flags);
  570. return 0;
  571. }
  572. static int ariadne_rx(struct net_device *dev)
  573. {
  574. struct ariadne_private *priv = netdev_priv(dev);
  575. int entry = priv->cur_rx % RX_RING_SIZE;
  576. int i;
  577. /* If we own the next entry, it's a new packet. Send it up. */
  578. while (!(lowb(priv->rx_ring[entry]->RMD1) & RF_OWN)) {
  579. int status = lowb(priv->rx_ring[entry]->RMD1);
  580. if (status != (RF_STP|RF_ENP)) { /* There was an error. */
  581. /* There is a tricky error noted by John Murphy,
  582. <murf@perftech.com> to Russ Nelson: Even with full-sized
  583. buffers it's possible for a jabber packet to use two
  584. buffers, with only the last correctly noting the error. */
  585. if (status & RF_ENP)
  586. /* Only count a general error at the end of a packet.*/
  587. dev->stats.rx_errors++;
  588. if (status & RF_FRAM)
  589. dev->stats.rx_frame_errors++;
  590. if (status & RF_OFLO)
  591. dev->stats.rx_over_errors++;
  592. if (status & RF_CRC)
  593. dev->stats.rx_crc_errors++;
  594. if (status & RF_BUFF)
  595. dev->stats.rx_fifo_errors++;
  596. priv->rx_ring[entry]->RMD1 &= 0xff00|RF_STP|RF_ENP;
  597. } else {
  598. /* Malloc up new buffer, compatible with net-3. */
  599. short pkt_len = swapw(priv->rx_ring[entry]->RMD3);
  600. struct sk_buff *skb;
  601. skb = dev_alloc_skb(pkt_len+2);
  602. if (skb == NULL) {
  603. printk(KERN_WARNING "%s: Memory squeeze, deferring packet.\n",
  604. dev->name);
  605. for (i = 0; i < RX_RING_SIZE; i++)
  606. if (lowb(priv->rx_ring[(entry+i) % RX_RING_SIZE]->RMD1) & RF_OWN)
  607. break;
  608. if (i > RX_RING_SIZE-2) {
  609. dev->stats.rx_dropped++;
  610. priv->rx_ring[entry]->RMD1 |= RF_OWN;
  611. priv->cur_rx++;
  612. }
  613. break;
  614. }
  615. skb_reserve(skb,2); /* 16 byte align */
  616. skb_put(skb,pkt_len); /* Make room */
  617. skb_copy_to_linear_data(skb, (char *)priv->rx_buff[entry], pkt_len);
  618. skb->protocol=eth_type_trans(skb,dev);
  619. #if 0
  620. {
  621. DECLARE_MAC_BUF(mac);
  622. printk(KERN_DEBUG "RX pkt type 0x%04x from ",
  623. ((u_short *)skb->data)[6]);
  624. {
  625. u_char *ptr = &((u_char *)skb->data)[6];
  626. printk("%s", print_mac(mac, ptr));
  627. }
  628. printk(" to ");
  629. {
  630. u_char *ptr = (u_char *)skb->data;
  631. printk("%s", print_mac(mac, ptr));
  632. }
  633. printk(" data 0x%08x len %d\n", (int)skb->data, (int)skb->len);
  634. }
  635. #endif
  636. netif_rx(skb);
  637. dev->last_rx = jiffies;
  638. dev->stats.rx_packets++;
  639. dev->stats.rx_bytes += pkt_len;
  640. }
  641. priv->rx_ring[entry]->RMD1 |= RF_OWN;
  642. entry = (++priv->cur_rx) % RX_RING_SIZE;
  643. }
  644. priv->cur_rx = priv->cur_rx % RX_RING_SIZE;
  645. /* We should check that at least two ring entries are free. If not,
  646. we should free one and mark stats->rx_dropped++. */
  647. return 0;
  648. }
  649. static struct net_device_stats *ariadne_get_stats(struct net_device *dev)
  650. {
  651. volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
  652. short saved_addr;
  653. unsigned long flags;
  654. local_irq_save(flags);
  655. saved_addr = lance->RAP;
  656. lance->RAP = CSR112; /* Missed Frame Count */
  657. dev->stats.rx_missed_errors = swapw(lance->RDP);
  658. lance->RAP = saved_addr;
  659. local_irq_restore(flags);
  660. return &dev->stats;
  661. }
  662. /* Set or clear the multicast filter for this adaptor.
  663. num_addrs == -1 Promiscuous mode, receive all packets
  664. num_addrs == 0 Normal mode, clear multicast list
  665. num_addrs > 0 Multicast mode, receive normal and MC packets, and do
  666. best-effort filtering.
  667. */
  668. static void set_multicast_list(struct net_device *dev)
  669. {
  670. volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
  671. if (!netif_running(dev))
  672. return;
  673. netif_stop_queue(dev);
  674. /* We take the simple way out and always enable promiscuous mode. */
  675. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  676. lance->RDP = STOP; /* Temporarily stop the lance. */
  677. ariadne_init_ring(dev);
  678. if (dev->flags & IFF_PROMISC) {
  679. lance->RAP = CSR15; /* Mode Register */
  680. lance->RDP = PROM; /* Set promiscuous mode */
  681. } else {
  682. short multicast_table[4];
  683. int num_addrs = dev->mc_count;
  684. int i;
  685. /* We don't use the multicast table, but rely on upper-layer filtering. */
  686. memset(multicast_table, (num_addrs == 0) ? 0 : -1,
  687. sizeof(multicast_table));
  688. for (i = 0; i < 4; i++) {
  689. lance->RAP = CSR8+(i<<8); /* Logical Address Filter */
  690. lance->RDP = swapw(multicast_table[i]);
  691. }
  692. lance->RAP = CSR15; /* Mode Register */
  693. lance->RDP = 0x0000; /* Unset promiscuous mode */
  694. }
  695. lance->RAP = CSR0; /* PCnet-ISA Controller Status */
  696. lance->RDP = INEA|STRT|IDON; /* Resume normal operation. */
  697. netif_wake_queue(dev);
  698. }
  699. static void __devexit ariadne_remove_one(struct zorro_dev *z)
  700. {
  701. struct net_device *dev = zorro_get_drvdata(z);
  702. unregister_netdev(dev);
  703. release_mem_region(ZTWO_PADDR(dev->base_addr), sizeof(struct Am79C960));
  704. release_mem_region(ZTWO_PADDR(dev->mem_start), ARIADNE_RAM_SIZE);
  705. free_netdev(dev);
  706. }
  707. static int __init ariadne_init_module(void)
  708. {
  709. return zorro_register_driver(&ariadne_driver);
  710. }
  711. static void __exit ariadne_cleanup_module(void)
  712. {
  713. zorro_unregister_driver(&ariadne_driver);
  714. }
  715. module_init(ariadne_init_module);
  716. module_exit(ariadne_cleanup_module);
  717. MODULE_LICENSE("GPL");