htc_send.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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 <cdf_nbuf.h> /* cdf_nbuf_t */
  29. #include <cdf_memory.h> /* cdf_mem_malloc */
  30. #include "epping_main.h"
  31. /* #define USB_HIF_SINGLE_PIPE_DATA_SCHED */
  32. /* #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED */
  33. #define DATA_EP_SIZE 4
  34. /* #endif */
  35. #define HTC_DATA_RESOURCE_THRS 256
  36. #define HTC_DATA_MINDESC_PERPACKET 2
  37. typedef enum _HTC_SEND_QUEUE_RESULT {
  38. HTC_SEND_QUEUE_OK = 0, /* packet was queued */
  39. HTC_SEND_QUEUE_DROP = 1, /* this packet should be dropped */
  40. } HTC_SEND_QUEUE_RESULT;
  41. #ifndef DEBUG_CREDIT
  42. #define DEBUG_CREDIT 0
  43. #endif
  44. #if DEBUG_CREDIT
  45. /* bit mask to enable debug certain endpoint */
  46. static unsigned ep_debug_mask =
  47. (1 << ENDPOINT_0) | (1 << ENDPOINT_1) | (1 << ENDPOINT_2);
  48. #endif
  49. /* HTC Control Path Credit History */
  50. A_UINT32 g_htc_credit_history_idx = 0;
  51. HTC_CREDIT_HISTORY htc_credit_history_buffer[HTC_CREDIT_HISTORY_MAX];
  52. /**
  53. * htc_credit_record() - records tx que state & credit transactions
  54. * @type: type of echange can be HTC_REQUEST_CREDIT
  55. * or HTC_PROCESS_CREDIT_REPORT
  56. * @tx_credits: current number of tx_credits
  57. * @htc_tx_queue_depth: current hct tx queue depth
  58. *
  59. * This function records the credits and pending commands whenever a command is
  60. * sent or credits are returned. Call this after the credits have been updated
  61. * according to the transaction. Call this before dequeing commands.
  62. *
  63. * Consider making this function accept an HTC_ENDPOINT and find the current
  64. * credits and queue depth itself.
  65. *
  66. * Consider moving the LOCK_HTC_CREDIT(target); logic into this function as well.
  67. */
  68. void htc_credit_record(htc_credit_exchange_type type, uint32_t tx_credit,
  69. uint32_t htc_tx_queue_depth) {
  70. if (HTC_CREDIT_HISTORY_MAX <= g_htc_credit_history_idx)
  71. g_htc_credit_history_idx = 0;
  72. htc_credit_history_buffer[g_htc_credit_history_idx].type = type;
  73. htc_credit_history_buffer[g_htc_credit_history_idx].time =
  74. cdf_get_log_timestamp();
  75. htc_credit_history_buffer[g_htc_credit_history_idx].tx_credit =
  76. tx_credit;
  77. htc_credit_history_buffer[g_htc_credit_history_idx].htc_tx_queue_depth =
  78. htc_tx_queue_depth;
  79. g_htc_credit_history_idx++;
  80. #ifdef QCA_WIFI_3_0_EMU
  81. if (type == HTC_REQUEST_CREDIT)
  82. printk("\nrequest_credits-> current_credit %d, pending commands %d\n",
  83. tx_credit, htc_tx_queue_depth);
  84. else if (type == HTC_PROCESS_CREDIT_REPORT)
  85. printk("\ncredit_report<- current_credit %d, pending commands %d\n",
  86. tx_credit, htc_tx_queue_depth);
  87. #endif
  88. }
  89. void htc_dump_counter_info(HTC_HANDLE HTCHandle)
  90. {
  91. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  92. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  93. ("\n%s: ce_send_cnt = %d, TX_comp_cnt = %d\n",
  94. __func__, target->ce_send_cnt, target->TX_comp_cnt));
  95. }
  96. void htc_get_control_endpoint_tx_host_credits(HTC_HANDLE HTCHandle, int *credits)
  97. {
  98. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  99. HTC_ENDPOINT *pEndpoint;
  100. int i;
  101. if (!credits || !target) {
  102. AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: invalid args", __func__));
  103. return;
  104. }
  105. *credits = 0;
  106. LOCK_HTC_TX(target);
  107. for (i = 0; i < ENDPOINT_MAX; i++) {
  108. pEndpoint = &target->endpoint[i];
  109. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  110. *credits = pEndpoint->TxCredits;
  111. break;
  112. }
  113. }
  114. UNLOCK_HTC_TX(target);
  115. }
  116. static INLINE void restore_tx_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
  117. {
  118. if (pPacket->PktInfo.AsTx.Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) {
  119. cdf_nbuf_t netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  120. cdf_nbuf_unmap(target->osdev, netbuf, CDF_DMA_TO_DEVICE);
  121. cdf_nbuf_pull_head(netbuf, sizeof(HTC_FRAME_HDR));
  122. pPacket->PktInfo.AsTx.Flags &= ~HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  123. }
  124. }
  125. static void do_send_completion(HTC_ENDPOINT *pEndpoint,
  126. HTC_PACKET_QUEUE *pQueueToIndicate)
  127. {
  128. do {
  129. if (HTC_QUEUE_EMPTY(pQueueToIndicate)) {
  130. /* nothing to indicate */
  131. break;
  132. }
  133. if (pEndpoint->EpCallBacks.EpTxCompleteMultiple != NULL) {
  134. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  135. (" HTC calling ep %d, send complete multiple callback (%d pkts) \n",
  136. pEndpoint->Id,
  137. HTC_PACKET_QUEUE_DEPTH
  138. (pQueueToIndicate)));
  139. /* a multiple send complete handler is being used, pass the queue to the handler */
  140. pEndpoint->EpCallBacks.EpTxCompleteMultiple(pEndpoint->
  141. EpCallBacks.
  142. pContext,
  143. pQueueToIndicate);
  144. /* all packets are now owned by the callback, reset queue to be safe */
  145. INIT_HTC_PACKET_QUEUE(pQueueToIndicate);
  146. } else {
  147. HTC_PACKET *pPacket;
  148. /* using legacy EpTxComplete */
  149. do {
  150. pPacket = htc_packet_dequeue(pQueueToIndicate);
  151. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  152. (" HTC calling ep %d send complete callback on packet %p \n",
  153. pEndpoint->Id, pPacket));
  154. pEndpoint->EpCallBacks.EpTxComplete(pEndpoint->
  155. EpCallBacks.
  156. pContext,
  157. pPacket);
  158. } while (!HTC_QUEUE_EMPTY(pQueueToIndicate));
  159. }
  160. } while (false);
  161. }
  162. static void send_packet_completion(HTC_TARGET *target, HTC_PACKET *pPacket)
  163. {
  164. HTC_ENDPOINT *pEndpoint = &target->endpoint[pPacket->Endpoint];
  165. HTC_PACKET_QUEUE container;
  166. restore_tx_packet(target, pPacket);
  167. INIT_HTC_PACKET_QUEUE_AND_ADD(&container, pPacket);
  168. /* do completion */
  169. do_send_completion(pEndpoint, &container);
  170. }
  171. void htc_send_complete_check_cleanup(void *context)
  172. {
  173. HTC_ENDPOINT *pEndpoint = (HTC_ENDPOINT *) context;
  174. htc_send_complete_check(pEndpoint, 1);
  175. }
  176. HTC_PACKET *allocate_htc_bundle_packet(HTC_TARGET *target)
  177. {
  178. HTC_PACKET *pPacket;
  179. HTC_PACKET_QUEUE *pQueueSave;
  180. cdf_nbuf_t netbuf;
  181. LOCK_HTC_TX(target);
  182. if (NULL == target->pBundleFreeList) {
  183. UNLOCK_HTC_TX(target);
  184. netbuf = cdf_nbuf_alloc(NULL,
  185. target->MaxMsgsPerHTCBundle *
  186. target->TargetCreditSize, 0, 4, false);
  187. AR_DEBUG_ASSERT(netbuf);
  188. if (!netbuf) {
  189. return NULL;
  190. }
  191. pPacket = cdf_mem_malloc(sizeof(HTC_PACKET));
  192. AR_DEBUG_ASSERT(pPacket);
  193. if (!pPacket) {
  194. cdf_nbuf_free(netbuf);
  195. return NULL;
  196. }
  197. pQueueSave = cdf_mem_malloc(sizeof(HTC_PACKET_QUEUE));
  198. AR_DEBUG_ASSERT(pQueueSave);
  199. if (!pQueueSave) {
  200. cdf_nbuf_free(netbuf);
  201. cdf_mem_free(pPacket);
  202. return NULL;
  203. }
  204. INIT_HTC_PACKET_QUEUE(pQueueSave);
  205. pPacket->pContext = pQueueSave;
  206. SET_HTC_PACKET_NET_BUF_CONTEXT(pPacket, netbuf);
  207. pPacket->pBuffer = cdf_nbuf_data(netbuf);
  208. pPacket->BufferLength = cdf_nbuf_len(netbuf);
  209. /* store the original head room so that we can restore this when we "free" the packet */
  210. /* free packet puts the packet back on the free list */
  211. pPacket->netbufOrigHeadRoom = cdf_nbuf_headroom(netbuf);
  212. return pPacket;
  213. }
  214. /* already done malloc - restore from free list */
  215. pPacket = target->pBundleFreeList;
  216. AR_DEBUG_ASSERT(pPacket);
  217. if (!pPacket) {
  218. UNLOCK_HTC_TX(target);
  219. return NULL;
  220. }
  221. target->pBundleFreeList = (HTC_PACKET *) pPacket->ListLink.pNext;
  222. UNLOCK_HTC_TX(target);
  223. pPacket->ListLink.pNext = NULL;
  224. return pPacket;
  225. }
  226. void free_htc_bundle_packet(HTC_TARGET *target, HTC_PACKET *pPacket)
  227. {
  228. A_UINT32 curentHeadRoom;
  229. cdf_nbuf_t netbuf;
  230. HTC_PACKET_QUEUE *pQueueSave;
  231. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  232. AR_DEBUG_ASSERT(netbuf);
  233. if (!netbuf) {
  234. AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("\n%s: Invalid netbuf in HTC "
  235. "Packet\n", __func__));
  236. return;
  237. }
  238. /* HIF adds data to the headroom section of the nbuf, restore the original */
  239. /* size. If this is not done, headroom keeps shrinking with every HIF send */
  240. /* and eventually HIF ends up doing another malloc big enough to store the */
  241. /* data + its header */
  242. curentHeadRoom = cdf_nbuf_headroom(netbuf);
  243. cdf_nbuf_pull_head(netbuf,
  244. pPacket->netbufOrigHeadRoom - curentHeadRoom);
  245. cdf_nbuf_trim_tail(netbuf, cdf_nbuf_len(netbuf));
  246. /* restore the pBuffer pointer. HIF changes this */
  247. pPacket->pBuffer = cdf_nbuf_data(netbuf);
  248. pPacket->BufferLength = cdf_nbuf_len(netbuf);
  249. /* restore queue */
  250. pQueueSave = (HTC_PACKET_QUEUE *) pPacket->pContext;
  251. AR_DEBUG_ASSERT(pQueueSave);
  252. INIT_HTC_PACKET_QUEUE(pQueueSave);
  253. LOCK_HTC_TX(target);
  254. if (target->pBundleFreeList == NULL) {
  255. target->pBundleFreeList = pPacket;
  256. pPacket->ListLink.pNext = NULL;
  257. } else {
  258. pPacket->ListLink.pNext = (DL_LIST *) target->pBundleFreeList;
  259. target->pBundleFreeList = pPacket;
  260. }
  261. UNLOCK_HTC_TX(target);
  262. }
  263. #if defined(HIF_USB) || defined(HIF_SDIO)
  264. #ifdef ENABLE_BUNDLE_TX
  265. static A_STATUS htc_send_bundled_netbuf(HTC_TARGET *target,
  266. HTC_ENDPOINT *pEndpoint,
  267. unsigned char *pBundleBuffer,
  268. HTC_PACKET *pPacketTx)
  269. {
  270. cdf_size_t data_len;
  271. A_STATUS status;
  272. cdf_nbuf_t bundleBuf;
  273. uint32_t data_attr = 0;
  274. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  275. data_len = pBundleBuffer - cdf_nbuf_data(bundleBuf);
  276. cdf_nbuf_put_tail(bundleBuf, data_len);
  277. SET_HTC_PACKET_INFO_TX(pPacketTx,
  278. target,
  279. pBundleBuffer,
  280. data_len,
  281. pEndpoint->Id, HTC_TX_PACKET_TAG_BUNDLED);
  282. LOCK_HTC_TX(target);
  283. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacketTx);
  284. UNLOCK_HTC_TX(target);
  285. #if DEBUG_BUNDLE
  286. cdf_print(" Send bundle EP%d buffer size:0x%x, total:0x%x, count:%d.\n",
  287. pEndpoint->Id,
  288. pEndpoint->TxCreditSize,
  289. data_len, data_len / pEndpoint->TxCreditSize);
  290. #endif
  291. status = hif_send_head(target->hif_dev,
  292. pEndpoint->UL_PipeID,
  293. pEndpoint->Id, data_len, bundleBuf, data_attr);
  294. if (status != A_OK) {
  295. cdf_print("%s:hif_send_head failed(len=%d).\n", __FUNCTION__,
  296. data_len);
  297. }
  298. return status;
  299. }
  300. static void htc_issue_packets_bundle(HTC_TARGET *target,
  301. HTC_ENDPOINT *pEndpoint,
  302. HTC_PACKET_QUEUE *pPktQueue)
  303. {
  304. int i, frag_count, nbytes;
  305. cdf_nbuf_t netbuf, bundleBuf;
  306. unsigned char *pBundleBuffer = NULL;
  307. HTC_PACKET *pPacket = NULL, *pPacketTx = NULL;
  308. HTC_FRAME_HDR *pHtcHdr;
  309. int creditPad, creditRemainder, transferLength, bundlesSpaceRemaining =
  310. 0;
  311. HTC_PACKET_QUEUE *pQueueSave = NULL;
  312. bundlesSpaceRemaining =
  313. target->MaxMsgsPerHTCBundle * pEndpoint->TxCreditSize;
  314. pPacketTx = allocate_htc_bundle_packet(target);
  315. if (!pPacketTx) {
  316. /* good time to panic */
  317. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  318. ("allocate_htc_bundle_packet failed \n"));
  319. AR_DEBUG_ASSERT(false);
  320. return;
  321. }
  322. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  323. pBundleBuffer = cdf_nbuf_data(bundleBuf);
  324. pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
  325. while (1) {
  326. pPacket = htc_packet_dequeue(pPktQueue);
  327. if (pPacket == NULL) {
  328. break;
  329. }
  330. creditPad = 0;
  331. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  332. creditRemainder = transferLength % pEndpoint->TxCreditSize;
  333. if (creditRemainder != 0) {
  334. if (transferLength < pEndpoint->TxCreditSize) {
  335. creditPad =
  336. pEndpoint->TxCreditSize - transferLength;
  337. } else {
  338. creditPad = creditRemainder;
  339. }
  340. transferLength += creditPad;
  341. }
  342. if (bundlesSpaceRemaining < transferLength) {
  343. /* send out previous buffer */
  344. htc_send_bundled_netbuf(target, pEndpoint, pBundleBuffer,
  345. pPacketTx);
  346. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue) <
  347. HTC_MIN_MSG_PER_BUNDLE) {
  348. return;
  349. }
  350. bundlesSpaceRemaining =
  351. target->MaxMsgsPerHTCBundle *
  352. pEndpoint->TxCreditSize;
  353. pPacketTx = allocate_htc_bundle_packet(target);
  354. if (!pPacketTx) {
  355. /* good time to panic */
  356. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  357. ("allocate_htc_bundle_packet failed \n"));
  358. AR_DEBUG_ASSERT(false);
  359. return;
  360. }
  361. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  362. pBundleBuffer = cdf_nbuf_data(bundleBuf);
  363. pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
  364. }
  365. bundlesSpaceRemaining -= transferLength;
  366. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  367. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  368. HTC_WRITE32(pHtcHdr,
  369. SM(pPacket->ActualLength,
  370. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->PktInfo.
  371. AsTx.
  372. SendFlags |
  373. HTC_FLAGS_SEND_BUNDLE,
  374. HTC_FRAME_HDR_FLAGS)
  375. | SM(pPacket->Endpoint, HTC_FRAME_HDR_ENDPOINTID));
  376. HTC_WRITE32((A_UINT32 *) pHtcHdr + 1,
  377. SM(pPacket->PktInfo.AsTx.SeqNo,
  378. HTC_FRAME_HDR_CONTROLBYTES1) | SM(creditPad,
  379. HTC_FRAME_HDR_RESERVED));
  380. pHtcHdr->reserved = creditPad;
  381. frag_count = cdf_nbuf_get_num_frags(netbuf);
  382. nbytes = pPacket->ActualLength + HTC_HDR_LENGTH;
  383. for (i = 0; i < frag_count && nbytes > 0; i++) {
  384. int frag_len = cdf_nbuf_get_frag_len(netbuf, i);
  385. unsigned char *frag_addr =
  386. cdf_nbuf_get_frag_vaddr(netbuf, i);
  387. if (frag_len > nbytes) {
  388. frag_len = nbytes;
  389. }
  390. A_MEMCPY(pBundleBuffer, frag_addr, frag_len);
  391. nbytes -= frag_len;
  392. pBundleBuffer += frag_len;
  393. }
  394. HTC_PACKET_ENQUEUE(pQueueSave, pPacket);
  395. pBundleBuffer += creditPad;
  396. }
  397. if (pBundleBuffer != cdf_nbuf_data(bundleBuf)) {
  398. /* send out remaining buffer */
  399. htc_send_bundled_netbuf(target, pEndpoint, pBundleBuffer,
  400. pPacketTx);
  401. } else {
  402. free_htc_bundle_packet(target, pPacketTx);
  403. }
  404. }
  405. #endif /* ENABLE_BUNDLE_TX */
  406. #endif
  407. static A_STATUS htc_issue_packets(HTC_TARGET *target,
  408. HTC_ENDPOINT *pEndpoint,
  409. HTC_PACKET_QUEUE *pPktQueue)
  410. {
  411. A_STATUS status = A_OK;
  412. cdf_nbuf_t netbuf;
  413. HTC_PACKET *pPacket = NULL;
  414. uint16_t payloadLen;
  415. HTC_FRAME_HDR *pHtcHdr;
  416. uint32_t data_attr = 0;
  417. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  418. ("+htc_issue_packets: Queue: %p, Pkts %d \n", pPktQueue,
  419. HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
  420. while (true) {
  421. #if defined(HIF_USB) || defined(HIF_SDIO)
  422. #ifdef ENABLE_BUNDLE_TX
  423. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint) &&
  424. HTC_ENABLE_BUNDLE(target) &&
  425. HTC_PACKET_QUEUE_DEPTH(pPktQueue) >=
  426. HTC_MIN_MSG_PER_BUNDLE) {
  427. htc_issue_packets_bundle(target, pEndpoint, pPktQueue);
  428. }
  429. #endif
  430. #endif
  431. /* if not bundling or there was a packet that could not be placed in a bundle,
  432. * and send it by normal way
  433. */
  434. pPacket = htc_packet_dequeue(pPktQueue);
  435. if (NULL == pPacket) {
  436. /* local queue is fully drained */
  437. break;
  438. }
  439. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  440. AR_DEBUG_ASSERT(netbuf);
  441. /* Non-credit enabled endpoints have been mapped and setup by now,
  442. * so no need to revisit the HTC headers
  443. */
  444. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  445. payloadLen = pPacket->ActualLength;
  446. /* setup HTC frame header */
  447. pHtcHdr =
  448. (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf,
  449. 0);
  450. AR_DEBUG_ASSERT(pHtcHdr);
  451. HTC_WRITE32(pHtcHdr,
  452. SM(payloadLen,
  453. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->
  454. PktInfo.
  455. AsTx.
  456. SendFlags,
  457. HTC_FRAME_HDR_FLAGS)
  458. | SM(pPacket->Endpoint,
  459. HTC_FRAME_HDR_ENDPOINTID));
  460. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  461. SM(pPacket->PktInfo.AsTx.SeqNo,
  462. HTC_FRAME_HDR_CONTROLBYTES1));
  463. /*
  464. * Now that the HTC frame header has been added, the netbuf can be
  465. * mapped. This only applies to non-data frames, since data frames
  466. * were already mapped as they entered into the driver.
  467. * Check the "FIXUP_NETBUF" flag to see whether this is a data netbuf
  468. * that is already mapped, or a non-data netbuf that needs to be
  469. * mapped.
  470. */
  471. if (pPacket->PktInfo.AsTx.
  472. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) {
  473. cdf_nbuf_map(target->osdev,
  474. GET_HTC_PACKET_NET_BUF_CONTEXT
  475. (pPacket), CDF_DMA_TO_DEVICE);
  476. }
  477. }
  478. LOCK_HTC_TX(target);
  479. /* store in look up queue to match completions */
  480. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  481. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  482. pEndpoint->ul_outstanding_cnt++;
  483. UNLOCK_HTC_TX(target);
  484. hif_send_complete_check(target->hif_dev, pEndpoint->UL_PipeID, false);
  485. status = hif_send_head(target->hif_dev,
  486. pEndpoint->UL_PipeID, pEndpoint->Id,
  487. HTC_HDR_LENGTH + pPacket->ActualLength,
  488. netbuf, data_attr);
  489. #if DEBUG_BUNDLE
  490. cdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
  491. pEndpoint->Id,
  492. pEndpoint->TxCreditSize,
  493. HTC_HDR_LENGTH + pPacket->ActualLength);
  494. #endif
  495. target->ce_send_cnt++;
  496. if (cdf_unlikely(A_FAILED(status))) {
  497. if (status != A_NO_RESOURCE) {
  498. /* TODO : if more than 1 endpoint maps to the same PipeID it is possible
  499. * to run out of resources in the HIF layer. Don't emit the error */
  500. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  501. ("hif_send Failed status:%d \n",
  502. status));
  503. }
  504. LOCK_HTC_TX(target);
  505. target->ce_send_cnt--;
  506. pEndpoint->ul_outstanding_cnt--;
  507. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  508. /* reclaim credits */
  509. #if defined(HIF_USB)
  510. if (pEndpoint->Id >= ENDPOINT_2
  511. && pEndpoint->Id <= ENDPOINT_5)
  512. target->avail_tx_credits +=
  513. pPacket->PktInfo.AsTx.CreditsUsed;
  514. else
  515. pEndpoint->TxCredits +=
  516. pPacket->PktInfo.AsTx.CreditsUsed;
  517. #else
  518. pEndpoint->TxCredits +=
  519. pPacket->PktInfo.AsTx.CreditsUsed;
  520. #endif
  521. /* put it back into the callers queue */
  522. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  523. UNLOCK_HTC_TX(target);
  524. break;
  525. }
  526. }
  527. if (cdf_unlikely(A_FAILED(status))) {
  528. #if defined(HIF_USB)
  529. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5)
  530. target->avail_tx_credits +=
  531. pPacket->PktInfo.AsTx.CreditsUsed;
  532. else
  533. pEndpoint->TxCredits +=
  534. pPacket->PktInfo.AsTx.CreditsUsed;
  535. #endif
  536. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  537. ("htc_issue_packets, failed pkt:0x%p status:%d",
  538. pPacket, status));
  539. }
  540. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets \n"));
  541. return status;
  542. }
  543. /* get HTC send packets from the TX queue on an endpoint, based on available credits */
  544. void get_htc_send_packets_credit_based(HTC_TARGET *target,
  545. HTC_ENDPOINT *pEndpoint,
  546. HTC_PACKET_QUEUE *pQueue)
  547. {
  548. int creditsRequired;
  549. int remainder;
  550. A_UINT8 sendFlags;
  551. HTC_PACKET *pPacket;
  552. unsigned int transferLength;
  553. /****** NOTE : the TX lock is held when this function is called *****************/
  554. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+get_htc_send_packets_credit_based \n"));
  555. /* loop until we can grab as many packets out of the queue as we can */
  556. while (true) {
  557. sendFlags = 0;
  558. /* get packet at head, but don't remove it */
  559. pPacket = htc_get_pkt_at_head(&pEndpoint->TxQueue);
  560. if (pPacket == NULL) {
  561. break;
  562. }
  563. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  564. (" Got head packet:%p , Queue Depth: %d\n",
  565. pPacket,
  566. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)));
  567. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  568. if (transferLength <= pEndpoint->TxCreditSize) {
  569. creditsRequired = 1;
  570. } else {
  571. /* figure out how many credits this message requires */
  572. creditsRequired =
  573. transferLength / pEndpoint->TxCreditSize;
  574. remainder = transferLength % pEndpoint->TxCreditSize;
  575. if (remainder) {
  576. creditsRequired++;
  577. }
  578. }
  579. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  580. (" Credits Required:%d Got:%d\n",
  581. creditsRequired, pEndpoint->TxCredits));
  582. if (pEndpoint->Id == ENDPOINT_0) {
  583. /* endpoint 0 is special, it always has a credit and does not require credit based
  584. * flow control */
  585. creditsRequired = 0;
  586. #if defined(HIF_USB)
  587. } else if (pEndpoint->Id >= ENDPOINT_2
  588. && pEndpoint->Id <= ENDPOINT_5) {
  589. if (target->avail_tx_credits < creditsRequired)
  590. break;
  591. target->avail_tx_credits -= creditsRequired;
  592. if (target->avail_tx_credits < 9) {
  593. /* tell the target we need credits ASAP! */
  594. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  595. INC_HTC_EP_STAT(pEndpoint,
  596. TxCreditLowIndications, 1);
  597. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  598. (" Host Needs Credits \n"));
  599. }
  600. #endif
  601. } else {
  602. if (pEndpoint->TxCredits < creditsRequired) {
  603. #if DEBUG_CREDIT
  604. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  605. (" EP%d, No Credit now. %d < %d\n",
  606. pEndpoint->Id,
  607. pEndpoint->TxCredits,
  608. creditsRequired));
  609. #endif
  610. break;
  611. }
  612. pEndpoint->TxCredits -= creditsRequired;
  613. INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
  614. creditsRequired);
  615. /* check if we need credits back from the target */
  616. if (pEndpoint->TxCredits <=
  617. pEndpoint->TxCreditsPerMaxMsg) {
  618. /* tell the target we need credits ASAP! */
  619. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  620. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  621. LOCK_HTC_CREDIT(target);
  622. htc_credit_record(HTC_REQUEST_CREDIT,
  623. pEndpoint->TxCredits,
  624. HTC_PACKET_QUEUE_DEPTH
  625. (&pEndpoint->
  626. TxQueue));
  627. UNLOCK_HTC_CREDIT(target);
  628. }
  629. INC_HTC_EP_STAT(pEndpoint,
  630. TxCreditLowIndications, 1);
  631. #if DEBUG_CREDIT
  632. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  633. (" EP%d Needs Credits\n",
  634. pEndpoint->Id));
  635. #endif
  636. }
  637. }
  638. /* now we can fully dequeue */
  639. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  640. if (pPacket) {
  641. /* save the number of credits this packet consumed */
  642. pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
  643. /* save send flags */
  644. pPacket->PktInfo.AsTx.SendFlags = sendFlags;
  645. /* queue this packet into the caller's queue */
  646. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  647. }
  648. }
  649. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets_credit_based \n"));
  650. }
  651. void get_htc_send_packets(HTC_TARGET *target,
  652. HTC_ENDPOINT *pEndpoint,
  653. HTC_PACKET_QUEUE *pQueue, int Resources)
  654. {
  655. HTC_PACKET *pPacket;
  656. /****** NOTE : the TX lock is held when this function is called *****************/
  657. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  658. ("+get_htc_send_packets %d resources\n", Resources));
  659. /* loop until we can grab as many packets out of the queue as we can */
  660. while (Resources > 0) {
  661. int num_frags;
  662. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  663. if (pPacket == NULL) {
  664. break;
  665. }
  666. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  667. (" Got packet:%p , New Queue Depth: %d\n",
  668. pPacket,
  669. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)));
  670. /* For non-credit path the sequence number is already embedded
  671. * in the constructed HTC header
  672. */
  673. #if 0
  674. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  675. pEndpoint->SeqNo++;
  676. #endif
  677. pPacket->PktInfo.AsTx.SendFlags = 0;
  678. pPacket->PktInfo.AsTx.CreditsUsed = 0;
  679. /* queue this packet into the caller's queue */
  680. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  681. /*
  682. * FIX THIS:
  683. * For now, avoid calling cdf_nbuf_get_num_frags before calling
  684. * cdf_nbuf_map, because the MacOS version of cdf_nbuf_t doesn't
  685. * support cdf_nbuf_get_num_frags until after cdf_nbuf_map has
  686. * been done.
  687. * Assume that the non-data netbufs, i.e. the WMI message netbufs,
  688. * consist of a single fragment.
  689. */
  690. num_frags =
  691. (pPacket->PktInfo.AsTx.
  692. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1
  693. /* WMI messages are in a single-fragment network buffer */ :
  694. cdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
  695. (pPacket));
  696. Resources -= num_frags;
  697. }
  698. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets \n"));
  699. }
  700. static HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
  701. HTC_ENDPOINT *pEndpoint,
  702. HTC_PACKET_QUEUE *pCallersSendQueue)
  703. {
  704. HTC_PACKET_QUEUE sendQueue; /* temp queue to hold packets at various stages */
  705. HTC_PACKET *pPacket;
  706. int tx_resources;
  707. int overflow;
  708. HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
  709. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%p Depth:%d)\n",
  710. pCallersSendQueue,
  711. (pCallersSendQueue ==
  712. NULL) ? 0 :
  713. HTC_PACKET_QUEUE_DEPTH
  714. (pCallersSendQueue)));
  715. /* init the local send queue */
  716. INIT_HTC_PACKET_QUEUE(&sendQueue);
  717. do {
  718. if (NULL == pCallersSendQueue) {
  719. /* caller didn't provide a queue, just wants us to check queues and send */
  720. break;
  721. }
  722. if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
  723. /* empty queue */
  724. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  725. HTC_PKT_Q_EMPTY);
  726. result = HTC_SEND_QUEUE_DROP;
  727. break;
  728. }
  729. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
  730. pEndpoint->MaxTxQueueDepth) {
  731. /* we've already overflowed */
  732. overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  733. } else {
  734. /* figure out how much we will overflow by */
  735. overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  736. overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  737. /* figure out how much we will overflow the TX queue by */
  738. overflow -= pEndpoint->MaxTxQueueDepth;
  739. }
  740. /* if overflow is negative or zero, we are okay */
  741. if (overflow > 0) {
  742. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  743. (" Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d \n",
  744. pEndpoint->Id, overflow,
  745. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  746. TxQueue),
  747. pEndpoint->MaxTxQueueDepth));
  748. }
  749. if ((overflow <= 0)
  750. || (pEndpoint->EpCallBacks.EpSendFull == NULL)) {
  751. /* all packets will fit or caller did not provide send full indication handler
  752. * -- just move all of them to the local sendQueue object */
  753. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
  754. pCallersSendQueue);
  755. } else {
  756. int i;
  757. int goodPkts =
  758. HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
  759. overflow;
  760. A_ASSERT(goodPkts >= 0);
  761. /* we have overflowed, and a callback is provided */
  762. /* dequeue all non-overflow packets into the sendqueue */
  763. for (i = 0; i < goodPkts; i++) {
  764. /* pop off caller's queue */
  765. pPacket = htc_packet_dequeue(pCallersSendQueue);
  766. A_ASSERT(pPacket != NULL);
  767. /* insert into local queue */
  768. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  769. }
  770. /* the caller's queue has all the packets that won't fit */
  771. /* walk through the caller's queue and indicate each one to the send full handler */
  772. ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
  773. QueueHead, pPacket,
  774. HTC_PACKET, ListLink) {
  775. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  776. (" Indicating overflowed TX packet: %p \n",
  777. pPacket));
  778. /*
  779. * Remove headroom reserved for HTC_FRAME_HDR before giving
  780. * the packet back to the user via the EpSendFull callback.
  781. */
  782. restore_tx_packet(target, pPacket);
  783. if (pEndpoint->EpCallBacks.
  784. EpSendFull(pEndpoint->EpCallBacks.pContext,
  785. pPacket) == HTC_SEND_FULL_DROP) {
  786. /* callback wants the packet dropped */
  787. INC_HTC_EP_STAT(pEndpoint, TxDropped,
  788. 1);
  789. /* leave this one in the caller's queue for cleanup */
  790. } else {
  791. /* callback wants to keep this packet, remove from caller's queue */
  792. HTC_PACKET_REMOVE(pCallersSendQueue,
  793. pPacket);
  794. /* put it in the send queue */
  795. /* add HTC_FRAME_HDR space reservation again */
  796. cdf_nbuf_push_head
  797. (GET_HTC_PACKET_NET_BUF_CONTEXT
  798. (pPacket), sizeof(HTC_FRAME_HDR));
  799. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  800. }
  801. }
  802. ITERATE_END;
  803. if (HTC_QUEUE_EMPTY(&sendQueue)) {
  804. /* no packets made it in, caller will cleanup */
  805. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  806. HTC_SEND_Q_EMPTY);
  807. result = HTC_SEND_QUEUE_DROP;
  808. break;
  809. }
  810. }
  811. } while (false);
  812. if (result != HTC_SEND_QUEUE_OK) {
  813. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: \n"));
  814. return result;
  815. }
  816. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  817. tx_resources =
  818. hif_get_free_queue_number(target->hif_dev,
  819. pEndpoint->UL_PipeID);
  820. } else {
  821. tx_resources = 0;
  822. }
  823. LOCK_HTC_TX(target);
  824. if (!HTC_QUEUE_EMPTY(&sendQueue)) {
  825. /* transfer packets to tail */
  826. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
  827. &sendQueue);
  828. A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
  829. INIT_HTC_PACKET_QUEUE(&sendQueue);
  830. }
  831. /* increment tx processing count on entry */
  832. cdf_atomic_inc(&pEndpoint->TxProcessCount);
  833. if (cdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  834. /* another thread or task is draining the TX queues on this endpoint
  835. * that thread will reset the tx processing count when the queue is drained */
  836. cdf_atomic_dec(&pEndpoint->TxProcessCount);
  837. UNLOCK_HTC_TX(target);
  838. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy) \n"));
  839. return HTC_SEND_QUEUE_OK;
  840. }
  841. /***** beyond this point only 1 thread may enter ******/
  842. /* now drain the endpoint TX queue for transmission as long as we have enough
  843. * transmit resources */
  844. while (true) {
  845. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0) {
  846. break;
  847. }
  848. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  849. #if DEBUG_CREDIT
  850. int cred = pEndpoint->TxCredits;
  851. #endif
  852. /* credit based mechanism provides flow control based on target transmit resource availability, we
  853. * assume that the HIF layer will always have bus resources greater than target transmit resources */
  854. get_htc_send_packets_credit_based(target, pEndpoint,
  855. &sendQueue);
  856. #if DEBUG_CREDIT
  857. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  858. if (cred - pEndpoint->TxCredits > 0) {
  859. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  860. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  861. pEndpoint->Id, cred,
  862. cred -
  863. pEndpoint->TxCredits,
  864. pEndpoint->TxCredits));
  865. }
  866. }
  867. #endif
  868. } else {
  869. /* get all the packets for this endpoint that we can for this pass */
  870. get_htc_send_packets(target, pEndpoint, &sendQueue,
  871. tx_resources);
  872. }
  873. if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
  874. /* didn't get any packets due to a lack of resources or TX queue was drained */
  875. break;
  876. }
  877. UNLOCK_HTC_TX(target);
  878. /* send what we can */
  879. result = htc_issue_packets(target, pEndpoint, &sendQueue);
  880. if (result) {
  881. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  882. ("htc_issue_packets, failed status:%d put it back to head of callersSendQueue",
  883. result));
  884. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  885. &sendQueue);
  886. LOCK_HTC_TX(target);
  887. break;
  888. }
  889. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  890. tx_resources =
  891. hif_get_free_queue_number(target->hif_dev,
  892. pEndpoint->UL_PipeID);
  893. }
  894. LOCK_HTC_TX(target);
  895. }
  896. UNLOCK_HTC_TX(target);
  897. /* done with this endpoint, we can clear the count */
  898. cdf_atomic_init(&pEndpoint->TxProcessCount);
  899. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: \n"));
  900. return HTC_SEND_QUEUE_OK;
  901. }
  902. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  903. static A_UINT16 htc_send_pkts_sched_check(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID id)
  904. {
  905. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  906. HTC_ENDPOINT *pEndpoint;
  907. HTC_ENDPOINT_ID eid;
  908. HTC_PACKET_QUEUE *pTxQueue;
  909. A_UINT16 resources;
  910. A_UINT16 acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
  911. if (id < ENDPOINT_2 || id > ENDPOINT_5) {
  912. return 1;
  913. }
  914. for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
  915. pEndpoint = &target->endpoint[eid];
  916. pTxQueue = &pEndpoint->TxQueue;
  917. if (HTC_QUEUE_EMPTY(pTxQueue)) {
  918. acQueueStatus[eid - 2] = 1;
  919. }
  920. }
  921. switch (id) {
  922. case ENDPOINT_2: /* BE */
  923. return (acQueueStatus[0] && acQueueStatus[2]
  924. && acQueueStatus[3]);
  925. case ENDPOINT_3: /* BK */
  926. return (acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
  927. && acQueueStatus[3]);
  928. case ENDPOINT_4: /* VI */
  929. return (acQueueStatus[2] && acQueueStatus[3]);
  930. case ENDPOINT_5: /* VO */
  931. return (acQueueStatus[3]);
  932. default:
  933. return 0;
  934. }
  935. }
  936. static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
  937. HTC_PACKET_QUEUE *pPktQueue,
  938. HTC_ENDPOINT_ID eid)
  939. {
  940. HTC_ENDPOINT *pEndpoint;
  941. HTC_PACKET_QUEUE *pTxQueue;
  942. HTC_PACKET *pPacket;
  943. int goodPkts;
  944. pEndpoint = &target->endpoint[eid];
  945. pTxQueue = &pEndpoint->TxQueue;
  946. LOCK_HTC_TX(target);
  947. goodPkts =
  948. pEndpoint->MaxTxQueueDepth -
  949. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  950. if (goodPkts > 0) {
  951. while (!HTC_QUEUE_EMPTY(pPktQueue)) {
  952. pPacket = htc_packet_dequeue(pPktQueue);
  953. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  954. goodPkts--;
  955. if (goodPkts <= 0) {
  956. break;
  957. }
  958. }
  959. }
  960. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
  961. ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
  962. HTC_PACKET, ListLink) {
  963. if (pEndpoint->EpCallBacks.
  964. EpSendFull(pEndpoint->EpCallBacks.pContext,
  965. pPacket) == HTC_SEND_FULL_DROP) {
  966. INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
  967. } else {
  968. HTC_PACKET_REMOVE(pPktQueue, pPacket);
  969. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  970. }
  971. }
  972. ITERATE_END;
  973. }
  974. UNLOCK_HTC_TX(target);
  975. return A_OK;
  976. }
  977. #endif
  978. A_STATUS htc_send_pkts_multiple(HTC_HANDLE HTCHandle, HTC_PACKET_QUEUE *pPktQueue)
  979. {
  980. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  981. HTC_ENDPOINT *pEndpoint;
  982. HTC_PACKET *pPacket;
  983. cdf_nbuf_t netbuf;
  984. HTC_FRAME_HDR *pHtcHdr;
  985. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  986. ("+htc_send_pkts_multiple: Queue: %p, Pkts %d \n",
  987. pPktQueue, HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
  988. /* get packet at head to figure out which endpoint these packets will go into */
  989. pPacket = htc_get_pkt_at_head(pPktQueue);
  990. if (NULL == pPacket) {
  991. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
  992. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_send_pkts_multiple \n"));
  993. return A_EINVAL;
  994. }
  995. AR_DEBUG_ASSERT(pPacket->Endpoint < ENDPOINT_MAX);
  996. pEndpoint = &target->endpoint[pPacket->Endpoint];
  997. if (!pEndpoint->service_id) {
  998. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s service_id is invalid\n",
  999. __func__));
  1000. return A_EINVAL;
  1001. }
  1002. #ifdef HTC_EP_STAT_PROFILING
  1003. LOCK_HTC_TX(target);
  1004. INC_HTC_EP_STAT(pEndpoint, TxPosted, HTC_PACKET_QUEUE_DEPTH(pPktQueue));
  1005. UNLOCK_HTC_TX(target);
  1006. #endif
  1007. /* provide room in each packet's netbuf for the HTC frame header */
  1008. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue, pPacket) {
  1009. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1010. AR_DEBUG_ASSERT(netbuf);
  1011. cdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
  1012. /* setup HTC frame header */
  1013. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  1014. AR_DEBUG_ASSERT(pHtcHdr);
  1015. HTC_WRITE32(pHtcHdr,
  1016. SM(pPacket->ActualLength,
  1017. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->Endpoint,
  1018. HTC_FRAME_HDR_ENDPOINTID));
  1019. LOCK_HTC_TX(target);
  1020. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1021. pEndpoint->SeqNo++;
  1022. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1023. SM(pPacket->PktInfo.AsTx.SeqNo,
  1024. HTC_FRAME_HDR_CONTROLBYTES1));
  1025. UNLOCK_HTC_TX(target);
  1026. /*
  1027. * Now that the HTC frame header has been added, the netbuf can be
  1028. * mapped. This only applies to non-data frames, since data frames
  1029. * were already mapped as they entered into the driver.
  1030. */
  1031. cdf_nbuf_map(target->osdev,
  1032. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  1033. CDF_DMA_TO_DEVICE);
  1034. pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  1035. }
  1036. HTC_PACKET_QUEUE_ITERATE_END;
  1037. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1038. if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id)) {
  1039. htc_send_pkts_sched_queue(HTCHandle, pPktQueue, pEndpoint->Id);
  1040. } else {
  1041. htc_try_send(target, pEndpoint, pPktQueue);
  1042. }
  1043. #else
  1044. htc_try_send(target, pEndpoint, pPktQueue);
  1045. #endif
  1046. /* do completion on any packets that couldn't get in */
  1047. if (!HTC_QUEUE_EMPTY(pPktQueue)) {
  1048. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue, pPacket) {
  1049. /* remove the headroom reserved for HTC_FRAME_HDR */
  1050. restore_tx_packet(target, pPacket);
  1051. if (HTC_STOPPING(target)) {
  1052. pPacket->Status = A_ECANCELED;
  1053. } else {
  1054. pPacket->Status = A_NO_RESOURCE;
  1055. }
  1056. }
  1057. HTC_PACKET_QUEUE_ITERATE_END;
  1058. do_send_completion(pEndpoint, pPktQueue);
  1059. }
  1060. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_send_pkts_multiple \n"));
  1061. return A_OK;
  1062. }
  1063. /* HTC API - htc_send_pkt */
  1064. A_STATUS htc_send_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket)
  1065. {
  1066. HTC_PACKET_QUEUE queue;
  1067. if (HTCHandle == NULL || pPacket == NULL) {
  1068. return A_ERROR;
  1069. }
  1070. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1071. ("+-htc_send_pkt: Enter endPointId: %d, buffer: %p, length: %d \n",
  1072. pPacket->Endpoint, pPacket->pBuffer,
  1073. pPacket->ActualLength));
  1074. INIT_HTC_PACKET_QUEUE_AND_ADD(&queue, pPacket);
  1075. return htc_send_pkts_multiple(HTCHandle, &queue);
  1076. }
  1077. #ifdef ATH_11AC_TXCOMPACT
  1078. A_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, cdf_nbuf_t netbuf, int Epid,
  1079. int ActualLength)
  1080. {
  1081. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1082. HTC_ENDPOINT *pEndpoint;
  1083. HTC_FRAME_HDR *pHtcHdr;
  1084. A_STATUS status = A_OK;
  1085. int tx_resources;
  1086. uint32_t data_attr = 0;
  1087. pEndpoint = &target->endpoint[Epid];
  1088. tx_resources =
  1089. hif_get_free_queue_number(target->hif_dev, pEndpoint->UL_PipeID);
  1090. if (tx_resources < HTC_DATA_RESOURCE_THRS) {
  1091. if (pEndpoint->ul_is_polled) {
  1092. hif_send_complete_check(pEndpoint->target->hif_dev,
  1093. pEndpoint->UL_PipeID, 1);
  1094. tx_resources =
  1095. hif_get_free_queue_number(target->hif_dev,
  1096. pEndpoint->UL_PipeID);
  1097. }
  1098. if (tx_resources < HTC_DATA_MINDESC_PERPACKET) {
  1099. return A_ERROR;
  1100. }
  1101. }
  1102. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  1103. AR_DEBUG_ASSERT(pHtcHdr);
  1104. data_attr = cdf_nbuf_data_attr_get(netbuf);
  1105. HTC_WRITE32(pHtcHdr, SM(ActualLength, HTC_FRAME_HDR_PAYLOADLEN) |
  1106. SM(Epid, HTC_FRAME_HDR_ENDPOINTID));
  1107. /*
  1108. * If the HIF pipe for the data endpoint is polled rather than
  1109. * interrupt-driven, this is a good point to check whether any
  1110. * data previously sent through the HIF pipe have finished being
  1111. * sent.
  1112. * Since this may result in callbacks to htc_tx_completion_handler,
  1113. * which can take the HTC tx lock, make the hif_send_complete_check
  1114. * call before acquiring the HTC tx lock.
  1115. * Call hif_send_complete_check directly, rather than calling
  1116. * htc_send_complete_check, and call the PollTimerStart separately
  1117. * after calling hif_send_head, so the timer will be started to
  1118. * check for completion of the new outstanding download (in the
  1119. * unexpected event that other polling calls don't catch it).
  1120. */
  1121. LOCK_HTC_TX(target);
  1122. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1123. SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
  1124. pEndpoint->SeqNo++;
  1125. NBUF_UPDATE_TX_PKT_COUNT(netbuf, NBUF_TX_PKT_HTC);
  1126. DPTRACE(cdf_dp_trace(netbuf, CDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1127. (uint8_t *)(cdf_nbuf_data(netbuf)),
  1128. sizeof(cdf_nbuf_data(netbuf))));
  1129. status = hif_send_head(target->hif_dev,
  1130. pEndpoint->UL_PipeID,
  1131. pEndpoint->Id, ActualLength, netbuf, data_attr);
  1132. UNLOCK_HTC_TX(target);
  1133. return status;
  1134. }
  1135. #else /*ATH_11AC_TXCOMPACT */
  1136. A_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
  1137. A_UINT8 more_data)
  1138. {
  1139. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1140. HTC_ENDPOINT *pEndpoint;
  1141. HTC_FRAME_HDR *pHtcHdr;
  1142. HTC_PACKET_QUEUE sendQueue;
  1143. cdf_nbuf_t netbuf = NULL;
  1144. int tx_resources;
  1145. A_STATUS status = A_OK;
  1146. uint32_t data_attr = 0;
  1147. if (pPacket) {
  1148. AR_DEBUG_ASSERT(pPacket->Endpoint < ENDPOINT_MAX);
  1149. pEndpoint = &target->endpoint[pPacket->Endpoint];
  1150. /* add HTC_FRAME_HDR in the initial fragment */
  1151. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1152. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  1153. AR_DEBUG_ASSERT(pHtcHdr);
  1154. HTC_WRITE32(pHtcHdr,
  1155. SM(pPacket->ActualLength,
  1156. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->PktInfo.
  1157. AsTx.SendFlags,
  1158. HTC_FRAME_HDR_FLAGS)
  1159. | SM(pPacket->Endpoint, HTC_FRAME_HDR_ENDPOINTID));
  1160. /*
  1161. * If the HIF pipe for the data endpoint is polled rather than
  1162. * interrupt-driven, this is a good point to check whether any
  1163. * data previously sent through the HIF pipe have finished being
  1164. * sent.
  1165. * Since this may result in callbacks to htc_tx_completion_handler,
  1166. * which can take the HTC tx lock, make the hif_send_complete_check
  1167. * call before acquiring the HTC tx lock.
  1168. * Call hif_send_complete_check directly, rather than calling
  1169. * htc_send_complete_check, and call the PollTimerStart separately
  1170. * after calling hif_send_head, so the timer will be started to
  1171. * check for completion of the new outstanding download (in the
  1172. * unexpected event that other polling calls don't catch it).
  1173. */
  1174. if (pEndpoint->ul_is_polled) {
  1175. htc_send_complete_poll_timer_stop(pEndpoint);
  1176. hif_send_complete_check(pEndpoint->target->hif_dev,
  1177. pEndpoint->UL_PipeID, 0);
  1178. }
  1179. LOCK_HTC_TX(target);
  1180. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1181. pEndpoint->SeqNo++;
  1182. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1183. SM(pPacket->PktInfo.AsTx.SeqNo,
  1184. HTC_FRAME_HDR_CONTROLBYTES1));
  1185. /* append new packet to pEndpoint->TxQueue */
  1186. HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
  1187. #ifdef ENABLE_BUNDLE_TX
  1188. if (HTC_ENABLE_BUNDLE(target) && (more_data)) {
  1189. UNLOCK_HTC_TX(target);
  1190. return A_OK;
  1191. }
  1192. #endif
  1193. } else {
  1194. LOCK_HTC_TX(target);
  1195. pEndpoint = &target->endpoint[1];
  1196. }
  1197. /* increment tx processing count on entry */
  1198. cdf_atomic_inc(&pEndpoint->TxProcessCount);
  1199. if (cdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  1200. /*
  1201. * Another thread or task is draining the TX queues on this endpoint.
  1202. * That thread will reset the tx processing count when the queue is
  1203. * drained.
  1204. */
  1205. cdf_atomic_dec(&pEndpoint->TxProcessCount);
  1206. UNLOCK_HTC_TX(target);
  1207. return A_OK;
  1208. }
  1209. /***** beyond this point only 1 thread may enter ******/
  1210. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1211. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1212. #if DEBUG_CREDIT
  1213. int cred = pEndpoint->TxCredits;
  1214. #endif
  1215. get_htc_send_packets_credit_based(target, pEndpoint, &sendQueue);
  1216. #if DEBUG_CREDIT
  1217. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1218. if (cred - pEndpoint->TxCredits > 0) {
  1219. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1220. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  1221. pEndpoint->Id, cred,
  1222. cred - pEndpoint->TxCredits,
  1223. pEndpoint->TxCredits));
  1224. }
  1225. }
  1226. #endif
  1227. UNLOCK_HTC_TX(target);
  1228. }
  1229. #ifdef ENABLE_BUNDLE_TX
  1230. else if (HTC_ENABLE_BUNDLE(target)) {
  1231. /* Dequeue max packets from endpoint tx queue */
  1232. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1233. HTC_MAX_TX_BUNDLE_SEND_LIMIT);
  1234. UNLOCK_HTC_TX(target);
  1235. }
  1236. #endif
  1237. else {
  1238. /*
  1239. * Now drain the endpoint TX queue for transmission as long as we have
  1240. * enough transmit resources
  1241. */
  1242. tx_resources =
  1243. hif_get_free_queue_number(target->hif_dev,
  1244. pEndpoint->UL_PipeID);
  1245. get_htc_send_packets(target, pEndpoint, &sendQueue, tx_resources);
  1246. UNLOCK_HTC_TX(target);
  1247. }
  1248. NBUF_UPDATE_TX_PKT_COUNT(netbuf, NBUF_TX_PKT_HTC);
  1249. DPTRACE(cdf_dp_trace(netbuf, CDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1250. (uint8_t *)(cdf_nbuf_data(netbuf)),
  1251. sizeof(cdf_nbuf_data(netbuf))));
  1252. /* send what we can */
  1253. while (true) {
  1254. #if defined(HIF_USB) || defined(HIF_SDIO)
  1255. #ifdef ENABLE_BUNDLE_TX
  1256. if (HTC_ENABLE_BUNDLE(target) &&
  1257. HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
  1258. HTC_MIN_MSG_PER_BUNDLE) {
  1259. htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
  1260. }
  1261. #endif
  1262. #endif
  1263. pPacket = htc_packet_dequeue(&sendQueue);
  1264. if (pPacket == NULL) {
  1265. break;
  1266. }
  1267. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1268. LOCK_HTC_TX(target);
  1269. /* store in look up queue to match completions */
  1270. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  1271. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  1272. pEndpoint->ul_outstanding_cnt++;
  1273. UNLOCK_HTC_TX(target);
  1274. status = hif_send_head(target->hif_dev,
  1275. pEndpoint->UL_PipeID,
  1276. pEndpoint->Id,
  1277. HTC_HDR_LENGTH + pPacket->ActualLength,
  1278. netbuf, data_attr);
  1279. #if DEBUG_BUNDLE
  1280. cdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
  1281. pEndpoint->Id,
  1282. pEndpoint->TxCreditSize,
  1283. HTC_HDR_LENGTH + pPacket->ActualLength);
  1284. #endif
  1285. if (cdf_unlikely(A_FAILED(status))) {
  1286. LOCK_HTC_TX(target);
  1287. pEndpoint->ul_outstanding_cnt--;
  1288. /* remove this packet from the tx completion queue */
  1289. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  1290. /*
  1291. * Don't bother reclaiming credits - HTC flow control
  1292. * is not applicable to tx data.
  1293. * In LL systems, there is no download flow control,
  1294. * since there's virtually no download delay.
  1295. * In HL systems, the txrx SW explicitly performs the
  1296. * tx flow control.
  1297. */
  1298. /* pEndpoint->TxCredits += pPacket->PktInfo.AsTx.CreditsUsed; */
  1299. /* put this frame back at the front of the sendQueue */
  1300. HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
  1301. /* put the sendQueue back at the front of pEndpoint->TxQueue */
  1302. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1303. &sendQueue);
  1304. UNLOCK_HTC_TX(target);
  1305. break; /* still need to reset TxProcessCount */
  1306. }
  1307. }
  1308. /* done with this endpoint, we can clear the count */
  1309. cdf_atomic_init(&pEndpoint->TxProcessCount);
  1310. if (pEndpoint->ul_is_polled) {
  1311. /*
  1312. * Start a cleanup timer to poll for download completion.
  1313. * The download completion should be noticed promptly from
  1314. * other polling calls, but the timer provides a safety net
  1315. * in case other polling calls don't occur as expected.
  1316. */
  1317. htc_send_complete_poll_timer_start(pEndpoint);
  1318. }
  1319. return status;
  1320. }
  1321. #endif /*ATH_11AC_TXCOMPACT */
  1322. /*
  1323. * In the adapted HIF layer, cdf_nbuf_t are passed between HIF and HTC, since upper layers expects
  1324. * HTC_PACKET containers we use the completed netbuf and lookup its corresponding HTC packet buffer
  1325. * from a lookup list.
  1326. * This is extra overhead that can be fixed by re-aligning HIF interfaces with HTC.
  1327. *
  1328. */
  1329. static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
  1330. HTC_ENDPOINT *pEndpoint,
  1331. cdf_nbuf_t netbuf)
  1332. {
  1333. HTC_PACKET *pPacket = NULL;
  1334. HTC_PACKET *pFoundPacket = NULL;
  1335. HTC_PACKET_QUEUE lookupQueue;
  1336. INIT_HTC_PACKET_QUEUE(&lookupQueue);
  1337. LOCK_HTC_TX(target);
  1338. /* mark that HIF has indicated the send complete for another packet */
  1339. pEndpoint->ul_outstanding_cnt--;
  1340. /* Dequeue first packet directly because of in-order completion */
  1341. pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
  1342. if (cdf_unlikely(!pPacket)) {
  1343. UNLOCK_HTC_TX(target);
  1344. return NULL;
  1345. }
  1346. if (netbuf == (cdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1347. UNLOCK_HTC_TX(target);
  1348. return pPacket;
  1349. } else {
  1350. HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
  1351. }
  1352. /*
  1353. * Move TX lookup queue to temp queue because most of packets that are not index 0
  1354. * are not top 10 packets.
  1355. */
  1356. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
  1357. &pEndpoint->TxLookupQueue);
  1358. UNLOCK_HTC_TX(target);
  1359. ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
  1360. HTC_PACKET, ListLink) {
  1361. if (NULL == pPacket) {
  1362. pFoundPacket = pPacket;
  1363. break;
  1364. }
  1365. /* check for removal */
  1366. if (netbuf ==
  1367. (cdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1368. /* found it */
  1369. HTC_PACKET_REMOVE(&lookupQueue, pPacket);
  1370. pFoundPacket = pPacket;
  1371. break;
  1372. }
  1373. }
  1374. ITERATE_END;
  1375. LOCK_HTC_TX(target);
  1376. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
  1377. &lookupQueue);
  1378. UNLOCK_HTC_TX(target);
  1379. return pFoundPacket;
  1380. }
  1381. CDF_STATUS htc_tx_completion_handler(void *Context,
  1382. cdf_nbuf_t netbuf, unsigned int EpID,
  1383. uint32_t toeplitz_hash_result)
  1384. {
  1385. HTC_TARGET *target = (HTC_TARGET *) Context;
  1386. HTC_ENDPOINT *pEndpoint;
  1387. HTC_PACKET *pPacket;
  1388. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1389. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] =
  1390. { ENDPOINT_5, ENDPOINT_4, ENDPOINT_2, ENDPOINT_3 };
  1391. int epidIdx;
  1392. A_UINT16 resourcesThresh[DATA_EP_SIZE]; /* urb resources */
  1393. A_UINT16 resources;
  1394. A_UINT16 resourcesMax;
  1395. #endif
  1396. pEndpoint = &target->endpoint[EpID];
  1397. target->TX_comp_cnt++;
  1398. do {
  1399. pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
  1400. if (NULL == pPacket) {
  1401. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1402. ("HTC TX lookup failed!\n"));
  1403. /* may have already been flushed and freed */
  1404. netbuf = NULL;
  1405. break;
  1406. }
  1407. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
  1408. HTC_PACKET *pPacketTemp;
  1409. HTC_PACKET_QUEUE *pQueueSave =
  1410. (HTC_PACKET_QUEUE *) pPacket->pContext;
  1411. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
  1412. pPacketTemp) {
  1413. pPacket->Status = A_OK;
  1414. send_packet_completion(target, pPacketTemp);
  1415. }
  1416. HTC_PACKET_QUEUE_ITERATE_END;
  1417. free_htc_bundle_packet(target, pPacket);
  1418. return CDF_STATUS_SUCCESS;
  1419. }
  1420. /* will be giving this buffer back to upper layers */
  1421. netbuf = NULL;
  1422. pPacket->Status = CDF_STATUS_SUCCESS;
  1423. send_packet_completion(target, pPacket);
  1424. } while (false);
  1425. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1426. /* note: when using TX credit flow, the re-checking of queues happens
  1427. * when credits flow back from the target.
  1428. * in the non-TX credit case, we recheck after the packet completes */
  1429. htc_try_send(target, pEndpoint, NULL);
  1430. }
  1431. return CDF_STATUS_SUCCESS;
  1432. }
  1433. /* callback when TX resources become available */
  1434. void htc_tx_resource_avail_handler(void *context, A_UINT8 pipeID)
  1435. {
  1436. int i;
  1437. HTC_TARGET *target = (HTC_TARGET *) context;
  1438. HTC_ENDPOINT *pEndpoint = NULL;
  1439. for (i = 0; i < ENDPOINT_MAX; i++) {
  1440. pEndpoint = &target->endpoint[i];
  1441. if (pEndpoint->service_id != 0) {
  1442. if (pEndpoint->UL_PipeID == pipeID) {
  1443. break;
  1444. }
  1445. }
  1446. }
  1447. if (i >= ENDPOINT_MAX) {
  1448. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1449. ("Invalid pipe indicated for TX resource avail : %d!\n",
  1450. pipeID));
  1451. return;
  1452. }
  1453. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1454. ("HIF indicated more resources for pipe:%d \n",
  1455. pipeID));
  1456. htc_try_send(target, pEndpoint, NULL);
  1457. }
  1458. /* flush endpoint TX queue */
  1459. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  1460. HTC_TX_TAG Tag)
  1461. {
  1462. HTC_PACKET *pPacket;
  1463. LOCK_HTC_TX(target);
  1464. while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1465. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  1466. if (pPacket) {
  1467. /* let the sender know the packet was not delivered */
  1468. pPacket->Status = A_ECANCELED;
  1469. send_packet_completion(target, pPacket);
  1470. }
  1471. }
  1472. UNLOCK_HTC_TX(target);
  1473. }
  1474. /* HTC API to flush an endpoint's TX queue*/
  1475. void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
  1476. HTC_TX_TAG Tag)
  1477. {
  1478. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1479. HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
  1480. if (pEndpoint->service_id == 0) {
  1481. AR_DEBUG_ASSERT(false);
  1482. /* not in use.. */
  1483. return;
  1484. }
  1485. htc_flush_endpoint_tx(target, pEndpoint, Tag);
  1486. }
  1487. /* HTC API to indicate activity to the credit distribution function */
  1488. void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
  1489. HTC_ENDPOINT_ID Endpoint, A_BOOL Active)
  1490. {
  1491. /* TODO */
  1492. }
  1493. A_BOOL htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
  1494. {
  1495. return true;
  1496. }
  1497. /* process credit reports and call distribution function */
  1498. void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
  1499. int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
  1500. {
  1501. int i;
  1502. HTC_ENDPOINT *pEndpoint;
  1503. int totalCredits = 0;
  1504. A_UINT8 rpt_credits, rpt_ep_id;
  1505. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1506. ("+htc_process_credit_rpt, Credit Report Entries:%d \n",
  1507. NumEntries));
  1508. /* lock out TX while we update credits */
  1509. LOCK_HTC_TX(target);
  1510. for (i = 0; i < NumEntries; i++, pRpt++) {
  1511. rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
  1512. if (rpt_ep_id >= ENDPOINT_MAX) {
  1513. AR_DEBUG_ASSERT(false);
  1514. break;
  1515. }
  1516. rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
  1517. pEndpoint = &target->endpoint[rpt_ep_id];
  1518. #if DEBUG_CREDIT
  1519. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1520. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1521. (" <HTC> Increase EP%d %d + %d = %d credits\n",
  1522. rpt_ep_id, pEndpoint->TxCredits,
  1523. rpt_credits,
  1524. pEndpoint->TxCredits + rpt_credits));
  1525. }
  1526. #endif
  1527. #ifdef HTC_EP_STAT_PROFILING
  1528. INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
  1529. INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
  1530. if (FromEndpoint == rpt_ep_id) {
  1531. /* this credit report arrived on the same endpoint indicating it arrived in an RX
  1532. * packet */
  1533. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
  1534. rpt_credits);
  1535. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
  1536. } else if (FromEndpoint == ENDPOINT_0) {
  1537. /* this credit arrived on endpoint 0 as a NULL message */
  1538. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
  1539. rpt_credits);
  1540. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
  1541. } else {
  1542. /* arrived on another endpoint */
  1543. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
  1544. rpt_credits);
  1545. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
  1546. }
  1547. #endif
  1548. #if defined(HIF_USB)
  1549. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5) {
  1550. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] =
  1551. { ENDPOINT_5, ENDPOINT_4, ENDPOINT_2, ENDPOINT_3 };
  1552. int epid_idx;
  1553. target->avail_tx_credits += rpt_credits;
  1554. for (epid_idx = 0; epid_idx < DATA_EP_SIZE; epid_idx++) {
  1555. pEndpoint = &target->endpoint[eid[epid_idx]];
  1556. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1557. break;
  1558. }
  1559. }
  1560. UNLOCK_HTC_TX(target);
  1561. htc_try_send(target, pEndpoint, NULL);
  1562. LOCK_HTC_TX(target);
  1563. } else {
  1564. pEndpoint->TxCredits += rpt_credits;
  1565. if (pEndpoint->TxCredits
  1566. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1567. UNLOCK_HTC_TX(target);
  1568. htc_try_send(target, pEndpoint, NULL);
  1569. LOCK_HTC_TX(target);
  1570. }
  1571. }
  1572. #else
  1573. pEndpoint->TxCredits += rpt_credits;
  1574. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  1575. LOCK_HTC_CREDIT(target);
  1576. htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
  1577. pEndpoint->TxCredits,
  1578. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  1579. TxQueue));
  1580. UNLOCK_HTC_CREDIT(target);
  1581. }
  1582. if (pEndpoint->TxCredits
  1583. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1584. UNLOCK_HTC_TX(target);
  1585. #ifdef ATH_11AC_TXCOMPACT
  1586. htc_try_send(target, pEndpoint, NULL);
  1587. #else
  1588. if (pEndpoint->service_id == HTT_DATA_MSG_SVC) {
  1589. htc_send_data_pkt(target, NULL, 0);
  1590. } else {
  1591. htc_try_send(target, pEndpoint, NULL);
  1592. }
  1593. #endif
  1594. LOCK_HTC_TX(target);
  1595. }
  1596. #endif
  1597. totalCredits += rpt_credits;
  1598. }
  1599. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1600. (" Report indicated %d credits to distribute \n",
  1601. totalCredits));
  1602. UNLOCK_HTC_TX(target);
  1603. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt \n"));
  1604. }
  1605. /* function to fetch stats from htc layer*/
  1606. struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
  1607. {
  1608. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1609. return (&(target->htc_pkt_stats));
  1610. }