mace.c 28 KB

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