blob: 195ac1b8e2620e06c06d9cc89f7e3c50a689c35f [file] [log] [blame]
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001/*
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) channel support.
12 *
13 * This is the part of XPC that manages the channels and
14 * sends/receives messages across them to/from other partitions.
15 *
16 */
17
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/sched.h>
22#include <linux/cache.h>
23#include <linux/interrupt.h>
24#include <linux/slab.h>
25#include <asm/sn/bte.h>
26#include <asm/sn/sn_sal.h>
27#include "xpc.h"
28
29
30/*
31 * Set up the initial values for the XPartition Communication channels.
32 */
33static void
34xpc_initialize_channels(struct xpc_partition *part, partid_t partid)
35{
36 int ch_number;
37 struct xpc_channel *ch;
38
39
40 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
41 ch = &part->channels[ch_number];
42
43 ch->partid = partid;
44 ch->number = ch_number;
45 ch->flags = XPC_C_DISCONNECTED;
46
47 ch->local_GP = &part->local_GPs[ch_number];
48 ch->local_openclose_args =
49 &part->local_openclose_args[ch_number];
50
51 atomic_set(&ch->kthreads_assigned, 0);
52 atomic_set(&ch->kthreads_idle, 0);
53 atomic_set(&ch->kthreads_active, 0);
54
55 atomic_set(&ch->references, 0);
56 atomic_set(&ch->n_to_notify, 0);
57
58 spin_lock_init(&ch->lock);
59 sema_init(&ch->msg_to_pull_sema, 1); /* mutex */
Dean Nelsona607c382005-09-01 14:01:37 -050060 sema_init(&ch->wdisconnect_sema, 0); /* event wait */
Dean Nelson89eb8eb2005-03-23 19:50:00 -070061
62 atomic_set(&ch->n_on_msg_allocate_wq, 0);
63 init_waitqueue_head(&ch->msg_allocate_wq);
64 init_waitqueue_head(&ch->idle_wq);
65 }
66}
67
68
69/*
70 * Setup the infrastructure necessary to support XPartition Communication
71 * between the specified remote partition and the local one.
72 */
73enum xpc_retval
74xpc_setup_infrastructure(struct xpc_partition *part)
75{
Dean Nelson59a0a8a2005-07-13 05:45:00 -070076 int ret, cpuid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070077 struct timer_list *timer;
78 partid_t partid = XPC_PARTID(part);
79
80
81 /*
82 * Zero out MOST of the entry for this partition. Only the fields
83 * starting with `nchannels' will be zeroed. The preceding fields must
84 * remain `viable' across partition ups and downs, since they may be
85 * referenced during this memset() operation.
86 */
87 memset(&part->nchannels, 0, sizeof(struct xpc_partition) -
88 offsetof(struct xpc_partition, nchannels));
89
90 /*
91 * Allocate all of the channel structures as a contiguous chunk of
92 * memory.
93 */
94 part->channels = kmalloc(sizeof(struct xpc_channel) * XPC_NCHANNELS,
95 GFP_KERNEL);
96 if (part->channels == NULL) {
97 dev_err(xpc_chan, "can't get memory for channels\n");
98 return xpcNoMemory;
99 }
100 memset(part->channels, 0, sizeof(struct xpc_channel) * XPC_NCHANNELS);
101
102 part->nchannels = XPC_NCHANNELS;
103
104
105 /* allocate all the required GET/PUT values */
106
107 part->local_GPs = xpc_kmalloc_cacheline_aligned(XPC_GP_SIZE,
108 GFP_KERNEL, &part->local_GPs_base);
109 if (part->local_GPs == NULL) {
110 kfree(part->channels);
111 part->channels = NULL;
112 dev_err(xpc_chan, "can't get memory for local get/put "
113 "values\n");
114 return xpcNoMemory;
115 }
116 memset(part->local_GPs, 0, XPC_GP_SIZE);
117
118 part->remote_GPs = xpc_kmalloc_cacheline_aligned(XPC_GP_SIZE,
119 GFP_KERNEL, &part->remote_GPs_base);
120 if (part->remote_GPs == NULL) {
121 kfree(part->channels);
122 part->channels = NULL;
123 kfree(part->local_GPs_base);
124 part->local_GPs = NULL;
125 dev_err(xpc_chan, "can't get memory for remote get/put "
126 "values\n");
127 return xpcNoMemory;
128 }
129 memset(part->remote_GPs, 0, XPC_GP_SIZE);
130
131
132 /* allocate all the required open and close args */
133
134 part->local_openclose_args = xpc_kmalloc_cacheline_aligned(
135 XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
136 &part->local_openclose_args_base);
137 if (part->local_openclose_args == NULL) {
138 kfree(part->channels);
139 part->channels = NULL;
140 kfree(part->local_GPs_base);
141 part->local_GPs = NULL;
142 kfree(part->remote_GPs_base);
143 part->remote_GPs = NULL;
144 dev_err(xpc_chan, "can't get memory for local connect args\n");
145 return xpcNoMemory;
146 }
147 memset(part->local_openclose_args, 0, XPC_OPENCLOSE_ARGS_SIZE);
148
149 part->remote_openclose_args = xpc_kmalloc_cacheline_aligned(
150 XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
151 &part->remote_openclose_args_base);
152 if (part->remote_openclose_args == NULL) {
153 kfree(part->channels);
154 part->channels = NULL;
155 kfree(part->local_GPs_base);
156 part->local_GPs = NULL;
157 kfree(part->remote_GPs_base);
158 part->remote_GPs = NULL;
159 kfree(part->local_openclose_args_base);
160 part->local_openclose_args = NULL;
161 dev_err(xpc_chan, "can't get memory for remote connect args\n");
162 return xpcNoMemory;
163 }
164 memset(part->remote_openclose_args, 0, XPC_OPENCLOSE_ARGS_SIZE);
165
166
167 xpc_initialize_channels(part, partid);
168
169 atomic_set(&part->nchannels_active, 0);
Dean Nelsona607c382005-09-01 14:01:37 -0500170 atomic_set(&part->nchannels_engaged, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700171
172
173 /* local_IPI_amo were set to 0 by an earlier memset() */
174
175 /* Initialize this partitions AMO_t structure */
176 part->local_IPI_amo_va = xpc_IPI_init(partid);
177
178 spin_lock_init(&part->IPI_lock);
179
180 atomic_set(&part->channel_mgr_requests, 1);
181 init_waitqueue_head(&part->channel_mgr_wq);
182
183 sprintf(part->IPI_owner, "xpc%02d", partid);
184 ret = request_irq(SGI_XPC_NOTIFY, xpc_notify_IRQ_handler, SA_SHIRQ,
185 part->IPI_owner, (void *) (u64) partid);
186 if (ret != 0) {
187 kfree(part->channels);
188 part->channels = NULL;
189 kfree(part->local_GPs_base);
190 part->local_GPs = NULL;
191 kfree(part->remote_GPs_base);
192 part->remote_GPs = NULL;
193 kfree(part->local_openclose_args_base);
194 part->local_openclose_args = NULL;
195 kfree(part->remote_openclose_args_base);
196 part->remote_openclose_args = NULL;
197 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
198 "errno=%d\n", -ret);
199 return xpcLackOfResources;
200 }
201
202 /* Setup a timer to check for dropped IPIs */
203 timer = &part->dropped_IPI_timer;
204 init_timer(timer);
205 timer->function = (void (*)(unsigned long)) xpc_dropped_IPI_check;
206 timer->data = (unsigned long) part;
207 timer->expires = jiffies + XPC_P_DROPPED_IPI_WAIT;
208 add_timer(timer);
209
210 /*
211 * With the setting of the partition setup_state to XPC_P_SETUP, we're
212 * declaring that this partition is ready to go.
213 */
Dave Jones821fe942005-06-25 14:54:29 -0700214 part->setup_state = XPC_P_SETUP;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700215
216
217 /*
218 * Setup the per partition specific variables required by the
219 * remote partition to establish channel connections with us.
220 *
221 * The setting of the magic # indicates that these per partition
222 * specific variables are ready to be used.
223 */
224 xpc_vars_part[partid].GPs_pa = __pa(part->local_GPs);
225 xpc_vars_part[partid].openclose_args_pa =
226 __pa(part->local_openclose_args);
227 xpc_vars_part[partid].IPI_amo_pa = __pa(part->local_IPI_amo_va);
Dean Nelson59a0a8a2005-07-13 05:45:00 -0700228 cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
229 xpc_vars_part[partid].IPI_nasid = cpuid_to_nasid(cpuid);
230 xpc_vars_part[partid].IPI_phys_cpuid = cpu_physical_id(cpuid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700231 xpc_vars_part[partid].nchannels = part->nchannels;
Dave Jones821fe942005-06-25 14:54:29 -0700232 xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700233
234 return xpcSuccess;
235}
236
237
238/*
239 * Create a wrapper that hides the underlying mechanism for pulling a cacheline
240 * (or multiple cachelines) from a remote partition.
241 *
242 * src must be a cacheline aligned physical address on the remote partition.
243 * dst must be a cacheline aligned virtual address on this partition.
244 * cnt must be an cacheline sized
245 */
246static enum xpc_retval
247xpc_pull_remote_cachelines(struct xpc_partition *part, void *dst,
248 const void *src, size_t cnt)
249{
250 bte_result_t bte_ret;
251
252
253 DBUG_ON((u64) src != L1_CACHE_ALIGN((u64) src));
254 DBUG_ON((u64) dst != L1_CACHE_ALIGN((u64) dst));
255 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
256
257 if (part->act_state == XPC_P_DEACTIVATING) {
258 return part->reason;
259 }
260
261 bte_ret = xp_bte_copy((u64) src, (u64) ia64_tpa((u64) dst),
262 (u64) cnt, (BTE_NORMAL | BTE_WACQUIRE), NULL);
263 if (bte_ret == BTE_SUCCESS) {
264 return xpcSuccess;
265 }
266
267 dev_dbg(xpc_chan, "xp_bte_copy() from partition %d failed, ret=%d\n",
268 XPC_PARTID(part), bte_ret);
269
270 return xpc_map_bte_errors(bte_ret);
271}
272
273
274/*
275 * Pull the remote per partititon specific variables from the specified
276 * partition.
277 */
278enum xpc_retval
279xpc_pull_remote_vars_part(struct xpc_partition *part)
280{
281 u8 buffer[L1_CACHE_BYTES * 2];
282 struct xpc_vars_part *pulled_entry_cacheline =
283 (struct xpc_vars_part *) L1_CACHE_ALIGN((u64) buffer);
284 struct xpc_vars_part *pulled_entry;
285 u64 remote_entry_cacheline_pa, remote_entry_pa;
286 partid_t partid = XPC_PARTID(part);
287 enum xpc_retval ret;
288
289
290 /* pull the cacheline that contains the variables we're interested in */
291
292 DBUG_ON(part->remote_vars_part_pa !=
293 L1_CACHE_ALIGN(part->remote_vars_part_pa));
294 DBUG_ON(sizeof(struct xpc_vars_part) != L1_CACHE_BYTES / 2);
295
296 remote_entry_pa = part->remote_vars_part_pa +
297 sn_partition_id * sizeof(struct xpc_vars_part);
298
299 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
300
301 pulled_entry = (struct xpc_vars_part *) ((u64) pulled_entry_cacheline +
302 (remote_entry_pa & (L1_CACHE_BYTES - 1)));
303
304 ret = xpc_pull_remote_cachelines(part, pulled_entry_cacheline,
305 (void *) remote_entry_cacheline_pa,
306 L1_CACHE_BYTES);
307 if (ret != xpcSuccess) {
308 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
309 "partition %d, ret=%d\n", partid, ret);
310 return ret;
311 }
312
313
314 /* see if they've been set up yet */
315
316 if (pulled_entry->magic != XPC_VP_MAGIC1 &&
317 pulled_entry->magic != XPC_VP_MAGIC2) {
318
319 if (pulled_entry->magic != 0) {
320 dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
321 "partition %d has bad magic value (=0x%lx)\n",
322 partid, sn_partition_id, pulled_entry->magic);
323 return xpcBadMagic;
324 }
325
326 /* they've not been initialized yet */
327 return xpcRetry;
328 }
329
330 if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
331
332 /* validate the variables */
333
334 if (pulled_entry->GPs_pa == 0 ||
335 pulled_entry->openclose_args_pa == 0 ||
336 pulled_entry->IPI_amo_pa == 0) {
337
338 dev_err(xpc_chan, "partition %d's XPC vars_part for "
339 "partition %d are not valid\n", partid,
340 sn_partition_id);
341 return xpcInvalidAddress;
342 }
343
344 /* the variables we imported look to be valid */
345
346 part->remote_GPs_pa = pulled_entry->GPs_pa;
347 part->remote_openclose_args_pa =
348 pulled_entry->openclose_args_pa;
349 part->remote_IPI_amo_va =
350 (AMO_t *) __va(pulled_entry->IPI_amo_pa);
351 part->remote_IPI_nasid = pulled_entry->IPI_nasid;
352 part->remote_IPI_phys_cpuid = pulled_entry->IPI_phys_cpuid;
353
354 if (part->nchannels > pulled_entry->nchannels) {
355 part->nchannels = pulled_entry->nchannels;
356 }
357
358 /* let the other side know that we've pulled their variables */
359
Dave Jones821fe942005-06-25 14:54:29 -0700360 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700361 }
362
363 if (pulled_entry->magic == XPC_VP_MAGIC1) {
364 return xpcRetry;
365 }
366
367 return xpcSuccess;
368}
369
370
371/*
372 * Get the IPI flags and pull the openclose args and/or remote GPs as needed.
373 */
374static u64
375xpc_get_IPI_flags(struct xpc_partition *part)
376{
377 unsigned long irq_flags;
378 u64 IPI_amo;
379 enum xpc_retval ret;
380
381
382 /*
383 * See if there are any IPI flags to be handled.
384 */
385
386 spin_lock_irqsave(&part->IPI_lock, irq_flags);
387 if ((IPI_amo = part->local_IPI_amo) != 0) {
388 part->local_IPI_amo = 0;
389 }
390 spin_unlock_irqrestore(&part->IPI_lock, irq_flags);
391
392
393 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_amo)) {
394 ret = xpc_pull_remote_cachelines(part,
395 part->remote_openclose_args,
396 (void *) part->remote_openclose_args_pa,
397 XPC_OPENCLOSE_ARGS_SIZE);
398 if (ret != xpcSuccess) {
399 XPC_DEACTIVATE_PARTITION(part, ret);
400
401 dev_dbg(xpc_chan, "failed to pull openclose args from "
402 "partition %d, ret=%d\n", XPC_PARTID(part),
403 ret);
404
405 /* don't bother processing IPIs anymore */
406 IPI_amo = 0;
407 }
408 }
409
410 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_amo)) {
411 ret = xpc_pull_remote_cachelines(part, part->remote_GPs,
412 (void *) part->remote_GPs_pa,
413 XPC_GP_SIZE);
414 if (ret != xpcSuccess) {
415 XPC_DEACTIVATE_PARTITION(part, ret);
416
417 dev_dbg(xpc_chan, "failed to pull GPs from partition "
418 "%d, ret=%d\n", XPC_PARTID(part), ret);
419
420 /* don't bother processing IPIs anymore */
421 IPI_amo = 0;
422 }
423 }
424
425 return IPI_amo;
426}
427
428
429/*
430 * Allocate the local message queue and the notify queue.
431 */
432static enum xpc_retval
433xpc_allocate_local_msgqueue(struct xpc_channel *ch)
434{
435 unsigned long irq_flags;
436 int nentries;
437 size_t nbytes;
438
439
440 // >>> may want to check for ch->flags & XPC_C_DISCONNECTING between
441 // >>> iterations of the for-loop, bail if set?
442
443 // >>> should we impose a minumum #of entries? like 4 or 8?
444 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
445
446 nbytes = nentries * ch->msg_size;
447 ch->local_msgqueue = xpc_kmalloc_cacheline_aligned(nbytes,
448 (GFP_KERNEL | GFP_DMA),
449 &ch->local_msgqueue_base);
450 if (ch->local_msgqueue == NULL) {
451 continue;
452 }
453 memset(ch->local_msgqueue, 0, nbytes);
454
455 nbytes = nentries * sizeof(struct xpc_notify);
456 ch->notify_queue = kmalloc(nbytes, (GFP_KERNEL | GFP_DMA));
457 if (ch->notify_queue == NULL) {
458 kfree(ch->local_msgqueue_base);
459 ch->local_msgqueue = NULL;
460 continue;
461 }
462 memset(ch->notify_queue, 0, nbytes);
463
464 spin_lock_irqsave(&ch->lock, irq_flags);
465 if (nentries < ch->local_nentries) {
466 dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
467 "partid=%d, channel=%d\n", nentries,
468 ch->local_nentries, ch->partid, ch->number);
469
470 ch->local_nentries = nentries;
471 }
472 spin_unlock_irqrestore(&ch->lock, irq_flags);
473 return xpcSuccess;
474 }
475
476 dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
477 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
478 return xpcNoMemory;
479}
480
481
482/*
483 * Allocate the cached remote message queue.
484 */
485static enum xpc_retval
486xpc_allocate_remote_msgqueue(struct xpc_channel *ch)
487{
488 unsigned long irq_flags;
489 int nentries;
490 size_t nbytes;
491
492
493 DBUG_ON(ch->remote_nentries <= 0);
494
495 // >>> may want to check for ch->flags & XPC_C_DISCONNECTING between
496 // >>> iterations of the for-loop, bail if set?
497
498 // >>> should we impose a minumum #of entries? like 4 or 8?
499 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
500
501 nbytes = nentries * ch->msg_size;
502 ch->remote_msgqueue = xpc_kmalloc_cacheline_aligned(nbytes,
503 (GFP_KERNEL | GFP_DMA),
504 &ch->remote_msgqueue_base);
505 if (ch->remote_msgqueue == NULL) {
506 continue;
507 }
508 memset(ch->remote_msgqueue, 0, nbytes);
509
510 spin_lock_irqsave(&ch->lock, irq_flags);
511 if (nentries < ch->remote_nentries) {
512 dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
513 "partid=%d, channel=%d\n", nentries,
514 ch->remote_nentries, ch->partid, ch->number);
515
516 ch->remote_nentries = nentries;
517 }
518 spin_unlock_irqrestore(&ch->lock, irq_flags);
519 return xpcSuccess;
520 }
521
522 dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
523 "partid=%d, channel=%d\n", ch->partid, ch->number);
524 return xpcNoMemory;
525}
526
527
528/*
529 * Allocate message queues and other stuff associated with a channel.
530 *
531 * Note: Assumes all of the channel sizes are filled in.
532 */
533static enum xpc_retval
534xpc_allocate_msgqueues(struct xpc_channel *ch)
535{
536 unsigned long irq_flags;
537 int i;
538 enum xpc_retval ret;
539
540
541 DBUG_ON(ch->flags & XPC_C_SETUP);
542
543 if ((ret = xpc_allocate_local_msgqueue(ch)) != xpcSuccess) {
544 return ret;
545 }
546
547 if ((ret = xpc_allocate_remote_msgqueue(ch)) != xpcSuccess) {
548 kfree(ch->local_msgqueue_base);
549 ch->local_msgqueue = NULL;
550 kfree(ch->notify_queue);
551 ch->notify_queue = NULL;
552 return ret;
553 }
554
555 for (i = 0; i < ch->local_nentries; i++) {
556 /* use a semaphore as an event wait queue */
557 sema_init(&ch->notify_queue[i].sema, 0);
558 }
559
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700560 spin_lock_irqsave(&ch->lock, irq_flags);
561 ch->flags |= XPC_C_SETUP;
562 spin_unlock_irqrestore(&ch->lock, irq_flags);
563
564 return xpcSuccess;
565}
566
567
568/*
569 * Process a connect message from a remote partition.
570 *
571 * Note: xpc_process_connect() is expecting to be called with the
572 * spin_lock_irqsave held and will leave it locked upon return.
573 */
574static void
575xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
576{
577 enum xpc_retval ret;
578
579
580 DBUG_ON(!spin_is_locked(&ch->lock));
581
582 if (!(ch->flags & XPC_C_OPENREQUEST) ||
583 !(ch->flags & XPC_C_ROPENREQUEST)) {
584 /* nothing more to do for now */
585 return;
586 }
587 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
588
589 if (!(ch->flags & XPC_C_SETUP)) {
590 spin_unlock_irqrestore(&ch->lock, *irq_flags);
591 ret = xpc_allocate_msgqueues(ch);
592 spin_lock_irqsave(&ch->lock, *irq_flags);
593
594 if (ret != xpcSuccess) {
595 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
596 }
597 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING)) {
598 return;
599 }
600
601 DBUG_ON(!(ch->flags & XPC_C_SETUP));
602 DBUG_ON(ch->local_msgqueue == NULL);
603 DBUG_ON(ch->remote_msgqueue == NULL);
604 }
605
606 if (!(ch->flags & XPC_C_OPENREPLY)) {
607 ch->flags |= XPC_C_OPENREPLY;
608 xpc_IPI_send_openreply(ch, irq_flags);
609 }
610
611 if (!(ch->flags & XPC_C_ROPENREPLY)) {
612 return;
613 }
614
615 DBUG_ON(ch->remote_msgqueue_pa == 0);
616
617 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
618
619 dev_info(xpc_chan, "channel %d to partition %d connected\n",
620 ch->number, ch->partid);
621
622 spin_unlock_irqrestore(&ch->lock, *irq_flags);
623 xpc_create_kthreads(ch, 1);
624 spin_lock_irqsave(&ch->lock, *irq_flags);
625}
626
627
628/*
Dean Nelsona607c382005-09-01 14:01:37 -0500629 * Notify those who wanted to be notified upon delivery of their message.
630 */
631static void
632xpc_notify_senders(struct xpc_channel *ch, enum xpc_retval reason, s64 put)
633{
634 struct xpc_notify *notify;
635 u8 notify_type;
636 s64 get = ch->w_remote_GP.get - 1;
637
638
639 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
640
641 notify = &ch->notify_queue[get % ch->local_nentries];
642
643 /*
644 * See if the notify entry indicates it was associated with
645 * a message who's sender wants to be notified. It is possible
646 * that it is, but someone else is doing or has done the
647 * notification.
648 */
649 notify_type = notify->type;
650 if (notify_type == 0 ||
651 cmpxchg(&notify->type, notify_type, 0) !=
652 notify_type) {
653 continue;
654 }
655
656 DBUG_ON(notify_type != XPC_N_CALL);
657
658 atomic_dec(&ch->n_to_notify);
659
660 if (notify->func != NULL) {
661 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
662 "msg_number=%ld, partid=%d, channel=%d\n",
663 (void *) notify, get, ch->partid, ch->number);
664
665 notify->func(reason, ch->partid, ch->number,
666 notify->key);
667
668 dev_dbg(xpc_chan, "notify->func() returned, "
669 "notify=0x%p, msg_number=%ld, partid=%d, "
670 "channel=%d\n", (void *) notify, get,
671 ch->partid, ch->number);
672 }
673 }
674}
675
676
677/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700678 * Free up message queues and other stuff that were allocated for the specified
679 * channel.
680 *
681 * Note: ch->reason and ch->reason_line are left set for debugging purposes,
682 * they're cleared when XPC_C_DISCONNECTED is cleared.
683 */
684static void
685xpc_free_msgqueues(struct xpc_channel *ch)
686{
687 DBUG_ON(!spin_is_locked(&ch->lock));
688 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
689
690 ch->remote_msgqueue_pa = 0;
691 ch->func = NULL;
692 ch->key = NULL;
693 ch->msg_size = 0;
694 ch->local_nentries = 0;
695 ch->remote_nentries = 0;
696 ch->kthreads_assigned_limit = 0;
697 ch->kthreads_idle_limit = 0;
698
699 ch->local_GP->get = 0;
700 ch->local_GP->put = 0;
701 ch->remote_GP.get = 0;
702 ch->remote_GP.put = 0;
703 ch->w_local_GP.get = 0;
704 ch->w_local_GP.put = 0;
705 ch->w_remote_GP.get = 0;
706 ch->w_remote_GP.put = 0;
707 ch->next_msg_to_pull = 0;
708
709 if (ch->flags & XPC_C_SETUP) {
710 ch->flags &= ~XPC_C_SETUP;
711
712 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
713 ch->flags, ch->partid, ch->number);
714
715 kfree(ch->local_msgqueue_base);
716 ch->local_msgqueue = NULL;
717 kfree(ch->remote_msgqueue_base);
718 ch->remote_msgqueue = NULL;
719 kfree(ch->notify_queue);
720 ch->notify_queue = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700721 }
722}
723
724
725/*
726 * spin_lock_irqsave() is expected to be held on entry.
727 */
728static void
729xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
730{
731 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c382005-09-01 14:01:37 -0500732 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700733
734
735 DBUG_ON(!spin_is_locked(&ch->lock));
736
737 if (!(ch->flags & XPC_C_DISCONNECTING)) {
738 return;
739 }
740
741 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
742
743 /* make sure all activity has settled down first */
744
745 if (atomic_read(&ch->references) > 0) {
746 return;
747 }
748 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
749
Dean Nelsona607c382005-09-01 14:01:37 -0500750 if (part->act_state == XPC_P_DEACTIVATING) {
751 /* can't proceed until the other side disengages from us */
752 if (xpc_partition_engaged(1UL << ch->partid)) {
753 return;
754 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700755
Dean Nelsona607c382005-09-01 14:01:37 -0500756 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700757
758 /* as long as the other side is up do the full protocol */
759
760 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
761 return;
762 }
763
764 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
765 ch->flags |= XPC_C_CLOSEREPLY;
766 xpc_IPI_send_closereply(ch, irq_flags);
767 }
768
769 if (!(ch->flags & XPC_C_RCLOSEREPLY)) {
770 return;
771 }
772 }
773
Dean Nelsona607c382005-09-01 14:01:37 -0500774 /* wake those waiting for notify completion */
775 if (atomic_read(&ch->n_to_notify) > 0) {
776 /* >>> we do callout while holding ch->lock */
777 xpc_notify_senders(ch, ch->reason, ch->w_local_GP.put);
778 }
779
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700780 /* both sides are disconnected now */
781
Dean Nelsona607c382005-09-01 14:01:37 -0500782 /* it's now safe to free the channel's message queues */
783 xpc_free_msgqueues(ch);
784
785 /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
786 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700787
788 atomic_dec(&part->nchannels_active);
789
Dean Nelsona607c382005-09-01 14:01:37 -0500790 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700791 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
792 "reason=%d\n", ch->number, ch->partid, ch->reason);
793 }
Dean Nelsona607c382005-09-01 14:01:37 -0500794
795 /* wake the thread that is waiting for this channel to disconnect */
796 if (ch->flags & XPC_C_WDISCONNECT) {
797 spin_unlock_irqrestore(&ch->lock, *irq_flags);
798 up(&ch->wdisconnect_sema);
799 spin_lock_irqsave(&ch->lock, *irq_flags);
800 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700801}
802
803
804/*
805 * Process a change in the channel's remote connection state.
806 */
807static void
808xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
809 u8 IPI_flags)
810{
811 unsigned long irq_flags;
812 struct xpc_openclose_args *args =
813 &part->remote_openclose_args[ch_number];
814 struct xpc_channel *ch = &part->channels[ch_number];
815 enum xpc_retval reason;
816
817
818
819 spin_lock_irqsave(&ch->lock, irq_flags);
820
821
822 if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
823
824 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
825 "from partid=%d, channel=%d\n", args->reason,
826 ch->partid, ch->number);
827
828 /*
829 * If RCLOSEREQUEST is set, we're probably waiting for
830 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelsona607c382005-09-01 14:01:37 -0500831 * with this RCLOSEREQUEST in the IPI_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700832 */
833
834 if (ch->flags & XPC_C_RCLOSEREQUEST) {
835 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
836 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
837 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
838 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
839
840 DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
841 IPI_flags &= ~XPC_IPI_CLOSEREPLY;
842 ch->flags |= XPC_C_RCLOSEREPLY;
843
844 /* both sides have finished disconnecting */
845 xpc_process_disconnect(ch, &irq_flags);
846 }
847
848 if (ch->flags & XPC_C_DISCONNECTED) {
849 // >>> explain this section
850
851 if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
852 DBUG_ON(part->act_state !=
853 XPC_P_DEACTIVATING);
854 spin_unlock_irqrestore(&ch->lock, irq_flags);
855 return;
856 }
857
858 XPC_SET_REASON(ch, 0, 0);
859 ch->flags &= ~XPC_C_DISCONNECTED;
860
861 atomic_inc(&part->nchannels_active);
862 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
863 }
864
865 IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
866
867 /*
868 * The meaningful CLOSEREQUEST connection state fields are:
869 * reason = reason connection is to be closed
870 */
871
872 ch->flags |= XPC_C_RCLOSEREQUEST;
873
874 if (!(ch->flags & XPC_C_DISCONNECTING)) {
875 reason = args->reason;
876 if (reason <= xpcSuccess || reason > xpcUnknownReason) {
877 reason = xpcUnknownReason;
878 } else if (reason == xpcUnregistering) {
879 reason = xpcOtherUnregistering;
880 }
881
882 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
883 } else {
884 xpc_process_disconnect(ch, &irq_flags);
885 }
886 }
887
888
889 if (IPI_flags & XPC_IPI_CLOSEREPLY) {
890
891 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
892 " channel=%d\n", ch->partid, ch->number);
893
894 if (ch->flags & XPC_C_DISCONNECTED) {
895 DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
896 spin_unlock_irqrestore(&ch->lock, irq_flags);
897 return;
898 }
899
900 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
901 DBUG_ON(!(ch->flags & XPC_C_RCLOSEREQUEST));
902
903 ch->flags |= XPC_C_RCLOSEREPLY;
904
905 if (ch->flags & XPC_C_CLOSEREPLY) {
906 /* both sides have finished disconnecting */
907 xpc_process_disconnect(ch, &irq_flags);
908 }
909 }
910
911
912 if (IPI_flags & XPC_IPI_OPENREQUEST) {
913
914 dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
915 "local_nentries=%d) received from partid=%d, "
916 "channel=%d\n", args->msg_size, args->local_nentries,
917 ch->partid, ch->number);
918
Dean Nelsona607c382005-09-01 14:01:37 -0500919 if ((ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) ||
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700920 part->act_state == XPC_P_DEACTIVATING) {
921 spin_unlock_irqrestore(&ch->lock, irq_flags);
922 return;
923 }
924 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
925 XPC_C_OPENREQUEST)));
926 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
927 XPC_C_OPENREPLY | XPC_C_CONNECTED));
928
929 /*
930 * The meaningful OPENREQUEST connection state fields are:
931 * msg_size = size of channel's messages in bytes
932 * local_nentries = remote partition's local_nentries
933 */
934 DBUG_ON(args->msg_size == 0);
935 DBUG_ON(args->local_nentries == 0);
936
937 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
938 ch->remote_nentries = args->local_nentries;
939
940
941 if (ch->flags & XPC_C_OPENREQUEST) {
942 if (args->msg_size != ch->msg_size) {
943 XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
944 &irq_flags);
945 spin_unlock_irqrestore(&ch->lock, irq_flags);
946 return;
947 }
948 } else {
949 ch->msg_size = args->msg_size;
950
951 XPC_SET_REASON(ch, 0, 0);
952 ch->flags &= ~XPC_C_DISCONNECTED;
953
954 atomic_inc(&part->nchannels_active);
955 }
956
957 xpc_process_connect(ch, &irq_flags);
958 }
959
960
961 if (IPI_flags & XPC_IPI_OPENREPLY) {
962
963 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
964 "local_nentries=%d, remote_nentries=%d) received from "
965 "partid=%d, channel=%d\n", args->local_msgqueue_pa,
966 args->local_nentries, args->remote_nentries,
967 ch->partid, ch->number);
968
969 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
970 spin_unlock_irqrestore(&ch->lock, irq_flags);
971 return;
972 }
973 DBUG_ON(!(ch->flags & XPC_C_OPENREQUEST));
974 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
975 DBUG_ON(ch->flags & XPC_C_CONNECTED);
976
977 /*
978 * The meaningful OPENREPLY connection state fields are:
979 * local_msgqueue_pa = physical address of remote
980 * partition's local_msgqueue
981 * local_nentries = remote partition's local_nentries
982 * remote_nentries = remote partition's remote_nentries
983 */
984 DBUG_ON(args->local_msgqueue_pa == 0);
985 DBUG_ON(args->local_nentries == 0);
986 DBUG_ON(args->remote_nentries == 0);
987
988 ch->flags |= XPC_C_ROPENREPLY;
989 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
990
991 if (args->local_nentries < ch->remote_nentries) {
992 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
993 "remote_nentries=%d, old remote_nentries=%d, "
994 "partid=%d, channel=%d\n",
995 args->local_nentries, ch->remote_nentries,
996 ch->partid, ch->number);
997
998 ch->remote_nentries = args->local_nentries;
999 }
1000 if (args->remote_nentries < ch->local_nentries) {
1001 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1002 "local_nentries=%d, old local_nentries=%d, "
1003 "partid=%d, channel=%d\n",
1004 args->remote_nentries, ch->local_nentries,
1005 ch->partid, ch->number);
1006
1007 ch->local_nentries = args->remote_nentries;
1008 }
1009
1010 xpc_process_connect(ch, &irq_flags);
1011 }
1012
1013 spin_unlock_irqrestore(&ch->lock, irq_flags);
1014}
1015
1016
1017/*
1018 * Attempt to establish a channel connection to a remote partition.
1019 */
1020static enum xpc_retval
1021xpc_connect_channel(struct xpc_channel *ch)
1022{
1023 unsigned long irq_flags;
1024 struct xpc_registration *registration = &xpc_registrations[ch->number];
1025
1026
1027 if (down_interruptible(&registration->sema) != 0) {
1028 return xpcInterrupted;
1029 }
1030
1031 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
1032 up(&registration->sema);
1033 return xpcUnregistered;
1034 }
1035
1036 spin_lock_irqsave(&ch->lock, irq_flags);
1037
1038 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1039 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
1040
1041 if (ch->flags & XPC_C_DISCONNECTING) {
1042 spin_unlock_irqrestore(&ch->lock, irq_flags);
1043 up(&registration->sema);
1044 return ch->reason;
1045 }
1046
1047
1048 /* add info from the channel connect registration to the channel */
1049
1050 ch->kthreads_assigned_limit = registration->assigned_limit;
1051 ch->kthreads_idle_limit = registration->idle_limit;
1052 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
1053 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
1054 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
1055
1056 ch->func = registration->func;
1057 DBUG_ON(registration->func == NULL);
1058 ch->key = registration->key;
1059
1060 ch->local_nentries = registration->nentries;
1061
1062 if (ch->flags & XPC_C_ROPENREQUEST) {
1063 if (registration->msg_size != ch->msg_size) {
1064 /* the local and remote sides aren't the same */
1065
1066 /*
1067 * Because XPC_DISCONNECT_CHANNEL() can block we're
1068 * forced to up the registration sema before we unlock
1069 * the channel lock. But that's okay here because we're
1070 * done with the part that required the registration
1071 * sema. XPC_DISCONNECT_CHANNEL() requires that the
1072 * channel lock be locked and will unlock and relock
1073 * the channel lock as needed.
1074 */
1075 up(&registration->sema);
1076 XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
1077 &irq_flags);
1078 spin_unlock_irqrestore(&ch->lock, irq_flags);
1079 return xpcUnequalMsgSizes;
1080 }
1081 } else {
1082 ch->msg_size = registration->msg_size;
1083
1084 XPC_SET_REASON(ch, 0, 0);
1085 ch->flags &= ~XPC_C_DISCONNECTED;
1086
1087 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
1088 }
1089
1090 up(&registration->sema);
1091
1092
1093 /* initiate the connection */
1094
1095 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
1096 xpc_IPI_send_openrequest(ch, &irq_flags);
1097
1098 xpc_process_connect(ch, &irq_flags);
1099
1100 spin_unlock_irqrestore(&ch->lock, irq_flags);
1101
1102 return xpcSuccess;
1103}
1104
1105
1106/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001107 * Clear some of the msg flags in the local message queue.
1108 */
1109static inline void
1110xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
1111{
1112 struct xpc_msg *msg;
1113 s64 get;
1114
1115
1116 get = ch->w_remote_GP.get;
1117 do {
1118 msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
1119 (get % ch->local_nentries) * ch->msg_size);
1120 msg->flags = 0;
1121 } while (++get < (volatile s64) ch->remote_GP.get);
1122}
1123
1124
1125/*
1126 * Clear some of the msg flags in the remote message queue.
1127 */
1128static inline void
1129xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
1130{
1131 struct xpc_msg *msg;
1132 s64 put;
1133
1134
1135 put = ch->w_remote_GP.put;
1136 do {
1137 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
1138 (put % ch->remote_nentries) * ch->msg_size);
1139 msg->flags = 0;
1140 } while (++put < (volatile s64) ch->remote_GP.put);
1141}
1142
1143
1144static void
1145xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
1146{
1147 struct xpc_channel *ch = &part->channels[ch_number];
1148 int nmsgs_sent;
1149
1150
1151 ch->remote_GP = part->remote_GPs[ch_number];
1152
1153
1154 /* See what, if anything, has changed for each connected channel */
1155
1156 xpc_msgqueue_ref(ch);
1157
1158 if (ch->w_remote_GP.get == ch->remote_GP.get &&
1159 ch->w_remote_GP.put == ch->remote_GP.put) {
1160 /* nothing changed since GPs were last pulled */
1161 xpc_msgqueue_deref(ch);
1162 return;
1163 }
1164
1165 if (!(ch->flags & XPC_C_CONNECTED)){
1166 xpc_msgqueue_deref(ch);
1167 return;
1168 }
1169
1170
1171 /*
1172 * First check to see if messages recently sent by us have been
1173 * received by the other side. (The remote GET value will have
1174 * changed since we last looked at it.)
1175 */
1176
1177 if (ch->w_remote_GP.get != ch->remote_GP.get) {
1178
1179 /*
1180 * We need to notify any senders that want to be notified
1181 * that their sent messages have been received by their
1182 * intended recipients. We need to do this before updating
1183 * w_remote_GP.get so that we don't allocate the same message
1184 * queue entries prematurely (see xpc_allocate_msg()).
1185 */
1186 if (atomic_read(&ch->n_to_notify) > 0) {
1187 /*
1188 * Notify senders that messages sent have been
1189 * received and delivered by the other side.
1190 */
1191 xpc_notify_senders(ch, xpcMsgDelivered,
1192 ch->remote_GP.get);
1193 }
1194
1195 /*
1196 * Clear msg->flags in previously sent messages, so that
1197 * they're ready for xpc_allocate_msg().
1198 */
1199 xpc_clear_local_msgqueue_flags(ch);
1200
Dave Jones821fe942005-06-25 14:54:29 -07001201 ch->w_remote_GP.get = ch->remote_GP.get;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001202
1203 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1204 "channel=%d\n", ch->w_remote_GP.get, ch->partid,
1205 ch->number);
1206
1207 /*
1208 * If anyone was waiting for message queue entries to become
1209 * available, wake them up.
1210 */
1211 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
1212 wake_up(&ch->msg_allocate_wq);
1213 }
1214 }
1215
1216
1217 /*
1218 * Now check for newly sent messages by the other side. (The remote
1219 * PUT value will have changed since we last looked at it.)
1220 */
1221
1222 if (ch->w_remote_GP.put != ch->remote_GP.put) {
1223 /*
1224 * Clear msg->flags in previously received messages, so that
1225 * they're ready for xpc_get_deliverable_msg().
1226 */
1227 xpc_clear_remote_msgqueue_flags(ch);
1228
Dave Jones821fe942005-06-25 14:54:29 -07001229 ch->w_remote_GP.put = ch->remote_GP.put;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001230
1231 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1232 "channel=%d\n", ch->w_remote_GP.put, ch->partid,
1233 ch->number);
1234
1235 nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
1236 if (nmsgs_sent > 0) {
1237 dev_dbg(xpc_chan, "msgs waiting to be copied and "
1238 "delivered=%d, partid=%d, channel=%d\n",
1239 nmsgs_sent, ch->partid, ch->number);
1240
1241 if (ch->flags & XPC_C_CONNECTCALLOUT) {
1242 xpc_activate_kthreads(ch, nmsgs_sent);
1243 }
1244 }
1245 }
1246
1247 xpc_msgqueue_deref(ch);
1248}
1249
1250
1251void
1252xpc_process_channel_activity(struct xpc_partition *part)
1253{
1254 unsigned long irq_flags;
1255 u64 IPI_amo, IPI_flags;
1256 struct xpc_channel *ch;
1257 int ch_number;
Dean Nelsona607c382005-09-01 14:01:37 -05001258 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001259
1260
1261 IPI_amo = xpc_get_IPI_flags(part);
1262
1263 /*
1264 * Initiate channel connections for registered channels.
1265 *
1266 * For each connected channel that has pending messages activate idle
1267 * kthreads and/or create new kthreads as needed.
1268 */
1269
1270 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1271 ch = &part->channels[ch_number];
1272
1273
1274 /*
1275 * Process any open or close related IPI flags, and then deal
1276 * with connecting or disconnecting the channel as required.
1277 */
1278
1279 IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
1280
1281 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags)) {
1282 xpc_process_openclose_IPI(part, ch_number, IPI_flags);
1283 }
1284
Dean Nelsona607c382005-09-01 14:01:37 -05001285 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001286
Dean Nelsona607c382005-09-01 14:01:37 -05001287 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001288 spin_lock_irqsave(&ch->lock, irq_flags);
1289 xpc_process_disconnect(ch, &irq_flags);
1290 spin_unlock_irqrestore(&ch->lock, irq_flags);
1291 continue;
1292 }
1293
1294 if (part->act_state == XPC_P_DEACTIVATING) {
1295 continue;
1296 }
1297
Dean Nelsona607c382005-09-01 14:01:37 -05001298 if (!(ch_flags & XPC_C_CONNECTED)) {
1299 if (!(ch_flags & XPC_C_OPENREQUEST)) {
1300 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001301 (void) xpc_connect_channel(ch);
1302 } else {
1303 spin_lock_irqsave(&ch->lock, irq_flags);
1304 xpc_process_connect(ch, &irq_flags);
1305 spin_unlock_irqrestore(&ch->lock, irq_flags);
1306 }
1307 continue;
1308 }
1309
1310
1311 /*
1312 * Process any message related IPI flags, this may involve the
1313 * activation of kthreads to deliver any pending messages sent
1314 * from the other partition.
1315 */
1316
1317 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags)) {
1318 xpc_process_msg_IPI(part, ch_number);
1319 }
1320 }
1321}
1322
1323
1324/*
Dean Nelsona607c382005-09-01 14:01:37 -05001325 * XPC's heartbeat code calls this function to inform XPC that a partition is
1326 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001327 * infrastructure used for the just downed partition.
1328 *
1329 * XPC's heartbeat code will never call this function and xpc_partition_up()
1330 * at the same time. Nor will it ever make multiple calls to either function
1331 * at the same time.
1332 */
1333void
Dean Nelsona607c382005-09-01 14:01:37 -05001334xpc_partition_going_down(struct xpc_partition *part, enum xpc_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001335{
1336 unsigned long irq_flags;
1337 int ch_number;
1338 struct xpc_channel *ch;
1339
1340
1341 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
1342 XPC_PARTID(part), reason);
1343
1344 if (!xpc_part_ref(part)) {
1345 /* infrastructure for this partition isn't currently set up */
1346 return;
1347 }
1348
1349
Dean Nelsona607c382005-09-01 14:01:37 -05001350 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001351
1352 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1353 ch = &part->channels[ch_number];
1354
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001355 xpc_msgqueue_ref(ch);
1356 spin_lock_irqsave(&ch->lock, irq_flags);
1357
1358 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
1359
1360 spin_unlock_irqrestore(&ch->lock, irq_flags);
1361 xpc_msgqueue_deref(ch);
1362 }
1363
1364 xpc_wakeup_channel_mgr(part);
1365
1366 xpc_part_deref(part);
1367}
1368
1369
1370/*
1371 * Teardown the infrastructure necessary to support XPartition Communication
1372 * between the specified remote partition and the local one.
1373 */
1374void
1375xpc_teardown_infrastructure(struct xpc_partition *part)
1376{
1377 partid_t partid = XPC_PARTID(part);
1378
1379
1380 /*
1381 * We start off by making this partition inaccessible to local
1382 * processes by marking it as no longer setup. Then we make it
1383 * inaccessible to remote processes by clearing the XPC per partition
1384 * specific variable's magic # (which indicates that these variables
1385 * are no longer valid) and by ignoring all XPC notify IPIs sent to
1386 * this partition.
1387 */
1388
Dean Nelsona607c382005-09-01 14:01:37 -05001389 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001390 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1391 DBUG_ON(part->setup_state != XPC_P_SETUP);
1392 part->setup_state = XPC_P_WTEARDOWN;
1393
1394 xpc_vars_part[partid].magic = 0;
1395
1396
1397 free_irq(SGI_XPC_NOTIFY, (void *) (u64) partid);
1398
1399
1400 /*
1401 * Before proceding with the teardown we have to wait until all
1402 * existing references cease.
1403 */
1404 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1405
1406
1407 /* now we can begin tearing down the infrastructure */
1408
1409 part->setup_state = XPC_P_TORNDOWN;
1410
1411 /* in case we've still got outstanding timers registered... */
1412 del_timer_sync(&part->dropped_IPI_timer);
1413
1414 kfree(part->remote_openclose_args_base);
1415 part->remote_openclose_args = NULL;
1416 kfree(part->local_openclose_args_base);
1417 part->local_openclose_args = NULL;
1418 kfree(part->remote_GPs_base);
1419 part->remote_GPs = NULL;
1420 kfree(part->local_GPs_base);
1421 part->local_GPs = NULL;
1422 kfree(part->channels);
1423 part->channels = NULL;
1424 part->local_IPI_amo_va = NULL;
1425}
1426
1427
1428/*
1429 * Called by XP at the time of channel connection registration to cause
1430 * XPC to establish connections to all currently active partitions.
1431 */
1432void
1433xpc_initiate_connect(int ch_number)
1434{
1435 partid_t partid;
1436 struct xpc_partition *part;
1437 struct xpc_channel *ch;
1438
1439
1440 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1441
1442 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1443 part = &xpc_partitions[partid];
1444
1445 if (xpc_part_ref(part)) {
1446 ch = &part->channels[ch_number];
1447
1448 if (!(ch->flags & XPC_C_DISCONNECTING)) {
1449 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
1450 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1451 DBUG_ON(ch->flags & XPC_C_SETUP);
1452
1453 /*
1454 * Initiate the establishment of a connection
1455 * on the newly registered channel to the
1456 * remote partition.
1457 */
1458 xpc_wakeup_channel_mgr(part);
1459 }
1460
1461 xpc_part_deref(part);
1462 }
1463 }
1464}
1465
1466
1467void
1468xpc_connected_callout(struct xpc_channel *ch)
1469{
1470 unsigned long irq_flags;
1471
1472
1473 /* let the registerer know that a connection has been established */
1474
1475 if (ch->func != NULL) {
1476 dev_dbg(xpc_chan, "ch->func() called, reason=xpcConnected, "
1477 "partid=%d, channel=%d\n", ch->partid, ch->number);
1478
1479 ch->func(xpcConnected, ch->partid, ch->number,
1480 (void *) (u64) ch->local_nentries, ch->key);
1481
1482 dev_dbg(xpc_chan, "ch->func() returned, reason=xpcConnected, "
1483 "partid=%d, channel=%d\n", ch->partid, ch->number);
1484 }
1485
1486 spin_lock_irqsave(&ch->lock, irq_flags);
1487 ch->flags |= XPC_C_CONNECTCALLOUT;
1488 spin_unlock_irqrestore(&ch->lock, irq_flags);
1489}
1490
1491
1492/*
1493 * Called by XP at the time of channel connection unregistration to cause
1494 * XPC to teardown all current connections for the specified channel.
1495 *
1496 * Before returning xpc_initiate_disconnect() will wait until all connections
1497 * on the specified channel have been closed/torndown. So the caller can be
1498 * assured that they will not be receiving any more callouts from XPC to the
1499 * function they registered via xpc_connect().
1500 *
1501 * Arguments:
1502 *
1503 * ch_number - channel # to unregister.
1504 */
1505void
1506xpc_initiate_disconnect(int ch_number)
1507{
1508 unsigned long irq_flags;
1509 partid_t partid;
1510 struct xpc_partition *part;
1511 struct xpc_channel *ch;
1512
1513
1514 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1515
1516 /* initiate the channel disconnect for every active partition */
1517 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1518 part = &xpc_partitions[partid];
1519
1520 if (xpc_part_ref(part)) {
1521 ch = &part->channels[ch_number];
1522 xpc_msgqueue_ref(ch);
1523
1524 spin_lock_irqsave(&ch->lock, irq_flags);
1525
Dean Nelsona607c382005-09-01 14:01:37 -05001526 if (!(ch->flags & XPC_C_DISCONNECTED)) {
1527 ch->flags |= XPC_C_WDISCONNECT;
1528
1529 XPC_DISCONNECT_CHANNEL(ch, xpcUnregistering,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001530 &irq_flags);
Dean Nelsona607c382005-09-01 14:01:37 -05001531 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001532
1533 spin_unlock_irqrestore(&ch->lock, irq_flags);
1534
1535 xpc_msgqueue_deref(ch);
1536 xpc_part_deref(part);
1537 }
1538 }
1539
1540 xpc_disconnect_wait(ch_number);
1541}
1542
1543
1544/*
1545 * To disconnect a channel, and reflect it back to all who may be waiting.
1546 *
Dean Nelsona607c382005-09-01 14:01:37 -05001547 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
1548 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
1549 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001550 *
1551 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
1552 */
1553void
1554xpc_disconnect_channel(const int line, struct xpc_channel *ch,
1555 enum xpc_retval reason, unsigned long *irq_flags)
1556{
Dean Nelsona607c382005-09-01 14:01:37 -05001557 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001558
1559
1560 DBUG_ON(!spin_is_locked(&ch->lock));
1561
1562 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
1563 return;
1564 }
1565 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
1566
1567 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
1568 reason, line, ch->partid, ch->number);
1569
1570 XPC_SET_REASON(ch, reason, line);
1571
Dean Nelsona607c382005-09-01 14:01:37 -05001572 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001573 /* some of these may not have been set */
1574 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
1575 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
1576 XPC_C_CONNECTING | XPC_C_CONNECTED);
1577
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001578 xpc_IPI_send_closerequest(ch, irq_flags);
1579
Dean Nelsona607c382005-09-01 14:01:37 -05001580 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001581 ch->flags |= XPC_C_WASCONNECTED;
1582 }
1583
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001584 spin_unlock_irqrestore(&ch->lock, *irq_flags);
1585
Dean Nelsona607c382005-09-01 14:01:37 -05001586 /* wake all idle kthreads so they can exit */
1587 if (atomic_read(&ch->kthreads_idle) > 0) {
1588 wake_up_all(&ch->idle_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001589 }
1590
Dean Nelsona607c382005-09-01 14:01:37 -05001591 /* wake those waiting to allocate an entry from the local msg queue */
1592 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
1593 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001594 }
1595
1596 spin_lock_irqsave(&ch->lock, *irq_flags);
1597}
1598
1599
1600void
Dean Nelsona607c382005-09-01 14:01:37 -05001601xpc_disconnecting_callout(struct xpc_channel *ch)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001602{
1603 /*
Dean Nelsona607c382005-09-01 14:01:37 -05001604 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001605 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c382005-09-01 14:01:37 -05001606 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001607 */
1608
1609 if (ch->func != NULL) {
Dean Nelsona607c382005-09-01 14:01:37 -05001610 dev_dbg(xpc_chan, "ch->func() called, reason=xpcDisconnecting,"
1611 " partid=%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001612
Dean Nelsona607c382005-09-01 14:01:37 -05001613 ch->func(xpcDisconnecting, ch->partid, ch->number, NULL,
1614 ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001615
Dean Nelsona607c382005-09-01 14:01:37 -05001616 dev_dbg(xpc_chan, "ch->func() returned, reason="
1617 "xpcDisconnecting, partid=%d, channel=%d\n",
1618 ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001619 }
1620}
1621
1622
1623/*
1624 * Wait for a message entry to become available for the specified channel,
1625 * but don't wait any longer than 1 jiffy.
1626 */
1627static enum xpc_retval
1628xpc_allocate_msg_wait(struct xpc_channel *ch)
1629{
1630 enum xpc_retval ret;
1631
1632
1633 if (ch->flags & XPC_C_DISCONNECTING) {
1634 DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
1635 return ch->reason;
1636 }
1637
1638 atomic_inc(&ch->n_on_msg_allocate_wq);
1639 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
1640 atomic_dec(&ch->n_on_msg_allocate_wq);
1641
1642 if (ch->flags & XPC_C_DISCONNECTING) {
1643 ret = ch->reason;
1644 DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
1645 } else if (ret == 0) {
1646 ret = xpcTimeout;
1647 } else {
1648 ret = xpcInterrupted;
1649 }
1650
1651 return ret;
1652}
1653
1654
1655/*
1656 * Allocate an entry for a message from the message queue associated with the
1657 * specified channel.
1658 */
1659static enum xpc_retval
1660xpc_allocate_msg(struct xpc_channel *ch, u32 flags,
1661 struct xpc_msg **address_of_msg)
1662{
1663 struct xpc_msg *msg;
1664 enum xpc_retval ret;
1665 s64 put;
1666
1667
1668 /* this reference will be dropped in xpc_send_msg() */
1669 xpc_msgqueue_ref(ch);
1670
1671 if (ch->flags & XPC_C_DISCONNECTING) {
1672 xpc_msgqueue_deref(ch);
1673 return ch->reason;
1674 }
1675 if (!(ch->flags & XPC_C_CONNECTED)) {
1676 xpc_msgqueue_deref(ch);
1677 return xpcNotConnected;
1678 }
1679
1680
1681 /*
1682 * Get the next available message entry from the local message queue.
1683 * If none are available, we'll make sure that we grab the latest
1684 * GP values.
1685 */
1686 ret = xpcTimeout;
1687
1688 while (1) {
1689
1690 put = (volatile s64) ch->w_local_GP.put;
1691 if (put - (volatile s64) ch->w_remote_GP.get <
1692 ch->local_nentries) {
1693
1694 /* There are available message entries. We need to try
1695 * to secure one for ourselves. We'll do this by trying
1696 * to increment w_local_GP.put as long as someone else
1697 * doesn't beat us to it. If they do, we'll have to
1698 * try again.
1699 */
1700 if (cmpxchg(&ch->w_local_GP.put, put, put + 1) ==
1701 put) {
1702 /* we got the entry referenced by put */
1703 break;
1704 }
1705 continue; /* try again */
1706 }
1707
1708
1709 /*
1710 * There aren't any available msg entries at this time.
1711 *
1712 * In waiting for a message entry to become available,
1713 * we set a timeout in case the other side is not
1714 * sending completion IPIs. This lets us fake an IPI
1715 * that will cause the IPI handler to fetch the latest
1716 * GP values as if an IPI was sent by the other side.
1717 */
1718 if (ret == xpcTimeout) {
1719 xpc_IPI_send_local_msgrequest(ch);
1720 }
1721
1722 if (flags & XPC_NOWAIT) {
1723 xpc_msgqueue_deref(ch);
1724 return xpcNoWait;
1725 }
1726
1727 ret = xpc_allocate_msg_wait(ch);
1728 if (ret != xpcInterrupted && ret != xpcTimeout) {
1729 xpc_msgqueue_deref(ch);
1730 return ret;
1731 }
1732 }
1733
1734
1735 /* get the message's address and initialize it */
1736 msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
1737 (put % ch->local_nentries) * ch->msg_size);
1738
1739
1740 DBUG_ON(msg->flags != 0);
1741 msg->number = put;
1742
1743 dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
1744 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
1745 (void *) msg, msg->number, ch->partid, ch->number);
1746
1747 *address_of_msg = msg;
1748
1749 return xpcSuccess;
1750}
1751
1752
1753/*
1754 * Allocate an entry for a message from the message queue associated with the
1755 * specified channel. NOTE that this routine can sleep waiting for a message
1756 * entry to become available. To not sleep, pass in the XPC_NOWAIT flag.
1757 *
1758 * Arguments:
1759 *
1760 * partid - ID of partition to which the channel is connected.
1761 * ch_number - channel #.
1762 * flags - see xpc.h for valid flags.
1763 * payload - address of the allocated payload area pointer (filled in on
1764 * return) in which the user-defined message is constructed.
1765 */
1766enum xpc_retval
1767xpc_initiate_allocate(partid_t partid, int ch_number, u32 flags, void **payload)
1768{
1769 struct xpc_partition *part = &xpc_partitions[partid];
1770 enum xpc_retval ret = xpcUnknownReason;
1771 struct xpc_msg *msg;
1772
1773
1774 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
1775 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1776
1777 *payload = NULL;
1778
1779 if (xpc_part_ref(part)) {
1780 ret = xpc_allocate_msg(&part->channels[ch_number], flags, &msg);
1781 xpc_part_deref(part);
1782
1783 if (msg != NULL) {
1784 *payload = &msg->payload;
1785 }
1786 }
1787
1788 return ret;
1789}
1790
1791
1792/*
1793 * Now we actually send the messages that are ready to be sent by advancing
1794 * the local message queue's Put value and then send an IPI to the recipient
1795 * partition.
1796 */
1797static void
1798xpc_send_msgs(struct xpc_channel *ch, s64 initial_put)
1799{
1800 struct xpc_msg *msg;
1801 s64 put = initial_put + 1;
1802 int send_IPI = 0;
1803
1804
1805 while (1) {
1806
1807 while (1) {
1808 if (put == (volatile s64) ch->w_local_GP.put) {
1809 break;
1810 }
1811
1812 msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
1813 (put % ch->local_nentries) * ch->msg_size);
1814
1815 if (!(msg->flags & XPC_M_READY)) {
1816 break;
1817 }
1818
1819 put++;
1820 }
1821
1822 if (put == initial_put) {
1823 /* nothing's changed */
1824 break;
1825 }
1826
1827 if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) !=
1828 initial_put) {
1829 /* someone else beat us to it */
1830 DBUG_ON((volatile s64) ch->local_GP->put < initial_put);
1831 break;
1832 }
1833
1834 /* we just set the new value of local_GP->put */
1835
1836 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
1837 "channel=%d\n", put, ch->partid, ch->number);
1838
1839 send_IPI = 1;
1840
1841 /*
1842 * We need to ensure that the message referenced by
1843 * local_GP->put is not XPC_M_READY or that local_GP->put
1844 * equals w_local_GP.put, so we'll go have a look.
1845 */
1846 initial_put = put;
1847 }
1848
1849 if (send_IPI) {
1850 xpc_IPI_send_msgrequest(ch);
1851 }
1852}
1853
1854
1855/*
1856 * Common code that does the actual sending of the message by advancing the
1857 * local message queue's Put value and sends an IPI to the partition the
1858 * message is being sent to.
1859 */
1860static enum xpc_retval
1861xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type,
1862 xpc_notify_func func, void *key)
1863{
1864 enum xpc_retval ret = xpcSuccess;
Dean Nelsona607c382005-09-01 14:01:37 -05001865 struct xpc_notify *notify = notify;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001866 s64 put, msg_number = msg->number;
1867
1868
1869 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
1870 DBUG_ON((((u64) msg - (u64) ch->local_msgqueue) / ch->msg_size) !=
1871 msg_number % ch->local_nentries);
1872 DBUG_ON(msg->flags & XPC_M_READY);
1873
1874 if (ch->flags & XPC_C_DISCONNECTING) {
1875 /* drop the reference grabbed in xpc_allocate_msg() */
1876 xpc_msgqueue_deref(ch);
1877 return ch->reason;
1878 }
1879
1880 if (notify_type != 0) {
1881 /*
1882 * Tell the remote side to send an ACK interrupt when the
1883 * message has been delivered.
1884 */
1885 msg->flags |= XPC_M_INTERRUPT;
1886
1887 atomic_inc(&ch->n_to_notify);
1888
1889 notify = &ch->notify_queue[msg_number % ch->local_nentries];
1890 notify->func = func;
1891 notify->key = key;
Dave Jones821fe942005-06-25 14:54:29 -07001892 notify->type = notify_type;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001893
1894 // >>> is a mb() needed here?
1895
1896 if (ch->flags & XPC_C_DISCONNECTING) {
1897 /*
1898 * An error occurred between our last error check and
1899 * this one. We will try to clear the type field from
1900 * the notify entry. If we succeed then
1901 * xpc_disconnect_channel() didn't already process
1902 * the notify entry.
1903 */
1904 if (cmpxchg(&notify->type, notify_type, 0) ==
1905 notify_type) {
1906 atomic_dec(&ch->n_to_notify);
1907 ret = ch->reason;
1908 }
1909
1910 /* drop the reference grabbed in xpc_allocate_msg() */
1911 xpc_msgqueue_deref(ch);
1912 return ret;
1913 }
1914 }
1915
1916 msg->flags |= XPC_M_READY;
1917
1918 /*
1919 * The preceding store of msg->flags must occur before the following
1920 * load of ch->local_GP->put.
1921 */
1922 mb();
1923
1924 /* see if the message is next in line to be sent, if so send it */
1925
1926 put = ch->local_GP->put;
1927 if (put == msg_number) {
1928 xpc_send_msgs(ch, put);
1929 }
1930
1931 /* drop the reference grabbed in xpc_allocate_msg() */
1932 xpc_msgqueue_deref(ch);
1933 return ret;
1934}
1935
1936
1937/*
1938 * Send a message previously allocated using xpc_initiate_allocate() on the
1939 * specified channel connected to the specified partition.
1940 *
1941 * This routine will not wait for the message to be received, nor will
1942 * notification be given when it does happen. Once this routine has returned
1943 * the message entry allocated via xpc_initiate_allocate() is no longer
1944 * accessable to the caller.
1945 *
1946 * This routine, although called by users, does not call xpc_part_ref() to
1947 * ensure that the partition infrastructure is in place. It relies on the
1948 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
1949 *
1950 * Arguments:
1951 *
1952 * partid - ID of partition to which the channel is connected.
1953 * ch_number - channel # to send message on.
1954 * payload - pointer to the payload area allocated via
1955 * xpc_initiate_allocate().
1956 */
1957enum xpc_retval
1958xpc_initiate_send(partid_t partid, int ch_number, void *payload)
1959{
1960 struct xpc_partition *part = &xpc_partitions[partid];
1961 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
1962 enum xpc_retval ret;
1963
1964
1965 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
1966 partid, ch_number);
1967
1968 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
1969 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1970 DBUG_ON(msg == NULL);
1971
1972 ret = xpc_send_msg(&part->channels[ch_number], msg, 0, NULL, NULL);
1973
1974 return ret;
1975}
1976
1977
1978/*
1979 * Send a message previously allocated using xpc_initiate_allocate on the
1980 * specified channel connected to the specified partition.
1981 *
1982 * This routine will not wait for the message to be sent. Once this routine
1983 * has returned the message entry allocated via xpc_initiate_allocate() is no
1984 * longer accessable to the caller.
1985 *
1986 * Once the remote end of the channel has received the message, the function
1987 * passed as an argument to xpc_initiate_send_notify() will be called. This
1988 * allows the sender to free up or re-use any buffers referenced by the
1989 * message, but does NOT mean the message has been processed at the remote
1990 * end by a receiver.
1991 *
1992 * If this routine returns an error, the caller's function will NOT be called.
1993 *
1994 * This routine, although called by users, does not call xpc_part_ref() to
1995 * ensure that the partition infrastructure is in place. It relies on the
1996 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
1997 *
1998 * Arguments:
1999 *
2000 * partid - ID of partition to which the channel is connected.
2001 * ch_number - channel # to send message on.
2002 * payload - pointer to the payload area allocated via
2003 * xpc_initiate_allocate().
2004 * func - function to call with asynchronous notification of message
2005 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
2006 * key - user-defined key to be passed to the function when it's called.
2007 */
2008enum xpc_retval
2009xpc_initiate_send_notify(partid_t partid, int ch_number, void *payload,
2010 xpc_notify_func func, void *key)
2011{
2012 struct xpc_partition *part = &xpc_partitions[partid];
2013 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2014 enum xpc_retval ret;
2015
2016
2017 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
2018 partid, ch_number);
2019
2020 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2021 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2022 DBUG_ON(msg == NULL);
2023 DBUG_ON(func == NULL);
2024
2025 ret = xpc_send_msg(&part->channels[ch_number], msg, XPC_N_CALL,
2026 func, key);
2027 return ret;
2028}
2029
2030
2031static struct xpc_msg *
2032xpc_pull_remote_msg(struct xpc_channel *ch, s64 get)
2033{
2034 struct xpc_partition *part = &xpc_partitions[ch->partid];
2035 struct xpc_msg *remote_msg, *msg;
2036 u32 msg_index, nmsgs;
2037 u64 msg_offset;
2038 enum xpc_retval ret;
2039
2040
2041 if (down_interruptible(&ch->msg_to_pull_sema) != 0) {
2042 /* we were interrupted by a signal */
2043 return NULL;
2044 }
2045
2046 while (get >= ch->next_msg_to_pull) {
2047
2048 /* pull as many messages as are ready and able to be pulled */
2049
2050 msg_index = ch->next_msg_to_pull % ch->remote_nentries;
2051
2052 DBUG_ON(ch->next_msg_to_pull >=
2053 (volatile s64) ch->w_remote_GP.put);
2054 nmsgs = (volatile s64) ch->w_remote_GP.put -
2055 ch->next_msg_to_pull;
2056 if (msg_index + nmsgs > ch->remote_nentries) {
2057 /* ignore the ones that wrap the msg queue for now */
2058 nmsgs = ch->remote_nentries - msg_index;
2059 }
2060
2061 msg_offset = msg_index * ch->msg_size;
2062 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
2063 msg_offset);
2064 remote_msg = (struct xpc_msg *) (ch->remote_msgqueue_pa +
2065 msg_offset);
2066
2067 if ((ret = xpc_pull_remote_cachelines(part, msg, remote_msg,
2068 nmsgs * ch->msg_size)) != xpcSuccess) {
2069
2070 dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
2071 " msg %ld from partition %d, channel=%d, "
2072 "ret=%d\n", nmsgs, ch->next_msg_to_pull,
2073 ch->partid, ch->number, ret);
2074
2075 XPC_DEACTIVATE_PARTITION(part, ret);
2076
2077 up(&ch->msg_to_pull_sema);
2078 return NULL;
2079 }
2080
2081 mb(); /* >>> this may not be needed, we're not sure */
2082
2083 ch->next_msg_to_pull += nmsgs;
2084 }
2085
2086 up(&ch->msg_to_pull_sema);
2087
2088 /* return the message we were looking for */
2089 msg_offset = (get % ch->remote_nentries) * ch->msg_size;
2090 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue + msg_offset);
2091
2092 return msg;
2093}
2094
2095
2096/*
2097 * Get a message to be delivered.
2098 */
2099static struct xpc_msg *
2100xpc_get_deliverable_msg(struct xpc_channel *ch)
2101{
2102 struct xpc_msg *msg = NULL;
2103 s64 get;
2104
2105
2106 do {
2107 if ((volatile u32) ch->flags & XPC_C_DISCONNECTING) {
2108 break;
2109 }
2110
2111 get = (volatile s64) ch->w_local_GP.get;
2112 if (get == (volatile s64) ch->w_remote_GP.put) {
2113 break;
2114 }
2115
2116 /* There are messages waiting to be pulled and delivered.
2117 * We need to try to secure one for ourselves. We'll do this
2118 * by trying to increment w_local_GP.get and hope that no one
2119 * else beats us to it. If they do, we'll we'll simply have
2120 * to try again for the next one.
2121 */
2122
2123 if (cmpxchg(&ch->w_local_GP.get, get, get + 1) == get) {
2124 /* we got the entry referenced by get */
2125
2126 dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
2127 "partid=%d, channel=%d\n", get + 1,
2128 ch->partid, ch->number);
2129
2130 /* pull the message from the remote partition */
2131
2132 msg = xpc_pull_remote_msg(ch, get);
2133
2134 DBUG_ON(msg != NULL && msg->number != get);
2135 DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
2136 DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
2137
2138 break;
2139 }
2140
2141 } while (1);
2142
2143 return msg;
2144}
2145
2146
2147/*
2148 * Deliver a message to its intended recipient.
2149 */
2150void
2151xpc_deliver_msg(struct xpc_channel *ch)
2152{
2153 struct xpc_msg *msg;
2154
2155
2156 if ((msg = xpc_get_deliverable_msg(ch)) != NULL) {
2157
2158 /*
2159 * This ref is taken to protect the payload itself from being
2160 * freed before the user is finished with it, which the user
2161 * indicates by calling xpc_initiate_received().
2162 */
2163 xpc_msgqueue_ref(ch);
2164
2165 atomic_inc(&ch->kthreads_active);
2166
2167 if (ch->func != NULL) {
2168 dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
2169 "msg_number=%ld, partid=%d, channel=%d\n",
2170 (void *) msg, msg->number, ch->partid,
2171 ch->number);
2172
2173 /* deliver the message to its intended recipient */
2174 ch->func(xpcMsgReceived, ch->partid, ch->number,
2175 &msg->payload, ch->key);
2176
2177 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
2178 "msg_number=%ld, partid=%d, channel=%d\n",
2179 (void *) msg, msg->number, ch->partid,
2180 ch->number);
2181 }
2182
2183 atomic_dec(&ch->kthreads_active);
2184 }
2185}
2186
2187
2188/*
2189 * Now we actually acknowledge the messages that have been delivered and ack'd
2190 * by advancing the cached remote message queue's Get value and if requested
2191 * send an IPI to the message sender's partition.
2192 */
2193static void
2194xpc_acknowledge_msgs(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2195{
2196 struct xpc_msg *msg;
2197 s64 get = initial_get + 1;
2198 int send_IPI = 0;
2199
2200
2201 while (1) {
2202
2203 while (1) {
2204 if (get == (volatile s64) ch->w_local_GP.get) {
2205 break;
2206 }
2207
2208 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
2209 (get % ch->remote_nentries) * ch->msg_size);
2210
2211 if (!(msg->flags & XPC_M_DONE)) {
2212 break;
2213 }
2214
2215 msg_flags |= msg->flags;
2216 get++;
2217 }
2218
2219 if (get == initial_get) {
2220 /* nothing's changed */
2221 break;
2222 }
2223
2224 if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) !=
2225 initial_get) {
2226 /* someone else beat us to it */
2227 DBUG_ON((volatile s64) ch->local_GP->get <=
2228 initial_get);
2229 break;
2230 }
2231
2232 /* we just set the new value of local_GP->get */
2233
2234 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2235 "channel=%d\n", get, ch->partid, ch->number);
2236
2237 send_IPI = (msg_flags & XPC_M_INTERRUPT);
2238
2239 /*
2240 * We need to ensure that the message referenced by
2241 * local_GP->get is not XPC_M_DONE or that local_GP->get
2242 * equals w_local_GP.get, so we'll go have a look.
2243 */
2244 initial_get = get;
2245 }
2246
2247 if (send_IPI) {
2248 xpc_IPI_send_msgrequest(ch);
2249 }
2250}
2251
2252
2253/*
2254 * Acknowledge receipt of a delivered message.
2255 *
2256 * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
2257 * that sent the message.
2258 *
2259 * This function, although called by users, does not call xpc_part_ref() to
2260 * ensure that the partition infrastructure is in place. It relies on the
2261 * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
2262 *
2263 * Arguments:
2264 *
2265 * partid - ID of partition to which the channel is connected.
2266 * ch_number - channel # message received on.
2267 * payload - pointer to the payload area allocated via
2268 * xpc_initiate_allocate().
2269 */
2270void
2271xpc_initiate_received(partid_t partid, int ch_number, void *payload)
2272{
2273 struct xpc_partition *part = &xpc_partitions[partid];
2274 struct xpc_channel *ch;
2275 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2276 s64 get, msg_number = msg->number;
2277
2278
2279 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2280 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2281
2282 ch = &part->channels[ch_number];
2283
2284 dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
2285 (void *) msg, msg_number, ch->partid, ch->number);
2286
2287 DBUG_ON((((u64) msg - (u64) ch->remote_msgqueue) / ch->msg_size) !=
2288 msg_number % ch->remote_nentries);
2289 DBUG_ON(msg->flags & XPC_M_DONE);
2290
2291 msg->flags |= XPC_M_DONE;
2292
2293 /*
2294 * The preceding store of msg->flags must occur before the following
2295 * load of ch->local_GP->get.
2296 */
2297 mb();
2298
2299 /*
2300 * See if this message is next in line to be acknowledged as having
2301 * been delivered.
2302 */
2303 get = ch->local_GP->get;
2304 if (get == msg_number) {
2305 xpc_acknowledge_msgs(ch, get, msg->flags);
2306 }
2307
2308 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
2309 xpc_msgqueue_deref(ch);
2310}
2311