operation.h 6.5 KB

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