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