Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Linux ARCnet driver - "raw mode" packet encapsulation (no soft headers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Written 1994-1999 by Avery Pennarun.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Derived from skeleton.c by Donald Becker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  for sponsoring the further development of this driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * **********************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * The original copyright of skeleton.c was as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * skeleton.c Written 1993 by Donald Becker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * Copyright 1993 United States Government as represented by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * Director, National Security Agency.  This software may only be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * and distributed according to the terms of the GNU General Public License as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * modified by SRC, incorporated herein by reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * **********************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * For more details, see drivers/net/arcnet.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * **********************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <net/arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include "arcdevice.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /* packet receiver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static void rx(struct net_device *dev, int bufnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	       struct archdr *pkthdr, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct arcnet_local *lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct archdr *pkt = pkthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	arc_printk(D_DURING, dev, "it's a raw packet (length=%d)\n", length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (length > MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		ofs = 512 - length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		ofs = 256 - length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	skb_put(skb, length + ARC_HDR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	pkt = (struct archdr *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	skb_pull(skb, ARC_HDR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	/* up to sizeof(pkt->soft) has already been copied from the card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	memcpy(pkt, pkthdr, sizeof(struct archdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (length > sizeof(pkt->soft))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				      pkt->soft.raw + sizeof(pkt->soft),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 				      length - sizeof(pkt->soft));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (BUGLVL(D_SKB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		arcnet_dump_skb(dev, skb, "rx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	skb->protocol = cpu_to_be16(ETH_P_ARCNET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) /* Create the ARCnet hard/soft headers for raw mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * There aren't any soft headers in raw mode - not even the protocol id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static int build_header(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			unsigned short type, uint8_t daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	int hdr_size = ARC_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct archdr *pkt = skb_push(skb, hdr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	/* Set the source hardware address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 * This is pretty pointless for most purposes, but it can help in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 * debugging.  ARCnet does not allow us to change the source address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * in the actual packet sent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	pkt->hard.source = *dev->dev_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	/* see linux/net/ethernet/eth.c to see where I got the following */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		/* FIXME: fill in the last byte of the dest ipaddr here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		 * to better comply with RFC1051 in "noarp" mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		pkt->hard.dest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return hdr_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	/* otherwise, just fill it in and go! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	pkt->hard.dest = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return hdr_size;	/* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		      int bufnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct arcnet_local *lp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct arc_hardware *hard = &pkt->hard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	arc_printk(D_DURING, dev, "prepare_tx: txbufs=%d/%d/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		   lp->next_tx, lp->cur_tx, bufnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	/* hard header is not included in packet length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	length -= ARC_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (length > XMTU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		/* should never happen! other people already check for this. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		arc_printk(D_NORMAL, dev, "Bug!  prepare_tx with size %d (> %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			   length, XMTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		length = XMTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (length >= MinTU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		hard->offset[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		hard->offset[1] = ofs = 512 - length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	} else if (length > MTU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		hard->offset[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		hard->offset[1] = ofs = 512 - length - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		hard->offset[0] = ofs = 256 - length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	arc_printk(D_DURING, dev, "prepare_tx: length=%d ofs=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		   length, ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	lp->lastload_dest = hard->dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return 1;		/* done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static struct ArcProto rawmode_proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	.suffix		= 'r',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	.mtu		= XMTU,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	.rx		= rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	.build_header	= build_header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	.prepare_tx	= prepare_tx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	.continue_tx    = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	.ack_tx         = NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int __init arcnet_raw_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	pr_info("raw mode (`r') encapsulation support loaded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	for (count = 0; count < 256; count++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		if (arc_proto_map[count] == arc_proto_default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			arc_proto_map[count] = &rawmode_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	/* for raw mode, we only set the bcast proto if there's no better one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (arc_bcast_proto == arc_proto_default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		arc_bcast_proto = &rawmode_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	arc_proto_default = &rawmode_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static void __exit arcnet_raw_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	arcnet_unregister_proto(&rawmode_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) module_init(arcnet_raw_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) module_exit(arcnet_raw_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) MODULE_LICENSE("GPL");