Merge branch 'devel'
[linux-2.6] / drivers / net / arcnet / rfc1051.c
1 /*
2  * Linux ARCnet driver - RFC1051 ("simple" standard) packet encapsulation
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 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/if_arp.h>
29 #include <net/arp.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/arcdevice.h>
33
34 #define VERSION "arcnet: RFC1051 \"simple standard\" (`s') encapsulation support loaded.\n"
35
36
37 static __be16 type_trans(struct sk_buff *skb, struct net_device *dev);
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 static struct ArcProto rfc1051_proto =
47 {
48         .suffix         = 's',
49         .mtu            = XMTU - RFC1051_HDR_SIZE,
50         .is_ip          = 1,
51         .rx             = rx,
52         .build_header   = build_header,
53         .prepare_tx     = prepare_tx,
54         .continue_tx    = NULL,
55         .ack_tx         = NULL
56 };
57
58
59 static int __init arcnet_rfc1051_init(void)
60 {
61         printk(VERSION);
62
63         arc_proto_map[ARC_P_IP_RFC1051]
64             = arc_proto_map[ARC_P_ARP_RFC1051]
65             = &rfc1051_proto;
66
67         /* if someone else already owns the broadcast, we won't take it */
68         if (arc_bcast_proto == arc_proto_default)
69                 arc_bcast_proto = &rfc1051_proto;
70
71         return 0;
72 }
73
74 static void __exit arcnet_rfc1051_exit(void)
75 {
76         arcnet_unregister_proto(&rfc1051_proto);
77 }
78
79 module_init(arcnet_rfc1051_init);
80 module_exit(arcnet_rfc1051_exit);
81
82 MODULE_LICENSE("GPL");
83
84 /*
85  * Determine a packet's protocol ID.
86  * 
87  * With ARCnet we have to convert everything to Ethernet-style stuff.
88  */
89 static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
90 {
91         struct arcnet_local *lp = netdev_priv(dev);
92         struct archdr *pkt = (struct archdr *) skb->data;
93         struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
94         int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
95
96         /* Pull off the arcnet header. */
97         skb_reset_mac_header(skb);
98         skb_pull(skb, hdr_size);
99
100         if (pkt->hard.dest == 0)
101                 skb->pkt_type = PACKET_BROADCAST;
102         else if (dev->flags & IFF_PROMISC) {
103                 /* if we're not sending to ourselves :) */
104                 if (pkt->hard.dest != dev->dev_addr[0])
105                         skb->pkt_type = PACKET_OTHERHOST;
106         }
107         /* now return the protocol number */
108         switch (soft->proto) {
109         case ARC_P_IP_RFC1051:
110                 return htons(ETH_P_IP);
111         case ARC_P_ARP_RFC1051:
112                 return htons(ETH_P_ARP);
113
114         default:
115                 lp->stats.rx_errors++;
116                 lp->stats.rx_crc_errors++;
117                 return 0;
118         }
119
120         return htons(ETH_P_IP);
121 }
122
123
124 /* packet receiver */
125 static void rx(struct net_device *dev, int bufnum,
126                struct archdr *pkthdr, int length)
127 {
128         struct arcnet_local *lp = netdev_priv(dev);
129         struct sk_buff *skb;
130         struct archdr *pkt = pkthdr;
131         int ofs;
132
133         BUGMSG(D_DURING, "it's a raw packet (length=%d)\n", length);
134
135         if (length >= MinTU)
136                 ofs = 512 - length;
137         else
138                 ofs = 256 - length;
139
140         skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
141         if (skb == NULL) {
142                 BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
143                 lp->stats.rx_dropped++;
144                 return;
145         }
146         skb_put(skb, length + ARC_HDR_SIZE);
147         skb->dev = dev;
148
149         pkt = (struct archdr *) skb->data;
150
151         /* up to sizeof(pkt->soft) has already been copied from the card */
152         memcpy(pkt, pkthdr, sizeof(struct archdr));
153         if (length > sizeof(pkt->soft))
154                 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
155                                       pkt->soft.raw + sizeof(pkt->soft),
156                                       length - sizeof(pkt->soft));
157
158         BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
159
160         skb->protocol = type_trans(skb, dev);
161         netif_rx(skb);
162 }
163
164
165 /*
166  * Create the ARCnet hard/soft headers for RFC1051.
167  */
168 static int build_header(struct sk_buff *skb, struct net_device *dev,
169                         unsigned short type, uint8_t daddr)
170 {
171         struct arcnet_local *lp = netdev_priv(dev);
172         int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
173         struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
174         struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
175
176         /* set the protocol ID according to RFC1051 */
177         switch (type) {
178         case ETH_P_IP:
179                 soft->proto = ARC_P_IP_RFC1051;
180                 break;
181         case ETH_P_ARP:
182                 soft->proto = ARC_P_ARP_RFC1051;
183                 break;
184         default:
185                 BUGMSG(D_NORMAL, "RFC1051: I don't understand protocol %d (%Xh)\n",
186                        type, type);
187                 lp->stats.tx_errors++;
188                 lp->stats.tx_aborted_errors++;
189                 return 0;
190         }
191
192
193         /*
194          * Set the source hardware address.
195          *
196          * This is pretty pointless for most purposes, but it can help in
197          * debugging.  ARCnet does not allow us to change the source address in
198          * the actual packet sent)
199          */
200         pkt->hard.source = *dev->dev_addr;
201
202         /* see linux/net/ethernet/eth.c to see where I got the following */
203
204         if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
205                 /* 
206                  * FIXME: fill in the last byte of the dest ipaddr here to better
207                  * comply with RFC1051 in "noarp" mode.
208                  */
209                 pkt->hard.dest = 0;
210                 return hdr_size;
211         }
212         /* otherwise, just fill it in and go! */
213         pkt->hard.dest = daddr;
214
215         return hdr_size;        /* success */
216 }
217
218
219 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
220                       int bufnum)
221 {
222         struct arcnet_local *lp = netdev_priv(dev);
223         struct arc_hardware *hard = &pkt->hard;
224         int ofs;
225
226         BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
227                lp->next_tx, lp->cur_tx, bufnum);
228
229         length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
230
231         if (length > XMTU) {
232                 /* should never happen! other people already check for this. */
233                 BUGMSG(D_NORMAL, "Bug!  prepare_tx with size %d (> %d)\n",
234                        length, XMTU);
235                 length = XMTU;
236         }
237         if (length > MinTU) {
238                 hard->offset[0] = 0;
239                 hard->offset[1] = ofs = 512 - length;
240         } else if (length > MTU) {
241                 hard->offset[0] = 0;
242                 hard->offset[1] = ofs = 512 - length - 3;
243         } else
244                 hard->offset[0] = ofs = 256 - length;
245
246         lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
247         lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
248
249         lp->lastload_dest = hard->dest;
250
251         return 1;               /* done */
252 }