mace.c 27 KB

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