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 been copied from the card */
192 memcpy(pkt, pkthdr, sizeof(struct archdr));
193 if (length > sizeof(pkt->soft))
194 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
195 pkt->soft.raw + sizeof(pkt->soft),
196 length - sizeof(pkt->soft));
198 /* ARP packets have problems when sent from some DOS systems:
199 * the source address is always 0!
200 * So we take the hardware source addr (which is impossible
201 * to fumble) and insert it ourselves.
203 if (soft->proto == ARC_P_ARP) {
204 struct arphdr *arp = (struct arphdr *)soft->payload;
206 /* make sure addresses are the right length */
207 if (arp->ar_hln == 1 && arp->ar_pln == 4) {
208 uint8_t *cptr = (uint8_t *)arp + sizeof(struct arphdr);
210 if (!*cptr) { /* is saddr = 00? */
211 arc_printk(D_EXTRA, dev,
212 "ARP source address was 00h, set to %02Xh\n",
214 dev->stats.rx_crc_errors++;
217 arc_printk(D_DURING, dev, "ARP source address (%Xh) is fine.\n",
221 arc_printk(D_NORMAL, dev, "funny-shaped ARP packet. (%Xh, %Xh)\n",
222 arp->ar_hln, arp->ar_pln);
223 dev->stats.rx_errors++;
224 dev->stats.rx_crc_errors++;
228 arcnet_dump_skb(dev, skb, "rx");
230 skb->protocol = type_trans(skb, dev);
232 } else { /* split packet */
233 /* NOTE: MSDOS ARP packet correction should only need to
234 * apply to unsplit packets, since ARP packets are so short.
236 * My interpretation of the RFC1201 document is that if a
237 * packet is received out of order, the entire assembly
238 * process should be aborted.
240 * The RFC also mentions "it is possible for successfully
241 * received packets to be retransmitted." As of 0.40 all
242 * previously received packets are allowed, not just the
245 * We allow multiple assembly processes, one for each
246 * ARCnet card possible on the network.
247 * Seems rather like a waste of memory, but there's no
248 * other way to be reliable.
251 arc_printk(D_RX, dev, "packet is split (splitflag=%d, seq=%d)\n",
252 soft->split_flag, in->sequence);
254 if (in->skb && in->sequence != soft->sequence) {
255 arc_printk(D_EXTRA, dev, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
256 saddr, in->sequence, soft->sequence,
258 dev_kfree_skb_irq(in->skb);
260 dev->stats.rx_errors++;
261 dev->stats.rx_missed_errors++;
262 in->lastpacket = in->numpackets = 0;
264 if (soft->split_flag & 1) { /* first packet in split */
265 arc_printk(D_RX, dev, "brand new splitpacket (splitflag=%d)\n",
267 if (in->skb) { /* already assembling one! */
268 arc_printk(D_EXTRA, dev, "aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
269 in->sequence, soft->split_flag,
271 dev->stats.rx_errors++;
272 dev->stats.rx_missed_errors++;
273 dev_kfree_skb_irq(in->skb);
275 in->sequence = soft->sequence;
276 in->numpackets = ((unsigned)soft->split_flag >> 1) + 2;
279 if (in->numpackets > 16) {
280 arc_printk(D_EXTRA, dev, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
282 lp->rfc1201.aborted_seq = soft->sequence;
283 dev->stats.rx_errors++;
284 dev->stats.rx_length_errors++;
287 in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
290 arc_printk(D_NORMAL, dev, "(split) memory squeeze, dropping packet.\n");
291 lp->rfc1201.aborted_seq = soft->sequence;
292 dev->stats.rx_dropped++;
296 pkt = (struct archdr *)skb->data;
297 soft = &pkt->soft.rfc1201;
299 memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
300 skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
302 soft->split_flag = 0; /* end result won't be split */
303 } else { /* not first packet */
304 int packetnum = ((unsigned)soft->split_flag >> 1) + 1;
306 /* if we're not assembling, there's no point trying to
310 if (lp->rfc1201.aborted_seq != soft->sequence) {
311 arc_printk(D_EXTRA, dev, "can't continue split without starting first! (splitflag=%d, seq=%d, aborted=%d)\n",
314 lp->rfc1201.aborted_seq);
315 dev->stats.rx_errors++;
316 dev->stats.rx_missed_errors++;
321 if (packetnum != in->lastpacket) { /* not the right flag! */
322 /* harmless duplicate? ignore. */
323 if (packetnum <= in->lastpacket - 1) {
324 arc_printk(D_EXTRA, dev, "duplicate splitpacket ignored! (splitflag=%d)\n",
326 dev->stats.rx_errors++;
327 dev->stats.rx_frame_errors++;
330 /* "bad" duplicate, kill reassembly */
331 arc_printk(D_EXTRA, dev, "out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",
332 in->sequence, soft->split_flag,
334 lp->rfc1201.aborted_seq = soft->sequence;
335 dev_kfree_skb_irq(in->skb);
337 dev->stats.rx_errors++;
338 dev->stats.rx_missed_errors++;
339 in->lastpacket = in->numpackets = 0;
342 pkt = (struct archdr *)in->skb->data;
343 soft = &pkt->soft.rfc1201;
348 lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
349 skb->data + skb->len,
350 length - RFC1201_HDR_SIZE);
351 skb_put(skb, length - RFC1201_HDR_SIZE);
354 if (in->lastpacket == in->numpackets) {
356 in->lastpacket = in->numpackets = 0;
358 arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (unsplit)\n",
359 skb->len, pkt->hard.source);
360 arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (split)\n",
361 skb->len, pkt->hard.source);
363 arcnet_dump_skb(dev, skb, "rx");
365 skb->protocol = type_trans(skb, dev);
371 /* Create the ARCnet hard/soft headers for RFC1201. */
372 static int build_header(struct sk_buff *skb, struct net_device *dev,
373 unsigned short type, uint8_t daddr)
375 struct arcnet_local *lp = netdev_priv(dev);
376 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
377 struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
378 struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
380 /* set the protocol ID according to RFC1201 */
383 soft->proto = ARC_P_IP;
386 soft->proto = ARC_P_IPV6;
389 soft->proto = ARC_P_ARP;
392 soft->proto = ARC_P_RARP;
397 soft->proto = ARC_P_IPX;
400 soft->proto = ARC_P_ATALK;
403 arc_printk(D_NORMAL, dev, "RFC1201: I don't understand protocol %d (%Xh)\n",
405 dev->stats.tx_errors++;
406 dev->stats.tx_aborted_errors++;
410 /* Set the source hardware address.
412 * This is pretty pointless for most purposes, but it can help in
413 * debugging. ARCnet does not allow us to change the source address
414 * in the actual packet sent.
416 pkt->hard.source = *dev->dev_addr;
418 soft->sequence = htons(lp->rfc1201.sequence++);
419 soft->split_flag = 0; /* split packets are done elsewhere */
421 /* see linux/net/ethernet/eth.c to see where I got the following */
423 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
424 /* FIXME: fill in the last byte of the dest ipaddr here
425 * to better comply with RFC1051 in "noarp" mode.
426 * For now, always broadcasting will probably at least get
427 * packets sent out :)
432 /* otherwise, drop in the dest address */
433 pkt->hard.dest = daddr;
437 static void load_pkt(struct net_device *dev, struct arc_hardware *hard,
438 struct arc_rfc1201 *soft, int softlen, int bufnum)
440 struct arcnet_local *lp = netdev_priv(dev);
443 /* assume length <= XMTU: someone should have handled that by now. */
445 if (softlen > MinTU) {
447 hard->offset[1] = ofs = 512 - softlen;
448 } else if (softlen > MTU) { /* exception packet - add an extra header */
449 struct arc_rfc1201 excsoft;
451 excsoft.proto = soft->proto;
452 excsoft.split_flag = 0xff;
453 excsoft.sequence = htons(0xffff);
457 hard->offset[1] = ofs - RFC1201_HDR_SIZE;
458 lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE,
459 &excsoft, RFC1201_HDR_SIZE);
461 hard->offset[0] = ofs = 256 - softlen;
464 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
465 lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
467 lp->lastload_dest = hard->dest;
470 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
473 struct arcnet_local *lp = netdev_priv(dev);
474 const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
475 struct Outgoing *out;
477 arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
478 lp->next_tx, lp->cur_tx, bufnum);
480 length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
481 pkt->soft.rfc1201.split_flag = 0;
483 /* need to do a split packet? */
487 out->length = length - RFC1201_HDR_SIZE;
488 out->dataleft = lp->outgoing.length;
489 out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
492 arc_printk(D_DURING, dev, "rfc1201 prep_tx: ready for %d-segment split (%d bytes, seq=%d)\n",
493 out->numsegs, out->length,
494 pkt->soft.rfc1201.sequence);
496 return 0; /* not done */
498 /* just load the packet into the buffers and send it off */
499 load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
504 static int continue_tx(struct net_device *dev, int bufnum)
506 struct arcnet_local *lp = netdev_priv(dev);
507 struct Outgoing *out = &lp->outgoing;
508 struct arc_hardware *hard = &out->pkt->hard;
509 struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
510 int maxsegsize = XMTU - RFC1201_HDR_SIZE;
513 arc_printk(D_DURING, dev,
514 "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
515 out->segnum, out->numsegs, soft->sequence);
517 /* the "new" soft header comes right before the data chunk */
518 newsoft = (struct arc_rfc1201 *)
519 (out->pkt->soft.raw + out->length - out->dataleft);
521 if (!out->segnum) /* first packet; newsoft == soft */
522 newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
524 newsoft->split_flag = out->segnum << 1;
525 newsoft->proto = soft->proto;
526 newsoft->sequence = soft->sequence;
530 if (seglen > out->dataleft)
531 seglen = out->dataleft;
532 out->dataleft -= seglen;
534 load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
537 if (out->segnum >= out->numsegs)