4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/wait.h>
15 #include <linux/workqueue.h>
18 #include "greybus_trace.h"
20 static struct kmem_cache *gb_operation_cache;
21 static struct kmem_cache *gb_message_cache;
23 /* Workqueue to handle Greybus operation completions. */
24 static struct workqueue_struct *gb_operation_completion_wq;
26 /* Wait queue for synchronous cancellations. */
27 static DECLARE_WAIT_QUEUE_HEAD(gb_operation_cancellation_queue);
30 * Protects updates to operation->errno.
32 static DEFINE_SPINLOCK(gb_operations_lock);
34 static int gb_operation_response_send(struct gb_operation *operation,
38 * Increment operation active count and add to connection list unless the
39 * connection is going away.
41 * Caller holds operation reference.
43 static int gb_operation_get_active(struct gb_operation *operation)
45 struct gb_connection *connection = operation->connection;
48 spin_lock_irqsave(&connection->lock, flags);
49 switch (connection->state) {
50 case GB_CONNECTION_STATE_ENABLED:
52 case GB_CONNECTION_STATE_ENABLED_TX:
53 if (gb_operation_is_incoming(operation))
56 case GB_CONNECTION_STATE_DISCONNECTING:
57 if (!gb_operation_is_core(operation))
64 if (operation->active++ == 0)
65 list_add_tail(&operation->links, &connection->operations);
67 trace_gb_operation_get_active(operation);
69 spin_unlock_irqrestore(&connection->lock, flags);
74 spin_unlock_irqrestore(&connection->lock, flags);
79 /* Caller holds operation reference. */
80 static void gb_operation_put_active(struct gb_operation *operation)
82 struct gb_connection *connection = operation->connection;
85 spin_lock_irqsave(&connection->lock, flags);
87 trace_gb_operation_put_active(operation);
89 if (--operation->active == 0) {
90 list_del(&operation->links);
91 if (atomic_read(&operation->waiters))
92 wake_up(&gb_operation_cancellation_queue);
94 spin_unlock_irqrestore(&connection->lock, flags);
97 static bool gb_operation_is_active(struct gb_operation *operation)
99 struct gb_connection *connection = operation->connection;
103 spin_lock_irqsave(&connection->lock, flags);
104 ret = operation->active;
105 spin_unlock_irqrestore(&connection->lock, flags);
111 * Set an operation's result.
113 * Initially an outgoing operation's errno value is -EBADR.
114 * If no error occurs before sending the request message the only
115 * valid value operation->errno can be set to is -EINPROGRESS,
116 * indicating the request has been (or rather is about to be) sent.
117 * At that point nobody should be looking at the result until the
120 * The first time the result gets set after the request has been
121 * sent, that result "sticks." That is, if two concurrent threads
122 * race to set the result, the first one wins. The return value
123 * tells the caller whether its result was recorded; if not the
124 * caller has nothing more to do.
126 * The result value -EILSEQ is reserved to signal an implementation
127 * error; if it's ever observed, the code performing the request has
128 * done something fundamentally wrong. It is an error to try to set
129 * the result to -EBADR, and attempts to do so result in a warning,
130 * and -EILSEQ is used instead. Similarly, the only valid result
131 * value to set for an operation in initial state is -EINPROGRESS.
132 * Attempts to do otherwise will also record a (successful) -EILSEQ
135 static bool gb_operation_result_set(struct gb_operation *operation, int result)
140 if (result == -EINPROGRESS) {
142 * -EINPROGRESS is used to indicate the request is
143 * in flight. It should be the first result value
144 * set after the initial -EBADR. Issue a warning
145 * and record an implementation error if it's
146 * set at any other time.
148 spin_lock_irqsave(&gb_operations_lock, flags);
149 prev = operation->errno;
151 operation->errno = result;
153 operation->errno = -EILSEQ;
154 spin_unlock_irqrestore(&gb_operations_lock, flags);
155 WARN_ON(prev != -EBADR);
161 * The first result value set after a request has been sent
162 * will be the final result of the operation. Subsequent
163 * attempts to set the result are ignored.
165 * Note that -EBADR is a reserved "initial state" result
166 * value. Attempts to set this value result in a warning,
167 * and the result code is set to -EILSEQ instead.
169 if (WARN_ON(result == -EBADR))
170 result = -EILSEQ; /* Nobody should be setting -EBADR */
172 spin_lock_irqsave(&gb_operations_lock, flags);
173 prev = operation->errno;
174 if (prev == -EINPROGRESS)
175 operation->errno = result; /* First and final result */
176 spin_unlock_irqrestore(&gb_operations_lock, flags);
178 return prev == -EINPROGRESS;
181 int gb_operation_result(struct gb_operation *operation)
183 int result = operation->errno;
185 WARN_ON(result == -EBADR);
186 WARN_ON(result == -EINPROGRESS);
190 EXPORT_SYMBOL_GPL(gb_operation_result);
193 * Looks up an outgoing operation on a connection and returns a refcounted
194 * pointer if found, or NULL otherwise.
196 static struct gb_operation *
197 gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id)
199 struct gb_operation *operation;
203 spin_lock_irqsave(&connection->lock, flags);
204 list_for_each_entry(operation, &connection->operations, links)
205 if (operation->id == operation_id &&
206 !gb_operation_is_incoming(operation)) {
207 gb_operation_get(operation);
211 spin_unlock_irqrestore(&connection->lock, flags);
213 return found ? operation : NULL;
216 static int gb_message_send(struct gb_message *message, gfp_t gfp)
218 struct gb_connection *connection = message->operation->connection;
220 trace_gb_message_send(message);
221 return connection->hd->driver->message_send(connection->hd,
222 connection->hd_cport_id,
228 * Cancel a message we have passed to the host device layer to be sent.
230 static void gb_message_cancel(struct gb_message *message)
232 struct gb_host_device *hd = message->operation->connection->hd;
234 hd->driver->message_cancel(message);
237 static void gb_operation_request_handle(struct gb_operation *operation)
239 struct gb_connection *connection = operation->connection;
243 if (connection->handler) {
244 status = connection->handler(operation);
246 dev_err(&connection->hd->dev,
247 "%s: unexpected incoming request of type 0x%02x\n",
248 connection->name, operation->type);
250 status = -EPROTONOSUPPORT;
253 ret = gb_operation_response_send(operation, status);
255 dev_err(&connection->hd->dev,
256 "%s: failed to send response %d for type 0x%02x: %d\n",
257 connection->name, status, operation->type, ret);
263 * Process operation work.
265 * For incoming requests, call the protocol request handler. The operation
266 * result should be -EINPROGRESS at this point.
268 * For outgoing requests, the operation result value should have
269 * been set before queueing this. The operation callback function
270 * allows the original requester to know the request has completed
271 * and its result is available.
273 static void gb_operation_work(struct work_struct *work)
275 struct gb_operation *operation;
278 operation = container_of(work, struct gb_operation, work);
280 if (gb_operation_is_incoming(operation)) {
281 gb_operation_request_handle(operation);
283 ret = del_timer_sync(&operation->timer);
285 /* Cancel request message if scheduled by timeout. */
286 if (gb_operation_result(operation) == -ETIMEDOUT)
287 gb_message_cancel(operation->request);
290 operation->callback(operation);
293 gb_operation_put_active(operation);
294 gb_operation_put(operation);
297 static void gb_operation_timeout(unsigned long arg)
299 struct gb_operation *operation = (void *)arg;
301 if (gb_operation_result_set(operation, -ETIMEDOUT)) {
303 * A stuck request message will be cancelled from the
306 queue_work(gb_operation_completion_wq, &operation->work);
310 static void gb_operation_message_init(struct gb_host_device *hd,
311 struct gb_message *message, u16 operation_id,
312 size_t payload_size, u8 type)
314 struct gb_operation_msg_hdr *header;
316 header = message->buffer;
318 message->header = header;
319 message->payload = payload_size ? header + 1 : NULL;
320 message->payload_size = payload_size;
323 * The type supplied for incoming message buffers will be
324 * GB_REQUEST_TYPE_INVALID. Such buffers will be overwritten by
325 * arriving data so there's no need to initialize the message header.
327 if (type != GB_REQUEST_TYPE_INVALID) {
328 u16 message_size = (u16)(sizeof(*header) + payload_size);
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.
335 * The result field in a request message must be
336 * zero. It will be set just prior to sending for
339 header->size = cpu_to_le16(message_size);
340 header->operation_id = 0;
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.
353 * The headers for inbound messages don't need to be initialized;
354 * they'll be filled in by arriving data.
356 * Our message buffers have the following layout:
357 * message header \_ these combined are
358 * message payload / the message size
360 static struct gb_message *
361 gb_operation_message_alloc(struct gb_host_device *hd, u8 type,
362 size_t payload_size, gfp_t gfp_flags)
364 struct gb_message *message;
365 struct gb_operation_msg_hdr *header;
366 size_t message_size = payload_size + sizeof(*header);
368 if (message_size > hd->buffer_size_max) {
369 dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n",
370 message_size, hd->buffer_size_max);
374 /* Allocate the message structure and buffer. */
375 message = kmem_cache_zalloc(gb_message_cache, gfp_flags);
379 message->buffer = kzalloc(message_size, gfp_flags);
380 if (!message->buffer)
381 goto err_free_message;
383 /* Initialize the message. Operation id is filled in later. */
384 gb_operation_message_init(hd, message, 0, payload_size, type);
389 kmem_cache_free(gb_message_cache, message);
394 static void gb_operation_message_free(struct gb_message *message)
396 kfree(message->buffer);
397 kmem_cache_free(gb_message_cache, message);
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.
404 static int gb_operation_status_map(u8 status)
409 case GB_OP_INTERRUPTED:
413 case GB_OP_NO_MEMORY:
415 case GB_OP_PROTOCOL_BAD:
416 return -EPROTONOSUPPORT;
423 case GB_OP_NONEXISTENT:
425 case GB_OP_MALFUNCTION:
427 case GB_OP_UNKNOWN_ERROR:
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).
439 static u8 gb_operation_errno_map(int errno)
443 return GB_OP_SUCCESS;
445 return GB_OP_INTERRUPTED;
447 return GB_OP_TIMEOUT;
449 return GB_OP_NO_MEMORY;
450 case -EPROTONOSUPPORT:
451 return GB_OP_PROTOCOL_BAD;
453 return GB_OP_OVERFLOW; /* Could be underflow too */
455 return GB_OP_INVALID;
459 return GB_OP_MALFUNCTION;
461 return GB_OP_NONEXISTENT;
464 return GB_OP_UNKNOWN_ERROR;
468 bool gb_operation_response_alloc(struct gb_operation *operation,
469 size_t response_size, gfp_t gfp)
471 struct gb_host_device *hd = operation->connection->hd;
472 struct gb_operation_msg_hdr *request_header;
473 struct gb_message *response;
476 type = operation->type | GB_MESSAGE_TYPE_RESPONSE;
477 response = gb_operation_message_alloc(hd, type, response_size, gfp);
480 response->operation = operation;
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).
488 request_header = operation->request->header;
489 response->header->operation_id = request_header->operation_id;
490 operation->response = response;
494 EXPORT_SYMBOL_GPL(gb_operation_response_alloc);
497 * Create a Greybus operation to be sent over the given connection.
498 * The request buffer will be big enough for a payload of the given
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.
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.
515 * Returns a pointer to the new operation or a null pointer if an
518 static struct gb_operation *
519 gb_operation_create_common(struct gb_connection *connection, u8 type,
520 size_t request_size, size_t response_size,
521 unsigned long op_flags, gfp_t gfp_flags)
523 struct gb_host_device *hd = connection->hd;
524 struct gb_operation *operation;
526 operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
529 operation->connection = connection;
531 operation->request = gb_operation_message_alloc(hd, type, request_size,
533 if (!operation->request)
535 operation->request->operation = operation;
537 /* Allocate the response buffer for outgoing operations */
538 if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) {
539 if (!gb_operation_response_alloc(operation, response_size,
544 setup_timer(&operation->timer, gb_operation_timeout,
545 (unsigned long)operation);
548 operation->flags = op_flags;
549 operation->type = type;
550 operation->errno = -EBADR; /* Initial value--means "never set" */
552 INIT_WORK(&operation->work, gb_operation_work);
553 init_completion(&operation->completion);
554 kref_init(&operation->kref);
555 atomic_set(&operation->waiters, 0);
560 gb_operation_message_free(operation->request);
562 kmem_cache_free(gb_operation_cache, operation);
568 * Create a new operation associated with the given connection. The
569 * request and response sizes provided are the number of bytes
570 * required to hold the request/response payload only. Both of
571 * these are allowed to be 0. Note that 0x00 is reserved as an
572 * invalid operation type for all protocols, and this is enforced
575 struct gb_operation *
576 gb_operation_create_flags(struct gb_connection *connection,
577 u8 type, size_t request_size,
578 size_t response_size, unsigned long flags,
581 struct gb_operation *operation;
583 if (WARN_ON_ONCE(type == GB_REQUEST_TYPE_INVALID))
585 if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE))
586 type &= ~GB_MESSAGE_TYPE_RESPONSE;
588 if (WARN_ON_ONCE(flags & ~GB_OPERATION_FLAG_USER_MASK))
589 flags &= GB_OPERATION_FLAG_USER_MASK;
591 operation = gb_operation_create_common(connection, type,
592 request_size, response_size,
595 trace_gb_operation_create(operation);
599 EXPORT_SYMBOL_GPL(gb_operation_create_flags);
601 struct gb_operation *
602 gb_operation_create_core(struct gb_connection *connection,
603 u8 type, size_t request_size,
604 size_t response_size, unsigned long flags,
607 struct gb_operation *operation;
609 flags |= GB_OPERATION_FLAG_CORE;
611 operation = gb_operation_create_common(connection, type,
612 request_size, response_size,
615 trace_gb_operation_create_core(operation);
619 /* Do not export this function. */
621 size_t gb_operation_get_payload_size_max(struct gb_connection *connection)
623 struct gb_host_device *hd = connection->hd;
625 return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr);
627 EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max);
629 static struct gb_operation *
630 gb_operation_create_incoming(struct gb_connection *connection, u16 id,
631 u8 type, void *data, size_t size)
633 struct gb_operation *operation;
635 unsigned long flags = GB_OPERATION_FLAG_INCOMING;
637 /* Caller has made sure we at least have a message header. */
638 request_size = size - sizeof(struct gb_operation_msg_hdr);
641 flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL;
643 operation = gb_operation_create_common(connection, type,
645 GB_REQUEST_TYPE_INVALID,
651 memcpy(operation->request->header, data, size);
652 trace_gb_operation_create_incoming(operation);
658 * Get an additional reference on an operation.
660 void gb_operation_get(struct gb_operation *operation)
662 kref_get(&operation->kref);
664 EXPORT_SYMBOL_GPL(gb_operation_get);
667 * Destroy a previously created operation.
669 static void _gb_operation_destroy(struct kref *kref)
671 struct gb_operation *operation;
673 operation = container_of(kref, struct gb_operation, kref);
675 trace_gb_operation_destroy(operation);
677 if (operation->response)
678 gb_operation_message_free(operation->response);
679 gb_operation_message_free(operation->request);
681 kmem_cache_free(gb_operation_cache, operation);
685 * Drop a reference on an operation, and destroy it when the last
688 void gb_operation_put(struct gb_operation *operation)
690 if (WARN_ON(!operation))
693 kref_put(&operation->kref, _gb_operation_destroy);
695 EXPORT_SYMBOL_GPL(gb_operation_put);
697 /* Tell the requester we're done */
698 static void gb_operation_sync_callback(struct gb_operation *operation)
700 complete(&operation->completion);
704 * gb_operation_request_send() - send an operation request message
705 * @operation: the operation to initiate
706 * @callback: the operation completion callback
707 * @timeout: operation timeout in milliseconds, or zero for no timeout
708 * @gfp: the memory flags to use for any allocations
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
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
717 * Return: 0 if the request was successfully queued in the host-driver queues,
718 * or a negative errno.
720 int gb_operation_request_send(struct gb_operation *operation,
721 gb_operation_callback callback,
722 unsigned int timeout,
725 struct gb_connection *connection = operation->connection;
726 struct gb_operation_msg_hdr *header;
730 if (gb_connection_is_offloaded(connection))
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.
741 operation->callback = callback;
744 * Assign the operation's id, and store it in the request header.
745 * Zero is a reserved operation id for unidirectional operations.
747 if (gb_operation_is_unidirectional(operation)) {
750 cycle = (unsigned int)atomic_inc_return(&connection->op_cycle);
751 operation->id = (u16)(cycle % U16_MAX + 1);
754 header = operation->request->header;
755 header->operation_id = cpu_to_le16(operation->id);
757 gb_operation_result_set(operation, -EINPROGRESS);
760 * Get an extra reference on the operation. It'll be dropped when the
761 * operation completes.
763 gb_operation_get(operation);
764 ret = gb_operation_get_active(operation);
768 ret = gb_message_send(operation->request, gfp);
773 operation->timer.expires = jiffies + msecs_to_jiffies(timeout);
774 add_timer(&operation->timer);
780 gb_operation_put_active(operation);
782 gb_operation_put(operation);
786 EXPORT_SYMBOL_GPL(gb_operation_request_send);
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
794 int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
795 unsigned int timeout)
799 ret = gb_operation_request_send(operation, gb_operation_sync_callback,
800 timeout, GFP_KERNEL);
804 ret = wait_for_completion_interruptible(&operation->completion);
806 /* Cancel the operation if interrupted */
807 gb_operation_cancel(operation, -ECANCELED);
810 return gb_operation_result(operation);
812 EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout);
815 * Send a response for an incoming operation request. A non-zero
816 * errno indicates a failed operation.
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.
823 static int gb_operation_response_send(struct gb_operation *operation,
826 struct gb_connection *connection = operation->connection;
829 if (!operation->response &&
830 !gb_operation_is_unidirectional(operation)) {
831 if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL))
835 /* Record the result */
836 if (!gb_operation_result_set(operation, errno)) {
837 dev_err(&connection->hd->dev, "request result already set\n");
838 return -EIO; /* Shouldn't happen */
841 /* Sender of request does not care about response. */
842 if (gb_operation_is_unidirectional(operation))
845 /* Reference will be dropped when message has been sent. */
846 gb_operation_get(operation);
847 ret = gb_operation_get_active(operation);
851 /* Fill in the response header and send it */
852 operation->response->header->result = gb_operation_errno_map(errno);
854 ret = gb_message_send(operation->response, GFP_KERNEL);
861 gb_operation_put_active(operation);
863 gb_operation_put(operation);
869 * This function is called when a message send request has completed.
871 void greybus_message_sent(struct gb_host_device *hd,
872 struct gb_message *message, int status)
874 struct gb_operation *operation = message->operation;
875 struct gb_connection *connection = operation->connection;
878 * If the message was a response, we just need to drop our
879 * reference to the operation. If an error occurred, report
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.
888 if (message == operation->response) {
890 dev_err(&connection->hd->dev,
891 "%s: error sending response 0x%02x: %d\n",
892 connection->name, operation->type, status);
895 gb_operation_put_active(operation);
896 gb_operation_put(operation);
897 } else if (status || gb_operation_is_unidirectional(operation)) {
898 if (gb_operation_result_set(operation, status)) {
899 queue_work(gb_operation_completion_wq,
904 EXPORT_SYMBOL_GPL(greybus_message_sent);
907 * We've received data on a connection, and it doesn't look like a
908 * response, so we assume it's a request.
910 * This is called in interrupt context, so just copy the incoming
911 * data into the request buffer and handle the rest via workqueue.
913 static void gb_connection_recv_request(struct gb_connection *connection,
914 const struct gb_operation_msg_hdr *header,
915 void *data, size_t size)
917 struct gb_operation *operation;
922 operation_id = le16_to_cpu(header->operation_id);
925 operation = gb_operation_create_incoming(connection, operation_id,
928 dev_err(&connection->hd->dev,
929 "%s: can't create incoming operation\n",
934 ret = gb_operation_get_active(operation);
936 gb_operation_put(operation);
939 trace_gb_message_recv_request(operation->request);
942 * The initial reference to the operation will be dropped when the
943 * request handler returns.
945 if (gb_operation_result_set(operation, -EINPROGRESS))
946 queue_work(connection->wq, &operation->work);
950 * We've received data that appears to be an operation response
951 * message. Look up the operation, and record that we've received
954 * This is called in interrupt context, so just copy the incoming
955 * data into the response buffer and handle the rest via workqueue.
957 static void gb_connection_recv_response(struct gb_connection *connection,
958 const struct gb_operation_msg_hdr *header,
959 void *data, size_t size)
961 struct gb_operation *operation;
962 struct gb_message *message;
967 operation_id = le16_to_cpu(header->operation_id);
970 dev_err_ratelimited(&connection->hd->dev,
971 "%s: invalid response id 0 received\n",
976 operation = gb_operation_find_outgoing(connection, operation_id);
978 dev_err_ratelimited(&connection->hd->dev,
979 "%s: unexpected response id 0x%04x received\n",
980 connection->name, operation_id);
984 errno = gb_operation_status_map(header->result);
985 message = operation->response;
986 message_size = sizeof(*header) + message->payload_size;
987 if (!errno && size > message_size) {
988 dev_err_ratelimited(&connection->hd->dev,
989 "%s: malformed response 0x%02x received (%zu > %zu)\n",
990 connection->name, header->type,
993 } else if (!errno && size < message_size) {
994 if (gb_operation_short_response_allowed(operation)) {
995 message->payload_size = size - sizeof(*header);
997 dev_err_ratelimited(&connection->hd->dev,
998 "%s: short response 0x%02x received (%zu < %zu)\n",
999 connection->name, header->type,
1000 size, message_size);
1005 /* We must ignore the payload if a bad status is returned */
1007 size = sizeof(*header);
1009 /* The rest will be handled in work queue context */
1010 if (gb_operation_result_set(operation, errno)) {
1011 memcpy(message->buffer, data, size);
1013 trace_gb_message_recv_response(message);
1015 queue_work(gb_operation_completion_wq, &operation->work);
1018 gb_operation_put(operation);
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).
1026 void gb_connection_recv(struct gb_connection *connection,
1027 void *data, size_t size)
1029 struct gb_operation_msg_hdr header;
1030 struct device *dev = &connection->hd->dev;
1033 if (connection->state == GB_CONNECTION_STATE_DISABLED ||
1034 gb_connection_is_offloaded(connection)) {
1035 dev_warn_ratelimited(dev, "%s: dropping %zu received bytes\n",
1036 connection->name, size);
1040 if (size < sizeof(header)) {
1041 dev_err_ratelimited(dev, "%s: short message received\n",
1046 /* Use memcpy as data may be unaligned */
1047 memcpy(&header, data, sizeof(header));
1048 msg_size = le16_to_cpu(header.size);
1049 if (size < msg_size) {
1050 dev_err_ratelimited(dev,
1051 "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n",
1053 le16_to_cpu(header.operation_id),
1054 header.type, size, msg_size);
1055 return; /* XXX Should still complete operation */
1058 if (header.type & GB_MESSAGE_TYPE_RESPONSE) {
1059 gb_connection_recv_response(connection, &header, data,
1062 gb_connection_recv_request(connection, &header, data,
1068 * Cancel an outgoing operation synchronously, and record the given error to
1071 void gb_operation_cancel(struct gb_operation *operation, int errno)
1073 if (WARN_ON(gb_operation_is_incoming(operation)))
1076 if (gb_operation_result_set(operation, errno)) {
1077 gb_message_cancel(operation->request);
1078 queue_work(gb_operation_completion_wq, &operation->work);
1080 trace_gb_message_cancel_outgoing(operation->request);
1082 atomic_inc(&operation->waiters);
1083 wait_event(gb_operation_cancellation_queue,
1084 !gb_operation_is_active(operation));
1085 atomic_dec(&operation->waiters);
1087 EXPORT_SYMBOL_GPL(gb_operation_cancel);
1090 * Cancel an incoming operation synchronously. Called during connection tear
1093 void gb_operation_cancel_incoming(struct gb_operation *operation, int errno)
1095 if (WARN_ON(!gb_operation_is_incoming(operation)))
1098 if (!gb_operation_is_unidirectional(operation)) {
1100 * Make sure the request handler has submitted the response
1101 * before cancelling it.
1103 flush_work(&operation->work);
1104 if (!gb_operation_result_set(operation, errno))
1105 gb_message_cancel(operation->response);
1107 trace_gb_message_cancel_incoming(operation->response);
1109 atomic_inc(&operation->waiters);
1110 wait_event(gb_operation_cancellation_queue,
1111 !gb_operation_is_active(operation));
1112 atomic_dec(&operation->waiters);
1116 * gb_operation_sync_timeout() - implement a "simple" synchronous operation
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.
1123 * @timeout: operation timeout in milliseconds
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.
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
1136 * If there is an error, the response buffer is left alone.
1138 int gb_operation_sync_timeout(struct gb_connection *connection, int type,
1139 void *request, int request_size,
1140 void *response, int response_size,
1141 unsigned int timeout)
1143 struct gb_operation *operation;
1146 if ((response_size && !response) ||
1147 (request_size && !request))
1150 operation = gb_operation_create(connection, type,
1151 request_size, response_size,
1157 memcpy(operation->request->payload, request, request_size);
1159 ret = gb_operation_request_send_sync_timeout(operation, timeout);
1161 dev_err(&connection->hd->dev,
1162 "%s: synchronous operation id 0x%04x of type 0x%02x failed: %d\n",
1163 connection->name, operation->id, type, ret);
1165 if (response_size) {
1166 memcpy(response, operation->response->payload,
1171 gb_operation_put(operation);
1175 EXPORT_SYMBOL_GPL(gb_operation_sync_timeout);
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
1185 * Initiate a unidirectional operation by sending a request message and
1186 * waiting for it to be acknowledged as sent by the host device.
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.
1191 int gb_operation_unidirectional_timeout(struct gb_connection *connection,
1192 int type, void *request, int request_size,
1193 unsigned int timeout)
1195 struct gb_operation *operation;
1198 if (request_size && !request)
1201 operation = gb_operation_create_flags(connection, type,
1203 GB_OPERATION_FLAG_UNIDIRECTIONAL,
1209 memcpy(operation->request->payload, request, request_size);
1211 ret = gb_operation_request_send_sync_timeout(operation, timeout);
1213 dev_err(&connection->hd->dev,
1214 "%s: unidirectional operation of type 0x%02x failed: %d\n",
1215 connection->name, type, ret);
1218 gb_operation_put(operation);
1222 EXPORT_SYMBOL_GPL(gb_operation_unidirectional_timeout);
1224 int __init gb_operation_init(void)
1226 gb_message_cache = kmem_cache_create("gb_message_cache",
1227 sizeof(struct gb_message), 0, 0, NULL);
1228 if (!gb_message_cache)
1231 gb_operation_cache = kmem_cache_create("gb_operation_cache",
1232 sizeof(struct gb_operation), 0, 0, NULL);
1233 if (!gb_operation_cache)
1234 goto err_destroy_message_cache;
1236 gb_operation_completion_wq = alloc_workqueue("greybus_completion",
1238 if (!gb_operation_completion_wq)
1239 goto err_destroy_operation_cache;
1243 err_destroy_operation_cache:
1244 kmem_cache_destroy(gb_operation_cache);
1245 gb_operation_cache = NULL;
1246 err_destroy_message_cache:
1247 kmem_cache_destroy(gb_message_cache);
1248 gb_message_cache = NULL;
1253 void gb_operation_exit(void)
1255 destroy_workqueue(gb_operation_completion_wq);
1256 gb_operation_completion_wq = NULL;
1257 kmem_cache_destroy(gb_operation_cache);
1258 gb_operation_cache = NULL;
1259 kmem_cache_destroy(gb_message_cache);
1260 gb_message_cache = NULL;