mcdi.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* SPDX-License-Identifier: GPL-2.0
  2. *
  3. * Copyright 2008-2013 Solarflare Communications Inc.
  4. * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
  5. */
  6. #ifndef CDX_MCDI_H
  7. #define CDX_MCDI_H
  8. #include <linux/mutex.h>
  9. #include <linux/kref.h>
  10. #include <linux/rpmsg.h>
  11. #include "bitfield.h"
  12. #include "mc_cdx_pcol.h"
  13. #ifdef DEBUG
  14. #define CDX_WARN_ON_ONCE_PARANOID(x) WARN_ON_ONCE(x)
  15. #define CDX_WARN_ON_PARANOID(x) WARN_ON(x)
  16. #else
  17. #define CDX_WARN_ON_ONCE_PARANOID(x) do {} while (0)
  18. #define CDX_WARN_ON_PARANOID(x) do {} while (0)
  19. #endif
  20. /**
  21. * enum cdx_mcdi_mode - MCDI transaction mode
  22. * @MCDI_MODE_EVENTS: wait for an mcdi response callback.
  23. * @MCDI_MODE_FAIL: we think MCDI is dead, so fail-fast all calls
  24. */
  25. enum cdx_mcdi_mode {
  26. MCDI_MODE_EVENTS,
  27. MCDI_MODE_FAIL,
  28. };
  29. #define MCDI_RPC_TIMEOUT (10 * HZ)
  30. #define MCDI_RPC_LONG_TIMEOU (60 * HZ)
  31. #define MCDI_RPC_POST_RST_TIME (10 * HZ)
  32. #define MCDI_BUF_LEN (8 + MCDI_CTL_SDU_LEN_MAX)
  33. /**
  34. * enum cdx_mcdi_cmd_state - State for an individual MCDI command
  35. * @MCDI_STATE_QUEUED: Command not started and is waiting to run.
  36. * @MCDI_STATE_RETRY: Command was submitted and MC rejected with no resources,
  37. * as MC have too many outstanding commands. Command will be retried once
  38. * another command returns.
  39. * @MCDI_STATE_RUNNING: Command was accepted and is running.
  40. * @MCDI_STATE_RUNNING_CANCELLED: Command is running but the issuer cancelled
  41. * the command.
  42. * @MCDI_STATE_FINISHED: Processing of this command has completed.
  43. */
  44. enum cdx_mcdi_cmd_state {
  45. MCDI_STATE_QUEUED,
  46. MCDI_STATE_RETRY,
  47. MCDI_STATE_RUNNING,
  48. MCDI_STATE_RUNNING_CANCELLED,
  49. MCDI_STATE_FINISHED,
  50. };
  51. /**
  52. * struct cdx_mcdi - CDX MCDI Firmware interface, to interact
  53. * with CDX controller.
  54. * @mcdi: MCDI interface
  55. * @mcdi_ops: MCDI operations
  56. * @r5_rproc : R5 Remoteproc device handle
  57. * @rpdev: RPMsg device
  58. * @ept: RPMsg endpoint
  59. * @work: Post probe work
  60. */
  61. struct cdx_mcdi {
  62. /* MCDI interface */
  63. struct cdx_mcdi_data *mcdi;
  64. const struct cdx_mcdi_ops *mcdi_ops;
  65. struct rproc *r5_rproc;
  66. struct rpmsg_device *rpdev;
  67. struct rpmsg_endpoint *ept;
  68. struct work_struct work;
  69. };
  70. struct cdx_mcdi_ops {
  71. void (*mcdi_request)(struct cdx_mcdi *cdx,
  72. const struct cdx_dword *hdr, size_t hdr_len,
  73. const struct cdx_dword *sdu, size_t sdu_len);
  74. unsigned int (*mcdi_rpc_timeout)(struct cdx_mcdi *cdx, unsigned int cmd);
  75. };
  76. typedef void cdx_mcdi_async_completer(struct cdx_mcdi *cdx,
  77. unsigned long cookie, int rc,
  78. struct cdx_dword *outbuf,
  79. size_t outlen_actual);
  80. /**
  81. * struct cdx_mcdi_cmd - An outstanding MCDI command
  82. * @ref: Reference count. There will be one reference if the command is
  83. * in the mcdi_iface cmd_list, another if it's on a cleanup list,
  84. * and a third if it's queued in the work queue.
  85. * @list: The data for this entry in mcdi->cmd_list
  86. * @cleanup_list: The data for this entry in a cleanup list
  87. * @work: The work item for this command, queued in mcdi->workqueue
  88. * @mcdi: The mcdi_iface for this command
  89. * @state: The state of this command
  90. * @inlen: inbuf length
  91. * @inbuf: Input buffer
  92. * @quiet: Whether to silence errors
  93. * @reboot_seen: Whether a reboot has been seen during this command,
  94. * to prevent duplicates
  95. * @seq: Sequence number
  96. * @started: Jiffies this command was started at
  97. * @cookie: Context for completion function
  98. * @completer: Completion function
  99. * @handle: Command handle
  100. * @cmd: Command number
  101. * @rc: Return code
  102. * @outlen: Length of output buffer
  103. * @outbuf: Output buffer
  104. */
  105. struct cdx_mcdi_cmd {
  106. struct kref ref;
  107. struct list_head list;
  108. struct list_head cleanup_list;
  109. struct work_struct work;
  110. struct cdx_mcdi_iface *mcdi;
  111. enum cdx_mcdi_cmd_state state;
  112. size_t inlen;
  113. const struct cdx_dword *inbuf;
  114. bool quiet;
  115. bool reboot_seen;
  116. u8 seq;
  117. unsigned long started;
  118. unsigned long cookie;
  119. cdx_mcdi_async_completer *completer;
  120. unsigned int handle;
  121. unsigned int cmd;
  122. int rc;
  123. size_t outlen;
  124. struct cdx_dword *outbuf;
  125. /* followed by inbuf data if necessary */
  126. };
  127. /**
  128. * struct cdx_mcdi_iface - MCDI protocol context
  129. * @cdx: The associated NIC
  130. * @iface_lock: Serialise access to this structure
  131. * @outstanding_cleanups: Count of cleanups
  132. * @cmd_list: List of outstanding and running commands
  133. * @workqueue: Workqueue used for delayed processing
  134. * @cmd_complete_wq: Waitqueue for command completion
  135. * @db_held_by: Command the MC doorbell is in use by
  136. * @seq_held_by: Command each sequence number is in use by
  137. * @prev_handle: The last used command handle
  138. * @mode: Poll for mcdi completion, or wait for an mcdi_event
  139. * @prev_seq: The last used sequence number
  140. * @new_epoch: Indicates start of day or start of MC reboot recovery
  141. */
  142. struct cdx_mcdi_iface {
  143. struct cdx_mcdi *cdx;
  144. /* Serialise access */
  145. struct mutex iface_lock;
  146. unsigned int outstanding_cleanups;
  147. struct list_head cmd_list;
  148. struct workqueue_struct *workqueue;
  149. wait_queue_head_t cmd_complete_wq;
  150. struct cdx_mcdi_cmd *db_held_by;
  151. struct cdx_mcdi_cmd *seq_held_by[16];
  152. unsigned int prev_handle;
  153. enum cdx_mcdi_mode mode;
  154. u8 prev_seq;
  155. bool new_epoch;
  156. };
  157. /**
  158. * struct cdx_mcdi_data - extra state for NICs that implement MCDI
  159. * @iface: Interface/protocol state
  160. * @fn_flags: Flags for this function, as returned by %MC_CMD_DRV_ATTACH.
  161. */
  162. struct cdx_mcdi_data {
  163. struct cdx_mcdi_iface iface;
  164. u32 fn_flags;
  165. };
  166. static inline struct cdx_mcdi_iface *cdx_mcdi_if(struct cdx_mcdi *cdx)
  167. {
  168. return cdx->mcdi ? &cdx->mcdi->iface : NULL;
  169. }
  170. int cdx_mcdi_init(struct cdx_mcdi *cdx);
  171. void cdx_mcdi_finish(struct cdx_mcdi *cdx);
  172. void cdx_mcdi_process_cmd(struct cdx_mcdi *cdx, struct cdx_dword *outbuf, int len);
  173. int cdx_mcdi_rpc(struct cdx_mcdi *cdx, unsigned int cmd,
  174. const struct cdx_dword *inbuf, size_t inlen,
  175. struct cdx_dword *outbuf, size_t outlen, size_t *outlen_actual);
  176. int cdx_mcdi_rpc_async(struct cdx_mcdi *cdx, unsigned int cmd,
  177. const struct cdx_dword *inbuf, size_t inlen,
  178. cdx_mcdi_async_completer *complete,
  179. unsigned long cookie);
  180. int cdx_mcdi_wait_for_quiescence(struct cdx_mcdi *cdx,
  181. unsigned int timeout_jiffies);
  182. /*
  183. * We expect that 16- and 32-bit fields in MCDI requests and responses
  184. * are appropriately aligned, but 64-bit fields are only
  185. * 32-bit-aligned.
  186. */
  187. #define MCDI_DECLARE_BUF(_name, _len) struct cdx_dword _name[DIV_ROUND_UP(_len, 4)] = {{0}}
  188. #define _MCDI_PTR(_buf, _offset) \
  189. ((u8 *)(_buf) + (_offset))
  190. #define MCDI_PTR(_buf, _field) \
  191. _MCDI_PTR(_buf, MC_CMD_ ## _field ## _OFST)
  192. #define _MCDI_CHECK_ALIGN(_ofst, _align) \
  193. ((void)BUILD_BUG_ON_ZERO((_ofst) & ((_align) - 1)), \
  194. (_ofst))
  195. #define _MCDI_DWORD(_buf, _field) \
  196. ((_buf) + (_MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, 4) >> 2))
  197. #define MCDI_BYTE(_buf, _field) \
  198. ((void)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 1), \
  199. *MCDI_PTR(_buf, _field))
  200. #define MCDI_WORD(_buf, _field) \
  201. ((void)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2), \
  202. le16_to_cpu(*(__force const __le16 *)MCDI_PTR(_buf, _field)))
  203. #define MCDI_SET_DWORD(_buf, _field, _value) \
  204. CDX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), CDX_DWORD, _value)
  205. #define MCDI_DWORD(_buf, _field) \
  206. CDX_DWORD_FIELD(*_MCDI_DWORD(_buf, _field), CDX_DWORD)
  207. #define MCDI_POPULATE_DWORD_1(_buf, _field, _name1, _value1) \
  208. CDX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), \
  209. MC_CMD_ ## _name1, _value1)
  210. #define MCDI_SET_QWORD(_buf, _field, _value) \
  211. do { \
  212. CDX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[0], \
  213. CDX_DWORD, (u32)(_value)); \
  214. CDX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[1], \
  215. CDX_DWORD, (u64)(_value) >> 32); \
  216. } while (0)
  217. #define MCDI_QWORD(_buf, _field) \
  218. (CDX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[0], CDX_DWORD) | \
  219. (u64)CDX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[1], CDX_DWORD) << 32)
  220. #endif /* CDX_MCDI_H */