1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
30
31#include <linux/module.h>
32#include <linux/gfp.h>
33#include <linux/init.h>
34#include <linux/if_arp.h>
35#include <net/arp.h>
36#include <linux/netdevice.h>
37#include <linux/skbuff.h>
38
39#include "arcdevice.h"
40
41
42static void rx(struct net_device *dev, int bufnum,
43 struct archdr *pkthdr, int length)
44{
45 struct arcnet_local *lp = netdev_priv(dev);
46 struct sk_buff *skb;
47 struct archdr *pkt;
48 char *pktbuf, *pkthdrbuf;
49 int ofs;
50
51 arc_printk(D_DURING, dev, "it's a raw(cap) packet (length=%d)\n",
52 length);
53
54 if (length >= MinTU)
55 ofs = 512 - length;
56 else
57 ofs = 256 - length;
58
59 skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC);
60 if (!skb) {
61 dev->stats.rx_dropped++;
62 return;
63 }
64 skb_put(skb, length + ARC_HDR_SIZE + sizeof(int));
65 skb->dev = dev;
66 skb_reset_mac_header(skb);
67 pkt = (struct archdr *)skb_mac_header(skb);
68 skb_pull(skb, ARC_HDR_SIZE);
69
70
71
72
73
74
75 pktbuf = (char *)pkt;
76 pkthdrbuf = (char *)pkthdr;
77 memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto));
78 memcpy(pktbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto) + sizeof(int),
79 pkthdrbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto),
80 sizeof(struct archdr) - ARC_HDR_SIZE - sizeof(pkt->soft.cap.proto));
81
82 if (length > sizeof(pkt->soft))
83 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
84 pkt->soft.raw + sizeof(pkt->soft)
85 + sizeof(int),
86 length - sizeof(pkt->soft));
87
88 if (BUGLVL(D_SKB))
89 arcnet_dump_skb(dev, skb, "rx");
90
91 skb->protocol = cpu_to_be16(ETH_P_ARCNET);
92 netif_rx(skb);
93}
94
95
96
97
98static int build_header(struct sk_buff *skb,
99 struct net_device *dev,
100 unsigned short type,
101 uint8_t daddr)
102{
103 int hdr_size = ARC_HDR_SIZE;
104 struct archdr *pkt = skb_push(skb, hdr_size);
105
106 arc_printk(D_PROTO, dev, "Preparing header for cap packet %x.\n",
107 *((int *)&pkt->soft.cap.cookie[0]));
108
109
110
111
112
113
114
115 pkt->hard.source = *dev->dev_addr;
116
117
118
119 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
120
121
122
123 pkt->hard.dest = 0;
124 return hdr_size;
125 }
126
127 pkt->hard.dest = daddr;
128
129 return hdr_size;
130}
131
132static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
133 int bufnum)
134{
135 struct arcnet_local *lp = netdev_priv(dev);
136 struct arc_hardware *hard = &pkt->hard;
137 int ofs;
138
139
140 length -= ARC_HDR_SIZE;
141
142 length -= sizeof(int);
143
144 arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
145 lp->next_tx, lp->cur_tx, bufnum);
146
147 arc_printk(D_PROTO, dev, "Sending for cap packet %x.\n",
148 *((int *)&pkt->soft.cap.cookie[0]));
149
150 if (length > XMTU) {
151
152 arc_printk(D_NORMAL, dev, "Bug! prepare_tx with size %d (> %d)\n",
153 length, XMTU);
154 length = XMTU;
155 }
156 if (length > MinTU) {
157 hard->offset[0] = 0;
158 hard->offset[1] = ofs = 512 - length;
159 } else if (length > MTU) {
160 hard->offset[0] = 0;
161 hard->offset[1] = ofs = 512 - length - 3;
162 } else {
163 hard->offset[0] = ofs = 256 - length;
164 }
165
166 arc_printk(D_DURING, dev, "prepare_tx: length=%d ofs=%d\n",
167 length, ofs);
168
169
170 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
171 lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto,
172 sizeof(pkt->soft.cap.proto));
173
174
175
176
177 lp->hw.copy_to_card(dev, bufnum, ofs + 1,
178 ((unsigned char *)&pkt->soft.cap.mes), length - 1);
179
180 lp->lastload_dest = hard->dest;
181
182 return 1;
183}
184
185static int ack_tx(struct net_device *dev, int acked)
186{
187 struct arcnet_local *lp = netdev_priv(dev);
188 struct sk_buff *ackskb;
189 struct archdr *ackpkt;
190 int length = sizeof(struct arc_cap);
191
192 arc_printk(D_DURING, dev, "capmode: ack_tx: protocol: %x: result: %d\n",
193 lp->outgoing.skb->protocol, acked);
194
195 if (BUGLVL(D_SKB))
196 arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx");
197
198
199 ackskb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
200 if (!ackskb)
201 goto free_outskb;
202
203 skb_put(ackskb, length + ARC_HDR_SIZE);
204 ackskb->dev = dev;
205
206 skb_reset_mac_header(ackskb);
207 ackpkt = (struct archdr *)skb_mac_header(ackskb);
208
209
210 skb_copy_from_linear_data(lp->outgoing.skb, ackpkt,
211 ARC_HDR_SIZE + sizeof(struct arc_cap));
212 ackpkt->soft.cap.proto = 0;
213 ackpkt->soft.cap.mes.ack = acked;
214
215 arc_printk(D_PROTO, dev, "Acknowledge for cap packet %x.\n",
216 *((int *)&ackpkt->soft.cap.cookie[0]));
217
218 ackskb->protocol = cpu_to_be16(ETH_P_ARCNET);
219
220 if (BUGLVL(D_SKB))
221 arcnet_dump_skb(dev, ackskb, "ack_tx_recv");
222 netif_rx(ackskb);
223
224free_outskb:
225 dev_kfree_skb_irq(lp->outgoing.skb);
226 lp->outgoing.proto = NULL;
227
228
229 return 0;
230}
231
232static struct ArcProto capmode_proto = {
233 .suffix = 'r',
234 .mtu = XMTU,
235 .rx = rx,
236 .build_header = build_header,
237 .prepare_tx = prepare_tx,
238 .ack_tx = ack_tx
239};
240
241static int __init capmode_module_init(void)
242{
243 int count;
244
245 pr_info("cap mode (`c') encapsulation support loaded\n");
246
247 for (count = 1; count <= 8; count++)
248 if (arc_proto_map[count] == arc_proto_default)
249 arc_proto_map[count] = &capmode_proto;
250
251
252 if (arc_bcast_proto == arc_proto_default)
253 arc_bcast_proto = &capmode_proto;
254
255 arc_proto_default = &capmode_proto;
256 arc_raw_proto = &capmode_proto;
257
258 return 0;
259}
260
261static void __exit capmode_module_exit(void)
262{
263 arcnet_unregister_proto(&capmode_proto);
264}
265module_init(capmode_module_init);
266module_exit(capmode_module_exit);
267
268MODULE_LICENSE("GPL");
269