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
6 * Copyright (c) 2004-2006 Silicon Graphics, Inc. All Rights Reserved.
11 * Cross Partition Communication (XPC) support - standard version.
13 * XPC provides a message passing capability that crosses partition
14 * boundaries. This module is made up of two parts:
16 * partition This part detects the presence/absence of other
17 * partitions. It provides a heartbeat and monitors
18 * the heartbeats of other partitions.
20 * channel This part manages the channels and sends/receives
21 * messages across them to/from other partitions.
23 * There are a couple of additional functions residing in XP, which
24 * provide an interface to XPC for its users.
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
42 * If/when new hardware solves this IPI problem, we should abandon
43 * the current approach.
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/slab.h>
56 #include <linux/delay.h>
57 #include <linux/reboot.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>
65 /* define two XPC debug device structures to be used with dev_dbg() et al */
67 struct device_driver xpc_dbg_name = {
71 struct device xpc_part_dbg_subname = {
72 .bus_id = {0}, /* set to "part" at xpc_init() time */
73 .driver = &xpc_dbg_name
76 struct device xpc_chan_dbg_subname = {
77 .bus_id = {0}, /* set to "chan" at xpc_init() time */
78 .driver = &xpc_dbg_name
81 struct device *xpc_part = &xpc_part_dbg_subname;
82 struct device *xpc_chan = &xpc_chan_dbg_subname;
85 static int xpc_kdebug_ignore;
88 /* systune related variables for /proc/sys directories */
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;
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;
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;
102 static ctl_table xpc_sys_xpc_hb_dir[] = {
110 &proc_dointvec_minmax,
113 &xpc_hb_min_interval,
119 &xpc_hb_check_interval,
123 &proc_dointvec_minmax,
126 &xpc_hb_check_min_interval,
127 &xpc_hb_check_max_interval
131 static ctl_table xpc_sys_xpc_dir[] = {
142 "disengage_request_timelimit",
143 &xpc_disengage_request_timelimit,
147 &proc_dointvec_minmax,
150 &xpc_disengage_request_min_timelimit,
151 &xpc_disengage_request_max_timelimit
155 static ctl_table xpc_sys_dir[] = {
166 static struct ctl_table_header *xpc_sysctl;
168 /* non-zero if any remote partition disengage request was timed out */
169 int xpc_disengage_request_timedout;
171 /* #of IRQs received */
172 static atomic_t xpc_act_IRQ_rcvd;
174 /* IRQ handler notifies this wait queue on receipt of an IRQ */
175 static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
177 static unsigned long xpc_hb_check_timeout;
179 /* notification that the xpc_hb_checker thread has exited */
180 static DECLARE_MUTEX_LOCKED(xpc_hb_checker_exited);
182 /* notification that the xpc_discovery thread has exited */
183 static DECLARE_MUTEX_LOCKED(xpc_discovery_exited);
186 static struct timer_list xpc_hb_timer;
189 static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
192 static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
193 static struct notifier_block xpc_reboot_notifier = {
194 .notifier_call = xpc_system_reboot,
197 static int xpc_system_die(struct notifier_block *, unsigned long, void *);
198 static struct notifier_block xpc_die_notifier = {
199 .notifier_call = xpc_system_die,
204 * Timer function to enforce the timelimit on the partition disengage request.
207 xpc_timeout_partition_disengage_request(unsigned long data)
209 struct xpc_partition *part = (struct xpc_partition *) data;
212 DBUG_ON(jiffies < part->disengage_request_timeout);
214 (void) xpc_partition_disengaged(part);
216 DBUG_ON(part->disengage_request_timeout != 0);
217 DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
222 * Notify the heartbeat check thread that an IRQ has been received.
225 xpc_act_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
227 atomic_inc(&xpc_act_IRQ_rcvd);
228 wake_up_interruptible(&xpc_act_IRQ_wq);
234 * Timer to produce the heartbeat. The timer structures function is
235 * already set when this is initially called. A tunable is used to
236 * specify when the next timeout should occur.
239 xpc_hb_beater(unsigned long dummy)
241 xpc_vars->heartbeat++;
243 if (jiffies >= xpc_hb_check_timeout) {
244 wake_up_interruptible(&xpc_act_IRQ_wq);
247 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
248 add_timer(&xpc_hb_timer);
253 * This thread is responsible for nearly all of the partition
254 * activation/deactivation.
257 xpc_hb_checker(void *ignore)
259 int last_IRQ_count = 0;
264 /* this thread was marked active by xpc_hb_init() */
266 daemonize(XPC_HB_CHECK_THREAD_NAME);
268 set_cpus_allowed(current, cpumask_of_cpu(XPC_HB_CHECK_CPU));
270 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
272 while (!(volatile int) xpc_exiting) {
274 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
276 (int) (xpc_hb_check_timeout - jiffies),
277 atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
280 /* checking of remote heartbeats is skewed by IRQ handling */
281 if (jiffies >= xpc_hb_check_timeout) {
282 dev_dbg(xpc_part, "checking remote heartbeats\n");
283 xpc_check_remote_hb();
286 * We need to periodically recheck to ensure no
287 * IPI/AMO pairs have been missed. That check
288 * must always reset xpc_hb_check_timeout.
294 /* check for outstanding IRQs */
295 new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
296 if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
299 dev_dbg(xpc_part, "found an IRQ to process; will be "
300 "resetting xpc_hb_check_timeout\n");
302 last_IRQ_count += xpc_identify_act_IRQ_sender();
303 if (last_IRQ_count < new_IRQ_count) {
304 /* retry once to help avoid missing AMO */
305 (void) xpc_identify_act_IRQ_sender();
307 last_IRQ_count = new_IRQ_count;
309 xpc_hb_check_timeout = jiffies +
310 (xpc_hb_check_interval * HZ);
313 /* wait for IRQ or timeout */
314 (void) wait_event_interruptible(xpc_act_IRQ_wq,
315 (last_IRQ_count < atomic_read(&xpc_act_IRQ_rcvd) ||
316 jiffies >= xpc_hb_check_timeout ||
317 (volatile int) xpc_exiting));
320 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
323 /* mark this thread as having exited */
324 up(&xpc_hb_checker_exited);
330 * This thread will attempt to discover other partitions to activate
331 * based on info provided by SAL. This new thread is short lived and
332 * will exit once discovery is complete.
335 xpc_initiate_discovery(void *ignore)
337 daemonize(XPC_DISCOVERY_THREAD_NAME);
341 dev_dbg(xpc_part, "discovery thread is exiting\n");
343 /* mark this thread as having exited */
344 up(&xpc_discovery_exited);
350 * Establish first contact with the remote partititon. This involves pulling
351 * the XPC per partition variables from the remote partition and waiting for
352 * the remote partition to pull ours.
354 static enum xpc_retval
355 xpc_make_first_contact(struct xpc_partition *part)
360 while ((ret = xpc_pull_remote_vars_part(part)) != xpcSuccess) {
361 if (ret != xpcRetry) {
362 XPC_DEACTIVATE_PARTITION(part, ret);
366 dev_dbg(xpc_chan, "waiting to make first contact with "
367 "partition %d\n", XPC_PARTID(part));
369 /* wait a 1/4 of a second or so */
370 (void) msleep_interruptible(250);
372 if (part->act_state == XPC_P_DEACTIVATING) {
377 return xpc_mark_partition_active(part);
382 * The first kthread assigned to a newly activated partition is the one
383 * created by XPC HB with which it calls xpc_partition_up(). XPC hangs on to
384 * that kthread until the partition is brought down, at which time that kthread
385 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
386 * that XPC has dismantled all communication infrastructure for the associated
387 * partition.) This kthread becomes the channel manager for that partition.
389 * Each active partition has a channel manager, who, besides connecting and
390 * disconnecting channels, will ensure that each of the partition's connected
391 * channels has the required number of assigned kthreads to get the work done.
394 xpc_channel_mgr(struct xpc_partition *part)
396 while (part->act_state != XPC_P_DEACTIVATING ||
397 atomic_read(&part->nchannels_active) > 0 ||
398 !xpc_partition_disengaged(part)) {
400 xpc_process_channel_activity(part);
404 * Wait until we've been requested to activate kthreads or
405 * all of the channel's message queues have been torn down or
406 * a signal is pending.
408 * The channel_mgr_requests is set to 1 after being awakened,
409 * This is done to prevent the channel mgr from making one pass
410 * through the loop for each request, since he will
411 * be servicing all the requests in one pass. The reason it's
412 * set to 1 instead of 0 is so that other kthreads will know
413 * that the channel mgr is running and won't bother trying to
416 atomic_dec(&part->channel_mgr_requests);
417 (void) wait_event_interruptible(part->channel_mgr_wq,
418 (atomic_read(&part->channel_mgr_requests) > 0 ||
419 (volatile u64) part->local_IPI_amo != 0 ||
420 ((volatile u8) part->act_state ==
421 XPC_P_DEACTIVATING &&
422 atomic_read(&part->nchannels_active) == 0 &&
423 xpc_partition_disengaged(part))));
424 atomic_set(&part->channel_mgr_requests, 1);
426 // >>> Does it need to wakeup periodically as well? In case we
427 // >>> miscalculated the #of kthreads to wakeup or create?
433 * When XPC HB determines that a partition has come up, it will create a new
434 * kthread and that kthread will call this function to attempt to set up the
435 * basic infrastructure used for Cross Partition Communication with the newly
438 * The kthread that was created by XPC HB and which setup the XPC
439 * infrastructure will remain assigned to the partition until the partition
440 * goes down. At which time the kthread will teardown the XPC infrastructure
443 * XPC HB will put the remote partition's XPC per partition specific variables
444 * physical address into xpc_partitions[partid].remote_vars_part_pa prior to
445 * calling xpc_partition_up().
448 xpc_partition_up(struct xpc_partition *part)
450 DBUG_ON(part->channels != NULL);
452 dev_dbg(xpc_chan, "activating partition %d\n", XPC_PARTID(part));
454 if (xpc_setup_infrastructure(part) != xpcSuccess) {
459 * The kthread that XPC HB called us with will become the
460 * channel manager for this partition. It will not return
461 * back to XPC HB until the partition's XPC infrastructure
462 * has been dismantled.
465 (void) xpc_part_ref(part); /* this will always succeed */
467 if (xpc_make_first_contact(part) == xpcSuccess) {
468 xpc_channel_mgr(part);
471 xpc_part_deref(part);
473 xpc_teardown_infrastructure(part);
478 xpc_activating(void *__partid)
480 partid_t partid = (u64) __partid;
481 struct xpc_partition *part = &xpc_partitions[partid];
482 unsigned long irq_flags;
483 struct sched_param param = { sched_priority: MAX_RT_PRIO - 1 };
487 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
489 spin_lock_irqsave(&part->act_lock, irq_flags);
491 if (part->act_state == XPC_P_DEACTIVATING) {
492 part->act_state = XPC_P_INACTIVE;
493 spin_unlock_irqrestore(&part->act_lock, irq_flags);
494 part->remote_rp_pa = 0;
498 /* indicate the thread is activating */
499 DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
500 part->act_state = XPC_P_ACTIVATING;
502 XPC_SET_REASON(part, 0, 0);
503 spin_unlock_irqrestore(&part->act_lock, irq_flags);
505 dev_dbg(xpc_part, "bringing partition %d up\n", partid);
507 daemonize("xpc%02d", partid);
510 * This thread needs to run at a realtime priority to prevent a
511 * significant performance degradation.
513 ret = sched_setscheduler(current, SCHED_FIFO, ¶m);
515 dev_warn(xpc_part, "unable to set pid %d to a realtime "
516 "priority, ret=%d\n", current->pid, ret);
519 /* allow this thread and its children to run on any CPU */
520 set_cpus_allowed(current, CPU_MASK_ALL);
523 * Register the remote partition's AMOs with SAL so it can handle
524 * and cleanup errors within that address range should the remote
525 * partition go down. We don't unregister this range because it is
526 * difficult to tell when outstanding writes to the remote partition
527 * are finished and thus when it is safe to unregister. This should
528 * not result in wasted space in the SAL xp_addr_region table because
529 * we should get the same page for remote_amos_page_pa after module
530 * reloads and system reboots.
532 if (sn_register_xp_addr_region(part->remote_amos_page_pa,
534 dev_warn(xpc_part, "xpc_partition_up(%d) failed to register "
535 "xp_addr region\n", partid);
537 spin_lock_irqsave(&part->act_lock, irq_flags);
538 part->act_state = XPC_P_INACTIVE;
539 XPC_SET_REASON(part, xpcPhysAddrRegFailed, __LINE__);
540 spin_unlock_irqrestore(&part->act_lock, irq_flags);
541 part->remote_rp_pa = 0;
545 xpc_allow_hb(partid, xpc_vars);
546 xpc_IPI_send_activated(part);
550 * xpc_partition_up() holds this thread and marks this partition as
551 * XPC_P_ACTIVE by calling xpc_hb_mark_active().
553 (void) xpc_partition_up(part);
555 xpc_disallow_hb(partid, xpc_vars);
556 xpc_mark_partition_inactive(part);
558 if (part->reason == xpcReactivating) {
559 /* interrupting ourselves results in activating partition */
560 xpc_IPI_send_reactivate(part);
568 xpc_activate_partition(struct xpc_partition *part)
570 partid_t partid = XPC_PARTID(part);
571 unsigned long irq_flags;
575 spin_lock_irqsave(&part->act_lock, irq_flags);
577 pid = kernel_thread(xpc_activating, (void *) ((u64) partid), 0);
579 DBUG_ON(part->act_state != XPC_P_INACTIVE);
582 part->act_state = XPC_P_ACTIVATION_REQ;
583 XPC_SET_REASON(part, xpcCloneKThread, __LINE__);
585 XPC_SET_REASON(part, xpcCloneKThreadFailed, __LINE__);
588 spin_unlock_irqrestore(&part->act_lock, irq_flags);
593 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
594 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
595 * than one partition, we use an AMO_t structure per partition to indicate
596 * whether a partition has sent an IPI or not. >>> If it has, then wake up the
597 * associated kthread to handle it.
599 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
600 * running on other partitions.
602 * Noteworthy Arguments:
604 * irq - Interrupt ReQuest number. NOT USED.
606 * dev_id - partid of IPI's potential sender.
608 * regs - processor's context before the processor entered
609 * interrupt code. NOT USED.
612 xpc_notify_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
614 partid_t partid = (partid_t) (u64) dev_id;
615 struct xpc_partition *part = &xpc_partitions[partid];
618 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
620 if (xpc_part_ref(part)) {
621 xpc_check_for_channel_activity(part);
623 xpc_part_deref(part);
630 * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
631 * because the write to their associated IPI amo completed after the IRQ/IPI
635 xpc_dropped_IPI_check(struct xpc_partition *part)
637 if (xpc_part_ref(part)) {
638 xpc_check_for_channel_activity(part);
640 part->dropped_IPI_timer.expires = jiffies +
641 XPC_P_DROPPED_IPI_WAIT;
642 add_timer(&part->dropped_IPI_timer);
643 xpc_part_deref(part);
649 xpc_activate_kthreads(struct xpc_channel *ch, int needed)
651 int idle = atomic_read(&ch->kthreads_idle);
652 int assigned = atomic_read(&ch->kthreads_assigned);
656 DBUG_ON(needed <= 0);
659 wakeup = (needed > idle) ? idle : needed;
662 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
663 "channel=%d\n", wakeup, ch->partid, ch->number);
665 /* only wakeup the requested number of kthreads */
666 wake_up_nr(&ch->idle_wq, wakeup);
673 if (needed + assigned > ch->kthreads_assigned_limit) {
674 needed = ch->kthreads_assigned_limit - assigned;
675 // >>>should never be less than 0
681 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
682 needed, ch->partid, ch->number);
684 xpc_create_kthreads(ch, needed);
689 * This function is where XPC's kthreads wait for messages to deliver.
692 xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
695 /* deliver messages to their intended recipients */
697 while ((volatile s64) ch->w_local_GP.get <
698 (volatile s64) ch->w_remote_GP.put &&
699 !((volatile u32) ch->flags &
700 XPC_C_DISCONNECTING)) {
704 if (atomic_inc_return(&ch->kthreads_idle) >
705 ch->kthreads_idle_limit) {
706 /* too many idle kthreads on this channel */
707 atomic_dec(&ch->kthreads_idle);
711 dev_dbg(xpc_chan, "idle kthread calling "
712 "wait_event_interruptible_exclusive()\n");
714 (void) wait_event_interruptible_exclusive(ch->idle_wq,
715 ((volatile s64) ch->w_local_GP.get <
716 (volatile s64) ch->w_remote_GP.put ||
717 ((volatile u32) ch->flags &
718 XPC_C_DISCONNECTING)));
720 atomic_dec(&ch->kthreads_idle);
722 } while (!((volatile u32) ch->flags & XPC_C_DISCONNECTING));
727 xpc_daemonize_kthread(void *args)
729 partid_t partid = XPC_UNPACK_ARG1(args);
730 u16 ch_number = XPC_UNPACK_ARG2(args);
731 struct xpc_partition *part = &xpc_partitions[partid];
732 struct xpc_channel *ch;
734 unsigned long irq_flags;
737 daemonize("xpc%02dc%d", partid, ch_number);
739 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
742 ch = &part->channels[ch_number];
744 if (!(ch->flags & XPC_C_DISCONNECTING)) {
746 /* let registerer know that connection has been established */
748 spin_lock_irqsave(&ch->lock, irq_flags);
749 if (!(ch->flags & XPC_C_CONNECTCALLOUT)) {
750 ch->flags |= XPC_C_CONNECTCALLOUT;
751 spin_unlock_irqrestore(&ch->lock, irq_flags);
753 xpc_connected_callout(ch);
756 * It is possible that while the callout was being
757 * made that the remote partition sent some messages.
758 * If that is the case, we may need to activate
759 * additional kthreads to help deliver them. We only
760 * need one less than total #of messages to deliver.
762 n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
764 !(ch->flags & XPC_C_DISCONNECTING)) {
765 xpc_activate_kthreads(ch, n_needed);
768 spin_unlock_irqrestore(&ch->lock, irq_flags);
771 xpc_kthread_waitmsgs(part, ch);
774 if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
775 spin_lock_irqsave(&ch->lock, irq_flags);
776 if ((ch->flags & XPC_C_CONNECTCALLOUT) &&
777 !(ch->flags & XPC_C_DISCONNECTCALLOUT)) {
778 ch->flags |= XPC_C_DISCONNECTCALLOUT;
779 spin_unlock_irqrestore(&ch->lock, irq_flags);
781 xpc_disconnect_callout(ch, xpcDisconnecting);
783 spin_unlock_irqrestore(&ch->lock, irq_flags);
785 if (atomic_dec_return(&part->nchannels_engaged) == 0) {
786 xpc_mark_partition_disengaged(part);
787 xpc_IPI_send_disengage(part);
792 xpc_msgqueue_deref(ch);
794 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
797 xpc_part_deref(part);
803 * For each partition that XPC has established communications with, there is
804 * a minimum of one kernel thread assigned to perform any operation that
805 * may potentially sleep or block (basically the callouts to the asynchronous
806 * functions registered via xpc_connect()).
808 * Additional kthreads are created and destroyed by XPC as the workload
811 * A kthread is assigned to one of the active channels that exists for a given
815 xpc_create_kthreads(struct xpc_channel *ch, int needed)
817 unsigned long irq_flags;
819 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
820 struct xpc_partition *part = &xpc_partitions[ch->partid];
823 while (needed-- > 0) {
826 * The following is done on behalf of the newly created
827 * kthread. That kthread is responsible for doing the
828 * counterpart to the following before it exits.
830 (void) xpc_part_ref(part);
831 xpc_msgqueue_ref(ch);
832 if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
833 atomic_inc_return(&part->nchannels_engaged) == 1) {
834 xpc_mark_partition_engaged(part);
837 pid = kernel_thread(xpc_daemonize_kthread, (void *) args, 0);
839 /* the fork failed */
840 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
841 atomic_dec_return(&part->nchannels_engaged) == 0) {
842 xpc_mark_partition_disengaged(part);
843 xpc_IPI_send_disengage(part);
845 xpc_msgqueue_deref(ch);
846 xpc_part_deref(part);
848 if (atomic_read(&ch->kthreads_assigned) <
849 ch->kthreads_idle_limit) {
851 * Flag this as an error only if we have an
852 * insufficient #of kthreads for the channel
855 * No xpc_msgqueue_ref() is needed here since
856 * the channel mgr is doing this.
858 spin_lock_irqsave(&ch->lock, irq_flags);
859 XPC_DISCONNECT_CHANNEL(ch, xpcLackOfResources,
861 spin_unlock_irqrestore(&ch->lock, irq_flags);
866 ch->kthreads_created++; // >>> temporary debug only!!!
872 xpc_disconnect_wait(int ch_number)
874 unsigned long irq_flags;
876 struct xpc_partition *part;
877 struct xpc_channel *ch;
878 int wakeup_channel_mgr;
881 /* now wait for all callouts to the caller's function to cease */
882 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
883 part = &xpc_partitions[partid];
885 if (!xpc_part_ref(part)) {
889 ch = &part->channels[ch_number];
891 if (!(ch->flags & XPC_C_WDISCONNECT)) {
892 xpc_part_deref(part);
896 (void) down(&ch->wdisconnect_sema);
898 spin_lock_irqsave(&ch->lock, irq_flags);
899 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
900 wakeup_channel_mgr = 0;
902 if (ch->delayed_IPI_flags) {
903 if (part->act_state != XPC_P_DEACTIVATING) {
904 spin_lock(&part->IPI_lock);
905 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
906 ch->number, ch->delayed_IPI_flags);
907 spin_unlock(&part->IPI_lock);
908 wakeup_channel_mgr = 1;
910 ch->delayed_IPI_flags = 0;
913 ch->flags &= ~XPC_C_WDISCONNECT;
914 spin_unlock_irqrestore(&ch->lock, irq_flags);
916 if (wakeup_channel_mgr) {
917 xpc_wakeup_channel_mgr(part);
920 xpc_part_deref(part);
926 xpc_do_exit(enum xpc_retval reason)
929 int active_part_count, printed_waiting_msg = 0;
930 struct xpc_partition *part;
931 unsigned long printmsg_time, disengage_request_timeout = 0;
934 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
935 DBUG_ON(xpc_exiting == 1);
938 * Let the heartbeat checker thread and the discovery thread
939 * (if one is running) know that they should exit. Also wake up
940 * the heartbeat checker thread in case it's sleeping.
943 wake_up_interruptible(&xpc_act_IRQ_wq);
945 /* ignore all incoming interrupts */
946 free_irq(SGI_XPC_ACTIVATE, NULL);
948 /* wait for the discovery thread to exit */
949 down(&xpc_discovery_exited);
951 /* wait for the heartbeat checker thread to exit */
952 down(&xpc_hb_checker_exited);
955 /* sleep for a 1/3 of a second or so */
956 (void) msleep_interruptible(300);
959 /* wait for all partitions to become inactive */
961 printmsg_time = jiffies + (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
962 xpc_disengage_request_timedout = 0;
965 active_part_count = 0;
967 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
968 part = &xpc_partitions[partid];
970 if (xpc_partition_disengaged(part) &&
971 part->act_state == XPC_P_INACTIVE) {
977 XPC_DEACTIVATE_PARTITION(part, reason);
979 if (part->disengage_request_timeout >
980 disengage_request_timeout) {
981 disengage_request_timeout =
982 part->disengage_request_timeout;
986 if (xpc_partition_engaged(-1UL)) {
987 if (time_after(jiffies, printmsg_time)) {
988 dev_info(xpc_part, "waiting for remote "
989 "partitions to disengage, timeout in "
991 (disengage_request_timeout - jiffies)
993 printmsg_time = jiffies +
994 (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
995 printed_waiting_msg = 1;
998 } else if (active_part_count > 0) {
999 if (printed_waiting_msg) {
1000 dev_info(xpc_part, "waiting for local partition"
1002 printed_waiting_msg = 0;
1006 if (!xpc_disengage_request_timedout) {
1007 dev_info(xpc_part, "all partitions have "
1013 /* sleep for a 1/3 of a second or so */
1014 (void) msleep_interruptible(300);
1018 DBUG_ON(xpc_partition_engaged(-1UL));
1021 /* indicate to others that our reserved page is uninitialized */
1022 xpc_rsvd_page->vars_pa = 0;
1024 /* now it's time to eliminate our heartbeat */
1025 del_timer_sync(&xpc_hb_timer);
1026 DBUG_ON(xpc_vars->heartbeating_to_mask != 0);
1028 if (reason == xpcUnloading) {
1029 /* take ourselves off of the reboot_notifier_list */
1030 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
1032 /* take ourselves off of the die_notifier list */
1033 (void) unregister_die_notifier(&xpc_die_notifier);
1036 /* close down protections for IPI operations */
1037 xpc_restrict_IPI_ops();
1040 /* clear the interface to XPC's functions */
1041 xpc_clear_interface();
1044 unregister_sysctl_table(xpc_sysctl);
1050 * This function is called when the system is being rebooted.
1053 xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
1055 enum xpc_retval reason;
1060 reason = xpcSystemReboot;
1063 reason = xpcSystemHalt;
1066 reason = xpcSystemPoweroff;
1069 reason = xpcSystemGoingDown;
1072 xpc_do_exit(reason);
1078 * Notify other partitions to disengage from all references to our memory.
1081 xpc_die_disengage(void)
1083 struct xpc_partition *part;
1085 unsigned long engaged;
1086 long time, printmsg_time, disengage_request_timeout;
1089 /* keep xpc_hb_checker thread from doing anything (just in case) */
1092 xpc_vars->heartbeating_to_mask = 0; /* indicate we're deactivated */
1094 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1095 part = &xpc_partitions[partid];
1097 if (!XPC_SUPPORTS_DISENGAGE_REQUEST(part->
1098 remote_vars_version)) {
1100 /* just in case it was left set by an earlier XPC */
1101 xpc_clear_partition_engaged(1UL << partid);
1105 if (xpc_partition_engaged(1UL << partid) ||
1106 part->act_state != XPC_P_INACTIVE) {
1107 xpc_request_partition_disengage(part);
1108 xpc_mark_partition_disengaged(part);
1109 xpc_IPI_send_disengage(part);
1114 printmsg_time = time +
1115 (XPC_DISENGAGE_PRINTMSG_INTERVAL * sn_rtc_cycles_per_second);
1116 disengage_request_timeout = time +
1117 (xpc_disengage_request_timelimit * sn_rtc_cycles_per_second);
1119 /* wait for all other partitions to disengage from us */
1122 engaged = xpc_partition_engaged(-1UL);
1124 dev_info(xpc_part, "all partitions have disengaged\n");
1129 if (time >= disengage_request_timeout) {
1130 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1131 if (engaged & (1UL << partid)) {
1132 dev_info(xpc_part, "disengage from "
1133 "remote partition %d timed "
1140 if (time >= printmsg_time) {
1141 dev_info(xpc_part, "waiting for remote partitions to "
1142 "disengage, timeout in %ld seconds\n",
1143 (disengage_request_timeout - time) /
1144 sn_rtc_cycles_per_second);
1145 printmsg_time = time +
1146 (XPC_DISENGAGE_PRINTMSG_INTERVAL *
1147 sn_rtc_cycles_per_second);
1154 * This function is called when the system is being restarted or halted due
1155 * to some sort of system failure. If this is the case we need to notify the
1156 * other partitions to disengage from all references to our memory.
1157 * This function can also be called when our heartbeater could be offlined
1158 * for a time. In this case we need to notify other partitions to not worry
1159 * about the lack of a heartbeat.
1162 xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1165 case DIE_MACHINE_RESTART:
1166 case DIE_MACHINE_HALT:
1167 xpc_die_disengage();
1170 case DIE_KDEBUG_ENTER:
1171 /* Should lack of heartbeat be ignored by other partitions? */
1172 if (!xpc_kdebug_ignore) {
1176 case DIE_MCA_MONARCH_ENTER:
1177 case DIE_INIT_MONARCH_ENTER:
1178 xpc_vars->heartbeat++;
1179 xpc_vars->heartbeat_offline = 1;
1182 case DIE_KDEBUG_LEAVE:
1183 /* Is lack of heartbeat being ignored by other partitions? */
1184 if (!xpc_kdebug_ignore) {
1188 case DIE_MCA_MONARCH_LEAVE:
1189 case DIE_INIT_MONARCH_LEAVE:
1190 xpc_vars->heartbeat++;
1191 xpc_vars->heartbeat_offline = 0;
1204 struct xpc_partition *part;
1208 if (!ia64_platform_is("sn2")) {
1213 * xpc_remote_copy_buffer is used as a temporary buffer for bte_copy'ng
1214 * various portions of a partition's reserved page. Its size is based
1215 * on the size of the reserved page header and part_nasids mask. So we
1216 * need to ensure that the other items will fit as well.
1218 if (XPC_RP_VARS_SIZE > XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES) {
1219 dev_err(xpc_part, "xpc_remote_copy_buffer is not big enough\n");
1222 DBUG_ON((u64) xpc_remote_copy_buffer !=
1223 L1_CACHE_ALIGN((u64) xpc_remote_copy_buffer));
1225 snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1226 snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
1228 xpc_sysctl = register_sysctl_table(xpc_sys_dir, 1);
1231 * The first few fields of each entry of xpc_partitions[] need to
1232 * be initialized now so that calls to xpc_connect() and
1233 * xpc_disconnect() can be made prior to the activation of any remote
1234 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
1235 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
1236 * PARTITION HAS BEEN ACTIVATED.
1238 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1239 part = &xpc_partitions[partid];
1241 DBUG_ON((u64) part != L1_CACHE_ALIGN((u64) part));
1243 part->act_IRQ_rcvd = 0;
1244 spin_lock_init(&part->act_lock);
1245 part->act_state = XPC_P_INACTIVE;
1246 XPC_SET_REASON(part, 0, 0);
1248 init_timer(&part->disengage_request_timer);
1249 part->disengage_request_timer.function =
1250 xpc_timeout_partition_disengage_request;
1251 part->disengage_request_timer.data = (unsigned long) part;
1253 part->setup_state = XPC_P_UNSET;
1254 init_waitqueue_head(&part->teardown_wq);
1255 atomic_set(&part->references, 0);
1259 * Open up protections for IPI operations (and AMO operations on
1260 * Shub 1.1 systems).
1262 xpc_allow_IPI_ops();
1265 * Interrupts being processed will increment this atomic variable and
1266 * awaken the heartbeat thread which will process the interrupts.
1268 atomic_set(&xpc_act_IRQ_rcvd, 0);
1271 * This is safe to do before the xpc_hb_checker thread has started
1272 * because the handler releases a wait queue. If an interrupt is
1273 * received before the thread is waiting, it will not go to sleep,
1274 * but rather immediately process the interrupt.
1276 ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
1279 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
1280 "errno=%d\n", -ret);
1282 xpc_restrict_IPI_ops();
1285 unregister_sysctl_table(xpc_sysctl);
1291 * Fill the partition reserved page with the information needed by
1292 * other partitions to discover we are alive and establish initial
1295 xpc_rsvd_page = xpc_rsvd_page_init();
1296 if (xpc_rsvd_page == NULL) {
1297 dev_err(xpc_part, "could not setup our reserved page\n");
1299 free_irq(SGI_XPC_ACTIVATE, NULL);
1300 xpc_restrict_IPI_ops();
1303 unregister_sysctl_table(xpc_sysctl);
1309 /* add ourselves to the reboot_notifier_list */
1310 ret = register_reboot_notifier(&xpc_reboot_notifier);
1312 dev_warn(xpc_part, "can't register reboot notifier\n");
1315 /* add ourselves to the die_notifier list (i.e., ia64die_chain) */
1316 ret = register_die_notifier(&xpc_die_notifier);
1318 dev_warn(xpc_part, "can't register die notifier\n");
1323 * Set the beating to other partitions into motion. This is
1324 * the last requirement for other partitions' discovery to
1325 * initiate communications with us.
1327 init_timer(&xpc_hb_timer);
1328 xpc_hb_timer.function = xpc_hb_beater;
1333 * The real work-horse behind xpc. This processes incoming
1334 * interrupts and monitors remote heartbeats.
1336 pid = kernel_thread(xpc_hb_checker, NULL, 0);
1338 dev_err(xpc_part, "failed while forking hb check thread\n");
1340 /* indicate to others that our reserved page is uninitialized */
1341 xpc_rsvd_page->vars_pa = 0;
1343 /* take ourselves off of the reboot_notifier_list */
1344 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
1346 /* take ourselves off of the die_notifier list */
1347 (void) unregister_die_notifier(&xpc_die_notifier);
1349 del_timer_sync(&xpc_hb_timer);
1350 free_irq(SGI_XPC_ACTIVATE, NULL);
1351 xpc_restrict_IPI_ops();
1354 unregister_sysctl_table(xpc_sysctl);
1361 * Startup a thread that will attempt to discover other partitions to
1362 * activate based on info provided by SAL. This new thread is short
1363 * lived and will exit once discovery is complete.
1365 pid = kernel_thread(xpc_initiate_discovery, NULL, 0);
1367 dev_err(xpc_part, "failed while forking discovery thread\n");
1369 /* mark this new thread as a non-starter */
1370 up(&xpc_discovery_exited);
1372 xpc_do_exit(xpcUnloading);
1377 /* set the interface to point at XPC's functions */
1378 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
1379 xpc_initiate_allocate, xpc_initiate_send,
1380 xpc_initiate_send_notify, xpc_initiate_received,
1381 xpc_initiate_partid_to_nasids);
1385 module_init(xpc_init);
1391 xpc_do_exit(xpcUnloading);
1393 module_exit(xpc_exit);
1396 MODULE_AUTHOR("Silicon Graphics, Inc.");
1397 MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1398 MODULE_LICENSE("GPL");
1400 module_param(xpc_hb_interval, int, 0);
1401 MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
1402 "heartbeat increments.");
1404 module_param(xpc_hb_check_interval, int, 0);
1405 MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
1406 "heartbeat checks.");
1408 module_param(xpc_disengage_request_timelimit, int, 0);
1409 MODULE_PARM_DESC(xpc_disengage_request_timelimit, "Number of seconds to wait "
1410 "for disengage request to complete.");
1412 module_param(xpc_kdebug_ignore, int, 0);
1413 MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
1414 "other partitions when dropping into kdebug.");