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