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