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 | /* |
| 21 | * No protocol may define an operation that has numeric value 0x00. |
| 22 | * It is reserved as an explicitly invalid value. |
| 23 | */ |
| 24 | #define GB_OPERATION_TYPE_INVALID ((u8)0x00) |
| 25 | |
| 26 | /* |
| 27 | * The top bit of the type in an operation message header indicates |
| 28 | * whether the message is a request (bit clear) or response (bit set) |
| 29 | */ |
Alex Elder | 6d65337 | 2015-05-07 13:03:52 -0500 | [diff] [blame] | 30 | #define GB_MESSAGE_TYPE_RESPONSE ((u8)0x80) |
Alex Elder | c939c2f | 2014-12-02 17:25:11 -0600 | [diff] [blame] | 31 | |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 32 | enum gb_operation_result { |
Alex Elder | 57248fa | 2014-12-01 07:53:09 -0600 | [diff] [blame] | 33 | GB_OP_SUCCESS = 0x00, |
| 34 | GB_OP_INTERRUPTED = 0x01, |
| 35 | GB_OP_TIMEOUT = 0x02, |
| 36 | GB_OP_NO_MEMORY = 0x03, |
| 37 | GB_OP_PROTOCOL_BAD = 0x04, |
| 38 | GB_OP_OVERFLOW = 0x05, |
| 39 | GB_OP_INVALID = 0x06, |
| 40 | GB_OP_RETRY = 0x07, |
Alex Elder | aa26351 | 2014-12-10 08:43:33 -0600 | [diff] [blame] | 41 | GB_OP_NONEXISTENT = 0x08, |
Alex Elder | 57248fa | 2014-12-01 07:53:09 -0600 | [diff] [blame] | 42 | GB_OP_UNKNOWN_ERROR = 0xfe, |
| 43 | GB_OP_MALFUNCTION = 0xff, |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 44 | }; |
| 45 | |
Johan Hovold | 8e929a8 | 2015-05-19 11:22:45 +0200 | [diff] [blame] | 46 | #define GB_OPERATION_MESSAGE_SIZE_MIN sizeof(struct gb_operation_msg_hdr) |
Johan Hovold | c38cf10 | 2015-05-19 11:22:44 +0200 | [diff] [blame] | 47 | #define GB_OPERATION_MESSAGE_SIZE_MAX U16_MAX |
Johan Hovold | d933667 | 2015-05-19 11:22:43 +0200 | [diff] [blame] | 48 | |
Johan Hovold | ac67acd | 2015-04-07 11:27:15 +0200 | [diff] [blame] | 49 | /* |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 50 | * Protocol code should only examine the payload and payload_size fields, and |
| 51 | * host-controller drivers may use the hcpriv field. All other fields are |
| 52 | * intended to be private to the operations core code. |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 53 | */ |
Alex Elder | 3690a82 | 2014-11-17 18:08:31 -0600 | [diff] [blame] | 54 | struct gb_message { |
Alex Elder | 0a4e14a | 2014-11-20 16:09:14 -0600 | [diff] [blame] | 55 | struct gb_operation *operation; |
Alex Elder | 7cfa699 | 2014-12-03 12:27:44 -0600 | [diff] [blame] | 56 | struct gb_operation_msg_hdr *header; |
| 57 | |
| 58 | void *payload; |
| 59 | size_t payload_size; |
Alex Elder | 87d208f | 2014-11-20 16:09:16 -0600 | [diff] [blame] | 60 | |
Johan Hovold | 1e5613b | 2015-04-07 11:27:17 +0200 | [diff] [blame] | 61 | void *buffer; |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 62 | |
| 63 | void *hcpriv; |
Alex Elder | 3690a82 | 2014-11-17 18:08:31 -0600 | [diff] [blame] | 64 | }; |
| 65 | |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 66 | #define GB_OPERATION_FLAG_INCOMING BIT(0) |
Johan Hovold | e339881 | 2015-07-01 12:37:27 +0200 | [diff] [blame] | 67 | #define GB_OPERATION_FLAG_UNIDIRECTIONAL BIT(1) |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 68 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 69 | /* |
| 70 | * A Greybus operation is a remote procedure call performed over a |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 71 | * connection between two UniPro interfaces. |
| 72 | * |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 73 | * Every operation consists of a request message sent to the other |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 74 | * end of the connection coupled with a reply message returned to |
| 75 | * the sender. Every operation has a type, whose interpretation is |
| 76 | * dependent on the protocol associated with the connection. |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 77 | * |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 78 | * Only four things in an operation structure are intended to be |
| 79 | * directly usable by protocol handlers: the operation's connection |
| 80 | * pointer; the operation type; the request message payload (and |
| 81 | * size); and the response message payload (and size). Note that a |
| 82 | * message with a 0-byte payload has a null message payload pointer. |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 83 | * |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 84 | * In addition, every operation has a result, which is an errno |
| 85 | * value. Protocol handlers access the operation result using |
| 86 | * gb_operation_result(). |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 87 | */ |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 88 | typedef void (*gb_operation_callback)(struct gb_operation *); |
| 89 | struct gb_operation { |
| 90 | struct gb_connection *connection; |
Alex Elder | c08b1dd | 2014-11-20 16:09:15 -0600 | [diff] [blame] | 91 | struct gb_message *request; |
| 92 | struct gb_message *response; |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 93 | |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 94 | unsigned long flags; |
| 95 | u8 type; |
Alex Elder | 82b5e3f | 2014-12-03 12:27:46 -0600 | [diff] [blame] | 96 | u16 id; |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 97 | int errno; /* Operation result */ |
| 98 | |
Alex Elder | ee637a9 | 2014-11-21 19:29:13 -0600 | [diff] [blame] | 99 | struct work_struct work; |
Johan Hovold | 2593261 | 2015-07-01 12:37:28 +0200 | [diff] [blame] | 100 | gb_operation_callback callback; |
| 101 | struct completion completion; |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 102 | |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 103 | struct kref kref; |
Johan Hovold | fd7134a | 2015-07-14 15:43:26 +0200 | [diff] [blame] | 104 | atomic_t waiters; |
Johan Hovold | 3eeac7e | 2015-07-14 15:43:25 +0200 | [diff] [blame] | 105 | |
Johan Hovold | 008974c | 2015-07-14 15:43:31 +0200 | [diff] [blame] | 106 | int active; |
Alex Elder | afb2e13 | 2014-12-03 08:35:07 -0600 | [diff] [blame] | 107 | struct list_head links; /* connection->operations */ |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 108 | }; |
| 109 | |
Johan Hovold | 710067e | 2015-07-01 12:37:26 +0200 | [diff] [blame] | 110 | static inline bool |
| 111 | gb_operation_is_incoming(struct gb_operation *operation) |
| 112 | { |
| 113 | return operation->flags & GB_OPERATION_FLAG_INCOMING; |
| 114 | } |
| 115 | |
Johan Hovold | e339881 | 2015-07-01 12:37:27 +0200 | [diff] [blame] | 116 | static inline bool |
| 117 | gb_operation_is_unidirectional(struct gb_operation *operation) |
| 118 | { |
| 119 | return operation->flags & GB_OPERATION_FLAG_UNIDIRECTIONAL; |
| 120 | } |
| 121 | |
Alex Elder | 61089e8 | 2014-11-18 13:26:50 -0600 | [diff] [blame] | 122 | void gb_connection_recv(struct gb_connection *connection, |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 123 | void *data, size_t size); |
| 124 | |
Alex Elder | ba986b5 | 2014-11-25 11:33:13 -0600 | [diff] [blame] | 125 | int gb_operation_result(struct gb_operation *operation); |
| 126 | |
Johan Hovold | d52b35f | 2015-05-19 11:22:46 +0200 | [diff] [blame] | 127 | size_t gb_operation_get_payload_size_max(struct gb_connection *connection); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 128 | struct gb_operation *gb_operation_create(struct gb_connection *connection, |
Alex Elder | 22b320f | 2014-10-16 06:35:31 -0500 | [diff] [blame] | 129 | u8 type, size_t request_size, |
Johan Hovold | e420721 | 2015-07-01 12:37:22 +0200 | [diff] [blame] | 130 | size_t response_size, |
| 131 | gfp_t gfp); |
Alex Elder | deb4b9e | 2014-11-21 19:29:15 -0600 | [diff] [blame] | 132 | void gb_operation_get(struct gb_operation *operation); |
Alex Elder | c7d0f25 | 2014-11-17 08:08:40 -0600 | [diff] [blame] | 133 | void gb_operation_put(struct gb_operation *operation); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 134 | |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 135 | bool gb_operation_response_alloc(struct gb_operation *operation, |
Johan Hovold | 1c7658c | 2015-07-17 18:50:25 +0200 | [diff] [blame] | 136 | size_t response_size, gfp_t gfp); |
Alex Elder | 82e26f7 | 2014-12-02 08:30:39 -0600 | [diff] [blame] | 137 | |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 138 | int gb_operation_request_send(struct gb_operation *operation, |
Johan Hovold | a52c435 | 2015-07-01 12:37:23 +0200 | [diff] [blame] | 139 | gb_operation_callback callback, |
| 140 | gfp_t gfp); |
Johan Hovold | 4f2c08a | 2015-07-14 15:43:36 +0200 | [diff] [blame] | 141 | int gb_operation_request_send_sync_timeout(struct gb_operation *operation, |
| 142 | unsigned int timeout); |
| 143 | static inline int |
| 144 | gb_operation_request_send_sync(struct gb_operation *operation) |
| 145 | { |
| 146 | return gb_operation_request_send_sync_timeout(operation, |
| 147 | GB_OPERATION_TIMEOUT_DEFAULT); |
| 148 | } |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 149 | |
Alex Elder | f68c05c | 2014-11-21 19:29:17 -0600 | [diff] [blame] | 150 | void gb_operation_cancel(struct gb_operation *operation, int errno); |
Johan Hovold | 5a3be76 | 2015-07-14 15:43:35 +0200 | [diff] [blame] | 151 | void gb_operation_cancel_incoming(struct gb_operation *operation, int errno); |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 152 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 153 | void greybus_message_sent(struct gb_host_device *hd, |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 154 | struct gb_message *message, int status); |
Alex Elder | d98b52b | 2014-11-20 16:09:17 -0600 | [diff] [blame] | 155 | |
Johan Hovold | 129a06f | 2015-07-14 15:43:37 +0200 | [diff] [blame] | 156 | int gb_operation_sync_timeout(struct gb_connection *connection, int type, |
| 157 | void *request, int request_size, |
| 158 | void *response, int response_size, |
| 159 | unsigned int timeout); |
| 160 | |
| 161 | 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] | 162 | void *request, int request_size, |
Johan Hovold | 129a06f | 2015-07-14 15:43:37 +0200 | [diff] [blame] | 163 | void *response, int response_size) |
| 164 | { |
| 165 | return gb_operation_sync_timeout(connection, type, |
| 166 | request, request_size, response, response_size, |
| 167 | GB_OPERATION_TIMEOUT_DEFAULT); |
| 168 | } |
Greg Kroah-Hartman | 10aa801 | 2014-11-24 11:19:13 -0800 | [diff] [blame] | 169 | |
Greg Kroah-Hartman | 3d0421e | 2015-06-11 09:22:51 -0700 | [diff] [blame] | 170 | int gb_operation_init(void); |
Alex Elder | f35ab90 | 2015-06-09 17:42:51 -0500 | [diff] [blame] | 171 | void gb_operation_exit(void); |
Alex Elder | 2eb585f | 2014-10-16 06:35:34 -0500 | [diff] [blame] | 172 | |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 173 | #endif /* !__OPERATION_H */ |