htc_services.c 12 KB

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