]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/misc/sgi-xp/xpc_channel.c
sgi-xpc: clean up numerous globals
[net-next-2.6.git] / drivers / misc / sgi-xp / xpc_channel.c
CommitLineData
89eb8eb9
DN
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 *
efdd06ed 6 * Copyright (c) 2004-2009 Silicon Graphics, Inc. All Rights Reserved.
89eb8eb9
DN
7 */
8
89eb8eb9
DN
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
261f3b49 17#include <linux/device.h>
45d9ca49 18#include "xpc.h"
89eb8eb9 19
89eb8eb9
DN
20/*
21 * Process a connect message from a remote partition.
22 *
23 * Note: xpc_process_connect() is expecting to be called with the
24 * spin_lock_irqsave held and will leave it locked upon return.
25 */
26static void
27xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
28{
65c17b80 29 enum xp_retval ret;
89eb8eb9 30
89eb8eb9
DN
31 DBUG_ON(!spin_is_locked(&ch->lock));
32
33 if (!(ch->flags & XPC_C_OPENREQUEST) ||
35190506 34 !(ch->flags & XPC_C_ROPENREQUEST)) {
89eb8eb9
DN
35 /* nothing more to do for now */
36 return;
37 }
38 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
39
40 if (!(ch->flags & XPC_C_SETUP)) {
41 spin_unlock_irqrestore(&ch->lock, *irq_flags);
a7665b0a 42 ret = xpc_arch_ops.setup_msg_structures(ch);
89eb8eb9
DN
43 spin_lock_irqsave(&ch->lock, *irq_flags);
44
65c17b80 45 if (ret != xpSuccess)
89eb8eb9 46 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
efdd06ed
RH
47 else
48 ch->flags |= XPC_C_SETUP;
2c2b94f9 49
efdd06ed 50 if (ch->flags & XPC_C_DISCONNECTING)
89eb8eb9 51 return;
89eb8eb9
DN
52 }
53
54 if (!(ch->flags & XPC_C_OPENREPLY)) {
55 ch->flags |= XPC_C_OPENREPLY;
a7665b0a 56 xpc_arch_ops.send_chctl_openreply(ch, irq_flags);
89eb8eb9
DN
57 }
58
2c2b94f9 59 if (!(ch->flags & XPC_C_ROPENREPLY))
89eb8eb9 60 return;
89eb8eb9 61
efdd06ed
RH
62 if (!(ch->flags & XPC_C_OPENCOMPLETE)) {
63 ch->flags |= (XPC_C_OPENCOMPLETE | XPC_C_CONNECTED);
a7665b0a 64 xpc_arch_ops.send_chctl_opencomplete(ch, irq_flags);
efdd06ed
RH
65 }
66
67 if (!(ch->flags & XPC_C_ROPENCOMPLETE))
68 return;
89eb8eb9
DN
69
70 dev_info(xpc_chan, "channel %d to partition %d connected\n",
35190506 71 ch->number, ch->partid);
89eb8eb9 72
efdd06ed 73 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
89eb8eb9
DN
74}
75
89eb8eb9
DN
76/*
77 * spin_lock_irqsave() is expected to be held on entry.
78 */
79static void
80xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
81{
82 struct xpc_partition *part = &xpc_partitions[ch->partid];
a607c389 83 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
89eb8eb9 84
89eb8eb9
DN
85 DBUG_ON(!spin_is_locked(&ch->lock));
86
2c2b94f9 87 if (!(ch->flags & XPC_C_DISCONNECTING))
89eb8eb9 88 return;
89eb8eb9
DN
89
90 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
91
92 /* make sure all activity has settled down first */
93
a460ef8d 94 if (atomic_read(&ch->kthreads_assigned) > 0 ||
35190506 95 atomic_read(&ch->references) > 0) {
89eb8eb9
DN
96 return;
97 }
a460ef8d 98 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
35190506 99 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
89eb8eb9 100
83469b55 101 if (part->act_state == XPC_P_AS_DEACTIVATING) {
a607c389 102 /* can't proceed until the other side disengages from us */
a7665b0a 103 if (xpc_arch_ops.partition_engaged(ch->partid))
a607c389 104 return;
89eb8eb9 105
a607c389 106 } else {
89eb8eb9
DN
107
108 /* as long as the other side is up do the full protocol */
109
2c2b94f9 110 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
89eb8eb9 111 return;
89eb8eb9
DN
112
113 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
114 ch->flags |= XPC_C_CLOSEREPLY;
a7665b0a 115 xpc_arch_ops.send_chctl_closereply(ch, irq_flags);
89eb8eb9
DN
116 }
117
2c2b94f9 118 if (!(ch->flags & XPC_C_RCLOSEREPLY))
89eb8eb9 119 return;
89eb8eb9
DN
120 }
121
a607c389
DN
122 /* wake those waiting for notify completion */
123 if (atomic_read(&ch->n_to_notify) > 0) {
ea57f80c 124 /* we do callout while holding ch->lock, callout can't block */
a7665b0a 125 xpc_arch_ops.notify_senders_of_disconnect(ch);
a607c389
DN
126 }
127
89eb8eb9
DN
128 /* both sides are disconnected now */
129
4c2cd966 130 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
246c7e33 131 spin_unlock_irqrestore(&ch->lock, *irq_flags);
65c17b80 132 xpc_disconnect_callout(ch, xpDisconnected);
246c7e33
DN
133 spin_lock_irqsave(&ch->lock, *irq_flags);
134 }
135
5b8669df
DN
136 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
137
a607c389 138 /* it's now safe to free the channel's message queues */
a7665b0a 139 xpc_arch_ops.teardown_msg_structures(ch);
5b8669df
DN
140
141 ch->func = NULL;
142 ch->key = NULL;
bd3e64c1 143 ch->entry_size = 0;
5b8669df
DN
144 ch->local_nentries = 0;
145 ch->remote_nentries = 0;
146 ch->kthreads_assigned_limit = 0;
147 ch->kthreads_idle_limit = 0;
a607c389 148
185c3a1b
DN
149 /*
150 * Mark the channel disconnected and clear all other flags, including
a7665b0a
RH
151 * XPC_C_SETUP (because of call to
152 * xpc_arch_ops.teardown_msg_structures()) but not including
153 * XPC_C_WDISCONNECT (if it was set).
185c3a1b 154 */
a607c389 155 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
89eb8eb9
DN
156
157 atomic_dec(&part->nchannels_active);
158
a607c389 159 if (channel_was_connected) {
89eb8eb9 160 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
35190506 161 "reason=%d\n", ch->number, ch->partid, ch->reason);
89eb8eb9 162 }
a607c389 163
a607c389 164 if (ch->flags & XPC_C_WDISCONNECT) {
f9e505a9
JS
165 /* we won't lose the CPU since we're holding ch->lock */
166 complete(&ch->wdisconnect_wait);
7fb5e59d 167 } else if (ch->delayed_chctl_flags) {
83469b55 168 if (part->act_state != XPC_P_AS_DEACTIVATING) {
7fb5e59d
DN
169 /* time to take action on any delayed chctl flags */
170 spin_lock(&part->chctl_lock);
171 part->chctl.flags[ch->number] |=
172 ch->delayed_chctl_flags;
173 spin_unlock(&part->chctl_lock);
e54af724 174 }
7fb5e59d 175 ch->delayed_chctl_flags = 0;
a607c389 176 }
89eb8eb9
DN
177}
178
89eb8eb9
DN
179/*
180 * Process a change in the channel's remote connection state.
181 */
182static void
7fb5e59d
DN
183xpc_process_openclose_chctl_flags(struct xpc_partition *part, int ch_number,
184 u8 chctl_flags)
89eb8eb9
DN
185{
186 unsigned long irq_flags;
187 struct xpc_openclose_args *args =
35190506 188 &part->remote_openclose_args[ch_number];
89eb8eb9 189 struct xpc_channel *ch = &part->channels[ch_number];
65c17b80 190 enum xp_retval reason;
6f2584f4 191 enum xp_retval ret;
efdd06ed 192 int create_kthread = 0;
89eb8eb9 193
89eb8eb9
DN
194 spin_lock_irqsave(&ch->lock, irq_flags);
195
2c2b94f9 196again:
e54af724 197
2c2b94f9
DN
198 if ((ch->flags & XPC_C_DISCONNECTED) &&
199 (ch->flags & XPC_C_WDISCONNECT)) {
e54af724 200 /*
7fb5e59d 201 * Delay processing chctl flags until thread waiting disconnect
e54af724
DN
202 * has had a chance to see that the channel is disconnected.
203 */
7fb5e59d 204 ch->delayed_chctl_flags |= chctl_flags;
efdd06ed 205 goto out;
e54af724
DN
206 }
207
7fb5e59d 208 if (chctl_flags & XPC_CHCTL_CLOSEREQUEST) {
89eb8eb9 209
7fb5e59d 210 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
89eb8eb9
DN
211 "from partid=%d, channel=%d\n", args->reason,
212 ch->partid, ch->number);
213
214 /*
215 * If RCLOSEREQUEST is set, we're probably waiting for
216 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
7fb5e59d 217 * with this RCLOSEREQUEST in the chctl_flags.
89eb8eb9
DN
218 */
219
220 if (ch->flags & XPC_C_RCLOSEREQUEST) {
221 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
222 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
223 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
224 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
225
7fb5e59d
DN
226 DBUG_ON(!(chctl_flags & XPC_CHCTL_CLOSEREPLY));
227 chctl_flags &= ~XPC_CHCTL_CLOSEREPLY;
89eb8eb9
DN
228 ch->flags |= XPC_C_RCLOSEREPLY;
229
230 /* both sides have finished disconnecting */
231 xpc_process_disconnect(ch, &irq_flags);
e54af724
DN
232 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
233 goto again;
89eb8eb9
DN
234 }
235
236 if (ch->flags & XPC_C_DISCONNECTED) {
7fb5e59d
DN
237 if (!(chctl_flags & XPC_CHCTL_OPENREQUEST)) {
238 if (part->chctl.flags[ch_number] &
239 XPC_CHCTL_OPENREQUEST) {
240
241 DBUG_ON(ch->delayed_chctl_flags != 0);
242 spin_lock(&part->chctl_lock);
243 part->chctl.flags[ch_number] |=
244 XPC_CHCTL_CLOSEREQUEST;
245 spin_unlock(&part->chctl_lock);
e54af724 246 }
efdd06ed 247 goto out;
89eb8eb9
DN
248 }
249
250 XPC_SET_REASON(ch, 0, 0);
251 ch->flags &= ~XPC_C_DISCONNECTED;
252
253 atomic_inc(&part->nchannels_active);
254 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
255 }
256
efdd06ed
RH
257 chctl_flags &= ~(XPC_CHCTL_OPENREQUEST | XPC_CHCTL_OPENREPLY |
258 XPC_CHCTL_OPENCOMPLETE);
89eb8eb9
DN
259
260 /*
261 * The meaningful CLOSEREQUEST connection state fields are:
262 * reason = reason connection is to be closed
263 */
264
265 ch->flags |= XPC_C_RCLOSEREQUEST;
266
267 if (!(ch->flags & XPC_C_DISCONNECTING)) {
268 reason = args->reason;
65c17b80
DN
269 if (reason <= xpSuccess || reason > xpUnknownReason)
270 reason = xpUnknownReason;
271 else if (reason == xpUnregistering)
272 reason = xpOtherUnregistering;
89eb8eb9
DN
273
274 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
e54af724 275
7fb5e59d 276 DBUG_ON(chctl_flags & XPC_CHCTL_CLOSEREPLY);
efdd06ed 277 goto out;
89eb8eb9 278 }
e54af724
DN
279
280 xpc_process_disconnect(ch, &irq_flags);
89eb8eb9
DN
281 }
282
7fb5e59d 283 if (chctl_flags & XPC_CHCTL_CLOSEREPLY) {
89eb8eb9 284
7fb5e59d
DN
285 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREPLY received from partid="
286 "%d, channel=%d\n", ch->partid, ch->number);
89eb8eb9
DN
287
288 if (ch->flags & XPC_C_DISCONNECTED) {
83469b55 289 DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING);
efdd06ed 290 goto out;
89eb8eb9
DN
291 }
292
293 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
e54af724
DN
294
295 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
7fb5e59d
DN
296 if (part->chctl.flags[ch_number] &
297 XPC_CHCTL_CLOSEREQUEST) {
298
299 DBUG_ON(ch->delayed_chctl_flags != 0);
300 spin_lock(&part->chctl_lock);
301 part->chctl.flags[ch_number] |=
302 XPC_CHCTL_CLOSEREPLY;
303 spin_unlock(&part->chctl_lock);
e54af724 304 }
efdd06ed 305 goto out;
e54af724 306 }
89eb8eb9
DN
307
308 ch->flags |= XPC_C_RCLOSEREPLY;
309
310 if (ch->flags & XPC_C_CLOSEREPLY) {
311 /* both sides have finished disconnecting */
312 xpc_process_disconnect(ch, &irq_flags);
313 }
314 }
315
7fb5e59d 316 if (chctl_flags & XPC_CHCTL_OPENREQUEST) {
89eb8eb9 317
bd3e64c1 318 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREQUEST (entry_size=%d, "
89eb8eb9 319 "local_nentries=%d) received from partid=%d, "
bd3e64c1 320 "channel=%d\n", args->entry_size, args->local_nentries,
89eb8eb9
DN
321 ch->partid, ch->number);
322
83469b55 323 if (part->act_state == XPC_P_AS_DEACTIVATING ||
35190506 324 (ch->flags & XPC_C_ROPENREQUEST)) {
efdd06ed 325 goto out;
e54af724
DN
326 }
327
328 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
7fb5e59d 329 ch->delayed_chctl_flags |= XPC_CHCTL_OPENREQUEST;
efdd06ed 330 goto out;
89eb8eb9
DN
331 }
332 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
35190506 333 XPC_C_OPENREQUEST)));
89eb8eb9 334 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
35190506 335 XPC_C_OPENREPLY | XPC_C_CONNECTED));
89eb8eb9
DN
336
337 /*
338 * The meaningful OPENREQUEST connection state fields are:
bd3e64c1 339 * entry_size = size of channel's messages in bytes
89eb8eb9
DN
340 * local_nentries = remote partition's local_nentries
341 */
bd3e64c1 342 if (args->entry_size == 0 || args->local_nentries == 0) {
e54af724 343 /* assume OPENREQUEST was delayed by mistake */
efdd06ed 344 goto out;
e54af724 345 }
89eb8eb9
DN
346
347 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
348 ch->remote_nentries = args->local_nentries;
349
89eb8eb9 350 if (ch->flags & XPC_C_OPENREQUEST) {
bd3e64c1 351 if (args->entry_size != ch->entry_size) {
65c17b80 352 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
35190506 353 &irq_flags);
efdd06ed 354 goto out;
89eb8eb9
DN
355 }
356 } else {
bd3e64c1 357 ch->entry_size = args->entry_size;
89eb8eb9
DN
358
359 XPC_SET_REASON(ch, 0, 0);
360 ch->flags &= ~XPC_C_DISCONNECTED;
361
362 atomic_inc(&part->nchannels_active);
363 }
364
365 xpc_process_connect(ch, &irq_flags);
366 }
367
7fb5e59d 368 if (chctl_flags & XPC_CHCTL_OPENREPLY) {
89eb8eb9 369
7fb5e59d
DN
370 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
371 "0x%lx, local_nentries=%d, remote_nentries=%d) "
372 "received from partid=%d, channel=%d\n",
a812dcc3
DN
373 args->local_msgqueue_pa, args->local_nentries,
374 args->remote_nentries, ch->partid, ch->number);
89eb8eb9 375
efdd06ed
RH
376 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
377 goto out;
378
e54af724 379 if (!(ch->flags & XPC_C_OPENREQUEST)) {
65c17b80 380 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
35190506 381 &irq_flags);
efdd06ed 382 goto out;
e54af724
DN
383 }
384
89eb8eb9
DN
385 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
386 DBUG_ON(ch->flags & XPC_C_CONNECTED);
387
388 /*
389 * The meaningful OPENREPLY connection state fields are:
390 * local_msgqueue_pa = physical address of remote
35190506 391 * partition's local_msgqueue
89eb8eb9
DN
392 * local_nentries = remote partition's local_nentries
393 * remote_nentries = remote partition's remote_nentries
394 */
395 DBUG_ON(args->local_msgqueue_pa == 0);
396 DBUG_ON(args->local_nentries == 0);
397 DBUG_ON(args->remote_nentries == 0);
398
a7665b0a
RH
399 ret = xpc_arch_ops.save_remote_msgqueue_pa(ch,
400 args->local_msgqueue_pa);
6f2584f4
JS
401 if (ret != xpSuccess) {
402 XPC_DISCONNECT_CHANNEL(ch, ret, &irq_flags);
efdd06ed 403 goto out;
6f2584f4 404 }
89eb8eb9 405 ch->flags |= XPC_C_ROPENREPLY;
89eb8eb9
DN
406
407 if (args->local_nentries < ch->remote_nentries) {
7fb5e59d 408 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
89eb8eb9
DN
409 "remote_nentries=%d, old remote_nentries=%d, "
410 "partid=%d, channel=%d\n",
411 args->local_nentries, ch->remote_nentries,
412 ch->partid, ch->number);
413
414 ch->remote_nentries = args->local_nentries;
415 }
416 if (args->remote_nentries < ch->local_nentries) {
7fb5e59d 417 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
89eb8eb9
DN
418 "local_nentries=%d, old local_nentries=%d, "
419 "partid=%d, channel=%d\n",
420 args->remote_nentries, ch->local_nentries,
421 ch->partid, ch->number);
422
423 ch->local_nentries = args->remote_nentries;
424 }
425
426 xpc_process_connect(ch, &irq_flags);
427 }
428
efdd06ed
RH
429 if (chctl_flags & XPC_CHCTL_OPENCOMPLETE) {
430
431 dev_dbg(xpc_chan, "XPC_CHCTL_OPENCOMPLETE received from "
432 "partid=%d, channel=%d\n", ch->partid, ch->number);
433
434 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
435 goto out;
436
437 if (!(ch->flags & XPC_C_OPENREQUEST) ||
438 !(ch->flags & XPC_C_OPENREPLY)) {
439 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
440 &irq_flags);
441 goto out;
442 }
443
444 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
445 DBUG_ON(!(ch->flags & XPC_C_ROPENREPLY));
446 DBUG_ON(!(ch->flags & XPC_C_CONNECTED));
447
448 ch->flags |= XPC_C_ROPENCOMPLETE;
449
450 xpc_process_connect(ch, &irq_flags);
451 create_kthread = 1;
452 }
453
454out:
89eb8eb9 455 spin_unlock_irqrestore(&ch->lock, irq_flags);
efdd06ed
RH
456
457 if (create_kthread)
458 xpc_create_kthreads(ch, 1, 0);
89eb8eb9
DN
459}
460
89eb8eb9
DN
461/*
462 * Attempt to establish a channel connection to a remote partition.
463 */
65c17b80 464static enum xp_retval
89eb8eb9
DN
465xpc_connect_channel(struct xpc_channel *ch)
466{
467 unsigned long irq_flags;
468 struct xpc_registration *registration = &xpc_registrations[ch->number];
469
2c2b94f9 470 if (mutex_trylock(&registration->mutex) == 0)
65c17b80 471 return xpRetry;
89eb8eb9
DN
472
473 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
f9e505a9 474 mutex_unlock(&registration->mutex);
65c17b80 475 return xpUnregistered;
89eb8eb9
DN
476 }
477
478 spin_lock_irqsave(&ch->lock, irq_flags);
479
480 DBUG_ON(ch->flags & XPC_C_CONNECTED);
481 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
482
483 if (ch->flags & XPC_C_DISCONNECTING) {
484 spin_unlock_irqrestore(&ch->lock, irq_flags);
f9e505a9 485 mutex_unlock(&registration->mutex);
89eb8eb9
DN
486 return ch->reason;
487 }
488
89eb8eb9
DN
489 /* add info from the channel connect registration to the channel */
490
491 ch->kthreads_assigned_limit = registration->assigned_limit;
492 ch->kthreads_idle_limit = registration->idle_limit;
493 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
494 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
495 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
496
497 ch->func = registration->func;
498 DBUG_ON(registration->func == NULL);
499 ch->key = registration->key;
500
501 ch->local_nentries = registration->nentries;
502
503 if (ch->flags & XPC_C_ROPENREQUEST) {
bd3e64c1 504 if (registration->entry_size != ch->entry_size) {
89eb8eb9
DN
505 /* the local and remote sides aren't the same */
506
507 /*
508 * Because XPC_DISCONNECT_CHANNEL() can block we're
509 * forced to up the registration sema before we unlock
510 * the channel lock. But that's okay here because we're
511 * done with the part that required the registration
512 * sema. XPC_DISCONNECT_CHANNEL() requires that the
513 * channel lock be locked and will unlock and relock
514 * the channel lock as needed.
515 */
f9e505a9 516 mutex_unlock(&registration->mutex);
65c17b80 517 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
35190506 518 &irq_flags);
89eb8eb9 519 spin_unlock_irqrestore(&ch->lock, irq_flags);
65c17b80 520 return xpUnequalMsgSizes;
89eb8eb9
DN
521 }
522 } else {
bd3e64c1 523 ch->entry_size = registration->entry_size;
89eb8eb9
DN
524
525 XPC_SET_REASON(ch, 0, 0);
526 ch->flags &= ~XPC_C_DISCONNECTED;
527
528 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
529 }
530
f9e505a9 531 mutex_unlock(&registration->mutex);
89eb8eb9 532
89eb8eb9
DN
533 /* initiate the connection */
534
535 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
a7665b0a 536 xpc_arch_ops.send_chctl_openrequest(ch, &irq_flags);
89eb8eb9
DN
537
538 xpc_process_connect(ch, &irq_flags);
539
540 spin_unlock_irqrestore(&ch->lock, irq_flags);
541
65c17b80 542 return xpSuccess;
89eb8eb9
DN
543}
544
89eb8eb9 545void
7fb5e59d 546xpc_process_sent_chctl_flags(struct xpc_partition *part)
89eb8eb9
DN
547{
548 unsigned long irq_flags;
7fb5e59d 549 union xpc_channel_ctl_flags chctl;
89eb8eb9
DN
550 struct xpc_channel *ch;
551 int ch_number;
a607c389 552 u32 ch_flags;
89eb8eb9 553
a7665b0a 554 chctl.all_flags = xpc_arch_ops.get_chctl_all_flags(part);
89eb8eb9
DN
555
556 /*
557 * Initiate channel connections for registered channels.
558 *
559 * For each connected channel that has pending messages activate idle
560 * kthreads and/or create new kthreads as needed.
561 */
562
563 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
564 ch = &part->channels[ch_number];
565
89eb8eb9 566 /*
7fb5e59d 567 * Process any open or close related chctl flags, and then deal
89eb8eb9
DN
568 * with connecting or disconnecting the channel as required.
569 */
570
7fb5e59d
DN
571 if (chctl.flags[ch_number] & XPC_OPENCLOSE_CHCTL_FLAGS) {
572 xpc_process_openclose_chctl_flags(part, ch_number,
573 chctl.flags[ch_number]);
574 }
89eb8eb9 575
a607c389 576 ch_flags = ch->flags; /* need an atomic snapshot of flags */
89eb8eb9 577
a607c389 578 if (ch_flags & XPC_C_DISCONNECTING) {
89eb8eb9
DN
579 spin_lock_irqsave(&ch->lock, irq_flags);
580 xpc_process_disconnect(ch, &irq_flags);
581 spin_unlock_irqrestore(&ch->lock, irq_flags);
582 continue;
583 }
584
83469b55 585 if (part->act_state == XPC_P_AS_DEACTIVATING)
89eb8eb9 586 continue;
89eb8eb9 587
a607c389
DN
588 if (!(ch_flags & XPC_C_CONNECTED)) {
589 if (!(ch_flags & XPC_C_OPENREQUEST)) {
590 DBUG_ON(ch_flags & XPC_C_SETUP);
35190506 591 (void)xpc_connect_channel(ch);
89eb8eb9
DN
592 }
593 continue;
594 }
595
89eb8eb9 596 /*
7fb5e59d
DN
597 * Process any message related chctl flags, this may involve
598 * the activation of kthreads to deliver any pending messages
599 * sent from the other partition.
89eb8eb9
DN
600 */
601
7fb5e59d 602 if (chctl.flags[ch_number] & XPC_MSG_CHCTL_FLAGS)
a7665b0a 603 xpc_arch_ops.process_msg_chctl_flags(part, ch_number);
89eb8eb9
DN
604 }
605}
606
89eb8eb9 607/*
a607c389
DN
608 * XPC's heartbeat code calls this function to inform XPC that a partition is
609 * going down. XPC responds by tearing down the XPartition Communication
89eb8eb9
DN
610 * infrastructure used for the just downed partition.
611 *
612 * XPC's heartbeat code will never call this function and xpc_partition_up()
613 * at the same time. Nor will it ever make multiple calls to either function
614 * at the same time.
615 */
616void
65c17b80 617xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
89eb8eb9
DN
618{
619 unsigned long irq_flags;
620 int ch_number;
621 struct xpc_channel *ch;
622
89eb8eb9
DN
623 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
624 XPC_PARTID(part), reason);
625
626 if (!xpc_part_ref(part)) {
627 /* infrastructure for this partition isn't currently set up */
628 return;
629 }
630
a607c389 631 /* disconnect channels associated with the partition going down */
89eb8eb9
DN
632
633 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
634 ch = &part->channels[ch_number];
635
89eb8eb9
DN
636 xpc_msgqueue_ref(ch);
637 spin_lock_irqsave(&ch->lock, irq_flags);
638
639 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
640
641 spin_unlock_irqrestore(&ch->lock, irq_flags);
642 xpc_msgqueue_deref(ch);
643 }
644
645 xpc_wakeup_channel_mgr(part);
646
647 xpc_part_deref(part);
648}
649
89eb8eb9
DN
650/*
651 * Called by XP at the time of channel connection registration to cause
652 * XPC to establish connections to all currently active partitions.
653 */
654void
655xpc_initiate_connect(int ch_number)
656{
64d032ba 657 short partid;
89eb8eb9
DN
658 struct xpc_partition *part;
659 struct xpc_channel *ch;
660
bc63d387 661 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
89eb8eb9 662
bc63d387 663 for (partid = 0; partid < xp_max_npartitions; partid++) {
89eb8eb9
DN
664 part = &xpc_partitions[partid];
665
666 if (xpc_part_ref(part)) {
667 ch = &part->channels[ch_number];
668
e54af724
DN
669 /*
670 * Initiate the establishment of a connection on the
671 * newly registered channel to the remote partition.
672 */
673 xpc_wakeup_channel_mgr(part);
89eb8eb9
DN
674 xpc_part_deref(part);
675 }
676 }
677}
678
89eb8eb9
DN
679void
680xpc_connected_callout(struct xpc_channel *ch)
681{
89eb8eb9
DN
682 /* let the registerer know that a connection has been established */
683
684 if (ch->func != NULL) {
65c17b80 685 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
89eb8eb9
DN
686 "partid=%d, channel=%d\n", ch->partid, ch->number);
687
65c17b80 688 ch->func(xpConnected, ch->partid, ch->number,
35190506 689 (void *)(u64)ch->local_nentries, ch->key);
89eb8eb9 690
65c17b80 691 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
89eb8eb9
DN
692 "partid=%d, channel=%d\n", ch->partid, ch->number);
693 }
89eb8eb9
DN
694}
695
89eb8eb9
DN
696/*
697 * Called by XP at the time of channel connection unregistration to cause
698 * XPC to teardown all current connections for the specified channel.
699 *
700 * Before returning xpc_initiate_disconnect() will wait until all connections
701 * on the specified channel have been closed/torndown. So the caller can be
702 * assured that they will not be receiving any more callouts from XPC to the
703 * function they registered via xpc_connect().
704 *
705 * Arguments:
706 *
707 * ch_number - channel # to unregister.
708 */
709void
710xpc_initiate_disconnect(int ch_number)
711{
712 unsigned long irq_flags;
64d032ba 713 short partid;
89eb8eb9
DN
714 struct xpc_partition *part;
715 struct xpc_channel *ch;
716
bc63d387 717 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
89eb8eb9
DN
718
719 /* initiate the channel disconnect for every active partition */
bc63d387 720 for (partid = 0; partid < xp_max_npartitions; partid++) {
89eb8eb9
DN
721 part = &xpc_partitions[partid];
722
723 if (xpc_part_ref(part)) {
724 ch = &part->channels[ch_number];
725 xpc_msgqueue_ref(ch);
726
727 spin_lock_irqsave(&ch->lock, irq_flags);
728
a607c389
DN
729 if (!(ch->flags & XPC_C_DISCONNECTED)) {
730 ch->flags |= XPC_C_WDISCONNECT;
731
65c17b80 732 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
35190506 733 &irq_flags);
a607c389 734 }
89eb8eb9
DN
735
736 spin_unlock_irqrestore(&ch->lock, irq_flags);
737
738 xpc_msgqueue_deref(ch);
739 xpc_part_deref(part);
740 }
741 }
742
743 xpc_disconnect_wait(ch_number);
744}
745
89eb8eb9
DN
746/*
747 * To disconnect a channel, and reflect it back to all who may be waiting.
748 *
a607c389
DN
749 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
750 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
751 * xpc_disconnect_wait().
89eb8eb9
DN
752 *
753 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
754 */
755void
756xpc_disconnect_channel(const int line, struct xpc_channel *ch,
65c17b80 757 enum xp_retval reason, unsigned long *irq_flags)
89eb8eb9 758{
a607c389 759 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
89eb8eb9 760
89eb8eb9
DN
761 DBUG_ON(!spin_is_locked(&ch->lock));
762
2c2b94f9 763 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
89eb8eb9 764 return;
2c2b94f9 765
89eb8eb9
DN
766 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
767
768 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
769 reason, line, ch->partid, ch->number);
770
771 XPC_SET_REASON(ch, reason, line);
772
a607c389 773 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
89eb8eb9
DN
774 /* some of these may not have been set */
775 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
35190506
DN
776 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
777 XPC_C_CONNECTING | XPC_C_CONNECTED);
89eb8eb9 778
a7665b0a 779 xpc_arch_ops.send_chctl_closerequest(ch, irq_flags);
89eb8eb9 780
2c2b94f9 781 if (channel_was_connected)
89eb8eb9 782 ch->flags |= XPC_C_WASCONNECTED;
89eb8eb9 783
a607c389
DN
784 spin_unlock_irqrestore(&ch->lock, *irq_flags);
785
786 /* wake all idle kthreads so they can exit */
89eb8eb9 787 if (atomic_read(&ch->kthreads_idle) > 0) {
89eb8eb9 788 wake_up_all(&ch->idle_wq);
a460ef8d
DN
789
790 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
35190506 791 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
65c17b80 792 /* start a kthread that will do the xpDisconnecting callout */
a460ef8d 793 xpc_create_kthreads(ch, 1, 1);
89eb8eb9
DN
794 }
795
89eb8eb9 796 /* wake those waiting to allocate an entry from the local msg queue */
2c2b94f9 797 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
89eb8eb9 798 wake_up(&ch->msg_allocate_wq);
89eb8eb9 799
89eb8eb9
DN
800 spin_lock_irqsave(&ch->lock, *irq_flags);
801}
802
89eb8eb9 803void
65c17b80 804xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
89eb8eb9
DN
805{
806 /*
a607c389 807 * Let the channel's registerer know that the channel is being
89eb8eb9 808 * disconnected. We don't want to do this if the registerer was never
a607c389 809 * informed of a connection being made.
89eb8eb9
DN
810 */
811
812 if (ch->func != NULL) {
246c7e33
DN
813 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
814 "channel=%d\n", reason, ch->partid, ch->number);
89eb8eb9 815
246c7e33 816 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
89eb8eb9 817
246c7e33
DN
818 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
819 "channel=%d\n", reason, ch->partid, ch->number);
89eb8eb9
DN
820 }
821}
822
89eb8eb9
DN
823/*
824 * Wait for a message entry to become available for the specified channel,
825 * but don't wait any longer than 1 jiffy.
826 */
33ba3c77 827enum xp_retval
89eb8eb9
DN
828xpc_allocate_msg_wait(struct xpc_channel *ch)
829{
65c17b80 830 enum xp_retval ret;
89eb8eb9 831
89eb8eb9 832 if (ch->flags & XPC_C_DISCONNECTING) {
65c17b80 833 DBUG_ON(ch->reason == xpInterrupted);
89eb8eb9
DN
834 return ch->reason;
835 }
836
837 atomic_inc(&ch->n_on_msg_allocate_wq);
838 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
839 atomic_dec(&ch->n_on_msg_allocate_wq);
840
841 if (ch->flags & XPC_C_DISCONNECTING) {
842 ret = ch->reason;
65c17b80 843 DBUG_ON(ch->reason == xpInterrupted);
89eb8eb9 844 } else if (ret == 0) {
65c17b80 845 ret = xpTimeout;
89eb8eb9 846 } else {
65c17b80 847 ret = xpInterrupted;
89eb8eb9
DN
848 }
849
850 return ret;
851}
852
89eb8eb9 853/*
97bf1aa1
DN
854 * Send a message that contains the user's payload on the specified channel
855 * connected to the specified partition.
89eb8eb9 856 *
97bf1aa1
DN
857 * NOTE that this routine can sleep waiting for a message entry to become
858 * available. To not sleep, pass in the XPC_NOWAIT flag.
89eb8eb9 859 *
97bf1aa1
DN
860 * Once sent, this routine will not wait for the message to be received, nor
861 * will notification be given when it does happen.
89eb8eb9
DN
862 *
863 * Arguments:
864 *
865 * partid - ID of partition to which the channel is connected.
866 * ch_number - channel # to send message on.
97bf1aa1
DN
867 * flags - see xp.h for valid flags.
868 * payload - pointer to the payload which is to be sent.
869 * payload_size - size of the payload in bytes.
89eb8eb9 870 */
65c17b80 871enum xp_retval
97bf1aa1
DN
872xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
873 u16 payload_size)
89eb8eb9
DN
874{
875 struct xpc_partition *part = &xpc_partitions[partid];
97bf1aa1 876 enum xp_retval ret = xpUnknownReason;
89eb8eb9 877
97bf1aa1 878 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
89eb8eb9
DN
879 partid, ch_number);
880
bc63d387 881 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
89eb8eb9 882 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
97bf1aa1 883 DBUG_ON(payload == NULL);
89eb8eb9 884
97bf1aa1 885 if (xpc_part_ref(part)) {
a7665b0a
RH
886 ret = xpc_arch_ops.send_payload(&part->channels[ch_number],
887 flags, payload, payload_size, 0, NULL, NULL);
97bf1aa1
DN
888 xpc_part_deref(part);
889 }
89eb8eb9
DN
890
891 return ret;
892}
893
89eb8eb9 894/*
97bf1aa1
DN
895 * Send a message that contains the user's payload on the specified channel
896 * connected to the specified partition.
89eb8eb9 897 *
97bf1aa1
DN
898 * NOTE that this routine can sleep waiting for a message entry to become
899 * available. To not sleep, pass in the XPC_NOWAIT flag.
900 *
901 * This routine will not wait for the message to be sent or received.
89eb8eb9
DN
902 *
903 * Once the remote end of the channel has received the message, the function
904 * passed as an argument to xpc_initiate_send_notify() will be called. This
905 * allows the sender to free up or re-use any buffers referenced by the
906 * message, but does NOT mean the message has been processed at the remote
907 * end by a receiver.
908 *
909 * If this routine returns an error, the caller's function will NOT be called.
910 *
89eb8eb9
DN
911 * Arguments:
912 *
913 * partid - ID of partition to which the channel is connected.
914 * ch_number - channel # to send message on.
97bf1aa1
DN
915 * flags - see xp.h for valid flags.
916 * payload - pointer to the payload which is to be sent.
917 * payload_size - size of the payload in bytes.
89eb8eb9
DN
918 * func - function to call with asynchronous notification of message
919 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
920 * key - user-defined key to be passed to the function when it's called.
921 */
65c17b80 922enum xp_retval
97bf1aa1
DN
923xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
924 u16 payload_size, xpc_notify_func func, void *key)
89eb8eb9
DN
925{
926 struct xpc_partition *part = &xpc_partitions[partid];
97bf1aa1 927 enum xp_retval ret = xpUnknownReason;
89eb8eb9 928
97bf1aa1 929 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
89eb8eb9
DN
930 partid, ch_number);
931
bc63d387 932 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
89eb8eb9 933 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
97bf1aa1 934 DBUG_ON(payload == NULL);
89eb8eb9
DN
935 DBUG_ON(func == NULL);
936
97bf1aa1 937 if (xpc_part_ref(part)) {
a7665b0a
RH
938 ret = xpc_arch_ops.send_payload(&part->channels[ch_number],
939 flags, payload, payload_size, XPC_N_CALL, func, key);
97bf1aa1
DN
940 xpc_part_deref(part);
941 }
89eb8eb9
DN
942 return ret;
943}
944
89eb8eb9 945/*
bd3e64c1 946 * Deliver a message's payload to its intended recipient.
89eb8eb9
DN
947 */
948void
bd3e64c1 949xpc_deliver_payload(struct xpc_channel *ch)
89eb8eb9 950{
bd3e64c1 951 void *payload;
89eb8eb9 952
a7665b0a 953 payload = xpc_arch_ops.get_deliverable_payload(ch);
bd3e64c1 954 if (payload != NULL) {
89eb8eb9
DN
955
956 /*
957 * This ref is taken to protect the payload itself from being
958 * freed before the user is finished with it, which the user
959 * indicates by calling xpc_initiate_received().
960 */
961 xpc_msgqueue_ref(ch);
962
963 atomic_inc(&ch->kthreads_active);
964
965 if (ch->func != NULL) {
bd3e64c1
DN
966 dev_dbg(xpc_chan, "ch->func() called, payload=0x%p "
967 "partid=%d channel=%d\n", payload, ch->partid,
89eb8eb9
DN
968 ch->number);
969
970 /* deliver the message to its intended recipient */
bd3e64c1
DN
971 ch->func(xpMsgReceived, ch->partid, ch->number, payload,
972 ch->key);
89eb8eb9 973
bd3e64c1
DN
974 dev_dbg(xpc_chan, "ch->func() returned, payload=0x%p "
975 "partid=%d channel=%d\n", payload, ch->partid,
89eb8eb9
DN
976 ch->number);
977 }
978
979 atomic_dec(&ch->kthreads_active);
980 }
981}
982
89eb8eb9 983/*
bd3e64c1 984 * Acknowledge receipt of a delivered message's payload.
89eb8eb9
DN
985 *
986 * This function, although called by users, does not call xpc_part_ref() to
987 * ensure that the partition infrastructure is in place. It relies on the
bd3e64c1 988 * fact that we called xpc_msgqueue_ref() in xpc_deliver_payload().
89eb8eb9
DN
989 *
990 * Arguments:
991 *
992 * partid - ID of partition to which the channel is connected.
993 * ch_number - channel # message received on.
994 * payload - pointer to the payload area allocated via
97bf1aa1 995 * xpc_initiate_send() or xpc_initiate_send_notify().
89eb8eb9
DN
996 */
997void
64d032ba 998xpc_initiate_received(short partid, int ch_number, void *payload)
89eb8eb9
DN
999{
1000 struct xpc_partition *part = &xpc_partitions[partid];
1001 struct xpc_channel *ch;
89eb8eb9 1002
bc63d387 1003 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
89eb8eb9
DN
1004 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1005
1006 ch = &part->channels[ch_number];
a7665b0a 1007 xpc_arch_ops.received_payload(ch, payload);
89eb8eb9 1008
bd3e64c1 1009 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_payload() */
89eb8eb9
DN
1010 xpc_msgqueue_deref(ch);
1011}