htc_internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2013-2016 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 "htc_api.h"
  32. #include "htc_packet.h"
  33. #include <hif.h>
  34. #include <htc.h>
  35. #include <qdf_atomic.h>
  36. #include <qdf_event.h>
  37. #include <qdf_lock.h>
  38. #include <qdf_nbuf.h>
  39. #include <qdf_timer.h>
  40. #include <qdf_types.h>
  41. /* HTC operational parameters */
  42. #define HTC_TARGET_RESPONSE_TIMEOUT 2000 /* in ms */
  43. #define HTC_TARGET_DEBUG_INTR_MASK 0x01
  44. #define HTC_TARGET_CREDIT_INTR_MASK 0xF0
  45. #define HTC_MIN_MSG_PER_BUNDLE 2
  46. #if defined(HIF_USB)
  47. #define HTC_MAX_MSG_PER_BUNDLE_RX 11
  48. #define HTC_MAX_MSG_PER_BUNDLE_TX 8
  49. #else
  50. #define HTC_MAX_MSG_PER_BUNDLE_RX 64
  51. #define HTC_MAX_MSG_PER_BUNDLE 16
  52. #define HTC_MAX_MSG_PER_BUNDLE_TX 32
  53. #endif
  54. /*
  55. * HTC_MAX_TX_BUNDLE_SEND_LIMIT -
  56. * This value is in units of tx frame fragments.
  57. * It needs to be at least as large as the maximum number of tx frames in a
  58. * HTC download bundle times the average number of fragments in each such frame
  59. * (In certain operating systems, such as Linux, we expect to only have
  60. * a single fragment per frame anyway.)
  61. */
  62. #define HTC_MAX_TX_BUNDLE_SEND_LIMIT 255
  63. #define HTC_PACKET_CONTAINER_ALLOCATION 32
  64. #define NUM_CONTROL_TX_BUFFERS 2
  65. #define HTC_CONTROL_BUFFER_SIZE (HTC_MAX_CONTROL_MESSAGE_LENGTH + HTC_HDR_LENGTH)
  66. #define HTC_CONTROL_BUFFER_ALIGN 32
  67. #define HTC_TARGET_RESPONSE_POLL_MS 10
  68. #if !defined(A_SIMOS_DEVHOST)
  69. #define HTC_TARGET_MAX_RESPONSE_POLL 200 /* actual HW */
  70. #else
  71. #define HTC_TARGET_MAX_RESPONSE_POLL 600 /* host + target simulation */
  72. #endif
  73. #define HTC_SERVICE_TX_PACKET_TAG HTC_TX_PACKET_TAG_INTERNAL
  74. #define HTC_CREDIT_HISTORY_MAX 1024
  75. #define HTC_IS_EPPING_ENABLED(_x) ((_x) == QDF_GLOBAL_EPPING_MODE)
  76. typedef enum {
  77. HTC_REQUEST_CREDIT,
  78. HTC_PROCESS_CREDIT_REPORT,
  79. HTC_SUSPEND_ACK,
  80. HTC_SUSPEND_NACK,
  81. HTC_INITIAL_WAKE_UP,
  82. } htc_credit_exchange_type;
  83. typedef struct {
  84. htc_credit_exchange_type type;
  85. uint64_t time;
  86. uint32_t tx_credit;
  87. uint32_t htc_tx_queue_depth;
  88. } HTC_CREDIT_HISTORY;
  89. typedef struct _HTC_ENDPOINT {
  90. HTC_ENDPOINT_ID Id;
  91. /* service ID this endpoint is bound to
  92. * non-zero value means this endpoint is in use
  93. */
  94. HTC_SERVICE_ID service_id;
  95. HTC_EP_CALLBACKS EpCallBacks; /* callbacks associated with this endpoint */
  96. HTC_PACKET_QUEUE TxQueue; /* HTC frame buffer TX queue */
  97. int MaxTxQueueDepth; /* max depth of the TX queue before we need to
  98. call driver's full handler */
  99. int MaxMsgLength; /* max length of endpoint message */
  100. uint8_t UL_PipeID;
  101. uint8_t DL_PipeID;
  102. int ul_is_polled; /* Need to call HIF to get tx completion callbacks? */
  103. qdf_timer_t ul_poll_timer;
  104. int ul_poll_timer_active;
  105. int ul_outstanding_cnt;
  106. int dl_is_polled; /* Need to call HIF to fetch rx? (Not currently supported.) */
  107. #if 0 /* not currently supported */
  108. qdf_timer_t dl_poll_timer;
  109. #endif
  110. HTC_PACKET_QUEUE TxLookupQueue; /* lookup queue to match netbufs to htc packets */
  111. HTC_PACKET_QUEUE RxBufferHoldQueue; /* temporary hold queue for back compatibility */
  112. uint8_t SeqNo; /* TX seq no (helpful) for debugging */
  113. qdf_atomic_t TxProcessCount; /* serialization */
  114. struct _HTC_TARGET *target;
  115. int TxCredits; /* TX credits available on this endpoint */
  116. int TxCreditSize; /* size in bytes of each credit (set by HTC) */
  117. int TxCreditsPerMaxMsg; /* credits required per max message (precalculated) */
  118. #ifdef HTC_EP_STAT_PROFILING
  119. HTC_ENDPOINT_STATS endpoint_stats; /* endpoint statistics */
  120. #endif
  121. bool TxCreditFlowEnabled;
  122. bool async_update; /* packets can be queued asynchronously */
  123. } HTC_ENDPOINT;
  124. #ifdef HTC_EP_STAT_PROFILING
  125. #define INC_HTC_EP_STAT(p, stat, count) ((p)->endpoint_stats.stat += (count))
  126. #else
  127. #define INC_HTC_EP_STAT(p, stat, count)
  128. #endif
  129. typedef struct {
  130. uint16_t service_id;
  131. uint8_t CreditAllocation;
  132. } HTC_SERVICE_TX_CREDIT_ALLOCATION;
  133. #define HTC_MAX_SERVICE_ALLOC_ENTRIES 8
  134. /* Error codes for HTC layer packet stats*/
  135. enum ol_ath_htc_pkt_ecodes {
  136. GET_HTC_PKT_Q_FAIL = 0, /* error- get packet at head of HTC_PACKET_Q */
  137. HTC_PKT_Q_EMPTY,
  138. HTC_SEND_Q_EMPTY
  139. };
  140. /* our HTC target state */
  141. typedef struct _HTC_TARGET {
  142. struct hif_opaque_softc *hif_dev;
  143. HTC_ENDPOINT endpoint[ENDPOINT_MAX];
  144. qdf_spinlock_t HTCLock;
  145. qdf_spinlock_t HTCRxLock;
  146. qdf_spinlock_t HTCTxLock;
  147. qdf_spinlock_t HTCCreditLock;
  148. uint32_t HTCStateFlags;
  149. void *host_handle;
  150. HTC_INIT_INFO HTCInitInfo;
  151. HTC_PACKET *pHTCPacketStructPool; /* pool of HTC packets */
  152. HTC_PACKET_QUEUE ControlBufferTXFreeList;
  153. uint8_t CtrlResponseBuffer[HTC_MAX_CONTROL_MESSAGE_LENGTH];
  154. int CtrlResponseLength;
  155. qdf_event_t ctrl_response_valid;
  156. bool CtrlResponseProcessing;
  157. int TotalTransmitCredits;
  158. HTC_SERVICE_TX_CREDIT_ALLOCATION
  159. ServiceTxAllocTable[HTC_MAX_SERVICE_ALLOC_ENTRIES];
  160. int TargetCreditSize;
  161. #ifdef RX_SG_SUPPORT
  162. qdf_nbuf_queue_t RxSgQueue;
  163. bool IsRxSgInprogress;
  164. uint32_t CurRxSgTotalLen; /* current total length */
  165. uint32_t ExpRxSgTotalLen; /* expected total length */
  166. #endif
  167. qdf_device_t osdev;
  168. struct ol_ath_htc_stats htc_pkt_stats;
  169. HTC_PACKET *pBundleFreeList;
  170. uint32_t ce_send_cnt;
  171. uint32_t TX_comp_cnt;
  172. uint8_t MaxMsgsPerHTCBundle;
  173. qdf_work_t queue_kicker;
  174. #ifdef HIF_SDIO
  175. A_UINT16 AltDataCreditSize;
  176. #endif
  177. A_UINT32 avail_tx_credits;
  178. #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
  179. A_UINT32 rx_bundle_stats[HTC_MAX_MSG_PER_BUNDLE_RX];
  180. A_UINT32 tx_bundle_stats[HTC_MAX_MSG_PER_BUNDLE_TX];
  181. #endif
  182. uint32_t con_mode;
  183. /*
  184. * This flag is from the mboxping tool. It indicates that we cannot
  185. * drop it. Besides, nodrop pkts have higher priority than normal pkts.
  186. */
  187. A_BOOL is_nodrop_pkt;
  188. } HTC_TARGET;
  189. #if defined ENABLE_BUNDLE_TX
  190. #define HTC_TX_BUNDLE_ENABLED(target) (target->MaxMsgsPerHTCBundle > 1)
  191. #else
  192. #define HTC_TX_BUNDLE_ENABLED(target) 0
  193. #endif
  194. #if defined ENABLE_BUNDLE_RX
  195. #define HTC_RX_BUNDLE_ENABLED(target) (target->MaxMsgsPerHTCBundle > 1)
  196. #else
  197. #define HTC_RX_BUNDLE_ENABLED(target) 0
  198. #endif
  199. #define HTC_ENABLE_BUNDLE(target) (target->MaxMsgsPerHTCBundle > 1)
  200. #ifdef RX_SG_SUPPORT
  201. #define RESET_RX_SG_CONFIG(_target) \
  202. _target->ExpRxSgTotalLen = 0; \
  203. _target->CurRxSgTotalLen = 0; \
  204. _target->IsRxSgInprogress = false;
  205. #endif
  206. #define HTC_STATE_STOPPING (1 << 0)
  207. #define HTC_STOPPING(t) ((t)->HTCStateFlags & HTC_STATE_STOPPING)
  208. #define LOCK_HTC(t) qdf_spin_lock_bh(&(t)->HTCLock);
  209. #define UNLOCK_HTC(t) qdf_spin_unlock_bh(&(t)->HTCLock);
  210. #define LOCK_HTC_RX(t) qdf_spin_lock_bh(&(t)->HTCRxLock);
  211. #define UNLOCK_HTC_RX(t) qdf_spin_unlock_bh(&(t)->HTCRxLock);
  212. #define LOCK_HTC_TX(t) qdf_spin_lock_bh(&(t)->HTCTxLock);
  213. #define UNLOCK_HTC_TX(t) qdf_spin_unlock_bh(&(t)->HTCTxLock);
  214. #define LOCK_HTC_CREDIT(t) qdf_spin_lock_bh(&(t)->HTCCreditLock);
  215. #define UNLOCK_HTC_CREDIT(t) qdf_spin_unlock_bh(&(t)->HTCCreditLock);
  216. #define GET_HTC_TARGET_FROM_HANDLE(hnd) ((HTC_TARGET *)(hnd))
  217. #define IS_TX_CREDIT_FLOW_ENABLED(ep) ((ep)->TxCreditFlowEnabled)
  218. #define HTC_POLL_CLEANUP_PERIOD_MS 10 /* milliseconds */
  219. /* Macro to Increment the HTC_PACKET_ERRORS for Tx.*/
  220. #define OL_ATH_HTC_PKT_ERROR_COUNT_INCR(_target, _ecode) \
  221. do { \
  222. if (_ecode == GET_HTC_PKT_Q_FAIL) \
  223. (_target->htc_pkt_stats.htc_get_pkt_q_fail_count) += 1 \
  224. ; \
  225. if (_ecode == HTC_PKT_Q_EMPTY) \
  226. (_target->htc_pkt_stats.htc_pkt_q_empty_count) += 1 \
  227. ; \
  228. if (_ecode == HTC_SEND_Q_EMPTY) \
  229. (_target->htc_pkt_stats.htc_send_q_empty_count) += 1 \
  230. ; \
  231. } while (0);
  232. /* internal HTC functions */
  233. QDF_STATUS htc_rx_completion_handler(void *Context, qdf_nbuf_t netbuf,
  234. uint8_t pipeID);
  235. QDF_STATUS htc_tx_completion_handler(void *Context, qdf_nbuf_t netbuf,
  236. unsigned int transferID, uint32_t toeplitz_hash_result);
  237. HTC_PACKET *allocate_htc_bundle_packet(HTC_TARGET *target);
  238. void free_htc_bundle_packet(HTC_TARGET *target, HTC_PACKET *pPacket);
  239. HTC_PACKET *allocate_htc_packet_container(HTC_TARGET *target);
  240. void free_htc_packet_container(HTC_TARGET *target, HTC_PACKET *pPacket);
  241. void htc_flush_rx_hold_queue(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint);
  242. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  243. HTC_TX_TAG Tag);
  244. void htc_recv_init(HTC_TARGET *target);
  245. A_STATUS htc_wait_recv_ctrl_message(HTC_TARGET *target);
  246. void htc_free_control_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket);
  247. HTC_PACKET *htc_alloc_control_tx_packet(HTC_TARGET *target);
  248. uint8_t htc_get_credit_allocation(HTC_TARGET *target, uint16_t service_id);
  249. void htc_tx_resource_avail_handler(void *context, uint8_t pipeID);
  250. void htc_control_rx_complete(void *Context, HTC_PACKET *pPacket);
  251. void htc_process_credit_rpt(HTC_TARGET *target,
  252. HTC_CREDIT_REPORT *pRpt,
  253. int NumEntries, HTC_ENDPOINT_ID FromEndpoint);
  254. void htc_fw_event_handler(void *context, QDF_STATUS status);
  255. void htc_send_complete_check_cleanup(void *context);
  256. #ifdef FEATURE_RUNTIME_PM
  257. void htc_kick_queues(void *context);
  258. #endif
  259. void htc_credit_record(htc_credit_exchange_type type, uint32_t tx_credit,
  260. uint32_t htc_tx_queue_depth);
  261. static inline void htc_send_complete_poll_timer_stop(HTC_ENDPOINT *
  262. pEndpoint) {
  263. LOCK_HTC_TX(pEndpoint->target);
  264. if (pEndpoint->ul_poll_timer_active) {
  265. /* qdf_timer_stop(&pEndpoint->ul_poll_timer); */
  266. pEndpoint->ul_poll_timer_active = 0;
  267. }
  268. UNLOCK_HTC_TX(pEndpoint->target);
  269. }
  270. static inline void htc_send_complete_poll_timer_start(HTC_ENDPOINT *
  271. pEndpoint) {
  272. LOCK_HTC_TX(pEndpoint->target);
  273. if (pEndpoint->ul_outstanding_cnt
  274. && !pEndpoint->ul_poll_timer_active) {
  275. /*
  276. qdf_timer_start(
  277. &pEndpoint->ul_poll_timer, HTC_POLL_CLEANUP_PERIOD_MS);
  278. */
  279. pEndpoint->ul_poll_timer_active = 1;
  280. }
  281. UNLOCK_HTC_TX(pEndpoint->target);
  282. }
  283. static inline void
  284. htc_send_complete_check(HTC_ENDPOINT *pEndpoint, int force) {
  285. /*
  286. * Stop the polling-cleanup timer that will result in a later call to
  287. * this function. It may get started again below, if there are still
  288. * outsending sends.
  289. */
  290. htc_send_complete_poll_timer_stop(pEndpoint);
  291. /*
  292. * Check whether HIF has any prior sends that have finished,
  293. * have not had the post-processing done.
  294. */
  295. hif_send_complete_check(pEndpoint->target->hif_dev,
  296. pEndpoint->UL_PipeID, force);
  297. /*
  298. * If there are still outstanding sends after polling, start a timer
  299. * to check again a little later.
  300. */
  301. htc_send_complete_poll_timer_start(pEndpoint);
  302. }
  303. #ifdef __cplusplus
  304. }
  305. #endif
  306. #ifndef DEBUG_BUNDLE
  307. #define DEBUG_BUNDLE 0
  308. #endif
  309. #if defined(HIF_SDIO) || defined(HIF_USB)
  310. #ifndef ENABLE_BUNDLE_TX
  311. #define ENABLE_BUNDLE_TX 1
  312. #endif
  313. #ifndef ENABLE_BUNDLE_RX
  314. #define ENABLE_BUNDLE_RX 1
  315. #endif
  316. #endif /*defined(HIF_SDIO) || defined(HIF_USB)*/
  317. #endif /* !_HTC_HOST_INTERNAL_H_ */