mcdi.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2008-2013 Solarflare Communications Inc.
  5. */
  6. #ifndef EFX_MCDI_H
  7. #define EFX_MCDI_H
  8. /**
  9. * enum efx_mcdi_state - MCDI request handling state
  10. * @MCDI_STATE_QUIESCENT: No pending MCDI requests. If the caller holds the
  11. * mcdi @iface_lock then they are able to move to %MCDI_STATE_RUNNING
  12. * @MCDI_STATE_RUNNING_SYNC: There is a synchronous MCDI request pending.
  13. * Only the thread that moved into this state is allowed to move out of it.
  14. * @MCDI_STATE_RUNNING_ASYNC: There is an asynchronous MCDI request pending.
  15. * @MCDI_STATE_PROXY_WAIT: An MCDI request has completed with a response that
  16. * indicates we must wait for a proxy try again message.
  17. * @MCDI_STATE_COMPLETED: An MCDI request has completed, but the owning thread
  18. * has not yet consumed the result. For all other threads, equivalent to
  19. * %MCDI_STATE_RUNNING.
  20. */
  21. enum efx_mcdi_state {
  22. MCDI_STATE_QUIESCENT,
  23. MCDI_STATE_RUNNING_SYNC,
  24. MCDI_STATE_RUNNING_ASYNC,
  25. MCDI_STATE_PROXY_WAIT,
  26. MCDI_STATE_COMPLETED,
  27. };
  28. /**
  29. * enum efx_mcdi_mode - MCDI transaction mode
  30. * @MCDI_MODE_POLL: poll for MCDI completion, until timeout
  31. * @MCDI_MODE_EVENTS: wait for an mcdi_event. On timeout, poll once
  32. * @MCDI_MODE_FAIL: we think MCDI is dead, so fail-fast all calls
  33. */
  34. enum efx_mcdi_mode {
  35. MCDI_MODE_POLL,
  36. MCDI_MODE_EVENTS,
  37. MCDI_MODE_FAIL,
  38. };
  39. /**
  40. * struct efx_mcdi_iface - MCDI protocol context
  41. * @efx: The associated NIC.
  42. * @state: Request handling state. Waited for by @wq.
  43. * @mode: Poll for mcdi completion, or wait for an mcdi_event.
  44. * @wq: Wait queue for threads waiting for @state != %MCDI_STATE_RUNNING
  45. * @new_epoch: Indicates start of day or start of MC reboot recovery
  46. * @iface_lock: Serialises access to @seqno, @credits and response metadata
  47. * @seqno: The next sequence number to use for mcdi requests.
  48. * @credits: Number of spurious MCDI completion events allowed before we
  49. * trigger a fatal error
  50. * @resprc: Response error/success code (Linux numbering)
  51. * @resp_hdr_len: Response header length
  52. * @resp_data_len: Response data (SDU or error) length
  53. * @async_lock: Serialises access to @async_list while event processing is
  54. * enabled
  55. * @async_list: Queue of asynchronous requests
  56. * @async_timer: Timer for asynchronous request timeout
  57. * @logging_buffer: buffer that may be used to build MCDI tracing messages
  58. * @logging_enabled: whether to trace MCDI
  59. * @proxy_rx_handle: Most recently received proxy authorisation handle
  60. * @proxy_rx_status: Status of most recent proxy authorisation
  61. * @proxy_rx_wq: Wait queue for updates to proxy_rx_handle
  62. */
  63. struct efx_mcdi_iface {
  64. struct efx_nic *efx;
  65. enum efx_mcdi_state state;
  66. enum efx_mcdi_mode mode;
  67. wait_queue_head_t wq;
  68. spinlock_t iface_lock;
  69. bool new_epoch;
  70. unsigned int credits;
  71. unsigned int seqno;
  72. int resprc;
  73. int resprc_raw;
  74. size_t resp_hdr_len;
  75. size_t resp_data_len;
  76. spinlock_t async_lock;
  77. struct list_head async_list;
  78. struct timer_list async_timer;
  79. #ifdef CONFIG_SFC_SIENA_MCDI_LOGGING
  80. char *logging_buffer;
  81. bool logging_enabled;
  82. #endif
  83. unsigned int proxy_rx_handle;
  84. int proxy_rx_status;
  85. wait_queue_head_t proxy_rx_wq;
  86. };
  87. struct efx_mcdi_mon {
  88. struct efx_buffer dma_buf;
  89. struct mutex update_lock;
  90. unsigned long last_update;
  91. struct device *device;
  92. struct efx_mcdi_mon_attribute *attrs;
  93. struct attribute_group group;
  94. const struct attribute_group *groups[2];
  95. unsigned int n_attrs;
  96. };
  97. struct efx_mcdi_mtd_partition {
  98. struct efx_mtd_partition common;
  99. bool updating;
  100. u16 nvram_type;
  101. u16 fw_subtype;
  102. };
  103. #define to_efx_mcdi_mtd_partition(mtd) \
  104. container_of(mtd, struct efx_mcdi_mtd_partition, common.mtd)
  105. /**
  106. * struct efx_mcdi_data - extra state for NICs that implement MCDI
  107. * @iface: Interface/protocol state
  108. * @hwmon: Hardware monitor state
  109. * @fn_flags: Flags for this function, as returned by %MC_CMD_DRV_ATTACH.
  110. */
  111. struct efx_mcdi_data {
  112. struct efx_mcdi_iface iface;
  113. #ifdef CONFIG_SFC_SIENA_MCDI_MON
  114. struct efx_mcdi_mon hwmon;
  115. #endif
  116. u32 fn_flags;
  117. };
  118. static inline struct efx_mcdi_iface *efx_mcdi(struct efx_nic *efx)
  119. {
  120. EFX_WARN_ON_PARANOID(!efx->mcdi);
  121. return &efx->mcdi->iface;
  122. }
  123. #ifdef CONFIG_SFC_SIENA_MCDI_MON
  124. static inline struct efx_mcdi_mon *efx_mcdi_mon(struct efx_nic *efx)
  125. {
  126. EFX_WARN_ON_PARANOID(!efx->mcdi);
  127. return &efx->mcdi->hwmon;
  128. }
  129. #endif
  130. int efx_siena_mcdi_init(struct efx_nic *efx);
  131. void efx_siena_mcdi_detach(struct efx_nic *efx);
  132. void efx_siena_mcdi_fini(struct efx_nic *efx);
  133. int efx_siena_mcdi_rpc(struct efx_nic *efx, unsigned int cmd,
  134. const efx_dword_t *inbuf, size_t inlen,
  135. efx_dword_t *outbuf, size_t outlen,
  136. size_t *outlen_actual);
  137. int efx_siena_mcdi_rpc_quiet(struct efx_nic *efx, unsigned int cmd,
  138. const efx_dword_t *inbuf, size_t inlen,
  139. efx_dword_t *outbuf, size_t outlen,
  140. size_t *outlen_actual);
  141. int efx_siena_mcdi_rpc_start(struct efx_nic *efx, unsigned int cmd,
  142. const efx_dword_t *inbuf, size_t inlen);
  143. int efx_siena_mcdi_rpc_finish(struct efx_nic *efx, unsigned int cmd,
  144. size_t inlen, efx_dword_t *outbuf, size_t outlen,
  145. size_t *outlen_actual);
  146. int efx_siena_mcdi_rpc_finish_quiet(struct efx_nic *efx, unsigned int cmd,
  147. size_t inlen, efx_dword_t *outbuf,
  148. size_t outlen, size_t *outlen_actual);
  149. typedef void efx_mcdi_async_completer(struct efx_nic *efx,
  150. unsigned long cookie, int rc,
  151. efx_dword_t *outbuf,
  152. size_t outlen_actual);
  153. int efx_siena_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
  154. const efx_dword_t *inbuf, size_t inlen,
  155. size_t outlen,
  156. efx_mcdi_async_completer *complete,
  157. unsigned long cookie);
  158. int efx_siena_mcdi_rpc_async_quiet(struct efx_nic *efx, unsigned int cmd,
  159. const efx_dword_t *inbuf, size_t inlen,
  160. size_t outlen,
  161. efx_mcdi_async_completer *complete,
  162. unsigned long cookie);
  163. void efx_siena_mcdi_display_error(struct efx_nic *efx, unsigned int cmd,
  164. size_t inlen, efx_dword_t *outbuf,
  165. size_t outlen, int rc);
  166. int efx_siena_mcdi_poll_reboot(struct efx_nic *efx);
  167. void efx_siena_mcdi_mode_poll(struct efx_nic *efx);
  168. void efx_siena_mcdi_mode_event(struct efx_nic *efx);
  169. void efx_siena_mcdi_flush_async(struct efx_nic *efx);
  170. void efx_siena_mcdi_process_event(struct efx_channel *channel, efx_qword_t *event);
  171. void efx_siena_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev);
  172. /* We expect that 16- and 32-bit fields in MCDI requests and responses
  173. * are appropriately aligned, but 64-bit fields are only
  174. * 32-bit-aligned. Also, on Siena we must copy to the MC shared
  175. * memory strictly 32 bits at a time, so add any necessary padding.
  176. */
  177. #define MCDI_TX_BUF_LEN(_len) DIV_ROUND_UP((_len), 4)
  178. #define _MCDI_DECLARE_BUF(_name, _len) \
  179. efx_dword_t _name[DIV_ROUND_UP(_len, 4)]
  180. #define MCDI_DECLARE_BUF(_name, _len) \
  181. _MCDI_DECLARE_BUF(_name, _len) = {{{0}}}
  182. #define MCDI_DECLARE_BUF_ERR(_name) \
  183. MCDI_DECLARE_BUF(_name, 8)
  184. #define _MCDI_PTR(_buf, _offset) \
  185. ((u8 *)(_buf) + (_offset))
  186. #define MCDI_PTR(_buf, _field) \
  187. _MCDI_PTR(_buf, MC_CMD_ ## _field ## _OFST)
  188. #define _MCDI_CHECK_ALIGN(_ofst, _align) \
  189. ((_ofst) + BUILD_BUG_ON_ZERO((_ofst) & (_align - 1)))
  190. #define _MCDI_DWORD(_buf, _field) \
  191. ((_buf) + (_MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, 4) >> 2))
  192. #define MCDI_BYTE(_buf, _field) \
  193. ((void)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 1), \
  194. *MCDI_PTR(_buf, _field))
  195. #define MCDI_WORD(_buf, _field) \
  196. ((u16)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2) + \
  197. le16_to_cpu(*(__force const __le16 *)MCDI_PTR(_buf, _field)))
  198. #define MCDI_SET_DWORD(_buf, _field, _value) \
  199. EFX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), EFX_DWORD_0, _value)
  200. #define MCDI_DWORD(_buf, _field) \
  201. EFX_DWORD_FIELD(*_MCDI_DWORD(_buf, _field), EFX_DWORD_0)
  202. #define MCDI_POPULATE_DWORD_1(_buf, _field, _name1, _value1) \
  203. EFX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), \
  204. MC_CMD_ ## _name1, _value1)
  205. #define MCDI_POPULATE_DWORD_2(_buf, _field, _name1, _value1, \
  206. _name2, _value2) \
  207. EFX_POPULATE_DWORD_2(*_MCDI_DWORD(_buf, _field), \
  208. MC_CMD_ ## _name1, _value1, \
  209. MC_CMD_ ## _name2, _value2)
  210. #define MCDI_POPULATE_DWORD_3(_buf, _field, _name1, _value1, \
  211. _name2, _value2, _name3, _value3) \
  212. EFX_POPULATE_DWORD_3(*_MCDI_DWORD(_buf, _field), \
  213. MC_CMD_ ## _name1, _value1, \
  214. MC_CMD_ ## _name2, _value2, \
  215. MC_CMD_ ## _name3, _value3)
  216. #define MCDI_POPULATE_DWORD_4(_buf, _field, _name1, _value1, \
  217. _name2, _value2, _name3, _value3, \
  218. _name4, _value4) \
  219. EFX_POPULATE_DWORD_4(*_MCDI_DWORD(_buf, _field), \
  220. MC_CMD_ ## _name1, _value1, \
  221. MC_CMD_ ## _name2, _value2, \
  222. MC_CMD_ ## _name3, _value3, \
  223. MC_CMD_ ## _name4, _value4)
  224. #define MCDI_POPULATE_DWORD_5(_buf, _field, _name1, _value1, \
  225. _name2, _value2, _name3, _value3, \
  226. _name4, _value4, _name5, _value5) \
  227. EFX_POPULATE_DWORD_5(*_MCDI_DWORD(_buf, _field), \
  228. MC_CMD_ ## _name1, _value1, \
  229. MC_CMD_ ## _name2, _value2, \
  230. MC_CMD_ ## _name3, _value3, \
  231. MC_CMD_ ## _name4, _value4, \
  232. MC_CMD_ ## _name5, _value5)
  233. #define MCDI_POPULATE_DWORD_6(_buf, _field, _name1, _value1, \
  234. _name2, _value2, _name3, _value3, \
  235. _name4, _value4, _name5, _value5, \
  236. _name6, _value6) \
  237. EFX_POPULATE_DWORD_6(*_MCDI_DWORD(_buf, _field), \
  238. MC_CMD_ ## _name1, _value1, \
  239. MC_CMD_ ## _name2, _value2, \
  240. MC_CMD_ ## _name3, _value3, \
  241. MC_CMD_ ## _name4, _value4, \
  242. MC_CMD_ ## _name5, _value5, \
  243. MC_CMD_ ## _name6, _value6)
  244. #define MCDI_POPULATE_DWORD_7(_buf, _field, _name1, _value1, \
  245. _name2, _value2, _name3, _value3, \
  246. _name4, _value4, _name5, _value5, \
  247. _name6, _value6, _name7, _value7) \
  248. EFX_POPULATE_DWORD_7(*_MCDI_DWORD(_buf, _field), \
  249. MC_CMD_ ## _name1, _value1, \
  250. MC_CMD_ ## _name2, _value2, \
  251. MC_CMD_ ## _name3, _value3, \
  252. MC_CMD_ ## _name4, _value4, \
  253. MC_CMD_ ## _name5, _value5, \
  254. MC_CMD_ ## _name6, _value6, \
  255. MC_CMD_ ## _name7, _value7)
  256. #define MCDI_SET_QWORD(_buf, _field, _value) \
  257. do { \
  258. EFX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[0], \
  259. EFX_DWORD_0, (u32)(_value)); \
  260. EFX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[1], \
  261. EFX_DWORD_0, (u64)(_value) >> 32); \
  262. } while (0)
  263. #define MCDI_QWORD(_buf, _field) \
  264. (EFX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[0], EFX_DWORD_0) | \
  265. (u64)EFX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[1], EFX_DWORD_0) << 32)
  266. #define MCDI_FIELD(_ptr, _type, _field) \
  267. EFX_EXTRACT_DWORD( \
  268. *(efx_dword_t *) \
  269. _MCDI_PTR(_ptr, MC_CMD_ ## _type ## _ ## _field ## _OFST & ~3),\
  270. MC_CMD_ ## _type ## _ ## _field ## _LBN & 0x1f, \
  271. (MC_CMD_ ## _type ## _ ## _field ## _LBN & 0x1f) + \
  272. MC_CMD_ ## _type ## _ ## _field ## _WIDTH - 1)
  273. #define _MCDI_ARRAY_PTR(_buf, _field, _index, _align) \
  274. (_MCDI_PTR(_buf, _MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, _align))\
  275. + (_index) * _MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _LEN, _align))
  276. #define MCDI_DECLARE_STRUCT_PTR(_name) \
  277. efx_dword_t *_name
  278. #define MCDI_ARRAY_STRUCT_PTR(_buf, _field, _index) \
  279. ((efx_dword_t *)_MCDI_ARRAY_PTR(_buf, _field, _index, 4))
  280. #define MCDI_VAR_ARRAY_LEN(_len, _field) \
  281. min_t(size_t, MC_CMD_ ## _field ## _MAXNUM, \
  282. ((_len) - MC_CMD_ ## _field ## _OFST) / MC_CMD_ ## _field ## _LEN)
  283. #define MCDI_ARRAY_WORD(_buf, _field, _index) \
  284. (BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2) + \
  285. le16_to_cpu(*(__force const __le16 *) \
  286. _MCDI_ARRAY_PTR(_buf, _field, _index, 2)))
  287. #define _MCDI_ARRAY_DWORD(_buf, _field, _index) \
  288. (BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 4) + \
  289. (efx_dword_t *)_MCDI_ARRAY_PTR(_buf, _field, _index, 4))
  290. #define MCDI_SET_ARRAY_DWORD(_buf, _field, _index, _value) \
  291. EFX_SET_DWORD_FIELD(*_MCDI_ARRAY_DWORD(_buf, _field, _index), \
  292. EFX_DWORD_0, _value)
  293. #define MCDI_ARRAY_DWORD(_buf, _field, _index) \
  294. EFX_DWORD_FIELD(*_MCDI_ARRAY_DWORD(_buf, _field, _index), EFX_DWORD_0)
  295. #define _MCDI_ARRAY_QWORD(_buf, _field, _index) \
  296. (BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 8) + \
  297. (efx_dword_t *)_MCDI_ARRAY_PTR(_buf, _field, _index, 4))
  298. #define MCDI_SET_ARRAY_QWORD(_buf, _field, _index, _value) \
  299. do { \
  300. EFX_SET_DWORD_FIELD(_MCDI_ARRAY_QWORD(_buf, _field, _index)[0],\
  301. EFX_DWORD_0, (u32)(_value)); \
  302. EFX_SET_DWORD_FIELD(_MCDI_ARRAY_QWORD(_buf, _field, _index)[1],\
  303. EFX_DWORD_0, (u64)(_value) >> 32); \
  304. } while (0)
  305. #define MCDI_ARRAY_FIELD(_buf, _field1, _type, _index, _field2) \
  306. MCDI_FIELD(MCDI_ARRAY_STRUCT_PTR(_buf, _field1, _index), \
  307. _type ## _TYPEDEF, _field2)
  308. #define MCDI_EVENT_FIELD(_ev, _field) \
  309. EFX_QWORD_FIELD(_ev, MCDI_EVENT_ ## _field)
  310. #define MCDI_CAPABILITY(field) \
  311. MC_CMD_GET_CAPABILITIES_V8_OUT_ ## field ## _LBN
  312. #define MCDI_CAPABILITY_OFST(field) \
  313. MC_CMD_GET_CAPABILITIES_V8_OUT_ ## field ## _OFST
  314. #define efx_has_cap(efx, field) \
  315. efx->type->check_caps(efx, \
  316. MCDI_CAPABILITY(field), \
  317. MCDI_CAPABILITY_OFST(field))
  318. void efx_siena_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len);
  319. int efx_siena_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
  320. u16 *fw_subtype_list, u32 *capabilities);
  321. int efx_siena_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart,
  322. u32 dest_evq);
  323. int efx_siena_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out);
  324. int efx_siena_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
  325. size_t *size_out, size_t *erase_size_out,
  326. bool *protected_out);
  327. int efx_siena_mcdi_nvram_test_all(struct efx_nic *efx);
  328. int efx_siena_mcdi_handle_assertion(struct efx_nic *efx);
  329. int efx_siena_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode);
  330. int efx_siena_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac,
  331. int *id_out);
  332. int efx_siena_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out);
  333. int efx_siena_mcdi_wol_filter_remove(struct efx_nic *efx, int id);
  334. int efx_siena_mcdi_wol_filter_reset(struct efx_nic *efx);
  335. int efx_siena_mcdi_flush_rxqs(struct efx_nic *efx);
  336. void efx_siena_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev);
  337. void efx_siena_mcdi_mac_start_stats(struct efx_nic *efx);
  338. void efx_siena_mcdi_mac_stop_stats(struct efx_nic *efx);
  339. void efx_siena_mcdi_mac_pull_stats(struct efx_nic *efx);
  340. enum reset_type efx_siena_mcdi_map_reset_reason(enum reset_type reason);
  341. int efx_siena_mcdi_reset(struct efx_nic *efx, enum reset_type method);
  342. #ifdef CONFIG_SFC_SIENA_MCDI_MON
  343. int efx_siena_mcdi_mon_probe(struct efx_nic *efx);
  344. void efx_siena_mcdi_mon_remove(struct efx_nic *efx);
  345. #else
  346. static inline int efx_siena_mcdi_mon_probe(struct efx_nic *efx) { return 0; }
  347. static inline void efx_siena_mcdi_mon_remove(struct efx_nic *efx) {}
  348. #endif
  349. #ifdef CONFIG_SFC_SIENA_MTD
  350. int efx_siena_mcdi_mtd_read(struct mtd_info *mtd, loff_t start, size_t len,
  351. size_t *retlen, u8 *buffer);
  352. int efx_siena_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len);
  353. int efx_siena_mcdi_mtd_write(struct mtd_info *mtd, loff_t start, size_t len,
  354. size_t *retlen, const u8 *buffer);
  355. int efx_siena_mcdi_mtd_sync(struct mtd_info *mtd);
  356. void efx_siena_mcdi_mtd_rename(struct efx_mtd_partition *part);
  357. #endif
  358. #endif /* EFX_MCDI_H */