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