blob: 9c90c2d55c081172dc5ebcc9dbaa0ff15f20b888 [file] [log] [blame]
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Dean Nelson45d9ca42008-04-22 14:46:56 -05006 * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07007 */
8
Dean Nelson89eb8eb2005-03-23 19:50:00 -07009/*
10 * Cross Partition Communication (XPC) channel support.
11 *
12 * This is the part of XPC that manages the channels and
13 * sends/receives messages across them to/from other partitions.
14 *
15 */
16
Dean Nelson89eb8eb2005-03-23 19:50:00 -070017#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/sched.h>
20#include <linux/cache.h>
21#include <linux/interrupt.h>
Jes Sorensenf9e505a2006-01-17 12:52:21 -050022#include <linux/mutex.h>
23#include <linux/completion.h>
Dean Nelson89eb8eb2005-03-23 19:50:00 -070024#include <asm/sn/bte.h>
25#include <asm/sn/sn_sal.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050026#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070027
Dean Nelson89eb8eb2005-03-23 19:50:00 -070028/*
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050029 * Guarantee that the kzalloc'd memory is cacheline aligned.
30 */
31static void *
32xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
33{
34 /* see if kzalloc will give us cachline aligned memory by default */
35 *base = kzalloc(size, flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050036 if (*base == NULL)
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050037 return NULL;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050038
39 if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050040 return *base;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050041
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050042 kfree(*base);
43
44 /* nope, we'll have to do it ourselves */
45 *base = kzalloc(size + L1_CACHE_BYTES, flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050046 if (*base == NULL)
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050047 return NULL;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050048
Dean Nelson35190502008-04-22 14:48:55 -050049 return (void *)L1_CACHE_ALIGN((u64)*base);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050050}
51
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050052/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070053 * Set up the initial values for the XPartition Communication channels.
54 */
55static void
Dean Nelson64d032b2008-05-12 14:02:03 -070056xpc_initialize_channels(struct xpc_partition *part, short partid)
Dean Nelson89eb8eb2005-03-23 19:50:00 -070057{
58 int ch_number;
59 struct xpc_channel *ch;
60
Dean Nelson89eb8eb2005-03-23 19:50:00 -070061 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
62 ch = &part->channels[ch_number];
63
64 ch->partid = partid;
65 ch->number = ch_number;
66 ch->flags = XPC_C_DISCONNECTED;
67
68 ch->local_GP = &part->local_GPs[ch_number];
69 ch->local_openclose_args =
Dean Nelson35190502008-04-22 14:48:55 -050070 &part->local_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -070071
72 atomic_set(&ch->kthreads_assigned, 0);
73 atomic_set(&ch->kthreads_idle, 0);
74 atomic_set(&ch->kthreads_active, 0);
75
76 atomic_set(&ch->references, 0);
77 atomic_set(&ch->n_to_notify, 0);
78
79 spin_lock_init(&ch->lock);
Jes Sorensenf9e505a2006-01-17 12:52:21 -050080 mutex_init(&ch->msg_to_pull_mutex);
81 init_completion(&ch->wdisconnect_wait);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070082
83 atomic_set(&ch->n_on_msg_allocate_wq, 0);
84 init_waitqueue_head(&ch->msg_allocate_wq);
85 init_waitqueue_head(&ch->idle_wq);
86 }
87}
88
Dean Nelson89eb8eb2005-03-23 19:50:00 -070089/*
90 * Setup the infrastructure necessary to support XPartition Communication
91 * between the specified remote partition and the local one.
92 */
Dean Nelson65c17b82008-05-12 14:02:02 -070093enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -070094xpc_setup_infrastructure(struct xpc_partition *part)
95{
Dean Nelson59a0a8a2005-07-13 05:45:00 -070096 int ret, cpuid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070097 struct timer_list *timer;
Dean Nelson64d032b2008-05-12 14:02:03 -070098 short partid = XPC_PARTID(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070099
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700100 /*
101 * Zero out MOST of the entry for this partition. Only the fields
102 * starting with `nchannels' will be zeroed. The preceding fields must
103 * remain `viable' across partition ups and downs, since they may be
104 * referenced during this memset() operation.
105 */
106 memset(&part->nchannels, 0, sizeof(struct xpc_partition) -
Dean Nelson35190502008-04-22 14:48:55 -0500107 offsetof(struct xpc_partition, nchannels));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700108
109 /*
110 * Allocate all of the channel structures as a contiguous chunk of
111 * memory.
112 */
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500113 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_NCHANNELS,
Dean Nelson35190502008-04-22 14:48:55 -0500114 GFP_KERNEL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700115 if (part->channels == NULL) {
116 dev_err(xpc_chan, "can't get memory for channels\n");
Dean Nelson65c17b82008-05-12 14:02:02 -0700117 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700118 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700119
120 part->nchannels = XPC_NCHANNELS;
121
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700122 /* allocate all the required GET/PUT values */
123
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500124 part->local_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
Dean Nelson35190502008-04-22 14:48:55 -0500125 GFP_KERNEL,
126 &part->local_GPs_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700127 if (part->local_GPs == NULL) {
128 kfree(part->channels);
129 part->channels = NULL;
130 dev_err(xpc_chan, "can't get memory for local get/put "
131 "values\n");
Dean Nelson65c17b82008-05-12 14:02:02 -0700132 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700133 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700134
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500135 part->remote_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
Dean Nelson35190502008-04-22 14:48:55 -0500136 GFP_KERNEL,
137 &part->
138 remote_GPs_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700139 if (part->remote_GPs == NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700140 dev_err(xpc_chan, "can't get memory for remote get/put "
141 "values\n");
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500142 kfree(part->local_GPs_base);
143 part->local_GPs = NULL;
144 kfree(part->channels);
145 part->channels = NULL;
Dean Nelson65c17b82008-05-12 14:02:02 -0700146 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700147 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700148
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700149 /* allocate all the required open and close args */
150
Dean Nelson35190502008-04-22 14:48:55 -0500151 part->local_openclose_args =
152 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
153 &part->local_openclose_args_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700154 if (part->local_openclose_args == NULL) {
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500155 dev_err(xpc_chan, "can't get memory for local connect args\n");
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700156 kfree(part->remote_GPs_base);
157 part->remote_GPs = NULL;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500158 kfree(part->local_GPs_base);
159 part->local_GPs = NULL;
160 kfree(part->channels);
161 part->channels = NULL;
Dean Nelson65c17b82008-05-12 14:02:02 -0700162 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700163 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700164
Dean Nelson35190502008-04-22 14:48:55 -0500165 part->remote_openclose_args =
166 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
167 &part->remote_openclose_args_base);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700168 if (part->remote_openclose_args == NULL) {
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500169 dev_err(xpc_chan, "can't get memory for remote connect args\n");
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700170 kfree(part->local_openclose_args_base);
171 part->local_openclose_args = NULL;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500172 kfree(part->remote_GPs_base);
173 part->remote_GPs = NULL;
174 kfree(part->local_GPs_base);
175 part->local_GPs = NULL;
176 kfree(part->channels);
177 part->channels = NULL;
Dean Nelson65c17b82008-05-12 14:02:02 -0700178 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700179 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700180
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700181 xpc_initialize_channels(part, partid);
182
183 atomic_set(&part->nchannels_active, 0);
Dean Nelsona607c382005-09-01 14:01:37 -0500184 atomic_set(&part->nchannels_engaged, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700185
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700186 /* local_IPI_amo were set to 0 by an earlier memset() */
187
188 /* Initialize this partitions AMO_t structure */
189 part->local_IPI_amo_va = xpc_IPI_init(partid);
190
191 spin_lock_init(&part->IPI_lock);
192
193 atomic_set(&part->channel_mgr_requests, 1);
194 init_waitqueue_head(&part->channel_mgr_wq);
195
196 sprintf(part->IPI_owner, "xpc%02d", partid);
Thomas Gleixner121a4222006-07-01 19:29:17 -0700197 ret = request_irq(SGI_XPC_NOTIFY, xpc_notify_IRQ_handler, IRQF_SHARED,
Dean Nelson35190502008-04-22 14:48:55 -0500198 part->IPI_owner, (void *)(u64)partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700199 if (ret != 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700200 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
201 "errno=%d\n", -ret);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500202 kfree(part->remote_openclose_args_base);
203 part->remote_openclose_args = NULL;
204 kfree(part->local_openclose_args_base);
205 part->local_openclose_args = NULL;
206 kfree(part->remote_GPs_base);
207 part->remote_GPs = NULL;
208 kfree(part->local_GPs_base);
209 part->local_GPs = NULL;
210 kfree(part->channels);
211 part->channels = NULL;
Dean Nelson65c17b82008-05-12 14:02:02 -0700212 return xpLackOfResources;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700213 }
214
215 /* Setup a timer to check for dropped IPIs */
216 timer = &part->dropped_IPI_timer;
217 init_timer(timer);
Dean Nelson35190502008-04-22 14:48:55 -0500218 timer->function = (void (*)(unsigned long))xpc_dropped_IPI_check;
219 timer->data = (unsigned long)part;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700220 timer->expires = jiffies + XPC_P_DROPPED_IPI_WAIT;
221 add_timer(timer);
222
223 /*
224 * With the setting of the partition setup_state to XPC_P_SETUP, we're
225 * declaring that this partition is ready to go.
226 */
Dave Jones821fe942005-06-25 14:54:29 -0700227 part->setup_state = XPC_P_SETUP;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700228
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700229 /*
230 * Setup the per partition specific variables required by the
231 * remote partition to establish channel connections with us.
232 *
233 * The setting of the magic # indicates that these per partition
234 * specific variables are ready to be used.
235 */
236 xpc_vars_part[partid].GPs_pa = __pa(part->local_GPs);
237 xpc_vars_part[partid].openclose_args_pa =
Dean Nelson35190502008-04-22 14:48:55 -0500238 __pa(part->local_openclose_args);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700239 xpc_vars_part[partid].IPI_amo_pa = __pa(part->local_IPI_amo_va);
Dean Nelson59a0a8a2005-07-13 05:45:00 -0700240 cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
241 xpc_vars_part[partid].IPI_nasid = cpuid_to_nasid(cpuid);
242 xpc_vars_part[partid].IPI_phys_cpuid = cpu_physical_id(cpuid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700243 xpc_vars_part[partid].nchannels = part->nchannels;
Dave Jones821fe942005-06-25 14:54:29 -0700244 xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700245
Dean Nelson65c17b82008-05-12 14:02:02 -0700246 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700247}
248
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700249/*
250 * Create a wrapper that hides the underlying mechanism for pulling a cacheline
251 * (or multiple cachelines) from a remote partition.
252 *
253 * src must be a cacheline aligned physical address on the remote partition.
254 * dst must be a cacheline aligned virtual address on this partition.
255 * cnt must be an cacheline sized
256 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700257static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700258xpc_pull_remote_cachelines(struct xpc_partition *part, void *dst,
Dean Nelson35190502008-04-22 14:48:55 -0500259 const void *src, size_t cnt)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700260{
261 bte_result_t bte_ret;
262
Dean Nelson35190502008-04-22 14:48:55 -0500263 DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
264 DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700265 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
266
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500267 if (part->act_state == XPC_P_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700268 return part->reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700269
Dean Nelson35190502008-04-22 14:48:55 -0500270 bte_ret = xp_bte_copy((u64)src, (u64)dst, (u64)cnt,
271 (BTE_NORMAL | BTE_WACQUIRE), NULL);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500272 if (bte_ret == BTE_SUCCESS)
Dean Nelson65c17b82008-05-12 14:02:02 -0700273 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700274
275 dev_dbg(xpc_chan, "xp_bte_copy() from partition %d failed, ret=%d\n",
276 XPC_PARTID(part), bte_ret);
277
278 return xpc_map_bte_errors(bte_ret);
279}
280
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700281/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700282 * Pull the remote per partition specific variables from the specified
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700283 * partition.
284 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700285enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700286xpc_pull_remote_vars_part(struct xpc_partition *part)
287{
288 u8 buffer[L1_CACHE_BYTES * 2];
289 struct xpc_vars_part *pulled_entry_cacheline =
Dean Nelson35190502008-04-22 14:48:55 -0500290 (struct xpc_vars_part *)L1_CACHE_ALIGN((u64)buffer);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700291 struct xpc_vars_part *pulled_entry;
292 u64 remote_entry_cacheline_pa, remote_entry_pa;
Dean Nelson64d032b2008-05-12 14:02:03 -0700293 short partid = XPC_PARTID(part);
Dean Nelson65c17b82008-05-12 14:02:02 -0700294 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700295
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700296 /* pull the cacheline that contains the variables we're interested in */
297
298 DBUG_ON(part->remote_vars_part_pa !=
Dean Nelson35190502008-04-22 14:48:55 -0500299 L1_CACHE_ALIGN(part->remote_vars_part_pa));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700300 DBUG_ON(sizeof(struct xpc_vars_part) != L1_CACHE_BYTES / 2);
301
302 remote_entry_pa = part->remote_vars_part_pa +
Dean Nelson35190502008-04-22 14:48:55 -0500303 sn_partition_id * sizeof(struct xpc_vars_part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700304
305 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
306
Dean Nelson35190502008-04-22 14:48:55 -0500307 pulled_entry = (struct xpc_vars_part *)((u64)pulled_entry_cacheline +
308 (remote_entry_pa &
309 (L1_CACHE_BYTES - 1)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700310
311 ret = xpc_pull_remote_cachelines(part, pulled_entry_cacheline,
Dean Nelson35190502008-04-22 14:48:55 -0500312 (void *)remote_entry_cacheline_pa,
313 L1_CACHE_BYTES);
Dean Nelson65c17b82008-05-12 14:02:02 -0700314 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700315 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
316 "partition %d, ret=%d\n", partid, ret);
317 return ret;
318 }
319
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700320 /* see if they've been set up yet */
321
322 if (pulled_entry->magic != XPC_VP_MAGIC1 &&
Dean Nelson35190502008-04-22 14:48:55 -0500323 pulled_entry->magic != XPC_VP_MAGIC2) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700324
325 if (pulled_entry->magic != 0) {
326 dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
327 "partition %d has bad magic value (=0x%lx)\n",
328 partid, sn_partition_id, pulled_entry->magic);
Dean Nelson65c17b82008-05-12 14:02:02 -0700329 return xpBadMagic;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700330 }
331
332 /* they've not been initialized yet */
Dean Nelson65c17b82008-05-12 14:02:02 -0700333 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700334 }
335
336 if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
337
338 /* validate the variables */
339
340 if (pulled_entry->GPs_pa == 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500341 pulled_entry->openclose_args_pa == 0 ||
342 pulled_entry->IPI_amo_pa == 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700343
344 dev_err(xpc_chan, "partition %d's XPC vars_part for "
345 "partition %d are not valid\n", partid,
346 sn_partition_id);
Dean Nelson65c17b82008-05-12 14:02:02 -0700347 return xpInvalidAddress;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700348 }
349
350 /* the variables we imported look to be valid */
351
352 part->remote_GPs_pa = pulled_entry->GPs_pa;
353 part->remote_openclose_args_pa =
Dean Nelson35190502008-04-22 14:48:55 -0500354 pulled_entry->openclose_args_pa;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700355 part->remote_IPI_amo_va =
Dean Nelson35190502008-04-22 14:48:55 -0500356 (AMO_t *)__va(pulled_entry->IPI_amo_pa);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700357 part->remote_IPI_nasid = pulled_entry->IPI_nasid;
358 part->remote_IPI_phys_cpuid = pulled_entry->IPI_phys_cpuid;
359
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500360 if (part->nchannels > pulled_entry->nchannels)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700361 part->nchannels = pulled_entry->nchannels;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700362
363 /* let the other side know that we've pulled their variables */
364
Dave Jones821fe942005-06-25 14:54:29 -0700365 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700366 }
367
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500368 if (pulled_entry->magic == XPC_VP_MAGIC1)
Dean Nelson65c17b82008-05-12 14:02:02 -0700369 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700370
Dean Nelson65c17b82008-05-12 14:02:02 -0700371 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700372}
373
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700374/*
375 * Get the IPI flags and pull the openclose args and/or remote GPs as needed.
376 */
377static u64
378xpc_get_IPI_flags(struct xpc_partition *part)
379{
380 unsigned long irq_flags;
381 u64 IPI_amo;
Dean Nelson65c17b82008-05-12 14:02:02 -0700382 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700383
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700384 /*
385 * See if there are any IPI flags to be handled.
386 */
387
388 spin_lock_irqsave(&part->IPI_lock, irq_flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500389 IPI_amo = part->local_IPI_amo;
390 if (IPI_amo != 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700391 part->local_IPI_amo = 0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500392
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700393 spin_unlock_irqrestore(&part->IPI_lock, irq_flags);
394
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700395 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_amo)) {
396 ret = xpc_pull_remote_cachelines(part,
Dean Nelson35190502008-04-22 14:48:55 -0500397 part->remote_openclose_args,
398 (void *)part->
399 remote_openclose_args_pa,
400 XPC_OPENCLOSE_ARGS_SIZE);
Dean Nelson65c17b82008-05-12 14:02:02 -0700401 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700402 XPC_DEACTIVATE_PARTITION(part, ret);
403
404 dev_dbg(xpc_chan, "failed to pull openclose args from "
405 "partition %d, ret=%d\n", XPC_PARTID(part),
406 ret);
407
408 /* don't bother processing IPIs anymore */
409 IPI_amo = 0;
410 }
411 }
412
413 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_amo)) {
414 ret = xpc_pull_remote_cachelines(part, part->remote_GPs,
Dean Nelson35190502008-04-22 14:48:55 -0500415 (void *)part->remote_GPs_pa,
416 XPC_GP_SIZE);
Dean Nelson65c17b82008-05-12 14:02:02 -0700417 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700418 XPC_DEACTIVATE_PARTITION(part, ret);
419
420 dev_dbg(xpc_chan, "failed to pull GPs from partition "
421 "%d, ret=%d\n", XPC_PARTID(part), ret);
422
423 /* don't bother processing IPIs anymore */
424 IPI_amo = 0;
425 }
426 }
427
428 return IPI_amo;
429}
430
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700431/*
432 * Allocate the local message queue and the notify queue.
433 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700434static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700435xpc_allocate_local_msgqueue(struct xpc_channel *ch)
436{
437 unsigned long irq_flags;
438 int nentries;
439 size_t nbytes;
440
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700441 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
442
443 nbytes = nentries * ch->msg_size;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500444 ch->local_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
Dean Nelson35190502008-04-22 14:48:55 -0500445 GFP_KERNEL,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500446 &ch->local_msgqueue_base);
447 if (ch->local_msgqueue == NULL)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700448 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700449
450 nbytes = nentries * sizeof(struct xpc_notify);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500451 ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700452 if (ch->notify_queue == NULL) {
453 kfree(ch->local_msgqueue_base);
454 ch->local_msgqueue = NULL;
455 continue;
456 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700457
458 spin_lock_irqsave(&ch->lock, irq_flags);
459 if (nentries < ch->local_nentries) {
460 dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
461 "partid=%d, channel=%d\n", nentries,
462 ch->local_nentries, ch->partid, ch->number);
463
464 ch->local_nentries = nentries;
465 }
466 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700467 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700468 }
469
470 dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
471 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson65c17b82008-05-12 14:02:02 -0700472 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700473}
474
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700475/*
476 * Allocate the cached remote message queue.
477 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700478static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700479xpc_allocate_remote_msgqueue(struct xpc_channel *ch)
480{
481 unsigned long irq_flags;
482 int nentries;
483 size_t nbytes;
484
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700485 DBUG_ON(ch->remote_nentries <= 0);
486
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700487 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
488
489 nbytes = nentries * ch->msg_size;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500490 ch->remote_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
Dean Nelson35190502008-04-22 14:48:55 -0500491 GFP_KERNEL,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500492 &ch->remote_msgqueue_base);
493 if (ch->remote_msgqueue == NULL)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700494 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700495
496 spin_lock_irqsave(&ch->lock, irq_flags);
497 if (nentries < ch->remote_nentries) {
498 dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
499 "partid=%d, channel=%d\n", nentries,
500 ch->remote_nentries, ch->partid, ch->number);
501
502 ch->remote_nentries = nentries;
503 }
504 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700505 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700506 }
507
508 dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
509 "partid=%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson65c17b82008-05-12 14:02:02 -0700510 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700511}
512
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700513/*
514 * Allocate message queues and other stuff associated with a channel.
515 *
516 * Note: Assumes all of the channel sizes are filled in.
517 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700518static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700519xpc_allocate_msgqueues(struct xpc_channel *ch)
520{
521 unsigned long irq_flags;
Dean Nelson65c17b82008-05-12 14:02:02 -0700522 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700523
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700524 DBUG_ON(ch->flags & XPC_C_SETUP);
525
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500526 ret = xpc_allocate_local_msgqueue(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -0700527 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700528 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700529
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500530 ret = xpc_allocate_remote_msgqueue(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -0700531 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700532 kfree(ch->local_msgqueue_base);
533 ch->local_msgqueue = NULL;
534 kfree(ch->notify_queue);
535 ch->notify_queue = NULL;
536 return ret;
537 }
538
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700539 spin_lock_irqsave(&ch->lock, irq_flags);
540 ch->flags |= XPC_C_SETUP;
541 spin_unlock_irqrestore(&ch->lock, irq_flags);
542
Dean Nelson65c17b82008-05-12 14:02:02 -0700543 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700544}
545
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700546/*
547 * Process a connect message from a remote partition.
548 *
549 * Note: xpc_process_connect() is expecting to be called with the
550 * spin_lock_irqsave held and will leave it locked upon return.
551 */
552static void
553xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
554{
Dean Nelson65c17b82008-05-12 14:02:02 -0700555 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700556
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700557 DBUG_ON(!spin_is_locked(&ch->lock));
558
559 if (!(ch->flags & XPC_C_OPENREQUEST) ||
Dean Nelson35190502008-04-22 14:48:55 -0500560 !(ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700561 /* nothing more to do for now */
562 return;
563 }
564 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
565
566 if (!(ch->flags & XPC_C_SETUP)) {
567 spin_unlock_irqrestore(&ch->lock, *irq_flags);
568 ret = xpc_allocate_msgqueues(ch);
569 spin_lock_irqsave(&ch->lock, *irq_flags);
570
Dean Nelson65c17b82008-05-12 14:02:02 -0700571 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700572 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500573
574 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700575 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700576
577 DBUG_ON(!(ch->flags & XPC_C_SETUP));
578 DBUG_ON(ch->local_msgqueue == NULL);
579 DBUG_ON(ch->remote_msgqueue == NULL);
580 }
581
582 if (!(ch->flags & XPC_C_OPENREPLY)) {
583 ch->flags |= XPC_C_OPENREPLY;
584 xpc_IPI_send_openreply(ch, irq_flags);
585 }
586
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500587 if (!(ch->flags & XPC_C_ROPENREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700588 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700589
590 DBUG_ON(ch->remote_msgqueue_pa == 0);
591
592 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
593
594 dev_info(xpc_chan, "channel %d to partition %d connected\n",
Dean Nelson35190502008-04-22 14:48:55 -0500595 ch->number, ch->partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700596
597 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -0600598 xpc_create_kthreads(ch, 1, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700599 spin_lock_irqsave(&ch->lock, *irq_flags);
600}
601
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700602/*
Dean Nelsona607c382005-09-01 14:01:37 -0500603 * Notify those who wanted to be notified upon delivery of their message.
604 */
605static void
Dean Nelson65c17b82008-05-12 14:02:02 -0700606xpc_notify_senders(struct xpc_channel *ch, enum xp_retval reason, s64 put)
Dean Nelsona607c382005-09-01 14:01:37 -0500607{
608 struct xpc_notify *notify;
609 u8 notify_type;
610 s64 get = ch->w_remote_GP.get - 1;
611
Dean Nelsona607c382005-09-01 14:01:37 -0500612 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
613
614 notify = &ch->notify_queue[get % ch->local_nentries];
615
616 /*
617 * See if the notify entry indicates it was associated with
618 * a message who's sender wants to be notified. It is possible
619 * that it is, but someone else is doing or has done the
620 * notification.
621 */
622 notify_type = notify->type;
623 if (notify_type == 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500624 cmpxchg(&notify->type, notify_type, 0) != notify_type) {
Dean Nelsona607c382005-09-01 14:01:37 -0500625 continue;
626 }
627
628 DBUG_ON(notify_type != XPC_N_CALL);
629
630 atomic_dec(&ch->n_to_notify);
631
632 if (notify->func != NULL) {
633 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
634 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -0500635 (void *)notify, get, ch->partid, ch->number);
Dean Nelsona607c382005-09-01 14:01:37 -0500636
637 notify->func(reason, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500638 notify->key);
Dean Nelsona607c382005-09-01 14:01:37 -0500639
640 dev_dbg(xpc_chan, "notify->func() returned, "
641 "notify=0x%p, msg_number=%ld, partid=%d, "
Dean Nelson35190502008-04-22 14:48:55 -0500642 "channel=%d\n", (void *)notify, get,
Dean Nelsona607c382005-09-01 14:01:37 -0500643 ch->partid, ch->number);
644 }
645 }
646}
647
Dean Nelsona607c382005-09-01 14:01:37 -0500648/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700649 * Free up message queues and other stuff that were allocated for the specified
650 * channel.
651 *
652 * Note: ch->reason and ch->reason_line are left set for debugging purposes,
653 * they're cleared when XPC_C_DISCONNECTED is cleared.
654 */
655static void
656xpc_free_msgqueues(struct xpc_channel *ch)
657{
658 DBUG_ON(!spin_is_locked(&ch->lock));
659 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
660
661 ch->remote_msgqueue_pa = 0;
662 ch->func = NULL;
663 ch->key = NULL;
664 ch->msg_size = 0;
665 ch->local_nentries = 0;
666 ch->remote_nentries = 0;
667 ch->kthreads_assigned_limit = 0;
668 ch->kthreads_idle_limit = 0;
669
670 ch->local_GP->get = 0;
671 ch->local_GP->put = 0;
672 ch->remote_GP.get = 0;
673 ch->remote_GP.put = 0;
674 ch->w_local_GP.get = 0;
675 ch->w_local_GP.put = 0;
676 ch->w_remote_GP.get = 0;
677 ch->w_remote_GP.put = 0;
678 ch->next_msg_to_pull = 0;
679
680 if (ch->flags & XPC_C_SETUP) {
681 ch->flags &= ~XPC_C_SETUP;
682
683 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
684 ch->flags, ch->partid, ch->number);
685
686 kfree(ch->local_msgqueue_base);
687 ch->local_msgqueue = NULL;
688 kfree(ch->remote_msgqueue_base);
689 ch->remote_msgqueue = NULL;
690 kfree(ch->notify_queue);
691 ch->notify_queue = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700692 }
693}
694
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700695/*
696 * spin_lock_irqsave() is expected to be held on entry.
697 */
698static void
699xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
700{
701 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c382005-09-01 14:01:37 -0500702 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700703
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700704 DBUG_ON(!spin_is_locked(&ch->lock));
705
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500706 if (!(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700707 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700708
709 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
710
711 /* make sure all activity has settled down first */
712
Dean Nelsona460ef82006-11-22 08:25:00 -0600713 if (atomic_read(&ch->kthreads_assigned) > 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500714 atomic_read(&ch->references) > 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700715 return;
716 }
Dean Nelsona460ef82006-11-22 08:25:00 -0600717 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500718 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700719
Dean Nelsona607c382005-09-01 14:01:37 -0500720 if (part->act_state == XPC_P_DEACTIVATING) {
721 /* can't proceed until the other side disengages from us */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500722 if (xpc_partition_engaged(1UL << ch->partid))
Dean Nelsona607c382005-09-01 14:01:37 -0500723 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700724
Dean Nelsona607c382005-09-01 14:01:37 -0500725 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700726
727 /* as long as the other side is up do the full protocol */
728
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500729 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700730 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700731
732 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
733 ch->flags |= XPC_C_CLOSEREPLY;
734 xpc_IPI_send_closereply(ch, irq_flags);
735 }
736
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500737 if (!(ch->flags & XPC_C_RCLOSEREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700738 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700739 }
740
Dean Nelsona607c382005-09-01 14:01:37 -0500741 /* wake those waiting for notify completion */
742 if (atomic_read(&ch->n_to_notify) > 0) {
743 /* >>> we do callout while holding ch->lock */
744 xpc_notify_senders(ch, ch->reason, ch->w_local_GP.put);
745 }
746
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700747 /* both sides are disconnected now */
748
Dean Nelson4c2cd962006-02-15 08:02:21 -0600749 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600750 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700751 xpc_disconnect_callout(ch, xpDisconnected);
Dean Nelson246c7e32005-12-22 14:32:56 -0600752 spin_lock_irqsave(&ch->lock, *irq_flags);
753 }
754
Dean Nelsona607c382005-09-01 14:01:37 -0500755 /* it's now safe to free the channel's message queues */
756 xpc_free_msgqueues(ch);
757
758 /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
759 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700760
761 atomic_dec(&part->nchannels_active);
762
Dean Nelsona607c382005-09-01 14:01:37 -0500763 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700764 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
Dean Nelson35190502008-04-22 14:48:55 -0500765 "reason=%d\n", ch->number, ch->partid, ch->reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700766 }
Dean Nelsona607c382005-09-01 14:01:37 -0500767
Dean Nelsona607c382005-09-01 14:01:37 -0500768 if (ch->flags & XPC_C_WDISCONNECT) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500769 /* we won't lose the CPU since we're holding ch->lock */
770 complete(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500771 } else if (ch->delayed_IPI_flags) {
772 if (part->act_state != XPC_P_DEACTIVATING) {
773 /* time to take action on any delayed IPI flags */
774 spin_lock(&part->IPI_lock);
775 XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500776 ch->delayed_IPI_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500777 spin_unlock(&part->IPI_lock);
778 }
779 ch->delayed_IPI_flags = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500780 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700781}
782
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700783/*
784 * Process a change in the channel's remote connection state.
785 */
786static void
787xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
Dean Nelson35190502008-04-22 14:48:55 -0500788 u8 IPI_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700789{
790 unsigned long irq_flags;
791 struct xpc_openclose_args *args =
Dean Nelson35190502008-04-22 14:48:55 -0500792 &part->remote_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700793 struct xpc_channel *ch = &part->channels[ch_number];
Dean Nelson65c17b82008-05-12 14:02:02 -0700794 enum xp_retval reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700795
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700796 spin_lock_irqsave(&ch->lock, irq_flags);
797
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500798again:
Dean Nelsone54af722005-10-25 14:07:43 -0500799
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500800 if ((ch->flags & XPC_C_DISCONNECTED) &&
801 (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500802 /*
803 * Delay processing IPI flags until thread waiting disconnect
804 * has had a chance to see that the channel is disconnected.
805 */
806 ch->delayed_IPI_flags |= IPI_flags;
807 spin_unlock_irqrestore(&ch->lock, irq_flags);
808 return;
809 }
810
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700811 if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
812
813 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
814 "from partid=%d, channel=%d\n", args->reason,
815 ch->partid, ch->number);
816
817 /*
818 * If RCLOSEREQUEST is set, we're probably waiting for
819 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelsona607c382005-09-01 14:01:37 -0500820 * with this RCLOSEREQUEST in the IPI_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700821 */
822
823 if (ch->flags & XPC_C_RCLOSEREQUEST) {
824 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
825 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
826 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
827 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
828
829 DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
830 IPI_flags &= ~XPC_IPI_CLOSEREPLY;
831 ch->flags |= XPC_C_RCLOSEREPLY;
832
833 /* both sides have finished disconnecting */
834 xpc_process_disconnect(ch, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500835 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
836 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700837 }
838
839 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700840 if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500841 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500842 ch_number) &
843 XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500844
845 DBUG_ON(ch->delayed_IPI_flags != 0);
846 spin_lock(&part->IPI_lock);
847 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500848 ch_number,
849 XPC_IPI_CLOSEREQUEST);
Dean Nelsone54af722005-10-25 14:07:43 -0500850 spin_unlock(&part->IPI_lock);
851 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700852 spin_unlock_irqrestore(&ch->lock, irq_flags);
853 return;
854 }
855
856 XPC_SET_REASON(ch, 0, 0);
857 ch->flags &= ~XPC_C_DISCONNECTED;
858
859 atomic_inc(&part->nchannels_active);
860 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
861 }
862
863 IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
864
865 /*
866 * The meaningful CLOSEREQUEST connection state fields are:
867 * reason = reason connection is to be closed
868 */
869
870 ch->flags |= XPC_C_RCLOSEREQUEST;
871
872 if (!(ch->flags & XPC_C_DISCONNECTING)) {
873 reason = args->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700874 if (reason <= xpSuccess || reason > xpUnknownReason)
875 reason = xpUnknownReason;
876 else if (reason == xpUnregistering)
877 reason = xpOtherUnregistering;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700878
879 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500880
881 DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
882 spin_unlock_irqrestore(&ch->lock, irq_flags);
883 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700884 }
Dean Nelsone54af722005-10-25 14:07:43 -0500885
886 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700887 }
888
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700889 if (IPI_flags & XPC_IPI_CLOSEREPLY) {
890
891 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
892 " channel=%d\n", ch->partid, ch->number);
893
894 if (ch->flags & XPC_C_DISCONNECTED) {
895 DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
896 spin_unlock_irqrestore(&ch->lock, irq_flags);
897 return;
898 }
899
900 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
Dean Nelsone54af722005-10-25 14:07:43 -0500901
902 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
903 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
Dean Nelson35190502008-04-22 14:48:55 -0500904 & XPC_IPI_CLOSEREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500905
906 DBUG_ON(ch->delayed_IPI_flags != 0);
907 spin_lock(&part->IPI_lock);
908 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500909 ch_number,
910 XPC_IPI_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500911 spin_unlock(&part->IPI_lock);
912 }
913 spin_unlock_irqrestore(&ch->lock, irq_flags);
914 return;
915 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700916
917 ch->flags |= XPC_C_RCLOSEREPLY;
918
919 if (ch->flags & XPC_C_CLOSEREPLY) {
920 /* both sides have finished disconnecting */
921 xpc_process_disconnect(ch, &irq_flags);
922 }
923 }
924
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700925 if (IPI_flags & XPC_IPI_OPENREQUEST) {
926
927 dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
928 "local_nentries=%d) received from partid=%d, "
929 "channel=%d\n", args->msg_size, args->local_nentries,
930 ch->partid, ch->number);
931
Dean Nelsone54af722005-10-25 14:07:43 -0500932 if (part->act_state == XPC_P_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500933 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500934 spin_unlock_irqrestore(&ch->lock, irq_flags);
935 return;
936 }
937
938 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
939 ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700940 spin_unlock_irqrestore(&ch->lock, irq_flags);
941 return;
942 }
943 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500944 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700945 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500946 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700947
948 /*
949 * The meaningful OPENREQUEST connection state fields are:
950 * msg_size = size of channel's messages in bytes
951 * local_nentries = remote partition's local_nentries
952 */
Dean Nelsone54af722005-10-25 14:07:43 -0500953 if (args->msg_size == 0 || args->local_nentries == 0) {
954 /* assume OPENREQUEST was delayed by mistake */
955 spin_unlock_irqrestore(&ch->lock, irq_flags);
956 return;
957 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700958
959 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
960 ch->remote_nentries = args->local_nentries;
961
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700962 if (ch->flags & XPC_C_OPENREQUEST) {
963 if (args->msg_size != ch->msg_size) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700964 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500965 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700966 spin_unlock_irqrestore(&ch->lock, irq_flags);
967 return;
968 }
969 } else {
970 ch->msg_size = args->msg_size;
971
972 XPC_SET_REASON(ch, 0, 0);
973 ch->flags &= ~XPC_C_DISCONNECTED;
974
975 atomic_inc(&part->nchannels_active);
976 }
977
978 xpc_process_connect(ch, &irq_flags);
979 }
980
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700981 if (IPI_flags & XPC_IPI_OPENREPLY) {
982
983 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
984 "local_nentries=%d, remote_nentries=%d) received from "
985 "partid=%d, channel=%d\n", args->local_msgqueue_pa,
986 args->local_nentries, args->remote_nentries,
987 ch->partid, ch->number);
988
989 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
990 spin_unlock_irqrestore(&ch->lock, irq_flags);
991 return;
992 }
Dean Nelsone54af722005-10-25 14:07:43 -0500993 if (!(ch->flags & XPC_C_OPENREQUEST)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700994 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -0500995 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500996 spin_unlock_irqrestore(&ch->lock, irq_flags);
997 return;
998 }
999
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001000 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
1001 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1002
1003 /*
1004 * The meaningful OPENREPLY connection state fields are:
1005 * local_msgqueue_pa = physical address of remote
Dean Nelson35190502008-04-22 14:48:55 -05001006 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001007 * local_nentries = remote partition's local_nentries
1008 * remote_nentries = remote partition's remote_nentries
1009 */
1010 DBUG_ON(args->local_msgqueue_pa == 0);
1011 DBUG_ON(args->local_nentries == 0);
1012 DBUG_ON(args->remote_nentries == 0);
1013
1014 ch->flags |= XPC_C_ROPENREPLY;
1015 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
1016
1017 if (args->local_nentries < ch->remote_nentries) {
1018 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1019 "remote_nentries=%d, old remote_nentries=%d, "
1020 "partid=%d, channel=%d\n",
1021 args->local_nentries, ch->remote_nentries,
1022 ch->partid, ch->number);
1023
1024 ch->remote_nentries = args->local_nentries;
1025 }
1026 if (args->remote_nentries < ch->local_nentries) {
1027 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1028 "local_nentries=%d, old local_nentries=%d, "
1029 "partid=%d, channel=%d\n",
1030 args->remote_nentries, ch->local_nentries,
1031 ch->partid, ch->number);
1032
1033 ch->local_nentries = args->remote_nentries;
1034 }
1035
1036 xpc_process_connect(ch, &irq_flags);
1037 }
1038
1039 spin_unlock_irqrestore(&ch->lock, irq_flags);
1040}
1041
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001042/*
1043 * Attempt to establish a channel connection to a remote partition.
1044 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001045static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001046xpc_connect_channel(struct xpc_channel *ch)
1047{
1048 unsigned long irq_flags;
1049 struct xpc_registration *registration = &xpc_registrations[ch->number];
1050
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001051 if (mutex_trylock(&registration->mutex) == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -07001052 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001053
1054 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001055 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -07001056 return xpUnregistered;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001057 }
1058
1059 spin_lock_irqsave(&ch->lock, irq_flags);
1060
1061 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1062 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
1063
1064 if (ch->flags & XPC_C_DISCONNECTING) {
1065 spin_unlock_irqrestore(&ch->lock, irq_flags);
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001066 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001067 return ch->reason;
1068 }
1069
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001070 /* add info from the channel connect registration to the channel */
1071
1072 ch->kthreads_assigned_limit = registration->assigned_limit;
1073 ch->kthreads_idle_limit = registration->idle_limit;
1074 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
1075 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
1076 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
1077
1078 ch->func = registration->func;
1079 DBUG_ON(registration->func == NULL);
1080 ch->key = registration->key;
1081
1082 ch->local_nentries = registration->nentries;
1083
1084 if (ch->flags & XPC_C_ROPENREQUEST) {
1085 if (registration->msg_size != ch->msg_size) {
1086 /* the local and remote sides aren't the same */
1087
1088 /*
1089 * Because XPC_DISCONNECT_CHANNEL() can block we're
1090 * forced to up the registration sema before we unlock
1091 * the channel lock. But that's okay here because we're
1092 * done with the part that required the registration
1093 * sema. XPC_DISCONNECT_CHANNEL() requires that the
1094 * channel lock be locked and will unlock and relock
1095 * the channel lock as needed.
1096 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001097 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -07001098 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -05001099 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001100 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -07001101 return xpUnequalMsgSizes;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001102 }
1103 } else {
1104 ch->msg_size = registration->msg_size;
1105
1106 XPC_SET_REASON(ch, 0, 0);
1107 ch->flags &= ~XPC_C_DISCONNECTED;
1108
1109 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
1110 }
1111
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001112 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001113
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001114 /* initiate the connection */
1115
1116 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
1117 xpc_IPI_send_openrequest(ch, &irq_flags);
1118
1119 xpc_process_connect(ch, &irq_flags);
1120
1121 spin_unlock_irqrestore(&ch->lock, irq_flags);
1122
Dean Nelson65c17b82008-05-12 14:02:02 -07001123 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001124}
1125
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001126/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001127 * Clear some of the msg flags in the local message queue.
1128 */
1129static inline void
1130xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
1131{
1132 struct xpc_msg *msg;
1133 s64 get;
1134
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001135 get = ch->w_remote_GP.get;
1136 do {
Dean Nelson35190502008-04-22 14:48:55 -05001137 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1138 (get % ch->local_nentries) *
1139 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001140 msg->flags = 0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001141 } while (++get < ch->remote_GP.get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001142}
1143
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001144/*
1145 * Clear some of the msg flags in the remote message queue.
1146 */
1147static inline void
1148xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
1149{
1150 struct xpc_msg *msg;
1151 s64 put;
1152
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001153 put = ch->w_remote_GP.put;
1154 do {
Dean Nelson35190502008-04-22 14:48:55 -05001155 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
1156 (put % ch->remote_nentries) *
1157 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001158 msg->flags = 0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001159 } while (++put < ch->remote_GP.put);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001160}
1161
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001162static void
1163xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
1164{
1165 struct xpc_channel *ch = &part->channels[ch_number];
1166 int nmsgs_sent;
1167
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001168 ch->remote_GP = part->remote_GPs[ch_number];
1169
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001170 /* See what, if anything, has changed for each connected channel */
1171
1172 xpc_msgqueue_ref(ch);
1173
1174 if (ch->w_remote_GP.get == ch->remote_GP.get &&
Dean Nelson35190502008-04-22 14:48:55 -05001175 ch->w_remote_GP.put == ch->remote_GP.put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001176 /* nothing changed since GPs were last pulled */
1177 xpc_msgqueue_deref(ch);
1178 return;
1179 }
1180
Dean Nelson35190502008-04-22 14:48:55 -05001181 if (!(ch->flags & XPC_C_CONNECTED)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001182 xpc_msgqueue_deref(ch);
1183 return;
1184 }
1185
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001186 /*
1187 * First check to see if messages recently sent by us have been
1188 * received by the other side. (The remote GET value will have
1189 * changed since we last looked at it.)
1190 */
1191
1192 if (ch->w_remote_GP.get != ch->remote_GP.get) {
1193
1194 /*
1195 * We need to notify any senders that want to be notified
1196 * that their sent messages have been received by their
1197 * intended recipients. We need to do this before updating
1198 * w_remote_GP.get so that we don't allocate the same message
1199 * queue entries prematurely (see xpc_allocate_msg()).
1200 */
1201 if (atomic_read(&ch->n_to_notify) > 0) {
1202 /*
1203 * Notify senders that messages sent have been
1204 * received and delivered by the other side.
1205 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001206 xpc_notify_senders(ch, xpMsgDelivered,
Dean Nelson35190502008-04-22 14:48:55 -05001207 ch->remote_GP.get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001208 }
1209
1210 /*
1211 * Clear msg->flags in previously sent messages, so that
1212 * they're ready for xpc_allocate_msg().
1213 */
1214 xpc_clear_local_msgqueue_flags(ch);
1215
Dave Jones821fe942005-06-25 14:54:29 -07001216 ch->w_remote_GP.get = ch->remote_GP.get;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001217
1218 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1219 "channel=%d\n", ch->w_remote_GP.get, ch->partid,
1220 ch->number);
1221
1222 /*
1223 * If anyone was waiting for message queue entries to become
1224 * available, wake them up.
1225 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001226 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001227 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001228 }
1229
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001230 /*
1231 * Now check for newly sent messages by the other side. (The remote
1232 * PUT value will have changed since we last looked at it.)
1233 */
1234
1235 if (ch->w_remote_GP.put != ch->remote_GP.put) {
1236 /*
1237 * Clear msg->flags in previously received messages, so that
1238 * they're ready for xpc_get_deliverable_msg().
1239 */
1240 xpc_clear_remote_msgqueue_flags(ch);
1241
Dave Jones821fe942005-06-25 14:54:29 -07001242 ch->w_remote_GP.put = ch->remote_GP.put;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001243
1244 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1245 "channel=%d\n", ch->w_remote_GP.put, ch->partid,
1246 ch->number);
1247
1248 nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
1249 if (nmsgs_sent > 0) {
1250 dev_dbg(xpc_chan, "msgs waiting to be copied and "
1251 "delivered=%d, partid=%d, channel=%d\n",
1252 nmsgs_sent, ch->partid, ch->number);
1253
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001254 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001255 xpc_activate_kthreads(ch, nmsgs_sent);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001256 }
1257 }
1258
1259 xpc_msgqueue_deref(ch);
1260}
1261
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001262void
1263xpc_process_channel_activity(struct xpc_partition *part)
1264{
1265 unsigned long irq_flags;
1266 u64 IPI_amo, IPI_flags;
1267 struct xpc_channel *ch;
1268 int ch_number;
Dean Nelsona607c382005-09-01 14:01:37 -05001269 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001270
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001271 IPI_amo = xpc_get_IPI_flags(part);
1272
1273 /*
1274 * Initiate channel connections for registered channels.
1275 *
1276 * For each connected channel that has pending messages activate idle
1277 * kthreads and/or create new kthreads as needed.
1278 */
1279
1280 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1281 ch = &part->channels[ch_number];
1282
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001283 /*
1284 * Process any open or close related IPI flags, and then deal
1285 * with connecting or disconnecting the channel as required.
1286 */
1287
1288 IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
1289
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001290 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001291 xpc_process_openclose_IPI(part, ch_number, IPI_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001292
Dean Nelsona607c382005-09-01 14:01:37 -05001293 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001294
Dean Nelsona607c382005-09-01 14:01:37 -05001295 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001296 spin_lock_irqsave(&ch->lock, irq_flags);
1297 xpc_process_disconnect(ch, &irq_flags);
1298 spin_unlock_irqrestore(&ch->lock, irq_flags);
1299 continue;
1300 }
1301
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001302 if (part->act_state == XPC_P_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001303 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001304
Dean Nelsona607c382005-09-01 14:01:37 -05001305 if (!(ch_flags & XPC_C_CONNECTED)) {
1306 if (!(ch_flags & XPC_C_OPENREQUEST)) {
1307 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -05001308 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001309 } else {
1310 spin_lock_irqsave(&ch->lock, irq_flags);
1311 xpc_process_connect(ch, &irq_flags);
1312 spin_unlock_irqrestore(&ch->lock, irq_flags);
1313 }
1314 continue;
1315 }
1316
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001317 /*
1318 * Process any message related IPI flags, this may involve the
1319 * activation of kthreads to deliver any pending messages sent
1320 * from the other partition.
1321 */
1322
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001323 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001324 xpc_process_msg_IPI(part, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001325 }
1326}
1327
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001328/*
Dean Nelsona607c382005-09-01 14:01:37 -05001329 * XPC's heartbeat code calls this function to inform XPC that a partition is
1330 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001331 * infrastructure used for the just downed partition.
1332 *
1333 * XPC's heartbeat code will never call this function and xpc_partition_up()
1334 * at the same time. Nor will it ever make multiple calls to either function
1335 * at the same time.
1336 */
1337void
Dean Nelson65c17b82008-05-12 14:02:02 -07001338xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001339{
1340 unsigned long irq_flags;
1341 int ch_number;
1342 struct xpc_channel *ch;
1343
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001344 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
1345 XPC_PARTID(part), reason);
1346
1347 if (!xpc_part_ref(part)) {
1348 /* infrastructure for this partition isn't currently set up */
1349 return;
1350 }
1351
Dean Nelsona607c382005-09-01 14:01:37 -05001352 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001353
1354 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1355 ch = &part->channels[ch_number];
1356
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001357 xpc_msgqueue_ref(ch);
1358 spin_lock_irqsave(&ch->lock, irq_flags);
1359
1360 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
1361
1362 spin_unlock_irqrestore(&ch->lock, irq_flags);
1363 xpc_msgqueue_deref(ch);
1364 }
1365
1366 xpc_wakeup_channel_mgr(part);
1367
1368 xpc_part_deref(part);
1369}
1370
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001371/*
1372 * Teardown the infrastructure necessary to support XPartition Communication
1373 * between the specified remote partition and the local one.
1374 */
1375void
1376xpc_teardown_infrastructure(struct xpc_partition *part)
1377{
Dean Nelson64d032b2008-05-12 14:02:03 -07001378 short partid = XPC_PARTID(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001379
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001380 /*
1381 * We start off by making this partition inaccessible to local
1382 * processes by marking it as no longer setup. Then we make it
1383 * inaccessible to remote processes by clearing the XPC per partition
1384 * specific variable's magic # (which indicates that these variables
1385 * are no longer valid) and by ignoring all XPC notify IPIs sent to
1386 * this partition.
1387 */
1388
Dean Nelsona607c382005-09-01 14:01:37 -05001389 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001390 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1391 DBUG_ON(part->setup_state != XPC_P_SETUP);
1392 part->setup_state = XPC_P_WTEARDOWN;
1393
1394 xpc_vars_part[partid].magic = 0;
1395
Dean Nelson35190502008-04-22 14:48:55 -05001396 free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001397
1398 /*
Simon Arlott72fdbdc2007-05-11 14:55:43 -07001399 * Before proceeding with the teardown we have to wait until all
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001400 * existing references cease.
1401 */
1402 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1403
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001404 /* now we can begin tearing down the infrastructure */
1405
1406 part->setup_state = XPC_P_TORNDOWN;
1407
1408 /* in case we've still got outstanding timers registered... */
1409 del_timer_sync(&part->dropped_IPI_timer);
1410
1411 kfree(part->remote_openclose_args_base);
1412 part->remote_openclose_args = NULL;
1413 kfree(part->local_openclose_args_base);
1414 part->local_openclose_args = NULL;
1415 kfree(part->remote_GPs_base);
1416 part->remote_GPs = NULL;
1417 kfree(part->local_GPs_base);
1418 part->local_GPs = NULL;
1419 kfree(part->channels);
1420 part->channels = NULL;
1421 part->local_IPI_amo_va = NULL;
1422}
1423
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001424/*
1425 * Called by XP at the time of channel connection registration to cause
1426 * XPC to establish connections to all currently active partitions.
1427 */
1428void
1429xpc_initiate_connect(int ch_number)
1430{
Dean Nelson64d032b2008-05-12 14:02:03 -07001431 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001432 struct xpc_partition *part;
1433 struct xpc_channel *ch;
1434
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001435 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1436
1437 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1438 part = &xpc_partitions[partid];
1439
1440 if (xpc_part_ref(part)) {
1441 ch = &part->channels[ch_number];
1442
Dean Nelsone54af722005-10-25 14:07:43 -05001443 /*
1444 * Initiate the establishment of a connection on the
1445 * newly registered channel to the remote partition.
1446 */
1447 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001448 xpc_part_deref(part);
1449 }
1450 }
1451}
1452
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001453void
1454xpc_connected_callout(struct xpc_channel *ch)
1455{
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001456 /* let the registerer know that a connection has been established */
1457
1458 if (ch->func != NULL) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001459 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001460 "partid=%d, channel=%d\n", ch->partid, ch->number);
1461
Dean Nelson65c17b82008-05-12 14:02:02 -07001462 ch->func(xpConnected, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -05001463 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001464
Dean Nelson65c17b82008-05-12 14:02:02 -07001465 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001466 "partid=%d, channel=%d\n", ch->partid, ch->number);
1467 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001468}
1469
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001470/*
1471 * Called by XP at the time of channel connection unregistration to cause
1472 * XPC to teardown all current connections for the specified channel.
1473 *
1474 * Before returning xpc_initiate_disconnect() will wait until all connections
1475 * on the specified channel have been closed/torndown. So the caller can be
1476 * assured that they will not be receiving any more callouts from XPC to the
1477 * function they registered via xpc_connect().
1478 *
1479 * Arguments:
1480 *
1481 * ch_number - channel # to unregister.
1482 */
1483void
1484xpc_initiate_disconnect(int ch_number)
1485{
1486 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -07001487 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001488 struct xpc_partition *part;
1489 struct xpc_channel *ch;
1490
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001491 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1492
1493 /* initiate the channel disconnect for every active partition */
1494 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1495 part = &xpc_partitions[partid];
1496
1497 if (xpc_part_ref(part)) {
1498 ch = &part->channels[ch_number];
1499 xpc_msgqueue_ref(ch);
1500
1501 spin_lock_irqsave(&ch->lock, irq_flags);
1502
Dean Nelsona607c382005-09-01 14:01:37 -05001503 if (!(ch->flags & XPC_C_DISCONNECTED)) {
1504 ch->flags |= XPC_C_WDISCONNECT;
1505
Dean Nelson65c17b82008-05-12 14:02:02 -07001506 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -05001507 &irq_flags);
Dean Nelsona607c382005-09-01 14:01:37 -05001508 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001509
1510 spin_unlock_irqrestore(&ch->lock, irq_flags);
1511
1512 xpc_msgqueue_deref(ch);
1513 xpc_part_deref(part);
1514 }
1515 }
1516
1517 xpc_disconnect_wait(ch_number);
1518}
1519
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001520/*
1521 * To disconnect a channel, and reflect it back to all who may be waiting.
1522 *
Dean Nelsona607c382005-09-01 14:01:37 -05001523 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
1524 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
1525 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001526 *
1527 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
1528 */
1529void
1530xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson65c17b82008-05-12 14:02:02 -07001531 enum xp_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001532{
Dean Nelsona607c382005-09-01 14:01:37 -05001533 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001534
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001535 DBUG_ON(!spin_is_locked(&ch->lock));
1536
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001537 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001538 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001539
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001540 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
1541
1542 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
1543 reason, line, ch->partid, ch->number);
1544
1545 XPC_SET_REASON(ch, reason, line);
1546
Dean Nelsona607c382005-09-01 14:01:37 -05001547 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001548 /* some of these may not have been set */
1549 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -05001550 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
1551 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001552
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001553 xpc_IPI_send_closerequest(ch, irq_flags);
1554
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001555 if (channel_was_connected)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001556 ch->flags |= XPC_C_WASCONNECTED;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001557
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001558 spin_unlock_irqrestore(&ch->lock, *irq_flags);
1559
Dean Nelsona607c382005-09-01 14:01:37 -05001560 /* wake all idle kthreads so they can exit */
1561 if (atomic_read(&ch->kthreads_idle) > 0) {
1562 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -06001563
1564 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -05001565 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001566 /* start a kthread that will do the xpDisconnecting callout */
Dean Nelsona460ef82006-11-22 08:25:00 -06001567 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001568 }
1569
Dean Nelsona607c382005-09-01 14:01:37 -05001570 /* wake those waiting to allocate an entry from the local msg queue */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001571 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelsona607c382005-09-01 14:01:37 -05001572 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001573
1574 spin_lock_irqsave(&ch->lock, *irq_flags);
1575}
1576
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001577void
Dean Nelson65c17b82008-05-12 14:02:02 -07001578xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001579{
1580 /*
Dean Nelsona607c382005-09-01 14:01:37 -05001581 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001582 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c382005-09-01 14:01:37 -05001583 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001584 */
1585
1586 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -06001587 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
1588 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001589
Dean Nelson246c7e32005-12-22 14:32:56 -06001590 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001591
Dean Nelson246c7e32005-12-22 14:32:56 -06001592 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
1593 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001594 }
1595}
1596
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001597/*
1598 * Wait for a message entry to become available for the specified channel,
1599 * but don't wait any longer than 1 jiffy.
1600 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001601static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001602xpc_allocate_msg_wait(struct xpc_channel *ch)
1603{
Dean Nelson65c17b82008-05-12 14:02:02 -07001604 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001605
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001606 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001607 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001608 return ch->reason;
1609 }
1610
1611 atomic_inc(&ch->n_on_msg_allocate_wq);
1612 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
1613 atomic_dec(&ch->n_on_msg_allocate_wq);
1614
1615 if (ch->flags & XPC_C_DISCONNECTING) {
1616 ret = ch->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -07001617 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001618 } else if (ret == 0) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001619 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001620 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -07001621 ret = xpInterrupted;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001622 }
1623
1624 return ret;
1625}
1626
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001627/*
1628 * Allocate an entry for a message from the message queue associated with the
1629 * specified channel.
1630 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001631static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001632xpc_allocate_msg(struct xpc_channel *ch, u32 flags,
Dean Nelson35190502008-04-22 14:48:55 -05001633 struct xpc_msg **address_of_msg)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001634{
1635 struct xpc_msg *msg;
Dean Nelson65c17b82008-05-12 14:02:02 -07001636 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001637 s64 put;
1638
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001639 /* this reference will be dropped in xpc_send_msg() */
1640 xpc_msgqueue_ref(ch);
1641
1642 if (ch->flags & XPC_C_DISCONNECTING) {
1643 xpc_msgqueue_deref(ch);
1644 return ch->reason;
1645 }
1646 if (!(ch->flags & XPC_C_CONNECTED)) {
1647 xpc_msgqueue_deref(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -07001648 return xpNotConnected;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001649 }
1650
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001651 /*
1652 * Get the next available message entry from the local message queue.
1653 * If none are available, we'll make sure that we grab the latest
1654 * GP values.
1655 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001656 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001657
1658 while (1) {
1659
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001660 put = ch->w_local_GP.put;
1661 rmb(); /* guarantee that .put loads before .get */
1662 if (put - ch->w_remote_GP.get < ch->local_nentries) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001663
1664 /* There are available message entries. We need to try
1665 * to secure one for ourselves. We'll do this by trying
1666 * to increment w_local_GP.put as long as someone else
1667 * doesn't beat us to it. If they do, we'll have to
1668 * try again.
Dean Nelson35190502008-04-22 14:48:55 -05001669 */
1670 if (cmpxchg(&ch->w_local_GP.put, put, put + 1) == put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001671 /* we got the entry referenced by put */
1672 break;
1673 }
1674 continue; /* try again */
1675 }
1676
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001677 /*
1678 * There aren't any available msg entries at this time.
1679 *
1680 * In waiting for a message entry to become available,
1681 * we set a timeout in case the other side is not
1682 * sending completion IPIs. This lets us fake an IPI
1683 * that will cause the IPI handler to fetch the latest
1684 * GP values as if an IPI was sent by the other side.
1685 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001686 if (ret == xpTimeout)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001687 xpc_IPI_send_local_msgrequest(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001688
1689 if (flags & XPC_NOWAIT) {
1690 xpc_msgqueue_deref(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -07001691 return xpNoWait;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001692 }
1693
1694 ret = xpc_allocate_msg_wait(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -07001695 if (ret != xpInterrupted && ret != xpTimeout) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001696 xpc_msgqueue_deref(ch);
1697 return ret;
1698 }
1699 }
1700
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001701 /* get the message's address and initialize it */
Dean Nelson35190502008-04-22 14:48:55 -05001702 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1703 (put % ch->local_nentries) * ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001704
1705 DBUG_ON(msg->flags != 0);
1706 msg->number = put;
1707
1708 dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
1709 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
Dean Nelson35190502008-04-22 14:48:55 -05001710 (void *)msg, msg->number, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001711
1712 *address_of_msg = msg;
1713
Dean Nelson65c17b82008-05-12 14:02:02 -07001714 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001715}
1716
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001717/*
1718 * Allocate an entry for a message from the message queue associated with the
1719 * specified channel. NOTE that this routine can sleep waiting for a message
1720 * entry to become available. To not sleep, pass in the XPC_NOWAIT flag.
1721 *
1722 * Arguments:
1723 *
1724 * partid - ID of partition to which the channel is connected.
1725 * ch_number - channel #.
1726 * flags - see xpc.h for valid flags.
1727 * payload - address of the allocated payload area pointer (filled in on
1728 * return) in which the user-defined message is constructed.
1729 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001730enum xp_retval
Dean Nelson64d032b2008-05-12 14:02:03 -07001731xpc_initiate_allocate(short partid, int ch_number, u32 flags, void **payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001732{
1733 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson65c17b82008-05-12 14:02:02 -07001734 enum xp_retval ret = xpUnknownReason;
Tony Luck27f4aa32006-04-04 14:11:49 -07001735 struct xpc_msg *msg = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001736
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001737 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
1738 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1739
1740 *payload = NULL;
1741
1742 if (xpc_part_ref(part)) {
1743 ret = xpc_allocate_msg(&part->channels[ch_number], flags, &msg);
1744 xpc_part_deref(part);
1745
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001746 if (msg != NULL)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001747 *payload = &msg->payload;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001748 }
1749
1750 return ret;
1751}
1752
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001753/*
1754 * Now we actually send the messages that are ready to be sent by advancing
1755 * the local message queue's Put value and then send an IPI to the recipient
1756 * partition.
1757 */
1758static void
1759xpc_send_msgs(struct xpc_channel *ch, s64 initial_put)
1760{
1761 struct xpc_msg *msg;
1762 s64 put = initial_put + 1;
1763 int send_IPI = 0;
1764
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001765 while (1) {
1766
1767 while (1) {
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001768 if (put == ch->w_local_GP.put)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001769 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001770
Dean Nelson35190502008-04-22 14:48:55 -05001771 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1772 (put % ch->local_nentries) *
1773 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001774
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001775 if (!(msg->flags & XPC_M_READY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001776 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001777
1778 put++;
1779 }
1780
1781 if (put == initial_put) {
1782 /* nothing's changed */
1783 break;
1784 }
1785
1786 if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) !=
Dean Nelson35190502008-04-22 14:48:55 -05001787 initial_put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001788 /* someone else beat us to it */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001789 DBUG_ON(ch->local_GP->put < initial_put);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001790 break;
1791 }
1792
1793 /* we just set the new value of local_GP->put */
1794
1795 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
1796 "channel=%d\n", put, ch->partid, ch->number);
1797
1798 send_IPI = 1;
1799
1800 /*
1801 * We need to ensure that the message referenced by
1802 * local_GP->put is not XPC_M_READY or that local_GP->put
1803 * equals w_local_GP.put, so we'll go have a look.
1804 */
1805 initial_put = put;
1806 }
1807
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001808 if (send_IPI)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001809 xpc_IPI_send_msgrequest(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001810}
1811
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001812/*
1813 * Common code that does the actual sending of the message by advancing the
1814 * local message queue's Put value and sends an IPI to the partition the
1815 * message is being sent to.
1816 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001817static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001818xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type,
Dean Nelson35190502008-04-22 14:48:55 -05001819 xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001820{
Dean Nelson65c17b82008-05-12 14:02:02 -07001821 enum xp_retval ret = xpSuccess;
Dean Nelsona607c382005-09-01 14:01:37 -05001822 struct xpc_notify *notify = notify;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001823 s64 put, msg_number = msg->number;
1824
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001825 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
Dean Nelson35190502008-04-22 14:48:55 -05001826 DBUG_ON((((u64)msg - (u64)ch->local_msgqueue) / ch->msg_size) !=
1827 msg_number % ch->local_nentries);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001828 DBUG_ON(msg->flags & XPC_M_READY);
1829
1830 if (ch->flags & XPC_C_DISCONNECTING) {
1831 /* drop the reference grabbed in xpc_allocate_msg() */
1832 xpc_msgqueue_deref(ch);
1833 return ch->reason;
1834 }
1835
1836 if (notify_type != 0) {
1837 /*
1838 * Tell the remote side to send an ACK interrupt when the
1839 * message has been delivered.
1840 */
1841 msg->flags |= XPC_M_INTERRUPT;
1842
1843 atomic_inc(&ch->n_to_notify);
1844
1845 notify = &ch->notify_queue[msg_number % ch->local_nentries];
1846 notify->func = func;
1847 notify->key = key;
Dave Jones821fe942005-06-25 14:54:29 -07001848 notify->type = notify_type;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001849
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001850 /* >>> is a mb() needed here? */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001851
1852 if (ch->flags & XPC_C_DISCONNECTING) {
1853 /*
1854 * An error occurred between our last error check and
1855 * this one. We will try to clear the type field from
1856 * the notify entry. If we succeed then
1857 * xpc_disconnect_channel() didn't already process
1858 * the notify entry.
1859 */
1860 if (cmpxchg(&notify->type, notify_type, 0) ==
Dean Nelson35190502008-04-22 14:48:55 -05001861 notify_type) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001862 atomic_dec(&ch->n_to_notify);
1863 ret = ch->reason;
1864 }
1865
1866 /* drop the reference grabbed in xpc_allocate_msg() */
1867 xpc_msgqueue_deref(ch);
1868 return ret;
1869 }
1870 }
1871
1872 msg->flags |= XPC_M_READY;
1873
1874 /*
1875 * The preceding store of msg->flags must occur before the following
1876 * load of ch->local_GP->put.
1877 */
1878 mb();
1879
1880 /* see if the message is next in line to be sent, if so send it */
1881
1882 put = ch->local_GP->put;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001883 if (put == msg_number)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001884 xpc_send_msgs(ch, put);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001885
1886 /* drop the reference grabbed in xpc_allocate_msg() */
1887 xpc_msgqueue_deref(ch);
1888 return ret;
1889}
1890
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001891/*
1892 * Send a message previously allocated using xpc_initiate_allocate() on the
1893 * specified channel connected to the specified partition.
1894 *
1895 * This routine will not wait for the message to be received, nor will
1896 * notification be given when it does happen. Once this routine has returned
1897 * the message entry allocated via xpc_initiate_allocate() is no longer
1898 * accessable to the caller.
1899 *
1900 * This routine, although called by users, does not call xpc_part_ref() to
1901 * ensure that the partition infrastructure is in place. It relies on the
1902 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
1903 *
1904 * Arguments:
1905 *
1906 * partid - ID of partition to which the channel is connected.
1907 * ch_number - channel # to send message on.
1908 * payload - pointer to the payload area allocated via
1909 * xpc_initiate_allocate().
1910 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001911enum xp_retval
Dean Nelson64d032b2008-05-12 14:02:03 -07001912xpc_initiate_send(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001913{
1914 struct xpc_partition *part = &xpc_partitions[partid];
1915 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
Dean Nelson65c17b82008-05-12 14:02:02 -07001916 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001917
Dean Nelson35190502008-04-22 14:48:55 -05001918 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001919 partid, ch_number);
1920
1921 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
1922 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1923 DBUG_ON(msg == NULL);
1924
1925 ret = xpc_send_msg(&part->channels[ch_number], msg, 0, NULL, NULL);
1926
1927 return ret;
1928}
1929
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001930/*
1931 * Send a message previously allocated using xpc_initiate_allocate on the
1932 * specified channel connected to the specified partition.
1933 *
1934 * This routine will not wait for the message to be sent. Once this routine
1935 * has returned the message entry allocated via xpc_initiate_allocate() is no
1936 * longer accessable to the caller.
1937 *
1938 * Once the remote end of the channel has received the message, the function
1939 * passed as an argument to xpc_initiate_send_notify() will be called. This
1940 * allows the sender to free up or re-use any buffers referenced by the
1941 * message, but does NOT mean the message has been processed at the remote
1942 * end by a receiver.
1943 *
1944 * If this routine returns an error, the caller's function will NOT be called.
1945 *
1946 * This routine, although called by users, does not call xpc_part_ref() to
1947 * ensure that the partition infrastructure is in place. It relies on the
1948 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
1949 *
1950 * Arguments:
1951 *
1952 * partid - ID of partition to which the channel is connected.
1953 * ch_number - channel # to send message on.
1954 * payload - pointer to the payload area allocated via
1955 * xpc_initiate_allocate().
1956 * func - function to call with asynchronous notification of message
1957 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
1958 * key - user-defined key to be passed to the function when it's called.
1959 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001960enum xp_retval
Dean Nelson64d032b2008-05-12 14:02:03 -07001961xpc_initiate_send_notify(short partid, int ch_number, void *payload,
Dean Nelson35190502008-04-22 14:48:55 -05001962 xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001963{
1964 struct xpc_partition *part = &xpc_partitions[partid];
1965 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
Dean Nelson65c17b82008-05-12 14:02:02 -07001966 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001967
Dean Nelson35190502008-04-22 14:48:55 -05001968 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001969 partid, ch_number);
1970
1971 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
1972 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1973 DBUG_ON(msg == NULL);
1974 DBUG_ON(func == NULL);
1975
1976 ret = xpc_send_msg(&part->channels[ch_number], msg, XPC_N_CALL,
Dean Nelson35190502008-04-22 14:48:55 -05001977 func, key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001978 return ret;
1979}
1980
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001981static struct xpc_msg *
1982xpc_pull_remote_msg(struct xpc_channel *ch, s64 get)
1983{
1984 struct xpc_partition *part = &xpc_partitions[ch->partid];
1985 struct xpc_msg *remote_msg, *msg;
1986 u32 msg_index, nmsgs;
1987 u64 msg_offset;
Dean Nelson65c17b82008-05-12 14:02:02 -07001988 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001989
Jes Sorensenf9e505a2006-01-17 12:52:21 -05001990 if (mutex_lock_interruptible(&ch->msg_to_pull_mutex) != 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001991 /* we were interrupted by a signal */
1992 return NULL;
1993 }
1994
1995 while (get >= ch->next_msg_to_pull) {
1996
1997 /* pull as many messages as are ready and able to be pulled */
1998
1999 msg_index = ch->next_msg_to_pull % ch->remote_nentries;
2000
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002001 DBUG_ON(ch->next_msg_to_pull >= ch->w_remote_GP.put);
2002 nmsgs = ch->w_remote_GP.put - ch->next_msg_to_pull;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002003 if (msg_index + nmsgs > ch->remote_nentries) {
2004 /* ignore the ones that wrap the msg queue for now */
2005 nmsgs = ch->remote_nentries - msg_index;
2006 }
2007
2008 msg_offset = msg_index * ch->msg_size;
Dean Nelson35190502008-04-22 14:48:55 -05002009 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
2010 remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
2011 msg_offset);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002012
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002013 ret = xpc_pull_remote_cachelines(part, msg, remote_msg,
2014 nmsgs * ch->msg_size);
Dean Nelson65c17b82008-05-12 14:02:02 -07002015 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002016
2017 dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
2018 " msg %ld from partition %d, channel=%d, "
2019 "ret=%d\n", nmsgs, ch->next_msg_to_pull,
2020 ch->partid, ch->number, ret);
2021
2022 XPC_DEACTIVATE_PARTITION(part, ret);
2023
Jes Sorensenf9e505a2006-01-17 12:52:21 -05002024 mutex_unlock(&ch->msg_to_pull_mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002025 return NULL;
2026 }
2027
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002028 ch->next_msg_to_pull += nmsgs;
2029 }
2030
Jes Sorensenf9e505a2006-01-17 12:52:21 -05002031 mutex_unlock(&ch->msg_to_pull_mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002032
2033 /* return the message we were looking for */
2034 msg_offset = (get % ch->remote_nentries) * ch->msg_size;
Dean Nelson35190502008-04-22 14:48:55 -05002035 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002036
2037 return msg;
2038}
2039
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002040/*
2041 * Get a message to be delivered.
2042 */
2043static struct xpc_msg *
2044xpc_get_deliverable_msg(struct xpc_channel *ch)
2045{
2046 struct xpc_msg *msg = NULL;
2047 s64 get;
2048
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002049 do {
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002050 if (ch->flags & XPC_C_DISCONNECTING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002051 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002052
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002053 get = ch->w_local_GP.get;
2054 rmb(); /* guarantee that .get loads before .put */
2055 if (get == ch->w_remote_GP.put)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002056 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002057
2058 /* There are messages waiting to be pulled and delivered.
2059 * We need to try to secure one for ourselves. We'll do this
2060 * by trying to increment w_local_GP.get and hope that no one
2061 * else beats us to it. If they do, we'll we'll simply have
2062 * to try again for the next one.
Dean Nelson35190502008-04-22 14:48:55 -05002063 */
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002064
2065 if (cmpxchg(&ch->w_local_GP.get, get, get + 1) == get) {
2066 /* we got the entry referenced by get */
2067
2068 dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
2069 "partid=%d, channel=%d\n", get + 1,
2070 ch->partid, ch->number);
2071
2072 /* pull the message from the remote partition */
2073
2074 msg = xpc_pull_remote_msg(ch, get);
2075
2076 DBUG_ON(msg != NULL && msg->number != get);
2077 DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
2078 DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
2079
2080 break;
2081 }
2082
2083 } while (1);
2084
2085 return msg;
2086}
2087
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002088/*
2089 * Deliver a message to its intended recipient.
2090 */
2091void
2092xpc_deliver_msg(struct xpc_channel *ch)
2093{
2094 struct xpc_msg *msg;
2095
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002096 msg = xpc_get_deliverable_msg(ch);
2097 if (msg != NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002098
2099 /*
2100 * This ref is taken to protect the payload itself from being
2101 * freed before the user is finished with it, which the user
2102 * indicates by calling xpc_initiate_received().
2103 */
2104 xpc_msgqueue_ref(ch);
2105
2106 atomic_inc(&ch->kthreads_active);
2107
2108 if (ch->func != NULL) {
2109 dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
2110 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05002111 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002112 ch->number);
2113
2114 /* deliver the message to its intended recipient */
Dean Nelson65c17b82008-05-12 14:02:02 -07002115 ch->func(xpMsgReceived, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -05002116 &msg->payload, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002117
2118 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
2119 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05002120 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002121 ch->number);
2122 }
2123
2124 atomic_dec(&ch->kthreads_active);
2125 }
2126}
2127
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002128/*
2129 * Now we actually acknowledge the messages that have been delivered and ack'd
2130 * by advancing the cached remote message queue's Get value and if requested
2131 * send an IPI to the message sender's partition.
2132 */
2133static void
2134xpc_acknowledge_msgs(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2135{
2136 struct xpc_msg *msg;
2137 s64 get = initial_get + 1;
2138 int send_IPI = 0;
2139
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002140 while (1) {
2141
2142 while (1) {
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002143 if (get == ch->w_local_GP.get)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002144 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002145
Dean Nelson35190502008-04-22 14:48:55 -05002146 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
2147 (get % ch->remote_nentries) *
2148 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002149
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002150 if (!(msg->flags & XPC_M_DONE))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002151 break;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002152
2153 msg_flags |= msg->flags;
2154 get++;
2155 }
2156
2157 if (get == initial_get) {
2158 /* nothing's changed */
2159 break;
2160 }
2161
2162 if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) !=
Dean Nelson35190502008-04-22 14:48:55 -05002163 initial_get) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002164 /* someone else beat us to it */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002165 DBUG_ON(ch->local_GP->get <= initial_get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002166 break;
2167 }
2168
2169 /* we just set the new value of local_GP->get */
2170
2171 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2172 "channel=%d\n", get, ch->partid, ch->number);
2173
2174 send_IPI = (msg_flags & XPC_M_INTERRUPT);
2175
2176 /*
2177 * We need to ensure that the message referenced by
2178 * local_GP->get is not XPC_M_DONE or that local_GP->get
2179 * equals w_local_GP.get, so we'll go have a look.
2180 */
2181 initial_get = get;
2182 }
2183
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002184 if (send_IPI)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002185 xpc_IPI_send_msgrequest(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002186}
2187
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002188/*
2189 * Acknowledge receipt of a delivered message.
2190 *
2191 * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
2192 * that sent the message.
2193 *
2194 * This function, although called by users, does not call xpc_part_ref() to
2195 * ensure that the partition infrastructure is in place. It relies on the
2196 * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
2197 *
2198 * Arguments:
2199 *
2200 * partid - ID of partition to which the channel is connected.
2201 * ch_number - channel # message received on.
2202 * payload - pointer to the payload area allocated via
2203 * xpc_initiate_allocate().
2204 */
2205void
Dean Nelson64d032b2008-05-12 14:02:03 -07002206xpc_initiate_received(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002207{
2208 struct xpc_partition *part = &xpc_partitions[partid];
2209 struct xpc_channel *ch;
2210 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2211 s64 get, msg_number = msg->number;
2212
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002213 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2214 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2215
2216 ch = &part->channels[ch_number];
2217
2218 dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05002219 (void *)msg, msg_number, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002220
Dean Nelson35190502008-04-22 14:48:55 -05002221 DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
2222 msg_number % ch->remote_nentries);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002223 DBUG_ON(msg->flags & XPC_M_DONE);
2224
2225 msg->flags |= XPC_M_DONE;
2226
2227 /*
2228 * The preceding store of msg->flags must occur before the following
2229 * load of ch->local_GP->get.
2230 */
2231 mb();
2232
2233 /*
2234 * See if this message is next in line to be acknowledged as having
2235 * been delivered.
2236 */
2237 get = ch->local_GP->get;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05002238 if (get == msg_number)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002239 xpc_acknowledge_msgs(ch, get, msg->flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07002240
2241 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
2242 xpc_msgqueue_deref(ch);
2243}