htc_send.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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. /**
  1459. * htc_kick_queues(): resumes tx transactions of suspended endpoints
  1460. * @context: pointer to the htc target context
  1461. *
  1462. * Iterates throught the enpoints and provides a context to empty queues
  1463. * int the hif layer when they are stalled due to runtime suspend.
  1464. *
  1465. * Return: none
  1466. */
  1467. void htc_kick_queues(void *context)
  1468. {
  1469. int i;
  1470. HTC_TARGET *target = (HTC_TARGET *)context;
  1471. HTC_ENDPOINT *endpoint = NULL;
  1472. for (i = 0; i < ENDPOINT_MAX; i++) {
  1473. endpoint = &target->endpoint[i];
  1474. if (endpoint->service_id == 0)
  1475. continue;
  1476. if (endpoint->EpCallBacks.ep_resume_tx_queue)
  1477. endpoint->EpCallBacks.ep_resume_tx_queue(
  1478. endpoint->EpCallBacks.pContext);
  1479. htc_try_send(target, endpoint, NULL);
  1480. }
  1481. }
  1482. /* flush endpoint TX queue */
  1483. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  1484. HTC_TX_TAG Tag)
  1485. {
  1486. HTC_PACKET *pPacket;
  1487. LOCK_HTC_TX(target);
  1488. while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1489. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  1490. if (pPacket) {
  1491. /* let the sender know the packet was not delivered */
  1492. pPacket->Status = A_ECANCELED;
  1493. send_packet_completion(target, pPacket);
  1494. }
  1495. }
  1496. UNLOCK_HTC_TX(target);
  1497. }
  1498. /* HTC API to flush an endpoint's TX queue*/
  1499. void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
  1500. HTC_TX_TAG Tag)
  1501. {
  1502. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1503. HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
  1504. if (pEndpoint->service_id == 0) {
  1505. AR_DEBUG_ASSERT(false);
  1506. /* not in use.. */
  1507. return;
  1508. }
  1509. htc_flush_endpoint_tx(target, pEndpoint, Tag);
  1510. }
  1511. /* HTC API to indicate activity to the credit distribution function */
  1512. void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
  1513. HTC_ENDPOINT_ID Endpoint, A_BOOL Active)
  1514. {
  1515. /* TODO */
  1516. }
  1517. A_BOOL htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
  1518. {
  1519. return true;
  1520. }
  1521. /* process credit reports and call distribution function */
  1522. void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
  1523. int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
  1524. {
  1525. int i;
  1526. HTC_ENDPOINT *pEndpoint;
  1527. int totalCredits = 0;
  1528. A_UINT8 rpt_credits, rpt_ep_id;
  1529. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1530. ("+htc_process_credit_rpt, Credit Report Entries:%d \n",
  1531. NumEntries));
  1532. /* lock out TX while we update credits */
  1533. LOCK_HTC_TX(target);
  1534. for (i = 0; i < NumEntries; i++, pRpt++) {
  1535. rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
  1536. if (rpt_ep_id >= ENDPOINT_MAX) {
  1537. AR_DEBUG_ASSERT(false);
  1538. break;
  1539. }
  1540. rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
  1541. pEndpoint = &target->endpoint[rpt_ep_id];
  1542. #if DEBUG_CREDIT
  1543. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1544. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1545. (" <HTC> Increase EP%d %d + %d = %d credits\n",
  1546. rpt_ep_id, pEndpoint->TxCredits,
  1547. rpt_credits,
  1548. pEndpoint->TxCredits + rpt_credits));
  1549. }
  1550. #endif
  1551. #ifdef HTC_EP_STAT_PROFILING
  1552. INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
  1553. INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
  1554. if (FromEndpoint == rpt_ep_id) {
  1555. /* this credit report arrived on the same endpoint indicating it arrived in an RX
  1556. * packet */
  1557. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
  1558. rpt_credits);
  1559. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
  1560. } else if (FromEndpoint == ENDPOINT_0) {
  1561. /* this credit arrived on endpoint 0 as a NULL message */
  1562. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
  1563. rpt_credits);
  1564. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
  1565. } else {
  1566. /* arrived on another endpoint */
  1567. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
  1568. rpt_credits);
  1569. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
  1570. }
  1571. #endif
  1572. #if defined(HIF_USB)
  1573. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5) {
  1574. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] =
  1575. { ENDPOINT_5, ENDPOINT_4, ENDPOINT_2, ENDPOINT_3 };
  1576. int epid_idx;
  1577. target->avail_tx_credits += rpt_credits;
  1578. for (epid_idx = 0; epid_idx < DATA_EP_SIZE; epid_idx++) {
  1579. pEndpoint = &target->endpoint[eid[epid_idx]];
  1580. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1581. break;
  1582. }
  1583. }
  1584. UNLOCK_HTC_TX(target);
  1585. htc_try_send(target, pEndpoint, NULL);
  1586. LOCK_HTC_TX(target);
  1587. } else {
  1588. pEndpoint->TxCredits += rpt_credits;
  1589. if (pEndpoint->TxCredits
  1590. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1591. UNLOCK_HTC_TX(target);
  1592. htc_try_send(target, pEndpoint, NULL);
  1593. LOCK_HTC_TX(target);
  1594. }
  1595. }
  1596. #else
  1597. pEndpoint->TxCredits += rpt_credits;
  1598. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  1599. LOCK_HTC_CREDIT(target);
  1600. htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
  1601. pEndpoint->TxCredits,
  1602. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  1603. TxQueue));
  1604. UNLOCK_HTC_CREDIT(target);
  1605. }
  1606. if (pEndpoint->TxCredits
  1607. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1608. UNLOCK_HTC_TX(target);
  1609. #ifdef ATH_11AC_TXCOMPACT
  1610. htc_try_send(target, pEndpoint, NULL);
  1611. #else
  1612. if (pEndpoint->service_id == HTT_DATA_MSG_SVC) {
  1613. htc_send_data_pkt(target, NULL, 0);
  1614. } else {
  1615. htc_try_send(target, pEndpoint, NULL);
  1616. }
  1617. #endif
  1618. LOCK_HTC_TX(target);
  1619. }
  1620. #endif
  1621. totalCredits += rpt_credits;
  1622. }
  1623. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1624. (" Report indicated %d credits to distribute \n",
  1625. totalCredits));
  1626. UNLOCK_HTC_TX(target);
  1627. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt \n"));
  1628. }
  1629. /* function to fetch stats from htc layer*/
  1630. struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
  1631. {
  1632. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1633. return (&(target->htc_pkt_stats));
  1634. }