htc_send.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  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_mem.h> /* qdf_mem_malloc */
  29. #include <qdf_nbuf.h> /* qdf_nbuf_t */
  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. uint32_t 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. uint32_t 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,
  293. bundleBuf, data_attr);
  294. if (status != A_OK) {
  295. qdf_print("%s:hif_send_head failed(len=%zu).\n", __func__,
  296. data_len);
  297. }
  298. return status;
  299. }
  300. static void htc_issue_packets_bundle(HTC_TARGET *target,
  301. HTC_ENDPOINT *pEndpoint,
  302. HTC_PACKET_QUEUE *pPktQueue)
  303. {
  304. int i, frag_count, nbytes;
  305. qdf_nbuf_t netbuf, bundleBuf;
  306. unsigned char *pBundleBuffer = NULL;
  307. HTC_PACKET *pPacket = NULL, *pPacketTx = NULL;
  308. HTC_FRAME_HDR *pHtcHdr;
  309. int creditPad, creditRemainder, transferLength, bundlesSpaceRemaining =
  310. 0;
  311. HTC_PACKET_QUEUE *pQueueSave = NULL;
  312. bundlesSpaceRemaining =
  313. target->MaxMsgsPerHTCBundle * pEndpoint->TxCreditSize;
  314. pPacketTx = allocate_htc_bundle_packet(target);
  315. if (!pPacketTx) {
  316. /* good time to panic */
  317. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  318. ("allocate_htc_bundle_packet failed \n"));
  319. AR_DEBUG_ASSERT(false);
  320. return;
  321. }
  322. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  323. pBundleBuffer = qdf_nbuf_data(bundleBuf);
  324. pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
  325. while (1) {
  326. pPacket = htc_packet_dequeue(pPktQueue);
  327. if (pPacket == NULL) {
  328. break;
  329. }
  330. creditPad = 0;
  331. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  332. creditRemainder = transferLength % pEndpoint->TxCreditSize;
  333. if (creditRemainder != 0) {
  334. if (transferLength < pEndpoint->TxCreditSize) {
  335. creditPad =
  336. pEndpoint->TxCreditSize - transferLength;
  337. } else {
  338. creditPad = creditRemainder;
  339. }
  340. transferLength += creditPad;
  341. }
  342. if (bundlesSpaceRemaining < transferLength) {
  343. /* send out previous buffer */
  344. htc_send_bundled_netbuf(target, pEndpoint, pBundleBuffer,
  345. pPacketTx);
  346. if (HTC_PACKET_QUEUE_DEPTH(pPktQueue) <
  347. HTC_MIN_MSG_PER_BUNDLE) {
  348. return;
  349. }
  350. bundlesSpaceRemaining =
  351. target->MaxMsgsPerHTCBundle *
  352. pEndpoint->TxCreditSize;
  353. pPacketTx = allocate_htc_bundle_packet(target);
  354. if (!pPacketTx) {
  355. /* good time to panic */
  356. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  357. ("allocate_htc_bundle_packet failed \n"));
  358. AR_DEBUG_ASSERT(false);
  359. return;
  360. }
  361. bundleBuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacketTx);
  362. pBundleBuffer = qdf_nbuf_data(bundleBuf);
  363. pQueueSave = (HTC_PACKET_QUEUE *) pPacketTx->pContext;
  364. }
  365. bundlesSpaceRemaining -= transferLength;
  366. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  367. pHtcHdr = (HTC_FRAME_HDR *) qdf_nbuf_get_frag_vaddr(netbuf, 0);
  368. HTC_WRITE32(pHtcHdr,
  369. SM(pPacket->ActualLength,
  370. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->PktInfo.
  371. AsTx.
  372. SendFlags |
  373. HTC_FLAGS_SEND_BUNDLE,
  374. HTC_FRAME_HDR_FLAGS)
  375. | SM(pPacket->Endpoint, HTC_FRAME_HDR_ENDPOINTID));
  376. HTC_WRITE32((uint32_t *) pHtcHdr + 1,
  377. SM(pPacket->PktInfo.AsTx.SeqNo,
  378. HTC_FRAME_HDR_CONTROLBYTES1) | SM(creditPad,
  379. HTC_FRAME_HDR_RESERVED));
  380. pHtcHdr->reserved = creditPad;
  381. frag_count = qdf_nbuf_get_num_frags(netbuf);
  382. nbytes = pPacket->ActualLength + HTC_HDR_LENGTH;
  383. for (i = 0; i < frag_count && nbytes > 0; i++) {
  384. int frag_len = qdf_nbuf_get_frag_len(netbuf, i);
  385. unsigned char *frag_addr =
  386. qdf_nbuf_get_frag_vaddr(netbuf, i);
  387. if (frag_len > nbytes) {
  388. frag_len = nbytes;
  389. }
  390. qdf_mem_copy(pBundleBuffer, frag_addr, frag_len);
  391. nbytes -= frag_len;
  392. pBundleBuffer += frag_len;
  393. }
  394. HTC_PACKET_ENQUEUE(pQueueSave, pPacket);
  395. pBundleBuffer += creditPad;
  396. }
  397. if (pBundleBuffer != qdf_nbuf_data(bundleBuf)) {
  398. /* send out remaining buffer */
  399. htc_send_bundled_netbuf(target, pEndpoint, pBundleBuffer,
  400. pPacketTx);
  401. } else {
  402. free_htc_bundle_packet(target, pPacketTx);
  403. }
  404. }
  405. #endif /* ENABLE_BUNDLE_TX */
  406. #endif
  407. static A_STATUS htc_issue_packets(HTC_TARGET *target,
  408. HTC_ENDPOINT *pEndpoint,
  409. HTC_PACKET_QUEUE *pPktQueue)
  410. {
  411. A_STATUS status = A_OK;
  412. qdf_nbuf_t netbuf;
  413. HTC_PACKET *pPacket = NULL;
  414. uint16_t payloadLen;
  415. HTC_FRAME_HDR *pHtcHdr;
  416. uint32_t data_attr = 0;
  417. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  418. ("+htc_issue_packets: Queue: %p, Pkts %d \n", pPktQueue,
  419. HTC_PACKET_QUEUE_DEPTH(pPktQueue)));
  420. while (true) {
  421. #if defined(HIF_USB) || defined(HIF_SDIO)
  422. #ifdef ENABLE_BUNDLE_TX
  423. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint) &&
  424. HTC_ENABLE_BUNDLE(target) &&
  425. HTC_PACKET_QUEUE_DEPTH(pPktQueue) >=
  426. HTC_MIN_MSG_PER_BUNDLE) {
  427. htc_issue_packets_bundle(target, pEndpoint, pPktQueue);
  428. }
  429. #endif
  430. #endif
  431. /* if not bundling or there was a packet that could not be placed in a bundle,
  432. * and send it by normal way
  433. */
  434. pPacket = htc_packet_dequeue(pPktQueue);
  435. if (NULL == pPacket) {
  436. /* local queue is fully drained */
  437. break;
  438. }
  439. netbuf = GET_HTC_PACKET_NET_BUF_CONTEXT(pPacket);
  440. AR_DEBUG_ASSERT(netbuf);
  441. /* Non-credit enabled endpoints have been mapped and setup by now,
  442. * so no need to revisit the HTC headers
  443. */
  444. if (IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  445. payloadLen = pPacket->ActualLength;
  446. /* setup HTC frame header */
  447. pHtcHdr = (HTC_FRAME_HDR *)
  448. qdf_nbuf_get_frag_vaddr(netbuf, 0);
  449. AR_DEBUG_ASSERT(pHtcHdr);
  450. HTC_WRITE32(pHtcHdr,
  451. SM(payloadLen,
  452. HTC_FRAME_HDR_PAYLOADLEN) | SM(pPacket->
  453. PktInfo.
  454. AsTx.
  455. SendFlags,
  456. HTC_FRAME_HDR_FLAGS)
  457. | SM(pPacket->Endpoint,
  458. HTC_FRAME_HDR_ENDPOINTID));
  459. HTC_WRITE32(((uint32_t *) pHtcHdr) + 1,
  460. SM(pPacket->PktInfo.AsTx.SeqNo,
  461. HTC_FRAME_HDR_CONTROLBYTES1));
  462. /*
  463. * Now that the HTC frame header has been added, the netbuf can be
  464. * mapped. This only applies to non-data frames, since data frames
  465. * were already mapped as they entered into the driver.
  466. * Check the "FIXUP_NETBUF" flag to see whether this is a data netbuf
  467. * that is already mapped, or a non-data netbuf that needs to be
  468. * mapped.
  469. */
  470. if (pPacket->PktInfo.AsTx.
  471. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) {
  472. qdf_nbuf_map(target->osdev,
  473. GET_HTC_PACKET_NET_BUF_CONTEXT
  474. (pPacket), QDF_DMA_TO_DEVICE);
  475. }
  476. }
  477. LOCK_HTC_TX(target);
  478. /* store in look up queue to match completions */
  479. HTC_PACKET_ENQUEUE(&pEndpoint->TxLookupQueue, pPacket);
  480. INC_HTC_EP_STAT(pEndpoint, TxIssued, 1);
  481. pEndpoint->ul_outstanding_cnt++;
  482. UNLOCK_HTC_TX(target);
  483. hif_send_complete_check(target->hif_dev, pEndpoint->UL_PipeID, false);
  484. status = hif_send_head(target->hif_dev,
  485. pEndpoint->UL_PipeID, pEndpoint->Id,
  486. HTC_HDR_LENGTH + pPacket->ActualLength,
  487. netbuf, data_attr);
  488. #if DEBUG_BUNDLE
  489. qdf_print(" Send single EP%d buffer size:0x%x, total:0x%x.\n",
  490. pEndpoint->Id,
  491. pEndpoint->TxCreditSize,
  492. HTC_HDR_LENGTH + pPacket->ActualLength);
  493. #endif
  494. target->ce_send_cnt++;
  495. if (qdf_unlikely(A_FAILED(status))) {
  496. if (status != A_NO_RESOURCE) {
  497. /* TODO : if more than 1 endpoint maps to the same PipeID it is possible
  498. * to run out of resources in the HIF layer. Don't emit the error */
  499. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  500. ("hif_send Failed status:%d \n",
  501. status));
  502. }
  503. LOCK_HTC_TX(target);
  504. target->ce_send_cnt--;
  505. pEndpoint->ul_outstanding_cnt--;
  506. HTC_PACKET_REMOVE(&pEndpoint->TxLookupQueue, pPacket);
  507. /* reclaim credits */
  508. #if defined(HIF_USB)
  509. if (pEndpoint->Id >= ENDPOINT_2
  510. && pEndpoint->Id <= ENDPOINT_5)
  511. target->avail_tx_credits +=
  512. pPacket->PktInfo.AsTx.CreditsUsed;
  513. else
  514. pEndpoint->TxCredits +=
  515. pPacket->PktInfo.AsTx.CreditsUsed;
  516. #else
  517. pEndpoint->TxCredits +=
  518. pPacket->PktInfo.AsTx.CreditsUsed;
  519. #endif
  520. /* put it back into the callers queue */
  521. HTC_PACKET_ENQUEUE_TO_HEAD(pPktQueue, pPacket);
  522. UNLOCK_HTC_TX(target);
  523. break;
  524. }
  525. /*
  526. * For HTT messages without a response from fw,
  527. * do the runtime put here.
  528. * otherwise runtime put will be done when the fw response comes
  529. */
  530. if (pPacket->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_RUNTIME_PUT)
  531. hif_pm_runtime_put(target->hif_dev);
  532. }
  533. if (qdf_unlikely(A_FAILED(status))) {
  534. #if defined(HIF_USB)
  535. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5)
  536. target->avail_tx_credits +=
  537. pPacket->PktInfo.AsTx.CreditsUsed;
  538. else
  539. pEndpoint->TxCredits +=
  540. pPacket->PktInfo.AsTx.CreditsUsed;
  541. #endif
  542. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  543. ("htc_issue_packets, failed pkt:0x%p status:%d",
  544. pPacket, status));
  545. }
  546. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_issue_packets \n"));
  547. return status;
  548. }
  549. #ifdef FEATURE_RUNTIME_PM
  550. /**
  551. * extract_htc_pm_packtes(): move pm packets from endpoint into queue
  552. * @endpoint: which enpoint to extract packets from
  553. * @queue: a queue to store extracted packets in.
  554. *
  555. * remove pm packets from the endpoint's tx queue.
  556. * queue them into a queue
  557. */
  558. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  559. HTC_PACKET_QUEUE *queue)
  560. {
  561. HTC_PACKET *packet;
  562. /* only WMI endpoint has power management packets */
  563. if (endpoint->service_id != WMI_CONTROL_SVC)
  564. return;
  565. ITERATE_OVER_LIST_ALLOW_REMOVE(&endpoint->TxQueue.QueueHead, packet,
  566. HTC_PACKET, ListLink) {
  567. if (packet->PktInfo.AsTx.Tag == HTC_TX_PACKET_TAG_AUTO_PM) {
  568. HTC_PACKET_REMOVE(&endpoint->TxQueue, packet);
  569. HTC_PACKET_ENQUEUE(queue, packet);
  570. }
  571. } ITERATE_END
  572. }
  573. /**
  574. * queue_htc_pm_packets(): queue pm packets with priority
  575. * @endpoint: enpoint to queue packets to
  576. * @queue: queue of pm packets to enque
  577. *
  578. * suspend resume packets get special treatment & priority.
  579. * need to queue them at the front of the queue.
  580. */
  581. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  582. HTC_PACKET_QUEUE *queue)
  583. {
  584. if (endpoint->service_id != WMI_CONTROL_SVC)
  585. return;
  586. HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(&endpoint->TxQueue, queue);
  587. }
  588. #else
  589. static void extract_htc_pm_packets(HTC_ENDPOINT *endpoint,
  590. HTC_PACKET_QUEUE *queue)
  591. {}
  592. static void queue_htc_pm_packets(HTC_ENDPOINT *endpoint,
  593. HTC_PACKET_QUEUE *queue)
  594. {}
  595. #endif
  596. /* get HTC send packets from the TX queue on an endpoint, based on available credits */
  597. void get_htc_send_packets_credit_based(HTC_TARGET *target,
  598. HTC_ENDPOINT *pEndpoint,
  599. HTC_PACKET_QUEUE *pQueue)
  600. {
  601. int creditsRequired;
  602. int remainder;
  603. uint8_t sendFlags;
  604. HTC_PACKET *pPacket;
  605. unsigned int transferLength;
  606. HTC_PACKET_QUEUE *tx_queue;
  607. HTC_PACKET_QUEUE pm_queue;
  608. bool do_pm_get = false;
  609. /****** NOTE : the TX lock is held when this function is called *****************/
  610. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+get_htc_send_packets_credit_based\n"));
  611. INIT_HTC_PACKET_QUEUE(&pm_queue);
  612. extract_htc_pm_packets(pEndpoint, &pm_queue);
  613. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  614. tx_queue = &pEndpoint->TxQueue;
  615. do_pm_get = true;
  616. } else {
  617. tx_queue = &pm_queue;
  618. }
  619. /* loop until we can grab as many packets out of the queue as we can */
  620. while (true) {
  621. if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
  622. /* bus suspended, runtime resume issued */
  623. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  624. break;
  625. }
  626. sendFlags = 0;
  627. /* get packet at head, but don't remove it */
  628. pPacket = htc_get_pkt_at_head(tx_queue);
  629. if (pPacket == NULL) {
  630. if (do_pm_get)
  631. hif_pm_runtime_put(target->hif_dev);
  632. break;
  633. }
  634. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  635. (" Got head packet:%p , Queue Depth: %d\n",
  636. pPacket,
  637. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  638. transferLength = pPacket->ActualLength + HTC_HDR_LENGTH;
  639. if (transferLength <= pEndpoint->TxCreditSize) {
  640. creditsRequired = 1;
  641. } else {
  642. /* figure out how many credits this message requires */
  643. creditsRequired =
  644. transferLength / pEndpoint->TxCreditSize;
  645. remainder = transferLength % pEndpoint->TxCreditSize;
  646. if (remainder) {
  647. creditsRequired++;
  648. }
  649. }
  650. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  651. (" Credits Required:%d Got:%d\n",
  652. creditsRequired, pEndpoint->TxCredits));
  653. if (pEndpoint->Id == ENDPOINT_0) {
  654. /* endpoint 0 is special, it always has a credit and does not require credit based
  655. * flow control */
  656. creditsRequired = 0;
  657. #if defined(HIF_USB)
  658. } else if (pEndpoint->Id >= ENDPOINT_2
  659. && pEndpoint->Id <= ENDPOINT_5) {
  660. if (target->avail_tx_credits < creditsRequired)
  661. break;
  662. target->avail_tx_credits -= creditsRequired;
  663. if (target->avail_tx_credits < 9) {
  664. /* tell the target we need credits ASAP! */
  665. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  666. INC_HTC_EP_STAT(pEndpoint,
  667. TxCreditLowIndications, 1);
  668. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  669. (" Host Needs Credits \n"));
  670. }
  671. #endif
  672. } else {
  673. if (pEndpoint->TxCredits < creditsRequired) {
  674. #if DEBUG_CREDIT
  675. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  676. (" EP%d, No Credit now. %d < %d\n",
  677. pEndpoint->Id,
  678. pEndpoint->TxCredits,
  679. creditsRequired));
  680. #endif
  681. if (do_pm_get)
  682. hif_pm_runtime_put(target->hif_dev);
  683. break;
  684. }
  685. pEndpoint->TxCredits -= creditsRequired;
  686. INC_HTC_EP_STAT(pEndpoint, TxCreditsConsummed,
  687. creditsRequired);
  688. /* check if we need credits back from the target */
  689. if (pEndpoint->TxCredits <=
  690. pEndpoint->TxCreditsPerMaxMsg) {
  691. /* tell the target we need credits ASAP! */
  692. sendFlags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
  693. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  694. LOCK_HTC_CREDIT(target);
  695. htc_credit_record(HTC_REQUEST_CREDIT,
  696. pEndpoint->TxCredits,
  697. HTC_PACKET_QUEUE_DEPTH
  698. (tx_queue));
  699. UNLOCK_HTC_CREDIT(target);
  700. }
  701. INC_HTC_EP_STAT(pEndpoint,
  702. TxCreditLowIndications, 1);
  703. #if DEBUG_CREDIT
  704. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  705. (" EP%d Needs Credits\n",
  706. pEndpoint->Id));
  707. #endif
  708. }
  709. }
  710. /* now we can fully dequeue */
  711. pPacket = htc_packet_dequeue(tx_queue);
  712. if (pPacket) {
  713. /* save the number of credits this packet consumed */
  714. pPacket->PktInfo.AsTx.CreditsUsed = creditsRequired;
  715. /* save send flags */
  716. pPacket->PktInfo.AsTx.SendFlags = sendFlags;
  717. /* queue this packet into the caller's queue */
  718. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  719. }
  720. }
  721. if (!HTC_QUEUE_EMPTY(&pm_queue))
  722. queue_htc_pm_packets(pEndpoint, &pm_queue);
  723. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  724. ("-get_htc_send_packets_credit_based\n"));
  725. }
  726. void get_htc_send_packets(HTC_TARGET *target,
  727. HTC_ENDPOINT *pEndpoint,
  728. HTC_PACKET_QUEUE *pQueue, int Resources)
  729. {
  730. HTC_PACKET *pPacket;
  731. HTC_PACKET_QUEUE *tx_queue;
  732. HTC_PACKET_QUEUE pm_queue;
  733. bool do_pm_get;
  734. /****** NOTE : the TX lock is held when this function is called *****************/
  735. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  736. ("+get_htc_send_packets %d resources\n", Resources));
  737. INIT_HTC_PACKET_QUEUE(&pm_queue);
  738. extract_htc_pm_packets(pEndpoint, &pm_queue);
  739. if (HTC_QUEUE_EMPTY(&pm_queue)) {
  740. tx_queue = &pEndpoint->TxQueue;
  741. do_pm_get = true;
  742. } else {
  743. tx_queue = &pm_queue;
  744. }
  745. /* loop until we can grab as many packets out of the queue as we can */
  746. while (Resources > 0) {
  747. int num_frags;
  748. if (do_pm_get && hif_pm_runtime_get(target->hif_dev)) {
  749. /* bus suspended, runtime resume issued */
  750. QDF_ASSERT(HTC_PACKET_QUEUE_DEPTH(pQueue) == 0);
  751. break;
  752. }
  753. pPacket = htc_packet_dequeue(tx_queue);
  754. if (pPacket == NULL) {
  755. if (do_pm_get)
  756. hif_pm_runtime_put(target->hif_dev);
  757. break;
  758. }
  759. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  760. (" Got packet:%p , New Queue Depth: %d\n",
  761. pPacket,
  762. HTC_PACKET_QUEUE_DEPTH(tx_queue)));
  763. /* For non-credit path the sequence number is already embedded
  764. * in the constructed HTC header
  765. */
  766. #if 0
  767. pPacket->PktInfo.AsTx.SeqNo = pEndpoint->SeqNo;
  768. pEndpoint->SeqNo++;
  769. #endif
  770. pPacket->PktInfo.AsTx.SendFlags = 0;
  771. pPacket->PktInfo.AsTx.CreditsUsed = 0;
  772. /* queue this packet into the caller's queue */
  773. HTC_PACKET_ENQUEUE(pQueue, pPacket);
  774. /*
  775. * FIX THIS:
  776. * For now, avoid calling qdf_nbuf_get_num_frags before calling
  777. * qdf_nbuf_map, because the MacOS version of qdf_nbuf_t doesn't
  778. * support qdf_nbuf_get_num_frags until after qdf_nbuf_map has
  779. * been done.
  780. * Assume that the non-data netbufs, i.e. the WMI message netbufs,
  781. * consist of a single fragment.
  782. */
  783. num_frags =
  784. (pPacket->PktInfo.AsTx.
  785. Flags & HTC_TX_PACKET_FLAG_FIXUP_NETBUF) ? 1
  786. /* WMI messages are in a single-fragment network buffer */ :
  787. qdf_nbuf_get_num_frags(GET_HTC_PACKET_NET_BUF_CONTEXT
  788. (pPacket));
  789. Resources -= num_frags;
  790. }
  791. if (!HTC_QUEUE_EMPTY(&pm_queue))
  792. queue_htc_pm_packets(pEndpoint, &pm_queue);
  793. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-get_htc_send_packets\n"));
  794. }
  795. static HTC_SEND_QUEUE_RESULT htc_try_send(HTC_TARGET *target,
  796. HTC_ENDPOINT *pEndpoint,
  797. HTC_PACKET_QUEUE *pCallersSendQueue)
  798. {
  799. HTC_PACKET_QUEUE sendQueue; /* temp queue to hold packets at various stages */
  800. HTC_PACKET *pPacket;
  801. int tx_resources;
  802. int overflow;
  803. HTC_SEND_QUEUE_RESULT result = HTC_SEND_QUEUE_OK;
  804. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("+htc_try_send (Queue:%p Depth:%d)\n",
  805. pCallersSendQueue,
  806. (pCallersSendQueue ==
  807. NULL) ? 0 :
  808. HTC_PACKET_QUEUE_DEPTH
  809. (pCallersSendQueue)));
  810. /* init the local send queue */
  811. INIT_HTC_PACKET_QUEUE(&sendQueue);
  812. do {
  813. if (NULL == pCallersSendQueue) {
  814. /* caller didn't provide a queue, just wants us to check queues and send */
  815. break;
  816. }
  817. if (HTC_QUEUE_EMPTY(pCallersSendQueue)) {
  818. /* empty queue */
  819. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  820. HTC_PKT_Q_EMPTY);
  821. result = HTC_SEND_QUEUE_DROP;
  822. break;
  823. }
  824. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue) >=
  825. pEndpoint->MaxTxQueueDepth) {
  826. /* we've already overflowed */
  827. overflow = HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  828. } else {
  829. /* figure out how much we will overflow by */
  830. overflow = HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue);
  831. overflow += HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue);
  832. /* figure out how much we will overflow the TX queue by */
  833. overflow -= pEndpoint->MaxTxQueueDepth;
  834. }
  835. /* if overflow is negative or zero, we are okay */
  836. if (overflow > 0) {
  837. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  838. (" Endpoint %d, TX queue will overflow :%d , Tx Depth:%d, Max:%d \n",
  839. pEndpoint->Id, overflow,
  840. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  841. TxQueue),
  842. pEndpoint->MaxTxQueueDepth));
  843. }
  844. if ((overflow <= 0)
  845. || (pEndpoint->EpCallBacks.EpSendFull == NULL)) {
  846. /* all packets will fit or caller did not provide send full indication handler
  847. * -- just move all of them to the local sendQueue object */
  848. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&sendQueue,
  849. pCallersSendQueue);
  850. } else {
  851. int i;
  852. int goodPkts =
  853. HTC_PACKET_QUEUE_DEPTH(pCallersSendQueue) -
  854. overflow;
  855. A_ASSERT(goodPkts >= 0);
  856. /* we have overflowed, and a callback is provided */
  857. /* dequeue all non-overflow packets into the sendqueue */
  858. for (i = 0; i < goodPkts; i++) {
  859. /* pop off caller's queue */
  860. pPacket = htc_packet_dequeue(pCallersSendQueue);
  861. A_ASSERT(pPacket != NULL);
  862. /* insert into local queue */
  863. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  864. }
  865. /* the caller's queue has all the packets that won't fit */
  866. /* walk through the caller's queue and indicate each one to the send full handler */
  867. ITERATE_OVER_LIST_ALLOW_REMOVE(&pCallersSendQueue->
  868. QueueHead, pPacket,
  869. HTC_PACKET, ListLink) {
  870. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  871. (" Indicating overflowed TX packet: %p \n",
  872. pPacket));
  873. /*
  874. * Remove headroom reserved for HTC_FRAME_HDR before giving
  875. * the packet back to the user via the EpSendFull callback.
  876. */
  877. restore_tx_packet(target, pPacket);
  878. if (pEndpoint->EpCallBacks.
  879. EpSendFull(pEndpoint->EpCallBacks.pContext,
  880. pPacket) == HTC_SEND_FULL_DROP) {
  881. /* callback wants the packet dropped */
  882. INC_HTC_EP_STAT(pEndpoint, TxDropped,
  883. 1);
  884. /* leave this one in the caller's queue for cleanup */
  885. } else {
  886. /* callback wants to keep this packet, remove from caller's queue */
  887. HTC_PACKET_REMOVE(pCallersSendQueue,
  888. pPacket);
  889. /* put it in the send queue */
  890. /* add HTC_FRAME_HDR space reservation again */
  891. qdf_nbuf_push_head
  892. (GET_HTC_PACKET_NET_BUF_CONTEXT
  893. (pPacket), sizeof(HTC_FRAME_HDR));
  894. HTC_PACKET_ENQUEUE(&sendQueue, pPacket);
  895. }
  896. }
  897. ITERATE_END;
  898. if (HTC_QUEUE_EMPTY(&sendQueue)) {
  899. /* no packets made it in, caller will cleanup */
  900. OL_ATH_HTC_PKT_ERROR_COUNT_INCR(target,
  901. HTC_SEND_Q_EMPTY);
  902. result = HTC_SEND_QUEUE_DROP;
  903. break;
  904. }
  905. }
  906. } while (false);
  907. if (result != HTC_SEND_QUEUE_OK) {
  908. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_try_send: \n"));
  909. return result;
  910. }
  911. if (!IS_TX_CREDIT_FLOW_ENABLED(pEndpoint)) {
  912. tx_resources =
  913. hif_get_free_queue_number(target->hif_dev,
  914. pEndpoint->UL_PipeID);
  915. } else {
  916. tx_resources = 0;
  917. }
  918. LOCK_HTC_TX(target);
  919. if (!HTC_QUEUE_EMPTY(&sendQueue)) {
  920. /* transfer packets to tail */
  921. HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(&pEndpoint->TxQueue,
  922. &sendQueue);
  923. A_ASSERT(HTC_QUEUE_EMPTY(&sendQueue));
  924. INIT_HTC_PACKET_QUEUE(&sendQueue);
  925. }
  926. /* increment tx processing count on entry */
  927. if (qdf_atomic_inc_return(&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. /* done with this endpoint, we can clear the count */
  994. qdf_atomic_init(&pEndpoint->TxProcessCount);
  995. UNLOCK_HTC_TX(target);
  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 uint16_t 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. uint16_t resources;
  1007. uint16_t 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(((uint32_t *) 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(((uint32_t *) 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. qdf_nbuf_data_addr(netbuf),
  1227. sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
  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. uint8_t 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(((uint32_t *) 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. qdf_nbuf_data_addr(netbuf),
  1350. sizeof(qdf_nbuf_data(netbuf)), QDF_TX));
  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. uint16_t resourcesThresh[DATA_EP_SIZE]; /* urb resources */
  1493. uint16_t resources;
  1494. uint16_t 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. #if WLAN_FEATURE_FASTPATH
  1536. /**
  1537. * htc_ctrl_msg_cmpl(): checks for tx completion for the endpoint specified
  1538. * @HTC_HANDLE : pointer to the htc target context
  1539. * @htc_ep_id : end point id
  1540. *
  1541. * checks HTC tx completion
  1542. *
  1543. * Return: none
  1544. */
  1545. void htc_ctrl_msg_cmpl(HTC_HANDLE htc_pdev, HTC_ENDPOINT_ID htc_ep_id)
  1546. {
  1547. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(htc_pdev);
  1548. HTC_ENDPOINT *pendpoint = &target->endpoint[htc_ep_id];
  1549. htc_send_complete_check(pendpoint, 1);
  1550. }
  1551. #endif
  1552. /* callback when TX resources become available */
  1553. void htc_tx_resource_avail_handler(void *context, uint8_t pipeID)
  1554. {
  1555. int i;
  1556. HTC_TARGET *target = (HTC_TARGET *) context;
  1557. HTC_ENDPOINT *pEndpoint = NULL;
  1558. for (i = 0; i < ENDPOINT_MAX; i++) {
  1559. pEndpoint = &target->endpoint[i];
  1560. if (pEndpoint->service_id != 0) {
  1561. if (pEndpoint->UL_PipeID == pipeID) {
  1562. break;
  1563. }
  1564. }
  1565. }
  1566. if (i >= ENDPOINT_MAX) {
  1567. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1568. ("Invalid pipe indicated for TX resource avail : %d!\n",
  1569. pipeID));
  1570. return;
  1571. }
  1572. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1573. ("HIF indicated more resources for pipe:%d \n",
  1574. pipeID));
  1575. htc_try_send(target, pEndpoint, NULL);
  1576. }
  1577. #ifdef FEATURE_RUNTIME_PM
  1578. /**
  1579. * htc_kick_queues(): resumes tx transactions of suspended endpoints
  1580. * @context: pointer to the htc target context
  1581. *
  1582. * Iterates throught the enpoints and provides a context to empty queues
  1583. * int the hif layer when they are stalled due to runtime suspend.
  1584. *
  1585. * Return: none
  1586. */
  1587. void htc_kick_queues(void *context)
  1588. {
  1589. int i;
  1590. HTC_TARGET *target = (HTC_TARGET *)context;
  1591. HTC_ENDPOINT *endpoint = NULL;
  1592. for (i = 0; i < ENDPOINT_MAX; i++) {
  1593. endpoint = &target->endpoint[i];
  1594. if (endpoint->service_id == 0)
  1595. continue;
  1596. if (endpoint->EpCallBacks.ep_resume_tx_queue)
  1597. endpoint->EpCallBacks.ep_resume_tx_queue(
  1598. endpoint->EpCallBacks.pContext);
  1599. htc_try_send(target, endpoint, NULL);
  1600. }
  1601. }
  1602. #endif
  1603. /* flush endpoint TX queue */
  1604. void htc_flush_endpoint_tx(HTC_TARGET *target, HTC_ENDPOINT *pEndpoint,
  1605. HTC_TX_TAG Tag)
  1606. {
  1607. HTC_PACKET *pPacket;
  1608. LOCK_HTC_TX(target);
  1609. while (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1610. pPacket = htc_packet_dequeue(&pEndpoint->TxQueue);
  1611. if (pPacket) {
  1612. /* let the sender know the packet was not delivered */
  1613. pPacket->Status = A_ECANCELED;
  1614. send_packet_completion(target, pPacket);
  1615. }
  1616. }
  1617. UNLOCK_HTC_TX(target);
  1618. }
  1619. /* HTC API to flush an endpoint's TX queue*/
  1620. void htc_flush_endpoint(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint,
  1621. HTC_TX_TAG Tag)
  1622. {
  1623. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1624. HTC_ENDPOINT *pEndpoint = &target->endpoint[Endpoint];
  1625. if (pEndpoint->service_id == 0) {
  1626. AR_DEBUG_ASSERT(false);
  1627. /* not in use.. */
  1628. return;
  1629. }
  1630. htc_flush_endpoint_tx(target, pEndpoint, Tag);
  1631. }
  1632. /* HTC API to indicate activity to the credit distribution function */
  1633. void htc_indicate_activity_change(HTC_HANDLE HTCHandle,
  1634. HTC_ENDPOINT_ID Endpoint, bool Active)
  1635. {
  1636. /* TODO */
  1637. }
  1638. bool htc_is_endpoint_active(HTC_HANDLE HTCHandle, HTC_ENDPOINT_ID Endpoint)
  1639. {
  1640. return true;
  1641. }
  1642. /* process credit reports and call distribution function */
  1643. void htc_process_credit_rpt(HTC_TARGET *target, HTC_CREDIT_REPORT *pRpt,
  1644. int NumEntries, HTC_ENDPOINT_ID FromEndpoint)
  1645. {
  1646. int i;
  1647. HTC_ENDPOINT *pEndpoint;
  1648. int totalCredits = 0;
  1649. uint8_t rpt_credits, rpt_ep_id;
  1650. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1651. ("+htc_process_credit_rpt, Credit Report Entries:%d \n",
  1652. NumEntries));
  1653. /* lock out TX while we update credits */
  1654. LOCK_HTC_TX(target);
  1655. for (i = 0; i < NumEntries; i++, pRpt++) {
  1656. rpt_ep_id = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, ENDPOINTID);
  1657. if (rpt_ep_id >= ENDPOINT_MAX) {
  1658. AR_DEBUG_ASSERT(false);
  1659. break;
  1660. }
  1661. rpt_credits = HTC_GET_FIELD(pRpt, HTC_CREDIT_REPORT, CREDITS);
  1662. pEndpoint = &target->endpoint[rpt_ep_id];
  1663. #if DEBUG_CREDIT
  1664. if (ep_debug_mask & (1 << pEndpoint->Id)) {
  1665. AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
  1666. (" <HTC> Increase EP%d %d + %d = %d credits\n",
  1667. rpt_ep_id, pEndpoint->TxCredits,
  1668. rpt_credits,
  1669. pEndpoint->TxCredits + rpt_credits));
  1670. }
  1671. #endif
  1672. #ifdef HTC_EP_STAT_PROFILING
  1673. INC_HTC_EP_STAT(pEndpoint, TxCreditRpts, 1);
  1674. INC_HTC_EP_STAT(pEndpoint, TxCreditsReturned, rpt_credits);
  1675. if (FromEndpoint == rpt_ep_id) {
  1676. /* this credit report arrived on the same endpoint indicating it arrived in an RX
  1677. * packet */
  1678. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromRx,
  1679. rpt_credits);
  1680. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromRx, 1);
  1681. } else if (FromEndpoint == ENDPOINT_0) {
  1682. /* this credit arrived on endpoint 0 as a NULL message */
  1683. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromEp0,
  1684. rpt_credits);
  1685. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromEp0, 1);
  1686. } else {
  1687. /* arrived on another endpoint */
  1688. INC_HTC_EP_STAT(pEndpoint, TxCreditsFromOther,
  1689. rpt_credits);
  1690. INC_HTC_EP_STAT(pEndpoint, TxCreditRptsFromOther, 1);
  1691. }
  1692. #endif
  1693. #if defined(HIF_USB)
  1694. if (pEndpoint->Id >= ENDPOINT_2 && pEndpoint->Id <= ENDPOINT_5) {
  1695. HTC_ENDPOINT_ID eid[DATA_EP_SIZE] = { ENDPOINT_5,
  1696. ENDPOINT_4, ENDPOINT_2, ENDPOINT_3 };
  1697. int epid_idx;
  1698. target->avail_tx_credits += rpt_credits;
  1699. for (epid_idx = 0; epid_idx < DATA_EP_SIZE; epid_idx++) {
  1700. pEndpoint = &target->endpoint[eid[epid_idx]];
  1701. if (HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1702. break;
  1703. }
  1704. }
  1705. UNLOCK_HTC_TX(target);
  1706. htc_try_send(target, pEndpoint, NULL);
  1707. LOCK_HTC_TX(target);
  1708. } else {
  1709. pEndpoint->TxCredits += rpt_credits;
  1710. if (pEndpoint->TxCredits
  1711. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1712. UNLOCK_HTC_TX(target);
  1713. htc_try_send(target, pEndpoint, NULL);
  1714. LOCK_HTC_TX(target);
  1715. }
  1716. }
  1717. #else
  1718. pEndpoint->TxCredits += rpt_credits;
  1719. if (pEndpoint->service_id == WMI_CONTROL_SVC) {
  1720. LOCK_HTC_CREDIT(target);
  1721. htc_credit_record(HTC_PROCESS_CREDIT_REPORT,
  1722. pEndpoint->TxCredits,
  1723. HTC_PACKET_QUEUE_DEPTH(&pEndpoint->
  1724. TxQueue));
  1725. UNLOCK_HTC_CREDIT(target);
  1726. }
  1727. if (pEndpoint->TxCredits
  1728. && HTC_PACKET_QUEUE_DEPTH(&pEndpoint->TxQueue)) {
  1729. UNLOCK_HTC_TX(target);
  1730. #ifdef ATH_11AC_TXCOMPACT
  1731. htc_try_send(target, pEndpoint, NULL);
  1732. #else
  1733. if (pEndpoint->service_id == HTT_DATA_MSG_SVC) {
  1734. htc_send_data_pkt(target, NULL, 0);
  1735. } else {
  1736. htc_try_send(target, pEndpoint, NULL);
  1737. }
  1738. #endif
  1739. LOCK_HTC_TX(target);
  1740. }
  1741. #endif
  1742. totalCredits += rpt_credits;
  1743. }
  1744. AR_DEBUG_PRINTF(ATH_DEBUG_SEND,
  1745. (" Report indicated %d credits to distribute \n",
  1746. totalCredits));
  1747. UNLOCK_HTC_TX(target);
  1748. AR_DEBUG_PRINTF(ATH_DEBUG_SEND, ("-htc_process_credit_rpt \n"));
  1749. }
  1750. /* function to fetch stats from htc layer*/
  1751. struct ol_ath_htc_stats *ieee80211_ioctl_get_htc_stats(HTC_HANDLE HTCHandle)
  1752. {
  1753. HTC_TARGET *target = GET_HTC_TARGET_FROM_HANDLE(HTCHandle);
  1754. return &(target->htc_pkt_stats);
  1755. }