htc_internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2013-2015 The Linux Foundation. All rights reserved.
  3. *
  4. * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
  5. *
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for
  8. * any purpose with or without fee is hereby granted, provided that the
  9. * above copyright notice and this permission notice appear in all
  10. * copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  13. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  15. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  16. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  17. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19. * PERFORMANCE OF THIS SOFTWARE.
  20. */
  21. /*
  22. * This file was originally distributed by Qualcomm Atheros, Inc.
  23. * under proprietary terms before Copyright ownership was assigned
  24. * to the Linux Foundation.
  25. */
  26. #ifndef _HTC_INTERNAL_H_
  27. #define _HTC_INTERNAL_H_
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif /* __cplusplus */
  31. #include <athdefs.h>
  32. #include "a_types.h"
  33. #include "osapi_linux.h"
  34. #include <cdf_nbuf.h>
  35. #include <cdf_types.h>
  36. #include <cdf_lock.h>
  37. #include <cdf_softirq_timer.h>
  38. #include <cdf_atomic.h>
  39. #include "hif.h"
  40. #include <htc.h>
  41. #include "htc_api.h"
  42. #include "htc_packet.h"
  43. /* HTC operational parameters */
  44. #define HTC_TARGET_RESPONSE_TIMEOUT 2000 /* in ms */
  45. #define HTC_TARGET_DEBUG_INTR_MASK 0x01
  46. #define HTC_TARGET_CREDIT_INTR_MASK 0xF0
  47. #define HTC_MIN_MSG_PER_BUNDLE 2
  48. #if defined(HIF_USB)
  49. #define HTC_MAX_MSG_PER_BUNDLE 9
  50. #else
  51. #define HTC_MAX_MSG_PER_BUNDLE 16
  52. #endif
  53. /*
  54. * HTC_MAX_TX_BUNDLE_SEND_LIMIT -
  55. * This value is in units of tx frame fragments.
  56. * It needs to be at least as large as the maximum number of tx frames in a
  57. * HTC download bundle times the average number of fragments in each such frame
  58. * (In certain operating systems, such as Linux, we expect to only have
  59. * a single fragment per frame anyway.)
  60. */
  61. #define HTC_MAX_TX_BUNDLE_SEND_LIMIT 255
  62. #define HTC_PACKET_CONTAINER_ALLOCATION 32
  63. #define NUM_CONTROL_TX_BUFFERS 2
  64. #define HTC_CONTROL_BUFFER_SIZE (HTC_MAX_CONTROL_MESSAGE_LENGTH + HTC_HDR_LENGTH)
  65. #define HTC_CONTROL_BUFFER_ALIGN 32
  66. #define HTC_TARGET_RESPONSE_POLL_MS 10
  67. #if !defined(A_SIMOS_DEVHOST)
  68. #define HTC_TARGET_MAX_RESPONSE_POLL 200 /* actual HW */
  69. #else
  70. #define HTC_TARGET_MAX_RESPONSE_POLL 600 /* host + target simulation */
  71. #endif
  72. #define HTC_SERVICE_TX_PACKET_TAG HTC_TX_PACKET_TAG_INTERNAL
  73. #define HTC_CREDIT_HISTORY_MAX 1024
  74. typedef enum {
  75. HTC_REQUEST_CREDIT,
  76. HTC_PROCESS_CREDIT_REPORT,
  77. HTC_SUSPEND_ACK,
  78. HTC_SUSPEND_NACK,
  79. } htc_credit_exchange_type;
  80. typedef struct {
  81. htc_credit_exchange_type type;
  82. uint64_t time;
  83. uint32_t tx_credit;
  84. uint32_t htc_tx_queue_depth;
  85. } HTC_CREDIT_HISTORY;
  86. typedef struct _HTC_ENDPOINT {
  87. HTC_ENDPOINT_ID Id;
  88. HTC_SERVICE_ID ServiceID; /* service ID this endpoint is bound to
  89. non-zero value means this endpoint is in use */
  90. HTC_EP_CALLBACKS EpCallBacks; /* callbacks associated with this endpoint */
  91. HTC_PACKET_QUEUE TxQueue; /* HTC frame buffer TX queue */
  92. int MaxTxQueueDepth; /* max depth of the TX queue before we need to
  93. call driver's full handler */
  94. int MaxMsgLength; /* max length of endpoint message */
  95. uint8_t UL_PipeID;
  96. uint8_t DL_PipeID;
  97. int ul_is_polled; /* Need to call HIF to get tx completion callbacks? */
  98. cdf_softirq_timer_t ul_poll_timer;
  99. int ul_poll_timer_active;
  100. int ul_outstanding_cnt;
  101. int dl_is_polled; /* Need to call HIF to fetch rx? (Not currently supported.) */
  102. #if 0 /* not currently supported */
  103. cdf_softirq_timer_t dl_poll_timer;
  104. #endif
  105. HTC_PACKET_QUEUE TxLookupQueue; /* lookup queue to match netbufs to htc packets */
  106. HTC_PACKET_QUEUE RxBufferHoldQueue; /* temporary hold queue for back compatibility */
  107. A_UINT8 SeqNo; /* TX seq no (helpful) for debugging */
  108. cdf_atomic_t TxProcessCount; /* serialization */
  109. struct _HTC_TARGET *target;
  110. int TxCredits; /* TX credits available on this endpoint */
  111. int TxCreditSize; /* size in bytes of each credit (set by HTC) */
  112. int TxCreditsPerMaxMsg; /* credits required per max message (precalculated) */
  113. #ifdef HTC_EP_STAT_PROFILING
  114. HTC_ENDPOINT_STATS EndPointStats; /* endpoint statistics */
  115. #endif
  116. A_BOOL TxCreditFlowEnabled;
  117. } HTC_ENDPOINT;
  118. #ifdef HTC_EP_STAT_PROFILING
  119. #define INC_HTC_EP_STAT(p,stat,count) (p)->EndPointStats.stat += (count);
  120. #else
  121. #define INC_HTC_EP_STAT(p,stat,count)
  122. #endif
  123. typedef struct {
  124. A_UINT16 ServiceID;
  125. A_UINT8 CreditAllocation;
  126. } HTC_SERVICE_TX_CREDIT_ALLOCATION;
  127. #define HTC_MAX_SERVICE_ALLOC_ENTRIES 8
  128. /* Error codes for HTC layer packet stats*/
  129. enum ol_ath_htc_pkt_ecodes {
  130. GET_HTC_PKT_Q_FAIL = 0, /* error- get packet at head of HTC_PACKET_Q */
  131. HTC_PKT_Q_EMPTY,
  132. HTC_SEND_Q_EMPTY
  133. };
  134. /* our HTC target state */
  135. typedef struct _HTC_TARGET {
  136. struct ol_softc *hif_dev;
  137. HTC_ENDPOINT EndPoint[ENDPOINT_MAX];
  138. cdf_spinlock_t HTCLock;
  139. cdf_spinlock_t HTCRxLock;
  140. cdf_spinlock_t HTCTxLock;
  141. cdf_spinlock_t HTCCreditLock;
  142. A_UINT32 HTCStateFlags;
  143. void *host_handle;
  144. HTC_INIT_INFO HTCInitInfo;
  145. HTC_PACKET *pHTCPacketStructPool; /* pool of HTC packets */
  146. HTC_PACKET_QUEUE ControlBufferTXFreeList;
  147. A_UINT8 CtrlResponseBuffer[HTC_MAX_CONTROL_MESSAGE_LENGTH];
  148. int CtrlResponseLength;
  149. cdf_event_t ctrl_response_valid;
  150. A_BOOL CtrlResponseProcessing;
  151. int TotalTransmitCredits;
  152. HTC_SERVICE_TX_CREDIT_ALLOCATION
  153. ServiceTxAllocTable[HTC_MAX_SERVICE_ALLOC_ENTRIES];
  154. int TargetCreditSize;
  155. #ifdef RX_SG_SUPPORT
  156. cdf_nbuf_queue_t RxSgQueue;
  157. A_BOOL IsRxSgInprogress;
  158. A_UINT32 CurRxSgTotalLen; /* current total length */
  159. A_UINT32 ExpRxSgTotalLen; /* expected total length */
  160. #endif
  161. cdf_device_t osdev;
  162. struct ol_ath_htc_stats htc_pkt_stats;
  163. HTC_PACKET *pBundleFreeList;
  164. A_UINT32 ce_send_cnt;
  165. A_UINT32 TX_comp_cnt;
  166. A_UINT8 MaxMsgsPerHTCBundle;
  167. } HTC_TARGET;
  168. #define HTC_ENABLE_BUNDLE(target) (target->MaxMsgsPerHTCBundle > 1)
  169. #ifdef RX_SG_SUPPORT
  170. #define RESET_RX_SG_CONFIG(_target) \
  171. _target->ExpRxSgTotalLen = 0; \
  172. _target->CurRxSgTotalLen = 0; \
  173. _target->IsRxSgInprogress = false;
  174. #endif
  175. #define HTC_STATE_STOPPING (1 << 0)
  176. #define HTC_STOPPING(t) ((t)->HTCStateFlags & HTC_STATE_STOPPING)
  177. #define LOCK_HTC(t) cdf_spin_lock_bh(&(t)->HTCLock);
  178. #define UNLOCK_HTC(t) cdf_spin_unlock_bh(&(t)->HTCLock);
  179. #define LOCK_HTC_RX(t) cdf_spin_lock_bh(&(t)->HTCRxLock);
  180. #define UNLOCK_HTC_RX(t) cdf_spin_unlock_bh(&(t)->HTCRxLock);
  181. #define LOCK_HTC_TX(t) cdf_spin_lock_bh(&(t)->HTCTxLock);
  182. #define UNLOCK_HTC_TX(t) cdf_spin_unlock_bh(&(t)->HTCTxLock);
  183. #define LOCK_HTC_CREDIT(t) cdf_spin_lock_bh(&(t)->HTCCreditLock);
  184. #define UNLOCK_HTC_CREDIT(t) cdf_spin_unlock_bh(&(t)->HTCCreditLock);
  185. #define GET_HTC_TARGET_FROM_HANDLE(hnd) ((HTC_TARGET *)(hnd))
  186. #define IS_TX_CREDIT_FLOW_ENABLED(ep) ((ep)->TxCreditFlowEnabled)
  187. #define HTC_POLL_CLEANUP_PERIOD_MS 10 /* milliseconds */
  188. /* Macro to Increment the HTC_PACKET_ERRORS for Tx.*/
  189. #define OL_ATH_HTC_PKT_ERROR_COUNT_INCR(_target,_ecode) \
  190. do { \
  191. if(_ecode==GET_HTC_PKT_Q_FAIL) (_target->htc_pkt_stats.htc_get_pkt_q_fail_count)+=1; \
  192. if(_ecode==HTC_PKT_Q_EMPTY) (_target->htc_pkt_stats.htc_pkt_q_empty_count)+=1; \
  193. if(_ecode==HTC_SEND_Q_EMPTY) (_target->htc_pkt_stats.htc_send_q_empty_count)+=1; \
  194. } while(0);
  195. /* internal HTC functions */
  196. CDF_STATUS htc_rx_completion_handler(void *Context, cdf_nbuf_t netbuf,
  197. uint8_t pipeID);
  198. CDF_STATUS htc_tx_completion_handler(void *Context, cdf_nbuf_t netbuf,
  199. unsigned int transferID, uint32_t toeplitz_hash_result);
  200. HTC_PACKET *allocate_htc_bundle_packet(HTC_TARGET *target);
  201. void free_htc_bundle_packet(HTC_TARGET *target, HTC_PACKET *pPacket);
  202. HTC_PACKET *allocate_htc_packet_container(HTC_TARGET *target);
  203. void free_htc_packet_container(HTC_TARGET *target, HTC_PACKET *pPacket);
  204. void htc_flush_rx_hold_queue(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint);
  205. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  206. HTC_TX_TAG Tag);
  207. void htc_recv_init(HTC_TARGET *target);
  208. A_STATUS htc_wait_recv_ctrl_message(HTC_TARGET *target);
  209. void htc_free_control_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket);
  210. HTC_PACKET *htc_alloc_control_tx_packet(HTC_TARGET *target);
  211. A_UINT8 htc_get_credit_allocation(HTC_TARGET *target, A_UINT16 ServiceID);
  212. void htc_tx_resource_avail_handler(void *context, A_UINT8 pipeID);
  213. void htc_control_rx_complete(void *Context, HTC_PACKET *pPacket);
  214. void htc_process_credit_rpt(HTC_TARGET *target,
  215. HTC_CREDIT_REPORT *pRpt,
  216. int NumEntries, HTC_ENDPOINT_ID FromEndpoint);
  217. void htc_fw_event_handler(void *context, CDF_STATUS status);
  218. void htc_send_complete_check_cleanup(void *context);
  219. void htc_credit_record(htc_credit_exchange_type type, uint32_t tx_credit,
  220. uint32_t htc_tx_queue_depth);
  221. static inline void htc_send_complete_poll_timer_stop(HTC_ENDPOINT *
  222. pEndpoint) {
  223. LOCK_HTC_TX(pEndpoint->target);
  224. if (pEndpoint->ul_poll_timer_active) {
  225. /* cdf_softirq_timer_cancel(&pEndpoint->ul_poll_timer); */
  226. pEndpoint->ul_poll_timer_active = 0;
  227. }
  228. UNLOCK_HTC_TX(pEndpoint->target);
  229. }
  230. static inline void htc_send_complete_poll_timer_start(HTC_ENDPOINT *
  231. pEndpoint) {
  232. LOCK_HTC_TX(pEndpoint->target);
  233. if (pEndpoint->ul_outstanding_cnt
  234. && !pEndpoint->ul_poll_timer_active) {
  235. /*
  236. cdf_softirq_timer_start(
  237. &pEndpoint->ul_poll_timer, HTC_POLL_CLEANUP_PERIOD_MS);
  238. */
  239. pEndpoint->ul_poll_timer_active = 1;
  240. }
  241. UNLOCK_HTC_TX(pEndpoint->target);
  242. }
  243. static inline void
  244. htc_send_complete_check(HTC_ENDPOINT *pEndpoint, int force) {
  245. /*
  246. * Stop the polling-cleanup timer that will result in a later call to
  247. * this function. It may get started again below, if there are still
  248. * outsending sends.
  249. */
  250. htc_send_complete_poll_timer_stop(pEndpoint);
  251. /*
  252. * Check whether HIF has any prior sends that have finished,
  253. * have not had the post-processing done.
  254. */
  255. hif_send_complete_check(pEndpoint->target->hif_dev,
  256. pEndpoint->UL_PipeID, force);
  257. /*
  258. * If there are still outstanding sends after polling, start a timer
  259. * to check again a little later.
  260. */
  261. htc_send_complete_poll_timer_start(pEndpoint);
  262. }
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. #ifndef DEBUG_BUNDLE
  267. #define DEBUG_BUNDLE 0
  268. #endif
  269. #ifdef HIF_SDIO
  270. #ifndef ENABLE_BUNDLE_TX
  271. #define ENABLE_BUNDLE_TX 1
  272. #endif
  273. #ifndef ENABLE_BUNDLE_RX
  274. #define ENABLE_BUNDLE_RX 1
  275. #endif
  276. #endif /* HIF_SDIO */
  277. #endif /* !_HTC_HOST_INTERNAL_H_ */