Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Greybus operations |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * |
| 6 | * Released under the GPLv2 only. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/workqueue.h> |
| 13 | |
| 14 | #include "greybus.h" |
| 15 | |
| 16 | /* |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 17 | * The top bit of the type in an operation message header indicates |
| 18 | * whether the message is a request (bit clear) or response (bit set) |
| 19 | */ |
| 20 | #define GB_OPERATION_TYPE_RESPONSE 0x80 |
| 21 | |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 22 | #define CONNECTION_TIMEOUT_DEFAULT 1000 /* milliseconds */ |
| 23 | |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 24 | /* |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 25 | * XXX This needs to be coordinated with host driver parameters |
| 26 | */ |
| 27 | #define GB_OPERATION_MESSAGE_SIZE_MAX 4096 |
| 28 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 29 | static struct kmem_cache *gb_operation_cache; |
| 30 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 31 | /* Workqueue to handle Greybus operation completions. */ |
| 32 | static struct workqueue_struct *gb_operation_recv_workqueue; |
| 33 | |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 34 | /* |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 35 | * All operation messages (both requests and responses) begin with |
| 36 | * a common header that encodes the size of the data (header |
| 37 | * included). This header also contains a unique identifier, which |
| 38 | * is used to keep track of in-flight operations. Finally, the |
| 39 | * header contains a operation type field, whose interpretation is |
| 40 | * dependent on what type of device lies on the other end of the |
| 41 | * connection. Response messages are distinguished from request |
| 42 | * messages by setting the high bit (0x80) in the operation type |
| 43 | * value. |
| 44 | * |
| 45 | * The wire format for all numeric fields in the header is little |
| 46 | * endian. Any operation-specific data begins immediately after the |
| 47 | * header, and is 64-bit aligned. |
| 48 | */ |
| 49 | struct gb_operation_msg_hdr { |
| 50 | __le16 size; /* Size in bytes of header + payload */ |
| 51 | __le16 id; /* Operation unique id */ |
| 52 | __u8 type; /* E.g GB_I2C_TYPE_* or GB_GPIO_TYPE_* */ |
| 53 | /* 3 bytes pad, must be zero (ignore when read) */ |
| 54 | } __aligned(sizeof(u64)); |
| 55 | |
| 56 | /* XXX Could be per-host device, per-module, or even per-connection */ |
| 57 | static DEFINE_SPINLOCK(gb_operations_lock); |
| 58 | |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 59 | static void gb_operation_insert(struct gb_operation *operation) |
| 60 | { |
| 61 | struct gb_connection *connection = operation->connection; |
| 62 | struct rb_root *root = &connection->pending; |
| 63 | struct rb_node *node = &operation->node; |
| 64 | struct rb_node **link = &root->rb_node; |
| 65 | struct rb_node *above = NULL; |
| 66 | struct gb_operation_msg_hdr *header; |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 67 | unsigned long timeout; |
| 68 | bool start_timer; |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 69 | __le16 wire_id; |
| 70 | |
| 71 | /* |
| 72 | * Assign the operation's id, and store it in the header of |
| 73 | * both request and response message headers. |
| 74 | */ |
| 75 | operation->id = gb_connection_operation_id(connection); |
| 76 | wire_id = cpu_to_le16(operation->id); |
| 77 | header = operation->request->transfer_buffer; |
| 78 | header->id = wire_id; |
| 79 | |
| 80 | /* OK, insert the operation into its connection's tree */ |
| 81 | spin_lock_irq(&gb_operations_lock); |
| 82 | |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 83 | /* |
| 84 | * We impose a time limit for requests to complete. If |
| 85 | * there are no requests pending there is no need for a |
| 86 | * timer. So if this will be the only one in flight we'll |
| 87 | * need to start the timer. Otherwise we just update the |
| 88 | * existing one to give this request a full timeout period |
| 89 | * to complete. |
| 90 | */ |
| 91 | start_timer = RB_EMPTY_ROOT(root); |
| 92 | |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 93 | while (*link) { |
| 94 | struct gb_operation *other; |
| 95 | |
| 96 | above = *link; |
| 97 | other = rb_entry(above, struct gb_operation, node); |
| 98 | header = other->request->transfer_buffer; |
| 99 | if (other->id > operation->id) |
| 100 | link = &above->rb_left; |
| 101 | else if (other->id < operation->id) |
| 102 | link = &above->rb_right; |
| 103 | } |
| 104 | rb_link_node(node, above, link); |
| 105 | rb_insert_color(node, root); |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 106 | spin_unlock_irq(&gb_operations_lock); |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 107 | |
| 108 | timeout = msecs_to_jiffies(CONNECTION_TIMEOUT_DEFAULT); |
| 109 | if (start_timer) |
| 110 | schedule_delayed_work(&connection->timeout_work, timeout); |
| 111 | else |
| 112 | mod_delayed_work(system_wq, &connection->timeout_work, timeout); |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void gb_operation_remove(struct gb_operation *operation) |
| 116 | { |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 117 | struct gb_connection *connection = operation->connection; |
| 118 | bool last_pending; |
| 119 | |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 120 | spin_lock_irq(&gb_operations_lock); |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 121 | rb_erase(&operation->node, &connection->pending); |
| 122 | last_pending = RB_EMPTY_ROOT(&connection->pending); |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 123 | spin_unlock_irq(&gb_operations_lock); |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * If there are no more pending requests, we can stop the |
| 127 | * timeout timer. |
| 128 | */ |
| 129 | if (last_pending) |
| 130 | cancel_delayed_work(&connection->timeout_work); |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static struct gb_operation * |
| 134 | gb_operation_find(struct gb_connection *connection, u16 id) |
| 135 | { |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 136 | struct gb_operation *operation = NULL; |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 137 | struct rb_node *node; |
| 138 | bool found = false; |
| 139 | |
| 140 | spin_lock_irq(&gb_operations_lock); |
| 141 | node = connection->pending.rb_node; |
| 142 | while (node && !found) { |
| 143 | operation = rb_entry(node, struct gb_operation, node); |
| 144 | if (operation->id > id) |
| 145 | node = node->rb_left; |
| 146 | else if (operation->id < id) |
| 147 | node = node->rb_right; |
| 148 | else |
| 149 | found = true; |
| 150 | } |
| 151 | spin_unlock_irq(&gb_operations_lock); |
| 152 | |
| 153 | return found ? operation : NULL; |
| 154 | } |
| 155 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 156 | /* |
| 157 | * An operations's response message has arrived. If no callback was |
| 158 | * supplied it was submitted for asynchronous completion, so we notify |
| 159 | * any waiters. Otherwise we assume calling the completion is enough |
| 160 | * and nobody else will be waiting. |
| 161 | */ |
| 162 | void gb_operation_complete(struct gb_operation *operation) |
| 163 | { |
| 164 | if (operation->callback) |
| 165 | operation->callback(operation); |
| 166 | else |
| 167 | complete_all(&operation->completion); |
| 168 | } |
| 169 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 170 | /* Wait for a submitted operation to complete */ |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 171 | int gb_operation_wait(struct gb_operation *operation) |
| 172 | { |
| 173 | int ret; |
| 174 | |
| 175 | ret = wait_for_completion_interruptible(&operation->completion); |
| 176 | /* If interrupted, cancel the in-flight buffer */ |
| 177 | if (ret < 0) |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 178 | ret = greybus_kill_gbuf(operation->request); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 179 | return ret; |
| 180 | |
| 181 | } |
| 182 | |
Alex Elder | ed8800d | 2014-10-16 06:35:38 -0500 | [diff] [blame] | 183 | /* |
| 184 | * This handler is used if no operation response messages are ever |
| 185 | * expected for a given protocol. |
| 186 | */ |
| 187 | static void gb_operation_recv_none(struct gb_operation *operation) |
| 188 | { |
| 189 | /* Nothing to do! */ |
| 190 | } |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 191 | |
| 192 | typedef void (*gb_operation_recv_handler)(struct gb_operation *operation); |
| 193 | static gb_operation_recv_handler gb_operation_recv_handlers[] = { |
| 194 | [GREYBUS_PROTOCOL_CONTROL] = NULL, |
| 195 | [GREYBUS_PROTOCOL_AP] = NULL, |
| 196 | [GREYBUS_PROTOCOL_GPIO] = NULL, |
Alex Elder | ed8800d | 2014-10-16 06:35:38 -0500 | [diff] [blame] | 197 | [GREYBUS_PROTOCOL_I2C] = gb_operation_recv_none, |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 198 | [GREYBUS_PROTOCOL_UART] = NULL, |
| 199 | [GREYBUS_PROTOCOL_HID] = NULL, |
Greg Kroah-Hartman | 2bb7eae | 2014-10-20 15:24:57 +0800 | [diff] [blame] | 200 | [GREYBUS_PROTOCOL_BATTERY] = gb_operation_recv_none, |
Greg Kroah-Hartman | 42d4a22 | 2014-10-20 16:02:56 +0800 | [diff] [blame] | 201 | [GREYBUS_PROTOCOL_LED] = NULL, |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 202 | [GREYBUS_PROTOCOL_VENDOR] = NULL, |
| 203 | }; |
| 204 | |
| 205 | static void gb_operation_request_handle(struct gb_operation *operation) |
| 206 | { |
| 207 | u8 protocol = operation->connection->protocol; |
| 208 | |
| 209 | /* Subtract one from array size to stay within u8 range */ |
| 210 | if (protocol <= (u8)(ARRAY_SIZE(gb_operation_recv_handlers) - 1)) { |
| 211 | gb_operation_recv_handler handler; |
| 212 | |
| 213 | handler = gb_operation_recv_handlers[protocol]; |
| 214 | if (handler) { |
| 215 | handler(operation); /* Handle the request */ |
| 216 | return; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | gb_connection_err(operation->connection, "unrecognized protocol %u\n", |
| 221 | (unsigned int)protocol); |
| 222 | operation->result = GB_OP_PROTOCOL_BAD; |
| 223 | gb_operation_complete(operation); |
| 224 | } |
| 225 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 226 | /* |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 227 | * Either this operation contains an incoming request, or its |
| 228 | * response has arrived. An incoming request will have a null |
| 229 | * response buffer pointer (it is the responsibility of the request |
| 230 | * handler to allocate and fill in the response buffer). |
| 231 | */ |
| 232 | static void gb_operation_recv_work(struct work_struct *recv_work) |
| 233 | { |
| 234 | struct gb_operation *operation; |
| 235 | bool incoming_request; |
| 236 | |
| 237 | operation = container_of(recv_work, struct gb_operation, recv_work); |
| 238 | incoming_request = operation->response == NULL; |
| 239 | if (incoming_request) |
| 240 | gb_operation_request_handle(operation); |
| 241 | gb_operation_complete(operation); |
| 242 | |
| 243 | /* We're finished with the buffer we read into */ |
| 244 | if (incoming_request) |
| 245 | greybus_gbuf_finished(operation->request); |
| 246 | else |
| 247 | greybus_gbuf_finished(operation->response); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Buffer completion function. We get notified whenever any buffer |
| 252 | * completes. For outbound messages, this tells us that the message |
| 253 | * has been sent. For inbound messages, it means the data has |
| 254 | * landed in the buffer and is ready to be processed. |
| 255 | * |
| 256 | * Either way, we don't do anything. We don't really care when an |
| 257 | * outbound message has been sent, and for incoming messages we |
| 258 | * we'll be done with everything we need to do before we mark it |
| 259 | * finished. |
| 260 | * |
Alex Elder | f012a52 | 2014-10-17 21:03:49 -0500 | [diff] [blame] | 261 | * XXX We may want to record that a request is (or is no longer) in flight. |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 262 | */ |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 263 | static void gb_operation_gbuf_complete(struct gbuf *gbuf) |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 264 | { |
Alex Elder | f012a52 | 2014-10-17 21:03:49 -0500 | [diff] [blame] | 265 | if (gbuf->status) { |
| 266 | struct gb_operation *operation = gbuf->context; |
| 267 | struct gb_operation_msg_hdr *header; |
| 268 | int id; |
| 269 | int type; |
| 270 | |
| 271 | if (gbuf == operation->request) |
| 272 | header = operation->request_payload; |
| 273 | else if (gbuf == operation->response) |
| 274 | header = operation->response_payload; |
| 275 | else |
| 276 | header = NULL; |
| 277 | id = header ? (int)header->id : -1; |
| 278 | type = header ? (int)header->type : -1; |
| 279 | |
| 280 | gb_connection_err(operation->connection, |
| 281 | "operation %d type %d gbuf error %d", |
| 282 | id, type, gbuf->status); |
| 283 | } |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 284 | return; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 288 | * Allocate a buffer to be used for an operation request or response |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 289 | * message. For outgoing messages, both types of message contain a |
| 290 | * common header, which is filled in here. Incoming requests or |
| 291 | * responses also contain the same header, but there's no need to |
| 292 | * initialize it here (it'll be overwritten by the incoming |
| 293 | * message). |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 294 | */ |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 295 | struct gbuf *gb_operation_gbuf_create(struct gb_operation *operation, |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 296 | u8 type, size_t size, bool data_out) |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 297 | { |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 298 | struct gb_connection *connection = operation->connection; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 299 | struct gb_operation_msg_hdr *header; |
| 300 | struct gbuf *gbuf; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 301 | gfp_t gfp_flags = data_out ? GFP_KERNEL : GFP_ATOMIC; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 302 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 303 | size += sizeof(*header); |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 304 | gbuf = greybus_alloc_gbuf(connection, gb_operation_gbuf_complete, |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 305 | size, data_out, gfp_flags, operation); |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 306 | if (!gbuf) |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 307 | return NULL; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 308 | |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 309 | /* Fill in the header structure */ |
| 310 | header = (struct gb_operation_msg_hdr *)gbuf->transfer_buffer; |
Greg Kroah-Hartman | 322543a | 2014-10-02 21:25:21 -0700 | [diff] [blame] | 311 | header->size = cpu_to_le16(size); |
Alex Elder | b0b6575 | 2014-10-03 15:05:20 -0500 | [diff] [blame] | 312 | header->id = 0; /* Filled in when submitted */ |
| 313 | header->type = type; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 314 | |
| 315 | return gbuf; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Create a Greybus operation to be sent over the given connection. |
| 320 | * The request buffer will big enough for a payload of the given |
| 321 | * size. Outgoing requests must specify the size of the response |
| 322 | * buffer size, which must be sufficient to hold all expected |
| 323 | * response data. |
| 324 | * |
| 325 | * Incoming requests will supply a response size of 0, and in that |
| 326 | * case no response buffer is allocated. (A response always |
| 327 | * includes a status byte, so 0 is not a valid size.) Whatever |
| 328 | * handles the operation request is responsible for allocating the |
| 329 | * response buffer. |
| 330 | * |
| 331 | * Returns a pointer to the new operation or a null pointer if an |
| 332 | * error occurs. |
| 333 | */ |
| 334 | struct gb_operation *gb_operation_create(struct gb_connection *connection, |
| 335 | u8 type, size_t request_size, |
| 336 | size_t response_size) |
| 337 | { |
| 338 | struct gb_operation *operation; |
| 339 | gfp_t gfp_flags = response_size ? GFP_KERNEL : GFP_ATOMIC; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 340 | bool outgoing = response_size != 0; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 341 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 342 | operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags); |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 343 | if (!operation) |
| 344 | return NULL; |
| 345 | operation->connection = connection; /* XXX refcount? */ |
| 346 | |
| 347 | operation->request = gb_operation_gbuf_create(operation, type, |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 348 | request_size, |
| 349 | outgoing); |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 350 | if (!operation->request) |
| 351 | goto err_cache; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 352 | operation->request_payload = operation->request->transfer_buffer + |
| 353 | sizeof(struct gb_operation_msg_hdr); |
| 354 | /* We always use the full request buffer */ |
| 355 | operation->request->actual_length = request_size; |
| 356 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 357 | if (outgoing) { |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 358 | type |= GB_OPERATION_TYPE_RESPONSE; |
| 359 | operation->response = gb_operation_gbuf_create(operation, |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 360 | type, response_size, |
| 361 | false); |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 362 | if (!operation->response) |
| 363 | goto err_request; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 364 | operation->response_payload = |
| 365 | operation->response->transfer_buffer + |
| 366 | sizeof(struct gb_operation_msg_hdr); |
| 367 | } |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 368 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 369 | INIT_WORK(&operation->recv_work, gb_operation_recv_work); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 370 | operation->callback = NULL; /* set at submit time */ |
| 371 | init_completion(&operation->completion); |
| 372 | |
| 373 | spin_lock_irq(&gb_operations_lock); |
| 374 | list_add_tail(&operation->links, &connection->operations); |
| 375 | spin_unlock_irq(&gb_operations_lock); |
| 376 | |
| 377 | return operation; |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 378 | |
| 379 | err_request: |
| 380 | greybus_free_gbuf(operation->request); |
| 381 | err_cache: |
| 382 | kmem_cache_free(gb_operation_cache, operation); |
| 383 | |
| 384 | return NULL; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Destroy a previously created operation. |
| 389 | */ |
| 390 | void gb_operation_destroy(struct gb_operation *operation) |
| 391 | { |
| 392 | if (WARN_ON(!operation)) |
| 393 | return; |
| 394 | |
| 395 | /* XXX Make sure it's not in flight */ |
| 396 | spin_lock_irq(&gb_operations_lock); |
| 397 | list_del(&operation->links); |
| 398 | spin_unlock_irq(&gb_operations_lock); |
| 399 | |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 400 | greybus_free_gbuf(operation->response); |
| 401 | greybus_free_gbuf(operation->request); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 402 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 403 | kmem_cache_free(gb_operation_cache, operation); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 404 | } |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * Send an operation request message. The caller has filled in |
| 408 | * any payload so the request message is ready to go. If non-null, |
| 409 | * the callback function supplied will be called when the response |
| 410 | * message has arrived indicating the operation is complete. A null |
| 411 | * callback function is used for a synchronous request; return from |
| 412 | * this function won't occur until the operation is complete (or an |
| 413 | * interrupt occurs). |
| 414 | */ |
| 415 | int gb_operation_request_send(struct gb_operation *operation, |
| 416 | gb_operation_callback callback) |
| 417 | { |
| 418 | int ret; |
| 419 | |
| 420 | /* |
| 421 | * XXX |
| 422 | * I think the order of operations is going to be |
| 423 | * significant, and if so, we may need a mutex to surround |
| 424 | * setting the operation id and submitting the gbuf. |
| 425 | */ |
| 426 | operation->callback = callback; |
| 427 | gb_operation_insert(operation); |
| 428 | ret = greybus_submit_gbuf(operation->request, GFP_KERNEL); |
| 429 | if (ret) |
| 430 | return ret; |
| 431 | if (!callback) |
| 432 | ret = gb_operation_wait(operation); |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Send a response for an incoming operation request. |
| 439 | */ |
| 440 | int gb_operation_response_send(struct gb_operation *operation) |
| 441 | { |
| 442 | /* XXX |
| 443 | * Caller needs to have set operation->response->actual_length |
| 444 | */ |
| 445 | gb_operation_remove(operation); |
| 446 | gb_operation_destroy(operation); |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 451 | /* |
| 452 | * Handle data arriving on a connection. This is called in |
| 453 | * interrupt context, so just copy the incoming data into a buffer |
| 454 | * and do remaining handling via a work queue. |
| 455 | */ |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 456 | void gb_connection_operation_recv(struct gb_connection *connection, |
| 457 | void *data, size_t size) |
| 458 | { |
| 459 | struct gb_operation_msg_hdr *header; |
| 460 | struct gb_operation *operation; |
| 461 | struct gbuf *gbuf; |
| 462 | u16 msg_size; |
| 463 | |
| 464 | if (size > GB_OPERATION_MESSAGE_SIZE_MAX) { |
| 465 | gb_connection_err(connection, "message too big"); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | header = data; |
| 470 | msg_size = le16_to_cpu(header->size); |
| 471 | if (header->type & GB_OPERATION_TYPE_RESPONSE) { |
| 472 | u16 id = le16_to_cpu(header->id); |
| 473 | |
| 474 | operation = gb_operation_find(connection, id); |
| 475 | if (!operation) { |
| 476 | gb_connection_err(connection, "operation not found"); |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 477 | return; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 478 | } |
| 479 | gb_operation_remove(operation); |
| 480 | gbuf = operation->response; |
Alex Elder | bedfdf3 | 2014-10-17 05:18:22 -0500 | [diff] [blame] | 481 | gbuf->status = GB_OP_SUCCESS; /* If we got here we're good */ |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 482 | if (size > gbuf->transfer_buffer_length) { |
| 483 | gb_connection_err(connection, "recv buffer too small"); |
| 484 | return; |
| 485 | } |
| 486 | } else { |
| 487 | WARN_ON(msg_size != size); |
| 488 | operation = gb_operation_create(connection, header->type, |
| 489 | msg_size, 0); |
| 490 | if (!operation) { |
| 491 | gb_connection_err(connection, "can't create operation"); |
| 492 | return; |
| 493 | } |
| 494 | gbuf = operation->request; |
| 495 | } |
| 496 | |
| 497 | memcpy(gbuf->transfer_buffer, data, msg_size); |
| 498 | gbuf->actual_length = msg_size; |
| 499 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 500 | /* The rest will be handled in work queue context */ |
| 501 | queue_work(gb_operation_recv_workqueue, &operation->recv_work); |
| 502 | } |
| 503 | |
Alex Elder | e1158df | 2014-10-22 02:04:29 -0500 | [diff] [blame^] | 504 | /* |
| 505 | * Cancel an operation. |
| 506 | */ |
| 507 | void gb_operation_cancel(struct gb_operation *operation) |
| 508 | { |
| 509 | int ret; |
| 510 | |
| 511 | operation->canceled = true; |
| 512 | ret = greybus_kill_gbuf(operation->request); |
| 513 | if (ret) |
| 514 | pr_warn("error %d killing request gbuf\n", ret); |
| 515 | if (operation->response) { |
| 516 | ret = greybus_kill_gbuf(operation->response); |
| 517 | if (ret) |
| 518 | pr_warn("error %d killing response gbuf\n", ret); |
| 519 | } |
| 520 | } |
| 521 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 522 | int gb_operation_init(void) |
| 523 | { |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 524 | gb_operation_cache = kmem_cache_create("gb_operation_cache", |
| 525 | sizeof(struct gb_operation), 0, 0, NULL); |
| 526 | if (!gb_operation_cache) |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 527 | return -ENOMEM; |
| 528 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 529 | gb_operation_recv_workqueue = alloc_workqueue("greybus_recv", 0, 1); |
| 530 | if (!gb_operation_recv_workqueue) { |
| 531 | kmem_cache_destroy(gb_operation_cache); |
| 532 | gb_operation_cache = NULL; |
| 533 | return -ENOMEM; |
| 534 | } |
| 535 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | void gb_operation_exit(void) |
| 540 | { |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 541 | kmem_cache_destroy(gb_operation_cache); |
| 542 | gb_operation_cache = NULL; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 543 | destroy_workqueue(gb_operation_recv_workqueue); |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 544 | gb_operation_recv_workqueue = NULL; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 545 | } |