htc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright (c) 2004-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef HTC_H
  17. #define HTC_H
  18. #include "common.h"
  19. /* frame header flags */
  20. /* send direction */
  21. #define HTC_FLAGS_NEED_CREDIT_UPDATE (1 << 0)
  22. #define HTC_FLAGS_SEND_BUNDLE (1 << 1)
  23. /* receive direction */
  24. #define HTC_FLG_RX_UNUSED (1 << 0)
  25. #define HTC_FLG_RX_TRAILER (1 << 1)
  26. /* Bundle count maske and shift */
  27. #define HTC_FLG_RX_BNDL_CNT (0xF0)
  28. #define HTC_FLG_RX_BNDL_CNT_S 4
  29. #define HTC_HDR_LENGTH (sizeof(struct htc_frame_hdr))
  30. #define HTC_MAX_PAYLOAD_LENGTH (4096 - sizeof(struct htc_frame_hdr))
  31. /* HTC control message IDs */
  32. #define HTC_MSG_READY_ID 1
  33. #define HTC_MSG_CONN_SVC_ID 2
  34. #define HTC_MSG_CONN_SVC_RESP_ID 3
  35. #define HTC_MSG_SETUP_COMPLETE_ID 4
  36. #define HTC_MSG_SETUP_COMPLETE_EX_ID 5
  37. #define HTC_MAX_CTRL_MSG_LEN 256
  38. #define HTC_VERSION_2P0 0x00
  39. #define HTC_VERSION_2P1 0x01
  40. #define HTC_SERVICE_META_DATA_MAX_LENGTH 128
  41. #define HTC_CONN_FLGS_THRESH_LVL_QUAT 0x0
  42. #define HTC_CONN_FLGS_THRESH_LVL_HALF 0x1
  43. #define HTC_CONN_FLGS_THRESH_LVL_THREE_QUAT 0x2
  44. #define HTC_CONN_FLGS_REDUCE_CRED_DRIB 0x4
  45. #define HTC_CONN_FLGS_THRESH_MASK 0x3
  46. /* connect response status codes */
  47. #define HTC_SERVICE_SUCCESS 0
  48. #define HTC_SERVICE_NOT_FOUND 1
  49. #define HTC_SERVICE_FAILED 2
  50. /* no resources (i.e. no more endpoints) */
  51. #define HTC_SERVICE_NO_RESOURCES 3
  52. /* specific service is not allowing any more endpoints */
  53. #define HTC_SERVICE_NO_MORE_EP 4
  54. /* report record IDs */
  55. #define HTC_RECORD_NULL 0
  56. #define HTC_RECORD_CREDITS 1
  57. #define HTC_RECORD_LOOKAHEAD 2
  58. #define HTC_RECORD_LOOKAHEAD_BUNDLE 3
  59. #define HTC_SETUP_COMP_FLG_RX_BNDL_EN (1 << 0)
  60. #define MAKE_SERVICE_ID(group, index) \
  61. (int)(((int)group << 8) | (int)(index))
  62. /* NOTE: service ID of 0x0000 is reserved and should never be used */
  63. #define HTC_CTRL_RSVD_SVC MAKE_SERVICE_ID(RSVD_SERVICE_GROUP, 1)
  64. #define WMI_CONTROL_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 0)
  65. #define WMI_DATA_BE_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 1)
  66. #define WMI_DATA_BK_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 2)
  67. #define WMI_DATA_VI_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 3)
  68. #define WMI_DATA_VO_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 4)
  69. #define WMI_MAX_SERVICES 5
  70. /* reserved and used to flush ALL packets */
  71. #define HTC_TX_PACKET_TAG_ALL 0
  72. #define HTC_SERVICE_TX_PACKET_TAG 1
  73. #define HTC_TX_PACKET_TAG_USER_DEFINED (HTC_SERVICE_TX_PACKET_TAG + 9)
  74. /* more packets on this endpoint are being fetched */
  75. #define HTC_RX_FLAGS_INDICATE_MORE_PKTS (1 << 0)
  76. /* TODO.. for BMI */
  77. #define ENDPOINT1 0
  78. /* TODO -remove me, but we have to fix BMI first */
  79. #define HTC_MAILBOX_NUM_MAX 4
  80. /* enable send bundle padding for this endpoint */
  81. #define HTC_FLGS_TX_BNDL_PAD_EN (1 << 0)
  82. #define HTC_EP_ACTIVE ((u32) (1u << 31))
  83. /* HTC operational parameters */
  84. #define HTC_TARGET_RESPONSE_TIMEOUT 2000 /* in ms */
  85. #define HTC_TARGET_DEBUG_INTR_MASK 0x01
  86. #define HTC_TARGET_CREDIT_INTR_MASK 0xF0
  87. #define HTC_HOST_MAX_MSG_PER_BUNDLE 8
  88. #define HTC_MIN_HTC_MSGS_TO_BUNDLE 2
  89. /* packet flags */
  90. #define HTC_RX_PKT_IGNORE_LOOKAHEAD (1 << 0)
  91. #define HTC_RX_PKT_REFRESH_HDR (1 << 1)
  92. #define HTC_RX_PKT_PART_OF_BUNDLE (1 << 2)
  93. #define HTC_RX_PKT_NO_RECYCLE (1 << 3)
  94. /* scatter request flags */
  95. #define HTC_SCAT_REQ_FLG_PART_BNDL (1 << 0)
  96. #define NUM_CONTROL_BUFFERS 8
  97. #define NUM_CONTROL_TX_BUFFERS 2
  98. #define NUM_CONTROL_RX_BUFFERS (NUM_CONTROL_BUFFERS - NUM_CONTROL_TX_BUFFERS)
  99. #define HTC_RECV_WAIT_BUFFERS (1 << 0)
  100. #define HTC_OP_STATE_STOPPING (1 << 0)
  101. /*
  102. * The frame header length and message formats defined herein were selected
  103. * to accommodate optimal alignment for target processing. This reduces
  104. * code size and improves performance. Any changes to the header length may
  105. * alter the alignment and cause exceptions on the target. When adding to
  106. * the messagestructures insure that fields are properly aligned.
  107. */
  108. /* HTC frame header
  109. *
  110. * NOTE: do not remove or re-arrange the fields, these are minimally
  111. * required to take advantage of 4-byte lookaheads in some hardware
  112. * implementations.
  113. */
  114. struct htc_frame_hdr {
  115. u8 eid;
  116. u8 flags;
  117. /* length of data (including trailer) that follows the header */
  118. __le16 payld_len;
  119. /* end of 4-byte lookahead */
  120. u8 ctrl[2];
  121. } __packed;
  122. /* HTC ready message */
  123. struct htc_ready_msg {
  124. __le16 msg_id;
  125. __le16 cred_cnt;
  126. __le16 cred_sz;
  127. u8 max_ep;
  128. u8 pad;
  129. } __packed;
  130. /* extended HTC ready message */
  131. struct htc_ready_ext_msg {
  132. struct htc_ready_msg ver2_0_info;
  133. u8 htc_ver;
  134. u8 msg_per_htc_bndl;
  135. } __packed;
  136. /* connect service */
  137. struct htc_conn_service_msg {
  138. __le16 msg_id;
  139. __le16 svc_id;
  140. __le16 conn_flags;
  141. u8 svc_meta_len;
  142. u8 pad;
  143. } __packed;
  144. /* connect response */
  145. struct htc_conn_service_resp {
  146. __le16 msg_id;
  147. __le16 svc_id;
  148. u8 status;
  149. u8 eid;
  150. __le16 max_msg_sz;
  151. u8 svc_meta_len;
  152. u8 pad;
  153. } __packed;
  154. struct htc_setup_comp_msg {
  155. __le16 msg_id;
  156. } __packed;
  157. /* extended setup completion message */
  158. struct htc_setup_comp_ext_msg {
  159. __le16 msg_id;
  160. __le32 flags;
  161. u8 msg_per_rxbndl;
  162. u8 Rsvd[3];
  163. } __packed;
  164. struct htc_record_hdr {
  165. u8 rec_id;
  166. u8 len;
  167. } __packed;
  168. struct htc_credit_report {
  169. u8 eid;
  170. u8 credits;
  171. } __packed;
  172. /*
  173. * NOTE: The lk_ahd array is guarded by a pre_valid
  174. * and Post Valid guard bytes. The pre_valid bytes must
  175. * equal the inverse of the post_valid byte.
  176. */
  177. struct htc_lookahead_report {
  178. u8 pre_valid;
  179. u8 lk_ahd[4];
  180. u8 post_valid;
  181. } __packed;
  182. struct htc_bundle_lkahd_rpt {
  183. u8 lk_ahd[4];
  184. } __packed;
  185. /* Current service IDs */
  186. enum htc_service_grp_ids {
  187. RSVD_SERVICE_GROUP = 0,
  188. WMI_SERVICE_GROUP = 1,
  189. HTC_TEST_GROUP = 254,
  190. HTC_SERVICE_GROUP_LAST = 255
  191. };
  192. /* ------ endpoint IDS ------ */
  193. enum htc_endpoint_id {
  194. ENDPOINT_UNUSED = -1,
  195. ENDPOINT_0 = 0,
  196. ENDPOINT_1 = 1,
  197. ENDPOINT_2 = 2,
  198. ENDPOINT_3,
  199. ENDPOINT_4,
  200. ENDPOINT_5,
  201. ENDPOINT_6,
  202. ENDPOINT_7,
  203. ENDPOINT_8,
  204. ENDPOINT_MAX,
  205. };
  206. struct htc_tx_packet_info {
  207. u16 tag;
  208. int cred_used;
  209. u8 flags;
  210. int seqno;
  211. };
  212. struct htc_rx_packet_info {
  213. u32 exp_hdr;
  214. u32 rx_flags;
  215. u32 indicat_flags;
  216. };
  217. struct htc_target;
  218. /* wrapper around endpoint-specific packets */
  219. struct htc_packet {
  220. struct list_head list;
  221. /* caller's per packet specific context */
  222. void *pkt_cntxt;
  223. /*
  224. * the true buffer start , the caller can store the real
  225. * buffer start here. In receive callbacks, the HTC layer
  226. * sets buf to the start of the payload past the header.
  227. * This field allows the caller to reset buf when it recycles
  228. * receive packets back to HTC.
  229. */
  230. u8 *buf_start;
  231. /*
  232. * Pointer to the start of the buffer. In the transmit
  233. * direction this points to the start of the payload. In the
  234. * receive direction, however, the buffer when queued up
  235. * points to the start of the HTC header but when returned
  236. * to the caller points to the start of the payload
  237. */
  238. u8 *buf;
  239. u32 buf_len;
  240. /* actual length of payload */
  241. u32 act_len;
  242. /* endpoint that this packet was sent/recv'd from */
  243. enum htc_endpoint_id endpoint;
  244. /* completion status */
  245. int status;
  246. union {
  247. struct htc_tx_packet_info tx;
  248. struct htc_rx_packet_info rx;
  249. } info;
  250. void (*completion) (struct htc_target *, struct htc_packet *);
  251. struct htc_target *context;
  252. };
  253. enum htc_send_full_action {
  254. HTC_SEND_FULL_KEEP = 0,
  255. HTC_SEND_FULL_DROP = 1,
  256. };
  257. struct htc_ep_callbacks {
  258. void (*rx) (struct htc_target *, struct htc_packet *);
  259. void (*rx_refill) (struct htc_target *, enum htc_endpoint_id endpoint);
  260. enum htc_send_full_action (*tx_full) (struct htc_target *,
  261. struct htc_packet *);
  262. struct htc_packet *(*rx_allocthresh) (struct htc_target *,
  263. enum htc_endpoint_id, int);
  264. int rx_alloc_thresh;
  265. int rx_refill_thresh;
  266. };
  267. /* service connection information */
  268. struct htc_service_connect_req {
  269. u16 svc_id;
  270. u16 conn_flags;
  271. struct htc_ep_callbacks ep_cb;
  272. int max_txq_depth;
  273. u32 flags;
  274. unsigned int max_rxmsg_sz;
  275. };
  276. /* service connection response information */
  277. struct htc_service_connect_resp {
  278. u8 buf_len;
  279. u8 act_len;
  280. enum htc_endpoint_id endpoint;
  281. unsigned int len_max;
  282. u8 resp_code;
  283. };
  284. /* endpoint distributionstructure */
  285. struct htc_endpoint_credit_dist {
  286. struct list_head list;
  287. /* Service ID (set by HTC) */
  288. u16 svc_id;
  289. /* endpoint for this distributionstruct (set by HTC) */
  290. enum htc_endpoint_id endpoint;
  291. u32 dist_flags;
  292. /*
  293. * credits for normal operation, anything above this
  294. * indicates the endpoint is over-subscribed.
  295. */
  296. int cred_norm;
  297. /* floor for credit distribution */
  298. int cred_min;
  299. int cred_assngd;
  300. /* current credits available */
  301. int credits;
  302. /*
  303. * pending credits to distribute on this endpoint, this
  304. * is set by HTC when credit reports arrive. The credit
  305. * distribution functions sets this to zero when it distributes
  306. * the credits.
  307. */
  308. int cred_to_dist;
  309. /*
  310. * the number of credits that the current pending TX packet needs
  311. * to transmit. This is set by HTC when endpoint needs credits in
  312. * order to transmit.
  313. */
  314. int seek_cred;
  315. /* size in bytes of each credit */
  316. int cred_sz;
  317. /* credits required for a maximum sized messages */
  318. int cred_per_msg;
  319. /* reserved for HTC use */
  320. void *htc_rsvd;
  321. /*
  322. * current depth of TX queue , i.e. messages waiting for credits
  323. * This field is valid only when HTC_CREDIT_DIST_ACTIVITY_CHANGE
  324. * or HTC_CREDIT_DIST_SEND_COMPLETE is indicated on an endpoint
  325. * that has non-zero credits to recover.
  326. */
  327. int txq_depth;
  328. };
  329. /*
  330. * credit distibution code that is passed into the distrbution function,
  331. * there are mandatory and optional codes that must be handled
  332. */
  333. enum htc_credit_dist_reason {
  334. HTC_CREDIT_DIST_SEND_COMPLETE = 0,
  335. HTC_CREDIT_DIST_ACTIVITY_CHANGE = 1,
  336. HTC_CREDIT_DIST_SEEK_CREDITS,
  337. };
  338. struct htc_credit_state_info {
  339. int total_avail_credits;
  340. int cur_free_credits;
  341. struct list_head lowestpri_ep_dist;
  342. };
  343. /* endpoint statistics */
  344. struct htc_endpoint_stats {
  345. /*
  346. * number of times the host set the credit-low flag in a send
  347. * message on this endpoint
  348. */
  349. u32 cred_low_indicate;
  350. u32 tx_issued;
  351. u32 tx_pkt_bundled;
  352. u32 tx_bundles;
  353. u32 tx_dropped;
  354. /* running count of total credit reports received for this endpoint */
  355. u32 tx_cred_rpt;
  356. /* credit reports received from this endpoint's RX packets */
  357. u32 cred_rpt_from_rx;
  358. /* credit reports received from RX packets of other endpoints */
  359. u32 cred_rpt_from_other;
  360. /* credit reports received from endpoint 0 RX packets */
  361. u32 cred_rpt_ep0;
  362. /* count of credits received via Rx packets on this endpoint */
  363. u32 cred_from_rx;
  364. /* count of credits received via another endpoint */
  365. u32 cred_from_other;
  366. /* count of credits received via another endpoint */
  367. u32 cred_from_ep0;
  368. /* count of consummed credits */
  369. u32 cred_cosumd;
  370. /* count of credits returned */
  371. u32 cred_retnd;
  372. u32 rx_pkts;
  373. /* count of lookahead records found in Rx msg */
  374. u32 rx_lkahds;
  375. /* count of recv packets received in a bundle */
  376. u32 rx_bundl;
  377. /* count of number of bundled lookaheads */
  378. u32 rx_bundle_lkahd;
  379. /* count of the number of bundle indications from the HTC header */
  380. u32 rx_bundle_from_hdr;
  381. /* the number of times the recv allocation threshold was hit */
  382. u32 rx_alloc_thresh_hit;
  383. /* total number of bytes */
  384. u32 rxalloc_thresh_byte;
  385. };
  386. struct htc_endpoint {
  387. enum htc_endpoint_id eid;
  388. u16 svc_id;
  389. struct list_head txq;
  390. struct list_head rx_bufq;
  391. struct htc_endpoint_credit_dist cred_dist;
  392. struct htc_ep_callbacks ep_cb;
  393. int max_txq_depth;
  394. int len_max;
  395. int tx_proc_cnt;
  396. int rx_proc_cnt;
  397. struct htc_target *target;
  398. u8 seqno;
  399. u32 conn_flags;
  400. struct htc_endpoint_stats ep_st;
  401. };
  402. struct htc_control_buffer {
  403. struct htc_packet packet;
  404. u8 *buf;
  405. };
  406. struct ath6kl_device;
  407. /* our HTC target state */
  408. struct htc_target {
  409. struct htc_endpoint endpoint[ENDPOINT_MAX];
  410. struct list_head cred_dist_list;
  411. struct list_head free_ctrl_txbuf;
  412. struct list_head free_ctrl_rxbuf;
  413. struct htc_credit_state_info *cred_dist_cntxt;
  414. int tgt_creds;
  415. unsigned int tgt_cred_sz;
  416. spinlock_t htc_lock;
  417. spinlock_t rx_lock;
  418. spinlock_t tx_lock;
  419. struct ath6kl_device *dev;
  420. u32 htc_flags;
  421. u32 rx_st_flags;
  422. enum htc_endpoint_id ep_waiting;
  423. u8 htc_tgt_ver;
  424. /* max messages per bundle for HTC */
  425. int msg_per_bndl_max;
  426. bool tx_bndl_enable;
  427. int rx_bndl_enable;
  428. };
  429. void *htc_create(struct ath6kl *ar);
  430. void htc_set_credit_dist(struct htc_target *target,
  431. struct htc_credit_state_info *cred_info,
  432. u16 svc_pri_order[], int len);
  433. int htc_wait_target(struct htc_target *target);
  434. int htc_start(struct htc_target *target);
  435. int htc_conn_service(struct htc_target *target,
  436. struct htc_service_connect_req *req,
  437. struct htc_service_connect_resp *resp);
  438. int htc_tx(struct htc_target *target, struct htc_packet *packet);
  439. void htc_stop(struct htc_target *target);
  440. void htc_cleanup(struct htc_target *target);
  441. void htc_flush_txep(struct htc_target *target,
  442. enum htc_endpoint_id endpoint, u16 tag);
  443. void htc_flush_rx_buf(struct htc_target *target);
  444. void htc_indicate_activity_change(struct htc_target *target,
  445. enum htc_endpoint_id endpoint, bool active);
  446. int htc_get_rxbuf_num(struct htc_target *target, enum htc_endpoint_id endpoint);
  447. int htc_add_rxbuf_multiple(struct htc_target *target, struct list_head *pktq);
  448. static inline void set_htc_pkt_info(struct htc_packet *packet, void *context,
  449. u8 *buf, unsigned int len,
  450. enum htc_endpoint_id eid, u16 tag)
  451. {
  452. packet->pkt_cntxt = context;
  453. packet->buf = buf;
  454. packet->act_len = len;
  455. packet->endpoint = eid;
  456. packet->info.tx.tag = tag;
  457. }
  458. static inline void htc_rxpkt_reset(struct htc_packet *packet)
  459. {
  460. packet->buf = packet->buf_start;
  461. packet->act_len = 0;
  462. }
  463. static inline void set_htc_rxpkt_info(struct htc_packet *packet, void *context,
  464. u8 *buf, unsigned long len,
  465. enum htc_endpoint_id eid)
  466. {
  467. packet->pkt_cntxt = context;
  468. packet->buf = buf;
  469. packet->buf_start = buf;
  470. packet->buf_len = len;
  471. packet->endpoint = eid;
  472. }
  473. static inline int get_queue_depth(struct list_head *queue)
  474. {
  475. struct list_head *tmp_list;
  476. int depth = 0;
  477. list_for_each(tmp_list, queue)
  478. depth++;
  479. return depth;
  480. }
  481. #endif