blob: 2180f1f7e0870d64661b29d9a2150eb19b893052 [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 *
Dean Nelson45d9ca42008-04-22 14:46:56 -05006 * Copyright (c) 2004-2008 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 *
28 * . We currently have no way to determine which nasid an IPI came
29 * from. Thus, xpc_IPI_send() does a remote AMO write followed by
30 * an IPI. The AMO indicates where data is to be pulled from, so
31 * after the IPI arrives, the remote partition checks the AMO word.
32 * The IPI can actually arrive before the AMO however, so other code
33 * must periodically check for this case. Also, remote AMO operations
34 * do not reliably time out. Thus we do a remote PIO read solely to
35 * know whether the remote partition is down and whether we should
36 * stop sending IPIs to it. This remote PIO read operation is set up
37 * in a special nofault region so SAL knows to ignore (and cleanup)
38 * any errors due to the remote AMO write, PIO read, and/or PIO
39 * write operations.
40 *
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/kernel.h>
47#include <linux/module.h>
48#include <linux/init.h>
Dean Nelson89eb8eb2005-03-23 19:50:00 -070049#include <linux/cache.h>
50#include <linux/interrupt.h>
Nishanth Aravamudan69913922005-07-08 17:10:00 -070051#include <linux/delay.h>
Dean Nelsona607c382005-09-01 14:01:37 -050052#include <linux/reboot.h>
Jes Sorensenf9e505a2006-01-17 12:52:21 -050053#include <linux/completion.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070054#include <linux/kdebug.h>
Dean Nelson2c2b94f2008-04-22 14:50:17 -050055#include <linux/kthread.h>
56#include <linux/uaccess.h>
Dean Nelson89eb8eb2005-03-23 19:50:00 -070057#include <asm/sn/intr.h>
58#include <asm/sn/sn_sal.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050059#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070060
Dean Nelson89eb8eb2005-03-23 19:50:00 -070061/* define two XPC debug device structures to be used with dev_dbg() et al */
62
63struct device_driver xpc_dbg_name = {
64 .name = "xpc"
65};
66
67struct device xpc_part_dbg_subname = {
68 .bus_id = {0}, /* set to "part" at xpc_init() time */
69 .driver = &xpc_dbg_name
70};
71
72struct device xpc_chan_dbg_subname = {
73 .bus_id = {0}, /* set to "chan" at xpc_init() time */
74 .driver = &xpc_dbg_name
75};
76
77struct device *xpc_part = &xpc_part_dbg_subname;
78struct device *xpc_chan = &xpc_chan_dbg_subname;
79
Dean Nelson1f4674b2006-01-10 11:08:00 -060080static int xpc_kdebug_ignore;
81
Dean Nelson89eb8eb2005-03-23 19:50:00 -070082/* systune related variables for /proc/sys directories */
83
Dean Nelsona607c382005-09-01 14:01:37 -050084static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
85static int xpc_hb_min_interval = 1;
86static int xpc_hb_max_interval = 10;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070087
Dean Nelsona607c382005-09-01 14:01:37 -050088static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
89static int xpc_hb_check_min_interval = 10;
90static int xpc_hb_check_max_interval = 120;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070091
Dean Nelsone54af722005-10-25 14:07:43 -050092int xpc_disengage_request_timelimit = XPC_DISENGAGE_REQUEST_DEFAULT_TIMELIMIT;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050093static int xpc_disengage_request_min_timelimit; /* = 0 */
Dean Nelsone54af722005-10-25 14:07:43 -050094static int xpc_disengage_request_max_timelimit = 120;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070095
96static ctl_table xpc_sys_xpc_hb_dir[] = {
97 {
Dean Nelson35190502008-04-22 14:48:55 -050098 .ctl_name = CTL_UNNUMBERED,
99 .procname = "hb_interval",
100 .data = &xpc_hb_interval,
101 .maxlen = sizeof(int),
102 .mode = 0644,
103 .proc_handler = &proc_dointvec_minmax,
104 .strategy = &sysctl_intvec,
105 .extra1 = &xpc_hb_min_interval,
106 .extra2 = &xpc_hb_max_interval},
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700107 {
Dean Nelson35190502008-04-22 14:48:55 -0500108 .ctl_name = CTL_UNNUMBERED,
109 .procname = "hb_check_interval",
110 .data = &xpc_hb_check_interval,
111 .maxlen = sizeof(int),
112 .mode = 0644,
113 .proc_handler = &proc_dointvec_minmax,
114 .strategy = &sysctl_intvec,
115 .extra1 = &xpc_hb_check_min_interval,
116 .extra2 = &xpc_hb_check_max_interval},
Eric W. Biederman68cbf072007-02-14 00:33:41 -0800117 {}
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700118};
119static ctl_table xpc_sys_xpc_dir[] = {
120 {
Dean Nelson35190502008-04-22 14:48:55 -0500121 .ctl_name = CTL_UNNUMBERED,
122 .procname = "hb",
123 .mode = 0555,
124 .child = xpc_sys_xpc_hb_dir},
Dean Nelsone54af722005-10-25 14:07:43 -0500125 {
Dean Nelson35190502008-04-22 14:48:55 -0500126 .ctl_name = CTL_UNNUMBERED,
127 .procname = "disengage_request_timelimit",
128 .data = &xpc_disengage_request_timelimit,
129 .maxlen = sizeof(int),
130 .mode = 0644,
131 .proc_handler = &proc_dointvec_minmax,
132 .strategy = &sysctl_intvec,
133 .extra1 = &xpc_disengage_request_min_timelimit,
134 .extra2 = &xpc_disengage_request_max_timelimit},
Eric W. Biederman68cbf072007-02-14 00:33:41 -0800135 {}
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700136};
137static ctl_table xpc_sys_dir[] = {
138 {
Dean Nelson35190502008-04-22 14:48:55 -0500139 .ctl_name = CTL_UNNUMBERED,
140 .procname = "xpc",
141 .mode = 0555,
142 .child = xpc_sys_xpc_dir},
Eric W. Biederman68cbf072007-02-14 00:33:41 -0800143 {}
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700144};
145static struct ctl_table_header *xpc_sysctl;
146
Dean Nelson1ecaded2006-01-06 09:48:21 -0600147/* non-zero if any remote partition disengage request was timed out */
148int xpc_disengage_request_timedout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700149
150/* #of IRQs received */
151static atomic_t xpc_act_IRQ_rcvd;
152
153/* IRQ handler notifies this wait queue on receipt of an IRQ */
154static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
155
156static unsigned long xpc_hb_check_timeout;
157
Dean Nelsone54af722005-10-25 14:07:43 -0500158/* notification that the xpc_hb_checker thread has exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500159static DECLARE_COMPLETION(xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700160
Dean Nelsone54af722005-10-25 14:07:43 -0500161/* notification that the xpc_discovery thread has exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500162static DECLARE_COMPLETION(xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700163
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700164static struct timer_list xpc_hb_timer;
165
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700166static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
167
Dean Nelsona607c382005-09-01 14:01:37 -0500168static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
169static struct notifier_block xpc_reboot_notifier = {
170 .notifier_call = xpc_system_reboot,
171};
172
Dean Nelson780d09e2005-11-09 14:41:57 -0600173static int xpc_system_die(struct notifier_block *, unsigned long, void *);
174static struct notifier_block xpc_die_notifier = {
175 .notifier_call = xpc_system_die,
176};
177
Dean Nelson94bd2702008-07-29 22:34:05 -0700178enum xp_retval (*xpc_rsvd_page_init) (struct xpc_rsvd_page *rp);
179
Dean Nelsona607c382005-09-01 14:01:37 -0500180/*
181 * Timer function to enforce the timelimit on the partition disengage request.
182 */
183static void
184xpc_timeout_partition_disengage_request(unsigned long data)
185{
Dean Nelson35190502008-04-22 14:48:55 -0500186 struct xpc_partition *part = (struct xpc_partition *)data;
Dean Nelsona607c382005-09-01 14:01:37 -0500187
Robert P. J. Dayd167cb82008-03-29 10:05:30 -0400188 DBUG_ON(time_before(jiffies, part->disengage_request_timeout));
Dean Nelsona607c382005-09-01 14:01:37 -0500189
Dean Nelson35190502008-04-22 14:48:55 -0500190 (void)xpc_partition_disengaged(part);
Dean Nelsona607c382005-09-01 14:01:37 -0500191
192 DBUG_ON(part->disengage_request_timeout != 0);
193 DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
194}
195
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700196/*
197 * Notify the heartbeat check thread that an IRQ has been received.
198 */
199static irqreturn_t
Al Viro5dcded12006-10-08 14:59:19 +0100200xpc_act_IRQ_handler(int irq, void *dev_id)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700201{
202 atomic_inc(&xpc_act_IRQ_rcvd);
203 wake_up_interruptible(&xpc_act_IRQ_wq);
204 return IRQ_HANDLED;
205}
206
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700207/*
208 * Timer to produce the heartbeat. The timer structures function is
209 * already set when this is initially called. A tunable is used to
210 * specify when the next timeout should occur.
211 */
212static void
213xpc_hb_beater(unsigned long dummy)
214{
215 xpc_vars->heartbeat++;
216
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500217 if (time_after_eq(jiffies, xpc_hb_check_timeout))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700218 wake_up_interruptible(&xpc_act_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700219
220 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
221 add_timer(&xpc_hb_timer);
222}
223
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700224/*
225 * This thread is responsible for nearly all of the partition
226 * activation/deactivation.
227 */
228static int
229xpc_hb_checker(void *ignore)
230{
231 int last_IRQ_count = 0;
232 int new_IRQ_count;
Dean Nelson35190502008-04-22 14:48:55 -0500233 int force_IRQ = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700234
235 /* this thread was marked active by xpc_hb_init() */
236
Mike Travis0bc3cc02008-07-24 18:21:31 -0700237 set_cpus_allowed_ptr(current, &cpumask_of_cpu(XPC_HB_CHECK_CPU));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700238
Dean Nelson4c013f52007-11-07 07:53:06 -0600239 /* set our heartbeating to other partitions into motion */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700240 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
Dean Nelson4c013f52007-11-07 07:53:06 -0600241 xpc_hb_beater(0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700242
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500243 while (!xpc_exiting) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700244
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700245 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
246 "been received\n",
Dean Nelson35190502008-04-22 14:48:55 -0500247 (int)(xpc_hb_check_timeout - jiffies),
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700248 atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
249
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700250 /* checking of remote heartbeats is skewed by IRQ handling */
Robert P. J. Dayd167cb82008-03-29 10:05:30 -0400251 if (time_after_eq(jiffies, xpc_hb_check_timeout)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700252 dev_dbg(xpc_part, "checking remote heartbeats\n");
253 xpc_check_remote_hb();
254
255 /*
256 * We need to periodically recheck to ensure no
257 * IPI/AMO pairs have been missed. That check
258 * must always reset xpc_hb_check_timeout.
259 */
260 force_IRQ = 1;
261 }
262
Dean Nelsona607c382005-09-01 14:01:37 -0500263 /* check for outstanding IRQs */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700264 new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
265 if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
266 force_IRQ = 0;
267
268 dev_dbg(xpc_part, "found an IRQ to process; will be "
269 "resetting xpc_hb_check_timeout\n");
270
271 last_IRQ_count += xpc_identify_act_IRQ_sender();
272 if (last_IRQ_count < new_IRQ_count) {
273 /* retry once to help avoid missing AMO */
Dean Nelson35190502008-04-22 14:48:55 -0500274 (void)xpc_identify_act_IRQ_sender();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700275 }
276 last_IRQ_count = new_IRQ_count;
277
278 xpc_hb_check_timeout = jiffies +
Dean Nelson35190502008-04-22 14:48:55 -0500279 (xpc_hb_check_interval * HZ);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700280 }
Dean Nelsona607c382005-09-01 14:01:37 -0500281
282 /* wait for IRQ or timeout */
Dean Nelson35190502008-04-22 14:48:55 -0500283 (void)wait_event_interruptible(xpc_act_IRQ_wq,
284 (last_IRQ_count <
285 atomic_read(&xpc_act_IRQ_rcvd)
286 || time_after_eq(jiffies,
287 xpc_hb_check_timeout) ||
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500288 xpc_exiting));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700289 }
290
291 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
292
Dean Nelsone54af722005-10-25 14:07:43 -0500293 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500294 complete(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700295 return 0;
296}
297
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700298/*
299 * This thread will attempt to discover other partitions to activate
300 * based on info provided by SAL. This new thread is short lived and
301 * will exit once discovery is complete.
302 */
303static int
304xpc_initiate_discovery(void *ignore)
305{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700306 xpc_discovery();
307
308 dev_dbg(xpc_part, "discovery thread is exiting\n");
309
Dean Nelsone54af722005-10-25 14:07:43 -0500310 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500311 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700312 return 0;
313}
314
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700315/*
316 * Establish first contact with the remote partititon. This involves pulling
317 * the XPC per partition variables from the remote partition and waiting for
318 * the remote partition to pull ours.
319 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700320static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700321xpc_make_first_contact(struct xpc_partition *part)
322{
Dean Nelson65c17b82008-05-12 14:02:02 -0700323 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700324
Dean Nelson65c17b82008-05-12 14:02:02 -0700325 while ((ret = xpc_pull_remote_vars_part(part)) != xpSuccess) {
326 if (ret != xpRetry) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700327 XPC_DEACTIVATE_PARTITION(part, ret);
328 return ret;
329 }
330
331 dev_dbg(xpc_chan, "waiting to make first contact with "
332 "partition %d\n", XPC_PARTID(part));
333
334 /* wait a 1/4 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -0500335 (void)msleep_interruptible(250);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700336
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500337 if (part->act_state == XPC_P_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700338 return part->reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700339 }
340
341 return xpc_mark_partition_active(part);
342}
343
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700344/*
345 * The first kthread assigned to a newly activated partition is the one
346 * created by XPC HB with which it calls xpc_partition_up(). XPC hangs on to
347 * that kthread until the partition is brought down, at which time that kthread
348 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
349 * that XPC has dismantled all communication infrastructure for the associated
350 * partition.) This kthread becomes the channel manager for that partition.
351 *
352 * Each active partition has a channel manager, who, besides connecting and
353 * disconnecting channels, will ensure that each of the partition's connected
354 * channels has the required number of assigned kthreads to get the work done.
355 */
356static void
357xpc_channel_mgr(struct xpc_partition *part)
358{
359 while (part->act_state != XPC_P_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500360 atomic_read(&part->nchannels_active) > 0 ||
361 !xpc_partition_disengaged(part)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700362
363 xpc_process_channel_activity(part);
364
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700365 /*
366 * Wait until we've been requested to activate kthreads or
367 * all of the channel's message queues have been torn down or
368 * a signal is pending.
369 *
370 * The channel_mgr_requests is set to 1 after being awakened,
371 * This is done to prevent the channel mgr from making one pass
372 * through the loop for each request, since he will
373 * be servicing all the requests in one pass. The reason it's
374 * set to 1 instead of 0 is so that other kthreads will know
375 * that the channel mgr is running and won't bother trying to
376 * wake him up.
377 */
378 atomic_dec(&part->channel_mgr_requests);
Dean Nelson35190502008-04-22 14:48:55 -0500379 (void)wait_event_interruptible(part->channel_mgr_wq,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500380 (atomic_read(&part->channel_mgr_requests) > 0 ||
381 part->local_IPI_amo != 0 ||
382 (part->act_state == XPC_P_DEACTIVATING &&
383 atomic_read(&part->nchannels_active) == 0 &&
384 xpc_partition_disengaged(part))));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700385 atomic_set(&part->channel_mgr_requests, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700386 }
387}
388
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700389/*
390 * When XPC HB determines that a partition has come up, it will create a new
391 * kthread and that kthread will call this function to attempt to set up the
392 * basic infrastructure used for Cross Partition Communication with the newly
393 * upped partition.
394 *
395 * The kthread that was created by XPC HB and which setup the XPC
396 * infrastructure will remain assigned to the partition until the partition
397 * goes down. At which time the kthread will teardown the XPC infrastructure
398 * and then exit.
399 *
400 * XPC HB will put the remote partition's XPC per partition specific variables
401 * physical address into xpc_partitions[partid].remote_vars_part_pa prior to
402 * calling xpc_partition_up().
403 */
404static void
405xpc_partition_up(struct xpc_partition *part)
406{
407 DBUG_ON(part->channels != NULL);
408
409 dev_dbg(xpc_chan, "activating partition %d\n", XPC_PARTID(part));
410
Dean Nelson65c17b82008-05-12 14:02:02 -0700411 if (xpc_setup_infrastructure(part) != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700412 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700413
414 /*
415 * The kthread that XPC HB called us with will become the
416 * channel manager for this partition. It will not return
417 * back to XPC HB until the partition's XPC infrastructure
418 * has been dismantled.
419 */
420
Dean Nelson35190502008-04-22 14:48:55 -0500421 (void)xpc_part_ref(part); /* this will always succeed */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700422
Dean Nelson65c17b82008-05-12 14:02:02 -0700423 if (xpc_make_first_contact(part) == xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700424 xpc_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700425
426 xpc_part_deref(part);
427
428 xpc_teardown_infrastructure(part);
429}
430
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700431static int
432xpc_activating(void *__partid)
433{
Dean Nelson64d032b2008-05-12 14:02:03 -0700434 short partid = (u64)__partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700435 struct xpc_partition *part = &xpc_partitions[partid];
436 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700437
Dean Nelsonbc63d382008-07-29 22:34:04 -0700438 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700439
440 spin_lock_irqsave(&part->act_lock, irq_flags);
441
442 if (part->act_state == XPC_P_DEACTIVATING) {
443 part->act_state = XPC_P_INACTIVE;
444 spin_unlock_irqrestore(&part->act_lock, irq_flags);
445 part->remote_rp_pa = 0;
446 return 0;
447 }
448
449 /* indicate the thread is activating */
450 DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
451 part->act_state = XPC_P_ACTIVATING;
452
453 XPC_SET_REASON(part, 0, 0);
454 spin_unlock_irqrestore(&part->act_lock, irq_flags);
455
456 dev_dbg(xpc_part, "bringing partition %d up\n", partid);
457
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700458 /*
459 * Register the remote partition's AMOs with SAL so it can handle
460 * and cleanup errors within that address range should the remote
461 * partition go down. We don't unregister this range because it is
462 * difficult to tell when outstanding writes to the remote partition
463 * are finished and thus when it is safe to unregister. This should
464 * not result in wasted space in the SAL xp_addr_region table because
465 * we should get the same page for remote_amos_page_pa after module
466 * reloads and system reboots.
467 */
468 if (sn_register_xp_addr_region(part->remote_amos_page_pa,
Dean Nelson35190502008-04-22 14:48:55 -0500469 PAGE_SIZE, 1) < 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700470 dev_warn(xpc_part, "xpc_partition_up(%d) failed to register "
Dean Nelson35190502008-04-22 14:48:55 -0500471 "xp_addr region\n", partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700472
473 spin_lock_irqsave(&part->act_lock, irq_flags);
474 part->act_state = XPC_P_INACTIVE;
Dean Nelson65c17b82008-05-12 14:02:02 -0700475 XPC_SET_REASON(part, xpPhysAddrRegFailed, __LINE__);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700476 spin_unlock_irqrestore(&part->act_lock, irq_flags);
477 part->remote_rp_pa = 0;
478 return 0;
479 }
480
Dean Nelsona607c382005-09-01 14:01:37 -0500481 xpc_allow_hb(partid, xpc_vars);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700482 xpc_IPI_send_activated(part);
483
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700484 /*
485 * xpc_partition_up() holds this thread and marks this partition as
486 * XPC_P_ACTIVE by calling xpc_hb_mark_active().
487 */
Dean Nelson35190502008-04-22 14:48:55 -0500488 (void)xpc_partition_up(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700489
Dean Nelsona607c382005-09-01 14:01:37 -0500490 xpc_disallow_hb(partid, xpc_vars);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700491 xpc_mark_partition_inactive(part);
492
Dean Nelson65c17b82008-05-12 14:02:02 -0700493 if (part->reason == xpReactivating) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700494 /* interrupting ourselves results in activating partition */
495 xpc_IPI_send_reactivate(part);
496 }
497
498 return 0;
499}
500
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700501void
502xpc_activate_partition(struct xpc_partition *part)
503{
Dean Nelson64d032b2008-05-12 14:02:03 -0700504 short partid = XPC_PARTID(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700505 unsigned long irq_flags;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500506 struct task_struct *kthread;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700507
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700508 spin_lock_irqsave(&part->act_lock, irq_flags);
509
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700510 DBUG_ON(part->act_state != XPC_P_INACTIVE);
511
Robin Holt7c6c6632006-02-02 12:30:21 -0600512 part->act_state = XPC_P_ACTIVATION_REQ;
Dean Nelson65c17b82008-05-12 14:02:02 -0700513 XPC_SET_REASON(part, xpCloneKThread, __LINE__);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700514
515 spin_unlock_irqrestore(&part->act_lock, irq_flags);
Robin Holt7c6c6632006-02-02 12:30:21 -0600516
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500517 kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
518 partid);
519 if (IS_ERR(kthread)) {
Robin Holt7c6c6632006-02-02 12:30:21 -0600520 spin_lock_irqsave(&part->act_lock, irq_flags);
521 part->act_state = XPC_P_INACTIVE;
Dean Nelson65c17b82008-05-12 14:02:02 -0700522 XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
Robin Holt7c6c6632006-02-02 12:30:21 -0600523 spin_unlock_irqrestore(&part->act_lock, irq_flags);
524 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700525}
526
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700527/*
528 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
529 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
530 * than one partition, we use an AMO_t structure per partition to indicate
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500531 * whether a partition has sent an IPI or not. If it has, then wake up the
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700532 * associated kthread to handle it.
533 *
534 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
535 * running on other partitions.
536 *
537 * Noteworthy Arguments:
538 *
539 * irq - Interrupt ReQuest number. NOT USED.
540 *
541 * dev_id - partid of IPI's potential sender.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700542 */
543irqreturn_t
Al Viro5dcded12006-10-08 14:59:19 +0100544xpc_notify_IRQ_handler(int irq, void *dev_id)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700545{
Dean Nelson64d032b2008-05-12 14:02:03 -0700546 short partid = (short)(u64)dev_id;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700547 struct xpc_partition *part = &xpc_partitions[partid];
548
Dean Nelsonbc63d382008-07-29 22:34:04 -0700549 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700550
551 if (xpc_part_ref(part)) {
552 xpc_check_for_channel_activity(part);
553
554 xpc_part_deref(part);
555 }
556 return IRQ_HANDLED;
557}
558
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700559/*
560 * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
561 * because the write to their associated IPI amo completed after the IRQ/IPI
562 * was received.
563 */
564void
565xpc_dropped_IPI_check(struct xpc_partition *part)
566{
567 if (xpc_part_ref(part)) {
568 xpc_check_for_channel_activity(part);
569
570 part->dropped_IPI_timer.expires = jiffies +
Dean Nelson35190502008-04-22 14:48:55 -0500571 XPC_P_DROPPED_IPI_WAIT;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700572 add_timer(&part->dropped_IPI_timer);
573 xpc_part_deref(part);
574 }
575}
576
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700577void
578xpc_activate_kthreads(struct xpc_channel *ch, int needed)
579{
580 int idle = atomic_read(&ch->kthreads_idle);
581 int assigned = atomic_read(&ch->kthreads_assigned);
582 int wakeup;
583
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700584 DBUG_ON(needed <= 0);
585
586 if (idle > 0) {
587 wakeup = (needed > idle) ? idle : needed;
588 needed -= wakeup;
589
590 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
591 "channel=%d\n", wakeup, ch->partid, ch->number);
592
593 /* only wakeup the requested number of kthreads */
594 wake_up_nr(&ch->idle_wq, wakeup);
595 }
596
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500597 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700598 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700599
600 if (needed + assigned > ch->kthreads_assigned_limit) {
601 needed = ch->kthreads_assigned_limit - assigned;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500602 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700603 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700604 }
605
606 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
607 needed, ch->partid, ch->number);
608
Dean Nelsona460ef82006-11-22 08:25:00 -0600609 xpc_create_kthreads(ch, needed, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700610}
611
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700612/*
613 * This function is where XPC's kthreads wait for messages to deliver.
614 */
615static void
616xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
617{
618 do {
619 /* deliver messages to their intended recipients */
620
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500621 while (ch->w_local_GP.get < ch->w_remote_GP.put &&
622 !(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700623 xpc_deliver_msg(ch);
624 }
625
626 if (atomic_inc_return(&ch->kthreads_idle) >
Dean Nelson35190502008-04-22 14:48:55 -0500627 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700628 /* too many idle kthreads on this channel */
629 atomic_dec(&ch->kthreads_idle);
630 break;
631 }
632
633 dev_dbg(xpc_chan, "idle kthread calling "
634 "wait_event_interruptible_exclusive()\n");
635
Dean Nelson35190502008-04-22 14:48:55 -0500636 (void)wait_event_interruptible_exclusive(ch->idle_wq,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500637 (ch->w_local_GP.get < ch->w_remote_GP.put ||
638 (ch->flags & XPC_C_DISCONNECTING)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700639
640 atomic_dec(&ch->kthreads_idle);
641
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500642 } while (!(ch->flags & XPC_C_DISCONNECTING));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700643}
644
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700645static int
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500646xpc_kthread_start(void *args)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700647{
Dean Nelson64d032b2008-05-12 14:02:03 -0700648 short partid = XPC_UNPACK_ARG1(args);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700649 u16 ch_number = XPC_UNPACK_ARG2(args);
650 struct xpc_partition *part = &xpc_partitions[partid];
651 struct xpc_channel *ch;
652 int n_needed;
Dean Nelsone54af722005-10-25 14:07:43 -0500653 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700654
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700655 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
656 partid, ch_number);
657
658 ch = &part->channels[ch_number];
659
660 if (!(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700661
662 /* let registerer know that connection has been established */
663
Dean Nelsone54af722005-10-25 14:07:43 -0500664 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson4c2cd962006-02-15 08:02:21 -0600665 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
666 ch->flags |= XPC_C_CONNECTEDCALLOUT;
Dean Nelsone54af722005-10-25 14:07:43 -0500667 spin_unlock_irqrestore(&ch->lock, irq_flags);
668
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700669 xpc_connected_callout(ch);
670
Dean Nelson4c2cd962006-02-15 08:02:21 -0600671 spin_lock_irqsave(&ch->lock, irq_flags);
672 ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
673 spin_unlock_irqrestore(&ch->lock, irq_flags);
674
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700675 /*
676 * It is possible that while the callout was being
677 * made that the remote partition sent some messages.
678 * If that is the case, we may need to activate
679 * additional kthreads to help deliver them. We only
680 * need one less than total #of messages to deliver.
681 */
682 n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500683 if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700684 xpc_activate_kthreads(ch, n_needed);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500685
Dean Nelsone54af722005-10-25 14:07:43 -0500686 } else {
687 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700688 }
689
690 xpc_kthread_waitmsgs(part, ch);
691 }
692
Dean Nelsona460ef82006-11-22 08:25:00 -0600693 /* let registerer know that connection is disconnecting */
Dean Nelsone54af722005-10-25 14:07:43 -0500694
Dean Nelsona460ef82006-11-22 08:25:00 -0600695 spin_lock_irqsave(&ch->lock, irq_flags);
696 if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500697 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelsona460ef82006-11-22 08:25:00 -0600698 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
Dean Nelson4c2cd962006-02-15 08:02:21 -0600699 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -0600700
Dean Nelson65c17b82008-05-12 14:02:02 -0700701 xpc_disconnect_callout(ch, xpDisconnecting);
Dean Nelsona460ef82006-11-22 08:25:00 -0600702
703 spin_lock_irqsave(&ch->lock, irq_flags);
704 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
705 }
706 spin_unlock_irqrestore(&ch->lock, irq_flags);
707
708 if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
Dean Nelsona607c382005-09-01 14:01:37 -0500709 if (atomic_dec_return(&part->nchannels_engaged) == 0) {
710 xpc_mark_partition_disengaged(part);
711 xpc_IPI_send_disengage(part);
712 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700713 }
714
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700715 xpc_msgqueue_deref(ch);
716
717 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
718 partid, ch_number);
719
720 xpc_part_deref(part);
721 return 0;
722}
723
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700724/*
725 * For each partition that XPC has established communications with, there is
726 * a minimum of one kernel thread assigned to perform any operation that
727 * may potentially sleep or block (basically the callouts to the asynchronous
728 * functions registered via xpc_connect()).
729 *
730 * Additional kthreads are created and destroyed by XPC as the workload
731 * demands.
732 *
733 * A kthread is assigned to one of the active channels that exists for a given
734 * partition.
735 */
736void
Dean Nelsona460ef82006-11-22 08:25:00 -0600737xpc_create_kthreads(struct xpc_channel *ch, int needed,
Dean Nelson35190502008-04-22 14:48:55 -0500738 int ignore_disconnecting)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700739{
740 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700741 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
Dean Nelsona607c382005-09-01 14:01:37 -0500742 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500743 struct task_struct *kthread;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700744
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700745 while (needed-- > 0) {
Dean Nelsone54af722005-10-25 14:07:43 -0500746
747 /*
748 * The following is done on behalf of the newly created
749 * kthread. That kthread is responsible for doing the
750 * counterpart to the following before it exits.
751 */
Dean Nelsona460ef82006-11-22 08:25:00 -0600752 if (ignore_disconnecting) {
753 if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
754 /* kthreads assigned had gone to zero */
755 BUG_ON(!(ch->flags &
Dean Nelson35190502008-04-22 14:48:55 -0500756 XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelsona460ef82006-11-22 08:25:00 -0600757 break;
758 }
759
760 } else if (ch->flags & XPC_C_DISCONNECTING) {
761 break;
762
763 } else if (atomic_inc_return(&ch->kthreads_assigned) == 1) {
764 if (atomic_inc_return(&part->nchannels_engaged) == 1)
765 xpc_mark_partition_engaged(part);
766 }
Dean Nelson35190502008-04-22 14:48:55 -0500767 (void)xpc_part_ref(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500768 xpc_msgqueue_ref(ch);
Dean Nelsone54af722005-10-25 14:07:43 -0500769
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500770 kthread = kthread_run(xpc_kthread_start, (void *)args,
771 "xpc%02dc%d", ch->partid, ch->number);
772 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700773 /* the fork failed */
Dean Nelsona460ef82006-11-22 08:25:00 -0600774
775 /*
776 * NOTE: if (ignore_disconnecting &&
777 * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
778 * then we'll deadlock if all other kthreads assigned
779 * to this channel are blocked in the channel's
780 * registerer, because the only thing that will unblock
Dean Nelson65c17b82008-05-12 14:02:02 -0700781 * them is the xpDisconnecting callout that this
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500782 * failed kthread_run() would have made.
Dean Nelsona460ef82006-11-22 08:25:00 -0600783 */
784
Dean Nelsone54af722005-10-25 14:07:43 -0500785 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
786 atomic_dec_return(&part->nchannels_engaged) == 0) {
787 xpc_mark_partition_disengaged(part);
788 xpc_IPI_send_disengage(part);
789 }
790 xpc_msgqueue_deref(ch);
791 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700792
793 if (atomic_read(&ch->kthreads_assigned) <
Dean Nelson35190502008-04-22 14:48:55 -0500794 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700795 /*
796 * Flag this as an error only if we have an
797 * insufficient #of kthreads for the channel
798 * to function.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700799 */
800 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700801 XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
Dean Nelson35190502008-04-22 14:48:55 -0500802 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700803 spin_unlock_irqrestore(&ch->lock, irq_flags);
804 }
805 break;
806 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700807 }
808}
809
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700810void
811xpc_disconnect_wait(int ch_number)
812{
Dean Nelsona607c382005-09-01 14:01:37 -0500813 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700814 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700815 struct xpc_partition *part;
816 struct xpc_channel *ch;
Dean Nelsone54af722005-10-25 14:07:43 -0500817 int wakeup_channel_mgr;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700818
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700819 /* now wait for all callouts to the caller's function to cease */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700820 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700821 part = &xpc_partitions[partid];
822
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500823 if (!xpc_part_ref(part))
Dean Nelsone54af722005-10-25 14:07:43 -0500824 continue;
Dean Nelsone54af722005-10-25 14:07:43 -0500825
826 ch = &part->channels[ch_number];
827
828 if (!(ch->flags & XPC_C_WDISCONNECT)) {
829 xpc_part_deref(part);
830 continue;
831 }
832
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500833 wait_for_completion(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500834
835 spin_lock_irqsave(&ch->lock, irq_flags);
836 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
837 wakeup_channel_mgr = 0;
838
839 if (ch->delayed_IPI_flags) {
840 if (part->act_state != XPC_P_DEACTIVATING) {
841 spin_lock(&part->IPI_lock);
842 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500843 ch->number,
844 ch->delayed_IPI_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500845 spin_unlock(&part->IPI_lock);
846 wakeup_channel_mgr = 1;
847 }
848 ch->delayed_IPI_flags = 0;
849 }
850
851 ch->flags &= ~XPC_C_WDISCONNECT;
852 spin_unlock_irqrestore(&ch->lock, irq_flags);
853
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500854 if (wakeup_channel_mgr)
Dean Nelsone54af722005-10-25 14:07:43 -0500855 xpc_wakeup_channel_mgr(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500856
857 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700858 }
859}
860
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700861static void
Dean Nelson65c17b82008-05-12 14:02:02 -0700862xpc_do_exit(enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700863{
Dean Nelson64d032b2008-05-12 14:02:03 -0700864 short partid;
Dean Nelson1ecaded2006-01-06 09:48:21 -0600865 int active_part_count, printed_waiting_msg = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700866 struct xpc_partition *part;
Dean Nelson1ecaded2006-01-06 09:48:21 -0600867 unsigned long printmsg_time, disengage_request_timeout = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700868
Dean Nelsona607c382005-09-01 14:01:37 -0500869 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
870 DBUG_ON(xpc_exiting == 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700871
872 /*
Dean Nelsona607c382005-09-01 14:01:37 -0500873 * Let the heartbeat checker thread and the discovery thread
874 * (if one is running) know that they should exit. Also wake up
875 * the heartbeat checker thread in case it's sleeping.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700876 */
877 xpc_exiting = 1;
878 wake_up_interruptible(&xpc_act_IRQ_wq);
879
Dean Nelsona607c382005-09-01 14:01:37 -0500880 /* ignore all incoming interrupts */
881 free_irq(SGI_XPC_ACTIVATE, NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700882
Dean Nelsone54af722005-10-25 14:07:43 -0500883 /* wait for the discovery thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500884 wait_for_completion(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700885
Dean Nelsone54af722005-10-25 14:07:43 -0500886 /* wait for the heartbeat checker thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500887 wait_for_completion(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700888
Dean Nelsona607c382005-09-01 14:01:37 -0500889 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -0500890 (void)msleep_interruptible(300);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700891
892 /* wait for all partitions to become inactive */
893
Dean Nelson1ecaded2006-01-06 09:48:21 -0600894 printmsg_time = jiffies + (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
895 xpc_disengage_request_timedout = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500896
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700897 do {
898 active_part_count = 0;
899
Dean Nelsonbc63d382008-07-29 22:34:04 -0700900 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700901 part = &xpc_partitions[partid];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700902
Dean Nelsona607c382005-09-01 14:01:37 -0500903 if (xpc_partition_disengaged(part) &&
Dean Nelson35190502008-04-22 14:48:55 -0500904 part->act_state == XPC_P_INACTIVE) {
Dean Nelsona607c382005-09-01 14:01:37 -0500905 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700906 }
Dean Nelsona607c382005-09-01 14:01:37 -0500907
908 active_part_count++;
909
910 XPC_DEACTIVATE_PARTITION(part, reason);
Dean Nelson1ecaded2006-01-06 09:48:21 -0600911
912 if (part->disengage_request_timeout >
Dean Nelson35190502008-04-22 14:48:55 -0500913 disengage_request_timeout) {
Dean Nelson1ecaded2006-01-06 09:48:21 -0600914 disengage_request_timeout =
Dean Nelson35190502008-04-22 14:48:55 -0500915 part->disengage_request_timeout;
Dean Nelson1ecaded2006-01-06 09:48:21 -0600916 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700917 }
918
Dean Nelson1ecaded2006-01-06 09:48:21 -0600919 if (xpc_partition_engaged(-1UL)) {
920 if (time_after(jiffies, printmsg_time)) {
921 dev_info(xpc_part, "waiting for remote "
Dean Nelson35190502008-04-22 14:48:55 -0500922 "partitions to disengage, timeout in "
923 "%ld seconds\n",
924 (disengage_request_timeout - jiffies)
925 / HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -0600926 printmsg_time = jiffies +
Dean Nelson35190502008-04-22 14:48:55 -0500927 (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -0600928 printed_waiting_msg = 1;
929 }
930
931 } else if (active_part_count > 0) {
932 if (printed_waiting_msg) {
933 dev_info(xpc_part, "waiting for local partition"
Dean Nelson35190502008-04-22 14:48:55 -0500934 " to disengage\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -0600935 printed_waiting_msg = 0;
936 }
937
938 } else {
939 if (!xpc_disengage_request_timedout) {
940 dev_info(xpc_part, "all partitions have "
Dean Nelson35190502008-04-22 14:48:55 -0500941 "disengaged\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -0600942 }
943 break;
Dean Nelsona607c382005-09-01 14:01:37 -0500944 }
945
946 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -0500947 (void)msleep_interruptible(300);
Dean Nelsona607c382005-09-01 14:01:37 -0500948
949 } while (1);
950
951 DBUG_ON(xpc_partition_engaged(-1UL));
952
Dean Nelsona607c382005-09-01 14:01:37 -0500953 /* indicate to others that our reserved page is uninitialized */
Dean Nelson94bd2702008-07-29 22:34:05 -0700954 xpc_rsvd_page->stamp = ZERO_STAMP;
Dean Nelsona607c382005-09-01 14:01:37 -0500955
956 /* now it's time to eliminate our heartbeat */
957 del_timer_sync(&xpc_hb_timer);
Dean Nelsone54af722005-10-25 14:07:43 -0500958 DBUG_ON(xpc_vars->heartbeating_to_mask != 0);
Dean Nelsona607c382005-09-01 14:01:37 -0500959
Dean Nelson65c17b82008-05-12 14:02:02 -0700960 if (reason == xpUnloading) {
Dean Nelson35190502008-04-22 14:48:55 -0500961 (void)unregister_die_notifier(&xpc_die_notifier);
Dean Nelsonbc63d382008-07-29 22:34:04 -0700962 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson0752c672006-01-10 11:07:19 -0600963 }
Dean Nelson780d09e2005-11-09 14:41:57 -0600964
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700965 /* close down protections for IPI operations */
966 xpc_restrict_IPI_ops();
967
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700968 /* clear the interface to XPC's functions */
969 xpc_clear_interface();
970
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500971 if (xpc_sysctl)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700972 unregister_sysctl_table(xpc_sysctl);
Dean Nelson7682a4c2006-08-08 15:03:29 -0500973
Dean Nelsonbc63d382008-07-29 22:34:04 -0700974 kfree(xpc_partitions);
Dean Nelson7682a4c2006-08-08 15:03:29 -0500975 kfree(xpc_remote_copy_buffer_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700976}
977
Dean Nelsona607c382005-09-01 14:01:37 -0500978/*
Dean Nelsond6ad0332006-01-10 11:08:55 -0600979 * This function is called when the system is being rebooted.
980 */
981static int
982xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
983{
Dean Nelson65c17b82008-05-12 14:02:02 -0700984 enum xp_retval reason;
Dean Nelsond6ad0332006-01-10 11:08:55 -0600985
Dean Nelsond6ad0332006-01-10 11:08:55 -0600986 switch (event) {
987 case SYS_RESTART:
Dean Nelson65c17b82008-05-12 14:02:02 -0700988 reason = xpSystemReboot;
Dean Nelsond6ad0332006-01-10 11:08:55 -0600989 break;
990 case SYS_HALT:
Dean Nelson65c17b82008-05-12 14:02:02 -0700991 reason = xpSystemHalt;
Dean Nelsond6ad0332006-01-10 11:08:55 -0600992 break;
993 case SYS_POWER_OFF:
Dean Nelson65c17b82008-05-12 14:02:02 -0700994 reason = xpSystemPoweroff;
Dean Nelsond6ad0332006-01-10 11:08:55 -0600995 break;
996 default:
Dean Nelson65c17b82008-05-12 14:02:02 -0700997 reason = xpSystemGoingDown;
Dean Nelsond6ad0332006-01-10 11:08:55 -0600998 }
999
1000 xpc_do_exit(reason);
1001 return NOTIFY_DONE;
1002}
1003
Dean Nelsond6ad0332006-01-10 11:08:55 -06001004/*
1005 * Notify other partitions to disengage from all references to our memory.
Dean Nelson780d09e2005-11-09 14:41:57 -06001006 */
1007static void
1008xpc_die_disengage(void)
1009{
1010 struct xpc_partition *part;
Dean Nelson64d032b2008-05-12 14:02:03 -07001011 short partid;
Dean Nelson780d09e2005-11-09 14:41:57 -06001012 unsigned long engaged;
Dean Nelson1ecaded2006-01-06 09:48:21 -06001013 long time, printmsg_time, disengage_request_timeout;
Dean Nelson780d09e2005-11-09 14:41:57 -06001014
Dean Nelson780d09e2005-11-09 14:41:57 -06001015 /* keep xpc_hb_checker thread from doing anything (just in case) */
1016 xpc_exiting = 1;
1017
Dean Nelson35190502008-04-22 14:48:55 -05001018 xpc_vars->heartbeating_to_mask = 0; /* indicate we're deactivated */
Dean Nelson780d09e2005-11-09 14:41:57 -06001019
Dean Nelsonbc63d382008-07-29 22:34:04 -07001020 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001021 part = &xpc_partitions[partid];
1022
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001023 if (!XPC_SUPPORTS_DISENGAGE_REQUEST(part->
1024 remote_vars_version)) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001025
1026 /* just in case it was left set by an earlier XPC */
1027 xpc_clear_partition_engaged(1UL << partid);
1028 continue;
1029 }
1030
1031 if (xpc_partition_engaged(1UL << partid) ||
Dean Nelson35190502008-04-22 14:48:55 -05001032 part->act_state != XPC_P_INACTIVE) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001033 xpc_request_partition_disengage(part);
1034 xpc_mark_partition_disengaged(part);
1035 xpc_IPI_send_disengage(part);
1036 }
1037 }
1038
Dean Nelson1ecaded2006-01-06 09:48:21 -06001039 time = rtc_time();
1040 printmsg_time = time +
Dean Nelson35190502008-04-22 14:48:55 -05001041 (XPC_DISENGAGE_PRINTMSG_INTERVAL * sn_rtc_cycles_per_second);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001042 disengage_request_timeout = time +
Dean Nelson35190502008-04-22 14:48:55 -05001043 (xpc_disengage_request_timelimit * sn_rtc_cycles_per_second);
Dean Nelson780d09e2005-11-09 14:41:57 -06001044
1045 /* wait for all other partitions to disengage from us */
1046
Dean Nelson1ecaded2006-01-06 09:48:21 -06001047 while (1) {
1048 engaged = xpc_partition_engaged(-1UL);
1049 if (!engaged) {
1050 dev_info(xpc_part, "all partitions have disengaged\n");
1051 break;
1052 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001053
Dean Nelson1ecaded2006-01-06 09:48:21 -06001054 time = rtc_time();
1055 if (time >= disengage_request_timeout) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001056 for (partid = 0; partid < xp_max_npartitions;
1057 partid++) {
Dean Nelson1ecaded2006-01-06 09:48:21 -06001058 if (engaged & (1UL << partid)) {
1059 dev_info(xpc_part, "disengage from "
Dean Nelson35190502008-04-22 14:48:55 -05001060 "remote partition %d timed "
1061 "out\n", partid);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001062 }
1063 }
1064 break;
1065 }
1066
1067 if (time >= printmsg_time) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001068 dev_info(xpc_part, "waiting for remote partitions to "
Dean Nelson35190502008-04-22 14:48:55 -05001069 "disengage, timeout in %ld seconds\n",
1070 (disengage_request_timeout - time) /
1071 sn_rtc_cycles_per_second);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001072 printmsg_time = time +
Dean Nelson35190502008-04-22 14:48:55 -05001073 (XPC_DISENGAGE_PRINTMSG_INTERVAL *
1074 sn_rtc_cycles_per_second);
Dean Nelson780d09e2005-11-09 14:41:57 -06001075 }
1076 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001077}
1078
Dean Nelson780d09e2005-11-09 14:41:57 -06001079/*
Dean Nelson1f4674b2006-01-10 11:08:00 -06001080 * This function is called when the system is being restarted or halted due
1081 * to some sort of system failure. If this is the case we need to notify the
1082 * other partitions to disengage from all references to our memory.
1083 * This function can also be called when our heartbeater could be offlined
1084 * for a time. In this case we need to notify other partitions to not worry
1085 * about the lack of a heartbeat.
Dean Nelson780d09e2005-11-09 14:41:57 -06001086 */
1087static int
1088xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1089{
1090 switch (event) {
1091 case DIE_MACHINE_RESTART:
1092 case DIE_MACHINE_HALT:
1093 xpc_die_disengage();
1094 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001095
1096 case DIE_KDEBUG_ENTER:
1097 /* Should lack of heartbeat be ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001098 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001099 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001100
Dean Nelson1f4674b2006-01-10 11:08:00 -06001101 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001102 case DIE_MCA_MONARCH_ENTER:
1103 case DIE_INIT_MONARCH_ENTER:
1104 xpc_vars->heartbeat++;
1105 xpc_vars->heartbeat_offline = 1;
1106 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001107
1108 case DIE_KDEBUG_LEAVE:
1109 /* Is lack of heartbeat being ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001110 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001111 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001112
Dean Nelson1f4674b2006-01-10 11:08:00 -06001113 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001114 case DIE_MCA_MONARCH_LEAVE:
1115 case DIE_INIT_MONARCH_LEAVE:
1116 xpc_vars->heartbeat++;
1117 xpc_vars->heartbeat_offline = 0;
1118 break;
1119 }
1120
1121 return NOTIFY_DONE;
1122}
1123
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001124int __init
1125xpc_init(void)
1126{
1127 int ret;
Dean Nelson64d032b2008-05-12 14:02:03 -07001128 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001129 struct xpc_partition *part;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001130 struct task_struct *kthread;
Dean Nelson7682a4c2006-08-08 15:03:29 -05001131 size_t buf_size;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001132
Dean Nelson94bd2702008-07-29 22:34:05 -07001133 if (is_shub()) {
1134 /*
1135 * The ia64-sn2 architecture supports at most 64 partitions.
1136 * And the inability to unregister remote AMOs restricts us
1137 * further to only support exactly 64 partitions on this
1138 * architecture, no less.
1139 */
1140 if (xp_max_npartitions != 64)
1141 return -EINVAL;
1142
1143 xpc_init_sn2();
1144
1145 } else if (is_uv()) {
1146 xpc_init_uv();
1147
1148 } else {
Dean Nelson408865c2005-09-08 10:46:58 -05001149 return -ENODEV;
Dean Nelson94bd2702008-07-29 22:34:05 -07001150 }
Dean Nelson408865c2005-09-08 10:46:58 -05001151
Dean Nelsonbc63d382008-07-29 22:34:04 -07001152 snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1153 snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
1154
Dean Nelson7682a4c2006-08-08 15:03:29 -05001155 buf_size = max(XPC_RP_VARS_SIZE,
Dean Nelson35190502008-04-22 14:48:55 -05001156 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES);
Dean Nelson7682a4c2006-08-08 15:03:29 -05001157 xpc_remote_copy_buffer = xpc_kmalloc_cacheline_aligned(buf_size,
Dean Nelson35190502008-04-22 14:48:55 -05001158 GFP_KERNEL,
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001159 &xpc_remote_copy_buffer_base);
Dean Nelsonbc63d382008-07-29 22:34:04 -07001160 if (xpc_remote_copy_buffer == NULL) {
1161 dev_err(xpc_part, "can't get memory for remote copy buffer\n");
Dean Nelson7682a4c2006-08-08 15:03:29 -05001162 return -ENOMEM;
Dean Nelsonbc63d382008-07-29 22:34:04 -07001163 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001164
Dean Nelsonbc63d382008-07-29 22:34:04 -07001165 xpc_partitions = kzalloc(sizeof(struct xpc_partition) *
1166 xp_max_npartitions, GFP_KERNEL);
1167 if (xpc_partitions == NULL) {
1168 dev_err(xpc_part, "can't get memory for partition structure\n");
1169 ret = -ENOMEM;
1170 goto out_1;
1171 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001172
1173 /*
1174 * The first few fields of each entry of xpc_partitions[] need to
1175 * be initialized now so that calls to xpc_connect() and
1176 * xpc_disconnect() can be made prior to the activation of any remote
1177 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
1178 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
1179 * PARTITION HAS BEEN ACTIVATED.
1180 */
Dean Nelsonbc63d382008-07-29 22:34:04 -07001181 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001182 part = &xpc_partitions[partid];
1183
Dean Nelson35190502008-04-22 14:48:55 -05001184 DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001185
1186 part->act_IRQ_rcvd = 0;
1187 spin_lock_init(&part->act_lock);
1188 part->act_state = XPC_P_INACTIVE;
1189 XPC_SET_REASON(part, 0, 0);
Dean Nelsona607c382005-09-01 14:01:37 -05001190
1191 init_timer(&part->disengage_request_timer);
1192 part->disengage_request_timer.function =
Dean Nelson35190502008-04-22 14:48:55 -05001193 xpc_timeout_partition_disengage_request;
1194 part->disengage_request_timer.data = (unsigned long)part;
Dean Nelsona607c382005-09-01 14:01:37 -05001195
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001196 part->setup_state = XPC_P_UNSET;
1197 init_waitqueue_head(&part->teardown_wq);
1198 atomic_set(&part->references, 0);
1199 }
1200
Dean Nelsonbc63d382008-07-29 22:34:04 -07001201 xpc_sysctl = register_sysctl_table(xpc_sys_dir);
1202
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001203 /*
1204 * Open up protections for IPI operations (and AMO operations on
1205 * Shub 1.1 systems).
1206 */
1207 xpc_allow_IPI_ops();
1208
1209 /*
1210 * Interrupts being processed will increment this atomic variable and
1211 * awaken the heartbeat thread which will process the interrupts.
1212 */
1213 atomic_set(&xpc_act_IRQ_rcvd, 0);
1214
1215 /*
1216 * This is safe to do before the xpc_hb_checker thread has started
1217 * because the handler releases a wait queue. If an interrupt is
1218 * received before the thread is waiting, it will not go to sleep,
1219 * but rather immediately process the interrupt.
1220 */
1221 ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
Dean Nelson35190502008-04-22 14:48:55 -05001222 "xpc hb", NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001223 if (ret != 0) {
1224 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
1225 "errno=%d\n", -ret);
Dean Nelsonbc63d382008-07-29 22:34:04 -07001226 ret = -EBUSY;
1227 goto out_2;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001228 }
1229
1230 /*
1231 * Fill the partition reserved page with the information needed by
1232 * other partitions to discover we are alive and establish initial
1233 * communications.
1234 */
Dean Nelson94bd2702008-07-29 22:34:05 -07001235 xpc_rsvd_page = xpc_setup_rsvd_page();
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001236 if (xpc_rsvd_page == NULL) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001237 dev_err(xpc_part, "can't setup our reserved page\n");
1238 ret = -EBUSY;
1239 goto out_3;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001240 }
1241
Dean Nelsona607c382005-09-01 14:01:37 -05001242 /* add ourselves to the reboot_notifier_list */
1243 ret = register_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001244 if (ret != 0)
Dean Nelsona607c382005-09-01 14:01:37 -05001245 dev_warn(xpc_part, "can't register reboot notifier\n");
Dean Nelsona607c382005-09-01 14:01:37 -05001246
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07001247 /* add ourselves to the die_notifier list */
Dean Nelson780d09e2005-11-09 14:41:57 -06001248 ret = register_die_notifier(&xpc_die_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001249 if (ret != 0)
Dean Nelson780d09e2005-11-09 14:41:57 -06001250 dev_warn(xpc_part, "can't register die notifier\n");
Dean Nelson780d09e2005-11-09 14:41:57 -06001251
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001252 init_timer(&xpc_hb_timer);
1253 xpc_hb_timer.function = xpc_hb_beater;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001254
1255 /*
1256 * The real work-horse behind xpc. This processes incoming
1257 * interrupts and monitors remote heartbeats.
1258 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001259 kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
1260 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001261 dev_err(xpc_part, "failed while forking hb check thread\n");
Dean Nelsonbc63d382008-07-29 22:34:04 -07001262 ret = -EBUSY;
1263 goto out_4;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001264 }
1265
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001266 /*
1267 * Startup a thread that will attempt to discover other partitions to
1268 * activate based on info provided by SAL. This new thread is short
1269 * lived and will exit once discovery is complete.
1270 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001271 kthread = kthread_run(xpc_initiate_discovery, NULL,
1272 XPC_DISCOVERY_THREAD_NAME);
1273 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001274 dev_err(xpc_part, "failed while forking discovery thread\n");
1275
1276 /* mark this new thread as a non-starter */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001277 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001278
Dean Nelson65c17b82008-05-12 14:02:02 -07001279 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001280 return -EBUSY;
1281 }
1282
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001283 /* set the interface to point at XPC's functions */
1284 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
1285 xpc_initiate_allocate, xpc_initiate_send,
1286 xpc_initiate_send_notify, xpc_initiate_received,
1287 xpc_initiate_partid_to_nasids);
1288
1289 return 0;
Dean Nelsonbc63d382008-07-29 22:34:04 -07001290
1291 /* initialization was not successful */
1292out_4:
1293 /* indicate to others that our reserved page is uninitialized */
Dean Nelson94bd2702008-07-29 22:34:05 -07001294 xpc_rsvd_page->stamp = ZERO_STAMP;
1295
Dean Nelsonbc63d382008-07-29 22:34:04 -07001296 del_timer_sync(&xpc_hb_timer);
1297 (void)unregister_die_notifier(&xpc_die_notifier);
1298 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
1299out_3:
1300 free_irq(SGI_XPC_ACTIVATE, NULL);
1301out_2:
1302 xpc_restrict_IPI_ops();
1303 if (xpc_sysctl)
1304 unregister_sysctl_table(xpc_sysctl);
1305 kfree(xpc_partitions);
1306out_1:
1307 kfree(xpc_remote_copy_buffer_base);
1308 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001309}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001310
Dean Nelson35190502008-04-22 14:48:55 -05001311module_init(xpc_init);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001312
1313void __exit
1314xpc_exit(void)
1315{
Dean Nelson65c17b82008-05-12 14:02:02 -07001316 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001317}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001318
Dean Nelson35190502008-04-22 14:48:55 -05001319module_exit(xpc_exit);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001320
1321MODULE_AUTHOR("Silicon Graphics, Inc.");
1322MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1323MODULE_LICENSE("GPL");
1324
1325module_param(xpc_hb_interval, int, 0);
1326MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001327 "heartbeat increments.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001328
1329module_param(xpc_hb_check_interval, int, 0);
1330MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001331 "heartbeat checks.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001332
Dean Nelsone54af722005-10-25 14:07:43 -05001333module_param(xpc_disengage_request_timelimit, int, 0);
1334MODULE_PARM_DESC(xpc_disengage_request_timelimit, "Number of seconds to wait "
Dean Nelson35190502008-04-22 14:48:55 -05001335 "for disengage request to complete.");
Dean Nelsone54af722005-10-25 14:07:43 -05001336
Dean Nelson1f4674b2006-01-10 11:08:00 -06001337module_param(xpc_kdebug_ignore, int, 0);
1338MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
Dean Nelson35190502008-04-22 14:48:55 -05001339 "other partitions when dropping into kdebug.");