- patches.apparmor/remove_suid_new_case_in_2.6.22.diff: Merge fix.
[linux-flexiantxendom0-3.2.10.git] / arch / ia64 / sn / kernel / xpc_main.c
1 /*
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  *
6  * Copyright (c) 2004-2006 Silicon Graphics, Inc.  All Rights Reserved.
7  */
8
9
10 /*
11  * Cross Partition Communication (XPC) support - standard version.
12  *
13  *      XPC provides a message passing capability that crosses partition
14  *      boundaries. This module is made up of two parts:
15  *
16  *          partition   This part detects the presence/absence of other
17  *                      partitions. It provides a heartbeat and monitors
18  *                      the heartbeats of other partitions.
19  *
20  *          channel     This part manages the channels and sends/receives
21  *                      messages across them to/from other partitions.
22  *
23  *      There are a couple of additional functions residing in XP, which
24  *      provide an interface to XPC for its users.
25  *
26  *
27  *      Caveats:
28  *
29  *        . We currently have no way to determine which nasid an IPI came
30  *          from. Thus, xpc_IPI_send() does a remote AMO write followed by
31  *          an IPI. The AMO indicates where data is to be pulled from, so
32  *          after the IPI arrives, the remote partition checks the AMO word.
33  *          The IPI can actually arrive before the AMO however, so other code
34  *          must periodically check for this case. Also, remote AMO operations
35  *          do not reliably time out. Thus we do a remote PIO read solely to
36  *          know whether the remote partition is down and whether we should
37  *          stop sending IPIs to it. This remote PIO read operation is set up
38  *          in a special nofault region so SAL knows to ignore (and cleanup)
39  *          any errors due to the remote AMO write, PIO read, and/or PIO
40  *          write operations.
41  *
42  *          If/when new hardware solves this IPI problem, we should abandon
43  *          the current approach.
44  *
45  */
46
47
48 #include <linux/kernel.h>
49 #include <linux/module.h>
50 #include <linux/init.h>
51 #include <linux/sched.h>
52 #include <linux/syscalls.h>
53 #include <linux/cache.h>
54 #include <linux/interrupt.h>
55 #include <linux/delay.h>
56 #include <linux/reboot.h>
57 #include <linux/completion.h>
58 #include <linux/kdebug.h>
59 #include <asm/sn/intr.h>
60 #include <asm/sn/sn_sal.h>
61 #include <asm/uaccess.h>
62 #include <asm/sn/xpc.h>
63
64
65 /* define two XPC debug device structures to be used with dev_dbg() et al */
66
67 struct device_driver xpc_dbg_name = {
68         .name = "xpc"
69 };
70
71 struct device xpc_part_dbg_subname = {
72         .bus_id = {0},          /* set to "part" at xpc_init() time */
73         .driver = &xpc_dbg_name
74 };
75
76 struct device xpc_chan_dbg_subname = {
77         .bus_id = {0},          /* set to "chan" at xpc_init() time */
78         .driver = &xpc_dbg_name
79 };
80
81 struct device *xpc_part = &xpc_part_dbg_subname;
82 struct device *xpc_chan = &xpc_chan_dbg_subname;
83
84
85 static int xpc_kdebug_ignore;
86 static int xpc_kdebug_entered;
87
88
89 /* systune related variables for /proc/sys directories */
90
91 static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
92 static int xpc_hb_min_interval = 1;
93 static int xpc_hb_max_interval = 10;
94
95 static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
96 static int xpc_hb_check_min_interval = 10;
97 static int xpc_hb_check_max_interval = 120;
98
99 int xpc_disengage_request_timelimit = XPC_DISENGAGE_REQUEST_DEFAULT_TIMELIMIT;
100 static int xpc_disengage_request_min_timelimit = 0;
101 static int xpc_disengage_request_max_timelimit = 120;
102
103 static ctl_table xpc_sys_xpc_hb_dir[] = {
104         {
105                 .ctl_name       = CTL_UNNUMBERED,
106                 .procname       = "hb_interval",
107                 .data           = &xpc_hb_interval,
108                 .maxlen         = sizeof(int),
109                 .mode           = 0644,
110                 .proc_handler   = &proc_dointvec_minmax,
111                 .strategy       = &sysctl_intvec,
112                 .extra1         = &xpc_hb_min_interval,
113                 .extra2         = &xpc_hb_max_interval
114         },
115         {
116                 .ctl_name       = CTL_UNNUMBERED,
117                 .procname       = "hb_check_interval",
118                 .data           = &xpc_hb_check_interval,
119                 .maxlen         = sizeof(int),
120                 .mode           = 0644,
121                 .proc_handler   = &proc_dointvec_minmax,
122                 .strategy       = &sysctl_intvec,
123                 .extra1         = &xpc_hb_check_min_interval,
124                 .extra2         = &xpc_hb_check_max_interval
125         },
126         {}
127 };
128 static ctl_table xpc_sys_xpc_dir[] = {
129         {
130                 .ctl_name       = CTL_UNNUMBERED,
131                 .procname       = "hb",
132                 .mode           = 0555,
133                 .child          = xpc_sys_xpc_hb_dir
134         },
135         {
136                 .ctl_name       = CTL_UNNUMBERED,
137                 .procname       = "disengage_request_timelimit",
138                 .data           = &xpc_disengage_request_timelimit,
139                 .maxlen         = sizeof(int),
140                 .mode           = 0644,
141                 .proc_handler   = &proc_dointvec_minmax,
142                 .strategy       = &sysctl_intvec,
143                 .extra1         = &xpc_disengage_request_min_timelimit,
144                 .extra2         = &xpc_disengage_request_max_timelimit
145         },
146         {}
147 };
148 static ctl_table xpc_sys_dir[] = {
149         {
150                 .ctl_name       = CTL_UNNUMBERED,
151                 .procname       = "xpc",
152                 .mode           = 0555,
153                 .child          = xpc_sys_xpc_dir
154         },
155         {}
156 };
157 static struct ctl_table_header *xpc_sysctl;
158
159 /* non-zero if any remote partition disengage request was timed out */
160 int xpc_disengage_request_timedout;
161
162 /* #of IRQs received */
163 static atomic_t xpc_act_IRQ_rcvd;
164
165 /* IRQ handler notifies this wait queue on receipt of an IRQ */
166 static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
167
168 static unsigned long xpc_hb_check_timeout;
169
170 /* notification that the xpc_hb_checker thread has exited */
171 static DECLARE_COMPLETION(xpc_hb_checker_exited);
172
173 /* notification that the xpc_discovery thread has exited */
174 static DECLARE_COMPLETION(xpc_discovery_exited);
175
176
177 static struct timer_list xpc_hb_timer;
178
179
180 static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
181
182
183 static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
184 static struct notifier_block xpc_reboot_notifier = {
185         .notifier_call = xpc_system_reboot,
186 };
187
188 static int xpc_system_die(struct notifier_block *, unsigned long, void *);
189 static struct notifier_block xpc_die_notifier = {
190         .notifier_call = xpc_system_die,
191 };
192
193
194 /*
195  * Timer function to enforce the timelimit on the partition disengage request.
196  */
197 static void
198 xpc_timeout_partition_disengage_request(unsigned long data)
199 {
200         struct xpc_partition *part = (struct xpc_partition *) data;
201
202
203         DBUG_ON(jiffies < part->disengage_request_timeout);
204
205         (void) xpc_partition_disengaged(part);
206
207         DBUG_ON(part->disengage_request_timeout != 0);
208         DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
209 }
210
211
212 /*
213  * Notify the heartbeat check thread that an IRQ has been received.
214  */
215 static irqreturn_t
216 xpc_act_IRQ_handler(int irq, void *dev_id)
217 {
218         atomic_inc(&xpc_act_IRQ_rcvd);
219         wake_up_interruptible(&xpc_act_IRQ_wq);
220         return IRQ_HANDLED;
221 }
222
223
224 /*
225  * Timer to produce the heartbeat.  The timer structures function is
226  * already set when this is initially called.  A tunable is used to
227  * specify when the next timeout should occur.
228  */
229 static void
230 xpc_hb_beater(unsigned long dummy)
231 {
232         xpc_vars->heartbeat++;
233
234         if (jiffies >= xpc_hb_check_timeout) {
235                 wake_up_interruptible(&xpc_act_IRQ_wq);
236         }
237
238         xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
239         add_timer(&xpc_hb_timer);
240 }
241
242
243 /*
244  * This thread is responsible for nearly all of the partition
245  * activation/deactivation.
246  */
247 static int
248 xpc_hb_checker(void *ignore)
249 {
250         int last_IRQ_count = 0;
251         int new_IRQ_count;
252         int force_IRQ=0;
253
254
255         /* this thread was marked active by xpc_hb_init() */
256
257         daemonize(XPC_HB_CHECK_THREAD_NAME);
258
259         set_cpus_allowed(current, cpumask_of_cpu(XPC_HB_CHECK_CPU));
260
261         xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
262
263         while (!(volatile int) xpc_exiting) {
264
265                 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
266                         "been received\n",
267                         (int) (xpc_hb_check_timeout - jiffies),
268                         atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
269
270
271                 /* checking of remote heartbeats is skewed by IRQ handling */
272                 if (jiffies >= xpc_hb_check_timeout) {
273                         dev_dbg(xpc_part, "checking remote heartbeats\n");
274                         xpc_check_remote_hb();
275
276                         /*
277                          * We need to periodically recheck to ensure no
278                          * IPI/AMO pairs have been missed.  That check
279                          * must always reset xpc_hb_check_timeout.
280                          */
281                         force_IRQ = 1;
282                 }
283
284
285                 /* check for outstanding IRQs */
286                 new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
287                 if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
288                         force_IRQ = 0;
289
290                         dev_dbg(xpc_part, "found an IRQ to process; will be "
291                                 "resetting xpc_hb_check_timeout\n");
292
293                         last_IRQ_count += xpc_identify_act_IRQ_sender();
294                         if (last_IRQ_count < new_IRQ_count) {
295                                 /* retry once to help avoid missing AMO */
296                                 (void) xpc_identify_act_IRQ_sender();
297                         }
298                         last_IRQ_count = new_IRQ_count;
299
300                         xpc_hb_check_timeout = jiffies +
301                                            (xpc_hb_check_interval * HZ);
302                 }
303
304                 /* wait for IRQ or timeout */
305                 (void) wait_event_interruptible(xpc_act_IRQ_wq,
306                             (last_IRQ_count < atomic_read(&xpc_act_IRQ_rcvd) ||
307                                         jiffies >= xpc_hb_check_timeout ||
308                                                 (volatile int) xpc_exiting));
309         }
310
311         dev_dbg(xpc_part, "heartbeat checker is exiting\n");
312
313
314         /* mark this thread as having exited */
315         complete(&xpc_hb_checker_exited);
316         return 0;
317 }
318
319
320 /*
321  * This thread will attempt to discover other partitions to activate
322  * based on info provided by SAL. This new thread is short lived and
323  * will exit once discovery is complete.
324  */
325 static int
326 xpc_initiate_discovery(void *ignore)
327 {
328         daemonize(XPC_DISCOVERY_THREAD_NAME);
329
330         xpc_discovery();
331
332         dev_dbg(xpc_part, "discovery thread is exiting\n");
333
334         /* mark this thread as having exited */
335         complete(&xpc_discovery_exited);
336         return 0;
337 }
338
339
340 /*
341  * Establish first contact with the remote partititon. This involves pulling
342  * the XPC per partition variables from the remote partition and waiting for
343  * the remote partition to pull ours.
344  */
345 static enum xpc_retval
346 xpc_make_first_contact(struct xpc_partition *part)
347 {
348         enum xpc_retval ret;
349
350
351         while ((ret = xpc_pull_remote_vars_part(part)) != xpcSuccess) {
352                 if (ret != xpcRetry) {
353                         XPC_DEACTIVATE_PARTITION(part, ret);
354                         return ret;
355                 }
356
357                 dev_dbg(xpc_chan, "waiting to make first contact with "
358                         "partition %d\n", XPC_PARTID(part));
359
360                 /* wait a 1/4 of a second or so */
361                 (void) msleep_interruptible(250);
362
363                 if (part->act_state == XPC_P_DEACTIVATING) {
364                         return part->reason;
365                 }
366         }
367
368         return xpc_mark_partition_active(part);
369 }
370
371
372 /*
373  * The first kthread assigned to a newly activated partition is the one
374  * created by XPC HB with which it calls xpc_partition_up(). XPC hangs on to
375  * that kthread until the partition is brought down, at which time that kthread
376  * returns back to XPC HB. (The return of that kthread will signify to XPC HB
377  * that XPC has dismantled all communication infrastructure for the associated
378  * partition.) This kthread becomes the channel manager for that partition.
379  *
380  * Each active partition has a channel manager, who, besides connecting and
381  * disconnecting channels, will ensure that each of the partition's connected
382  * channels has the required number of assigned kthreads to get the work done.
383  */
384 static void
385 xpc_channel_mgr(struct xpc_partition *part)
386 {
387         while (part->act_state != XPC_P_DEACTIVATING ||
388                         atomic_read(&part->nchannels_active) > 0 ||
389                                         !xpc_partition_disengaged(part)) {
390
391                 xpc_process_channel_activity(part);
392
393
394                 /*
395                  * Wait until we've been requested to activate kthreads or
396                  * all of the channel's message queues have been torn down or
397                  * a signal is pending.
398                  *
399                  * The channel_mgr_requests is set to 1 after being awakened,
400                  * This is done to prevent the channel mgr from making one pass
401                  * through the loop for each request, since he will
402                  * be servicing all the requests in one pass. The reason it's
403                  * set to 1 instead of 0 is so that other kthreads will know
404                  * that the channel mgr is running and won't bother trying to
405                  * wake him up.
406                  */
407                 atomic_dec(&part->channel_mgr_requests);
408                 (void) wait_event_interruptible(part->channel_mgr_wq,
409                                 (atomic_read(&part->channel_mgr_requests) > 0 ||
410                                 (volatile u64) part->local_IPI_amo != 0 ||
411                                 ((volatile u8) part->act_state ==
412                                                         XPC_P_DEACTIVATING &&
413                                 atomic_read(&part->nchannels_active) == 0 &&
414                                 xpc_partition_disengaged(part))));
415                 atomic_set(&part->channel_mgr_requests, 1);
416
417                 // >>> Does it need to wakeup periodically as well? In case we
418                 // >>> miscalculated the #of kthreads to wakeup or create?
419         }
420 }
421
422
423 /*
424  * When XPC HB determines that a partition has come up, it will create a new
425  * kthread and that kthread will call this function to attempt to set up the
426  * basic infrastructure used for Cross Partition Communication with the newly
427  * upped partition.
428  *
429  * The kthread that was created by XPC HB and which setup the XPC
430  * infrastructure will remain assigned to the partition until the partition
431  * goes down. At which time the kthread will teardown the XPC infrastructure
432  * and then exit.
433  *
434  * XPC HB will put the remote partition's XPC per partition specific variables
435  * physical address into xpc_partitions[partid].remote_vars_part_pa prior to
436  * calling xpc_partition_up().
437  */
438 static void
439 xpc_partition_up(struct xpc_partition *part)
440 {
441         DBUG_ON(part->channels != NULL);
442
443         dev_dbg(xpc_chan, "activating partition %d\n", XPC_PARTID(part));
444
445         if (xpc_setup_infrastructure(part) != xpcSuccess) {
446                 return;
447         }
448
449         /*
450          * The kthread that XPC HB called us with will become the
451          * channel manager for this partition. It will not return
452          * back to XPC HB until the partition's XPC infrastructure
453          * has been dismantled.
454          */
455
456         (void) xpc_part_ref(part);      /* this will always succeed */
457
458         if (xpc_make_first_contact(part) == xpcSuccess) {
459                 xpc_channel_mgr(part);
460         }
461
462         xpc_part_deref(part);
463
464         xpc_teardown_infrastructure(part);
465 }
466
467
468 static int
469 xpc_activating(void *__partid)
470 {
471         partid_t partid = (u64) __partid;
472         struct xpc_partition *part = &xpc_partitions[partid];
473         unsigned long irq_flags;
474         struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
475         int ret;
476
477
478         DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
479
480         spin_lock_irqsave(&part->act_lock, irq_flags);
481
482         if (part->act_state == XPC_P_DEACTIVATING) {
483                 part->act_state = XPC_P_INACTIVE;
484                 spin_unlock_irqrestore(&part->act_lock, irq_flags);
485                 part->remote_rp_pa = 0;
486                 return 0;
487         }
488
489         /* indicate the thread is activating */
490         DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
491         part->act_state = XPC_P_ACTIVATING;
492
493         XPC_SET_REASON(part, 0, 0);
494         spin_unlock_irqrestore(&part->act_lock, irq_flags);
495
496         dev_dbg(xpc_part, "bringing partition %d up\n", partid);
497
498         daemonize("xpc%02d", partid);
499
500         /*
501          * This thread needs to run at a realtime priority to prevent a
502          * significant performance degradation.
503          */
504         ret = sched_setscheduler(current, SCHED_FIFO, &param);
505         if (ret != 0) {
506                 dev_warn(xpc_part, "unable to set pid %d to a realtime "
507                         "priority, ret=%d\n", current->pid, ret);
508         }
509
510         /* allow this thread and its children to run on any CPU */
511         set_cpus_allowed(current, CPU_MASK_ALL);
512
513         /*
514          * Register the remote partition's AMOs with SAL so it can handle
515          * and cleanup errors within that address range should the remote
516          * partition go down. We don't unregister this range because it is
517          * difficult to tell when outstanding writes to the remote partition
518          * are finished and thus when it is safe to unregister. This should
519          * not result in wasted space in the SAL xp_addr_region table because
520          * we should get the same page for remote_amos_page_pa after module
521          * reloads and system reboots.
522          */
523         if (sn_register_xp_addr_region(part->remote_amos_page_pa,
524                                                         PAGE_SIZE, 1) < 0) {
525                 dev_warn(xpc_part, "xpc_partition_up(%d) failed to register "
526                         "xp_addr region\n", partid);
527
528                 spin_lock_irqsave(&part->act_lock, irq_flags);
529                 part->act_state = XPC_P_INACTIVE;
530                 XPC_SET_REASON(part, xpcPhysAddrRegFailed, __LINE__);
531                 spin_unlock_irqrestore(&part->act_lock, irq_flags);
532                 part->remote_rp_pa = 0;
533                 return 0;
534         }
535
536         xpc_allow_hb(partid, xpc_vars);
537         xpc_IPI_send_activated(part);
538
539
540         /*
541          * xpc_partition_up() holds this thread and marks this partition as
542          * XPC_P_ACTIVE by calling xpc_hb_mark_active().
543          */
544         (void) xpc_partition_up(part);
545
546         xpc_disallow_hb(partid, xpc_vars);
547         xpc_mark_partition_inactive(part);
548
549         if (part->reason == xpcReactivating) {
550                 /* interrupting ourselves results in activating partition */
551                 xpc_IPI_send_reactivate(part);
552         }
553
554         return 0;
555 }
556
557
558 void
559 xpc_activate_partition(struct xpc_partition *part)
560 {
561         partid_t partid = XPC_PARTID(part);
562         unsigned long irq_flags;
563         pid_t pid;
564
565
566         spin_lock_irqsave(&part->act_lock, irq_flags);
567
568         DBUG_ON(part->act_state != XPC_P_INACTIVE);
569
570         part->act_state = XPC_P_ACTIVATION_REQ;
571         XPC_SET_REASON(part, xpcCloneKThread, __LINE__);
572
573         spin_unlock_irqrestore(&part->act_lock, irq_flags);
574
575         pid = kernel_thread(xpc_activating, (void *) ((u64) partid), 0);
576
577         if (unlikely(pid <= 0)) {
578                 spin_lock_irqsave(&part->act_lock, irq_flags);
579                 part->act_state = XPC_P_INACTIVE;
580                 XPC_SET_REASON(part, xpcCloneKThreadFailed, __LINE__);
581                 spin_unlock_irqrestore(&part->act_lock, irq_flags);
582         }
583 }
584
585
586 /*
587  * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
588  * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
589  * than one partition, we use an AMO_t structure per partition to indicate
590  * whether a partition has sent an IPI or not.  >>> If it has, then wake up the
591  * associated kthread to handle it.
592  *
593  * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
594  * running on other partitions.
595  *
596  * Noteworthy Arguments:
597  *
598  *      irq - Interrupt ReQuest number. NOT USED.
599  *
600  *      dev_id - partid of IPI's potential sender.
601  */
602 irqreturn_t
603 xpc_notify_IRQ_handler(int irq, void *dev_id)
604 {
605         partid_t partid = (partid_t) (u64) dev_id;
606         struct xpc_partition *part = &xpc_partitions[partid];
607
608
609         DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
610
611         if (xpc_part_ref(part)) {
612                 xpc_check_for_channel_activity(part);
613
614                 xpc_part_deref(part);
615         }
616         return IRQ_HANDLED;
617 }
618
619
620 /*
621  * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
622  * because the write to their associated IPI amo completed after the IRQ/IPI
623  * was received.
624  */
625 void
626 xpc_dropped_IPI_check(struct xpc_partition *part)
627 {
628         if (xpc_part_ref(part)) {
629                 xpc_check_for_channel_activity(part);
630
631                 part->dropped_IPI_timer.expires = jiffies +
632                                                         XPC_P_DROPPED_IPI_WAIT;
633                 add_timer(&part->dropped_IPI_timer);
634                 xpc_part_deref(part);
635         }
636 }
637
638
639 void
640 xpc_activate_kthreads(struct xpc_channel *ch, int needed)
641 {
642         int idle = atomic_read(&ch->kthreads_idle);
643         int assigned = atomic_read(&ch->kthreads_assigned);
644         int wakeup;
645
646
647         DBUG_ON(needed <= 0);
648
649         if (idle > 0) {
650                 wakeup = (needed > idle) ? idle : needed;
651                 needed -= wakeup;
652
653                 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
654                         "channel=%d\n", wakeup, ch->partid, ch->number);
655
656                 /* only wakeup the requested number of kthreads */
657                 wake_up_nr(&ch->idle_wq, wakeup);
658         }
659
660         if (needed <= 0) {
661                 return;
662         }
663
664         if (needed + assigned > ch->kthreads_assigned_limit) {
665                 needed = ch->kthreads_assigned_limit - assigned;
666                 // >>>should never be less than 0
667                 if (needed <= 0) {
668                         return;
669                 }
670         }
671
672         dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
673                 needed, ch->partid, ch->number);
674
675         xpc_create_kthreads(ch, needed, 0);
676 }
677
678
679 /*
680  * This function is where XPC's kthreads wait for messages to deliver.
681  */
682 static void
683 xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
684 {
685         do {
686                 /* deliver messages to their intended recipients */
687
688                 while ((volatile s64) ch->w_local_GP.get <
689                                 (volatile s64) ch->w_remote_GP.put &&
690                                         !((volatile u32) ch->flags &
691                                                 XPC_C_DISCONNECTING)) {
692                         xpc_deliver_msg(ch);
693                 }
694
695                 if (atomic_inc_return(&ch->kthreads_idle) >
696                                                 ch->kthreads_idle_limit) {
697                         /* too many idle kthreads on this channel */
698                         atomic_dec(&ch->kthreads_idle);
699                         break;
700                 }
701
702                 dev_dbg(xpc_chan, "idle kthread calling "
703                         "wait_event_interruptible_exclusive()\n");
704
705                 (void) wait_event_interruptible_exclusive(ch->idle_wq,
706                                 ((volatile s64) ch->w_local_GP.get <
707                                         (volatile s64) ch->w_remote_GP.put ||
708                                 ((volatile u32) ch->flags &
709                                                 XPC_C_DISCONNECTING)));
710
711                 atomic_dec(&ch->kthreads_idle);
712
713         } while (!((volatile u32) ch->flags & XPC_C_DISCONNECTING));
714 }
715
716
717 static int
718 xpc_daemonize_kthread(void *args)
719 {
720         partid_t partid = XPC_UNPACK_ARG1(args);
721         u16 ch_number = XPC_UNPACK_ARG2(args);
722         struct xpc_partition *part = &xpc_partitions[partid];
723         struct xpc_channel *ch;
724         int n_needed;
725         unsigned long irq_flags;
726
727
728         daemonize("xpc%02dc%d", partid, ch_number);
729
730         dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
731                 partid, ch_number);
732
733         ch = &part->channels[ch_number];
734
735         if (!(ch->flags & XPC_C_DISCONNECTING)) {
736
737                 /* let registerer know that connection has been established */
738
739                 spin_lock_irqsave(&ch->lock, irq_flags);
740                 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
741                         ch->flags |= XPC_C_CONNECTEDCALLOUT;
742                         spin_unlock_irqrestore(&ch->lock, irq_flags);
743
744                         xpc_connected_callout(ch);
745
746                         spin_lock_irqsave(&ch->lock, irq_flags);
747                         ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
748                         spin_unlock_irqrestore(&ch->lock, irq_flags);
749
750                         /*
751                          * It is possible that while the callout was being
752                          * made that the remote partition sent some messages.
753                          * If that is the case, we may need to activate
754                          * additional kthreads to help deliver them. We only
755                          * need one less than total #of messages to deliver.
756                          */
757                         n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
758                         if (n_needed > 0 &&
759                                         !(ch->flags & XPC_C_DISCONNECTING)) {
760                                 xpc_activate_kthreads(ch, n_needed);
761                         }
762                 } else {
763                         spin_unlock_irqrestore(&ch->lock, irq_flags);
764                 }
765
766                 xpc_kthread_waitmsgs(part, ch);
767         }
768
769         /* let registerer know that connection is disconnecting */
770
771         spin_lock_irqsave(&ch->lock, irq_flags);
772         if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
773                         !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
774                 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
775                 spin_unlock_irqrestore(&ch->lock, irq_flags);
776
777                 xpc_disconnect_callout(ch, xpcDisconnecting);
778
779                 spin_lock_irqsave(&ch->lock, irq_flags);
780                 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
781         }
782         spin_unlock_irqrestore(&ch->lock, irq_flags);
783
784         if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
785                 if (atomic_dec_return(&part->nchannels_engaged) == 0) {
786                         xpc_mark_partition_disengaged(part);
787                         xpc_IPI_send_disengage(part);
788                 }
789         }
790
791         xpc_msgqueue_deref(ch);
792
793         dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
794                 partid, ch_number);
795
796         xpc_part_deref(part);
797         return 0;
798 }
799
800
801 /*
802  * For each partition that XPC has established communications with, there is
803  * a minimum of one kernel thread assigned to perform any operation that
804  * may potentially sleep or block (basically the callouts to the asynchronous
805  * functions registered via xpc_connect()).
806  *
807  * Additional kthreads are created and destroyed by XPC as the workload
808  * demands.
809  *
810  * A kthread is assigned to one of the active channels that exists for a given
811  * partition.
812  */
813 void
814 xpc_create_kthreads(struct xpc_channel *ch, int needed,
815                         int ignore_disconnecting)
816 {
817         unsigned long irq_flags;
818         pid_t pid;
819         u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
820         struct xpc_partition *part = &xpc_partitions[ch->partid];
821
822
823         while (needed-- > 0) {
824
825                 /*
826                  * The following is done on behalf of the newly created
827                  * kthread. That kthread is responsible for doing the
828                  * counterpart to the following before it exits.
829                  */
830                 if (ignore_disconnecting) {
831                         if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
832                                 /* kthreads assigned had gone to zero */
833                                 BUG_ON(!(ch->flags &
834                                         XPC_C_DISCONNECTINGCALLOUT_MADE));
835                                 break;
836                         }
837
838                 } else if (ch->flags & XPC_C_DISCONNECTING) {
839                         break;
840
841                 } else if (atomic_inc_return(&ch->kthreads_assigned) == 1) {
842                         if (atomic_inc_return(&part->nchannels_engaged) == 1)
843                                 xpc_mark_partition_engaged(part);
844                 }
845                 (void) xpc_part_ref(part);
846                 xpc_msgqueue_ref(ch);
847
848                 pid = kernel_thread(xpc_daemonize_kthread, (void *) args, 0);
849                 if (pid < 0) {
850                         /* the fork failed */
851
852                         /*
853                          * NOTE: if (ignore_disconnecting &&
854                          * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
855                          * then we'll deadlock if all other kthreads assigned
856                          * to this channel are blocked in the channel's
857                          * registerer, because the only thing that will unblock
858                          * them is the xpcDisconnecting callout that this
859                          * failed kernel_thread would have made.
860                          */
861
862                         if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
863                             atomic_dec_return(&part->nchannels_engaged) == 0) {
864                                 xpc_mark_partition_disengaged(part);
865                                 xpc_IPI_send_disengage(part);
866                         }
867                         xpc_msgqueue_deref(ch);
868                         xpc_part_deref(part);
869
870                         if (atomic_read(&ch->kthreads_assigned) <
871                                                 ch->kthreads_idle_limit) {
872                                 /*
873                                  * Flag this as an error only if we have an
874                                  * insufficient #of kthreads for the channel
875                                  * to function.
876                                  */
877                                 spin_lock_irqsave(&ch->lock, irq_flags);
878                                 XPC_DISCONNECT_CHANNEL(ch, xpcLackOfResources,
879                                                                 &irq_flags);
880                                 spin_unlock_irqrestore(&ch->lock, irq_flags);
881                         }
882                         break;
883                 }
884
885                 ch->kthreads_created++; // >>> temporary debug only!!!
886         }
887 }
888
889
890 void
891 xpc_disconnect_wait(int ch_number)
892 {
893         unsigned long irq_flags;
894         partid_t partid;
895         struct xpc_partition *part;
896         struct xpc_channel *ch;
897         int wakeup_channel_mgr;
898
899
900         /* now wait for all callouts to the caller's function to cease */
901         for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
902                 part = &xpc_partitions[partid];
903
904                 if (!xpc_part_ref(part)) {
905                         continue;
906                 }
907
908                 ch = &part->channels[ch_number];
909
910                 if (!(ch->flags & XPC_C_WDISCONNECT)) {
911                         xpc_part_deref(part);
912                         continue;
913                 }
914
915                 wait_for_completion(&ch->wdisconnect_wait);
916
917                 spin_lock_irqsave(&ch->lock, irq_flags);
918                 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
919                 wakeup_channel_mgr = 0;
920
921                 if (ch->delayed_IPI_flags) {
922                         if (part->act_state != XPC_P_DEACTIVATING) {
923                                 spin_lock(&part->IPI_lock);
924                                 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
925                                         ch->number, ch->delayed_IPI_flags);
926                                 spin_unlock(&part->IPI_lock);
927                                 wakeup_channel_mgr = 1;
928                         }
929                         ch->delayed_IPI_flags = 0;
930                 }
931
932                 ch->flags &= ~XPC_C_WDISCONNECT;
933                 spin_unlock_irqrestore(&ch->lock, irq_flags);
934
935                 if (wakeup_channel_mgr) {
936                         xpc_wakeup_channel_mgr(part);
937                 }
938
939                 xpc_part_deref(part);
940         }
941 }
942
943
944 static void
945 xpc_do_exit(enum xpc_retval reason)
946 {
947         partid_t partid;
948         int active_part_count, printed_waiting_msg = 0;
949         struct xpc_partition *part;
950         unsigned long printmsg_time, disengage_request_timeout = 0;
951
952
953         /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
954         DBUG_ON(xpc_exiting == 1);
955
956         /*
957          * Let the heartbeat checker thread and the discovery thread
958          * (if one is running) know that they should exit. Also wake up
959          * the heartbeat checker thread in case it's sleeping.
960          */
961         xpc_exiting = 1;
962         wake_up_interruptible(&xpc_act_IRQ_wq);
963
964         /* ignore all incoming interrupts */
965         free_irq(SGI_XPC_ACTIVATE, NULL);
966
967         /* wait for the discovery thread to exit */
968         wait_for_completion(&xpc_discovery_exited);
969
970         /* wait for the heartbeat checker thread to exit */
971         wait_for_completion(&xpc_hb_checker_exited);
972
973
974         /* sleep for a 1/3 of a second or so */
975         (void) msleep_interruptible(300);
976
977
978         /* wait for all partitions to become inactive */
979
980         printmsg_time = jiffies + (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
981         xpc_disengage_request_timedout = 0;
982
983         do {
984                 active_part_count = 0;
985
986                 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
987                         part = &xpc_partitions[partid];
988
989                         if (xpc_partition_disengaged(part) &&
990                                         part->act_state == XPC_P_INACTIVE) {
991                                 continue;
992                         }
993
994                         active_part_count++;
995
996                         XPC_DEACTIVATE_PARTITION(part, reason);
997
998                         if (part->disengage_request_timeout >
999                                                 disengage_request_timeout) {
1000                                 disengage_request_timeout =
1001                                                 part->disengage_request_timeout;
1002                         }
1003                 }
1004
1005                 if (xpc_partition_engaged(-1UL)) {
1006                         if (time_after(jiffies, printmsg_time)) {
1007                                 dev_info(xpc_part, "waiting for remote "
1008                                         "partitions to disengage, timeout in "
1009                                         "%ld seconds\n",
1010                                         (disengage_request_timeout - jiffies)
1011                                                                         / HZ);
1012                                 printmsg_time = jiffies +
1013                                         (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
1014                                 printed_waiting_msg = 1;
1015                         }
1016
1017                 } else if (active_part_count > 0) {
1018                         if (printed_waiting_msg) {
1019                                 dev_info(xpc_part, "waiting for local partition"
1020                                         " to disengage\n");
1021                                 printed_waiting_msg = 0;
1022                         }
1023
1024                 } else {
1025                         if (!xpc_disengage_request_timedout) {
1026                                 dev_info(xpc_part, "all partitions have "
1027                                         "disengaged\n");
1028                         }
1029                         break;
1030                 }
1031
1032                 /* sleep for a 1/3 of a second or so */
1033                 (void) msleep_interruptible(300);
1034
1035         } while (1);
1036
1037         DBUG_ON(xpc_partition_engaged(-1UL));
1038
1039
1040         /* indicate to others that our reserved page is uninitialized */
1041         xpc_rsvd_page->vars_pa = 0;
1042
1043         /* now it's time to eliminate our heartbeat */
1044         del_timer_sync(&xpc_hb_timer);
1045         DBUG_ON(xpc_vars->heartbeating_to_mask != 0);
1046
1047         if (reason == xpcUnloading) {
1048                 /* take ourselves off of the reboot_notifier_list */
1049                 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
1050
1051                 /* take ourselves off of the die_notifier list */
1052                 (void) unregister_die_notifier(&xpc_die_notifier);
1053         }
1054
1055         /* close down protections for IPI operations */
1056         xpc_restrict_IPI_ops();
1057
1058
1059         /* clear the interface to XPC's functions */
1060         xpc_clear_interface();
1061
1062         if (xpc_sysctl) {
1063                 unregister_sysctl_table(xpc_sysctl);
1064         }
1065
1066         kfree(xpc_remote_copy_buffer_base);
1067 }
1068
1069
1070 /*
1071  * This function is called when the system is being rebooted.
1072  */
1073 static int
1074 xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
1075 {
1076         enum xpc_retval reason;
1077
1078
1079         switch (event) {
1080         case SYS_RESTART:
1081                 reason = xpcSystemReboot;
1082                 break;
1083         case SYS_HALT:
1084                 reason = xpcSystemHalt;
1085                 break;
1086         case SYS_POWER_OFF:
1087                 reason = xpcSystemPoweroff;
1088                 break;
1089         default:
1090                 reason = xpcSystemGoingDown;
1091         }
1092
1093         xpc_do_exit(reason);
1094         return NOTIFY_DONE;
1095 }
1096
1097
1098 /*
1099  * Notify other partitions to disengage from all references to our memory.
1100  */
1101 static void
1102 xpc_die_disengage(void)
1103 {
1104         struct xpc_partition *part;
1105         partid_t partid;
1106         unsigned long engaged;
1107         long time, printmsg_time, disengage_request_timeout;
1108
1109
1110         /* keep xpc_hb_checker thread from doing anything (just in case) */
1111         xpc_exiting = 1;
1112
1113         xpc_vars->heartbeating_to_mask = 0;  /* indicate we're deactivated */
1114
1115         for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1116                 part = &xpc_partitions[partid];
1117
1118                 if (!XPC_SUPPORTS_DISENGAGE_REQUEST(part->
1119                                                         remote_vars_version)) {
1120
1121                         /* just in case it was left set by an earlier XPC */
1122                         xpc_clear_partition_engaged(1UL << partid);
1123                         continue;
1124                 }
1125
1126                 if (xpc_partition_engaged(1UL << partid) ||
1127                                         part->act_state != XPC_P_INACTIVE) {
1128                         xpc_request_partition_disengage(part);
1129                         xpc_mark_partition_disengaged(part);
1130                         xpc_IPI_send_disengage(part);
1131                 }
1132         }
1133
1134         time = rtc_time();
1135         printmsg_time = time +
1136                 (XPC_DISENGAGE_PRINTMSG_INTERVAL * sn_rtc_cycles_per_second);
1137         disengage_request_timeout = time +
1138                 (xpc_disengage_request_timelimit * sn_rtc_cycles_per_second);
1139
1140         /* wait for all other partitions to disengage from us */
1141
1142         while (1) {
1143                 engaged = xpc_partition_engaged(-1UL);
1144                 if (!engaged) {
1145                         dev_info(xpc_part, "all partitions have disengaged\n");
1146                         break;
1147                 }
1148
1149                 time = rtc_time();
1150                 if (time >= disengage_request_timeout) {
1151                         for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1152                                 if (engaged & (1UL << partid)) {
1153                                         dev_info(xpc_part, "disengage from "
1154                                                 "remote partition %d timed "
1155                                                 "out\n", partid);
1156                                 }
1157                         }
1158                         break;
1159                 }
1160
1161                 if (time >= printmsg_time) {
1162                         dev_info(xpc_part, "waiting for remote partitions to "
1163                                 "disengage, timeout in %ld seconds\n",
1164                                 (disengage_request_timeout - time) /
1165                                                 sn_rtc_cycles_per_second);
1166                         printmsg_time = time +
1167                                         (XPC_DISENGAGE_PRINTMSG_INTERVAL *
1168                                                 sn_rtc_cycles_per_second);
1169                 }
1170         }
1171 }
1172
1173
1174 /*
1175  * This function is called when the system is being restarted or halted due
1176  * to some sort of system failure. If this is the case we need to notify the
1177  * other partitions to disengage from all references to our memory.
1178  * This function can also be called when our heartbeater could be offlined
1179  * for a time. In this case we need to notify other partitions to not worry
1180  * about the lack of a heartbeat.
1181  */
1182 static int
1183 xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1184 {
1185         switch (event) {
1186         case DIE_MACHINE_RESTART:
1187         case DIE_MACHINE_HALT:
1188                 xpc_die_disengage();
1189                 break;
1190
1191         case DIE_KDEBUG_ENTER:
1192                 xpc_kdebug_entered = 1;
1193
1194                 /* Should lack of heartbeat be ignored by other partitions? */
1195                 if (!xpc_kdebug_ignore) {
1196                         break;
1197                 }
1198                 /* fall through */
1199         case DIE_MCA_MONARCH_ENTER:
1200         case DIE_INIT_MONARCH_ENTER:
1201                 xpc_vars->heartbeat++;
1202                 xpc_vars->heartbeat_offline = 1;
1203                 break;
1204
1205         case DIE_KDEBUG_LEAVE:
1206                 xpc_kdebug_entered = 0;
1207
1208                 /* Is lack of heartbeat being ignored by other partitions? */
1209                 if (!xpc_kdebug_ignore) {
1210                         break;
1211                 }
1212                 /* fall through */
1213         case DIE_MCA_MONARCH_LEAVE:
1214         case DIE_INIT_MONARCH_LEAVE:
1215                 xpc_vars->heartbeat++;
1216                 xpc_vars->heartbeat_offline = 0;
1217                 break;
1218         }
1219
1220         return NOTIFY_DONE;
1221 }
1222
1223
1224 int
1225 xpc_kdebug_force_disengage(void)
1226 {
1227         if (!xpc_kdebug_entered) {
1228                 /* not being called from kdebug */
1229                 return 1;
1230         }
1231
1232         xpc_vars->heartbeat_offline = 0;
1233         xpc_die_disengage();
1234         return 0;
1235 }
1236 EXPORT_SYMBOL_GPL(xpc_kdebug_force_disengage);
1237
1238
1239 int __init
1240 xpc_init(void)
1241 {
1242         int ret;
1243         partid_t partid;
1244         struct xpc_partition *part;
1245         pid_t pid;
1246         size_t buf_size;
1247
1248
1249         if (!ia64_platform_is("sn2")) {
1250                 return -ENODEV;
1251         }
1252
1253
1254         buf_size = max(XPC_RP_VARS_SIZE,
1255                                 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES);
1256         xpc_remote_copy_buffer = xpc_kmalloc_cacheline_aligned(buf_size,
1257                                      GFP_KERNEL, &xpc_remote_copy_buffer_base);
1258         if (xpc_remote_copy_buffer == NULL)
1259                 return -ENOMEM;
1260
1261         snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1262         snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
1263
1264         xpc_sysctl = register_sysctl_table(xpc_sys_dir);
1265
1266         /*
1267          * The first few fields of each entry of xpc_partitions[] need to
1268          * be initialized now so that calls to xpc_connect() and
1269          * xpc_disconnect() can be made prior to the activation of any remote
1270          * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
1271          * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
1272          * PARTITION HAS BEEN ACTIVATED.
1273          */
1274         for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1275                 part = &xpc_partitions[partid];
1276
1277                 DBUG_ON((u64) part != L1_CACHE_ALIGN((u64) part));
1278
1279                 part->act_IRQ_rcvd = 0;
1280                 spin_lock_init(&part->act_lock);
1281                 part->act_state = XPC_P_INACTIVE;
1282                 XPC_SET_REASON(part, 0, 0);
1283
1284                 init_timer(&part->disengage_request_timer);
1285                 part->disengage_request_timer.function =
1286                                 xpc_timeout_partition_disengage_request;
1287                 part->disengage_request_timer.data = (unsigned long) part;
1288
1289                 part->setup_state = XPC_P_UNSET;
1290                 init_waitqueue_head(&part->teardown_wq);
1291                 atomic_set(&part->references, 0);
1292         }
1293
1294         /*
1295          * Open up protections for IPI operations (and AMO operations on
1296          * Shub 1.1 systems).
1297          */
1298         xpc_allow_IPI_ops();
1299
1300         /*
1301          * Interrupts being processed will increment this atomic variable and
1302          * awaken the heartbeat thread which will process the interrupts.
1303          */
1304         atomic_set(&xpc_act_IRQ_rcvd, 0);
1305
1306         /*
1307          * This is safe to do before the xpc_hb_checker thread has started
1308          * because the handler releases a wait queue.  If an interrupt is
1309          * received before the thread is waiting, it will not go to sleep,
1310          * but rather immediately process the interrupt.
1311          */
1312         ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
1313                                                         "xpc hb", NULL);
1314         if (ret != 0) {
1315                 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
1316                         "errno=%d\n", -ret);
1317
1318                 xpc_restrict_IPI_ops();
1319
1320                 if (xpc_sysctl) {
1321                         unregister_sysctl_table(xpc_sysctl);
1322                 }
1323
1324                 kfree(xpc_remote_copy_buffer_base);
1325                 return -EBUSY;
1326         }
1327
1328         /*
1329          * Fill the partition reserved page with the information needed by
1330          * other partitions to discover we are alive and establish initial
1331          * communications.
1332          */
1333         xpc_rsvd_page = xpc_rsvd_page_init();
1334         if (xpc_rsvd_page == NULL) {
1335                 dev_err(xpc_part, "could not setup our reserved page\n");
1336
1337                 free_irq(SGI_XPC_ACTIVATE, NULL);
1338                 xpc_restrict_IPI_ops();
1339
1340                 if (xpc_sysctl) {
1341                         unregister_sysctl_table(xpc_sysctl);
1342                 }
1343
1344                 kfree(xpc_remote_copy_buffer_base);
1345                 return -EBUSY;
1346         }
1347
1348
1349         /* add ourselves to the reboot_notifier_list */
1350         ret = register_reboot_notifier(&xpc_reboot_notifier);
1351         if (ret != 0) {
1352                 dev_warn(xpc_part, "can't register reboot notifier\n");
1353         }
1354
1355         /* add ourselves to the die_notifier list */
1356         ret = register_die_notifier(&xpc_die_notifier);
1357         if (ret != 0) {
1358                 dev_warn(xpc_part, "can't register die notifier\n");
1359         }
1360
1361
1362         /*
1363          * Set the beating to other partitions into motion.  This is
1364          * the last requirement for other partitions' discovery to
1365          * initiate communications with us.
1366          */
1367         init_timer(&xpc_hb_timer);
1368         xpc_hb_timer.function = xpc_hb_beater;
1369         xpc_hb_beater(0);
1370
1371
1372         /*
1373          * The real work-horse behind xpc.  This processes incoming
1374          * interrupts and monitors remote heartbeats.
1375          */
1376         pid = kernel_thread(xpc_hb_checker, NULL, 0);
1377         if (pid < 0) {
1378                 dev_err(xpc_part, "failed while forking hb check thread\n");
1379
1380                 /* indicate to others that our reserved page is uninitialized */
1381                 xpc_rsvd_page->vars_pa = 0;
1382
1383                 /* take ourselves off of the reboot_notifier_list */
1384                 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
1385
1386                 /* take ourselves off of the die_notifier list */
1387                 (void) unregister_die_notifier(&xpc_die_notifier);
1388
1389                 del_timer_sync(&xpc_hb_timer);
1390                 free_irq(SGI_XPC_ACTIVATE, NULL);
1391                 xpc_restrict_IPI_ops();
1392
1393                 if (xpc_sysctl) {
1394                         unregister_sysctl_table(xpc_sysctl);
1395                 }
1396
1397                 kfree(xpc_remote_copy_buffer_base);
1398                 return -EBUSY;
1399         }
1400
1401
1402         /*
1403          * Startup a thread that will attempt to discover other partitions to
1404          * activate based on info provided by SAL. This new thread is short
1405          * lived and will exit once discovery is complete.
1406          */
1407         pid = kernel_thread(xpc_initiate_discovery, NULL, 0);
1408         if (pid < 0) {
1409                 dev_err(xpc_part, "failed while forking discovery thread\n");
1410
1411                 /* mark this new thread as a non-starter */
1412                 complete(&xpc_discovery_exited);
1413
1414                 xpc_do_exit(xpcUnloading);
1415                 return -EBUSY;
1416         }
1417
1418
1419         /* set the interface to point at XPC's functions */
1420         xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
1421                           xpc_initiate_allocate, xpc_initiate_send,
1422                           xpc_initiate_send_notify, xpc_initiate_received,
1423                           xpc_initiate_partid_to_nasids);
1424
1425         return 0;
1426 }
1427 module_init(xpc_init);
1428
1429
1430 void __exit
1431 xpc_exit(void)
1432 {
1433         xpc_do_exit(xpcUnloading);
1434 }
1435 module_exit(xpc_exit);
1436
1437
1438 MODULE_AUTHOR("Silicon Graphics, Inc.");
1439 MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1440 MODULE_LICENSE("GPL");
1441
1442 module_param(xpc_hb_interval, int, 0);
1443 MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
1444                 "heartbeat increments.");
1445
1446 module_param(xpc_hb_check_interval, int, 0);
1447 MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
1448                 "heartbeat checks.");
1449
1450 module_param(xpc_disengage_request_timelimit, int, 0);
1451 MODULE_PARM_DESC(xpc_disengage_request_timelimit, "Number of seconds to wait "
1452                 "for disengage request to complete.");
1453
1454 module_param(xpc_kdebug_ignore, int, 0);
1455 MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
1456                 "other partitions when dropping into kdebug.");
1457