blob: 7f327121e6d7c43416a3ca6820cd39111796d683 [file] [log] [blame]
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Robin Holta374c572009-04-13 14:40:18 -07006 * Copyright (c) 2004-2009 Silicon Graphics, Inc. All Rights Reserved.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07007 */
8
Dean Nelson89eb8eb2005-03-23 19:50:00 -07009/*
10 * Cross Partition Communication (XPC) support - standard version.
11 *
12 * XPC provides a message passing capability that crosses partition
13 * boundaries. This module is made up of two parts:
14 *
15 * partition This part detects the presence/absence of other
16 * partitions. It provides a heartbeat and monitors
17 * the heartbeats of other partitions.
18 *
19 * channel This part manages the channels and sends/receives
20 * messages across them to/from other partitions.
21 *
22 * There are a couple of additional functions residing in XP, which
23 * provide an interface to XPC for its users.
24 *
25 *
26 * Caveats:
27 *
Dean Nelson7fb5e592008-07-29 22:34:10 -070028 * . Currently on sn2, we have no way to determine which nasid an IRQ
Dean Nelsonc39838c2008-07-29 22:34:11 -070029 * came from. Thus, xpc_send_IRQ_sn2() does a remote amo write
30 * followed by an IPI. The amo indicates where data is to be pulled
31 * from, so after the IPI arrives, the remote partition checks the amo
32 * word. The IPI can actually arrive before the amo however, so other
33 * code must periodically check for this case. Also, remote amo
Dean Nelson7fb5e592008-07-29 22:34:10 -070034 * operations do not reliably time out. Thus we do a remote PIO read
35 * solely to know whether the remote partition is down and whether we
36 * should stop sending IPIs to it. This remote PIO read operation is
37 * set up in a special nofault region so SAL knows to ignore (and
Dean Nelsonc39838c2008-07-29 22:34:11 -070038 * cleanup) any errors due to the remote amo write, PIO read, and/or
Dean Nelson7fb5e592008-07-29 22:34:10 -070039 * PIO write operations.
Dean Nelson89eb8eb2005-03-23 19:50:00 -070040 *
41 * If/when new hardware solves this IPI problem, we should abandon
42 * the current approach.
43 *
44 */
45
Dean Nelson89eb8eb2005-03-23 19:50:00 -070046#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Dean Nelson261f3b42008-07-29 22:34:16 -070048#include <linux/sysctl.h>
49#include <linux/device.h>
Nishanth Aravamudan69913922005-07-08 17:10:00 -070050#include <linux/delay.h>
Dean Nelsona607c382005-09-01 14:01:37 -050051#include <linux/reboot.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070052#include <linux/kdebug.h>
Dean Nelson2c2b94f2008-04-22 14:50:17 -050053#include <linux/kthread.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050054#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070055
Robin Holt891348c2012-12-20 15:05:50 -080056#ifdef CONFIG_X86_64
57#include <asm/traps.h>
58#endif
59
Dean Nelson89eb8eb2005-03-23 19:50:00 -070060/* define two XPC debug device structures to be used with dev_dbg() et al */
61
62struct device_driver xpc_dbg_name = {
63 .name = "xpc"
64};
65
66struct device xpc_part_dbg_subname = {
Kay Sieversbb0dc432009-01-06 10:44:37 -080067 .init_name = "", /* set to "part" at xpc_init() time */
Dean Nelson89eb8eb2005-03-23 19:50:00 -070068 .driver = &xpc_dbg_name
69};
70
71struct device xpc_chan_dbg_subname = {
Kay Sieversbb0dc432009-01-06 10:44:37 -080072 .init_name = "", /* set to "chan" at xpc_init() time */
Dean Nelson89eb8eb2005-03-23 19:50:00 -070073 .driver = &xpc_dbg_name
74};
75
76struct device *xpc_part = &xpc_part_dbg_subname;
77struct device *xpc_chan = &xpc_chan_dbg_subname;
78
Dean Nelson1f4674b2006-01-10 11:08:00 -060079static int xpc_kdebug_ignore;
80
Dean Nelson89eb8eb2005-03-23 19:50:00 -070081/* systune related variables for /proc/sys directories */
82
Dean Nelsona607c382005-09-01 14:01:37 -050083static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
84static int xpc_hb_min_interval = 1;
85static int xpc_hb_max_interval = 10;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070086
Dean Nelsona607c382005-09-01 14:01:37 -050087static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
88static int xpc_hb_check_min_interval = 10;
89static int xpc_hb_check_max_interval = 120;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070090
Dean Nelsona47d5da2008-07-29 22:34:09 -070091int xpc_disengage_timelimit = XPC_DISENGAGE_DEFAULT_TIMELIMIT;
92static int xpc_disengage_min_timelimit; /* = 0 */
93static int xpc_disengage_max_timelimit = 120;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070094
Joe Perchesf0b76552013-06-13 19:37:39 -070095static struct ctl_table xpc_sys_xpc_hb_dir[] = {
Dean Nelson89eb8eb2005-03-23 19:50:00 -070096 {
Dean Nelson35190502008-04-22 14:48:55 -050097 .procname = "hb_interval",
98 .data = &xpc_hb_interval,
99 .maxlen = sizeof(int),
100 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800101 .proc_handler = proc_dointvec_minmax,
Dean Nelson35190502008-04-22 14:48:55 -0500102 .extra1 = &xpc_hb_min_interval,
103 .extra2 = &xpc_hb_max_interval},
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700104 {
Dean Nelson35190502008-04-22 14:48:55 -0500105 .procname = "hb_check_interval",
106 .data = &xpc_hb_check_interval,
107 .maxlen = sizeof(int),
108 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800109 .proc_handler = proc_dointvec_minmax,
Dean Nelson35190502008-04-22 14:48:55 -0500110 .extra1 = &xpc_hb_check_min_interval,
111 .extra2 = &xpc_hb_check_max_interval},
Eric W. Biederman68cbf072007-02-14 00:33:41 -0800112 {}
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700113};
Joe Perchesf0b76552013-06-13 19:37:39 -0700114static struct ctl_table xpc_sys_xpc_dir[] = {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700115 {
Dean Nelson35190502008-04-22 14:48:55 -0500116 .procname = "hb",
117 .mode = 0555,
118 .child = xpc_sys_xpc_hb_dir},
Dean Nelsone54af722005-10-25 14:07:43 -0500119 {
Dean Nelsona47d5da2008-07-29 22:34:09 -0700120 .procname = "disengage_timelimit",
121 .data = &xpc_disengage_timelimit,
Dean Nelson35190502008-04-22 14:48:55 -0500122 .maxlen = sizeof(int),
123 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec_minmax,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700125 .extra1 = &xpc_disengage_min_timelimit,
126 .extra2 = &xpc_disengage_max_timelimit},
Eric W. Biederman68cbf072007-02-14 00:33:41 -0800127 {}
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700128};
Joe Perchesf0b76552013-06-13 19:37:39 -0700129static struct ctl_table xpc_sys_dir[] = {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700130 {
Dean Nelson35190502008-04-22 14:48:55 -0500131 .procname = "xpc",
132 .mode = 0555,
133 .child = xpc_sys_xpc_dir},
Eric W. Biederman68cbf072007-02-14 00:33:41 -0800134 {}
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700135};
136static struct ctl_table_header *xpc_sysctl;
137
Dean Nelsona47d5da2008-07-29 22:34:09 -0700138/* non-zero if any remote partition disengage was timed out */
139int xpc_disengage_timedout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700140
Dean Nelson5b8669d2008-07-29 22:34:18 -0700141/* #of activate IRQs received and not yet processed */
142int xpc_activate_IRQ_rcvd;
143DEFINE_SPINLOCK(xpc_activate_IRQ_rcvd_lock);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700144
145/* IRQ handler notifies this wait queue on receipt of an IRQ */
Dean Nelson6e410172008-07-29 22:34:09 -0700146DECLARE_WAIT_QUEUE_HEAD(xpc_activate_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700147
148static unsigned long xpc_hb_check_timeout;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700149static struct timer_list xpc_hb_timer;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700150
Dean Nelsone54af722005-10-25 14:07:43 -0500151/* notification that the xpc_hb_checker thread has exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500152static DECLARE_COMPLETION(xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700153
Dean Nelsone54af722005-10-25 14:07:43 -0500154/* notification that the xpc_discovery thread has exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500155static DECLARE_COMPLETION(xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700156
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700157static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
158
Dean Nelsona607c382005-09-01 14:01:37 -0500159static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
160static struct notifier_block xpc_reboot_notifier = {
161 .notifier_call = xpc_system_reboot,
162};
163
Dean Nelson780d09e2005-11-09 14:41:57 -0600164static int xpc_system_die(struct notifier_block *, unsigned long, void *);
165static struct notifier_block xpc_die_notifier = {
166 .notifier_call = xpc_system_die,
167};
168
Robin Holta7665b02009-04-13 14:40:19 -0700169struct xpc_arch_operations xpc_arch_ops;
Dean Nelson94bd2702008-07-29 22:34:05 -0700170
Dean Nelsona607c382005-09-01 14:01:37 -0500171/*
Dean Nelsona47d5da2008-07-29 22:34:09 -0700172 * Timer function to enforce the timelimit on the partition disengage.
Dean Nelsona607c382005-09-01 14:01:37 -0500173 */
174static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700175xpc_timeout_partition_disengage(unsigned long data)
Dean Nelsona607c382005-09-01 14:01:37 -0500176{
Dean Nelson35190502008-04-22 14:48:55 -0500177 struct xpc_partition *part = (struct xpc_partition *)data;
Dean Nelsona607c382005-09-01 14:01:37 -0500178
Dean Nelsona47d5da2008-07-29 22:34:09 -0700179 DBUG_ON(time_is_after_jiffies(part->disengage_timeout));
Dean Nelsona607c382005-09-01 14:01:37 -0500180
Dean Nelson35190502008-04-22 14:48:55 -0500181 (void)xpc_partition_disengaged(part);
Dean Nelsona607c382005-09-01 14:01:37 -0500182
Dean Nelsona47d5da2008-07-29 22:34:09 -0700183 DBUG_ON(part->disengage_timeout != 0);
Robin Holta7665b02009-04-13 14:40:19 -0700184 DBUG_ON(xpc_arch_ops.partition_engaged(XPC_PARTID(part)));
Dean Nelsona607c382005-09-01 14:01:37 -0500185}
186
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700187/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700188 * Timer to produce the heartbeat. The timer structures function is
189 * already set when this is initially called. A tunable is used to
190 * specify when the next timeout should occur.
191 */
192static void
193xpc_hb_beater(unsigned long dummy)
194{
Robin Holta7665b02009-04-13 14:40:19 -0700195 xpc_arch_ops.increment_heartbeat();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700196
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700197 if (time_is_before_eq_jiffies(xpc_hb_check_timeout))
Dean Nelson6e410172008-07-29 22:34:09 -0700198 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700199
200 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
201 add_timer(&xpc_hb_timer);
202}
203
Dean Nelson33ba3c72008-07-29 22:34:07 -0700204static void
205xpc_start_hb_beater(void)
206{
Robin Holta7665b02009-04-13 14:40:19 -0700207 xpc_arch_ops.heartbeat_init();
Dean Nelson33ba3c72008-07-29 22:34:07 -0700208 init_timer(&xpc_hb_timer);
209 xpc_hb_timer.function = xpc_hb_beater;
210 xpc_hb_beater(0);
211}
212
213static void
214xpc_stop_hb_beater(void)
215{
216 del_timer_sync(&xpc_hb_timer);
Robin Holta7665b02009-04-13 14:40:19 -0700217 xpc_arch_ops.heartbeat_exit();
Dean Nelson33ba3c72008-07-29 22:34:07 -0700218}
219
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700220/*
Dean Nelson61deb862008-07-29 22:34:17 -0700221 * At periodic intervals, scan through all active partitions and ensure
222 * their heartbeat is still active. If not, the partition is deactivated.
223 */
224static void
225xpc_check_remote_hb(void)
226{
227 struct xpc_partition *part;
228 short partid;
229 enum xp_retval ret;
230
231 for (partid = 0; partid < xp_max_npartitions; partid++) {
232
233 if (xpc_exiting)
234 break;
235
236 if (partid == xp_partition_id)
237 continue;
238
239 part = &xpc_partitions[partid];
240
Dean Nelson83469b52008-07-29 22:34:18 -0700241 if (part->act_state == XPC_P_AS_INACTIVE ||
242 part->act_state == XPC_P_AS_DEACTIVATING) {
Dean Nelson61deb862008-07-29 22:34:17 -0700243 continue;
244 }
245
Robin Holta7665b02009-04-13 14:40:19 -0700246 ret = xpc_arch_ops.get_remote_heartbeat(part);
Dean Nelson61deb862008-07-29 22:34:17 -0700247 if (ret != xpSuccess)
248 XPC_DEACTIVATE_PARTITION(part, ret);
249 }
250}
251
252/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700253 * This thread is responsible for nearly all of the partition
254 * activation/deactivation.
255 */
256static int
257xpc_hb_checker(void *ignore)
258{
Dean Nelson35190502008-04-22 14:48:55 -0500259 int force_IRQ = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700260
261 /* this thread was marked active by xpc_hb_init() */
262
Rusty Russellf7df8ed2009-01-10 21:58:09 -0800263 set_cpus_allowed_ptr(current, cpumask_of(XPC_HB_CHECK_CPU));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700264
Dean Nelson4c013f52007-11-07 07:53:06 -0600265 /* set our heartbeating to other partitions into motion */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700266 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700267 xpc_start_hb_beater();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700268
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500269 while (!xpc_exiting) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700270
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700271 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
272 "been received\n",
Dean Nelson35190502008-04-22 14:48:55 -0500273 (int)(xpc_hb_check_timeout - jiffies),
Dean Nelson5b8669d2008-07-29 22:34:18 -0700274 xpc_activate_IRQ_rcvd);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700275
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700276 /* checking of remote heartbeats is skewed by IRQ handling */
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700277 if (time_is_before_eq_jiffies(xpc_hb_check_timeout)) {
Dean Nelson5b8669d2008-07-29 22:34:18 -0700278 xpc_hb_check_timeout = jiffies +
279 (xpc_hb_check_interval * HZ);
280
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700281 dev_dbg(xpc_part, "checking remote heartbeats\n");
282 xpc_check_remote_hb();
283
284 /*
Dean Nelson5b8669d2008-07-29 22:34:18 -0700285 * On sn2 we need to periodically recheck to ensure no
286 * IRQ/amo pairs have been missed.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700287 */
Dean Nelson5b8669d2008-07-29 22:34:18 -0700288 if (is_shub())
289 force_IRQ = 1;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700290 }
291
Dean Nelsona607c382005-09-01 14:01:37 -0500292 /* check for outstanding IRQs */
Dean Nelson5b8669d2008-07-29 22:34:18 -0700293 if (xpc_activate_IRQ_rcvd > 0 || force_IRQ != 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700294 force_IRQ = 0;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700295 dev_dbg(xpc_part, "processing activate IRQs "
296 "received\n");
Robin Holta7665b02009-04-13 14:40:19 -0700297 xpc_arch_ops.process_activate_IRQ_rcvd();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700298 }
Dean Nelsona607c382005-09-01 14:01:37 -0500299
300 /* wait for IRQ or timeout */
Dean Nelson6e410172008-07-29 22:34:09 -0700301 (void)wait_event_interruptible(xpc_activate_IRQ_wq,
Dean Nelson5b8669d2008-07-29 22:34:18 -0700302 (time_is_before_eq_jiffies(
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700303 xpc_hb_check_timeout) ||
Dean Nelson5b8669d2008-07-29 22:34:18 -0700304 xpc_activate_IRQ_rcvd > 0 ||
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500305 xpc_exiting));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700306 }
307
Dean Nelson33ba3c72008-07-29 22:34:07 -0700308 xpc_stop_hb_beater();
309
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700310 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
311
Dean Nelsone54af722005-10-25 14:07:43 -0500312 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500313 complete(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700314 return 0;
315}
316
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700317/*
318 * This thread will attempt to discover other partitions to activate
319 * based on info provided by SAL. This new thread is short lived and
320 * will exit once discovery is complete.
321 */
322static int
323xpc_initiate_discovery(void *ignore)
324{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700325 xpc_discovery();
326
327 dev_dbg(xpc_part, "discovery thread is exiting\n");
328
Dean Nelsone54af722005-10-25 14:07:43 -0500329 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500330 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700331 return 0;
332}
333
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700334/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700335 * The first kthread assigned to a newly activated partition is the one
Dean Nelsone17d4162008-07-29 22:34:06 -0700336 * created by XPC HB with which it calls xpc_activating(). XPC hangs on to
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700337 * that kthread until the partition is brought down, at which time that kthread
338 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
339 * that XPC has dismantled all communication infrastructure for the associated
340 * partition.) This kthread becomes the channel manager for that partition.
341 *
342 * Each active partition has a channel manager, who, besides connecting and
343 * disconnecting channels, will ensure that each of the partition's connected
344 * channels has the required number of assigned kthreads to get the work done.
345 */
346static void
347xpc_channel_mgr(struct xpc_partition *part)
348{
Dean Nelson83469b52008-07-29 22:34:18 -0700349 while (part->act_state != XPC_P_AS_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500350 atomic_read(&part->nchannels_active) > 0 ||
351 !xpc_partition_disengaged(part)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700352
Dean Nelson7fb5e592008-07-29 22:34:10 -0700353 xpc_process_sent_chctl_flags(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700354
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700355 /*
356 * Wait until we've been requested to activate kthreads or
357 * all of the channel's message queues have been torn down or
358 * a signal is pending.
359 *
360 * The channel_mgr_requests is set to 1 after being awakened,
361 * This is done to prevent the channel mgr from making one pass
362 * through the loop for each request, since he will
363 * be servicing all the requests in one pass. The reason it's
364 * set to 1 instead of 0 is so that other kthreads will know
365 * that the channel mgr is running and won't bother trying to
366 * wake him up.
367 */
368 atomic_dec(&part->channel_mgr_requests);
Dean Nelson35190502008-04-22 14:48:55 -0500369 (void)wait_event_interruptible(part->channel_mgr_wq,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500370 (atomic_read(&part->channel_mgr_requests) > 0 ||
Dean Nelson7fb5e592008-07-29 22:34:10 -0700371 part->chctl.all_flags != 0 ||
Dean Nelson83469b52008-07-29 22:34:18 -0700372 (part->act_state == XPC_P_AS_DEACTIVATING &&
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500373 atomic_read(&part->nchannels_active) == 0 &&
374 xpc_partition_disengaged(part))));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700375 atomic_set(&part->channel_mgr_requests, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700376 }
377}
378
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700379/*
Dean Nelson5b8669d2008-07-29 22:34:18 -0700380 * Guarantee that the kzalloc'd memory is cacheline aligned.
381 */
382void *
383xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
384{
385 /* see if kzalloc will give us cachline aligned memory by default */
386 *base = kzalloc(size, flags);
387 if (*base == NULL)
388 return NULL;
389
390 if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
391 return *base;
392
393 kfree(*base);
394
395 /* nope, we'll have to do it ourselves */
396 *base = kzalloc(size + L1_CACHE_BYTES, flags);
397 if (*base == NULL)
398 return NULL;
399
400 return (void *)L1_CACHE_ALIGN((u64)*base);
401}
402
403/*
404 * Setup the channel structures necessary to support XPartition Communication
405 * between the specified remote partition and the local one.
406 */
407static enum xp_retval
408xpc_setup_ch_structures(struct xpc_partition *part)
409{
410 enum xp_retval ret;
411 int ch_number;
412 struct xpc_channel *ch;
413 short partid = XPC_PARTID(part);
414
415 /*
416 * Allocate all of the channel structures as a contiguous chunk of
417 * memory.
418 */
419 DBUG_ON(part->channels != NULL);
420 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
421 GFP_KERNEL);
422 if (part->channels == NULL) {
423 dev_err(xpc_chan, "can't get memory for channels\n");
424 return xpNoMemory;
425 }
426
427 /* allocate the remote open and close args */
428
429 part->remote_openclose_args =
430 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE,
431 GFP_KERNEL, &part->
432 remote_openclose_args_base);
433 if (part->remote_openclose_args == NULL) {
434 dev_err(xpc_chan, "can't get memory for remote connect args\n");
435 ret = xpNoMemory;
436 goto out_1;
437 }
438
439 part->chctl.all_flags = 0;
440 spin_lock_init(&part->chctl_lock);
441
442 atomic_set(&part->channel_mgr_requests, 1);
443 init_waitqueue_head(&part->channel_mgr_wq);
444
445 part->nchannels = XPC_MAX_NCHANNELS;
446
447 atomic_set(&part->nchannels_active, 0);
448 atomic_set(&part->nchannels_engaged, 0);
449
450 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
451 ch = &part->channels[ch_number];
452
453 ch->partid = partid;
454 ch->number = ch_number;
455 ch->flags = XPC_C_DISCONNECTED;
456
457 atomic_set(&ch->kthreads_assigned, 0);
458 atomic_set(&ch->kthreads_idle, 0);
459 atomic_set(&ch->kthreads_active, 0);
460
461 atomic_set(&ch->references, 0);
462 atomic_set(&ch->n_to_notify, 0);
463
464 spin_lock_init(&ch->lock);
465 init_completion(&ch->wdisconnect_wait);
466
467 atomic_set(&ch->n_on_msg_allocate_wq, 0);
468 init_waitqueue_head(&ch->msg_allocate_wq);
469 init_waitqueue_head(&ch->idle_wq);
470 }
471
Robin Holta7665b02009-04-13 14:40:19 -0700472 ret = xpc_arch_ops.setup_ch_structures(part);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700473 if (ret != xpSuccess)
474 goto out_2;
475
476 /*
477 * With the setting of the partition setup_state to XPC_P_SS_SETUP,
478 * we're declaring that this partition is ready to go.
479 */
480 part->setup_state = XPC_P_SS_SETUP;
481
482 return xpSuccess;
483
484 /* setup of ch structures failed */
485out_2:
486 kfree(part->remote_openclose_args_base);
487 part->remote_openclose_args = NULL;
488out_1:
489 kfree(part->channels);
490 part->channels = NULL;
491 return ret;
492}
493
494/*
495 * Teardown the channel structures necessary to support XPartition Communication
496 * between the specified remote partition and the local one.
497 */
498static void
499xpc_teardown_ch_structures(struct xpc_partition *part)
500{
501 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
502 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
503
504 /*
505 * Make this partition inaccessible to local processes by marking it
506 * as no longer setup. Then wait before proceeding with the teardown
507 * until all existing references cease.
508 */
509 DBUG_ON(part->setup_state != XPC_P_SS_SETUP);
510 part->setup_state = XPC_P_SS_WTEARDOWN;
511
512 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
513
514 /* now we can begin tearing down the infrastructure */
515
Robin Holta7665b02009-04-13 14:40:19 -0700516 xpc_arch_ops.teardown_ch_structures(part);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700517
518 kfree(part->remote_openclose_args_base);
519 part->remote_openclose_args = NULL;
520 kfree(part->channels);
521 part->channels = NULL;
522
523 part->setup_state = XPC_P_SS_TORNDOWN;
524}
525
526/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700527 * When XPC HB determines that a partition has come up, it will create a new
528 * kthread and that kthread will call this function to attempt to set up the
529 * basic infrastructure used for Cross Partition Communication with the newly
530 * upped partition.
531 *
532 * The kthread that was created by XPC HB and which setup the XPC
Dean Nelsone17d4162008-07-29 22:34:06 -0700533 * infrastructure will remain assigned to the partition becoming the channel
534 * manager for that partition until the partition is deactivating, at which
535 * time the kthread will teardown the XPC infrastructure and then exit.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700536 */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700537static int
538xpc_activating(void *__partid)
539{
Dean Nelson64d032b2008-05-12 14:02:03 -0700540 short partid = (u64)__partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700541 struct xpc_partition *part = &xpc_partitions[partid];
542 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700543
Dean Nelsonbc63d382008-07-29 22:34:04 -0700544 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700545
546 spin_lock_irqsave(&part->act_lock, irq_flags);
547
Dean Nelson83469b52008-07-29 22:34:18 -0700548 if (part->act_state == XPC_P_AS_DEACTIVATING) {
549 part->act_state = XPC_P_AS_INACTIVE;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700550 spin_unlock_irqrestore(&part->act_lock, irq_flags);
551 part->remote_rp_pa = 0;
552 return 0;
553 }
554
555 /* indicate the thread is activating */
Dean Nelson83469b52008-07-29 22:34:18 -0700556 DBUG_ON(part->act_state != XPC_P_AS_ACTIVATION_REQ);
557 part->act_state = XPC_P_AS_ACTIVATING;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700558
559 XPC_SET_REASON(part, 0, 0);
560 spin_unlock_irqrestore(&part->act_lock, irq_flags);
561
Dean Nelsone17d4162008-07-29 22:34:06 -0700562 dev_dbg(xpc_part, "activating partition %d\n", partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700563
Robin Holta7665b02009-04-13 14:40:19 -0700564 xpc_arch_ops.allow_hb(partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700565
Dean Nelson5b8669d2008-07-29 22:34:18 -0700566 if (xpc_setup_ch_structures(part) == xpSuccess) {
Dean Nelsone17d4162008-07-29 22:34:06 -0700567 (void)xpc_part_ref(part); /* this will always succeed */
568
Robin Holta7665b02009-04-13 14:40:19 -0700569 if (xpc_arch_ops.make_first_contact(part) == xpSuccess) {
Dean Nelsone17d4162008-07-29 22:34:06 -0700570 xpc_mark_partition_active(part);
571 xpc_channel_mgr(part);
572 /* won't return until partition is deactivating */
573 }
574
575 xpc_part_deref(part);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700576 xpc_teardown_ch_structures(part);
Dean Nelsone17d4162008-07-29 22:34:06 -0700577 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700578
Robin Holta7665b02009-04-13 14:40:19 -0700579 xpc_arch_ops.disallow_hb(partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700580 xpc_mark_partition_inactive(part);
581
Dean Nelson65c17b82008-05-12 14:02:02 -0700582 if (part->reason == xpReactivating) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700583 /* interrupting ourselves results in activating partition */
Robin Holta7665b02009-04-13 14:40:19 -0700584 xpc_arch_ops.request_partition_reactivation(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700585 }
586
587 return 0;
588}
589
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700590void
591xpc_activate_partition(struct xpc_partition *part)
592{
Dean Nelson64d032b2008-05-12 14:02:03 -0700593 short partid = XPC_PARTID(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700594 unsigned long irq_flags;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500595 struct task_struct *kthread;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700596
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700597 spin_lock_irqsave(&part->act_lock, irq_flags);
598
Dean Nelson83469b52008-07-29 22:34:18 -0700599 DBUG_ON(part->act_state != XPC_P_AS_INACTIVE);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700600
Dean Nelson83469b52008-07-29 22:34:18 -0700601 part->act_state = XPC_P_AS_ACTIVATION_REQ;
Dean Nelson65c17b82008-05-12 14:02:02 -0700602 XPC_SET_REASON(part, xpCloneKThread, __LINE__);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700603
604 spin_unlock_irqrestore(&part->act_lock, irq_flags);
Robin Holt7c6c6632006-02-02 12:30:21 -0600605
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500606 kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
607 partid);
608 if (IS_ERR(kthread)) {
Robin Holt7c6c6632006-02-02 12:30:21 -0600609 spin_lock_irqsave(&part->act_lock, irq_flags);
Dean Nelson83469b52008-07-29 22:34:18 -0700610 part->act_state = XPC_P_AS_INACTIVE;
Dean Nelson65c17b82008-05-12 14:02:02 -0700611 XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
Robin Holt7c6c6632006-02-02 12:30:21 -0600612 spin_unlock_irqrestore(&part->act_lock, irq_flags);
613 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700614}
615
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700616void
617xpc_activate_kthreads(struct xpc_channel *ch, int needed)
618{
619 int idle = atomic_read(&ch->kthreads_idle);
620 int assigned = atomic_read(&ch->kthreads_assigned);
621 int wakeup;
622
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700623 DBUG_ON(needed <= 0);
624
625 if (idle > 0) {
626 wakeup = (needed > idle) ? idle : needed;
627 needed -= wakeup;
628
629 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
630 "channel=%d\n", wakeup, ch->partid, ch->number);
631
632 /* only wakeup the requested number of kthreads */
633 wake_up_nr(&ch->idle_wq, wakeup);
634 }
635
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500636 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700637 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700638
639 if (needed + assigned > ch->kthreads_assigned_limit) {
640 needed = ch->kthreads_assigned_limit - assigned;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500641 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700642 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700643 }
644
645 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
646 needed, ch->partid, ch->number);
647
Dean Nelsona460ef82006-11-22 08:25:00 -0600648 xpc_create_kthreads(ch, needed, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700649}
650
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700651/*
652 * This function is where XPC's kthreads wait for messages to deliver.
653 */
654static void
655xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
656{
Robin Holta7665b02009-04-13 14:40:19 -0700657 int (*n_of_deliverable_payloads) (struct xpc_channel *) =
658 xpc_arch_ops.n_of_deliverable_payloads;
659
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700660 do {
661 /* deliver messages to their intended recipients */
662
Robin Holta7665b02009-04-13 14:40:19 -0700663 while (n_of_deliverable_payloads(ch) > 0 &&
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500664 !(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700665 xpc_deliver_payload(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700666 }
667
668 if (atomic_inc_return(&ch->kthreads_idle) >
Dean Nelson35190502008-04-22 14:48:55 -0500669 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700670 /* too many idle kthreads on this channel */
671 atomic_dec(&ch->kthreads_idle);
672 break;
673 }
674
675 dev_dbg(xpc_chan, "idle kthread calling "
676 "wait_event_interruptible_exclusive()\n");
677
Dean Nelson35190502008-04-22 14:48:55 -0500678 (void)wait_event_interruptible_exclusive(ch->idle_wq,
Robin Holta7665b02009-04-13 14:40:19 -0700679 (n_of_deliverable_payloads(ch) > 0 ||
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500680 (ch->flags & XPC_C_DISCONNECTING)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700681
682 atomic_dec(&ch->kthreads_idle);
683
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500684 } while (!(ch->flags & XPC_C_DISCONNECTING));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700685}
686
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700687static int
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500688xpc_kthread_start(void *args)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700689{
Dean Nelson64d032b2008-05-12 14:02:03 -0700690 short partid = XPC_UNPACK_ARG1(args);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700691 u16 ch_number = XPC_UNPACK_ARG2(args);
692 struct xpc_partition *part = &xpc_partitions[partid];
693 struct xpc_channel *ch;
694 int n_needed;
Dean Nelsone54af722005-10-25 14:07:43 -0500695 unsigned long irq_flags;
Robin Holta7665b02009-04-13 14:40:19 -0700696 int (*n_of_deliverable_payloads) (struct xpc_channel *) =
697 xpc_arch_ops.n_of_deliverable_payloads;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700698
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700699 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
700 partid, ch_number);
701
702 ch = &part->channels[ch_number];
703
704 if (!(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700705
706 /* let registerer know that connection has been established */
707
Dean Nelsone54af722005-10-25 14:07:43 -0500708 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson4c2cd962006-02-15 08:02:21 -0600709 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
710 ch->flags |= XPC_C_CONNECTEDCALLOUT;
Dean Nelsone54af722005-10-25 14:07:43 -0500711 spin_unlock_irqrestore(&ch->lock, irq_flags);
712
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700713 xpc_connected_callout(ch);
714
Dean Nelson4c2cd962006-02-15 08:02:21 -0600715 spin_lock_irqsave(&ch->lock, irq_flags);
716 ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
717 spin_unlock_irqrestore(&ch->lock, irq_flags);
718
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700719 /*
720 * It is possible that while the callout was being
721 * made that the remote partition sent some messages.
722 * If that is the case, we may need to activate
723 * additional kthreads to help deliver them. We only
724 * need one less than total #of messages to deliver.
725 */
Robin Holta7665b02009-04-13 14:40:19 -0700726 n_needed = n_of_deliverable_payloads(ch) - 1;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500727 if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700728 xpc_activate_kthreads(ch, n_needed);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500729
Dean Nelsone54af722005-10-25 14:07:43 -0500730 } else {
731 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700732 }
733
734 xpc_kthread_waitmsgs(part, ch);
735 }
736
Dean Nelsona460ef82006-11-22 08:25:00 -0600737 /* let registerer know that connection is disconnecting */
Dean Nelsone54af722005-10-25 14:07:43 -0500738
Dean Nelsona460ef82006-11-22 08:25:00 -0600739 spin_lock_irqsave(&ch->lock, irq_flags);
740 if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500741 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelsona460ef82006-11-22 08:25:00 -0600742 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
Dean Nelson4c2cd962006-02-15 08:02:21 -0600743 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -0600744
Dean Nelson65c17b82008-05-12 14:02:02 -0700745 xpc_disconnect_callout(ch, xpDisconnecting);
Dean Nelsona460ef82006-11-22 08:25:00 -0600746
747 spin_lock_irqsave(&ch->lock, irq_flags);
748 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
749 }
750 spin_unlock_irqrestore(&ch->lock, irq_flags);
751
Dean Nelsona47d5da2008-07-29 22:34:09 -0700752 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
753 atomic_dec_return(&part->nchannels_engaged) == 0) {
Robin Holta7665b02009-04-13 14:40:19 -0700754 xpc_arch_ops.indicate_partition_disengaged(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700755 }
756
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700757 xpc_msgqueue_deref(ch);
758
759 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
760 partid, ch_number);
761
762 xpc_part_deref(part);
763 return 0;
764}
765
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700766/*
767 * For each partition that XPC has established communications with, there is
768 * a minimum of one kernel thread assigned to perform any operation that
769 * may potentially sleep or block (basically the callouts to the asynchronous
770 * functions registered via xpc_connect()).
771 *
772 * Additional kthreads are created and destroyed by XPC as the workload
773 * demands.
774 *
775 * A kthread is assigned to one of the active channels that exists for a given
776 * partition.
777 */
778void
Dean Nelsona460ef82006-11-22 08:25:00 -0600779xpc_create_kthreads(struct xpc_channel *ch, int needed,
Dean Nelson35190502008-04-22 14:48:55 -0500780 int ignore_disconnecting)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700781{
782 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700783 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
Dean Nelsona607c382005-09-01 14:01:37 -0500784 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500785 struct task_struct *kthread;
Robin Holta7665b02009-04-13 14:40:19 -0700786 void (*indicate_partition_disengaged) (struct xpc_partition *) =
787 xpc_arch_ops.indicate_partition_disengaged;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700788
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700789 while (needed-- > 0) {
Dean Nelsone54af722005-10-25 14:07:43 -0500790
791 /*
792 * The following is done on behalf of the newly created
793 * kthread. That kthread is responsible for doing the
794 * counterpart to the following before it exits.
795 */
Dean Nelsona460ef82006-11-22 08:25:00 -0600796 if (ignore_disconnecting) {
797 if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
798 /* kthreads assigned had gone to zero */
799 BUG_ON(!(ch->flags &
Dean Nelson35190502008-04-22 14:48:55 -0500800 XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelsona460ef82006-11-22 08:25:00 -0600801 break;
802 }
803
804 } else if (ch->flags & XPC_C_DISCONNECTING) {
805 break;
806
Dean Nelsona47d5da2008-07-29 22:34:09 -0700807 } else if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
808 atomic_inc_return(&part->nchannels_engaged) == 1) {
Robin Holta7665b02009-04-13 14:40:19 -0700809 xpc_arch_ops.indicate_partition_engaged(part);
Dean Nelsona460ef82006-11-22 08:25:00 -0600810 }
Dean Nelson35190502008-04-22 14:48:55 -0500811 (void)xpc_part_ref(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500812 xpc_msgqueue_ref(ch);
Dean Nelsone54af722005-10-25 14:07:43 -0500813
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500814 kthread = kthread_run(xpc_kthread_start, (void *)args,
815 "xpc%02dc%d", ch->partid, ch->number);
816 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700817 /* the fork failed */
Dean Nelsona460ef82006-11-22 08:25:00 -0600818
819 /*
820 * NOTE: if (ignore_disconnecting &&
821 * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
822 * then we'll deadlock if all other kthreads assigned
823 * to this channel are blocked in the channel's
824 * registerer, because the only thing that will unblock
Dean Nelson65c17b82008-05-12 14:02:02 -0700825 * them is the xpDisconnecting callout that this
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500826 * failed kthread_run() would have made.
Dean Nelsona460ef82006-11-22 08:25:00 -0600827 */
828
Dean Nelsone54af722005-10-25 14:07:43 -0500829 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
830 atomic_dec_return(&part->nchannels_engaged) == 0) {
Robin Holta7665b02009-04-13 14:40:19 -0700831 indicate_partition_disengaged(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500832 }
833 xpc_msgqueue_deref(ch);
834 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700835
836 if (atomic_read(&ch->kthreads_assigned) <
Dean Nelson35190502008-04-22 14:48:55 -0500837 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700838 /*
839 * Flag this as an error only if we have an
840 * insufficient #of kthreads for the channel
841 * to function.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700842 */
843 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700844 XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
Dean Nelson35190502008-04-22 14:48:55 -0500845 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700846 spin_unlock_irqrestore(&ch->lock, irq_flags);
847 }
848 break;
849 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700850 }
851}
852
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700853void
854xpc_disconnect_wait(int ch_number)
855{
Dean Nelsona607c382005-09-01 14:01:37 -0500856 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700857 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700858 struct xpc_partition *part;
859 struct xpc_channel *ch;
Dean Nelsone54af722005-10-25 14:07:43 -0500860 int wakeup_channel_mgr;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700861
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700862 /* now wait for all callouts to the caller's function to cease */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700863 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700864 part = &xpc_partitions[partid];
865
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500866 if (!xpc_part_ref(part))
Dean Nelsone54af722005-10-25 14:07:43 -0500867 continue;
Dean Nelsone54af722005-10-25 14:07:43 -0500868
869 ch = &part->channels[ch_number];
870
871 if (!(ch->flags & XPC_C_WDISCONNECT)) {
872 xpc_part_deref(part);
873 continue;
874 }
875
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500876 wait_for_completion(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500877
878 spin_lock_irqsave(&ch->lock, irq_flags);
879 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
880 wakeup_channel_mgr = 0;
881
Dean Nelson7fb5e592008-07-29 22:34:10 -0700882 if (ch->delayed_chctl_flags) {
Dean Nelson83469b52008-07-29 22:34:18 -0700883 if (part->act_state != XPC_P_AS_DEACTIVATING) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700884 spin_lock(&part->chctl_lock);
885 part->chctl.flags[ch->number] |=
886 ch->delayed_chctl_flags;
887 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500888 wakeup_channel_mgr = 1;
889 }
Dean Nelson7fb5e592008-07-29 22:34:10 -0700890 ch->delayed_chctl_flags = 0;
Dean Nelsone54af722005-10-25 14:07:43 -0500891 }
892
893 ch->flags &= ~XPC_C_WDISCONNECT;
894 spin_unlock_irqrestore(&ch->lock, irq_flags);
895
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500896 if (wakeup_channel_mgr)
Dean Nelsone54af722005-10-25 14:07:43 -0500897 xpc_wakeup_channel_mgr(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500898
899 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700900 }
901}
902
Dean Nelson5b8669d2008-07-29 22:34:18 -0700903static int
904xpc_setup_partitions(void)
905{
906 short partid;
907 struct xpc_partition *part;
908
909 xpc_partitions = kzalloc(sizeof(struct xpc_partition) *
910 xp_max_npartitions, GFP_KERNEL);
911 if (xpc_partitions == NULL) {
912 dev_err(xpc_part, "can't get memory for partition structure\n");
913 return -ENOMEM;
914 }
915
916 /*
917 * The first few fields of each entry of xpc_partitions[] need to
918 * be initialized now so that calls to xpc_connect() and
919 * xpc_disconnect() can be made prior to the activation of any remote
920 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
921 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
922 * PARTITION HAS BEEN ACTIVATED.
923 */
924 for (partid = 0; partid < xp_max_npartitions; partid++) {
925 part = &xpc_partitions[partid];
926
927 DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
928
929 part->activate_IRQ_rcvd = 0;
930 spin_lock_init(&part->act_lock);
931 part->act_state = XPC_P_AS_INACTIVE;
932 XPC_SET_REASON(part, 0, 0);
933
934 init_timer(&part->disengage_timer);
935 part->disengage_timer.function =
936 xpc_timeout_partition_disengage;
937 part->disengage_timer.data = (unsigned long)part;
938
939 part->setup_state = XPC_P_SS_UNSET;
940 init_waitqueue_head(&part->teardown_wq);
941 atomic_set(&part->references, 0);
942 }
943
Robin Holta7665b02009-04-13 14:40:19 -0700944 return xpc_arch_ops.setup_partitions();
Dean Nelson5b8669d2008-07-29 22:34:18 -0700945}
946
947static void
948xpc_teardown_partitions(void)
949{
Robin Holta7665b02009-04-13 14:40:19 -0700950 xpc_arch_ops.teardown_partitions();
Dean Nelson5b8669d2008-07-29 22:34:18 -0700951 kfree(xpc_partitions);
952}
953
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700954static void
Dean Nelson65c17b82008-05-12 14:02:02 -0700955xpc_do_exit(enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700956{
Dean Nelson64d032b2008-05-12 14:02:03 -0700957 short partid;
Dean Nelson1ecaded2006-01-06 09:48:21 -0600958 int active_part_count, printed_waiting_msg = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700959 struct xpc_partition *part;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700960 unsigned long printmsg_time, disengage_timeout = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700961
Dean Nelsona607c382005-09-01 14:01:37 -0500962 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
963 DBUG_ON(xpc_exiting == 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700964
965 /*
Dean Nelsona607c382005-09-01 14:01:37 -0500966 * Let the heartbeat checker thread and the discovery thread
967 * (if one is running) know that they should exit. Also wake up
968 * the heartbeat checker thread in case it's sleeping.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700969 */
970 xpc_exiting = 1;
Dean Nelson6e410172008-07-29 22:34:09 -0700971 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700972
Dean Nelsone54af722005-10-25 14:07:43 -0500973 /* wait for the discovery thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500974 wait_for_completion(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700975
Dean Nelsone54af722005-10-25 14:07:43 -0500976 /* wait for the heartbeat checker thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500977 wait_for_completion(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700978
Dean Nelsona607c382005-09-01 14:01:37 -0500979 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -0500980 (void)msleep_interruptible(300);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700981
982 /* wait for all partitions to become inactive */
983
Dean Nelsona47d5da2008-07-29 22:34:09 -0700984 printmsg_time = jiffies + (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
985 xpc_disengage_timedout = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500986
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700987 do {
988 active_part_count = 0;
989
Dean Nelsonbc63d382008-07-29 22:34:04 -0700990 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700991 part = &xpc_partitions[partid];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700992
Dean Nelsona607c382005-09-01 14:01:37 -0500993 if (xpc_partition_disengaged(part) &&
Dean Nelson83469b52008-07-29 22:34:18 -0700994 part->act_state == XPC_P_AS_INACTIVE) {
Dean Nelsona607c382005-09-01 14:01:37 -0500995 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700996 }
Dean Nelsona607c382005-09-01 14:01:37 -0500997
998 active_part_count++;
999
1000 XPC_DEACTIVATE_PARTITION(part, reason);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001001
Dean Nelsona47d5da2008-07-29 22:34:09 -07001002 if (part->disengage_timeout > disengage_timeout)
1003 disengage_timeout = part->disengage_timeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001004 }
1005
Robin Holta7665b02009-04-13 14:40:19 -07001006 if (xpc_arch_ops.any_partition_engaged()) {
Dean Nelsonaaa3cd62008-07-29 22:34:07 -07001007 if (time_is_before_jiffies(printmsg_time)) {
Dean Nelson1ecaded2006-01-06 09:48:21 -06001008 dev_info(xpc_part, "waiting for remote "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001009 "partitions to deactivate, timeout in "
1010 "%ld seconds\n", (disengage_timeout -
1011 jiffies) / HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001012 printmsg_time = jiffies +
Dean Nelsona47d5da2008-07-29 22:34:09 -07001013 (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001014 printed_waiting_msg = 1;
1015 }
1016
1017 } else if (active_part_count > 0) {
1018 if (printed_waiting_msg) {
1019 dev_info(xpc_part, "waiting for local partition"
Dean Nelsona47d5da2008-07-29 22:34:09 -07001020 " to deactivate\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -06001021 printed_waiting_msg = 0;
1022 }
1023
1024 } else {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001025 if (!xpc_disengage_timedout) {
Dean Nelson1ecaded2006-01-06 09:48:21 -06001026 dev_info(xpc_part, "all partitions have "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001027 "deactivated\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -06001028 }
1029 break;
Dean Nelsona607c382005-09-01 14:01:37 -05001030 }
1031
1032 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -05001033 (void)msleep_interruptible(300);
Dean Nelsona607c382005-09-01 14:01:37 -05001034
1035 } while (1);
1036
Robin Holta7665b02009-04-13 14:40:19 -07001037 DBUG_ON(xpc_arch_ops.any_partition_engaged());
Dean Nelsona607c382005-09-01 14:01:37 -05001038
Dean Nelson5b8669d2008-07-29 22:34:18 -07001039 xpc_teardown_rsvd_page();
Dean Nelsona607c382005-09-01 14:01:37 -05001040
Dean Nelson65c17b82008-05-12 14:02:02 -07001041 if (reason == xpUnloading) {
Dean Nelson35190502008-04-22 14:48:55 -05001042 (void)unregister_die_notifier(&xpc_die_notifier);
Dean Nelsonbc63d382008-07-29 22:34:04 -07001043 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson0752c672006-01-10 11:07:19 -06001044 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001045
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001046 /* clear the interface to XPC's functions */
1047 xpc_clear_interface();
1048
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001049 if (xpc_sysctl)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001050 unregister_sysctl_table(xpc_sysctl);
Dean Nelson7682a4c2006-08-08 15:03:29 -05001051
Dean Nelson5b8669d2008-07-29 22:34:18 -07001052 xpc_teardown_partitions();
Dean Nelson6e410172008-07-29 22:34:09 -07001053
1054 if (is_shub())
1055 xpc_exit_sn2();
Dean Nelsonb7f7b072008-10-29 14:01:12 -07001056 else if (is_uv())
Dean Nelson6e410172008-07-29 22:34:09 -07001057 xpc_exit_uv();
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001058}
1059
Dean Nelsona607c382005-09-01 14:01:37 -05001060/*
Dean Nelsond6ad0332006-01-10 11:08:55 -06001061 * This function is called when the system is being rebooted.
1062 */
1063static int
1064xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
1065{
Dean Nelson65c17b82008-05-12 14:02:02 -07001066 enum xp_retval reason;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001067
Dean Nelsond6ad0332006-01-10 11:08:55 -06001068 switch (event) {
1069 case SYS_RESTART:
Dean Nelson65c17b82008-05-12 14:02:02 -07001070 reason = xpSystemReboot;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001071 break;
1072 case SYS_HALT:
Dean Nelson65c17b82008-05-12 14:02:02 -07001073 reason = xpSystemHalt;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001074 break;
1075 case SYS_POWER_OFF:
Dean Nelson65c17b82008-05-12 14:02:02 -07001076 reason = xpSystemPoweroff;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001077 break;
1078 default:
Dean Nelson65c17b82008-05-12 14:02:02 -07001079 reason = xpSystemGoingDown;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001080 }
1081
1082 xpc_do_exit(reason);
1083 return NOTIFY_DONE;
1084}
1085
Robin Holt891348c2012-12-20 15:05:50 -08001086/* Used to only allow one cpu to complete disconnect */
1087static unsigned int xpc_die_disconnecting;
1088
Dean Nelsond6ad0332006-01-10 11:08:55 -06001089/*
Dean Nelsona47d5da2008-07-29 22:34:09 -07001090 * Notify other partitions to deactivate from us by first disengaging from all
1091 * references to our memory.
Dean Nelson780d09e2005-11-09 14:41:57 -06001092 */
1093static void
Dean Nelsona47d5da2008-07-29 22:34:09 -07001094xpc_die_deactivate(void)
Dean Nelson780d09e2005-11-09 14:41:57 -06001095{
1096 struct xpc_partition *part;
Dean Nelson64d032b2008-05-12 14:02:03 -07001097 short partid;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001098 int any_engaged;
Dean Nelson261f3b42008-07-29 22:34:16 -07001099 long keep_waiting;
1100 long wait_to_print;
Dean Nelson780d09e2005-11-09 14:41:57 -06001101
Robin Holt891348c2012-12-20 15:05:50 -08001102 if (cmpxchg(&xpc_die_disconnecting, 0, 1))
1103 return;
1104
Dean Nelson780d09e2005-11-09 14:41:57 -06001105 /* keep xpc_hb_checker thread from doing anything (just in case) */
1106 xpc_exiting = 1;
1107
Robin Holta7665b02009-04-13 14:40:19 -07001108 xpc_arch_ops.disallow_all_hbs(); /*indicate we're deactivated */
Dean Nelson780d09e2005-11-09 14:41:57 -06001109
Dean Nelsonbc63d382008-07-29 22:34:04 -07001110 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001111 part = &xpc_partitions[partid];
1112
Robin Holta7665b02009-04-13 14:40:19 -07001113 if (xpc_arch_ops.partition_engaged(partid) ||
Dean Nelson83469b52008-07-29 22:34:18 -07001114 part->act_state != XPC_P_AS_INACTIVE) {
Robin Holta7665b02009-04-13 14:40:19 -07001115 xpc_arch_ops.request_partition_deactivation(part);
1116 xpc_arch_ops.indicate_partition_disengaged(part);
Dean Nelson780d09e2005-11-09 14:41:57 -06001117 }
1118 }
1119
Dean Nelsona47d5da2008-07-29 22:34:09 -07001120 /*
1121 * Though we requested that all other partitions deactivate from us,
Dean Nelson261f3b42008-07-29 22:34:16 -07001122 * we only wait until they've all disengaged or we've reached the
1123 * defined timelimit.
1124 *
1125 * Given that one iteration through the following while-loop takes
1126 * approximately 200 microseconds, calculate the #of loops to take
1127 * before bailing and the #of loops before printing a waiting message.
Dean Nelsona47d5da2008-07-29 22:34:09 -07001128 */
Dean Nelson261f3b42008-07-29 22:34:16 -07001129 keep_waiting = xpc_disengage_timelimit * 1000 * 5;
1130 wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL * 1000 * 5;
Dean Nelson780d09e2005-11-09 14:41:57 -06001131
Dean Nelson1ecaded2006-01-06 09:48:21 -06001132 while (1) {
Robin Holta7665b02009-04-13 14:40:19 -07001133 any_engaged = xpc_arch_ops.any_partition_engaged();
Dean Nelsona47d5da2008-07-29 22:34:09 -07001134 if (!any_engaged) {
1135 dev_info(xpc_part, "all partitions have deactivated\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -06001136 break;
1137 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001138
Dean Nelson261f3b42008-07-29 22:34:16 -07001139 if (!keep_waiting--) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001140 for (partid = 0; partid < xp_max_npartitions;
1141 partid++) {
Robin Holta7665b02009-04-13 14:40:19 -07001142 if (xpc_arch_ops.partition_engaged(partid)) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001143 dev_info(xpc_part, "deactivate from "
Dean Nelson35190502008-04-22 14:48:55 -05001144 "remote partition %d timed "
1145 "out\n", partid);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001146 }
1147 }
1148 break;
1149 }
1150
Dean Nelson261f3b42008-07-29 22:34:16 -07001151 if (!wait_to_print--) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001152 dev_info(xpc_part, "waiting for remote partitions to "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001153 "deactivate, timeout in %ld seconds\n",
Dean Nelson261f3b42008-07-29 22:34:16 -07001154 keep_waiting / (1000 * 5));
1155 wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL *
1156 1000 * 5;
Dean Nelson780d09e2005-11-09 14:41:57 -06001157 }
Dean Nelson261f3b42008-07-29 22:34:16 -07001158
1159 udelay(200);
Dean Nelson780d09e2005-11-09 14:41:57 -06001160 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001161}
1162
Dean Nelson780d09e2005-11-09 14:41:57 -06001163/*
Dean Nelson1f4674b2006-01-10 11:08:00 -06001164 * This function is called when the system is being restarted or halted due
1165 * to some sort of system failure. If this is the case we need to notify the
1166 * other partitions to disengage from all references to our memory.
1167 * This function can also be called when our heartbeater could be offlined
1168 * for a time. In this case we need to notify other partitions to not worry
1169 * about the lack of a heartbeat.
Dean Nelson780d09e2005-11-09 14:41:57 -06001170 */
1171static int
Robin Holt891348c2012-12-20 15:05:50 -08001172xpc_system_die(struct notifier_block *nb, unsigned long event, void *_die_args)
Dean Nelson780d09e2005-11-09 14:41:57 -06001173{
Dean Nelson261f3b42008-07-29 22:34:16 -07001174#ifdef CONFIG_IA64 /* !!! temporary kludge */
Dean Nelson780d09e2005-11-09 14:41:57 -06001175 switch (event) {
1176 case DIE_MACHINE_RESTART:
1177 case DIE_MACHINE_HALT:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001178 xpc_die_deactivate();
Dean Nelson780d09e2005-11-09 14:41:57 -06001179 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001180
1181 case DIE_KDEBUG_ENTER:
1182 /* Should lack of heartbeat be ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001183 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001184 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001185
Dean Nelson1f4674b2006-01-10 11:08:00 -06001186 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001187 case DIE_MCA_MONARCH_ENTER:
1188 case DIE_INIT_MONARCH_ENTER:
Robin Holta7665b02009-04-13 14:40:19 -07001189 xpc_arch_ops.offline_heartbeat();
Dean Nelson780d09e2005-11-09 14:41:57 -06001190 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001191
1192 case DIE_KDEBUG_LEAVE:
1193 /* Is lack of heartbeat being ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001194 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001195 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001196
Dean Nelson1f4674b2006-01-10 11:08:00 -06001197 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001198 case DIE_MCA_MONARCH_LEAVE:
1199 case DIE_INIT_MONARCH_LEAVE:
Robin Holta7665b02009-04-13 14:40:19 -07001200 xpc_arch_ops.online_heartbeat();
Dean Nelson780d09e2005-11-09 14:41:57 -06001201 break;
1202 }
Dean Nelson261f3b42008-07-29 22:34:16 -07001203#else
Robin Holt891348c2012-12-20 15:05:50 -08001204 struct die_args *die_args = _die_args;
1205
1206 switch (event) {
1207 case DIE_TRAP:
1208 if (die_args->trapnr == X86_TRAP_DF)
1209 xpc_die_deactivate();
1210
1211 if (((die_args->trapnr == X86_TRAP_MF) ||
1212 (die_args->trapnr == X86_TRAP_XF)) &&
Andy Lutomirskif39b6f02015-03-18 18:33:33 -07001213 !user_mode(die_args->regs))
Robin Holt891348c2012-12-20 15:05:50 -08001214 xpc_die_deactivate();
1215
1216 break;
1217 case DIE_INT3:
1218 case DIE_DEBUG:
1219 break;
1220 case DIE_OOPS:
1221 case DIE_GPF:
1222 default:
1223 xpc_die_deactivate();
1224 }
Dean Nelson261f3b42008-07-29 22:34:16 -07001225#endif
Dean Nelson780d09e2005-11-09 14:41:57 -06001226
1227 return NOTIFY_DONE;
1228}
1229
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001230int __init
1231xpc_init(void)
1232{
1233 int ret;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001234 struct task_struct *kthread;
Dean Nelsonee6665e2008-07-29 22:34:13 -07001235
Kay Sieversbb0dc432009-01-06 10:44:37 -08001236 dev_set_name(xpc_part, "part");
1237 dev_set_name(xpc_chan, "chan");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001238
Dean Nelson94bd2702008-07-29 22:34:05 -07001239 if (is_shub()) {
1240 /*
1241 * The ia64-sn2 architecture supports at most 64 partitions.
Dean Nelsonc39838c2008-07-29 22:34:11 -07001242 * And the inability to unregister remote amos restricts us
Dean Nelson94bd2702008-07-29 22:34:05 -07001243 * further to only support exactly 64 partitions on this
1244 * architecture, no less.
1245 */
Dean Nelson5b8669d2008-07-29 22:34:18 -07001246 if (xp_max_npartitions != 64) {
1247 dev_err(xpc_part, "max #of partitions not set to 64\n");
1248 ret = -EINVAL;
1249 } else {
1250 ret = xpc_init_sn2();
1251 }
Dean Nelson94bd2702008-07-29 22:34:05 -07001252
1253 } else if (is_uv()) {
Dean Nelson5b8669d2008-07-29 22:34:18 -07001254 ret = xpc_init_uv();
Dean Nelson94bd2702008-07-29 22:34:05 -07001255
1256 } else {
Dean Nelson5b8669d2008-07-29 22:34:18 -07001257 ret = -ENODEV;
Dean Nelson94bd2702008-07-29 22:34:05 -07001258 }
Dean Nelson408865c2005-09-08 10:46:58 -05001259
Dean Nelson5b8669d2008-07-29 22:34:18 -07001260 if (ret != 0)
1261 return ret;
1262
1263 ret = xpc_setup_partitions();
1264 if (ret != 0) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001265 dev_err(xpc_part, "can't get memory for partition structure\n");
Dean Nelsonee6665e2008-07-29 22:34:13 -07001266 goto out_1;
Dean Nelsonbc63d382008-07-29 22:34:04 -07001267 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001268
Dean Nelsonbc63d382008-07-29 22:34:04 -07001269 xpc_sysctl = register_sysctl_table(xpc_sys_dir);
1270
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001271 /*
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001272 * Fill the partition reserved page with the information needed by
1273 * other partitions to discover we are alive and establish initial
1274 * communications.
1275 */
Dean Nelson5b8669d2008-07-29 22:34:18 -07001276 ret = xpc_setup_rsvd_page();
1277 if (ret != 0) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001278 dev_err(xpc_part, "can't setup our reserved page\n");
Dean Nelsonee6665e2008-07-29 22:34:13 -07001279 goto out_2;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001280 }
1281
Dean Nelsona607c382005-09-01 14:01:37 -05001282 /* add ourselves to the reboot_notifier_list */
1283 ret = register_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001284 if (ret != 0)
Dean Nelsona607c382005-09-01 14:01:37 -05001285 dev_warn(xpc_part, "can't register reboot notifier\n");
Dean Nelsona607c382005-09-01 14:01:37 -05001286
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07001287 /* add ourselves to the die_notifier list */
Dean Nelson780d09e2005-11-09 14:41:57 -06001288 ret = register_die_notifier(&xpc_die_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001289 if (ret != 0)
Dean Nelson780d09e2005-11-09 14:41:57 -06001290 dev_warn(xpc_part, "can't register die notifier\n");
Dean Nelson780d09e2005-11-09 14:41:57 -06001291
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001292 /*
1293 * The real work-horse behind xpc. This processes incoming
1294 * interrupts and monitors remote heartbeats.
1295 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001296 kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
1297 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001298 dev_err(xpc_part, "failed while forking hb check thread\n");
Dean Nelsonbc63d382008-07-29 22:34:04 -07001299 ret = -EBUSY;
Dean Nelsonee6665e2008-07-29 22:34:13 -07001300 goto out_3;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001301 }
1302
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001303 /*
1304 * Startup a thread that will attempt to discover other partitions to
1305 * activate based on info provided by SAL. This new thread is short
1306 * lived and will exit once discovery is complete.
1307 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001308 kthread = kthread_run(xpc_initiate_discovery, NULL,
1309 XPC_DISCOVERY_THREAD_NAME);
1310 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001311 dev_err(xpc_part, "failed while forking discovery thread\n");
1312
1313 /* mark this new thread as a non-starter */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001314 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001315
Dean Nelson65c17b82008-05-12 14:02:02 -07001316 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001317 return -EBUSY;
1318 }
1319
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001320 /* set the interface to point at XPC's functions */
1321 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001322 xpc_initiate_send, xpc_initiate_send_notify,
1323 xpc_initiate_received, xpc_initiate_partid_to_nasids);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001324
1325 return 0;
Dean Nelsonbc63d382008-07-29 22:34:04 -07001326
1327 /* initialization was not successful */
Dean Nelsonee6665e2008-07-29 22:34:13 -07001328out_3:
Dean Nelson5b8669d2008-07-29 22:34:18 -07001329 xpc_teardown_rsvd_page();
Dean Nelson94bd2702008-07-29 22:34:05 -07001330
Dean Nelsonbc63d382008-07-29 22:34:04 -07001331 (void)unregister_die_notifier(&xpc_die_notifier);
1332 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
Dean Nelsonee6665e2008-07-29 22:34:13 -07001333out_2:
Dean Nelsonbc63d382008-07-29 22:34:04 -07001334 if (xpc_sysctl)
1335 unregister_sysctl_table(xpc_sysctl);
Dean Nelson5b8669d2008-07-29 22:34:18 -07001336
1337 xpc_teardown_partitions();
Dean Nelson6e410172008-07-29 22:34:09 -07001338out_1:
1339 if (is_shub())
1340 xpc_exit_sn2();
Dean Nelsonb7f7b072008-10-29 14:01:12 -07001341 else if (is_uv())
Dean Nelson6e410172008-07-29 22:34:09 -07001342 xpc_exit_uv();
Dean Nelsonbc63d382008-07-29 22:34:04 -07001343 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001344}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001345
Dean Nelson35190502008-04-22 14:48:55 -05001346module_init(xpc_init);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001347
1348void __exit
1349xpc_exit(void)
1350{
Dean Nelson65c17b82008-05-12 14:02:02 -07001351 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001352}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001353
Dean Nelson35190502008-04-22 14:48:55 -05001354module_exit(xpc_exit);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001355
1356MODULE_AUTHOR("Silicon Graphics, Inc.");
1357MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1358MODULE_LICENSE("GPL");
1359
1360module_param(xpc_hb_interval, int, 0);
1361MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001362 "heartbeat increments.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001363
1364module_param(xpc_hb_check_interval, int, 0);
1365MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001366 "heartbeat checks.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001367
Dean Nelsona47d5da2008-07-29 22:34:09 -07001368module_param(xpc_disengage_timelimit, int, 0);
1369MODULE_PARM_DESC(xpc_disengage_timelimit, "Number of seconds to wait "
1370 "for disengage to complete.");
Dean Nelsone54af722005-10-25 14:07:43 -05001371
Dean Nelson1f4674b2006-01-10 11:08:00 -06001372module_param(xpc_kdebug_ignore, int, 0);
1373MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
Dean Nelson35190502008-04-22 14:48:55 -05001374 "other partitions when dropping into kdebug.");