nfeth.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * atari_nfeth.c - ARAnyM ethernet card driver for GNU/Linux
  3. *
  4. * Copyright (c) 2005 Milan Jurik, Petr Stehlik of ARAnyM dev team
  5. *
  6. * Based on ARAnyM driver for FreeMiNT written by Standa Opichal
  7. *
  8. * This software may be used and distributed according to the terms of
  9. * the GNU General Public License (GPL), incorporated herein by reference.
  10. */
  11. #define DRV_VERSION "0.3"
  12. #define DRV_RELDATE "10/12/2005"
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <asm/natfeat.h>
  19. #include <asm/virtconvert.h>
  20. enum {
  21. GET_VERSION = 0,/* no parameters, return NFAPI_VERSION in d0 */
  22. XIF_INTLEVEL, /* no parameters, return Interrupt Level in d0 */
  23. XIF_IRQ, /* acknowledge interrupt from host */
  24. XIF_START, /* (ethX), called on 'ifup', start receiver thread */
  25. XIF_STOP, /* (ethX), called on 'ifdown', stop the thread */
  26. XIF_READLENGTH, /* (ethX), return size of network data block to read */
  27. XIF_READBLOCK, /* (ethX, buffer, size), read block of network data */
  28. XIF_WRITEBLOCK, /* (ethX, buffer, size), write block of network data */
  29. XIF_GET_MAC, /* (ethX, buffer, size), return MAC HW addr in buffer */
  30. XIF_GET_IPHOST, /* (ethX, buffer, size), return IP address of host */
  31. XIF_GET_IPATARI,/* (ethX, buffer, size), return IP address of atari */
  32. XIF_GET_NETMASK /* (ethX, buffer, size), return IP netmask */
  33. };
  34. #define MAX_UNIT 8
  35. /* These identify the driver base version and may not be removed. */
  36. static const char version[] =
  37. KERN_INFO KBUILD_MODNAME ".c:v" DRV_VERSION " " DRV_RELDATE
  38. " S.Opichal, M.Jurik, P.Stehlik\n"
  39. KERN_INFO " http://aranym.org/\n";
  40. MODULE_AUTHOR("Milan Jurik");
  41. MODULE_DESCRIPTION("Atari NFeth driver");
  42. MODULE_LICENSE("GPL");
  43. /*
  44. MODULE_PARM(nfeth_debug, "i");
  45. MODULE_PARM_DESC(nfeth_debug, "nfeth_debug level (1-2)");
  46. */
  47. static long nfEtherID;
  48. static int nfEtherIRQ;
  49. struct nfeth_private {
  50. int ethX;
  51. };
  52. static struct net_device *nfeth_dev[MAX_UNIT];
  53. static int nfeth_open(struct net_device *dev)
  54. {
  55. struct nfeth_private *priv = netdev_priv(dev);
  56. int res;
  57. res = nf_call(nfEtherID + XIF_START, priv->ethX);
  58. netdev_dbg(dev, "%s: %d\n", __func__, res);
  59. /* Ready for data */
  60. netif_start_queue(dev);
  61. return 0;
  62. }
  63. static int nfeth_stop(struct net_device *dev)
  64. {
  65. struct nfeth_private *priv = netdev_priv(dev);
  66. /* No more data */
  67. netif_stop_queue(dev);
  68. nf_call(nfEtherID + XIF_STOP, priv->ethX);
  69. return 0;
  70. }
  71. /*
  72. * Read a packet out of the adapter and pass it to the upper layers
  73. */
  74. static inline void recv_packet(struct net_device *dev)
  75. {
  76. struct nfeth_private *priv = netdev_priv(dev);
  77. unsigned short pktlen;
  78. struct sk_buff *skb;
  79. /* read packet length (excluding 32 bit crc) */
  80. pktlen = nf_call(nfEtherID + XIF_READLENGTH, priv->ethX);
  81. netdev_dbg(dev, "%s: %u\n", __func__, pktlen);
  82. if (!pktlen) {
  83. netdev_dbg(dev, "%s: pktlen == 0\n", __func__);
  84. dev->stats.rx_errors++;
  85. return;
  86. }
  87. skb = dev_alloc_skb(pktlen + 2);
  88. if (!skb) {
  89. netdev_dbg(dev, "%s: out of mem (buf_alloc failed)\n",
  90. __func__);
  91. dev->stats.rx_dropped++;
  92. return;
  93. }
  94. skb->dev = dev;
  95. skb_reserve(skb, 2); /* 16 Byte align */
  96. skb_put(skb, pktlen); /* make room */
  97. nf_call(nfEtherID + XIF_READBLOCK, priv->ethX, virt_to_phys(skb->data),
  98. pktlen);
  99. skb->protocol = eth_type_trans(skb, dev);
  100. netif_rx(skb);
  101. dev->last_rx = jiffies;
  102. dev->stats.rx_packets++;
  103. dev->stats.rx_bytes += pktlen;
  104. /* and enqueue packet */
  105. return;
  106. }
  107. static irqreturn_t nfeth_interrupt(int irq, void *dev_id)
  108. {
  109. int i, m, mask;
  110. mask = nf_call(nfEtherID + XIF_IRQ, 0);
  111. for (i = 0, m = 1; i < MAX_UNIT; m <<= 1, i++) {
  112. if (mask & m && nfeth_dev[i]) {
  113. recv_packet(nfeth_dev[i]);
  114. nf_call(nfEtherID + XIF_IRQ, m);
  115. }
  116. }
  117. return IRQ_HANDLED;
  118. }
  119. static int nfeth_xmit(struct sk_buff *skb, struct net_device *dev)
  120. {
  121. unsigned int len;
  122. char *data, shortpkt[ETH_ZLEN];
  123. struct nfeth_private *priv = netdev_priv(dev);
  124. data = skb->data;
  125. len = skb->len;
  126. if (len < ETH_ZLEN) {
  127. memset(shortpkt, 0, ETH_ZLEN);
  128. memcpy(shortpkt, data, len);
  129. data = shortpkt;
  130. len = ETH_ZLEN;
  131. }
  132. netdev_dbg(dev, "%s: send %u bytes\n", __func__, len);
  133. nf_call(nfEtherID + XIF_WRITEBLOCK, priv->ethX, virt_to_phys(data),
  134. len);
  135. dev->stats.tx_packets++;
  136. dev->stats.tx_bytes += len;
  137. dev_kfree_skb(skb);
  138. return 0;
  139. }
  140. static void nfeth_tx_timeout(struct net_device *dev)
  141. {
  142. dev->stats.tx_errors++;
  143. netif_wake_queue(dev);
  144. }
  145. static const struct net_device_ops nfeth_netdev_ops = {
  146. .ndo_open = nfeth_open,
  147. .ndo_stop = nfeth_stop,
  148. .ndo_start_xmit = nfeth_xmit,
  149. .ndo_tx_timeout = nfeth_tx_timeout,
  150. .ndo_validate_addr = eth_validate_addr,
  151. .ndo_change_mtu = eth_change_mtu,
  152. .ndo_set_mac_address = eth_mac_addr,
  153. };
  154. static struct net_device * __init nfeth_probe(int unit)
  155. {
  156. struct net_device *dev;
  157. struct nfeth_private *priv;
  158. char mac[ETH_ALEN], host_ip[32], local_ip[32];
  159. int err;
  160. if (!nf_call(nfEtherID + XIF_GET_MAC, unit, virt_to_phys(mac),
  161. ETH_ALEN))
  162. return NULL;
  163. dev = alloc_etherdev(sizeof(struct nfeth_private));
  164. if (!dev)
  165. return NULL;
  166. dev->irq = nfEtherIRQ;
  167. dev->netdev_ops = &nfeth_netdev_ops;
  168. memcpy(dev->dev_addr, mac, ETH_ALEN);
  169. priv = netdev_priv(dev);
  170. priv->ethX = unit;
  171. err = register_netdev(dev);
  172. if (err) {
  173. free_netdev(dev);
  174. return NULL;
  175. }
  176. nf_call(nfEtherID + XIF_GET_IPHOST, unit,
  177. virt_to_phys(host_ip), sizeof(host_ip));
  178. nf_call(nfEtherID + XIF_GET_IPATARI, unit,
  179. virt_to_phys(local_ip), sizeof(local_ip));
  180. netdev_info(dev, KBUILD_MODNAME " addr:%s (%s) HWaddr:%pM\n", host_ip,
  181. local_ip, mac);
  182. return dev;
  183. }
  184. static int __init nfeth_init(void)
  185. {
  186. long ver;
  187. int error, i;
  188. nfEtherID = nf_get_id("ETHERNET");
  189. if (!nfEtherID)
  190. return -ENODEV;
  191. ver = nf_call(nfEtherID + GET_VERSION);
  192. pr_info("API %lu\n", ver);
  193. nfEtherIRQ = nf_call(nfEtherID + XIF_INTLEVEL);
  194. error = request_irq(nfEtherIRQ, nfeth_interrupt, IRQF_SHARED,
  195. "eth emu", nfeth_interrupt);
  196. if (error) {
  197. pr_err("request for irq %d failed %d", nfEtherIRQ, error);
  198. return error;
  199. }
  200. for (i = 0; i < MAX_UNIT; i++)
  201. nfeth_dev[i] = nfeth_probe(i);
  202. return 0;
  203. }
  204. static void __exit nfeth_cleanup(void)
  205. {
  206. int i;
  207. for (i = 0; i < MAX_UNIT; i++) {
  208. if (nfeth_dev[i]) {
  209. unregister_netdev(nfeth_dev[0]);
  210. free_netdev(nfeth_dev[0]);
  211. }
  212. }
  213. free_irq(nfEtherIRQ, nfeth_interrupt);
  214. }
  215. module_init(nfeth_init);
  216. module_exit(nfeth_cleanup);