htc_send.c 57 KB

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