htc_recv.c 18 KB

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