1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Linux ARCnet driver - RFC1051 ("simple" standard) packet encapsulation
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Written 1994-1999 by Avery Pennarun.
5*4882a593Smuzhiyun * Derived from skeleton.c by Donald Becker.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
8*4882a593Smuzhiyun * for sponsoring the further development of this driver.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * **********************
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * The original copyright of skeleton.c was as follows:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * skeleton.c Written 1993 by Donald Becker.
15*4882a593Smuzhiyun * Copyright 1993 United States Government as represented by the
16*4882a593Smuzhiyun * Director, National Security Agency. This software may only be used
17*4882a593Smuzhiyun * and distributed according to the terms of the GNU General Public License as
18*4882a593Smuzhiyun * modified by SRC, incorporated herein by reference.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * **********************
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * For more details, see drivers/net/arcnet.c
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * **********************
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <linux/module.h>
30*4882a593Smuzhiyun #include <linux/gfp.h>
31*4882a593Smuzhiyun #include <linux/init.h>
32*4882a593Smuzhiyun #include <linux/if_arp.h>
33*4882a593Smuzhiyun #include <net/arp.h>
34*4882a593Smuzhiyun #include <linux/netdevice.h>
35*4882a593Smuzhiyun #include <linux/skbuff.h>
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #include "arcdevice.h"
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun static __be16 type_trans(struct sk_buff *skb, struct net_device *dev);
40*4882a593Smuzhiyun static void rx(struct net_device *dev, int bufnum,
41*4882a593Smuzhiyun struct archdr *pkthdr, int length);
42*4882a593Smuzhiyun static int build_header(struct sk_buff *skb, struct net_device *dev,
43*4882a593Smuzhiyun unsigned short type, uint8_t daddr);
44*4882a593Smuzhiyun static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
45*4882a593Smuzhiyun int bufnum);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun static struct ArcProto rfc1051_proto = {
48*4882a593Smuzhiyun .suffix = 's',
49*4882a593Smuzhiyun .mtu = XMTU - RFC1051_HDR_SIZE,
50*4882a593Smuzhiyun .is_ip = 1,
51*4882a593Smuzhiyun .rx = rx,
52*4882a593Smuzhiyun .build_header = build_header,
53*4882a593Smuzhiyun .prepare_tx = prepare_tx,
54*4882a593Smuzhiyun .continue_tx = NULL,
55*4882a593Smuzhiyun .ack_tx = NULL
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun
arcnet_rfc1051_init(void)58*4882a593Smuzhiyun static int __init arcnet_rfc1051_init(void)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun pr_info("%s\n", "RFC1051 \"simple standard\" (`s') encapsulation support loaded");
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun arc_proto_map[ARC_P_IP_RFC1051]
63*4882a593Smuzhiyun = arc_proto_map[ARC_P_ARP_RFC1051]
64*4882a593Smuzhiyun = &rfc1051_proto;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* if someone else already owns the broadcast, we won't take it */
67*4882a593Smuzhiyun if (arc_bcast_proto == arc_proto_default)
68*4882a593Smuzhiyun arc_bcast_proto = &rfc1051_proto;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun return 0;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
arcnet_rfc1051_exit(void)73*4882a593Smuzhiyun static void __exit arcnet_rfc1051_exit(void)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun arcnet_unregister_proto(&rfc1051_proto);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun module_init(arcnet_rfc1051_init);
79*4882a593Smuzhiyun module_exit(arcnet_rfc1051_exit);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun MODULE_LICENSE("GPL");
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* Determine a packet's protocol ID.
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * With ARCnet we have to convert everything to Ethernet-style stuff.
86*4882a593Smuzhiyun */
type_trans(struct sk_buff * skb,struct net_device * dev)87*4882a593Smuzhiyun static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun struct archdr *pkt = (struct archdr *)skb->data;
90*4882a593Smuzhiyun struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
91*4882a593Smuzhiyun int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* Pull off the arcnet header. */
94*4882a593Smuzhiyun skb_reset_mac_header(skb);
95*4882a593Smuzhiyun skb_pull(skb, hdr_size);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun if (pkt->hard.dest == 0) {
98*4882a593Smuzhiyun skb->pkt_type = PACKET_BROADCAST;
99*4882a593Smuzhiyun } else if (dev->flags & IFF_PROMISC) {
100*4882a593Smuzhiyun /* if we're not sending to ourselves :) */
101*4882a593Smuzhiyun if (pkt->hard.dest != dev->dev_addr[0])
102*4882a593Smuzhiyun skb->pkt_type = PACKET_OTHERHOST;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun /* now return the protocol number */
105*4882a593Smuzhiyun switch (soft->proto) {
106*4882a593Smuzhiyun case ARC_P_IP_RFC1051:
107*4882a593Smuzhiyun return htons(ETH_P_IP);
108*4882a593Smuzhiyun case ARC_P_ARP_RFC1051:
109*4882a593Smuzhiyun return htons(ETH_P_ARP);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun default:
112*4882a593Smuzhiyun dev->stats.rx_errors++;
113*4882a593Smuzhiyun dev->stats.rx_crc_errors++;
114*4882a593Smuzhiyun return 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun return htons(ETH_P_IP);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /* packet receiver */
rx(struct net_device * dev,int bufnum,struct archdr * pkthdr,int length)121*4882a593Smuzhiyun static void rx(struct net_device *dev, int bufnum,
122*4882a593Smuzhiyun struct archdr *pkthdr, int length)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun struct arcnet_local *lp = netdev_priv(dev);
125*4882a593Smuzhiyun struct sk_buff *skb;
126*4882a593Smuzhiyun struct archdr *pkt = pkthdr;
127*4882a593Smuzhiyun int ofs;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun arc_printk(D_DURING, dev, "it's a raw packet (length=%d)\n", length);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun if (length >= MinTU)
132*4882a593Smuzhiyun ofs = 512 - length;
133*4882a593Smuzhiyun else
134*4882a593Smuzhiyun ofs = 256 - length;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
137*4882a593Smuzhiyun if (!skb) {
138*4882a593Smuzhiyun dev->stats.rx_dropped++;
139*4882a593Smuzhiyun return;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun skb_put(skb, length + ARC_HDR_SIZE);
142*4882a593Smuzhiyun skb->dev = dev;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun pkt = (struct archdr *)skb->data;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* up to sizeof(pkt->soft) has already been copied from the card */
147*4882a593Smuzhiyun memcpy(pkt, pkthdr, sizeof(struct archdr));
148*4882a593Smuzhiyun if (length > sizeof(pkt->soft))
149*4882a593Smuzhiyun lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
150*4882a593Smuzhiyun pkt->soft.raw + sizeof(pkt->soft),
151*4882a593Smuzhiyun length - sizeof(pkt->soft));
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun if (BUGLVL(D_SKB))
154*4882a593Smuzhiyun arcnet_dump_skb(dev, skb, "rx");
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun skb->protocol = type_trans(skb, dev);
157*4882a593Smuzhiyun netif_rx(skb);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /* Create the ARCnet hard/soft headers for RFC1051 */
build_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,uint8_t daddr)161*4882a593Smuzhiyun static int build_header(struct sk_buff *skb, struct net_device *dev,
162*4882a593Smuzhiyun unsigned short type, uint8_t daddr)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
165*4882a593Smuzhiyun struct archdr *pkt = skb_push(skb, hdr_size);
166*4882a593Smuzhiyun struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* set the protocol ID according to RFC1051 */
169*4882a593Smuzhiyun switch (type) {
170*4882a593Smuzhiyun case ETH_P_IP:
171*4882a593Smuzhiyun soft->proto = ARC_P_IP_RFC1051;
172*4882a593Smuzhiyun break;
173*4882a593Smuzhiyun case ETH_P_ARP:
174*4882a593Smuzhiyun soft->proto = ARC_P_ARP_RFC1051;
175*4882a593Smuzhiyun break;
176*4882a593Smuzhiyun default:
177*4882a593Smuzhiyun arc_printk(D_NORMAL, dev, "RFC1051: I don't understand protocol %d (%Xh)\n",
178*4882a593Smuzhiyun type, type);
179*4882a593Smuzhiyun dev->stats.tx_errors++;
180*4882a593Smuzhiyun dev->stats.tx_aborted_errors++;
181*4882a593Smuzhiyun return 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /* Set the source hardware address.
185*4882a593Smuzhiyun *
186*4882a593Smuzhiyun * This is pretty pointless for most purposes, but it can help in
187*4882a593Smuzhiyun * debugging. ARCnet does not allow us to change the source address
188*4882a593Smuzhiyun * in the actual packet sent.
189*4882a593Smuzhiyun */
190*4882a593Smuzhiyun pkt->hard.source = *dev->dev_addr;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /* see linux/net/ethernet/eth.c to see where I got the following */
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
195*4882a593Smuzhiyun /* FIXME: fill in the last byte of the dest ipaddr here to
196*4882a593Smuzhiyun * better comply with RFC1051 in "noarp" mode.
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun pkt->hard.dest = 0;
199*4882a593Smuzhiyun return hdr_size;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun /* otherwise, just fill it in and go! */
202*4882a593Smuzhiyun pkt->hard.dest = daddr;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun return hdr_size; /* success */
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
prepare_tx(struct net_device * dev,struct archdr * pkt,int length,int bufnum)207*4882a593Smuzhiyun static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
208*4882a593Smuzhiyun int bufnum)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun struct arcnet_local *lp = netdev_priv(dev);
211*4882a593Smuzhiyun struct arc_hardware *hard = &pkt->hard;
212*4882a593Smuzhiyun int ofs;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
215*4882a593Smuzhiyun lp->next_tx, lp->cur_tx, bufnum);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* hard header is not included in packet length */
218*4882a593Smuzhiyun length -= ARC_HDR_SIZE;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun if (length > XMTU) {
221*4882a593Smuzhiyun /* should never happen! other people already check for this. */
222*4882a593Smuzhiyun arc_printk(D_NORMAL, dev, "Bug! prepare_tx with size %d (> %d)\n",
223*4882a593Smuzhiyun length, XMTU);
224*4882a593Smuzhiyun length = XMTU;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun if (length > MinTU) {
227*4882a593Smuzhiyun hard->offset[0] = 0;
228*4882a593Smuzhiyun hard->offset[1] = ofs = 512 - length;
229*4882a593Smuzhiyun } else if (length > MTU) {
230*4882a593Smuzhiyun hard->offset[0] = 0;
231*4882a593Smuzhiyun hard->offset[1] = ofs = 512 - length - 3;
232*4882a593Smuzhiyun } else {
233*4882a593Smuzhiyun hard->offset[0] = ofs = 256 - length;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
237*4882a593Smuzhiyun lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun lp->lastload_dest = hard->dest;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun return 1; /* done */
242*4882a593Smuzhiyun }
243