18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Linux ARCnet driver - RFC1201 (standard) packet encapsulation
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Written 1994-1999 by Avery Pennarun.
58c2ecf20Sopenharmony_ci * Derived from skeleton.c by Donald Becker.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
88c2ecf20Sopenharmony_ci *  for sponsoring the further development of this driver.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * **********************
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * The original copyright of skeleton.c was as follows:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * skeleton.c Written 1993 by Donald Becker.
158c2ecf20Sopenharmony_ci * Copyright 1993 United States Government as represented by the
168c2ecf20Sopenharmony_ci * Director, National Security Agency.  This software may only be used
178c2ecf20Sopenharmony_ci * and distributed according to the terms of the GNU General Public License as
188c2ecf20Sopenharmony_ci * modified by SRC, incorporated herein by reference.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * **********************
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * For more details, see drivers/net/arcnet.c
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * **********************
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <linux/gfp.h>
308c2ecf20Sopenharmony_ci#include <linux/module.h>
318c2ecf20Sopenharmony_ci#include <linux/init.h>
328c2ecf20Sopenharmony_ci#include <linux/if_arp.h>
338c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
348c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include "arcdevice.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic __be16 type_trans(struct sk_buff *skb, struct net_device *dev);
418c2ecf20Sopenharmony_cistatic void rx(struct net_device *dev, int bufnum,
428c2ecf20Sopenharmony_ci	       struct archdr *pkthdr, int length);
438c2ecf20Sopenharmony_cistatic int build_header(struct sk_buff *skb, struct net_device *dev,
448c2ecf20Sopenharmony_ci			unsigned short type, uint8_t daddr);
458c2ecf20Sopenharmony_cistatic int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
468c2ecf20Sopenharmony_ci		      int bufnum);
478c2ecf20Sopenharmony_cistatic int continue_tx(struct net_device *dev, int bufnum);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic struct ArcProto rfc1201_proto = {
508c2ecf20Sopenharmony_ci	.suffix		= 'a',
518c2ecf20Sopenharmony_ci	.mtu		= 1500,	/* could be more, but some receivers can't handle it... */
528c2ecf20Sopenharmony_ci	.is_ip          = 1,    /* This is for sending IP and ARP packages */
538c2ecf20Sopenharmony_ci	.rx		= rx,
548c2ecf20Sopenharmony_ci	.build_header	= build_header,
558c2ecf20Sopenharmony_ci	.prepare_tx	= prepare_tx,
568c2ecf20Sopenharmony_ci	.continue_tx	= continue_tx,
578c2ecf20Sopenharmony_ci	.ack_tx         = NULL
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic int __init arcnet_rfc1201_init(void)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	pr_info("%s\n", "RFC1201 \"standard\" (`a') encapsulation support loaded");
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	arc_proto_map[ARC_P_IP]
658c2ecf20Sopenharmony_ci	    = arc_proto_map[ARC_P_IPV6]
668c2ecf20Sopenharmony_ci	    = arc_proto_map[ARC_P_ARP]
678c2ecf20Sopenharmony_ci	    = arc_proto_map[ARC_P_RARP]
688c2ecf20Sopenharmony_ci	    = arc_proto_map[ARC_P_IPX]
698c2ecf20Sopenharmony_ci	    = arc_proto_map[ARC_P_NOVELL_EC]
708c2ecf20Sopenharmony_ci	    = &rfc1201_proto;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* if someone else already owns the broadcast, we won't take it */
738c2ecf20Sopenharmony_ci	if (arc_bcast_proto == arc_proto_default)
748c2ecf20Sopenharmony_ci		arc_bcast_proto = &rfc1201_proto;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic void __exit arcnet_rfc1201_exit(void)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	arcnet_unregister_proto(&rfc1201_proto);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cimodule_init(arcnet_rfc1201_init);
858c2ecf20Sopenharmony_cimodule_exit(arcnet_rfc1201_exit);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* Determine a packet's protocol ID.
888c2ecf20Sopenharmony_ci *
898c2ecf20Sopenharmony_ci * With ARCnet we have to convert everything to Ethernet-style stuff.
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_cistatic __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	struct archdr *pkt = (struct archdr *)skb->data;
948c2ecf20Sopenharmony_ci	struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
958c2ecf20Sopenharmony_ci	int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* Pull off the arcnet header. */
988c2ecf20Sopenharmony_ci	skb_reset_mac_header(skb);
998c2ecf20Sopenharmony_ci	skb_pull(skb, hdr_size);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (pkt->hard.dest == 0) {
1028c2ecf20Sopenharmony_ci		skb->pkt_type = PACKET_BROADCAST;
1038c2ecf20Sopenharmony_ci	} else if (dev->flags & IFF_PROMISC) {
1048c2ecf20Sopenharmony_ci		/* if we're not sending to ourselves :) */
1058c2ecf20Sopenharmony_ci		if (pkt->hard.dest != dev->dev_addr[0])
1068c2ecf20Sopenharmony_ci			skb->pkt_type = PACKET_OTHERHOST;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci	/* now return the protocol number */
1098c2ecf20Sopenharmony_ci	switch (soft->proto) {
1108c2ecf20Sopenharmony_ci	case ARC_P_IP:
1118c2ecf20Sopenharmony_ci		return htons(ETH_P_IP);
1128c2ecf20Sopenharmony_ci	case ARC_P_IPV6:
1138c2ecf20Sopenharmony_ci		return htons(ETH_P_IPV6);
1148c2ecf20Sopenharmony_ci	case ARC_P_ARP:
1158c2ecf20Sopenharmony_ci		return htons(ETH_P_ARP);
1168c2ecf20Sopenharmony_ci	case ARC_P_RARP:
1178c2ecf20Sopenharmony_ci		return htons(ETH_P_RARP);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	case ARC_P_IPX:
1208c2ecf20Sopenharmony_ci	case ARC_P_NOVELL_EC:
1218c2ecf20Sopenharmony_ci		return htons(ETH_P_802_3);
1228c2ecf20Sopenharmony_ci	default:
1238c2ecf20Sopenharmony_ci		dev->stats.rx_errors++;
1248c2ecf20Sopenharmony_ci		dev->stats.rx_crc_errors++;
1258c2ecf20Sopenharmony_ci		return 0;
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	return htons(ETH_P_IP);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* packet receiver */
1328c2ecf20Sopenharmony_cistatic void rx(struct net_device *dev, int bufnum,
1338c2ecf20Sopenharmony_ci	       struct archdr *pkthdr, int length)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
1368c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1378c2ecf20Sopenharmony_ci	struct archdr *pkt = pkthdr;
1388c2ecf20Sopenharmony_ci	struct arc_rfc1201 *soft = &pkthdr->soft.rfc1201;
1398c2ecf20Sopenharmony_ci	int saddr = pkt->hard.source, ofs;
1408c2ecf20Sopenharmony_ci	struct Incoming *in = &lp->rfc1201.incoming[saddr];
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	arc_printk(D_DURING, dev, "it's an RFC1201 packet (length=%d)\n",
1438c2ecf20Sopenharmony_ci		   length);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	if (length >= MinTU)
1468c2ecf20Sopenharmony_ci		ofs = 512 - length;
1478c2ecf20Sopenharmony_ci	else
1488c2ecf20Sopenharmony_ci		ofs = 256 - length;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if (soft->split_flag == 0xFF) {		/* Exception Packet */
1518c2ecf20Sopenharmony_ci		if (length >= 4 + RFC1201_HDR_SIZE) {
1528c2ecf20Sopenharmony_ci			arc_printk(D_DURING, dev, "compensating for exception packet\n");
1538c2ecf20Sopenharmony_ci		} else {
1548c2ecf20Sopenharmony_ci			arc_printk(D_EXTRA, dev, "short RFC1201 exception packet from %02Xh",
1558c2ecf20Sopenharmony_ci				   saddr);
1568c2ecf20Sopenharmony_ci			return;
1578c2ecf20Sopenharmony_ci		}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		/* skip over 4-byte junkola */
1608c2ecf20Sopenharmony_ci		length -= 4;
1618c2ecf20Sopenharmony_ci		ofs += 4;
1628c2ecf20Sopenharmony_ci		lp->hw.copy_from_card(dev, bufnum, 512 - length,
1638c2ecf20Sopenharmony_ci				      soft, sizeof(pkt->soft));
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci	if (!soft->split_flag) {	/* not split */
1668c2ecf20Sopenharmony_ci		arc_printk(D_RX, dev, "incoming is not split (splitflag=%d)\n",
1678c2ecf20Sopenharmony_ci			   soft->split_flag);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		if (in->skb) {	/* already assembling one! */
1708c2ecf20Sopenharmony_ci			arc_printk(D_EXTRA, dev, "aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
1718c2ecf20Sopenharmony_ci				   in->sequence, soft->split_flag,
1728c2ecf20Sopenharmony_ci				   soft->sequence);
1738c2ecf20Sopenharmony_ci			lp->rfc1201.aborted_seq = soft->sequence;
1748c2ecf20Sopenharmony_ci			dev_kfree_skb_irq(in->skb);
1758c2ecf20Sopenharmony_ci			dev->stats.rx_errors++;
1768c2ecf20Sopenharmony_ci			dev->stats.rx_missed_errors++;
1778c2ecf20Sopenharmony_ci			in->skb = NULL;
1788c2ecf20Sopenharmony_ci		}
1798c2ecf20Sopenharmony_ci		in->sequence = soft->sequence;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci		skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
1828c2ecf20Sopenharmony_ci		if (!skb) {
1838c2ecf20Sopenharmony_ci			dev->stats.rx_dropped++;
1848c2ecf20Sopenharmony_ci			return;
1858c2ecf20Sopenharmony_ci		}
1868c2ecf20Sopenharmony_ci		skb_put(skb, length + ARC_HDR_SIZE);
1878c2ecf20Sopenharmony_ci		skb->dev = dev;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci		pkt = (struct archdr *)skb->data;
1908c2ecf20Sopenharmony_ci		soft = &pkt->soft.rfc1201;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci		/* up to sizeof(pkt->soft) has already
1938c2ecf20Sopenharmony_ci		 * been copied from the card
1948c2ecf20Sopenharmony_ci		 */
1958c2ecf20Sopenharmony_ci		memcpy(pkt, pkthdr, sizeof(struct archdr));
1968c2ecf20Sopenharmony_ci		if (length > sizeof(pkt->soft))
1978c2ecf20Sopenharmony_ci			lp->hw.copy_from_card(dev, bufnum,
1988c2ecf20Sopenharmony_ci					      ofs + sizeof(pkt->soft),
1998c2ecf20Sopenharmony_ci					      pkt->soft.raw + sizeof(pkt->soft),
2008c2ecf20Sopenharmony_ci					      length - sizeof(pkt->soft));
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci		/* ARP packets have problems when sent from some DOS systems:
2038c2ecf20Sopenharmony_ci		 * the source address is always 0!
2048c2ecf20Sopenharmony_ci		 * So we take the hardware source addr (which is impossible
2058c2ecf20Sopenharmony_ci		 * to fumble) and insert it ourselves.
2068c2ecf20Sopenharmony_ci		 */
2078c2ecf20Sopenharmony_ci		if (soft->proto == ARC_P_ARP) {
2088c2ecf20Sopenharmony_ci			struct arphdr *arp = (struct arphdr *)soft->payload;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci			/* make sure addresses are the right length */
2118c2ecf20Sopenharmony_ci			if (arp->ar_hln == 1 && arp->ar_pln == 4) {
2128c2ecf20Sopenharmony_ci				uint8_t *cptr = (uint8_t *)arp + sizeof(struct arphdr);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci				if (!*cptr) {	/* is saddr = 00? */
2158c2ecf20Sopenharmony_ci					arc_printk(D_EXTRA, dev,
2168c2ecf20Sopenharmony_ci						   "ARP source address was 00h, set to %02Xh\n",
2178c2ecf20Sopenharmony_ci						   saddr);
2188c2ecf20Sopenharmony_ci					dev->stats.rx_crc_errors++;
2198c2ecf20Sopenharmony_ci					*cptr = saddr;
2208c2ecf20Sopenharmony_ci				} else {
2218c2ecf20Sopenharmony_ci					arc_printk(D_DURING, dev, "ARP source address (%Xh) is fine.\n",
2228c2ecf20Sopenharmony_ci						   *cptr);
2238c2ecf20Sopenharmony_ci				}
2248c2ecf20Sopenharmony_ci			} else {
2258c2ecf20Sopenharmony_ci				arc_printk(D_NORMAL, dev, "funny-shaped ARP packet. (%Xh, %Xh)\n",
2268c2ecf20Sopenharmony_ci					   arp->ar_hln, arp->ar_pln);
2278c2ecf20Sopenharmony_ci				dev->stats.rx_errors++;
2288c2ecf20Sopenharmony_ci				dev->stats.rx_crc_errors++;
2298c2ecf20Sopenharmony_ci			}
2308c2ecf20Sopenharmony_ci		}
2318c2ecf20Sopenharmony_ci		if (BUGLVL(D_SKB))
2328c2ecf20Sopenharmony_ci			arcnet_dump_skb(dev, skb, "rx");
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		skb->protocol = type_trans(skb, dev);
2358c2ecf20Sopenharmony_ci		netif_rx(skb);
2368c2ecf20Sopenharmony_ci	} else {		/* split packet */
2378c2ecf20Sopenharmony_ci		/* NOTE: MSDOS ARP packet correction should only need to
2388c2ecf20Sopenharmony_ci		 * apply to unsplit packets, since ARP packets are so short.
2398c2ecf20Sopenharmony_ci		 *
2408c2ecf20Sopenharmony_ci		 * My interpretation of the RFC1201 document is that if a
2418c2ecf20Sopenharmony_ci		 * packet is received out of order, the entire assembly
2428c2ecf20Sopenharmony_ci		 * process should be aborted.
2438c2ecf20Sopenharmony_ci		 *
2448c2ecf20Sopenharmony_ci		 * The RFC also mentions "it is possible for successfully
2458c2ecf20Sopenharmony_ci		 * received packets to be retransmitted." As of 0.40 all
2468c2ecf20Sopenharmony_ci		 * previously received packets are allowed, not just the
2478c2ecf20Sopenharmony_ci		 * most recent one.
2488c2ecf20Sopenharmony_ci		 *
2498c2ecf20Sopenharmony_ci		 * We allow multiple assembly processes, one for each
2508c2ecf20Sopenharmony_ci		 * ARCnet card possible on the network.
2518c2ecf20Sopenharmony_ci		 * Seems rather like a waste of memory, but there's no
2528c2ecf20Sopenharmony_ci		 * other way to be reliable.
2538c2ecf20Sopenharmony_ci		 */
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci		arc_printk(D_RX, dev, "packet is split (splitflag=%d, seq=%d)\n",
2568c2ecf20Sopenharmony_ci			   soft->split_flag, in->sequence);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		if (in->skb && in->sequence != soft->sequence) {
2598c2ecf20Sopenharmony_ci			arc_printk(D_EXTRA, dev, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
2608c2ecf20Sopenharmony_ci				   saddr, in->sequence, soft->sequence,
2618c2ecf20Sopenharmony_ci				   soft->split_flag);
2628c2ecf20Sopenharmony_ci			dev_kfree_skb_irq(in->skb);
2638c2ecf20Sopenharmony_ci			in->skb = NULL;
2648c2ecf20Sopenharmony_ci			dev->stats.rx_errors++;
2658c2ecf20Sopenharmony_ci			dev->stats.rx_missed_errors++;
2668c2ecf20Sopenharmony_ci			in->lastpacket = in->numpackets = 0;
2678c2ecf20Sopenharmony_ci		}
2688c2ecf20Sopenharmony_ci		if (soft->split_flag & 1) {	/* first packet in split */
2698c2ecf20Sopenharmony_ci			arc_printk(D_RX, dev, "brand new splitpacket (splitflag=%d)\n",
2708c2ecf20Sopenharmony_ci				   soft->split_flag);
2718c2ecf20Sopenharmony_ci			if (in->skb) {	/* already assembling one! */
2728c2ecf20Sopenharmony_ci				arc_printk(D_EXTRA, dev, "aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
2738c2ecf20Sopenharmony_ci					   in->sequence, soft->split_flag,
2748c2ecf20Sopenharmony_ci					   soft->sequence);
2758c2ecf20Sopenharmony_ci				dev->stats.rx_errors++;
2768c2ecf20Sopenharmony_ci				dev->stats.rx_missed_errors++;
2778c2ecf20Sopenharmony_ci				dev_kfree_skb_irq(in->skb);
2788c2ecf20Sopenharmony_ci			}
2798c2ecf20Sopenharmony_ci			in->sequence = soft->sequence;
2808c2ecf20Sopenharmony_ci			in->numpackets = ((unsigned)soft->split_flag >> 1) + 2;
2818c2ecf20Sopenharmony_ci			in->lastpacket = 1;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci			if (in->numpackets > 16) {
2848c2ecf20Sopenharmony_ci				arc_printk(D_EXTRA, dev, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
2858c2ecf20Sopenharmony_ci					   soft->split_flag);
2868c2ecf20Sopenharmony_ci				lp->rfc1201.aborted_seq = soft->sequence;
2878c2ecf20Sopenharmony_ci				dev->stats.rx_errors++;
2888c2ecf20Sopenharmony_ci				dev->stats.rx_length_errors++;
2898c2ecf20Sopenharmony_ci				return;
2908c2ecf20Sopenharmony_ci			}
2918c2ecf20Sopenharmony_ci			in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
2928c2ecf20Sopenharmony_ci						  GFP_ATOMIC);
2938c2ecf20Sopenharmony_ci			if (!skb) {
2948c2ecf20Sopenharmony_ci				arc_printk(D_NORMAL, dev, "(split) memory squeeze, dropping packet.\n");
2958c2ecf20Sopenharmony_ci				lp->rfc1201.aborted_seq = soft->sequence;
2968c2ecf20Sopenharmony_ci				dev->stats.rx_dropped++;
2978c2ecf20Sopenharmony_ci				return;
2988c2ecf20Sopenharmony_ci			}
2998c2ecf20Sopenharmony_ci			skb->dev = dev;
3008c2ecf20Sopenharmony_ci			pkt = (struct archdr *)skb->data;
3018c2ecf20Sopenharmony_ci			soft = &pkt->soft.rfc1201;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci			memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
3048c2ecf20Sopenharmony_ci			skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci			soft->split_flag = 0;	/* end result won't be split */
3078c2ecf20Sopenharmony_ci		} else {	/* not first packet */
3088c2ecf20Sopenharmony_ci			int packetnum = ((unsigned)soft->split_flag >> 1) + 1;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci			/* if we're not assembling, there's no point trying to
3118c2ecf20Sopenharmony_ci			 * continue.
3128c2ecf20Sopenharmony_ci			 */
3138c2ecf20Sopenharmony_ci			if (!in->skb) {
3148c2ecf20Sopenharmony_ci				if (lp->rfc1201.aborted_seq != soft->sequence) {
3158c2ecf20Sopenharmony_ci					arc_printk(D_EXTRA, dev, "can't continue split without starting first! (splitflag=%d, seq=%d, aborted=%d)\n",
3168c2ecf20Sopenharmony_ci						   soft->split_flag,
3178c2ecf20Sopenharmony_ci						   soft->sequence,
3188c2ecf20Sopenharmony_ci						   lp->rfc1201.aborted_seq);
3198c2ecf20Sopenharmony_ci					dev->stats.rx_errors++;
3208c2ecf20Sopenharmony_ci					dev->stats.rx_missed_errors++;
3218c2ecf20Sopenharmony_ci				}
3228c2ecf20Sopenharmony_ci				return;
3238c2ecf20Sopenharmony_ci			}
3248c2ecf20Sopenharmony_ci			in->lastpacket++;
3258c2ecf20Sopenharmony_ci			/* if not the right flag */
3268c2ecf20Sopenharmony_ci			if (packetnum != in->lastpacket) {
3278c2ecf20Sopenharmony_ci				/* harmless duplicate? ignore. */
3288c2ecf20Sopenharmony_ci				if (packetnum <= in->lastpacket - 1) {
3298c2ecf20Sopenharmony_ci					arc_printk(D_EXTRA, dev, "duplicate splitpacket ignored! (splitflag=%d)\n",
3308c2ecf20Sopenharmony_ci						   soft->split_flag);
3318c2ecf20Sopenharmony_ci					dev->stats.rx_errors++;
3328c2ecf20Sopenharmony_ci					dev->stats.rx_frame_errors++;
3338c2ecf20Sopenharmony_ci					return;
3348c2ecf20Sopenharmony_ci				}
3358c2ecf20Sopenharmony_ci				/* "bad" duplicate, kill reassembly */
3368c2ecf20Sopenharmony_ci				arc_printk(D_EXTRA, dev, "out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",
3378c2ecf20Sopenharmony_ci					   in->sequence, soft->split_flag,
3388c2ecf20Sopenharmony_ci					   soft->sequence);
3398c2ecf20Sopenharmony_ci				lp->rfc1201.aborted_seq = soft->sequence;
3408c2ecf20Sopenharmony_ci				dev_kfree_skb_irq(in->skb);
3418c2ecf20Sopenharmony_ci				in->skb = NULL;
3428c2ecf20Sopenharmony_ci				dev->stats.rx_errors++;
3438c2ecf20Sopenharmony_ci				dev->stats.rx_missed_errors++;
3448c2ecf20Sopenharmony_ci				in->lastpacket = in->numpackets = 0;
3458c2ecf20Sopenharmony_ci				return;
3468c2ecf20Sopenharmony_ci			}
3478c2ecf20Sopenharmony_ci			pkt = (struct archdr *)in->skb->data;
3488c2ecf20Sopenharmony_ci			soft = &pkt->soft.rfc1201;
3498c2ecf20Sopenharmony_ci		}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci		skb = in->skb;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci		lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
3548c2ecf20Sopenharmony_ci				      skb->data + skb->len,
3558c2ecf20Sopenharmony_ci				      length - RFC1201_HDR_SIZE);
3568c2ecf20Sopenharmony_ci		skb_put(skb, length - RFC1201_HDR_SIZE);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci		/* are we done? */
3598c2ecf20Sopenharmony_ci		if (in->lastpacket == in->numpackets) {
3608c2ecf20Sopenharmony_ci			in->skb = NULL;
3618c2ecf20Sopenharmony_ci			in->lastpacket = in->numpackets = 0;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci			arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (unsplit)\n",
3648c2ecf20Sopenharmony_ci				   skb->len, pkt->hard.source);
3658c2ecf20Sopenharmony_ci			arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (split)\n",
3668c2ecf20Sopenharmony_ci				   skb->len, pkt->hard.source);
3678c2ecf20Sopenharmony_ci			if (BUGLVL(D_SKB))
3688c2ecf20Sopenharmony_ci				arcnet_dump_skb(dev, skb, "rx");
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci			skb->protocol = type_trans(skb, dev);
3718c2ecf20Sopenharmony_ci			netif_rx(skb);
3728c2ecf20Sopenharmony_ci		}
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci/* Create the ARCnet hard/soft headers for RFC1201. */
3778c2ecf20Sopenharmony_cistatic int build_header(struct sk_buff *skb, struct net_device *dev,
3788c2ecf20Sopenharmony_ci			unsigned short type, uint8_t daddr)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
3818c2ecf20Sopenharmony_ci	int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
3828c2ecf20Sopenharmony_ci	struct archdr *pkt = skb_push(skb, hdr_size);
3838c2ecf20Sopenharmony_ci	struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/* set the protocol ID according to RFC1201 */
3868c2ecf20Sopenharmony_ci	switch (type) {
3878c2ecf20Sopenharmony_ci	case ETH_P_IP:
3888c2ecf20Sopenharmony_ci		soft->proto = ARC_P_IP;
3898c2ecf20Sopenharmony_ci		break;
3908c2ecf20Sopenharmony_ci	case ETH_P_IPV6:
3918c2ecf20Sopenharmony_ci		soft->proto = ARC_P_IPV6;
3928c2ecf20Sopenharmony_ci		break;
3938c2ecf20Sopenharmony_ci	case ETH_P_ARP:
3948c2ecf20Sopenharmony_ci		soft->proto = ARC_P_ARP;
3958c2ecf20Sopenharmony_ci		break;
3968c2ecf20Sopenharmony_ci	case ETH_P_RARP:
3978c2ecf20Sopenharmony_ci		soft->proto = ARC_P_RARP;
3988c2ecf20Sopenharmony_ci		break;
3998c2ecf20Sopenharmony_ci	case ETH_P_IPX:
4008c2ecf20Sopenharmony_ci	case ETH_P_802_3:
4018c2ecf20Sopenharmony_ci	case ETH_P_802_2:
4028c2ecf20Sopenharmony_ci		soft->proto = ARC_P_IPX;
4038c2ecf20Sopenharmony_ci		break;
4048c2ecf20Sopenharmony_ci	case ETH_P_ATALK:
4058c2ecf20Sopenharmony_ci		soft->proto = ARC_P_ATALK;
4068c2ecf20Sopenharmony_ci		break;
4078c2ecf20Sopenharmony_ci	default:
4088c2ecf20Sopenharmony_ci		arc_printk(D_NORMAL, dev, "RFC1201: I don't understand protocol %d (%Xh)\n",
4098c2ecf20Sopenharmony_ci			   type, type);
4108c2ecf20Sopenharmony_ci		dev->stats.tx_errors++;
4118c2ecf20Sopenharmony_ci		dev->stats.tx_aborted_errors++;
4128c2ecf20Sopenharmony_ci		return 0;
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	/* Set the source hardware address.
4168c2ecf20Sopenharmony_ci	 *
4178c2ecf20Sopenharmony_ci	 * This is pretty pointless for most purposes, but it can help in
4188c2ecf20Sopenharmony_ci	 * debugging.  ARCnet does not allow us to change the source address
4198c2ecf20Sopenharmony_ci	 * in the actual packet sent.
4208c2ecf20Sopenharmony_ci	 */
4218c2ecf20Sopenharmony_ci	pkt->hard.source = *dev->dev_addr;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	soft->sequence = htons(lp->rfc1201.sequence++);
4248c2ecf20Sopenharmony_ci	soft->split_flag = 0;	/* split packets are done elsewhere */
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	/* see linux/net/ethernet/eth.c to see where I got the following */
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
4298c2ecf20Sopenharmony_ci		/* FIXME: fill in the last byte of the dest ipaddr here
4308c2ecf20Sopenharmony_ci		 * to better comply with RFC1051 in "noarp" mode.
4318c2ecf20Sopenharmony_ci		 * For now, always broadcasting will probably at least get
4328c2ecf20Sopenharmony_ci		 * packets sent out :)
4338c2ecf20Sopenharmony_ci		 */
4348c2ecf20Sopenharmony_ci		pkt->hard.dest = 0;
4358c2ecf20Sopenharmony_ci		return hdr_size;
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci	/* otherwise, drop in the dest address */
4388c2ecf20Sopenharmony_ci	pkt->hard.dest = daddr;
4398c2ecf20Sopenharmony_ci	return hdr_size;
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_cistatic void load_pkt(struct net_device *dev, struct arc_hardware *hard,
4438c2ecf20Sopenharmony_ci		     struct arc_rfc1201 *soft, int softlen, int bufnum)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
4468c2ecf20Sopenharmony_ci	int ofs;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	/* assume length <= XMTU: someone should have handled that by now. */
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	if (softlen > MinTU) {
4518c2ecf20Sopenharmony_ci		hard->offset[0] = 0;
4528c2ecf20Sopenharmony_ci		hard->offset[1] = ofs = 512 - softlen;
4538c2ecf20Sopenharmony_ci	} else if (softlen > MTU) {	/* exception packet - add an extra header */
4548c2ecf20Sopenharmony_ci		struct arc_rfc1201 excsoft;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci		excsoft.proto = soft->proto;
4578c2ecf20Sopenharmony_ci		excsoft.split_flag = 0xff;
4588c2ecf20Sopenharmony_ci		excsoft.sequence = htons(0xffff);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci		hard->offset[0] = 0;
4618c2ecf20Sopenharmony_ci		ofs = 512 - softlen;
4628c2ecf20Sopenharmony_ci		hard->offset[1] = ofs - RFC1201_HDR_SIZE;
4638c2ecf20Sopenharmony_ci		lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE,
4648c2ecf20Sopenharmony_ci				    &excsoft, RFC1201_HDR_SIZE);
4658c2ecf20Sopenharmony_ci	} else {
4668c2ecf20Sopenharmony_ci		hard->offset[0] = ofs = 256 - softlen;
4678c2ecf20Sopenharmony_ci	}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
4708c2ecf20Sopenharmony_ci	lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	lp->lastload_dest = hard->dest;
4738c2ecf20Sopenharmony_ci}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_cistatic int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
4768c2ecf20Sopenharmony_ci		      int bufnum)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
4798c2ecf20Sopenharmony_ci	const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
4808c2ecf20Sopenharmony_ci	struct Outgoing *out;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
4838c2ecf20Sopenharmony_ci		   lp->next_tx, lp->cur_tx, bufnum);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	/* hard header is not included in packet length */
4868c2ecf20Sopenharmony_ci	length -= ARC_HDR_SIZE;
4878c2ecf20Sopenharmony_ci	pkt->soft.rfc1201.split_flag = 0;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	/* need to do a split packet? */
4908c2ecf20Sopenharmony_ci	if (length > XMTU) {
4918c2ecf20Sopenharmony_ci		out = &lp->outgoing;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		out->length = length - RFC1201_HDR_SIZE;
4948c2ecf20Sopenharmony_ci		out->dataleft = lp->outgoing.length;
4958c2ecf20Sopenharmony_ci		out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
4968c2ecf20Sopenharmony_ci		out->segnum = 0;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci		arc_printk(D_DURING, dev, "rfc1201 prep_tx: ready for %d-segment split (%d bytes, seq=%d)\n",
4998c2ecf20Sopenharmony_ci			   out->numsegs, out->length,
5008c2ecf20Sopenharmony_ci			   pkt->soft.rfc1201.sequence);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci		return 0;	/* not done */
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci	/* just load the packet into the buffers and send it off */
5058c2ecf20Sopenharmony_ci	load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	return 1;		/* done */
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic int continue_tx(struct net_device *dev, int bufnum)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci	struct arcnet_local *lp = netdev_priv(dev);
5138c2ecf20Sopenharmony_ci	struct Outgoing *out = &lp->outgoing;
5148c2ecf20Sopenharmony_ci	struct arc_hardware *hard = &out->pkt->hard;
5158c2ecf20Sopenharmony_ci	struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
5168c2ecf20Sopenharmony_ci	int maxsegsize = XMTU - RFC1201_HDR_SIZE;
5178c2ecf20Sopenharmony_ci	int seglen;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	arc_printk(D_DURING, dev,
5208c2ecf20Sopenharmony_ci		   "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
5218c2ecf20Sopenharmony_ci		   out->segnum, out->numsegs, soft->sequence);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	/* the "new" soft header comes right before the data chunk */
5248c2ecf20Sopenharmony_ci	newsoft = (struct arc_rfc1201 *)
5258c2ecf20Sopenharmony_ci	    (out->pkt->soft.raw + out->length - out->dataleft);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (!out->segnum)	/* first packet; newsoft == soft */
5288c2ecf20Sopenharmony_ci		newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
5298c2ecf20Sopenharmony_ci	else {
5308c2ecf20Sopenharmony_ci		newsoft->split_flag = out->segnum << 1;
5318c2ecf20Sopenharmony_ci		newsoft->proto = soft->proto;
5328c2ecf20Sopenharmony_ci		newsoft->sequence = soft->sequence;
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	seglen = maxsegsize;
5368c2ecf20Sopenharmony_ci	if (seglen > out->dataleft)
5378c2ecf20Sopenharmony_ci		seglen = out->dataleft;
5388c2ecf20Sopenharmony_ci	out->dataleft -= seglen;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	out->segnum++;
5438c2ecf20Sopenharmony_ci	if (out->segnum >= out->numsegs)
5448c2ecf20Sopenharmony_ci		return 1;
5458c2ecf20Sopenharmony_ci	else
5468c2ecf20Sopenharmony_ci		return 0;
5478c2ecf20Sopenharmony_ci}
548