mace.c 27 KB

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