htc_internal.h 13 KB

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