Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v4.10 210 lines 6.3 kB view raw
1/* 2 * Greybus operations 3 * 4 * Copyright 2014 Google Inc. 5 * Copyright 2014 Linaro Ltd. 6 * 7 * Released under the GPLv2 only. 8 */ 9 10#ifndef __OPERATION_H 11#define __OPERATION_H 12 13#include <linux/completion.h> 14 15struct gb_operation; 16 17/* The default amount of time a request is given to complete */ 18#define GB_OPERATION_TIMEOUT_DEFAULT 1000 /* milliseconds */ 19 20/* 21 * The top bit of the type in an operation message header indicates 22 * whether the message is a request (bit clear) or response (bit set) 23 */ 24#define GB_MESSAGE_TYPE_RESPONSE ((u8)0x80) 25 26enum gb_operation_result { 27 GB_OP_SUCCESS = 0x00, 28 GB_OP_INTERRUPTED = 0x01, 29 GB_OP_TIMEOUT = 0x02, 30 GB_OP_NO_MEMORY = 0x03, 31 GB_OP_PROTOCOL_BAD = 0x04, 32 GB_OP_OVERFLOW = 0x05, 33 GB_OP_INVALID = 0x06, 34 GB_OP_RETRY = 0x07, 35 GB_OP_NONEXISTENT = 0x08, 36 GB_OP_UNKNOWN_ERROR = 0xfe, 37 GB_OP_MALFUNCTION = 0xff, 38}; 39 40#define GB_OPERATION_MESSAGE_SIZE_MIN sizeof(struct gb_operation_msg_hdr) 41#define GB_OPERATION_MESSAGE_SIZE_MAX U16_MAX 42 43/* 44 * Protocol code should only examine the payload and payload_size fields, and 45 * host-controller drivers may use the hcpriv field. All other fields are 46 * intended to be private to the operations core code. 47 */ 48struct gb_message { 49 struct gb_operation *operation; 50 struct gb_operation_msg_hdr *header; 51 52 void *payload; 53 size_t payload_size; 54 55 void *buffer; 56 57 void *hcpriv; 58}; 59 60#define GB_OPERATION_FLAG_INCOMING BIT(0) 61#define GB_OPERATION_FLAG_UNIDIRECTIONAL BIT(1) 62#define GB_OPERATION_FLAG_SHORT_RESPONSE BIT(2) 63#define GB_OPERATION_FLAG_CORE BIT(3) 64 65#define GB_OPERATION_FLAG_USER_MASK (GB_OPERATION_FLAG_SHORT_RESPONSE | \ 66 GB_OPERATION_FLAG_UNIDIRECTIONAL) 67 68/* 69 * A Greybus operation is a remote procedure call performed over a 70 * connection between two UniPro interfaces. 71 * 72 * Every operation consists of a request message sent to the other 73 * end of the connection coupled with a reply message returned to 74 * the sender. Every operation has a type, whose interpretation is 75 * dependent on the protocol associated with the connection. 76 * 77 * Only four things in an operation structure are intended to be 78 * directly usable by protocol handlers: the operation's connection 79 * pointer; the operation type; the request message payload (and 80 * size); and the response message payload (and size). Note that a 81 * message with a 0-byte payload has a null message payload pointer. 82 * 83 * In addition, every operation has a result, which is an errno 84 * value. Protocol handlers access the operation result using 85 * gb_operation_result(). 86 */ 87typedef void (*gb_operation_callback)(struct gb_operation *); 88struct gb_operation { 89 struct gb_connection *connection; 90 struct gb_message *request; 91 struct gb_message *response; 92 93 unsigned long flags; 94 u8 type; 95 u16 id; 96 int errno; /* Operation result */ 97 98 struct work_struct work; 99 gb_operation_callback callback; 100 struct completion completion; 101 102 struct kref kref; 103 atomic_t waiters; 104 105 int active; 106 struct list_head links; /* connection->operations */ 107}; 108 109static inline bool 110gb_operation_is_incoming(struct gb_operation *operation) 111{ 112 return operation->flags & GB_OPERATION_FLAG_INCOMING; 113} 114 115static inline bool 116gb_operation_is_unidirectional(struct gb_operation *operation) 117{ 118 return operation->flags & GB_OPERATION_FLAG_UNIDIRECTIONAL; 119} 120 121static inline bool 122gb_operation_short_response_allowed(struct gb_operation *operation) 123{ 124 return operation->flags & GB_OPERATION_FLAG_SHORT_RESPONSE; 125} 126 127static inline bool gb_operation_is_core(struct gb_operation *operation) 128{ 129 return operation->flags & GB_OPERATION_FLAG_CORE; 130} 131 132void gb_connection_recv(struct gb_connection *connection, 133 void *data, size_t size); 134 135int gb_operation_result(struct gb_operation *operation); 136 137size_t gb_operation_get_payload_size_max(struct gb_connection *connection); 138struct gb_operation * 139gb_operation_create_flags(struct gb_connection *connection, 140 u8 type, size_t request_size, 141 size_t response_size, unsigned long flags, 142 gfp_t gfp); 143 144static inline struct gb_operation * 145gb_operation_create(struct gb_connection *connection, 146 u8 type, size_t request_size, 147 size_t response_size, gfp_t gfp) 148{ 149 return gb_operation_create_flags(connection, type, request_size, 150 response_size, 0, gfp); 151} 152 153struct gb_operation * 154gb_operation_create_core(struct gb_connection *connection, 155 u8 type, size_t request_size, 156 size_t response_size, unsigned long flags, 157 gfp_t gfp); 158 159void gb_operation_get(struct gb_operation *operation); 160void gb_operation_put(struct gb_operation *operation); 161 162bool gb_operation_response_alloc(struct gb_operation *operation, 163 size_t response_size, gfp_t gfp); 164 165int gb_operation_request_send(struct gb_operation *operation, 166 gb_operation_callback callback, 167 gfp_t gfp); 168int gb_operation_request_send_sync_timeout(struct gb_operation *operation, 169 unsigned int timeout); 170static inline int 171gb_operation_request_send_sync(struct gb_operation *operation) 172{ 173 return gb_operation_request_send_sync_timeout(operation, 174 GB_OPERATION_TIMEOUT_DEFAULT); 175} 176 177void gb_operation_cancel(struct gb_operation *operation, int errno); 178void gb_operation_cancel_incoming(struct gb_operation *operation, int errno); 179 180void greybus_message_sent(struct gb_host_device *hd, 181 struct gb_message *message, int status); 182 183int gb_operation_sync_timeout(struct gb_connection *connection, int type, 184 void *request, int request_size, 185 void *response, int response_size, 186 unsigned int timeout); 187int gb_operation_unidirectional_timeout(struct gb_connection *connection, 188 int type, void *request, int request_size, 189 unsigned int timeout); 190 191static inline int gb_operation_sync(struct gb_connection *connection, int type, 192 void *request, int request_size, 193 void *response, int response_size) 194{ 195 return gb_operation_sync_timeout(connection, type, 196 request, request_size, response, response_size, 197 GB_OPERATION_TIMEOUT_DEFAULT); 198} 199 200static inline int gb_operation_unidirectional(struct gb_connection *connection, 201 int type, void *request, int request_size) 202{ 203 return gb_operation_unidirectional_timeout(connection, type, 204 request, request_size, GB_OPERATION_TIMEOUT_DEFAULT); 205} 206 207int gb_operation_init(void); 208void gb_operation_exit(void); 209 210#endif /* !__OPERATION_H */