blob: 579974cf4c9a0b79eba54b564c66aa17337fdad3 [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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 /* Tx control lock. This protects the transmit buffer ring
42 * state along with the "tx full" state of the driver. This
43 * means all netif_queue flow control actions are protected
44 * by this lock as well.
45 */
Stephen Hemminger0e2445f2009-01-07 18:03:43 -080046 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 spinlock_t lock;
48 struct sk_buff *skbs[MAX_SKB_BUFFERS]; /* pointers to tx-skbs */
49 int in_idx, out_idx; /* indexes to buffer ring */
50 int sk_count; /* number of buffers currently in ring */
51}; /* net_local */
52
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*********************************************************************/
56/* Open/initialize the board. This is called (in the current kernel) */
57/* sometime after booting when the 'ifconfig' program is run. */
58/* This routine should set everything up anew at each open, even */
59/* registers that "should" only need to be set once at boot, so that */
60/* there is non-reboot way to recover if something goes wrong. */
61/*********************************************************************/
62static int
63net_open(struct net_device *dev)
64{
65 struct in_device *in_dev;
Wang Chen25dd7e62008-12-03 15:49:07 -080066 hysdn_card *card = dev->ml_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int i;
68
69 netif_start_queue(dev); /* start tx-queueing */
70
71 /* Fill in the MAC-level header (if not already set) */
72 if (!card->mac_addr[0]) {
Pascal Terjanbd091412008-12-01 12:24:25 +000073 for (i = 0; i < ETH_ALEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 dev->dev_addr[i] = 0xfc;
75 if ((in_dev = dev->ip_ptr) != NULL) {
76 struct in_ifaddr *ifa = in_dev->ifa_list;
77 if (ifa != NULL)
Pascal Terjanbd091412008-12-01 12:24:25 +000078 memcpy(dev->dev_addr + (ETH_ALEN - sizeof(ifa->ifa_local)), &ifa->ifa_local, sizeof(ifa->ifa_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
80 } else
81 memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);
82
83 return (0);
84} /* net_open */
85
86/*******************************************/
87/* flush the currently occupied tx-buffers */
88/* must only be called when device closed */
89/*******************************************/
90static void
91flush_tx_buffers(struct net_local *nl)
92{
93
94 while (nl->sk_count) {
95 dev_kfree_skb(nl->skbs[nl->out_idx++]); /* free skb */
96 if (nl->out_idx >= MAX_SKB_BUFFERS)
97 nl->out_idx = 0; /* wrap around */
98 nl->sk_count--;
99 }
100} /* flush_tx_buffers */
101
102
103/*********************************************************************/
104/* close/decativate the device. The device is not removed, but only */
105/* deactivated. */
106/*********************************************************************/
107static int
108net_close(struct net_device *dev)
109{
110
111 netif_stop_queue(dev); /* disable queueing */
112
113 flush_tx_buffers((struct net_local *) dev);
114
115 return (0); /* success */
116} /* net_close */
117
118/************************************/
119/* send a packet on this interface. */
120/* new style for kernel >= 2.3.33 */
121/************************************/
122static int
123net_send_packet(struct sk_buff *skb, struct net_device *dev)
124{
125 struct net_local *lp = (struct net_local *) dev;
126
127 spin_lock_irq(&lp->lock);
128
129 lp->skbs[lp->in_idx++] = skb; /* add to buffer list */
130 if (lp->in_idx >= MAX_SKB_BUFFERS)
131 lp->in_idx = 0; /* wrap around */
132 lp->sk_count++; /* adjust counter */
133 dev->trans_start = jiffies;
134
135 /* If we just used up the very last entry in the
136 * TX ring on this device, tell the queueing
137 * layer to send no more.
138 */
139 if (lp->sk_count >= MAX_SKB_BUFFERS)
140 netif_stop_queue(dev);
141
142 /* When the TX completion hw interrupt arrives, this
143 * is when the transmit statistics are updated.
144 */
145
146 spin_unlock_irq(&lp->lock);
147
148 if (lp->sk_count <= 3) {
Wang Chen25dd7e62008-12-03 15:49:07 -0800149 schedule_work(&((hysdn_card *) dev->ml_priv)->irq_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 return (0); /* success */
152} /* net_send_packet */
153
154
155
156/***********************************************************************/
157/* acknowlegde a packet send. The network layer will be informed about */
158/* completion */
159/***********************************************************************/
160void
161hysdn_tx_netack(hysdn_card * card)
162{
163 struct net_local *lp = card->netif;
164
165 if (!lp)
166 return; /* non existing device */
167
168
169 if (!lp->sk_count)
170 return; /* error condition */
171
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800172 lp->dev->stats.tx_packets++;
173 lp->dev->stats.tx_bytes += lp->skbs[lp->out_idx]->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 dev_kfree_skb(lp->skbs[lp->out_idx++]); /* free skb */
176 if (lp->out_idx >= MAX_SKB_BUFFERS)
177 lp->out_idx = 0; /* wrap around */
178
179 if (lp->sk_count-- == MAX_SKB_BUFFERS) /* dec usage count */
180 netif_start_queue((struct net_device *) lp);
181} /* hysdn_tx_netack */
182
183/*****************************************************/
184/* we got a packet from the network, go and queue it */
185/*****************************************************/
186void
Andrew Mortonc721bcc2006-03-25 03:07:04 -0800187hysdn_rx_netpkt(hysdn_card * card, unsigned char *buf, unsigned short len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct net_local *lp = card->netif;
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800190 struct net_device *dev = lp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct sk_buff *skb;
192
193 if (!lp)
194 return; /* non existing device */
195
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800196 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 skb = dev_alloc_skb(len);
199 if (skb == NULL) {
200 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800201 dev->name);
202 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* copy the data */
206 memcpy(skb_put(skb, len), buf, len);
207
208 /* determine the used protocol */
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800209 skb->protocol = eth_type_trans(skb, dev);
210
211 dev->stats.rx_packets++; /* adjust packet count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214} /* hysdn_rx_netpkt */
215
216/*****************************************************/
217/* return the pointer to a network packet to be send */
218/*****************************************************/
219struct sk_buff *
220hysdn_tx_netget(hysdn_card * card)
221{
222 struct net_local *lp = card->netif;
223
224 if (!lp)
225 return (NULL); /* non existing device */
226
227 if (!lp->sk_count)
228 return (NULL); /* nothing available */
229
230 return (lp->skbs[lp->out_idx]); /* next packet to send */
231} /* hysdn_tx_netget */
232
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800233static const struct net_device_ops hysdn_netdev_ops = {
234 .ndo_open = net_open,
235 .ndo_stop = net_close,
236 .ndo_start_xmit = net_send_packet,
237 .ndo_change_mtu = eth_change_mtu,
238 .ndo_set_mac_address = eth_mac_addr,
239 .ndo_validate_addr = eth_validate_addr,
240};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/*****************************************************************************/
244/* hysdn_net_create creates a new net device for the given card. If a device */
245/* already exists, it will be deleted and created a new one. The return value */
246/* 0 announces success, else a negative error code will be returned. */
247/*****************************************************************************/
248int
249hysdn_net_create(hysdn_card * card)
250{
251 struct net_device *dev;
252 int i;
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800253 struct net_local *lp;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if(!card) {
256 printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");
257 return (-ENOMEM);
258 }
259 hysdn_net_release(card); /* release an existing net device */
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800260
261 dev = alloc_etherdev(sizeof(struct net_local));
262 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 printk(KERN_WARNING "HYSDN: unable to allocate mem\n");
264 return (-ENOMEM);
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800267 lp = netdev_priv(dev);
268 lp->dev = dev;
269
270 dev->netdev_ops = &hysdn_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 spin_lock_init(&((struct net_local *) dev)->lock);
272
273 /* initialise necessary or informing fields */
274 dev->base_addr = card->iobase; /* IO address */
275 dev->irq = card->irq; /* irq */
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800276
277 dev->netdev_ops = &hysdn_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if ((i = register_netdev(dev))) {
279 printk(KERN_WARNING "HYSDN: unable to create network device\n");
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800280 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return (i);
282 }
Wang Chen25dd7e62008-12-03 15:49:07 -0800283 dev->ml_priv = card; /* remember pointer to own data structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 card->netif = dev; /* setup the local pointer */
285
286 if (card->debug_flags & LOG_NET_INIT)
287 hysdn_addlog(card, "network device created");
288 return (0); /* and return success */
289} /* hysdn_net_create */
290
291/***************************************************************************/
292/* hysdn_net_release deletes the net device for the given card. The return */
293/* value 0 announces success, else a negative error code will be returned. */
294/***************************************************************************/
295int
296hysdn_net_release(hysdn_card * card)
297{
298 struct net_device *dev = card->netif;
299
300 if (!dev)
301 return (0); /* non existing */
302
303 card->netif = NULL; /* clear out pointer */
Stephen Hemminger0e2445f2009-01-07 18:03:43 -0800304 net_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 flush_tx_buffers((struct net_local *) dev); /* empty buffers */
307
308 unregister_netdev(dev); /* release the device */
309 free_netdev(dev); /* release the memory allocated */
310 if (card->debug_flags & LOG_NET_INIT)
311 hysdn_addlog(card, "network device deleted");
312
313 return (0); /* always successful */
314} /* hysdn_net_release */
315
316/*****************************************************************************/
317/* hysdn_net_getname returns a pointer to the name of the network interface. */
318/* if the interface is not existing, a "-" is returned. */
319/*****************************************************************************/
320char *
321hysdn_net_getname(hysdn_card * card)
322{
323 struct net_device *dev = card->netif;
324
325 if (!dev)
326 return ("-"); /* non existing */
327
328 return (dev->name);
329} /* hysdn_net_getname */