272ab4deb573bf974d721e3e8f3c64f88995c689
[safe/jmp/linux-2.6] / arch / ia64 / sn / kernel / xpc_channel.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) 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  */
33 static void
34 xpc_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 */
60                 sema_init(&ch->wdisconnect_sema, 0);    /* event wait */
61
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  */
73 enum xpc_retval
74 xpc_setup_infrastructure(struct xpc_partition *part)
75 {
76         int ret, cpuid;
77         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);
170         atomic_set(&part->nchannels_engaged, 0);
171
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          */
214         part->setup_state = XPC_P_SETUP;
215
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);
228         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);
231         xpc_vars_part[partid].nchannels = part->nchannels;
232         xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
233
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  */
246 static enum xpc_retval
247 xpc_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  */
278 enum xpc_retval
279 xpc_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
360                 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
361         }
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  */
374 static u64
375 xpc_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  */
432 static enum xpc_retval
433 xpc_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  */
485 static enum xpc_retval
486 xpc_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  */
533 static enum xpc_retval
534 xpc_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
560         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  */
574 static void
575 xpc_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 /*
629  * Notify those who wanted to be notified upon delivery of their message.
630  */
631 static void
632 xpc_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 /*
678  * 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  */
684 static void
685 xpc_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;
721         }
722 }
723
724
725 /*
726  * spin_lock_irqsave() is expected to be held on entry.
727  */
728 static void
729 xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
730 {
731         struct xpc_partition *part = &xpc_partitions[ch->partid];
732         u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
733
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
750         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                 }
755
756         } else {
757
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
774         /* 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
780         /* both sides are disconnected now */
781
782         if (ch->flags & XPC_C_CONNECTCALLOUT) {
783                 spin_unlock_irqrestore(&ch->lock, *irq_flags);
784                 xpc_disconnect_callout(ch, xpcDisconnected);
785                 spin_lock_irqsave(&ch->lock, *irq_flags);
786         }
787
788         /* it's now safe to free the channel's message queues */
789         xpc_free_msgqueues(ch);
790
791         /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
792         ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
793
794         atomic_dec(&part->nchannels_active);
795
796         if (channel_was_connected) {
797                 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
798                         "reason=%d\n", ch->number, ch->partid, ch->reason);
799         }
800
801         if (ch->flags & XPC_C_WDISCONNECT) {
802                 spin_unlock_irqrestore(&ch->lock, *irq_flags);
803                 up(&ch->wdisconnect_sema);
804                 spin_lock_irqsave(&ch->lock, *irq_flags);
805
806         } else if (ch->delayed_IPI_flags) {
807                 if (part->act_state != XPC_P_DEACTIVATING) {
808                         /* time to take action on any delayed IPI flags */
809                         spin_lock(&part->IPI_lock);
810                         XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number,
811                                                         ch->delayed_IPI_flags);
812                         spin_unlock(&part->IPI_lock);
813                 }
814                 ch->delayed_IPI_flags = 0;
815         }
816 }
817
818
819 /*
820  * Process a change in the channel's remote connection state.
821  */
822 static void
823 xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
824                                 u8 IPI_flags)
825 {
826         unsigned long irq_flags;
827         struct xpc_openclose_args *args =
828                                 &part->remote_openclose_args[ch_number];
829         struct xpc_channel *ch = &part->channels[ch_number];
830         enum xpc_retval reason;
831
832
833
834         spin_lock_irqsave(&ch->lock, irq_flags);
835
836 again:
837
838         if ((ch->flags & XPC_C_DISCONNECTED) &&
839                                         (ch->flags & XPC_C_WDISCONNECT)) {
840                 /*
841                  * Delay processing IPI flags until thread waiting disconnect
842                  * has had a chance to see that the channel is disconnected.
843                  */
844                 ch->delayed_IPI_flags |= IPI_flags;
845                 spin_unlock_irqrestore(&ch->lock, irq_flags);
846                 return;
847         }
848
849
850         if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
851
852                 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
853                         "from partid=%d, channel=%d\n", args->reason,
854                         ch->partid, ch->number);
855
856                 /*
857                  * If RCLOSEREQUEST is set, we're probably waiting for
858                  * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
859                  * with this RCLOSEREQUEST in the IPI_flags.
860                  */
861
862                 if (ch->flags & XPC_C_RCLOSEREQUEST) {
863                         DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
864                         DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
865                         DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
866                         DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
867
868                         DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
869                         IPI_flags &= ~XPC_IPI_CLOSEREPLY;
870                         ch->flags |= XPC_C_RCLOSEREPLY;
871
872                         /* both sides have finished disconnecting */
873                         xpc_process_disconnect(ch, &irq_flags);
874                         DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
875                         goto again;
876                 }
877
878                 if (ch->flags & XPC_C_DISCONNECTED) {
879                         if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
880                                 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
881                                          ch_number) & XPC_IPI_OPENREQUEST)) {
882
883                                         DBUG_ON(ch->delayed_IPI_flags != 0);
884                                         spin_lock(&part->IPI_lock);
885                                         XPC_SET_IPI_FLAGS(part->local_IPI_amo,
886                                                         ch_number,
887                                                         XPC_IPI_CLOSEREQUEST);
888                                         spin_unlock(&part->IPI_lock);
889                                 }
890                                 spin_unlock_irqrestore(&ch->lock, irq_flags);
891                                 return;
892                         }
893
894                         XPC_SET_REASON(ch, 0, 0);
895                         ch->flags &= ~XPC_C_DISCONNECTED;
896
897                         atomic_inc(&part->nchannels_active);
898                         ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
899                 }
900
901                 IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
902
903                 /*
904                  * The meaningful CLOSEREQUEST connection state fields are:
905                  *      reason = reason connection is to be closed
906                  */
907
908                 ch->flags |= XPC_C_RCLOSEREQUEST;
909
910                 if (!(ch->flags & XPC_C_DISCONNECTING)) {
911                         reason = args->reason;
912                         if (reason <= xpcSuccess || reason > xpcUnknownReason) {
913                                 reason = xpcUnknownReason;
914                         } else if (reason == xpcUnregistering) {
915                                 reason = xpcOtherUnregistering;
916                         }
917
918                         XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
919
920                         DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
921                         spin_unlock_irqrestore(&ch->lock, irq_flags);
922                         return;
923                 }
924
925                 xpc_process_disconnect(ch, &irq_flags);
926         }
927
928
929         if (IPI_flags & XPC_IPI_CLOSEREPLY) {
930
931                 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
932                         " channel=%d\n", ch->partid, ch->number);
933
934                 if (ch->flags & XPC_C_DISCONNECTED) {
935                         DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
936                         spin_unlock_irqrestore(&ch->lock, irq_flags);
937                         return;
938                 }
939
940                 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
941
942                 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
943                         if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
944                                                 & XPC_IPI_CLOSEREQUEST)) {
945
946                                 DBUG_ON(ch->delayed_IPI_flags != 0);
947                                 spin_lock(&part->IPI_lock);
948                                 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
949                                                 ch_number, XPC_IPI_CLOSEREPLY);
950                                 spin_unlock(&part->IPI_lock);
951                         }
952                         spin_unlock_irqrestore(&ch->lock, irq_flags);
953                         return;
954                 }
955
956                 ch->flags |= XPC_C_RCLOSEREPLY;
957
958                 if (ch->flags & XPC_C_CLOSEREPLY) {
959                         /* both sides have finished disconnecting */
960                         xpc_process_disconnect(ch, &irq_flags);
961                 }
962         }
963
964
965         if (IPI_flags & XPC_IPI_OPENREQUEST) {
966
967                 dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
968                         "local_nentries=%d) received from partid=%d, "
969                         "channel=%d\n", args->msg_size, args->local_nentries,
970                         ch->partid, ch->number);
971
972                 if (part->act_state == XPC_P_DEACTIVATING ||
973                                         (ch->flags & XPC_C_ROPENREQUEST)) {
974                         spin_unlock_irqrestore(&ch->lock, irq_flags);
975                         return;
976                 }
977
978                 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
979                         ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST;
980                         spin_unlock_irqrestore(&ch->lock, irq_flags);
981                         return;
982                 }
983                 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
984                                                         XPC_C_OPENREQUEST)));
985                 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
986                                         XPC_C_OPENREPLY | XPC_C_CONNECTED));
987
988                 /*
989                  * The meaningful OPENREQUEST connection state fields are:
990                  *      msg_size = size of channel's messages in bytes
991                  *      local_nentries = remote partition's local_nentries
992                  */
993                 if (args->msg_size == 0 || args->local_nentries == 0) {
994                         /* assume OPENREQUEST was delayed by mistake */
995                         spin_unlock_irqrestore(&ch->lock, irq_flags);
996                         return;
997                 }
998
999                 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
1000                 ch->remote_nentries = args->local_nentries;
1001
1002
1003                 if (ch->flags & XPC_C_OPENREQUEST) {
1004                         if (args->msg_size != ch->msg_size) {
1005                                 XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
1006                                                                 &irq_flags);
1007                                 spin_unlock_irqrestore(&ch->lock, irq_flags);
1008                                 return;
1009                         }
1010                 } else {
1011                         ch->msg_size = args->msg_size;
1012
1013                         XPC_SET_REASON(ch, 0, 0);
1014                         ch->flags &= ~XPC_C_DISCONNECTED;
1015
1016                         atomic_inc(&part->nchannels_active);
1017                 }
1018
1019                 xpc_process_connect(ch, &irq_flags);
1020         }
1021
1022
1023         if (IPI_flags & XPC_IPI_OPENREPLY) {
1024
1025                 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
1026                         "local_nentries=%d, remote_nentries=%d) received from "
1027                         "partid=%d, channel=%d\n", args->local_msgqueue_pa,
1028                         args->local_nentries, args->remote_nentries,
1029                         ch->partid, ch->number);
1030
1031                 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
1032                         spin_unlock_irqrestore(&ch->lock, irq_flags);
1033                         return;
1034                 }
1035                 if (!(ch->flags & XPC_C_OPENREQUEST)) {
1036                         XPC_DISCONNECT_CHANNEL(ch, xpcOpenCloseError,
1037                                                                 &irq_flags);
1038                         spin_unlock_irqrestore(&ch->lock, irq_flags);
1039                         return;
1040                 }
1041
1042                 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
1043                 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1044
1045                 /*
1046                  * The meaningful OPENREPLY connection state fields are:
1047                  *      local_msgqueue_pa = physical address of remote
1048                  *                          partition's local_msgqueue
1049                  *      local_nentries = remote partition's local_nentries
1050                  *      remote_nentries = remote partition's remote_nentries
1051                  */
1052                 DBUG_ON(args->local_msgqueue_pa == 0);
1053                 DBUG_ON(args->local_nentries == 0);
1054                 DBUG_ON(args->remote_nentries == 0);
1055
1056                 ch->flags |= XPC_C_ROPENREPLY;
1057                 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
1058
1059                 if (args->local_nentries < ch->remote_nentries) {
1060                         dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1061                                 "remote_nentries=%d, old remote_nentries=%d, "
1062                                 "partid=%d, channel=%d\n",
1063                                 args->local_nentries, ch->remote_nentries,
1064                                 ch->partid, ch->number);
1065
1066                         ch->remote_nentries = args->local_nentries;
1067                 }
1068                 if (args->remote_nentries < ch->local_nentries) {
1069                         dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1070                                 "local_nentries=%d, old local_nentries=%d, "
1071                                 "partid=%d, channel=%d\n",
1072                                 args->remote_nentries, ch->local_nentries,
1073                                 ch->partid, ch->number);
1074
1075                         ch->local_nentries = args->remote_nentries;
1076                 }
1077
1078                 xpc_process_connect(ch, &irq_flags);
1079         }
1080
1081         spin_unlock_irqrestore(&ch->lock, irq_flags);
1082 }
1083
1084
1085 /*
1086  * Attempt to establish a channel connection to a remote partition.
1087  */
1088 static enum xpc_retval
1089 xpc_connect_channel(struct xpc_channel *ch)
1090 {
1091         unsigned long irq_flags;
1092         struct xpc_registration *registration = &xpc_registrations[ch->number];
1093
1094
1095         if (down_trylock(&registration->sema) != 0) {
1096                 return xpcRetry;
1097         }
1098
1099         if (!XPC_CHANNEL_REGISTERED(ch->number)) {
1100                 up(&registration->sema);
1101                 return xpcUnregistered;
1102         }
1103
1104         spin_lock_irqsave(&ch->lock, irq_flags);
1105
1106         DBUG_ON(ch->flags & XPC_C_CONNECTED);
1107         DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
1108
1109         if (ch->flags & XPC_C_DISCONNECTING) {
1110                 spin_unlock_irqrestore(&ch->lock, irq_flags);
1111                 up(&registration->sema);
1112                 return ch->reason;
1113         }
1114
1115
1116         /* add info from the channel connect registration to the channel */
1117
1118         ch->kthreads_assigned_limit = registration->assigned_limit;
1119         ch->kthreads_idle_limit = registration->idle_limit;
1120         DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
1121         DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
1122         DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
1123
1124         ch->func = registration->func;
1125         DBUG_ON(registration->func == NULL);
1126         ch->key = registration->key;
1127
1128         ch->local_nentries = registration->nentries;
1129
1130         if (ch->flags & XPC_C_ROPENREQUEST) {
1131                 if (registration->msg_size != ch->msg_size) {
1132                         /* the local and remote sides aren't the same */
1133
1134                         /*
1135                          * Because XPC_DISCONNECT_CHANNEL() can block we're
1136                          * forced to up the registration sema before we unlock
1137                          * the channel lock. But that's okay here because we're
1138                          * done with the part that required the registration
1139                          * sema. XPC_DISCONNECT_CHANNEL() requires that the
1140                          * channel lock be locked and will unlock and relock
1141                          * the channel lock as needed.
1142                          */
1143                         up(&registration->sema);
1144                         XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
1145                                                                 &irq_flags);
1146                         spin_unlock_irqrestore(&ch->lock, irq_flags);
1147                         return xpcUnequalMsgSizes;
1148                 }
1149         } else {
1150                 ch->msg_size = registration->msg_size;
1151
1152                 XPC_SET_REASON(ch, 0, 0);
1153                 ch->flags &= ~XPC_C_DISCONNECTED;
1154
1155                 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
1156         }
1157
1158         up(&registration->sema);
1159
1160
1161         /* initiate the connection */
1162
1163         ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
1164         xpc_IPI_send_openrequest(ch, &irq_flags);
1165
1166         xpc_process_connect(ch, &irq_flags);
1167
1168         spin_unlock_irqrestore(&ch->lock, irq_flags);
1169
1170         return xpcSuccess;
1171 }
1172
1173
1174 /*
1175  * Clear some of the msg flags in the local message queue.
1176  */
1177 static inline void
1178 xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
1179 {
1180         struct xpc_msg *msg;
1181         s64 get;
1182
1183
1184         get = ch->w_remote_GP.get;
1185         do {
1186                 msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
1187                                 (get % ch->local_nentries) * ch->msg_size);
1188                 msg->flags = 0;
1189         } while (++get < (volatile s64) ch->remote_GP.get);
1190 }
1191
1192
1193 /*
1194  * Clear some of the msg flags in the remote message queue.
1195  */
1196 static inline void
1197 xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
1198 {
1199         struct xpc_msg *msg;
1200         s64 put;
1201
1202
1203         put = ch->w_remote_GP.put;
1204         do {
1205                 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
1206                                 (put % ch->remote_nentries) * ch->msg_size);
1207                 msg->flags = 0;
1208         } while (++put < (volatile s64) ch->remote_GP.put);
1209 }
1210
1211
1212 static void
1213 xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
1214 {
1215         struct xpc_channel *ch = &part->channels[ch_number];
1216         int nmsgs_sent;
1217
1218
1219         ch->remote_GP = part->remote_GPs[ch_number];
1220
1221
1222         /* See what, if anything, has changed for each connected channel */
1223
1224         xpc_msgqueue_ref(ch);
1225
1226         if (ch->w_remote_GP.get == ch->remote_GP.get &&
1227                                 ch->w_remote_GP.put == ch->remote_GP.put) {
1228                 /* nothing changed since GPs were last pulled */
1229                 xpc_msgqueue_deref(ch);
1230                 return;
1231         }
1232
1233         if (!(ch->flags & XPC_C_CONNECTED)){
1234                 xpc_msgqueue_deref(ch);
1235                 return;
1236         }
1237
1238
1239         /*
1240          * First check to see if messages recently sent by us have been
1241          * received by the other side. (The remote GET value will have
1242          * changed since we last looked at it.)
1243          */
1244
1245         if (ch->w_remote_GP.get != ch->remote_GP.get) {
1246
1247                 /*
1248                  * We need to notify any senders that want to be notified
1249                  * that their sent messages have been received by their
1250                  * intended recipients. We need to do this before updating
1251                  * w_remote_GP.get so that we don't allocate the same message
1252                  * queue entries prematurely (see xpc_allocate_msg()).
1253                  */
1254                 if (atomic_read(&ch->n_to_notify) > 0) {
1255                         /*
1256                          * Notify senders that messages sent have been
1257                          * received and delivered by the other side.
1258                          */
1259                         xpc_notify_senders(ch, xpcMsgDelivered,
1260                                                         ch->remote_GP.get);
1261                 }
1262
1263                 /*
1264                  * Clear msg->flags in previously sent messages, so that
1265                  * they're ready for xpc_allocate_msg().
1266                  */
1267                 xpc_clear_local_msgqueue_flags(ch);
1268
1269                 ch->w_remote_GP.get = ch->remote_GP.get;
1270
1271                 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1272                         "channel=%d\n", ch->w_remote_GP.get, ch->partid,
1273                         ch->number);
1274
1275                 /*
1276                  * If anyone was waiting for message queue entries to become
1277                  * available, wake them up.
1278                  */
1279                 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
1280                         wake_up(&ch->msg_allocate_wq);
1281                 }
1282         }
1283
1284
1285         /*
1286          * Now check for newly sent messages by the other side. (The remote
1287          * PUT value will have changed since we last looked at it.)
1288          */
1289
1290         if (ch->w_remote_GP.put != ch->remote_GP.put) {
1291                 /*
1292                  * Clear msg->flags in previously received messages, so that
1293                  * they're ready for xpc_get_deliverable_msg().
1294                  */
1295                 xpc_clear_remote_msgqueue_flags(ch);
1296
1297                 ch->w_remote_GP.put = ch->remote_GP.put;
1298
1299                 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1300                         "channel=%d\n", ch->w_remote_GP.put, ch->partid,
1301                         ch->number);
1302
1303                 nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
1304                 if (nmsgs_sent > 0) {
1305                         dev_dbg(xpc_chan, "msgs waiting to be copied and "
1306                                 "delivered=%d, partid=%d, channel=%d\n",
1307                                 nmsgs_sent, ch->partid, ch->number);
1308
1309                         if (ch->flags & XPC_C_CONNECTCALLOUT) {
1310                                 xpc_activate_kthreads(ch, nmsgs_sent);
1311                         }
1312                 }
1313         }
1314
1315         xpc_msgqueue_deref(ch);
1316 }
1317
1318
1319 void
1320 xpc_process_channel_activity(struct xpc_partition *part)
1321 {
1322         unsigned long irq_flags;
1323         u64 IPI_amo, IPI_flags;
1324         struct xpc_channel *ch;
1325         int ch_number;
1326         u32 ch_flags;
1327
1328
1329         IPI_amo = xpc_get_IPI_flags(part);
1330
1331         /*
1332          * Initiate channel connections for registered channels.
1333          *
1334          * For each connected channel that has pending messages activate idle
1335          * kthreads and/or create new kthreads as needed.
1336          */
1337
1338         for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1339                 ch = &part->channels[ch_number];
1340
1341
1342                 /*
1343                  * Process any open or close related IPI flags, and then deal
1344                  * with connecting or disconnecting the channel as required.
1345                  */
1346
1347                 IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
1348
1349                 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags)) {
1350                         xpc_process_openclose_IPI(part, ch_number, IPI_flags);
1351                 }
1352
1353                 ch_flags = ch->flags;   /* need an atomic snapshot of flags */
1354
1355                 if (ch_flags & XPC_C_DISCONNECTING) {
1356                         spin_lock_irqsave(&ch->lock, irq_flags);
1357                         xpc_process_disconnect(ch, &irq_flags);
1358                         spin_unlock_irqrestore(&ch->lock, irq_flags);
1359                         continue;
1360                 }
1361
1362                 if (part->act_state == XPC_P_DEACTIVATING) {
1363                         continue;
1364                 }
1365
1366                 if (!(ch_flags & XPC_C_CONNECTED)) {
1367                         if (!(ch_flags & XPC_C_OPENREQUEST)) {
1368                                 DBUG_ON(ch_flags & XPC_C_SETUP);
1369                                 (void) xpc_connect_channel(ch);
1370                         } else {
1371                                 spin_lock_irqsave(&ch->lock, irq_flags);
1372                                 xpc_process_connect(ch, &irq_flags);
1373                                 spin_unlock_irqrestore(&ch->lock, irq_flags);
1374                         }
1375                         continue;
1376                 }
1377
1378
1379                 /*
1380                  * Process any message related IPI flags, this may involve the
1381                  * activation of kthreads to deliver any pending messages sent
1382                  * from the other partition.
1383                  */
1384
1385                 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags)) {
1386                         xpc_process_msg_IPI(part, ch_number);
1387                 }
1388         }
1389 }
1390
1391
1392 /*
1393  * XPC's heartbeat code calls this function to inform XPC that a partition is
1394  * going down.  XPC responds by tearing down the XPartition Communication
1395  * infrastructure used for the just downed partition.
1396  *
1397  * XPC's heartbeat code will never call this function and xpc_partition_up()
1398  * at the same time. Nor will it ever make multiple calls to either function
1399  * at the same time.
1400  */
1401 void
1402 xpc_partition_going_down(struct xpc_partition *part, enum xpc_retval reason)
1403 {
1404         unsigned long irq_flags;
1405         int ch_number;
1406         struct xpc_channel *ch;
1407
1408
1409         dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
1410                 XPC_PARTID(part), reason);
1411
1412         if (!xpc_part_ref(part)) {
1413                 /* infrastructure for this partition isn't currently set up */
1414                 return;
1415         }
1416
1417
1418         /* disconnect channels associated with the partition going down */
1419
1420         for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1421                 ch = &part->channels[ch_number];
1422
1423                 xpc_msgqueue_ref(ch);
1424                 spin_lock_irqsave(&ch->lock, irq_flags);
1425
1426                 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
1427
1428                 spin_unlock_irqrestore(&ch->lock, irq_flags);
1429                 xpc_msgqueue_deref(ch);
1430         }
1431
1432         xpc_wakeup_channel_mgr(part);
1433
1434         xpc_part_deref(part);
1435 }
1436
1437
1438 /*
1439  * Teardown the infrastructure necessary to support XPartition Communication
1440  * between the specified remote partition and the local one.
1441  */
1442 void
1443 xpc_teardown_infrastructure(struct xpc_partition *part)
1444 {
1445         partid_t partid = XPC_PARTID(part);
1446
1447
1448         /*
1449          * We start off by making this partition inaccessible to local
1450          * processes by marking it as no longer setup. Then we make it
1451          * inaccessible to remote processes by clearing the XPC per partition
1452          * specific variable's magic # (which indicates that these variables
1453          * are no longer valid) and by ignoring all XPC notify IPIs sent to
1454          * this partition.
1455          */
1456
1457         DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
1458         DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1459         DBUG_ON(part->setup_state != XPC_P_SETUP);
1460         part->setup_state = XPC_P_WTEARDOWN;
1461
1462         xpc_vars_part[partid].magic = 0;
1463
1464
1465         free_irq(SGI_XPC_NOTIFY, (void *) (u64) partid);
1466
1467
1468         /*
1469          * Before proceding with the teardown we have to wait until all
1470          * existing references cease.
1471          */
1472         wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1473
1474
1475         /* now we can begin tearing down the infrastructure */
1476
1477         part->setup_state = XPC_P_TORNDOWN;
1478
1479         /* in case we've still got outstanding timers registered... */
1480         del_timer_sync(&part->dropped_IPI_timer);
1481
1482         kfree(part->remote_openclose_args_base);
1483         part->remote_openclose_args = NULL;
1484         kfree(part->local_openclose_args_base);
1485         part->local_openclose_args = NULL;
1486         kfree(part->remote_GPs_base);
1487         part->remote_GPs = NULL;
1488         kfree(part->local_GPs_base);
1489         part->local_GPs = NULL;
1490         kfree(part->channels);
1491         part->channels = NULL;
1492         part->local_IPI_amo_va = NULL;
1493 }
1494
1495
1496 /*
1497  * Called by XP at the time of channel connection registration to cause
1498  * XPC to establish connections to all currently active partitions.
1499  */
1500 void
1501 xpc_initiate_connect(int ch_number)
1502 {
1503         partid_t partid;
1504         struct xpc_partition *part;
1505         struct xpc_channel *ch;
1506
1507
1508         DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1509
1510         for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1511                 part = &xpc_partitions[partid];
1512
1513                 if (xpc_part_ref(part)) {
1514                         ch = &part->channels[ch_number];
1515
1516                         /*
1517                          * Initiate the establishment of a connection on the
1518                          * newly registered channel to the remote partition.
1519                          */
1520                         xpc_wakeup_channel_mgr(part);
1521                         xpc_part_deref(part);
1522                 }
1523         }
1524 }
1525
1526
1527 void
1528 xpc_connected_callout(struct xpc_channel *ch)
1529 {
1530         /* let the registerer know that a connection has been established */
1531
1532         if (ch->func != NULL) {
1533                 dev_dbg(xpc_chan, "ch->func() called, reason=xpcConnected, "
1534                         "partid=%d, channel=%d\n", ch->partid, ch->number);
1535
1536                 ch->func(xpcConnected, ch->partid, ch->number,
1537                                 (void *) (u64) ch->local_nentries, ch->key);
1538
1539                 dev_dbg(xpc_chan, "ch->func() returned, reason=xpcConnected, "
1540                         "partid=%d, channel=%d\n", ch->partid, ch->number);
1541         }
1542 }
1543
1544
1545 /*
1546  * Called by XP at the time of channel connection unregistration to cause
1547  * XPC to teardown all current connections for the specified channel.
1548  *
1549  * Before returning xpc_initiate_disconnect() will wait until all connections
1550  * on the specified channel have been closed/torndown. So the caller can be
1551  * assured that they will not be receiving any more callouts from XPC to the
1552  * function they registered via xpc_connect().
1553  *
1554  * Arguments:
1555  *
1556  *      ch_number - channel # to unregister.
1557  */
1558 void
1559 xpc_initiate_disconnect(int ch_number)
1560 {
1561         unsigned long irq_flags;
1562         partid_t partid;
1563         struct xpc_partition *part;
1564         struct xpc_channel *ch;
1565
1566
1567         DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1568
1569         /* initiate the channel disconnect for every active partition */
1570         for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1571                 part = &xpc_partitions[partid];
1572
1573                 if (xpc_part_ref(part)) {
1574                         ch = &part->channels[ch_number];
1575                         xpc_msgqueue_ref(ch);
1576
1577                         spin_lock_irqsave(&ch->lock, irq_flags);
1578
1579                         if (!(ch->flags & XPC_C_DISCONNECTED)) {
1580                                 ch->flags |= XPC_C_WDISCONNECT;
1581
1582                                 XPC_DISCONNECT_CHANNEL(ch, xpcUnregistering,
1583                                                                 &irq_flags);
1584                         }
1585
1586                         spin_unlock_irqrestore(&ch->lock, irq_flags);
1587
1588                         xpc_msgqueue_deref(ch);
1589                         xpc_part_deref(part);
1590                 }
1591         }
1592
1593         xpc_disconnect_wait(ch_number);
1594 }
1595
1596
1597 /*
1598  * To disconnect a channel, and reflect it back to all who may be waiting.
1599  *
1600  * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
1601  * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
1602  * xpc_disconnect_wait().
1603  *
1604  * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
1605  */
1606 void
1607 xpc_disconnect_channel(const int line, struct xpc_channel *ch,
1608                         enum xpc_retval reason, unsigned long *irq_flags)
1609 {
1610         u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
1611
1612
1613         DBUG_ON(!spin_is_locked(&ch->lock));
1614
1615         if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
1616                 return;
1617         }
1618         DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
1619
1620         dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
1621                 reason, line, ch->partid, ch->number);
1622
1623         XPC_SET_REASON(ch, reason, line);
1624
1625         ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
1626         /* some of these may not have been set */
1627         ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
1628                         XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
1629                         XPC_C_CONNECTING | XPC_C_CONNECTED);
1630
1631         xpc_IPI_send_closerequest(ch, irq_flags);
1632
1633         if (channel_was_connected) {
1634                 ch->flags |= XPC_C_WASCONNECTED;
1635         }
1636
1637         spin_unlock_irqrestore(&ch->lock, *irq_flags);
1638
1639         /* wake all idle kthreads so they can exit */
1640         if (atomic_read(&ch->kthreads_idle) > 0) {
1641                 wake_up_all(&ch->idle_wq);
1642         }
1643
1644         /* wake those waiting to allocate an entry from the local msg queue */
1645         if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
1646                 wake_up(&ch->msg_allocate_wq);
1647         }
1648
1649         spin_lock_irqsave(&ch->lock, *irq_flags);
1650 }
1651
1652
1653 void
1654 xpc_disconnect_callout(struct xpc_channel *ch, enum xpc_retval reason)
1655 {
1656         /*
1657          * Let the channel's registerer know that the channel is being
1658          * disconnected. We don't want to do this if the registerer was never
1659          * informed of a connection being made.
1660          */
1661
1662         if (ch->func != NULL) {
1663                 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
1664                         "channel=%d\n", reason, ch->partid, ch->number);
1665
1666                 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
1667
1668                 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
1669                         "channel=%d\n", reason, ch->partid, ch->number);
1670         }
1671 }
1672
1673
1674 /*
1675  * Wait for a message entry to become available for the specified channel,
1676  * but don't wait any longer than 1 jiffy.
1677  */
1678 static enum xpc_retval
1679 xpc_allocate_msg_wait(struct xpc_channel *ch)
1680 {
1681         enum xpc_retval ret;
1682
1683
1684         if (ch->flags & XPC_C_DISCONNECTING) {
1685                 DBUG_ON(ch->reason == xpcInterrupted);  // >>> Is this true?
1686                 return ch->reason;
1687         }
1688
1689         atomic_inc(&ch->n_on_msg_allocate_wq);
1690         ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
1691         atomic_dec(&ch->n_on_msg_allocate_wq);
1692
1693         if (ch->flags & XPC_C_DISCONNECTING) {
1694                 ret = ch->reason;
1695                 DBUG_ON(ch->reason == xpcInterrupted);  // >>> Is this true?
1696         } else if (ret == 0) {
1697                 ret = xpcTimeout;
1698         } else {
1699                 ret = xpcInterrupted;
1700         }
1701
1702         return ret;
1703 }
1704
1705
1706 /*
1707  * Allocate an entry for a message from the message queue associated with the
1708  * specified channel.
1709  */
1710 static enum xpc_retval
1711 xpc_allocate_msg(struct xpc_channel *ch, u32 flags,
1712                         struct xpc_msg **address_of_msg)
1713 {
1714         struct xpc_msg *msg;
1715         enum xpc_retval ret;
1716         s64 put;
1717
1718
1719         /* this reference will be dropped in xpc_send_msg() */
1720         xpc_msgqueue_ref(ch);
1721
1722         if (ch->flags & XPC_C_DISCONNECTING) {
1723                 xpc_msgqueue_deref(ch);
1724                 return ch->reason;
1725         }
1726         if (!(ch->flags & XPC_C_CONNECTED)) {
1727                 xpc_msgqueue_deref(ch);
1728                 return xpcNotConnected;
1729         }
1730
1731
1732         /*
1733          * Get the next available message entry from the local message queue.
1734          * If none are available, we'll make sure that we grab the latest
1735          * GP values.
1736          */
1737         ret = xpcTimeout;
1738
1739         while (1) {
1740
1741                 put = (volatile s64) ch->w_local_GP.put;
1742                 if (put - (volatile s64) ch->w_remote_GP.get <
1743                                                         ch->local_nentries) {
1744
1745                         /* There are available message entries. We need to try
1746                          * to secure one for ourselves. We'll do this by trying
1747                          * to increment w_local_GP.put as long as someone else
1748                          * doesn't beat us to it. If they do, we'll have to
1749                          * try again.
1750                          */
1751                         if (cmpxchg(&ch->w_local_GP.put, put, put + 1) ==
1752                                                                         put) {
1753                                 /* we got the entry referenced by put */
1754                                 break;
1755                         }
1756                         continue;       /* try again */
1757                 }
1758
1759
1760                 /*
1761                  * There aren't any available msg entries at this time.
1762                  *
1763                  * In waiting for a message entry to become available,
1764                  * we set a timeout in case the other side is not
1765                  * sending completion IPIs. This lets us fake an IPI
1766                  * that will cause the IPI handler to fetch the latest
1767                  * GP values as if an IPI was sent by the other side.
1768                  */
1769                 if (ret == xpcTimeout) {
1770                         xpc_IPI_send_local_msgrequest(ch);
1771                 }
1772
1773                 if (flags & XPC_NOWAIT) {
1774                         xpc_msgqueue_deref(ch);
1775                         return xpcNoWait;
1776                 }
1777
1778                 ret = xpc_allocate_msg_wait(ch);
1779                 if (ret != xpcInterrupted && ret != xpcTimeout) {
1780                         xpc_msgqueue_deref(ch);
1781                         return ret;
1782                 }
1783         }
1784
1785
1786         /* get the message's address and initialize it */
1787         msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
1788                                 (put % ch->local_nentries) * ch->msg_size);
1789
1790
1791         DBUG_ON(msg->flags != 0);
1792         msg->number = put;
1793
1794         dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
1795                 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
1796                 (void *) msg, msg->number, ch->partid, ch->number);
1797
1798         *address_of_msg = msg;
1799
1800         return xpcSuccess;
1801 }
1802
1803
1804 /*
1805  * Allocate an entry for a message from the message queue associated with the
1806  * specified channel. NOTE that this routine can sleep waiting for a message
1807  * entry to become available. To not sleep, pass in the XPC_NOWAIT flag.
1808  *
1809  * Arguments:
1810  *
1811  *      partid - ID of partition to which the channel is connected.
1812  *      ch_number - channel #.
1813  *      flags - see xpc.h for valid flags.
1814  *      payload - address of the allocated payload area pointer (filled in on
1815  *                return) in which the user-defined message is constructed.
1816  */
1817 enum xpc_retval
1818 xpc_initiate_allocate(partid_t partid, int ch_number, u32 flags, void **payload)
1819 {
1820         struct xpc_partition *part = &xpc_partitions[partid];
1821         enum xpc_retval ret = xpcUnknownReason;
1822         struct xpc_msg *msg;
1823
1824
1825         DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
1826         DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1827
1828         *payload = NULL;
1829
1830         if (xpc_part_ref(part)) {
1831                 ret = xpc_allocate_msg(&part->channels[ch_number], flags, &msg);
1832                 xpc_part_deref(part);
1833
1834                 if (msg != NULL) {
1835                         *payload = &msg->payload;
1836                 }
1837         }
1838
1839         return ret;
1840 }
1841
1842
1843 /*
1844  * Now we actually send the messages that are ready to be sent by advancing
1845  * the local message queue's Put value and then send an IPI to the recipient
1846  * partition.
1847  */
1848 static void
1849 xpc_send_msgs(struct xpc_channel *ch, s64 initial_put)
1850 {
1851         struct xpc_msg *msg;
1852         s64 put = initial_put + 1;
1853         int send_IPI = 0;
1854
1855
1856         while (1) {
1857
1858                 while (1) {
1859                         if (put == (volatile s64) ch->w_local_GP.put) {
1860                                 break;
1861                         }
1862
1863                         msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
1864                                (put % ch->local_nentries) * ch->msg_size);
1865
1866                         if (!(msg->flags & XPC_M_READY)) {
1867                                 break;
1868                         }
1869
1870                         put++;
1871                 }
1872
1873                 if (put == initial_put) {
1874                         /* nothing's changed */
1875                         break;
1876                 }
1877
1878                 if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) !=
1879                                                                 initial_put) {
1880                         /* someone else beat us to it */
1881                         DBUG_ON((volatile s64) ch->local_GP->put < initial_put);
1882                         break;
1883                 }
1884
1885                 /* we just set the new value of local_GP->put */
1886
1887                 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
1888                         "channel=%d\n", put, ch->partid, ch->number);
1889
1890                 send_IPI = 1;
1891
1892                 /*
1893                  * We need to ensure that the message referenced by
1894                  * local_GP->put is not XPC_M_READY or that local_GP->put
1895                  * equals w_local_GP.put, so we'll go have a look.
1896                  */
1897                 initial_put = put;
1898         }
1899
1900         if (send_IPI) {
1901                 xpc_IPI_send_msgrequest(ch);
1902         }
1903 }
1904
1905
1906 /*
1907  * Common code that does the actual sending of the message by advancing the
1908  * local message queue's Put value and sends an IPI to the partition the
1909  * message is being sent to.
1910  */
1911 static enum xpc_retval
1912 xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type,
1913                         xpc_notify_func func, void *key)
1914 {
1915         enum xpc_retval ret = xpcSuccess;
1916         struct xpc_notify *notify = notify;
1917         s64 put, msg_number = msg->number;
1918
1919
1920         DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
1921         DBUG_ON((((u64) msg - (u64) ch->local_msgqueue) / ch->msg_size) !=
1922                                         msg_number % ch->local_nentries);
1923         DBUG_ON(msg->flags & XPC_M_READY);
1924
1925         if (ch->flags & XPC_C_DISCONNECTING) {
1926                 /* drop the reference grabbed in xpc_allocate_msg() */
1927                 xpc_msgqueue_deref(ch);
1928                 return ch->reason;
1929         }
1930
1931         if (notify_type != 0) {
1932                 /*
1933                  * Tell the remote side to send an ACK interrupt when the
1934                  * message has been delivered.
1935                  */
1936                 msg->flags |= XPC_M_INTERRUPT;
1937
1938                 atomic_inc(&ch->n_to_notify);
1939
1940                 notify = &ch->notify_queue[msg_number % ch->local_nentries];
1941                 notify->func = func;
1942                 notify->key = key;
1943                 notify->type = notify_type;
1944
1945                 // >>> is a mb() needed here?
1946
1947                 if (ch->flags & XPC_C_DISCONNECTING) {
1948                         /*
1949                          * An error occurred between our last error check and
1950                          * this one. We will try to clear the type field from
1951                          * the notify entry. If we succeed then
1952                          * xpc_disconnect_channel() didn't already process
1953                          * the notify entry.
1954                          */
1955                         if (cmpxchg(&notify->type, notify_type, 0) ==
1956                                                                 notify_type) {
1957                                 atomic_dec(&ch->n_to_notify);
1958                                 ret = ch->reason;
1959                         }
1960
1961                         /* drop the reference grabbed in xpc_allocate_msg() */
1962                         xpc_msgqueue_deref(ch);
1963                         return ret;
1964                 }
1965         }
1966
1967         msg->flags |= XPC_M_READY;
1968
1969         /*
1970          * The preceding store of msg->flags must occur before the following
1971          * load of ch->local_GP->put.
1972          */
1973         mb();
1974
1975         /* see if the message is next in line to be sent, if so send it */
1976
1977         put = ch->local_GP->put;
1978         if (put == msg_number) {
1979                 xpc_send_msgs(ch, put);
1980         }
1981
1982         /* drop the reference grabbed in xpc_allocate_msg() */
1983         xpc_msgqueue_deref(ch);
1984         return ret;
1985 }
1986
1987
1988 /*
1989  * Send a message previously allocated using xpc_initiate_allocate() on the
1990  * specified channel connected to the specified partition.
1991  *
1992  * This routine will not wait for the message to be received, nor will
1993  * notification be given when it does happen. Once this routine has returned
1994  * the message entry allocated via xpc_initiate_allocate() is no longer
1995  * accessable to the caller.
1996  *
1997  * This routine, although called by users, does not call xpc_part_ref() to
1998  * ensure that the partition infrastructure is in place. It relies on the
1999  * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
2000  *
2001  * Arguments:
2002  *
2003  *      partid - ID of partition to which the channel is connected.
2004  *      ch_number - channel # to send message on.
2005  *      payload - pointer to the payload area allocated via
2006  *                      xpc_initiate_allocate().
2007  */
2008 enum xpc_retval
2009 xpc_initiate_send(partid_t partid, int ch_number, void *payload)
2010 {
2011         struct xpc_partition *part = &xpc_partitions[partid];
2012         struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2013         enum xpc_retval ret;
2014
2015
2016         dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
2017                 partid, ch_number);
2018
2019         DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2020         DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2021         DBUG_ON(msg == NULL);
2022
2023         ret = xpc_send_msg(&part->channels[ch_number], msg, 0, NULL, NULL);
2024
2025         return ret;
2026 }
2027
2028
2029 /*
2030  * Send a message previously allocated using xpc_initiate_allocate on the
2031  * specified channel connected to the specified partition.
2032  *
2033  * This routine will not wait for the message to be sent. Once this routine
2034  * has returned the message entry allocated via xpc_initiate_allocate() is no
2035  * longer accessable to the caller.
2036  *
2037  * Once the remote end of the channel has received the message, the function
2038  * passed as an argument to xpc_initiate_send_notify() will be called. This
2039  * allows the sender to free up or re-use any buffers referenced by the
2040  * message, but does NOT mean the message has been processed at the remote
2041  * end by a receiver.
2042  *
2043  * If this routine returns an error, the caller's function will NOT be called.
2044  *
2045  * This routine, although called by users, does not call xpc_part_ref() to
2046  * ensure that the partition infrastructure is in place. It relies on the
2047  * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
2048  *
2049  * Arguments:
2050  *
2051  *      partid - ID of partition to which the channel is connected.
2052  *      ch_number - channel # to send message on.
2053  *      payload - pointer to the payload area allocated via
2054  *                      xpc_initiate_allocate().
2055  *      func - function to call with asynchronous notification of message
2056  *                receipt. THIS FUNCTION MUST BE NON-BLOCKING.
2057  *      key - user-defined key to be passed to the function when it's called.
2058  */
2059 enum xpc_retval
2060 xpc_initiate_send_notify(partid_t partid, int ch_number, void *payload,
2061                                 xpc_notify_func func, void *key)
2062 {
2063         struct xpc_partition *part = &xpc_partitions[partid];
2064         struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2065         enum xpc_retval ret;
2066
2067
2068         dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
2069                 partid, ch_number);
2070
2071         DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2072         DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2073         DBUG_ON(msg == NULL);
2074         DBUG_ON(func == NULL);
2075
2076         ret = xpc_send_msg(&part->channels[ch_number], msg, XPC_N_CALL,
2077                                                                 func, key);
2078         return ret;
2079 }
2080
2081
2082 static struct xpc_msg *
2083 xpc_pull_remote_msg(struct xpc_channel *ch, s64 get)
2084 {
2085         struct xpc_partition *part = &xpc_partitions[ch->partid];
2086         struct xpc_msg *remote_msg, *msg;
2087         u32 msg_index, nmsgs;
2088         u64 msg_offset;
2089         enum xpc_retval ret;
2090
2091
2092         if (down_interruptible(&ch->msg_to_pull_sema) != 0) {
2093                 /* we were interrupted by a signal */
2094                 return NULL;
2095         }
2096
2097         while (get >= ch->next_msg_to_pull) {
2098
2099                 /* pull as many messages as are ready and able to be pulled */
2100
2101                 msg_index = ch->next_msg_to_pull % ch->remote_nentries;
2102
2103                 DBUG_ON(ch->next_msg_to_pull >=
2104                                         (volatile s64) ch->w_remote_GP.put);
2105                 nmsgs =  (volatile s64) ch->w_remote_GP.put -
2106                                                 ch->next_msg_to_pull;
2107                 if (msg_index + nmsgs > ch->remote_nentries) {
2108                         /* ignore the ones that wrap the msg queue for now */
2109                         nmsgs = ch->remote_nentries - msg_index;
2110                 }
2111
2112                 msg_offset = msg_index * ch->msg_size;
2113                 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
2114                                                                 msg_offset);
2115                 remote_msg = (struct xpc_msg *) (ch->remote_msgqueue_pa +
2116                                                                 msg_offset);
2117
2118                 if ((ret = xpc_pull_remote_cachelines(part, msg, remote_msg,
2119                                 nmsgs * ch->msg_size)) != xpcSuccess) {
2120
2121                         dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
2122                                 " msg %ld from partition %d, channel=%d, "
2123                                 "ret=%d\n", nmsgs, ch->next_msg_to_pull,
2124                                 ch->partid, ch->number, ret);
2125
2126                         XPC_DEACTIVATE_PARTITION(part, ret);
2127
2128                         up(&ch->msg_to_pull_sema);
2129                         return NULL;
2130                 }
2131
2132                 mb();   /* >>> this may not be needed, we're not sure */
2133
2134                 ch->next_msg_to_pull += nmsgs;
2135         }
2136
2137         up(&ch->msg_to_pull_sema);
2138
2139         /* return the message we were looking for */
2140         msg_offset = (get % ch->remote_nentries) * ch->msg_size;
2141         msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue + msg_offset);
2142
2143         return msg;
2144 }
2145
2146
2147 /*
2148  * Get a message to be delivered.
2149  */
2150 static struct xpc_msg *
2151 xpc_get_deliverable_msg(struct xpc_channel *ch)
2152 {
2153         struct xpc_msg *msg = NULL;
2154         s64 get;
2155
2156
2157         do {
2158                 if ((volatile u32) ch->flags & XPC_C_DISCONNECTING) {
2159                         break;
2160                 }
2161
2162                 get = (volatile s64) ch->w_local_GP.get;
2163                 if (get == (volatile s64) ch->w_remote_GP.put) {
2164                         break;
2165                 }
2166
2167                 /* There are messages waiting to be pulled and delivered.
2168                  * We need to try to secure one for ourselves. We'll do this
2169                  * by trying to increment w_local_GP.get and hope that no one
2170                  * else beats us to it. If they do, we'll we'll simply have
2171                  * to try again for the next one.
2172                  */
2173
2174                 if (cmpxchg(&ch->w_local_GP.get, get, get + 1) == get) {
2175                         /* we got the entry referenced by get */
2176
2177                         dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
2178                                 "partid=%d, channel=%d\n", get + 1,
2179                                 ch->partid, ch->number);
2180
2181                         /* pull the message from the remote partition */
2182
2183                         msg = xpc_pull_remote_msg(ch, get);
2184
2185                         DBUG_ON(msg != NULL && msg->number != get);
2186                         DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
2187                         DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
2188
2189                         break;
2190                 }
2191
2192         } while (1);
2193
2194         return msg;
2195 }
2196
2197
2198 /*
2199  * Deliver a message to its intended recipient.
2200  */
2201 void
2202 xpc_deliver_msg(struct xpc_channel *ch)
2203 {
2204         struct xpc_msg *msg;
2205
2206
2207         if ((msg = xpc_get_deliverable_msg(ch)) != NULL) {
2208
2209                 /*
2210                  * This ref is taken to protect the payload itself from being
2211                  * freed before the user is finished with it, which the user
2212                  * indicates by calling xpc_initiate_received().
2213                  */
2214                 xpc_msgqueue_ref(ch);
2215
2216                 atomic_inc(&ch->kthreads_active);
2217
2218                 if (ch->func != NULL) {
2219                         dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
2220                                 "msg_number=%ld, partid=%d, channel=%d\n",
2221                                 (void *) msg, msg->number, ch->partid,
2222                                 ch->number);
2223
2224                         /* deliver the message to its intended recipient */
2225                         ch->func(xpcMsgReceived, ch->partid, ch->number,
2226                                         &msg->payload, ch->key);
2227
2228                         dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
2229                                 "msg_number=%ld, partid=%d, channel=%d\n",
2230                                 (void *) msg, msg->number, ch->partid,
2231                                 ch->number);
2232                 }
2233
2234                 atomic_dec(&ch->kthreads_active);
2235         }
2236 }
2237
2238
2239 /*
2240  * Now we actually acknowledge the messages that have been delivered and ack'd
2241  * by advancing the cached remote message queue's Get value and if requested
2242  * send an IPI to the message sender's partition.
2243  */
2244 static void
2245 xpc_acknowledge_msgs(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2246 {
2247         struct xpc_msg *msg;
2248         s64 get = initial_get + 1;
2249         int send_IPI = 0;
2250
2251
2252         while (1) {
2253
2254                 while (1) {
2255                         if (get == (volatile s64) ch->w_local_GP.get) {
2256                                 break;
2257                         }
2258
2259                         msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
2260                                (get % ch->remote_nentries) * ch->msg_size);
2261
2262                         if (!(msg->flags & XPC_M_DONE)) {
2263                                 break;
2264                         }
2265
2266                         msg_flags |= msg->flags;
2267                         get++;
2268                 }
2269
2270                 if (get == initial_get) {
2271                         /* nothing's changed */
2272                         break;
2273                 }
2274
2275                 if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) !=
2276                                                                 initial_get) {
2277                         /* someone else beat us to it */
2278                         DBUG_ON((volatile s64) ch->local_GP->get <=
2279                                                                 initial_get);
2280                         break;
2281                 }
2282
2283                 /* we just set the new value of local_GP->get */
2284
2285                 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2286                         "channel=%d\n", get, ch->partid, ch->number);
2287
2288                 send_IPI = (msg_flags & XPC_M_INTERRUPT);
2289
2290                 /*
2291                  * We need to ensure that the message referenced by
2292                  * local_GP->get is not XPC_M_DONE or that local_GP->get
2293                  * equals w_local_GP.get, so we'll go have a look.
2294                  */
2295                 initial_get = get;
2296         }
2297
2298         if (send_IPI) {
2299                 xpc_IPI_send_msgrequest(ch);
2300         }
2301 }
2302
2303
2304 /*
2305  * Acknowledge receipt of a delivered message.
2306  *
2307  * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
2308  * that sent the message.
2309  *
2310  * This function, although called by users, does not call xpc_part_ref() to
2311  * ensure that the partition infrastructure is in place. It relies on the
2312  * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
2313  *
2314  * Arguments:
2315  *
2316  *      partid - ID of partition to which the channel is connected.
2317  *      ch_number - channel # message received on.
2318  *      payload - pointer to the payload area allocated via
2319  *                      xpc_initiate_allocate().
2320  */
2321 void
2322 xpc_initiate_received(partid_t partid, int ch_number, void *payload)
2323 {
2324         struct xpc_partition *part = &xpc_partitions[partid];
2325         struct xpc_channel *ch;
2326         struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2327         s64 get, msg_number = msg->number;
2328
2329
2330         DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2331         DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2332
2333         ch = &part->channels[ch_number];
2334
2335         dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
2336                 (void *) msg, msg_number, ch->partid, ch->number);
2337
2338         DBUG_ON((((u64) msg - (u64) ch->remote_msgqueue) / ch->msg_size) !=
2339                                         msg_number % ch->remote_nentries);
2340         DBUG_ON(msg->flags & XPC_M_DONE);
2341
2342         msg->flags |= XPC_M_DONE;
2343
2344         /*
2345          * The preceding store of msg->flags must occur before the following
2346          * load of ch->local_GP->get.
2347          */
2348         mb();
2349
2350         /*
2351          * See if this message is next in line to be acknowledged as having
2352          * been delivered.
2353          */
2354         get = ch->local_GP->get;
2355         if (get == msg_number) {
2356                 xpc_acknowledge_msgs(ch, get, msg->flags);
2357         }
2358
2359         /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg()  */
2360         xpc_msgqueue_deref(ch);
2361 }
2362