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