blob: 9e79ad7eafe5cd911b45eaf550feaea1159523dc [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/sn_sal.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050025#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070026
Dean Nelson89eb8eb2005-03-23 19:50:00 -070027/*
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050028 * Guarantee that the kzalloc'd memory is cacheline aligned.
29 */
30static void *
31xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
32{
33 /* see if kzalloc will give us cachline aligned memory by default */
34 *base = kzalloc(size, flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050035 if (*base == NULL)
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050036 return NULL;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050037
38 if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050039 return *base;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050040
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050041 kfree(*base);
42
43 /* nope, we'll have to do it ourselves */
44 *base = kzalloc(size + L1_CACHE_BYTES, flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050045 if (*base == NULL)
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050046 return NULL;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050047
Dean Nelson35190502008-04-22 14:48:55 -050048 return (void *)L1_CACHE_ALIGN((u64)*base);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050049}
50
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050051/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070052 * Set up the initial values for the XPartition Communication channels.
53 */
54static void
Dean Nelson64d032b2008-05-12 14:02:03 -070055xpc_initialize_channels(struct xpc_partition *part, short partid)
Dean Nelson89eb8eb2005-03-23 19:50:00 -070056{
57 int ch_number;
58 struct xpc_channel *ch;
59
Dean Nelson89eb8eb2005-03-23 19:50:00 -070060 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
61 ch = &part->channels[ch_number];
62
63 ch->partid = partid;
64 ch->number = ch_number;
65 ch->flags = XPC_C_DISCONNECTED;
66
67 ch->local_GP = &part->local_GPs[ch_number];
68 ch->local_openclose_args =
Dean Nelson35190502008-04-22 14:48:55 -050069 &part->local_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -070070
71 atomic_set(&ch->kthreads_assigned, 0);
72 atomic_set(&ch->kthreads_idle, 0);
73 atomic_set(&ch->kthreads_active, 0);
74
75 atomic_set(&ch->references, 0);
76 atomic_set(&ch->n_to_notify, 0);
77
78 spin_lock_init(&ch->lock);
Jes Sorensenf9e505a2006-01-17 12:52:21 -050079 mutex_init(&ch->msg_to_pull_mutex);
80 init_completion(&ch->wdisconnect_wait);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070081
82 atomic_set(&ch->n_on_msg_allocate_wq, 0);
83 init_waitqueue_head(&ch->msg_allocate_wq);
84 init_waitqueue_head(&ch->idle_wq);
85 }
86}
87
Dean Nelson89eb8eb2005-03-23 19:50:00 -070088/*
89 * Setup the infrastructure necessary to support XPartition Communication
90 * between the specified remote partition and the local one.
91 */
Dean Nelson65c17b82008-05-12 14:02:02 -070092enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -070093xpc_setup_infrastructure(struct xpc_partition *part)
94{
Dean Nelson59a0a8a2005-07-13 05:45:00 -070095 int ret, cpuid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070096 struct timer_list *timer;
Dean Nelson64d032b2008-05-12 14:02:03 -070097 short partid = XPC_PARTID(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070098
Dean Nelson89eb8eb2005-03-23 19:50:00 -070099 /*
100 * Zero out MOST of the entry for this partition. Only the fields
101 * starting with `nchannels' will be zeroed. The preceding fields must
102 * remain `viable' across partition ups and downs, since they may be
103 * referenced during this memset() operation.
104 */
105 memset(&part->nchannels, 0, sizeof(struct xpc_partition) -
Dean Nelson35190502008-04-22 14:48:55 -0500106 offsetof(struct xpc_partition, nchannels));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700107
108 /*
109 * Allocate all of the channel structures as a contiguous chunk of
110 * memory.
111 */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700112 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
Dean Nelson35190502008-04-22 14:48:55 -0500113 GFP_KERNEL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700114 if (part->channels == NULL) {
115 dev_err(xpc_chan, "can't get memory for channels\n");
Dean Nelson65c17b82008-05-12 14:02:02 -0700116 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700117 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700118
Dean Nelsonbc63d382008-07-29 22:34:04 -0700119 part->nchannels = XPC_MAX_NCHANNELS;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700120
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700121 /* allocate all the required GET/PUT values */
122
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500123 part->local_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
Dean Nelson35190502008-04-22 14:48:55 -0500124 GFP_KERNEL,
125 &part->local_GPs_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700126 if (part->local_GPs == NULL) {
127 kfree(part->channels);
128 part->channels = NULL;
129 dev_err(xpc_chan, "can't get memory for local get/put "
130 "values\n");
Dean Nelson65c17b82008-05-12 14:02:02 -0700131 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700132 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700133
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500134 part->remote_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
Dean Nelson35190502008-04-22 14:48:55 -0500135 GFP_KERNEL,
136 &part->
137 remote_GPs_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700138 if (part->remote_GPs == NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700139 dev_err(xpc_chan, "can't get memory for remote get/put "
140 "values\n");
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500141 kfree(part->local_GPs_base);
142 part->local_GPs = NULL;
143 kfree(part->channels);
144 part->channels = NULL;
Dean Nelson65c17b82008-05-12 14:02:02 -0700145 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700146 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700147
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700148 /* allocate all the required open and close args */
149
Dean Nelson35190502008-04-22 14:48:55 -0500150 part->local_openclose_args =
151 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
152 &part->local_openclose_args_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700153 if (part->local_openclose_args == NULL) {
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500154 dev_err(xpc_chan, "can't get memory for local connect args\n");
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700155 kfree(part->remote_GPs_base);
156 part->remote_GPs = NULL;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500157 kfree(part->local_GPs_base);
158 part->local_GPs = NULL;
159 kfree(part->channels);
160 part->channels = NULL;
Dean Nelson65c17b82008-05-12 14:02:02 -0700161 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700162 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700163
Dean Nelson35190502008-04-22 14:48:55 -0500164 part->remote_openclose_args =
165 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
166 &part->remote_openclose_args_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700167 if (part->remote_openclose_args == NULL) {
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500168 dev_err(xpc_chan, "can't get memory for remote connect args\n");
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700169 kfree(part->local_openclose_args_base);
170 part->local_openclose_args = NULL;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500171 kfree(part->remote_GPs_base);
172 part->remote_GPs = NULL;
173 kfree(part->local_GPs_base);
174 part->local_GPs = NULL;
175 kfree(part->channels);
176 part->channels = NULL;
Dean Nelson65c17b82008-05-12 14:02:02 -0700177 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700178 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700179
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700180 xpc_initialize_channels(part, partid);
181
182 atomic_set(&part->nchannels_active, 0);
Dean Nelsona607c382005-09-01 14:01:37 -0500183 atomic_set(&part->nchannels_engaged, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700184
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700185 /* local_IPI_amo were set to 0 by an earlier memset() */
186
187 /* Initialize this partitions AMO_t structure */
188 part->local_IPI_amo_va = xpc_IPI_init(partid);
189
190 spin_lock_init(&part->IPI_lock);
191
192 atomic_set(&part->channel_mgr_requests, 1);
193 init_waitqueue_head(&part->channel_mgr_wq);
194
195 sprintf(part->IPI_owner, "xpc%02d", partid);
Thomas Gleixner121a4222006-07-01 19:29:17 -0700196 ret = request_irq(SGI_XPC_NOTIFY, xpc_notify_IRQ_handler, IRQF_SHARED,
Dean Nelson35190502008-04-22 14:48:55 -0500197 part->IPI_owner, (void *)(u64)partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700198 if (ret != 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700199 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
200 "errno=%d\n", -ret);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500201 kfree(part->remote_openclose_args_base);
202 part->remote_openclose_args = NULL;
203 kfree(part->local_openclose_args_base);
204 part->local_openclose_args = NULL;
205 kfree(part->remote_GPs_base);
206 part->remote_GPs = NULL;
207 kfree(part->local_GPs_base);
208 part->local_GPs = NULL;
209 kfree(part->channels);
210 part->channels = NULL;
Dean Nelson65c17b82008-05-12 14:02:02 -0700211 return xpLackOfResources;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700212 }
213
214 /* Setup a timer to check for dropped IPIs */
215 timer = &part->dropped_IPI_timer;
216 init_timer(timer);
Dean Nelson35190502008-04-22 14:48:55 -0500217 timer->function = (void (*)(unsigned long))xpc_dropped_IPI_check;
218 timer->data = (unsigned long)part;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700219 timer->expires = jiffies + XPC_P_DROPPED_IPI_WAIT;
220 add_timer(timer);
221
222 /*
223 * With the setting of the partition setup_state to XPC_P_SETUP, we're
224 * declaring that this partition is ready to go.
225 */
Dave Jones821fe942005-06-25 14:54:29 -0700226 part->setup_state = XPC_P_SETUP;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700227
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700228 /*
229 * Setup the per partition specific variables required by the
230 * remote partition to establish channel connections with us.
231 *
232 * The setting of the magic # indicates that these per partition
233 * specific variables are ready to be used.
234 */
235 xpc_vars_part[partid].GPs_pa = __pa(part->local_GPs);
236 xpc_vars_part[partid].openclose_args_pa =
Dean Nelson35190502008-04-22 14:48:55 -0500237 __pa(part->local_openclose_args);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700238 xpc_vars_part[partid].IPI_amo_pa = __pa(part->local_IPI_amo_va);
Dean Nelson59a0a8a2005-07-13 05:45:00 -0700239 cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
240 xpc_vars_part[partid].IPI_nasid = cpuid_to_nasid(cpuid);
241 xpc_vars_part[partid].IPI_phys_cpuid = cpu_physical_id(cpuid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700242 xpc_vars_part[partid].nchannels = part->nchannels;
Dave Jones821fe942005-06-25 14:54:29 -0700243 xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700244
Dean Nelson65c17b82008-05-12 14:02:02 -0700245 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700246}
247
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700248/*
249 * Create a wrapper that hides the underlying mechanism for pulling a cacheline
250 * (or multiple cachelines) from a remote partition.
251 *
252 * src must be a cacheline aligned physical address on the remote partition.
253 * dst must be a cacheline aligned virtual address on this partition.
Dean Nelson908787d2008-07-29 22:34:05 -0700254 * cnt must be cacheline sized
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700255 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700256static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700257xpc_pull_remote_cachelines(struct xpc_partition *part, void *dst,
Dean Nelson35190502008-04-22 14:48:55 -0500258 const void *src, size_t cnt)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700259{
Dean Nelson908787d2008-07-29 22:34:05 -0700260 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700261
Dean Nelson35190502008-04-22 14:48:55 -0500262 DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
263 DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700264 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
265
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500266 if (part->act_state == XPC_P_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700267 return part->reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700268
Dean Nelson908787d2008-07-29 22:34:05 -0700269 ret = xp_remote_memcpy(dst, src, cnt);
270 if (ret != xpSuccess) {
271 dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
272 " ret=%d\n", XPC_PARTID(part), ret);
273 }
274 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700275}
276
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700277/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700278 * Pull the remote per partition specific variables from the specified
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700279 * partition.
280 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700281enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700282xpc_pull_remote_vars_part(struct xpc_partition *part)
283{
284 u8 buffer[L1_CACHE_BYTES * 2];
285 struct xpc_vars_part *pulled_entry_cacheline =
Dean Nelson35190502008-04-22 14:48:55 -0500286 (struct xpc_vars_part *)L1_CACHE_ALIGN((u64)buffer);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700287 struct xpc_vars_part *pulled_entry;
288 u64 remote_entry_cacheline_pa, remote_entry_pa;
Dean Nelson64d032b2008-05-12 14:02:03 -0700289 short partid = XPC_PARTID(part);
Dean Nelson65c17b82008-05-12 14:02:02 -0700290 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700291
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700292 /* pull the cacheline that contains the variables we're interested in */
293
294 DBUG_ON(part->remote_vars_part_pa !=
Dean Nelson35190502008-04-22 14:48:55 -0500295 L1_CACHE_ALIGN(part->remote_vars_part_pa));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700296 DBUG_ON(sizeof(struct xpc_vars_part) != L1_CACHE_BYTES / 2);
297
298 remote_entry_pa = part->remote_vars_part_pa +
Dean Nelson35190502008-04-22 14:48:55 -0500299 sn_partition_id * sizeof(struct xpc_vars_part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700300
301 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
302
Dean Nelson35190502008-04-22 14:48:55 -0500303 pulled_entry = (struct xpc_vars_part *)((u64)pulled_entry_cacheline +
304 (remote_entry_pa &
305 (L1_CACHE_BYTES - 1)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700306
307 ret = xpc_pull_remote_cachelines(part, pulled_entry_cacheline,
Dean Nelson35190502008-04-22 14:48:55 -0500308 (void *)remote_entry_cacheline_pa,
309 L1_CACHE_BYTES);
Dean Nelson65c17b82008-05-12 14:02:02 -0700310 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700311 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
312 "partition %d, ret=%d\n", partid, ret);
313 return ret;
314 }
315
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700316 /* see if they've been set up yet */
317
318 if (pulled_entry->magic != XPC_VP_MAGIC1 &&
Dean Nelson35190502008-04-22 14:48:55 -0500319 pulled_entry->magic != XPC_VP_MAGIC2) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700320
321 if (pulled_entry->magic != 0) {
322 dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
323 "partition %d has bad magic value (=0x%lx)\n",
324 partid, sn_partition_id, pulled_entry->magic);
Dean Nelson65c17b82008-05-12 14:02:02 -0700325 return xpBadMagic;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700326 }
327
328 /* they've not been initialized yet */
Dean Nelson65c17b82008-05-12 14:02:02 -0700329 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700330 }
331
332 if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
333
334 /* validate the variables */
335
336 if (pulled_entry->GPs_pa == 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500337 pulled_entry->openclose_args_pa == 0 ||
338 pulled_entry->IPI_amo_pa == 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700339
340 dev_err(xpc_chan, "partition %d's XPC vars_part for "
341 "partition %d are not valid\n", partid,
342 sn_partition_id);
Dean Nelson65c17b82008-05-12 14:02:02 -0700343 return xpInvalidAddress;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700344 }
345
346 /* the variables we imported look to be valid */
347
348 part->remote_GPs_pa = pulled_entry->GPs_pa;
349 part->remote_openclose_args_pa =
Dean Nelson35190502008-04-22 14:48:55 -0500350 pulled_entry->openclose_args_pa;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700351 part->remote_IPI_amo_va =
Dean Nelson35190502008-04-22 14:48:55 -0500352 (AMO_t *)__va(pulled_entry->IPI_amo_pa);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700353 part->remote_IPI_nasid = pulled_entry->IPI_nasid;
354 part->remote_IPI_phys_cpuid = pulled_entry->IPI_phys_cpuid;
355
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500356 if (part->nchannels > pulled_entry->nchannels)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700357 part->nchannels = pulled_entry->nchannels;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700358
359 /* let the other side know that we've pulled their variables */
360
Dave Jones821fe942005-06-25 14:54:29 -0700361 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700362 }
363
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500364 if (pulled_entry->magic == XPC_VP_MAGIC1)
Dean Nelson65c17b82008-05-12 14:02:02 -0700365 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700366
Dean Nelson65c17b82008-05-12 14:02:02 -0700367 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700368}
369
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700370/*
371 * Get the IPI flags and pull the openclose args and/or remote GPs as needed.
372 */
373static u64
374xpc_get_IPI_flags(struct xpc_partition *part)
375{
376 unsigned long irq_flags;
377 u64 IPI_amo;
Dean Nelson65c17b82008-05-12 14:02:02 -0700378 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700379
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700380 /*
381 * See if there are any IPI flags to be handled.
382 */
383
384 spin_lock_irqsave(&part->IPI_lock, irq_flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500385 IPI_amo = part->local_IPI_amo;
386 if (IPI_amo != 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700387 part->local_IPI_amo = 0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500388
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700389 spin_unlock_irqrestore(&part->IPI_lock, irq_flags);
390
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700391 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_amo)) {
392 ret = xpc_pull_remote_cachelines(part,
Dean Nelson35190502008-04-22 14:48:55 -0500393 part->remote_openclose_args,
394 (void *)part->
395 remote_openclose_args_pa,
396 XPC_OPENCLOSE_ARGS_SIZE);
Dean Nelson65c17b82008-05-12 14:02:02 -0700397 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700398 XPC_DEACTIVATE_PARTITION(part, ret);
399
400 dev_dbg(xpc_chan, "failed to pull openclose args from "
401 "partition %d, ret=%d\n", XPC_PARTID(part),
402 ret);
403
404 /* don't bother processing IPIs anymore */
405 IPI_amo = 0;
406 }
407 }
408
409 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_amo)) {
410 ret = xpc_pull_remote_cachelines(part, part->remote_GPs,
Dean Nelson35190502008-04-22 14:48:55 -0500411 (void *)part->remote_GPs_pa,
412 XPC_GP_SIZE);
Dean Nelson65c17b82008-05-12 14:02:02 -0700413 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700414 XPC_DEACTIVATE_PARTITION(part, ret);
415
416 dev_dbg(xpc_chan, "failed to pull GPs from partition "
417 "%d, ret=%d\n", XPC_PARTID(part), ret);
418
419 /* don't bother processing IPIs anymore */
420 IPI_amo = 0;
421 }
422 }
423
424 return IPI_amo;
425}
426
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700427/*
428 * Allocate the local message queue and the notify queue.
429 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700430static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700431xpc_allocate_local_msgqueue(struct xpc_channel *ch)
432{
433 unsigned long irq_flags;
434 int nentries;
435 size_t nbytes;
436
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700437 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
438
439 nbytes = nentries * ch->msg_size;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500440 ch->local_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
Dean Nelson35190502008-04-22 14:48:55 -0500441 GFP_KERNEL,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500442 &ch->local_msgqueue_base);
443 if (ch->local_msgqueue == NULL)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700444 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700445
446 nbytes = nentries * sizeof(struct xpc_notify);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500447 ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700448 if (ch->notify_queue == NULL) {
449 kfree(ch->local_msgqueue_base);
450 ch->local_msgqueue = NULL;
451 continue;
452 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700453
454 spin_lock_irqsave(&ch->lock, irq_flags);
455 if (nentries < ch->local_nentries) {
456 dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
457 "partid=%d, channel=%d\n", nentries,
458 ch->local_nentries, ch->partid, ch->number);
459
460 ch->local_nentries = nentries;
461 }
462 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700463 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700464 }
465
466 dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
467 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson65c17b82008-05-12 14:02:02 -0700468 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700469}
470
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700471/*
472 * Allocate the cached remote message queue.
473 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700474static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700475xpc_allocate_remote_msgqueue(struct xpc_channel *ch)
476{
477 unsigned long irq_flags;
478 int nentries;
479 size_t nbytes;
480
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700481 DBUG_ON(ch->remote_nentries <= 0);
482
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700483 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
484
485 nbytes = nentries * ch->msg_size;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500486 ch->remote_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
Dean Nelson35190502008-04-22 14:48:55 -0500487 GFP_KERNEL,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500488 &ch->remote_msgqueue_base);
489 if (ch->remote_msgqueue == NULL)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700490 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700491
492 spin_lock_irqsave(&ch->lock, irq_flags);
493 if (nentries < ch->remote_nentries) {
494 dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
495 "partid=%d, channel=%d\n", nentries,
496 ch->remote_nentries, ch->partid, ch->number);
497
498 ch->remote_nentries = nentries;
499 }
500 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700501 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700502 }
503
504 dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
505 "partid=%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson65c17b82008-05-12 14:02:02 -0700506 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700507}
508
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700509/*
510 * Allocate message queues and other stuff associated with a channel.
511 *
512 * Note: Assumes all of the channel sizes are filled in.
513 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700514static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700515xpc_allocate_msgqueues(struct xpc_channel *ch)
516{
517 unsigned long irq_flags;
Dean Nelson65c17b82008-05-12 14:02:02 -0700518 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700519
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700520 DBUG_ON(ch->flags & XPC_C_SETUP);
521
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500522 ret = xpc_allocate_local_msgqueue(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -0700523 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700524 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700525
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500526 ret = xpc_allocate_remote_msgqueue(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -0700527 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700528 kfree(ch->local_msgqueue_base);
529 ch->local_msgqueue = NULL;
530 kfree(ch->notify_queue);
531 ch->notify_queue = NULL;
532 return ret;
533 }
534
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700535 spin_lock_irqsave(&ch->lock, irq_flags);
536 ch->flags |= XPC_C_SETUP;
537 spin_unlock_irqrestore(&ch->lock, irq_flags);
538
Dean Nelson65c17b82008-05-12 14:02:02 -0700539 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700540}
541
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700542/*
543 * Process a connect message from a remote partition.
544 *
545 * Note: xpc_process_connect() is expecting to be called with the
546 * spin_lock_irqsave held and will leave it locked upon return.
547 */
548static void
549xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
550{
Dean Nelson65c17b82008-05-12 14:02:02 -0700551 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700552
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700553 DBUG_ON(!spin_is_locked(&ch->lock));
554
555 if (!(ch->flags & XPC_C_OPENREQUEST) ||
Dean Nelson35190502008-04-22 14:48:55 -0500556 !(ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700557 /* nothing more to do for now */
558 return;
559 }
560 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
561
562 if (!(ch->flags & XPC_C_SETUP)) {
563 spin_unlock_irqrestore(&ch->lock, *irq_flags);
564 ret = xpc_allocate_msgqueues(ch);
565 spin_lock_irqsave(&ch->lock, *irq_flags);
566
Dean Nelson65c17b82008-05-12 14:02:02 -0700567 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700568 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500569
570 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700571 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700572
573 DBUG_ON(!(ch->flags & XPC_C_SETUP));
574 DBUG_ON(ch->local_msgqueue == NULL);
575 DBUG_ON(ch->remote_msgqueue == NULL);
576 }
577
578 if (!(ch->flags & XPC_C_OPENREPLY)) {
579 ch->flags |= XPC_C_OPENREPLY;
580 xpc_IPI_send_openreply(ch, irq_flags);
581 }
582
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500583 if (!(ch->flags & XPC_C_ROPENREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700584 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700585
586 DBUG_ON(ch->remote_msgqueue_pa == 0);
587
588 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
589
590 dev_info(xpc_chan, "channel %d to partition %d connected\n",
Dean Nelson35190502008-04-22 14:48:55 -0500591 ch->number, ch->partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700592
593 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -0600594 xpc_create_kthreads(ch, 1, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700595 spin_lock_irqsave(&ch->lock, *irq_flags);
596}
597
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700598/*
Dean Nelsona607c382005-09-01 14:01:37 -0500599 * Notify those who wanted to be notified upon delivery of their message.
600 */
601static void
Dean Nelson65c17b82008-05-12 14:02:02 -0700602xpc_notify_senders(struct xpc_channel *ch, enum xp_retval reason, s64 put)
Dean Nelsona607c382005-09-01 14:01:37 -0500603{
604 struct xpc_notify *notify;
605 u8 notify_type;
606 s64 get = ch->w_remote_GP.get - 1;
607
Dean Nelsona607c382005-09-01 14:01:37 -0500608 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
609
610 notify = &ch->notify_queue[get % ch->local_nentries];
611
612 /*
613 * See if the notify entry indicates it was associated with
614 * a message who's sender wants to be notified. It is possible
615 * that it is, but someone else is doing or has done the
616 * notification.
617 */
618 notify_type = notify->type;
619 if (notify_type == 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500620 cmpxchg(&notify->type, notify_type, 0) != notify_type) {
Dean Nelsona607c382005-09-01 14:01:37 -0500621 continue;
622 }
623
624 DBUG_ON(notify_type != XPC_N_CALL);
625
626 atomic_dec(&ch->n_to_notify);
627
628 if (notify->func != NULL) {
629 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
630 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -0500631 (void *)notify, get, ch->partid, ch->number);
Dean Nelsona607c382005-09-01 14:01:37 -0500632
633 notify->func(reason, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500634 notify->key);
Dean Nelsona607c382005-09-01 14:01:37 -0500635
636 dev_dbg(xpc_chan, "notify->func() returned, "
637 "notify=0x%p, msg_number=%ld, partid=%d, "
Dean Nelson35190502008-04-22 14:48:55 -0500638 "channel=%d\n", (void *)notify, get,
Dean Nelsona607c382005-09-01 14:01:37 -0500639 ch->partid, ch->number);
640 }
641 }
642}
643
Dean Nelsona607c382005-09-01 14:01:37 -0500644/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700645 * Free up message queues and other stuff that were allocated for the specified
646 * channel.
647 *
648 * Note: ch->reason and ch->reason_line are left set for debugging purposes,
649 * they're cleared when XPC_C_DISCONNECTED is cleared.
650 */
651static void
652xpc_free_msgqueues(struct xpc_channel *ch)
653{
654 DBUG_ON(!spin_is_locked(&ch->lock));
655 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
656
657 ch->remote_msgqueue_pa = 0;
658 ch->func = NULL;
659 ch->key = NULL;
660 ch->msg_size = 0;
661 ch->local_nentries = 0;
662 ch->remote_nentries = 0;
663 ch->kthreads_assigned_limit = 0;
664 ch->kthreads_idle_limit = 0;
665
666 ch->local_GP->get = 0;
667 ch->local_GP->put = 0;
668 ch->remote_GP.get = 0;
669 ch->remote_GP.put = 0;
670 ch->w_local_GP.get = 0;
671 ch->w_local_GP.put = 0;
672 ch->w_remote_GP.get = 0;
673 ch->w_remote_GP.put = 0;
674 ch->next_msg_to_pull = 0;
675
676 if (ch->flags & XPC_C_SETUP) {
677 ch->flags &= ~XPC_C_SETUP;
678
679 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
680 ch->flags, ch->partid, ch->number);
681
682 kfree(ch->local_msgqueue_base);
683 ch->local_msgqueue = NULL;
684 kfree(ch->remote_msgqueue_base);
685 ch->remote_msgqueue = NULL;
686 kfree(ch->notify_queue);
687 ch->notify_queue = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700688 }
689}
690
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700691/*
692 * spin_lock_irqsave() is expected to be held on entry.
693 */
694static void
695xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
696{
697 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c382005-09-01 14:01:37 -0500698 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700699
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700700 DBUG_ON(!spin_is_locked(&ch->lock));
701
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500702 if (!(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700703 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700704
705 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
706
707 /* make sure all activity has settled down first */
708
Dean Nelsona460ef82006-11-22 08:25:00 -0600709 if (atomic_read(&ch->kthreads_assigned) > 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500710 atomic_read(&ch->references) > 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700711 return;
712 }
Dean Nelsona460ef82006-11-22 08:25:00 -0600713 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500714 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700715
Dean Nelsona607c382005-09-01 14:01:37 -0500716 if (part->act_state == XPC_P_DEACTIVATING) {
717 /* can't proceed until the other side disengages from us */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500718 if (xpc_partition_engaged(1UL << ch->partid))
Dean Nelsona607c382005-09-01 14:01:37 -0500719 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700720
Dean Nelsona607c382005-09-01 14:01:37 -0500721 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700722
723 /* as long as the other side is up do the full protocol */
724
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500725 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700726 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700727
728 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
729 ch->flags |= XPC_C_CLOSEREPLY;
730 xpc_IPI_send_closereply(ch, irq_flags);
731 }
732
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500733 if (!(ch->flags & XPC_C_RCLOSEREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700734 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700735 }
736
Dean Nelsona607c382005-09-01 14:01:37 -0500737 /* wake those waiting for notify completion */
738 if (atomic_read(&ch->n_to_notify) > 0) {
739 /* >>> we do callout while holding ch->lock */
740 xpc_notify_senders(ch, ch->reason, ch->w_local_GP.put);
741 }
742
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700743 /* both sides are disconnected now */
744
Dean Nelson4c2cd962006-02-15 08:02:21 -0600745 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600746 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700747 xpc_disconnect_callout(ch, xpDisconnected);
Dean Nelson246c7e32005-12-22 14:32:56 -0600748 spin_lock_irqsave(&ch->lock, *irq_flags);
749 }
750
Dean Nelsona607c382005-09-01 14:01:37 -0500751 /* it's now safe to free the channel's message queues */
752 xpc_free_msgqueues(ch);
753
754 /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
755 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700756
757 atomic_dec(&part->nchannels_active);
758
Dean Nelsona607c382005-09-01 14:01:37 -0500759 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700760 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
Dean Nelson35190502008-04-22 14:48:55 -0500761 "reason=%d\n", ch->number, ch->partid, ch->reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700762 }
Dean Nelsona607c382005-09-01 14:01:37 -0500763
Dean Nelsona607c382005-09-01 14:01:37 -0500764 if (ch->flags & XPC_C_WDISCONNECT) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500765 /* we won't lose the CPU since we're holding ch->lock */
766 complete(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500767 } else if (ch->delayed_IPI_flags) {
768 if (part->act_state != XPC_P_DEACTIVATING) {
769 /* time to take action on any delayed IPI flags */
770 spin_lock(&part->IPI_lock);
771 XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500772 ch->delayed_IPI_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500773 spin_unlock(&part->IPI_lock);
774 }
775 ch->delayed_IPI_flags = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500776 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700777}
778
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700779/*
780 * Process a change in the channel's remote connection state.
781 */
782static void
783xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
Dean Nelson35190502008-04-22 14:48:55 -0500784 u8 IPI_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700785{
786 unsigned long irq_flags;
787 struct xpc_openclose_args *args =
Dean Nelson35190502008-04-22 14:48:55 -0500788 &part->remote_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700789 struct xpc_channel *ch = &part->channels[ch_number];
Dean Nelson65c17b82008-05-12 14:02:02 -0700790 enum xp_retval reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700791
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700792 spin_lock_irqsave(&ch->lock, irq_flags);
793
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500794again:
Dean Nelsone54af722005-10-25 14:07:43 -0500795
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500796 if ((ch->flags & XPC_C_DISCONNECTED) &&
797 (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500798 /*
799 * Delay processing IPI flags until thread waiting disconnect
800 * has had a chance to see that the channel is disconnected.
801 */
802 ch->delayed_IPI_flags |= IPI_flags;
803 spin_unlock_irqrestore(&ch->lock, irq_flags);
804 return;
805 }
806
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700807 if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
808
809 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
810 "from partid=%d, channel=%d\n", args->reason,
811 ch->partid, ch->number);
812
813 /*
814 * If RCLOSEREQUEST is set, we're probably waiting for
815 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelsona607c382005-09-01 14:01:37 -0500816 * with this RCLOSEREQUEST in the IPI_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700817 */
818
819 if (ch->flags & XPC_C_RCLOSEREQUEST) {
820 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
821 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
822 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
823 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
824
825 DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
826 IPI_flags &= ~XPC_IPI_CLOSEREPLY;
827 ch->flags |= XPC_C_RCLOSEREPLY;
828
829 /* both sides have finished disconnecting */
830 xpc_process_disconnect(ch, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500831 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
832 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700833 }
834
835 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700836 if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500837 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500838 ch_number) &
839 XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500840
841 DBUG_ON(ch->delayed_IPI_flags != 0);
842 spin_lock(&part->IPI_lock);
843 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500844 ch_number,
845 XPC_IPI_CLOSEREQUEST);
Dean Nelsone54af722005-10-25 14:07:43 -0500846 spin_unlock(&part->IPI_lock);
847 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700848 spin_unlock_irqrestore(&ch->lock, irq_flags);
849 return;
850 }
851
852 XPC_SET_REASON(ch, 0, 0);
853 ch->flags &= ~XPC_C_DISCONNECTED;
854
855 atomic_inc(&part->nchannels_active);
856 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
857 }
858
859 IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
860
861 /*
862 * The meaningful CLOSEREQUEST connection state fields are:
863 * reason = reason connection is to be closed
864 */
865
866 ch->flags |= XPC_C_RCLOSEREQUEST;
867
868 if (!(ch->flags & XPC_C_DISCONNECTING)) {
869 reason = args->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700870 if (reason <= xpSuccess || reason > xpUnknownReason)
871 reason = xpUnknownReason;
872 else if (reason == xpUnregistering)
873 reason = xpOtherUnregistering;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700874
875 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500876
877 DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
878 spin_unlock_irqrestore(&ch->lock, irq_flags);
879 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700880 }
Dean Nelsone54af722005-10-25 14:07:43 -0500881
882 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700883 }
884
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700885 if (IPI_flags & XPC_IPI_CLOSEREPLY) {
886
887 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
888 " channel=%d\n", ch->partid, ch->number);
889
890 if (ch->flags & XPC_C_DISCONNECTED) {
891 DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
892 spin_unlock_irqrestore(&ch->lock, irq_flags);
893 return;
894 }
895
896 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
Dean Nelsone54af722005-10-25 14:07:43 -0500897
898 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
899 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
Dean Nelson35190502008-04-22 14:48:55 -0500900 & XPC_IPI_CLOSEREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500901
902 DBUG_ON(ch->delayed_IPI_flags != 0);
903 spin_lock(&part->IPI_lock);
904 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500905 ch_number,
906 XPC_IPI_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500907 spin_unlock(&part->IPI_lock);
908 }
909 spin_unlock_irqrestore(&ch->lock, irq_flags);
910 return;
911 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700912
913 ch->flags |= XPC_C_RCLOSEREPLY;
914
915 if (ch->flags & XPC_C_CLOSEREPLY) {
916 /* both sides have finished disconnecting */
917 xpc_process_disconnect(ch, &irq_flags);
918 }
919 }
920
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700921 if (IPI_flags & XPC_IPI_OPENREQUEST) {
922
923 dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
924 "local_nentries=%d) received from partid=%d, "
925 "channel=%d\n", args->msg_size, args->local_nentries,
926 ch->partid, ch->number);
927
Dean Nelsone54af722005-10-25 14:07:43 -0500928 if (part->act_state == XPC_P_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500929 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500930 spin_unlock_irqrestore(&ch->lock, irq_flags);
931 return;
932 }
933
934 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
935 ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700936 spin_unlock_irqrestore(&ch->lock, irq_flags);
937 return;
938 }
939 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500940 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700941 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500942 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700943
944 /*
945 * The meaningful OPENREQUEST connection state fields are:
946 * msg_size = size of channel's messages in bytes
947 * local_nentries = remote partition's local_nentries
948 */
Dean Nelsone54af722005-10-25 14:07:43 -0500949 if (args->msg_size == 0 || args->local_nentries == 0) {
950 /* assume OPENREQUEST was delayed by mistake */
951 spin_unlock_irqrestore(&ch->lock, irq_flags);
952 return;
953 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700954
955 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
956 ch->remote_nentries = args->local_nentries;
957
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700958 if (ch->flags & XPC_C_OPENREQUEST) {
959 if (args->msg_size != ch->msg_size) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700960 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500961 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700962 spin_unlock_irqrestore(&ch->lock, irq_flags);
963 return;
964 }
965 } else {
966 ch->msg_size = args->msg_size;
967
968 XPC_SET_REASON(ch, 0, 0);
969 ch->flags &= ~XPC_C_DISCONNECTED;
970
971 atomic_inc(&part->nchannels_active);
972 }
973
974 xpc_process_connect(ch, &irq_flags);
975 }
976
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700977 if (IPI_flags & XPC_IPI_OPENREPLY) {
978
979 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
980 "local_nentries=%d, remote_nentries=%d) received from "
981 "partid=%d, channel=%d\n", args->local_msgqueue_pa,
982 args->local_nentries, args->remote_nentries,
983 ch->partid, ch->number);
984
985 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
986 spin_unlock_irqrestore(&ch->lock, irq_flags);
987 return;
988 }
Dean Nelsone54af722005-10-25 14:07:43 -0500989 if (!(ch->flags & XPC_C_OPENREQUEST)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700990 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -0500991 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500992 spin_unlock_irqrestore(&ch->lock, irq_flags);
993 return;
994 }
995
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700996 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
997 DBUG_ON(ch->flags & XPC_C_CONNECTED);
998
999 /*
1000 * The meaningful OPENREPLY connection state fields are:
1001 * local_msgqueue_pa = physical address of remote
Dean Nelson35190502008-04-22 14:48:55 -05001002 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001003 * local_nentries = remote partition's local_nentries
1004 * remote_nentries = remote partition's remote_nentries
1005 */
1006 DBUG_ON(args->local_msgqueue_pa == 0);
1007 DBUG_ON(args->local_nentries == 0);
1008 DBUG_ON(args->remote_nentries == 0);
1009
1010 ch->flags |= XPC_C_ROPENREPLY;
1011 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
1012
1013 if (args->local_nentries < ch->remote_nentries) {
1014 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1015 "remote_nentries=%d, old remote_nentries=%d, "
1016 "partid=%d, channel=%d\n",
1017 args->local_nentries, ch->remote_nentries,
1018 ch->partid, ch->number);
1019
1020 ch->remote_nentries = args->local_nentries;
1021 }
1022 if (args->remote_nentries < ch->local_nentries) {
1023 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1024 "local_nentries=%d, old local_nentries=%d, "
1025 "partid=%d, channel=%d\n",
1026 args->remote_nentries, ch->local_nentries,
1027 ch->partid, ch->number);
1028
1029 ch->local_nentries = args->remote_nentries;
1030 }
1031
1032 xpc_process_connect(ch, &irq_flags);
1033 }
1034
1035 spin_unlock_irqrestore(&ch->lock, irq_flags);
1036}
1037
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001038/*
1039 * Attempt to establish a channel connection to a remote partition.
1040 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001041static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001042xpc_connect_channel(struct xpc_channel *ch)
1043{
1044 unsigned long irq_flags;
1045 struct xpc_registration *registration = &xpc_registrations[ch->number];
1046
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001047 if (mutex_trylock(&registration->mutex) == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -07001048 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001049
1050 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001051 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -07001052 return xpUnregistered;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001053 }
1054
1055 spin_lock_irqsave(&ch->lock, irq_flags);
1056
1057 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1058 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
1059
1060 if (ch->flags & XPC_C_DISCONNECTING) {
1061 spin_unlock_irqrestore(&ch->lock, irq_flags);
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001062 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001063 return ch->reason;
1064 }
1065
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001066 /* add info from the channel connect registration to the channel */
1067
1068 ch->kthreads_assigned_limit = registration->assigned_limit;
1069 ch->kthreads_idle_limit = registration->idle_limit;
1070 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
1071 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
1072 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
1073
1074 ch->func = registration->func;
1075 DBUG_ON(registration->func == NULL);
1076 ch->key = registration->key;
1077
1078 ch->local_nentries = registration->nentries;
1079
1080 if (ch->flags & XPC_C_ROPENREQUEST) {
1081 if (registration->msg_size != ch->msg_size) {
1082 /* the local and remote sides aren't the same */
1083
1084 /*
1085 * Because XPC_DISCONNECT_CHANNEL() can block we're
1086 * forced to up the registration sema before we unlock
1087 * the channel lock. But that's okay here because we're
1088 * done with the part that required the registration
1089 * sema. XPC_DISCONNECT_CHANNEL() requires that the
1090 * channel lock be locked and will unlock and relock
1091 * the channel lock as needed.
1092 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001093 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -07001094 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -05001095 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001096 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -07001097 return xpUnequalMsgSizes;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001098 }
1099 } else {
1100 ch->msg_size = registration->msg_size;
1101
1102 XPC_SET_REASON(ch, 0, 0);
1103 ch->flags &= ~XPC_C_DISCONNECTED;
1104
1105 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
1106 }
1107
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001108 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001109
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001110 /* initiate the connection */
1111
1112 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
1113 xpc_IPI_send_openrequest(ch, &irq_flags);
1114
1115 xpc_process_connect(ch, &irq_flags);
1116
1117 spin_unlock_irqrestore(&ch->lock, irq_flags);
1118
Dean Nelson65c17b82008-05-12 14:02:02 -07001119 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001120}
1121
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001122/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001123 * Clear some of the msg flags in the local message queue.
1124 */
1125static inline void
1126xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
1127{
1128 struct xpc_msg *msg;
1129 s64 get;
1130
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001131 get = ch->w_remote_GP.get;
1132 do {
Dean Nelson35190502008-04-22 14:48:55 -05001133 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1134 (get % ch->local_nentries) *
1135 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001136 msg->flags = 0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001137 } while (++get < ch->remote_GP.get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001138}
1139
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001140/*
1141 * Clear some of the msg flags in the remote message queue.
1142 */
1143static inline void
1144xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
1145{
1146 struct xpc_msg *msg;
1147 s64 put;
1148
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001149 put = ch->w_remote_GP.put;
1150 do {
Dean Nelson35190502008-04-22 14:48:55 -05001151 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
1152 (put % ch->remote_nentries) *
1153 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001154 msg->flags = 0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001155 } while (++put < ch->remote_GP.put);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001156}
1157
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001158static void
1159xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
1160{
1161 struct xpc_channel *ch = &part->channels[ch_number];
1162 int nmsgs_sent;
1163
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001164 ch->remote_GP = part->remote_GPs[ch_number];
1165
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001166 /* See what, if anything, has changed for each connected channel */
1167
1168 xpc_msgqueue_ref(ch);
1169
1170 if (ch->w_remote_GP.get == ch->remote_GP.get &&
Dean Nelson35190502008-04-22 14:48:55 -05001171 ch->w_remote_GP.put == ch->remote_GP.put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001172 /* nothing changed since GPs were last pulled */
1173 xpc_msgqueue_deref(ch);
1174 return;
1175 }
1176
Dean Nelson35190502008-04-22 14:48:55 -05001177 if (!(ch->flags & XPC_C_CONNECTED)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001178 xpc_msgqueue_deref(ch);
1179 return;
1180 }
1181
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001182 /*
1183 * First check to see if messages recently sent by us have been
1184 * received by the other side. (The remote GET value will have
1185 * changed since we last looked at it.)
1186 */
1187
1188 if (ch->w_remote_GP.get != ch->remote_GP.get) {
1189
1190 /*
1191 * We need to notify any senders that want to be notified
1192 * that their sent messages have been received by their
1193 * intended recipients. We need to do this before updating
1194 * w_remote_GP.get so that we don't allocate the same message
1195 * queue entries prematurely (see xpc_allocate_msg()).
1196 */
1197 if (atomic_read(&ch->n_to_notify) > 0) {
1198 /*
1199 * Notify senders that messages sent have been
1200 * received and delivered by the other side.
1201 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001202 xpc_notify_senders(ch, xpMsgDelivered,
Dean Nelson35190502008-04-22 14:48:55 -05001203 ch->remote_GP.get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001204 }
1205
1206 /*
1207 * Clear msg->flags in previously sent messages, so that
1208 * they're ready for xpc_allocate_msg().
1209 */
1210 xpc_clear_local_msgqueue_flags(ch);
1211
Dave Jones821fe942005-06-25 14:54:29 -07001212 ch->w_remote_GP.get = ch->remote_GP.get;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001213
1214 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1215 "channel=%d\n", ch->w_remote_GP.get, ch->partid,
1216 ch->number);
1217
1218 /*
1219 * If anyone was waiting for message queue entries to become
1220 * available, wake them up.
1221 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001222 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001223 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001224 }
1225
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001226 /*
1227 * Now check for newly sent messages by the other side. (The remote
1228 * PUT value will have changed since we last looked at it.)
1229 */
1230
1231 if (ch->w_remote_GP.put != ch->remote_GP.put) {
1232 /*
1233 * Clear msg->flags in previously received messages, so that
1234 * they're ready for xpc_get_deliverable_msg().
1235 */
1236 xpc_clear_remote_msgqueue_flags(ch);
1237
Dave Jones821fe942005-06-25 14:54:29 -07001238 ch->w_remote_GP.put = ch->remote_GP.put;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001239
1240 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1241 "channel=%d\n", ch->w_remote_GP.put, ch->partid,
1242 ch->number);
1243
1244 nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
1245 if (nmsgs_sent > 0) {
1246 dev_dbg(xpc_chan, "msgs waiting to be copied and "
1247 "delivered=%d, partid=%d, channel=%d\n",
1248 nmsgs_sent, ch->partid, ch->number);
1249
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001250 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001251 xpc_activate_kthreads(ch, nmsgs_sent);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001252 }
1253 }
1254
1255 xpc_msgqueue_deref(ch);
1256}
1257
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001258void
1259xpc_process_channel_activity(struct xpc_partition *part)
1260{
1261 unsigned long irq_flags;
1262 u64 IPI_amo, IPI_flags;
1263 struct xpc_channel *ch;
1264 int ch_number;
Dean Nelsona607c382005-09-01 14:01:37 -05001265 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001266
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001267 IPI_amo = xpc_get_IPI_flags(part);
1268
1269 /*
1270 * Initiate channel connections for registered channels.
1271 *
1272 * For each connected channel that has pending messages activate idle
1273 * kthreads and/or create new kthreads as needed.
1274 */
1275
1276 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1277 ch = &part->channels[ch_number];
1278
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001279 /*
1280 * Process any open or close related IPI flags, and then deal
1281 * with connecting or disconnecting the channel as required.
1282 */
1283
1284 IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
1285
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001286 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001287 xpc_process_openclose_IPI(part, ch_number, IPI_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001288
Dean Nelsona607c382005-09-01 14:01:37 -05001289 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001290
Dean Nelsona607c382005-09-01 14:01:37 -05001291 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001292 spin_lock_irqsave(&ch->lock, irq_flags);
1293 xpc_process_disconnect(ch, &irq_flags);
1294 spin_unlock_irqrestore(&ch->lock, irq_flags);
1295 continue;
1296 }
1297
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001298 if (part->act_state == XPC_P_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001299 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001300
Dean Nelsona607c382005-09-01 14:01:37 -05001301 if (!(ch_flags & XPC_C_CONNECTED)) {
1302 if (!(ch_flags & XPC_C_OPENREQUEST)) {
1303 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -05001304 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001305 } else {
1306 spin_lock_irqsave(&ch->lock, irq_flags);
1307 xpc_process_connect(ch, &irq_flags);
1308 spin_unlock_irqrestore(&ch->lock, irq_flags);
1309 }
1310 continue;
1311 }
1312
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001313 /*
1314 * Process any message related IPI flags, this may involve the
1315 * activation of kthreads to deliver any pending messages sent
1316 * from the other partition.
1317 */
1318
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001319 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001320 xpc_process_msg_IPI(part, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001321 }
1322}
1323
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001324/*
Dean Nelsona607c382005-09-01 14:01:37 -05001325 * XPC's heartbeat code calls this function to inform XPC that a partition is
1326 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001327 * infrastructure used for the just downed partition.
1328 *
1329 * XPC's heartbeat code will never call this function and xpc_partition_up()
1330 * at the same time. Nor will it ever make multiple calls to either function
1331 * at the same time.
1332 */
1333void
Dean Nelson65c17b82008-05-12 14:02:02 -07001334xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001335{
1336 unsigned long irq_flags;
1337 int ch_number;
1338 struct xpc_channel *ch;
1339
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001340 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
1341 XPC_PARTID(part), reason);
1342
1343 if (!xpc_part_ref(part)) {
1344 /* infrastructure for this partition isn't currently set up */
1345 return;
1346 }
1347
Dean Nelsona607c382005-09-01 14:01:37 -05001348 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001349
1350 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1351 ch = &part->channels[ch_number];
1352
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001353 xpc_msgqueue_ref(ch);
1354 spin_lock_irqsave(&ch->lock, irq_flags);
1355
1356 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
1357
1358 spin_unlock_irqrestore(&ch->lock, irq_flags);
1359 xpc_msgqueue_deref(ch);
1360 }
1361
1362 xpc_wakeup_channel_mgr(part);
1363
1364 xpc_part_deref(part);
1365}
1366
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001367/*
1368 * Teardown the infrastructure necessary to support XPartition Communication
1369 * between the specified remote partition and the local one.
1370 */
1371void
1372xpc_teardown_infrastructure(struct xpc_partition *part)
1373{
Dean Nelson64d032b2008-05-12 14:02:03 -07001374 short partid = XPC_PARTID(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001375
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001376 /*
1377 * We start off by making this partition inaccessible to local
1378 * processes by marking it as no longer setup. Then we make it
1379 * inaccessible to remote processes by clearing the XPC per partition
1380 * specific variable's magic # (which indicates that these variables
1381 * are no longer valid) and by ignoring all XPC notify IPIs sent to
1382 * this partition.
1383 */
1384
Dean Nelsona607c382005-09-01 14:01:37 -05001385 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001386 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1387 DBUG_ON(part->setup_state != XPC_P_SETUP);
1388 part->setup_state = XPC_P_WTEARDOWN;
1389
1390 xpc_vars_part[partid].magic = 0;
1391
Dean Nelson35190502008-04-22 14:48:55 -05001392 free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001393
1394 /*
Simon Arlott72fdbdc2007-05-11 14:55:43 -07001395 * Before proceeding with the teardown we have to wait until all
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001396 * existing references cease.
1397 */
1398 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1399
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001400 /* now we can begin tearing down the infrastructure */
1401
1402 part->setup_state = XPC_P_TORNDOWN;
1403
1404 /* in case we've still got outstanding timers registered... */
1405 del_timer_sync(&part->dropped_IPI_timer);
1406
1407 kfree(part->remote_openclose_args_base);
1408 part->remote_openclose_args = NULL;
1409 kfree(part->local_openclose_args_base);
1410 part->local_openclose_args = NULL;
1411 kfree(part->remote_GPs_base);
1412 part->remote_GPs = NULL;
1413 kfree(part->local_GPs_base);
1414 part->local_GPs = NULL;
1415 kfree(part->channels);
1416 part->channels = NULL;
1417 part->local_IPI_amo_va = NULL;
1418}
1419
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001420/*
1421 * Called by XP at the time of channel connection registration to cause
1422 * XPC to establish connections to all currently active partitions.
1423 */
1424void
1425xpc_initiate_connect(int ch_number)
1426{
Dean Nelson64d032b2008-05-12 14:02:03 -07001427 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001428 struct xpc_partition *part;
1429 struct xpc_channel *ch;
1430
Dean Nelsonbc63d382008-07-29 22:34:04 -07001431 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001432
Dean Nelsonbc63d382008-07-29 22:34:04 -07001433 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001434 part = &xpc_partitions[partid];
1435
1436 if (xpc_part_ref(part)) {
1437 ch = &part->channels[ch_number];
1438
Dean Nelsone54af722005-10-25 14:07:43 -05001439 /*
1440 * Initiate the establishment of a connection on the
1441 * newly registered channel to the remote partition.
1442 */
1443 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001444 xpc_part_deref(part);
1445 }
1446 }
1447}
1448
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001449void
1450xpc_connected_callout(struct xpc_channel *ch)
1451{
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001452 /* let the registerer know that a connection has been established */
1453
1454 if (ch->func != NULL) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001455 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001456 "partid=%d, channel=%d\n", ch->partid, ch->number);
1457
Dean Nelson65c17b82008-05-12 14:02:02 -07001458 ch->func(xpConnected, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -05001459 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001460
Dean Nelson65c17b82008-05-12 14:02:02 -07001461 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001462 "partid=%d, channel=%d\n", ch->partid, ch->number);
1463 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001464}
1465
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001466/*
1467 * Called by XP at the time of channel connection unregistration to cause
1468 * XPC to teardown all current connections for the specified channel.
1469 *
1470 * Before returning xpc_initiate_disconnect() will wait until all connections
1471 * on the specified channel have been closed/torndown. So the caller can be
1472 * assured that they will not be receiving any more callouts from XPC to the
1473 * function they registered via xpc_connect().
1474 *
1475 * Arguments:
1476 *
1477 * ch_number - channel # to unregister.
1478 */
1479void
1480xpc_initiate_disconnect(int ch_number)
1481{
1482 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -07001483 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001484 struct xpc_partition *part;
1485 struct xpc_channel *ch;
1486
Dean Nelsonbc63d382008-07-29 22:34:04 -07001487 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001488
1489 /* initiate the channel disconnect for every active partition */
Dean Nelsonbc63d382008-07-29 22:34:04 -07001490 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001491 part = &xpc_partitions[partid];
1492
1493 if (xpc_part_ref(part)) {
1494 ch = &part->channels[ch_number];
1495 xpc_msgqueue_ref(ch);
1496
1497 spin_lock_irqsave(&ch->lock, irq_flags);
1498
Dean Nelsona607c382005-09-01 14:01:37 -05001499 if (!(ch->flags & XPC_C_DISCONNECTED)) {
1500 ch->flags |= XPC_C_WDISCONNECT;
1501
Dean Nelson65c17b82008-05-12 14:02:02 -07001502 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -05001503 &irq_flags);
Dean Nelsona607c382005-09-01 14:01:37 -05001504 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001505
1506 spin_unlock_irqrestore(&ch->lock, irq_flags);
1507
1508 xpc_msgqueue_deref(ch);
1509 xpc_part_deref(part);
1510 }
1511 }
1512
1513 xpc_disconnect_wait(ch_number);
1514}
1515
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001516/*
1517 * To disconnect a channel, and reflect it back to all who may be waiting.
1518 *
Dean Nelsona607c382005-09-01 14:01:37 -05001519 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
1520 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
1521 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001522 *
1523 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
1524 */
1525void
1526xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson65c17b82008-05-12 14:02:02 -07001527 enum xp_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001528{
Dean Nelsona607c382005-09-01 14:01:37 -05001529 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001530
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001531 DBUG_ON(!spin_is_locked(&ch->lock));
1532
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001533 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001534 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001535
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001536 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
1537
1538 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
1539 reason, line, ch->partid, ch->number);
1540
1541 XPC_SET_REASON(ch, reason, line);
1542
Dean Nelsona607c382005-09-01 14:01:37 -05001543 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001544 /* some of these may not have been set */
1545 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -05001546 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
1547 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001548
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001549 xpc_IPI_send_closerequest(ch, irq_flags);
1550
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001551 if (channel_was_connected)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001552 ch->flags |= XPC_C_WASCONNECTED;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001553
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001554 spin_unlock_irqrestore(&ch->lock, *irq_flags);
1555
Dean Nelsona607c382005-09-01 14:01:37 -05001556 /* wake all idle kthreads so they can exit */
1557 if (atomic_read(&ch->kthreads_idle) > 0) {
1558 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -06001559
1560 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -05001561 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001562 /* start a kthread that will do the xpDisconnecting callout */
Dean Nelsona460ef82006-11-22 08:25:00 -06001563 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001564 }
1565
Dean Nelsona607c382005-09-01 14:01:37 -05001566 /* wake those waiting to allocate an entry from the local msg queue */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001567 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelsona607c382005-09-01 14:01:37 -05001568 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001569
1570 spin_lock_irqsave(&ch->lock, *irq_flags);
1571}
1572
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001573void
Dean Nelson65c17b82008-05-12 14:02:02 -07001574xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001575{
1576 /*
Dean Nelsona607c382005-09-01 14:01:37 -05001577 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001578 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c382005-09-01 14:01:37 -05001579 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001580 */
1581
1582 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -06001583 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
1584 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001585
Dean Nelson246c7e32005-12-22 14:32:56 -06001586 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001587
Dean Nelson246c7e32005-12-22 14:32:56 -06001588 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
1589 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001590 }
1591}
1592
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001593/*
1594 * Wait for a message entry to become available for the specified channel,
1595 * but don't wait any longer than 1 jiffy.
1596 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001597static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001598xpc_allocate_msg_wait(struct xpc_channel *ch)
1599{
Dean Nelson65c17b82008-05-12 14:02:02 -07001600 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001601
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001602 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001603 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001604 return ch->reason;
1605 }
1606
1607 atomic_inc(&ch->n_on_msg_allocate_wq);
1608 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
1609 atomic_dec(&ch->n_on_msg_allocate_wq);
1610
1611 if (ch->flags & XPC_C_DISCONNECTING) {
1612 ret = ch->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -07001613 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001614 } else if (ret == 0) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001615 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001616 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -07001617 ret = xpInterrupted;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001618 }
1619
1620 return ret;
1621}
1622
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001623/*
1624 * Allocate an entry for a message from the message queue associated with the
1625 * specified channel.
1626 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001627static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001628xpc_allocate_msg(struct xpc_channel *ch, u32 flags,
Dean Nelson35190502008-04-22 14:48:55 -05001629 struct xpc_msg **address_of_msg)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001630{
1631 struct xpc_msg *msg;
Dean Nelson65c17b82008-05-12 14:02:02 -07001632 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001633 s64 put;
1634
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001635 /* this reference will be dropped in xpc_send_msg() */
1636 xpc_msgqueue_ref(ch);
1637
1638 if (ch->flags & XPC_C_DISCONNECTING) {
1639 xpc_msgqueue_deref(ch);
1640 return ch->reason;
1641 }
1642 if (!(ch->flags & XPC_C_CONNECTED)) {
1643 xpc_msgqueue_deref(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -07001644 return xpNotConnected;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001645 }
1646
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001647 /*
1648 * Get the next available message entry from the local message queue.
1649 * If none are available, we'll make sure that we grab the latest
1650 * GP values.
1651 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001652 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001653
1654 while (1) {
1655
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001656 put = ch->w_local_GP.put;
1657 rmb(); /* guarantee that .put loads before .get */
1658 if (put - ch->w_remote_GP.get < ch->local_nentries) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001659
1660 /* There are available message entries. We need to try
1661 * to secure one for ourselves. We'll do this by trying
1662 * to increment w_local_GP.put as long as someone else
1663 * doesn't beat us to it. If they do, we'll have to
1664 * try again.
Dean Nelson35190502008-04-22 14:48:55 -05001665 */
1666 if (cmpxchg(&ch->w_local_GP.put, put, put + 1) == put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001667 /* we got the entry referenced by put */
1668 break;
1669 }
1670 continue; /* try again */
1671 }
1672
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001673 /*
1674 * There aren't any available msg entries at this time.
1675 *
1676 * In waiting for a message entry to become available,
1677 * we set a timeout in case the other side is not
1678 * sending completion IPIs. This lets us fake an IPI
1679 * that will cause the IPI handler to fetch the latest
1680 * GP values as if an IPI was sent by the other side.
1681 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001682 if (ret == xpTimeout)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001683 xpc_IPI_send_local_msgrequest(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001684
1685 if (flags & XPC_NOWAIT) {
1686 xpc_msgqueue_deref(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -07001687 return xpNoWait;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001688 }
1689
1690 ret = xpc_allocate_msg_wait(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -07001691 if (ret != xpInterrupted && ret != xpTimeout) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001692 xpc_msgqueue_deref(ch);
1693 return ret;
1694 }
1695 }
1696
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001697 /* get the message's address and initialize it */
Dean Nelson35190502008-04-22 14:48:55 -05001698 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1699 (put % ch->local_nentries) * ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001700
1701 DBUG_ON(msg->flags != 0);
1702 msg->number = put;
1703
1704 dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
1705 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
Dean Nelson35190502008-04-22 14:48:55 -05001706 (void *)msg, msg->number, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001707
1708 *address_of_msg = msg;
1709
Dean Nelson65c17b82008-05-12 14:02:02 -07001710 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001711}
1712
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001713/*
1714 * Allocate an entry for a message from the message queue associated with the
1715 * specified channel. NOTE that this routine can sleep waiting for a message
1716 * entry to become available. To not sleep, pass in the XPC_NOWAIT flag.
1717 *
1718 * Arguments:
1719 *
1720 * partid - ID of partition to which the channel is connected.
1721 * ch_number - channel #.
1722 * flags - see xpc.h for valid flags.
1723 * payload - address of the allocated payload area pointer (filled in on
1724 * return) in which the user-defined message is constructed.
1725 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001726enum xp_retval
Dean Nelson64d032b2008-05-12 14:02:03 -07001727xpc_initiate_allocate(short partid, int ch_number, u32 flags, void **payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001728{
1729 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson65c17b82008-05-12 14:02:02 -07001730 enum xp_retval ret = xpUnknownReason;
Tony Luck27f4aa32006-04-04 14:11:49 -07001731 struct xpc_msg *msg = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001732
Dean Nelsonbc63d382008-07-29 22:34:04 -07001733 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001734 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1735
1736 *payload = NULL;
1737
1738 if (xpc_part_ref(part)) {
1739 ret = xpc_allocate_msg(&part->channels[ch_number], flags, &msg);
1740 xpc_part_deref(part);
1741
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001742 if (msg != NULL)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001743 *payload = &msg->payload;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001744 }
1745
1746 return ret;
1747}
1748
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001749/*
1750 * Now we actually send the messages that are ready to be sent by advancing
1751 * the local message queue's Put value and then send an IPI to the recipient
1752 * partition.
1753 */
1754static void
1755xpc_send_msgs(struct xpc_channel *ch, s64 initial_put)
1756{
1757 struct xpc_msg *msg;
1758 s64 put = initial_put + 1;
1759 int send_IPI = 0;
1760
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001761 while (1) {
1762
1763 while (1) {
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001764 if (put == ch->w_local_GP.put)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001765 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001766
Dean Nelson35190502008-04-22 14:48:55 -05001767 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1768 (put % ch->local_nentries) *
1769 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001770
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001771 if (!(msg->flags & XPC_M_READY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001772 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001773
1774 put++;
1775 }
1776
1777 if (put == initial_put) {
1778 /* nothing's changed */
1779 break;
1780 }
1781
1782 if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) !=
Dean Nelson35190502008-04-22 14:48:55 -05001783 initial_put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001784 /* someone else beat us to it */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001785 DBUG_ON(ch->local_GP->put < initial_put);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001786 break;
1787 }
1788
1789 /* we just set the new value of local_GP->put */
1790
1791 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
1792 "channel=%d\n", put, ch->partid, ch->number);
1793
1794 send_IPI = 1;
1795
1796 /*
1797 * We need to ensure that the message referenced by
1798 * local_GP->put is not XPC_M_READY or that local_GP->put
1799 * equals w_local_GP.put, so we'll go have a look.
1800 */
1801 initial_put = put;
1802 }
1803
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001804 if (send_IPI)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001805 xpc_IPI_send_msgrequest(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001806}
1807
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001808/*
1809 * Common code that does the actual sending of the message by advancing the
1810 * local message queue's Put value and sends an IPI to the partition the
1811 * message is being sent to.
1812 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001813static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001814xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type,
Dean Nelson35190502008-04-22 14:48:55 -05001815 xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001816{
Dean Nelson65c17b82008-05-12 14:02:02 -07001817 enum xp_retval ret = xpSuccess;
Dean Nelsona607c382005-09-01 14:01:37 -05001818 struct xpc_notify *notify = notify;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001819 s64 put, msg_number = msg->number;
1820
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001821 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
Dean Nelson35190502008-04-22 14:48:55 -05001822 DBUG_ON((((u64)msg - (u64)ch->local_msgqueue) / ch->msg_size) !=
1823 msg_number % ch->local_nentries);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001824 DBUG_ON(msg->flags & XPC_M_READY);
1825
1826 if (ch->flags & XPC_C_DISCONNECTING) {
1827 /* drop the reference grabbed in xpc_allocate_msg() */
1828 xpc_msgqueue_deref(ch);
1829 return ch->reason;
1830 }
1831
1832 if (notify_type != 0) {
1833 /*
1834 * Tell the remote side to send an ACK interrupt when the
1835 * message has been delivered.
1836 */
1837 msg->flags |= XPC_M_INTERRUPT;
1838
1839 atomic_inc(&ch->n_to_notify);
1840
1841 notify = &ch->notify_queue[msg_number % ch->local_nentries];
1842 notify->func = func;
1843 notify->key = key;
Dave Jones821fe942005-06-25 14:54:29 -07001844 notify->type = notify_type;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001845
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001846 /* >>> is a mb() needed here? */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001847
1848 if (ch->flags & XPC_C_DISCONNECTING) {
1849 /*
1850 * An error occurred between our last error check and
1851 * this one. We will try to clear the type field from
1852 * the notify entry. If we succeed then
1853 * xpc_disconnect_channel() didn't already process
1854 * the notify entry.
1855 */
1856 if (cmpxchg(&notify->type, notify_type, 0) ==
Dean Nelson35190502008-04-22 14:48:55 -05001857 notify_type) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001858 atomic_dec(&ch->n_to_notify);
1859 ret = ch->reason;
1860 }
1861
1862 /* drop the reference grabbed in xpc_allocate_msg() */
1863 xpc_msgqueue_deref(ch);
1864 return ret;
1865 }
1866 }
1867
1868 msg->flags |= XPC_M_READY;
1869
1870 /*
1871 * The preceding store of msg->flags must occur before the following
1872 * load of ch->local_GP->put.
1873 */
1874 mb();
1875
1876 /* see if the message is next in line to be sent, if so send it */
1877
1878 put = ch->local_GP->put;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001879 if (put == msg_number)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001880 xpc_send_msgs(ch, put);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001881
1882 /* drop the reference grabbed in xpc_allocate_msg() */
1883 xpc_msgqueue_deref(ch);
1884 return ret;
1885}
1886
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001887/*
1888 * Send a message previously allocated using xpc_initiate_allocate() on the
1889 * specified channel connected to the specified partition.
1890 *
1891 * This routine will not wait for the message to be received, nor will
1892 * notification be given when it does happen. Once this routine has returned
1893 * the message entry allocated via xpc_initiate_allocate() is no longer
1894 * accessable to the caller.
1895 *
1896 * This routine, although called by users, does not call xpc_part_ref() to
1897 * ensure that the partition infrastructure is in place. It relies on the
1898 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
1899 *
1900 * Arguments:
1901 *
1902 * partid - ID of partition to which the channel is connected.
1903 * ch_number - channel # to send message on.
1904 * payload - pointer to the payload area allocated via
1905 * xpc_initiate_allocate().
1906 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001907enum xp_retval
Dean Nelson64d032b2008-05-12 14:02:03 -07001908xpc_initiate_send(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001909{
1910 struct xpc_partition *part = &xpc_partitions[partid];
1911 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
Dean Nelson65c17b82008-05-12 14:02:02 -07001912 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001913
Dean Nelson35190502008-04-22 14:48:55 -05001914 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001915 partid, ch_number);
1916
Dean Nelsonbc63d382008-07-29 22:34:04 -07001917 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001918 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1919 DBUG_ON(msg == NULL);
1920
1921 ret = xpc_send_msg(&part->channels[ch_number], msg, 0, NULL, NULL);
1922
1923 return ret;
1924}
1925
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001926/*
1927 * Send a message previously allocated using xpc_initiate_allocate on the
1928 * specified channel connected to the specified partition.
1929 *
1930 * This routine will not wait for the message to be sent. Once this routine
1931 * has returned the message entry allocated via xpc_initiate_allocate() is no
1932 * longer accessable to the caller.
1933 *
1934 * Once the remote end of the channel has received the message, the function
1935 * passed as an argument to xpc_initiate_send_notify() will be called. This
1936 * allows the sender to free up or re-use any buffers referenced by the
1937 * message, but does NOT mean the message has been processed at the remote
1938 * end by a receiver.
1939 *
1940 * If this routine returns an error, the caller's function will NOT be called.
1941 *
1942 * This routine, although called by users, does not call xpc_part_ref() to
1943 * ensure that the partition infrastructure is in place. It relies on the
1944 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
1945 *
1946 * Arguments:
1947 *
1948 * partid - ID of partition to which the channel is connected.
1949 * ch_number - channel # to send message on.
1950 * payload - pointer to the payload area allocated via
1951 * xpc_initiate_allocate().
1952 * func - function to call with asynchronous notification of message
1953 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
1954 * key - user-defined key to be passed to the function when it's called.
1955 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001956enum xp_retval
Dean Nelson64d032b2008-05-12 14:02:03 -07001957xpc_initiate_send_notify(short partid, int ch_number, void *payload,
Dean Nelson35190502008-04-22 14:48:55 -05001958 xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001959{
1960 struct xpc_partition *part = &xpc_partitions[partid];
1961 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
Dean Nelson65c17b82008-05-12 14:02:02 -07001962 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001963
Dean Nelson35190502008-04-22 14:48:55 -05001964 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001965 partid, ch_number);
1966
Dean Nelsonbc63d382008-07-29 22:34:04 -07001967 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001968 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1969 DBUG_ON(msg == NULL);
1970 DBUG_ON(func == NULL);
1971
1972 ret = xpc_send_msg(&part->channels[ch_number], msg, XPC_N_CALL,
Dean Nelson35190502008-04-22 14:48:55 -05001973 func, key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001974 return ret;
1975}
1976
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001977static struct xpc_msg *
1978xpc_pull_remote_msg(struct xpc_channel *ch, s64 get)
1979{
1980 struct xpc_partition *part = &xpc_partitions[ch->partid];
1981 struct xpc_msg *remote_msg, *msg;
1982 u32 msg_index, nmsgs;
1983 u64 msg_offset;
Dean Nelson65c17b82008-05-12 14:02:02 -07001984 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001985
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001986 if (mutex_lock_interruptible(&ch->msg_to_pull_mutex) != 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001987 /* we were interrupted by a signal */
1988 return NULL;
1989 }
1990
1991 while (get >= ch->next_msg_to_pull) {
1992
1993 /* pull as many messages as are ready and able to be pulled */
1994
1995 msg_index = ch->next_msg_to_pull % ch->remote_nentries;
1996
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001997 DBUG_ON(ch->next_msg_to_pull >= ch->w_remote_GP.put);
1998 nmsgs = ch->w_remote_GP.put - ch->next_msg_to_pull;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001999 if (msg_index + nmsgs > ch->remote_nentries) {
2000 /* ignore the ones that wrap the msg queue for now */
2001 nmsgs = ch->remote_nentries - msg_index;
2002 }
2003
2004 msg_offset = msg_index * ch->msg_size;
Dean Nelson35190502008-04-22 14:48:55 -05002005 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
2006 remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
2007 msg_offset);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002008
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002009 ret = xpc_pull_remote_cachelines(part, msg, remote_msg,
2010 nmsgs * ch->msg_size);
Dean Nelson65c17b82008-05-12 14:02:02 -07002011 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002012
2013 dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
2014 " msg %ld from partition %d, channel=%d, "
2015 "ret=%d\n", nmsgs, ch->next_msg_to_pull,
2016 ch->partid, ch->number, ret);
2017
2018 XPC_DEACTIVATE_PARTITION(part, ret);
2019
Jes Sorensenf9e505a2006-01-17 12:52:21 -05002020 mutex_unlock(&ch->msg_to_pull_mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002021 return NULL;
2022 }
2023
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002024 ch->next_msg_to_pull += nmsgs;
2025 }
2026
Jes Sorensenf9e505a2006-01-17 12:52:21 -05002027 mutex_unlock(&ch->msg_to_pull_mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002028
2029 /* return the message we were looking for */
2030 msg_offset = (get % ch->remote_nentries) * ch->msg_size;
Dean Nelson35190502008-04-22 14:48:55 -05002031 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002032
2033 return msg;
2034}
2035
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002036/*
2037 * Get a message to be delivered.
2038 */
2039static struct xpc_msg *
2040xpc_get_deliverable_msg(struct xpc_channel *ch)
2041{
2042 struct xpc_msg *msg = NULL;
2043 s64 get;
2044
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002045 do {
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002046 if (ch->flags & XPC_C_DISCONNECTING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002047 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002048
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002049 get = ch->w_local_GP.get;
2050 rmb(); /* guarantee that .get loads before .put */
2051 if (get == ch->w_remote_GP.put)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002052 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002053
2054 /* There are messages waiting to be pulled and delivered.
2055 * We need to try to secure one for ourselves. We'll do this
2056 * by trying to increment w_local_GP.get and hope that no one
2057 * else beats us to it. If they do, we'll we'll simply have
2058 * to try again for the next one.
Dean Nelson35190502008-04-22 14:48:55 -05002059 */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002060
2061 if (cmpxchg(&ch->w_local_GP.get, get, get + 1) == get) {
2062 /* we got the entry referenced by get */
2063
2064 dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
2065 "partid=%d, channel=%d\n", get + 1,
2066 ch->partid, ch->number);
2067
2068 /* pull the message from the remote partition */
2069
2070 msg = xpc_pull_remote_msg(ch, get);
2071
2072 DBUG_ON(msg != NULL && msg->number != get);
2073 DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
2074 DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
2075
2076 break;
2077 }
2078
2079 } while (1);
2080
2081 return msg;
2082}
2083
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002084/*
2085 * Deliver a message to its intended recipient.
2086 */
2087void
2088xpc_deliver_msg(struct xpc_channel *ch)
2089{
2090 struct xpc_msg *msg;
2091
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002092 msg = xpc_get_deliverable_msg(ch);
2093 if (msg != NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002094
2095 /*
2096 * This ref is taken to protect the payload itself from being
2097 * freed before the user is finished with it, which the user
2098 * indicates by calling xpc_initiate_received().
2099 */
2100 xpc_msgqueue_ref(ch);
2101
2102 atomic_inc(&ch->kthreads_active);
2103
2104 if (ch->func != NULL) {
2105 dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
2106 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05002107 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002108 ch->number);
2109
2110 /* deliver the message to its intended recipient */
Dean Nelson65c17b82008-05-12 14:02:02 -07002111 ch->func(xpMsgReceived, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -05002112 &msg->payload, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002113
2114 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
2115 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05002116 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002117 ch->number);
2118 }
2119
2120 atomic_dec(&ch->kthreads_active);
2121 }
2122}
2123
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002124/*
2125 * Now we actually acknowledge the messages that have been delivered and ack'd
2126 * by advancing the cached remote message queue's Get value and if requested
2127 * send an IPI to the message sender's partition.
2128 */
2129static void
2130xpc_acknowledge_msgs(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2131{
2132 struct xpc_msg *msg;
2133 s64 get = initial_get + 1;
2134 int send_IPI = 0;
2135
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002136 while (1) {
2137
2138 while (1) {
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002139 if (get == ch->w_local_GP.get)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002140 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002141
Dean Nelson35190502008-04-22 14:48:55 -05002142 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
2143 (get % ch->remote_nentries) *
2144 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002145
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002146 if (!(msg->flags & XPC_M_DONE))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002147 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002148
2149 msg_flags |= msg->flags;
2150 get++;
2151 }
2152
2153 if (get == initial_get) {
2154 /* nothing's changed */
2155 break;
2156 }
2157
2158 if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) !=
Dean Nelson35190502008-04-22 14:48:55 -05002159 initial_get) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002160 /* someone else beat us to it */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002161 DBUG_ON(ch->local_GP->get <= initial_get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002162 break;
2163 }
2164
2165 /* we just set the new value of local_GP->get */
2166
2167 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2168 "channel=%d\n", get, ch->partid, ch->number);
2169
2170 send_IPI = (msg_flags & XPC_M_INTERRUPT);
2171
2172 /*
2173 * We need to ensure that the message referenced by
2174 * local_GP->get is not XPC_M_DONE or that local_GP->get
2175 * equals w_local_GP.get, so we'll go have a look.
2176 */
2177 initial_get = get;
2178 }
2179
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002180 if (send_IPI)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002181 xpc_IPI_send_msgrequest(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002182}
2183
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002184/*
2185 * Acknowledge receipt of a delivered message.
2186 *
2187 * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
2188 * that sent the message.
2189 *
2190 * This function, although called by users, does not call xpc_part_ref() to
2191 * ensure that the partition infrastructure is in place. It relies on the
2192 * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
2193 *
2194 * Arguments:
2195 *
2196 * partid - ID of partition to which the channel is connected.
2197 * ch_number - channel # message received on.
2198 * payload - pointer to the payload area allocated via
2199 * xpc_initiate_allocate().
2200 */
2201void
Dean Nelson64d032b2008-05-12 14:02:03 -07002202xpc_initiate_received(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002203{
2204 struct xpc_partition *part = &xpc_partitions[partid];
2205 struct xpc_channel *ch;
2206 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2207 s64 get, msg_number = msg->number;
2208
Dean Nelsonbc63d382008-07-29 22:34:04 -07002209 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002210 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2211
2212 ch = &part->channels[ch_number];
2213
2214 dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05002215 (void *)msg, msg_number, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002216
Dean Nelson35190502008-04-22 14:48:55 -05002217 DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
2218 msg_number % ch->remote_nentries);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002219 DBUG_ON(msg->flags & XPC_M_DONE);
2220
2221 msg->flags |= XPC_M_DONE;
2222
2223 /*
2224 * The preceding store of msg->flags must occur before the following
2225 * load of ch->local_GP->get.
2226 */
2227 mb();
2228
2229 /*
2230 * See if this message is next in line to be acknowledged as having
2231 * been delivered.
2232 */
2233 get = ch->local_GP->get;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002234 if (get == msg_number)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002235 xpc_acknowledge_msgs(ch, get, msg->flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002236
2237 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
2238 xpc_msgqueue_deref(ch);
2239}