htc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Copyright (c) 2004-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011 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 "common.h"
  20. /* frame header flags */
  21. /* send direction */
  22. #define HTC_FLAGS_NEED_CREDIT_UPDATE (1 << 0)
  23. #define HTC_FLAGS_SEND_BUNDLE (1 << 1)
  24. /* receive direction */
  25. #define HTC_FLG_RX_UNUSED (1 << 0)
  26. #define HTC_FLG_RX_TRAILER (1 << 1)
  27. /* Bundle count maske and shift */
  28. #define HTC_FLG_RX_BNDL_CNT (0xF0)
  29. #define HTC_FLG_RX_BNDL_CNT_S 4
  30. #define HTC_HDR_LENGTH (sizeof(struct htc_frame_hdr))
  31. #define HTC_MAX_PAYLOAD_LENGTH (4096 - sizeof(struct htc_frame_hdr))
  32. /* HTC control message IDs */
  33. #define HTC_MSG_READY_ID 1
  34. #define HTC_MSG_CONN_SVC_ID 2
  35. #define HTC_MSG_CONN_SVC_RESP_ID 3
  36. #define HTC_MSG_SETUP_COMPLETE_ID 4
  37. #define HTC_MSG_SETUP_COMPLETE_EX_ID 5
  38. #define HTC_MAX_CTRL_MSG_LEN 256
  39. #define HTC_VERSION_2P0 0x00
  40. #define HTC_VERSION_2P1 0x01
  41. #define HTC_SERVICE_META_DATA_MAX_LENGTH 128
  42. #define HTC_CONN_FLGS_THRESH_LVL_QUAT 0x0
  43. #define HTC_CONN_FLGS_THRESH_LVL_HALF 0x1
  44. #define HTC_CONN_FLGS_THRESH_LVL_THREE_QUAT 0x2
  45. #define HTC_CONN_FLGS_REDUCE_CRED_DRIB 0x4
  46. #define HTC_CONN_FLGS_THRESH_MASK 0x3
  47. /* connect response status codes */
  48. #define HTC_SERVICE_SUCCESS 0
  49. #define HTC_SERVICE_NOT_FOUND 1
  50. #define HTC_SERVICE_FAILED 2
  51. /* no resources (i.e. no more endpoints) */
  52. #define HTC_SERVICE_NO_RESOURCES 3
  53. /* specific service is not allowing any more endpoints */
  54. #define HTC_SERVICE_NO_MORE_EP 4
  55. /* report record IDs */
  56. #define HTC_RECORD_NULL 0
  57. #define HTC_RECORD_CREDITS 1
  58. #define HTC_RECORD_LOOKAHEAD 2
  59. #define HTC_RECORD_LOOKAHEAD_BUNDLE 3
  60. #define HTC_SETUP_COMP_FLG_RX_BNDL_EN (1 << 0)
  61. #define MAKE_SERVICE_ID(group, index) \
  62. (int)(((int)group << 8) | (int)(index))
  63. /* NOTE: service ID of 0x0000 is reserved and should never be used */
  64. #define HTC_CTRL_RSVD_SVC MAKE_SERVICE_ID(RSVD_SERVICE_GROUP, 1)
  65. #define WMI_CONTROL_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 0)
  66. #define WMI_DATA_BE_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 1)
  67. #define WMI_DATA_BK_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 2)
  68. #define WMI_DATA_VI_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 3)
  69. #define WMI_DATA_VO_SVC MAKE_SERVICE_ID(WMI_SERVICE_GROUP, 4)
  70. #define WMI_MAX_SERVICES 5
  71. #define WMM_NUM_AC 4
  72. /* reserved and used to flush ALL packets */
  73. #define HTC_TX_PACKET_TAG_ALL 0
  74. #define HTC_SERVICE_TX_PACKET_TAG 1
  75. #define HTC_TX_PACKET_TAG_USER_DEFINED (HTC_SERVICE_TX_PACKET_TAG + 9)
  76. /* more packets on this endpoint are being fetched */
  77. #define HTC_RX_FLAGS_INDICATE_MORE_PKTS (1 << 0)
  78. /* TODO.. for BMI */
  79. #define ENDPOINT1 0
  80. /* TODO -remove me, but we have to fix BMI first */
  81. #define HTC_MAILBOX_NUM_MAX 4
  82. /* enable send bundle padding for this endpoint */
  83. #define HTC_FLGS_TX_BNDL_PAD_EN (1 << 0)
  84. #define HTC_EP_ACTIVE ((u32) (1u << 31))
  85. /* HTC operational parameters */
  86. #define HTC_TARGET_RESPONSE_TIMEOUT 2000 /* in ms */
  87. #define HTC_TARGET_DEBUG_INTR_MASK 0x01
  88. #define HTC_TARGET_CREDIT_INTR_MASK 0xF0
  89. #define HTC_HOST_MAX_MSG_PER_BUNDLE 8
  90. #define HTC_MIN_HTC_MSGS_TO_BUNDLE 2
  91. /* packet flags */
  92. #define HTC_RX_PKT_IGNORE_LOOKAHEAD (1 << 0)
  93. #define HTC_RX_PKT_REFRESH_HDR (1 << 1)
  94. #define HTC_RX_PKT_PART_OF_BUNDLE (1 << 2)
  95. #define HTC_RX_PKT_NO_RECYCLE (1 << 3)
  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. struct htc_endpoint *htc_ep;
  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 ath6kl_htc_credit_info {
  339. int total_avail_credits;
  340. int cur_free_credits;
  341. /* list of lowest priority endpoints */
  342. struct list_head lowestpri_ep_dist;
  343. };
  344. /* endpoint statistics */
  345. struct htc_endpoint_stats {
  346. /*
  347. * number of times the host set the credit-low flag in a send
  348. * message on this endpoint
  349. */
  350. u32 cred_low_indicate;
  351. u32 tx_issued;
  352. u32 tx_pkt_bundled;
  353. u32 tx_bundles;
  354. u32 tx_dropped;
  355. /* running count of total credit reports received for this endpoint */
  356. u32 tx_cred_rpt;
  357. /* credit reports received from this endpoint's RX packets */
  358. u32 cred_rpt_from_rx;
  359. /* credit reports received from RX packets of other endpoints */
  360. u32 cred_rpt_from_other;
  361. /* credit reports received from endpoint 0 RX packets */
  362. u32 cred_rpt_ep0;
  363. /* count of credits received via Rx packets on this endpoint */
  364. u32 cred_from_rx;
  365. /* count of credits received via another endpoint */
  366. u32 cred_from_other;
  367. /* count of credits received via another endpoint */
  368. u32 cred_from_ep0;
  369. /* count of consummed credits */
  370. u32 cred_cosumd;
  371. /* count of credits returned */
  372. u32 cred_retnd;
  373. u32 rx_pkts;
  374. /* count of lookahead records found in Rx msg */
  375. u32 rx_lkahds;
  376. /* count of recv packets received in a bundle */
  377. u32 rx_bundl;
  378. /* count of number of bundled lookaheads */
  379. u32 rx_bundle_lkahd;
  380. /* count of the number of bundle indications from the HTC header */
  381. u32 rx_bundle_from_hdr;
  382. /* the number of times the recv allocation threshold was hit */
  383. u32 rx_alloc_thresh_hit;
  384. /* total number of bytes */
  385. u32 rxalloc_thresh_byte;
  386. };
  387. struct htc_endpoint {
  388. enum htc_endpoint_id eid;
  389. u16 svc_id;
  390. struct list_head txq;
  391. struct list_head rx_bufq;
  392. struct htc_endpoint_credit_dist cred_dist;
  393. struct htc_ep_callbacks ep_cb;
  394. int max_txq_depth;
  395. int len_max;
  396. int tx_proc_cnt;
  397. int rx_proc_cnt;
  398. struct htc_target *target;
  399. u8 seqno;
  400. u32 conn_flags;
  401. struct htc_endpoint_stats ep_st;
  402. u16 tx_drop_packet_threshold;
  403. };
  404. struct htc_control_buffer {
  405. struct htc_packet packet;
  406. u8 *buf;
  407. };
  408. struct ath6kl_device;
  409. /* our HTC target state */
  410. struct htc_target {
  411. struct htc_endpoint endpoint[ENDPOINT_MAX];
  412. /* contains struct htc_endpoint_credit_dist */
  413. struct list_head cred_dist_list;
  414. struct list_head free_ctrl_txbuf;
  415. struct list_head free_ctrl_rxbuf;
  416. struct ath6kl_htc_credit_info *credit_info;
  417. int tgt_creds;
  418. unsigned int tgt_cred_sz;
  419. /* protects free_ctrl_txbuf and free_ctrl_rxbuf */
  420. spinlock_t htc_lock;
  421. /* FIXME: does this protext rx_bufq and endpoint structures or what? */
  422. spinlock_t rx_lock;
  423. /* protects endpoint->txq */
  424. spinlock_t tx_lock;
  425. struct ath6kl_device *dev;
  426. u32 htc_flags;
  427. u32 rx_st_flags;
  428. enum htc_endpoint_id ep_waiting;
  429. u8 htc_tgt_ver;
  430. /* max messages per bundle for HTC */
  431. int msg_per_bndl_max;
  432. u32 tx_bndl_mask;
  433. int rx_bndl_enable;
  434. int max_rx_bndl_sz;
  435. int max_tx_bndl_sz;
  436. u32 block_sz;
  437. u32 block_mask;
  438. int max_scat_entries;
  439. int max_xfer_szper_scatreq;
  440. int chk_irq_status_cnt;
  441. /* counts the number of Tx without bundling continously per AC */
  442. u32 ac_tx_count[WMM_NUM_AC];
  443. };
  444. void *ath6kl_htc_create(struct ath6kl *ar);
  445. void ath6kl_htc_set_credit_dist(struct htc_target *target,
  446. struct ath6kl_htc_credit_info *cred_info,
  447. u16 svc_pri_order[], int len);
  448. int ath6kl_htc_wait_target(struct htc_target *target);
  449. int ath6kl_htc_start(struct htc_target *target);
  450. int ath6kl_htc_conn_service(struct htc_target *target,
  451. struct htc_service_connect_req *req,
  452. struct htc_service_connect_resp *resp);
  453. int ath6kl_htc_tx(struct htc_target *target, struct htc_packet *packet);
  454. void ath6kl_htc_stop(struct htc_target *target);
  455. void ath6kl_htc_cleanup(struct htc_target *target);
  456. void ath6kl_htc_flush_txep(struct htc_target *target,
  457. enum htc_endpoint_id endpoint, u16 tag);
  458. void ath6kl_htc_flush_rx_buf(struct htc_target *target);
  459. void ath6kl_htc_indicate_activity_change(struct htc_target *target,
  460. enum htc_endpoint_id endpoint,
  461. bool active);
  462. int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
  463. enum htc_endpoint_id endpoint);
  464. int ath6kl_htc_add_rxbuf_multiple(struct htc_target *target,
  465. struct list_head *pktq);
  466. int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
  467. u32 msg_look_ahead, int *n_pkts);
  468. int ath6kl_credit_setup(void *htc_handle,
  469. struct ath6kl_htc_credit_info *cred_info);
  470. static inline void set_htc_pkt_info(struct htc_packet *packet, void *context,
  471. u8 *buf, unsigned int len,
  472. enum htc_endpoint_id eid, u16 tag)
  473. {
  474. packet->pkt_cntxt = context;
  475. packet->buf = buf;
  476. packet->act_len = len;
  477. packet->endpoint = eid;
  478. packet->info.tx.tag = tag;
  479. }
  480. static inline void htc_rxpkt_reset(struct htc_packet *packet)
  481. {
  482. packet->buf = packet->buf_start;
  483. packet->act_len = 0;
  484. }
  485. static inline void set_htc_rxpkt_info(struct htc_packet *packet, void *context,
  486. u8 *buf, unsigned long len,
  487. enum htc_endpoint_id eid)
  488. {
  489. packet->pkt_cntxt = context;
  490. packet->buf = buf;
  491. packet->buf_start = buf;
  492. packet->buf_len = len;
  493. packet->endpoint = eid;
  494. }
  495. static inline int get_queue_depth(struct list_head *queue)
  496. {
  497. struct list_head *tmp_list;
  498. int depth = 0;
  499. list_for_each(tmp_list, queue)
  500. depth++;
  501. return depth;
  502. }
  503. #endif