htc_recv.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 <qdf_nbuf.h> /* qdf_nbuf_t */
  29. /* HTC Control message receive timeout msec */
  30. #define HTC_CONTROL_RX_TIMEOUT 3000
  31. #if defined(WLAN_DEBUG) || defined(DEBUG)
  32. void debug_dump_bytes(uint8_t *buffer, uint16_t length, char *pDescription)
  33. {
  34. int8_t stream[60];
  35. int8_t byteOffsetStr[10];
  36. uint32_t i;
  37. uint16_t offset, count, byteOffset;
  38. A_PRINTF("<---------Dumping %d Bytes : %s ------>\n", length,
  39. pDescription);
  40. count = 0;
  41. offset = 0;
  42. byteOffset = 0;
  43. for (i = 0; i < length; i++) {
  44. A_SNPRINTF(stream + offset, (sizeof(stream) - offset),
  45. "%02X ", buffer[i]);
  46. count++;
  47. offset += 3;
  48. if (count == 16) {
  49. count = 0;
  50. offset = 0;
  51. A_SNPRINTF(byteOffsetStr, sizeof(byteOffset), "%4.4X",
  52. byteOffset);
  53. A_PRINTF("[%s]: %s\n", byteOffsetStr, stream);
  54. qdf_mem_zero(stream, 60);
  55. byteOffset += 16;
  56. }
  57. }
  58. if (offset != 0) {
  59. A_SNPRINTF(byteOffsetStr, sizeof(byteOffset), "%4.4X",
  60. byteOffset);
  61. A_PRINTF("[%s]: %s\n", byteOffsetStr, stream);
  62. }
  63. A_PRINTF("<------------------------------------------------->\n");
  64. }
  65. #else
  66. void debug_dump_bytes(uint8_t *buffer, uint16_t length, char *pDescription)
  67. {
  68. }
  69. #endif
  70. static A_STATUS htc_process_trailer(HTC_TARGET *target,
  71. uint8_t *pBuffer,
  72. int Length, HTC_ENDPOINT_ID FromEndpoint);
  73. static void do_recv_completion(HTC_ENDPOINT *pEndpoint,
  74. HTC_PACKET_QUEUE *pQueueToIndicate)
  75. {
  76. do {
  77. if (HTC_QUEUE_EMPTY(pQueueToIndicate)) {
  78. /* nothing to indicate */
  79. break;
  80. }
  81. if (pEndpoint->EpCallBacks.EpRecvPktMultiple != NULL) {
  82. AR_DEBUG_PRINTF(ATH_DEBUG_RECV,
  83. (" HTC calling ep %d, recv multiple callback (%d pkts) \n",
  84. pEndpoint->Id,
  85. HTC_PACKET_QUEUE_DEPTH
  86. (pQueueToIndicate)));
  87. /* a recv multiple handler is being used, pass the queue to the handler */
  88. pEndpoint->EpCallBacks.EpRecvPktMultiple(pEndpoint->
  89. EpCallBacks.
  90. pContext,
  91. pQueueToIndicate);
  92. INIT_HTC_PACKET_QUEUE(pQueueToIndicate);
  93. } else {
  94. HTC_PACKET *pPacket;
  95. /* using legacy EpRecv */
  96. while (!HTC_QUEUE_EMPTY(pQueueToIndicate)) {
  97. pPacket = htc_packet_dequeue(pQueueToIndicate);
  98. if (pEndpoint->EpCallBacks.EpRecv == NULL) {
  99. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  100. ("HTC ep %d has NULL recv callback on packet %p\n",
  101. pEndpoint->Id,
  102. pPacket));
  103. if (pPacket)
  104. qdf_nbuf_free(
  105. pPacket->pPktContext);
  106. continue;
  107. }
  108. AR_DEBUG_PRINTF(ATH_DEBUG_RECV,
  109. ("HTC calling ep %d recv callback on packet %p\n",
  110. pEndpoint->Id, pPacket));
  111. pEndpoint->EpCallBacks.EpRecv(pEndpoint->
  112. EpCallBacks.
  113. pContext,
  114. pPacket);
  115. }
  116. }
  117. } while (false);
  118. }
  119. static void recv_packet_completion(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  120. HTC_PACKET *pPacket)
  121. {
  122. HTC_PACKET_QUEUE container;
  123. INIT_HTC_PACKET_QUEUE_AND_ADD(&container, pPacket);
  124. /* do completion */
  125. do_recv_completion(pEndpoint, &container);
  126. }
  127. void htc_control_rx_complete(void *Context, HTC_PACKET *pPacket)
  128. {
  129. /* TODO, can't really receive HTC control messages yet.... */
  130. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  131. ("Invalid call to htc_control_rx_complete\n"));
  132. }
  133. void htc_unblock_recv(HTC_HANDLE HTCHandle)
  134. {
  135. /* TODO find the Need in new model */
  136. }
  137. void htc_enable_recv(HTC_HANDLE HTCHandle)
  138. {
  139. /* TODO find the Need in new model */
  140. }
  141. void htc_disable_recv(HTC_HANDLE HTCHandle)
  142. {
  143. /* TODO find the Need in new model */
  144. }
  145. int htc_get_num_recv_buffers(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
  146. {
  147. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  148. HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
  149. return HTC_PACKET_QUEUE_DEPTH(&pEndpoint->RxBufferHoldQueue);
  150. }
  151. HTC_PACKET *allocate_htc_packet_container(HTC_TARGET *target)
  152. {
  153. HTC_PACKET *pPacket;
  154. LOCK_HTC_RX(target);
  155. if (NULL == target->pHTCPacketStructPool) {
  156. UNLOCK_HTC_RX(target);
  157. return NULL;
  158. }
  159. pPacket = target->pHTCPacketStructPool;
  160. target->pHTCPacketStructPool = (HTC_PACKET *) pPacket->ListLink.pNext;
  161. UNLOCK_HTC_RX(target);
  162. pPacket->ListLink.pNext = NULL;
  163. return pPacket;
  164. }
  165. void free_htc_packet_container(HTC_TARGET *target, HTC_PACKET *pPacket)
  166. {
  167. LOCK_HTC_RX(target);
  168. if (NULL == target->pHTCPacketStructPool) {
  169. target->pHTCPacketStructPool = pPacket;
  170. pPacket->ListLink.pNext = NULL;
  171. } else {
  172. pPacket->ListLink.pNext =
  173. (DL_LIST *) target->pHTCPacketStructPool;
  174. target->pHTCPacketStructPool = pPacket;
  175. }
  176. UNLOCK_HTC_RX(target);
  177. }
  178. #ifdef RX_SG_SUPPORT
  179. qdf_nbuf_t rx_sg_to_single_netbuf(HTC_TARGET *target)
  180. {
  181. qdf_nbuf_t skb;
  182. uint8_t *anbdata;
  183. uint8_t *anbdata_new;
  184. uint32_t anblen;
  185. qdf_nbuf_t new_skb = NULL;
  186. uint32_t sg_queue_len;
  187. qdf_nbuf_queue_t *rx_sg_queue = &target->RxSgQueue;
  188. sg_queue_len = qdf_nbuf_queue_len(rx_sg_queue);
  189. if (sg_queue_len <= 1) {
  190. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  191. ("rx_sg_to_single_netbuf: invalid sg queue len %u\n"));
  192. goto _failed;
  193. }
  194. new_skb = qdf_nbuf_alloc(target->ExpRxSgTotalLen, 0, 4, false);
  195. if (new_skb == NULL) {
  196. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  197. ("rx_sg_to_single_netbuf: can't allocate %u size netbuf\n",
  198. target->ExpRxSgTotalLen));
  199. goto _failed;
  200. }
  201. qdf_nbuf_peek_header(new_skb, &anbdata_new, &anblen);
  202. skb = qdf_nbuf_queue_remove(rx_sg_queue);
  203. do {
  204. qdf_nbuf_peek_header(skb, &anbdata, &anblen);
  205. qdf_mem_copy(anbdata_new, anbdata, qdf_nbuf_len(skb));
  206. qdf_nbuf_put_tail(new_skb, qdf_nbuf_len(skb));
  207. anbdata_new += qdf_nbuf_len(skb);
  208. qdf_nbuf_free(skb);
  209. skb = qdf_nbuf_queue_remove(rx_sg_queue);
  210. } while (skb != NULL);
  211. RESET_RX_SG_CONFIG(target);
  212. return new_skb;
  213. _failed:
  214. while ((skb = qdf_nbuf_queue_remove(rx_sg_queue)) != NULL)
  215. qdf_nbuf_free(skb);
  216. RESET_RX_SG_CONFIG(target);
  217. return NULL;
  218. }
  219. #endif
  220. #ifdef CONFIG_WIN
  221. #define HTC_MSG_NACK_SUSPEND 7
  222. #endif
  223. QDF_STATUS htc_rx_completion_handler(void *Context, qdf_nbuf_t netbuf,
  224. uint8_t pipeID)
  225. {
  226. QDF_STATUS status = QDF_STATUS_SUCCESS;
  227. HTC_FRAME_HDR *HtcHdr;
  228. HTC_TARGET *target = (HTC_TARGET *) Context;
  229. uint8_t *netdata;
  230. uint32_t netlen;
  231. HTC_ENDPOINT *pEndpoint;
  232. HTC_PACKET *pPacket;
  233. uint16_t payloadLen;
  234. uint32_t trailerlen = 0;
  235. uint8_t htc_ep_id;
  236. HTC_INIT_INFO *info;
  237. #ifdef RX_SG_SUPPORT
  238. LOCK_HTC_RX(target);
  239. if (target->IsRxSgInprogress) {
  240. target->CurRxSgTotalLen += qdf_nbuf_len(netbuf);
  241. qdf_nbuf_queue_add(&target->RxSgQueue, netbuf);
  242. if (target->CurRxSgTotalLen == target->ExpRxSgTotalLen) {
  243. netbuf = rx_sg_to_single_netbuf(target);
  244. if (netbuf == NULL) {
  245. UNLOCK_HTC_RX(target);
  246. goto _out;
  247. }
  248. } else {
  249. netbuf = NULL;
  250. UNLOCK_HTC_RX(target);
  251. goto _out;
  252. }
  253. }
  254. UNLOCK_HTC_RX(target);
  255. #endif
  256. netdata = qdf_nbuf_data(netbuf);
  257. netlen = qdf_nbuf_len(netbuf);
  258. HtcHdr = (HTC_FRAME_HDR *) netdata;
  259. do {
  260. htc_ep_id = HTC_GET_FIELD(HtcHdr, HTC_FRAME_HDR, ENDPOINTID);
  261. if (htc_ep_id >= ENDPOINT_MAX) {
  262. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  263. ("HTC Rx: invalid EndpointID=%d\n",
  264. htc_ep_id));
  265. debug_dump_bytes((uint8_t *) HtcHdr,
  266. sizeof(HTC_FRAME_HDR), "BAD HTC Header");
  267. status = QDF_STATUS_E_FAILURE;
  268. QDF_BUG(0);
  269. break;
  270. }
  271. pEndpoint = &target->endpoint[htc_ep_id];
  272. /*
  273. * If this endpoint that received a message from the target has
  274. * a to-target HIF pipe whose send completions are polled rather
  275. * than interrupt-driven, this is a good point to ask HIF to check
  276. * whether it has any completed sends to handle.
  277. */
  278. if (pEndpoint->ul_is_polled) {
  279. htc_send_complete_check(pEndpoint, 1);
  280. }
  281. payloadLen = HTC_GET_FIELD(HtcHdr, HTC_FRAME_HDR, PAYLOADLEN);
  282. if (netlen < (payloadLen + HTC_HDR_LENGTH)) {
  283. #ifdef RX_SG_SUPPORT
  284. LOCK_HTC_RX(target);
  285. target->IsRxSgInprogress = true;
  286. qdf_nbuf_queue_init(&target->RxSgQueue);
  287. qdf_nbuf_queue_add(&target->RxSgQueue, netbuf);
  288. target->ExpRxSgTotalLen = (payloadLen + HTC_HDR_LENGTH);
  289. target->CurRxSgTotalLen += netlen;
  290. UNLOCK_HTC_RX(target);
  291. netbuf = NULL;
  292. break;
  293. #else
  294. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  295. ("HTC Rx: insufficient length, got:%d expected =%zu\n",
  296. netlen, payloadLen + HTC_HDR_LENGTH));
  297. debug_dump_bytes((uint8_t *) HtcHdr,
  298. sizeof(HTC_FRAME_HDR),
  299. "BAD RX packet length");
  300. status = QDF_STATUS_E_FAILURE;
  301. QDF_BUG(0);
  302. break;
  303. #endif
  304. }
  305. #ifdef HTC_EP_STAT_PROFILING
  306. LOCK_HTC_RX(target);
  307. INC_HTC_EP_STAT(pEndpoint, RxReceived, 1);
  308. UNLOCK_HTC_RX(target);
  309. #endif
  310. /* if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) { */
  311. {
  312. uint8_t temp;
  313. A_STATUS temp_status;
  314. /* get flags to check for trailer */
  315. temp = HTC_GET_FIELD(HtcHdr, HTC_FRAME_HDR, FLAGS);
  316. if (temp & HTC_FLAGS_RECV_TRAILER) {
  317. /* extract the trailer length */
  318. temp =
  319. HTC_GET_FIELD(HtcHdr, HTC_FRAME_HDR,
  320. CONTROLBYTES0);
  321. if ((temp < sizeof(HTC_RECORD_HDR))
  322. || (temp > payloadLen)) {
  323. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  324. ("htc_rx_completion_handler, invalid header (payloadlength should be :%d, CB[0] is:%d)\n",
  325. payloadLen, temp));
  326. status = QDF_STATUS_E_INVAL;
  327. break;
  328. }
  329. trailerlen = temp;
  330. /* process trailer data that follows HDR + application payload */
  331. temp_status = htc_process_trailer(target,
  332. ((uint8_t *) HtcHdr +
  333. HTC_HDR_LENGTH +
  334. payloadLen - temp),
  335. temp, htc_ep_id);
  336. if (A_FAILED(temp_status)) {
  337. status = QDF_STATUS_E_FAILURE;
  338. break;
  339. }
  340. }
  341. }
  342. if (((int)payloadLen - (int)trailerlen) <= 0) {
  343. /* zero length packet with trailer data, just drop these */
  344. break;
  345. }
  346. if (htc_ep_id == ENDPOINT_0) {
  347. uint16_t message_id;
  348. HTC_UNKNOWN_MSG *htc_msg;
  349. bool wow_nack;
  350. /* remove HTC header */
  351. qdf_nbuf_pull_head(netbuf, HTC_HDR_LENGTH);
  352. netdata = qdf_nbuf_data(netbuf);
  353. netlen = qdf_nbuf_len(netbuf);
  354. htc_msg = (HTC_UNKNOWN_MSG *) netdata;
  355. message_id =
  356. HTC_GET_FIELD(htc_msg, HTC_UNKNOWN_MSG, MESSAGEID);
  357. switch (message_id) {
  358. default:
  359. /* handle HTC control message */
  360. if (target->CtrlResponseProcessing) {
  361. /* this is a fatal error, target should not be sending unsolicited messages
  362. * on the endpoint 0 */
  363. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  364. ("HTC Rx Ctrl still processing\n"));
  365. status = QDF_STATUS_E_FAILURE;
  366. QDF_BUG(false);
  367. break;
  368. }
  369. LOCK_HTC_RX(target);
  370. target->CtrlResponseLength =
  371. min((int)netlen,
  372. HTC_MAX_CONTROL_MESSAGE_LENGTH);
  373. qdf_mem_copy(target->CtrlResponseBuffer,
  374. netdata,
  375. target->CtrlResponseLength);
  376. /* Requester will clear this flag */
  377. target->CtrlResponseProcessing = true;
  378. UNLOCK_HTC_RX(target);
  379. qdf_event_set(&target->ctrl_response_valid);
  380. break;
  381. #ifdef HTC_MSG_WAKEUP_FROM_SUSPEND_ID
  382. case HTC_MSG_WAKEUP_FROM_SUSPEND_ID:
  383. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  384. ("Received initial wake up"));
  385. LOCK_HTC_CREDIT(target);
  386. htc_credit_record(HTC_INITIAL_WAKE_UP,
  387. pEndpoint->TxCredits,
  388. HTC_PACKET_QUEUE_DEPTH(
  389. &pEndpoint->TxQueue));
  390. UNLOCK_HTC_CREDIT(target);
  391. info = &target->HTCInitInfo;
  392. if (info && info->target_initial_wakeup_cb)
  393. info->target_initial_wakeup_cb(
  394. info->target_psoc);
  395. else
  396. AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
  397. ("No initial wake up cb"));
  398. break;
  399. #endif
  400. case HTC_MSG_SEND_SUSPEND_COMPLETE:
  401. wow_nack = false;
  402. LOCK_HTC_CREDIT(target);
  403. htc_credit_record(HTC_SUSPEND_ACK,
  404. pEndpoint->TxCredits,
  405. HTC_PACKET_QUEUE_DEPTH(
  406. &pEndpoint->TxQueue));
  407. UNLOCK_HTC_CREDIT(target);
  408. target->HTCInitInfo.TargetSendSuspendComplete(
  409. target->HTCInitInfo.target_psoc,
  410. wow_nack);
  411. break;
  412. case HTC_MSG_NACK_SUSPEND:
  413. wow_nack = true;
  414. LOCK_HTC_CREDIT(target);
  415. htc_credit_record(HTC_SUSPEND_ACK,
  416. pEndpoint->TxCredits,
  417. HTC_PACKET_QUEUE_DEPTH(
  418. &pEndpoint->TxQueue));
  419. UNLOCK_HTC_CREDIT(target);
  420. target->HTCInitInfo.TargetSendSuspendComplete(
  421. target->HTCInitInfo.target_psoc,
  422. wow_nack);
  423. break;
  424. }
  425. qdf_nbuf_free(netbuf);
  426. netbuf = NULL;
  427. break;
  428. }
  429. /* the current message based HIF architecture allocates net bufs for recv packets
  430. * since this layer bridges that HIF to upper layers , which expects HTC packets,
  431. * we form the packets here
  432. * TODO_FIXME */
  433. pPacket = allocate_htc_packet_container(target);
  434. if (NULL == pPacket) {
  435. status = QDF_STATUS_E_RESOURCES;
  436. break;
  437. }
  438. pPacket->Status = QDF_STATUS_SUCCESS;
  439. pPacket->Endpoint = htc_ep_id;
  440. pPacket->pPktContext = netbuf;
  441. pPacket->pBuffer = qdf_nbuf_data(netbuf) + HTC_HDR_LENGTH;
  442. pPacket->ActualLength = netlen - HTC_HEADER_LEN - trailerlen;
  443. qdf_nbuf_pull_head(netbuf, HTC_HEADER_LEN);
  444. qdf_nbuf_set_pktlen(netbuf, pPacket->ActualLength);
  445. recv_packet_completion(target, pEndpoint, pPacket);
  446. /* recover the packet container */
  447. free_htc_packet_container(target, pPacket);
  448. netbuf = NULL;
  449. } while (false);
  450. #ifdef RX_SG_SUPPORT
  451. _out:
  452. #endif
  453. if (netbuf != NULL) {
  454. qdf_nbuf_free(netbuf);
  455. }
  456. return status;
  457. }
  458. A_STATUS htc_add_receive_pkt_multiple(HTC_HANDLE HTCHandle,
  459. HTC_PACKET_QUEUE *pPktQueue)
  460. {
  461. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  462. HTC_ENDPOINT *pEndpoint;
  463. HTC_PACKET *pFirstPacket;
  464. A_STATUS status = A_OK;
  465. HTC_PACKET *pPacket;
  466. pFirstPacket = htc_get_pkt_at_head(pPktQueue);
  467. if (NULL == pFirstPacket) {
  468. A_ASSERT(false);
  469. return A_EINVAL;
  470. }
  471. AR_DEBUG_ASSERT(pFirstPacket->Endpoint < ENDPOINT_MAX);
  472. AR_DEBUG_PRINTF(ATH_DEBUG_RECV,
  473. ("+- htc_add_receive_pkt_multiple : endPointId: %d, cnt:%d, length: %d\n",
  474. pFirstPacket->Endpoint,
  475. HTC_PACKET_QUEUE_DEPTH(pPktQueue),
  476. pFirstPacket->BufferLength));
  477. pEndpoint = &target->endpoint[pFirstPacket->Endpoint];
  478. LOCK_HTC_RX(target);
  479. do {
  480. if (HTC_STOPPING(target)) {
  481. status = A_ERROR;
  482. break;
  483. }
  484. /* store receive packets */
  485. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->RxBufferHoldQueue,
  486. pPktQueue);
  487. } while (false);
  488. UNLOCK_HTC_RX(target);
  489. if (A_FAILED(status)) {
  490. /* walk through queue and mark each one canceled */
  491. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue, pPacket) {
  492. pPacket->Status = A_ECANCELED;
  493. }
  494. HTC_PACKET_QUEUE_ITERATE_END;
  495. do_recv_completion(pEndpoint, pPktQueue);
  496. }
  497. return status;
  498. }
  499. A_STATUS htc_add_receive_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket)
  500. {
  501. HTC_PACKET_QUEUE queue;
  502. INIT_HTC_PACKET_QUEUE_AND_ADD(&queue, pPacket);
  503. return htc_add_receive_pkt_multiple(HTCHandle, &queue);
  504. }
  505. void htc_flush_rx_hold_queue(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint)
  506. {
  507. HTC_PACKET *pPacket;
  508. HTC_PACKET_QUEUE container;
  509. LOCK_HTC_RX(target);
  510. while (1) {
  511. pPacket = htc_packet_dequeue(&pEndpoint->RxBufferHoldQueue);
  512. if (NULL == pPacket) {
  513. break;
  514. }
  515. UNLOCK_HTC_RX(target);
  516. pPacket->Status = A_ECANCELED;
  517. pPacket->ActualLength = 0;
  518. AR_DEBUG_PRINTF(ATH_DEBUG_RECV,
  519. (" Flushing RX packet:%p, length:%d, ep:%d \n",
  520. pPacket, pPacket->BufferLength,
  521. pPacket->Endpoint));
  522. INIT_HTC_PACKET_QUEUE_AND_ADD(&container, pPacket);
  523. /* give the packet back */
  524. do_recv_completion(pEndpoint, &container);
  525. LOCK_HTC_RX(target);
  526. }
  527. UNLOCK_HTC_RX(target);
  528. }
  529. void htc_recv_init(HTC_TARGET *target)
  530. {
  531. /* Initialize ctrl_response_valid to block */
  532. qdf_event_create(&target->ctrl_response_valid);
  533. }
  534. /* polling routine to wait for a control packet to be received */
  535. A_STATUS htc_wait_recv_ctrl_message(HTC_TARGET *target)
  536. {
  537. /* int count = HTC_TARGET_MAX_RESPONSE_POLL; */
  538. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("+HTCWaitCtrlMessageRecv\n"));
  539. /* Wait for BMI request/response transaction to complete */
  540. if (qdf_wait_single_event(&target->ctrl_response_valid,
  541. HTC_CONTROL_RX_TIMEOUT)) {
  542. QDF_BUG(0);
  543. return A_ERROR;
  544. }
  545. LOCK_HTC_RX(target);
  546. /* caller will clear this flag */
  547. target->CtrlResponseProcessing = true;
  548. UNLOCK_HTC_RX(target);
  549. #if 0
  550. while (count > 0) {
  551. LOCK_HTC_RX(target);
  552. if (target->CtrlResponseValid) {
  553. target->CtrlResponseValid = false;
  554. /* caller will clear this flag */
  555. target->CtrlResponseProcessing = true;
  556. UNLOCK_HTC_RX(target);
  557. break;
  558. }
  559. UNLOCK_HTC_RX(target);
  560. count--;
  561. A_MSLEEP(HTC_TARGET_RESPONSE_POLL_MS);
  562. }
  563. if (count <= 0) {
  564. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  565. ("-HTCWaitCtrlMessageRecv: Timeout!\n"));
  566. return A_ECOMM;
  567. }
  568. #endif
  569. AR_DEBUG_PRINTF(ATH_DEBUG_TRC, ("-HTCWaitCtrlMessageRecv success\n"));
  570. return A_OK;
  571. }
  572. static A_STATUS htc_process_trailer(HTC_TARGET *target,
  573. uint8_t *pBuffer,
  574. int Length, HTC_ENDPOINT_ID FromEndpoint)
  575. {
  576. HTC_RECORD_HDR *pRecord;
  577. uint8_t htc_rec_id;
  578. uint8_t htc_rec_len;
  579. uint8_t *pRecordBuf;
  580. uint8_t *pOrigBuffer;
  581. int origLength;
  582. A_STATUS status;
  583. AR_DEBUG_PRINTF(ATH_DEBUG_RECV,
  584. ("+htc_process_trailer (length:%d) \n", Length));
  585. if (AR_DEBUG_LVL_CHECK(ATH_DEBUG_RECV)) {
  586. AR_DEBUG_PRINTBUF(pBuffer, Length, "Recv Trailer");
  587. }
  588. pOrigBuffer = pBuffer;
  589. origLength = Length;
  590. status = A_OK;
  591. while (Length > 0) {
  592. if (Length < sizeof(HTC_RECORD_HDR)) {
  593. status = A_EPROTO;
  594. break;
  595. }
  596. /* these are byte aligned structs */
  597. pRecord = (HTC_RECORD_HDR *) pBuffer;
  598. Length -= sizeof(HTC_RECORD_HDR);
  599. pBuffer += sizeof(HTC_RECORD_HDR);
  600. htc_rec_len = HTC_GET_FIELD(pRecord, HTC_RECORD_HDR, LENGTH);
  601. htc_rec_id = HTC_GET_FIELD(pRecord, HTC_RECORD_HDR, RECORDID);
  602. if (htc_rec_len > Length) {
  603. /* no room left in buffer for record */
  604. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  605. (" invalid record length: %d (id:%d) buffer has: %d bytes left \n",
  606. htc_rec_len, htc_rec_id, Length));
  607. status = A_EPROTO;
  608. break;
  609. }
  610. /* start of record follows the header */
  611. pRecordBuf = pBuffer;
  612. switch (htc_rec_id) {
  613. case HTC_RECORD_CREDITS:
  614. AR_DEBUG_ASSERT(htc_rec_len >=
  615. sizeof(HTC_CREDIT_REPORT));
  616. htc_process_credit_rpt(target,
  617. (HTC_CREDIT_REPORT *) pRecordBuf,
  618. htc_rec_len /
  619. (sizeof(HTC_CREDIT_REPORT)),
  620. FromEndpoint);
  621. break;
  622. #ifdef HIF_SDIO
  623. case HTC_RECORD_LOOKAHEAD:
  624. /* Process in HIF layer */
  625. break;
  626. case HTC_RECORD_LOOKAHEAD_BUNDLE:
  627. /* Process in HIF layer */
  628. break;
  629. #endif /* HIF_SDIO */
  630. default:
  631. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  632. (" HTC unhandled record: id:%d length:%d \n",
  633. htc_rec_id, htc_rec_len));
  634. break;
  635. }
  636. if (A_FAILED(status)) {
  637. break;
  638. }
  639. /* advance buffer past this record for next time around */
  640. pBuffer += htc_rec_len;
  641. Length -= htc_rec_len;
  642. }
  643. if (A_FAILED(status)) {
  644. debug_dump_bytes(pOrigBuffer, origLength, "BAD Recv Trailer");
  645. }
  646. AR_DEBUG_PRINTF(ATH_DEBUG_RECV, ("-htc_process_trailer \n"));
  647. return status;
  648. }