xref: /openbmc/linux/drivers/misc/sgi-xp/xpc_main.c (revision b085dbf6)
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
7a374c57bSRobin Holt  * Copyright (c) 2004-2009 Silicon Graphics, Inc.  All Rights Reserved.
845d9ca49SDean Nelson  */
945d9ca49SDean Nelson 
1045d9ca49SDean Nelson /*
1145d9ca49SDean Nelson  * Cross Partition Communication (XPC) support - standard version.
1245d9ca49SDean Nelson  *
1345d9ca49SDean Nelson  *	XPC provides a message passing capability that crosses partition
1445d9ca49SDean Nelson  *	boundaries. This module is made up of two parts:
1545d9ca49SDean Nelson  *
1645d9ca49SDean Nelson  *	    partition	This part detects the presence/absence of other
1745d9ca49SDean Nelson  *			partitions. It provides a heartbeat and monitors
1845d9ca49SDean Nelson  *			the heartbeats of other partitions.
1945d9ca49SDean Nelson  *
2045d9ca49SDean Nelson  *	    channel	This part manages the channels and sends/receives
2145d9ca49SDean Nelson  *			messages across them to/from other partitions.
2245d9ca49SDean Nelson  *
2345d9ca49SDean Nelson  *	There are a couple of additional functions residing in XP, which
2445d9ca49SDean Nelson  *	provide an interface to XPC for its users.
2545d9ca49SDean Nelson  *
2645d9ca49SDean Nelson  *
2745d9ca49SDean Nelson  *	Caveats:
2845d9ca49SDean Nelson  *
297fb5e59dSDean Nelson  *	  . Currently on sn2, we have no way to determine which nasid an IRQ
30c39838ceSDean Nelson  *	    came from. Thus, xpc_send_IRQ_sn2() does a remote amo write
31c39838ceSDean Nelson  *	    followed by an IPI. The amo indicates where data is to be pulled
32c39838ceSDean Nelson  *	    from, so after the IPI arrives, the remote partition checks the amo
33c39838ceSDean Nelson  *	    word. The IPI can actually arrive before the amo however, so other
34c39838ceSDean Nelson  *	    code must periodically check for this case. Also, remote amo
357fb5e59dSDean Nelson  *	    operations do not reliably time out. Thus we do a remote PIO read
367fb5e59dSDean Nelson  *	    solely to know whether the remote partition is down and whether we
377fb5e59dSDean Nelson  *	    should stop sending IPIs to it. This remote PIO read operation is
387fb5e59dSDean Nelson  *	    set up in a special nofault region so SAL knows to ignore (and
39c39838ceSDean Nelson  *	    cleanup) any errors due to the remote amo write, PIO read, and/or
407fb5e59dSDean Nelson  *	    PIO write operations.
4145d9ca49SDean Nelson  *
4245d9ca49SDean Nelson  *	    If/when new hardware solves this IPI problem, we should abandon
4345d9ca49SDean Nelson  *	    the current approach.
4445d9ca49SDean Nelson  *
4545d9ca49SDean Nelson  */
4645d9ca49SDean Nelson 
4745d9ca49SDean Nelson #include <linux/module.h>
485a0e3ad6STejun Heo #include <linux/slab.h>
49261f3b49SDean Nelson #include <linux/sysctl.h>
50261f3b49SDean Nelson #include <linux/device.h>
5145d9ca49SDean Nelson #include <linux/delay.h>
5245d9ca49SDean Nelson #include <linux/reboot.h>
5345d9ca49SDean Nelson #include <linux/kdebug.h>
542c2b94f9SDean Nelson #include <linux/kthread.h>
5545d9ca49SDean Nelson #include "xpc.h"
5645d9ca49SDean Nelson 
57891348caSRobin Holt #ifdef CONFIG_X86_64
58891348caSRobin Holt #include <asm/traps.h>
59891348caSRobin Holt #endif
60891348caSRobin Holt 
6145d9ca49SDean Nelson /* define two XPC debug device structures to be used with dev_dbg() et al */
6245d9ca49SDean Nelson 
6319df2f8eSJason Yan static struct device_driver xpc_dbg_name = {
6445d9ca49SDean Nelson 	.name = "xpc"
6545d9ca49SDean Nelson };
6645d9ca49SDean Nelson 
6719df2f8eSJason Yan static struct device xpc_part_dbg_subname = {
68bb0dc43eSKay Sievers 	.init_name = "",	/* set to "part" at xpc_init() time */
6945d9ca49SDean Nelson 	.driver = &xpc_dbg_name
7045d9ca49SDean Nelson };
7145d9ca49SDean Nelson 
7219df2f8eSJason Yan static struct device xpc_chan_dbg_subname = {
73bb0dc43eSKay Sievers 	.init_name = "",	/* set to "chan" at xpc_init() time */
7445d9ca49SDean Nelson 	.driver = &xpc_dbg_name
7545d9ca49SDean Nelson };
7645d9ca49SDean Nelson 
7745d9ca49SDean Nelson struct device *xpc_part = &xpc_part_dbg_subname;
7845d9ca49SDean Nelson struct device *xpc_chan = &xpc_chan_dbg_subname;
7945d9ca49SDean Nelson 
8045d9ca49SDean Nelson static int xpc_kdebug_ignore;
8145d9ca49SDean Nelson 
8245d9ca49SDean Nelson /* systune related variables for /proc/sys directories */
8345d9ca49SDean Nelson 
8445d9ca49SDean Nelson static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
8545d9ca49SDean Nelson static int xpc_hb_min_interval = 1;
8645d9ca49SDean Nelson static int xpc_hb_max_interval = 10;
8745d9ca49SDean Nelson 
8845d9ca49SDean Nelson static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
8945d9ca49SDean Nelson static int xpc_hb_check_min_interval = 10;
9045d9ca49SDean Nelson static int xpc_hb_check_max_interval = 120;
9145d9ca49SDean Nelson 
92a47d5dacSDean Nelson int xpc_disengage_timelimit = XPC_DISENGAGE_DEFAULT_TIMELIMIT;
93a47d5dacSDean Nelson static int xpc_disengage_min_timelimit;	/* = 0 */
94a47d5dacSDean Nelson static int xpc_disengage_max_timelimit = 120;
9545d9ca49SDean Nelson 
96*b085dbf6SLuis Chamberlain static struct ctl_table xpc_sys_xpc_hb[] = {
9745d9ca49SDean Nelson 	{
9845d9ca49SDean Nelson 	 .procname = "hb_interval",
9945d9ca49SDean Nelson 	 .data = &xpc_hb_interval,
10045d9ca49SDean Nelson 	 .maxlen = sizeof(int),
10145d9ca49SDean Nelson 	 .mode = 0644,
1026d456111SEric W. Biederman 	 .proc_handler = proc_dointvec_minmax,
10345d9ca49SDean Nelson 	 .extra1 = &xpc_hb_min_interval,
10435190506SDean Nelson 	 .extra2 = &xpc_hb_max_interval},
10545d9ca49SDean Nelson 	{
10645d9ca49SDean Nelson 	 .procname = "hb_check_interval",
10745d9ca49SDean Nelson 	 .data = &xpc_hb_check_interval,
10845d9ca49SDean Nelson 	 .maxlen = sizeof(int),
10945d9ca49SDean Nelson 	 .mode = 0644,
1106d456111SEric W. Biederman 	 .proc_handler = proc_dointvec_minmax,
11145d9ca49SDean Nelson 	 .extra1 = &xpc_hb_check_min_interval,
11235190506SDean Nelson 	 .extra2 = &xpc_hb_check_max_interval},
11345d9ca49SDean Nelson 	{}
11445d9ca49SDean Nelson };
115*b085dbf6SLuis Chamberlain static struct ctl_table xpc_sys_xpc[] = {
11645d9ca49SDean Nelson 	{
117a47d5dacSDean Nelson 	 .procname = "disengage_timelimit",
118a47d5dacSDean Nelson 	 .data = &xpc_disengage_timelimit,
11945d9ca49SDean Nelson 	 .maxlen = sizeof(int),
12045d9ca49SDean Nelson 	 .mode = 0644,
1216d456111SEric W. Biederman 	 .proc_handler = proc_dointvec_minmax,
122a47d5dacSDean Nelson 	 .extra1 = &xpc_disengage_min_timelimit,
123a47d5dacSDean Nelson 	 .extra2 = &xpc_disengage_max_timelimit},
12445d9ca49SDean Nelson 	{}
12545d9ca49SDean Nelson };
126*b085dbf6SLuis Chamberlain 
12745d9ca49SDean Nelson static struct ctl_table_header *xpc_sysctl;
128*b085dbf6SLuis Chamberlain static struct ctl_table_header *xpc_sysctl_hb;
12945d9ca49SDean Nelson 
130a47d5dacSDean Nelson /* non-zero if any remote partition disengage was timed out */
131a47d5dacSDean Nelson int xpc_disengage_timedout;
13245d9ca49SDean Nelson 
1335b8669dfSDean Nelson /* #of activate IRQs received and not yet processed */
1345b8669dfSDean Nelson int xpc_activate_IRQ_rcvd;
1355b8669dfSDean Nelson DEFINE_SPINLOCK(xpc_activate_IRQ_rcvd_lock);
13645d9ca49SDean Nelson 
13745d9ca49SDean Nelson /* IRQ handler notifies this wait queue on receipt of an IRQ */
1386e41017aSDean Nelson DECLARE_WAIT_QUEUE_HEAD(xpc_activate_IRQ_wq);
13945d9ca49SDean Nelson 
14045d9ca49SDean Nelson static unsigned long xpc_hb_check_timeout;
14133ba3c77SDean Nelson static struct timer_list xpc_hb_timer;
14245d9ca49SDean Nelson 
14345d9ca49SDean Nelson /* notification that the xpc_hb_checker thread has exited */
14445d9ca49SDean Nelson static DECLARE_COMPLETION(xpc_hb_checker_exited);
14545d9ca49SDean Nelson 
14645d9ca49SDean Nelson /* notification that the xpc_discovery thread has exited */
14745d9ca49SDean Nelson static DECLARE_COMPLETION(xpc_discovery_exited);
14845d9ca49SDean Nelson 
14945d9ca49SDean Nelson static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
15045d9ca49SDean Nelson 
15145d9ca49SDean Nelson static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
15245d9ca49SDean Nelson static struct notifier_block xpc_reboot_notifier = {
15345d9ca49SDean Nelson 	.notifier_call = xpc_system_reboot,
15445d9ca49SDean Nelson };
15545d9ca49SDean Nelson 
15645d9ca49SDean Nelson static int xpc_system_die(struct notifier_block *, unsigned long, void *);
15745d9ca49SDean Nelson static struct notifier_block xpc_die_notifier = {
15845d9ca49SDean Nelson 	.notifier_call = xpc_system_die,
15945d9ca49SDean Nelson };
16045d9ca49SDean Nelson 
161a7665b0aSRobin Holt struct xpc_arch_operations xpc_arch_ops;
16294bd2708SDean Nelson 
16345d9ca49SDean Nelson /*
164a47d5dacSDean Nelson  * Timer function to enforce the timelimit on the partition disengage.
16545d9ca49SDean Nelson  */
16645d9ca49SDean Nelson static void
xpc_timeout_partition_disengage(struct timer_list * t)16725b42fa8SKees Cook xpc_timeout_partition_disengage(struct timer_list *t)
16845d9ca49SDean Nelson {
16925b42fa8SKees Cook 	struct xpc_partition *part = from_timer(part, t, disengage_timer);
17045d9ca49SDean Nelson 
171a47d5dacSDean Nelson 	DBUG_ON(time_is_after_jiffies(part->disengage_timeout));
17245d9ca49SDean Nelson 
173997754f1SThomas Gleixner 	xpc_partition_disengaged_from_timer(part);
17445d9ca49SDean Nelson 
175a47d5dacSDean Nelson 	DBUG_ON(part->disengage_timeout != 0);
176a7665b0aSRobin Holt 	DBUG_ON(xpc_arch_ops.partition_engaged(XPC_PARTID(part)));
17745d9ca49SDean Nelson }
17845d9ca49SDean Nelson 
17945d9ca49SDean Nelson /*
18045d9ca49SDean Nelson  * Timer to produce the heartbeat.  The timer structures function is
18145d9ca49SDean Nelson  * already set when this is initially called.  A tunable is used to
18245d9ca49SDean Nelson  * specify when the next timeout should occur.
18345d9ca49SDean Nelson  */
18445d9ca49SDean Nelson static void
xpc_hb_beater(struct timer_list * unused)18525b42fa8SKees Cook xpc_hb_beater(struct timer_list *unused)
18645d9ca49SDean Nelson {
187a7665b0aSRobin Holt 	xpc_arch_ops.increment_heartbeat();
18845d9ca49SDean Nelson 
189aaa3cd69SDean Nelson 	if (time_is_before_eq_jiffies(xpc_hb_check_timeout))
1906e41017aSDean Nelson 		wake_up_interruptible(&xpc_activate_IRQ_wq);
19145d9ca49SDean Nelson 
19245d9ca49SDean Nelson 	xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
19345d9ca49SDean Nelson 	add_timer(&xpc_hb_timer);
19445d9ca49SDean Nelson }
19545d9ca49SDean Nelson 
19633ba3c77SDean Nelson static void
xpc_start_hb_beater(void)19733ba3c77SDean Nelson xpc_start_hb_beater(void)
19833ba3c77SDean Nelson {
199a7665b0aSRobin Holt 	xpc_arch_ops.heartbeat_init();
20025b42fa8SKees Cook 	timer_setup(&xpc_hb_timer, xpc_hb_beater, 0);
201ac41ae0bSYang Li 	xpc_hb_beater(NULL);
20233ba3c77SDean Nelson }
20333ba3c77SDean Nelson 
20433ba3c77SDean Nelson static void
xpc_stop_hb_beater(void)20533ba3c77SDean Nelson xpc_stop_hb_beater(void)
20633ba3c77SDean Nelson {
20733ba3c77SDean Nelson 	del_timer_sync(&xpc_hb_timer);
208a7665b0aSRobin Holt 	xpc_arch_ops.heartbeat_exit();
20933ba3c77SDean Nelson }
21033ba3c77SDean Nelson 
21145d9ca49SDean Nelson /*
21261deb86eSDean Nelson  * At periodic intervals, scan through all active partitions and ensure
21361deb86eSDean Nelson  * their heartbeat is still active.  If not, the partition is deactivated.
21461deb86eSDean Nelson  */
21561deb86eSDean Nelson static void
xpc_check_remote_hb(void)21661deb86eSDean Nelson xpc_check_remote_hb(void)
21761deb86eSDean Nelson {
21861deb86eSDean Nelson 	struct xpc_partition *part;
21961deb86eSDean Nelson 	short partid;
22061deb86eSDean Nelson 	enum xp_retval ret;
22161deb86eSDean Nelson 
22261deb86eSDean Nelson 	for (partid = 0; partid < xp_max_npartitions; partid++) {
22361deb86eSDean Nelson 
22461deb86eSDean Nelson 		if (xpc_exiting)
22561deb86eSDean Nelson 			break;
22661deb86eSDean Nelson 
22761deb86eSDean Nelson 		if (partid == xp_partition_id)
22861deb86eSDean Nelson 			continue;
22961deb86eSDean Nelson 
23061deb86eSDean Nelson 		part = &xpc_partitions[partid];
23161deb86eSDean Nelson 
23283469b55SDean Nelson 		if (part->act_state == XPC_P_AS_INACTIVE ||
23383469b55SDean Nelson 		    part->act_state == XPC_P_AS_DEACTIVATING) {
23461deb86eSDean Nelson 			continue;
23561deb86eSDean Nelson 		}
23661deb86eSDean Nelson 
237a7665b0aSRobin Holt 		ret = xpc_arch_ops.get_remote_heartbeat(part);
23861deb86eSDean Nelson 		if (ret != xpSuccess)
23961deb86eSDean Nelson 			XPC_DEACTIVATE_PARTITION(part, ret);
24061deb86eSDean Nelson 	}
24161deb86eSDean Nelson }
24261deb86eSDean Nelson 
24361deb86eSDean Nelson /*
24445d9ca49SDean Nelson  * This thread is responsible for nearly all of the partition
24545d9ca49SDean Nelson  * activation/deactivation.
24645d9ca49SDean Nelson  */
24745d9ca49SDean Nelson static int
xpc_hb_checker(void * ignore)24845d9ca49SDean Nelson xpc_hb_checker(void *ignore)
24945d9ca49SDean Nelson {
25045d9ca49SDean Nelson 	int force_IRQ = 0;
25145d9ca49SDean Nelson 
25245d9ca49SDean Nelson 	/* this thread was marked active by xpc_hb_init() */
25345d9ca49SDean Nelson 
254f7df8ed1SRusty Russell 	set_cpus_allowed_ptr(current, cpumask_of(XPC_HB_CHECK_CPU));
25545d9ca49SDean Nelson 
25645d9ca49SDean Nelson 	/* set our heartbeating to other partitions into motion */
25745d9ca49SDean Nelson 	xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
25833ba3c77SDean Nelson 	xpc_start_hb_beater();
25945d9ca49SDean Nelson 
2602c2b94f9SDean Nelson 	while (!xpc_exiting) {
26145d9ca49SDean Nelson 
26245d9ca49SDean Nelson 		dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
26345d9ca49SDean Nelson 			"been received\n",
26445d9ca49SDean Nelson 			(int)(xpc_hb_check_timeout - jiffies),
2655b8669dfSDean Nelson 			xpc_activate_IRQ_rcvd);
26645d9ca49SDean Nelson 
26745d9ca49SDean Nelson 		/* checking of remote heartbeats is skewed by IRQ handling */
268aaa3cd69SDean Nelson 		if (time_is_before_eq_jiffies(xpc_hb_check_timeout)) {
2695b8669dfSDean Nelson 			xpc_hb_check_timeout = jiffies +
2705b8669dfSDean Nelson 			    (xpc_hb_check_interval * HZ);
2715b8669dfSDean Nelson 
27245d9ca49SDean Nelson 			dev_dbg(xpc_part, "checking remote heartbeats\n");
27345d9ca49SDean Nelson 			xpc_check_remote_hb();
27445d9ca49SDean Nelson 		}
27545d9ca49SDean Nelson 
27645d9ca49SDean Nelson 		/* check for outstanding IRQs */
2775b8669dfSDean Nelson 		if (xpc_activate_IRQ_rcvd > 0 || force_IRQ != 0) {
27845d9ca49SDean Nelson 			force_IRQ = 0;
2795b8669dfSDean Nelson 			dev_dbg(xpc_part, "processing activate IRQs "
2805b8669dfSDean Nelson 				"received\n");
281a7665b0aSRobin Holt 			xpc_arch_ops.process_activate_IRQ_rcvd();
28245d9ca49SDean Nelson 		}
28345d9ca49SDean Nelson 
28445d9ca49SDean Nelson 		/* wait for IRQ or timeout */
2856e41017aSDean Nelson 		(void)wait_event_interruptible(xpc_activate_IRQ_wq,
2865b8669dfSDean Nelson 					       (time_is_before_eq_jiffies(
28735190506SDean Nelson 						xpc_hb_check_timeout) ||
2885b8669dfSDean Nelson 						xpc_activate_IRQ_rcvd > 0 ||
2892c2b94f9SDean Nelson 						xpc_exiting));
29045d9ca49SDean Nelson 	}
29145d9ca49SDean Nelson 
29233ba3c77SDean Nelson 	xpc_stop_hb_beater();
29333ba3c77SDean Nelson 
29445d9ca49SDean Nelson 	dev_dbg(xpc_part, "heartbeat checker is exiting\n");
29545d9ca49SDean Nelson 
29645d9ca49SDean Nelson 	/* mark this thread as having exited */
29745d9ca49SDean Nelson 	complete(&xpc_hb_checker_exited);
29845d9ca49SDean Nelson 	return 0;
29945d9ca49SDean Nelson }
30045d9ca49SDean Nelson 
30145d9ca49SDean Nelson /*
30245d9ca49SDean Nelson  * This thread will attempt to discover other partitions to activate
30345d9ca49SDean Nelson  * based on info provided by SAL. This new thread is short lived and
30445d9ca49SDean Nelson  * will exit once discovery is complete.
30545d9ca49SDean Nelson  */
30645d9ca49SDean Nelson static int
xpc_initiate_discovery(void * ignore)30745d9ca49SDean Nelson xpc_initiate_discovery(void *ignore)
30845d9ca49SDean Nelson {
30945d9ca49SDean Nelson 	xpc_discovery();
31045d9ca49SDean Nelson 
31145d9ca49SDean Nelson 	dev_dbg(xpc_part, "discovery thread is exiting\n");
31245d9ca49SDean Nelson 
31345d9ca49SDean Nelson 	/* mark this thread as having exited */
31445d9ca49SDean Nelson 	complete(&xpc_discovery_exited);
31545d9ca49SDean Nelson 	return 0;
31645d9ca49SDean Nelson }
31745d9ca49SDean Nelson 
31845d9ca49SDean Nelson /*
31945d9ca49SDean Nelson  * The first kthread assigned to a newly activated partition is the one
320e17d416bSDean Nelson  * created by XPC HB with which it calls xpc_activating(). XPC hangs on to
32145d9ca49SDean Nelson  * that kthread until the partition is brought down, at which time that kthread
32245d9ca49SDean Nelson  * returns back to XPC HB. (The return of that kthread will signify to XPC HB
32345d9ca49SDean Nelson  * that XPC has dismantled all communication infrastructure for the associated
32445d9ca49SDean Nelson  * partition.) This kthread becomes the channel manager for that partition.
32545d9ca49SDean Nelson  *
32645d9ca49SDean Nelson  * Each active partition has a channel manager, who, besides connecting and
32745d9ca49SDean Nelson  * disconnecting channels, will ensure that each of the partition's connected
32845d9ca49SDean Nelson  * channels has the required number of assigned kthreads to get the work done.
32945d9ca49SDean Nelson  */
33045d9ca49SDean Nelson static void
xpc_channel_mgr(struct xpc_partition * part)33145d9ca49SDean Nelson xpc_channel_mgr(struct xpc_partition *part)
33245d9ca49SDean Nelson {
33383469b55SDean Nelson 	while (part->act_state != XPC_P_AS_DEACTIVATING ||
33445d9ca49SDean Nelson 	       atomic_read(&part->nchannels_active) > 0 ||
33545d9ca49SDean Nelson 	       !xpc_partition_disengaged(part)) {
33645d9ca49SDean Nelson 
3377fb5e59dSDean Nelson 		xpc_process_sent_chctl_flags(part);
33845d9ca49SDean Nelson 
33945d9ca49SDean Nelson 		/*
34045d9ca49SDean Nelson 		 * Wait until we've been requested to activate kthreads or
34145d9ca49SDean Nelson 		 * all of the channel's message queues have been torn down or
34245d9ca49SDean Nelson 		 * a signal is pending.
34345d9ca49SDean Nelson 		 *
34445d9ca49SDean Nelson 		 * The channel_mgr_requests is set to 1 after being awakened,
34545d9ca49SDean Nelson 		 * This is done to prevent the channel mgr from making one pass
34645d9ca49SDean Nelson 		 * through the loop for each request, since he will
34745d9ca49SDean Nelson 		 * be servicing all the requests in one pass. The reason it's
34845d9ca49SDean Nelson 		 * set to 1 instead of 0 is so that other kthreads will know
34945d9ca49SDean Nelson 		 * that the channel mgr is running and won't bother trying to
35045d9ca49SDean Nelson 		 * wake him up.
35145d9ca49SDean Nelson 		 */
35245d9ca49SDean Nelson 		atomic_dec(&part->channel_mgr_requests);
35345d9ca49SDean Nelson 		(void)wait_event_interruptible(part->channel_mgr_wq,
3542c2b94f9SDean Nelson 				(atomic_read(&part->channel_mgr_requests) > 0 ||
3557fb5e59dSDean Nelson 				 part->chctl.all_flags != 0 ||
35683469b55SDean Nelson 				 (part->act_state == XPC_P_AS_DEACTIVATING &&
3572c2b94f9SDean Nelson 				 atomic_read(&part->nchannels_active) == 0 &&
3582c2b94f9SDean Nelson 				 xpc_partition_disengaged(part))));
35945d9ca49SDean Nelson 		atomic_set(&part->channel_mgr_requests, 1);
36045d9ca49SDean Nelson 	}
36145d9ca49SDean Nelson }
36245d9ca49SDean Nelson 
36345d9ca49SDean Nelson /*
3645b8669dfSDean Nelson  * Guarantee that the kzalloc'd memory is cacheline aligned.
3655b8669dfSDean Nelson  */
3665b8669dfSDean Nelson void *
xpc_kzalloc_cacheline_aligned(size_t size,gfp_t flags,void ** base)3675b8669dfSDean Nelson xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
3685b8669dfSDean Nelson {
3695b8669dfSDean Nelson 	/* see if kzalloc will give us cachline aligned memory by default */
3705b8669dfSDean Nelson 	*base = kzalloc(size, flags);
3715b8669dfSDean Nelson 	if (*base == NULL)
3725b8669dfSDean Nelson 		return NULL;
3735b8669dfSDean Nelson 
3745b8669dfSDean Nelson 	if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
3755b8669dfSDean Nelson 		return *base;
3765b8669dfSDean Nelson 
3775b8669dfSDean Nelson 	kfree(*base);
3785b8669dfSDean Nelson 
3795b8669dfSDean Nelson 	/* nope, we'll have to do it ourselves */
3805b8669dfSDean Nelson 	*base = kzalloc(size + L1_CACHE_BYTES, flags);
3815b8669dfSDean Nelson 	if (*base == NULL)
3825b8669dfSDean Nelson 		return NULL;
3835b8669dfSDean Nelson 
3845b8669dfSDean Nelson 	return (void *)L1_CACHE_ALIGN((u64)*base);
3855b8669dfSDean Nelson }
3865b8669dfSDean Nelson 
3875b8669dfSDean Nelson /*
3885b8669dfSDean Nelson  * Setup the channel structures necessary to support XPartition Communication
3895b8669dfSDean Nelson  * between the specified remote partition and the local one.
3905b8669dfSDean Nelson  */
3915b8669dfSDean Nelson static enum xp_retval
xpc_setup_ch_structures(struct xpc_partition * part)3925b8669dfSDean Nelson xpc_setup_ch_structures(struct xpc_partition *part)
3935b8669dfSDean Nelson {
3945b8669dfSDean Nelson 	enum xp_retval ret;
3955b8669dfSDean Nelson 	int ch_number;
3965b8669dfSDean Nelson 	struct xpc_channel *ch;
3975b8669dfSDean Nelson 	short partid = XPC_PARTID(part);
3985b8669dfSDean Nelson 
3995b8669dfSDean Nelson 	/*
4005b8669dfSDean Nelson 	 * Allocate all of the channel structures as a contiguous chunk of
4015b8669dfSDean Nelson 	 * memory.
4025b8669dfSDean Nelson 	 */
4035b8669dfSDean Nelson 	DBUG_ON(part->channels != NULL);
4046396bb22SKees Cook 	part->channels = kcalloc(XPC_MAX_NCHANNELS,
4056396bb22SKees Cook 				 sizeof(struct xpc_channel),
4065b8669dfSDean Nelson 				 GFP_KERNEL);
4075b8669dfSDean Nelson 	if (part->channels == NULL) {
4085b8669dfSDean Nelson 		dev_err(xpc_chan, "can't get memory for channels\n");
4095b8669dfSDean Nelson 		return xpNoMemory;
4105b8669dfSDean Nelson 	}
4115b8669dfSDean Nelson 
4125b8669dfSDean Nelson 	/* allocate the remote open and close args */
4135b8669dfSDean Nelson 
4145b8669dfSDean Nelson 	part->remote_openclose_args =
4155b8669dfSDean Nelson 	    xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE,
4165b8669dfSDean Nelson 					  GFP_KERNEL, &part->
4175b8669dfSDean Nelson 					  remote_openclose_args_base);
4185b8669dfSDean Nelson 	if (part->remote_openclose_args == NULL) {
4195b8669dfSDean Nelson 		dev_err(xpc_chan, "can't get memory for remote connect args\n");
4205b8669dfSDean Nelson 		ret = xpNoMemory;
4215b8669dfSDean Nelson 		goto out_1;
4225b8669dfSDean Nelson 	}
4235b8669dfSDean Nelson 
4245b8669dfSDean Nelson 	part->chctl.all_flags = 0;
4255b8669dfSDean Nelson 	spin_lock_init(&part->chctl_lock);
4265b8669dfSDean Nelson 
4275b8669dfSDean Nelson 	atomic_set(&part->channel_mgr_requests, 1);
4285b8669dfSDean Nelson 	init_waitqueue_head(&part->channel_mgr_wq);
4295b8669dfSDean Nelson 
4305b8669dfSDean Nelson 	part->nchannels = XPC_MAX_NCHANNELS;
4315b8669dfSDean Nelson 
4325b8669dfSDean Nelson 	atomic_set(&part->nchannels_active, 0);
4335b8669dfSDean Nelson 	atomic_set(&part->nchannels_engaged, 0);
4345b8669dfSDean Nelson 
4355b8669dfSDean Nelson 	for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
4365b8669dfSDean Nelson 		ch = &part->channels[ch_number];
4375b8669dfSDean Nelson 
4385b8669dfSDean Nelson 		ch->partid = partid;
4395b8669dfSDean Nelson 		ch->number = ch_number;
4405b8669dfSDean Nelson 		ch->flags = XPC_C_DISCONNECTED;
4415b8669dfSDean Nelson 
4425b8669dfSDean Nelson 		atomic_set(&ch->kthreads_assigned, 0);
4435b8669dfSDean Nelson 		atomic_set(&ch->kthreads_idle, 0);
4445b8669dfSDean Nelson 		atomic_set(&ch->kthreads_active, 0);
4455b8669dfSDean Nelson 
4465b8669dfSDean Nelson 		atomic_set(&ch->references, 0);
4475b8669dfSDean Nelson 		atomic_set(&ch->n_to_notify, 0);
4485b8669dfSDean Nelson 
4495b8669dfSDean Nelson 		spin_lock_init(&ch->lock);
4505b8669dfSDean Nelson 		init_completion(&ch->wdisconnect_wait);
4515b8669dfSDean Nelson 
4525b8669dfSDean Nelson 		atomic_set(&ch->n_on_msg_allocate_wq, 0);
4535b8669dfSDean Nelson 		init_waitqueue_head(&ch->msg_allocate_wq);
4545b8669dfSDean Nelson 		init_waitqueue_head(&ch->idle_wq);
4555b8669dfSDean Nelson 	}
4565b8669dfSDean Nelson 
457a7665b0aSRobin Holt 	ret = xpc_arch_ops.setup_ch_structures(part);
4585b8669dfSDean Nelson 	if (ret != xpSuccess)
4595b8669dfSDean Nelson 		goto out_2;
4605b8669dfSDean Nelson 
4615b8669dfSDean Nelson 	/*
4625b8669dfSDean Nelson 	 * With the setting of the partition setup_state to XPC_P_SS_SETUP,
4635b8669dfSDean Nelson 	 * we're declaring that this partition is ready to go.
4645b8669dfSDean Nelson 	 */
4655b8669dfSDean Nelson 	part->setup_state = XPC_P_SS_SETUP;
4665b8669dfSDean Nelson 
4675b8669dfSDean Nelson 	return xpSuccess;
4685b8669dfSDean Nelson 
4695b8669dfSDean Nelson 	/* setup of ch structures failed */
4705b8669dfSDean Nelson out_2:
4715b8669dfSDean Nelson 	kfree(part->remote_openclose_args_base);
4725b8669dfSDean Nelson 	part->remote_openclose_args = NULL;
4735b8669dfSDean Nelson out_1:
4745b8669dfSDean Nelson 	kfree(part->channels);
4755b8669dfSDean Nelson 	part->channels = NULL;
4765b8669dfSDean Nelson 	return ret;
4775b8669dfSDean Nelson }
4785b8669dfSDean Nelson 
4795b8669dfSDean Nelson /*
4805b8669dfSDean Nelson  * Teardown the channel structures necessary to support XPartition Communication
4815b8669dfSDean Nelson  * between the specified remote partition and the local one.
4825b8669dfSDean Nelson  */
4835b8669dfSDean Nelson static void
xpc_teardown_ch_structures(struct xpc_partition * part)4845b8669dfSDean Nelson xpc_teardown_ch_structures(struct xpc_partition *part)
4855b8669dfSDean Nelson {
4865b8669dfSDean Nelson 	DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
4875b8669dfSDean Nelson 	DBUG_ON(atomic_read(&part->nchannels_active) != 0);
4885b8669dfSDean Nelson 
4895b8669dfSDean Nelson 	/*
4905b8669dfSDean Nelson 	 * Make this partition inaccessible to local processes by marking it
4915b8669dfSDean Nelson 	 * as no longer setup. Then wait before proceeding with the teardown
4925b8669dfSDean Nelson 	 * until all existing references cease.
4935b8669dfSDean Nelson 	 */
4945b8669dfSDean Nelson 	DBUG_ON(part->setup_state != XPC_P_SS_SETUP);
4955b8669dfSDean Nelson 	part->setup_state = XPC_P_SS_WTEARDOWN;
4965b8669dfSDean Nelson 
4975b8669dfSDean Nelson 	wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
4985b8669dfSDean Nelson 
4995b8669dfSDean Nelson 	/* now we can begin tearing down the infrastructure */
5005b8669dfSDean Nelson 
501a7665b0aSRobin Holt 	xpc_arch_ops.teardown_ch_structures(part);
5025b8669dfSDean Nelson 
5035b8669dfSDean Nelson 	kfree(part->remote_openclose_args_base);
5045b8669dfSDean Nelson 	part->remote_openclose_args = NULL;
5055b8669dfSDean Nelson 	kfree(part->channels);
5065b8669dfSDean Nelson 	part->channels = NULL;
5075b8669dfSDean Nelson 
5085b8669dfSDean Nelson 	part->setup_state = XPC_P_SS_TORNDOWN;
5095b8669dfSDean Nelson }
5105b8669dfSDean Nelson 
5115b8669dfSDean Nelson /*
51245d9ca49SDean Nelson  * When XPC HB determines that a partition has come up, it will create a new
51345d9ca49SDean Nelson  * kthread and that kthread will call this function to attempt to set up the
51445d9ca49SDean Nelson  * basic infrastructure used for Cross Partition Communication with the newly
51545d9ca49SDean Nelson  * upped partition.
51645d9ca49SDean Nelson  *
51745d9ca49SDean Nelson  * The kthread that was created by XPC HB and which setup the XPC
518e17d416bSDean Nelson  * infrastructure will remain assigned to the partition becoming the channel
519e17d416bSDean Nelson  * manager for that partition until the partition is deactivating, at which
520e17d416bSDean Nelson  * time the kthread will teardown the XPC infrastructure and then exit.
52145d9ca49SDean Nelson  */
52245d9ca49SDean Nelson static int
xpc_activating(void * __partid)52345d9ca49SDean Nelson xpc_activating(void *__partid)
52445d9ca49SDean Nelson {
52564d032baSDean Nelson 	short partid = (u64)__partid;
52645d9ca49SDean Nelson 	struct xpc_partition *part = &xpc_partitions[partid];
52745d9ca49SDean Nelson 	unsigned long irq_flags;
52845d9ca49SDean Nelson 
529bc63d387SDean Nelson 	DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
53045d9ca49SDean Nelson 
53145d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
53245d9ca49SDean Nelson 
53383469b55SDean Nelson 	if (part->act_state == XPC_P_AS_DEACTIVATING) {
53483469b55SDean Nelson 		part->act_state = XPC_P_AS_INACTIVE;
53545d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
53645d9ca49SDean Nelson 		part->remote_rp_pa = 0;
53745d9ca49SDean Nelson 		return 0;
53845d9ca49SDean Nelson 	}
53945d9ca49SDean Nelson 
54045d9ca49SDean Nelson 	/* indicate the thread is activating */
54183469b55SDean Nelson 	DBUG_ON(part->act_state != XPC_P_AS_ACTIVATION_REQ);
54283469b55SDean Nelson 	part->act_state = XPC_P_AS_ACTIVATING;
54345d9ca49SDean Nelson 
54445d9ca49SDean Nelson 	XPC_SET_REASON(part, 0, 0);
54545d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
54645d9ca49SDean Nelson 
547e17d416bSDean Nelson 	dev_dbg(xpc_part, "activating partition %d\n", partid);
54845d9ca49SDean Nelson 
549a7665b0aSRobin Holt 	xpc_arch_ops.allow_hb(partid);
55045d9ca49SDean Nelson 
5515b8669dfSDean Nelson 	if (xpc_setup_ch_structures(part) == xpSuccess) {
552e17d416bSDean Nelson 		(void)xpc_part_ref(part);	/* this will always succeed */
553e17d416bSDean Nelson 
554a7665b0aSRobin Holt 		if (xpc_arch_ops.make_first_contact(part) == xpSuccess) {
555e17d416bSDean Nelson 			xpc_mark_partition_active(part);
556e17d416bSDean Nelson 			xpc_channel_mgr(part);
557e17d416bSDean Nelson 			/* won't return until partition is deactivating */
558e17d416bSDean Nelson 		}
559e17d416bSDean Nelson 
560e17d416bSDean Nelson 		xpc_part_deref(part);
5615b8669dfSDean Nelson 		xpc_teardown_ch_structures(part);
562e17d416bSDean Nelson 	}
56345d9ca49SDean Nelson 
564a7665b0aSRobin Holt 	xpc_arch_ops.disallow_hb(partid);
56545d9ca49SDean Nelson 	xpc_mark_partition_inactive(part);
56645d9ca49SDean Nelson 
56765c17b80SDean Nelson 	if (part->reason == xpReactivating) {
56845d9ca49SDean Nelson 		/* interrupting ourselves results in activating partition */
569a7665b0aSRobin Holt 		xpc_arch_ops.request_partition_reactivation(part);
57045d9ca49SDean Nelson 	}
57145d9ca49SDean Nelson 
57245d9ca49SDean Nelson 	return 0;
57345d9ca49SDean Nelson }
57445d9ca49SDean Nelson 
57545d9ca49SDean Nelson void
xpc_activate_partition(struct xpc_partition * part)57645d9ca49SDean Nelson xpc_activate_partition(struct xpc_partition *part)
57745d9ca49SDean Nelson {
57864d032baSDean Nelson 	short partid = XPC_PARTID(part);
57945d9ca49SDean Nelson 	unsigned long irq_flags;
5802c2b94f9SDean Nelson 	struct task_struct *kthread;
58145d9ca49SDean Nelson 
58245d9ca49SDean Nelson 	spin_lock_irqsave(&part->act_lock, irq_flags);
58345d9ca49SDean Nelson 
58483469b55SDean Nelson 	DBUG_ON(part->act_state != XPC_P_AS_INACTIVE);
58545d9ca49SDean Nelson 
58683469b55SDean Nelson 	part->act_state = XPC_P_AS_ACTIVATION_REQ;
58765c17b80SDean Nelson 	XPC_SET_REASON(part, xpCloneKThread, __LINE__);
58845d9ca49SDean Nelson 
58945d9ca49SDean Nelson 	spin_unlock_irqrestore(&part->act_lock, irq_flags);
59045d9ca49SDean Nelson 
5912c2b94f9SDean Nelson 	kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
5922c2b94f9SDean Nelson 			      partid);
5932c2b94f9SDean Nelson 	if (IS_ERR(kthread)) {
59445d9ca49SDean Nelson 		spin_lock_irqsave(&part->act_lock, irq_flags);
59583469b55SDean Nelson 		part->act_state = XPC_P_AS_INACTIVE;
59665c17b80SDean Nelson 		XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
59745d9ca49SDean Nelson 		spin_unlock_irqrestore(&part->act_lock, irq_flags);
59845d9ca49SDean Nelson 	}
59945d9ca49SDean Nelson }
60045d9ca49SDean Nelson 
60145d9ca49SDean Nelson void
xpc_activate_kthreads(struct xpc_channel * ch,int needed)60245d9ca49SDean Nelson xpc_activate_kthreads(struct xpc_channel *ch, int needed)
60345d9ca49SDean Nelson {
60445d9ca49SDean Nelson 	int idle = atomic_read(&ch->kthreads_idle);
60545d9ca49SDean Nelson 	int assigned = atomic_read(&ch->kthreads_assigned);
60645d9ca49SDean Nelson 	int wakeup;
60745d9ca49SDean Nelson 
60845d9ca49SDean Nelson 	DBUG_ON(needed <= 0);
60945d9ca49SDean Nelson 
61045d9ca49SDean Nelson 	if (idle > 0) {
61145d9ca49SDean Nelson 		wakeup = (needed > idle) ? idle : needed;
61245d9ca49SDean Nelson 		needed -= wakeup;
61345d9ca49SDean Nelson 
61445d9ca49SDean Nelson 		dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
61545d9ca49SDean Nelson 			"channel=%d\n", wakeup, ch->partid, ch->number);
61645d9ca49SDean Nelson 
61745d9ca49SDean Nelson 		/* only wakeup the requested number of kthreads */
61845d9ca49SDean Nelson 		wake_up_nr(&ch->idle_wq, wakeup);
61945d9ca49SDean Nelson 	}
62045d9ca49SDean Nelson 
6212c2b94f9SDean Nelson 	if (needed <= 0)
62245d9ca49SDean Nelson 		return;
62345d9ca49SDean Nelson 
62445d9ca49SDean Nelson 	if (needed + assigned > ch->kthreads_assigned_limit) {
62545d9ca49SDean Nelson 		needed = ch->kthreads_assigned_limit - assigned;
6262c2b94f9SDean Nelson 		if (needed <= 0)
62745d9ca49SDean Nelson 			return;
62845d9ca49SDean Nelson 	}
62945d9ca49SDean Nelson 
63045d9ca49SDean Nelson 	dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
63145d9ca49SDean Nelson 		needed, ch->partid, ch->number);
63245d9ca49SDean Nelson 
63345d9ca49SDean Nelson 	xpc_create_kthreads(ch, needed, 0);
63445d9ca49SDean Nelson }
63545d9ca49SDean Nelson 
63645d9ca49SDean Nelson /*
63745d9ca49SDean Nelson  * This function is where XPC's kthreads wait for messages to deliver.
63845d9ca49SDean Nelson  */
63945d9ca49SDean Nelson static void
xpc_kthread_waitmsgs(struct xpc_partition * part,struct xpc_channel * ch)64045d9ca49SDean Nelson xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
64145d9ca49SDean Nelson {
642a7665b0aSRobin Holt 	int (*n_of_deliverable_payloads) (struct xpc_channel *) =
643a7665b0aSRobin Holt 		xpc_arch_ops.n_of_deliverable_payloads;
644a7665b0aSRobin Holt 
64545d9ca49SDean Nelson 	do {
64645d9ca49SDean Nelson 		/* deliver messages to their intended recipients */
64745d9ca49SDean Nelson 
648a7665b0aSRobin Holt 		while (n_of_deliverable_payloads(ch) > 0 &&
6492c2b94f9SDean Nelson 		       !(ch->flags & XPC_C_DISCONNECTING)) {
650bd3e64c1SDean Nelson 			xpc_deliver_payload(ch);
65145d9ca49SDean Nelson 		}
65245d9ca49SDean Nelson 
65345d9ca49SDean Nelson 		if (atomic_inc_return(&ch->kthreads_idle) >
65445d9ca49SDean Nelson 		    ch->kthreads_idle_limit) {
65545d9ca49SDean Nelson 			/* too many idle kthreads on this channel */
65645d9ca49SDean Nelson 			atomic_dec(&ch->kthreads_idle);
65745d9ca49SDean Nelson 			break;
65845d9ca49SDean Nelson 		}
65945d9ca49SDean Nelson 
66045d9ca49SDean Nelson 		dev_dbg(xpc_chan, "idle kthread calling "
66145d9ca49SDean Nelson 			"wait_event_interruptible_exclusive()\n");
66245d9ca49SDean Nelson 
66345d9ca49SDean Nelson 		(void)wait_event_interruptible_exclusive(ch->idle_wq,
664a7665b0aSRobin Holt 				(n_of_deliverable_payloads(ch) > 0 ||
6652c2b94f9SDean Nelson 				 (ch->flags & XPC_C_DISCONNECTING)));
66645d9ca49SDean Nelson 
66745d9ca49SDean Nelson 		atomic_dec(&ch->kthreads_idle);
66845d9ca49SDean Nelson 
6692c2b94f9SDean Nelson 	} while (!(ch->flags & XPC_C_DISCONNECTING));
67045d9ca49SDean Nelson }
67145d9ca49SDean Nelson 
67245d9ca49SDean Nelson static int
xpc_kthread_start(void * args)6732c2b94f9SDean Nelson xpc_kthread_start(void *args)
67445d9ca49SDean Nelson {
67564d032baSDean Nelson 	short partid = XPC_UNPACK_ARG1(args);
67645d9ca49SDean Nelson 	u16 ch_number = XPC_UNPACK_ARG2(args);
67745d9ca49SDean Nelson 	struct xpc_partition *part = &xpc_partitions[partid];
67845d9ca49SDean Nelson 	struct xpc_channel *ch;
67945d9ca49SDean Nelson 	int n_needed;
68045d9ca49SDean Nelson 	unsigned long irq_flags;
681a7665b0aSRobin Holt 	int (*n_of_deliverable_payloads) (struct xpc_channel *) =
682a7665b0aSRobin Holt 		xpc_arch_ops.n_of_deliverable_payloads;
68345d9ca49SDean Nelson 
68445d9ca49SDean Nelson 	dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
68545d9ca49SDean Nelson 		partid, ch_number);
68645d9ca49SDean Nelson 
68745d9ca49SDean Nelson 	ch = &part->channels[ch_number];
68845d9ca49SDean Nelson 
68945d9ca49SDean Nelson 	if (!(ch->flags & XPC_C_DISCONNECTING)) {
69045d9ca49SDean Nelson 
69145d9ca49SDean Nelson 		/* let registerer know that connection has been established */
69245d9ca49SDean Nelson 
69345d9ca49SDean Nelson 		spin_lock_irqsave(&ch->lock, irq_flags);
69445d9ca49SDean Nelson 		if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
69545d9ca49SDean Nelson 			ch->flags |= XPC_C_CONNECTEDCALLOUT;
69645d9ca49SDean Nelson 			spin_unlock_irqrestore(&ch->lock, irq_flags);
69745d9ca49SDean Nelson 
69845d9ca49SDean Nelson 			xpc_connected_callout(ch);
69945d9ca49SDean Nelson 
70045d9ca49SDean Nelson 			spin_lock_irqsave(&ch->lock, irq_flags);
70145d9ca49SDean Nelson 			ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
70245d9ca49SDean Nelson 			spin_unlock_irqrestore(&ch->lock, irq_flags);
70345d9ca49SDean Nelson 
70445d9ca49SDean Nelson 			/*
70545d9ca49SDean Nelson 			 * It is possible that while the callout was being
70645d9ca49SDean Nelson 			 * made that the remote partition sent some messages.
70745d9ca49SDean Nelson 			 * If that is the case, we may need to activate
70845d9ca49SDean Nelson 			 * additional kthreads to help deliver them. We only
70945d9ca49SDean Nelson 			 * need one less than total #of messages to deliver.
71045d9ca49SDean Nelson 			 */
711a7665b0aSRobin Holt 			n_needed = n_of_deliverable_payloads(ch) - 1;
7122c2b94f9SDean Nelson 			if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
71345d9ca49SDean Nelson 				xpc_activate_kthreads(ch, n_needed);
7142c2b94f9SDean Nelson 
71545d9ca49SDean Nelson 		} else {
71645d9ca49SDean Nelson 			spin_unlock_irqrestore(&ch->lock, irq_flags);
71745d9ca49SDean Nelson 		}
71845d9ca49SDean Nelson 
71945d9ca49SDean Nelson 		xpc_kthread_waitmsgs(part, ch);
72045d9ca49SDean Nelson 	}
72145d9ca49SDean Nelson 
72245d9ca49SDean Nelson 	/* let registerer know that connection is disconnecting */
72345d9ca49SDean Nelson 
72445d9ca49SDean Nelson 	spin_lock_irqsave(&ch->lock, irq_flags);
72545d9ca49SDean Nelson 	if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
72645d9ca49SDean Nelson 	    !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
72745d9ca49SDean Nelson 		ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
72845d9ca49SDean Nelson 		spin_unlock_irqrestore(&ch->lock, irq_flags);
72945d9ca49SDean Nelson 
73065c17b80SDean Nelson 		xpc_disconnect_callout(ch, xpDisconnecting);
73145d9ca49SDean Nelson 
73245d9ca49SDean Nelson 		spin_lock_irqsave(&ch->lock, irq_flags);
73345d9ca49SDean Nelson 		ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
73445d9ca49SDean Nelson 	}
73545d9ca49SDean Nelson 	spin_unlock_irqrestore(&ch->lock, irq_flags);
73645d9ca49SDean Nelson 
737a47d5dacSDean Nelson 	if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
738a47d5dacSDean Nelson 	    atomic_dec_return(&part->nchannels_engaged) == 0) {
739a7665b0aSRobin Holt 		xpc_arch_ops.indicate_partition_disengaged(part);
74045d9ca49SDean Nelson 	}
74145d9ca49SDean Nelson 
74245d9ca49SDean Nelson 	xpc_msgqueue_deref(ch);
74345d9ca49SDean Nelson 
74445d9ca49SDean Nelson 	dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
74545d9ca49SDean Nelson 		partid, ch_number);
74645d9ca49SDean Nelson 
74745d9ca49SDean Nelson 	xpc_part_deref(part);
74845d9ca49SDean Nelson 	return 0;
74945d9ca49SDean Nelson }
75045d9ca49SDean Nelson 
75145d9ca49SDean Nelson /*
75245d9ca49SDean Nelson  * For each partition that XPC has established communications with, there is
75345d9ca49SDean Nelson  * a minimum of one kernel thread assigned to perform any operation that
75445d9ca49SDean Nelson  * may potentially sleep or block (basically the callouts to the asynchronous
75545d9ca49SDean Nelson  * functions registered via xpc_connect()).
75645d9ca49SDean Nelson  *
75745d9ca49SDean Nelson  * Additional kthreads are created and destroyed by XPC as the workload
75845d9ca49SDean Nelson  * demands.
75945d9ca49SDean Nelson  *
76045d9ca49SDean Nelson  * A kthread is assigned to one of the active channels that exists for a given
76145d9ca49SDean Nelson  * partition.
76245d9ca49SDean Nelson  */
76345d9ca49SDean Nelson void
xpc_create_kthreads(struct xpc_channel * ch,int needed,int ignore_disconnecting)76445d9ca49SDean Nelson xpc_create_kthreads(struct xpc_channel *ch, int needed,
76545d9ca49SDean Nelson 		    int ignore_disconnecting)
76645d9ca49SDean Nelson {
76745d9ca49SDean Nelson 	unsigned long irq_flags;
76845d9ca49SDean Nelson 	u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
76945d9ca49SDean Nelson 	struct xpc_partition *part = &xpc_partitions[ch->partid];
7702c2b94f9SDean Nelson 	struct task_struct *kthread;
771a7665b0aSRobin Holt 	void (*indicate_partition_disengaged) (struct xpc_partition *) =
772a7665b0aSRobin Holt 		xpc_arch_ops.indicate_partition_disengaged;
77345d9ca49SDean Nelson 
77445d9ca49SDean Nelson 	while (needed-- > 0) {
77545d9ca49SDean Nelson 
77645d9ca49SDean Nelson 		/*
77745d9ca49SDean Nelson 		 * The following is done on behalf of the newly created
77845d9ca49SDean Nelson 		 * kthread. That kthread is responsible for doing the
77945d9ca49SDean Nelson 		 * counterpart to the following before it exits.
78045d9ca49SDean Nelson 		 */
78145d9ca49SDean Nelson 		if (ignore_disconnecting) {
78245d9ca49SDean Nelson 			if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
78345d9ca49SDean Nelson 				/* kthreads assigned had gone to zero */
78445d9ca49SDean Nelson 				BUG_ON(!(ch->flags &
78545d9ca49SDean Nelson 					 XPC_C_DISCONNECTINGCALLOUT_MADE));
78645d9ca49SDean Nelson 				break;
78745d9ca49SDean Nelson 			}
78845d9ca49SDean Nelson 
78945d9ca49SDean Nelson 		} else if (ch->flags & XPC_C_DISCONNECTING) {
79045d9ca49SDean Nelson 			break;
79145d9ca49SDean Nelson 
792a47d5dacSDean Nelson 		} else if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
793a47d5dacSDean Nelson 			   atomic_inc_return(&part->nchannels_engaged) == 1) {
794a7665b0aSRobin Holt 			xpc_arch_ops.indicate_partition_engaged(part);
79545d9ca49SDean Nelson 		}
79645d9ca49SDean Nelson 		(void)xpc_part_ref(part);
79745d9ca49SDean Nelson 		xpc_msgqueue_ref(ch);
79845d9ca49SDean Nelson 
7992c2b94f9SDean Nelson 		kthread = kthread_run(xpc_kthread_start, (void *)args,
8002c2b94f9SDean Nelson 				      "xpc%02dc%d", ch->partid, ch->number);
8012c2b94f9SDean Nelson 		if (IS_ERR(kthread)) {
80245d9ca49SDean Nelson 			/* the fork failed */
80345d9ca49SDean Nelson 
80445d9ca49SDean Nelson 			/*
80545d9ca49SDean Nelson 			 * NOTE: if (ignore_disconnecting &&
80645d9ca49SDean Nelson 			 * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
80745d9ca49SDean Nelson 			 * then we'll deadlock if all other kthreads assigned
80845d9ca49SDean Nelson 			 * to this channel are blocked in the channel's
80945d9ca49SDean Nelson 			 * registerer, because the only thing that will unblock
81065c17b80SDean Nelson 			 * them is the xpDisconnecting callout that this
8112c2b94f9SDean Nelson 			 * failed kthread_run() would have made.
81245d9ca49SDean Nelson 			 */
81345d9ca49SDean Nelson 
81445d9ca49SDean Nelson 			if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
81545d9ca49SDean Nelson 			    atomic_dec_return(&part->nchannels_engaged) == 0) {
816a7665b0aSRobin Holt 				indicate_partition_disengaged(part);
81745d9ca49SDean Nelson 			}
81845d9ca49SDean Nelson 			xpc_msgqueue_deref(ch);
81945d9ca49SDean Nelson 			xpc_part_deref(part);
82045d9ca49SDean Nelson 
82145d9ca49SDean Nelson 			if (atomic_read(&ch->kthreads_assigned) <
82245d9ca49SDean Nelson 			    ch->kthreads_idle_limit) {
82345d9ca49SDean Nelson 				/*
82445d9ca49SDean Nelson 				 * Flag this as an error only if we have an
82545d9ca49SDean Nelson 				 * insufficient #of kthreads for the channel
82645d9ca49SDean Nelson 				 * to function.
82745d9ca49SDean Nelson 				 */
82845d9ca49SDean Nelson 				spin_lock_irqsave(&ch->lock, irq_flags);
82965c17b80SDean Nelson 				XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
83045d9ca49SDean Nelson 						       &irq_flags);
83145d9ca49SDean Nelson 				spin_unlock_irqrestore(&ch->lock, irq_flags);
83245d9ca49SDean Nelson 			}
83345d9ca49SDean Nelson 			break;
83445d9ca49SDean Nelson 		}
83545d9ca49SDean Nelson 	}
83645d9ca49SDean Nelson }
83745d9ca49SDean Nelson 
83845d9ca49SDean Nelson void
xpc_disconnect_wait(int ch_number)83945d9ca49SDean Nelson xpc_disconnect_wait(int ch_number)
84045d9ca49SDean Nelson {
84145d9ca49SDean Nelson 	unsigned long irq_flags;
84264d032baSDean Nelson 	short partid;
84345d9ca49SDean Nelson 	struct xpc_partition *part;
84445d9ca49SDean Nelson 	struct xpc_channel *ch;
84545d9ca49SDean Nelson 	int wakeup_channel_mgr;
84645d9ca49SDean Nelson 
84745d9ca49SDean Nelson 	/* now wait for all callouts to the caller's function to cease */
848bc63d387SDean Nelson 	for (partid = 0; partid < xp_max_npartitions; partid++) {
84945d9ca49SDean Nelson 		part = &xpc_partitions[partid];
85045d9ca49SDean Nelson 
8512c2b94f9SDean Nelson 		if (!xpc_part_ref(part))
85245d9ca49SDean Nelson 			continue;
85345d9ca49SDean Nelson 
85445d9ca49SDean Nelson 		ch = &part->channels[ch_number];
85545d9ca49SDean Nelson 
85645d9ca49SDean Nelson 		if (!(ch->flags & XPC_C_WDISCONNECT)) {
85745d9ca49SDean Nelson 			xpc_part_deref(part);
85845d9ca49SDean Nelson 			continue;
85945d9ca49SDean Nelson 		}
86045d9ca49SDean Nelson 
86145d9ca49SDean Nelson 		wait_for_completion(&ch->wdisconnect_wait);
86245d9ca49SDean Nelson 
86345d9ca49SDean Nelson 		spin_lock_irqsave(&ch->lock, irq_flags);
86445d9ca49SDean Nelson 		DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
86545d9ca49SDean Nelson 		wakeup_channel_mgr = 0;
86645d9ca49SDean Nelson 
8677fb5e59dSDean Nelson 		if (ch->delayed_chctl_flags) {
86883469b55SDean Nelson 			if (part->act_state != XPC_P_AS_DEACTIVATING) {
8697fb5e59dSDean Nelson 				spin_lock(&part->chctl_lock);
8707fb5e59dSDean Nelson 				part->chctl.flags[ch->number] |=
8717fb5e59dSDean Nelson 				    ch->delayed_chctl_flags;
8727fb5e59dSDean Nelson 				spin_unlock(&part->chctl_lock);
87345d9ca49SDean Nelson 				wakeup_channel_mgr = 1;
87445d9ca49SDean Nelson 			}
8757fb5e59dSDean Nelson 			ch->delayed_chctl_flags = 0;
87645d9ca49SDean Nelson 		}
87745d9ca49SDean Nelson 
87845d9ca49SDean Nelson 		ch->flags &= ~XPC_C_WDISCONNECT;
87945d9ca49SDean Nelson 		spin_unlock_irqrestore(&ch->lock, irq_flags);
88045d9ca49SDean Nelson 
8812c2b94f9SDean Nelson 		if (wakeup_channel_mgr)
88245d9ca49SDean Nelson 			xpc_wakeup_channel_mgr(part);
88345d9ca49SDean Nelson 
88445d9ca49SDean Nelson 		xpc_part_deref(part);
88545d9ca49SDean Nelson 	}
88645d9ca49SDean Nelson }
88745d9ca49SDean Nelson 
8885b8669dfSDean Nelson static int
xpc_setup_partitions(void)8895b8669dfSDean Nelson xpc_setup_partitions(void)
8905b8669dfSDean Nelson {
8915b8669dfSDean Nelson 	short partid;
8925b8669dfSDean Nelson 	struct xpc_partition *part;
8935b8669dfSDean Nelson 
8946396bb22SKees Cook 	xpc_partitions = kcalloc(xp_max_npartitions,
8956396bb22SKees Cook 				 sizeof(struct xpc_partition),
8966396bb22SKees Cook 				 GFP_KERNEL);
8975b8669dfSDean Nelson 	if (xpc_partitions == NULL) {
8985b8669dfSDean Nelson 		dev_err(xpc_part, "can't get memory for partition structure\n");
8995b8669dfSDean Nelson 		return -ENOMEM;
9005b8669dfSDean Nelson 	}
9015b8669dfSDean Nelson 
9025b8669dfSDean Nelson 	/*
9035b8669dfSDean Nelson 	 * The first few fields of each entry of xpc_partitions[] need to
9045b8669dfSDean Nelson 	 * be initialized now so that calls to xpc_connect() and
9055b8669dfSDean Nelson 	 * xpc_disconnect() can be made prior to the activation of any remote
9065b8669dfSDean Nelson 	 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
9075b8669dfSDean Nelson 	 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
9085b8669dfSDean Nelson 	 * PARTITION HAS BEEN ACTIVATED.
9095b8669dfSDean Nelson 	 */
9105b8669dfSDean Nelson 	for (partid = 0; partid < xp_max_npartitions; partid++) {
9115b8669dfSDean Nelson 		part = &xpc_partitions[partid];
9125b8669dfSDean Nelson 
9135b8669dfSDean Nelson 		DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
9145b8669dfSDean Nelson 
9155b8669dfSDean Nelson 		part->activate_IRQ_rcvd = 0;
9165b8669dfSDean Nelson 		spin_lock_init(&part->act_lock);
9175b8669dfSDean Nelson 		part->act_state = XPC_P_AS_INACTIVE;
9185b8669dfSDean Nelson 		XPC_SET_REASON(part, 0, 0);
9195b8669dfSDean Nelson 
92025b42fa8SKees Cook 		timer_setup(&part->disengage_timer,
92125b42fa8SKees Cook 			    xpc_timeout_partition_disengage, 0);
9225b8669dfSDean Nelson 
9235b8669dfSDean Nelson 		part->setup_state = XPC_P_SS_UNSET;
9245b8669dfSDean Nelson 		init_waitqueue_head(&part->teardown_wq);
9255b8669dfSDean Nelson 		atomic_set(&part->references, 0);
9265b8669dfSDean Nelson 	}
9275b8669dfSDean Nelson 
928a7665b0aSRobin Holt 	return xpc_arch_ops.setup_partitions();
9295b8669dfSDean Nelson }
9305b8669dfSDean Nelson 
9315b8669dfSDean Nelson static void
xpc_teardown_partitions(void)9325b8669dfSDean Nelson xpc_teardown_partitions(void)
9335b8669dfSDean Nelson {
934a7665b0aSRobin Holt 	xpc_arch_ops.teardown_partitions();
9355b8669dfSDean Nelson 	kfree(xpc_partitions);
9365b8669dfSDean Nelson }
9375b8669dfSDean Nelson 
93845d9ca49SDean Nelson static void
xpc_do_exit(enum xp_retval reason)93965c17b80SDean Nelson xpc_do_exit(enum xp_retval reason)
94045d9ca49SDean Nelson {
94164d032baSDean Nelson 	short partid;
94245d9ca49SDean Nelson 	int active_part_count, printed_waiting_msg = 0;
94345d9ca49SDean Nelson 	struct xpc_partition *part;
944a47d5dacSDean Nelson 	unsigned long printmsg_time, disengage_timeout = 0;
94545d9ca49SDean Nelson 
94645d9ca49SDean Nelson 	/* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
94745d9ca49SDean Nelson 	DBUG_ON(xpc_exiting == 1);
94845d9ca49SDean Nelson 
94945d9ca49SDean Nelson 	/*
95045d9ca49SDean Nelson 	 * Let the heartbeat checker thread and the discovery thread
95145d9ca49SDean Nelson 	 * (if one is running) know that they should exit. Also wake up
95245d9ca49SDean Nelson 	 * the heartbeat checker thread in case it's sleeping.
95345d9ca49SDean Nelson 	 */
95445d9ca49SDean Nelson 	xpc_exiting = 1;
9556e41017aSDean Nelson 	wake_up_interruptible(&xpc_activate_IRQ_wq);
95645d9ca49SDean Nelson 
95745d9ca49SDean Nelson 	/* wait for the discovery thread to exit */
95845d9ca49SDean Nelson 	wait_for_completion(&xpc_discovery_exited);
95945d9ca49SDean Nelson 
96045d9ca49SDean Nelson 	/* wait for the heartbeat checker thread to exit */
96145d9ca49SDean Nelson 	wait_for_completion(&xpc_hb_checker_exited);
96245d9ca49SDean Nelson 
96345d9ca49SDean Nelson 	/* sleep for a 1/3 of a second or so */
96445d9ca49SDean Nelson 	(void)msleep_interruptible(300);
96545d9ca49SDean Nelson 
96645d9ca49SDean Nelson 	/* wait for all partitions to become inactive */
96745d9ca49SDean Nelson 
968a47d5dacSDean Nelson 	printmsg_time = jiffies + (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
969a47d5dacSDean Nelson 	xpc_disengage_timedout = 0;
97045d9ca49SDean Nelson 
97145d9ca49SDean Nelson 	do {
97245d9ca49SDean Nelson 		active_part_count = 0;
97345d9ca49SDean Nelson 
974bc63d387SDean Nelson 		for (partid = 0; partid < xp_max_npartitions; partid++) {
97545d9ca49SDean Nelson 			part = &xpc_partitions[partid];
97645d9ca49SDean Nelson 
97745d9ca49SDean Nelson 			if (xpc_partition_disengaged(part) &&
97883469b55SDean Nelson 			    part->act_state == XPC_P_AS_INACTIVE) {
97945d9ca49SDean Nelson 				continue;
98045d9ca49SDean Nelson 			}
98145d9ca49SDean Nelson 
98245d9ca49SDean Nelson 			active_part_count++;
98345d9ca49SDean Nelson 
98445d9ca49SDean Nelson 			XPC_DEACTIVATE_PARTITION(part, reason);
98545d9ca49SDean Nelson 
986a47d5dacSDean Nelson 			if (part->disengage_timeout > disengage_timeout)
987a47d5dacSDean Nelson 				disengage_timeout = part->disengage_timeout;
98845d9ca49SDean Nelson 		}
98945d9ca49SDean Nelson 
990a7665b0aSRobin Holt 		if (xpc_arch_ops.any_partition_engaged()) {
991aaa3cd69SDean Nelson 			if (time_is_before_jiffies(printmsg_time)) {
99245d9ca49SDean Nelson 				dev_info(xpc_part, "waiting for remote "
993a47d5dacSDean Nelson 					 "partitions to deactivate, timeout in "
994a47d5dacSDean Nelson 					 "%ld seconds\n", (disengage_timeout -
995a47d5dacSDean Nelson 					 jiffies) / HZ);
99645d9ca49SDean Nelson 				printmsg_time = jiffies +
997a47d5dacSDean Nelson 				    (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
99845d9ca49SDean Nelson 				printed_waiting_msg = 1;
99945d9ca49SDean Nelson 			}
100045d9ca49SDean Nelson 
100145d9ca49SDean Nelson 		} else if (active_part_count > 0) {
100245d9ca49SDean Nelson 			if (printed_waiting_msg) {
100345d9ca49SDean Nelson 				dev_info(xpc_part, "waiting for local partition"
1004a47d5dacSDean Nelson 					 " to deactivate\n");
100545d9ca49SDean Nelson 				printed_waiting_msg = 0;
100645d9ca49SDean Nelson 			}
100745d9ca49SDean Nelson 
100845d9ca49SDean Nelson 		} else {
1009a47d5dacSDean Nelson 			if (!xpc_disengage_timedout) {
101045d9ca49SDean Nelson 				dev_info(xpc_part, "all partitions have "
1011a47d5dacSDean Nelson 					 "deactivated\n");
101245d9ca49SDean Nelson 			}
101345d9ca49SDean Nelson 			break;
101445d9ca49SDean Nelson 		}
101545d9ca49SDean Nelson 
101645d9ca49SDean Nelson 		/* sleep for a 1/3 of a second or so */
101745d9ca49SDean Nelson 		(void)msleep_interruptible(300);
101845d9ca49SDean Nelson 
101945d9ca49SDean Nelson 	} while (1);
102045d9ca49SDean Nelson 
1021a7665b0aSRobin Holt 	DBUG_ON(xpc_arch_ops.any_partition_engaged());
102245d9ca49SDean Nelson 
10235b8669dfSDean Nelson 	xpc_teardown_rsvd_page();
102445d9ca49SDean Nelson 
102565c17b80SDean Nelson 	if (reason == xpUnloading) {
102645d9ca49SDean Nelson 		(void)unregister_die_notifier(&xpc_die_notifier);
1027bc63d387SDean Nelson 		(void)unregister_reboot_notifier(&xpc_reboot_notifier);
102845d9ca49SDean Nelson 	}
102945d9ca49SDean Nelson 
103045d9ca49SDean Nelson 	/* clear the interface to XPC's functions */
103145d9ca49SDean Nelson 	xpc_clear_interface();
103245d9ca49SDean Nelson 
10332c2b94f9SDean Nelson 	if (xpc_sysctl)
103445d9ca49SDean Nelson 		unregister_sysctl_table(xpc_sysctl);
1035*b085dbf6SLuis Chamberlain 	if (xpc_sysctl_hb)
1036*b085dbf6SLuis Chamberlain 		unregister_sysctl_table(xpc_sysctl_hb);
103745d9ca49SDean Nelson 
10385b8669dfSDean Nelson 	xpc_teardown_partitions();
10396e41017aSDean Nelson 
1040788b66e3SMike Travis 	if (is_uv_system())
10416e41017aSDean Nelson 		xpc_exit_uv();
104245d9ca49SDean Nelson }
104345d9ca49SDean Nelson 
104445d9ca49SDean Nelson /*
104545d9ca49SDean Nelson  * This function is called when the system is being rebooted.
104645d9ca49SDean Nelson  */
104745d9ca49SDean Nelson static int
xpc_system_reboot(struct notifier_block * nb,unsigned long event,void * unused)104845d9ca49SDean Nelson xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
104945d9ca49SDean Nelson {
105065c17b80SDean Nelson 	enum xp_retval reason;
105145d9ca49SDean Nelson 
105245d9ca49SDean Nelson 	switch (event) {
105345d9ca49SDean Nelson 	case SYS_RESTART:
105465c17b80SDean Nelson 		reason = xpSystemReboot;
105545d9ca49SDean Nelson 		break;
105645d9ca49SDean Nelson 	case SYS_HALT:
105765c17b80SDean Nelson 		reason = xpSystemHalt;
105845d9ca49SDean Nelson 		break;
105945d9ca49SDean Nelson 	case SYS_POWER_OFF:
106065c17b80SDean Nelson 		reason = xpSystemPoweroff;
106145d9ca49SDean Nelson 		break;
106245d9ca49SDean Nelson 	default:
106365c17b80SDean Nelson 		reason = xpSystemGoingDown;
106445d9ca49SDean Nelson 	}
106545d9ca49SDean Nelson 
106645d9ca49SDean Nelson 	xpc_do_exit(reason);
106745d9ca49SDean Nelson 	return NOTIFY_DONE;
106845d9ca49SDean Nelson }
106945d9ca49SDean Nelson 
1070891348caSRobin Holt /* Used to only allow one cpu to complete disconnect */
1071891348caSRobin Holt static unsigned int xpc_die_disconnecting;
1072891348caSRobin Holt 
107345d9ca49SDean Nelson /*
1074a47d5dacSDean Nelson  * Notify other partitions to deactivate from us by first disengaging from all
1075a47d5dacSDean Nelson  * references to our memory.
107645d9ca49SDean Nelson  */
107745d9ca49SDean Nelson static void
xpc_die_deactivate(void)1078a47d5dacSDean Nelson xpc_die_deactivate(void)
107945d9ca49SDean Nelson {
108045d9ca49SDean Nelson 	struct xpc_partition *part;
108164d032baSDean Nelson 	short partid;
1082a47d5dacSDean Nelson 	int any_engaged;
1083261f3b49SDean Nelson 	long keep_waiting;
1084261f3b49SDean Nelson 	long wait_to_print;
108545d9ca49SDean Nelson 
1086891348caSRobin Holt 	if (cmpxchg(&xpc_die_disconnecting, 0, 1))
1087891348caSRobin Holt 		return;
1088891348caSRobin Holt 
108945d9ca49SDean Nelson 	/* keep xpc_hb_checker thread from doing anything (just in case) */
109045d9ca49SDean Nelson 	xpc_exiting = 1;
109145d9ca49SDean Nelson 
1092a7665b0aSRobin Holt 	xpc_arch_ops.disallow_all_hbs();   /*indicate we're deactivated */
109345d9ca49SDean Nelson 
1094bc63d387SDean Nelson 	for (partid = 0; partid < xp_max_npartitions; partid++) {
109545d9ca49SDean Nelson 		part = &xpc_partitions[partid];
109645d9ca49SDean Nelson 
1097a7665b0aSRobin Holt 		if (xpc_arch_ops.partition_engaged(partid) ||
109883469b55SDean Nelson 		    part->act_state != XPC_P_AS_INACTIVE) {
1099a7665b0aSRobin Holt 			xpc_arch_ops.request_partition_deactivation(part);
1100a7665b0aSRobin Holt 			xpc_arch_ops.indicate_partition_disengaged(part);
110145d9ca49SDean Nelson 		}
110245d9ca49SDean Nelson 	}
110345d9ca49SDean Nelson 
1104a47d5dacSDean Nelson 	/*
1105a47d5dacSDean Nelson 	 * Though we requested that all other partitions deactivate from us,
1106261f3b49SDean Nelson 	 * we only wait until they've all disengaged or we've reached the
1107261f3b49SDean Nelson 	 * defined timelimit.
1108261f3b49SDean Nelson 	 *
1109261f3b49SDean Nelson 	 * Given that one iteration through the following while-loop takes
1110261f3b49SDean Nelson 	 * approximately 200 microseconds, calculate the #of loops to take
1111261f3b49SDean Nelson 	 * before bailing and the #of loops before printing a waiting message.
1112a47d5dacSDean Nelson 	 */
1113261f3b49SDean Nelson 	keep_waiting = xpc_disengage_timelimit * 1000 * 5;
1114261f3b49SDean Nelson 	wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL * 1000 * 5;
111545d9ca49SDean Nelson 
111645d9ca49SDean Nelson 	while (1) {
1117a7665b0aSRobin Holt 		any_engaged = xpc_arch_ops.any_partition_engaged();
1118a47d5dacSDean Nelson 		if (!any_engaged) {
1119a47d5dacSDean Nelson 			dev_info(xpc_part, "all partitions have deactivated\n");
112045d9ca49SDean Nelson 			break;
112145d9ca49SDean Nelson 		}
112245d9ca49SDean Nelson 
1123261f3b49SDean Nelson 		if (!keep_waiting--) {
1124bc63d387SDean Nelson 			for (partid = 0; partid < xp_max_npartitions;
1125bc63d387SDean Nelson 			     partid++) {
1126a7665b0aSRobin Holt 				if (xpc_arch_ops.partition_engaged(partid)) {
1127a47d5dacSDean Nelson 					dev_info(xpc_part, "deactivate from "
112845d9ca49SDean Nelson 						 "remote partition %d timed "
112945d9ca49SDean Nelson 						 "out\n", partid);
113045d9ca49SDean Nelson 				}
113145d9ca49SDean Nelson 			}
113245d9ca49SDean Nelson 			break;
113345d9ca49SDean Nelson 		}
113445d9ca49SDean Nelson 
1135261f3b49SDean Nelson 		if (!wait_to_print--) {
113645d9ca49SDean Nelson 			dev_info(xpc_part, "waiting for remote partitions to "
1137a47d5dacSDean Nelson 				 "deactivate, timeout in %ld seconds\n",
1138261f3b49SDean Nelson 				 keep_waiting / (1000 * 5));
1139261f3b49SDean Nelson 			wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL *
1140261f3b49SDean Nelson 			    1000 * 5;
114145d9ca49SDean Nelson 		}
1142261f3b49SDean Nelson 
1143261f3b49SDean Nelson 		udelay(200);
114445d9ca49SDean Nelson 	}
114545d9ca49SDean Nelson }
114645d9ca49SDean Nelson 
114745d9ca49SDean Nelson /*
114845d9ca49SDean Nelson  * This function is called when the system is being restarted or halted due
114945d9ca49SDean Nelson  * to some sort of system failure. If this is the case we need to notify the
115045d9ca49SDean Nelson  * other partitions to disengage from all references to our memory.
115145d9ca49SDean Nelson  * This function can also be called when our heartbeater could be offlined
115245d9ca49SDean Nelson  * for a time. In this case we need to notify other partitions to not worry
115345d9ca49SDean Nelson  * about the lack of a heartbeat.
115445d9ca49SDean Nelson  */
115545d9ca49SDean Nelson static int
xpc_system_die(struct notifier_block * nb,unsigned long event,void * _die_args)1156891348caSRobin Holt xpc_system_die(struct notifier_block *nb, unsigned long event, void *_die_args)
115745d9ca49SDean Nelson {
1158261f3b49SDean Nelson #ifdef CONFIG_IA64		/* !!! temporary kludge */
115945d9ca49SDean Nelson 	switch (event) {
116045d9ca49SDean Nelson 	case DIE_MACHINE_RESTART:
116145d9ca49SDean Nelson 	case DIE_MACHINE_HALT:
1162a47d5dacSDean Nelson 		xpc_die_deactivate();
116345d9ca49SDean Nelson 		break;
116445d9ca49SDean Nelson 
116545d9ca49SDean Nelson 	case DIE_KDEBUG_ENTER:
116645d9ca49SDean Nelson 		/* Should lack of heartbeat be ignored by other partitions? */
11672c2b94f9SDean Nelson 		if (!xpc_kdebug_ignore)
116845d9ca49SDean Nelson 			break;
11692c2b94f9SDean Nelson 
1170df561f66SGustavo A. R. Silva 		fallthrough;
117145d9ca49SDean Nelson 	case DIE_MCA_MONARCH_ENTER:
117245d9ca49SDean Nelson 	case DIE_INIT_MONARCH_ENTER:
1173a7665b0aSRobin Holt 		xpc_arch_ops.offline_heartbeat();
117445d9ca49SDean Nelson 		break;
117545d9ca49SDean Nelson 
117645d9ca49SDean Nelson 	case DIE_KDEBUG_LEAVE:
117745d9ca49SDean Nelson 		/* Is lack of heartbeat being ignored by other partitions? */
11782c2b94f9SDean Nelson 		if (!xpc_kdebug_ignore)
117945d9ca49SDean Nelson 			break;
11802c2b94f9SDean Nelson 
1181df561f66SGustavo A. R. Silva 		fallthrough;
118245d9ca49SDean Nelson 	case DIE_MCA_MONARCH_LEAVE:
118345d9ca49SDean Nelson 	case DIE_INIT_MONARCH_LEAVE:
1184a7665b0aSRobin Holt 		xpc_arch_ops.online_heartbeat();
118545d9ca49SDean Nelson 		break;
118645d9ca49SDean Nelson 	}
1187261f3b49SDean Nelson #else
1188891348caSRobin Holt 	struct die_args *die_args = _die_args;
1189891348caSRobin Holt 
1190891348caSRobin Holt 	switch (event) {
1191891348caSRobin Holt 	case DIE_TRAP:
1192891348caSRobin Holt 		if (die_args->trapnr == X86_TRAP_DF)
1193261f3b49SDean Nelson 			xpc_die_deactivate();
1194891348caSRobin Holt 
1195891348caSRobin Holt 		if (((die_args->trapnr == X86_TRAP_MF) ||
1196891348caSRobin Holt 		     (die_args->trapnr == X86_TRAP_XF)) &&
1197f39b6f0eSAndy Lutomirski 		    !user_mode(die_args->regs))
1198891348caSRobin Holt 			xpc_die_deactivate();
1199891348caSRobin Holt 
1200891348caSRobin Holt 		break;
1201891348caSRobin Holt 	case DIE_INT3:
1202891348caSRobin Holt 	case DIE_DEBUG:
1203891348caSRobin Holt 		break;
1204891348caSRobin Holt 	case DIE_OOPS:
1205891348caSRobin Holt 	case DIE_GPF:
1206891348caSRobin Holt 	default:
1207891348caSRobin Holt 		xpc_die_deactivate();
1208891348caSRobin Holt 	}
1209261f3b49SDean Nelson #endif
121045d9ca49SDean Nelson 
121145d9ca49SDean Nelson 	return NOTIFY_DONE;
121245d9ca49SDean Nelson }
121345d9ca49SDean Nelson 
121419df2f8eSJason Yan static int __init
xpc_init(void)121545d9ca49SDean Nelson xpc_init(void)
121645d9ca49SDean Nelson {
121745d9ca49SDean Nelson 	int ret;
12182c2b94f9SDean Nelson 	struct task_struct *kthread;
1219ee6665e3SDean Nelson 
1220bb0dc43eSKay Sievers 	dev_set_name(xpc_part, "part");
1221bb0dc43eSKay Sievers 	dev_set_name(xpc_chan, "chan");
122245d9ca49SDean Nelson 
1223788b66e3SMike Travis 	if (is_uv_system()) {
12245b8669dfSDean Nelson 		ret = xpc_init_uv();
12255b8669dfSDean Nelson 
12265b8669dfSDean Nelson 	} else {
12275b8669dfSDean Nelson 		ret = -ENODEV;
12285b8669dfSDean Nelson 	}
12295b8669dfSDean Nelson 
12306e41017aSDean Nelson 	if (ret != 0)
12316e41017aSDean Nelson 		return ret;
123294bd2708SDean Nelson 
12335b8669dfSDean Nelson 	ret = xpc_setup_partitions();
12345b8669dfSDean Nelson 	if (ret != 0) {
1235bc63d387SDean Nelson 		dev_err(xpc_part, "can't get memory for partition structure\n");
1236ee6665e3SDean Nelson 		goto out_1;
1237bc63d387SDean Nelson 	}
123845d9ca49SDean Nelson 
1239*b085dbf6SLuis Chamberlain 	xpc_sysctl = register_sysctl("xpc", xpc_sys_xpc);
1240*b085dbf6SLuis Chamberlain 	xpc_sysctl_hb = register_sysctl("xpc/hb", xpc_sys_xpc_hb);
1241bc63d387SDean Nelson 
124245d9ca49SDean Nelson 	/*
124345d9ca49SDean Nelson 	 * Fill the partition reserved page with the information needed by
124445d9ca49SDean Nelson 	 * other partitions to discover we are alive and establish initial
124545d9ca49SDean Nelson 	 * communications.
124645d9ca49SDean Nelson 	 */
12475b8669dfSDean Nelson 	ret = xpc_setup_rsvd_page();
12485b8669dfSDean Nelson 	if (ret != 0) {
1249bc63d387SDean Nelson 		dev_err(xpc_part, "can't setup our reserved page\n");
1250ee6665e3SDean Nelson 		goto out_2;
125145d9ca49SDean Nelson 	}
125245d9ca49SDean Nelson 
125345d9ca49SDean Nelson 	/* add ourselves to the reboot_notifier_list */
125445d9ca49SDean Nelson 	ret = register_reboot_notifier(&xpc_reboot_notifier);
12552c2b94f9SDean Nelson 	if (ret != 0)
125645d9ca49SDean Nelson 		dev_warn(xpc_part, "can't register reboot notifier\n");
125745d9ca49SDean Nelson 
125845d9ca49SDean Nelson 	/* add ourselves to the die_notifier list */
125945d9ca49SDean Nelson 	ret = register_die_notifier(&xpc_die_notifier);
12602c2b94f9SDean Nelson 	if (ret != 0)
126145d9ca49SDean Nelson 		dev_warn(xpc_part, "can't register die notifier\n");
126245d9ca49SDean Nelson 
126345d9ca49SDean Nelson 	/*
126445d9ca49SDean Nelson 	 * The real work-horse behind xpc.  This processes incoming
126545d9ca49SDean Nelson 	 * interrupts and monitors remote heartbeats.
126645d9ca49SDean Nelson 	 */
12672c2b94f9SDean Nelson 	kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
12682c2b94f9SDean Nelson 	if (IS_ERR(kthread)) {
126945d9ca49SDean Nelson 		dev_err(xpc_part, "failed while forking hb check thread\n");
1270bc63d387SDean Nelson 		ret = -EBUSY;
1271ee6665e3SDean Nelson 		goto out_3;
127245d9ca49SDean Nelson 	}
127345d9ca49SDean Nelson 
127445d9ca49SDean Nelson 	/*
127545d9ca49SDean Nelson 	 * Startup a thread that will attempt to discover other partitions to
127645d9ca49SDean Nelson 	 * activate based on info provided by SAL. This new thread is short
127745d9ca49SDean Nelson 	 * lived and will exit once discovery is complete.
127845d9ca49SDean Nelson 	 */
12792c2b94f9SDean Nelson 	kthread = kthread_run(xpc_initiate_discovery, NULL,
12802c2b94f9SDean Nelson 			      XPC_DISCOVERY_THREAD_NAME);
12812c2b94f9SDean Nelson 	if (IS_ERR(kthread)) {
128245d9ca49SDean Nelson 		dev_err(xpc_part, "failed while forking discovery thread\n");
128345d9ca49SDean Nelson 
128445d9ca49SDean Nelson 		/* mark this new thread as a non-starter */
128545d9ca49SDean Nelson 		complete(&xpc_discovery_exited);
128645d9ca49SDean Nelson 
128765c17b80SDean Nelson 		xpc_do_exit(xpUnloading);
128845d9ca49SDean Nelson 		return -EBUSY;
128945d9ca49SDean Nelson 	}
129045d9ca49SDean Nelson 
129145d9ca49SDean Nelson 	/* set the interface to point at XPC's functions */
129245d9ca49SDean Nelson 	xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
129397bf1aa1SDean Nelson 			  xpc_initiate_send, xpc_initiate_send_notify,
129497bf1aa1SDean Nelson 			  xpc_initiate_received, xpc_initiate_partid_to_nasids);
129545d9ca49SDean Nelson 
129645d9ca49SDean Nelson 	return 0;
1297bc63d387SDean Nelson 
1298bc63d387SDean Nelson 	/* initialization was not successful */
1299ee6665e3SDean Nelson out_3:
13005b8669dfSDean Nelson 	xpc_teardown_rsvd_page();
130194bd2708SDean Nelson 
1302bc63d387SDean Nelson 	(void)unregister_die_notifier(&xpc_die_notifier);
1303bc63d387SDean Nelson 	(void)unregister_reboot_notifier(&xpc_reboot_notifier);
1304ee6665e3SDean Nelson out_2:
1305*b085dbf6SLuis Chamberlain 	if (xpc_sysctl_hb)
1306*b085dbf6SLuis Chamberlain 		unregister_sysctl_table(xpc_sysctl_hb);
1307bc63d387SDean Nelson 	if (xpc_sysctl)
1308bc63d387SDean Nelson 		unregister_sysctl_table(xpc_sysctl);
13095b8669dfSDean Nelson 
13105b8669dfSDean Nelson 	xpc_teardown_partitions();
13116e41017aSDean Nelson out_1:
1312788b66e3SMike Travis 	if (is_uv_system())
13136e41017aSDean Nelson 		xpc_exit_uv();
1314bc63d387SDean Nelson 	return ret;
131545d9ca49SDean Nelson }
131645d9ca49SDean Nelson 
131735190506SDean Nelson module_init(xpc_init);
131845d9ca49SDean Nelson 
131919df2f8eSJason Yan static void __exit
xpc_exit(void)132045d9ca49SDean Nelson xpc_exit(void)
132145d9ca49SDean Nelson {
132265c17b80SDean Nelson 	xpc_do_exit(xpUnloading);
132345d9ca49SDean Nelson }
132445d9ca49SDean Nelson 
132535190506SDean Nelson module_exit(xpc_exit);
132645d9ca49SDean Nelson 
132745d9ca49SDean Nelson MODULE_AUTHOR("Silicon Graphics, Inc.");
132845d9ca49SDean Nelson MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
132945d9ca49SDean Nelson MODULE_LICENSE("GPL");
133045d9ca49SDean Nelson 
133145d9ca49SDean Nelson module_param(xpc_hb_interval, int, 0);
133245d9ca49SDean Nelson MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
133345d9ca49SDean Nelson 		 "heartbeat increments.");
133445d9ca49SDean Nelson 
133545d9ca49SDean Nelson module_param(xpc_hb_check_interval, int, 0);
133645d9ca49SDean Nelson MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
133745d9ca49SDean Nelson 		 "heartbeat checks.");
133845d9ca49SDean Nelson 
1339a47d5dacSDean Nelson module_param(xpc_disengage_timelimit, int, 0);
1340a47d5dacSDean Nelson MODULE_PARM_DESC(xpc_disengage_timelimit, "Number of seconds to wait "
1341a47d5dacSDean Nelson 		 "for disengage to complete.");
134245d9ca49SDean Nelson 
134345d9ca49SDean Nelson module_param(xpc_kdebug_ignore, int, 0);
134445d9ca49SDean Nelson MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
134545d9ca49SDean Nelson 		 "other partitions when dropping into kdebug.");
1346