2014-10-02 02:54:15 +00:00
|
|
|
/*
|
|
|
|
* Greybus operations
|
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
|
|
|
|
#include "greybus.h"
|
|
|
|
|
2014-10-16 11:35:31 +00:00
|
|
|
/*
|
|
|
|
* The top bit of the type in an operation message header indicates
|
|
|
|
* whether the message is a request (bit clear) or response (bit set)
|
|
|
|
*/
|
|
|
|
#define GB_OPERATION_TYPE_RESPONSE 0x80
|
|
|
|
|
2014-10-27 07:40:09 +00:00
|
|
|
#define OPERATION_TIMEOUT_DEFAULT 1000 /* milliseconds */
|
2014-10-22 07:04:28 +00:00
|
|
|
|
2014-10-16 11:35:33 +00:00
|
|
|
/*
|
|
|
|
* XXX This needs to be coordinated with host driver parameters
|
2014-11-18 00:08:32 +00:00
|
|
|
* XXX May need to reduce to allow for message header within a page
|
2014-10-16 11:35:33 +00:00
|
|
|
*/
|
|
|
|
#define GB_OPERATION_MESSAGE_SIZE_MAX 4096
|
|
|
|
|
2014-10-20 15:27:56 +00:00
|
|
|
static struct kmem_cache *gb_operation_cache;
|
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
/* Workqueue to handle Greybus operation completions. */
|
|
|
|
static struct workqueue_struct *gb_operation_recv_workqueue;
|
|
|
|
|
2014-10-02 02:54:15 +00:00
|
|
|
/*
|
|
|
|
* All operation messages (both requests and responses) begin with
|
|
|
|
* a common header that encodes the size of the data (header
|
|
|
|
* included). This header also contains a unique identifier, which
|
|
|
|
* is used to keep track of in-flight operations. Finally, the
|
|
|
|
* header contains a operation type field, whose interpretation is
|
|
|
|
* dependent on what type of device lies on the other end of the
|
|
|
|
* connection. Response messages are distinguished from request
|
|
|
|
* messages by setting the high bit (0x80) in the operation type
|
|
|
|
* value.
|
|
|
|
*
|
|
|
|
* The wire format for all numeric fields in the header is little
|
|
|
|
* endian. Any operation-specific data begins immediately after the
|
|
|
|
* header, and is 64-bit aligned.
|
|
|
|
*/
|
|
|
|
struct gb_operation_msg_hdr {
|
|
|
|
__le16 size; /* Size in bytes of header + payload */
|
|
|
|
__le16 id; /* Operation unique id */
|
|
|
|
__u8 type; /* E.g GB_I2C_TYPE_* or GB_GPIO_TYPE_* */
|
|
|
|
/* 3 bytes pad, must be zero (ignore when read) */
|
|
|
|
} __aligned(sizeof(u64));
|
|
|
|
|
|
|
|
/* XXX Could be per-host device, per-module, or even per-connection */
|
|
|
|
static DEFINE_SPINLOCK(gb_operations_lock);
|
|
|
|
|
2014-11-12 21:17:53 +00:00
|
|
|
static void gb_pending_operation_insert(struct gb_operation *operation)
|
2014-10-16 11:35:32 +00:00
|
|
|
{
|
|
|
|
struct gb_connection *connection = operation->connection;
|
|
|
|
struct gb_operation_msg_hdr *header;
|
|
|
|
|
2014-11-12 21:17:54 +00:00
|
|
|
/*
|
|
|
|
* Assign the operation's id and move it into its
|
|
|
|
* connection's pending list.
|
2014-10-16 11:35:32 +00:00
|
|
|
*/
|
|
|
|
spin_lock_irq(&gb_operations_lock);
|
2014-11-12 21:17:54 +00:00
|
|
|
operation->id = ++connection->op_cycle;
|
2014-11-12 21:17:53 +00:00
|
|
|
list_move_tail(&operation->links, &connection->pending);
|
2014-10-16 11:35:32 +00:00
|
|
|
spin_unlock_irq(&gb_operations_lock);
|
2014-11-12 21:17:54 +00:00
|
|
|
|
|
|
|
/* Store the operation id in the request header */
|
2014-11-18 00:08:35 +00:00
|
|
|
header = operation->request.gbuf.transfer_buffer;
|
2014-11-12 21:17:54 +00:00
|
|
|
header->id = cpu_to_le16(operation->id);
|
2014-10-16 11:35:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 21:17:53 +00:00
|
|
|
static void gb_pending_operation_remove(struct gb_operation *operation)
|
2014-10-16 11:35:32 +00:00
|
|
|
{
|
2014-10-22 07:04:28 +00:00
|
|
|
struct gb_connection *connection = operation->connection;
|
|
|
|
|
2014-10-27 07:40:09 +00:00
|
|
|
/* Take us off of the list of pending operations */
|
2014-10-16 11:35:32 +00:00
|
|
|
spin_lock_irq(&gb_operations_lock);
|
2014-11-12 21:17:53 +00:00
|
|
|
list_move_tail(&operation->links, &connection->operations);
|
2014-10-16 11:35:32 +00:00
|
|
|
spin_unlock_irq(&gb_operations_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct gb_operation *
|
2014-11-12 21:17:53 +00:00
|
|
|
gb_pending_operation_find(struct gb_connection *connection, u16 id)
|
2014-10-16 11:35:32 +00:00
|
|
|
{
|
2014-11-12 21:17:53 +00:00
|
|
|
struct gb_operation *operation;
|
2014-10-16 11:35:32 +00:00
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
spin_lock_irq(&gb_operations_lock);
|
2014-11-12 21:17:53 +00:00
|
|
|
list_for_each_entry(operation, &connection->pending, links)
|
|
|
|
if (operation->id == id) {
|
2014-10-16 11:35:32 +00:00
|
|
|
found = true;
|
2014-11-12 21:17:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-10-16 11:35:32 +00:00
|
|
|
spin_unlock_irq(&gb_operations_lock);
|
|
|
|
|
|
|
|
return found ? operation : NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-18 19:26:48 +00:00
|
|
|
static int gb_message_send(struct gb_message *message, gfp_t gfp_mask)
|
2014-11-18 00:08:37 +00:00
|
|
|
{
|
2014-11-18 19:26:48 +00:00
|
|
|
struct greybus_host_device *hd = message->gbuf.hd;
|
|
|
|
|
|
|
|
message->gbuf.status = -EINPROGRESS;
|
|
|
|
message->gbuf.hcd_data = hd->driver->buffer_send(hd,
|
|
|
|
message->gbuf.dest_cport_id,
|
|
|
|
message->gbuf.transfer_buffer,
|
|
|
|
message->gbuf.transfer_buffer_length,
|
|
|
|
gfp_mask);
|
|
|
|
if (IS_ERR(message->gbuf.hcd_data)) {
|
|
|
|
message->gbuf.status = PTR_ERR(message->gbuf.hcd_data);
|
|
|
|
message->gbuf.hcd_data = NULL;
|
|
|
|
|
|
|
|
return message->gbuf.status;
|
2014-11-18 19:26:46 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2014-11-18 00:08:37 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 19:26:49 +00:00
|
|
|
static void gb_message_cancel(struct gb_message *message)
|
2014-11-18 00:08:37 +00:00
|
|
|
{
|
2014-11-18 19:26:49 +00:00
|
|
|
if (message->gbuf.status != -EINPROGRESS)
|
2014-11-18 00:08:37 +00:00
|
|
|
return;
|
|
|
|
|
2014-11-18 19:26:49 +00:00
|
|
|
message->gbuf.hd->driver->buffer_cancel(message->gbuf.hcd_data);
|
2014-11-18 00:08:37 +00:00
|
|
|
}
|
2014-11-18 19:26:44 +00:00
|
|
|
|
2014-10-02 02:54:15 +00:00
|
|
|
/*
|
|
|
|
* An operations's response message has arrived. If no callback was
|
|
|
|
* supplied it was submitted for asynchronous completion, so we notify
|
|
|
|
* any waiters. Otherwise we assume calling the completion is enough
|
|
|
|
* and nobody else will be waiting.
|
|
|
|
*/
|
2014-10-27 07:40:09 +00:00
|
|
|
static void gb_operation_complete(struct gb_operation *operation)
|
2014-10-02 02:54:15 +00:00
|
|
|
{
|
|
|
|
if (operation->callback)
|
|
|
|
operation->callback(operation);
|
|
|
|
else
|
|
|
|
complete_all(&operation->completion);
|
|
|
|
}
|
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
/* Wait for a submitted operation to complete */
|
2014-10-02 02:54:15 +00:00
|
|
|
int gb_operation_wait(struct gb_operation *operation)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = wait_for_completion_interruptible(&operation->completion);
|
|
|
|
/* If interrupted, cancel the in-flight buffer */
|
|
|
|
if (ret < 0)
|
2014-11-18 19:26:49 +00:00
|
|
|
gb_message_cancel(&operation->request);
|
2014-10-02 02:54:15 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
static void gb_operation_request_handle(struct gb_operation *operation)
|
|
|
|
{
|
2014-11-05 22:12:55 +00:00
|
|
|
struct gb_protocol *protocol = operation->connection->protocol;
|
|
|
|
struct gb_operation_msg_hdr *header;
|
2014-10-16 11:35:34 +00:00
|
|
|
|
2014-11-18 00:08:35 +00:00
|
|
|
header = operation->request.gbuf.transfer_buffer;
|
2014-11-12 21:17:55 +00:00
|
|
|
|
2014-11-05 22:12:55 +00:00
|
|
|
/*
|
|
|
|
* If the protocol has no incoming request handler, report
|
|
|
|
* an error and mark the request bad.
|
|
|
|
*/
|
|
|
|
if (protocol->request_recv) {
|
2014-11-12 21:17:55 +00:00
|
|
|
protocol->request_recv(header->type, operation);
|
2014-11-05 22:12:55 +00:00
|
|
|
goto out;
|
2014-10-16 11:35:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 00:35:59 +00:00
|
|
|
gb_connection_err(operation->connection,
|
2014-11-05 22:12:55 +00:00
|
|
|
"unexpected incoming request type 0x%02hhx\n", header->type);
|
2014-10-16 11:35:34 +00:00
|
|
|
operation->result = GB_OP_PROTOCOL_BAD;
|
2014-11-05 22:12:55 +00:00
|
|
|
out:
|
2014-10-16 11:35:34 +00:00
|
|
|
gb_operation_complete(operation);
|
|
|
|
}
|
|
|
|
|
2014-10-02 02:54:15 +00:00
|
|
|
/*
|
2014-10-16 11:35:34 +00:00
|
|
|
* Either this operation contains an incoming request, or its
|
|
|
|
* response has arrived. An incoming request will have a null
|
|
|
|
* response buffer pointer (it is the responsibility of the request
|
|
|
|
* handler to allocate and fill in the response buffer).
|
2014-10-02 02:54:15 +00:00
|
|
|
*/
|
2014-10-16 11:35:34 +00:00
|
|
|
static void gb_operation_recv_work(struct work_struct *recv_work)
|
2014-10-02 02:54:15 +00:00
|
|
|
{
|
2014-10-16 11:35:32 +00:00
|
|
|
struct gb_operation *operation;
|
2014-10-16 11:35:34 +00:00
|
|
|
bool incoming_request;
|
2014-10-16 11:35:32 +00:00
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
operation = container_of(recv_work, struct gb_operation, recv_work);
|
2014-11-18 00:08:35 +00:00
|
|
|
incoming_request = operation->response.gbuf.transfer_buffer == NULL;
|
2014-10-16 11:35:34 +00:00
|
|
|
if (incoming_request)
|
|
|
|
gb_operation_request_handle(operation);
|
|
|
|
gb_operation_complete(operation);
|
|
|
|
}
|
|
|
|
|
2014-10-27 07:40:09 +00:00
|
|
|
/*
|
|
|
|
* Timeout call for the operation.
|
|
|
|
*
|
|
|
|
* If this fires, something went wrong, so mark the result as timed out, and
|
|
|
|
* run the completion handler, which (hopefully) should clean up the operation
|
|
|
|
* properly.
|
|
|
|
*/
|
|
|
|
static void operation_timeout(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct gb_operation *operation;
|
|
|
|
|
|
|
|
operation = container_of(work, struct gb_operation, timeout_work.work);
|
2014-11-13 12:44:35 +00:00
|
|
|
pr_debug("%s: timeout!\n", __func__);
|
2014-10-27 07:40:09 +00:00
|
|
|
|
|
|
|
operation->result = GB_OP_TIMEOUT;
|
|
|
|
gb_operation_complete(operation);
|
|
|
|
}
|
|
|
|
|
2014-10-02 02:54:15 +00:00
|
|
|
/*
|
2014-10-16 11:35:31 +00:00
|
|
|
* Allocate a buffer to be used for an operation request or response
|
2014-10-16 11:35:34 +00:00
|
|
|
* message. For outgoing messages, both types of message contain a
|
|
|
|
* common header, which is filled in here. Incoming requests or
|
|
|
|
* responses also contain the same header, but there's no need to
|
|
|
|
* initialize it here (it'll be overwritten by the incoming
|
|
|
|
* message).
|
2014-10-16 11:35:31 +00:00
|
|
|
*/
|
2014-11-18 00:08:32 +00:00
|
|
|
static int gb_operation_message_init(struct gb_operation *operation,
|
|
|
|
u8 type, size_t size,
|
2014-11-18 19:26:38 +00:00
|
|
|
bool request, bool outbound)
|
2014-10-16 11:35:31 +00:00
|
|
|
{
|
2014-11-17 14:08:43 +00:00
|
|
|
struct gb_connection *connection = operation->connection;
|
2014-11-18 00:08:33 +00:00
|
|
|
struct greybus_host_device *hd = connection->hd;
|
2014-11-18 00:08:32 +00:00
|
|
|
struct gb_message *message;
|
2014-10-16 11:35:31 +00:00
|
|
|
struct gb_operation_msg_hdr *header;
|
2014-11-18 19:26:38 +00:00
|
|
|
gfp_t gfp_flags = request && !outbound ? GFP_ATOMIC : GFP_KERNEL;
|
2014-11-17 14:08:41 +00:00
|
|
|
u16 dest_cport_id;
|
2014-10-16 11:35:31 +00:00
|
|
|
|
2014-11-17 14:08:39 +00:00
|
|
|
if (size > GB_OPERATION_MESSAGE_SIZE_MAX)
|
2014-11-18 00:08:32 +00:00
|
|
|
return -E2BIG;
|
2014-11-18 00:08:35 +00:00
|
|
|
size += sizeof(*header);
|
2014-11-17 14:08:39 +00:00
|
|
|
|
2014-11-18 00:08:32 +00:00
|
|
|
if (request) {
|
|
|
|
message = &operation->request;
|
|
|
|
} else {
|
|
|
|
message = &operation->response;
|
|
|
|
type |= GB_OPERATION_TYPE_RESPONSE;
|
|
|
|
}
|
2014-11-18 00:08:35 +00:00
|
|
|
|
2014-11-18 19:26:38 +00:00
|
|
|
if (outbound)
|
2014-11-17 14:08:43 +00:00
|
|
|
dest_cport_id = connection->interface_cport_id;
|
2014-11-17 14:08:41 +00:00
|
|
|
else
|
|
|
|
dest_cport_id = CPORT_ID_BAD;
|
2014-11-18 00:08:32 +00:00
|
|
|
|
2014-11-18 19:26:47 +00:00
|
|
|
message->gbuf.transfer_buffer = hd->driver->buffer_alloc(size, gfp_flags);
|
|
|
|
if (!message->gbuf.transfer_buffer)
|
2014-11-18 19:26:42 +00:00
|
|
|
return -ENOMEM;
|
2014-11-18 19:26:47 +00:00
|
|
|
message->gbuf.transfer_buffer_length = size;
|
|
|
|
message->gbuf.hd = hd;
|
|
|
|
message->gbuf.dest_cport_id = dest_cport_id;
|
|
|
|
message->gbuf.status = -EBADR; /* Initial value--means "never set" */
|
2014-10-16 11:35:31 +00:00
|
|
|
|
|
|
|
/* Fill in the header structure */
|
2014-11-18 19:26:47 +00:00
|
|
|
header = message->gbuf.transfer_buffer;
|
2014-10-16 11:35:31 +00:00
|
|
|
header->size = cpu_to_le16(size);
|
|
|
|
header->id = 0; /* Filled in when submitted */
|
|
|
|
header->type = type;
|
|
|
|
|
2014-11-18 00:08:32 +00:00
|
|
|
message->payload = header + 1;
|
|
|
|
message->operation = operation;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_operation_message_exit(struct gb_message *message)
|
|
|
|
{
|
|
|
|
message->operation = NULL;
|
|
|
|
message->payload = NULL;
|
2014-11-18 19:26:43 +00:00
|
|
|
message->gbuf.hd->driver->buffer_free(message->gbuf.transfer_buffer);
|
|
|
|
message->gbuf.transfer_buffer = NULL;
|
|
|
|
message->gbuf.transfer_buffer_length = 0;
|
2014-10-16 11:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a Greybus operation to be sent over the given connection.
|
|
|
|
* The request buffer will big enough for a payload of the given
|
|
|
|
* size. Outgoing requests must specify the size of the response
|
|
|
|
* buffer size, which must be sufficient to hold all expected
|
|
|
|
* response data.
|
|
|
|
*
|
|
|
|
* Incoming requests will supply a response size of 0, and in that
|
|
|
|
* case no response buffer is allocated. (A response always
|
|
|
|
* includes a status byte, so 0 is not a valid size.) Whatever
|
|
|
|
* handles the operation request is responsible for allocating the
|
|
|
|
* response buffer.
|
2014-10-02 02:54:15 +00:00
|
|
|
*
|
2014-10-16 11:35:31 +00:00
|
|
|
* Returns a pointer to the new operation or a null pointer if an
|
|
|
|
* error occurs.
|
2014-10-02 02:54:15 +00:00
|
|
|
*/
|
|
|
|
struct gb_operation *gb_operation_create(struct gb_connection *connection,
|
2014-10-16 11:35:31 +00:00
|
|
|
u8 type, size_t request_size,
|
|
|
|
size_t response_size)
|
2014-10-02 02:54:15 +00:00
|
|
|
{
|
|
|
|
struct gb_operation *operation;
|
2014-10-16 11:35:31 +00:00
|
|
|
gfp_t gfp_flags = response_size ? GFP_KERNEL : GFP_ATOMIC;
|
2014-10-16 11:35:34 +00:00
|
|
|
bool outgoing = response_size != 0;
|
2014-11-18 00:08:32 +00:00
|
|
|
int ret;
|
2014-10-02 02:54:15 +00:00
|
|
|
|
2014-10-20 15:27:56 +00:00
|
|
|
operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
|
2014-10-02 02:54:15 +00:00
|
|
|
if (!operation)
|
|
|
|
return NULL;
|
2014-10-27 09:58:54 +00:00
|
|
|
operation->connection = connection;
|
2014-10-02 02:54:15 +00:00
|
|
|
|
2014-11-18 00:08:32 +00:00
|
|
|
ret = gb_operation_message_init(operation, type, request_size,
|
|
|
|
true, outgoing);
|
|
|
|
if (ret)
|
2014-10-20 15:27:56 +00:00
|
|
|
goto err_cache;
|
2014-10-16 11:35:31 +00:00
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
if (outgoing) {
|
2014-11-18 00:08:32 +00:00
|
|
|
ret = gb_operation_message_init(operation, type, response_size,
|
|
|
|
false, false);
|
|
|
|
if (ret)
|
2014-10-20 15:27:56 +00:00
|
|
|
goto err_request;
|
2014-10-16 11:35:31 +00:00
|
|
|
}
|
2014-10-02 02:54:15 +00:00
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
INIT_WORK(&operation->recv_work, gb_operation_recv_work);
|
2014-10-02 02:54:15 +00:00
|
|
|
operation->callback = NULL; /* set at submit time */
|
|
|
|
init_completion(&operation->completion);
|
2014-10-27 07:40:09 +00:00
|
|
|
INIT_DELAYED_WORK(&operation->timeout_work, operation_timeout);
|
2014-11-17 14:08:40 +00:00
|
|
|
kref_init(&operation->kref);
|
2014-10-02 02:54:15 +00:00
|
|
|
|
|
|
|
spin_lock_irq(&gb_operations_lock);
|
|
|
|
list_add_tail(&operation->links, &connection->operations);
|
|
|
|
spin_unlock_irq(&gb_operations_lock);
|
|
|
|
|
|
|
|
return operation;
|
2014-10-20 15:27:56 +00:00
|
|
|
|
|
|
|
err_request:
|
2014-11-18 00:08:32 +00:00
|
|
|
gb_operation_message_exit(&operation->request);
|
2014-10-20 15:27:56 +00:00
|
|
|
err_cache:
|
|
|
|
kmem_cache_free(gb_operation_cache, operation);
|
|
|
|
|
|
|
|
return NULL;
|
2014-10-02 02:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a previously created operation.
|
|
|
|
*/
|
2014-11-17 14:08:40 +00:00
|
|
|
static void _gb_operation_destroy(struct kref *kref)
|
2014-10-02 02:54:15 +00:00
|
|
|
{
|
2014-11-17 14:08:40 +00:00
|
|
|
struct gb_operation *operation;
|
|
|
|
|
|
|
|
operation = container_of(kref, struct gb_operation, kref);
|
2014-10-02 02:54:15 +00:00
|
|
|
|
|
|
|
/* XXX Make sure it's not in flight */
|
|
|
|
spin_lock_irq(&gb_operations_lock);
|
|
|
|
list_del(&operation->links);
|
|
|
|
spin_unlock_irq(&gb_operations_lock);
|
|
|
|
|
2014-11-18 00:08:32 +00:00
|
|
|
gb_operation_message_exit(&operation->response);
|
|
|
|
gb_operation_message_exit(&operation->request);
|
2014-10-02 02:54:15 +00:00
|
|
|
|
2014-10-20 15:27:56 +00:00
|
|
|
kmem_cache_free(gb_operation_cache, operation);
|
2014-10-02 02:54:15 +00:00
|
|
|
}
|
2014-10-16 11:35:33 +00:00
|
|
|
|
2014-11-17 14:08:40 +00:00
|
|
|
void gb_operation_put(struct gb_operation *operation)
|
|
|
|
{
|
|
|
|
if (!WARN_ON(!operation))
|
|
|
|
kref_put(&operation->kref, _gb_operation_destroy);
|
|
|
|
}
|
|
|
|
|
2014-10-16 11:35:33 +00:00
|
|
|
/*
|
|
|
|
* Send an operation request message. The caller has filled in
|
|
|
|
* any payload so the request message is ready to go. If non-null,
|
|
|
|
* the callback function supplied will be called when the response
|
|
|
|
* message has arrived indicating the operation is complete. A null
|
|
|
|
* callback function is used for a synchronous request; return from
|
|
|
|
* this function won't occur until the operation is complete (or an
|
|
|
|
* interrupt occurs).
|
|
|
|
*/
|
|
|
|
int gb_operation_request_send(struct gb_operation *operation,
|
|
|
|
gb_operation_callback callback)
|
|
|
|
{
|
2014-11-12 21:17:52 +00:00
|
|
|
unsigned long timeout;
|
2014-10-16 11:35:33 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-10-22 07:04:30 +00:00
|
|
|
if (operation->connection->state != GB_CONNECTION_STATE_ENABLED)
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
2014-10-16 11:35:33 +00:00
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* I think the order of operations is going to be
|
|
|
|
* significant, and if so, we may need a mutex to surround
|
|
|
|
* setting the operation id and submitting the gbuf.
|
|
|
|
*/
|
|
|
|
operation->callback = callback;
|
2014-11-12 21:17:53 +00:00
|
|
|
gb_pending_operation_insert(operation);
|
2014-11-18 19:26:48 +00:00
|
|
|
ret = gb_message_send(&operation->request, GFP_KERNEL);
|
2014-10-16 11:35:33 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-11-12 21:17:52 +00:00
|
|
|
|
|
|
|
/* We impose a time limit for requests to complete. */
|
|
|
|
timeout = msecs_to_jiffies(OPERATION_TIMEOUT_DEFAULT);
|
|
|
|
schedule_delayed_work(&operation->timeout_work, timeout);
|
2014-10-16 11:35:33 +00:00
|
|
|
if (!callback)
|
|
|
|
ret = gb_operation_wait(operation);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a response for an incoming operation request.
|
|
|
|
*/
|
|
|
|
int gb_operation_response_send(struct gb_operation *operation)
|
|
|
|
{
|
|
|
|
gb_operation_destroy(operation);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
/*
|
2014-11-17 14:08:39 +00:00
|
|
|
* Handle data arriving on a connection. As soon as we return, the
|
|
|
|
* incoming data buffer will be reused, so we need to copy the data
|
|
|
|
* into one of our own operation message buffers.
|
|
|
|
*
|
|
|
|
* If the incoming data is an operation response message, look up
|
|
|
|
* the operation and copy the incoming data into its response
|
|
|
|
* buffer. Otherwise allocate a new operation and copy the incoming
|
|
|
|
* data into its request buffer.
|
|
|
|
*
|
|
|
|
* This is called in interrupt context, so just copy the incoming
|
|
|
|
* data into the buffer and do remaining handling via a work queue.
|
|
|
|
*
|
2014-10-16 11:35:34 +00:00
|
|
|
*/
|
2014-10-16 11:35:33 +00:00
|
|
|
void gb_connection_operation_recv(struct gb_connection *connection,
|
|
|
|
void *data, size_t size)
|
|
|
|
{
|
|
|
|
struct gb_operation_msg_hdr *header;
|
|
|
|
struct gb_operation *operation;
|
|
|
|
struct gbuf *gbuf;
|
|
|
|
u16 msg_size;
|
|
|
|
|
2014-10-22 07:04:30 +00:00
|
|
|
if (connection->state != GB_CONNECTION_STATE_ENABLED)
|
|
|
|
return;
|
|
|
|
|
2014-11-17 14:08:39 +00:00
|
|
|
if (size < sizeof(*header)) {
|
|
|
|
gb_connection_err(connection, "message too small");
|
2014-10-16 11:35:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
header = data;
|
|
|
|
msg_size = le16_to_cpu(header->size);
|
|
|
|
if (header->type & GB_OPERATION_TYPE_RESPONSE) {
|
|
|
|
u16 id = le16_to_cpu(header->id);
|
|
|
|
|
2014-11-12 21:17:53 +00:00
|
|
|
operation = gb_pending_operation_find(connection, id);
|
2014-10-16 11:35:33 +00:00
|
|
|
if (!operation) {
|
|
|
|
gb_connection_err(connection, "operation not found");
|
2014-10-16 11:35:34 +00:00
|
|
|
return;
|
2014-10-16 11:35:33 +00:00
|
|
|
}
|
2014-11-17 14:08:36 +00:00
|
|
|
cancel_delayed_work(&operation->timeout_work);
|
2014-11-12 21:17:53 +00:00
|
|
|
gb_pending_operation_remove(operation);
|
2014-11-18 00:08:35 +00:00
|
|
|
gbuf = &operation->response.gbuf;
|
2014-10-16 11:35:33 +00:00
|
|
|
if (size > gbuf->transfer_buffer_length) {
|
2014-11-17 14:08:39 +00:00
|
|
|
operation->result = GB_OP_OVERFLOW;
|
2014-10-16 11:35:33 +00:00
|
|
|
gb_connection_err(connection, "recv buffer too small");
|
|
|
|
return;
|
|
|
|
}
|
2014-11-17 14:08:39 +00:00
|
|
|
operation->result = GB_OP_SUCCESS;
|
2014-10-16 11:35:33 +00:00
|
|
|
} else {
|
|
|
|
WARN_ON(msg_size != size);
|
|
|
|
operation = gb_operation_create(connection, header->type,
|
|
|
|
msg_size, 0);
|
|
|
|
if (!operation) {
|
|
|
|
gb_connection_err(connection, "can't create operation");
|
|
|
|
return;
|
|
|
|
}
|
2014-11-18 00:08:35 +00:00
|
|
|
gbuf = &operation->request.gbuf;
|
2014-10-16 11:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(gbuf->transfer_buffer, data, msg_size);
|
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
/* The rest will be handled in work queue context */
|
|
|
|
queue_work(gb_operation_recv_workqueue, &operation->recv_work);
|
|
|
|
}
|
|
|
|
|
2014-10-22 07:04:29 +00:00
|
|
|
/*
|
|
|
|
* Cancel an operation.
|
|
|
|
*/
|
|
|
|
void gb_operation_cancel(struct gb_operation *operation)
|
|
|
|
{
|
|
|
|
operation->canceled = true;
|
2014-11-18 19:26:49 +00:00
|
|
|
gb_message_cancel(&operation->request);
|
2014-11-18 00:08:35 +00:00
|
|
|
if (operation->response.gbuf.transfer_buffer)
|
2014-11-18 19:26:49 +00:00
|
|
|
gb_message_cancel(&operation->response);
|
2014-10-22 07:04:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
int gb_operation_init(void)
|
|
|
|
{
|
2014-10-20 15:27:56 +00:00
|
|
|
gb_operation_cache = kmem_cache_create("gb_operation_cache",
|
|
|
|
sizeof(struct gb_operation), 0, 0, NULL);
|
|
|
|
if (!gb_operation_cache)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
gb_operation_recv_workqueue = alloc_workqueue("greybus_recv", 0, 1);
|
2014-10-20 15:27:56 +00:00
|
|
|
if (!gb_operation_recv_workqueue) {
|
|
|
|
kmem_cache_destroy(gb_operation_cache);
|
|
|
|
gb_operation_cache = NULL;
|
2014-10-16 11:35:34 +00:00
|
|
|
return -ENOMEM;
|
2014-10-20 15:27:56 +00:00
|
|
|
}
|
2014-10-16 11:35:34 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gb_operation_exit(void)
|
|
|
|
{
|
|
|
|
destroy_workqueue(gb_operation_recv_workqueue);
|
2014-10-20 15:27:56 +00:00
|
|
|
gb_operation_recv_workqueue = NULL;
|
2014-11-14 11:55:00 +00:00
|
|
|
kmem_cache_destroy(gb_operation_cache);
|
|
|
|
gb_operation_cache = NULL;
|
2014-10-16 11:35:33 +00:00
|
|
|
}
|