blob: e7ec907f4680c41ac50efcd8323acddba17c4358 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux ARCnet driver - "cap mode" packet encapsulation.
3 * It adds sequence numbers to packets for communicating between a user space
4 * application and the driver. After a transmit it sends a packet with protocol
5 * byte 0 back up to the userspace containing the sequence number of the packet
6 * plus the transmit-status on the ArcNet.
7 *
8 * Written 2002-4 by Esben Nielsen, Vestas Wind Systems A/S
9 * Derived from arc-rawmode.c by Avery Pennarun.
10 * arc-rawmode was in turned based on skeleton.c, see below.
11 *
12 * **********************
13 *
14 * The original copyright of skeleton.c was as follows:
15 *
16 * skeleton.c Written 1993 by Donald Becker.
17 * Copyright 1993 United States Government as represented by the
18 * Director, National Security Agency. This software may only be used
19 * and distributed according to the terms of the GNU General Public License as
20 * modified by SRC, incorporated herein by reference.
21 *
22 * **********************
23 *
24 * For more details, see drivers/net/arcnet.c
25 *
26 * **********************
27 */
28
29#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32#include <linux/if_arp.h>
33#include <net/arp.h>
34#include <linux/netdevice.h>
35#include <linux/skbuff.h>
36#include <linux/arcdevice.h>
37
38#define VERSION "arcnet: cap mode (`c') encapsulation support loaded.\n"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* packet receiver */
41static void rx(struct net_device *dev, int bufnum,
42 struct archdr *pkthdr, int length)
43{
Wang Chen454d7c92008-11-12 23:37:49 -080044 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct sk_buff *skb;
46 struct archdr *pkt = pkthdr;
47 char *pktbuf, *pkthdrbuf;
48 int ofs;
49
50 BUGMSG(D_DURING, "it's a raw(cap) packet (length=%d)\n", length);
51
52 if (length >= MinTU)
53 ofs = 512 - length;
54 else
55 ofs = 256 - length;
56
57 skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC);
58 if (skb == NULL) {
59 BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
Stephen Hemminger5803c512009-01-09 13:01:08 +000060 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return;
62 }
63 skb_put(skb, length + ARC_HDR_SIZE + sizeof(int));
64 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070065 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070066 pkt = (struct archdr *)skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 skb_pull(skb, ARC_HDR_SIZE);
68
Joe Perchesf2f0a162015-05-05 10:05:52 -070069 /* up to sizeof(pkt->soft) has already been copied from the card
70 * squeeze in an int for the cap encapsulation
71 * use these variables to be sure we count in bytes, not in
72 * sizeof(struct archdr)
73 */
Joe Perchescb334642015-05-05 10:05:47 -070074 pktbuf = (char *)pkt;
75 pkthdrbuf = (char *)pkthdr;
76 memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto));
77 memcpy(pktbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto) + sizeof(int),
78 pkthdrbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto),
79 sizeof(struct archdr) - ARC_HDR_SIZE - sizeof(pkt->soft.cap.proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 if (length > sizeof(pkt->soft))
82 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
83 pkt->soft.raw + sizeof(pkt->soft)
84 + sizeof(int),
85 length - sizeof(pkt->soft));
86
Joe Perches72aeea42015-05-05 10:05:54 -070087 if (BUGLVL(D_SKB))
88 arcnet_dump_skb(dev, skb, "rx");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Harvey Harrison09640e62009-02-01 00:45:17 -080090 skb->protocol = cpu_to_be16(ETH_P_ARCNET);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Joe Perchesf2f0a162015-05-05 10:05:52 -070094/* Create the ARCnet hard/soft headers for cap mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * There aren't any soft headers in cap mode - not even the protocol id.
96 */
97static int build_header(struct sk_buff *skb,
98 struct net_device *dev,
99 unsigned short type,
100 uint8_t daddr)
101{
102 int hdr_size = ARC_HDR_SIZE;
Joe Perchescb334642015-05-05 10:05:47 -0700103 struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 BUGMSG(D_PROTO, "Preparing header for cap packet %x.\n",
Joe Perchescb334642015-05-05 10:05:47 -0700106 *((int *)&pkt->soft.cap.cookie[0]));
Joe Perchesf2f0a162015-05-05 10:05:52 -0700107
108 /* Set the source hardware address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
110 * This is pretty pointless for most purposes, but it can help in
111 * debugging. ARCnet does not allow us to change the source address in
112 * the actual packet sent)
113 */
114 pkt->hard.source = *dev->dev_addr;
115
116 /* see linux/net/ethernet/eth.c to see where I got the following */
117
118 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
Joe Perchesf2f0a162015-05-05 10:05:52 -0700119 /* FIXME: fill in the last byte of the dest ipaddr here to
120 * better comply with RFC1051 in "noarp" mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122 pkt->hard.dest = 0;
123 return hdr_size;
124 }
125 /* otherwise, just fill it in and go! */
126 pkt->hard.dest = daddr;
127
128 return hdr_size; /* success */
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
132 int bufnum)
133{
Wang Chen454d7c92008-11-12 23:37:49 -0800134 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct arc_hardware *hard = &pkt->hard;
136 int ofs;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* hard header is not included in packet length */
139 length -= ARC_HDR_SIZE;
140 /* And neither is the cookie field */
141 length -= sizeof(int);
142
143 BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
144 lp->next_tx, lp->cur_tx, bufnum);
145
146 BUGMSG(D_PROTO, "Sending for cap packet %x.\n",
Joe Perchescb334642015-05-05 10:05:47 -0700147 *((int *)&pkt->soft.cap.cookie[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (length > XMTU) {
150 /* should never happen! other people already check for this. */
151 BUGMSG(D_NORMAL, "Bug! prepare_tx with size %d (> %d)\n",
152 length, XMTU);
153 length = XMTU;
154 }
155 if (length > MinTU) {
156 hard->offset[0] = 0;
157 hard->offset[1] = ofs = 512 - length;
158 } else if (length > MTU) {
159 hard->offset[0] = 0;
160 hard->offset[1] = ofs = 512 - length - 3;
Joe Perches7f5e7602015-05-05 10:05:49 -0700161 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 hard->offset[0] = ofs = 256 - length;
Joe Perches7f5e7602015-05-05 10:05:49 -0700163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 BUGMSG(D_DURING, "prepare_tx: length=%d ofs=%d\n",
Joe Perchescb334642015-05-05 10:05:47 -0700166 length, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Daniel Mack0da529a2010-05-31 00:35:13 -0700168 /* Copy the arcnet-header + the protocol byte down: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
170 lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
171 sizeof(pkt->soft.cap.proto));
172
Daniel Mack0da529a2010-05-31 00:35:13 -0700173 /* Skip the extra integer we have written into it as a cookie
Joe Perchesf2f0a162015-05-05 10:05:52 -0700174 * but write the rest of the message:
175 */
Joe Perchescb334642015-05-05 10:05:47 -0700176 lp->hw.copy_to_card(dev, bufnum, ofs + 1,
177 ((unsigned char *)&pkt->soft.cap.mes), length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 lp->lastload_dest = hard->dest;
180
Daniel Mack0da529a2010-05-31 00:35:13 -0700181 return 1; /* done */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static int ack_tx(struct net_device *dev, int acked)
185{
Daniel Mack0da529a2010-05-31 00:35:13 -0700186 struct arcnet_local *lp = netdev_priv(dev);
187 struct sk_buff *ackskb;
188 struct archdr *ackpkt;
Joe Perchescb334642015-05-05 10:05:47 -0700189 int length = sizeof(struct arc_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Daniel Mack0da529a2010-05-31 00:35:13 -0700191 BUGMSG(D_DURING, "capmode: ack_tx: protocol: %x: result: %d\n",
Joe Perchescb334642015-05-05 10:05:47 -0700192 lp->outgoing.skb->protocol, acked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Joe Perches72aeea42015-05-05 10:05:54 -0700194 if (BUGLVL(D_SKB))
195 arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Daniel Mack0da529a2010-05-31 00:35:13 -0700197 /* Now alloc a skb to send back up through the layers: */
Joe Perchescb334642015-05-05 10:05:47 -0700198 ackskb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
Daniel Mack0da529a2010-05-31 00:35:13 -0700199 if (ackskb == NULL) {
200 BUGMSG(D_NORMAL, "Memory squeeze, can't acknowledge.\n");
201 goto free_outskb;
202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Joe Perchescb334642015-05-05 10:05:47 -0700204 skb_put(ackskb, length + ARC_HDR_SIZE);
Daniel Mack0da529a2010-05-31 00:35:13 -0700205 ackskb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Daniel Mack0da529a2010-05-31 00:35:13 -0700207 skb_reset_mac_header(ackskb);
208 ackpkt = (struct archdr *)skb_mac_header(ackskb);
209 /* skb_pull(ackskb, ARC_HDR_SIZE); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Daniel Mack0da529a2010-05-31 00:35:13 -0700211 skb_copy_from_linear_data(lp->outgoing.skb, ackpkt,
212 ARC_HDR_SIZE + sizeof(struct arc_cap));
213 ackpkt->soft.cap.proto = 0; /* using protocol 0 for acknowledge */
Joe Perchescb334642015-05-05 10:05:47 -0700214 ackpkt->soft.cap.mes.ack = acked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Daniel Mack0da529a2010-05-31 00:35:13 -0700216 BUGMSG(D_PROTO, "Ackknowledge for cap packet %x.\n",
Joe Perchescb334642015-05-05 10:05:47 -0700217 *((int *)&ackpkt->soft.cap.cookie[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Daniel Mack0da529a2010-05-31 00:35:13 -0700219 ackskb->protocol = cpu_to_be16(ETH_P_ARCNET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Joe Perches72aeea42015-05-05 10:05:54 -0700221 if (BUGLVL(D_SKB))
222 arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
Daniel Mack0da529a2010-05-31 00:35:13 -0700223 netif_rx(ackskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Daniel Mack0da529a2010-05-31 00:35:13 -0700225free_outskb:
226 dev_kfree_skb_irq(lp->outgoing.skb);
227 lp->outgoing.proto = NULL; /* We are always finished when in this protocol */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Daniel Mack0da529a2010-05-31 00:35:13 -0700229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
Daniel Mack0da529a2010-05-31 00:35:13 -0700231
Joe Perches7f5e7602015-05-05 10:05:49 -0700232static struct ArcProto capmode_proto = {
Daniel Mack0da529a2010-05-31 00:35:13 -0700233 'r',
234 XMTU,
235 0,
236 rx,
237 build_header,
238 prepare_tx,
239 NULL,
240 ack_tx
241};
242
243static void arcnet_cap_init(void)
244{
245 int count;
246
247 for (count = 1; count <= 8; count++)
248 if (arc_proto_map[count] == arc_proto_default)
249 arc_proto_map[count] = &capmode_proto;
250
251 /* for cap mode, we only set the bcast proto if there's no better one */
252 if (arc_bcast_proto == arc_proto_default)
253 arc_bcast_proto = &capmode_proto;
254
255 arc_proto_default = &capmode_proto;
256 arc_raw_proto = &capmode_proto;
257}
258
259static int __init capmode_module_init(void)
260{
261 printk(VERSION);
262 arcnet_cap_init();
263 return 0;
264}
265
266static void __exit capmode_module_exit(void)
267{
268 arcnet_unregister_proto(&capmode_proto);
269}
270module_init(capmode_module_init);
271module_exit(capmode_module_exit);
272
273MODULE_LICENSE("GPL");