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