Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Greybus operations |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
Alex Elder | a46e967 | 2014-12-12 12:08:42 -0600 | [diff] [blame] | 5 | * Copyright 2014 Linaro Ltd. |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #ifndef __OPERATION_H |
| 11 | #define __OPERATION_H |
| 12 | |
| 13 | #include <linux/completion.h> |
| 14 | |
Alex Elder | 3c3cef4 | 2014-11-17 18:08:30 -0600 | [diff] [blame] | 15 | struct gb_operation; |
| 16 | |
Johan Hovold | 4f2c08a | 2015-07-14 15:43:36 +0200 | [diff] [blame] | 17 | /* The default amount of time a request is given to complete */ |
| 18 | #define GB_OPERATION_TIMEOUT_DEFAULT 1000 /* milliseconds */ |
| 19 | |
Alex Elder | c939c2f | 2014-12-02 17:25:11 -0600 | [diff] [blame] | 20 | /* |
Alex Elder | c939c2f | 2014-12-02 17:25:11 -0600 | [diff] [blame] | 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 | */ |
Alex Elder | 6d65337 | 2015-05-07 13:03:52 -0500 | [diff] [blame] | 24 | #define GB_MESSAGE_TYPE_RESPONSE ((u8)0x80) |
Alex Elder | c939c2f | 2014-12-02 17:25:11 -0600 | [diff] [blame] | 25 | |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 26 | enum gb_operation_result { |
Alex Elder | 57248fa | 2014-12-01 07:53:09 -0600 | [diff] [blame] | 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, |
Alex Elder | aa26351 | 2014-12-10 08:43:33 -0600 | [diff] [blame] | 35 | GB_OP_NONEXISTENT = 0x08, |
Alex Elder | 57248fa | 2014-12-01 07:53:09 -0600 | [diff] [blame] | 36 | GB_OP_UNKNOWN_ERROR = 0xfe, |
| 37 | GB_OP_MALFUNCTION = 0xff, |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 38 | }; |
| 39 | |
Johan Hovold | 8e929a8 | 2015-05-19 11:22:45 +0200 | [diff] [blame] | 40 | #define GB_OPERATION_MESSAGE_SIZE_MIN sizeof(struct gb_operation_msg_hdr) |
Johan Hovold | c38cf10 | 2015-05-19 11:22:44 +0200 | [diff] [blame] | 41 | #define GB_OPERATION_MESSAGE_SIZE_MAX U16_MAX |
Johan Hovold | d933667 | 2015-05-19 11:22:43 +0200 | [diff] [blame] | 42 | |
Johan Hovold | ac67acd | 2015-04-07 11:27:15 +0200 | [diff] [blame] | 43 | /* |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 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. |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 47 | */ |
Alex Elder | 3690a82 | 2014-11-17 18:08:31 -0600 | [diff] [blame] | 48 | struct gb_message { |
Alex Elder | 0a4e14a | 2014-11-20 16:09:14 -0600 | [diff] [blame] | 49 | struct gb_operation *operation; |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 50 | struct gb_operation_msg_hdr *header; |
| 51 | |
| 52 | void *payload; |
| 53 | size_t payload_size; |
Alex Elder | 87d208f | 2014-11-20 16:09:16 -0600 | [diff] [blame] | 54 | |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 55 | void *buffer; |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 56 | |
| 57 | void *hcpriv; |
Alex Elder | 3690a82 | 2014-11-17 18:08:31 -0600 | [diff] [blame] | 58 | }; |
| 59 | |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 60 | #define GB_OPERATION_FLAG_INCOMING BIT(0) |
Johan Hovold | e339881 | 2015-07-01 12:37:27 +0200 | [diff] [blame] | 61 | #define GB_OPERATION_FLAG_UNIDIRECTIONAL BIT(1) |
Johan Hovold | 7e43e33 | 2016-02-25 14:40:24 +0100 | [diff] [blame] | 62 | #define GB_OPERATION_FLAG_SHORT_RESPONSE BIT(2) |
Johan Hovold | 18079ec | 2016-05-27 17:26:35 +0200 | [diff] [blame] | 63 | #define GB_OPERATION_FLAG_CORE BIT(3) |
Johan Hovold | 7e43e33 | 2016-02-25 14:40:24 +0100 | [diff] [blame] | 64 | |
Johan Hovold | 3e2ee2c | 2016-04-29 17:08:32 +0200 | [diff] [blame] | 65 | #define GB_OPERATION_FLAG_USER_MASK (GB_OPERATION_FLAG_SHORT_RESPONSE | \ |
| 66 | GB_OPERATION_FLAG_UNIDIRECTIONAL) |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 67 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 68 | /* |
| 69 | * A Greybus operation is a remote procedure call performed over a |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 70 | * connection between two UniPro interfaces. |
| 71 | * |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 72 | * Every operation consists of a request message sent to the other |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 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. |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 76 | * |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 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. |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 82 | * |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 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(). |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 86 | */ |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 87 | typedef void (*gb_operation_callback)(struct gb_operation *); |
| 88 | struct gb_operation { |
| 89 | struct gb_connection *connection; |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 90 | struct gb_message *request; |
| 91 | struct gb_message *response; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 92 | |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 93 | unsigned long flags; |
| 94 | u8 type; |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 95 | u16 id; |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 96 | int errno; /* Operation result */ |
| 97 | |
Alex Elder | ee637a9 | 2014-11-21 19:29:13 -0600 | [diff] [blame] | 98 | struct work_struct work; |
Johan Hovold | 2593261 | 2015-07-01 12:37:28 +0200 | [diff] [blame] | 99 | gb_operation_callback callback; |
| 100 | struct completion completion; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 101 | |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 102 | struct kref kref; |
Johan Hovold | fd7134a | 2015-07-14 15:43:26 +0200 | [diff] [blame] | 103 | atomic_t waiters; |
Johan Hovold | 3eeac7e | 2015-07-14 15:43:25 +0200 | [diff] [blame] | 104 | |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 105 | int active; |
Alex Elder | afb2e13 | 2014-12-03 08:35:07 -0600 | [diff] [blame] | 106 | struct list_head links; /* connection->operations */ |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 107 | }; |
| 108 | |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 109 | static inline bool |
| 110 | gb_operation_is_incoming(struct gb_operation *operation) |
| 111 | { |
| 112 | return operation->flags & GB_OPERATION_FLAG_INCOMING; |
| 113 | } |
| 114 | |
Johan Hovold | e339881 | 2015-07-01 12:37:27 +0200 | [diff] [blame] | 115 | static inline bool |
| 116 | gb_operation_is_unidirectional(struct gb_operation *operation) |
| 117 | { |
| 118 | return operation->flags & GB_OPERATION_FLAG_UNIDIRECTIONAL; |
| 119 | } |
| 120 | |
Johan Hovold | 7e43e33 | 2016-02-25 14:40:24 +0100 | [diff] [blame] | 121 | static inline bool |
| 122 | gb_operation_short_response_allowed(struct gb_operation *operation) |
| 123 | { |
| 124 | return operation->flags & GB_OPERATION_FLAG_SHORT_RESPONSE; |
| 125 | } |
| 126 | |
Johan Hovold | 18079ec | 2016-05-27 17:26:35 +0200 | [diff] [blame] | 127 | static inline bool gb_operation_is_core(struct gb_operation *operation) |
| 128 | { |
| 129 | return operation->flags & GB_OPERATION_FLAG_CORE; |
| 130 | } |
| 131 | |
Alex Elder | 61089e8 | 2014-11-18 13:26:50 -0600 | [diff] [blame] | 132 | void gb_connection_recv(struct gb_connection *connection, |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 133 | void *data, size_t size); |
| 134 | |
Alex Elder | ba986b5 | 2014-11-25 11:33:13 -0600 | [diff] [blame] | 135 | int gb_operation_result(struct gb_operation *operation); |
| 136 | |
Johan Hovold | d52b35f | 2015-05-19 11:22:46 +0200 | [diff] [blame] | 137 | size_t gb_operation_get_payload_size_max(struct gb_connection *connection); |
Johan Hovold | 7e43e33 | 2016-02-25 14:40:24 +0100 | [diff] [blame] | 138 | struct gb_operation * |
| 139 | gb_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 | |
| 144 | static inline struct gb_operation * |
| 145 | gb_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 | |
Johan Hovold | 18079ec | 2016-05-27 17:26:35 +0200 | [diff] [blame] | 153 | struct gb_operation * |
| 154 | gb_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 | |
Alex Elder | deb4b9e | 2014-11-21 19:29:15 -0600 | [diff] [blame] | 159 | void gb_operation_get(struct gb_operation *operation); |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 160 | void gb_operation_put(struct gb_operation *operation); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 161 | |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 162 | bool gb_operation_response_alloc(struct gb_operation *operation, |
Johan Hovold | 1c7658c | 2015-07-17 18:50:25 +0200 | [diff] [blame] | 163 | size_t response_size, gfp_t gfp); |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 164 | |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 165 | int gb_operation_request_send(struct gb_operation *operation, |
Johan Hovold | a52c435 | 2015-07-01 12:37:23 +0200 | [diff] [blame] | 166 | gb_operation_callback callback, |
| 167 | gfp_t gfp); |
Johan Hovold | 4f2c08a | 2015-07-14 15:43:36 +0200 | [diff] [blame] | 168 | int gb_operation_request_send_sync_timeout(struct gb_operation *operation, |
| 169 | unsigned int timeout); |
| 170 | static inline int |
| 171 | gb_operation_request_send_sync(struct gb_operation *operation) |
| 172 | { |
| 173 | return gb_operation_request_send_sync_timeout(operation, |
| 174 | GB_OPERATION_TIMEOUT_DEFAULT); |
| 175 | } |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 176 | |
Alex Elder | f68c05c | 2014-11-21 19:29:17 -0600 | [diff] [blame] | 177 | void gb_operation_cancel(struct gb_operation *operation, int errno); |
Johan Hovold | 5a3be76 | 2015-07-14 15:43:35 +0200 | [diff] [blame] | 178 | void gb_operation_cancel_incoming(struct gb_operation *operation, int errno); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 179 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 180 | void greybus_message_sent(struct gb_host_device *hd, |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 181 | struct gb_message *message, int status); |
Alex Elder | d98b52b | 2014-11-20 16:09:17 -0600 | [diff] [blame] | 182 | |
Johan Hovold | 129a06f | 2015-07-14 15:43:37 +0200 | [diff] [blame] | 183 | int 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); |
Johan Hovold | 5fdc027 | 2016-04-29 17:08:33 +0200 | [diff] [blame] | 187 | int gb_operation_unidirectional_timeout(struct gb_connection *connection, |
| 188 | int type, void *request, int request_size, |
| 189 | unsigned int timeout); |
Johan Hovold | 129a06f | 2015-07-14 15:43:37 +0200 | [diff] [blame] | 190 | |
| 191 | static inline int gb_operation_sync(struct gb_connection *connection, int type, |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 192 | void *request, int request_size, |
Johan Hovold | 129a06f | 2015-07-14 15:43:37 +0200 | [diff] [blame] | 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 | } |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 199 | |
Johan Hovold | 5fdc027 | 2016-04-29 17:08:33 +0200 | [diff] [blame] | 200 | static 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 | |
Greg Kroah-Hartman | 3d0421e | 2015-06-11 09:22:51 -0700 | [diff] [blame] | 207 | int gb_operation_init(void); |
Alex Elder | f35ab90 | 2015-06-09 17:42:51 -0500 | [diff] [blame] | 208 | void gb_operation_exit(void); |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 209 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 210 | #endif /* !__OPERATION_H */ |