htc_recv.c 19 KB

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