2 * Linux ARCnet driver - RFC1201 (standard) packet encapsulation
4 * Written 1994-1999 by Avery Pennarun.
5 * Derived from skeleton.c by Donald Becker.
7 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
8 * for sponsoring the further development of this driver.
10 * **********************
12 * The original copyright of skeleton.c was as follows:
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.
20 * **********************
22 * For more details, see drivers/net/arcnet.c
24 * **********************
27 #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
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>
37 MODULE_LICENSE("GPL");
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,
46 static int continue_tx(struct net_device *dev, int bufnum);
48 static struct ArcProto rfc1201_proto = {
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 */
53 .build_header = build_header,
54 .prepare_tx = prepare_tx,
55 .continue_tx = continue_tx,
59 static int __init arcnet_rfc1201_init(void)
61 pr_info("%s\n", "RFC1201 \"standard\" (`a') encapsulation support loaded");
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]
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;
78 static void __exit arcnet_rfc1201_exit(void)
80 arcnet_unregister_proto(&rfc1201_proto);
83 module_init(arcnet_rfc1201_init);
84 module_exit(arcnet_rfc1201_exit);
86 /* Determine a packet's protocol ID.
88 * With ARCnet we have to convert everything to Ethernet-style stuff.
90 static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
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;
96 /* Pull off the arcnet header. */
97 skb_reset_mac_header(skb);
98 skb_pull(skb, hdr_size);
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;
107 /* now return the protocol number */
108 switch (soft->proto) {
110 return htons(ETH_P_IP);
112 return htons(ETH_P_IPV6);
114 return htons(ETH_P_ARP);
116 return htons(ETH_P_RARP);
119 case ARC_P_NOVELL_EC:
120 return htons(ETH_P_802_3);
122 dev->stats.rx_errors++;
123 dev->stats.rx_crc_errors++;
127 return htons(ETH_P_IP);
130 /* packet receiver */
131 static void rx(struct net_device *dev, int bufnum,
132 struct archdr *pkthdr, int length)
134 struct arcnet_local *lp = netdev_priv(dev);
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];
141 arc_printk(D_DURING, dev, "it's an RFC1201 packet (length=%d)\n",
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");
153 arc_printk(D_EXTRA, dev, "short RFC1201 exception packet from %02Xh",
158 /* skip over 4-byte junkola */
161 lp->hw.copy_from_card(dev, bufnum, 512 - length,
162 soft, sizeof(pkt->soft));
164 if (!soft->split_flag) { /* not split */
165 arc_printk(D_RX, dev, "incoming is not split (splitflag=%d)\n",
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,
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++;
178 in->sequence = soft->sequence;
180 skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
182 dev->stats.rx_dropped++;
185 skb_put(skb, length + ARC_HDR_SIZE);
188 pkt = (struct archdr *)skb->data;
189 soft = &pkt->soft.rfc1201;
191 /* up to sizeof(pkt->soft) has already
192 * been copied from the card
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));
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.
206 if (soft->proto == ARC_P_ARP) {
207 struct arphdr *arp = (struct arphdr *)soft->payload;
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);
213 if (!*cptr) { /* is saddr = 00? */
214 arc_printk(D_EXTRA, dev,
215 "ARP source address was 00h, set to %02Xh\n",
217 dev->stats.rx_crc_errors++;
220 arc_printk(D_DURING, dev, "ARP source address (%Xh) is fine.\n",
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++;
231 arcnet_dump_skb(dev, skb, "rx");
233 skb->protocol = type_trans(skb, dev);
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.
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.
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
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.
254 arc_printk(D_RX, dev, "packet is split (splitflag=%d, seq=%d)\n",
255 soft->split_flag, in->sequence);
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,
261 dev_kfree_skb_irq(in->skb);
263 dev->stats.rx_errors++;
264 dev->stats.rx_missed_errors++;
265 in->lastpacket = in->numpackets = 0;
267 if (soft->split_flag & 1) { /* first packet in split */
268 arc_printk(D_RX, dev, "brand new splitpacket (splitflag=%d)\n",
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,
274 dev->stats.rx_errors++;
275 dev->stats.rx_missed_errors++;
276 dev_kfree_skb_irq(in->skb);
278 in->sequence = soft->sequence;
279 in->numpackets = ((unsigned)soft->split_flag >> 1) + 2;
282 if (in->numpackets > 16) {
283 arc_printk(D_EXTRA, dev, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
285 lp->rfc1201.aborted_seq = soft->sequence;
286 dev->stats.rx_errors++;
287 dev->stats.rx_length_errors++;
290 in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
293 arc_printk(D_NORMAL, dev, "(split) memory squeeze, dropping packet.\n");
294 lp->rfc1201.aborted_seq = soft->sequence;
295 dev->stats.rx_dropped++;
299 pkt = (struct archdr *)skb->data;
300 soft = &pkt->soft.rfc1201;
302 memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
303 skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
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;
309 /* if we're not assembling, there's no point trying to
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",
317 lp->rfc1201.aborted_seq);
318 dev->stats.rx_errors++;
319 dev->stats.rx_missed_errors++;
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",
330 dev->stats.rx_errors++;
331 dev->stats.rx_frame_errors++;
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,
338 lp->rfc1201.aborted_seq = soft->sequence;
339 dev_kfree_skb_irq(in->skb);
341 dev->stats.rx_errors++;
342 dev->stats.rx_missed_errors++;
343 in->lastpacket = in->numpackets = 0;
346 pkt = (struct archdr *)in->skb->data;
347 soft = &pkt->soft.rfc1201;
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);
358 if (in->lastpacket == in->numpackets) {
360 in->lastpacket = in->numpackets = 0;
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);
367 arcnet_dump_skb(dev, skb, "rx");
369 skb->protocol = type_trans(skb, dev);
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)
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;
384 /* set the protocol ID according to RFC1201 */
387 soft->proto = ARC_P_IP;
390 soft->proto = ARC_P_IPV6;
393 soft->proto = ARC_P_ARP;
396 soft->proto = ARC_P_RARP;
401 soft->proto = ARC_P_IPX;
404 soft->proto = ARC_P_ATALK;
407 arc_printk(D_NORMAL, dev, "RFC1201: I don't understand protocol %d (%Xh)\n",
409 dev->stats.tx_errors++;
410 dev->stats.tx_aborted_errors++;
414 /* Set the source hardware address.
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.
420 pkt->hard.source = *dev->dev_addr;
422 soft->sequence = htons(lp->rfc1201.sequence++);
423 soft->split_flag = 0; /* split packets are done elsewhere */
425 /* see linux/net/ethernet/eth.c to see where I got the following */
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 :)
436 /* otherwise, drop in the dest address */
437 pkt->hard.dest = daddr;
441 static void load_pkt(struct net_device *dev, struct arc_hardware *hard,
442 struct arc_rfc1201 *soft, int softlen, int bufnum)
444 struct arcnet_local *lp = netdev_priv(dev);
447 /* assume length <= XMTU: someone should have handled that by now. */
449 if (softlen > MinTU) {
451 hard->offset[1] = ofs = 512 - softlen;
452 } else if (softlen > MTU) { /* exception packet - add an extra header */
453 struct arc_rfc1201 excsoft;
455 excsoft.proto = soft->proto;
456 excsoft.split_flag = 0xff;
457 excsoft.sequence = htons(0xffff);
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);
465 hard->offset[0] = ofs = 256 - softlen;
468 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
469 lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
471 lp->lastload_dest = hard->dest;
474 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
477 struct arcnet_local *lp = netdev_priv(dev);
478 const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
479 struct Outgoing *out;
481 arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
482 lp->next_tx, lp->cur_tx, bufnum);
484 /* hard header is not included in packet length */
485 length -= ARC_HDR_SIZE;
486 pkt->soft.rfc1201.split_flag = 0;
488 /* need to do a split packet? */
492 out->length = length - RFC1201_HDR_SIZE;
493 out->dataleft = lp->outgoing.length;
494 out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
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);
501 return 0; /* not done */
503 /* just load the packet into the buffers and send it off */
504 load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
509 static int continue_tx(struct net_device *dev, int bufnum)
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;
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);
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);
526 if (!out->segnum) /* first packet; newsoft == soft */
527 newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
529 newsoft->split_flag = out->segnum << 1;
530 newsoft->proto = soft->proto;
531 newsoft->sequence = soft->sequence;
535 if (seglen > out->dataleft)
536 seglen = out->dataleft;
537 out->dataleft -= seglen;
539 load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
542 if (out->segnum >= out->numsegs)