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