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);
50 if (connection->state != GB_CONNECTION_STATE_ENABLED &&
51 connection->state != GB_CONNECTION_STATE_ENABLED_TX &&
52 !gb_operation_is_incoming(operation)) {
53 spin_unlock_irqrestore(&connection->lock, flags);
57 if (operation->active++ == 0)
58 list_add_tail(&operation->links, &connection->operations);
60 spin_unlock_irqrestore(&connection->lock, flags);
65 /* Caller holds operation reference. */
66 static void gb_operation_put_active(struct gb_operation *operation)
68 struct gb_connection *connection = operation->connection;
71 spin_lock_irqsave(&connection->lock, flags);
72 if (--operation->active == 0) {
73 list_del(&operation->links);
74 if (atomic_read(&operation->waiters))
75 wake_up(&gb_operation_cancellation_queue);
77 spin_unlock_irqrestore(&connection->lock, flags);
80 static bool gb_operation_is_active(struct gb_operation *operation)
82 struct gb_connection *connection = operation->connection;
86 spin_lock_irqsave(&connection->lock, flags);
87 ret = operation->active;
88 spin_unlock_irqrestore(&connection->lock, flags);
94 * Set an operation's result.
96 * Initially an outgoing operation's errno value is -EBADR.
97 * If no error occurs before sending the request message the only
98 * valid value operation->errno can be set to is -EINPROGRESS,
99 * indicating the request has been (or rather is about to be) sent.
100 * At that point nobody should be looking at the result until the
103 * The first time the result gets set after the request has been
104 * sent, that result "sticks." That is, if two concurrent threads
105 * race to set the result, the first one wins. The return value
106 * tells the caller whether its result was recorded; if not the
107 * caller has nothing more to do.
109 * The result value -EILSEQ is reserved to signal an implementation
110 * error; if it's ever observed, the code performing the request has
111 * done something fundamentally wrong. It is an error to try to set
112 * the result to -EBADR, and attempts to do so result in a warning,
113 * and -EILSEQ is used instead. Similarly, the only valid result
114 * value to set for an operation in initial state is -EINPROGRESS.
115 * Attempts to do otherwise will also record a (successful) -EILSEQ
118 static bool gb_operation_result_set(struct gb_operation *operation, int result)
123 if (result == -EINPROGRESS) {
125 * -EINPROGRESS is used to indicate the request is
126 * in flight. It should be the first result value
127 * set after the initial -EBADR. Issue a warning
128 * and record an implementation error if it's
129 * set at any other time.
131 spin_lock_irqsave(&gb_operations_lock, flags);
132 prev = operation->errno;
134 operation->errno = result;
136 operation->errno = -EILSEQ;
137 spin_unlock_irqrestore(&gb_operations_lock, flags);
138 WARN_ON(prev != -EBADR);
144 * The first result value set after a request has been sent
145 * will be the final result of the operation. Subsequent
146 * attempts to set the result are ignored.
148 * Note that -EBADR is a reserved "initial state" result
149 * value. Attempts to set this value result in a warning,
150 * and the result code is set to -EILSEQ instead.
152 if (WARN_ON(result == -EBADR))
153 result = -EILSEQ; /* Nobody should be setting -EBADR */
155 spin_lock_irqsave(&gb_operations_lock, flags);
156 prev = operation->errno;
157 if (prev == -EINPROGRESS)
158 operation->errno = result; /* First and final result */
159 spin_unlock_irqrestore(&gb_operations_lock, flags);
161 return prev == -EINPROGRESS;
164 int gb_operation_result(struct gb_operation *operation)
166 int result = operation->errno;
168 WARN_ON(result == -EBADR);
169 WARN_ON(result == -EINPROGRESS);
173 EXPORT_SYMBOL_GPL(gb_operation_result);
176 * Looks up an outgoing operation on a connection and returns a refcounted
177 * pointer if found, or NULL otherwise.
179 static struct gb_operation *
180 gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id)
182 struct gb_operation *operation;
186 spin_lock_irqsave(&connection->lock, flags);
187 list_for_each_entry(operation, &connection->operations, links)
188 if (operation->id == operation_id &&
189 !gb_operation_is_incoming(operation)) {
190 gb_operation_get(operation);
194 spin_unlock_irqrestore(&connection->lock, flags);
196 return found ? operation : NULL;
199 static int gb_message_send(struct gb_message *message, gfp_t gfp)
201 struct gb_connection *connection = message->operation->connection;
203 trace_gb_message_send(message);
204 return connection->hd->driver->message_send(connection->hd,
205 connection->hd_cport_id,
211 * Cancel a message we have passed to the host device layer to be sent.
213 static void gb_message_cancel(struct gb_message *message)
215 struct gb_host_device *hd = message->operation->connection->hd;
217 hd->driver->message_cancel(message);
220 static void gb_operation_request_handle(struct gb_operation *operation)
222 struct gb_connection *connection = operation->connection;
226 if (connection->handler) {
227 status = connection->handler(operation);
229 dev_err(&connection->hd->dev,
230 "%s: unexpected incoming request of type 0x%02x\n",
231 connection->name, operation->type);
233 status = -EPROTONOSUPPORT;
236 ret = gb_operation_response_send(operation, status);
238 dev_err(&connection->hd->dev,
239 "%s: failed to send response %d for type 0x%02x: %d\n",
240 connection->name, status, operation->type, ret);
246 * Process operation work.
248 * For incoming requests, call the protocol request handler. The operation
249 * result should be -EINPROGRESS at this point.
251 * For outgoing requests, the operation result value should have
252 * been set before queueing this. The operation callback function
253 * allows the original requester to know the request has completed
254 * and its result is available.
256 static void gb_operation_work(struct work_struct *work)
258 struct gb_operation *operation;
260 operation = container_of(work, struct gb_operation, work);
262 if (gb_operation_is_incoming(operation))
263 gb_operation_request_handle(operation);
265 operation->callback(operation);
267 gb_operation_put_active(operation);
268 gb_operation_put(operation);
271 static void gb_operation_message_init(struct gb_host_device *hd,
272 struct gb_message *message, u16 operation_id,
273 size_t payload_size, u8 type)
275 struct gb_operation_msg_hdr *header;
277 header = message->buffer;
279 message->header = header;
280 message->payload = payload_size ? header + 1 : NULL;
281 message->payload_size = payload_size;
284 * The type supplied for incoming message buffers will be
285 * GB_REQUEST_TYPE_INVALID. Such buffers will be overwritten by
286 * arriving data so there's no need to initialize the message header.
288 if (type != GB_REQUEST_TYPE_INVALID) {
289 u16 message_size = (u16)(sizeof(*header) + payload_size);
292 * For a request, the operation id gets filled in
293 * when the message is sent. For a response, it
294 * will be copied from the request by the caller.
296 * The result field in a request message must be
297 * zero. It will be set just prior to sending for
300 header->size = cpu_to_le16(message_size);
301 header->operation_id = 0;
308 * Allocate a message to be used for an operation request or response.
309 * Both types of message contain a common header. The request message
310 * for an outgoing operation is outbound, as is the response message
311 * for an incoming operation. The message header for an outbound
312 * message is partially initialized here.
314 * The headers for inbound messages don't need to be initialized;
315 * they'll be filled in by arriving data.
317 * Our message buffers have the following layout:
318 * message header \_ these combined are
319 * message payload / the message size
321 static struct gb_message *
322 gb_operation_message_alloc(struct gb_host_device *hd, u8 type,
323 size_t payload_size, gfp_t gfp_flags)
325 struct gb_message *message;
326 struct gb_operation_msg_hdr *header;
327 size_t message_size = payload_size + sizeof(*header);
329 if (message_size > hd->buffer_size_max) {
330 dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n",
331 message_size, hd->buffer_size_max);
335 /* Allocate the message structure and buffer. */
336 message = kmem_cache_zalloc(gb_message_cache, gfp_flags);
340 message->buffer = kzalloc(message_size, gfp_flags);
341 if (!message->buffer)
342 goto err_free_message;
344 /* Initialize the message. Operation id is filled in later. */
345 gb_operation_message_init(hd, message, 0, payload_size, type);
350 kmem_cache_free(gb_message_cache, message);
355 static void gb_operation_message_free(struct gb_message *message)
357 kfree(message->buffer);
358 kmem_cache_free(gb_message_cache, message);
362 * Map an enum gb_operation_status value (which is represented in a
363 * message as a single byte) to an appropriate Linux negative errno.
365 static int gb_operation_status_map(u8 status)
370 case GB_OP_INTERRUPTED:
374 case GB_OP_NO_MEMORY:
376 case GB_OP_PROTOCOL_BAD:
377 return -EPROTONOSUPPORT;
384 case GB_OP_NONEXISTENT:
386 case GB_OP_MALFUNCTION:
388 case GB_OP_UNKNOWN_ERROR:
395 * Map a Linux errno value (from operation->errno) into the value
396 * that should represent it in a response message status sent
397 * over the wire. Returns an enum gb_operation_status value (which
398 * is represented in a message as a single byte).
400 static u8 gb_operation_errno_map(int errno)
404 return GB_OP_SUCCESS;
406 return GB_OP_INTERRUPTED;
408 return GB_OP_TIMEOUT;
410 return GB_OP_NO_MEMORY;
411 case -EPROTONOSUPPORT:
412 return GB_OP_PROTOCOL_BAD;
414 return GB_OP_OVERFLOW; /* Could be underflow too */
416 return GB_OP_INVALID;
420 return GB_OP_MALFUNCTION;
422 return GB_OP_NONEXISTENT;
425 return GB_OP_UNKNOWN_ERROR;
429 bool gb_operation_response_alloc(struct gb_operation *operation,
430 size_t response_size, gfp_t gfp)
432 struct gb_host_device *hd = operation->connection->hd;
433 struct gb_operation_msg_hdr *request_header;
434 struct gb_message *response;
437 type = operation->type | GB_MESSAGE_TYPE_RESPONSE;
438 response = gb_operation_message_alloc(hd, type, response_size, gfp);
441 response->operation = operation;
444 * Size and type get initialized when the message is
445 * allocated. The errno will be set before sending. All
446 * that's left is the operation id, which we copy from the
447 * request message header (as-is, in little-endian order).
449 request_header = operation->request->header;
450 response->header->operation_id = request_header->operation_id;
451 operation->response = response;
455 EXPORT_SYMBOL_GPL(gb_operation_response_alloc);
458 * Create a Greybus operation to be sent over the given connection.
459 * The request buffer will be big enough for a payload of the given
462 * For outgoing requests, the request message's header will be
463 * initialized with the type of the request and the message size.
464 * Outgoing operations must also specify the response buffer size,
465 * which must be sufficient to hold all expected response data. The
466 * response message header will eventually be overwritten, so there's
467 * no need to initialize it here.
469 * Request messages for incoming operations can arrive in interrupt
470 * context, so they must be allocated with GFP_ATOMIC. In this case
471 * the request buffer will be immediately overwritten, so there is
472 * no need to initialize the message header. Responsibility for
473 * allocating a response buffer lies with the incoming request
474 * handler for a protocol. So we don't allocate that here.
476 * Returns a pointer to the new operation or a null pointer if an
479 static struct gb_operation *
480 gb_operation_create_common(struct gb_connection *connection, u8 type,
481 size_t request_size, size_t response_size,
482 unsigned long op_flags, gfp_t gfp_flags)
484 struct gb_host_device *hd = connection->hd;
485 struct gb_operation *operation;
487 operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
490 operation->connection = connection;
492 operation->request = gb_operation_message_alloc(hd, type, request_size,
494 if (!operation->request)
496 operation->request->operation = operation;
498 /* Allocate the response buffer for outgoing operations */
499 if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) {
500 if (!gb_operation_response_alloc(operation, response_size,
506 operation->flags = op_flags;
507 operation->type = type;
508 operation->errno = -EBADR; /* Initial value--means "never set" */
510 INIT_WORK(&operation->work, gb_operation_work);
511 init_completion(&operation->completion);
512 kref_init(&operation->kref);
513 atomic_set(&operation->waiters, 0);
518 gb_operation_message_free(operation->request);
520 kmem_cache_free(gb_operation_cache, operation);
526 * Create a new operation associated with the given connection. The
527 * request and response sizes provided are the number of bytes
528 * required to hold the request/response payload only. Both of
529 * these are allowed to be 0. Note that 0x00 is reserved as an
530 * invalid operation type for all protocols, and this is enforced
533 struct gb_operation *
534 gb_operation_create_flags(struct gb_connection *connection,
535 u8 type, size_t request_size,
536 size_t response_size, unsigned long flags,
539 if (WARN_ON_ONCE(type == GB_REQUEST_TYPE_INVALID))
541 if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE))
542 type &= ~GB_MESSAGE_TYPE_RESPONSE;
544 if (WARN_ON_ONCE(flags & ~GB_OPERATION_FLAG_USER_MASK))
545 flags &= GB_OPERATION_FLAG_USER_MASK;
547 return gb_operation_create_common(connection, type,
548 request_size, response_size,
551 EXPORT_SYMBOL_GPL(gb_operation_create_flags);
553 size_t gb_operation_get_payload_size_max(struct gb_connection *connection)
555 struct gb_host_device *hd = connection->hd;
557 return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr);
559 EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max);
561 static struct gb_operation *
562 gb_operation_create_incoming(struct gb_connection *connection, u16 id,
563 u8 type, void *data, size_t size)
565 struct gb_operation *operation;
567 unsigned long flags = GB_OPERATION_FLAG_INCOMING;
569 /* Caller has made sure we at least have a message header. */
570 request_size = size - sizeof(struct gb_operation_msg_hdr);
573 flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL;
575 operation = gb_operation_create_common(connection, type,
577 GB_REQUEST_TYPE_INVALID,
583 memcpy(operation->request->header, data, size);
589 * Get an additional reference on an operation.
591 void gb_operation_get(struct gb_operation *operation)
593 kref_get(&operation->kref);
595 EXPORT_SYMBOL_GPL(gb_operation_get);
598 * Destroy a previously created operation.
600 static void _gb_operation_destroy(struct kref *kref)
602 struct gb_operation *operation;
604 operation = container_of(kref, struct gb_operation, kref);
606 if (operation->response)
607 gb_operation_message_free(operation->response);
608 gb_operation_message_free(operation->request);
610 kmem_cache_free(gb_operation_cache, operation);
614 * Drop a reference on an operation, and destroy it when the last
617 void gb_operation_put(struct gb_operation *operation)
619 if (WARN_ON(!operation))
622 kref_put(&operation->kref, _gb_operation_destroy);
624 EXPORT_SYMBOL_GPL(gb_operation_put);
626 /* Tell the requester we're done */
627 static void gb_operation_sync_callback(struct gb_operation *operation)
629 complete(&operation->completion);
633 * gb_operation_request_send() - send an operation request message
634 * @operation: the operation to initiate
635 * @callback: the operation completion callback
636 * @gfp: the memory flags to use for any allocations
638 * The caller has filled in any payload so the request message is ready to go.
639 * The callback function supplied will be called when the response message has
640 * arrived, a unidirectional request has been sent, or the operation is
641 * cancelled, indicating that the operation is complete. The callback function
642 * can fetch the result of the operation using gb_operation_result() if
645 * Return: 0 if the request was successfully queued in the host-driver queues,
646 * or a negative errno.
648 int gb_operation_request_send(struct gb_operation *operation,
649 gb_operation_callback callback,
652 struct gb_connection *connection = operation->connection;
653 struct gb_operation_msg_hdr *header;
657 if (gb_connection_is_offloaded(connection))
664 * Record the callback function, which is executed in
665 * non-atomic (workqueue) context when the final result
666 * of an operation has been set.
668 operation->callback = callback;
671 * Assign the operation's id, and store it in the request header.
672 * Zero is a reserved operation id for unidirectional operations.
674 if (gb_operation_is_unidirectional(operation)) {
677 cycle = (unsigned int)atomic_inc_return(&connection->op_cycle);
678 operation->id = (u16)(cycle % U16_MAX + 1);
681 header = operation->request->header;
682 header->operation_id = cpu_to_le16(operation->id);
684 gb_operation_result_set(operation, -EINPROGRESS);
687 * Get an extra reference on the operation. It'll be dropped when the
688 * operation completes.
690 gb_operation_get(operation);
691 ret = gb_operation_get_active(operation);
695 ret = gb_message_send(operation->request, gfp);
702 gb_operation_put_active(operation);
704 gb_operation_put(operation);
708 EXPORT_SYMBOL_GPL(gb_operation_request_send);
711 * Send a synchronous operation. This function is expected to
712 * block, returning only when the response has arrived, (or when an
713 * error is detected. The return value is the result of the
716 int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
717 unsigned int timeout)
720 unsigned long timeout_jiffies;
722 ret = gb_operation_request_send(operation, gb_operation_sync_callback,
728 timeout_jiffies = msecs_to_jiffies(timeout);
730 timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
732 ret = wait_for_completion_interruptible_timeout(&operation->completion,
735 /* Cancel the operation if interrupted */
736 gb_operation_cancel(operation, -ECANCELED);
737 } else if (ret == 0) {
738 /* Cancel the operation if op timed out */
739 gb_operation_cancel(operation, -ETIMEDOUT);
742 return gb_operation_result(operation);
744 EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout);
747 * Send a response for an incoming operation request. A non-zero
748 * errno indicates a failed operation.
750 * If there is any response payload, the incoming request handler is
751 * responsible for allocating the response message. Otherwise the
752 * it can simply supply the result errno; this function will
753 * allocate the response message if necessary.
755 static int gb_operation_response_send(struct gb_operation *operation,
758 struct gb_connection *connection = operation->connection;
761 if (!operation->response &&
762 !gb_operation_is_unidirectional(operation)) {
763 if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL))
767 /* Record the result */
768 if (!gb_operation_result_set(operation, errno)) {
769 dev_err(&connection->hd->dev, "request result already set\n");
770 return -EIO; /* Shouldn't happen */
773 /* Sender of request does not care about response. */
774 if (gb_operation_is_unidirectional(operation))
777 /* Reference will be dropped when message has been sent. */
778 gb_operation_get(operation);
779 ret = gb_operation_get_active(operation);
783 /* Fill in the response header and send it */
784 operation->response->header->result = gb_operation_errno_map(errno);
786 ret = gb_message_send(operation->response, GFP_KERNEL);
793 gb_operation_put_active(operation);
795 gb_operation_put(operation);
801 * This function is called when a message send request has completed.
803 void greybus_message_sent(struct gb_host_device *hd,
804 struct gb_message *message, int status)
806 struct gb_operation *operation = message->operation;
807 struct gb_connection *connection = operation->connection;
810 * If the message was a response, we just need to drop our
811 * reference to the operation. If an error occurred, report
814 * For requests, if there's no error and the operation in not
815 * unidirectional, there's nothing more to do until the response
816 * arrives. If an error occurred attempting to send it, or if the
817 * operation is unidrectional, record the result of the operation and
818 * schedule its completion.
820 if (message == operation->response) {
822 dev_err(&connection->hd->dev,
823 "%s: error sending response 0x%02x: %d\n",
824 connection->name, operation->type, status);
827 gb_operation_put_active(operation);
828 gb_operation_put(operation);
829 } else if (status || gb_operation_is_unidirectional(operation)) {
830 if (gb_operation_result_set(operation, status)) {
831 queue_work(gb_operation_completion_wq,
836 EXPORT_SYMBOL_GPL(greybus_message_sent);
839 * We've received data on a connection, and it doesn't look like a
840 * response, so we assume it's a request.
842 * This is called in interrupt context, so just copy the incoming
843 * data into the request buffer and handle the rest via workqueue.
845 static void gb_connection_recv_request(struct gb_connection *connection,
846 u16 operation_id, u8 type,
847 void *data, size_t size)
849 struct gb_operation *operation;
852 operation = gb_operation_create_incoming(connection, operation_id,
855 dev_err(&connection->hd->dev,
856 "%s: can't create incoming operation\n",
861 ret = gb_operation_get_active(operation);
863 gb_operation_put(operation);
866 trace_gb_message_recv_request(operation->request);
869 * The initial reference to the operation will be dropped when the
870 * request handler returns.
872 if (gb_operation_result_set(operation, -EINPROGRESS))
873 queue_work(connection->wq, &operation->work);
877 * We've received data that appears to be an operation response
878 * message. Look up the operation, and record that we've received
881 * This is called in interrupt context, so just copy the incoming
882 * data into the response buffer and handle the rest via workqueue.
884 static void gb_connection_recv_response(struct gb_connection *connection,
885 u16 operation_id, u8 result, void *data, size_t size)
887 struct gb_operation_msg_hdr *header;
888 struct gb_operation *operation;
889 struct gb_message *message;
890 int errno = gb_operation_status_map(result);
894 dev_err_ratelimited(&connection->hd->dev,
895 "%s: invalid response id 0 received\n",
900 operation = gb_operation_find_outgoing(connection, operation_id);
902 dev_err_ratelimited(&connection->hd->dev,
903 "%s: unexpected response id 0x%04x received\n",
904 connection->name, operation_id);
908 message = operation->response;
909 header = message->header;
910 message_size = sizeof(*header) + message->payload_size;
911 if (!errno && size > message_size) {
912 dev_err_ratelimited(&connection->hd->dev,
913 "%s: malformed response 0x%02x received (%zu > %zu)\n",
914 connection->name, header->type,
917 } else if (!errno && size < message_size) {
918 if (gb_operation_short_response_allowed(operation)) {
919 message->payload_size = size - sizeof(*header);
921 dev_err_ratelimited(&connection->hd->dev,
922 "%s: short response 0x%02x received (%zu < %zu)\n",
923 connection->name, header->type,
928 trace_gb_message_recv_response(operation->response);
930 /* We must ignore the payload if a bad status is returned */
932 size = sizeof(*header);
934 /* The rest will be handled in work queue context */
935 if (gb_operation_result_set(operation, errno)) {
936 memcpy(header, data, size);
937 queue_work(gb_operation_completion_wq, &operation->work);
940 gb_operation_put(operation);
944 * Handle data arriving on a connection. As soon as we return the
945 * supplied data buffer will be reused (so unless we do something
946 * with, it's effectively dropped).
948 void gb_connection_recv(struct gb_connection *connection,
949 void *data, size_t size)
951 struct gb_operation_msg_hdr header;
952 struct device *dev = &connection->hd->dev;
956 if ((connection->state != GB_CONNECTION_STATE_ENABLED &&
957 connection->state != GB_CONNECTION_STATE_ENABLED_TX) ||
958 gb_connection_is_offloaded(connection)) {
959 dev_warn_ratelimited(dev, "%s: dropping %zu received bytes\n",
960 connection->name, size);
964 if (size < sizeof(header)) {
965 dev_err_ratelimited(dev, "%s: short message received\n",
970 /* Use memcpy as data may be unaligned */
971 memcpy(&header, data, sizeof(header));
972 msg_size = le16_to_cpu(header.size);
973 if (size < msg_size) {
974 dev_err_ratelimited(dev,
975 "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n",
977 le16_to_cpu(header.operation_id),
978 header.type, size, msg_size);
979 return; /* XXX Should still complete operation */
982 operation_id = le16_to_cpu(header.operation_id);
983 if (header.type & GB_MESSAGE_TYPE_RESPONSE)
984 gb_connection_recv_response(connection, operation_id,
985 header.result, data, msg_size);
987 gb_connection_recv_request(connection, operation_id,
988 header.type, data, msg_size);
992 * Cancel an outgoing operation synchronously, and record the given error to
995 void gb_operation_cancel(struct gb_operation *operation, int errno)
997 if (WARN_ON(gb_operation_is_incoming(operation)))
1000 if (gb_operation_result_set(operation, errno)) {
1001 gb_message_cancel(operation->request);
1002 queue_work(gb_operation_completion_wq, &operation->work);
1004 trace_gb_message_cancel_outgoing(operation->request);
1006 atomic_inc(&operation->waiters);
1007 wait_event(gb_operation_cancellation_queue,
1008 !gb_operation_is_active(operation));
1009 atomic_dec(&operation->waiters);
1011 EXPORT_SYMBOL_GPL(gb_operation_cancel);
1014 * Cancel an incoming operation synchronously. Called during connection tear
1017 void gb_operation_cancel_incoming(struct gb_operation *operation, int errno)
1019 if (WARN_ON(!gb_operation_is_incoming(operation)))
1022 if (!gb_operation_is_unidirectional(operation)) {
1024 * Make sure the request handler has submitted the response
1025 * before cancelling it.
1027 flush_work(&operation->work);
1028 if (!gb_operation_result_set(operation, errno))
1029 gb_message_cancel(operation->response);
1031 trace_gb_message_cancel_incoming(operation->response);
1033 atomic_inc(&operation->waiters);
1034 wait_event(gb_operation_cancellation_queue,
1035 !gb_operation_is_active(operation));
1036 atomic_dec(&operation->waiters);
1040 * gb_operation_sync_timeout() - implement a "simple" synchronous operation
1041 * @connection: the Greybus connection to send this to
1042 * @type: the type of operation to send
1043 * @request: pointer to a memory buffer to copy the request from
1044 * @request_size: size of @request
1045 * @response: pointer to a memory buffer to copy the response to
1046 * @response_size: the size of @response.
1047 * @timeout: operation timeout in milliseconds
1049 * This function implements a simple synchronous Greybus operation. It sends
1050 * the provided operation request and waits (sleeps) until the corresponding
1051 * operation response message has been successfully received, or an error
1052 * occurs. @request and @response are buffers to hold the request and response
1053 * data respectively, and if they are not NULL, their size must be specified in
1054 * @request_size and @response_size.
1056 * If a response payload is to come back, and @response is not NULL,
1057 * @response_size number of bytes will be copied into @response if the operation
1060 * If there is an error, the response buffer is left alone.
1062 int gb_operation_sync_timeout(struct gb_connection *connection, int type,
1063 void *request, int request_size,
1064 void *response, int response_size,
1065 unsigned int timeout)
1067 struct gb_operation *operation;
1070 if ((response_size && !response) ||
1071 (request_size && !request))
1074 operation = gb_operation_create(connection, type,
1075 request_size, response_size,
1081 memcpy(operation->request->payload, request, request_size);
1083 ret = gb_operation_request_send_sync_timeout(operation, timeout);
1085 dev_err(&connection->hd->dev,
1086 "%s: synchronous operation of type 0x%02x failed: %d\n",
1087 connection->name, type, ret);
1089 if (response_size) {
1090 memcpy(response, operation->response->payload,
1095 gb_operation_put(operation);
1099 EXPORT_SYMBOL_GPL(gb_operation_sync_timeout);
1102 * gb_operation_unidirectional_timeout() - initiate a unidirectional operation
1103 * @connection: connection to use
1104 * @type: type of operation to send
1105 * @request: memory buffer to copy the request from
1106 * @request_size: size of @request
1107 * @timeout: send timeout in milliseconds
1109 * Initiate a unidirectional operation by sending a request message and
1110 * waiting for it to be acknowledged as sent by the host device.
1112 * Note that successful send of a unidirectional operation does not imply that
1113 * the request as actually reached the remote end of the connection.
1115 int gb_operation_unidirectional_timeout(struct gb_connection *connection,
1116 int type, void *request, int request_size,
1117 unsigned int timeout)
1119 struct gb_operation *operation;
1122 if (request_size && !request)
1125 operation = gb_operation_create_flags(connection, type,
1127 GB_OPERATION_FLAG_UNIDIRECTIONAL,
1133 memcpy(operation->request->payload, request, request_size);
1135 ret = gb_operation_request_send_sync_timeout(operation, timeout);
1137 dev_err(&connection->hd->dev,
1138 "%s: unidirectional operation of type 0x%02x failed: %d\n",
1139 connection->name, type, ret);
1142 gb_operation_put(operation);
1146 EXPORT_SYMBOL_GPL(gb_operation_unidirectional_timeout);
1148 int __init gb_operation_init(void)
1150 gb_message_cache = kmem_cache_create("gb_message_cache",
1151 sizeof(struct gb_message), 0, 0, NULL);
1152 if (!gb_message_cache)
1155 gb_operation_cache = kmem_cache_create("gb_operation_cache",
1156 sizeof(struct gb_operation), 0, 0, NULL);
1157 if (!gb_operation_cache)
1158 goto err_destroy_message_cache;
1160 gb_operation_completion_wq = alloc_workqueue("greybus_completion",
1162 if (!gb_operation_completion_wq)
1163 goto err_destroy_operation_cache;
1167 err_destroy_operation_cache:
1168 kmem_cache_destroy(gb_operation_cache);
1169 gb_operation_cache = NULL;
1170 err_destroy_message_cache:
1171 kmem_cache_destroy(gb_message_cache);
1172 gb_message_cache = NULL;
1177 void gb_operation_exit(void)
1179 destroy_workqueue(gb_operation_completion_wq);
1180 gb_operation_completion_wq = NULL;
1181 kmem_cache_destroy(gb_operation_cache);
1182 gb_operation_cache = NULL;
1183 kmem_cache_destroy(gb_message_cache);
1184 gb_message_cache = NULL;