/* $Id: hysdn_net.c,v 1.8.6.4 2001/09/23 22:24:54 kai Exp $1*2* Linux driver for HYSDN cards, net (ethernet type) handling routines.3*4* Author Werner Cornelius ([email protected]) for Hypercope GmbH5* Copyright 1999 by Werner Cornelius ([email protected])6*7* This software may be used and distributed according to the terms8* of the GNU General Public License, incorporated herein by reference.9*10* This net module has been inspired by the skeleton driver from11* Donald Becker ([email protected])12*13*/1415#include <linux/module.h>16#include <linux/signal.h>17#include <linux/kernel.h>18#include <linux/netdevice.h>19#include <linux/etherdevice.h>20#include <linux/skbuff.h>21#include <linux/inetdevice.h>2223#include "hysdn_defs.h"2425unsigned int hynet_enable = 0xffffffff;26module_param(hynet_enable, uint, 0);2728#define MAX_SKB_BUFFERS 20 /* number of buffers for keeping TX-data */2930/****************************************************************************/31/* structure containing the complete network data. The structure is aligned */32/* in a way that both, the device and statistics are kept inside it. */33/* for proper access, the device structure MUST be the first var/struct */34/* inside the definition. */35/****************************************************************************/36struct net_local {37/* Tx control lock. This protects the transmit buffer ring38* state along with the "tx full" state of the driver. This39* means all netif_queue flow control actions are protected40* by this lock as well.41*/42struct net_device *dev;43spinlock_t lock;44struct sk_buff *skbs[MAX_SKB_BUFFERS]; /* pointers to tx-skbs */45int in_idx, out_idx; /* indexes to buffer ring */46int sk_count; /* number of buffers currently in ring */47}; /* net_local */48495051/*********************************************************************/52/* Open/initialize the board. This is called (in the current kernel) */53/* sometime after booting when the 'ifconfig' program is run. */54/* This routine should set everything up anew at each open, even */55/* registers that "should" only need to be set once at boot, so that */56/* there is non-reboot way to recover if something goes wrong. */57/*********************************************************************/58static int59net_open(struct net_device *dev)60{61struct in_device *in_dev;62hysdn_card *card = dev->ml_priv;63int i;6465netif_start_queue(dev); /* start tx-queueing */6667/* Fill in the MAC-level header (if not already set) */68if (!card->mac_addr[0]) {69for (i = 0; i < ETH_ALEN; i++)70dev->dev_addr[i] = 0xfc;71if ((in_dev = dev->ip_ptr) != NULL) {72struct in_ifaddr *ifa = in_dev->ifa_list;73if (ifa != NULL)74memcpy(dev->dev_addr + (ETH_ALEN - sizeof(ifa->ifa_local)), &ifa->ifa_local, sizeof(ifa->ifa_local));75}76} else77memcpy(dev->dev_addr, card->mac_addr, ETH_ALEN);7879return (0);80} /* net_open */8182/*******************************************/83/* flush the currently occupied tx-buffers */84/* must only be called when device closed */85/*******************************************/86static void87flush_tx_buffers(struct net_local *nl)88{8990while (nl->sk_count) {91dev_kfree_skb(nl->skbs[nl->out_idx++]); /* free skb */92if (nl->out_idx >= MAX_SKB_BUFFERS)93nl->out_idx = 0; /* wrap around */94nl->sk_count--;95}96} /* flush_tx_buffers */979899/*********************************************************************/100/* close/decativate the device. The device is not removed, but only */101/* deactivated. */102/*********************************************************************/103static int104net_close(struct net_device *dev)105{106107netif_stop_queue(dev); /* disable queueing */108109flush_tx_buffers((struct net_local *) dev);110111return (0); /* success */112} /* net_close */113114/************************************/115/* send a packet on this interface. */116/* new style for kernel >= 2.3.33 */117/************************************/118static netdev_tx_t119net_send_packet(struct sk_buff *skb, struct net_device *dev)120{121struct net_local *lp = (struct net_local *) dev;122123spin_lock_irq(&lp->lock);124125lp->skbs[lp->in_idx++] = skb; /* add to buffer list */126if (lp->in_idx >= MAX_SKB_BUFFERS)127lp->in_idx = 0; /* wrap around */128lp->sk_count++; /* adjust counter */129dev->trans_start = jiffies;130131/* If we just used up the very last entry in the132* TX ring on this device, tell the queueing133* layer to send no more.134*/135if (lp->sk_count >= MAX_SKB_BUFFERS)136netif_stop_queue(dev);137138/* When the TX completion hw interrupt arrives, this139* is when the transmit statistics are updated.140*/141142spin_unlock_irq(&lp->lock);143144if (lp->sk_count <= 3) {145schedule_work(&((hysdn_card *) dev->ml_priv)->irq_queue);146}147return NETDEV_TX_OK; /* success */148} /* net_send_packet */149150151152/***********************************************************************/153/* acknowlegde a packet send. The network layer will be informed about */154/* completion */155/***********************************************************************/156void157hysdn_tx_netack(hysdn_card * card)158{159struct net_local *lp = card->netif;160161if (!lp)162return; /* non existing device */163164165if (!lp->sk_count)166return; /* error condition */167168lp->dev->stats.tx_packets++;169lp->dev->stats.tx_bytes += lp->skbs[lp->out_idx]->len;170171dev_kfree_skb(lp->skbs[lp->out_idx++]); /* free skb */172if (lp->out_idx >= MAX_SKB_BUFFERS)173lp->out_idx = 0; /* wrap around */174175if (lp->sk_count-- == MAX_SKB_BUFFERS) /* dec usage count */176netif_start_queue((struct net_device *) lp);177} /* hysdn_tx_netack */178179/*****************************************************/180/* we got a packet from the network, go and queue it */181/*****************************************************/182void183hysdn_rx_netpkt(hysdn_card * card, unsigned char *buf, unsigned short len)184{185struct net_local *lp = card->netif;186struct net_device *dev;187struct sk_buff *skb;188189if (!lp)190return; /* non existing device */191192dev = lp->dev;193dev->stats.rx_bytes += len;194195skb = dev_alloc_skb(len);196if (skb == NULL) {197printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",198dev->name);199dev->stats.rx_dropped++;200return;201}202/* copy the data */203memcpy(skb_put(skb, len), buf, len);204205/* determine the used protocol */206skb->protocol = eth_type_trans(skb, dev);207208dev->stats.rx_packets++; /* adjust packet count */209210netif_rx(skb);211} /* hysdn_rx_netpkt */212213/*****************************************************/214/* return the pointer to a network packet to be send */215/*****************************************************/216struct sk_buff *217hysdn_tx_netget(hysdn_card * card)218{219struct net_local *lp = card->netif;220221if (!lp)222return (NULL); /* non existing device */223224if (!lp->sk_count)225return (NULL); /* nothing available */226227return (lp->skbs[lp->out_idx]); /* next packet to send */228} /* hysdn_tx_netget */229230static const struct net_device_ops hysdn_netdev_ops = {231.ndo_open = net_open,232.ndo_stop = net_close,233.ndo_start_xmit = net_send_packet,234.ndo_change_mtu = eth_change_mtu,235.ndo_set_mac_address = eth_mac_addr,236.ndo_validate_addr = eth_validate_addr,237};238239240/*****************************************************************************/241/* hysdn_net_create creates a new net device for the given card. If a device */242/* already exists, it will be deleted and created a new one. The return value */243/* 0 announces success, else a negative error code will be returned. */244/*****************************************************************************/245int246hysdn_net_create(hysdn_card * card)247{248struct net_device *dev;249int i;250struct net_local *lp;251252if(!card) {253printk(KERN_WARNING "No card-pt in hysdn_net_create!\n");254return (-ENOMEM);255}256hysdn_net_release(card); /* release an existing net device */257258dev = alloc_etherdev(sizeof(struct net_local));259if (!dev) {260printk(KERN_WARNING "HYSDN: unable to allocate mem\n");261return (-ENOMEM);262}263264lp = netdev_priv(dev);265lp->dev = dev;266267dev->netdev_ops = &hysdn_netdev_ops;268spin_lock_init(&((struct net_local *) dev)->lock);269270/* initialise necessary or informing fields */271dev->base_addr = card->iobase; /* IO address */272dev->irq = card->irq; /* irq */273274dev->netdev_ops = &hysdn_netdev_ops;275if ((i = register_netdev(dev))) {276printk(KERN_WARNING "HYSDN: unable to create network device\n");277free_netdev(dev);278return (i);279}280dev->ml_priv = card; /* remember pointer to own data structure */281card->netif = dev; /* setup the local pointer */282283if (card->debug_flags & LOG_NET_INIT)284hysdn_addlog(card, "network device created");285return (0); /* and return success */286} /* hysdn_net_create */287288/***************************************************************************/289/* hysdn_net_release deletes the net device for the given card. The return */290/* value 0 announces success, else a negative error code will be returned. */291/***************************************************************************/292int293hysdn_net_release(hysdn_card * card)294{295struct net_device *dev = card->netif;296297if (!dev)298return (0); /* non existing */299300card->netif = NULL; /* clear out pointer */301net_close(dev);302303flush_tx_buffers((struct net_local *) dev); /* empty buffers */304305unregister_netdev(dev); /* release the device */306free_netdev(dev); /* release the memory allocated */307if (card->debug_flags & LOG_NET_INIT)308hysdn_addlog(card, "network device deleted");309310return (0); /* always successful */311} /* hysdn_net_release */312313/*****************************************************************************/314/* hysdn_net_getname returns a pointer to the name of the network interface. */315/* if the interface is not existing, a "-" is returned. */316/*****************************************************************************/317char *318hysdn_net_getname(hysdn_card * card)319{320struct net_device *dev = card->netif;321322if (!dev)323return ("-"); /* non existing */324325return (dev->name);326} /* hysdn_net_getname */327328329