xref: /linux/drivers/net/arcnet/rfc1051.c (revision 72aeea4841c037b9b3abf65859673cbd7b6664a9)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Linux ARCnet driver - RFC1051 ("simple" standard) packet encapsulation
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Written 1994-1999 by Avery Pennarun.
51da177e4SLinus Torvalds  * Derived from skeleton.c by Donald Becker.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
81da177e4SLinus Torvalds  *  for sponsoring the further development of this driver.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * **********************
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * The original copyright of skeleton.c was as follows:
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * skeleton.c Written 1993 by Donald Becker.
151da177e4SLinus Torvalds  * Copyright 1993 United States Government as represented by the
161da177e4SLinus Torvalds  * Director, National Security Agency.  This software may only be used
171da177e4SLinus Torvalds  * and distributed according to the terms of the GNU General Public License as
181da177e4SLinus Torvalds  * modified by SRC, incorporated herein by reference.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * **********************
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * For more details, see drivers/net/arcnet.c
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * **********************
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds #include <linux/module.h>
275a0e3ad6STejun Heo #include <linux/gfp.h>
281da177e4SLinus Torvalds #include <linux/init.h>
291da177e4SLinus Torvalds #include <linux/if_arp.h>
301da177e4SLinus Torvalds #include <net/arp.h>
311da177e4SLinus Torvalds #include <linux/netdevice.h>
321da177e4SLinus Torvalds #include <linux/skbuff.h>
331da177e4SLinus Torvalds #include <linux/arcdevice.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #define VERSION "arcnet: RFC1051 \"simple standard\" (`s') encapsulation support loaded.\n"
361da177e4SLinus Torvalds 
37701181acSAl Viro static __be16 type_trans(struct sk_buff *skb, struct net_device *dev);
381da177e4SLinus Torvalds static void rx(struct net_device *dev, int bufnum,
391da177e4SLinus Torvalds 	       struct archdr *pkthdr, int length);
401da177e4SLinus Torvalds static int build_header(struct sk_buff *skb, struct net_device *dev,
411da177e4SLinus Torvalds 			unsigned short type, uint8_t daddr);
421da177e4SLinus Torvalds static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
431da177e4SLinus Torvalds 		      int bufnum);
441da177e4SLinus Torvalds 
457f5e760cSJoe Perches static struct ArcProto rfc1051_proto = {
461da177e4SLinus Torvalds 	.suffix		= 's',
471da177e4SLinus Torvalds 	.mtu		= XMTU - RFC1051_HDR_SIZE,
481da177e4SLinus Torvalds 	.is_ip          = 1,
491da177e4SLinus Torvalds 	.rx		= rx,
501da177e4SLinus Torvalds 	.build_header	= build_header,
511da177e4SLinus Torvalds 	.prepare_tx	= prepare_tx,
521da177e4SLinus Torvalds 	.continue_tx    = NULL,
531da177e4SLinus Torvalds 	.ack_tx         = NULL
541da177e4SLinus Torvalds };
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static int __init arcnet_rfc1051_init(void)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	printk(VERSION);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	arc_proto_map[ARC_P_IP_RFC1051]
611da177e4SLinus Torvalds 	    = arc_proto_map[ARC_P_ARP_RFC1051]
621da177e4SLinus Torvalds 	    = &rfc1051_proto;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	/* if someone else already owns the broadcast, we won't take it */
651da177e4SLinus Torvalds 	if (arc_bcast_proto == arc_proto_default)
661da177e4SLinus Torvalds 		arc_bcast_proto = &rfc1051_proto;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	return 0;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds static void __exit arcnet_rfc1051_exit(void)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds 	arcnet_unregister_proto(&rfc1051_proto);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds module_init(arcnet_rfc1051_init);
771da177e4SLinus Torvalds module_exit(arcnet_rfc1051_exit);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds MODULE_LICENSE("GPL");
801da177e4SLinus Torvalds 
81f2f0a16bSJoe Perches /* Determine a packet's protocol ID.
821da177e4SLinus Torvalds  *
831da177e4SLinus Torvalds  * With ARCnet we have to convert everything to Ethernet-style stuff.
841da177e4SLinus Torvalds  */
85701181acSAl Viro static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	struct archdr *pkt = (struct archdr *)skb->data;
881da177e4SLinus Torvalds 	struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
891da177e4SLinus Torvalds 	int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	/* Pull off the arcnet header. */
92459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
931da177e4SLinus Torvalds 	skb_pull(skb, hdr_size);
941da177e4SLinus Torvalds 
957f5e760cSJoe Perches 	if (pkt->hard.dest == 0) {
961da177e4SLinus Torvalds 		skb->pkt_type = PACKET_BROADCAST;
977f5e760cSJoe Perches 	} else if (dev->flags & IFF_PROMISC) {
981da177e4SLinus Torvalds 		/* if we're not sending to ourselves :) */
991da177e4SLinus Torvalds 		if (pkt->hard.dest != dev->dev_addr[0])
1001da177e4SLinus Torvalds 			skb->pkt_type = PACKET_OTHERHOST;
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds 	/* now return the protocol number */
1031da177e4SLinus Torvalds 	switch (soft->proto) {
1041da177e4SLinus Torvalds 	case ARC_P_IP_RFC1051:
1051da177e4SLinus Torvalds 		return htons(ETH_P_IP);
1061da177e4SLinus Torvalds 	case ARC_P_ARP_RFC1051:
1071da177e4SLinus Torvalds 		return htons(ETH_P_ARP);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	default:
1105803c512SStephen Hemminger 		dev->stats.rx_errors++;
1115803c512SStephen Hemminger 		dev->stats.rx_crc_errors++;
1121da177e4SLinus Torvalds 		return 0;
1131da177e4SLinus Torvalds 	}
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	return htons(ETH_P_IP);
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds /* packet receiver */
1191da177e4SLinus Torvalds static void rx(struct net_device *dev, int bufnum,
1201da177e4SLinus Torvalds 	       struct archdr *pkthdr, int length)
1211da177e4SLinus Torvalds {
122454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
1231da177e4SLinus Torvalds 	struct sk_buff *skb;
1241da177e4SLinus Torvalds 	struct archdr *pkt = pkthdr;
1251da177e4SLinus Torvalds 	int ofs;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	BUGMSG(D_DURING, "it's a raw packet (length=%d)\n", length);
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	if (length >= MinTU)
1301da177e4SLinus Torvalds 		ofs = 512 - length;
1311da177e4SLinus Torvalds 	else
1321da177e4SLinus Torvalds 		ofs = 256 - length;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
1351da177e4SLinus Torvalds 	if (skb == NULL) {
1361da177e4SLinus Torvalds 		BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
1375803c512SStephen Hemminger 		dev->stats.rx_dropped++;
1381da177e4SLinus Torvalds 		return;
1391da177e4SLinus Torvalds 	}
1401da177e4SLinus Torvalds 	skb_put(skb, length + ARC_HDR_SIZE);
1411da177e4SLinus Torvalds 	skb->dev = dev;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	pkt = (struct archdr *)skb->data;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	/* up to sizeof(pkt->soft) has already been copied from the card */
1461da177e4SLinus Torvalds 	memcpy(pkt, pkthdr, sizeof(struct archdr));
1471da177e4SLinus Torvalds 	if (length > sizeof(pkt->soft))
1481da177e4SLinus Torvalds 		lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
1491da177e4SLinus Torvalds 				      pkt->soft.raw + sizeof(pkt->soft),
1501da177e4SLinus Torvalds 				      length - sizeof(pkt->soft));
1511da177e4SLinus Torvalds 
152*72aeea48SJoe Perches 	if (BUGLVL(D_SKB))
153*72aeea48SJoe Perches 		arcnet_dump_skb(dev, skb, "rx");
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	skb->protocol = type_trans(skb, dev);
1561da177e4SLinus Torvalds 	netif_rx(skb);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
159f2f0a16bSJoe Perches /* Create the ARCnet hard/soft headers for RFC1051 */
1601da177e4SLinus Torvalds static int build_header(struct sk_buff *skb, struct net_device *dev,
1611da177e4SLinus Torvalds 			unsigned short type, uint8_t daddr)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
1641da177e4SLinus Torvalds 	struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
1651da177e4SLinus Torvalds 	struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	/* set the protocol ID according to RFC1051 */
1681da177e4SLinus Torvalds 	switch (type) {
1691da177e4SLinus Torvalds 	case ETH_P_IP:
1701da177e4SLinus Torvalds 		soft->proto = ARC_P_IP_RFC1051;
1711da177e4SLinus Torvalds 		break;
1721da177e4SLinus Torvalds 	case ETH_P_ARP:
1731da177e4SLinus Torvalds 		soft->proto = ARC_P_ARP_RFC1051;
1741da177e4SLinus Torvalds 		break;
1751da177e4SLinus Torvalds 	default:
1761da177e4SLinus Torvalds 		BUGMSG(D_NORMAL, "RFC1051: I don't understand protocol %d (%Xh)\n",
1771da177e4SLinus Torvalds 		       type, type);
1785803c512SStephen Hemminger 		dev->stats.tx_errors++;
1795803c512SStephen Hemminger 		dev->stats.tx_aborted_errors++;
1801da177e4SLinus Torvalds 		return 0;
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 
183f2f0a16bSJoe Perches 	/* Set the source hardware address.
1841da177e4SLinus Torvalds 	 *
1851da177e4SLinus Torvalds 	 * This is pretty pointless for most purposes, but it can help in
186f2f0a16bSJoe Perches 	 * debugging.  ARCnet does not allow us to change the source address
187f2f0a16bSJoe Perches 	 * in the actual packet sent.
1881da177e4SLinus Torvalds 	 */
1891da177e4SLinus Torvalds 	pkt->hard.source = *dev->dev_addr;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	/* see linux/net/ethernet/eth.c to see where I got the following */
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
194f2f0a16bSJoe Perches 		/* FIXME: fill in the last byte of the dest ipaddr here to
195f2f0a16bSJoe Perches 		 * better comply with RFC1051 in "noarp" mode.
1961da177e4SLinus Torvalds 		 */
1971da177e4SLinus Torvalds 		pkt->hard.dest = 0;
1981da177e4SLinus Torvalds 		return hdr_size;
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 	/* otherwise, just fill it in and go! */
2011da177e4SLinus Torvalds 	pkt->hard.dest = daddr;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	return hdr_size;	/* success */
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
2071da177e4SLinus Torvalds 		      int bufnum)
2081da177e4SLinus Torvalds {
209454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
2101da177e4SLinus Torvalds 	struct arc_hardware *hard = &pkt->hard;
2111da177e4SLinus Torvalds 	int ofs;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
2141da177e4SLinus Torvalds 	       lp->next_tx, lp->cur_tx, bufnum);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	length -= ARC_HDR_SIZE;	/* hard header is not included in packet length */
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if (length > XMTU) {
2191da177e4SLinus Torvalds 		/* should never happen! other people already check for this. */
2201da177e4SLinus Torvalds 		BUGMSG(D_NORMAL, "Bug!  prepare_tx with size %d (> %d)\n",
2211da177e4SLinus Torvalds 		       length, XMTU);
2221da177e4SLinus Torvalds 		length = XMTU;
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds 	if (length > MinTU) {
2251da177e4SLinus Torvalds 		hard->offset[0] = 0;
2261da177e4SLinus Torvalds 		hard->offset[1] = ofs = 512 - length;
2271da177e4SLinus Torvalds 	} else if (length > MTU) {
2281da177e4SLinus Torvalds 		hard->offset[0] = 0;
2291da177e4SLinus Torvalds 		hard->offset[1] = ofs = 512 - length - 3;
2307f5e760cSJoe Perches 	} else {
2311da177e4SLinus Torvalds 		hard->offset[0] = ofs = 256 - length;
2327f5e760cSJoe Perches 	}
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
2351da177e4SLinus Torvalds 	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	lp->lastload_dest = hard->dest;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	return 1;		/* done */
2401da177e4SLinus Torvalds }
241