]>
Commit | Line | Data |
---|---|---|
eb50fd3a | 1 | // SPDX-License-Identifier: GPL-2.0 |
e88afa58 AE |
2 | /* |
3 | * Greybus operations | |
4 | * | |
d3d2bea1 AE |
5 | * Copyright 2014-2015 Google Inc. |
6 | * Copyright 2014-2015 Linaro Ltd. | |
e88afa58 AE |
7 | */ |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/slab.h> | |
11 | #include <linux/module.h> | |
fd7134a3 JH |
12 | #include <linux/sched.h> |
13 | #include <linux/wait.h> | |
e88afa58 AE |
14 | #include <linux/workqueue.h> |
15 | ||
16 | #include "greybus.h" | |
5c8ad599 | 17 | #include "greybus_trace.h" |
e88afa58 | 18 | |
5b3db0dd | 19 | static struct kmem_cache *gb_operation_cache; |
1e5613b4 | 20 | static struct kmem_cache *gb_message_cache; |
5b3db0dd | 21 | |
701615f8 JH |
22 | /* Workqueue to handle Greybus operation completions. */ |
23 | static struct workqueue_struct *gb_operation_completion_wq; | |
24 | ||
fd7134a3 JH |
25 | /* Wait queue for synchronous cancellations. */ |
26 | static DECLARE_WAIT_QUEUE_HEAD(gb_operation_cancellation_queue); | |
27 | ||
82b5e3fe | 28 | /* |
008974cb | 29 | * Protects updates to operation->errno. |
82b5e3fe | 30 | */ |
e88afa58 AE |
31 | static DEFINE_SPINLOCK(gb_operations_lock); |
32 | ||
abb722e7 JH |
33 | static int gb_operation_response_send(struct gb_operation *operation, |
34 | int errno); | |
35 | ||
008974cb JH |
36 | /* |
37 | * Increment operation active count and add to connection list unless the | |
38 | * connection is going away. | |
39 | * | |
40 | * Caller holds operation reference. | |
41 | */ | |
42 | static int gb_operation_get_active(struct gb_operation *operation) | |
3eeac7e3 | 43 | { |
008974cb JH |
44 | struct gb_connection *connection = operation->connection; |
45 | unsigned long flags; | |
46 | ||
47 | spin_lock_irqsave(&connection->lock, flags); | |
77bbbcf6 JH |
48 | switch (connection->state) { |
49 | case GB_CONNECTION_STATE_ENABLED: | |
50 | break; | |
51 | case GB_CONNECTION_STATE_ENABLED_TX: | |
52 | if (gb_operation_is_incoming(operation)) | |
53 | goto err_unlock; | |
54 | break; | |
3de5acfa JH |
55 | case GB_CONNECTION_STATE_DISCONNECTING: |
56 | if (!gb_operation_is_core(operation)) | |
57 | goto err_unlock; | |
58 | break; | |
77bbbcf6 JH |
59 | default: |
60 | goto err_unlock; | |
008974cb JH |
61 | } |
62 | ||
63 | if (operation->active++ == 0) | |
64 | list_add_tail(&operation->links, &connection->operations); | |
65 | ||
f866e66f AE |
66 | trace_gb_operation_get_active(operation); |
67 | ||
008974cb JH |
68 | spin_unlock_irqrestore(&connection->lock, flags); |
69 | ||
70 | return 0; | |
77bbbcf6 JH |
71 | |
72 | err_unlock: | |
73 | spin_unlock_irqrestore(&connection->lock, flags); | |
74 | ||
75 | return -ENOTCONN; | |
3eeac7e3 JH |
76 | } |
77 | ||
78 | /* Caller holds operation reference. */ | |
008974cb | 79 | static void gb_operation_put_active(struct gb_operation *operation) |
3eeac7e3 | 80 | { |
008974cb JH |
81 | struct gb_connection *connection = operation->connection; |
82 | unsigned long flags; | |
83 | ||
84 | spin_lock_irqsave(&connection->lock, flags); | |
f866e66f | 85 | |
df732546 | 86 | trace_gb_operation_put_active(operation); |
f866e66f | 87 | |
008974cb JH |
88 | if (--operation->active == 0) { |
89 | list_del(&operation->links); | |
fd7134a3 JH |
90 | if (atomic_read(&operation->waiters)) |
91 | wake_up(&gb_operation_cancellation_queue); | |
92 | } | |
008974cb | 93 | spin_unlock_irqrestore(&connection->lock, flags); |
fd7134a3 JH |
94 | } |
95 | ||
008974cb | 96 | static bool gb_operation_is_active(struct gb_operation *operation) |
fd7134a3 | 97 | { |
008974cb JH |
98 | struct gb_connection *connection = operation->connection; |
99 | unsigned long flags; | |
100 | bool ret; | |
101 | ||
102 | spin_lock_irqsave(&connection->lock, flags); | |
103 | ret = operation->active; | |
104 | spin_unlock_irqrestore(&connection->lock, flags); | |
105 | ||
106 | return ret; | |
3eeac7e3 JH |
107 | } |
108 | ||
3deb37d4 | 109 | /* |
2fb2d2a7 AE |
110 | * Set an operation's result. |
111 | * | |
112 | * Initially an outgoing operation's errno value is -EBADR. | |
113 | * If no error occurs before sending the request message the only | |
114 | * valid value operation->errno can be set to is -EINPROGRESS, | |
115 | * indicating the request has been (or rather is about to be) sent. | |
116 | * At that point nobody should be looking at the result until the | |
d5062834 | 117 | * response arrives. |
3deb37d4 AE |
118 | * |
119 | * The first time the result gets set after the request has been | |
120 | * sent, that result "sticks." That is, if two concurrent threads | |
121 | * race to set the result, the first one wins. The return value | |
122 | * tells the caller whether its result was recorded; if not the | |
2fb2d2a7 AE |
123 | * caller has nothing more to do. |
124 | * | |
125 | * The result value -EILSEQ is reserved to signal an implementation | |
126 | * error; if it's ever observed, the code performing the request has | |
127 | * done something fundamentally wrong. It is an error to try to set | |
128 | * the result to -EBADR, and attempts to do so result in a warning, | |
129 | * and -EILSEQ is used instead. Similarly, the only valid result | |
130 | * value to set for an operation in initial state is -EINPROGRESS. | |
131 | * Attempts to do otherwise will also record a (successful) -EILSEQ | |
132 | * operation result. | |
3deb37d4 | 133 | */ |
abe9a300 | 134 | static bool gb_operation_result_set(struct gb_operation *operation, int result) |
ba986b5a | 135 | { |
184ab534 | 136 | unsigned long flags; |
894cbc31 AE |
137 | int prev; |
138 | ||
3deb37d4 | 139 | if (result == -EINPROGRESS) { |
2fb2d2a7 AE |
140 | /* |
141 | * -EINPROGRESS is used to indicate the request is | |
142 | * in flight. It should be the first result value | |
143 | * set after the initial -EBADR. Issue a warning | |
144 | * and record an implementation error if it's | |
145 | * set at any other time. | |
146 | */ | |
184ab534 | 147 | spin_lock_irqsave(&gb_operations_lock, flags); |
894cbc31 AE |
148 | prev = operation->errno; |
149 | if (prev == -EBADR) | |
150 | operation->errno = result; | |
2fb2d2a7 AE |
151 | else |
152 | operation->errno = -EILSEQ; | |
184ab534 | 153 | spin_unlock_irqrestore(&gb_operations_lock, flags); |
2fb2d2a7 | 154 | WARN_ON(prev != -EBADR); |
894cbc31 | 155 | |
2fb2d2a7 | 156 | return true; |
3deb37d4 | 157 | } |
3deb37d4 | 158 | |
2fb2d2a7 AE |
159 | /* |
160 | * The first result value set after a request has been sent | |
161 | * will be the final result of the operation. Subsequent | |
162 | * attempts to set the result are ignored. | |
163 | * | |
164 | * Note that -EBADR is a reserved "initial state" result | |
165 | * value. Attempts to set this value result in a warning, | |
166 | * and the result code is set to -EILSEQ instead. | |
167 | */ | |
168 | if (WARN_ON(result == -EBADR)) | |
169 | result = -EILSEQ; /* Nobody should be setting -EBADR */ | |
170 | ||
184ab534 | 171 | spin_lock_irqsave(&gb_operations_lock, flags); |
894cbc31 AE |
172 | prev = operation->errno; |
173 | if (prev == -EINPROGRESS) | |
2fb2d2a7 | 174 | operation->errno = result; /* First and final result */ |
184ab534 | 175 | spin_unlock_irqrestore(&gb_operations_lock, flags); |
894cbc31 AE |
176 | |
177 | return prev == -EINPROGRESS; | |
ba986b5a AE |
178 | } |
179 | ||
180 | int gb_operation_result(struct gb_operation *operation) | |
181 | { | |
3deb37d4 AE |
182 | int result = operation->errno; |
183 | ||
2fb2d2a7 | 184 | WARN_ON(result == -EBADR); |
3deb37d4 AE |
185 | WARN_ON(result == -EINPROGRESS); |
186 | ||
187 | return result; | |
ba986b5a | 188 | } |
1dad6b35 | 189 | EXPORT_SYMBOL_GPL(gb_operation_result); |
ba986b5a | 190 | |
0581f28e | 191 | /* |
048a7ffe JH |
192 | * Looks up an outgoing operation on a connection and returns a refcounted |
193 | * pointer if found, or NULL otherwise. | |
0581f28e | 194 | */ |
84d148b1 | 195 | static struct gb_operation * |
048a7ffe | 196 | gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id) |
84d148b1 | 197 | { |
b8616da8 | 198 | struct gb_operation *operation; |
184ab534 | 199 | unsigned long flags; |
84d148b1 AE |
200 | bool found = false; |
201 | ||
008974cb | 202 | spin_lock_irqsave(&connection->lock, flags); |
afb2e134 | 203 | list_for_each_entry(operation, &connection->operations, links) |
048a7ffe JH |
204 | if (operation->id == operation_id && |
205 | !gb_operation_is_incoming(operation)) { | |
0581f28e | 206 | gb_operation_get(operation); |
84d148b1 | 207 | found = true; |
b8616da8 AE |
208 | break; |
209 | } | |
008974cb | 210 | spin_unlock_irqrestore(&connection->lock, flags); |
84d148b1 AE |
211 | |
212 | return found ? operation : NULL; | |
213 | } | |
214 | ||
a52c4352 | 215 | static int gb_message_send(struct gb_message *message, gfp_t gfp) |
374e6a26 | 216 | { |
3ed67aba | 217 | struct gb_connection *connection = message->operation->connection; |
002fe66a | 218 | |
5c8ad599 | 219 | trace_gb_message_send(message); |
3e136cc9 | 220 | return connection->hd->driver->message_send(connection->hd, |
0a9c4d70 | 221 | connection->hd_cport_id, |
7cf7bca9 | 222 | message, |
b84abdcb | 223 | gfp); |
374e6a26 AE |
224 | } |
225 | ||
6014718d | 226 | /* |
7cf7bca9 | 227 | * Cancel a message we have passed to the host device layer to be sent. |
6014718d | 228 | */ |
35b1342b | 229 | static void gb_message_cancel(struct gb_message *message) |
374e6a26 | 230 | { |
2537636a | 231 | struct gb_host_device *hd = message->operation->connection->hd; |
374e6a26 | 232 | |
3e136cc9 | 233 | hd->driver->message_cancel(message); |
374e6a26 | 234 | } |
a9163b2c | 235 | |
2eb585f8 AE |
236 | static void gb_operation_request_handle(struct gb_operation *operation) |
237 | { | |
25cdd7aa | 238 | struct gb_connection *connection = operation->connection; |
973ccfd6 | 239 | int status; |
ff65be7a | 240 | int ret; |
c3cf2785 | 241 | |
bfa9a5e2 JH |
242 | if (connection->handler) { |
243 | status = connection->handler(operation); | |
973ccfd6 | 244 | } else { |
25cdd7aa | 245 | dev_err(&connection->hd->dev, |
2f3db927 | 246 | "%s: unexpected incoming request of type 0x%02x\n", |
25cdd7aa | 247 | connection->name, operation->type); |
2eb585f8 | 248 | |
973ccfd6 JH |
249 | status = -EPROTONOSUPPORT; |
250 | } | |
ff65be7a | 251 | |
973ccfd6 | 252 | ret = gb_operation_response_send(operation, status); |
ff65be7a | 253 | if (ret) { |
25cdd7aa | 254 | dev_err(&connection->hd->dev, |
2f3db927 | 255 | "%s: failed to send response %d for type 0x%02x: %d\n", |
25cdd7aa | 256 | connection->name, status, operation->type, ret); |
c77bac08 | 257 | return; |
ff65be7a | 258 | } |
2eb585f8 AE |
259 | } |
260 | ||
e88afa58 | 261 | /* |
c600e535 JH |
262 | * Process operation work. |
263 | * | |
264 | * For incoming requests, call the protocol request handler. The operation | |
265 | * result should be -EINPROGRESS at this point. | |
d4a1ff67 AE |
266 | * |
267 | * For outgoing requests, the operation result value should have | |
268 | * been set before queueing this. The operation callback function | |
269 | * allows the original requester to know the request has completed | |
270 | * and its result is available. | |
e88afa58 | 271 | */ |
ee637a9b | 272 | static void gb_operation_work(struct work_struct *work) |
e88afa58 | 273 | { |
84d148b1 | 274 | struct gb_operation *operation; |
dbec2729 | 275 | int ret; |
84d148b1 | 276 | |
ee637a9b | 277 | operation = container_of(work, struct gb_operation, work); |
37754030 | 278 | |
dbec2729 | 279 | if (gb_operation_is_incoming(operation)) { |
c600e535 | 280 | gb_operation_request_handle(operation); |
dbec2729 JH |
281 | } else { |
282 | ret = del_timer_sync(&operation->timer); | |
283 | if (!ret) { | |
284 | /* Cancel request message if scheduled by timeout. */ | |
285 | if (gb_operation_result(operation) == -ETIMEDOUT) | |
286 | gb_message_cancel(operation->request); | |
287 | } | |
288 | ||
c600e535 | 289 | operation->callback(operation); |
dbec2729 | 290 | } |
37754030 | 291 | |
3eeac7e3 | 292 | gb_operation_put_active(operation); |
10c69399 | 293 | gb_operation_put(operation); |
2eb585f8 AE |
294 | } |
295 | ||
e99e88a9 | 296 | static void gb_operation_timeout(struct timer_list *t) |
dbec2729 | 297 | { |
e99e88a9 | 298 | struct gb_operation *operation = from_timer(operation, t, timer); |
dbec2729 JH |
299 | |
300 | if (gb_operation_result_set(operation, -ETIMEDOUT)) { | |
301 | /* | |
302 | * A stuck request message will be cancelled from the | |
303 | * workqueue. | |
304 | */ | |
305 | queue_work(gb_operation_completion_wq, &operation->work); | |
306 | } | |
307 | } | |
308 | ||
2537636a | 309 | static void gb_operation_message_init(struct gb_host_device *hd, |
dc779229 | 310 | struct gb_message *message, u16 operation_id, |
7cfa6995 | 311 | size_t payload_size, u8 type) |
dc779229 AE |
312 | { |
313 | struct gb_operation_msg_hdr *header; | |
dc779229 | 314 | |
24ef4853 | 315 | header = message->buffer; |
dc779229 AE |
316 | |
317 | message->header = header; | |
746e0ef9 | 318 | message->payload = payload_size ? header + 1 : NULL; |
7cfa6995 | 319 | message->payload_size = payload_size; |
dc779229 AE |
320 | |
321 | /* | |
322 | * The type supplied for incoming message buffers will be | |
7adb32b4 JH |
323 | * GB_REQUEST_TYPE_INVALID. Such buffers will be overwritten by |
324 | * arriving data so there's no need to initialize the message header. | |
dc779229 | 325 | */ |
7adb32b4 | 326 | if (type != GB_REQUEST_TYPE_INVALID) { |
7cfa6995 AE |
327 | u16 message_size = (u16)(sizeof(*header) + payload_size); |
328 | ||
dc779229 AE |
329 | /* |
330 | * For a request, the operation id gets filled in | |
331 | * when the message is sent. For a response, it | |
332 | * will be copied from the request by the caller. | |
333 | * | |
334 | * The result field in a request message must be | |
335 | * zero. It will be set just prior to sending for | |
336 | * a response. | |
337 | */ | |
338 | header->size = cpu_to_le16(message_size); | |
339 | header->operation_id = 0; | |
340 | header->type = type; | |
341 | header->result = 0; | |
342 | } | |
343 | } | |
344 | ||
e88afa58 | 345 | /* |
ea64cd9a AE |
346 | * Allocate a message to be used for an operation request or response. |
347 | * Both types of message contain a common header. The request message | |
348 | * for an outgoing operation is outbound, as is the response message | |
349 | * for an incoming operation. The message header for an outbound | |
350 | * message is partially initialized here. | |
351 | * | |
352 | * The headers for inbound messages don't need to be initialized; | |
353 | * they'll be filled in by arriving data. | |
87d208fe | 354 | * |
1e5613b4 | 355 | * Our message buffers have the following layout: |
87d208fe AE |
356 | * message header \_ these combined are |
357 | * message payload / the message size | |
22b320f4 | 358 | */ |
c08b1dda | 359 | static struct gb_message * |
2537636a | 360 | gb_operation_message_alloc(struct gb_host_device *hd, u8 type, |
87d208fe | 361 | size_t payload_size, gfp_t gfp_flags) |
22b320f4 | 362 | { |
c7f82d5d | 363 | struct gb_message *message; |
22b320f4 | 364 | struct gb_operation_msg_hdr *header; |
87d208fe | 365 | size_t message_size = payload_size + sizeof(*header); |
22b320f4 | 366 | |
1e5613b4 | 367 | if (message_size > hd->buffer_size_max) { |
b427572e | 368 | dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n", |
0cffcac3 | 369 | message_size, hd->buffer_size_max); |
1e5613b4 | 370 | return NULL; |
0cffcac3 | 371 | } |
1e5613b4 JH |
372 | |
373 | /* Allocate the message structure and buffer. */ | |
374 | message = kmem_cache_zalloc(gb_message_cache, gfp_flags); | |
c08b1dda AE |
375 | if (!message) |
376 | return NULL; | |
22b320f4 | 377 | |
24ef4853 | 378 | message->buffer = kzalloc(message_size, gfp_flags); |
1e5613b4 JH |
379 | if (!message->buffer) |
380 | goto err_free_message; | |
381 | ||
dc779229 | 382 | /* Initialize the message. Operation id is filled in later. */ |
7cfa6995 | 383 | gb_operation_message_init(hd, message, 0, payload_size, type); |
ea64cd9a | 384 | |
c08b1dda | 385 | return message; |
1e5613b4 JH |
386 | |
387 | err_free_message: | |
388 | kmem_cache_free(gb_message_cache, message); | |
389 | ||
390 | return NULL; | |
c7f82d5d AE |
391 | } |
392 | ||
c08b1dda | 393 | static void gb_operation_message_free(struct gb_message *message) |
c7f82d5d | 394 | { |
1e5613b4 JH |
395 | kfree(message->buffer); |
396 | kmem_cache_free(gb_message_cache, message); | |
22b320f4 AE |
397 | } |
398 | ||
bc717fcb | 399 | /* |
696e0cca VK |
400 | * Map an enum gb_operation_status value (which is represented in a |
401 | * message as a single byte) to an appropriate Linux negative errno. | |
bc717fcb | 402 | */ |
0c90fff4 | 403 | static int gb_operation_status_map(u8 status) |
bc717fcb AE |
404 | { |
405 | switch (status) { | |
406 | case GB_OP_SUCCESS: | |
407 | return 0; | |
bc717fcb AE |
408 | case GB_OP_INTERRUPTED: |
409 | return -EINTR; | |
57248fac AE |
410 | case GB_OP_TIMEOUT: |
411 | return -ETIMEDOUT; | |
412 | case GB_OP_NO_MEMORY: | |
413 | return -ENOMEM; | |
bc717fcb AE |
414 | case GB_OP_PROTOCOL_BAD: |
415 | return -EPROTONOSUPPORT; | |
416 | case GB_OP_OVERFLOW: | |
1a365154 | 417 | return -EMSGSIZE; |
57248fac AE |
418 | case GB_OP_INVALID: |
419 | return -EINVAL; | |
420 | case GB_OP_RETRY: | |
421 | return -EAGAIN; | |
aa26351d AE |
422 | case GB_OP_NONEXISTENT: |
423 | return -ENODEV; | |
57248fac AE |
424 | case GB_OP_MALFUNCTION: |
425 | return -EILSEQ; | |
426 | case GB_OP_UNKNOWN_ERROR: | |
bc717fcb AE |
427 | default: |
428 | return -EIO; | |
429 | } | |
430 | } | |
431 | ||
0c90fff4 AE |
432 | /* |
433 | * Map a Linux errno value (from operation->errno) into the value | |
434 | * that should represent it in a response message status sent | |
435 | * over the wire. Returns an enum gb_operation_status value (which | |
436 | * is represented in a message as a single byte). | |
437 | */ | |
438 | static u8 gb_operation_errno_map(int errno) | |
439 | { | |
440 | switch (errno) { | |
441 | case 0: | |
442 | return GB_OP_SUCCESS; | |
443 | case -EINTR: | |
444 | return GB_OP_INTERRUPTED; | |
445 | case -ETIMEDOUT: | |
446 | return GB_OP_TIMEOUT; | |
447 | case -ENOMEM: | |
448 | return GB_OP_NO_MEMORY; | |
449 | case -EPROTONOSUPPORT: | |
450 | return GB_OP_PROTOCOL_BAD; | |
451 | case -EMSGSIZE: | |
452 | return GB_OP_OVERFLOW; /* Could be underflow too */ | |
453 | case -EINVAL: | |
454 | return GB_OP_INVALID; | |
455 | case -EAGAIN: | |
456 | return GB_OP_RETRY; | |
457 | case -EILSEQ: | |
458 | return GB_OP_MALFUNCTION; | |
aa26351d AE |
459 | case -ENODEV: |
460 | return GB_OP_NONEXISTENT; | |
0c90fff4 AE |
461 | case -EIO: |
462 | default: | |
463 | return GB_OP_UNKNOWN_ERROR; | |
464 | } | |
465 | } | |
466 | ||
82e26f73 | 467 | bool gb_operation_response_alloc(struct gb_operation *operation, |
1c7658cf | 468 | size_t response_size, gfp_t gfp) |
82e26f73 | 469 | { |
2537636a | 470 | struct gb_host_device *hd = operation->connection->hd; |
82e26f73 AE |
471 | struct gb_operation_msg_hdr *request_header; |
472 | struct gb_message *response; | |
473 | u8 type; | |
474 | ||
6d653370 | 475 | type = operation->type | GB_MESSAGE_TYPE_RESPONSE; |
1c7658cf | 476 | response = gb_operation_message_alloc(hd, type, response_size, gfp); |
82e26f73 AE |
477 | if (!response) |
478 | return false; | |
479 | response->operation = operation; | |
480 | ||
481 | /* | |
482 | * Size and type get initialized when the message is | |
483 | * allocated. The errno will be set before sending. All | |
484 | * that's left is the operation id, which we copy from the | |
485 | * request message header (as-is, in little-endian order). | |
486 | */ | |
82b5e3fe | 487 | request_header = operation->request->header; |
82e26f73 AE |
488 | response->header->operation_id = request_header->operation_id; |
489 | operation->response = response; | |
490 | ||
491 | return true; | |
492 | } | |
1dad6b35 | 493 | EXPORT_SYMBOL_GPL(gb_operation_response_alloc); |
82e26f73 | 494 | |
22b320f4 AE |
495 | /* |
496 | * Create a Greybus operation to be sent over the given connection. | |
696e0cca | 497 | * The request buffer will be big enough for a payload of the given |
ea64cd9a AE |
498 | * size. |
499 | * | |
500 | * For outgoing requests, the request message's header will be | |
501 | * initialized with the type of the request and the message size. | |
502 | * Outgoing operations must also specify the response buffer size, | |
503 | * which must be sufficient to hold all expected response data. The | |
504 | * response message header will eventually be overwritten, so there's | |
505 | * no need to initialize it here. | |
22b320f4 | 506 | * |
ea64cd9a AE |
507 | * Request messages for incoming operations can arrive in interrupt |
508 | * context, so they must be allocated with GFP_ATOMIC. In this case | |
509 | * the request buffer will be immediately overwritten, so there is | |
510 | * no need to initialize the message header. Responsibility for | |
511 | * allocating a response buffer lies with the incoming request | |
512 | * handler for a protocol. So we don't allocate that here. | |
e88afa58 | 513 | * |
22b320f4 AE |
514 | * Returns a pointer to the new operation or a null pointer if an |
515 | * error occurs. | |
e88afa58 | 516 | */ |
30a2964f | 517 | static struct gb_operation * |
ea64cd9a | 518 | gb_operation_create_common(struct gb_connection *connection, u8 type, |
e420721b | 519 | size_t request_size, size_t response_size, |
710067e2 | 520 | unsigned long op_flags, gfp_t gfp_flags) |
e88afa58 | 521 | { |
2537636a | 522 | struct gb_host_device *hd = connection->hd; |
e88afa58 | 523 | struct gb_operation *operation; |
e88afa58 | 524 | |
5b3db0dd | 525 | operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags); |
e88afa58 AE |
526 | if (!operation) |
527 | return NULL; | |
6507cced | 528 | operation->connection = connection; |
e88afa58 | 529 | |
c08b1dda AE |
530 | operation->request = gb_operation_message_alloc(hd, type, request_size, |
531 | gfp_flags); | |
532 | if (!operation->request) | |
5b3db0dd | 533 | goto err_cache; |
c08b1dda | 534 | operation->request->operation = operation; |
22b320f4 | 535 | |
ea64cd9a | 536 | /* Allocate the response buffer for outgoing operations */ |
710067e2 | 537 | if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) { |
1c7658cf JH |
538 | if (!gb_operation_response_alloc(operation, response_size, |
539 | gfp_flags)) { | |
5b3db0dd | 540 | goto err_request; |
1c7658cf | 541 | } |
dbec2729 | 542 | |
e99e88a9 | 543 | timer_setup(&operation->timer, gb_operation_timeout, 0); |
82b5e3fe | 544 | } |
710067e2 JH |
545 | |
546 | operation->flags = op_flags; | |
547 | operation->type = type; | |
3deb37d4 | 548 | operation->errno = -EBADR; /* Initial value--means "never set" */ |
e88afa58 | 549 | |
ee637a9b | 550 | INIT_WORK(&operation->work, gb_operation_work); |
e88afa58 | 551 | init_completion(&operation->completion); |
c7d0f258 | 552 | kref_init(&operation->kref); |
fd7134a3 | 553 | atomic_set(&operation->waiters, 0); |
e88afa58 | 554 | |
e88afa58 | 555 | return operation; |
5b3db0dd AE |
556 | |
557 | err_request: | |
c08b1dda | 558 | gb_operation_message_free(operation->request); |
5b3db0dd AE |
559 | err_cache: |
560 | kmem_cache_free(gb_operation_cache, operation); | |
561 | ||
562 | return NULL; | |
e88afa58 AE |
563 | } |
564 | ||
55f66a88 AE |
565 | /* |
566 | * Create a new operation associated with the given connection. The | |
567 | * request and response sizes provided are the number of bytes | |
568 | * required to hold the request/response payload only. Both of | |
569 | * these are allowed to be 0. Note that 0x00 is reserved as an | |
570 | * invalid operation type for all protocols, and this is enforced | |
571 | * here. | |
572 | */ | |
7e43e337 JH |
573 | struct gb_operation * |
574 | gb_operation_create_flags(struct gb_connection *connection, | |
575 | u8 type, size_t request_size, | |
576 | size_t response_size, unsigned long flags, | |
577 | gfp_t gfp) | |
30a2964f | 578 | { |
f866e66f AE |
579 | struct gb_operation *operation; |
580 | ||
7adb32b4 | 581 | if (WARN_ON_ONCE(type == GB_REQUEST_TYPE_INVALID)) |
55f66a88 | 582 | return NULL; |
6d653370 AE |
583 | if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE)) |
584 | type &= ~GB_MESSAGE_TYPE_RESPONSE; | |
55f66a88 | 585 | |
7e43e337 JH |
586 | if (WARN_ON_ONCE(flags & ~GB_OPERATION_FLAG_USER_MASK)) |
587 | flags &= GB_OPERATION_FLAG_USER_MASK; | |
588 | ||
f866e66f | 589 | operation = gb_operation_create_common(connection, type, |
7e43e337 JH |
590 | request_size, response_size, |
591 | flags, gfp); | |
f866e66f AE |
592 | if (operation) |
593 | trace_gb_operation_create(operation); | |
594 | ||
595 | return operation; | |
30a2964f | 596 | } |
7e43e337 | 597 | EXPORT_SYMBOL_GPL(gb_operation_create_flags); |
30a2964f | 598 | |
18079ece JH |
599 | struct gb_operation * |
600 | gb_operation_create_core(struct gb_connection *connection, | |
601 | u8 type, size_t request_size, | |
602 | size_t response_size, unsigned long flags, | |
603 | gfp_t gfp) | |
604 | { | |
605 | struct gb_operation *operation; | |
606 | ||
607 | flags |= GB_OPERATION_FLAG_CORE; | |
608 | ||
609 | operation = gb_operation_create_common(connection, type, | |
610 | request_size, response_size, | |
611 | flags, gfp); | |
612 | if (operation) | |
613 | trace_gb_operation_create_core(operation); | |
614 | ||
615 | return operation; | |
616 | } | |
617 | /* Do not export this function. */ | |
618 | ||
d52b35f6 JH |
619 | size_t gb_operation_get_payload_size_max(struct gb_connection *connection) |
620 | { | |
2537636a | 621 | struct gb_host_device *hd = connection->hd; |
d52b35f6 JH |
622 | |
623 | return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr); | |
624 | } | |
625 | EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max); | |
626 | ||
30a2964f | 627 | static struct gb_operation * |
ea64cd9a | 628 | gb_operation_create_incoming(struct gb_connection *connection, u16 id, |
cfa79699 | 629 | u8 type, void *data, size_t size) |
30a2964f | 630 | { |
34db1f91 | 631 | struct gb_operation *operation; |
cfa79699 | 632 | size_t request_size; |
710067e2 | 633 | unsigned long flags = GB_OPERATION_FLAG_INCOMING; |
cfa79699 JH |
634 | |
635 | /* Caller has made sure we at least have a message header. */ | |
636 | request_size = size - sizeof(struct gb_operation_msg_hdr); | |
34db1f91 | 637 | |
e3398811 JH |
638 | if (!id) |
639 | flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL; | |
640 | ||
710067e2 | 641 | operation = gb_operation_create_common(connection, type, |
7adb32b4 JH |
642 | request_size, |
643 | GB_REQUEST_TYPE_INVALID, | |
644 | flags, GFP_ATOMIC); | |
9a586bd2 JH |
645 | if (!operation) |
646 | return NULL; | |
647 | ||
648 | operation->id = id; | |
649 | memcpy(operation->request->header, data, size); | |
f866e66f | 650 | trace_gb_operation_create_incoming(operation); |
34db1f91 AE |
651 | |
652 | return operation; | |
30a2964f AE |
653 | } |
654 | ||
deb4b9ef AE |
655 | /* |
656 | * Get an additional reference on an operation. | |
657 | */ | |
658 | void gb_operation_get(struct gb_operation *operation) | |
659 | { | |
660 | kref_get(&operation->kref); | |
661 | } | |
1dad6b35 | 662 | EXPORT_SYMBOL_GPL(gb_operation_get); |
deb4b9ef | 663 | |
e88afa58 AE |
664 | /* |
665 | * Destroy a previously created operation. | |
666 | */ | |
c7d0f258 | 667 | static void _gb_operation_destroy(struct kref *kref) |
e88afa58 | 668 | { |
c7d0f258 AE |
669 | struct gb_operation *operation; |
670 | ||
671 | operation = container_of(kref, struct gb_operation, kref); | |
e88afa58 | 672 | |
f866e66f AE |
673 | trace_gb_operation_destroy(operation); |
674 | ||
94896676 JH |
675 | if (operation->response) |
676 | gb_operation_message_free(operation->response); | |
c08b1dda | 677 | gb_operation_message_free(operation->request); |
e88afa58 | 678 | |
5b3db0dd | 679 | kmem_cache_free(gb_operation_cache, operation); |
e88afa58 | 680 | } |
d90c25b0 | 681 | |
deb4b9ef AE |
682 | /* |
683 | * Drop a reference on an operation, and destroy it when the last | |
684 | * one is gone. | |
685 | */ | |
c7d0f258 AE |
686 | void gb_operation_put(struct gb_operation *operation) |
687 | { | |
85109f7d JH |
688 | if (WARN_ON(!operation)) |
689 | return; | |
690 | ||
008974cb | 691 | kref_put(&operation->kref, _gb_operation_destroy); |
c7d0f258 | 692 | } |
df469a94 | 693 | EXPORT_SYMBOL_GPL(gb_operation_put); |
c7d0f258 | 694 | |
10c69399 AE |
695 | /* Tell the requester we're done */ |
696 | static void gb_operation_sync_callback(struct gb_operation *operation) | |
697 | { | |
698 | complete(&operation->completion); | |
699 | } | |
700 | ||
613c15e8 JH |
701 | /** |
702 | * gb_operation_request_send() - send an operation request message | |
703 | * @operation: the operation to initiate | |
704 | * @callback: the operation completion callback | |
dbec2729 | 705 | * @timeout: operation timeout in milliseconds, or zero for no timeout |
613c15e8 JH |
706 | * @gfp: the memory flags to use for any allocations |
707 | * | |
708 | * The caller has filled in any payload so the request message is ready to go. | |
709 | * The callback function supplied will be called when the response message has | |
3e2ee2c1 JH |
710 | * arrived, a unidirectional request has been sent, or the operation is |
711 | * cancelled, indicating that the operation is complete. The callback function | |
712 | * can fetch the result of the operation using gb_operation_result() if | |
713 | * desired. | |
613c15e8 JH |
714 | * |
715 | * Return: 0 if the request was successfully queued in the host-driver queues, | |
716 | * or a negative errno. | |
d90c25b0 AE |
717 | */ |
718 | int gb_operation_request_send(struct gb_operation *operation, | |
a52c4352 | 719 | gb_operation_callback callback, |
dbec2729 | 720 | unsigned int timeout, |
a52c4352 | 721 | gfp_t gfp) |
d90c25b0 | 722 | { |
afb2e134 AE |
723 | struct gb_connection *connection = operation->connection; |
724 | struct gb_operation_msg_hdr *header; | |
4afb7fd0 | 725 | unsigned int cycle; |
ea2c2ee8 | 726 | int ret; |
d90c25b0 | 727 | |
ca1f8f80 JH |
728 | if (gb_connection_is_offloaded(connection)) |
729 | return -EBUSY; | |
730 | ||
37754030 JH |
731 | if (!callback) |
732 | return -EINVAL; | |
3e2ee2c1 | 733 | |
c25572ca AE |
734 | /* |
735 | * Record the callback function, which is executed in | |
736 | * non-atomic (workqueue) context when the final result | |
737 | * of an operation has been set. | |
738 | */ | |
739 | operation->callback = callback; | |
afb2e134 AE |
740 | |
741 | /* | |
742 | * Assign the operation's id, and store it in the request header. | |
3e2ee2c1 | 743 | * Zero is a reserved operation id for unidirectional operations. |
afb2e134 | 744 | */ |
3e2ee2c1 JH |
745 | if (gb_operation_is_unidirectional(operation)) { |
746 | operation->id = 0; | |
747 | } else { | |
748 | cycle = (unsigned int)atomic_inc_return(&connection->op_cycle); | |
749 | operation->id = (u16)(cycle % U16_MAX + 1); | |
750 | } | |
751 | ||
afb2e134 AE |
752 | header = operation->request->header; |
753 | header->operation_id = cpu_to_le16(operation->id); | |
e8b48d15 | 754 | |
3deb37d4 | 755 | gb_operation_result_set(operation, -EINPROGRESS); |
c25572ca | 756 | |
3325a4ad JH |
757 | /* |
758 | * Get an extra reference on the operation. It'll be dropped when the | |
759 | * operation completes. | |
760 | */ | |
761 | gb_operation_get(operation); | |
762 | ret = gb_operation_get_active(operation); | |
763 | if (ret) | |
764 | goto err_put; | |
765 | ||
a52c4352 | 766 | ret = gb_message_send(operation->request, gfp); |
008974cb JH |
767 | if (ret) |
768 | goto err_put_active; | |
769 | ||
dbec2729 JH |
770 | if (timeout) { |
771 | operation->timer.expires = jiffies + msecs_to_jiffies(timeout); | |
772 | add_timer(&operation->timer); | |
773 | } | |
774 | ||
008974cb JH |
775 | return 0; |
776 | ||
777 | err_put_active: | |
778 | gb_operation_put_active(operation); | |
779 | err_put: | |
780 | gb_operation_put(operation); | |
ea2c2ee8 JH |
781 | |
782 | return ret; | |
c25572ca | 783 | } |
1dad6b35 | 784 | EXPORT_SYMBOL_GPL(gb_operation_request_send); |
c25572ca AE |
785 | |
786 | /* | |
787 | * Send a synchronous operation. This function is expected to | |
788 | * block, returning only when the response has arrived, (or when an | |
789 | * error is detected. The return value is the result of the | |
790 | * operation. | |
791 | */ | |
4f2c08ab JH |
792 | int gb_operation_request_send_sync_timeout(struct gb_operation *operation, |
793 | unsigned int timeout) | |
c25572ca AE |
794 | { |
795 | int ret; | |
796 | ||
a52c4352 | 797 | ret = gb_operation_request_send(operation, gb_operation_sync_callback, |
dbec2729 | 798 | timeout, GFP_KERNEL); |
c25572ca | 799 | if (ret) |
d90c25b0 | 800 | return ret; |
8350e7a0 | 801 | |
dbec2729 | 802 | ret = wait_for_completion_interruptible(&operation->completion); |
7bad4e85 PH |
803 | if (ret < 0) { |
804 | /* Cancel the operation if interrupted */ | |
1a365154 | 805 | gb_operation_cancel(operation, -ECANCELED); |
7bad4e85 | 806 | } |
2cf72a23 | 807 | |
ba986b5a | 808 | return gb_operation_result(operation); |
d90c25b0 | 809 | } |
4f2c08ab | 810 | EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout); |
d90c25b0 AE |
811 | |
812 | /* | |
82e26f73 AE |
813 | * Send a response for an incoming operation request. A non-zero |
814 | * errno indicates a failed operation. | |
815 | * | |
816 | * If there is any response payload, the incoming request handler is | |
817 | * responsible for allocating the response message. Otherwise the | |
818 | * it can simply supply the result errno; this function will | |
819 | * allocate the response message if necessary. | |
d90c25b0 | 820 | */ |
abb722e7 JH |
821 | static int gb_operation_response_send(struct gb_operation *operation, |
822 | int errno) | |
d90c25b0 | 823 | { |
e1baa3f0 | 824 | struct gb_connection *connection = operation->connection; |
0fb5acc4 JH |
825 | int ret; |
826 | ||
fde7382b JH |
827 | if (!operation->response && |
828 | !gb_operation_is_unidirectional(operation)) { | |
1c7658cf | 829 | if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL)) |
fde7382b JH |
830 | return -ENOMEM; |
831 | } | |
832 | ||
d2d2c0fe AE |
833 | /* Record the result */ |
834 | if (!gb_operation_result_set(operation, errno)) { | |
25cdd7aa | 835 | dev_err(&connection->hd->dev, "request result already set\n"); |
d2d2c0fe AE |
836 | return -EIO; /* Shouldn't happen */ |
837 | } | |
d90c25b0 | 838 | |
1d771fe4 | 839 | /* Sender of request does not care about response. */ |
e3398811 | 840 | if (gb_operation_is_unidirectional(operation)) |
1d771fe4 JH |
841 | return 0; |
842 | ||
0fb5acc4 JH |
843 | /* Reference will be dropped when message has been sent. */ |
844 | gb_operation_get(operation); | |
008974cb JH |
845 | ret = gb_operation_get_active(operation); |
846 | if (ret) | |
847 | goto err_put; | |
0fb5acc4 | 848 | |
82e26f73 AE |
849 | /* Fill in the response header and send it */ |
850 | operation->response->header->result = gb_operation_errno_map(errno); | |
851 | ||
a52c4352 | 852 | ret = gb_message_send(operation->response, GFP_KERNEL); |
008974cb JH |
853 | if (ret) |
854 | goto err_put_active; | |
855 | ||
856 | return 0; | |
857 | ||
858 | err_put_active: | |
859 | gb_operation_put_active(operation); | |
860 | err_put: | |
861 | gb_operation_put(operation); | |
0fb5acc4 JH |
862 | |
863 | return ret; | |
d90c25b0 AE |
864 | } |
865 | ||
d98b52b0 | 866 | /* |
7cf7bca9 | 867 | * This function is called when a message send request has completed. |
d98b52b0 | 868 | */ |
2537636a | 869 | void greybus_message_sent(struct gb_host_device *hd, |
7cf7bca9 | 870 | struct gb_message *message, int status) |
d98b52b0 | 871 | { |
a4e08469 JH |
872 | struct gb_operation *operation = message->operation; |
873 | struct gb_connection *connection = operation->connection; | |
d98b52b0 | 874 | |
d4a1ff67 AE |
875 | /* |
876 | * If the message was a response, we just need to drop our | |
877 | * reference to the operation. If an error occurred, report | |
878 | * it. | |
879 | * | |
3e2ee2c1 JH |
880 | * For requests, if there's no error and the operation in not |
881 | * unidirectional, there's nothing more to do until the response | |
882 | * arrives. If an error occurred attempting to send it, or if the | |
883 | * operation is unidrectional, record the result of the operation and | |
884 | * schedule its completion. | |
d4a1ff67 | 885 | */ |
d4a1ff67 | 886 | if (message == operation->response) { |
e1baa3f0 | 887 | if (status) { |
25cdd7aa | 888 | dev_err(&connection->hd->dev, |
2f3db927 | 889 | "%s: error sending response 0x%02x: %d\n", |
25cdd7aa | 890 | connection->name, operation->type, status); |
e1baa3f0 | 891 | } |
3e2ee2c1 | 892 | |
3eeac7e3 | 893 | gb_operation_put_active(operation); |
d4a1ff67 | 894 | gb_operation_put(operation); |
3e2ee2c1 | 895 | } else if (status || gb_operation_is_unidirectional(operation)) { |
701615f8 JH |
896 | if (gb_operation_result_set(operation, status)) { |
897 | queue_work(gb_operation_completion_wq, | |
898 | &operation->work); | |
899 | } | |
d4a1ff67 | 900 | } |
d98b52b0 | 901 | } |
7cf7bca9 | 902 | EXPORT_SYMBOL_GPL(greybus_message_sent); |
d98b52b0 | 903 | |
2eb585f8 | 904 | /* |
d37b1db1 AE |
905 | * We've received data on a connection, and it doesn't look like a |
906 | * response, so we assume it's a request. | |
78496db0 AE |
907 | * |
908 | * This is called in interrupt context, so just copy the incoming | |
d37b1db1 AE |
909 | * data into the request buffer and handle the rest via workqueue. |
910 | */ | |
85a04428 | 911 | static void gb_connection_recv_request(struct gb_connection *connection, |
2321f049 JH |
912 | const struct gb_operation_msg_hdr *header, |
913 | void *data, size_t size) | |
d37b1db1 AE |
914 | { |
915 | struct gb_operation *operation; | |
2321f049 JH |
916 | u16 operation_id; |
917 | u8 type; | |
008974cb | 918 | int ret; |
d37b1db1 | 919 | |
2321f049 JH |
920 | operation_id = le16_to_cpu(header->operation_id); |
921 | type = header->type; | |
922 | ||
34db1f91 | 923 | operation = gb_operation_create_incoming(connection, operation_id, |
82b5e3fe | 924 | type, data, size); |
d37b1db1 | 925 | if (!operation) { |
25cdd7aa JH |
926 | dev_err(&connection->hd->dev, |
927 | "%s: can't create incoming operation\n", | |
928 | connection->name); | |
ff65e20e | 929 | return; |
d37b1db1 | 930 | } |
d37b1db1 | 931 | |
008974cb JH |
932 | ret = gb_operation_get_active(operation); |
933 | if (ret) { | |
934 | gb_operation_put(operation); | |
935 | return; | |
936 | } | |
5c8ad599 | 937 | trace_gb_message_recv_request(operation->request); |
3eeac7e3 | 938 | |
d4a1ff67 | 939 | /* |
c600e535 JH |
940 | * The initial reference to the operation will be dropped when the |
941 | * request handler returns. | |
d4a1ff67 | 942 | */ |
d4a1ff67 | 943 | if (gb_operation_result_set(operation, -EINPROGRESS)) |
5a5bc354 | 944 | queue_work(connection->wq, &operation->work); |
d37b1db1 AE |
945 | } |
946 | ||
947 | /* | |
948 | * We've received data that appears to be an operation response | |
949 | * message. Look up the operation, and record that we've received | |
696e0cca | 950 | * its response. |
78496db0 | 951 | * |
d37b1db1 AE |
952 | * This is called in interrupt context, so just copy the incoming |
953 | * data into the response buffer and handle the rest via workqueue. | |
954 | */ | |
955 | static void gb_connection_recv_response(struct gb_connection *connection, | |
dfcba862 JH |
956 | const struct gb_operation_msg_hdr *header, |
957 | void *data, size_t size) | |
d37b1db1 AE |
958 | { |
959 | struct gb_operation *operation; | |
960 | struct gb_message *message; | |
7cfa6995 | 961 | size_t message_size; |
dfcba862 JH |
962 | u16 operation_id; |
963 | int errno; | |
964 | ||
965 | operation_id = le16_to_cpu(header->operation_id); | |
d37b1db1 | 966 | |
3e2ee2c1 | 967 | if (!operation_id) { |
b0e97bce | 968 | dev_err_ratelimited(&connection->hd->dev, |
3e2ee2c1 JH |
969 | "%s: invalid response id 0 received\n", |
970 | connection->name); | |
971 | return; | |
972 | } | |
973 | ||
048a7ffe | 974 | operation = gb_operation_find_outgoing(connection, operation_id); |
d37b1db1 | 975 | if (!operation) { |
b0e97bce ES |
976 | dev_err_ratelimited(&connection->hd->dev, |
977 | "%s: unexpected response id 0x%04x received\n", | |
978 | connection->name, operation_id); | |
d37b1db1 AE |
979 | return; |
980 | } | |
981 | ||
dfcba862 | 982 | errno = gb_operation_status_map(header->result); |
c08b1dda | 983 | message = operation->response; |
34804efb | 984 | message_size = sizeof(*header) + message->payload_size; |
7e43e337 | 985 | if (!errno && size > message_size) { |
b0e97bce | 986 | dev_err_ratelimited(&connection->hd->dev, |
7e43e337 JH |
987 | "%s: malformed response 0x%02x received (%zu > %zu)\n", |
988 | connection->name, header->type, | |
989 | size, message_size); | |
64ce39a3 | 990 | errno = -EMSGSIZE; |
7e43e337 JH |
991 | } else if (!errno && size < message_size) { |
992 | if (gb_operation_short_response_allowed(operation)) { | |
993 | message->payload_size = size - sizeof(*header); | |
994 | } else { | |
b0e97bce | 995 | dev_err_ratelimited(&connection->hd->dev, |
7e43e337 JH |
996 | "%s: short response 0x%02x received (%zu < %zu)\n", |
997 | connection->name, header->type, | |
998 | size, message_size); | |
999 | errno = -EMSGSIZE; | |
1000 | } | |
d37b1db1 | 1001 | } |
d37b1db1 | 1002 | |
25d0f81a | 1003 | /* We must ignore the payload if a bad status is returned */ |
64ce39a3 | 1004 | if (errno) |
34804efb | 1005 | size = sizeof(*header); |
d37b1db1 AE |
1006 | |
1007 | /* The rest will be handled in work queue context */ | |
e4340b13 | 1008 | if (gb_operation_result_set(operation, errno)) { |
dfcba862 | 1009 | memcpy(message->buffer, data, size); |
112f563e JH |
1010 | |
1011 | trace_gb_message_recv_response(message); | |
1012 | ||
701615f8 | 1013 | queue_work(gb_operation_completion_wq, &operation->work); |
e4340b13 | 1014 | } |
0581f28e JH |
1015 | |
1016 | gb_operation_put(operation); | |
d37b1db1 AE |
1017 | } |
1018 | ||
1019 | /* | |
1020 | * Handle data arriving on a connection. As soon as we return the | |
1021 | * supplied data buffer will be reused (so unless we do something | |
1022 | * with, it's effectively dropped). | |
2eb585f8 | 1023 | */ |
61089e89 | 1024 | void gb_connection_recv(struct gb_connection *connection, |
d90c25b0 AE |
1025 | void *data, size_t size) |
1026 | { | |
564c72b1 | 1027 | struct gb_operation_msg_hdr header; |
25cdd7aa | 1028 | struct device *dev = &connection->hd->dev; |
d37b1db1 | 1029 | size_t msg_size; |
d90c25b0 | 1030 | |
8890f957 | 1031 | if (connection->state == GB_CONNECTION_STATE_DISABLED || |
ca1f8f80 | 1032 | gb_connection_is_offloaded(connection)) { |
b0e97bce | 1033 | dev_warn_ratelimited(dev, "%s: dropping %zu received bytes\n", |
25cdd7aa | 1034 | connection->name, size); |
36561f23 | 1035 | return; |
d37b1db1 | 1036 | } |
36561f23 | 1037 | |
564c72b1 | 1038 | if (size < sizeof(header)) { |
b0e97bce ES |
1039 | dev_err_ratelimited(dev, "%s: short message received\n", |
1040 | connection->name); | |
d90c25b0 AE |
1041 | return; |
1042 | } | |
1043 | ||
564c72b1 JH |
1044 | /* Use memcpy as data may be unaligned */ |
1045 | memcpy(&header, data, sizeof(header)); | |
1046 | msg_size = le16_to_cpu(header.size); | |
0150bd7f | 1047 | if (size < msg_size) { |
b0e97bce ES |
1048 | dev_err_ratelimited(dev, |
1049 | "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n", | |
1050 | connection->name, | |
1051 | le16_to_cpu(header.operation_id), | |
1052 | header.type, size, msg_size); | |
d37b1db1 | 1053 | return; /* XXX Should still complete operation */ |
d90c25b0 AE |
1054 | } |
1055 | ||
2321f049 | 1056 | if (header.type & GB_MESSAGE_TYPE_RESPONSE) { |
dfcba862 JH |
1057 | gb_connection_recv_response(connection, &header, data, |
1058 | msg_size); | |
2321f049 JH |
1059 | } else { |
1060 | gb_connection_recv_request(connection, &header, data, | |
1061 | msg_size); | |
1062 | } | |
2eb585f8 AE |
1063 | } |
1064 | ||
e1158df0 | 1065 | /* |
5a3be769 JH |
1066 | * Cancel an outgoing operation synchronously, and record the given error to |
1067 | * indicate why. | |
e1158df0 | 1068 | */ |
f68c05c0 | 1069 | void gb_operation_cancel(struct gb_operation *operation, int errno) |
e1158df0 | 1070 | { |
5a3be769 JH |
1071 | if (WARN_ON(gb_operation_is_incoming(operation))) |
1072 | return; | |
1073 | ||
1074 | if (gb_operation_result_set(operation, errno)) { | |
1075 | gb_message_cancel(operation->request); | |
701615f8 | 1076 | queue_work(gb_operation_completion_wq, &operation->work); |
abe9a300 | 1077 | } |
5c8ad599 | 1078 | trace_gb_message_cancel_outgoing(operation->request); |
fd7134a3 JH |
1079 | |
1080 | atomic_inc(&operation->waiters); | |
1081 | wait_event(gb_operation_cancellation_queue, | |
1082 | !gb_operation_is_active(operation)); | |
1083 | atomic_dec(&operation->waiters); | |
e1158df0 | 1084 | } |
1dad6b35 | 1085 | EXPORT_SYMBOL_GPL(gb_operation_cancel); |
e1158df0 | 1086 | |
5a3be769 JH |
1087 | /* |
1088 | * Cancel an incoming operation synchronously. Called during connection tear | |
1089 | * down. | |
1090 | */ | |
1091 | void gb_operation_cancel_incoming(struct gb_operation *operation, int errno) | |
1092 | { | |
1093 | if (WARN_ON(!gb_operation_is_incoming(operation))) | |
1094 | return; | |
1095 | ||
1096 | if (!gb_operation_is_unidirectional(operation)) { | |
1097 | /* | |
1098 | * Make sure the request handler has submitted the response | |
1099 | * before cancelling it. | |
1100 | */ | |
1101 | flush_work(&operation->work); | |
1102 | if (!gb_operation_result_set(operation, errno)) | |
1103 | gb_message_cancel(operation->response); | |
1104 | } | |
5c8ad599 | 1105 | trace_gb_message_cancel_incoming(operation->response); |
5a3be769 JH |
1106 | |
1107 | atomic_inc(&operation->waiters); | |
1108 | wait_event(gb_operation_cancellation_queue, | |
1109 | !gb_operation_is_active(operation)); | |
1110 | atomic_dec(&operation->waiters); | |
1111 | } | |
1112 | ||
10aa801d | 1113 | /** |
410abddb | 1114 | * gb_operation_sync_timeout() - implement a "simple" synchronous operation |
10aa801d GKH |
1115 | * @connection: the Greybus connection to send this to |
1116 | * @type: the type of operation to send | |
1117 | * @request: pointer to a memory buffer to copy the request from | |
1118 | * @request_size: size of @request | |
1119 | * @response: pointer to a memory buffer to copy the response to | |
1120 | * @response_size: the size of @response. | |
129a06f5 | 1121 | * @timeout: operation timeout in milliseconds |
10aa801d GKH |
1122 | * |
1123 | * This function implements a simple synchronous Greybus operation. It sends | |
1124 | * the provided operation request and waits (sleeps) until the corresponding | |
1125 | * operation response message has been successfully received, or an error | |
1126 | * occurs. @request and @response are buffers to hold the request and response | |
1127 | * data respectively, and if they are not NULL, their size must be specified in | |
1128 | * @request_size and @response_size. | |
1129 | * | |
1130 | * If a response payload is to come back, and @response is not NULL, | |
1131 | * @response_size number of bytes will be copied into @response if the operation | |
1132 | * is successful. | |
1133 | * | |
1134 | * If there is an error, the response buffer is left alone. | |
1135 | */ | |
129a06f5 JH |
1136 | int gb_operation_sync_timeout(struct gb_connection *connection, int type, |
1137 | void *request, int request_size, | |
1138 | void *response, int response_size, | |
1139 | unsigned int timeout) | |
10aa801d GKH |
1140 | { |
1141 | struct gb_operation *operation; | |
1142 | int ret; | |
1143 | ||
1144 | if ((response_size && !response) || | |
1145 | (request_size && !request)) | |
1146 | return -EINVAL; | |
1147 | ||
1148 | operation = gb_operation_create(connection, type, | |
e420721b JH |
1149 | request_size, response_size, |
1150 | GFP_KERNEL); | |
10aa801d GKH |
1151 | if (!operation) |
1152 | return -ENOMEM; | |
1153 | ||
1154 | if (request_size) | |
6cd6ec55 | 1155 | memcpy(operation->request->payload, request, request_size); |
10aa801d | 1156 | |
129a06f5 | 1157 | ret = gb_operation_request_send_sync_timeout(operation, timeout); |
ee8f81b0 | 1158 | if (ret) { |
05e30955 | 1159 | dev_err(&connection->hd->dev, |
e514dec7 DL |
1160 | "%s: synchronous operation id 0x%04x of type 0x%02x failed: %d\n", |
1161 | connection->name, operation->id, type, ret); | |
ee8f81b0 JH |
1162 | } else { |
1163 | if (response_size) { | |
10aa801d GKH |
1164 | memcpy(response, operation->response->payload, |
1165 | response_size); | |
ee8f81b0 JH |
1166 | } |
1167 | } | |
6ab1ce4d JH |
1168 | |
1169 | gb_operation_put(operation); | |
10aa801d GKH |
1170 | |
1171 | return ret; | |
1172 | } | |
129a06f5 | 1173 | EXPORT_SYMBOL_GPL(gb_operation_sync_timeout); |
10aa801d | 1174 | |
5fdc027d JH |
1175 | /** |
1176 | * gb_operation_unidirectional_timeout() - initiate a unidirectional operation | |
1177 | * @connection: connection to use | |
1178 | * @type: type of operation to send | |
1179 | * @request: memory buffer to copy the request from | |
1180 | * @request_size: size of @request | |
1181 | * @timeout: send timeout in milliseconds | |
1182 | * | |
1183 | * Initiate a unidirectional operation by sending a request message and | |
1184 | * waiting for it to be acknowledged as sent by the host device. | |
1185 | * | |
1186 | * Note that successful send of a unidirectional operation does not imply that | |
1187 | * the request as actually reached the remote end of the connection. | |
1188 | */ | |
1189 | int gb_operation_unidirectional_timeout(struct gb_connection *connection, | |
1190 | int type, void *request, int request_size, | |
1191 | unsigned int timeout) | |
1192 | { | |
1193 | struct gb_operation *operation; | |
1194 | int ret; | |
1195 | ||
1196 | if (request_size && !request) | |
1197 | return -EINVAL; | |
1198 | ||
1199 | operation = gb_operation_create_flags(connection, type, | |
1200 | request_size, 0, | |
1201 | GB_OPERATION_FLAG_UNIDIRECTIONAL, | |
1202 | GFP_KERNEL); | |
1203 | if (!operation) | |
1204 | return -ENOMEM; | |
1205 | ||
1206 | if (request_size) | |
1207 | memcpy(operation->request->payload, request, request_size); | |
1208 | ||
1209 | ret = gb_operation_request_send_sync_timeout(operation, timeout); | |
1210 | if (ret) { | |
1211 | dev_err(&connection->hd->dev, | |
1212 | "%s: unidirectional operation of type 0x%02x failed: %d\n", | |
1213 | connection->name, type, ret); | |
1214 | } | |
1215 | ||
1216 | gb_operation_put(operation); | |
1217 | ||
1218 | return ret; | |
1219 | } | |
1220 | EXPORT_SYMBOL_GPL(gb_operation_unidirectional_timeout); | |
1221 | ||
47ed2c92 | 1222 | int __init gb_operation_init(void) |
2eb585f8 | 1223 | { |
1e5613b4 JH |
1224 | gb_message_cache = kmem_cache_create("gb_message_cache", |
1225 | sizeof(struct gb_message), 0, 0, NULL); | |
1226 | if (!gb_message_cache) | |
0cffcac3 AE |
1227 | return -ENOMEM; |
1228 | ||
5b3db0dd AE |
1229 | gb_operation_cache = kmem_cache_create("gb_operation_cache", |
1230 | sizeof(struct gb_operation), 0, 0, NULL); | |
1231 | if (!gb_operation_cache) | |
1e5613b4 | 1232 | goto err_destroy_message_cache; |
5b3db0dd | 1233 | |
701615f8 JH |
1234 | gb_operation_completion_wq = alloc_workqueue("greybus_completion", |
1235 | 0, 0); | |
1236 | if (!gb_operation_completion_wq) | |
1237 | goto err_destroy_operation_cache; | |
1238 | ||
2eb585f8 | 1239 | return 0; |
5a5bc354 | 1240 | |
701615f8 JH |
1241 | err_destroy_operation_cache: |
1242 | kmem_cache_destroy(gb_operation_cache); | |
1243 | gb_operation_cache = NULL; | |
1e5613b4 JH |
1244 | err_destroy_message_cache: |
1245 | kmem_cache_destroy(gb_message_cache); | |
1246 | gb_message_cache = NULL; | |
0cffcac3 AE |
1247 | |
1248 | return -ENOMEM; | |
2eb585f8 AE |
1249 | } |
1250 | ||
f35ab903 | 1251 | void gb_operation_exit(void) |
2eb585f8 | 1252 | { |
701615f8 JH |
1253 | destroy_workqueue(gb_operation_completion_wq); |
1254 | gb_operation_completion_wq = NULL; | |
837b3b7c VK |
1255 | kmem_cache_destroy(gb_operation_cache); |
1256 | gb_operation_cache = NULL; | |
1e5613b4 JH |
1257 | kmem_cache_destroy(gb_message_cache); |
1258 | gb_message_cache = NULL; | |
d90c25b0 | 1259 | } |