arcnet: Convert printk to pr_<level>
[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 == NULL) {
182                         arc_printk(D_NORMAL, dev, "Memory squeeze, dropping packet\n");
183                         dev->stats.rx_dropped++;
184                         return;
185                 }
186                 skb_put(skb, length + ARC_HDR_SIZE);
187                 skb->dev = dev;
188
189                 pkt = (struct archdr *)skb->data;
190                 soft = &pkt->soft.rfc1201;
191
192                 /* up to sizeof(pkt->soft) has already been copied from the card */
193                 memcpy(pkt, pkthdr, sizeof(struct archdr));
194                 if (length > sizeof(pkt->soft))
195                         lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
196                                        pkt->soft.raw + sizeof(pkt->soft),
197                                               length - sizeof(pkt->soft));
198
199                 /* ARP packets have problems when sent from some DOS systems:
200                  * the source address is always 0!
201                  * So we take the hardware source addr (which is impossible
202                  * to fumble) and insert it ourselves.
203                  */
204                 if (soft->proto == ARC_P_ARP) {
205                         struct arphdr *arp = (struct arphdr *)soft->payload;
206
207                         /* make sure addresses are the right length */
208                         if (arp->ar_hln == 1 && arp->ar_pln == 4) {
209                                 uint8_t *cptr = (uint8_t *)arp + sizeof(struct arphdr);
210
211                                 if (!*cptr) {   /* is saddr = 00? */
212                                         arc_printk(D_EXTRA, dev,
213                                                    "ARP source address was 00h, set to %02Xh\n",
214                                                    saddr);
215                                         dev->stats.rx_crc_errors++;
216                                         *cptr = saddr;
217                                 } else {
218                                         arc_printk(D_DURING, dev, "ARP source address (%Xh) is fine.\n",
219                                                    *cptr);
220                                 }
221                         } else {
222                                 arc_printk(D_NORMAL, dev, "funny-shaped ARP packet. (%Xh, %Xh)\n",
223                                            arp->ar_hln, arp->ar_pln);
224                                 dev->stats.rx_errors++;
225                                 dev->stats.rx_crc_errors++;
226                         }
227                 }
228                 if (BUGLVL(D_SKB))
229                         arcnet_dump_skb(dev, skb, "rx");
230
231                 skb->protocol = type_trans(skb, dev);
232                 netif_rx(skb);
233         } else {                /* split packet */
234                 /* NOTE: MSDOS ARP packet correction should only need to
235                  * apply to unsplit packets, since ARP packets are so short.
236                  *
237                  * My interpretation of the RFC1201 document is that if a
238                  * packet is received out of order, the entire assembly
239                  * process should be aborted.
240                  *
241                  * The RFC also mentions "it is possible for successfully
242                  * received packets to be retransmitted." As of 0.40 all
243                  * previously received packets are allowed, not just the
244                  * most recent one.
245                  *
246                  * We allow multiple assembly processes, one for each
247                  * ARCnet card possible on the network.
248                  * Seems rather like a waste of memory, but there's no
249                  * other way to be reliable.
250                  */
251
252                 arc_printk(D_RX, dev, "packet is split (splitflag=%d, seq=%d)\n",
253                            soft->split_flag, in->sequence);
254
255                 if (in->skb && in->sequence != soft->sequence) {
256                         arc_printk(D_EXTRA, dev, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
257                                    saddr, in->sequence, soft->sequence,
258                                    soft->split_flag);
259                         dev_kfree_skb_irq(in->skb);
260                         in->skb = NULL;
261                         dev->stats.rx_errors++;
262                         dev->stats.rx_missed_errors++;
263                         in->lastpacket = in->numpackets = 0;
264                 }
265                 if (soft->split_flag & 1) {     /* first packet in split */
266                         arc_printk(D_RX, dev, "brand new splitpacket (splitflag=%d)\n",
267                                    soft->split_flag);
268                         if (in->skb) {  /* already assembling one! */
269                                 arc_printk(D_EXTRA, dev, "aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
270                                            in->sequence, soft->split_flag,
271                                            soft->sequence);
272                                 dev->stats.rx_errors++;
273                                 dev->stats.rx_missed_errors++;
274                                 dev_kfree_skb_irq(in->skb);
275                         }
276                         in->sequence = soft->sequence;
277                         in->numpackets = ((unsigned)soft->split_flag >> 1) + 2;
278                         in->lastpacket = 1;
279
280                         if (in->numpackets > 16) {
281                                 arc_printk(D_EXTRA, dev, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
282                                            soft->split_flag);
283                                 lp->rfc1201.aborted_seq = soft->sequence;
284                                 dev->stats.rx_errors++;
285                                 dev->stats.rx_length_errors++;
286                                 return;
287                         }
288                         in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
289                                                   GFP_ATOMIC);
290                         if (skb == NULL) {
291                                 arc_printk(D_NORMAL, dev, "(split) memory squeeze, dropping packet.\n");
292                                 lp->rfc1201.aborted_seq = soft->sequence;
293                                 dev->stats.rx_dropped++;
294                                 return;
295                         }
296                         skb->dev = dev;
297                         pkt = (struct archdr *)skb->data;
298                         soft = &pkt->soft.rfc1201;
299
300                         memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
301                         skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
302
303                         soft->split_flag = 0;   /* end result won't be split */
304                 } else {        /* not first packet */
305                         int packetnum = ((unsigned)soft->split_flag >> 1) + 1;
306
307                         /* if we're not assembling, there's no point trying to
308                          * continue.
309                          */
310                         if (!in->skb) {
311                                 if (lp->rfc1201.aborted_seq != soft->sequence) {
312                                         arc_printk(D_EXTRA, dev, "can't continue split without starting first! (splitflag=%d, seq=%d, aborted=%d)\n",
313                                                    soft->split_flag,
314                                                    soft->sequence,
315                                                    lp->rfc1201.aborted_seq);
316                                         dev->stats.rx_errors++;
317                                         dev->stats.rx_missed_errors++;
318                                 }
319                                 return;
320                         }
321                         in->lastpacket++;
322                         if (packetnum != in->lastpacket) {      /* not the right flag! */
323                                 /* harmless duplicate? ignore. */
324                                 if (packetnum <= in->lastpacket - 1) {
325                                         arc_printk(D_EXTRA, dev, "duplicate splitpacket ignored! (splitflag=%d)\n",
326                                                    soft->split_flag);
327                                         dev->stats.rx_errors++;
328                                         dev->stats.rx_frame_errors++;
329                                         return;
330                                 }
331                                 /* "bad" duplicate, kill reassembly */
332                                 arc_printk(D_EXTRA, dev, "out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",
333                                            in->sequence, soft->split_flag,
334                                            soft->sequence);
335                                 lp->rfc1201.aborted_seq = soft->sequence;
336                                 dev_kfree_skb_irq(in->skb);
337                                 in->skb = NULL;
338                                 dev->stats.rx_errors++;
339                                 dev->stats.rx_missed_errors++;
340                                 in->lastpacket = in->numpackets = 0;
341                                 return;
342                         }
343                         pkt = (struct archdr *)in->skb->data;
344                         soft = &pkt->soft.rfc1201;
345                 }
346
347                 skb = in->skb;
348
349                 lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
350                                       skb->data + skb->len,
351                                       length - RFC1201_HDR_SIZE);
352                 skb_put(skb, length - RFC1201_HDR_SIZE);
353
354                 /* are we done? */
355                 if (in->lastpacket == in->numpackets) {
356                         in->skb = NULL;
357                         in->lastpacket = in->numpackets = 0;
358
359                         arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (unsplit)\n",
360                                    skb->len, pkt->hard.source);
361                         arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (split)\n",
362                                    skb->len, pkt->hard.source);
363                         if (BUGLVL(D_SKB))
364                                 arcnet_dump_skb(dev, skb, "rx");
365
366                         skb->protocol = type_trans(skb, dev);
367                         netif_rx(skb);
368                 }
369         }
370 }
371
372 /* Create the ARCnet hard/soft headers for RFC1201. */
373 static int build_header(struct sk_buff *skb, struct net_device *dev,
374                         unsigned short type, uint8_t daddr)
375 {
376         struct arcnet_local *lp = netdev_priv(dev);
377         int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
378         struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
379         struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
380
381         /* set the protocol ID according to RFC1201 */
382         switch (type) {
383         case ETH_P_IP:
384                 soft->proto = ARC_P_IP;
385                 break;
386         case ETH_P_IPV6:
387                 soft->proto = ARC_P_IPV6;
388                 break;
389         case ETH_P_ARP:
390                 soft->proto = ARC_P_ARP;
391                 break;
392         case ETH_P_RARP:
393                 soft->proto = ARC_P_RARP;
394                 break;
395         case ETH_P_IPX:
396         case ETH_P_802_3:
397         case ETH_P_802_2:
398                 soft->proto = ARC_P_IPX;
399                 break;
400         case ETH_P_ATALK:
401                 soft->proto = ARC_P_ATALK;
402                 break;
403         default:
404                 arc_printk(D_NORMAL, dev, "RFC1201: I don't understand protocol %d (%Xh)\n",
405                            type, type);
406                 dev->stats.tx_errors++;
407                 dev->stats.tx_aborted_errors++;
408                 return 0;
409         }
410
411         /* Set the source hardware address.
412          *
413          * This is pretty pointless for most purposes, but it can help in
414          * debugging.  ARCnet does not allow us to change the source address
415          * in the actual packet sent.
416          */
417         pkt->hard.source = *dev->dev_addr;
418
419         soft->sequence = htons(lp->rfc1201.sequence++);
420         soft->split_flag = 0;   /* split packets are done elsewhere */
421
422         /* see linux/net/ethernet/eth.c to see where I got the following */
423
424         if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
425                 /* FIXME: fill in the last byte of the dest ipaddr here
426                  * to better comply with RFC1051 in "noarp" mode.
427                  * For now, always broadcasting will probably at least get
428                  * packets sent out :)
429                  */
430                 pkt->hard.dest = 0;
431                 return hdr_size;
432         }
433         /* otherwise, drop in the dest address */
434         pkt->hard.dest = daddr;
435         return hdr_size;
436 }
437
438 static void load_pkt(struct net_device *dev, struct arc_hardware *hard,
439                      struct arc_rfc1201 *soft, int softlen, int bufnum)
440 {
441         struct arcnet_local *lp = netdev_priv(dev);
442         int ofs;
443
444         /* assume length <= XMTU: someone should have handled that by now. */
445
446         if (softlen > MinTU) {
447                 hard->offset[0] = 0;
448                 hard->offset[1] = ofs = 512 - softlen;
449         } else if (softlen > MTU) {     /* exception packet - add an extra header */
450                 struct arc_rfc1201 excsoft;
451
452                 excsoft.proto = soft->proto;
453                 excsoft.split_flag = 0xff;
454                 excsoft.sequence = htons(0xffff);
455
456                 hard->offset[0] = 0;
457                 ofs = 512 - softlen;
458                 hard->offset[1] = ofs - RFC1201_HDR_SIZE;
459                 lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE,
460                                     &excsoft, RFC1201_HDR_SIZE);
461         } else {
462                 hard->offset[0] = ofs = 256 - softlen;
463         }
464
465         lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
466         lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
467
468         lp->lastload_dest = hard->dest;
469 }
470
471 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
472                       int bufnum)
473 {
474         struct arcnet_local *lp = netdev_priv(dev);
475         const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
476         struct Outgoing *out;
477
478         arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
479                    lp->next_tx, lp->cur_tx, bufnum);
480
481         length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
482         pkt->soft.rfc1201.split_flag = 0;
483
484         /* need to do a split packet? */
485         if (length > XMTU) {
486                 out = &lp->outgoing;
487
488                 out->length = length - RFC1201_HDR_SIZE;
489                 out->dataleft = lp->outgoing.length;
490                 out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
491                 out->segnum = 0;
492
493                 arc_printk(D_DURING, dev, "rfc1201 prep_tx: ready for %d-segment split (%d bytes, seq=%d)\n",
494                            out->numsegs, out->length,
495                            pkt->soft.rfc1201.sequence);
496
497                 return 0;       /* not done */
498         }
499         /* just load the packet into the buffers and send it off */
500         load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
501
502         return 1;               /* done */
503 }
504
505 static int continue_tx(struct net_device *dev, int bufnum)
506 {
507         struct arcnet_local *lp = netdev_priv(dev);
508         struct Outgoing *out = &lp->outgoing;
509         struct arc_hardware *hard = &out->pkt->hard;
510         struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
511         int maxsegsize = XMTU - RFC1201_HDR_SIZE;
512         int seglen;
513
514         arc_printk(D_DURING, dev,
515                    "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
516                    out->segnum, out->numsegs, soft->sequence);
517
518         /* the "new" soft header comes right before the data chunk */
519         newsoft = (struct arc_rfc1201 *)
520             (out->pkt->soft.raw + out->length - out->dataleft);
521
522         if (!out->segnum)       /* first packet; newsoft == soft */
523                 newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
524         else {
525                 newsoft->split_flag = out->segnum << 1;
526                 newsoft->proto = soft->proto;
527                 newsoft->sequence = soft->sequence;
528         }
529
530         seglen = maxsegsize;
531         if (seglen > out->dataleft)
532                 seglen = out->dataleft;
533         out->dataleft -= seglen;
534
535         load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
536
537         out->segnum++;
538         if (out->segnum >= out->numsegs)
539                 return 1;
540         else
541                 return 0;
542 }