blob: 566da5ecdc9d6b6c26b0242543f52b1bc7be8766 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux ARCnet driver - RFC1201 (standard) packet encapsulation
Joe Perchescb334642015-05-05 10:05:47 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written 1994-1999 by Avery Pennarun.
5 * Derived from skeleton.c by Donald Becker.
6 *
7 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
8 * for sponsoring the further development of this driver.
9 *
10 * **********************
11 *
12 * The original copyright of skeleton.c was as follows:
13 *
14 * skeleton.c Written 1993 by Donald Becker.
15 * Copyright 1993 United States Government as represented by the
16 * Director, National Security Agency. This software may only be used
17 * and distributed according to the terms of the GNU General Public License as
18 * modified by SRC, incorporated herein by reference.
19 *
20 * **********************
21 *
22 * For more details, see drivers/net/arcnet.c
23 *
24 * **********************
25 */
Joe Perches05a24b22015-05-05 10:05:56 -070026
27#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
28
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/if_arp.h>
33#include <linux/netdevice.h>
34#include <linux/skbuff.h>
Joe Perches26c6d282015-05-05 10:06:03 -070035
36#include "arcdevice.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Al Viro701181a2007-08-22 22:59:11 -040040static __be16 type_trans(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static void rx(struct net_device *dev, int bufnum,
42 struct archdr *pkthdr, int length);
43static int build_header(struct sk_buff *skb, struct net_device *dev,
44 unsigned short type, uint8_t daddr);
45static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
46 int bufnum);
47static int continue_tx(struct net_device *dev, int bufnum);
48
Joe Perches7f5e7602015-05-05 10:05:49 -070049static struct ArcProto rfc1201_proto = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 .suffix = 'a',
51 .mtu = 1500, /* could be more, but some receivers can't handle it... */
52 .is_ip = 1, /* This is for sending IP and ARP packages */
53 .rx = rx,
54 .build_header = build_header,
55 .prepare_tx = prepare_tx,
56 .continue_tx = continue_tx,
57 .ack_tx = NULL
58};
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int __init arcnet_rfc1201_init(void)
61{
Joe Perches05a24b22015-05-05 10:05:56 -070062 pr_info("%s\n", "RFC1201 \"standard\" (`a') encapsulation support loaded");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 arc_proto_map[ARC_P_IP]
65 = arc_proto_map[ARC_P_IPV6]
66 = arc_proto_map[ARC_P_ARP]
67 = arc_proto_map[ARC_P_RARP]
68 = arc_proto_map[ARC_P_IPX]
69 = arc_proto_map[ARC_P_NOVELL_EC]
70 = &rfc1201_proto;
71
72 /* if someone else already owns the broadcast, we won't take it */
73 if (arc_bcast_proto == arc_proto_default)
74 arc_bcast_proto = &rfc1201_proto;
75
76 return 0;
77}
78
79static void __exit arcnet_rfc1201_exit(void)
80{
81 arcnet_unregister_proto(&rfc1201_proto);
82}
83
84module_init(arcnet_rfc1201_init);
85module_exit(arcnet_rfc1201_exit);
86
Joe Perchesf2f0a162015-05-05 10:05:52 -070087/* Determine a packet's protocol ID.
Joe Perchescb334642015-05-05 10:05:47 -070088 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * With ARCnet we have to convert everything to Ethernet-style stuff.
90 */
Al Viro701181a2007-08-22 22:59:11 -040091static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Joe Perchescb334642015-05-05 10:05:47 -070093 struct archdr *pkt = (struct archdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
96
97 /* Pull off the arcnet header. */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070098 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 skb_pull(skb, hdr_size);
100
Joe Perches7f5e7602015-05-05 10:05:49 -0700101 if (pkt->hard.dest == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 skb->pkt_type = PACKET_BROADCAST;
Joe Perches7f5e7602015-05-05 10:05:49 -0700103 } else if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* if we're not sending to ourselves :) */
105 if (pkt->hard.dest != dev->dev_addr[0])
106 skb->pkt_type = PACKET_OTHERHOST;
107 }
108 /* now return the protocol number */
109 switch (soft->proto) {
110 case ARC_P_IP:
111 return htons(ETH_P_IP);
112 case ARC_P_IPV6:
113 return htons(ETH_P_IPV6);
114 case ARC_P_ARP:
115 return htons(ETH_P_ARP);
116 case ARC_P_RARP:
117 return htons(ETH_P_RARP);
118
119 case ARC_P_IPX:
120 case ARC_P_NOVELL_EC:
121 return htons(ETH_P_802_3);
122 default:
Stephen Hemminger5803c512009-01-09 13:01:08 +0000123 dev->stats.rx_errors++;
124 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126 }
127
128 return htons(ETH_P_IP);
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* packet receiver */
132static void rx(struct net_device *dev, int bufnum,
133 struct archdr *pkthdr, int length)
134{
Wang Chen454d7c92008-11-12 23:37:49 -0800135 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct sk_buff *skb;
137 struct archdr *pkt = pkthdr;
138 struct arc_rfc1201 *soft = &pkthdr->soft.rfc1201;
139 int saddr = pkt->hard.source, ofs;
140 struct Incoming *in = &lp->rfc1201.incoming[saddr];
141
Joe Perchesa34c0932015-05-05 10:05:55 -0700142 arc_printk(D_DURING, dev, "it's an RFC1201 packet (length=%d)\n",
143 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 if (length >= MinTU)
146 ofs = 512 - length;
147 else
148 ofs = 256 - length;
149
150 if (soft->split_flag == 0xFF) { /* Exception Packet */
Joe Perches7f5e7602015-05-05 10:05:49 -0700151 if (length >= 4 + RFC1201_HDR_SIZE) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700152 arc_printk(D_DURING, dev, "compensating for exception packet\n");
Joe Perches7f5e7602015-05-05 10:05:49 -0700153 } else {
Joe Perchesa34c0932015-05-05 10:05:55 -0700154 arc_printk(D_EXTRA, dev, "short RFC1201 exception packet from %02Xh",
155 saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return;
157 }
158
159 /* skip over 4-byte junkola */
160 length -= 4;
161 ofs += 4;
162 lp->hw.copy_from_card(dev, bufnum, 512 - length,
163 soft, sizeof(pkt->soft));
164 }
165 if (!soft->split_flag) { /* not split */
Joe Perchesa34c0932015-05-05 10:05:55 -0700166 arc_printk(D_RX, dev, "incoming is not split (splitflag=%d)\n",
167 soft->split_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 if (in->skb) { /* already assembling one! */
Joe Perchesa34c0932015-05-05 10:05:55 -0700170 arc_printk(D_EXTRA, dev, "aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
171 in->sequence, soft->split_flag,
172 soft->sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 lp->rfc1201.aborted_seq = soft->sequence;
174 dev_kfree_skb_irq(in->skb);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000175 dev->stats.rx_errors++;
176 dev->stats.rx_missed_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 in->skb = NULL;
178 }
179 in->sequence = soft->sequence;
180
181 skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
Joe Perchesd8101e72015-05-05 10:06:00 -0700182 if (!skb) {
Stephen Hemminger5803c512009-01-09 13:01:08 +0000183 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return;
185 }
186 skb_put(skb, length + ARC_HDR_SIZE);
187 skb->dev = dev;
188
Joe Perchescb334642015-05-05 10:05:47 -0700189 pkt = (struct archdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 soft = &pkt->soft.rfc1201;
191
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700192 /* up to sizeof(pkt->soft) has already
193 * been copied from the card
194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 memcpy(pkt, pkthdr, sizeof(struct archdr));
196 if (length > sizeof(pkt->soft))
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700197 lp->hw.copy_from_card(dev, bufnum,
198 ofs + sizeof(pkt->soft),
199 pkt->soft.raw + sizeof(pkt->soft),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 length - sizeof(pkt->soft));
201
Joe Perchesf2f0a162015-05-05 10:05:52 -0700202 /* ARP packets have problems when sent from some DOS systems:
203 * the source address is always 0!
204 * So we take the hardware source addr (which is impossible
205 * to fumble) and insert it ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
207 if (soft->proto == ARC_P_ARP) {
Joe Perchescb334642015-05-05 10:05:47 -0700208 struct arphdr *arp = (struct arphdr *)soft->payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* make sure addresses are the right length */
211 if (arp->ar_hln == 1 && arp->ar_pln == 4) {
Joe Perchescb334642015-05-05 10:05:47 -0700212 uint8_t *cptr = (uint8_t *)arp + sizeof(struct arphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 if (!*cptr) { /* is saddr = 00? */
Joe Perchesa34c0932015-05-05 10:05:55 -0700215 arc_printk(D_EXTRA, dev,
216 "ARP source address was 00h, set to %02Xh\n",
217 saddr);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000218 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *cptr = saddr;
220 } else {
Joe Perchesa34c0932015-05-05 10:05:55 -0700221 arc_printk(D_DURING, dev, "ARP source address (%Xh) is fine.\n",
222 *cptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 } else {
Joe Perchesa34c0932015-05-05 10:05:55 -0700225 arc_printk(D_NORMAL, dev, "funny-shaped ARP packet. (%Xh, %Xh)\n",
226 arp->ar_hln, arp->ar_pln);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000227 dev->stats.rx_errors++;
228 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 }
Joe Perches72aeea42015-05-05 10:05:54 -0700231 if (BUGLVL(D_SKB))
232 arcnet_dump_skb(dev, skb, "rx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 skb->protocol = type_trans(skb, dev);
235 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 } else { /* split packet */
Joe Perchesf2f0a162015-05-05 10:05:52 -0700237 /* NOTE: MSDOS ARP packet correction should only need to
238 * apply to unsplit packets, since ARP packets are so short.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
Joe Perchesf2f0a162015-05-05 10:05:52 -0700240 * My interpretation of the RFC1201 document is that if a
241 * packet is received out of order, the entire assembly
242 * process should be aborted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *
Joe Perchesf2f0a162015-05-05 10:05:52 -0700244 * The RFC also mentions "it is possible for successfully
245 * received packets to be retransmitted." As of 0.40 all
246 * previously received packets are allowed, not just the
247 * most recent one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 *
Joe Perchesf2f0a162015-05-05 10:05:52 -0700249 * We allow multiple assembly processes, one for each
250 * ARCnet card possible on the network.
251 * Seems rather like a waste of memory, but there's no
252 * other way to be reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
254
Joe Perchesa34c0932015-05-05 10:05:55 -0700255 arc_printk(D_RX, dev, "packet is split (splitflag=%d, seq=%d)\n",
256 soft->split_flag, in->sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (in->skb && in->sequence != soft->sequence) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700259 arc_printk(D_EXTRA, dev, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
260 saddr, in->sequence, soft->sequence,
261 soft->split_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dev_kfree_skb_irq(in->skb);
263 in->skb = NULL;
Stephen Hemminger5803c512009-01-09 13:01:08 +0000264 dev->stats.rx_errors++;
265 dev->stats.rx_missed_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 in->lastpacket = in->numpackets = 0;
267 }
268 if (soft->split_flag & 1) { /* first packet in split */
Joe Perchesa34c0932015-05-05 10:05:55 -0700269 arc_printk(D_RX, dev, "brand new splitpacket (splitflag=%d)\n",
270 soft->split_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (in->skb) { /* already assembling one! */
Joe Perchesa34c0932015-05-05 10:05:55 -0700272 arc_printk(D_EXTRA, dev, "aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
273 in->sequence, soft->split_flag,
274 soft->sequence);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000275 dev->stats.rx_errors++;
276 dev->stats.rx_missed_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 dev_kfree_skb_irq(in->skb);
278 }
279 in->sequence = soft->sequence;
Joe Perchescb334642015-05-05 10:05:47 -0700280 in->numpackets = ((unsigned)soft->split_flag >> 1) + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 in->lastpacket = 1;
282
283 if (in->numpackets > 16) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700284 arc_printk(D_EXTRA, dev, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
285 soft->split_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 lp->rfc1201.aborted_seq = soft->sequence;
Stephen Hemminger5803c512009-01-09 13:01:08 +0000287 dev->stats.rx_errors++;
288 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return;
290 }
291 in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
292 GFP_ATOMIC);
Joe Perchesd8101e72015-05-05 10:06:00 -0700293 if (!skb) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700294 arc_printk(D_NORMAL, dev, "(split) memory squeeze, dropping packet.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 lp->rfc1201.aborted_seq = soft->sequence;
Stephen Hemminger5803c512009-01-09 13:01:08 +0000296 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return;
298 }
299 skb->dev = dev;
Joe Perchescb334642015-05-05 10:05:47 -0700300 pkt = (struct archdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 soft = &pkt->soft.rfc1201;
302
303 memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
304 skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
305
306 soft->split_flag = 0; /* end result won't be split */
307 } else { /* not first packet */
Joe Perchescb334642015-05-05 10:05:47 -0700308 int packetnum = ((unsigned)soft->split_flag >> 1) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Joe Perchesf2f0a162015-05-05 10:05:52 -0700310 /* if we're not assembling, there's no point trying to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * continue.
312 */
313 if (!in->skb) {
314 if (lp->rfc1201.aborted_seq != soft->sequence) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700315 arc_printk(D_EXTRA, dev, "can't continue split without starting first! (splitflag=%d, seq=%d, aborted=%d)\n",
316 soft->split_flag,
317 soft->sequence,
318 lp->rfc1201.aborted_seq);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000319 dev->stats.rx_errors++;
320 dev->stats.rx_missed_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 return;
323 }
324 in->lastpacket++;
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700325 /* if not the right flag */
326 if (packetnum != in->lastpacket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* harmless duplicate? ignore. */
328 if (packetnum <= in->lastpacket - 1) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700329 arc_printk(D_EXTRA, dev, "duplicate splitpacket ignored! (splitflag=%d)\n",
330 soft->split_flag);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000331 dev->stats.rx_errors++;
332 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return;
334 }
335 /* "bad" duplicate, kill reassembly */
Joe Perchesa34c0932015-05-05 10:05:55 -0700336 arc_printk(D_EXTRA, dev, "out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",
337 in->sequence, soft->split_flag,
338 soft->sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 lp->rfc1201.aborted_seq = soft->sequence;
340 dev_kfree_skb_irq(in->skb);
341 in->skb = NULL;
Stephen Hemminger5803c512009-01-09 13:01:08 +0000342 dev->stats.rx_errors++;
343 dev->stats.rx_missed_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 in->lastpacket = in->numpackets = 0;
345 return;
346 }
Joe Perchescb334642015-05-05 10:05:47 -0700347 pkt = (struct archdr *)in->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 soft = &pkt->soft.rfc1201;
349 }
350
351 skb = in->skb;
352
353 lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
354 skb->data + skb->len,
355 length - RFC1201_HDR_SIZE);
356 skb_put(skb, length - RFC1201_HDR_SIZE);
357
358 /* are we done? */
359 if (in->lastpacket == in->numpackets) {
360 in->skb = NULL;
361 in->lastpacket = in->numpackets = 0;
362
Joe Perchesa34c0932015-05-05 10:05:55 -0700363 arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (unsplit)\n",
364 skb->len, pkt->hard.source);
365 arc_printk(D_SKB_SIZE, dev, "skb: received %d bytes from %02X (split)\n",
366 skb->len, pkt->hard.source);
Joe Perches72aeea42015-05-05 10:05:54 -0700367 if (BUGLVL(D_SKB))
368 arcnet_dump_skb(dev, skb, "rx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 skb->protocol = type_trans(skb, dev);
371 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 }
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/* Create the ARCnet hard/soft headers for RFC1201. */
377static int build_header(struct sk_buff *skb, struct net_device *dev,
378 unsigned short type, uint8_t daddr)
379{
Wang Chen454d7c92008-11-12 23:37:49 -0800380 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
Joe Perchescb334642015-05-05 10:05:47 -0700382 struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
384
385 /* set the protocol ID according to RFC1201 */
386 switch (type) {
387 case ETH_P_IP:
388 soft->proto = ARC_P_IP;
389 break;
390 case ETH_P_IPV6:
391 soft->proto = ARC_P_IPV6;
392 break;
393 case ETH_P_ARP:
394 soft->proto = ARC_P_ARP;
395 break;
396 case ETH_P_RARP:
397 soft->proto = ARC_P_RARP;
398 break;
399 case ETH_P_IPX:
400 case ETH_P_802_3:
401 case ETH_P_802_2:
402 soft->proto = ARC_P_IPX;
403 break;
404 case ETH_P_ATALK:
405 soft->proto = ARC_P_ATALK;
406 break;
407 default:
Joe Perchesa34c0932015-05-05 10:05:55 -0700408 arc_printk(D_NORMAL, dev, "RFC1201: I don't understand protocol %d (%Xh)\n",
409 type, type);
Stephen Hemminger5803c512009-01-09 13:01:08 +0000410 dev->stats.tx_errors++;
411 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 0;
413 }
414
Joe Perchesf2f0a162015-05-05 10:05:52 -0700415 /* Set the source hardware address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *
417 * This is pretty pointless for most purposes, but it can help in
Joe Perchesf2f0a162015-05-05 10:05:52 -0700418 * debugging. ARCnet does not allow us to change the source address
419 * in the actual packet sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
421 pkt->hard.source = *dev->dev_addr;
422
423 soft->sequence = htons(lp->rfc1201.sequence++);
424 soft->split_flag = 0; /* split packets are done elsewhere */
425
426 /* see linux/net/ethernet/eth.c to see where I got the following */
427
428 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
Joe Perchesf2f0a162015-05-05 10:05:52 -0700429 /* FIXME: fill in the last byte of the dest ipaddr here
430 * to better comply with RFC1051 in "noarp" mode.
431 * For now, always broadcasting will probably at least get
432 * packets sent out :)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434 pkt->hard.dest = 0;
435 return hdr_size;
436 }
437 /* otherwise, drop in the dest address */
438 pkt->hard.dest = daddr;
439 return hdr_size;
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static void load_pkt(struct net_device *dev, struct arc_hardware *hard,
443 struct arc_rfc1201 *soft, int softlen, int bufnum)
444{
Wang Chen454d7c92008-11-12 23:37:49 -0800445 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int ofs;
447
448 /* assume length <= XMTU: someone should have handled that by now. */
449
450 if (softlen > MinTU) {
451 hard->offset[0] = 0;
452 hard->offset[1] = ofs = 512 - softlen;
453 } else if (softlen > MTU) { /* exception packet - add an extra header */
454 struct arc_rfc1201 excsoft;
455
456 excsoft.proto = soft->proto;
457 excsoft.split_flag = 0xff;
Al Viro701181a2007-08-22 22:59:11 -0400458 excsoft.sequence = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 hard->offset[0] = 0;
461 ofs = 512 - softlen;
462 hard->offset[1] = ofs - RFC1201_HDR_SIZE;
463 lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE,
464 &excsoft, RFC1201_HDR_SIZE);
Joe Perches7f5e7602015-05-05 10:05:49 -0700465 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 hard->offset[0] = ofs = 256 - softlen;
Joe Perches7f5e7602015-05-05 10:05:49 -0700467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
470 lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
471
472 lp->lastload_dest = hard->dest;
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
476 int bufnum)
477{
Wang Chen454d7c92008-11-12 23:37:49 -0800478 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
480 struct Outgoing *out;
481
Joe Perchesa34c0932015-05-05 10:05:55 -0700482 arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
483 lp->next_tx, lp->cur_tx, bufnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Joe Perchesd6d7d3e2015-05-05 10:06:02 -0700485 /* hard header is not included in packet length */
486 length -= ARC_HDR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 pkt->soft.rfc1201.split_flag = 0;
488
489 /* need to do a split packet? */
490 if (length > XMTU) {
491 out = &lp->outgoing;
492
493 out->length = length - RFC1201_HDR_SIZE;
494 out->dataleft = lp->outgoing.length;
495 out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
496 out->segnum = 0;
497
Joe Perchesa34c0932015-05-05 10:05:55 -0700498 arc_printk(D_DURING, dev, "rfc1201 prep_tx: ready for %d-segment split (%d bytes, seq=%d)\n",
499 out->numsegs, out->length,
500 pkt->soft.rfc1201.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 return 0; /* not done */
503 }
504 /* just load the packet into the buffers and send it off */
505 load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
506
507 return 1; /* done */
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static int continue_tx(struct net_device *dev, int bufnum)
511{
Wang Chen454d7c92008-11-12 23:37:49 -0800512 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct Outgoing *out = &lp->outgoing;
514 struct arc_hardware *hard = &out->pkt->hard;
515 struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
516 int maxsegsize = XMTU - RFC1201_HDR_SIZE;
517 int seglen;
518
Joe Perchesa34c0932015-05-05 10:05:55 -0700519 arc_printk(D_DURING, dev,
520 "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
521 out->segnum, out->numsegs, soft->sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* the "new" soft header comes right before the data chunk */
524 newsoft = (struct arc_rfc1201 *)
525 (out->pkt->soft.raw + out->length - out->dataleft);
526
527 if (!out->segnum) /* first packet; newsoft == soft */
528 newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
529 else {
530 newsoft->split_flag = out->segnum << 1;
531 newsoft->proto = soft->proto;
532 newsoft->sequence = soft->sequence;
533 }
534
535 seglen = maxsegsize;
536 if (seglen > out->dataleft)
537 seglen = out->dataleft;
538 out->dataleft -= seglen;
539
540 load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
541
542 out->segnum++;
543 if (out->segnum >= out->numsegs)
544 return 1;
545 else
546 return 0;
547}