mace.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * Network device driver for the MACE ethernet controller on
  3. * Apple Powermacs. Assumes it's under a DBDMA controller.
  4. *
  5. * Copyright (C) 1996 Paul Mackerras.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/delay.h>
  12. #include <linux/string.h>
  13. #include <linux/timer.h>
  14. #include <linux/init.h>
  15. #include <linux/crc32.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/bitrev.h>
  18. #include <asm/prom.h>
  19. #include <asm/dbdma.h>
  20. #include <asm/io.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/macio.h>
  23. #include "mace.h"
  24. static int port_aaui = -1;
  25. #define N_RX_RING 8
  26. #define N_TX_RING 6
  27. #define MAX_TX_ACTIVE 1
  28. #define NCMDS_TX 1 /* dma commands per element in tx ring */
  29. #define RX_BUFLEN (ETH_FRAME_LEN + 8)
  30. #define TX_TIMEOUT HZ /* 1 second */
  31. /* Chip rev needs workaround on HW & multicast addr change */
  32. #define BROKEN_ADDRCHG_REV 0x0941
  33. /* Bits in transmit DMA status */
  34. #define TX_DMA_ERR 0x80
  35. struct mace_data {
  36. volatile struct mace __iomem *mace;
  37. volatile struct dbdma_regs __iomem *tx_dma;
  38. int tx_dma_intr;
  39. volatile struct dbdma_regs __iomem *rx_dma;
  40. int rx_dma_intr;
  41. volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
  42. volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
  43. struct sk_buff *rx_bufs[N_RX_RING];
  44. int rx_fill;
  45. int rx_empty;
  46. struct sk_buff *tx_bufs[N_TX_RING];
  47. int tx_fill;
  48. int tx_empty;
  49. unsigned char maccc;
  50. unsigned char tx_fullup;
  51. unsigned char tx_active;
  52. unsigned char tx_bad_runt;
  53. struct timer_list tx_timeout;
  54. int timeout_active;
  55. int port_aaui;
  56. int chipid;
  57. struct macio_dev *mdev;
  58. spinlock_t lock;
  59. };
  60. /*
  61. * Number of bytes of private data per MACE: allow enough for
  62. * the rx and tx dma commands plus a branch dma command each,
  63. * and another 16 bytes to allow us to align the dma command
  64. * buffers on a 16 byte boundary.
  65. */
  66. #define PRIV_BYTES (sizeof(struct mace_data) \
  67. + (N_RX_RING + NCMDS_TX * N_TX_RING + 3) * sizeof(struct dbdma_cmd))
  68. static int mace_open(struct net_device *dev);
  69. static int mace_close(struct net_device *dev);
  70. static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev);
  71. static void mace_set_multicast(struct net_device *dev);
  72. static void mace_reset(struct net_device *dev);
  73. static int mace_set_address(struct net_device *dev, void *addr);
  74. static irqreturn_t mace_interrupt(int irq, void *dev_id);
  75. static irqreturn_t mace_txdma_intr(int irq, void *dev_id);
  76. static irqreturn_t mace_rxdma_intr(int irq, void *dev_id);
  77. static void mace_set_timeout(struct net_device *dev);
  78. static void mace_tx_timeout(unsigned long data);
  79. static inline void dbdma_reset(volatile struct dbdma_regs __iomem *dma);
  80. static inline void mace_clean_rings(struct mace_data *mp);
  81. static void __mace_set_address(struct net_device *dev, void *addr);
  82. /*
  83. * If we can't get a skbuff when we need it, we use this area for DMA.
  84. */
  85. static unsigned char *dummy_buf;
  86. static int __devinit mace_probe(struct macio_dev *mdev, const struct of_device_id *match)
  87. {
  88. struct device_node *mace = macio_get_of_node(mdev);
  89. struct net_device *dev;
  90. struct mace_data *mp;
  91. const unsigned char *addr;
  92. int j, rev, rc = -EBUSY;
  93. if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {
  94. printk(KERN_ERR "can't use MACE %s: need 3 addrs and 3 irqs\n",
  95. mace->full_name);
  96. return -ENODEV;
  97. }
  98. addr = of_get_property(mace, "mac-address", NULL);
  99. if (addr == NULL) {
  100. addr = of_get_property(mace, "local-mac-address", NULL);
  101. if (addr == NULL) {
  102. printk(KERN_ERR "Can't get mac-address for MACE %s\n",
  103. mace->full_name);
  104. return -ENODEV;
  105. }
  106. }
  107. /*
  108. * lazy allocate the driver-wide dummy buffer. (Note that we
  109. * never have more than one MACE in the system anyway)
  110. */
  111. if (dummy_buf == NULL) {
  112. dummy_buf = kmalloc(RX_BUFLEN+2, GFP_KERNEL);
  113. if (dummy_buf == NULL) {
  114. printk(KERN_ERR "MACE: couldn't allocate dummy buffer\n");
  115. return -ENOMEM;
  116. }
  117. }
  118. if (macio_request_resources(mdev, "mace")) {
  119. printk(KERN_ERR "MACE: can't request IO resources !\n");
  120. return -EBUSY;
  121. }
  122. dev = alloc_etherdev(PRIV_BYTES);
  123. if (!dev) {
  124. printk(KERN_ERR "MACE: can't allocate ethernet device !\n");
  125. rc = -ENOMEM;
  126. goto err_release;
  127. }
  128. SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
  129. mp = netdev_priv(dev);
  130. mp->mdev = mdev;
  131. macio_set_drvdata(mdev, dev);
  132. dev->base_addr = macio_resource_start(mdev, 0);
  133. mp->mace = ioremap(dev->base_addr, 0x1000);
  134. if (mp->mace == NULL) {
  135. printk(KERN_ERR "MACE: can't map IO resources !\n");
  136. rc = -ENOMEM;
  137. goto err_free;
  138. }
  139. dev->irq = macio_irq(mdev, 0);
  140. rev = addr[0] == 0 && addr[1] == 0xA0;
  141. for (j = 0; j < 6; ++j) {
  142. dev->dev_addr[j] = rev ? bitrev8(addr[j]): addr[j];
  143. }
  144. mp->chipid = (in_8(&mp->mace->chipid_hi) << 8) |
  145. in_8(&mp->mace->chipid_lo);
  146. mp = netdev_priv(dev);
  147. mp->maccc = ENXMT | ENRCV;
  148. mp->tx_dma = ioremap(macio_resource_start(mdev, 1), 0x1000);
  149. if (mp->tx_dma == NULL) {
  150. printk(KERN_ERR "MACE: can't map TX DMA resources !\n");
  151. rc = -ENOMEM;
  152. goto err_unmap_io;
  153. }
  154. mp->tx_dma_intr = macio_irq(mdev, 1);
  155. mp->rx_dma = ioremap(macio_resource_start(mdev, 2), 0x1000);
  156. if (mp->rx_dma == NULL) {
  157. printk(KERN_ERR "MACE: can't map RX DMA resources !\n");
  158. rc = -ENOMEM;
  159. goto err_unmap_tx_dma;
  160. }
  161. mp->rx_dma_intr = macio_irq(mdev, 2);
  162. mp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(mp + 1);
  163. mp->rx_cmds = mp->tx_cmds + NCMDS_TX * N_TX_RING + 1;
  164. memset((char *) mp->tx_cmds, 0,
  165. (NCMDS_TX*N_TX_RING + N_RX_RING + 2) * sizeof(struct dbdma_cmd));
  166. init_timer(&mp->tx_timeout);
  167. spin_lock_init(&mp->lock);
  168. mp->timeout_active = 0;
  169. if (port_aaui >= 0)
  170. mp->port_aaui = port_aaui;
  171. else {
  172. /* Apple Network Server uses the AAUI port */
  173. if (machine_is_compatible("AAPL,ShinerESB"))
  174. mp->port_aaui = 1;
  175. else {
  176. #ifdef CONFIG_MACE_AAUI_PORT
  177. mp->port_aaui = 1;
  178. #else
  179. mp->port_aaui = 0;
  180. #endif
  181. }
  182. }
  183. dev->open = mace_open;
  184. dev->stop = mace_close;
  185. dev->hard_start_xmit = mace_xmit_start;
  186. dev->set_multicast_list = mace_set_multicast;
  187. dev->set_mac_address = mace_set_address;
  188. /*
  189. * Most of what is below could be moved to mace_open()
  190. */
  191. mace_reset(dev);
  192. rc = request_irq(dev->irq, mace_interrupt, 0, "MACE", dev);
  193. if (rc) {
  194. printk(KERN_ERR "MACE: can't get irq %d\n", dev->irq);
  195. goto err_unmap_rx_dma;
  196. }
  197. rc = request_irq(mp->tx_dma_intr, mace_txdma_intr, 0, "MACE-txdma", dev);
  198. if (rc) {
  199. printk(KERN_ERR "MACE: can't get irq %d\n", mp->tx_dma_intr);
  200. goto err_free_irq;
  201. }
  202. rc = request_irq(mp->rx_dma_intr, mace_rxdma_intr, 0, "MACE-rxdma", dev);
  203. if (rc) {
  204. printk(KERN_ERR "MACE: can't get irq %d\n", mp->rx_dma_intr);
  205. goto err_free_tx_irq;
  206. }
  207. rc = register_netdev(dev);
  208. if (rc) {
  209. printk(KERN_ERR "MACE: Cannot register net device, aborting.\n");
  210. goto err_free_rx_irq;
  211. }
  212. printk(KERN_INFO "%s: MACE at %pM, chip revision %d.%d\n",
  213. dev->name, dev->dev_addr,
  214. mp->chipid >> 8, mp->chipid & 0xff);
  215. return 0;
  216. err_free_rx_irq:
  217. free_irq(macio_irq(mdev, 2), dev);
  218. err_free_tx_irq:
  219. free_irq(macio_irq(mdev, 1), dev);
  220. err_free_irq:
  221. free_irq(macio_irq(mdev, 0), dev);
  222. err_unmap_rx_dma:
  223. iounmap(mp->rx_dma);
  224. err_unmap_tx_dma:
  225. iounmap(mp->tx_dma);
  226. err_unmap_io:
  227. iounmap(mp->mace);
  228. err_free:
  229. free_netdev(dev);
  230. err_release:
  231. macio_release_resources(mdev);
  232. return rc;
  233. }
  234. static int __devexit mace_remove(struct macio_dev *mdev)
  235. {
  236. struct net_device *dev = macio_get_drvdata(mdev);
  237. struct mace_data *mp;
  238. BUG_ON(dev == NULL);
  239. macio_set_drvdata(mdev, NULL);
  240. mp = netdev_priv(dev);
  241. unregister_netdev(dev);
  242. free_irq(dev->irq, dev);
  243. free_irq(mp->tx_dma_intr, dev);
  244. free_irq(mp->rx_dma_intr, dev);
  245. iounmap(mp->rx_dma);
  246. iounmap(mp->tx_dma);
  247. iounmap(mp->mace);
  248. free_netdev(dev);
  249. macio_release_resources(mdev);
  250. return 0;
  251. }
  252. static void dbdma_reset(volatile struct dbdma_regs __iomem *dma)
  253. {
  254. int i;
  255. out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
  256. /*
  257. * Yes this looks peculiar, but apparently it needs to be this
  258. * way on some machines.
  259. */
  260. for (i = 200; i > 0; --i)
  261. if (ld_le32(&dma->control) & RUN)
  262. udelay(1);
  263. }
  264. static void mace_reset(struct net_device *dev)
  265. {
  266. struct mace_data *mp = netdev_priv(dev);
  267. volatile struct mace __iomem *mb = mp->mace;
  268. int i;
  269. /* soft-reset the chip */
  270. i = 200;
  271. while (--i) {
  272. out_8(&mb->biucc, SWRST);
  273. if (in_8(&mb->biucc) & SWRST) {
  274. udelay(10);
  275. continue;
  276. }
  277. break;
  278. }
  279. if (!i) {
  280. printk(KERN_ERR "mace: cannot reset chip!\n");
  281. return;
  282. }
  283. out_8(&mb->imr, 0xff); /* disable all intrs for now */
  284. i = in_8(&mb->ir);
  285. out_8(&mb->maccc, 0); /* turn off tx, rx */
  286. out_8(&mb->biucc, XMTSP_64);
  287. out_8(&mb->utr, RTRD);
  288. out_8(&mb->fifocc, RCVFW_32 | XMTFW_16 | XMTFWU | RCVFWU | XMTBRST);
  289. out_8(&mb->xmtfc, AUTO_PAD_XMIT); /* auto-pad short frames */
  290. out_8(&mb->rcvfc, 0);
  291. /* load up the hardware address */
  292. __mace_set_address(dev, dev->dev_addr);
  293. /* clear the multicast filter */
  294. if (mp->chipid == BROKEN_ADDRCHG_REV)
  295. out_8(&mb->iac, LOGADDR);
  296. else {
  297. out_8(&mb->iac, ADDRCHG | LOGADDR);
  298. while ((in_8(&mb->iac) & ADDRCHG) != 0)
  299. ;
  300. }
  301. for (i = 0; i < 8; ++i)
  302. out_8(&mb->ladrf, 0);
  303. /* done changing address */
  304. if (mp->chipid != BROKEN_ADDRCHG_REV)
  305. out_8(&mb->iac, 0);
  306. if (mp->port_aaui)
  307. out_8(&mb->plscc, PORTSEL_AUI + ENPLSIO);
  308. else
  309. out_8(&mb->plscc, PORTSEL_GPSI + ENPLSIO);
  310. }
  311. static void __mace_set_address(struct net_device *dev, void *addr)
  312. {
  313. struct mace_data *mp = netdev_priv(dev);
  314. volatile struct mace __iomem *mb = mp->mace;
  315. unsigned char *p = addr;
  316. int i;
  317. /* load up the hardware address */
  318. if (mp->chipid == BROKEN_ADDRCHG_REV)
  319. out_8(&mb->iac, PHYADDR);
  320. else {
  321. out_8(&mb->iac, ADDRCHG | PHYADDR);
  322. while ((in_8(&mb->iac) & ADDRCHG) != 0)
  323. ;
  324. }
  325. for (i = 0; i < 6; ++i)
  326. out_8(&mb->padr, dev->dev_addr[i] = p[i]);
  327. if (mp->chipid != BROKEN_ADDRCHG_REV)
  328. out_8(&mb->iac, 0);
  329. }
  330. static int mace_set_address(struct net_device *dev, void *addr)
  331. {
  332. struct mace_data *mp = netdev_priv(dev);
  333. volatile struct mace __iomem *mb = mp->mace;
  334. unsigned long flags;
  335. spin_lock_irqsave(&mp->lock, flags);
  336. __mace_set_address(dev, addr);
  337. /* note: setting ADDRCHG clears ENRCV */
  338. out_8(&mb->maccc, mp->maccc);
  339. spin_unlock_irqrestore(&mp->lock, flags);
  340. return 0;
  341. }
  342. static inline void mace_clean_rings(struct mace_data *mp)
  343. {
  344. int i;
  345. /* free some skb's */
  346. for (i = 0; i < N_RX_RING; ++i) {
  347. if (mp->rx_bufs[i] != NULL) {
  348. dev_kfree_skb(mp->rx_bufs[i]);
  349. mp->rx_bufs[i] = NULL;
  350. }
  351. }
  352. for (i = mp->tx_empty; i != mp->tx_fill; ) {
  353. dev_kfree_skb(mp->tx_bufs[i]);
  354. if (++i >= N_TX_RING)
  355. i = 0;
  356. }
  357. }
  358. static int mace_open(struct net_device *dev)
  359. {
  360. struct mace_data *mp = netdev_priv(dev);
  361. volatile struct mace __iomem *mb = mp->mace;
  362. volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
  363. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  364. volatile struct dbdma_cmd *cp;
  365. int i;
  366. struct sk_buff *skb;
  367. unsigned char *data;
  368. /* reset the chip */
  369. mace_reset(dev);
  370. /* initialize list of sk_buffs for receiving and set up recv dma */
  371. mace_clean_rings(mp);
  372. memset((char *)mp->rx_cmds, 0, N_RX_RING * sizeof(struct dbdma_cmd));
  373. cp = mp->rx_cmds;
  374. for (i = 0; i < N_RX_RING - 1; ++i) {
  375. skb = dev_alloc_skb(RX_BUFLEN + 2);
  376. if (!skb) {
  377. data = dummy_buf;
  378. } else {
  379. skb_reserve(skb, 2); /* so IP header lands on 4-byte bdry */
  380. data = skb->data;
  381. }
  382. mp->rx_bufs[i] = skb;
  383. st_le16(&cp->req_count, RX_BUFLEN);
  384. st_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
  385. st_le32(&cp->phy_addr, virt_to_bus(data));
  386. cp->xfer_status = 0;
  387. ++cp;
  388. }
  389. mp->rx_bufs[i] = NULL;
  390. st_le16(&cp->command, DBDMA_STOP);
  391. mp->rx_fill = i;
  392. mp->rx_empty = 0;
  393. /* Put a branch back to the beginning of the receive command list */
  394. ++cp;
  395. st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
  396. st_le32(&cp->cmd_dep, virt_to_bus(mp->rx_cmds));
  397. /* start rx dma */
  398. out_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
  399. out_le32(&rd->cmdptr, virt_to_bus(mp->rx_cmds));
  400. out_le32(&rd->control, (RUN << 16) | RUN);
  401. /* put a branch at the end of the tx command list */
  402. cp = mp->tx_cmds + NCMDS_TX * N_TX_RING;
  403. st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
  404. st_le32(&cp->cmd_dep, virt_to_bus(mp->tx_cmds));
  405. /* reset tx dma */
  406. out_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
  407. out_le32(&td->cmdptr, virt_to_bus(mp->tx_cmds));
  408. mp->tx_fill = 0;
  409. mp->tx_empty = 0;
  410. mp->tx_fullup = 0;
  411. mp->tx_active = 0;
  412. mp->tx_bad_runt = 0;
  413. /* turn it on! */
  414. out_8(&mb->maccc, mp->maccc);
  415. /* enable all interrupts except receive interrupts */
  416. out_8(&mb->imr, RCVINT);
  417. return 0;
  418. }
  419. static int mace_close(struct net_device *dev)
  420. {
  421. struct mace_data *mp = netdev_priv(dev);
  422. volatile struct mace __iomem *mb = mp->mace;
  423. volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
  424. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  425. /* disable rx and tx */
  426. out_8(&mb->maccc, 0);
  427. out_8(&mb->imr, 0xff); /* disable all intrs */
  428. /* disable rx and tx dma */
  429. st_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
  430. st_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
  431. mace_clean_rings(mp);
  432. return 0;
  433. }
  434. static inline void mace_set_timeout(struct net_device *dev)
  435. {
  436. struct mace_data *mp = netdev_priv(dev);
  437. if (mp->timeout_active)
  438. del_timer(&mp->tx_timeout);
  439. mp->tx_timeout.expires = jiffies + TX_TIMEOUT;
  440. mp->tx_timeout.function = mace_tx_timeout;
  441. mp->tx_timeout.data = (unsigned long) dev;
  442. add_timer(&mp->tx_timeout);
  443. mp->timeout_active = 1;
  444. }
  445. static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev)
  446. {
  447. struct mace_data *mp = netdev_priv(dev);
  448. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  449. volatile struct dbdma_cmd *cp, *np;
  450. unsigned long flags;
  451. int fill, next, len;
  452. /* see if there's a free slot in the tx ring */
  453. spin_lock_irqsave(&mp->lock, flags);
  454. fill = mp->tx_fill;
  455. next = fill + 1;
  456. if (next >= N_TX_RING)
  457. next = 0;
  458. if (next == mp->tx_empty) {
  459. netif_stop_queue(dev);
  460. mp->tx_fullup = 1;
  461. spin_unlock_irqrestore(&mp->lock, flags);
  462. return 1; /* can't take it at the moment */
  463. }
  464. spin_unlock_irqrestore(&mp->lock, flags);
  465. /* partially fill in the dma command block */
  466. len = skb->len;
  467. if (len > ETH_FRAME_LEN) {
  468. printk(KERN_DEBUG "mace: xmit frame too long (%d)\n", len);
  469. len = ETH_FRAME_LEN;
  470. }
  471. mp->tx_bufs[fill] = skb;
  472. cp = mp->tx_cmds + NCMDS_TX * fill;
  473. st_le16(&cp->req_count, len);
  474. st_le32(&cp->phy_addr, virt_to_bus(skb->data));
  475. np = mp->tx_cmds + NCMDS_TX * next;
  476. out_le16(&np->command, DBDMA_STOP);
  477. /* poke the tx dma channel */
  478. spin_lock_irqsave(&mp->lock, flags);
  479. mp->tx_fill = next;
  480. if (!mp->tx_bad_runt && mp->tx_active < MAX_TX_ACTIVE) {
  481. out_le16(&cp->xfer_status, 0);
  482. out_le16(&cp->command, OUTPUT_LAST);
  483. out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
  484. ++mp->tx_active;
  485. mace_set_timeout(dev);
  486. }
  487. if (++next >= N_TX_RING)
  488. next = 0;
  489. if (next == mp->tx_empty)
  490. netif_stop_queue(dev);
  491. spin_unlock_irqrestore(&mp->lock, flags);
  492. return 0;
  493. }
  494. static void mace_set_multicast(struct net_device *dev)
  495. {
  496. struct mace_data *mp = netdev_priv(dev);
  497. volatile struct mace __iomem *mb = mp->mace;
  498. int i, j;
  499. u32 crc;
  500. unsigned long flags;
  501. spin_lock_irqsave(&mp->lock, flags);
  502. mp->maccc &= ~PROM;
  503. if (dev->flags & IFF_PROMISC) {
  504. mp->maccc |= PROM;
  505. } else {
  506. unsigned char multicast_filter[8];
  507. struct dev_mc_list *dmi = dev->mc_list;
  508. if (dev->flags & IFF_ALLMULTI) {
  509. for (i = 0; i < 8; i++)
  510. multicast_filter[i] = 0xff;
  511. } else {
  512. for (i = 0; i < 8; i++)
  513. multicast_filter[i] = 0;
  514. for (i = 0; i < dev->mc_count; i++) {
  515. crc = ether_crc_le(6, dmi->dmi_addr);
  516. j = crc >> 26; /* bit number in multicast_filter */
  517. multicast_filter[j >> 3] |= 1 << (j & 7);
  518. dmi = dmi->next;
  519. }
  520. }
  521. #if 0
  522. printk("Multicast filter :");
  523. for (i = 0; i < 8; i++)
  524. printk("%02x ", multicast_filter[i]);
  525. printk("\n");
  526. #endif
  527. if (mp->chipid == BROKEN_ADDRCHG_REV)
  528. out_8(&mb->iac, LOGADDR);
  529. else {
  530. out_8(&mb->iac, ADDRCHG | LOGADDR);
  531. while ((in_8(&mb->iac) & ADDRCHG) != 0)
  532. ;
  533. }
  534. for (i = 0; i < 8; ++i)
  535. out_8(&mb->ladrf, multicast_filter[i]);
  536. if (mp->chipid != BROKEN_ADDRCHG_REV)
  537. out_8(&mb->iac, 0);
  538. }
  539. /* reset maccc */
  540. out_8(&mb->maccc, mp->maccc);
  541. spin_unlock_irqrestore(&mp->lock, flags);
  542. }
  543. static void mace_handle_misc_intrs(struct mace_data *mp, int intr, struct net_device *dev)
  544. {
  545. volatile struct mace __iomem *mb = mp->mace;
  546. static int mace_babbles, mace_jabbers;
  547. if (intr & MPCO)
  548. dev->stats.rx_missed_errors += 256;
  549. dev->stats.rx_missed_errors += in_8(&mb->mpc); /* reading clears it */
  550. if (intr & RNTPCO)
  551. dev->stats.rx_length_errors += 256;
  552. dev->stats.rx_length_errors += in_8(&mb->rntpc); /* reading clears it */
  553. if (intr & CERR)
  554. ++dev->stats.tx_heartbeat_errors;
  555. if (intr & BABBLE)
  556. if (mace_babbles++ < 4)
  557. printk(KERN_DEBUG "mace: babbling transmitter\n");
  558. if (intr & JABBER)
  559. if (mace_jabbers++ < 4)
  560. printk(KERN_DEBUG "mace: jabbering transceiver\n");
  561. }
  562. static irqreturn_t mace_interrupt(int irq, void *dev_id)
  563. {
  564. struct net_device *dev = (struct net_device *) dev_id;
  565. struct mace_data *mp = netdev_priv(dev);
  566. volatile struct mace __iomem *mb = mp->mace;
  567. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  568. volatile struct dbdma_cmd *cp;
  569. int intr, fs, i, stat, x;
  570. int xcount, dstat;
  571. unsigned long flags;
  572. /* static int mace_last_fs, mace_last_xcount; */
  573. spin_lock_irqsave(&mp->lock, flags);
  574. intr = in_8(&mb->ir); /* read interrupt register */
  575. in_8(&mb->xmtrc); /* get retries */
  576. mace_handle_misc_intrs(mp, intr, dev);
  577. i = mp->tx_empty;
  578. while (in_8(&mb->pr) & XMTSV) {
  579. del_timer(&mp->tx_timeout);
  580. mp->timeout_active = 0;
  581. /*
  582. * Clear any interrupt indication associated with this status
  583. * word. This appears to unlatch any error indication from
  584. * the DMA controller.
  585. */
  586. intr = in_8(&mb->ir);
  587. if (intr != 0)
  588. mace_handle_misc_intrs(mp, intr, dev);
  589. if (mp->tx_bad_runt) {
  590. fs = in_8(&mb->xmtfs);
  591. mp->tx_bad_runt = 0;
  592. out_8(&mb->xmtfc, AUTO_PAD_XMIT);
  593. continue;
  594. }
  595. dstat = ld_le32(&td->status);
  596. /* stop DMA controller */
  597. out_le32(&td->control, RUN << 16);
  598. /*
  599. * xcount is the number of complete frames which have been
  600. * written to the fifo but for which status has not been read.
  601. */
  602. xcount = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
  603. if (xcount == 0 || (dstat & DEAD)) {
  604. /*
  605. * If a packet was aborted before the DMA controller has
  606. * finished transferring it, it seems that there are 2 bytes
  607. * which are stuck in some buffer somewhere. These will get
  608. * transmitted as soon as we read the frame status (which
  609. * reenables the transmit data transfer request). Turning
  610. * off the DMA controller and/or resetting the MACE doesn't
  611. * help. So we disable auto-padding and FCS transmission
  612. * so the two bytes will only be a runt packet which should
  613. * be ignored by other stations.
  614. */
  615. out_8(&mb->xmtfc, DXMTFCS);
  616. }
  617. fs = in_8(&mb->xmtfs);
  618. if ((fs & XMTSV) == 0) {
  619. printk(KERN_ERR "mace: xmtfs not valid! (fs=%x xc=%d ds=%x)\n",
  620. fs, xcount, dstat);
  621. mace_reset(dev);
  622. /*
  623. * XXX mace likes to hang the machine after a xmtfs error.
  624. * This is hard to reproduce, reseting *may* help
  625. */
  626. }
  627. cp = mp->tx_cmds + NCMDS_TX * i;
  628. stat = ld_le16(&cp->xfer_status);
  629. if ((fs & (UFLO|LCOL|LCAR|RTRY)) || (dstat & DEAD) || xcount == 0) {
  630. /*
  631. * Check whether there were in fact 2 bytes written to
  632. * the transmit FIFO.
  633. */
  634. udelay(1);
  635. x = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
  636. if (x != 0) {
  637. /* there were two bytes with an end-of-packet indication */
  638. mp->tx_bad_runt = 1;
  639. mace_set_timeout(dev);
  640. } else {
  641. /*
  642. * Either there weren't the two bytes buffered up, or they
  643. * didn't have an end-of-packet indication.
  644. * We flush the transmit FIFO just in case (by setting the
  645. * XMTFWU bit with the transmitter disabled).
  646. */
  647. out_8(&mb->maccc, in_8(&mb->maccc) & ~ENXMT);
  648. out_8(&mb->fifocc, in_8(&mb->fifocc) | XMTFWU);
  649. udelay(1);
  650. out_8(&mb->maccc, in_8(&mb->maccc) | ENXMT);
  651. out_8(&mb->xmtfc, AUTO_PAD_XMIT);
  652. }
  653. }
  654. /* dma should have finished */
  655. if (i == mp->tx_fill) {
  656. printk(KERN_DEBUG "mace: tx ring ran out? (fs=%x xc=%d ds=%x)\n",
  657. fs, xcount, dstat);
  658. continue;
  659. }
  660. /* Update stats */
  661. if (fs & (UFLO|LCOL|LCAR|RTRY)) {
  662. ++dev->stats.tx_errors;
  663. if (fs & LCAR)
  664. ++dev->stats.tx_carrier_errors;
  665. if (fs & (UFLO|LCOL|RTRY))
  666. ++dev->stats.tx_aborted_errors;
  667. } else {
  668. dev->stats.tx_bytes += mp->tx_bufs[i]->len;
  669. ++dev->stats.tx_packets;
  670. }
  671. dev_kfree_skb_irq(mp->tx_bufs[i]);
  672. --mp->tx_active;
  673. if (++i >= N_TX_RING)
  674. i = 0;
  675. #if 0
  676. mace_last_fs = fs;
  677. mace_last_xcount = xcount;
  678. #endif
  679. }
  680. if (i != mp->tx_empty) {
  681. mp->tx_fullup = 0;
  682. netif_wake_queue(dev);
  683. }
  684. mp->tx_empty = i;
  685. i += mp->tx_active;
  686. if (i >= N_TX_RING)
  687. i -= N_TX_RING;
  688. if (!mp->tx_bad_runt && i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE) {
  689. do {
  690. /* set up the next one */
  691. cp = mp->tx_cmds + NCMDS_TX * i;
  692. out_le16(&cp->xfer_status, 0);
  693. out_le16(&cp->command, OUTPUT_LAST);
  694. ++mp->tx_active;
  695. if (++i >= N_TX_RING)
  696. i = 0;
  697. } while (i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE);
  698. out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
  699. mace_set_timeout(dev);
  700. }
  701. spin_unlock_irqrestore(&mp->lock, flags);
  702. return IRQ_HANDLED;
  703. }
  704. static void mace_tx_timeout(unsigned long data)
  705. {
  706. struct net_device *dev = (struct net_device *) data;
  707. struct mace_data *mp = netdev_priv(dev);
  708. volatile struct mace __iomem *mb = mp->mace;
  709. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  710. volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
  711. volatile struct dbdma_cmd *cp;
  712. unsigned long flags;
  713. int i;
  714. spin_lock_irqsave(&mp->lock, flags);
  715. mp->timeout_active = 0;
  716. if (mp->tx_active == 0 && !mp->tx_bad_runt)
  717. goto out;
  718. /* update various counters */
  719. mace_handle_misc_intrs(mp, in_8(&mb->ir), dev);
  720. cp = mp->tx_cmds + NCMDS_TX * mp->tx_empty;
  721. /* turn off both tx and rx and reset the chip */
  722. out_8(&mb->maccc, 0);
  723. printk(KERN_ERR "mace: transmit timeout - resetting\n");
  724. dbdma_reset(td);
  725. mace_reset(dev);
  726. /* restart rx dma */
  727. cp = bus_to_virt(ld_le32(&rd->cmdptr));
  728. dbdma_reset(rd);
  729. out_le16(&cp->xfer_status, 0);
  730. out_le32(&rd->cmdptr, virt_to_bus(cp));
  731. out_le32(&rd->control, (RUN << 16) | RUN);
  732. /* fix up the transmit side */
  733. i = mp->tx_empty;
  734. mp->tx_active = 0;
  735. ++dev->stats.tx_errors;
  736. if (mp->tx_bad_runt) {
  737. mp->tx_bad_runt = 0;
  738. } else if (i != mp->tx_fill) {
  739. dev_kfree_skb(mp->tx_bufs[i]);
  740. if (++i >= N_TX_RING)
  741. i = 0;
  742. mp->tx_empty = i;
  743. }
  744. mp->tx_fullup = 0;
  745. netif_wake_queue(dev);
  746. if (i != mp->tx_fill) {
  747. cp = mp->tx_cmds + NCMDS_TX * i;
  748. out_le16(&cp->xfer_status, 0);
  749. out_le16(&cp->command, OUTPUT_LAST);
  750. out_le32(&td->cmdptr, virt_to_bus(cp));
  751. out_le32(&td->control, (RUN << 16) | RUN);
  752. ++mp->tx_active;
  753. mace_set_timeout(dev);
  754. }
  755. /* turn it back on */
  756. out_8(&mb->imr, RCVINT);
  757. out_8(&mb->maccc, mp->maccc);
  758. out:
  759. spin_unlock_irqrestore(&mp->lock, flags);
  760. }
  761. static irqreturn_t mace_txdma_intr(int irq, void *dev_id)
  762. {
  763. return IRQ_HANDLED;
  764. }
  765. static irqreturn_t mace_rxdma_intr(int irq, void *dev_id)
  766. {
  767. struct net_device *dev = (struct net_device *) dev_id;
  768. struct mace_data *mp = netdev_priv(dev);
  769. volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
  770. volatile struct dbdma_cmd *cp, *np;
  771. int i, nb, stat, next;
  772. struct sk_buff *skb;
  773. unsigned frame_status;
  774. static int mace_lost_status;
  775. unsigned char *data;
  776. unsigned long flags;
  777. spin_lock_irqsave(&mp->lock, flags);
  778. for (i = mp->rx_empty; i != mp->rx_fill; ) {
  779. cp = mp->rx_cmds + i;
  780. stat = ld_le16(&cp->xfer_status);
  781. if ((stat & ACTIVE) == 0) {
  782. next = i + 1;
  783. if (next >= N_RX_RING)
  784. next = 0;
  785. np = mp->rx_cmds + next;
  786. if (next != mp->rx_fill
  787. && (ld_le16(&np->xfer_status) & ACTIVE) != 0) {
  788. printk(KERN_DEBUG "mace: lost a status word\n");
  789. ++mace_lost_status;
  790. } else
  791. break;
  792. }
  793. nb = ld_le16(&cp->req_count) - ld_le16(&cp->res_count);
  794. out_le16(&cp->command, DBDMA_STOP);
  795. /* got a packet, have a look at it */
  796. skb = mp->rx_bufs[i];
  797. if (!skb) {
  798. ++dev->stats.rx_dropped;
  799. } else if (nb > 8) {
  800. data = skb->data;
  801. frame_status = (data[nb-3] << 8) + data[nb-4];
  802. if (frame_status & (RS_OFLO|RS_CLSN|RS_FRAMERR|RS_FCSERR)) {
  803. ++dev->stats.rx_errors;
  804. if (frame_status & RS_OFLO)
  805. ++dev->stats.rx_over_errors;
  806. if (frame_status & RS_FRAMERR)
  807. ++dev->stats.rx_frame_errors;
  808. if (frame_status & RS_FCSERR)
  809. ++dev->stats.rx_crc_errors;
  810. } else {
  811. /* Mace feature AUTO_STRIP_RCV is on by default, dropping the
  812. * FCS on frames with 802.3 headers. This means that Ethernet
  813. * frames have 8 extra octets at the end, while 802.3 frames
  814. * have only 4. We need to correctly account for this. */
  815. if (*(unsigned short *)(data+12) < 1536) /* 802.3 header */
  816. nb -= 4;
  817. else /* Ethernet header; mace includes FCS */
  818. nb -= 8;
  819. skb_put(skb, nb);
  820. skb->protocol = eth_type_trans(skb, dev);
  821. dev->stats.rx_bytes += skb->len;
  822. netif_rx(skb);
  823. mp->rx_bufs[i] = NULL;
  824. ++dev->stats.rx_packets;
  825. }
  826. } else {
  827. ++dev->stats.rx_errors;
  828. ++dev->stats.rx_length_errors;
  829. }
  830. /* advance to next */
  831. if (++i >= N_RX_RING)
  832. i = 0;
  833. }
  834. mp->rx_empty = i;
  835. i = mp->rx_fill;
  836. for (;;) {
  837. next = i + 1;
  838. if (next >= N_RX_RING)
  839. next = 0;
  840. if (next == mp->rx_empty)
  841. break;
  842. cp = mp->rx_cmds + i;
  843. skb = mp->rx_bufs[i];
  844. if (!skb) {
  845. skb = dev_alloc_skb(RX_BUFLEN + 2);
  846. if (skb) {
  847. skb_reserve(skb, 2);
  848. mp->rx_bufs[i] = skb;
  849. }
  850. }
  851. st_le16(&cp->req_count, RX_BUFLEN);
  852. data = skb? skb->data: dummy_buf;
  853. st_le32(&cp->phy_addr, virt_to_bus(data));
  854. out_le16(&cp->xfer_status, 0);
  855. out_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
  856. #if 0
  857. if ((ld_le32(&rd->status) & ACTIVE) != 0) {
  858. out_le32(&rd->control, (PAUSE << 16) | PAUSE);
  859. while ((in_le32(&rd->status) & ACTIVE) != 0)
  860. ;
  861. }
  862. #endif
  863. i = next;
  864. }
  865. if (i != mp->rx_fill) {
  866. out_le32(&rd->control, ((RUN|WAKE) << 16) | (RUN|WAKE));
  867. mp->rx_fill = i;
  868. }
  869. spin_unlock_irqrestore(&mp->lock, flags);
  870. return IRQ_HANDLED;
  871. }
  872. static struct of_device_id mace_match[] =
  873. {
  874. {
  875. .name = "mace",
  876. },
  877. {},
  878. };
  879. MODULE_DEVICE_TABLE (of, mace_match);
  880. static struct macio_driver mace_driver =
  881. {
  882. .name = "mace",
  883. .match_table = mace_match,
  884. .probe = mace_probe,
  885. .remove = mace_remove,
  886. };
  887. static int __init mace_init(void)
  888. {
  889. return macio_register_driver(&mace_driver);
  890. }
  891. static void __exit mace_cleanup(void)
  892. {
  893. macio_unregister_driver(&mace_driver);
  894. kfree(dummy_buf);
  895. dummy_buf = NULL;
  896. }
  897. MODULE_AUTHOR("Paul Mackerras");
  898. MODULE_DESCRIPTION("PowerMac MACE driver.");
  899. module_param(port_aaui, int, 0);
  900. MODULE_PARM_DESC(port_aaui, "MACE uses AAUI port (0-1)");
  901. MODULE_LICENSE("GPL");
  902. module_init(mace_init);
  903. module_exit(mace_cleanup);