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/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>
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,
114 &xpc_hb_min_interval,
120 &xpc_hb_check_interval,
124 &proc_dointvec_minmax,
128 &xpc_hb_check_min_interval,
129 &xpc_hb_check_max_interval
133 static ctl_table xpc_sys_xpc_dir[] = {
144 "disengage_request_timelimit",
145 &xpc_disengage_request_timelimit,
149 &proc_dointvec_minmax,
153 &xpc_disengage_request_min_timelimit,
154 &xpc_disengage_request_max_timelimit
158 static ctl_table xpc_sys_dir[] = {
169 static struct ctl_table_header *xpc_sysctl;
171 /* non-zero if any remote partition disengage request was timed out */
172 int xpc_disengage_request_timedout;
174 /* #of IRQs received */
175 static atomic_t xpc_act_IRQ_rcvd;
177 /* IRQ handler notifies this wait queue on receipt of an IRQ */
178 static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
180 static unsigned long xpc_hb_check_timeout;
182 /* notification that the xpc_hb_checker thread has exited */
183 static DECLARE_COMPLETION(xpc_hb_checker_exited);
185 /* notification that the xpc_discovery thread has exited */
186 static DECLARE_COMPLETION(xpc_discovery_exited);
189 static struct timer_list xpc_hb_timer;
192 static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
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,
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,
207 * Timer function to enforce the timelimit on the partition disengage request.
210 xpc_timeout_partition_disengage_request(unsigned long data)
212 struct xpc_partition *part = (struct xpc_partition *) data;
215 DBUG_ON(jiffies < part->disengage_request_timeout);
217 (void) xpc_partition_disengaged(part);
219 DBUG_ON(part->disengage_request_timeout != 0);
220 DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
225 * Notify the heartbeat check thread that an IRQ has been received.
228 xpc_act_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
230 atomic_inc(&xpc_act_IRQ_rcvd);
231 wake_up_interruptible(&xpc_act_IRQ_wq);
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.
242 xpc_hb_beater(unsigned long dummy)
244 xpc_vars->heartbeat++;
246 if (jiffies >= xpc_hb_check_timeout) {
247 wake_up_interruptible(&xpc_act_IRQ_wq);
250 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
251 add_timer(&xpc_hb_timer);
256 * This thread is responsible for nearly all of the partition
257 * activation/deactivation.
260 xpc_hb_checker(void *ignore)
262 int last_IRQ_count = 0;
267 /* this thread was marked active by xpc_hb_init() */
269 daemonize(XPC_HB_CHECK_THREAD_NAME);
271 set_cpus_allowed(current, cpumask_of_cpu(XPC_HB_CHECK_CPU));
273 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
275 while (!(volatile int) xpc_exiting) {
277 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
279 (int) (xpc_hb_check_timeout - jiffies),
280 atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
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();
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.
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) {
302 dev_dbg(xpc_part, "found an IRQ to process; will be "
303 "resetting xpc_hb_check_timeout\n");
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();
310 last_IRQ_count = new_IRQ_count;
312 xpc_hb_check_timeout = jiffies +
313 (xpc_hb_check_interval * HZ);
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));
323 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
326 /* mark this thread as having exited */
327 complete(&xpc_hb_checker_exited);
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.
338 xpc_initiate_discovery(void *ignore)
340 daemonize(XPC_DISCOVERY_THREAD_NAME);
344 dev_dbg(xpc_part, "discovery thread is exiting\n");
346 /* mark this thread as having exited */
347 complete(&xpc_discovery_exited);
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.
357 static enum xpc_retval
358 xpc_make_first_contact(struct xpc_partition *part)
363 while ((ret = xpc_pull_remote_vars_part(part)) != xpcSuccess) {
364 if (ret != xpcRetry) {
365 XPC_DEACTIVATE_PARTITION(part, ret);
369 dev_dbg(xpc_chan, "waiting to make first contact with "
370 "partition %d\n", XPC_PARTID(part));
372 /* wait a 1/4 of a second or so */
373 (void) msleep_interruptible(250);
375 if (part->act_state == XPC_P_DEACTIVATING) {
380 return xpc_mark_partition_active(part);
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.
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.
397 xpc_channel_mgr(struct xpc_partition *part)
399 while (part->act_state != XPC_P_DEACTIVATING ||
400 atomic_read(&part->nchannels_active) > 0 ||
401 !xpc_partition_disengaged(part)) {
403 xpc_process_channel_activity(part);
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.
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
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);
429 // >>> Does it need to wakeup periodically as well? In case we
430 // >>> miscalculated the #of kthreads to wakeup or create?
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
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
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().
451 xpc_partition_up(struct xpc_partition *part)
453 DBUG_ON(part->channels != NULL);
455 dev_dbg(xpc_chan, "activating partition %d\n", XPC_PARTID(part));
457 if (xpc_setup_infrastructure(part) != xpcSuccess) {
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.
468 (void) xpc_part_ref(part); /* this will always succeed */
470 if (xpc_make_first_contact(part) == xpcSuccess) {
471 xpc_channel_mgr(part);
474 xpc_part_deref(part);
476 xpc_teardown_infrastructure(part);
481 xpc_activating(void *__partid)
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 };
490 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
492 spin_lock_irqsave(&part->act_lock, irq_flags);
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;
501 /* indicate the thread is activating */
502 DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
503 part->act_state = XPC_P_ACTIVATING;
505 XPC_SET_REASON(part, 0, 0);
506 spin_unlock_irqrestore(&part->act_lock, irq_flags);
508 dev_dbg(xpc_part, "bringing partition %d up\n", partid);
510 daemonize("xpc%02d", partid);
513 * This thread needs to run at a realtime priority to prevent a
514 * significant performance degradation.
516 ret = sched_setscheduler(current, SCHED_FIFO, ¶m);
518 dev_warn(xpc_part, "unable to set pid %d to a realtime "
519 "priority, ret=%d\n", current->pid, ret);
522 /* allow this thread and its children to run on any CPU */
523 set_cpus_allowed(current, CPU_MASK_ALL);
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.
535 if (sn_register_xp_addr_region(part->remote_amos_page_pa,
537 dev_warn(xpc_part, "xpc_partition_up(%d) failed to register "
538 "xp_addr region\n", partid);
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;
548 xpc_allow_hb(partid, xpc_vars);
549 xpc_IPI_send_activated(part);
553 * xpc_partition_up() holds this thread and marks this partition as
554 * XPC_P_ACTIVE by calling xpc_hb_mark_active().
556 (void) xpc_partition_up(part);
558 xpc_disallow_hb(partid, xpc_vars);
559 xpc_mark_partition_inactive(part);
561 if (part->reason == xpcReactivating) {
562 /* interrupting ourselves results in activating partition */
563 xpc_IPI_send_reactivate(part);
571 xpc_activate_partition(struct xpc_partition *part)
573 partid_t partid = XPC_PARTID(part);
574 unsigned long irq_flags;
578 spin_lock_irqsave(&part->act_lock, irq_flags);
580 DBUG_ON(part->act_state != XPC_P_INACTIVE);
582 part->act_state = XPC_P_ACTIVATION_REQ;
583 XPC_SET_REASON(part, xpcCloneKThread, __LINE__);
585 spin_unlock_irqrestore(&part->act_lock, irq_flags);
587 pid = kernel_thread(xpc_activating, (void *) ((u64) partid), 0);
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);
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.
605 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
606 * running on other partitions.
608 * Noteworthy Arguments:
610 * irq - Interrupt ReQuest number. NOT USED.
612 * dev_id - partid of IPI's potential sender.
614 * regs - processor's context before the processor entered
615 * interrupt code. NOT USED.
618 xpc_notify_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
620 partid_t partid = (partid_t) (u64) dev_id;
621 struct xpc_partition *part = &xpc_partitions[partid];
624 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
626 if (xpc_part_ref(part)) {
627 xpc_check_for_channel_activity(part);
629 xpc_part_deref(part);
636 * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
637 * because the write to their associated IPI amo completed after the IRQ/IPI
641 xpc_dropped_IPI_check(struct xpc_partition *part)
643 if (xpc_part_ref(part)) {
644 xpc_check_for_channel_activity(part);
646 part->dropped_IPI_timer.expires = jiffies +
647 XPC_P_DROPPED_IPI_WAIT;
648 add_timer(&part->dropped_IPI_timer);
649 xpc_part_deref(part);
655 xpc_activate_kthreads(struct xpc_channel *ch, int needed)
657 int idle = atomic_read(&ch->kthreads_idle);
658 int assigned = atomic_read(&ch->kthreads_assigned);
662 DBUG_ON(needed <= 0);
665 wakeup = (needed > idle) ? idle : needed;
668 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
669 "channel=%d\n", wakeup, ch->partid, ch->number);
671 /* only wakeup the requested number of kthreads */
672 wake_up_nr(&ch->idle_wq, wakeup);
679 if (needed + assigned > ch->kthreads_assigned_limit) {
680 needed = ch->kthreads_assigned_limit - assigned;
681 // >>>should never be less than 0
687 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
688 needed, ch->partid, ch->number);
690 xpc_create_kthreads(ch, needed);
695 * This function is where XPC's kthreads wait for messages to deliver.
698 xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
701 /* deliver messages to their intended recipients */
703 while ((volatile s64) ch->w_local_GP.get <
704 (volatile s64) ch->w_remote_GP.put &&
705 !((volatile u32) ch->flags &
706 XPC_C_DISCONNECTING)) {
710 if (atomic_inc_return(&ch->kthreads_idle) >
711 ch->kthreads_idle_limit) {
712 /* too many idle kthreads on this channel */
713 atomic_dec(&ch->kthreads_idle);
717 dev_dbg(xpc_chan, "idle kthread calling "
718 "wait_event_interruptible_exclusive()\n");
720 (void) wait_event_interruptible_exclusive(ch->idle_wq,
721 ((volatile s64) ch->w_local_GP.get <
722 (volatile s64) ch->w_remote_GP.put ||
723 ((volatile u32) ch->flags &
724 XPC_C_DISCONNECTING)));
726 atomic_dec(&ch->kthreads_idle);
728 } while (!((volatile u32) ch->flags & XPC_C_DISCONNECTING));
733 xpc_daemonize_kthread(void *args)
735 partid_t partid = XPC_UNPACK_ARG1(args);
736 u16 ch_number = XPC_UNPACK_ARG2(args);
737 struct xpc_partition *part = &xpc_partitions[partid];
738 struct xpc_channel *ch;
740 unsigned long irq_flags;
743 daemonize("xpc%02dc%d", partid, ch_number);
745 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
748 ch = &part->channels[ch_number];
750 if (!(ch->flags & XPC_C_DISCONNECTING)) {
752 /* let registerer know that connection has been established */
754 spin_lock_irqsave(&ch->lock, irq_flags);
755 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
756 ch->flags |= XPC_C_CONNECTEDCALLOUT;
757 spin_unlock_irqrestore(&ch->lock, irq_flags);
759 xpc_connected_callout(ch);
761 spin_lock_irqsave(&ch->lock, irq_flags);
762 ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
763 spin_unlock_irqrestore(&ch->lock, irq_flags);
766 * It is possible that while the callout was being
767 * made that the remote partition sent some messages.
768 * If that is the case, we may need to activate
769 * additional kthreads to help deliver them. We only
770 * need one less than total #of messages to deliver.
772 n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
774 !(ch->flags & XPC_C_DISCONNECTING)) {
775 xpc_activate_kthreads(ch, n_needed);
778 spin_unlock_irqrestore(&ch->lock, irq_flags);
781 xpc_kthread_waitmsgs(part, ch);
784 if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
785 spin_lock_irqsave(&ch->lock, irq_flags);
786 if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
787 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
788 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
789 spin_unlock_irqrestore(&ch->lock, irq_flags);
791 xpc_disconnect_callout(ch, xpcDisconnecting);
793 spin_lock_irqsave(&ch->lock, irq_flags);
794 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
796 spin_unlock_irqrestore(&ch->lock, irq_flags);
797 if (atomic_dec_return(&part->nchannels_engaged) == 0) {
798 xpc_mark_partition_disengaged(part);
799 xpc_IPI_send_disengage(part);
804 xpc_msgqueue_deref(ch);
806 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
809 xpc_part_deref(part);
815 * For each partition that XPC has established communications with, there is
816 * a minimum of one kernel thread assigned to perform any operation that
817 * may potentially sleep or block (basically the callouts to the asynchronous
818 * functions registered via xpc_connect()).
820 * Additional kthreads are created and destroyed by XPC as the workload
823 * A kthread is assigned to one of the active channels that exists for a given
827 xpc_create_kthreads(struct xpc_channel *ch, int needed)
829 unsigned long irq_flags;
831 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
832 struct xpc_partition *part = &xpc_partitions[ch->partid];
835 while (needed-- > 0) {
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.
842 (void) xpc_part_ref(part);
843 xpc_msgqueue_ref(ch);
844 if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
845 atomic_inc_return(&part->nchannels_engaged) == 1) {
846 xpc_mark_partition_engaged(part);
849 pid = kernel_thread(xpc_daemonize_kthread, (void *) args, 0);
851 /* the fork failed */
852 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
853 atomic_dec_return(&part->nchannels_engaged) == 0) {
854 xpc_mark_partition_disengaged(part);
855 xpc_IPI_send_disengage(part);
857 xpc_msgqueue_deref(ch);
858 xpc_part_deref(part);
860 if (atomic_read(&ch->kthreads_assigned) <
861 ch->kthreads_idle_limit) {
863 * Flag this as an error only if we have an
864 * insufficient #of kthreads for the channel
867 * No xpc_msgqueue_ref() is needed here since
868 * the channel mgr is doing this.
870 spin_lock_irqsave(&ch->lock, irq_flags);
871 XPC_DISCONNECT_CHANNEL(ch, xpcLackOfResources,
873 spin_unlock_irqrestore(&ch->lock, irq_flags);
878 ch->kthreads_created++; // >>> temporary debug only!!!
884 xpc_disconnect_wait(int ch_number)
886 unsigned long irq_flags;
888 struct xpc_partition *part;
889 struct xpc_channel *ch;
890 int wakeup_channel_mgr;
893 /* now wait for all callouts to the caller's function to cease */
894 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
895 part = &xpc_partitions[partid];
897 if (!xpc_part_ref(part)) {
901 ch = &part->channels[ch_number];
903 if (!(ch->flags & XPC_C_WDISCONNECT)) {
904 xpc_part_deref(part);
908 wait_for_completion(&ch->wdisconnect_wait);
910 spin_lock_irqsave(&ch->lock, irq_flags);
911 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
912 wakeup_channel_mgr = 0;
914 if (ch->delayed_IPI_flags) {
915 if (part->act_state != XPC_P_DEACTIVATING) {
916 spin_lock(&part->IPI_lock);
917 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
918 ch->number, ch->delayed_IPI_flags);
919 spin_unlock(&part->IPI_lock);
920 wakeup_channel_mgr = 1;
922 ch->delayed_IPI_flags = 0;
925 ch->flags &= ~XPC_C_WDISCONNECT;
926 spin_unlock_irqrestore(&ch->lock, irq_flags);
928 if (wakeup_channel_mgr) {
929 xpc_wakeup_channel_mgr(part);
932 xpc_part_deref(part);
938 xpc_do_exit(enum xpc_retval reason)
941 int active_part_count, printed_waiting_msg = 0;
942 struct xpc_partition *part;
943 unsigned long printmsg_time, disengage_request_timeout = 0;
946 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
947 DBUG_ON(xpc_exiting == 1);
950 * Let the heartbeat checker thread and the discovery thread
951 * (if one is running) know that they should exit. Also wake up
952 * the heartbeat checker thread in case it's sleeping.
955 wake_up_interruptible(&xpc_act_IRQ_wq);
957 /* ignore all incoming interrupts */
958 free_irq(SGI_XPC_ACTIVATE, NULL);
960 /* wait for the discovery thread to exit */
961 wait_for_completion(&xpc_discovery_exited);
963 /* wait for the heartbeat checker thread to exit */
964 wait_for_completion(&xpc_hb_checker_exited);
967 /* sleep for a 1/3 of a second or so */
968 (void) msleep_interruptible(300);
971 /* wait for all partitions to become inactive */
973 printmsg_time = jiffies + (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
974 xpc_disengage_request_timedout = 0;
977 active_part_count = 0;
979 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
980 part = &xpc_partitions[partid];
982 if (xpc_partition_disengaged(part) &&
983 part->act_state == XPC_P_INACTIVE) {
989 XPC_DEACTIVATE_PARTITION(part, reason);
991 if (part->disengage_request_timeout >
992 disengage_request_timeout) {
993 disengage_request_timeout =
994 part->disengage_request_timeout;
998 if (xpc_partition_engaged(-1UL)) {
999 if (time_after(jiffies, printmsg_time)) {
1000 dev_info(xpc_part, "waiting for remote "
1001 "partitions to disengage, timeout in "
1003 (disengage_request_timeout - jiffies)
1005 printmsg_time = jiffies +
1006 (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
1007 printed_waiting_msg = 1;
1010 } else if (active_part_count > 0) {
1011 if (printed_waiting_msg) {
1012 dev_info(xpc_part, "waiting for local partition"
1014 printed_waiting_msg = 0;
1018 if (!xpc_disengage_request_timedout) {
1019 dev_info(xpc_part, "all partitions have "
1025 /* sleep for a 1/3 of a second or so */
1026 (void) msleep_interruptible(300);
1030 DBUG_ON(xpc_partition_engaged(-1UL));
1033 /* indicate to others that our reserved page is uninitialized */
1034 xpc_rsvd_page->vars_pa = 0;
1036 /* now it's time to eliminate our heartbeat */
1037 del_timer_sync(&xpc_hb_timer);
1038 DBUG_ON(xpc_vars->heartbeating_to_mask != 0);
1040 if (reason == xpcUnloading) {
1041 /* take ourselves off of the reboot_notifier_list */
1042 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
1044 /* take ourselves off of the die_notifier list */
1045 (void) unregister_die_notifier(&xpc_die_notifier);
1048 /* close down protections for IPI operations */
1049 xpc_restrict_IPI_ops();
1052 /* clear the interface to XPC's functions */
1053 xpc_clear_interface();
1056 unregister_sysctl_table(xpc_sysctl);
1059 kfree(xpc_remote_copy_buffer_base);
1064 * This function is called when the system is being rebooted.
1067 xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
1069 enum xpc_retval reason;
1074 reason = xpcSystemReboot;
1077 reason = xpcSystemHalt;
1080 reason = xpcSystemPoweroff;
1083 reason = xpcSystemGoingDown;
1086 xpc_do_exit(reason);
1092 * Notify other partitions to disengage from all references to our memory.
1095 xpc_die_disengage(void)
1097 struct xpc_partition *part;
1099 unsigned long engaged;
1100 long time, printmsg_time, disengage_request_timeout;
1103 /* keep xpc_hb_checker thread from doing anything (just in case) */
1106 xpc_vars->heartbeating_to_mask = 0; /* indicate we're deactivated */
1108 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1109 part = &xpc_partitions[partid];
1111 if (!XPC_SUPPORTS_DISENGAGE_REQUEST(part->
1112 remote_vars_version)) {
1114 /* just in case it was left set by an earlier XPC */
1115 xpc_clear_partition_engaged(1UL << partid);
1119 if (xpc_partition_engaged(1UL << partid) ||
1120 part->act_state != XPC_P_INACTIVE) {
1121 xpc_request_partition_disengage(part);
1122 xpc_mark_partition_disengaged(part);
1123 xpc_IPI_send_disengage(part);
1128 printmsg_time = time +
1129 (XPC_DISENGAGE_PRINTMSG_INTERVAL * sn_rtc_cycles_per_second);
1130 disengage_request_timeout = time +
1131 (xpc_disengage_request_timelimit * sn_rtc_cycles_per_second);
1133 /* wait for all other partitions to disengage from us */
1136 engaged = xpc_partition_engaged(-1UL);
1138 dev_info(xpc_part, "all partitions have disengaged\n");
1143 if (time >= disengage_request_timeout) {
1144 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1145 if (engaged & (1UL << partid)) {
1146 dev_info(xpc_part, "disengage from "
1147 "remote partition %d timed "
1154 if (time >= printmsg_time) {
1155 dev_info(xpc_part, "waiting for remote partitions to "
1156 "disengage, timeout in %ld seconds\n",
1157 (disengage_request_timeout - time) /
1158 sn_rtc_cycles_per_second);
1159 printmsg_time = time +
1160 (XPC_DISENGAGE_PRINTMSG_INTERVAL *
1161 sn_rtc_cycles_per_second);
1168 * This function is called when the system is being restarted or halted due
1169 * to some sort of system failure. If this is the case we need to notify the
1170 * other partitions to disengage from all references to our memory.
1171 * This function can also be called when our heartbeater could be offlined
1172 * for a time. In this case we need to notify other partitions to not worry
1173 * about the lack of a heartbeat.
1176 xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1179 case DIE_MACHINE_RESTART:
1180 case DIE_MACHINE_HALT:
1181 xpc_die_disengage();
1184 case DIE_KDEBUG_ENTER:
1185 /* Should lack of heartbeat be ignored by other partitions? */
1186 if (!xpc_kdebug_ignore) {
1190 case DIE_MCA_MONARCH_ENTER:
1191 case DIE_INIT_MONARCH_ENTER:
1192 xpc_vars->heartbeat++;
1193 xpc_vars->heartbeat_offline = 1;
1196 case DIE_KDEBUG_LEAVE:
1197 /* Is lack of heartbeat being ignored by other partitions? */
1198 if (!xpc_kdebug_ignore) {
1202 case DIE_MCA_MONARCH_LEAVE:
1203 case DIE_INIT_MONARCH_LEAVE:
1204 xpc_vars->heartbeat++;
1205 xpc_vars->heartbeat_offline = 0;
1218 struct xpc_partition *part;
1223 if (!ia64_platform_is("sn2")) {
1228 buf_size = max(XPC_RP_VARS_SIZE,
1229 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES);
1230 xpc_remote_copy_buffer = xpc_kmalloc_cacheline_aligned(buf_size,
1231 GFP_KERNEL, &xpc_remote_copy_buffer_base);
1232 if (xpc_remote_copy_buffer == NULL)
1235 snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1236 snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
1238 xpc_sysctl = register_sysctl_table(xpc_sys_dir, 1);
1241 * The first few fields of each entry of xpc_partitions[] need to
1242 * be initialized now so that calls to xpc_connect() and
1243 * xpc_disconnect() can be made prior to the activation of any remote
1244 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
1245 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
1246 * PARTITION HAS BEEN ACTIVATED.
1248 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1249 part = &xpc_partitions[partid];
1251 DBUG_ON((u64) part != L1_CACHE_ALIGN((u64) part));
1253 part->act_IRQ_rcvd = 0;
1254 spin_lock_init(&part->act_lock);
1255 part->act_state = XPC_P_INACTIVE;
1256 XPC_SET_REASON(part, 0, 0);
1258 init_timer(&part->disengage_request_timer);
1259 part->disengage_request_timer.function =
1260 xpc_timeout_partition_disengage_request;
1261 part->disengage_request_timer.data = (unsigned long) part;
1263 part->setup_state = XPC_P_UNSET;
1264 init_waitqueue_head(&part->teardown_wq);
1265 atomic_set(&part->references, 0);
1269 * Open up protections for IPI operations (and AMO operations on
1270 * Shub 1.1 systems).
1272 xpc_allow_IPI_ops();
1275 * Interrupts being processed will increment this atomic variable and
1276 * awaken the heartbeat thread which will process the interrupts.
1278 atomic_set(&xpc_act_IRQ_rcvd, 0);
1281 * This is safe to do before the xpc_hb_checker thread has started
1282 * because the handler releases a wait queue. If an interrupt is
1283 * received before the thread is waiting, it will not go to sleep,
1284 * but rather immediately process the interrupt.
1286 ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
1289 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
1290 "errno=%d\n", -ret);
1292 xpc_restrict_IPI_ops();
1295 unregister_sysctl_table(xpc_sysctl);
1298 kfree(xpc_remote_copy_buffer_base);
1303 * Fill the partition reserved page with the information needed by
1304 * other partitions to discover we are alive and establish initial
1307 xpc_rsvd_page = xpc_rsvd_page_init();
1308 if (xpc_rsvd_page == NULL) {
1309 dev_err(xpc_part, "could not setup our reserved page\n");
1311 free_irq(SGI_XPC_ACTIVATE, NULL);
1312 xpc_restrict_IPI_ops();
1315 unregister_sysctl_table(xpc_sysctl);
1318 kfree(xpc_remote_copy_buffer_base);
1323 /* add ourselves to the reboot_notifier_list */
1324 ret = register_reboot_notifier(&xpc_reboot_notifier);
1326 dev_warn(xpc_part, "can't register reboot notifier\n");
1329 /* add ourselves to the die_notifier list (i.e., ia64die_chain) */
1330 ret = register_die_notifier(&xpc_die_notifier);
1332 dev_warn(xpc_part, "can't register die notifier\n");
1337 * Set the beating to other partitions into motion. This is
1338 * the last requirement for other partitions' discovery to
1339 * initiate communications with us.
1341 init_timer(&xpc_hb_timer);
1342 xpc_hb_timer.function = xpc_hb_beater;
1347 * The real work-horse behind xpc. This processes incoming
1348 * interrupts and monitors remote heartbeats.
1350 pid = kernel_thread(xpc_hb_checker, NULL, 0);
1352 dev_err(xpc_part, "failed while forking hb check thread\n");
1354 /* indicate to others that our reserved page is uninitialized */
1355 xpc_rsvd_page->vars_pa = 0;
1357 /* take ourselves off of the reboot_notifier_list */
1358 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
1360 /* take ourselves off of the die_notifier list */
1361 (void) unregister_die_notifier(&xpc_die_notifier);
1363 del_timer_sync(&xpc_hb_timer);
1364 free_irq(SGI_XPC_ACTIVATE, NULL);
1365 xpc_restrict_IPI_ops();
1368 unregister_sysctl_table(xpc_sysctl);
1371 kfree(xpc_remote_copy_buffer_base);
1377 * Startup a thread that will attempt to discover other partitions to
1378 * activate based on info provided by SAL. This new thread is short
1379 * lived and will exit once discovery is complete.
1381 pid = kernel_thread(xpc_initiate_discovery, NULL, 0);
1383 dev_err(xpc_part, "failed while forking discovery thread\n");
1385 /* mark this new thread as a non-starter */
1386 complete(&xpc_discovery_exited);
1388 xpc_do_exit(xpcUnloading);
1393 /* set the interface to point at XPC's functions */
1394 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
1395 xpc_initiate_allocate, xpc_initiate_send,
1396 xpc_initiate_send_notify, xpc_initiate_received,
1397 xpc_initiate_partid_to_nasids);
1401 module_init(xpc_init);
1407 xpc_do_exit(xpcUnloading);
1409 module_exit(xpc_exit);
1412 MODULE_AUTHOR("Silicon Graphics, Inc.");
1413 MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1414 MODULE_LICENSE("GPL");
1416 module_param(xpc_hb_interval, int, 0);
1417 MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
1418 "heartbeat increments.");
1420 module_param(xpc_hb_check_interval, int, 0);
1421 MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
1422 "heartbeat checks.");
1424 module_param(xpc_disengage_request_timelimit, int, 0);
1425 MODULE_PARM_DESC(xpc_disengage_request_timelimit, "Number of seconds to wait "
1426 "for disengage request to complete.");
1428 module_param(xpc_kdebug_ignore, int, 0);
1429 MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
1430 "other partitions when dropping into kdebug.");