blob: c3f7ce71b0897b96805f7c0808bee5d220d5759c [file] [log] [blame]
Alex Eldere88afa52014-10-01 21:54:15 -05001/*
2 * Greybus operations
3 *
4 * Copyright 2014 Google Inc.
Alex Eldera46e9672014-12-12 12:08:42 -06005 * Copyright 2014 Linaro Ltd.
Alex Eldere88afa52014-10-01 21:54:15 -05006 *
7 * Released under the GPLv2 only.
8 */
9
10#ifndef __OPERATION_H
11#define __OPERATION_H
12
13#include <linux/completion.h>
14
Alex Elder3c3cef42014-11-17 18:08:30 -060015struct gb_operation;
16
Johan Hovold4f2c08a2015-07-14 15:43:36 +020017/* The default amount of time a request is given to complete */
18#define GB_OPERATION_TIMEOUT_DEFAULT 1000 /* milliseconds */
19
Alex Elderc939c2f2014-12-02 17:25:11 -060020/*
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 Elder6d653372015-05-07 13:03:52 -050030#define GB_MESSAGE_TYPE_RESPONSE ((u8)0x80)
Alex Elderc939c2f2014-12-02 17:25:11 -060031
Alex Elder23383de2014-11-21 19:29:12 -060032enum gb_operation_result {
Alex Elder57248fa2014-12-01 07:53:09 -060033 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 Elderaa263512014-12-10 08:43:33 -060041 GB_OP_NONEXISTENT = 0x08,
Alex Elder57248fa2014-12-01 07:53:09 -060042 GB_OP_UNKNOWN_ERROR = 0xfe,
43 GB_OP_MALFUNCTION = 0xff,
Alex Eldere88afa52014-10-01 21:54:15 -050044};
45
Johan Hovold8e929a82015-05-19 11:22:45 +020046#define GB_OPERATION_MESSAGE_SIZE_MIN sizeof(struct gb_operation_msg_hdr)
Johan Hovoldc38cf102015-05-19 11:22:44 +020047#define GB_OPERATION_MESSAGE_SIZE_MAX U16_MAX
Johan Hovoldd9336672015-05-19 11:22:43 +020048
Johan Hovoldac67acd2015-04-07 11:27:15 +020049/*
Johan Hovold3e136cc2015-07-01 12:37:21 +020050 * 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 Elder7cfa6992014-12-03 12:27:44 -060053 */
Alex Elder3690a822014-11-17 18:08:31 -060054struct gb_message {
Alex Elder0a4e14a2014-11-20 16:09:14 -060055 struct gb_operation *operation;
Alex Elder7cfa6992014-12-03 12:27:44 -060056 struct gb_operation_msg_hdr *header;
57
58 void *payload;
59 size_t payload_size;
Alex Elder87d208f2014-11-20 16:09:16 -060060
Johan Hovold1e5613b2015-04-07 11:27:17 +020061 void *buffer;
Johan Hovold3e136cc2015-07-01 12:37:21 +020062
63 void *hcpriv;
Alex Elder3690a822014-11-17 18:08:31 -060064};
65
Johan Hovold710067e2015-07-01 12:37:26 +020066#define GB_OPERATION_FLAG_INCOMING BIT(0)
Johan Hovolde3398812015-07-01 12:37:27 +020067#define GB_OPERATION_FLAG_UNIDIRECTIONAL BIT(1)
Johan Hovold710067e2015-07-01 12:37:26 +020068
Alex Eldere88afa52014-10-01 21:54:15 -050069/*
70 * A Greybus operation is a remote procedure call performed over a
Alex Elder82b5e3f2014-12-03 12:27:46 -060071 * connection between two UniPro interfaces.
72 *
Alex Eldere88afa52014-10-01 21:54:15 -050073 * Every operation consists of a request message sent to the other
Alex Elder82b5e3f2014-12-03 12:27:46 -060074 * 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 Eldere88afa52014-10-01 21:54:15 -050077 *
Alex Elder82b5e3f2014-12-03 12:27:46 -060078 * 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 Eldere88afa52014-10-01 21:54:15 -050083 *
Alex Elder82b5e3f2014-12-03 12:27:46 -060084 * 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 Eldere88afa52014-10-01 21:54:15 -050087 */
Alex Eldere88afa52014-10-01 21:54:15 -050088typedef void (*gb_operation_callback)(struct gb_operation *);
89struct gb_operation {
90 struct gb_connection *connection;
Alex Elderc08b1dd2014-11-20 16:09:15 -060091 struct gb_message *request;
92 struct gb_message *response;
Alex Elder22b320f2014-10-16 06:35:31 -050093
Johan Hovold710067e2015-07-01 12:37:26 +020094 unsigned long flags;
95 u8 type;
Alex Elder82b5e3f2014-12-03 12:27:46 -060096 u16 id;
Alex Elder23383de2014-11-21 19:29:12 -060097 int errno; /* Operation result */
98
Alex Elderee637a92014-11-21 19:29:13 -060099 struct work_struct work;
Johan Hovold25932612015-07-01 12:37:28 +0200100 gb_operation_callback callback;
101 struct completion completion;
Alex Eldere88afa52014-10-01 21:54:15 -0500102
Alex Elderc7d0f252014-11-17 08:08:40 -0600103 struct kref kref;
Johan Hovoldfd7134a2015-07-14 15:43:26 +0200104 atomic_t waiters;
Johan Hovold3eeac7e2015-07-14 15:43:25 +0200105
Johan Hovold008974c2015-07-14 15:43:31 +0200106 int active;
Alex Elderafb2e132014-12-03 08:35:07 -0600107 struct list_head links; /* connection->operations */
Alex Eldere88afa52014-10-01 21:54:15 -0500108};
109
Johan Hovold710067e2015-07-01 12:37:26 +0200110static inline bool
111gb_operation_is_incoming(struct gb_operation *operation)
112{
113 return operation->flags & GB_OPERATION_FLAG_INCOMING;
114}
115
Johan Hovolde3398812015-07-01 12:37:27 +0200116static inline bool
117gb_operation_is_unidirectional(struct gb_operation *operation)
118{
119 return operation->flags & GB_OPERATION_FLAG_UNIDIRECTIONAL;
120}
121
Alex Elder61089e82014-11-18 13:26:50 -0600122void gb_connection_recv(struct gb_connection *connection,
Alex Elderd90c25b2014-10-16 06:35:33 -0500123 void *data, size_t size);
124
Alex Elderba986b52014-11-25 11:33:13 -0600125int gb_operation_result(struct gb_operation *operation);
126
Johan Hovoldd52b35f2015-05-19 11:22:46 +0200127size_t gb_operation_get_payload_size_max(struct gb_connection *connection);
Alex Eldere88afa52014-10-01 21:54:15 -0500128struct gb_operation *gb_operation_create(struct gb_connection *connection,
Alex Elder22b320f2014-10-16 06:35:31 -0500129 u8 type, size_t request_size,
Johan Hovolde4207212015-07-01 12:37:22 +0200130 size_t response_size,
131 gfp_t gfp);
Alex Elderdeb4b9e2014-11-21 19:29:15 -0600132void gb_operation_get(struct gb_operation *operation);
Alex Elderc7d0f252014-11-17 08:08:40 -0600133void gb_operation_put(struct gb_operation *operation);
Alex Eldere88afa52014-10-01 21:54:15 -0500134
Alex Elder82e26f72014-12-02 08:30:39 -0600135bool gb_operation_response_alloc(struct gb_operation *operation,
Johan Hovold1c7658c2015-07-17 18:50:25 +0200136 size_t response_size, gfp_t gfp);
Alex Elder82e26f72014-12-02 08:30:39 -0600137
Alex Elderd90c25b2014-10-16 06:35:33 -0500138int gb_operation_request_send(struct gb_operation *operation,
Johan Hovolda52c4352015-07-01 12:37:23 +0200139 gb_operation_callback callback,
140 gfp_t gfp);
Johan Hovold4f2c08a2015-07-14 15:43:36 +0200141int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
142 unsigned int timeout);
143static inline int
144gb_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 Elderd90c25b2014-10-16 06:35:33 -0500149
Alex Elderf68c05c2014-11-21 19:29:17 -0600150void gb_operation_cancel(struct gb_operation *operation, int errno);
Johan Hovold5a3be762015-07-14 15:43:35 +0200151void gb_operation_cancel_incoming(struct gb_operation *operation, int errno);
Alex Eldere88afa52014-10-01 21:54:15 -0500152
Johan Hovold25376362015-11-03 18:03:23 +0100153void greybus_message_sent(struct gb_host_device *hd,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200154 struct gb_message *message, int status);
Alex Elderd98b52b2014-11-20 16:09:17 -0600155
Johan Hovold129a06f2015-07-14 15:43:37 +0200156int 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
161static inline int gb_operation_sync(struct gb_connection *connection, int type,
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -0800162 void *request, int request_size,
Johan Hovold129a06f2015-07-14 15:43:37 +0200163 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-Hartman10aa8012014-11-24 11:19:13 -0800169
Greg Kroah-Hartman3d0421e2015-06-11 09:22:51 -0700170int gb_operation_init(void);
Alex Elderf35ab902015-06-09 17:42:51 -0500171void gb_operation_exit(void);
Alex Elder2eb585f2014-10-16 06:35:34 -0500172
Alex Eldere88afa52014-10-01 21:54:15 -0500173#endif /* !__OPERATION_H */