htc.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifndef _HTC_H_
  18. #define _HTC_H_
  19. #include <linux/kernel.h>
  20. #include <linux/list.h>
  21. #include <linux/bug.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/timer.h>
  24. struct ath10k;
  25. /****************/
  26. /* HTC protocol */
  27. /****************/
  28. /*
  29. * HTC - host-target control protocol
  30. *
  31. * tx packets are generally <htc_hdr><payload>
  32. * rx packets are more complex: <htc_hdr><payload><trailer>
  33. *
  34. * The payload + trailer length is stored in len.
  35. * To get payload-only length one needs to payload - trailer_len.
  36. *
  37. * Trailer contains (possibly) multiple <htc_record>.
  38. * Each record is a id-len-value.
  39. *
  40. * HTC header flags, control_byte0, control_byte1
  41. * have different meaning depending whether its tx
  42. * or rx.
  43. *
  44. * Alignment: htc_hdr, payload and trailer are
  45. * 4-byte aligned.
  46. */
  47. enum ath10k_htc_tx_flags {
  48. ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE = 0x01,
  49. ATH10K_HTC_FLAG_SEND_BUNDLE = 0x02
  50. };
  51. enum ath10k_htc_rx_flags {
  52. ATH10K_HTC_FLAG_TRAILER_PRESENT = 0x02,
  53. ATH10K_HTC_FLAG_BUNDLE_MASK = 0xF0
  54. };
  55. struct ath10k_htc_hdr {
  56. u8 eid; /* @enum ath10k_htc_ep_id */
  57. u8 flags; /* @enum ath10k_htc_tx_flags, ath10k_htc_rx_flags */
  58. __le16 len;
  59. union {
  60. u8 trailer_len; /* for rx */
  61. u8 control_byte0;
  62. } __packed;
  63. union {
  64. u8 seq_no; /* for tx */
  65. u8 control_byte1;
  66. } __packed;
  67. u8 pad0;
  68. u8 pad1;
  69. } __packed __aligned(4);
  70. enum ath10k_ath10k_htc_msg_id {
  71. ATH10K_HTC_MSG_READY_ID = 1,
  72. ATH10K_HTC_MSG_CONNECT_SERVICE_ID = 2,
  73. ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID = 3,
  74. ATH10K_HTC_MSG_SETUP_COMPLETE_ID = 4,
  75. ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID = 5,
  76. ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE = 6
  77. };
  78. enum ath10k_htc_version {
  79. ATH10K_HTC_VERSION_2P0 = 0x00, /* 2.0 */
  80. ATH10K_HTC_VERSION_2P1 = 0x01, /* 2.1 */
  81. };
  82. enum ath10k_htc_conn_flags {
  83. ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_FOURTH = 0x0,
  84. ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_HALF = 0x1,
  85. ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_THREE_FOURTHS = 0x2,
  86. ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_UNITY = 0x3,
  87. #define ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_MASK 0x3
  88. ATH10K_HTC_CONN_FLAGS_REDUCE_CREDIT_DRIBBLE = 1 << 2,
  89. ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL = 1 << 3
  90. #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_MASK 0xFF00
  91. #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_LSB 8
  92. };
  93. enum ath10k_htc_conn_svc_status {
  94. ATH10K_HTC_CONN_SVC_STATUS_SUCCESS = 0,
  95. ATH10K_HTC_CONN_SVC_STATUS_NOT_FOUND = 1,
  96. ATH10K_HTC_CONN_SVC_STATUS_FAILED = 2,
  97. ATH10K_HTC_CONN_SVC_STATUS_NO_RESOURCES = 3,
  98. ATH10K_HTC_CONN_SVC_STATUS_NO_MORE_EP = 4
  99. };
  100. struct ath10k_ath10k_htc_msg_hdr {
  101. __le16 message_id; /* @enum htc_message_id */
  102. } __packed;
  103. struct ath10k_htc_unknown {
  104. u8 pad0;
  105. u8 pad1;
  106. } __packed;
  107. struct ath10k_htc_ready {
  108. __le16 credit_count;
  109. __le16 credit_size;
  110. u8 max_endpoints;
  111. u8 pad0;
  112. } __packed;
  113. struct ath10k_htc_ready_extended {
  114. struct ath10k_htc_ready base;
  115. u8 htc_version; /* @enum ath10k_htc_version */
  116. u8 max_msgs_per_htc_bundle;
  117. u8 pad0;
  118. u8 pad1;
  119. } __packed;
  120. struct ath10k_htc_conn_svc {
  121. __le16 service_id;
  122. __le16 flags; /* @enum ath10k_htc_conn_flags */
  123. u8 pad0;
  124. u8 pad1;
  125. } __packed;
  126. struct ath10k_htc_conn_svc_response {
  127. __le16 service_id;
  128. u8 status; /* @enum ath10k_htc_conn_svc_status */
  129. u8 eid;
  130. __le16 max_msg_size;
  131. } __packed;
  132. struct ath10k_htc_setup_complete_extended {
  133. u8 pad0;
  134. u8 pad1;
  135. __le32 flags; /* @enum htc_setup_complete_flags */
  136. u8 max_msgs_per_bundled_recv;
  137. u8 pad2;
  138. u8 pad3;
  139. u8 pad4;
  140. } __packed;
  141. struct ath10k_htc_msg {
  142. struct ath10k_ath10k_htc_msg_hdr hdr;
  143. union {
  144. /* host-to-target */
  145. struct ath10k_htc_conn_svc connect_service;
  146. struct ath10k_htc_ready ready;
  147. struct ath10k_htc_ready_extended ready_ext;
  148. struct ath10k_htc_unknown unknown;
  149. struct ath10k_htc_setup_complete_extended setup_complete_ext;
  150. /* target-to-host */
  151. struct ath10k_htc_conn_svc_response connect_service_response;
  152. };
  153. } __packed __aligned(4);
  154. enum ath10k_ath10k_htc_record_id {
  155. ATH10K_HTC_RECORD_NULL = 0,
  156. ATH10K_HTC_RECORD_CREDITS = 1
  157. };
  158. struct ath10k_ath10k_htc_record_hdr {
  159. u8 id; /* @enum ath10k_ath10k_htc_record_id */
  160. u8 len;
  161. u8 pad0;
  162. u8 pad1;
  163. } __packed;
  164. struct ath10k_htc_credit_report {
  165. u8 eid; /* @enum ath10k_htc_ep_id */
  166. u8 credits;
  167. u8 pad0;
  168. u8 pad1;
  169. } __packed;
  170. struct ath10k_htc_record {
  171. struct ath10k_ath10k_htc_record_hdr hdr;
  172. union {
  173. struct ath10k_htc_credit_report credit_report[0];
  174. u8 pauload[0];
  175. };
  176. } __packed __aligned(4);
  177. /*
  178. * note: the trailer offset is dynamic depending
  179. * on payload length. this is only a struct layout draft
  180. */
  181. struct ath10k_htc_frame {
  182. struct ath10k_htc_hdr hdr;
  183. union {
  184. struct ath10k_htc_msg msg;
  185. u8 payload[0];
  186. };
  187. struct ath10k_htc_record trailer[0];
  188. } __packed __aligned(4);
  189. /*******************/
  190. /* Host-side stuff */
  191. /*******************/
  192. enum ath10k_htc_svc_gid {
  193. ATH10K_HTC_SVC_GRP_RSVD = 0,
  194. ATH10K_HTC_SVC_GRP_WMI = 1,
  195. ATH10K_HTC_SVC_GRP_NMI = 2,
  196. ATH10K_HTC_SVC_GRP_HTT = 3,
  197. ATH10K_HTC_SVC_GRP_TEST = 254,
  198. ATH10K_HTC_SVC_GRP_LAST = 255,
  199. };
  200. #define SVC(group, idx) \
  201. (int)(((int)(group) << 8) | (int)(idx))
  202. enum ath10k_htc_svc_id {
  203. /* NOTE: service ID of 0x0000 is reserved and should never be used */
  204. ATH10K_HTC_SVC_ID_RESERVED = 0x0000,
  205. ATH10K_HTC_SVC_ID_UNUSED = ATH10K_HTC_SVC_ID_RESERVED,
  206. ATH10K_HTC_SVC_ID_RSVD_CTRL = SVC(ATH10K_HTC_SVC_GRP_RSVD, 1),
  207. ATH10K_HTC_SVC_ID_WMI_CONTROL = SVC(ATH10K_HTC_SVC_GRP_WMI, 0),
  208. ATH10K_HTC_SVC_ID_WMI_DATA_BE = SVC(ATH10K_HTC_SVC_GRP_WMI, 1),
  209. ATH10K_HTC_SVC_ID_WMI_DATA_BK = SVC(ATH10K_HTC_SVC_GRP_WMI, 2),
  210. ATH10K_HTC_SVC_ID_WMI_DATA_VI = SVC(ATH10K_HTC_SVC_GRP_WMI, 3),
  211. ATH10K_HTC_SVC_ID_WMI_DATA_VO = SVC(ATH10K_HTC_SVC_GRP_WMI, 4),
  212. ATH10K_HTC_SVC_ID_NMI_CONTROL = SVC(ATH10K_HTC_SVC_GRP_NMI, 0),
  213. ATH10K_HTC_SVC_ID_NMI_DATA = SVC(ATH10K_HTC_SVC_GRP_NMI, 1),
  214. ATH10K_HTC_SVC_ID_HTT_DATA_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 0),
  215. /* raw stream service (i.e. flash, tcmd, calibration apps) */
  216. ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS = SVC(ATH10K_HTC_SVC_GRP_TEST, 0),
  217. };
  218. #undef SVC
  219. enum ath10k_htc_ep_id {
  220. ATH10K_HTC_EP_UNUSED = -1,
  221. ATH10K_HTC_EP_0 = 0,
  222. ATH10K_HTC_EP_1 = 1,
  223. ATH10K_HTC_EP_2,
  224. ATH10K_HTC_EP_3,
  225. ATH10K_HTC_EP_4,
  226. ATH10K_HTC_EP_5,
  227. ATH10K_HTC_EP_6,
  228. ATH10K_HTC_EP_7,
  229. ATH10K_HTC_EP_8,
  230. ATH10K_HTC_EP_COUNT,
  231. };
  232. struct ath10k_htc_ops {
  233. void (*target_send_suspend_complete)(struct ath10k *ar);
  234. };
  235. struct ath10k_htc_ep_ops {
  236. void (*ep_tx_complete)(struct ath10k *, struct sk_buff *);
  237. void (*ep_rx_complete)(struct ath10k *, struct sk_buff *);
  238. void (*ep_tx_credits)(struct ath10k *);
  239. };
  240. /* service connection information */
  241. struct ath10k_htc_svc_conn_req {
  242. u16 service_id;
  243. struct ath10k_htc_ep_ops ep_ops;
  244. int max_send_queue_depth;
  245. };
  246. /* service connection response information */
  247. struct ath10k_htc_svc_conn_resp {
  248. u8 buffer_len;
  249. u8 actual_len;
  250. enum ath10k_htc_ep_id eid;
  251. unsigned int max_msg_len;
  252. u8 connect_resp_code;
  253. };
  254. #define ATH10K_NUM_CONTROL_TX_BUFFERS 2
  255. #define ATH10K_HTC_MAX_LEN 4096
  256. #define ATH10K_HTC_MAX_CTRL_MSG_LEN 256
  257. #define ATH10K_HTC_WAIT_TIMEOUT_HZ (1 * HZ)
  258. #define ATH10K_HTC_CONTROL_BUFFER_SIZE (ATH10K_HTC_MAX_CTRL_MSG_LEN + \
  259. sizeof(struct ath10k_htc_hdr))
  260. #define ATH10K_HTC_CONN_SVC_TIMEOUT_HZ (1 * HZ)
  261. struct ath10k_htc_ep {
  262. struct ath10k_htc *htc;
  263. enum ath10k_htc_ep_id eid;
  264. enum ath10k_htc_svc_id service_id;
  265. struct ath10k_htc_ep_ops ep_ops;
  266. int max_tx_queue_depth;
  267. int max_ep_message_len;
  268. u8 ul_pipe_id;
  269. u8 dl_pipe_id;
  270. u8 seq_no; /* for debugging */
  271. int tx_credits;
  272. int tx_credit_size;
  273. int tx_credits_per_max_message;
  274. bool tx_credit_flow_enabled;
  275. };
  276. struct ath10k_htc_svc_tx_credits {
  277. u16 service_id;
  278. u8 credit_allocation;
  279. };
  280. struct ath10k_htc {
  281. struct ath10k *ar;
  282. struct ath10k_htc_ep endpoint[ATH10K_HTC_EP_COUNT];
  283. /* protects endpoints */
  284. spinlock_t tx_lock;
  285. struct ath10k_htc_ops htc_ops;
  286. u8 control_resp_buffer[ATH10K_HTC_MAX_CTRL_MSG_LEN];
  287. int control_resp_len;
  288. struct completion ctl_resp;
  289. int total_transmit_credits;
  290. struct ath10k_htc_svc_tx_credits service_tx_alloc[ATH10K_HTC_EP_COUNT];
  291. int target_credit_size;
  292. };
  293. int ath10k_htc_init(struct ath10k *ar);
  294. int ath10k_htc_wait_target(struct ath10k_htc *htc);
  295. int ath10k_htc_start(struct ath10k_htc *htc);
  296. int ath10k_htc_connect_service(struct ath10k_htc *htc,
  297. struct ath10k_htc_svc_conn_req *conn_req,
  298. struct ath10k_htc_svc_conn_resp *conn_resp);
  299. int ath10k_htc_send(struct ath10k_htc *htc, enum ath10k_htc_ep_id eid,
  300. struct sk_buff *packet);
  301. struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size);
  302. void ath10k_htc_tx_completion_handler(struct ath10k *ar, struct sk_buff *skb);
  303. void ath10k_htc_rx_completion_handler(struct ath10k *ar, struct sk_buff *skb);
  304. #endif