162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Linux ARCnet driver - "cap mode" packet encapsulation.
362306a36Sopenharmony_ci * It adds sequence numbers to packets for communicating between a user space
462306a36Sopenharmony_ci * application and the driver. After a transmit it sends a packet with protocol
562306a36Sopenharmony_ci * byte 0 back up to the userspace containing the sequence number of the packet
662306a36Sopenharmony_ci * plus the transmit-status on the ArcNet.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Written 2002-4 by Esben Nielsen, Vestas Wind Systems A/S
962306a36Sopenharmony_ci * Derived from arc-rawmode.c by Avery Pennarun.
1062306a36Sopenharmony_ci * arc-rawmode was in turned based on skeleton.c, see below.
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * **********************
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * The original copyright of skeleton.c was as follows:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * skeleton.c Written 1993 by Donald Becker.
1762306a36Sopenharmony_ci * Copyright 1993 United States Government as represented by the
1862306a36Sopenharmony_ci * Director, National Security Agency.  This software may only be used
1962306a36Sopenharmony_ci * and distributed according to the terms of the GNU General Public License as
2062306a36Sopenharmony_ci * modified by SRC, incorporated herein by reference.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * **********************
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * For more details, see drivers/net/arcnet.c
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * **********************
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#include <linux/module.h>
3262306a36Sopenharmony_ci#include <linux/gfp.h>
3362306a36Sopenharmony_ci#include <linux/init.h>
3462306a36Sopenharmony_ci#include <linux/if_arp.h>
3562306a36Sopenharmony_ci#include <net/arp.h>
3662306a36Sopenharmony_ci#include <linux/netdevice.h>
3762306a36Sopenharmony_ci#include <linux/skbuff.h>
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#include "arcdevice.h"
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* packet receiver */
4262306a36Sopenharmony_cistatic void rx(struct net_device *dev, int bufnum,
4362306a36Sopenharmony_ci	       struct archdr *pkthdr, int length)
4462306a36Sopenharmony_ci{
4562306a36Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
4662306a36Sopenharmony_ci	struct sk_buff *skb;
4762306a36Sopenharmony_ci	struct archdr *pkt;
4862306a36Sopenharmony_ci	char *pktbuf, *pkthdrbuf;
4962306a36Sopenharmony_ci	int ofs;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	arc_printk(D_DURING, dev, "it's a raw(cap) packet (length=%d)\n",
5262306a36Sopenharmony_ci		   length);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	if (length >= MinTU)
5562306a36Sopenharmony_ci		ofs = 512 - length;
5662306a36Sopenharmony_ci	else
5762306a36Sopenharmony_ci		ofs = 256 - length;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC);
6062306a36Sopenharmony_ci	if (!skb) {
6162306a36Sopenharmony_ci		dev->stats.rx_dropped++;
6262306a36Sopenharmony_ci		return;
6362306a36Sopenharmony_ci	}
6462306a36Sopenharmony_ci	skb_put(skb, length + ARC_HDR_SIZE + sizeof(int));
6562306a36Sopenharmony_ci	skb->dev = dev;
6662306a36Sopenharmony_ci	skb_reset_mac_header(skb);
6762306a36Sopenharmony_ci	pkt = (struct archdr *)skb_mac_header(skb);
6862306a36Sopenharmony_ci	skb_pull(skb, ARC_HDR_SIZE);
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	/* up to sizeof(pkt->soft) has already been copied from the card
7162306a36Sopenharmony_ci	 * squeeze in an int for the cap encapsulation
7262306a36Sopenharmony_ci	 * use these variables to be sure we count in bytes, not in
7362306a36Sopenharmony_ci	 * sizeof(struct archdr)
7462306a36Sopenharmony_ci	 */
7562306a36Sopenharmony_ci	pktbuf = (char *)pkt;
7662306a36Sopenharmony_ci	pkthdrbuf = (char *)pkthdr;
7762306a36Sopenharmony_ci	memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto));
7862306a36Sopenharmony_ci	memcpy(pktbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto) + sizeof(int),
7962306a36Sopenharmony_ci	       pkthdrbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto),
8062306a36Sopenharmony_ci	       sizeof(struct archdr) - ARC_HDR_SIZE - sizeof(pkt->soft.cap.proto));
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	if (length > sizeof(pkt->soft))
8362306a36Sopenharmony_ci		lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
8462306a36Sopenharmony_ci				      pkt->soft.raw + sizeof(pkt->soft)
8562306a36Sopenharmony_ci				      + sizeof(int),
8662306a36Sopenharmony_ci				      length - sizeof(pkt->soft));
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	if (BUGLVL(D_SKB))
8962306a36Sopenharmony_ci		arcnet_dump_skb(dev, skb, "rx");
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	skb->protocol = cpu_to_be16(ETH_P_ARCNET);
9262306a36Sopenharmony_ci	netif_rx(skb);
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci/* Create the ARCnet hard/soft headers for cap mode.
9662306a36Sopenharmony_ci * There aren't any soft headers in cap mode - not even the protocol id.
9762306a36Sopenharmony_ci */
9862306a36Sopenharmony_cistatic int build_header(struct sk_buff *skb,
9962306a36Sopenharmony_ci			struct net_device *dev,
10062306a36Sopenharmony_ci			unsigned short type,
10162306a36Sopenharmony_ci			uint8_t daddr)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	int hdr_size = ARC_HDR_SIZE;
10462306a36Sopenharmony_ci	struct archdr *pkt = skb_push(skb, hdr_size);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	arc_printk(D_PROTO, dev, "Preparing header for cap packet %x.\n",
10762306a36Sopenharmony_ci		   *((int *)&pkt->soft.cap.cookie[0]));
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	/* Set the source hardware address.
11062306a36Sopenharmony_ci	 *
11162306a36Sopenharmony_ci	 * This is pretty pointless for most purposes, but it can help in
11262306a36Sopenharmony_ci	 * debugging.  ARCnet does not allow us to change the source address in
11362306a36Sopenharmony_ci	 * the actual packet sent)
11462306a36Sopenharmony_ci	 */
11562306a36Sopenharmony_ci	pkt->hard.source = *dev->dev_addr;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	/* see linux/net/ethernet/eth.c to see where I got the following */
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
12062306a36Sopenharmony_ci		/* FIXME: fill in the last byte of the dest ipaddr here to
12162306a36Sopenharmony_ci		 * better comply with RFC1051 in "noarp" mode.
12262306a36Sopenharmony_ci		 */
12362306a36Sopenharmony_ci		pkt->hard.dest = 0;
12462306a36Sopenharmony_ci		return hdr_size;
12562306a36Sopenharmony_ci	}
12662306a36Sopenharmony_ci	/* otherwise, just fill it in and go! */
12762306a36Sopenharmony_ci	pkt->hard.dest = daddr;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	return hdr_size;	/* success */
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_cistatic int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
13362306a36Sopenharmony_ci		      int bufnum)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
13662306a36Sopenharmony_ci	struct arc_hardware *hard = &pkt->hard;
13762306a36Sopenharmony_ci	int ofs;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	/* hard header is not included in packet length */
14062306a36Sopenharmony_ci	length -= ARC_HDR_SIZE;
14162306a36Sopenharmony_ci	/* And neither is the cookie field */
14262306a36Sopenharmony_ci	length -= sizeof(int);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
14562306a36Sopenharmony_ci		   lp->next_tx, lp->cur_tx, bufnum);
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	arc_printk(D_PROTO, dev, "Sending for cap packet %x.\n",
14862306a36Sopenharmony_ci		   *((int *)&pkt->soft.cap.cookie[0]));
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	if (length > XMTU) {
15162306a36Sopenharmony_ci		/* should never happen! other people already check for this. */
15262306a36Sopenharmony_ci		arc_printk(D_NORMAL, dev, "Bug!  prepare_tx with size %d (> %d)\n",
15362306a36Sopenharmony_ci			   length, XMTU);
15462306a36Sopenharmony_ci		length = XMTU;
15562306a36Sopenharmony_ci	}
15662306a36Sopenharmony_ci	if (length > MinTU) {
15762306a36Sopenharmony_ci		hard->offset[0] = 0;
15862306a36Sopenharmony_ci		hard->offset[1] = ofs = 512 - length;
15962306a36Sopenharmony_ci	} else if (length > MTU) {
16062306a36Sopenharmony_ci		hard->offset[0] = 0;
16162306a36Sopenharmony_ci		hard->offset[1] = ofs = 512 - length - 3;
16262306a36Sopenharmony_ci	} else {
16362306a36Sopenharmony_ci		hard->offset[0] = ofs = 256 - length;
16462306a36Sopenharmony_ci	}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	arc_printk(D_DURING, dev, "prepare_tx: length=%d ofs=%d\n",
16762306a36Sopenharmony_ci		   length, ofs);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	/* Copy the arcnet-header + the protocol byte down: */
17062306a36Sopenharmony_ci	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
17162306a36Sopenharmony_ci	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
17262306a36Sopenharmony_ci			    sizeof(pkt->soft.cap.proto));
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	/* Skip the extra integer we have written into it as a cookie
17562306a36Sopenharmony_ci	 * but write the rest of the message:
17662306a36Sopenharmony_ci	 */
17762306a36Sopenharmony_ci	lp->hw.copy_to_card(dev, bufnum, ofs + 1,
17862306a36Sopenharmony_ci			    ((unsigned char *)&pkt->soft.cap.mes), length - 1);
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	lp->lastload_dest = hard->dest;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	return 1;	/* done */
18362306a36Sopenharmony_ci}
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cistatic int ack_tx(struct net_device *dev, int acked)
18662306a36Sopenharmony_ci{
18762306a36Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
18862306a36Sopenharmony_ci	struct sk_buff *ackskb;
18962306a36Sopenharmony_ci	struct archdr *ackpkt;
19062306a36Sopenharmony_ci	int length = sizeof(struct arc_cap);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	arc_printk(D_DURING, dev, "capmode: ack_tx: protocol: %x: result: %d\n",
19362306a36Sopenharmony_ci		   lp->outgoing.skb->protocol, acked);
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	if (BUGLVL(D_SKB))
19662306a36Sopenharmony_ci		arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	/* Now alloc a skb to send back up through the layers: */
19962306a36Sopenharmony_ci	ackskb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
20062306a36Sopenharmony_ci	if (!ackskb)
20162306a36Sopenharmony_ci		goto free_outskb;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	skb_put(ackskb, length + ARC_HDR_SIZE);
20462306a36Sopenharmony_ci	ackskb->dev = dev;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	skb_reset_mac_header(ackskb);
20762306a36Sopenharmony_ci	ackpkt = (struct archdr *)skb_mac_header(ackskb);
20862306a36Sopenharmony_ci	/* skb_pull(ackskb, ARC_HDR_SIZE); */
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	skb_copy_from_linear_data(lp->outgoing.skb, ackpkt,
21162306a36Sopenharmony_ci				  ARC_HDR_SIZE + sizeof(struct arc_cap));
21262306a36Sopenharmony_ci	ackpkt->soft.cap.proto = 0; /* using protocol 0 for acknowledge */
21362306a36Sopenharmony_ci	ackpkt->soft.cap.mes.ack = acked;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	arc_printk(D_PROTO, dev, "Acknowledge for cap packet %x.\n",
21662306a36Sopenharmony_ci		   *((int *)&ackpkt->soft.cap.cookie[0]));
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	ackskb->protocol = cpu_to_be16(ETH_P_ARCNET);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	if (BUGLVL(D_SKB))
22162306a36Sopenharmony_ci		arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
22262306a36Sopenharmony_ci	netif_rx(ackskb);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_cifree_outskb:
22562306a36Sopenharmony_ci	dev_kfree_skb_irq(lp->outgoing.skb);
22662306a36Sopenharmony_ci	lp->outgoing.proto = NULL;
22762306a36Sopenharmony_ci			/* We are always finished when in this protocol */
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	return 0;
23062306a36Sopenharmony_ci}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistatic struct ArcProto capmode_proto = {
23362306a36Sopenharmony_ci	.suffix		= 'r',
23462306a36Sopenharmony_ci	.mtu		= XMTU,
23562306a36Sopenharmony_ci	.rx		= rx,
23662306a36Sopenharmony_ci	.build_header	= build_header,
23762306a36Sopenharmony_ci	.prepare_tx	= prepare_tx,
23862306a36Sopenharmony_ci	.ack_tx		= ack_tx
23962306a36Sopenharmony_ci};
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_cistatic int __init capmode_module_init(void)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	int count;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	pr_info("cap mode (`c') encapsulation support loaded\n");
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	for (count = 1; count <= 8; count++)
24862306a36Sopenharmony_ci		if (arc_proto_map[count] == arc_proto_default)
24962306a36Sopenharmony_ci			arc_proto_map[count] = &capmode_proto;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	/* for cap mode, we only set the bcast proto if there's no better one */
25262306a36Sopenharmony_ci	if (arc_bcast_proto == arc_proto_default)
25362306a36Sopenharmony_ci		arc_bcast_proto = &capmode_proto;
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	arc_proto_default = &capmode_proto;
25662306a36Sopenharmony_ci	arc_raw_proto = &capmode_proto;
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	return 0;
25962306a36Sopenharmony_ci}
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_cistatic void __exit capmode_module_exit(void)
26262306a36Sopenharmony_ci{
26362306a36Sopenharmony_ci	arcnet_unregister_proto(&capmode_proto);
26462306a36Sopenharmony_ci}
26562306a36Sopenharmony_cimodule_init(capmode_module_init);
26662306a36Sopenharmony_cimodule_exit(capmode_module_exit);
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ciMODULE_LICENSE("GPL");
269