hysdn_net.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* $Id: hysdn_net.c,v 1.8.6.4 2001/09/23 22:24:54 kai Exp $
  2. *
  3. * Linux driver for HYSDN cards, net (ethernet type) handling routines.
  4. *
  5. * Author Werner Cornelius (werner@titro.de) for Hypercope GmbH
  6. * Copyright 1999 by Werner Cornelius (werner@titro.de)
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. * This net module has been inspired by the skeleton driver from
  12. * Donald Becker (becker@CESDIS.gsfc.nasa.gov)
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/signal.h>
  17. #include <linux/kernel.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/inetdevice.h>
  22. #include "hysdn_defs.h"
  23. unsigned int hynet_enable = 0xffffffff;
  24. module_param(hynet_enable, uint, 0);
  25. #define MAX_SKB_BUFFERS 20 /* number of buffers for keeping TX-data */
  26. /****************************************************************************/
  27. /* structure containing the complete network data. The structure is aligned */
  28. /* in a way that both, the device and statistics are kept inside it. */
  29. /* for proper access, the device structure MUST be the first var/struct */
  30. /* inside the definition. */
  31. /****************************************************************************/
  32. struct net_local {
  33. /* Tx control lock. This protects the transmit buffer ring
  34. * state along with the "tx full" state of the driver. This
  35. * means all netif_queue flow control actions are protected
  36. * by this lock as well.
  37. */
  38. struct net_device *dev;
  39. spinlock_t lock;
  40. struct sk_buff *skbs[MAX_SKB_BUFFERS]; /* pointers to tx-skbs */
  41. int in_idx, out_idx; /* indexes to buffer ring */
  42. int sk_count; /* number of buffers currently in ring */
  43. }; /* net_local */
  44. /*********************************************************************/
  45. /* Open/initialize the board. This is called (in the current kernel) */
  46. /* sometime after booting when the 'ifconfig' program is run. */
  47. /* This routine should set everything up anew at each open, even */
  48. /* registers that "should" only need to be set once at boot, so that */
  49. /* there is non-reboot way to recover if something goes wrong. */
  50. /*********************************************************************/
  51. static int
  52. net_open(struct net_device *dev)
  53. {
  54. struct in_device *in_dev;
  55. hysdn_card *card = dev->ml_priv;
  56. int i;
  57. netif_start_queue(dev); /* start tx-queueing */
  58. /* Fill in the MAC-level header (if not already set) */
  59. if (!card->mac_addr[0]) {
  60. for (i = 0; i < ETH_ALEN; i++)
  61. dev->dev_addr[i] = 0xfc;
  62. if ((in_dev = dev->ip_ptr) != NULL) {
  63. struct in_ifaddr *ifa = in_dev->ifa_list;
  64. if (ifa != NULL)
  65. memcpy(dev->dev_addr + (ETH_ALEN - sizeof(ifa->ifa_local)), &ifa->ifa_local, sizeof(ifa->ifa_local));
  66. }
  67. } else
  68. memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);
  69. return (0);
  70. } /* net_open */
  71. /*******************************************/
  72. /* flush the currently occupied tx-buffers */
  73. /* must only be called when device closed */
  74. /*******************************************/
  75. static void
  76. flush_tx_buffers(struct net_local *nl)
  77. {
  78. while (nl->sk_count) {
  79. dev_kfree_skb(nl->skbs[nl->out_idx++]); /* free skb */
  80. if (nl->out_idx >= MAX_SKB_BUFFERS)
  81. nl->out_idx = 0; /* wrap around */
  82. nl->sk_count--;
  83. }
  84. } /* flush_tx_buffers */
  85. /*********************************************************************/
  86. /* close/decativate the device. The device is not removed, but only */
  87. /* deactivated. */
  88. /*********************************************************************/
  89. static int
  90. net_close(struct net_device *dev)
  91. {
  92. netif_stop_queue(dev); /* disable queueing */
  93. flush_tx_buffers((struct net_local *) dev);
  94. return (0); /* success */
  95. } /* net_close */
  96. /************************************/
  97. /* send a packet on this interface. */
  98. /* new style for kernel >= 2.3.33 */
  99. /************************************/
  100. static netdev_tx_t
  101. net_send_packet(struct sk_buff *skb, struct net_device *dev)
  102. {
  103. struct net_local *lp = (struct net_local *) dev;
  104. spin_lock_irq(&lp->lock);
  105. lp->skbs[lp->in_idx++] = skb; /* add to buffer list */
  106. if (lp->in_idx >= MAX_SKB_BUFFERS)
  107. lp->in_idx = 0; /* wrap around */
  108. lp->sk_count++; /* adjust counter */
  109. netif_trans_update(dev);
  110. /* If we just used up the very last entry in the
  111. * TX ring on this device, tell the queueing
  112. * layer to send no more.
  113. */
  114. if (lp->sk_count >= MAX_SKB_BUFFERS)
  115. netif_stop_queue(dev);
  116. /* When the TX completion hw interrupt arrives, this
  117. * is when the transmit statistics are updated.
  118. */
  119. spin_unlock_irq(&lp->lock);
  120. if (lp->sk_count <= 3) {
  121. schedule_work(&((hysdn_card *) dev->ml_priv)->irq_queue);
  122. }
  123. return NETDEV_TX_OK; /* success */
  124. } /* net_send_packet */
  125. /***********************************************************************/
  126. /* acknowlegde a packet send. The network layer will be informed about */
  127. /* completion */
  128. /***********************************************************************/
  129. void
  130. hysdn_tx_netack(hysdn_card *card)
  131. {
  132. struct net_local *lp = card->netif;
  133. if (!lp)
  134. return; /* non existing device */
  135. if (!lp->sk_count)
  136. return; /* error condition */
  137. lp->dev->stats.tx_packets++;
  138. lp->dev->stats.tx_bytes += lp->skbs[lp->out_idx]->len;
  139. dev_kfree_skb(lp->skbs[lp->out_idx++]); /* free skb */
  140. if (lp->out_idx >= MAX_SKB_BUFFERS)
  141. lp->out_idx = 0; /* wrap around */
  142. if (lp->sk_count-- == MAX_SKB_BUFFERS) /* dec usage count */
  143. netif_start_queue((struct net_device *) lp);
  144. } /* hysdn_tx_netack */
  145. /*****************************************************/
  146. /* we got a packet from the network, go and queue it */
  147. /*****************************************************/
  148. void
  149. hysdn_rx_netpkt(hysdn_card *card, unsigned char *buf, unsigned short len)
  150. {
  151. struct net_local *lp = card->netif;
  152. struct net_device *dev;
  153. struct sk_buff *skb;
  154. if (!lp)
  155. return; /* non existing device */
  156. dev = lp->dev;
  157. dev->stats.rx_bytes += len;
  158. skb = dev_alloc_skb(len);
  159. if (skb == NULL) {
  160. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
  161. dev->name);
  162. dev->stats.rx_dropped++;
  163. return;
  164. }
  165. /* copy the data */
  166. memcpy(skb_put(skb, len), buf, len);
  167. /* determine the used protocol */
  168. skb->protocol = eth_type_trans(skb, dev);
  169. dev->stats.rx_packets++; /* adjust packet count */
  170. netif_rx(skb);
  171. } /* hysdn_rx_netpkt */
  172. /*****************************************************/
  173. /* return the pointer to a network packet to be send */
  174. /*****************************************************/
  175. struct sk_buff *
  176. hysdn_tx_netget(hysdn_card *card)
  177. {
  178. struct net_local *lp = card->netif;
  179. if (!lp)
  180. return (NULL); /* non existing device */
  181. if (!lp->sk_count)
  182. return (NULL); /* nothing available */
  183. return (lp->skbs[lp->out_idx]); /* next packet to send */
  184. } /* hysdn_tx_netget */
  185. static const struct net_device_ops hysdn_netdev_ops = {
  186. .ndo_open = net_open,
  187. .ndo_stop = net_close,
  188. .ndo_start_xmit = net_send_packet,
  189. .ndo_change_mtu = eth_change_mtu,
  190. .ndo_set_mac_address = eth_mac_addr,
  191. .ndo_validate_addr = eth_validate_addr,
  192. };
  193. /*****************************************************************************/
  194. /* hysdn_net_create creates a new net device for the given card. If a device */
  195. /* already exists, it will be deleted and created a new one. The return value */
  196. /* 0 announces success, else a negative error code will be returned. */
  197. /*****************************************************************************/
  198. int
  199. hysdn_net_create(hysdn_card *card)
  200. {
  201. struct net_device *dev;
  202. int i;
  203. struct net_local *lp;
  204. if (!card) {
  205. printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");
  206. return (-ENOMEM);
  207. }
  208. hysdn_net_release(card); /* release an existing net device */
  209. dev = alloc_etherdev(sizeof(struct net_local));
  210. if (!dev) {
  211. printk(KERN_WARNING "HYSDN: unable to allocate mem\n");
  212. return (-ENOMEM);
  213. }
  214. lp = netdev_priv(dev);
  215. lp->dev = dev;
  216. dev->netdev_ops = &hysdn_netdev_ops;
  217. spin_lock_init(&((struct net_local *) dev)->lock);
  218. /* initialise necessary or informing fields */
  219. dev->base_addr = card->iobase; /* IO address */
  220. dev->irq = card->irq; /* irq */
  221. dev->netdev_ops = &hysdn_netdev_ops;
  222. if ((i = register_netdev(dev))) {
  223. printk(KERN_WARNING "HYSDN: unable to create network device\n");
  224. free_netdev(dev);
  225. return (i);
  226. }
  227. dev->ml_priv = card; /* remember pointer to own data structure */
  228. card->netif = dev; /* setup the local pointer */
  229. if (card->debug_flags & LOG_NET_INIT)
  230. hysdn_addlog(card, "network device created");
  231. return (0); /* and return success */
  232. } /* hysdn_net_create */
  233. /***************************************************************************/
  234. /* hysdn_net_release deletes the net device for the given card. The return */
  235. /* value 0 announces success, else a negative error code will be returned. */
  236. /***************************************************************************/
  237. int
  238. hysdn_net_release(hysdn_card *card)
  239. {
  240. struct net_device *dev = card->netif;
  241. if (!dev)
  242. return (0); /* non existing */
  243. card->netif = NULL; /* clear out pointer */
  244. net_close(dev);
  245. flush_tx_buffers((struct net_local *) dev); /* empty buffers */
  246. unregister_netdev(dev); /* release the device */
  247. free_netdev(dev); /* release the memory allocated */
  248. if (card->debug_flags & LOG_NET_INIT)
  249. hysdn_addlog(card, "network device deleted");
  250. return (0); /* always successful */
  251. } /* hysdn_net_release */
  252. /*****************************************************************************/
  253. /* hysdn_net_getname returns a pointer to the name of the network interface. */
  254. /* if the interface is not existing, a "-" is returned. */
  255. /*****************************************************************************/
  256. char *
  257. hysdn_net_getname(hysdn_card *card)
  258. {
  259. struct net_device *dev = card->netif;
  260. if (!dev)
  261. return ("-"); /* non existing */
  262. return (dev->name);
  263. } /* hysdn_net_getname */