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