Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Greybus operations |
| 3 | * |
Alex Elder | d3d2bea | 2015-03-26 21:25:01 -0500 | [diff] [blame] | 4 | * Copyright 2014-2015 Google Inc. |
| 5 | * Copyright 2014-2015 Linaro Ltd. |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/module.h> |
Johan Hovold | fd7134a | 2015-07-14 15:43:26 +0200 | [diff] [blame] | 13 | #include <linux/sched.h> |
| 14 | #include <linux/wait.h> |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 15 | #include <linux/workqueue.h> |
| 16 | |
| 17 | #include "greybus.h" |
Bryan O'Donoghue | 5c8ad59 | 2015-09-18 16:38:45 +0100 | [diff] [blame] | 18 | #include "greybus_trace.h" |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 19 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 20 | static struct kmem_cache *gb_operation_cache; |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 21 | static struct kmem_cache *gb_message_cache; |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 22 | |
Johan Hovold | 701615f | 2015-07-23 10:50:03 +0200 | [diff] [blame] | 23 | /* Workqueue to handle Greybus operation completions. */ |
| 24 | static struct workqueue_struct *gb_operation_completion_wq; |
| 25 | |
Johan Hovold | fd7134a | 2015-07-14 15:43:26 +0200 | [diff] [blame] | 26 | /* Wait queue for synchronous cancellations. */ |
| 27 | static DECLARE_WAIT_QUEUE_HEAD(gb_operation_cancellation_queue); |
| 28 | |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 29 | /* |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 30 | * Protects updates to operation->errno. |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 31 | */ |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 32 | static DEFINE_SPINLOCK(gb_operations_lock); |
| 33 | |
Johan Hovold | abb722e | 2015-07-01 12:37:24 +0200 | [diff] [blame] | 34 | static int gb_operation_response_send(struct gb_operation *operation, |
| 35 | int errno); |
| 36 | |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 37 | /* |
| 38 | * Increment operation active count and add to connection list unless the |
| 39 | * connection is going away. |
| 40 | * |
| 41 | * Caller holds operation reference. |
| 42 | */ |
| 43 | static int gb_operation_get_active(struct gb_operation *operation) |
Johan Hovold | 3eeac7e | 2015-07-14 15:43:25 +0200 | [diff] [blame] | 44 | { |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 45 | struct gb_connection *connection = operation->connection; |
| 46 | unsigned long flags; |
| 47 | |
| 48 | spin_lock_irqsave(&connection->lock, flags); |
| 49 | |
Johan Hovold | 570dfa7 | 2016-01-19 12:51:07 +0100 | [diff] [blame] | 50 | if (connection->state != GB_CONNECTION_STATE_ENABLED && |
| 51 | connection->state != GB_CONNECTION_STATE_ENABLED_TX && |
| 52 | !gb_operation_is_incoming(operation)) { |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 53 | spin_unlock_irqrestore(&connection->lock, flags); |
| 54 | return -ENOTCONN; |
| 55 | } |
| 56 | |
| 57 | if (operation->active++ == 0) |
| 58 | list_add_tail(&operation->links, &connection->operations); |
| 59 | |
| 60 | spin_unlock_irqrestore(&connection->lock, flags); |
| 61 | |
| 62 | return 0; |
Johan Hovold | 3eeac7e | 2015-07-14 15:43:25 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* Caller holds operation reference. */ |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 66 | static void gb_operation_put_active(struct gb_operation *operation) |
Johan Hovold | 3eeac7e | 2015-07-14 15:43:25 +0200 | [diff] [blame] | 67 | { |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 68 | struct gb_connection *connection = operation->connection; |
| 69 | unsigned long flags; |
| 70 | |
| 71 | spin_lock_irqsave(&connection->lock, flags); |
| 72 | if (--operation->active == 0) { |
| 73 | list_del(&operation->links); |
Johan Hovold | fd7134a | 2015-07-14 15:43:26 +0200 | [diff] [blame] | 74 | if (atomic_read(&operation->waiters)) |
| 75 | wake_up(&gb_operation_cancellation_queue); |
| 76 | } |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 77 | spin_unlock_irqrestore(&connection->lock, flags); |
Johan Hovold | fd7134a | 2015-07-14 15:43:26 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 80 | static bool gb_operation_is_active(struct gb_operation *operation) |
Johan Hovold | fd7134a | 2015-07-14 15:43:26 +0200 | [diff] [blame] | 81 | { |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 82 | struct gb_connection *connection = operation->connection; |
| 83 | unsigned long flags; |
| 84 | bool ret; |
| 85 | |
| 86 | spin_lock_irqsave(&connection->lock, flags); |
| 87 | ret = operation->active; |
| 88 | spin_unlock_irqrestore(&connection->lock, flags); |
| 89 | |
| 90 | return ret; |
Johan Hovold | 3eeac7e | 2015-07-14 15:43:25 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Alex Elder | 3deb37d | 2014-11-25 11:33:15 -0600 | [diff] [blame] | 93 | /* |
Alex Elder | 2fb2d2a | 2014-12-01 07:53:08 -0600 | [diff] [blame] | 94 | * Set an operation's result. |
| 95 | * |
| 96 | * Initially an outgoing operation's errno value is -EBADR. |
| 97 | * If no error occurs before sending the request message the only |
| 98 | * valid value operation->errno can be set to is -EINPROGRESS, |
| 99 | * indicating the request has been (or rather is about to be) sent. |
| 100 | * At that point nobody should be looking at the result until the |
Johan Hovold | d506283 | 2015-03-27 12:41:11 +0100 | [diff] [blame] | 101 | * response arrives. |
Alex Elder | 3deb37d | 2014-11-25 11:33:15 -0600 | [diff] [blame] | 102 | * |
| 103 | * The first time the result gets set after the request has been |
| 104 | * sent, that result "sticks." That is, if two concurrent threads |
| 105 | * race to set the result, the first one wins. The return value |
| 106 | * tells the caller whether its result was recorded; if not the |
Alex Elder | 2fb2d2a | 2014-12-01 07:53:08 -0600 | [diff] [blame] | 107 | * caller has nothing more to do. |
| 108 | * |
| 109 | * The result value -EILSEQ is reserved to signal an implementation |
| 110 | * error; if it's ever observed, the code performing the request has |
| 111 | * done something fundamentally wrong. It is an error to try to set |
| 112 | * the result to -EBADR, and attempts to do so result in a warning, |
| 113 | * and -EILSEQ is used instead. Similarly, the only valid result |
| 114 | * value to set for an operation in initial state is -EINPROGRESS. |
| 115 | * Attempts to do otherwise will also record a (successful) -EILSEQ |
| 116 | * operation result. |
Alex Elder | 3deb37d | 2014-11-25 11:33:15 -0600 | [diff] [blame] | 117 | */ |
Alex Elder | abe9a30 | 2014-11-25 11:33:14 -0600 | [diff] [blame] | 118 | static bool gb_operation_result_set(struct gb_operation *operation, int result) |
Alex Elder | ba986b5 | 2014-11-25 11:33:13 -0600 | [diff] [blame] | 119 | { |
Johan Hovold | 184ab53 | 2015-03-02 12:34:40 +0100 | [diff] [blame] | 120 | unsigned long flags; |
Alex Elder | 894cbc3 | 2014-11-25 16:54:02 -0600 | [diff] [blame] | 121 | int prev; |
| 122 | |
Alex Elder | 3deb37d | 2014-11-25 11:33:15 -0600 | [diff] [blame] | 123 | if (result == -EINPROGRESS) { |
Alex Elder | 2fb2d2a | 2014-12-01 07:53:08 -0600 | [diff] [blame] | 124 | /* |
| 125 | * -EINPROGRESS is used to indicate the request is |
| 126 | * in flight. It should be the first result value |
| 127 | * set after the initial -EBADR. Issue a warning |
| 128 | * and record an implementation error if it's |
| 129 | * set at any other time. |
| 130 | */ |
Johan Hovold | 184ab53 | 2015-03-02 12:34:40 +0100 | [diff] [blame] | 131 | spin_lock_irqsave(&gb_operations_lock, flags); |
Alex Elder | 894cbc3 | 2014-11-25 16:54:02 -0600 | [diff] [blame] | 132 | prev = operation->errno; |
| 133 | if (prev == -EBADR) |
| 134 | operation->errno = result; |
Alex Elder | 2fb2d2a | 2014-12-01 07:53:08 -0600 | [diff] [blame] | 135 | else |
| 136 | operation->errno = -EILSEQ; |
Johan Hovold | 184ab53 | 2015-03-02 12:34:40 +0100 | [diff] [blame] | 137 | spin_unlock_irqrestore(&gb_operations_lock, flags); |
Alex Elder | 2fb2d2a | 2014-12-01 07:53:08 -0600 | [diff] [blame] | 138 | WARN_ON(prev != -EBADR); |
Alex Elder | 3deb37d | 2014-11-25 11:33:15 -0600 | [diff] [blame] | 139 | |
Alex Elder | 2fb2d2a | 2014-12-01 07:53:08 -0600 | [diff] [blame] | 140 | return true; |
Alex Elder | 894cbc3 | 2014-11-25 16:54:02 -0600 | [diff] [blame] | 141 | } |
| 142 | |
Alex Elder | 2fb2d2a | 2014-12-01 07:53:08 -0600 | [diff] [blame] | 143 | /* |
| 144 | * The first result value set after a request has been sent |
| 145 | * will be the final result of the operation. Subsequent |
| 146 | * attempts to set the result are ignored. |
| 147 | * |
| 148 | * Note that -EBADR is a reserved "initial state" result |
| 149 | * value. Attempts to set this value result in a warning, |
| 150 | * and the result code is set to -EILSEQ instead. |
| 151 | */ |
| 152 | if (WARN_ON(result == -EBADR)) |
| 153 | result = -EILSEQ; /* Nobody should be setting -EBADR */ |
| 154 | |
Johan Hovold | 184ab53 | 2015-03-02 12:34:40 +0100 | [diff] [blame] | 155 | spin_lock_irqsave(&gb_operations_lock, flags); |
Alex Elder | 894cbc3 | 2014-11-25 16:54:02 -0600 | [diff] [blame] | 156 | prev = operation->errno; |
| 157 | if (prev == -EINPROGRESS) |
Alex Elder | 2fb2d2a | 2014-12-01 07:53:08 -0600 | [diff] [blame] | 158 | operation->errno = result; /* First and final result */ |
Johan Hovold | 184ab53 | 2015-03-02 12:34:40 +0100 | [diff] [blame] | 159 | spin_unlock_irqrestore(&gb_operations_lock, flags); |
Alex Elder | 894cbc3 | 2014-11-25 16:54:02 -0600 | [diff] [blame] | 160 | |
| 161 | return prev == -EINPROGRESS; |
Alex Elder | ba986b5 | 2014-11-25 11:33:13 -0600 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | int gb_operation_result(struct gb_operation *operation) |
| 165 | { |
Alex Elder | 3deb37d | 2014-11-25 11:33:15 -0600 | [diff] [blame] | 166 | int result = operation->errno; |
| 167 | |
Alex Elder | 2fb2d2a | 2014-12-01 07:53:08 -0600 | [diff] [blame] | 168 | WARN_ON(result == -EBADR); |
Alex Elder | 3deb37d | 2014-11-25 11:33:15 -0600 | [diff] [blame] | 169 | WARN_ON(result == -EINPROGRESS); |
| 170 | |
| 171 | return result; |
Alex Elder | ba986b5 | 2014-11-25 11:33:13 -0600 | [diff] [blame] | 172 | } |
Johan Hovold | 1dad6b3 | 2015-03-27 12:41:10 +0100 | [diff] [blame] | 173 | EXPORT_SYMBOL_GPL(gb_operation_result); |
Alex Elder | ba986b5 | 2014-11-25 11:33:13 -0600 | [diff] [blame] | 174 | |
Johan Hovold | 0581f28e | 2015-07-09 15:17:59 +0200 | [diff] [blame] | 175 | /* |
Johan Hovold | 048a7ff | 2015-07-09 15:18:00 +0200 | [diff] [blame] | 176 | * Looks up an outgoing operation on a connection and returns a refcounted |
| 177 | * pointer if found, or NULL otherwise. |
Johan Hovold | 0581f28e | 2015-07-09 15:17:59 +0200 | [diff] [blame] | 178 | */ |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 179 | static struct gb_operation * |
Johan Hovold | 048a7ff | 2015-07-09 15:18:00 +0200 | [diff] [blame] | 180 | gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id) |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 181 | { |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 182 | struct gb_operation *operation; |
Johan Hovold | 184ab53 | 2015-03-02 12:34:40 +0100 | [diff] [blame] | 183 | unsigned long flags; |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 184 | bool found = false; |
| 185 | |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 186 | spin_lock_irqsave(&connection->lock, flags); |
Alex Elder | afb2e13 | 2014-12-03 08:35:07 -0600 | [diff] [blame] | 187 | list_for_each_entry(operation, &connection->operations, links) |
Johan Hovold | 048a7ff | 2015-07-09 15:18:00 +0200 | [diff] [blame] | 188 | if (operation->id == operation_id && |
| 189 | !gb_operation_is_incoming(operation)) { |
Johan Hovold | 0581f28e | 2015-07-09 15:17:59 +0200 | [diff] [blame] | 190 | gb_operation_get(operation); |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 191 | found = true; |
Alex Elder | b8616da | 2014-11-12 15:17:53 -0600 | [diff] [blame] | 192 | break; |
| 193 | } |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 194 | spin_unlock_irqrestore(&connection->lock, flags); |
Alex Elder | 84d148b | 2014-10-16 06:35:32 -0500 | [diff] [blame] | 195 | |
| 196 | return found ? operation : NULL; |
| 197 | } |
| 198 | |
Johan Hovold | a52c435 | 2015-07-01 12:37:23 +0200 | [diff] [blame] | 199 | static int gb_message_send(struct gb_message *message, gfp_t gfp) |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 200 | { |
Alex Elder | 3ed67ab | 2014-11-18 13:26:52 -0600 | [diff] [blame] | 201 | struct gb_connection *connection = message->operation->connection; |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 202 | |
Bryan O'Donoghue | 5c8ad59 | 2015-09-18 16:38:45 +0100 | [diff] [blame] | 203 | trace_gb_message_send(message); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 204 | return connection->hd->driver->message_send(connection->hd, |
Alex Elder | 0a9c4d7 | 2014-12-11 16:48:38 -0600 | [diff] [blame] | 205 | connection->hd_cport_id, |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 206 | message, |
Johan Hovold | b84abdc | 2015-07-17 18:50:26 +0200 | [diff] [blame] | 207 | gfp); |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 208 | } |
| 209 | |
Alex Elder | 6014718 | 2014-11-19 12:27:15 -0600 | [diff] [blame] | 210 | /* |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 211 | * Cancel a message we have passed to the host device layer to be sent. |
Alex Elder | 6014718 | 2014-11-19 12:27:15 -0600 | [diff] [blame] | 212 | */ |
Alex Elder | 35b1342 | 2014-11-18 13:26:49 -0600 | [diff] [blame] | 213 | static void gb_message_cancel(struct gb_message *message) |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 214 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 215 | struct gb_host_device *hd = message->operation->connection->hd; |
Alex Elder | 3ed67ab | 2014-11-18 13:26:52 -0600 | [diff] [blame] | 216 | |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 217 | hd->driver->message_cancel(message); |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 218 | } |
Alex Elder | a9163b2 | 2014-11-18 13:26:44 -0600 | [diff] [blame] | 219 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 220 | static void gb_operation_request_handle(struct gb_operation *operation) |
| 221 | { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 222 | struct gb_connection *connection = operation->connection; |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 223 | int status; |
Johan Hovold | ff65be7 | 2015-03-27 12:41:15 +0100 | [diff] [blame] | 224 | int ret; |
Alex Elder | c3cf278 | 2014-11-12 15:17:55 -0600 | [diff] [blame] | 225 | |
Johan Hovold | bfa9a5e | 2016-01-19 12:51:02 +0100 | [diff] [blame] | 226 | if (connection->handler) { |
| 227 | status = connection->handler(operation); |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 228 | } else { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 229 | dev_err(&connection->hd->dev, |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 230 | "%s: unexpected incoming request of type 0x%02x\n", |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 231 | connection->name, operation->type); |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 232 | |
| 233 | status = -EPROTONOSUPPORT; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 236 | ret = gb_operation_response_send(operation, status); |
Johan Hovold | ff65be7 | 2015-03-27 12:41:15 +0100 | [diff] [blame] | 237 | if (ret) { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 238 | dev_err(&connection->hd->dev, |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 239 | "%s: failed to send response %d for type 0x%02x: %d\n", |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 240 | connection->name, status, operation->type, ret); |
Johan Hovold | ff65be7 | 2015-03-27 12:41:15 +0100 | [diff] [blame] | 241 | return; |
| 242 | } |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 243 | } |
| 244 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 245 | /* |
Johan Hovold | c600e53 | 2015-07-14 15:43:28 +0200 | [diff] [blame] | 246 | * Process operation work. |
| 247 | * |
| 248 | * For incoming requests, call the protocol request handler. The operation |
| 249 | * result should be -EINPROGRESS at this point. |
Alex Elder | d4a1ff6 | 2014-12-02 08:30:37 -0600 | [diff] [blame] | 250 | * |
| 251 | * For outgoing requests, the operation result value should have |
| 252 | * been set before queueing this. The operation callback function |
| 253 | * allows the original requester to know the request has completed |
| 254 | * and its result is available. |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 255 | */ |
Alex Elder | ee637a9 | 2014-11-21 19:29:13 -0600 | [diff] [blame] | 256 | static void gb_operation_work(struct work_struct *work) |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 257 | { |
| 258 | struct gb_operation *operation; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 259 | |
Alex Elder | ee637a9 | 2014-11-21 19:29:13 -0600 | [diff] [blame] | 260 | operation = container_of(work, struct gb_operation, work); |
Johan Hovold | 3775403 | 2015-03-27 12:41:12 +0100 | [diff] [blame] | 261 | |
Johan Hovold | c600e53 | 2015-07-14 15:43:28 +0200 | [diff] [blame] | 262 | if (gb_operation_is_incoming(operation)) |
| 263 | gb_operation_request_handle(operation); |
| 264 | else |
| 265 | operation->callback(operation); |
Johan Hovold | 3775403 | 2015-03-27 12:41:12 +0100 | [diff] [blame] | 266 | |
Johan Hovold | 3eeac7e | 2015-07-14 15:43:25 +0200 | [diff] [blame] | 267 | gb_operation_put_active(operation); |
Alex Elder | 10c6939 | 2014-11-21 19:29:20 -0600 | [diff] [blame] | 268 | gb_operation_put(operation); |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 271 | static void gb_operation_message_init(struct gb_host_device *hd, |
Alex Elder | dc77922 | 2014-12-02 08:30:33 -0600 | [diff] [blame] | 272 | struct gb_message *message, u16 operation_id, |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 273 | size_t payload_size, u8 type) |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 274 | { |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 275 | struct gb_operation_msg_hdr *header; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 276 | |
Johan Hovold | 24ef485 | 2015-04-07 11:27:21 +0200 | [diff] [blame] | 277 | header = message->buffer; |
Alex Elder | bc46fab | 2014-11-17 18:08:35 -0600 | [diff] [blame] | 278 | |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 279 | message->header = header; |
Alex Elder | 746e0ef | 2014-12-03 12:27:45 -0600 | [diff] [blame] | 280 | message->payload = payload_size ? header + 1 : NULL; |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 281 | message->payload_size = payload_size; |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 282 | |
Alex Elder | ea64cd9 | 2014-12-02 08:30:32 -0600 | [diff] [blame] | 283 | /* |
| 284 | * The type supplied for incoming message buffers will be |
| 285 | * 0x00. Such buffers will be overwritten by arriving data |
| 286 | * so there's no need to initialize the message header. |
| 287 | */ |
Alex Elder | c939c2f | 2014-12-02 17:25:11 -0600 | [diff] [blame] | 288 | if (type != GB_OPERATION_TYPE_INVALID) { |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 289 | u16 message_size = (u16)(sizeof(*header) + payload_size); |
| 290 | |
Alex Elder | ea64cd9 | 2014-12-02 08:30:32 -0600 | [diff] [blame] | 291 | /* |
| 292 | * For a request, the operation id gets filled in |
| 293 | * when the message is sent. For a response, it |
| 294 | * will be copied from the request by the caller. |
| 295 | * |
| 296 | * The result field in a request message must be |
| 297 | * zero. It will be set just prior to sending for |
| 298 | * a response. |
| 299 | */ |
| 300 | header->size = cpu_to_le16(message_size); |
| 301 | header->operation_id = 0; |
| 302 | header->type = type; |
| 303 | header->result = 0; |
| 304 | } |
Alex Elder | dc77922 | 2014-12-02 08:30:33 -0600 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Allocate a message to be used for an operation request or response. |
| 309 | * Both types of message contain a common header. The request message |
| 310 | * for an outgoing operation is outbound, as is the response message |
| 311 | * for an incoming operation. The message header for an outbound |
| 312 | * message is partially initialized here. |
| 313 | * |
| 314 | * The headers for inbound messages don't need to be initialized; |
| 315 | * they'll be filled in by arriving data. |
| 316 | * |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 317 | * Our message buffers have the following layout: |
Alex Elder | dc77922 | 2014-12-02 08:30:33 -0600 | [diff] [blame] | 318 | * message header \_ these combined are |
| 319 | * message payload / the message size |
| 320 | */ |
| 321 | static struct gb_message * |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 322 | gb_operation_message_alloc(struct gb_host_device *hd, u8 type, |
Alex Elder | dc77922 | 2014-12-02 08:30:33 -0600 | [diff] [blame] | 323 | size_t payload_size, gfp_t gfp_flags) |
| 324 | { |
| 325 | struct gb_message *message; |
| 326 | struct gb_operation_msg_hdr *header; |
| 327 | size_t message_size = payload_size + sizeof(*header); |
Alex Elder | dc77922 | 2014-12-02 08:30:33 -0600 | [diff] [blame] | 328 | |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 329 | if (message_size > hd->buffer_size_max) { |
Johan Hovold | b427572 | 2016-02-11 13:52:47 +0100 | [diff] [blame] | 330 | dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n", |
Alex Elder | 0cffcac | 2014-12-02 08:30:35 -0600 | [diff] [blame] | 331 | message_size, hd->buffer_size_max); |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 332 | return NULL; |
Alex Elder | 0cffcac | 2014-12-02 08:30:35 -0600 | [diff] [blame] | 333 | } |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 334 | |
| 335 | /* Allocate the message structure and buffer. */ |
| 336 | message = kmem_cache_zalloc(gb_message_cache, gfp_flags); |
Alex Elder | dc77922 | 2014-12-02 08:30:33 -0600 | [diff] [blame] | 337 | if (!message) |
| 338 | return NULL; |
| 339 | |
Johan Hovold | 24ef485 | 2015-04-07 11:27:21 +0200 | [diff] [blame] | 340 | message->buffer = kzalloc(message_size, gfp_flags); |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 341 | if (!message->buffer) |
| 342 | goto err_free_message; |
| 343 | |
Alex Elder | dc77922 | 2014-12-02 08:30:33 -0600 | [diff] [blame] | 344 | /* Initialize the message. Operation id is filled in later. */ |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 345 | gb_operation_message_init(hd, message, 0, payload_size, type); |
Alex Elder | ea64cd9 | 2014-12-02 08:30:32 -0600 | [diff] [blame] | 346 | |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 347 | return message; |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 348 | |
| 349 | err_free_message: |
| 350 | kmem_cache_free(gb_message_cache, message); |
| 351 | |
| 352 | return NULL; |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 353 | } |
| 354 | |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 355 | static void gb_operation_message_free(struct gb_message *message) |
Alex Elder | c7f82d5 | 2014-11-17 18:08:32 -0600 | [diff] [blame] | 356 | { |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 357 | kfree(message->buffer); |
| 358 | kmem_cache_free(gb_message_cache, message); |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* |
Viresh Kumar | 696e0cc | 2014-11-21 11:26:30 +0530 | [diff] [blame] | 362 | * Map an enum gb_operation_status value (which is represented in a |
| 363 | * message as a single byte) to an appropriate Linux negative errno. |
Alex Elder | bc717fc | 2014-11-19 17:55:04 -0600 | [diff] [blame] | 364 | */ |
Alex Elder | 0c90fff | 2014-12-02 08:30:38 -0600 | [diff] [blame] | 365 | static int gb_operation_status_map(u8 status) |
Alex Elder | bc717fc | 2014-11-19 17:55:04 -0600 | [diff] [blame] | 366 | { |
| 367 | switch (status) { |
| 368 | case GB_OP_SUCCESS: |
| 369 | return 0; |
Alex Elder | bc717fc | 2014-11-19 17:55:04 -0600 | [diff] [blame] | 370 | case GB_OP_INTERRUPTED: |
| 371 | return -EINTR; |
Alex Elder | 57248fa | 2014-12-01 07:53:09 -0600 | [diff] [blame] | 372 | case GB_OP_TIMEOUT: |
| 373 | return -ETIMEDOUT; |
| 374 | case GB_OP_NO_MEMORY: |
| 375 | return -ENOMEM; |
Alex Elder | bc717fc | 2014-11-19 17:55:04 -0600 | [diff] [blame] | 376 | case GB_OP_PROTOCOL_BAD: |
| 377 | return -EPROTONOSUPPORT; |
| 378 | case GB_OP_OVERFLOW: |
Alex Elder | 1a36515 | 2014-11-25 13:06:44 -0600 | [diff] [blame] | 379 | return -EMSGSIZE; |
Alex Elder | 57248fa | 2014-12-01 07:53:09 -0600 | [diff] [blame] | 380 | case GB_OP_INVALID: |
| 381 | return -EINVAL; |
| 382 | case GB_OP_RETRY: |
| 383 | return -EAGAIN; |
Alex Elder | aa26351 | 2014-12-10 08:43:33 -0600 | [diff] [blame] | 384 | case GB_OP_NONEXISTENT: |
| 385 | return -ENODEV; |
Alex Elder | 57248fa | 2014-12-01 07:53:09 -0600 | [diff] [blame] | 386 | case GB_OP_MALFUNCTION: |
| 387 | return -EILSEQ; |
| 388 | case GB_OP_UNKNOWN_ERROR: |
Alex Elder | bc717fc | 2014-11-19 17:55:04 -0600 | [diff] [blame] | 389 | default: |
| 390 | return -EIO; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* |
Alex Elder | 0c90fff | 2014-12-02 08:30:38 -0600 | [diff] [blame] | 395 | * Map a Linux errno value (from operation->errno) into the value |
| 396 | * that should represent it in a response message status sent |
| 397 | * over the wire. Returns an enum gb_operation_status value (which |
| 398 | * is represented in a message as a single byte). |
| 399 | */ |
| 400 | static u8 gb_operation_errno_map(int errno) |
| 401 | { |
| 402 | switch (errno) { |
| 403 | case 0: |
| 404 | return GB_OP_SUCCESS; |
| 405 | case -EINTR: |
| 406 | return GB_OP_INTERRUPTED; |
| 407 | case -ETIMEDOUT: |
| 408 | return GB_OP_TIMEOUT; |
| 409 | case -ENOMEM: |
| 410 | return GB_OP_NO_MEMORY; |
| 411 | case -EPROTONOSUPPORT: |
| 412 | return GB_OP_PROTOCOL_BAD; |
| 413 | case -EMSGSIZE: |
| 414 | return GB_OP_OVERFLOW; /* Could be underflow too */ |
| 415 | case -EINVAL: |
| 416 | return GB_OP_INVALID; |
| 417 | case -EAGAIN: |
| 418 | return GB_OP_RETRY; |
| 419 | case -EILSEQ: |
| 420 | return GB_OP_MALFUNCTION; |
Alex Elder | aa26351 | 2014-12-10 08:43:33 -0600 | [diff] [blame] | 421 | case -ENODEV: |
| 422 | return GB_OP_NONEXISTENT; |
Alex Elder | 0c90fff | 2014-12-02 08:30:38 -0600 | [diff] [blame] | 423 | case -EIO: |
| 424 | default: |
| 425 | return GB_OP_UNKNOWN_ERROR; |
| 426 | } |
| 427 | } |
| 428 | |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 429 | bool gb_operation_response_alloc(struct gb_operation *operation, |
Johan Hovold | 1c7658c | 2015-07-17 18:50:25 +0200 | [diff] [blame] | 430 | size_t response_size, gfp_t gfp) |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 431 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 432 | struct gb_host_device *hd = operation->connection->hd; |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 433 | struct gb_operation_msg_hdr *request_header; |
| 434 | struct gb_message *response; |
| 435 | u8 type; |
| 436 | |
Alex Elder | 6d65337 | 2015-05-07 13:03:52 -0500 | [diff] [blame] | 437 | type = operation->type | GB_MESSAGE_TYPE_RESPONSE; |
Johan Hovold | 1c7658c | 2015-07-17 18:50:25 +0200 | [diff] [blame] | 438 | response = gb_operation_message_alloc(hd, type, response_size, gfp); |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 439 | if (!response) |
| 440 | return false; |
| 441 | response->operation = operation; |
| 442 | |
| 443 | /* |
| 444 | * Size and type get initialized when the message is |
| 445 | * allocated. The errno will be set before sending. All |
| 446 | * that's left is the operation id, which we copy from the |
| 447 | * request message header (as-is, in little-endian order). |
| 448 | */ |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 449 | request_header = operation->request->header; |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 450 | response->header->operation_id = request_header->operation_id; |
| 451 | operation->response = response; |
| 452 | |
| 453 | return true; |
| 454 | } |
Johan Hovold | 1dad6b3 | 2015-03-27 12:41:10 +0100 | [diff] [blame] | 455 | EXPORT_SYMBOL_GPL(gb_operation_response_alloc); |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 456 | |
Alex Elder | 0c90fff | 2014-12-02 08:30:38 -0600 | [diff] [blame] | 457 | /* |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 458 | * Create a Greybus operation to be sent over the given connection. |
Viresh Kumar | 696e0cc | 2014-11-21 11:26:30 +0530 | [diff] [blame] | 459 | * The request buffer will be big enough for a payload of the given |
Alex Elder | ea64cd9 | 2014-12-02 08:30:32 -0600 | [diff] [blame] | 460 | * size. |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 461 | * |
Alex Elder | ea64cd9 | 2014-12-02 08:30:32 -0600 | [diff] [blame] | 462 | * For outgoing requests, the request message's header will be |
| 463 | * initialized with the type of the request and the message size. |
| 464 | * Outgoing operations must also specify the response buffer size, |
| 465 | * which must be sufficient to hold all expected response data. The |
| 466 | * response message header will eventually be overwritten, so there's |
| 467 | * no need to initialize it here. |
| 468 | * |
| 469 | * Request messages for incoming operations can arrive in interrupt |
| 470 | * context, so they must be allocated with GFP_ATOMIC. In this case |
| 471 | * the request buffer will be immediately overwritten, so there is |
| 472 | * no need to initialize the message header. Responsibility for |
| 473 | * allocating a response buffer lies with the incoming request |
| 474 | * handler for a protocol. So we don't allocate that here. |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 475 | * |
| 476 | * Returns a pointer to the new operation or a null pointer if an |
| 477 | * error occurs. |
| 478 | */ |
Alex Elder | 30a2964 | 2014-11-19 17:55:02 -0600 | [diff] [blame] | 479 | static struct gb_operation * |
Alex Elder | ea64cd9 | 2014-12-02 08:30:32 -0600 | [diff] [blame] | 480 | gb_operation_create_common(struct gb_connection *connection, u8 type, |
Johan Hovold | e420721 | 2015-07-01 12:37:22 +0200 | [diff] [blame] | 481 | size_t request_size, size_t response_size, |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 482 | unsigned long op_flags, gfp_t gfp_flags) |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 483 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 484 | struct gb_host_device *hd = connection->hd; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 485 | struct gb_operation *operation; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 486 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 487 | operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags); |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 488 | if (!operation) |
| 489 | return NULL; |
Greg Kroah-Hartman | 6507cce | 2014-10-27 17:58:54 +0800 | [diff] [blame] | 490 | operation->connection = connection; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 491 | |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 492 | operation->request = gb_operation_message_alloc(hd, type, request_size, |
| 493 | gfp_flags); |
| 494 | if (!operation->request) |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 495 | goto err_cache; |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 496 | operation->request->operation = operation; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 497 | |
Alex Elder | ea64cd9 | 2014-12-02 08:30:32 -0600 | [diff] [blame] | 498 | /* Allocate the response buffer for outgoing operations */ |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 499 | if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) { |
Johan Hovold | 1c7658c | 2015-07-17 18:50:25 +0200 | [diff] [blame] | 500 | if (!gb_operation_response_alloc(operation, response_size, |
| 501 | gfp_flags)) { |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 502 | goto err_request; |
Johan Hovold | 1c7658c | 2015-07-17 18:50:25 +0200 | [diff] [blame] | 503 | } |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 504 | } |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 505 | |
| 506 | operation->flags = op_flags; |
| 507 | operation->type = type; |
Alex Elder | 3deb37d | 2014-11-25 11:33:15 -0600 | [diff] [blame] | 508 | operation->errno = -EBADR; /* Initial value--means "never set" */ |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 509 | |
Alex Elder | ee637a9 | 2014-11-21 19:29:13 -0600 | [diff] [blame] | 510 | INIT_WORK(&operation->work, gb_operation_work); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 511 | init_completion(&operation->completion); |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 512 | kref_init(&operation->kref); |
Johan Hovold | fd7134a | 2015-07-14 15:43:26 +0200 | [diff] [blame] | 513 | atomic_set(&operation->waiters, 0); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 514 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 515 | return operation; |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 516 | |
| 517 | err_request: |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 518 | gb_operation_message_free(operation->request); |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 519 | err_cache: |
| 520 | kmem_cache_free(gb_operation_cache, operation); |
| 521 | |
| 522 | return NULL; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Alex Elder | 55f66a8 | 2014-12-02 08:30:31 -0600 | [diff] [blame] | 525 | /* |
| 526 | * Create a new operation associated with the given connection. The |
| 527 | * request and response sizes provided are the number of bytes |
| 528 | * required to hold the request/response payload only. Both of |
| 529 | * these are allowed to be 0. Note that 0x00 is reserved as an |
| 530 | * invalid operation type for all protocols, and this is enforced |
| 531 | * here. |
| 532 | */ |
Alex Elder | 30a2964 | 2014-11-19 17:55:02 -0600 | [diff] [blame] | 533 | struct gb_operation *gb_operation_create(struct gb_connection *connection, |
| 534 | u8 type, size_t request_size, |
Johan Hovold | e420721 | 2015-07-01 12:37:22 +0200 | [diff] [blame] | 535 | size_t response_size, |
| 536 | gfp_t gfp) |
Alex Elder | 30a2964 | 2014-11-19 17:55:02 -0600 | [diff] [blame] | 537 | { |
Alex Elder | c939c2f | 2014-12-02 17:25:11 -0600 | [diff] [blame] | 538 | if (WARN_ON_ONCE(type == GB_OPERATION_TYPE_INVALID)) |
Alex Elder | 55f66a8 | 2014-12-02 08:30:31 -0600 | [diff] [blame] | 539 | return NULL; |
Alex Elder | 6d65337 | 2015-05-07 13:03:52 -0500 | [diff] [blame] | 540 | if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE)) |
| 541 | type &= ~GB_MESSAGE_TYPE_RESPONSE; |
Alex Elder | 55f66a8 | 2014-12-02 08:30:31 -0600 | [diff] [blame] | 542 | |
Alex Elder | ea64cd9 | 2014-12-02 08:30:32 -0600 | [diff] [blame] | 543 | return gb_operation_create_common(connection, type, |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 544 | request_size, response_size, 0, gfp); |
Alex Elder | 30a2964 | 2014-11-19 17:55:02 -0600 | [diff] [blame] | 545 | } |
Greg Kroah-Hartman | df469a9 | 2014-12-23 15:16:52 -0800 | [diff] [blame] | 546 | EXPORT_SYMBOL_GPL(gb_operation_create); |
Alex Elder | 30a2964 | 2014-11-19 17:55:02 -0600 | [diff] [blame] | 547 | |
Johan Hovold | d52b35f | 2015-05-19 11:22:46 +0200 | [diff] [blame] | 548 | size_t gb_operation_get_payload_size_max(struct gb_connection *connection) |
| 549 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 550 | struct gb_host_device *hd = connection->hd; |
Johan Hovold | d52b35f | 2015-05-19 11:22:46 +0200 | [diff] [blame] | 551 | |
| 552 | return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr); |
| 553 | } |
| 554 | EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max); |
| 555 | |
Alex Elder | 30a2964 | 2014-11-19 17:55:02 -0600 | [diff] [blame] | 556 | static struct gb_operation * |
Alex Elder | ea64cd9 | 2014-12-02 08:30:32 -0600 | [diff] [blame] | 557 | gb_operation_create_incoming(struct gb_connection *connection, u16 id, |
Johan Hovold | cfa7969 | 2015-03-27 12:41:18 +0100 | [diff] [blame] | 558 | u8 type, void *data, size_t size) |
Alex Elder | 30a2964 | 2014-11-19 17:55:02 -0600 | [diff] [blame] | 559 | { |
Alex Elder | 34db1f9 | 2014-12-02 08:30:28 -0600 | [diff] [blame] | 560 | struct gb_operation *operation; |
Johan Hovold | cfa7969 | 2015-03-27 12:41:18 +0100 | [diff] [blame] | 561 | size_t request_size; |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 562 | unsigned long flags = GB_OPERATION_FLAG_INCOMING; |
Johan Hovold | cfa7969 | 2015-03-27 12:41:18 +0100 | [diff] [blame] | 563 | |
| 564 | /* Caller has made sure we at least have a message header. */ |
| 565 | request_size = size - sizeof(struct gb_operation_msg_hdr); |
Alex Elder | 34db1f9 | 2014-12-02 08:30:28 -0600 | [diff] [blame] | 566 | |
Johan Hovold | e339881 | 2015-07-01 12:37:27 +0200 | [diff] [blame] | 567 | if (!id) |
| 568 | flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL; |
| 569 | |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 570 | operation = gb_operation_create_common(connection, type, |
| 571 | request_size, 0, flags, GFP_ATOMIC); |
Johan Hovold | 9a586bd | 2015-07-14 15:43:24 +0200 | [diff] [blame] | 572 | if (!operation) |
| 573 | return NULL; |
| 574 | |
| 575 | operation->id = id; |
| 576 | memcpy(operation->request->header, data, size); |
Alex Elder | 34db1f9 | 2014-12-02 08:30:28 -0600 | [diff] [blame] | 577 | |
| 578 | return operation; |
Alex Elder | 30a2964 | 2014-11-19 17:55:02 -0600 | [diff] [blame] | 579 | } |
| 580 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 581 | /* |
Alex Elder | deb4b9e | 2014-11-21 19:29:15 -0600 | [diff] [blame] | 582 | * Get an additional reference on an operation. |
| 583 | */ |
| 584 | void gb_operation_get(struct gb_operation *operation) |
| 585 | { |
| 586 | kref_get(&operation->kref); |
| 587 | } |
Johan Hovold | 1dad6b3 | 2015-03-27 12:41:10 +0100 | [diff] [blame] | 588 | EXPORT_SYMBOL_GPL(gb_operation_get); |
Alex Elder | deb4b9e | 2014-11-21 19:29:15 -0600 | [diff] [blame] | 589 | |
| 590 | /* |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 591 | * Destroy a previously created operation. |
| 592 | */ |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 593 | static void _gb_operation_destroy(struct kref *kref) |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 594 | { |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 595 | struct gb_operation *operation; |
| 596 | |
| 597 | operation = container_of(kref, struct gb_operation, kref); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 598 | |
Johan Hovold | 94896676 | 2015-03-27 12:41:17 +0100 | [diff] [blame] | 599 | if (operation->response) |
| 600 | gb_operation_message_free(operation->response); |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 601 | gb_operation_message_free(operation->request); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 602 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 603 | kmem_cache_free(gb_operation_cache, operation); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 604 | } |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 605 | |
Alex Elder | deb4b9e | 2014-11-21 19:29:15 -0600 | [diff] [blame] | 606 | /* |
| 607 | * Drop a reference on an operation, and destroy it when the last |
| 608 | * one is gone. |
| 609 | */ |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 610 | void gb_operation_put(struct gb_operation *operation) |
| 611 | { |
Johan Hovold | 85109f7 | 2015-07-09 15:17:58 +0200 | [diff] [blame] | 612 | if (WARN_ON(!operation)) |
| 613 | return; |
| 614 | |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 615 | kref_put(&operation->kref, _gb_operation_destroy); |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 616 | } |
Greg Kroah-Hartman | df469a9 | 2014-12-23 15:16:52 -0800 | [diff] [blame] | 617 | EXPORT_SYMBOL_GPL(gb_operation_put); |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 618 | |
Alex Elder | 10c6939 | 2014-11-21 19:29:20 -0600 | [diff] [blame] | 619 | /* Tell the requester we're done */ |
| 620 | static void gb_operation_sync_callback(struct gb_operation *operation) |
| 621 | { |
| 622 | complete(&operation->completion); |
| 623 | } |
| 624 | |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 625 | /* |
Johan Hovold | 3775403 | 2015-03-27 12:41:12 +0100 | [diff] [blame] | 626 | * Send an operation request message. The caller has filled in any payload so |
| 627 | * the request message is ready to go. The callback function supplied will be |
| 628 | * called when the response message has arrived indicating the operation is |
| 629 | * complete. In that case, the callback function is responsible for fetching |
| 630 | * the result of the operation using gb_operation_result() if desired, and |
| 631 | * dropping the initial reference to the operation. |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 632 | */ |
| 633 | int gb_operation_request_send(struct gb_operation *operation, |
Johan Hovold | a52c435 | 2015-07-01 12:37:23 +0200 | [diff] [blame] | 634 | gb_operation_callback callback, |
| 635 | gfp_t gfp) |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 636 | { |
Alex Elder | afb2e13 | 2014-12-03 08:35:07 -0600 | [diff] [blame] | 637 | struct gb_connection *connection = operation->connection; |
| 638 | struct gb_operation_msg_hdr *header; |
Alex Elder | 4afb7fd | 2014-12-03 08:35:08 -0600 | [diff] [blame] | 639 | unsigned int cycle; |
Johan Hovold | ea2c2ee | 2015-03-27 12:41:14 +0100 | [diff] [blame] | 640 | int ret; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 641 | |
Johan Hovold | 3775403 | 2015-03-27 12:41:12 +0100 | [diff] [blame] | 642 | if (!callback) |
| 643 | return -EINVAL; |
Alex Elder | c25572c | 2014-12-03 08:35:09 -0600 | [diff] [blame] | 644 | /* |
| 645 | * Record the callback function, which is executed in |
| 646 | * non-atomic (workqueue) context when the final result |
| 647 | * of an operation has been set. |
| 648 | */ |
| 649 | operation->callback = callback; |
Alex Elder | afb2e13 | 2014-12-03 08:35:07 -0600 | [diff] [blame] | 650 | |
| 651 | /* |
| 652 | * Assign the operation's id, and store it in the request header. |
| 653 | * Zero is a reserved operation id. |
| 654 | */ |
Alex Elder | 4afb7fd | 2014-12-03 08:35:08 -0600 | [diff] [blame] | 655 | cycle = (unsigned int)atomic_inc_return(&connection->op_cycle); |
| 656 | operation->id = (u16)(cycle % U16_MAX + 1); |
Alex Elder | afb2e13 | 2014-12-03 08:35:07 -0600 | [diff] [blame] | 657 | header = operation->request->header; |
| 658 | header->operation_id = cpu_to_le16(operation->id); |
Alex Elder | e8b48d1 | 2014-11-20 15:37:06 -0600 | [diff] [blame] | 659 | |
Alex Elder | 3deb37d | 2014-11-25 11:33:15 -0600 | [diff] [blame] | 660 | gb_operation_result_set(operation, -EINPROGRESS); |
Alex Elder | c25572c | 2014-12-03 08:35:09 -0600 | [diff] [blame] | 661 | |
Johan Hovold | 3325a4a | 2015-07-14 15:43:33 +0200 | [diff] [blame] | 662 | /* |
| 663 | * Get an extra reference on the operation. It'll be dropped when the |
| 664 | * operation completes. |
| 665 | */ |
| 666 | gb_operation_get(operation); |
| 667 | ret = gb_operation_get_active(operation); |
| 668 | if (ret) |
| 669 | goto err_put; |
| 670 | |
Johan Hovold | a52c435 | 2015-07-01 12:37:23 +0200 | [diff] [blame] | 671 | ret = gb_message_send(operation->request, gfp); |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 672 | if (ret) |
| 673 | goto err_put_active; |
| 674 | |
| 675 | return 0; |
| 676 | |
| 677 | err_put_active: |
| 678 | gb_operation_put_active(operation); |
| 679 | err_put: |
| 680 | gb_operation_put(operation); |
Johan Hovold | ea2c2ee | 2015-03-27 12:41:14 +0100 | [diff] [blame] | 681 | |
| 682 | return ret; |
Alex Elder | c25572c | 2014-12-03 08:35:09 -0600 | [diff] [blame] | 683 | } |
Johan Hovold | 1dad6b3 | 2015-03-27 12:41:10 +0100 | [diff] [blame] | 684 | EXPORT_SYMBOL_GPL(gb_operation_request_send); |
Alex Elder | c25572c | 2014-12-03 08:35:09 -0600 | [diff] [blame] | 685 | |
| 686 | /* |
| 687 | * Send a synchronous operation. This function is expected to |
| 688 | * block, returning only when the response has arrived, (or when an |
| 689 | * error is detected. The return value is the result of the |
| 690 | * operation. |
| 691 | */ |
Johan Hovold | 4f2c08a | 2015-07-14 15:43:36 +0200 | [diff] [blame] | 692 | int gb_operation_request_send_sync_timeout(struct gb_operation *operation, |
| 693 | unsigned int timeout) |
Alex Elder | c25572c | 2014-12-03 08:35:09 -0600 | [diff] [blame] | 694 | { |
| 695 | int ret; |
Johan Hovold | 4f2c08a | 2015-07-14 15:43:36 +0200 | [diff] [blame] | 696 | unsigned long timeout_jiffies; |
Alex Elder | c25572c | 2014-12-03 08:35:09 -0600 | [diff] [blame] | 697 | |
Johan Hovold | a52c435 | 2015-07-01 12:37:23 +0200 | [diff] [blame] | 698 | ret = gb_operation_request_send(operation, gb_operation_sync_callback, |
| 699 | GFP_KERNEL); |
Alex Elder | c25572c | 2014-12-03 08:35:09 -0600 | [diff] [blame] | 700 | if (ret) |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 701 | return ret; |
Alex Elder | 8350e7a | 2014-11-12 15:17:52 -0600 | [diff] [blame] | 702 | |
Johan Hovold | 4f2c08a | 2015-07-14 15:43:36 +0200 | [diff] [blame] | 703 | if (timeout) |
| 704 | timeout_jiffies = msecs_to_jiffies(timeout); |
| 705 | else |
| 706 | timeout_jiffies = MAX_SCHEDULE_TIMEOUT; |
| 707 | |
| 708 | ret = wait_for_completion_interruptible_timeout(&operation->completion, |
| 709 | timeout_jiffies); |
Perry Hung | 7bad4e8 | 2015-01-14 16:19:26 -0500 | [diff] [blame] | 710 | if (ret < 0) { |
| 711 | /* Cancel the operation if interrupted */ |
Alex Elder | 1a36515 | 2014-11-25 13:06:44 -0600 | [diff] [blame] | 712 | gb_operation_cancel(operation, -ECANCELED); |
Perry Hung | 7bad4e8 | 2015-01-14 16:19:26 -0500 | [diff] [blame] | 713 | } else if (ret == 0) { |
| 714 | /* Cancel the operation if op timed out */ |
| 715 | gb_operation_cancel(operation, -ETIMEDOUT); |
| 716 | } |
Alex Elder | 2cf72a2 | 2014-11-21 19:29:19 -0600 | [diff] [blame] | 717 | |
Alex Elder | ba986b5 | 2014-11-25 11:33:13 -0600 | [diff] [blame] | 718 | return gb_operation_result(operation); |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 719 | } |
Johan Hovold | 4f2c08a | 2015-07-14 15:43:36 +0200 | [diff] [blame] | 720 | EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout); |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 721 | |
| 722 | /* |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 723 | * Send a response for an incoming operation request. A non-zero |
| 724 | * errno indicates a failed operation. |
| 725 | * |
| 726 | * If there is any response payload, the incoming request handler is |
| 727 | * responsible for allocating the response message. Otherwise the |
| 728 | * it can simply supply the result errno; this function will |
| 729 | * allocate the response message if necessary. |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 730 | */ |
Johan Hovold | abb722e | 2015-07-01 12:37:24 +0200 | [diff] [blame] | 731 | static int gb_operation_response_send(struct gb_operation *operation, |
| 732 | int errno) |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 733 | { |
Johan Hovold | e1baa3f | 2015-03-27 12:41:19 +0100 | [diff] [blame] | 734 | struct gb_connection *connection = operation->connection; |
Johan Hovold | 0fb5acc | 2015-03-27 12:41:13 +0100 | [diff] [blame] | 735 | int ret; |
| 736 | |
Johan Hovold | fde7382 | 2015-07-01 12:37:30 +0200 | [diff] [blame] | 737 | if (!operation->response && |
| 738 | !gb_operation_is_unidirectional(operation)) { |
Johan Hovold | 1c7658c | 2015-07-17 18:50:25 +0200 | [diff] [blame] | 739 | if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL)) |
Johan Hovold | fde7382 | 2015-07-01 12:37:30 +0200 | [diff] [blame] | 740 | return -ENOMEM; |
| 741 | } |
| 742 | |
Alex Elder | d2d2c0f | 2014-12-02 08:30:36 -0600 | [diff] [blame] | 743 | /* Record the result */ |
| 744 | if (!gb_operation_result_set(operation, errno)) { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 745 | dev_err(&connection->hd->dev, "request result already set\n"); |
Alex Elder | d2d2c0f | 2014-12-02 08:30:36 -0600 | [diff] [blame] | 746 | return -EIO; /* Shouldn't happen */ |
| 747 | } |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 748 | |
Johan Hovold | 1d771fe | 2015-05-26 15:29:18 +0200 | [diff] [blame] | 749 | /* Sender of request does not care about response. */ |
Johan Hovold | e339881 | 2015-07-01 12:37:27 +0200 | [diff] [blame] | 750 | if (gb_operation_is_unidirectional(operation)) |
Johan Hovold | 1d771fe | 2015-05-26 15:29:18 +0200 | [diff] [blame] | 751 | return 0; |
| 752 | |
Johan Hovold | 0fb5acc | 2015-03-27 12:41:13 +0100 | [diff] [blame] | 753 | /* Reference will be dropped when message has been sent. */ |
| 754 | gb_operation_get(operation); |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 755 | ret = gb_operation_get_active(operation); |
| 756 | if (ret) |
| 757 | goto err_put; |
Johan Hovold | 0fb5acc | 2015-03-27 12:41:13 +0100 | [diff] [blame] | 758 | |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 759 | /* Fill in the response header and send it */ |
| 760 | operation->response->header->result = gb_operation_errno_map(errno); |
| 761 | |
Johan Hovold | a52c435 | 2015-07-01 12:37:23 +0200 | [diff] [blame] | 762 | ret = gb_message_send(operation->response, GFP_KERNEL); |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 763 | if (ret) |
| 764 | goto err_put_active; |
| 765 | |
| 766 | return 0; |
| 767 | |
| 768 | err_put_active: |
| 769 | gb_operation_put_active(operation); |
| 770 | err_put: |
| 771 | gb_operation_put(operation); |
Johan Hovold | 0fb5acc | 2015-03-27 12:41:13 +0100 | [diff] [blame] | 772 | |
| 773 | return ret; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 774 | } |
| 775 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 776 | /* |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 777 | * This function is called when a message send request has completed. |
Alex Elder | d98b52b | 2014-11-20 16:09:17 -0600 | [diff] [blame] | 778 | */ |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 779 | void greybus_message_sent(struct gb_host_device *hd, |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 780 | struct gb_message *message, int status) |
Alex Elder | d98b52b | 2014-11-20 16:09:17 -0600 | [diff] [blame] | 781 | { |
Johan Hovold | a4e0846 | 2015-07-22 17:49:18 +0200 | [diff] [blame] | 782 | struct gb_operation *operation = message->operation; |
| 783 | struct gb_connection *connection = operation->connection; |
Alex Elder | d98b52b | 2014-11-20 16:09:17 -0600 | [diff] [blame] | 784 | |
Alex Elder | d4a1ff6 | 2014-12-02 08:30:37 -0600 | [diff] [blame] | 785 | /* |
| 786 | * If the message was a response, we just need to drop our |
| 787 | * reference to the operation. If an error occurred, report |
| 788 | * it. |
| 789 | * |
| 790 | * For requests, if there's no error, there's nothing more |
| 791 | * to do until the response arrives. If an error occurred |
| 792 | * attempting to send it, record that as the result of |
| 793 | * the operation and schedule its completion. |
| 794 | */ |
Alex Elder | d4a1ff6 | 2014-12-02 08:30:37 -0600 | [diff] [blame] | 795 | if (message == operation->response) { |
Johan Hovold | e1baa3f | 2015-03-27 12:41:19 +0100 | [diff] [blame] | 796 | if (status) { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 797 | dev_err(&connection->hd->dev, |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 798 | "%s: error sending response 0x%02x: %d\n", |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 799 | connection->name, operation->type, status); |
Johan Hovold | e1baa3f | 2015-03-27 12:41:19 +0100 | [diff] [blame] | 800 | } |
Johan Hovold | 3eeac7e | 2015-07-14 15:43:25 +0200 | [diff] [blame] | 801 | gb_operation_put_active(operation); |
Alex Elder | d4a1ff6 | 2014-12-02 08:30:37 -0600 | [diff] [blame] | 802 | gb_operation_put(operation); |
| 803 | } else if (status) { |
Johan Hovold | 701615f | 2015-07-23 10:50:03 +0200 | [diff] [blame] | 804 | if (gb_operation_result_set(operation, status)) { |
| 805 | queue_work(gb_operation_completion_wq, |
| 806 | &operation->work); |
| 807 | } |
Alex Elder | d4a1ff6 | 2014-12-02 08:30:37 -0600 | [diff] [blame] | 808 | } |
Alex Elder | d98b52b | 2014-11-20 16:09:17 -0600 | [diff] [blame] | 809 | } |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 810 | EXPORT_SYMBOL_GPL(greybus_message_sent); |
Alex Elder | d98b52b | 2014-11-20 16:09:17 -0600 | [diff] [blame] | 811 | |
| 812 | /* |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 813 | * We've received data on a connection, and it doesn't look like a |
| 814 | * response, so we assume it's a request. |
Alex Elder | 78496db | 2014-11-17 08:08:39 -0600 | [diff] [blame] | 815 | * |
| 816 | * This is called in interrupt context, so just copy the incoming |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 817 | * data into the request buffer and handle the rest via workqueue. |
| 818 | */ |
Greg Kroah-Hartman | 85a0442 | 2014-12-01 20:42:20 -0800 | [diff] [blame] | 819 | static void gb_connection_recv_request(struct gb_connection *connection, |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 820 | u16 operation_id, u8 type, |
| 821 | void *data, size_t size) |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 822 | { |
| 823 | struct gb_operation *operation; |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 824 | int ret; |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 825 | |
Alex Elder | 34db1f9 | 2014-12-02 08:30:28 -0600 | [diff] [blame] | 826 | operation = gb_operation_create_incoming(connection, operation_id, |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 827 | type, data, size); |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 828 | if (!operation) { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 829 | dev_err(&connection->hd->dev, |
| 830 | "%s: can't create incoming operation\n", |
| 831 | connection->name); |
Johan Hovold | ff65e20 | 2015-10-13 19:10:22 +0200 | [diff] [blame] | 832 | return; |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 833 | } |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 834 | |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 835 | ret = gb_operation_get_active(operation); |
| 836 | if (ret) { |
| 837 | gb_operation_put(operation); |
| 838 | return; |
| 839 | } |
Bryan O'Donoghue | 5c8ad59 | 2015-09-18 16:38:45 +0100 | [diff] [blame] | 840 | trace_gb_message_recv_request(operation->request); |
Johan Hovold | 3eeac7e | 2015-07-14 15:43:25 +0200 | [diff] [blame] | 841 | |
Alex Elder | d4a1ff6 | 2014-12-02 08:30:37 -0600 | [diff] [blame] | 842 | /* |
Johan Hovold | c600e53 | 2015-07-14 15:43:28 +0200 | [diff] [blame] | 843 | * The initial reference to the operation will be dropped when the |
| 844 | * request handler returns. |
Alex Elder | d4a1ff6 | 2014-12-02 08:30:37 -0600 | [diff] [blame] | 845 | */ |
Alex Elder | d4a1ff6 | 2014-12-02 08:30:37 -0600 | [diff] [blame] | 846 | if (gb_operation_result_set(operation, -EINPROGRESS)) |
Johan Hovold | 5a5bc35 | 2015-07-23 10:50:02 +0200 | [diff] [blame] | 847 | queue_work(connection->wq, &operation->work); |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /* |
| 851 | * We've received data that appears to be an operation response |
| 852 | * message. Look up the operation, and record that we've received |
Viresh Kumar | 696e0cc | 2014-11-21 11:26:30 +0530 | [diff] [blame] | 853 | * its response. |
Alex Elder | 78496db | 2014-11-17 08:08:39 -0600 | [diff] [blame] | 854 | * |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 855 | * This is called in interrupt context, so just copy the incoming |
| 856 | * data into the response buffer and handle the rest via workqueue. |
| 857 | */ |
| 858 | static void gb_connection_recv_response(struct gb_connection *connection, |
Alex Elder | 64ce39a | 2014-12-02 08:30:30 -0600 | [diff] [blame] | 859 | u16 operation_id, u8 result, void *data, size_t size) |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 860 | { |
| 861 | struct gb_operation *operation; |
| 862 | struct gb_message *message; |
Alex Elder | 64ce39a | 2014-12-02 08:30:30 -0600 | [diff] [blame] | 863 | int errno = gb_operation_status_map(result); |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 864 | size_t message_size; |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 865 | |
Johan Hovold | 048a7ff | 2015-07-09 15:18:00 +0200 | [diff] [blame] | 866 | operation = gb_operation_find_outgoing(connection, operation_id); |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 867 | if (!operation) { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 868 | dev_err(&connection->hd->dev, |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 869 | "%s: unexpected response id 0x%04x received\n", |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 870 | connection->name, operation_id); |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 871 | return; |
| 872 | } |
| 873 | |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 874 | message = operation->response; |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 875 | message_size = sizeof(*message->header) + message->payload_size; |
| 876 | if (!errno && size != message_size) { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 877 | dev_err(&connection->hd->dev, |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 878 | "%s: malformed response 0x%02x received (%zu != %zu)\n", |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 879 | connection->name, message->header->type, size, |
| 880 | message_size); |
Alex Elder | 64ce39a | 2014-12-02 08:30:30 -0600 | [diff] [blame] | 881 | errno = -EMSGSIZE; |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 882 | } |
Bryan O'Donoghue | 5c8ad59 | 2015-09-18 16:38:45 +0100 | [diff] [blame] | 883 | trace_gb_message_recv_response(operation->response); |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 884 | |
Alex Elder | 25d0f81 | 2014-11-19 17:55:05 -0600 | [diff] [blame] | 885 | /* We must ignore the payload if a bad status is returned */ |
Alex Elder | 64ce39a | 2014-12-02 08:30:30 -0600 | [diff] [blame] | 886 | if (errno) |
Alex Elder | f71e1cc | 2014-12-02 08:30:29 -0600 | [diff] [blame] | 887 | size = sizeof(*message->header); |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 888 | |
| 889 | /* The rest will be handled in work queue context */ |
Johan Hovold | e4340b1 | 2015-07-09 15:18:01 +0200 | [diff] [blame] | 890 | if (gb_operation_result_set(operation, errno)) { |
| 891 | memcpy(message->header, data, size); |
Johan Hovold | 701615f | 2015-07-23 10:50:03 +0200 | [diff] [blame] | 892 | queue_work(gb_operation_completion_wq, &operation->work); |
Johan Hovold | e4340b1 | 2015-07-09 15:18:01 +0200 | [diff] [blame] | 893 | } |
Johan Hovold | 0581f28e | 2015-07-09 15:17:59 +0200 | [diff] [blame] | 894 | |
| 895 | gb_operation_put(operation); |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* |
| 899 | * Handle data arriving on a connection. As soon as we return the |
| 900 | * supplied data buffer will be reused (so unless we do something |
| 901 | * with, it's effectively dropped). |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 902 | */ |
Alex Elder | 61089e8 | 2014-11-18 13:26:50 -0600 | [diff] [blame] | 903 | void gb_connection_recv(struct gb_connection *connection, |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 904 | void *data, size_t size) |
| 905 | { |
Johan Hovold | 564c72b | 2015-04-07 11:27:13 +0200 | [diff] [blame] | 906 | struct gb_operation_msg_hdr header; |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 907 | struct device *dev = &connection->hd->dev; |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 908 | size_t msg_size; |
| 909 | u16 operation_id; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 910 | |
Johan Hovold | 570dfa7 | 2016-01-19 12:51:07 +0100 | [diff] [blame] | 911 | if (connection->state != GB_CONNECTION_STATE_ENABLED && |
| 912 | connection->state != GB_CONNECTION_STATE_ENABLED_TX) { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 913 | dev_warn(dev, "%s: dropping %zu received bytes\n", |
| 914 | connection->name, size); |
Alex Elder | 36561f2 | 2014-10-22 02:04:30 -0500 | [diff] [blame] | 915 | return; |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 916 | } |
Alex Elder | 36561f2 | 2014-10-22 02:04:30 -0500 | [diff] [blame] | 917 | |
Johan Hovold | 564c72b | 2015-04-07 11:27:13 +0200 | [diff] [blame] | 918 | if (size < sizeof(header)) { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 919 | dev_err(dev, "%s: short message received\n", connection->name); |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 920 | return; |
| 921 | } |
| 922 | |
Johan Hovold | 564c72b | 2015-04-07 11:27:13 +0200 | [diff] [blame] | 923 | /* Use memcpy as data may be unaligned */ |
| 924 | memcpy(&header, data, sizeof(header)); |
| 925 | msg_size = le16_to_cpu(header.size); |
Johan Hovold | 0150bd7 | 2015-03-27 12:41:20 +0100 | [diff] [blame] | 926 | if (size < msg_size) { |
Greg Kroah-Hartman | c3aa655 | 2015-10-14 11:21:06 -0700 | [diff] [blame] | 927 | dev_err(dev, |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 928 | "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n", |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 929 | connection->name, le16_to_cpu(header.operation_id), |
| 930 | header.type, size, msg_size); |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 931 | return; /* XXX Should still complete operation */ |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 932 | } |
| 933 | |
Johan Hovold | 564c72b | 2015-04-07 11:27:13 +0200 | [diff] [blame] | 934 | operation_id = le16_to_cpu(header.operation_id); |
Alex Elder | 6d65337 | 2015-05-07 13:03:52 -0500 | [diff] [blame] | 935 | if (header.type & GB_MESSAGE_TYPE_RESPONSE) |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 936 | gb_connection_recv_response(connection, operation_id, |
Johan Hovold | 564c72b | 2015-04-07 11:27:13 +0200 | [diff] [blame] | 937 | header.result, data, msg_size); |
Alex Elder | d37b1db | 2014-11-19 12:27:17 -0600 | [diff] [blame] | 938 | else |
| 939 | gb_connection_recv_request(connection, operation_id, |
Johan Hovold | 564c72b | 2015-04-07 11:27:13 +0200 | [diff] [blame] | 940 | header.type, data, msg_size); |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 941 | } |
| 942 | |
Alex Elder | e1158df | 2014-10-22 02:04:29 -0500 | [diff] [blame] | 943 | /* |
Johan Hovold | 5a3be76 | 2015-07-14 15:43:35 +0200 | [diff] [blame] | 944 | * Cancel an outgoing operation synchronously, and record the given error to |
| 945 | * indicate why. |
Alex Elder | e1158df | 2014-10-22 02:04:29 -0500 | [diff] [blame] | 946 | */ |
Alex Elder | f68c05c | 2014-11-21 19:29:17 -0600 | [diff] [blame] | 947 | void gb_operation_cancel(struct gb_operation *operation, int errno) |
Alex Elder | e1158df | 2014-10-22 02:04:29 -0500 | [diff] [blame] | 948 | { |
Johan Hovold | 5a3be76 | 2015-07-14 15:43:35 +0200 | [diff] [blame] | 949 | if (WARN_ON(gb_operation_is_incoming(operation))) |
| 950 | return; |
| 951 | |
| 952 | if (gb_operation_result_set(operation, errno)) { |
| 953 | gb_message_cancel(operation->request); |
Johan Hovold | 701615f | 2015-07-23 10:50:03 +0200 | [diff] [blame] | 954 | queue_work(gb_operation_completion_wq, &operation->work); |
Alex Elder | abe9a30 | 2014-11-25 11:33:14 -0600 | [diff] [blame] | 955 | } |
Bryan O'Donoghue | 5c8ad59 | 2015-09-18 16:38:45 +0100 | [diff] [blame] | 956 | trace_gb_message_cancel_outgoing(operation->request); |
Johan Hovold | fd7134a | 2015-07-14 15:43:26 +0200 | [diff] [blame] | 957 | |
| 958 | atomic_inc(&operation->waiters); |
| 959 | wait_event(gb_operation_cancellation_queue, |
| 960 | !gb_operation_is_active(operation)); |
| 961 | atomic_dec(&operation->waiters); |
Alex Elder | e1158df | 2014-10-22 02:04:29 -0500 | [diff] [blame] | 962 | } |
Johan Hovold | 1dad6b3 | 2015-03-27 12:41:10 +0100 | [diff] [blame] | 963 | EXPORT_SYMBOL_GPL(gb_operation_cancel); |
Alex Elder | e1158df | 2014-10-22 02:04:29 -0500 | [diff] [blame] | 964 | |
Johan Hovold | 5a3be76 | 2015-07-14 15:43:35 +0200 | [diff] [blame] | 965 | /* |
| 966 | * Cancel an incoming operation synchronously. Called during connection tear |
| 967 | * down. |
| 968 | */ |
| 969 | void gb_operation_cancel_incoming(struct gb_operation *operation, int errno) |
| 970 | { |
| 971 | if (WARN_ON(!gb_operation_is_incoming(operation))) |
| 972 | return; |
| 973 | |
| 974 | if (!gb_operation_is_unidirectional(operation)) { |
| 975 | /* |
| 976 | * Make sure the request handler has submitted the response |
| 977 | * before cancelling it. |
| 978 | */ |
| 979 | flush_work(&operation->work); |
| 980 | if (!gb_operation_result_set(operation, errno)) |
| 981 | gb_message_cancel(operation->response); |
| 982 | } |
Bryan O'Donoghue | 5c8ad59 | 2015-09-18 16:38:45 +0100 | [diff] [blame] | 983 | trace_gb_message_cancel_incoming(operation->response); |
Johan Hovold | 5a3be76 | 2015-07-14 15:43:35 +0200 | [diff] [blame] | 984 | |
| 985 | atomic_inc(&operation->waiters); |
| 986 | wait_event(gb_operation_cancellation_queue, |
| 987 | !gb_operation_is_active(operation)); |
| 988 | atomic_dec(&operation->waiters); |
| 989 | } |
| 990 | |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 991 | /** |
| 992 | * gb_operation_sync: implement a "simple" synchronous gb operation. |
| 993 | * @connection: the Greybus connection to send this to |
| 994 | * @type: the type of operation to send |
| 995 | * @request: pointer to a memory buffer to copy the request from |
| 996 | * @request_size: size of @request |
| 997 | * @response: pointer to a memory buffer to copy the response to |
| 998 | * @response_size: the size of @response. |
Johan Hovold | 129a06f | 2015-07-14 15:43:37 +0200 | [diff] [blame] | 999 | * @timeout: operation timeout in milliseconds |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 1000 | * |
| 1001 | * This function implements a simple synchronous Greybus operation. It sends |
| 1002 | * the provided operation request and waits (sleeps) until the corresponding |
| 1003 | * operation response message has been successfully received, or an error |
| 1004 | * occurs. @request and @response are buffers to hold the request and response |
| 1005 | * data respectively, and if they are not NULL, their size must be specified in |
| 1006 | * @request_size and @response_size. |
| 1007 | * |
| 1008 | * If a response payload is to come back, and @response is not NULL, |
| 1009 | * @response_size number of bytes will be copied into @response if the operation |
| 1010 | * is successful. |
| 1011 | * |
| 1012 | * If there is an error, the response buffer is left alone. |
| 1013 | */ |
Johan Hovold | 129a06f | 2015-07-14 15:43:37 +0200 | [diff] [blame] | 1014 | int gb_operation_sync_timeout(struct gb_connection *connection, int type, |
| 1015 | void *request, int request_size, |
| 1016 | void *response, int response_size, |
| 1017 | unsigned int timeout) |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 1018 | { |
| 1019 | struct gb_operation *operation; |
| 1020 | int ret; |
| 1021 | |
| 1022 | if ((response_size && !response) || |
| 1023 | (request_size && !request)) |
| 1024 | return -EINVAL; |
| 1025 | |
| 1026 | operation = gb_operation_create(connection, type, |
Johan Hovold | e420721 | 2015-07-01 12:37:22 +0200 | [diff] [blame] | 1027 | request_size, response_size, |
| 1028 | GFP_KERNEL); |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 1029 | if (!operation) |
| 1030 | return -ENOMEM; |
| 1031 | |
| 1032 | if (request_size) |
Alex Elder | 6cd6ec5 | 2014-12-02 17:03:51 -0600 | [diff] [blame] | 1033 | memcpy(operation->request->payload, request, request_size); |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 1034 | |
Johan Hovold | 129a06f | 2015-07-14 15:43:37 +0200 | [diff] [blame] | 1035 | ret = gb_operation_request_send_sync_timeout(operation, timeout); |
Johan Hovold | ee8f81b | 2015-03-19 16:46:18 +0100 | [diff] [blame] | 1036 | if (ret) { |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 1037 | dev_err(&connection->hd->dev, |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 1038 | "%s: synchronous operation of type 0x%02x failed: %d\n", |
Johan Hovold | 25cdd7a | 2015-11-25 15:59:16 +0100 | [diff] [blame] | 1039 | connection->name, type, ret); |
Johan Hovold | ee8f81b | 2015-03-19 16:46:18 +0100 | [diff] [blame] | 1040 | } else { |
| 1041 | if (response_size) { |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 1042 | memcpy(response, operation->response->payload, |
| 1043 | response_size); |
Johan Hovold | ee8f81b | 2015-03-19 16:46:18 +0100 | [diff] [blame] | 1044 | } |
| 1045 | } |
Johan Hovold | 6ab1ce4 | 2015-09-26 17:59:15 -0700 | [diff] [blame] | 1046 | |
| 1047 | gb_operation_put(operation); |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 1048 | |
| 1049 | return ret; |
| 1050 | } |
Johan Hovold | 129a06f | 2015-07-14 15:43:37 +0200 | [diff] [blame] | 1051 | EXPORT_SYMBOL_GPL(gb_operation_sync_timeout); |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 1052 | |
Alex Elder | 47ed2c9 | 2015-06-09 17:42:50 -0500 | [diff] [blame] | 1053 | int __init gb_operation_init(void) |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 1054 | { |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 1055 | gb_message_cache = kmem_cache_create("gb_message_cache", |
| 1056 | sizeof(struct gb_message), 0, 0, NULL); |
| 1057 | if (!gb_message_cache) |
Alex Elder | 0cffcac | 2014-12-02 08:30:35 -0600 | [diff] [blame] | 1058 | return -ENOMEM; |
| 1059 | |
Alex Elder | 5b3db0d | 2014-10-20 10:27:56 -0500 | [diff] [blame] | 1060 | gb_operation_cache = kmem_cache_create("gb_operation_cache", |
| 1061 | sizeof(struct gb_operation), 0, 0, NULL); |
| 1062 | if (!gb_operation_cache) |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 1063 | goto err_destroy_message_cache; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 1064 | |
Johan Hovold | 701615f | 2015-07-23 10:50:03 +0200 | [diff] [blame] | 1065 | gb_operation_completion_wq = alloc_workqueue("greybus_completion", |
| 1066 | 0, 0); |
| 1067 | if (!gb_operation_completion_wq) |
| 1068 | goto err_destroy_operation_cache; |
| 1069 | |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 1070 | return 0; |
Johan Hovold | 5a5bc35 | 2015-07-23 10:50:02 +0200 | [diff] [blame] | 1071 | |
Johan Hovold | 701615f | 2015-07-23 10:50:03 +0200 | [diff] [blame] | 1072 | err_destroy_operation_cache: |
| 1073 | kmem_cache_destroy(gb_operation_cache); |
| 1074 | gb_operation_cache = NULL; |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 1075 | err_destroy_message_cache: |
| 1076 | kmem_cache_destroy(gb_message_cache); |
| 1077 | gb_message_cache = NULL; |
Alex Elder | 0cffcac | 2014-12-02 08:30:35 -0600 | [diff] [blame] | 1078 | |
| 1079 | return -ENOMEM; |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 1080 | } |
| 1081 | |
Alex Elder | f35ab90 | 2015-06-09 17:42:51 -0500 | [diff] [blame] | 1082 | void gb_operation_exit(void) |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 1083 | { |
Johan Hovold | 701615f | 2015-07-23 10:50:03 +0200 | [diff] [blame] | 1084 | destroy_workqueue(gb_operation_completion_wq); |
| 1085 | gb_operation_completion_wq = NULL; |
Viresh Kumar | 837b3b7 | 2014-11-14 17:25:00 +0530 | [diff] [blame] | 1086 | kmem_cache_destroy(gb_operation_cache); |
| 1087 | gb_operation_cache = NULL; |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 1088 | kmem_cache_destroy(gb_message_cache); |
| 1089 | gb_message_cache = NULL; |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 1090 | } |