hysdn_net.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. /* store the actual version for log reporting */
  26. char *hysdn_net_revision = "$Revision: 1.8.6.4 $";
  27. #define MAX_SKB_BUFFERS 20 /* number of buffers for keeping TX-data */
  28. /****************************************************************************/
  29. /* structure containing the complete network data. The structure is aligned */
  30. /* in a way that both, the device and statistics are kept inside it. */
  31. /* for proper access, the device structure MUST be the first var/struct */
  32. /* inside the definition. */
  33. /****************************************************************************/
  34. struct net_local {
  35. struct net_device netdev; /* the network device */
  36. struct net_device_stats stats;
  37. /* additional vars may be added here */
  38. char dev_name[9]; /* our own device name */
  39. /* Tx control lock. This protects the transmit buffer ring
  40. * state along with the "tx full" state of the driver. This
  41. * means all netif_queue flow control actions are protected
  42. * by this lock as well.
  43. */
  44. spinlock_t lock;
  45. struct sk_buff *skbs[MAX_SKB_BUFFERS]; /* pointers to tx-skbs */
  46. int in_idx, out_idx; /* indexes to buffer ring */
  47. int sk_count; /* number of buffers currently in ring */
  48. }; /* net_local */
  49. /*****************************************************/
  50. /* Get the current statistics for this card. */
  51. /* This may be called with the card open or closed ! */
  52. /*****************************************************/
  53. static struct net_device_stats *
  54. net_get_stats(struct net_device *dev)
  55. {
  56. return (&((struct net_local *) dev)->stats);
  57. } /* net_device_stats */
  58. /*********************************************************************/
  59. /* Open/initialize the board. This is called (in the current kernel) */
  60. /* sometime after booting when the 'ifconfig' program is run. */
  61. /* This routine should set everything up anew at each open, even */
  62. /* registers that "should" only need to be set once at boot, so that */
  63. /* there is non-reboot way to recover if something goes wrong. */
  64. /*********************************************************************/
  65. static int
  66. net_open(struct net_device *dev)
  67. {
  68. struct in_device *in_dev;
  69. hysdn_card *card = dev->priv;
  70. int i;
  71. netif_start_queue(dev); /* start tx-queueing */
  72. /* Fill in the MAC-level header (if not already set) */
  73. if (!card->mac_addr[0]) {
  74. for (i = 0; i < ETH_ALEN - sizeof(unsigned long); i++)
  75. dev->dev_addr[i] = 0xfc;
  76. if ((in_dev = dev->ip_ptr) != NULL) {
  77. struct in_ifaddr *ifa = in_dev->ifa_list;
  78. if (ifa != NULL)
  79. memcpy(dev->dev_addr + (ETH_ALEN - sizeof(unsigned long)), &ifa->ifa_local, sizeof(unsigned long));
  80. }
  81. } else
  82. memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);
  83. return (0);
  84. } /* net_open */
  85. /*******************************************/
  86. /* flush the currently occupied tx-buffers */
  87. /* must only be called when device closed */
  88. /*******************************************/
  89. static void
  90. flush_tx_buffers(struct net_local *nl)
  91. {
  92. while (nl->sk_count) {
  93. dev_kfree_skb(nl->skbs[nl->out_idx++]); /* free skb */
  94. if (nl->out_idx >= MAX_SKB_BUFFERS)
  95. nl->out_idx = 0; /* wrap around */
  96. nl->sk_count--;
  97. }
  98. } /* flush_tx_buffers */
  99. /*********************************************************************/
  100. /* close/decativate the device. The device is not removed, but only */
  101. /* deactivated. */
  102. /*********************************************************************/
  103. static int
  104. net_close(struct net_device *dev)
  105. {
  106. netif_stop_queue(dev); /* disable queueing */
  107. flush_tx_buffers((struct net_local *) dev);
  108. return (0); /* success */
  109. } /* net_close */
  110. /************************************/
  111. /* send a packet on this interface. */
  112. /* new style for kernel >= 2.3.33 */
  113. /************************************/
  114. static int
  115. net_send_packet(struct sk_buff *skb, struct net_device *dev)
  116. {
  117. struct net_local *lp = (struct net_local *) dev;
  118. spin_lock_irq(&lp->lock);
  119. lp->skbs[lp->in_idx++] = skb; /* add to buffer list */
  120. if (lp->in_idx >= MAX_SKB_BUFFERS)
  121. lp->in_idx = 0; /* wrap around */
  122. lp->sk_count++; /* adjust counter */
  123. dev->trans_start = jiffies;
  124. /* If we just used up the very last entry in the
  125. * TX ring on this device, tell the queueing
  126. * layer to send no more.
  127. */
  128. if (lp->sk_count >= MAX_SKB_BUFFERS)
  129. netif_stop_queue(dev);
  130. /* When the TX completion hw interrupt arrives, this
  131. * is when the transmit statistics are updated.
  132. */
  133. spin_unlock_irq(&lp->lock);
  134. if (lp->sk_count <= 3) {
  135. schedule_work(&((hysdn_card *) dev->priv)->irq_queue);
  136. }
  137. return (0); /* success */
  138. } /* net_send_packet */
  139. /***********************************************************************/
  140. /* acknowlegde a packet send. The network layer will be informed about */
  141. /* completion */
  142. /***********************************************************************/
  143. void
  144. hysdn_tx_netack(hysdn_card * card)
  145. {
  146. struct net_local *lp = card->netif;
  147. if (!lp)
  148. return; /* non existing device */
  149. if (!lp->sk_count)
  150. return; /* error condition */
  151. lp->stats.tx_packets++;
  152. lp->stats.tx_bytes += lp->skbs[lp->out_idx]->len;
  153. dev_kfree_skb(lp->skbs[lp->out_idx++]); /* free skb */
  154. if (lp->out_idx >= MAX_SKB_BUFFERS)
  155. lp->out_idx = 0; /* wrap around */
  156. if (lp->sk_count-- == MAX_SKB_BUFFERS) /* dec usage count */
  157. netif_start_queue((struct net_device *) lp);
  158. } /* hysdn_tx_netack */
  159. /*****************************************************/
  160. /* we got a packet from the network, go and queue it */
  161. /*****************************************************/
  162. void
  163. hysdn_rx_netpkt(hysdn_card * card, unsigned char *buf, unsigned short len)
  164. {
  165. struct net_local *lp = card->netif;
  166. struct sk_buff *skb;
  167. if (!lp)
  168. return; /* non existing device */
  169. lp->stats.rx_bytes += len;
  170. skb = dev_alloc_skb(len);
  171. if (skb == NULL) {
  172. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
  173. lp->netdev.name);
  174. lp->stats.rx_dropped++;
  175. return;
  176. }
  177. /* copy the data */
  178. memcpy(skb_put(skb, len), buf, len);
  179. /* determine the used protocol */
  180. skb->protocol = eth_type_trans(skb, &lp->netdev);
  181. netif_rx(skb);
  182. lp->stats.rx_packets++; /* adjust packet count */
  183. } /* hysdn_rx_netpkt */
  184. /*****************************************************/
  185. /* return the pointer to a network packet to be send */
  186. /*****************************************************/
  187. struct sk_buff *
  188. hysdn_tx_netget(hysdn_card * card)
  189. {
  190. struct net_local *lp = card->netif;
  191. if (!lp)
  192. return (NULL); /* non existing device */
  193. if (!lp->sk_count)
  194. return (NULL); /* nothing available */
  195. return (lp->skbs[lp->out_idx]); /* next packet to send */
  196. } /* hysdn_tx_netget */
  197. /*******************************************/
  198. /* init function called by register device */
  199. /*******************************************/
  200. static int
  201. net_init(struct net_device *dev)
  202. {
  203. /* setup the function table */
  204. dev->open = net_open;
  205. dev->stop = net_close;
  206. dev->hard_start_xmit = net_send_packet;
  207. dev->get_stats = net_get_stats;
  208. /* Fill in the fields of the device structure with ethernet values. */
  209. ether_setup(dev);
  210. return (0); /* success */
  211. } /* net_init */
  212. /*****************************************************************************/
  213. /* hysdn_net_create creates a new net device for the given card. If a device */
  214. /* already exists, it will be deleted and created a new one. The return value */
  215. /* 0 announces success, else a negative error code will be returned. */
  216. /*****************************************************************************/
  217. int
  218. hysdn_net_create(hysdn_card * card)
  219. {
  220. struct net_device *dev;
  221. int i;
  222. if(!card) {
  223. printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");
  224. return (-ENOMEM);
  225. }
  226. hysdn_net_release(card); /* release an existing net device */
  227. if ((dev = kzalloc(sizeof(struct net_local), GFP_KERNEL)) == NULL) {
  228. printk(KERN_WARNING "HYSDN: unable to allocate mem\n");
  229. return (-ENOMEM);
  230. }
  231. spin_lock_init(&((struct net_local *) dev)->lock);
  232. /* initialise necessary or informing fields */
  233. dev->base_addr = card->iobase; /* IO address */
  234. dev->irq = card->irq; /* irq */
  235. dev->init = net_init; /* the init function of the device */
  236. if(dev->name) {
  237. strcpy(dev->name, ((struct net_local *) dev)->dev_name);
  238. }
  239. if ((i = register_netdev(dev))) {
  240. printk(KERN_WARNING "HYSDN: unable to create network device\n");
  241. kfree(dev);
  242. return (i);
  243. }
  244. dev->priv = card; /* remember pointer to own data structure */
  245. card->netif = dev; /* setup the local pointer */
  246. if (card->debug_flags & LOG_NET_INIT)
  247. hysdn_addlog(card, "network device created");
  248. return (0); /* and return success */
  249. } /* hysdn_net_create */
  250. /***************************************************************************/
  251. /* hysdn_net_release deletes the net device for the given card. The return */
  252. /* value 0 announces success, else a negative error code will be returned. */
  253. /***************************************************************************/
  254. int
  255. hysdn_net_release(hysdn_card * card)
  256. {
  257. struct net_device *dev = card->netif;
  258. if (!dev)
  259. return (0); /* non existing */
  260. card->netif = NULL; /* clear out pointer */
  261. dev->stop(dev); /* close the device */
  262. flush_tx_buffers((struct net_local *) dev); /* empty buffers */
  263. unregister_netdev(dev); /* release the device */
  264. free_netdev(dev); /* release the memory allocated */
  265. if (card->debug_flags & LOG_NET_INIT)
  266. hysdn_addlog(card, "network device deleted");
  267. return (0); /* always successful */
  268. } /* hysdn_net_release */
  269. /*****************************************************************************/
  270. /* hysdn_net_getname returns a pointer to the name of the network interface. */
  271. /* if the interface is not existing, a "-" is returned. */
  272. /*****************************************************************************/
  273. char *
  274. hysdn_net_getname(hysdn_card * card)
  275. {
  276. struct net_device *dev = card->netif;
  277. if (!dev)
  278. return ("-"); /* non existing */
  279. return (dev->name);
  280. } /* hysdn_net_getname */