18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Linux ARCnet driver - "cap mode" packet encapsulation. 38c2ecf20Sopenharmony_ci * It adds sequence numbers to packets for communicating between a user space 48c2ecf20Sopenharmony_ci * application and the driver. After a transmit it sends a packet with protocol 58c2ecf20Sopenharmony_ci * byte 0 back up to the userspace containing the sequence number of the packet 68c2ecf20Sopenharmony_ci * plus the transmit-status on the ArcNet. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Written 2002-4 by Esben Nielsen, Vestas Wind Systems A/S 98c2ecf20Sopenharmony_ci * Derived from arc-rawmode.c by Avery Pennarun. 108c2ecf20Sopenharmony_ci * arc-rawmode was in turned based on skeleton.c, see below. 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * ********************** 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * The original copyright of skeleton.c was as follows: 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * skeleton.c Written 1993 by Donald Becker. 178c2ecf20Sopenharmony_ci * Copyright 1993 United States Government as represented by the 188c2ecf20Sopenharmony_ci * Director, National Security Agency. This software may only be used 198c2ecf20Sopenharmony_ci * and distributed according to the terms of the GNU General Public License as 208c2ecf20Sopenharmony_ci * modified by SRC, incorporated herein by reference. 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * ********************** 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * For more details, see drivers/net/arcnet.c 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * ********************** 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include <linux/module.h> 328c2ecf20Sopenharmony_ci#include <linux/gfp.h> 338c2ecf20Sopenharmony_ci#include <linux/init.h> 348c2ecf20Sopenharmony_ci#include <linux/if_arp.h> 358c2ecf20Sopenharmony_ci#include <net/arp.h> 368c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 378c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#include "arcdevice.h" 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* packet receiver */ 428c2ecf20Sopenharmony_cistatic void rx(struct net_device *dev, int bufnum, 438c2ecf20Sopenharmony_ci struct archdr *pkthdr, int length) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci struct arcnet_local *lp = netdev_priv(dev); 468c2ecf20Sopenharmony_ci struct sk_buff *skb; 478c2ecf20Sopenharmony_ci struct archdr *pkt; 488c2ecf20Sopenharmony_ci char *pktbuf, *pkthdrbuf; 498c2ecf20Sopenharmony_ci int ofs; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci arc_printk(D_DURING, dev, "it's a raw(cap) packet (length=%d)\n", 528c2ecf20Sopenharmony_ci length); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci if (length >= MinTU) 558c2ecf20Sopenharmony_ci ofs = 512 - length; 568c2ecf20Sopenharmony_ci else 578c2ecf20Sopenharmony_ci ofs = 256 - length; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci skb = alloc_skb(length + ARC_HDR_SIZE + sizeof(int), GFP_ATOMIC); 608c2ecf20Sopenharmony_ci if (!skb) { 618c2ecf20Sopenharmony_ci dev->stats.rx_dropped++; 628c2ecf20Sopenharmony_ci return; 638c2ecf20Sopenharmony_ci } 648c2ecf20Sopenharmony_ci skb_put(skb, length + ARC_HDR_SIZE + sizeof(int)); 658c2ecf20Sopenharmony_ci skb->dev = dev; 668c2ecf20Sopenharmony_ci skb_reset_mac_header(skb); 678c2ecf20Sopenharmony_ci pkt = (struct archdr *)skb_mac_header(skb); 688c2ecf20Sopenharmony_ci skb_pull(skb, ARC_HDR_SIZE); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci /* up to sizeof(pkt->soft) has already been copied from the card 718c2ecf20Sopenharmony_ci * squeeze in an int for the cap encapsulation 728c2ecf20Sopenharmony_ci * use these variables to be sure we count in bytes, not in 738c2ecf20Sopenharmony_ci * sizeof(struct archdr) 748c2ecf20Sopenharmony_ci */ 758c2ecf20Sopenharmony_ci pktbuf = (char *)pkt; 768c2ecf20Sopenharmony_ci pkthdrbuf = (char *)pkthdr; 778c2ecf20Sopenharmony_ci memcpy(pktbuf, pkthdrbuf, ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto)); 788c2ecf20Sopenharmony_ci memcpy(pktbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto) + sizeof(int), 798c2ecf20Sopenharmony_ci pkthdrbuf + ARC_HDR_SIZE + sizeof(pkt->soft.cap.proto), 808c2ecf20Sopenharmony_ci sizeof(struct archdr) - ARC_HDR_SIZE - sizeof(pkt->soft.cap.proto)); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci if (length > sizeof(pkt->soft)) 838c2ecf20Sopenharmony_ci lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft), 848c2ecf20Sopenharmony_ci pkt->soft.raw + sizeof(pkt->soft) 858c2ecf20Sopenharmony_ci + sizeof(int), 868c2ecf20Sopenharmony_ci length - sizeof(pkt->soft)); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci if (BUGLVL(D_SKB)) 898c2ecf20Sopenharmony_ci arcnet_dump_skb(dev, skb, "rx"); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci skb->protocol = cpu_to_be16(ETH_P_ARCNET); 928c2ecf20Sopenharmony_ci netif_rx(skb); 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci/* Create the ARCnet hard/soft headers for cap mode. 968c2ecf20Sopenharmony_ci * There aren't any soft headers in cap mode - not even the protocol id. 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_cistatic int build_header(struct sk_buff *skb, 998c2ecf20Sopenharmony_ci struct net_device *dev, 1008c2ecf20Sopenharmony_ci unsigned short type, 1018c2ecf20Sopenharmony_ci uint8_t daddr) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci int hdr_size = ARC_HDR_SIZE; 1048c2ecf20Sopenharmony_ci struct archdr *pkt = skb_push(skb, hdr_size); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci arc_printk(D_PROTO, dev, "Preparing header for cap packet %x.\n", 1078c2ecf20Sopenharmony_ci *((int *)&pkt->soft.cap.cookie[0])); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci /* Set the source hardware address. 1108c2ecf20Sopenharmony_ci * 1118c2ecf20Sopenharmony_ci * This is pretty pointless for most purposes, but it can help in 1128c2ecf20Sopenharmony_ci * debugging. ARCnet does not allow us to change the source address in 1138c2ecf20Sopenharmony_ci * the actual packet sent) 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ci pkt->hard.source = *dev->dev_addr; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci /* see linux/net/ethernet/eth.c to see where I got the following */ 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) { 1208c2ecf20Sopenharmony_ci /* FIXME: fill in the last byte of the dest ipaddr here to 1218c2ecf20Sopenharmony_ci * better comply with RFC1051 in "noarp" mode. 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_ci pkt->hard.dest = 0; 1248c2ecf20Sopenharmony_ci return hdr_size; 1258c2ecf20Sopenharmony_ci } 1268c2ecf20Sopenharmony_ci /* otherwise, just fill it in and go! */ 1278c2ecf20Sopenharmony_ci pkt->hard.dest = daddr; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci return hdr_size; /* success */ 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic int prepare_tx(struct net_device *dev, struct archdr *pkt, int length, 1338c2ecf20Sopenharmony_ci int bufnum) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci struct arcnet_local *lp = netdev_priv(dev); 1368c2ecf20Sopenharmony_ci struct arc_hardware *hard = &pkt->hard; 1378c2ecf20Sopenharmony_ci int ofs; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci /* hard header is not included in packet length */ 1408c2ecf20Sopenharmony_ci length -= ARC_HDR_SIZE; 1418c2ecf20Sopenharmony_ci /* And neither is the cookie field */ 1428c2ecf20Sopenharmony_ci length -= sizeof(int); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n", 1458c2ecf20Sopenharmony_ci lp->next_tx, lp->cur_tx, bufnum); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci arc_printk(D_PROTO, dev, "Sending for cap packet %x.\n", 1488c2ecf20Sopenharmony_ci *((int *)&pkt->soft.cap.cookie[0])); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci if (length > XMTU) { 1518c2ecf20Sopenharmony_ci /* should never happen! other people already check for this. */ 1528c2ecf20Sopenharmony_ci arc_printk(D_NORMAL, dev, "Bug! prepare_tx with size %d (> %d)\n", 1538c2ecf20Sopenharmony_ci length, XMTU); 1548c2ecf20Sopenharmony_ci length = XMTU; 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci if (length > MinTU) { 1578c2ecf20Sopenharmony_ci hard->offset[0] = 0; 1588c2ecf20Sopenharmony_ci hard->offset[1] = ofs = 512 - length; 1598c2ecf20Sopenharmony_ci } else if (length > MTU) { 1608c2ecf20Sopenharmony_ci hard->offset[0] = 0; 1618c2ecf20Sopenharmony_ci hard->offset[1] = ofs = 512 - length - 3; 1628c2ecf20Sopenharmony_ci } else { 1638c2ecf20Sopenharmony_ci hard->offset[0] = ofs = 256 - length; 1648c2ecf20Sopenharmony_ci } 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci arc_printk(D_DURING, dev, "prepare_tx: length=%d ofs=%d\n", 1678c2ecf20Sopenharmony_ci length, ofs); 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci /* Copy the arcnet-header + the protocol byte down: */ 1708c2ecf20Sopenharmony_ci lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE); 1718c2ecf20Sopenharmony_ci lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft.cap.proto, 1728c2ecf20Sopenharmony_ci sizeof(pkt->soft.cap.proto)); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci /* Skip the extra integer we have written into it as a cookie 1758c2ecf20Sopenharmony_ci * but write the rest of the message: 1768c2ecf20Sopenharmony_ci */ 1778c2ecf20Sopenharmony_ci lp->hw.copy_to_card(dev, bufnum, ofs + 1, 1788c2ecf20Sopenharmony_ci ((unsigned char *)&pkt->soft.cap.mes), length - 1); 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci lp->lastload_dest = hard->dest; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci return 1; /* done */ 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_cistatic int ack_tx(struct net_device *dev, int acked) 1868c2ecf20Sopenharmony_ci{ 1878c2ecf20Sopenharmony_ci struct arcnet_local *lp = netdev_priv(dev); 1888c2ecf20Sopenharmony_ci struct sk_buff *ackskb; 1898c2ecf20Sopenharmony_ci struct archdr *ackpkt; 1908c2ecf20Sopenharmony_ci int length = sizeof(struct arc_cap); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci arc_printk(D_DURING, dev, "capmode: ack_tx: protocol: %x: result: %d\n", 1938c2ecf20Sopenharmony_ci lp->outgoing.skb->protocol, acked); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci if (BUGLVL(D_SKB)) 1968c2ecf20Sopenharmony_ci arcnet_dump_skb(dev, lp->outgoing.skb, "ack_tx"); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci /* Now alloc a skb to send back up through the layers: */ 1998c2ecf20Sopenharmony_ci ackskb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC); 2008c2ecf20Sopenharmony_ci if (!ackskb) 2018c2ecf20Sopenharmony_ci goto free_outskb; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci skb_put(ackskb, length + ARC_HDR_SIZE); 2048c2ecf20Sopenharmony_ci ackskb->dev = dev; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci skb_reset_mac_header(ackskb); 2078c2ecf20Sopenharmony_ci ackpkt = (struct archdr *)skb_mac_header(ackskb); 2088c2ecf20Sopenharmony_ci /* skb_pull(ackskb, ARC_HDR_SIZE); */ 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci skb_copy_from_linear_data(lp->outgoing.skb, ackpkt, 2118c2ecf20Sopenharmony_ci ARC_HDR_SIZE + sizeof(struct arc_cap)); 2128c2ecf20Sopenharmony_ci ackpkt->soft.cap.proto = 0; /* using protocol 0 for acknowledge */ 2138c2ecf20Sopenharmony_ci ackpkt->soft.cap.mes.ack = acked; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci arc_printk(D_PROTO, dev, "Acknowledge for cap packet %x.\n", 2168c2ecf20Sopenharmony_ci *((int *)&ackpkt->soft.cap.cookie[0])); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci ackskb->protocol = cpu_to_be16(ETH_P_ARCNET); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci if (BUGLVL(D_SKB)) 2218c2ecf20Sopenharmony_ci arcnet_dump_skb(dev, ackskb, "ack_tx_recv"); 2228c2ecf20Sopenharmony_ci netif_rx(ackskb); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_cifree_outskb: 2258c2ecf20Sopenharmony_ci dev_kfree_skb_irq(lp->outgoing.skb); 2268c2ecf20Sopenharmony_ci lp->outgoing.proto = NULL; 2278c2ecf20Sopenharmony_ci /* We are always finished when in this protocol */ 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci return 0; 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cistatic struct ArcProto capmode_proto = { 2338c2ecf20Sopenharmony_ci .suffix = 'r', 2348c2ecf20Sopenharmony_ci .mtu = XMTU, 2358c2ecf20Sopenharmony_ci .rx = rx, 2368c2ecf20Sopenharmony_ci .build_header = build_header, 2378c2ecf20Sopenharmony_ci .prepare_tx = prepare_tx, 2388c2ecf20Sopenharmony_ci .ack_tx = ack_tx 2398c2ecf20Sopenharmony_ci}; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistatic int __init capmode_module_init(void) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci int count; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci pr_info("cap mode (`c') encapsulation support loaded\n"); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci for (count = 1; count <= 8; count++) 2488c2ecf20Sopenharmony_ci if (arc_proto_map[count] == arc_proto_default) 2498c2ecf20Sopenharmony_ci arc_proto_map[count] = &capmode_proto; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci /* for cap mode, we only set the bcast proto if there's no better one */ 2528c2ecf20Sopenharmony_ci if (arc_bcast_proto == arc_proto_default) 2538c2ecf20Sopenharmony_ci arc_bcast_proto = &capmode_proto; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci arc_proto_default = &capmode_proto; 2568c2ecf20Sopenharmony_ci arc_raw_proto = &capmode_proto; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci return 0; 2598c2ecf20Sopenharmony_ci} 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistatic void __exit capmode_module_exit(void) 2628c2ecf20Sopenharmony_ci{ 2638c2ecf20Sopenharmony_ci arcnet_unregister_proto(&capmode_proto); 2648c2ecf20Sopenharmony_ci} 2658c2ecf20Sopenharmony_cimodule_init(capmode_module_init); 2668c2ecf20Sopenharmony_cimodule_exit(capmode_module_exit); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 269