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 switch (connection->state) {
51 case GB_CONNECTION_STATE_ENABLED:
53 case GB_CONNECTION_STATE_ENABLED_TX:
54 if (gb_operation_is_incoming(operation))
61 if (operation->active++ == 0)
62 list_add_tail(&operation->links, &connection->operations);
64 trace_gb_operation_get_active(operation);
66 spin_unlock_irqrestore(&connection->lock, flags);
71 spin_unlock_irqrestore(&connection->lock, flags);
76 /* Caller holds operation reference. */
77 static void gb_operation_put_active(struct gb_operation *operation)
79 struct gb_connection *connection = operation->connection;
82 spin_lock_irqsave(&connection->lock, flags);
84 trace_gb_operation_put_active(operation);
86 if (--operation->active == 0) {
87 list_del(&operation->links);
88 if (atomic_read(&operation->waiters))
89 wake_up(&gb_operation_cancellation_queue);
91 spin_unlock_irqrestore(&connection->lock, flags);
94 static bool gb_operation_is_active(struct gb_operation *operation)
96 struct gb_connection *connection = operation->connection;
100 spin_lock_irqsave(&connection->lock, flags);
101 ret = operation->active;
102 spin_unlock_irqrestore(&connection->lock, flags);
108 * Set an operation's result.
110 * Initially an outgoing operation's errno value is -EBADR.
111 * If no error occurs before sending the request message the only
112 * valid value operation->errno can be set to is -EINPROGRESS,
113 * indicating the request has been (or rather is about to be) sent.
114 * At that point nobody should be looking at the result until the
117 * The first time the result gets set after the request has been
118 * sent, that result "sticks." That is, if two concurrent threads
119 * race to set the result, the first one wins. The return value
120 * tells the caller whether its result was recorded; if not the
121 * caller has nothing more to do.
123 * The result value -EILSEQ is reserved to signal an implementation
124 * error; if it's ever observed, the code performing the request has
125 * done something fundamentally wrong. It is an error to try to set
126 * the result to -EBADR, and attempts to do so result in a warning,
127 * and -EILSEQ is used instead. Similarly, the only valid result
128 * value to set for an operation in initial state is -EINPROGRESS.
129 * Attempts to do otherwise will also record a (successful) -EILSEQ
132 static bool gb_operation_result_set(struct gb_operation *operation, int result)
137 if (result == -EINPROGRESS) {
139 * -EINPROGRESS is used to indicate the request is
140 * in flight. It should be the first result value
141 * set after the initial -EBADR. Issue a warning
142 * and record an implementation error if it's
143 * set at any other time.
145 spin_lock_irqsave(&gb_operations_lock, flags);
146 prev = operation->errno;
148 operation->errno = result;
150 operation->errno = -EILSEQ;
151 spin_unlock_irqrestore(&gb_operations_lock, flags);
152 WARN_ON(prev != -EBADR);
158 * The first result value set after a request has been sent
159 * will be the final result of the operation. Subsequent
160 * attempts to set the result are ignored.
162 * Note that -EBADR is a reserved "initial state" result
163 * value. Attempts to set this value result in a warning,
164 * and the result code is set to -EILSEQ instead.
166 if (WARN_ON(result == -EBADR))
167 result = -EILSEQ; /* Nobody should be setting -EBADR */
169 spin_lock_irqsave(&gb_operations_lock, flags);
170 prev = operation->errno;
171 if (prev == -EINPROGRESS)
172 operation->errno = result; /* First and final result */
173 spin_unlock_irqrestore(&gb_operations_lock, flags);
175 return prev == -EINPROGRESS;
178 int gb_operation_result(struct gb_operation *operation)
180 int result = operation->errno;
182 WARN_ON(result == -EBADR);
183 WARN_ON(result == -EINPROGRESS);
187 EXPORT_SYMBOL_GPL(gb_operation_result);
190 * Looks up an outgoing operation on a connection and returns a refcounted
191 * pointer if found, or NULL otherwise.
193 static struct gb_operation *
194 gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id)
196 struct gb_operation *operation;
200 spin_lock_irqsave(&connection->lock, flags);
201 list_for_each_entry(operation, &connection->operations, links)
202 if (operation->id == operation_id &&
203 !gb_operation_is_incoming(operation)) {
204 gb_operation_get(operation);
208 spin_unlock_irqrestore(&connection->lock, flags);
210 return found ? operation : NULL;
213 static int gb_message_send(struct gb_message *message, gfp_t gfp)
215 struct gb_connection *connection = message->operation->connection;
217 trace_gb_message_send(message);
218 return connection->hd->driver->message_send(connection->hd,
219 connection->hd_cport_id,
225 * Cancel a message we have passed to the host device layer to be sent.
227 static void gb_message_cancel(struct gb_message *message)
229 struct gb_host_device *hd = message->operation->connection->hd;
231 hd->driver->message_cancel(message);
234 static void gb_operation_request_handle(struct gb_operation *operation)
236 struct gb_connection *connection = operation->connection;
240 if (connection->handler) {
241 status = connection->handler(operation);
243 dev_err(&connection->hd->dev,
244 "%s: unexpected incoming request of type 0x%02x\n",
245 connection->name, operation->type);
247 status = -EPROTONOSUPPORT;
250 ret = gb_operation_response_send(operation, status);
252 dev_err(&connection->hd->dev,
253 "%s: failed to send response %d for type 0x%02x: %d\n",
254 connection->name, status, operation->type, ret);
260 * Process operation work.
262 * For incoming requests, call the protocol request handler. The operation
263 * result should be -EINPROGRESS at this point.
265 * For outgoing requests, the operation result value should have
266 * been set before queueing this. The operation callback function
267 * allows the original requester to know the request has completed
268 * and its result is available.
270 static void gb_operation_work(struct work_struct *work)
272 struct gb_operation *operation;
274 operation = container_of(work, struct gb_operation, work);
276 if (gb_operation_is_incoming(operation))
277 gb_operation_request_handle(operation);
279 operation->callback(operation);
281 gb_operation_put_active(operation);
282 gb_operation_put(operation);
285 static void gb_operation_message_init(struct gb_host_device *hd,
286 struct gb_message *message, u16 operation_id,
287 size_t payload_size, u8 type)
289 struct gb_operation_msg_hdr *header;
291 header = message->buffer;
293 message->header = header;
294 message->payload = payload_size ? header + 1 : NULL;
295 message->payload_size = payload_size;
298 * The type supplied for incoming message buffers will be
299 * GB_REQUEST_TYPE_INVALID. Such buffers will be overwritten by
300 * arriving data so there's no need to initialize the message header.
302 if (type != GB_REQUEST_TYPE_INVALID) {
303 u16 message_size = (u16)(sizeof(*header) + payload_size);
306 * For a request, the operation id gets filled in
307 * when the message is sent. For a response, it
308 * will be copied from the request by the caller.
310 * The result field in a request message must be
311 * zero. It will be set just prior to sending for
314 header->size = cpu_to_le16(message_size);
315 header->operation_id = 0;
322 * Allocate a message to be used for an operation request or response.
323 * Both types of message contain a common header. The request message
324 * for an outgoing operation is outbound, as is the response message
325 * for an incoming operation. The message header for an outbound
326 * message is partially initialized here.
328 * The headers for inbound messages don't need to be initialized;
329 * they'll be filled in by arriving data.
331 * Our message buffers have the following layout:
332 * message header \_ these combined are
333 * message payload / the message size
335 static struct gb_message *
336 gb_operation_message_alloc(struct gb_host_device *hd, u8 type,
337 size_t payload_size, gfp_t gfp_flags)
339 struct gb_message *message;
340 struct gb_operation_msg_hdr *header;
341 size_t message_size = payload_size + sizeof(*header);
343 if (message_size > hd->buffer_size_max) {
344 dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n",
345 message_size, hd->buffer_size_max);
349 /* Allocate the message structure and buffer. */
350 message = kmem_cache_zalloc(gb_message_cache, gfp_flags);
354 message->buffer = kzalloc(message_size, gfp_flags);
355 if (!message->buffer)
356 goto err_free_message;
358 /* Initialize the message. Operation id is filled in later. */
359 gb_operation_message_init(hd, message, 0, payload_size, type);
364 kmem_cache_free(gb_message_cache, message);
369 static void gb_operation_message_free(struct gb_message *message)
371 kfree(message->buffer);
372 kmem_cache_free(gb_message_cache, message);
376 * Map an enum gb_operation_status value (which is represented in a
377 * message as a single byte) to an appropriate Linux negative errno.
379 static int gb_operation_status_map(u8 status)
384 case GB_OP_INTERRUPTED:
388 case GB_OP_NO_MEMORY:
390 case GB_OP_PROTOCOL_BAD:
391 return -EPROTONOSUPPORT;
398 case GB_OP_NONEXISTENT:
400 case GB_OP_MALFUNCTION:
402 case GB_OP_UNKNOWN_ERROR:
409 * Map a Linux errno value (from operation->errno) into the value
410 * that should represent it in a response message status sent
411 * over the wire. Returns an enum gb_operation_status value (which
412 * is represented in a message as a single byte).
414 static u8 gb_operation_errno_map(int errno)
418 return GB_OP_SUCCESS;
420 return GB_OP_INTERRUPTED;
422 return GB_OP_TIMEOUT;
424 return GB_OP_NO_MEMORY;
425 case -EPROTONOSUPPORT:
426 return GB_OP_PROTOCOL_BAD;
428 return GB_OP_OVERFLOW; /* Could be underflow too */
430 return GB_OP_INVALID;
434 return GB_OP_MALFUNCTION;
436 return GB_OP_NONEXISTENT;
439 return GB_OP_UNKNOWN_ERROR;
443 bool gb_operation_response_alloc(struct gb_operation *operation,
444 size_t response_size, gfp_t gfp)
446 struct gb_host_device *hd = operation->connection->hd;
447 struct gb_operation_msg_hdr *request_header;
448 struct gb_message *response;
451 type = operation->type | GB_MESSAGE_TYPE_RESPONSE;
452 response = gb_operation_message_alloc(hd, type, response_size, gfp);
455 response->operation = operation;
458 * Size and type get initialized when the message is
459 * allocated. The errno will be set before sending. All
460 * that's left is the operation id, which we copy from the
461 * request message header (as-is, in little-endian order).
463 request_header = operation->request->header;
464 response->header->operation_id = request_header->operation_id;
465 operation->response = response;
469 EXPORT_SYMBOL_GPL(gb_operation_response_alloc);
472 * Create a Greybus operation to be sent over the given connection.
473 * The request buffer will be big enough for a payload of the given
476 * For outgoing requests, the request message's header will be
477 * initialized with the type of the request and the message size.
478 * Outgoing operations must also specify the response buffer size,
479 * which must be sufficient to hold all expected response data. The
480 * response message header will eventually be overwritten, so there's
481 * no need to initialize it here.
483 * Request messages for incoming operations can arrive in interrupt
484 * context, so they must be allocated with GFP_ATOMIC. In this case
485 * the request buffer will be immediately overwritten, so there is
486 * no need to initialize the message header. Responsibility for
487 * allocating a response buffer lies with the incoming request
488 * handler for a protocol. So we don't allocate that here.
490 * Returns a pointer to the new operation or a null pointer if an
493 static struct gb_operation *
494 gb_operation_create_common(struct gb_connection *connection, u8 type,
495 size_t request_size, size_t response_size,
496 unsigned long op_flags, gfp_t gfp_flags)
498 struct gb_host_device *hd = connection->hd;
499 struct gb_operation *operation;
501 operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
504 operation->connection = connection;
506 operation->request = gb_operation_message_alloc(hd, type, request_size,
508 if (!operation->request)
510 operation->request->operation = operation;
512 /* Allocate the response buffer for outgoing operations */
513 if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) {
514 if (!gb_operation_response_alloc(operation, response_size,
520 operation->flags = op_flags;
521 operation->type = type;
522 operation->errno = -EBADR; /* Initial value--means "never set" */
524 INIT_WORK(&operation->work, gb_operation_work);
525 init_completion(&operation->completion);
526 kref_init(&operation->kref);
527 atomic_set(&operation->waiters, 0);
532 gb_operation_message_free(operation->request);
534 kmem_cache_free(gb_operation_cache, operation);
540 * Create a new operation associated with the given connection. The
541 * request and response sizes provided are the number of bytes
542 * required to hold the request/response payload only. Both of
543 * these are allowed to be 0. Note that 0x00 is reserved as an
544 * invalid operation type for all protocols, and this is enforced
547 struct gb_operation *
548 gb_operation_create_flags(struct gb_connection *connection,
549 u8 type, size_t request_size,
550 size_t response_size, unsigned long flags,
553 struct gb_operation *operation;
555 if (WARN_ON_ONCE(type == GB_REQUEST_TYPE_INVALID))
557 if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE))
558 type &= ~GB_MESSAGE_TYPE_RESPONSE;
560 if (WARN_ON_ONCE(flags & ~GB_OPERATION_FLAG_USER_MASK))
561 flags &= GB_OPERATION_FLAG_USER_MASK;
563 operation = gb_operation_create_common(connection, type,
564 request_size, response_size,
567 trace_gb_operation_create(operation);
572 EXPORT_SYMBOL_GPL(gb_operation_create_flags);
574 size_t gb_operation_get_payload_size_max(struct gb_connection *connection)
576 struct gb_host_device *hd = connection->hd;
578 return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr);
580 EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max);
582 static struct gb_operation *
583 gb_operation_create_incoming(struct gb_connection *connection, u16 id,
584 u8 type, void *data, size_t size)
586 struct gb_operation *operation;
588 unsigned long flags = GB_OPERATION_FLAG_INCOMING;
590 /* Caller has made sure we at least have a message header. */
591 request_size = size - sizeof(struct gb_operation_msg_hdr);
594 flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL;
596 operation = gb_operation_create_common(connection, type,
598 GB_REQUEST_TYPE_INVALID,
604 memcpy(operation->request->header, data, size);
605 trace_gb_operation_create_incoming(operation);
611 * Get an additional reference on an operation.
613 void gb_operation_get(struct gb_operation *operation)
615 kref_get(&operation->kref);
617 EXPORT_SYMBOL_GPL(gb_operation_get);
620 * Destroy a previously created operation.
622 static void _gb_operation_destroy(struct kref *kref)
624 struct gb_operation *operation;
626 operation = container_of(kref, struct gb_operation, kref);
628 trace_gb_operation_destroy(operation);
630 if (operation->response)
631 gb_operation_message_free(operation->response);
632 gb_operation_message_free(operation->request);
634 kmem_cache_free(gb_operation_cache, operation);
638 * Drop a reference on an operation, and destroy it when the last
641 void gb_operation_put(struct gb_operation *operation)
643 if (WARN_ON(!operation))
646 kref_put(&operation->kref, _gb_operation_destroy);
648 EXPORT_SYMBOL_GPL(gb_operation_put);
650 /* Tell the requester we're done */
651 static void gb_operation_sync_callback(struct gb_operation *operation)
653 complete(&operation->completion);
657 * gb_operation_request_send() - send an operation request message
658 * @operation: the operation to initiate
659 * @callback: the operation completion callback
660 * @gfp: the memory flags to use for any allocations
662 * The caller has filled in any payload so the request message is ready to go.
663 * The callback function supplied will be called when the response message has
664 * arrived, a unidirectional request has been sent, or the operation is
665 * cancelled, indicating that the operation is complete. The callback function
666 * can fetch the result of the operation using gb_operation_result() if
669 * Return: 0 if the request was successfully queued in the host-driver queues,
670 * or a negative errno.
672 int gb_operation_request_send(struct gb_operation *operation,
673 gb_operation_callback callback,
676 struct gb_connection *connection = operation->connection;
677 struct gb_operation_msg_hdr *header;
681 if (gb_connection_is_offloaded(connection))
688 * Record the callback function, which is executed in
689 * non-atomic (workqueue) context when the final result
690 * of an operation has been set.
692 operation->callback = callback;
695 * Assign the operation's id, and store it in the request header.
696 * Zero is a reserved operation id for unidirectional operations.
698 if (gb_operation_is_unidirectional(operation)) {
701 cycle = (unsigned int)atomic_inc_return(&connection->op_cycle);
702 operation->id = (u16)(cycle % U16_MAX + 1);
705 header = operation->request->header;
706 header->operation_id = cpu_to_le16(operation->id);
708 gb_operation_result_set(operation, -EINPROGRESS);
711 * Get an extra reference on the operation. It'll be dropped when the
712 * operation completes.
714 gb_operation_get(operation);
715 ret = gb_operation_get_active(operation);
719 ret = gb_message_send(operation->request, gfp);
726 gb_operation_put_active(operation);
728 gb_operation_put(operation);
732 EXPORT_SYMBOL_GPL(gb_operation_request_send);
735 * Send a synchronous operation. This function is expected to
736 * block, returning only when the response has arrived, (or when an
737 * error is detected. The return value is the result of the
740 int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
741 unsigned int timeout)
744 unsigned long timeout_jiffies;
746 ret = gb_operation_request_send(operation, gb_operation_sync_callback,
752 timeout_jiffies = msecs_to_jiffies(timeout);
754 timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
756 ret = wait_for_completion_interruptible_timeout(&operation->completion,
759 /* Cancel the operation if interrupted */
760 gb_operation_cancel(operation, -ECANCELED);
761 } else if (ret == 0) {
762 /* Cancel the operation if op timed out */
763 gb_operation_cancel(operation, -ETIMEDOUT);
766 return gb_operation_result(operation);
768 EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout);
771 * Send a response for an incoming operation request. A non-zero
772 * errno indicates a failed operation.
774 * If there is any response payload, the incoming request handler is
775 * responsible for allocating the response message. Otherwise the
776 * it can simply supply the result errno; this function will
777 * allocate the response message if necessary.
779 static int gb_operation_response_send(struct gb_operation *operation,
782 struct gb_connection *connection = operation->connection;
785 if (!operation->response &&
786 !gb_operation_is_unidirectional(operation)) {
787 if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL))
791 /* Record the result */
792 if (!gb_operation_result_set(operation, errno)) {
793 dev_err(&connection->hd->dev, "request result already set\n");
794 return -EIO; /* Shouldn't happen */
797 /* Sender of request does not care about response. */
798 if (gb_operation_is_unidirectional(operation))
801 /* Reference will be dropped when message has been sent. */
802 gb_operation_get(operation);
803 ret = gb_operation_get_active(operation);
807 /* Fill in the response header and send it */
808 operation->response->header->result = gb_operation_errno_map(errno);
810 ret = gb_message_send(operation->response, GFP_KERNEL);
817 gb_operation_put_active(operation);
819 gb_operation_put(operation);
825 * This function is called when a message send request has completed.
827 void greybus_message_sent(struct gb_host_device *hd,
828 struct gb_message *message, int status)
830 struct gb_operation *operation = message->operation;
831 struct gb_connection *connection = operation->connection;
834 * If the message was a response, we just need to drop our
835 * reference to the operation. If an error occurred, report
838 * For requests, if there's no error and the operation in not
839 * unidirectional, there's nothing more to do until the response
840 * arrives. If an error occurred attempting to send it, or if the
841 * operation is unidrectional, record the result of the operation and
842 * schedule its completion.
844 if (message == operation->response) {
846 dev_err(&connection->hd->dev,
847 "%s: error sending response 0x%02x: %d\n",
848 connection->name, operation->type, status);
851 gb_operation_put_active(operation);
852 gb_operation_put(operation);
853 } else if (status || gb_operation_is_unidirectional(operation)) {
854 if (gb_operation_result_set(operation, status)) {
855 queue_work(gb_operation_completion_wq,
860 EXPORT_SYMBOL_GPL(greybus_message_sent);
863 * We've received data on a connection, and it doesn't look like a
864 * response, so we assume it's a request.
866 * This is called in interrupt context, so just copy the incoming
867 * data into the request buffer and handle the rest via workqueue.
869 static void gb_connection_recv_request(struct gb_connection *connection,
870 u16 operation_id, u8 type,
871 void *data, size_t size)
873 struct gb_operation *operation;
876 operation = gb_operation_create_incoming(connection, operation_id,
879 dev_err(&connection->hd->dev,
880 "%s: can't create incoming operation\n",
885 ret = gb_operation_get_active(operation);
887 gb_operation_put(operation);
890 trace_gb_message_recv_request(operation->request);
893 * The initial reference to the operation will be dropped when the
894 * request handler returns.
896 if (gb_operation_result_set(operation, -EINPROGRESS))
897 queue_work(connection->wq, &operation->work);
901 * We've received data that appears to be an operation response
902 * message. Look up the operation, and record that we've received
905 * This is called in interrupt context, so just copy the incoming
906 * data into the response buffer and handle the rest via workqueue.
908 static void gb_connection_recv_response(struct gb_connection *connection,
909 u16 operation_id, u8 result, void *data, size_t size)
911 struct gb_operation_msg_hdr *header;
912 struct gb_operation *operation;
913 struct gb_message *message;
914 int errno = gb_operation_status_map(result);
918 dev_err_ratelimited(&connection->hd->dev,
919 "%s: invalid response id 0 received\n",
924 operation = gb_operation_find_outgoing(connection, operation_id);
926 dev_err_ratelimited(&connection->hd->dev,
927 "%s: unexpected response id 0x%04x received\n",
928 connection->name, operation_id);
932 message = operation->response;
933 header = message->header;
934 message_size = sizeof(*header) + message->payload_size;
935 if (!errno && size > message_size) {
936 dev_err_ratelimited(&connection->hd->dev,
937 "%s: malformed response 0x%02x received (%zu > %zu)\n",
938 connection->name, header->type,
941 } else if (!errno && size < message_size) {
942 if (gb_operation_short_response_allowed(operation)) {
943 message->payload_size = size - sizeof(*header);
945 dev_err_ratelimited(&connection->hd->dev,
946 "%s: short response 0x%02x received (%zu < %zu)\n",
947 connection->name, header->type,
952 trace_gb_message_recv_response(operation->response);
954 /* We must ignore the payload if a bad status is returned */
956 size = sizeof(*header);
958 /* The rest will be handled in work queue context */
959 if (gb_operation_result_set(operation, errno)) {
960 memcpy(header, data, size);
961 queue_work(gb_operation_completion_wq, &operation->work);
964 gb_operation_put(operation);
968 * Handle data arriving on a connection. As soon as we return the
969 * supplied data buffer will be reused (so unless we do something
970 * with, it's effectively dropped).
972 void gb_connection_recv(struct gb_connection *connection,
973 void *data, size_t size)
975 struct gb_operation_msg_hdr header;
976 struct device *dev = &connection->hd->dev;
980 if (connection->state == GB_CONNECTION_STATE_DISABLED ||
981 gb_connection_is_offloaded(connection)) {
982 dev_warn_ratelimited(dev, "%s: dropping %zu received bytes\n",
983 connection->name, size);
987 if (size < sizeof(header)) {
988 dev_err_ratelimited(dev, "%s: short message received\n",
993 /* Use memcpy as data may be unaligned */
994 memcpy(&header, data, sizeof(header));
995 msg_size = le16_to_cpu(header.size);
996 if (size < msg_size) {
997 dev_err_ratelimited(dev,
998 "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n",
1000 le16_to_cpu(header.operation_id),
1001 header.type, size, msg_size);
1002 return; /* XXX Should still complete operation */
1005 operation_id = le16_to_cpu(header.operation_id);
1006 if (header.type & GB_MESSAGE_TYPE_RESPONSE)
1007 gb_connection_recv_response(connection, operation_id,
1008 header.result, data, msg_size);
1010 gb_connection_recv_request(connection, operation_id,
1011 header.type, data, msg_size);
1015 * Cancel an outgoing operation synchronously, and record the given error to
1018 void gb_operation_cancel(struct gb_operation *operation, int errno)
1020 if (WARN_ON(gb_operation_is_incoming(operation)))
1023 if (gb_operation_result_set(operation, errno)) {
1024 gb_message_cancel(operation->request);
1025 queue_work(gb_operation_completion_wq, &operation->work);
1027 trace_gb_message_cancel_outgoing(operation->request);
1029 atomic_inc(&operation->waiters);
1030 wait_event(gb_operation_cancellation_queue,
1031 !gb_operation_is_active(operation));
1032 atomic_dec(&operation->waiters);
1034 EXPORT_SYMBOL_GPL(gb_operation_cancel);
1037 * Cancel an incoming operation synchronously. Called during connection tear
1040 void gb_operation_cancel_incoming(struct gb_operation *operation, int errno)
1042 if (WARN_ON(!gb_operation_is_incoming(operation)))
1045 if (!gb_operation_is_unidirectional(operation)) {
1047 * Make sure the request handler has submitted the response
1048 * before cancelling it.
1050 flush_work(&operation->work);
1051 if (!gb_operation_result_set(operation, errno))
1052 gb_message_cancel(operation->response);
1054 trace_gb_message_cancel_incoming(operation->response);
1056 atomic_inc(&operation->waiters);
1057 wait_event(gb_operation_cancellation_queue,
1058 !gb_operation_is_active(operation));
1059 atomic_dec(&operation->waiters);
1063 * gb_operation_sync_timeout() - implement a "simple" synchronous operation
1064 * @connection: the Greybus connection to send this to
1065 * @type: the type of operation to send
1066 * @request: pointer to a memory buffer to copy the request from
1067 * @request_size: size of @request
1068 * @response: pointer to a memory buffer to copy the response to
1069 * @response_size: the size of @response.
1070 * @timeout: operation timeout in milliseconds
1072 * This function implements a simple synchronous Greybus operation. It sends
1073 * the provided operation request and waits (sleeps) until the corresponding
1074 * operation response message has been successfully received, or an error
1075 * occurs. @request and @response are buffers to hold the request and response
1076 * data respectively, and if they are not NULL, their size must be specified in
1077 * @request_size and @response_size.
1079 * If a response payload is to come back, and @response is not NULL,
1080 * @response_size number of bytes will be copied into @response if the operation
1083 * If there is an error, the response buffer is left alone.
1085 int gb_operation_sync_timeout(struct gb_connection *connection, int type,
1086 void *request, int request_size,
1087 void *response, int response_size,
1088 unsigned int timeout)
1090 struct gb_operation *operation;
1093 if ((response_size && !response) ||
1094 (request_size && !request))
1097 operation = gb_operation_create(connection, type,
1098 request_size, response_size,
1104 memcpy(operation->request->payload, request, request_size);
1106 ret = gb_operation_request_send_sync_timeout(operation, timeout);
1108 dev_err(&connection->hd->dev,
1109 "%s: synchronous operation of type 0x%02x failed: %d\n",
1110 connection->name, type, ret);
1112 if (response_size) {
1113 memcpy(response, operation->response->payload,
1118 gb_operation_put(operation);
1122 EXPORT_SYMBOL_GPL(gb_operation_sync_timeout);
1125 * gb_operation_unidirectional_timeout() - initiate a unidirectional operation
1126 * @connection: connection to use
1127 * @type: type of operation to send
1128 * @request: memory buffer to copy the request from
1129 * @request_size: size of @request
1130 * @timeout: send timeout in milliseconds
1132 * Initiate a unidirectional operation by sending a request message and
1133 * waiting for it to be acknowledged as sent by the host device.
1135 * Note that successful send of a unidirectional operation does not imply that
1136 * the request as actually reached the remote end of the connection.
1138 int gb_operation_unidirectional_timeout(struct gb_connection *connection,
1139 int type, void *request, int request_size,
1140 unsigned int timeout)
1142 struct gb_operation *operation;
1145 if (request_size && !request)
1148 operation = gb_operation_create_flags(connection, type,
1150 GB_OPERATION_FLAG_UNIDIRECTIONAL,
1156 memcpy(operation->request->payload, request, request_size);
1158 ret = gb_operation_request_send_sync_timeout(operation, timeout);
1160 dev_err(&connection->hd->dev,
1161 "%s: unidirectional operation of type 0x%02x failed: %d\n",
1162 connection->name, type, ret);
1165 gb_operation_put(operation);
1169 EXPORT_SYMBOL_GPL(gb_operation_unidirectional_timeout);
1171 int __init gb_operation_init(void)
1173 gb_message_cache = kmem_cache_create("gb_message_cache",
1174 sizeof(struct gb_message), 0, 0, NULL);
1175 if (!gb_message_cache)
1178 gb_operation_cache = kmem_cache_create("gb_operation_cache",
1179 sizeof(struct gb_operation), 0, 0, NULL);
1180 if (!gb_operation_cache)
1181 goto err_destroy_message_cache;
1183 gb_operation_completion_wq = alloc_workqueue("greybus_completion",
1185 if (!gb_operation_completion_wq)
1186 goto err_destroy_operation_cache;
1190 err_destroy_operation_cache:
1191 kmem_cache_destroy(gb_operation_cache);
1192 gb_operation_cache = NULL;
1193 err_destroy_message_cache:
1194 kmem_cache_destroy(gb_message_cache);
1195 gb_message_cache = NULL;
1200 void gb_operation_exit(void)
1202 destroy_workqueue(gb_operation_completion_wq);
1203 gb_operation_completion_wq = NULL;
1204 kmem_cache_destroy(gb_operation_cache);
1205 gb_operation_cache = NULL;
1206 kmem_cache_destroy(gb_message_cache);
1207 gb_message_cache = NULL;