xref: /OK3568_Linux_fs/kernel/drivers/net/arcnet/capmode.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Linux ARCnet driver - "cap mode" packet encapsulation.
3*4882a593Smuzhiyun  * It adds sequence numbers to packets for communicating between a user space
4*4882a593Smuzhiyun  * application and the driver. After a transmit it sends a packet with protocol
5*4882a593Smuzhiyun  * byte 0 back up to the userspace containing the sequence number of the packet
6*4882a593Smuzhiyun  * plus the transmit-status on the ArcNet.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Written 2002-4 by Esben Nielsen, Vestas Wind Systems A/S
9*4882a593Smuzhiyun  * Derived from arc-rawmode.c by Avery Pennarun.
10*4882a593Smuzhiyun  * arc-rawmode was in turned based on skeleton.c, see below.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * **********************
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The original copyright of skeleton.c was as follows:
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * skeleton.c Written 1993 by Donald Becker.
17*4882a593Smuzhiyun  * Copyright 1993 United States Government as represented by the
18*4882a593Smuzhiyun  * Director, National Security Agency.  This software may only be used
19*4882a593Smuzhiyun  * and distributed according to the terms of the GNU General Public License as
20*4882a593Smuzhiyun  * modified by SRC, incorporated herein by reference.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * **********************
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * For more details, see drivers/net/arcnet.c
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * **********************
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <linux/module.h>
32*4882a593Smuzhiyun #include <linux/gfp.h>
33*4882a593Smuzhiyun #include <linux/init.h>
34*4882a593Smuzhiyun #include <linux/if_arp.h>
35*4882a593Smuzhiyun #include <net/arp.h>
36*4882a593Smuzhiyun #include <linux/netdevice.h>
37*4882a593Smuzhiyun #include <linux/skbuff.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include "arcdevice.h"
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /* packet receiver */
rx(struct net_device * dev,int bufnum,struct archdr * pkthdr,int length)42*4882a593Smuzhiyun static void rx(struct net_device *dev, int bufnum,
43*4882a593Smuzhiyun 	       struct archdr *pkthdr, int length)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	struct arcnet_local *lp = netdev_priv(dev);
46*4882a593Smuzhiyun 	struct sk_buff *skb;
47*4882a593Smuzhiyun 	struct archdr *pkt;
48*4882a593Smuzhiyun 	char *pktbuf, *pkthdrbuf;
49*4882a593Smuzhiyun 	int ofs;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	arc_printk(D_DURING, dev, "it's a raw(cap) packet (length=%d)\n",
52*4882a593Smuzhiyun 		   length);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	if (length >= MinTU)
55*4882a593Smuzhiyun 		ofs = 512 - length;
56*4882a593Smuzhiyun 	else
57*4882a593Smuzhiyun 		ofs = 256 - length;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC);
60*4882a593Smuzhiyun 	if (!skb) {
61*4882a593Smuzhiyun 		dev->stats.rx_dropped++;
62*4882a593Smuzhiyun 		return;
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun 	skb_put(skb, length + ARC_HDR_SIZE + sizeof(int));
65*4882a593Smuzhiyun 	skb->dev = dev;
66*4882a593Smuzhiyun 	skb_reset_mac_header(skb);
67*4882a593Smuzhiyun 	pkt = (struct archdr *)skb_mac_header(skb);
68*4882a593Smuzhiyun 	skb_pull(skb, ARC_HDR_SIZE);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/* up to sizeof(pkt->soft) has already been copied from the card
71*4882a593Smuzhiyun 	 * squeeze in an int for the cap encapsulation
72*4882a593Smuzhiyun 	 * use these variables to be sure we count in bytes, not in
73*4882a593Smuzhiyun 	 * sizeof(struct archdr)
74*4882a593Smuzhiyun 	 */
75*4882a593Smuzhiyun 	pktbuf = (char *)pkt;
76*4882a593Smuzhiyun 	pkthdrbuf = (char *)pkthdr;
77*4882a593Smuzhiyun 	memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto));
78*4882a593Smuzhiyun 	memcpy(pktbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto) + sizeof(int),
79*4882a593Smuzhiyun 	       pkthdrbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto),
80*4882a593Smuzhiyun 	       sizeof(struct archdr) - ARC_HDR_SIZE - sizeof(pkt->soft.cap.proto));
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	if (length > sizeof(pkt->soft))
83*4882a593Smuzhiyun 		lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
84*4882a593Smuzhiyun 				      pkt->soft.raw + sizeof(pkt->soft)
85*4882a593Smuzhiyun 				      + sizeof(int),
86*4882a593Smuzhiyun 				      length - sizeof(pkt->soft));
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	if (BUGLVL(D_SKB))
89*4882a593Smuzhiyun 		arcnet_dump_skb(dev, skb, "rx");
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	skb->protocol = cpu_to_be16(ETH_P_ARCNET);
92*4882a593Smuzhiyun 	netif_rx(skb);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun /* Create the ARCnet hard/soft headers for cap mode.
96*4882a593Smuzhiyun  * There aren't any soft headers in cap mode - not even the protocol id.
97*4882a593Smuzhiyun  */
build_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,uint8_t daddr)98*4882a593Smuzhiyun static int build_header(struct sk_buff *skb,
99*4882a593Smuzhiyun 			struct net_device *dev,
100*4882a593Smuzhiyun 			unsigned short type,
101*4882a593Smuzhiyun 			uint8_t daddr)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	int hdr_size = ARC_HDR_SIZE;
104*4882a593Smuzhiyun 	struct archdr *pkt = skb_push(skb, hdr_size);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	arc_printk(D_PROTO, dev, "Preparing header for cap packet %x.\n",
107*4882a593Smuzhiyun 		   *((int *)&pkt->soft.cap.cookie[0]));
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	/* Set the source hardware address.
110*4882a593Smuzhiyun 	 *
111*4882a593Smuzhiyun 	 * This is pretty pointless for most purposes, but it can help in
112*4882a593Smuzhiyun 	 * debugging.  ARCnet does not allow us to change the source address in
113*4882a593Smuzhiyun 	 * the actual packet sent)
114*4882a593Smuzhiyun 	 */
115*4882a593Smuzhiyun 	pkt->hard.source = *dev->dev_addr;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/* see linux/net/ethernet/eth.c to see where I got the following */
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
120*4882a593Smuzhiyun 		/* FIXME: fill in the last byte of the dest ipaddr here to
121*4882a593Smuzhiyun 		 * better comply with RFC1051 in "noarp" mode.
122*4882a593Smuzhiyun 		 */
123*4882a593Smuzhiyun 		pkt->hard.dest = 0;
124*4882a593Smuzhiyun 		return hdr_size;
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 	/* otherwise, just fill it in and go! */
127*4882a593Smuzhiyun 	pkt->hard.dest = daddr;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	return hdr_size;	/* success */
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
prepare_tx(struct net_device * dev,struct archdr * pkt,int length,int bufnum)132*4882a593Smuzhiyun static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
133*4882a593Smuzhiyun 		      int bufnum)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	struct arcnet_local *lp = netdev_priv(dev);
136*4882a593Smuzhiyun 	struct arc_hardware *hard = &pkt->hard;
137*4882a593Smuzhiyun 	int ofs;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* hard header is not included in packet length */
140*4882a593Smuzhiyun 	length -= ARC_HDR_SIZE;
141*4882a593Smuzhiyun 	/* And neither is the cookie field */
142*4882a593Smuzhiyun 	length -= sizeof(int);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
145*4882a593Smuzhiyun 		   lp->next_tx, lp->cur_tx, bufnum);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	arc_printk(D_PROTO, dev, "Sending for cap packet %x.\n",
148*4882a593Smuzhiyun 		   *((int *)&pkt->soft.cap.cookie[0]));
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	if (length > XMTU) {
151*4882a593Smuzhiyun 		/* should never happen! other people already check for this. */
152*4882a593Smuzhiyun 		arc_printk(D_NORMAL, dev, "Bug!  prepare_tx with size %d (> %d)\n",
153*4882a593Smuzhiyun 			   length, XMTU);
154*4882a593Smuzhiyun 		length = XMTU;
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 	if (length > MinTU) {
157*4882a593Smuzhiyun 		hard->offset[0] = 0;
158*4882a593Smuzhiyun 		hard->offset[1] = ofs = 512 - length;
159*4882a593Smuzhiyun 	} else if (length > MTU) {
160*4882a593Smuzhiyun 		hard->offset[0] = 0;
161*4882a593Smuzhiyun 		hard->offset[1] = ofs = 512 - length - 3;
162*4882a593Smuzhiyun 	} else {
163*4882a593Smuzhiyun 		hard->offset[0] = ofs = 256 - length;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	arc_printk(D_DURING, dev, "prepare_tx: length=%d ofs=%d\n",
167*4882a593Smuzhiyun 		   length, ofs);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/* Copy the arcnet-header + the protocol byte down: */
170*4882a593Smuzhiyun 	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
171*4882a593Smuzhiyun 	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
172*4882a593Smuzhiyun 			    sizeof(pkt->soft.cap.proto));
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* Skip the extra integer we have written into it as a cookie
175*4882a593Smuzhiyun 	 * but write the rest of the message:
176*4882a593Smuzhiyun 	 */
177*4882a593Smuzhiyun 	lp->hw.copy_to_card(dev, bufnum, ofs + 1,
178*4882a593Smuzhiyun 			    ((unsigned char *)&pkt->soft.cap.mes), length - 1);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	lp->lastload_dest = hard->dest;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	return 1;	/* done */
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
ack_tx(struct net_device * dev,int acked)185*4882a593Smuzhiyun static int ack_tx(struct net_device *dev, int acked)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	struct arcnet_local *lp = netdev_priv(dev);
188*4882a593Smuzhiyun 	struct sk_buff *ackskb;
189*4882a593Smuzhiyun 	struct archdr *ackpkt;
190*4882a593Smuzhiyun 	int length = sizeof(struct arc_cap);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	arc_printk(D_DURING, dev, "capmode: ack_tx: protocol: %x: result: %d\n",
193*4882a593Smuzhiyun 		   lp->outgoing.skb->protocol, acked);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	if (BUGLVL(D_SKB))
196*4882a593Smuzhiyun 		arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/* Now alloc a skb to send back up through the layers: */
199*4882a593Smuzhiyun 	ackskb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
200*4882a593Smuzhiyun 	if (!ackskb)
201*4882a593Smuzhiyun 		goto free_outskb;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	skb_put(ackskb, length + ARC_HDR_SIZE);
204*4882a593Smuzhiyun 	ackskb->dev = dev;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	skb_reset_mac_header(ackskb);
207*4882a593Smuzhiyun 	ackpkt = (struct archdr *)skb_mac_header(ackskb);
208*4882a593Smuzhiyun 	/* skb_pull(ackskb, ARC_HDR_SIZE); */
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	skb_copy_from_linear_data(lp->outgoing.skb, ackpkt,
211*4882a593Smuzhiyun 				  ARC_HDR_SIZE + sizeof(struct arc_cap));
212*4882a593Smuzhiyun 	ackpkt->soft.cap.proto = 0; /* using protocol 0 for acknowledge */
213*4882a593Smuzhiyun 	ackpkt->soft.cap.mes.ack = acked;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	arc_printk(D_PROTO, dev, "Acknowledge for cap packet %x.\n",
216*4882a593Smuzhiyun 		   *((int *)&ackpkt->soft.cap.cookie[0]));
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	ackskb->protocol = cpu_to_be16(ETH_P_ARCNET);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	if (BUGLVL(D_SKB))
221*4882a593Smuzhiyun 		arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
222*4882a593Smuzhiyun 	netif_rx(ackskb);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun free_outskb:
225*4882a593Smuzhiyun 	dev_kfree_skb_irq(lp->outgoing.skb);
226*4882a593Smuzhiyun 	lp->outgoing.proto = NULL;
227*4882a593Smuzhiyun 			/* We are always finished when in this protocol */
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	return 0;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun static struct ArcProto capmode_proto = {
233*4882a593Smuzhiyun 	.suffix		= 'r',
234*4882a593Smuzhiyun 	.mtu		= XMTU,
235*4882a593Smuzhiyun 	.rx		= rx,
236*4882a593Smuzhiyun 	.build_header	= build_header,
237*4882a593Smuzhiyun 	.prepare_tx	= prepare_tx,
238*4882a593Smuzhiyun 	.ack_tx		= ack_tx
239*4882a593Smuzhiyun };
240*4882a593Smuzhiyun 
capmode_module_init(void)241*4882a593Smuzhiyun static int __init capmode_module_init(void)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	int count;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	pr_info("cap mode (`c') encapsulation support loaded\n");
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	for (count = 1; count <= 8; count++)
248*4882a593Smuzhiyun 		if (arc_proto_map[count] == arc_proto_default)
249*4882a593Smuzhiyun 			arc_proto_map[count] = &capmode_proto;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* for cap mode, we only set the bcast proto if there's no better one */
252*4882a593Smuzhiyun 	if (arc_bcast_proto == arc_proto_default)
253*4882a593Smuzhiyun 		arc_bcast_proto = &capmode_proto;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	arc_proto_default = &capmode_proto;
256*4882a593Smuzhiyun 	arc_raw_proto = &capmode_proto;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	return 0;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
capmode_module_exit(void)261*4882a593Smuzhiyun static void __exit capmode_module_exit(void)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	arcnet_unregister_proto(&capmode_proto);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun module_init(capmode_module_init);
266*4882a593Smuzhiyun module_exit(capmode_module_exit);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun MODULE_LICENSE("GPL");
269