arc-rawmode.c 5.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Linux ARCnet driver - "raw mode" packet encapsulation (no soft headers)
3
 *
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * Written 1994-1999 by Avery Pennarun.
 * 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 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
 *
 * **********************
 */

27 28
#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
29
#include <linux/module.h>
30
#include <linux/gfp.h>
L
Linus Torvalds 已提交
31 32 33 34 35
#include <linux/init.h>
#include <linux/if_arp.h>
#include <net/arp.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
36 37

#include "arcdevice.h"
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45

static void rx(struct net_device *dev, int bufnum,
	       struct archdr *pkthdr, int length);
static int build_header(struct sk_buff *skb, struct net_device *dev,
			unsigned short type, uint8_t daddr);
static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
		      int bufnum);

46
static struct ArcProto rawmode_proto = {
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59
	.suffix		= 'r',
	.mtu		= XMTU,
	.rx		= rx,
	.build_header	= build_header,
	.prepare_tx	= prepare_tx,
	.continue_tx    = NULL,
	.ack_tx         = NULL
};

static int __init arcnet_raw_init(void)
{
	int count;

60
	pr_info("%s\n", "raw mode (`r') encapsulation support loaded");
L
Linus Torvalds 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

	for (count = 0; count < 256; count++)
		if (arc_proto_map[count] == arc_proto_default)
			arc_proto_map[count] = &rawmode_proto;

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

	arc_proto_default = &rawmode_proto;
	return 0;
}

static void __exit arcnet_raw_exit(void)
{
	arcnet_unregister_proto(&rawmode_proto);
}

module_init(arcnet_raw_init);
module_exit(arcnet_raw_exit);

MODULE_LICENSE("GPL");

/* packet receiver */
static void rx(struct net_device *dev, int bufnum,
	       struct archdr *pkthdr, int length)
{
88
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
89 90 91 92
	struct sk_buff *skb;
	struct archdr *pkt = pkthdr;
	int ofs;

93
	arc_printk(D_DURING, dev, "it's a raw packet (length=%d)\n", length);
L
Linus Torvalds 已提交
94

95
	if (length > MTU)
L
Linus Torvalds 已提交
96 97 98 99 100
		ofs = 512 - length;
	else
		ofs = 256 - length;

	skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
101
	if (!skb) {
102
		dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
103 104 105 106 107
		return;
	}
	skb_put(skb, length + ARC_HDR_SIZE);
	skb->dev = dev;

108
	pkt = (struct archdr *)skb->data;
L
Linus Torvalds 已提交
109

110
	skb_reset_mac_header(skb);
L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119
	skb_pull(skb, ARC_HDR_SIZE);

	/* up to sizeof(pkt->soft) has already been copied from the card */
	memcpy(pkt, pkthdr, sizeof(struct archdr));
	if (length > sizeof(pkt->soft))
		lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
				      pkt->soft.raw + sizeof(pkt->soft),
				      length - sizeof(pkt->soft));

120 121
	if (BUGLVL(D_SKB))
		arcnet_dump_skb(dev, skb, "rx");
L
Linus Torvalds 已提交
122

123
	skb->protocol = cpu_to_be16(ETH_P_ARCNET);
L
Linus Torvalds 已提交
124 125 126
	netif_rx(skb);
}

127
/* Create the ARCnet hard/soft headers for raw mode.
L
Linus Torvalds 已提交
128 129 130 131 132 133
 * There aren't any soft headers in raw 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;
134
	struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
L
Linus Torvalds 已提交
135

136
	/* Set the source hardware address.
L
Linus Torvalds 已提交
137 138
	 *
	 * This is pretty pointless for most purposes, but it can help in
139 140
	 * debugging.  ARCnet does not allow us to change the source address
	 * in the actual packet sent.
L
Linus Torvalds 已提交
141 142 143 144 145 146
	 */
	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)) {
147 148
		/* FIXME: fill in the last byte of the dest ipaddr here
		 * to better comply with RFC1051 in "noarp" mode.
L
Linus Torvalds 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161
		 */
		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)
{
162
	struct arcnet_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
163 164 165
	struct arc_hardware *hard = &pkt->hard;
	int ofs;

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

J
Joe Perches 已提交
169 170
	/* hard header is not included in packet length */
	length -= ARC_HDR_SIZE;
L
Linus Torvalds 已提交
171 172 173

	if (length > XMTU) {
		/* should never happen! other people already check for this. */
174 175
		arc_printk(D_NORMAL, dev, "Bug!  prepare_tx with size %d (> %d)\n",
			   length, XMTU);
L
Linus Torvalds 已提交
176 177
		length = XMTU;
	}
178
	if (length >= MinTU) {
L
Linus Torvalds 已提交
179 180 181 182 183
		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;
184
	} else {
L
Linus Torvalds 已提交
185
		hard->offset[0] = ofs = 256 - length;
186
	}
L
Linus Torvalds 已提交
187

188 189
	arc_printk(D_DURING, dev, "prepare_tx: length=%d ofs=%d\n",
		   length, ofs);
L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197

	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);

	lp->lastload_dest = hard->dest;

	return 1;		/* done */
}