1 /* $Id: hysdn_net.c,v 1.8.6.4 2001/09/23 22:24:54 kai Exp $
3 * Linux driver for HYSDN cards, net (ethernet type) handling routines.
5 * Author Werner Cornelius (werner@titro.de) for Hypercope GmbH
6 * Copyright 1999 by Werner Cornelius (werner@titro.de)
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
11 * This net module has been inspired by the skeleton driver from
12 * Donald Becker (becker@CESDIS.gsfc.nasa.gov)
16 #include <linux/module.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>
24 #include "hysdn_defs.h"
26 unsigned int hynet_enable = 0xffffffff;
27 module_param(hynet_enable, uint, 0);
29 #define MAX_SKB_BUFFERS 20 /* number of buffers for keeping TX-data */
31 /****************************************************************************/
32 /* structure containing the complete network data. The structure is aligned */
33 /* in a way that both, the device and statistics are kept inside it. */
34 /* for proper access, the device structure MUST be the first var/struct */
35 /* inside the definition. */
36 /****************************************************************************/
38 /* Tx control lock. This protects the transmit buffer ring
39 * state along with the "tx full" state of the driver. This
40 * means all netif_queue flow control actions are protected
41 * by this lock as well.
43 struct net_device *dev;
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 */
52 /*********************************************************************/
53 /* Open/initialize the board. This is called (in the current kernel) */
54 /* sometime after booting when the 'ifconfig' program is run. */
55 /* This routine should set everything up anew at each open, even */
56 /* registers that "should" only need to be set once at boot, so that */
57 /* there is non-reboot way to recover if something goes wrong. */
58 /*********************************************************************/
60 net_open(struct net_device *dev)
62 struct in_device *in_dev;
63 hysdn_card *card = dev->ml_priv;
66 netif_start_queue(dev); /* start tx-queueing */
68 /* Fill in the MAC-level header (if not already set) */
69 if (!card->mac_addr[0]) {
70 for (i = 0; i < ETH_ALEN; i++)
71 dev->dev_addr[i] = 0xfc;
72 if ((in_dev = dev->ip_ptr) != NULL) {
73 const struct in_ifaddr *ifa;
76 ifa = rcu_dereference(in_dev->ifa_list);
78 memcpy(dev->dev_addr + (ETH_ALEN - sizeof(ifa->ifa_local)), &ifa->ifa_local, sizeof(ifa->ifa_local));
82 memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);
87 /*******************************************/
88 /* flush the currently occupied tx-buffers */
89 /* must only be called when device closed */
90 /*******************************************/
92 flush_tx_buffers(struct net_local *nl)
95 while (nl->sk_count) {
96 dev_kfree_skb(nl->skbs[nl->out_idx++]); /* free skb */
97 if (nl->out_idx >= MAX_SKB_BUFFERS)
98 nl->out_idx = 0; /* wrap around */
101 } /* flush_tx_buffers */
104 /*********************************************************************/
105 /* close/decativate the device. The device is not removed, but only */
107 /*********************************************************************/
109 net_close(struct net_device *dev)
112 netif_stop_queue(dev); /* disable queueing */
114 flush_tx_buffers((struct net_local *) dev);
116 return (0); /* success */
119 /************************************/
120 /* send a packet on this interface. */
121 /* new style for kernel >= 2.3.33 */
122 /************************************/
124 net_send_packet(struct sk_buff *skb, struct net_device *dev)
126 struct net_local *lp = (struct net_local *) dev;
128 spin_lock_irq(&lp->lock);
130 lp->skbs[lp->in_idx++] = skb; /* add to buffer list */
131 if (lp->in_idx >= MAX_SKB_BUFFERS)
132 lp->in_idx = 0; /* wrap around */
133 lp->sk_count++; /* adjust counter */
134 netif_trans_update(dev);
136 /* If we just used up the very last entry in the
137 * TX ring on this device, tell the queueing
138 * layer to send no more.
140 if (lp->sk_count >= MAX_SKB_BUFFERS)
141 netif_stop_queue(dev);
143 /* When the TX completion hw interrupt arrives, this
144 * is when the transmit statistics are updated.
147 spin_unlock_irq(&lp->lock);
149 if (lp->sk_count <= 3) {
150 schedule_work(&((hysdn_card *) dev->ml_priv)->irq_queue);
152 return NETDEV_TX_OK; /* success */
153 } /* net_send_packet */
157 /***********************************************************************/
158 /* acknowlegde a packet send. The network layer will be informed about */
160 /***********************************************************************/
162 hysdn_tx_netack(hysdn_card *card)
164 struct net_local *lp = card->netif;
167 return; /* non existing device */
171 return; /* error condition */
173 lp->dev->stats.tx_packets++;
174 lp->dev->stats.tx_bytes += lp->skbs[lp->out_idx]->len;
176 dev_kfree_skb(lp->skbs[lp->out_idx++]); /* free skb */
177 if (lp->out_idx >= MAX_SKB_BUFFERS)
178 lp->out_idx = 0; /* wrap around */
180 if (lp->sk_count-- == MAX_SKB_BUFFERS) /* dec usage count */
181 netif_start_queue((struct net_device *) lp);
182 } /* hysdn_tx_netack */
184 /*****************************************************/
185 /* we got a packet from the network, go and queue it */
186 /*****************************************************/
188 hysdn_rx_netpkt(hysdn_card *card, unsigned char *buf, unsigned short len)
190 struct net_local *lp = card->netif;
191 struct net_device *dev;
195 return; /* non existing device */
198 dev->stats.rx_bytes += len;
200 skb = dev_alloc_skb(len);
202 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
204 dev->stats.rx_dropped++;
208 skb_put_data(skb, buf, len);
210 /* determine the used protocol */
211 skb->protocol = eth_type_trans(skb, dev);
213 dev->stats.rx_packets++; /* adjust packet count */
216 } /* hysdn_rx_netpkt */
218 /*****************************************************/
219 /* return the pointer to a network packet to be send */
220 /*****************************************************/
222 hysdn_tx_netget(hysdn_card *card)
224 struct net_local *lp = card->netif;
227 return (NULL); /* non existing device */
230 return (NULL); /* nothing available */
232 return (lp->skbs[lp->out_idx]); /* next packet to send */
233 } /* hysdn_tx_netget */
235 static const struct net_device_ops hysdn_netdev_ops = {
236 .ndo_open = net_open,
237 .ndo_stop = net_close,
238 .ndo_start_xmit = net_send_packet,
239 .ndo_set_mac_address = eth_mac_addr,
240 .ndo_validate_addr = eth_validate_addr,
244 /*****************************************************************************/
245 /* hysdn_net_create creates a new net device for the given card. If a device */
246 /* already exists, it will be deleted and created a new one. The return value */
247 /* 0 announces success, else a negative error code will be returned. */
248 /*****************************************************************************/
250 hysdn_net_create(hysdn_card *card)
252 struct net_device *dev;
254 struct net_local *lp;
257 printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");
260 hysdn_net_release(card); /* release an existing net device */
262 dev = alloc_etherdev(sizeof(struct net_local));
264 printk(KERN_WARNING "HYSDN: unable to allocate mem\n");
268 lp = netdev_priv(dev);
271 dev->netdev_ops = &hysdn_netdev_ops;
272 spin_lock_init(&((struct net_local *) dev)->lock);
274 /* initialise necessary or informing fields */
275 dev->base_addr = card->iobase; /* IO address */
276 dev->irq = card->irq; /* irq */
278 dev->netdev_ops = &hysdn_netdev_ops;
279 if ((i = register_netdev(dev))) {
280 printk(KERN_WARNING "HYSDN: unable to create network device\n");
284 dev->ml_priv = card; /* remember pointer to own data structure */
285 card->netif = dev; /* setup the local pointer */
287 if (card->debug_flags & LOG_NET_INIT)
288 hysdn_addlog(card, "network device created");
289 return 0; /* and return success */
290 } /* hysdn_net_create */
292 /***************************************************************************/
293 /* hysdn_net_release deletes the net device for the given card. The return */
294 /* value 0 announces success, else a negative error code will be returned. */
295 /***************************************************************************/
297 hysdn_net_release(hysdn_card *card)
299 struct net_device *dev = card->netif;
302 return (0); /* non existing */
304 card->netif = NULL; /* clear out pointer */
307 flush_tx_buffers((struct net_local *) dev); /* empty buffers */
309 unregister_netdev(dev); /* release the device */
310 free_netdev(dev); /* release the memory allocated */
311 if (card->debug_flags & LOG_NET_INIT)
312 hysdn_addlog(card, "network device deleted");
314 return (0); /* always successful */
315 } /* hysdn_net_release */
317 /*****************************************************************************/
318 /* hysdn_net_getname returns a pointer to the name of the network interface. */
319 /* if the interface is not existing, a "-" is returned. */
320 /*****************************************************************************/
322 hysdn_net_getname(hysdn_card *card)
324 struct net_device *dev = card->netif;
327 return ("-"); /* non existing */
330 } /* hysdn_net_getname */