mace.c 27 KB

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