Merge branch 'slabh' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/misc
[sfrench/cifs-2.6.git] / drivers / net / arcnet / capmode.c
1 /*
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>
30 #include <linux/gfp.h>
31 #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
40
41 static void rx(struct net_device *dev, int bufnum,
42                struct archdr *pkthdr, int length);
43 static int build_header(struct sk_buff *skb,
44                         struct net_device *dev,
45                         unsigned short type,
46                         uint8_t daddr);
47 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
48                       int bufnum);
49 static int ack_tx(struct net_device *dev, int acked);
50
51
52 static struct ArcProto capmode_proto =
53 {
54         'r',
55         XMTU,
56         0,
57         rx,
58         build_header,
59         prepare_tx,
60         NULL,
61         ack_tx
62 };
63
64
65 static void arcnet_cap_init(void)
66 {
67         int count;
68
69         for (count = 1; count <= 8; count++)
70                 if (arc_proto_map[count] == arc_proto_default)
71                         arc_proto_map[count] = &capmode_proto;
72
73         /* for cap mode, we only set the bcast proto if there's no better one */
74         if (arc_bcast_proto == arc_proto_default)
75                 arc_bcast_proto = &capmode_proto;
76
77         arc_proto_default = &capmode_proto;
78         arc_raw_proto = &capmode_proto;
79 }
80
81
82 #ifdef MODULE
83
84 static int __init capmode_module_init(void)
85 {
86         printk(VERSION);
87         arcnet_cap_init();
88         return 0;
89 }
90
91 static void __exit capmode_module_exit(void)
92 {
93         arcnet_unregister_proto(&capmode_proto);
94 }
95 module_init(capmode_module_init);
96 module_exit(capmode_module_exit);
97
98 MODULE_LICENSE("GPL");
99 #endif                          /* MODULE */
100
101
102
103 /* packet receiver */
104 static void rx(struct net_device *dev, int bufnum,
105                struct archdr *pkthdr, int length)
106 {
107         struct arcnet_local *lp = netdev_priv(dev);
108         struct sk_buff *skb;
109         struct archdr *pkt = pkthdr;
110         char *pktbuf, *pkthdrbuf;
111         int ofs;
112
113         BUGMSG(D_DURING, "it's a raw(cap) packet (length=%d)\n", length);
114
115         if (length >= MinTU)
116                 ofs = 512 - length;
117         else
118                 ofs = 256 - length;
119
120         skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC);
121         if (skb == NULL) {
122                 BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
123                 dev->stats.rx_dropped++;
124                 return;
125         }
126         skb_put(skb, length + ARC_HDR_SIZE + sizeof(int));
127         skb->dev = dev;
128         skb_reset_mac_header(skb);
129         pkt = (struct archdr *)skb_mac_header(skb);
130         skb_pull(skb, ARC_HDR_SIZE);
131
132         /* up to sizeof(pkt->soft) has already been copied from the card */
133         /* squeeze in an int for the cap encapsulation */
134
135         /* use these variables to be sure we count in bytes, not in
136            sizeof(struct archdr) */
137         pktbuf=(char*)pkt;
138         pkthdrbuf=(char*)pkthdr;
139         memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto));
140         memcpy(pktbuf+ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto)+sizeof(int),
141                pkthdrbuf+ARC_HDR_SIZE+sizeof(pkt->soft.cap.proto),
142                sizeof(struct archdr)-ARC_HDR_SIZE-sizeof(pkt->soft.cap.proto));
143
144         if (length > sizeof(pkt->soft))
145                 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
146                                       pkt->soft.raw + sizeof(pkt->soft)
147                                       + sizeof(int),
148                                       length - sizeof(pkt->soft));
149
150         BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
151
152         skb->protocol = cpu_to_be16(ETH_P_ARCNET);
153         netif_rx(skb);
154 }
155
156
157 /*
158  * Create the ARCnet hard/soft headers for cap mode.
159  * There aren't any soft headers in cap mode - not even the protocol id.
160  */
161 static int build_header(struct sk_buff *skb,
162                         struct net_device *dev,
163                         unsigned short type,
164                         uint8_t daddr)
165 {
166         int hdr_size = ARC_HDR_SIZE;
167         struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
168
169         BUGMSG(D_PROTO, "Preparing header for cap packet %x.\n",
170                *((int*)&pkt->soft.cap.cookie[0]));
171         /*
172          * Set the source hardware address.
173          *
174          * This is pretty pointless for most purposes, but it can help in
175          * debugging.  ARCnet does not allow us to change the source address in
176          * the actual packet sent)
177          */
178         pkt->hard.source = *dev->dev_addr;
179
180         /* see linux/net/ethernet/eth.c to see where I got the following */
181
182         if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
183                 /*
184                  * FIXME: fill in the last byte of the dest ipaddr here to better
185                  * comply with RFC1051 in "noarp" mode.
186                  */
187                 pkt->hard.dest = 0;
188                 return hdr_size;
189         }
190         /* otherwise, just fill it in and go! */
191         pkt->hard.dest = daddr;
192
193         return hdr_size;        /* success */
194 }
195
196
197 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
198                       int bufnum)
199 {
200         struct arcnet_local *lp = netdev_priv(dev);
201         struct arc_hardware *hard = &pkt->hard;
202         int ofs;
203
204
205         /* hard header is not included in packet length */
206         length -= ARC_HDR_SIZE;
207         /* And neither is the cookie field */
208         length -= sizeof(int);
209
210         BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
211                lp->next_tx, lp->cur_tx, bufnum);
212
213         BUGMSG(D_PROTO, "Sending for cap packet %x.\n",
214                *((int*)&pkt->soft.cap.cookie[0]));
215
216         if (length > XMTU) {
217                 /* should never happen! other people already check for this. */
218                 BUGMSG(D_NORMAL, "Bug!  prepare_tx with size %d (> %d)\n",
219                        length, XMTU);
220                 length = XMTU;
221         }
222         if (length > MinTU) {
223                 hard->offset[0] = 0;
224                 hard->offset[1] = ofs = 512 - length;
225         } else if (length > MTU) {
226                 hard->offset[0] = 0;
227                 hard->offset[1] = ofs = 512 - length - 3;
228         } else
229                 hard->offset[0] = ofs = 256 - length;
230
231         BUGMSG(D_DURING, "prepare_tx: length=%d ofs=%d\n",
232                length,ofs);
233
234         // Copy the arcnet-header + the protocol byte down:
235         lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
236         lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
237                             sizeof(pkt->soft.cap.proto));
238
239         // Skip the extra integer we have written into it as a cookie
240         // but write the rest of the message:
241         lp->hw.copy_to_card(dev, bufnum, ofs+1,
242                             ((unsigned char*)&pkt->soft.cap.mes),length-1);
243
244         lp->lastload_dest = hard->dest;
245
246         return 1;               /* done */
247 }
248
249
250 static int ack_tx(struct net_device *dev, int acked)
251 {
252   struct arcnet_local *lp = netdev_priv(dev);
253   struct sk_buff *ackskb;
254   struct archdr *ackpkt;
255   int length=sizeof(struct arc_cap);
256
257   BUGMSG(D_DURING, "capmode: ack_tx: protocol: %x: result: %d\n",
258          lp->outgoing.skb->protocol, acked);
259
260   BUGLVL(D_SKB) arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
261
262   /* Now alloc a skb to send back up through the layers: */
263   ackskb = alloc_skb(length + ARC_HDR_SIZE , GFP_ATOMIC);
264   if (ackskb == NULL) {
265           BUGMSG(D_NORMAL, "Memory squeeze, can't acknowledge.\n");
266           goto free_outskb;
267   }
268
269   skb_put(ackskb, length + ARC_HDR_SIZE );
270   ackskb->dev = dev;
271
272   skb_reset_mac_header(ackskb);
273   ackpkt = (struct archdr *)skb_mac_header(ackskb);
274   /* skb_pull(ackskb, ARC_HDR_SIZE); */
275
276
277   skb_copy_from_linear_data(lp->outgoing.skb, ackpkt,
278                 ARC_HDR_SIZE + sizeof(struct arc_cap));
279   ackpkt->soft.cap.proto=0; /* using protocol 0 for acknowledge */
280   ackpkt->soft.cap.mes.ack=acked;
281
282   BUGMSG(D_PROTO, "Ackknowledge for cap packet %x.\n",
283          *((int*)&ackpkt->soft.cap.cookie[0]));
284
285   ackskb->protocol = cpu_to_be16(ETH_P_ARCNET);
286
287   BUGLVL(D_SKB) arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
288   netif_rx(ackskb);
289
290  free_outskb:
291   dev_kfree_skb_irq(lp->outgoing.skb);
292   lp->outgoing.proto = NULL; /* We are always finished when in this protocol */
293
294   return 0;
295 }