blob: de09a2c7de54a647e3e115d91cddc19072b735be [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/*
Alex Elderc939c2f2014-12-02 17:25:11 -060021 * 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 Elder6d653372015-05-07 13:03:52 -050024#define GB_MESSAGE_TYPE_RESPONSE ((u8)0x80)
Alex Elderc939c2f2014-12-02 17:25:11 -060025
Alex Elder23383de2014-11-21 19:29:12 -060026enum gb_operation_result {
Alex Elder57248fa2014-12-01 07:53:09 -060027 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 Elderaa263512014-12-10 08:43:33 -060035 GB_OP_NONEXISTENT = 0x08,
Alex Elder57248fa2014-12-01 07:53:09 -060036 GB_OP_UNKNOWN_ERROR = 0xfe,
37 GB_OP_MALFUNCTION = 0xff,
Alex Eldere88afa52014-10-01 21:54:15 -050038};
39
Johan Hovold8e929a82015-05-19 11:22:45 +020040#define GB_OPERATION_MESSAGE_SIZE_MIN sizeof(struct gb_operation_msg_hdr)
Johan Hovoldc38cf102015-05-19 11:22:44 +020041#define GB_OPERATION_MESSAGE_SIZE_MAX U16_MAX
Johan Hovoldd9336672015-05-19 11:22:43 +020042
Johan Hovoldac67acd2015-04-07 11:27:15 +020043/*
Johan Hovold3e136cc2015-07-01 12:37:21 +020044 * 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 Elder7cfa6992014-12-03 12:27:44 -060047 */
Alex Elder3690a822014-11-17 18:08:31 -060048struct gb_message {
Alex Elder0a4e14a2014-11-20 16:09:14 -060049 struct gb_operation *operation;
Alex Elder7cfa6992014-12-03 12:27:44 -060050 struct gb_operation_msg_hdr *header;
51
52 void *payload;
53 size_t payload_size;
Alex Elder87d208f2014-11-20 16:09:16 -060054
Johan Hovold1e5613b2015-04-07 11:27:17 +020055 void *buffer;
Johan Hovold3e136cc2015-07-01 12:37:21 +020056
57 void *hcpriv;
Alex Elder3690a822014-11-17 18:08:31 -060058};
59
Johan Hovold710067e2015-07-01 12:37:26 +020060#define GB_OPERATION_FLAG_INCOMING BIT(0)
Johan Hovolde3398812015-07-01 12:37:27 +020061#define GB_OPERATION_FLAG_UNIDIRECTIONAL BIT(1)
Johan Hovold7e43e332016-02-25 14:40:24 +010062#define GB_OPERATION_FLAG_SHORT_RESPONSE BIT(2)
Johan Hovold18079ec2016-05-27 17:26:35 +020063#define GB_OPERATION_FLAG_CORE BIT(3)
Johan Hovold7e43e332016-02-25 14:40:24 +010064
Johan Hovold3e2ee2c2016-04-29 17:08:32 +020065#define GB_OPERATION_FLAG_USER_MASK (GB_OPERATION_FLAG_SHORT_RESPONSE | \
66 GB_OPERATION_FLAG_UNIDIRECTIONAL)
Johan Hovold710067e2015-07-01 12:37:26 +020067
Alex Eldere88afa52014-10-01 21:54:15 -050068/*
69 * A Greybus operation is a remote procedure call performed over a
Alex Elder82b5e3f2014-12-03 12:27:46 -060070 * connection between two UniPro interfaces.
71 *
Alex Eldere88afa52014-10-01 21:54:15 -050072 * Every operation consists of a request message sent to the other
Alex Elder82b5e3f2014-12-03 12:27:46 -060073 * 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 Eldere88afa52014-10-01 21:54:15 -050076 *
Alex Elder82b5e3f2014-12-03 12:27:46 -060077 * 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 Eldere88afa52014-10-01 21:54:15 -050082 *
Alex Elder82b5e3f2014-12-03 12:27:46 -060083 * 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 Eldere88afa52014-10-01 21:54:15 -050086 */
Alex Eldere88afa52014-10-01 21:54:15 -050087typedef void (*gb_operation_callback)(struct gb_operation *);
88struct gb_operation {
89 struct gb_connection *connection;
Alex Elderc08b1dd2014-11-20 16:09:15 -060090 struct gb_message *request;
91 struct gb_message *response;
Alex Elder22b320f2014-10-16 06:35:31 -050092
Johan Hovold710067e2015-07-01 12:37:26 +020093 unsigned long flags;
94 u8 type;
Alex Elder82b5e3f2014-12-03 12:27:46 -060095 u16 id;
Alex Elder23383de2014-11-21 19:29:12 -060096 int errno; /* Operation result */
97
Alex Elderee637a92014-11-21 19:29:13 -060098 struct work_struct work;
Johan Hovold25932612015-07-01 12:37:28 +020099 gb_operation_callback callback;
100 struct completion completion;
Alex Eldere88afa52014-10-01 21:54:15 -0500101
Alex Elderc7d0f252014-11-17 08:08:40 -0600102 struct kref kref;
Johan Hovoldfd7134a2015-07-14 15:43:26 +0200103 atomic_t waiters;
Johan Hovold3eeac7e2015-07-14 15:43:25 +0200104
Johan Hovold008974c2015-07-14 15:43:31 +0200105 int active;
Alex Elderafb2e132014-12-03 08:35:07 -0600106 struct list_head links; /* connection->operations */
Alex Eldere88afa52014-10-01 21:54:15 -0500107};
108
Johan Hovold710067e2015-07-01 12:37:26 +0200109static inline bool
110gb_operation_is_incoming(struct gb_operation *operation)
111{
112 return operation->flags & GB_OPERATION_FLAG_INCOMING;
113}
114
Johan Hovolde3398812015-07-01 12:37:27 +0200115static inline bool
116gb_operation_is_unidirectional(struct gb_operation *operation)
117{
118 return operation->flags & GB_OPERATION_FLAG_UNIDIRECTIONAL;
119}
120
Johan Hovold7e43e332016-02-25 14:40:24 +0100121static inline bool
122gb_operation_short_response_allowed(struct gb_operation *operation)
123{
124 return operation->flags & GB_OPERATION_FLAG_SHORT_RESPONSE;
125}
126
Johan Hovold18079ec2016-05-27 17:26:35 +0200127static inline bool gb_operation_is_core(struct gb_operation *operation)
128{
129 return operation->flags & GB_OPERATION_FLAG_CORE;
130}
131
Alex Elder61089e82014-11-18 13:26:50 -0600132void gb_connection_recv(struct gb_connection *connection,
Alex Elderd90c25b2014-10-16 06:35:33 -0500133 void *data, size_t size);
134
Alex Elderba986b52014-11-25 11:33:13 -0600135int gb_operation_result(struct gb_operation *operation);
136
Johan Hovoldd52b35f2015-05-19 11:22:46 +0200137size_t gb_operation_get_payload_size_max(struct gb_connection *connection);
Johan Hovold7e43e332016-02-25 14:40:24 +0100138struct 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
Johan Hovold18079ec2016-05-27 17:26:35 +0200153struct 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
Alex Elderdeb4b9e2014-11-21 19:29:15 -0600159void gb_operation_get(struct gb_operation *operation);
Alex Elderc7d0f252014-11-17 08:08:40 -0600160void gb_operation_put(struct gb_operation *operation);
Alex Eldere88afa52014-10-01 21:54:15 -0500161
Alex Elder82e26f72014-12-02 08:30:39 -0600162bool gb_operation_response_alloc(struct gb_operation *operation,
Johan Hovold1c7658c2015-07-17 18:50:25 +0200163 size_t response_size, gfp_t gfp);
Alex Elder82e26f72014-12-02 08:30:39 -0600164
Alex Elderd90c25b2014-10-16 06:35:33 -0500165int gb_operation_request_send(struct gb_operation *operation,
Johan Hovolda52c4352015-07-01 12:37:23 +0200166 gb_operation_callback callback,
167 gfp_t gfp);
Johan Hovold4f2c08a2015-07-14 15:43:36 +0200168int 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}
Alex Elderd90c25b2014-10-16 06:35:33 -0500176
Alex Elderf68c05c2014-11-21 19:29:17 -0600177void gb_operation_cancel(struct gb_operation *operation, int errno);
Johan Hovold5a3be762015-07-14 15:43:35 +0200178void gb_operation_cancel_incoming(struct gb_operation *operation, int errno);
Alex Eldere88afa52014-10-01 21:54:15 -0500179
Johan Hovold25376362015-11-03 18:03:23 +0100180void greybus_message_sent(struct gb_host_device *hd,
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200181 struct gb_message *message, int status);
Alex Elderd98b52b2014-11-20 16:09:17 -0600182
Johan Hovold129a06f2015-07-14 15:43:37 +0200183int 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 Hovold5fdc0272016-04-29 17:08:33 +0200187int gb_operation_unidirectional_timeout(struct gb_connection *connection,
188 int type, void *request, int request_size,
189 unsigned int timeout);
Johan Hovold129a06f2015-07-14 15:43:37 +0200190
191static inline int gb_operation_sync(struct gb_connection *connection, int type,
Greg Kroah-Hartman10aa8012014-11-24 11:19:13 -0800192 void *request, int request_size,
Johan Hovold129a06f2015-07-14 15:43:37 +0200193 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-Hartman10aa8012014-11-24 11:19:13 -0800199
Johan Hovold5fdc0272016-04-29 17:08:33 +0200200static 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-Hartman3d0421e2015-06-11 09:22:51 -0700207int gb_operation_init(void);
Alex Elderf35ab902015-06-09 17:42:51 -0500208void gb_operation_exit(void);
Alex Elder2eb585f2014-10-16 06:35:34 -0500209
Alex Eldere88afa52014-10-01 21:54:15 -0500210#endif /* !__OPERATION_H */