meth.c 24 KB

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