znet.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /* znet.c: An Zenith Z-Note ethernet driver for linux. */
  2. /*
  3. Written by Donald Becker.
  4. The author may be reached as becker@scyld.com.
  5. This driver is based on the Linux skeleton driver. The copyright of the
  6. skeleton driver is held by the United States Government, as represented
  7. by DIRNSA, and it is released under the GPL.
  8. Thanks to Mike Hollick for alpha testing and suggestions.
  9. References:
  10. The Crynwr packet driver.
  11. "82593 CSMA/CD Core LAN Controller" Intel datasheet, 1992
  12. Intel Microcommunications Databook, Vol. 1, 1990.
  13. As usual with Intel, the documentation is incomplete and inaccurate.
  14. I had to read the Crynwr packet driver to figure out how to actually
  15. use the i82593, and guess at what register bits matched the loosely
  16. related i82586.
  17. Theory of Operation
  18. The i82593 used in the Zenith Z-Note series operates using two(!) slave
  19. DMA channels, one interrupt, and one 8-bit I/O port.
  20. While there several ways to configure '593 DMA system, I chose the one
  21. that seemed commensurate with the highest system performance in the face
  22. of moderate interrupt latency: Both DMA channels are configured as
  23. recirculating ring buffers, with one channel (#0) dedicated to Rx and
  24. the other channel (#1) to Tx and configuration. (Note that this is
  25. different than the Crynwr driver, where the Tx DMA channel is initialized
  26. before each operation. That approach simplifies operation and Tx error
  27. recovery, but requires additional I/O in normal operation and precludes
  28. transmit buffer chaining.)
  29. Both rings are set to 8192 bytes using {TX,RX}_RING_SIZE. This provides
  30. a reasonable ring size for Rx, while simplifying DMA buffer allocation --
  31. DMA buffers must not cross a 128K boundary. (In truth the size selection
  32. was influenced by my lack of '593 documentation. I thus was constrained
  33. to use the Crynwr '593 initialization table, which sets the Rx ring size
  34. to 8K.)
  35. Despite my usual low opinion about Intel-designed parts, I must admit
  36. that the bulk data handling of the i82593 is a good design for
  37. an integrated system, like a laptop, where using two slave DMA channels
  38. doesn't pose a problem. I still take issue with using only a single I/O
  39. port. In the same controlled environment there are essentially no
  40. limitations on I/O space, and using multiple locations would eliminate
  41. the need for multiple operations when looking at status registers,
  42. setting the Rx ring boundary, or switching to promiscuous mode.
  43. I also question Zenith's selection of the '593: one of the advertised
  44. advantages of earlier Intel parts was that if you figured out the magic
  45. initialization incantation you could use the same part on many different
  46. network types. Zenith's use of the "FriendlyNet" (sic) connector rather
  47. than an on-board transceiver leads me to believe that they were planning
  48. to take advantage of this. But, uhmmm, the '593 omits all but ethernet
  49. functionality from the serial subsystem.
  50. */
  51. /* 10/2002
  52. o Resurected for Linux 2.5+ by Marc Zyngier <maz@wild-wind.fr.eu.org> :
  53. - Removed strange DMA snooping in znet_sent_packet, which lead to
  54. TX buffer corruption on my laptop.
  55. - Use init_etherdev stuff.
  56. - Use kmalloc-ed DMA buffers.
  57. - Use as few global variables as possible.
  58. - Use proper resources management.
  59. - Use wireless/i82593.h as much as possible (structure, constants)
  60. - Compiles as module or build-in.
  61. - Now survives unplugging/replugging cable.
  62. Some code was taken from wavelan_cs.
  63. Tested on a vintage Zenith Z-Note 433Lnp+. Probably broken on
  64. anything else. Testers (and detailed bug reports) are welcome :-).
  65. o TODO :
  66. - Properly handle multicast
  67. - Understand why some traffic patterns add a 1s latency...
  68. */
  69. #include <linux/module.h>
  70. #include <linux/kernel.h>
  71. #include <linux/string.h>
  72. #include <linux/slab.h>
  73. #include <linux/errno.h>
  74. #include <linux/interrupt.h>
  75. #include <linux/ioport.h>
  76. #include <linux/init.h>
  77. #include <linux/delay.h>
  78. #include <linux/netdevice.h>
  79. #include <linux/etherdevice.h>
  80. #include <linux/skbuff.h>
  81. #include <linux/if_arp.h>
  82. #include <linux/bitops.h>
  83. #include <asm/system.h>
  84. #include <asm/io.h>
  85. #include <asm/dma.h>
  86. #include <linux/i82593.h>
  87. static char version[] __initdata = "znet.c:v1.02 9/23/94 becker@scyld.com\n";
  88. #ifndef ZNET_DEBUG
  89. #define ZNET_DEBUG 1
  90. #endif
  91. static unsigned int znet_debug = ZNET_DEBUG;
  92. module_param (znet_debug, int, 0);
  93. MODULE_PARM_DESC (znet_debug, "ZNet debug level");
  94. MODULE_LICENSE("GPL");
  95. /* The DMA modes we need aren't in <dma.h>. */
  96. #define DMA_RX_MODE 0x14 /* Auto init, I/O to mem, ++, demand. */
  97. #define DMA_TX_MODE 0x18 /* Auto init, Mem to I/O, ++, demand. */
  98. #define dma_page_eq(ptr1, ptr2) ((long)(ptr1)>>17 == (long)(ptr2)>>17)
  99. #define RX_BUF_SIZE 8192
  100. #define TX_BUF_SIZE 8192
  101. #define DMA_BUF_SIZE (RX_BUF_SIZE + 16) /* 8k + 16 bytes for trailers */
  102. #define TX_TIMEOUT (HZ/10)
  103. struct znet_private {
  104. int rx_dma, tx_dma;
  105. spinlock_t lock;
  106. short sia_base, sia_size, io_size;
  107. struct i82593_conf_block i593_init;
  108. /* The starting, current, and end pointers for the packet buffers. */
  109. ushort *rx_start, *rx_cur, *rx_end;
  110. ushort *tx_start, *tx_cur, *tx_end;
  111. ushort tx_buf_len; /* Tx buffer length, in words. */
  112. };
  113. /* Only one can be built-in;-> */
  114. static struct net_device *znet_dev;
  115. struct netidblk {
  116. char magic[8]; /* The magic number (string) "NETIDBLK" */
  117. unsigned char netid[8]; /* The physical station address */
  118. char nettype, globalopt;
  119. char vendor[8]; /* The machine vendor and product name. */
  120. char product[8];
  121. char irq1, irq2; /* Interrupts, only one is currently used. */
  122. char dma1, dma2;
  123. short dma_mem_misc[8]; /* DMA buffer locations (unused in Linux). */
  124. short iobase1, iosize1;
  125. short iobase2, iosize2; /* Second iobase unused. */
  126. char driver_options; /* Misc. bits */
  127. char pad;
  128. };
  129. static int znet_open(struct net_device *dev);
  130. static netdev_tx_t znet_send_packet(struct sk_buff *skb,
  131. struct net_device *dev);
  132. static irqreturn_t znet_interrupt(int irq, void *dev_id);
  133. static void znet_rx(struct net_device *dev);
  134. static int znet_close(struct net_device *dev);
  135. static void hardware_init(struct net_device *dev);
  136. static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset);
  137. static void znet_tx_timeout (struct net_device *dev);
  138. /* Request needed resources */
  139. static int znet_request_resources (struct net_device *dev)
  140. {
  141. struct znet_private *znet = netdev_priv(dev);
  142. if (request_irq (dev->irq, znet_interrupt, 0, "ZNet", dev))
  143. goto failed;
  144. if (request_dma (znet->rx_dma, "ZNet rx"))
  145. goto free_irq;
  146. if (request_dma (znet->tx_dma, "ZNet tx"))
  147. goto free_rx_dma;
  148. if (!request_region (znet->sia_base, znet->sia_size, "ZNet SIA"))
  149. goto free_tx_dma;
  150. if (!request_region (dev->base_addr, znet->io_size, "ZNet I/O"))
  151. goto free_sia;
  152. return 0; /* Happy ! */
  153. free_sia:
  154. release_region (znet->sia_base, znet->sia_size);
  155. free_tx_dma:
  156. free_dma (znet->tx_dma);
  157. free_rx_dma:
  158. free_dma (znet->rx_dma);
  159. free_irq:
  160. free_irq (dev->irq, dev);
  161. failed:
  162. return -1;
  163. }
  164. static void znet_release_resources (struct net_device *dev)
  165. {
  166. struct znet_private *znet = netdev_priv(dev);
  167. release_region (znet->sia_base, znet->sia_size);
  168. release_region (dev->base_addr, znet->io_size);
  169. free_dma (znet->tx_dma);
  170. free_dma (znet->rx_dma);
  171. free_irq (dev->irq, dev);
  172. }
  173. /* Keep the magical SIA stuff in a single function... */
  174. static void znet_transceiver_power (struct net_device *dev, int on)
  175. {
  176. struct znet_private *znet = netdev_priv(dev);
  177. unsigned char v;
  178. /* Turn on/off the 82501 SIA, using zenith-specific magic. */
  179. /* Select LAN control register */
  180. outb(0x10, znet->sia_base);
  181. if (on)
  182. v = inb(znet->sia_base + 1) | 0x84;
  183. else
  184. v = inb(znet->sia_base + 1) & ~0x84;
  185. outb(v, znet->sia_base+1); /* Turn on/off LAN power (bit 2). */
  186. }
  187. /* Init the i82593, with current promisc/mcast configuration.
  188. Also used from hardware_init. */
  189. static void znet_set_multicast_list (struct net_device *dev)
  190. {
  191. struct znet_private *znet = netdev_priv(dev);
  192. short ioaddr = dev->base_addr;
  193. struct i82593_conf_block *cfblk = &znet->i593_init;
  194. memset(cfblk, 0x00, sizeof(struct i82593_conf_block));
  195. /* The configuration block. What an undocumented nightmare.
  196. The first set of values are those suggested (without explanation)
  197. for ethernet in the Intel 82586 databook. The rest appear to be
  198. completely undocumented, except for cryptic notes in the Crynwr
  199. packet driver. This driver uses the Crynwr values verbatim. */
  200. /* maz : Rewritten to take advantage of the wanvelan includes.
  201. At least we have names, not just blind values */
  202. /* Byte 0 */
  203. cfblk->fifo_limit = 10; /* = 16 B rx and 80 B tx fifo thresholds */
  204. cfblk->forgnesi = 0; /* 0=82C501, 1=AMD7992B compatibility */
  205. cfblk->fifo_32 = 1;
  206. cfblk->d6mod = 0; /* Run in i82593 advanced mode */
  207. cfblk->throttle_enb = 1;
  208. /* Byte 1 */
  209. cfblk->throttle = 8; /* Continuous w/interrupts, 128-clock DMA. */
  210. cfblk->cntrxint = 0; /* enable continuous mode receive interrupts */
  211. cfblk->contin = 1; /* enable continuous mode */
  212. /* Byte 2 */
  213. cfblk->addr_len = ETH_ALEN;
  214. cfblk->acloc = 1; /* Disable source addr insertion by i82593 */
  215. cfblk->preamb_len = 2; /* 8 bytes preamble */
  216. cfblk->loopback = 0; /* Loopback off */
  217. /* Byte 3 */
  218. cfblk->lin_prio = 0; /* Default priorities & backoff methods. */
  219. cfblk->tbofstop = 0;
  220. cfblk->exp_prio = 0;
  221. cfblk->bof_met = 0;
  222. /* Byte 4 */
  223. cfblk->ifrm_spc = 6; /* 96 bit times interframe spacing */
  224. /* Byte 5 */
  225. cfblk->slottim_low = 0; /* 512 bit times slot time (low) */
  226. /* Byte 6 */
  227. cfblk->slottim_hi = 2; /* 512 bit times slot time (high) */
  228. cfblk->max_retr = 15; /* 15 collisions retries */
  229. /* Byte 7 */
  230. cfblk->prmisc = ((dev->flags & IFF_PROMISC) ? 1 : 0); /* Promiscuous mode */
  231. cfblk->bc_dis = 0; /* Enable broadcast reception */
  232. cfblk->crs_1 = 0; /* Don't transmit without carrier sense */
  233. cfblk->nocrc_ins = 0; /* i82593 generates CRC */
  234. cfblk->crc_1632 = 0; /* 32-bit Autodin-II CRC */
  235. cfblk->crs_cdt = 0; /* CD not to be interpreted as CS */
  236. /* Byte 8 */
  237. cfblk->cs_filter = 0; /* CS is recognized immediately */
  238. cfblk->crs_src = 0; /* External carrier sense */
  239. cfblk->cd_filter = 0; /* CD is recognized immediately */
  240. /* Byte 9 */
  241. cfblk->min_fr_len = ETH_ZLEN >> 2; /* Minimum frame length */
  242. /* Byte A */
  243. cfblk->lng_typ = 1; /* Type/length checks OFF */
  244. cfblk->lng_fld = 1; /* Disable 802.3 length field check */
  245. cfblk->rxcrc_xf = 1; /* Don't transfer CRC to memory */
  246. cfblk->artx = 1; /* Disable automatic retransmission */
  247. cfblk->sarec = 1; /* Disable source addr trig of CD */
  248. cfblk->tx_jabber = 0; /* Disable jabber jam sequence */
  249. cfblk->hash_1 = 1; /* Use bits 0-5 in mc address hash */
  250. cfblk->lbpkpol = 0; /* Loopback pin active high */
  251. /* Byte B */
  252. cfblk->fdx = 0; /* Disable full duplex operation */
  253. /* Byte C */
  254. cfblk->dummy_6 = 0x3f; /* all ones, Default multicast addresses & backoff. */
  255. cfblk->mult_ia = 0; /* No multiple individual addresses */
  256. cfblk->dis_bof = 0; /* Disable the backoff algorithm ?! */
  257. /* Byte D */
  258. cfblk->dummy_1 = 1; /* set to 1 */
  259. cfblk->tx_ifs_retrig = 3; /* Hmm... Disabled */
  260. cfblk->mc_all = (!netdev_mc_empty(dev) ||
  261. (dev->flags & IFF_ALLMULTI)); /* multicast all mode */
  262. cfblk->rcv_mon = 0; /* Monitor mode disabled */
  263. cfblk->frag_acpt = 0; /* Do not accept fragments */
  264. cfblk->tstrttrs = 0; /* No start transmission threshold */
  265. /* Byte E */
  266. cfblk->fretx = 1; /* FIFO automatic retransmission */
  267. cfblk->runt_eop = 0; /* drop "runt" packets */
  268. cfblk->hw_sw_pin = 0; /* ?? */
  269. cfblk->big_endn = 0; /* Big Endian ? no... */
  270. cfblk->syncrqs = 1; /* Synchronous DRQ deassertion... */
  271. cfblk->sttlen = 1; /* 6 byte status registers */
  272. cfblk->rx_eop = 0; /* Signal EOP on packet reception */
  273. cfblk->tx_eop = 0; /* Signal EOP on packet transmission */
  274. /* Byte F */
  275. cfblk->rbuf_size = RX_BUF_SIZE >> 12; /* Set receive buffer size */
  276. cfblk->rcvstop = 1; /* Enable Receive Stop Register */
  277. if (znet_debug > 2) {
  278. int i;
  279. unsigned char *c;
  280. for (i = 0, c = (char *) cfblk; i < sizeof (*cfblk); i++)
  281. printk ("%02X ", c[i]);
  282. printk ("\n");
  283. }
  284. *znet->tx_cur++ = sizeof(struct i82593_conf_block);
  285. memcpy(znet->tx_cur, cfblk, sizeof(struct i82593_conf_block));
  286. znet->tx_cur += sizeof(struct i82593_conf_block)/2;
  287. outb(OP0_CONFIGURE | CR0_CHNL, ioaddr);
  288. /* XXX FIXME maz : Add multicast addresses here, so having a
  289. * multicast address configured isn't equal to IFF_ALLMULTI */
  290. }
  291. static const struct net_device_ops znet_netdev_ops = {
  292. .ndo_open = znet_open,
  293. .ndo_stop = znet_close,
  294. .ndo_start_xmit = znet_send_packet,
  295. .ndo_set_multicast_list = znet_set_multicast_list,
  296. .ndo_tx_timeout = znet_tx_timeout,
  297. .ndo_change_mtu = eth_change_mtu,
  298. .ndo_set_mac_address = eth_mac_addr,
  299. .ndo_validate_addr = eth_validate_addr,
  300. };
  301. /* The Z-Note probe is pretty easy. The NETIDBLK exists in the safe-to-probe
  302. BIOS area. We just scan for the signature, and pull the vital parameters
  303. out of the structure. */
  304. static int __init znet_probe (void)
  305. {
  306. int i;
  307. struct netidblk *netinfo;
  308. struct znet_private *znet;
  309. struct net_device *dev;
  310. char *p;
  311. int err = -ENOMEM;
  312. /* This code scans the region 0xf0000 to 0xfffff for a "NETIDBLK". */
  313. for(p = (char *)phys_to_virt(0xf0000); p < (char *)phys_to_virt(0x100000); p++)
  314. if (*p == 'N' && strncmp(p, "NETIDBLK", 8) == 0)
  315. break;
  316. if (p >= (char *)phys_to_virt(0x100000)) {
  317. if (znet_debug > 1)
  318. printk(KERN_INFO "No Z-Note ethernet adaptor found.\n");
  319. return -ENODEV;
  320. }
  321. dev = alloc_etherdev(sizeof(struct znet_private));
  322. if (!dev)
  323. return -ENOMEM;
  324. znet = netdev_priv(dev);
  325. netinfo = (struct netidblk *)p;
  326. dev->base_addr = netinfo->iobase1;
  327. dev->irq = netinfo->irq1;
  328. /* The station address is in the "netidblk" at 0x0f0000. */
  329. for (i = 0; i < 6; i++)
  330. dev->dev_addr[i] = netinfo->netid[i];
  331. printk(KERN_INFO "%s: ZNET at %#3lx, %pM"
  332. ", using IRQ %d DMA %d and %d.\n",
  333. dev->name, dev->base_addr, dev->dev_addr,
  334. dev->irq, netinfo->dma1, netinfo->dma2);
  335. if (znet_debug > 1) {
  336. printk(KERN_INFO "%s: vendor '%16.16s' IRQ1 %d IRQ2 %d DMA1 %d DMA2 %d.\n",
  337. dev->name, netinfo->vendor,
  338. netinfo->irq1, netinfo->irq2,
  339. netinfo->dma1, netinfo->dma2);
  340. printk(KERN_INFO "%s: iobase1 %#x size %d iobase2 %#x size %d net type %2.2x.\n",
  341. dev->name, netinfo->iobase1, netinfo->iosize1,
  342. netinfo->iobase2, netinfo->iosize2, netinfo->nettype);
  343. }
  344. if (znet_debug > 0)
  345. printk(KERN_INFO "%s", version);
  346. znet->rx_dma = netinfo->dma1;
  347. znet->tx_dma = netinfo->dma2;
  348. spin_lock_init(&znet->lock);
  349. znet->sia_base = 0xe6; /* Magic address for the 82501 SIA */
  350. znet->sia_size = 2;
  351. /* maz: Despite the '593 being advertised above as using a
  352. * single 8bits I/O port, this driver does many 16bits
  353. * access. So set io_size accordingly */
  354. znet->io_size = 2;
  355. if (!(znet->rx_start = kmalloc (DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA)))
  356. goto free_dev;
  357. if (!(znet->tx_start = kmalloc (DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA)))
  358. goto free_rx;
  359. if (!dma_page_eq (znet->rx_start, znet->rx_start + (RX_BUF_SIZE/2-1)) ||
  360. !dma_page_eq (znet->tx_start, znet->tx_start + (TX_BUF_SIZE/2-1))) {
  361. printk (KERN_WARNING "tx/rx crossing DMA frontiers, giving up\n");
  362. goto free_tx;
  363. }
  364. znet->rx_end = znet->rx_start + RX_BUF_SIZE/2;
  365. znet->tx_buf_len = TX_BUF_SIZE/2;
  366. znet->tx_end = znet->tx_start + znet->tx_buf_len;
  367. /* The ZNET-specific entries in the device structure. */
  368. dev->netdev_ops = &znet_netdev_ops;
  369. dev->watchdog_timeo = TX_TIMEOUT;
  370. err = register_netdev(dev);
  371. if (err)
  372. goto free_tx;
  373. znet_dev = dev;
  374. return 0;
  375. free_tx:
  376. kfree(znet->tx_start);
  377. free_rx:
  378. kfree(znet->rx_start);
  379. free_dev:
  380. free_netdev(dev);
  381. return err;
  382. }
  383. static int znet_open(struct net_device *dev)
  384. {
  385. int ioaddr = dev->base_addr;
  386. if (znet_debug > 2)
  387. printk(KERN_DEBUG "%s: znet_open() called.\n", dev->name);
  388. /* These should never fail. You can't add devices to a sealed box! */
  389. if (znet_request_resources (dev)) {
  390. printk(KERN_WARNING "%s: Not opened -- resource busy?!?\n", dev->name);
  391. return -EBUSY;
  392. }
  393. znet_transceiver_power (dev, 1);
  394. /* According to the Crynwr driver we should wait 50 msec. for the
  395. LAN clock to stabilize. My experiments indicates that the '593 can
  396. be initialized immediately. The delay is probably needed for the
  397. DC-to-DC converter to come up to full voltage, and for the oscillator
  398. to be spot-on at 20Mhz before transmitting.
  399. Until this proves to be a problem we rely on the higher layers for the
  400. delay and save allocating a timer entry. */
  401. /* maz : Well, I'm getting every time the following message
  402. * without the delay on a 486@33. This machine is much too
  403. * fast... :-) So maybe the Crynwr driver wasn't wrong after
  404. * all, even if the message is completly harmless on my
  405. * setup. */
  406. mdelay (50);
  407. /* This follows the packet driver's lead, and checks for success. */
  408. if (inb(ioaddr) != 0x10 && inb(ioaddr) != 0x00)
  409. printk(KERN_WARNING "%s: Problem turning on the transceiver power.\n",
  410. dev->name);
  411. hardware_init(dev);
  412. netif_start_queue (dev);
  413. return 0;
  414. }
  415. static void znet_tx_timeout (struct net_device *dev)
  416. {
  417. int ioaddr = dev->base_addr;
  418. ushort event, tx_status, rx_offset, state;
  419. outb (CR0_STATUS_0, ioaddr);
  420. event = inb (ioaddr);
  421. outb (CR0_STATUS_1, ioaddr);
  422. tx_status = inw (ioaddr);
  423. outb (CR0_STATUS_2, ioaddr);
  424. rx_offset = inw (ioaddr);
  425. outb (CR0_STATUS_3, ioaddr);
  426. state = inb (ioaddr);
  427. printk (KERN_WARNING "%s: transmit timed out, status %02x %04x %04x %02x,"
  428. " resetting.\n", dev->name, event, tx_status, rx_offset, state);
  429. if (tx_status == TX_LOST_CRS)
  430. printk (KERN_WARNING "%s: Tx carrier error, check transceiver cable.\n",
  431. dev->name);
  432. outb (OP0_RESET, ioaddr);
  433. hardware_init (dev);
  434. netif_wake_queue (dev);
  435. }
  436. static netdev_tx_t znet_send_packet(struct sk_buff *skb, struct net_device *dev)
  437. {
  438. int ioaddr = dev->base_addr;
  439. struct znet_private *znet = netdev_priv(dev);
  440. unsigned long flags;
  441. short length = skb->len;
  442. if (znet_debug > 4)
  443. printk(KERN_DEBUG "%s: ZNet_send_packet.\n", dev->name);
  444. if (length < ETH_ZLEN) {
  445. if (skb_padto(skb, ETH_ZLEN))
  446. return NETDEV_TX_OK;
  447. length = ETH_ZLEN;
  448. }
  449. netif_stop_queue (dev);
  450. /* Check that the part hasn't reset itself, probably from suspend. */
  451. outb(CR0_STATUS_0, ioaddr);
  452. if (inw(ioaddr) == 0x0010 &&
  453. inw(ioaddr) == 0x0000 &&
  454. inw(ioaddr) == 0x0010) {
  455. if (znet_debug > 1)
  456. printk (KERN_WARNING "%s : waking up\n", dev->name);
  457. hardware_init(dev);
  458. znet_transceiver_power (dev, 1);
  459. }
  460. if (1) {
  461. unsigned char *buf = (void *)skb->data;
  462. ushort *tx_link = znet->tx_cur - 1;
  463. ushort rnd_len = (length + 1)>>1;
  464. dev->stats.tx_bytes+=length;
  465. if (znet->tx_cur >= znet->tx_end)
  466. znet->tx_cur = znet->tx_start;
  467. *znet->tx_cur++ = length;
  468. if (znet->tx_cur + rnd_len + 1 > znet->tx_end) {
  469. int semi_cnt = (znet->tx_end - znet->tx_cur)<<1; /* Cvrt to byte cnt. */
  470. memcpy(znet->tx_cur, buf, semi_cnt);
  471. rnd_len -= semi_cnt>>1;
  472. memcpy(znet->tx_start, buf + semi_cnt, length - semi_cnt);
  473. znet->tx_cur = znet->tx_start + rnd_len;
  474. } else {
  475. memcpy(znet->tx_cur, buf, skb->len);
  476. znet->tx_cur += rnd_len;
  477. }
  478. *znet->tx_cur++ = 0;
  479. spin_lock_irqsave(&znet->lock, flags);
  480. {
  481. *tx_link = OP0_TRANSMIT | CR0_CHNL;
  482. /* Is this always safe to do? */
  483. outb(OP0_TRANSMIT | CR0_CHNL, ioaddr);
  484. }
  485. spin_unlock_irqrestore (&znet->lock, flags);
  486. netif_start_queue (dev);
  487. if (znet_debug > 4)
  488. printk(KERN_DEBUG "%s: Transmitter queued, length %d.\n", dev->name, length);
  489. }
  490. dev_kfree_skb(skb);
  491. return NETDEV_TX_OK;
  492. }
  493. /* The ZNET interrupt handler. */
  494. static irqreturn_t znet_interrupt(int irq, void *dev_id)
  495. {
  496. struct net_device *dev = dev_id;
  497. struct znet_private *znet = netdev_priv(dev);
  498. int ioaddr;
  499. int boguscnt = 20;
  500. int handled = 0;
  501. spin_lock (&znet->lock);
  502. ioaddr = dev->base_addr;
  503. outb(CR0_STATUS_0, ioaddr);
  504. do {
  505. ushort status = inb(ioaddr);
  506. if (znet_debug > 5) {
  507. ushort result, rx_ptr, running;
  508. outb(CR0_STATUS_1, ioaddr);
  509. result = inw(ioaddr);
  510. outb(CR0_STATUS_2, ioaddr);
  511. rx_ptr = inw(ioaddr);
  512. outb(CR0_STATUS_3, ioaddr);
  513. running = inb(ioaddr);
  514. printk(KERN_DEBUG "%s: interrupt, status %02x, %04x %04x %02x serial %d.\n",
  515. dev->name, status, result, rx_ptr, running, boguscnt);
  516. }
  517. if ((status & SR0_INTERRUPT) == 0)
  518. break;
  519. handled = 1;
  520. if ((status & SR0_EVENT_MASK) == SR0_TRANSMIT_DONE ||
  521. (status & SR0_EVENT_MASK) == SR0_RETRANSMIT_DONE ||
  522. (status & SR0_EVENT_MASK) == SR0_TRANSMIT_NO_CRC_DONE) {
  523. int tx_status;
  524. outb(CR0_STATUS_1, ioaddr);
  525. tx_status = inw(ioaddr);
  526. /* It's undocumented, but tx_status seems to match the i82586. */
  527. if (tx_status & TX_OK) {
  528. dev->stats.tx_packets++;
  529. dev->stats.collisions += tx_status & TX_NCOL_MASK;
  530. } else {
  531. if (tx_status & (TX_LOST_CTS | TX_LOST_CRS))
  532. dev->stats.tx_carrier_errors++;
  533. if (tx_status & TX_UND_RUN)
  534. dev->stats.tx_fifo_errors++;
  535. if (!(tx_status & TX_HRT_BEAT))
  536. dev->stats.tx_heartbeat_errors++;
  537. if (tx_status & TX_MAX_COL)
  538. dev->stats.tx_aborted_errors++;
  539. /* ...and the catch-all. */
  540. if ((tx_status | (TX_LOST_CRS | TX_LOST_CTS | TX_UND_RUN | TX_HRT_BEAT | TX_MAX_COL)) != (TX_LOST_CRS | TX_LOST_CTS | TX_UND_RUN | TX_HRT_BEAT | TX_MAX_COL))
  541. dev->stats.tx_errors++;
  542. /* Transceiver may be stuck if cable
  543. * was removed while emitting a
  544. * packet. Flip it off, then on to
  545. * reset it. This is very empirical,
  546. * but it seems to work. */
  547. znet_transceiver_power (dev, 0);
  548. znet_transceiver_power (dev, 1);
  549. }
  550. netif_wake_queue (dev);
  551. }
  552. if ((status & SR0_RECEPTION) ||
  553. (status & SR0_EVENT_MASK) == SR0_STOP_REG_HIT) {
  554. znet_rx(dev);
  555. }
  556. /* Clear the interrupts we've handled. */
  557. outb(CR0_INT_ACK, ioaddr);
  558. } while (boguscnt--);
  559. spin_unlock (&znet->lock);
  560. return IRQ_RETVAL(handled);
  561. }
  562. static void znet_rx(struct net_device *dev)
  563. {
  564. struct znet_private *znet = netdev_priv(dev);
  565. int ioaddr = dev->base_addr;
  566. int boguscount = 1;
  567. short next_frame_end_offset = 0; /* Offset of next frame start. */
  568. short *cur_frame_end;
  569. short cur_frame_end_offset;
  570. outb(CR0_STATUS_2, ioaddr);
  571. cur_frame_end_offset = inw(ioaddr);
  572. if (cur_frame_end_offset == znet->rx_cur - znet->rx_start) {
  573. printk(KERN_WARNING "%s: Interrupted, but nothing to receive, offset %03x.\n",
  574. dev->name, cur_frame_end_offset);
  575. return;
  576. }
  577. /* Use same method as the Crynwr driver: construct a forward list in
  578. the same area of the backwards links we now have. This allows us to
  579. pass packets to the upper layers in the order they were received --
  580. important for fast-path sequential operations. */
  581. while (znet->rx_start + cur_frame_end_offset != znet->rx_cur &&
  582. ++boguscount < 5) {
  583. unsigned short hi_cnt, lo_cnt, hi_status, lo_status;
  584. int count, status;
  585. if (cur_frame_end_offset < 4) {
  586. /* Oh no, we have a special case: the frame trailer wraps around
  587. the end of the ring buffer. We've saved space at the end of
  588. the ring buffer for just this problem. */
  589. memcpy(znet->rx_end, znet->rx_start, 8);
  590. cur_frame_end_offset += (RX_BUF_SIZE/2);
  591. }
  592. cur_frame_end = znet->rx_start + cur_frame_end_offset - 4;
  593. lo_status = *cur_frame_end++;
  594. hi_status = *cur_frame_end++;
  595. status = ((hi_status & 0xff) << 8) + (lo_status & 0xff);
  596. lo_cnt = *cur_frame_end++;
  597. hi_cnt = *cur_frame_end++;
  598. count = ((hi_cnt & 0xff) << 8) + (lo_cnt & 0xff);
  599. if (znet_debug > 5)
  600. printk(KERN_DEBUG "Constructing trailer at location %03x, %04x %04x %04x %04x"
  601. " count %#x status %04x.\n",
  602. cur_frame_end_offset<<1, lo_status, hi_status, lo_cnt, hi_cnt,
  603. count, status);
  604. cur_frame_end[-4] = status;
  605. cur_frame_end[-3] = next_frame_end_offset;
  606. cur_frame_end[-2] = count;
  607. next_frame_end_offset = cur_frame_end_offset;
  608. cur_frame_end_offset -= ((count + 1)>>1) + 3;
  609. if (cur_frame_end_offset < 0)
  610. cur_frame_end_offset += RX_BUF_SIZE/2;
  611. };
  612. /* Now step forward through the list. */
  613. do {
  614. ushort *this_rfp_ptr = znet->rx_start + next_frame_end_offset;
  615. int status = this_rfp_ptr[-4];
  616. int pkt_len = this_rfp_ptr[-2];
  617. if (znet_debug > 5)
  618. printk(KERN_DEBUG "Looking at trailer ending at %04x status %04x length %03x"
  619. " next %04x.\n", next_frame_end_offset<<1, status, pkt_len,
  620. this_rfp_ptr[-3]<<1);
  621. /* Once again we must assume that the i82586 docs apply. */
  622. if ( ! (status & RX_RCV_OK)) { /* There was an error. */
  623. dev->stats.rx_errors++;
  624. if (status & RX_CRC_ERR) dev->stats.rx_crc_errors++;
  625. if (status & RX_ALG_ERR) dev->stats.rx_frame_errors++;
  626. #if 0
  627. if (status & 0x0200) dev->stats.rx_over_errors++; /* Wrong. */
  628. if (status & 0x0100) dev->stats.rx_fifo_errors++;
  629. #else
  630. /* maz : Wild guess... */
  631. if (status & RX_OVRRUN) dev->stats.rx_over_errors++;
  632. #endif
  633. if (status & RX_SRT_FRM) dev->stats.rx_length_errors++;
  634. } else if (pkt_len > 1536) {
  635. dev->stats.rx_length_errors++;
  636. } else {
  637. /* Malloc up new buffer. */
  638. struct sk_buff *skb;
  639. skb = dev_alloc_skb(pkt_len);
  640. if (skb == NULL) {
  641. if (znet_debug)
  642. printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
  643. dev->stats.rx_dropped++;
  644. break;
  645. }
  646. if (&znet->rx_cur[(pkt_len+1)>>1] > znet->rx_end) {
  647. int semi_cnt = (znet->rx_end - znet->rx_cur)<<1;
  648. memcpy(skb_put(skb,semi_cnt), znet->rx_cur, semi_cnt);
  649. memcpy(skb_put(skb,pkt_len-semi_cnt), znet->rx_start,
  650. pkt_len - semi_cnt);
  651. } else {
  652. memcpy(skb_put(skb,pkt_len), znet->rx_cur, pkt_len);
  653. if (znet_debug > 6) {
  654. unsigned int *packet = (unsigned int *) skb->data;
  655. printk(KERN_DEBUG "Packet data is %08x %08x %08x %08x.\n", packet[0],
  656. packet[1], packet[2], packet[3]);
  657. }
  658. }
  659. skb->protocol=eth_type_trans(skb,dev);
  660. netif_rx(skb);
  661. dev->stats.rx_packets++;
  662. dev->stats.rx_bytes += pkt_len;
  663. }
  664. znet->rx_cur = this_rfp_ptr;
  665. if (znet->rx_cur >= znet->rx_end)
  666. znet->rx_cur -= RX_BUF_SIZE/2;
  667. update_stop_hit(ioaddr, (znet->rx_cur - znet->rx_start)<<1);
  668. next_frame_end_offset = this_rfp_ptr[-3];
  669. if (next_frame_end_offset == 0) /* Read all the frames? */
  670. break; /* Done for now */
  671. this_rfp_ptr = znet->rx_start + next_frame_end_offset;
  672. } while (--boguscount);
  673. /* If any worth-while packets have been received, dev_rint()
  674. has done a mark_bh(INET_BH) for us and will work on them
  675. when we get to the bottom-half routine. */
  676. }
  677. /* The inverse routine to znet_open(). */
  678. static int znet_close(struct net_device *dev)
  679. {
  680. int ioaddr = dev->base_addr;
  681. netif_stop_queue (dev);
  682. outb(OP0_RESET, ioaddr); /* CMD0_RESET */
  683. if (znet_debug > 1)
  684. printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
  685. /* Turn off transceiver power. */
  686. znet_transceiver_power (dev, 0);
  687. znet_release_resources (dev);
  688. return 0;
  689. }
  690. static void show_dma(struct net_device *dev)
  691. {
  692. short ioaddr = dev->base_addr;
  693. unsigned char stat = inb (ioaddr);
  694. struct znet_private *znet = netdev_priv(dev);
  695. unsigned long flags;
  696. short dma_port = ((znet->tx_dma&3)<<2) + IO_DMA2_BASE;
  697. unsigned addr = inb(dma_port);
  698. short residue;
  699. addr |= inb(dma_port) << 8;
  700. residue = get_dma_residue(znet->tx_dma);
  701. if (znet_debug > 1) {
  702. flags=claim_dma_lock();
  703. printk(KERN_DEBUG "Stat:%02x Addr: %04x cnt:%3x\n",
  704. stat, addr<<1, residue);
  705. release_dma_lock(flags);
  706. }
  707. }
  708. /* Initialize the hardware. We have to do this when the board is open()ed
  709. or when we come out of suspend mode. */
  710. static void hardware_init(struct net_device *dev)
  711. {
  712. unsigned long flags;
  713. short ioaddr = dev->base_addr;
  714. struct znet_private *znet = netdev_priv(dev);
  715. znet->rx_cur = znet->rx_start;
  716. znet->tx_cur = znet->tx_start;
  717. /* Reset the chip, and start it up. */
  718. outb(OP0_RESET, ioaddr);
  719. flags=claim_dma_lock();
  720. disable_dma(znet->rx_dma); /* reset by an interrupting task. */
  721. clear_dma_ff(znet->rx_dma);
  722. set_dma_mode(znet->rx_dma, DMA_RX_MODE);
  723. set_dma_addr(znet->rx_dma, (unsigned int) znet->rx_start);
  724. set_dma_count(znet->rx_dma, RX_BUF_SIZE);
  725. enable_dma(znet->rx_dma);
  726. /* Now set up the Tx channel. */
  727. disable_dma(znet->tx_dma);
  728. clear_dma_ff(znet->tx_dma);
  729. set_dma_mode(znet->tx_dma, DMA_TX_MODE);
  730. set_dma_addr(znet->tx_dma, (unsigned int) znet->tx_start);
  731. set_dma_count(znet->tx_dma, znet->tx_buf_len<<1);
  732. enable_dma(znet->tx_dma);
  733. release_dma_lock(flags);
  734. if (znet_debug > 1)
  735. printk(KERN_DEBUG "%s: Initializing the i82593, rx buf %p tx buf %p\n",
  736. dev->name, znet->rx_start,znet->tx_start);
  737. /* Do an empty configure command, just like the Crynwr driver. This
  738. resets to chip to its default values. */
  739. *znet->tx_cur++ = 0;
  740. *znet->tx_cur++ = 0;
  741. show_dma(dev);
  742. outb(OP0_CONFIGURE | CR0_CHNL, ioaddr);
  743. znet_set_multicast_list (dev);
  744. *znet->tx_cur++ = 6;
  745. memcpy(znet->tx_cur, dev->dev_addr, 6);
  746. znet->tx_cur += 3;
  747. show_dma(dev);
  748. outb(OP0_IA_SETUP | CR0_CHNL, ioaddr);
  749. show_dma(dev);
  750. update_stop_hit(ioaddr, 8192);
  751. if (znet_debug > 1) printk(KERN_DEBUG "enabling Rx.\n");
  752. outb(OP0_RCV_ENABLE, ioaddr);
  753. netif_start_queue (dev);
  754. }
  755. static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset)
  756. {
  757. outb(OP0_SWIT_TO_PORT_1 | CR0_CHNL, ioaddr);
  758. if (znet_debug > 5)
  759. printk(KERN_DEBUG "Updating stop hit with value %02x.\n",
  760. (rx_stop_offset >> 6) | CR1_STOP_REG_UPDATE);
  761. outb((rx_stop_offset >> 6) | CR1_STOP_REG_UPDATE, ioaddr);
  762. outb(OP1_SWIT_TO_PORT_0, ioaddr);
  763. }
  764. static __exit void znet_cleanup (void)
  765. {
  766. if (znet_dev) {
  767. struct znet_private *znet = netdev_priv(znet_dev);
  768. unregister_netdev (znet_dev);
  769. kfree (znet->rx_start);
  770. kfree (znet->tx_start);
  771. free_netdev (znet_dev);
  772. }
  773. }
  774. module_init (znet_probe);
  775. module_exit (znet_cleanup);