162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Linux ARCnet driver - "raw mode" packet encapsulation (no soft headers)
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Written 1994-1999 by Avery Pennarun.
562306a36Sopenharmony_ci * Derived from skeleton.c by Donald Becker.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
862306a36Sopenharmony_ci *  for sponsoring the further development of this driver.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * **********************
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * The original copyright of skeleton.c was as follows:
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * skeleton.c Written 1993 by Donald Becker.
1562306a36Sopenharmony_ci * Copyright 1993 United States Government as represented by the
1662306a36Sopenharmony_ci * Director, National Security Agency.  This software may only be used
1762306a36Sopenharmony_ci * and distributed according to the terms of the GNU General Public License as
1862306a36Sopenharmony_ci * modified by SRC, incorporated herein by reference.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * **********************
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * For more details, see drivers/net/arcnet.c
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * **********************
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <linux/module.h>
3062306a36Sopenharmony_ci#include <linux/gfp.h>
3162306a36Sopenharmony_ci#include <linux/init.h>
3262306a36Sopenharmony_ci#include <linux/if_arp.h>
3362306a36Sopenharmony_ci#include <net/arp.h>
3462306a36Sopenharmony_ci#include <linux/netdevice.h>
3562306a36Sopenharmony_ci#include <linux/skbuff.h>
3662306a36Sopenharmony_ci#include "arcdevice.h"
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* packet receiver */
3962306a36Sopenharmony_cistatic void rx(struct net_device *dev, int bufnum,
4062306a36Sopenharmony_ci	       struct archdr *pkthdr, int length)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
4362306a36Sopenharmony_ci	struct sk_buff *skb;
4462306a36Sopenharmony_ci	struct archdr *pkt = pkthdr;
4562306a36Sopenharmony_ci	int ofs;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	arc_printk(D_DURING, dev, "it's a raw packet (length=%d)\n", length);
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	if (length > MTU)
5062306a36Sopenharmony_ci		ofs = 512 - length;
5162306a36Sopenharmony_ci	else
5262306a36Sopenharmony_ci		ofs = 256 - length;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
5562306a36Sopenharmony_ci	if (!skb) {
5662306a36Sopenharmony_ci		dev->stats.rx_dropped++;
5762306a36Sopenharmony_ci		return;
5862306a36Sopenharmony_ci	}
5962306a36Sopenharmony_ci	skb_put(skb, length + ARC_HDR_SIZE);
6062306a36Sopenharmony_ci	skb->dev = dev;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	pkt = (struct archdr *)skb->data;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	skb_reset_mac_header(skb);
6562306a36Sopenharmony_ci	skb_pull(skb, ARC_HDR_SIZE);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	/* up to sizeof(pkt->soft) has already been copied from the card */
6862306a36Sopenharmony_ci	memcpy(pkt, pkthdr, sizeof(struct archdr));
6962306a36Sopenharmony_ci	if (length > sizeof(pkt->soft))
7062306a36Sopenharmony_ci		lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
7162306a36Sopenharmony_ci				      pkt->soft.raw + sizeof(pkt->soft),
7262306a36Sopenharmony_ci				      length - sizeof(pkt->soft));
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	if (BUGLVL(D_SKB))
7562306a36Sopenharmony_ci		arcnet_dump_skb(dev, skb, "rx");
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	skb->protocol = cpu_to_be16(ETH_P_ARCNET);
7862306a36Sopenharmony_ci	netif_rx(skb);
7962306a36Sopenharmony_ci}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/* Create the ARCnet hard/soft headers for raw mode.
8262306a36Sopenharmony_ci * There aren't any soft headers in raw mode - not even the protocol id.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_cistatic int build_header(struct sk_buff *skb, struct net_device *dev,
8562306a36Sopenharmony_ci			unsigned short type, uint8_t daddr)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	int hdr_size = ARC_HDR_SIZE;
8862306a36Sopenharmony_ci	struct archdr *pkt = skb_push(skb, hdr_size);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	/* Set the source hardware address.
9162306a36Sopenharmony_ci	 *
9262306a36Sopenharmony_ci	 * This is pretty pointless for most purposes, but it can help in
9362306a36Sopenharmony_ci	 * debugging.  ARCnet does not allow us to change the source address
9462306a36Sopenharmony_ci	 * in the actual packet sent.
9562306a36Sopenharmony_ci	 */
9662306a36Sopenharmony_ci	pkt->hard.source = *dev->dev_addr;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	/* see linux/net/ethernet/eth.c to see where I got the following */
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
10162306a36Sopenharmony_ci		/* FIXME: fill in the last byte of the dest ipaddr here
10262306a36Sopenharmony_ci		 * to better comply with RFC1051 in "noarp" mode.
10362306a36Sopenharmony_ci		 */
10462306a36Sopenharmony_ci		pkt->hard.dest = 0;
10562306a36Sopenharmony_ci		return hdr_size;
10662306a36Sopenharmony_ci	}
10762306a36Sopenharmony_ci	/* otherwise, just fill it in and go! */
10862306a36Sopenharmony_ci	pkt->hard.dest = daddr;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	return hdr_size;	/* success */
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
11462306a36Sopenharmony_ci		      int bufnum)
11562306a36Sopenharmony_ci{
11662306a36Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
11762306a36Sopenharmony_ci	struct arc_hardware *hard = &pkt->hard;
11862306a36Sopenharmony_ci	int ofs;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
12162306a36Sopenharmony_ci		   lp->next_tx, lp->cur_tx, bufnum);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	/* hard header is not included in packet length */
12462306a36Sopenharmony_ci	length -= ARC_HDR_SIZE;
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	if (length > XMTU) {
12762306a36Sopenharmony_ci		/* should never happen! other people already check for this. */
12862306a36Sopenharmony_ci		arc_printk(D_NORMAL, dev, "Bug!  prepare_tx with size %d (> %d)\n",
12962306a36Sopenharmony_ci			   length, XMTU);
13062306a36Sopenharmony_ci		length = XMTU;
13162306a36Sopenharmony_ci	}
13262306a36Sopenharmony_ci	if (length >= MinTU) {
13362306a36Sopenharmony_ci		hard->offset[0] = 0;
13462306a36Sopenharmony_ci		hard->offset[1] = ofs = 512 - length;
13562306a36Sopenharmony_ci	} else if (length > MTU) {
13662306a36Sopenharmony_ci		hard->offset[0] = 0;
13762306a36Sopenharmony_ci		hard->offset[1] = ofs = 512 - length - 3;
13862306a36Sopenharmony_ci	} else {
13962306a36Sopenharmony_ci		hard->offset[0] = ofs = 256 - length;
14062306a36Sopenharmony_ci	}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	arc_printk(D_DURING, dev, "prepare_tx: length=%d ofs=%d\n",
14362306a36Sopenharmony_ci		   length, ofs);
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
14662306a36Sopenharmony_ci	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	lp->lastload_dest = hard->dest;
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	return 1;		/* done */
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistatic struct ArcProto rawmode_proto = {
15462306a36Sopenharmony_ci	.suffix		= 'r',
15562306a36Sopenharmony_ci	.mtu		= XMTU,
15662306a36Sopenharmony_ci	.rx		= rx,
15762306a36Sopenharmony_ci	.build_header	= build_header,
15862306a36Sopenharmony_ci	.prepare_tx	= prepare_tx,
15962306a36Sopenharmony_ci	.continue_tx    = NULL,
16062306a36Sopenharmony_ci	.ack_tx         = NULL
16162306a36Sopenharmony_ci};
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_cistatic int __init arcnet_raw_init(void)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	int count;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	pr_info("raw mode (`r') encapsulation support loaded\n");
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	for (count = 0; count < 256; count++)
17062306a36Sopenharmony_ci		if (arc_proto_map[count] == arc_proto_default)
17162306a36Sopenharmony_ci			arc_proto_map[count] = &rawmode_proto;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	/* for raw mode, we only set the bcast proto if there's no better one */
17462306a36Sopenharmony_ci	if (arc_bcast_proto == arc_proto_default)
17562306a36Sopenharmony_ci		arc_bcast_proto = &rawmode_proto;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	arc_proto_default = &rawmode_proto;
17862306a36Sopenharmony_ci	return 0;
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic void __exit arcnet_raw_exit(void)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	arcnet_unregister_proto(&rawmode_proto);
18462306a36Sopenharmony_ci}
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_cimodule_init(arcnet_raw_init);
18762306a36Sopenharmony_cimodule_exit(arcnet_raw_exit);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ciMODULE_LICENSE("GPL");
190