blob: 8459e9bc07498988ab46b5fdf411bcd679299ca4 [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Alex Eldere88afa52014-10-01 21:54:15 -05002/*
3 * Greybus operations
4 *
Alex Elderd3d2bea2015-03-26 21:25:01 -05005 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
Alex Eldere88afa52014-10-01 21:54:15 -05007 */
8
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <linux/module.h>
Johan Hovoldfd7134a2015-07-14 15:43:26 +020012#include <linux/sched.h>
13#include <linux/wait.h>
Alex Eldere88afa52014-10-01 21:54:15 -050014#include <linux/workqueue.h>
Greg Kroah-Hartmanec0ad862019-08-25 07:54:27 +020015#include <linux/greybus.h>
Alex Eldere88afa52014-10-01 21:54:15 -050016
Bryan O'Donoghue5c8ad592015-09-18 16:38:45 +010017#include "greybus_trace.h"
Alex Eldere88afa52014-10-01 21:54:15 -050018
Alex Elder5b3db0d2014-10-20 10:27:56 -050019static struct kmem_cache *gb_operation_cache;
Johan Hovold1e5613b2015-04-07 11:27:17 +020020static struct kmem_cache *gb_message_cache;
Alex Elder5b3db0d2014-10-20 10:27:56 -050021
Johan Hovold701615f2015-07-23 10:50:03 +020022/* Workqueue to handle Greybus operation completions. */
23static struct workqueue_struct *gb_operation_completion_wq;
24
Johan Hovoldfd7134a2015-07-14 15:43:26 +020025/* Wait queue for synchronous cancellations. */
26static DECLARE_WAIT_QUEUE_HEAD(gb_operation_cancellation_queue);
27
Alex Elderd90c25b2014-10-16 06:35:33 -050028/*
Johan Hovold008974c2015-07-14 15:43:31 +020029 * Protects updates to operation->errno.
Alex Elder82b5e3f2014-12-03 12:27:46 -060030 */
Alex Eldere88afa52014-10-01 21:54:15 -050031static DEFINE_SPINLOCK(gb_operations_lock);
32
Johan Hovoldabb722e2015-07-01 12:37:24 +020033static int gb_operation_response_send(struct gb_operation *operation,
Cristian Sicilia8478c352018-11-25 17:58:15 +010034 int errno);
Johan Hovoldabb722e2015-07-01 12:37:24 +020035
Johan Hovold008974c2015-07-14 15:43:31 +020036/*
37 * Increment operation active count and add to connection list unless the
38 * connection is going away.
39 *
40 * Caller holds operation reference.
41 */
42static int gb_operation_get_active(struct gb_operation *operation)
Johan Hovold3eeac7e2015-07-14 15:43:25 +020043{
Johan Hovold008974c2015-07-14 15:43:31 +020044 struct gb_connection *connection = operation->connection;
45 unsigned long flags;
46
47 spin_lock_irqsave(&connection->lock, flags);
Johan Hovold77bbbcf2016-05-27 17:26:34 +020048 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 Hovold3de5acf2016-05-27 17:26:36 +020055 case GB_CONNECTION_STATE_DISCONNECTING:
56 if (!gb_operation_is_core(operation))
57 goto err_unlock;
58 break;
Johan Hovold77bbbcf2016-05-27 17:26:34 +020059 default:
60 goto err_unlock;
Johan Hovold008974c2015-07-14 15:43:31 +020061 }
62
63 if (operation->active++ == 0)
64 list_add_tail(&operation->links, &connection->operations);
65
Alex Elderf866e662016-05-20 11:59:57 -050066 trace_gb_operation_get_active(operation);
67
Johan Hovold008974c2015-07-14 15:43:31 +020068 spin_unlock_irqrestore(&connection->lock, flags);
69
70 return 0;
Johan Hovold77bbbcf2016-05-27 17:26:34 +020071
72err_unlock:
73 spin_unlock_irqrestore(&connection->lock, flags);
74
75 return -ENOTCONN;
Johan Hovold3eeac7e2015-07-14 15:43:25 +020076}
77
78/* Caller holds operation reference. */
Johan Hovold008974c2015-07-14 15:43:31 +020079static void gb_operation_put_active(struct gb_operation *operation)
Johan Hovold3eeac7e2015-07-14 15:43:25 +020080{
Johan Hovold008974c2015-07-14 15:43:31 +020081 struct gb_connection *connection = operation->connection;
82 unsigned long flags;
83
84 spin_lock_irqsave(&connection->lock, flags);
Alex Elderf866e662016-05-20 11:59:57 -050085
Alex Elderdf732542016-05-23 23:05:29 -050086 trace_gb_operation_put_active(operation);
Alex Elderf866e662016-05-20 11:59:57 -050087
Johan Hovold008974c2015-07-14 15:43:31 +020088 if (--operation->active == 0) {
89 list_del(&operation->links);
Johan Hovoldfd7134a2015-07-14 15:43:26 +020090 if (atomic_read(&operation->waiters))
91 wake_up(&gb_operation_cancellation_queue);
92 }
Johan Hovold008974c2015-07-14 15:43:31 +020093 spin_unlock_irqrestore(&connection->lock, flags);
Johan Hovoldfd7134a2015-07-14 15:43:26 +020094}
95
Johan Hovold008974c2015-07-14 15:43:31 +020096static bool gb_operation_is_active(struct gb_operation *operation)
Johan Hovoldfd7134a2015-07-14 15:43:26 +020097{
Johan Hovold008974c2015-07-14 15:43:31 +020098 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 Hovold3eeac7e2015-07-14 15:43:25 +0200107}
108
Alex Elder3deb37d2014-11-25 11:33:15 -0600109/*
Alex Elder2fb2d2a2014-12-01 07:53:08 -0600110 * 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 Hovoldd5062832015-03-27 12:41:11 +0100117 * response arrives.
Alex Elder3deb37d2014-11-25 11:33:15 -0600118 *
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 Elder2fb2d2a2014-12-01 07:53:08 -0600123 * 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 Elder3deb37d2014-11-25 11:33:15 -0600133 */
Alex Elderabe9a302014-11-25 11:33:14 -0600134static bool gb_operation_result_set(struct gb_operation *operation, int result)
Alex Elderba986b52014-11-25 11:33:13 -0600135{
Johan Hovold184ab532015-03-02 12:34:40 +0100136 unsigned long flags;
Alex Elder894cbc32014-11-25 16:54:02 -0600137 int prev;
138
Alex Elder3deb37d2014-11-25 11:33:15 -0600139 if (result == -EINPROGRESS) {
Alex Elder2fb2d2a2014-12-01 07:53:08 -0600140 /*
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 Hovold184ab532015-03-02 12:34:40 +0100147 spin_lock_irqsave(&gb_operations_lock, flags);
Alex Elder894cbc32014-11-25 16:54:02 -0600148 prev = operation->errno;
149 if (prev == -EBADR)
150 operation->errno = result;
Alex Elder2fb2d2a2014-12-01 07:53:08 -0600151 else
152 operation->errno = -EILSEQ;
Johan Hovold184ab532015-03-02 12:34:40 +0100153 spin_unlock_irqrestore(&gb_operations_lock, flags);
Alex Elder2fb2d2a2014-12-01 07:53:08 -0600154 WARN_ON(prev != -EBADR);
Alex Elder3deb37d2014-11-25 11:33:15 -0600155
Alex Elder2fb2d2a2014-12-01 07:53:08 -0600156 return true;
Alex Elder894cbc32014-11-25 16:54:02 -0600157 }
158
Alex Elder2fb2d2a2014-12-01 07:53:08 -0600159 /*
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 Hovold184ab532015-03-02 12:34:40 +0100171 spin_lock_irqsave(&gb_operations_lock, flags);
Alex Elder894cbc32014-11-25 16:54:02 -0600172 prev = operation->errno;
173 if (prev == -EINPROGRESS)
Alex Elder2fb2d2a2014-12-01 07:53:08 -0600174 operation->errno = result; /* First and final result */
Johan Hovold184ab532015-03-02 12:34:40 +0100175 spin_unlock_irqrestore(&gb_operations_lock, flags);
Alex Elder894cbc32014-11-25 16:54:02 -0600176
177 return prev == -EINPROGRESS;
Alex Elderba986b52014-11-25 11:33:13 -0600178}
179
180int gb_operation_result(struct gb_operation *operation)
181{
Alex Elder3deb37d2014-11-25 11:33:15 -0600182 int result = operation->errno;
183
Alex Elder2fb2d2a2014-12-01 07:53:08 -0600184 WARN_ON(result == -EBADR);
Alex Elder3deb37d2014-11-25 11:33:15 -0600185 WARN_ON(result == -EINPROGRESS);
186
187 return result;
Alex Elderba986b52014-11-25 11:33:13 -0600188}
Johan Hovold1dad6b32015-03-27 12:41:10 +0100189EXPORT_SYMBOL_GPL(gb_operation_result);
Alex Elderba986b52014-11-25 11:33:13 -0600190
Johan Hovold0581f28e2015-07-09 15:17:59 +0200191/*
Johan Hovold048a7ff2015-07-09 15:18:00 +0200192 * Looks up an outgoing operation on a connection and returns a refcounted
193 * pointer if found, or NULL otherwise.
Johan Hovold0581f28e2015-07-09 15:17:59 +0200194 */
Alex Elder84d148b2014-10-16 06:35:32 -0500195static struct gb_operation *
Johan Hovold048a7ff2015-07-09 15:18:00 +0200196gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id)
Alex Elder84d148b2014-10-16 06:35:32 -0500197{
Alex Elderb8616da2014-11-12 15:17:53 -0600198 struct gb_operation *operation;
Johan Hovold184ab532015-03-02 12:34:40 +0100199 unsigned long flags;
Alex Elder84d148b2014-10-16 06:35:32 -0500200 bool found = false;
201
Johan Hovold008974c2015-07-14 15:43:31 +0200202 spin_lock_irqsave(&connection->lock, flags);
Alex Elderafb2e132014-12-03 08:35:07 -0600203 list_for_each_entry(operation, &connection->operations, links)
Johan Hovold048a7ff2015-07-09 15:18:00 +0200204 if (operation->id == operation_id &&
Cristian Sicilia8478c352018-11-25 17:58:15 +0100205 !gb_operation_is_incoming(operation)) {
Johan Hovold0581f28e2015-07-09 15:17:59 +0200206 gb_operation_get(operation);
Alex Elder84d148b2014-10-16 06:35:32 -0500207 found = true;
Alex Elderb8616da2014-11-12 15:17:53 -0600208 break;
209 }
Johan Hovold008974c2015-07-14 15:43:31 +0200210 spin_unlock_irqrestore(&connection->lock, flags);
Alex Elder84d148b2014-10-16 06:35:32 -0500211
212 return found ? operation : NULL;
213}
214
Johan Hovolda52c4352015-07-01 12:37:23 +0200215static int gb_message_send(struct gb_message *message, gfp_t gfp)
Alex Elder374e6a22014-11-17 18:08:37 -0600216{
Alex Elder3ed67ab2014-11-18 13:26:52 -0600217 struct gb_connection *connection = message->operation->connection;
Alex Elder374e6a22014-11-17 18:08:37 -0600218
Bryan O'Donoghue5c8ad592015-09-18 16:38:45 +0100219 trace_gb_message_send(message);
Johan Hovold3e136cc2015-07-01 12:37:21 +0200220 return connection->hd->driver->message_send(connection->hd,
Alex Elder0a9c4d72014-12-11 16:48:38 -0600221 connection->hd_cport_id,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200222 message,
Johan Hovoldb84abdc2015-07-17 18:50:26 +0200223 gfp);
Alex Elder374e6a22014-11-17 18:08:37 -0600224}
225
Alex Elder60147182014-11-19 12:27:15 -0600226/*
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200227 * Cancel a message we have passed to the host device layer to be sent.
Alex Elder60147182014-11-19 12:27:15 -0600228 */
Alex Elder35b13422014-11-18 13:26:49 -0600229static void gb_message_cancel(struct gb_message *message)
Alex Elder374e6a22014-11-17 18:08:37 -0600230{
Johan Hovold25376362015-11-03 18:03:23 +0100231 struct gb_host_device *hd = message->operation->connection->hd;
Alex Elder3ed67ab2014-11-18 13:26:52 -0600232
Johan Hovold3e136cc2015-07-01 12:37:21 +0200233 hd->driver->message_cancel(message);
Alex Elder374e6a22014-11-17 18:08:37 -0600234}
Alex Eldera9163b22014-11-18 13:26:44 -0600235
Alex Elder2eb585f2014-10-16 06:35:34 -0500236static void gb_operation_request_handle(struct gb_operation *operation)
237{
Johan Hovold25cdd7a2015-11-25 15:59:16 +0100238 struct gb_connection *connection = operation->connection;
Johan Hovold973ccfd2015-03-27 12:45:49 +0100239 int status;
Johan Hovoldff65be72015-03-27 12:41:15 +0100240 int ret;
Alex Elderc3cf2782014-11-12 15:17:55 -0600241
Johan Hovoldbfa9a5e2016-01-19 12:51:02 +0100242 if (connection->handler) {
243 status = connection->handler(operation);
Johan Hovold973ccfd2015-03-27 12:45:49 +0100244 } else {
Johan Hovold25cdd7a2015-11-25 15:59:16 +0100245 dev_err(&connection->hd->dev,
Viresh Kumar2f3db922015-12-04 21:30:09 +0530246 "%s: unexpected incoming request of type 0x%02x\n",
Johan Hovold25cdd7a2015-11-25 15:59:16 +0100247 connection->name, operation->type);
Johan Hovold973ccfd2015-03-27 12:45:49 +0100248
249 status = -EPROTONOSUPPORT;
Alex Elder2eb585f2014-10-16 06:35:34 -0500250 }
251
Johan Hovold973ccfd2015-03-27 12:45:49 +0100252 ret = gb_operation_response_send(operation, status);
Johan Hovoldff65be72015-03-27 12:41:15 +0100253 if (ret) {
Johan Hovold25cdd7a2015-11-25 15:59:16 +0100254 dev_err(&connection->hd->dev,
Viresh Kumar2f3db922015-12-04 21:30:09 +0530255 "%s: failed to send response %d for type 0x%02x: %d\n",
Johan Hovold25cdd7a2015-11-25 15:59:16 +0100256 connection->name, status, operation->type, ret);
David Linc77bac02016-05-06 18:16:24 -0700257 return;
Johan Hovoldff65be72015-03-27 12:41:15 +0100258 }
Alex Elder2eb585f2014-10-16 06:35:34 -0500259}
260
Alex Eldere88afa52014-10-01 21:54:15 -0500261/*
Johan Hovoldc600e532015-07-14 15:43:28 +0200262 * Process operation work.
263 *
264 * For incoming requests, call the protocol request handler. The operation
265 * result should be -EINPROGRESS at this point.
Alex Elderd4a1ff62014-12-02 08:30:37 -0600266 *
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 Elder2eb585f2014-10-16 06:35:34 -0500271 */
Alex Elderee637a92014-11-21 19:29:13 -0600272static void gb_operation_work(struct work_struct *work)
Alex Elder2eb585f2014-10-16 06:35:34 -0500273{
274 struct gb_operation *operation;
Johan Hovolddbec2722017-01-23 13:04:14 +0100275 int ret;
Alex Elder2eb585f2014-10-16 06:35:34 -0500276
Alex Elderee637a92014-11-21 19:29:13 -0600277 operation = container_of(work, struct gb_operation, work);
Johan Hovold37754032015-03-27 12:41:12 +0100278
Johan Hovolddbec2722017-01-23 13:04:14 +0100279 if (gb_operation_is_incoming(operation)) {
Johan Hovoldc600e532015-07-14 15:43:28 +0200280 gb_operation_request_handle(operation);
Johan Hovolddbec2722017-01-23 13:04:14 +0100281 } 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 Hovoldc600e532015-07-14 15:43:28 +0200289 operation->callback(operation);
Johan Hovolddbec2722017-01-23 13:04:14 +0100290 }
Johan Hovold37754032015-03-27 12:41:12 +0100291
Johan Hovold3eeac7e2015-07-14 15:43:25 +0200292 gb_operation_put_active(operation);
Alex Elder10c69392014-11-21 19:29:20 -0600293 gb_operation_put(operation);
Alex Elder2eb585f2014-10-16 06:35:34 -0500294}
295
Kees Cooke99e88a2017-10-16 14:43:17 -0700296static void gb_operation_timeout(struct timer_list *t)
Johan Hovolddbec2722017-01-23 13:04:14 +0100297{
Kees Cooke99e88a2017-10-16 14:43:17 -0700298 struct gb_operation *operation = from_timer(operation, t, timer);
Johan Hovolddbec2722017-01-23 13:04:14 +0100299
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 Hovold25376362015-11-03 18:03:23 +0100309static void gb_operation_message_init(struct gb_host_device *hd,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100310 struct gb_message *message,
311 u16 operation_id,
312 size_t payload_size, u8 type)
Alex Eldere88afa52014-10-01 21:54:15 -0500313{
Alex Eldere88afa52014-10-01 21:54:15 -0500314 struct gb_operation_msg_hdr *header;
Alex Eldere88afa52014-10-01 21:54:15 -0500315
Johan Hovold24ef4852015-04-07 11:27:21 +0200316 header = message->buffer;
Alex Elderbc46fab2014-11-17 18:08:35 -0600317
Alex Elderc08b1dd2014-11-20 16:09:15 -0600318 message->header = header;
Alex Elder746e0ef2014-12-03 12:27:45 -0600319 message->payload = payload_size ? header + 1 : NULL;
Alex Elder7cfa6992014-12-03 12:27:44 -0600320 message->payload_size = payload_size;
Alex Elderc7f82d52014-11-17 18:08:32 -0600321
Alex Elderea64cd92014-12-02 08:30:32 -0600322 /*
323 * The type supplied for incoming message buffers will be
Johan Hovold7adb32b2016-04-29 17:08:37 +0200324 * GB_REQUEST_TYPE_INVALID. Such buffers will be overwritten by
325 * arriving data so there's no need to initialize the message header.
Alex Elderea64cd92014-12-02 08:30:32 -0600326 */
Johan Hovold7adb32b2016-04-29 17:08:37 +0200327 if (type != GB_REQUEST_TYPE_INVALID) {
Alex Elder7cfa6992014-12-03 12:27:44 -0600328 u16 message_size = (u16)(sizeof(*header) + payload_size);
329
Alex Elderea64cd92014-12-02 08:30:32 -0600330 /*
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 Elderdc779222014-12-02 08:30:33 -0600344}
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 Hovold1e5613b2015-04-07 11:27:17 +0200356 * Our message buffers have the following layout:
Alex Elderdc779222014-12-02 08:30:33 -0600357 * message header \_ these combined are
358 * message payload / the message size
359 */
360static struct gb_message *
Johan Hovold25376362015-11-03 18:03:23 +0100361gb_operation_message_alloc(struct gb_host_device *hd, u8 type,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100362 size_t payload_size, gfp_t gfp_flags)
Alex Elderdc779222014-12-02 08:30:33 -0600363{
364 struct gb_message *message;
365 struct gb_operation_msg_hdr *header;
366 size_t message_size = payload_size + sizeof(*header);
Alex Elderdc779222014-12-02 08:30:33 -0600367
Johan Hovold1e5613b2015-04-07 11:27:17 +0200368 if (message_size > hd->buffer_size_max) {
Johan Hovoldb4275722016-02-11 13:52:47 +0100369 dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100370 message_size, hd->buffer_size_max);
Johan Hovold1e5613b2015-04-07 11:27:17 +0200371 return NULL;
Alex Elder0cffcac32014-12-02 08:30:35 -0600372 }
Johan Hovold1e5613b2015-04-07 11:27:17 +0200373
374 /* Allocate the message structure and buffer. */
375 message = kmem_cache_zalloc(gb_message_cache, gfp_flags);
Alex Elderdc779222014-12-02 08:30:33 -0600376 if (!message)
377 return NULL;
378
Johan Hovold24ef4852015-04-07 11:27:21 +0200379 message->buffer = kzalloc(message_size, gfp_flags);
Johan Hovold1e5613b2015-04-07 11:27:17 +0200380 if (!message->buffer)
381 goto err_free_message;
382
Alex Elderdc779222014-12-02 08:30:33 -0600383 /* Initialize the message. Operation id is filled in later. */
Alex Elder7cfa6992014-12-03 12:27:44 -0600384 gb_operation_message_init(hd, message, 0, payload_size, type);
Alex Elderea64cd92014-12-02 08:30:32 -0600385
Alex Elderc08b1dd2014-11-20 16:09:15 -0600386 return message;
Johan Hovold1e5613b2015-04-07 11:27:17 +0200387
388err_free_message:
389 kmem_cache_free(gb_message_cache, message);
390
391 return NULL;
Alex Elderc7f82d52014-11-17 18:08:32 -0600392}
393
Alex Elderc08b1dd2014-11-20 16:09:15 -0600394static void gb_operation_message_free(struct gb_message *message)
Alex Elderc7f82d52014-11-17 18:08:32 -0600395{
Johan Hovold1e5613b2015-04-07 11:27:17 +0200396 kfree(message->buffer);
397 kmem_cache_free(gb_message_cache, message);
Alex Elder22b320f2014-10-16 06:35:31 -0500398}
399
400/*
Viresh Kumar696e0cc2014-11-21 11:26:30 +0530401 * 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 Elderbc717fc2014-11-19 17:55:04 -0600403 */
Alex Elder0c90fff2014-12-02 08:30:38 -0600404static int gb_operation_status_map(u8 status)
Alex Elderbc717fc2014-11-19 17:55:04 -0600405{
406 switch (status) {
407 case GB_OP_SUCCESS:
408 return 0;
Alex Elderbc717fc2014-11-19 17:55:04 -0600409 case GB_OP_INTERRUPTED:
410 return -EINTR;
Alex Elder57248fa2014-12-01 07:53:09 -0600411 case GB_OP_TIMEOUT:
412 return -ETIMEDOUT;
413 case GB_OP_NO_MEMORY:
414 return -ENOMEM;
Alex Elderbc717fc2014-11-19 17:55:04 -0600415 case GB_OP_PROTOCOL_BAD:
416 return -EPROTONOSUPPORT;
417 case GB_OP_OVERFLOW:
Alex Elder1a365152014-11-25 13:06:44 -0600418 return -EMSGSIZE;
Alex Elder57248fa2014-12-01 07:53:09 -0600419 case GB_OP_INVALID:
420 return -EINVAL;
421 case GB_OP_RETRY:
422 return -EAGAIN;
Alex Elderaa263512014-12-10 08:43:33 -0600423 case GB_OP_NONEXISTENT:
424 return -ENODEV;
Alex Elder57248fa2014-12-01 07:53:09 -0600425 case GB_OP_MALFUNCTION:
426 return -EILSEQ;
427 case GB_OP_UNKNOWN_ERROR:
Alex Elderbc717fc2014-11-19 17:55:04 -0600428 default:
429 return -EIO;
430 }
431}
432
433/*
Alex Elder0c90fff2014-12-02 08:30:38 -0600434 * 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 */
439static 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 Elderaa263512014-12-10 08:43:33 -0600460 case -ENODEV:
461 return GB_OP_NONEXISTENT;
Alex Elder0c90fff2014-12-02 08:30:38 -0600462 case -EIO:
463 default:
464 return GB_OP_UNKNOWN_ERROR;
465 }
466}
467
Alex Elder82e26f72014-12-02 08:30:39 -0600468bool gb_operation_response_alloc(struct gb_operation *operation,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100469 size_t response_size, gfp_t gfp)
Alex Elder82e26f72014-12-02 08:30:39 -0600470{
Johan Hovold25376362015-11-03 18:03:23 +0100471 struct gb_host_device *hd = operation->connection->hd;
Alex Elder82e26f72014-12-02 08:30:39 -0600472 struct gb_operation_msg_hdr *request_header;
473 struct gb_message *response;
474 u8 type;
475
Alex Elder6d653372015-05-07 13:03:52 -0500476 type = operation->type | GB_MESSAGE_TYPE_RESPONSE;
Johan Hovold1c7658c2015-07-17 18:50:25 +0200477 response = gb_operation_message_alloc(hd, type, response_size, gfp);
Alex Elder82e26f72014-12-02 08:30:39 -0600478 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 Elder82b5e3f2014-12-03 12:27:46 -0600488 request_header = operation->request->header;
Alex Elder82e26f72014-12-02 08:30:39 -0600489 response->header->operation_id = request_header->operation_id;
490 operation->response = response;
491
492 return true;
493}
Johan Hovold1dad6b32015-03-27 12:41:10 +0100494EXPORT_SYMBOL_GPL(gb_operation_response_alloc);
Alex Elder82e26f72014-12-02 08:30:39 -0600495
Alex Elder0c90fff2014-12-02 08:30:38 -0600496/*
Alex Elder22b320f2014-10-16 06:35:31 -0500497 * Create a Greybus operation to be sent over the given connection.
Viresh Kumar696e0cc2014-11-21 11:26:30 +0530498 * The request buffer will be big enough for a payload of the given
Alex Elderea64cd92014-12-02 08:30:32 -0600499 * size.
Alex Elder22b320f2014-10-16 06:35:31 -0500500 *
Alex Elderea64cd92014-12-02 08:30:32 -0600501 * 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 Elder22b320f2014-10-16 06:35:31 -0500514 *
515 * Returns a pointer to the new operation or a null pointer if an
516 * error occurs.
517 */
Alex Elder30a29642014-11-19 17:55:02 -0600518static struct gb_operation *
Alex Elderea64cd92014-12-02 08:30:32 -0600519gb_operation_create_common(struct gb_connection *connection, u8 type,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100520 size_t request_size, size_t response_size,
521 unsigned long op_flags, gfp_t gfp_flags)
Alex Elder22b320f2014-10-16 06:35:31 -0500522{
Johan Hovold25376362015-11-03 18:03:23 +0100523 struct gb_host_device *hd = connection->hd;
Alex Elder22b320f2014-10-16 06:35:31 -0500524 struct gb_operation *operation;
Alex Elder22b320f2014-10-16 06:35:31 -0500525
Alex Elder5b3db0d2014-10-20 10:27:56 -0500526 operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
Alex Elder22b320f2014-10-16 06:35:31 -0500527 if (!operation)
528 return NULL;
Greg Kroah-Hartman6507cce2014-10-27 17:58:54 +0800529 operation->connection = connection;
Alex Elder22b320f2014-10-16 06:35:31 -0500530
Alex Elderc08b1dd2014-11-20 16:09:15 -0600531 operation->request = gb_operation_message_alloc(hd, type, request_size,
532 gfp_flags);
533 if (!operation->request)
Alex Elder5b3db0d2014-10-20 10:27:56 -0500534 goto err_cache;
Alex Elderc08b1dd2014-11-20 16:09:15 -0600535 operation->request->operation = operation;
Alex Elder22b320f2014-10-16 06:35:31 -0500536
Alex Elderea64cd92014-12-02 08:30:32 -0600537 /* Allocate the response buffer for outgoing operations */
Johan Hovold710067e2015-07-01 12:37:26 +0200538 if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) {
Johan Hovold1c7658c2015-07-17 18:50:25 +0200539 if (!gb_operation_response_alloc(operation, response_size,
540 gfp_flags)) {
Alex Elder5b3db0d2014-10-20 10:27:56 -0500541 goto err_request;
Johan Hovold1c7658c2015-07-17 18:50:25 +0200542 }
Johan Hovolddbec2722017-01-23 13:04:14 +0100543
Kees Cooke99e88a2017-10-16 14:43:17 -0700544 timer_setup(&operation->timer, gb_operation_timeout, 0);
Alex Elder82b5e3f2014-12-03 12:27:46 -0600545 }
Johan Hovold710067e2015-07-01 12:37:26 +0200546
547 operation->flags = op_flags;
548 operation->type = type;
Alex Elder3deb37d2014-11-25 11:33:15 -0600549 operation->errno = -EBADR; /* Initial value--means "never set" */
Alex Eldere88afa52014-10-01 21:54:15 -0500550
Alex Elderee637a92014-11-21 19:29:13 -0600551 INIT_WORK(&operation->work, gb_operation_work);
Alex Eldere88afa52014-10-01 21:54:15 -0500552 init_completion(&operation->completion);
Alex Elderc7d0f252014-11-17 08:08:40 -0600553 kref_init(&operation->kref);
Johan Hovoldfd7134a2015-07-14 15:43:26 +0200554 atomic_set(&operation->waiters, 0);
Alex Eldere88afa52014-10-01 21:54:15 -0500555
Alex Eldere88afa52014-10-01 21:54:15 -0500556 return operation;
Alex Elder5b3db0d2014-10-20 10:27:56 -0500557
558err_request:
Alex Elderc08b1dd2014-11-20 16:09:15 -0600559 gb_operation_message_free(operation->request);
Alex Elder5b3db0d2014-10-20 10:27:56 -0500560err_cache:
561 kmem_cache_free(gb_operation_cache, operation);
562
563 return NULL;
Alex Eldere88afa52014-10-01 21:54:15 -0500564}
565
Alex Elder55f66a82014-12-02 08:30:31 -0600566/*
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 Hovold7e43e332016-02-25 14:40:24 +0100574struct gb_operation *
575gb_operation_create_flags(struct gb_connection *connection,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100576 u8 type, size_t request_size,
577 size_t response_size, unsigned long flags,
578 gfp_t gfp)
Alex Elder30a29642014-11-19 17:55:02 -0600579{
Alex Elderf866e662016-05-20 11:59:57 -0500580 struct gb_operation *operation;
581
Johan Hovold7adb32b2016-04-29 17:08:37 +0200582 if (WARN_ON_ONCE(type == GB_REQUEST_TYPE_INVALID))
Alex Elder55f66a82014-12-02 08:30:31 -0600583 return NULL;
Alex Elder6d653372015-05-07 13:03:52 -0500584 if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE))
585 type &= ~GB_MESSAGE_TYPE_RESPONSE;
Alex Elder55f66a82014-12-02 08:30:31 -0600586
Johan Hovold7e43e332016-02-25 14:40:24 +0100587 if (WARN_ON_ONCE(flags & ~GB_OPERATION_FLAG_USER_MASK))
588 flags &= GB_OPERATION_FLAG_USER_MASK;
589
Alex Elderf866e662016-05-20 11:59:57 -0500590 operation = gb_operation_create_common(connection, type,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100591 request_size, response_size,
592 flags, gfp);
Alex Elderf866e662016-05-20 11:59:57 -0500593 if (operation)
594 trace_gb_operation_create(operation);
595
596 return operation;
Alex Elder30a29642014-11-19 17:55:02 -0600597}
Johan Hovold7e43e332016-02-25 14:40:24 +0100598EXPORT_SYMBOL_GPL(gb_operation_create_flags);
Alex Elder30a29642014-11-19 17:55:02 -0600599
Johan Hovold18079ec2016-05-27 17:26:35 +0200600struct gb_operation *
601gb_operation_create_core(struct gb_connection *connection,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100602 u8 type, size_t request_size,
603 size_t response_size, unsigned long flags,
604 gfp_t gfp)
Johan Hovold18079ec2016-05-27 17:26:35 +0200605{
606 struct gb_operation *operation;
607
608 flags |= GB_OPERATION_FLAG_CORE;
609
610 operation = gb_operation_create_common(connection, type,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100611 request_size, response_size,
612 flags, gfp);
Johan Hovold18079ec2016-05-27 17:26:35 +0200613 if (operation)
614 trace_gb_operation_create_core(operation);
615
616 return operation;
617}
Cristian Sicilia8478c352018-11-25 17:58:15 +0100618
Johan Hovold18079ec2016-05-27 17:26:35 +0200619/* Do not export this function. */
620
Johan Hovoldd52b35f2015-05-19 11:22:46 +0200621size_t gb_operation_get_payload_size_max(struct gb_connection *connection)
622{
Johan Hovold25376362015-11-03 18:03:23 +0100623 struct gb_host_device *hd = connection->hd;
Johan Hovoldd52b35f2015-05-19 11:22:46 +0200624
625 return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr);
626}
627EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max);
628
Alex Elder30a29642014-11-19 17:55:02 -0600629static struct gb_operation *
Alex Elderea64cd92014-12-02 08:30:32 -0600630gb_operation_create_incoming(struct gb_connection *connection, u16 id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100631 u8 type, void *data, size_t size)
Alex Elder30a29642014-11-19 17:55:02 -0600632{
Alex Elder34db1f92014-12-02 08:30:28 -0600633 struct gb_operation *operation;
Johan Hovoldcfa79692015-03-27 12:41:18 +0100634 size_t request_size;
Johan Hovold710067e2015-07-01 12:37:26 +0200635 unsigned long flags = GB_OPERATION_FLAG_INCOMING;
Johan Hovoldcfa79692015-03-27 12:41:18 +0100636
637 /* Caller has made sure we at least have a message header. */
638 request_size = size - sizeof(struct gb_operation_msg_hdr);
Alex Elder34db1f92014-12-02 08:30:28 -0600639
Johan Hovolde3398812015-07-01 12:37:27 +0200640 if (!id)
641 flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL;
642
Johan Hovold710067e2015-07-01 12:37:26 +0200643 operation = gb_operation_create_common(connection, type,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100644 request_size,
645 GB_REQUEST_TYPE_INVALID,
646 flags, GFP_ATOMIC);
Johan Hovold9a586bd2015-07-14 15:43:24 +0200647 if (!operation)
648 return NULL;
649
650 operation->id = id;
651 memcpy(operation->request->header, data, size);
Alex Elderf866e662016-05-20 11:59:57 -0500652 trace_gb_operation_create_incoming(operation);
Alex Elder34db1f92014-12-02 08:30:28 -0600653
654 return operation;
Alex Elder30a29642014-11-19 17:55:02 -0600655}
656
Alex Eldere88afa52014-10-01 21:54:15 -0500657/*
Alex Elderdeb4b9e2014-11-21 19:29:15 -0600658 * Get an additional reference on an operation.
659 */
660void gb_operation_get(struct gb_operation *operation)
661{
662 kref_get(&operation->kref);
663}
Johan Hovold1dad6b32015-03-27 12:41:10 +0100664EXPORT_SYMBOL_GPL(gb_operation_get);
Alex Elderdeb4b9e2014-11-21 19:29:15 -0600665
666/*
Alex Eldere88afa52014-10-01 21:54:15 -0500667 * Destroy a previously created operation.
668 */
Alex Elderc7d0f252014-11-17 08:08:40 -0600669static void _gb_operation_destroy(struct kref *kref)
Alex Eldere88afa52014-10-01 21:54:15 -0500670{
Alex Elderc7d0f252014-11-17 08:08:40 -0600671 struct gb_operation *operation;
672
673 operation = container_of(kref, struct gb_operation, kref);
Alex Eldere88afa52014-10-01 21:54:15 -0500674
Alex Elderf866e662016-05-20 11:59:57 -0500675 trace_gb_operation_destroy(operation);
676
Johan Hovold948966762015-03-27 12:41:17 +0100677 if (operation->response)
678 gb_operation_message_free(operation->response);
Alex Elderc08b1dd2014-11-20 16:09:15 -0600679 gb_operation_message_free(operation->request);
Alex Eldere88afa52014-10-01 21:54:15 -0500680
Alex Elder5b3db0d2014-10-20 10:27:56 -0500681 kmem_cache_free(gb_operation_cache, operation);
Alex Eldere88afa52014-10-01 21:54:15 -0500682}
Alex Elderd90c25b2014-10-16 06:35:33 -0500683
Alex Elderdeb4b9e2014-11-21 19:29:15 -0600684/*
685 * Drop a reference on an operation, and destroy it when the last
686 * one is gone.
687 */
Alex Elderc7d0f252014-11-17 08:08:40 -0600688void gb_operation_put(struct gb_operation *operation)
689{
Johan Hovold85109f72015-07-09 15:17:58 +0200690 if (WARN_ON(!operation))
691 return;
692
Johan Hovold008974c2015-07-14 15:43:31 +0200693 kref_put(&operation->kref, _gb_operation_destroy);
Alex Elderc7d0f252014-11-17 08:08:40 -0600694}
Greg Kroah-Hartmandf469a92014-12-23 15:16:52 -0800695EXPORT_SYMBOL_GPL(gb_operation_put);
Alex Elderc7d0f252014-11-17 08:08:40 -0600696
Alex Elder10c69392014-11-21 19:29:20 -0600697/* Tell the requester we're done */
698static void gb_operation_sync_callback(struct gb_operation *operation)
699{
700 complete(&operation->completion);
701}
702
Johan Hovold613c15e2016-04-29 17:08:31 +0200703/**
704 * gb_operation_request_send() - send an operation request message
705 * @operation: the operation to initiate
706 * @callback: the operation completion callback
Johan Hovolddbec2722017-01-23 13:04:14 +0100707 * @timeout: operation timeout in milliseconds, or zero for no timeout
Johan Hovold613c15e2016-04-29 17:08:31 +0200708 * @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 Hovold3e2ee2c2016-04-29 17:08:32 +0200712 * 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 Hovold613c15e2016-04-29 17:08:31 +0200716 *
717 * Return: 0 if the request was successfully queued in the host-driver queues,
718 * or a negative errno.
Alex Elderd90c25b2014-10-16 06:35:33 -0500719 */
720int gb_operation_request_send(struct gb_operation *operation,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100721 gb_operation_callback callback,
722 unsigned int timeout,
723 gfp_t gfp)
Alex Elderd90c25b2014-10-16 06:35:33 -0500724{
Alex Elderafb2e132014-12-03 08:35:07 -0600725 struct gb_connection *connection = operation->connection;
726 struct gb_operation_msg_hdr *header;
Alex Elder4afb7fd2014-12-03 08:35:08 -0600727 unsigned int cycle;
Johan Hovoldea2c2ee2015-03-27 12:41:14 +0100728 int ret;
Alex Elderd90c25b2014-10-16 06:35:33 -0500729
Johan Hovoldca1f8f82016-05-11 10:17:56 +0200730 if (gb_connection_is_offloaded(connection))
731 return -EBUSY;
732
Johan Hovold37754032015-03-27 12:41:12 +0100733 if (!callback)
734 return -EINVAL;
Johan Hovold3e2ee2c2016-04-29 17:08:32 +0200735
Alex Elderc25572c2014-12-03 08:35:09 -0600736 /*
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 Elderafb2e132014-12-03 08:35:07 -0600742
743 /*
744 * Assign the operation's id, and store it in the request header.
Johan Hovold3e2ee2c2016-04-29 17:08:32 +0200745 * Zero is a reserved operation id for unidirectional operations.
Alex Elderafb2e132014-12-03 08:35:07 -0600746 */
Johan Hovold3e2ee2c2016-04-29 17:08:32 +0200747 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 Elderafb2e132014-12-03 08:35:07 -0600754 header = operation->request->header;
755 header->operation_id = cpu_to_le16(operation->id);
Alex Eldere8b48d12014-11-20 15:37:06 -0600756
Alex Elder3deb37d2014-11-25 11:33:15 -0600757 gb_operation_result_set(operation, -EINPROGRESS);
Alex Elderc25572c2014-12-03 08:35:09 -0600758
Johan Hovold3325a4a2015-07-14 15:43:33 +0200759 /*
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 Hovolda52c4352015-07-01 12:37:23 +0200768 ret = gb_message_send(operation->request, gfp);
Johan Hovold008974c2015-07-14 15:43:31 +0200769 if (ret)
770 goto err_put_active;
771
Johan Hovolddbec2722017-01-23 13:04:14 +0100772 if (timeout) {
773 operation->timer.expires = jiffies + msecs_to_jiffies(timeout);
774 add_timer(&operation->timer);
775 }
776
Johan Hovold008974c2015-07-14 15:43:31 +0200777 return 0;
778
779err_put_active:
780 gb_operation_put_active(operation);
781err_put:
782 gb_operation_put(operation);
Johan Hovoldea2c2ee2015-03-27 12:41:14 +0100783
784 return ret;
Alex Elderc25572c2014-12-03 08:35:09 -0600785}
Johan Hovold1dad6b32015-03-27 12:41:10 +0100786EXPORT_SYMBOL_GPL(gb_operation_request_send);
Alex Elderc25572c2014-12-03 08:35:09 -0600787
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 Hovold4f2c08a2015-07-14 15:43:36 +0200794int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100795 unsigned int timeout)
Alex Elderc25572c2014-12-03 08:35:09 -0600796{
797 int ret;
798
Johan Hovolda52c4352015-07-01 12:37:23 +0200799 ret = gb_operation_request_send(operation, gb_operation_sync_callback,
Johan Hovolddbec2722017-01-23 13:04:14 +0100800 timeout, GFP_KERNEL);
Alex Elderc25572c2014-12-03 08:35:09 -0600801 if (ret)
Alex Elderd90c25b2014-10-16 06:35:33 -0500802 return ret;
Alex Elder8350e7a2014-11-12 15:17:52 -0600803
Johan Hovolddbec2722017-01-23 13:04:14 +0100804 ret = wait_for_completion_interruptible(&operation->completion);
Perry Hung7bad4e82015-01-14 16:19:26 -0500805 if (ret < 0) {
806 /* Cancel the operation if interrupted */
Alex Elder1a365152014-11-25 13:06:44 -0600807 gb_operation_cancel(operation, -ECANCELED);
Perry Hung7bad4e82015-01-14 16:19:26 -0500808 }
Alex Elder2cf72a22014-11-21 19:29:19 -0600809
Alex Elderba986b52014-11-25 11:33:13 -0600810 return gb_operation_result(operation);
Alex Elderd90c25b2014-10-16 06:35:33 -0500811}
Johan Hovold4f2c08a2015-07-14 15:43:36 +0200812EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout);
Alex Elderd90c25b2014-10-16 06:35:33 -0500813
814/*
Alex Elder82e26f72014-12-02 08:30:39 -0600815 * 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 Elderd90c25b2014-10-16 06:35:33 -0500822 */
Johan Hovoldabb722e2015-07-01 12:37:24 +0200823static int gb_operation_response_send(struct gb_operation *operation,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100824 int errno)
Alex Elderd90c25b2014-10-16 06:35:33 -0500825{
Johan Hovolde1baa3f2015-03-27 12:41:19 +0100826 struct gb_connection *connection = operation->connection;
Johan Hovold0fb5acc2015-03-27 12:41:13 +0100827 int ret;
828
Johan Hovoldfde73822015-07-01 12:37:30 +0200829 if (!operation->response &&
Cristian Sicilia8478c352018-11-25 17:58:15 +0100830 !gb_operation_is_unidirectional(operation)) {
Johan Hovold1c7658c2015-07-17 18:50:25 +0200831 if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL))
Johan Hovoldfde73822015-07-01 12:37:30 +0200832 return -ENOMEM;
833 }
834
Alex Elderd2d2c0f2014-12-02 08:30:36 -0600835 /* Record the result */
836 if (!gb_operation_result_set(operation, errno)) {
Johan Hovold25cdd7a2015-11-25 15:59:16 +0100837 dev_err(&connection->hd->dev, "request result already set\n");
Alex Elderd2d2c0f2014-12-02 08:30:36 -0600838 return -EIO; /* Shouldn't happen */
839 }
Alex Elderd90c25b2014-10-16 06:35:33 -0500840
Johan Hovold1d771fe2015-05-26 15:29:18 +0200841 /* Sender of request does not care about response. */
Johan Hovolde3398812015-07-01 12:37:27 +0200842 if (gb_operation_is_unidirectional(operation))
Johan Hovold1d771fe2015-05-26 15:29:18 +0200843 return 0;
844
Johan Hovold0fb5acc2015-03-27 12:41:13 +0100845 /* Reference will be dropped when message has been sent. */
846 gb_operation_get(operation);
Johan Hovold008974c2015-07-14 15:43:31 +0200847 ret = gb_operation_get_active(operation);
848 if (ret)
849 goto err_put;
Johan Hovold0fb5acc2015-03-27 12:41:13 +0100850
Alex Elder82e26f72014-12-02 08:30:39 -0600851 /* Fill in the response header and send it */
852 operation->response->header->result = gb_operation_errno_map(errno);
853
Johan Hovolda52c4352015-07-01 12:37:23 +0200854 ret = gb_message_send(operation->response, GFP_KERNEL);
Johan Hovold008974c2015-07-14 15:43:31 +0200855 if (ret)
856 goto err_put_active;
857
858 return 0;
859
860err_put_active:
861 gb_operation_put_active(operation);
862err_put:
863 gb_operation_put(operation);
Johan Hovold0fb5acc2015-03-27 12:41:13 +0100864
865 return ret;
Alex Elderd90c25b2014-10-16 06:35:33 -0500866}
867
Alex Elder2eb585f2014-10-16 06:35:34 -0500868/*
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200869 * This function is called when a message send request has completed.
Alex Elderd98b52b2014-11-20 16:09:17 -0600870 */
Johan Hovold25376362015-11-03 18:03:23 +0100871void greybus_message_sent(struct gb_host_device *hd,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100872 struct gb_message *message, int status)
Alex Elderd98b52b2014-11-20 16:09:17 -0600873{
Johan Hovolda4e08462015-07-22 17:49:18 +0200874 struct gb_operation *operation = message->operation;
875 struct gb_connection *connection = operation->connection;
Alex Elderd98b52b2014-11-20 16:09:17 -0600876
Alex Elderd4a1ff62014-12-02 08:30:37 -0600877 /*
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 Hovold3e2ee2c2016-04-29 17:08:32 +0200882 * 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 Elderd4a1ff62014-12-02 08:30:37 -0600887 */
Alex Elderd4a1ff62014-12-02 08:30:37 -0600888 if (message == operation->response) {
Johan Hovolde1baa3f2015-03-27 12:41:19 +0100889 if (status) {
Johan Hovold25cdd7a2015-11-25 15:59:16 +0100890 dev_err(&connection->hd->dev,
Viresh Kumar2f3db922015-12-04 21:30:09 +0530891 "%s: error sending response 0x%02x: %d\n",
Johan Hovold25cdd7a2015-11-25 15:59:16 +0100892 connection->name, operation->type, status);
Johan Hovolde1baa3f2015-03-27 12:41:19 +0100893 }
Johan Hovold3e2ee2c2016-04-29 17:08:32 +0200894
Johan Hovold3eeac7e2015-07-14 15:43:25 +0200895 gb_operation_put_active(operation);
Alex Elderd4a1ff62014-12-02 08:30:37 -0600896 gb_operation_put(operation);
Johan Hovold3e2ee2c2016-04-29 17:08:32 +0200897 } else if (status || gb_operation_is_unidirectional(operation)) {
Johan Hovold701615f2015-07-23 10:50:03 +0200898 if (gb_operation_result_set(operation, status)) {
899 queue_work(gb_operation_completion_wq,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100900 &operation->work);
Johan Hovold701615f2015-07-23 10:50:03 +0200901 }
Alex Elderd4a1ff62014-12-02 08:30:37 -0600902 }
Alex Elderd98b52b2014-11-20 16:09:17 -0600903}
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200904EXPORT_SYMBOL_GPL(greybus_message_sent);
Alex Elderd98b52b2014-11-20 16:09:17 -0600905
906/*
Alex Elderd37b1db2014-11-19 12:27:17 -0600907 * We've received data on a connection, and it doesn't look like a
908 * response, so we assume it's a request.
Alex Elder78496db2014-11-17 08:08:39 -0600909 *
910 * This is called in interrupt context, so just copy the incoming
Alex Elderd37b1db2014-11-19 12:27:17 -0600911 * data into the request buffer and handle the rest via workqueue.
912 */
Greg Kroah-Hartman85a04422014-12-01 20:42:20 -0800913static void gb_connection_recv_request(struct gb_connection *connection,
Johan Hovold2321f042016-07-26 17:11:30 +0200914 const struct gb_operation_msg_hdr *header,
915 void *data, size_t size)
Alex Elderd37b1db2014-11-19 12:27:17 -0600916{
917 struct gb_operation *operation;
Johan Hovold2321f042016-07-26 17:11:30 +0200918 u16 operation_id;
919 u8 type;
Johan Hovold008974c2015-07-14 15:43:31 +0200920 int ret;
Alex Elderd37b1db2014-11-19 12:27:17 -0600921
Johan Hovold2321f042016-07-26 17:11:30 +0200922 operation_id = le16_to_cpu(header->operation_id);
923 type = header->type;
924
Alex Elder34db1f92014-12-02 08:30:28 -0600925 operation = gb_operation_create_incoming(connection, operation_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100926 type, data, size);
Alex Elderd37b1db2014-11-19 12:27:17 -0600927 if (!operation) {
Johan Hovold25cdd7a2015-11-25 15:59:16 +0100928 dev_err(&connection->hd->dev,
929 "%s: can't create incoming operation\n",
930 connection->name);
Johan Hovoldff65e202015-10-13 19:10:22 +0200931 return;
Alex Elderd37b1db2014-11-19 12:27:17 -0600932 }
Alex Elderd37b1db2014-11-19 12:27:17 -0600933
Johan Hovold008974c2015-07-14 15:43:31 +0200934 ret = gb_operation_get_active(operation);
935 if (ret) {
936 gb_operation_put(operation);
937 return;
938 }
Bryan O'Donoghue5c8ad592015-09-18 16:38:45 +0100939 trace_gb_message_recv_request(operation->request);
Johan Hovold3eeac7e2015-07-14 15:43:25 +0200940
Alex Elderd4a1ff62014-12-02 08:30:37 -0600941 /*
Johan Hovoldc600e532015-07-14 15:43:28 +0200942 * The initial reference to the operation will be dropped when the
943 * request handler returns.
Alex Elderd4a1ff62014-12-02 08:30:37 -0600944 */
Alex Elderd4a1ff62014-12-02 08:30:37 -0600945 if (gb_operation_result_set(operation, -EINPROGRESS))
Johan Hovold5a5bc352015-07-23 10:50:02 +0200946 queue_work(connection->wq, &operation->work);
Alex Elderd37b1db2014-11-19 12:27:17 -0600947}
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 Kumar696e0cc2014-11-21 11:26:30 +0530952 * its response.
Alex Elder78496db2014-11-17 08:08:39 -0600953 *
Alex Elderd37b1db2014-11-19 12:27:17 -0600954 * This is called in interrupt context, so just copy the incoming
955 * data into the response buffer and handle the rest via workqueue.
956 */
957static void gb_connection_recv_response(struct gb_connection *connection,
Johan Hovolddfcba862016-07-26 17:11:28 +0200958 const struct gb_operation_msg_hdr *header,
959 void *data, size_t size)
Alex Elderd37b1db2014-11-19 12:27:17 -0600960{
961 struct gb_operation *operation;
962 struct gb_message *message;
Alex Elder7cfa6992014-12-03 12:27:44 -0600963 size_t message_size;
Johan Hovolddfcba862016-07-26 17:11:28 +0200964 u16 operation_id;
965 int errno;
966
967 operation_id = le16_to_cpu(header->operation_id);
Alex Elderd37b1db2014-11-19 12:27:17 -0600968
Johan Hovold3e2ee2c2016-04-29 17:08:32 +0200969 if (!operation_id) {
Eli Senneshb0e97bc2016-05-13 13:27:40 -0400970 dev_err_ratelimited(&connection->hd->dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100971 "%s: invalid response id 0 received\n",
972 connection->name);
Johan Hovold3e2ee2c2016-04-29 17:08:32 +0200973 return;
974 }
975
Johan Hovold048a7ff2015-07-09 15:18:00 +0200976 operation = gb_operation_find_outgoing(connection, operation_id);
Alex Elderd37b1db2014-11-19 12:27:17 -0600977 if (!operation) {
Eli Senneshb0e97bc2016-05-13 13:27:40 -0400978 dev_err_ratelimited(&connection->hd->dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100979 "%s: unexpected response id 0x%04x received\n",
980 connection->name, operation_id);
Alex Elderd37b1db2014-11-19 12:27:17 -0600981 return;
982 }
983
Johan Hovolddfcba862016-07-26 17:11:28 +0200984 errno = gb_operation_status_map(header->result);
Alex Elderc08b1dd2014-11-20 16:09:15 -0600985 message = operation->response;
Johan Hovold34804ef2016-02-25 14:40:23 +0100986 message_size = sizeof(*header) + message->payload_size;
Johan Hovold7e43e332016-02-25 14:40:24 +0100987 if (!errno && size > message_size) {
Eli Senneshb0e97bc2016-05-13 13:27:40 -0400988 dev_err_ratelimited(&connection->hd->dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100989 "%s: malformed response 0x%02x received (%zu > %zu)\n",
990 connection->name, header->type,
991 size, message_size);
Alex Elder64ce39a2014-12-02 08:30:30 -0600992 errno = -EMSGSIZE;
Johan Hovold7e43e332016-02-25 14:40:24 +0100993 } else if (!errno && size < message_size) {
994 if (gb_operation_short_response_allowed(operation)) {
995 message->payload_size = size - sizeof(*header);
996 } else {
Eli Senneshb0e97bc2016-05-13 13:27:40 -0400997 dev_err_ratelimited(&connection->hd->dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100998 "%s: short response 0x%02x received (%zu < %zu)\n",
999 connection->name, header->type,
1000 size, message_size);
Johan Hovold7e43e332016-02-25 14:40:24 +01001001 errno = -EMSGSIZE;
1002 }
Alex Elderd37b1db2014-11-19 12:27:17 -06001003 }
Alex Elderd37b1db2014-11-19 12:27:17 -06001004
Alex Elder25d0f812014-11-19 17:55:05 -06001005 /* We must ignore the payload if a bad status is returned */
Alex Elder64ce39a2014-12-02 08:30:30 -06001006 if (errno)
Johan Hovold34804ef2016-02-25 14:40:23 +01001007 size = sizeof(*header);
Alex Elderd37b1db2014-11-19 12:27:17 -06001008
1009 /* The rest will be handled in work queue context */
Johan Hovolde4340b12015-07-09 15:18:01 +02001010 if (gb_operation_result_set(operation, errno)) {
Johan Hovolddfcba862016-07-26 17:11:28 +02001011 memcpy(message->buffer, data, size);
Johan Hovold112f5632016-07-26 17:11:29 +02001012
1013 trace_gb_message_recv_response(message);
1014
Johan Hovold701615f2015-07-23 10:50:03 +02001015 queue_work(gb_operation_completion_wq, &operation->work);
Johan Hovolde4340b12015-07-09 15:18:01 +02001016 }
Johan Hovold0581f28e2015-07-09 15:17:59 +02001017
1018 gb_operation_put(operation);
Alex Elderd37b1db2014-11-19 12:27:17 -06001019}
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 Elder2eb585f2014-10-16 06:35:34 -05001025 */
Alex Elder61089e82014-11-18 13:26:50 -06001026void gb_connection_recv(struct gb_connection *connection,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001027 void *data, size_t size)
Alex Elderd90c25b2014-10-16 06:35:33 -05001028{
Johan Hovold564c72b2015-04-07 11:27:13 +02001029 struct gb_operation_msg_hdr header;
Johan Hovold25cdd7a2015-11-25 15:59:16 +01001030 struct device *dev = &connection->hd->dev;
Alex Elderd37b1db2014-11-19 12:27:17 -06001031 size_t msg_size;
Alex Elderd90c25b2014-10-16 06:35:33 -05001032
Johan Hovold8890f952016-05-27 17:26:33 +02001033 if (connection->state == GB_CONNECTION_STATE_DISABLED ||
Cristian Sicilia8478c352018-11-25 17:58:15 +01001034 gb_connection_is_offloaded(connection)) {
Eli Senneshb0e97bc2016-05-13 13:27:40 -04001035 dev_warn_ratelimited(dev, "%s: dropping %zu received bytes\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001036 connection->name, size);
Alex Elder36561f22014-10-22 02:04:30 -05001037 return;
Alex Elderd37b1db2014-11-19 12:27:17 -06001038 }
Alex Elder36561f22014-10-22 02:04:30 -05001039
Johan Hovold564c72b2015-04-07 11:27:13 +02001040 if (size < sizeof(header)) {
Eli Senneshb0e97bc2016-05-13 13:27:40 -04001041 dev_err_ratelimited(dev, "%s: short message received\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001042 connection->name);
Alex Elderd90c25b2014-10-16 06:35:33 -05001043 return;
1044 }
1045
Johan Hovold564c72b2015-04-07 11:27:13 +02001046 /* Use memcpy as data may be unaligned */
1047 memcpy(&header, data, sizeof(header));
1048 msg_size = le16_to_cpu(header.size);
Johan Hovold0150bd72015-03-27 12:41:20 +01001049 if (size < msg_size) {
Eli Senneshb0e97bc2016-05-13 13:27:40 -04001050 dev_err_ratelimited(dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001051 "%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 Elderd37b1db2014-11-19 12:27:17 -06001055 return; /* XXX Should still complete operation */
Alex Elderd90c25b2014-10-16 06:35:33 -05001056 }
1057
Johan Hovold2321f042016-07-26 17:11:30 +02001058 if (header.type & GB_MESSAGE_TYPE_RESPONSE) {
Johan Hovolddfcba862016-07-26 17:11:28 +02001059 gb_connection_recv_response(connection, &header, data,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001060 msg_size);
Johan Hovold2321f042016-07-26 17:11:30 +02001061 } else {
1062 gb_connection_recv_request(connection, &header, data,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001063 msg_size);
Johan Hovold2321f042016-07-26 17:11:30 +02001064 }
Alex Elder2eb585f2014-10-16 06:35:34 -05001065}
1066
Alex Eldere1158df2014-10-22 02:04:29 -05001067/*
Johan Hovold5a3be762015-07-14 15:43:35 +02001068 * Cancel an outgoing operation synchronously, and record the given error to
1069 * indicate why.
Alex Eldere1158df2014-10-22 02:04:29 -05001070 */
Alex Elderf68c05c2014-11-21 19:29:17 -06001071void gb_operation_cancel(struct gb_operation *operation, int errno)
Alex Eldere1158df2014-10-22 02:04:29 -05001072{
Johan Hovold5a3be762015-07-14 15:43:35 +02001073 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 Hovold701615f2015-07-23 10:50:03 +02001078 queue_work(gb_operation_completion_wq, &operation->work);
Alex Elderabe9a302014-11-25 11:33:14 -06001079 }
Bryan O'Donoghue5c8ad592015-09-18 16:38:45 +01001080 trace_gb_message_cancel_outgoing(operation->request);
Johan Hovoldfd7134a2015-07-14 15:43:26 +02001081
1082 atomic_inc(&operation->waiters);
1083 wait_event(gb_operation_cancellation_queue,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001084 !gb_operation_is_active(operation));
Johan Hovoldfd7134a2015-07-14 15:43:26 +02001085 atomic_dec(&operation->waiters);
Alex Eldere1158df2014-10-22 02:04:29 -05001086}
Johan Hovold1dad6b32015-03-27 12:41:10 +01001087EXPORT_SYMBOL_GPL(gb_operation_cancel);
Alex Eldere1158df2014-10-22 02:04:29 -05001088
Johan Hovold5a3be762015-07-14 15:43:35 +02001089/*
1090 * Cancel an incoming operation synchronously. Called during connection tear
1091 * down.
1092 */
1093void 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'Donoghue5c8ad592015-09-18 16:38:45 +01001107 trace_gb_message_cancel_incoming(operation->response);
Johan Hovold5a3be762015-07-14 15:43:35 +02001108
1109 atomic_inc(&operation->waiters);
1110 wait_event(gb_operation_cancellation_queue,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001111 !gb_operation_is_active(operation));
Johan Hovold5a3be762015-07-14 15:43:35 +02001112 atomic_dec(&operation->waiters);
1113}
1114
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -08001115/**
Johan Hovold410abdd2016-04-29 17:08:30 +02001116 * gb_operation_sync_timeout() - implement a "simple" synchronous operation
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -08001117 * @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 Hovold129a06f2015-07-14 15:43:37 +02001123 * @timeout: operation timeout in milliseconds
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -08001124 *
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 Hovold129a06f2015-07-14 15:43:37 +02001138int gb_operation_sync_timeout(struct gb_connection *connection, int type,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001139 void *request, int request_size,
1140 void *response, int response_size,
1141 unsigned int timeout)
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -08001142{
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 Hovolde4207212015-07-01 12:37:22 +02001151 request_size, response_size,
1152 GFP_KERNEL);
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -08001153 if (!operation)
1154 return -ENOMEM;
1155
1156 if (request_size)
Alex Elder6cd6ec52014-12-02 17:03:51 -06001157 memcpy(operation->request->payload, request, request_size);
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -08001158
Johan Hovold129a06f2015-07-14 15:43:37 +02001159 ret = gb_operation_request_send_sync_timeout(operation, timeout);
Johan Hovoldee8f81b2015-03-19 16:46:18 +01001160 if (ret) {
Viresh Kumar05e30952016-06-23 23:22:15 +05301161 dev_err(&connection->hd->dev,
David Line514dec2016-07-22 13:46:25 -07001162 "%s: synchronous operation id 0x%04x of type 0x%02x failed: %d\n",
1163 connection->name, operation->id, type, ret);
Johan Hovoldee8f81b2015-03-19 16:46:18 +01001164 } else {
1165 if (response_size) {
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -08001166 memcpy(response, operation->response->payload,
1167 response_size);
Johan Hovoldee8f81b2015-03-19 16:46:18 +01001168 }
1169 }
Johan Hovold6ab1ce42015-09-26 17:59:15 -07001170
1171 gb_operation_put(operation);
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -08001172
1173 return ret;
1174}
Johan Hovold129a06f2015-07-14 15:43:37 +02001175EXPORT_SYMBOL_GPL(gb_operation_sync_timeout);
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -08001176
Johan Hovold5fdc0272016-04-29 17:08:33 +02001177/**
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 */
1191int gb_operation_unidirectional_timeout(struct gb_connection *connection,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001192 int type, void *request,
1193 int request_size,
1194 unsigned int timeout)
Johan Hovold5fdc0272016-04-29 17:08:33 +02001195{
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 Sicilia8478c352018-11-25 17:58:15 +01001203 request_size, 0,
1204 GB_OPERATION_FLAG_UNIDIRECTIONAL,
1205 GFP_KERNEL);
Johan Hovold5fdc0272016-04-29 17:08:33 +02001206 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}
1223EXPORT_SYMBOL_GPL(gb_operation_unidirectional_timeout);
1224
Alex Elder47ed2c92015-06-09 17:42:50 -05001225int __init gb_operation_init(void)
Alex Elder2eb585f2014-10-16 06:35:34 -05001226{
Johan Hovold1e5613b2015-04-07 11:27:17 +02001227 gb_message_cache = kmem_cache_create("gb_message_cache",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001228 sizeof(struct gb_message), 0, 0,
1229 NULL);
Johan Hovold1e5613b2015-04-07 11:27:17 +02001230 if (!gb_message_cache)
Alex Elder0cffcac32014-12-02 08:30:35 -06001231 return -ENOMEM;
1232
Alex Elder5b3db0d2014-10-20 10:27:56 -05001233 gb_operation_cache = kmem_cache_create("gb_operation_cache",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001234 sizeof(struct gb_operation), 0,
1235 0, NULL);
Alex Elder5b3db0d2014-10-20 10:27:56 -05001236 if (!gb_operation_cache)
Johan Hovold1e5613b2015-04-07 11:27:17 +02001237 goto err_destroy_message_cache;
Alex Elder2eb585f2014-10-16 06:35:34 -05001238
Johan Hovold701615f2015-07-23 10:50:03 +02001239 gb_operation_completion_wq = alloc_workqueue("greybus_completion",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001240 0, 0);
Johan Hovold701615f2015-07-23 10:50:03 +02001241 if (!gb_operation_completion_wq)
1242 goto err_destroy_operation_cache;
1243
Alex Elder2eb585f2014-10-16 06:35:34 -05001244 return 0;
Johan Hovold5a5bc352015-07-23 10:50:02 +02001245
Johan Hovold701615f2015-07-23 10:50:03 +02001246err_destroy_operation_cache:
1247 kmem_cache_destroy(gb_operation_cache);
1248 gb_operation_cache = NULL;
Johan Hovold1e5613b2015-04-07 11:27:17 +02001249err_destroy_message_cache:
1250 kmem_cache_destroy(gb_message_cache);
1251 gb_message_cache = NULL;
Alex Elder0cffcac32014-12-02 08:30:35 -06001252
1253 return -ENOMEM;
Alex Elder2eb585f2014-10-16 06:35:34 -05001254}
1255
Alex Elderf35ab902015-06-09 17:42:51 -05001256void gb_operation_exit(void)
Alex Elder2eb585f2014-10-16 06:35:34 -05001257{
Johan Hovold701615f2015-07-23 10:50:03 +02001258 destroy_workqueue(gb_operation_completion_wq);
1259 gb_operation_completion_wq = NULL;
Viresh Kumar837b3b72014-11-14 17:25:00 +05301260 kmem_cache_destroy(gb_operation_cache);
1261 gb_operation_cache = NULL;
Johan Hovold1e5613b2015-04-07 11:27:17 +02001262 kmem_cache_destroy(gb_message_cache);
1263 gb_message_cache = NULL;
Alex Elderd90c25b2014-10-16 06:35:33 -05001264}