blob: 13ec47928994c3f8cbefcce00bccc2fc8fabb85f [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 *
Dean Nelson7fb5e592008-07-29 22:34:10 -070028 * . Currently on sn2, we have no way to determine which nasid an IRQ
Dean Nelsonc39838c2008-07-29 22:34:11 -070029 * came from. Thus, xpc_send_IRQ_sn2() does a remote amo write
30 * followed by an IPI. The amo indicates where data is to be pulled
31 * from, so after the IPI arrives, the remote partition checks the amo
32 * word. The IPI can actually arrive before the amo however, so other
33 * code must periodically check for this case. Also, remote amo
Dean Nelson7fb5e592008-07-29 22:34:10 -070034 * operations do not reliably time out. Thus we do a remote PIO read
35 * solely to know whether the remote partition is down and whether we
36 * should stop sending IPIs to it. This remote PIO read operation is
37 * set up in a special nofault region so SAL knows to ignore (and
Dean Nelsonc39838c2008-07-29 22:34:11 -070038 * cleanup) any errors due to the remote amo write, PIO read, and/or
Dean Nelson7fb5e592008-07-29 22:34:10 -070039 * PIO write operations.
Dean Nelson89eb8eb2005-03-23 19:50:00 -070040 *
41 * If/when new hardware solves this IPI problem, we should abandon
42 * the current approach.
43 *
44 */
45
Dean Nelson89eb8eb2005-03-23 19:50:00 -070046#include <linux/module.h>
Dean Nelson261f3b42008-07-29 22:34:16 -070047#include <linux/sysctl.h>
48#include <linux/device.h>
Nishanth Aravamudan69913922005-07-08 17:10:00 -070049#include <linux/delay.h>
Dean Nelsona607c382005-09-01 14:01:37 -050050#include <linux/reboot.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070051#include <linux/kdebug.h>
Dean Nelson2c2b94f2008-04-22 14:50:17 -050052#include <linux/kthread.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050053#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070054
Dean Nelson89eb8eb2005-03-23 19:50:00 -070055/* define two XPC debug device structures to be used with dev_dbg() et al */
56
57struct device_driver xpc_dbg_name = {
58 .name = "xpc"
59};
60
61struct device xpc_part_dbg_subname = {
62 .bus_id = {0}, /* set to "part" at xpc_init() time */
63 .driver = &xpc_dbg_name
64};
65
66struct device xpc_chan_dbg_subname = {
67 .bus_id = {0}, /* set to "chan" at xpc_init() time */
68 .driver = &xpc_dbg_name
69};
70
71struct device *xpc_part = &xpc_part_dbg_subname;
72struct device *xpc_chan = &xpc_chan_dbg_subname;
73
Dean Nelson1f4674b2006-01-10 11:08:00 -060074static int xpc_kdebug_ignore;
75
Dean Nelson89eb8eb2005-03-23 19:50:00 -070076/* systune related variables for /proc/sys directories */
77
Dean Nelsona607c382005-09-01 14:01:37 -050078static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
79static int xpc_hb_min_interval = 1;
80static int xpc_hb_max_interval = 10;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070081
Dean Nelsona607c382005-09-01 14:01:37 -050082static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
83static int xpc_hb_check_min_interval = 10;
84static int xpc_hb_check_max_interval = 120;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070085
Dean Nelsona47d5da2008-07-29 22:34:09 -070086int xpc_disengage_timelimit = XPC_DISENGAGE_DEFAULT_TIMELIMIT;
87static int xpc_disengage_min_timelimit; /* = 0 */
88static int xpc_disengage_max_timelimit = 120;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070089
90static ctl_table xpc_sys_xpc_hb_dir[] = {
91 {
Dean Nelson35190502008-04-22 14:48:55 -050092 .ctl_name = CTL_UNNUMBERED,
93 .procname = "hb_interval",
94 .data = &xpc_hb_interval,
95 .maxlen = sizeof(int),
96 .mode = 0644,
97 .proc_handler = &proc_dointvec_minmax,
98 .strategy = &sysctl_intvec,
99 .extra1 = &xpc_hb_min_interval,
100 .extra2 = &xpc_hb_max_interval},
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700101 {
Dean Nelson35190502008-04-22 14:48:55 -0500102 .ctl_name = CTL_UNNUMBERED,
103 .procname = "hb_check_interval",
104 .data = &xpc_hb_check_interval,
105 .maxlen = sizeof(int),
106 .mode = 0644,
107 .proc_handler = &proc_dointvec_minmax,
108 .strategy = &sysctl_intvec,
109 .extra1 = &xpc_hb_check_min_interval,
110 .extra2 = &xpc_hb_check_max_interval},
Eric W. Biederman68cbf072007-02-14 00:33:41 -0800111 {}
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700112};
113static ctl_table xpc_sys_xpc_dir[] = {
114 {
Dean Nelson35190502008-04-22 14:48:55 -0500115 .ctl_name = CTL_UNNUMBERED,
116 .procname = "hb",
117 .mode = 0555,
118 .child = xpc_sys_xpc_hb_dir},
Dean Nelsone54af722005-10-25 14:07:43 -0500119 {
Dean Nelson35190502008-04-22 14:48:55 -0500120 .ctl_name = CTL_UNNUMBERED,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700121 .procname = "disengage_timelimit",
122 .data = &xpc_disengage_timelimit,
Dean Nelson35190502008-04-22 14:48:55 -0500123 .maxlen = sizeof(int),
124 .mode = 0644,
125 .proc_handler = &proc_dointvec_minmax,
126 .strategy = &sysctl_intvec,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700127 .extra1 = &xpc_disengage_min_timelimit,
128 .extra2 = &xpc_disengage_max_timelimit},
Eric W. Biederman68cbf072007-02-14 00:33:41 -0800129 {}
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700130};
131static ctl_table xpc_sys_dir[] = {
132 {
Dean Nelson35190502008-04-22 14:48:55 -0500133 .ctl_name = CTL_UNNUMBERED,
134 .procname = "xpc",
135 .mode = 0555,
136 .child = xpc_sys_xpc_dir},
Eric W. Biederman68cbf072007-02-14 00:33:41 -0800137 {}
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700138};
139static struct ctl_table_header *xpc_sysctl;
140
Dean Nelsona47d5da2008-07-29 22:34:09 -0700141/* non-zero if any remote partition disengage was timed out */
142int xpc_disengage_timedout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700143
Dean Nelson5b8669d2008-07-29 22:34:18 -0700144/* #of activate IRQs received and not yet processed */
145int xpc_activate_IRQ_rcvd;
146DEFINE_SPINLOCK(xpc_activate_IRQ_rcvd_lock);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700147
148/* IRQ handler notifies this wait queue on receipt of an IRQ */
Dean Nelson6e410172008-07-29 22:34:09 -0700149DECLARE_WAIT_QUEUE_HEAD(xpc_activate_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700150
151static unsigned long xpc_hb_check_timeout;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700152static struct timer_list xpc_hb_timer;
153void *xpc_heartbeating_to_mask;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700154
Dean Nelsone54af722005-10-25 14:07:43 -0500155/* notification that the xpc_hb_checker thread has exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500156static DECLARE_COMPLETION(xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700157
Dean Nelsone54af722005-10-25 14:07:43 -0500158/* notification that the xpc_discovery thread has exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500159static DECLARE_COMPLETION(xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700160
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700161static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
162
Dean Nelsona607c382005-09-01 14:01:37 -0500163static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
164static struct notifier_block xpc_reboot_notifier = {
165 .notifier_call = xpc_system_reboot,
166};
167
Dean Nelson780d09e2005-11-09 14:41:57 -0600168static int xpc_system_die(struct notifier_block *, unsigned long, void *);
169static struct notifier_block xpc_die_notifier = {
170 .notifier_call = xpc_system_die,
171};
172
Dean Nelson5b8669d2008-07-29 22:34:18 -0700173int (*xpc_setup_partitions_sn) (void);
Dean Nelsona812dcc2008-07-29 22:34:16 -0700174enum xp_retval (*xpc_get_partition_rsvd_page_pa) (void *buf, u64 *cookie,
175 unsigned long *rp_pa,
176 size_t *len);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700177int (*xpc_setup_rsvd_page_sn) (struct xpc_rsvd_page *rp);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700178void (*xpc_heartbeat_init) (void);
179void (*xpc_heartbeat_exit) (void);
180void (*xpc_increment_heartbeat) (void);
181void (*xpc_offline_heartbeat) (void);
182void (*xpc_online_heartbeat) (void);
Dean Nelson61deb862008-07-29 22:34:17 -0700183enum xp_retval (*xpc_get_remote_heartbeat) (struct xpc_partition *part);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700184
Dean Nelsone17d4162008-07-29 22:34:06 -0700185enum xp_retval (*xpc_make_first_contact) (struct xpc_partition *part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700186void (*xpc_notify_senders_of_disconnect) (struct xpc_channel *ch);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700187u64 (*xpc_get_chctl_all_flags) (struct xpc_partition *part);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700188enum xp_retval (*xpc_setup_msg_structures) (struct xpc_channel *ch);
189void (*xpc_teardown_msg_structures) (struct xpc_channel *ch);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700190void (*xpc_process_msg_chctl_flags) (struct xpc_partition *part, int ch_number);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700191int (*xpc_n_of_deliverable_msgs) (struct xpc_channel *ch);
Dean Nelsone17d4162008-07-29 22:34:06 -0700192struct xpc_msg *(*xpc_get_deliverable_msg) (struct xpc_channel *ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700193
Dean Nelsona47d5da2008-07-29 22:34:09 -0700194void (*xpc_request_partition_activation) (struct xpc_rsvd_page *remote_rp,
Dean Nelsona812dcc2008-07-29 22:34:16 -0700195 unsigned long remote_rp_pa,
196 int nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700197void (*xpc_request_partition_reactivation) (struct xpc_partition *part);
198void (*xpc_request_partition_deactivation) (struct xpc_partition *part);
199void (*xpc_cancel_partition_deactivation_request) (struct xpc_partition *part);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700200
Dean Nelson5b8669d2008-07-29 22:34:18 -0700201void (*xpc_process_activate_IRQ_rcvd) (void);
202enum xp_retval (*xpc_setup_ch_structures_sn) (struct xpc_partition *part);
203void (*xpc_teardown_ch_structures_sn) (struct xpc_partition *part);
Dean Nelsone17d4162008-07-29 22:34:06 -0700204
Dean Nelsona47d5da2008-07-29 22:34:09 -0700205void (*xpc_indicate_partition_engaged) (struct xpc_partition *part);
206int (*xpc_partition_engaged) (short partid);
207int (*xpc_any_partition_engaged) (void);
208void (*xpc_indicate_partition_disengaged) (struct xpc_partition *part);
209void (*xpc_assume_partition_disengaged) (short partid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700210
Dean Nelson7fb5e592008-07-29 22:34:10 -0700211void (*xpc_send_chctl_closerequest) (struct xpc_channel *ch,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700212 unsigned long *irq_flags);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700213void (*xpc_send_chctl_closereply) (struct xpc_channel *ch,
214 unsigned long *irq_flags);
215void (*xpc_send_chctl_openrequest) (struct xpc_channel *ch,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700216 unsigned long *irq_flags);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700217void (*xpc_send_chctl_openreply) (struct xpc_channel *ch,
218 unsigned long *irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700219
Dean Nelson5b8669d2008-07-29 22:34:18 -0700220void (*xpc_save_remote_msgqueue_pa) (struct xpc_channel *ch,
221 unsigned long msgqueue_pa);
222
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700223enum xp_retval (*xpc_send_msg) (struct xpc_channel *ch, u32 flags,
224 void *payload, u16 payload_size, u8 notify_type,
225 xpc_notify_func func, void *key);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700226void (*xpc_received_msg) (struct xpc_channel *ch, struct xpc_msg *msg);
Dean Nelson94bd2702008-07-29 22:34:05 -0700227
Dean Nelsona607c382005-09-01 14:01:37 -0500228/*
Dean Nelsona47d5da2008-07-29 22:34:09 -0700229 * Timer function to enforce the timelimit on the partition disengage.
Dean Nelsona607c382005-09-01 14:01:37 -0500230 */
231static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700232xpc_timeout_partition_disengage(unsigned long data)
Dean Nelsona607c382005-09-01 14:01:37 -0500233{
Dean Nelson35190502008-04-22 14:48:55 -0500234 struct xpc_partition *part = (struct xpc_partition *)data;
Dean Nelsona607c382005-09-01 14:01:37 -0500235
Dean Nelsona47d5da2008-07-29 22:34:09 -0700236 DBUG_ON(time_is_after_jiffies(part->disengage_timeout));
Dean Nelsona607c382005-09-01 14:01:37 -0500237
Dean Nelson35190502008-04-22 14:48:55 -0500238 (void)xpc_partition_disengaged(part);
Dean Nelsona607c382005-09-01 14:01:37 -0500239
Dean Nelsona47d5da2008-07-29 22:34:09 -0700240 DBUG_ON(part->disengage_timeout != 0);
241 DBUG_ON(xpc_partition_engaged(XPC_PARTID(part)));
Dean Nelsona607c382005-09-01 14:01:37 -0500242}
243
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700244/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700245 * Timer to produce the heartbeat. The timer structures function is
246 * already set when this is initially called. A tunable is used to
247 * specify when the next timeout should occur.
248 */
249static void
250xpc_hb_beater(unsigned long dummy)
251{
Dean Nelson33ba3c72008-07-29 22:34:07 -0700252 xpc_increment_heartbeat();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700253
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700254 if (time_is_before_eq_jiffies(xpc_hb_check_timeout))
Dean Nelson6e410172008-07-29 22:34:09 -0700255 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700256
257 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
258 add_timer(&xpc_hb_timer);
259}
260
Dean Nelson33ba3c72008-07-29 22:34:07 -0700261static void
262xpc_start_hb_beater(void)
263{
264 xpc_heartbeat_init();
265 init_timer(&xpc_hb_timer);
266 xpc_hb_timer.function = xpc_hb_beater;
267 xpc_hb_beater(0);
268}
269
270static void
271xpc_stop_hb_beater(void)
272{
273 del_timer_sync(&xpc_hb_timer);
274 xpc_heartbeat_exit();
275}
276
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700277/*
Dean Nelson61deb862008-07-29 22:34:17 -0700278 * At periodic intervals, scan through all active partitions and ensure
279 * their heartbeat is still active. If not, the partition is deactivated.
280 */
281static void
282xpc_check_remote_hb(void)
283{
284 struct xpc_partition *part;
285 short partid;
286 enum xp_retval ret;
287
288 for (partid = 0; partid < xp_max_npartitions; partid++) {
289
290 if (xpc_exiting)
291 break;
292
293 if (partid == xp_partition_id)
294 continue;
295
296 part = &xpc_partitions[partid];
297
Dean Nelson83469b52008-07-29 22:34:18 -0700298 if (part->act_state == XPC_P_AS_INACTIVE ||
299 part->act_state == XPC_P_AS_DEACTIVATING) {
Dean Nelson61deb862008-07-29 22:34:17 -0700300 continue;
301 }
302
303 ret = xpc_get_remote_heartbeat(part);
304 if (ret != xpSuccess)
305 XPC_DEACTIVATE_PARTITION(part, ret);
306 }
307}
308
309/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700310 * This thread is responsible for nearly all of the partition
311 * activation/deactivation.
312 */
313static int
314xpc_hb_checker(void *ignore)
315{
Dean Nelson35190502008-04-22 14:48:55 -0500316 int force_IRQ = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700317
318 /* this thread was marked active by xpc_hb_init() */
319
Mike Travis0bc3cc02008-07-24 18:21:31 -0700320 set_cpus_allowed_ptr(current, &cpumask_of_cpu(XPC_HB_CHECK_CPU));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700321
Dean Nelson4c013f52007-11-07 07:53:06 -0600322 /* set our heartbeating to other partitions into motion */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700323 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700324 xpc_start_hb_beater();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700325
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500326 while (!xpc_exiting) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700327
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700328 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
329 "been received\n",
Dean Nelson35190502008-04-22 14:48:55 -0500330 (int)(xpc_hb_check_timeout - jiffies),
Dean Nelson5b8669d2008-07-29 22:34:18 -0700331 xpc_activate_IRQ_rcvd);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700332
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700333 /* checking of remote heartbeats is skewed by IRQ handling */
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700334 if (time_is_before_eq_jiffies(xpc_hb_check_timeout)) {
Dean Nelson5b8669d2008-07-29 22:34:18 -0700335 xpc_hb_check_timeout = jiffies +
336 (xpc_hb_check_interval * HZ);
337
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700338 dev_dbg(xpc_part, "checking remote heartbeats\n");
339 xpc_check_remote_hb();
340
341 /*
Dean Nelson5b8669d2008-07-29 22:34:18 -0700342 * On sn2 we need to periodically recheck to ensure no
343 * IRQ/amo pairs have been missed.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700344 */
Dean Nelson5b8669d2008-07-29 22:34:18 -0700345 if (is_shub())
346 force_IRQ = 1;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700347 }
348
Dean Nelsona607c382005-09-01 14:01:37 -0500349 /* check for outstanding IRQs */
Dean Nelson5b8669d2008-07-29 22:34:18 -0700350 if (xpc_activate_IRQ_rcvd > 0 || force_IRQ != 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700351 force_IRQ = 0;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700352 dev_dbg(xpc_part, "processing activate IRQs "
353 "received\n");
354 xpc_process_activate_IRQ_rcvd();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700355 }
Dean Nelsona607c382005-09-01 14:01:37 -0500356
357 /* wait for IRQ or timeout */
Dean Nelson6e410172008-07-29 22:34:09 -0700358 (void)wait_event_interruptible(xpc_activate_IRQ_wq,
Dean Nelson5b8669d2008-07-29 22:34:18 -0700359 (time_is_before_eq_jiffies(
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700360 xpc_hb_check_timeout) ||
Dean Nelson5b8669d2008-07-29 22:34:18 -0700361 xpc_activate_IRQ_rcvd > 0 ||
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500362 xpc_exiting));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700363 }
364
Dean Nelson33ba3c72008-07-29 22:34:07 -0700365 xpc_stop_hb_beater();
366
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700367 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
368
Dean Nelsone54af722005-10-25 14:07:43 -0500369 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500370 complete(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700371 return 0;
372}
373
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700374/*
375 * This thread will attempt to discover other partitions to activate
376 * based on info provided by SAL. This new thread is short lived and
377 * will exit once discovery is complete.
378 */
379static int
380xpc_initiate_discovery(void *ignore)
381{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700382 xpc_discovery();
383
384 dev_dbg(xpc_part, "discovery thread is exiting\n");
385
Dean Nelsone54af722005-10-25 14:07:43 -0500386 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500387 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700388 return 0;
389}
390
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700391/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700392 * The first kthread assigned to a newly activated partition is the one
Dean Nelsone17d4162008-07-29 22:34:06 -0700393 * created by XPC HB with which it calls xpc_activating(). XPC hangs on to
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700394 * that kthread until the partition is brought down, at which time that kthread
395 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
396 * that XPC has dismantled all communication infrastructure for the associated
397 * partition.) This kthread becomes the channel manager for that partition.
398 *
399 * Each active partition has a channel manager, who, besides connecting and
400 * disconnecting channels, will ensure that each of the partition's connected
401 * channels has the required number of assigned kthreads to get the work done.
402 */
403static void
404xpc_channel_mgr(struct xpc_partition *part)
405{
Dean Nelson83469b52008-07-29 22:34:18 -0700406 while (part->act_state != XPC_P_AS_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500407 atomic_read(&part->nchannels_active) > 0 ||
408 !xpc_partition_disengaged(part)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700409
Dean Nelson7fb5e592008-07-29 22:34:10 -0700410 xpc_process_sent_chctl_flags(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700411
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700412 /*
413 * Wait until we've been requested to activate kthreads or
414 * all of the channel's message queues have been torn down or
415 * a signal is pending.
416 *
417 * The channel_mgr_requests is set to 1 after being awakened,
418 * This is done to prevent the channel mgr from making one pass
419 * through the loop for each request, since he will
420 * be servicing all the requests in one pass. The reason it's
421 * set to 1 instead of 0 is so that other kthreads will know
422 * that the channel mgr is running and won't bother trying to
423 * wake him up.
424 */
425 atomic_dec(&part->channel_mgr_requests);
Dean Nelson35190502008-04-22 14:48:55 -0500426 (void)wait_event_interruptible(part->channel_mgr_wq,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500427 (atomic_read(&part->channel_mgr_requests) > 0 ||
Dean Nelson7fb5e592008-07-29 22:34:10 -0700428 part->chctl.all_flags != 0 ||
Dean Nelson83469b52008-07-29 22:34:18 -0700429 (part->act_state == XPC_P_AS_DEACTIVATING &&
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500430 atomic_read(&part->nchannels_active) == 0 &&
431 xpc_partition_disengaged(part))));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700432 atomic_set(&part->channel_mgr_requests, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700433 }
434}
435
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700436/*
Dean Nelson5b8669d2008-07-29 22:34:18 -0700437 * Guarantee that the kzalloc'd memory is cacheline aligned.
438 */
439void *
440xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
441{
442 /* see if kzalloc will give us cachline aligned memory by default */
443 *base = kzalloc(size, flags);
444 if (*base == NULL)
445 return NULL;
446
447 if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
448 return *base;
449
450 kfree(*base);
451
452 /* nope, we'll have to do it ourselves */
453 *base = kzalloc(size + L1_CACHE_BYTES, flags);
454 if (*base == NULL)
455 return NULL;
456
457 return (void *)L1_CACHE_ALIGN((u64)*base);
458}
459
460/*
461 * Setup the channel structures necessary to support XPartition Communication
462 * between the specified remote partition and the local one.
463 */
464static enum xp_retval
465xpc_setup_ch_structures(struct xpc_partition *part)
466{
467 enum xp_retval ret;
468 int ch_number;
469 struct xpc_channel *ch;
470 short partid = XPC_PARTID(part);
471
472 /*
473 * Allocate all of the channel structures as a contiguous chunk of
474 * memory.
475 */
476 DBUG_ON(part->channels != NULL);
477 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
478 GFP_KERNEL);
479 if (part->channels == NULL) {
480 dev_err(xpc_chan, "can't get memory for channels\n");
481 return xpNoMemory;
482 }
483
484 /* allocate the remote open and close args */
485
486 part->remote_openclose_args =
487 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE,
488 GFP_KERNEL, &part->
489 remote_openclose_args_base);
490 if (part->remote_openclose_args == NULL) {
491 dev_err(xpc_chan, "can't get memory for remote connect args\n");
492 ret = xpNoMemory;
493 goto out_1;
494 }
495
496 part->chctl.all_flags = 0;
497 spin_lock_init(&part->chctl_lock);
498
499 atomic_set(&part->channel_mgr_requests, 1);
500 init_waitqueue_head(&part->channel_mgr_wq);
501
502 part->nchannels = XPC_MAX_NCHANNELS;
503
504 atomic_set(&part->nchannels_active, 0);
505 atomic_set(&part->nchannels_engaged, 0);
506
507 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
508 ch = &part->channels[ch_number];
509
510 ch->partid = partid;
511 ch->number = ch_number;
512 ch->flags = XPC_C_DISCONNECTED;
513
514 atomic_set(&ch->kthreads_assigned, 0);
515 atomic_set(&ch->kthreads_idle, 0);
516 atomic_set(&ch->kthreads_active, 0);
517
518 atomic_set(&ch->references, 0);
519 atomic_set(&ch->n_to_notify, 0);
520
521 spin_lock_init(&ch->lock);
522 init_completion(&ch->wdisconnect_wait);
523
524 atomic_set(&ch->n_on_msg_allocate_wq, 0);
525 init_waitqueue_head(&ch->msg_allocate_wq);
526 init_waitqueue_head(&ch->idle_wq);
527 }
528
529 ret = xpc_setup_ch_structures_sn(part);
530 if (ret != xpSuccess)
531 goto out_2;
532
533 /*
534 * With the setting of the partition setup_state to XPC_P_SS_SETUP,
535 * we're declaring that this partition is ready to go.
536 */
537 part->setup_state = XPC_P_SS_SETUP;
538
539 return xpSuccess;
540
541 /* setup of ch structures failed */
542out_2:
543 kfree(part->remote_openclose_args_base);
544 part->remote_openclose_args = NULL;
545out_1:
546 kfree(part->channels);
547 part->channels = NULL;
548 return ret;
549}
550
551/*
552 * Teardown the channel structures necessary to support XPartition Communication
553 * between the specified remote partition and the local one.
554 */
555static void
556xpc_teardown_ch_structures(struct xpc_partition *part)
557{
558 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
559 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
560
561 /*
562 * Make this partition inaccessible to local processes by marking it
563 * as no longer setup. Then wait before proceeding with the teardown
564 * until all existing references cease.
565 */
566 DBUG_ON(part->setup_state != XPC_P_SS_SETUP);
567 part->setup_state = XPC_P_SS_WTEARDOWN;
568
569 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
570
571 /* now we can begin tearing down the infrastructure */
572
573 xpc_teardown_ch_structures_sn(part);
574
575 kfree(part->remote_openclose_args_base);
576 part->remote_openclose_args = NULL;
577 kfree(part->channels);
578 part->channels = NULL;
579
580 part->setup_state = XPC_P_SS_TORNDOWN;
581}
582
583/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700584 * When XPC HB determines that a partition has come up, it will create a new
585 * kthread and that kthread will call this function to attempt to set up the
586 * basic infrastructure used for Cross Partition Communication with the newly
587 * upped partition.
588 *
589 * The kthread that was created by XPC HB and which setup the XPC
Dean Nelsone17d4162008-07-29 22:34:06 -0700590 * infrastructure will remain assigned to the partition becoming the channel
591 * manager for that partition until the partition is deactivating, at which
592 * time the kthread will teardown the XPC infrastructure and then exit.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700593 */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700594static int
595xpc_activating(void *__partid)
596{
Dean Nelson64d032b2008-05-12 14:02:03 -0700597 short partid = (u64)__partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700598 struct xpc_partition *part = &xpc_partitions[partid];
599 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700600
Dean Nelsonbc63d382008-07-29 22:34:04 -0700601 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700602
603 spin_lock_irqsave(&part->act_lock, irq_flags);
604
Dean Nelson83469b52008-07-29 22:34:18 -0700605 if (part->act_state == XPC_P_AS_DEACTIVATING) {
606 part->act_state = XPC_P_AS_INACTIVE;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700607 spin_unlock_irqrestore(&part->act_lock, irq_flags);
608 part->remote_rp_pa = 0;
609 return 0;
610 }
611
612 /* indicate the thread is activating */
Dean Nelson83469b52008-07-29 22:34:18 -0700613 DBUG_ON(part->act_state != XPC_P_AS_ACTIVATION_REQ);
614 part->act_state = XPC_P_AS_ACTIVATING;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700615
616 XPC_SET_REASON(part, 0, 0);
617 spin_unlock_irqrestore(&part->act_lock, irq_flags);
618
Dean Nelsone17d4162008-07-29 22:34:06 -0700619 dev_dbg(xpc_part, "activating partition %d\n", partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700620
Dean Nelson33ba3c72008-07-29 22:34:07 -0700621 xpc_allow_hb(partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700622
Dean Nelson5b8669d2008-07-29 22:34:18 -0700623 if (xpc_setup_ch_structures(part) == xpSuccess) {
Dean Nelsone17d4162008-07-29 22:34:06 -0700624 (void)xpc_part_ref(part); /* this will always succeed */
625
626 if (xpc_make_first_contact(part) == xpSuccess) {
627 xpc_mark_partition_active(part);
628 xpc_channel_mgr(part);
629 /* won't return until partition is deactivating */
630 }
631
632 xpc_part_deref(part);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700633 xpc_teardown_ch_structures(part);
Dean Nelsone17d4162008-07-29 22:34:06 -0700634 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700635
Dean Nelson33ba3c72008-07-29 22:34:07 -0700636 xpc_disallow_hb(partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700637 xpc_mark_partition_inactive(part);
638
Dean Nelson65c17b82008-05-12 14:02:02 -0700639 if (part->reason == xpReactivating) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700640 /* interrupting ourselves results in activating partition */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700641 xpc_request_partition_reactivation(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700642 }
643
644 return 0;
645}
646
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700647void
648xpc_activate_partition(struct xpc_partition *part)
649{
Dean Nelson64d032b2008-05-12 14:02:03 -0700650 short partid = XPC_PARTID(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700651 unsigned long irq_flags;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500652 struct task_struct *kthread;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700653
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700654 spin_lock_irqsave(&part->act_lock, irq_flags);
655
Dean Nelson83469b52008-07-29 22:34:18 -0700656 DBUG_ON(part->act_state != XPC_P_AS_INACTIVE);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700657
Dean Nelson83469b52008-07-29 22:34:18 -0700658 part->act_state = XPC_P_AS_ACTIVATION_REQ;
Dean Nelson65c17b82008-05-12 14:02:02 -0700659 XPC_SET_REASON(part, xpCloneKThread, __LINE__);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700660
661 spin_unlock_irqrestore(&part->act_lock, irq_flags);
Robin Holt7c6c6632006-02-02 12:30:21 -0600662
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500663 kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
664 partid);
665 if (IS_ERR(kthread)) {
Robin Holt7c6c6632006-02-02 12:30:21 -0600666 spin_lock_irqsave(&part->act_lock, irq_flags);
Dean Nelson83469b52008-07-29 22:34:18 -0700667 part->act_state = XPC_P_AS_INACTIVE;
Dean Nelson65c17b82008-05-12 14:02:02 -0700668 XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
Robin Holt7c6c6632006-02-02 12:30:21 -0600669 spin_unlock_irqrestore(&part->act_lock, irq_flags);
670 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700671}
672
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700673void
674xpc_activate_kthreads(struct xpc_channel *ch, int needed)
675{
676 int idle = atomic_read(&ch->kthreads_idle);
677 int assigned = atomic_read(&ch->kthreads_assigned);
678 int wakeup;
679
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700680 DBUG_ON(needed <= 0);
681
682 if (idle > 0) {
683 wakeup = (needed > idle) ? idle : needed;
684 needed -= wakeup;
685
686 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
687 "channel=%d\n", wakeup, ch->partid, ch->number);
688
689 /* only wakeup the requested number of kthreads */
690 wake_up_nr(&ch->idle_wq, wakeup);
691 }
692
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500693 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700694 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700695
696 if (needed + assigned > ch->kthreads_assigned_limit) {
697 needed = ch->kthreads_assigned_limit - assigned;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500698 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700699 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700700 }
701
702 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
703 needed, ch->partid, ch->number);
704
Dean Nelsona460ef82006-11-22 08:25:00 -0600705 xpc_create_kthreads(ch, needed, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700706}
707
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700708/*
709 * This function is where XPC's kthreads wait for messages to deliver.
710 */
711static void
712xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
713{
714 do {
715 /* deliver messages to their intended recipients */
716
Dean Nelsona47d5da2008-07-29 22:34:09 -0700717 while (xpc_n_of_deliverable_msgs(ch) > 0 &&
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500718 !(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700719 xpc_deliver_msg(ch);
720 }
721
722 if (atomic_inc_return(&ch->kthreads_idle) >
Dean Nelson35190502008-04-22 14:48:55 -0500723 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700724 /* too many idle kthreads on this channel */
725 atomic_dec(&ch->kthreads_idle);
726 break;
727 }
728
729 dev_dbg(xpc_chan, "idle kthread calling "
730 "wait_event_interruptible_exclusive()\n");
731
Dean Nelson35190502008-04-22 14:48:55 -0500732 (void)wait_event_interruptible_exclusive(ch->idle_wq,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700733 (xpc_n_of_deliverable_msgs(ch) > 0 ||
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500734 (ch->flags & XPC_C_DISCONNECTING)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700735
736 atomic_dec(&ch->kthreads_idle);
737
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500738 } while (!(ch->flags & XPC_C_DISCONNECTING));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700739}
740
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700741static int
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500742xpc_kthread_start(void *args)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700743{
Dean Nelson64d032b2008-05-12 14:02:03 -0700744 short partid = XPC_UNPACK_ARG1(args);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700745 u16 ch_number = XPC_UNPACK_ARG2(args);
746 struct xpc_partition *part = &xpc_partitions[partid];
747 struct xpc_channel *ch;
748 int n_needed;
Dean Nelsone54af722005-10-25 14:07:43 -0500749 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700750
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700751 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
752 partid, ch_number);
753
754 ch = &part->channels[ch_number];
755
756 if (!(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700757
758 /* let registerer know that connection has been established */
759
Dean Nelsone54af722005-10-25 14:07:43 -0500760 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson4c2cd962006-02-15 08:02:21 -0600761 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
762 ch->flags |= XPC_C_CONNECTEDCALLOUT;
Dean Nelsone54af722005-10-25 14:07:43 -0500763 spin_unlock_irqrestore(&ch->lock, irq_flags);
764
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700765 xpc_connected_callout(ch);
766
Dean Nelson4c2cd962006-02-15 08:02:21 -0600767 spin_lock_irqsave(&ch->lock, irq_flags);
768 ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
769 spin_unlock_irqrestore(&ch->lock, irq_flags);
770
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700771 /*
772 * It is possible that while the callout was being
773 * made that the remote partition sent some messages.
774 * If that is the case, we may need to activate
775 * additional kthreads to help deliver them. We only
776 * need one less than total #of messages to deliver.
777 */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700778 n_needed = xpc_n_of_deliverable_msgs(ch) - 1;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500779 if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700780 xpc_activate_kthreads(ch, n_needed);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500781
Dean Nelsone54af722005-10-25 14:07:43 -0500782 } else {
783 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700784 }
785
786 xpc_kthread_waitmsgs(part, ch);
787 }
788
Dean Nelsona460ef82006-11-22 08:25:00 -0600789 /* let registerer know that connection is disconnecting */
Dean Nelsone54af722005-10-25 14:07:43 -0500790
Dean Nelsona460ef82006-11-22 08:25:00 -0600791 spin_lock_irqsave(&ch->lock, irq_flags);
792 if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500793 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelsona460ef82006-11-22 08:25:00 -0600794 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
Dean Nelson4c2cd962006-02-15 08:02:21 -0600795 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -0600796
Dean Nelson65c17b82008-05-12 14:02:02 -0700797 xpc_disconnect_callout(ch, xpDisconnecting);
Dean Nelsona460ef82006-11-22 08:25:00 -0600798
799 spin_lock_irqsave(&ch->lock, irq_flags);
800 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
801 }
802 spin_unlock_irqrestore(&ch->lock, irq_flags);
803
Dean Nelsona47d5da2008-07-29 22:34:09 -0700804 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
805 atomic_dec_return(&part->nchannels_engaged) == 0) {
806 xpc_indicate_partition_disengaged(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700807 }
808
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700809 xpc_msgqueue_deref(ch);
810
811 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
812 partid, ch_number);
813
814 xpc_part_deref(part);
815 return 0;
816}
817
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700818/*
819 * For each partition that XPC has established communications with, there is
820 * a minimum of one kernel thread assigned to perform any operation that
821 * may potentially sleep or block (basically the callouts to the asynchronous
822 * functions registered via xpc_connect()).
823 *
824 * Additional kthreads are created and destroyed by XPC as the workload
825 * demands.
826 *
827 * A kthread is assigned to one of the active channels that exists for a given
828 * partition.
829 */
830void
Dean Nelsona460ef82006-11-22 08:25:00 -0600831xpc_create_kthreads(struct xpc_channel *ch, int needed,
Dean Nelson35190502008-04-22 14:48:55 -0500832 int ignore_disconnecting)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700833{
834 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700835 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
Dean Nelsona607c382005-09-01 14:01:37 -0500836 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500837 struct task_struct *kthread;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700838
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700839 while (needed-- > 0) {
Dean Nelsone54af722005-10-25 14:07:43 -0500840
841 /*
842 * The following is done on behalf of the newly created
843 * kthread. That kthread is responsible for doing the
844 * counterpart to the following before it exits.
845 */
Dean Nelsona460ef82006-11-22 08:25:00 -0600846 if (ignore_disconnecting) {
847 if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
848 /* kthreads assigned had gone to zero */
849 BUG_ON(!(ch->flags &
Dean Nelson35190502008-04-22 14:48:55 -0500850 XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelsona460ef82006-11-22 08:25:00 -0600851 break;
852 }
853
854 } else if (ch->flags & XPC_C_DISCONNECTING) {
855 break;
856
Dean Nelsona47d5da2008-07-29 22:34:09 -0700857 } else if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
858 atomic_inc_return(&part->nchannels_engaged) == 1) {
859 xpc_indicate_partition_engaged(part);
Dean Nelsona460ef82006-11-22 08:25:00 -0600860 }
Dean Nelson35190502008-04-22 14:48:55 -0500861 (void)xpc_part_ref(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500862 xpc_msgqueue_ref(ch);
Dean Nelsone54af722005-10-25 14:07:43 -0500863
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500864 kthread = kthread_run(xpc_kthread_start, (void *)args,
865 "xpc%02dc%d", ch->partid, ch->number);
866 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700867 /* the fork failed */
Dean Nelsona460ef82006-11-22 08:25:00 -0600868
869 /*
870 * NOTE: if (ignore_disconnecting &&
871 * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
872 * then we'll deadlock if all other kthreads assigned
873 * to this channel are blocked in the channel's
874 * registerer, because the only thing that will unblock
Dean Nelson65c17b82008-05-12 14:02:02 -0700875 * them is the xpDisconnecting callout that this
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500876 * failed kthread_run() would have made.
Dean Nelsona460ef82006-11-22 08:25:00 -0600877 */
878
Dean Nelsone54af722005-10-25 14:07:43 -0500879 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
880 atomic_dec_return(&part->nchannels_engaged) == 0) {
Dean Nelsona47d5da2008-07-29 22:34:09 -0700881 xpc_indicate_partition_disengaged(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500882 }
883 xpc_msgqueue_deref(ch);
884 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700885
886 if (atomic_read(&ch->kthreads_assigned) <
Dean Nelson35190502008-04-22 14:48:55 -0500887 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700888 /*
889 * Flag this as an error only if we have an
890 * insufficient #of kthreads for the channel
891 * to function.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700892 */
893 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700894 XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
Dean Nelson35190502008-04-22 14:48:55 -0500895 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700896 spin_unlock_irqrestore(&ch->lock, irq_flags);
897 }
898 break;
899 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700900 }
901}
902
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700903void
904xpc_disconnect_wait(int ch_number)
905{
Dean Nelsona607c382005-09-01 14:01:37 -0500906 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700907 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700908 struct xpc_partition *part;
909 struct xpc_channel *ch;
Dean Nelsone54af722005-10-25 14:07:43 -0500910 int wakeup_channel_mgr;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700911
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700912 /* now wait for all callouts to the caller's function to cease */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700913 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700914 part = &xpc_partitions[partid];
915
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500916 if (!xpc_part_ref(part))
Dean Nelsone54af722005-10-25 14:07:43 -0500917 continue;
Dean Nelsone54af722005-10-25 14:07:43 -0500918
919 ch = &part->channels[ch_number];
920
921 if (!(ch->flags & XPC_C_WDISCONNECT)) {
922 xpc_part_deref(part);
923 continue;
924 }
925
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500926 wait_for_completion(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500927
928 spin_lock_irqsave(&ch->lock, irq_flags);
929 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
930 wakeup_channel_mgr = 0;
931
Dean Nelson7fb5e592008-07-29 22:34:10 -0700932 if (ch->delayed_chctl_flags) {
Dean Nelson83469b52008-07-29 22:34:18 -0700933 if (part->act_state != XPC_P_AS_DEACTIVATING) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700934 spin_lock(&part->chctl_lock);
935 part->chctl.flags[ch->number] |=
936 ch->delayed_chctl_flags;
937 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500938 wakeup_channel_mgr = 1;
939 }
Dean Nelson7fb5e592008-07-29 22:34:10 -0700940 ch->delayed_chctl_flags = 0;
Dean Nelsone54af722005-10-25 14:07:43 -0500941 }
942
943 ch->flags &= ~XPC_C_WDISCONNECT;
944 spin_unlock_irqrestore(&ch->lock, irq_flags);
945
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500946 if (wakeup_channel_mgr)
Dean Nelsone54af722005-10-25 14:07:43 -0500947 xpc_wakeup_channel_mgr(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500948
949 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700950 }
951}
952
Dean Nelson5b8669d2008-07-29 22:34:18 -0700953static int
954xpc_setup_partitions(void)
955{
956 short partid;
957 struct xpc_partition *part;
958
959 xpc_partitions = kzalloc(sizeof(struct xpc_partition) *
960 xp_max_npartitions, GFP_KERNEL);
961 if (xpc_partitions == NULL) {
962 dev_err(xpc_part, "can't get memory for partition structure\n");
963 return -ENOMEM;
964 }
965
966 /*
967 * The first few fields of each entry of xpc_partitions[] need to
968 * be initialized now so that calls to xpc_connect() and
969 * xpc_disconnect() can be made prior to the activation of any remote
970 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
971 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
972 * PARTITION HAS BEEN ACTIVATED.
973 */
974 for (partid = 0; partid < xp_max_npartitions; partid++) {
975 part = &xpc_partitions[partid];
976
977 DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
978
979 part->activate_IRQ_rcvd = 0;
980 spin_lock_init(&part->act_lock);
981 part->act_state = XPC_P_AS_INACTIVE;
982 XPC_SET_REASON(part, 0, 0);
983
984 init_timer(&part->disengage_timer);
985 part->disengage_timer.function =
986 xpc_timeout_partition_disengage;
987 part->disengage_timer.data = (unsigned long)part;
988
989 part->setup_state = XPC_P_SS_UNSET;
990 init_waitqueue_head(&part->teardown_wq);
991 atomic_set(&part->references, 0);
992 }
993
994 return xpc_setup_partitions_sn();
995}
996
997static void
998xpc_teardown_partitions(void)
999{
1000 kfree(xpc_partitions);
1001}
1002
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001003static void
Dean Nelson65c17b82008-05-12 14:02:02 -07001004xpc_do_exit(enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001005{
Dean Nelson64d032b2008-05-12 14:02:03 -07001006 short partid;
Dean Nelson1ecaded2006-01-06 09:48:21 -06001007 int active_part_count, printed_waiting_msg = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001008 struct xpc_partition *part;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001009 unsigned long printmsg_time, disengage_timeout = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001010
Dean Nelsona607c382005-09-01 14:01:37 -05001011 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
1012 DBUG_ON(xpc_exiting == 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001013
1014 /*
Dean Nelsona607c382005-09-01 14:01:37 -05001015 * Let the heartbeat checker thread and the discovery thread
1016 * (if one is running) know that they should exit. Also wake up
1017 * the heartbeat checker thread in case it's sleeping.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001018 */
1019 xpc_exiting = 1;
Dean Nelson6e410172008-07-29 22:34:09 -07001020 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001021
Dean Nelsone54af722005-10-25 14:07:43 -05001022 /* wait for the discovery thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001023 wait_for_completion(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001024
Dean Nelsone54af722005-10-25 14:07:43 -05001025 /* wait for the heartbeat checker thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001026 wait_for_completion(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001027
Dean Nelsona607c382005-09-01 14:01:37 -05001028 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -05001029 (void)msleep_interruptible(300);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001030
1031 /* wait for all partitions to become inactive */
1032
Dean Nelsona47d5da2008-07-29 22:34:09 -07001033 printmsg_time = jiffies + (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
1034 xpc_disengage_timedout = 0;
Dean Nelsona607c382005-09-01 14:01:37 -05001035
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001036 do {
1037 active_part_count = 0;
1038
Dean Nelsonbc63d382008-07-29 22:34:04 -07001039 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001040 part = &xpc_partitions[partid];
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001041
Dean Nelsona607c382005-09-01 14:01:37 -05001042 if (xpc_partition_disengaged(part) &&
Dean Nelson83469b52008-07-29 22:34:18 -07001043 part->act_state == XPC_P_AS_INACTIVE) {
Dean Nelsona607c382005-09-01 14:01:37 -05001044 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001045 }
Dean Nelsona607c382005-09-01 14:01:37 -05001046
1047 active_part_count++;
1048
1049 XPC_DEACTIVATE_PARTITION(part, reason);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001050
Dean Nelsona47d5da2008-07-29 22:34:09 -07001051 if (part->disengage_timeout > disengage_timeout)
1052 disengage_timeout = part->disengage_timeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001053 }
1054
Dean Nelsona47d5da2008-07-29 22:34:09 -07001055 if (xpc_any_partition_engaged()) {
Dean Nelsonaaa3cd62008-07-29 22:34:07 -07001056 if (time_is_before_jiffies(printmsg_time)) {
Dean Nelson1ecaded2006-01-06 09:48:21 -06001057 dev_info(xpc_part, "waiting for remote "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001058 "partitions to deactivate, timeout in "
1059 "%ld seconds\n", (disengage_timeout -
1060 jiffies) / HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001061 printmsg_time = jiffies +
Dean Nelsona47d5da2008-07-29 22:34:09 -07001062 (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001063 printed_waiting_msg = 1;
1064 }
1065
1066 } else if (active_part_count > 0) {
1067 if (printed_waiting_msg) {
1068 dev_info(xpc_part, "waiting for local partition"
Dean Nelsona47d5da2008-07-29 22:34:09 -07001069 " to deactivate\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -06001070 printed_waiting_msg = 0;
1071 }
1072
1073 } else {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001074 if (!xpc_disengage_timedout) {
Dean Nelson1ecaded2006-01-06 09:48:21 -06001075 dev_info(xpc_part, "all partitions have "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001076 "deactivated\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -06001077 }
1078 break;
Dean Nelsona607c382005-09-01 14:01:37 -05001079 }
1080
1081 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -05001082 (void)msleep_interruptible(300);
Dean Nelsona607c382005-09-01 14:01:37 -05001083
1084 } while (1);
1085
Dean Nelsona47d5da2008-07-29 22:34:09 -07001086 DBUG_ON(xpc_any_partition_engaged());
Dean Nelson33ba3c72008-07-29 22:34:07 -07001087 DBUG_ON(xpc_any_hbs_allowed() != 0);
Dean Nelsona607c382005-09-01 14:01:37 -05001088
Dean Nelson5b8669d2008-07-29 22:34:18 -07001089 xpc_teardown_rsvd_page();
Dean Nelsona607c382005-09-01 14:01:37 -05001090
Dean Nelson65c17b82008-05-12 14:02:02 -07001091 if (reason == xpUnloading) {
Dean Nelson35190502008-04-22 14:48:55 -05001092 (void)unregister_die_notifier(&xpc_die_notifier);
Dean Nelsonbc63d382008-07-29 22:34:04 -07001093 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson0752c672006-01-10 11:07:19 -06001094 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001095
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001096 /* clear the interface to XPC's functions */
1097 xpc_clear_interface();
1098
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001099 if (xpc_sysctl)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001100 unregister_sysctl_table(xpc_sysctl);
Dean Nelson7682a4c2006-08-08 15:03:29 -05001101
Dean Nelson5b8669d2008-07-29 22:34:18 -07001102 xpc_teardown_partitions();
Dean Nelson6e410172008-07-29 22:34:09 -07001103
1104 if (is_shub())
1105 xpc_exit_sn2();
1106 else
1107 xpc_exit_uv();
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001108}
1109
Dean Nelsona607c382005-09-01 14:01:37 -05001110/*
Dean Nelsond6ad0332006-01-10 11:08:55 -06001111 * This function is called when the system is being rebooted.
1112 */
1113static int
1114xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
1115{
Dean Nelson65c17b82008-05-12 14:02:02 -07001116 enum xp_retval reason;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001117
Dean Nelsond6ad0332006-01-10 11:08:55 -06001118 switch (event) {
1119 case SYS_RESTART:
Dean Nelson65c17b82008-05-12 14:02:02 -07001120 reason = xpSystemReboot;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001121 break;
1122 case SYS_HALT:
Dean Nelson65c17b82008-05-12 14:02:02 -07001123 reason = xpSystemHalt;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001124 break;
1125 case SYS_POWER_OFF:
Dean Nelson65c17b82008-05-12 14:02:02 -07001126 reason = xpSystemPoweroff;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001127 break;
1128 default:
Dean Nelson65c17b82008-05-12 14:02:02 -07001129 reason = xpSystemGoingDown;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001130 }
1131
1132 xpc_do_exit(reason);
1133 return NOTIFY_DONE;
1134}
1135
Dean Nelsond6ad0332006-01-10 11:08:55 -06001136/*
Dean Nelsona47d5da2008-07-29 22:34:09 -07001137 * Notify other partitions to deactivate from us by first disengaging from all
1138 * references to our memory.
Dean Nelson780d09e2005-11-09 14:41:57 -06001139 */
1140static void
Dean Nelsona47d5da2008-07-29 22:34:09 -07001141xpc_die_deactivate(void)
Dean Nelson780d09e2005-11-09 14:41:57 -06001142{
1143 struct xpc_partition *part;
Dean Nelson64d032b2008-05-12 14:02:03 -07001144 short partid;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001145 int any_engaged;
Dean Nelson261f3b42008-07-29 22:34:16 -07001146 long keep_waiting;
1147 long wait_to_print;
Dean Nelson780d09e2005-11-09 14:41:57 -06001148
Dean Nelson780d09e2005-11-09 14:41:57 -06001149 /* keep xpc_hb_checker thread from doing anything (just in case) */
1150 xpc_exiting = 1;
1151
Dean Nelson33ba3c72008-07-29 22:34:07 -07001152 xpc_disallow_all_hbs(); /*indicate we're deactivated */
Dean Nelson780d09e2005-11-09 14:41:57 -06001153
Dean Nelsonbc63d382008-07-29 22:34:04 -07001154 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001155 part = &xpc_partitions[partid];
1156
Dean Nelsona47d5da2008-07-29 22:34:09 -07001157 if (xpc_partition_engaged(partid) ||
Dean Nelson83469b52008-07-29 22:34:18 -07001158 part->act_state != XPC_P_AS_INACTIVE) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001159 xpc_request_partition_deactivation(part);
1160 xpc_indicate_partition_disengaged(part);
Dean Nelson780d09e2005-11-09 14:41:57 -06001161 }
1162 }
1163
Dean Nelsona47d5da2008-07-29 22:34:09 -07001164 /*
1165 * Though we requested that all other partitions deactivate from us,
Dean Nelson261f3b42008-07-29 22:34:16 -07001166 * we only wait until they've all disengaged or we've reached the
1167 * defined timelimit.
1168 *
1169 * Given that one iteration through the following while-loop takes
1170 * approximately 200 microseconds, calculate the #of loops to take
1171 * before bailing and the #of loops before printing a waiting message.
Dean Nelsona47d5da2008-07-29 22:34:09 -07001172 */
Dean Nelson261f3b42008-07-29 22:34:16 -07001173 keep_waiting = xpc_disengage_timelimit * 1000 * 5;
1174 wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL * 1000 * 5;
Dean Nelson780d09e2005-11-09 14:41:57 -06001175
Dean Nelson1ecaded2006-01-06 09:48:21 -06001176 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001177 any_engaged = xpc_any_partition_engaged();
1178 if (!any_engaged) {
1179 dev_info(xpc_part, "all partitions have deactivated\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -06001180 break;
1181 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001182
Dean Nelson261f3b42008-07-29 22:34:16 -07001183 if (!keep_waiting--) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001184 for (partid = 0; partid < xp_max_npartitions;
1185 partid++) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001186 if (xpc_partition_engaged(partid)) {
1187 dev_info(xpc_part, "deactivate from "
Dean Nelson35190502008-04-22 14:48:55 -05001188 "remote partition %d timed "
1189 "out\n", partid);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001190 }
1191 }
1192 break;
1193 }
1194
Dean Nelson261f3b42008-07-29 22:34:16 -07001195 if (!wait_to_print--) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001196 dev_info(xpc_part, "waiting for remote partitions to "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001197 "deactivate, timeout in %ld seconds\n",
Dean Nelson261f3b42008-07-29 22:34:16 -07001198 keep_waiting / (1000 * 5));
1199 wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL *
1200 1000 * 5;
Dean Nelson780d09e2005-11-09 14:41:57 -06001201 }
Dean Nelson261f3b42008-07-29 22:34:16 -07001202
1203 udelay(200);
Dean Nelson780d09e2005-11-09 14:41:57 -06001204 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001205}
1206
Dean Nelson780d09e2005-11-09 14:41:57 -06001207/*
Dean Nelson1f4674b2006-01-10 11:08:00 -06001208 * This function is called when the system is being restarted or halted due
1209 * to some sort of system failure. If this is the case we need to notify the
1210 * other partitions to disengage from all references to our memory.
1211 * This function can also be called when our heartbeater could be offlined
1212 * for a time. In this case we need to notify other partitions to not worry
1213 * about the lack of a heartbeat.
Dean Nelson780d09e2005-11-09 14:41:57 -06001214 */
1215static int
1216xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1217{
Dean Nelson261f3b42008-07-29 22:34:16 -07001218#ifdef CONFIG_IA64 /* !!! temporary kludge */
Dean Nelson780d09e2005-11-09 14:41:57 -06001219 switch (event) {
1220 case DIE_MACHINE_RESTART:
1221 case DIE_MACHINE_HALT:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001222 xpc_die_deactivate();
Dean Nelson780d09e2005-11-09 14:41:57 -06001223 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001224
1225 case DIE_KDEBUG_ENTER:
1226 /* Should lack of heartbeat be ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001227 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001228 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001229
Dean Nelson1f4674b2006-01-10 11:08:00 -06001230 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001231 case DIE_MCA_MONARCH_ENTER:
1232 case DIE_INIT_MONARCH_ENTER:
Dean Nelson33ba3c72008-07-29 22:34:07 -07001233 xpc_offline_heartbeat();
Dean Nelson780d09e2005-11-09 14:41:57 -06001234 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001235
1236 case DIE_KDEBUG_LEAVE:
1237 /* Is lack of heartbeat being ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001238 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001239 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001240
Dean Nelson1f4674b2006-01-10 11:08:00 -06001241 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001242 case DIE_MCA_MONARCH_LEAVE:
1243 case DIE_INIT_MONARCH_LEAVE:
Dean Nelson33ba3c72008-07-29 22:34:07 -07001244 xpc_online_heartbeat();
Dean Nelson780d09e2005-11-09 14:41:57 -06001245 break;
1246 }
Dean Nelson261f3b42008-07-29 22:34:16 -07001247#else
1248 xpc_die_deactivate();
1249#endif
Dean Nelson780d09e2005-11-09 14:41:57 -06001250
1251 return NOTIFY_DONE;
1252}
1253
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001254int __init
1255xpc_init(void)
1256{
1257 int ret;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001258 struct task_struct *kthread;
Dean Nelsonee6665e2008-07-29 22:34:13 -07001259
1260 snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1261 snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001262
Dean Nelson94bd2702008-07-29 22:34:05 -07001263 if (is_shub()) {
1264 /*
1265 * The ia64-sn2 architecture supports at most 64 partitions.
Dean Nelsonc39838c2008-07-29 22:34:11 -07001266 * And the inability to unregister remote amos restricts us
Dean Nelson94bd2702008-07-29 22:34:05 -07001267 * further to only support exactly 64 partitions on this
1268 * architecture, no less.
1269 */
Dean Nelson5b8669d2008-07-29 22:34:18 -07001270 if (xp_max_npartitions != 64) {
1271 dev_err(xpc_part, "max #of partitions not set to 64\n");
1272 ret = -EINVAL;
1273 } else {
1274 ret = xpc_init_sn2();
1275 }
Dean Nelson94bd2702008-07-29 22:34:05 -07001276
1277 } else if (is_uv()) {
Dean Nelson5b8669d2008-07-29 22:34:18 -07001278 ret = xpc_init_uv();
Dean Nelson94bd2702008-07-29 22:34:05 -07001279
1280 } else {
Dean Nelson5b8669d2008-07-29 22:34:18 -07001281 ret = -ENODEV;
Dean Nelson94bd2702008-07-29 22:34:05 -07001282 }
Dean Nelson408865c2005-09-08 10:46:58 -05001283
Dean Nelson5b8669d2008-07-29 22:34:18 -07001284 if (ret != 0)
1285 return ret;
1286
1287 ret = xpc_setup_partitions();
1288 if (ret != 0) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001289 dev_err(xpc_part, "can't get memory for partition structure\n");
Dean Nelsonee6665e2008-07-29 22:34:13 -07001290 goto out_1;
Dean Nelsonbc63d382008-07-29 22:34:04 -07001291 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001292
Dean Nelsonbc63d382008-07-29 22:34:04 -07001293 xpc_sysctl = register_sysctl_table(xpc_sys_dir);
1294
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001295 /*
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001296 * Fill the partition reserved page with the information needed by
1297 * other partitions to discover we are alive and establish initial
1298 * communications.
1299 */
Dean Nelson5b8669d2008-07-29 22:34:18 -07001300 ret = xpc_setup_rsvd_page();
1301 if (ret != 0) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001302 dev_err(xpc_part, "can't setup our reserved page\n");
Dean Nelsonee6665e2008-07-29 22:34:13 -07001303 goto out_2;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001304 }
1305
Dean Nelsona607c382005-09-01 14:01:37 -05001306 /* add ourselves to the reboot_notifier_list */
1307 ret = register_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001308 if (ret != 0)
Dean Nelsona607c382005-09-01 14:01:37 -05001309 dev_warn(xpc_part, "can't register reboot notifier\n");
Dean Nelsona607c382005-09-01 14:01:37 -05001310
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07001311 /* add ourselves to the die_notifier list */
Dean Nelson780d09e2005-11-09 14:41:57 -06001312 ret = register_die_notifier(&xpc_die_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001313 if (ret != 0)
Dean Nelson780d09e2005-11-09 14:41:57 -06001314 dev_warn(xpc_part, "can't register die notifier\n");
Dean Nelson780d09e2005-11-09 14:41:57 -06001315
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001316 /*
1317 * The real work-horse behind xpc. This processes incoming
1318 * interrupts and monitors remote heartbeats.
1319 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001320 kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
1321 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001322 dev_err(xpc_part, "failed while forking hb check thread\n");
Dean Nelsonbc63d382008-07-29 22:34:04 -07001323 ret = -EBUSY;
Dean Nelsonee6665e2008-07-29 22:34:13 -07001324 goto out_3;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001325 }
1326
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001327 /*
1328 * Startup a thread that will attempt to discover other partitions to
1329 * activate based on info provided by SAL. This new thread is short
1330 * lived and will exit once discovery is complete.
1331 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001332 kthread = kthread_run(xpc_initiate_discovery, NULL,
1333 XPC_DISCOVERY_THREAD_NAME);
1334 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001335 dev_err(xpc_part, "failed while forking discovery thread\n");
1336
1337 /* mark this new thread as a non-starter */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001338 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001339
Dean Nelson65c17b82008-05-12 14:02:02 -07001340 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001341 return -EBUSY;
1342 }
1343
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001344 /* set the interface to point at XPC's functions */
1345 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001346 xpc_initiate_send, xpc_initiate_send_notify,
1347 xpc_initiate_received, xpc_initiate_partid_to_nasids);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001348
1349 return 0;
Dean Nelsonbc63d382008-07-29 22:34:04 -07001350
1351 /* initialization was not successful */
Dean Nelsonee6665e2008-07-29 22:34:13 -07001352out_3:
Dean Nelson5b8669d2008-07-29 22:34:18 -07001353 xpc_teardown_rsvd_page();
Dean Nelson94bd2702008-07-29 22:34:05 -07001354
Dean Nelsonbc63d382008-07-29 22:34:04 -07001355 (void)unregister_die_notifier(&xpc_die_notifier);
1356 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
Dean Nelsonee6665e2008-07-29 22:34:13 -07001357out_2:
Dean Nelsonbc63d382008-07-29 22:34:04 -07001358 if (xpc_sysctl)
1359 unregister_sysctl_table(xpc_sysctl);
Dean Nelson5b8669d2008-07-29 22:34:18 -07001360
1361 xpc_teardown_partitions();
Dean Nelson6e410172008-07-29 22:34:09 -07001362out_1:
1363 if (is_shub())
1364 xpc_exit_sn2();
1365 else
1366 xpc_exit_uv();
Dean Nelsonbc63d382008-07-29 22:34:04 -07001367 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001368}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001369
Dean Nelson35190502008-04-22 14:48:55 -05001370module_init(xpc_init);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001371
1372void __exit
1373xpc_exit(void)
1374{
Dean Nelson65c17b82008-05-12 14:02:02 -07001375 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001376}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001377
Dean Nelson35190502008-04-22 14:48:55 -05001378module_exit(xpc_exit);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001379
1380MODULE_AUTHOR("Silicon Graphics, Inc.");
1381MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1382MODULE_LICENSE("GPL");
1383
1384module_param(xpc_hb_interval, int, 0);
1385MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001386 "heartbeat increments.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001387
1388module_param(xpc_hb_check_interval, int, 0);
1389MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001390 "heartbeat checks.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001391
Dean Nelsona47d5da2008-07-29 22:34:09 -07001392module_param(xpc_disengage_timelimit, int, 0);
1393MODULE_PARM_DESC(xpc_disengage_timelimit, "Number of seconds to wait "
1394 "for disengage to complete.");
Dean Nelsone54af722005-10-25 14:07:43 -05001395
Dean Nelson1f4674b2006-01-10 11:08:00 -06001396module_param(xpc_kdebug_ignore, int, 0);
1397MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
Dean Nelson35190502008-04-22 14:48:55 -05001398 "other partitions when dropping into kdebug.");