mace.c 28 KB

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