hysdn_net.c 11 KB

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