blob: 15cb91a82102f76c36d40d21dbe7239076092cc8 [file] [log] [blame]
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Dean Nelson45d9ca42008-04-22 14:46:56 -05006 * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07007 */
8
Dean Nelson89eb8eb2005-03-23 19:50:00 -07009/*
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -070017#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/sched.h>
20#include <linux/cache.h>
21#include <linux/interrupt.h>
Jes Sorensenf9e505a2006-01-17 12:52:21 -050022#include <linux/mutex.h>
23#include <linux/completion.h>
Dean Nelson89eb8eb2005-03-23 19:50:00 -070024#include <asm/sn/bte.h>
25#include <asm/sn/sn_sal.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050026#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070027
Dean Nelson89eb8eb2005-03-23 19:50:00 -070028/*
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050029 * Guarantee that the kzalloc'd memory is cacheline aligned.
30 */
31static void *
32xpc_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 }
Dean Nelson35190502008-04-22 14:48:55 -050039 if ((u64)*base == L1_CACHE_ALIGN((u64)*base)) {
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050040 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 }
Dean Nelson35190502008-04-22 14:48:55 -050049 return (void *)L1_CACHE_ALIGN((u64)*base);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050050}
51
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050052/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070053 * Set up the initial values for the XPartition Communication channels.
54 */
55static void
56xpc_initialize_channels(struct xpc_partition *part, partid_t partid)
57{
58 int ch_number;
59 struct xpc_channel *ch;
60
Dean Nelson89eb8eb2005-03-23 19:50:00 -070061 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 =
Dean Nelson35190502008-04-22 14:48:55 -050070 &part->local_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -070071
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);
Jes Sorensenf9e505a2006-01-17 12:52:21 -050080 mutex_init(&ch->msg_to_pull_mutex);
81 init_completion(&ch->wdisconnect_wait);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070082
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -070089/*
90 * Setup the infrastructure necessary to support XPartition Communication
91 * between the specified remote partition and the local one.
92 */
93enum xpc_retval
94xpc_setup_infrastructure(struct xpc_partition *part)
95{
Dean Nelson59a0a8a2005-07-13 05:45:00 -070096 int ret, cpuid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070097 struct timer_list *timer;
98 partid_t partid = XPC_PARTID(part);
99
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700100 /*
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) -
Dean Nelson35190502008-04-22 14:48:55 -0500107 offsetof(struct xpc_partition, nchannels));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700108
109 /*
110 * Allocate all of the channel structures as a contiguous chunk of
111 * memory.
112 */
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500113 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_NCHANNELS,
Dean Nelson35190502008-04-22 14:48:55 -0500114 GFP_KERNEL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700115 if (part->channels == NULL) {
116 dev_err(xpc_chan, "can't get memory for channels\n");
117 return xpcNoMemory;
118 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700119
120 part->nchannels = XPC_NCHANNELS;
121
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700122 /* allocate all the required GET/PUT values */
123
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500124 part->local_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
Dean Nelson35190502008-04-22 14:48:55 -0500125 GFP_KERNEL,
126 &part->local_GPs_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700127 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 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700134
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500135 part->remote_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
Dean Nelson35190502008-04-22 14:48:55 -0500136 GFP_KERNEL,
137 &part->
138 remote_GPs_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700139 if (part->remote_GPs == NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700140 dev_err(xpc_chan, "can't get memory for remote get/put "
141 "values\n");
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500142 kfree(part->local_GPs_base);
143 part->local_GPs = NULL;
144 kfree(part->channels);
145 part->channels = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700146 return xpcNoMemory;
147 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700148
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700149 /* allocate all the required open and close args */
150
Dean Nelson35190502008-04-22 14:48:55 -0500151 part->local_openclose_args =
152 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
153 &part->local_openclose_args_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700154 if (part->local_openclose_args == NULL) {
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500155 dev_err(xpc_chan, "can't get memory for local connect args\n");
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700156 kfree(part->remote_GPs_base);
157 part->remote_GPs = NULL;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500158 kfree(part->local_GPs_base);
159 part->local_GPs = NULL;
160 kfree(part->channels);
161 part->channels = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700162 return xpcNoMemory;
163 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700164
Dean Nelson35190502008-04-22 14:48:55 -0500165 part->remote_openclose_args =
166 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
167 &part->remote_openclose_args_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700168 if (part->remote_openclose_args == NULL) {
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500169 dev_err(xpc_chan, "can't get memory for remote connect args\n");
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700170 kfree(part->local_openclose_args_base);
171 part->local_openclose_args = NULL;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500172 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;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700178 return xpcNoMemory;
179 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700180
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700181 xpc_initialize_channels(part, partid);
182
183 atomic_set(&part->nchannels_active, 0);
Dean Nelsona607c382005-09-01 14:01:37 -0500184 atomic_set(&part->nchannels_engaged, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700185
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700186 /* 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);
Thomas Gleixner121a4222006-07-01 19:29:17 -0700197 ret = request_irq(SGI_XPC_NOTIFY, xpc_notify_IRQ_handler, IRQF_SHARED,
Dean Nelson35190502008-04-22 14:48:55 -0500198 part->IPI_owner, (void *)(u64)partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700199 if (ret != 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700200 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
201 "errno=%d\n", -ret);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500202 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;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700212 return xpcLackOfResources;
213 }
214
215 /* Setup a timer to check for dropped IPIs */
216 timer = &part->dropped_IPI_timer;
217 init_timer(timer);
Dean Nelson35190502008-04-22 14:48:55 -0500218 timer->function = (void (*)(unsigned long))xpc_dropped_IPI_check;
219 timer->data = (unsigned long)part;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700220 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 */
Dave Jones821fe942005-06-25 14:54:29 -0700227 part->setup_state = XPC_P_SETUP;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700228
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700229 /*
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 =
Dean Nelson35190502008-04-22 14:48:55 -0500238 __pa(part->local_openclose_args);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700239 xpc_vars_part[partid].IPI_amo_pa = __pa(part->local_IPI_amo_va);
Dean Nelson59a0a8a2005-07-13 05:45:00 -0700240 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);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700243 xpc_vars_part[partid].nchannels = part->nchannels;
Dave Jones821fe942005-06-25 14:54:29 -0700244 xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700245
246 return xpcSuccess;
247}
248
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700249/*
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 */
257static enum xpc_retval
258xpc_pull_remote_cachelines(struct xpc_partition *part, void *dst,
Dean Nelson35190502008-04-22 14:48:55 -0500259 const void *src, size_t cnt)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700260{
261 bte_result_t bte_ret;
262
Dean Nelson35190502008-04-22 14:48:55 -0500263 DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
264 DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700265 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
266
267 if (part->act_state == XPC_P_DEACTIVATING) {
268 return part->reason;
269 }
270
Dean Nelson35190502008-04-22 14:48:55 -0500271 bte_ret = xp_bte_copy((u64)src, (u64)dst, (u64)cnt,
272 (BTE_NORMAL | BTE_WACQUIRE), NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700273 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700283/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700284 * Pull the remote per partition specific variables from the specified
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700285 * partition.
286 */
287enum xpc_retval
288xpc_pull_remote_vars_part(struct xpc_partition *part)
289{
290 u8 buffer[L1_CACHE_BYTES * 2];
291 struct xpc_vars_part *pulled_entry_cacheline =
Dean Nelson35190502008-04-22 14:48:55 -0500292 (struct xpc_vars_part *)L1_CACHE_ALIGN((u64)buffer);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700293 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700298 /* pull the cacheline that contains the variables we're interested in */
299
300 DBUG_ON(part->remote_vars_part_pa !=
Dean Nelson35190502008-04-22 14:48:55 -0500301 L1_CACHE_ALIGN(part->remote_vars_part_pa));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700302 DBUG_ON(sizeof(struct xpc_vars_part) != L1_CACHE_BYTES / 2);
303
304 remote_entry_pa = part->remote_vars_part_pa +
Dean Nelson35190502008-04-22 14:48:55 -0500305 sn_partition_id * sizeof(struct xpc_vars_part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700306
307 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
308
Dean Nelson35190502008-04-22 14:48:55 -0500309 pulled_entry = (struct xpc_vars_part *)((u64)pulled_entry_cacheline +
310 (remote_entry_pa &
311 (L1_CACHE_BYTES - 1)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700312
313 ret = xpc_pull_remote_cachelines(part, pulled_entry_cacheline,
Dean Nelson35190502008-04-22 14:48:55 -0500314 (void *)remote_entry_cacheline_pa,
315 L1_CACHE_BYTES);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700316 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700322 /* see if they've been set up yet */
323
324 if (pulled_entry->magic != XPC_VP_MAGIC1 &&
Dean Nelson35190502008-04-22 14:48:55 -0500325 pulled_entry->magic != XPC_VP_MAGIC2) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700326
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 ||
Dean Nelson35190502008-04-22 14:48:55 -0500343 pulled_entry->openclose_args_pa == 0 ||
344 pulled_entry->IPI_amo_pa == 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700345
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 =
Dean Nelson35190502008-04-22 14:48:55 -0500356 pulled_entry->openclose_args_pa;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700357 part->remote_IPI_amo_va =
Dean Nelson35190502008-04-22 14:48:55 -0500358 (AMO_t *)__va(pulled_entry->IPI_amo_pa);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700359 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
Dave Jones821fe942005-06-25 14:54:29 -0700368 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700369 }
370
371 if (pulled_entry->magic == XPC_VP_MAGIC1) {
372 return xpcRetry;
373 }
374
375 return xpcSuccess;
376}
377
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700378/*
379 * Get the IPI flags and pull the openclose args and/or remote GPs as needed.
380 */
381static u64
382xpc_get_IPI_flags(struct xpc_partition *part)
383{
384 unsigned long irq_flags;
385 u64 IPI_amo;
386 enum xpc_retval ret;
387
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700388 /*
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700398 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_amo)) {
399 ret = xpc_pull_remote_cachelines(part,
Dean Nelson35190502008-04-22 14:48:55 -0500400 part->remote_openclose_args,
401 (void *)part->
402 remote_openclose_args_pa,
403 XPC_OPENCLOSE_ARGS_SIZE);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700404 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,
Dean Nelson35190502008-04-22 14:48:55 -0500418 (void *)part->remote_GPs_pa,
419 XPC_GP_SIZE);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700420 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700434/*
435 * Allocate the local message queue and the notify queue.
436 */
437static enum xpc_retval
438xpc_allocate_local_msgqueue(struct xpc_channel *ch)
439{
440 unsigned long irq_flags;
441 int nentries;
442 size_t nbytes;
443
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700444 // >>> may want to check for ch->flags & XPC_C_DISCONNECTING between
445 // >>> iterations of the for-loop, bail if set?
446
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700447 // >>> should we impose a minimum #of entries? like 4 or 8?
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700448 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
449
450 nbytes = nentries * ch->msg_size;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500451 ch->local_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
Dean Nelson35190502008-04-22 14:48:55 -0500452 GFP_KERNEL,
453 &ch->
454 local_msgqueue_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700455 if (ch->local_msgqueue == NULL) {
456 continue;
457 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700458
459 nbytes = nentries * sizeof(struct xpc_notify);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500460 ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700461 if (ch->notify_queue == NULL) {
462 kfree(ch->local_msgqueue_base);
463 ch->local_msgqueue = NULL;
464 continue;
465 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700466
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700484/*
485 * Allocate the cached remote message queue.
486 */
487static enum xpc_retval
488xpc_allocate_remote_msgqueue(struct xpc_channel *ch)
489{
490 unsigned long irq_flags;
491 int nentries;
492 size_t nbytes;
493
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700494 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
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700499 // >>> should we impose a minimum #of entries? like 4 or 8?
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700500 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
501
502 nbytes = nentries * ch->msg_size;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500503 ch->remote_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
Dean Nelson35190502008-04-22 14:48:55 -0500504 GFP_KERNEL,
505 &ch->
506 remote_msgqueue_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700507 if (ch->remote_msgqueue == NULL) {
508 continue;
509 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700510
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700528/*
529 * Allocate message queues and other stuff associated with a channel.
530 *
531 * Note: Assumes all of the channel sizes are filled in.
532 */
533static enum xpc_retval
534xpc_allocate_msgqueues(struct xpc_channel *ch)
535{
536 unsigned long irq_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700537 enum xpc_retval ret;
538
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700539 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700553 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700560/*
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 */
566static void
567xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
568{
569 enum xpc_retval ret;
570
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700571 DBUG_ON(!spin_is_locked(&ch->lock));
572
573 if (!(ch->flags & XPC_C_OPENREQUEST) ||
Dean Nelson35190502008-04-22 14:48:55 -0500574 !(ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700575 /* 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",
Dean Nelson35190502008-04-22 14:48:55 -0500611 ch->number, ch->partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700612
613 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -0600614 xpc_create_kthreads(ch, 1, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700615 spin_lock_irqsave(&ch->lock, *irq_flags);
616}
617
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700618/*
Dean Nelsona607c382005-09-01 14:01:37 -0500619 * Notify those who wanted to be notified upon delivery of their message.
620 */
621static void
622xpc_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
Dean Nelsona607c382005-09-01 14:01:37 -0500628 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 ||
Dean Nelson35190502008-04-22 14:48:55 -0500640 cmpxchg(&notify->type, notify_type, 0) != notify_type) {
Dean Nelsona607c382005-09-01 14:01:37 -0500641 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",
Dean Nelson35190502008-04-22 14:48:55 -0500651 (void *)notify, get, ch->partid, ch->number);
Dean Nelsona607c382005-09-01 14:01:37 -0500652
653 notify->func(reason, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500654 notify->key);
Dean Nelsona607c382005-09-01 14:01:37 -0500655
656 dev_dbg(xpc_chan, "notify->func() returned, "
657 "notify=0x%p, msg_number=%ld, partid=%d, "
Dean Nelson35190502008-04-22 14:48:55 -0500658 "channel=%d\n", (void *)notify, get,
Dean Nelsona607c382005-09-01 14:01:37 -0500659 ch->partid, ch->number);
660 }
661 }
662}
663
Dean Nelsona607c382005-09-01 14:01:37 -0500664/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700665 * 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 */
671static void
672xpc_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;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700708 }
709}
710
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700711/*
712 * spin_lock_irqsave() is expected to be held on entry.
713 */
714static void
715xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
716{
717 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c382005-09-01 14:01:37 -0500718 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700719
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700720 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
Dean Nelsona460ef82006-11-22 08:25:00 -0600730 if (atomic_read(&ch->kthreads_assigned) > 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500731 atomic_read(&ch->references) > 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700732 return;
733 }
Dean Nelsona460ef82006-11-22 08:25:00 -0600734 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500735 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700736
Dean Nelsona607c382005-09-01 14:01:37 -0500737 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 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700742
Dean Nelsona607c382005-09-01 14:01:37 -0500743 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700744
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
Dean Nelsona607c382005-09-01 14:01:37 -0500761 /* 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700767 /* both sides are disconnected now */
768
Dean Nelson4c2cd962006-02-15 08:02:21 -0600769 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600770 spin_unlock_irqrestore(&ch->lock, *irq_flags);
771 xpc_disconnect_callout(ch, xpcDisconnected);
772 spin_lock_irqsave(&ch->lock, *irq_flags);
773 }
774
Dean Nelsona607c382005-09-01 14:01:37 -0500775 /* 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));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700780
781 atomic_dec(&part->nchannels_active);
782
Dean Nelsona607c382005-09-01 14:01:37 -0500783 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700784 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
Dean Nelson35190502008-04-22 14:48:55 -0500785 "reason=%d\n", ch->number, ch->partid, ch->reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700786 }
Dean Nelsona607c382005-09-01 14:01:37 -0500787
Dean Nelsona607c382005-09-01 14:01:37 -0500788 if (ch->flags & XPC_C_WDISCONNECT) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500789 /* we won't lose the CPU since we're holding ch->lock */
790 complete(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500791 } 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,
Dean Nelson35190502008-04-22 14:48:55 -0500796 ch->delayed_IPI_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500797 spin_unlock(&part->IPI_lock);
798 }
799 ch->delayed_IPI_flags = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500800 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700801}
802
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700803/*
804 * Process a change in the channel's remote connection state.
805 */
806static void
807xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
Dean Nelson35190502008-04-22 14:48:55 -0500808 u8 IPI_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700809{
810 unsigned long irq_flags;
811 struct xpc_openclose_args *args =
Dean Nelson35190502008-04-22 14:48:55 -0500812 &part->remote_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700813 struct xpc_channel *ch = &part->channels[ch_number];
814 enum xpc_retval reason;
815
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700816 spin_lock_irqsave(&ch->lock, irq_flags);
817
Dean Nelson35190502008-04-22 14:48:55 -0500818 again:
Dean Nelsone54af722005-10-25 14:07:43 -0500819
Dean Nelson35190502008-04-22 14:48:55 -0500820 if ((ch->flags & XPC_C_DISCONNECTED) && (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500821 /*
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700830 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
Dean Nelsona607c382005-09-01 14:01:37 -0500839 * with this RCLOSEREQUEST in the IPI_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700840 */
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);
Dean Nelsone54af722005-10-25 14:07:43 -0500854 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
855 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700856 }
857
858 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700859 if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500860 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500861 ch_number) &
862 XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500863
864 DBUG_ON(ch->delayed_IPI_flags != 0);
865 spin_lock(&part->IPI_lock);
866 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500867 ch_number,
868 XPC_IPI_CLOSEREQUEST);
Dean Nelsone54af722005-10-25 14:07:43 -0500869 spin_unlock(&part->IPI_lock);
870 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700871 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);
Dean Nelsone54af722005-10-25 14:07:43 -0500900
901 DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
902 spin_unlock_irqrestore(&ch->lock, irq_flags);
903 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700904 }
Dean Nelsone54af722005-10-25 14:07:43 -0500905
906 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700907 }
908
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700909 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));
Dean Nelsone54af722005-10-25 14:07:43 -0500921
922 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
923 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
Dean Nelson35190502008-04-22 14:48:55 -0500924 & XPC_IPI_CLOSEREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500925
926 DBUG_ON(ch->delayed_IPI_flags != 0);
927 spin_lock(&part->IPI_lock);
928 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500929 ch_number,
930 XPC_IPI_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500931 spin_unlock(&part->IPI_lock);
932 }
933 spin_unlock_irqrestore(&ch->lock, irq_flags);
934 return;
935 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700936
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700945 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
Dean Nelsone54af722005-10-25 14:07:43 -0500952 if (part->act_state == XPC_P_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500953 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500954 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;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700960 spin_unlock_irqrestore(&ch->lock, irq_flags);
961 return;
962 }
963 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500964 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700965 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500966 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700967
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 */
Dean Nelsone54af722005-10-25 14:07:43 -0500973 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 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700978
979 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
980 ch->remote_nentries = args->local_nentries;
981
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700982 if (ch->flags & XPC_C_OPENREQUEST) {
983 if (args->msg_size != ch->msg_size) {
984 XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500985 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700986 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001001 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 }
Dean Nelsone54af722005-10-25 14:07:43 -05001013 if (!(ch->flags & XPC_C_OPENREQUEST)) {
1014 XPC_DISCONNECT_CHANNEL(ch, xpcOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -05001015 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -05001016 spin_unlock_irqrestore(&ch->lock, irq_flags);
1017 return;
1018 }
1019
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001020 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
Dean Nelson35190502008-04-22 14:48:55 -05001026 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001027 * 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001062/*
1063 * Attempt to establish a channel connection to a remote partition.
1064 */
1065static enum xpc_retval
1066xpc_connect_channel(struct xpc_channel *ch)
1067{
1068 unsigned long irq_flags;
1069 struct xpc_registration *registration = &xpc_registrations[ch->number];
1070
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001071 if (mutex_trylock(&registration->mutex) == 0) {
Dean Nelsone54af722005-10-25 14:07:43 -05001072 return xpcRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001073 }
1074
1075 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001076 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001077 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);
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001087 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001088 return ch->reason;
1089 }
1090
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001091 /* 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 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001118 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001119 XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -05001120 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001121 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
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001133 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001134
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001135 /* 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001147/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001148 * Clear some of the msg flags in the local message queue.
1149 */
1150static inline void
1151xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
1152{
1153 struct xpc_msg *msg;
1154 s64 get;
1155
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001156 get = ch->w_remote_GP.get;
1157 do {
Dean Nelson35190502008-04-22 14:48:55 -05001158 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1159 (get % ch->local_nentries) *
1160 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001161 msg->flags = 0;
Dean Nelson35190502008-04-22 14:48:55 -05001162 } while (++get < (volatile s64)ch->remote_GP.get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001163}
1164
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001165/*
1166 * Clear some of the msg flags in the remote message queue.
1167 */
1168static inline void
1169xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
1170{
1171 struct xpc_msg *msg;
1172 s64 put;
1173
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001174 put = ch->w_remote_GP.put;
1175 do {
Dean Nelson35190502008-04-22 14:48:55 -05001176 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
1177 (put % ch->remote_nentries) *
1178 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001179 msg->flags = 0;
Dean Nelson35190502008-04-22 14:48:55 -05001180 } while (++put < (volatile s64)ch->remote_GP.put);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001181}
1182
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001183static void
1184xpc_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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001189 ch->remote_GP = part->remote_GPs[ch_number];
1190
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001191 /* 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 &&
Dean Nelson35190502008-04-22 14:48:55 -05001196 ch->w_remote_GP.put == ch->remote_GP.put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001197 /* nothing changed since GPs were last pulled */
1198 xpc_msgqueue_deref(ch);
1199 return;
1200 }
1201
Dean Nelson35190502008-04-22 14:48:55 -05001202 if (!(ch->flags & XPC_C_CONNECTED)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001203 xpc_msgqueue_deref(ch);
1204 return;
1205 }
1206
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001207 /*
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,
Dean Nelson35190502008-04-22 14:48:55 -05001228 ch->remote_GP.get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001229 }
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
Dave Jones821fe942005-06-25 14:54:29 -07001237 ch->w_remote_GP.get = ch->remote_GP.get;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001238
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001252 /*
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
Dave Jones821fe942005-06-25 14:54:29 -07001264 ch->w_remote_GP.put = ch->remote_GP.put;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001265
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
Dean Nelson4c2cd962006-02-15 08:02:21 -06001276 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001277 xpc_activate_kthreads(ch, nmsgs_sent);
1278 }
1279 }
1280 }
1281
1282 xpc_msgqueue_deref(ch);
1283}
1284
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001285void
1286xpc_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;
Dean Nelsona607c382005-09-01 14:01:37 -05001292 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001293
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001294 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001306 /*
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
Dean Nelsona607c382005-09-01 14:01:37 -05001317 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001318
Dean Nelsona607c382005-09-01 14:01:37 -05001319 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001320 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
Dean Nelsona607c382005-09-01 14:01:37 -05001330 if (!(ch_flags & XPC_C_CONNECTED)) {
1331 if (!(ch_flags & XPC_C_OPENREQUEST)) {
1332 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -05001333 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001334 } 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001342 /*
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001354/*
Dean Nelsona607c382005-09-01 14:01:37 -05001355 * 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001357 * 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 */
1363void
Dean Nelsona607c382005-09-01 14:01:37 -05001364xpc_partition_going_down(struct xpc_partition *part, enum xpc_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001365{
1366 unsigned long irq_flags;
1367 int ch_number;
1368 struct xpc_channel *ch;
1369
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001370 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
Dean Nelsona607c382005-09-01 14:01:37 -05001378 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001379
1380 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1381 ch = &part->channels[ch_number];
1382
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001383 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001397/*
1398 * Teardown the infrastructure necessary to support XPartition Communication
1399 * between the specified remote partition and the local one.
1400 */
1401void
1402xpc_teardown_infrastructure(struct xpc_partition *part)
1403{
1404 partid_t partid = XPC_PARTID(part);
1405
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001406 /*
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
Dean Nelsona607c382005-09-01 14:01:37 -05001415 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001416 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
Dean Nelson35190502008-04-22 14:48:55 -05001422 free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001423
1424 /*
Simon Arlott72fdbdc2007-05-11 14:55:43 -07001425 * Before proceeding with the teardown we have to wait until all
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001426 * existing references cease.
1427 */
1428 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1429
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001430 /* 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001450/*
1451 * Called by XP at the time of channel connection registration to cause
1452 * XPC to establish connections to all currently active partitions.
1453 */
1454void
1455xpc_initiate_connect(int ch_number)
1456{
1457 partid_t partid;
1458 struct xpc_partition *part;
1459 struct xpc_channel *ch;
1460
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001461 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
Dean Nelsone54af722005-10-25 14:07:43 -05001469 /*
1470 * Initiate the establishment of a connection on the
1471 * newly registered channel to the remote partition.
1472 */
1473 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001474 xpc_part_deref(part);
1475 }
1476 }
1477}
1478
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001479void
1480xpc_connected_callout(struct xpc_channel *ch)
1481{
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001482 /* 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,
Dean Nelson35190502008-04-22 14:48:55 -05001489 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001490
1491 dev_dbg(xpc_chan, "ch->func() returned, reason=xpcConnected, "
1492 "partid=%d, channel=%d\n", ch->partid, ch->number);
1493 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001494}
1495
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001496/*
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 */
1509void
1510xpc_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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001517 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
Dean Nelsona607c382005-09-01 14:01:37 -05001529 if (!(ch->flags & XPC_C_DISCONNECTED)) {
1530 ch->flags |= XPC_C_WDISCONNECT;
1531
1532 XPC_DISCONNECT_CHANNEL(ch, xpcUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -05001533 &irq_flags);
Dean Nelsona607c382005-09-01 14:01:37 -05001534 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001535
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001546/*
1547 * To disconnect a channel, and reflect it back to all who may be waiting.
1548 *
Dean Nelsona607c382005-09-01 14:01:37 -05001549 * 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().
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001552 *
1553 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
1554 */
1555void
1556xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson35190502008-04-22 14:48:55 -05001557 enum xpc_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001558{
Dean Nelsona607c382005-09-01 14:01:37 -05001559 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001560
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001561 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
Dean Nelsona607c382005-09-01 14:01:37 -05001573 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001574 /* some of these may not have been set */
1575 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -05001576 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
1577 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001578
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001579 xpc_IPI_send_closerequest(ch, irq_flags);
1580
Dean Nelsona607c382005-09-01 14:01:37 -05001581 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001582 ch->flags |= XPC_C_WASCONNECTED;
1583 }
1584
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001585 spin_unlock_irqrestore(&ch->lock, *irq_flags);
1586
Dean Nelsona607c382005-09-01 14:01:37 -05001587 /* wake all idle kthreads so they can exit */
1588 if (atomic_read(&ch->kthreads_idle) > 0) {
1589 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -06001590
1591 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -05001592 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelsona460ef82006-11-22 08:25:00 -06001593 /* start a kthread that will do the xpcDisconnecting callout */
1594 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001595 }
1596
Dean Nelsona607c382005-09-01 14:01:37 -05001597 /* 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);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001600 }
1601
1602 spin_lock_irqsave(&ch->lock, *irq_flags);
1603}
1604
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001605void
Dean Nelson246c7e32005-12-22 14:32:56 -06001606xpc_disconnect_callout(struct xpc_channel *ch, enum xpc_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001607{
1608 /*
Dean Nelsona607c382005-09-01 14:01:37 -05001609 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001610 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c382005-09-01 14:01:37 -05001611 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001612 */
1613
1614 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -06001615 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
1616 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001617
Dean Nelson246c7e32005-12-22 14:32:56 -06001618 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001619
Dean Nelson246c7e32005-12-22 14:32:56 -06001620 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
1621 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001622 }
1623}
1624
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001625/*
1626 * Wait for a message entry to become available for the specified channel,
1627 * but don't wait any longer than 1 jiffy.
1628 */
1629static enum xpc_retval
1630xpc_allocate_msg_wait(struct xpc_channel *ch)
1631{
1632 enum xpc_retval ret;
1633
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001634 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson35190502008-04-22 14:48:55 -05001635 DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001636 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;
Dean Nelson35190502008-04-22 14:48:55 -05001645 DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001646 } else if (ret == 0) {
1647 ret = xpcTimeout;
1648 } else {
1649 ret = xpcInterrupted;
1650 }
1651
1652 return ret;
1653}
1654
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001655/*
1656 * Allocate an entry for a message from the message queue associated with the
1657 * specified channel.
1658 */
1659static enum xpc_retval
1660xpc_allocate_msg(struct xpc_channel *ch, u32 flags,
Dean Nelson35190502008-04-22 14:48:55 -05001661 struct xpc_msg **address_of_msg)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001662{
1663 struct xpc_msg *msg;
1664 enum xpc_retval ret;
1665 s64 put;
1666
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001667 /* 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001679 /*
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
Dean Nelson35190502008-04-22 14:48:55 -05001688 put = (volatile s64)ch->w_local_GP.put;
1689 if (put - (volatile s64)ch->w_remote_GP.get <
1690 ch->local_nentries) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001691
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.
Dean Nelson35190502008-04-22 14:48:55 -05001697 */
1698 if (cmpxchg(&ch->w_local_GP.put, put, put + 1) == put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001699 /* we got the entry referenced by put */
1700 break;
1701 }
1702 continue; /* try again */
1703 }
1704
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001705 /*
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001730 /* get the message's address and initialize it */
Dean Nelson35190502008-04-22 14:48:55 -05001731 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1732 (put % ch->local_nentries) * ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001733
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,
Dean Nelson35190502008-04-22 14:48:55 -05001739 (void *)msg, msg->number, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001740
1741 *address_of_msg = msg;
1742
1743 return xpcSuccess;
1744}
1745
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001746/*
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 */
1759enum xpc_retval
1760xpc_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;
Tony Luck27f4aa32006-04-04 14:11:49 -07001764 struct xpc_msg *msg = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001765
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001766 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001783/*
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 */
1788static void
1789xpc_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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001795 while (1) {
1796
1797 while (1) {
Dean Nelson35190502008-04-22 14:48:55 -05001798 if (put == (volatile s64)ch->w_local_GP.put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001799 break;
1800 }
1801
Dean Nelson35190502008-04-22 14:48:55 -05001802 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1803 (put % ch->local_nentries) *
1804 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001805
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) !=
Dean Nelson35190502008-04-22 14:48:55 -05001819 initial_put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001820 /* someone else beat us to it */
Dean Nelson35190502008-04-22 14:48:55 -05001821 DBUG_ON((volatile s64)ch->local_GP->put < initial_put);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001822 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001845/*
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 */
1850static enum xpc_retval
1851xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type,
Dean Nelson35190502008-04-22 14:48:55 -05001852 xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001853{
1854 enum xpc_retval ret = xpcSuccess;
Dean Nelsona607c382005-09-01 14:01:37 -05001855 struct xpc_notify *notify = notify;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001856 s64 put, msg_number = msg->number;
1857
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001858 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
Dean Nelson35190502008-04-22 14:48:55 -05001859 DBUG_ON((((u64)msg - (u64)ch->local_msgqueue) / ch->msg_size) !=
1860 msg_number % ch->local_nentries);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001861 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;
Dave Jones821fe942005-06-25 14:54:29 -07001881 notify->type = notify_type;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001882
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) ==
Dean Nelson35190502008-04-22 14:48:55 -05001894 notify_type) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001895 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001925/*
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 */
1945enum xpc_retval
1946xpc_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
Dean Nelson35190502008-04-22 14:48:55 -05001952 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001953 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001964/*
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 */
1994enum xpc_retval
1995xpc_initiate_send_notify(partid_t partid, int ch_number, void *payload,
Dean Nelson35190502008-04-22 14:48:55 -05001996 xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001997{
1998 struct xpc_partition *part = &xpc_partitions[partid];
1999 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2000 enum xpc_retval ret;
2001
Dean Nelson35190502008-04-22 14:48:55 -05002002 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002003 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,
Dean Nelson35190502008-04-22 14:48:55 -05002011 func, key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002012 return ret;
2013}
2014
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002015static struct xpc_msg *
2016xpc_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
Jes Sorensenf9e505a2006-01-17 12:52:21 -05002024 if (mutex_lock_interruptible(&ch->msg_to_pull_mutex) != 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002025 /* 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 >=
Dean Nelson35190502008-04-22 14:48:55 -05002036 (volatile s64)ch->w_remote_GP.put);
2037 nmsgs = (volatile s64)ch->w_remote_GP.put -
2038 ch->next_msg_to_pull;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002039 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;
Dean Nelson35190502008-04-22 14:48:55 -05002045 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
2046 remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
2047 msg_offset);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002048
2049 if ((ret = xpc_pull_remote_cachelines(part, msg, remote_msg,
Dean Nelson35190502008-04-22 14:48:55 -05002050 nmsgs * ch->msg_size)) !=
2051 xpcSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002052
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
Jes Sorensenf9e505a2006-01-17 12:52:21 -05002060 mutex_unlock(&ch->msg_to_pull_mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002061 return NULL;
2062 }
2063
Dean Nelson35190502008-04-22 14:48:55 -05002064 mb(); /* >>> this may not be needed, we're not sure */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002065
2066 ch->next_msg_to_pull += nmsgs;
2067 }
2068
Jes Sorensenf9e505a2006-01-17 12:52:21 -05002069 mutex_unlock(&ch->msg_to_pull_mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002070
2071 /* return the message we were looking for */
2072 msg_offset = (get % ch->remote_nentries) * ch->msg_size;
Dean Nelson35190502008-04-22 14:48:55 -05002073 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002074
2075 return msg;
2076}
2077
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002078/*
2079 * Get a message to be delivered.
2080 */
2081static struct xpc_msg *
2082xpc_get_deliverable_msg(struct xpc_channel *ch)
2083{
2084 struct xpc_msg *msg = NULL;
2085 s64 get;
2086
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002087 do {
Dean Nelson35190502008-04-22 14:48:55 -05002088 if ((volatile u32)ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002089 break;
2090 }
2091
Dean Nelson35190502008-04-22 14:48:55 -05002092 get = (volatile s64)ch->w_local_GP.get;
2093 if (get == (volatile s64)ch->w_remote_GP.put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002094 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.
Dean Nelson35190502008-04-22 14:48:55 -05002102 */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002103
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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002127/*
2128 * Deliver a message to its intended recipient.
2129 */
2130void
2131xpc_deliver_msg(struct xpc_channel *ch)
2132{
2133 struct xpc_msg *msg;
2134
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002135 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",
Dean Nelson35190502008-04-22 14:48:55 -05002149 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002150 ch->number);
2151
2152 /* deliver the message to its intended recipient */
2153 ch->func(xpcMsgReceived, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -05002154 &msg->payload, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002155
2156 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
2157 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05002158 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002159 ch->number);
2160 }
2161
2162 atomic_dec(&ch->kthreads_active);
2163 }
2164}
2165
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002166/*
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 */
2171static void
2172xpc_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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002178 while (1) {
2179
2180 while (1) {
Dean Nelson35190502008-04-22 14:48:55 -05002181 if (get == (volatile s64)ch->w_local_GP.get) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002182 break;
2183 }
2184
Dean Nelson35190502008-04-22 14:48:55 -05002185 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
2186 (get % ch->remote_nentries) *
2187 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002188
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) !=
Dean Nelson35190502008-04-22 14:48:55 -05002203 initial_get) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002204 /* someone else beat us to it */
Dean Nelson35190502008-04-22 14:48:55 -05002205 DBUG_ON((volatile s64)ch->local_GP->get <= initial_get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002206 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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002229/*
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 */
2246void
2247xpc_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
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002254 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",
Dean Nelson35190502008-04-22 14:48:55 -05002260 (void *)msg, msg_number, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002261
Dean Nelson35190502008-04-22 14:48:55 -05002262 DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
2263 msg_number % ch->remote_nentries);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002264 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}