arcnet: Add and remove blank lines
[linux-2.6-block.git] / drivers / net / arcnet / rfc1201.c
CommitLineData
1da177e4
LT
1/*
2 * Linux ARCnet driver - RFC1201 (standard) packet encapsulation
cb334648 3 *
1da177e4
LT
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 */
5a0e3ad6 26#include <linux/gfp.h>
1da177e4
LT
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/if_arp.h>
30#include <linux/netdevice.h>
31#include <linux/skbuff.h>
32#include <linux/arcdevice.h>
33
34MODULE_LICENSE("GPL");
35#define VERSION "arcnet: RFC1201 \"standard\" (`a') encapsulation support loaded.\n"
36
701181ac 37static __be16 type_trans(struct sk_buff *skb, struct net_device *dev);
1da177e4
LT
38static void rx(struct net_device *dev, int bufnum,
39 struct archdr *pkthdr, int length);
40static int build_header(struct sk_buff *skb, struct net_device *dev,
41 unsigned short type, uint8_t daddr);
42static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
43 int bufnum);
44static int continue_tx(struct net_device *dev, int bufnum);
45
f03aa2d8 46static struct ArcProto rfc1201_proto =
1da177e4
LT
47{
48 .suffix = 'a',
49 .mtu = 1500, /* could be more, but some receivers can't handle it... */
50 .is_ip = 1, /* This is for sending IP and ARP packages */
51 .rx = rx,
52 .build_header = build_header,
53 .prepare_tx = prepare_tx,
54 .continue_tx = continue_tx,
55 .ack_tx = NULL
56};
57
1da177e4
LT
58static int __init arcnet_rfc1201_init(void)
59{
60 printk(VERSION);
61
62 arc_proto_map[ARC_P_IP]
63 = arc_proto_map[ARC_P_IPV6]
64 = arc_proto_map[ARC_P_ARP]
65 = arc_proto_map[ARC_P_RARP]
66 = arc_proto_map[ARC_P_IPX]
67 = arc_proto_map[ARC_P_NOVELL_EC]
68 = &rfc1201_proto;
69
70 /* if someone else already owns the broadcast, we won't take it */
71 if (arc_bcast_proto == arc_proto_default)
72 arc_bcast_proto = &rfc1201_proto;
73
74 return 0;
75}
76
77static void __exit arcnet_rfc1201_exit(void)
78{
79 arcnet_unregister_proto(&rfc1201_proto);
80}
81
82module_init(arcnet_rfc1201_init);
83module_exit(arcnet_rfc1201_exit);
84
85/*
86 * Determine a packet's protocol ID.
cb334648 87 *
1da177e4
LT
88 * With ARCnet we have to convert everything to Ethernet-style stuff.
89 */
701181ac 90static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
1da177e4 91{
cb334648 92 struct archdr *pkt = (struct archdr *)skb->data;
1da177e4 93 struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
1da177e4
LT
94 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
95
96 /* Pull off the arcnet header. */
459a98ed 97 skb_reset_mac_header(skb);
1da177e4
LT
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:
110 return htons(ETH_P_IP);
111 case ARC_P_IPV6:
112 return htons(ETH_P_IPV6);
113 case ARC_P_ARP:
114 return htons(ETH_P_ARP);
115 case ARC_P_RARP:
116 return htons(ETH_P_RARP);
117
118 case ARC_P_IPX:
119 case ARC_P_NOVELL_EC:
120 return htons(ETH_P_802_3);
121 default:
5803c512
SH
122 dev->stats.rx_errors++;
123 dev->stats.rx_crc_errors++;
1da177e4
LT
124 return 0;
125 }
126
127 return htons(ETH_P_IP);
128}
129
1da177e4
LT
130/* packet receiver */
131static void rx(struct net_device *dev, int bufnum,
132 struct archdr *pkthdr, int length)
133{
454d7c9b 134 struct arcnet_local *lp = netdev_priv(dev);
1da177e4
LT
135 struct sk_buff *skb;
136 struct archdr *pkt = pkthdr;
137 struct arc_rfc1201 *soft = &pkthdr->soft.rfc1201;
138 int saddr = pkt->hard.source, ofs;
139 struct Incoming *in = &lp->rfc1201.incoming[saddr];
140
141 BUGMSG(D_DURING, "it's an RFC1201 packet (length=%d)\n", length);
142
143 if (length >= MinTU)
144 ofs = 512 - length;
145 else
146 ofs = 256 - length;
147
148 if (soft->split_flag == 0xFF) { /* Exception Packet */
149 if (length >= 4 + RFC1201_HDR_SIZE)
150 BUGMSG(D_DURING, "compensating for exception packet\n");
151 else {
152 BUGMSG(D_EXTRA, "short RFC1201 exception packet from %02Xh",
153 saddr);
154 return;
155 }
156
157 /* skip over 4-byte junkola */
158 length -= 4;
159 ofs += 4;
160 lp->hw.copy_from_card(dev, bufnum, 512 - length,
161 soft, sizeof(pkt->soft));
162 }
163 if (!soft->split_flag) { /* not split */
164 BUGMSG(D_RX, "incoming is not split (splitflag=%d)\n",
165 soft->split_flag);
166
167 if (in->skb) { /* already assembling one! */
168 BUGMSG(D_EXTRA, "aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
cb334648 169 in->sequence, soft->split_flag, soft->sequence);
1da177e4
LT
170 lp->rfc1201.aborted_seq = soft->sequence;
171 dev_kfree_skb_irq(in->skb);
5803c512
SH
172 dev->stats.rx_errors++;
173 dev->stats.rx_missed_errors++;
1da177e4
LT
174 in->skb = NULL;
175 }
176 in->sequence = soft->sequence;
177
178 skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
179 if (skb == NULL) {
180 BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
5803c512 181 dev->stats.rx_dropped++;
1da177e4
LT
182 return;
183 }
184 skb_put(skb, length + ARC_HDR_SIZE);
185 skb->dev = dev;
186
cb334648 187 pkt = (struct archdr *)skb->data;
1da177e4
LT
188 soft = &pkt->soft.rfc1201;
189
190 /* up to sizeof(pkt->soft) has already been copied from the card */
191 memcpy(pkt, pkthdr, sizeof(struct archdr));
192 if (length > sizeof(pkt->soft))
193 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
194 pkt->soft.raw + sizeof(pkt->soft),
195 length - sizeof(pkt->soft));
196
197 /*
198 * ARP packets have problems when sent from some DOS systems: the
199 * source address is always 0! So we take the hardware source addr
200 * (which is impossible to fumble) and insert it ourselves.
201 */
202 if (soft->proto == ARC_P_ARP) {
cb334648 203 struct arphdr *arp = (struct arphdr *)soft->payload;
1da177e4
LT
204
205 /* make sure addresses are the right length */
206 if (arp->ar_hln == 1 && arp->ar_pln == 4) {
cb334648 207 uint8_t *cptr = (uint8_t *)arp + sizeof(struct arphdr);
1da177e4
LT
208
209 if (!*cptr) { /* is saddr = 00? */
210 BUGMSG(D_EXTRA,
211 "ARP source address was 00h, set to %02Xh.\n",
212 saddr);
5803c512 213 dev->stats.rx_crc_errors++;
1da177e4
LT
214 *cptr = saddr;
215 } else {
216 BUGMSG(D_DURING, "ARP source address (%Xh) is fine.\n",
217 *cptr);
218 }
219 } else {
220 BUGMSG(D_NORMAL, "funny-shaped ARP packet. (%Xh, %Xh)\n",
221 arp->ar_hln, arp->ar_pln);
5803c512
SH
222 dev->stats.rx_errors++;
223 dev->stats.rx_crc_errors++;
1da177e4
LT
224 }
225 }
226 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
227
228 skb->protocol = type_trans(skb, dev);
229 netif_rx(skb);
1da177e4
LT
230 } else { /* split packet */
231 /*
232 * NOTE: MSDOS ARP packet correction should only need to apply to
233 * unsplit packets, since ARP packets are so short.
234 *
235 * My interpretation of the RFC1201 document is that if a packet is
236 * received out of order, the entire assembly process should be
237 * aborted.
238 *
239 * The RFC also mentions "it is possible for successfully received
240 * packets to be retransmitted." As of 0.40 all previously received
241 * packets are allowed, not just the most recent one.
242 *
243 * We allow multiple assembly processes, one for each ARCnet card
244 * possible on the network. Seems rather like a waste of memory,
245 * but there's no other way to be reliable.
246 */
247
248 BUGMSG(D_RX, "packet is split (splitflag=%d, seq=%d)\n",
249 soft->split_flag, in->sequence);
250
251 if (in->skb && in->sequence != soft->sequence) {
252 BUGMSG(D_EXTRA, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
253 saddr, in->sequence, soft->sequence,
254 soft->split_flag);
255 dev_kfree_skb_irq(in->skb);
256 in->skb = NULL;
5803c512
SH
257 dev->stats.rx_errors++;
258 dev->stats.rx_missed_errors++;
1da177e4
LT
259 in->lastpacket = in->numpackets = 0;
260 }
261 if (soft->split_flag & 1) { /* first packet in split */
262 BUGMSG(D_RX, "brand new splitpacket (splitflag=%d)\n",
263 soft->split_flag);
264 if (in->skb) { /* already assembling one! */
265 BUGMSG(D_EXTRA, "aborting previous (seq=%d) assembly "
266 "(splitflag=%d, seq=%d)\n",
267 in->sequence, soft->split_flag,
268 soft->sequence);
5803c512
SH
269 dev->stats.rx_errors++;
270 dev->stats.rx_missed_errors++;
1da177e4
LT
271 dev_kfree_skb_irq(in->skb);
272 }
273 in->sequence = soft->sequence;
cb334648 274 in->numpackets = ((unsigned)soft->split_flag >> 1) + 2;
1da177e4
LT
275 in->lastpacket = 1;
276
277 if (in->numpackets > 16) {
278 BUGMSG(D_EXTRA, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
279 soft->split_flag);
280 lp->rfc1201.aborted_seq = soft->sequence;
5803c512
SH
281 dev->stats.rx_errors++;
282 dev->stats.rx_length_errors++;
1da177e4
LT
283 return;
284 }
285 in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
286 GFP_ATOMIC);
287 if (skb == NULL) {
288 BUGMSG(D_NORMAL, "(split) memory squeeze, dropping packet.\n");
289 lp->rfc1201.aborted_seq = soft->sequence;
5803c512 290 dev->stats.rx_dropped++;
1da177e4
LT
291 return;
292 }
293 skb->dev = dev;
cb334648 294 pkt = (struct archdr *)skb->data;
1da177e4
LT
295 soft = &pkt->soft.rfc1201;
296
297 memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
298 skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
299
300 soft->split_flag = 0; /* end result won't be split */
301 } else { /* not first packet */
cb334648 302 int packetnum = ((unsigned)soft->split_flag >> 1) + 1;
1da177e4
LT
303
304 /*
305 * if we're not assembling, there's no point trying to
306 * continue.
307 */
308 if (!in->skb) {
309 if (lp->rfc1201.aborted_seq != soft->sequence) {
310 BUGMSG(D_EXTRA, "can't continue split without starting "
311 "first! (splitflag=%d, seq=%d, aborted=%d)\n",
312 soft->split_flag, soft->sequence,
313 lp->rfc1201.aborted_seq);
5803c512
SH
314 dev->stats.rx_errors++;
315 dev->stats.rx_missed_errors++;
1da177e4
LT
316 }
317 return;
318 }
319 in->lastpacket++;
320 if (packetnum != in->lastpacket) { /* not the right flag! */
321 /* harmless duplicate? ignore. */
322 if (packetnum <= in->lastpacket - 1) {
323 BUGMSG(D_EXTRA, "duplicate splitpacket ignored! (splitflag=%d)\n",
324 soft->split_flag);
5803c512
SH
325 dev->stats.rx_errors++;
326 dev->stats.rx_frame_errors++;
1da177e4
LT
327 return;
328 }
329 /* "bad" duplicate, kill reassembly */
330 BUGMSG(D_EXTRA, "out-of-order splitpacket, reassembly "
331 "(seq=%d) aborted (splitflag=%d, seq=%d)\n",
332 in->sequence, soft->split_flag, soft->sequence);
333 lp->rfc1201.aborted_seq = soft->sequence;
334 dev_kfree_skb_irq(in->skb);
335 in->skb = NULL;
5803c512
SH
336 dev->stats.rx_errors++;
337 dev->stats.rx_missed_errors++;
1da177e4
LT
338 in->lastpacket = in->numpackets = 0;
339 return;
340 }
cb334648 341 pkt = (struct archdr *)in->skb->data;
1da177e4
LT
342 soft = &pkt->soft.rfc1201;
343 }
344
345 skb = in->skb;
346
347 lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
348 skb->data + skb->len,
349 length - RFC1201_HDR_SIZE);
350 skb_put(skb, length - RFC1201_HDR_SIZE);
351
352 /* are we done? */
353 if (in->lastpacket == in->numpackets) {
354 in->skb = NULL;
355 in->lastpacket = in->numpackets = 0;
356
cb334648
JP
357 BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (unsplit)\n",
358 skb->len, pkt->hard.source);
359 BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (split)\n",
360 skb->len, pkt->hard.source);
1da177e4
LT
361 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
362
363 skb->protocol = type_trans(skb, dev);
364 netif_rx(skb);
1da177e4
LT
365 }
366 }
367}
368
1da177e4
LT
369/* Create the ARCnet hard/soft headers for RFC1201. */
370static int build_header(struct sk_buff *skb, struct net_device *dev,
371 unsigned short type, uint8_t daddr)
372{
454d7c9b 373 struct arcnet_local *lp = netdev_priv(dev);
1da177e4 374 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
cb334648 375 struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
1da177e4
LT
376 struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
377
378 /* set the protocol ID according to RFC1201 */
379 switch (type) {
380 case ETH_P_IP:
381 soft->proto = ARC_P_IP;
382 break;
383 case ETH_P_IPV6:
384 soft->proto = ARC_P_IPV6;
385 break;
386 case ETH_P_ARP:
387 soft->proto = ARC_P_ARP;
388 break;
389 case ETH_P_RARP:
390 soft->proto = ARC_P_RARP;
391 break;
392 case ETH_P_IPX:
393 case ETH_P_802_3:
394 case ETH_P_802_2:
395 soft->proto = ARC_P_IPX;
396 break;
397 case ETH_P_ATALK:
398 soft->proto = ARC_P_ATALK;
399 break;
400 default:
401 BUGMSG(D_NORMAL, "RFC1201: I don't understand protocol %d (%Xh)\n",
402 type, type);
5803c512
SH
403 dev->stats.tx_errors++;
404 dev->stats.tx_aborted_errors++;
1da177e4
LT
405 return 0;
406 }
407
408 /*
409 * Set the source hardware address.
410 *
411 * This is pretty pointless for most purposes, but it can help in
412 * debugging. ARCnet does not allow us to change the source address in
413 * the actual packet sent)
414 */
415 pkt->hard.source = *dev->dev_addr;
416
417 soft->sequence = htons(lp->rfc1201.sequence++);
418 soft->split_flag = 0; /* split packets are done elsewhere */
419
420 /* see linux/net/ethernet/eth.c to see where I got the following */
421
422 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
cb334648 423 /*
1da177e4
LT
424 * FIXME: fill in the last byte of the dest ipaddr here to better
425 * comply with RFC1051 in "noarp" mode. For now, always broadcasting
426 * will probably at least get packets sent out :)
427 */
428 pkt->hard.dest = 0;
429 return hdr_size;
430 }
431 /* otherwise, drop in the dest address */
432 pkt->hard.dest = daddr;
433 return hdr_size;
434}
435
1da177e4
LT
436static void load_pkt(struct net_device *dev, struct arc_hardware *hard,
437 struct arc_rfc1201 *soft, int softlen, int bufnum)
438{
454d7c9b 439 struct arcnet_local *lp = netdev_priv(dev);
1da177e4
LT
440 int ofs;
441
442 /* assume length <= XMTU: someone should have handled that by now. */
443
444 if (softlen > MinTU) {
445 hard->offset[0] = 0;
446 hard->offset[1] = ofs = 512 - softlen;
447 } else if (softlen > MTU) { /* exception packet - add an extra header */
448 struct arc_rfc1201 excsoft;
449
450 excsoft.proto = soft->proto;
451 excsoft.split_flag = 0xff;
701181ac 452 excsoft.sequence = htons(0xffff);
1da177e4
LT
453
454 hard->offset[0] = 0;
455 ofs = 512 - softlen;
456 hard->offset[1] = ofs - RFC1201_HDR_SIZE;
457 lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE,
458 &excsoft, RFC1201_HDR_SIZE);
459 } else
460 hard->offset[0] = ofs = 256 - softlen;
461
462 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
463 lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
464
465 lp->lastload_dest = hard->dest;
466}
467
1da177e4
LT
468static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
469 int bufnum)
470{
454d7c9b 471 struct arcnet_local *lp = netdev_priv(dev);
1da177e4
LT
472 const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
473 struct Outgoing *out;
474
1da177e4
LT
475 BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
476 lp->next_tx, lp->cur_tx, bufnum);
477
478 length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
479 pkt->soft.rfc1201.split_flag = 0;
480
481 /* need to do a split packet? */
482 if (length > XMTU) {
483 out = &lp->outgoing;
484
485 out->length = length - RFC1201_HDR_SIZE;
486 out->dataleft = lp->outgoing.length;
487 out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
488 out->segnum = 0;
489
490 BUGMSG(D_DURING, "rfc1201 prep_tx: ready for %d-segment split "
491 "(%d bytes, seq=%d)\n", out->numsegs, out->length,
492 pkt->soft.rfc1201.sequence);
493
494 return 0; /* not done */
495 }
496 /* just load the packet into the buffers and send it off */
497 load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
498
499 return 1; /* done */
500}
501
1da177e4
LT
502static int continue_tx(struct net_device *dev, int bufnum)
503{
454d7c9b 504 struct arcnet_local *lp = netdev_priv(dev);
1da177e4
LT
505 struct Outgoing *out = &lp->outgoing;
506 struct arc_hardware *hard = &out->pkt->hard;
507 struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
508 int maxsegsize = XMTU - RFC1201_HDR_SIZE;
509 int seglen;
510
511 BUGMSG(D_DURING,
cb334648 512 "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
1da177e4
LT
513 out->segnum, out->numsegs, soft->sequence);
514
515 /* the "new" soft header comes right before the data chunk */
516 newsoft = (struct arc_rfc1201 *)
517 (out->pkt->soft.raw + out->length - out->dataleft);
518
519 if (!out->segnum) /* first packet; newsoft == soft */
520 newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
521 else {
522 newsoft->split_flag = out->segnum << 1;
523 newsoft->proto = soft->proto;
524 newsoft->sequence = soft->sequence;
525 }
526
527 seglen = maxsegsize;
528 if (seglen > out->dataleft)
529 seglen = out->dataleft;
530 out->dataleft -= seglen;
531
532 load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
533
534 out->segnum++;
535 if (out->segnum >= out->numsegs)
536 return 1;
537 else
538 return 0;
539}