1 /*
2  * Linux ARCnet driver - "raw mode" packet encapsulation (no soft headers)
3  *
4  * Written 1994-1999 by Avery Pennarun.
5  * Derived from skeleton.c by Donald Becker.
6  *
7  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
8  *  for sponsoring the further development of this driver.
9  *
10  * **********************
11  *
12  * The original copyright of skeleton.c was as follows:
13  *
14  * skeleton.c Written 1993 by Donald Becker.
15  * Copyright 1993 United States Government as represented by the
16  * Director, National Security Agency.  This software may only be used
17  * and distributed according to the terms of the GNU General Public License as
18  * modified by SRC, incorporated herein by reference.
19  *
20  * **********************
21  *
22  * For more details, see drivers/net/arcnet.c
23  *
24  * **********************
25  */
26 
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/if_arp.h>
30 #include <net/arp.h>
31 #include <linux/netdevice.h>
32 #include <linux/skbuff.h>
33 #include <linux/arcdevice.h>
34 
35 #define VERSION "arcnet: raw mode (`r') encapsulation support loaded.\n"
36 
37 
38 static void rx(struct net_device *dev, int bufnum,
39 	       struct archdr *pkthdr, int length);
40 static int build_header(struct sk_buff *skb, struct net_device *dev,
41 			unsigned short type, uint8_t daddr);
42 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
43 		      int bufnum);
44 
45 
46 struct ArcProto rawmode_proto =
47 {
48 	'r',
49 	XMTU,
50 	rx,
51 	build_header,
52 	prepare_tx
53 };
54 
55 
arcnet_raw_init(void)56 void arcnet_raw_init(void)
57 {
58 	int count;
59 
60 	for (count = 0; count < 256; count++)
61 		if (arc_proto_map[count] == arc_proto_default)
62 			arc_proto_map[count] = &rawmode_proto;
63 
64 	/* for raw mode, we only set the bcast proto if there's no better one */
65 	if (arc_bcast_proto == arc_proto_default)
66 		arc_bcast_proto = &rawmode_proto;
67 
68 	arc_proto_default = &rawmode_proto;
69 }
70 
71 
72 #ifdef MODULE
73 
init_module(void)74 int __init init_module(void)
75 {
76 	printk(VERSION);
77 	arcnet_raw_init();
78 	return 0;
79 }
80 
cleanup_module(void)81 void cleanup_module(void)
82 {
83 	arcnet_unregister_proto(&rawmode_proto);
84 }
85 
86 MODULE_LICENSE("GPL");
87 #endif				/* MODULE */
88 
89 
90 
91 /* packet receiver */
rx(struct net_device * dev,int bufnum,struct archdr * pkthdr,int length)92 static void rx(struct net_device *dev, int bufnum,
93 	       struct archdr *pkthdr, int length)
94 {
95 	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
96 	struct sk_buff *skb;
97 	struct archdr *pkt = pkthdr;
98 	int ofs;
99 
100 	BUGMSG(D_DURING, "it's a raw packet (length=%d)\n", length);
101 
102 	if (length >= MinTU)
103 		ofs = 512 - length;
104 	else
105 		ofs = 256 - length;
106 
107 	skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
108 	if (skb == NULL) {
109 		BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
110 		lp->stats.rx_dropped++;
111 		return;
112 	}
113 	skb_put(skb, length + ARC_HDR_SIZE);
114 	skb->dev = dev;
115 
116 	pkt = (struct archdr *) skb->data;
117 
118 	skb->mac.raw = skb->data;
119 	skb_pull(skb, ARC_HDR_SIZE);
120 
121 	/* up to sizeof(pkt->soft) has already been copied from the card */
122 	memcpy(pkt, pkthdr, sizeof(struct archdr));
123 	if (length > sizeof(pkt->soft))
124 		lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
125 				      pkt->soft.raw + sizeof(pkt->soft),
126 				      length - sizeof(pkt->soft));
127 
128 	BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
129 
130 	skb->protocol = 0;
131 	netif_rx(skb);
132 	dev->last_rx = jiffies;
133 }
134 
135 
136 /*
137  * Create the ARCnet hard/soft headers for raw mode.
138  * There aren't any soft headers in raw mode - not even the protocol id.
139  */
build_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,uint8_t daddr)140 static int build_header(struct sk_buff *skb, struct net_device *dev,
141 			unsigned short type, uint8_t daddr)
142 {
143 	int hdr_size = ARC_HDR_SIZE;
144 	struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
145 
146 	/*
147 	 * Set the source hardware address.
148 	 *
149 	 * This is pretty pointless for most purposes, but it can help in
150 	 * debugging.  ARCnet does not allow us to change the source address in
151 	 * the actual packet sent)
152 	 */
153 	pkt->hard.source = *dev->dev_addr;
154 
155 	/* see linux/net/ethernet/eth.c to see where I got the following */
156 
157 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
158 		/*
159 		 * FIXME: fill in the last byte of the dest ipaddr here to better
160 		 * comply with RFC1051 in "noarp" mode.
161 		 */
162 		pkt->hard.dest = 0;
163 		return hdr_size;
164 	}
165 	/* otherwise, just fill it in and go! */
166 	pkt->hard.dest = daddr;
167 
168 	return hdr_size;	/* success */
169 }
170 
171 
prepare_tx(struct net_device * dev,struct archdr * pkt,int length,int bufnum)172 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
173 		      int bufnum)
174 {
175 	struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
176 	struct arc_hardware *hard = &pkt->hard;
177 	int ofs;
178 
179 	BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
180 	       lp->next_tx, lp->cur_tx, bufnum);
181 
182 	length -= ARC_HDR_SIZE;	/* hard header is not included in packet length */
183 
184 	if (length > XMTU) {
185 		/* should never happen! other people already check for this. */
186 		BUGMSG(D_NORMAL, "Bug!  prepare_tx with size %d (> %d)\n",
187 		       length, XMTU);
188 		length = XMTU;
189 	}
190 	if (length > MinTU) {
191 		hard->offset[0] = 0;
192 		hard->offset[1] = ofs = 512 - length;
193 	} else if (length > MTU) {
194 		hard->offset[0] = 0;
195 		hard->offset[1] = ofs = 512 - length - 3;
196 	} else
197 		hard->offset[0] = ofs = 256 - length;
198 
199 	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
200 	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
201 
202 	lp->lastload_dest = hard->dest;
203 
204 	return 1;		/* done */
205 }
206