operation.h 6.1 KB

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