htc_services.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (c) 2013-2018 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. #include "htc_debug.h"
  27. #include "htc_internal.h"
  28. #include <hif.h>
  29. #include <qdf_nbuf.h> /* qdf_nbuf_t */
  30. #include "qdf_module.h"
  31. /* use credit flow control over HTC */
  32. unsigned int htc_credit_flow = 1;
  33. #ifndef DEBUG_CREDIT
  34. #define DEBUG_CREDIT 0
  35. #endif
  36. /* HTC credit flow global disable */
  37. void htc_global_credit_flow_disable(void)
  38. {
  39. htc_credit_flow = 0;
  40. }
  41. /* HTC credit flow global enable */
  42. void htc_global_credit_flow_enable(void)
  43. {
  44. htc_credit_flow = 1;
  45. }
  46. #ifdef HIF_SDIO
  47. /**
  48. * htc_alt_data_credit_size_update() - update tx credit size info
  49. * on max bundle size
  50. * @target: hif context
  51. * @ul_pipe: endpoint ul pipe id
  52. * @dl_pipe: endpoint dl pipe id
  53. * @txCreditSize: endpoint tx credit size
  54. *
  55. *
  56. * When AltDataCreditSize is non zero, it indicates the credit size for
  57. * HTT and all other services on Mbox0. Mbox1 has WMI_CONTROL_SVC which
  58. * uses the default credit size. Use AltDataCreditSize only when
  59. * mailbox is swapped. Mailbox swap bit is set by bmi_target_ready at
  60. * the end of BMI phase.
  61. *
  62. * The Credit Size is a parameter associated with the mbox rather than
  63. * a service. Multiple services can run on this mbox.
  64. *
  65. * If AltDataCreditSize is 0, that means the firmware doesn't support
  66. * this feature. Default to the TargetCreditSize
  67. *
  68. * Return: None
  69. */
  70. static inline void
  71. htc_alt_data_credit_size_update(HTC_TARGET *target,
  72. uint8_t *ul_pipe,
  73. uint8_t *dl_pipe,
  74. int *txCreditSize)
  75. {
  76. if ((target->AltDataCreditSize) &&
  77. (*ul_pipe == 1) && (*dl_pipe == 0))
  78. *txCreditSize = target->AltDataCreditSize;
  79. }
  80. #else
  81. static inline void
  82. htc_alt_data_credit_size_update(HTC_TARGET *target,
  83. uint8_t *ul_pipe,
  84. uint8_t *dl_pipe,
  85. int *txCreditSize)
  86. {
  87. }
  88. #endif
  89. QDF_STATUS htc_connect_service(HTC_HANDLE HTCHandle,
  90. struct htc_service_connect_req *pConnectReq,
  91. struct htc_service_connect_resp *pConnectResp)
  92. {
  93. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  94. QDF_STATUS status = QDF_STATUS_SUCCESS;
  95. HTC_PACKET *pSendPacket = NULL;
  96. HTC_CONNECT_SERVICE_RESPONSE_MSG *pResponseMsg;
  97. HTC_CONNECT_SERVICE_MSG *pConnectMsg;
  98. HTC_ENDPOINT_ID assignedEndpoint = ENDPOINT_MAX;
  99. HTC_ENDPOINT *pEndpoint;
  100. unsigned int maxMsgSize = 0;
  101. qdf_nbuf_t netbuf;
  102. uint8_t txAlloc;
  103. int length;
  104. bool disableCreditFlowCtrl = false;
  105. uint16_t conn_flags;
  106. uint16_t rsp_msg_id, rsp_msg_serv_id, rsp_msg_max_msg_size;
  107. uint8_t rsp_msg_status, rsp_msg_end_id, rsp_msg_serv_meta_len;
  108. AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
  109. ("+htc_connect_service, target:%pK SvcID:0x%X\n", target,
  110. pConnectReq->service_id));
  111. do {
  112. AR_DEBUG_ASSERT(pConnectReq->service_id != 0);
  113. if (HTC_CTRL_RSVD_SVC == pConnectReq->service_id) {
  114. /* special case for pseudo control service */
  115. assignedEndpoint = ENDPOINT_0;
  116. maxMsgSize = HTC_MAX_CONTROL_MESSAGE_LENGTH;
  117. txAlloc = 0;
  118. } else {
  119. txAlloc = htc_get_credit_allocation(target,
  120. pConnectReq->service_id);
  121. if (!txAlloc) {
  122. AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
  123. ("Service %d does not allocate target credits!\n",
  124. pConnectReq->service_id));
  125. }
  126. /* allocate a packet to send to the target */
  127. pSendPacket = htc_alloc_control_tx_packet(target);
  128. if (NULL == pSendPacket) {
  129. AR_DEBUG_ASSERT(false);
  130. status = QDF_STATUS_E_NOMEM;
  131. break;
  132. }
  133. netbuf =
  134. (qdf_nbuf_t)
  135. GET_HTC_PACKET_NET_BUF_CONTEXT(pSendPacket);
  136. length =
  137. sizeof(HTC_CONNECT_SERVICE_MSG) +
  138. pConnectReq->MetaDataLength;
  139. /* assemble connect service message */
  140. qdf_nbuf_put_tail(netbuf, length);
  141. pConnectMsg =
  142. (HTC_CONNECT_SERVICE_MSG *) qdf_nbuf_data(netbuf);
  143. if (NULL == pConnectMsg) {
  144. AR_DEBUG_ASSERT(0);
  145. status = QDF_STATUS_E_FAULT;
  146. break;
  147. }
  148. qdf_mem_zero(pConnectMsg,
  149. sizeof(HTC_CONNECT_SERVICE_MSG));
  150. conn_flags =
  151. (pConnectReq->
  152. ConnectionFlags & ~HTC_SET_RECV_ALLOC_MASK) |
  153. HTC_CONNECT_FLAGS_SET_RECV_ALLOCATION(txAlloc);
  154. HTC_SET_FIELD(pConnectMsg, HTC_CONNECT_SERVICE_MSG,
  155. MESSAGEID, HTC_MSG_CONNECT_SERVICE_ID);
  156. HTC_SET_FIELD(pConnectMsg, HTC_CONNECT_SERVICE_MSG,
  157. SERVICE_ID, pConnectReq->service_id);
  158. HTC_SET_FIELD(pConnectMsg, HTC_CONNECT_SERVICE_MSG,
  159. CONNECTIONFLAGS, conn_flags);
  160. if (pConnectReq->
  161. ConnectionFlags &
  162. HTC_CONNECT_FLAGS_DISABLE_CREDIT_FLOW_CTRL) {
  163. disableCreditFlowCtrl = true;
  164. }
  165. if (!htc_credit_flow)
  166. disableCreditFlowCtrl = true;
  167. /* check caller if it wants to transfer meta data */
  168. if ((pConnectReq->pMetaData != NULL) &&
  169. (pConnectReq->MetaDataLength <=
  170. HTC_SERVICE_META_DATA_MAX_LENGTH)) {
  171. /* copy meta data into msg buffer (after hdr) */
  172. qdf_mem_copy((uint8_t *) pConnectMsg +
  173. sizeof(HTC_CONNECT_SERVICE_MSG),
  174. pConnectReq->pMetaData,
  175. pConnectReq->MetaDataLength);
  176. HTC_SET_FIELD(pConnectMsg,
  177. HTC_CONNECT_SERVICE_MSG,
  178. SERVICEMETALENGTH,
  179. pConnectReq->MetaDataLength);
  180. }
  181. SET_HTC_PACKET_INFO_TX(pSendPacket,
  182. NULL,
  183. (uint8_t *) pConnectMsg,
  184. length,
  185. ENDPOINT_0,
  186. HTC_SERVICE_TX_PACKET_TAG);
  187. status = htc_send_pkt((HTC_HANDLE) target, pSendPacket);
  188. /* we don't own it anymore */
  189. pSendPacket = NULL;
  190. if (QDF_IS_STATUS_ERROR(status))
  191. break;
  192. /* wait for response */
  193. status = htc_wait_recv_ctrl_message(target);
  194. if (QDF_IS_STATUS_ERROR(status))
  195. break;
  196. /* we controlled the buffer creation so it has to be
  197. * properly aligned
  198. */
  199. pResponseMsg =
  200. (HTC_CONNECT_SERVICE_RESPONSE_MSG *) target->
  201. CtrlResponseBuffer;
  202. rsp_msg_id = HTC_GET_FIELD(pResponseMsg,
  203. HTC_CONNECT_SERVICE_RESPONSE_MSG,
  204. MESSAGEID);
  205. rsp_msg_serv_id =
  206. HTC_GET_FIELD(pResponseMsg,
  207. HTC_CONNECT_SERVICE_RESPONSE_MSG,
  208. SERVICEID);
  209. rsp_msg_status =
  210. HTC_GET_FIELD(pResponseMsg,
  211. HTC_CONNECT_SERVICE_RESPONSE_MSG,
  212. STATUS);
  213. rsp_msg_end_id =
  214. HTC_GET_FIELD(pResponseMsg,
  215. HTC_CONNECT_SERVICE_RESPONSE_MSG,
  216. ENDPOINTID);
  217. rsp_msg_max_msg_size =
  218. HTC_GET_FIELD(pResponseMsg,
  219. HTC_CONNECT_SERVICE_RESPONSE_MSG,
  220. MAXMSGSIZE);
  221. rsp_msg_serv_meta_len =
  222. HTC_GET_FIELD(pResponseMsg,
  223. HTC_CONNECT_SERVICE_RESPONSE_MSG,
  224. SERVICEMETALENGTH);
  225. if ((rsp_msg_id != HTC_MSG_CONNECT_SERVICE_RESPONSE_ID)
  226. || (target->CtrlResponseLength <
  227. sizeof(HTC_CONNECT_SERVICE_RESPONSE_MSG))) {
  228. /* this message is not valid */
  229. AR_DEBUG_ASSERT(false);
  230. status = QDF_STATUS_E_PROTO;
  231. break;
  232. }
  233. AR_DEBUG_PRINTF(ATH_DEBUG_TRC,
  234. ("htc_connect_service, service 0x%X connect response from target status:%d, assigned ep: %d\n",
  235. rsp_msg_serv_id, rsp_msg_status,
  236. rsp_msg_end_id));
  237. pConnectResp->ConnectRespCode = rsp_msg_status;
  238. /* check response status */
  239. if (rsp_msg_status != HTC_SERVICE_SUCCESS) {
  240. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  241. (" Target failed service 0x%X connect request (status:%d)\n",
  242. rsp_msg_serv_id,
  243. rsp_msg_status));
  244. status = QDF_STATUS_E_PROTO;
  245. /* TODO: restore the ifdef when FW supports services 301 and 302
  246. * (HTT_MSG_DATA[23]_MSG_SVC)
  247. */
  248. /* #ifdef QCA_TX_HTT2_SUPPORT */
  249. /* Keep work and not to block the control msg */
  250. target->CtrlResponseProcessing = false;
  251. /* #endif */ /* QCA_TX_HTT2_SUPPORT */
  252. break;
  253. }
  254. assignedEndpoint = (HTC_ENDPOINT_ID) rsp_msg_end_id;
  255. maxMsgSize = rsp_msg_max_msg_size;
  256. if ((pConnectResp->pMetaData != NULL) &&
  257. (rsp_msg_serv_meta_len > 0) &&
  258. (rsp_msg_serv_meta_len <=
  259. HTC_SERVICE_META_DATA_MAX_LENGTH)) {
  260. /* caller supplied a buffer and the target
  261. * responded with data
  262. */
  263. int copyLength =
  264. min((int)pConnectResp->BufferLength,
  265. (int)rsp_msg_serv_meta_len);
  266. /* copy the meta data */
  267. qdf_mem_copy(pConnectResp->pMetaData,
  268. ((uint8_t *) pResponseMsg) +
  269. sizeof
  270. (HTC_CONNECT_SERVICE_RESPONSE_MSG),
  271. copyLength);
  272. pConnectResp->ActualLength = copyLength;
  273. }
  274. /* done processing response buffer */
  275. target->CtrlResponseProcessing = false;
  276. }
  277. /* rest of these are parameter checks so set the error status */
  278. status = QDF_STATUS_E_PROTO;
  279. if (assignedEndpoint >= ENDPOINT_MAX) {
  280. AR_DEBUG_ASSERT(false);
  281. break;
  282. }
  283. if (0 == maxMsgSize) {
  284. AR_DEBUG_ASSERT(false);
  285. break;
  286. }
  287. pEndpoint = &target->endpoint[assignedEndpoint];
  288. pEndpoint->Id = assignedEndpoint;
  289. if (pEndpoint->service_id != 0) {
  290. /* endpoint already in use! */
  291. AR_DEBUG_ASSERT(false);
  292. break;
  293. }
  294. /* return assigned endpoint to caller */
  295. pConnectResp->Endpoint = assignedEndpoint;
  296. pConnectResp->MaxMsgLength = maxMsgSize;
  297. /* setup the endpoint */
  298. /* service_id marks the endpoint in use */
  299. pEndpoint->service_id = pConnectReq->service_id;
  300. pEndpoint->MaxTxQueueDepth = pConnectReq->MaxSendQueueDepth;
  301. pEndpoint->MaxMsgLength = maxMsgSize;
  302. pEndpoint->TxCredits = txAlloc;
  303. pEndpoint->TxCreditSize = target->TargetCreditSize;
  304. pEndpoint->TxCreditsPerMaxMsg =
  305. maxMsgSize / target->TargetCreditSize;
  306. if (maxMsgSize % target->TargetCreditSize)
  307. pEndpoint->TxCreditsPerMaxMsg++;
  308. #if DEBUG_CREDIT
  309. qdf_print(" Endpoint%d initial credit:%d, size:%d.\n",
  310. pEndpoint->Id, pEndpoint->TxCredits,
  311. pEndpoint->TxCreditSize);
  312. #endif
  313. /* copy all the callbacks */
  314. pEndpoint->EpCallBacks = pConnectReq->EpCallbacks;
  315. pEndpoint->async_update = 0;
  316. status = hif_map_service_to_pipe(target->hif_dev,
  317. pEndpoint->service_id,
  318. &pEndpoint->UL_PipeID,
  319. &pEndpoint->DL_PipeID,
  320. &pEndpoint->ul_is_polled,
  321. &pEndpoint->dl_is_polled);
  322. if (QDF_IS_STATUS_ERROR(status))
  323. break;
  324. htc_alt_data_credit_size_update(target,
  325. &pEndpoint->UL_PipeID,
  326. &pEndpoint->DL_PipeID,
  327. &pEndpoint->TxCreditSize);
  328. /* not currently supported */
  329. qdf_assert(!pEndpoint->dl_is_polled);
  330. if (pEndpoint->ul_is_polled) {
  331. qdf_timer_init(target->osdev,
  332. &pEndpoint->ul_poll_timer,
  333. htc_send_complete_check_cleanup,
  334. pEndpoint,
  335. QDF_TIMER_TYPE_SW);
  336. }
  337. AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
  338. ("SVC:0x%4.4X, ULpipe:%d DLpipe:%d id:%d Ready",
  339. pEndpoint->service_id, pEndpoint->UL_PipeID,
  340. pEndpoint->DL_PipeID, pEndpoint->Id));
  341. if (disableCreditFlowCtrl && pEndpoint->TxCreditFlowEnabled) {
  342. pEndpoint->TxCreditFlowEnabled = false;
  343. AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
  344. ("SVC:0x%4.4X ep:%d TX flow control disabled",
  345. pEndpoint->service_id,
  346. assignedEndpoint));
  347. }
  348. } while (false);
  349. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-htc_connect_service\n"));
  350. return status;
  351. }
  352. qdf_export_symbol(htc_connect_service);
  353. void htc_set_credit_distribution(HTC_HANDLE HTCHandle,
  354. void *pCreditDistContext,
  355. HTC_CREDIT_DIST_CALLBACK CreditDistFunc,
  356. HTC_CREDIT_INIT_CALLBACK CreditInitFunc,
  357. HTC_SERVICE_ID ServicePriorityOrder[],
  358. int ListLength)
  359. {
  360. /* NOT Supported, this transport does not use a credit based flow
  361. * control mechanism
  362. */
  363. }
  364. void htc_fw_event_handler(void *context, QDF_STATUS status)
  365. {
  366. HTC_TARGET *target = (HTC_TARGET *) context;
  367. struct htc_init_info *initInfo = &target->HTCInitInfo;
  368. /* check if target failure handler exists and pass error code to it. */
  369. if (target->HTCInitInfo.TargetFailure != NULL)
  370. initInfo->TargetFailure(initInfo->pContext, status);
  371. }
  372. void htc_set_async_ep(HTC_HANDLE HTCHandle,
  373. HTC_ENDPOINT_ID htc_ep_id, bool value)
  374. {
  375. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  376. HTC_ENDPOINT *pEndpoint = &target->endpoint[htc_ep_id];
  377. pEndpoint->async_update = value;
  378. qdf_print("%s: htc_handle %pK, ep %d, value %d\n", __func__,
  379. HTCHandle, htc_ep_id, value);
  380. }