From dc779229b538f1b5cd5d20a5afdfdfb4c83e5429 Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Tue, 2 Dec 2014 08:30:33 -0600 Subject: [PATCH] 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: Alex Elder Signed-off-by: Greg Kroah-Hartman --- drivers/staging/greybus/operation.c | 66 +++++++++++++++++------------ 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/drivers/staging/greybus/operation.c b/drivers/staging/greybus/operation.c index f474e8fea1b4..d335bd2454ae 100644 --- a/drivers/staging/greybus/operation.c +++ b/drivers/staging/greybus/operation.c @@ -301,6 +301,43 @@ gb_hd_message_find(struct greybus_host_device *hd, void *header) 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. * 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, struct gb_operation_msg_hdr *header; size_t message_size = payload_size + sizeof(*header); size_t size; - u8 *buffer; if (hd->buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) { pr_warn("limiting buffer size to %u\n", @@ -340,33 +376,9 @@ gb_operation_message_alloc(struct greybus_host_device *hd, u8 type, message = kzalloc(size, gfp_flags); if (!message) return NULL; - 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; - } + /* Initialize the message. Operation id is filled in later. */ + gb_operation_message_init(hd, message, 0, message_size, type); return message; } -- 2.39.5