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 | |
Greg Kroah-Hartman | 708971e | 2014-10-27 15:40:09 +0800 | [diff] [blame] | 22 | #define OPERATION_TIMEOUT_DEFAULT 1000 /* milliseconds */ |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 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 |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 26 | * XXX May need to reduce to allow for message header within a page |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 27 | */ |
| 28 | #define GB_OPERATION_MESSAGE_SIZE_MAX 4096 |
| 29 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 30 | static struct kmem_cache *gb_operation_cache; |
| 31 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 32 | /* Workqueue to handle Greybus operation completions. */ |
| 33 | static struct workqueue_struct *gb_operation_recv_workqueue; |
| 34 | |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 35 | /* |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 36 | * All operation messages (both requests and responses) begin with |
| 37 | * a common header that encodes the size of the data (header |
| 38 | * included). This header also contains a unique identifier, which |
| 39 | * is used to keep track of in-flight operations. Finally, the |
| 40 | * header contains a operation type field, whose interpretation is |
| 41 | * dependent on what type of device lies on the other end of the |
| 42 | * connection. Response messages are distinguished from request |
| 43 | * messages by setting the high bit (0x80) in the operation type |
| 44 | * value. |
| 45 | * |
| 46 | * The wire format for all numeric fields in the header is little |
| 47 | * endian. Any operation-specific data begins immediately after the |
| 48 | * header, and is 64-bit aligned. |
| 49 | */ |
| 50 | struct gb_operation_msg_hdr { |
| 51 | __le16 size; /* Size in bytes of header + payload */ |
| 52 | __le16 id; /* Operation unique id */ |
| 53 | __u8 type; /* E.g GB_I2C_TYPE_* or GB_GPIO_TYPE_* */ |
| 54 | /* 3 bytes pad, must be zero (ignore when read) */ |
| 55 | } __aligned(sizeof(u64)); |
| 56 | |
| 57 | /* XXX Could be per-host device, per-module, or even per-connection */ |
| 58 | static DEFINE_SPINLOCK(gb_operations_lock); |
| 59 | |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 60 | static void gb_pending_operation_insert(struct gb_operation *operation) |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 61 | { |
| 62 | struct gb_connection *connection = operation->connection; |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 63 | struct gb_operation_msg_hdr *header; |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 64 | |
Alex Elder | 360a877 | 2014-11-12 15:17:54 -0600 | [diff] [blame] | 65 | /* |
| 66 | * Assign the operation's id and move it into its |
| 67 | * connection's pending list. |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 68 | */ |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 69 | spin_lock_irq(&gb_operations_lock); |
Alex Elder | 360a877 | 2014-11-12 15:17:54 -0600 | [diff] [blame] | 70 | operation->id = ++connection->op_cycle; |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 71 | list_move_tail(&operation->links, &connection->pending); |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 72 | spin_unlock_irq(&gb_operations_lock); |
Alex Elder | 360a877 | 2014-11-12 15:17:54 -0600 | [diff] [blame] | 73 | |
| 74 | /* Store the operation id in the request header */ |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 75 | header = operation->request.gbuf.transfer_buffer; |
Alex Elder | 360a877 | 2014-11-12 15:17:54 -0600 | [diff] [blame] | 76 | header->id = cpu_to_le16(operation->id); |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 79 | static void gb_pending_operation_remove(struct gb_operation *operation) |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 80 | { |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 81 | struct gb_connection *connection = operation->connection; |
Alex Elder | e816e37 | 2014-10-22 02:04:28 -0500 | [diff] [blame] | 82 | |
Greg Kroah-Hartman | 708971e | 2014-10-27 15:40:09 +0800 | [diff] [blame] | 83 | /* Take us off of the list of pending operations */ |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 84 | spin_lock_irq(&gb_operations_lock); |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 85 | list_move_tail(&operation->links, &connection->operations); |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 86 | spin_unlock_irq(&gb_operations_lock); |
| 87 | } |
| 88 | |
| 89 | static struct gb_operation * |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 90 | gb_pending_operation_find(struct gb_connection *connection, u16 id) |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 91 | { |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 92 | struct gb_operation *operation; |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 93 | bool found = false; |
| 94 | |
| 95 | spin_lock_irq(&gb_operations_lock); |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 96 | list_for_each_entry(operation, &connection->pending, links) |
| 97 | if (operation->id == id) { |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 98 | found = true; |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 99 | break; |
| 100 | } |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 101 | spin_unlock_irq(&gb_operations_lock); |
| 102 | |
| 103 | return found ? operation : NULL; |
| 104 | } |
| 105 | |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 106 | static int greybus_submit_gbuf(struct gbuf *gbuf, gfp_t gfp_mask) |
| 107 | { |
| 108 | gbuf->status = -EINPROGRESS; |
| 109 | |
| 110 | return gbuf->hd->driver->submit_gbuf(gbuf, gfp_mask); |
| 111 | } |
| 112 | |
| 113 | static void greybus_kill_gbuf(struct gbuf *gbuf) |
| 114 | { |
| 115 | if (gbuf->status != -EINPROGRESS) |
| 116 | return; |
| 117 | |
| 118 | gbuf->hd->driver->kill_gbuf(gbuf); |
| 119 | } |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 120 | /* |
| 121 | * An operations's response message has arrived. If no callback was |
| 122 | * supplied it was submitted for asynchronous completion, so we notify |
| 123 | * any waiters. Otherwise we assume calling the completion is enough |
| 124 | * and nobody else will be waiting. |
| 125 | */ |
Greg Kroah-Hartman | 708971e | 2014-10-27 15:40:09 +0800 | [diff] [blame] | 126 | static void gb_operation_complete(struct gb_operation *operation) |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 127 | { |
| 128 | if (operation->callback) |
| 129 | operation->callback(operation); |
| 130 | else |
| 131 | complete_all(&operation->completion); |
| 132 | } |
| 133 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 134 | /* Wait for a submitted operation to complete */ |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 135 | int gb_operation_wait(struct gb_operation *operation) |
| 136 | { |
| 137 | int ret; |
| 138 | |
| 139 | ret = wait_for_completion_interruptible(&operation->completion); |
| 140 | /* If interrupted, cancel the in-flight buffer */ |
| 141 | if (ret < 0) |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 142 | greybus_kill_gbuf(&operation->request.gbuf); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 143 | return ret; |
| 144 | |
| 145 | } |
| 146 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 147 | static void gb_operation_request_handle(struct gb_operation *operation) |
| 148 | { |
Alex Elder | f8fb05e | 2014-11-05 16:12:55 -0600 | [diff] [blame] | 149 | struct gb_protocol *protocol = operation->connection->protocol; |
| 150 | struct gb_operation_msg_hdr *header; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 151 | |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 152 | header = operation->request.gbuf.transfer_buffer; |
Alex Elder | c3cf278 | 2014-11-12 15:17:55 -0600 | [diff] [blame] | 153 | |
Alex Elder | f8fb05e | 2014-11-05 16:12:55 -0600 | [diff] [blame] | 154 | /* |
| 155 | * If the protocol has no incoming request handler, report |
| 156 | * an error and mark the request bad. |
| 157 | */ |
| 158 | if (protocol->request_recv) { |
Alex Elder | c3cf278 | 2014-11-12 15:17:55 -0600 | [diff] [blame] | 159 | protocol->request_recv(header->type, operation); |
Alex Elder | f8fb05e | 2014-11-05 16:12:55 -0600 | [diff] [blame] | 160 | goto out; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Alex Elder | 7fba007 | 2014-10-28 19:35:59 -0500 | [diff] [blame] | 163 | gb_connection_err(operation->connection, |
Alex Elder | f8fb05e | 2014-11-05 16:12:55 -0600 | [diff] [blame] | 164 | "unexpected incoming request type 0x%02hhx\n", header->type); |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 165 | operation->result = GB_OP_PROTOCOL_BAD; |
Alex Elder | f8fb05e | 2014-11-05 16:12:55 -0600 | [diff] [blame] | 166 | out: |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 167 | gb_operation_complete(operation); |
| 168 | } |
| 169 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 170 | /* |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 171 | * Either this operation contains an incoming request, or its |
| 172 | * response has arrived. An incoming request will have a null |
| 173 | * response buffer pointer (it is the responsibility of the request |
| 174 | * handler to allocate and fill in the response buffer). |
| 175 | */ |
| 176 | static void gb_operation_recv_work(struct work_struct *recv_work) |
| 177 | { |
| 178 | struct gb_operation *operation; |
| 179 | bool incoming_request; |
| 180 | |
| 181 | operation = container_of(recv_work, struct gb_operation, recv_work); |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 182 | incoming_request = operation->response.gbuf.transfer_buffer == NULL; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 183 | if (incoming_request) |
| 184 | gb_operation_request_handle(operation); |
| 185 | gb_operation_complete(operation); |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
Greg Kroah-Hartman | 708971e | 2014-10-27 15:40:09 +0800 | [diff] [blame] | 189 | * Timeout call for the operation. |
| 190 | * |
| 191 | * If this fires, something went wrong, so mark the result as timed out, and |
| 192 | * run the completion handler, which (hopefully) should clean up the operation |
| 193 | * properly. |
| 194 | */ |
| 195 | static void operation_timeout(struct work_struct *work) |
| 196 | { |
| 197 | struct gb_operation *operation; |
| 198 | |
| 199 | operation = container_of(work, struct gb_operation, timeout_work.work); |
Viresh Kumar | 37d8afc | 2014-11-13 18:14:35 +0530 | [diff] [blame] | 200 | pr_debug("%s: timeout!\n", __func__); |
Greg Kroah-Hartman | 708971e | 2014-10-27 15:40:09 +0800 | [diff] [blame] | 201 | |
| 202 | operation->result = GB_OP_TIMEOUT; |
| 203 | gb_operation_complete(operation); |
| 204 | } |
| 205 | |
| 206 | /* |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 207 | * Allocate a buffer to be used for an operation request or response |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 208 | * message. For outgoing messages, both types of message contain a |
| 209 | * common header, which is filled in here. Incoming requests or |
| 210 | * responses also contain the same header, but there's no need to |
| 211 | * initialize it here (it'll be overwritten by the incoming |
| 212 | * message). |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 213 | */ |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 214 | static int gb_operation_message_init(struct gb_operation *operation, |
| 215 | u8 type, size_t size, |
Alex Elder | 4f83b8d | 2014-11-18 13:26:38 -0600 | [diff] [blame] | 216 | bool request, bool outbound) |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 217 | { |
Alex Elder | ba99346 | 2014-11-17 08:08:43 -0600 | [diff] [blame] | 218 | struct gb_connection *connection = operation->connection; |
Alex Elder | bb88896 | 2014-11-17 18:08:33 -0600 | [diff] [blame] | 219 | struct greybus_host_device *hd = connection->hd; |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 220 | struct gb_message *message; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 221 | struct gb_operation_msg_hdr *header; |
Alex Elder | bb88896 | 2014-11-17 18:08:33 -0600 | [diff] [blame] | 222 | struct gbuf *gbuf; |
Alex Elder | 4f83b8d | 2014-11-18 13:26:38 -0600 | [diff] [blame] | 223 | gfp_t gfp_flags = request && !outbound ? GFP_ATOMIC : GFP_KERNEL; |
Alex Elder | 63921d8 | 2014-11-17 08:08:41 -0600 | [diff] [blame] | 224 | u16 dest_cport_id; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 225 | |
Alex Elder | 78496db | 2014-11-17 08:08:39 -0600 | [diff] [blame] | 226 | if (size > GB_OPERATION_MESSAGE_SIZE_MAX) |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 227 | return -E2BIG; |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 228 | size += sizeof(*header); |
Alex Elder | 78496db | 2014-11-17 08:08:39 -0600 | [diff] [blame] | 229 | |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 230 | if (request) { |
| 231 | message = &operation->request; |
| 232 | } else { |
| 233 | message = &operation->response; |
| 234 | type |= GB_OPERATION_TYPE_RESPONSE; |
| 235 | } |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 236 | gbuf = &message->gbuf; |
| 237 | |
Alex Elder | 4f83b8d | 2014-11-18 13:26:38 -0600 | [diff] [blame] | 238 | if (outbound) |
Alex Elder | ba99346 | 2014-11-17 08:08:43 -0600 | [diff] [blame] | 239 | dest_cport_id = connection->interface_cport_id; |
Alex Elder | 63921d8 | 2014-11-17 08:08:41 -0600 | [diff] [blame] | 240 | else |
| 241 | dest_cport_id = CPORT_ID_BAD; |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 242 | |
Alex Elder | d2a259f | 2014-11-18 13:26:42 -0600 | [diff] [blame^] | 243 | gbuf->transfer_buffer = hd->driver->buffer_alloc(size, gfp_flags); |
| 244 | if (!gbuf->transfer_buffer) |
| 245 | return -ENOMEM; |
| 246 | gbuf->transfer_buffer_length = size; |
Alex Elder | 0f4c808 | 2014-11-18 13:26:41 -0600 | [diff] [blame] | 247 | gbuf->hd = hd; |
| 248 | gbuf->dest_cport_id = dest_cport_id; |
| 249 | gbuf->status = -EBADR; /* Initial value--means "never set" */ |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 250 | |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 251 | /* Fill in the header structure */ |
Alex Elder | bb88896 | 2014-11-17 18:08:33 -0600 | [diff] [blame] | 252 | header = (struct gb_operation_msg_hdr *)gbuf->transfer_buffer; |
Greg Kroah-Hartman | 322543a | 2014-10-02 21:25:21 -0700 | [diff] [blame] | 253 | header->size = cpu_to_le16(size); |
Alex Elder | b0b6575 | 2014-10-03 15:05:20 -0500 | [diff] [blame] | 254 | header->id = 0; /* Filled in when submitted */ |
| 255 | header->type = type; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 256 | |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 257 | message->payload = header + 1; |
| 258 | message->operation = operation; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static void gb_operation_message_exit(struct gb_message *message) |
| 264 | { |
| 265 | message->operation = NULL; |
| 266 | message->payload = NULL; |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 267 | message->gbuf.hd->driver->free_gbuf_data(&message->gbuf); |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Create a Greybus operation to be sent over the given connection. |
| 272 | * The request buffer will big enough for a payload of the given |
| 273 | * size. Outgoing requests must specify the size of the response |
| 274 | * buffer size, which must be sufficient to hold all expected |
| 275 | * response data. |
| 276 | * |
| 277 | * Incoming requests will supply a response size of 0, and in that |
| 278 | * case no response buffer is allocated. (A response always |
| 279 | * includes a status byte, so 0 is not a valid size.) Whatever |
| 280 | * handles the operation request is responsible for allocating the |
| 281 | * response buffer. |
| 282 | * |
| 283 | * Returns a pointer to the new operation or a null pointer if an |
| 284 | * error occurs. |
| 285 | */ |
| 286 | struct gb_operation *gb_operation_create(struct gb_connection *connection, |
| 287 | u8 type, size_t request_size, |
| 288 | size_t response_size) |
| 289 | { |
| 290 | struct gb_operation *operation; |
| 291 | gfp_t gfp_flags = response_size ? GFP_KERNEL : GFP_ATOMIC; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 292 | bool outgoing = response_size != 0; |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 293 | int ret; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 294 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 295 | operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags); |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 296 | if (!operation) |
| 297 | return NULL; |
Greg Kroah-Hartman | 6507cce | 2014-10-27 17:58:54 +0800 | [diff] [blame] | 298 | operation->connection = connection; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 299 | |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 300 | ret = gb_operation_message_init(operation, type, request_size, |
| 301 | true, outgoing); |
| 302 | if (ret) |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 303 | goto err_cache; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 304 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 305 | if (outgoing) { |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 306 | ret = gb_operation_message_init(operation, type, response_size, |
| 307 | false, false); |
| 308 | if (ret) |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 309 | goto err_request; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 310 | } |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 311 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 312 | INIT_WORK(&operation->recv_work, gb_operation_recv_work); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 313 | operation->callback = NULL; /* set at submit time */ |
| 314 | init_completion(&operation->completion); |
Greg Kroah-Hartman | 708971e | 2014-10-27 15:40:09 +0800 | [diff] [blame] | 315 | INIT_DELAYED_WORK(&operation->timeout_work, operation_timeout); |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 316 | kref_init(&operation->kref); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 317 | |
| 318 | spin_lock_irq(&gb_operations_lock); |
| 319 | list_add_tail(&operation->links, &connection->operations); |
| 320 | spin_unlock_irq(&gb_operations_lock); |
| 321 | |
| 322 | return operation; |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 323 | |
| 324 | err_request: |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 325 | gb_operation_message_exit(&operation->request); |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 326 | err_cache: |
| 327 | kmem_cache_free(gb_operation_cache, operation); |
| 328 | |
| 329 | return NULL; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Destroy a previously created operation. |
| 334 | */ |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 335 | static void _gb_operation_destroy(struct kref *kref) |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 336 | { |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 337 | struct gb_operation *operation; |
| 338 | |
| 339 | operation = container_of(kref, struct gb_operation, kref); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 340 | |
| 341 | /* XXX Make sure it's not in flight */ |
| 342 | spin_lock_irq(&gb_operations_lock); |
| 343 | list_del(&operation->links); |
| 344 | spin_unlock_irq(&gb_operations_lock); |
| 345 | |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 346 | gb_operation_message_exit(&operation->response); |
| 347 | gb_operation_message_exit(&operation->request); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 348 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 349 | kmem_cache_free(gb_operation_cache, operation); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 350 | } |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 351 | |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 352 | void gb_operation_put(struct gb_operation *operation) |
| 353 | { |
| 354 | if (!WARN_ON(!operation)) |
| 355 | kref_put(&operation->kref, _gb_operation_destroy); |
| 356 | } |
| 357 | |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 358 | /* |
| 359 | * Send an operation request message. The caller has filled in |
| 360 | * any payload so the request message is ready to go. If non-null, |
| 361 | * the callback function supplied will be called when the response |
| 362 | * message has arrived indicating the operation is complete. A null |
| 363 | * callback function is used for a synchronous request; return from |
| 364 | * this function won't occur until the operation is complete (or an |
| 365 | * interrupt occurs). |
| 366 | */ |
| 367 | int gb_operation_request_send(struct gb_operation *operation, |
| 368 | gb_operation_callback callback) |
| 369 | { |
Alex Elder | 8350e7a | 2014-11-12 15:17:52 -0600 | [diff] [blame] | 370 | unsigned long timeout; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 371 | int ret; |
| 372 | |
Alex Elder | 36561f2 | 2014-10-22 02:04:30 -0500 | [diff] [blame] | 373 | if (operation->connection->state != GB_CONNECTION_STATE_ENABLED) |
| 374 | return -ENOTCONN; |
| 375 | |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 376 | /* |
| 377 | * XXX |
| 378 | * I think the order of operations is going to be |
| 379 | * significant, and if so, we may need a mutex to surround |
| 380 | * setting the operation id and submitting the gbuf. |
| 381 | */ |
| 382 | operation->callback = callback; |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 383 | gb_pending_operation_insert(operation); |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 384 | ret = greybus_submit_gbuf(&operation->request.gbuf, GFP_KERNEL); |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 385 | if (ret) |
| 386 | return ret; |
Alex Elder | 8350e7a | 2014-11-12 15:17:52 -0600 | [diff] [blame] | 387 | |
| 388 | /* We impose a time limit for requests to complete. */ |
| 389 | timeout = msecs_to_jiffies(OPERATION_TIMEOUT_DEFAULT); |
| 390 | schedule_delayed_work(&operation->timeout_work, timeout); |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 391 | if (!callback) |
| 392 | ret = gb_operation_wait(operation); |
| 393 | |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Send a response for an incoming operation request. |
| 399 | */ |
| 400 | int gb_operation_response_send(struct gb_operation *operation) |
| 401 | { |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 402 | gb_operation_destroy(operation); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 407 | /* |
Alex Elder | 78496db | 2014-11-17 08:08:39 -0600 | [diff] [blame] | 408 | * Handle data arriving on a connection. As soon as we return, the |
| 409 | * incoming data buffer will be reused, so we need to copy the data |
| 410 | * into one of our own operation message buffers. |
| 411 | * |
| 412 | * If the incoming data is an operation response message, look up |
| 413 | * the operation and copy the incoming data into its response |
| 414 | * buffer. Otherwise allocate a new operation and copy the incoming |
| 415 | * data into its request buffer. |
| 416 | * |
| 417 | * This is called in interrupt context, so just copy the incoming |
| 418 | * data into the buffer and do remaining handling via a work queue. |
| 419 | * |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 420 | */ |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 421 | void gb_connection_operation_recv(struct gb_connection *connection, |
| 422 | void *data, size_t size) |
| 423 | { |
| 424 | struct gb_operation_msg_hdr *header; |
| 425 | struct gb_operation *operation; |
| 426 | struct gbuf *gbuf; |
| 427 | u16 msg_size; |
| 428 | |
Alex Elder | 36561f2 | 2014-10-22 02:04:30 -0500 | [diff] [blame] | 429 | if (connection->state != GB_CONNECTION_STATE_ENABLED) |
| 430 | return; |
| 431 | |
Alex Elder | 78496db | 2014-11-17 08:08:39 -0600 | [diff] [blame] | 432 | if (size < sizeof(*header)) { |
| 433 | gb_connection_err(connection, "message too small"); |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
| 437 | header = data; |
| 438 | msg_size = le16_to_cpu(header->size); |
| 439 | if (header->type & GB_OPERATION_TYPE_RESPONSE) { |
| 440 | u16 id = le16_to_cpu(header->id); |
| 441 | |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 442 | operation = gb_pending_operation_find(connection, id); |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 443 | if (!operation) { |
| 444 | gb_connection_err(connection, "operation not found"); |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 445 | return; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 446 | } |
Alex Elder | 19363a2 | 2014-11-17 08:08:36 -0600 | [diff] [blame] | 447 | cancel_delayed_work(&operation->timeout_work); |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 448 | gb_pending_operation_remove(operation); |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 449 | gbuf = &operation->response.gbuf; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 450 | if (size > gbuf->transfer_buffer_length) { |
Alex Elder | 78496db | 2014-11-17 08:08:39 -0600 | [diff] [blame] | 451 | operation->result = GB_OP_OVERFLOW; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 452 | gb_connection_err(connection, "recv buffer too small"); |
| 453 | return; |
| 454 | } |
Alex Elder | 78496db | 2014-11-17 08:08:39 -0600 | [diff] [blame] | 455 | operation->result = GB_OP_SUCCESS; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 456 | } else { |
| 457 | WARN_ON(msg_size != size); |
| 458 | operation = gb_operation_create(connection, header->type, |
| 459 | msg_size, 0); |
| 460 | if (!operation) { |
| 461 | gb_connection_err(connection, "can't create operation"); |
| 462 | return; |
| 463 | } |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 464 | gbuf = &operation->request.gbuf; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | memcpy(gbuf->transfer_buffer, data, msg_size); |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 468 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 469 | /* The rest will be handled in work queue context */ |
| 470 | queue_work(gb_operation_recv_workqueue, &operation->recv_work); |
| 471 | } |
| 472 | |
Alex Elder | e1158df | 2014-10-22 02:04:29 -0500 | [diff] [blame] | 473 | /* |
| 474 | * Cancel an operation. |
| 475 | */ |
| 476 | void gb_operation_cancel(struct gb_operation *operation) |
| 477 | { |
Alex Elder | e1158df | 2014-10-22 02:04:29 -0500 | [diff] [blame] | 478 | operation->canceled = true; |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 479 | greybus_kill_gbuf(&operation->request.gbuf); |
| 480 | if (operation->response.gbuf.transfer_buffer) |
| 481 | greybus_kill_gbuf(&operation->response.gbuf); |
Alex Elder | e1158df | 2014-10-22 02:04:29 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 484 | int gb_operation_init(void) |
| 485 | { |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 486 | gb_operation_cache = kmem_cache_create("gb_operation_cache", |
| 487 | sizeof(struct gb_operation), 0, 0, NULL); |
| 488 | if (!gb_operation_cache) |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 489 | return -ENOMEM; |
| 490 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 491 | gb_operation_recv_workqueue = alloc_workqueue("greybus_recv", 0, 1); |
| 492 | if (!gb_operation_recv_workqueue) { |
| 493 | kmem_cache_destroy(gb_operation_cache); |
| 494 | gb_operation_cache = NULL; |
| 495 | return -ENOMEM; |
| 496 | } |
| 497 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | void gb_operation_exit(void) |
| 502 | { |
| 503 | destroy_workqueue(gb_operation_recv_workqueue); |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 504 | gb_operation_recv_workqueue = NULL; |
Viresh Kumar | 837b3b7 | 2014-11-14 17:25:00 +0530 | [diff] [blame] | 505 | kmem_cache_destroy(gb_operation_cache); |
| 506 | gb_operation_cache = NULL; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 507 | } |