xref: /linux/drivers/net/arcnet/rfc1051.c (revision d6d7d3ed56e3bfe7fd34108dbe23f0610e3d8621)
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  */
2605a24b23SJoe Perches 
2705a24b23SJoe Perches #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
2805a24b23SJoe Perches 
291da177e4SLinus Torvalds #include <linux/module.h>
305a0e3ad6STejun Heo #include <linux/gfp.h>
311da177e4SLinus Torvalds #include <linux/init.h>
321da177e4SLinus Torvalds #include <linux/if_arp.h>
331da177e4SLinus Torvalds #include <net/arp.h>
341da177e4SLinus Torvalds #include <linux/netdevice.h>
351da177e4SLinus Torvalds #include <linux/skbuff.h>
361da177e4SLinus Torvalds #include <linux/arcdevice.h>
371da177e4SLinus Torvalds 
38701181acSAl Viro static __be16 type_trans(struct sk_buff *skb, struct net_device *dev);
391da177e4SLinus Torvalds static void rx(struct net_device *dev, int bufnum,
401da177e4SLinus Torvalds 	       struct archdr *pkthdr, int length);
411da177e4SLinus Torvalds static int build_header(struct sk_buff *skb, struct net_device *dev,
421da177e4SLinus Torvalds 			unsigned short type, uint8_t daddr);
431da177e4SLinus Torvalds static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
441da177e4SLinus Torvalds 		      int bufnum);
451da177e4SLinus Torvalds 
467f5e760cSJoe Perches static struct ArcProto rfc1051_proto = {
471da177e4SLinus Torvalds 	.suffix		= 's',
481da177e4SLinus Torvalds 	.mtu		= XMTU - RFC1051_HDR_SIZE,
491da177e4SLinus Torvalds 	.is_ip          = 1,
501da177e4SLinus Torvalds 	.rx		= rx,
511da177e4SLinus Torvalds 	.build_header	= build_header,
521da177e4SLinus Torvalds 	.prepare_tx	= prepare_tx,
531da177e4SLinus Torvalds 	.continue_tx    = NULL,
541da177e4SLinus Torvalds 	.ack_tx         = NULL
551da177e4SLinus Torvalds };
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static int __init arcnet_rfc1051_init(void)
581da177e4SLinus Torvalds {
5905a24b23SJoe Perches 	pr_info("%s\n", "RFC1051 \"simple standard\" (`s') encapsulation support loaded");
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 	arc_proto_map[ARC_P_IP_RFC1051]
621da177e4SLinus Torvalds 	    = arc_proto_map[ARC_P_ARP_RFC1051]
631da177e4SLinus Torvalds 	    = &rfc1051_proto;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	/* if someone else already owns the broadcast, we won't take it */
661da177e4SLinus Torvalds 	if (arc_bcast_proto == arc_proto_default)
671da177e4SLinus Torvalds 		arc_bcast_proto = &rfc1051_proto;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	return 0;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static void __exit arcnet_rfc1051_exit(void)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	arcnet_unregister_proto(&rfc1051_proto);
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds module_init(arcnet_rfc1051_init);
781da177e4SLinus Torvalds module_exit(arcnet_rfc1051_exit);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds MODULE_LICENSE("GPL");
811da177e4SLinus Torvalds 
82f2f0a16bSJoe Perches /* Determine a packet's protocol ID.
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * With ARCnet we have to convert everything to Ethernet-style stuff.
851da177e4SLinus Torvalds  */
86701181acSAl Viro static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	struct archdr *pkt = (struct archdr *)skb->data;
891da177e4SLinus Torvalds 	struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
901da177e4SLinus Torvalds 	int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	/* Pull off the arcnet header. */
93459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
941da177e4SLinus Torvalds 	skb_pull(skb, hdr_size);
951da177e4SLinus Torvalds 
967f5e760cSJoe Perches 	if (pkt->hard.dest == 0) {
971da177e4SLinus Torvalds 		skb->pkt_type = PACKET_BROADCAST;
987f5e760cSJoe Perches 	} else if (dev->flags & IFF_PROMISC) {
991da177e4SLinus Torvalds 		/* if we're not sending to ourselves :) */
1001da177e4SLinus Torvalds 		if (pkt->hard.dest != dev->dev_addr[0])
1011da177e4SLinus Torvalds 			skb->pkt_type = PACKET_OTHERHOST;
1021da177e4SLinus Torvalds 	}
1031da177e4SLinus Torvalds 	/* now return the protocol number */
1041da177e4SLinus Torvalds 	switch (soft->proto) {
1051da177e4SLinus Torvalds 	case ARC_P_IP_RFC1051:
1061da177e4SLinus Torvalds 		return htons(ETH_P_IP);
1071da177e4SLinus Torvalds 	case ARC_P_ARP_RFC1051:
1081da177e4SLinus Torvalds 		return htons(ETH_P_ARP);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	default:
1115803c512SStephen Hemminger 		dev->stats.rx_errors++;
1125803c512SStephen Hemminger 		dev->stats.rx_crc_errors++;
1131da177e4SLinus Torvalds 		return 0;
1141da177e4SLinus Torvalds 	}
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	return htons(ETH_P_IP);
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /* packet receiver */
1201da177e4SLinus Torvalds static void rx(struct net_device *dev, int bufnum,
1211da177e4SLinus Torvalds 	       struct archdr *pkthdr, int length)
1221da177e4SLinus Torvalds {
123454d7c9bSWang Chen 	struct arcnet_local *lp = netdev_priv(dev);
1241da177e4SLinus Torvalds 	struct sk_buff *skb;
1251da177e4SLinus Torvalds 	struct archdr *pkt = pkthdr;
1261da177e4SLinus Torvalds 	int ofs;
1271da177e4SLinus Torvalds 
128a34c0932SJoe Perches 	arc_printk(D_DURING, dev, "it's a raw packet (length=%d)\n", length);
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	if (length >= MinTU)
1311da177e4SLinus Torvalds 		ofs = 512 - length;
1321da177e4SLinus Torvalds 	else
1331da177e4SLinus Torvalds 		ofs = 256 - length;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
136d8101e7bSJoe Perches 	if (!skb) {
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 
15272aeea48SJoe Perches 	if (BUGLVL(D_SKB))
15372aeea48SJoe 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:
176a34c0932SJoe Perches 		arc_printk(D_NORMAL, dev, "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 
213a34c0932SJoe Perches 	arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
2141da177e4SLinus Torvalds 		   lp->next_tx, lp->cur_tx, bufnum);
2151da177e4SLinus Torvalds 
216*d6d7d3edSJoe Perches 	/* hard header is not included in packet length */
217*d6d7d3edSJoe Perches 	length -= ARC_HDR_SIZE;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	if (length > XMTU) {
2201da177e4SLinus Torvalds 		/* should never happen! other people already check for this. */
221a34c0932SJoe Perches 		arc_printk(D_NORMAL, dev, "Bug!  prepare_tx with size %d (> %d)\n",
2221da177e4SLinus Torvalds 			   length, XMTU);
2231da177e4SLinus Torvalds 		length = XMTU;
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 	if (length > MinTU) {
2261da177e4SLinus Torvalds 		hard->offset[0] = 0;
2271da177e4SLinus Torvalds 		hard->offset[1] = ofs = 512 - length;
2281da177e4SLinus Torvalds 	} else if (length > MTU) {
2291da177e4SLinus Torvalds 		hard->offset[0] = 0;
2301da177e4SLinus Torvalds 		hard->offset[1] = ofs = 512 - length - 3;
2317f5e760cSJoe Perches 	} else {
2321da177e4SLinus Torvalds 		hard->offset[0] = ofs = 256 - length;
2337f5e760cSJoe Perches 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
2361da177e4SLinus Torvalds 	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	lp->lastload_dest = hard->dest;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	return 1;		/* done */
2411da177e4SLinus Torvalds }
242