htc_internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (c) 2013-2021 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for
  6. * any purpose with or without fee is hereby granted, provided that the
  7. * above copyright notice and this permission notice appear in all
  8. * copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  13. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  14. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  15. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #ifndef _HTC_INTERNAL_H_
  20. #define _HTC_INTERNAL_H_
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif /* __cplusplus */
  24. #include "htc_api.h"
  25. #include "htc_packet.h"
  26. #include <hif.h>
  27. #include <htc.h>
  28. #include <qdf_atomic.h>
  29. #include <qdf_event.h>
  30. #include <qdf_lock.h>
  31. #include <qdf_nbuf.h>
  32. #include <qdf_timer.h>
  33. #include <qdf_types.h>
  34. /* HTC operational parameters */
  35. #define HTC_TARGET_RESPONSE_TIMEOUT 2000 /* in ms */
  36. #define HTC_TARGET_DEBUG_INTR_MASK 0x01
  37. #define HTC_TARGET_CREDIT_INTR_MASK 0xF0
  38. #define HTC_MIN_MSG_PER_BUNDLE 2
  39. #if defined(HIF_USB)
  40. #define HTC_MAX_MSG_PER_BUNDLE_RX 11
  41. #if defined(CFG_HTC_MAX_MSG_PER_BUNDLE_TX)
  42. #define HTC_MAX_MSG_PER_BUNDLE_TX CFG_HTC_MAX_MSG_PER_BUNDLE_TX
  43. #else
  44. #define HTC_MAX_MSG_PER_BUNDLE_TX 8
  45. #endif /* CFG_HTC_MAX_MSG_PER_BUNDLE_TX */
  46. #else
  47. #define HTC_MAX_MSG_PER_BUNDLE_RX 64
  48. #define HTC_MAX_MSG_PER_BUNDLE 16
  49. #define HTC_MAX_MSG_PER_BUNDLE_TX 32
  50. #endif
  51. #ifdef HIF_SDIO
  52. #define UPDATE_ALT_CREDIT(tar, val) (tar->AltDataCreditSize = (uint16_t) val)
  53. #else
  54. #define UPDATE_ALT_CREDIT(tar, val) /* no-op */
  55. #endif
  56. /*
  57. * HTC_MAX_TX_BUNDLE_SEND_LIMIT -
  58. * This value is in units of tx frame fragments.
  59. * It needs to be at least as large as the maximum number of tx frames in a
  60. * HTC download bundle times the average number of fragments in each such frame
  61. * (In certain operating systems, such as Linux, we expect to only have
  62. * a single fragment per frame anyway.)
  63. */
  64. #define HTC_MAX_TX_BUNDLE_SEND_LIMIT 255
  65. #define HTC_PACKET_CONTAINER_ALLOCATION 32
  66. #define NUM_CONTROL_TX_BUFFERS 2
  67. #define HTC_CONTROL_BUFFER_SIZE (HTC_MAX_CONTROL_MESSAGE_LENGTH + \
  68. HTC_HDR_LENGTH)
  69. #define HTC_CONTROL_BUFFER_ALIGN 32
  70. #define HTC_TARGET_RESPONSE_POLL_MS 10
  71. #if !defined(A_SIMOS_DEVHOST)
  72. #define HTC_TARGET_MAX_RESPONSE_POLL 200 /* actual HW */
  73. #else
  74. #define HTC_TARGET_MAX_RESPONSE_POLL 600 /* host + target simulation */
  75. #endif
  76. #define HTC_SERVICE_TX_PACKET_TAG HTC_TX_PACKET_TAG_INTERNAL
  77. #ifndef HTC_CREDIT_HISTORY_MAX
  78. #define HTC_CREDIT_HISTORY_MAX 1024
  79. #endif
  80. #define HTC_IS_EPPING_ENABLED(_x) ((_x) == QDF_GLOBAL_EPPING_MODE)
  81. enum htc_credit_exchange_type {
  82. HTC_REQUEST_CREDIT,
  83. HTC_PROCESS_CREDIT_REPORT,
  84. HTC_SUSPEND_ACK,
  85. HTC_SUSPEND_NACK,
  86. HTC_INITIAL_WAKE_UP,
  87. };
  88. static inline const char*
  89. htc_credit_exchange_type_str(enum htc_credit_exchange_type type)
  90. {
  91. switch (type) {
  92. case HTC_REQUEST_CREDIT:
  93. return "HTC_REQUEST_CREDIT";
  94. case HTC_PROCESS_CREDIT_REPORT:
  95. return "HTC_PROCESS_CREDIT_REPORT";
  96. case HTC_SUSPEND_ACK:
  97. return "HTC_SUSPEND_ACK";
  98. case HTC_SUSPEND_NACK:
  99. return "HTC_SUSPEND_NACK";
  100. case HTC_INITIAL_WAKE_UP:
  101. return "HTC_INITIAL_WAKE_UP";
  102. default:
  103. return "Unknown htc_credit_exchange_type";
  104. }
  105. }
  106. typedef struct _HTC_ENDPOINT {
  107. HTC_ENDPOINT_ID Id;
  108. /* service ID this endpoint is bound to
  109. * non-zero value means this endpoint is in use
  110. */
  111. HTC_SERVICE_ID service_id;
  112. /* callbacks associated with this endpoint */
  113. struct htc_ep_callbacks EpCallBacks;
  114. /* HTC frame buffer TX queue */
  115. HTC_PACKET_QUEUE TxQueue;
  116. /* max depth of the TX queue before calling driver's full handler */
  117. int MaxTxQueueDepth;
  118. /* max length of endpoint message */
  119. int MaxMsgLength;
  120. uint8_t UL_PipeID;
  121. uint8_t DL_PipeID;
  122. /* Need to call HIF to get tx completion callbacks? */
  123. int ul_is_polled;
  124. qdf_timer_t ul_poll_timer;
  125. int ul_poll_timer_active;
  126. int ul_outstanding_cnt;
  127. uint32_t htc_send_cnt;
  128. uint32_t htc_comp_cnt;
  129. /* Need to call HIF to fetch rx? (Not currently supported.) */
  130. int dl_is_polled;
  131. /* not currently supported */
  132. /* qdf_timer_t dl_poll_timer; */
  133. /* lookup queue to match netbufs to htc packets */
  134. HTC_PACKET_QUEUE TxLookupQueue;
  135. /* temporary hold queue for back compatibility */
  136. HTC_PACKET_QUEUE RxBufferHoldQueue;
  137. /* TX seq no (helpful) for debugging */
  138. uint8_t SeqNo;
  139. /* serialization */
  140. qdf_atomic_t TxProcessCount;
  141. struct _HTC_TARGET *target;
  142. /* TX credits available on this endpoint */
  143. int TxCredits;
  144. /* size in bytes of each credit (set by HTC) */
  145. int TxCreditSize;
  146. /* credits required per max message (precalculated) */
  147. int TxCreditsPerMaxMsg;
  148. #ifdef HTC_EP_STAT_PROFILING
  149. /* endpoint statistics */
  150. struct htc_endpoint_stats endpoint_stats;
  151. #endif
  152. bool TxCreditFlowEnabled;
  153. bool async_update; /* packets can be queued asynchronously */
  154. qdf_spinlock_t lookup_queue_lock;
  155. /* number of consecutive requeue attempts used for print */
  156. uint32_t num_requeues_warn;
  157. /* total number of requeue attempts */
  158. uint32_t total_num_requeues;
  159. } HTC_ENDPOINT;
  160. #ifdef HTC_EP_STAT_PROFILING
  161. #define INC_HTC_EP_STAT(p, stat, count) ((p)->endpoint_stats.stat += (count))
  162. #else
  163. #define INC_HTC_EP_STAT(p, stat, count)
  164. #endif
  165. struct htc_service_tx_credit_allocation {
  166. uint16_t service_id;
  167. uint8_t CreditAllocation;
  168. };
  169. #define HTC_MAX_SERVICE_ALLOC_ENTRIES 8
  170. /* Error codes for HTC layer packet stats*/
  171. enum ol_ath_htc_pkt_ecodes {
  172. /* error- get packet at head of HTC_PACKET_Q */
  173. GET_HTC_PKT_Q_FAIL = 0,
  174. HTC_PKT_Q_EMPTY,
  175. HTC_SEND_Q_EMPTY
  176. };
  177. /* our HTC target state */
  178. typedef struct _HTC_TARGET {
  179. struct hif_opaque_softc *hif_dev;
  180. HTC_ENDPOINT endpoint[ENDPOINT_MAX];
  181. qdf_spinlock_t HTCLock;
  182. qdf_spinlock_t HTCRxLock;
  183. qdf_spinlock_t HTCTxLock;
  184. uint32_t HTCStateFlags;
  185. void *host_handle;
  186. struct htc_init_info HTCInitInfo;
  187. HTC_PACKET *pHTCPacketStructPool; /* pool of HTC packets */
  188. HTC_PACKET_QUEUE ControlBufferTXFreeList;
  189. uint8_t CtrlResponseBuffer[HTC_MAX_CONTROL_MESSAGE_LENGTH];
  190. int CtrlResponseLength;
  191. qdf_event_t ctrl_response_valid;
  192. bool CtrlResponseProcessing;
  193. int TotalTransmitCredits;
  194. struct htc_service_tx_credit_allocation
  195. ServiceTxAllocTable[HTC_MAX_SERVICE_ALLOC_ENTRIES];
  196. int TargetCreditSize;
  197. #ifdef RX_SG_SUPPORT
  198. qdf_nbuf_queue_t RxSgQueue;
  199. bool IsRxSgInprogress;
  200. uint32_t CurRxSgTotalLen; /* current total length */
  201. uint32_t ExpRxSgTotalLen; /* expected total length */
  202. #endif
  203. qdf_device_t osdev;
  204. struct ol_ath_htc_stats htc_pkt_stats;
  205. HTC_PACKET *pBundleFreeList;
  206. uint32_t ce_send_cnt;
  207. uint32_t TX_comp_cnt;
  208. uint8_t MaxMsgsPerHTCBundle;
  209. qdf_work_t queue_kicker;
  210. #ifdef HIF_SDIO
  211. uint16_t AltDataCreditSize;
  212. #endif
  213. uint32_t avail_tx_credits;
  214. #if defined(DEBUG_HL_LOGGING) && defined(CONFIG_HL_SUPPORT)
  215. uint32_t rx_bundle_stats[HTC_MAX_MSG_PER_BUNDLE_RX];
  216. uint32_t tx_bundle_stats[HTC_MAX_MSG_PER_BUNDLE_TX];
  217. #endif
  218. uint32_t con_mode;
  219. /*
  220. * This flag is from the mboxping tool. It indicates that we cannot
  221. * drop it. Besides, nodrop pkts have higher priority than normal pkts.
  222. */
  223. A_BOOL is_nodrop_pkt;
  224. /*
  225. * Number of WMI endpoints used.
  226. * Default value is 1. But it should be overridden after htc_create to
  227. * reflect the actual count.
  228. */
  229. uint8_t wmi_ep_count;
  230. /* Flag to indicate whether htc header length check is required */
  231. bool htc_hdr_length_check;
  232. /* flag to enable packet send debug */
  233. bool htc_pkt_dbg;
  234. #ifdef FEATURE_RUNTIME_PM
  235. /* Runtime count for H2T HTT msg with response */
  236. qdf_atomic_t htc_htt_runtime_cnt;
  237. /* Runtime count for WMI msg*/
  238. qdf_atomic_t htc_wmi_runtime_cnt;
  239. #endif
  240. /* Non flow ctrl enabled endpoints nbuf map unmap count */
  241. uint32_t nbuf_nfc_map_count;
  242. uint32_t nbuf_nfc_unmap_count;
  243. } HTC_TARGET;
  244. #ifdef RX_SG_SUPPORT
  245. #define RESET_RX_SG_CONFIG(_target) \
  246. do { \
  247. _target->ExpRxSgTotalLen = 0; \
  248. _target->CurRxSgTotalLen = 0; \
  249. _target->IsRxSgInprogress = false; \
  250. } while (0)
  251. #endif
  252. #define HTC_STATE_STOPPING (1 << 0)
  253. #define HTC_STOPPING(t) ((t)->HTCStateFlags & HTC_STATE_STOPPING)
  254. #define LOCK_HTC(t) qdf_spin_lock_bh(&(t)->HTCLock)
  255. #define UNLOCK_HTC(t) qdf_spin_unlock_bh(&(t)->HTCLock)
  256. #define LOCK_HTC_RX(t) qdf_spin_lock_bh(&(t)->HTCRxLock)
  257. #define UNLOCK_HTC_RX(t) qdf_spin_unlock_bh(&(t)->HTCRxLock)
  258. #define LOCK_HTC_TX(t) qdf_spin_lock_bh(&(t)->HTCTxLock)
  259. #define UNLOCK_HTC_TX(t) qdf_spin_unlock_bh(&(t)->HTCTxLock)
  260. #define LOCK_HTC_EP_TX_LOOKUP(t) qdf_spin_lock_bh(&(t)->lookup_queue_lock)
  261. #define UNLOCK_HTC_EP_TX_LOOKUP(t) qdf_spin_unlock_bh(&(t)->lookup_queue_lock)
  262. #define GET_HTC_TARGET_FROM_HANDLE(hnd) ((HTC_TARGET *)(hnd))
  263. #define IS_TX_CREDIT_FLOW_ENABLED(ep) ((ep)->TxCreditFlowEnabled)
  264. #define HTC_POLL_CLEANUP_PERIOD_MS 10 /* milliseconds */
  265. /* Macro to Increment the HTC_PACKET_ERRORS for Tx.*/
  266. #define OL_ATH_HTC_PKT_ERROR_COUNT_INCR(_target, _ecode) \
  267. do { \
  268. if (_ecode == GET_HTC_PKT_Q_FAIL) \
  269. (_target->htc_pkt_stats.htc_get_pkt_q_fail_count) += 1; \
  270. if (_ecode == HTC_PKT_Q_EMPTY) \
  271. (_target->htc_pkt_stats.htc_pkt_q_empty_count) += 1; \
  272. if (_ecode == HTC_SEND_Q_EMPTY) \
  273. (_target->htc_pkt_stats.htc_send_q_empty_count) += 1; \
  274. } while (0)
  275. /* internal HTC functions */
  276. QDF_STATUS htc_rx_completion_handler(void *Context, qdf_nbuf_t netbuf,
  277. uint8_t pipeID);
  278. QDF_STATUS htc_tx_completion_handler(void *Context, qdf_nbuf_t netbuf,
  279. unsigned int transferID,
  280. uint32_t toeplitz_hash_result);
  281. HTC_PACKET *allocate_htc_bundle_packet(HTC_TARGET *target);
  282. void free_htc_bundle_packet(HTC_TARGET *target, HTC_PACKET *pPacket);
  283. HTC_PACKET *allocate_htc_packet_container(HTC_TARGET *target);
  284. void free_htc_packet_container(HTC_TARGET *target, HTC_PACKET *pPacket);
  285. void htc_flush_rx_hold_queue(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint);
  286. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  287. HTC_TX_TAG Tag);
  288. /**
  289. * htc_flush_endpoint_txlookupQ() - Flush EP's lookup queue
  290. * @target: HTC target
  291. * @endpoint_id: EP ID
  292. * @call_ep_callback: whether to call EP tx completion callback
  293. *
  294. * Return: void
  295. */
  296. void htc_flush_endpoint_txlookupQ(HTC_TARGET *target,
  297. HTC_ENDPOINT_ID endpoint_id,
  298. bool call_ep_callback);
  299. void htc_recv_init(HTC_TARGET *target);
  300. QDF_STATUS htc_wait_recv_ctrl_message(HTC_TARGET *target);
  301. void htc_free_control_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket);
  302. HTC_PACKET *htc_alloc_control_tx_packet(HTC_TARGET *target);
  303. uint8_t htc_get_credit_allocation(HTC_TARGET *target, uint16_t service_id);
  304. void htc_tx_resource_avail_handler(void *context, uint8_t pipeID);
  305. void htc_control_rx_complete(void *Context, HTC_PACKET *pPacket);
  306. void htc_process_credit_rpt(HTC_TARGET *target,
  307. HTC_CREDIT_REPORT *pRpt,
  308. int NumEntries, HTC_ENDPOINT_ID FromEndpoint);
  309. void htc_fw_event_handler(void *context, QDF_STATUS status);
  310. void htc_send_complete_check_cleanup(void *context);
  311. #ifdef FEATURE_RUNTIME_PM
  312. void htc_kick_queues(void *context);
  313. #endif
  314. static inline void htc_send_complete_poll_timer_stop(HTC_ENDPOINT *
  315. pEndpoint) {
  316. LOCK_HTC_TX(pEndpoint->target);
  317. if (pEndpoint->ul_poll_timer_active) {
  318. /* qdf_timer_stop(&pEndpoint->ul_poll_timer); */
  319. pEndpoint->ul_poll_timer_active = 0;
  320. }
  321. UNLOCK_HTC_TX(pEndpoint->target);
  322. }
  323. static inline void htc_send_complete_poll_timer_start(HTC_ENDPOINT *
  324. pEndpoint) {
  325. LOCK_HTC_TX(pEndpoint->target);
  326. if (pEndpoint->ul_outstanding_cnt
  327. && !pEndpoint->ul_poll_timer_active) {
  328. /* qdf_timer_start(
  329. * &pEndpoint->ul_poll_timer, HTC_POLL_CLEANUP_PERIOD_MS);
  330. */
  331. pEndpoint->ul_poll_timer_active = 1;
  332. }
  333. UNLOCK_HTC_TX(pEndpoint->target);
  334. }
  335. static inline void
  336. htc_send_complete_check(HTC_ENDPOINT *pEndpoint, int force) {
  337. /*
  338. * Stop the polling-cleanup timer that will result in a later call to
  339. * this function. It may get started again below, if there are still
  340. * outsending sends.
  341. */
  342. htc_send_complete_poll_timer_stop(pEndpoint);
  343. /*
  344. * Check whether HIF has any prior sends that have finished,
  345. * have not had the post-processing done.
  346. */
  347. hif_send_complete_check(pEndpoint->target->hif_dev,
  348. pEndpoint->UL_PipeID, force);
  349. /*
  350. * If there are still outstanding sends after polling, start a timer
  351. * to check again a little later.
  352. */
  353. htc_send_complete_poll_timer_start(pEndpoint);
  354. }
  355. #ifdef __cplusplus
  356. }
  357. #endif
  358. #ifndef DEBUG_BUNDLE
  359. #define DEBUG_BUNDLE 0
  360. #endif
  361. /* HTC Control message receive timeout msec */
  362. #define HTC_CONTROL_RX_TIMEOUT 6000
  363. #if defined(HIF_SDIO) || defined(HIF_USB)
  364. #ifndef ENABLE_BUNDLE_TX
  365. #define ENABLE_BUNDLE_TX 1
  366. #endif
  367. #ifndef ENABLE_BUNDLE_RX
  368. #define ENABLE_BUNDLE_RX 1
  369. #endif
  370. #endif /*defined(HIF_SDIO) || defined(HIF_USB)*/
  371. #if defined ENABLE_BUNDLE_TX
  372. #define HTC_TX_BUNDLE_ENABLED(target) (target->MaxMsgsPerHTCBundle > 1)
  373. #else
  374. #define HTC_TX_BUNDLE_ENABLED(target) 0
  375. #endif
  376. #if defined ENABLE_BUNDLE_RX
  377. #define HTC_RX_BUNDLE_ENABLED(target) (target->MaxMsgsPerHTCBundle > 1)
  378. #else
  379. #define HTC_RX_BUNDLE_ENABLED(target) 0
  380. #endif
  381. #define HTC_ENABLE_BUNDLE(target) (target->MaxMsgsPerHTCBundle > 1)
  382. #endif /* !_HTC_HOST_INTERNAL_H_ */