htc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* SPDX-License-Identifier: ISC */
  2. /*
  3. * Copyright (c) 2005-2011 Atheros Communications Inc.
  4. * Copyright (c) 2011-2016 Qualcomm Atheros, Inc.
  5. */
  6. #ifndef _HTC_H_
  7. #define _HTC_H_
  8. #include <linux/kernel.h>
  9. #include <linux/list.h>
  10. #include <linux/bug.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/timer.h>
  13. #include <linux/bitfield.h>
  14. struct ath10k;
  15. /****************/
  16. /* HTC protocol */
  17. /****************/
  18. /*
  19. * HTC - host-target control protocol
  20. *
  21. * tx packets are generally <htc_hdr><payload>
  22. * rx packets are more complex: <htc_hdr><payload><trailer>
  23. *
  24. * The payload + trailer length is stored in len.
  25. * To get payload-only length one needs to payload - trailer_len.
  26. *
  27. * Trailer contains (possibly) multiple <htc_record>.
  28. * Each record is a id-len-value.
  29. *
  30. * HTC header flags, control_byte0, control_byte1
  31. * have different meaning depending whether its tx
  32. * or rx.
  33. *
  34. * Alignment: htc_hdr, payload and trailer are
  35. * 4-byte aligned.
  36. */
  37. #define HTC_HOST_MAX_MSG_PER_RX_BUNDLE 32
  38. enum ath10k_htc_tx_flags {
  39. ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE = 0x01,
  40. ATH10K_HTC_FLAG_SEND_BUNDLE = 0x02
  41. };
  42. enum ath10k_htc_rx_flags {
  43. ATH10K_HTC_FLAGS_RECV_1MORE_BLOCK = 0x01,
  44. ATH10K_HTC_FLAG_TRAILER_PRESENT = 0x02,
  45. };
  46. #define ATH10K_HTC_FLAG_BUNDLE_MASK GENMASK(7, 4)
  47. /* bits 2-3 are for extra bundle count bits 4-5 */
  48. #define ATH10K_HTC_BUNDLE_EXTRA_MASK GENMASK(3, 2)
  49. #define ATH10K_HTC_BUNDLE_EXTRA_SHIFT 4
  50. static inline unsigned int ath10k_htc_get_bundle_count(u8 max_msgs, u8 flags)
  51. {
  52. unsigned int count, extra_count = 0;
  53. count = FIELD_GET(ATH10K_HTC_FLAG_BUNDLE_MASK, flags);
  54. if (max_msgs > 16)
  55. extra_count = FIELD_GET(ATH10K_HTC_BUNDLE_EXTRA_MASK, flags) <<
  56. ATH10K_HTC_BUNDLE_EXTRA_SHIFT;
  57. return count + extra_count;
  58. }
  59. struct ath10k_htc_hdr {
  60. u8 eid; /* @enum ath10k_htc_ep_id */
  61. u8 flags; /* @enum ath10k_htc_tx_flags, ath10k_htc_rx_flags */
  62. __le16 len;
  63. union {
  64. u8 trailer_len; /* for rx */
  65. u8 control_byte0;
  66. } __packed;
  67. union {
  68. u8 seq_no; /* for tx */
  69. u8 control_byte1;
  70. } __packed;
  71. union {
  72. __le16 pad_len;
  73. struct {
  74. u8 pad0;
  75. u8 pad1;
  76. } __packed;
  77. } __packed;
  78. } __packed __aligned(4);
  79. enum ath10k_ath10k_htc_msg_id {
  80. ATH10K_HTC_MSG_READY_ID = 1,
  81. ATH10K_HTC_MSG_CONNECT_SERVICE_ID = 2,
  82. ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID = 3,
  83. ATH10K_HTC_MSG_SETUP_COMPLETE_ID = 4,
  84. ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID = 5,
  85. ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE = 6
  86. };
  87. enum ath10k_htc_version {
  88. ATH10K_HTC_VERSION_2P0 = 0x00, /* 2.0 */
  89. ATH10K_HTC_VERSION_2P1 = 0x01, /* 2.1 */
  90. };
  91. enum ath10k_htc_conn_flags {
  92. ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_FOURTH = 0x0,
  93. ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_HALF = 0x1,
  94. ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_THREE_FOURTHS = 0x2,
  95. ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_UNITY = 0x3,
  96. #define ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_MASK 0x3
  97. ATH10K_HTC_CONN_FLAGS_REDUCE_CREDIT_DRIBBLE = 1 << 2,
  98. ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL = 1 << 3
  99. #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_MASK 0xFF00
  100. #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_LSB 8
  101. };
  102. #define ATH10K_HTC_MSG_READY_EXT_ALT_DATA_MASK 0xFFF
  103. enum ath10k_htc_conn_svc_status {
  104. ATH10K_HTC_CONN_SVC_STATUS_SUCCESS = 0,
  105. ATH10K_HTC_CONN_SVC_STATUS_NOT_FOUND = 1,
  106. ATH10K_HTC_CONN_SVC_STATUS_FAILED = 2,
  107. ATH10K_HTC_CONN_SVC_STATUS_NO_RESOURCES = 3,
  108. ATH10K_HTC_CONN_SVC_STATUS_NO_MORE_EP = 4
  109. };
  110. #define ATH10K_MAX_MSG_PER_HTC_TX_BUNDLE 32
  111. #define ATH10K_MIN_MSG_PER_HTC_TX_BUNDLE 2
  112. #define ATH10K_MIN_CREDIT_PER_HTC_TX_BUNDLE 2
  113. enum ath10k_htc_setup_complete_flags {
  114. ATH10K_HTC_SETUP_COMPLETE_FLAGS_RX_BNDL_EN = 1
  115. };
  116. struct ath10k_ath10k_htc_msg_hdr {
  117. __le16 message_id; /* @enum htc_message_id */
  118. } __packed;
  119. struct ath10k_htc_unknown {
  120. u8 pad0;
  121. u8 pad1;
  122. } __packed;
  123. struct ath10k_htc_ready {
  124. __le16 credit_count;
  125. __le16 credit_size;
  126. u8 max_endpoints;
  127. u8 pad0;
  128. } __packed;
  129. struct ath10k_htc_ready_extended {
  130. struct ath10k_htc_ready base;
  131. u8 htc_version; /* @enum ath10k_htc_version */
  132. u8 max_msgs_per_htc_bundle;
  133. union {
  134. __le16 reserved;
  135. struct {
  136. u8 pad0;
  137. u8 pad1;
  138. } __packed;
  139. } __packed;
  140. } __packed;
  141. struct ath10k_htc_conn_svc {
  142. __le16 service_id;
  143. __le16 flags; /* @enum ath10k_htc_conn_flags */
  144. u8 pad0;
  145. u8 pad1;
  146. } __packed;
  147. struct ath10k_htc_conn_svc_response {
  148. __le16 service_id;
  149. u8 status; /* @enum ath10k_htc_conn_svc_status */
  150. u8 eid;
  151. __le16 max_msg_size;
  152. } __packed;
  153. struct ath10k_htc_setup_complete_extended {
  154. u8 pad0;
  155. u8 pad1;
  156. __le32 flags; /* @enum htc_setup_complete_flags */
  157. u8 max_msgs_per_bundled_recv;
  158. u8 pad2;
  159. u8 pad3;
  160. u8 pad4;
  161. } __packed;
  162. struct ath10k_htc_msg {
  163. struct ath10k_ath10k_htc_msg_hdr hdr;
  164. union {
  165. /* host-to-target */
  166. struct ath10k_htc_conn_svc connect_service;
  167. struct ath10k_htc_ready ready;
  168. struct ath10k_htc_ready_extended ready_ext;
  169. struct ath10k_htc_unknown unknown;
  170. struct ath10k_htc_setup_complete_extended setup_complete_ext;
  171. /* target-to-host */
  172. struct ath10k_htc_conn_svc_response connect_service_response;
  173. };
  174. } __packed __aligned(4);
  175. enum ath10k_ath10k_htc_record_id {
  176. ATH10K_HTC_RECORD_NULL = 0,
  177. ATH10K_HTC_RECORD_CREDITS = 1,
  178. ATH10K_HTC_RECORD_LOOKAHEAD = 2,
  179. ATH10K_HTC_RECORD_LOOKAHEAD_BUNDLE = 3,
  180. };
  181. struct ath10k_ath10k_htc_record_hdr {
  182. u8 id; /* @enum ath10k_ath10k_htc_record_id */
  183. u8 len;
  184. u8 pad0;
  185. u8 pad1;
  186. } __packed;
  187. struct ath10k_htc_credit_report {
  188. u8 eid; /* @enum ath10k_htc_ep_id */
  189. u8 credits;
  190. u8 pad0;
  191. u8 pad1;
  192. } __packed;
  193. struct ath10k_htc_lookahead_report {
  194. u8 pre_valid;
  195. u8 pad0;
  196. u8 pad1;
  197. u8 pad2;
  198. u8 lookahead[4];
  199. u8 post_valid;
  200. u8 pad3;
  201. u8 pad4;
  202. u8 pad5;
  203. } __packed;
  204. struct ath10k_htc_lookahead_bundle {
  205. u8 lookahead[4];
  206. } __packed;
  207. struct ath10k_htc_record {
  208. struct ath10k_ath10k_htc_record_hdr hdr;
  209. union {
  210. struct ath10k_htc_credit_report credit_report[0];
  211. struct ath10k_htc_lookahead_report lookahead_report[0];
  212. struct ath10k_htc_lookahead_bundle lookahead_bundle[0];
  213. u8 pauload[0];
  214. };
  215. } __packed __aligned(4);
  216. /*
  217. * note: the trailer offset is dynamic depending
  218. * on payload length. this is only a struct layout draft
  219. */
  220. struct ath10k_htc_frame {
  221. struct ath10k_htc_hdr hdr;
  222. union {
  223. struct ath10k_htc_msg msg;
  224. u8 payload[0];
  225. };
  226. struct ath10k_htc_record trailer[0];
  227. } __packed __aligned(4);
  228. /*******************/
  229. /* Host-side stuff */
  230. /*******************/
  231. enum ath10k_htc_svc_gid {
  232. ATH10K_HTC_SVC_GRP_RSVD = 0,
  233. ATH10K_HTC_SVC_GRP_WMI = 1,
  234. ATH10K_HTC_SVC_GRP_NMI = 2,
  235. ATH10K_HTC_SVC_GRP_HTT = 3,
  236. ATH10K_LOG_SERVICE_GROUP = 6,
  237. ATH10K_HTC_SVC_GRP_TEST = 254,
  238. ATH10K_HTC_SVC_GRP_LAST = 255,
  239. };
  240. #define SVC(group, idx) \
  241. (int)(((int)(group) << 8) | (int)(idx))
  242. enum ath10k_htc_svc_id {
  243. /* NOTE: service ID of 0x0000 is reserved and should never be used */
  244. ATH10K_HTC_SVC_ID_RESERVED = 0x0000,
  245. ATH10K_HTC_SVC_ID_UNUSED = ATH10K_HTC_SVC_ID_RESERVED,
  246. ATH10K_HTC_SVC_ID_RSVD_CTRL = SVC(ATH10K_HTC_SVC_GRP_RSVD, 1),
  247. ATH10K_HTC_SVC_ID_WMI_CONTROL = SVC(ATH10K_HTC_SVC_GRP_WMI, 0),
  248. ATH10K_HTC_SVC_ID_WMI_DATA_BE = SVC(ATH10K_HTC_SVC_GRP_WMI, 1),
  249. ATH10K_HTC_SVC_ID_WMI_DATA_BK = SVC(ATH10K_HTC_SVC_GRP_WMI, 2),
  250. ATH10K_HTC_SVC_ID_WMI_DATA_VI = SVC(ATH10K_HTC_SVC_GRP_WMI, 3),
  251. ATH10K_HTC_SVC_ID_WMI_DATA_VO = SVC(ATH10K_HTC_SVC_GRP_WMI, 4),
  252. ATH10K_HTC_SVC_ID_NMI_CONTROL = SVC(ATH10K_HTC_SVC_GRP_NMI, 0),
  253. ATH10K_HTC_SVC_ID_NMI_DATA = SVC(ATH10K_HTC_SVC_GRP_NMI, 1),
  254. ATH10K_HTC_SVC_ID_HTT_DATA_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 0),
  255. ATH10K_HTC_SVC_ID_HTT_DATA2_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 1),
  256. ATH10K_HTC_SVC_ID_HTT_DATA3_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 2),
  257. ATH10K_HTC_SVC_ID_HTT_LOG_MSG = SVC(ATH10K_LOG_SERVICE_GROUP, 0),
  258. /* raw stream service (i.e. flash, tcmd, calibration apps) */
  259. ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS = SVC(ATH10K_HTC_SVC_GRP_TEST, 0),
  260. };
  261. #undef SVC
  262. enum ath10k_htc_ep_id {
  263. ATH10K_HTC_EP_UNUSED = -1,
  264. ATH10K_HTC_EP_0 = 0,
  265. ATH10K_HTC_EP_1 = 1,
  266. ATH10K_HTC_EP_2,
  267. ATH10K_HTC_EP_3,
  268. ATH10K_HTC_EP_4,
  269. ATH10K_HTC_EP_5,
  270. ATH10K_HTC_EP_6,
  271. ATH10K_HTC_EP_7,
  272. ATH10K_HTC_EP_8,
  273. ATH10K_HTC_EP_COUNT,
  274. };
  275. struct ath10k_htc_ops {
  276. void (*target_send_suspend_complete)(struct ath10k *ar);
  277. };
  278. struct ath10k_htc_ep_ops {
  279. void (*ep_tx_complete)(struct ath10k *, struct sk_buff *);
  280. void (*ep_rx_complete)(struct ath10k *, struct sk_buff *);
  281. void (*ep_tx_credits)(struct ath10k *);
  282. };
  283. /* service connection information */
  284. struct ath10k_htc_svc_conn_req {
  285. u16 service_id;
  286. struct ath10k_htc_ep_ops ep_ops;
  287. int max_send_queue_depth;
  288. };
  289. /* service connection response information */
  290. struct ath10k_htc_svc_conn_resp {
  291. u8 buffer_len;
  292. u8 actual_len;
  293. enum ath10k_htc_ep_id eid;
  294. unsigned int max_msg_len;
  295. u8 connect_resp_code;
  296. };
  297. #define ATH10K_NUM_CONTROL_TX_BUFFERS 2
  298. #define ATH10K_HTC_MAX_LEN 4096
  299. #define ATH10K_HTC_MAX_CTRL_MSG_LEN 256
  300. #define ATH10K_HTC_WAIT_TIMEOUT_HZ (1 * HZ)
  301. #define ATH10K_HTC_CONTROL_BUFFER_SIZE (ATH10K_HTC_MAX_CTRL_MSG_LEN + \
  302. sizeof(struct ath10k_htc_hdr))
  303. #define ATH10K_HTC_CONN_SVC_TIMEOUT_HZ (1 * HZ)
  304. struct ath10k_htc_ep {
  305. struct ath10k_htc *htc;
  306. enum ath10k_htc_ep_id eid;
  307. enum ath10k_htc_svc_id service_id;
  308. struct ath10k_htc_ep_ops ep_ops;
  309. int max_tx_queue_depth;
  310. int max_ep_message_len;
  311. u8 ul_pipe_id;
  312. u8 dl_pipe_id;
  313. u8 seq_no; /* for debugging */
  314. int tx_credits;
  315. int tx_credit_size;
  316. bool tx_credit_flow_enabled;
  317. bool bundle_tx;
  318. struct sk_buff_head tx_req_head;
  319. struct sk_buff_head tx_complete_head;
  320. };
  321. struct ath10k_htc_svc_tx_credits {
  322. u16 service_id;
  323. u8 credit_allocation;
  324. };
  325. struct ath10k_htc {
  326. struct ath10k *ar;
  327. struct ath10k_htc_ep endpoint[ATH10K_HTC_EP_COUNT];
  328. /* protects endpoints */
  329. spinlock_t tx_lock;
  330. struct ath10k_htc_ops htc_ops;
  331. u8 control_resp_buffer[ATH10K_HTC_MAX_CTRL_MSG_LEN];
  332. int control_resp_len;
  333. struct completion ctl_resp;
  334. int total_transmit_credits;
  335. int target_credit_size;
  336. u8 max_msgs_per_htc_bundle;
  337. int alt_data_credit_size;
  338. };
  339. int ath10k_htc_init(struct ath10k *ar);
  340. int ath10k_htc_wait_target(struct ath10k_htc *htc);
  341. void ath10k_htc_setup_tx_req(struct ath10k_htc_ep *ep);
  342. int ath10k_htc_start(struct ath10k_htc *htc);
  343. int ath10k_htc_connect_service(struct ath10k_htc *htc,
  344. struct ath10k_htc_svc_conn_req *conn_req,
  345. struct ath10k_htc_svc_conn_resp *conn_resp);
  346. void ath10k_htc_change_tx_credit_flow(struct ath10k_htc *htc,
  347. enum ath10k_htc_ep_id eid,
  348. bool enable);
  349. int ath10k_htc_send(struct ath10k_htc *htc, enum ath10k_htc_ep_id eid,
  350. struct sk_buff *packet);
  351. void ath10k_htc_stop_hl(struct ath10k *ar);
  352. int ath10k_htc_send_hl(struct ath10k_htc *htc, enum ath10k_htc_ep_id eid,
  353. struct sk_buff *packet);
  354. struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size);
  355. void ath10k_htc_tx_completion_handler(struct ath10k *ar, struct sk_buff *skb);
  356. void ath10k_htc_rx_completion_handler(struct ath10k *ar, struct sk_buff *skb);
  357. void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
  358. struct sk_buff *skb);
  359. int ath10k_htc_process_trailer(struct ath10k_htc *htc,
  360. u8 *buffer,
  361. int length,
  362. enum ath10k_htc_ep_id src_eid,
  363. void *next_lookaheads,
  364. int *next_lookaheads_len);
  365. #endif