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