htc_send.c 53 KB

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