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/version.h>
18 #include <linux/signal.h>
19 #include <linux/kernel.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/inetdevice.h>
25 #include "hysdn_defs.h"
27 unsigned int hynet_enable
= 0xffffffff;
28 MODULE_PARM(hynet_enable
, "i");
30 /* store the actual version for log reporting */
31 char *hysdn_net_revision
= "$Revision: 1.8.6.4 $";
33 #define MAX_SKB_BUFFERS 20 /* number of buffers for keeping TX-data */
35 /****************************************************************************/
36 /* structure containing the complete network data. The structure is aligned */
37 /* in a way that both, the device and statistics are kept inside it. */
38 /* for proper access, the device structure MUST be the first var/struct */
39 /* inside the definition. */
40 /****************************************************************************/
42 struct net_device netdev
; /* the network device */
43 struct net_device_stats stats
;
44 /* additional vars may be added here */
45 char dev_name
[9]; /* our own device name */
47 /* Tx control lock. This protects the transmit buffer ring
48 * state along with the "tx full" state of the driver. This
49 * means all netif_queue flow control actions are protected
50 * by this lock as well.
53 struct sk_buff
*skbs
[MAX_SKB_BUFFERS
]; /* pointers to tx-skbs */
54 int in_idx
, out_idx
; /* indexes to buffer ring */
55 int sk_count
; /* number of buffers currently in ring */
59 /*****************************************************/
60 /* Get the current statistics for this card. */
61 /* This may be called with the card open or closed ! */
62 /*****************************************************/
63 static struct net_device_stats
*
64 net_get_stats(struct net_device
*dev
)
66 return (&((struct net_local
*) dev
)->stats
);
67 } /* net_device_stats */
69 /*********************************************************************/
70 /* Open/initialize the board. This is called (in the current kernel) */
71 /* sometime after booting when the 'ifconfig' program is run. */
72 /* This routine should set everything up anew at each open, even */
73 /* registers that "should" only need to be set once at boot, so that */
74 /* there is non-reboot way to recover if something goes wrong. */
75 /*********************************************************************/
77 net_open(struct net_device
*dev
)
79 struct in_device
*in_dev
;
80 hysdn_card
*card
= dev
->priv
;
83 netif_start_queue(dev
); /* start tx-queueing */
85 /* Fill in the MAC-level header (if not already set) */
86 if (!card
->mac_addr
[0]) {
87 for (i
= 0; i
< ETH_ALEN
- sizeof(ulong
); i
++)
88 dev
->dev_addr
[i
] = 0xfc;
89 if ((in_dev
= dev
->ip_ptr
) != NULL
) {
90 struct in_ifaddr
*ifa
= in_dev
->ifa_list
;
92 memcpy(dev
->dev_addr
+ (ETH_ALEN
- sizeof(ulong
)), &ifa
->ifa_local
, sizeof(ulong
));
95 memcpy(dev
->dev_addr
, card
->mac_addr
, ETH_ALEN
);
100 /*******************************************/
101 /* flush the currently occupied tx-buffers */
102 /* must only be called when device closed */
103 /*******************************************/
105 flush_tx_buffers(struct net_local
*nl
)
108 while (nl
->sk_count
) {
109 dev_kfree_skb(nl
->skbs
[nl
->out_idx
++]); /* free skb */
110 if (nl
->out_idx
>= MAX_SKB_BUFFERS
)
111 nl
->out_idx
= 0; /* wrap around */
114 } /* flush_tx_buffers */
117 /*********************************************************************/
118 /* close/decativate the device. The device is not removed, but only */
120 /*********************************************************************/
122 net_close(struct net_device
*dev
)
125 netif_stop_queue(dev
); /* disable queueing */
127 flush_tx_buffers((struct net_local
*) dev
);
129 return (0); /* success */
132 /************************************/
133 /* send a packet on this interface. */
134 /* new style for kernel >= 2.3.33 */
135 /************************************/
137 net_send_packet(struct sk_buff
*skb
, struct net_device
*dev
)
139 struct net_local
*lp
= (struct net_local
*) dev
;
141 spin_lock_irq(&lp
->lock
);
143 lp
->skbs
[lp
->in_idx
++] = skb
; /* add to buffer list */
144 if (lp
->in_idx
>= MAX_SKB_BUFFERS
)
145 lp
->in_idx
= 0; /* wrap around */
146 lp
->sk_count
++; /* adjust counter */
147 dev
->trans_start
= jiffies
;
149 /* If we just used up the very last entry in the
150 * TX ring on this device, tell the queueing
151 * layer to send no more.
153 if (lp
->sk_count
>= MAX_SKB_BUFFERS
)
154 netif_stop_queue(dev
);
156 /* When the TX completion hw interrupt arrives, this
157 * is when the transmit statistics are updated.
160 spin_unlock_irq(&lp
->lock
);
162 if (lp
->sk_count
<= 3) {
163 schedule_work(&((hysdn_card
*) dev
->priv
)->irq_queue
);
165 return (0); /* success */
166 } /* net_send_packet */
170 /***********************************************************************/
171 /* acknowlegde a packet send. The network layer will be informed about */
173 /***********************************************************************/
175 hysdn_tx_netack(hysdn_card
* card
)
177 struct net_local
*lp
= card
->netif
;
180 return; /* non existing device */
184 return; /* error condition */
186 lp
->stats
.tx_packets
++;
187 lp
->stats
.tx_bytes
+= lp
->skbs
[lp
->out_idx
]->len
;
189 dev_kfree_skb(lp
->skbs
[lp
->out_idx
++]); /* free skb */
190 if (lp
->out_idx
>= MAX_SKB_BUFFERS
)
191 lp
->out_idx
= 0; /* wrap around */
193 if (lp
->sk_count
-- == MAX_SKB_BUFFERS
) /* dec usage count */
194 netif_start_queue((struct net_device
*) lp
);
195 } /* hysdn_tx_netack */
197 /*****************************************************/
198 /* we got a packet from the network, go and queue it */
199 /*****************************************************/
201 hysdn_rx_netpkt(hysdn_card
* card
, uchar
* buf
, word len
)
203 struct net_local
*lp
= card
->netif
;
207 return; /* non existing device */
209 lp
->stats
.rx_bytes
+= len
;
211 skb
= dev_alloc_skb(len
);
213 printk(KERN_NOTICE
"%s: Memory squeeze, dropping packet.\n",
215 lp
->stats
.rx_dropped
++;
218 skb
->dev
= &lp
->netdev
;
221 memcpy(skb_put(skb
, len
), buf
, len
);
223 /* determine the used protocol */
224 skb
->protocol
= eth_type_trans(skb
, &lp
->netdev
);
227 lp
->stats
.rx_packets
++; /* adjust packet count */
229 } /* hysdn_rx_netpkt */
231 /*****************************************************/
232 /* return the pointer to a network packet to be send */
233 /*****************************************************/
235 hysdn_tx_netget(hysdn_card
* card
)
237 struct net_local
*lp
= card
->netif
;
240 return (NULL
); /* non existing device */
243 return (NULL
); /* nothing available */
245 return (lp
->skbs
[lp
->out_idx
]); /* next packet to send */
246 } /* hysdn_tx_netget */
249 /*******************************************/
250 /* init function called by register device */
251 /*******************************************/
253 net_init(struct net_device
*dev
)
255 /* setup the function table */
256 dev
->open
= net_open
;
257 dev
->stop
= net_close
;
258 dev
->hard_start_xmit
= net_send_packet
;
259 dev
->get_stats
= net_get_stats
;
261 /* Fill in the fields of the device structure with ethernet values. */
264 return (0); /* success */
267 /*****************************************************************************/
268 /* hysdn_net_create creates a new net device for the given card. If a device */
269 /* already exists, it will be deleted and created a new one. The return value */
270 /* 0 announces success, else a negative error code will be returned. */
271 /*****************************************************************************/
273 hysdn_net_create(hysdn_card
* card
)
275 struct net_device
*dev
;
278 printk(KERN_WARNING
"No card-pt in hysdn_net_create!\n");
281 hysdn_net_release(card
); /* release an existing net device */
282 if ((dev
= kmalloc(sizeof(struct net_local
), GFP_KERNEL
)) == NULL
) {
283 printk(KERN_WARNING
"HYSDN: unable to allocate mem\n");
286 memset(dev
, 0, sizeof(struct net_local
)); /* clean the structure */
288 spin_lock_init(&((struct net_local
*) dev
)->lock
);
290 /* initialise necessary or informing fields */
291 dev
->base_addr
= card
->iobase
; /* IO address */
292 dev
->irq
= card
->irq
; /* irq */
293 dev
->init
= net_init
; /* the init function of the device */
295 strcpy(dev
->name
, ((struct net_local
*) dev
)->dev_name
);
297 if ((i
= register_netdev(dev
))) {
298 printk(KERN_WARNING
"HYSDN: unable to create network device\n");
302 dev
->priv
= card
; /* remember pointer to own data structure */
303 card
->netif
= dev
; /* setup the local pointer */
305 if (card
->debug_flags
& LOG_NET_INIT
)
306 hysdn_addlog(card
, "network device created");
307 return (0); /* and return success */
308 } /* hysdn_net_create */
310 /***************************************************************************/
311 /* hysdn_net_release deletes the net device for the given card. The return */
312 /* value 0 announces success, else a negative error code will be returned. */
313 /***************************************************************************/
315 hysdn_net_release(hysdn_card
* card
)
317 struct net_device
*dev
= card
->netif
;
320 return (0); /* non existing */
322 card
->netif
= NULL
; /* clear out pointer */
323 dev
->stop(dev
); /* close the device */
325 flush_tx_buffers((struct net_local
*) dev
); /* empty buffers */
327 unregister_netdev(dev
); /* release the device */
328 free_netdev(dev
); /* release the memory allocated */
329 if (card
->debug_flags
& LOG_NET_INIT
)
330 hysdn_addlog(card
, "network device deleted");
332 return (0); /* always successful */
333 } /* hysdn_net_release */
335 /*****************************************************************************/
336 /* hysdn_net_getname returns a pointer to the name of the network interface. */
337 /* if the interface is not existing, a "-" is returned. */
338 /*****************************************************************************/
340 hysdn_net_getname(hysdn_card
* card
)
342 struct net_device
*dev
= card
->netif
;
345 return ("-"); /* non existing */
348 } /* hysdn_net_getname */