mace.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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(struct timer_list *t);
  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 %pOF: need 3 addrs and 3 irqs\n",
  105. mace);
  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 %pOF\n",
  113. mace);
  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. timer_setup(&mp->tx_timeout, mace_tx_timeout, 0);
  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. add_timer(&mp->tx_timeout);
  444. mp->timeout_active = 1;
  445. }
  446. static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev)
  447. {
  448. struct mace_data *mp = netdev_priv(dev);
  449. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  450. volatile struct dbdma_cmd *cp, *np;
  451. unsigned long flags;
  452. int fill, next, len;
  453. /* see if there's a free slot in the tx ring */
  454. spin_lock_irqsave(&mp->lock, flags);
  455. fill = mp->tx_fill;
  456. next = fill + 1;
  457. if (next >= N_TX_RING)
  458. next = 0;
  459. if (next == mp->tx_empty) {
  460. netif_stop_queue(dev);
  461. mp->tx_fullup = 1;
  462. spin_unlock_irqrestore(&mp->lock, flags);
  463. return NETDEV_TX_BUSY; /* can't take it at the moment */
  464. }
  465. spin_unlock_irqrestore(&mp->lock, flags);
  466. /* partially fill in the dma command block */
  467. len = skb->len;
  468. if (len > ETH_FRAME_LEN) {
  469. printk(KERN_DEBUG "mace: xmit frame too long (%d)\n", len);
  470. len = ETH_FRAME_LEN;
  471. }
  472. mp->tx_bufs[fill] = skb;
  473. cp = mp->tx_cmds + NCMDS_TX * fill;
  474. cp->req_count = cpu_to_le16(len);
  475. cp->phy_addr = cpu_to_le32(virt_to_bus(skb->data));
  476. np = mp->tx_cmds + NCMDS_TX * next;
  477. out_le16(&np->command, DBDMA_STOP);
  478. /* poke the tx dma channel */
  479. spin_lock_irqsave(&mp->lock, flags);
  480. mp->tx_fill = next;
  481. if (!mp->tx_bad_runt && mp->tx_active < MAX_TX_ACTIVE) {
  482. out_le16(&cp->xfer_status, 0);
  483. out_le16(&cp->command, OUTPUT_LAST);
  484. out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
  485. ++mp->tx_active;
  486. mace_set_timeout(dev);
  487. }
  488. if (++next >= N_TX_RING)
  489. next = 0;
  490. if (next == mp->tx_empty)
  491. netif_stop_queue(dev);
  492. spin_unlock_irqrestore(&mp->lock, flags);
  493. return NETDEV_TX_OK;
  494. }
  495. static void mace_set_multicast(struct net_device *dev)
  496. {
  497. struct mace_data *mp = netdev_priv(dev);
  498. volatile struct mace __iomem *mb = mp->mace;
  499. int i;
  500. u32 crc;
  501. unsigned long flags;
  502. spin_lock_irqsave(&mp->lock, flags);
  503. mp->maccc &= ~PROM;
  504. if (dev->flags & IFF_PROMISC) {
  505. mp->maccc |= PROM;
  506. } else {
  507. unsigned char multicast_filter[8];
  508. struct netdev_hw_addr *ha;
  509. if (dev->flags & IFF_ALLMULTI) {
  510. for (i = 0; i < 8; i++)
  511. multicast_filter[i] = 0xff;
  512. } else {
  513. for (i = 0; i < 8; i++)
  514. multicast_filter[i] = 0;
  515. netdev_for_each_mc_addr(ha, dev) {
  516. crc = ether_crc_le(6, ha->addr);
  517. i = crc >> 26; /* bit number in multicast_filter */
  518. multicast_filter[i >> 3] |= 1 << (i & 7);
  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 = le32_to_cpu(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, resetting *may* help
  625. */
  626. }
  627. cp = mp->tx_cmds + NCMDS_TX * i;
  628. stat = le16_to_cpu(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(struct timer_list *t)
  705. {
  706. struct mace_data *mp = from_timer(mp, t, tx_timeout);
  707. struct net_device *dev = macio_get_drvdata(mp->mdev);
  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(le32_to_cpu(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 = le16_to_cpu(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. (le16_to_cpu(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 = le16_to_cpu(cp->req_count) - le16_to_cpu(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 = netdev_alloc_skb(dev, RX_BUFLEN + 2);
  846. if (skb) {
  847. skb_reserve(skb, 2);
  848. mp->rx_bufs[i] = skb;
  849. }
  850. }
  851. cp->req_count = cpu_to_le16(RX_BUFLEN);
  852. data = skb? skb->data: dummy_buf;
  853. cp->phy_addr = cpu_to_le32(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 ((le32_to_cpu(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 const 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. .driver = {
  883. .name = "mace",
  884. .owner = THIS_MODULE,
  885. .of_match_table = mace_match,
  886. },
  887. .probe = mace_probe,
  888. .remove = mace_remove,
  889. };
  890. static int __init mace_init(void)
  891. {
  892. return macio_register_driver(&mace_driver);
  893. }
  894. static void __exit mace_cleanup(void)
  895. {
  896. macio_unregister_driver(&mace_driver);
  897. kfree(dummy_buf);
  898. dummy_buf = NULL;
  899. }
  900. MODULE_AUTHOR("Paul Mackerras");
  901. MODULE_DESCRIPTION("PowerMac MACE driver.");
  902. module_param(port_aaui, int, 0);
  903. MODULE_PARM_DESC(port_aaui, "MACE uses AAUI port (0-1)");
  904. MODULE_LICENSE("GPL");
  905. module_init(mace_init);
  906. module_exit(mace_cleanup);