xref: /openbmc/linux/drivers/misc/sgi-xp/xpnet.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
145d9ca49SDean Nelson /*
245d9ca49SDean Nelson  * This file is subject to the terms and conditions of the GNU General Public
345d9ca49SDean Nelson  * License.  See the file "COPYING" in the main directory of this archive
445d9ca49SDean Nelson  * for more details.
545d9ca49SDean Nelson  *
67a6d94f0SMike Travis  * (C) Copyright 2020 Hewlett Packard Enterprise Development LP
7714c48f1SDean Nelson  * Copyright (C) 1999-2009 Silicon Graphics, Inc. All rights reserved.
845d9ca49SDean Nelson  */
945d9ca49SDean Nelson 
1045d9ca49SDean Nelson /*
1145d9ca49SDean Nelson  * Cross Partition Network Interface (XPNET) support
1245d9ca49SDean Nelson  *
1345d9ca49SDean Nelson  *	XPNET provides a virtual network layered on top of the Cross
1445d9ca49SDean Nelson  *	Partition communication layer.
1545d9ca49SDean Nelson  *
1645d9ca49SDean Nelson  *	XPNET provides direct point-to-point and broadcast-like support
1745d9ca49SDean Nelson  *	for an ethernet-like device.  The ethernet broadcast medium is
1845d9ca49SDean Nelson  *	replaced with a point-to-point message structure which passes
1945d9ca49SDean Nelson  *	pointers to a DMA-capable block that a remote partition should
2045d9ca49SDean Nelson  *	retrieve and pass to the upper level networking layer.
2145d9ca49SDean Nelson  *
2245d9ca49SDean Nelson  */
2345d9ca49SDean Nelson 
245a0e3ad6STejun Heo #include <linux/slab.h>
2545d9ca49SDean Nelson #include <linux/module.h>
2645d9ca49SDean Nelson #include <linux/netdevice.h>
2745d9ca49SDean Nelson #include <linux/etherdevice.h>
2845d9ca49SDean Nelson #include "xp.h"
2945d9ca49SDean Nelson 
3045d9ca49SDean Nelson /*
3145d9ca49SDean Nelson  * The message payload transferred by XPC.
3245d9ca49SDean Nelson  *
3345d9ca49SDean Nelson  * buf_pa is the physical address where the DMA should pull from.
3445d9ca49SDean Nelson  *
3545d9ca49SDean Nelson  * NOTE: for performance reasons, buf_pa should _ALWAYS_ begin on a
3645d9ca49SDean Nelson  * cacheline boundary.  To accomplish this, we record the number of
3745d9ca49SDean Nelson  * bytes from the beginning of the first cacheline to the first useful
3845d9ca49SDean Nelson  * byte of the skb (leadin_ignore) and the number of bytes from the
3945d9ca49SDean Nelson  * last useful byte of the skb to the end of the last cacheline
4045d9ca49SDean Nelson  * (tailout_ignore).
4145d9ca49SDean Nelson  *
4245d9ca49SDean Nelson  * size is the number of bytes to transfer which includes the skb->len
4345d9ca49SDean Nelson  * (useful bytes of the senders skb) plus the leadin and tailout
4445d9ca49SDean Nelson  */
4545d9ca49SDean Nelson struct xpnet_message {
4645d9ca49SDean Nelson 	u16 version;		/* Version for this message */
4745d9ca49SDean Nelson 	u16 embedded_bytes;	/* #of bytes embedded in XPC message */
4845d9ca49SDean Nelson 	u32 magic;		/* Special number indicating this is xpnet */
49a812dcc3SDean Nelson 	unsigned long buf_pa;	/* phys address of buffer to retrieve */
5045d9ca49SDean Nelson 	u32 size;		/* #of bytes in buffer */
5145d9ca49SDean Nelson 	u8 leadin_ignore;	/* #of bytes to ignore at the beginning */
5245d9ca49SDean Nelson 	u8 tailout_ignore;	/* #of bytes to ignore at the end */
5345d9ca49SDean Nelson 	unsigned char data;	/* body of small packets */
5445d9ca49SDean Nelson };
5545d9ca49SDean Nelson 
5645d9ca49SDean Nelson /*
5745d9ca49SDean Nelson  * Determine the size of our message, the cacheline aligned size,
5845d9ca49SDean Nelson  * and then the number of message will request from XPC.
5945d9ca49SDean Nelson  *
6045d9ca49SDean Nelson  * XPC expects each message to exist in an individual cacheline.
6145d9ca49SDean Nelson  */
62bd3e64c1SDean Nelson #define XPNET_MSG_SIZE		XPC_MSG_PAYLOAD_MAX_SIZE
6345d9ca49SDean Nelson #define XPNET_MSG_DATA_MAX	\
64bd3e64c1SDean Nelson 		(XPNET_MSG_SIZE - offsetof(struct xpnet_message, data))
65bd3e64c1SDean Nelson #define XPNET_MSG_NENTRIES	(PAGE_SIZE / XPC_MSG_MAX_SIZE)
6645d9ca49SDean Nelson 
6745d9ca49SDean Nelson #define XPNET_MAX_KTHREADS	(XPNET_MSG_NENTRIES + 1)
6845d9ca49SDean Nelson #define XPNET_MAX_IDLE_KTHREADS	(XPNET_MSG_NENTRIES + 1)
6945d9ca49SDean Nelson 
7045d9ca49SDean Nelson /*
7145d9ca49SDean Nelson  * Version number of XPNET implementation. XPNET can always talk to versions
7245d9ca49SDean Nelson  * with same major #, and never talk to versions with a different version.
7345d9ca49SDean Nelson  */
7445d9ca49SDean Nelson #define _XPNET_VERSION(_major, _minor)	(((_major) << 4) | (_minor))
7545d9ca49SDean Nelson #define XPNET_VERSION_MAJOR(_v)		((_v) >> 4)
7645d9ca49SDean Nelson #define XPNET_VERSION_MINOR(_v)		((_v) & 0xf)
7745d9ca49SDean Nelson 
7845d9ca49SDean Nelson #define	XPNET_VERSION _XPNET_VERSION(1, 0)	/* version 1.0 */
7945d9ca49SDean Nelson #define	XPNET_VERSION_EMBED _XPNET_VERSION(1, 1)	/* version 1.1 */
8045d9ca49SDean Nelson #define XPNET_MAGIC	0x88786984	/* "XNET" */
8145d9ca49SDean Nelson 
8245d9ca49SDean Nelson #define XPNET_VALID_MSG(_m)						     \
8345d9ca49SDean Nelson    ((XPNET_VERSION_MAJOR(_m->version) == XPNET_VERSION_MAJOR(XPNET_VERSION)) \
8445d9ca49SDean Nelson     && (msg->magic == XPNET_MAGIC))
8545d9ca49SDean Nelson 
8645d9ca49SDean Nelson #define XPNET_DEVICE_NAME		"xp0"
8745d9ca49SDean Nelson 
8845d9ca49SDean Nelson /*
8945d9ca49SDean Nelson  * When messages are queued with xpc_send_notify, a kmalloc'd buffer
9045d9ca49SDean Nelson  * of the following type is passed as a notification cookie.  When the
9145d9ca49SDean Nelson  * notification function is called, we use the cookie to decide
9245d9ca49SDean Nelson  * whether all outstanding message sends have completed.  The skb can
9345d9ca49SDean Nelson  * then be released.
9445d9ca49SDean Nelson  */
9545d9ca49SDean Nelson struct xpnet_pending_msg {
9645d9ca49SDean Nelson 	struct sk_buff *skb;
9745d9ca49SDean Nelson 	atomic_t use_count;
9845d9ca49SDean Nelson };
9945d9ca49SDean Nelson 
1002a23537bSJason Yan static struct net_device *xpnet_device;
10145d9ca49SDean Nelson 
10245d9ca49SDean Nelson /*
10345d9ca49SDean Nelson  * When we are notified of other partitions activating, we add them to
10445d9ca49SDean Nelson  * our bitmask of partitions to which we broadcast.
10545d9ca49SDean Nelson  */
106a7b4d509SDean Nelson static unsigned long *xpnet_broadcast_partitions;
10745d9ca49SDean Nelson /* protect above */
10845d9ca49SDean Nelson static DEFINE_SPINLOCK(xpnet_broadcast_lock);
10945d9ca49SDean Nelson 
11045d9ca49SDean Nelson /*
11145d9ca49SDean Nelson  * Since the Block Transfer Engine (BTE) is being used for the transfer
11245d9ca49SDean Nelson  * and it relies upon cache-line size transfers, we need to reserve at
11345d9ca49SDean Nelson  * least one cache-line for head and tail alignment.  The BTE is
11445d9ca49SDean Nelson  * limited to 8MB transfers.
11545d9ca49SDean Nelson  *
11645d9ca49SDean Nelson  * Testing has shown that changing MTU to greater than 64KB has no effect
11745d9ca49SDean Nelson  * on TCP as the two sides negotiate a Max Segment Size that is limited
11845d9ca49SDean Nelson  * to 64K.  Other protocols May use packets greater than this, but for
11945d9ca49SDean Nelson  * now, the default is 64KB.
12045d9ca49SDean Nelson  */
12145d9ca49SDean Nelson #define XPNET_MAX_MTU (0x800000UL - L1_CACHE_BYTES)
122b3e3893eSJarod Wilson /* 68 comes from min TCP+IP+MAC header */
123b3e3893eSJarod Wilson #define XPNET_MIN_MTU 68
12445d9ca49SDean Nelson /* 32KB has been determined to be the ideal */
12545d9ca49SDean Nelson #define XPNET_DEF_MTU (0x8000UL)
12645d9ca49SDean Nelson 
12745d9ca49SDean Nelson /*
128a7b4d509SDean Nelson  * The partid is encapsulated in the MAC address beginning in the following
129a7b4d509SDean Nelson  * octet and it consists of two octets.
13045d9ca49SDean Nelson  */
131a7b4d509SDean Nelson #define XPNET_PARTID_OCTET	2
13245d9ca49SDean Nelson 
133a7b4d509SDean Nelson /* Define the XPNET debug device structures to be used with dev_dbg() et al */
134a7b4d509SDean Nelson 
1352a23537bSJason Yan static struct device_driver xpnet_dbg_name = {
13645d9ca49SDean Nelson 	.name = "xpnet"
13745d9ca49SDean Nelson };
13845d9ca49SDean Nelson 
1392a23537bSJason Yan static struct device xpnet_dbg_subname = {
140bb0dc43eSKay Sievers 	.init_name = "",	/* set to "" */
14145d9ca49SDean Nelson 	.driver = &xpnet_dbg_name
14245d9ca49SDean Nelson };
14345d9ca49SDean Nelson 
1442a23537bSJason Yan static struct device *xpnet = &xpnet_dbg_subname;
14545d9ca49SDean Nelson 
14645d9ca49SDean Nelson /*
14745d9ca49SDean Nelson  * Packet was recevied by XPC and forwarded to us.
14845d9ca49SDean Nelson  */
14945d9ca49SDean Nelson static void
xpnet_receive(short partid,int channel,struct xpnet_message * msg)15064d032baSDean Nelson xpnet_receive(short partid, int channel, struct xpnet_message *msg)
15145d9ca49SDean Nelson {
15245d9ca49SDean Nelson 	struct sk_buff *skb;
153a812dcc3SDean Nelson 	void *dst;
154908787dbSDean Nelson 	enum xp_retval ret;
15545d9ca49SDean Nelson 
15645d9ca49SDean Nelson 	if (!XPNET_VALID_MSG(msg)) {
15745d9ca49SDean Nelson 		/*
15845d9ca49SDean Nelson 		 * Packet with a different XPC version.  Ignore.
15945d9ca49SDean Nelson 		 */
16045d9ca49SDean Nelson 		xpc_received(partid, channel, (void *)msg);
16145d9ca49SDean Nelson 
162e8ac9c55SStephen Hemminger 		xpnet_device->stats.rx_errors++;
16345d9ca49SDean Nelson 
16445d9ca49SDean Nelson 		return;
16545d9ca49SDean Nelson 	}
166a812dcc3SDean Nelson 	dev_dbg(xpnet, "received 0x%lx, %d, %d, %d\n", msg->buf_pa, msg->size,
167a812dcc3SDean Nelson 		msg->leadin_ignore, msg->tailout_ignore);
16845d9ca49SDean Nelson 
16945d9ca49SDean Nelson 	/* reserve an extra cache line */
17045d9ca49SDean Nelson 	skb = dev_alloc_skb(msg->size + L1_CACHE_BYTES);
17145d9ca49SDean Nelson 	if (!skb) {
17245d9ca49SDean Nelson 		dev_err(xpnet, "failed on dev_alloc_skb(%d)\n",
17345d9ca49SDean Nelson 			msg->size + L1_CACHE_BYTES);
17445d9ca49SDean Nelson 
17545d9ca49SDean Nelson 		xpc_received(partid, channel, (void *)msg);
17645d9ca49SDean Nelson 
177e8ac9c55SStephen Hemminger 		xpnet_device->stats.rx_errors++;
17845d9ca49SDean Nelson 
17945d9ca49SDean Nelson 		return;
18045d9ca49SDean Nelson 	}
18145d9ca49SDean Nelson 
18245d9ca49SDean Nelson 	/*
18345d9ca49SDean Nelson 	 * The allocated skb has some reserved space.
184908787dbSDean Nelson 	 * In order to use xp_remote_memcpy(), we need to get the
18545d9ca49SDean Nelson 	 * skb->data pointer moved forward.
18645d9ca49SDean Nelson 	 */
18745d9ca49SDean Nelson 	skb_reserve(skb, (L1_CACHE_BYTES - ((u64)skb->data &
18845d9ca49SDean Nelson 					    (L1_CACHE_BYTES - 1)) +
18945d9ca49SDean Nelson 			  msg->leadin_ignore));
19045d9ca49SDean Nelson 
19145d9ca49SDean Nelson 	/*
19245d9ca49SDean Nelson 	 * Update the tail pointer to indicate data actually
19345d9ca49SDean Nelson 	 * transferred.
19445d9ca49SDean Nelson 	 */
19545d9ca49SDean Nelson 	skb_put(skb, (msg->size - msg->leadin_ignore - msg->tailout_ignore));
19645d9ca49SDean Nelson 
19745d9ca49SDean Nelson 	/*
19845d9ca49SDean Nelson 	 * Move the data over from the other side.
19945d9ca49SDean Nelson 	 */
20045d9ca49SDean Nelson 	if ((XPNET_VERSION_MINOR(msg->version) == 1) &&
20145d9ca49SDean Nelson 	    (msg->embedded_bytes != 0)) {
20245d9ca49SDean Nelson 		dev_dbg(xpnet, "copying embedded message. memcpy(0x%p, 0x%p, "
20345d9ca49SDean Nelson 			"%lu)\n", skb->data, &msg->data,
20445d9ca49SDean Nelson 			(size_t)msg->embedded_bytes);
20545d9ca49SDean Nelson 
20635190506SDean Nelson 		skb_copy_to_linear_data(skb, &msg->data,
20735190506SDean Nelson 					(size_t)msg->embedded_bytes);
20845d9ca49SDean Nelson 	} else {
209a812dcc3SDean Nelson 		dst = (void *)((u64)skb->data & ~(L1_CACHE_BYTES - 1));
21045d9ca49SDean Nelson 		dev_dbg(xpnet, "transferring buffer to the skb->data area;\n\t"
211c41e21dcSTom Rix 			"xp_remote_memcpy(0x%p, 0x%p, %u)\n", dst,
212908787dbSDean Nelson 					  (void *)msg->buf_pa, msg->size);
21345d9ca49SDean Nelson 
214a812dcc3SDean Nelson 		ret = xp_remote_memcpy(xp_pa(dst), msg->buf_pa, msg->size);
215908787dbSDean Nelson 		if (ret != xpSuccess) {
2162c2b94f9SDean Nelson 			/*
217ea57f80cSDean Nelson 			 * !!! Need better way of cleaning skb.  Currently skb
218ea57f80cSDean Nelson 			 * !!! appears in_use and we can't just call
219ea57f80cSDean Nelson 			 * !!! dev_kfree_skb.
2202c2b94f9SDean Nelson 			 */
221c41e21dcSTom Rix 			dev_err(xpnet, "xp_remote_memcpy(0x%p, 0x%p, 0x%x) "
222a812dcc3SDean Nelson 				"returned error=0x%x\n", dst,
223908787dbSDean Nelson 				(void *)msg->buf_pa, msg->size, ret);
22445d9ca49SDean Nelson 
22545d9ca49SDean Nelson 			xpc_received(partid, channel, (void *)msg);
22645d9ca49SDean Nelson 
227e8ac9c55SStephen Hemminger 			xpnet_device->stats.rx_errors++;
22845d9ca49SDean Nelson 
22945d9ca49SDean Nelson 			return;
23045d9ca49SDean Nelson 		}
23145d9ca49SDean Nelson 	}
23245d9ca49SDean Nelson 
23345d9ca49SDean Nelson 	dev_dbg(xpnet, "<skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
23445d9ca49SDean Nelson 		"skb->end=0x%p skb->len=%d\n", (void *)skb->head,
23545d9ca49SDean Nelson 		(void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
23645d9ca49SDean Nelson 		skb->len);
23745d9ca49SDean Nelson 
23845d9ca49SDean Nelson 	skb->protocol = eth_type_trans(skb, xpnet_device);
23945d9ca49SDean Nelson 	skb->ip_summed = CHECKSUM_UNNECESSARY;
24045d9ca49SDean Nelson 
24145d9ca49SDean Nelson 	dev_dbg(xpnet, "passing skb to network layer\n"
242ad361c98SJoe Perches 		"\tskb->head=0x%p skb->data=0x%p skb->tail=0x%p "
24345d9ca49SDean Nelson 		"skb->end=0x%p skb->len=%d\n",
24445d9ca49SDean Nelson 		(void *)skb->head, (void *)skb->data, skb_tail_pointer(skb),
24545d9ca49SDean Nelson 		skb_end_pointer(skb), skb->len);
24645d9ca49SDean Nelson 
247e8ac9c55SStephen Hemminger 	xpnet_device->stats.rx_packets++;
248e8ac9c55SStephen Hemminger 	xpnet_device->stats.rx_bytes += skb->len + ETH_HLEN;
24945d9ca49SDean Nelson 
2504343b866SSebastian Andrzej Siewior 	netif_rx(skb);
25145d9ca49SDean Nelson 	xpc_received(partid, channel, (void *)msg);
25245d9ca49SDean Nelson }
25345d9ca49SDean Nelson 
25445d9ca49SDean Nelson /*
25545d9ca49SDean Nelson  * This is the handler which XPC calls during any sort of change in
25645d9ca49SDean Nelson  * state or message reception on a connection.
25745d9ca49SDean Nelson  */
25845d9ca49SDean Nelson static void
xpnet_connection_activity(enum xp_retval reason,short partid,int channel,void * data,void * key)25964d032baSDean Nelson xpnet_connection_activity(enum xp_retval reason, short partid, int channel,
26045d9ca49SDean Nelson 			  void *data, void *key)
26145d9ca49SDean Nelson {
262bc63d387SDean Nelson 	DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
26345d9ca49SDean Nelson 	DBUG_ON(channel != XPC_NET_CHANNEL);
26445d9ca49SDean Nelson 
26545d9ca49SDean Nelson 	switch (reason) {
26665c17b80SDean Nelson 	case xpMsgReceived:	/* message received */
26745d9ca49SDean Nelson 		DBUG_ON(data == NULL);
26845d9ca49SDean Nelson 
26945d9ca49SDean Nelson 		xpnet_receive(partid, channel, (struct xpnet_message *)data);
27045d9ca49SDean Nelson 		break;
27145d9ca49SDean Nelson 
27265c17b80SDean Nelson 	case xpConnected:	/* connection completed to a partition */
27345d9ca49SDean Nelson 		spin_lock_bh(&xpnet_broadcast_lock);
274a7b4d509SDean Nelson 		__set_bit(partid, xpnet_broadcast_partitions);
27545d9ca49SDean Nelson 		spin_unlock_bh(&xpnet_broadcast_lock);
27645d9ca49SDean Nelson 
27745d9ca49SDean Nelson 		netif_carrier_on(xpnet_device);
27845d9ca49SDean Nelson 
279a7b4d509SDean Nelson 		dev_dbg(xpnet, "%s connected to partition %d\n",
280a7b4d509SDean Nelson 			xpnet_device->name, partid);
28145d9ca49SDean Nelson 		break;
28245d9ca49SDean Nelson 
28345d9ca49SDean Nelson 	default:
28445d9ca49SDean Nelson 		spin_lock_bh(&xpnet_broadcast_lock);
285a7b4d509SDean Nelson 		__clear_bit(partid, xpnet_broadcast_partitions);
28645d9ca49SDean Nelson 		spin_unlock_bh(&xpnet_broadcast_lock);
28745d9ca49SDean Nelson 
288*64c59166SChristophe JAILLET 		if (bitmap_empty(xpnet_broadcast_partitions,
289a7b4d509SDean Nelson 				 xp_max_npartitions)) {
29045d9ca49SDean Nelson 			netif_carrier_off(xpnet_device);
291a7b4d509SDean Nelson 		}
29245d9ca49SDean Nelson 
293a7b4d509SDean Nelson 		dev_dbg(xpnet, "%s disconnected from partition %d\n",
294a7b4d509SDean Nelson 			xpnet_device->name, partid);
29545d9ca49SDean Nelson 		break;
29645d9ca49SDean Nelson 	}
29745d9ca49SDean Nelson }
29845d9ca49SDean Nelson 
29945d9ca49SDean Nelson static int
xpnet_dev_open(struct net_device * dev)30045d9ca49SDean Nelson xpnet_dev_open(struct net_device *dev)
30145d9ca49SDean Nelson {
30265c17b80SDean Nelson 	enum xp_retval ret;
30345d9ca49SDean Nelson 
30445d9ca49SDean Nelson 	dev_dbg(xpnet, "calling xpc_connect(%d, 0x%p, NULL, %ld, %ld, %ld, "
30545d9ca49SDean Nelson 		"%ld)\n", XPC_NET_CHANNEL, xpnet_connection_activity,
306261f3b49SDean Nelson 		(unsigned long)XPNET_MSG_SIZE,
307261f3b49SDean Nelson 		(unsigned long)XPNET_MSG_NENTRIES,
308261f3b49SDean Nelson 		(unsigned long)XPNET_MAX_KTHREADS,
309261f3b49SDean Nelson 		(unsigned long)XPNET_MAX_IDLE_KTHREADS);
31045d9ca49SDean Nelson 
31145d9ca49SDean Nelson 	ret = xpc_connect(XPC_NET_CHANNEL, xpnet_connection_activity, NULL,
31245d9ca49SDean Nelson 			  XPNET_MSG_SIZE, XPNET_MSG_NENTRIES,
31345d9ca49SDean Nelson 			  XPNET_MAX_KTHREADS, XPNET_MAX_IDLE_KTHREADS);
31465c17b80SDean Nelson 	if (ret != xpSuccess) {
31545d9ca49SDean Nelson 		dev_err(xpnet, "ifconfig up of %s failed on XPC connect, "
31645d9ca49SDean Nelson 			"ret=%d\n", dev->name, ret);
31745d9ca49SDean Nelson 
31845d9ca49SDean Nelson 		return -ENOMEM;
31945d9ca49SDean Nelson 	}
32045d9ca49SDean Nelson 
32145d9ca49SDean Nelson 	dev_dbg(xpnet, "ifconfig up of %s; XPC connected\n", dev->name);
32245d9ca49SDean Nelson 
32345d9ca49SDean Nelson 	return 0;
32445d9ca49SDean Nelson }
32545d9ca49SDean Nelson 
32645d9ca49SDean Nelson static int
xpnet_dev_stop(struct net_device * dev)32745d9ca49SDean Nelson xpnet_dev_stop(struct net_device *dev)
32845d9ca49SDean Nelson {
32945d9ca49SDean Nelson 	xpc_disconnect(XPC_NET_CHANNEL);
33045d9ca49SDean Nelson 
33145d9ca49SDean Nelson 	dev_dbg(xpnet, "ifconfig down of %s; XPC disconnected\n", dev->name);
33245d9ca49SDean Nelson 
33345d9ca49SDean Nelson 	return 0;
33445d9ca49SDean Nelson }
33545d9ca49SDean Nelson 
33645d9ca49SDean Nelson /*
33745d9ca49SDean Nelson  * Notification that the other end has received the message and
33845d9ca49SDean Nelson  * DMA'd the skb information.  At this point, they are done with
33945d9ca49SDean Nelson  * our side.  When all recipients are done processing, we
34045d9ca49SDean Nelson  * release the skb and then release our pending message structure.
34145d9ca49SDean Nelson  */
34245d9ca49SDean Nelson static void
xpnet_send_completed(enum xp_retval reason,short partid,int channel,void * __qm)34364d032baSDean Nelson xpnet_send_completed(enum xp_retval reason, short partid, int channel,
34445d9ca49SDean Nelson 		     void *__qm)
34545d9ca49SDean Nelson {
34635190506SDean Nelson 	struct xpnet_pending_msg *queued_msg = (struct xpnet_pending_msg *)__qm;
34745d9ca49SDean Nelson 
34845d9ca49SDean Nelson 	DBUG_ON(queued_msg == NULL);
34945d9ca49SDean Nelson 
35045d9ca49SDean Nelson 	dev_dbg(xpnet, "message to %d notified with reason %d\n",
35145d9ca49SDean Nelson 		partid, reason);
35245d9ca49SDean Nelson 
35345d9ca49SDean Nelson 	if (atomic_dec_return(&queued_msg->use_count) == 0) {
35445d9ca49SDean Nelson 		dev_dbg(xpnet, "all acks for skb->head=-x%p\n",
35545d9ca49SDean Nelson 			(void *)queued_msg->skb->head);
35645d9ca49SDean Nelson 
35745d9ca49SDean Nelson 		dev_kfree_skb_any(queued_msg->skb);
35845d9ca49SDean Nelson 		kfree(queued_msg);
35945d9ca49SDean Nelson 	}
36045d9ca49SDean Nelson }
36145d9ca49SDean Nelson 
362a7b4d509SDean Nelson static void
xpnet_send(struct sk_buff * skb,struct xpnet_pending_msg * queued_msg,u64 start_addr,u64 end_addr,u16 embedded_bytes,int dest_partid)363a7b4d509SDean Nelson xpnet_send(struct sk_buff *skb, struct xpnet_pending_msg *queued_msg,
364a7b4d509SDean Nelson 	   u64 start_addr, u64 end_addr, u16 embedded_bytes, int dest_partid)
36545d9ca49SDean Nelson {
36697bf1aa1SDean Nelson 	u8 msg_buffer[XPNET_MSG_SIZE];
367a7b4d509SDean Nelson 	struct xpnet_message *msg = (struct xpnet_message *)&msg_buffer;
368bd3e64c1SDean Nelson 	u16 msg_size = sizeof(struct xpnet_message);
369a7b4d509SDean Nelson 	enum xp_retval ret;
37045d9ca49SDean Nelson 
37145d9ca49SDean Nelson 	msg->embedded_bytes = embedded_bytes;
37245d9ca49SDean Nelson 	if (unlikely(embedded_bytes != 0)) {
37345d9ca49SDean Nelson 		msg->version = XPNET_VERSION_EMBED;
37445d9ca49SDean Nelson 		dev_dbg(xpnet, "calling memcpy(0x%p, 0x%p, 0x%lx)\n",
37545d9ca49SDean Nelson 			&msg->data, skb->data, (size_t)embedded_bytes);
37645d9ca49SDean Nelson 		skb_copy_from_linear_data(skb, &msg->data,
37745d9ca49SDean Nelson 					  (size_t)embedded_bytes);
378bd3e64c1SDean Nelson 		msg_size += embedded_bytes - 1;
37945d9ca49SDean Nelson 	} else {
38045d9ca49SDean Nelson 		msg->version = XPNET_VERSION;
38145d9ca49SDean Nelson 	}
38245d9ca49SDean Nelson 	msg->magic = XPNET_MAGIC;
38345d9ca49SDean Nelson 	msg->size = end_addr - start_addr;
38445d9ca49SDean Nelson 	msg->leadin_ignore = (u64)skb->data - start_addr;
38545d9ca49SDean Nelson 	msg->tailout_ignore = end_addr - (u64)skb_tail_pointer(skb);
386a812dcc3SDean Nelson 	msg->buf_pa = xp_pa((void *)start_addr);
38745d9ca49SDean Nelson 
38845d9ca49SDean Nelson 	dev_dbg(xpnet, "sending XPC message to %d:%d\n"
389ad361c98SJoe Perches 		"msg->buf_pa=0x%lx, msg->size=%u, "
39045d9ca49SDean Nelson 		"msg->leadin_ignore=%u, msg->tailout_ignore=%u\n",
391a812dcc3SDean Nelson 		dest_partid, XPC_NET_CHANNEL, msg->buf_pa, msg->size,
392a812dcc3SDean Nelson 		msg->leadin_ignore, msg->tailout_ignore);
39345d9ca49SDean Nelson 
39445d9ca49SDean Nelson 	atomic_inc(&queued_msg->use_count);
39545d9ca49SDean Nelson 
396a7b4d509SDean Nelson 	ret = xpc_send_notify(dest_partid, XPC_NET_CHANNEL, XPC_NOWAIT, msg,
397bd3e64c1SDean Nelson 			      msg_size, xpnet_send_completed, queued_msg);
398a7b4d509SDean Nelson 	if (unlikely(ret != xpSuccess))
39945d9ca49SDean Nelson 		atomic_dec(&queued_msg->use_count);
400a7b4d509SDean Nelson }
401a7b4d509SDean Nelson 
402a7b4d509SDean Nelson /*
403a7b4d509SDean Nelson  * Network layer has formatted a packet (skb) and is ready to place it
404a7b4d509SDean Nelson  * "on the wire".  Prepare and send an xpnet_message to all partitions
405a7b4d509SDean Nelson  * which have connected with us and are targets of this packet.
406a7b4d509SDean Nelson  *
407a7b4d509SDean Nelson  * MAC-NOTE:  For the XPNET driver, the MAC address contains the
408a7b4d509SDean Nelson  * destination partid.  If the destination partid octets are 0xffff,
409a7b4d509SDean Nelson  * this packet is to be broadcast to all connected partitions.
410a7b4d509SDean Nelson  */
41172b6500cSLuc Van Oostenryck static netdev_tx_t
xpnet_dev_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)412a7b4d509SDean Nelson xpnet_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
413a7b4d509SDean Nelson {
414a7b4d509SDean Nelson 	struct xpnet_pending_msg *queued_msg;
415a7b4d509SDean Nelson 	u64 start_addr, end_addr;
416a7b4d509SDean Nelson 	short dest_partid;
417a7b4d509SDean Nelson 	u16 embedded_bytes = 0;
418a7b4d509SDean Nelson 
419a7b4d509SDean Nelson 	dev_dbg(xpnet, ">skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
420a7b4d509SDean Nelson 		"skb->end=0x%p skb->len=%d\n", (void *)skb->head,
421a7b4d509SDean Nelson 		(void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
422a7b4d509SDean Nelson 		skb->len);
423a7b4d509SDean Nelson 
424a7b4d509SDean Nelson 	if (skb->data[0] == 0x33) {
425a7b4d509SDean Nelson 		dev_kfree_skb(skb);
4266ed10654SPatrick McHardy 		return NETDEV_TX_OK;	/* nothing needed to be done */
427a7b4d509SDean Nelson 	}
428a7b4d509SDean Nelson 
429a7b4d509SDean Nelson 	/*
430a7b4d509SDean Nelson 	 * The xpnet_pending_msg tracks how many outstanding
431a7b4d509SDean Nelson 	 * xpc_send_notifies are relying on this skb.  When none
432a7b4d509SDean Nelson 	 * remain, release the skb.
433a7b4d509SDean Nelson 	 */
434a7b4d509SDean Nelson 	queued_msg = kmalloc(sizeof(struct xpnet_pending_msg), GFP_ATOMIC);
435a7b4d509SDean Nelson 	if (queued_msg == NULL) {
436a7b4d509SDean Nelson 		dev_warn(xpnet, "failed to kmalloc %ld bytes; dropping "
437a7b4d509SDean Nelson 			 "packet\n", sizeof(struct xpnet_pending_msg));
438a7b4d509SDean Nelson 
439e8ac9c55SStephen Hemminger 		dev->stats.tx_errors++;
4403790c8cdSPatrick McHardy 		dev_kfree_skb(skb);
4413790c8cdSPatrick McHardy 		return NETDEV_TX_OK;
442a7b4d509SDean Nelson 	}
443a7b4d509SDean Nelson 
444a7b4d509SDean Nelson 	/* get the beginning of the first cacheline and end of last */
445a7b4d509SDean Nelson 	start_addr = ((u64)skb->data & ~(L1_CACHE_BYTES - 1));
446a7b4d509SDean Nelson 	end_addr = L1_CACHE_ALIGN((u64)skb_tail_pointer(skb));
447a7b4d509SDean Nelson 
448a7b4d509SDean Nelson 	/* calculate how many bytes to embed in the XPC message */
449a7b4d509SDean Nelson 	if (unlikely(skb->len <= XPNET_MSG_DATA_MAX)) {
450a7b4d509SDean Nelson 		/* skb->data does fit so embed */
451a7b4d509SDean Nelson 		embedded_bytes = skb->len;
452a7b4d509SDean Nelson 	}
453a7b4d509SDean Nelson 
454a7b4d509SDean Nelson 	/*
455a7b4d509SDean Nelson 	 * Since the send occurs asynchronously, we set the count to one
456a7b4d509SDean Nelson 	 * and begin sending.  Any sends that happen to complete before
457a7b4d509SDean Nelson 	 * we are done sending will not free the skb.  We will be left
458a7b4d509SDean Nelson 	 * with that task during exit.  This also handles the case of
459a7b4d509SDean Nelson 	 * a packet destined for a partition which is no longer up.
460a7b4d509SDean Nelson 	 */
461a7b4d509SDean Nelson 	atomic_set(&queued_msg->use_count, 1);
462a7b4d509SDean Nelson 	queued_msg->skb = skb;
463a7b4d509SDean Nelson 
464a7b4d509SDean Nelson 	if (skb->data[0] == 0xff) {
465a7b4d509SDean Nelson 		/* we are being asked to broadcast to all partitions */
466984b3f57SAkinobu Mita 		for_each_set_bit(dest_partid, xpnet_broadcast_partitions,
467a7b4d509SDean Nelson 			     xp_max_npartitions) {
468a7b4d509SDean Nelson 
469a7b4d509SDean Nelson 			xpnet_send(skb, queued_msg, start_addr, end_addr,
470a7b4d509SDean Nelson 				   embedded_bytes, dest_partid);
471a7b4d509SDean Nelson 		}
472a7b4d509SDean Nelson 	} else {
473a7b4d509SDean Nelson 		dest_partid = (short)skb->data[XPNET_PARTID_OCTET + 1];
474a7b4d509SDean Nelson 		dest_partid |= (short)skb->data[XPNET_PARTID_OCTET + 0] << 8;
475a7b4d509SDean Nelson 
476a7b4d509SDean Nelson 		if (dest_partid >= 0 &&
477a7b4d509SDean Nelson 		    dest_partid < xp_max_npartitions &&
478a7b4d509SDean Nelson 		    test_bit(dest_partid, xpnet_broadcast_partitions) != 0) {
479a7b4d509SDean Nelson 
480a7b4d509SDean Nelson 			xpnet_send(skb, queued_msg, start_addr, end_addr,
481a7b4d509SDean Nelson 				   embedded_bytes, dest_partid);
48245d9ca49SDean Nelson 		}
48345d9ca49SDean Nelson 	}
48445d9ca49SDean Nelson 
48544da29d2SEric Dumazet 	dev->stats.tx_packets++;
48644da29d2SEric Dumazet 	dev->stats.tx_bytes += skb->len;
48744da29d2SEric Dumazet 
48845d9ca49SDean Nelson 	if (atomic_dec_return(&queued_msg->use_count) == 0) {
48945d9ca49SDean Nelson 		dev_kfree_skb(skb);
49045d9ca49SDean Nelson 		kfree(queued_msg);
49145d9ca49SDean Nelson 	}
49245d9ca49SDean Nelson 
4936ed10654SPatrick McHardy 	return NETDEV_TX_OK;
49445d9ca49SDean Nelson }
49545d9ca49SDean Nelson 
49645d9ca49SDean Nelson /*
49745d9ca49SDean Nelson  * Deal with transmit timeouts coming from the network layer.
49845d9ca49SDean Nelson  */
49945d9ca49SDean Nelson static void
xpnet_dev_tx_timeout(struct net_device * dev,unsigned int txqueue)5000290bd29SMichael S. Tsirkin xpnet_dev_tx_timeout(struct net_device *dev, unsigned int txqueue)
50145d9ca49SDean Nelson {
502e8ac9c55SStephen Hemminger 	dev->stats.tx_errors++;
50345d9ca49SDean Nelson }
50445d9ca49SDean Nelson 
505e8ac9c55SStephen Hemminger static const struct net_device_ops xpnet_netdev_ops = {
506e8ac9c55SStephen Hemminger 	.ndo_open		= xpnet_dev_open,
507e8ac9c55SStephen Hemminger 	.ndo_stop		= xpnet_dev_stop,
508e8ac9c55SStephen Hemminger 	.ndo_start_xmit		= xpnet_dev_hard_start_xmit,
509e8ac9c55SStephen Hemminger 	.ndo_tx_timeout		= xpnet_dev_tx_timeout,
510e8ac9c55SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
511e8ac9c55SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
512e8ac9c55SStephen Hemminger };
513e8ac9c55SStephen Hemminger 
51445d9ca49SDean Nelson static int __init
xpnet_init(void)51545d9ca49SDean Nelson xpnet_init(void)
51645d9ca49SDean Nelson {
51740d5cb40SJakub Kicinski 	u8 addr[ETH_ALEN];
518a7b4d509SDean Nelson 	int result;
51945d9ca49SDean Nelson 
520788b66e3SMike Travis 	if (!is_uv_system())
52145d9ca49SDean Nelson 		return -ENODEV;
52245d9ca49SDean Nelson 
52345d9ca49SDean Nelson 	dev_info(xpnet, "registering network device %s\n", XPNET_DEVICE_NAME);
52445d9ca49SDean Nelson 
525*64c59166SChristophe JAILLET 	xpnet_broadcast_partitions = bitmap_zalloc(xp_max_npartitions,
5266396bb22SKees Cook 						   GFP_KERNEL);
527a7b4d509SDean Nelson 	if (xpnet_broadcast_partitions == NULL)
528a7b4d509SDean Nelson 		return -ENOMEM;
529a7b4d509SDean Nelson 
53045d9ca49SDean Nelson 	/*
53145d9ca49SDean Nelson 	 * use ether_setup() to init the majority of our device
53245d9ca49SDean Nelson 	 * structure and then override the necessary pieces.
53345d9ca49SDean Nelson 	 */
534c835a677STom Gundersen 	xpnet_device = alloc_netdev(0, XPNET_DEVICE_NAME, NET_NAME_UNKNOWN,
535c835a677STom Gundersen 				    ether_setup);
536a7b4d509SDean Nelson 	if (xpnet_device == NULL) {
537*64c59166SChristophe JAILLET 		bitmap_free(xpnet_broadcast_partitions);
53845d9ca49SDean Nelson 		return -ENOMEM;
539a7b4d509SDean Nelson 	}
54045d9ca49SDean Nelson 
54145d9ca49SDean Nelson 	netif_carrier_off(xpnet_device);
54245d9ca49SDean Nelson 
543714c48f1SDean Nelson 	xpnet_device->netdev_ops = &xpnet_netdev_ops;
54445d9ca49SDean Nelson 	xpnet_device->mtu = XPNET_DEF_MTU;
545b3e3893eSJarod Wilson 	xpnet_device->min_mtu = XPNET_MIN_MTU;
546b3e3893eSJarod Wilson 	xpnet_device->max_mtu = XPNET_MAX_MTU;
54745d9ca49SDean Nelson 
54840d5cb40SJakub Kicinski 	memset(addr, 0, sizeof(addr));
54945d9ca49SDean Nelson 	/*
55045d9ca49SDean Nelson 	 * Multicast assumes the LSB of the first octet is set for multicast
55145d9ca49SDean Nelson 	 * MAC addresses.  We chose the first octet of the MAC to be unlikely
55245d9ca49SDean Nelson 	 * to collide with any vendor's officially issued MAC.
55345d9ca49SDean Nelson 	 */
55440d5cb40SJakub Kicinski 	addr[0] = 0x02;     /* locally administered, no OUI */
555a7b4d509SDean Nelson 
55640d5cb40SJakub Kicinski 	addr[XPNET_PARTID_OCTET + 1] = xp_partition_id;
55740d5cb40SJakub Kicinski 	addr[XPNET_PARTID_OCTET + 0] = (xp_partition_id >> 8);
55840d5cb40SJakub Kicinski 	eth_hw_addr_set(xpnet_device, addr);
55945d9ca49SDean Nelson 
56045d9ca49SDean Nelson 	/*
56145d9ca49SDean Nelson 	 * ether_setup() sets this to a multicast device.  We are
56245d9ca49SDean Nelson 	 * really not supporting multicast at this time.
56345d9ca49SDean Nelson 	 */
56445d9ca49SDean Nelson 	xpnet_device->flags &= ~IFF_MULTICAST;
56545d9ca49SDean Nelson 
56645d9ca49SDean Nelson 	/*
56745d9ca49SDean Nelson 	 * No need to checksum as it is a DMA transfer.  The BTE will
56845d9ca49SDean Nelson 	 * report an error if the data is not retrievable and the
56945d9ca49SDean Nelson 	 * packet will be dropped.
57045d9ca49SDean Nelson 	 */
57134324dc2SMichał Mirosław 	xpnet_device->features = NETIF_F_HW_CSUM;
57245d9ca49SDean Nelson 
57345d9ca49SDean Nelson 	result = register_netdev(xpnet_device);
574a7b4d509SDean Nelson 	if (result != 0) {
57545d9ca49SDean Nelson 		free_netdev(xpnet_device);
576*64c59166SChristophe JAILLET 		bitmap_free(xpnet_broadcast_partitions);
577a7b4d509SDean Nelson 	}
57845d9ca49SDean Nelson 
57945d9ca49SDean Nelson 	return result;
58045d9ca49SDean Nelson }
58145d9ca49SDean Nelson 
58235190506SDean Nelson module_init(xpnet_init);
58345d9ca49SDean Nelson 
58445d9ca49SDean Nelson static void __exit
xpnet_exit(void)58545d9ca49SDean Nelson xpnet_exit(void)
58645d9ca49SDean Nelson {
58745d9ca49SDean Nelson 	dev_info(xpnet, "unregistering network device %s\n",
58845d9ca49SDean Nelson 		 xpnet_device[0].name);
58945d9ca49SDean Nelson 
59045d9ca49SDean Nelson 	unregister_netdev(xpnet_device);
59145d9ca49SDean Nelson 	free_netdev(xpnet_device);
592*64c59166SChristophe JAILLET 	bitmap_free(xpnet_broadcast_partitions);
59345d9ca49SDean Nelson }
59445d9ca49SDean Nelson 
59535190506SDean Nelson module_exit(xpnet_exit);
59645d9ca49SDean Nelson 
59745d9ca49SDean Nelson MODULE_AUTHOR("Silicon Graphics, Inc.");
59845d9ca49SDean Nelson MODULE_DESCRIPTION("Cross Partition Network adapter (XPNET)");
59945d9ca49SDean Nelson MODULE_LICENSE("GPL");
600