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>
19 static struct kmem_cache *gb_operation_cache;
20 static struct kmem_cache *gb_message_cache;
22 /* Workqueue to handle Greybus operation completions. */
23 static struct workqueue_struct *gb_operation_workqueue;
25 /* Wait queue for synchronous cancellations. */
26 static DECLARE_WAIT_QUEUE_HEAD(gb_operation_cancellation_queue);
29 * Protects updates to operation->errno.
31 static DEFINE_SPINLOCK(gb_operations_lock);
33 static int gb_operation_response_send(struct gb_operation *operation,
37 * Increment operation active count and add to connection list unless the
38 * connection is going away.
40 * Caller holds operation reference.
42 static int gb_operation_get_active(struct gb_operation *operation)
44 struct gb_connection *connection = operation->connection;
47 spin_lock_irqsave(&connection->lock, flags);
49 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
50 spin_unlock_irqrestore(&connection->lock, flags);
54 if (operation->active++ == 0)
55 list_add_tail(&operation->links, &connection->operations);
57 spin_unlock_irqrestore(&connection->lock, flags);
62 /* Caller holds operation reference. */
63 static void gb_operation_put_active(struct gb_operation *operation)
65 struct gb_connection *connection = operation->connection;
68 spin_lock_irqsave(&connection->lock, flags);
69 if (--operation->active == 0) {
70 list_del(&operation->links);
71 if (atomic_read(&operation->waiters))
72 wake_up(&gb_operation_cancellation_queue);
74 spin_unlock_irqrestore(&connection->lock, flags);
77 static bool gb_operation_is_active(struct gb_operation *operation)
79 struct gb_connection *connection = operation->connection;
83 spin_lock_irqsave(&connection->lock, flags);
84 ret = operation->active;
85 spin_unlock_irqrestore(&connection->lock, flags);
91 * Set an operation's result.
93 * Initially an outgoing operation's errno value is -EBADR.
94 * If no error occurs before sending the request message the only
95 * valid value operation->errno can be set to is -EINPROGRESS,
96 * indicating the request has been (or rather is about to be) sent.
97 * At that point nobody should be looking at the result until the
100 * The first time the result gets set after the request has been
101 * sent, that result "sticks." That is, if two concurrent threads
102 * race to set the result, the first one wins. The return value
103 * tells the caller whether its result was recorded; if not the
104 * caller has nothing more to do.
106 * The result value -EILSEQ is reserved to signal an implementation
107 * error; if it's ever observed, the code performing the request has
108 * done something fundamentally wrong. It is an error to try to set
109 * the result to -EBADR, and attempts to do so result in a warning,
110 * and -EILSEQ is used instead. Similarly, the only valid result
111 * value to set for an operation in initial state is -EINPROGRESS.
112 * Attempts to do otherwise will also record a (successful) -EILSEQ
115 static bool gb_operation_result_set(struct gb_operation *operation, int result)
120 if (result == -EINPROGRESS) {
122 * -EINPROGRESS is used to indicate the request is
123 * in flight. It should be the first result value
124 * set after the initial -EBADR. Issue a warning
125 * and record an implementation error if it's
126 * set at any other time.
128 spin_lock_irqsave(&gb_operations_lock, flags);
129 prev = operation->errno;
131 operation->errno = result;
133 operation->errno = -EILSEQ;
134 spin_unlock_irqrestore(&gb_operations_lock, flags);
135 WARN_ON(prev != -EBADR);
141 * The first result value set after a request has been sent
142 * will be the final result of the operation. Subsequent
143 * attempts to set the result are ignored.
145 * Note that -EBADR is a reserved "initial state" result
146 * value. Attempts to set this value result in a warning,
147 * and the result code is set to -EILSEQ instead.
149 if (WARN_ON(result == -EBADR))
150 result = -EILSEQ; /* Nobody should be setting -EBADR */
152 spin_lock_irqsave(&gb_operations_lock, flags);
153 prev = operation->errno;
154 if (prev == -EINPROGRESS)
155 operation->errno = result; /* First and final result */
156 spin_unlock_irqrestore(&gb_operations_lock, flags);
158 return prev == -EINPROGRESS;
161 int gb_operation_result(struct gb_operation *operation)
163 int result = operation->errno;
165 WARN_ON(result == -EBADR);
166 WARN_ON(result == -EINPROGRESS);
170 EXPORT_SYMBOL_GPL(gb_operation_result);
173 * Looks up an outgoing operation on a connection and returns a refcounted
174 * pointer if found, or NULL otherwise.
176 static struct gb_operation *
177 gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id)
179 struct gb_operation *operation;
183 spin_lock_irqsave(&connection->lock, flags);
184 list_for_each_entry(operation, &connection->operations, links)
185 if (operation->id == operation_id &&
186 !gb_operation_is_incoming(operation)) {
187 gb_operation_get(operation);
191 spin_unlock_irqrestore(&connection->lock, flags);
193 return found ? operation : NULL;
196 static int gb_message_send(struct gb_message *message, gfp_t gfp)
198 struct gb_connection *connection = message->operation->connection;
200 return connection->hd->driver->message_send(connection->hd,
201 connection->hd_cport_id,
207 * Cancel a message we have passed to the host device layer to be sent.
209 static void gb_message_cancel(struct gb_message *message)
211 struct greybus_host_device *hd = message->operation->connection->hd;
213 hd->driver->message_cancel(message);
216 static void gb_operation_request_handle(struct gb_operation *operation)
218 struct gb_protocol *protocol = operation->connection->protocol;
225 if (protocol->request_recv) {
226 status = protocol->request_recv(operation->type, operation);
228 dev_err(&operation->connection->dev,
229 "unexpected incoming request type 0x%02hhx\n",
232 status = -EPROTONOSUPPORT;
235 ret = gb_operation_response_send(operation, status);
237 dev_err(&operation->connection->dev,
238 "failed to send response %d: %d\n",
245 * Process operation work.
247 * For incoming requests, call the protocol request handler. The operation
248 * result should be -EINPROGRESS at this point.
250 * For outgoing requests, the operation result value should have
251 * been set before queueing this. The operation callback function
252 * allows the original requester to know the request has completed
253 * and its result is available.
255 static void gb_operation_work(struct work_struct *work)
257 struct gb_operation *operation;
259 operation = container_of(work, struct gb_operation, work);
261 if (gb_operation_is_incoming(operation))
262 gb_operation_request_handle(operation);
264 operation->callback(operation);
266 gb_operation_put_active(operation);
267 gb_operation_put(operation);
270 static void gb_operation_message_init(struct greybus_host_device *hd,
271 struct gb_message *message, u16 operation_id,
272 size_t payload_size, u8 type)
274 struct gb_operation_msg_hdr *header;
276 header = message->buffer;
278 message->header = header;
279 message->payload = payload_size ? header + 1 : NULL;
280 message->payload_size = payload_size;
283 * The type supplied for incoming message buffers will be
284 * 0x00. Such buffers will be overwritten by arriving data
285 * so there's no need to initialize the message header.
287 if (type != GB_OPERATION_TYPE_INVALID) {
288 u16 message_size = (u16)(sizeof(*header) + payload_size);
291 * For a request, the operation id gets filled in
292 * when the message is sent. For a response, it
293 * will be copied from the request by the caller.
295 * The result field in a request message must be
296 * zero. It will be set just prior to sending for
299 header->size = cpu_to_le16(message_size);
300 header->operation_id = 0;
307 * Allocate a message to be used for an operation request or response.
308 * Both types of message contain a common header. The request message
309 * for an outgoing operation is outbound, as is the response message
310 * for an incoming operation. The message header for an outbound
311 * message is partially initialized here.
313 * The headers for inbound messages don't need to be initialized;
314 * they'll be filled in by arriving data.
316 * Our message buffers have the following layout:
317 * message header \_ these combined are
318 * message payload / the message size
320 static struct gb_message *
321 gb_operation_message_alloc(struct greybus_host_device *hd, u8 type,
322 size_t payload_size, gfp_t gfp_flags)
324 struct gb_message *message;
325 struct gb_operation_msg_hdr *header;
326 size_t message_size = payload_size + sizeof(*header);
328 if (message_size > hd->buffer_size_max) {
329 pr_warn("requested message size too big (%zu > %zu)\n",
330 message_size, hd->buffer_size_max);
334 /* Allocate the message structure and buffer. */
335 message = kmem_cache_zalloc(gb_message_cache, gfp_flags);
339 message->buffer = kzalloc(message_size, gfp_flags);
340 if (!message->buffer)
341 goto err_free_message;
343 /* Initialize the message. Operation id is filled in later. */
344 gb_operation_message_init(hd, message, 0, payload_size, type);
349 kmem_cache_free(gb_message_cache, message);
354 static void gb_operation_message_free(struct gb_message *message)
356 kfree(message->buffer);
357 kmem_cache_free(gb_message_cache, message);
361 * Map an enum gb_operation_status value (which is represented in a
362 * message as a single byte) to an appropriate Linux negative errno.
364 static int gb_operation_status_map(u8 status)
369 case GB_OP_INTERRUPTED:
373 case GB_OP_NO_MEMORY:
375 case GB_OP_PROTOCOL_BAD:
376 return -EPROTONOSUPPORT;
383 case GB_OP_NONEXISTENT:
385 case GB_OP_MALFUNCTION:
387 case GB_OP_UNKNOWN_ERROR:
394 * Map a Linux errno value (from operation->errno) into the value
395 * that should represent it in a response message status sent
396 * over the wire. Returns an enum gb_operation_status value (which
397 * is represented in a message as a single byte).
399 static u8 gb_operation_errno_map(int errno)
403 return GB_OP_SUCCESS;
405 return GB_OP_INTERRUPTED;
407 return GB_OP_TIMEOUT;
409 return GB_OP_NO_MEMORY;
410 case -EPROTONOSUPPORT:
411 return GB_OP_PROTOCOL_BAD;
413 return GB_OP_OVERFLOW; /* Could be underflow too */
415 return GB_OP_INVALID;
419 return GB_OP_MALFUNCTION;
421 return GB_OP_NONEXISTENT;
424 return GB_OP_UNKNOWN_ERROR;
428 bool gb_operation_response_alloc(struct gb_operation *operation,
429 size_t response_size, gfp_t gfp)
431 struct greybus_host_device *hd = operation->connection->hd;
432 struct gb_operation_msg_hdr *request_header;
433 struct gb_message *response;
436 type = operation->type | GB_MESSAGE_TYPE_RESPONSE;
437 response = gb_operation_message_alloc(hd, type, response_size, gfp);
440 response->operation = operation;
443 * Size and type get initialized when the message is
444 * allocated. The errno will be set before sending. All
445 * that's left is the operation id, which we copy from the
446 * request message header (as-is, in little-endian order).
448 request_header = operation->request->header;
449 response->header->operation_id = request_header->operation_id;
450 operation->response = response;
454 EXPORT_SYMBOL_GPL(gb_operation_response_alloc);
457 * Create a Greybus operation to be sent over the given connection.
458 * The request buffer will be big enough for a payload of the given
461 * For outgoing requests, the request message's header will be
462 * initialized with the type of the request and the message size.
463 * Outgoing operations must also specify the response buffer size,
464 * which must be sufficient to hold all expected response data. The
465 * response message header will eventually be overwritten, so there's
466 * no need to initialize it here.
468 * Request messages for incoming operations can arrive in interrupt
469 * context, so they must be allocated with GFP_ATOMIC. In this case
470 * the request buffer will be immediately overwritten, so there is
471 * no need to initialize the message header. Responsibility for
472 * allocating a response buffer lies with the incoming request
473 * handler for a protocol. So we don't allocate that here.
475 * Returns a pointer to the new operation or a null pointer if an
478 static struct gb_operation *
479 gb_operation_create_common(struct gb_connection *connection, u8 type,
480 size_t request_size, size_t response_size,
481 unsigned long op_flags, gfp_t gfp_flags)
483 struct greybus_host_device *hd = connection->hd;
484 struct gb_operation *operation;
486 operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
489 operation->connection = connection;
491 operation->request = gb_operation_message_alloc(hd, type, request_size,
493 if (!operation->request)
495 operation->request->operation = operation;
497 /* Allocate the response buffer for outgoing operations */
498 if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) {
499 if (!gb_operation_response_alloc(operation, response_size,
505 operation->flags = op_flags;
506 operation->type = type;
507 operation->errno = -EBADR; /* Initial value--means "never set" */
509 INIT_WORK(&operation->work, gb_operation_work);
510 init_completion(&operation->completion);
511 kref_init(&operation->kref);
512 atomic_set(&operation->waiters, 0);
517 gb_operation_message_free(operation->request);
519 kmem_cache_free(gb_operation_cache, operation);
525 * Create a new operation associated with the given connection. The
526 * request and response sizes provided are the number of bytes
527 * required to hold the request/response payload only. Both of
528 * these are allowed to be 0. Note that 0x00 is reserved as an
529 * invalid operation type for all protocols, and this is enforced
532 struct gb_operation *gb_operation_create(struct gb_connection *connection,
533 u8 type, size_t request_size,
534 size_t response_size,
537 if (WARN_ON_ONCE(type == GB_OPERATION_TYPE_INVALID))
539 if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE))
540 type &= ~GB_MESSAGE_TYPE_RESPONSE;
542 return gb_operation_create_common(connection, type,
543 request_size, response_size, 0, gfp);
545 EXPORT_SYMBOL_GPL(gb_operation_create);
547 size_t gb_operation_get_payload_size_max(struct gb_connection *connection)
549 struct greybus_host_device *hd = connection->hd;
551 return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr);
553 EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max);
555 static struct gb_operation *
556 gb_operation_create_incoming(struct gb_connection *connection, u16 id,
557 u8 type, void *data, size_t size)
559 struct gb_operation *operation;
561 unsigned long flags = GB_OPERATION_FLAG_INCOMING;
563 /* Caller has made sure we at least have a message header. */
564 request_size = size - sizeof(struct gb_operation_msg_hdr);
567 flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL;
569 operation = gb_operation_create_common(connection, type,
570 request_size, 0, flags, GFP_ATOMIC);
575 memcpy(operation->request->header, data, size);
581 * Get an additional reference on an operation.
583 void gb_operation_get(struct gb_operation *operation)
585 kref_get(&operation->kref);
587 EXPORT_SYMBOL_GPL(gb_operation_get);
590 * Destroy a previously created operation.
592 static void _gb_operation_destroy(struct kref *kref)
594 struct gb_operation *operation;
596 operation = container_of(kref, struct gb_operation, kref);
598 if (operation->response)
599 gb_operation_message_free(operation->response);
600 gb_operation_message_free(operation->request);
602 kmem_cache_free(gb_operation_cache, operation);
606 * Drop a reference on an operation, and destroy it when the last
609 void gb_operation_put(struct gb_operation *operation)
611 if (WARN_ON(!operation))
614 kref_put(&operation->kref, _gb_operation_destroy);
616 EXPORT_SYMBOL_GPL(gb_operation_put);
618 /* Tell the requester we're done */
619 static void gb_operation_sync_callback(struct gb_operation *operation)
621 complete(&operation->completion);
625 * Send an operation request message. The caller has filled in any payload so
626 * the request message is ready to go. The callback function supplied will be
627 * called when the response message has arrived indicating the operation is
628 * complete. In that case, the callback function is responsible for fetching
629 * the result of the operation using gb_operation_result() if desired, and
630 * dropping the initial reference to the operation.
632 int gb_operation_request_send(struct gb_operation *operation,
633 gb_operation_callback callback,
636 struct gb_connection *connection = operation->connection;
637 struct gb_operation_msg_hdr *header;
644 * Record the callback function, which is executed in
645 * non-atomic (workqueue) context when the final result
646 * of an operation has been set.
648 operation->callback = callback;
651 * Assign the operation's id, and store it in the request header.
652 * Zero is a reserved operation id.
654 cycle = (unsigned int)atomic_inc_return(&connection->op_cycle);
655 operation->id = (u16)(cycle % U16_MAX + 1);
656 header = operation->request->header;
657 header->operation_id = cpu_to_le16(operation->id);
659 gb_operation_result_set(operation, -EINPROGRESS);
662 * Get an extra reference on the operation. It'll be dropped when the
663 * operation completes.
665 gb_operation_get(operation);
666 ret = gb_operation_get_active(operation);
670 ret = gb_message_send(operation->request, gfp);
677 gb_operation_put_active(operation);
679 gb_operation_put(operation);
683 EXPORT_SYMBOL_GPL(gb_operation_request_send);
686 * Send a synchronous operation. This function is expected to
687 * block, returning only when the response has arrived, (or when an
688 * error is detected. The return value is the result of the
691 int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
692 unsigned int timeout)
695 unsigned long timeout_jiffies;
697 ret = gb_operation_request_send(operation, gb_operation_sync_callback,
703 timeout_jiffies = msecs_to_jiffies(timeout);
705 timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
707 ret = wait_for_completion_interruptible_timeout(&operation->completion,
710 /* Cancel the operation if interrupted */
711 gb_operation_cancel(operation, -ECANCELED);
712 } else if (ret == 0) {
713 /* Cancel the operation if op timed out */
714 gb_operation_cancel(operation, -ETIMEDOUT);
717 return gb_operation_result(operation);
719 EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout);
722 * Send a response for an incoming operation request. A non-zero
723 * errno indicates a failed operation.
725 * If there is any response payload, the incoming request handler is
726 * responsible for allocating the response message. Otherwise the
727 * it can simply supply the result errno; this function will
728 * allocate the response message if necessary.
730 static int gb_operation_response_send(struct gb_operation *operation,
733 struct gb_connection *connection = operation->connection;
736 if (!operation->response &&
737 !gb_operation_is_unidirectional(operation)) {
738 if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL))
742 /* Record the result */
743 if (!gb_operation_result_set(operation, errno)) {
744 dev_err(&connection->dev, "request result already set\n");
745 return -EIO; /* Shouldn't happen */
748 /* Sender of request does not care about response. */
749 if (gb_operation_is_unidirectional(operation))
752 /* Reference will be dropped when message has been sent. */
753 gb_operation_get(operation);
754 ret = gb_operation_get_active(operation);
758 /* Fill in the response header and send it */
759 operation->response->header->result = gb_operation_errno_map(errno);
761 ret = gb_message_send(operation->response, GFP_KERNEL);
768 gb_operation_put_active(operation);
770 gb_operation_put(operation);
776 * This function is called when a message send request has completed.
778 void greybus_message_sent(struct greybus_host_device *hd,
779 struct gb_message *message, int status)
781 struct gb_operation *operation;
784 * If the message was a response, we just need to drop our
785 * reference to the operation. If an error occurred, report
788 * For requests, if there's no error, there's nothing more
789 * to do until the response arrives. If an error occurred
790 * attempting to send it, record that as the result of
791 * the operation and schedule its completion.
793 operation = message->operation;
794 if (message == operation->response) {
796 dev_err(&operation->connection->dev,
797 "error sending response: %d\n", status);
799 gb_operation_put_active(operation);
800 gb_operation_put(operation);
802 if (gb_operation_result_set(operation, status))
803 queue_work(gb_operation_workqueue, &operation->work);
806 EXPORT_SYMBOL_GPL(greybus_message_sent);
809 * We've received data on a connection, and it doesn't look like a
810 * response, so we assume it's a request.
812 * This is called in interrupt context, so just copy the incoming
813 * data into the request buffer and handle the rest via workqueue.
815 static void gb_connection_recv_request(struct gb_connection *connection,
816 u16 operation_id, u8 type,
817 void *data, size_t size)
819 struct gb_operation *operation;
822 operation = gb_operation_create_incoming(connection, operation_id,
825 dev_err(&connection->dev, "can't create operation\n");
826 return; /* XXX Respond with pre-allocated ENOMEM */
829 ret = gb_operation_get_active(operation);
831 gb_operation_put(operation);
836 * The initial reference to the operation will be dropped when the
837 * request handler returns.
839 if (gb_operation_result_set(operation, -EINPROGRESS))
840 queue_work(gb_operation_workqueue, &operation->work);
844 * We've received data that appears to be an operation response
845 * message. Look up the operation, and record that we've received
848 * This is called in interrupt context, so just copy the incoming
849 * data into the response buffer and handle the rest via workqueue.
851 static void gb_connection_recv_response(struct gb_connection *connection,
852 u16 operation_id, u8 result, void *data, size_t size)
854 struct gb_operation *operation;
855 struct gb_message *message;
856 int errno = gb_operation_status_map(result);
859 operation = gb_operation_find_outgoing(connection, operation_id);
861 dev_err(&connection->dev, "operation not found\n");
865 message = operation->response;
866 message_size = sizeof(*message->header) + message->payload_size;
867 if (!errno && size != message_size) {
868 dev_err(&connection->dev, "bad message size (%zu != %zu)\n",
873 /* We must ignore the payload if a bad status is returned */
875 size = sizeof(*message->header);
877 /* The rest will be handled in work queue context */
878 if (gb_operation_result_set(operation, errno)) {
879 memcpy(message->header, data, size);
880 queue_work(gb_operation_workqueue, &operation->work);
883 gb_operation_put(operation);
887 * Handle data arriving on a connection. As soon as we return the
888 * supplied data buffer will be reused (so unless we do something
889 * with, it's effectively dropped).
891 void gb_connection_recv(struct gb_connection *connection,
892 void *data, size_t size)
894 struct gb_operation_msg_hdr header;
898 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
899 dev_err(&connection->dev, "dropping %zu received bytes\n",
904 if (size < sizeof(header)) {
905 dev_err(&connection->dev, "message too small\n");
909 /* Use memcpy as data may be unaligned */
910 memcpy(&header, data, sizeof(header));
911 msg_size = le16_to_cpu(header.size);
912 if (size < msg_size) {
913 dev_err(&connection->dev,
914 "incomplete message received: 0x%04x (%zu < %zu)\n",
915 le16_to_cpu(header.operation_id), size, msg_size);
916 return; /* XXX Should still complete operation */
919 operation_id = le16_to_cpu(header.operation_id);
920 if (header.type & GB_MESSAGE_TYPE_RESPONSE)
921 gb_connection_recv_response(connection, operation_id,
922 header.result, data, msg_size);
924 gb_connection_recv_request(connection, operation_id,
925 header.type, data, msg_size);
929 * Cancel an outgoing operation synchronously, and record the given error to
932 void gb_operation_cancel(struct gb_operation *operation, int errno)
934 if (WARN_ON(gb_operation_is_incoming(operation)))
937 if (gb_operation_result_set(operation, errno)) {
938 gb_message_cancel(operation->request);
939 queue_work(gb_operation_workqueue, &operation->work);
942 atomic_inc(&operation->waiters);
943 wait_event(gb_operation_cancellation_queue,
944 !gb_operation_is_active(operation));
945 atomic_dec(&operation->waiters);
947 EXPORT_SYMBOL_GPL(gb_operation_cancel);
950 * Cancel an incoming operation synchronously. Called during connection tear
953 void gb_operation_cancel_incoming(struct gb_operation *operation, int errno)
955 if (WARN_ON(!gb_operation_is_incoming(operation)))
958 if (!gb_operation_is_unidirectional(operation)) {
960 * Make sure the request handler has submitted the response
961 * before cancelling it.
963 flush_work(&operation->work);
964 if (!gb_operation_result_set(operation, errno))
965 gb_message_cancel(operation->response);
968 atomic_inc(&operation->waiters);
969 wait_event(gb_operation_cancellation_queue,
970 !gb_operation_is_active(operation));
971 atomic_dec(&operation->waiters);
975 * gb_operation_sync: implement a "simple" synchronous gb operation.
976 * @connection: the Greybus connection to send this to
977 * @type: the type of operation to send
978 * @request: pointer to a memory buffer to copy the request from
979 * @request_size: size of @request
980 * @response: pointer to a memory buffer to copy the response to
981 * @response_size: the size of @response.
982 * @timeout: operation timeout in milliseconds
984 * This function implements a simple synchronous Greybus operation. It sends
985 * the provided operation request and waits (sleeps) until the corresponding
986 * operation response message has been successfully received, or an error
987 * occurs. @request and @response are buffers to hold the request and response
988 * data respectively, and if they are not NULL, their size must be specified in
989 * @request_size and @response_size.
991 * If a response payload is to come back, and @response is not NULL,
992 * @response_size number of bytes will be copied into @response if the operation
995 * If there is an error, the response buffer is left alone.
997 int gb_operation_sync_timeout(struct gb_connection *connection, int type,
998 void *request, int request_size,
999 void *response, int response_size,
1000 unsigned int timeout)
1002 struct gb_operation *operation;
1005 if ((response_size && !response) ||
1006 (request_size && !request))
1009 operation = gb_operation_create(connection, type,
1010 request_size, response_size,
1016 memcpy(operation->request->payload, request, request_size);
1018 ret = gb_operation_request_send_sync_timeout(operation, timeout);
1020 dev_err(&connection->dev, "synchronous operation failed: %d\n",
1023 if (response_size) {
1024 memcpy(response, operation->response->payload,
1028 gb_operation_destroy(operation);
1032 EXPORT_SYMBOL_GPL(gb_operation_sync_timeout);
1034 int __init gb_operation_init(void)
1036 gb_message_cache = kmem_cache_create("gb_message_cache",
1037 sizeof(struct gb_message), 0, 0, NULL);
1038 if (!gb_message_cache)
1041 gb_operation_cache = kmem_cache_create("gb_operation_cache",
1042 sizeof(struct gb_operation), 0, 0, NULL);
1043 if (!gb_operation_cache)
1044 goto err_destroy_message_cache;
1046 gb_operation_workqueue = alloc_workqueue("greybus_operation",
1048 if (!gb_operation_workqueue)
1053 kmem_cache_destroy(gb_operation_cache);
1054 gb_operation_cache = NULL;
1055 err_destroy_message_cache:
1056 kmem_cache_destroy(gb_message_cache);
1057 gb_message_cache = NULL;
1062 void gb_operation_exit(void)
1064 destroy_workqueue(gb_operation_workqueue);
1065 gb_operation_workqueue = NULL;
1066 kmem_cache_destroy(gb_operation_cache);
1067 gb_operation_cache = NULL;
1068 kmem_cache_destroy(gb_message_cache);
1069 gb_message_cache = NULL;