blob: aae90f5933b52dd95a8bfc8cce9f0888275ac0fc [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 */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700151atomic_t xpc_act_IRQ_rcvd;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700152
153/* IRQ handler notifies this wait queue on receipt of an IRQ */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700154DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700155
156static unsigned long xpc_hb_check_timeout;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700157static struct timer_list xpc_hb_timer;
158void *xpc_heartbeating_to_mask;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700159
Dean Nelsone54af722005-10-25 14:07:43 -0500160/* notification that the xpc_hb_checker thread has exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500161static DECLARE_COMPLETION(xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700162
Dean Nelsone54af722005-10-25 14:07:43 -0500163/* notification that the xpc_discovery thread has exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500164static DECLARE_COMPLETION(xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700165
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);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700179void (*xpc_heartbeat_init) (void);
180void (*xpc_heartbeat_exit) (void);
181void (*xpc_increment_heartbeat) (void);
182void (*xpc_offline_heartbeat) (void);
183void (*xpc_online_heartbeat) (void);
184void (*xpc_check_remote_hb) (void);
185
Dean Nelsone17d4162008-07-29 22:34:06 -0700186enum xp_retval (*xpc_make_first_contact) (struct xpc_partition *part);
187u64 (*xpc_get_IPI_flags) (struct xpc_partition *part);
188struct xpc_msg *(*xpc_get_deliverable_msg) (struct xpc_channel *ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700189
190void (*xpc_initiate_partition_activation) (struct xpc_rsvd_page *remote_rp,
191 u64 remote_rp_pa, int nasid);
192
193void (*xpc_process_act_IRQ_rcvd) (int n_IRQs_expected);
Dean Nelsone17d4162008-07-29 22:34:06 -0700194enum xp_retval (*xpc_setup_infrastructure) (struct xpc_partition *part);
195void (*xpc_teardown_infrastructure) (struct xpc_partition *part);
196
Dean Nelson33ba3c72008-07-29 22:34:07 -0700197void (*xpc_mark_partition_engaged) (struct xpc_partition *part);
198void (*xpc_mark_partition_disengaged) (struct xpc_partition *part);
199void (*xpc_request_partition_disengage) (struct xpc_partition *part);
200void (*xpc_cancel_partition_disengage_request) (struct xpc_partition *part);
201u64 (*xpc_partition_engaged) (u64 partid_mask);
202u64 (*xpc_partition_disengage_requested) (u64 partid_mask);
203void (*xpc_clear_partition_engaged) (u64 partid_mask);
204void (*xpc_clear_partition_disengage_request) (u64 partid_mask);
205
206void (*xpc_IPI_send_local_activate) (int from_nasid);
207void (*xpc_IPI_send_activated) (struct xpc_partition *part);
208void (*xpc_IPI_send_local_reactivate) (int from_nasid);
209void (*xpc_IPI_send_disengage) (struct xpc_partition *part);
210
211void (*xpc_IPI_send_closerequest) (struct xpc_channel *ch,
212 unsigned long *irq_flags);
213void (*xpc_IPI_send_closereply) (struct xpc_channel *ch,
214 unsigned long *irq_flags);
215void (*xpc_IPI_send_openrequest) (struct xpc_channel *ch,
216 unsigned long *irq_flags);
217void (*xpc_IPI_send_openreply) (struct xpc_channel *ch,
218 unsigned long *irq_flags);
219
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700220enum xp_retval (*xpc_send_msg) (struct xpc_channel *ch, u32 flags,
221 void *payload, u16 payload_size, u8 notify_type,
222 xpc_notify_func func, void *key);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700223void (*xpc_received_msg) (struct xpc_channel *ch, struct xpc_msg *msg);
Dean Nelson94bd2702008-07-29 22:34:05 -0700224
Dean Nelsona607c382005-09-01 14:01:37 -0500225/*
226 * Timer function to enforce the timelimit on the partition disengage request.
227 */
228static void
229xpc_timeout_partition_disengage_request(unsigned long data)
230{
Dean Nelson35190502008-04-22 14:48:55 -0500231 struct xpc_partition *part = (struct xpc_partition *)data;
Dean Nelsona607c382005-09-01 14:01:37 -0500232
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700233 DBUG_ON(time_is_after_jiffies(part->disengage_request_timeout));
Dean Nelsona607c382005-09-01 14:01:37 -0500234
Dean Nelson35190502008-04-22 14:48:55 -0500235 (void)xpc_partition_disengaged(part);
Dean Nelsona607c382005-09-01 14:01:37 -0500236
237 DBUG_ON(part->disengage_request_timeout != 0);
238 DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
239}
240
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700241/*
242 * Notify the heartbeat check thread that an IRQ has been received.
243 */
244static irqreturn_t
Al Viro5dcded12006-10-08 14:59:19 +0100245xpc_act_IRQ_handler(int irq, void *dev_id)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700246{
247 atomic_inc(&xpc_act_IRQ_rcvd);
248 wake_up_interruptible(&xpc_act_IRQ_wq);
249 return IRQ_HANDLED;
250}
251
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700252/*
253 * Timer to produce the heartbeat. The timer structures function is
254 * already set when this is initially called. A tunable is used to
255 * specify when the next timeout should occur.
256 */
257static void
258xpc_hb_beater(unsigned long dummy)
259{
Dean Nelson33ba3c72008-07-29 22:34:07 -0700260 xpc_increment_heartbeat();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700261
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700262 if (time_is_before_eq_jiffies(xpc_hb_check_timeout))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700263 wake_up_interruptible(&xpc_act_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700264
265 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
266 add_timer(&xpc_hb_timer);
267}
268
Dean Nelson33ba3c72008-07-29 22:34:07 -0700269static void
270xpc_start_hb_beater(void)
271{
272 xpc_heartbeat_init();
273 init_timer(&xpc_hb_timer);
274 xpc_hb_timer.function = xpc_hb_beater;
275 xpc_hb_beater(0);
276}
277
278static void
279xpc_stop_hb_beater(void)
280{
281 del_timer_sync(&xpc_hb_timer);
282 xpc_heartbeat_exit();
283}
284
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700285/*
286 * This thread is responsible for nearly all of the partition
287 * activation/deactivation.
288 */
289static int
290xpc_hb_checker(void *ignore)
291{
292 int last_IRQ_count = 0;
293 int new_IRQ_count;
Dean Nelson35190502008-04-22 14:48:55 -0500294 int force_IRQ = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700295
296 /* this thread was marked active by xpc_hb_init() */
297
Mike Travis0bc3cc02008-07-24 18:21:31 -0700298 set_cpus_allowed_ptr(current, &cpumask_of_cpu(XPC_HB_CHECK_CPU));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700299
Dean Nelson4c013f52007-11-07 07:53:06 -0600300 /* set our heartbeating to other partitions into motion */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700301 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700302 xpc_start_hb_beater();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700303
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500304 while (!xpc_exiting) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700305
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700306 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
307 "been received\n",
Dean Nelson35190502008-04-22 14:48:55 -0500308 (int)(xpc_hb_check_timeout - jiffies),
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700309 atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
310
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700311 /* checking of remote heartbeats is skewed by IRQ handling */
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700312 if (time_is_before_eq_jiffies(xpc_hb_check_timeout)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700313 dev_dbg(xpc_part, "checking remote heartbeats\n");
314 xpc_check_remote_hb();
315
316 /*
317 * We need to periodically recheck to ensure no
318 * IPI/AMO pairs have been missed. That check
319 * must always reset xpc_hb_check_timeout.
320 */
321 force_IRQ = 1;
322 }
323
Dean Nelsona607c382005-09-01 14:01:37 -0500324 /* check for outstanding IRQs */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700325 new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
326 if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
327 force_IRQ = 0;
328
329 dev_dbg(xpc_part, "found an IRQ to process; will be "
330 "resetting xpc_hb_check_timeout\n");
331
Dean Nelson33ba3c72008-07-29 22:34:07 -0700332 xpc_process_act_IRQ_rcvd(new_IRQ_count -
333 last_IRQ_count);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700334 last_IRQ_count = new_IRQ_count;
335
336 xpc_hb_check_timeout = jiffies +
Dean Nelson35190502008-04-22 14:48:55 -0500337 (xpc_hb_check_interval * HZ);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700338 }
Dean Nelsona607c382005-09-01 14:01:37 -0500339
340 /* wait for IRQ or timeout */
Dean Nelson35190502008-04-22 14:48:55 -0500341 (void)wait_event_interruptible(xpc_act_IRQ_wq,
342 (last_IRQ_count <
343 atomic_read(&xpc_act_IRQ_rcvd)
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700344 || time_is_before_eq_jiffies(
345 xpc_hb_check_timeout) ||
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500346 xpc_exiting));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700347 }
348
Dean Nelson33ba3c72008-07-29 22:34:07 -0700349 xpc_stop_hb_beater();
350
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700351 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
352
Dean Nelsone54af722005-10-25 14:07:43 -0500353 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500354 complete(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700355 return 0;
356}
357
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700358/*
359 * This thread will attempt to discover other partitions to activate
360 * based on info provided by SAL. This new thread is short lived and
361 * will exit once discovery is complete.
362 */
363static int
364xpc_initiate_discovery(void *ignore)
365{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700366 xpc_discovery();
367
368 dev_dbg(xpc_part, "discovery thread is exiting\n");
369
Dean Nelsone54af722005-10-25 14:07:43 -0500370 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500371 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700372 return 0;
373}
374
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700375/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700376 * The first kthread assigned to a newly activated partition is the one
Dean Nelsone17d4162008-07-29 22:34:06 -0700377 * created by XPC HB with which it calls xpc_activating(). XPC hangs on to
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700378 * that kthread until the partition is brought down, at which time that kthread
379 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
380 * that XPC has dismantled all communication infrastructure for the associated
381 * partition.) This kthread becomes the channel manager for that partition.
382 *
383 * Each active partition has a channel manager, who, besides connecting and
384 * disconnecting channels, will ensure that each of the partition's connected
385 * channels has the required number of assigned kthreads to get the work done.
386 */
387static void
388xpc_channel_mgr(struct xpc_partition *part)
389{
390 while (part->act_state != XPC_P_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500391 atomic_read(&part->nchannels_active) > 0 ||
392 !xpc_partition_disengaged(part)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700393
394 xpc_process_channel_activity(part);
395
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700396 /*
397 * Wait until we've been requested to activate kthreads or
398 * all of the channel's message queues have been torn down or
399 * a signal is pending.
400 *
401 * The channel_mgr_requests is set to 1 after being awakened,
402 * This is done to prevent the channel mgr from making one pass
403 * through the loop for each request, since he will
404 * be servicing all the requests in one pass. The reason it's
405 * set to 1 instead of 0 is so that other kthreads will know
406 * that the channel mgr is running and won't bother trying to
407 * wake him up.
408 */
409 atomic_dec(&part->channel_mgr_requests);
Dean Nelson35190502008-04-22 14:48:55 -0500410 (void)wait_event_interruptible(part->channel_mgr_wq,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500411 (atomic_read(&part->channel_mgr_requests) > 0 ||
412 part->local_IPI_amo != 0 ||
413 (part->act_state == XPC_P_DEACTIVATING &&
414 atomic_read(&part->nchannels_active) == 0 &&
415 xpc_partition_disengaged(part))));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700416 atomic_set(&part->channel_mgr_requests, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700417 }
418}
419
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700420/*
421 * When XPC HB determines that a partition has come up, it will create a new
422 * kthread and that kthread will call this function to attempt to set up the
423 * basic infrastructure used for Cross Partition Communication with the newly
424 * upped partition.
425 *
426 * The kthread that was created by XPC HB and which setup the XPC
Dean Nelsone17d4162008-07-29 22:34:06 -0700427 * infrastructure will remain assigned to the partition becoming the channel
428 * manager for that partition until the partition is deactivating, at which
429 * time the kthread will teardown the XPC infrastructure and then exit.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700430 */
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
Dean Nelsone17d4162008-07-29 22:34:06 -0700456 dev_dbg(xpc_part, "activating partition %d\n", partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700457
Dean Nelson33ba3c72008-07-29 22:34:07 -0700458 xpc_allow_hb(partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700459
Dean Nelsone17d4162008-07-29 22:34:06 -0700460 if (xpc_setup_infrastructure(part) == xpSuccess) {
461 (void)xpc_part_ref(part); /* this will always succeed */
462
463 if (xpc_make_first_contact(part) == xpSuccess) {
464 xpc_mark_partition_active(part);
465 xpc_channel_mgr(part);
466 /* won't return until partition is deactivating */
467 }
468
469 xpc_part_deref(part);
470 xpc_teardown_infrastructure(part);
471 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700472
Dean Nelson33ba3c72008-07-29 22:34:07 -0700473 xpc_disallow_hb(partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700474 xpc_mark_partition_inactive(part);
475
Dean Nelson65c17b82008-05-12 14:02:02 -0700476 if (part->reason == xpReactivating) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700477 /* interrupting ourselves results in activating partition */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700478 xpc_IPI_send_local_reactivate(part->reactivate_nasid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700479 }
480
481 return 0;
482}
483
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700484void
485xpc_activate_partition(struct xpc_partition *part)
486{
Dean Nelson64d032b2008-05-12 14:02:03 -0700487 short partid = XPC_PARTID(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700488 unsigned long irq_flags;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500489 struct task_struct *kthread;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700490
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700491 spin_lock_irqsave(&part->act_lock, irq_flags);
492
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700493 DBUG_ON(part->act_state != XPC_P_INACTIVE);
494
Robin Holt7c6c6632006-02-02 12:30:21 -0600495 part->act_state = XPC_P_ACTIVATION_REQ;
Dean Nelson65c17b82008-05-12 14:02:02 -0700496 XPC_SET_REASON(part, xpCloneKThread, __LINE__);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700497
498 spin_unlock_irqrestore(&part->act_lock, irq_flags);
Robin Holt7c6c6632006-02-02 12:30:21 -0600499
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500500 kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
501 partid);
502 if (IS_ERR(kthread)) {
Robin Holt7c6c6632006-02-02 12:30:21 -0600503 spin_lock_irqsave(&part->act_lock, irq_flags);
504 part->act_state = XPC_P_INACTIVE;
Dean Nelson65c17b82008-05-12 14:02:02 -0700505 XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
Robin Holt7c6c6632006-02-02 12:30:21 -0600506 spin_unlock_irqrestore(&part->act_lock, irq_flags);
507 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700508}
509
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700510/*
Dean Nelson33ba3c72008-07-29 22:34:07 -0700511 * Check to see if there is any channel activity to/from the specified
512 * partition.
513 */
514static void
515xpc_check_for_channel_activity(struct xpc_partition *part)
516{
517 u64 IPI_amo;
518 unsigned long irq_flags;
519
520/* this needs to be uncommented, but I'm thinking this function and the */
521/* ones that call it need to be moved into xpc_sn2.c... */
522 IPI_amo = 0; /* = xpc_IPI_receive(part->local_IPI_amo_va); */
523 if (IPI_amo == 0)
524 return;
525
526 spin_lock_irqsave(&part->IPI_lock, irq_flags);
527 part->local_IPI_amo |= IPI_amo;
528 spin_unlock_irqrestore(&part->IPI_lock, irq_flags);
529
530 dev_dbg(xpc_chan, "received IPI from partid=%d, IPI_amo=0x%lx\n",
531 XPC_PARTID(part), IPI_amo);
532
533 xpc_wakeup_channel_mgr(part);
534}
535
536/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700537 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
538 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
539 * than one partition, we use an AMO_t structure per partition to indicate
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500540 * whether a partition has sent an IPI or not. If it has, then wake up the
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700541 * associated kthread to handle it.
542 *
543 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
544 * running on other partitions.
545 *
546 * Noteworthy Arguments:
547 *
548 * irq - Interrupt ReQuest number. NOT USED.
549 *
550 * dev_id - partid of IPI's potential sender.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700551 */
552irqreturn_t
Al Viro5dcded12006-10-08 14:59:19 +0100553xpc_notify_IRQ_handler(int irq, void *dev_id)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700554{
Dean Nelson64d032b2008-05-12 14:02:03 -0700555 short partid = (short)(u64)dev_id;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700556 struct xpc_partition *part = &xpc_partitions[partid];
557
Dean Nelsonbc63d382008-07-29 22:34:04 -0700558 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700559
560 if (xpc_part_ref(part)) {
561 xpc_check_for_channel_activity(part);
562
563 xpc_part_deref(part);
564 }
565 return IRQ_HANDLED;
566}
567
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700568/*
569 * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
570 * because the write to their associated IPI amo completed after the IRQ/IPI
571 * was received.
572 */
573void
574xpc_dropped_IPI_check(struct xpc_partition *part)
575{
576 if (xpc_part_ref(part)) {
577 xpc_check_for_channel_activity(part);
578
579 part->dropped_IPI_timer.expires = jiffies +
Dean Nelsone17d4162008-07-29 22:34:06 -0700580 XPC_P_DROPPED_IPI_WAIT_INTERVAL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700581 add_timer(&part->dropped_IPI_timer);
582 xpc_part_deref(part);
583 }
584}
585
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700586void
587xpc_activate_kthreads(struct xpc_channel *ch, int needed)
588{
589 int idle = atomic_read(&ch->kthreads_idle);
590 int assigned = atomic_read(&ch->kthreads_assigned);
591 int wakeup;
592
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700593 DBUG_ON(needed <= 0);
594
595 if (idle > 0) {
596 wakeup = (needed > idle) ? idle : needed;
597 needed -= wakeup;
598
599 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
600 "channel=%d\n", wakeup, ch->partid, ch->number);
601
602 /* only wakeup the requested number of kthreads */
603 wake_up_nr(&ch->idle_wq, wakeup);
604 }
605
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500606 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700607 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700608
609 if (needed + assigned > ch->kthreads_assigned_limit) {
610 needed = ch->kthreads_assigned_limit - assigned;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500611 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700612 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700613 }
614
615 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
616 needed, ch->partid, ch->number);
617
Dean Nelsona460ef82006-11-22 08:25:00 -0600618 xpc_create_kthreads(ch, needed, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700619}
620
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700621/*
622 * This function is where XPC's kthreads wait for messages to deliver.
623 */
624static void
625xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
626{
627 do {
628 /* deliver messages to their intended recipients */
629
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500630 while (ch->w_local_GP.get < ch->w_remote_GP.put &&
631 !(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700632 xpc_deliver_msg(ch);
633 }
634
635 if (atomic_inc_return(&ch->kthreads_idle) >
Dean Nelson35190502008-04-22 14:48:55 -0500636 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700637 /* too many idle kthreads on this channel */
638 atomic_dec(&ch->kthreads_idle);
639 break;
640 }
641
642 dev_dbg(xpc_chan, "idle kthread calling "
643 "wait_event_interruptible_exclusive()\n");
644
Dean Nelson35190502008-04-22 14:48:55 -0500645 (void)wait_event_interruptible_exclusive(ch->idle_wq,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500646 (ch->w_local_GP.get < ch->w_remote_GP.put ||
647 (ch->flags & XPC_C_DISCONNECTING)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700648
649 atomic_dec(&ch->kthreads_idle);
650
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500651 } while (!(ch->flags & XPC_C_DISCONNECTING));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700652}
653
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700654static int
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500655xpc_kthread_start(void *args)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700656{
Dean Nelson64d032b2008-05-12 14:02:03 -0700657 short partid = XPC_UNPACK_ARG1(args);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700658 u16 ch_number = XPC_UNPACK_ARG2(args);
659 struct xpc_partition *part = &xpc_partitions[partid];
660 struct xpc_channel *ch;
661 int n_needed;
Dean Nelsone54af722005-10-25 14:07:43 -0500662 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700663
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700664 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
665 partid, ch_number);
666
667 ch = &part->channels[ch_number];
668
669 if (!(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700670
671 /* let registerer know that connection has been established */
672
Dean Nelsone54af722005-10-25 14:07:43 -0500673 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson4c2cd962006-02-15 08:02:21 -0600674 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
675 ch->flags |= XPC_C_CONNECTEDCALLOUT;
Dean Nelsone54af722005-10-25 14:07:43 -0500676 spin_unlock_irqrestore(&ch->lock, irq_flags);
677
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700678 xpc_connected_callout(ch);
679
Dean Nelson4c2cd962006-02-15 08:02:21 -0600680 spin_lock_irqsave(&ch->lock, irq_flags);
681 ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
682 spin_unlock_irqrestore(&ch->lock, irq_flags);
683
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700684 /*
685 * It is possible that while the callout was being
686 * made that the remote partition sent some messages.
687 * If that is the case, we may need to activate
688 * additional kthreads to help deliver them. We only
689 * need one less than total #of messages to deliver.
690 */
691 n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500692 if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700693 xpc_activate_kthreads(ch, n_needed);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500694
Dean Nelsone54af722005-10-25 14:07:43 -0500695 } else {
696 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700697 }
698
699 xpc_kthread_waitmsgs(part, ch);
700 }
701
Dean Nelsona460ef82006-11-22 08:25:00 -0600702 /* let registerer know that connection is disconnecting */
Dean Nelsone54af722005-10-25 14:07:43 -0500703
Dean Nelsona460ef82006-11-22 08:25:00 -0600704 spin_lock_irqsave(&ch->lock, irq_flags);
705 if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500706 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelsona460ef82006-11-22 08:25:00 -0600707 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
Dean Nelson4c2cd962006-02-15 08:02:21 -0600708 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -0600709
Dean Nelson65c17b82008-05-12 14:02:02 -0700710 xpc_disconnect_callout(ch, xpDisconnecting);
Dean Nelsona460ef82006-11-22 08:25:00 -0600711
712 spin_lock_irqsave(&ch->lock, irq_flags);
713 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
714 }
715 spin_unlock_irqrestore(&ch->lock, irq_flags);
716
717 if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
Dean Nelsona607c382005-09-01 14:01:37 -0500718 if (atomic_dec_return(&part->nchannels_engaged) == 0) {
719 xpc_mark_partition_disengaged(part);
720 xpc_IPI_send_disengage(part);
721 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700722 }
723
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700724 xpc_msgqueue_deref(ch);
725
726 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
727 partid, ch_number);
728
729 xpc_part_deref(part);
730 return 0;
731}
732
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700733/*
734 * For each partition that XPC has established communications with, there is
735 * a minimum of one kernel thread assigned to perform any operation that
736 * may potentially sleep or block (basically the callouts to the asynchronous
737 * functions registered via xpc_connect()).
738 *
739 * Additional kthreads are created and destroyed by XPC as the workload
740 * demands.
741 *
742 * A kthread is assigned to one of the active channels that exists for a given
743 * partition.
744 */
745void
Dean Nelsona460ef82006-11-22 08:25:00 -0600746xpc_create_kthreads(struct xpc_channel *ch, int needed,
Dean Nelson35190502008-04-22 14:48:55 -0500747 int ignore_disconnecting)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700748{
749 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700750 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
Dean Nelsona607c382005-09-01 14:01:37 -0500751 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500752 struct task_struct *kthread;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700753
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700754 while (needed-- > 0) {
Dean Nelsone54af722005-10-25 14:07:43 -0500755
756 /*
757 * The following is done on behalf of the newly created
758 * kthread. That kthread is responsible for doing the
759 * counterpart to the following before it exits.
760 */
Dean Nelsona460ef82006-11-22 08:25:00 -0600761 if (ignore_disconnecting) {
762 if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
763 /* kthreads assigned had gone to zero */
764 BUG_ON(!(ch->flags &
Dean Nelson35190502008-04-22 14:48:55 -0500765 XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelsona460ef82006-11-22 08:25:00 -0600766 break;
767 }
768
769 } else if (ch->flags & XPC_C_DISCONNECTING) {
770 break;
771
772 } else if (atomic_inc_return(&ch->kthreads_assigned) == 1) {
773 if (atomic_inc_return(&part->nchannels_engaged) == 1)
774 xpc_mark_partition_engaged(part);
775 }
Dean Nelson35190502008-04-22 14:48:55 -0500776 (void)xpc_part_ref(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500777 xpc_msgqueue_ref(ch);
Dean Nelsone54af722005-10-25 14:07:43 -0500778
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500779 kthread = kthread_run(xpc_kthread_start, (void *)args,
780 "xpc%02dc%d", ch->partid, ch->number);
781 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700782 /* the fork failed */
Dean Nelsona460ef82006-11-22 08:25:00 -0600783
784 /*
785 * NOTE: if (ignore_disconnecting &&
786 * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
787 * then we'll deadlock if all other kthreads assigned
788 * to this channel are blocked in the channel's
789 * registerer, because the only thing that will unblock
Dean Nelson65c17b82008-05-12 14:02:02 -0700790 * them is the xpDisconnecting callout that this
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500791 * failed kthread_run() would have made.
Dean Nelsona460ef82006-11-22 08:25:00 -0600792 */
793
Dean Nelsone54af722005-10-25 14:07:43 -0500794 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
795 atomic_dec_return(&part->nchannels_engaged) == 0) {
796 xpc_mark_partition_disengaged(part);
797 xpc_IPI_send_disengage(part);
798 }
799 xpc_msgqueue_deref(ch);
800 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700801
802 if (atomic_read(&ch->kthreads_assigned) <
Dean Nelson35190502008-04-22 14:48:55 -0500803 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700804 /*
805 * Flag this as an error only if we have an
806 * insufficient #of kthreads for the channel
807 * to function.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700808 */
809 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700810 XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
Dean Nelson35190502008-04-22 14:48:55 -0500811 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700812 spin_unlock_irqrestore(&ch->lock, irq_flags);
813 }
814 break;
815 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700816 }
817}
818
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700819void
820xpc_disconnect_wait(int ch_number)
821{
Dean Nelsona607c382005-09-01 14:01:37 -0500822 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700823 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700824 struct xpc_partition *part;
825 struct xpc_channel *ch;
Dean Nelsone54af722005-10-25 14:07:43 -0500826 int wakeup_channel_mgr;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700827
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700828 /* now wait for all callouts to the caller's function to cease */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700829 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700830 part = &xpc_partitions[partid];
831
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500832 if (!xpc_part_ref(part))
Dean Nelsone54af722005-10-25 14:07:43 -0500833 continue;
Dean Nelsone54af722005-10-25 14:07:43 -0500834
835 ch = &part->channels[ch_number];
836
837 if (!(ch->flags & XPC_C_WDISCONNECT)) {
838 xpc_part_deref(part);
839 continue;
840 }
841
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500842 wait_for_completion(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500843
844 spin_lock_irqsave(&ch->lock, irq_flags);
845 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
846 wakeup_channel_mgr = 0;
847
848 if (ch->delayed_IPI_flags) {
849 if (part->act_state != XPC_P_DEACTIVATING) {
850 spin_lock(&part->IPI_lock);
851 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500852 ch->number,
853 ch->delayed_IPI_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500854 spin_unlock(&part->IPI_lock);
855 wakeup_channel_mgr = 1;
856 }
857 ch->delayed_IPI_flags = 0;
858 }
859
860 ch->flags &= ~XPC_C_WDISCONNECT;
861 spin_unlock_irqrestore(&ch->lock, irq_flags);
862
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500863 if (wakeup_channel_mgr)
Dean Nelsone54af722005-10-25 14:07:43 -0500864 xpc_wakeup_channel_mgr(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500865
866 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700867 }
868}
869
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700870static void
Dean Nelson65c17b82008-05-12 14:02:02 -0700871xpc_do_exit(enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700872{
Dean Nelson64d032b2008-05-12 14:02:03 -0700873 short partid;
Dean Nelson1ecaded2006-01-06 09:48:21 -0600874 int active_part_count, printed_waiting_msg = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700875 struct xpc_partition *part;
Dean Nelson1ecaded2006-01-06 09:48:21 -0600876 unsigned long printmsg_time, disengage_request_timeout = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700877
Dean Nelsona607c382005-09-01 14:01:37 -0500878 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
879 DBUG_ON(xpc_exiting == 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700880
881 /*
Dean Nelsona607c382005-09-01 14:01:37 -0500882 * Let the heartbeat checker thread and the discovery thread
883 * (if one is running) know that they should exit. Also wake up
884 * the heartbeat checker thread in case it's sleeping.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700885 */
886 xpc_exiting = 1;
887 wake_up_interruptible(&xpc_act_IRQ_wq);
888
Dean Nelsona607c382005-09-01 14:01:37 -0500889 /* ignore all incoming interrupts */
890 free_irq(SGI_XPC_ACTIVATE, NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700891
Dean Nelsone54af722005-10-25 14:07:43 -0500892 /* wait for the discovery thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500893 wait_for_completion(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700894
Dean Nelsone54af722005-10-25 14:07:43 -0500895 /* wait for the heartbeat checker thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500896 wait_for_completion(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700897
Dean Nelsona607c382005-09-01 14:01:37 -0500898 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -0500899 (void)msleep_interruptible(300);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700900
901 /* wait for all partitions to become inactive */
902
Dean Nelson1ecaded2006-01-06 09:48:21 -0600903 printmsg_time = jiffies + (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
904 xpc_disengage_request_timedout = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500905
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700906 do {
907 active_part_count = 0;
908
Dean Nelsonbc63d382008-07-29 22:34:04 -0700909 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700910 part = &xpc_partitions[partid];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700911
Dean Nelsona607c382005-09-01 14:01:37 -0500912 if (xpc_partition_disengaged(part) &&
Dean Nelson35190502008-04-22 14:48:55 -0500913 part->act_state == XPC_P_INACTIVE) {
Dean Nelsona607c382005-09-01 14:01:37 -0500914 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700915 }
Dean Nelsona607c382005-09-01 14:01:37 -0500916
917 active_part_count++;
918
919 XPC_DEACTIVATE_PARTITION(part, reason);
Dean Nelson1ecaded2006-01-06 09:48:21 -0600920
921 if (part->disengage_request_timeout >
Dean Nelson35190502008-04-22 14:48:55 -0500922 disengage_request_timeout) {
Dean Nelson1ecaded2006-01-06 09:48:21 -0600923 disengage_request_timeout =
Dean Nelson35190502008-04-22 14:48:55 -0500924 part->disengage_request_timeout;
Dean Nelson1ecaded2006-01-06 09:48:21 -0600925 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700926 }
927
Dean Nelson1ecaded2006-01-06 09:48:21 -0600928 if (xpc_partition_engaged(-1UL)) {
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700929 if (time_is_before_jiffies(printmsg_time)) {
Dean Nelson1ecaded2006-01-06 09:48:21 -0600930 dev_info(xpc_part, "waiting for remote "
Dean Nelson35190502008-04-22 14:48:55 -0500931 "partitions to disengage, timeout in "
932 "%ld seconds\n",
933 (disengage_request_timeout - jiffies)
934 / HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -0600935 printmsg_time = jiffies +
Dean Nelson35190502008-04-22 14:48:55 -0500936 (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -0600937 printed_waiting_msg = 1;
938 }
939
940 } else if (active_part_count > 0) {
941 if (printed_waiting_msg) {
942 dev_info(xpc_part, "waiting for local partition"
Dean Nelson35190502008-04-22 14:48:55 -0500943 " to disengage\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -0600944 printed_waiting_msg = 0;
945 }
946
947 } else {
948 if (!xpc_disengage_request_timedout) {
949 dev_info(xpc_part, "all partitions have "
Dean Nelson35190502008-04-22 14:48:55 -0500950 "disengaged\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -0600951 }
952 break;
Dean Nelsona607c382005-09-01 14:01:37 -0500953 }
954
955 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -0500956 (void)msleep_interruptible(300);
Dean Nelsona607c382005-09-01 14:01:37 -0500957
958 } while (1);
959
960 DBUG_ON(xpc_partition_engaged(-1UL));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700961 DBUG_ON(xpc_any_hbs_allowed() != 0);
Dean Nelsona607c382005-09-01 14:01:37 -0500962
Dean Nelsona607c382005-09-01 14:01:37 -0500963 /* indicate to others that our reserved page is uninitialized */
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700964 xpc_rsvd_page->stamp = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500965
Dean Nelson65c17b82008-05-12 14:02:02 -0700966 if (reason == xpUnloading) {
Dean Nelson35190502008-04-22 14:48:55 -0500967 (void)unregister_die_notifier(&xpc_die_notifier);
Dean Nelsonbc63d382008-07-29 22:34:04 -0700968 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson0752c672006-01-10 11:07:19 -0600969 }
Dean Nelson780d09e2005-11-09 14:41:57 -0600970
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700971 /* close down protections for IPI operations */
972 xpc_restrict_IPI_ops();
973
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700974 /* clear the interface to XPC's functions */
975 xpc_clear_interface();
976
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500977 if (xpc_sysctl)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700978 unregister_sysctl_table(xpc_sysctl);
Dean Nelson7682a4c2006-08-08 15:03:29 -0500979
Dean Nelsonbc63d382008-07-29 22:34:04 -0700980 kfree(xpc_partitions);
Dean Nelson7682a4c2006-08-08 15:03:29 -0500981 kfree(xpc_remote_copy_buffer_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700982}
983
Dean Nelsona607c382005-09-01 14:01:37 -0500984/*
Dean Nelsond6ad0332006-01-10 11:08:55 -0600985 * This function is called when the system is being rebooted.
986 */
987static int
988xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
989{
Dean Nelson65c17b82008-05-12 14:02:02 -0700990 enum xp_retval reason;
Dean Nelsond6ad0332006-01-10 11:08:55 -0600991
Dean Nelsond6ad0332006-01-10 11:08:55 -0600992 switch (event) {
993 case SYS_RESTART:
Dean Nelson65c17b82008-05-12 14:02:02 -0700994 reason = xpSystemReboot;
Dean Nelsond6ad0332006-01-10 11:08:55 -0600995 break;
996 case SYS_HALT:
Dean Nelson65c17b82008-05-12 14:02:02 -0700997 reason = xpSystemHalt;
Dean Nelsond6ad0332006-01-10 11:08:55 -0600998 break;
999 case SYS_POWER_OFF:
Dean Nelson65c17b82008-05-12 14:02:02 -07001000 reason = xpSystemPoweroff;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001001 break;
1002 default:
Dean Nelson65c17b82008-05-12 14:02:02 -07001003 reason = xpSystemGoingDown;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001004 }
1005
1006 xpc_do_exit(reason);
1007 return NOTIFY_DONE;
1008}
1009
Dean Nelsond6ad0332006-01-10 11:08:55 -06001010/*
1011 * Notify other partitions to disengage from all references to our memory.
Dean Nelson780d09e2005-11-09 14:41:57 -06001012 */
1013static void
1014xpc_die_disengage(void)
1015{
1016 struct xpc_partition *part;
Dean Nelson64d032b2008-05-12 14:02:03 -07001017 short partid;
Dean Nelson780d09e2005-11-09 14:41:57 -06001018 unsigned long engaged;
Dean Nelson1ecaded2006-01-06 09:48:21 -06001019 long time, printmsg_time, disengage_request_timeout;
Dean Nelson780d09e2005-11-09 14:41:57 -06001020
Dean Nelson780d09e2005-11-09 14:41:57 -06001021 /* keep xpc_hb_checker thread from doing anything (just in case) */
1022 xpc_exiting = 1;
1023
Dean Nelson33ba3c72008-07-29 22:34:07 -07001024 xpc_disallow_all_hbs(); /*indicate we're deactivated */
Dean Nelson780d09e2005-11-09 14:41:57 -06001025
Dean Nelsonbc63d382008-07-29 22:34:04 -07001026 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001027 part = &xpc_partitions[partid];
1028
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001029 if (!XPC_SUPPORTS_DISENGAGE_REQUEST(part->
1030 remote_vars_version)) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001031
1032 /* just in case it was left set by an earlier XPC */
1033 xpc_clear_partition_engaged(1UL << partid);
1034 continue;
1035 }
1036
1037 if (xpc_partition_engaged(1UL << partid) ||
Dean Nelson35190502008-04-22 14:48:55 -05001038 part->act_state != XPC_P_INACTIVE) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001039 xpc_request_partition_disengage(part);
1040 xpc_mark_partition_disengaged(part);
1041 xpc_IPI_send_disengage(part);
1042 }
1043 }
1044
Dean Nelson1ecaded2006-01-06 09:48:21 -06001045 time = rtc_time();
1046 printmsg_time = time +
Dean Nelson35190502008-04-22 14:48:55 -05001047 (XPC_DISENGAGE_PRINTMSG_INTERVAL * sn_rtc_cycles_per_second);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001048 disengage_request_timeout = time +
Dean Nelson35190502008-04-22 14:48:55 -05001049 (xpc_disengage_request_timelimit * sn_rtc_cycles_per_second);
Dean Nelson780d09e2005-11-09 14:41:57 -06001050
1051 /* wait for all other partitions to disengage from us */
1052
Dean Nelson1ecaded2006-01-06 09:48:21 -06001053 while (1) {
1054 engaged = xpc_partition_engaged(-1UL);
1055 if (!engaged) {
1056 dev_info(xpc_part, "all partitions have disengaged\n");
1057 break;
1058 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001059
Dean Nelson1ecaded2006-01-06 09:48:21 -06001060 time = rtc_time();
1061 if (time >= disengage_request_timeout) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001062 for (partid = 0; partid < xp_max_npartitions;
1063 partid++) {
Dean Nelson1ecaded2006-01-06 09:48:21 -06001064 if (engaged & (1UL << partid)) {
1065 dev_info(xpc_part, "disengage from "
Dean Nelson35190502008-04-22 14:48:55 -05001066 "remote partition %d timed "
1067 "out\n", partid);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001068 }
1069 }
1070 break;
1071 }
1072
1073 if (time >= printmsg_time) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001074 dev_info(xpc_part, "waiting for remote partitions to "
Dean Nelson35190502008-04-22 14:48:55 -05001075 "disengage, timeout in %ld seconds\n",
1076 (disengage_request_timeout - time) /
1077 sn_rtc_cycles_per_second);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001078 printmsg_time = time +
Dean Nelson35190502008-04-22 14:48:55 -05001079 (XPC_DISENGAGE_PRINTMSG_INTERVAL *
1080 sn_rtc_cycles_per_second);
Dean Nelson780d09e2005-11-09 14:41:57 -06001081 }
1082 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001083}
1084
Dean Nelson780d09e2005-11-09 14:41:57 -06001085/*
Dean Nelson1f4674b2006-01-10 11:08:00 -06001086 * This function is called when the system is being restarted or halted due
1087 * to some sort of system failure. If this is the case we need to notify the
1088 * other partitions to disengage from all references to our memory.
1089 * This function can also be called when our heartbeater could be offlined
1090 * for a time. In this case we need to notify other partitions to not worry
1091 * about the lack of a heartbeat.
Dean Nelson780d09e2005-11-09 14:41:57 -06001092 */
1093static int
1094xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1095{
1096 switch (event) {
1097 case DIE_MACHINE_RESTART:
1098 case DIE_MACHINE_HALT:
1099 xpc_die_disengage();
1100 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001101
1102 case DIE_KDEBUG_ENTER:
1103 /* Should lack of heartbeat be ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001104 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001105 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001106
Dean Nelson1f4674b2006-01-10 11:08:00 -06001107 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001108 case DIE_MCA_MONARCH_ENTER:
1109 case DIE_INIT_MONARCH_ENTER:
Dean Nelson33ba3c72008-07-29 22:34:07 -07001110 xpc_offline_heartbeat();
Dean Nelson780d09e2005-11-09 14:41:57 -06001111 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001112
1113 case DIE_KDEBUG_LEAVE:
1114 /* Is lack of heartbeat being ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001115 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001116 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001117
Dean Nelson1f4674b2006-01-10 11:08:00 -06001118 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001119 case DIE_MCA_MONARCH_LEAVE:
1120 case DIE_INIT_MONARCH_LEAVE:
Dean Nelson33ba3c72008-07-29 22:34:07 -07001121 xpc_online_heartbeat();
Dean Nelson780d09e2005-11-09 14:41:57 -06001122 break;
1123 }
1124
1125 return NOTIFY_DONE;
1126}
1127
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001128int __init
1129xpc_init(void)
1130{
1131 int ret;
Dean Nelson64d032b2008-05-12 14:02:03 -07001132 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001133 struct xpc_partition *part;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001134 struct task_struct *kthread;
Dean Nelson7682a4c2006-08-08 15:03:29 -05001135 size_t buf_size;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001136
Dean Nelson94bd2702008-07-29 22:34:05 -07001137 if (is_shub()) {
1138 /*
1139 * The ia64-sn2 architecture supports at most 64 partitions.
1140 * And the inability to unregister remote AMOs restricts us
1141 * further to only support exactly 64 partitions on this
1142 * architecture, no less.
1143 */
1144 if (xp_max_npartitions != 64)
1145 return -EINVAL;
1146
1147 xpc_init_sn2();
1148
1149 } else if (is_uv()) {
1150 xpc_init_uv();
1151
1152 } else {
Dean Nelson408865c2005-09-08 10:46:58 -05001153 return -ENODEV;
Dean Nelson94bd2702008-07-29 22:34:05 -07001154 }
Dean Nelson408865c2005-09-08 10:46:58 -05001155
Dean Nelsonbc63d382008-07-29 22:34:04 -07001156 snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1157 snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
1158
Dean Nelson7682a4c2006-08-08 15:03:29 -05001159 buf_size = max(XPC_RP_VARS_SIZE,
Dean Nelson35190502008-04-22 14:48:55 -05001160 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES);
Dean Nelson7682a4c2006-08-08 15:03:29 -05001161 xpc_remote_copy_buffer = xpc_kmalloc_cacheline_aligned(buf_size,
Dean Nelson35190502008-04-22 14:48:55 -05001162 GFP_KERNEL,
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001163 &xpc_remote_copy_buffer_base);
Dean Nelsonbc63d382008-07-29 22:34:04 -07001164 if (xpc_remote_copy_buffer == NULL) {
1165 dev_err(xpc_part, "can't get memory for remote copy buffer\n");
Dean Nelson7682a4c2006-08-08 15:03:29 -05001166 return -ENOMEM;
Dean Nelsonbc63d382008-07-29 22:34:04 -07001167 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001168
Dean Nelsonbc63d382008-07-29 22:34:04 -07001169 xpc_partitions = kzalloc(sizeof(struct xpc_partition) *
1170 xp_max_npartitions, GFP_KERNEL);
1171 if (xpc_partitions == NULL) {
1172 dev_err(xpc_part, "can't get memory for partition structure\n");
1173 ret = -ENOMEM;
1174 goto out_1;
1175 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001176
1177 /*
1178 * The first few fields of each entry of xpc_partitions[] need to
1179 * be initialized now so that calls to xpc_connect() and
1180 * xpc_disconnect() can be made prior to the activation of any remote
1181 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
1182 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
1183 * PARTITION HAS BEEN ACTIVATED.
1184 */
Dean Nelsonbc63d382008-07-29 22:34:04 -07001185 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001186 part = &xpc_partitions[partid];
1187
Dean Nelson35190502008-04-22 14:48:55 -05001188 DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001189
1190 part->act_IRQ_rcvd = 0;
1191 spin_lock_init(&part->act_lock);
1192 part->act_state = XPC_P_INACTIVE;
1193 XPC_SET_REASON(part, 0, 0);
Dean Nelsona607c382005-09-01 14:01:37 -05001194
1195 init_timer(&part->disengage_request_timer);
1196 part->disengage_request_timer.function =
Dean Nelson35190502008-04-22 14:48:55 -05001197 xpc_timeout_partition_disengage_request;
1198 part->disengage_request_timer.data = (unsigned long)part;
Dean Nelsona607c382005-09-01 14:01:37 -05001199
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001200 part->setup_state = XPC_P_UNSET;
1201 init_waitqueue_head(&part->teardown_wq);
1202 atomic_set(&part->references, 0);
1203 }
1204
Dean Nelsonbc63d382008-07-29 22:34:04 -07001205 xpc_sysctl = register_sysctl_table(xpc_sys_dir);
1206
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001207 /*
1208 * Open up protections for IPI operations (and AMO operations on
1209 * Shub 1.1 systems).
1210 */
1211 xpc_allow_IPI_ops();
1212
1213 /*
1214 * Interrupts being processed will increment this atomic variable and
1215 * awaken the heartbeat thread which will process the interrupts.
1216 */
1217 atomic_set(&xpc_act_IRQ_rcvd, 0);
1218
1219 /*
1220 * This is safe to do before the xpc_hb_checker thread has started
1221 * because the handler releases a wait queue. If an interrupt is
1222 * received before the thread is waiting, it will not go to sleep,
1223 * but rather immediately process the interrupt.
1224 */
1225 ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
Dean Nelson35190502008-04-22 14:48:55 -05001226 "xpc hb", NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001227 if (ret != 0) {
1228 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
1229 "errno=%d\n", -ret);
Dean Nelsonbc63d382008-07-29 22:34:04 -07001230 ret = -EBUSY;
1231 goto out_2;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001232 }
1233
1234 /*
1235 * Fill the partition reserved page with the information needed by
1236 * other partitions to discover we are alive and establish initial
1237 * communications.
1238 */
Dean Nelson94bd2702008-07-29 22:34:05 -07001239 xpc_rsvd_page = xpc_setup_rsvd_page();
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001240 if (xpc_rsvd_page == NULL) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001241 dev_err(xpc_part, "can't setup our reserved page\n");
1242 ret = -EBUSY;
1243 goto out_3;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001244 }
1245
Dean Nelsona607c382005-09-01 14:01:37 -05001246 /* add ourselves to the reboot_notifier_list */
1247 ret = register_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001248 if (ret != 0)
Dean Nelsona607c382005-09-01 14:01:37 -05001249 dev_warn(xpc_part, "can't register reboot notifier\n");
Dean Nelsona607c382005-09-01 14:01:37 -05001250
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07001251 /* add ourselves to the die_notifier list */
Dean Nelson780d09e2005-11-09 14:41:57 -06001252 ret = register_die_notifier(&xpc_die_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001253 if (ret != 0)
Dean Nelson780d09e2005-11-09 14:41:57 -06001254 dev_warn(xpc_part, "can't register die notifier\n");
Dean Nelson780d09e2005-11-09 14:41:57 -06001255
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001256 /*
1257 * The real work-horse behind xpc. This processes incoming
1258 * interrupts and monitors remote heartbeats.
1259 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001260 kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
1261 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001262 dev_err(xpc_part, "failed while forking hb check thread\n");
Dean Nelsonbc63d382008-07-29 22:34:04 -07001263 ret = -EBUSY;
1264 goto out_4;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001265 }
1266
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001267 /*
1268 * Startup a thread that will attempt to discover other partitions to
1269 * activate based on info provided by SAL. This new thread is short
1270 * lived and will exit once discovery is complete.
1271 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001272 kthread = kthread_run(xpc_initiate_discovery, NULL,
1273 XPC_DISCOVERY_THREAD_NAME);
1274 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001275 dev_err(xpc_part, "failed while forking discovery thread\n");
1276
1277 /* mark this new thread as a non-starter */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001278 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001279
Dean Nelson65c17b82008-05-12 14:02:02 -07001280 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001281 return -EBUSY;
1282 }
1283
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001284 /* set the interface to point at XPC's functions */
1285 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001286 xpc_initiate_send, xpc_initiate_send_notify,
1287 xpc_initiate_received, xpc_initiate_partid_to_nasids);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001288
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 Nelsonaaa3cd62008-07-29 22:34:07 -07001294 xpc_rsvd_page->stamp = 0;
Dean Nelson94bd2702008-07-29 22:34:05 -07001295
Dean Nelsonbc63d382008-07-29 22:34:04 -07001296 (void)unregister_die_notifier(&xpc_die_notifier);
1297 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
1298out_3:
1299 free_irq(SGI_XPC_ACTIVATE, NULL);
1300out_2:
1301 xpc_restrict_IPI_ops();
1302 if (xpc_sysctl)
1303 unregister_sysctl_table(xpc_sysctl);
1304 kfree(xpc_partitions);
1305out_1:
1306 kfree(xpc_remote_copy_buffer_base);
1307 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001308}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001309
Dean Nelson35190502008-04-22 14:48:55 -05001310module_init(xpc_init);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001311
1312void __exit
1313xpc_exit(void)
1314{
Dean Nelson65c17b82008-05-12 14:02:02 -07001315 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001316}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001317
Dean Nelson35190502008-04-22 14:48:55 -05001318module_exit(xpc_exit);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001319
1320MODULE_AUTHOR("Silicon Graphics, Inc.");
1321MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1322MODULE_LICENSE("GPL");
1323
1324module_param(xpc_hb_interval, int, 0);
1325MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001326 "heartbeat increments.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001327
1328module_param(xpc_hb_check_interval, int, 0);
1329MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001330 "heartbeat checks.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001331
Dean Nelsone54af722005-10-25 14:07:43 -05001332module_param(xpc_disengage_request_timelimit, int, 0);
1333MODULE_PARM_DESC(xpc_disengage_request_timelimit, "Number of seconds to wait "
Dean Nelson35190502008-04-22 14:48:55 -05001334 "for disengage request to complete.");
Dean Nelsone54af722005-10-25 14:07:43 -05001335
Dean Nelson1f4674b2006-01-10 11:08:00 -06001336module_param(xpc_kdebug_ignore, int, 0);
1337MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
Dean Nelson35190502008-04-22 14:48:55 -05001338 "other partitions when dropping into kdebug.");