18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
38c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
48c2ecf20Sopenharmony_ci * for more details.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * (C) Copyright 2020 Hewlett Packard Enterprise Development LP
78c2ecf20Sopenharmony_ci * Copyright (C) 1999-2009 Silicon Graphics, Inc. All rights reserved.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/*
118c2ecf20Sopenharmony_ci * Cross Partition Network Interface (XPNET) support
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *	XPNET provides a virtual network layered on top of the Cross
148c2ecf20Sopenharmony_ci *	Partition communication layer.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *	XPNET provides direct point-to-point and broadcast-like support
178c2ecf20Sopenharmony_ci *	for an ethernet-like device.  The ethernet broadcast medium is
188c2ecf20Sopenharmony_ci *	replaced with a point-to-point message structure which passes
198c2ecf20Sopenharmony_ci *	pointers to a DMA-capable block that a remote partition should
208c2ecf20Sopenharmony_ci *	retrieve and pass to the upper level networking layer.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci#include <linux/module.h>
268c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
278c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
288c2ecf20Sopenharmony_ci#include "xp.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * The message payload transferred by XPC.
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * buf_pa is the physical address where the DMA should pull from.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * NOTE: for performance reasons, buf_pa should _ALWAYS_ begin on a
368c2ecf20Sopenharmony_ci * cacheline boundary.  To accomplish this, we record the number of
378c2ecf20Sopenharmony_ci * bytes from the beginning of the first cacheline to the first useful
388c2ecf20Sopenharmony_ci * byte of the skb (leadin_ignore) and the number of bytes from the
398c2ecf20Sopenharmony_ci * last useful byte of the skb to the end of the last cacheline
408c2ecf20Sopenharmony_ci * (tailout_ignore).
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * size is the number of bytes to transfer which includes the skb->len
438c2ecf20Sopenharmony_ci * (useful bytes of the senders skb) plus the leadin and tailout
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_cistruct xpnet_message {
468c2ecf20Sopenharmony_ci	u16 version;		/* Version for this message */
478c2ecf20Sopenharmony_ci	u16 embedded_bytes;	/* #of bytes embedded in XPC message */
488c2ecf20Sopenharmony_ci	u32 magic;		/* Special number indicating this is xpnet */
498c2ecf20Sopenharmony_ci	unsigned long buf_pa;	/* phys address of buffer to retrieve */
508c2ecf20Sopenharmony_ci	u32 size;		/* #of bytes in buffer */
518c2ecf20Sopenharmony_ci	u8 leadin_ignore;	/* #of bytes to ignore at the beginning */
528c2ecf20Sopenharmony_ci	u8 tailout_ignore;	/* #of bytes to ignore at the end */
538c2ecf20Sopenharmony_ci	unsigned char data;	/* body of small packets */
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/*
578c2ecf20Sopenharmony_ci * Determine the size of our message, the cacheline aligned size,
588c2ecf20Sopenharmony_ci * and then the number of message will request from XPC.
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * XPC expects each message to exist in an individual cacheline.
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_ci#define XPNET_MSG_SIZE		XPC_MSG_PAYLOAD_MAX_SIZE
638c2ecf20Sopenharmony_ci#define XPNET_MSG_DATA_MAX	\
648c2ecf20Sopenharmony_ci		(XPNET_MSG_SIZE - offsetof(struct xpnet_message, data))
658c2ecf20Sopenharmony_ci#define XPNET_MSG_NENTRIES	(PAGE_SIZE / XPC_MSG_MAX_SIZE)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define XPNET_MAX_KTHREADS	(XPNET_MSG_NENTRIES + 1)
688c2ecf20Sopenharmony_ci#define XPNET_MAX_IDLE_KTHREADS	(XPNET_MSG_NENTRIES + 1)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/*
718c2ecf20Sopenharmony_ci * Version number of XPNET implementation. XPNET can always talk to versions
728c2ecf20Sopenharmony_ci * with same major #, and never talk to versions with a different version.
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_ci#define _XPNET_VERSION(_major, _minor)	(((_major) << 4) | (_minor))
758c2ecf20Sopenharmony_ci#define XPNET_VERSION_MAJOR(_v)		((_v) >> 4)
768c2ecf20Sopenharmony_ci#define XPNET_VERSION_MINOR(_v)		((_v) & 0xf)
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#define	XPNET_VERSION _XPNET_VERSION(1, 0)	/* version 1.0 */
798c2ecf20Sopenharmony_ci#define	XPNET_VERSION_EMBED _XPNET_VERSION(1, 1)	/* version 1.1 */
808c2ecf20Sopenharmony_ci#define XPNET_MAGIC	0x88786984	/* "XNET" */
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define XPNET_VALID_MSG(_m)						     \
838c2ecf20Sopenharmony_ci   ((XPNET_VERSION_MAJOR(_m->version) == XPNET_VERSION_MAJOR(XPNET_VERSION)) \
848c2ecf20Sopenharmony_ci    && (msg->magic == XPNET_MAGIC))
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define XPNET_DEVICE_NAME		"xp0"
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/*
898c2ecf20Sopenharmony_ci * When messages are queued with xpc_send_notify, a kmalloc'd buffer
908c2ecf20Sopenharmony_ci * of the following type is passed as a notification cookie.  When the
918c2ecf20Sopenharmony_ci * notification function is called, we use the cookie to decide
928c2ecf20Sopenharmony_ci * whether all outstanding message sends have completed.  The skb can
938c2ecf20Sopenharmony_ci * then be released.
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_cistruct xpnet_pending_msg {
968c2ecf20Sopenharmony_ci	struct sk_buff *skb;
978c2ecf20Sopenharmony_ci	atomic_t use_count;
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic struct net_device *xpnet_device;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/*
1038c2ecf20Sopenharmony_ci * When we are notified of other partitions activating, we add them to
1048c2ecf20Sopenharmony_ci * our bitmask of partitions to which we broadcast.
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_cistatic unsigned long *xpnet_broadcast_partitions;
1078c2ecf20Sopenharmony_ci/* protect above */
1088c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(xpnet_broadcast_lock);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/*
1118c2ecf20Sopenharmony_ci * Since the Block Transfer Engine (BTE) is being used for the transfer
1128c2ecf20Sopenharmony_ci * and it relies upon cache-line size transfers, we need to reserve at
1138c2ecf20Sopenharmony_ci * least one cache-line for head and tail alignment.  The BTE is
1148c2ecf20Sopenharmony_ci * limited to 8MB transfers.
1158c2ecf20Sopenharmony_ci *
1168c2ecf20Sopenharmony_ci * Testing has shown that changing MTU to greater than 64KB has no effect
1178c2ecf20Sopenharmony_ci * on TCP as the two sides negotiate a Max Segment Size that is limited
1188c2ecf20Sopenharmony_ci * to 64K.  Other protocols May use packets greater than this, but for
1198c2ecf20Sopenharmony_ci * now, the default is 64KB.
1208c2ecf20Sopenharmony_ci */
1218c2ecf20Sopenharmony_ci#define XPNET_MAX_MTU (0x800000UL - L1_CACHE_BYTES)
1228c2ecf20Sopenharmony_ci/* 68 comes from min TCP+IP+MAC header */
1238c2ecf20Sopenharmony_ci#define XPNET_MIN_MTU 68
1248c2ecf20Sopenharmony_ci/* 32KB has been determined to be the ideal */
1258c2ecf20Sopenharmony_ci#define XPNET_DEF_MTU (0x8000UL)
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * The partid is encapsulated in the MAC address beginning in the following
1298c2ecf20Sopenharmony_ci * octet and it consists of two octets.
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_ci#define XPNET_PARTID_OCTET	2
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/* Define the XPNET debug device structures to be used with dev_dbg() et al */
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic struct device_driver xpnet_dbg_name = {
1368c2ecf20Sopenharmony_ci	.name = "xpnet"
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic struct device xpnet_dbg_subname = {
1408c2ecf20Sopenharmony_ci	.init_name = "",	/* set to "" */
1418c2ecf20Sopenharmony_ci	.driver = &xpnet_dbg_name
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic struct device *xpnet = &xpnet_dbg_subname;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/*
1478c2ecf20Sopenharmony_ci * Packet was recevied by XPC and forwarded to us.
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_cistatic void
1508c2ecf20Sopenharmony_cixpnet_receive(short partid, int channel, struct xpnet_message *msg)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1538c2ecf20Sopenharmony_ci	void *dst;
1548c2ecf20Sopenharmony_ci	enum xp_retval ret;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (!XPNET_VALID_MSG(msg)) {
1578c2ecf20Sopenharmony_ci		/*
1588c2ecf20Sopenharmony_ci		 * Packet with a different XPC version.  Ignore.
1598c2ecf20Sopenharmony_ci		 */
1608c2ecf20Sopenharmony_ci		xpc_received(partid, channel, (void *)msg);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		xpnet_device->stats.rx_errors++;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		return;
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci	dev_dbg(xpnet, "received 0x%lx, %d, %d, %d\n", msg->buf_pa, msg->size,
1678c2ecf20Sopenharmony_ci		msg->leadin_ignore, msg->tailout_ignore);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* reserve an extra cache line */
1708c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(msg->size + L1_CACHE_BYTES);
1718c2ecf20Sopenharmony_ci	if (!skb) {
1728c2ecf20Sopenharmony_ci		dev_err(xpnet, "failed on dev_alloc_skb(%d)\n",
1738c2ecf20Sopenharmony_ci			msg->size + L1_CACHE_BYTES);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		xpc_received(partid, channel, (void *)msg);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci		xpnet_device->stats.rx_errors++;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci		return;
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/*
1838c2ecf20Sopenharmony_ci	 * The allocated skb has some reserved space.
1848c2ecf20Sopenharmony_ci	 * In order to use xp_remote_memcpy(), we need to get the
1858c2ecf20Sopenharmony_ci	 * skb->data pointer moved forward.
1868c2ecf20Sopenharmony_ci	 */
1878c2ecf20Sopenharmony_ci	skb_reserve(skb, (L1_CACHE_BYTES - ((u64)skb->data &
1888c2ecf20Sopenharmony_ci					    (L1_CACHE_BYTES - 1)) +
1898c2ecf20Sopenharmony_ci			  msg->leadin_ignore));
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/*
1928c2ecf20Sopenharmony_ci	 * Update the tail pointer to indicate data actually
1938c2ecf20Sopenharmony_ci	 * transferred.
1948c2ecf20Sopenharmony_ci	 */
1958c2ecf20Sopenharmony_ci	skb_put(skb, (msg->size - msg->leadin_ignore - msg->tailout_ignore));
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/*
1988c2ecf20Sopenharmony_ci	 * Move the data over from the other side.
1998c2ecf20Sopenharmony_ci	 */
2008c2ecf20Sopenharmony_ci	if ((XPNET_VERSION_MINOR(msg->version) == 1) &&
2018c2ecf20Sopenharmony_ci	    (msg->embedded_bytes != 0)) {
2028c2ecf20Sopenharmony_ci		dev_dbg(xpnet, "copying embedded message. memcpy(0x%p, 0x%p, "
2038c2ecf20Sopenharmony_ci			"%lu)\n", skb->data, &msg->data,
2048c2ecf20Sopenharmony_ci			(size_t)msg->embedded_bytes);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci		skb_copy_to_linear_data(skb, &msg->data,
2078c2ecf20Sopenharmony_ci					(size_t)msg->embedded_bytes);
2088c2ecf20Sopenharmony_ci	} else {
2098c2ecf20Sopenharmony_ci		dst = (void *)((u64)skb->data & ~(L1_CACHE_BYTES - 1));
2108c2ecf20Sopenharmony_ci		dev_dbg(xpnet, "transferring buffer to the skb->data area;\n\t"
2118c2ecf20Sopenharmony_ci			"xp_remote_memcpy(0x%p, 0x%p, %hu)\n", dst,
2128c2ecf20Sopenharmony_ci					  (void *)msg->buf_pa, msg->size);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci		ret = xp_remote_memcpy(xp_pa(dst), msg->buf_pa, msg->size);
2158c2ecf20Sopenharmony_ci		if (ret != xpSuccess) {
2168c2ecf20Sopenharmony_ci			/*
2178c2ecf20Sopenharmony_ci			 * !!! Need better way of cleaning skb.  Currently skb
2188c2ecf20Sopenharmony_ci			 * !!! appears in_use and we can't just call
2198c2ecf20Sopenharmony_ci			 * !!! dev_kfree_skb.
2208c2ecf20Sopenharmony_ci			 */
2218c2ecf20Sopenharmony_ci			dev_err(xpnet, "xp_remote_memcpy(0x%p, 0x%p, 0x%hx) "
2228c2ecf20Sopenharmony_ci				"returned error=0x%x\n", dst,
2238c2ecf20Sopenharmony_ci				(void *)msg->buf_pa, msg->size, ret);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci			xpc_received(partid, channel, (void *)msg);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci			xpnet_device->stats.rx_errors++;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci			return;
2308c2ecf20Sopenharmony_ci		}
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	dev_dbg(xpnet, "<skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
2348c2ecf20Sopenharmony_ci		"skb->end=0x%p skb->len=%d\n", (void *)skb->head,
2358c2ecf20Sopenharmony_ci		(void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
2368c2ecf20Sopenharmony_ci		skb->len);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	skb->protocol = eth_type_trans(skb, xpnet_device);
2398c2ecf20Sopenharmony_ci	skb->ip_summed = CHECKSUM_UNNECESSARY;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	dev_dbg(xpnet, "passing skb to network layer\n"
2428c2ecf20Sopenharmony_ci		"\tskb->head=0x%p skb->data=0x%p skb->tail=0x%p "
2438c2ecf20Sopenharmony_ci		"skb->end=0x%p skb->len=%d\n",
2448c2ecf20Sopenharmony_ci		(void *)skb->head, (void *)skb->data, skb_tail_pointer(skb),
2458c2ecf20Sopenharmony_ci		skb_end_pointer(skb), skb->len);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	xpnet_device->stats.rx_packets++;
2488c2ecf20Sopenharmony_ci	xpnet_device->stats.rx_bytes += skb->len + ETH_HLEN;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	netif_rx_ni(skb);
2518c2ecf20Sopenharmony_ci	xpc_received(partid, channel, (void *)msg);
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/*
2558c2ecf20Sopenharmony_ci * This is the handler which XPC calls during any sort of change in
2568c2ecf20Sopenharmony_ci * state or message reception on a connection.
2578c2ecf20Sopenharmony_ci */
2588c2ecf20Sopenharmony_cistatic void
2598c2ecf20Sopenharmony_cixpnet_connection_activity(enum xp_retval reason, short partid, int channel,
2608c2ecf20Sopenharmony_ci			  void *data, void *key)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
2638c2ecf20Sopenharmony_ci	DBUG_ON(channel != XPC_NET_CHANNEL);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	switch (reason) {
2668c2ecf20Sopenharmony_ci	case xpMsgReceived:	/* message received */
2678c2ecf20Sopenharmony_ci		DBUG_ON(data == NULL);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci		xpnet_receive(partid, channel, (struct xpnet_message *)data);
2708c2ecf20Sopenharmony_ci		break;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	case xpConnected:	/* connection completed to a partition */
2738c2ecf20Sopenharmony_ci		spin_lock_bh(&xpnet_broadcast_lock);
2748c2ecf20Sopenharmony_ci		__set_bit(partid, xpnet_broadcast_partitions);
2758c2ecf20Sopenharmony_ci		spin_unlock_bh(&xpnet_broadcast_lock);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci		netif_carrier_on(xpnet_device);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci		dev_dbg(xpnet, "%s connected to partition %d\n",
2808c2ecf20Sopenharmony_ci			xpnet_device->name, partid);
2818c2ecf20Sopenharmony_ci		break;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	default:
2848c2ecf20Sopenharmony_ci		spin_lock_bh(&xpnet_broadcast_lock);
2858c2ecf20Sopenharmony_ci		__clear_bit(partid, xpnet_broadcast_partitions);
2868c2ecf20Sopenharmony_ci		spin_unlock_bh(&xpnet_broadcast_lock);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		if (bitmap_empty((unsigned long *)xpnet_broadcast_partitions,
2898c2ecf20Sopenharmony_ci				 xp_max_npartitions)) {
2908c2ecf20Sopenharmony_ci			netif_carrier_off(xpnet_device);
2918c2ecf20Sopenharmony_ci		}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci		dev_dbg(xpnet, "%s disconnected from partition %d\n",
2948c2ecf20Sopenharmony_ci			xpnet_device->name, partid);
2958c2ecf20Sopenharmony_ci		break;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic int
3008c2ecf20Sopenharmony_cixpnet_dev_open(struct net_device *dev)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	enum xp_retval ret;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	dev_dbg(xpnet, "calling xpc_connect(%d, 0x%p, NULL, %ld, %ld, %ld, "
3058c2ecf20Sopenharmony_ci		"%ld)\n", XPC_NET_CHANNEL, xpnet_connection_activity,
3068c2ecf20Sopenharmony_ci		(unsigned long)XPNET_MSG_SIZE,
3078c2ecf20Sopenharmony_ci		(unsigned long)XPNET_MSG_NENTRIES,
3088c2ecf20Sopenharmony_ci		(unsigned long)XPNET_MAX_KTHREADS,
3098c2ecf20Sopenharmony_ci		(unsigned long)XPNET_MAX_IDLE_KTHREADS);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	ret = xpc_connect(XPC_NET_CHANNEL, xpnet_connection_activity, NULL,
3128c2ecf20Sopenharmony_ci			  XPNET_MSG_SIZE, XPNET_MSG_NENTRIES,
3138c2ecf20Sopenharmony_ci			  XPNET_MAX_KTHREADS, XPNET_MAX_IDLE_KTHREADS);
3148c2ecf20Sopenharmony_ci	if (ret != xpSuccess) {
3158c2ecf20Sopenharmony_ci		dev_err(xpnet, "ifconfig up of %s failed on XPC connect, "
3168c2ecf20Sopenharmony_ci			"ret=%d\n", dev->name, ret);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci		return -ENOMEM;
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	dev_dbg(xpnet, "ifconfig up of %s; XPC connected\n", dev->name);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	return 0;
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic int
3278c2ecf20Sopenharmony_cixpnet_dev_stop(struct net_device *dev)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	xpc_disconnect(XPC_NET_CHANNEL);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	dev_dbg(xpnet, "ifconfig down of %s; XPC disconnected\n", dev->name);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	return 0;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci/*
3378c2ecf20Sopenharmony_ci * Notification that the other end has received the message and
3388c2ecf20Sopenharmony_ci * DMA'd the skb information.  At this point, they are done with
3398c2ecf20Sopenharmony_ci * our side.  When all recipients are done processing, we
3408c2ecf20Sopenharmony_ci * release the skb and then release our pending message structure.
3418c2ecf20Sopenharmony_ci */
3428c2ecf20Sopenharmony_cistatic void
3438c2ecf20Sopenharmony_cixpnet_send_completed(enum xp_retval reason, short partid, int channel,
3448c2ecf20Sopenharmony_ci		     void *__qm)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	struct xpnet_pending_msg *queued_msg = (struct xpnet_pending_msg *)__qm;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	DBUG_ON(queued_msg == NULL);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	dev_dbg(xpnet, "message to %d notified with reason %d\n",
3518c2ecf20Sopenharmony_ci		partid, reason);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	if (atomic_dec_return(&queued_msg->use_count) == 0) {
3548c2ecf20Sopenharmony_ci		dev_dbg(xpnet, "all acks for skb->head=-x%p\n",
3558c2ecf20Sopenharmony_ci			(void *)queued_msg->skb->head);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci		dev_kfree_skb_any(queued_msg->skb);
3588c2ecf20Sopenharmony_ci		kfree(queued_msg);
3598c2ecf20Sopenharmony_ci	}
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic void
3638c2ecf20Sopenharmony_cixpnet_send(struct sk_buff *skb, struct xpnet_pending_msg *queued_msg,
3648c2ecf20Sopenharmony_ci	   u64 start_addr, u64 end_addr, u16 embedded_bytes, int dest_partid)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	u8 msg_buffer[XPNET_MSG_SIZE];
3678c2ecf20Sopenharmony_ci	struct xpnet_message *msg = (struct xpnet_message *)&msg_buffer;
3688c2ecf20Sopenharmony_ci	u16 msg_size = sizeof(struct xpnet_message);
3698c2ecf20Sopenharmony_ci	enum xp_retval ret;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	msg->embedded_bytes = embedded_bytes;
3728c2ecf20Sopenharmony_ci	if (unlikely(embedded_bytes != 0)) {
3738c2ecf20Sopenharmony_ci		msg->version = XPNET_VERSION_EMBED;
3748c2ecf20Sopenharmony_ci		dev_dbg(xpnet, "calling memcpy(0x%p, 0x%p, 0x%lx)\n",
3758c2ecf20Sopenharmony_ci			&msg->data, skb->data, (size_t)embedded_bytes);
3768c2ecf20Sopenharmony_ci		skb_copy_from_linear_data(skb, &msg->data,
3778c2ecf20Sopenharmony_ci					  (size_t)embedded_bytes);
3788c2ecf20Sopenharmony_ci		msg_size += embedded_bytes - 1;
3798c2ecf20Sopenharmony_ci	} else {
3808c2ecf20Sopenharmony_ci		msg->version = XPNET_VERSION;
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci	msg->magic = XPNET_MAGIC;
3838c2ecf20Sopenharmony_ci	msg->size = end_addr - start_addr;
3848c2ecf20Sopenharmony_ci	msg->leadin_ignore = (u64)skb->data - start_addr;
3858c2ecf20Sopenharmony_ci	msg->tailout_ignore = end_addr - (u64)skb_tail_pointer(skb);
3868c2ecf20Sopenharmony_ci	msg->buf_pa = xp_pa((void *)start_addr);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	dev_dbg(xpnet, "sending XPC message to %d:%d\n"
3898c2ecf20Sopenharmony_ci		"msg->buf_pa=0x%lx, msg->size=%u, "
3908c2ecf20Sopenharmony_ci		"msg->leadin_ignore=%u, msg->tailout_ignore=%u\n",
3918c2ecf20Sopenharmony_ci		dest_partid, XPC_NET_CHANNEL, msg->buf_pa, msg->size,
3928c2ecf20Sopenharmony_ci		msg->leadin_ignore, msg->tailout_ignore);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	atomic_inc(&queued_msg->use_count);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	ret = xpc_send_notify(dest_partid, XPC_NET_CHANNEL, XPC_NOWAIT, msg,
3978c2ecf20Sopenharmony_ci			      msg_size, xpnet_send_completed, queued_msg);
3988c2ecf20Sopenharmony_ci	if (unlikely(ret != xpSuccess))
3998c2ecf20Sopenharmony_ci		atomic_dec(&queued_msg->use_count);
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci/*
4038c2ecf20Sopenharmony_ci * Network layer has formatted a packet (skb) and is ready to place it
4048c2ecf20Sopenharmony_ci * "on the wire".  Prepare and send an xpnet_message to all partitions
4058c2ecf20Sopenharmony_ci * which have connected with us and are targets of this packet.
4068c2ecf20Sopenharmony_ci *
4078c2ecf20Sopenharmony_ci * MAC-NOTE:  For the XPNET driver, the MAC address contains the
4088c2ecf20Sopenharmony_ci * destination partid.  If the destination partid octets are 0xffff,
4098c2ecf20Sopenharmony_ci * this packet is to be broadcast to all connected partitions.
4108c2ecf20Sopenharmony_ci */
4118c2ecf20Sopenharmony_cistatic netdev_tx_t
4128c2ecf20Sopenharmony_cixpnet_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	struct xpnet_pending_msg *queued_msg;
4158c2ecf20Sopenharmony_ci	u64 start_addr, end_addr;
4168c2ecf20Sopenharmony_ci	short dest_partid;
4178c2ecf20Sopenharmony_ci	u16 embedded_bytes = 0;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	dev_dbg(xpnet, ">skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
4208c2ecf20Sopenharmony_ci		"skb->end=0x%p skb->len=%d\n", (void *)skb->head,
4218c2ecf20Sopenharmony_ci		(void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
4228c2ecf20Sopenharmony_ci		skb->len);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	if (skb->data[0] == 0x33) {
4258c2ecf20Sopenharmony_ci		dev_kfree_skb(skb);
4268c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;	/* nothing needed to be done */
4278c2ecf20Sopenharmony_ci	}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	/*
4308c2ecf20Sopenharmony_ci	 * The xpnet_pending_msg tracks how many outstanding
4318c2ecf20Sopenharmony_ci	 * xpc_send_notifies are relying on this skb.  When none
4328c2ecf20Sopenharmony_ci	 * remain, release the skb.
4338c2ecf20Sopenharmony_ci	 */
4348c2ecf20Sopenharmony_ci	queued_msg = kmalloc(sizeof(struct xpnet_pending_msg), GFP_ATOMIC);
4358c2ecf20Sopenharmony_ci	if (queued_msg == NULL) {
4368c2ecf20Sopenharmony_ci		dev_warn(xpnet, "failed to kmalloc %ld bytes; dropping "
4378c2ecf20Sopenharmony_ci			 "packet\n", sizeof(struct xpnet_pending_msg));
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		dev->stats.tx_errors++;
4408c2ecf20Sopenharmony_ci		dev_kfree_skb(skb);
4418c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
4428c2ecf20Sopenharmony_ci	}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/* get the beginning of the first cacheline and end of last */
4458c2ecf20Sopenharmony_ci	start_addr = ((u64)skb->data & ~(L1_CACHE_BYTES - 1));
4468c2ecf20Sopenharmony_ci	end_addr = L1_CACHE_ALIGN((u64)skb_tail_pointer(skb));
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	/* calculate how many bytes to embed in the XPC message */
4498c2ecf20Sopenharmony_ci	if (unlikely(skb->len <= XPNET_MSG_DATA_MAX)) {
4508c2ecf20Sopenharmony_ci		/* skb->data does fit so embed */
4518c2ecf20Sopenharmony_ci		embedded_bytes = skb->len;
4528c2ecf20Sopenharmony_ci	}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	/*
4558c2ecf20Sopenharmony_ci	 * Since the send occurs asynchronously, we set the count to one
4568c2ecf20Sopenharmony_ci	 * and begin sending.  Any sends that happen to complete before
4578c2ecf20Sopenharmony_ci	 * we are done sending will not free the skb.  We will be left
4588c2ecf20Sopenharmony_ci	 * with that task during exit.  This also handles the case of
4598c2ecf20Sopenharmony_ci	 * a packet destined for a partition which is no longer up.
4608c2ecf20Sopenharmony_ci	 */
4618c2ecf20Sopenharmony_ci	atomic_set(&queued_msg->use_count, 1);
4628c2ecf20Sopenharmony_ci	queued_msg->skb = skb;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	if (skb->data[0] == 0xff) {
4658c2ecf20Sopenharmony_ci		/* we are being asked to broadcast to all partitions */
4668c2ecf20Sopenharmony_ci		for_each_set_bit(dest_partid, xpnet_broadcast_partitions,
4678c2ecf20Sopenharmony_ci			     xp_max_npartitions) {
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci			xpnet_send(skb, queued_msg, start_addr, end_addr,
4708c2ecf20Sopenharmony_ci				   embedded_bytes, dest_partid);
4718c2ecf20Sopenharmony_ci		}
4728c2ecf20Sopenharmony_ci	} else {
4738c2ecf20Sopenharmony_ci		dest_partid = (short)skb->data[XPNET_PARTID_OCTET + 1];
4748c2ecf20Sopenharmony_ci		dest_partid |= (short)skb->data[XPNET_PARTID_OCTET + 0] << 8;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci		if (dest_partid >= 0 &&
4778c2ecf20Sopenharmony_ci		    dest_partid < xp_max_npartitions &&
4788c2ecf20Sopenharmony_ci		    test_bit(dest_partid, xpnet_broadcast_partitions) != 0) {
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci			xpnet_send(skb, queued_msg, start_addr, end_addr,
4818c2ecf20Sopenharmony_ci				   embedded_bytes, dest_partid);
4828c2ecf20Sopenharmony_ci		}
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	dev->stats.tx_packets++;
4868c2ecf20Sopenharmony_ci	dev->stats.tx_bytes += skb->len;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	if (atomic_dec_return(&queued_msg->use_count) == 0) {
4898c2ecf20Sopenharmony_ci		dev_kfree_skb(skb);
4908c2ecf20Sopenharmony_ci		kfree(queued_msg);
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci/*
4978c2ecf20Sopenharmony_ci * Deal with transmit timeouts coming from the network layer.
4988c2ecf20Sopenharmony_ci */
4998c2ecf20Sopenharmony_cistatic void
5008c2ecf20Sopenharmony_cixpnet_dev_tx_timeout(struct net_device *dev, unsigned int txqueue)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	dev->stats.tx_errors++;
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic const struct net_device_ops xpnet_netdev_ops = {
5068c2ecf20Sopenharmony_ci	.ndo_open		= xpnet_dev_open,
5078c2ecf20Sopenharmony_ci	.ndo_stop		= xpnet_dev_stop,
5088c2ecf20Sopenharmony_ci	.ndo_start_xmit		= xpnet_dev_hard_start_xmit,
5098c2ecf20Sopenharmony_ci	.ndo_tx_timeout		= xpnet_dev_tx_timeout,
5108c2ecf20Sopenharmony_ci	.ndo_set_mac_address 	= eth_mac_addr,
5118c2ecf20Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
5128c2ecf20Sopenharmony_ci};
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic int __init
5158c2ecf20Sopenharmony_cixpnet_init(void)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	int result;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	if (!is_uv_system())
5208c2ecf20Sopenharmony_ci		return -ENODEV;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	dev_info(xpnet, "registering network device %s\n", XPNET_DEVICE_NAME);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	xpnet_broadcast_partitions = kcalloc(BITS_TO_LONGS(xp_max_npartitions),
5258c2ecf20Sopenharmony_ci					     sizeof(long),
5268c2ecf20Sopenharmony_ci					     GFP_KERNEL);
5278c2ecf20Sopenharmony_ci	if (xpnet_broadcast_partitions == NULL)
5288c2ecf20Sopenharmony_ci		return -ENOMEM;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	/*
5318c2ecf20Sopenharmony_ci	 * use ether_setup() to init the majority of our device
5328c2ecf20Sopenharmony_ci	 * structure and then override the necessary pieces.
5338c2ecf20Sopenharmony_ci	 */
5348c2ecf20Sopenharmony_ci	xpnet_device = alloc_netdev(0, XPNET_DEVICE_NAME, NET_NAME_UNKNOWN,
5358c2ecf20Sopenharmony_ci				    ether_setup);
5368c2ecf20Sopenharmony_ci	if (xpnet_device == NULL) {
5378c2ecf20Sopenharmony_ci		kfree(xpnet_broadcast_partitions);
5388c2ecf20Sopenharmony_ci		return -ENOMEM;
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	netif_carrier_off(xpnet_device);
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	xpnet_device->netdev_ops = &xpnet_netdev_ops;
5448c2ecf20Sopenharmony_ci	xpnet_device->mtu = XPNET_DEF_MTU;
5458c2ecf20Sopenharmony_ci	xpnet_device->min_mtu = XPNET_MIN_MTU;
5468c2ecf20Sopenharmony_ci	xpnet_device->max_mtu = XPNET_MAX_MTU;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/*
5498c2ecf20Sopenharmony_ci	 * Multicast assumes the LSB of the first octet is set for multicast
5508c2ecf20Sopenharmony_ci	 * MAC addresses.  We chose the first octet of the MAC to be unlikely
5518c2ecf20Sopenharmony_ci	 * to collide with any vendor's officially issued MAC.
5528c2ecf20Sopenharmony_ci	 */
5538c2ecf20Sopenharmony_ci	xpnet_device->dev_addr[0] = 0x02;     /* locally administered, no OUI */
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	xpnet_device->dev_addr[XPNET_PARTID_OCTET + 1] = xp_partition_id;
5568c2ecf20Sopenharmony_ci	xpnet_device->dev_addr[XPNET_PARTID_OCTET + 0] = (xp_partition_id >> 8);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	/*
5598c2ecf20Sopenharmony_ci	 * ether_setup() sets this to a multicast device.  We are
5608c2ecf20Sopenharmony_ci	 * really not supporting multicast at this time.
5618c2ecf20Sopenharmony_ci	 */
5628c2ecf20Sopenharmony_ci	xpnet_device->flags &= ~IFF_MULTICAST;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	/*
5658c2ecf20Sopenharmony_ci	 * No need to checksum as it is a DMA transfer.  The BTE will
5668c2ecf20Sopenharmony_ci	 * report an error if the data is not retrievable and the
5678c2ecf20Sopenharmony_ci	 * packet will be dropped.
5688c2ecf20Sopenharmony_ci	 */
5698c2ecf20Sopenharmony_ci	xpnet_device->features = NETIF_F_HW_CSUM;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	result = register_netdev(xpnet_device);
5728c2ecf20Sopenharmony_ci	if (result != 0) {
5738c2ecf20Sopenharmony_ci		free_netdev(xpnet_device);
5748c2ecf20Sopenharmony_ci		kfree(xpnet_broadcast_partitions);
5758c2ecf20Sopenharmony_ci	}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	return result;
5788c2ecf20Sopenharmony_ci}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_cimodule_init(xpnet_init);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_cistatic void __exit
5838c2ecf20Sopenharmony_cixpnet_exit(void)
5848c2ecf20Sopenharmony_ci{
5858c2ecf20Sopenharmony_ci	dev_info(xpnet, "unregistering network device %s\n",
5868c2ecf20Sopenharmony_ci		 xpnet_device[0].name);
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	unregister_netdev(xpnet_device);
5898c2ecf20Sopenharmony_ci	free_netdev(xpnet_device);
5908c2ecf20Sopenharmony_ci	kfree(xpnet_broadcast_partitions);
5918c2ecf20Sopenharmony_ci}
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_cimodule_exit(xpnet_exit);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ciMODULE_AUTHOR("Silicon Graphics, Inc.");
5968c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Cross Partition Network adapter (XPNET)");
5978c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
598