Commit dc779229 authored by Alex Elder's avatar Alex Elder Committed by Greg Kroah-Hartman

greybus: introduce gb_operation_message_init()

Separate the allocation of a message structure from its basic
initialization.  This will allow very common fixed-size operation
response buffers to be allocated from a slab cache.
Signed-off-by: default avatarAlex Elder <elder@linaro.org>
Signed-off-by: default avatarGreg Kroah-Hartman <greg@kroah.com>
parent ea64cd9a
...@@ -301,6 +301,43 @@ gb_hd_message_find(struct greybus_host_device *hd, void *header) ...@@ -301,6 +301,43 @@ gb_hd_message_find(struct greybus_host_device *hd, void *header)
return message; return message;
} }
static void gb_operation_message_init(struct greybus_host_device *hd,
struct gb_message *message, u16 operation_id,
size_t message_size, u8 type)
{
struct gb_operation_msg_hdr *header;
u8 *buffer;
BUG_ON(message_size < sizeof(*header));
buffer = &message->buffer[0];
header = (struct gb_operation_msg_hdr *)(buffer + hd->buffer_headroom);
message->header = header;
message->payload = header + 1;
message->size = message_size;
/*
* The type supplied for incoming message buffers will be
* 0x00. Such buffers will be overwritten by arriving data
* so there's no need to initialize the message header.
*/
if (type) {
/*
* For a request, the operation id gets filled in
* when the message is sent. For a response, it
* will be copied from the request by the caller.
*
* The result field in a request message must be
* zero. It will be set just prior to sending for
* a response.
*/
header->size = cpu_to_le16(message_size);
header->operation_id = 0;
header->type = type;
header->result = 0;
}
}
/* /*
* Allocate a message to be used for an operation request or response. * Allocate a message to be used for an operation request or response.
* Both types of message contain a common header. The request message * Both types of message contain a common header. The request message
...@@ -325,7 +362,6 @@ gb_operation_message_alloc(struct greybus_host_device *hd, u8 type, ...@@ -325,7 +362,6 @@ gb_operation_message_alloc(struct greybus_host_device *hd, u8 type,
struct gb_operation_msg_hdr *header; struct gb_operation_msg_hdr *header;
size_t message_size = payload_size + sizeof(*header); size_t message_size = payload_size + sizeof(*header);
size_t size; size_t size;
u8 *buffer;
if (hd->buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) { if (hd->buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) {
pr_warn("limiting buffer size to %u\n", pr_warn("limiting buffer size to %u\n",
...@@ -340,33 +376,9 @@ gb_operation_message_alloc(struct greybus_host_device *hd, u8 type, ...@@ -340,33 +376,9 @@ gb_operation_message_alloc(struct greybus_host_device *hd, u8 type,
message = kzalloc(size, gfp_flags); message = kzalloc(size, gfp_flags);
if (!message) if (!message)
return NULL; return NULL;
buffer = &message->buffer[0];
header = (struct gb_operation_msg_hdr *)(buffer + hd->buffer_headroom);
message->header = header; /* Initialize the message. Operation id is filled in later. */
message->payload = header + 1; gb_operation_message_init(hd, message, 0, message_size, type);
message->size = message_size;
/*
* The type supplied for incoming message buffers will be
* 0x00. Such buffers will be overwritten by arriving data
* so there's no need to initialize the message header.
*/
if (type) {
/*
* For a request, the operation id gets filled in
* when the message is sent. For a response, it
* will be copied from the request by the caller.
*
* The result field in a request message must be
* zero. It will be set just prior to sending for
* a response.
*/
header->size = cpu_to_le16(message_size);
header->operation_id = 0;
header->type = type;
header->result = 0;
}
return message; return message;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment