/* * Linux ARCnet driver - device-independent routines * * Written 1997 by David Woodhouse. * Written 1994-1999 by Avery Pennarun. * Written 1999-2000 by Martin Mares <mj@ucw.cz>. * Derived from skeleton.c by Donald Becker. * * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com) * for sponsoring the further development of this driver. * * ********************** * * The original copyright was as follows: * * skeleton.c Written 1993 by Donald Becker. * Copyright 1993 United States Government as represented by the * Director, National Security Agency. This software may only be used * and distributed according to the terms of the GNU General Public License as * modified by SRC, incorporated herein by reference. * * ********************** * * The change log is now in a file called ChangeLog in this directory. * * Sources: * - Crynwr arcnet.com/arcether.com packet drivers. * - arcnet.c v0.00 dated 1/1/94 and apparently by * Donald Becker - it didn't work :) * - skeleton.c v0.05 dated 11/16/93 by Donald Becker * (from Linux Kernel 1.1.45) * - RFC's 1201 and 1051 - re: TCP/IP over ARCnet * - The official ARCnet COM9026 data sheets (!) thanks to * Ken Cornetet <kcornete@nyx10.cs.du.edu> * - The official ARCnet COM20020 data sheets. * - Information on some more obscure ARCnet controller chips, thanks * to the nice people at SMSC. * - net/inet/eth.c (from kernel 1.1.50) for header-building info. * - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su> * - Textual information and more alternate source from Joachim Koenig * <jojo@repas.de>
*/
/* "do nothing" functions for protocol drivers */ staticvoid null_rx(struct net_device *dev, int bufnum, struct archdr *pkthdr, int length); staticint null_build_header(struct sk_buff *skb, struct net_device *dev, unsignedshort type, uint8_t daddr); staticint null_prepare_tx(struct net_device *dev, struct archdr *pkt, int length, int bufnum);
staticvoid arcnet_rx(struct net_device *dev, int bufnum);
/* one ArcProto per possible proto ID. None of the elements of * arc_proto_map are allowed to be NULL; they will get set to * arc_proto_default instead. It also must not be NULL; if you would like * to set it to NULL, set it to &arc_proto_null instead.
*/ struct ArcProto *arc_proto_map[256];
EXPORT_SYMBOL(arc_proto_map);
/* Dump the contents of an ARCnet buffer */ #if (ARCNET_DEBUG_MAX & (D_RX | D_TX)) staticvoid arcnet_dump_packet(struct net_device *dev, int bufnum, char *desc, int take_arcnet_lock)
{ struct arcnet_local *lp = netdev_priv(dev); int i, length; unsignedlong flags = 0; static uint8_t buf[512]; char hdr[32];
/* hw.copy_from_card expects IRQ context so take the IRQ lock * to keep it single threaded
*/ if (take_arcnet_lock)
spin_lock_irqsave(&lp->lock, flags);
lp->hw.copy_from_card(dev, bufnum, 0, buf, 512); if (take_arcnet_lock)
spin_unlock_irqrestore(&lp->lock, flags);
/* if the offset[0] byte is nonzero, this is a 256-byte packet */
length = (buf[2] ? 256 : 512);
/* Register ARCNET LED triggers for a arcnet device * * This is normally called from a driver's probe function
*/ void devm_arcnet_led_init(struct net_device *netdev, int index, int subid)
{ struct arcnet_local *lp = netdev_priv(netdev); void *res;
res = devres_alloc(arcnet_led_release, 0, GFP_KERNEL); if (!res) {
netdev_err(netdev, "cannot register LED triggers\n"); return;
}
/* Unregister a protocol driver from the arc_proto_map. Protocol drivers * are responsible for registering themselves, but the unregister routine * is pretty generic so we'll do it here.
*/ void arcnet_unregister_proto(struct ArcProto *proto)
{ int count;
if (arc_proto_default == proto)
arc_proto_default = &arc_proto_null; if (arc_bcast_proto == proto)
arc_bcast_proto = arc_proto_default; if (arc_raw_proto == proto)
arc_raw_proto = arc_proto_default;
for (count = 0; count < 256; count++) { if (arc_proto_map[count] == proto)
arc_proto_map[count] = arc_proto_default;
}
}
EXPORT_SYMBOL(arcnet_unregister_proto);
/* Add a buffer to the queue. Only the interrupt handler is allowed to do * this, unless interrupts are disabled. * * Note: we don't check for a full queue, since there aren't enough buffers * to more than fill it.
*/ staticvoid release_arcbuf(struct net_device *dev, int bufnum)
{ struct arcnet_local *lp = netdev_priv(dev); int i;
if (BUGLVL(D_DURING)) {
arc_printk(D_DURING, dev, "release_arcbuf: freed #%d; buffer queue is now: ",
bufnum); for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
arc_cont(D_DURING, "\n");
}
}
/* Get a buffer from the queue. * If this returns -1, there are no buffers available.
*/ staticint get_arcbuf(struct net_device *dev)
{ struct arcnet_local *lp = netdev_priv(dev); int buf = -1, i;
if (!atomic_dec_and_test(&lp->buf_lock)) { /* already in this function */
arc_printk(D_NORMAL, dev, "get_arcbuf: overlap (%d)!\n",
lp->buf_lock.counter);
} else { /* we can continue */ if (lp->next_buf >= 5)
lp->next_buf -= 5;
if (lp->next_buf == lp->first_free_buf) {
arc_printk(D_NORMAL, dev, "get_arcbuf: BUG: no buffers are available??\n");
} else {
buf = lp->buf_queue[lp->next_buf++];
lp->next_buf %= 5;
}
}
if (BUGLVL(D_DURING)) {
arc_printk(D_DURING, dev, "get_arcbuf: got #%d; buffer queue is now: ",
buf); for (i = lp->next_buf; i != lp->first_free_buf; i = (i + 1) % 5)
arc_cont(D_DURING, "#%d ", lp->buf_queue[i]);
arc_cont(D_DURING, "\n");
}
atomic_inc(&lp->buf_lock); return buf;
}
staticint choose_mtu(void)
{ int count, mtu = 65535;
/* choose the smallest MTU of all available encaps */ for (count = 0; count < 256; count++) { if (arc_proto_map[count] != &arc_proto_null &&
arc_proto_map[count]->mtu < mtu) {
mtu = arc_proto_map[count]->mtu;
}
}
lp = container_of(work, struct arcnet_local, reset_work);
dev = lp->dev;
/* Do not bring the network interface back up if an ifdown * was already done.
*/ if (!netif_running(dev) || !lp->reset_in_progress) return;
rtnl_lock();
/* Do another check, in case of an ifdown that was triggered in * the small race window between the exit condition above and * acquiring RTNL.
*/ if (!netif_running(dev) || !lp->reset_in_progress) goto out;
/* Do not cancel this at ->ndo_close(), as the workqueue itself * indirectly calls the ifdown path through dev_close().
*/
cancel_work_sync(&lp->reset_work);
free_netdev(dev);
}
EXPORT_SYMBOL(free_arcdev);
/* Open/initialize the board. This is called sometime after booting when * the 'ifconfig' program is run. * * This routine should set everything up anew at each open, even registers * that "should" only need to be set once at boot, so that there is * non-reboot way to recover if something goes wrong.
*/ int arcnet_open(struct net_device *dev)
{ struct arcnet_local *lp = netdev_priv(dev); int count, newmtu, error;
arc_printk(D_INIT, dev, "opened.");
if (!try_module_get(lp->hw.owner)) return -ENODEV;
if (BUGLVL(D_PROTO)) {
arc_printk(D_PROTO, dev, "protocol map (default is '%c'): ",
arc_proto_default->suffix); for (count = 0; count < 256; count++)
arc_cont(D_PROTO, "%c", arc_proto_map[count]->suffix);
arc_cont(D_PROTO, "\n");
}
/* try to put the card in a defined state - if it fails the first * time, actually reset it.
*/
error = -ENODEV; if (lp->hw.reset(dev, 0) && lp->hw.reset(dev, 1)) goto out_module_put;
newmtu = choose_mtu(); if (newmtu < dev->mtu)
dev->mtu = newmtu;
/* autodetect the encapsulation for each host. */
memset(lp->default_proto, 0, sizeof(lp->default_proto));
/* the broadcast address is special - use the 'bcast' protocol */ for (count = 0; count < 256; count++) { if (arc_proto_map[count] == arc_bcast_proto) {
lp->default_proto[0] = count; break;
}
}
/* bring up the hardware driver */ if (lp->hw.open)
lp->hw.open(dev);
if (dev->dev_addr[0] == 0)
arc_printk(D_NORMAL, dev, "WARNING! Station address 00 is reserved for broadcasts!\n"); elseif (dev->dev_addr[0] == 255)
arc_printk(D_NORMAL, dev, "WARNING! Station address FF may confuse DOS networking programs!\n");
arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); /* make sure we're ready to receive IRQ's. */
lp->hw.intmask(dev, 0);
udelay(1); /* give it time to set the mask before * we reset it again. (may not even be * necessary)
*/
arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
lp->intmask = NORXflag | RECONflag;
lp->hw.intmask(dev, lp->intmask);
arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
if (skb->len != 0 && len != skb->len)
arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! skb->len(%d) != len(%d)!\n",
skb->len, len);
/* Type is host order - ? */ if (type == ETH_P_ARCNET) {
proto = arc_raw_proto;
arc_printk(D_DEBUG, dev, "arc_raw_proto used. proto='%c'\n",
proto->suffix);
_daddr = daddr ? *(uint8_t *)daddr : 0;
} elseif (!daddr) { /* if the dest addr isn't provided, we can't choose an * encapsulation! Store the packet type (eg. ETH_P_IP) * for now, and we'll push on a real header when we do * rebuild_header.
*/
*(uint16_t *)skb_push(skb, 2) = type; /* XXX: Why not use skb->mac_len? */ if (skb->network_header - skb->mac_header != 2)
arc_printk(D_NORMAL, dev, "arcnet_header: Yikes! diff (%u) is not 2!\n",
skb->network_header - skb->mac_header); return -2; /* return error -- can't transmit yet! */
} else { /* otherwise, we can just add the header as usual. */
_daddr = *(uint8_t *)daddr;
proto_num = lp->default_proto[_daddr];
proto = arc_proto_map[proto_num];
arc_printk(D_DURING, dev, "building header for %02Xh using protocol '%c'\n",
proto_num, proto->suffix); if (proto == &arc_proto_null && arc_bcast_proto != proto) {
arc_printk(D_DURING, dev, "actually, let's use '%c' instead.\n",
arc_bcast_proto->suffix);
proto = arc_bcast_proto;
}
} return proto->build_header(skb, dev, type, _daddr);
}
/* Called by the kernel in order to transmit a packet. */
netdev_tx_t arcnet_send_packet(struct sk_buff *skb, struct net_device *dev)
{ struct arcnet_local *lp = netdev_priv(dev); struct archdr *pkt; struct arc_rfc1201 *soft; struct ArcProto *proto; int txbuf; unsignedlong flags; int retval;
if (txbuf != -1) {
lp->outgoing.skb = skb; if (proto->prepare_tx(dev, pkt, skb->len, txbuf) &&
!proto->ack_tx) { /* done right away and we don't want to acknowledge * the package later - forget about it now
*/
dev->stats.tx_bytes += skb->len;
} else { /* do it the 'split' way */
lp->outgoing.proto = proto;
lp->outgoing.skb = skb;
lp->outgoing.pkt = pkt;
arc_printk(D_DEBUG, dev, "%s: %d: %s, status: %x\n",
__FILE__, __LINE__, __func__, lp->hw.status(dev)); /* make sure we didn't ignore a TX IRQ while we were in here */
lp->hw.intmask(dev, 0);
spin_unlock_irqrestore(&lp->lock, flags); return retval; /* no need to try again */
}
EXPORT_SYMBOL(arcnet_send_packet);
/* Actually start transmitting a packet that was loaded into a buffer * by prepare_tx. This should _only_ be called by the interrupt handler.
*/ staticint go_tx(struct net_device *dev)
{ struct arcnet_local *lp = netdev_priv(dev);
/* Called by the kernel when transmit times out */ void arcnet_timeout(struct net_device *dev, unsignedint txqueue)
{ unsignedlong flags; struct arcnet_local *lp = netdev_priv(dev); int status = lp->hw.status(dev); char *msg;
if (lp->cur_tx == -1)
netif_wake_queue(dev);
}
EXPORT_SYMBOL(arcnet_timeout);
/* The typical workload of the driver: Handle the network interface * interrupts. Establish which device needs attention, and call the correct * chipset interrupt handler.
*/
irqreturn_t arcnet_interrupt(int irq, void *dev_id)
{ struct net_device *dev = dev_id; struct arcnet_local *lp; int recbuf, status, diagstatus, didsomething, boguscount; unsignedlong flags; int retval = IRQ_NONE;
arc_printk(D_DURING, dev, "\n");
arc_printk(D_DURING, dev, "in arcnet_interrupt\n");
lp = netdev_priv(dev);
BUG_ON(!lp);
spin_lock_irqsave(&lp->lock, flags);
if (lp->reset_in_progress) goto out;
/* RESET flag was enabled - if device is not running, we must * clear it right away (but nothing else).
*/ if (!netif_running(dev)) { if (lp->hw.status(dev) & RESETflag)
lp->hw.command(dev, CFLAGScmd | RESETclear);
lp->hw.intmask(dev, 0);
spin_unlock_irqrestore(&lp->lock, flags); return retval;
}
arc_printk(D_DURING, dev, "in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
lp->hw.status(dev), lp->intmask);
boguscount = 5; do {
status = lp->hw.status(dev);
diagstatus = (status >> 8) & 0xFF;
/* RESET flag was enabled - card is resetting and if RX is * disabled, it's NOT because we just got a packet. * * The card is in an undefined state. * Clear it out and start over.
*/ if (status & RESETflag) {
arc_printk(D_NORMAL, dev, "spurious reset (status=%Xh)\n",
status);
/* get out of the interrupt handler! */ goto out;
} /* RX is inhibited - we must have received something. * Prepare to receive into the next buffer. * * We don't actually copy the received packet from the card * until after the transmit handler runs (and possibly * launches the next tx); this should improve latency slightly * if we get both types of interrupts at once.
*/
recbuf = -1; if (status & lp->intmask & NORXflag) {
recbuf = lp->cur_rx;
arc_printk(D_DURING, dev, "Buffer #%d: receive irq (status=%Xh)\n",
recbuf, status);
/* a transmit finished, and we're interested in it. */ if ((status & lp->intmask & TXFREEflag) || lp->timed_out) { int ackstatus;
lp->intmask &= ~(TXFREEflag | EXCNAKflag);
/* send another packet if there is one */
go_tx(dev);
/* continue a split packet, if any */ if (lp->outgoing.proto &&
lp->outgoing.proto->continue_tx) { int txbuf = get_arcbuf(dev);
if (txbuf != -1) { if (lp->outgoing.proto->continue_tx(dev, txbuf)) { /* that was the last segment */
dev->stats.tx_bytes += lp->outgoing.skb->len; if (!lp->outgoing.proto->ack_tx) {
dev_kfree_skb_irq(lp->outgoing.skb);
lp->outgoing.proto = NULL;
}
}
lp->next_tx = txbuf;
}
} /* inform upper layers of idleness, if necessary */ if (lp->cur_tx == -1)
netif_wake_queue(dev);
} /* now process the received packet, if any */ if (recbuf != -1) { if (BUGLVL(D_RX))
arcnet_dump_packet(dev, recbuf, "rx irq", 0);
arcnet_led_event(dev, ARCNET_LED_EVENT_RECON); /* MYRECON bit is at bit 7 of diagstatus */ if (diagstatus & 0x80)
arc_printk(D_RECON, dev, "Put out that recon myself\n");
/* is the RECON info empty or old? */ if (!lp->first_recon || !lp->last_recon ||
time_after(jiffies, lp->last_recon + HZ * 10)) { if (lp->network_down)
arc_printk(D_NORMAL, dev, "reconfiguration detected: cabling restored?\n");
lp->first_recon = lp->last_recon = jiffies;
lp->num_recons = lp->network_down = 0;
arc_printk(D_DURING, dev, "recon: clearing counters.\n");
} else { /* add to current RECON counter */
lp->last_recon = jiffies;
lp->num_recons++;
/* This is a generic packet receiver that calls arcnet??_rx depending on the * protocol ID found.
*/ staticvoid arcnet_rx(struct net_device *dev, int bufnum)
{ struct arcnet_local *lp = netdev_priv(dev); union { struct archdr pkt; char buf[512];
} rxdata; struct arc_rfc1201 *soft; int length, ofs;
/* get the full header, if possible */ if (sizeof(rxdata.pkt.soft) <= length) {
lp->hw.copy_from_card(dev, bufnum, ofs, soft, sizeof(rxdata.pkt.soft));
} else {
memset(&rxdata.pkt.soft, 0, sizeof(rxdata.pkt.soft));
lp->hw.copy_from_card(dev, bufnum, ofs, soft, length);
}
arc_printk(D_DURING, dev, "Buffer #%d: received packet from %02Xh to %02Xh (%d+4 bytes)\n",
bufnum, rxdata.pkt.hard.source, rxdata.pkt.hard.dest, length);
/* call the right receiver for the protocol */ if (arc_proto_map[soft->proto]->is_ip) { if (BUGLVL(D_PROTO)) { struct ArcProto
*oldp = arc_proto_map[lp->default_proto[rxdata.pkt.hard.source]],
*newp = arc_proto_map[soft->proto];
if (oldp != newp) {
arc_printk(D_PROTO, dev, "got protocol %02Xh; encap for host %02Xh is now '%c' (was '%c')\n",
soft->proto, rxdata.pkt.hard.source,
newp->suffix, oldp->suffix);
}
}
/* broadcasts will always be done with the last-used encap. */
lp->default_proto[0] = soft->proto;
/* in striking contrast, the following isn't a hack. */
lp->default_proto[rxdata.pkt.hard.source] = soft->proto;
} /* call the protocol-specific receiver. */
arc_proto_map[soft->proto]->rx(dev, bufnum, &rxdata.pkt, length);
}
staticvoid null_rx(struct net_device *dev, int bufnum, struct archdr *pkthdr, int length)
{
arc_printk(D_PROTO, dev, "rx: don't know how to deal with proto %02Xh from host %02Xh.\n",
pkthdr->soft.rfc1201.proto, pkthdr->hard.source);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.