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