meth.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * meth.c -- O2 Builtin 10/100 Ethernet driver
  3. *
  4. * Copyright (C) 2001-2003 Ilya Volynets
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/errno.h>
  19. #include <linux/types.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/in.h>
  22. #include <linux/in6.h>
  23. #include <linux/device.h> /* struct device, et al */
  24. #include <linux/netdevice.h> /* struct device, and other headers */
  25. #include <linux/etherdevice.h> /* eth_type_trans */
  26. #include <linux/ip.h> /* struct iphdr */
  27. #include <linux/tcp.h> /* struct tcphdr */
  28. #include <linux/skbuff.h>
  29. #include <linux/mii.h> /* MII definitions */
  30. #include <asm/ip32/mace.h>
  31. #include <asm/ip32/ip32_ints.h>
  32. #include <asm/io.h>
  33. #include "meth.h"
  34. #ifndef MFE_DEBUG
  35. #define MFE_DEBUG 0
  36. #endif
  37. #if MFE_DEBUG>=1
  38. #define DPRINTK(str,args...) printk(KERN_DEBUG "meth: %s: " str, __func__ , ## args)
  39. #define MFE_RX_DEBUG 2
  40. #else
  41. #define DPRINTK(str,args...)
  42. #define MFE_RX_DEBUG 0
  43. #endif
  44. static const char *meth_str="SGI O2 Fast Ethernet";
  45. /* The maximum time waited (in jiffies) before assuming a Tx failed. (400ms) */
  46. #define TX_TIMEOUT (400*HZ/1000)
  47. static int timeout = TX_TIMEOUT;
  48. module_param(timeout, int, 0);
  49. /*
  50. * This structure is private to each device. It is used to pass
  51. * packets in and out, so there is place for a packet
  52. */
  53. struct meth_private {
  54. /* in-memory copy of MAC Control register */
  55. unsigned long mac_ctrl;
  56. /* in-memory copy of DMA Control register */
  57. unsigned long dma_ctrl;
  58. /* address of PHY, used by mdio_* functions, initialized in mdio_probe */
  59. unsigned long phy_addr;
  60. tx_packet *tx_ring;
  61. dma_addr_t tx_ring_dma;
  62. struct sk_buff *tx_skbs[TX_RING_ENTRIES];
  63. dma_addr_t tx_skb_dmas[TX_RING_ENTRIES];
  64. unsigned long tx_read, tx_write, tx_count;
  65. rx_packet *rx_ring[RX_RING_ENTRIES];
  66. dma_addr_t rx_ring_dmas[RX_RING_ENTRIES];
  67. struct sk_buff *rx_skbs[RX_RING_ENTRIES];
  68. unsigned long rx_write;
  69. spinlock_t meth_lock;
  70. };
  71. static void meth_tx_timeout(struct net_device *dev);
  72. static irqreturn_t meth_interrupt(int irq, void *dev_id);
  73. /* global, initialized in ip32-setup.c */
  74. char o2meth_eaddr[8]={0,0,0,0,0,0,0,0};
  75. static inline void load_eaddr(struct net_device *dev)
  76. {
  77. int i;
  78. u64 macaddr;
  79. DPRINTK("Loading MAC Address: %pM\n", dev->dev_addr);
  80. macaddr = 0;
  81. for (i = 0; i < 6; i++)
  82. macaddr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
  83. mace->eth.mac_addr = macaddr;
  84. }
  85. /*
  86. * Waits for BUSY status of mdio bus to clear
  87. */
  88. #define WAIT_FOR_PHY(___rval) \
  89. while ((___rval = mace->eth.phy_data) & MDIO_BUSY) { \
  90. udelay(25); \
  91. }
  92. /*read phy register, return value read */
  93. static unsigned long mdio_read(struct meth_private *priv, unsigned long phyreg)
  94. {
  95. unsigned long rval;
  96. WAIT_FOR_PHY(rval);
  97. mace->eth.phy_regs = (priv->phy_addr << 5) | (phyreg & 0x1f);
  98. udelay(25);
  99. mace->eth.phy_trans_go = 1;
  100. udelay(25);
  101. WAIT_FOR_PHY(rval);
  102. return rval & MDIO_DATA_MASK;
  103. }
  104. static int mdio_probe(struct meth_private *priv)
  105. {
  106. int i;
  107. unsigned long p2, p3, flags;
  108. /* check if phy is detected already */
  109. if(priv->phy_addr>=0&&priv->phy_addr<32)
  110. return 0;
  111. spin_lock_irqsave(&priv->meth_lock, flags);
  112. for (i=0;i<32;++i){
  113. priv->phy_addr=i;
  114. p2=mdio_read(priv,2);
  115. p3=mdio_read(priv,3);
  116. #if MFE_DEBUG>=2
  117. switch ((p2<<12)|(p3>>4)){
  118. case PHY_QS6612X:
  119. DPRINTK("PHY is QS6612X\n");
  120. break;
  121. case PHY_ICS1889:
  122. DPRINTK("PHY is ICS1889\n");
  123. break;
  124. case PHY_ICS1890:
  125. DPRINTK("PHY is ICS1890\n");
  126. break;
  127. case PHY_DP83840:
  128. DPRINTK("PHY is DP83840\n");
  129. break;
  130. }
  131. #endif
  132. if(p2!=0xffff&&p2!=0x0000){
  133. DPRINTK("PHY code: %x\n",(p2<<12)|(p3>>4));
  134. break;
  135. }
  136. }
  137. spin_unlock_irqrestore(&priv->meth_lock, flags);
  138. if(priv->phy_addr<32) {
  139. return 0;
  140. }
  141. DPRINTK("Oopsie! PHY is not known!\n");
  142. priv->phy_addr=-1;
  143. return -ENODEV;
  144. }
  145. static void meth_check_link(struct net_device *dev)
  146. {
  147. struct meth_private *priv = netdev_priv(dev);
  148. unsigned long mii_advertising = mdio_read(priv, 4);
  149. unsigned long mii_partner = mdio_read(priv, 5);
  150. unsigned long negotiated = mii_advertising & mii_partner;
  151. unsigned long duplex, speed;
  152. if (mii_partner == 0xffff)
  153. return;
  154. speed = (negotiated & 0x0380) ? METH_100MBIT : 0;
  155. duplex = ((negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040) ?
  156. METH_PHY_FDX : 0;
  157. if ((priv->mac_ctrl & METH_PHY_FDX) ^ duplex) {
  158. DPRINTK("Setting %s-duplex\n", duplex ? "full" : "half");
  159. if (duplex)
  160. priv->mac_ctrl |= METH_PHY_FDX;
  161. else
  162. priv->mac_ctrl &= ~METH_PHY_FDX;
  163. mace->eth.mac_ctrl = priv->mac_ctrl;
  164. }
  165. if ((priv->mac_ctrl & METH_100MBIT) ^ speed) {
  166. DPRINTK("Setting %dMbs mode\n", speed ? 100 : 10);
  167. if (duplex)
  168. priv->mac_ctrl |= METH_100MBIT;
  169. else
  170. priv->mac_ctrl &= ~METH_100MBIT;
  171. mace->eth.mac_ctrl = priv->mac_ctrl;
  172. }
  173. }
  174. static int meth_init_tx_ring(struct meth_private *priv)
  175. {
  176. /* Init TX ring */
  177. priv->tx_ring = dma_alloc_coherent(NULL, TX_RING_BUFFER_SIZE,
  178. &priv->tx_ring_dma, GFP_ATOMIC);
  179. if (!priv->tx_ring)
  180. return -ENOMEM;
  181. memset(priv->tx_ring, 0, TX_RING_BUFFER_SIZE);
  182. priv->tx_count = priv->tx_read = priv->tx_write = 0;
  183. mace->eth.tx_ring_base = priv->tx_ring_dma;
  184. /* Now init skb save area */
  185. memset(priv->tx_skbs, 0, sizeof(priv->tx_skbs));
  186. memset(priv->tx_skb_dmas, 0, sizeof(priv->tx_skb_dmas));
  187. return 0;
  188. }
  189. static int meth_init_rx_ring(struct meth_private *priv)
  190. {
  191. int i;
  192. for (i = 0; i < RX_RING_ENTRIES; i++) {
  193. priv->rx_skbs[i] = alloc_skb(METH_RX_BUFF_SIZE, 0);
  194. /* 8byte status vector + 3quad padding + 2byte padding,
  195. * to put data on 64bit aligned boundary */
  196. skb_reserve(priv->rx_skbs[i],METH_RX_HEAD);
  197. priv->rx_ring[i]=(rx_packet*)(priv->rx_skbs[i]->head);
  198. /* I'll need to re-sync it after each RX */
  199. priv->rx_ring_dmas[i] =
  200. dma_map_single(NULL, priv->rx_ring[i],
  201. METH_RX_BUFF_SIZE, DMA_FROM_DEVICE);
  202. mace->eth.rx_fifo = priv->rx_ring_dmas[i];
  203. }
  204. priv->rx_write = 0;
  205. return 0;
  206. }
  207. static void meth_free_tx_ring(struct meth_private *priv)
  208. {
  209. int i;
  210. /* Remove any pending skb */
  211. for (i = 0; i < TX_RING_ENTRIES; i++) {
  212. if (priv->tx_skbs[i])
  213. dev_kfree_skb(priv->tx_skbs[i]);
  214. priv->tx_skbs[i] = NULL;
  215. }
  216. dma_free_coherent(NULL, TX_RING_BUFFER_SIZE, priv->tx_ring,
  217. priv->tx_ring_dma);
  218. }
  219. /* Presumes RX DMA engine is stopped, and RX fifo ring is reset */
  220. static void meth_free_rx_ring(struct meth_private *priv)
  221. {
  222. int i;
  223. for (i = 0; i < RX_RING_ENTRIES; i++) {
  224. dma_unmap_single(NULL, priv->rx_ring_dmas[i],
  225. METH_RX_BUFF_SIZE, DMA_FROM_DEVICE);
  226. priv->rx_ring[i] = 0;
  227. priv->rx_ring_dmas[i] = 0;
  228. kfree_skb(priv->rx_skbs[i]);
  229. }
  230. }
  231. int meth_reset(struct net_device *dev)
  232. {
  233. struct meth_private *priv = netdev_priv(dev);
  234. /* Reset card */
  235. mace->eth.mac_ctrl = SGI_MAC_RESET;
  236. udelay(1);
  237. mace->eth.mac_ctrl = 0;
  238. udelay(25);
  239. /* Load ethernet address */
  240. load_eaddr(dev);
  241. /* Should load some "errata", but later */
  242. /* Check for device */
  243. if (mdio_probe(priv) < 0) {
  244. DPRINTK("Unable to find PHY\n");
  245. return -ENODEV;
  246. }
  247. /* Initial mode: 10 | Half-duplex | Accept normal packets */
  248. priv->mac_ctrl = METH_ACCEPT_MCAST | METH_DEFAULT_IPG;
  249. if (dev->flags & IFF_PROMISC)
  250. priv->mac_ctrl |= METH_PROMISC;
  251. mace->eth.mac_ctrl = priv->mac_ctrl;
  252. /* Autonegotiate speed and duplex mode */
  253. meth_check_link(dev);
  254. /* Now set dma control, but don't enable DMA, yet */
  255. priv->dma_ctrl = (4 << METH_RX_OFFSET_SHIFT) |
  256. (RX_RING_ENTRIES << METH_RX_DEPTH_SHIFT);
  257. mace->eth.dma_ctrl = priv->dma_ctrl;
  258. return 0;
  259. }
  260. /*============End Helper Routines=====================*/
  261. /*
  262. * Open and close
  263. */
  264. static int meth_open(struct net_device *dev)
  265. {
  266. struct meth_private *priv = netdev_priv(dev);
  267. int ret;
  268. priv->phy_addr = -1; /* No PHY is known yet... */
  269. /* Initialize the hardware */
  270. ret = meth_reset(dev);
  271. if (ret < 0)
  272. return ret;
  273. /* Allocate the ring buffers */
  274. ret = meth_init_tx_ring(priv);
  275. if (ret < 0)
  276. return ret;
  277. ret = meth_init_rx_ring(priv);
  278. if (ret < 0)
  279. goto out_free_tx_ring;
  280. ret = request_irq(dev->irq, meth_interrupt, 0, meth_str, dev);
  281. if (ret) {
  282. printk(KERN_ERR "%s: Can't get irq %d\n", dev->name, dev->irq);
  283. goto out_free_rx_ring;
  284. }
  285. /* Start DMA */
  286. priv->dma_ctrl |= METH_DMA_TX_EN | /*METH_DMA_TX_INT_EN |*/
  287. METH_DMA_RX_EN | METH_DMA_RX_INT_EN;
  288. mace->eth.dma_ctrl = priv->dma_ctrl;
  289. DPRINTK("About to start queue\n");
  290. netif_start_queue(dev);
  291. return 0;
  292. out_free_rx_ring:
  293. meth_free_rx_ring(priv);
  294. out_free_tx_ring:
  295. meth_free_tx_ring(priv);
  296. return ret;
  297. }
  298. static int meth_release(struct net_device *dev)
  299. {
  300. struct meth_private *priv = netdev_priv(dev);
  301. DPRINTK("Stopping queue\n");
  302. netif_stop_queue(dev); /* can't transmit any more */
  303. /* shut down DMA */
  304. priv->dma_ctrl &= ~(METH_DMA_TX_EN | METH_DMA_TX_INT_EN |
  305. METH_DMA_RX_EN | METH_DMA_RX_INT_EN);
  306. mace->eth.dma_ctrl = priv->dma_ctrl;
  307. free_irq(dev->irq, dev);
  308. meth_free_tx_ring(priv);
  309. meth_free_rx_ring(priv);
  310. return 0;
  311. }
  312. /*
  313. * Receive a packet: retrieve, encapsulate and pass over to upper levels
  314. */
  315. static void meth_rx(struct net_device* dev, unsigned long int_status)
  316. {
  317. struct sk_buff *skb;
  318. unsigned long status, flags;
  319. struct meth_private *priv = netdev_priv(dev);
  320. unsigned long fifo_rptr = (int_status & METH_INT_RX_RPTR_MASK) >> 8;
  321. spin_lock_irqsave(&priv->meth_lock, flags);
  322. priv->dma_ctrl &= ~METH_DMA_RX_INT_EN;
  323. mace->eth.dma_ctrl = priv->dma_ctrl;
  324. spin_unlock_irqrestore(&priv->meth_lock, flags);
  325. if (int_status & METH_INT_RX_UNDERFLOW) {
  326. fifo_rptr = (fifo_rptr - 1) & 0x0f;
  327. }
  328. while (priv->rx_write != fifo_rptr) {
  329. dma_unmap_single(NULL, priv->rx_ring_dmas[priv->rx_write],
  330. METH_RX_BUFF_SIZE, DMA_FROM_DEVICE);
  331. status = priv->rx_ring[priv->rx_write]->status.raw;
  332. #if MFE_DEBUG
  333. if (!(status & METH_RX_ST_VALID)) {
  334. DPRINTK("Not received? status=%016lx\n",status);
  335. }
  336. #endif
  337. if ((!(status & METH_RX_STATUS_ERRORS)) && (status & METH_RX_ST_VALID)) {
  338. int len = (status & 0xffff) - 4; /* omit CRC */
  339. /* length sanity check */
  340. if (len < 60 || len > 1518) {
  341. printk(KERN_DEBUG "%s: bogus packet size: %ld, status=%#2Lx.\n",
  342. dev->name, priv->rx_write,
  343. priv->rx_ring[priv->rx_write]->status.raw);
  344. dev->stats.rx_errors++;
  345. dev->stats.rx_length_errors++;
  346. skb = priv->rx_skbs[priv->rx_write];
  347. } else {
  348. skb = alloc_skb(METH_RX_BUFF_SIZE, GFP_ATOMIC);
  349. if (!skb) {
  350. /* Ouch! No memory! Drop packet on the floor */
  351. DPRINTK("No mem: dropping packet\n");
  352. dev->stats.rx_dropped++;
  353. skb = priv->rx_skbs[priv->rx_write];
  354. } else {
  355. struct sk_buff *skb_c = priv->rx_skbs[priv->rx_write];
  356. /* 8byte status vector + 3quad padding + 2byte padding,
  357. * to put data on 64bit aligned boundary */
  358. skb_reserve(skb, METH_RX_HEAD);
  359. /* Write metadata, and then pass to the receive level */
  360. skb_put(skb_c, len);
  361. priv->rx_skbs[priv->rx_write] = skb;
  362. skb_c->protocol = eth_type_trans(skb_c, dev);
  363. dev->stats.rx_packets++;
  364. dev->stats.rx_bytes += len;
  365. netif_rx(skb_c);
  366. }
  367. }
  368. } else {
  369. dev->stats.rx_errors++;
  370. skb=priv->rx_skbs[priv->rx_write];
  371. #if MFE_DEBUG>0
  372. printk(KERN_WARNING "meth: RX error: status=0x%016lx\n",status);
  373. if(status&METH_RX_ST_RCV_CODE_VIOLATION)
  374. printk(KERN_WARNING "Receive Code Violation\n");
  375. if(status&METH_RX_ST_CRC_ERR)
  376. printk(KERN_WARNING "CRC error\n");
  377. if(status&METH_RX_ST_INV_PREAMBLE_CTX)
  378. printk(KERN_WARNING "Invalid Preamble Context\n");
  379. if(status&METH_RX_ST_LONG_EVT_SEEN)
  380. printk(KERN_WARNING "Long Event Seen...\n");
  381. if(status&METH_RX_ST_BAD_PACKET)
  382. printk(KERN_WARNING "Bad Packet\n");
  383. if(status&METH_RX_ST_CARRIER_EVT_SEEN)
  384. printk(KERN_WARNING "Carrier Event Seen\n");
  385. #endif
  386. }
  387. priv->rx_ring[priv->rx_write] = (rx_packet*)skb->head;
  388. priv->rx_ring[priv->rx_write]->status.raw = 0;
  389. priv->rx_ring_dmas[priv->rx_write] =
  390. dma_map_single(NULL, priv->rx_ring[priv->rx_write],
  391. METH_RX_BUFF_SIZE, DMA_FROM_DEVICE);
  392. mace->eth.rx_fifo = priv->rx_ring_dmas[priv->rx_write];
  393. ADVANCE_RX_PTR(priv->rx_write);
  394. }
  395. spin_lock_irqsave(&priv->meth_lock, flags);
  396. /* In case there was underflow, and Rx DMA was disabled */
  397. priv->dma_ctrl |= METH_DMA_RX_INT_EN | METH_DMA_RX_EN;
  398. mace->eth.dma_ctrl = priv->dma_ctrl;
  399. mace->eth.int_stat = METH_INT_RX_THRESHOLD;
  400. spin_unlock_irqrestore(&priv->meth_lock, flags);
  401. }
  402. static int meth_tx_full(struct net_device *dev)
  403. {
  404. struct meth_private *priv = netdev_priv(dev);
  405. return priv->tx_count >= TX_RING_ENTRIES - 1;
  406. }
  407. static void meth_tx_cleanup(struct net_device* dev, unsigned long int_status)
  408. {
  409. struct meth_private *priv = netdev_priv(dev);
  410. unsigned long status, flags;
  411. struct sk_buff *skb;
  412. unsigned long rptr = (int_status&TX_INFO_RPTR) >> 16;
  413. spin_lock_irqsave(&priv->meth_lock, flags);
  414. /* Stop DMA notification */
  415. priv->dma_ctrl &= ~(METH_DMA_TX_INT_EN);
  416. mace->eth.dma_ctrl = priv->dma_ctrl;
  417. while (priv->tx_read != rptr) {
  418. skb = priv->tx_skbs[priv->tx_read];
  419. status = priv->tx_ring[priv->tx_read].header.raw;
  420. #if MFE_DEBUG>=1
  421. if (priv->tx_read == priv->tx_write)
  422. DPRINTK("Auchi! tx_read=%d,tx_write=%d,rptr=%d?\n", priv->tx_read, priv->tx_write,rptr);
  423. #endif
  424. if (status & METH_TX_ST_DONE) {
  425. if (status & METH_TX_ST_SUCCESS){
  426. dev->stats.tx_packets++;
  427. dev->stats.tx_bytes += skb->len;
  428. } else {
  429. dev->stats.tx_errors++;
  430. #if MFE_DEBUG>=1
  431. DPRINTK("TX error: status=%016lx <",status);
  432. if(status & METH_TX_ST_SUCCESS)
  433. printk(" SUCCESS");
  434. if(status & METH_TX_ST_TOOLONG)
  435. printk(" TOOLONG");
  436. if(status & METH_TX_ST_UNDERRUN)
  437. printk(" UNDERRUN");
  438. if(status & METH_TX_ST_EXCCOLL)
  439. printk(" EXCCOLL");
  440. if(status & METH_TX_ST_DEFER)
  441. printk(" DEFER");
  442. if(status & METH_TX_ST_LATECOLL)
  443. printk(" LATECOLL");
  444. printk(" >\n");
  445. #endif
  446. }
  447. } else {
  448. DPRINTK("RPTR points us here, but packet not done?\n");
  449. break;
  450. }
  451. dev_kfree_skb_irq(skb);
  452. priv->tx_skbs[priv->tx_read] = NULL;
  453. priv->tx_ring[priv->tx_read].header.raw = 0;
  454. priv->tx_read = (priv->tx_read+1)&(TX_RING_ENTRIES-1);
  455. priv->tx_count--;
  456. }
  457. /* wake up queue if it was stopped */
  458. if (netif_queue_stopped(dev) && !meth_tx_full(dev)) {
  459. netif_wake_queue(dev);
  460. }
  461. mace->eth.int_stat = METH_INT_TX_EMPTY | METH_INT_TX_PKT;
  462. spin_unlock_irqrestore(&priv->meth_lock, flags);
  463. }
  464. static void meth_error(struct net_device* dev, unsigned status)
  465. {
  466. struct meth_private *priv = netdev_priv(dev);
  467. unsigned long flags;
  468. printk(KERN_WARNING "meth: error status: 0x%08x\n",status);
  469. /* check for errors too... */
  470. if (status & (METH_INT_TX_LINK_FAIL))
  471. printk(KERN_WARNING "meth: link failure\n");
  472. /* Should I do full reset in this case? */
  473. if (status & (METH_INT_MEM_ERROR))
  474. printk(KERN_WARNING "meth: memory error\n");
  475. if (status & (METH_INT_TX_ABORT))
  476. printk(KERN_WARNING "meth: aborted\n");
  477. if (status & (METH_INT_RX_OVERFLOW))
  478. printk(KERN_WARNING "meth: Rx overflow\n");
  479. if (status & (METH_INT_RX_UNDERFLOW)) {
  480. printk(KERN_WARNING "meth: Rx underflow\n");
  481. spin_lock_irqsave(&priv->meth_lock, flags);
  482. mace->eth.int_stat = METH_INT_RX_UNDERFLOW;
  483. /* more underflow interrupts will be delivered,
  484. * effectively throwing us into an infinite loop.
  485. * Thus I stop processing Rx in this case. */
  486. priv->dma_ctrl &= ~METH_DMA_RX_EN;
  487. mace->eth.dma_ctrl = priv->dma_ctrl;
  488. DPRINTK("Disabled meth Rx DMA temporarily\n");
  489. spin_unlock_irqrestore(&priv->meth_lock, flags);
  490. }
  491. mace->eth.int_stat = METH_INT_ERROR;
  492. }
  493. /*
  494. * The typical interrupt entry point
  495. */
  496. static irqreturn_t meth_interrupt(int irq, void *dev_id)
  497. {
  498. struct net_device *dev = (struct net_device *)dev_id;
  499. struct meth_private *priv = netdev_priv(dev);
  500. unsigned long status;
  501. status = mace->eth.int_stat;
  502. while (status & 0xff) {
  503. /* First handle errors - if we get Rx underflow,
  504. * Rx DMA will be disabled, and Rx handler will reenable
  505. * it. I don't think it's possible to get Rx underflow,
  506. * without getting Rx interrupt */
  507. if (status & METH_INT_ERROR) {
  508. meth_error(dev, status);
  509. }
  510. if (status & (METH_INT_TX_EMPTY | METH_INT_TX_PKT)) {
  511. /* a transmission is over: free the skb */
  512. meth_tx_cleanup(dev, status);
  513. }
  514. if (status & METH_INT_RX_THRESHOLD) {
  515. if (!(priv->dma_ctrl & METH_DMA_RX_INT_EN))
  516. break;
  517. /* send it to meth_rx for handling */
  518. meth_rx(dev, status);
  519. }
  520. status = mace->eth.int_stat;
  521. }
  522. return IRQ_HANDLED;
  523. }
  524. /*
  525. * Transmits packets that fit into TX descriptor (are <=120B)
  526. */
  527. static void meth_tx_short_prepare(struct meth_private *priv,
  528. struct sk_buff *skb)
  529. {
  530. tx_packet *desc = &priv->tx_ring[priv->tx_write];
  531. int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
  532. desc->header.raw = METH_TX_CMD_INT_EN | (len-1) | ((128-len) << 16);
  533. /* maybe I should set whole thing to 0 first... */
  534. skb_copy_from_linear_data(skb, desc->data.dt + (120 - len), skb->len);
  535. if (skb->len < len)
  536. memset(desc->data.dt + 120 - len + skb->len, 0, len-skb->len);
  537. }
  538. #define TX_CATBUF1 BIT(25)
  539. static void meth_tx_1page_prepare(struct meth_private *priv,
  540. struct sk_buff *skb)
  541. {
  542. tx_packet *desc = &priv->tx_ring[priv->tx_write];
  543. void *buffer_data = (void *)(((unsigned long)skb->data + 7) & ~7);
  544. int unaligned_len = (int)((unsigned long)buffer_data - (unsigned long)skb->data);
  545. int buffer_len = skb->len - unaligned_len;
  546. dma_addr_t catbuf;
  547. desc->header.raw = METH_TX_CMD_INT_EN | TX_CATBUF1 | (skb->len - 1);
  548. /* unaligned part */
  549. if (unaligned_len) {
  550. skb_copy_from_linear_data(skb, desc->data.dt + (120 - unaligned_len),
  551. unaligned_len);
  552. desc->header.raw |= (128 - unaligned_len) << 16;
  553. }
  554. /* first page */
  555. catbuf = dma_map_single(NULL, buffer_data, buffer_len,
  556. DMA_TO_DEVICE);
  557. desc->data.cat_buf[0].form.start_addr = catbuf >> 3;
  558. desc->data.cat_buf[0].form.len = buffer_len - 1;
  559. }
  560. #define TX_CATBUF2 BIT(26)
  561. static void meth_tx_2page_prepare(struct meth_private *priv,
  562. struct sk_buff *skb)
  563. {
  564. tx_packet *desc = &priv->tx_ring[priv->tx_write];
  565. void *buffer1_data = (void *)(((unsigned long)skb->data + 7) & ~7);
  566. void *buffer2_data = (void *)PAGE_ALIGN((unsigned long)skb->data);
  567. int unaligned_len = (int)((unsigned long)buffer1_data - (unsigned long)skb->data);
  568. int buffer1_len = (int)((unsigned long)buffer2_data - (unsigned long)buffer1_data);
  569. int buffer2_len = skb->len - buffer1_len - unaligned_len;
  570. dma_addr_t catbuf1, catbuf2;
  571. desc->header.raw = METH_TX_CMD_INT_EN | TX_CATBUF1 | TX_CATBUF2| (skb->len - 1);
  572. /* unaligned part */
  573. if (unaligned_len){
  574. skb_copy_from_linear_data(skb, desc->data.dt + (120 - unaligned_len),
  575. unaligned_len);
  576. desc->header.raw |= (128 - unaligned_len) << 16;
  577. }
  578. /* first page */
  579. catbuf1 = dma_map_single(NULL, buffer1_data, buffer1_len,
  580. DMA_TO_DEVICE);
  581. desc->data.cat_buf[0].form.start_addr = catbuf1 >> 3;
  582. desc->data.cat_buf[0].form.len = buffer1_len - 1;
  583. /* second page */
  584. catbuf2 = dma_map_single(NULL, buffer2_data, buffer2_len,
  585. DMA_TO_DEVICE);
  586. desc->data.cat_buf[1].form.start_addr = catbuf2 >> 3;
  587. desc->data.cat_buf[1].form.len = buffer2_len - 1;
  588. }
  589. static void meth_add_to_tx_ring(struct meth_private *priv, struct sk_buff *skb)
  590. {
  591. /* Remember the skb, so we can free it at interrupt time */
  592. priv->tx_skbs[priv->tx_write] = skb;
  593. if (skb->len <= 120) {
  594. /* Whole packet fits into descriptor */
  595. meth_tx_short_prepare(priv, skb);
  596. } else if (PAGE_ALIGN((unsigned long)skb->data) !=
  597. PAGE_ALIGN((unsigned long)skb->data + skb->len - 1)) {
  598. /* Packet crosses page boundary */
  599. meth_tx_2page_prepare(priv, skb);
  600. } else {
  601. /* Packet is in one page */
  602. meth_tx_1page_prepare(priv, skb);
  603. }
  604. priv->tx_write = (priv->tx_write + 1) & (TX_RING_ENTRIES - 1);
  605. mace->eth.tx_info = priv->tx_write;
  606. priv->tx_count++;
  607. }
  608. /*
  609. * Transmit a packet (called by the kernel)
  610. */
  611. static int meth_tx(struct sk_buff *skb, struct net_device *dev)
  612. {
  613. struct meth_private *priv = netdev_priv(dev);
  614. unsigned long flags;
  615. spin_lock_irqsave(&priv->meth_lock, flags);
  616. /* Stop DMA notification */
  617. priv->dma_ctrl &= ~(METH_DMA_TX_INT_EN);
  618. mace->eth.dma_ctrl = priv->dma_ctrl;
  619. meth_add_to_tx_ring(priv, skb);
  620. dev->trans_start = jiffies; /* save the timestamp */
  621. /* If TX ring is full, tell the upper layer to stop sending packets */
  622. if (meth_tx_full(dev)) {
  623. printk(KERN_DEBUG "TX full: stopping\n");
  624. netif_stop_queue(dev);
  625. }
  626. /* Restart DMA notification */
  627. priv->dma_ctrl |= METH_DMA_TX_INT_EN;
  628. mace->eth.dma_ctrl = priv->dma_ctrl;
  629. spin_unlock_irqrestore(&priv->meth_lock, flags);
  630. return NETDEV_TX_OK;
  631. }
  632. /*
  633. * Deal with a transmit timeout.
  634. */
  635. static void meth_tx_timeout(struct net_device *dev)
  636. {
  637. struct meth_private *priv = netdev_priv(dev);
  638. unsigned long flags;
  639. printk(KERN_WARNING "%s: transmit timed out\n", dev->name);
  640. /* Protect against concurrent rx interrupts */
  641. spin_lock_irqsave(&priv->meth_lock,flags);
  642. /* Try to reset the interface. */
  643. meth_reset(dev);
  644. dev->stats.tx_errors++;
  645. /* Clear all rings */
  646. meth_free_tx_ring(priv);
  647. meth_free_rx_ring(priv);
  648. meth_init_tx_ring(priv);
  649. meth_init_rx_ring(priv);
  650. /* Restart dma */
  651. priv->dma_ctrl |= METH_DMA_TX_EN | METH_DMA_RX_EN | METH_DMA_RX_INT_EN;
  652. mace->eth.dma_ctrl = priv->dma_ctrl;
  653. /* Enable interrupt */
  654. spin_unlock_irqrestore(&priv->meth_lock, flags);
  655. dev->trans_start = jiffies; /* prevent tx timeout */
  656. netif_wake_queue(dev);
  657. }
  658. /*
  659. * Ioctl commands
  660. */
  661. static int meth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  662. {
  663. /* XXX Not yet implemented */
  664. switch(cmd) {
  665. case SIOCGMIIPHY:
  666. case SIOCGMIIREG:
  667. case SIOCSMIIREG:
  668. default:
  669. return -EOPNOTSUPP;
  670. }
  671. }
  672. static const struct net_device_ops meth_netdev_ops = {
  673. .ndo_open = meth_open,
  674. .ndo_stop = meth_release,
  675. .ndo_start_xmit = meth_tx,
  676. .ndo_do_ioctl = meth_ioctl,
  677. .ndo_tx_timeout = meth_tx_timeout,
  678. .ndo_change_mtu = eth_change_mtu,
  679. .ndo_validate_addr = eth_validate_addr,
  680. .ndo_set_mac_address = eth_mac_addr,
  681. };
  682. /*
  683. * The init function.
  684. */
  685. static int __devinit meth_probe(struct platform_device *pdev)
  686. {
  687. struct net_device *dev;
  688. struct meth_private *priv;
  689. int err;
  690. dev = alloc_etherdev(sizeof(struct meth_private));
  691. if (!dev)
  692. return -ENOMEM;
  693. dev->netdev_ops = &meth_netdev_ops;
  694. dev->watchdog_timeo = timeout;
  695. dev->irq = MACE_ETHERNET_IRQ;
  696. dev->base_addr = (unsigned long)&mace->eth;
  697. memcpy(dev->dev_addr, o2meth_eaddr, 6);
  698. priv = netdev_priv(dev);
  699. spin_lock_init(&priv->meth_lock);
  700. SET_NETDEV_DEV(dev, &pdev->dev);
  701. err = register_netdev(dev);
  702. if (err) {
  703. free_netdev(dev);
  704. return err;
  705. }
  706. printk(KERN_INFO "%s: SGI MACE Ethernet rev. %d\n",
  707. dev->name, (unsigned int)(mace->eth.mac_ctrl >> 29));
  708. return 0;
  709. }
  710. static int __exit meth_remove(struct platform_device *pdev)
  711. {
  712. struct net_device *dev = platform_get_drvdata(pdev);
  713. unregister_netdev(dev);
  714. free_netdev(dev);
  715. platform_set_drvdata(pdev, NULL);
  716. return 0;
  717. }
  718. static struct platform_driver meth_driver = {
  719. .probe = meth_probe,
  720. .remove = __exit_p(meth_remove),
  721. .driver = {
  722. .name = "meth",
  723. .owner = THIS_MODULE,
  724. }
  725. };
  726. static int __init meth_init_module(void)
  727. {
  728. int err;
  729. err = platform_driver_register(&meth_driver);
  730. if (err)
  731. printk(KERN_ERR "Driver registration failed\n");
  732. return err;
  733. }
  734. static void __exit meth_exit_module(void)
  735. {
  736. platform_driver_unregister(&meth_driver);
  737. }
  738. module_init(meth_init_module);
  739. module_exit(meth_exit_module);
  740. MODULE_AUTHOR("Ilya Volynets <ilya@theIlya.com>");
  741. MODULE_DESCRIPTION("SGI O2 Builtin Fast Ethernet driver");
  742. MODULE_LICENSE("GPL");
  743. MODULE_ALIAS("platform:meth");