[IA64-SGI] get XPC to cleanly disengage from remote memory references
[deliverable/linux.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-2005 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/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/uaccess.h>
61 #include "xpc.h"
62
63
64 /* define two XPC debug device structures to be used with dev_dbg() et al */
65
66 struct device_driver xpc_dbg_name = {
67 .name = "xpc"
68 };
69
70 struct device xpc_part_dbg_subname = {
71 .bus_id = {0}, /* set to "part" at xpc_init() time */
72 .driver = &xpc_dbg_name
73 };
74
75 struct device xpc_chan_dbg_subname = {
76 .bus_id = {0}, /* set to "chan" at xpc_init() time */
77 .driver = &xpc_dbg_name
78 };
79
80 struct device *xpc_part = &xpc_part_dbg_subname;
81 struct device *xpc_chan = &xpc_chan_dbg_subname;
82
83
84 /* systune related variables for /proc/sys directories */
85
86 static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
87 static int xpc_hb_min_interval = 1;
88 static int xpc_hb_max_interval = 10;
89
90 static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
91 static int xpc_hb_check_min_interval = 10;
92 static int xpc_hb_check_max_interval = 120;
93
94 static ctl_table xpc_sys_xpc_hb_dir[] = {
95 {
96 1,
97 "hb_interval",
98 &xpc_hb_interval,
99 sizeof(int),
100 0644,
101 NULL,
102 &proc_dointvec_minmax,
103 &sysctl_intvec,
104 NULL,
105 &xpc_hb_min_interval,
106 &xpc_hb_max_interval
107 },
108 {
109 2,
110 "hb_check_interval",
111 &xpc_hb_check_interval,
112 sizeof(int),
113 0644,
114 NULL,
115 &proc_dointvec_minmax,
116 &sysctl_intvec,
117 NULL,
118 &xpc_hb_check_min_interval,
119 &xpc_hb_check_max_interval
120 },
121 {0}
122 };
123 static ctl_table xpc_sys_xpc_dir[] = {
124 {
125 1,
126 "hb",
127 NULL,
128 0,
129 0555,
130 xpc_sys_xpc_hb_dir
131 },
132 {0}
133 };
134 static ctl_table xpc_sys_dir[] = {
135 {
136 1,
137 "xpc",
138 NULL,
139 0,
140 0555,
141 xpc_sys_xpc_dir
142 },
143 {0}
144 };
145 static struct ctl_table_header *xpc_sysctl;
146
147
148 /* #of IRQs received */
149 static atomic_t xpc_act_IRQ_rcvd;
150
151 /* IRQ handler notifies this wait queue on receipt of an IRQ */
152 static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
153
154 static unsigned long xpc_hb_check_timeout;
155
156 /* used as an indication of when the xpc_hb_checker thread is inactive */
157 static DECLARE_MUTEX_LOCKED(xpc_hb_checker_inactive);
158
159 /* used as an indication of when the xpc_discovery thread is inactive */
160 static DECLARE_MUTEX_LOCKED(xpc_discovery_inactive);
161
162
163 static struct timer_list xpc_hb_timer;
164
165
166 static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
167
168
169 static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
170 static struct notifier_block xpc_reboot_notifier = {
171 .notifier_call = xpc_system_reboot,
172 };
173
174
175 /*
176 * Timer function to enforce the timelimit on the partition disengage request.
177 */
178 static void
179 xpc_timeout_partition_disengage_request(unsigned long data)
180 {
181 struct xpc_partition *part = (struct xpc_partition *) data;
182
183
184 DBUG_ON(XPC_TICKS < part->disengage_request_timeout);
185
186 (void) xpc_partition_disengaged(part);
187
188 DBUG_ON(part->disengage_request_timeout != 0);
189 DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
190 }
191
192
193 /*
194 * Notify the heartbeat check thread that an IRQ has been received.
195 */
196 static irqreturn_t
197 xpc_act_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
198 {
199 atomic_inc(&xpc_act_IRQ_rcvd);
200 wake_up_interruptible(&xpc_act_IRQ_wq);
201 return IRQ_HANDLED;
202 }
203
204
205 /*
206 * Timer to produce the heartbeat. The timer structures function is
207 * already set when this is initially called. A tunable is used to
208 * specify when the next timeout should occur.
209 */
210 static void
211 xpc_hb_beater(unsigned long dummy)
212 {
213 xpc_vars->heartbeat++;
214
215 if (jiffies >= xpc_hb_check_timeout) {
216 wake_up_interruptible(&xpc_act_IRQ_wq);
217 }
218
219 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
220 add_timer(&xpc_hb_timer);
221 }
222
223
224 /*
225 * This thread is responsible for nearly all of the partition
226 * activation/deactivation.
227 */
228 static int
229 xpc_hb_checker(void *ignore)
230 {
231 int last_IRQ_count = 0;
232 int new_IRQ_count;
233 int force_IRQ=0;
234
235
236 /* this thread was marked active by xpc_hb_init() */
237
238 daemonize(XPC_HB_CHECK_THREAD_NAME);
239
240 set_cpus_allowed(current, cpumask_of_cpu(XPC_HB_CHECK_CPU));
241
242 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
243
244 while (!(volatile int) xpc_exiting) {
245
246 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
247 "been received\n",
248 (int) (xpc_hb_check_timeout - jiffies),
249 atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
250
251
252 /* checking of remote heartbeats is skewed by IRQ handling */
253 if (jiffies >= xpc_hb_check_timeout) {
254 dev_dbg(xpc_part, "checking remote heartbeats\n");
255 xpc_check_remote_hb();
256
257 /*
258 * We need to periodically recheck to ensure no
259 * IPI/AMO pairs have been missed. That check
260 * must always reset xpc_hb_check_timeout.
261 */
262 force_IRQ = 1;
263 }
264
265
266 /* check for outstanding IRQs */
267 new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
268 if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
269 force_IRQ = 0;
270
271 dev_dbg(xpc_part, "found an IRQ to process; will be "
272 "resetting xpc_hb_check_timeout\n");
273
274 last_IRQ_count += xpc_identify_act_IRQ_sender();
275 if (last_IRQ_count < new_IRQ_count) {
276 /* retry once to help avoid missing AMO */
277 (void) xpc_identify_act_IRQ_sender();
278 }
279 last_IRQ_count = new_IRQ_count;
280
281 xpc_hb_check_timeout = jiffies +
282 (xpc_hb_check_interval * HZ);
283 }
284
285 /* wait for IRQ or timeout */
286 (void) wait_event_interruptible(xpc_act_IRQ_wq,
287 (last_IRQ_count < atomic_read(&xpc_act_IRQ_rcvd) ||
288 jiffies >= xpc_hb_check_timeout ||
289 (volatile int) xpc_exiting));
290 }
291
292 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
293
294
295 /* mark this thread as inactive */
296 up(&xpc_hb_checker_inactive);
297 return 0;
298 }
299
300
301 /*
302 * This thread will attempt to discover other partitions to activate
303 * based on info provided by SAL. This new thread is short lived and
304 * will exit once discovery is complete.
305 */
306 static int
307 xpc_initiate_discovery(void *ignore)
308 {
309 daemonize(XPC_DISCOVERY_THREAD_NAME);
310
311 xpc_discovery();
312
313 dev_dbg(xpc_part, "discovery thread is exiting\n");
314
315 /* mark this thread as inactive */
316 up(&xpc_discovery_inactive);
317 return 0;
318 }
319
320
321 /*
322 * Establish first contact with the remote partititon. This involves pulling
323 * the XPC per partition variables from the remote partition and waiting for
324 * the remote partition to pull ours.
325 */
326 static enum xpc_retval
327 xpc_make_first_contact(struct xpc_partition *part)
328 {
329 enum xpc_retval ret;
330
331
332 while ((ret = xpc_pull_remote_vars_part(part)) != xpcSuccess) {
333 if (ret != xpcRetry) {
334 XPC_DEACTIVATE_PARTITION(part, ret);
335 return ret;
336 }
337
338 dev_dbg(xpc_chan, "waiting to make first contact with "
339 "partition %d\n", XPC_PARTID(part));
340
341 /* wait a 1/4 of a second or so */
342 (void) msleep_interruptible(250);
343
344 if (part->act_state == XPC_P_DEACTIVATING) {
345 return part->reason;
346 }
347 }
348
349 return xpc_mark_partition_active(part);
350 }
351
352
353 /*
354 * The first kthread assigned to a newly activated partition is the one
355 * created by XPC HB with which it calls xpc_partition_up(). XPC hangs on to
356 * that kthread until the partition is brought down, at which time that kthread
357 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
358 * that XPC has dismantled all communication infrastructure for the associated
359 * partition.) This kthread becomes the channel manager for that partition.
360 *
361 * Each active partition has a channel manager, who, besides connecting and
362 * disconnecting channels, will ensure that each of the partition's connected
363 * channels has the required number of assigned kthreads to get the work done.
364 */
365 static void
366 xpc_channel_mgr(struct xpc_partition *part)
367 {
368 while (part->act_state != XPC_P_DEACTIVATING ||
369 atomic_read(&part->nchannels_active) > 0 ||
370 !xpc_partition_disengaged(part)) {
371
372 xpc_process_channel_activity(part);
373
374
375 /*
376 * Wait until we've been requested to activate kthreads or
377 * all of the channel's message queues have been torn down or
378 * a signal is pending.
379 *
380 * The channel_mgr_requests is set to 1 after being awakened,
381 * This is done to prevent the channel mgr from making one pass
382 * through the loop for each request, since he will
383 * be servicing all the requests in one pass. The reason it's
384 * set to 1 instead of 0 is so that other kthreads will know
385 * that the channel mgr is running and won't bother trying to
386 * wake him up.
387 */
388 atomic_dec(&part->channel_mgr_requests);
389 (void) wait_event_interruptible(part->channel_mgr_wq,
390 (atomic_read(&part->channel_mgr_requests) > 0 ||
391 (volatile u64) part->local_IPI_amo != 0 ||
392 ((volatile u8) part->act_state ==
393 XPC_P_DEACTIVATING &&
394 atomic_read(&part->nchannels_active) == 0 &&
395 xpc_partition_disengaged(part))));
396 atomic_set(&part->channel_mgr_requests, 1);
397
398 // >>> Does it need to wakeup periodically as well? In case we
399 // >>> miscalculated the #of kthreads to wakeup or create?
400 }
401 }
402
403
404 /*
405 * When XPC HB determines that a partition has come up, it will create a new
406 * kthread and that kthread will call this function to attempt to set up the
407 * basic infrastructure used for Cross Partition Communication with the newly
408 * upped partition.
409 *
410 * The kthread that was created by XPC HB and which setup the XPC
411 * infrastructure will remain assigned to the partition until the partition
412 * goes down. At which time the kthread will teardown the XPC infrastructure
413 * and then exit.
414 *
415 * XPC HB will put the remote partition's XPC per partition specific variables
416 * physical address into xpc_partitions[partid].remote_vars_part_pa prior to
417 * calling xpc_partition_up().
418 */
419 static void
420 xpc_partition_up(struct xpc_partition *part)
421 {
422 DBUG_ON(part->channels != NULL);
423
424 dev_dbg(xpc_chan, "activating partition %d\n", XPC_PARTID(part));
425
426 if (xpc_setup_infrastructure(part) != xpcSuccess) {
427 return;
428 }
429
430 /*
431 * The kthread that XPC HB called us with will become the
432 * channel manager for this partition. It will not return
433 * back to XPC HB until the partition's XPC infrastructure
434 * has been dismantled.
435 */
436
437 (void) xpc_part_ref(part); /* this will always succeed */
438
439 if (xpc_make_first_contact(part) == xpcSuccess) {
440 xpc_channel_mgr(part);
441 }
442
443 xpc_part_deref(part);
444
445 xpc_teardown_infrastructure(part);
446 }
447
448
449 static int
450 xpc_activating(void *__partid)
451 {
452 partid_t partid = (u64) __partid;
453 struct xpc_partition *part = &xpc_partitions[partid];
454 unsigned long irq_flags;
455 struct sched_param param = { sched_priority: MAX_RT_PRIO - 1 };
456 int ret;
457
458
459 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
460
461 spin_lock_irqsave(&part->act_lock, irq_flags);
462
463 if (part->act_state == XPC_P_DEACTIVATING) {
464 part->act_state = XPC_P_INACTIVE;
465 spin_unlock_irqrestore(&part->act_lock, irq_flags);
466 part->remote_rp_pa = 0;
467 return 0;
468 }
469
470 /* indicate the thread is activating */
471 DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
472 part->act_state = XPC_P_ACTIVATING;
473
474 XPC_SET_REASON(part, 0, 0);
475 spin_unlock_irqrestore(&part->act_lock, irq_flags);
476
477 dev_dbg(xpc_part, "bringing partition %d up\n", partid);
478
479 daemonize("xpc%02d", partid);
480
481 /*
482 * This thread needs to run at a realtime priority to prevent a
483 * significant performance degradation.
484 */
485 ret = sched_setscheduler(current, SCHED_FIFO, &param);
486 if (ret != 0) {
487 dev_warn(xpc_part, "unable to set pid %d to a realtime "
488 "priority, ret=%d\n", current->pid, ret);
489 }
490
491 /* allow this thread and its children to run on any CPU */
492 set_cpus_allowed(current, CPU_MASK_ALL);
493
494 /*
495 * Register the remote partition's AMOs with SAL so it can handle
496 * and cleanup errors within that address range should the remote
497 * partition go down. We don't unregister this range because it is
498 * difficult to tell when outstanding writes to the remote partition
499 * are finished and thus when it is safe to unregister. This should
500 * not result in wasted space in the SAL xp_addr_region table because
501 * we should get the same page for remote_amos_page_pa after module
502 * reloads and system reboots.
503 */
504 if (sn_register_xp_addr_region(part->remote_amos_page_pa,
505 PAGE_SIZE, 1) < 0) {
506 dev_warn(xpc_part, "xpc_partition_up(%d) failed to register "
507 "xp_addr region\n", partid);
508
509 spin_lock_irqsave(&part->act_lock, irq_flags);
510 part->act_state = XPC_P_INACTIVE;
511 XPC_SET_REASON(part, xpcPhysAddrRegFailed, __LINE__);
512 spin_unlock_irqrestore(&part->act_lock, irq_flags);
513 part->remote_rp_pa = 0;
514 return 0;
515 }
516
517 xpc_allow_hb(partid, xpc_vars);
518 xpc_IPI_send_activated(part);
519
520
521 /*
522 * xpc_partition_up() holds this thread and marks this partition as
523 * XPC_P_ACTIVE by calling xpc_hb_mark_active().
524 */
525 (void) xpc_partition_up(part);
526
527 xpc_disallow_hb(partid, xpc_vars);
528 xpc_mark_partition_inactive(part);
529
530 if (part->reason == xpcReactivating) {
531 /* interrupting ourselves results in activating partition */
532 xpc_IPI_send_reactivate(part);
533 }
534
535 return 0;
536 }
537
538
539 void
540 xpc_activate_partition(struct xpc_partition *part)
541 {
542 partid_t partid = XPC_PARTID(part);
543 unsigned long irq_flags;
544 pid_t pid;
545
546
547 spin_lock_irqsave(&part->act_lock, irq_flags);
548
549 pid = kernel_thread(xpc_activating, (void *) ((u64) partid), 0);
550
551 DBUG_ON(part->act_state != XPC_P_INACTIVE);
552
553 if (pid > 0) {
554 part->act_state = XPC_P_ACTIVATION_REQ;
555 XPC_SET_REASON(part, xpcCloneKThread, __LINE__);
556 } else {
557 XPC_SET_REASON(part, xpcCloneKThreadFailed, __LINE__);
558 }
559
560 spin_unlock_irqrestore(&part->act_lock, irq_flags);
561 }
562
563
564 /*
565 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
566 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
567 * than one partition, we use an AMO_t structure per partition to indicate
568 * whether a partition has sent an IPI or not. >>> If it has, then wake up the
569 * associated kthread to handle it.
570 *
571 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
572 * running on other partitions.
573 *
574 * Noteworthy Arguments:
575 *
576 * irq - Interrupt ReQuest number. NOT USED.
577 *
578 * dev_id - partid of IPI's potential sender.
579 *
580 * regs - processor's context before the processor entered
581 * interrupt code. NOT USED.
582 */
583 irqreturn_t
584 xpc_notify_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
585 {
586 partid_t partid = (partid_t) (u64) dev_id;
587 struct xpc_partition *part = &xpc_partitions[partid];
588
589
590 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
591
592 if (xpc_part_ref(part)) {
593 xpc_check_for_channel_activity(part);
594
595 xpc_part_deref(part);
596 }
597 return IRQ_HANDLED;
598 }
599
600
601 /*
602 * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
603 * because the write to their associated IPI amo completed after the IRQ/IPI
604 * was received.
605 */
606 void
607 xpc_dropped_IPI_check(struct xpc_partition *part)
608 {
609 if (xpc_part_ref(part)) {
610 xpc_check_for_channel_activity(part);
611
612 part->dropped_IPI_timer.expires = jiffies +
613 XPC_P_DROPPED_IPI_WAIT;
614 add_timer(&part->dropped_IPI_timer);
615 xpc_part_deref(part);
616 }
617 }
618
619
620 void
621 xpc_activate_kthreads(struct xpc_channel *ch, int needed)
622 {
623 int idle = atomic_read(&ch->kthreads_idle);
624 int assigned = atomic_read(&ch->kthreads_assigned);
625 int wakeup;
626
627
628 DBUG_ON(needed <= 0);
629
630 if (idle > 0) {
631 wakeup = (needed > idle) ? idle : needed;
632 needed -= wakeup;
633
634 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
635 "channel=%d\n", wakeup, ch->partid, ch->number);
636
637 /* only wakeup the requested number of kthreads */
638 wake_up_nr(&ch->idle_wq, wakeup);
639 }
640
641 if (needed <= 0) {
642 return;
643 }
644
645 if (needed + assigned > ch->kthreads_assigned_limit) {
646 needed = ch->kthreads_assigned_limit - assigned;
647 // >>>should never be less than 0
648 if (needed <= 0) {
649 return;
650 }
651 }
652
653 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
654 needed, ch->partid, ch->number);
655
656 xpc_create_kthreads(ch, needed);
657 }
658
659
660 /*
661 * This function is where XPC's kthreads wait for messages to deliver.
662 */
663 static void
664 xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
665 {
666 do {
667 /* deliver messages to their intended recipients */
668
669 while ((volatile s64) ch->w_local_GP.get <
670 (volatile s64) ch->w_remote_GP.put &&
671 !((volatile u32) ch->flags &
672 XPC_C_DISCONNECTING)) {
673 xpc_deliver_msg(ch);
674 }
675
676 if (atomic_inc_return(&ch->kthreads_idle) >
677 ch->kthreads_idle_limit) {
678 /* too many idle kthreads on this channel */
679 atomic_dec(&ch->kthreads_idle);
680 break;
681 }
682
683 dev_dbg(xpc_chan, "idle kthread calling "
684 "wait_event_interruptible_exclusive()\n");
685
686 (void) wait_event_interruptible_exclusive(ch->idle_wq,
687 ((volatile s64) ch->w_local_GP.get <
688 (volatile s64) ch->w_remote_GP.put ||
689 ((volatile u32) ch->flags &
690 XPC_C_DISCONNECTING)));
691
692 atomic_dec(&ch->kthreads_idle);
693
694 } while (!((volatile u32) ch->flags & XPC_C_DISCONNECTING));
695 }
696
697
698 static int
699 xpc_daemonize_kthread(void *args)
700 {
701 partid_t partid = XPC_UNPACK_ARG1(args);
702 u16 ch_number = XPC_UNPACK_ARG2(args);
703 struct xpc_partition *part = &xpc_partitions[partid];
704 struct xpc_channel *ch;
705 int n_needed;
706
707
708 daemonize("xpc%02dc%d", partid, ch_number);
709
710 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
711 partid, ch_number);
712
713 ch = &part->channels[ch_number];
714
715 if (!(ch->flags & XPC_C_DISCONNECTING)) {
716 DBUG_ON(!(ch->flags & XPC_C_CONNECTED));
717
718 /* let registerer know that connection has been established */
719
720 if (atomic_read(&ch->kthreads_assigned) == 1) {
721 xpc_connected_callout(ch);
722
723 /*
724 * It is possible that while the callout was being
725 * made that the remote partition sent some messages.
726 * If that is the case, we may need to activate
727 * additional kthreads to help deliver them. We only
728 * need one less than total #of messages to deliver.
729 */
730 n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
731 if (n_needed > 0 &&
732 !(ch->flags & XPC_C_DISCONNECTING)) {
733 xpc_activate_kthreads(ch, n_needed);
734 }
735 }
736
737 xpc_kthread_waitmsgs(part, ch);
738 }
739
740 if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
741 if (ch->flags & XPC_C_CONNECTCALLOUT) {
742 xpc_disconnecting_callout(ch);
743 }
744 if (atomic_dec_return(&part->nchannels_engaged) == 0) {
745 xpc_mark_partition_disengaged(part);
746 xpc_IPI_send_disengage(part);
747 }
748 }
749
750
751 xpc_msgqueue_deref(ch);
752
753 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
754 partid, ch_number);
755
756 xpc_part_deref(part);
757 return 0;
758 }
759
760
761 /*
762 * For each partition that XPC has established communications with, there is
763 * a minimum of one kernel thread assigned to perform any operation that
764 * may potentially sleep or block (basically the callouts to the asynchronous
765 * functions registered via xpc_connect()).
766 *
767 * Additional kthreads are created and destroyed by XPC as the workload
768 * demands.
769 *
770 * A kthread is assigned to one of the active channels that exists for a given
771 * partition.
772 */
773 void
774 xpc_create_kthreads(struct xpc_channel *ch, int needed)
775 {
776 unsigned long irq_flags;
777 pid_t pid;
778 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
779 struct xpc_partition *part = &xpc_partitions[ch->partid];
780
781
782 while (needed-- > 0) {
783 pid = kernel_thread(xpc_daemonize_kthread, (void *) args, 0);
784 if (pid < 0) {
785 /* the fork failed */
786
787 if (atomic_read(&ch->kthreads_assigned) <
788 ch->kthreads_idle_limit) {
789 /*
790 * Flag this as an error only if we have an
791 * insufficient #of kthreads for the channel
792 * to function.
793 *
794 * No xpc_msgqueue_ref() is needed here since
795 * the channel mgr is doing this.
796 */
797 spin_lock_irqsave(&ch->lock, irq_flags);
798 XPC_DISCONNECT_CHANNEL(ch, xpcLackOfResources,
799 &irq_flags);
800 spin_unlock_irqrestore(&ch->lock, irq_flags);
801 }
802 break;
803 }
804
805 /*
806 * The following is done on behalf of the newly created
807 * kthread. That kthread is responsible for doing the
808 * counterpart to the following before it exits.
809 */
810 (void) xpc_part_ref(part);
811 xpc_msgqueue_ref(ch);
812 if (atomic_inc_return(&ch->kthreads_assigned) == 1) {
813 if (atomic_inc_return(&part->nchannels_engaged) == 1) {
814 xpc_mark_partition_engaged(part);
815 }
816 }
817 ch->kthreads_created++; // >>> temporary debug only!!!
818 }
819 }
820
821
822 void
823 xpc_disconnect_wait(int ch_number)
824 {
825 unsigned long irq_flags;
826 partid_t partid;
827 struct xpc_partition *part;
828 struct xpc_channel *ch;
829
830
831 /* now wait for all callouts to the caller's function to cease */
832 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
833 part = &xpc_partitions[partid];
834
835 if (xpc_part_ref(part)) {
836 ch = &part->channels[ch_number];
837
838 if (ch->flags & XPC_C_WDISCONNECT) {
839 if (!(ch->flags & XPC_C_DISCONNECTED)) {
840 (void) down(&ch->wdisconnect_sema);
841 }
842 spin_lock_irqsave(&ch->lock, irq_flags);
843 ch->flags &= ~XPC_C_WDISCONNECT;
844 spin_unlock_irqrestore(&ch->lock, irq_flags);
845 }
846
847 xpc_part_deref(part);
848 }
849 }
850 }
851
852
853 static void
854 xpc_do_exit(enum xpc_retval reason)
855 {
856 partid_t partid;
857 int active_part_count;
858 struct xpc_partition *part;
859 unsigned long printmsg_time;
860
861
862 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
863 DBUG_ON(xpc_exiting == 1);
864
865 /*
866 * Let the heartbeat checker thread and the discovery thread
867 * (if one is running) know that they should exit. Also wake up
868 * the heartbeat checker thread in case it's sleeping.
869 */
870 xpc_exiting = 1;
871 wake_up_interruptible(&xpc_act_IRQ_wq);
872
873 /* ignore all incoming interrupts */
874 free_irq(SGI_XPC_ACTIVATE, NULL);
875
876 /* wait for the discovery thread to mark itself inactive */
877 down(&xpc_discovery_inactive);
878
879 /* wait for the heartbeat checker thread to mark itself inactive */
880 down(&xpc_hb_checker_inactive);
881
882
883 /* sleep for a 1/3 of a second or so */
884 (void) msleep_interruptible(300);
885
886
887 /* wait for all partitions to become inactive */
888
889 printmsg_time = jiffies;
890
891 do {
892 active_part_count = 0;
893
894 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
895 part = &xpc_partitions[partid];
896 if (xpc_partition_disengaged(part) &&
897 part->act_state == XPC_P_INACTIVE) {
898 continue;
899 }
900
901 active_part_count++;
902
903 XPC_DEACTIVATE_PARTITION(part, reason);
904 }
905
906 if (active_part_count == 0) {
907 break;
908 }
909
910 if (jiffies >= printmsg_time) {
911 dev_info(xpc_part, "waiting for partitions to "
912 "deactivate/disengage, active count=%d, remote "
913 "engaged=0x%lx\n", active_part_count,
914 xpc_partition_engaged(1UL << partid));
915
916 printmsg_time = jiffies +
917 (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
918 }
919
920 /* sleep for a 1/3 of a second or so */
921 (void) msleep_interruptible(300);
922
923 } while (1);
924
925 DBUG_ON(xpc_partition_engaged(-1UL));
926
927
928 /* indicate to others that our reserved page is uninitialized */
929 xpc_rsvd_page->vars_pa = 0;
930
931 /* now it's time to eliminate our heartbeat */
932 del_timer_sync(&xpc_hb_timer);
933 DBUG_ON(xpc_vars->heartbeating_to_mask == 0);
934
935 /* take ourselves off of the reboot_notifier_list */
936 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
937
938 /* close down protections for IPI operations */
939 xpc_restrict_IPI_ops();
940
941
942 /* clear the interface to XPC's functions */
943 xpc_clear_interface();
944
945 if (xpc_sysctl) {
946 unregister_sysctl_table(xpc_sysctl);
947 }
948 }
949
950
951 /*
952 * This function is called when the system is being rebooted.
953 */
954 static int
955 xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
956 {
957 enum xpc_retval reason;
958
959
960 switch (event) {
961 case SYS_RESTART:
962 reason = xpcSystemReboot;
963 break;
964 case SYS_HALT:
965 reason = xpcSystemHalt;
966 break;
967 case SYS_POWER_OFF:
968 reason = xpcSystemPoweroff;
969 break;
970 default:
971 reason = xpcSystemGoingDown;
972 }
973
974 xpc_do_exit(reason);
975 return NOTIFY_DONE;
976 }
977
978
979 int __init
980 xpc_init(void)
981 {
982 int ret;
983 partid_t partid;
984 struct xpc_partition *part;
985 pid_t pid;
986
987
988 /*
989 * xpc_remote_copy_buffer is used as a temporary buffer for bte_copy'ng
990 * both a partition's reserved page and its XPC variables. Its size was
991 * based on the size of a reserved page. So we need to ensure that the
992 * XPC variables will fit as well.
993 */
994 if (XPC_VARS_ALIGNED_SIZE > XPC_RSVD_PAGE_ALIGNED_SIZE) {
995 dev_err(xpc_part, "xpc_remote_copy_buffer is not big enough\n");
996 return -EPERM;
997 }
998 DBUG_ON((u64) xpc_remote_copy_buffer !=
999 L1_CACHE_ALIGN((u64) xpc_remote_copy_buffer));
1000
1001 snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1002 snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
1003
1004 xpc_sysctl = register_sysctl_table(xpc_sys_dir, 1);
1005
1006 /*
1007 * The first few fields of each entry of xpc_partitions[] need to
1008 * be initialized now so that calls to xpc_connect() and
1009 * xpc_disconnect() can be made prior to the activation of any remote
1010 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
1011 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
1012 * PARTITION HAS BEEN ACTIVATED.
1013 */
1014 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1015 part = &xpc_partitions[partid];
1016
1017 DBUG_ON((u64) part != L1_CACHE_ALIGN((u64) part));
1018
1019 part->act_IRQ_rcvd = 0;
1020 spin_lock_init(&part->act_lock);
1021 part->act_state = XPC_P_INACTIVE;
1022 XPC_SET_REASON(part, 0, 0);
1023
1024 init_timer(&part->disengage_request_timer);
1025 part->disengage_request_timer.function =
1026 xpc_timeout_partition_disengage_request;
1027 part->disengage_request_timer.data = (unsigned long) part;
1028
1029 part->setup_state = XPC_P_UNSET;
1030 init_waitqueue_head(&part->teardown_wq);
1031 atomic_set(&part->references, 0);
1032 }
1033
1034 /*
1035 * Open up protections for IPI operations (and AMO operations on
1036 * Shub 1.1 systems).
1037 */
1038 xpc_allow_IPI_ops();
1039
1040 /*
1041 * Interrupts being processed will increment this atomic variable and
1042 * awaken the heartbeat thread which will process the interrupts.
1043 */
1044 atomic_set(&xpc_act_IRQ_rcvd, 0);
1045
1046 /*
1047 * This is safe to do before the xpc_hb_checker thread has started
1048 * because the handler releases a wait queue. If an interrupt is
1049 * received before the thread is waiting, it will not go to sleep,
1050 * but rather immediately process the interrupt.
1051 */
1052 ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
1053 "xpc hb", NULL);
1054 if (ret != 0) {
1055 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
1056 "errno=%d\n", -ret);
1057
1058 xpc_restrict_IPI_ops();
1059
1060 if (xpc_sysctl) {
1061 unregister_sysctl_table(xpc_sysctl);
1062 }
1063 return -EBUSY;
1064 }
1065
1066 /*
1067 * Fill the partition reserved page with the information needed by
1068 * other partitions to discover we are alive and establish initial
1069 * communications.
1070 */
1071 xpc_rsvd_page = xpc_rsvd_page_init();
1072 if (xpc_rsvd_page == NULL) {
1073 dev_err(xpc_part, "could not setup our reserved page\n");
1074
1075 free_irq(SGI_XPC_ACTIVATE, NULL);
1076 xpc_restrict_IPI_ops();
1077
1078 if (xpc_sysctl) {
1079 unregister_sysctl_table(xpc_sysctl);
1080 }
1081 return -EBUSY;
1082 }
1083
1084
1085 /* add ourselves to the reboot_notifier_list */
1086 ret = register_reboot_notifier(&xpc_reboot_notifier);
1087 if (ret != 0) {
1088 dev_warn(xpc_part, "can't register reboot notifier\n");
1089 }
1090
1091
1092 /*
1093 * Set the beating to other partitions into motion. This is
1094 * the last requirement for other partitions' discovery to
1095 * initiate communications with us.
1096 */
1097 init_timer(&xpc_hb_timer);
1098 xpc_hb_timer.function = xpc_hb_beater;
1099 xpc_hb_beater(0);
1100
1101
1102 /*
1103 * The real work-horse behind xpc. This processes incoming
1104 * interrupts and monitors remote heartbeats.
1105 */
1106 pid = kernel_thread(xpc_hb_checker, NULL, 0);
1107 if (pid < 0) {
1108 dev_err(xpc_part, "failed while forking hb check thread\n");
1109
1110 /* indicate to others that our reserved page is uninitialized */
1111 xpc_rsvd_page->vars_pa = 0;
1112
1113 /* take ourselves off of the reboot_notifier_list */
1114 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
1115
1116 del_timer_sync(&xpc_hb_timer);
1117 free_irq(SGI_XPC_ACTIVATE, NULL);
1118 xpc_restrict_IPI_ops();
1119
1120 if (xpc_sysctl) {
1121 unregister_sysctl_table(xpc_sysctl);
1122 }
1123 return -EBUSY;
1124 }
1125
1126
1127 /*
1128 * Startup a thread that will attempt to discover other partitions to
1129 * activate based on info provided by SAL. This new thread is short
1130 * lived and will exit once discovery is complete.
1131 */
1132 pid = kernel_thread(xpc_initiate_discovery, NULL, 0);
1133 if (pid < 0) {
1134 dev_err(xpc_part, "failed while forking discovery thread\n");
1135
1136 /* mark this new thread as a non-starter */
1137 up(&xpc_discovery_inactive);
1138
1139 xpc_do_exit(xpcUnloading);
1140 return -EBUSY;
1141 }
1142
1143
1144 /* set the interface to point at XPC's functions */
1145 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
1146 xpc_initiate_allocate, xpc_initiate_send,
1147 xpc_initiate_send_notify, xpc_initiate_received,
1148 xpc_initiate_partid_to_nasids);
1149
1150 return 0;
1151 }
1152 module_init(xpc_init);
1153
1154
1155 void __exit
1156 xpc_exit(void)
1157 {
1158 xpc_do_exit(xpcUnloading);
1159 }
1160 module_exit(xpc_exit);
1161
1162
1163 MODULE_AUTHOR("Silicon Graphics, Inc.");
1164 MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1165 MODULE_LICENSE("GPL");
1166
1167 module_param(xpc_hb_interval, int, 0);
1168 MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
1169 "heartbeat increments.");
1170
1171 module_param(xpc_hb_check_interval, int, 0);
1172 MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
1173 "heartbeat checks.");
1174
This page took 0.200308 seconds and 5 git commands to generate.