capmode.c 7.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Linux ARCnet driver - "cap mode" packet encapsulation.
 * It adds sequence numbers to packets for communicating between a user space
 * application and the driver. After a transmit it sends a packet with protocol
 * byte 0 back up to the userspace containing the sequence number of the packet
 * plus the transmit-status on the ArcNet.
 *
 * Written 2002-4 by Esben Nielsen, Vestas Wind Systems A/S
 * Derived from arc-rawmode.c by Avery Pennarun.
 * arc-rawmode was in turned based on skeleton.c, see below.
 *
 * **********************
 *
 * The original copyright of skeleton.c 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.
 *
 * **********************
 *
 * For more details, see drivers/net/arcnet.c
 *
 * **********************
 */

#include <linux/module.h>
30
#include <linux/gfp.h>
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
#include <linux/init.h>
#include <linux/if_arp.h>
#include <net/arp.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/arcdevice.h>

#define VERSION "arcnet: cap mode (`c') encapsulation support loaded.\n"

/* packet receiver */
static void rx(struct net_device *dev, int bufnum,
	       struct archdr *pkthdr, int length)
{
44
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
45 46 47 48 49
	struct sk_buff *skb;
	struct archdr *pkt = pkthdr;
	char *pktbuf, *pkthdrbuf;
	int ofs;

50 51
	arc_printk(D_DURING, dev, "it's a raw(cap) packet (length=%d)\n",
		   length);
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59

	if (length >= MinTU)
		ofs = 512 - length;
	else
		ofs = 256 - length;

	skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC);
	if (skb == NULL) {
60
		arc_printk(D_NORMAL, dev, "Memory squeeze, dropping packet\n");
61
		dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
62 63 64 65
		return;
	}
	skb_put(skb, length + ARC_HDR_SIZE + sizeof(int));
	skb->dev = dev;
66
	skb_reset_mac_header(skb);
67
	pkt = (struct archdr *)skb_mac_header(skb);
L
Linus Torvalds 已提交
68 69
	skb_pull(skb, ARC_HDR_SIZE);

70 71 72 73 74
	/* up to sizeof(pkt->soft) has already been copied from the card
	 * squeeze in an int for the cap encapsulation
	 * use these variables to be sure we count in bytes, not in
	 * sizeof(struct archdr)
	 */
75 76 77 78 79 80
	pktbuf = (char *)pkt;
	pkthdrbuf = (char *)pkthdr;
	memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto));
	memcpy(pktbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto) + sizeof(int),
	       pkthdrbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto),
	       sizeof(struct archdr) - ARC_HDR_SIZE - sizeof(pkt->soft.cap.proto));
L
Linus Torvalds 已提交
81 82 83 84 85 86 87

	if (length > sizeof(pkt->soft))
		lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
				      pkt->soft.raw + sizeof(pkt->soft)
				      + sizeof(int),
				      length - sizeof(pkt->soft));

88 89
	if (BUGLVL(D_SKB))
		arcnet_dump_skb(dev, skb, "rx");
L
Linus Torvalds 已提交
90

91
	skb->protocol = cpu_to_be16(ETH_P_ARCNET);
L
Linus Torvalds 已提交
92 93 94
	netif_rx(skb);
}

95
/* Create the ARCnet hard/soft headers for cap mode.
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103
 * There aren't any soft headers in cap mode - not even the protocol id.
 */
static int build_header(struct sk_buff *skb,
			struct net_device *dev,
			unsigned short type,
			uint8_t daddr)
{
	int hdr_size = ARC_HDR_SIZE;
104
	struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
L
Linus Torvalds 已提交
105

106 107
	arc_printk(D_PROTO, dev, "Preparing header for cap packet %x.\n",
		   *((int *)&pkt->soft.cap.cookie[0]));
108 109

	/* Set the source hardware address.
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119
	 *
	 * This is pretty pointless for most purposes, but it can help in
	 * debugging.  ARCnet does not allow us to change the source address in
	 * the actual packet sent)
	 */
	pkt->hard.source = *dev->dev_addr;

	/* see linux/net/ethernet/eth.c to see where I got the following */

	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
120 121
		/* FIXME: fill in the last byte of the dest ipaddr here to
		 * better comply with RFC1051 in "noarp" mode.
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134
		 */
		pkt->hard.dest = 0;
		return hdr_size;
	}
	/* otherwise, just fill it in and go! */
	pkt->hard.dest = daddr;

	return hdr_size;	/* success */
}

static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
		      int bufnum)
{
135
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143
	struct arc_hardware *hard = &pkt->hard;
	int ofs;

	/* hard header is not included in packet length */
	length -= ARC_HDR_SIZE;
	/* And neither is the cookie field */
	length -= sizeof(int);

144 145
	arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
		   lp->next_tx, lp->cur_tx, bufnum);
L
Linus Torvalds 已提交
146

147 148
	arc_printk(D_PROTO, dev, "Sending for cap packet %x.\n",
		   *((int *)&pkt->soft.cap.cookie[0]));
L
Linus Torvalds 已提交
149 150 151

	if (length > XMTU) {
		/* should never happen! other people already check for this. */
152 153
		arc_printk(D_NORMAL, dev, "Bug!  prepare_tx with size %d (> %d)\n",
			   length, XMTU);
L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161
		length = XMTU;
	}
	if (length > MinTU) {
		hard->offset[0] = 0;
		hard->offset[1] = ofs = 512 - length;
	} else if (length > MTU) {
		hard->offset[0] = 0;
		hard->offset[1] = ofs = 512 - length - 3;
162
	} else {
L
Linus Torvalds 已提交
163
		hard->offset[0] = ofs = 256 - length;
164
	}
L
Linus Torvalds 已提交
165

166 167
	arc_printk(D_DURING, dev, "prepare_tx: length=%d ofs=%d\n",
		   length, ofs);
L
Linus Torvalds 已提交
168

169
	/* Copy the arcnet-header + the protocol byte down: */
L
Linus Torvalds 已提交
170 171 172 173
	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
			    sizeof(pkt->soft.cap.proto));

174
	/* Skip the extra integer we have written into it as a cookie
175 176
	 * but write the rest of the message:
	 */
177 178
	lp->hw.copy_to_card(dev, bufnum, ofs + 1,
			    ((unsigned char *)&pkt->soft.cap.mes), length - 1);
L
Linus Torvalds 已提交
179 180 181

	lp->lastload_dest = hard->dest;

182
	return 1;	/* done */
L
Linus Torvalds 已提交
183 184 185 186
}

static int ack_tx(struct net_device *dev, int acked)
{
187 188 189
	struct arcnet_local *lp = netdev_priv(dev);
	struct sk_buff *ackskb;
	struct archdr *ackpkt;
190
	int length = sizeof(struct arc_cap);
L
Linus Torvalds 已提交
191

192 193
	arc_printk(D_DURING, dev, "capmode: ack_tx: protocol: %x: result: %d\n",
		   lp->outgoing.skb->protocol, acked);
L
Linus Torvalds 已提交
194

195 196
	if (BUGLVL(D_SKB))
		arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
L
Linus Torvalds 已提交
197

198
	/* Now alloc a skb to send back up through the layers: */
199
	ackskb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
200
	if (ackskb == NULL) {
201
		arc_printk(D_NORMAL, dev, "Memory squeeze, can't acknowledge\n");
202 203 204
		goto free_outskb;
	}

205
	skb_put(ackskb, length + ARC_HDR_SIZE);
206 207 208 209 210
	ackskb->dev = dev;

	skb_reset_mac_header(ackskb);
	ackpkt = (struct archdr *)skb_mac_header(ackskb);
	/* skb_pull(ackskb, ARC_HDR_SIZE); */
L
Linus Torvalds 已提交
211

212 213 214
	skb_copy_from_linear_data(lp->outgoing.skb, ackpkt,
				  ARC_HDR_SIZE + sizeof(struct arc_cap));
	ackpkt->soft.cap.proto = 0; /* using protocol 0 for acknowledge */
215
	ackpkt->soft.cap.mes.ack = acked;
L
Linus Torvalds 已提交
216

217 218
	arc_printk(D_PROTO, dev, "Ackknowledge for cap packet %x.\n",
		   *((int *)&ackpkt->soft.cap.cookie[0]));
L
Linus Torvalds 已提交
219

220
	ackskb->protocol = cpu_to_be16(ETH_P_ARCNET);
L
Linus Torvalds 已提交
221

222 223
	if (BUGLVL(D_SKB))
		arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
224
	netif_rx(ackskb);
L
Linus Torvalds 已提交
225

226 227 228
free_outskb:
	dev_kfree_skb_irq(lp->outgoing.skb);
	lp->outgoing.proto = NULL; /* We are always finished when in this protocol */
L
Linus Torvalds 已提交
229

230 231
	return 0;
}
L
Linus Torvalds 已提交
232

233
static struct ArcProto capmode_proto = {
234 235 236 237 238 239 240 241 242
	'r',
	XMTU,
	0,
	rx,
	build_header,
	prepare_tx,
	NULL,
	ack_tx
};
L
Linus Torvalds 已提交
243

244 245 246
static void arcnet_cap_init(void)
{
	int count;
L
Linus Torvalds 已提交
247

248 249 250 251 252 253 254 255 256 257
	for (count = 1; count <= 8; count++)
		if (arc_proto_map[count] == arc_proto_default)
			arc_proto_map[count] = &capmode_proto;

	/* for cap mode, we only set the bcast proto if there's no better one */
	if (arc_bcast_proto == arc_proto_default)
		arc_bcast_proto = &capmode_proto;

	arc_proto_default = &capmode_proto;
	arc_raw_proto = &capmode_proto;
L
Linus Torvalds 已提交
258
}
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

static int __init capmode_module_init(void)
{
	printk(VERSION);
	arcnet_cap_init();
	return 0;
}

static void __exit capmode_module_exit(void)
{
	arcnet_unregister_proto(&capmode_proto);
}
module_init(capmode_module_init);
module_exit(capmode_module_exit);

MODULE_LICENSE("GPL");