blob: 1ab9fda87fabaf581fcb5bf6e6151da7b2ca1e27 [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 = {
Kay Sieversbb0dc432009-01-06 10:44:37 -080062 .init_name = "", /* set to "part" at xpc_init() time */
Dean Nelson89eb8eb2005-03-23 19:50:00 -070063 .driver = &xpc_dbg_name
64};
65
66struct device xpc_chan_dbg_subname = {
Kay Sieversbb0dc432009-01-06 10:44:37 -080067 .init_name = "", /* set to "chan" at xpc_init() time */
Dean Nelson89eb8eb2005-03-23 19:50:00 -070068 .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);
Jack Steiner6f2584f2009-04-02 16:59:10 -0700174void (*xpc_teardown_partitions_sn) (void);
Dean Nelsona812dcc2008-07-29 22:34:16 -0700175enum xp_retval (*xpc_get_partition_rsvd_page_pa) (void *buf, u64 *cookie,
176 unsigned long *rp_pa,
177 size_t *len);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700178int (*xpc_setup_rsvd_page_sn) (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);
Dean Nelson61deb862008-07-29 22:34:17 -0700184enum xp_retval (*xpc_get_remote_heartbeat) (struct xpc_partition *part);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700185
Dean Nelsone17d4162008-07-29 22:34:06 -0700186enum xp_retval (*xpc_make_first_contact) (struct xpc_partition *part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700187void (*xpc_notify_senders_of_disconnect) (struct xpc_channel *ch);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700188u64 (*xpc_get_chctl_all_flags) (struct xpc_partition *part);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700189enum xp_retval (*xpc_setup_msg_structures) (struct xpc_channel *ch);
190void (*xpc_teardown_msg_structures) (struct xpc_channel *ch);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700191void (*xpc_process_msg_chctl_flags) (struct xpc_partition *part, int ch_number);
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700192int (*xpc_n_of_deliverable_payloads) (struct xpc_channel *ch);
193void *(*xpc_get_deliverable_payload) (struct xpc_channel *ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700194
Dean Nelsona47d5da2008-07-29 22:34:09 -0700195void (*xpc_request_partition_activation) (struct xpc_rsvd_page *remote_rp,
Dean Nelsona812dcc2008-07-29 22:34:16 -0700196 unsigned long remote_rp_pa,
197 int nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700198void (*xpc_request_partition_reactivation) (struct xpc_partition *part);
199void (*xpc_request_partition_deactivation) (struct xpc_partition *part);
200void (*xpc_cancel_partition_deactivation_request) (struct xpc_partition *part);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700201
Dean Nelson5b8669d2008-07-29 22:34:18 -0700202void (*xpc_process_activate_IRQ_rcvd) (void);
203enum xp_retval (*xpc_setup_ch_structures_sn) (struct xpc_partition *part);
204void (*xpc_teardown_ch_structures_sn) (struct xpc_partition *part);
Dean Nelsone17d4162008-07-29 22:34:06 -0700205
Dean Nelsona47d5da2008-07-29 22:34:09 -0700206void (*xpc_indicate_partition_engaged) (struct xpc_partition *part);
207int (*xpc_partition_engaged) (short partid);
208int (*xpc_any_partition_engaged) (void);
209void (*xpc_indicate_partition_disengaged) (struct xpc_partition *part);
210void (*xpc_assume_partition_disengaged) (short partid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700211
Dean Nelson7fb5e592008-07-29 22:34:10 -0700212void (*xpc_send_chctl_closerequest) (struct xpc_channel *ch,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700213 unsigned long *irq_flags);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700214void (*xpc_send_chctl_closereply) (struct xpc_channel *ch,
215 unsigned long *irq_flags);
216void (*xpc_send_chctl_openrequest) (struct xpc_channel *ch,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700217 unsigned long *irq_flags);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700218void (*xpc_send_chctl_openreply) (struct xpc_channel *ch,
219 unsigned long *irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700220
Jack Steiner6f2584f2009-04-02 16:59:10 -0700221enum xp_retval (*xpc_save_remote_msgqueue_pa) (struct xpc_channel *ch,
222 unsigned long msgqueue_pa);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700223
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700224enum xp_retval (*xpc_send_payload) (struct xpc_channel *ch, u32 flags,
225 void *payload, u16 payload_size,
226 u8 notify_type, xpc_notify_func func,
227 void *key);
228void (*xpc_received_payload) (struct xpc_channel *ch, void *payload);
Dean Nelson94bd2702008-07-29 22:34:05 -0700229
Dean Nelsona607c382005-09-01 14:01:37 -0500230/*
Dean Nelsona47d5da2008-07-29 22:34:09 -0700231 * Timer function to enforce the timelimit on the partition disengage.
Dean Nelsona607c382005-09-01 14:01:37 -0500232 */
233static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700234xpc_timeout_partition_disengage(unsigned long data)
Dean Nelsona607c382005-09-01 14:01:37 -0500235{
Dean Nelson35190502008-04-22 14:48:55 -0500236 struct xpc_partition *part = (struct xpc_partition *)data;
Dean Nelsona607c382005-09-01 14:01:37 -0500237
Dean Nelsona47d5da2008-07-29 22:34:09 -0700238 DBUG_ON(time_is_after_jiffies(part->disengage_timeout));
Dean Nelsona607c382005-09-01 14:01:37 -0500239
Dean Nelson35190502008-04-22 14:48:55 -0500240 (void)xpc_partition_disengaged(part);
Dean Nelsona607c382005-09-01 14:01:37 -0500241
Dean Nelsona47d5da2008-07-29 22:34:09 -0700242 DBUG_ON(part->disengage_timeout != 0);
243 DBUG_ON(xpc_partition_engaged(XPC_PARTID(part)));
Dean Nelsona607c382005-09-01 14:01:37 -0500244}
245
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700246/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700247 * Timer to produce the heartbeat. The timer structures function is
248 * already set when this is initially called. A tunable is used to
249 * specify when the next timeout should occur.
250 */
251static void
252xpc_hb_beater(unsigned long dummy)
253{
Dean Nelson33ba3c72008-07-29 22:34:07 -0700254 xpc_increment_heartbeat();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700255
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700256 if (time_is_before_eq_jiffies(xpc_hb_check_timeout))
Dean Nelson6e410172008-07-29 22:34:09 -0700257 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700258
259 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
260 add_timer(&xpc_hb_timer);
261}
262
Dean Nelson33ba3c72008-07-29 22:34:07 -0700263static void
264xpc_start_hb_beater(void)
265{
266 xpc_heartbeat_init();
267 init_timer(&xpc_hb_timer);
268 xpc_hb_timer.function = xpc_hb_beater;
269 xpc_hb_beater(0);
270}
271
272static void
273xpc_stop_hb_beater(void)
274{
275 del_timer_sync(&xpc_hb_timer);
276 xpc_heartbeat_exit();
277}
278
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700279/*
Dean Nelson61deb862008-07-29 22:34:17 -0700280 * At periodic intervals, scan through all active partitions and ensure
281 * their heartbeat is still active. If not, the partition is deactivated.
282 */
283static void
284xpc_check_remote_hb(void)
285{
286 struct xpc_partition *part;
287 short partid;
288 enum xp_retval ret;
289
290 for (partid = 0; partid < xp_max_npartitions; partid++) {
291
292 if (xpc_exiting)
293 break;
294
295 if (partid == xp_partition_id)
296 continue;
297
298 part = &xpc_partitions[partid];
299
Dean Nelson83469b52008-07-29 22:34:18 -0700300 if (part->act_state == XPC_P_AS_INACTIVE ||
301 part->act_state == XPC_P_AS_DEACTIVATING) {
Dean Nelson61deb862008-07-29 22:34:17 -0700302 continue;
303 }
304
305 ret = xpc_get_remote_heartbeat(part);
306 if (ret != xpSuccess)
307 XPC_DEACTIVATE_PARTITION(part, ret);
308 }
309}
310
311/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700312 * This thread is responsible for nearly all of the partition
313 * activation/deactivation.
314 */
315static int
316xpc_hb_checker(void *ignore)
317{
Dean Nelson35190502008-04-22 14:48:55 -0500318 int force_IRQ = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700319
320 /* this thread was marked active by xpc_hb_init() */
321
Rusty Russellf7df8ed2009-01-10 21:58:09 -0800322 set_cpus_allowed_ptr(current, cpumask_of(XPC_HB_CHECK_CPU));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700323
Dean Nelson4c013f52007-11-07 07:53:06 -0600324 /* set our heartbeating to other partitions into motion */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700325 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700326 xpc_start_hb_beater();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700327
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500328 while (!xpc_exiting) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700329
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700330 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
331 "been received\n",
Dean Nelson35190502008-04-22 14:48:55 -0500332 (int)(xpc_hb_check_timeout - jiffies),
Dean Nelson5b8669d2008-07-29 22:34:18 -0700333 xpc_activate_IRQ_rcvd);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700334
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700335 /* checking of remote heartbeats is skewed by IRQ handling */
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700336 if (time_is_before_eq_jiffies(xpc_hb_check_timeout)) {
Dean Nelson5b8669d2008-07-29 22:34:18 -0700337 xpc_hb_check_timeout = jiffies +
338 (xpc_hb_check_interval * HZ);
339
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700340 dev_dbg(xpc_part, "checking remote heartbeats\n");
341 xpc_check_remote_hb();
342
343 /*
Dean Nelson5b8669d2008-07-29 22:34:18 -0700344 * On sn2 we need to periodically recheck to ensure no
345 * IRQ/amo pairs have been missed.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700346 */
Dean Nelson5b8669d2008-07-29 22:34:18 -0700347 if (is_shub())
348 force_IRQ = 1;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700349 }
350
Dean Nelsona607c382005-09-01 14:01:37 -0500351 /* check for outstanding IRQs */
Dean Nelson5b8669d2008-07-29 22:34:18 -0700352 if (xpc_activate_IRQ_rcvd > 0 || force_IRQ != 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700353 force_IRQ = 0;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700354 dev_dbg(xpc_part, "processing activate IRQs "
355 "received\n");
356 xpc_process_activate_IRQ_rcvd();
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700357 }
Dean Nelsona607c382005-09-01 14:01:37 -0500358
359 /* wait for IRQ or timeout */
Dean Nelson6e410172008-07-29 22:34:09 -0700360 (void)wait_event_interruptible(xpc_activate_IRQ_wq,
Dean Nelson5b8669d2008-07-29 22:34:18 -0700361 (time_is_before_eq_jiffies(
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700362 xpc_hb_check_timeout) ||
Dean Nelson5b8669d2008-07-29 22:34:18 -0700363 xpc_activate_IRQ_rcvd > 0 ||
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500364 xpc_exiting));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700365 }
366
Dean Nelson33ba3c72008-07-29 22:34:07 -0700367 xpc_stop_hb_beater();
368
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700369 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
370
Dean Nelsone54af722005-10-25 14:07:43 -0500371 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500372 complete(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700373 return 0;
374}
375
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700376/*
377 * This thread will attempt to discover other partitions to activate
378 * based on info provided by SAL. This new thread is short lived and
379 * will exit once discovery is complete.
380 */
381static int
382xpc_initiate_discovery(void *ignore)
383{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700384 xpc_discovery();
385
386 dev_dbg(xpc_part, "discovery thread is exiting\n");
387
Dean Nelsone54af722005-10-25 14:07:43 -0500388 /* mark this thread as having exited */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500389 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700390 return 0;
391}
392
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700393/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700394 * The first kthread assigned to a newly activated partition is the one
Dean Nelsone17d4162008-07-29 22:34:06 -0700395 * created by XPC HB with which it calls xpc_activating(). XPC hangs on to
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700396 * that kthread until the partition is brought down, at which time that kthread
397 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
398 * that XPC has dismantled all communication infrastructure for the associated
399 * partition.) This kthread becomes the channel manager for that partition.
400 *
401 * Each active partition has a channel manager, who, besides connecting and
402 * disconnecting channels, will ensure that each of the partition's connected
403 * channels has the required number of assigned kthreads to get the work done.
404 */
405static void
406xpc_channel_mgr(struct xpc_partition *part)
407{
Dean Nelson83469b52008-07-29 22:34:18 -0700408 while (part->act_state != XPC_P_AS_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500409 atomic_read(&part->nchannels_active) > 0 ||
410 !xpc_partition_disengaged(part)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700411
Dean Nelson7fb5e592008-07-29 22:34:10 -0700412 xpc_process_sent_chctl_flags(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700413
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700414 /*
415 * Wait until we've been requested to activate kthreads or
416 * all of the channel's message queues have been torn down or
417 * a signal is pending.
418 *
419 * The channel_mgr_requests is set to 1 after being awakened,
420 * This is done to prevent the channel mgr from making one pass
421 * through the loop for each request, since he will
422 * be servicing all the requests in one pass. The reason it's
423 * set to 1 instead of 0 is so that other kthreads will know
424 * that the channel mgr is running and won't bother trying to
425 * wake him up.
426 */
427 atomic_dec(&part->channel_mgr_requests);
Dean Nelson35190502008-04-22 14:48:55 -0500428 (void)wait_event_interruptible(part->channel_mgr_wq,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500429 (atomic_read(&part->channel_mgr_requests) > 0 ||
Dean Nelson7fb5e592008-07-29 22:34:10 -0700430 part->chctl.all_flags != 0 ||
Dean Nelson83469b52008-07-29 22:34:18 -0700431 (part->act_state == XPC_P_AS_DEACTIVATING &&
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500432 atomic_read(&part->nchannels_active) == 0 &&
433 xpc_partition_disengaged(part))));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700434 atomic_set(&part->channel_mgr_requests, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700435 }
436}
437
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700438/*
Dean Nelson5b8669d2008-07-29 22:34:18 -0700439 * Guarantee that the kzalloc'd memory is cacheline aligned.
440 */
441void *
442xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
443{
444 /* see if kzalloc will give us cachline aligned memory by default */
445 *base = kzalloc(size, flags);
446 if (*base == NULL)
447 return NULL;
448
449 if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
450 return *base;
451
452 kfree(*base);
453
454 /* nope, we'll have to do it ourselves */
455 *base = kzalloc(size + L1_CACHE_BYTES, flags);
456 if (*base == NULL)
457 return NULL;
458
459 return (void *)L1_CACHE_ALIGN((u64)*base);
460}
461
462/*
463 * Setup the channel structures necessary to support XPartition Communication
464 * between the specified remote partition and the local one.
465 */
466static enum xp_retval
467xpc_setup_ch_structures(struct xpc_partition *part)
468{
469 enum xp_retval ret;
470 int ch_number;
471 struct xpc_channel *ch;
472 short partid = XPC_PARTID(part);
473
474 /*
475 * Allocate all of the channel structures as a contiguous chunk of
476 * memory.
477 */
478 DBUG_ON(part->channels != NULL);
479 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
480 GFP_KERNEL);
481 if (part->channels == NULL) {
482 dev_err(xpc_chan, "can't get memory for channels\n");
483 return xpNoMemory;
484 }
485
486 /* allocate the remote open and close args */
487
488 part->remote_openclose_args =
489 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE,
490 GFP_KERNEL, &part->
491 remote_openclose_args_base);
492 if (part->remote_openclose_args == NULL) {
493 dev_err(xpc_chan, "can't get memory for remote connect args\n");
494 ret = xpNoMemory;
495 goto out_1;
496 }
497
498 part->chctl.all_flags = 0;
499 spin_lock_init(&part->chctl_lock);
500
501 atomic_set(&part->channel_mgr_requests, 1);
502 init_waitqueue_head(&part->channel_mgr_wq);
503
504 part->nchannels = XPC_MAX_NCHANNELS;
505
506 atomic_set(&part->nchannels_active, 0);
507 atomic_set(&part->nchannels_engaged, 0);
508
509 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
510 ch = &part->channels[ch_number];
511
512 ch->partid = partid;
513 ch->number = ch_number;
514 ch->flags = XPC_C_DISCONNECTED;
515
516 atomic_set(&ch->kthreads_assigned, 0);
517 atomic_set(&ch->kthreads_idle, 0);
518 atomic_set(&ch->kthreads_active, 0);
519
520 atomic_set(&ch->references, 0);
521 atomic_set(&ch->n_to_notify, 0);
522
523 spin_lock_init(&ch->lock);
524 init_completion(&ch->wdisconnect_wait);
525
526 atomic_set(&ch->n_on_msg_allocate_wq, 0);
527 init_waitqueue_head(&ch->msg_allocate_wq);
528 init_waitqueue_head(&ch->idle_wq);
529 }
530
531 ret = xpc_setup_ch_structures_sn(part);
532 if (ret != xpSuccess)
533 goto out_2;
534
535 /*
536 * With the setting of the partition setup_state to XPC_P_SS_SETUP,
537 * we're declaring that this partition is ready to go.
538 */
539 part->setup_state = XPC_P_SS_SETUP;
540
541 return xpSuccess;
542
543 /* setup of ch structures failed */
544out_2:
545 kfree(part->remote_openclose_args_base);
546 part->remote_openclose_args = NULL;
547out_1:
548 kfree(part->channels);
549 part->channels = NULL;
550 return ret;
551}
552
553/*
554 * Teardown the channel structures necessary to support XPartition Communication
555 * between the specified remote partition and the local one.
556 */
557static void
558xpc_teardown_ch_structures(struct xpc_partition *part)
559{
560 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
561 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
562
563 /*
564 * Make this partition inaccessible to local processes by marking it
565 * as no longer setup. Then wait before proceeding with the teardown
566 * until all existing references cease.
567 */
568 DBUG_ON(part->setup_state != XPC_P_SS_SETUP);
569 part->setup_state = XPC_P_SS_WTEARDOWN;
570
571 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
572
573 /* now we can begin tearing down the infrastructure */
574
575 xpc_teardown_ch_structures_sn(part);
576
577 kfree(part->remote_openclose_args_base);
578 part->remote_openclose_args = NULL;
579 kfree(part->channels);
580 part->channels = NULL;
581
582 part->setup_state = XPC_P_SS_TORNDOWN;
583}
584
585/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700586 * When XPC HB determines that a partition has come up, it will create a new
587 * kthread and that kthread will call this function to attempt to set up the
588 * basic infrastructure used for Cross Partition Communication with the newly
589 * upped partition.
590 *
591 * The kthread that was created by XPC HB and which setup the XPC
Dean Nelsone17d4162008-07-29 22:34:06 -0700592 * infrastructure will remain assigned to the partition becoming the channel
593 * manager for that partition until the partition is deactivating, at which
594 * time the kthread will teardown the XPC infrastructure and then exit.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700595 */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700596static int
597xpc_activating(void *__partid)
598{
Dean Nelson64d032b2008-05-12 14:02:03 -0700599 short partid = (u64)__partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700600 struct xpc_partition *part = &xpc_partitions[partid];
601 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700602
Dean Nelsonbc63d382008-07-29 22:34:04 -0700603 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700604
605 spin_lock_irqsave(&part->act_lock, irq_flags);
606
Dean Nelson83469b52008-07-29 22:34:18 -0700607 if (part->act_state == XPC_P_AS_DEACTIVATING) {
608 part->act_state = XPC_P_AS_INACTIVE;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700609 spin_unlock_irqrestore(&part->act_lock, irq_flags);
610 part->remote_rp_pa = 0;
611 return 0;
612 }
613
614 /* indicate the thread is activating */
Dean Nelson83469b52008-07-29 22:34:18 -0700615 DBUG_ON(part->act_state != XPC_P_AS_ACTIVATION_REQ);
616 part->act_state = XPC_P_AS_ACTIVATING;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700617
618 XPC_SET_REASON(part, 0, 0);
619 spin_unlock_irqrestore(&part->act_lock, irq_flags);
620
Dean Nelsone17d4162008-07-29 22:34:06 -0700621 dev_dbg(xpc_part, "activating partition %d\n", partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700622
Dean Nelson33ba3c72008-07-29 22:34:07 -0700623 xpc_allow_hb(partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700624
Dean Nelson5b8669d2008-07-29 22:34:18 -0700625 if (xpc_setup_ch_structures(part) == xpSuccess) {
Dean Nelsone17d4162008-07-29 22:34:06 -0700626 (void)xpc_part_ref(part); /* this will always succeed */
627
628 if (xpc_make_first_contact(part) == xpSuccess) {
629 xpc_mark_partition_active(part);
630 xpc_channel_mgr(part);
631 /* won't return until partition is deactivating */
632 }
633
634 xpc_part_deref(part);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700635 xpc_teardown_ch_structures(part);
Dean Nelsone17d4162008-07-29 22:34:06 -0700636 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700637
Dean Nelson33ba3c72008-07-29 22:34:07 -0700638 xpc_disallow_hb(partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700639 xpc_mark_partition_inactive(part);
640
Dean Nelson65c17b82008-05-12 14:02:02 -0700641 if (part->reason == xpReactivating) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700642 /* interrupting ourselves results in activating partition */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700643 xpc_request_partition_reactivation(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700644 }
645
646 return 0;
647}
648
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700649void
650xpc_activate_partition(struct xpc_partition *part)
651{
Dean Nelson64d032b2008-05-12 14:02:03 -0700652 short partid = XPC_PARTID(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700653 unsigned long irq_flags;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500654 struct task_struct *kthread;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700655
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700656 spin_lock_irqsave(&part->act_lock, irq_flags);
657
Dean Nelson83469b52008-07-29 22:34:18 -0700658 DBUG_ON(part->act_state != XPC_P_AS_INACTIVE);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700659
Dean Nelson83469b52008-07-29 22:34:18 -0700660 part->act_state = XPC_P_AS_ACTIVATION_REQ;
Dean Nelson65c17b82008-05-12 14:02:02 -0700661 XPC_SET_REASON(part, xpCloneKThread, __LINE__);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700662
663 spin_unlock_irqrestore(&part->act_lock, irq_flags);
Robin Holt7c6c6632006-02-02 12:30:21 -0600664
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500665 kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
666 partid);
667 if (IS_ERR(kthread)) {
Robin Holt7c6c6632006-02-02 12:30:21 -0600668 spin_lock_irqsave(&part->act_lock, irq_flags);
Dean Nelson83469b52008-07-29 22:34:18 -0700669 part->act_state = XPC_P_AS_INACTIVE;
Dean Nelson65c17b82008-05-12 14:02:02 -0700670 XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
Robin Holt7c6c6632006-02-02 12:30:21 -0600671 spin_unlock_irqrestore(&part->act_lock, irq_flags);
672 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700673}
674
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700675void
676xpc_activate_kthreads(struct xpc_channel *ch, int needed)
677{
678 int idle = atomic_read(&ch->kthreads_idle);
679 int assigned = atomic_read(&ch->kthreads_assigned);
680 int wakeup;
681
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700682 DBUG_ON(needed <= 0);
683
684 if (idle > 0) {
685 wakeup = (needed > idle) ? idle : needed;
686 needed -= wakeup;
687
688 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
689 "channel=%d\n", wakeup, ch->partid, ch->number);
690
691 /* only wakeup the requested number of kthreads */
692 wake_up_nr(&ch->idle_wq, wakeup);
693 }
694
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500695 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700696 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700697
698 if (needed + assigned > ch->kthreads_assigned_limit) {
699 needed = ch->kthreads_assigned_limit - assigned;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500700 if (needed <= 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700701 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700702 }
703
704 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
705 needed, ch->partid, ch->number);
706
Dean Nelsona460ef82006-11-22 08:25:00 -0600707 xpc_create_kthreads(ch, needed, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700708}
709
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700710/*
711 * This function is where XPC's kthreads wait for messages to deliver.
712 */
713static void
714xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
715{
716 do {
717 /* deliver messages to their intended recipients */
718
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700719 while (xpc_n_of_deliverable_payloads(ch) > 0 &&
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500720 !(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700721 xpc_deliver_payload(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700722 }
723
724 if (atomic_inc_return(&ch->kthreads_idle) >
Dean Nelson35190502008-04-22 14:48:55 -0500725 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700726 /* too many idle kthreads on this channel */
727 atomic_dec(&ch->kthreads_idle);
728 break;
729 }
730
731 dev_dbg(xpc_chan, "idle kthread calling "
732 "wait_event_interruptible_exclusive()\n");
733
Dean Nelson35190502008-04-22 14:48:55 -0500734 (void)wait_event_interruptible_exclusive(ch->idle_wq,
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700735 (xpc_n_of_deliverable_payloads(ch) > 0 ||
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500736 (ch->flags & XPC_C_DISCONNECTING)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700737
738 atomic_dec(&ch->kthreads_idle);
739
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500740 } while (!(ch->flags & XPC_C_DISCONNECTING));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700741}
742
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700743static int
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500744xpc_kthread_start(void *args)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700745{
Dean Nelson64d032b2008-05-12 14:02:03 -0700746 short partid = XPC_UNPACK_ARG1(args);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700747 u16 ch_number = XPC_UNPACK_ARG2(args);
748 struct xpc_partition *part = &xpc_partitions[partid];
749 struct xpc_channel *ch;
750 int n_needed;
Dean Nelsone54af722005-10-25 14:07:43 -0500751 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700752
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700753 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
754 partid, ch_number);
755
756 ch = &part->channels[ch_number];
757
758 if (!(ch->flags & XPC_C_DISCONNECTING)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700759
760 /* let registerer know that connection has been established */
761
Dean Nelsone54af722005-10-25 14:07:43 -0500762 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson4c2cd962006-02-15 08:02:21 -0600763 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
764 ch->flags |= XPC_C_CONNECTEDCALLOUT;
Dean Nelsone54af722005-10-25 14:07:43 -0500765 spin_unlock_irqrestore(&ch->lock, irq_flags);
766
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700767 xpc_connected_callout(ch);
768
Dean Nelson4c2cd962006-02-15 08:02:21 -0600769 spin_lock_irqsave(&ch->lock, irq_flags);
770 ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
771 spin_unlock_irqrestore(&ch->lock, irq_flags);
772
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700773 /*
774 * It is possible that while the callout was being
775 * made that the remote partition sent some messages.
776 * If that is the case, we may need to activate
777 * additional kthreads to help deliver them. We only
778 * need one less than total #of messages to deliver.
779 */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700780 n_needed = xpc_n_of_deliverable_payloads(ch) - 1;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500781 if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700782 xpc_activate_kthreads(ch, n_needed);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500783
Dean Nelsone54af722005-10-25 14:07:43 -0500784 } else {
785 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700786 }
787
788 xpc_kthread_waitmsgs(part, ch);
789 }
790
Dean Nelsona460ef82006-11-22 08:25:00 -0600791 /* let registerer know that connection is disconnecting */
Dean Nelsone54af722005-10-25 14:07:43 -0500792
Dean Nelsona460ef82006-11-22 08:25:00 -0600793 spin_lock_irqsave(&ch->lock, irq_flags);
794 if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500795 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelsona460ef82006-11-22 08:25:00 -0600796 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
Dean Nelson4c2cd962006-02-15 08:02:21 -0600797 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -0600798
Dean Nelson65c17b82008-05-12 14:02:02 -0700799 xpc_disconnect_callout(ch, xpDisconnecting);
Dean Nelsona460ef82006-11-22 08:25:00 -0600800
801 spin_lock_irqsave(&ch->lock, irq_flags);
802 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
803 }
804 spin_unlock_irqrestore(&ch->lock, irq_flags);
805
Dean Nelsona47d5da2008-07-29 22:34:09 -0700806 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
807 atomic_dec_return(&part->nchannels_engaged) == 0) {
808 xpc_indicate_partition_disengaged(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700809 }
810
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700811 xpc_msgqueue_deref(ch);
812
813 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
814 partid, ch_number);
815
816 xpc_part_deref(part);
817 return 0;
818}
819
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700820/*
821 * For each partition that XPC has established communications with, there is
822 * a minimum of one kernel thread assigned to perform any operation that
823 * may potentially sleep or block (basically the callouts to the asynchronous
824 * functions registered via xpc_connect()).
825 *
826 * Additional kthreads are created and destroyed by XPC as the workload
827 * demands.
828 *
829 * A kthread is assigned to one of the active channels that exists for a given
830 * partition.
831 */
832void
Dean Nelsona460ef82006-11-22 08:25:00 -0600833xpc_create_kthreads(struct xpc_channel *ch, int needed,
Dean Nelson35190502008-04-22 14:48:55 -0500834 int ignore_disconnecting)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700835{
836 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700837 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
Dean Nelsona607c382005-09-01 14:01:37 -0500838 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500839 struct task_struct *kthread;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700840
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700841 while (needed-- > 0) {
Dean Nelsone54af722005-10-25 14:07:43 -0500842
843 /*
844 * The following is done on behalf of the newly created
845 * kthread. That kthread is responsible for doing the
846 * counterpart to the following before it exits.
847 */
Dean Nelsona460ef82006-11-22 08:25:00 -0600848 if (ignore_disconnecting) {
849 if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
850 /* kthreads assigned had gone to zero */
851 BUG_ON(!(ch->flags &
Dean Nelson35190502008-04-22 14:48:55 -0500852 XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelsona460ef82006-11-22 08:25:00 -0600853 break;
854 }
855
856 } else if (ch->flags & XPC_C_DISCONNECTING) {
857 break;
858
Dean Nelsona47d5da2008-07-29 22:34:09 -0700859 } else if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
860 atomic_inc_return(&part->nchannels_engaged) == 1) {
861 xpc_indicate_partition_engaged(part);
Dean Nelsona460ef82006-11-22 08:25:00 -0600862 }
Dean Nelson35190502008-04-22 14:48:55 -0500863 (void)xpc_part_ref(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500864 xpc_msgqueue_ref(ch);
Dean Nelsone54af722005-10-25 14:07:43 -0500865
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500866 kthread = kthread_run(xpc_kthread_start, (void *)args,
867 "xpc%02dc%d", ch->partid, ch->number);
868 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700869 /* the fork failed */
Dean Nelsona460ef82006-11-22 08:25:00 -0600870
871 /*
872 * NOTE: if (ignore_disconnecting &&
873 * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
874 * then we'll deadlock if all other kthreads assigned
875 * to this channel are blocked in the channel's
876 * registerer, because the only thing that will unblock
Dean Nelson65c17b82008-05-12 14:02:02 -0700877 * them is the xpDisconnecting callout that this
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500878 * failed kthread_run() would have made.
Dean Nelsona460ef82006-11-22 08:25:00 -0600879 */
880
Dean Nelsone54af722005-10-25 14:07:43 -0500881 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
882 atomic_dec_return(&part->nchannels_engaged) == 0) {
Dean Nelsona47d5da2008-07-29 22:34:09 -0700883 xpc_indicate_partition_disengaged(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500884 }
885 xpc_msgqueue_deref(ch);
886 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700887
888 if (atomic_read(&ch->kthreads_assigned) <
Dean Nelson35190502008-04-22 14:48:55 -0500889 ch->kthreads_idle_limit) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700890 /*
891 * Flag this as an error only if we have an
892 * insufficient #of kthreads for the channel
893 * to function.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700894 */
895 spin_lock_irqsave(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700896 XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
Dean Nelson35190502008-04-22 14:48:55 -0500897 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700898 spin_unlock_irqrestore(&ch->lock, irq_flags);
899 }
900 break;
901 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700902 }
903}
904
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700905void
906xpc_disconnect_wait(int ch_number)
907{
Dean Nelsona607c382005-09-01 14:01:37 -0500908 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700909 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700910 struct xpc_partition *part;
911 struct xpc_channel *ch;
Dean Nelsone54af722005-10-25 14:07:43 -0500912 int wakeup_channel_mgr;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700913
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700914 /* now wait for all callouts to the caller's function to cease */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700915 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700916 part = &xpc_partitions[partid];
917
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500918 if (!xpc_part_ref(part))
Dean Nelsone54af722005-10-25 14:07:43 -0500919 continue;
Dean Nelsone54af722005-10-25 14:07:43 -0500920
921 ch = &part->channels[ch_number];
922
923 if (!(ch->flags & XPC_C_WDISCONNECT)) {
924 xpc_part_deref(part);
925 continue;
926 }
927
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500928 wait_for_completion(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500929
930 spin_lock_irqsave(&ch->lock, irq_flags);
931 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
932 wakeup_channel_mgr = 0;
933
Dean Nelson7fb5e592008-07-29 22:34:10 -0700934 if (ch->delayed_chctl_flags) {
Dean Nelson83469b52008-07-29 22:34:18 -0700935 if (part->act_state != XPC_P_AS_DEACTIVATING) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700936 spin_lock(&part->chctl_lock);
937 part->chctl.flags[ch->number] |=
938 ch->delayed_chctl_flags;
939 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500940 wakeup_channel_mgr = 1;
941 }
Dean Nelson7fb5e592008-07-29 22:34:10 -0700942 ch->delayed_chctl_flags = 0;
Dean Nelsone54af722005-10-25 14:07:43 -0500943 }
944
945 ch->flags &= ~XPC_C_WDISCONNECT;
946 spin_unlock_irqrestore(&ch->lock, irq_flags);
947
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500948 if (wakeup_channel_mgr)
Dean Nelsone54af722005-10-25 14:07:43 -0500949 xpc_wakeup_channel_mgr(part);
Dean Nelsone54af722005-10-25 14:07:43 -0500950
951 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700952 }
953}
954
Dean Nelson5b8669d2008-07-29 22:34:18 -0700955static int
956xpc_setup_partitions(void)
957{
958 short partid;
959 struct xpc_partition *part;
960
961 xpc_partitions = kzalloc(sizeof(struct xpc_partition) *
962 xp_max_npartitions, GFP_KERNEL);
963 if (xpc_partitions == NULL) {
964 dev_err(xpc_part, "can't get memory for partition structure\n");
965 return -ENOMEM;
966 }
967
968 /*
969 * The first few fields of each entry of xpc_partitions[] need to
970 * be initialized now so that calls to xpc_connect() and
971 * xpc_disconnect() can be made prior to the activation of any remote
972 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
973 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
974 * PARTITION HAS BEEN ACTIVATED.
975 */
976 for (partid = 0; partid < xp_max_npartitions; partid++) {
977 part = &xpc_partitions[partid];
978
979 DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
980
981 part->activate_IRQ_rcvd = 0;
982 spin_lock_init(&part->act_lock);
983 part->act_state = XPC_P_AS_INACTIVE;
984 XPC_SET_REASON(part, 0, 0);
985
986 init_timer(&part->disengage_timer);
987 part->disengage_timer.function =
988 xpc_timeout_partition_disengage;
989 part->disengage_timer.data = (unsigned long)part;
990
991 part->setup_state = XPC_P_SS_UNSET;
992 init_waitqueue_head(&part->teardown_wq);
993 atomic_set(&part->references, 0);
994 }
995
996 return xpc_setup_partitions_sn();
997}
998
999static void
1000xpc_teardown_partitions(void)
1001{
Jack Steiner6f2584f2009-04-02 16:59:10 -07001002 xpc_teardown_partitions_sn();
Dean Nelson5b8669d2008-07-29 22:34:18 -07001003 kfree(xpc_partitions);
1004}
1005
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001006static void
Dean Nelson65c17b82008-05-12 14:02:02 -07001007xpc_do_exit(enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001008{
Dean Nelson64d032b2008-05-12 14:02:03 -07001009 short partid;
Dean Nelson1ecaded2006-01-06 09:48:21 -06001010 int active_part_count, printed_waiting_msg = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001011 struct xpc_partition *part;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001012 unsigned long printmsg_time, disengage_timeout = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001013
Dean Nelsona607c382005-09-01 14:01:37 -05001014 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
1015 DBUG_ON(xpc_exiting == 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001016
1017 /*
Dean Nelsona607c382005-09-01 14:01:37 -05001018 * Let the heartbeat checker thread and the discovery thread
1019 * (if one is running) know that they should exit. Also wake up
1020 * the heartbeat checker thread in case it's sleeping.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001021 */
1022 xpc_exiting = 1;
Dean Nelson6e410172008-07-29 22:34:09 -07001023 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001024
Dean Nelsone54af722005-10-25 14:07:43 -05001025 /* wait for the discovery thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001026 wait_for_completion(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001027
Dean Nelsone54af722005-10-25 14:07:43 -05001028 /* wait for the heartbeat checker thread to exit */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001029 wait_for_completion(&xpc_hb_checker_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001030
Dean Nelsona607c382005-09-01 14:01:37 -05001031 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -05001032 (void)msleep_interruptible(300);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001033
1034 /* wait for all partitions to become inactive */
1035
Dean Nelsona47d5da2008-07-29 22:34:09 -07001036 printmsg_time = jiffies + (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
1037 xpc_disengage_timedout = 0;
Dean Nelsona607c382005-09-01 14:01:37 -05001038
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001039 do {
1040 active_part_count = 0;
1041
Dean Nelsonbc63d382008-07-29 22:34:04 -07001042 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001043 part = &xpc_partitions[partid];
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001044
Dean Nelsona607c382005-09-01 14:01:37 -05001045 if (xpc_partition_disengaged(part) &&
Dean Nelson83469b52008-07-29 22:34:18 -07001046 part->act_state == XPC_P_AS_INACTIVE) {
Dean Nelsona607c382005-09-01 14:01:37 -05001047 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001048 }
Dean Nelsona607c382005-09-01 14:01:37 -05001049
1050 active_part_count++;
1051
1052 XPC_DEACTIVATE_PARTITION(part, reason);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001053
Dean Nelsona47d5da2008-07-29 22:34:09 -07001054 if (part->disengage_timeout > disengage_timeout)
1055 disengage_timeout = part->disengage_timeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001056 }
1057
Dean Nelsona47d5da2008-07-29 22:34:09 -07001058 if (xpc_any_partition_engaged()) {
Dean Nelsonaaa3cd62008-07-29 22:34:07 -07001059 if (time_is_before_jiffies(printmsg_time)) {
Dean Nelson1ecaded2006-01-06 09:48:21 -06001060 dev_info(xpc_part, "waiting for remote "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001061 "partitions to deactivate, timeout in "
1062 "%ld seconds\n", (disengage_timeout -
1063 jiffies) / HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001064 printmsg_time = jiffies +
Dean Nelsona47d5da2008-07-29 22:34:09 -07001065 (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001066 printed_waiting_msg = 1;
1067 }
1068
1069 } else if (active_part_count > 0) {
1070 if (printed_waiting_msg) {
1071 dev_info(xpc_part, "waiting for local partition"
Dean Nelsona47d5da2008-07-29 22:34:09 -07001072 " to deactivate\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -06001073 printed_waiting_msg = 0;
1074 }
1075
1076 } else {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001077 if (!xpc_disengage_timedout) {
Dean Nelson1ecaded2006-01-06 09:48:21 -06001078 dev_info(xpc_part, "all partitions have "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001079 "deactivated\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -06001080 }
1081 break;
Dean Nelsona607c382005-09-01 14:01:37 -05001082 }
1083
1084 /* sleep for a 1/3 of a second or so */
Dean Nelson35190502008-04-22 14:48:55 -05001085 (void)msleep_interruptible(300);
Dean Nelsona607c382005-09-01 14:01:37 -05001086
1087 } while (1);
1088
Dean Nelsona47d5da2008-07-29 22:34:09 -07001089 DBUG_ON(xpc_any_partition_engaged());
Dean Nelson33ba3c72008-07-29 22:34:07 -07001090 DBUG_ON(xpc_any_hbs_allowed() != 0);
Dean Nelsona607c382005-09-01 14:01:37 -05001091
Dean Nelson5b8669d2008-07-29 22:34:18 -07001092 xpc_teardown_rsvd_page();
Dean Nelsona607c382005-09-01 14:01:37 -05001093
Dean Nelson65c17b82008-05-12 14:02:02 -07001094 if (reason == xpUnloading) {
Dean Nelson35190502008-04-22 14:48:55 -05001095 (void)unregister_die_notifier(&xpc_die_notifier);
Dean Nelsonbc63d382008-07-29 22:34:04 -07001096 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson0752c672006-01-10 11:07:19 -06001097 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001098
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001099 /* clear the interface to XPC's functions */
1100 xpc_clear_interface();
1101
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001102 if (xpc_sysctl)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001103 unregister_sysctl_table(xpc_sysctl);
Dean Nelson7682a4c2006-08-08 15:03:29 -05001104
Dean Nelson5b8669d2008-07-29 22:34:18 -07001105 xpc_teardown_partitions();
Dean Nelson6e410172008-07-29 22:34:09 -07001106
1107 if (is_shub())
1108 xpc_exit_sn2();
Dean Nelsonb7f7b072008-10-29 14:01:12 -07001109 else if (is_uv())
Dean Nelson6e410172008-07-29 22:34:09 -07001110 xpc_exit_uv();
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001111}
1112
Dean Nelsona607c382005-09-01 14:01:37 -05001113/*
Dean Nelsond6ad0332006-01-10 11:08:55 -06001114 * This function is called when the system is being rebooted.
1115 */
1116static int
1117xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
1118{
Dean Nelson65c17b82008-05-12 14:02:02 -07001119 enum xp_retval reason;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001120
Dean Nelsond6ad0332006-01-10 11:08:55 -06001121 switch (event) {
1122 case SYS_RESTART:
Dean Nelson65c17b82008-05-12 14:02:02 -07001123 reason = xpSystemReboot;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001124 break;
1125 case SYS_HALT:
Dean Nelson65c17b82008-05-12 14:02:02 -07001126 reason = xpSystemHalt;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001127 break;
1128 case SYS_POWER_OFF:
Dean Nelson65c17b82008-05-12 14:02:02 -07001129 reason = xpSystemPoweroff;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001130 break;
1131 default:
Dean Nelson65c17b82008-05-12 14:02:02 -07001132 reason = xpSystemGoingDown;
Dean Nelsond6ad0332006-01-10 11:08:55 -06001133 }
1134
1135 xpc_do_exit(reason);
1136 return NOTIFY_DONE;
1137}
1138
Dean Nelsond6ad0332006-01-10 11:08:55 -06001139/*
Dean Nelsona47d5da2008-07-29 22:34:09 -07001140 * Notify other partitions to deactivate from us by first disengaging from all
1141 * references to our memory.
Dean Nelson780d09e2005-11-09 14:41:57 -06001142 */
1143static void
Dean Nelsona47d5da2008-07-29 22:34:09 -07001144xpc_die_deactivate(void)
Dean Nelson780d09e2005-11-09 14:41:57 -06001145{
1146 struct xpc_partition *part;
Dean Nelson64d032b2008-05-12 14:02:03 -07001147 short partid;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001148 int any_engaged;
Dean Nelson261f3b42008-07-29 22:34:16 -07001149 long keep_waiting;
1150 long wait_to_print;
Dean Nelson780d09e2005-11-09 14:41:57 -06001151
Dean Nelson780d09e2005-11-09 14:41:57 -06001152 /* keep xpc_hb_checker thread from doing anything (just in case) */
1153 xpc_exiting = 1;
1154
Dean Nelson33ba3c72008-07-29 22:34:07 -07001155 xpc_disallow_all_hbs(); /*indicate we're deactivated */
Dean Nelson780d09e2005-11-09 14:41:57 -06001156
Dean Nelsonbc63d382008-07-29 22:34:04 -07001157 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001158 part = &xpc_partitions[partid];
1159
Dean Nelsona47d5da2008-07-29 22:34:09 -07001160 if (xpc_partition_engaged(partid) ||
Dean Nelson83469b52008-07-29 22:34:18 -07001161 part->act_state != XPC_P_AS_INACTIVE) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001162 xpc_request_partition_deactivation(part);
1163 xpc_indicate_partition_disengaged(part);
Dean Nelson780d09e2005-11-09 14:41:57 -06001164 }
1165 }
1166
Dean Nelsona47d5da2008-07-29 22:34:09 -07001167 /*
1168 * Though we requested that all other partitions deactivate from us,
Dean Nelson261f3b42008-07-29 22:34:16 -07001169 * we only wait until they've all disengaged or we've reached the
1170 * defined timelimit.
1171 *
1172 * Given that one iteration through the following while-loop takes
1173 * approximately 200 microseconds, calculate the #of loops to take
1174 * before bailing and the #of loops before printing a waiting message.
Dean Nelsona47d5da2008-07-29 22:34:09 -07001175 */
Dean Nelson261f3b42008-07-29 22:34:16 -07001176 keep_waiting = xpc_disengage_timelimit * 1000 * 5;
1177 wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL * 1000 * 5;
Dean Nelson780d09e2005-11-09 14:41:57 -06001178
Dean Nelson1ecaded2006-01-06 09:48:21 -06001179 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001180 any_engaged = xpc_any_partition_engaged();
1181 if (!any_engaged) {
1182 dev_info(xpc_part, "all partitions have deactivated\n");
Dean Nelson1ecaded2006-01-06 09:48:21 -06001183 break;
1184 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001185
Dean Nelson261f3b42008-07-29 22:34:16 -07001186 if (!keep_waiting--) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001187 for (partid = 0; partid < xp_max_npartitions;
1188 partid++) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001189 if (xpc_partition_engaged(partid)) {
1190 dev_info(xpc_part, "deactivate from "
Dean Nelson35190502008-04-22 14:48:55 -05001191 "remote partition %d timed "
1192 "out\n", partid);
Dean Nelson1ecaded2006-01-06 09:48:21 -06001193 }
1194 }
1195 break;
1196 }
1197
Dean Nelson261f3b42008-07-29 22:34:16 -07001198 if (!wait_to_print--) {
Dean Nelson780d09e2005-11-09 14:41:57 -06001199 dev_info(xpc_part, "waiting for remote partitions to "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001200 "deactivate, timeout in %ld seconds\n",
Dean Nelson261f3b42008-07-29 22:34:16 -07001201 keep_waiting / (1000 * 5));
1202 wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL *
1203 1000 * 5;
Dean Nelson780d09e2005-11-09 14:41:57 -06001204 }
Dean Nelson261f3b42008-07-29 22:34:16 -07001205
1206 udelay(200);
Dean Nelson780d09e2005-11-09 14:41:57 -06001207 }
Dean Nelson780d09e2005-11-09 14:41:57 -06001208}
1209
Dean Nelson780d09e2005-11-09 14:41:57 -06001210/*
Dean Nelson1f4674b2006-01-10 11:08:00 -06001211 * This function is called when the system is being restarted or halted due
1212 * to some sort of system failure. If this is the case we need to notify the
1213 * other partitions to disengage from all references to our memory.
1214 * This function can also be called when our heartbeater could be offlined
1215 * for a time. In this case we need to notify other partitions to not worry
1216 * about the lack of a heartbeat.
Dean Nelson780d09e2005-11-09 14:41:57 -06001217 */
1218static int
1219xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1220{
Dean Nelson261f3b42008-07-29 22:34:16 -07001221#ifdef CONFIG_IA64 /* !!! temporary kludge */
Dean Nelson780d09e2005-11-09 14:41:57 -06001222 switch (event) {
1223 case DIE_MACHINE_RESTART:
1224 case DIE_MACHINE_HALT:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001225 xpc_die_deactivate();
Dean Nelson780d09e2005-11-09 14:41:57 -06001226 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001227
1228 case DIE_KDEBUG_ENTER:
1229 /* Should lack of heartbeat be ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001230 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001231 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001232
Dean Nelson1f4674b2006-01-10 11:08:00 -06001233 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001234 case DIE_MCA_MONARCH_ENTER:
1235 case DIE_INIT_MONARCH_ENTER:
Dean Nelson33ba3c72008-07-29 22:34:07 -07001236 xpc_offline_heartbeat();
Dean Nelson780d09e2005-11-09 14:41:57 -06001237 break;
Dean Nelson1f4674b2006-01-10 11:08:00 -06001238
1239 case DIE_KDEBUG_LEAVE:
1240 /* Is lack of heartbeat being ignored by other partitions? */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001241 if (!xpc_kdebug_ignore)
Dean Nelson1f4674b2006-01-10 11:08:00 -06001242 break;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001243
Dean Nelson1f4674b2006-01-10 11:08:00 -06001244 /* fall through */
Dean Nelson780d09e2005-11-09 14:41:57 -06001245 case DIE_MCA_MONARCH_LEAVE:
1246 case DIE_INIT_MONARCH_LEAVE:
Dean Nelson33ba3c72008-07-29 22:34:07 -07001247 xpc_online_heartbeat();
Dean Nelson780d09e2005-11-09 14:41:57 -06001248 break;
1249 }
Dean Nelson261f3b42008-07-29 22:34:16 -07001250#else
1251 xpc_die_deactivate();
1252#endif
Dean Nelson780d09e2005-11-09 14:41:57 -06001253
1254 return NOTIFY_DONE;
1255}
1256
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001257int __init
1258xpc_init(void)
1259{
1260 int ret;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001261 struct task_struct *kthread;
Dean Nelsonee6665e2008-07-29 22:34:13 -07001262
Kay Sieversbb0dc432009-01-06 10:44:37 -08001263 dev_set_name(xpc_part, "part");
1264 dev_set_name(xpc_chan, "chan");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001265
Dean Nelson94bd2702008-07-29 22:34:05 -07001266 if (is_shub()) {
1267 /*
1268 * The ia64-sn2 architecture supports at most 64 partitions.
Dean Nelsonc39838c2008-07-29 22:34:11 -07001269 * And the inability to unregister remote amos restricts us
Dean Nelson94bd2702008-07-29 22:34:05 -07001270 * further to only support exactly 64 partitions on this
1271 * architecture, no less.
1272 */
Dean Nelson5b8669d2008-07-29 22:34:18 -07001273 if (xp_max_npartitions != 64) {
1274 dev_err(xpc_part, "max #of partitions not set to 64\n");
1275 ret = -EINVAL;
1276 } else {
1277 ret = xpc_init_sn2();
1278 }
Dean Nelson94bd2702008-07-29 22:34:05 -07001279
1280 } else if (is_uv()) {
Dean Nelson5b8669d2008-07-29 22:34:18 -07001281 ret = xpc_init_uv();
Dean Nelson94bd2702008-07-29 22:34:05 -07001282
1283 } else {
Dean Nelson5b8669d2008-07-29 22:34:18 -07001284 ret = -ENODEV;
Dean Nelson94bd2702008-07-29 22:34:05 -07001285 }
Dean Nelson408865c2005-09-08 10:46:58 -05001286
Dean Nelson5b8669d2008-07-29 22:34:18 -07001287 if (ret != 0)
1288 return ret;
1289
1290 ret = xpc_setup_partitions();
1291 if (ret != 0) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001292 dev_err(xpc_part, "can't get memory for partition structure\n");
Dean Nelsonee6665e2008-07-29 22:34:13 -07001293 goto out_1;
Dean Nelsonbc63d382008-07-29 22:34:04 -07001294 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001295
Dean Nelsonbc63d382008-07-29 22:34:04 -07001296 xpc_sysctl = register_sysctl_table(xpc_sys_dir);
1297
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001298 /*
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001299 * Fill the partition reserved page with the information needed by
1300 * other partitions to discover we are alive and establish initial
1301 * communications.
1302 */
Dean Nelson5b8669d2008-07-29 22:34:18 -07001303 ret = xpc_setup_rsvd_page();
1304 if (ret != 0) {
Dean Nelsonbc63d382008-07-29 22:34:04 -07001305 dev_err(xpc_part, "can't setup our reserved page\n");
Dean Nelsonee6665e2008-07-29 22:34:13 -07001306 goto out_2;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001307 }
1308
Dean Nelsona607c382005-09-01 14:01:37 -05001309 /* add ourselves to the reboot_notifier_list */
1310 ret = register_reboot_notifier(&xpc_reboot_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001311 if (ret != 0)
Dean Nelsona607c382005-09-01 14:01:37 -05001312 dev_warn(xpc_part, "can't register reboot notifier\n");
Dean Nelsona607c382005-09-01 14:01:37 -05001313
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07001314 /* add ourselves to the die_notifier list */
Dean Nelson780d09e2005-11-09 14:41:57 -06001315 ret = register_die_notifier(&xpc_die_notifier);
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001316 if (ret != 0)
Dean Nelson780d09e2005-11-09 14:41:57 -06001317 dev_warn(xpc_part, "can't register die notifier\n");
Dean Nelson780d09e2005-11-09 14:41:57 -06001318
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001319 /*
1320 * The real work-horse behind xpc. This processes incoming
1321 * interrupts and monitors remote heartbeats.
1322 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001323 kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
1324 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001325 dev_err(xpc_part, "failed while forking hb check thread\n");
Dean Nelsonbc63d382008-07-29 22:34:04 -07001326 ret = -EBUSY;
Dean Nelsonee6665e2008-07-29 22:34:13 -07001327 goto out_3;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001328 }
1329
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001330 /*
1331 * Startup a thread that will attempt to discover other partitions to
1332 * activate based on info provided by SAL. This new thread is short
1333 * lived and will exit once discovery is complete.
1334 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001335 kthread = kthread_run(xpc_initiate_discovery, NULL,
1336 XPC_DISCOVERY_THREAD_NAME);
1337 if (IS_ERR(kthread)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001338 dev_err(xpc_part, "failed while forking discovery thread\n");
1339
1340 /* mark this new thread as a non-starter */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001341 complete(&xpc_discovery_exited);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001342
Dean Nelson65c17b82008-05-12 14:02:02 -07001343 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001344 return -EBUSY;
1345 }
1346
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001347 /* set the interface to point at XPC's functions */
1348 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001349 xpc_initiate_send, xpc_initiate_send_notify,
1350 xpc_initiate_received, xpc_initiate_partid_to_nasids);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001351
1352 return 0;
Dean Nelsonbc63d382008-07-29 22:34:04 -07001353
1354 /* initialization was not successful */
Dean Nelsonee6665e2008-07-29 22:34:13 -07001355out_3:
Dean Nelson5b8669d2008-07-29 22:34:18 -07001356 xpc_teardown_rsvd_page();
Dean Nelson94bd2702008-07-29 22:34:05 -07001357
Dean Nelsonbc63d382008-07-29 22:34:04 -07001358 (void)unregister_die_notifier(&xpc_die_notifier);
1359 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
Dean Nelsonee6665e2008-07-29 22:34:13 -07001360out_2:
Dean Nelsonbc63d382008-07-29 22:34:04 -07001361 if (xpc_sysctl)
1362 unregister_sysctl_table(xpc_sysctl);
Dean Nelson5b8669d2008-07-29 22:34:18 -07001363
1364 xpc_teardown_partitions();
Dean Nelson6e410172008-07-29 22:34:09 -07001365out_1:
1366 if (is_shub())
1367 xpc_exit_sn2();
Dean Nelsonb7f7b072008-10-29 14:01:12 -07001368 else if (is_uv())
Dean Nelson6e410172008-07-29 22:34:09 -07001369 xpc_exit_uv();
Dean Nelsonbc63d382008-07-29 22:34:04 -07001370 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001371}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001372
Dean Nelson35190502008-04-22 14:48:55 -05001373module_init(xpc_init);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001374
1375void __exit
1376xpc_exit(void)
1377{
Dean Nelson65c17b82008-05-12 14:02:02 -07001378 xpc_do_exit(xpUnloading);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001379}
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001380
Dean Nelson35190502008-04-22 14:48:55 -05001381module_exit(xpc_exit);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001382
1383MODULE_AUTHOR("Silicon Graphics, Inc.");
1384MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1385MODULE_LICENSE("GPL");
1386
1387module_param(xpc_hb_interval, int, 0);
1388MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001389 "heartbeat increments.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001390
1391module_param(xpc_hb_check_interval, int, 0);
1392MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
Dean Nelson35190502008-04-22 14:48:55 -05001393 "heartbeat checks.");
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001394
Dean Nelsona47d5da2008-07-29 22:34:09 -07001395module_param(xpc_disengage_timelimit, int, 0);
1396MODULE_PARM_DESC(xpc_disengage_timelimit, "Number of seconds to wait "
1397 "for disengage to complete.");
Dean Nelsone54af722005-10-25 14:07:43 -05001398
Dean Nelson1f4674b2006-01-10 11:08:00 -06001399module_param(xpc_kdebug_ignore, int, 0);
1400MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
Dean Nelson35190502008-04-22 14:48:55 -05001401 "other partitions when dropping into kdebug.");