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