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