htc_send.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  1. /*
  2. * Copyright (c) 2013-2015 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->ServiceID == 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. status = hif_send_head(target->hif_dev,
  485. pEndpoint->UL_PipeID, pEndpoint->Id,
  486. HTC_HDR_LENGTH + pPacket->ActualLength,
  487. netbuf, data_attr);
  488. #if DEBUG_BUNDLE
  489. cdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
  490. pEndpoint->Id,
  491. pEndpoint->TxCreditSize,
  492. HTC_HDR_LENGTH + pPacket->ActualLength);
  493. #endif
  494. target->ce_send_cnt++;
  495. if (cdf_unlikely(A_FAILED(status))) {
  496. if (status != A_NO_RESOURCE) {
  497. /* TODO : if more than 1 endpoint maps to the same PipeID it is possible
  498. * to run out of resources in the HIF layer. Don't emit the error */
  499. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  500. ("hif_send Failed status:%d \n",
  501. status));
  502. }
  503. LOCK_HTC_TX(target);
  504. target->ce_send_cnt--;
  505. pEndpoint->ul_outstanding_cnt--;
  506. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  507. /* reclaim credits */
  508. #if defined(HIF_USB)
  509. if (pEndpoint->Id >= ENDPOINT_2
  510. && pEndpoint->Id <= ENDPOINT_5)
  511. target->avail_tx_credits +=
  512. pPacket->PktInfo.AsTx.CreditsUsed;
  513. else
  514. pEndpoint->TxCredits +=
  515. pPacket->PktInfo.AsTx.CreditsUsed;
  516. #else
  517. pEndpoint->TxCredits +=
  518. pPacket->PktInfo.AsTx.CreditsUsed;
  519. #endif
  520. /* put it back into the callers queue */
  521. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  522. UNLOCK_HTC_TX(target);
  523. break;
  524. }
  525. }
  526. if (cdf_unlikely(A_FAILED(status))) {
  527. #if defined(HIF_USB)
  528. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5)
  529. target->avail_tx_credits +=
  530. pPacket->PktInfo.AsTx.CreditsUsed;
  531. else
  532. pEndpoint->TxCredits +=
  533. pPacket->PktInfo.AsTx.CreditsUsed;
  534. #endif
  535. while (!HTC_QUEUE_EMPTY(pPktQueue)) {
  536. if (status != A_NO_RESOURCE) {
  537. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  538. ("htc_issue_packets, failed pkt:0x%p status:%d \n",
  539. pPacket, status));
  540. }
  541. pPacket = htc_packet_dequeue(pPktQueue);
  542. if (pPacket) {
  543. pPacket->Status = status;
  544. send_packet_completion(target, pPacket);
  545. }
  546. }
  547. }
  548. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets \n"));
  549. return status;
  550. }
  551. /* get HTC send packets from the TX queue on an endpoint, based on available credits */
  552. void get_htc_send_packets_credit_based(HTC_TARGET *target,
  553. HTC_ENDPOINT *pEndpoint,
  554. HTC_PACKET_QUEUE *pQueue)
  555. {
  556. int creditsRequired;
  557. int remainder;
  558. A_UINT8 sendFlags;
  559. HTC_PACKET *pPacket;
  560. unsigned int transferLength;
  561. /****** NOTE : the TX lock is held when this function is called *****************/
  562. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+get_htc_send_packets_credit_based \n"));
  563. /* loop until we can grab as many packets out of the queue as we can */
  564. while (true) {
  565. sendFlags = 0;
  566. /* get packet at head, but don't remove it */
  567. pPacket = htc_get_pkt_at_head(&pEndpoint->TxQueue);
  568. if (pPacket == NULL) {
  569. break;
  570. }
  571. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  572. (" Got head packet:%p , Queue Depth: %d\n",
  573. pPacket,
  574. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)));
  575. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  576. if (transferLength <= pEndpoint->TxCreditSize) {
  577. creditsRequired = 1;
  578. } else {
  579. /* figure out how many credits this message requires */
  580. creditsRequired =
  581. transferLength / pEndpoint->TxCreditSize;
  582. remainder = transferLength % pEndpoint->TxCreditSize;
  583. if (remainder) {
  584. creditsRequired++;
  585. }
  586. }
  587. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  588. (" Credits Required:%d Got:%d\n",
  589. creditsRequired, pEndpoint->TxCredits));
  590. if (pEndpoint->Id == ENDPOINT_0) {
  591. /* endpoint 0 is special, it always has a credit and does not require credit based
  592. * flow control */
  593. creditsRequired = 0;
  594. #if defined(HIF_USB)
  595. } else if (pEndpoint->Id >= ENDPOINT_2
  596. && pEndpoint->Id <= ENDPOINT_5) {
  597. if (target->avail_tx_credits < creditsRequired)
  598. break;
  599. target->avail_tx_credits -= creditsRequired;
  600. if (target->avail_tx_credits < 9) {
  601. /* tell the target we need credits ASAP! */
  602. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  603. INC_HTC_EP_STAT(pEndpoint,
  604. TxCreditLowIndications, 1);
  605. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  606. (" Host Needs Credits \n"));
  607. }
  608. #endif
  609. } else {
  610. if (pEndpoint->TxCredits < creditsRequired) {
  611. #if DEBUG_CREDIT
  612. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  613. (" EP%d, No Credit now. %d < %d\n",
  614. pEndpoint->Id,
  615. pEndpoint->TxCredits,
  616. creditsRequired));
  617. #endif
  618. break;
  619. }
  620. pEndpoint->TxCredits -= creditsRequired;
  621. INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
  622. creditsRequired);
  623. /* check if we need credits back from the target */
  624. if (pEndpoint->TxCredits <=
  625. pEndpoint->TxCreditsPerMaxMsg) {
  626. /* tell the target we need credits ASAP! */
  627. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  628. if (pEndpoint->ServiceID == WMI_CONTROL_SVC) {
  629. LOCK_HTC_CREDIT(target);
  630. htc_credit_record(HTC_REQUEST_CREDIT,
  631. pEndpoint->TxCredits,
  632. HTC_PACKET_QUEUE_DEPTH
  633. (&pEndpoint->
  634. TxQueue));
  635. UNLOCK_HTC_CREDIT(target);
  636. }
  637. INC_HTC_EP_STAT(pEndpoint,
  638. TxCreditLowIndications, 1);
  639. #if DEBUG_CREDIT
  640. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  641. (" EP%d Needs Credits\n",
  642. pEndpoint->Id));
  643. #endif
  644. }
  645. }
  646. /* now we can fully dequeue */
  647. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  648. if (pPacket) {
  649. /* save the number of credits this packet consumed */
  650. pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
  651. /* save send flags */
  652. pPacket->PktInfo.AsTx.SendFlags = sendFlags;
  653. /* queue this packet into the caller's queue */
  654. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  655. }
  656. }
  657. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets_credit_based \n"));
  658. }
  659. void get_htc_send_packets(HTC_TARGET *target,
  660. HTC_ENDPOINT *pEndpoint,
  661. HTC_PACKET_QUEUE *pQueue, int Resources)
  662. {
  663. HTC_PACKET *pPacket;
  664. /****** NOTE : the TX lock is held when this function is called *****************/
  665. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  666. ("+get_htc_send_packets %d resources\n", Resources));
  667. /* loop until we can grab as many packets out of the queue as we can */
  668. while (Resources > 0) {
  669. int num_frags;
  670. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  671. if (pPacket == NULL) {
  672. break;
  673. }
  674. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  675. (" Got packet:%p , New Queue Depth: %d\n",
  676. pPacket,
  677. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)));
  678. /* For non-credit path the sequence number is already embedded
  679. * in the constructed HTC header
  680. */
  681. #if 0
  682. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  683. pEndpoint->SeqNo++;
  684. #endif
  685. pPacket->PktInfo.AsTx.SendFlags = 0;
  686. pPacket->PktInfo.AsTx.CreditsUsed = 0;
  687. /* queue this packet into the caller's queue */
  688. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  689. /*
  690. * FIX THIS:
  691. * For now, avoid calling cdf_nbuf_get_num_frags before calling
  692. * cdf_nbuf_map, because the MacOS version of cdf_nbuf_t doesn't
  693. * support cdf_nbuf_get_num_frags until after cdf_nbuf_map has
  694. * been done.
  695. * Assume that the non-data netbufs, i.e. the WMI message netbufs,
  696. * consist of a single fragment.
  697. */
  698. num_frags =
  699. (pPacket->PktInfo.AsTx.
  700. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1
  701. /* WMI messages are in a single-fragment network buffer */ :
  702. cdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
  703. (pPacket));
  704. Resources -= num_frags;
  705. }
  706. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets \n"));
  707. }
  708. static HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
  709. HTC_ENDPOINT *pEndpoint,
  710. HTC_PACKET_QUEUE *pCallersSendQueue)
  711. {
  712. HTC_PACKET_QUEUE sendQueue; /* temp queue to hold packets at various stages */
  713. HTC_PACKET *pPacket;
  714. int tx_resources;
  715. int overflow;
  716. HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
  717. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%p Depth:%d)\n",
  718. pCallersSendQueue,
  719. (pCallersSendQueue ==
  720. NULL) ? 0 :
  721. HTC_PACKET_QUEUE_DEPTH
  722. (pCallersSendQueue)));
  723. /* init the local send queue */
  724. INIT_HTC_PACKET_QUEUE(&sendQueue);
  725. do {
  726. if (NULL == pCallersSendQueue) {
  727. /* caller didn't provide a queue, just wants us to check queues and send */
  728. break;
  729. }
  730. if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
  731. /* empty queue */
  732. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  733. HTC_PKT_Q_EMPTY);
  734. result = HTC_SEND_QUEUE_DROP;
  735. break;
  736. }
  737. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
  738. pEndpoint->MaxTxQueueDepth) {
  739. /* we've already overflowed */
  740. overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  741. } else {
  742. /* figure out how much we will overflow by */
  743. overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  744. overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  745. /* figure out how much we will overflow the TX queue by */
  746. overflow -= pEndpoint->MaxTxQueueDepth;
  747. }
  748. /* if overflow is negative or zero, we are okay */
  749. if (overflow > 0) {
  750. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  751. (" Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d \n",
  752. pEndpoint->Id, overflow,
  753. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  754. TxQueue),
  755. pEndpoint->MaxTxQueueDepth));
  756. }
  757. if ((overflow <= 0)
  758. || (pEndpoint->EpCallBacks.EpSendFull == NULL)) {
  759. /* all packets will fit or caller did not provide send full indication handler
  760. * -- just move all of them to the local sendQueue object */
  761. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
  762. pCallersSendQueue);
  763. } else {
  764. int i;
  765. int goodPkts =
  766. HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
  767. overflow;
  768. A_ASSERT(goodPkts >= 0);
  769. /* we have overflowed, and a callback is provided */
  770. /* dequeue all non-overflow packets into the sendqueue */
  771. for (i = 0; i < goodPkts; i++) {
  772. /* pop off caller's queue */
  773. pPacket = htc_packet_dequeue(pCallersSendQueue);
  774. A_ASSERT(pPacket != NULL);
  775. /* insert into local queue */
  776. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  777. }
  778. /* the caller's queue has all the packets that won't fit */
  779. /* walk through the caller's queue and indicate each one to the send full handler */
  780. ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
  781. QueueHead, pPacket,
  782. HTC_PACKET, ListLink) {
  783. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  784. (" Indicating overflowed TX packet: %p \n",
  785. pPacket));
  786. /*
  787. * Remove headroom reserved for HTC_FRAME_HDR before giving
  788. * the packet back to the user via the EpSendFull callback.
  789. */
  790. restore_tx_packet(target, pPacket);
  791. if (pEndpoint->EpCallBacks.
  792. EpSendFull(pEndpoint->EpCallBacks.pContext,
  793. pPacket) == HTC_SEND_FULL_DROP) {
  794. /* callback wants the packet dropped */
  795. INC_HTC_EP_STAT(pEndpoint, TxDropped,
  796. 1);
  797. /* leave this one in the caller's queue for cleanup */
  798. } else {
  799. /* callback wants to keep this packet, remove from caller's queue */
  800. HTC_PACKET_REMOVE(pCallersSendQueue,
  801. pPacket);
  802. /* put it in the send queue */
  803. /* add HTC_FRAME_HDR space reservation again */
  804. cdf_nbuf_push_head
  805. (GET_HTC_PACKET_NET_BUF_CONTEXT
  806. (pPacket), sizeof(HTC_FRAME_HDR));
  807. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  808. }
  809. }
  810. ITERATE_END;
  811. if (HTC_QUEUE_EMPTY(&sendQueue)) {
  812. /* no packets made it in, caller will cleanup */
  813. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  814. HTC_SEND_Q_EMPTY);
  815. result = HTC_SEND_QUEUE_DROP;
  816. break;
  817. }
  818. }
  819. } while (false);
  820. if (result != HTC_SEND_QUEUE_OK) {
  821. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: \n"));
  822. return result;
  823. }
  824. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  825. tx_resources =
  826. hif_get_free_queue_number(target->hif_dev,
  827. pEndpoint->UL_PipeID);
  828. } else {
  829. tx_resources = 0;
  830. }
  831. LOCK_HTC_TX(target);
  832. if (!HTC_QUEUE_EMPTY(&sendQueue)) {
  833. /* transfer packets to tail */
  834. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
  835. &sendQueue);
  836. A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
  837. INIT_HTC_PACKET_QUEUE(&sendQueue);
  838. }
  839. /* increment tx processing count on entry */
  840. cdf_atomic_inc(&pEndpoint->TxProcessCount);
  841. if (cdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  842. /* another thread or task is draining the TX queues on this endpoint
  843. * that thread will reset the tx processing count when the queue is drained */
  844. cdf_atomic_dec(&pEndpoint->TxProcessCount);
  845. UNLOCK_HTC_TX(target);
  846. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send (busy) \n"));
  847. return HTC_SEND_QUEUE_OK;
  848. }
  849. /***** beyond this point only 1 thread may enter ******/
  850. /* now drain the endpoint TX queue for transmission as long as we have enough
  851. * transmit resources */
  852. while (true) {
  853. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) == 0) {
  854. break;
  855. }
  856. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  857. #if DEBUG_CREDIT
  858. int cred = pEndpoint->TxCredits;
  859. #endif
  860. /* credit based mechanism provides flow control based on target transmit resource availability, we
  861. * assume that the HIF layer will always have bus resources greater than target transmit resources */
  862. get_htc_send_packets_credit_based(target, pEndpoint,
  863. &sendQueue);
  864. #if DEBUG_CREDIT
  865. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  866. if (cred - pEndpoint->TxCredits > 0) {
  867. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  868. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  869. pEndpoint->Id, cred,
  870. cred -
  871. pEndpoint->TxCredits,
  872. pEndpoint->TxCredits));
  873. }
  874. }
  875. #endif
  876. } else {
  877. /* get all the packets for this endpoint that we can for this pass */
  878. get_htc_send_packets(target, pEndpoint, &sendQueue,
  879. tx_resources);
  880. }
  881. if (HTC_PACKET_QUEUE_DEPTH(&sendQueue) == 0) {
  882. /* didn't get any packets due to a lack of resources or TX queue was drained */
  883. break;
  884. }
  885. UNLOCK_HTC_TX(target);
  886. /* send what we can */
  887. htc_issue_packets(target, pEndpoint, &sendQueue);
  888. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  889. tx_resources =
  890. hif_get_free_queue_number(target->hif_dev,
  891. pEndpoint->UL_PipeID);
  892. }
  893. LOCK_HTC_TX(target);
  894. }
  895. UNLOCK_HTC_TX(target);
  896. /* done with this endpoint, we can clear the count */
  897. cdf_atomic_init(&pEndpoint->TxProcessCount);
  898. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: \n"));
  899. return HTC_SEND_QUEUE_OK;
  900. }
  901. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  902. static A_UINT16 htc_send_pkts_sched_check(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID id)
  903. {
  904. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  905. HTC_ENDPOINT *pEndpoint;
  906. HTC_ENDPOINT_ID eid;
  907. HTC_PACKET_QUEUE *pTxQueue;
  908. A_UINT16 resources;
  909. A_UINT16 acQueueStatus[DATA_EP_SIZE] = { 0, 0, 0, 0 };
  910. if (id < ENDPOINT_2 || id > ENDPOINT_5) {
  911. return 1;
  912. }
  913. for (eid = ENDPOINT_2; eid <= ENDPOINT_5; eid++) {
  914. pEndpoint = &target->EndPoint[eid];
  915. pTxQueue = &pEndpoint->TxQueue;
  916. if (HTC_QUEUE_EMPTY(pTxQueue)) {
  917. acQueueStatus[eid - 2] = 1;
  918. }
  919. }
  920. switch (id) {
  921. case ENDPOINT_2: /* BE */
  922. return (acQueueStatus[0] && acQueueStatus[2]
  923. && acQueueStatus[3]);
  924. case ENDPOINT_3: /* BK */
  925. return (acQueueStatus[0] && acQueueStatus[1] && acQueueStatus[2]
  926. && acQueueStatus[3]);
  927. case ENDPOINT_4: /* VI */
  928. return (acQueueStatus[2] && acQueueStatus[3]);
  929. case ENDPOINT_5: /* VO */
  930. return (acQueueStatus[3]);
  931. default:
  932. return 0;
  933. }
  934. }
  935. static A_STATUS htc_send_pkts_sched_queue(HTC_TARGET *target,
  936. HTC_PACKET_QUEUE *pPktQueue,
  937. HTC_ENDPOINT_ID eid)
  938. {
  939. HTC_ENDPOINT *pEndpoint;
  940. HTC_PACKET_QUEUE *pTxQueue;
  941. HTC_PACKET *pPacket;
  942. int goodPkts;
  943. pEndpoint = &target->EndPoint[eid];
  944. pTxQueue = &pEndpoint->TxQueue;
  945. LOCK_HTC_TX(target);
  946. goodPkts =
  947. pEndpoint->MaxTxQueueDepth -
  948. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  949. if (goodPkts > 0) {
  950. while (!HTC_QUEUE_EMPTY(pPktQueue)) {
  951. pPacket = htc_packet_dequeue(pPktQueue);
  952. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  953. goodPkts--;
  954. if (goodPkts <= 0) {
  955. break;
  956. }
  957. }
  958. }
  959. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue)) {
  960. ITERATE_OVER_LIST_ALLOW_REMOVE(&pPktQueue->QueueHead, pPacket,
  961. HTC_PACKET, ListLink) {
  962. if (pEndpoint->EpCallBacks.
  963. EpSendFull(pEndpoint->EpCallBacks.pContext,
  964. pPacket) == HTC_SEND_FULL_DROP) {
  965. INC_HTC_EP_STAT(pEndpoint, TxDropped, 1);
  966. } else {
  967. HTC_PACKET_REMOVE(pPktQueue, pPacket);
  968. HTC_PACKET_ENQUEUE(pTxQueue, pPacket);
  969. }
  970. }
  971. ITERATE_END;
  972. }
  973. UNLOCK_HTC_TX(target);
  974. return A_OK;
  975. }
  976. #endif
  977. A_STATUS htc_send_pkts_multiple(HTC_HANDLE HTCHandle, HTC_PACKET_QUEUE *pPktQueue)
  978. {
  979. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  980. HTC_ENDPOINT *pEndpoint;
  981. HTC_PACKET *pPacket;
  982. cdf_nbuf_t netbuf;
  983. HTC_FRAME_HDR *pHtcHdr;
  984. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  985. ("+htc_send_pkts_multiple: Queue: %p, Pkts %d \n",
  986. pPktQueue, HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
  987. /* get packet at head to figure out which endpoint these packets will go into */
  988. pPacket = htc_get_pkt_at_head(pPktQueue);
  989. if (NULL == pPacket) {
  990. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target, GET_HTC_PKT_Q_FAIL);
  991. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_send_pkts_multiple \n"));
  992. return A_EINVAL;
  993. }
  994. AR_DEBUG_ASSERT(pPacket->Endpoint < ENDPOINT_MAX);
  995. pEndpoint = &target->EndPoint[pPacket->Endpoint];
  996. if (!pEndpoint->ServiceID) {
  997. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("%s ServiceID is invalid\n",
  998. __func__));
  999. return A_EINVAL;
  1000. }
  1001. #ifdef HTC_EP_STAT_PROFILING
  1002. LOCK_HTC_TX(target);
  1003. INC_HTC_EP_STAT(pEndpoint, TxPosted, HTC_PACKET_QUEUE_DEPTH(pPktQueue));
  1004. UNLOCK_HTC_TX(target);
  1005. #endif
  1006. /* provide room in each packet's netbuf for the HTC frame header */
  1007. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue, pPacket) {
  1008. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1009. AR_DEBUG_ASSERT(netbuf);
  1010. cdf_nbuf_push_head(netbuf, sizeof(HTC_FRAME_HDR));
  1011. /* setup HTC frame header */
  1012. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  1013. AR_DEBUG_ASSERT(pHtcHdr);
  1014. HTC_WRITE32(pHtcHdr,
  1015. SM(pPacket->ActualLength,
  1016. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->Endpoint,
  1017. HTC_FRAME_HDR_ENDPOINTID));
  1018. LOCK_HTC_TX(target);
  1019. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1020. pEndpoint->SeqNo++;
  1021. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1022. SM(pPacket->PktInfo.AsTx.SeqNo,
  1023. HTC_FRAME_HDR_CONTROLBYTES1));
  1024. UNLOCK_HTC_TX(target);
  1025. /*
  1026. * Now that the HTC frame header has been added, the netbuf can be
  1027. * mapped. This only applies to non-data frames, since data frames
  1028. * were already mapped as they entered into the driver.
  1029. */
  1030. cdf_nbuf_map(target->osdev,
  1031. GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket),
  1032. CDF_DMA_TO_DEVICE);
  1033. pPacket->PktInfo.AsTx.Flags |= HTC_TX_PACKET_FLAG_FIXUP_NETBUF;
  1034. }
  1035. HTC_PACKET_QUEUE_ITERATE_END;
  1036. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1037. if (!htc_send_pkts_sched_check(HTCHandle, pEndpoint->Id)) {
  1038. htc_send_pkts_sched_queue(HTCHandle, pPktQueue, pEndpoint->Id);
  1039. } else {
  1040. htc_try_send(target, pEndpoint, pPktQueue);
  1041. }
  1042. #else
  1043. htc_try_send(target, pEndpoint, pPktQueue);
  1044. #endif
  1045. /* do completion on any packets that couldn't get in */
  1046. if (!HTC_QUEUE_EMPTY(pPktQueue)) {
  1047. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pPktQueue, pPacket) {
  1048. /* remove the headroom reserved for HTC_FRAME_HDR */
  1049. restore_tx_packet(target, pPacket);
  1050. if (HTC_STOPPING(target)) {
  1051. pPacket->Status = A_ECANCELED;
  1052. } else {
  1053. pPacket->Status = A_NO_RESOURCE;
  1054. }
  1055. }
  1056. HTC_PACKET_QUEUE_ITERATE_END;
  1057. do_send_completion(pEndpoint, pPktQueue);
  1058. }
  1059. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_send_pkts_multiple \n"));
  1060. return A_OK;
  1061. }
  1062. /* HTC API - htc_send_pkt */
  1063. A_STATUS htc_send_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket)
  1064. {
  1065. HTC_PACKET_QUEUE queue;
  1066. if (HTCHandle == NULL || pPacket == NULL) {
  1067. return A_ERROR;
  1068. }
  1069. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1070. ("+-htc_send_pkt: Enter endPointId: %d, buffer: %p, length: %d \n",
  1071. pPacket->Endpoint, pPacket->pBuffer,
  1072. pPacket->ActualLength));
  1073. INIT_HTC_PACKET_QUEUE_AND_ADD(&queue, pPacket);
  1074. return htc_send_pkts_multiple(HTCHandle, &queue);
  1075. }
  1076. #ifdef ATH_11AC_TXCOMPACT
  1077. A_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, cdf_nbuf_t netbuf, int Epid,
  1078. int ActualLength)
  1079. {
  1080. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1081. HTC_ENDPOINT *pEndpoint;
  1082. HTC_FRAME_HDR *pHtcHdr;
  1083. A_STATUS status = A_OK;
  1084. int tx_resources;
  1085. uint32_t data_attr = 0;
  1086. pEndpoint = &target->EndPoint[Epid];
  1087. tx_resources =
  1088. hif_get_free_queue_number(target->hif_dev, pEndpoint->UL_PipeID);
  1089. if (tx_resources < HTC_DATA_RESOURCE_THRS) {
  1090. if (pEndpoint->ul_is_polled) {
  1091. hif_send_complete_check(pEndpoint->target->hif_dev,
  1092. pEndpoint->UL_PipeID, 1);
  1093. tx_resources =
  1094. hif_get_free_queue_number(target->hif_dev,
  1095. pEndpoint->UL_PipeID);
  1096. }
  1097. if (tx_resources < HTC_DATA_MINDESC_PERPACKET) {
  1098. return A_ERROR;
  1099. }
  1100. }
  1101. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  1102. AR_DEBUG_ASSERT(pHtcHdr);
  1103. data_attr = cdf_nbuf_data_attr_get(netbuf);
  1104. HTC_WRITE32(pHtcHdr, SM(ActualLength, HTC_FRAME_HDR_PAYLOADLEN) |
  1105. SM(Epid, HTC_FRAME_HDR_ENDPOINTID));
  1106. /*
  1107. * If the HIF pipe for the data endpoint is polled rather than
  1108. * interrupt-driven, this is a good point to check whether any
  1109. * data previously sent through the HIF pipe have finished being
  1110. * sent.
  1111. * Since this may result in callbacks to htc_tx_completion_handler,
  1112. * which can take the HTC tx lock, make the hif_send_complete_check
  1113. * call before acquiring the HTC tx lock.
  1114. * Call hif_send_complete_check directly, rather than calling
  1115. * htc_send_complete_check, and call the PollTimerStart separately
  1116. * after calling hif_send_head, so the timer will be started to
  1117. * check for completion of the new outstanding download (in the
  1118. * unexpected event that other polling calls don't catch it).
  1119. */
  1120. LOCK_HTC_TX(target);
  1121. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1122. SM(pEndpoint->SeqNo, HTC_FRAME_HDR_CONTROLBYTES1));
  1123. pEndpoint->SeqNo++;
  1124. NBUF_UPDATE_TX_PKT_COUNT(netbuf, NBUF_TX_PKT_HTC);
  1125. DPTRACE(cdf_dp_trace(netbuf, CDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1126. (uint8_t *)(cdf_nbuf_data(netbuf)),
  1127. sizeof(cdf_nbuf_data(netbuf))));
  1128. status = hif_send_head(target->hif_dev,
  1129. pEndpoint->UL_PipeID,
  1130. pEndpoint->Id, ActualLength, netbuf, data_attr);
  1131. UNLOCK_HTC_TX(target);
  1132. return status;
  1133. }
  1134. #else /*ATH_11AC_TXCOMPACT */
  1135. A_STATUS htc_send_data_pkt(HTC_HANDLE HTCHandle, HTC_PACKET *pPacket,
  1136. A_UINT8 more_data)
  1137. {
  1138. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1139. HTC_ENDPOINT *pEndpoint;
  1140. HTC_FRAME_HDR *pHtcHdr;
  1141. HTC_PACKET_QUEUE sendQueue;
  1142. cdf_nbuf_t netbuf;
  1143. int tx_resources;
  1144. A_STATUS status = A_OK;
  1145. uint32_t data_attr = 0;
  1146. if (pPacket) {
  1147. AR_DEBUG_ASSERT(pPacket->Endpoint < ENDPOINT_MAX);
  1148. pEndpoint = &target->EndPoint[pPacket->Endpoint];
  1149. /* add HTC_FRAME_HDR in the initial fragment */
  1150. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1151. pHtcHdr = (HTC_FRAME_HDR *) cdf_nbuf_get_frag_vaddr(netbuf, 0);
  1152. AR_DEBUG_ASSERT(pHtcHdr);
  1153. HTC_WRITE32(pHtcHdr,
  1154. SM(pPacket->ActualLength,
  1155. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->PktInfo.
  1156. AsTx.SendFlags,
  1157. HTC_FRAME_HDR_FLAGS)
  1158. | SM(pPacket->Endpoint, HTC_FRAME_HDR_ENDPOINTID));
  1159. /*
  1160. * If the HIF pipe for the data endpoint is polled rather than
  1161. * interrupt-driven, this is a good point to check whether any
  1162. * data previously sent through the HIF pipe have finished being
  1163. * sent.
  1164. * Since this may result in callbacks to htc_tx_completion_handler,
  1165. * which can take the HTC tx lock, make the hif_send_complete_check
  1166. * call before acquiring the HTC tx lock.
  1167. * Call hif_send_complete_check directly, rather than calling
  1168. * htc_send_complete_check, and call the PollTimerStart separately
  1169. * after calling hif_send_head, so the timer will be started to
  1170. * check for completion of the new outstanding download (in the
  1171. * unexpected event that other polling calls don't catch it).
  1172. */
  1173. if (pEndpoint->ul_is_polled) {
  1174. htc_send_complete_poll_timer_stop(pEndpoint);
  1175. hif_send_complete_check(pEndpoint->target->hif_dev,
  1176. pEndpoint->UL_PipeID, 0);
  1177. }
  1178. LOCK_HTC_TX(target);
  1179. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  1180. pEndpoint->SeqNo++;
  1181. HTC_WRITE32(((A_UINT32 *) pHtcHdr) + 1,
  1182. SM(pPacket->PktInfo.AsTx.SeqNo,
  1183. HTC_FRAME_HDR_CONTROLBYTES1));
  1184. /* append new packet to pEndpoint->TxQueue */
  1185. HTC_PACKET_ENQUEUE(&pEndpoint->TxQueue, pPacket);
  1186. #ifdef ENABLE_BUNDLE_TX
  1187. if (HTC_ENABLE_BUNDLE(target) && (more_data)) {
  1188. UNLOCK_HTC_TX(target);
  1189. return A_OK;
  1190. }
  1191. #endif
  1192. } else {
  1193. LOCK_HTC_TX(target);
  1194. pEndpoint = &target->EndPoint[1];
  1195. }
  1196. /* increment tx processing count on entry */
  1197. cdf_atomic_inc(&pEndpoint->TxProcessCount);
  1198. if (cdf_atomic_read(&pEndpoint->TxProcessCount) > 1) {
  1199. /*
  1200. * Another thread or task is draining the TX queues on this endpoint.
  1201. * That thread will reset the tx processing count when the queue is
  1202. * drained.
  1203. */
  1204. cdf_atomic_dec(&pEndpoint->TxProcessCount);
  1205. UNLOCK_HTC_TX(target);
  1206. return A_OK;
  1207. }
  1208. /***** beyond this point only 1 thread may enter ******/
  1209. INIT_HTC_PACKET_QUEUE(&sendQueue);
  1210. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1211. #if DEBUG_CREDIT
  1212. int cred = pEndpoint->TxCredits;
  1213. #endif
  1214. get_htc_send_packets_credit_based(target, pEndpoint, &sendQueue);
  1215. #if DEBUG_CREDIT
  1216. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1217. if (cred - pEndpoint->TxCredits > 0) {
  1218. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1219. (" <HTC> Decrease EP%d %d - %d = %d credits.\n",
  1220. pEndpoint->Id, cred,
  1221. cred - pEndpoint->TxCredits,
  1222. pEndpoint->TxCredits));
  1223. }
  1224. }
  1225. #endif
  1226. UNLOCK_HTC_TX(target);
  1227. }
  1228. #ifdef ENABLE_BUNDLE_TX
  1229. else if (HTC_ENABLE_BUNDLE(target)) {
  1230. /* Dequeue max packets from endpoint tx queue */
  1231. get_htc_send_packets(target, pEndpoint, &sendQueue,
  1232. HTC_MAX_TX_BUNDLE_SEND_LIMIT);
  1233. UNLOCK_HTC_TX(target);
  1234. }
  1235. #endif
  1236. else {
  1237. /*
  1238. * Now drain the endpoint TX queue for transmission as long as we have
  1239. * enough transmit resources
  1240. */
  1241. tx_resources =
  1242. hif_get_free_queue_number(target->hif_dev,
  1243. pEndpoint->UL_PipeID);
  1244. get_htc_send_packets(target, pEndpoint, &sendQueue, tx_resources);
  1245. UNLOCK_HTC_TX(target);
  1246. }
  1247. NBUF_UPDATE_TX_PKT_COUNT(netbuf, NBUF_TX_PKT_HTC);
  1248. DPTRACE(cdf_dp_trace(netbuf, CDF_DP_TRACE_HTC_PACKET_PTR_RECORD,
  1249. (uint8_t *)(cdf_nbuf_data(netbuf)),
  1250. sizeof(cdf_nbuf_data(netbuf))));
  1251. /* send what we can */
  1252. while (true) {
  1253. #if defined(HIF_USB) || defined(HIF_SDIO)
  1254. #ifdef ENABLE_BUNDLE_TX
  1255. if (HTC_ENABLE_BUNDLE(target) &&
  1256. HTC_PACKET_QUEUE_DEPTH(&sendQueue) >=
  1257. HTC_MIN_MSG_PER_BUNDLE) {
  1258. htc_issue_packets_bundle(target, pEndpoint, &sendQueue);
  1259. }
  1260. #endif
  1261. #endif
  1262. pPacket = htc_packet_dequeue(&sendQueue);
  1263. if (pPacket == NULL) {
  1264. break;
  1265. }
  1266. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  1267. LOCK_HTC_TX(target);
  1268. /* store in look up queue to match completions */
  1269. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  1270. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  1271. pEndpoint->ul_outstanding_cnt++;
  1272. UNLOCK_HTC_TX(target);
  1273. status = hif_send_head(target->hif_dev,
  1274. pEndpoint->UL_PipeID,
  1275. pEndpoint->Id,
  1276. HTC_HDR_LENGTH + pPacket->ActualLength,
  1277. netbuf, data_attr);
  1278. #if DEBUG_BUNDLE
  1279. cdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
  1280. pEndpoint->Id,
  1281. pEndpoint->TxCreditSize,
  1282. HTC_HDR_LENGTH + pPacket->ActualLength);
  1283. #endif
  1284. if (cdf_unlikely(A_FAILED(status))) {
  1285. LOCK_HTC_TX(target);
  1286. pEndpoint->ul_outstanding_cnt--;
  1287. /* remove this packet from the tx completion queue */
  1288. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  1289. /*
  1290. * Don't bother reclaiming credits - HTC flow control
  1291. * is not applicable to tx data.
  1292. * In LL systems, there is no download flow control,
  1293. * since there's virtually no download delay.
  1294. * In HL systems, the txrx SW explicitly performs the
  1295. * tx flow control.
  1296. */
  1297. /* pEndpoint->TxCredits += pPacket->PktInfo.AsTx.CreditsUsed; */
  1298. /* put this frame back at the front of the sendQueue */
  1299. HTC_PACKET_ENQUEUE_TO_HEAD(&sendQueue, pPacket);
  1300. /* put the sendQueue back at the front of pEndpoint->TxQueue */
  1301. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxQueue,
  1302. &sendQueue);
  1303. UNLOCK_HTC_TX(target);
  1304. break; /* still need to reset TxProcessCount */
  1305. }
  1306. }
  1307. /* done with this endpoint, we can clear the count */
  1308. cdf_atomic_init(&pEndpoint->TxProcessCount);
  1309. if (pEndpoint->ul_is_polled) {
  1310. /*
  1311. * Start a cleanup timer to poll for download completion.
  1312. * The download completion should be noticed promptly from
  1313. * other polling calls, but the timer provides a safety net
  1314. * in case other polling calls don't occur as expected.
  1315. */
  1316. htc_send_complete_poll_timer_start(pEndpoint);
  1317. }
  1318. return status;
  1319. }
  1320. #endif /*ATH_11AC_TXCOMPACT */
  1321. /*
  1322. * In the adapted HIF layer, cdf_nbuf_t are passed between HIF and HTC, since upper layers expects
  1323. * HTC_PACKET containers we use the completed netbuf and lookup its corresponding HTC packet buffer
  1324. * from a lookup list.
  1325. * This is extra overhead that can be fixed by re-aligning HIF interfaces with HTC.
  1326. *
  1327. */
  1328. static HTC_PACKET *htc_lookup_tx_packet(HTC_TARGET *target,
  1329. HTC_ENDPOINT *pEndpoint,
  1330. cdf_nbuf_t netbuf)
  1331. {
  1332. HTC_PACKET *pPacket = NULL;
  1333. HTC_PACKET *pFoundPacket = NULL;
  1334. HTC_PACKET_QUEUE lookupQueue;
  1335. INIT_HTC_PACKET_QUEUE(&lookupQueue);
  1336. LOCK_HTC_TX(target);
  1337. /* mark that HIF has indicated the send complete for another packet */
  1338. pEndpoint->ul_outstanding_cnt--;
  1339. /* Dequeue first packet directly because of in-order completion */
  1340. pPacket = htc_packet_dequeue(&pEndpoint->TxLookupQueue);
  1341. if (cdf_unlikely(!pPacket)) {
  1342. UNLOCK_HTC_TX(target);
  1343. return NULL;
  1344. }
  1345. if (netbuf == (cdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1346. UNLOCK_HTC_TX(target);
  1347. return pPacket;
  1348. } else {
  1349. HTC_PACKET_ENQUEUE(&lookupQueue, pPacket);
  1350. }
  1351. /*
  1352. * Move TX lookup queue to temp queue because most of packets that are not index 0
  1353. * are not top 10 packets.
  1354. */
  1355. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&lookupQueue,
  1356. &pEndpoint->TxLookupQueue);
  1357. UNLOCK_HTC_TX(target);
  1358. ITERATE_OVER_LIST_ALLOW_REMOVE(&lookupQueue.QueueHead, pPacket,
  1359. HTC_PACKET, ListLink) {
  1360. if (NULL == pPacket) {
  1361. pFoundPacket = pPacket;
  1362. break;
  1363. }
  1364. /* check for removal */
  1365. if (netbuf ==
  1366. (cdf_nbuf_t) GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket)) {
  1367. /* found it */
  1368. HTC_PACKET_REMOVE(&lookupQueue, pPacket);
  1369. pFoundPacket = pPacket;
  1370. break;
  1371. }
  1372. }
  1373. ITERATE_END;
  1374. LOCK_HTC_TX(target);
  1375. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&pEndpoint->TxLookupQueue,
  1376. &lookupQueue);
  1377. UNLOCK_HTC_TX(target);
  1378. return pFoundPacket;
  1379. }
  1380. CDF_STATUS htc_tx_completion_handler(void *Context,
  1381. cdf_nbuf_t netbuf, unsigned int EpID,
  1382. uint32_t toeplitz_hash_result)
  1383. {
  1384. HTC_TARGET *target = (HTC_TARGET *) Context;
  1385. HTC_ENDPOINT *pEndpoint;
  1386. HTC_PACKET *pPacket;
  1387. #ifdef USB_HIF_SINGLE_PIPE_DATA_SCHED
  1388. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] =
  1389. { ENDPOINT_5, ENDPOINT_4, ENDPOINT_2, ENDPOINT_3 };
  1390. int epidIdx;
  1391. A_UINT16 resourcesThresh[DATA_EP_SIZE]; /* urb resources */
  1392. A_UINT16 resources;
  1393. A_UINT16 resourcesMax;
  1394. #endif
  1395. pEndpoint = &target->EndPoint[EpID];
  1396. target->TX_comp_cnt++;
  1397. do {
  1398. pPacket = htc_lookup_tx_packet(target, pEndpoint, netbuf);
  1399. if (NULL == pPacket) {
  1400. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1401. ("HTC TX lookup failed!\n"));
  1402. /* may have already been flushed and freed */
  1403. netbuf = NULL;
  1404. break;
  1405. }
  1406. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_BUNDLED) {
  1407. HTC_PACKET *pPacketTemp;
  1408. HTC_PACKET_QUEUE *pQueueSave =
  1409. (HTC_PACKET_QUEUE *) pPacket->pContext;
  1410. HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQueueSave,
  1411. pPacketTemp) {
  1412. pPacket->Status = A_OK;
  1413. send_packet_completion(target, pPacketTemp);
  1414. }
  1415. HTC_PACKET_QUEUE_ITERATE_END;
  1416. free_htc_bundle_packet(target, pPacket);
  1417. return CDF_STATUS_SUCCESS;
  1418. }
  1419. /* will be giving this buffer back to upper layers */
  1420. netbuf = NULL;
  1421. pPacket->Status = CDF_STATUS_SUCCESS;
  1422. send_packet_completion(target, pPacket);
  1423. } while (false);
  1424. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  1425. /* note: when using TX credit flow, the re-checking of queues happens
  1426. * when credits flow back from the target.
  1427. * in the non-TX credit case, we recheck after the packet completes */
  1428. htc_try_send(target, pEndpoint, NULL);
  1429. }
  1430. return CDF_STATUS_SUCCESS;
  1431. }
  1432. /* callback when TX resources become available */
  1433. void htc_tx_resource_avail_handler(void *context, A_UINT8 pipeID)
  1434. {
  1435. int i;
  1436. HTC_TARGET *target = (HTC_TARGET *) context;
  1437. HTC_ENDPOINT *pEndpoint = NULL;
  1438. for (i = 0; i < ENDPOINT_MAX; i++) {
  1439. pEndpoint = &target->EndPoint[i];
  1440. if (pEndpoint->ServiceID != 0) {
  1441. if (pEndpoint->UL_PipeID == pipeID) {
  1442. break;
  1443. }
  1444. }
  1445. }
  1446. if (i >= ENDPOINT_MAX) {
  1447. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1448. ("Invalid pipe indicated for TX resource avail : %d!\n",
  1449. pipeID));
  1450. return;
  1451. }
  1452. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1453. ("HIF indicated more resources for pipe:%d \n",
  1454. pipeID));
  1455. htc_try_send(target, pEndpoint, NULL);
  1456. }
  1457. /* flush endpoint TX queue */
  1458. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  1459. HTC_TX_TAG Tag)
  1460. {
  1461. HTC_PACKET *pPacket;
  1462. LOCK_HTC_TX(target);
  1463. while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1464. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  1465. if (pPacket) {
  1466. /* let the sender know the packet was not delivered */
  1467. pPacket->Status = A_ECANCELED;
  1468. send_packet_completion(target, pPacket);
  1469. }
  1470. }
  1471. UNLOCK_HTC_TX(target);
  1472. }
  1473. /* HTC API to flush an endpoint's TX queue*/
  1474. void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
  1475. HTC_TX_TAG Tag)
  1476. {
  1477. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1478. HTC_ENDPOINT *pEndpoint = &target->EndPoint[Endpoint];
  1479. if (pEndpoint->ServiceID == 0) {
  1480. AR_DEBUG_ASSERT(false);
  1481. /* not in use.. */
  1482. return;
  1483. }
  1484. htc_flush_endpoint_tx(target, pEndpoint, Tag);
  1485. }
  1486. /* HTC API to indicate activity to the credit distribution function */
  1487. void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
  1488. HTC_ENDPOINT_ID Endpoint, A_BOOL Active)
  1489. {
  1490. /* TODO */
  1491. }
  1492. A_BOOL htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
  1493. {
  1494. return true;
  1495. }
  1496. /* process credit reports and call distribution function */
  1497. void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
  1498. int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
  1499. {
  1500. int i;
  1501. HTC_ENDPOINT *pEndpoint;
  1502. int totalCredits = 0;
  1503. A_UINT8 rpt_credits, rpt_ep_id;
  1504. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1505. ("+htc_process_credit_rpt, Credit Report Entries:%d \n",
  1506. NumEntries));
  1507. /* lock out TX while we update credits */
  1508. LOCK_HTC_TX(target);
  1509. for (i = 0; i < NumEntries; i++, pRpt++) {
  1510. rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
  1511. if (rpt_ep_id >= ENDPOINT_MAX) {
  1512. AR_DEBUG_ASSERT(false);
  1513. break;
  1514. }
  1515. rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
  1516. pEndpoint = &target->EndPoint[rpt_ep_id];
  1517. #if DEBUG_CREDIT
  1518. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1519. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1520. (" <HTC> Increase EP%d %d + %d = %d credits\n",
  1521. rpt_ep_id, pEndpoint->TxCredits,
  1522. rpt_credits,
  1523. pEndpoint->TxCredits + rpt_credits));
  1524. }
  1525. #endif
  1526. #ifdef HTC_EP_STAT_PROFILING
  1527. INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
  1528. INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
  1529. if (FromEndpoint == rpt_ep_id) {
  1530. /* this credit report arrived on the same endpoint indicating it arrived in an RX
  1531. * packet */
  1532. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
  1533. rpt_credits);
  1534. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
  1535. } else if (FromEndpoint == ENDPOINT_0) {
  1536. /* this credit arrived on endpoint 0 as a NULL message */
  1537. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
  1538. rpt_credits);
  1539. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
  1540. } else {
  1541. /* arrived on another endpoint */
  1542. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
  1543. rpt_credits);
  1544. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
  1545. }
  1546. #endif
  1547. #if defined(HIF_USB)
  1548. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5) {
  1549. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] =
  1550. { ENDPOINT_5, ENDPOINT_4, ENDPOINT_2, ENDPOINT_3 };
  1551. int epid_idx;
  1552. target->avail_tx_credits += rpt_credits;
  1553. for (epid_idx = 0; epid_idx < DATA_EP_SIZE; epid_idx++) {
  1554. pEndpoint = &target->EndPoint[eid[epid_idx]];
  1555. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1556. break;
  1557. }
  1558. }
  1559. UNLOCK_HTC_TX(target);
  1560. htc_try_send(target, pEndpoint, NULL);
  1561. LOCK_HTC_TX(target);
  1562. } else {
  1563. pEndpoint->TxCredits += rpt_credits;
  1564. if (pEndpoint->TxCredits
  1565. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1566. UNLOCK_HTC_TX(target);
  1567. htc_try_send(target, pEndpoint, NULL);
  1568. LOCK_HTC_TX(target);
  1569. }
  1570. }
  1571. #else
  1572. pEndpoint->TxCredits += rpt_credits;
  1573. if (pEndpoint->ServiceID == WMI_CONTROL_SVC) {
  1574. LOCK_HTC_CREDIT(target);
  1575. htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
  1576. pEndpoint->TxCredits,
  1577. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  1578. TxQueue));
  1579. UNLOCK_HTC_CREDIT(target);
  1580. }
  1581. if (pEndpoint->TxCredits
  1582. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1583. UNLOCK_HTC_TX(target);
  1584. #ifdef ATH_11AC_TXCOMPACT
  1585. htc_try_send(target, pEndpoint, NULL);
  1586. #else
  1587. if (pEndpoint->ServiceID == HTT_DATA_MSG_SVC) {
  1588. htc_send_data_pkt(target, NULL, 0);
  1589. } else {
  1590. htc_try_send(target, pEndpoint, NULL);
  1591. }
  1592. #endif
  1593. LOCK_HTC_TX(target);
  1594. }
  1595. #endif
  1596. totalCredits += rpt_credits;
  1597. }
  1598. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1599. (" Report indicated %d credits to distribute \n",
  1600. totalCredits));
  1601. UNLOCK_HTC_TX(target);
  1602. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt \n"));
  1603. }
  1604. /* function to fetch stats from htc layer*/
  1605. struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
  1606. {
  1607. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1608. return (&(target->htc_pkt_stats));
  1609. }