Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
Dean Nelson | 45d9ca4 | 2008-04-22 14:46:56 -0500 | [diff] [blame] | 6 | * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved. |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 9 | /* |
| 10 | * Cross Partition Communication (XPC) channel support. |
| 11 | * |
| 12 | * This is the part of XPC that manages the channels and |
| 13 | * sends/receives messages across them to/from other partitions. |
| 14 | * |
| 15 | */ |
| 16 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/cache.h> |
| 21 | #include <linux/interrupt.h> |
Jes Sorensen | f9e505a | 2006-01-17 12:52:21 -0500 | [diff] [blame] | 22 | #include <linux/mutex.h> |
| 23 | #include <linux/completion.h> |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 24 | #include <asm/sn/sn_sal.h> |
Dean Nelson | 45d9ca4 | 2008-04-22 14:46:56 -0500 | [diff] [blame] | 25 | #include "xpc.h" |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 26 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 27 | /* |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 28 | * Guarantee that the kzalloc'd memory is cacheline aligned. |
| 29 | */ |
Dean Nelson | e17d416 | 2008-07-29 22:34:06 -0700 | [diff] [blame^] | 30 | void * |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 31 | xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base) |
| 32 | { |
| 33 | /* see if kzalloc will give us cachline aligned memory by default */ |
| 34 | *base = kzalloc(size, flags); |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 35 | if (*base == NULL) |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 36 | return NULL; |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 37 | |
| 38 | if ((u64)*base == L1_CACHE_ALIGN((u64)*base)) |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 39 | return *base; |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 40 | |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 41 | kfree(*base); |
| 42 | |
| 43 | /* nope, we'll have to do it ourselves */ |
| 44 | *base = kzalloc(size + L1_CACHE_BYTES, flags); |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 45 | if (*base == NULL) |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 46 | return NULL; |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 47 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 48 | return (void *)L1_CACHE_ALIGN((u64)*base); |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 51 | /* |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 52 | * Allocate the local message queue and the notify queue. |
| 53 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 54 | static enum xp_retval |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 55 | xpc_allocate_local_msgqueue(struct xpc_channel *ch) |
| 56 | { |
| 57 | unsigned long irq_flags; |
| 58 | int nentries; |
| 59 | size_t nbytes; |
| 60 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 61 | for (nentries = ch->local_nentries; nentries > 0; nentries--) { |
| 62 | |
| 63 | nbytes = nentries * ch->msg_size; |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 64 | ch->local_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 65 | GFP_KERNEL, |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 66 | &ch->local_msgqueue_base); |
| 67 | if (ch->local_msgqueue == NULL) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 68 | continue; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 69 | |
| 70 | nbytes = nentries * sizeof(struct xpc_notify); |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 71 | ch->notify_queue = kzalloc(nbytes, GFP_KERNEL); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 72 | if (ch->notify_queue == NULL) { |
| 73 | kfree(ch->local_msgqueue_base); |
| 74 | ch->local_msgqueue = NULL; |
| 75 | continue; |
| 76 | } |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 77 | |
| 78 | spin_lock_irqsave(&ch->lock, irq_flags); |
| 79 | if (nentries < ch->local_nentries) { |
| 80 | dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, " |
| 81 | "partid=%d, channel=%d\n", nentries, |
| 82 | ch->local_nentries, ch->partid, ch->number); |
| 83 | |
| 84 | ch->local_nentries = nentries; |
| 85 | } |
| 86 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 87 | return xpSuccess; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | dev_dbg(xpc_chan, "can't get memory for local message queue and notify " |
| 91 | "queue, partid=%d, channel=%d\n", ch->partid, ch->number); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 92 | return xpNoMemory; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 95 | /* |
| 96 | * Allocate the cached remote message queue. |
| 97 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 98 | static enum xp_retval |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 99 | xpc_allocate_remote_msgqueue(struct xpc_channel *ch) |
| 100 | { |
| 101 | unsigned long irq_flags; |
| 102 | int nentries; |
| 103 | size_t nbytes; |
| 104 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 105 | DBUG_ON(ch->remote_nentries <= 0); |
| 106 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 107 | for (nentries = ch->remote_nentries; nentries > 0; nentries--) { |
| 108 | |
| 109 | nbytes = nentries * ch->msg_size; |
Jes Sorensen | 7aa6ba4 | 2006-02-17 05:18:43 -0500 | [diff] [blame] | 110 | ch->remote_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 111 | GFP_KERNEL, |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 112 | &ch->remote_msgqueue_base); |
| 113 | if (ch->remote_msgqueue == NULL) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 114 | continue; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 115 | |
| 116 | spin_lock_irqsave(&ch->lock, irq_flags); |
| 117 | if (nentries < ch->remote_nentries) { |
| 118 | dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, " |
| 119 | "partid=%d, channel=%d\n", nentries, |
| 120 | ch->remote_nentries, ch->partid, ch->number); |
| 121 | |
| 122 | ch->remote_nentries = nentries; |
| 123 | } |
| 124 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 125 | return xpSuccess; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | dev_dbg(xpc_chan, "can't get memory for cached remote message queue, " |
| 129 | "partid=%d, channel=%d\n", ch->partid, ch->number); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 130 | return xpNoMemory; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 133 | /* |
| 134 | * Allocate message queues and other stuff associated with a channel. |
| 135 | * |
| 136 | * Note: Assumes all of the channel sizes are filled in. |
| 137 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 138 | static enum xp_retval |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 139 | xpc_allocate_msgqueues(struct xpc_channel *ch) |
| 140 | { |
| 141 | unsigned long irq_flags; |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 142 | enum xp_retval ret; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 143 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 144 | DBUG_ON(ch->flags & XPC_C_SETUP); |
| 145 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 146 | ret = xpc_allocate_local_msgqueue(ch); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 147 | if (ret != xpSuccess) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 148 | return ret; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 149 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 150 | ret = xpc_allocate_remote_msgqueue(ch); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 151 | if (ret != xpSuccess) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 152 | kfree(ch->local_msgqueue_base); |
| 153 | ch->local_msgqueue = NULL; |
| 154 | kfree(ch->notify_queue); |
| 155 | ch->notify_queue = NULL; |
| 156 | return ret; |
| 157 | } |
| 158 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 159 | spin_lock_irqsave(&ch->lock, irq_flags); |
| 160 | ch->flags |= XPC_C_SETUP; |
| 161 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 162 | |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 163 | return xpSuccess; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 166 | /* |
| 167 | * Process a connect message from a remote partition. |
| 168 | * |
| 169 | * Note: xpc_process_connect() is expecting to be called with the |
| 170 | * spin_lock_irqsave held and will leave it locked upon return. |
| 171 | */ |
| 172 | static void |
| 173 | xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags) |
| 174 | { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 175 | enum xp_retval ret; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 176 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 177 | DBUG_ON(!spin_is_locked(&ch->lock)); |
| 178 | |
| 179 | if (!(ch->flags & XPC_C_OPENREQUEST) || |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 180 | !(ch->flags & XPC_C_ROPENREQUEST)) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 181 | /* nothing more to do for now */ |
| 182 | return; |
| 183 | } |
| 184 | DBUG_ON(!(ch->flags & XPC_C_CONNECTING)); |
| 185 | |
| 186 | if (!(ch->flags & XPC_C_SETUP)) { |
| 187 | spin_unlock_irqrestore(&ch->lock, *irq_flags); |
| 188 | ret = xpc_allocate_msgqueues(ch); |
| 189 | spin_lock_irqsave(&ch->lock, *irq_flags); |
| 190 | |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 191 | if (ret != xpSuccess) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 192 | XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags); |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 193 | |
| 194 | if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 195 | return; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 196 | |
| 197 | DBUG_ON(!(ch->flags & XPC_C_SETUP)); |
| 198 | DBUG_ON(ch->local_msgqueue == NULL); |
| 199 | DBUG_ON(ch->remote_msgqueue == NULL); |
| 200 | } |
| 201 | |
| 202 | if (!(ch->flags & XPC_C_OPENREPLY)) { |
| 203 | ch->flags |= XPC_C_OPENREPLY; |
| 204 | xpc_IPI_send_openreply(ch, irq_flags); |
| 205 | } |
| 206 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 207 | if (!(ch->flags & XPC_C_ROPENREPLY)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 208 | return; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 209 | |
| 210 | DBUG_ON(ch->remote_msgqueue_pa == 0); |
| 211 | |
| 212 | ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */ |
| 213 | |
| 214 | dev_info(xpc_chan, "channel %d to partition %d connected\n", |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 215 | ch->number, ch->partid); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 216 | |
| 217 | spin_unlock_irqrestore(&ch->lock, *irq_flags); |
Dean Nelson | a460ef8 | 2006-11-22 08:25:00 -0600 | [diff] [blame] | 218 | xpc_create_kthreads(ch, 1, 0); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 219 | spin_lock_irqsave(&ch->lock, *irq_flags); |
| 220 | } |
| 221 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 222 | /* |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 223 | * Notify those who wanted to be notified upon delivery of their message. |
| 224 | */ |
| 225 | static void |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 226 | xpc_notify_senders(struct xpc_channel *ch, enum xp_retval reason, s64 put) |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 227 | { |
| 228 | struct xpc_notify *notify; |
| 229 | u8 notify_type; |
| 230 | s64 get = ch->w_remote_GP.get - 1; |
| 231 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 232 | while (++get < put && atomic_read(&ch->n_to_notify) > 0) { |
| 233 | |
| 234 | notify = &ch->notify_queue[get % ch->local_nentries]; |
| 235 | |
| 236 | /* |
| 237 | * See if the notify entry indicates it was associated with |
| 238 | * a message who's sender wants to be notified. It is possible |
| 239 | * that it is, but someone else is doing or has done the |
| 240 | * notification. |
| 241 | */ |
| 242 | notify_type = notify->type; |
| 243 | if (notify_type == 0 || |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 244 | cmpxchg(¬ify->type, notify_type, 0) != notify_type) { |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 245 | continue; |
| 246 | } |
| 247 | |
| 248 | DBUG_ON(notify_type != XPC_N_CALL); |
| 249 | |
| 250 | atomic_dec(&ch->n_to_notify); |
| 251 | |
| 252 | if (notify->func != NULL) { |
| 253 | dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, " |
| 254 | "msg_number=%ld, partid=%d, channel=%d\n", |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 255 | (void *)notify, get, ch->partid, ch->number); |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 256 | |
| 257 | notify->func(reason, ch->partid, ch->number, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 258 | notify->key); |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 259 | |
| 260 | dev_dbg(xpc_chan, "notify->func() returned, " |
| 261 | "notify=0x%p, msg_number=%ld, partid=%d, " |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 262 | "channel=%d\n", (void *)notify, get, |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 263 | ch->partid, ch->number); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 268 | /* |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 269 | * Free up message queues and other stuff that were allocated for the specified |
| 270 | * channel. |
| 271 | * |
| 272 | * Note: ch->reason and ch->reason_line are left set for debugging purposes, |
| 273 | * they're cleared when XPC_C_DISCONNECTED is cleared. |
| 274 | */ |
| 275 | static void |
| 276 | xpc_free_msgqueues(struct xpc_channel *ch) |
| 277 | { |
| 278 | DBUG_ON(!spin_is_locked(&ch->lock)); |
| 279 | DBUG_ON(atomic_read(&ch->n_to_notify) != 0); |
| 280 | |
| 281 | ch->remote_msgqueue_pa = 0; |
| 282 | ch->func = NULL; |
| 283 | ch->key = NULL; |
| 284 | ch->msg_size = 0; |
| 285 | ch->local_nentries = 0; |
| 286 | ch->remote_nentries = 0; |
| 287 | ch->kthreads_assigned_limit = 0; |
| 288 | ch->kthreads_idle_limit = 0; |
| 289 | |
| 290 | ch->local_GP->get = 0; |
| 291 | ch->local_GP->put = 0; |
| 292 | ch->remote_GP.get = 0; |
| 293 | ch->remote_GP.put = 0; |
| 294 | ch->w_local_GP.get = 0; |
| 295 | ch->w_local_GP.put = 0; |
| 296 | ch->w_remote_GP.get = 0; |
| 297 | ch->w_remote_GP.put = 0; |
| 298 | ch->next_msg_to_pull = 0; |
| 299 | |
| 300 | if (ch->flags & XPC_C_SETUP) { |
| 301 | ch->flags &= ~XPC_C_SETUP; |
| 302 | |
| 303 | dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n", |
| 304 | ch->flags, ch->partid, ch->number); |
| 305 | |
| 306 | kfree(ch->local_msgqueue_base); |
| 307 | ch->local_msgqueue = NULL; |
| 308 | kfree(ch->remote_msgqueue_base); |
| 309 | ch->remote_msgqueue = NULL; |
| 310 | kfree(ch->notify_queue); |
| 311 | ch->notify_queue = NULL; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 315 | /* |
| 316 | * spin_lock_irqsave() is expected to be held on entry. |
| 317 | */ |
| 318 | static void |
| 319 | xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags) |
| 320 | { |
| 321 | struct xpc_partition *part = &xpc_partitions[ch->partid]; |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 322 | u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 323 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 324 | DBUG_ON(!spin_is_locked(&ch->lock)); |
| 325 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 326 | if (!(ch->flags & XPC_C_DISCONNECTING)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 327 | return; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 328 | |
| 329 | DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST)); |
| 330 | |
| 331 | /* make sure all activity has settled down first */ |
| 332 | |
Dean Nelson | a460ef8 | 2006-11-22 08:25:00 -0600 | [diff] [blame] | 333 | if (atomic_read(&ch->kthreads_assigned) > 0 || |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 334 | atomic_read(&ch->references) > 0) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 335 | return; |
| 336 | } |
Dean Nelson | a460ef8 | 2006-11-22 08:25:00 -0600 | [diff] [blame] | 337 | DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) && |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 338 | !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE)); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 339 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 340 | if (part->act_state == XPC_P_DEACTIVATING) { |
| 341 | /* can't proceed until the other side disengages from us */ |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 342 | if (xpc_partition_engaged(1UL << ch->partid)) |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 343 | return; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 344 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 345 | } else { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 346 | |
| 347 | /* as long as the other side is up do the full protocol */ |
| 348 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 349 | if (!(ch->flags & XPC_C_RCLOSEREQUEST)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 350 | return; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 351 | |
| 352 | if (!(ch->flags & XPC_C_CLOSEREPLY)) { |
| 353 | ch->flags |= XPC_C_CLOSEREPLY; |
| 354 | xpc_IPI_send_closereply(ch, irq_flags); |
| 355 | } |
| 356 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 357 | if (!(ch->flags & XPC_C_RCLOSEREPLY)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 358 | return; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 361 | /* wake those waiting for notify completion */ |
| 362 | if (atomic_read(&ch->n_to_notify) > 0) { |
| 363 | /* >>> we do callout while holding ch->lock */ |
| 364 | xpc_notify_senders(ch, ch->reason, ch->w_local_GP.put); |
| 365 | } |
| 366 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 367 | /* both sides are disconnected now */ |
| 368 | |
Dean Nelson | 4c2cd96 | 2006-02-15 08:02:21 -0600 | [diff] [blame] | 369 | if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) { |
Dean Nelson | 246c7e3 | 2005-12-22 14:32:56 -0600 | [diff] [blame] | 370 | spin_unlock_irqrestore(&ch->lock, *irq_flags); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 371 | xpc_disconnect_callout(ch, xpDisconnected); |
Dean Nelson | 246c7e3 | 2005-12-22 14:32:56 -0600 | [diff] [blame] | 372 | spin_lock_irqsave(&ch->lock, *irq_flags); |
| 373 | } |
| 374 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 375 | /* it's now safe to free the channel's message queues */ |
| 376 | xpc_free_msgqueues(ch); |
| 377 | |
| 378 | /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */ |
| 379 | ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT)); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 380 | |
| 381 | atomic_dec(&part->nchannels_active); |
| 382 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 383 | if (channel_was_connected) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 384 | dev_info(xpc_chan, "channel %d to partition %d disconnected, " |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 385 | "reason=%d\n", ch->number, ch->partid, ch->reason); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 386 | } |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 387 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 388 | if (ch->flags & XPC_C_WDISCONNECT) { |
Jes Sorensen | f9e505a | 2006-01-17 12:52:21 -0500 | [diff] [blame] | 389 | /* we won't lose the CPU since we're holding ch->lock */ |
| 390 | complete(&ch->wdisconnect_wait); |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 391 | } else if (ch->delayed_IPI_flags) { |
| 392 | if (part->act_state != XPC_P_DEACTIVATING) { |
| 393 | /* time to take action on any delayed IPI flags */ |
| 394 | spin_lock(&part->IPI_lock); |
| 395 | XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 396 | ch->delayed_IPI_flags); |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 397 | spin_unlock(&part->IPI_lock); |
| 398 | } |
| 399 | ch->delayed_IPI_flags = 0; |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 400 | } |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 403 | /* |
| 404 | * Process a change in the channel's remote connection state. |
| 405 | */ |
| 406 | static void |
| 407 | xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 408 | u8 IPI_flags) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 409 | { |
| 410 | unsigned long irq_flags; |
| 411 | struct xpc_openclose_args *args = |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 412 | &part->remote_openclose_args[ch_number]; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 413 | struct xpc_channel *ch = &part->channels[ch_number]; |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 414 | enum xp_retval reason; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 415 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 416 | spin_lock_irqsave(&ch->lock, irq_flags); |
| 417 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 418 | again: |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 419 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 420 | if ((ch->flags & XPC_C_DISCONNECTED) && |
| 421 | (ch->flags & XPC_C_WDISCONNECT)) { |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 422 | /* |
| 423 | * Delay processing IPI flags until thread waiting disconnect |
| 424 | * has had a chance to see that the channel is disconnected. |
| 425 | */ |
| 426 | ch->delayed_IPI_flags |= IPI_flags; |
| 427 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 428 | return; |
| 429 | } |
| 430 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 431 | if (IPI_flags & XPC_IPI_CLOSEREQUEST) { |
| 432 | |
| 433 | dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received " |
| 434 | "from partid=%d, channel=%d\n", args->reason, |
| 435 | ch->partid, ch->number); |
| 436 | |
| 437 | /* |
| 438 | * If RCLOSEREQUEST is set, we're probably waiting for |
| 439 | * RCLOSEREPLY. We should find it and a ROPENREQUEST packed |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 440 | * with this RCLOSEREQUEST in the IPI_flags. |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 441 | */ |
| 442 | |
| 443 | if (ch->flags & XPC_C_RCLOSEREQUEST) { |
| 444 | DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING)); |
| 445 | DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST)); |
| 446 | DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY)); |
| 447 | DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY); |
| 448 | |
| 449 | DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY)); |
| 450 | IPI_flags &= ~XPC_IPI_CLOSEREPLY; |
| 451 | ch->flags |= XPC_C_RCLOSEREPLY; |
| 452 | |
| 453 | /* both sides have finished disconnecting */ |
| 454 | xpc_process_disconnect(ch, &irq_flags); |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 455 | DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED)); |
| 456 | goto again; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | if (ch->flags & XPC_C_DISCONNECTED) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 460 | if (!(IPI_flags & XPC_IPI_OPENREQUEST)) { |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 461 | if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 462 | ch_number) & |
| 463 | XPC_IPI_OPENREQUEST)) { |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 464 | |
| 465 | DBUG_ON(ch->delayed_IPI_flags != 0); |
| 466 | spin_lock(&part->IPI_lock); |
| 467 | XPC_SET_IPI_FLAGS(part->local_IPI_amo, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 468 | ch_number, |
| 469 | XPC_IPI_CLOSEREQUEST); |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 470 | spin_unlock(&part->IPI_lock); |
| 471 | } |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 472 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | XPC_SET_REASON(ch, 0, 0); |
| 477 | ch->flags &= ~XPC_C_DISCONNECTED; |
| 478 | |
| 479 | atomic_inc(&part->nchannels_active); |
| 480 | ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST); |
| 481 | } |
| 482 | |
| 483 | IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY); |
| 484 | |
| 485 | /* |
| 486 | * The meaningful CLOSEREQUEST connection state fields are: |
| 487 | * reason = reason connection is to be closed |
| 488 | */ |
| 489 | |
| 490 | ch->flags |= XPC_C_RCLOSEREQUEST; |
| 491 | |
| 492 | if (!(ch->flags & XPC_C_DISCONNECTING)) { |
| 493 | reason = args->reason; |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 494 | if (reason <= xpSuccess || reason > xpUnknownReason) |
| 495 | reason = xpUnknownReason; |
| 496 | else if (reason == xpUnregistering) |
| 497 | reason = xpOtherUnregistering; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 498 | |
| 499 | XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags); |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 500 | |
| 501 | DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY); |
| 502 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 503 | return; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 504 | } |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 505 | |
| 506 | xpc_process_disconnect(ch, &irq_flags); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 509 | if (IPI_flags & XPC_IPI_CLOSEREPLY) { |
| 510 | |
| 511 | dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d," |
| 512 | " channel=%d\n", ch->partid, ch->number); |
| 513 | |
| 514 | if (ch->flags & XPC_C_DISCONNECTED) { |
| 515 | DBUG_ON(part->act_state != XPC_P_DEACTIVATING); |
| 516 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST)); |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 521 | |
| 522 | if (!(ch->flags & XPC_C_RCLOSEREQUEST)) { |
| 523 | if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number) |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 524 | & XPC_IPI_CLOSEREQUEST)) { |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 525 | |
| 526 | DBUG_ON(ch->delayed_IPI_flags != 0); |
| 527 | spin_lock(&part->IPI_lock); |
| 528 | XPC_SET_IPI_FLAGS(part->local_IPI_amo, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 529 | ch_number, |
| 530 | XPC_IPI_CLOSEREPLY); |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 531 | spin_unlock(&part->IPI_lock); |
| 532 | } |
| 533 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 534 | return; |
| 535 | } |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 536 | |
| 537 | ch->flags |= XPC_C_RCLOSEREPLY; |
| 538 | |
| 539 | if (ch->flags & XPC_C_CLOSEREPLY) { |
| 540 | /* both sides have finished disconnecting */ |
| 541 | xpc_process_disconnect(ch, &irq_flags); |
| 542 | } |
| 543 | } |
| 544 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 545 | if (IPI_flags & XPC_IPI_OPENREQUEST) { |
| 546 | |
| 547 | dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, " |
| 548 | "local_nentries=%d) received from partid=%d, " |
| 549 | "channel=%d\n", args->msg_size, args->local_nentries, |
| 550 | ch->partid, ch->number); |
| 551 | |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 552 | if (part->act_state == XPC_P_DEACTIVATING || |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 553 | (ch->flags & XPC_C_ROPENREQUEST)) { |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 554 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) { |
| 559 | ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 560 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 561 | return; |
| 562 | } |
| 563 | DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 564 | XPC_C_OPENREQUEST))); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 565 | DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 566 | XPC_C_OPENREPLY | XPC_C_CONNECTED)); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * The meaningful OPENREQUEST connection state fields are: |
| 570 | * msg_size = size of channel's messages in bytes |
| 571 | * local_nentries = remote partition's local_nentries |
| 572 | */ |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 573 | if (args->msg_size == 0 || args->local_nentries == 0) { |
| 574 | /* assume OPENREQUEST was delayed by mistake */ |
| 575 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 576 | return; |
| 577 | } |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 578 | |
| 579 | ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING); |
| 580 | ch->remote_nentries = args->local_nentries; |
| 581 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 582 | if (ch->flags & XPC_C_OPENREQUEST) { |
| 583 | if (args->msg_size != ch->msg_size) { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 584 | XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 585 | &irq_flags); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 586 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 587 | return; |
| 588 | } |
| 589 | } else { |
| 590 | ch->msg_size = args->msg_size; |
| 591 | |
| 592 | XPC_SET_REASON(ch, 0, 0); |
| 593 | ch->flags &= ~XPC_C_DISCONNECTED; |
| 594 | |
| 595 | atomic_inc(&part->nchannels_active); |
| 596 | } |
| 597 | |
| 598 | xpc_process_connect(ch, &irq_flags); |
| 599 | } |
| 600 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 601 | if (IPI_flags & XPC_IPI_OPENREPLY) { |
| 602 | |
| 603 | dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, " |
| 604 | "local_nentries=%d, remote_nentries=%d) received from " |
| 605 | "partid=%d, channel=%d\n", args->local_msgqueue_pa, |
| 606 | args->local_nentries, args->remote_nentries, |
| 607 | ch->partid, ch->number); |
| 608 | |
| 609 | if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) { |
| 610 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 611 | return; |
| 612 | } |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 613 | if (!(ch->flags & XPC_C_OPENREQUEST)) { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 614 | XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 615 | &irq_flags); |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 616 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 617 | return; |
| 618 | } |
| 619 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 620 | DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST)); |
| 621 | DBUG_ON(ch->flags & XPC_C_CONNECTED); |
| 622 | |
| 623 | /* |
| 624 | * The meaningful OPENREPLY connection state fields are: |
| 625 | * local_msgqueue_pa = physical address of remote |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 626 | * partition's local_msgqueue |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 627 | * local_nentries = remote partition's local_nentries |
| 628 | * remote_nentries = remote partition's remote_nentries |
| 629 | */ |
| 630 | DBUG_ON(args->local_msgqueue_pa == 0); |
| 631 | DBUG_ON(args->local_nentries == 0); |
| 632 | DBUG_ON(args->remote_nentries == 0); |
| 633 | |
| 634 | ch->flags |= XPC_C_ROPENREPLY; |
| 635 | ch->remote_msgqueue_pa = args->local_msgqueue_pa; |
| 636 | |
| 637 | if (args->local_nentries < ch->remote_nentries) { |
| 638 | dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new " |
| 639 | "remote_nentries=%d, old remote_nentries=%d, " |
| 640 | "partid=%d, channel=%d\n", |
| 641 | args->local_nentries, ch->remote_nentries, |
| 642 | ch->partid, ch->number); |
| 643 | |
| 644 | ch->remote_nentries = args->local_nentries; |
| 645 | } |
| 646 | if (args->remote_nentries < ch->local_nentries) { |
| 647 | dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new " |
| 648 | "local_nentries=%d, old local_nentries=%d, " |
| 649 | "partid=%d, channel=%d\n", |
| 650 | args->remote_nentries, ch->local_nentries, |
| 651 | ch->partid, ch->number); |
| 652 | |
| 653 | ch->local_nentries = args->remote_nentries; |
| 654 | } |
| 655 | |
| 656 | xpc_process_connect(ch, &irq_flags); |
| 657 | } |
| 658 | |
| 659 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 660 | } |
| 661 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 662 | /* |
| 663 | * Attempt to establish a channel connection to a remote partition. |
| 664 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 665 | static enum xp_retval |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 666 | xpc_connect_channel(struct xpc_channel *ch) |
| 667 | { |
| 668 | unsigned long irq_flags; |
| 669 | struct xpc_registration *registration = &xpc_registrations[ch->number]; |
| 670 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 671 | if (mutex_trylock(®istration->mutex) == 0) |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 672 | return xpRetry; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 673 | |
| 674 | if (!XPC_CHANNEL_REGISTERED(ch->number)) { |
Jes Sorensen | f9e505a | 2006-01-17 12:52:21 -0500 | [diff] [blame] | 675 | mutex_unlock(®istration->mutex); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 676 | return xpUnregistered; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | spin_lock_irqsave(&ch->lock, irq_flags); |
| 680 | |
| 681 | DBUG_ON(ch->flags & XPC_C_CONNECTED); |
| 682 | DBUG_ON(ch->flags & XPC_C_OPENREQUEST); |
| 683 | |
| 684 | if (ch->flags & XPC_C_DISCONNECTING) { |
| 685 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
Jes Sorensen | f9e505a | 2006-01-17 12:52:21 -0500 | [diff] [blame] | 686 | mutex_unlock(®istration->mutex); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 687 | return ch->reason; |
| 688 | } |
| 689 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 690 | /* add info from the channel connect registration to the channel */ |
| 691 | |
| 692 | ch->kthreads_assigned_limit = registration->assigned_limit; |
| 693 | ch->kthreads_idle_limit = registration->idle_limit; |
| 694 | DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0); |
| 695 | DBUG_ON(atomic_read(&ch->kthreads_idle) != 0); |
| 696 | DBUG_ON(atomic_read(&ch->kthreads_active) != 0); |
| 697 | |
| 698 | ch->func = registration->func; |
| 699 | DBUG_ON(registration->func == NULL); |
| 700 | ch->key = registration->key; |
| 701 | |
| 702 | ch->local_nentries = registration->nentries; |
| 703 | |
| 704 | if (ch->flags & XPC_C_ROPENREQUEST) { |
| 705 | if (registration->msg_size != ch->msg_size) { |
| 706 | /* the local and remote sides aren't the same */ |
| 707 | |
| 708 | /* |
| 709 | * Because XPC_DISCONNECT_CHANNEL() can block we're |
| 710 | * forced to up the registration sema before we unlock |
| 711 | * the channel lock. But that's okay here because we're |
| 712 | * done with the part that required the registration |
| 713 | * sema. XPC_DISCONNECT_CHANNEL() requires that the |
| 714 | * channel lock be locked and will unlock and relock |
| 715 | * the channel lock as needed. |
| 716 | */ |
Jes Sorensen | f9e505a | 2006-01-17 12:52:21 -0500 | [diff] [blame] | 717 | mutex_unlock(®istration->mutex); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 718 | XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 719 | &irq_flags); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 720 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 721 | return xpUnequalMsgSizes; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 722 | } |
| 723 | } else { |
| 724 | ch->msg_size = registration->msg_size; |
| 725 | |
| 726 | XPC_SET_REASON(ch, 0, 0); |
| 727 | ch->flags &= ~XPC_C_DISCONNECTED; |
| 728 | |
| 729 | atomic_inc(&xpc_partitions[ch->partid].nchannels_active); |
| 730 | } |
| 731 | |
Jes Sorensen | f9e505a | 2006-01-17 12:52:21 -0500 | [diff] [blame] | 732 | mutex_unlock(®istration->mutex); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 733 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 734 | /* initiate the connection */ |
| 735 | |
| 736 | ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING); |
| 737 | xpc_IPI_send_openrequest(ch, &irq_flags); |
| 738 | |
| 739 | xpc_process_connect(ch, &irq_flags); |
| 740 | |
| 741 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 742 | |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 743 | return xpSuccess; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 746 | /* |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 747 | * Clear some of the msg flags in the local message queue. |
| 748 | */ |
| 749 | static inline void |
| 750 | xpc_clear_local_msgqueue_flags(struct xpc_channel *ch) |
| 751 | { |
| 752 | struct xpc_msg *msg; |
| 753 | s64 get; |
| 754 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 755 | get = ch->w_remote_GP.get; |
| 756 | do { |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 757 | msg = (struct xpc_msg *)((u64)ch->local_msgqueue + |
| 758 | (get % ch->local_nentries) * |
| 759 | ch->msg_size); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 760 | msg->flags = 0; |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 761 | } while (++get < ch->remote_GP.get); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 764 | /* |
| 765 | * Clear some of the msg flags in the remote message queue. |
| 766 | */ |
| 767 | static inline void |
| 768 | xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch) |
| 769 | { |
| 770 | struct xpc_msg *msg; |
| 771 | s64 put; |
| 772 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 773 | put = ch->w_remote_GP.put; |
| 774 | do { |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 775 | msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + |
| 776 | (put % ch->remote_nentries) * |
| 777 | ch->msg_size); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 778 | msg->flags = 0; |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 779 | } while (++put < ch->remote_GP.put); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 782 | static void |
| 783 | xpc_process_msg_IPI(struct xpc_partition *part, int ch_number) |
| 784 | { |
| 785 | struct xpc_channel *ch = &part->channels[ch_number]; |
| 786 | int nmsgs_sent; |
| 787 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 788 | ch->remote_GP = part->remote_GPs[ch_number]; |
| 789 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 790 | /* See what, if anything, has changed for each connected channel */ |
| 791 | |
| 792 | xpc_msgqueue_ref(ch); |
| 793 | |
| 794 | if (ch->w_remote_GP.get == ch->remote_GP.get && |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 795 | ch->w_remote_GP.put == ch->remote_GP.put) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 796 | /* nothing changed since GPs were last pulled */ |
| 797 | xpc_msgqueue_deref(ch); |
| 798 | return; |
| 799 | } |
| 800 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 801 | if (!(ch->flags & XPC_C_CONNECTED)) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 802 | xpc_msgqueue_deref(ch); |
| 803 | return; |
| 804 | } |
| 805 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 806 | /* |
| 807 | * First check to see if messages recently sent by us have been |
| 808 | * received by the other side. (The remote GET value will have |
| 809 | * changed since we last looked at it.) |
| 810 | */ |
| 811 | |
| 812 | if (ch->w_remote_GP.get != ch->remote_GP.get) { |
| 813 | |
| 814 | /* |
| 815 | * We need to notify any senders that want to be notified |
| 816 | * that their sent messages have been received by their |
| 817 | * intended recipients. We need to do this before updating |
| 818 | * w_remote_GP.get so that we don't allocate the same message |
| 819 | * queue entries prematurely (see xpc_allocate_msg()). |
| 820 | */ |
| 821 | if (atomic_read(&ch->n_to_notify) > 0) { |
| 822 | /* |
| 823 | * Notify senders that messages sent have been |
| 824 | * received and delivered by the other side. |
| 825 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 826 | xpc_notify_senders(ch, xpMsgDelivered, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 827 | ch->remote_GP.get); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Clear msg->flags in previously sent messages, so that |
| 832 | * they're ready for xpc_allocate_msg(). |
| 833 | */ |
| 834 | xpc_clear_local_msgqueue_flags(ch); |
| 835 | |
Dave Jones | 821fe94 | 2005-06-25 14:54:29 -0700 | [diff] [blame] | 836 | ch->w_remote_GP.get = ch->remote_GP.get; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 837 | |
| 838 | dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, " |
| 839 | "channel=%d\n", ch->w_remote_GP.get, ch->partid, |
| 840 | ch->number); |
| 841 | |
| 842 | /* |
| 843 | * If anyone was waiting for message queue entries to become |
| 844 | * available, wake them up. |
| 845 | */ |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 846 | if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 847 | wake_up(&ch->msg_allocate_wq); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 850 | /* |
| 851 | * Now check for newly sent messages by the other side. (The remote |
| 852 | * PUT value will have changed since we last looked at it.) |
| 853 | */ |
| 854 | |
| 855 | if (ch->w_remote_GP.put != ch->remote_GP.put) { |
| 856 | /* |
| 857 | * Clear msg->flags in previously received messages, so that |
| 858 | * they're ready for xpc_get_deliverable_msg(). |
| 859 | */ |
| 860 | xpc_clear_remote_msgqueue_flags(ch); |
| 861 | |
Dave Jones | 821fe94 | 2005-06-25 14:54:29 -0700 | [diff] [blame] | 862 | ch->w_remote_GP.put = ch->remote_GP.put; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 863 | |
| 864 | dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, " |
| 865 | "channel=%d\n", ch->w_remote_GP.put, ch->partid, |
| 866 | ch->number); |
| 867 | |
| 868 | nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get; |
| 869 | if (nmsgs_sent > 0) { |
| 870 | dev_dbg(xpc_chan, "msgs waiting to be copied and " |
| 871 | "delivered=%d, partid=%d, channel=%d\n", |
| 872 | nmsgs_sent, ch->partid, ch->number); |
| 873 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 874 | if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 875 | xpc_activate_kthreads(ch, nmsgs_sent); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
| 879 | xpc_msgqueue_deref(ch); |
| 880 | } |
| 881 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 882 | void |
| 883 | xpc_process_channel_activity(struct xpc_partition *part) |
| 884 | { |
| 885 | unsigned long irq_flags; |
| 886 | u64 IPI_amo, IPI_flags; |
| 887 | struct xpc_channel *ch; |
| 888 | int ch_number; |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 889 | u32 ch_flags; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 890 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 891 | IPI_amo = xpc_get_IPI_flags(part); |
| 892 | |
| 893 | /* |
| 894 | * Initiate channel connections for registered channels. |
| 895 | * |
| 896 | * For each connected channel that has pending messages activate idle |
| 897 | * kthreads and/or create new kthreads as needed. |
| 898 | */ |
| 899 | |
| 900 | for (ch_number = 0; ch_number < part->nchannels; ch_number++) { |
| 901 | ch = &part->channels[ch_number]; |
| 902 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 903 | /* |
| 904 | * Process any open or close related IPI flags, and then deal |
| 905 | * with connecting or disconnecting the channel as required. |
| 906 | */ |
| 907 | |
| 908 | IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number); |
| 909 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 910 | if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 911 | xpc_process_openclose_IPI(part, ch_number, IPI_flags); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 912 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 913 | ch_flags = ch->flags; /* need an atomic snapshot of flags */ |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 914 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 915 | if (ch_flags & XPC_C_DISCONNECTING) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 916 | spin_lock_irqsave(&ch->lock, irq_flags); |
| 917 | xpc_process_disconnect(ch, &irq_flags); |
| 918 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 919 | continue; |
| 920 | } |
| 921 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 922 | if (part->act_state == XPC_P_DEACTIVATING) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 923 | continue; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 924 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 925 | if (!(ch_flags & XPC_C_CONNECTED)) { |
| 926 | if (!(ch_flags & XPC_C_OPENREQUEST)) { |
| 927 | DBUG_ON(ch_flags & XPC_C_SETUP); |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 928 | (void)xpc_connect_channel(ch); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 929 | } else { |
| 930 | spin_lock_irqsave(&ch->lock, irq_flags); |
| 931 | xpc_process_connect(ch, &irq_flags); |
| 932 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 933 | } |
| 934 | continue; |
| 935 | } |
| 936 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 937 | /* |
| 938 | * Process any message related IPI flags, this may involve the |
| 939 | * activation of kthreads to deliver any pending messages sent |
| 940 | * from the other partition. |
| 941 | */ |
| 942 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 943 | if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 944 | xpc_process_msg_IPI(part, ch_number); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 948 | /* |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 949 | * XPC's heartbeat code calls this function to inform XPC that a partition is |
| 950 | * going down. XPC responds by tearing down the XPartition Communication |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 951 | * infrastructure used for the just downed partition. |
| 952 | * |
| 953 | * XPC's heartbeat code will never call this function and xpc_partition_up() |
| 954 | * at the same time. Nor will it ever make multiple calls to either function |
| 955 | * at the same time. |
| 956 | */ |
| 957 | void |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 958 | xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 959 | { |
| 960 | unsigned long irq_flags; |
| 961 | int ch_number; |
| 962 | struct xpc_channel *ch; |
| 963 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 964 | dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n", |
| 965 | XPC_PARTID(part), reason); |
| 966 | |
| 967 | if (!xpc_part_ref(part)) { |
| 968 | /* infrastructure for this partition isn't currently set up */ |
| 969 | return; |
| 970 | } |
| 971 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 972 | /* disconnect channels associated with the partition going down */ |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 973 | |
| 974 | for (ch_number = 0; ch_number < part->nchannels; ch_number++) { |
| 975 | ch = &part->channels[ch_number]; |
| 976 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 977 | xpc_msgqueue_ref(ch); |
| 978 | spin_lock_irqsave(&ch->lock, irq_flags); |
| 979 | |
| 980 | XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags); |
| 981 | |
| 982 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 983 | xpc_msgqueue_deref(ch); |
| 984 | } |
| 985 | |
| 986 | xpc_wakeup_channel_mgr(part); |
| 987 | |
| 988 | xpc_part_deref(part); |
| 989 | } |
| 990 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 991 | /* |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 992 | * Called by XP at the time of channel connection registration to cause |
| 993 | * XPC to establish connections to all currently active partitions. |
| 994 | */ |
| 995 | void |
| 996 | xpc_initiate_connect(int ch_number) |
| 997 | { |
Dean Nelson | 64d032b | 2008-05-12 14:02:03 -0700 | [diff] [blame] | 998 | short partid; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 999 | struct xpc_partition *part; |
| 1000 | struct xpc_channel *ch; |
| 1001 | |
Dean Nelson | bc63d38 | 2008-07-29 22:34:04 -0700 | [diff] [blame] | 1002 | DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1003 | |
Dean Nelson | bc63d38 | 2008-07-29 22:34:04 -0700 | [diff] [blame] | 1004 | for (partid = 0; partid < xp_max_npartitions; partid++) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1005 | part = &xpc_partitions[partid]; |
| 1006 | |
| 1007 | if (xpc_part_ref(part)) { |
| 1008 | ch = &part->channels[ch_number]; |
| 1009 | |
Dean Nelson | e54af72 | 2005-10-25 14:07:43 -0500 | [diff] [blame] | 1010 | /* |
| 1011 | * Initiate the establishment of a connection on the |
| 1012 | * newly registered channel to the remote partition. |
| 1013 | */ |
| 1014 | xpc_wakeup_channel_mgr(part); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1015 | xpc_part_deref(part); |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1020 | void |
| 1021 | xpc_connected_callout(struct xpc_channel *ch) |
| 1022 | { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1023 | /* let the registerer know that a connection has been established */ |
| 1024 | |
| 1025 | if (ch->func != NULL) { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1026 | dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, " |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1027 | "partid=%d, channel=%d\n", ch->partid, ch->number); |
| 1028 | |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1029 | ch->func(xpConnected, ch->partid, ch->number, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1030 | (void *)(u64)ch->local_nentries, ch->key); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1031 | |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1032 | dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, " |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1033 | "partid=%d, channel=%d\n", ch->partid, ch->number); |
| 1034 | } |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1037 | /* |
| 1038 | * Called by XP at the time of channel connection unregistration to cause |
| 1039 | * XPC to teardown all current connections for the specified channel. |
| 1040 | * |
| 1041 | * Before returning xpc_initiate_disconnect() will wait until all connections |
| 1042 | * on the specified channel have been closed/torndown. So the caller can be |
| 1043 | * assured that they will not be receiving any more callouts from XPC to the |
| 1044 | * function they registered via xpc_connect(). |
| 1045 | * |
| 1046 | * Arguments: |
| 1047 | * |
| 1048 | * ch_number - channel # to unregister. |
| 1049 | */ |
| 1050 | void |
| 1051 | xpc_initiate_disconnect(int ch_number) |
| 1052 | { |
| 1053 | unsigned long irq_flags; |
Dean Nelson | 64d032b | 2008-05-12 14:02:03 -0700 | [diff] [blame] | 1054 | short partid; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1055 | struct xpc_partition *part; |
| 1056 | struct xpc_channel *ch; |
| 1057 | |
Dean Nelson | bc63d38 | 2008-07-29 22:34:04 -0700 | [diff] [blame] | 1058 | DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1059 | |
| 1060 | /* initiate the channel disconnect for every active partition */ |
Dean Nelson | bc63d38 | 2008-07-29 22:34:04 -0700 | [diff] [blame] | 1061 | for (partid = 0; partid < xp_max_npartitions; partid++) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1062 | part = &xpc_partitions[partid]; |
| 1063 | |
| 1064 | if (xpc_part_ref(part)) { |
| 1065 | ch = &part->channels[ch_number]; |
| 1066 | xpc_msgqueue_ref(ch); |
| 1067 | |
| 1068 | spin_lock_irqsave(&ch->lock, irq_flags); |
| 1069 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1070 | if (!(ch->flags & XPC_C_DISCONNECTED)) { |
| 1071 | ch->flags |= XPC_C_WDISCONNECT; |
| 1072 | |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1073 | XPC_DISCONNECT_CHANNEL(ch, xpUnregistering, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1074 | &irq_flags); |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1075 | } |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1076 | |
| 1077 | spin_unlock_irqrestore(&ch->lock, irq_flags); |
| 1078 | |
| 1079 | xpc_msgqueue_deref(ch); |
| 1080 | xpc_part_deref(part); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | xpc_disconnect_wait(ch_number); |
| 1085 | } |
| 1086 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1087 | /* |
| 1088 | * To disconnect a channel, and reflect it back to all who may be waiting. |
| 1089 | * |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1090 | * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by |
| 1091 | * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by |
| 1092 | * xpc_disconnect_wait(). |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1093 | * |
| 1094 | * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN. |
| 1095 | */ |
| 1096 | void |
| 1097 | xpc_disconnect_channel(const int line, struct xpc_channel *ch, |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1098 | enum xp_retval reason, unsigned long *irq_flags) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1099 | { |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1100 | u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1101 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1102 | DBUG_ON(!spin_is_locked(&ch->lock)); |
| 1103 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1104 | if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1105 | return; |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1106 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1107 | DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED))); |
| 1108 | |
| 1109 | dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n", |
| 1110 | reason, line, ch->partid, ch->number); |
| 1111 | |
| 1112 | XPC_SET_REASON(ch, reason, line); |
| 1113 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1114 | ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1115 | /* some of these may not have been set */ |
| 1116 | ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1117 | XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY | |
| 1118 | XPC_C_CONNECTING | XPC_C_CONNECTED); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1119 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1120 | xpc_IPI_send_closerequest(ch, irq_flags); |
| 1121 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1122 | if (channel_was_connected) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1123 | ch->flags |= XPC_C_WASCONNECTED; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1124 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1125 | spin_unlock_irqrestore(&ch->lock, *irq_flags); |
| 1126 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1127 | /* wake all idle kthreads so they can exit */ |
| 1128 | if (atomic_read(&ch->kthreads_idle) > 0) { |
| 1129 | wake_up_all(&ch->idle_wq); |
Dean Nelson | a460ef8 | 2006-11-22 08:25:00 -0600 | [diff] [blame] | 1130 | |
| 1131 | } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) && |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1132 | !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1133 | /* start a kthread that will do the xpDisconnecting callout */ |
Dean Nelson | a460ef8 | 2006-11-22 08:25:00 -0600 | [diff] [blame] | 1134 | xpc_create_kthreads(ch, 1, 1); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1137 | /* wake those waiting to allocate an entry from the local msg queue */ |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1138 | if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1139 | wake_up(&ch->msg_allocate_wq); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1140 | |
| 1141 | spin_lock_irqsave(&ch->lock, *irq_flags); |
| 1142 | } |
| 1143 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1144 | void |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1145 | xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1146 | { |
| 1147 | /* |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1148 | * Let the channel's registerer know that the channel is being |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1149 | * disconnected. We don't want to do this if the registerer was never |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1150 | * informed of a connection being made. |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1151 | */ |
| 1152 | |
| 1153 | if (ch->func != NULL) { |
Dean Nelson | 246c7e3 | 2005-12-22 14:32:56 -0600 | [diff] [blame] | 1154 | dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, " |
| 1155 | "channel=%d\n", reason, ch->partid, ch->number); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1156 | |
Dean Nelson | 246c7e3 | 2005-12-22 14:32:56 -0600 | [diff] [blame] | 1157 | ch->func(reason, ch->partid, ch->number, NULL, ch->key); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1158 | |
Dean Nelson | 246c7e3 | 2005-12-22 14:32:56 -0600 | [diff] [blame] | 1159 | dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, " |
| 1160 | "channel=%d\n", reason, ch->partid, ch->number); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1164 | /* |
| 1165 | * Wait for a message entry to become available for the specified channel, |
| 1166 | * but don't wait any longer than 1 jiffy. |
| 1167 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1168 | static enum xp_retval |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1169 | xpc_allocate_msg_wait(struct xpc_channel *ch) |
| 1170 | { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1171 | enum xp_retval ret; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1172 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1173 | if (ch->flags & XPC_C_DISCONNECTING) { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1174 | DBUG_ON(ch->reason == xpInterrupted); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1175 | return ch->reason; |
| 1176 | } |
| 1177 | |
| 1178 | atomic_inc(&ch->n_on_msg_allocate_wq); |
| 1179 | ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1); |
| 1180 | atomic_dec(&ch->n_on_msg_allocate_wq); |
| 1181 | |
| 1182 | if (ch->flags & XPC_C_DISCONNECTING) { |
| 1183 | ret = ch->reason; |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1184 | DBUG_ON(ch->reason == xpInterrupted); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1185 | } else if (ret == 0) { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1186 | ret = xpTimeout; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1187 | } else { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1188 | ret = xpInterrupted; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1194 | /* |
| 1195 | * Allocate an entry for a message from the message queue associated with the |
| 1196 | * specified channel. |
| 1197 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1198 | static enum xp_retval |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1199 | xpc_allocate_msg(struct xpc_channel *ch, u32 flags, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1200 | struct xpc_msg **address_of_msg) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1201 | { |
| 1202 | struct xpc_msg *msg; |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1203 | enum xp_retval ret; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1204 | s64 put; |
| 1205 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1206 | /* this reference will be dropped in xpc_send_msg() */ |
| 1207 | xpc_msgqueue_ref(ch); |
| 1208 | |
| 1209 | if (ch->flags & XPC_C_DISCONNECTING) { |
| 1210 | xpc_msgqueue_deref(ch); |
| 1211 | return ch->reason; |
| 1212 | } |
| 1213 | if (!(ch->flags & XPC_C_CONNECTED)) { |
| 1214 | xpc_msgqueue_deref(ch); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1215 | return xpNotConnected; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1218 | /* |
| 1219 | * Get the next available message entry from the local message queue. |
| 1220 | * If none are available, we'll make sure that we grab the latest |
| 1221 | * GP values. |
| 1222 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1223 | ret = xpTimeout; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1224 | |
| 1225 | while (1) { |
| 1226 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1227 | put = ch->w_local_GP.put; |
| 1228 | rmb(); /* guarantee that .put loads before .get */ |
| 1229 | if (put - ch->w_remote_GP.get < ch->local_nentries) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1230 | |
| 1231 | /* There are available message entries. We need to try |
| 1232 | * to secure one for ourselves. We'll do this by trying |
| 1233 | * to increment w_local_GP.put as long as someone else |
| 1234 | * doesn't beat us to it. If they do, we'll have to |
| 1235 | * try again. |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1236 | */ |
| 1237 | if (cmpxchg(&ch->w_local_GP.put, put, put + 1) == put) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1238 | /* we got the entry referenced by put */ |
| 1239 | break; |
| 1240 | } |
| 1241 | continue; /* try again */ |
| 1242 | } |
| 1243 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1244 | /* |
| 1245 | * There aren't any available msg entries at this time. |
| 1246 | * |
| 1247 | * In waiting for a message entry to become available, |
| 1248 | * we set a timeout in case the other side is not |
| 1249 | * sending completion IPIs. This lets us fake an IPI |
| 1250 | * that will cause the IPI handler to fetch the latest |
| 1251 | * GP values as if an IPI was sent by the other side. |
| 1252 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1253 | if (ret == xpTimeout) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1254 | xpc_IPI_send_local_msgrequest(ch); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1255 | |
| 1256 | if (flags & XPC_NOWAIT) { |
| 1257 | xpc_msgqueue_deref(ch); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1258 | return xpNoWait; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | ret = xpc_allocate_msg_wait(ch); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1262 | if (ret != xpInterrupted && ret != xpTimeout) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1263 | xpc_msgqueue_deref(ch); |
| 1264 | return ret; |
| 1265 | } |
| 1266 | } |
| 1267 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1268 | /* get the message's address and initialize it */ |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1269 | msg = (struct xpc_msg *)((u64)ch->local_msgqueue + |
| 1270 | (put % ch->local_nentries) * ch->msg_size); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1271 | |
| 1272 | DBUG_ON(msg->flags != 0); |
| 1273 | msg->number = put; |
| 1274 | |
| 1275 | dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, " |
| 1276 | "msg_number=%ld, partid=%d, channel=%d\n", put + 1, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1277 | (void *)msg, msg->number, ch->partid, ch->number); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1278 | |
| 1279 | *address_of_msg = msg; |
| 1280 | |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1281 | return xpSuccess; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1284 | /* |
| 1285 | * Allocate an entry for a message from the message queue associated with the |
| 1286 | * specified channel. NOTE that this routine can sleep waiting for a message |
| 1287 | * entry to become available. To not sleep, pass in the XPC_NOWAIT flag. |
| 1288 | * |
| 1289 | * Arguments: |
| 1290 | * |
| 1291 | * partid - ID of partition to which the channel is connected. |
| 1292 | * ch_number - channel #. |
| 1293 | * flags - see xpc.h for valid flags. |
| 1294 | * payload - address of the allocated payload area pointer (filled in on |
| 1295 | * return) in which the user-defined message is constructed. |
| 1296 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1297 | enum xp_retval |
Dean Nelson | 64d032b | 2008-05-12 14:02:03 -0700 | [diff] [blame] | 1298 | xpc_initiate_allocate(short partid, int ch_number, u32 flags, void **payload) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1299 | { |
| 1300 | struct xpc_partition *part = &xpc_partitions[partid]; |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1301 | enum xp_retval ret = xpUnknownReason; |
Tony Luck | 27f4aa3 | 2006-04-04 14:11:49 -0700 | [diff] [blame] | 1302 | struct xpc_msg *msg = NULL; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1303 | |
Dean Nelson | bc63d38 | 2008-07-29 22:34:04 -0700 | [diff] [blame] | 1304 | DBUG_ON(partid < 0 || partid >= xp_max_npartitions); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1305 | DBUG_ON(ch_number < 0 || ch_number >= part->nchannels); |
| 1306 | |
| 1307 | *payload = NULL; |
| 1308 | |
| 1309 | if (xpc_part_ref(part)) { |
| 1310 | ret = xpc_allocate_msg(&part->channels[ch_number], flags, &msg); |
| 1311 | xpc_part_deref(part); |
| 1312 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1313 | if (msg != NULL) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1314 | *payload = &msg->payload; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | return ret; |
| 1318 | } |
| 1319 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1320 | /* |
| 1321 | * Now we actually send the messages that are ready to be sent by advancing |
| 1322 | * the local message queue's Put value and then send an IPI to the recipient |
| 1323 | * partition. |
| 1324 | */ |
| 1325 | static void |
| 1326 | xpc_send_msgs(struct xpc_channel *ch, s64 initial_put) |
| 1327 | { |
| 1328 | struct xpc_msg *msg; |
| 1329 | s64 put = initial_put + 1; |
| 1330 | int send_IPI = 0; |
| 1331 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1332 | while (1) { |
| 1333 | |
| 1334 | while (1) { |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1335 | if (put == ch->w_local_GP.put) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1336 | break; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1337 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1338 | msg = (struct xpc_msg *)((u64)ch->local_msgqueue + |
| 1339 | (put % ch->local_nentries) * |
| 1340 | ch->msg_size); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1341 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1342 | if (!(msg->flags & XPC_M_READY)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1343 | break; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1344 | |
| 1345 | put++; |
| 1346 | } |
| 1347 | |
| 1348 | if (put == initial_put) { |
| 1349 | /* nothing's changed */ |
| 1350 | break; |
| 1351 | } |
| 1352 | |
| 1353 | if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) != |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1354 | initial_put) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1355 | /* someone else beat us to it */ |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1356 | DBUG_ON(ch->local_GP->put < initial_put); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1357 | break; |
| 1358 | } |
| 1359 | |
| 1360 | /* we just set the new value of local_GP->put */ |
| 1361 | |
| 1362 | dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, " |
| 1363 | "channel=%d\n", put, ch->partid, ch->number); |
| 1364 | |
| 1365 | send_IPI = 1; |
| 1366 | |
| 1367 | /* |
| 1368 | * We need to ensure that the message referenced by |
| 1369 | * local_GP->put is not XPC_M_READY or that local_GP->put |
| 1370 | * equals w_local_GP.put, so we'll go have a look. |
| 1371 | */ |
| 1372 | initial_put = put; |
| 1373 | } |
| 1374 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1375 | if (send_IPI) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1376 | xpc_IPI_send_msgrequest(ch); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1379 | /* |
| 1380 | * Common code that does the actual sending of the message by advancing the |
| 1381 | * local message queue's Put value and sends an IPI to the partition the |
| 1382 | * message is being sent to. |
| 1383 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1384 | static enum xp_retval |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1385 | xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1386 | xpc_notify_func func, void *key) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1387 | { |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1388 | enum xp_retval ret = xpSuccess; |
Dean Nelson | a607c38 | 2005-09-01 14:01:37 -0500 | [diff] [blame] | 1389 | struct xpc_notify *notify = notify; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1390 | s64 put, msg_number = msg->number; |
| 1391 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1392 | DBUG_ON(notify_type == XPC_N_CALL && func == NULL); |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1393 | DBUG_ON((((u64)msg - (u64)ch->local_msgqueue) / ch->msg_size) != |
| 1394 | msg_number % ch->local_nentries); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1395 | DBUG_ON(msg->flags & XPC_M_READY); |
| 1396 | |
| 1397 | if (ch->flags & XPC_C_DISCONNECTING) { |
| 1398 | /* drop the reference grabbed in xpc_allocate_msg() */ |
| 1399 | xpc_msgqueue_deref(ch); |
| 1400 | return ch->reason; |
| 1401 | } |
| 1402 | |
| 1403 | if (notify_type != 0) { |
| 1404 | /* |
| 1405 | * Tell the remote side to send an ACK interrupt when the |
| 1406 | * message has been delivered. |
| 1407 | */ |
| 1408 | msg->flags |= XPC_M_INTERRUPT; |
| 1409 | |
| 1410 | atomic_inc(&ch->n_to_notify); |
| 1411 | |
| 1412 | notify = &ch->notify_queue[msg_number % ch->local_nentries]; |
| 1413 | notify->func = func; |
| 1414 | notify->key = key; |
Dave Jones | 821fe94 | 2005-06-25 14:54:29 -0700 | [diff] [blame] | 1415 | notify->type = notify_type; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1416 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1417 | /* >>> is a mb() needed here? */ |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1418 | |
| 1419 | if (ch->flags & XPC_C_DISCONNECTING) { |
| 1420 | /* |
| 1421 | * An error occurred between our last error check and |
| 1422 | * this one. We will try to clear the type field from |
| 1423 | * the notify entry. If we succeed then |
| 1424 | * xpc_disconnect_channel() didn't already process |
| 1425 | * the notify entry. |
| 1426 | */ |
| 1427 | if (cmpxchg(¬ify->type, notify_type, 0) == |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1428 | notify_type) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1429 | atomic_dec(&ch->n_to_notify); |
| 1430 | ret = ch->reason; |
| 1431 | } |
| 1432 | |
| 1433 | /* drop the reference grabbed in xpc_allocate_msg() */ |
| 1434 | xpc_msgqueue_deref(ch); |
| 1435 | return ret; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | msg->flags |= XPC_M_READY; |
| 1440 | |
| 1441 | /* |
| 1442 | * The preceding store of msg->flags must occur before the following |
| 1443 | * load of ch->local_GP->put. |
| 1444 | */ |
| 1445 | mb(); |
| 1446 | |
| 1447 | /* see if the message is next in line to be sent, if so send it */ |
| 1448 | |
| 1449 | put = ch->local_GP->put; |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1450 | if (put == msg_number) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1451 | xpc_send_msgs(ch, put); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1452 | |
| 1453 | /* drop the reference grabbed in xpc_allocate_msg() */ |
| 1454 | xpc_msgqueue_deref(ch); |
| 1455 | return ret; |
| 1456 | } |
| 1457 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1458 | /* |
| 1459 | * Send a message previously allocated using xpc_initiate_allocate() on the |
| 1460 | * specified channel connected to the specified partition. |
| 1461 | * |
| 1462 | * This routine will not wait for the message to be received, nor will |
| 1463 | * notification be given when it does happen. Once this routine has returned |
| 1464 | * the message entry allocated via xpc_initiate_allocate() is no longer |
| 1465 | * accessable to the caller. |
| 1466 | * |
| 1467 | * This routine, although called by users, does not call xpc_part_ref() to |
| 1468 | * ensure that the partition infrastructure is in place. It relies on the |
| 1469 | * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg(). |
| 1470 | * |
| 1471 | * Arguments: |
| 1472 | * |
| 1473 | * partid - ID of partition to which the channel is connected. |
| 1474 | * ch_number - channel # to send message on. |
| 1475 | * payload - pointer to the payload area allocated via |
| 1476 | * xpc_initiate_allocate(). |
| 1477 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1478 | enum xp_retval |
Dean Nelson | 64d032b | 2008-05-12 14:02:03 -0700 | [diff] [blame] | 1479 | xpc_initiate_send(short partid, int ch_number, void *payload) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1480 | { |
| 1481 | struct xpc_partition *part = &xpc_partitions[partid]; |
| 1482 | struct xpc_msg *msg = XPC_MSG_ADDRESS(payload); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1483 | enum xp_retval ret; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1484 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1485 | dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg, |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1486 | partid, ch_number); |
| 1487 | |
Dean Nelson | bc63d38 | 2008-07-29 22:34:04 -0700 | [diff] [blame] | 1488 | DBUG_ON(partid < 0 || partid >= xp_max_npartitions); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1489 | DBUG_ON(ch_number < 0 || ch_number >= part->nchannels); |
| 1490 | DBUG_ON(msg == NULL); |
| 1491 | |
| 1492 | ret = xpc_send_msg(&part->channels[ch_number], msg, 0, NULL, NULL); |
| 1493 | |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1497 | /* |
| 1498 | * Send a message previously allocated using xpc_initiate_allocate on the |
| 1499 | * specified channel connected to the specified partition. |
| 1500 | * |
| 1501 | * This routine will not wait for the message to be sent. Once this routine |
| 1502 | * has returned the message entry allocated via xpc_initiate_allocate() is no |
| 1503 | * longer accessable to the caller. |
| 1504 | * |
| 1505 | * Once the remote end of the channel has received the message, the function |
| 1506 | * passed as an argument to xpc_initiate_send_notify() will be called. This |
| 1507 | * allows the sender to free up or re-use any buffers referenced by the |
| 1508 | * message, but does NOT mean the message has been processed at the remote |
| 1509 | * end by a receiver. |
| 1510 | * |
| 1511 | * If this routine returns an error, the caller's function will NOT be called. |
| 1512 | * |
| 1513 | * This routine, although called by users, does not call xpc_part_ref() to |
| 1514 | * ensure that the partition infrastructure is in place. It relies on the |
| 1515 | * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg(). |
| 1516 | * |
| 1517 | * Arguments: |
| 1518 | * |
| 1519 | * partid - ID of partition to which the channel is connected. |
| 1520 | * ch_number - channel # to send message on. |
| 1521 | * payload - pointer to the payload area allocated via |
| 1522 | * xpc_initiate_allocate(). |
| 1523 | * func - function to call with asynchronous notification of message |
| 1524 | * receipt. THIS FUNCTION MUST BE NON-BLOCKING. |
| 1525 | * key - user-defined key to be passed to the function when it's called. |
| 1526 | */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1527 | enum xp_retval |
Dean Nelson | 64d032b | 2008-05-12 14:02:03 -0700 | [diff] [blame] | 1528 | xpc_initiate_send_notify(short partid, int ch_number, void *payload, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1529 | xpc_notify_func func, void *key) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1530 | { |
| 1531 | struct xpc_partition *part = &xpc_partitions[partid]; |
| 1532 | struct xpc_msg *msg = XPC_MSG_ADDRESS(payload); |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1533 | enum xp_retval ret; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1534 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1535 | dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg, |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1536 | partid, ch_number); |
| 1537 | |
Dean Nelson | bc63d38 | 2008-07-29 22:34:04 -0700 | [diff] [blame] | 1538 | DBUG_ON(partid < 0 || partid >= xp_max_npartitions); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1539 | DBUG_ON(ch_number < 0 || ch_number >= part->nchannels); |
| 1540 | DBUG_ON(msg == NULL); |
| 1541 | DBUG_ON(func == NULL); |
| 1542 | |
| 1543 | ret = xpc_send_msg(&part->channels[ch_number], msg, XPC_N_CALL, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1544 | func, key); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1545 | return ret; |
| 1546 | } |
| 1547 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1548 | /* |
| 1549 | * Deliver a message to its intended recipient. |
| 1550 | */ |
| 1551 | void |
| 1552 | xpc_deliver_msg(struct xpc_channel *ch) |
| 1553 | { |
| 1554 | struct xpc_msg *msg; |
| 1555 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1556 | msg = xpc_get_deliverable_msg(ch); |
| 1557 | if (msg != NULL) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1558 | |
| 1559 | /* |
| 1560 | * This ref is taken to protect the payload itself from being |
| 1561 | * freed before the user is finished with it, which the user |
| 1562 | * indicates by calling xpc_initiate_received(). |
| 1563 | */ |
| 1564 | xpc_msgqueue_ref(ch); |
| 1565 | |
| 1566 | atomic_inc(&ch->kthreads_active); |
| 1567 | |
| 1568 | if (ch->func != NULL) { |
| 1569 | dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, " |
| 1570 | "msg_number=%ld, partid=%d, channel=%d\n", |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1571 | (void *)msg, msg->number, ch->partid, |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1572 | ch->number); |
| 1573 | |
| 1574 | /* deliver the message to its intended recipient */ |
Dean Nelson | 65c17b8 | 2008-05-12 14:02:02 -0700 | [diff] [blame] | 1575 | ch->func(xpMsgReceived, ch->partid, ch->number, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1576 | &msg->payload, ch->key); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1577 | |
| 1578 | dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, " |
| 1579 | "msg_number=%ld, partid=%d, channel=%d\n", |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1580 | (void *)msg, msg->number, ch->partid, |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1581 | ch->number); |
| 1582 | } |
| 1583 | |
| 1584 | atomic_dec(&ch->kthreads_active); |
| 1585 | } |
| 1586 | } |
| 1587 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1588 | /* |
| 1589 | * Now we actually acknowledge the messages that have been delivered and ack'd |
| 1590 | * by advancing the cached remote message queue's Get value and if requested |
| 1591 | * send an IPI to the message sender's partition. |
| 1592 | */ |
| 1593 | static void |
| 1594 | xpc_acknowledge_msgs(struct xpc_channel *ch, s64 initial_get, u8 msg_flags) |
| 1595 | { |
| 1596 | struct xpc_msg *msg; |
| 1597 | s64 get = initial_get + 1; |
| 1598 | int send_IPI = 0; |
| 1599 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1600 | while (1) { |
| 1601 | |
| 1602 | while (1) { |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1603 | if (get == ch->w_local_GP.get) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1604 | break; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1605 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1606 | msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + |
| 1607 | (get % ch->remote_nentries) * |
| 1608 | ch->msg_size); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1609 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1610 | if (!(msg->flags & XPC_M_DONE)) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1611 | break; |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1612 | |
| 1613 | msg_flags |= msg->flags; |
| 1614 | get++; |
| 1615 | } |
| 1616 | |
| 1617 | if (get == initial_get) { |
| 1618 | /* nothing's changed */ |
| 1619 | break; |
| 1620 | } |
| 1621 | |
| 1622 | if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) != |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1623 | initial_get) { |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1624 | /* someone else beat us to it */ |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1625 | DBUG_ON(ch->local_GP->get <= initial_get); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1626 | break; |
| 1627 | } |
| 1628 | |
| 1629 | /* we just set the new value of local_GP->get */ |
| 1630 | |
| 1631 | dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, " |
| 1632 | "channel=%d\n", get, ch->partid, ch->number); |
| 1633 | |
| 1634 | send_IPI = (msg_flags & XPC_M_INTERRUPT); |
| 1635 | |
| 1636 | /* |
| 1637 | * We need to ensure that the message referenced by |
| 1638 | * local_GP->get is not XPC_M_DONE or that local_GP->get |
| 1639 | * equals w_local_GP.get, so we'll go have a look. |
| 1640 | */ |
| 1641 | initial_get = get; |
| 1642 | } |
| 1643 | |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1644 | if (send_IPI) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1645 | xpc_IPI_send_msgrequest(ch); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1648 | /* |
| 1649 | * Acknowledge receipt of a delivered message. |
| 1650 | * |
| 1651 | * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition |
| 1652 | * that sent the message. |
| 1653 | * |
| 1654 | * This function, although called by users, does not call xpc_part_ref() to |
| 1655 | * ensure that the partition infrastructure is in place. It relies on the |
| 1656 | * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg(). |
| 1657 | * |
| 1658 | * Arguments: |
| 1659 | * |
| 1660 | * partid - ID of partition to which the channel is connected. |
| 1661 | * ch_number - channel # message received on. |
| 1662 | * payload - pointer to the payload area allocated via |
| 1663 | * xpc_initiate_allocate(). |
| 1664 | */ |
| 1665 | void |
Dean Nelson | 64d032b | 2008-05-12 14:02:03 -0700 | [diff] [blame] | 1666 | xpc_initiate_received(short partid, int ch_number, void *payload) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1667 | { |
| 1668 | struct xpc_partition *part = &xpc_partitions[partid]; |
| 1669 | struct xpc_channel *ch; |
| 1670 | struct xpc_msg *msg = XPC_MSG_ADDRESS(payload); |
| 1671 | s64 get, msg_number = msg->number; |
| 1672 | |
Dean Nelson | bc63d38 | 2008-07-29 22:34:04 -0700 | [diff] [blame] | 1673 | DBUG_ON(partid < 0 || partid >= xp_max_npartitions); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1674 | DBUG_ON(ch_number < 0 || ch_number >= part->nchannels); |
| 1675 | |
| 1676 | ch = &part->channels[ch_number]; |
| 1677 | |
| 1678 | dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n", |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1679 | (void *)msg, msg_number, ch->partid, ch->number); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1680 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame] | 1681 | DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) != |
| 1682 | msg_number % ch->remote_nentries); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1683 | DBUG_ON(msg->flags & XPC_M_DONE); |
| 1684 | |
| 1685 | msg->flags |= XPC_M_DONE; |
| 1686 | |
| 1687 | /* |
| 1688 | * The preceding store of msg->flags must occur before the following |
| 1689 | * load of ch->local_GP->get. |
| 1690 | */ |
| 1691 | mb(); |
| 1692 | |
| 1693 | /* |
| 1694 | * See if this message is next in line to be acknowledged as having |
| 1695 | * been delivered. |
| 1696 | */ |
| 1697 | get = ch->local_GP->get; |
Dean Nelson | 2c2b94f | 2008-04-22 14:50:17 -0500 | [diff] [blame] | 1698 | if (get == msg_number) |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1699 | xpc_acknowledge_msgs(ch, get, msg->flags); |
Dean Nelson | 89eb8eb | 2005-03-23 19:50:00 -0700 | [diff] [blame] | 1700 | |
| 1701 | /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */ |
| 1702 | xpc_msgqueue_deref(ch); |
| 1703 | } |