spin_unlock_irq(&gb_operations_lock);
/* Store the operation id in the request header */
- header = operation->request.buffer;
+ header = operation->request->header;
header->operation_id = cpu_to_le16(operation->id);
}
message->cookie = connection->hd->driver->buffer_send(connection->hd,
dest_cport_id,
- message->buffer,
- message->buffer_size,
+ message->header,
+ message->size,
gfp_mask);
if (IS_ERR(message->cookie)) {
ret = PTR_ERR(message->cookie);
}
/*
- * An operations's response message has arrived. If no callback was
+ * An operation'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.
ret = wait_for_completion_interruptible(&operation->completion);
/* If interrupted, cancel the in-flight buffer */
if (ret < 0)
- gb_message_cancel(&operation->request);
+ gb_message_cancel(operation->request);
return ret;
-
}
static void gb_operation_request_handle(struct gb_operation *operation)
struct gb_protocol *protocol = operation->connection->protocol;
struct gb_operation_msg_hdr *header;
- header = operation->request.buffer;
+ header = operation->request->header;
/*
* If the protocol has no incoming request handler, report
bool incoming_request;
operation = container_of(recv_work, struct gb_operation, recv_work);
- incoming_request = operation->response.buffer == NULL;
+ incoming_request = operation->response->header == NULL;
if (incoming_request)
gb_operation_request_handle(operation);
gb_operation_complete(operation);
gb_operation_complete(operation);
}
-static void *
-gb_buffer_alloc(struct greybus_host_device *hd, size_t size, gfp_t gfp_flags)
+/*
+ * Given a pointer to the header in a message sent on a given host
+ * device, return the associated message structure. (This "header"
+ * is just the buffer pointer we supply to the host device for
+ * sending.)
+ */
+static struct gb_message *
+gb_hd_message_find(struct greybus_host_device *hd, void *header)
{
- u8 *buffer;
-
- buffer = kzalloc(hd->buffer_headroom + size, gfp_flags);
- if (buffer)
- buffer += hd->buffer_headroom;
-
- return buffer;
-}
+ struct gb_message *message;
+ u8 *result;
-static void
-gb_buffer_free(struct greybus_host_device *hd, void *buffer)
-{
- u8 *allocated = buffer;
+ result = (u8 *)header - hd->buffer_headroom - sizeof(*message);
+ message = (struct gb_message *)result;
- if (allocated)
- kfree(allocated - hd->buffer_headroom);
+ return message;
}
/*
- * Allocate a buffer to be used for an operation request or response
- * message. For outgoing messages, both types of message contain a
+ * Allocate a message to be used for an operation request or
+ * response. 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).
+ *
+ * Our message structure consists of:
+ * message structure
+ * headroom
+ * message header \_ these combined are
+ * message payload / the message size
*/
-static int gb_operation_message_init(struct gb_operation *operation,
- u8 type, size_t size,
- bool request, gfp_t gfp_flags)
+static struct gb_message *
+gb_operation_message_alloc(struct greybus_host_device *hd, u8 type,
+ size_t payload_size, gfp_t gfp_flags)
{
- struct gb_connection *connection = operation->connection;
- struct greybus_host_device *hd = connection->hd;
struct gb_message *message;
struct gb_operation_msg_hdr *header;
+ size_t message_size = payload_size + sizeof(*header);
+ size_t size;
+ u8 *buffer;
- size += sizeof(*header);
- if (size > hd->buffer_size_max)
- return -E2BIG;
-
- if (request) {
- message = &operation->request;
- } else {
- message = &operation->response;
- type |= GB_OPERATION_TYPE_RESPONSE;
- }
+ if (message_size > hd->buffer_size_max)
+ return NULL;
- message->buffer = gb_buffer_alloc(hd, size, gfp_flags);
- if (!message->buffer)
- return -ENOMEM;
- message->buffer_size = size;
+ size = sizeof(*message) + hd->buffer_headroom + message_size;
+ message = kzalloc(size, gfp_flags);
+ if (!message)
+ return NULL;
+ buffer = &message->buffer[0];
+ header = (struct gb_operation_msg_hdr *)(buffer + hd->buffer_headroom);
/* Fill in the header structure */
- header = message->buffer;
- header->size = cpu_to_le16(size);
+ header->size = cpu_to_le16(message_size);
header->operation_id = 0; /* Filled in when submitted */
header->type = type;
+ message->header = header;
message->payload = header + 1;
- message->operation = operation;
+ message->size = message_size;
- return 0;
+ return message;
}
-static void gb_operation_message_exit(struct gb_message *message)
+static void gb_operation_message_free(struct gb_message *message)
{
- struct greybus_host_device *hd;
-
- hd = message->operation->connection->hd;
- gb_buffer_free(hd, message->buffer);
-
- message->operation = NULL;
- message->payload = NULL;
- message->buffer = NULL;
- message->buffer_size = 0;
+ kfree(message);
}
/*
- * Map an enum gb_operation_status value (which is represted in a
- * message as a single back a single byte) to an appropriate Linux
- * negative errno.
+ * Map an enum gb_operation_status value (which is represented in a
+ * message as a single byte) to an appropriate Linux negative errno.
*/
int gb_operation_status_map(u8 status)
{
/*
* Create a Greybus operation to be sent over the given connection.
- * The request buffer will big enough for a payload of the given
+ * The request buffer will be 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.
u8 type, size_t request_size,
size_t response_size)
{
+ struct greybus_host_device *hd = connection->hd;
struct gb_operation *operation;
gfp_t gfp_flags = response_size ? GFP_KERNEL : GFP_ATOMIC;
- int ret;
operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
if (!operation)
return NULL;
operation->connection = connection;
- ret = gb_operation_message_init(operation, type, request_size,
- true, gfp_flags);
- if (ret)
+ operation->request = gb_operation_message_alloc(hd, type, request_size,
+ gfp_flags);
+ if (!operation->request)
goto err_cache;
+ operation->request->operation = operation;
if (outgoing) {
- ret = gb_operation_message_init(operation, type, response_size,
- false, GFP_KERNEL);
- if (ret)
+ type |= GB_OPERATION_TYPE_RESPONSE;
+ operation->response = gb_operation_message_alloc(hd, type,
+ response_size, GFP_KERNEL);
+ if (!operation->response)
goto err_request;
+ operation->response->operation = operation;
}
INIT_WORK(&operation->recv_work, gb_operation_recv_work);
return operation;
err_request:
- gb_operation_message_exit(&operation->request);
+ gb_operation_message_free(operation->request);
err_cache:
kmem_cache_free(gb_operation_cache, operation);
list_del(&operation->links);
spin_unlock_irq(&gb_operations_lock);
- gb_operation_message_exit(&operation->response);
- gb_operation_message_exit(&operation->request);
+ gb_operation_message_free(operation->response);
+ gb_operation_message_free(operation->request);
kmem_cache_free(gb_operation_cache, operation);
}
schedule_delayed_work(&operation->timeout_work, timeout);
/* All set, send the request */
- ret = gb_message_send(&operation->request, GFP_KERNEL);
+ ret = gb_message_send(operation->request, GFP_KERNEL);
if (ret)
return ret;
return 0;
}
+/*
+ * This function is called when a buffer send request has completed.
+ * The "header" is the message header--the beginning of what we
+ * asked to have sent.
+ *
+ * XXX Mismatch between errno here and operation result code
+ */
+void
+greybus_data_sent(struct greybus_host_device *hd, void *header, int status)
+{
+ struct gb_message *message;
+ struct gb_operation *operation;
+
+ /* If there's no error, there's really nothing to do */
+ if (!status)
+ return; /* Mark it complete? */
+
+ /* XXX Right now we assume we're an outgoing request */
+ message = gb_hd_message_find(hd, header);
+ operation = message->operation;
+ gb_connection_err(operation->connection, "send error %d\n", status);
+ operation->result = status; /* XXX */
+ gb_operation_complete(operation);
+}
+EXPORT_SYMBOL_GPL(greybus_data_sent);
+
/*
* We've received data on a connection, and it doesn't look like a
* response, so we assume it's a request.
return; /* XXX Respond with pre-allocated ENOMEM */
}
operation->id = operation_id;
- memcpy(operation->request.buffer, data, size);
+ memcpy(operation->request->header, data, size);
/* The rest will be handled in work queue context */
queue_work(gb_operation_recv_workqueue, &operation->recv_work);
/*
* We've received data that appears to be an operation response
* message. Look up the operation, and record that we've received
- * its repsonse.
+ * its response.
*
* This is called in interrupt context, so just copy the incoming
* data into the response buffer and handle the rest via workqueue.
cancel_delayed_work(&operation->timeout_work);
gb_pending_operation_remove(operation);
- message = &operation->response;
- if (size <= message->buffer_size) {
+ message = operation->response;
+ if (size <= message->size) {
/* Transfer the operation result from the response header */
- header = message->buffer;
+ header = message->header;
operation->result = header->result;
} else {
gb_connection_err(connection, "recv buffer too small");
/* We must ignore the payload if a bad status is returned */
if (operation->result == GB_OP_SUCCESS)
- memcpy(message->buffer, data, size);
+ memcpy(message->header, data, size);
/* The rest will be handled in work queue context */
queue_work(gb_operation_recv_workqueue, &operation->recv_work);
void gb_operation_cancel(struct gb_operation *operation)
{
operation->canceled = true;
- gb_message_cancel(&operation->request);
- if (operation->response.buffer)
- gb_message_cancel(&operation->response);
+ gb_message_cancel(operation->request);
+ if (operation->response->header)
+ gb_message_cancel(operation->response);
}
int gb_operation_init(void)