mace.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:26k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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