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